mpeg2: don't decode anything before the first sequence_header().
[vaapi:hkaelbers-gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapidecoder_mpeg2.c
1 /*
2  *  gstvaapidecoder_mpeg2.c - MPEG-2 decoder
3  *
4  *  Copyright (C) 2011 Intel Corporation
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * SECTION:gstvaapidecoder_mpeg2
24  * @short_description: MPEG-2 decoder
25  */
26
27 #include "sysdeps.h"
28 #include <string.h>
29 #include <gst/base/gstbitreader.h>
30 #include <gst/codecparsers/gstmpegvideoparser.h>
31 #include "gstvaapidecoder_mpeg2.h"
32 #include "gstvaapidecoder_objects.h"
33 #include "gstvaapidecoder_priv.h"
34 #include "gstvaapidisplay_priv.h"
35 #include "gstvaapiobject_priv.h"
36
37 #define DEBUG 1
38 #include "gstvaapidebug.h"
39
40 G_DEFINE_TYPE(GstVaapiDecoderMpeg2,
41               gst_vaapi_decoder_mpeg2,
42               GST_VAAPI_TYPE_DECODER);
43
44 #define GST_VAAPI_DECODER_MPEG2_GET_PRIVATE(obj)                \
45     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                         \
46                                  GST_VAAPI_TYPE_DECODER_MPEG2,  \
47                                  GstVaapiDecoderMpeg2Private))
48
49 #define READ_UINT8(br, val, nbits) G_STMT_START {  \
50   if (!gst_bit_reader_get_bits_uint8 (br, &val, nbits)) { \
51     GST_WARNING ("failed to read uint8, nbits: %d", nbits); \
52     goto failed; \
53   } \
54 } G_STMT_END
55
56 #define SKIP(reader, nbits) G_STMT_START { \
57   if (!gst_bit_reader_skip (reader, nbits)) { \
58     GST_WARNING ("failed to skip nbits: %d", nbits); \
59     goto failed; \
60   } \
61 } G_STMT_END
62
63 struct _GstVaapiDecoderMpeg2Private {
64     GstVaapiProfile             profile;
65     guint                       width;
66     guint                       height;
67     guint                       fps_n;
68     guint                       fps_d;
69     GstMpegVideoSequenceHdr     seq_hdr;
70     GstMpegVideoSequenceExt     seq_ext;
71     GstMpegVideoPictureHdr      pic_hdr;
72     GstMpegVideoPictureExt      pic_ext;
73     GstMpegVideoQuantMatrixExt  quant_matrix_ext;
74     GstVaapiPicture            *current_picture;
75     GstVaapiPicture            *next_picture;
76     GstVaapiPicture            *prev_picture;
77     GstAdapter                 *adapter;
78     GstClockTime                seq_pts;
79     GstClockTime                gop_pts;
80     GstClockTime                pts_diff;
81     guint                       is_constructed          : 1;
82     guint                       is_opened               : 1;
83     guint                       is_first_field          : 1;
84     guint                       has_seq_ext             : 1;
85     guint                       has_seq_scalable_ext    : 1;
86     guint                       has_pic_ext             : 1;
87     guint                       has_quant_matrix_ext    : 1;
88     guint                       size_changed            : 1;
89     guint                       profile_changed         : 1;
90     guint                       quant_matrix_changed    : 1;
91     guint                       progressive_sequence    : 1;
92     guint                       closed_gop              : 1;
93     guint                       broken_link             : 1;
94 };
95
96 /* VLC decoder from gst-plugins-bad */
97 typedef struct _VLCTable VLCTable;
98 struct _VLCTable {
99     gint  value;
100     guint cword;
101     guint cbits;
102 };
103
104 static gboolean
105 decode_vlc(GstBitReader *br, gint *res, const VLCTable *table, guint length)
106 {
107     guint8 i;
108     guint cbits = 0;
109     guint32 value = 0;
110
111     for (i = 0; i < length; i++) {
112         if (cbits != table[i].cbits) {
113             cbits = table[i].cbits;
114             if (!gst_bit_reader_peek_bits_uint32(br, &value, cbits)) {
115                 goto failed;
116             }
117         }
118
119         if (value == table[i].cword) {
120             SKIP(br, cbits);
121             if (res)
122                 *res = table[i].value;
123             return TRUE;
124         }
125     }
126     GST_DEBUG("failed to find VLC code");
127
128 failed:
129     GST_WARNING("failed to decode VLC, returning");
130     return FALSE;
131 }
132
133 enum {
134     GST_MPEG_VIDEO_MACROBLOCK_ESCAPE = -1,
135 };
136
137 /* Table B-1: Variable length codes for macroblock_address_increment */
138 static const VLCTable mpeg2_mbaddr_vlc_table[] = {
139     {  1, 0x01,  1 },
140     {  2, 0x03,  3 },
141     {  3, 0x02,  3 },
142     {  4, 0x03,  4 },
143     {  5, 0x02,  4 },
144     {  6, 0x03,  5 },
145     {  7, 0x02,  5 },
146     {  8, 0x07,  7 },
147     {  9, 0x06,  7 },
148     { 10, 0x0b,  8 },
149     { 11, 0x0a,  8 },
150     { 12, 0x09,  8 },
151     { 13, 0x08,  8 },
152     { 14, 0x07,  8 },
153     { 15, 0x06,  8 },
154     { 16, 0x17, 10 },
155     { 17, 0x16, 10 },
156     { 18, 0x15, 10 },
157     { 19, 0x14, 10 },
158     { 20, 0x13, 10 },
159     { 21, 0x12, 10 },
160     { 22, 0x23, 11 },
161     { 23, 0x22, 11 },
162     { 24, 0x21, 11 },
163     { 25, 0x20, 11 },
164     { 26, 0x1f, 11 },
165     { 27, 0x1e, 11 },
166     { 28, 0x1d, 11 },
167     { 29, 0x1c, 11 },
168     { 30, 0x1b, 11 },
169     { 31, 0x1a, 11 },
170     { 32, 0x19, 11 },
171     { 33, 0x18, 11 },
172     { GST_MPEG_VIDEO_MACROBLOCK_ESCAPE, 0x08, 11 }
173 };
174
175 static void
176 gst_vaapi_decoder_mpeg2_close(GstVaapiDecoderMpeg2 *decoder)
177 {
178     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
179
180     gst_vaapi_picture_replace(&priv->current_picture, NULL);
181     gst_vaapi_picture_replace(&priv->next_picture,    NULL);
182     gst_vaapi_picture_replace(&priv->prev_picture,    NULL);
183
184     if (priv->adapter) {
185         gst_adapter_clear(priv->adapter);
186         g_object_unref(priv->adapter);
187         priv->adapter = NULL;
188     }
189 }
190
191 static gboolean
192 gst_vaapi_decoder_mpeg2_open(GstVaapiDecoderMpeg2 *decoder, GstBuffer *buffer)
193 {
194     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
195
196     gst_vaapi_decoder_mpeg2_close(decoder);
197
198     priv->adapter = gst_adapter_new();
199     if (!priv->adapter)
200         return FALSE;
201     return TRUE;
202 }
203
204 static void
205 gst_vaapi_decoder_mpeg2_destroy(GstVaapiDecoderMpeg2 *decoder)
206 {
207     gst_vaapi_decoder_mpeg2_close(decoder);
208 }
209
210 static gboolean
211 gst_vaapi_decoder_mpeg2_create(GstVaapiDecoderMpeg2 *decoder)
212 {
213     if (!GST_VAAPI_DECODER_CODEC(decoder))
214         return FALSE;
215     return TRUE;
216 }
217
218 static inline void
219 copy_quant_matrix(guint8 dst[64], const guint8 src[64])
220 {
221     memcpy(dst, src, 64);
222 }
223
224 static GstVaapiDecoderStatus
225 ensure_context(GstVaapiDecoderMpeg2 *decoder)
226 {
227     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
228     GstVaapiProfile profiles[2];
229     GstVaapiEntrypoint entrypoint = GST_VAAPI_ENTRYPOINT_VLD;
230     guint i, n_profiles = 0;
231     gboolean reset_context = FALSE;
232
233     if (priv->profile_changed) {
234         GST_DEBUG("profile changed");
235         priv->profile_changed = FALSE;
236         reset_context         = TRUE;
237
238         profiles[n_profiles++] = priv->profile;
239         if (priv->profile == GST_VAAPI_PROFILE_MPEG2_SIMPLE)
240             profiles[n_profiles++] = GST_VAAPI_PROFILE_MPEG2_MAIN;
241
242         for (i = 0; i < n_profiles; i++) {
243             if (gst_vaapi_display_has_decoder(GST_VAAPI_DECODER_DISPLAY(decoder),
244                                               profiles[i], entrypoint))
245                 break;
246         }
247         if (i == n_profiles)
248             return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
249         priv->profile = profiles[i];
250     }
251
252     if (priv->size_changed) {
253         GST_DEBUG("size changed");
254         priv->size_changed = FALSE;
255         reset_context      = TRUE;
256     }
257
258     if (reset_context) {
259         reset_context = gst_vaapi_decoder_ensure_context(
260             GST_VAAPI_DECODER(decoder),
261             priv->profile,
262             entrypoint,
263             priv->width, priv->height
264         );
265         if (!reset_context)
266             return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
267     }
268     return GST_VAAPI_DECODER_STATUS_SUCCESS;
269 }
270
271 static GstVaapiDecoderStatus
272 ensure_quant_matrix(GstVaapiDecoderMpeg2 *decoder, GstVaapiPicture *picture)
273 {
274     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
275     VAIQMatrixBufferMPEG2 *iq_matrix;
276     guint8 *intra_quant_matrix = NULL;
277     guint8 *non_intra_quant_matrix = NULL;
278     guint8 *chroma_intra_quant_matrix = NULL;
279     guint8 *chroma_non_intra_quant_matrix = NULL;
280
281     if (!priv->quant_matrix_changed)
282         return GST_VAAPI_DECODER_STATUS_SUCCESS;
283
284     priv->quant_matrix_changed = FALSE;
285
286     picture->iq_matrix = GST_VAAPI_IQ_MATRIX_NEW(MPEG2, decoder);
287     if (!picture->iq_matrix) {
288         GST_DEBUG("failed to allocate IQ matrix");
289         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
290     }
291     iq_matrix = picture->iq_matrix->param;
292
293     intra_quant_matrix     = priv->seq_hdr.intra_quantizer_matrix;
294     non_intra_quant_matrix = priv->seq_hdr.non_intra_quantizer_matrix;
295     if (priv->has_quant_matrix_ext) {
296         if (priv->quant_matrix_ext.load_intra_quantiser_matrix)
297             intra_quant_matrix = priv->quant_matrix_ext.intra_quantiser_matrix;
298         if (priv->quant_matrix_ext.load_non_intra_quantiser_matrix)
299             non_intra_quant_matrix = priv->quant_matrix_ext.non_intra_quantiser_matrix;
300         if (priv->quant_matrix_ext.load_chroma_intra_quantiser_matrix)
301             chroma_intra_quant_matrix = priv->quant_matrix_ext.chroma_intra_quantiser_matrix;
302         if (priv->quant_matrix_ext.load_chroma_non_intra_quantiser_matrix)
303             chroma_non_intra_quant_matrix = priv->quant_matrix_ext.chroma_non_intra_quantiser_matrix;
304     }
305
306     iq_matrix->load_intra_quantiser_matrix = intra_quant_matrix != NULL;
307     if (intra_quant_matrix)
308         copy_quant_matrix(iq_matrix->intra_quantiser_matrix,
309                           intra_quant_matrix);
310
311     iq_matrix->load_non_intra_quantiser_matrix = non_intra_quant_matrix != NULL;
312     if (non_intra_quant_matrix)
313         copy_quant_matrix(iq_matrix->non_intra_quantiser_matrix,
314                           non_intra_quant_matrix);
315
316     iq_matrix->load_chroma_intra_quantiser_matrix = chroma_intra_quant_matrix != NULL;
317     if (chroma_intra_quant_matrix)
318         copy_quant_matrix(iq_matrix->chroma_intra_quantiser_matrix,
319                           chroma_intra_quant_matrix);
320
321     iq_matrix->load_chroma_non_intra_quantiser_matrix = chroma_non_intra_quant_matrix != NULL;
322     if (chroma_non_intra_quant_matrix)
323         copy_quant_matrix(iq_matrix->chroma_non_intra_quantiser_matrix,
324                           chroma_non_intra_quant_matrix);
325     return GST_VAAPI_DECODER_STATUS_SUCCESS;
326 }
327
328 static inline GstVaapiDecoderStatus
329 render_picture(GstVaapiDecoderMpeg2 *decoder, GstVaapiPicture *picture)
330 {
331     if (!gst_vaapi_picture_output(picture))
332         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
333     return GST_VAAPI_DECODER_STATUS_SUCCESS;
334 }
335
336 static GstVaapiDecoderStatus
337 decode_current_picture(GstVaapiDecoderMpeg2 *decoder)
338 {
339     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
340     GstVaapiPicture * const picture = priv->current_picture;
341     GstVaapiDecoderStatus status = GST_VAAPI_DECODER_STATUS_SUCCESS;
342
343     if (picture) {
344         if (!gst_vaapi_picture_decode(picture))
345             status = GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
346         if (!GST_VAAPI_PICTURE_IS_REFERENCE(picture)) {
347             if ((priv->prev_picture && priv->next_picture) ||
348                 (priv->closed_gop && priv->next_picture))
349                 status = render_picture(decoder, picture);
350         }
351         gst_vaapi_picture_replace(&priv->current_picture, NULL);
352     }
353     return status;
354 }
355
356 static GstVaapiDecoderStatus
357 decode_sequence(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
358 {
359     GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER(decoder);
360     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
361     GstMpegVideoSequenceHdr * const seq_hdr = &priv->seq_hdr;
362
363     if (!gst_mpeg_video_parse_sequence_header(seq_hdr, buf, buf_size, 0)) {
364         GST_DEBUG("failed to parse sequence header");
365         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
366     }
367
368     priv->fps_n = seq_hdr->fps_n;
369     priv->fps_d = seq_hdr->fps_d;
370     gst_vaapi_decoder_set_framerate(base_decoder, priv->fps_n, priv->fps_d);
371
372     priv->seq_pts = gst_adapter_prev_timestamp(priv->adapter, NULL);
373
374     priv->width                 = seq_hdr->width;
375     priv->height                = seq_hdr->height;
376     priv->has_seq_ext           = FALSE;
377     priv->size_changed          = TRUE;
378     priv->quant_matrix_changed  = TRUE;
379     priv->progressive_sequence  = TRUE;
380     return GST_VAAPI_DECODER_STATUS_SUCCESS;
381 }
382
383 static GstVaapiDecoderStatus
384 decode_sequence_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
385 {
386     GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER(decoder);
387     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
388     GstMpegVideoSequenceExt * const seq_ext = &priv->seq_ext;
389     GstVaapiProfile profile;
390     guint width, height;
391
392     if (!gst_mpeg_video_parse_sequence_extension(seq_ext, buf, buf_size, 0)) {
393         GST_DEBUG("failed to parse sequence-extension");
394         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
395     }
396     priv->has_seq_ext = TRUE;
397     priv->progressive_sequence = seq_ext->progressive;
398
399     width  = (priv->width  & 0x0fff) | ((guint32)seq_ext->horiz_size_ext << 12);
400     height = (priv->height & 0x0fff) | ((guint32)seq_ext->vert_size_ext  << 12);
401     GST_DEBUG("video resolution %ux%u", width, height);
402
403     if (seq_ext->fps_n_ext && seq_ext->fps_d_ext) {
404         priv->fps_n *= seq_ext->fps_n_ext + 1;
405         priv->fps_d *= seq_ext->fps_d_ext + 1;
406         gst_vaapi_decoder_set_framerate(base_decoder, priv->fps_n, priv->fps_d);
407     }
408
409     if (priv->width != width) {
410         priv->width = width;
411         priv->size_changed = TRUE;
412     }
413
414     if (priv->height != height) {
415         priv->height = height;
416         priv->size_changed = TRUE;
417     }
418
419     switch (seq_ext->profile) {
420     case GST_MPEG_VIDEO_PROFILE_SIMPLE:
421         profile = GST_VAAPI_PROFILE_MPEG2_SIMPLE;
422         break;
423     case GST_MPEG_VIDEO_PROFILE_MAIN:
424         profile = GST_VAAPI_PROFILE_MPEG2_MAIN;
425         break;
426     default:
427         GST_DEBUG("unsupported profile %d", seq_ext->profile);
428         return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
429     }
430     if (priv->profile != profile) {
431         priv->profile = profile;
432         priv->profile_changed = TRUE;
433     }
434     return GST_VAAPI_DECODER_STATUS_SUCCESS;
435 }
436
437 static GstVaapiDecoderStatus
438 decode_sequence_end(GstVaapiDecoderMpeg2 *decoder)
439 {
440     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
441     GstVaapiDecoderStatus status;
442
443     if (priv->current_picture) {
444         status = decode_current_picture(decoder);
445         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
446             return status;
447     }
448
449     if (priv->next_picture) {
450         status = render_picture(decoder, priv->next_picture);
451         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
452             return status;
453     }
454     return GST_VAAPI_DECODER_STATUS_END_OF_STREAM;
455 }
456
457 static GstVaapiDecoderStatus
458 decode_quant_matrix_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
459 {
460     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
461     GstMpegVideoQuantMatrixExt * const quant_matrix_ext = &priv->quant_matrix_ext;
462
463     if (!gst_mpeg_video_parse_quant_matrix_extension(quant_matrix_ext, buf, buf_size, 0)) {
464         GST_DEBUG("failed to parse quant-matrix-extension");
465         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
466     }
467     priv->has_quant_matrix_ext = TRUE;
468     priv->quant_matrix_changed = TRUE;
469     return GST_VAAPI_DECODER_STATUS_SUCCESS;
470 }
471
472 static GstVaapiDecoderStatus
473 decode_gop(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
474 {
475     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
476     GstMpegVideoGop gop;
477     GstClockTime pts;
478
479     if (!gst_mpeg_video_parse_gop(&gop, buf, buf_size, 0)) {
480         GST_DEBUG("failed to parse GOP");
481         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
482     }
483
484     priv->closed_gop  = gop.closed_gop;
485     priv->broken_link = gop.broken_link;
486
487     GST_DEBUG("GOP %02u:%02u:%02u:%02u (closed_gop %d, broken_link %d)",
488               gop.hour, gop.minute, gop.second, gop.frame,
489               priv->closed_gop, priv->broken_link);
490
491     pts = GST_SECOND * (gop.hour * 3600 + gop.minute * 60 + gop.second);
492     pts += gst_util_uint64_scale(gop.frame, GST_SECOND * priv->fps_d, priv->fps_n);
493     priv->gop_pts = pts;
494     if (!priv->pts_diff)
495         priv->pts_diff = priv->seq_pts - priv->gop_pts;
496
497     priv->is_first_field = FALSE;
498     return GST_VAAPI_DECODER_STATUS_SUCCESS;
499 }
500
501 static GstVaapiDecoderStatus
502 decode_picture(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
503 {
504     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
505     GstMpegVideoPictureHdr * const pic_hdr = &priv->pic_hdr;
506     GstVaapiPicture *picture;
507     GstVaapiDecoderStatus status;
508     GstClockTime pts;
509
510     status = ensure_context(decoder);
511     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
512         GST_DEBUG("failed to reset context");
513         return status;
514     }
515
516     if (priv->current_picture) {
517         status = decode_current_picture(decoder);
518         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
519             return status;
520     }
521
522     priv->current_picture = GST_VAAPI_PICTURE_NEW(MPEG2, decoder);
523     if (!priv->current_picture) {
524         GST_DEBUG("failed to allocate picture");
525         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
526     }
527     picture = priv->current_picture;
528
529     status = ensure_quant_matrix(decoder, picture);
530     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
531         GST_DEBUG("failed to reset quantizer matrix");
532         return status;
533     }
534
535     if (!gst_mpeg_video_parse_picture_header(pic_hdr, buf, buf_size, 0)) {
536         GST_DEBUG("failed to parse picture header");
537         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
538     }
539     priv->has_pic_ext = FALSE;
540
541     switch (pic_hdr->pic_type) {
542     case GST_MPEG_VIDEO_PICTURE_TYPE_I:
543         picture->type = GST_VAAPI_PICTURE_TYPE_I;
544         break;
545     case GST_MPEG_VIDEO_PICTURE_TYPE_P:
546         picture->type = GST_VAAPI_PICTURE_TYPE_P;
547         break;
548     case GST_MPEG_VIDEO_PICTURE_TYPE_B:
549         picture->type = GST_VAAPI_PICTURE_TYPE_B;
550         break;
551     default:
552         GST_DEBUG("unsupported picture type %d", pic_hdr->pic_type);
553         return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
554     }
555
556     /* Update presentation time */
557     pts = priv->gop_pts;
558     pts += gst_util_uint64_scale(pic_hdr->tsn, GST_SECOND * priv->fps_d, priv->fps_n);
559     picture->pts = pts + priv->pts_diff;
560
561     /* Update reference pictures */
562     if (pic_hdr->pic_type != GST_MPEG_VIDEO_PICTURE_TYPE_B) {
563         GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_REFERENCE);
564         if (priv->next_picture)
565             status = render_picture(decoder, priv->next_picture);
566         gst_vaapi_picture_replace(&priv->prev_picture, priv->next_picture);
567         gst_vaapi_picture_replace(&priv->next_picture, picture);
568     }
569     return status;
570 }
571
572 static GstVaapiDecoderStatus
573 decode_picture_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
574 {
575     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
576     GstMpegVideoPictureExt * const pic_ext = &priv->pic_ext;
577     GstVaapiPicture * const picture = priv->current_picture;
578
579     if (!gst_mpeg_video_parse_picture_extension(pic_ext, buf, buf_size, 0)) {
580         GST_DEBUG("failed to parse picture-extension");
581         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
582     }
583     priv->has_pic_ext = TRUE;
584
585     if (pic_ext->picture_structure == 0 ||
586         (pic_ext->progressive_frame &&
587          pic_ext->picture_structure != GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME)) {
588         GST_WARNING("invalid picture_structure %d, replacing with \"frame\"",
589                     pic_ext->picture_structure);
590         pic_ext->picture_structure = GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME;
591     }
592
593     priv->is_first_field ^= 1;
594     switch (pic_ext->picture_structure) {
595     case GST_MPEG_VIDEO_PICTURE_STRUCTURE_TOP_FIELD:
596         GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_TOP_FIELD);
597         break;
598     case GST_MPEG_VIDEO_PICTURE_STRUCTURE_BOTTOM_FIELD:
599         GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_BOTTOM_FIELD);
600         break;
601     case GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME:
602         priv->is_first_field = TRUE;
603         break;
604     }
605     return GST_VAAPI_DECODER_STATUS_SUCCESS;
606 }
607
608 static inline guint32
609 pack_f_code(guint8 f_code[2][2])
610 {
611     return (((guint32)f_code[0][0] << 12) |
612             ((guint32)f_code[0][1] <<  8) |
613             ((guint32)f_code[1][0] <<  4) |
614             (         f_code[1][1]      ));
615 }
616
617 static gboolean
618 fill_picture(GstVaapiDecoderMpeg2 *decoder, GstVaapiPicture *picture)
619 {
620     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
621     VAPictureParameterBufferMPEG2 * const pic_param = picture->param;
622     GstMpegVideoPictureHdr * const pic_hdr = &priv->pic_hdr;
623     GstMpegVideoPictureExt * const pic_ext = &priv->pic_ext;
624
625     if (!priv->has_pic_ext)
626         return FALSE;
627
628     /* Fill in VAPictureParameterBufferMPEG2 */
629     pic_param->horizontal_size                                          = priv->width;
630     pic_param->vertical_size                                            = priv->height;
631     pic_param->forward_reference_picture                                = VA_INVALID_ID;
632     pic_param->backward_reference_picture                               = VA_INVALID_ID;
633     pic_param->picture_coding_type                                      = pic_hdr->pic_type;
634     pic_param->f_code                                                   = pack_f_code(pic_ext->f_code);
635
636 #define COPY_FIELD(a, b, f) \
637     pic_param->a.b.f = pic_ext->f
638     pic_param->picture_coding_extension.value                           = 0;
639     pic_param->picture_coding_extension.bits.is_first_field             = priv->is_first_field;
640     COPY_FIELD(picture_coding_extension, bits, intra_dc_precision);
641     COPY_FIELD(picture_coding_extension, bits, picture_structure);
642     COPY_FIELD(picture_coding_extension, bits, top_field_first);
643     COPY_FIELD(picture_coding_extension, bits, frame_pred_frame_dct);
644     COPY_FIELD(picture_coding_extension, bits, concealment_motion_vectors);
645     COPY_FIELD(picture_coding_extension, bits, q_scale_type);
646     COPY_FIELD(picture_coding_extension, bits, intra_vlc_format);
647     COPY_FIELD(picture_coding_extension, bits, alternate_scan);
648     COPY_FIELD(picture_coding_extension, bits, repeat_first_field);
649     COPY_FIELD(picture_coding_extension, bits, progressive_frame);
650
651     switch (pic_hdr->pic_type) {
652     case GST_MPEG_VIDEO_PICTURE_TYPE_B:
653         if (priv->next_picture)
654             pic_param->backward_reference_picture = priv->next_picture->surface_id;
655         // fall-through
656     case GST_MPEG_VIDEO_PICTURE_TYPE_P:
657         if (priv->prev_picture)
658             pic_param->forward_reference_picture = priv->prev_picture->surface_id;
659         break;
660     }
661     return TRUE;
662 }
663
664 static GstVaapiDecoderStatus
665 decode_slice(
666     GstVaapiDecoderMpeg2 *decoder,
667     int                   slice_no,
668     guchar               *buf,
669     guint                 buf_size
670 )
671 {
672     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
673     GstVaapiPicture * const picture = priv->current_picture;
674     GstVaapiSlice *slice;
675     VASliceParameterBufferMPEG2 *slice_param;
676     GstBitReader br;
677     gint mb_x, mb_y, mb_inc;
678     guint macroblock_offset;
679     guint8 slice_vertical_position_extension;
680     guint8 quantiser_scale_code;
681     guint8 intra_slice_flag, intra_slice = 0;
682     guint8 extra_bit_slice, junk8;
683
684     GST_DEBUG("slice %d @ %p, %u bytes)", slice_no, buf, buf_size);
685
686     if (picture->slices->len == 0 && !fill_picture(decoder, picture))
687         return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
688
689     slice = GST_VAAPI_SLICE_NEW(MPEG2, decoder, buf, buf_size);
690     if (!slice) {
691         GST_DEBUG("failed to allocate slice");
692         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
693     }
694     gst_vaapi_picture_add_slice(picture, slice);
695
696     /* Parse slice */
697     gst_bit_reader_init(&br, buf, buf_size);
698     if (priv->height > 2800)
699         READ_UINT8(&br, slice_vertical_position_extension, 3);
700     if (priv->has_seq_scalable_ext) {
701         GST_DEBUG("failed to parse slice %d. Unsupported sequence_scalable_extension()", slice_no);
702         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
703     }
704     READ_UINT8(&br, quantiser_scale_code, 5);
705     READ_UINT8(&br, extra_bit_slice, 1);
706     if (extra_bit_slice == 1) {
707         READ_UINT8(&br, intra_slice_flag, 1);
708         if (intra_slice_flag) {
709             READ_UINT8(&br, intra_slice, 1);
710             READ_UINT8(&br, junk8, 7);
711         }
712         READ_UINT8(&br, extra_bit_slice, 1);
713         while (extra_bit_slice == 1) {
714             READ_UINT8(&br, junk8, 8);
715             READ_UINT8(&br, extra_bit_slice, 1);
716         }
717     }
718     macroblock_offset = gst_bit_reader_get_pos(&br);
719
720     mb_y = slice_no;
721     mb_x = -1;
722     do {
723         if (!decode_vlc(&br, &mb_inc, mpeg2_mbaddr_vlc_table,
724                         G_N_ELEMENTS(mpeg2_mbaddr_vlc_table))) {
725             GST_WARNING("failed to decode first macroblock_address_increment");
726             goto failed;
727         }
728         mb_x += mb_inc == GST_MPEG_VIDEO_MACROBLOCK_ESCAPE ? 33 : mb_inc;
729     } while (mb_inc == GST_MPEG_VIDEO_MACROBLOCK_ESCAPE);
730
731     /* Fill in VASliceParameterBufferMPEG2 */
732     slice_param                            = slice->param;
733     slice_param->macroblock_offset         = macroblock_offset;
734     slice_param->slice_horizontal_position = mb_x;
735     slice_param->slice_vertical_position   = mb_y;
736     slice_param->quantiser_scale_code      = quantiser_scale_code;
737     slice_param->intra_slice_flag          = intra_slice;
738     return GST_VAAPI_DECODER_STATUS_SUCCESS;
739
740 failed:
741     return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
742 }
743
744 static inline gint
745 scan_for_start_code(GstAdapter *adapter, guint ofs, guint size, guint32 *scp)
746 {
747     return (gint)gst_adapter_masked_scan_uint32_peek(adapter,
748                                                      0xffffff00, 0x00000100,
749                                                      ofs, size,
750                                                      scp);
751 }
752
753 static GstVaapiDecoderStatus
754 decode_buffer(GstVaapiDecoderMpeg2 *decoder, GstBuffer *buffer)
755 {
756     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
757     GstVaapiDecoderStatus status;
758     guchar *buf;
759     guint buf_size, size;
760     guint32 start_code;
761     guint8 type;
762     gint ofs;
763
764     buf      = GST_BUFFER_DATA(buffer);
765     buf_size = GST_BUFFER_SIZE(buffer);
766     if (!buf && buf_size == 0)
767         return decode_sequence_end(decoder);
768
769     gst_adapter_push(priv->adapter, gst_buffer_ref(buffer));
770
771     size   = gst_adapter_available(priv->adapter);
772     status = GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
773     do {
774         if (size < 8)
775             break;
776         ofs = scan_for_start_code(priv->adapter, 0, size, &start_code);
777         if (ofs < 0)
778             break;
779         gst_adapter_flush(priv->adapter, ofs);
780         size -= ofs;
781
782         status = gst_vaapi_decoder_check_status(GST_VAAPI_DECODER(decoder));
783         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
784             break;
785
786         if (size < 8)
787             break;
788         ofs = scan_for_start_code(priv->adapter, 4, size - 4, NULL);
789         if (ofs < 0)
790             break;
791         gst_adapter_flush(priv->adapter, 4);
792         size -= ofs;
793
794         buffer   = gst_adapter_take_buffer(priv->adapter, ofs - 4);
795         buf      = GST_BUFFER_DATA(buffer);
796         buf_size = GST_BUFFER_SIZE(buffer);
797
798         type = start_code & 0xff;
799         switch (type) {
800         case GST_MPEG_VIDEO_PACKET_PICTURE:
801             if (!priv->width || !priv->height)
802                 break;
803             status = decode_picture(decoder, buf, buf_size);
804             break;
805         case GST_MPEG_VIDEO_PACKET_SEQUENCE:
806             status = decode_sequence(decoder, buf, buf_size);
807             break;
808         case GST_MPEG_VIDEO_PACKET_EXTENSION: {
809             const guchar id = buf[0] >> 4;
810             switch (id) {
811             case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE:
812                 status = decode_sequence_ext(decoder, buf, buf_size);
813                 break;
814             case GST_MPEG_VIDEO_PACKET_EXT_QUANT_MATRIX:
815                 status = decode_quant_matrix_ext(decoder, buf, buf_size);
816                 break;
817             case GST_MPEG_VIDEO_PACKET_EXT_PICTURE:
818                 if (!priv->width || !priv->height)
819                     break;
820                 status = decode_picture_ext(decoder, buf, buf_size);
821                 break;
822             default:
823                 // Ignore unknown extensions
824                 GST_DEBUG("unsupported start-code extension (0x%02x)", id);
825                 break;
826             }
827             break;
828         }
829         case GST_MPEG_VIDEO_PACKET_SEQUENCE_END:
830             status = decode_sequence_end(decoder);
831             break;
832         case GST_MPEG_VIDEO_PACKET_GOP:
833             status = decode_gop(decoder, buf, buf_size);
834             break;
835         case GST_MPEG_VIDEO_PACKET_USER_DATA:
836             // Ignore user-data packets
837             status = GST_VAAPI_DECODER_STATUS_SUCCESS;
838             break;
839         default:
840             if (type >= GST_MPEG_VIDEO_PACKET_SLICE_MIN &&
841                 type <= GST_MPEG_VIDEO_PACKET_SLICE_MAX) {
842                 if (!priv->current_picture)
843                     break;
844                 status = decode_slice(
845                     decoder,
846                     type - GST_MPEG_VIDEO_PACKET_SLICE_MIN,
847                     buf, buf_size
848                 );
849                 break;
850             }
851             else if (type >= 0xb9 && type <= 0xff) {
852                 // Ignore system start codes (PES headers)
853                 status = GST_VAAPI_DECODER_STATUS_SUCCESS;
854                 break;
855             }
856             GST_DEBUG("unsupported start code (0x%02x)", type);
857             status = GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
858             break;
859         }
860         gst_buffer_unref(buffer);
861     } while (status == GST_VAAPI_DECODER_STATUS_SUCCESS);
862     return status;
863 }
864
865 GstVaapiDecoderStatus
866 gst_vaapi_decoder_mpeg2_decode(GstVaapiDecoder *base, GstBuffer *buffer)
867 {
868     GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(base);
869     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
870
871     g_return_val_if_fail(priv->is_constructed,
872                          GST_VAAPI_DECODER_STATUS_ERROR_INIT_FAILED);
873
874     if (!priv->is_opened) {
875         priv->is_opened = gst_vaapi_decoder_mpeg2_open(decoder, buffer);
876         if (!priv->is_opened)
877             return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC;
878     }
879     return decode_buffer(decoder, buffer);
880 }
881
882 static void
883 gst_vaapi_decoder_mpeg2_finalize(GObject *object)
884 {
885     GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(object);
886
887     gst_vaapi_decoder_mpeg2_destroy(decoder);
888
889     G_OBJECT_CLASS(gst_vaapi_decoder_mpeg2_parent_class)->finalize(object);
890 }
891
892 static void
893 gst_vaapi_decoder_mpeg2_constructed(GObject *object)
894 {
895     GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(object);
896     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
897     GObjectClass *parent_class;
898
899     parent_class = G_OBJECT_CLASS(gst_vaapi_decoder_mpeg2_parent_class);
900     if (parent_class->constructed)
901         parent_class->constructed(object);
902
903     priv->is_constructed = gst_vaapi_decoder_mpeg2_create(decoder);
904 }
905
906 static void
907 gst_vaapi_decoder_mpeg2_class_init(GstVaapiDecoderMpeg2Class *klass)
908 {
909     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
910     GstVaapiDecoderClass * const decoder_class = GST_VAAPI_DECODER_CLASS(klass);
911
912     g_type_class_add_private(klass, sizeof(GstVaapiDecoderMpeg2Private));
913
914     object_class->finalize      = gst_vaapi_decoder_mpeg2_finalize;
915     object_class->constructed   = gst_vaapi_decoder_mpeg2_constructed;
916
917     decoder_class->decode       = gst_vaapi_decoder_mpeg2_decode;
918 }
919
920 static void
921 gst_vaapi_decoder_mpeg2_init(GstVaapiDecoderMpeg2 *decoder)
922 {
923     GstVaapiDecoderMpeg2Private *priv;
924
925     priv                        = GST_VAAPI_DECODER_MPEG2_GET_PRIVATE(decoder);
926     decoder->priv               = priv;
927     priv->width                 = 0;
928     priv->height                = 0;
929     priv->fps_n                 = 0;
930     priv->fps_d                 = 0;
931     priv->profile               = GST_VAAPI_PROFILE_MPEG2_SIMPLE;
932     priv->current_picture       = NULL;
933     priv->next_picture          = NULL;
934     priv->prev_picture          = NULL;
935     priv->adapter               = NULL;
936     priv->seq_pts               = GST_CLOCK_TIME_NONE;
937     priv->gop_pts               = GST_CLOCK_TIME_NONE;
938     priv->pts_diff              = 0;
939     priv->is_constructed        = FALSE;
940     priv->is_opened             = FALSE;
941     priv->is_first_field        = FALSE;
942     priv->has_seq_ext           = FALSE;
943     priv->has_seq_scalable_ext  = FALSE;
944     priv->has_pic_ext           = FALSE;
945     priv->has_quant_matrix_ext  = FALSE;
946     priv->size_changed          = FALSE;
947     priv->profile_changed       = FALSE;
948     priv->quant_matrix_changed  = FALSE;
949     priv->progressive_sequence  = FALSE;
950     priv->closed_gop            = FALSE;
951     priv->broken_link           = FALSE;
952 }
953
954 /**
955  * gst_vaapi_decoder_mpeg2_new:
956  * @display: a #GstVaapiDisplay
957  * @caps: a #GstCaps holding codec information
958  *
959  * Creates a new #GstVaapiDecoder for MPEG-2 decoding.  The @caps can
960  * hold extra information like codec-data and pictured coded size.
961  *
962  * Return value: the newly allocated #GstVaapiDecoder object
963  */
964 GstVaapiDecoder *
965 gst_vaapi_decoder_mpeg2_new(GstVaapiDisplay *display, GstCaps *caps)
966 {
967     GstVaapiDecoderMpeg2 *decoder;
968
969     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
970     g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
971
972     decoder = g_object_new(
973         GST_VAAPI_TYPE_DECODER_MPEG2,
974         "display",      display,
975         "caps",         caps,
976         NULL
977     );
978     if (!decoder->priv->is_constructed) {
979         g_object_unref(decoder);
980         return NULL;
981     }
982     return GST_VAAPI_DECODER_CAST(decoder);
983 }