Debian Codegraph @

ext/mpeg2dec/gstmpeg2dec.c

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