2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
27 #include "gstmpeg2dec.h"
29 #include <gst/video/gstvideometa.h>
30 #include <gst/video/gstvideopool.h>
32 /* 16byte-aligns a buffer for libmpeg2 */
33 #define ALIGN_16(p) ((void *)(((uintptr_t)(p) + 15) & ~((uintptr_t)15)))
35 GST_DEBUG_CATEGORY_STATIC (mpeg2dec_debug);
36 #define GST_CAT_DEFAULT mpeg2dec_debug
37 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
39 /* Send a warning message about decoding errors after receiving this many
40 * STATE_INVALID return values from mpeg2_parse. -1 means never.
42 #define WARN_THRESHOLD (5)
44 static GstStaticPadTemplate sink_template_factory =
45 GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_STATIC_CAPS ("video/mpeg, "
49 "mpegversion = (int) [ 1, 2 ], " "systemstream = (boolean) false")
52 static GstStaticPadTemplate src_template_factory =
53 GST_STATIC_PAD_TEMPLATE ("src",
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 ]")
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,
68 static void gst_mpeg2dec_finalize (GObject * object);
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,
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);
89 gst_mpeg2dec_class_init (GstMpeg2decClass * klass)
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);
95 gobject_class->finalize = gst_mpeg2dec_finalize;
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>");
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);
118 GST_DEBUG_CATEGORY_INIT (mpeg2dec_debug, "mpeg2dec", 0,
119 "MPEG-2 Video Decoder");
120 GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
124 gst_mpeg2dec_init (GstMpeg2dec * mpeg2dec)
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
130 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (mpeg2dec));
132 /* initialize the mpeg2dec acceleration */
136 gst_mpeg2dec_finalize (GObject * object)
138 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (object);
140 if (mpeg2dec->decoder) {
141 GST_DEBUG_OBJECT (mpeg2dec, "closing decoder");
142 mpeg2_close (mpeg2dec->decoder);
143 mpeg2dec->decoder = NULL;
146 gst_mpeg2dec_clear_buffers (mpeg2dec);
147 g_free (mpeg2dec->dummybuf[3]);
148 mpeg2dec->dummybuf[3] = NULL;
150 G_OBJECT_CLASS (parent_class)->finalize (object);
154 gst_mpeg2dec_open (GstVideoDecoder * decoder)
156 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
158 mpeg2_accel (MPEG2_ACCEL_DETECT);
159 if ((mpeg2dec->decoder = mpeg2_init ()) == NULL)
161 mpeg2dec->info = mpeg2_info (mpeg2dec->decoder);
167 gst_mpeg2dec_close (GstVideoDecoder * decoder)
169 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
171 if (mpeg2dec->decoder) {
172 mpeg2_close (mpeg2dec->decoder);
173 mpeg2dec->decoder = NULL;
174 mpeg2dec->info = NULL;
176 gst_mpeg2dec_clear_buffers (mpeg2dec);
182 gst_mpeg2dec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
184 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
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);
195 gst_mpeg2dec_start (GstVideoDecoder * decoder)
197 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
199 mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
205 gst_mpeg2dec_stop (GstVideoDecoder * decoder)
207 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
209 mpeg2_reset (mpeg2dec->decoder, 0);
210 mpeg2_skip (mpeg2dec->decoder, 1);
212 gst_mpeg2dec_clear_buffers (mpeg2dec);
214 if (mpeg2dec->input_state)
215 gst_video_codec_state_unref (mpeg2dec->input_state);
216 mpeg2dec->input_state = NULL;
218 if (mpeg2dec->downstream_pool) {
219 gst_buffer_pool_set_active (mpeg2dec->downstream_pool, FALSE);
220 gst_object_unref (mpeg2dec->downstream_pool);
227 gst_mpeg2dec_flush (GstVideoDecoder * decoder)
229 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
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);
236 gst_mpeg2dec_clear_buffers (mpeg2dec);
238 if (mpeg2dec->downstream_pool)
239 gst_buffer_pool_set_active (mpeg2dec->downstream_pool, FALSE);
245 gst_mpeg2dec_finish (GstVideoDecoder * decoder)
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)
256 GstStructure *config;
258 pool = gst_video_buffer_pool_new ();
259 config = gst_buffer_pool_get_config (pool);
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);
265 *out_config = config;
270 gst_mpeg2dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
272 GstMpeg2dec *dec = GST_MPEG2DEC (decoder);
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;
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;
289 /* Get negotiated allocation caps */
290 gst_query_parse_allocation (query, &caps, NULL);
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, ¶ms);
295 update_allocator = TRUE;
298 gst_allocation_params_init (¶ms);
299 update_allocator = FALSE;
302 params.align = MAX (params.align, 15);
304 if (update_allocator)
305 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
307 gst_query_add_allocation_param (query, allocator, ¶ms);
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,
314 gst_object_unref (allocator);
318 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
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;
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;
333 down_config = config;
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);
345 gst_structure_free (config);
350 pool = gst_mpeg2dec_create_generic_pool (allocator, ¶ms, caps, size,
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);
359 gst_object_unref (allocator);
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,
367 gst_structure_free (down_config);
371 if (!gst_buffer_pool_set_config (dec->downstream_pool, down_config))
375 if (!gst_buffer_pool_set_active (dec->downstream_pool, TRUE))
376 goto activate_failed;
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);
384 /* Check basic params */
385 if (!gst_buffer_pool_config_validate_params (config, caps, size, min, max)) {
386 gst_structure_free (config);
390 /* If needed, check that resulting alignment is still valid */
391 if (dec->need_alignment) {
392 GstVideoAlignment valign;
394 if (!gst_buffer_pool_config_get_video_alignment (config, &valign)) {
395 gst_structure_free (config);
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);
407 if (!gst_buffer_pool_set_config (pool, config))
411 /* For external pools, we need to check strides */
412 if (!GST_IS_VIDEO_BUFFER_POOL (pool) && has_videometa) {
414 const GstVideoFormatInfo *finfo;
418 if (!gst_buffer_pool_set_active (pool, TRUE))
419 goto activate_failed;
421 if (gst_buffer_pool_acquire_buffer (pool, &buffer, NULL) != GST_FLOW_OK) {
422 gst_buffer_pool_set_active (pool, FALSE);
426 vmeta = gst_buffer_get_video_meta (buffer);
427 finfo = gst_video_format_get_info (vmeta->format);
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
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);
437 pool = gst_mpeg2dec_create_generic_pool (allocator, ¶ms, caps, size,
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);
446 /* Generic pool don't fail on _set_config() */
447 gst_buffer_pool_set_config (pool, config);
450 gst_buffer_unref (buffer);
453 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
454 gst_object_unref (pool);
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."));
466 gst_object_unref (pool);
467 GST_ELEMENT_ERROR (dec, RESOURCE, SETTINGS,
468 ("Failed to activate buffer pool"), (NULL));
472 gst_object_unref (pool);
473 GST_ELEMENT_ERROR (dec, RESOURCE, SETTINGS,
474 ("Failed to acquire a buffer"), (NULL));
479 gst_mpeg2dec_crop_buffer (GstMpeg2dec * dec, GstVideoCodecFrame * in_frame,
480 GstVideoFrame * input_vframe)
482 GstVideoCodecState *state;
485 GstVideoFrame output_frame;
487 GstBuffer *buffer = NULL;
489 state = gst_video_decoder_get_output_state (GST_VIDEO_DECODER (dec));
491 dinfo = &dec->decoded_info;
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);
498 ret = gst_buffer_pool_acquire_buffer (dec->downstream_pool, &buffer, NULL);
499 if (ret != GST_FLOW_OK)
502 if (!gst_video_frame_map (&output_frame, info, buffer, GST_MAP_WRITE))
505 if (in_frame->output_buffer)
506 gst_buffer_unref (in_frame->output_buffer);
507 in_frame->output_buffer = buffer;
509 if (!gst_video_frame_copy (&output_frame, input_vframe))
512 gst_video_frame_unmap (&output_frame);
514 GST_BUFFER_FLAGS (in_frame->output_buffer) =
515 GST_BUFFER_FLAGS (input_vframe->buffer);
518 gst_video_codec_state_unref (state);
524 GST_ERROR_OBJECT (dec, "Failed to map output frame");
525 gst_video_codec_state_unref (state);
526 return GST_FLOW_ERROR;
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;
545 gst_mpeg2dec_clear_buffers (GstMpeg2dec * mpeg2dec)
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);
557 gst_mpeg2dec_save_buffer (GstMpeg2dec * mpeg2dec, gint id,
558 GstVideoFrame * frame)
560 GstMpeg2DecBuffer *mbuf;
562 GST_LOG_OBJECT (mpeg2dec, "Saving local info for frame %d", id);
564 mbuf = g_slice_new0 (GstMpeg2DecBuffer);
566 mbuf->frame = *frame;
568 mpeg2dec->buffers = g_list_prepend (mpeg2dec->buffers, mbuf);
572 gst_mpeg2dec_buffer_compare (GstMpeg2DecBuffer * mbuf, gconstpointer id)
574 if (mbuf->id == GPOINTER_TO_INT (id))
580 gst_mpeg2dec_discard_buffer (GstMpeg2dec * mpeg2dec, gint id)
582 GList *l = g_list_find_custom (mpeg2dec->buffers, GINT_TO_POINTER (id),
583 (GCompareFunc) gst_mpeg2dec_buffer_compare);
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);
592 GST_WARNING ("Could not find buffer %d, will be leaked until next reset",
597 static GstVideoFrame *
598 gst_mpeg2dec_get_buffer (GstMpeg2dec * mpeg2dec, gint id)
600 GList *l = g_list_find_custom (mpeg2dec->buffers, GINT_TO_POINTER (id),
601 (GCompareFunc) gst_mpeg2dec_buffer_compare);
604 GstMpeg2DecBuffer *mbuf = l->data;
612 init_dummybuf (GstMpeg2dec * mpeg2dec)
614 g_free (mpeg2dec->dummybuf[3]);
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);
628 handle_sequence (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
630 GstFlowReturn ret = GST_FLOW_OK;
631 GstClockTime latency;
632 const mpeg2_sequence_t *sequence;
633 GstVideoCodecState *state;
635 GstVideoFormat format;
637 sequence = info->sequence;
639 if (sequence->frame_period == 0)
640 goto invalid_frame_period;
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)
648 GST_DEBUG_OBJECT (mpeg2dec,
649 "widthxheight: %dx%d , decoded_widthxheight: %dx%d",
650 sequence->picture_width, sequence->picture_height, sequence->width,
653 gst_video_alignment_reset (&mpeg2dec->valign);
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;
667 goto invalid_picture;
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;
677 format = GST_VIDEO_FORMAT_Y42B;
681 format = GST_VIDEO_FORMAT_Y444;
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;
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;
695 if (mpeg2_guess_aspect (sequence, &pixel_width, &pixel_height)) {
696 vinfo->par_n = pixel_width;
697 vinfo->par_d = pixel_height;
699 GST_DEBUG_OBJECT (mpeg2dec, "Setting PAR %d x %d",
700 vinfo->par_n, vinfo->par_d);
702 vinfo->fps_n = 27000000;
703 vinfo->fps_d = sequence->frame_period;
705 if (!(sequence->flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE))
706 vinfo->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
708 vinfo->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
710 vinfo->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
711 vinfo->colorimetry.range = GST_VIDEO_COLOR_RANGE_16_235;
713 if (sequence->flags & SEQ_FLAG_COLOUR_DESCRIPTION) {
714 /* do color description */
715 switch (sequence->colour_primaries) {
717 vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
720 vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
723 vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
726 vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
729 vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
736 vinfo->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
739 /* matrix coefficients */
740 switch (sequence->matrix_coefficients) {
742 vinfo->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT709;
745 vinfo->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_FCC;
749 vinfo->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT601;
752 vinfo->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
759 vinfo->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
762 /* transfer characteristics */
763 switch (sequence->transfer_characteristics) {
765 vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
768 vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA22;
771 vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA28;
774 vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
777 vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_SMPTE240M;
780 vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA10;
787 vinfo->colorimetry.transfer = GST_VIDEO_TRANSFER_UNKNOWN;
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);
807 /* Save the padded video information */
808 mpeg2dec->decoded_info = *vinfo;
809 gst_video_info_align (&mpeg2dec->decoded_info, &mpeg2dec->valign);
811 /* Mpeg2dec has 2 frame latency to produce a picture and 1 frame latency in
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,
817 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (mpeg2dec)))
818 goto negotiation_fail;
820 gst_video_codec_state_unref (state);
822 mpeg2_custom_fbuf (mpeg2dec->decoder, 1);
824 init_dummybuf (mpeg2dec);
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);
835 invalid_frame_period:
837 GST_WARNING_OBJECT (mpeg2dec, "Frame period is 0!");
838 return GST_FLOW_ERROR;
842 GST_ERROR_OBJECT (mpeg2dec, "Invalid frame dimensions: %d x %d",
843 sequence->width, sequence->height);
844 return GST_FLOW_ERROR;
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;
858 GST_WARNING_OBJECT (mpeg2dec, "Failed to negotiate with downstream");
859 gst_video_codec_state_unref (state);
860 return GST_FLOW_ERROR;
865 handle_picture (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info,
866 GstVideoCodecFrame * frame)
868 GstVideoDecoder *decoder = (GstVideoDecoder *) mpeg2dec;
871 const gchar *type_str = NULL;
872 gboolean key_frame = FALSE;
873 const mpeg2_picture_t *picture = info->current_picture;
874 GstVideoFrame vframe;
877 ret = gst_video_decoder_allocate_output_frame (decoder, frame);
878 if (ret != GST_FLOW_OK)
881 type = picture->flags & PIC_MASK_CODING_TYPE;
883 case PIC_FLAG_CODING_TYPE_I:
885 mpeg2_skip (mpeg2dec->decoder, 0);
888 case PIC_FLAG_CODING_TYPE_P:
891 case PIC_FLAG_CODING_TYPE_B:
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);
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);
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);
911 if (!(picture->flags & PIC_FLAG_PROGRESSIVE_FRAME)) {
912 GST_BUFFER_FLAG_SET (frame->output_buffer,
913 GST_VIDEO_BUFFER_FLAG_INTERLACED);
915 if (picture->flags & PIC_FLAG_REPEAT_FIRST_FIELD) {
916 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
920 if (mpeg2dec->discont_state == MPEG2DEC_DISC_NEW_PICTURE && key_frame) {
921 mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_KEYFRAME;
924 GST_DEBUG_OBJECT (mpeg2dec,
925 "picture: %s %s %s %s %s fields:%d ts:%"
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));
934 if (!gst_video_frame_map (&vframe, &mpeg2dec->decoded_info,
935 frame->output_buffer, GST_MAP_READ | GST_MAP_WRITE))
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);
942 GST_DEBUG_OBJECT (mpeg2dec, "set_buf: %p %p %p, frame %i",
943 buf[0], buf[1], buf[2], frame->system_frame_number);
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
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);
957 GST_ELEMENT_ERROR (mpeg2dec, RESOURCE, WRITE, ("Failed to map frame"),
959 return GST_FLOW_ERROR;
964 handle_slice (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
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;
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);
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);
984 picture = info->display_picture;
985 key_frame = (picture->flags & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_I;
987 (picture->flags & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B;
988 closed_gop = (info->gop->flags & GOP_FLAG_CLOSED_GOP);
990 GST_DEBUG_OBJECT (mpeg2dec, "picture flags: %d, type: %d, keyframe: %d",
991 picture->flags, picture->flags & PIC_MASK_CODING_TYPE, key_frame);
994 mpeg2_skip (mpeg2dec->decoder, 0);
997 if (mpeg2dec->discont_state == MPEG2DEC_DISC_NEW_KEYFRAME && key_frame)
998 mpeg2dec->discont_state = MPEG2DEC_DISC_NONE;
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);
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);
1017 /* do cropping if the target region is smaller than the input one */
1018 if (mpeg2dec->downstream_pool) {
1019 GstVideoFrame *vframe;
1021 if (gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (mpeg2dec),
1023 GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer crop, too late");
1024 return gst_video_decoder_drop_frame (GST_VIDEO_DECODER (mpeg2dec), frame);
1027 GST_DEBUG_OBJECT (mpeg2dec, "Doing a crop copy of the decoded buffer");
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);
1033 if (ret != GST_FLOW_OK) {
1034 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (mpeg2dec), frame);
1039 ret = gst_video_decoder_finish_frame (GST_VIDEO_DECODER (mpeg2dec), frame);
1045 GST_DEBUG ("display buffer does not have a valid frame");
1050 static GstFlowReturn
1051 gst_mpeg2dec_handle_frame (GstVideoDecoder * decoder,
1052 GstVideoCodecFrame * frame)
1054 GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (decoder);
1055 GstBuffer *buf = frame->input_buffer;
1057 const mpeg2_info_t *info;
1058 mpeg2_state_t state;
1059 gboolean done = FALSE;
1060 GstFlowReturn ret = GST_FLOW_OK;
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));
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;
1074 info = mpeg2dec->info;
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");
1081 GST_LOG_OBJECT (mpeg2dec, "calling parse");
1082 state = mpeg2_parse (mpeg2dec->decoder);
1083 GST_DEBUG_OBJECT (mpeg2dec, "parse state %d", 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);
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
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);
1105 case STATE_SEQUENCE_REPEATED:
1106 GST_DEBUG_OBJECT (mpeg2dec, "sequence repeated");
1109 GST_DEBUG_OBJECT (mpeg2dec, "gop");
1112 ret = handle_picture (mpeg2dec, info, frame);
1114 case STATE_SLICE_1ST:
1115 GST_LOG_OBJECT (mpeg2dec, "1st slice of frame encountered");
1117 case STATE_PICTURE_2ND:
1118 GST_LOG_OBJECT (mpeg2dec,
1119 "Second picture header encountered. Decoding 2nd field");
1121 case STATE_INVALID_END:
1122 GST_DEBUG_OBJECT (mpeg2dec, "invalid end");
1124 GST_DEBUG_OBJECT (mpeg2dec, "end");
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);
1131 GST_DEBUG_OBJECT (mpeg2dec, "no picture to display");
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);
1145 GST_VIDEO_DECODER_ERROR (decoder, 1, STREAM, DECODE,
1146 ("decoding error"), ("Reached libmpeg2 invalid state"), ret);
1149 GST_ERROR_OBJECT (mpeg2dec, "Unknown libmpeg2 state %d, FIXME", state);
1151 gst_video_codec_frame_unref (frame);
1155 if (ret != GST_FLOW_OK) {
1156 GST_DEBUG_OBJECT (mpeg2dec, "exit loop, reason %s",
1157 gst_flow_get_name (ret));
1162 gst_video_codec_frame_unref (frame);
1165 gst_buffer_unmap (buf, &minfo);
1166 gst_buffer_unref (buf);
1171 plugin_init (GstPlugin * plugin)
1173 return GST_ELEMENT_REGISTER (mpeg2dec, plugin);
1176 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1179 "LibMpeg2 decoder", plugin_init, VERSION, "GPL", GST_PACKAGE_NAME,
1180 GST_PACKAGE_ORIGIN);