2 * gstvaapidecoder_h264.c - H.264 decoder
4 * Copyright (C) 2011-2012 Intel Corporation
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.
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.
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
23 * SECTION:gstvaapidecoder_h264
24 * @short_description: H.264 decoder
30 #include <gst/base/gstadapter.h>
31 #include <gst/codecparsers/gsth264parser.h>
32 #include "gstvaapidecoder_h264.h"
33 #include "gstvaapidecoder_objects.h"
34 #include "gstvaapidecoder_priv.h"
35 #include "gstvaapidisplay_priv.h"
36 #include "gstvaapiobject_priv.h"
39 #include "gstvaapidebug.h"
41 /* Defined to 1 if strict ordering of DPB is needed. Only useful for debug */
42 #define USE_STRICT_DPB_ORDERING 0
44 typedef struct _GstVaapiFrameStore GstVaapiFrameStore;
45 typedef struct _GstVaapiFrameStoreClass GstVaapiFrameStoreClass;
46 typedef struct _GstVaapiPictureH264 GstVaapiPictureH264;
47 typedef struct _GstVaapiPictureH264Class GstVaapiPictureH264Class;
48 typedef struct _GstVaapiSliceH264 GstVaapiSliceH264;
49 typedef struct _GstVaapiSliceH264Class GstVaapiSliceH264Class;
51 // Used for field_poc[]
53 #define BOTTOM_FIELD 1
55 /* ------------------------------------------------------------------------- */
56 /* --- H.264 Pictures --- */
57 /* ------------------------------------------------------------------------- */
59 #define GST_VAAPI_TYPE_PICTURE_H264 \
60 (gst_vaapi_picture_h264_get_type())
62 #define GST_VAAPI_PICTURE_H264_CAST(obj) \
63 ((GstVaapiPictureH264 *)(obj))
65 #define GST_VAAPI_PICTURE_H264(obj) \
66 (G_TYPE_CHECK_INSTANCE_CAST((obj), \
67 GST_VAAPI_TYPE_PICTURE_H264, \
70 #define GST_VAAPI_PICTURE_H264_CLASS(klass) \
71 (G_TYPE_CHECK_CLASS_CAST((klass), \
72 GST_VAAPI_TYPE_PICTURE_H264, \
73 GstVaapiPictureH264Class))
75 #define GST_VAAPI_IS_PICTURE_H264(obj) \
76 (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_VAAPI_TYPE_PICTURE_H264))
78 #define GST_VAAPI_IS_PICTURE_H264_CLASS(klass) \
79 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_PICTURE_H264))
81 #define GST_VAAPI_PICTURE_H264_GET_CLASS(obj) \
82 (G_TYPE_INSTANCE_GET_CLASS((obj), \
83 GST_VAAPI_TYPE_PICTURE_H264, \
84 GstVaapiPictureH264Class))
87 * Extended picture flags:
89 * @GST_VAAPI_PICTURE_FLAG_IDR: flag that specifies an IDR picture
90 * @GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE: flag that specifies
91 * "used for short-term reference"
92 * @GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE: flag that specifies
93 * "used for long-term reference"
94 * @GST_VAAPI_PICTURE_FLAGS_REFERENCE: mask covering any kind of
95 * reference picture (short-term reference or long-term reference)
98 GST_VAAPI_PICTURE_FLAG_IDR = (GST_VAAPI_PICTURE_FLAG_LAST << 0),
100 GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE = (
101 GST_VAAPI_PICTURE_FLAG_REFERENCE),
102 GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE = (
103 GST_VAAPI_PICTURE_FLAG_REFERENCE | (GST_VAAPI_PICTURE_FLAG_LAST << 1)),
104 GST_VAAPI_PICTURE_FLAGS_REFERENCE = (
105 GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE |
106 GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE),
109 #define GST_VAAPI_PICTURE_IS_IDR(picture) \
110 (GST_VAAPI_PICTURE_FLAG_IS_SET(picture, GST_VAAPI_PICTURE_FLAG_IDR))
112 #define GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(picture) \
113 ((GST_VAAPI_PICTURE_FLAGS(picture) & \
114 GST_VAAPI_PICTURE_FLAGS_REFERENCE) == \
115 GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE)
117 #define GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(picture) \
118 ((GST_VAAPI_PICTURE_FLAGS(picture) & \
119 GST_VAAPI_PICTURE_FLAGS_REFERENCE) == \
120 GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE)
122 struct _GstVaapiPictureH264 {
123 GstVaapiPicture base;
127 gint32 frame_num; // Original frame_num from slice_header()
128 gint32 frame_num_wrap; // Temporary for ref pic marking: FrameNumWrap
129 gint32 long_term_frame_idx; // Temporary for ref pic marking: LongTermFrameIdx
130 gint32 pic_num; // Temporary for ref pic marking: PicNum
131 gint32 long_term_pic_num; // Temporary for ref pic marking: LongTermPicNum
132 GstVaapiPictureH264 *other_field; // Temporary for ref pic marking: other field in the same frame store
133 guint output_flag : 1;
134 guint output_needed : 1;
137 struct _GstVaapiPictureH264Class {
139 GstVaapiPictureClass parent_class;
142 GST_VAAPI_CODEC_DEFINE_TYPE(GstVaapiPictureH264,
143 gst_vaapi_picture_h264,
144 GST_VAAPI_TYPE_PICTURE)
147 gst_vaapi_picture_h264_destroy(GstVaapiPictureH264 *decoder)
152 gst_vaapi_picture_h264_create(
153 GstVaapiPictureH264 *picture,
154 const GstVaapiCodecObjectConstructorArgs *args
161 gst_vaapi_picture_h264_init(GstVaapiPictureH264 *picture)
163 picture->field_poc[0] = G_MAXINT32;
164 picture->field_poc[1] = G_MAXINT32;
165 picture->output_needed = FALSE;
168 static inline GstVaapiPictureH264 *
169 gst_vaapi_picture_h264_new(GstVaapiDecoderH264 *decoder)
171 GstVaapiCodecObject *object;
173 g_return_val_if_fail(GST_VAAPI_IS_DECODER(decoder), NULL);
175 object = gst_vaapi_codec_object_new(
176 GST_VAAPI_TYPE_PICTURE_H264,
177 GST_VAAPI_CODEC_BASE(decoder),
178 NULL, sizeof(VAPictureParameterBufferH264),
183 return GST_VAAPI_PICTURE_H264_CAST(object);
187 gst_vaapi_picture_h264_set_reference(
188 GstVaapiPictureH264 *picture,
189 guint reference_flags,
193 g_return_if_fail(GST_VAAPI_IS_PICTURE_H264(picture));
195 GST_VAAPI_PICTURE_FLAG_UNSET(picture, GST_VAAPI_PICTURE_FLAGS_REFERENCE);
196 GST_VAAPI_PICTURE_FLAG_SET(picture, reference_flags);
198 if (!other_field || !(picture = picture->other_field))
200 GST_VAAPI_PICTURE_FLAG_UNSET(picture, GST_VAAPI_PICTURE_FLAGS_REFERENCE);
201 GST_VAAPI_PICTURE_FLAG_SET(picture, reference_flags);
204 static inline GstVaapiPictureH264 *
205 gst_vaapi_picture_h264_new_field(GstVaapiPictureH264 *picture)
207 GstVaapiPicture *base_picture;
209 g_return_val_if_fail(GST_VAAPI_IS_PICTURE_H264(picture), NULL);
211 base_picture = gst_vaapi_picture_new_field(&picture->base);
214 return GST_VAAPI_PICTURE_H264_CAST(base_picture);
217 static inline GstVaapiSliceH264 *
218 gst_vaapi_picture_h264_get_last_slice(GstVaapiPictureH264 *picture)
220 g_return_val_if_fail(GST_VAAPI_IS_PICTURE_H264(picture), NULL);
222 if (G_UNLIKELY(picture->base.slices->len < 1))
224 return g_ptr_array_index(picture->base.slices,
225 picture->base.slices->len - 1);
228 /* ------------------------------------------------------------------------- */
230 /* ------------------------------------------------------------------------- */
232 #define GST_VAAPI_TYPE_SLICE_H264 \
233 (gst_vaapi_slice_h264_get_type())
235 #define GST_VAAPI_SLICE_H264_CAST(obj) \
236 ((GstVaapiSliceH264 *)(obj))
238 #define GST_VAAPI_SLICE_H264(obj) \
239 (G_TYPE_CHECK_INSTANCE_CAST((obj), \
240 GST_VAAPI_TYPE_SLICE_H264, \
243 #define GST_VAAPI_SLICE_H264_CLASS(klass) \
244 (G_TYPE_CHECK_CLASS_CAST((klass), \
245 GST_VAAPI_TYPE_SLICE_H264, \
246 GstVaapiSliceH264Class))
248 #define GST_VAAPI_IS_SLICE_H264(obj) \
249 (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_VAAPI_TYPE_SLICE_H264))
251 #define GST_VAAPI_IS_SLICE_H264_CLASS(klass) \
252 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_SLICE_H264))
254 #define GST_VAAPI_SLICE_H264_GET_CLASS(obj) \
255 (G_TYPE_INSTANCE_GET_CLASS((obj), \
256 GST_VAAPI_TYPE_SLICE_H264, \
257 GstVaapiSliceH264Class))
259 struct _GstVaapiSliceH264 {
261 GstH264SliceHdr slice_hdr; // parsed slice_header()
264 struct _GstVaapiSliceH264Class {
266 GstVaapiSliceClass parent_class;
269 GST_VAAPI_CODEC_DEFINE_TYPE(GstVaapiSliceH264,
270 gst_vaapi_slice_h264,
271 GST_VAAPI_TYPE_SLICE)
274 gst_vaapi_slice_h264_destroy(GstVaapiSliceH264 *slice)
279 gst_vaapi_slice_h264_create(
280 GstVaapiSliceH264 *slice,
281 const GstVaapiCodecObjectConstructorArgs *args
288 gst_vaapi_slice_h264_init(GstVaapiSliceH264 *slice)
292 static inline GstVaapiSliceH264 *
293 gst_vaapi_slice_h264_new(
294 GstVaapiDecoderH264 *decoder,
299 GstVaapiCodecObject *object;
301 g_return_val_if_fail(GST_VAAPI_IS_DECODER(decoder), NULL);
303 object = gst_vaapi_codec_object_new(
304 GST_VAAPI_TYPE_SLICE_H264,
305 GST_VAAPI_CODEC_BASE(decoder),
306 NULL, sizeof(VASliceParameterBufferH264),
311 return GST_VAAPI_SLICE_H264_CAST(object);
314 /* ------------------------------------------------------------------------- */
315 /* --- Frame Buffers (DPB) --- */
316 /* ------------------------------------------------------------------------- */
318 #define GST_VAAPI_TYPE_FRAME_STORE \
319 (gst_vaapi_frame_store_get_type())
321 #define GST_VAAPI_FRAME_STORE_CAST(obj) \
322 ((GstVaapiFrameStore *)(obj))
324 #define GST_VAAPI_FRAME_STORE(obj) \
325 (G_TYPE_CHECK_INSTANCE_CAST((obj), \
326 GST_VAAPI_TYPE_FRAME_STORE, \
329 #define GST_VAAPI_FRAME_STORE_CLASS(klass) \
330 (G_TYPE_CHECK_CLASS_CAST((klass), \
331 GST_VAAPI_TYPE_FRAME_STORE, \
332 GstVaapiFrameStoreClass))
334 #define GST_VAAPI_IS_FRAME_STORE(obj) \
335 (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_VAAPI_TYPE_FRAME_STORE))
337 #define GST_VAAPI_IS_FRAME_STORE_CLASS(klass) \
338 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_FRAME_STORE))
340 #define GST_VAAPI_FRAME_STORE_GET_CLASS(obj) \
341 (G_TYPE_INSTANCE_GET_CLASS((obj), \
342 GST_VAAPI_TYPE_FRAME_STORE, \
343 GstVaapiFrameStoreClass))
345 struct _GstVaapiFrameStore {
347 GstMiniObject parent_instance;
350 GstVaapiPictureH264 *buffers[2];
355 struct _GstVaapiFrameStoreClass {
357 GstMiniObjectClass parent_class;
360 G_DEFINE_TYPE(GstVaapiFrameStore, gst_vaapi_frame_store, GST_TYPE_MINI_OBJECT)
363 gst_vaapi_frame_store_finalize(GstMiniObject *object)
365 GstVaapiFrameStore * const fs = GST_VAAPI_FRAME_STORE_CAST(object);
366 GstMiniObjectClass *parent_class;
369 for (i = 0; i < fs->num_buffers; i++)
370 gst_vaapi_picture_replace(&fs->buffers[i], NULL);
372 parent_class = GST_MINI_OBJECT_CLASS(gst_vaapi_frame_store_parent_class);
373 if (parent_class->finalize)
374 parent_class->finalize(object);
378 gst_vaapi_frame_store_init(GstVaapiFrameStore *fs)
383 gst_vaapi_frame_store_class_init(GstVaapiFrameStoreClass *klass)
385 GstMiniObjectClass * const object_class = GST_MINI_OBJECT_CLASS(klass);
387 object_class->finalize = gst_vaapi_frame_store_finalize;
390 static inline gpointer
391 _gst_vaapi_frame_store_new(void)
393 return gst_mini_object_new(GST_VAAPI_TYPE_FRAME_STORE);
396 static GstVaapiFrameStore *
397 gst_vaapi_frame_store_new(GstVaapiPictureH264 *picture)
399 GstVaapiFrameStore *fs;
401 g_return_val_if_fail(GST_VAAPI_IS_PICTURE_H264(picture), NULL);
403 fs = _gst_vaapi_frame_store_new();
407 fs->structure = picture->structure;
408 fs->buffers[0] = gst_vaapi_picture_ref(picture);
410 fs->output_needed = picture->output_needed;
415 gst_vaapi_frame_store_add(GstVaapiFrameStore *fs, GstVaapiPictureH264 *picture)
419 g_return_val_if_fail(GST_VAAPI_IS_FRAME_STORE(fs), FALSE);
420 g_return_val_if_fail(fs->num_buffers == 1, FALSE);
421 g_return_val_if_fail(GST_VAAPI_IS_PICTURE_H264(picture), FALSE);
422 g_return_val_if_fail(!GST_VAAPI_PICTURE_IS_FRAME(picture), FALSE);
424 gst_vaapi_picture_replace(&fs->buffers[fs->num_buffers++], picture);
425 if (picture->output_flag) {
426 picture->output_needed = TRUE;
430 fs->structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
432 field = picture->structure == GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD ?
433 TOP_FIELD : BOTTOM_FIELD;
434 g_return_val_if_fail(fs->buffers[0]->field_poc[field] == G_MAXINT32, FALSE);
435 fs->buffers[0]->field_poc[field] = picture->field_poc[field];
436 g_return_val_if_fail(picture->field_poc[!field] == G_MAXINT32, FALSE);
437 picture->field_poc[!field] = fs->buffers[0]->field_poc[!field];
442 gst_vaapi_frame_store_split_fields(GstVaapiFrameStore *fs)
444 GstVaapiPictureH264 * const first_field = fs->buffers[0];
445 GstVaapiPictureH264 *second_field;
447 g_return_val_if_fail(fs->num_buffers == 1, FALSE);
449 first_field->base.structure = GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD;
450 GST_VAAPI_PICTURE_FLAG_SET(first_field, GST_VAAPI_PICTURE_FLAG_INTERLACED);
452 second_field = gst_vaapi_picture_h264_new_field(first_field);
455 gst_vaapi_picture_replace(&fs->buffers[fs->num_buffers++], second_field);
456 gst_vaapi_picture_unref(second_field);
458 second_field->frame_num = first_field->frame_num;
459 second_field->field_poc[0] = first_field->field_poc[0];
460 second_field->field_poc[1] = first_field->field_poc[1];
461 second_field->output_flag = first_field->output_flag;
462 if (second_field->output_flag) {
463 second_field->output_needed = TRUE;
469 static inline gboolean
470 gst_vaapi_frame_store_has_frame(GstVaapiFrameStore *fs)
472 return fs->structure == GST_VAAPI_PICTURE_STRUCTURE_FRAME;
475 static inline gboolean
476 gst_vaapi_frame_store_has_reference(GstVaapiFrameStore *fs)
480 for (i = 0; i < fs->num_buffers; i++) {
481 if (GST_VAAPI_PICTURE_IS_REFERENCE(fs->buffers[i]))
487 #define gst_vaapi_frame_store_ref(fs) \
488 gst_mini_object_ref(GST_MINI_OBJECT(fs))
490 #define gst_vaapi_frame_store_unref(fs) \
491 gst_mini_object_unref(GST_MINI_OBJECT(fs))
493 #define gst_vaapi_frame_store_replace(old_fs_p, new_fs) \
494 gst_mini_object_replace((GstMiniObject **)(old_fs_p), \
495 (GstMiniObject *)(new_fs))
497 /* ------------------------------------------------------------------------- */
498 /* --- H.264 Decoder --- */
499 /* ------------------------------------------------------------------------- */
501 G_DEFINE_TYPE(GstVaapiDecoderH264,
502 gst_vaapi_decoder_h264,
503 GST_VAAPI_TYPE_DECODER)
505 #define GST_VAAPI_DECODER_H264_GET_PRIVATE(obj) \
506 (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
507 GST_VAAPI_TYPE_DECODER_H264, \
508 GstVaapiDecoderH264Private))
510 struct _GstVaapiDecoderH264Private {
512 GstH264NalParser *parser;
513 /* Last decoded SPS. May not be the last activated one. Just here because
514 it may not fit stack memory allocation in decode_sps() */
516 /* Last decoded PPS. May not be the last activated one. Just here because
517 it may not fit stack memory allocation in decode_pps() */
519 /* Temporary slice header. Just here because it may not fit stack
520 memory allocation in decode_slice() */
521 GstH264SliceHdr temp_slice_hdr;
522 GstVaapiPictureH264 *current_picture;
523 GstVaapiFrameStore *prev_frame;
524 GstVaapiFrameStore *dpb[16];
527 GstVaapiProfile profile;
528 GstVaapiEntrypoint entrypoint;
529 GstVaapiChromaType chroma_type;
530 GstVaapiPictureH264 *short_ref[32];
531 guint short_ref_count;
532 GstVaapiPictureH264 *long_ref[32];
533 guint long_ref_count;
534 GstVaapiPictureH264 *RefPicList0[32];
535 guint RefPicList0_count;
536 GstVaapiPictureH264 *RefPicList1[32];
537 guint RefPicList1_count;
538 guint nal_length_size;
541 gint32 field_poc[2]; // 0:TopFieldOrderCnt / 1:BottomFieldOrderCnt
542 gint32 poc_msb; // PicOrderCntMsb
543 gint32 poc_lsb; // pic_order_cnt_lsb (from slice_header())
544 gint32 prev_poc_msb; // prevPicOrderCntMsb
545 gint32 prev_poc_lsb; // prevPicOrderCntLsb
546 gint32 frame_num_offset; // FrameNumOffset
547 gint32 frame_num; // frame_num (from slice_header())
548 gint32 prev_frame_num; // prevFrameNum
549 gboolean prev_pic_has_mmco5; // prevMmco5Pic
550 gboolean prev_pic_structure; // previous picture structure
551 guint is_constructed : 1;
556 guint has_context : 1;
557 guint progressive_sequence : 1;
561 exec_ref_pic_marking(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture);
563 /* Get number of reference frames to use */
565 get_max_dec_frame_buffering(GstH264SPS *sps)
567 guint max_dec_frame_buffering, MaxDpbMbs, PicSizeMbs;
569 /* Table A-1 - Level limits */
570 switch (sps->level_idc) {
571 case 10: MaxDpbMbs = 396; break;
572 case 11: MaxDpbMbs = 900; break;
573 case 12: MaxDpbMbs = 2376; break;
574 case 13: MaxDpbMbs = 2376; break;
575 case 20: MaxDpbMbs = 2376; break;
576 case 21: MaxDpbMbs = 4752; break;
577 case 22: MaxDpbMbs = 8100; break;
578 case 30: MaxDpbMbs = 8100; break;
579 case 31: MaxDpbMbs = 18000; break;
580 case 32: MaxDpbMbs = 20480; break;
581 case 40: MaxDpbMbs = 32768; break;
582 case 41: MaxDpbMbs = 32768; break;
583 case 42: MaxDpbMbs = 34816; break;
584 case 50: MaxDpbMbs = 110400; break;
585 case 51: MaxDpbMbs = 184320; break;
587 g_assert(0 && "unhandled level");
591 PicSizeMbs = ((sps->pic_width_in_mbs_minus1 + 1) *
592 (sps->pic_height_in_map_units_minus1 + 1) *
593 (sps->frame_mbs_only_flag ? 1 : 2));
594 max_dec_frame_buffering = MaxDpbMbs / PicSizeMbs;
597 if (sps->vui_parameters_present_flag) {
598 GstH264VUIParams * const vui_params = &sps->vui_parameters;
599 if (vui_params->bitstream_restriction_flag)
600 max_dec_frame_buffering = vui_params->max_dec_frame_buffering;
602 switch (sps->profile_idc) {
603 case 44: // CAVLC 4:4:4 Intra profile
604 case 86: // Scalable High profile
605 case 100: // High profile
606 case 110: // High 10 profile
607 case 122: // High 4:2:2 profile
608 case 244: // High 4:4:4 Predictive profile
609 if (sps->constraint_set3_flag)
610 max_dec_frame_buffering = 0;
616 if (max_dec_frame_buffering > 16)
617 max_dec_frame_buffering = 16;
618 else if (max_dec_frame_buffering < sps->num_ref_frames)
619 max_dec_frame_buffering = sps->num_ref_frames;
620 return MAX(1, max_dec_frame_buffering);
624 array_remove_index_fast(void *array, guint *array_length_ptr, guint index)
626 gpointer * const entries = array;
627 guint num_entries = *array_length_ptr;
629 g_return_if_fail(index < num_entries);
631 if (index != --num_entries)
632 entries[index] = entries[num_entries];
633 entries[num_entries] = NULL;
634 *array_length_ptr = num_entries;
639 array_remove_index(void *array, guint *array_length_ptr, guint index)
641 array_remove_index_fast(array, array_length_ptr, index);
645 array_remove_index(void *array, guint *array_length_ptr, guint index)
647 gpointer * const entries = array;
648 const guint num_entries = *array_length_ptr - 1;
651 g_return_if_fail(index <= num_entries);
653 for (i = index; i < num_entries; i++)
654 entries[i] = entries[i + 1];
655 entries[num_entries] = NULL;
656 *array_length_ptr = num_entries;
660 #define ARRAY_REMOVE_INDEX(array, index) \
661 array_remove_index(array, &array##_count, index)
664 dpb_remove_index(GstVaapiDecoderH264 *decoder, guint index)
666 GstVaapiDecoderH264Private * const priv = decoder->priv;
667 guint i, num_frames = --priv->dpb_count;
669 if (USE_STRICT_DPB_ORDERING) {
670 for (i = index; i < num_frames; i++)
671 gst_vaapi_frame_store_replace(&priv->dpb[i], priv->dpb[i + 1]);
673 else if (index != num_frames)
674 gst_vaapi_frame_store_replace(&priv->dpb[index], priv->dpb[num_frames]);
675 gst_vaapi_frame_store_replace(&priv->dpb[num_frames], NULL);
680 GstVaapiDecoderH264 *decoder,
681 GstVaapiFrameStore *fs,
682 GstVaapiPictureH264 *picture
685 picture->output_needed = FALSE;
688 if (--fs->output_needed > 0)
690 picture = fs->buffers[0];
693 /* XXX: update cropping rectangle */
694 return gst_vaapi_picture_output(GST_VAAPI_PICTURE_CAST(picture));
698 dpb_evict(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture, guint i)
700 GstVaapiFrameStore * const fs = decoder->priv->dpb[i];
702 if (!fs->output_needed && !gst_vaapi_frame_store_has_reference(fs))
703 dpb_remove_index(decoder, i);
707 dpb_bump(GstVaapiDecoderH264 *decoder)
709 GstVaapiDecoderH264Private * const priv = decoder->priv;
710 GstVaapiPictureH264 *found_picture = NULL;
711 guint i, j, found_index;
714 for (i = 0; i < priv->dpb_count; i++) {
715 GstVaapiFrameStore * const fs = priv->dpb[i];
716 if (!fs->output_needed)
718 for (j = 0; j < fs->num_buffers; j++) {
719 GstVaapiPictureH264 * const picture = fs->buffers[j];
720 if (!picture->output_needed)
722 if (!found_picture || found_picture->base.poc > picture->base.poc)
723 found_picture = picture, found_index = i;
729 success = dpb_output(decoder, priv->dpb[found_index], found_picture);
730 dpb_evict(decoder, found_picture, found_index);
735 dpb_clear(GstVaapiDecoderH264 *decoder)
737 GstVaapiDecoderH264Private * const priv = decoder->priv;
740 for (i = 0; i < priv->dpb_count; i++)
741 gst_vaapi_frame_store_replace(&priv->dpb[i], NULL);
744 gst_vaapi_frame_store_replace(&priv->prev_frame, NULL);
748 dpb_flush(GstVaapiDecoderH264 *decoder)
750 while (dpb_bump(decoder))
756 dpb_add(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
758 GstVaapiDecoderH264Private * const priv = decoder->priv;
759 GstVaapiFrameStore *fs;
762 // Remove all unused pictures
763 if (!GST_VAAPI_PICTURE_IS_IDR(picture)) {
765 while (i < priv->dpb_count) {
766 GstVaapiFrameStore * const fs = priv->dpb[i];
767 if (!fs->output_needed && !gst_vaapi_frame_store_has_reference(fs))
768 dpb_remove_index(decoder, i);
774 // Check if picture is the second field and the first field is still in DPB
775 fs = priv->prev_frame;
776 if (fs && !gst_vaapi_frame_store_has_frame(fs)) {
777 g_return_val_if_fail(fs->num_buffers == 1, FALSE);
778 g_return_val_if_fail(!GST_VAAPI_PICTURE_IS_FRAME(picture), FALSE);
779 g_return_val_if_fail(!GST_VAAPI_PICTURE_IS_FIRST_FIELD(picture), FALSE);
780 return gst_vaapi_frame_store_add(fs, picture);
783 // Create new frame store, and split fields if necessary
784 fs = gst_vaapi_frame_store_new(picture);
787 gst_vaapi_frame_store_replace(&priv->prev_frame, fs);
788 gst_vaapi_frame_store_unref(fs);
790 if (!priv->progressive_sequence && gst_vaapi_frame_store_has_frame(fs)) {
791 if (!gst_vaapi_frame_store_split_fields(fs))
795 // C.4.5.1 - Storage and marking of a reference decoded picture into the DPB
796 if (GST_VAAPI_PICTURE_IS_REFERENCE(picture)) {
797 while (priv->dpb_count == priv->dpb_size) {
798 if (!dpb_bump(decoder))
801 gst_vaapi_frame_store_replace(&priv->dpb[priv->dpb_count++], fs);
802 if (picture->output_flag) {
803 picture->output_needed = TRUE;
808 // C.4.5.2 - Storage and marking of a non-reference decoded picture into the DPB
810 if (!picture->output_flag)
812 while (priv->dpb_count == priv->dpb_size) {
813 gboolean found_picture = FALSE;
814 for (i = 0; !found_picture && i < priv->dpb_count; i++) {
815 GstVaapiFrameStore * const fs = priv->dpb[i];
816 if (!fs->output_needed)
818 for (j = 0; !found_picture && j < fs->num_buffers; j++)
819 found_picture = fs->buffers[j]->output_needed &&
820 fs->buffers[j]->base.poc < picture->base.poc;
823 return dpb_output(decoder, NULL, picture);
824 if (!dpb_bump(decoder))
827 gst_vaapi_frame_store_replace(&priv->dpb[priv->dpb_count++], fs);
828 picture->output_needed = TRUE;
835 dpb_reset(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
837 GstVaapiDecoderH264Private * const priv = decoder->priv;
839 priv->dpb_size = get_max_dec_frame_buffering(sps);
840 GST_DEBUG("DPB size %u", priv->dpb_size);
843 static GstVaapiDecoderStatus
844 get_status(GstH264ParserResult result)
846 GstVaapiDecoderStatus status;
849 case GST_H264_PARSER_OK:
850 status = GST_VAAPI_DECODER_STATUS_SUCCESS;
852 case GST_H264_PARSER_NO_NAL_END:
853 status = GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
855 case GST_H264_PARSER_ERROR:
856 status = GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
859 status = GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
866 gst_vaapi_decoder_h264_clear_buffer(GstVaapiDecoder *base)
868 GstVaapiDecoderH264 * const decoder = GST_VAAPI_DECODER_H264(base);
869 GstVaapiDecoderH264Private * const priv = decoder->priv;
871 gst_vaapi_picture_replace(&priv->current_picture, NULL);
876 gst_adapter_clear(priv->adapter);
881 gst_vaapi_decoder_h264_close(GstVaapiDecoderH264 *decoder)
883 GstVaapiDecoderH264Private * const priv = decoder->priv;
885 gst_vaapi_decoder_h264_clear_buffer(GST_VAAPI_DECODER_CAST(decoder));
888 gst_h264_nal_parser_free(priv->parser);
893 g_object_unref(priv->adapter);
894 priv->adapter = NULL;
899 gst_vaapi_decoder_h264_open(GstVaapiDecoderH264 *decoder, GstBuffer *buffer)
901 GstVaapiDecoderH264Private * const priv = decoder->priv;
903 gst_vaapi_decoder_h264_close(decoder);
905 priv->adapter = gst_adapter_new();
909 priv->parser = gst_h264_nal_parser_new();
916 gst_vaapi_decoder_h264_destroy(GstVaapiDecoderH264 *decoder)
918 gst_vaapi_decoder_h264_close(decoder);
922 gst_vaapi_decoder_h264_create(GstVaapiDecoderH264 *decoder)
924 if (!GST_VAAPI_DECODER_CODEC(decoder))
930 h264_get_profile(GstH264SPS *sps)
934 switch (sps->profile_idc) {
936 profile = GST_VAAPI_PROFILE_H264_BASELINE;
939 profile = GST_VAAPI_PROFILE_H264_MAIN;
942 profile = GST_VAAPI_PROFILE_H264_HIGH;
949 h264_get_chroma_type(GstH264SPS *sps)
951 guint chroma_type = 0;
953 switch (sps->chroma_format_idc) {
955 chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
958 chroma_type = GST_VAAPI_CHROMA_TYPE_YUV422;
961 if (!sps->separate_colour_plane_flag)
962 chroma_type = GST_VAAPI_CHROMA_TYPE_YUV444;
968 static GstVaapiProfile
969 get_profile(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
971 GstVaapiDecoderH264Private * const priv = decoder->priv;
972 GstVaapiDisplay * const display = GST_VAAPI_DECODER_DISPLAY(decoder);
973 GstVaapiProfile profile, profiles[2];
974 guint i, n_profiles = 0;
976 profile = h264_get_profile(sps);
978 return GST_VAAPI_PROFILE_UNKNOWN;
980 profiles[n_profiles++] = profile;
982 case GST_VAAPI_PROFILE_H264_MAIN:
983 profiles[n_profiles++] = GST_VAAPI_PROFILE_H264_HIGH;
989 /* If the preferred profile (profiles[0]) matches one that we already
990 found, then just return it now instead of searching for it again */
991 if (profiles[0] == priv->profile)
992 return priv->profile;
994 for (i = 0; i < n_profiles; i++) {
995 if (gst_vaapi_display_has_decoder(display, profiles[i], priv->entrypoint))
998 return GST_VAAPI_PROFILE_UNKNOWN;
1001 static GstVaapiDecoderStatus
1002 ensure_context(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
1004 GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER_CAST(decoder);
1005 GstVaapiDecoderH264Private * const priv = decoder->priv;
1006 GstVaapiContextInfo info;
1007 GstVaapiProfile profile;
1008 GstVaapiChromaType chroma_type;
1009 gboolean reset_context = FALSE;
1010 guint mb_width, mb_height;
1012 profile = get_profile(decoder, sps);
1014 GST_ERROR("unsupported profile_idc %u", sps->profile_idc);
1015 return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
1018 if (priv->profile != profile) {
1019 GST_DEBUG("profile changed");
1020 reset_context = TRUE;
1021 priv->profile = profile;
1024 chroma_type = h264_get_chroma_type(sps);
1025 if (!chroma_type || chroma_type != GST_VAAPI_CHROMA_TYPE_YUV420) {
1026 GST_ERROR("unsupported chroma_format_idc %u", sps->chroma_format_idc);
1027 return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CHROMA_FORMAT;
1030 if (priv->chroma_type != chroma_type) {
1031 GST_DEBUG("chroma format changed");
1032 reset_context = TRUE;
1033 priv->chroma_type = chroma_type;
1036 mb_width = sps->pic_width_in_mbs_minus1 + 1;
1037 mb_height = (sps->pic_height_in_map_units_minus1 + 1) <<
1038 !sps->frame_mbs_only_flag;
1039 if (priv->mb_width != mb_width || priv->mb_height != mb_height) {
1040 GST_DEBUG("size changed");
1041 reset_context = TRUE;
1042 priv->mb_width = mb_width;
1043 priv->mb_height = mb_height;
1046 priv->progressive_sequence = sps->frame_mbs_only_flag;
1048 /* XXX: we only output complete frames for now */
1049 gst_vaapi_decoder_set_interlaced(base_decoder, !priv->progressive_sequence);
1052 gst_vaapi_decoder_set_pixel_aspect_ratio(
1054 sps->vui_parameters.par_n,
1055 sps->vui_parameters.par_d
1058 if (!reset_context && priv->has_context)
1059 return GST_VAAPI_DECODER_STATUS_SUCCESS;
1061 /* XXX: fix surface size when cropping is implemented */
1062 info.profile = priv->profile;
1063 info.entrypoint = priv->entrypoint;
1064 info.width = sps->width;
1065 info.height = sps->height;
1066 info.ref_frames = get_max_dec_frame_buffering(sps);
1068 if (!gst_vaapi_decoder_ensure_context(GST_VAAPI_DECODER(decoder), &info))
1069 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
1070 priv->has_context = TRUE;
1073 dpb_reset(decoder, sps);
1074 return GST_VAAPI_DECODER_STATUS_SUCCESS;
1078 fill_iq_matrix_4x4(VAIQMatrixBufferH264 *iq_matrix, const GstH264PPS *pps)
1080 const guint8 (* const ScalingList4x4)[6][16] = &pps->scaling_lists_4x4;
1083 /* There are always 6 4x4 scaling lists */
1084 g_assert(G_N_ELEMENTS(iq_matrix->ScalingList4x4) == 6);
1085 g_assert(G_N_ELEMENTS(iq_matrix->ScalingList4x4[0]) == 16);
1087 if (sizeof(iq_matrix->ScalingList4x4[0][0]) == 1)
1088 memcpy(iq_matrix->ScalingList4x4, *ScalingList4x4,
1089 sizeof(iq_matrix->ScalingList4x4));
1091 for (i = 0; i < G_N_ELEMENTS(iq_matrix->ScalingList4x4); i++) {
1092 for (j = 0; j < G_N_ELEMENTS(iq_matrix->ScalingList4x4[i]); j++)
1093 iq_matrix->ScalingList4x4[i][j] = (*ScalingList4x4)[i][j];
1099 fill_iq_matrix_8x8(VAIQMatrixBufferH264 *iq_matrix, const GstH264PPS *pps)
1101 const guint8 (* const ScalingList8x8)[6][64] = &pps->scaling_lists_8x8;
1102 const GstH264SPS * const sps = pps->sequence;
1105 /* If chroma_format_idc != 3, there are up to 2 8x8 scaling lists */
1106 if (!pps->transform_8x8_mode_flag)
1109 g_assert(G_N_ELEMENTS(iq_matrix->ScalingList8x8) >= 2);
1110 g_assert(G_N_ELEMENTS(iq_matrix->ScalingList8x8[0]) == 64);
1112 if (sizeof(iq_matrix->ScalingList8x8[0][0]) == 1)
1113 memcpy(iq_matrix->ScalingList8x8, *ScalingList8x8,
1114 sizeof(iq_matrix->ScalingList8x8));
1116 n = (sps->chroma_format_idc != 3) ? 2 : 6;
1117 for (i = 0; i < n; i++) {
1118 for (j = 0; j < G_N_ELEMENTS(iq_matrix->ScalingList8x8[i]); j++)
1119 iq_matrix->ScalingList8x8[i][j] = (*ScalingList8x8)[i][j];
1124 static GstVaapiDecoderStatus
1125 ensure_quant_matrix(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
1127 GstVaapiPicture * const base_picture = &picture->base;
1128 GstH264PPS * const pps = picture->pps;
1129 GstH264SPS * const sps = pps->sequence;
1130 VAIQMatrixBufferH264 *iq_matrix;
1132 base_picture->iq_matrix = GST_VAAPI_IQ_MATRIX_NEW(H264, decoder);
1133 if (!base_picture->iq_matrix) {
1134 GST_ERROR("failed to allocate IQ matrix");
1135 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
1137 iq_matrix = base_picture->iq_matrix->param;
1139 /* XXX: we can only support 4:2:0 or 4:2:2 since ScalingLists8x8[]
1140 is not large enough to hold lists for 4:4:4 */
1141 if (sps->chroma_format_idc == 3)
1142 return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CHROMA_FORMAT;
1144 fill_iq_matrix_4x4(iq_matrix, pps);
1145 fill_iq_matrix_8x8(iq_matrix, pps);
1147 return GST_VAAPI_DECODER_STATUS_SUCCESS;
1150 static GstVaapiDecoderStatus
1151 decode_current_picture(GstVaapiDecoderH264 *decoder)
1153 GstVaapiDecoderH264Private * const priv = decoder->priv;
1154 GstVaapiPictureH264 * const picture = priv->current_picture;
1155 GstVaapiDecoderStatus status;
1158 return GST_VAAPI_DECODER_STATUS_SUCCESS;
1160 status = ensure_context(decoder, picture->pps->sequence);
1161 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
1164 if (!exec_ref_pic_marking(decoder, picture))
1166 if (!dpb_add(decoder, picture))
1168 if (!gst_vaapi_picture_decode(GST_VAAPI_PICTURE_CAST(picture)))
1170 if (priv->prev_frame && gst_vaapi_frame_store_has_frame(priv->prev_frame))
1171 gst_vaapi_picture_replace(&priv->current_picture, NULL);
1172 return GST_VAAPI_DECODER_STATUS_SUCCESS;
1175 /* XXX: fix for cases where first field failed to be decoded */
1176 gst_vaapi_picture_replace(&priv->current_picture, NULL);
1177 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
1180 static GstVaapiDecoderStatus
1181 decode_sps(GstVaapiDecoderH264 *decoder, GstH264NalUnit *nalu)
1183 GstVaapiDecoderH264Private * const priv = decoder->priv;
1184 GstH264SPS * const sps = &priv->last_sps;
1185 GstH264ParserResult result;
1187 GST_DEBUG("decode SPS");
1189 memset(sps, 0, sizeof(*sps));
1190 result = gst_h264_parser_parse_sps(priv->parser, nalu, sps, TRUE);
1191 if (result != GST_H264_PARSER_OK) {
1192 priv->got_sps = FALSE;
1193 return get_status(result);
1196 priv->got_sps = TRUE;
1197 return GST_VAAPI_DECODER_STATUS_SUCCESS;
1200 static GstVaapiDecoderStatus
1201 decode_pps(GstVaapiDecoderH264 *decoder, GstH264NalUnit *nalu)
1203 GstVaapiDecoderH264Private * const priv = decoder->priv;
1204 GstH264PPS * const pps = &priv->last_pps;
1205 GstH264ParserResult result;
1207 GST_DEBUG("decode PPS");
1209 memset(pps, 0, sizeof(*pps));
1210 result = gst_h264_parser_parse_pps(priv->parser, nalu, pps);
1211 if (result != GST_H264_PARSER_OK) {
1212 priv->got_pps = FALSE;
1213 return get_status(result);
1216 priv->got_pps = TRUE;
1217 return GST_VAAPI_DECODER_STATUS_SUCCESS;
1220 static GstVaapiDecoderStatus
1221 decode_sei(GstVaapiDecoderH264 *decoder, GstH264NalUnit *nalu)
1223 GstVaapiDecoderH264Private * const priv = decoder->priv;
1224 GstH264SEIMessage sei;
1225 GstH264ParserResult result;
1227 GST_DEBUG("decode SEI");
1229 memset(&sei, 0, sizeof(sei));
1230 result = gst_h264_parser_parse_sei(priv->parser, nalu, &sei);
1231 if (result != GST_H264_PARSER_OK) {
1232 GST_WARNING("failed to decode SEI, payload type:%d", sei.payloadType);
1233 return get_status(result);
1236 return GST_VAAPI_DECODER_STATUS_SUCCESS;
1239 static GstVaapiDecoderStatus
1240 decode_sequence_end(GstVaapiDecoderH264 *decoder)
1242 GstVaapiDecoderStatus status;
1244 GST_DEBUG("decode sequence-end");
1246 status = decode_current_picture(decoder);
1247 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
1251 return GST_VAAPI_DECODER_STATUS_END_OF_STREAM;
1254 /* 8.2.1.1 - Decoding process for picture order count type 0 */
1257 GstVaapiDecoderH264 *decoder,
1258 GstVaapiPictureH264 *picture,
1259 GstH264SliceHdr *slice_hdr
1262 GstVaapiDecoderH264Private * const priv = decoder->priv;
1263 GstH264PPS * const pps = slice_hdr->pps;
1264 GstH264SPS * const sps = pps->sequence;
1265 const gint32 MaxPicOrderCntLsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1268 GST_DEBUG("decode picture order count type 0");
1270 if (GST_VAAPI_PICTURE_IS_IDR(picture)) {
1271 priv->prev_poc_msb = 0;
1272 priv->prev_poc_lsb = 0;
1274 else if (priv->prev_pic_has_mmco5) {
1275 priv->prev_poc_msb = 0;
1276 priv->prev_poc_lsb =
1277 (priv->prev_pic_structure == GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD ?
1278 0 : priv->field_poc[TOP_FIELD]);
1281 priv->prev_poc_msb = priv->poc_msb;
1282 priv->prev_poc_lsb = priv->poc_lsb;
1286 priv->poc_lsb = slice_hdr->pic_order_cnt_lsb;
1287 if (priv->poc_lsb < priv->prev_poc_lsb &&
1288 (priv->prev_poc_lsb - priv->poc_lsb) >= (MaxPicOrderCntLsb / 2))
1289 priv->poc_msb = priv->prev_poc_msb + MaxPicOrderCntLsb;
1290 else if (priv->poc_lsb > priv->prev_poc_lsb &&
1291 (priv->poc_lsb - priv->prev_poc_lsb) > (MaxPicOrderCntLsb / 2))
1292 priv->poc_msb = priv->prev_poc_msb - MaxPicOrderCntLsb;
1294 priv->poc_msb = priv->prev_poc_msb;
1296 temp_poc = priv->poc_msb + priv->poc_lsb;
1297 switch (picture->structure) {
1298 case GST_VAAPI_PICTURE_STRUCTURE_FRAME:
1300 priv->field_poc[TOP_FIELD] = temp_poc;
1301 priv->field_poc[BOTTOM_FIELD] = temp_poc +
1302 slice_hdr->delta_pic_order_cnt_bottom;
1304 case GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD:
1306 priv->field_poc[TOP_FIELD] = temp_poc;
1308 case GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD:
1310 priv->field_poc[BOTTOM_FIELD] = temp_poc;
1315 /* 8.2.1.2 - Decoding process for picture order count type 1 */
1318 GstVaapiDecoderH264 *decoder,
1319 GstVaapiPictureH264 *picture,
1320 GstH264SliceHdr *slice_hdr
1323 GstVaapiDecoderH264Private * const priv = decoder->priv;
1324 GstH264PPS * const pps = slice_hdr->pps;
1325 GstH264SPS * const sps = pps->sequence;
1326 const gint32 MaxFrameNum = 1 << (sps->log2_max_frame_num_minus4 + 4);
1327 gint32 prev_frame_num_offset, abs_frame_num, expected_poc;
1330 GST_DEBUG("decode picture order count type 1");
1332 if (priv->prev_pic_has_mmco5)
1333 prev_frame_num_offset = 0;
1335 prev_frame_num_offset = priv->frame_num_offset;
1338 if (GST_VAAPI_PICTURE_IS_IDR(picture))
1339 priv->frame_num_offset = 0;
1340 else if (priv->prev_frame_num > priv->frame_num)
1341 priv->frame_num_offset = prev_frame_num_offset + MaxFrameNum;
1343 priv->frame_num_offset = prev_frame_num_offset;
1346 if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0)
1347 abs_frame_num = priv->frame_num_offset + priv->frame_num;
1350 if (!GST_VAAPI_PICTURE_IS_REFERENCE(picture) && abs_frame_num > 0)
1351 abs_frame_num = abs_frame_num - 1;
1353 if (abs_frame_num > 0) {
1354 gint32 expected_delta_per_poc_cycle;
1355 gint32 poc_cycle_cnt, frame_num_in_poc_cycle;
1357 expected_delta_per_poc_cycle = 0;
1358 for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
1359 expected_delta_per_poc_cycle += sps->offset_for_ref_frame[i];
1362 poc_cycle_cnt = (abs_frame_num - 1) /
1363 sps->num_ref_frames_in_pic_order_cnt_cycle;
1364 frame_num_in_poc_cycle = (abs_frame_num - 1) %
1365 sps->num_ref_frames_in_pic_order_cnt_cycle;
1368 expected_poc = poc_cycle_cnt * expected_delta_per_poc_cycle;
1369 for (i = 0; i <= frame_num_in_poc_cycle; i++)
1370 expected_poc += sps->offset_for_ref_frame[i];
1374 if (!GST_VAAPI_PICTURE_IS_REFERENCE(picture))
1375 expected_poc += sps->offset_for_non_ref_pic;
1378 switch (picture->structure) {
1379 case GST_VAAPI_PICTURE_STRUCTURE_FRAME:
1380 priv->field_poc[TOP_FIELD] = expected_poc +
1381 slice_hdr->delta_pic_order_cnt[0];
1382 priv->field_poc[BOTTOM_FIELD] = priv->field_poc[TOP_FIELD] +
1383 sps->offset_for_top_to_bottom_field +
1384 slice_hdr->delta_pic_order_cnt[1];
1386 case GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD:
1387 priv->field_poc[TOP_FIELD] = expected_poc +
1388 slice_hdr->delta_pic_order_cnt[0];
1390 case GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD:
1391 priv->field_poc[BOTTOM_FIELD] = expected_poc +
1392 sps->offset_for_top_to_bottom_field +
1393 slice_hdr->delta_pic_order_cnt[0];
1398 /* 8.2.1.3 - Decoding process for picture order count type 2 */
1401 GstVaapiDecoderH264 *decoder,
1402 GstVaapiPictureH264 *picture,
1403 GstH264SliceHdr *slice_hdr
1406 GstVaapiDecoderH264Private * const priv = decoder->priv;
1407 GstH264PPS * const pps = slice_hdr->pps;
1408 GstH264SPS * const sps = pps->sequence;
1409 const gint32 MaxFrameNum = 1 << (sps->log2_max_frame_num_minus4 + 4);
1410 gint32 prev_frame_num_offset, temp_poc;
1412 GST_DEBUG("decode picture order count type 2");
1414 if (priv->prev_pic_has_mmco5)
1415 prev_frame_num_offset = 0;
1417 prev_frame_num_offset = priv->frame_num_offset;
1420 if (GST_VAAPI_PICTURE_IS_IDR(picture))
1421 priv->frame_num_offset = 0;
1422 else if (priv->prev_frame_num > priv->frame_num)
1423 priv->frame_num_offset = prev_frame_num_offset + MaxFrameNum;
1425 priv->frame_num_offset = prev_frame_num_offset;
1428 if (GST_VAAPI_PICTURE_IS_IDR(picture))
1430 else if (!GST_VAAPI_PICTURE_IS_REFERENCE(picture))
1431 temp_poc = 2 * (priv->frame_num_offset + priv->frame_num) - 1;
1433 temp_poc = 2 * (priv->frame_num_offset + priv->frame_num);
1436 if (picture->structure != GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD)
1437 priv->field_poc[TOP_FIELD] = temp_poc;
1438 if (picture->structure != GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD)
1439 priv->field_poc[BOTTOM_FIELD] = temp_poc;
1442 /* 8.2.1 - Decoding process for picture order count */
1445 GstVaapiDecoderH264 *decoder,
1446 GstVaapiPictureH264 *picture,
1447 GstH264SliceHdr *slice_hdr
1450 GstVaapiDecoderH264Private * const priv = decoder->priv;
1451 GstH264PPS * const pps = slice_hdr->pps;
1452 GstH264SPS * const sps = pps->sequence;
1454 switch (sps->pic_order_cnt_type) {
1456 init_picture_poc_0(decoder, picture, slice_hdr);
1459 init_picture_poc_1(decoder, picture, slice_hdr);
1462 init_picture_poc_2(decoder, picture, slice_hdr);
1466 if (picture->structure != GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD)
1467 picture->field_poc[TOP_FIELD] = priv->field_poc[TOP_FIELD];
1468 if (picture->structure != GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD)
1469 picture->field_poc[BOTTOM_FIELD] = priv->field_poc[BOTTOM_FIELD];
1470 picture->base.poc = MIN(picture->field_poc[0], picture->field_poc[1]);
1474 compare_picture_pic_num_dec(const void *a, const void *b)
1476 const GstVaapiPictureH264 * const picA = *(GstVaapiPictureH264 **)a;
1477 const GstVaapiPictureH264 * const picB = *(GstVaapiPictureH264 **)b;
1479 return picB->pic_num - picA->pic_num;
1483 compare_picture_long_term_pic_num_inc(const void *a, const void *b)
1485 const GstVaapiPictureH264 * const picA = *(GstVaapiPictureH264 **)a;
1486 const GstVaapiPictureH264 * const picB = *(GstVaapiPictureH264 **)b;
1488 return picA->long_term_pic_num - picB->long_term_pic_num;
1492 compare_picture_poc_dec(const void *a, const void *b)
1494 const GstVaapiPictureH264 * const picA = *(GstVaapiPictureH264 **)a;
1495 const GstVaapiPictureH264 * const picB = *(GstVaapiPictureH264 **)b;
1497 return picB->base.poc - picA->base.poc;
1501 compare_picture_poc_inc(const void *a, const void *b)
1503 const GstVaapiPictureH264 * const picA = *(GstVaapiPictureH264 **)a;
1504 const GstVaapiPictureH264 * const picB = *(GstVaapiPictureH264 **)b;
1506 return picA->base.poc - picB->base.poc;
1510 compare_picture_frame_num_wrap_dec(const void *a, const void *b)
1512 const GstVaapiPictureH264 * const picA = *(GstVaapiPictureH264 **)a;
1513 const GstVaapiPictureH264 * const picB = *(GstVaapiPictureH264 **)b;
1515 return picB->frame_num_wrap - picA->frame_num_wrap;
1519 compare_picture_long_term_frame_idx_inc(const void *a, const void *b)
1521 const GstVaapiPictureH264 * const picA = *(GstVaapiPictureH264 **)a;
1522 const GstVaapiPictureH264 * const picB = *(GstVaapiPictureH264 **)b;
1524 return picA->long_term_frame_idx - picB->long_term_frame_idx;
1527 /* 8.2.4.1 - Decoding process for picture numbers */
1529 init_picture_refs_pic_num(
1530 GstVaapiDecoderH264 *decoder,
1531 GstVaapiPictureH264 *picture,
1532 GstH264SliceHdr *slice_hdr
1535 GstVaapiDecoderH264Private * const priv = decoder->priv;
1536 GstH264PPS * const pps = slice_hdr->pps;
1537 GstH264SPS * const sps = pps->sequence;
1538 const gint32 MaxFrameNum = 1 << (sps->log2_max_frame_num_minus4 + 4);
1541 GST_DEBUG("decode picture numbers");
1543 for (i = 0; i < priv->short_ref_count; i++) {
1544 GstVaapiPictureH264 * const pic = priv->short_ref[i];
1547 if (pic->frame_num > priv->frame_num)
1548 pic->frame_num_wrap = pic->frame_num - MaxFrameNum;
1550 pic->frame_num_wrap = pic->frame_num;
1552 // (8-28, 8-30, 8-31)
1553 if (GST_VAAPI_PICTURE_IS_FRAME(picture))
1554 pic->pic_num = pic->frame_num_wrap;
1556 if (pic->structure == picture->structure)
1557 pic->pic_num = 2 * pic->frame_num_wrap + 1;
1559 pic->pic_num = 2 * pic->frame_num_wrap;
1563 for (i = 0; i < priv->long_ref_count; i++) {
1564 GstVaapiPictureH264 * const pic = priv->long_ref[i];
1566 // (8-29, 8-32, 8-33)
1567 if (GST_VAAPI_PICTURE_IS_FRAME(picture))
1568 pic->long_term_pic_num = pic->long_term_frame_idx;
1570 if (pic->structure == picture->structure)
1571 pic->long_term_pic_num = 2 * pic->long_term_frame_idx + 1;
1573 pic->long_term_pic_num = 2 * pic->long_term_frame_idx;
1578 #define SORT_REF_LIST(list, n, compare_func) \
1579 qsort(list, n, sizeof(*(list)), compare_picture_##compare_func)
1582 init_picture_refs_fields_1(
1583 guint picture_structure,
1584 GstVaapiPictureH264 *RefPicList[32],
1585 guint *RefPicList_count,
1586 GstVaapiPictureH264 *ref_list[32],
1587 guint ref_list_count
1594 n = *RefPicList_count;
1597 for (; i < ref_list_count; i++) {
1598 if (ref_list[i]->structure == picture_structure) {
1599 RefPicList[n++] = ref_list[i++];
1603 for (; j < ref_list_count; j++) {
1604 if (ref_list[j]->structure != picture_structure) {
1605 RefPicList[n++] = ref_list[j++];
1609 } while (i < ref_list_count || j < ref_list_count);
1610 *RefPicList_count = n;
1614 init_picture_refs_fields(
1615 GstVaapiPictureH264 *picture,
1616 GstVaapiPictureH264 *RefPicList[32],
1617 guint *RefPicList_count,
1618 GstVaapiPictureH264 *short_ref[32],
1619 guint short_ref_count,
1620 GstVaapiPictureH264 *long_ref[32],
1621 guint long_ref_count
1626 /* 8.2.4.2.5 - reference picture lists in fields */
1627 init_picture_refs_fields_1(picture->structure, RefPicList, &n,
1628 short_ref, short_ref_count);
1629 init_picture_refs_fields_1(picture->structure, RefPicList, &n,
1630 long_ref, long_ref_count);
1631 *RefPicList_count = n;
1635 init_picture_refs_p_slice(
1636 GstVaapiDecoderH264 *decoder,
1637 GstVaapiPictureH264 *picture,
1638 GstH264SliceHdr *slice_hdr
1641 GstVaapiDecoderH264Private * const priv = decoder->priv;
1642 GstVaapiPictureH264 **ref_list;
1645 GST_DEBUG("decode reference picture list for P and SP slices");
1647 if (GST_VAAPI_PICTURE_IS_FRAME(picture)) {
1648 /* 8.2.4.2.1 - P and SP slices in frames */
1649 if (priv->short_ref_count > 0) {
1650 ref_list = priv->RefPicList0;
1651 for (i = 0; i < priv->short_ref_count; i++)
1652 ref_list[i] = priv->short_ref[i];
1653 SORT_REF_LIST(ref_list, i, pic_num_dec);
1654 priv->RefPicList0_count += i;
1657 if (priv->long_ref_count > 0) {
1658 ref_list = &priv->RefPicList0[priv->RefPicList0_count];
1659 for (i = 0; i < priv->long_ref_count; i++)
1660 ref_list[i] = priv->long_ref[i];
1661 SORT_REF_LIST(ref_list, i, long_term_pic_num_inc);
1662 priv->RefPicList0_count += i;
1666 /* 8.2.4.2.2 - P and SP slices in fields */
1667 GstVaapiPictureH264 *short_ref[32];
1668 guint short_ref_count = 0;
1669 GstVaapiPictureH264 *long_ref[32];
1670 guint long_ref_count = 0;
1672 if (priv->short_ref_count > 0) {
1673 for (i = 0; i < priv->short_ref_count; i++)
1674 short_ref[i] = priv->short_ref[i];
1675 SORT_REF_LIST(short_ref, i, frame_num_wrap_dec);
1676 short_ref_count = i;
1679 if (priv->long_ref_count > 0) {
1680 for (i = 0; i < priv->long_ref_count; i++)
1681 long_ref[i] = priv->long_ref[i];
1682 SORT_REF_LIST(long_ref, i, long_term_frame_idx_inc);
1686 init_picture_refs_fields(
1688 priv->RefPicList0, &priv->RefPicList0_count,
1689 short_ref, short_ref_count,
1690 long_ref, long_ref_count
1696 init_picture_refs_b_slice(
1697 GstVaapiDecoderH264 *decoder,
1698 GstVaapiPictureH264 *picture,
1699 GstH264SliceHdr *slice_hdr
1702 GstVaapiDecoderH264Private * const priv = decoder->priv;
1703 GstVaapiPictureH264 **ref_list;
1706 GST_DEBUG("decode reference picture list for B slices");
1708 if (GST_VAAPI_PICTURE_IS_FRAME(picture)) {
1709 /* 8.2.4.2.3 - B slices in frames */
1712 if (priv->short_ref_count > 0) {
1713 // 1. Short-term references
1714 ref_list = priv->RefPicList0;
1715 for (n = 0, i = 0; i < priv->short_ref_count; i++) {
1716 if (priv->short_ref[i]->base.poc < picture->base.poc)
1717 ref_list[n++] = priv->short_ref[i];
1719 SORT_REF_LIST(ref_list, n, poc_dec);
1720 priv->RefPicList0_count += n;
1722 ref_list = &priv->RefPicList0[priv->RefPicList0_count];
1723 for (n = 0, i = 0; i < priv->short_ref_count; i++) {
1724 if (priv->short_ref[i]->base.poc >= picture->base.poc)
1725 ref_list[n++] = priv->short_ref[i];
1727 SORT_REF_LIST(ref_list, n, poc_inc);
1728 priv->RefPicList0_count += n;
1731 if (priv->long_ref_count > 0) {
1732 // 2. Long-term references
1733 ref_list = &priv->RefPicList0[priv->RefPicList0_count];
1734 for (n = 0, i = 0; i < priv->long_ref_count; i++)
1735 ref_list[n++] = priv->long_ref[i];
1736 SORT_REF_LIST(ref_list, n, long_term_pic_num_inc);
1737 priv->RefPicList0_count += n;
1741 if (priv->short_ref_count > 0) {
1742 // 1. Short-term references
1743 ref_list = priv->RefPicList1;
1744 for (n = 0, i = 0; i < priv->short_ref_count; i++) {
1745 if (priv->short_ref[i]->base.poc > picture->base.poc)
1746 ref_list[n++] = priv->short_ref[i];
1748 SORT_REF_LIST(ref_list, n, poc_inc);
1749 priv->RefPicList1_count += n;
1751 ref_list = &priv->RefPicList1[priv->RefPicList1_count];
1752 for (n = 0, i = 0; i < priv->short_ref_count; i++) {
1753 if (priv->short_ref[i]->base.poc <= picture->base.poc)
1754 ref_list[n++] = priv->short_ref[i];
1756 SORT_REF_LIST(ref_list, n, poc_dec);
1757 priv->RefPicList1_count += n;
1760 if (priv->long_ref_count > 0) {
1761 // 2. Long-term references
1762 ref_list = &priv->RefPicList1[priv->RefPicList1_count];
1763 for (n = 0, i = 0; i < priv->long_ref_count; i++)
1764 ref_list[n++] = priv->long_ref[i];
1765 SORT_REF_LIST(ref_list, n, long_term_pic_num_inc);
1766 priv->RefPicList1_count += n;
1770 /* 8.2.4.2.4 - B slices in fields */
1771 GstVaapiPictureH264 *short_ref0[32];
1772 guint short_ref0_count = 0;
1773 GstVaapiPictureH264 *short_ref1[32];
1774 guint short_ref1_count = 0;
1775 GstVaapiPictureH264 *long_ref[32];
1776 guint long_ref_count = 0;
1778 /* refFrameList0ShortTerm */
1779 if (priv->short_ref_count > 0) {
1780 ref_list = short_ref0;
1781 for (n = 0, i = 0; i < priv->short_ref_count; i++) {
1782 if (priv->short_ref[i]->base.poc <= picture->base.poc)
1783 ref_list[n++] = priv->short_ref[i];
1785 SORT_REF_LIST(ref_list, n, poc_dec);
1786 short_ref0_count += n;
1788 ref_list = &short_ref0[short_ref0_count];
1789 for (n = 0, i = 0; i < priv->short_ref_count; i++) {
1790 if (priv->short_ref[i]->base.poc > picture->base.poc)
1791 ref_list[n++] = priv->short_ref[i];
1793 SORT_REF_LIST(ref_list, n, poc_inc);
1794 short_ref0_count += n;
1797 /* refFrameList1ShortTerm */
1798 if (priv->short_ref_count > 0) {
1799 ref_list = short_ref1;
1800 for (n = 0, i = 0; i < priv->short_ref_count; i++) {
1801 if (priv->short_ref[i]->base.poc > picture->base.poc)
1802 ref_list[n++] = priv->short_ref[i];
1804 SORT_REF_LIST(ref_list, n, poc_inc);
1805 short_ref1_count += n;
1807 ref_list = &short_ref1[short_ref1_count];
1808 for (n = 0, i = 0; i < priv->short_ref_count; i++) {
1809 if (priv->short_ref[i]->base.poc <= picture->base.poc)
1810 ref_list[n++] = priv->short_ref[i];
1812 SORT_REF_LIST(ref_list, n, poc_dec);
1813 short_ref1_count += n;
1816 /* refFrameListLongTerm */
1817 if (priv->long_ref_count > 0) {
1818 for (i = 0; i < priv->long_ref_count; i++)
1819 long_ref[i] = priv->long_ref[i];
1820 SORT_REF_LIST(long_ref, i, long_term_frame_idx_inc);
1824 init_picture_refs_fields(
1826 priv->RefPicList0, &priv->RefPicList0_count,
1827 short_ref0, short_ref0_count,
1828 long_ref, long_ref_count
1831 init_picture_refs_fields(
1833 priv->RefPicList1, &priv->RefPicList1_count,
1834 short_ref1, short_ref1_count,
1835 long_ref, long_ref_count
1839 /* Check whether RefPicList1 is identical to RefPicList0, then
1840 swap if necessary */
1841 if (priv->RefPicList1_count > 1 &&
1842 priv->RefPicList1_count == priv->RefPicList0_count &&
1843 memcmp(priv->RefPicList0, priv->RefPicList1,
1844 priv->RefPicList0_count * sizeof(priv->RefPicList0[0])) == 0) {
1845 GstVaapiPictureH264 * const tmp = priv->RefPicList1[0];
1846 priv->RefPicList1[0] = priv->RefPicList1[1];
1847 priv->RefPicList1[1] = tmp;
1851 #undef SORT_REF_LIST
1854 find_short_term_reference(GstVaapiDecoderH264 *decoder, gint32 pic_num)
1856 GstVaapiDecoderH264Private * const priv = decoder->priv;
1859 for (i = 0; i < priv->short_ref_count; i++) {
1860 if (priv->short_ref[i]->pic_num == pic_num)
1863 GST_ERROR("found no short-term reference picture with PicNum = %d",
1869 find_long_term_reference(GstVaapiDecoderH264 *decoder, gint32 long_term_pic_num)
1871 GstVaapiDecoderH264Private * const priv = decoder->priv;
1874 for (i = 0; i < priv->long_ref_count; i++) {
1875 if (priv->long_ref[i]->long_term_pic_num == long_term_pic_num)
1878 GST_ERROR("found no long-term reference picture with LongTermPicNum = %d",
1884 exec_picture_refs_modification_1(
1885 GstVaapiDecoderH264 *decoder,
1886 GstVaapiPictureH264 *picture,
1887 GstH264SliceHdr *slice_hdr,
1891 GstVaapiDecoderH264Private * const priv = decoder->priv;
1892 GstH264PPS * const pps = slice_hdr->pps;
1893 GstH264SPS * const sps = pps->sequence;
1894 GstH264RefPicListModification *ref_pic_list_modification;
1895 guint num_ref_pic_list_modifications;
1896 GstVaapiPictureH264 **ref_list;
1897 guint *ref_list_count_ptr, ref_list_count, ref_list_idx = 0;
1898 guint i, j, n, num_refs;
1900 gint32 MaxPicNum, CurrPicNum, picNumPred;
1902 GST_DEBUG("modification process of reference picture list %u", list);
1905 ref_pic_list_modification = slice_hdr->ref_pic_list_modification_l0;
1906 num_ref_pic_list_modifications = slice_hdr->n_ref_pic_list_modification_l0;
1907 ref_list = priv->RefPicList0;
1908 ref_list_count_ptr = &priv->RefPicList0_count;
1909 num_refs = slice_hdr->num_ref_idx_l0_active_minus1 + 1;
1912 ref_pic_list_modification = slice_hdr->ref_pic_list_modification_l1;
1913 num_ref_pic_list_modifications = slice_hdr->n_ref_pic_list_modification_l1;
1914 ref_list = priv->RefPicList1;
1915 ref_list_count_ptr = &priv->RefPicList1_count;
1916 num_refs = slice_hdr->num_ref_idx_l1_active_minus1 + 1;
1918 ref_list_count = *ref_list_count_ptr;
1920 if (!GST_VAAPI_PICTURE_IS_FRAME(picture)) {
1921 MaxPicNum = 1 << (sps->log2_max_frame_num_minus4 + 5); // 2 * MaxFrameNum
1922 CurrPicNum = 2 * slice_hdr->frame_num + 1; // 2 * frame_num + 1
1925 MaxPicNum = 1 << (sps->log2_max_frame_num_minus4 + 4); // MaxFrameNum
1926 CurrPicNum = slice_hdr->frame_num; // frame_num
1929 picNumPred = CurrPicNum;
1931 for (i = 0; i < num_ref_pic_list_modifications; i++) {
1932 GstH264RefPicListModification * const l = &ref_pic_list_modification[i];
1933 if (l->modification_of_pic_nums_idc == 3)
1936 /* 8.2.4.3.1 - Short-term reference pictures */
1937 if (l->modification_of_pic_nums_idc == 0 || l->modification_of_pic_nums_idc == 1) {
1938 gint32 abs_diff_pic_num = l->value.abs_diff_pic_num_minus1 + 1;
1939 gint32 picNum, picNumNoWrap;
1942 if (l->modification_of_pic_nums_idc == 0) {
1943 picNumNoWrap = picNumPred - abs_diff_pic_num;
1944 if (picNumNoWrap < 0)
1945 picNumNoWrap += MaxPicNum;
1950 picNumNoWrap = picNumPred + abs_diff_pic_num;
1951 if (picNumNoWrap >= MaxPicNum)
1952 picNumNoWrap -= MaxPicNum;
1954 picNumPred = picNumNoWrap;
1957 picNum = picNumNoWrap;
1958 if (picNum > CurrPicNum)
1959 picNum -= MaxPicNum;
1962 for (j = num_refs; j > ref_list_idx; j--)
1963 ref_list[j] = ref_list[j - 1];
1964 found_ref_idx = find_short_term_reference(decoder, picNum);
1965 ref_list[ref_list_idx++] =
1966 found_ref_idx >= 0 ? priv->short_ref[found_ref_idx] : NULL;
1968 for (j = ref_list_idx; j <= num_refs; j++) {
1973 GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(ref_list[j]) ?
1974 ref_list[j]->pic_num : MaxPicNum;
1975 if (PicNumF != picNum)
1976 ref_list[n++] = ref_list[j];
1980 /* 8.2.4.3.2 - Long-term reference pictures */
1983 for (j = num_refs; j > ref_list_idx; j--)
1984 ref_list[j] = ref_list[j - 1];
1986 find_long_term_reference(decoder, l->value.long_term_pic_num);
1987 ref_list[ref_list_idx++] =
1988 found_ref_idx >= 0 ? priv->long_ref[found_ref_idx] : NULL;
1990 for (j = ref_list_idx; j <= num_refs; j++) {
1991 gint32 LongTermPicNumF;
1995 GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(ref_list[j]) ?
1996 ref_list[j]->long_term_pic_num : INT_MAX;
1997 if (LongTermPicNumF != l->value.long_term_pic_num)
1998 ref_list[n++] = ref_list[j];
2004 for (i = 0; i < num_refs; i++)
2006 GST_ERROR("list %u entry %u is empty", list, i);
2008 *ref_list_count_ptr = num_refs;
2011 /* 8.2.4.3 - Modification process for reference picture lists */
2013 exec_picture_refs_modification(
2014 GstVaapiDecoderH264 *decoder,
2015 GstVaapiPictureH264 *picture,
2016 GstH264SliceHdr *slice_hdr
2019 GST_DEBUG("execute ref_pic_list_modification()");
2022 if (!GST_H264_IS_I_SLICE(slice_hdr) && !GST_H264_IS_SI_SLICE(slice_hdr) &&
2023 slice_hdr->ref_pic_list_modification_flag_l0)
2024 exec_picture_refs_modification_1(decoder, picture, slice_hdr, 0);
2027 if (GST_H264_IS_B_SLICE(slice_hdr) &&
2028 slice_hdr->ref_pic_list_modification_flag_l1)
2029 exec_picture_refs_modification_1(decoder, picture, slice_hdr, 1);
2033 init_picture_ref_lists(GstVaapiDecoderH264 *decoder)
2035 GstVaapiDecoderH264Private * const priv = decoder->priv;
2036 guint i, j, short_ref_count, long_ref_count;
2038 short_ref_count = 0;
2040 if (GST_VAAPI_PICTURE_IS_FRAME(priv->current_picture)) {
2041 for (i = 0; i < priv->dpb_count; i++) {
2042 GstVaapiFrameStore * const fs = priv->dpb[i];
2043 GstVaapiPictureH264 *picture;
2044 if (!gst_vaapi_frame_store_has_frame(fs))
2046 picture = fs->buffers[0];
2047 if (GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(picture))
2048 priv->short_ref[short_ref_count++] = picture;
2049 else if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(picture))
2050 priv->long_ref[long_ref_count++] = picture;
2051 picture->structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
2052 picture->other_field = fs->buffers[1];
2056 for (i = 0; i < priv->dpb_count; i++) {
2057 GstVaapiFrameStore * const fs = priv->dpb[i];
2058 for (j = 0; j < fs->num_buffers; j++) {
2059 GstVaapiPictureH264 * const picture = fs->buffers[j];
2060 if (GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(picture))
2061 priv->short_ref[short_ref_count++] = picture;
2062 else if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(picture))
2063 priv->long_ref[long_ref_count++] = picture;
2064 picture->structure = picture->base.structure;
2065 picture->other_field = fs->buffers[j ^ 1];
2070 for (i = short_ref_count; i < priv->short_ref_count; i++)
2071 priv->short_ref[i] = NULL;
2072 priv->short_ref_count = short_ref_count;
2074 for (i = long_ref_count; i < priv->long_ref_count; i++)
2075 priv->long_ref[i] = NULL;
2076 priv->long_ref_count = long_ref_count;
2081 GstVaapiDecoderH264 *decoder,
2082 GstVaapiPictureH264 *picture,
2083 GstH264SliceHdr *slice_hdr
2086 GstVaapiDecoderH264Private * const priv = decoder->priv;
2087 GstVaapiPicture * const base_picture = &picture->base;
2090 init_picture_ref_lists(decoder);
2091 init_picture_refs_pic_num(decoder, picture, slice_hdr);
2093 priv->RefPicList0_count = 0;
2094 priv->RefPicList1_count = 0;
2096 switch (base_picture->type) {
2097 case GST_VAAPI_PICTURE_TYPE_P:
2098 case GST_VAAPI_PICTURE_TYPE_SP:
2099 init_picture_refs_p_slice(decoder, picture, slice_hdr);
2101 case GST_VAAPI_PICTURE_TYPE_B:
2102 init_picture_refs_b_slice(decoder, picture, slice_hdr);
2108 exec_picture_refs_modification(decoder, picture, slice_hdr);
2110 switch (base_picture->type) {
2111 case GST_VAAPI_PICTURE_TYPE_B:
2112 num_refs = 1 + slice_hdr->num_ref_idx_l1_active_minus1;
2113 for (i = priv->RefPicList1_count; i < num_refs; i++)
2114 priv->RefPicList1[i] = NULL;
2115 priv->RefPicList1_count = num_refs;
2118 case GST_VAAPI_PICTURE_TYPE_P:
2119 case GST_VAAPI_PICTURE_TYPE_SP:
2120 num_refs = 1 + slice_hdr->num_ref_idx_l0_active_minus1;
2121 for (i = priv->RefPicList0_count; i < num_refs; i++)
2122 priv->RefPicList0[i] = NULL;
2123 priv->RefPicList0_count = num_refs;
2132 GstVaapiDecoderH264 *decoder,
2133 GstVaapiPictureH264 *picture,
2134 GstH264SliceHdr *slice_hdr,
2135 GstH264NalUnit *nalu
2138 GstVaapiDecoderH264Private * const priv = decoder->priv;
2139 GstVaapiPicture * const base_picture = &picture->base;
2141 priv->prev_frame_num = priv->frame_num;
2142 priv->frame_num = slice_hdr->frame_num;
2143 picture->frame_num = priv->frame_num;
2144 picture->frame_num_wrap = priv->frame_num;
2145 picture->output_flag = TRUE; /* XXX: conformant to Annex A only */
2146 base_picture->pts = gst_adapter_prev_timestamp(priv->adapter, NULL);
2148 /* Reset decoder state for IDR pictures */
2149 if (nalu->type == GST_H264_NAL_SLICE_IDR) {
2151 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_IDR);
2155 /* Initialize slice type */
2156 switch (slice_hdr->type % 5) {
2157 case GST_H264_P_SLICE:
2158 base_picture->type = GST_VAAPI_PICTURE_TYPE_P;
2160 case GST_H264_B_SLICE:
2161 base_picture->type = GST_VAAPI_PICTURE_TYPE_B;
2163 case GST_H264_I_SLICE:
2164 base_picture->type = GST_VAAPI_PICTURE_TYPE_I;
2166 case GST_H264_SP_SLICE:
2167 base_picture->type = GST_VAAPI_PICTURE_TYPE_SP;
2169 case GST_H264_SI_SLICE:
2170 base_picture->type = GST_VAAPI_PICTURE_TYPE_SI;
2174 /* Initialize picture structure */
2175 if (!slice_hdr->field_pic_flag)
2176 base_picture->structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
2178 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_INTERLACED);
2179 if (!slice_hdr->bottom_field_flag)
2180 base_picture->structure = GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD;
2182 base_picture->structure = GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD;
2184 picture->structure = base_picture->structure;
2186 /* Initialize reference flags */
2187 if (nalu->ref_idc) {
2188 GstH264DecRefPicMarking * const dec_ref_pic_marking =
2189 &slice_hdr->dec_ref_pic_marking;
2191 if (GST_VAAPI_PICTURE_IS_IDR(picture) &&
2192 dec_ref_pic_marking->long_term_reference_flag)
2193 GST_VAAPI_PICTURE_FLAG_SET(picture,
2194 GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE);
2196 GST_VAAPI_PICTURE_FLAG_SET(picture,
2197 GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE);
2200 init_picture_poc(decoder, picture, slice_hdr);
2201 init_picture_refs(decoder, picture, slice_hdr);
2205 /* 8.2.5.3 - Sliding window decoded reference picture marking process */
2207 exec_ref_pic_marking_sliding_window(GstVaapiDecoderH264 *decoder)
2209 GstVaapiDecoderH264Private * const priv = decoder->priv;
2210 GstH264PPS * const pps = priv->current_picture->pps;
2211 GstH264SPS * const sps = pps->sequence;
2212 GstVaapiPictureH264 *ref_picture;
2213 guint i, m, max_num_ref_frames;
2215 GST_DEBUG("reference picture marking process (sliding window)");
2217 if (!GST_VAAPI_PICTURE_IS_FIRST_FIELD(priv->current_picture))
2220 max_num_ref_frames = sps->num_ref_frames;
2221 if (max_num_ref_frames == 0)
2222 max_num_ref_frames = 1;
2223 if (!GST_VAAPI_PICTURE_IS_FRAME(priv->current_picture))
2224 max_num_ref_frames <<= 1;
2226 if (priv->short_ref_count + priv->long_ref_count < max_num_ref_frames)
2228 if (priv->short_ref_count < 1)
2231 for (m = 0, i = 1; i < priv->short_ref_count; i++) {
2232 GstVaapiPictureH264 * const picture = priv->short_ref[i];
2233 if (picture->frame_num_wrap < priv->short_ref[m]->frame_num_wrap)
2237 ref_picture = priv->short_ref[m];
2238 gst_vaapi_picture_h264_set_reference(ref_picture, 0, TRUE);
2239 ARRAY_REMOVE_INDEX(priv->short_ref, m);
2241 /* Both fields need to be marked as "unused for reference", so
2242 remove the other field from the short_ref[] list as well */
2243 if (!GST_VAAPI_PICTURE_IS_FRAME(priv->current_picture) && ref_picture->other_field) {
2244 for (i = 0; i < priv->short_ref_count; i++) {
2245 if (priv->short_ref[i] == ref_picture->other_field) {
2246 ARRAY_REMOVE_INDEX(priv->short_ref, i);
2254 static inline gint32
2255 get_picNumX(GstVaapiPictureH264 *picture, GstH264RefPicMarking *ref_pic_marking)
2259 if (GST_VAAPI_PICTURE_IS_FRAME(picture))
2260 pic_num = picture->frame_num_wrap;
2262 pic_num = 2 * picture->frame_num_wrap + 1;
2263 pic_num -= ref_pic_marking->difference_of_pic_nums_minus1 + 1;
2267 /* 8.2.5.4.1. Mark short-term reference picture as "unused for reference" */
2269 exec_ref_pic_marking_adaptive_mmco_1(
2270 GstVaapiDecoderH264 *decoder,
2271 GstVaapiPictureH264 *picture,
2272 GstH264RefPicMarking *ref_pic_marking
2275 GstVaapiDecoderH264Private * const priv = decoder->priv;
2278 picNumX = get_picNumX(picture, ref_pic_marking);
2279 i = find_short_term_reference(decoder, picNumX);
2283 gst_vaapi_picture_h264_set_reference(priv->short_ref[i], 0,
2284 GST_VAAPI_PICTURE_IS_FRAME(picture));
2285 ARRAY_REMOVE_INDEX(priv->short_ref, i);
2288 /* 8.2.5.4.2. Mark long-term reference picture as "unused for reference" */
2290 exec_ref_pic_marking_adaptive_mmco_2(
2291 GstVaapiDecoderH264 *decoder,
2292 GstVaapiPictureH264 *picture,
2293 GstH264RefPicMarking *ref_pic_marking
2296 GstVaapiDecoderH264Private * const priv = decoder->priv;
2299 i = find_long_term_reference(decoder, ref_pic_marking->long_term_pic_num);
2303 gst_vaapi_picture_h264_set_reference(priv->long_ref[i], 0,
2304 GST_VAAPI_PICTURE_IS_FRAME(picture));
2305 ARRAY_REMOVE_INDEX(priv->long_ref, i);
2308 /* 8.2.5.4.3. Assign LongTermFrameIdx to a short-term reference picture */
2310 exec_ref_pic_marking_adaptive_mmco_3(
2311 GstVaapiDecoderH264 *decoder,
2312 GstVaapiPictureH264 *picture,
2313 GstH264RefPicMarking *ref_pic_marking
2316 GstVaapiDecoderH264Private * const priv = decoder->priv;
2317 GstVaapiPictureH264 *ref_picture;
2320 for (i = 0; i < priv->long_ref_count; i++) {
2321 if (priv->long_ref[i]->long_term_frame_idx == ref_pic_marking->long_term_frame_idx)
2324 if (i != priv->long_ref_count) {
2325 gst_vaapi_picture_h264_set_reference(priv->long_ref[i], 0, TRUE);
2326 ARRAY_REMOVE_INDEX(priv->long_ref, i);
2329 picNumX = get_picNumX(picture, ref_pic_marking);
2330 i = find_short_term_reference(decoder, picNumX);
2334 ref_picture = priv->short_ref[i];
2335 ARRAY_REMOVE_INDEX(priv->short_ref, i);
2336 priv->long_ref[priv->long_ref_count++] = ref_picture;
2338 ref_picture->long_term_frame_idx = ref_pic_marking->long_term_frame_idx;
2339 gst_vaapi_picture_h264_set_reference(ref_picture,
2340 GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE,
2341 GST_VAAPI_PICTURE_IS_FRAME(picture));
2344 /* 8.2.5.4.4. Mark pictures with LongTermFramIdx > max_long_term_frame_idx
2345 * as "unused for reference" */
2347 exec_ref_pic_marking_adaptive_mmco_4(
2348 GstVaapiDecoderH264 *decoder,
2349 GstVaapiPictureH264 *picture,
2350 GstH264RefPicMarking *ref_pic_marking
2353 GstVaapiDecoderH264Private * const priv = decoder->priv;
2354 gint32 i, long_term_frame_idx;
2356 long_term_frame_idx = ref_pic_marking->max_long_term_frame_idx_plus1 - 1;
2358 for (i = 0; i < priv->long_ref_count; i++) {
2359 if (priv->long_ref[i]->long_term_frame_idx <= long_term_frame_idx)
2361 gst_vaapi_picture_h264_set_reference(priv->long_ref[i], 0, FALSE);
2362 ARRAY_REMOVE_INDEX(priv->long_ref, i);
2367 /* 8.2.5.4.5. Mark all reference pictures as "unused for reference" */
2369 exec_ref_pic_marking_adaptive_mmco_5(
2370 GstVaapiDecoderH264 *decoder,
2371 GstVaapiPictureH264 *picture,
2372 GstH264RefPicMarking *ref_pic_marking
2375 GstVaapiDecoderH264Private * const priv = decoder->priv;
2379 priv->prev_pic_has_mmco5 = TRUE;
2381 /* The picture shall be inferred to have had frame_num equal to 0 (7.4.3) */
2382 priv->frame_num = 0;
2383 priv->frame_num_offset = 0;
2384 picture->frame_num = 0;
2386 /* Update TopFieldOrderCnt and BottomFieldOrderCnt (8.2.1) */
2387 if (picture->structure != GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD)
2388 picture->field_poc[TOP_FIELD] -= picture->base.poc;
2389 if (picture->structure != GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD)
2390 picture->field_poc[BOTTOM_FIELD] -= picture->base.poc;
2391 picture->base.poc = 0;
2394 /* 8.2.5.4.6. Assign a long-term frame index to the current picture */
2396 exec_ref_pic_marking_adaptive_mmco_6(
2397 GstVaapiDecoderH264 *decoder,
2398 GstVaapiPictureH264 *picture,
2399 GstH264RefPicMarking *ref_pic_marking
2402 picture->long_term_frame_idx = ref_pic_marking->long_term_frame_idx;
2403 gst_vaapi_picture_h264_set_reference(picture,
2404 GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE, FALSE);
2407 /* 8.2.5.4. Adaptive memory control decoded reference picture marking process */
2409 exec_ref_pic_marking_adaptive(
2410 GstVaapiDecoderH264 *decoder,
2411 GstVaapiPictureH264 *picture,
2412 GstH264DecRefPicMarking *dec_ref_pic_marking
2417 GST_DEBUG("reference picture marking process (adaptive memory control)");
2419 typedef void (*exec_ref_pic_marking_adaptive_mmco_func)(
2420 GstVaapiDecoderH264 *decoder,
2421 GstVaapiPictureH264 *picture,
2422 GstH264RefPicMarking *ref_pic_marking
2425 static const exec_ref_pic_marking_adaptive_mmco_func mmco_funcs[] = {
2427 exec_ref_pic_marking_adaptive_mmco_1,
2428 exec_ref_pic_marking_adaptive_mmco_2,
2429 exec_ref_pic_marking_adaptive_mmco_3,
2430 exec_ref_pic_marking_adaptive_mmco_4,
2431 exec_ref_pic_marking_adaptive_mmco_5,
2432 exec_ref_pic_marking_adaptive_mmco_6,
2435 for (i = 0; i < dec_ref_pic_marking->n_ref_pic_marking; i++) {
2436 GstH264RefPicMarking * const ref_pic_marking =
2437 &dec_ref_pic_marking->ref_pic_marking[i];
2439 const guint mmco = ref_pic_marking->memory_management_control_operation;
2440 if (mmco < G_N_ELEMENTS(mmco_funcs) && mmco_funcs[mmco])
2441 mmco_funcs[mmco](decoder, picture, ref_pic_marking);
2443 GST_ERROR("unhandled MMCO %u", mmco);
2450 /* 8.2.5 - Execute reference picture marking process */
2452 exec_ref_pic_marking(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
2454 GstVaapiDecoderH264Private * const priv = decoder->priv;
2456 priv->prev_pic_has_mmco5 = FALSE;
2457 priv->prev_pic_structure = picture->structure;
2459 if (!GST_VAAPI_PICTURE_IS_REFERENCE(picture))
2462 if (!GST_VAAPI_PICTURE_IS_IDR(picture)) {
2463 GstVaapiSliceH264 * const slice =
2464 gst_vaapi_picture_h264_get_last_slice(picture);
2465 GstH264DecRefPicMarking * const dec_ref_pic_marking =
2466 &slice->slice_hdr.dec_ref_pic_marking;
2467 if (dec_ref_pic_marking->adaptive_ref_pic_marking_mode_flag) {
2468 if (!exec_ref_pic_marking_adaptive(decoder, picture, dec_ref_pic_marking))
2472 if (!exec_ref_pic_marking_sliding_window(decoder))
2480 vaapi_init_picture(VAPictureH264 *pic)
2482 pic->picture_id = VA_INVALID_ID;
2484 pic->flags = VA_PICTURE_H264_INVALID;
2485 pic->TopFieldOrderCnt = 0;
2486 pic->BottomFieldOrderCnt = 0;
2490 vaapi_fill_picture(VAPictureH264 *pic, GstVaapiPictureH264 *picture,
2491 guint picture_structure)
2493 if (!picture_structure)
2494 picture_structure = picture->structure;
2496 pic->picture_id = picture->base.surface_id;
2499 if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(picture)) {
2500 pic->flags |= VA_PICTURE_H264_LONG_TERM_REFERENCE;
2501 pic->frame_idx = picture->long_term_frame_idx;
2504 if (GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(picture))
2505 pic->flags |= VA_PICTURE_H264_SHORT_TERM_REFERENCE;
2506 pic->frame_idx = picture->frame_num;
2509 switch (picture_structure) {
2510 case GST_VAAPI_PICTURE_STRUCTURE_FRAME:
2511 pic->TopFieldOrderCnt = picture->field_poc[TOP_FIELD];
2512 pic->BottomFieldOrderCnt = picture->field_poc[BOTTOM_FIELD];
2514 case GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD:
2515 pic->flags |= VA_PICTURE_H264_TOP_FIELD;
2516 pic->TopFieldOrderCnt = picture->field_poc[TOP_FIELD];
2517 pic->BottomFieldOrderCnt = 0;
2519 case GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD:
2520 pic->flags |= VA_PICTURE_H264_BOTTOM_FIELD;
2521 pic->BottomFieldOrderCnt = picture->field_poc[BOTTOM_FIELD];
2522 pic->TopFieldOrderCnt = 0;
2529 GstVaapiDecoderH264 *decoder,
2530 GstVaapiPictureH264 *picture,
2531 GstH264SliceHdr *slice_hdr,
2532 GstH264NalUnit *nalu
2535 GstVaapiDecoderH264Private * const priv = decoder->priv;
2536 GstVaapiPicture * const base_picture = &picture->base;
2537 GstH264PPS * const pps = picture->pps;
2538 GstH264SPS * const sps = pps->sequence;
2539 VAPictureParameterBufferH264 * const pic_param = base_picture->param;
2542 /* Fill in VAPictureParameterBufferH264 */
2543 vaapi_fill_picture(&pic_param->CurrPic, picture, 0);
2545 for (i = 0, n = 0; i < priv->dpb_count; i++) {
2546 GstVaapiFrameStore * const fs = priv->dpb[i];
2547 if (gst_vaapi_frame_store_has_reference(fs))
2548 vaapi_fill_picture(&pic_param->ReferenceFrames[n++],
2549 fs->buffers[0], fs->structure);
2551 for (; n < G_N_ELEMENTS(pic_param->ReferenceFrames); n++)
2552 vaapi_init_picture(&pic_param->ReferenceFrames[n]);
2554 #define COPY_FIELD(s, f) \
2555 pic_param->f = (s)->f
2557 #define COPY_BFM(a, s, f) \
2558 pic_param->a.bits.f = (s)->f
2560 pic_param->picture_width_in_mbs_minus1 = priv->mb_width - 1;
2561 pic_param->picture_height_in_mbs_minus1 = priv->mb_height - 1;
2562 pic_param->frame_num = priv->frame_num;
2564 COPY_FIELD(sps, bit_depth_luma_minus8);
2565 COPY_FIELD(sps, bit_depth_chroma_minus8);
2566 COPY_FIELD(sps, num_ref_frames);
2567 COPY_FIELD(pps, num_slice_groups_minus1);
2568 COPY_FIELD(pps, slice_group_map_type);
2569 COPY_FIELD(pps, slice_group_change_rate_minus1);
2570 COPY_FIELD(pps, pic_init_qp_minus26);
2571 COPY_FIELD(pps, pic_init_qs_minus26);
2572 COPY_FIELD(pps, chroma_qp_index_offset);
2573 COPY_FIELD(pps, second_chroma_qp_index_offset);
2575 pic_param->seq_fields.value = 0; /* reset all bits */
2576 pic_param->seq_fields.bits.residual_colour_transform_flag = sps->separate_colour_plane_flag;
2577 pic_param->seq_fields.bits.MinLumaBiPredSize8x8 = sps->level_idc >= 31; /* A.3.3.2 */
2579 COPY_BFM(seq_fields, sps, chroma_format_idc);
2580 COPY_BFM(seq_fields, sps, gaps_in_frame_num_value_allowed_flag);
2581 COPY_BFM(seq_fields, sps, frame_mbs_only_flag);
2582 COPY_BFM(seq_fields, sps, mb_adaptive_frame_field_flag);
2583 COPY_BFM(seq_fields, sps, direct_8x8_inference_flag);
2584 COPY_BFM(seq_fields, sps, log2_max_frame_num_minus4);
2585 COPY_BFM(seq_fields, sps, pic_order_cnt_type);
2586 COPY_BFM(seq_fields, sps, log2_max_pic_order_cnt_lsb_minus4);
2587 COPY_BFM(seq_fields, sps, delta_pic_order_always_zero_flag);
2589 pic_param->pic_fields.value = 0; /* reset all bits */
2590 pic_param->pic_fields.bits.field_pic_flag = slice_hdr->field_pic_flag;
2591 pic_param->pic_fields.bits.reference_pic_flag = GST_VAAPI_PICTURE_IS_REFERENCE(picture);
2593 COPY_BFM(pic_fields, pps, entropy_coding_mode_flag);
2594 COPY_BFM(pic_fields, pps, weighted_pred_flag);
2595 COPY_BFM(pic_fields, pps, weighted_bipred_idc);
2596 COPY_BFM(pic_fields, pps, transform_8x8_mode_flag);
2597 COPY_BFM(pic_fields, pps, constrained_intra_pred_flag);
2598 COPY_BFM(pic_fields, pps, pic_order_present_flag);
2599 COPY_BFM(pic_fields, pps, deblocking_filter_control_present_flag);
2600 COPY_BFM(pic_fields, pps, redundant_pic_cnt_present_flag);
2604 /* Detection of the first VCL NAL unit of a primary coded picture (7.4.1.2.4) */
2607 GstVaapiDecoderH264 *decoder,
2608 GstH264NalUnit *nalu,
2609 GstH264SliceHdr *slice_hdr
2612 GstVaapiDecoderH264Private * const priv = decoder->priv;
2613 GstH264PPS * const pps = slice_hdr->pps;
2614 GstH264SPS * const sps = pps->sequence;
2615 GstVaapiSliceH264 *slice;
2616 GstH264SliceHdr *prev_slice_hdr;
2618 if (!priv->current_picture)
2621 slice = gst_vaapi_picture_h264_get_last_slice(priv->current_picture);
2624 prev_slice_hdr = &slice->slice_hdr;
2626 #define CHECK_EXPR(expr, field_name) do { \
2628 GST_DEBUG(field_name " differs in value"); \
2633 #define CHECK_VALUE(new_slice_hdr, old_slice_hdr, field) \
2634 CHECK_EXPR(((new_slice_hdr)->field == (old_slice_hdr)->field), #field)
2636 /* frame_num differs in value, regardless of inferred values to 0 */
2637 CHECK_VALUE(slice_hdr, prev_slice_hdr, frame_num);
2639 /* pic_parameter_set_id differs in value */
2640 CHECK_VALUE(slice_hdr, prev_slice_hdr, pps);
2642 /* field_pic_flag differs in value */
2643 CHECK_VALUE(slice_hdr, prev_slice_hdr, field_pic_flag);
2645 /* bottom_field_flag is present in both and differs in value */
2646 if (slice_hdr->field_pic_flag && prev_slice_hdr->field_pic_flag)
2647 CHECK_VALUE(slice_hdr, prev_slice_hdr, bottom_field_flag);
2649 /* nal_ref_idc differs in value with one of the nal_ref_idc values is 0 */
2650 CHECK_EXPR(((GST_VAAPI_PICTURE_IS_REFERENCE(priv->current_picture) ^
2651 (nalu->ref_idc != 0)) == 0), "nal_ref_idc");
2653 /* POC type is 0 for both and either pic_order_cnt_lsb differs in
2654 value or delta_pic_order_cnt_bottom differs in value */
2655 if (sps->pic_order_cnt_type == 0) {
2656 CHECK_VALUE(slice_hdr, prev_slice_hdr, pic_order_cnt_lsb);
2657 if (pps->pic_order_present_flag && !slice_hdr->field_pic_flag)
2658 CHECK_VALUE(slice_hdr, prev_slice_hdr, delta_pic_order_cnt_bottom);
2661 /* POC type is 1 for both and either delta_pic_order_cnt[0]
2662 differs in value or delta_pic_order_cnt[1] differs in value */
2663 else if (sps->pic_order_cnt_type == 1) {
2664 CHECK_VALUE(slice_hdr, prev_slice_hdr, delta_pic_order_cnt[0]);
2665 CHECK_VALUE(slice_hdr, prev_slice_hdr, delta_pic_order_cnt[1]);
2668 /* IdrPicFlag differs in value */
2669 CHECK_EXPR(((GST_VAAPI_PICTURE_IS_IDR(priv->current_picture) ^
2670 (nalu->type == GST_H264_NAL_SLICE_IDR)) == 0), "IdrPicFlag");
2672 /* IdrPicFlag is equal to 1 for both and idr_pic_id differs in value */
2673 if (GST_VAAPI_PICTURE_IS_IDR(priv->current_picture))
2674 CHECK_VALUE(slice_hdr, prev_slice_hdr, idr_pic_id);
2681 static GstVaapiDecoderStatus
2682 decode_picture(GstVaapiDecoderH264 *decoder, GstH264NalUnit *nalu, GstH264SliceHdr *slice_hdr)
2684 GstVaapiDecoderH264Private * const priv = decoder->priv;
2685 GstVaapiPictureH264 *picture;
2686 GstVaapiDecoderStatus status;
2687 GstH264PPS * const pps = slice_hdr->pps;
2688 GstH264SPS * const sps = pps->sequence;
2690 status = decode_current_picture(decoder);
2691 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
2694 status = ensure_context(decoder, sps);
2695 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
2698 if (priv->current_picture) {
2699 /* Re-use current picture where the first field was decoded */
2700 picture = gst_vaapi_picture_h264_new_field(priv->current_picture);
2702 GST_ERROR("failed to allocate field picture");
2703 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
2707 /* Create new picture */
2708 picture = gst_vaapi_picture_h264_new(decoder);
2710 GST_ERROR("failed to allocate picture");
2711 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
2714 gst_vaapi_picture_replace(&priv->current_picture, picture);
2715 gst_vaapi_picture_unref(picture);
2719 status = ensure_quant_matrix(decoder, picture);
2720 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
2721 GST_ERROR("failed to reset quantizer matrix");
2725 if (!init_picture(decoder, picture, slice_hdr, nalu))
2726 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
2727 if (!fill_picture(decoder, picture, slice_hdr, nalu))
2728 return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
2729 return GST_VAAPI_DECODER_STATUS_SUCCESS;
2733 get_slice_data_bit_offset(GstH264SliceHdr *slice_hdr, GstH264NalUnit *nalu)
2737 epb_count = slice_hdr->n_emulation_prevention_bytes;
2738 return 8 /* nal_unit_type */ + slice_hdr->header_size - epb_count * 8;
2742 fill_pred_weight_table(GstVaapiDecoderH264 *decoder, GstVaapiSliceH264 *slice)
2744 GstH264SliceHdr * const slice_hdr = &slice->slice_hdr;
2745 GstH264PPS * const pps = slice_hdr->pps;
2746 GstH264SPS * const sps = pps->sequence;
2747 GstH264PredWeightTable * const w = &slice_hdr->pred_weight_table;
2748 VASliceParameterBufferH264 * const slice_param = slice->base.param;
2749 guint num_weight_tables = 0;
2752 if (pps->weighted_pred_flag &&
2753 (GST_H264_IS_P_SLICE(slice_hdr) || GST_H264_IS_SP_SLICE(slice_hdr)))
2754 num_weight_tables = 1;
2755 else if (pps->weighted_bipred_idc == 1 && GST_H264_IS_B_SLICE(slice_hdr))
2756 num_weight_tables = 2;
2758 num_weight_tables = 0;
2760 slice_param->luma_log2_weight_denom = w->luma_log2_weight_denom;
2761 slice_param->chroma_log2_weight_denom = w->chroma_log2_weight_denom;
2762 slice_param->luma_weight_l0_flag = 0;
2763 slice_param->chroma_weight_l0_flag = 0;
2764 slice_param->luma_weight_l1_flag = 0;
2765 slice_param->chroma_weight_l1_flag = 0;
2767 if (num_weight_tables < 1)
2770 slice_param->luma_weight_l0_flag = 1;
2771 for (i = 0; i <= slice_param->num_ref_idx_l0_active_minus1; i++) {
2772 slice_param->luma_weight_l0[i] = w->luma_weight_l0[i];
2773 slice_param->luma_offset_l0[i] = w->luma_offset_l0[i];
2776 slice_param->chroma_weight_l0_flag = sps->chroma_array_type != 0;
2777 if (slice_param->chroma_weight_l0_flag) {
2778 for (i = 0; i <= slice_param->num_ref_idx_l0_active_minus1; i++) {
2779 for (j = 0; j < 2; j++) {
2780 slice_param->chroma_weight_l0[i][j] = w->chroma_weight_l0[i][j];
2781 slice_param->chroma_offset_l0[i][j] = w->chroma_offset_l0[i][j];
2786 if (num_weight_tables < 2)
2789 slice_param->luma_weight_l1_flag = 1;
2790 for (i = 0; i <= slice_param->num_ref_idx_l1_active_minus1; i++) {
2791 slice_param->luma_weight_l1[i] = w->luma_weight_l1[i];
2792 slice_param->luma_offset_l1[i] = w->luma_offset_l1[i];
2795 slice_param->chroma_weight_l1_flag = sps->chroma_array_type != 0;
2796 if (slice_param->chroma_weight_l1_flag) {
2797 for (i = 0; i <= slice_param->num_ref_idx_l1_active_minus1; i++) {
2798 for (j = 0; j < 2; j++) {
2799 slice_param->chroma_weight_l1[i][j] = w->chroma_weight_l1[i][j];
2800 slice_param->chroma_offset_l1[i][j] = w->chroma_offset_l1[i][j];
2808 fill_RefPicList(GstVaapiDecoderH264 *decoder, GstVaapiSliceH264 *slice)
2810 GstVaapiDecoderH264Private * const priv = decoder->priv;
2811 GstH264SliceHdr * const slice_hdr = &slice->slice_hdr;
2812 VASliceParameterBufferH264 * const slice_param = slice->base.param;
2813 guint i, num_ref_lists = 0;
2815 slice_param->num_ref_idx_l0_active_minus1 = 0;
2816 slice_param->num_ref_idx_l1_active_minus1 = 0;
2818 if (GST_H264_IS_B_SLICE(slice_hdr))
2820 else if (GST_H264_IS_I_SLICE(slice_hdr))
2825 if (num_ref_lists < 1)
2828 slice_param->num_ref_idx_l0_active_minus1 =
2829 slice_hdr->num_ref_idx_l0_active_minus1;
2831 for (i = 0; i < priv->RefPicList0_count && priv->RefPicList0[i]; i++)
2832 vaapi_fill_picture(&slice_param->RefPicList0[i], priv->RefPicList0[i], 0);
2833 for (; i <= slice_param->num_ref_idx_l0_active_minus1; i++)
2834 vaapi_init_picture(&slice_param->RefPicList0[i]);
2836 if (num_ref_lists < 2)
2839 slice_param->num_ref_idx_l1_active_minus1 =
2840 slice_hdr->num_ref_idx_l1_active_minus1;
2842 for (i = 0; i < priv->RefPicList1_count && priv->RefPicList1[i]; i++)
2843 vaapi_fill_picture(&slice_param->RefPicList1[i], priv->RefPicList1[i], 0);
2844 for (; i <= slice_param->num_ref_idx_l1_active_minus1; i++)
2845 vaapi_init_picture(&slice_param->RefPicList1[i]);
2851 GstVaapiDecoderH264 *decoder,
2852 GstVaapiSliceH264 *slice,
2853 GstH264NalUnit *nalu
2856 GstH264SliceHdr * const slice_hdr = &slice->slice_hdr;
2857 VASliceParameterBufferH264 * const slice_param = slice->base.param;
2859 /* Fill in VASliceParameterBufferH264 */
2860 slice_param->slice_data_bit_offset = get_slice_data_bit_offset(slice_hdr, nalu);
2861 slice_param->first_mb_in_slice = slice_hdr->first_mb_in_slice;
2862 slice_param->slice_type = slice_hdr->type % 5;
2863 slice_param->direct_spatial_mv_pred_flag = slice_hdr->direct_spatial_mv_pred_flag;
2864 slice_param->cabac_init_idc = slice_hdr->cabac_init_idc;
2865 slice_param->slice_qp_delta = slice_hdr->slice_qp_delta;
2866 slice_param->disable_deblocking_filter_idc = slice_hdr->disable_deblocking_filter_idc;
2867 slice_param->slice_alpha_c0_offset_div2 = slice_hdr->slice_alpha_c0_offset_div2;
2868 slice_param->slice_beta_offset_div2 = slice_hdr->slice_beta_offset_div2;
2870 if (!fill_RefPicList(decoder, slice))
2872 if (!fill_pred_weight_table(decoder, slice))
2877 static GstVaapiDecoderStatus
2878 decode_slice(GstVaapiDecoderH264 *decoder, GstH264NalUnit *nalu)
2880 GstVaapiDecoderH264Private * const priv = decoder->priv;
2881 GstVaapiDecoderStatus status;
2882 GstVaapiPictureH264 *picture;
2883 GstVaapiSliceH264 *slice = NULL;
2884 GstH264SliceHdr *slice_hdr;
2885 GstH264ParserResult result;
2886 gboolean is_first_slice = !priv->has_context;
2888 GST_DEBUG("slice (%u bytes)", nalu->size);
2890 if (is_first_slice) {
2891 slice_hdr = &priv->temp_slice_hdr;
2892 memset(slice_hdr, 0, sizeof(*slice_hdr));
2893 result = gst_h264_parser_parse_slice_hdr(priv->parser, nalu,
2894 slice_hdr, TRUE, TRUE);
2895 if (result != GST_H264_PARSER_OK) {
2896 status = get_status(result);
2900 status = ensure_context(decoder, slice_hdr->pps->sequence);
2901 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
2905 slice = gst_vaapi_slice_h264_new(
2907 nalu->data + nalu->offset,
2911 GST_ERROR("failed to allocate slice");
2912 return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
2915 slice_hdr = &slice->slice_hdr;
2917 memcpy(slice_hdr, &priv->temp_slice_hdr, sizeof(*slice_hdr));
2919 memset(slice_hdr, 0, sizeof(*slice_hdr));
2920 result = gst_h264_parser_parse_slice_hdr(priv->parser, nalu,
2921 slice_hdr, TRUE, TRUE);
2922 if (result != GST_H264_PARSER_OK) {
2923 status = get_status(result);
2928 if (is_new_picture(decoder, nalu, slice_hdr)) {
2929 status = decode_picture(decoder, nalu, slice_hdr);
2930 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
2933 picture = priv->current_picture;
2935 if (!fill_slice(decoder, slice, nalu)) {
2936 status = GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
2939 gst_vaapi_picture_add_slice(
2940 GST_VAAPI_PICTURE_CAST(picture),
2941 GST_VAAPI_SLICE_CAST(slice)
2943 return GST_VAAPI_DECODER_STATUS_SUCCESS;
2947 gst_mini_object_unref(GST_MINI_OBJECT(slice));
2952 scan_for_start_code(GstAdapter *adapter, guint ofs, guint size, guint32 *scp)
2954 return (gint)gst_adapter_masked_scan_uint32_peek(adapter,
2955 0xffffff00, 0x00000100,
2960 static GstVaapiDecoderStatus
2961 decode_nalu(GstVaapiDecoderH264 *decoder, GstH264NalUnit *nalu)
2963 GstVaapiDecoderH264Private * const priv = decoder->priv;
2964 GstVaapiDecoderStatus status;
2966 switch (nalu->type) {
2967 case GST_H264_NAL_SLICE_IDR:
2968 /* fall-through. IDR specifics are handled in init_picture() */
2969 case GST_H264_NAL_SLICE:
2970 if (!priv->got_sps || !priv->got_pps)
2971 return GST_VAAPI_DECODER_STATUS_SUCCESS;
2972 status = decode_slice(decoder, nalu);
2974 case GST_H264_NAL_SPS:
2975 status = decode_sps(decoder, nalu);
2977 case GST_H264_NAL_PPS:
2978 status = decode_pps(decoder, nalu);
2980 case GST_H264_NAL_SEI:
2981 status = decode_sei(decoder, nalu);
2983 case GST_H264_NAL_SEQ_END:
2984 status = decode_sequence_end(decoder);
2986 case GST_H264_NAL_AU_DELIMITER:
2987 /* skip all Access Unit NALs */
2988 status = GST_VAAPI_DECODER_STATUS_SUCCESS;
2990 case GST_H264_NAL_FILLER_DATA:
2991 /* skip all Filler Data NALs */
2992 status = GST_VAAPI_DECODER_STATUS_SUCCESS;
2995 GST_WARNING("unsupported NAL unit type %d", nalu->type);
2996 status = GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
3002 static GstVaapiDecoderStatus
3003 decode_buffer(GstVaapiDecoderH264 *decoder, GstBuffer *buffer)
3005 GstVaapiDecoderH264Private * const priv = decoder->priv;
3006 GstVaapiDecoderStatus status;
3007 GstH264ParserResult result;
3008 GstH264NalUnit nalu;
3011 guint i, buf_size, nalu_size, size;
3015 buf = GST_BUFFER_DATA(buffer);
3016 buf_size = GST_BUFFER_SIZE(buffer);
3017 is_eos = GST_BUFFER_IS_EOS(buffer);
3018 if (buf && buf_size > 0)
3019 gst_adapter_push(priv->adapter, gst_buffer_ref(buffer));
3021 size = gst_adapter_available(priv->adapter);
3024 status = GST_VAAPI_DECODER_STATUS_SUCCESS;
3028 status = gst_vaapi_decoder_check_status(GST_VAAPI_DECODER(decoder));
3029 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
3032 status = GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
3034 if (size < priv->nal_length_size)
3036 buf = gst_adapter_peek(priv->adapter, priv->nal_length_size);
3039 for (i = 0; i < priv->nal_length_size; i++)
3040 nalu_size = (nalu_size << 8) | buf[i];
3042 buf_size = priv->nal_length_size + nalu_size;
3043 if (size < buf_size)
3045 buffer = gst_adapter_take_buffer(priv->adapter, buf_size);
3048 buf = GST_BUFFER_DATA(buffer);
3049 buf_size = GST_BUFFER_SIZE(buffer);
3051 result = gst_h264_parser_identify_nalu_avc(
3053 buf, 0, buf_size, priv->nal_length_size,
3060 ofs = scan_for_start_code(priv->adapter, 0, size, &start_code);
3063 gst_adapter_flush(priv->adapter, ofs);
3066 ofs = G_UNLIKELY(size < 8) ? -1 :
3067 scan_for_start_code(priv->adapter, 4, size - 4, NULL);
3069 // Assume the whole NAL unit is present if end-of-stream
3074 buffer = gst_adapter_take_buffer(priv->adapter, ofs);
3077 buf = GST_BUFFER_DATA(buffer);
3078 buf_size = GST_BUFFER_SIZE(buffer);
3080 result = gst_h264_parser_identify_nalu_unchecked(
3086 status = get_status(result);
3087 if (status == GST_VAAPI_DECODER_STATUS_SUCCESS)
3088 status = decode_nalu(decoder, &nalu);
3089 gst_buffer_unref(buffer);
3090 } while (status == GST_VAAPI_DECODER_STATUS_SUCCESS);
3092 if (is_eos && (status == GST_VAAPI_DECODER_STATUS_SUCCESS ||
3093 status == GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA))
3094 status = decode_sequence_end(decoder);
3098 static GstVaapiDecoderStatus
3099 decode_codec_data(GstVaapiDecoderH264 *decoder, GstBuffer *buffer)
3101 GstVaapiDecoderH264Private * const priv = decoder->priv;
3102 GstVaapiDecoderStatus status;
3103 GstH264NalUnit nalu;
3104 GstH264ParserResult result;
3107 guint i, ofs, num_sps, num_pps;
3109 buf = GST_BUFFER_DATA(buffer);
3110 buf_size = GST_BUFFER_SIZE(buffer);
3111 if (!buf || buf_size == 0)
3112 return GST_VAAPI_DECODER_STATUS_SUCCESS;
3115 return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
3118 GST_ERROR("failed to decode codec-data, not in avcC format");
3119 return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
3122 priv->nal_length_size = (buf[4] & 0x03) + 1;
3124 num_sps = buf[5] & 0x1f;
3127 for (i = 0; i < num_sps; i++) {
3128 result = gst_h264_parser_identify_nalu_avc(
3130 buf, ofs, buf_size, 2,
3133 if (result != GST_H264_PARSER_OK)
3134 return get_status(result);
3136 status = decode_sps(decoder, &nalu);
3137 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
3139 ofs = nalu.offset + nalu.size;
3145 for (i = 0; i < num_pps; i++) {
3146 result = gst_h264_parser_identify_nalu_avc(
3148 buf, ofs, buf_size, 2,
3151 if (result != GST_H264_PARSER_OK)
3152 return get_status(result);
3154 status = decode_pps(decoder, &nalu);
3155 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
3157 ofs = nalu.offset + nalu.size;
3160 priv->is_avc = TRUE;
3164 GstVaapiDecoderStatus
3165 gst_vaapi_decoder_h264_decode(GstVaapiDecoder *base, GstBuffer *buffer)
3167 GstVaapiDecoderH264 * const decoder = GST_VAAPI_DECODER_H264(base);
3168 GstVaapiDecoderH264Private * const priv = decoder->priv;
3169 GstVaapiDecoderStatus status;
3170 GstBuffer *codec_data;
3172 g_return_val_if_fail(priv->is_constructed,
3173 GST_VAAPI_DECODER_STATUS_ERROR_INIT_FAILED);
3175 if (!priv->is_opened) {
3176 priv->is_opened = gst_vaapi_decoder_h264_open(decoder, buffer);
3177 if (!priv->is_opened)
3178 return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC;
3180 codec_data = GST_VAAPI_DECODER_CODEC_DATA(decoder);
3182 status = decode_codec_data(decoder, codec_data);
3183 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
3187 return decode_buffer(decoder, buffer);
3191 gst_vaapi_decoder_h264_finalize(GObject *object)
3193 GstVaapiDecoderH264 * const decoder = GST_VAAPI_DECODER_H264(object);
3195 gst_vaapi_decoder_h264_destroy(decoder);
3197 G_OBJECT_CLASS(gst_vaapi_decoder_h264_parent_class)->finalize(object);
3201 gst_vaapi_decoder_h264_constructed(GObject *object)
3203 GstVaapiDecoderH264 * const decoder = GST_VAAPI_DECODER_H264(object);
3204 GstVaapiDecoderH264Private * const priv = decoder->priv;
3205 GObjectClass *parent_class;
3207 parent_class = G_OBJECT_CLASS(gst_vaapi_decoder_h264_parent_class);
3208 if (parent_class->constructed)
3209 parent_class->constructed(object);
3211 priv->is_constructed = gst_vaapi_decoder_h264_create(decoder);
3215 gst_vaapi_decoder_h264_class_init(GstVaapiDecoderH264Class *klass)
3217 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
3218 GstVaapiDecoderClass * const decoder_class = GST_VAAPI_DECODER_CLASS(klass);
3220 g_type_class_add_private(klass, sizeof(GstVaapiDecoderH264Private));
3222 object_class->finalize = gst_vaapi_decoder_h264_finalize;
3223 object_class->constructed = gst_vaapi_decoder_h264_constructed;
3225 decoder_class->decode = gst_vaapi_decoder_h264_decode;
3226 decoder_class->clear_buffer = gst_vaapi_decoder_h264_clear_buffer;
3230 gst_vaapi_decoder_h264_init(GstVaapiDecoderH264 *decoder)
3232 GstVaapiDecoderH264Private *priv;
3234 priv = GST_VAAPI_DECODER_H264_GET_PRIVATE(decoder);
3235 decoder->priv = priv;
3236 priv->parser = NULL;
3237 priv->current_picture = NULL;
3238 priv->dpb_count = 0;
3240 priv->profile = GST_VAAPI_PROFILE_UNKNOWN;
3241 priv->entrypoint = GST_VAAPI_ENTRYPOINT_VLD;
3242 priv->chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
3243 priv->short_ref_count = 0;
3244 priv->long_ref_count = 0;
3245 priv->RefPicList0_count = 0;
3246 priv->RefPicList1_count = 0;
3247 priv->nal_length_size = 0;
3248 priv->adapter = NULL;
3249 priv->field_poc[0] = 0;
3250 priv->field_poc[1] = 0;
3253 priv->prev_poc_msb = 0;
3254 priv->prev_poc_lsb = 0;
3255 priv->frame_num_offset = 0;
3256 priv->frame_num = 0;
3257 priv->prev_frame_num = 0;
3258 priv->prev_pic_has_mmco5 = FALSE;
3259 priv->prev_pic_structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
3260 priv->is_constructed = FALSE;
3261 priv->is_opened = FALSE;
3262 priv->is_avc = FALSE;
3263 priv->has_context = FALSE;
3264 priv->progressive_sequence = TRUE;
3266 memset(priv->dpb, 0, sizeof(priv->dpb));
3267 memset(priv->short_ref, 0, sizeof(priv->short_ref));
3268 memset(priv->long_ref, 0, sizeof(priv->long_ref));
3269 memset(priv->RefPicList0, 0, sizeof(priv->RefPicList0));
3270 memset(priv->RefPicList1, 0, sizeof(priv->RefPicList1));
3274 * gst_vaapi_decoder_h264_new:
3275 * @display: a #GstVaapiDisplay
3276 * @caps: a #GstCaps holding codec information
3278 * Creates a new #GstVaapiDecoder for MPEG-2 decoding. The @caps can
3279 * hold extra information like codec-data and pictured coded size.
3281 * Return value: the newly allocated #GstVaapiDecoder object
3284 gst_vaapi_decoder_h264_new(GstVaapiDisplay *display, GstCaps *caps)
3286 GstVaapiDecoderH264 *decoder;
3288 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
3289 g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
3291 decoder = g_object_new(
3292 GST_VAAPI_TYPE_DECODER_H264,
3297 if (!decoder->priv->is_constructed) {
3298 g_object_unref(decoder);
3301 return GST_VAAPI_DECODER_CAST(decoder);