Merging gst-plugins-ugly
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-ugly / ext / mpeg2dec / gstmpeg2dec.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <string.h>
24
25 #include <inttypes.h>
26
27 #include "gstmpeg2dec.h"
28
29 #include <gst/video/gstvideometa.h>
30 #include <gst/video/gstvideopool.h>
31
32 /* 16byte-aligns a buffer for libmpeg2 */
33 #define ALIGN_16(p) ((void *)(((uintptr_t)(p) + 15) & ~((uintptr_t)15)))
34
35 GST_DEBUG_CATEGORY_STATIC (mpeg2dec_debug);
36 #define GST_CAT_DEFAULT mpeg2dec_debug
37 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
38
39 /* Send a warning message about decoding errors after receiving this many
40  * STATE_INVALID return values from mpeg2_parse. -1 means never.
41  */
42 #define WARN_THRESHOLD (5)
43
44 static GstStaticPadTemplate sink_template_factory =
45 GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("video/mpeg, "
49         "mpegversion = (int) [ 1, 2 ], " "systemstream = (boolean) false")
50     );
51
52 static GstStaticPadTemplate src_template_factory =
53 GST_STATIC_PAD_TEMPLATE ("src",
54     GST_PAD_SRC,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("video/x-raw, "
57         "format = (string) { YV12, I420, Y42B, Y444 }, "
58         "width = (int) [ 16, 4096 ], "
59         "height = (int) [ 16, 4096 ], "
60         "framerate = (fraction) [ 0/1, 2147483647/1 ]")
61     );
62
63 #define gst_mpeg2dec_parent_class parent_class
64 G_DEFINE_TYPE (GstMpeg2dec, gst_mpeg2dec, GST_TYPE_VIDEO_DECODER);
65 GST_ELEMENT_REGISTER_DEFINE (mpeg2dec, "mpeg2dec", GST_RANK_SECONDARY,
66     GST_TYPE_MPEG2DEC);
67
68 static void gst_mpeg2dec_finalize (GObject * object);
69
70 /* GstVideoDecoder base class method */
71 static gboolean gst_mpeg2dec_open (GstVideoDecoder * decoder);
72 static gboolean gst_mpeg2dec_close (GstVideoDecoder * decoder);
73 static gboolean gst_mpeg2dec_start (GstVideoDecoder * decoder);
74 static gboolean gst_mpeg2dec_stop (GstVideoDecoder * decoder);
75 static gboolean gst_mpeg2dec_set_format (GstVideoDecoder * decoder,
76     GstVideoCodecState * state);
77 static gboolean gst_mpeg2dec_flush (GstVideoDecoder * decoder);
78 static GstFlowReturn gst_mpeg2dec_finish (GstVideoDecoder * decoder);
79 static GstFlowReturn gst_mpeg2dec_handle_frame (GstVideoDecoder * decoder,
80     GstVideoCodecFrame * frame);
81 static gboolean gst_mpeg2dec_decide_allocation (GstVideoDecoder * decoder,
82     GstQuery * query);
83
84 static void gst_mpeg2dec_clear_buffers (GstMpeg2dec * mpeg2dec);
85 static gboolean gst_mpeg2dec_crop_buffer (GstMpeg2dec * dec,
86     GstVideoCodecFrame * in_frame, GstVideoFrame * in_vframe);
87
88 static void
89 gst_mpeg2dec_class_init (GstMpeg2decClass * klass)
90 {
91   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
92   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
93   GstVideoDecoderClass *video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
94
95   gobject_class->finalize = gst_mpeg2dec_finalize;
96
97   gst_element_class_add_static_pad_template (element_class,
98       &src_template_factory);
99   gst_element_class_add_static_pad_template (element_class,
100       &sink_template_factory);
101   gst_element_class_set_static_metadata (element_class,
102       "mpeg1 and mpeg2 video decoder", "Codec/Decoder/Video",
103       "Uses libmpeg2 to decode MPEG video streams",
104       "Wim Taymans <wim.taymans@chello.be>");
105
106   video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_mpeg2dec_open);
107   video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_mpeg2dec_close);
108   video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_mpeg2dec_start);
109   video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg2dec_stop);
110   video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_mpeg2dec_flush);
111   video_decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_mpeg2dec_set_format);
112   video_decoder_class->handle_frame =
113       GST_DEBUG_FUNCPTR (gst_mpeg2dec_handle_frame);
114   video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_mpeg2dec_finish);
115   video_decoder_class->decide_allocation =
116       GST_DEBUG_FUNCPTR (gst_mpeg2dec_decide_allocation);
117
118   GST_DEBUG_CATEGORY_INIT (mpeg2dec_debug, "mpeg2dec", 0,
119       "MPEG-2 Video Decoder");
120   GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
121 }
122
123 static void
124 gst_mpeg2dec_init (GstMpeg2dec * mpeg2dec)
125 {
126   gst_video_decoder_set_packetized (GST_VIDEO_DECODER (mpeg2dec), TRUE);
127   gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (mpeg2dec), TRUE);
128   gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
129       (mpeg2dec), TRUE);
130   GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (mpeg2dec));
131
132   /* initialize the mpeg2dec acceleration */
133 }
134
135 static void
136 gst_mpeg2dec_finalize (GObject * object)
137 {
138   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (object);
139
140   if (mpeg2dec->decoder) {
141     GST_DEBUG_OBJECT (mpeg2dec, "closing decoder");
142     mpeg2_close (mpeg2dec->decoder);
143     mpeg2dec->decoder = NULL;
144   }
145
146   gst_mpeg2dec_clear_buffers (mpeg2dec);
147   g_free (mpeg2dec->dummybuf[3]);
148   mpeg2dec->dummybuf[3] = NULL;
149
150   G_OBJECT_CLASS (parent_class)->finalize (object);
151 }
152
153 static gboolean
154 gst_mpeg2dec_open (GstVideoDecoder * decoder)
155 {
156   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
157
158   mpeg2_accel (MPEG2_ACCEL_DETECT);
159   if ((mpeg2dec->decoder = mpeg2_init ()) == NULL)
160     return FALSE;
161   mpeg2dec->info = mpeg2_info (mpeg2dec->decoder);
162
163   return TRUE;
164 }
165
166 static gboolean
167 gst_mpeg2dec_close (GstVideoDecoder * decoder)
168 {
169   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
170
171   if (mpeg2dec->decoder) {
172     mpeg2_close (mpeg2dec->decoder);
173     mpeg2dec->decoder = NULL;
174     mpeg2dec->info = NULL;
175   }
176   gst_mpeg2dec_clear_buffers (mpeg2dec);
177
178   return TRUE;
179 }
180
181 static gboolean
182 gst_mpeg2dec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
183 {
184   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
185
186   /* Save input state to be used as reference for output state */
187   if (mpeg2dec->input_state)
188     gst_video_codec_state_unref (mpeg2dec->input_state);
189   mpeg2dec->input_state = gst_video_codec_state_ref (state);
190
191   return TRUE;
192 }
193
194 static gboolean
195 gst_mpeg2dec_start (GstVideoDecoder * decoder)
196 {
197   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
198
199   mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
200
201   return TRUE;
202 }
203
204 static gboolean
205 gst_mpeg2dec_stop (GstVideoDecoder * decoder)
206 {
207   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
208
209   mpeg2_reset (mpeg2dec->decoder, 0);
210   mpeg2_skip (mpeg2dec->decoder, 1);
211
212   gst_mpeg2dec_clear_buffers (mpeg2dec);
213
214   if (mpeg2dec->input_state)
215     gst_video_codec_state_unref (mpeg2dec->input_state);
216   mpeg2dec->input_state = NULL;
217
218   if (mpeg2dec->downstream_pool) {
219     gst_buffer_pool_set_active (mpeg2dec->downstream_pool, FALSE);
220     gst_object_unref (mpeg2dec->downstream_pool);
221   }
222
223   return TRUE;
224 }
225
226 static gboolean
227 gst_mpeg2dec_flush (GstVideoDecoder * decoder)
228 {
229   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
230
231   /* reset the initial video state */
232   mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
233   mpeg2_reset (mpeg2dec->decoder, 1);
234   mpeg2_skip (mpeg2dec->decoder, 1);
235
236   gst_mpeg2dec_clear_buffers (mpeg2dec);
237
238   if (mpeg2dec->downstream_pool)
239     gst_buffer_pool_set_active (mpeg2dec->downstream_pool, FALSE);
240
241   return TRUE;
242 }
243
244 static GstFlowReturn
245 gst_mpeg2dec_finish (GstVideoDecoder * decoder)
246 {
247   return GST_FLOW_OK;
248 }
249
250 static GstBufferPool *
251 gst_mpeg2dec_create_generic_pool (GstAllocator * allocator,
252     GstAllocationParams * params, GstCaps * caps, guint size, guint min,
253     guint max, GstStructure ** out_config)
254 {
255   GstBufferPool *pool;
256   GstStructure *config;
257
258   pool = gst_video_buffer_pool_new ();
259   config = gst_buffer_pool_get_config (pool);
260
261   gst_buffer_pool_config_set_allocator (config, allocator, params);
262   gst_buffer_pool_config_set_params (config, caps, size, min, max);
263   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
264
265   *out_config = config;
266   return pool;
267 }
268
269 static gboolean
270 gst_mpeg2dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
271 {
272   GstMpeg2dec *dec = GST_MPEG2DEC (decoder);
273   GstBufferPool *pool;
274   guint size, min, max;
275   GstStructure *config, *down_config = NULL;
276   GstAllocator *allocator;
277   GstAllocationParams params;
278   gboolean update_allocator;
279   gboolean has_videometa = FALSE;
280   GstCaps *caps;
281
282   /* Get rid of ancient pool */
283   if (dec->downstream_pool) {
284     gst_buffer_pool_set_active (dec->downstream_pool, FALSE);
285     gst_object_unref (dec->downstream_pool);
286     dec->downstream_pool = NULL;
287   }
288
289   /* Get negotiated allocation caps */
290   gst_query_parse_allocation (query, &caps, NULL);
291
292   /* Set allocation parameters to guarantee 16-byte aligned output buffers */
293   if (gst_query_get_n_allocation_params (query) > 0) {
294     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
295     update_allocator = TRUE;
296   } else {
297     allocator = NULL;
298     gst_allocation_params_init (&params);
299     update_allocator = FALSE;
300   }
301
302   params.align = MAX (params.align, 15);
303
304   if (update_allocator)
305     gst_query_set_nth_allocation_param (query, 0, allocator, &params);
306   else
307     gst_query_add_allocation_param (query, allocator, &params);
308
309   /* Now chain up to the parent class to guarantee that we can
310    * get a buffer pool from the query */
311   if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
312           query)) {
313     if (allocator)
314       gst_object_unref (allocator);
315     return FALSE;
316   }
317
318   gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
319
320   config = gst_buffer_pool_get_config (pool);
321   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
322     gst_buffer_pool_config_add_option (config,
323         GST_BUFFER_POOL_OPTION_VIDEO_META);
324     has_videometa = TRUE;
325   }
326
327   if (dec->need_alignment) {
328     /* If downstream does not support video meta, we will have to copy, keep
329      * the downstream pool to avoid double copying */
330     if (!has_videometa) {
331       dec->downstream_pool = pool;
332       pool = NULL;
333       down_config = config;
334       config = NULL;
335       min = 2;
336       max = 0;
337     }
338
339     /* In case downstream support video meta, but the downstream pool does not
340      * have alignment support, discard downstream pool and use video pool */
341     else if (!gst_buffer_pool_has_option (pool,
342             GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) {
343       gst_object_unref (pool);
344       pool = NULL;
345       gst_structure_free (config);
346       config = NULL;
347     }
348
349     if (!pool)
350       pool = gst_mpeg2dec_create_generic_pool (allocator, &params, caps, size,
351           min, max, &config);
352
353     gst_buffer_pool_config_add_option (config,
354         GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
355     gst_buffer_pool_config_set_video_alignment (config, &dec->valign);
356   }
357
358   if (allocator)
359     gst_object_unref (allocator);
360
361   /* If we are copying out, we'll need to setup and activate the other pool */
362   if (dec->downstream_pool) {
363     if (!gst_buffer_pool_set_config (dec->downstream_pool, down_config)) {
364       down_config = gst_buffer_pool_get_config (dec->downstream_pool);
365       if (!gst_buffer_pool_config_validate_params (down_config, caps, size, min,
366               max)) {
367         gst_structure_free (down_config);
368         goto config_failed;
369       }
370
371       if (!gst_buffer_pool_set_config (dec->downstream_pool, down_config))
372         goto config_failed;
373     }
374
375     if (!gst_buffer_pool_set_active (dec->downstream_pool, TRUE))
376       goto activate_failed;
377   }
378
379   /* Now configure the pool, if the pool had made some changes, it will
380    * return FALSE. Validate the changes ...*/
381   if (!gst_buffer_pool_set_config (pool, config)) {
382     config = gst_buffer_pool_get_config (pool);
383
384     /* Check basic params */
385     if (!gst_buffer_pool_config_validate_params (config, caps, size, min, max)) {
386       gst_structure_free (config);
387       goto config_failed;
388     }
389
390     /* If needed, check that resulting alignment is still valid */
391     if (dec->need_alignment) {
392       GstVideoAlignment valign;
393
394       if (!gst_buffer_pool_config_get_video_alignment (config, &valign)) {
395         gst_structure_free (config);
396         goto config_failed;
397       }
398
399       if (valign.padding_left != 0 || valign.padding_top != 0
400           || valign.padding_right < dec->valign.padding_right
401           || valign.padding_bottom < dec->valign.padding_bottom) {
402         gst_structure_free (config);
403         goto config_failed;
404       }
405     }
406
407     if (!gst_buffer_pool_set_config (pool, config))
408       goto config_failed;
409   }
410
411   /* For external pools, we need to check strides */
412   if (!GST_IS_VIDEO_BUFFER_POOL (pool) && has_videometa) {
413     GstBuffer *buffer;
414     const GstVideoFormatInfo *finfo;
415     GstVideoMeta *vmeta;
416     gint uv_stride;
417
418     if (!gst_buffer_pool_set_active (pool, TRUE))
419       goto activate_failed;
420
421     if (gst_buffer_pool_acquire_buffer (pool, &buffer, NULL) != GST_FLOW_OK) {
422       gst_buffer_pool_set_active (pool, FALSE);
423       goto acquire_failed;
424     }
425
426     vmeta = gst_buffer_get_video_meta (buffer);
427     finfo = gst_video_format_get_info (vmeta->format);
428
429     /* Check that strides are compatible. In this case, we can scale the
430      * stride directly since all the pixel strides for the formats we support
431      * is 1 */
432     uv_stride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, 1, vmeta->stride[0]);
433     if (uv_stride != vmeta->stride[1] || uv_stride != vmeta->stride[2]) {
434       gst_buffer_pool_set_active (pool, FALSE);
435       gst_object_unref (pool);
436
437       pool = gst_mpeg2dec_create_generic_pool (allocator, &params, caps, size,
438           min, max, &config);
439
440       if (dec->need_alignment) {
441         gst_buffer_pool_config_add_option (config,
442             GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
443         gst_buffer_pool_config_set_video_alignment (config, &dec->valign);
444       }
445
446       /* Generic pool don't fail on _set_config() */
447       gst_buffer_pool_set_config (pool, config);
448     }
449
450     gst_buffer_unref (buffer);
451   }
452
453   gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
454   gst_object_unref (pool);
455
456   return TRUE;
457
458 config_failed:
459   gst_object_unref (pool);
460   GST_ELEMENT_ERROR (dec, RESOURCE, SETTINGS,
461       ("Failed to configure buffer pool"),
462       ("Configuration is most likely invalid, please report this issue."));
463   return FALSE;
464
465 activate_failed:
466   gst_object_unref (pool);
467   GST_ELEMENT_ERROR (dec, RESOURCE, SETTINGS,
468       ("Failed to activate buffer pool"), (NULL));
469   return FALSE;
470
471 acquire_failed:
472   gst_object_unref (pool);
473   GST_ELEMENT_ERROR (dec, RESOURCE, SETTINGS,
474       ("Failed to acquire a buffer"), (NULL));
475   return FALSE;
476 }
477
478 static GstFlowReturn
479 gst_mpeg2dec_crop_buffer (GstMpeg2dec * dec, GstVideoCodecFrame * in_frame,
480     GstVideoFrame * input_vframe)
481 {
482   GstVideoCodecState *state;
483   GstVideoInfo *info;
484   GstVideoInfo *dinfo;
485   GstVideoFrame output_frame;
486   GstFlowReturn ret;
487   GstBuffer *buffer = NULL;
488
489   state = gst_video_decoder_get_output_state (GST_VIDEO_DECODER (dec));
490   info = &state->info;
491   dinfo = &dec->decoded_info;
492
493   GST_CAT_LOG_OBJECT (CAT_PERFORMANCE, dec,
494       "Copying input buffer %ux%u (%" G_GSIZE_FORMAT ") to output buffer "
495       "%ux%u (%" G_GSIZE_FORMAT ")", dinfo->width, dinfo->height,
496       dinfo->size, info->width, info->height, info->size);
497
498   ret = gst_buffer_pool_acquire_buffer (dec->downstream_pool, &buffer, NULL);
499   if (ret != GST_FLOW_OK)
500     goto beach;
501
502   if (!gst_video_frame_map (&output_frame, info, buffer, GST_MAP_WRITE))
503     goto map_fail;
504
505   if (in_frame->output_buffer)
506     gst_buffer_unref (in_frame->output_buffer);
507   in_frame->output_buffer = buffer;
508
509   if (!gst_video_frame_copy (&output_frame, input_vframe))
510     goto copy_failed;
511
512   gst_video_frame_unmap (&output_frame);
513
514   GST_BUFFER_FLAGS (in_frame->output_buffer) =
515       GST_BUFFER_FLAGS (input_vframe->buffer);
516
517 beach:
518   gst_video_codec_state_unref (state);
519
520   return ret;
521
522 map_fail:
523   {
524     GST_ERROR_OBJECT (dec, "Failed to map output frame");
525     gst_video_codec_state_unref (state);
526     return GST_FLOW_ERROR;
527   }
528
529 copy_failed:
530   {
531     GST_ERROR_OBJECT (dec, "Failed to copy output frame");
532     gst_video_frame_unmap (&output_frame);
533     gst_video_codec_state_unref (state);
534     return GST_FLOW_ERROR;
535   }
536 }
537
538 typedef struct
539 {
540   gint id;
541   GstVideoFrame frame;
542 } GstMpeg2DecBuffer;
543
544 static void
545 gst_mpeg2dec_clear_buffers (GstMpeg2dec * mpeg2dec)
546 {
547   GList *l;
548   while ((l = g_list_first (mpeg2dec->buffers))) {
549     GstMpeg2DecBuffer *mbuf = l->data;
550     gst_video_frame_unmap (&mbuf->frame);
551     g_slice_free (GstMpeg2DecBuffer, mbuf);
552     mpeg2dec->buffers = g_list_delete_link (mpeg2dec->buffers, l);
553   }
554 }
555
556 static void
557 gst_mpeg2dec_save_buffer (GstMpeg2dec * mpeg2dec, gint id,
558     GstVideoFrame * frame)
559 {
560   GstMpeg2DecBuffer *mbuf;
561
562   GST_LOG_OBJECT (mpeg2dec, "Saving local info for frame %d", id);
563
564   mbuf = g_slice_new0 (GstMpeg2DecBuffer);
565   mbuf->id = id;
566   mbuf->frame = *frame;
567
568   mpeg2dec->buffers = g_list_prepend (mpeg2dec->buffers, mbuf);
569 }
570
571 static gint
572 gst_mpeg2dec_buffer_compare (GstMpeg2DecBuffer * mbuf, gconstpointer id)
573 {
574   if (mbuf->id == GPOINTER_TO_INT (id))
575     return 0;
576   return -1;
577 }
578
579 static void
580 gst_mpeg2dec_discard_buffer (GstMpeg2dec * mpeg2dec, gint id)
581 {
582   GList *l = g_list_find_custom (mpeg2dec->buffers, GINT_TO_POINTER (id),
583       (GCompareFunc) gst_mpeg2dec_buffer_compare);
584
585   if (l) {
586     GstMpeg2DecBuffer *mbuf = l->data;
587     gst_video_frame_unmap (&mbuf->frame);
588     g_slice_free (GstMpeg2DecBuffer, mbuf);
589     mpeg2dec->buffers = g_list_delete_link (mpeg2dec->buffers, l);
590     GST_LOG_OBJECT (mpeg2dec, "Discarded local info for frame %d", id);
591   } else {
592     GST_WARNING ("Could not find buffer %d, will be leaked until next reset",
593         id);
594   }
595 }
596
597 static GstVideoFrame *
598 gst_mpeg2dec_get_buffer (GstMpeg2dec * mpeg2dec, gint id)
599 {
600   GList *l = g_list_find_custom (mpeg2dec->buffers, GINT_TO_POINTER (id),
601       (GCompareFunc) gst_mpeg2dec_buffer_compare);
602
603   if (l) {
604     GstMpeg2DecBuffer *mbuf = l->data;
605     return &mbuf->frame;
606   }
607
608   return NULL;
609 }
610
611 static void
612 init_dummybuf (GstMpeg2dec * mpeg2dec)
613 {
614   g_free (mpeg2dec->dummybuf[3]);
615
616   /* libmpeg2 needs 16 byte aligned buffers... care for this here */
617   mpeg2dec->dummybuf[3] = g_malloc0 (mpeg2dec->decoded_info.size + 15);
618   mpeg2dec->dummybuf[0] = ALIGN_16 (mpeg2dec->dummybuf[3]);
619   mpeg2dec->dummybuf[1] =
620       mpeg2dec->dummybuf[0] +
621       GST_VIDEO_INFO_PLANE_OFFSET (&mpeg2dec->decoded_info, 1);
622   mpeg2dec->dummybuf[2] =
623       mpeg2dec->dummybuf[0] +
624       GST_VIDEO_INFO_PLANE_OFFSET (&mpeg2dec->decoded_info, 2);
625 }
626
627 static GstFlowReturn
628 handle_sequence (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
629 {
630   GstFlowReturn ret = GST_FLOW_OK;
631   GstClockTime latency;
632   const mpeg2_sequence_t *sequence;
633   GstVideoCodecState *state;
634   GstVideoInfo *vinfo;
635   GstVideoFormat format;
636
637   sequence = info->sequence;
638
639   if (sequence->frame_period == 0)
640     goto invalid_frame_period;
641
642   /* mpeg2 video can only be from 16x16 to 4096x4096. Everything
643    * else is a corrupted file */
644   if (sequence->width > 4096 || sequence->width < 16 ||
645       sequence->height > 4096 || sequence->height < 16)
646     goto invalid_size;
647
648   GST_DEBUG_OBJECT (mpeg2dec,
649       "widthxheight: %dx%d , decoded_widthxheight: %dx%d",
650       sequence->picture_width, sequence->picture_height, sequence->width,
651       sequence->height);
652
653   gst_video_alignment_reset (&mpeg2dec->valign);
654
655   if (sequence->picture_width < sequence->width ||
656       sequence->picture_height < sequence->height) {
657     GST_DEBUG_OBJECT (mpeg2dec, "we need to crop");
658     mpeg2dec->valign.padding_right = sequence->width - sequence->picture_width;
659     mpeg2dec->valign.padding_bottom =
660         sequence->height - sequence->picture_height;
661     mpeg2dec->need_alignment = TRUE;
662   } else if (sequence->picture_width == sequence->width ||
663       sequence->picture_height == sequence->height) {
664     GST_DEBUG_OBJECT (mpeg2dec, "no cropping needed");
665     mpeg2dec->need_alignment = FALSE;
666   } else {
667     goto invalid_picture;
668   }
669
670   /* get subsampling */
671   if (sequence->chroma_width < sequence->width) {
672     /* horizontally subsampled */
673     if (sequence->chroma_height < sequence->height) {
674       /* and vertically subsamples */
675       format = GST_VIDEO_FORMAT_I420;
676     } else {
677       format = GST_VIDEO_FORMAT_Y42B;
678     }
679   } else {
680     /* not subsampled */
681     format = GST_VIDEO_FORMAT_Y444;
682   }
683
684   state = gst_video_decoder_set_output_state (GST_VIDEO_DECODER (mpeg2dec),
685       format, sequence->picture_width, sequence->picture_height,
686       mpeg2dec->input_state);
687   vinfo = &state->info;
688
689   /* If we don't have a valid upstream PAR override it */
690   if (GST_VIDEO_INFO_PAR_N (vinfo) == 1 &&
691       GST_VIDEO_INFO_PAR_D (vinfo) == 1 &&
692       sequence->pixel_width != 0 && sequence->pixel_height != 0) {
693     guint pixel_width, pixel_height;
694
695     if (mpeg2_guess_aspect (sequence, &pixel_width, &pixel_height)) {
696       vinfo->par_n = pixel_width;
697       vinfo->par_d = pixel_height;
698     }
699     GST_DEBUG_OBJECT (mpeg2dec, "Setting PAR %d x %d",
700         vinfo->par_n, vinfo->par_d);
701   }
702   vinfo->fps_n = 27000000;
703   vinfo->fps_d = sequence->frame_period;
704
705   if (!(sequence->flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE))
706     vinfo->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
707   else
708     vinfo->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
709
710   vinfo->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
711   vinfo->colorimetry.range = GST_VIDEO_COLOR_RANGE_16_235;
712
713   if (sequence->flags & SEQ_FLAG_COLOUR_DESCRIPTION) {
714     /* do color description */
715     switch (sequence->colour_primaries) {
716       case 1:
717         vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
718         break;
719       case 4:
720         vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
721         break;
722       case 5:
723         vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
724         break;
725       case 6:
726         vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
727         break;
728       case 7:
729         vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
730         break;
731         /* 0 forbidden */
732         /* 2 unspecified */
733         /* 3 reserved */
734         /* 8-255 reserved */
735       default:
736         vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
737         break;
738     }
739     /* matrix coefficients */
740     switch (sequence->matrix_coefficients) {
741       case 1:
742         vinfo->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT709;
743         break;
744       case 4:
745         vinfo->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_FCC;
746         break;
747       case 5:
748       case 6:
749         vinfo->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT601;
750         break;
751       case 7:
752         vinfo->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
753         break;
754         /* 0 forbidden */
755         /* 2 unspecified */
756         /* 3 reserved */
757         /* 8-255 reserved */
758       default:
759         vinfo->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
760         break;
761     }
762     /* transfer characteristics */
763     switch (sequence->transfer_characteristics) {
764       case 1:
765         vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
766         break;
767       case 4:
768         vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA22;
769         break;
770       case 5:
771         vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA28;
772         break;
773       case 6:
774         vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
775         break;
776       case 7:
777         vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_SMPTE240M;
778         break;
779       case 8:
780         vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA10;
781         break;
782         /* 0 forbidden */
783         /* 2 unspecified */
784         /* 3 reserved */
785         /* 9-255 reserved */
786       default:
787         vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_UNKNOWN;
788         break;
789     }
790   }
791
792   GST_DEBUG_OBJECT (mpeg2dec,
793       "sequence flags: %d, frame period: %d, frame rate: %d/%d",
794       sequence->flags, sequence->frame_period, vinfo->fps_n, vinfo->fps_d);
795   GST_DEBUG_OBJECT (mpeg2dec, "profile: %02x, colour_primaries: %d",
796       sequence->profile_level_id, sequence->colour_primaries);
797   GST_DEBUG_OBJECT (mpeg2dec, "transfer chars: %d, matrix coef: %d",
798       sequence->transfer_characteristics, sequence->matrix_coefficients);
799   GST_DEBUG_OBJECT (mpeg2dec,
800       "FLAGS: CONSTRAINED_PARAMETERS:%d, PROGRESSIVE_SEQUENCE:%d",
801       sequence->flags & SEQ_FLAG_CONSTRAINED_PARAMETERS,
802       sequence->flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE);
803   GST_DEBUG_OBJECT (mpeg2dec, "FLAGS: LOW_DELAY:%d, COLOUR_DESCRIPTION:%d",
804       sequence->flags & SEQ_FLAG_LOW_DELAY,
805       sequence->flags & SEQ_FLAG_COLOUR_DESCRIPTION);
806
807   /* Save the padded video information */
808   mpeg2dec->decoded_info = *vinfo;
809   gst_video_info_align (&mpeg2dec->decoded_info, &mpeg2dec->valign);
810
811   /* Mpeg2dec has 2 frame latency to produce a picture and 1 frame latency in
812    * it's parser */
813   latency = gst_util_uint64_scale (3 * GST_SECOND, vinfo->fps_d, vinfo->fps_n);
814   gst_video_decoder_set_latency (GST_VIDEO_DECODER (mpeg2dec), latency,
815       latency);
816
817   if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (mpeg2dec)))
818     goto negotiation_fail;
819
820   gst_video_codec_state_unref (state);
821
822   mpeg2_custom_fbuf (mpeg2dec->decoder, 1);
823
824   init_dummybuf (mpeg2dec);
825
826   /* Pump in some null buffers, because otherwise libmpeg2 doesn't
827    * initialise the discard_fbuf->id */
828   mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
829   mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
830   mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
831   gst_mpeg2dec_clear_buffers (mpeg2dec);
832
833   return ret;
834
835 invalid_frame_period:
836   {
837     GST_WARNING_OBJECT (mpeg2dec, "Frame period is 0!");
838     return GST_FLOW_ERROR;
839   }
840 invalid_size:
841   {
842     GST_ERROR_OBJECT (mpeg2dec, "Invalid frame dimensions: %d x %d",
843         sequence->width, sequence->height);
844     return GST_FLOW_ERROR;
845   }
846
847 invalid_picture:
848   {
849     GST_ERROR_OBJECT (mpeg2dec, "Picture dimension bigger then frame: "
850         "%d x %d is bigger then %d x %d", sequence->picture_width,
851         sequence->picture_height, sequence->width, sequence->height);
852     return GST_FLOW_ERROR;
853   }
854
855
856 negotiation_fail:
857   {
858     GST_WARNING_OBJECT (mpeg2dec, "Failed to negotiate with downstream");
859     gst_video_codec_state_unref (state);
860     return GST_FLOW_ERROR;
861   }
862 }
863
864 static GstFlowReturn
865 handle_picture (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info,
866     GstVideoCodecFrame * frame)
867 {
868   GstVideoDecoder *decoder = (GstVideoDecoder *) mpeg2dec;
869   GstFlowReturn ret;
870   gint type;
871   const gchar *type_str = NULL;
872   gboolean key_frame = FALSE;
873   const mpeg2_picture_t *picture = info->current_picture;
874   GstVideoFrame vframe;
875   guint8 *buf[3];
876
877   ret = gst_video_decoder_allocate_output_frame (decoder, frame);
878   if (ret != GST_FLOW_OK)
879     return ret;
880
881   type = picture->flags & PIC_MASK_CODING_TYPE;
882   switch (type) {
883     case PIC_FLAG_CODING_TYPE_I:
884       key_frame = TRUE;
885       mpeg2_skip (mpeg2dec->decoder, 0);
886       type_str = "I";
887       break;
888     case PIC_FLAG_CODING_TYPE_P:
889       type_str = "P";
890       break;
891     case PIC_FLAG_CODING_TYPE_B:
892       type_str = "B";
893       break;
894     default:
895       gst_video_codec_frame_ref (frame);
896       ret = gst_video_decoder_drop_frame (GST_VIDEO_DECODER (mpeg2dec), frame);
897       GST_VIDEO_DECODER_ERROR (mpeg2dec, 1, STREAM, DECODE,
898           ("decoding error"), ("Invalid picture type"), ret);
899       return ret;
900   }
901
902   GST_DEBUG_OBJECT (mpeg2dec, "handle picture type %s", type_str);
903   GST_DEBUG_OBJECT (mpeg2dec, "picture %s, frame %i",
904       key_frame ? ", kf," : "    ", frame->system_frame_number);
905
906   if (GST_VIDEO_INFO_IS_INTERLACED (&mpeg2dec->decoded_info)) {
907     /* This implies SEQ_FLAG_PROGRESSIVE_SEQUENCE is not set */
908     if (picture->flags & PIC_FLAG_TOP_FIELD_FIRST) {
909       GST_BUFFER_FLAG_SET (frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
910     }
911     if (!(picture->flags & PIC_FLAG_PROGRESSIVE_FRAME)) {
912       GST_BUFFER_FLAG_SET (frame->output_buffer,
913           GST_VIDEO_BUFFER_FLAG_INTERLACED);
914     }
915     if (picture->flags & PIC_FLAG_REPEAT_FIRST_FIELD) {
916       GST_BUFFER_FLAG_SET (frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
917     }
918   }
919
920   if (mpeg2dec->discont_state == MPEG2DEC_DISC_NEW_PICTURE && key_frame) {
921     mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_KEYFRAME;
922   }
923
924   GST_DEBUG_OBJECT (mpeg2dec,
925       "picture: %s %s %s %s %s fields:%d ts:%"
926       GST_TIME_FORMAT,
927       (picture->flags & PIC_FLAG_PROGRESSIVE_FRAME ? "prog" : "    "),
928       (picture->flags & PIC_FLAG_TOP_FIELD_FIRST ? "tff" : "   "),
929       (picture->flags & PIC_FLAG_REPEAT_FIRST_FIELD ? "rff" : "   "),
930       (picture->flags & PIC_FLAG_SKIP ? "skip" : "    "),
931       (picture->flags & PIC_FLAG_COMPOSITE_DISPLAY ? "composite" : "         "),
932       picture->nb_fields, GST_TIME_ARGS (frame->pts));
933
934   if (!gst_video_frame_map (&vframe, &mpeg2dec->decoded_info,
935           frame->output_buffer, GST_MAP_READ | GST_MAP_WRITE))
936     goto map_fail;
937
938   buf[0] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, 0);
939   buf[1] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, 1);
940   buf[2] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, 2);
941
942   GST_DEBUG_OBJECT (mpeg2dec, "set_buf: %p %p %p, frame %i",
943       buf[0], buf[1], buf[2], frame->system_frame_number);
944
945   /* Note: We use a non-null 'id' value to make the distinction
946    * between the dummy buffers (which have an id of NULL) and the
947    * ones we did */
948   mpeg2_stride (mpeg2dec->decoder, vframe.info.stride[0]);
949   mpeg2_set_buf (mpeg2dec->decoder, buf,
950       GINT_TO_POINTER (frame->system_frame_number + 1));
951   gst_mpeg2dec_save_buffer (mpeg2dec, frame->system_frame_number, &vframe);
952
953   return ret;
954
955 map_fail:
956   {
957     GST_ELEMENT_ERROR (mpeg2dec, RESOURCE, WRITE, ("Failed to map frame"),
958         (NULL));
959     return GST_FLOW_ERROR;
960   }
961 }
962
963 static GstFlowReturn
964 handle_slice (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
965 {
966   GstFlowReturn ret = GST_FLOW_OK;
967   GstVideoCodecFrame *frame;
968   const mpeg2_picture_t *picture;
969   gboolean key_frame = FALSE;
970   gboolean bidirect_frame = FALSE;
971   gboolean closed_gop = FALSE;
972
973   GST_DEBUG_OBJECT (mpeg2dec,
974       "fbuf:%p display_picture:%p current_picture:%p fbuf->id:%d",
975       info->display_fbuf, info->display_picture, info->current_picture,
976       GPOINTER_TO_INT (info->display_fbuf->id) - 1);
977
978   /* Note, the fbuf-id is shifted by 1 to make the difference between
979    * NULL values (used by dummy buffers) and 'real' values */
980   frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (mpeg2dec),
981       GPOINTER_TO_INT (info->display_fbuf->id) - 1);
982   if (!frame)
983     goto no_frame;
984   picture = info->display_picture;
985   key_frame = (picture->flags & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_I;
986   bidirect_frame =
987       (picture->flags & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B;
988   closed_gop = (info->gop->flags & GOP_FLAG_CLOSED_GOP);
989
990   GST_DEBUG_OBJECT (mpeg2dec, "picture flags: %d, type: %d, keyframe: %d",
991       picture->flags, picture->flags & PIC_MASK_CODING_TYPE, key_frame);
992
993   if (key_frame) {
994     mpeg2_skip (mpeg2dec->decoder, 0);
995   }
996
997   if (mpeg2dec->discont_state == MPEG2DEC_DISC_NEW_KEYFRAME && key_frame)
998     mpeg2dec->discont_state = MPEG2DEC_DISC_NONE;
999
1000   if (picture->flags & PIC_FLAG_SKIP) {
1001     GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer because of skip flag");
1002     ret = gst_video_decoder_drop_frame (GST_VIDEO_DECODER (mpeg2dec), frame);
1003     mpeg2_skip (mpeg2dec->decoder, 1);
1004     return ret;
1005   }
1006
1007   /* Skip B-frames if GOP is not closed and waiting for the first keyframe. */
1008   if (mpeg2dec->discont_state != MPEG2DEC_DISC_NONE) {
1009     if (bidirect_frame && !closed_gop) {
1010       GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer, discont state %d",
1011           mpeg2dec->discont_state);
1012       ret = gst_video_decoder_drop_frame (GST_VIDEO_DECODER (mpeg2dec), frame);
1013       return ret;
1014     }
1015   }
1016
1017   /* do cropping if the target region is smaller than the input one */
1018   if (mpeg2dec->downstream_pool) {
1019     GstVideoFrame *vframe;
1020
1021     if (gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (mpeg2dec),
1022             frame) < 0) {
1023       GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer crop, too late");
1024       return gst_video_decoder_drop_frame (GST_VIDEO_DECODER (mpeg2dec), frame);
1025     }
1026
1027     GST_DEBUG_OBJECT (mpeg2dec, "Doing a crop copy of the decoded buffer");
1028
1029     vframe = gst_mpeg2dec_get_buffer (mpeg2dec, frame->system_frame_number);
1030     g_assert (vframe != NULL);
1031     ret = gst_mpeg2dec_crop_buffer (mpeg2dec, frame, vframe);
1032
1033     if (ret != GST_FLOW_OK) {
1034       gst_video_decoder_drop_frame (GST_VIDEO_DECODER (mpeg2dec), frame);
1035       return ret;
1036     }
1037   }
1038
1039   ret = gst_video_decoder_finish_frame (GST_VIDEO_DECODER (mpeg2dec), frame);
1040
1041   return ret;
1042
1043 no_frame:
1044   {
1045     GST_DEBUG ("display buffer does not have a valid frame");
1046     return GST_FLOW_OK;
1047   }
1048 }
1049
1050 static GstFlowReturn
1051 gst_mpeg2dec_handle_frame (GstVideoDecoder * decoder,
1052     GstVideoCodecFrame * frame)
1053 {
1054   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
1055   GstBuffer *buf = frame->input_buffer;
1056   GstMapInfo minfo;
1057   const mpeg2_info_t *info;
1058   mpeg2_state_t state;
1059   gboolean done = FALSE;
1060   GstFlowReturn ret = GST_FLOW_OK;
1061
1062   GST_LOG_OBJECT (mpeg2dec, "received frame %d, timestamp %"
1063       GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
1064       frame->system_frame_number,
1065       GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1066
1067   gst_buffer_ref (buf);
1068   if (!gst_buffer_map (buf, &minfo, GST_MAP_READ)) {
1069     GST_ERROR_OBJECT (mpeg2dec, "Failed to map input buffer");
1070     gst_buffer_unref (buf);
1071     return GST_FLOW_ERROR;
1072   }
1073
1074   info = mpeg2dec->info;
1075
1076   GST_LOG_OBJECT (mpeg2dec, "calling mpeg2_buffer");
1077   mpeg2_buffer (mpeg2dec->decoder, minfo.data, minfo.data + minfo.size);
1078   GST_LOG_OBJECT (mpeg2dec, "calling mpeg2_buffer done");
1079
1080   while (!done) {
1081     GST_LOG_OBJECT (mpeg2dec, "calling parse");
1082     state = mpeg2_parse (mpeg2dec->decoder);
1083     GST_DEBUG_OBJECT (mpeg2dec, "parse state %d", state);
1084
1085     switch (state) {
1086       case STATE_SEQUENCE_MODIFIED:
1087         GST_DEBUG_OBJECT (mpeg2dec, "sequence modified");
1088         mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
1089         gst_mpeg2dec_clear_buffers (mpeg2dec);
1090         /* fall through */
1091       case STATE_SEQUENCE:
1092         ret = handle_sequence (mpeg2dec, info);
1093         /* if there is an error handling the sequence
1094          * reset the decoder, maybe something more elegant
1095          * could be done.
1096          */
1097         if (ret == GST_FLOW_ERROR) {
1098           GST_VIDEO_DECODER_ERROR (decoder, 1, STREAM, DECODE,
1099               ("decoding error"), ("Bad sequence header"), ret);
1100           gst_video_decoder_drop_frame (decoder, frame);
1101           gst_mpeg2dec_flush (decoder);
1102           goto done;
1103         }
1104         break;
1105       case STATE_SEQUENCE_REPEATED:
1106         GST_DEBUG_OBJECT (mpeg2dec, "sequence repeated");
1107         break;
1108       case STATE_GOP:
1109         GST_DEBUG_OBJECT (mpeg2dec, "gop");
1110         break;
1111       case STATE_PICTURE:
1112         ret = handle_picture (mpeg2dec, info, frame);
1113         break;
1114       case STATE_SLICE_1ST:
1115         GST_LOG_OBJECT (mpeg2dec, "1st slice of frame encountered");
1116         break;
1117       case STATE_PICTURE_2ND:
1118         GST_LOG_OBJECT (mpeg2dec,
1119             "Second picture header encountered. Decoding 2nd field");
1120         break;
1121       case STATE_INVALID_END:
1122         GST_DEBUG_OBJECT (mpeg2dec, "invalid end");
1123       case STATE_END:
1124         GST_DEBUG_OBJECT (mpeg2dec, "end");
1125       case STATE_SLICE:
1126         GST_DEBUG_OBJECT (mpeg2dec, "display_fbuf:%p, discard_fbuf:%p",
1127             info->display_fbuf, info->discard_fbuf);
1128         if (info->display_fbuf && info->display_fbuf->id) {
1129           ret = handle_slice (mpeg2dec, info);
1130         } else {
1131           GST_DEBUG_OBJECT (mpeg2dec, "no picture to display");
1132         }
1133         if (info->discard_fbuf && info->discard_fbuf->id)
1134           gst_mpeg2dec_discard_buffer (mpeg2dec,
1135               GPOINTER_TO_INT (info->discard_fbuf->id) - 1);
1136         if (state != STATE_SLICE) {
1137           gst_mpeg2dec_clear_buffers (mpeg2dec);
1138         }
1139         break;
1140       case STATE_BUFFER:
1141         done = TRUE;
1142         break;
1143         /* error */
1144       case STATE_INVALID:
1145         GST_VIDEO_DECODER_ERROR (decoder, 1, STREAM, DECODE,
1146             ("decoding error"), ("Reached libmpeg2 invalid state"), ret);
1147         continue;
1148       default:
1149         GST_ERROR_OBJECT (mpeg2dec, "Unknown libmpeg2 state %d, FIXME", state);
1150         ret = GST_FLOW_OK;
1151         gst_video_codec_frame_unref (frame);
1152         goto done;
1153     }
1154
1155     if (ret != GST_FLOW_OK) {
1156       GST_DEBUG_OBJECT (mpeg2dec, "exit loop, reason %s",
1157           gst_flow_get_name (ret));
1158       break;
1159     }
1160   }
1161
1162   gst_video_codec_frame_unref (frame);
1163
1164 done:
1165   gst_buffer_unmap (buf, &minfo);
1166   gst_buffer_unref (buf);
1167   return ret;
1168 }
1169
1170 static gboolean
1171 plugin_init (GstPlugin * plugin)
1172 {
1173   return GST_ELEMENT_REGISTER (mpeg2dec, plugin);
1174 }
1175
1176 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1177     GST_VERSION_MINOR,
1178     mpeg2dec,
1179     "LibMpeg2 decoder", plugin_init, VERSION, "GPL", GST_PACKAGE_NAME,
1180     GST_PACKAGE_ORIGIN);