mpeg2: fix PAR calculation from commit 132922d.
[vaapi: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_dpb.h"
34 #include "gstvaapidecoder_priv.h"
35 #include "gstvaapidisplay_priv.h"
36 #include "gstvaapiobject_priv.h"
37
38 #define DEBUG 1
39 #include "gstvaapidebug.h"
40
41 G_DEFINE_TYPE(GstVaapiDecoderMpeg2,
42               gst_vaapi_decoder_mpeg2,
43               GST_VAAPI_TYPE_DECODER)
44
45 #define GST_VAAPI_DECODER_MPEG2_GET_PRIVATE(obj)                \
46     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                         \
47                                  GST_VAAPI_TYPE_DECODER_MPEG2,  \
48                                  GstVaapiDecoderMpeg2Private))
49
50 #define READ_UINT8(br, val, nbits) G_STMT_START {  \
51   if (!gst_bit_reader_get_bits_uint8 (br, &val, nbits)) { \
52     GST_WARNING ("failed to read uint8, nbits: %d", nbits); \
53     goto failed; \
54   } \
55 } G_STMT_END
56
57 #define SKIP(reader, nbits) G_STMT_START { \
58   if (!gst_bit_reader_skip (reader, nbits)) { \
59     GST_WARNING ("failed to skip nbits: %d", nbits); \
60     goto failed; \
61   } \
62 } G_STMT_END
63
64 /* PTS Generator */
65 typedef struct _PTSGenerator PTSGenerator;
66 struct _PTSGenerator {
67     GstClockTime        gop_pts; // Current GOP PTS
68     GstClockTime        max_pts; // Max picture PTS
69     guint               gop_tsn; // Absolute GOP TSN
70     guint               max_tsn; // Max picture TSN, relative to last GOP TSN
71     guint               ovl_tsn; // How many times TSN overflowed since GOP
72     guint               lst_tsn; // Last picture TSN
73     guint               fps_n;
74     guint               fps_d;
75 };
76
77 static void
78 pts_init(PTSGenerator *tsg)
79 {
80     tsg->gop_pts = GST_CLOCK_TIME_NONE;
81     tsg->max_pts = GST_CLOCK_TIME_NONE;
82     tsg->gop_tsn = 0;
83     tsg->max_tsn = 0;
84     tsg->ovl_tsn = 0;
85     tsg->lst_tsn = 0;
86     tsg->fps_n   = 0;
87     tsg->fps_d   = 0;
88 }
89
90 static inline GstClockTime
91 pts_get_duration(PTSGenerator *tsg, guint num_frames)
92 {
93     return gst_util_uint64_scale(num_frames,
94                                  GST_SECOND * tsg->fps_d, tsg->fps_n);
95 }
96
97 static inline guint
98 pts_get_poc(PTSGenerator *tsg)
99 {
100     return tsg->gop_tsn + tsg->ovl_tsn * 1024 + tsg->lst_tsn;
101 }
102
103 static void
104 pts_set_framerate(PTSGenerator *tsg, guint fps_n, guint fps_d)
105 {
106     tsg->fps_n = fps_n;
107     tsg->fps_d = fps_d;
108 }
109
110 static void
111 pts_sync(PTSGenerator *tsg, GstClockTime gop_pts)
112 {
113     guint gop_tsn;
114
115     if (!GST_CLOCK_TIME_IS_VALID(gop_pts) ||
116         (GST_CLOCK_TIME_IS_VALID(tsg->max_pts) && tsg->max_pts >= gop_pts)) {
117         /* Invalid GOP PTS, interpolate from the last known picture PTS */
118         if (GST_CLOCK_TIME_IS_VALID(tsg->max_pts)) {
119             gop_pts = tsg->max_pts + pts_get_duration(tsg, 1);
120             gop_tsn = tsg->gop_tsn + tsg->ovl_tsn * 1024 + tsg->max_tsn + 1;
121         }
122         else {
123             gop_pts = 0;
124             gop_tsn = 0;
125         }
126     }
127     else {
128         /* Interpolate GOP TSN from this valid PTS */
129         if (GST_CLOCK_TIME_IS_VALID(tsg->gop_pts))
130             gop_tsn = tsg->gop_tsn + gst_util_uint64_scale(
131                 gop_pts - tsg->gop_pts + pts_get_duration(tsg, 1) - 1,
132                 tsg->fps_n, GST_SECOND * tsg->fps_d);
133         else
134             gop_tsn = 0;
135     }
136
137     tsg->gop_pts = gop_pts;
138     tsg->gop_tsn = gop_tsn;
139     tsg->max_tsn = 0;
140     tsg->ovl_tsn = 0;
141     tsg->lst_tsn = 0;
142 }
143
144 static GstClockTime
145 pts_eval(PTSGenerator *tsg, GstClockTime pic_pts, guint pic_tsn)
146 {
147     GstClockTime pts;
148
149     if (!GST_CLOCK_TIME_IS_VALID(tsg->gop_pts))
150         tsg->gop_pts = 0;
151
152     pts = tsg->gop_pts + pts_get_duration(tsg, tsg->ovl_tsn * 1024 + pic_tsn);
153
154     if (!GST_CLOCK_TIME_IS_VALID(tsg->max_pts) || tsg->max_pts < pts)
155         tsg->max_pts = pts;
156
157     if (tsg->max_tsn < pic_tsn)
158         tsg->max_tsn = pic_tsn;
159     else if (tsg->max_tsn == 1023 && pic_tsn < tsg->lst_tsn) { /* TSN wrapped */
160         tsg->max_tsn = pic_tsn;
161         tsg->ovl_tsn++;
162     }
163     tsg->lst_tsn = pic_tsn;
164     return pts;
165 }
166
167 struct _GstVaapiDecoderMpeg2Private {
168     GstVaapiProfile             profile;
169     GstVaapiProfile             hw_profile;
170     guint                       width;
171     guint                       height;
172     guint                       fps_n;
173     guint                       fps_d;
174     GstMpegVideoSequenceHdr     seq_hdr;
175     GstMpegVideoSequenceExt     seq_ext;
176     GstMpegVideoPictureHdr      pic_hdr;
177     GstMpegVideoPictureExt      pic_ext;
178     GstMpegVideoQuantMatrixExt  quant_matrix_ext;
179     GstVaapiPicture            *current_picture;
180     GstVaapiDpb                *dpb;
181     GstAdapter                 *adapter;
182     PTSGenerator                tsg;
183     guint                       is_constructed          : 1;
184     guint                       is_opened               : 1;
185     guint                       has_seq_ext             : 1;
186     guint                       has_seq_scalable_ext    : 1;
187     guint                       has_pic_ext             : 1;
188     guint                       has_quant_matrix_ext    : 1;
189     guint                       size_changed            : 1;
190     guint                       profile_changed         : 1;
191     guint                       quant_matrix_changed    : 1;
192     guint                       progressive_sequence    : 1;
193     guint                       closed_gop              : 1;
194     guint                       broken_link             : 1;
195 };
196
197 /* VLC decoder from gst-plugins-bad */
198 typedef struct _VLCTable VLCTable;
199 struct _VLCTable {
200     gint  value;
201     guint cword;
202     guint cbits;
203 };
204
205 static gboolean
206 decode_vlc(GstBitReader *br, gint *res, const VLCTable *table, guint length)
207 {
208     guint8 i;
209     guint cbits = 0;
210     guint32 value = 0;
211
212     for (i = 0; i < length; i++) {
213         if (cbits != table[i].cbits) {
214             cbits = table[i].cbits;
215             if (!gst_bit_reader_peek_bits_uint32(br, &value, cbits)) {
216                 goto failed;
217             }
218         }
219
220         if (value == table[i].cword) {
221             SKIP(br, cbits);
222             if (res)
223                 *res = table[i].value;
224             return TRUE;
225         }
226     }
227     GST_DEBUG("failed to find VLC code");
228
229 failed:
230     GST_WARNING("failed to decode VLC, returning");
231     return FALSE;
232 }
233
234 enum {
235     GST_MPEG_VIDEO_MACROBLOCK_ESCAPE = -1,
236 };
237
238 /* Table B-1: Variable length codes for macroblock_address_increment */
239 static const VLCTable mpeg2_mbaddr_vlc_table[] = {
240     {  1, 0x01,  1 },
241     {  2, 0x03,  3 },
242     {  3, 0x02,  3 },
243     {  4, 0x03,  4 },
244     {  5, 0x02,  4 },
245     {  6, 0x03,  5 },
246     {  7, 0x02,  5 },
247     {  8, 0x07,  7 },
248     {  9, 0x06,  7 },
249     { 10, 0x0b,  8 },
250     { 11, 0x0a,  8 },
251     { 12, 0x09,  8 },
252     { 13, 0x08,  8 },
253     { 14, 0x07,  8 },
254     { 15, 0x06,  8 },
255     { 16, 0x17, 10 },
256     { 17, 0x16, 10 },
257     { 18, 0x15, 10 },
258     { 19, 0x14, 10 },
259     { 20, 0x13, 10 },
260     { 21, 0x12, 10 },
261     { 22, 0x23, 11 },
262     { 23, 0x22, 11 },
263     { 24, 0x21, 11 },
264     { 25, 0x20, 11 },
265     { 26, 0x1f, 11 },
266     { 27, 0x1e, 11 },
267     { 28, 0x1d, 11 },
268     { 29, 0x1c, 11 },
269     { 30, 0x1b, 11 },
270     { 31, 0x1a, 11 },
271     { 32, 0x19, 11 },
272     { 33, 0x18, 11 },
273     { GST_MPEG_VIDEO_MACROBLOCK_ESCAPE, 0x08, 11 }
274 };
275
276 static void
277 gst_vaapi_decoder_mpeg2_close(GstVaapiDecoderMpeg2 *decoder)
278 {
279     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
280
281     gst_vaapi_picture_replace(&priv->current_picture, NULL);
282
283     if (priv->dpb) {
284         gst_vaapi_dpb_unref(priv->dpb);
285         priv->dpb = NULL;
286     }
287
288     if (priv->adapter) {
289         gst_adapter_clear(priv->adapter);
290         g_object_unref(priv->adapter);
291         priv->adapter = NULL;
292     }
293 }
294
295 static gboolean
296 gst_vaapi_decoder_mpeg2_open(GstVaapiDecoderMpeg2 *decoder, GstBuffer *buffer)
297 {
298     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
299
300     gst_vaapi_decoder_mpeg2_close(decoder);
301
302     priv->adapter = gst_adapter_new();
303     if (!priv->adapter)
304         return FALSE;
305
306     priv->dpb = gst_vaapi_dpb_mpeg2_new();
307     if (!priv->dpb)
308         return FALSE;
309
310     pts_init(&priv->tsg);
311     return TRUE;
312 }
313
314 static void
315 gst_vaapi_decoder_mpeg2_destroy(GstVaapiDecoderMpeg2 *decoder)
316 {
317     gst_vaapi_decoder_mpeg2_close(decoder);
318 }
319
320 static gboolean
321 gst_vaapi_decoder_mpeg2_create(GstVaapiDecoderMpeg2 *decoder)
322 {
323     if (!GST_VAAPI_DECODER_CODEC(decoder))
324         return FALSE;
325     return TRUE;
326 }
327
328 static inline void
329 copy_quant_matrix(guint8 dst[64], const guint8 src[64])
330 {
331     memcpy(dst, src, 64);
332 }
333
334 static const char *
335 get_profile_str(GstVaapiProfile profile)
336 {
337     char *str;
338
339     switch (profile) {
340     case GST_VAAPI_PROFILE_MPEG2_SIMPLE:    str = "simple";     break;
341     case GST_VAAPI_PROFILE_MPEG2_MAIN:      str = "main";       break;
342     case GST_VAAPI_PROFILE_MPEG2_HIGH:      str = "high";       break;
343     default:                                str = "<unknown>";  break;
344     }
345     return str;
346 }
347
348 static GstVaapiProfile
349 get_profile(GstVaapiDecoderMpeg2 *decoder, GstVaapiEntrypoint entrypoint)
350 {
351     GstVaapiDisplay * const va_display = GST_VAAPI_DECODER_DISPLAY(decoder);
352     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
353     GstVaapiProfile profile = priv->profile;
354
355     do {
356         /* Return immediately if the exact same profile was found */
357         if (gst_vaapi_display_has_decoder(va_display, profile, entrypoint))
358             break;
359
360         /* Otherwise, try to map to a higher profile */
361         switch (profile) {
362         case GST_VAAPI_PROFILE_MPEG2_SIMPLE:
363             profile = GST_VAAPI_PROFILE_MPEG2_MAIN;
364             break;
365         case GST_VAAPI_PROFILE_MPEG2_MAIN:
366             profile = GST_VAAPI_PROFILE_MPEG2_HIGH;
367             break;
368         case GST_VAAPI_PROFILE_MPEG2_HIGH:
369             // Try to map to main profile if no high profile specific bits used
370             if (priv->profile == profile    &&
371                 !priv->has_seq_scalable_ext &&
372                 (priv->has_seq_ext && priv->seq_ext.chroma_format == 1)) {
373                 profile = GST_VAAPI_PROFILE_MPEG2_MAIN;
374                 break;
375             }
376             // fall-through
377         default:
378             profile = GST_VAAPI_PROFILE_UNKNOWN;
379             break;
380         }
381     } while (profile != GST_VAAPI_PROFILE_UNKNOWN);
382
383     if (profile != priv->profile)
384         GST_INFO("forced %s profile to %s profile",
385                  get_profile_str(priv->profile), get_profile_str(profile));
386     return profile;
387 }
388
389 static GstVaapiDecoderStatus
390 ensure_context(GstVaapiDecoderMpeg2 *decoder)
391 {
392     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
393     GstVaapiEntrypoint entrypoint = GST_VAAPI_ENTRYPOINT_VLD;
394     gboolean reset_context = FALSE;
395
396     if (priv->profile_changed) {
397         GST_DEBUG("profile changed");
398         priv->profile_changed = FALSE;
399         reset_context         = TRUE;
400
401         priv->hw_profile = get_profile(decoder, entrypoint);
402         if (priv->hw_profile == GST_VAAPI_PROFILE_UNKNOWN)
403             return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
404     }
405
406     if (priv->size_changed) {
407         GST_DEBUG("size changed");
408         priv->size_changed = FALSE;
409         reset_context      = TRUE;
410     }
411
412     if (reset_context) {
413         GstVaapiContextInfo info;
414
415         info.profile    = priv->hw_profile;
416         info.entrypoint = entrypoint;
417         info.width      = priv->width;
418         info.height     = priv->height;
419         info.ref_frames = 2;
420         reset_context   = gst_vaapi_decoder_ensure_context(
421             GST_VAAPI_DECODER(decoder),
422             &info
423         );
424         if (!reset_context)
425             return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
426     }
427     return GST_VAAPI_DECODER_STATUS_SUCCESS;
428 }
429
430 static GstVaapiDecoderStatus
431 ensure_quant_matrix(GstVaapiDecoderMpeg2 *decoder, GstVaapiPicture *picture)
432 {
433     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
434     VAIQMatrixBufferMPEG2 *iq_matrix;
435     guint8 *intra_quant_matrix = NULL;
436     guint8 *non_intra_quant_matrix = NULL;
437     guint8 *chroma_intra_quant_matrix = NULL;
438     guint8 *chroma_non_intra_quant_matrix = NULL;
439
440     if (!priv->quant_matrix_changed)
441         return GST_VAAPI_DECODER_STATUS_SUCCESS;
442
443     priv->quant_matrix_changed = FALSE;
444
445     picture->iq_matrix = GST_VAAPI_IQ_MATRIX_NEW(MPEG2, decoder);
446     if (!picture->iq_matrix) {
447         GST_ERROR("failed to allocate IQ matrix");
448         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
449     }
450     iq_matrix = picture->iq_matrix->param;
451
452     intra_quant_matrix     = priv->seq_hdr.intra_quantizer_matrix;
453     non_intra_quant_matrix = priv->seq_hdr.non_intra_quantizer_matrix;
454     if (priv->has_quant_matrix_ext) {
455         if (priv->quant_matrix_ext.load_intra_quantiser_matrix)
456             intra_quant_matrix = priv->quant_matrix_ext.intra_quantiser_matrix;
457         if (priv->quant_matrix_ext.load_non_intra_quantiser_matrix)
458             non_intra_quant_matrix = priv->quant_matrix_ext.non_intra_quantiser_matrix;
459         if (priv->quant_matrix_ext.load_chroma_intra_quantiser_matrix)
460             chroma_intra_quant_matrix = priv->quant_matrix_ext.chroma_intra_quantiser_matrix;
461         if (priv->quant_matrix_ext.load_chroma_non_intra_quantiser_matrix)
462             chroma_non_intra_quant_matrix = priv->quant_matrix_ext.chroma_non_intra_quantiser_matrix;
463     }
464
465     iq_matrix->load_intra_quantiser_matrix = intra_quant_matrix != NULL;
466     if (intra_quant_matrix)
467         copy_quant_matrix(iq_matrix->intra_quantiser_matrix,
468                           intra_quant_matrix);
469
470     iq_matrix->load_non_intra_quantiser_matrix = non_intra_quant_matrix != NULL;
471     if (non_intra_quant_matrix)
472         copy_quant_matrix(iq_matrix->non_intra_quantiser_matrix,
473                           non_intra_quant_matrix);
474
475     iq_matrix->load_chroma_intra_quantiser_matrix = chroma_intra_quant_matrix != NULL;
476     if (chroma_intra_quant_matrix)
477         copy_quant_matrix(iq_matrix->chroma_intra_quantiser_matrix,
478                           chroma_intra_quant_matrix);
479
480     iq_matrix->load_chroma_non_intra_quantiser_matrix = chroma_non_intra_quant_matrix != NULL;
481     if (chroma_non_intra_quant_matrix)
482         copy_quant_matrix(iq_matrix->chroma_non_intra_quantiser_matrix,
483                           chroma_non_intra_quant_matrix);
484     return GST_VAAPI_DECODER_STATUS_SUCCESS;
485 }
486
487 static gboolean
488 decode_current_picture(GstVaapiDecoderMpeg2 *decoder)
489 {
490     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
491     GstVaapiPicture * const picture = priv->current_picture;
492
493     if (picture) {
494         if (!gst_vaapi_picture_decode(picture))
495             return FALSE;
496         if (GST_VAAPI_PICTURE_IS_COMPLETE(picture)) {
497             if (!gst_vaapi_dpb_add(priv->dpb, picture))
498                 return FALSE;
499             gst_vaapi_picture_replace(&priv->current_picture, NULL);
500         }
501     }
502     return TRUE;
503 }
504
505 static GstVaapiDecoderStatus
506 decode_sequence(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
507 {
508     GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER(decoder);
509     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
510     GstMpegVideoSequenceHdr * const seq_hdr = &priv->seq_hdr;
511
512     if (!gst_mpeg_video_parse_sequence_header(seq_hdr, buf, buf_size, 4)) {
513         GST_ERROR("failed to parse sequence header");
514         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
515     }
516
517     priv->fps_n = seq_hdr->fps_n;
518     priv->fps_d = seq_hdr->fps_d;
519     pts_set_framerate(&priv->tsg, priv->fps_n, priv->fps_d);
520     gst_vaapi_decoder_set_framerate(base_decoder, priv->fps_n, priv->fps_d);
521
522     if (gst_mpeg_video_finalise_mpeg2_sequence_header(seq_hdr, NULL, NULL))
523         gst_vaapi_decoder_set_pixel_aspect_ratio(base_decoder,
524             seq_hdr->par_w, seq_hdr->par_h);
525
526     priv->width                 = seq_hdr->width;
527     priv->height                = seq_hdr->height;
528     priv->has_seq_ext           = FALSE;
529     priv->size_changed          = TRUE;
530     priv->quant_matrix_changed  = TRUE;
531     priv->progressive_sequence  = TRUE;
532     return GST_VAAPI_DECODER_STATUS_SUCCESS;
533 }
534
535 static GstVaapiDecoderStatus
536 decode_sequence_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
537 {
538     GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER(decoder);
539     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
540     GstMpegVideoSequenceHdr * const seq_hdr = &priv->seq_hdr;
541     GstMpegVideoSequenceExt * const seq_ext = &priv->seq_ext;
542     GstVaapiProfile profile;
543     guint width, height;
544
545     if (!gst_mpeg_video_parse_sequence_extension(seq_ext, buf, buf_size, 4)) {
546         GST_ERROR("failed to parse sequence-extension");
547         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
548     }
549     priv->has_seq_ext = TRUE;
550     priv->progressive_sequence = seq_ext->progressive;
551     gst_vaapi_decoder_set_interlaced(base_decoder, !priv->progressive_sequence);
552
553     width  = (priv->width  & 0x0fff) | ((guint32)seq_ext->horiz_size_ext << 12);
554     height = (priv->height & 0x0fff) | ((guint32)seq_ext->vert_size_ext  << 12);
555     GST_DEBUG("video resolution %ux%u", width, height);
556
557     if (seq_ext->fps_n_ext && seq_ext->fps_d_ext) {
558         priv->fps_n *= seq_ext->fps_n_ext + 1;
559         priv->fps_d *= seq_ext->fps_d_ext + 1;
560         pts_set_framerate(&priv->tsg, priv->fps_n, priv->fps_d);
561         gst_vaapi_decoder_set_framerate(base_decoder, priv->fps_n, priv->fps_d);
562     }
563
564     if (priv->width != width) {
565         priv->width = width;
566         priv->size_changed = TRUE;
567     }
568
569     if (priv->height != height) {
570         priv->height = height;
571         priv->size_changed = TRUE;
572     }
573
574     switch (seq_ext->profile) {
575     case GST_MPEG_VIDEO_PROFILE_SIMPLE:
576         profile = GST_VAAPI_PROFILE_MPEG2_SIMPLE;
577         break;
578     case GST_MPEG_VIDEO_PROFILE_MAIN:
579         profile = GST_VAAPI_PROFILE_MPEG2_MAIN;
580         break;
581     case GST_MPEG_VIDEO_PROFILE_HIGH:
582         profile = GST_VAAPI_PROFILE_MPEG2_HIGH;
583         break;
584     default:
585         GST_ERROR("unsupported profile %d", seq_ext->profile);
586         return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
587     }
588     if (priv->profile != profile) {
589         priv->profile = profile;
590         priv->profile_changed = TRUE;
591     }
592
593     if (gst_mpeg_video_finalise_mpeg2_sequence_header(seq_hdr, seq_ext, NULL))
594         gst_vaapi_decoder_set_pixel_aspect_ratio(base_decoder,
595             seq_hdr->par_w, seq_hdr->par_h);
596
597     return GST_VAAPI_DECODER_STATUS_SUCCESS;
598 }
599
600 static GstVaapiDecoderStatus
601 decode_sequence_end(GstVaapiDecoderMpeg2 *decoder)
602 {
603     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
604
605     if (priv->current_picture && !decode_current_picture(decoder))
606         return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
607
608     gst_vaapi_dpb_flush(priv->dpb);
609     return GST_VAAPI_DECODER_STATUS_END_OF_STREAM;
610 }
611
612 static GstVaapiDecoderStatus
613 decode_quant_matrix_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
614 {
615     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
616     GstMpegVideoQuantMatrixExt * const quant_matrix_ext = &priv->quant_matrix_ext;
617
618     if (!gst_mpeg_video_parse_quant_matrix_extension(quant_matrix_ext, buf, buf_size, 4)) {
619         GST_ERROR("failed to parse quant-matrix-extension");
620         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
621     }
622     priv->has_quant_matrix_ext = TRUE;
623     priv->quant_matrix_changed = TRUE;
624     return GST_VAAPI_DECODER_STATUS_SUCCESS;
625 }
626
627 static GstVaapiDecoderStatus
628 decode_gop(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
629 {
630     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
631     GstMpegVideoGop gop;
632     GstClockTime pts;
633
634     if (!gst_mpeg_video_parse_gop(&gop, buf, buf_size, 4)) {
635         GST_ERROR("failed to parse GOP");
636         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
637     }
638
639     priv->closed_gop  = gop.closed_gop;
640     priv->broken_link = gop.broken_link;
641
642     GST_DEBUG("GOP %02u:%02u:%02u:%02u (closed_gop %d, broken_link %d)",
643               gop.hour, gop.minute, gop.second, gop.frame,
644               priv->closed_gop, priv->broken_link);
645
646     pts = gst_adapter_prev_timestamp(priv->adapter, NULL);
647     pts_sync(&priv->tsg, pts);
648     return GST_VAAPI_DECODER_STATUS_SUCCESS;
649 }
650
651 static GstVaapiDecoderStatus
652 decode_picture(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
653 {
654     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
655     GstMpegVideoPictureHdr * const pic_hdr = &priv->pic_hdr;
656     GstVaapiPicture *picture;
657     GstVaapiDecoderStatus status;
658     GstClockTime pts;
659
660     status = ensure_context(decoder);
661     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
662         GST_ERROR("failed to reset context");
663         return status;
664     }
665
666     if (priv->current_picture && !decode_current_picture(decoder))
667         return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
668
669     if (priv->current_picture) {
670         /* Re-use current picture where the first field was decoded */
671         picture = gst_vaapi_picture_new_field(priv->current_picture);
672         if (!picture) {
673             GST_ERROR("failed to allocate field picture");
674             return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
675         }
676     }
677     else {
678         /* Create new picture */
679         picture = GST_VAAPI_PICTURE_NEW(MPEG2, decoder);
680         if (!picture) {
681             GST_ERROR("failed to allocate picture");
682             return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
683         }
684     }
685     gst_vaapi_picture_replace(&priv->current_picture, picture);
686     gst_vaapi_picture_unref(picture);
687
688     status = ensure_quant_matrix(decoder, picture);
689     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
690         GST_ERROR("failed to reset quantizer matrix");
691         return status;
692     }
693
694     if (!gst_mpeg_video_parse_picture_header(pic_hdr, buf, buf_size, 4)) {
695         GST_ERROR("failed to parse picture header");
696         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
697     }
698     priv->has_pic_ext = FALSE;
699
700     switch (pic_hdr->pic_type) {
701     case GST_MPEG_VIDEO_PICTURE_TYPE_I:
702         GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_REFERENCE);
703         picture->type = GST_VAAPI_PICTURE_TYPE_I;
704         break;
705     case GST_MPEG_VIDEO_PICTURE_TYPE_P:
706         GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_REFERENCE);
707         picture->type = GST_VAAPI_PICTURE_TYPE_P;
708         break;
709     case GST_MPEG_VIDEO_PICTURE_TYPE_B:
710         picture->type = GST_VAAPI_PICTURE_TYPE_B;
711         break;
712     default:
713         GST_ERROR("unsupported picture type %d", pic_hdr->pic_type);
714         return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
715     }
716
717     /* Update presentation time */
718     pts = gst_adapter_prev_timestamp(priv->adapter, NULL);
719     picture->pts = pts_eval(&priv->tsg, pts, pic_hdr->tsn);
720     picture->poc = pts_get_poc(&priv->tsg);
721     return status;
722 }
723
724 static GstVaapiDecoderStatus
725 decode_picture_ext(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
726 {
727     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
728     GstMpegVideoPictureExt * const pic_ext = &priv->pic_ext;
729     GstVaapiPicture * const picture = priv->current_picture;
730
731     if (!gst_mpeg_video_parse_picture_extension(pic_ext, buf, buf_size, 4)) {
732         GST_ERROR("failed to parse picture-extension");
733         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
734     }
735     priv->has_pic_ext = TRUE;
736
737     if (priv->progressive_sequence && !pic_ext->progressive_frame) {
738         GST_WARNING("invalid interlaced frame in progressive sequence, fixing");
739         pic_ext->progressive_frame = 1;
740     }
741
742     if (pic_ext->picture_structure == 0 ||
743         (pic_ext->progressive_frame &&
744          pic_ext->picture_structure != GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME)) {
745         GST_WARNING("invalid picture_structure %d, replacing with \"frame\"",
746                     pic_ext->picture_structure);
747         pic_ext->picture_structure = GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME;
748     }
749
750     if (!priv->progressive_sequence && !pic_ext->progressive_frame) {
751         GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_INTERLACED);
752         if (pic_ext->top_field_first)
753             GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_TFF);
754     }
755
756     switch (pic_ext->picture_structure) {
757     case GST_MPEG_VIDEO_PICTURE_STRUCTURE_TOP_FIELD:
758         picture->structure = GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD;
759         break;
760     case GST_MPEG_VIDEO_PICTURE_STRUCTURE_BOTTOM_FIELD:
761         picture->structure = GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD;
762         break;
763     case GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME:
764         picture->structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
765         break;
766     }
767
768     /* Allocate dummy picture for first field based I-frame */
769     if (picture->type == GST_VAAPI_PICTURE_TYPE_I &&
770         !GST_VAAPI_PICTURE_IS_FRAME(picture) &&
771         gst_vaapi_dpb_size(priv->dpb) == 0) {
772         GstVaapiPicture *dummy_picture;
773         gboolean success;
774
775         dummy_picture = GST_VAAPI_PICTURE_NEW(MPEG2, decoder);
776         if (!dummy_picture) {
777             GST_ERROR("failed to allocate dummy picture");
778             return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
779         }
780
781         dummy_picture->type      = GST_VAAPI_PICTURE_TYPE_I;
782         dummy_picture->pts       = GST_CLOCK_TIME_NONE;
783         dummy_picture->poc       = -1;
784         dummy_picture->structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
785
786         GST_VAAPI_PICTURE_FLAG_SET(
787             dummy_picture,
788             (GST_VAAPI_PICTURE_FLAG_SKIPPED |
789              GST_VAAPI_PICTURE_FLAG_REFERENCE)
790         );
791
792         success = gst_vaapi_dpb_add(priv->dpb, dummy_picture);
793         gst_vaapi_picture_unref(dummy_picture);
794         if (!success) {
795             GST_ERROR("failed to add dummy picture into DPB");
796             return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
797         }
798         GST_INFO("allocated dummy picture for first field based I-frame");
799     }
800     return GST_VAAPI_DECODER_STATUS_SUCCESS;
801 }
802
803 static inline guint32
804 pack_f_code(guint8 f_code[2][2])
805 {
806     return (((guint32)f_code[0][0] << 12) |
807             ((guint32)f_code[0][1] <<  8) |
808             ((guint32)f_code[1][0] <<  4) |
809             (         f_code[1][1]      ));
810 }
811
812 static gboolean
813 fill_picture(GstVaapiDecoderMpeg2 *decoder, GstVaapiPicture *picture)
814 {
815     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
816     VAPictureParameterBufferMPEG2 * const pic_param = picture->param;
817     GstMpegVideoPictureHdr * const pic_hdr = &priv->pic_hdr;
818     GstMpegVideoPictureExt * const pic_ext = &priv->pic_ext;
819     GstVaapiPicture *prev_picture, *next_picture;
820
821     if (!priv->has_pic_ext)
822         return FALSE;
823
824     /* Fill in VAPictureParameterBufferMPEG2 */
825     pic_param->horizontal_size                                          = priv->width;
826     pic_param->vertical_size                                            = priv->height;
827     pic_param->forward_reference_picture                                = VA_INVALID_ID;
828     pic_param->backward_reference_picture                               = VA_INVALID_ID;
829     pic_param->picture_coding_type                                      = pic_hdr->pic_type;
830     pic_param->f_code                                                   = pack_f_code(pic_ext->f_code);
831
832 #define COPY_FIELD(a, b, f) \
833     pic_param->a.b.f = pic_ext->f
834     pic_param->picture_coding_extension.value                           = 0;
835     pic_param->picture_coding_extension.bits.is_first_field             = GST_VAAPI_PICTURE_IS_FIRST_FIELD(picture);
836     COPY_FIELD(picture_coding_extension, bits, intra_dc_precision);
837     COPY_FIELD(picture_coding_extension, bits, picture_structure);
838     COPY_FIELD(picture_coding_extension, bits, top_field_first);
839     COPY_FIELD(picture_coding_extension, bits, frame_pred_frame_dct);
840     COPY_FIELD(picture_coding_extension, bits, concealment_motion_vectors);
841     COPY_FIELD(picture_coding_extension, bits, q_scale_type);
842     COPY_FIELD(picture_coding_extension, bits, intra_vlc_format);
843     COPY_FIELD(picture_coding_extension, bits, alternate_scan);
844     COPY_FIELD(picture_coding_extension, bits, repeat_first_field);
845     COPY_FIELD(picture_coding_extension, bits, progressive_frame);
846
847     gst_vaapi_dpb_mpeg2_get_references(
848         priv->dpb,
849         picture,
850         &prev_picture,
851         &next_picture
852     );
853
854     switch (pic_hdr->pic_type) {
855     case GST_MPEG_VIDEO_PICTURE_TYPE_B:
856         if (next_picture)
857             pic_param->backward_reference_picture = next_picture->surface_id;
858         if (prev_picture)
859             pic_param->forward_reference_picture = prev_picture->surface_id;
860         else if (!priv->closed_gop)
861             GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_SKIPPED);
862         break;
863     case GST_MPEG_VIDEO_PICTURE_TYPE_P:
864         if (prev_picture)
865             pic_param->forward_reference_picture = prev_picture->surface_id;
866         break;
867     }
868     return TRUE;
869 }
870
871 static GstVaapiDecoderStatus
872 decode_slice(
873     GstVaapiDecoderMpeg2 *decoder,
874     int                   slice_no,
875     guchar               *buf,
876     guint                 buf_size
877 )
878 {
879     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
880     GstVaapiPicture * const picture = priv->current_picture;
881     GstVaapiSlice *slice;
882     VASliceParameterBufferMPEG2 *slice_param;
883     GstBitReader br;
884     gint mb_x, mb_y, mb_inc;
885     guint macroblock_offset;
886     guint8 slice_vertical_position_extension;
887     guint8 quantiser_scale_code;
888     guint8 intra_slice = 0;
889     guint8 extra_bit_slice, junk8;
890
891     GST_DEBUG("slice %d @ %p, %u bytes)", slice_no, buf, buf_size);
892
893     if (picture->slices->len == 0 && !fill_picture(decoder, picture))
894         return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
895
896     slice = GST_VAAPI_SLICE_NEW(MPEG2, decoder, buf, buf_size);
897     if (!slice) {
898         GST_ERROR("failed to allocate slice");
899         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
900     }
901     gst_vaapi_picture_add_slice(picture, slice);
902
903     /* Parse slice */
904     gst_bit_reader_init(&br, buf, buf_size);
905     SKIP(&br, 32); /* slice_start_code */
906     if (priv->height > 2800)
907         READ_UINT8(&br, slice_vertical_position_extension, 3);
908     if (priv->has_seq_scalable_ext) {
909         GST_ERROR("failed to parse slice %d. Unsupported sequence_scalable_extension()", slice_no);
910         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
911     }
912     READ_UINT8(&br, quantiser_scale_code, 5);
913     READ_UINT8(&br, extra_bit_slice, 1);
914     if (extra_bit_slice == 1) {
915         READ_UINT8(&br, intra_slice, 1);
916         READ_UINT8(&br, junk8, 7);
917         READ_UINT8(&br, extra_bit_slice, 1);
918         while (extra_bit_slice == 1) {
919             READ_UINT8(&br, junk8, 8);
920             READ_UINT8(&br, extra_bit_slice, 1);
921         }
922     }
923     macroblock_offset = gst_bit_reader_get_pos(&br);
924
925     mb_y = slice_no;
926     mb_x = -1;
927     do {
928         if (!decode_vlc(&br, &mb_inc, mpeg2_mbaddr_vlc_table,
929                         G_N_ELEMENTS(mpeg2_mbaddr_vlc_table))) {
930             GST_WARNING("failed to decode first macroblock_address_increment");
931             goto failed;
932         }
933         mb_x += mb_inc == GST_MPEG_VIDEO_MACROBLOCK_ESCAPE ? 33 : mb_inc;
934     } while (mb_inc == GST_MPEG_VIDEO_MACROBLOCK_ESCAPE);
935
936     /* Fill in VASliceParameterBufferMPEG2 */
937     slice_param                            = slice->param;
938     slice_param->macroblock_offset         = macroblock_offset;
939     slice_param->slice_horizontal_position = mb_x;
940     slice_param->slice_vertical_position   = mb_y;
941     slice_param->quantiser_scale_code      = quantiser_scale_code;
942     slice_param->intra_slice_flag          = intra_slice;
943     return GST_VAAPI_DECODER_STATUS_SUCCESS;
944
945 failed:
946     return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
947 }
948
949 static inline gint
950 scan_for_start_code(GstAdapter *adapter, guint ofs, guint size, guint32 *scp)
951 {
952     return (gint)gst_adapter_masked_scan_uint32_peek(adapter,
953                                                      0xffffff00, 0x00000100,
954                                                      ofs, size,
955                                                      scp);
956 }
957
958 static GstVaapiDecoderStatus
959 decode_packet(GstVaapiDecoderMpeg2 *decoder, guchar *buf, guint buf_size)
960 {
961     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
962     GstVaapiDecoderStatus status;
963     guchar type;
964
965     /* The packet defined by buf and buf_size contains the start code */
966     type = buf[3];
967     switch (type) {
968     case GST_MPEG_VIDEO_PACKET_PICTURE:
969         if (!priv->width || !priv->height)
970             goto unknown_picture_size;
971         status = decode_picture(decoder, buf, buf_size);
972         break;
973     case GST_MPEG_VIDEO_PACKET_SEQUENCE:
974         status = decode_sequence(decoder, buf, buf_size);
975         break;
976     case GST_MPEG_VIDEO_PACKET_EXTENSION: {
977         const guchar id = buf[4] >> 4;
978         switch (id) {
979         case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE:
980             status = decode_sequence_ext(decoder, buf, buf_size);
981             break;
982         case GST_MPEG_VIDEO_PACKET_EXT_QUANT_MATRIX:
983             status = decode_quant_matrix_ext(decoder, buf, buf_size);
984             break;
985         case GST_MPEG_VIDEO_PACKET_EXT_PICTURE:
986             if (!priv->width || !priv->height)
987                 goto unknown_picture_size;
988             status = decode_picture_ext(decoder, buf, buf_size);
989             break;
990         default:
991             // Ignore unknown start-code extensions
992             GST_WARNING("unsupported start code extension (0x%02x)", id);
993             status = GST_VAAPI_DECODER_STATUS_SUCCESS;
994             break;
995         }
996         break;
997     }
998     case GST_MPEG_VIDEO_PACKET_SEQUENCE_END:
999         status = decode_sequence_end(decoder);
1000         break;
1001     case GST_MPEG_VIDEO_PACKET_GOP:
1002         status = decode_gop(decoder, buf, buf_size);
1003         break;
1004     case GST_MPEG_VIDEO_PACKET_USER_DATA:
1005         // Ignore user-data packets
1006         status = GST_VAAPI_DECODER_STATUS_SUCCESS;
1007         break;
1008     default:
1009         if (type >= GST_MPEG_VIDEO_PACKET_SLICE_MIN &&
1010             type <= GST_MPEG_VIDEO_PACKET_SLICE_MAX) {
1011             if (!priv->current_picture)
1012                 goto undefined_picture;
1013             status = decode_slice(
1014                 decoder,
1015                 type - GST_MPEG_VIDEO_PACKET_SLICE_MIN,
1016                 buf, buf_size
1017             );
1018             break;
1019         }
1020         else if (type >= 0xb9 && type <= 0xff) {
1021             // Ignore system start codes (PES headers)
1022             status = GST_VAAPI_DECODER_STATUS_SUCCESS;
1023             break;
1024         }
1025         GST_WARNING("unsupported start code (0x%02x)", type);
1026         status = GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
1027         break;
1028     }
1029     return status;
1030
1031 unknown_picture_size:
1032     // Ignore packet while picture size is undefined
1033     // i.e. missing sequence headers, or not parsed correctly
1034     GST_WARNING("failed to parse picture of unknown size");
1035     return GST_VAAPI_DECODER_STATUS_SUCCESS;
1036
1037 undefined_picture:
1038     // Ignore packet while picture is undefined
1039     // i.e. missing picture headers, or not parsed correctly
1040     GST_WARNING("failed to parse slice with undefined picture");
1041     return GST_VAAPI_DECODER_STATUS_SUCCESS;
1042 }
1043
1044 static GstVaapiDecoderStatus
1045 decode_buffer(GstVaapiDecoderMpeg2 *decoder, GstBuffer *buffer)
1046 {
1047     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
1048     GstVaapiDecoderStatus status;
1049     gboolean is_eos;
1050     guchar *buf;
1051     guint buf_size, size;
1052     guint32 start_code;
1053     gint ofs;
1054
1055     buf      = GST_BUFFER_DATA(buffer);
1056     buf_size = GST_BUFFER_SIZE(buffer);
1057     is_eos   = GST_BUFFER_IS_EOS(buffer);
1058     if (buf && buf_size > 0)
1059         gst_adapter_push(priv->adapter, gst_buffer_ref(buffer));
1060
1061     size = gst_adapter_available(priv->adapter);
1062     do {
1063         if (size == 0) {
1064             status = GST_VAAPI_DECODER_STATUS_SUCCESS;
1065             break;
1066         }
1067
1068         status = GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
1069         if (size < 4)
1070             break;
1071         ofs = scan_for_start_code(priv->adapter, 0, size, &start_code);
1072         if (ofs < 0)
1073             break;
1074         gst_adapter_flush(priv->adapter, ofs);
1075         size -= ofs;
1076
1077         status = gst_vaapi_decoder_check_status(GST_VAAPI_DECODER(decoder));
1078         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
1079             break;
1080
1081         status = GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
1082         if (size < 8)
1083             break;
1084         ofs = scan_for_start_code(priv->adapter, 4, size - 4, NULL);
1085         if (ofs < 0) {
1086             // Assume the whole packet is present if end-of-stream
1087             if (!is_eos)
1088                 break;
1089             ofs = size;
1090         }
1091         buffer = gst_adapter_take_buffer(priv->adapter, ofs);
1092         size -= ofs;
1093
1094         buf      = GST_BUFFER_DATA(buffer);
1095         buf_size = GST_BUFFER_SIZE(buffer);
1096         status   = decode_packet(decoder, buf, buf_size);
1097
1098         gst_buffer_unref(buffer);
1099     } while (status == GST_VAAPI_DECODER_STATUS_SUCCESS);
1100
1101     if (is_eos && (status == GST_VAAPI_DECODER_STATUS_SUCCESS ||
1102                    status == GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA))
1103         status = decode_sequence_end(decoder);
1104     return status;
1105 }
1106
1107 GstVaapiDecoderStatus
1108 gst_vaapi_decoder_mpeg2_decode(GstVaapiDecoder *base, GstBuffer *buffer)
1109 {
1110     GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(base);
1111     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
1112
1113     g_return_val_if_fail(priv->is_constructed,
1114                          GST_VAAPI_DECODER_STATUS_ERROR_INIT_FAILED);
1115
1116     if (!priv->is_opened) {
1117         priv->is_opened = gst_vaapi_decoder_mpeg2_open(decoder, buffer);
1118         if (!priv->is_opened)
1119             return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC;
1120     }
1121     return decode_buffer(decoder, buffer);
1122 }
1123
1124 static void
1125 gst_vaapi_decoder_mpeg2_finalize(GObject *object)
1126 {
1127     GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(object);
1128
1129     gst_vaapi_decoder_mpeg2_destroy(decoder);
1130
1131     G_OBJECT_CLASS(gst_vaapi_decoder_mpeg2_parent_class)->finalize(object);
1132 }
1133
1134 static void
1135 gst_vaapi_decoder_mpeg2_constructed(GObject *object)
1136 {
1137     GstVaapiDecoderMpeg2 * const decoder = GST_VAAPI_DECODER_MPEG2(object);
1138     GstVaapiDecoderMpeg2Private * const priv = decoder->priv;
1139     GObjectClass *parent_class;
1140
1141     parent_class = G_OBJECT_CLASS(gst_vaapi_decoder_mpeg2_parent_class);
1142     if (parent_class->constructed)
1143         parent_class->constructed(object);
1144
1145     priv->is_constructed = gst_vaapi_decoder_mpeg2_create(decoder);
1146 }
1147
1148 static void
1149 gst_vaapi_decoder_mpeg2_class_init(GstVaapiDecoderMpeg2Class *klass)
1150 {
1151     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1152     GstVaapiDecoderClass * const decoder_class = GST_VAAPI_DECODER_CLASS(klass);
1153
1154     g_type_class_add_private(klass, sizeof(GstVaapiDecoderMpeg2Private));
1155
1156     object_class->finalize      = gst_vaapi_decoder_mpeg2_finalize;
1157     object_class->constructed   = gst_vaapi_decoder_mpeg2_constructed;
1158
1159     decoder_class->decode       = gst_vaapi_decoder_mpeg2_decode;
1160 }
1161
1162 static void
1163 gst_vaapi_decoder_mpeg2_init(GstVaapiDecoderMpeg2 *decoder)
1164 {
1165     GstVaapiDecoderMpeg2Private *priv;
1166
1167     priv                        = GST_VAAPI_DECODER_MPEG2_GET_PRIVATE(decoder);
1168     decoder->priv               = priv;
1169     priv->width                 = 0;
1170     priv->height                = 0;
1171     priv->fps_n                 = 0;
1172     priv->fps_d                 = 0;
1173     priv->hw_profile            = GST_VAAPI_PROFILE_UNKNOWN;
1174     priv->profile               = GST_VAAPI_PROFILE_MPEG2_SIMPLE;
1175     priv->current_picture       = NULL;
1176     priv->adapter               = NULL;
1177     priv->is_constructed        = FALSE;
1178     priv->is_opened             = FALSE;
1179     priv->has_seq_ext           = FALSE;
1180     priv->has_seq_scalable_ext  = FALSE;
1181     priv->has_pic_ext           = FALSE;
1182     priv->has_quant_matrix_ext  = FALSE;
1183     priv->size_changed          = FALSE;
1184     priv->profile_changed       = TRUE; /* Allow fallbacks to work */
1185     priv->quant_matrix_changed  = FALSE;
1186     priv->progressive_sequence  = FALSE;
1187     priv->closed_gop            = FALSE;
1188     priv->broken_link           = FALSE;
1189 }
1190
1191 /**
1192  * gst_vaapi_decoder_mpeg2_new:
1193  * @display: a #GstVaapiDisplay
1194  * @caps: a #GstCaps holding codec information
1195  *
1196  * Creates a new #GstVaapiDecoder for MPEG-2 decoding.  The @caps can
1197  * hold extra information like codec-data and pictured coded size.
1198  *
1199  * Return value: the newly allocated #GstVaapiDecoder object
1200  */
1201 GstVaapiDecoder *
1202 gst_vaapi_decoder_mpeg2_new(GstVaapiDisplay *display, GstCaps *caps)
1203 {
1204     GstVaapiDecoderMpeg2 *decoder;
1205
1206     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1207     g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
1208
1209     decoder = g_object_new(
1210         GST_VAAPI_TYPE_DECODER_MPEG2,
1211         "display",      display,
1212         "caps",         caps,
1213         NULL
1214     );
1215     if (!decoder->priv->is_constructed) {
1216         g_object_unref(decoder);
1217         return NULL;
1218     }
1219     return GST_VAAPI_DECODER_CAST(decoder);
1220 }