3 * Copyright (C) 2009 Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-vdpaumpegdec
24 * FIXME:Describe vdpaumpegdec here.
27 * <title>Example launch line</title>
29 * gst-launch -v -m fakesrc ! vdpaumpegdec ! fakesink silent=TRUE
39 #include <gst/base/gstbytereader.h>
40 #include <gst/base/gstbitreader.h>
45 #include "gstvdpmpegdec.h"
47 GST_DEBUG_CATEGORY_STATIC (gst_vdp_mpeg_dec_debug);
48 #define GST_CAT_DEFAULT gst_vdp_mpeg_dec_debug
50 /* the capabilities of the inputs and outputs.
52 * describe the real formats here.
54 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
57 GST_STATIC_CAPS ("video/mpeg, mpegversion = (int) [ 1, 2 ], "
58 "systemstream = (boolean) false")
61 #define DEBUG_INIT(bla) \
62 GST_DEBUG_CATEGORY_INIT (gst_vdp_mpeg_dec_debug, "vdpaumpegdec", 0, \
63 "VDPAU mpeg decoder");
65 GST_BOILERPLATE_FULL (GstVdpMpegDec, gst_vdp_mpeg_dec,
66 GstVdpDecoder, GST_TYPE_VDP_DECODER, DEBUG_INIT);
68 static void gst_vdp_mpeg_dec_init_info (VdpPictureInfoMPEG1Or2 * vdp_info);
70 #define SYNC_CODE_SIZE 3
72 static VdpDecoderProfile
73 gst_vdp_mpeg_dec_get_profile (MPEGSeqExtHdr * hdr)
75 VdpDecoderProfile profile;
77 switch (hdr->profile) {
79 profile = VDP_DECODER_PROFILE_MPEG2_SIMPLE;
82 profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
90 gst_vdp_mpeg_dec_handle_picture_coding (GstVdpMpegDec * mpeg_dec,
91 GstBuffer * buffer, GstVideoFrame * frame)
93 MPEGPictureExt pic_ext;
94 VdpPictureInfoMPEG1Or2 *info;
97 info = &mpeg_dec->vdp_info;
99 if (!mpeg_util_parse_picture_coding_extension (&pic_ext, buffer))
102 memcpy (&mpeg_dec->vdp_info.f_code, &pic_ext.f_code, 4);
104 info->intra_dc_precision = pic_ext.intra_dc_precision;
105 info->picture_structure = pic_ext.picture_structure;
106 info->top_field_first = pic_ext.top_field_first;
107 info->frame_pred_frame_dct = pic_ext.frame_pred_frame_dct;
108 info->concealment_motion_vectors = pic_ext.concealment_motion_vectors;
109 info->q_scale_type = pic_ext.q_scale_type;
110 info->intra_vlc_format = pic_ext.intra_vlc_format;
111 info->alternate_scan = pic_ext.alternate_scan;
114 if (pic_ext.picture_structure == 3) {
115 if (mpeg_dec->stream_info.interlaced) {
116 if (pic_ext.progressive_frame == 0)
118 if (pic_ext.progressive_frame == 0 && pic_ext.repeat_first_field == 0)
120 if (pic_ext.progressive_frame == 1 && pic_ext.repeat_first_field == 1)
123 if (pic_ext.repeat_first_field == 0)
125 if (pic_ext.repeat_first_field == 1 && pic_ext.top_field_first == 0)
127 if (pic_ext.repeat_first_field == 1 && pic_ext.top_field_first == 1)
133 frame->n_fields = fields;
135 if (pic_ext.top_field_first)
136 GST_VIDEO_FRAME_FLAG_SET (frame, GST_VIDEO_FRAME_FLAG_TFF);
142 gst_vdp_mpeg_dec_handle_picture (GstVdpMpegDec * mpeg_dec, GstBuffer * buffer)
144 MPEGPictureHdr pic_hdr;
146 if (!mpeg_util_parse_picture_hdr (&pic_hdr, buffer))
149 mpeg_dec->vdp_info.picture_coding_type = pic_hdr.pic_type;
151 if (mpeg_dec->stream_info.version == 1) {
152 mpeg_dec->vdp_info.full_pel_forward_vector =
153 pic_hdr.full_pel_forward_vector;
154 mpeg_dec->vdp_info.full_pel_backward_vector =
155 pic_hdr.full_pel_backward_vector;
156 memcpy (&mpeg_dec->vdp_info.f_code, &pic_hdr.f_code, 4);
159 mpeg_dec->frame_nr = mpeg_dec->gop_frame + pic_hdr.tsn;
165 gst_vdp_mpeg_dec_handle_gop (GstVdpMpegDec * mpeg_dec, GstBuffer * buffer)
170 if (!mpeg_util_parse_gop (&gop, buffer))
173 time = GST_SECOND * (gop.hour * 3600 + gop.minute * 60 + gop.second);
175 GST_DEBUG ("gop timestamp: %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
177 mpeg_dec->gop_frame =
178 gst_util_uint64_scale (time, mpeg_dec->stream_info.fps_n,
179 mpeg_dec->stream_info.fps_d * GST_SECOND) + gop.frame;
181 if (mpeg_dec->state == GST_VDP_MPEG_DEC_STATE_NEED_GOP)
182 mpeg_dec->state = GST_VDP_MPEG_DEC_STATE_NEED_DATA;
188 gst_vdp_mpeg_dec_handle_quant_matrix (GstVdpMpegDec * mpeg_dec,
193 if (!mpeg_util_parse_quant_matrix (&qm, buffer))
196 memcpy (&mpeg_dec->vdp_info.intra_quantizer_matrix,
197 &qm.intra_quantizer_matrix, 64);
198 memcpy (&mpeg_dec->vdp_info.non_intra_quantizer_matrix,
199 &qm.non_intra_quantizer_matrix, 64);
204 gst_vdp_mpeg_dec_handle_sequence (GstVdpMpegDec * mpeg_dec,
205 GstBuffer * seq, GstBuffer * seq_ext)
207 GstBaseVideoDecoder *base_video_decoder = GST_BASE_VIDEO_DECODER (mpeg_dec);
210 GstVdpMpegStreamInfo stream_info;
212 if (!mpeg_util_parse_sequence_hdr (&hdr, seq))
213 return GST_FLOW_CUSTOM_ERROR;
215 memcpy (&mpeg_dec->vdp_info.intra_quantizer_matrix,
216 &hdr.intra_quantizer_matrix, 64);
217 memcpy (&mpeg_dec->vdp_info.non_intra_quantizer_matrix,
218 &hdr.non_intra_quantizer_matrix, 64);
220 stream_info.width = hdr.width;
221 stream_info.height = hdr.height;
223 stream_info.fps_n = hdr.fps_n;
224 stream_info.fps_d = hdr.fps_d;
226 stream_info.par_n = hdr.par_w;
227 stream_info.par_d = hdr.par_h;
229 stream_info.interlaced = FALSE;
230 stream_info.version = 1;
231 stream_info.profile = VDP_DECODER_PROFILE_MPEG1;
236 if (!mpeg_util_parse_sequence_extension (&ext, seq_ext))
237 return GST_FLOW_CUSTOM_ERROR;
239 stream_info.fps_n *= (ext.fps_n_ext + 1);
240 stream_info.fps_d *= (ext.fps_d_ext + 1);
242 stream_info.width += (ext.horiz_size_ext << 12);
243 stream_info.height += (ext.vert_size_ext << 12);
245 stream_info.interlaced = !ext.progressive;
246 stream_info.version = 2;
247 stream_info.profile = gst_vdp_mpeg_dec_get_profile (&ext);
250 if (memcmp (&mpeg_dec->stream_info, &stream_info,
251 sizeof (GstVdpMpegStreamInfo)) != 0) {
255 state = gst_base_video_decoder_get_state (base_video_decoder);
257 state.width = stream_info.width;
258 state.height = stream_info.height;
260 state.fps_n = stream_info.fps_n;
261 state.fps_d = stream_info.fps_d;
263 state.par_n = stream_info.par_n;
264 state.par_d = stream_info.par_d;
266 state.interlaced = stream_info.interlaced;
268 gst_base_video_decoder_set_state (base_video_decoder, state);
270 ret = gst_vdp_decoder_init_decoder (GST_VDP_DECODER (mpeg_dec),
271 stream_info.profile, 2);
272 if (ret != GST_FLOW_OK)
275 memcpy (&mpeg_dec->stream_info, &stream_info,
276 sizeof (GstVdpMpegStreamInfo));
279 mpeg_dec->state = GST_VDP_MPEG_DEC_STATE_NEED_DATA;
285 gst_vdp_mpeg_dec_handle_frame (GstBaseVideoDecoder * base_video_decoder,
286 GstVideoFrame * frame, GstClockTimeDiff deadline)
288 GstVdpMpegDec *mpeg_dec = GST_VDP_MPEG_DEC (base_video_decoder);
290 VdpPictureInfoMPEG1Or2 *info;
291 GstVdpMpegFrame *mpeg_frame;
293 GstFlowReturn ret = GST_FLOW_OK;
294 VdpBitstreamBuffer vbit[1];
295 GstVdpVideoBuffer *outbuf;
297 /* MPEG_PACKET_SEQUENCE */
298 mpeg_frame = GST_VDP_MPEG_FRAME (frame);
299 if (mpeg_frame->seq) {
300 ret = gst_vdp_mpeg_dec_handle_sequence (mpeg_dec, mpeg_frame->seq,
301 mpeg_frame->seq_ext);
302 if (ret != GST_FLOW_OK) {
303 gst_base_video_decoder_skip_frame (base_video_decoder, frame);
308 if (mpeg_dec->state == GST_VDP_MPEG_DEC_STATE_NEED_SEQUENCE) {
309 GST_DEBUG_OBJECT (mpeg_dec, "Drop frame since we haven't found a "
310 "MPEG_PACKET_SEQUENCE yet");
312 gst_base_video_decoder_skip_frame (base_video_decoder, frame);
316 /* MPEG_PACKET_PICTURE */
318 gst_vdp_mpeg_dec_handle_picture (mpeg_dec, mpeg_frame->pic);
320 /* MPEG_PACKET_EXT_PICTURE_CODING */
321 if (mpeg_frame->pic_ext)
322 gst_vdp_mpeg_dec_handle_picture_coding (mpeg_dec, mpeg_frame->pic_ext,
325 /* MPEG_PACKET_GOP */
327 gst_vdp_mpeg_dec_handle_gop (mpeg_dec, mpeg_frame->gop);
329 /* MPEG_PACKET_EXT_QUANT_MATRIX */
330 if (mpeg_frame->qm_ext)
331 gst_vdp_mpeg_dec_handle_quant_matrix (mpeg_dec, mpeg_frame->qm_ext);
334 info = &mpeg_dec->vdp_info;
336 info->slice_count = mpeg_frame->n_slices;
338 /* check if we can decode the frame */
339 if (info->picture_coding_type != I_FRAME
340 && info->backward_reference == VDP_INVALID_HANDLE) {
341 GST_DEBUG_OBJECT (mpeg_dec,
342 "Drop frame since we haven't got an I_FRAME yet");
344 gst_base_video_decoder_skip_frame (base_video_decoder, frame);
347 if (info->picture_coding_type == B_FRAME
348 && info->forward_reference == VDP_INVALID_HANDLE) {
349 GST_DEBUG_OBJECT (mpeg_dec,
350 "Drop frame since we haven't got two non B_FRAMES yet");
352 gst_base_video_decoder_skip_frame (base_video_decoder, frame);
357 if (info->picture_coding_type != B_FRAME) {
358 if (info->backward_reference != VDP_INVALID_HANDLE) {
359 ret = gst_base_video_decoder_finish_frame (base_video_decoder,
363 if (info->forward_reference != VDP_INVALID_HANDLE) {
364 gst_video_frame_unref (mpeg_dec->f_frame);
365 info->forward_reference = VDP_INVALID_HANDLE;
368 info->forward_reference = info->backward_reference;
369 mpeg_dec->f_frame = mpeg_dec->b_frame;
371 info->backward_reference = VDP_INVALID_HANDLE;
374 if (ret != GST_FLOW_OK) {
375 gst_base_video_decoder_skip_frame (base_video_decoder, frame);
380 vbit[0].struct_version = VDP_BITSTREAM_BUFFER_VERSION;
381 vbit[0].bitstream = GST_BUFFER_DATA (mpeg_frame->slices);
382 vbit[0].bitstream_bytes = GST_BUFFER_SIZE (mpeg_frame->slices);
384 ret = gst_vdp_decoder_render (GST_VDP_DECODER (mpeg_dec),
385 (VdpPictureInfo *) info, 1, vbit, &outbuf);
386 if (ret != GST_FLOW_OK)
389 frame->src_buffer = GST_BUFFER_CAST (outbuf);
391 if (info->picture_coding_type == B_FRAME) {
392 ret = gst_base_video_decoder_finish_frame (base_video_decoder, frame);
394 info->backward_reference = GST_VDP_VIDEO_BUFFER (outbuf)->surface;
395 mpeg_dec->b_frame = gst_video_frame_ref (frame);
401 static GstVideoFrame *
402 gst_vdp_mpeg_dec_create_frame (GstBaseVideoDecoder * base_video_decoder)
404 return GST_VIDEO_FRAME (gst_vdp_mpeg_frame_new ());
408 gst_vdp_mpeg_dec_parse_data (GstBaseVideoDecoder * base_video_decoder,
409 GstBuffer * buf, gboolean at_eos, GstVideoFrame * frame)
411 GstVdpMpegDec *mpeg_dec = GST_VDP_MPEG_DEC (base_video_decoder);
413 GstVdpMpegFrame *mpeg_frame;
414 GstFlowReturn ret = GST_FLOW_OK;
415 GstBitReader b_reader = GST_BIT_READER_INIT_FROM_BUFFER (buf);
419 gst_bit_reader_skip (&b_reader, 8 * 3);
422 if (!gst_bit_reader_get_bits_uint8 (&b_reader, &start_code, 8))
423 return GST_FLOW_ERROR;
425 mpeg_frame = GST_VDP_MPEG_FRAME_CAST (frame);
427 if (start_code >= MPEG_PACKET_SLICE_MIN
428 && start_code <= MPEG_PACKET_SLICE_MAX) {
429 GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_SLICE");
431 gst_vdp_mpeg_frame_add_slice (mpeg_frame, buf);
435 switch (start_code) {
436 case MPEG_PACKET_SEQUENCE:
437 GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_SEQUENCE");
439 if (mpeg_dec->prev_packet != -1)
440 ret = gst_base_video_decoder_have_frame (base_video_decoder, FALSE,
441 (GstVideoFrame **) & mpeg_frame);
443 mpeg_frame->seq = buf;
446 case MPEG_PACKET_PICTURE:
447 GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_PICTURE");
449 if (mpeg_dec->prev_packet != MPEG_PACKET_SEQUENCE &&
450 mpeg_dec->prev_packet != MPEG_PACKET_GOP)
451 ret = gst_base_video_decoder_have_frame (base_video_decoder, FALSE,
452 (GstVideoFrame **) & mpeg_frame);
454 mpeg_frame->pic = buf;
457 case MPEG_PACKET_GOP:
458 GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_GOP");
460 if (mpeg_dec->prev_packet != MPEG_PACKET_SEQUENCE)
461 ret = gst_base_video_decoder_have_frame (base_video_decoder, FALSE,
462 (GstVideoFrame **) & mpeg_frame);
464 mpeg_frame->gop = buf;
467 case MPEG_PACKET_EXTENSION:
472 if (!gst_bit_reader_get_bits_uint8 (&b_reader, &ext_code, 4)) {
473 ret = GST_FLOW_ERROR;
474 gst_buffer_unref (buf);
478 GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_EXTENSION: %d", ext_code);
481 case MPEG_PACKET_EXT_SEQUENCE:
482 GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_EXT_SEQUENCE");
485 mpeg_frame->seq_ext = buf;
487 /* so that we don't finish the frame if we get a MPEG_PACKET_PICTURE
488 * or MPEG_PACKET_GOP after this */
489 start_code = MPEG_PACKET_SEQUENCE;
492 case MPEG_PACKET_EXT_SEQUENCE_DISPLAY:
493 GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_EXT_SEQUENCE_DISPLAY");
495 /* so that we don't finish the frame if we get a MPEG_PACKET_PICTURE
496 * or MPEG_PACKET_GOP after this */
497 start_code = MPEG_PACKET_SEQUENCE;
500 case MPEG_PACKET_EXT_PICTURE_CODING:
501 GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_EXT_PICTURE_CODING");
503 mpeg_frame->pic_ext = buf;
506 case MPEG_PACKET_EXT_QUANT_MATRIX:
507 GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_EXT_QUANT_MATRIX");
509 mpeg_frame->qm_ext = buf;
513 gst_buffer_unref (buf);
519 gst_buffer_unref (buf);
522 if (at_eos && mpeg_frame->slices)
523 ret = gst_base_video_decoder_have_frame (base_video_decoder, TRUE, NULL);
526 mpeg_dec->prev_packet = start_code;
532 gst_vdp_mpeg_dec_scan_for_sync (GstBaseVideoDecoder * base_video_decoder,
533 GstAdapter * adapter)
537 m = gst_adapter_masked_scan_uint32 (adapter, 0xffffff00, 0x00000100, 0,
538 gst_adapter_available (adapter));
540 return gst_adapter_available (adapter) - SYNC_CODE_SIZE;
545 static GstBaseVideoDecoderScanResult
546 gst_vdp_mpeg_dec_scan_for_packet_end (GstBaseVideoDecoder * base_video_decoder,
547 GstAdapter * adapter, guint * size, gboolean at_eos)
552 data = g_slice_alloc (SYNC_CODE_SIZE);
553 gst_adapter_copy (adapter, data, 0, SYNC_CODE_SIZE);
554 sync_code = ((data[0] << 16) | (data[1] << 8) | data[2]);
556 if (sync_code != 0x000001)
557 return GST_BASE_VIDEO_DECODER_SCAN_RESULT_LOST_SYNC;
559 *size = gst_adapter_masked_scan_uint32 (adapter, 0xffffff00, 0x00000100,
560 SYNC_CODE_SIZE, gst_adapter_available (adapter) - SYNC_CODE_SIZE);
563 return GST_BASE_VIDEO_DECODER_SCAN_RESULT_NEED_DATA;
565 return GST_BASE_VIDEO_DECODER_SCAN_RESULT_OK;
569 gst_vdp_mpeg_dec_flush (GstBaseVideoDecoder * base_video_decoder)
571 GstVdpMpegDec *mpeg_dec = GST_VDP_MPEG_DEC (base_video_decoder);
573 if (mpeg_dec->vdp_info.forward_reference != VDP_INVALID_HANDLE)
574 gst_video_frame_unref (mpeg_dec->f_frame);
575 if (mpeg_dec->vdp_info.backward_reference != VDP_INVALID_HANDLE)
576 gst_video_frame_unref (mpeg_dec->b_frame);
578 gst_vdp_mpeg_dec_init_info (&mpeg_dec->vdp_info);
580 mpeg_dec->prev_packet = -1;
586 gst_vdp_mpeg_dec_start (GstBaseVideoDecoder * base_video_decoder)
588 GstVdpMpegDec *mpeg_dec = GST_VDP_MPEG_DEC (base_video_decoder);
590 gst_vdp_mpeg_dec_init_info (&mpeg_dec->vdp_info);
592 mpeg_dec->decoder = VDP_INVALID_HANDLE;
593 mpeg_dec->state = GST_VDP_MPEG_DEC_STATE_NEED_SEQUENCE;
595 memset (&mpeg_dec->stream_info, 0, sizeof (GstVdpMpegStreamInfo));
597 return GST_BASE_VIDEO_DECODER_CLASS
598 (parent_class)->start (base_video_decoder);
602 gst_vdp_mpeg_dec_stop (GstBaseVideoDecoder * base_video_decoder)
604 GstVdpMpegDec *mpeg_dec = GST_VDP_MPEG_DEC (base_video_decoder);
606 if (mpeg_dec->vdp_info.forward_reference != VDP_INVALID_HANDLE)
607 mpeg_dec->vdp_info.forward_reference = VDP_INVALID_HANDLE;
608 if (mpeg_dec->vdp_info.backward_reference != VDP_INVALID_HANDLE)
609 mpeg_dec->vdp_info.backward_reference = VDP_INVALID_HANDLE;
611 mpeg_dec->state = GST_VDP_MPEG_DEC_STATE_NEED_SEQUENCE;
613 return GST_BASE_VIDEO_DECODER_CLASS (parent_class)->stop (base_video_decoder);
617 gst_vdp_mpeg_dec_base_init (gpointer gclass)
619 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
621 gst_element_class_set_details_simple (element_class,
622 "VDPAU Mpeg Decoder",
624 "Decode mpeg stream with vdpau",
625 "Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>");
627 gst_element_class_add_static_pad_template (element_class,
631 /* initialize the vdpaumpegdecoder's class */
633 gst_vdp_mpeg_dec_class_init (GstVdpMpegDecClass * klass)
635 GstBaseVideoDecoderClass *base_video_decoder_class;
637 base_video_decoder_class = GST_BASE_VIDEO_DECODER_CLASS (klass);
639 base_video_decoder_class->start = gst_vdp_mpeg_dec_start;
640 base_video_decoder_class->stop = gst_vdp_mpeg_dec_stop;
641 base_video_decoder_class->flush = gst_vdp_mpeg_dec_flush;
643 base_video_decoder_class->scan_for_sync = gst_vdp_mpeg_dec_scan_for_sync;
644 base_video_decoder_class->scan_for_packet_end =
645 gst_vdp_mpeg_dec_scan_for_packet_end;
646 base_video_decoder_class->parse_data = gst_vdp_mpeg_dec_parse_data;
648 base_video_decoder_class->handle_frame = gst_vdp_mpeg_dec_handle_frame;
649 base_video_decoder_class->create_frame = gst_vdp_mpeg_dec_create_frame;
653 gst_vdp_mpeg_dec_init_info (VdpPictureInfoMPEG1Or2 * vdp_info)
655 vdp_info->forward_reference = VDP_INVALID_HANDLE;
656 vdp_info->backward_reference = VDP_INVALID_HANDLE;
657 vdp_info->slice_count = 0;
658 vdp_info->picture_structure = 3;
659 vdp_info->picture_coding_type = 0;
660 vdp_info->intra_dc_precision = 0;
661 vdp_info->frame_pred_frame_dct = 1;
662 vdp_info->concealment_motion_vectors = 0;
663 vdp_info->intra_vlc_format = 0;
664 vdp_info->alternate_scan = 0;
665 vdp_info->q_scale_type = 0;
666 vdp_info->top_field_first = 1;
670 gst_vdp_mpeg_dec_init (GstVdpMpegDec * mpeg_dec, GstVdpMpegDecClass * gclass)