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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:element-theoradec
25 * @see_also: theoraenc, oggdemux
27 * This element decodes theora streams into raw video
28 * <ulink url="http://www.theora.org/">Theora</ulink> is a royalty-free
29 * video codec maintained by the <ulink url="http://www.xiph.org/">Xiph.org
30 * Foundation</ulink>, based on the VP3 codec.
33 * <title>Example pipeline</title>
35 * gst-launch -v filesrc location=videotestsrc.ogg ! oggdemux ! theoradec ! xvimagesink
36 * ]| This example pipeline will decode an ogg stream and decodes the theora video. Refer to
37 * the theoraenc example to create the ogg file.
40 * Last reviewed on 2006-03-01 (0.10.4)
47 #include "gsttheoradec.h"
48 #include <gst/tag/tag.h>
49 #include <gst/video/video.h>
50 #include <gst/video/gstvideometa.h>
51 #include <gst/video/gstvideopool.h>
53 #define GST_CAT_DEFAULT theoradec_debug
54 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
55 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
57 #define THEORA_DEF_TELEMETRY_MV 0
58 #define THEORA_DEF_TELEMETRY_MBMODE 0
59 #define THEORA_DEF_TELEMETRY_QI 0
60 #define THEORA_DEF_TELEMETRY_BITS 0
66 PROP_TELEMETRY_MBMODE,
71 static GstStaticPadTemplate theora_dec_src_factory =
72 GST_STATIC_PAD_TEMPLATE ("src",
75 GST_STATIC_CAPS ("video/x-raw, "
76 "format = (string) { I420, Y42B, Y444 }, "
77 "framerate = (fraction) [0/1, MAX], "
78 "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
81 static GstStaticPadTemplate theora_dec_sink_factory =
82 GST_STATIC_PAD_TEMPLATE ("sink",
85 GST_STATIC_CAPS ("video/x-theora")
88 #define gst_theora_dec_parent_class parent_class
89 G_DEFINE_TYPE (GstTheoraDec, gst_theora_dec, GST_TYPE_VIDEO_DECODER);
91 static void theora_dec_get_property (GObject * object, guint prop_id,
92 GValue * value, GParamSpec * pspec);
93 static void theora_dec_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
96 static gboolean theora_dec_start (GstVideoDecoder * decoder);
97 static gboolean theora_dec_stop (GstVideoDecoder * decoder);
98 static gboolean theora_dec_set_format (GstVideoDecoder * decoder,
99 GstVideoCodecState * state);
100 static gboolean theora_dec_reset (GstVideoDecoder * decoder, gboolean hard);
101 static GstFlowReturn theora_dec_parse (GstVideoDecoder * decoder,
102 GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos);
103 static GstFlowReturn theora_dec_handle_frame (GstVideoDecoder * decoder,
104 GstVideoCodecFrame * frame);
105 static gboolean theora_dec_decide_allocation (GstVideoDecoder * decoder,
108 static GstFlowReturn theora_dec_decode_buffer (GstTheoraDec * dec,
109 GstBuffer * buf, GstVideoCodecFrame * frame);
112 gst_theora_dec_ctl_is_supported (int req)
114 /* should return TH_EFAULT or TH_EINVAL if supported, and TH_EIMPL if not */
115 return (th_decode_ctl (NULL, req, NULL, 0) != TH_EIMPL);
119 gst_theora_dec_class_init (GstTheoraDecClass * klass)
121 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
122 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
123 GstVideoDecoderClass *video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
125 gobject_class->set_property = theora_dec_set_property;
126 gobject_class->get_property = theora_dec_get_property;
128 if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_MV)) {
129 g_object_class_install_property (gobject_class, PROP_TELEMETRY_MV,
130 g_param_spec_int ("visualize-motion-vectors",
131 "Visualize motion vectors",
132 "Show motion vector selection overlaid on image. "
133 "Value gives a mask for motion vector (MV) modes to show",
134 0, 0xffff, THEORA_DEF_TELEMETRY_MV,
135 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138 if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_MBMODE)) {
139 g_object_class_install_property (gobject_class, PROP_TELEMETRY_MBMODE,
140 g_param_spec_int ("visualize-macroblock-modes",
141 "Visualize macroblock modes",
142 "Show macroblock mode selection overlaid on image. "
143 "Value gives a mask for macroblock (MB) modes to show",
144 0, 0xffff, THEORA_DEF_TELEMETRY_MBMODE,
145 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148 if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_QI)) {
149 g_object_class_install_property (gobject_class, PROP_TELEMETRY_QI,
150 g_param_spec_int ("visualize-quantization-modes",
151 "Visualize adaptive quantization modes",
152 "Show adaptive quantization mode selection overlaid on image. "
153 "Value gives a mask for quantization (QI) modes to show",
154 0, 0xffff, THEORA_DEF_TELEMETRY_QI,
155 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158 if (gst_theora_dec_ctl_is_supported (TH_DECCTL_SET_TELEMETRY_BITS)) {
159 /* FIXME: make this a boolean instead? The value scales the bars so
160 * they're less wide. Default is to use full width, and anything else
161 * doesn't seem particularly useful, since the smaller bars just disappear
162 * then (they almost disappear for a value of 2 already). */
163 g_object_class_install_property (gobject_class, PROP_TELEMETRY_BITS,
164 g_param_spec_int ("visualize-bit-usage",
165 "Visualize bitstream usage breakdown",
166 "Sets the bitstream breakdown visualization mode. "
167 "Values influence the width of the bit usage bars to show",
168 0, 0xff, THEORA_DEF_TELEMETRY_BITS,
169 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172 gst_element_class_add_pad_template (element_class,
173 gst_static_pad_template_get (&theora_dec_src_factory));
174 gst_element_class_add_pad_template (element_class,
175 gst_static_pad_template_get (&theora_dec_sink_factory));
176 gst_element_class_set_details_simple (element_class,
177 "Theora video decoder", "Codec/Decoder/Video",
178 "decode raw theora streams to raw YUV video",
179 "Benjamin Otte <otte@gnome.org>, Wim Taymans <wim@fluendo.com>");
181 video_decoder_class->start = GST_DEBUG_FUNCPTR (theora_dec_start);
182 video_decoder_class->stop = GST_DEBUG_FUNCPTR (theora_dec_stop);
183 video_decoder_class->reset = GST_DEBUG_FUNCPTR (theora_dec_reset);
184 video_decoder_class->set_format = GST_DEBUG_FUNCPTR (theora_dec_set_format);
185 video_decoder_class->parse = GST_DEBUG_FUNCPTR (theora_dec_parse);
186 video_decoder_class->handle_frame =
187 GST_DEBUG_FUNCPTR (theora_dec_handle_frame);
188 video_decoder_class->decide_allocation =
189 GST_DEBUG_FUNCPTR (theora_dec_decide_allocation);
191 GST_DEBUG_CATEGORY_INIT (theoradec_debug, "theoradec", 0, "Theora decoder");
195 gst_theora_dec_init (GstTheoraDec * dec)
197 dec->telemetry_mv = THEORA_DEF_TELEMETRY_MV;
198 dec->telemetry_mbmode = THEORA_DEF_TELEMETRY_MBMODE;
199 dec->telemetry_qi = THEORA_DEF_TELEMETRY_QI;
200 dec->telemetry_bits = THEORA_DEF_TELEMETRY_BITS;
202 /* input is packetized,
203 * but is not marked that way so data gets parsed and keyframes marked */
207 gst_theora_dec_reset (GstTheoraDec * dec)
209 dec->need_keyframe = TRUE;
210 dec->can_crop = FALSE;
214 theora_dec_start (GstVideoDecoder * decoder)
216 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
218 GST_DEBUG_OBJECT (dec, "start");
219 th_info_clear (&dec->info);
220 th_comment_clear (&dec->comment);
221 GST_DEBUG_OBJECT (dec, "Setting have_header to FALSE");
222 dec->have_header = FALSE;
223 gst_theora_dec_reset (dec);
229 theora_dec_stop (GstVideoDecoder * decoder)
231 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
233 GST_DEBUG_OBJECT (dec, "stop");
234 th_info_clear (&dec->info);
235 th_comment_clear (&dec->comment);
236 th_setup_free (dec->setup);
238 th_decode_free (dec->decoder);
240 gst_theora_dec_reset (dec);
242 gst_tag_list_free (dec->tags);
249 /* FIXME : Do we want to handle hard resets differently ? */
251 theora_dec_reset (GstVideoDecoder * bdec, gboolean hard)
253 gst_theora_dec_reset (GST_THEORA_DEC (bdec));
258 theora_dec_parse (GstVideoDecoder * decoder,
259 GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos)
264 av = gst_adapter_available (adapter);
266 data = gst_adapter_map (adapter, 1);
267 /* check for keyframe; must not be header packet */
268 if (!(data[0] & 0x80) && (data[0] & 0x40) == 0)
269 GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
270 gst_adapter_unmap (adapter);
272 /* and pass along all */
273 gst_video_decoder_add_to_frame (decoder, av);
274 return gst_video_decoder_have_frame (decoder);
279 theora_dec_set_format (GstVideoDecoder * bdec, GstVideoCodecState * state)
283 dec = GST_THEORA_DEC (bdec);
285 /* Keep a copy of the input state */
286 if (dec->input_state)
287 gst_video_codec_state_unref (dec->input_state);
288 dec->input_state = gst_video_codec_state_ref (state);
290 /* FIXME : Interesting, we always accept any kind of caps ? */
291 if (state->codec_data) {
298 buffer = state->codec_data;
299 gst_buffer_map (buffer, &minfo, GST_MAP_READ);
303 data = (guint8 *) minfo.data;
309 psize = (data[0] << 8) | data[1];
315 /* make sure we don't read too much */
316 psize = MIN (psize, size);
318 buf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, psize);
320 /* first buffer is a discont buffer */
322 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
324 /* now feed it to the decoder we can ignore the error */
325 theora_dec_decode_buffer (dec, buf, NULL);
326 gst_buffer_unref (buf);
334 gst_buffer_unmap (buffer, &minfo);
337 GST_DEBUG_OBJECT (dec, "Done");
343 theora_handle_comment_packet (GstTheoraDec * dec, ogg_packet * packet)
345 gchar *encoder = NULL;
348 GST_DEBUG_OBJECT (dec, "parsing comment packet");
351 gst_tag_list_from_vorbiscomment (packet->packet, packet->bytes,
352 (guint8 *) "\201theora", 7, &encoder);
355 GST_ERROR_OBJECT (dec, "couldn't decode comments");
356 list = gst_tag_list_new_empty ();
359 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
360 GST_TAG_ENCODER, encoder, NULL);
363 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
364 GST_TAG_ENCODER_VERSION, dec->info.version_major,
365 GST_TAG_VIDEO_CODEC, "Theora", NULL);
367 if (dec->info.target_bitrate > 0) {
368 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
369 GST_TAG_BITRATE, dec->info.target_bitrate,
370 GST_TAG_NOMINAL_BITRATE, dec->info.target_bitrate, NULL);
374 gst_tag_list_free (dec->tags);
381 theora_handle_type_packet (GstTheoraDec * dec, ogg_packet * packet)
383 gint par_num, par_den;
384 GstFlowReturn ret = GST_FLOW_OK;
385 GstVideoCodecState *state;
387 GstVideoInfo *info = &dec->input_state->info;
389 GST_DEBUG_OBJECT (dec, "fps %d/%d, PAR %d/%d",
390 dec->info.fps_numerator, dec->info.fps_denominator,
391 dec->info.aspect_numerator, dec->info.aspect_denominator);
394 * the info.aspect_* values reflect PAR;
395 * 0:x and x:0 are allowed and can be interpreted as 1:1.
397 par_num = GST_VIDEO_INFO_PAR_N (info);
398 par_den = GST_VIDEO_INFO_PAR_D (info);
400 /* If we have a default PAR, see if the decoder specified a different one */
401 if (par_num == 1 && par_den == 1 &&
402 (dec->info.aspect_numerator != 0 && dec->info.aspect_denominator != 0)) {
403 par_num = dec->info.aspect_numerator;
404 par_den = dec->info.aspect_denominator;
408 * width/height : dimension of the encoded frame
409 * pic_width/pic_height : dimension of the visible part
410 * pic_x/pic_y : offset in encoded frame where visible part starts
412 GST_DEBUG_OBJECT (dec, "dimension %dx%d, PAR %d/%d", dec->info.pic_width,
413 dec->info.pic_height, par_num, par_den);
414 GST_DEBUG_OBJECT (dec, "frame dimension %dx%d, offset %d:%d",
415 dec->info.pic_width, dec->info.pic_height,
416 dec->info.pic_x, dec->info.pic_y);
418 switch (dec->info.pixel_fmt) {
420 fmt = GST_VIDEO_FORMAT_I420;
423 fmt = GST_VIDEO_FORMAT_Y42B;
426 fmt = GST_VIDEO_FORMAT_Y444;
429 goto unsupported_format;
432 GST_VIDEO_INFO_WIDTH (info) = dec->info.pic_width;
433 GST_VIDEO_INFO_HEIGHT (info) = dec->info.pic_height;
435 /* Ensure correct offsets in chroma for formats that need it
436 * by rounding the offset. libtheora will add proper pixels,
437 * so no need to handle them ourselves. */
438 if (dec->info.pic_x & 1 && dec->info.pixel_fmt != TH_PF_444) {
439 GST_VIDEO_INFO_WIDTH (info)++;
441 if (dec->info.pic_y & 1 && dec->info.pixel_fmt == TH_PF_420) {
442 GST_VIDEO_INFO_HEIGHT (info)++;
445 GST_DEBUG_OBJECT (dec, "after fixup frame dimension %dx%d, offset %d:%d",
446 info->width, info->height, dec->info.pic_x, dec->info.pic_y);
449 dec->decoder = th_decode_alloc (&dec->info, dec->setup);
451 if (th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_MV,
452 &dec->telemetry_mv, sizeof (dec->telemetry_mv)) != TH_EIMPL) {
453 GST_WARNING_OBJECT (dec, "Could not enable MV visualisation");
455 if (th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_MBMODE,
456 &dec->telemetry_mbmode, sizeof (dec->telemetry_mbmode)) != TH_EIMPL) {
457 GST_WARNING_OBJECT (dec, "Could not enable MB mode visualisation");
459 if (th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_QI,
460 &dec->telemetry_qi, sizeof (dec->telemetry_qi)) != TH_EIMPL) {
461 GST_WARNING_OBJECT (dec, "Could not enable QI mode visualisation");
463 if (th_decode_ctl (dec->decoder, TH_DECCTL_SET_TELEMETRY_BITS,
464 &dec->telemetry_bits, sizeof (dec->telemetry_bits)) != TH_EIMPL) {
465 GST_WARNING_OBJECT (dec, "Could not enable BITS mode visualisation");
468 /* Create the output state */
469 dec->output_state = state =
470 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec), fmt,
471 info->width, info->height, dec->input_state);
473 /* FIXME : Do we still need to set fps/par now that we pass the reference input stream ? */
474 state->info.fps_n = dec->info.fps_numerator;
475 state->info.fps_d = dec->info.fps_denominator;
476 state->info.par_n = par_num;
477 state->info.par_d = par_den;
479 /* these values are for all versions of the colorspace specified in the
481 state->info.chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
482 state->info.colorimetry.range = GST_VIDEO_COLOR_RANGE_16_235;
483 state->info.colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT601;
484 state->info.colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
485 switch (dec->info.colorspace) {
486 case TH_CS_ITU_REC_470M:
487 state->info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
489 case TH_CS_ITU_REC_470BG:
490 state->info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
493 state->info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
497 dec->have_header = TRUE;
499 /* FIXME : Put this on the next outgoing frame */
502 gst_pad_push_event (GST_VIDEO_DECODER (dec)->srcpad,
503 gst_event_new_tag (dec->tags));
512 GST_ERROR_OBJECT (dec, "Invalid pixel format %d", dec->info.pixel_fmt);
513 return GST_FLOW_ERROR;
518 theora_handle_header_packet (GstTheoraDec * dec, ogg_packet * packet)
523 GST_DEBUG_OBJECT (dec, "parsing header packet");
525 ret = th_decode_headerin (&dec->info, &dec->comment, &dec->setup, packet);
527 goto header_read_error;
529 switch (packet->packet[0]) {
531 res = theora_handle_comment_packet (dec, packet);
534 res = theora_handle_type_packet (dec, packet);
538 g_warning ("unknown theora header packet found");
540 /* nothing special, this is the identification header */
549 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
550 (NULL), ("couldn't read header packet"));
551 return GST_FLOW_ERROR;
555 /* Allocate buffer and copy image data into Y444 format */
557 theora_handle_image (GstTheoraDec * dec, th_ycbcr_buffer buf,
558 GstVideoCodecFrame * frame)
560 GstVideoDecoder *decoder = GST_VIDEO_DECODER (dec);
561 gint width, height, stride;
562 GstFlowReturn result;
565 GstVideoFrame vframe;
566 GstVideoCropMeta *crop;
567 gint pic_width, pic_height;
568 gint offset_x, offset_y;
570 result = gst_video_decoder_alloc_output_frame (decoder, frame);
572 if (G_UNLIKELY (result != GST_FLOW_OK)) {
573 GST_DEBUG_OBJECT (dec, "could not get buffer, reason: %s",
574 gst_flow_get_name (result));
578 if (!dec->can_crop) {
579 /* we need to crop the hard way */
580 offset_x = dec->info.pic_x;
581 offset_y = dec->info.pic_y;
582 pic_width = dec->info.pic_width;
583 pic_height = dec->info.pic_height;
584 /* Ensure correct offsets in chroma for formats that need it
585 * by rounding the offset. libtheora will add proper pixels,
586 * so no need to handle them ourselves. */
587 if (offset_x & 1 && dec->info.pixel_fmt != TH_PF_444)
589 if (offset_y & 1 && dec->info.pixel_fmt == TH_PF_420)
592 /* copy the whole frame */
595 pic_width = dec->info.frame_width;
596 pic_height = dec->info.frame_height;
598 if (dec->info.pic_width != dec->info.frame_width ||
599 dec->info.pic_height != dec->info.frame_height ||
600 dec->info.pic_x != 0 || dec->info.pic_y != 0) {
601 crop = gst_buffer_add_video_crop_meta (frame->output_buffer);
603 /* we can do things slightly more efficient when we know that
604 * downstream understands clipping */
605 crop->x = dec->info.pic_x;
606 crop->y = dec->info.pic_y;
607 crop->width = dec->info.pic_width;
608 crop->height = dec->info.pic_height;
612 /* if only libtheora would allow us to give it a destination frame */
613 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, dec,
614 "doing unavoidable video frame copy");
616 if (G_UNLIKELY (!gst_video_frame_map (&vframe, &dec->output_state->info,
617 frame->output_buffer, GST_MAP_WRITE)))
620 for (comp = 0; comp < 3; comp++) {
622 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vframe.info.finfo, comp, pic_width);
624 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vframe.info.finfo, comp,
626 stride = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, comp);
627 dest = GST_VIDEO_FRAME_COMP_DATA (&vframe, comp);
629 src = buf[comp].data;
630 src += ((height == pic_height) ? offset_y : offset_y / 2)
632 src += (width == pic_width) ? offset_x : offset_x / 2;
634 for (i = 0; i < height; i++) {
635 memcpy (dest, src, width);
638 src += buf[comp].stride;
641 gst_video_frame_unmap (&vframe);
646 GST_DEBUG_OBJECT (dec, "could not map video frame");
647 return GST_FLOW_ERROR;
652 theora_handle_data_packet (GstTheoraDec * dec, ogg_packet * packet,
653 GstVideoCodecFrame * frame)
655 /* normal data packet */
658 GstFlowReturn result;
661 if (G_UNLIKELY (!dec->have_header))
662 goto not_initialized;
664 /* the second most significant bit of the first data byte is cleared
665 * for keyframes. We can only check it if it's not a zero-length packet. */
666 keyframe = packet->bytes && ((packet->packet[0] & 0x40) == 0);
667 if (G_UNLIKELY (keyframe)) {
668 GST_DEBUG_OBJECT (dec, "we have a keyframe");
669 dec->need_keyframe = FALSE;
670 } else if (G_UNLIKELY (dec->need_keyframe)) {
674 GST_DEBUG_OBJECT (dec, "parsing data packet");
676 /* this does the decoding */
677 if (G_UNLIKELY (th_decode_packetin (dec->decoder, packet, &gp) < 0))
681 (gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (dec),
685 /* this does postprocessing and set up the decoded frame
686 * pointers in our yuv variable */
687 if (G_UNLIKELY (th_decode_ycbcr_out (dec->decoder, buf) < 0))
690 if (G_UNLIKELY ((buf[0].width != dec->info.frame_width)
691 || (buf[0].height != dec->info.frame_height)))
692 goto wrong_dimensions;
694 result = theora_handle_image (dec, buf, frame);
695 if (result != GST_FLOW_OK)
703 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
704 (NULL), ("no header sent yet"));
705 return GST_FLOW_ERROR;
709 GST_WARNING_OBJECT (dec, "dropping frame because we need a keyframe");
710 return GST_VIDEO_DECODER_FLOW_NEED_DATA;
714 GST_WARNING_OBJECT (dec, "dropping frame because of QoS");
715 return GST_VIDEO_DECODER_FLOW_NEED_DATA;
719 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
720 (NULL), ("theora decoder did not decode data packet"));
721 return GST_FLOW_ERROR;
725 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
726 (NULL), ("couldn't read out YUV image"));
727 return GST_FLOW_ERROR;
731 GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, FORMAT,
732 (NULL), ("dimensions of image do not match header"));
733 return GST_FLOW_ERROR;
738 theora_dec_decode_buffer (GstTheoraDec * dec, GstBuffer * buf,
739 GstVideoCodecFrame * frame)
742 GstFlowReturn result = GST_FLOW_OK;
745 /* make ogg_packet out of the buffer */
746 gst_buffer_map (buf, &minfo, GST_MAP_READ);
747 packet.packet = minfo.data;
748 packet.bytes = minfo.size;
749 packet.granulepos = -1;
750 packet.packetno = 0; /* we don't really care */
751 packet.b_o_s = dec->have_header ? 0 : 1;
752 /* EOS does not matter for the decoder */
755 GST_LOG_OBJECT (dec, "decode buffer of size %ld", packet.bytes);
757 GST_DEBUG_OBJECT (dec, "header=%02x", packet.bytes ? packet.packet[0] : -1);
759 /* switch depending on packet type. A zero byte packet is always a data
760 * packet; we don't dereference it in that case. */
761 if (packet.bytes && packet.packet[0] & 0x80) {
762 if (dec->have_header) {
763 GST_WARNING_OBJECT (GST_OBJECT (dec), "Ignoring header");
766 result = theora_handle_header_packet (dec, &packet);
767 /* header packets are not meant to be displayed */
768 /* FIXME : This is a temporary hack. The proper fix would be to
769 * not call _finish_frame() for these types of packets */
770 GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
771 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
773 result = theora_handle_data_packet (dec, &packet, frame);
777 gst_buffer_unmap (buf, &minfo);
783 theora_dec_handle_frame (GstVideoDecoder * bdec, GstVideoCodecFrame * frame)
788 dec = GST_THEORA_DEC (bdec);
790 res = theora_dec_decode_buffer (dec, frame->input_buffer, frame);
791 if (res == GST_FLOW_OK)
792 res = gst_video_decoder_finish_frame (bdec, frame);
798 theora_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
800 GstTheoraDec *dec = GST_THEORA_DEC (decoder);
801 GstVideoCodecState *state = gst_video_decoder_get_output_state (decoder);
804 guint size, min, max;
805 GstStructure *config;
807 if (gst_query_get_n_allocation_pools (query) > 0) {
808 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
811 size = MAX (size, state->info.size);
815 size = state->info.size;
820 /* no downstream pool, make our own */
822 pool = gst_video_buffer_pool_new ();
825 config = gst_buffer_pool_get_config (pool);
826 if (gst_query_has_allocation_meta (query, GST_VIDEO_META_API_TYPE)) {
827 gst_buffer_pool_config_add_option (config,
828 GST_BUFFER_POOL_OPTION_VIDEO_META);
832 gst_query_has_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE);
835 GstVideoInfo info = state->info;
837 /* Calculate uncropped size */
838 gst_video_info_set_format (&info, info.finfo->format, dec->info.frame_width,
839 dec->info.frame_height);
840 size = MAX (size, info.size);
843 gst_buffer_pool_set_config (pool, config);
846 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
848 gst_query_add_allocation_pool (query, pool, size, min, max);
851 gst_object_unref (pool);
853 return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
858 theora_dec_set_property (GObject * object, guint prop_id,
859 const GValue * value, GParamSpec * pspec)
861 GstTheoraDec *dec = GST_THEORA_DEC (object);
864 case PROP_TELEMETRY_MV:
865 dec->telemetry_mv = g_value_get_int (value);
867 case PROP_TELEMETRY_MBMODE:
868 dec->telemetry_mbmode = g_value_get_int (value);
870 case PROP_TELEMETRY_QI:
871 dec->telemetry_qi = g_value_get_int (value);
873 case PROP_TELEMETRY_BITS:
874 dec->telemetry_bits = g_value_get_int (value);
877 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
883 theora_dec_get_property (GObject * object, guint prop_id,
884 GValue * value, GParamSpec * pspec)
886 GstTheoraDec *dec = GST_THEORA_DEC (object);
889 case PROP_TELEMETRY_MV:
890 g_value_set_int (value, dec->telemetry_mv);
892 case PROP_TELEMETRY_MBMODE:
893 g_value_set_int (value, dec->telemetry_mbmode);
895 case PROP_TELEMETRY_QI:
896 g_value_set_int (value, dec->telemetry_qi);
898 case PROP_TELEMETRY_BITS:
899 g_value_set_int (value, dec->telemetry_bits);
902 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
908 gst_theora_dec_register (GstPlugin * plugin)
910 return gst_element_register (plugin, "theoradec",
911 GST_RANK_PRIMARY, GST_TYPE_THEORA_DEC);