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