348
443
#define GST_VAAPI_DECODER_H264_CAST(decoder) \
349
444
((GstVaapiDecoderH264 *)(decoder))
447
GST_H264_VIDEO_STATE_GOT_SPS = 1 << 0,
448
GST_H264_VIDEO_STATE_GOT_PPS = 1 << 1,
449
GST_H264_VIDEO_STATE_GOT_SLICE = 1 << 2,
451
GST_H264_VIDEO_STATE_VALID_PICTURE_HEADERS = (
452
GST_H264_VIDEO_STATE_GOT_SPS |
453
GST_H264_VIDEO_STATE_GOT_PPS),
454
GST_H264_VIDEO_STATE_VALID_PICTURE = (
455
GST_H264_VIDEO_STATE_VALID_PICTURE_HEADERS |
456
GST_H264_VIDEO_STATE_GOT_SLICE)
351
459
struct _GstVaapiDecoderH264Private {
352
460
GstH264NalParser *parser;
463
GstVaapiStreamAlignH264 stream_alignment;
353
464
GstVaapiPictureH264 *current_picture;
465
GstVaapiParserInfoH264 *sps[GST_H264_MAX_SPS_COUNT];
466
GstVaapiParserInfoH264 *active_sps;
467
GstVaapiParserInfoH264 *pps[GST_H264_MAX_PPS_COUNT];
468
GstVaapiParserInfoH264 *active_pps;
469
GstVaapiParserInfoH264 *prev_pi;
354
470
GstVaapiParserInfoH264 *prev_slice_pi;
355
GstVaapiFrameStore *prev_frame;
356
GstVaapiFrameStore *dpb[16];
471
GstVaapiFrameStore **prev_frames;
472
guint prev_frames_alloc;
473
GstVaapiFrameStore **dpb;
359
478
GstVaapiProfile profile;
360
479
GstVaapiEntrypoint entrypoint;
361
480
GstVaapiChromaType chroma_type;
481
GPtrArray *inter_views;
362
482
GstVaapiPictureH264 *short_ref[32];
363
483
guint short_ref_count;
364
484
GstVaapiPictureH264 *long_ref[32];
413
531
exec_ref_pic_marking(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture);
534
is_inter_view_reference_for_next_pictures(GstVaapiDecoderH264 *decoder,
535
GstVaapiPictureH264 *picture);
537
static inline gboolean
538
is_inter_view_reference_for_next_frames(GstVaapiDecoderH264 *decoder,
539
GstVaapiFrameStore *fs)
541
return is_inter_view_reference_for_next_pictures(decoder, fs->buffers[0]);
544
/* Determines if the supplied profile is one of the MVC set */
546
is_mvc_profile(GstH264Profile profile)
548
return profile == GST_H264_PROFILE_MULTIVIEW_HIGH ||
549
profile == GST_H264_PROFILE_STEREO_HIGH;
552
/* Determines the view_id from the supplied NAL unit */
554
get_view_id(GstH264NalUnit *nalu)
556
return GST_H264_IS_MVC_NALU(nalu) ? nalu->extension.mvc.view_id : 0;
559
/* Determines the view order index (VOIdx) from the supplied view_id */
561
get_view_order_index(GstH264SPS *sps, guint16 view_id)
563
GstH264SPSExtMVC *mvc;
566
if (!sps || sps->extension_type != GST_H264_NAL_EXTENSION_MVC)
569
mvc = &sps->extension.mvc;
570
for (i = 0; i <= mvc->num_views_minus1; i++) {
571
if (mvc->view[i].view_id == view_id)
574
GST_ERROR("failed to find VOIdx from view_id (%d)", view_id);
578
/* Determines NumViews */
580
get_num_views(GstH264SPS *sps)
582
return 1 + (sps->extension_type == GST_H264_NAL_EXTENSION_MVC ?
583
sps->extension.mvc.num_views_minus1 : 0);
415
586
/* Get number of reference frames to use */
417
588
get_max_dec_frame_buffering(GstH264SPS *sps)
419
guint max_dec_frame_buffering, MaxDpbMbs, PicSizeMbs;
590
guint num_views, max_dpb_frames;
591
guint max_dec_frame_buffering, PicSizeMbs;
592
GstVaapiLevelH264 level;
593
const GstVaapiH264LevelLimits *level_limits;
421
595
/* Table A-1 - Level limits */
422
switch (sps->level_idc) {
423
case 10: MaxDpbMbs = 396; break;
424
case 11: MaxDpbMbs = 900; break;
425
case 12: MaxDpbMbs = 2376; break;
426
case 13: MaxDpbMbs = 2376; break;
427
case 20: MaxDpbMbs = 2376; break;
428
case 21: MaxDpbMbs = 4752; break;
429
case 22: MaxDpbMbs = 8100; break;
430
case 30: MaxDpbMbs = 8100; break;
431
case 31: MaxDpbMbs = 18000; break;
432
case 32: MaxDpbMbs = 20480; break;
433
case 40: MaxDpbMbs = 32768; break;
434
case 41: MaxDpbMbs = 32768; break;
435
case 42: MaxDpbMbs = 34816; break;
436
case 50: MaxDpbMbs = 110400; break;
437
case 51: MaxDpbMbs = 184320; break;
439
g_assert(0 && "unhandled level");
443
PicSizeMbs = ((sps->pic_width_in_mbs_minus1 + 1) *
444
(sps->pic_height_in_map_units_minus1 + 1) *
445
(sps->frame_mbs_only_flag ? 1 : 2));
446
max_dec_frame_buffering = MaxDpbMbs / PicSizeMbs;
596
if (G_UNLIKELY(sps->level_idc == 11 && sps->constraint_set3_flag))
597
level = GST_VAAPI_LEVEL_H264_L1b;
599
level = gst_vaapi_utils_h264_get_level(sps->level_idc);
600
level_limits = gst_vaapi_utils_h264_get_level_limits(level);
601
if (G_UNLIKELY(!level_limits)) {
602
GST_FIXME("unsupported level_idc value (%d)", sps->level_idc);
603
max_dec_frame_buffering = 16;
606
PicSizeMbs = ((sps->pic_width_in_mbs_minus1 + 1) *
607
(sps->pic_height_in_map_units_minus1 + 1) *
608
(sps->frame_mbs_only_flag ? 1 : 2));
609
max_dec_frame_buffering = level_limits->MaxDpbMbs / PicSizeMbs;
611
if (is_mvc_profile(sps->profile_idc))
612
max_dec_frame_buffering <<= 1;
448
614
/* VUI parameters */
449
615
if (sps->vui_parameters_present_flag) {
554
729
dpb_remove_index(decoder, i);
558
dpb_bump(GstVaapiDecoderH264 *decoder)
732
/* Finds the frame store holding the supplied picture */
734
dpb_find_picture(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
736
GstVaapiDecoderH264Private * const priv = &decoder->priv;
739
for (i = 0; i < priv->dpb_count; i++) {
740
GstVaapiFrameStore * const fs = priv->dpb[i];
741
for (j = 0; j < fs->num_buffers; j++) {
742
if (fs->buffers[j] == picture)
749
/* Finds the picture with the lowest POC that needs to be output */
751
dpb_find_lowest_poc(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture,
752
GstVaapiPictureH264 **found_picture_ptr)
560
754
GstVaapiDecoderH264Private * const priv = &decoder->priv;
561
755
GstVaapiPictureH264 *found_picture = NULL;
562
756
guint i, j, found_index;
565
758
for (i = 0; i < priv->dpb_count; i++) {
566
759
GstVaapiFrameStore * const fs = priv->dpb[i];
567
760
if (!fs->output_needed)
569
for (j = 0; j < fs->num_buffers; j++) {
570
GstVaapiPictureH264 * const picture = fs->buffers[j];
571
if (!picture->output_needed)
573
if (!found_picture || found_picture->base.poc > picture->base.poc)
574
found_picture = picture, found_index = i;
762
if (picture && picture->base.view_id != fs->view_id)
764
for (j = 0; j < fs->num_buffers; j++) {
765
GstVaapiPictureH264 * const pic = fs->buffers[j];
766
if (!pic->output_needed)
768
if (!found_picture || found_picture->base.poc > pic->base.poc ||
769
(found_picture->base.poc == pic->base.poc &&
770
found_picture->base.voc > pic->base.voc))
771
found_picture = pic, found_index = i;
775
if (found_picture_ptr)
776
*found_picture_ptr = found_picture;
777
return found_picture ? found_index : -1;
780
/* Finds the picture with the lowest VOC that needs to be output */
782
dpb_find_lowest_voc(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture,
783
GstVaapiPictureH264 **found_picture_ptr)
785
GstVaapiDecoderH264Private * const priv = &decoder->priv;
786
GstVaapiPictureH264 *found_picture = NULL;
787
guint i, j, found_index;
789
for (i = 0; i < priv->dpb_count; i++) {
790
GstVaapiFrameStore * const fs = priv->dpb[i];
791
if (!fs->output_needed || fs->view_id == picture->base.view_id)
793
for (j = 0; j < fs->num_buffers; j++) {
794
GstVaapiPictureH264 * const pic = fs->buffers[j];
795
if (!pic->output_needed || pic->base.poc != picture->base.poc)
797
if (!found_picture || found_picture->base.voc > pic->base.voc)
798
found_picture = pic, found_index = i;
802
if (found_picture_ptr)
803
*found_picture_ptr = found_picture;
804
return found_picture ? found_index : -1;
808
dpb_output_other_views(GstVaapiDecoderH264 *decoder,
809
GstVaapiPictureH264 *picture, guint voc)
811
GstVaapiDecoderH264Private * const priv = &decoder->priv;
812
GstVaapiPictureH264 *found_picture;
816
if (priv->max_views == 1)
819
/* Emit all other view components that were in the same access
820
unit than the picture we have just found */
821
found_picture = picture;
823
found_index = dpb_find_lowest_voc(decoder, found_picture,
825
if (found_index < 0 || found_picture->base.voc >= voc)
827
success = dpb_output(decoder, priv->dpb[found_index]);
828
dpb_evict(decoder, found_picture, found_index);
836
dpb_bump(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
838
GstVaapiDecoderH264Private * const priv = &decoder->priv;
839
GstVaapiPictureH264 *found_picture;
843
found_index = dpb_find_lowest_poc(decoder, picture, &found_picture);
580
success = dpb_output(decoder, priv->dpb[found_index], found_picture);
847
if (picture && picture->base.poc != found_picture->base.poc)
848
dpb_output_other_views(decoder, found_picture, found_picture->base.voc);
850
success = dpb_output(decoder, priv->dpb[found_index]);
581
851
dpb_evict(decoder, found_picture, found_index);
852
if (priv->max_views == 1)
855
if (picture && picture->base.poc != found_picture->base.poc)
856
dpb_output_other_views(decoder, found_picture, G_MAXUINT32);
586
dpb_clear(GstVaapiDecoderH264 *decoder)
861
dpb_clear(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
588
863
GstVaapiDecoderH264Private * const priv = &decoder->priv;
591
for (i = 0; i < priv->dpb_count; i++)
866
for (i = 0; i < priv->dpb_count; i++) {
867
if (picture && picture->base.view_id != priv->dpb[i]->view_id)
592
869
gst_vaapi_frame_store_replace(&priv->dpb[i], NULL);
595
gst_vaapi_frame_store_replace(&priv->prev_frame, NULL);
872
/* Compact the resulting DPB, i.e. remove holes */
873
for (i = 0, n = 0; i < priv->dpb_count; i++) {
876
priv->dpb[n] = priv->dpb[i];
884
/* Clear previous frame buffers only if this is a "flush-all" operation,
885
or if the picture is the first one in the access unit */
886
if (priv->prev_frames && (!picture ||
887
GST_VAAPI_PICTURE_FLAG_IS_SET(picture,
888
GST_VAAPI_PICTURE_FLAG_AU_START))) {
889
for (i = 0; i < priv->max_views; i++)
890
gst_vaapi_frame_store_replace(&priv->prev_frames[i], NULL);
599
dpb_flush(GstVaapiDecoderH264 *decoder)
895
dpb_flush(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
601
while (dpb_bump(decoder))
897
GstVaapiDecoderH264Private * const priv = &decoder->priv;
900
/* Detect broken frames and mark them as having a single field if
902
for (i = 0; i < priv->dpb_count; i++) {
903
GstVaapiFrameStore * const fs = priv->dpb[i];
904
if (!fs->output_needed || gst_vaapi_frame_store_is_complete(fs))
906
GST_VAAPI_PICTURE_FLAG_SET(fs->buffers[0],
907
GST_VAAPI_PICTURE_FLAG_ONEFIELD);
910
/* Output any frame remaining in DPB */
911
while (dpb_bump(decoder, picture))
913
dpb_clear(decoder, picture);
917
dpb_prune_mvc(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
919
GstVaapiDecoderH264Private * const priv = &decoder->priv;
920
const gboolean is_last_picture = /* in the access unit */
921
GST_VAAPI_PICTURE_FLAG_IS_SET(picture, GST_VAAPI_PICTURE_FLAG_AU_END);
924
// Remove all unused inter-view only reference components of the current AU
926
while (i < priv->dpb_count) {
927
GstVaapiFrameStore * const fs = priv->dpb[i];
928
if (fs->view_id != picture->base.view_id &&
929
!fs->output_needed && !gst_vaapi_frame_store_has_reference(fs) &&
931
!is_inter_view_reference_for_next_frames(decoder, fs)))
932
dpb_remove_index(decoder, i);
625
961
// Check if picture is the second field and the first field is still in DPB
626
fs = priv->prev_frame;
627
if (fs && !gst_vaapi_frame_store_has_frame(fs))
628
return gst_vaapi_frame_store_add(fs, picture);
962
if (GST_VAAPI_PICTURE_IS_INTERLACED(picture) &&
963
!GST_VAAPI_PICTURE_IS_FIRST_FIELD(picture)) {
964
const gint found_index = dpb_find_picture(decoder,
965
GST_VAAPI_PICTURE_H264(picture->base.parent_picture));
966
if (found_index >= 0)
967
return gst_vaapi_frame_store_add(priv->dpb[found_index], picture);
969
// ... also check the previous picture that was immediately output
970
fs = priv->prev_frames[picture->base.voc];
971
if (fs && &fs->buffers[0]->base == picture->base.parent_picture) {
972
if (!gst_vaapi_frame_store_add(fs, picture))
974
return dpb_output(decoder, fs);
630
978
// Create new frame store, and split fields if necessary
631
979
fs = gst_vaapi_frame_store_new(picture);
634
gst_vaapi_frame_store_replace(&priv->prev_frame, fs);
982
gst_vaapi_frame_store_replace(&priv->prev_frames[picture->base.voc], fs);
635
983
gst_vaapi_frame_store_unref(fs);
637
985
if (!priv->progressive_sequence && gst_vaapi_frame_store_has_frame(fs)) {
642
990
// C.4.5.1 - Storage and marking of a reference decoded picture into the DPB
643
991
if (GST_VAAPI_PICTURE_IS_REFERENCE(picture)) {
644
992
while (priv->dpb_count == priv->dpb_size) {
645
if (!dpb_bump(decoder))
993
if (!dpb_bump(decoder, picture))
648
gst_vaapi_frame_store_replace(&priv->dpb[priv->dpb_count++], fs);
649
if (picture->output_flag) {
650
picture->output_needed = TRUE;
655
998
// C.4.5.2 - Storage and marking of a non-reference decoded picture into the DPB
657
if (!picture->output_flag)
1000
const gboolean StoreInterViewOnlyRefFlag =
1001
!GST_VAAPI_PICTURE_FLAG_IS_SET(picture,
1002
GST_VAAPI_PICTURE_FLAG_AU_END) &&
1003
GST_VAAPI_PICTURE_FLAG_IS_SET(picture,
1004
GST_VAAPI_PICTURE_FLAG_INTER_VIEW);
1005
if (!picture->output_flag && !StoreInterViewOnlyRefFlag)
659
1007
while (priv->dpb_count == priv->dpb_size) {
660
gboolean found_picture = FALSE;
661
for (i = 0; !found_picture && i < priv->dpb_count; i++) {
662
GstVaapiFrameStore * const fs = priv->dpb[i];
663
if (!fs->output_needed)
665
for (j = 0; !found_picture && j < fs->num_buffers; j++)
666
found_picture = fs->buffers[j]->output_needed &&
667
fs->buffers[j]->base.poc < picture->base.poc;
1008
GstVaapiPictureH264 *found_picture;
1009
if (!StoreInterViewOnlyRefFlag) {
1010
if (dpb_find_lowest_poc(decoder, picture, &found_picture) < 0 ||
1011
found_picture->base.poc > picture->base.poc)
1012
return dpb_output(decoder, fs);
670
return dpb_output(decoder, NULL, picture);
671
if (!dpb_bump(decoder))
1014
if (!dpb_bump(decoder, picture))
674
gst_vaapi_frame_store_replace(&priv->dpb[priv->dpb_count++], fs);
675
picture->output_needed = TRUE;
1018
gst_vaapi_frame_store_replace(&priv->dpb[priv->dpb_count++], fs);
682
dpb_reset(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
1023
dpb_reset(GstVaapiDecoderH264 *decoder, guint dpb_size)
684
1025
GstVaapiDecoderH264Private * const priv = &decoder->priv;
686
priv->dpb_size = get_max_dec_frame_buffering(sps);
1027
if (dpb_size > priv->dpb_size_max) {
1028
priv->dpb = g_try_realloc_n(priv->dpb, dpb_size, sizeof(*priv->dpb));
1031
memset(&priv->dpb[priv->dpb_size_max], 0,
1032
(dpb_size - priv->dpb_size_max) * sizeof(*priv->dpb));
1033
priv->dpb_size_max = dpb_size;
1035
priv->dpb_size = dpb_size;
687
1037
GST_DEBUG("DPB size %u", priv->dpb_size);
1042
unref_inter_view(GstVaapiPictureH264 *picture)
1046
GST_VAAPI_PICTURE_FLAG_UNSET(picture, GST_VAAPI_PICTURE_FLAG_INTER_VIEW);
1047
gst_vaapi_picture_unref(picture);
1050
/* Resets MVC resources */
1052
mvc_reset(GstVaapiDecoderH264 *decoder)
1054
GstVaapiDecoderH264Private * const priv = &decoder->priv;
1057
// Resize array of inter-view references
1058
if (!priv->inter_views) {
1059
priv->inter_views = g_ptr_array_new_full(priv->max_views,
1060
(GDestroyNotify)unref_inter_view);
1061
if (!priv->inter_views)
1065
// Resize array of previous frame buffers
1066
for (i = priv->max_views; i < priv->prev_frames_alloc; i++)
1067
gst_vaapi_frame_store_replace(&priv->prev_frames[i], NULL);
1069
priv->prev_frames = g_try_realloc_n(priv->prev_frames, priv->max_views,
1070
sizeof(*priv->prev_frames));
1071
if (!priv->prev_frames) {
1072
priv->prev_frames_alloc = 0;
1075
for (i = priv->prev_frames_alloc; i < priv->max_views; i++)
1076
priv->prev_frames[i] = NULL;
1077
priv->prev_frames_alloc = priv->max_views;
690
1081
static GstVaapiDecoderStatus
766
h264_get_profile(GstH264SPS *sps)
770
switch (sps->profile_idc) {
771
case GST_H264_PROFILE_BASELINE:
772
profile = GST_VAAPI_PROFILE_H264_BASELINE;
774
case GST_H264_PROFILE_MAIN:
775
profile = GST_VAAPI_PROFILE_H264_MAIN;
777
case GST_H264_PROFILE_HIGH:
778
profile = GST_VAAPI_PROFILE_H264_HIGH;
785
h264_get_chroma_type(GstH264SPS *sps)
787
guint chroma_type = 0;
789
switch (sps->chroma_format_idc) {
791
chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
794
chroma_type = GST_VAAPI_CHROMA_TYPE_YUV422;
797
if (!sps->separate_colour_plane_flag)
798
chroma_type = GST_VAAPI_CHROMA_TYPE_YUV444;
1180
/* Activates the supplied PPS */
1182
ensure_pps(GstVaapiDecoderH264 *decoder, GstH264PPS *pps)
1184
GstVaapiDecoderH264Private * const priv = &decoder->priv;
1185
GstVaapiParserInfoH264 * const pi = priv->pps[pps->id];
1187
gst_vaapi_parser_info_h264_replace(&priv->active_pps, pi);
1188
return pi ? &pi->data.pps : NULL;
1191
/* Returns the active PPS */
1192
static inline GstH264PPS *
1193
get_pps(GstVaapiDecoderH264 *decoder)
1195
GstVaapiParserInfoH264 * const pi = decoder->priv.active_pps;
1197
return pi ? &pi->data.pps : NULL;
1200
/* Activate the supplied SPS */
1202
ensure_sps(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
1204
GstVaapiDecoderH264Private * const priv = &decoder->priv;
1205
GstVaapiParserInfoH264 * const pi = priv->sps[sps->id];
1207
gst_vaapi_parser_info_h264_replace(&priv->active_sps, pi);
1208
return pi ? &pi->data.sps : NULL;
1211
/* Returns the active SPS */
1212
static inline GstH264SPS *
1213
get_sps(GstVaapiDecoderH264 *decoder)
1215
GstVaapiParserInfoH264 * const pi = decoder->priv.active_sps;
1217
return pi ? &pi->data.sps : NULL;
1221
fill_profiles(GstVaapiProfile profiles[16], guint *n_profiles_ptr,
1222
GstVaapiProfile profile)
1224
guint n_profiles = *n_profiles_ptr;
1226
profiles[n_profiles++] = profile;
1228
case GST_VAAPI_PROFILE_H264_MAIN:
1229
profiles[n_profiles++] = GST_VAAPI_PROFILE_H264_HIGH;
1234
*n_profiles_ptr = n_profiles;
1237
/* Fills in compatible profiles for MVC decoding */
1239
fill_profiles_mvc(GstVaapiDecoderH264 *decoder, GstVaapiProfile profiles[16],
1240
guint *n_profiles_ptr, guint dpb_size)
1242
const gchar * const vendor_string =
1243
gst_vaapi_display_get_vendor_string(GST_VAAPI_DECODER_DISPLAY(decoder));
1245
gboolean add_high_profile = FALSE;
1250
const struct map *m;
1252
// Drivers that support slice level decoding
1253
if (vendor_string && dpb_size <= 16) {
1254
static const struct map drv_names[] = {
1255
{ "Intel i965 driver", 17 },
1258
for (m = drv_names; m->str != NULL && !add_high_profile; m++) {
1259
if (g_ascii_strncasecmp(vendor_string, m->str, m->str_len) == 0)
1260
add_high_profile = TRUE;
1264
if (add_high_profile)
1265
fill_profiles(profiles, n_profiles_ptr, GST_VAAPI_PROFILE_H264_HIGH);
804
1268
static GstVaapiProfile
805
get_profile(GstVaapiDecoderH264 *decoder, GstH264SPS *sps)
1269
get_profile(GstVaapiDecoderH264 *decoder, GstH264SPS *sps, guint dpb_size)
807
1271
GstVaapiDecoderH264Private * const priv = &decoder->priv;
808
1272
GstVaapiDisplay * const display = GST_VAAPI_DECODER_DISPLAY(decoder);
809
GstVaapiProfile profile, profiles[2];
1273
GstVaapiProfile profile, profiles[4];
810
1274
guint i, n_profiles = 0;
812
profile = h264_get_profile(sps);
1276
profile = gst_vaapi_utils_h264_get_profile(sps->profile_idc);
814
1278
return GST_VAAPI_PROFILE_UNKNOWN;
816
profiles[n_profiles++] = profile;
1280
fill_profiles(profiles, &n_profiles, profile);
817
1281
switch (profile) {
818
case GST_VAAPI_PROFILE_H264_MAIN:
819
profiles[n_profiles++] = GST_VAAPI_PROFILE_H264_HIGH;
1282
case GST_VAAPI_PROFILE_H264_BASELINE:
1283
if (sps->constraint_set1_flag) { // A.2.2 (main profile)
1284
fill_profiles(profiles, &n_profiles,
1285
GST_VAAPI_PROFILE_H264_CONSTRAINED_BASELINE);
1286
fill_profiles(profiles, &n_profiles,
1287
GST_VAAPI_PROFILE_H264_MAIN);
1290
case GST_VAAPI_PROFILE_H264_EXTENDED:
1291
if (sps->constraint_set1_flag) { // A.2.2 (main profile)
1292
fill_profiles(profiles, &n_profiles,
1293
GST_VAAPI_PROFILE_H264_MAIN);
1296
case GST_VAAPI_PROFILE_H264_MULTIVIEW_HIGH:
1297
if (priv->max_views == 2) {
1298
fill_profiles(profiles, &n_profiles,
1299
GST_VAAPI_PROFILE_H264_STEREO_HIGH);
1301
fill_profiles_mvc(decoder, profiles, &n_profiles, dpb_size);
1303
case GST_VAAPI_PROFILE_H264_STEREO_HIGH:
1304
if (sps->frame_mbs_only_flag) {
1305
fill_profiles(profiles, &n_profiles,
1306
GST_VAAPI_PROFILE_H264_MULTIVIEW_HIGH);
1308
fill_profiles_mvc(decoder, profiles, &n_profiles, dpb_size);
843
1332
GstVaapiProfile profile;
844
1333
GstVaapiChromaType chroma_type;
845
1334
gboolean reset_context = FALSE;
846
guint mb_width, mb_height;
848
profile = get_profile(decoder, sps);
1335
guint mb_width, mb_height, dpb_size, num_views;
1337
num_views = get_num_views(sps);
1338
if (priv->max_views < num_views) {
1339
priv->max_views = num_views;
1340
GST_DEBUG("maximum number of views changed to %u", num_views);
1343
dpb_size = get_max_dec_frame_buffering(sps);
1344
if (priv->dpb_size < dpb_size) {
1345
GST_DEBUG("DPB size increased");
1346
reset_context = TRUE;
1349
profile = get_profile(decoder, sps, dpb_size);
850
1351
GST_ERROR("unsupported profile_idc %u", sps->profile_idc);
851
1352
return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
854
if (priv->profile != profile) {
1355
if (!priv->profile || (priv->profile != profile && priv->max_views == 1)) {
855
1356
GST_DEBUG("profile changed");
856
1357
reset_context = TRUE;
857
1358
priv->profile = profile;
860
chroma_type = h264_get_chroma_type(sps);
861
if (!chroma_type || chroma_type != GST_VAAPI_CHROMA_TYPE_YUV420) {
1361
chroma_type = gst_vaapi_utils_h264_get_chroma_type(sps->chroma_format_idc);
862
1363
GST_ERROR("unsupported chroma_format_idc %u", sps->chroma_format_idc);
863
1364
return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CHROMA_FORMAT;
897
1395
/* XXX: fix surface size when cropping is implemented */
898
1396
info.profile = priv->profile;
899
1397
info.entrypoint = priv->entrypoint;
1398
info.chroma_type = priv->chroma_type;
900
1399
info.width = sps->width;
901
1400
info.height = sps->height;
902
info.ref_frames = get_max_dec_frame_buffering(sps);
1401
info.ref_frames = dpb_size;
904
1403
if (!gst_vaapi_decoder_ensure_context(GST_VAAPI_DECODER(decoder), &info))
905
1404
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
906
1405
priv->has_context = TRUE;
909
dpb_reset(decoder, sps);
1408
if (!dpb_reset(decoder, dpb_size))
1409
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
1411
/* Reset MVC data */
1412
if (!mvc_reset(decoder))
1413
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
910
1414
return GST_VAAPI_DECODER_STATUS_SUCCESS;
914
fill_iq_matrix_4x4(VAIQMatrixBufferH264 *iq_matrix, const GstH264PPS *pps)
1418
fill_iq_matrix_4x4(VAIQMatrixBufferH264 *iq_matrix, const GstH264PPS *pps,
1419
const GstH264SPS *sps)
916
const guint8 (* const ScalingList4x4)[6][16] = &pps->scaling_lists_4x4;
919
1423
/* There are always 6 4x4 scaling lists */
920
1424
g_assert(G_N_ELEMENTS(iq_matrix->ScalingList4x4) == 6);
921
1425
g_assert(G_N_ELEMENTS(iq_matrix->ScalingList4x4[0]) == 16);
923
if (sizeof(iq_matrix->ScalingList4x4[0][0]) == 1)
924
memcpy(iq_matrix->ScalingList4x4, *ScalingList4x4,
925
sizeof(iq_matrix->ScalingList4x4));
927
for (i = 0; i < G_N_ELEMENTS(iq_matrix->ScalingList4x4); i++) {
928
for (j = 0; j < G_N_ELEMENTS(iq_matrix->ScalingList4x4[i]); j++)
929
iq_matrix->ScalingList4x4[i][j] = (*ScalingList4x4)[i][j];
1427
for (i = 0; i < G_N_ELEMENTS(iq_matrix->ScalingList4x4); i++)
1428
gst_h264_quant_matrix_4x4_get_raster_from_zigzag(
1429
iq_matrix->ScalingList4x4[i], pps->scaling_lists_4x4[i]);
935
fill_iq_matrix_8x8(VAIQMatrixBufferH264 *iq_matrix, const GstH264PPS *pps)
1433
fill_iq_matrix_8x8(VAIQMatrixBufferH264 *iq_matrix, const GstH264PPS *pps,
1434
const GstH264SPS *sps)
937
const guint8 (* const ScalingList8x8)[6][64] = &pps->scaling_lists_8x8;
938
const GstH264SPS * const sps = pps->sequence;
941
1438
/* If chroma_format_idc != 3, there are up to 2 8x8 scaling lists */
942
1439
if (!pps->transform_8x8_mode_flag)
977
1469
if (sps->chroma_format_idc == 3)
978
1470
return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CHROMA_FORMAT;
980
fill_iq_matrix_4x4(iq_matrix, pps);
981
fill_iq_matrix_8x8(iq_matrix, pps);
1472
fill_iq_matrix_4x4(iq_matrix, pps, sps);
1473
fill_iq_matrix_8x8(iq_matrix, pps, sps);
983
1475
return GST_VAAPI_DECODER_STATUS_SUCCESS;
1478
static inline gboolean
1479
is_valid_state(guint state, guint ref_state)
1481
return (state & ref_state) == ref_state;
986
1484
static GstVaapiDecoderStatus
987
1485
decode_current_picture(GstVaapiDecoderH264 *decoder)
989
1487
GstVaapiDecoderH264Private * const priv = &decoder->priv;
990
1488
GstVaapiPictureH264 * const picture = priv->current_picture;
1490
if (!is_valid_state(priv->decoder_state, GST_H264_VIDEO_STATE_VALID_PICTURE))
1492
priv->decoder_state = 0;
993
1495
return GST_VAAPI_DECODER_STATUS_SUCCESS;
1497
if (!gst_vaapi_picture_decode(GST_VAAPI_PICTURE_CAST(picture)))
995
1499
if (!exec_ref_pic_marking(decoder, picture))
997
1501
if (!dpb_add(decoder, picture))
999
if (!gst_vaapi_picture_decode(GST_VAAPI_PICTURE_CAST(picture)))
1001
if (priv->prev_frame && gst_vaapi_frame_store_has_frame(priv->prev_frame))
1002
gst_vaapi_picture_replace(&priv->current_picture, NULL);
1503
gst_vaapi_picture_replace(&priv->current_picture, NULL);
1003
1504
return GST_VAAPI_DECODER_STATUS_SUCCESS;
1006
1507
/* XXX: fix for cases where first field failed to be decoded */
1007
1508
gst_vaapi_picture_replace(&priv->current_picture, NULL);
1008
1509
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
1512
priv->decoder_state = 0;
1513
return GST_VAAPI_DECODER_STATUS_DROP_FRAME;
1011
1516
static GstVaapiDecoderStatus
1079
1609
GstVaapiDecoderH264Private * const priv = &decoder->priv;
1080
1610
GstVaapiParserInfoH264 * const pi = unit->parsed_info;
1081
1611
GstH264SliceHdr * const slice_hdr = &pi->data.slice_hdr;
1612
GstH264NalUnit * const nalu = &pi->nalu;
1082
1614
GstH264ParserResult result;
1084
1616
GST_DEBUG("parse slice");
1618
priv->parser_state &= (GST_H264_VIDEO_STATE_GOT_SPS|
1619
GST_H264_VIDEO_STATE_GOT_PPS);
1621
/* Propagate Prefix NAL unit info, if necessary */
1622
switch (nalu->type) {
1623
case GST_H264_NAL_SLICE:
1624
case GST_H264_NAL_SLICE_IDR: {
1625
GstVaapiParserInfoH264 * const prev_pi = priv->prev_pi;
1626
if (prev_pi && prev_pi->nalu.type == GST_H264_NAL_PREFIX_UNIT) {
1627
/* MVC sequences shall have a Prefix NAL unit immediately
1628
preceding this NAL unit */
1629
pi->nalu.extension_type = prev_pi->nalu.extension_type;
1630
pi->nalu.extension = prev_pi->nalu.extension;
1633
/* In the very unlikely case there is no Prefix NAL unit
1634
immediately preceding this NAL unit, try to infer some
1635
defaults (H.7.4.1.1) */
1636
GstH264NalUnitExtensionMVC * const mvc = &pi->nalu.extension.mvc;
1637
mvc->non_idr_flag = !(nalu->type == GST_H264_NAL_SLICE_IDR);
1638
nalu->idr_pic_flag = !mvc->non_idr_flag;
1639
mvc->priority_id = 0;
1641
mvc->temporal_id = 0;
1642
mvc->anchor_pic_flag = 0;
1643
mvc->inter_view_flag = 1;
1086
1649
/* Variables that don't have inferred values per the H.264
1087
1650
standard but that should get a default value anyway */
1088
1651
slice_hdr->cabac_init_idc = 0;
1093
1656
if (result != GST_H264_PARSER_OK)
1094
1657
return get_status(result);
1659
sps = slice_hdr->pps->sequence;
1661
/* Update MVC data */
1662
pi->view_id = get_view_id(&pi->nalu);
1663
pi->voc = get_view_order_index(sps, pi->view_id);
1665
priv->parser_state |= GST_H264_VIDEO_STATE_GOT_SLICE;
1666
return GST_VAAPI_DECODER_STATUS_SUCCESS;
1669
static GstVaapiDecoderStatus
1670
decode_sps(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
1672
GstVaapiDecoderH264Private * const priv = &decoder->priv;
1673
GstVaapiParserInfoH264 * const pi = unit->parsed_info;
1674
GstH264SPS * const sps = &pi->data.sps;
1676
GST_DEBUG("decode SPS");
1678
gst_vaapi_parser_info_h264_replace(&priv->sps[sps->id], pi);
1679
return GST_VAAPI_DECODER_STATUS_SUCCESS;
1682
static GstVaapiDecoderStatus
1683
decode_subset_sps(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
1685
GstVaapiDecoderH264Private * const priv = &decoder->priv;
1686
GstVaapiParserInfoH264 * const pi = unit->parsed_info;
1687
GstH264SPS * const sps = &pi->data.sps;
1689
GST_DEBUG("decode subset SPS");
1691
gst_vaapi_parser_info_h264_replace(&priv->sps[sps->id], pi);
1692
return GST_VAAPI_DECODER_STATUS_SUCCESS;
1695
static GstVaapiDecoderStatus
1696
decode_pps(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
1698
GstVaapiDecoderH264Private * const priv = &decoder->priv;
1699
GstVaapiParserInfoH264 * const pi = unit->parsed_info;
1700
GstH264PPS * const pps = &pi->data.pps;
1702
GST_DEBUG("decode PPS");
1704
gst_vaapi_parser_info_h264_replace(&priv->pps[pps->id], pi);
1096
1705
return GST_VAAPI_DECODER_STATUS_SUCCESS;
1099
1708
static GstVaapiDecoderStatus
1100
1709
decode_sequence_end(GstVaapiDecoderH264 *decoder)
1711
GstVaapiDecoderH264Private * const priv = &decoder->priv;
1102
1712
GstVaapiDecoderStatus status;
1104
1714
GST_DEBUG("decode sequence-end");
1491
2107
*RefPicList_count = n;
2110
/* Finds the inter-view reference picture with the supplied view id */
2111
static GstVaapiPictureH264 *
2112
find_inter_view_reference(GstVaapiDecoderH264 *decoder, guint16 view_id)
2114
GPtrArray * const inter_views = decoder->priv.inter_views;
2117
for (i = 0; i < inter_views->len; i++) {
2118
GstVaapiPictureH264 * const picture = g_ptr_array_index(inter_views, i);
2119
if (picture->base.view_id == view_id)
2123
GST_WARNING("failed to find inter-view reference picture for view_id: %d",
2128
/* Checks whether the view id exists in the supplied list of view ids */
2130
find_view_id(guint16 view_id, const guint16 *view_ids, guint num_view_ids)
2134
for (i = 0; i < num_view_ids; i++) {
2135
if (view_ids[i] == view_id)
2142
find_view_id_in_view(guint16 view_id, const GstH264SPSExtMVCView *view,
2146
return (find_view_id(view_id, view->anchor_ref_l0,
2147
view->num_anchor_refs_l0) ||
2148
find_view_id(view_id, view->anchor_ref_l1,
2149
view->num_anchor_refs_l1));
2151
return (find_view_id(view_id, view->non_anchor_ref_l0,
2152
view->num_non_anchor_refs_l0) ||
2153
find_view_id(view_id, view->non_anchor_ref_l1,
2154
view->num_non_anchor_refs_l1));
2157
/* Checks whether the inter-view reference picture with the supplied
2158
view id is used for decoding the current view component picture */
2160
is_inter_view_reference_for_picture(GstVaapiDecoderH264 *decoder,
2161
guint16 view_id, GstVaapiPictureH264 *picture)
2163
const GstH264SPS * const sps = get_sps(decoder);
2166
if (!GST_VAAPI_PICTURE_IS_MVC(picture) ||
2167
sps->extension_type != GST_H264_NAL_EXTENSION_MVC)
2170
is_anchor = GST_VAAPI_PICTURE_IS_ANCHOR(picture);
2171
return find_view_id_in_view(view_id,
2172
&sps->extension.mvc.view[picture->base.voc], is_anchor);
2175
/* Checks whether the supplied inter-view reference picture is used
2176
for decoding the next view component pictures */
2178
is_inter_view_reference_for_next_pictures(GstVaapiDecoderH264 *decoder,
2179
GstVaapiPictureH264 *picture)
2181
const GstH264SPS * const sps = get_sps(decoder);
2185
if (!GST_VAAPI_PICTURE_IS_MVC(picture) ||
2186
sps->extension_type != GST_H264_NAL_EXTENSION_MVC)
2189
is_anchor = GST_VAAPI_PICTURE_IS_ANCHOR(picture);
2190
num_views = sps->extension.mvc.num_views_minus1 + 1;
2191
for (i = picture->base.voc + 1; i < num_views; i++) {
2192
const GstH264SPSExtMVCView * const view = &sps->extension.mvc.view[i];
2193
if (find_view_id_in_view(picture->base.view_id, view, is_anchor))
2199
/* H.8.2.1 - Initialization process for inter-view prediction references */
2201
init_picture_refs_mvc_1(GstVaapiDecoderH264 *decoder,
2202
GstVaapiPictureH264 **ref_list, guint *ref_list_count_ptr, guint num_refs,
2203
const guint16 *view_ids, guint num_view_ids)
2207
n = *ref_list_count_ptr;
2208
for (j = 0; j < num_view_ids && n < num_refs; j++) {
2209
GstVaapiPictureH264 * const pic =
2210
find_inter_view_reference(decoder, view_ids[j]);
2212
ref_list[n++] = pic;
2214
*ref_list_count_ptr = n;
2218
init_picture_refs_mvc(GstVaapiDecoderH264 *decoder,
2219
GstVaapiPictureH264 *picture, GstH264SliceHdr *slice_hdr, guint list)
2221
GstVaapiDecoderH264Private * const priv = &decoder->priv;
2222
const GstH264SPS * const sps = get_sps(decoder);
2223
const GstH264SPSExtMVCView *view;
2225
GST_DEBUG("initialize reference picture list for inter-view prediction");
2227
if (sps->extension_type != GST_H264_NAL_EXTENSION_MVC)
2229
view = &sps->extension.mvc.view[picture->base.voc];
2231
#define INVOKE_INIT_PICTURE_REFS_MVC(ref_list, view_list) do { \
2232
init_picture_refs_mvc_1(decoder, \
2233
priv->RefPicList##ref_list, \
2234
&priv->RefPicList##ref_list##_count, \
2235
slice_hdr->num_ref_idx_l##ref_list##_active_minus1 + 1, \
2236
view->view_list##_l##ref_list, \
2237
view->num_##view_list##s_l##ref_list); \
2241
if (GST_VAAPI_PICTURE_IS_ANCHOR(picture))
2242
INVOKE_INIT_PICTURE_REFS_MVC(0, anchor_ref);
2244
INVOKE_INIT_PICTURE_REFS_MVC(0, non_anchor_ref);
2247
if (GST_VAAPI_PICTURE_IS_ANCHOR(picture))
2248
INVOKE_INIT_PICTURE_REFS_MVC(1, anchor_ref);
2250
INVOKE_INIT_PICTURE_REFS_MVC(1, non_anchor_ref);
2253
#undef INVOKE_INIT_PICTURE_REFS_MVC
1495
2257
init_picture_refs_p_slice(
1496
2258
GstVaapiDecoderH264 *decoder,
1854
2659
LongTermPicNumF =
1855
2660
GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(ref_list[j]) ?
1856
2661
ref_list[j]->long_term_pic_num : INT_MAX;
1857
if (LongTermPicNumF != l->value.long_term_pic_num)
2662
if (LongTermPicNumF != l->value.long_term_pic_num ||
2663
ref_list[j]->base.view_id != picture->base.view_id)
2664
ref_list[n++] = ref_list[j];
2668
/* H.8.2.2.3 - Inter-view prediction reference pictures */
2669
else if ((GST_VAAPI_PICTURE_IS_MVC(picture) &&
2670
sps->extension_type == GST_H264_NAL_EXTENSION_MVC) &&
2671
(l->modification_of_pic_nums_idc == 4 ||
2672
l->modification_of_pic_nums_idc == 5)) {
2673
gint32 abs_diff_view_idx = l->value.abs_diff_view_idx_minus1 + 1;
2674
gint32 picViewIdx, targetViewId;
2677
if (l->modification_of_pic_nums_idc == 4) {
2678
picViewIdx = picViewIdxPred - abs_diff_view_idx;
2680
picViewIdx += num_view_ids;
2685
picViewIdx = picViewIdxPred + abs_diff_view_idx;
2686
if (picViewIdx >= num_view_ids)
2687
picViewIdx -= num_view_ids;
2689
picViewIdxPred = picViewIdx;
2692
targetViewId = view_ids[picViewIdx];
2695
for (j = num_refs; j > ref_list_idx; j--)
2696
ref_list[j] = ref_list[j - 1];
2697
ref_list[ref_list_idx++] =
2698
find_inter_view_reference(decoder, targetViewId);
2700
for (j = ref_list_idx; j <= num_refs; j++) {
2703
if (ref_list[j]->base.view_id != targetViewId ||
2704
ref_list[j]->base.poc != picture->base.poc)
1858
2705
ref_list[n++] = ref_list[j];
1893
init_picture_ref_lists(GstVaapiDecoderH264 *decoder)
2740
init_picture_ref_lists(GstVaapiDecoderH264 *decoder,
2741
GstVaapiPictureH264 *picture)
1895
2743
GstVaapiDecoderH264Private * const priv = &decoder->priv;
1896
2744
guint i, j, short_ref_count, long_ref_count;
1898
2746
short_ref_count = 0;
1899
2747
long_ref_count = 0;
1900
if (GST_VAAPI_PICTURE_IS_FRAME(priv->current_picture)) {
2748
if (GST_VAAPI_PICTURE_IS_FRAME(picture)) {
1901
2749
for (i = 0; i < priv->dpb_count; i++) {
1902
2750
GstVaapiFrameStore * const fs = priv->dpb[i];
1903
GstVaapiPictureH264 *picture;
2751
GstVaapiPictureH264 *pic;
1904
2752
if (!gst_vaapi_frame_store_has_frame(fs))
1906
picture = fs->buffers[0];
1907
if (GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(picture))
1908
priv->short_ref[short_ref_count++] = picture;
1909
else if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(picture))
1910
priv->long_ref[long_ref_count++] = picture;
1911
picture->structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
1912
picture->other_field = fs->buffers[1];
2754
pic = fs->buffers[0];
2755
if (pic->base.view_id != picture->base.view_id)
2757
if (GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(pic))
2758
priv->short_ref[short_ref_count++] = pic;
2759
else if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(pic))
2760
priv->long_ref[long_ref_count++] = pic;
2761
pic->structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
2762
pic->other_field = fs->buffers[1];
1916
2766
for (i = 0; i < priv->dpb_count; i++) {
1917
2767
GstVaapiFrameStore * const fs = priv->dpb[i];
1918
2768
for (j = 0; j < fs->num_buffers; j++) {
1919
GstVaapiPictureH264 * const picture = fs->buffers[j];
1920
if (GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(picture))
1921
priv->short_ref[short_ref_count++] = picture;
1922
else if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(picture))
1923
priv->long_ref[long_ref_count++] = picture;
1924
picture->structure = picture->base.structure;
1925
picture->other_field = fs->buffers[j ^ 1];
2769
GstVaapiPictureH264 * const pic = fs->buffers[j];
2770
if (pic->base.view_id != picture->base.view_id)
2772
if (GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(pic))
2773
priv->short_ref[short_ref_count++] = pic;
2774
else if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(pic))
2775
priv->long_ref[long_ref_count++] = pic;
2776
pic->structure = pic->base.structure;
2777
pic->other_field = fs->buffers[j ^ 1];
2002
2853
picture->frame_num_wrap = priv->frame_num;
2003
2854
picture->output_flag = TRUE; /* XXX: conformant to Annex A only */
2004
2855
base_picture->pts = GST_VAAPI_DECODER_CODEC_FRAME(decoder)->pts;
2856
base_picture->type = GST_VAAPI_PICTURE_TYPE_NONE;
2857
base_picture->view_id = pi->view_id;
2858
base_picture->voc = pi->voc;
2860
/* Initialize extensions */
2861
switch (pi->nalu.extension_type) {
2862
case GST_H264_NAL_EXTENSION_MVC: {
2863
GstH264NalUnitExtensionMVC * const mvc = &pi->nalu.extension.mvc;
2865
GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_MVC);
2866
if (mvc->inter_view_flag)
2867
GST_VAAPI_PICTURE_FLAG_SET(picture,
2868
GST_VAAPI_PICTURE_FLAG_INTER_VIEW);
2869
if (mvc->anchor_pic_flag)
2870
GST_VAAPI_PICTURE_FLAG_SET(picture,
2871
GST_VAAPI_PICTURE_FLAG_ANCHOR);
2006
2876
/* Reset decoder state for IDR pictures */
2007
if (pi->nalu.type == GST_H264_NAL_SLICE_IDR) {
2877
if (pi->nalu.idr_pic_flag) {
2008
2878
GST_DEBUG("<IDR>");
2009
2879
GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_IDR);
2013
/* Initialize slice type */
2014
switch (slice_hdr->type % 5) {
2015
case GST_H264_P_SLICE:
2016
base_picture->type = GST_VAAPI_PICTURE_TYPE_P;
2018
case GST_H264_B_SLICE:
2019
base_picture->type = GST_VAAPI_PICTURE_TYPE_B;
2021
case GST_H264_I_SLICE:
2022
base_picture->type = GST_VAAPI_PICTURE_TYPE_I;
2024
case GST_H264_SP_SLICE:
2025
base_picture->type = GST_VAAPI_PICTURE_TYPE_SP;
2027
case GST_H264_SI_SLICE:
2028
base_picture->type = GST_VAAPI_PICTURE_TYPE_SI;
2880
dpb_flush(decoder, picture);
2032
2883
/* Initialize picture structure */
3262
vaapi_fill_picture_for_RefPicListX(VAPictureH264 *pic,
3263
GstVaapiPictureH264 *picture)
3265
vaapi_fill_picture(pic, picture, 0);
3267
/* H.8.4 - MVC inter prediction and inter-view prediction process */
3268
if (GST_VAAPI_PICTURE_IS_INTER_VIEW(picture)) {
3269
/* The inter-view reference components and inter-view only
3270
reference components that are included in the reference
3271
picture lists are considered as not being marked as "used for
3272
short-term reference" or "used for long-term reference" */
3273
pic->flags &= ~(VA_PICTURE_H264_SHORT_TERM_REFERENCE|
3274
VA_PICTURE_H264_LONG_TERM_REFERENCE);
2383
3278
static gboolean
2384
fill_picture(GstVaapiDecoderH264 *decoder,
2385
GstVaapiPictureH264 *picture, GstVaapiParserInfoH264 *pi)
3279
fill_picture(GstVaapiDecoderH264 *decoder, GstVaapiPictureH264 *picture)
2387
3281
GstVaapiDecoderH264Private * const priv = &decoder->priv;
2388
3282
GstVaapiPicture * const base_picture = &picture->base;
2389
GstH264SliceHdr * const slice_hdr = &pi->data.slice_hdr;
2390
GstH264PPS * const pps = picture->pps;
2391
GstH264SPS * const sps = pps->sequence;
3283
GstH264PPS * const pps = get_pps(decoder);
3284
GstH264SPS * const sps = get_sps(decoder);
2392
3285
VAPictureParameterBufferH264 * const pic_param = base_picture->param;
3426
/* Detection of a new access unit, assuming we are already in presence
3428
static inline gboolean
3429
is_new_access_unit(GstVaapiParserInfoH264 *pi, GstVaapiParserInfoH264 *prev_pi)
3431
if (!prev_pi || prev_pi->view_id == pi->view_id)
3433
return pi->voc < prev_pi->voc;
3436
/* Finds the first field picture corresponding to the supplied picture */
3437
static GstVaapiPictureH264 *
3438
find_first_field(GstVaapiDecoderH264 *decoder, GstVaapiParserInfoH264 *pi)
3440
GstVaapiDecoderH264Private * const priv = &decoder->priv;
3441
GstH264SliceHdr * const slice_hdr = &pi->data.slice_hdr;
3442
GstVaapiFrameStore *fs;
3444
if (!slice_hdr->field_pic_flag)
3447
fs = priv->prev_frames[pi->voc];
3448
if (!fs || gst_vaapi_frame_store_has_frame(fs))
3451
if (fs->buffers[0]->frame_num == slice_hdr->frame_num)
3452
return fs->buffers[0];
2524
3456
static GstVaapiDecoderStatus
2525
3457
decode_picture(GstVaapiDecoderH264 *decoder, GstVaapiDecoderUnit *unit)
2527
3459
GstVaapiDecoderH264Private * const priv = &decoder->priv;
2528
3460
GstVaapiParserInfoH264 * const pi = unit->parsed_info;
2529
3461
GstH264SliceHdr * const slice_hdr = &pi->data.slice_hdr;
2530
GstH264PPS * const pps = slice_hdr->pps;
2531
GstH264SPS * const sps = pps->sequence;
2532
GstVaapiPictureH264 *picture;
3462
GstH264PPS * const pps = ensure_pps(decoder, slice_hdr->pps);
3463
GstH264SPS * const sps = ensure_sps(decoder, slice_hdr->pps->sequence);
3464
GstVaapiPictureH264 *picture, *first_field;
2533
3465
GstVaapiDecoderStatus status;
3467
g_return_val_if_fail(pps != NULL, GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN);
3468
g_return_val_if_fail(sps != NULL, GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN);
3470
/* Only decode base stream for MVC */
3471
switch (sps->profile_idc) {
3472
case GST_H264_PROFILE_MULTIVIEW_HIGH:
3473
case GST_H264_PROFILE_STEREO_HIGH:
3475
GST_DEBUG("drop picture from substream");
3476
return GST_VAAPI_DECODER_STATUS_DROP_FRAME;
2535
3481
status = ensure_context(decoder, sps);
2536
3482
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
2539
if (priv->current_picture) {
3485
priv->decoder_state = 0;
3487
first_field = find_first_field(decoder, pi);
2540
3489
/* Re-use current picture where the first field was decoded */
2541
picture = gst_vaapi_picture_h264_new_field(priv->current_picture);
3490
picture = gst_vaapi_picture_h264_new_field(first_field);
2542
3491
if (!picture) {
2543
3492
GST_ERROR("failed to allocate field picture");
2544
3493
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
2739
3697
GST_DEBUG("slice (%u bytes)", pi->nalu.size);
2741
if (!priv->got_sps || !priv->got_pps) {
2742
GST_ERROR("not initialized yet");
3699
if (!is_valid_state(pi->state,
3700
GST_H264_VIDEO_STATE_VALID_PICTURE_HEADERS)) {
3701
GST_WARNING("failed to receive enough headers to decode slice");
2743
3702
return GST_VAAPI_DECODER_STATUS_SUCCESS;
3705
if (!ensure_pps(decoder, slice_hdr->pps)) {
3706
GST_ERROR("failed to activate PPS");
3707
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
3710
if (!ensure_sps(decoder, slice_hdr->pps->sequence)) {
3711
GST_ERROR("failed to activate SPS");
3712
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
2746
3715
if (!gst_buffer_map(buffer, &map_info, GST_MAP_READ)) {
2747
3716
GST_ERROR("failed to map buffer");
2748
3717
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
3720
/* Check wether this is the first/last slice in the current access unit */
3721
if (pi->flags & GST_VAAPI_DECODER_UNIT_FLAG_AU_START)
3722
GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_AU_START);
3723
if (pi->flags & GST_VAAPI_DECODER_UNIT_FLAG_AU_END)
3724
GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_AU_END);
2751
3726
slice = GST_VAAPI_SLICE_NEW(H264, decoder,
2752
3727
(map_info.data + unit->offset + pi->nalu.offset), pi->nalu.size);
2753
3728
gst_buffer_unmap(buffer, &map_info);
2833
3822
for (i = 0; i < num_sps; i++) {
3823
pi = gst_vaapi_parser_info_h264_new();
3825
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
3826
unit.parsed_info = pi;
2834
3828
result = gst_h264_parser_identify_nalu_avc(
2836
3830
buf, ofs, buf_size, 2,
2839
if (result != GST_H264_PARSER_OK)
2840
return get_status(result);
3833
if (result != GST_H264_PARSER_OK) {
3834
status = get_status(result);
2842
3838
status = parse_sps(decoder, &unit);
2843
3839
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
2845
ofs = pi.nalu.offset + pi.nalu.size;
3841
ofs = pi->nalu.offset + pi->nalu.size;
3843
status = decode_sps(decoder, &unit);
3844
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
3846
gst_vaapi_parser_info_h264_replace(&pi, NULL);
2848
3849
num_pps = buf[ofs];
2851
3852
for (i = 0; i < num_pps; i++) {
3853
pi = gst_vaapi_parser_info_h264_new();
3855
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
3856
unit.parsed_info = pi;
2852
3858
result = gst_h264_parser_identify_nalu_avc(
2854
3860
buf, ofs, buf_size, 2,
2857
if (result != GST_H264_PARSER_OK)
2858
return get_status(result);
3863
if (result != GST_H264_PARSER_OK) {
3864
status = get_status(result);
2860
3868
status = parse_pps(decoder, &unit);
2861
3869
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
2863
ofs = pi.nalu.offset + pi.nalu.size;
3871
ofs = pi->nalu.offset + pi->nalu.size;
3873
status = decode_pps(decoder, &unit);
3874
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
3876
gst_vaapi_parser_info_h264_replace(&pi, NULL);
2866
3879
priv->is_avcC = TRUE;
2867
return GST_VAAPI_DECODER_STATUS_SUCCESS;
3880
status = GST_VAAPI_DECODER_STATUS_SUCCESS;
3883
gst_vaapi_parser_info_h264_replace(&pi, NULL);
2870
3887
static GstVaapiDecoderStatus
2923
3949
buf_size = priv->nal_length_size + nalu_size;
2924
3950
if (size < buf_size)
2925
3951
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
3952
else if (priv->stream_alignment == GST_VAAPI_STREAM_ALIGN_H264_AU)
3953
at_au_end = (buf_size == size);
2929
3957
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
2931
ofs = scan_for_start_code(adapter, 0, size, NULL);
2933
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
2936
gst_adapter_flush(adapter, ofs);
2940
ofs2 = ps->input_offset2 - ofs - 4;
2944
ofs = G_UNLIKELY(size < ofs2 + 4) ? -1 :
2945
scan_for_start_code(adapter, ofs2, size - ofs2, NULL);
2947
// Assume the whole NAL unit is present if end-of-stream
2949
ps->input_offset2 = size;
3959
if (priv->stream_alignment == GST_VAAPI_STREAM_ALIGN_H264_NALU)
3962
ofs = scan_for_start_code(adapter, 0, size, NULL);
2950
3964
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
3967
gst_adapter_flush(adapter, ofs);
3971
ofs2 = ps->input_offset2 - ofs - 4;
3975
ofs = G_UNLIKELY(size < ofs2 + 4) ? -1 :
3976
scan_for_start_code(adapter, ofs2, size - ofs2, NULL);
3978
// Assume the whole NAL unit is present if end-of-stream
3979
// or stream buffers aligned on access unit boundaries
3980
if (priv->stream_alignment == GST_VAAPI_STREAM_ALIGN_H264_AU)
3983
ps->input_offset2 = size;
3984
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
2956
3991
ps->input_offset2 = 0;
3012
4061
/* fall-through */
3013
4062
case GST_H264_NAL_SEQ_END:
3014
4063
flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_END;
4064
flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_END;
3016
4066
case GST_H264_NAL_SPS:
4067
case GST_H264_NAL_SUBSET_SPS:
3017
4068
case GST_H264_NAL_PPS:
3018
flags |= GST_VAAPI_DECODER_UNIT_FLAG_SKIP;
3020
4069
case GST_H264_NAL_SEI:
4070
flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START;
3021
4071
flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
4073
case GST_H264_NAL_SLICE_EXT:
4074
if (!GST_H264_IS_MVC_NALU(&pi->nalu)) {
4075
flags |= GST_VAAPI_DECODER_UNIT_FLAG_SKIP;
3023
4079
case GST_H264_NAL_SLICE_IDR:
3024
4080
case GST_H264_NAL_SLICE:
3025
4081
flags |= GST_VAAPI_DECODER_UNIT_FLAG_SLICE;
3026
if (is_new_picture(pi, priv->prev_slice_pi))
4082
if (priv->prev_pi &&
4083
(priv->prev_pi->flags & GST_VAAPI_DECODER_UNIT_FLAG_AU_END)) {
4084
flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START |
4085
GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
4087
else if (is_new_picture(pi, priv->prev_slice_pi)) {
3027
4088
flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
4089
if (is_new_access_unit(pi, priv->prev_slice_pi))
4090
flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START;
3028
4092
gst_vaapi_parser_info_h264_replace(&priv->prev_slice_pi, pi);
4094
case GST_H264_NAL_SPS_EXT:
4095
case GST_H264_NAL_SLICE_AUX:
4096
/* skip SPS extension and auxiliary slice for now */
4097
flags |= GST_VAAPI_DECODER_UNIT_FLAG_SKIP;
4099
case GST_H264_NAL_PREFIX_UNIT:
4100
/* skip Prefix NAL units for now */
4101
flags |= GST_VAAPI_DECODER_UNIT_FLAG_SKIP |
4102
GST_VAAPI_DECODER_UNIT_FLAG_AU_START |
4103
GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
3031
4106
if (pi->nalu.type >= 14 && pi->nalu.type <= 18)
3032
flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
4107
flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START |
4108
GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
4111
if ((flags & GST_VAAPI_DECODER_UNIT_FLAGS_AU) && priv->prev_slice_pi)
4112
priv->prev_slice_pi->flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_END;
3035
4113
GST_VAAPI_DECODER_UNIT_FLAG_SET(unit, flags);
3037
4115
pi->nalu.data = NULL;
4116
pi->state = priv->parser_state;
4118
gst_vaapi_parser_info_h264_replace(&priv->prev_pi, pi);
3038
4119
return GST_VAAPI_DECODER_STATUS_SUCCESS;