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