1 /* GStreamer
2 * Copyright (C) 2004 Benjamin Otte <in7y118@public.uni-hamburg.de>
3 * Copyright (c) 2012 Collabora Ltd.
4 * Author : Edward Hervey <edward@collabora.com>
5 * Author : Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 /**
24 * SECTION:element-theoradec
25 * @title: theoradec
26 * @see_also: theoraenc, oggdemux
27 *
28 * This element decodes theora streams into raw video
29 * [Theora](http://www.theora.org/) is a royalty-free
30 * video codec maintained by the [Xiph.org Foundation](http://www.xiph.org/),
31 * based on the VP3 codec.
32 *
33 * ## Example pipeline
34 * |[
35 * gst-launch-1.0 -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! videoconvert ! videoscale ! autovideosink
36 * ]|
37 * This example pipeline will decode an ogg stream and decodes the theora video in it.
38 * Refer to the theoraenc example to create the ogg file.
39 *
40 */
41
42 #ifdef HAVE_CONFIG_H
43 # include "config.h"
44 #endif
45
46 #include "gsttheoradec.h"
47 #include <gst/tag/tag.h>
48 #include <gst/video/video.h>
49 #include <gst/video/gstvideometa.h>
50 #include <gst/video/gstvideopool.h>
51
52 #define GST_CAT_DEFAULT theoradec_debug
53 GST_DEBUG_CATEGORY_STATIC (theoradec_debug);
54 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
55
56 #define THEORA_DEF_TELEMETRY_MV 0
57 #define THEORA_DEF_TELEMETRY_MBMODE 0
58 #define THEORA_DEF_TELEMETRY_QI 0
59 #define THEORA_DEF_TELEMETRY_BITS 0
60
61 /* This was removed from the base class, this is used as a
62 temporary return to signal the need to call _drop_frame,
63 and does not leave theoraenc. */
64 #define GST_CUSTOM_FLOW_DROP GST_FLOW_CUSTOM_SUCCESS_1
65
66 enum
67 {
68 PROP_0,
69 PROP_TELEMETRY_MV,
70 PROP_TELEMETRY_MBMODE,
71 PROP_TELEMETRY_QI,
72 PROP_TELEMETRY_BITS
73 };
74
75 static GstStaticPadTemplate theora_dec_src_factory =
76 GST_STATIC_PAD_TEMPLATE ("src",
77 GST_PAD_SRC,
78 GST_PAD_ALWAYS,
79 GST_STATIC_CAPS ("video/x-raw, "
80 "format = (string) { I420, Y42B, Y444 }, "
81 "framerate = (fraction) [0/1, MAX], "
82 "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
83 );
84
85 static GstStaticPadTemplate theora_dec_sink_factory =
86 GST_STATIC_PAD_TEMPLATE ("sink",
87 GST_PAD_SINK,
88 GST_PAD_ALWAYS,
89 GST_STATIC_CAPS ("video/x-theora")
90 );
91
92 #define gst_theora_dec_parent_class parent_class
93 G_DEFINE_TYPE (GstTheoraDec, gst_theora_dec, GST_TYPE_VIDEO_DECODER);
94 GST_ELEMENT_REGISTER_DEFINE (theoradec, "theoradec",
95 GST_RANK_PRIMARY, GST_TYPE_THEORA_DEC);
96
97 static void theora_dec_get_property (GObject * object, guint prop_id,
98 GValue * value, GParamSpec * pspec);
99 static void theora_dec_set_property (GObject * object, guint prop_id,
100 const GValue * value, GParamSpec * pspec);
101
102 static gboolean theora_dec_start (GstVideoDecoder * decoder);
103 static gboolean theora_dec_stop (GstVideoDecoder * decoder);
104 static gboolean theora_dec_set_format (GstVideoDecoder * decoder,
105 GstVideoCodecState * state);
106 static gboolean theora_dec_flush (GstVideoDecoder * decoder);
107 static GstFlowReturn theora_dec_parse (GstVideoDecoder * decoder,
108 GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos);
109 static GstFlowReturn theora_dec_handle_frame (GstVideoDecoder * decoder,
110 GstVideoCodecFrame * frame);
111 static gboolean theora_dec_decide_allocation (GstVideoDecoder * decoder,
112 GstQuery * query);
113
114 static GstFlowReturn theora_dec_decode_buffer (GstTheoraDec * dec,
115 GstBuffer * buf, GstVideoCodecFrame * frame);
116
117 static gboolean
gst_theora_dec_ctl_is_supported(int req)118 gst_theora_dec_ctl_is_supported (int req)
119 {
120 /* should return TH_EFAULT or TH_EINVAL if supported, and TH_EIMPL if not */
121 return (th_decode_ctl (NULL, req, NULL, 0) != TH_EIMPL);
122 }
123
124 static void
gst_theora_dec_class_init(GstTheoraDecClass * klass)125 gst_theora_dec_class_init (GstTheoraDecClass * klass)
126 {
127 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
128 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
129 GstVideoDecoderClass *video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
130
131 gobject_class->set_property = theora_dec_set_property;
132 gobject_class->get_property = theora_dec_get_property;
133
134 if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_MV)) {
135 g_object_class_install_property (gobject_class, PROP_TELEMETRY_MV,
136 g_param_spec_int ("visualize-motion-vectors",
137 "Visualize motion vectors",
138 "Show motion vector selection overlaid on image. "
139 "Value gives a mask for motion vector (MV) modes to show",
140 0, 0xffff, THEORA_DEF_TELEMETRY_MV,
141 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
142 }
143
144 if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_MBMODE)) {
145 g_object_class_install_property (gobject_class, PROP_TELEMETRY_MBMODE,
146 g_param_spec_int ("visualize-macroblock-modes",
147 "Visualize macroblock modes",
148 "Show macroblock mode selection overlaid on image. "
149 "Value gives a mask for macroblock (MB) modes to show",
150 0, 0xffff, THEORA_DEF_TELEMETRY_MBMODE,
151 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 }
153
154 if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_QI)) {
155 g_object_class_install_property (gobject_class, PROP_TELEMETRY_QI,
156 g_param_spec_int ("visualize-quantization-modes",
157 "Visualize adaptive quantization modes",
158 "Show adaptive quantization mode selection overlaid on image. "
159 "Value gives a mask for quantization (QI) modes to show",
160 0, 0xffff, THEORA_DEF_TELEMETRY_QI,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 }
163
164 if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_BITS)) {
165 /* FIXME: make this a boolean instead? The value scales the bars so
166 * they're less wide. Default is to use full width, and anything else
167 * doesn't seem particularly useful, since the smaller bars just disappear
168 * then (they almost disappear for a value of 2 already). */
169 g_object_class_install_property (gobject_class, PROP_TELEMETRY_BITS,
170 g_param_spec_int ("visualize-bit-usage",
171 "Visualize bitstream usage breakdown",
172 "Sets the bitstream breakdown visualization mode. "
173 "Values influence the width of the bit usage bars to show",
174 0, 0xff, THEORA_DEF_TELEMETRY_BITS,
175 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176 }
177
178 gst_element_class_add_static_pad_template (element_class,
179 &theora_dec_src_factory);
180 gst_element_class_add_static_pad_template (element_class,
181 &theora_dec_sink_factory);
182 gst_element_class_set_static_metadata (element_class, "Theora video decoder",
183 "Codec/Decoder/Video", "decode raw theora streams to raw YUV video",
184 "Benjamin Otte <otte@gnome.org>, Wim Taymans <wim@fluendo.com>");
185
186 video_decoder_class->start = GST_DEBUG_FUNCPTR (theora_dec_start);
187 video_decoder_class->stop = GST_DEBUG_FUNCPTR (theora_dec_stop);
188 video_decoder_class->flush = GST_DEBUG_FUNCPTR (theora_dec_flush);
189 video_decoder_class->set_format = GST_DEBUG_FUNCPTR (theora_dec_set_format);
190 video_decoder_class->parse = GST_DEBUG_FUNCPTR (theora_dec_parse);
191 video_decoder_class->handle_frame =
192 GST_DEBUG_FUNCPTR (theora_dec_handle_frame);
193 video_decoder_class->decide_allocation =
194 GST_DEBUG_FUNCPTR (theora_dec_decide_allocation);
195
196 GST_DEBUG_CATEGORY_INIT (theoradec_debug, "theoradec", 0, "Theora decoder");
197 GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
198 }
199
200 static void
gst_theora_dec_init(GstTheoraDec * dec)201 gst_theora_dec_init (GstTheoraDec * dec)
202 {
203 dec->telemetry_mv = THEORA_DEF_TELEMETRY_MV;
204 dec->telemetry_mbmode = THEORA_DEF_TELEMETRY_MBMODE;
205 dec->telemetry_qi = THEORA_DEF_TELEMETRY_QI;
206 dec->telemetry_bits = THEORA_DEF_TELEMETRY_BITS;
207
208 /* input is packetized,
209 * but is not marked that way so data gets parsed and keyframes marked */
210 gst_video_decoder_set_packetized (GST_VIDEO_DECODER (dec), FALSE);
211 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (dec), TRUE);
212
213 gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
214 (dec), TRUE);
215 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (dec));
216 }
217
218 static gboolean
theora_dec_start(GstVideoDecoder * decoder)219 theora_dec_start (GstVideoDecoder * decoder)
220 {
221 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
222
223 GST_DEBUG_OBJECT (dec, "start");
224 GST_DEBUG_OBJECT (dec, "Setting have_header to FALSE");
225 dec->have_header = FALSE;
226 dec->can_crop = FALSE;
227
228 return TRUE;
229 }
230
231 static gboolean
theora_dec_stop(GstVideoDecoder * decoder)232 theora_dec_stop (GstVideoDecoder * decoder)
233 {
234 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
235
236 GST_DEBUG_OBJECT (dec, "stop");
237
238 th_info_clear (&dec->info);
239 th_comment_clear (&dec->comment);
240 if (dec->setup) {
241 th_setup_free (dec->setup);
242 dec->setup = NULL;
243 }
244 if (dec->decoder) {
245 th_decode_free (dec->decoder);
246 dec->decoder = NULL;
247 }
248
249 if (dec->input_state) {
250 gst_video_codec_state_unref (dec->input_state);
251 dec->input_state = NULL;
252 }
253 if (dec->output_state) {
254 gst_video_codec_state_unref (dec->output_state);
255 dec->output_state = NULL;
256 }
257 dec->can_crop = FALSE;
258
259 return TRUE;
260 }
261
262 static gboolean
theora_dec_flush(GstVideoDecoder * decoder)263 theora_dec_flush (GstVideoDecoder * decoder)
264 {
265 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
266
267 dec->need_keyframe = TRUE;
268
269 return TRUE;
270 }
271
272 static GstFlowReturn
theora_dec_parse(GstVideoDecoder * decoder,GstVideoCodecFrame * frame,GstAdapter * adapter,gboolean at_eos)273 theora_dec_parse (GstVideoDecoder * decoder,
274 GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos)
275 {
276 gint av;
277 const guint8 *data;
278
279 av = gst_adapter_available (adapter);
280
281 if (av > 0) {
282 data = gst_adapter_map (adapter, 1);
283 /* check for keyframe; must not be header packet (0x80 | 0x40) */
284 if (!(data[0] & 0xc0)) {
285 GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
286 GST_LOG_OBJECT (decoder, "Found keyframe");
287 }
288 gst_adapter_unmap (adapter);
289 }
290
291 /* and pass along all */
292 gst_video_decoder_add_to_frame (decoder, av);
293 return gst_video_decoder_have_frame (decoder);
294 }
295
296
297 static gboolean
theora_dec_set_format(GstVideoDecoder * bdec,GstVideoCodecState * state)298 theora_dec_set_format (GstVideoDecoder * bdec, GstVideoCodecState * state)
299 {
300 GstTheoraDec *dec;
301
302 dec = GST_THEORA_DEC (bdec);
303
304 /* Keep a copy of the input state */
305 if (dec->input_state)
306 gst_video_codec_state_unref (dec->input_state);
307 dec->input_state = gst_video_codec_state_ref (state);
308
309 /* FIXME : Interesting, we always accept any kind of caps ? */
310 if (state->codec_data) {
311 GstBuffer *buffer;
312 GstMapInfo minfo;
313 guint8 *data;
314 guint size;
315 guint offset;
316
317 buffer = state->codec_data;
318 gst_buffer_map (buffer, &minfo, GST_MAP_READ);
319
320 offset = 0;
321 size = minfo.size;
322 data = (guint8 *) minfo.data;
323
324 while (size > 2) {
325 guint psize;
326 GstBuffer *buf;
327
328 psize = (data[0] << 8) | data[1];
329 /* skip header */
330 data += 2;
331 size -= 2;
332 offset += 2;
333
334 /* make sure we don't read too much */
335 psize = MIN (psize, size);
336
337 buf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, psize);
338
339 /* first buffer is a discont buffer */
340 if (offset == 2)
341 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
342
343 /* now feed it to the decoder we can ignore the error */
344 theora_dec_decode_buffer (dec, buf, NULL);
345 gst_buffer_unref (buf);
346
347 /* skip the data */
348 size -= psize;
349 data += psize;
350 offset += psize;
351 }
352
353 gst_buffer_unmap (buffer, &minfo);
354 }
355
356 GST_DEBUG_OBJECT (dec, "Done");
357
358 return TRUE;
359 }
360
361 static GstFlowReturn
theora_handle_comment_packet(GstTheoraDec * dec,ogg_packet * packet)362 theora_handle_comment_packet (GstTheoraDec * dec, ogg_packet * packet)
363 {
364 gchar *encoder = NULL;
365 GstTagList *list;
366
367 GST_DEBUG_OBJECT (dec, "parsing comment packet");
368
369 list =
370 gst_tag_list_from_vorbiscomment (packet->packet, packet->bytes,
371 (guint8 *) "\201theora", 7, &encoder);
372
373 if (!list) {
374 GST_ERROR_OBJECT (dec, "couldn't decode comments");
375 list = gst_tag_list_new_empty ();
376 }
377 if (encoder) {
378 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
379 GST_TAG_ENCODER, encoder, NULL);
380 g_free (encoder);
381 }
382 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
383 GST_TAG_ENCODER_VERSION, dec->info.version_major,
384 GST_TAG_VIDEO_CODEC, "Theora", NULL);
385
386 if (dec->info.target_bitrate > 0) {
387 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
388 GST_TAG_BITRATE, dec->info.target_bitrate,
389 GST_TAG_NOMINAL_BITRATE, dec->info.target_bitrate, NULL);
390 }
391
392 gst_video_decoder_merge_tags (GST_VIDEO_DECODER (dec),
393 list, GST_TAG_MERGE_REPLACE);
394
395 gst_tag_list_unref (list);
396
397 return GST_FLOW_OK;
398 }
399
400 static GstFlowReturn
theora_handle_type_packet(GstTheoraDec * dec)401 theora_handle_type_packet (GstTheoraDec * dec)
402 {
403 gint par_num, par_den;
404 GstFlowReturn ret = GST_FLOW_OK;
405 GstVideoCodecState *state;
406 GstVideoFormat fmt;
407 GstVideoInfo *info;
408
409 info = &dec->input_state->info;
410
411 GST_DEBUG_OBJECT (dec, "fps %d/%d, PAR %d/%d",
412 dec->info.fps_numerator, dec->info.fps_denominator,
413 dec->info.aspect_numerator, dec->info.aspect_denominator);
414
415 /* calculate par
416 * the info.aspect_* values reflect PAR;
417 * 0:x and x:0 are allowed and can be interpreted as 1:1.
418 */
419 par_num = GST_VIDEO_INFO_PAR_N (info);
420 par_den = GST_VIDEO_INFO_PAR_D (info);
421
422 /* If we have a default PAR, see if the decoder specified a different one */
423 if (par_num == 1 && par_den == 1 &&
424 (dec->info.aspect_numerator != 0 && dec->info.aspect_denominator != 0)) {
425 par_num = dec->info.aspect_numerator;
426 par_den = dec->info.aspect_denominator;
427 }
428 /* theora has:
429 *
430 * width/height : dimension of the encoded frame
431 * pic_width/pic_height : dimension of the visible part
432 * pic_x/pic_y : offset in encoded frame where visible part starts
433 */
434 GST_DEBUG_OBJECT (dec, "dimension %dx%d, PAR %d/%d", dec->info.pic_width,
435 dec->info.pic_height, par_num, par_den);
436 GST_DEBUG_OBJECT (dec, "frame dimension %dx%d, offset %d:%d",
437 dec->info.pic_width, dec->info.pic_height,
438 dec->info.pic_x, dec->info.pic_y);
439
440 switch (dec->info.pixel_fmt) {
441 case TH_PF_420:
442 fmt = GST_VIDEO_FORMAT_I420;
443 break;
444 case TH_PF_422:
445 fmt = GST_VIDEO_FORMAT_Y42B;
446 break;
447 case TH_PF_444:
448 fmt = GST_VIDEO_FORMAT_Y444;
449 break;
450 default:
451 goto unsupported_format;
452 }
453
454 GST_VIDEO_INFO_WIDTH (info) = dec->info.pic_width;
455 GST_VIDEO_INFO_HEIGHT (info) = dec->info.pic_height;
456
457 /* Ensure correct offsets in chroma for formats that need it
458 * by rounding the offset. libtheora will add proper pixels,
459 * so no need to handle them ourselves. */
460 if (dec->info.pic_x & 1 && dec->info.pixel_fmt != TH_PF_444) {
461 GST_VIDEO_INFO_WIDTH (info)++;
462 }
463 if (dec->info.pic_y & 1 && dec->info.pixel_fmt == TH_PF_420) {
464 GST_VIDEO_INFO_HEIGHT (info)++;
465 }
466
467 GST_DEBUG_OBJECT (dec, "after fixup frame dimension %dx%d, offset %d:%d",
468 info->width, info->height, dec->info.pic_x, dec->info.pic_y);
469
470 if (info->width == 0 || info->height == 0)
471 goto invalid_dimensions;
472
473 /* done */
474 dec->decoder = th_decode_alloc (&dec->info, dec->setup);
475
476 if (dec->telemetry_mv &&
477 th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_MV,
478 &dec->telemetry_mv, sizeof (dec->telemetry_mv)) != TH_EIMPL) {
479 GST_WARNING_OBJECT (dec, "Could not enable MV visualisation");
480 }
481 if (dec->telemetry_mbmode &&
482 th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_MBMODE,
483 &dec->telemetry_mbmode, sizeof (dec->telemetry_mbmode)) != TH_EIMPL) {
484 GST_WARNING_OBJECT (dec, "Could not enable MB mode visualisation");
485 }
486 if (dec->telemetry_qi &&
487 th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_QI,
488 &dec->telemetry_qi, sizeof (dec->telemetry_qi)) != TH_EIMPL) {
489 GST_WARNING_OBJECT (dec, "Could not enable QI mode visualisation");
490 }
491 if (dec->telemetry_bits &&
492 th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_BITS,
493 &dec->telemetry_bits, sizeof (dec->telemetry_bits)) != TH_EIMPL) {
494 GST_WARNING_OBJECT (dec, "Could not enable BITS mode visualisation");
495 }
496
497 /* Create the output state */
498 dec->output_state = state =
499 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec), fmt,
500 info->width, info->height, dec->input_state);
501
502 /* FIXME : Do we still need to set fps/par now that we pass the reference input stream ? */
503 state->info.fps_n = dec->info.fps_numerator;
504 state->info.fps_d = dec->info.fps_denominator;
505 state->info.par_n = par_num;
506 state->info.par_d = par_den;
507
508 /* these values are for all versions of the colorspace specified in the
509 * theora info */
510 state->info.chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
511 state->info.colorimetry.range = GST_VIDEO_COLOR_RANGE_16_235;
512 state->info.colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT601;
513 state->info.colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
514 switch (dec->info.colorspace) {
515 case TH_CS_ITU_REC_470M:
516 state->info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
517 break;
518 case TH_CS_ITU_REC_470BG:
519 state->info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
520 break;
521 default:
522 state->info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
523 break;
524 }
525
526 dec->uncropped_info = state->info;
527
528 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (dec)))
529 goto not_negotiated;
530
531 dec->have_header = TRUE;
532
533 return ret;
534
535 /* ERRORS */
536 unsupported_format:
537 {
538 GST_ERROR_OBJECT (dec, "Invalid pixel format %d", dec->info.pixel_fmt);
539 return GST_FLOW_ERROR;
540 }
541
542 not_negotiated:
543 {
544 GST_ERROR_OBJECT (dec, "Failed to negotiate");
545 return GST_FLOW_NOT_NEGOTIATED;
546 }
547
548 invalid_dimensions:
549 {
550 GST_ERROR_OBJECT (dec, "Invalid dimensions (width:%d, height:%d)",
551 info->width, info->height);
552 return GST_FLOW_ERROR;
553 }
554 }
555
556 static GstFlowReturn
theora_handle_header_packet(GstTheoraDec * dec,ogg_packet * packet)557 theora_handle_header_packet (GstTheoraDec * dec, ogg_packet * packet)
558 {
559 GstFlowReturn res;
560 int ret;
561
562 GST_DEBUG_OBJECT (dec, "parsing header packet");
563
564 ret = th_decode_headerin (&dec->info, &dec->comment, &dec->setup, packet);
565 if (ret < 0)
566 goto header_read_error;
567
568 switch (packet->packet[0]) {
569 case 0x81:
570 res = theora_handle_comment_packet (dec, packet);
571 break;
572 case 0x82:
573 res = theora_handle_type_packet (dec);
574 break;
575 default:
576 /* ignore */
577 GST_WARNING_OBJECT (dec, "unknown theora header packet found");
578 case 0x80:
579 /* nothing special, this is the identification header */
580 res = GST_FLOW_OK;
581 break;
582 }
583 return res;
584
585 /* ERRORS */
586 header_read_error:
587 {
588 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
589 (NULL), ("couldn't read header packet"));
590 return GST_FLOW_ERROR;
591 }
592 }
593
594 #define MIN_NUM_HEADERS 3
595 static GstFlowReturn
theoradec_handle_header_caps(GstTheoraDec * dec)596 theoradec_handle_header_caps (GstTheoraDec * dec)
597 {
598 GstFlowReturn result = GST_CUSTOM_FLOW_DROP;
599 GstCaps *caps;
600 GstStructure *s = NULL;
601 const GValue *array = NULL;
602
603 GST_DEBUG_OBJECT (dec, "Looking for Theora headers in caps");
604 caps = gst_pad_get_current_caps (GST_VIDEO_DECODER_SINK_PAD (dec));
605 if (caps)
606 s = gst_caps_get_structure (caps, 0);
607 if (s)
608 array = gst_structure_get_value (s, "streamheader");
609
610 if (caps)
611 gst_caps_unref (caps);
612
613 if (array && (gst_value_array_get_size (array) >= MIN_NUM_HEADERS)) {
614 const GValue *value = NULL;
615 GstBuffer *buf = NULL;
616 gint i = 0;
617
618 while (result == GST_CUSTOM_FLOW_DROP
619 && i < gst_value_array_get_size (array)) {
620 value = gst_value_array_get_value (array, i);
621 buf = gst_value_get_buffer (value);
622 if (!buf)
623 goto null_buffer;
624 GST_LOG_OBJECT (dec, "Submitting header packet");
625 result = theora_dec_decode_buffer (dec, buf, NULL);
626 i++;
627 }
628 } else
629 goto array_error;
630
631 done:
632 return (result !=
633 GST_CUSTOM_FLOW_DROP ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_OK);
634
635 /* ERRORS */
636 array_error:
637 {
638 GST_WARNING_OBJECT (dec, "streamheader array not found");
639 result = GST_FLOW_ERROR;
640 goto done;
641 }
642 null_buffer:
643 {
644 GST_WARNING_OBJECT (dec, "streamheader with null buffer received");
645 result = GST_FLOW_ERROR;
646 goto done;
647 }
648 }
649
650 /* Allocate buffer and copy image data into Y444 format */
651 static GstFlowReturn
theora_handle_image(GstTheoraDec * dec,th_ycbcr_buffer buf,GstVideoCodecFrame * frame)652 theora_handle_image (GstTheoraDec * dec, th_ycbcr_buffer buf,
653 GstVideoCodecFrame * frame)
654 {
655 GstVideoDecoder *decoder = GST_VIDEO_DECODER (dec);
656 gint width, height, stride;
657 GstFlowReturn result;
658 gint i, comp;
659 guint8 *dest, *src;
660 GstVideoFrame vframe;
661 gint pic_width, pic_height;
662 gint offset_x, offset_y;
663
664 result = gst_video_decoder_allocate_output_frame (decoder, frame);
665
666 if (G_UNLIKELY (result != GST_FLOW_OK)) {
667 GST_DEBUG_OBJECT (dec, "could not get buffer, reason: %s",
668 gst_flow_get_name (result));
669 return result;
670 }
671
672 if (!dec->can_crop) {
673 /* we need to crop the hard way */
674 offset_x = dec->info.pic_x;
675 offset_y = dec->info.pic_y;
676 pic_width = dec->info.pic_width;
677 pic_height = dec->info.pic_height;
678 /* Ensure correct offsets in chroma for formats that need it
679 * by rounding the offset. libtheora will add proper pixels,
680 * so no need to handle them ourselves. */
681 if (offset_x & 1 && dec->info.pixel_fmt != TH_PF_444)
682 offset_x--;
683 if (offset_y & 1 && dec->info.pixel_fmt == TH_PF_420)
684 offset_y--;
685 } else {
686 /* copy the whole frame */
687 offset_x = 0;
688 offset_y = 0;
689 pic_width = dec->info.frame_width;
690 pic_height = dec->info.frame_height;
691
692 if (dec->info.pic_width != dec->info.frame_width ||
693 dec->info.pic_height != dec->info.frame_height ||
694 dec->info.pic_x != 0 || dec->info.pic_y != 0) {
695 GstVideoMeta *vmeta;
696 GstVideoCropMeta *cmeta;
697
698 vmeta = gst_buffer_get_video_meta (frame->output_buffer);
699 /* If the buffer pool didn't add the meta already
700 * we add it ourselves here */
701 if (!vmeta)
702 vmeta = gst_buffer_add_video_meta (frame->output_buffer,
703 GST_VIDEO_FRAME_FLAG_NONE,
704 dec->output_state->info.finfo->format,
705 dec->info.frame_width, dec->info.frame_height);
706
707 /* Just to be sure that the buffer pool doesn't do something
708 * completely weird and we would crash later
709 */
710 g_assert (vmeta->format == dec->output_state->info.finfo->format);
711 g_assert (vmeta->width == dec->info.frame_width);
712 g_assert (vmeta->height == dec->info.frame_height);
713
714 cmeta = gst_buffer_add_video_crop_meta (frame->output_buffer);
715
716 /* we can do things slightly more efficient when we know that
717 * downstream understands clipping */
718 cmeta->x = dec->info.pic_x;
719 cmeta->y = dec->info.pic_y;
720 cmeta->width = dec->info.pic_width;
721 cmeta->height = dec->info.pic_height;
722 }
723 }
724
725 /* if only libtheora would allow us to give it a destination frame */
726 GST_CAT_TRACE_OBJECT (CAT_PERFORMANCE, dec,
727 "doing unavoidable video frame copy");
728
729 if (G_UNLIKELY (!gst_video_frame_map (&vframe, &dec->uncropped_info,
730 frame->output_buffer, GST_MAP_WRITE)))
731 goto invalid_frame;
732
733 for (comp = 0; comp < 3; comp++) {
734 width =
735 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vframe.info.finfo, comp, pic_width);
736 height =
737 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vframe.info.finfo, comp,
738 pic_height);
739 stride = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, comp);
740 dest = GST_VIDEO_FRAME_COMP_DATA (&vframe, comp);
741
742 src = buf[comp].data;
743 src += ((height == pic_height) ? offset_y : offset_y / 2)
744 * buf[comp].stride;
745 src += (width == pic_width) ? offset_x : offset_x / 2;
746
747 for (i = 0; i < height; i++) {
748 memcpy (dest, src, width);
749
750 dest += stride;
751 src += buf[comp].stride;
752 }
753 }
754 gst_video_frame_unmap (&vframe);
755
756 return GST_FLOW_OK;
757 invalid_frame:
758 {
759 GST_DEBUG_OBJECT (dec, "could not map video frame");
760 return GST_FLOW_ERROR;
761 }
762 }
763
764 static GstFlowReturn
theora_handle_data_packet(GstTheoraDec * dec,ogg_packet * packet,GstVideoCodecFrame * frame)765 theora_handle_data_packet (GstTheoraDec * dec, ogg_packet * packet,
766 GstVideoCodecFrame * frame)
767 {
768 /* normal data packet */
769 th_ycbcr_buffer buf;
770 gboolean keyframe;
771 GstFlowReturn result;
772 ogg_int64_t gp;
773
774 if (G_UNLIKELY (!dec->have_header)) {
775 result = theoradec_handle_header_caps (dec);
776 if (result != GST_FLOW_OK)
777 goto not_initialized;
778 }
779
780 /* the second most significant bit of the first data byte is cleared
781 * for keyframes. We can only check it if it's not a zero-length packet. */
782 keyframe = packet->bytes && ((packet->packet[0] & 0x40) == 0);
783 if (G_UNLIKELY (keyframe)) {
784 GST_DEBUG_OBJECT (dec, "we have a keyframe");
785 dec->need_keyframe = FALSE;
786 } else if (G_UNLIKELY (dec->need_keyframe)) {
787 goto dropping;
788 }
789
790 GST_DEBUG_OBJECT (dec, "parsing data packet");
791
792 /* this does the decoding */
793 if (G_UNLIKELY (th_decode_packetin (dec->decoder, packet, &gp) < 0))
794 goto decode_error;
795
796 if (frame &&
797 (gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (dec),
798 frame) < 0))
799 goto dropping_qos;
800
801 /* this does postprocessing and set up the decoded frame
802 * pointers in our yuv variable */
803 if (G_UNLIKELY (th_decode_ycbcr_out (dec->decoder, buf) < 0))
804 goto no_yuv;
805
806 if (G_UNLIKELY ((buf[0].width != dec->info.frame_width)
807 || (buf[0].height != dec->info.frame_height)))
808 goto wrong_dimensions;
809
810 result = theora_handle_image (dec, buf, frame);
811
812 return result;
813
814 /* ERRORS */
815 not_initialized:
816 {
817 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
818 (NULL), ("no header sent yet"));
819 return GST_FLOW_ERROR;
820 }
821 dropping:
822 {
823 GST_WARNING_OBJECT (dec, "dropping frame because we need a keyframe");
824 return GST_CUSTOM_FLOW_DROP;
825 }
826 dropping_qos:
827 {
828 GST_WARNING_OBJECT (dec, "dropping frame because of QoS");
829 return GST_CUSTOM_FLOW_DROP;
830 }
831 decode_error:
832 {
833 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
834 (NULL), ("theora decoder did not decode data packet"));
835 return GST_FLOW_ERROR;
836 }
837 no_yuv:
838 {
839 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
840 (NULL), ("couldn't read out YUV image"));
841 return GST_FLOW_ERROR;
842 }
843 wrong_dimensions:
844 {
845 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, FORMAT,
846 (NULL), ("dimensions of image do not match header"));
847 return GST_FLOW_ERROR;
848 }
849 }
850
851 static GstFlowReturn
theora_dec_decode_buffer(GstTheoraDec * dec,GstBuffer * buf,GstVideoCodecFrame * frame)852 theora_dec_decode_buffer (GstTheoraDec * dec, GstBuffer * buf,
853 GstVideoCodecFrame * frame)
854 {
855 ogg_packet packet;
856 GstFlowReturn result = GST_FLOW_OK;
857 GstMapInfo minfo;
858
859 /* make ogg_packet out of the buffer */
860 gst_buffer_map (buf, &minfo, GST_MAP_READ);
861 packet.packet = minfo.data;
862 packet.bytes = minfo.size;
863 packet.granulepos = -1;
864 packet.packetno = 0; /* we don't really care */
865 packet.b_o_s = dec->have_header ? 0 : 1;
866 /* EOS does not matter for the decoder */
867 packet.e_o_s = 0;
868
869 GST_LOG_OBJECT (dec, "decode buffer of size %ld", packet.bytes);
870
871 GST_DEBUG_OBJECT (dec, "header=%02x", packet.bytes ? packet.packet[0] : -1);
872
873 /* switch depending on packet type. A zero byte packet is always a data
874 * packet; we don't dereference it in that case. */
875 if (packet.bytes && packet.packet[0] & 0x80) {
876 /* header packets are not meant to be displayed - return FLOW_DROP */
877 if (dec->have_header) {
878 GST_WARNING_OBJECT (GST_OBJECT (dec), "Ignoring header");
879 result = GST_CUSTOM_FLOW_DROP;
880 goto done;
881 }
882 if ((result = theora_handle_header_packet (dec, &packet)) != GST_FLOW_OK)
883 goto done;
884 result = GST_CUSTOM_FLOW_DROP;
885 } else {
886 result = theora_handle_data_packet (dec, &packet, frame);
887 }
888
889 done:
890 gst_buffer_unmap (buf, &minfo);
891
892 return result;
893 }
894
895 static GstFlowReturn
theora_dec_handle_frame(GstVideoDecoder * bdec,GstVideoCodecFrame * frame)896 theora_dec_handle_frame (GstVideoDecoder * bdec, GstVideoCodecFrame * frame)
897 {
898 GstTheoraDec *dec;
899 GstFlowReturn res;
900
901 dec = GST_THEORA_DEC (bdec);
902
903 res = theora_dec_decode_buffer (dec, frame->input_buffer, frame);
904 switch (res) {
905 case GST_FLOW_OK:
906 res = gst_video_decoder_finish_frame (bdec, frame);
907 break;
908 case GST_CUSTOM_FLOW_DROP:
909 res = gst_video_decoder_drop_frame (bdec, frame);
910 break;
911 default:
912 gst_video_codec_frame_unref (frame);
913 break;
914 }
915
916 return res;
917 }
918
919 static gboolean
theora_dec_decide_allocation(GstVideoDecoder * decoder,GstQuery * query)920 theora_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
921 {
922 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
923 GstVideoCodecState *state;
924 GstBufferPool *pool;
925 guint size, min, max;
926 GstStructure *config;
927
928 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
929 query))
930 return FALSE;
931
932 state = gst_video_decoder_get_output_state (decoder);
933
934 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
935
936 dec->can_crop = FALSE;
937 config = gst_buffer_pool_get_config (pool);
938 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
939 gst_buffer_pool_config_add_option (config,
940 GST_BUFFER_POOL_OPTION_VIDEO_META);
941 dec->can_crop =
942 gst_query_find_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE,
943 NULL);
944 }
945
946 if (dec->can_crop) {
947 GstVideoInfo *info = &dec->uncropped_info;
948 GstCaps *caps;
949
950 GST_LOG_OBJECT (decoder, "Using GstVideoCropMeta, uncropped wxh = %dx%d",
951 info->width, info->height);
952
953 gst_video_info_set_format (info, info->finfo->format, dec->info.frame_width,
954 dec->info.frame_height);
955
956 /* Calculate uncropped size */
957 size = MAX (size, info->size);
958 caps = gst_video_info_to_caps (info);
959 gst_buffer_pool_config_set_params (config, caps, size, min, max);
960 gst_caps_unref (caps);
961 }
962
963 gst_buffer_pool_set_config (pool, config);
964
965 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
966
967 gst_object_unref (pool);
968 gst_video_codec_state_unref (state);
969
970 return TRUE;
971 }
972
973 static void
theora_dec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)974 theora_dec_set_property (GObject * object, guint prop_id,
975 const GValue * value, GParamSpec * pspec)
976 {
977 GstTheoraDec *dec = GST_THEORA_DEC (object);
978
979 switch (prop_id) {
980 case PROP_TELEMETRY_MV:
981 dec->telemetry_mv = g_value_get_int (value);
982 break;
983 case PROP_TELEMETRY_MBMODE:
984 dec->telemetry_mbmode = g_value_get_int (value);
985 break;
986 case PROP_TELEMETRY_QI:
987 dec->telemetry_qi = g_value_get_int (value);
988 break;
989 case PROP_TELEMETRY_BITS:
990 dec->telemetry_bits = g_value_get_int (value);
991 break;
992 default:
993 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
994 break;
995 }
996 }
997
998 static void
theora_dec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)999 theora_dec_get_property (GObject * object, guint prop_id,
1000 GValue * value, GParamSpec * pspec)
1001 {
1002 GstTheoraDec *dec = GST_THEORA_DEC (object);
1003
1004 switch (prop_id) {
1005 case PROP_TELEMETRY_MV:
1006 g_value_set_int (value, dec->telemetry_mv);
1007 break;
1008 case PROP_TELEMETRY_MBMODE:
1009 g_value_set_int (value, dec->telemetry_mbmode);
1010 break;
1011 case PROP_TELEMETRY_QI:
1012 g_value_set_int (value, dec->telemetry_qi);
1013 break;
1014 case PROP_TELEMETRY_BITS:
1015 g_value_set_int (value, dec->telemetry_bits);
1016 break;
1017 default:
1018 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1019 break;
1020 }
1021 }
1022