theoradec: Update for video decoder API changes
[platform/upstream/gstreamer.git] / ext / theora / gsttheoradec.c
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:element-theoradec
25  * @see_also: theoraenc, oggdemux
26  *
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.
31  *
32  * <refsect2>
33  * <title>Example pipeline</title>
34  * |[
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.
38  * </refsect2>
39  *
40  * Last reviewed on 2006-03-01 (0.10.4)
41  */
42
43 #ifdef HAVE_CONFIG_H
44 #  include "config.h"
45 #endif
46
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>
52
53 #define GST_CAT_DEFAULT theoradec_debug
54 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
55 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
56
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
61
62 enum
63 {
64   PROP_0,
65   PROP_TELEMETRY_MV,
66   PROP_TELEMETRY_MBMODE,
67   PROP_TELEMETRY_QI,
68   PROP_TELEMETRY_BITS
69 };
70
71 static GstStaticPadTemplate theora_dec_src_factory =
72 GST_STATIC_PAD_TEMPLATE ("src",
73     GST_PAD_SRC,
74     GST_PAD_ALWAYS,
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 ]")
79     );
80
81 static GstStaticPadTemplate theora_dec_sink_factory =
82 GST_STATIC_PAD_TEMPLATE ("sink",
83     GST_PAD_SINK,
84     GST_PAD_ALWAYS,
85     GST_STATIC_CAPS ("video/x-theora")
86     );
87
88 #define gst_theora_dec_parent_class parent_class
89 G_DEFINE_TYPE (GstTheoraDec, gst_theora_dec, GST_TYPE_VIDEO_DECODER);
90
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);
95
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,
106     GstQuery * query);
107
108 static GstFlowReturn theora_dec_decode_buffer (GstTheoraDec * dec,
109     GstBuffer * buf, GstVideoCodecFrame * frame);
110
111 static gboolean
112 gst_theora_dec_ctl_is_supported (int req)
113 {
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);
116 }
117
118 static void
119 gst_theora_dec_class_init (GstTheoraDecClass * klass)
120 {
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);
124
125   gobject_class->set_property = theora_dec_set_property;
126   gobject_class->get_property = theora_dec_get_property;
127
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));
136   }
137
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));
146   }
147
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));
156   }
157
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));
170   }
171
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>");
180
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);
190
191   GST_DEBUG_CATEGORY_INIT (theoradec_debug, "theoradec", 0, "Theora decoder");
192 }
193
194 static void
195 gst_theora_dec_init (GstTheoraDec * dec)
196 {
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;
201
202   /* input is packetized,
203    * but is not marked that way so data gets parsed and keyframes marked */
204 }
205
206 static void
207 gst_theora_dec_reset (GstTheoraDec * dec)
208 {
209   dec->need_keyframe = TRUE;
210   dec->can_crop = FALSE;
211 }
212
213 static gboolean
214 theora_dec_start (GstVideoDecoder * decoder)
215 {
216   GstTheoraDec *dec = GST_THEORA_DEC (decoder);
217
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);
224
225   return TRUE;
226 }
227
228 static gboolean
229 theora_dec_stop (GstVideoDecoder * decoder)
230 {
231   GstTheoraDec *dec = GST_THEORA_DEC (decoder);
232
233   GST_DEBUG_OBJECT (dec, "stop");
234   th_info_clear (&dec->info);
235   th_comment_clear (&dec->comment);
236   th_setup_free (dec->setup);
237   dec->setup = NULL;
238   th_decode_free (dec->decoder);
239   dec->decoder = NULL;
240   gst_theora_dec_reset (dec);
241   if (dec->tags) {
242     gst_tag_list_free (dec->tags);
243     dec->tags = NULL;
244   }
245
246   return TRUE;
247 }
248
249 /* FIXME : Do we want to handle hard resets differently ? */
250 static gboolean
251 theora_dec_reset (GstVideoDecoder * bdec, gboolean hard)
252 {
253   gst_theora_dec_reset (GST_THEORA_DEC (bdec));
254   return TRUE;
255 }
256
257 static GstFlowReturn
258 theora_dec_parse (GstVideoDecoder * decoder,
259     GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos)
260 {
261   gint av;
262   const guint8 *data;
263
264   av = gst_adapter_available (adapter);
265
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);
271
272   /* and pass along all */
273   gst_video_decoder_add_to_frame (decoder, av);
274   return gst_video_decoder_have_frame (decoder);
275 }
276
277
278 static gboolean
279 theora_dec_set_format (GstVideoDecoder * bdec, GstVideoCodecState * state)
280 {
281   GstTheoraDec *dec;
282
283   dec = GST_THEORA_DEC (bdec);
284
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);
289
290   /* FIXME : Interesting, we always accept any kind of caps ? */
291   if (state->codec_data) {
292     GstBuffer *buffer;
293     GstMapInfo minfo;
294     guint8 *data;
295     guint size;
296     guint offset;
297
298     buffer = state->codec_data;
299     gst_buffer_map (buffer, &minfo, GST_MAP_READ);
300
301     offset = 0;
302     size = minfo.size;
303     data = (guint8 *) minfo.data;
304
305     while (size > 2) {
306       guint psize;
307       GstBuffer *buf;
308
309       psize = (data[0] << 8) | data[1];
310       /* skip header */
311       data += 2;
312       size -= 2;
313       offset += 2;
314
315       /* make sure we don't read too much */
316       psize = MIN (psize, size);
317
318       buf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, psize);
319
320       /* first buffer is a discont buffer */
321       if (offset == 2)
322         GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
323
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);
327
328       /* skip the data */
329       size -= psize;
330       data += psize;
331       offset += psize;
332     }
333
334     gst_buffer_unmap (buffer, &minfo);
335   }
336
337   GST_DEBUG_OBJECT (dec, "Done");
338
339   return TRUE;
340 }
341
342 static GstFlowReturn
343 theora_handle_comment_packet (GstTheoraDec * dec, ogg_packet * packet)
344 {
345   gchar *encoder = NULL;
346   GstTagList *list;
347
348   GST_DEBUG_OBJECT (dec, "parsing comment packet");
349
350   list =
351       gst_tag_list_from_vorbiscomment (packet->packet, packet->bytes,
352       (guint8 *) "\201theora", 7, &encoder);
353
354   if (!list) {
355     GST_ERROR_OBJECT (dec, "couldn't decode comments");
356     list = gst_tag_list_new_empty ();
357   }
358   if (encoder) {
359     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
360         GST_TAG_ENCODER, encoder, NULL);
361     g_free (encoder);
362   }
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);
366
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);
371   }
372
373   if (dec->tags)
374     gst_tag_list_free (dec->tags);
375   dec->tags = list;
376
377   return GST_FLOW_OK;
378 }
379
380 static GstFlowReturn
381 theora_handle_type_packet (GstTheoraDec * dec, ogg_packet * packet)
382 {
383   gint par_num, par_den;
384   GstFlowReturn ret = GST_FLOW_OK;
385   GstVideoCodecState *state;
386   GstVideoFormat fmt;
387   GstVideoInfo *info = &dec->input_state->info;
388
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);
392
393   /* calculate par
394    * the info.aspect_* values reflect PAR;
395    * 0:x and x:0 are allowed and can be interpreted as 1:1.
396    */
397   par_num = GST_VIDEO_INFO_PAR_N (info);
398   par_den = GST_VIDEO_INFO_PAR_D (info);
399
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;
405   }
406   /* theora has:
407    *
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
411    */
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);
417
418   switch (dec->info.pixel_fmt) {
419     case TH_PF_420:
420       fmt = GST_VIDEO_FORMAT_I420;
421       break;
422     case TH_PF_422:
423       fmt = GST_VIDEO_FORMAT_Y42B;
424       break;
425     case TH_PF_444:
426       fmt = GST_VIDEO_FORMAT_Y444;
427       break;
428     default:
429       goto unsupported_format;
430   }
431
432   GST_VIDEO_INFO_WIDTH (info) = dec->info.pic_width;
433   GST_VIDEO_INFO_HEIGHT (info) = dec->info.pic_height;
434
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)++;
440   }
441   if (dec->info.pic_y & 1 && dec->info.pixel_fmt == TH_PF_420) {
442     GST_VIDEO_INFO_HEIGHT (info)++;
443   }
444
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);
447
448   /* done */
449   dec->decoder = th_decode_alloc (&dec->info, dec->setup);
450
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");
454   }
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");
458   }
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");
462   }
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");
466   }
467
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);
472
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;
478
479   /* these values are for all versions of the colorspace specified in the
480    * theora info */
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;
488       break;
489     case TH_CS_ITU_REC_470BG:
490       state->info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
491       break;
492     default:
493       state->info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
494       break;
495   }
496
497   dec->have_header = TRUE;
498
499   /* FIXME : Put this on the next outgoing frame */
500   /* FIXME :  */
501   if (dec->tags) {
502     gst_pad_push_event (GST_VIDEO_DECODER (dec)->srcpad,
503         gst_event_new_tag (dec->tags));
504     dec->tags = NULL;
505   }
506
507   return ret;
508
509   /* ERRORS */
510 unsupported_format:
511   {
512     GST_ERROR_OBJECT (dec, "Invalid pixel format %d", dec->info.pixel_fmt);
513     return GST_FLOW_ERROR;
514   }
515 }
516
517 static GstFlowReturn
518 theora_handle_header_packet (GstTheoraDec * dec, ogg_packet * packet)
519 {
520   GstFlowReturn res;
521   int ret;
522
523   GST_DEBUG_OBJECT (dec, "parsing header packet");
524
525   ret = th_decode_headerin (&dec->info, &dec->comment, &dec->setup, packet);
526   if (ret < 0)
527     goto header_read_error;
528
529   switch (packet->packet[0]) {
530     case 0x81:
531       res = theora_handle_comment_packet (dec, packet);
532       break;
533     case 0x82:
534       res = theora_handle_type_packet (dec, packet);
535       break;
536     default:
537       /* ignore */
538       g_warning ("unknown theora header packet found");
539     case 0x80:
540       /* nothing special, this is the identification header */
541       res = GST_FLOW_OK;
542       break;
543   }
544   return res;
545
546   /* ERRORS */
547 header_read_error:
548   {
549     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
550         (NULL), ("couldn't read header packet"));
551     return GST_FLOW_ERROR;
552   }
553 }
554
555 /* Allocate buffer and copy image data into Y444 format */
556 static GstFlowReturn
557 theora_handle_image (GstTheoraDec * dec, th_ycbcr_buffer buf,
558     GstVideoCodecFrame * frame)
559 {
560   GstVideoDecoder *decoder = GST_VIDEO_DECODER (dec);
561   gint width, height, stride;
562   GstFlowReturn result;
563   gint i, comp;
564   guint8 *dest, *src;
565   GstVideoFrame vframe;
566   GstVideoCropMeta *crop;
567   gint pic_width, pic_height;
568   gint offset_x, offset_y;
569
570   result = gst_video_decoder_alloc_output_frame (decoder, frame);
571
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));
575     return result;
576   }
577
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)
588       offset_x--;
589     if (offset_y & 1 && dec->info.pixel_fmt == TH_PF_420)
590       offset_y--;
591   } else {
592     /* copy the whole frame */
593     offset_x = 0;
594     offset_y = 0;
595     pic_width = dec->info.frame_width;
596     pic_height = dec->info.frame_height;
597
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);
602
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;
609     }
610   }
611
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");
615
616   if (G_UNLIKELY (!gst_video_frame_map (&vframe, &dec->output_state->info,
617               frame->output_buffer, GST_MAP_WRITE)))
618     goto invalid_frame;
619
620   for (comp = 0; comp < 3; comp++) {
621     width =
622         GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vframe.info.finfo, comp, pic_width);
623     height =
624         GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vframe.info.finfo, comp,
625         pic_height);
626     stride = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, comp);
627     dest = GST_VIDEO_FRAME_COMP_DATA (&vframe, comp);
628
629     src = buf[comp].data;
630     src += ((height == pic_height) ? offset_y : offset_y / 2)
631         * buf[comp].stride;
632     src += (width == pic_width) ? offset_x : offset_x / 2;
633
634     for (i = 0; i < height; i++) {
635       memcpy (dest, src, width);
636
637       dest += stride;
638       src += buf[comp].stride;
639     }
640   }
641   gst_video_frame_unmap (&vframe);
642
643   return GST_FLOW_OK;
644 invalid_frame:
645   {
646     GST_DEBUG_OBJECT (dec, "could not map video frame");
647     return GST_FLOW_ERROR;
648   }
649 }
650
651 static GstFlowReturn
652 theora_handle_data_packet (GstTheoraDec * dec, ogg_packet * packet,
653     GstVideoCodecFrame * frame)
654 {
655   /* normal data packet */
656   th_ycbcr_buffer buf;
657   gboolean keyframe;
658   GstFlowReturn result;
659   ogg_int64_t gp;
660
661   if (G_UNLIKELY (!dec->have_header))
662     goto not_initialized;
663
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)) {
671     goto dropping;
672   }
673
674   GST_DEBUG_OBJECT (dec, "parsing data packet");
675
676   /* this does the decoding */
677   if (G_UNLIKELY (th_decode_packetin (dec->decoder, packet, &gp) < 0))
678     goto decode_error;
679
680   if (frame &&
681       (gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (dec),
682               frame) < 0))
683     goto dropping_qos;
684
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))
688     goto no_yuv;
689
690   if (G_UNLIKELY ((buf[0].width != dec->info.frame_width)
691           || (buf[0].height != dec->info.frame_height)))
692     goto wrong_dimensions;
693
694   result = theora_handle_image (dec, buf, frame);
695   if (result != GST_FLOW_OK)
696     return result;
697
698   return result;
699
700   /* ERRORS */
701 not_initialized:
702   {
703     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
704         (NULL), ("no header sent yet"));
705     return GST_FLOW_ERROR;
706   }
707 dropping:
708   {
709     GST_WARNING_OBJECT (dec, "dropping frame because we need a keyframe");
710     return GST_VIDEO_DECODER_FLOW_NEED_DATA;
711   }
712 dropping_qos:
713   {
714     GST_WARNING_OBJECT (dec, "dropping frame because of QoS");
715     return GST_VIDEO_DECODER_FLOW_NEED_DATA;
716   }
717 decode_error:
718   {
719     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
720         (NULL), ("theora decoder did not decode data packet"));
721     return GST_FLOW_ERROR;
722   }
723 no_yuv:
724   {
725     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
726         (NULL), ("couldn't read out YUV image"));
727     return GST_FLOW_ERROR;
728   }
729 wrong_dimensions:
730   {
731     GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, FORMAT,
732         (NULL), ("dimensions of image do not match header"));
733     return GST_FLOW_ERROR;
734   }
735 }
736
737 static GstFlowReturn
738 theora_dec_decode_buffer (GstTheoraDec * dec, GstBuffer * buf,
739     GstVideoCodecFrame * frame)
740 {
741   ogg_packet packet;
742   GstFlowReturn result = GST_FLOW_OK;
743   GstMapInfo minfo;
744
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 */
753   packet.e_o_s = 0;
754
755   GST_LOG_OBJECT (dec, "decode buffer of size %ld", packet.bytes);
756
757   GST_DEBUG_OBJECT (dec, "header=%02x", packet.bytes ? packet.packet[0] : -1);
758
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");
764       goto done;
765     }
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);
772   } else {
773     result = theora_handle_data_packet (dec, &packet, frame);
774   }
775
776 done:
777   gst_buffer_unmap (buf, &minfo);
778
779   return result;
780 }
781
782 static GstFlowReturn
783 theora_dec_handle_frame (GstVideoDecoder * bdec, GstVideoCodecFrame * frame)
784 {
785   GstTheoraDec *dec;
786   GstFlowReturn res;
787
788   dec = GST_THEORA_DEC (bdec);
789
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);
793
794   return res;
795 }
796
797 static gboolean
798 theora_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
799 {
800   GstTheoraDec *dec = GST_THEORA_DEC (decoder);
801   GstVideoCodecState *state = gst_video_decoder_get_output_state (decoder);
802   GstBufferPool *pool;
803   gboolean update;
804   guint size, min, max;
805   GstStructure *config;
806
807   if (gst_query_get_n_allocation_pools (query) > 0) {
808     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
809
810     /* adjust size */
811     size = MAX (size, state->info.size);
812     update = TRUE;
813   } else {
814     pool = NULL;
815     size = state->info.size;
816     min = max = 0;
817     update = FALSE;
818   }
819
820   /* no downstream pool, make our own */
821   if (pool == NULL) {
822     pool = gst_video_buffer_pool_new ();
823   }
824
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);
829   }
830
831   dec->can_crop =
832       gst_query_has_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE);
833
834   if (dec->can_crop) {
835     GstVideoInfo info = state->info;
836
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);
841   }
842
843   gst_buffer_pool_set_config (pool, config);
844
845   if (update)
846     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
847   else
848     gst_query_add_allocation_pool (query, pool, size, min, max);
849
850   if (pool)
851     gst_object_unref (pool);
852
853   return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
854       query);
855 }
856
857 static void
858 theora_dec_set_property (GObject * object, guint prop_id,
859     const GValue * value, GParamSpec * pspec)
860 {
861   GstTheoraDec *dec = GST_THEORA_DEC (object);
862
863   switch (prop_id) {
864     case PROP_TELEMETRY_MV:
865       dec->telemetry_mv = g_value_get_int (value);
866       break;
867     case PROP_TELEMETRY_MBMODE:
868       dec->telemetry_mbmode = g_value_get_int (value);
869       break;
870     case PROP_TELEMETRY_QI:
871       dec->telemetry_qi = g_value_get_int (value);
872       break;
873     case PROP_TELEMETRY_BITS:
874       dec->telemetry_bits = g_value_get_int (value);
875       break;
876     default:
877       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
878       break;
879   }
880 }
881
882 static void
883 theora_dec_get_property (GObject * object, guint prop_id,
884     GValue * value, GParamSpec * pspec)
885 {
886   GstTheoraDec *dec = GST_THEORA_DEC (object);
887
888   switch (prop_id) {
889     case PROP_TELEMETRY_MV:
890       g_value_set_int (value, dec->telemetry_mv);
891       break;
892     case PROP_TELEMETRY_MBMODE:
893       g_value_set_int (value, dec->telemetry_mbmode);
894       break;
895     case PROP_TELEMETRY_QI:
896       g_value_set_int (value, dec->telemetry_qi);
897       break;
898     case PROP_TELEMETRY_BITS:
899       g_value_set_int (value, dec->telemetry_bits);
900       break;
901     default:
902       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
903       break;
904   }
905 }
906
907 gboolean
908 gst_theora_dec_register (GstPlugin * plugin)
909 {
910   return gst_element_register (plugin, "theoradec",
911       GST_RANK_PRIMARY, GST_TYPE_THEORA_DEC);
912 }