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