Branch data Line data Source code
1 : : /*
2 : : * GStreamer
3 : : * Copyright (C) 2009 Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
4 : : *
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.
9 : : *
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.
14 : : *
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.
19 : : */
20 : :
21 : : /**
22 : : * SECTION:element-vdpaumpegdec
23 : : *
24 : : * FIXME:Describe vdpaumpegdec here.
25 : : *
26 : : * <refsect2>
27 : : * <title>Example launch line</title>
28 : : * |[
29 : : * gst-launch -v -m fakesrc ! vdpaumpegdec ! fakesink silent=TRUE
30 : : * ]|
31 : : * </refsect2>
32 : : */
33 : :
34 : : #ifdef HAVE_CONFIG_H
35 : : #include <config.h>
36 : : #endif
37 : :
38 : : #include <gst/gst.h>
39 : : #include <gst/base/gstbytereader.h>
40 : : #include <gst/base/gstbitreader.h>
41 : : #include <string.h>
42 : :
43 : : #include "mpegutil.h"
44 : :
45 : : #include "gstvdpmpegdec.h"
46 : :
47 : : GST_DEBUG_CATEGORY_STATIC (gst_vdp_mpeg_dec_debug);
48 : : #define GST_CAT_DEFAULT gst_vdp_mpeg_dec_debug
49 : :
50 : : /* the capabilities of the inputs and outputs.
51 : : *
52 : : * describe the real formats here.
53 : : */
54 : : static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
55 : : GST_PAD_SINK,
56 : : GST_PAD_ALWAYS,
57 : : GST_STATIC_CAPS ("video/mpeg, mpegversion = (int) [ 1, 2 ], "
58 : : "systemstream = (boolean) false")
59 : : );
60 : :
61 : : #define DEBUG_INIT(bla) \
62 : : GST_DEBUG_CATEGORY_INIT (gst_vdp_mpeg_dec_debug, "vdpaumpegdec", 0, \
63 : : "VDPAU mpeg decoder");
64 : :
65 [ + - ][ + - ]: 10 : GST_BOILERPLATE_FULL (GstVdpMpegDec, gst_vdp_mpeg_dec,
66 : 10 : GstVdpDecoder, GST_TYPE_VDP_DECODER, DEBUG_INIT);
67 : :
68 : : static void gst_vdp_mpeg_dec_init_info (VdpPictureInfoMPEG1Or2 * vdp_info);
69 : :
70 : : #define SYNC_CODE_SIZE 3
71 : :
72 : : static VdpDecoderProfile
73 : 0 : gst_vdp_mpeg_dec_get_profile (MPEGSeqExtHdr * hdr)
74 : : {
75 : : VdpDecoderProfile profile;
76 : :
77 [ # # ]: 0 : switch (hdr->profile) {
78 : : case 5:
79 : 0 : profile = VDP_DECODER_PROFILE_MPEG2_SIMPLE;
80 : 0 : break;
81 : : default:
82 : 0 : profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
83 : 0 : break;
84 : : }
85 : :
86 : 0 : return profile;
87 : : }
88 : :
89 : : static gboolean
90 : 0 : gst_vdp_mpeg_dec_handle_picture_coding (GstVdpMpegDec * mpeg_dec,
91 : : GstBuffer * buffer, GstVideoFrame * frame)
92 : : {
93 : : MPEGPictureExt pic_ext;
94 : : VdpPictureInfoMPEG1Or2 *info;
95 : : gint fields;
96 : :
97 : 0 : info = &mpeg_dec->vdp_info;
98 : :
99 [ # # ]: 0 : if (!mpeg_util_parse_picture_coding_extension (&pic_ext, buffer))
100 : 0 : return FALSE;
101 : :
102 : 0 : memcpy (&mpeg_dec->vdp_info.f_code, &pic_ext.f_code, 4);
103 : :
104 : 0 : info->intra_dc_precision = pic_ext.intra_dc_precision;
105 : 0 : info->picture_structure = pic_ext.picture_structure;
106 : 0 : info->top_field_first = pic_ext.top_field_first;
107 : 0 : info->frame_pred_frame_dct = pic_ext.frame_pred_frame_dct;
108 : 0 : info->concealment_motion_vectors = pic_ext.concealment_motion_vectors;
109 : 0 : info->q_scale_type = pic_ext.q_scale_type;
110 : 0 : info->intra_vlc_format = pic_ext.intra_vlc_format;
111 : 0 : info->alternate_scan = pic_ext.alternate_scan;
112 : :
113 : 0 : fields = 2;
114 [ # # ]: 0 : if (pic_ext.picture_structure == 3) {
115 [ # # ]: 0 : if (mpeg_dec->stream_info.interlaced) {
116 [ # # ]: 0 : if (pic_ext.progressive_frame == 0)
117 : 0 : fields = 2;
118 [ # # ][ # # ]: 0 : if (pic_ext.progressive_frame == 0 && pic_ext.repeat_first_field == 0)
119 : 0 : fields = 2;
120 [ # # ][ # # ]: 0 : if (pic_ext.progressive_frame == 1 && pic_ext.repeat_first_field == 1)
121 : 0 : fields = 3;
122 : : } else {
123 [ # # ]: 0 : if (pic_ext.repeat_first_field == 0)
124 : 0 : fields = 2;
125 [ # # ][ # # ]: 0 : if (pic_ext.repeat_first_field == 1 && pic_ext.top_field_first == 0)
126 : 0 : fields = 4;
127 [ # # ][ # # ]: 0 : if (pic_ext.repeat_first_field == 1 && pic_ext.top_field_first == 1)
128 : 0 : fields = 6;
129 : : }
130 : : } else
131 : 0 : fields = 1;
132 : :
133 : 0 : frame->n_fields = fields;
134 : :
135 [ # # ]: 0 : if (pic_ext.top_field_first)
136 : 0 : GST_VIDEO_FRAME_FLAG_SET (frame, GST_VIDEO_FRAME_FLAG_TFF);
137 : :
138 : 0 : return TRUE;
139 : : }
140 : :
141 : : static gboolean
142 : 0 : gst_vdp_mpeg_dec_handle_picture (GstVdpMpegDec * mpeg_dec, GstBuffer * buffer)
143 : : {
144 : : MPEGPictureHdr pic_hdr;
145 : :
146 [ # # ]: 0 : if (!mpeg_util_parse_picture_hdr (&pic_hdr, buffer))
147 : 0 : return FALSE;
148 : :
149 : 0 : mpeg_dec->vdp_info.picture_coding_type = pic_hdr.pic_type;
150 : :
151 [ # # ]: 0 : if (mpeg_dec->stream_info.version == 1) {
152 : 0 : mpeg_dec->vdp_info.full_pel_forward_vector =
153 : 0 : pic_hdr.full_pel_forward_vector;
154 : 0 : mpeg_dec->vdp_info.full_pel_backward_vector =
155 : 0 : pic_hdr.full_pel_backward_vector;
156 : 0 : memcpy (&mpeg_dec->vdp_info.f_code, &pic_hdr.f_code, 4);
157 : : }
158 : :
159 : 0 : mpeg_dec->frame_nr = mpeg_dec->gop_frame + pic_hdr.tsn;
160 : :
161 : 0 : return TRUE;
162 : : }
163 : :
164 : : static gboolean
165 : 0 : gst_vdp_mpeg_dec_handle_gop (GstVdpMpegDec * mpeg_dec, GstBuffer * buffer)
166 : : {
167 : : MPEGGop gop;
168 : : GstClockTime time;
169 : :
170 [ # # ]: 0 : if (!mpeg_util_parse_gop (&gop, buffer))
171 : 0 : return FALSE;
172 : :
173 : 0 : time = GST_SECOND * (gop.hour * 3600 + gop.minute * 60 + gop.second);
174 : :
175 [ # # ][ # # ]: 0 : GST_DEBUG ("gop timestamp: %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
[ # # ][ # # ]
[ # # ]
176 : :
177 : 0 : mpeg_dec->gop_frame =
178 : 0 : gst_util_uint64_scale (time, mpeg_dec->stream_info.fps_n,
179 : 0 : mpeg_dec->stream_info.fps_d * GST_SECOND) + gop.frame;
180 : :
181 [ # # ]: 0 : if (mpeg_dec->state == GST_VDP_MPEG_DEC_STATE_NEED_GOP)
182 : 0 : mpeg_dec->state = GST_VDP_MPEG_DEC_STATE_NEED_DATA;
183 : :
184 : 0 : return TRUE;
185 : : }
186 : :
187 : : static gboolean
188 : 0 : gst_vdp_mpeg_dec_handle_quant_matrix (GstVdpMpegDec * mpeg_dec,
189 : : GstBuffer * buffer)
190 : : {
191 : : MPEGQuantMatrix qm;
192 : :
193 [ # # ]: 0 : if (!mpeg_util_parse_quant_matrix (&qm, buffer))
194 : 0 : return FALSE;
195 : :
196 : 0 : memcpy (&mpeg_dec->vdp_info.intra_quantizer_matrix,
197 : : &qm.intra_quantizer_matrix, 64);
198 : 0 : memcpy (&mpeg_dec->vdp_info.non_intra_quantizer_matrix,
199 : : &qm.non_intra_quantizer_matrix, 64);
200 : 0 : return TRUE;
201 : : }
202 : :
203 : : static GstFlowReturn
204 : 0 : gst_vdp_mpeg_dec_handle_sequence (GstVdpMpegDec * mpeg_dec,
205 : : GstBuffer * seq, GstBuffer * seq_ext)
206 : : {
207 : 0 : GstBaseVideoDecoder *base_video_decoder = GST_BASE_VIDEO_DECODER (mpeg_dec);
208 : :
209 : : MPEGSeqHdr hdr;
210 : : GstVdpMpegStreamInfo stream_info;
211 : :
212 [ # # ]: 0 : if (!mpeg_util_parse_sequence_hdr (&hdr, seq))
213 : 0 : return GST_FLOW_CUSTOM_ERROR;
214 : :
215 : 0 : memcpy (&mpeg_dec->vdp_info.intra_quantizer_matrix,
216 : : &hdr.intra_quantizer_matrix, 64);
217 : 0 : memcpy (&mpeg_dec->vdp_info.non_intra_quantizer_matrix,
218 : : &hdr.non_intra_quantizer_matrix, 64);
219 : :
220 : 0 : stream_info.width = hdr.width;
221 : 0 : stream_info.height = hdr.height;
222 : :
223 : 0 : stream_info.fps_n = hdr.fps_n;
224 : 0 : stream_info.fps_d = hdr.fps_d;
225 : :
226 : 0 : stream_info.par_n = hdr.par_w;
227 : 0 : stream_info.par_d = hdr.par_h;
228 : :
229 : 0 : stream_info.interlaced = FALSE;
230 : 0 : stream_info.version = 1;
231 : 0 : stream_info.profile = VDP_DECODER_PROFILE_MPEG1;
232 : :
233 [ # # ]: 0 : if (seq_ext) {
234 : : MPEGSeqExtHdr ext;
235 : :
236 [ # # ]: 0 : if (!mpeg_util_parse_sequence_extension (&ext, seq_ext))
237 : 0 : return GST_FLOW_CUSTOM_ERROR;
238 : :
239 : 0 : stream_info.fps_n *= (ext.fps_n_ext + 1);
240 : 0 : stream_info.fps_d *= (ext.fps_d_ext + 1);
241 : :
242 : 0 : stream_info.width += (ext.horiz_size_ext << 12);
243 : 0 : stream_info.height += (ext.vert_size_ext << 12);
244 : :
245 : 0 : stream_info.interlaced = !ext.progressive;
246 : 0 : stream_info.version = 2;
247 : 0 : stream_info.profile = gst_vdp_mpeg_dec_get_profile (&ext);
248 : : }
249 : :
250 [ # # ]: 0 : if (memcmp (&mpeg_dec->stream_info, &stream_info,
251 : : sizeof (GstVdpMpegStreamInfo)) != 0) {
252 : : GstVideoState state;
253 : : GstFlowReturn ret;
254 : :
255 : 0 : state = gst_base_video_decoder_get_state (base_video_decoder);
256 : :
257 : 0 : state.width = stream_info.width;
258 : 0 : state.height = stream_info.height;
259 : :
260 : 0 : state.fps_n = stream_info.fps_n;
261 : 0 : state.fps_d = stream_info.fps_d;
262 : :
263 : 0 : state.par_n = stream_info.par_n;
264 : 0 : state.par_d = stream_info.par_d;
265 : :
266 : 0 : state.interlaced = stream_info.interlaced;
267 : :
268 : 0 : gst_base_video_decoder_set_state (base_video_decoder, state);
269 : :
270 : 0 : ret = gst_vdp_decoder_init_decoder (GST_VDP_DECODER (mpeg_dec),
271 : : stream_info.profile, 2);
272 [ # # ]: 0 : if (ret != GST_FLOW_OK)
273 : 0 : return ret;
274 : :
275 : 0 : memcpy (&mpeg_dec->stream_info, &stream_info,
276 : : sizeof (GstVdpMpegStreamInfo));
277 : : }
278 : :
279 : 0 : mpeg_dec->state = GST_VDP_MPEG_DEC_STATE_NEED_DATA;
280 : :
281 : 0 : return GST_FLOW_OK;
282 : : }
283 : :
284 : : static GstFlowReturn
285 : 0 : gst_vdp_mpeg_dec_handle_frame (GstBaseVideoDecoder * base_video_decoder,
286 : : GstVideoFrame * frame, GstClockTimeDiff deadline)
287 : : {
288 : 0 : GstVdpMpegDec *mpeg_dec = GST_VDP_MPEG_DEC (base_video_decoder);
289 : :
290 : : VdpPictureInfoMPEG1Or2 *info;
291 : : GstVdpMpegFrame *mpeg_frame;
292 : :
293 : : GstFlowReturn ret;
294 : : VdpBitstreamBuffer vbit[1];
295 : : GstVdpVideoBuffer *outbuf;
296 : :
297 : : /* MPEG_PACKET_SEQUENCE */
298 : 0 : mpeg_frame = GST_VDP_MPEG_FRAME (frame);
299 [ # # ]: 0 : if (mpeg_frame->seq) {
300 : 0 : ret = gst_vdp_mpeg_dec_handle_sequence (mpeg_dec, mpeg_frame->seq,
301 : : mpeg_frame->seq_ext);
302 [ # # ]: 0 : if (ret != GST_FLOW_OK) {
303 : 0 : gst_base_video_decoder_skip_frame (base_video_decoder, frame);
304 : 0 : return ret;
305 : : }
306 : : }
307 : :
308 [ # # ]: 0 : if (mpeg_dec->state == GST_VDP_MPEG_DEC_STATE_NEED_SEQUENCE) {
309 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec, "Drop frame since we haven't found a "
310 : : "MPEG_PACKET_SEQUENCE yet");
311 : :
312 : 0 : gst_base_video_decoder_skip_frame (base_video_decoder, frame);
313 : 0 : return GST_FLOW_OK;
314 : : }
315 : :
316 : : /* MPEG_PACKET_PICTURE */
317 [ # # ]: 0 : if (mpeg_frame->pic)
318 : 0 : gst_vdp_mpeg_dec_handle_picture (mpeg_dec, mpeg_frame->pic);
319 : :
320 : : /* MPEG_PACKET_EXT_PICTURE_CODING */
321 [ # # ]: 0 : if (mpeg_frame->pic_ext)
322 : 0 : gst_vdp_mpeg_dec_handle_picture_coding (mpeg_dec, mpeg_frame->pic_ext,
323 : : frame);
324 : :
325 : : /* MPEG_PACKET_GOP */
326 [ # # ]: 0 : if (mpeg_frame->gop)
327 : 0 : gst_vdp_mpeg_dec_handle_gop (mpeg_dec, mpeg_frame->gop);
328 : :
329 : : /* MPEG_PACKET_EXT_QUANT_MATRIX */
330 [ # # ]: 0 : if (mpeg_frame->qm_ext)
331 : 0 : gst_vdp_mpeg_dec_handle_quant_matrix (mpeg_dec, mpeg_frame->qm_ext);
332 : :
333 : :
334 : 0 : info = &mpeg_dec->vdp_info;
335 : :
336 : 0 : info->slice_count = mpeg_frame->n_slices;
337 : :
338 : : /* check if we can decode the frame */
339 [ # # ]: 0 : if (info->picture_coding_type != I_FRAME
340 [ # # ]: 0 : && info->backward_reference == VDP_INVALID_HANDLE) {
341 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec,
342 : : "Drop frame since we haven't got an I_FRAME yet");
343 : :
344 : 0 : gst_base_video_decoder_skip_frame (base_video_decoder, frame);
345 : 0 : return GST_FLOW_OK;
346 : : }
347 [ # # ]: 0 : if (info->picture_coding_type == B_FRAME
348 [ # # ]: 0 : && info->forward_reference == VDP_INVALID_HANDLE) {
349 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec,
350 : : "Drop frame since we haven't got two non B_FRAMES yet");
351 : :
352 : 0 : gst_base_video_decoder_skip_frame (base_video_decoder, frame);
353 : 0 : return GST_FLOW_OK;
354 : : }
355 : :
356 : :
357 [ # # ]: 0 : if (info->picture_coding_type != B_FRAME) {
358 [ # # ]: 0 : if (info->backward_reference != VDP_INVALID_HANDLE) {
359 : 0 : gst_base_video_decoder_finish_frame (base_video_decoder,
360 : : mpeg_dec->b_frame);
361 : : }
362 : :
363 [ # # ]: 0 : if (info->forward_reference != VDP_INVALID_HANDLE) {
364 : 0 : gst_video_frame_unref (mpeg_dec->f_frame);
365 : 0 : info->forward_reference = VDP_INVALID_HANDLE;
366 : : }
367 : :
368 : 0 : info->forward_reference = info->backward_reference;
369 : 0 : mpeg_dec->f_frame = mpeg_dec->b_frame;
370 : :
371 : 0 : info->backward_reference = VDP_INVALID_HANDLE;
372 : : }
373 : :
374 : : /* decode */
375 : 0 : vbit[0].struct_version = VDP_BITSTREAM_BUFFER_VERSION;
376 : 0 : vbit[0].bitstream = GST_BUFFER_DATA (mpeg_frame->slices);
377 : 0 : vbit[0].bitstream_bytes = GST_BUFFER_SIZE (mpeg_frame->slices);
378 : :
379 : 0 : ret = gst_vdp_decoder_render (GST_VDP_DECODER (mpeg_dec),
380 : : (VdpPictureInfo *) info, 1, vbit, &outbuf);
381 [ # # ]: 0 : if (ret != GST_FLOW_OK)
382 : 0 : return ret;
383 : :
384 : 0 : frame->src_buffer = GST_BUFFER_CAST (outbuf);
385 : :
386 [ # # ]: 0 : if (info->picture_coding_type == B_FRAME) {
387 : 0 : gst_base_video_decoder_finish_frame (base_video_decoder, frame);
388 : : } else {
389 : 0 : info->backward_reference = GST_VDP_VIDEO_BUFFER (outbuf)->surface;
390 : 0 : mpeg_dec->b_frame = gst_video_frame_ref (frame);
391 : : }
392 : :
393 : 0 : return GST_FLOW_OK;
394 : : }
395 : :
396 : : static GstVideoFrame *
397 : 0 : gst_vdp_mpeg_dec_create_frame (GstBaseVideoDecoder * base_video_decoder)
398 : : {
399 : 0 : return GST_VIDEO_FRAME (gst_vdp_mpeg_frame_new ());
400 : : }
401 : :
402 : : static GstFlowReturn
403 : 0 : gst_vdp_mpeg_dec_parse_data (GstBaseVideoDecoder * base_video_decoder,
404 : : GstBuffer * buf, gboolean at_eos, GstVideoFrame * frame)
405 : : {
406 : 0 : GstVdpMpegDec *mpeg_dec = GST_VDP_MPEG_DEC (base_video_decoder);
407 : :
408 : : GstVdpMpegFrame *mpeg_frame;
409 : 0 : GstFlowReturn ret = GST_FLOW_OK;
410 : 0 : GstBitReader b_reader = GST_BIT_READER_INIT_FROM_BUFFER (buf);
411 : : guint8 start_code;
412 : :
413 : : /* skip sync_code */
414 : 0 : gst_bit_reader_skip (&b_reader, 8 * 3);
415 : :
416 : : /* start_code */
417 [ # # ]: 0 : if (!gst_bit_reader_get_bits_uint8 (&b_reader, &start_code, 8))
418 : 0 : return GST_FLOW_ERROR;
419 : :
420 : 0 : mpeg_frame = GST_VDP_MPEG_FRAME_CAST (frame);
421 : :
422 [ # # ]: 0 : if (start_code >= MPEG_PACKET_SLICE_MIN
423 [ # # ]: 0 : && start_code <= MPEG_PACKET_SLICE_MAX) {
424 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_SLICE");
425 : :
426 : 0 : gst_vdp_mpeg_frame_add_slice (mpeg_frame, buf);
427 : 0 : goto done;
428 : : }
429 : :
430 [ # # # # : 0 : switch (start_code) {
# ]
431 : : case MPEG_PACKET_SEQUENCE:
432 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_SEQUENCE");
433 : :
434 [ # # ]: 0 : if (mpeg_dec->prev_packet != -1)
435 : 0 : ret = gst_base_video_decoder_have_frame (base_video_decoder, FALSE,
436 : : (GstVideoFrame **) & mpeg_frame);
437 : :
438 : 0 : mpeg_frame->seq = buf;
439 : 0 : break;
440 : :
441 : : case MPEG_PACKET_PICTURE:
442 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_PICTURE");
443 : :
444 [ # # ][ # # ]: 0 : if (mpeg_dec->prev_packet != MPEG_PACKET_SEQUENCE &&
445 : 0 : mpeg_dec->prev_packet != MPEG_PACKET_GOP)
446 : 0 : ret = gst_base_video_decoder_have_frame (base_video_decoder, FALSE,
447 : : (GstVideoFrame **) & mpeg_frame);
448 : :
449 : 0 : mpeg_frame->pic = buf;
450 : 0 : break;
451 : :
452 : : case MPEG_PACKET_GOP:
453 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_GOP");
454 : :
455 [ # # ]: 0 : if (mpeg_dec->prev_packet != MPEG_PACKET_SEQUENCE)
456 : 0 : ret = gst_base_video_decoder_have_frame (base_video_decoder, FALSE,
457 : : (GstVideoFrame **) & mpeg_frame);
458 : :
459 : 0 : mpeg_frame->gop = buf;
460 : 0 : break;
461 : :
462 : : case MPEG_PACKET_EXTENSION:
463 : : {
464 : : guint8 ext_code;
465 : :
466 : : /* ext_code */
467 [ # # ]: 0 : if (!gst_bit_reader_get_bits_uint8 (&b_reader, &ext_code, 4)) {
468 : 0 : ret = GST_FLOW_ERROR;
469 : 0 : gst_buffer_unref (buf);
470 : 0 : goto done;
471 : : }
472 : :
473 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_EXTENSION: %d", ext_code);
474 : :
475 [ # # # # : 0 : switch (ext_code) {
# ]
476 : : case MPEG_PACKET_EXT_SEQUENCE:
477 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_EXT_SEQUENCE");
478 : :
479 : :
480 : 0 : mpeg_frame->seq_ext = buf;
481 : :
482 : : /* so that we don't finish the frame if we get a MPEG_PACKET_PICTURE
483 : : * or MPEG_PACKET_GOP after this */
484 : 0 : start_code = MPEG_PACKET_SEQUENCE;
485 : 0 : break;
486 : :
487 : : case MPEG_PACKET_EXT_SEQUENCE_DISPLAY:
488 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_EXT_SEQUENCE_DISPLAY");
489 : :
490 : : /* so that we don't finish the frame if we get a MPEG_PACKET_PICTURE
491 : : * or MPEG_PACKET_GOP after this */
492 : 0 : start_code = MPEG_PACKET_SEQUENCE;
493 : 0 : break;
494 : :
495 : : case MPEG_PACKET_EXT_PICTURE_CODING:
496 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_EXT_PICTURE_CODING");
497 : :
498 : 0 : mpeg_frame->pic_ext = buf;
499 : 0 : break;
500 : :
501 : : case MPEG_PACKET_EXT_QUANT_MATRIX:
502 [ # # ]: 0 : GST_DEBUG_OBJECT (mpeg_dec, "MPEG_PACKET_EXT_QUANT_MATRIX");
503 : :
504 : 0 : mpeg_frame->qm_ext = buf;
505 : 0 : break;
506 : :
507 : : default:
508 : 0 : gst_buffer_unref (buf);
509 : : }
510 : 0 : break;
511 : : }
512 : :
513 : : default:
514 : 0 : gst_buffer_unref (buf);
515 : : }
516 : :
517 [ # # ][ # # ]: 0 : if (at_eos && mpeg_frame->slices)
518 : 0 : ret = gst_base_video_decoder_have_frame (base_video_decoder, TRUE, NULL);
519 : :
520 : : done:
521 : 0 : mpeg_dec->prev_packet = start_code;
522 : :
523 : 0 : return ret;
524 : : }
525 : :
526 : : static gint
527 : 0 : gst_vdp_mpeg_dec_scan_for_sync (GstBaseVideoDecoder * base_video_decoder,
528 : : GstAdapter * adapter)
529 : : {
530 : : gint m;
531 : :
532 : 0 : m = gst_adapter_masked_scan_uint32 (adapter, 0xffffff00, 0x00000100, 0,
533 : : gst_adapter_available (adapter));
534 [ # # ]: 0 : if (m == -1)
535 : 0 : return gst_adapter_available (adapter) - SYNC_CODE_SIZE;
536 : :
537 : 0 : return m;
538 : : }
539 : :
540 : : static GstBaseVideoDecoderScanResult
541 : 0 : gst_vdp_mpeg_dec_scan_for_packet_end (GstBaseVideoDecoder * base_video_decoder,
542 : : GstAdapter * adapter, guint * size, gboolean at_eos)
543 : : {
544 : : guint8 *data;
545 : : guint32 sync_code;
546 : :
547 : 0 : data = g_slice_alloc (SYNC_CODE_SIZE);
548 : 0 : gst_adapter_copy (adapter, data, 0, SYNC_CODE_SIZE);
549 : 0 : sync_code = ((data[0] << 16) | (data[1] << 8) | data[2]);
550 : :
551 [ # # ]: 0 : if (sync_code != 0x000001)
552 : 0 : return GST_BASE_VIDEO_DECODER_SCAN_RESULT_LOST_SYNC;
553 : :
554 : 0 : *size = gst_adapter_masked_scan_uint32 (adapter, 0xffffff00, 0x00000100,
555 : 0 : SYNC_CODE_SIZE, gst_adapter_available (adapter) - SYNC_CODE_SIZE);
556 : :
557 [ # # ]: 0 : if (*size == -1)
558 : 0 : return GST_BASE_VIDEO_DECODER_SCAN_RESULT_NEED_DATA;
559 : :
560 : 0 : return GST_BASE_VIDEO_DECODER_SCAN_RESULT_OK;
561 : : }
562 : :
563 : : static gboolean
564 : 0 : gst_vdp_mpeg_dec_flush (GstBaseVideoDecoder * base_video_decoder)
565 : : {
566 : 0 : GstVdpMpegDec *mpeg_dec = GST_VDP_MPEG_DEC (base_video_decoder);
567 : :
568 [ # # ]: 0 : if (mpeg_dec->vdp_info.forward_reference != VDP_INVALID_HANDLE)
569 : 0 : gst_video_frame_unref (mpeg_dec->f_frame);
570 [ # # ]: 0 : if (mpeg_dec->vdp_info.backward_reference != VDP_INVALID_HANDLE)
571 : 0 : gst_video_frame_unref (mpeg_dec->b_frame);
572 : :
573 : 0 : gst_vdp_mpeg_dec_init_info (&mpeg_dec->vdp_info);
574 : :
575 : 0 : mpeg_dec->prev_packet = -1;
576 : :
577 : 0 : return TRUE;
578 : : }
579 : :
580 : : static gboolean
581 : 0 : gst_vdp_mpeg_dec_start (GstBaseVideoDecoder * base_video_decoder)
582 : : {
583 : 0 : GstVdpMpegDec *mpeg_dec = GST_VDP_MPEG_DEC (base_video_decoder);
584 : :
585 : 0 : gst_vdp_mpeg_dec_init_info (&mpeg_dec->vdp_info);
586 : :
587 : 0 : mpeg_dec->decoder = VDP_INVALID_HANDLE;
588 : 0 : mpeg_dec->state = GST_VDP_MPEG_DEC_STATE_NEED_SEQUENCE;
589 : :
590 : 0 : memset (&mpeg_dec->stream_info, 0, sizeof (GstVdpMpegStreamInfo));
591 : :
592 : 0 : return GST_BASE_VIDEO_DECODER_CLASS
593 : 0 : (parent_class)->start (base_video_decoder);
594 : : }
595 : :
596 : : static gboolean
597 : 0 : gst_vdp_mpeg_dec_stop (GstBaseVideoDecoder * base_video_decoder)
598 : : {
599 : 0 : GstVdpMpegDec *mpeg_dec = GST_VDP_MPEG_DEC (base_video_decoder);
600 : :
601 [ # # ]: 0 : if (mpeg_dec->vdp_info.forward_reference != VDP_INVALID_HANDLE)
602 : 0 : mpeg_dec->vdp_info.forward_reference = VDP_INVALID_HANDLE;
603 [ # # ]: 0 : if (mpeg_dec->vdp_info.backward_reference != VDP_INVALID_HANDLE)
604 : 0 : mpeg_dec->vdp_info.backward_reference = VDP_INVALID_HANDLE;
605 : :
606 : 0 : mpeg_dec->state = GST_VDP_MPEG_DEC_STATE_NEED_SEQUENCE;
607 : :
608 : 0 : return GST_BASE_VIDEO_DECODER_CLASS (parent_class)->stop (base_video_decoder);
609 : : }
610 : :
611 : : static void
612 : 3 : gst_vdp_mpeg_dec_base_init (gpointer gclass)
613 : : {
614 : 3 : GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
615 : :
616 : 3 : gst_element_class_set_details_simple (element_class,
617 : : "VDPAU Mpeg Decoder",
618 : : "Decoder",
619 : : "Decode mpeg stream with vdpau",
620 : : "Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>");
621 : :
622 : 3 : gst_element_class_add_pad_template (element_class,
623 : : gst_static_pad_template_get (&sink_template));
624 : 3 : }
625 : :
626 : : /* initialize the vdpaumpegdecoder's class */
627 : : static void
628 : 3 : gst_vdp_mpeg_dec_class_init (GstVdpMpegDecClass * klass)
629 : : {
630 : : GstBaseVideoDecoderClass *base_video_decoder_class;
631 : :
632 : 3 : base_video_decoder_class = GST_BASE_VIDEO_DECODER_CLASS (klass);
633 : :
634 : 3 : base_video_decoder_class->start = gst_vdp_mpeg_dec_start;
635 : 3 : base_video_decoder_class->stop = gst_vdp_mpeg_dec_stop;
636 : 3 : base_video_decoder_class->flush = gst_vdp_mpeg_dec_flush;
637 : :
638 : 3 : base_video_decoder_class->scan_for_sync = gst_vdp_mpeg_dec_scan_for_sync;
639 : 3 : base_video_decoder_class->scan_for_packet_end =
640 : : gst_vdp_mpeg_dec_scan_for_packet_end;
641 : 3 : base_video_decoder_class->parse_data = gst_vdp_mpeg_dec_parse_data;
642 : :
643 : 3 : base_video_decoder_class->handle_frame = gst_vdp_mpeg_dec_handle_frame;
644 : 3 : base_video_decoder_class->create_frame = gst_vdp_mpeg_dec_create_frame;
645 : 3 : }
646 : :
647 : : static void
648 : 0 : gst_vdp_mpeg_dec_init_info (VdpPictureInfoMPEG1Or2 * vdp_info)
649 : : {
650 : 0 : vdp_info->forward_reference = VDP_INVALID_HANDLE;
651 : 0 : vdp_info->backward_reference = VDP_INVALID_HANDLE;
652 : 0 : vdp_info->slice_count = 0;
653 : 0 : vdp_info->picture_structure = 3;
654 : 0 : vdp_info->picture_coding_type = 0;
655 : 0 : vdp_info->intra_dc_precision = 0;
656 : 0 : vdp_info->frame_pred_frame_dct = 1;
657 : 0 : vdp_info->concealment_motion_vectors = 0;
658 : 0 : vdp_info->intra_vlc_format = 0;
659 : 0 : vdp_info->alternate_scan = 0;
660 : 0 : vdp_info->q_scale_type = 0;
661 : 0 : vdp_info->top_field_first = 1;
662 : 0 : }
663 : :
664 : : static void
665 : 1 : gst_vdp_mpeg_dec_init (GstVdpMpegDec * mpeg_dec, GstVdpMpegDecClass * gclass)
666 : : {
667 : 1 : }
|