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>
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.
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.
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.
24 * SECTION:element-theoradec
26 * @see_also: theoraenc, oggdemux
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.
35 * gst-launch-1.0 -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! videoconvert ! videoscale ! autovideosink
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.
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>
52 #define GST_CAT_DEFAULT theoradec_debug
53 GST_DEBUG_CATEGORY_STATIC (theoradec_debug);
54 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
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
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
70 PROP_TELEMETRY_MBMODE,
75 static GstStaticPadTemplate theora_dec_src_factory =
76 GST_STATIC_PAD_TEMPLATE ("src",
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 ]")
85 static GstStaticPadTemplate theora_dec_sink_factory =
86 GST_STATIC_PAD_TEMPLATE ("sink",
89 GST_STATIC_CAPS ("video/x-theora")
92 #define gst_theora_dec_parent_class parent_class
93 G_DEFINE_TYPE (GstTheoraDec, gst_theora_dec, GST_TYPE_VIDEO_DECODER);
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);
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,
112 static GstFlowReturn theora_dec_decode_buffer (GstTheoraDec * dec,
113 GstBuffer * buf, GstVideoCodecFrame * frame);
116 gst_theora_dec_ctl_is_supported (int req)
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);
123 gst_theora_dec_class_init (GstTheoraDecClass * klass)
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);
129 gobject_class->set_property = theora_dec_set_property;
130 gobject_class->get_property = theora_dec_get_property;
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));
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));
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));
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));
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>");
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);
194 GST_DEBUG_CATEGORY_INIT (theoradec_debug, "theoradec", 0, "Theora decoder");
195 GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
199 gst_theora_dec_init (GstTheoraDec * dec)
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;
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);
211 gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
213 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (dec));
217 theora_dec_start (GstVideoDecoder * decoder)
219 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
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;
230 theora_dec_stop (GstVideoDecoder * decoder)
232 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
234 GST_DEBUG_OBJECT (dec, "stop");
236 th_info_clear (&dec->info);
237 th_comment_clear (&dec->comment);
239 th_setup_free (dec->setup);
243 th_decode_free (dec->decoder);
247 if (dec->input_state) {
248 gst_video_codec_state_unref (dec->input_state);
249 dec->input_state = NULL;
251 if (dec->output_state) {
252 gst_video_codec_state_unref (dec->output_state);
253 dec->output_state = NULL;
255 dec->can_crop = FALSE;
261 theora_dec_flush (GstVideoDecoder * decoder)
263 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
265 dec->need_keyframe = TRUE;
271 theora_dec_parse (GstVideoDecoder * decoder,
272 GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos)
277 av = gst_adapter_available (adapter);
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");
286 gst_adapter_unmap (adapter);
289 /* and pass along all */
290 gst_video_decoder_add_to_frame (decoder, av);
291 return gst_video_decoder_have_frame (decoder);
296 theora_dec_set_format (GstVideoDecoder * bdec, GstVideoCodecState * state)
300 dec = GST_THEORA_DEC (bdec);
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);
307 /* FIXME : Interesting, we always accept any kind of caps ? */
308 if (state->codec_data) {
315 buffer = state->codec_data;
316 gst_buffer_map (buffer, &minfo, GST_MAP_READ);
320 data = (guint8 *) minfo.data;
326 psize = (data[0] << 8) | data[1];
332 /* make sure we don't read too much */
333 psize = MIN (psize, size);
335 buf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, psize);
337 /* first buffer is a discont buffer */
339 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
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);
351 gst_buffer_unmap (buffer, &minfo);
354 GST_DEBUG_OBJECT (dec, "Done");
360 theora_handle_comment_packet (GstTheoraDec * dec, ogg_packet * packet)
362 gchar *encoder = NULL;
365 GST_DEBUG_OBJECT (dec, "parsing comment packet");
368 gst_tag_list_from_vorbiscomment (packet->packet, packet->bytes,
369 (guint8 *) "\201theora", 7, &encoder);
372 GST_ERROR_OBJECT (dec, "couldn't decode comments");
373 list = gst_tag_list_new_empty ();
376 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
377 GST_TAG_ENCODER, encoder, NULL);
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);
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);
390 gst_video_decoder_merge_tags (GST_VIDEO_DECODER (dec),
391 list, GST_TAG_MERGE_REPLACE);
393 gst_tag_list_unref (list);
399 theora_handle_type_packet (GstTheoraDec * dec)
401 gint par_num, par_den;
402 GstFlowReturn ret = GST_FLOW_OK;
403 GstVideoCodecState *state;
407 info = &dec->input_state->info;
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);
414 * the info.aspect_* values reflect PAR;
415 * 0:x and x:0 are allowed and can be interpreted as 1:1.
417 par_num = GST_VIDEO_INFO_PAR_N (info);
418 par_den = GST_VIDEO_INFO_PAR_D (info);
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;
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
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);
438 switch (dec->info.pixel_fmt) {
440 fmt = GST_VIDEO_FORMAT_I420;
443 fmt = GST_VIDEO_FORMAT_Y42B;
446 fmt = GST_VIDEO_FORMAT_Y444;
449 goto unsupported_format;
452 GST_VIDEO_INFO_WIDTH (info) = dec->info.pic_width;
453 GST_VIDEO_INFO_HEIGHT (info) = dec->info.pic_height;
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)++;
461 if (dec->info.pic_y & 1 && dec->info.pixel_fmt == TH_PF_420) {
462 GST_VIDEO_INFO_HEIGHT (info)++;
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);
468 if (info->width == 0 || info->height == 0)
469 goto invalid_dimensions;
472 dec->decoder = th_decode_alloc (&dec->info, dec->setup);
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");
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");
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");
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");
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);
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;
502 /* these values are for all versions of the colorspace specified in the
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;
512 case TH_CS_ITU_REC_470BG:
513 state->info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
516 state->info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
520 dec->uncropped_info = state->info;
522 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (dec)))
525 dec->have_header = TRUE;
532 GST_ERROR_OBJECT (dec, "Invalid pixel format %d", dec->info.pixel_fmt);
533 return GST_FLOW_ERROR;
538 GST_ERROR_OBJECT (dec, "Failed to negotiate");
539 return GST_FLOW_NOT_NEGOTIATED;
544 GST_ERROR_OBJECT (dec, "Invalid dimensions (width:%d, height:%d)",
545 info->width, info->height);
546 return GST_FLOW_ERROR;
551 theora_handle_header_packet (GstTheoraDec * dec, ogg_packet * packet)
556 GST_DEBUG_OBJECT (dec, "parsing header packet");
558 ret = th_decode_headerin (&dec->info, &dec->comment, &dec->setup, packet);
560 goto header_read_error;
562 switch (packet->packet[0]) {
564 res = theora_handle_comment_packet (dec, packet);
567 res = theora_handle_type_packet (dec);
571 g_warning ("unknown theora header packet found");
573 /* nothing special, this is the identification header */
582 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
583 (NULL), ("couldn't read header packet"));
584 return GST_FLOW_ERROR;
588 #define MIN_NUM_HEADERS 3
590 theoradec_handle_header_caps (GstTheoraDec * dec)
592 GstFlowReturn result = GST_CUSTOM_FLOW_DROP;
594 GstStructure *s = NULL;
595 const GValue *array = NULL;
597 GST_DEBUG_OBJECT (dec, "Looking for Theora headers in caps");
598 caps = gst_pad_get_current_caps (GST_VIDEO_DECODER_SINK_PAD (dec));
600 s = gst_caps_get_structure (caps, 0);
602 array = gst_structure_get_value (s, "streamheader");
605 gst_caps_unref (caps);
607 if (array && (gst_value_array_get_size (array) >= MIN_NUM_HEADERS)) {
608 const GValue *value = NULL;
609 GstBuffer *buf = NULL;
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);
618 GST_LOG_OBJECT (dec, "Submitting header packet");
619 result = theora_dec_decode_buffer (dec, buf, NULL);
627 GST_CUSTOM_FLOW_DROP ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_OK);
632 GST_WARNING_OBJECT (dec, "streamheader array not found");
633 result = GST_FLOW_ERROR;
638 GST_WARNING_OBJECT (dec, "streamheader with null buffer received");
639 result = GST_FLOW_ERROR;
644 /* Allocate buffer and copy image data into Y444 format */
646 theora_handle_image (GstTheoraDec * dec, th_ycbcr_buffer buf,
647 GstVideoCodecFrame * frame)
649 GstVideoDecoder *decoder = GST_VIDEO_DECODER (dec);
650 gint width, height, stride;
651 GstFlowReturn result;
654 GstVideoFrame vframe;
655 gint pic_width, pic_height;
656 gint offset_x, offset_y;
658 result = gst_video_decoder_allocate_output_frame (decoder, frame);
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));
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)
677 if (offset_y & 1 && dec->info.pixel_fmt == TH_PF_420)
680 /* copy the whole frame */
683 pic_width = dec->info.frame_width;
684 pic_height = dec->info.frame_height;
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) {
690 GstVideoCropMeta *cmeta;
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 */
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);
701 /* Just to be sure that the buffer pool doesn't do something
702 * completely weird and we would crash later
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);
708 cmeta = gst_buffer_add_video_crop_meta (frame->output_buffer);
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;
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");
723 if (G_UNLIKELY (!gst_video_frame_map (&vframe, &dec->uncropped_info,
724 frame->output_buffer, GST_MAP_WRITE)))
727 for (comp = 0; comp < 3; comp++) {
729 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vframe.info.finfo, comp, pic_width);
731 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vframe.info.finfo, comp,
733 stride = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, comp);
734 dest = GST_VIDEO_FRAME_COMP_DATA (&vframe, comp);
736 src = buf[comp].data;
737 src += ((height == pic_height) ? offset_y : offset_y / 2)
739 src += (width == pic_width) ? offset_x : offset_x / 2;
741 for (i = 0; i < height; i++) {
742 memcpy (dest, src, width);
745 src += buf[comp].stride;
748 gst_video_frame_unmap (&vframe);
753 GST_DEBUG_OBJECT (dec, "could not map video frame");
754 return GST_FLOW_ERROR;
759 theora_handle_data_packet (GstTheoraDec * dec, ogg_packet * packet,
760 GstVideoCodecFrame * frame)
762 /* normal data packet */
765 GstFlowReturn result;
768 if (G_UNLIKELY (!dec->have_header)) {
769 result = theoradec_handle_header_caps (dec);
770 if (result != GST_FLOW_OK)
771 goto not_initialized;
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)) {
784 GST_DEBUG_OBJECT (dec, "parsing data packet");
786 /* this does the decoding */
787 if (G_UNLIKELY (th_decode_packetin (dec->decoder, packet, &gp) < 0))
791 (gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (dec),
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))
800 if (G_UNLIKELY ((buf[0].width != dec->info.frame_width)
801 || (buf[0].height != dec->info.frame_height)))
802 goto wrong_dimensions;
804 result = theora_handle_image (dec, buf, frame);
811 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
812 (NULL), ("no header sent yet"));
813 return GST_FLOW_ERROR;
817 GST_WARNING_OBJECT (dec, "dropping frame because we need a keyframe");
818 return GST_CUSTOM_FLOW_DROP;
822 GST_WARNING_OBJECT (dec, "dropping frame because of QoS");
823 return GST_CUSTOM_FLOW_DROP;
827 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
828 (NULL), ("theora decoder did not decode data packet"));
829 return GST_FLOW_ERROR;
833 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
834 (NULL), ("couldn't read out YUV image"));
835 return GST_FLOW_ERROR;
839 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, FORMAT,
840 (NULL), ("dimensions of image do not match header"));
841 return GST_FLOW_ERROR;
846 theora_dec_decode_buffer (GstTheoraDec * dec, GstBuffer * buf,
847 GstVideoCodecFrame * frame)
850 GstFlowReturn result = GST_FLOW_OK;
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 */
863 GST_LOG_OBJECT (dec, "decode buffer of size %ld", packet.bytes);
865 GST_DEBUG_OBJECT (dec, "header=%02x", packet.bytes ? packet.packet[0] : -1);
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;
876 if ((result = theora_handle_header_packet (dec, &packet)) != GST_FLOW_OK)
878 result = GST_CUSTOM_FLOW_DROP;
880 result = theora_handle_data_packet (dec, &packet, frame);
884 gst_buffer_unmap (buf, &minfo);
890 theora_dec_handle_frame (GstVideoDecoder * bdec, GstVideoCodecFrame * frame)
895 dec = GST_THEORA_DEC (bdec);
897 res = theora_dec_decode_buffer (dec, frame->input_buffer, frame);
900 res = gst_video_decoder_finish_frame (bdec, frame);
902 case GST_CUSTOM_FLOW_DROP:
903 res = gst_video_decoder_drop_frame (bdec, frame);
906 gst_video_codec_frame_unref (frame);
914 theora_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
916 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
917 GstVideoCodecState *state;
919 guint size, min, max;
920 GstStructure *config;
922 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
926 state = gst_video_decoder_get_output_state (decoder);
928 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
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);
936 gst_query_find_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE,
941 GstVideoInfo *info = &dec->uncropped_info;
944 GST_LOG_OBJECT (decoder, "Using GstVideoCropMeta, uncropped wxh = %dx%d",
945 info->width, info->height);
947 gst_video_info_set_format (info, info->finfo->format, dec->info.frame_width,
948 dec->info.frame_height);
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);
957 gst_buffer_pool_set_config (pool, config);
959 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
961 gst_object_unref (pool);
962 gst_video_codec_state_unref (state);
968 theora_dec_set_property (GObject * object, guint prop_id,
969 const GValue * value, GParamSpec * pspec)
971 GstTheoraDec *dec = GST_THEORA_DEC (object);
974 case PROP_TELEMETRY_MV:
975 dec->telemetry_mv = g_value_get_int (value);
977 case PROP_TELEMETRY_MBMODE:
978 dec->telemetry_mbmode = g_value_get_int (value);
980 case PROP_TELEMETRY_QI:
981 dec->telemetry_qi = g_value_get_int (value);
983 case PROP_TELEMETRY_BITS:
984 dec->telemetry_bits = g_value_get_int (value);
987 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
993 theora_dec_get_property (GObject * object, guint prop_id,
994 GValue * value, GParamSpec * pspec)
996 GstTheoraDec *dec = GST_THEORA_DEC (object);
999 case PROP_TELEMETRY_MV:
1000 g_value_set_int (value, dec->telemetry_mv);
1002 case PROP_TELEMETRY_MBMODE:
1003 g_value_set_int (value, dec->telemetry_mbmode);
1005 case PROP_TELEMETRY_QI:
1006 g_value_set_int (value, dec->telemetry_qi);
1008 case PROP_TELEMETRY_BITS:
1009 g_value_set_int (value, dec->telemetry_bits);
1012 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);