History

avcodec/cbs: add cbs implementation for H266/VVC

Add CodedBitstreamContext to parse VPS,SPS,PPS in VVC nal units. Implement parsing and writing of SPS,PPS,VPS,PH,AUD,SEI and slices. Add ff_cbs_type_h266 to cbs types tables and AV_CODEC_ID_H266 to cbs codec ids.

Nuo Mi committed 3 years ago

Commit dfc62fd1 parent 136e96a8 files · +4390 −2Browse files

Unified · Split

8 files
  1. configure +2 −0
  2. libavcodec/Makefile +1 −0
  3. libavcodec/cbs.c +6 −0
  4. libavcodec/cbs_h2645.c +452 −2
  5. libavcodec/cbs_h266.h +783 −0
  6. libavcodec/cbs_h266_syntax_template.c +3116 −0
  7. libavcodec/cbs_internal.h +1 −0
  8. libavcodec/cbs_sei.c +29 −0
configure+2−0
@@ -2477,6 +2477,7 @@ CONFIG_EXTRA=" · 2476 unmodified lines
24772477 cbs_av1
24782478 cbs_h264
24792479 cbs_h265
2480+ cbs_h266
24802481 cbs_jpeg
24812482 cbs_mpeg2
24822483 cbs_vp9
@@ -2761,6 +2762,7 @@ threads_if_any="$THREADS_LIST" · 278 unmodified lines
27612762 cbs_av1_select="cbs"
27622763 cbs_h264_select="cbs"
27632764 cbs_h265_select="cbs"
2765+cbs_h266_select="cbs"
27642766 cbs_jpeg_select="cbs"
27652767 cbs_mpeg2_select="cbs"
27662768 cbs_vp9_select="cbs"
libavcodec/Makefile+1−0
@@ -76,6 +76,7 @@ OBJS-$(CONFIG_CBS) += cbs.o cbs_bsf.o · 75 unmodified lines
7676 OBJS-$(CONFIG_CBS_AV1) += cbs_av1.o
7777 OBJS-$(CONFIG_CBS_H264) += cbs_h2645.o cbs_sei.o h2645_parse.o
7878 OBJS-$(CONFIG_CBS_H265) += cbs_h2645.o cbs_sei.o h2645_parse.o
79+OBJS-$(CONFIG_CBS_H266) += cbs_h2645.o cbs_sei.o h2645_parse.o
7980 OBJS-$(CONFIG_CBS_JPEG) += cbs_jpeg.o
8081 OBJS-$(CONFIG_CBS_MPEG2) += cbs_mpeg2.o
8182 OBJS-$(CONFIG_CBS_VP9) += cbs_vp9.o
libavcodec/cbs.c+6−0
@@ -40,6 +40,9 @@ static const CodedBitstreamType *const cbs_type_table[] = { · 39 unmodified lines
4040 #if CONFIG_CBS_H265
4141 &ff_cbs_type_h265,
4242 #endif
43+#if CONFIG_CBS_H266
44+ &ff_cbs_type_h266,
45+#endif
4346 #if CONFIG_CBS_JPEG
4447 &ff_cbs_type_jpeg,
4548 #endif
@@ -61,6 +64,9 @@ const enum AVCodecID ff_cbs_all_codec_ids[] = { · 15 unmodified lines
6164 #if CONFIG_CBS_H265
6265 AV_CODEC_ID_H265,
6366 #endif
67+#if CONFIG_CBS_H266
68+ AV_CODEC_ID_H266,
69+#endif
6470 #if CONFIG_CBS_JPEG
6571 AV_CODEC_ID_MJPEG,
6672 #endif
libavcodec/cbs_h2645.c+452−2
@@ -24,9 +24,11 @@ · 23 unmodified lines
2424 #include "cbs_internal.h"
2525 #include "cbs_h264.h"
2626 #include "cbs_h265.h"
27+#include "cbs_h266.h"
2728 #include "h264.h"
2829 #include "h2645_parse.h"
2930 #include "hevc.h"
31+#include "vvc.h"
3032
3133
3234 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
@@ -255,6 +257,7 @@ static int cbs_h265_payload_extension_present(GetBitContext *gbc, uint32_t paylo · 222 unmodified lines
255257 #define FUNC_NAME1(rw, codec, name) FUNC_NAME2(rw, codec, name)
256258 #define FUNC_H264(name) FUNC_NAME1(READWRITE, h264, name)
257259 #define FUNC_H265(name) FUNC_NAME1(READWRITE, h265, name)
260+#define FUNC_H266(name) FUNC_NAME1(READWRITE, h266, name)
258261 #define FUNC_SEI(name) FUNC_NAME1(READWRITE, sei, name)
259262
260263 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
@@ -369,6 +372,10 @@ static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc) · 108 unmodified lines
369372 #include "cbs_h265_syntax_template.c"
370373 #undef FUNC
371374
375+#define FUNC(name) FUNC_H266(name)
376+#include "cbs_h266_syntax_template.c"
377+#undef FUNC
378+
372379 #undef READ
373380 #undef READWRITE
374381 #undef RWContext
@@ -447,6 +454,10 @@ static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc) · 72 unmodified lines
447454 #include "cbs_h265_syntax_template.c"
448455 #undef FUNC
449456
457+#define FUNC(name) FUNC_H266(name)
458+#include "cbs_h266_syntax_template.c"
459+#undef FUNC
460+
450461 #undef WRITE
451462 #undef READWRITE
452463 #undef RWContext
@@ -476,8 +487,9 @@ static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx, · 23 unmodified lines
476487 const H2645NAL *nal = &packet->nals[i];
477488 AVBufferRef *ref;
478489 size_t size = nal->size;
490+ enum AVCodecID codec_id = ctx->codec->codec_id;
479491
480− if (nal->nuh_layer_id > 0)
492+ if (codec_id != AV_CODEC_ID_VVC && nal->nuh_layer_id > 0)
481493 continue;
482494
483495 // Remove trailing zeroes.
@@ -513,6 +525,12 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, · 29 unmodified lines
513525 if (frag->data_size == 0)
514526 return 0;
515527
528+ if (codec_id == AV_CODEC_ID_VVC) {
529+ //we deactive picture header here to avoid reuse previous au's ph.
530+ CodedBitstreamH266Context *h266 = ctx->priv_data;
531+ h266->priv.ph = NULL;
532+ }
533+
516534 if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
517535 // AVCC header.
518536 size_t size, start, end;
@@ -640,6 +658,71 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx, · 121 unmodified lines
640658 return err;
641659 }
642660
661+ } else if(header && frag->data[0] && codec_id == AV_CODEC_ID_VVC) {
662+ // VVCC header.
663+ int ptl_present_flag, num_arrays;
664+ int b, i, j;
665+
666+ priv->mp4 = 1;
667+
668+ bytestream2_init(&gbc, frag->data, frag->data_size);
669+
670+ b = bytestream2_get_byte(&gbc);
671+ priv->nal_length_size = ((b >> 1) & 3) + 1;
672+ ptl_present_flag = b & 1;
673+
674+ if(ptl_present_flag) {
675+ int num_sublayers, num_bytes_constraint_info, num_sub_profiles;
676+ num_sublayers = (bytestream2_get_be16u(&gbc) >> 4) & 7;
677+ bytestream2_skip(&gbc, 1);
678+
679+ // begin VvcPTLRecord(num_sublayers);
680+ num_bytes_constraint_info = bytestream2_get_byte(&gbc) & 0x3f;
681+ bytestream2_skip(&gbc, 2 + num_bytes_constraint_info);
682+ if(num_sublayers > 1) {
683+ int count_present_flags = 0;
684+ b = bytestream2_get_byte(&gbc);
685+ for(i = num_sublayers - 2; i >= 0; i--) {
686+ if((b >> (7 - (num_sublayers - 2 - i))) & 0x01)
687+ count_present_flags++;
688+ }
689+ bytestream2_skip(&gbc, count_present_flags);
690+ }
691+ num_sub_profiles = bytestream2_get_byte(&gbc);
692+ bytestream2_skip(&gbc, num_sub_profiles * 4);
693+ // end VvcPTLRecord(num_sublayers);
694+
695+ bytestream2_skip(&gbc, 3 * 2);
696+ }
697+
698+ num_arrays = bytestream2_get_byte(&gbc);
699+ for(j = 0; j < num_arrays; j++) {
700+ size_t start, end, size;
701+ int nal_unit_type = bytestream2_get_byte(&gbc) & 0x1f;
702+ unsigned int num_nalus = 1;
703+ if(nal_unit_type != VVC_DCI_NUT && nal_unit_type != VVC_OPI_NUT)
704+ num_nalus = bytestream2_get_be16u(&gbc);
705+
706+ start = bytestream2_tell(&gbc);
707+ for(i = 0; i < num_nalus; i++) {
708+ size = bytestream2_get_be16(&gbc);
709+ bytestream2_skip(&gbc, size);
710+ }
711+ end = bytestream2_tell(&gbc);
712+
713+ err = ff_h2645_packet_split(&priv->read_packet,
714+ frag->data + start, end - start,
715+ ctx->log_ctx, 1, 2, AV_CODEC_ID_VVC, 1, 1);
716+ if (err < 0) {
717+ av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
718+ "VVCC array %d (%d NAL units of type %d).\n",
719+ i, num_nalus, nal_unit_type);
720+ return err;
721+ }
722+ err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
723+ if (err < 0)
724+ return err;
725+ }
643726 } else {
644727 // Annex B, or later MP4 with already-known parameters.
645728
@@ -686,6 +769,46 @@ cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id) · 40 unmodified lines
686769 cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
687770 cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
688771
772+#define cbs_h266_replace_ps(h26n, ps_name, ps_var, id_element) \
773+static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
774+ CodedBitstreamUnit *unit) \
775+{ \
776+ CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
777+ H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
778+ unsigned int id = ps_var->id_element; \
779+ int err = ff_cbs_make_unit_refcounted(ctx, unit); \
780+ if (err < 0) \
781+ return err; \
782+ av_buffer_unref(&priv->ps_var ## _ref[id]); \
783+ av_assert0(unit->content_ref); \
784+ priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
785+ if (!priv->ps_var ## _ref[id]) \
786+ return AVERROR(ENOMEM); \
787+ priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
788+ return 0; \
789+}
790+
791+cbs_h266_replace_ps(6, VPS, vps, vps_video_parameter_set_id)
792+cbs_h266_replace_ps(6, SPS, sps, sps_seq_parameter_set_id)
793+cbs_h266_replace_ps(6, PPS, pps, pps_pic_parameter_set_id)
794+
795+static int cbs_h266_replace_ph(CodedBitstreamContext *ctx,
796+ CodedBitstreamUnit *unit)
797+{
798+ CodedBitstreamH266Context *h266 = ctx->priv_data;
799+ int err;
800+
801+ h266->priv.ph = NULL;
802+ err = ff_cbs_make_unit_refcounted(ctx, unit);
803+ if (err < 0)
804+ return err;
805+ err = av_buffer_replace(&h266->priv.ph_ref, unit->content_ref);
806+ if (err < 0)
807+ return err;
808+ h266->priv.ph = (H266RawPH*)h266->priv.ph_ref->data;
809+ return 0;
810+}
811+
689812 static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
690813 CodedBitstreamUnit *unit)
691814 {
@@ -926,6 +1049,130 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, · 234 unmodified lines
9261049 return 0;
9271050 }
9281051
1052+static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx,
1053+ CodedBitstreamUnit *unit)
1054+{
1055+ GetBitContext gbc;
1056+ int err;
1057+
1058+ err = init_get_bits8(&gbc, unit->data, unit->data_size);
1059+ if (err < 0)
1060+ return err;
1061+
1062+ err = ff_cbs_alloc_unit_content(ctx, unit);
1063+ if (err < 0)
1064+ return err;
1065+
1066+ switch (unit->type) {
1067+ case VVC_VPS_NUT:
1068+ {
1069+ H266RawVPS *vps = unit->content;
1070+
1071+ err = cbs_h266_read_vps(ctx, &gbc, vps);
1072+ if (err < 0)
1073+ return err;
1074+
1075+ err = cbs_h266_replace_vps(ctx, unit);
1076+ if (err < 0)
1077+ return err;
1078+ }
1079+ break;
1080+ case VVC_SPS_NUT:
1081+ {
1082+ H266RawSPS *sps = unit->content;
1083+
1084+ err = cbs_h266_read_sps(ctx, &gbc, sps);
1085+ if (err < 0)
1086+ return err;
1087+
1088+ err = cbs_h266_replace_sps(ctx, unit);
1089+ if (err < 0)
1090+ return err;
1091+ }
1092+ break;
1093+
1094+ case VVC_PPS_NUT:
1095+ {
1096+ H266RawPPS *pps = unit->content;
1097+
1098+ err = cbs_h266_read_pps(ctx, &gbc, pps);
1099+ if (err < 0)
1100+ return err;
1101+
1102+ err = cbs_h266_replace_pps(ctx, unit);
1103+ if (err < 0)
1104+ return err;
1105+ }
1106+ break;
1107+
1108+ case VVC_PH_NUT:
1109+ {
1110+ H266RawPH *ph = unit->content;
1111+ err = cbs_h266_read_ph(ctx, &gbc, ph);
1112+ if (err < 0)
1113+ return err;
1114+ err = cbs_h266_replace_ph(ctx, unit);
1115+ if (err < 0)
1116+ return err;
1117+ }
1118+ break;
1119+
1120+ case VVC_TRAIL_NUT:
1121+ case VVC_STSA_NUT:
1122+ case VVC_RADL_NUT:
1123+ case VVC_RASL_NUT:
1124+ case VVC_IDR_W_RADL:
1125+ case VVC_IDR_N_LP:
1126+ case VVC_CRA_NUT:
1127+ case VVC_GDR_NUT:
1128+ {
1129+ H266RawSlice *slice = unit->content;
1130+ int pos, len;
1131+
1132+ err = cbs_h266_read_slice_header(ctx, &gbc, &slice->header);
1133+ if (err < 0)
1134+ return err;
1135+
1136+ if (!cbs_h2645_read_more_rbsp_data(&gbc))
1137+ return AVERROR_INVALIDDATA;
1138+
1139+ pos = get_bits_count(&gbc);
1140+ len = unit->data_size;
1141+
1142+ slice->data_size = len - pos / 8;
1143+ slice->data_ref = av_buffer_ref(unit->data_ref);
1144+ if (!slice->data_ref)
1145+ return AVERROR(ENOMEM);
1146+ slice->data = unit->data + pos / 8;
1147+ slice->data_bit_start = pos % 8;
1148+ }
1149+ break;
1150+
1151+ case VVC_AUD_NUT:
1152+ {
1153+ err = cbs_h266_read_aud(ctx, &gbc, unit->content);
1154+ if (err < 0)
1155+ return err;
1156+ }
1157+ break;
1158+
1159+ case VVC_PREFIX_SEI_NUT:
1160+ case VVC_SUFFIX_SEI_NUT:
1161+ {
1162+ err = cbs_h266_read_sei(ctx, &gbc, unit->content,
1163+ unit->type == VVC_PREFIX_SEI_NUT);
1164+
1165+ if (err < 0)
1166+ return err;
1167+ }
1168+ break;
1169+
1170+ default:
1171+ return AVERROR(ENOSYS);
1172+ }
1173+ return 0;
1174+}
1175+
9291176 static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
9301177 PutBitContext *pbc, const uint8_t *data,
9311178 size_t data_size, int data_bit_start)
@@ -1338,11 +1585,127 @@ static int cbs_h265_discarded_nal_unit(CodedBitstreamContext *ctx, · 406 unmodified lines
13381585 return 0;
13391586 }
13401587
1588+static int cbs_h266_write_nal_unit(CodedBitstreamContext *ctx,
1589+ CodedBitstreamUnit *unit,
1590+ PutBitContext *pbc)
1591+{
1592+ int err;
1593+
1594+ switch (unit->type) {
1595+ case VVC_VPS_NUT:
1596+ {
1597+ H266RawVPS *vps = unit->content;
1598+
1599+ err = cbs_h266_write_vps(ctx, pbc, vps);
1600+ if (err < 0)
1601+ return err;
1602+
1603+ err = cbs_h266_replace_vps(ctx, unit);
1604+ if (err < 0)
1605+ return err;
1606+ }
1607+ break;
1608+ case VVC_SPS_NUT:
1609+ {
1610+ H266RawSPS *sps = unit->content;
1611+
1612+ err = cbs_h266_write_sps(ctx, pbc, sps);
1613+ if (err < 0)
1614+ return err;
1615+
1616+ err = cbs_h266_replace_sps(ctx, unit);
1617+ if (err < 0)
1618+ return err;
1619+ }
1620+ break;
1621+
1622+ case VVC_PPS_NUT:
1623+ {
1624+ H266RawPPS *pps = unit->content;
1625+
1626+ err = cbs_h266_write_pps(ctx, pbc, pps);
1627+ if (err < 0)
1628+ return err;
1629+
1630+ err = cbs_h266_replace_pps(ctx, unit);
1631+ if (err < 0)
1632+ return err;
1633+ }
1634+ break;
1635+
1636+ case VVC_PH_NUT:
1637+ {
1638+ H266RawPH *ph = unit->content;
1639+ err = cbs_h266_write_ph(ctx, pbc, ph);
1640+ if (err < 0)
1641+ return err;
1642+
1643+ err = cbs_h266_replace_ph(ctx, unit);
1644+ if (err < 0)
1645+ return err;
1646+ }
1647+ break;
1648+
1649+ case VVC_TRAIL_NUT:
1650+ case VVC_STSA_NUT:
1651+ case VVC_RADL_NUT:
1652+ case VVC_RASL_NUT:
1653+ case VVC_IDR_W_RADL:
1654+ case VVC_IDR_N_LP:
1655+ case VVC_CRA_NUT:
1656+ case VVC_GDR_NUT:
1657+ {
1658+ H266RawSlice *slice = unit->content;
1659+
1660+ err = cbs_h266_write_slice_header(ctx, pbc, &slice->header);
1661+ if (err < 0)
1662+ return err;
1663+
1664+ if (slice->data) {
1665+ err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1666+ slice->data_size,
1667+ slice->data_bit_start);
1668+ if (err < 0)
1669+ return err;
1670+ } else {
1671+ // No slice data - that was just the header.
1672+ }
1673+ }
1674+ break;
1675+
1676+ case VVC_AUD_NUT:
1677+ {
1678+ err = cbs_h266_write_aud(ctx, pbc, unit->content);
1679+ if (err < 0)
1680+ return err;
1681+ }
1682+ break;
1683+
1684+ case VVC_PREFIX_SEI_NUT:
1685+ case VVC_SUFFIX_SEI_NUT:
1686+ {
1687+ err = cbs_h266_write_sei(ctx, pbc, unit->content,
1688+ unit->type == VVC_PREFIX_SEI_NUT);
1689+
1690+ if (err < 0)
1691+ return err;
1692+ }
1693+ break;
1694+
1695+ default:
1696+ av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1697+ "NAL unit type %"PRIu32".\n", unit->type);
1698+ return AVERROR_PATCHWELCOME;
1699+ }
1700+
1701+ return 0;
1702+}
1703+
13411704 static int cbs_h2645_unit_requires_zero_byte(enum AVCodecID codec_id,
13421705 CodedBitstreamUnitType type,
13431706 int nal_unit_index)
13441707 {
1345− // Section B.1.2 in H.264, section B.2.2 in H.265.
1708+ // Section B.1.2 in H.264, section B.2.2 in H.265, H.266.
13461709 if (nal_unit_index == 0) {
13471710 // Assume that this is the first NAL unit in an access unit.
13481711 return 1;
@@ -1351,6 +1714,8 @@ static int cbs_h2645_unit_requires_zero_byte(enum AVCodecID codec_id, · 2 unmodified lines
13511714 return type == H264_NAL_SPS || type == H264_NAL_PPS;
13521715 if (codec_id == AV_CODEC_ID_HEVC)
13531716 return type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS;
1717+ if (codec_id == AV_CODEC_ID_VVC)
1718+ return type >= VVC_OPI_NUT && type <= VVC_SUFFIX_APS_NUT;
13541719 return 0;
13551720 }
13561721
@@ -1502,6 +1867,35 @@ static void cbs_h265_close(CodedBitstreamContext *ctx) · 145 unmodified lines
15021867 av_buffer_unref(&h265->pps_ref[i]);
15031868 }
15041869
1870+static void cbs_h266_flush(CodedBitstreamContext *ctx)
1871+{
1872+ CodedBitstreamH266Context *h266 = ctx->priv_data;
1873+
1874+ for (int i = 0; i < FF_ARRAY_ELEMS(h266->vps); i++) {
1875+ av_buffer_unref(&h266->vps_ref[i]);
1876+ h266->vps[i] = NULL;
1877+ }
1878+
1879+ for (int i = 0; i < FF_ARRAY_ELEMS(h266->sps); i++) {
1880+ av_buffer_unref(&h266->sps_ref[i]);
1881+ h266->sps[i] = NULL;
1882+ }
1883+ for (int i = 0; i < FF_ARRAY_ELEMS(h266->pps); i++) {
1884+ av_buffer_unref(&h266->pps_ref[i]);
1885+ h266->pps[i] = NULL;
1886+ }
1887+ av_buffer_unref(&h266->priv.ph_ref);
1888+ h266->priv.ph = NULL;
1889+}
1890+
1891+static void cbs_h266_close(CodedBitstreamContext *ctx)
1892+{
1893+ CodedBitstreamH266Context *h266 = ctx->priv_data;
1894+
1895+ cbs_h266_flush(ctx);
1896+ ff_h2645_packet_uninit(&h266->common.read_packet);
1897+ }
1898+
15051899 static void cbs_h264_free_sei(void *opaque, uint8_t *content)
15061900 {
15071901 H264RawSEI *sei = (H264RawSEI*)content;
@@ -1556,6 +1950,33 @@ static const CodedBitstreamUnitTypeDescriptor cbs_h265_unit_types[] = { · 48 unmodified lines
15561950 CBS_UNIT_TYPE_END_OF_LIST
15571951 };
15581952
1953+static void cbs_h266_free_sei(void *opaque, uint8_t *content)
1954+{
1955+ H266RawSEI *sei = (H266RawSEI*)content;
1956+ ff_cbs_sei_free_message_list(&sei->message_list);
1957+ av_free(content);
1958+}
1959+
1960+static const CodedBitstreamUnitTypeDescriptor cbs_h266_unit_types[] = {
1961+ CBS_UNIT_TYPE_INTERNAL_REF(VVC_VPS_NUT, H266RawVPS, extension_data.data),
1962+ CBS_UNIT_TYPE_INTERNAL_REF(VVC_SPS_NUT, H266RawSPS, extension_data.data),
1963+ CBS_UNIT_TYPE_INTERNAL_REF(VVC_PPS_NUT, H266RawPPS, extension_data.data),
1964+
1965+ CBS_UNIT_TYPE_POD(VVC_PH_NUT , H266RawPH),
1966+ CBS_UNIT_TYPE_POD(VVC_AUD_NUT, H266RawAUD),
1967+
1968+ CBS_UNIT_RANGE_INTERNAL_REF(VVC_TRAIL_NUT, VVC_RASL_NUT,
1969+ H266RawSlice, data),
1970+
1971+ CBS_UNIT_RANGE_INTERNAL_REF(VVC_IDR_W_RADL, VVC_GDR_NUT,
1972+ H266RawSlice, data),
1973+
1974+ CBS_UNIT_TYPES_COMPLEX((VVC_PREFIX_SEI_NUT, VVC_SUFFIX_SEI_NUT),
1975+ H266RawSEI, cbs_h266_free_sei),
1976+
1977+ CBS_UNIT_TYPE_END_OF_LIST
1978+};
1979+
15591980 const CodedBitstreamType ff_cbs_type_h264 = {
15601981 .codec_id = AV_CODEC_ID_H264,
15611982
@@ -1590,6 +2011,22 @@ const CodedBitstreamType ff_cbs_type_h265 = { · 28 unmodified lines
15902011 .close = &cbs_h265_close,
15912012 };
15922013
2014+const CodedBitstreamType ff_cbs_type_h266 = {
2015+ .codec_id = AV_CODEC_ID_VVC,
2016+
2017+ .priv_data_size = sizeof(CodedBitstreamH266Context),
2018+
2019+ .unit_types = cbs_h266_unit_types,
2020+
2021+ .split_fragment = &cbs_h2645_split_fragment,
2022+ .read_unit = &cbs_h266_read_nal_unit,
2023+ .write_unit = &cbs_h266_write_nal_unit,
2024+ .assemble_fragment = &cbs_h2645_assemble_fragment,
2025+
2026+ .flush = &cbs_h266_flush,
2027+ .close = &cbs_h266_close,
2028+};
2029+
15932030 static const SEIMessageTypeDescriptor cbs_sei_common_types[] = {
15942031 {
15952032 SEI_TYPE_FILLER_PAYLOAD,
@@ -1740,6 +2177,16 @@ static const SEIMessageTypeDescriptor cbs_sei_h265_types[] = { · 144 unmodified lines
17402177 SEI_MESSAGE_TYPE_END
17412178 };
17422179
2180+static const SEIMessageTypeDescriptor cbs_sei_h266_types[] = {
2181+ {
2182+ SEI_TYPE_DECODED_PICTURE_HASH,
2183+ 0, 1,
2184+ sizeof(H266RawSEIDecodedPictureHash),
2185+ SEI_MESSAGE_RW(h266, sei_decoded_picture_hash),
2186+ },
2187+ SEI_MESSAGE_TYPE_END
2188+};
2189+
17432190 const SEIMessageTypeDescriptor *ff_cbs_sei_find_type(CodedBitstreamContext *ctx,
17442191 int payload_type)
17452192 {
@@ -1758,6 +2205,9 @@ const SEIMessageTypeDescriptor *ff_cbs_sei_find_type(CodedBitstreamContext *ctx, · 12 unmodified lines
17582205 case AV_CODEC_ID_H265:
17592206 codec_list = cbs_sei_h265_types;
17602207 break;
2208+ case AV_CODEC_ID_H266:
2209+ codec_list = cbs_sei_h266_types;
2210+ break;
17612211 default:
17622212 return NULL;
17632213 }
libavcodec/cbs_h266.h+783−0
@@ -0,0 +1,783 @@
1+/*
2+ * This file is part of FFmpeg.
3+ *
4+ * FFmpeg is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Lesser General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2.1 of the License, or (at your option) any later version.
8+ *
9+ * FFmpeg is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * Lesser General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Lesser General Public
15+ * License along with FFmpeg; if not, write to the Free Software
16+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+ */
18+
19+#ifndef AVCODEC_CBS_H266_H
20+#define AVCODEC_CBS_H266_H
21+
22+#include <stddef.h>
23+#include <stdint.h>
24+
25+#include "cbs_h2645.h"
26+#include "cbs_sei.h"
27+#include "vvc.h"
28+
29+typedef struct H266RawNALUnitHeader {
30+ uint8_t nuh_layer_id;
31+ uint8_t nal_unit_type;
32+ uint8_t nuh_temporal_id_plus1;
33+ uint8_t nuh_reserved_zero_bit;
34+} H266RawNALUnitHeader;
35+
36+typedef struct H266GeneralConstraintsInfo {
37+ uint8_t gci_present_flag;
38+ /* general */
39+ uint8_t gci_intra_only_constraint_flag;
40+ uint8_t gci_all_layers_independent_constraint_flag;
41+ uint8_t gci_one_au_only_constraint_flag;
42+
43+ /* picture format */
44+ uint8_t gci_sixteen_minus_max_bitdepth_constraint_idc;
45+ uint8_t gci_three_minus_max_chroma_format_constraint_idc;
46+
47+ /* NAL unit type related */
48+ uint8_t gci_no_mixed_nalu_types_in_pic_constraint_flag;
49+ uint8_t gci_no_trail_constraint_flag;
50+ uint8_t gci_no_stsa_constraint_flag;
51+ uint8_t gci_no_rasl_constraint_flag;
52+ uint8_t gci_no_radl_constraint_flag;
53+ uint8_t gci_no_idr_constraint_flag;
54+ uint8_t gci_no_cra_constraint_flag;
55+ uint8_t gci_no_gdr_constraint_flag;
56+ uint8_t gci_no_aps_constraint_flag;
57+ uint8_t gci_no_idr_rpl_constraint_flag;
58+
59+ /* tile, slice, subpicture partitioning */
60+ uint8_t gci_one_tile_per_pic_constraint_flag;
61+ uint8_t gci_pic_header_in_slice_header_constraint_flag;
62+ uint8_t gci_one_slice_per_pic_constraint_flag;
63+ uint8_t gci_no_rectangular_slice_constraint_flag;
64+ uint8_t gci_one_slice_per_subpic_constraint_flag;
65+ uint8_t gci_no_subpic_info_constraint_flag;
66+
67+ /* CTU and block partitioning */
68+ uint8_t gci_three_minus_max_log2_ctu_size_constraint_idc;
69+ uint8_t gci_no_partition_constraints_override_constraint_flag;
70+ uint8_t gci_no_mtt_constraint_flag;
71+ uint8_t gci_no_qtbtt_dual_tree_intra_constraint_flag;
72+
73+ /* intra */
74+ uint8_t gci_no_palette_constraint_flag;
75+ uint8_t gci_no_ibc_constraint_flag;
76+ uint8_t gci_no_isp_constraint_flag;
77+ uint8_t gci_no_mrl_constraint_flag;
78+ uint8_t gci_no_mip_constraint_flag;
79+ uint8_t gci_no_cclm_constraint_flag;
80+
81+ /* inter */
82+ uint8_t gci_no_ref_pic_resampling_constraint_flag;
83+ uint8_t gci_no_res_change_in_clvs_constraint_flag;;
84+ uint8_t gci_no_weighted_prediction_constraint_flag;
85+ uint8_t gci_no_ref_wraparound_constraint_flag;
86+ uint8_t gci_no_temporal_mvp_constraint_flag;
87+ uint8_t gci_no_sbtmvp_constraint_flag;
88+ uint8_t gci_no_amvr_constraint_flag;
89+ uint8_t gci_no_bdof_constraint_flag;
90+ uint8_t gci_no_smvd_constraint_flag;
91+ uint8_t gci_no_dmvr_constraint_flag;
92+ uint8_t gci_no_mmvd_constraint_flag;
93+ uint8_t gci_no_affine_motion_constraint_flag;
94+ uint8_t gci_no_prof_constraint_flag;
95+ uint8_t gci_no_bcw_constraint_flag;
96+ uint8_t gci_no_ciip_constraint_flag;
97+ uint8_t gci_no_gpm_constraint_flag;
98+
99+ /* transform, quantization, residual */
100+ uint8_t gci_no_luma_transform_size_64_constraint_flag;
101+ uint8_t gci_no_transform_skip_constraint_flag;
102+ uint8_t gci_no_bdpcm_constraint_flag;
103+ uint8_t gci_no_mts_constraint_flag;
104+ uint8_t gci_no_lfnst_constraint_flag;
105+ uint8_t gci_no_joint_cbcr_constraint_flag;
106+ uint8_t gci_no_sbt_constraint_flag;
107+ uint8_t gci_no_act_constraint_flag;
108+ uint8_t gci_no_explicit_scaling_list_constraint_flag;
109+ uint8_t gci_no_dep_quant_constraint_flag;
110+ uint8_t gci_no_sign_data_hiding_constraint_flag;
111+ uint8_t gci_no_cu_qp_delta_constraint_flag;
112+ uint8_t gci_no_chroma_qp_offset_constraint_flag;
113+
114+ /* loop filter */
115+ uint8_t gci_no_sao_constraint_flag;
116+ uint8_t gci_no_alf_constraint_flag;
117+ uint8_t gci_no_ccalf_constraint_flag;
118+ uint8_t gci_no_lmcs_constraint_flag;
119+ uint8_t gci_no_ladf_constraint_flag;
120+ uint8_t gci_no_virtual_boundaries_constraint_flag;
121+ uint8_t gci_num_reserved_bits;
122+ uint8_t gci_reserved_zero_bit[255];
123+} H266GeneralConstraintsInfo;
124+
125+typedef struct H266RawProfileTierLevel {
126+ uint8_t general_profile_idc;
127+ uint8_t general_tier_flag;
128+ uint8_t general_level_idc;
129+ uint8_t ptl_frame_only_constraint_flag;
130+ uint8_t ptl_multilayer_enabled_flag;
131+ H266GeneralConstraintsInfo general_constraints_info;
132+ uint8_t ptl_sublayer_level_present_flag[VVC_MAX_SUBLAYERS - 1];
133+ uint8_t sublayer_level_idc[VVC_MAX_SUBLAYERS - 1];
134+ uint8_t ptl_num_sub_profiles;
135+ uint32_t general_sub_profile_idc[VVC_MAX_SUB_PROFILES];
136+
137+ uint8_t ptl_reserved_zero_bit;
138+} H266RawProfileTierLevel;
139+
140+typedef struct H266RawExtensionData {
141+ uint8_t *data;
142+ AVBufferRef *data_ref;
143+ size_t bit_length;
144+} H266RawExtensionData;
145+
146+typedef struct H266DpbParameters {
147+ uint8_t dpb_max_dec_pic_buffering_minus1[VVC_MAX_SUBLAYERS];
148+ uint8_t dpb_max_num_reorder_pics[VVC_MAX_SUBLAYERS];
149+ uint8_t dpb_max_latency_increase_plus1[VVC_MAX_SUBLAYERS];
150+} H266DpbParameters;
151+
152+typedef struct H266RefPicListStruct {
153+ uint8_t num_ref_entries;
154+ uint8_t ltrp_in_header_flag;
155+ uint8_t inter_layer_ref_pic_flag[VVC_MAX_REF_ENTRIES];
156+ uint8_t st_ref_pic_flag[VVC_MAX_REF_ENTRIES];
157+ uint8_t abs_delta_poc_st[VVC_MAX_REF_ENTRIES];
158+ uint8_t strp_entry_sign_flag[VVC_MAX_REF_ENTRIES];
159+ uint8_t rpls_poc_lsb_lt[VVC_MAX_REF_ENTRIES];
160+ uint8_t ilrp_idx[VVC_MAX_REF_ENTRIES];
161+} H266RefPicListStruct;
162+
163+typedef struct H266RefPicLists {
164+ uint8_t rpl_sps_flag[2];
165+ uint8_t rpl_idx[2];
166+ H266RefPicListStruct rpl_ref_list[2];
167+ uint16_t poc_lsb_lt[2][VVC_MAX_REF_ENTRIES];
168+ uint8_t delta_poc_msb_cycle_present_flag[2][VVC_MAX_REF_ENTRIES];
169+ uint16_t delta_poc_msb_cycle_lt[2][VVC_MAX_REF_ENTRIES];
170+} H266RefPicLists;
171+
172+typedef struct H266RawGeneralTimingHrdParameters {
173+ uint32_t num_units_in_tick;
174+ uint32_t time_scale;
175+ uint8_t general_nal_hrd_params_present_flag;
176+ uint8_t general_vcl_hrd_params_present_flag;
177+ uint8_t general_same_pic_timing_in_all_ols_flag;
178+ uint8_t general_du_hrd_params_present_flag;
179+ uint8_t tick_divisor_minus2;
180+ uint8_t bit_rate_scale;
181+ uint8_t cpb_size_scale;
182+ uint8_t cpb_size_du_scale;
183+ uint8_t hrd_cpb_cnt_minus1;
184+} H266RawGeneralTimingHrdParameters;
185+
186+typedef struct H266RawSubLayerHRDParameters {
187+ uint32_t bit_rate_value_minus1[VVC_MAX_SUBLAYERS][VVC_MAX_CPB_CNT];
188+ uint32_t cpb_size_value_minus1[VVC_MAX_SUBLAYERS][VVC_MAX_CPB_CNT];
189+ uint32_t cpb_size_du_value_minus1[VVC_MAX_SUBLAYERS][VVC_MAX_CPB_CNT];
190+ uint32_t bit_rate_du_value_minus1[VVC_MAX_SUBLAYERS][VVC_MAX_CPB_CNT];
191+ uint8_t cbr_flag[VVC_MAX_SUBLAYERS][VVC_MAX_CPB_CNT];
192+} H266RawSubLayerHRDParameters;
193+
194+typedef struct H266RawOlsTimingHrdParameters {
195+ uint8_t fixed_pic_rate_general_flag[VVC_MAX_SUBLAYERS];
196+ uint8_t fixed_pic_rate_within_cvs_flag[VVC_MAX_SUBLAYERS];
197+ uint16_t elemental_duration_in_tc_minus1[VVC_MAX_SUBLAYERS];
198+ uint8_t low_delay_hrd_flag[VVC_MAX_SUBLAYERS];
199+ H266RawSubLayerHRDParameters nal_sub_layer_hrd_parameters;
200+ H266RawSubLayerHRDParameters vcl_sub_layer_hrd_parameters;
201+} H266RawOlsTimingHrdParameters;
202+
203+typedef struct H266RawVUI {
204+ uint8_t vui_progressive_source_flag;
205+ uint8_t vui_interlaced_source_flag;
206+ uint8_t vui_non_packed_constraint_flag;
207+ uint8_t vui_non_projected_constraint_flag;
208+
209+ uint8_t vui_aspect_ratio_info_present_flag;
210+ uint8_t vui_aspect_ratio_constant_flag;
211+ uint8_t vui_aspect_ratio_idc;
212+
213+ uint16_t vui_sar_width;
214+ uint16_t vui_sar_height;;
215+
216+ uint8_t vui_overscan_info_present_flag;
217+ uint8_t vui_overscan_appropriate_flag;
218+
219+ uint8_t vui_colour_description_present_flag;
220+ uint8_t vui_colour_primaries;
221+
222+ uint8_t vui_transfer_characteristics;
223+ uint8_t vui_matrix_coeffs;
224+ uint8_t vui_full_range_flag;
225+
226+ uint8_t vui_chroma_loc_info_present_flag;
227+ uint8_t vui_chroma_sample_loc_type_frame;
228+ uint8_t vui_chroma_sample_loc_type_top_field;
229+ uint8_t vui_chroma_sample_loc_type_bottom_field;
230+ H266RawExtensionData extension_data;
231+} H266RawVUI;
232+
233+typedef struct H266RawVPS {
234+ H266RawNALUnitHeader nal_unit_header;
235+
236+ uint8_t vps_video_parameter_set_id;
237+ uint8_t vps_max_layers_minus1;
238+ uint8_t vps_max_sublayers_minus1;
239+ uint8_t vps_default_ptl_dpb_hrd_max_tid_flag;
240+ uint8_t vps_all_independent_layers_flag;
241+ uint8_t vps_layer_id[VVC_MAX_LAYERS];
242+ uint8_t vps_independent_layer_flag[VVC_MAX_LAYERS];
243+ uint8_t vps_max_tid_ref_present_flag[VVC_MAX_LAYERS];
244+ uint8_t vps_direct_ref_layer_flag[VVC_MAX_LAYERS][VVC_MAX_LAYERS - 1];
245+ uint8_t vps_max_tid_il_ref_pics_plus1[VVC_MAX_LAYERS][VVC_MAX_LAYERS - 1];
246+ uint8_t vps_each_layer_is_an_ols_flag;
247+ uint8_t vps_ols_mode_idc;
248+ uint8_t vps_num_output_layer_sets_minus2;
249+ uint8_t vps_ols_output_layer_flag[VVC_MAX_TOTAL_NUM_OLSS][VVC_MAX_LAYERS];
250+
251+ uint8_t vps_num_ptls_minus1;
252+ uint8_t vps_pt_present_flag[VVC_MAX_PTLS];
253+ uint8_t vps_ptl_max_tid[VVC_MAX_PTLS];
254+ H266RawProfileTierLevel vps_profile_tier_level[VVC_MAX_PTLS];
255+ uint8_t vps_ols_ptl_idx[VVC_MAX_TOTAL_NUM_OLSS];
256+
257+ uint16_t vps_num_dpb_params_minus1;
258+ uint8_t vps_sublayer_dpb_params_present_flag;
259+ uint8_t vps_dpb_max_tid[VVC_MAX_TOTAL_NUM_OLSS];
260+ H266DpbParameters vps_dpb_params[VVC_MAX_TOTAL_NUM_OLSS];
261+ uint16_t vps_ols_dpb_pic_width[VVC_MAX_TOTAL_NUM_OLSS];
262+ uint16_t vps_ols_dpb_pic_height[VVC_MAX_TOTAL_NUM_OLSS];
263+ uint8_t vps_ols_dpb_chroma_format[VVC_MAX_TOTAL_NUM_OLSS];
264+ uint8_t vps_ols_dpb_bitdepth_minus8[VVC_MAX_TOTAL_NUM_OLSS];
265+ uint16_t vps_ols_dpb_params_idx[VVC_MAX_TOTAL_NUM_OLSS];
266+
267+ uint8_t vps_timing_hrd_params_present_flag;
268+ H266RawGeneralTimingHrdParameters vps_general_timing_hrd_parameters;
269+ uint8_t vps_sublayer_cpb_params_present_flag;
270+ uint16_t vps_num_ols_timing_hrd_params_minus1;
271+ uint8_t vps_hrd_max_tid[VVC_MAX_TOTAL_NUM_OLSS];
272+ H266RawOlsTimingHrdParameters vps_ols_timing_hrd_parameters;
273+ uint8_t vps_ols_timing_hrd_idx[VVC_MAX_TOTAL_NUM_OLSS];
274+
275+ uint8_t vps_extension_flag;
276+ H266RawExtensionData extension_data;
277+} H266RawVPS;
278+
279+typedef struct H266RawSPS {
280+ H266RawNALUnitHeader nal_unit_header;
281+
282+ uint8_t sps_seq_parameter_set_id;
283+ uint8_t sps_video_parameter_set_id;
284+ uint8_t sps_max_sublayers_minus1;
285+ uint8_t sps_chroma_format_idc;
286+ uint8_t sps_log2_ctu_size_minus5;
287+ uint8_t sps_ptl_dpb_hrd_params_present_flag;
288+ H266RawProfileTierLevel profile_tier_level;
289+ uint8_t sps_gdr_enabled_flag;
290+ uint8_t sps_ref_pic_resampling_enabled_flag;
291+ uint8_t sps_res_change_in_clvs_allowed_flag;
292+
293+ uint16_t sps_pic_width_max_in_luma_samples;
294+ uint16_t sps_pic_height_max_in_luma_samples;
295+
296+ uint8_t sps_conformance_window_flag;
297+ uint16_t sps_conf_win_left_offset;
298+ uint16_t sps_conf_win_right_offset;
299+ uint16_t sps_conf_win_top_offset;
300+ uint16_t sps_conf_win_bottom_offset;
301+
302+ uint8_t sps_subpic_info_present_flag;
303+ uint16_t sps_num_subpics_minus1;
304+ uint8_t sps_independent_subpics_flag;
305+ uint8_t sps_subpic_same_size_flag;
306+ uint16_t sps_subpic_ctu_top_left_x[VVC_MAX_SLICES];
307+ uint16_t sps_subpic_ctu_top_left_y[VVC_MAX_SLICES];
308+ uint16_t sps_subpic_width_minus1[VVC_MAX_SLICES];
309+ uint16_t sps_subpic_height_minus1[VVC_MAX_SLICES];
310+ uint8_t sps_subpic_treated_as_pic_flag[VVC_MAX_SLICES];
311+ uint8_t sps_loop_filter_across_subpic_enabled_flag[VVC_MAX_SLICES];
312+ uint8_t sps_subpic_id_len_minus1;
313+ uint8_t sps_subpic_id_mapping_explicitly_signalled_flag;
314+ uint8_t sps_subpic_id_mapping_present_flag;
315+ uint32_t sps_subpic_id[VVC_MAX_SLICES];
316+
317+
318+ uint8_t sps_bitdepth_minus8;
319+ uint8_t sps_entropy_coding_sync_enabled_flag;
320+ uint8_t sps_entry_point_offsets_present_flag;
321+
322+ uint8_t sps_log2_max_pic_order_cnt_lsb_minus4;
323+ uint8_t sps_poc_msb_cycle_flag;
324+ uint8_t sps_poc_msb_cycle_len_minus1;
325+
326+ uint8_t sps_num_extra_ph_bytes;
327+ uint8_t sps_extra_ph_bit_present_flag[16];
328+
329+ uint8_t sps_num_extra_sh_bytes;
330+ uint8_t sps_extra_sh_bit_present_flag[16];
331+
332+ uint8_t sps_sublayer_dpb_params_flag;
333+ H266DpbParameters sps_dpb_params;
334+
335+ uint8_t sps_log2_min_luma_coding_block_size_minus2;
336+ uint8_t sps_partition_constraints_override_enabled_flag;
337+ uint8_t sps_log2_diff_min_qt_min_cb_intra_slice_luma;
338+ uint8_t sps_max_mtt_hierarchy_depth_intra_slice_luma;
339+ uint8_t sps_log2_diff_max_bt_min_qt_intra_slice_luma;
340+ uint8_t sps_log2_diff_max_tt_min_qt_intra_slice_luma;
341+
342+ uint8_t sps_qtbtt_dual_tree_intra_flag;
343+ uint8_t sps_log2_diff_min_qt_min_cb_intra_slice_chroma;
344+ uint8_t sps_max_mtt_hierarchy_depth_intra_slice_chroma;
345+ uint8_t sps_log2_diff_max_bt_min_qt_intra_slice_chroma;
346+ uint8_t sps_log2_diff_max_tt_min_qt_intra_slice_chroma;
347+
348+ uint8_t sps_log2_diff_min_qt_min_cb_inter_slice;
349+ uint8_t sps_max_mtt_hierarchy_depth_inter_slice;
350+ uint8_t sps_log2_diff_max_bt_min_qt_inter_slice;
351+ uint8_t sps_log2_diff_max_tt_min_qt_inter_slice;
352+
353+ uint8_t sps_max_luma_transform_size_64_flag;
354+
355+ uint8_t sps_transform_skip_enabled_flag;
356+ uint8_t sps_log2_transform_skip_max_size_minus2;
357+ uint8_t sps_bdpcm_enabled_flag;
358+
359+ uint8_t sps_mts_enabled_flag;
360+ uint8_t sps_explicit_mts_intra_enabled_flag;
361+ uint8_t sps_explicit_mts_inter_enabled_flag;
362+
363+ uint8_t sps_lfnst_enabled_flag;
364+
365+ uint8_t sps_joint_cbcr_enabled_flag;
366+ uint8_t sps_same_qp_table_for_chroma_flag;
367+
368+ int8_t sps_qp_table_start_minus26[VVC_MAX_SAMPLE_ARRAYS];
369+ uint8_t sps_num_points_in_qp_table_minus1[VVC_MAX_SAMPLE_ARRAYS];
370+ uint8_t sps_delta_qp_in_val_minus1[VVC_MAX_SAMPLE_ARRAYS][VVC_MAX_POINTS_IN_QP_TABLE];
371+ uint8_t sps_delta_qp_diff_val[VVC_MAX_SAMPLE_ARRAYS][VVC_MAX_POINTS_IN_QP_TABLE];
372+
373+ uint8_t sps_sao_enabled_flag;
374+ uint8_t sps_alf_enabled_flag;
375+ uint8_t sps_ccalf_enabled_flag;
376+ uint8_t sps_lmcs_enabled_flag;
377+ uint8_t sps_weighted_pred_flag;
378+ uint8_t sps_weighted_bipred_flag;
379+ uint8_t sps_long_term_ref_pics_flag;
380+ uint8_t sps_inter_layer_prediction_enabled_flag;
381+ uint8_t sps_idr_rpl_present_flag;
382+ uint8_t sps_rpl1_same_as_rpl0_flag;
383+
384+ uint8_t sps_num_ref_pic_lists[2];
385+ H266RefPicListStruct sps_ref_pic_list_struct[2][VVC_MAX_REF_PIC_LISTS];
386+
387+ uint8_t sps_ref_wraparound_enabled_flag;
388+ uint8_t sps_temporal_mvp_enabled_flag;
389+ uint8_t sps_sbtmvp_enabled_flag;
390+ uint8_t sps_amvr_enabled_flag;
391+ uint8_t sps_bdof_enabled_flag;
392+ uint8_t sps_bdof_control_present_in_ph_flag;
393+ uint8_t sps_smvd_enabled_flag;
394+ uint8_t sps_dmvr_enabled_flag;
395+ uint8_t sps_dmvr_control_present_in_ph_flag;
396+ uint8_t sps_mmvd_enabled_flag;
397+ uint8_t sps_mmvd_fullpel_only_enabled_flag;
398+ uint8_t sps_six_minus_max_num_merge_cand;
399+ uint8_t sps_sbt_enabled_flag;
400+ uint8_t sps_affine_enabled_flag;
401+ uint8_t sps_five_minus_max_num_subblock_merge_cand;
402+ uint8_t sps_6param_affine_enabled_flag;
403+ uint8_t sps_affine_amvr_enabled_flag;
404+ uint8_t sps_affine_prof_enabled_flag;
405+ uint8_t sps_prof_control_present_in_ph_flag;
406+ uint8_t sps_bcw_enabled_flag;
407+ uint8_t sps_ciip_enabled_flag;
408+ uint8_t sps_gpm_enabled_flag;
409+ uint8_t sps_max_num_merge_cand_minus_max_num_gpm_cand;
410+ uint8_t sps_log2_parallel_merge_level_minus2;
411+ uint8_t sps_isp_enabled_flag;
412+ uint8_t sps_mrl_enabled_flag;
413+ uint8_t sps_mip_enabled_flag;
414+ uint8_t sps_cclm_enabled_flag;
415+ uint8_t sps_chroma_horizontal_collocated_flag;
416+ uint8_t sps_chroma_vertical_collocated_flag;
417+ uint8_t sps_palette_enabled_flag;
418+ uint8_t sps_act_enabled_flag;
419+ uint8_t sps_min_qp_prime_ts;
420+ uint8_t sps_ibc_enabled_flag;
421+ uint8_t sps_six_minus_max_num_ibc_merge_cand;
422+ uint8_t sps_ladf_enabled_flag;
423+ uint8_t sps_num_ladf_intervals_minus2;
424+ int8_t sps_ladf_lowest_interval_qp_offset;
425+ int8_t sps_ladf_qp_offset[4];
426+ uint16_t sps_ladf_delta_threshold_minus1[4];
427+
428+ uint8_t sps_explicit_scaling_list_enabled_flag;
429+ uint8_t sps_scaling_matrix_for_lfnst_disabled_flag;
430+ uint8_t sps_scaling_matrix_for_alternative_colour_space_disabled_flag;
431+ uint8_t sps_scaling_matrix_designated_colour_space_flag;
432+ uint8_t sps_dep_quant_enabled_flag;
433+ uint8_t sps_sign_data_hiding_enabled_flag;
434+
435+ uint8_t sps_virtual_boundaries_enabled_flag;
436+ uint8_t sps_virtual_boundaries_present_flag;
437+ uint8_t sps_num_ver_virtual_boundaries;
438+ uint16_t sps_virtual_boundary_pos_x_minus1[3];
439+ uint8_t sps_num_hor_virtual_boundaries;
440+ uint16_t sps_virtual_boundary_pos_y_minus1[3];
441+
442+ uint8_t sps_timing_hrd_params_present_flag;
443+ uint8_t sps_sublayer_cpb_params_present_flag;
444+ H266RawGeneralTimingHrdParameters sps_general_timing_hrd_parameters;
445+ H266RawOlsTimingHrdParameters sps_ols_timing_hrd_parameters;
446+
447+ uint8_t sps_field_seq_flag;
448+ uint8_t sps_vui_parameters_present_flag;
449+ uint16_t sps_vui_payload_size_minus1;
450+ H266RawVUI vui;
451+
452+ uint8_t sps_extension_flag;
453+
454+ H266RawExtensionData extension_data;
455+
456+} H266RawSPS;
457+
458+typedef struct H266RawPPS {
459+ H266RawNALUnitHeader nal_unit_header;
460+
461+ uint8_t pps_pic_parameter_set_id;
462+ uint8_t pps_seq_parameter_set_id;
463+ uint8_t pps_mixed_nalu_types_in_pic_flag;
464+ uint16_t pps_pic_width_in_luma_samples;
465+ uint16_t pps_pic_height_in_luma_samples;
466+
467+ uint8_t pps_conformance_window_flag;
468+ uint16_t pps_conf_win_left_offset;
469+ uint16_t pps_conf_win_right_offset;
470+ uint16_t pps_conf_win_top_offset;
471+ uint16_t pps_conf_win_bottom_offset;
472+
473+ uint8_t pps_scaling_window_explicit_signalling_flag;
474+ int16_t pps_scaling_win_left_offset;
475+ int16_t pps_scaling_win_right_offset;
476+ int16_t pps_scaling_win_top_offset;
477+ int16_t pps_scaling_win_bottom_offset;
478+
479+ uint8_t pps_output_flag_present_flag;
480+ uint8_t pps_no_pic_partition_flag;
481+
482+ uint8_t pps_subpic_id_mapping_present_flag;
483+ uint16_t pps_num_subpics_minus1;
484+ uint8_t pps_subpic_id_len_minus1;
485+ uint16_t pps_subpic_id[VVC_MAX_SLICES];
486+
487+ uint8_t pps_log2_ctu_size_minus5;
488+ uint8_t pps_num_exp_tile_columns_minus1;
489+ uint8_t pps_num_exp_tile_rows_minus1;
490+ uint16_t pps_tile_column_width_minus1[VVC_MAX_TILE_COLUMNS];
491+ uint16_t pps_tile_row_height_minus1[VVC_MAX_TILE_ROWS];
492+
493+ uint8_t pps_loop_filter_across_tiles_enabled_flag;
494+ uint8_t pps_rect_slice_flag;
495+ uint8_t pps_single_slice_per_subpic_flag;
496+
497+ uint16_t pps_num_slices_in_pic_minus1;
498+ uint8_t pps_tile_idx_delta_present_flag;
499+ uint16_t pps_slice_width_in_tiles_minus1[VVC_MAX_SLICES];
500+ uint16_t pps_slice_height_in_tiles_minus1[VVC_MAX_SLICES];
501+ uint16_t pps_num_exp_slices_in_tile[VVC_MAX_SLICES];
502+ uint16_t pps_exp_slice_height_in_ctus_minus1[VVC_MAX_SLICES][VVC_MAX_TILE_ROWS];
503+ int16_t pps_tile_idx_delta_val[VVC_MAX_SLICES];
504+
505+ uint8_t pps_loop_filter_across_slices_enabled_flag;
506+ uint8_t pps_cabac_init_present_flag;
507+ uint8_t pps_num_ref_idx_default_active_minus1[2];
508+ uint8_t pps_rpl1_idx_present_flag;
509+ uint8_t pps_weighted_pred_flag;
510+ uint8_t pps_weighted_bipred_flag;
511+ uint8_t pps_ref_wraparound_enabled_flag;
512+ uint16_t pps_pic_width_minus_wraparound_offset;
513+ int8_t pps_init_qp_minus26;
514+ uint8_t pps_cu_qp_delta_enabled_flag;
515+ uint8_t pps_chroma_tool_offsets_present_flag;
516+ int8_t pps_cb_qp_offset;
517+ int8_t pps_cr_qp_offset;
518+ uint8_t pps_joint_cbcr_qp_offset_present_flag;
519+ int8_t pps_joint_cbcr_qp_offset_value;
520+ uint8_t pps_slice_chroma_qp_offsets_present_flag;
521+ uint8_t pps_cu_chroma_qp_offset_list_enabled_flag;
522+ uint8_t pps_chroma_qp_offset_list_len_minus1;
523+ uint8_t pps_cb_qp_offset_list[6];
524+ uint8_t pps_cr_qp_offset_list[6];
525+ uint8_t pps_joint_cbcr_qp_offset_list[6];
526+ uint8_t pps_deblocking_filter_control_present_flag;
527+ uint8_t pps_deblocking_filter_override_enabled_flag;
528+ uint8_t pps_deblocking_filter_disabled_flag;
529+ uint8_t pps_dbf_info_in_ph_flag;
530+
531+ int8_t pps_luma_beta_offset_div2;
532+ int8_t pps_luma_tc_offset_div2;
533+ int8_t pps_cb_beta_offset_div2;
534+ int8_t pps_cb_tc_offset_div2;
535+ int8_t pps_cr_beta_offset_div2;
536+ int8_t pps_cr_tc_offset_div2;
537+
538+ uint8_t pps_rpl_info_in_ph_flag;
539+ uint8_t pps_sao_info_in_ph_flag;
540+ uint8_t pps_alf_info_in_ph_flag;
541+ uint8_t pps_wp_info_in_ph_flag;
542+ uint8_t pps_qp_delta_info_in_ph_flag;
543+
544+ uint8_t pps_picture_header_extension_present_flag;
545+ uint8_t pps_slice_header_extension_present_flag;
546+ uint8_t pps_extension_flag;
547+ H266RawExtensionData extension_data;
548+
549+ //calculated value;
550+ uint16_t num_tile_columns;
551+ uint16_t num_tile_rows;
552+ uint16_t num_tiles_in_pic;
553+ uint16_t slice_height_in_ctus[VVC_MAX_SLICES];
554+ uint16_t num_slices_in_subpic[VVC_MAX_SLICES];
555+} H266RawPPS;
556+
557+typedef struct H266RawAUD {
558+ H266RawNALUnitHeader nal_unit_header;
559+ uint8_t aud_irap_or_gdr_flag;
560+ uint8_t aud_pic_type;
561+} H266RawAUD;
562+
563+typedef struct H266RawPredWeightTable {
564+ uint8_t luma_log2_weight_denom;
565+ int8_t delta_chroma_log2_weight_denom;
566+
567+ uint8_t num_l0_weights;
568+ uint8_t luma_weight_l0_flag[15];
569+ uint8_t chroma_weight_l0_flag[15];
570+ int8_t delta_luma_weight_l0[15];
571+ int8_t luma_offset_l0[15];
572+ int8_t delta_chroma_weight_l0[15][2];
573+ int16_t delta_chroma_offset_l0[15][2];
574+
575+ uint8_t num_l1_weights;
576+ uint8_t luma_weight_l1_flag[15];
577+ uint8_t chroma_weight_l1_flag[15];
578+ int8_t delta_luma_weight_l1[15];
579+ int8_t luma_offset_l1[15];
580+ int8_t delta_chroma_weight_l1[15][2];
581+ int16_t delta_chroma_offset_l1[15][2];
582+} H266RawPredWeightTable;
583+
584+typedef struct H266RawPH {
585+ H266RawNALUnitHeader nal_unit_header;
586+ uint8_t ph_gdr_or_irap_pic_flag;
587+ uint8_t ph_non_ref_pic_flag;
588+ uint8_t ph_gdr_pic_flag;
589+ uint8_t ph_inter_slice_allowed_flag;
590+ uint8_t ph_intra_slice_allowed_flag;
591+ uint8_t ph_pic_parameter_set_id;
592+ uint16_t ph_pic_order_cnt_lsb;
593+ uint8_t ph_recovery_poc_cnt;
594+ uint8_t ph_extra_bit[16];
595+ uint8_t ph_poc_msb_cycle_present_flag;
596+ uint8_t ph_poc_msb_cycle_val;
597+
598+ uint8_t ph_alf_enabled_flag;
599+ uint8_t ph_num_alf_aps_ids_luma;
600+ uint8_t ph_alf_aps_id_luma[8];
601+ uint8_t ph_alf_cb_enabled_flag;
602+ uint8_t ph_alf_cr_enabled_flag;
603+ uint8_t ph_alf_aps_id_chroma;
604+ uint8_t ph_alf_cc_cb_enabled_flag;
605+ uint8_t ph_alf_cc_cb_aps_id;
606+ uint8_t ph_alf_cc_cr_enabled_flag;
607+ uint8_t ph_alf_cc_cr_aps_id;
608+
609+ uint8_t ph_lmcs_enabled_flag;
610+ uint8_t ph_lmcs_aps_id;
611+ uint8_t ph_chroma_residual_scale_flag;
612+ uint8_t ph_explicit_scaling_list_enabled_flag;
613+ uint8_t ph_scaling_list_aps_id;
614+
615+ uint8_t ph_virtual_boundaries_present_flag;
616+ uint8_t ph_num_ver_virtual_boundaries;
617+ uint16_t ph_virtual_boundary_pos_x_minus1[3];
618+ uint8_t ph_num_hor_virtual_boundaries;
619+ uint16_t ph_virtual_boundary_pos_y_minus1[3];
620+
621+ uint8_t ph_pic_output_flag;
622+ H266RefPicLists ph_ref_pic_lists;
623+
624+ uint8_t ph_partition_constraints_override_flag;
625+
626+ uint8_t ph_log2_diff_min_qt_min_cb_intra_slice_luma;
627+ uint8_t ph_max_mtt_hierarchy_depth_intra_slice_luma;
628+ uint8_t ph_log2_diff_max_bt_min_qt_intra_slice_luma;
629+ uint8_t ph_log2_diff_max_tt_min_qt_intra_slice_luma;
630+ uint8_t ph_log2_diff_min_qt_min_cb_intra_slice_chroma;
631+
632+ uint8_t ph_max_mtt_hierarchy_depth_intra_slice_chroma;
633+ uint8_t ph_log2_diff_max_bt_min_qt_intra_slice_chroma;
634+ uint8_t ph_log2_diff_max_tt_min_qt_intra_slice_chroma;
635+
636+ uint8_t ph_cu_qp_delta_subdiv_intra_slice;
637+ uint8_t ph_cu_chroma_qp_offset_subdiv_intra_slice;
638+
639+ uint8_t ph_log2_diff_min_qt_min_cb_inter_slice;
640+ uint8_t ph_max_mtt_hierarchy_depth_inter_slice;
641+ uint8_t ph_log2_diff_max_bt_min_qt_inter_slice;
642+ uint8_t ph_log2_diff_max_tt_min_qt_inter_slice;
643+ uint8_t ph_cu_qp_delta_subdiv_inter_slice;
644+ uint8_t ph_cu_chroma_qp_offset_subdiv_inter_slice;
645+
646+ uint8_t ph_temporal_mvp_enabled_flag;
647+ uint8_t ph_collocated_from_l0_flag;
648+ uint8_t ph_collocated_ref_idx;
649+ uint8_t ph_mmvd_fullpel_only_flag;
650+ uint8_t ph_mvd_l1_zero_flag;
651+ uint8_t ph_bdof_disabled_flag;
652+ uint8_t ph_dmvr_disabled_flag;
653+ uint8_t ph_prof_disabled_flag;
654+
655+ H266RawPredWeightTable ph_pred_weight_table;
656+
657+ int8_t ph_qp_delta;
658+ uint8_t ph_joint_cbcr_sign_flag;
659+ uint8_t ph_sao_luma_enabled_flag;
660+ uint8_t ph_sao_chroma_enabled_flag;
661+
662+ uint8_t ph_deblocking_params_present_flag;
663+ uint8_t ph_deblocking_filter_disabled_flag;
664+ int8_t ph_luma_beta_offset_div2;
665+ int8_t ph_luma_tc_offset_div2;
666+ int8_t ph_cb_beta_offset_div2;
667+ int8_t ph_cb_tc_offset_div2;
668+ int8_t ph_cr_beta_offset_div2;
669+ int8_t ph_cr_tc_offset_div2;
670+
671+ uint8_t ph_extension_length;
672+ uint8_t ph_extension_data_byte[256];
673+} H266RawPH;
674+
675+typedef struct H266RawSliceHeader {
676+ H266RawNALUnitHeader nal_unit_header;
677+ uint8_t sh_picture_header_in_slice_header_flag;
678+ H266RawPH sh_picture_header;
679+
680+ uint16_t sh_subpic_id;
681+ uint16_t sh_slice_address;
682+ uint8_t sh_extra_bit[16];
683+ uint8_t sh_num_tiles_in_slice_minus1;
684+ uint8_t sh_slice_type;
685+ uint8_t sh_no_output_of_prior_pics_flag;
686+
687+ uint8_t sh_alf_enabled_flag;
688+ uint8_t sh_num_alf_aps_ids_luma;
689+ uint8_t sh_alf_aps_id_luma[8];
690+ uint8_t sh_alf_cb_enabled_flag;
691+ uint8_t sh_alf_cr_enabled_flag;
692+ uint8_t sh_alf_aps_id_chroma;
693+ uint8_t sh_alf_cc_cb_enabled_flag;
694+ uint8_t sh_alf_cc_cb_aps_id;
695+ uint8_t sh_alf_cc_cr_enabled_flag;
696+ uint8_t sh_alf_cc_cr_aps_id;
697+
698+ uint8_t sh_lmcs_used_flag;
699+ uint8_t sh_explicit_scaling_list_used_flag;
700+
701+ H266RefPicLists sh_ref_pic_lists;
702+
703+ uint8_t sh_num_ref_idx_active_override_flag;
704+ uint8_t sh_num_ref_idx_active_minus1[2];
705+ uint8_t sh_cabac_init_flag;
706+ uint8_t sh_collocated_from_l0_flag;
707+ uint8_t sh_collocated_ref_idx;
708+
709+ H266RawPredWeightTable sh_pred_weight_table;
710+
711+ int8_t sh_qp_delta;
712+ int8_t sh_cb_qp_offset;
713+ int8_t sh_cr_qp_offset;
714+ int8_t sh_joint_cbcr_qp_offset;
715+ uint8_t sh_cu_chroma_qp_offset_enabled_flag;
716+
717+ uint8_t sh_sao_luma_used_flag;
718+ uint8_t sh_sao_chroma_used_flag;
719+
720+ uint8_t sh_deblocking_params_present_flag;
721+ uint8_t sh_deblocking_filter_disabled_flag;
722+ int8_t sh_luma_beta_offset_div2;
723+ int8_t sh_luma_tc_offset_div2;
724+ int8_t sh_cb_beta_offset_div2;
725+ int8_t sh_cb_tc_offset_div2;
726+ int8_t sh_cr_beta_offset_div2;
727+ int8_t sh_cr_tc_offset_div2;
728+ uint8_t sh_dep_quant_used_flag;
729+
730+ uint8_t sh_sign_data_hiding_used_flag;
731+ uint8_t sh_ts_residual_coding_disabled_flag;
732+ uint16_t sh_slice_header_extension_length;
733+ uint8_t sh_slice_header_extension_data_byte[256];
734+
735+ uint8_t sh_entry_offset_len_minus1;
736+ uint32_t sh_entry_point_offset_minus1[VVC_MAX_ENTRY_POINTS];
737+
738+} H266RawSliceHeader;
739+
740+typedef struct H266RawSlice {
741+ H266RawSliceHeader header;
742+
743+ uint8_t *data;
744+ AVBufferRef *data_ref;
745+ size_t data_size;
746+ int data_bit_start;
747+} H266RawSlice;
748+
749+typedef struct H266RawSEIDecodedPictureHash {
750+ uint8_t dph_sei_hash_type;
751+ uint8_t dph_sei_single_component_flag;
752+ uint8_t dph_sei_picture_md5[3][16];
753+ uint16_t dph_sei_picture_crc[3];
754+ uint32_t dph_sei_picture_checksum[3];
755+
756+ uint8_t dph_sei_reserved_zero_7bits;
757+} H266RawSEIDecodedPictureHash;
758+
759+typedef struct H266RawSEI {
760+ H266RawNALUnitHeader nal_unit_header;
761+ SEIRawMessageList message_list;
762+} H266RawSEI;
763+
764+typedef struct CodedBitstreamH266Context {
765+ // Reader/writer context in common with the H.264 implementation.
766+ CodedBitstreamH2645Context common;
767+
768+ // All currently available parameter sets. These are updated when
769+ // any parameter set NAL unit is read/written with this context.
770+ AVBufferRef *vps_ref[VVC_MAX_VPS_COUNT];
771+ AVBufferRef *sps_ref[VVC_MAX_SPS_COUNT];
772+ AVBufferRef *pps_ref[VVC_MAX_PPS_COUNT];
773+ H266RawVPS *vps[VVC_MAX_SPS_COUNT];
774+ H266RawSPS *sps[VVC_MAX_SPS_COUNT];
775+ H266RawPPS *pps[VVC_MAX_PPS_COUNT];
776+
777+ struct {
778+ AVBufferRef *ph_ref;
779+ H266RawPH *ph;
780+ } priv;
781+} CodedBitstreamH266Context;
782+
783+#endif /* AVCODEC_CBS_H266_H */
libavcodec/cbs_h266_syntax_template.c+3116−0
@@ -0,0 +1,3116 @@
1+/*
2+ * This file is part of FFmpeg.
3+ *
4+ * FFmpeg is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Lesser General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2.1 of the License, or (at your option) any later version.
8+ *
9+ * FFmpeg is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * Lesser General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Lesser General Public
15+ * License along with FFmpeg; if not, write to the Free Software
16+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+ */
18+
19+static int FUNC(rbsp_trailing_bits) (CodedBitstreamContext *ctx,
20+ RWContext *rw)
21+{
22+ int err;
23+
24+ fixed(1, rbsp_stop_one_bit, 1);
25+ while (byte_alignment(rw) != 0)
26+ fixed(1, rbsp_alignment_zero_bit, 0);
27+ return 0;
28+}
29+
30+static int FUNC(nal_unit_header) (CodedBitstreamContext *ctx, RWContext *rw,
31+ H266RawNALUnitHeader *current,
32+ int expected_nal_unit_type)
33+{
34+ int err;
35+
36+ fixed(1, forbidden_zero_bit, 0);
37+ flag(nuh_reserved_zero_bit);
38+
39+ u(6, nuh_layer_id, 0, 55);
40+
41+ if (expected_nal_unit_type >= 0)
42+ u(5, nal_unit_type, expected_nal_unit_type, expected_nal_unit_type);
43+ else
44+ ub(5, nal_unit_type);
45+
46+ u(3, nuh_temporal_id_plus1, 1, 7);
47+ return 0;
48+}
49+
50+static int FUNC(byte_alignment) (CodedBitstreamContext *ctx, RWContext *rw)
51+{
52+ int err;
53+
54+ fixed(1, byte_alignment_bit_equal_to_one, 1);
55+ while (byte_alignment(rw) != 0)
56+ fixed(1, byte_alignment_bit_equal_to_zero, 0);
57+ return 0;
58+}
59+
60+static int FUNC(general_constraints_info) (CodedBitstreamContext *ctx,
61+ RWContext *rw,
62+ H266GeneralConstraintsInfo *current)
63+{
64+ int err, i;
65+
66+ flag(gci_present_flag);
67+ if (current->gci_present_flag) {
68+ /* general */
69+ flag(gci_intra_only_constraint_flag);
70+ flag(gci_all_layers_independent_constraint_flag);
71+ flag(gci_one_au_only_constraint_flag);
72+
73+ /* picture format */
74+ u(4, gci_sixteen_minus_max_bitdepth_constraint_idc, 0, 8);
75+ ub(2, gci_three_minus_max_chroma_format_constraint_idc);
76+
77+ /* NAL unit type related */
78+ flag(gci_no_mixed_nalu_types_in_pic_constraint_flag);
79+ flag(gci_no_trail_constraint_flag);
80+ flag(gci_no_stsa_constraint_flag);
81+ flag(gci_no_rasl_constraint_flag);
82+ flag(gci_no_radl_constraint_flag);
83+ flag(gci_no_idr_constraint_flag);
84+ flag(gci_no_cra_constraint_flag);
85+ flag(gci_no_gdr_constraint_flag);
86+ flag(gci_no_aps_constraint_flag);
87+ flag(gci_no_idr_rpl_constraint_flag);
88+
89+ /* tile, slice, subpicture partitioning */
90+ flag(gci_one_tile_per_pic_constraint_flag);
91+ flag(gci_pic_header_in_slice_header_constraint_flag);
92+ flag(gci_one_slice_per_pic_constraint_flag);
93+ flag(gci_no_rectangular_slice_constraint_flag);
94+ flag(gci_one_slice_per_subpic_constraint_flag);
95+ flag(gci_no_subpic_info_constraint_flag);
96+
97+ /* CTU and block partitioning */
98+ ub(2, gci_three_minus_max_log2_ctu_size_constraint_idc);
99+ flag(gci_no_partition_constraints_override_constraint_flag);
100+ flag(gci_no_mtt_constraint_flag);
101+ flag(gci_no_qtbtt_dual_tree_intra_constraint_flag);
102+
103+ /* intra */
104+ flag(gci_no_palette_constraint_flag);
105+ flag(gci_no_ibc_constraint_flag);
106+ flag(gci_no_isp_constraint_flag);
107+ flag(gci_no_mrl_constraint_flag);
108+ flag(gci_no_mip_constraint_flag);
109+ flag(gci_no_cclm_constraint_flag);
110+
111+ /* inter */
112+ flag(gci_no_ref_pic_resampling_constraint_flag);
113+ flag(gci_no_res_change_in_clvs_constraint_flag);
114+ flag(gci_no_weighted_prediction_constraint_flag);
115+ flag(gci_no_ref_wraparound_constraint_flag);
116+ flag(gci_no_temporal_mvp_constraint_flag);
117+ flag(gci_no_sbtmvp_constraint_flag);
118+ flag(gci_no_amvr_constraint_flag);
119+ flag(gci_no_bdof_constraint_flag);
120+ flag(gci_no_smvd_constraint_flag);
121+ flag(gci_no_dmvr_constraint_flag);
122+ flag(gci_no_mmvd_constraint_flag);
123+ flag(gci_no_affine_motion_constraint_flag);
124+ flag(gci_no_prof_constraint_flag);
125+ flag(gci_no_bcw_constraint_flag);
126+ flag(gci_no_ciip_constraint_flag);
127+ flag(gci_no_gpm_constraint_flag);
128+
129+ /* transform, quantization, residual */
130+ flag(gci_no_luma_transform_size_64_constraint_flag);
131+ flag(gci_no_transform_skip_constraint_flag);
132+ flag(gci_no_bdpcm_constraint_flag);
133+ flag(gci_no_mts_constraint_flag);
134+ flag(gci_no_lfnst_constraint_flag);
135+ flag(gci_no_joint_cbcr_constraint_flag);
136+ flag(gci_no_sbt_constraint_flag);
137+ flag(gci_no_act_constraint_flag);
138+ flag(gci_no_explicit_scaling_list_constraint_flag);
139+ flag(gci_no_dep_quant_constraint_flag);
140+ flag(gci_no_sign_data_hiding_constraint_flag);
141+ flag(gci_no_cu_qp_delta_constraint_flag);
142+ flag(gci_no_chroma_qp_offset_constraint_flag);
143+
144+ /* loop filter */
145+ flag(gci_no_sao_constraint_flag);
146+ flag(gci_no_alf_constraint_flag);
147+ flag(gci_no_ccalf_constraint_flag);
148+ flag(gci_no_lmcs_constraint_flag);
149+ flag(gci_no_ladf_constraint_flag);
150+ flag(gci_no_virtual_boundaries_constraint_flag);
151+ ub(8, gci_num_reserved_bits);
152+ for (i = 0; i < current->gci_num_reserved_bits; i++) {
153+ flags(gci_reserved_zero_bit[i], 1, i);
154+ }
155+ }
156+ while (byte_alignment(rw) != 0)
157+ fixed(1, gci_alignment_zero_bit, 0);
158+ return 0;
159+}
160+
161+static int FUNC(profile_tier_level) (CodedBitstreamContext *ctx,
162+ RWContext *rw,
163+ H266RawProfileTierLevel *current,
164+ int profile_tier_present_flag,
165+ int max_num_sub_layers_minus1)
166+{
167+ int err, i;
168+
169+ if (profile_tier_present_flag) {
170+ ub(7, general_profile_idc);
171+ flag(general_tier_flag);
172+ }
173+ ub(8, general_level_idc);
174+ flag(ptl_frame_only_constraint_flag);
175+ flag(ptl_multilayer_enabled_flag);
176+ if (profile_tier_present_flag) {
177+ CHECK(FUNC(general_constraints_info) (ctx, rw,
178+ &current->
179+ general_constraints_info));
180+ }
181+ for (i = max_num_sub_layers_minus1 - 1; i >= 0; i--)
182+ flags(ptl_sublayer_level_present_flag[i], 1, i);
183+ while (byte_alignment(rw) != 0)
184+ flag(ptl_reserved_zero_bit);
185+ for (i = max_num_sub_layers_minus1 - 1; i >= 0; i--)
186+ if (current->ptl_sublayer_level_present_flag[i])
187+ ubs(8, sublayer_level_idc[i], 1, i);
188+ if (profile_tier_present_flag) {
189+ ub(8, ptl_num_sub_profiles);
190+ for (i = 0; i < current->ptl_num_sub_profiles; i++)
191+ ubs(32, general_sub_profile_idc[i], 1, i);
192+ }
193+ return 0;
194+}
195+
196+static int FUNC(vui_parameters_default) (CodedBitstreamContext *ctx,
197+ RWContext *rw, H266RawVUI *current)
198+{
199+ //defined in D.8
200+ infer(vui_progressive_source_flag, 0);
201+ infer(vui_interlaced_source_flag, 0);
202+
203+ infer(vui_non_packed_constraint_flag, 0);
204+ infer(vui_non_projected_constraint_flag, 0);
205+
206+ infer(vui_aspect_ratio_constant_flag, 0);
207+ infer(vui_aspect_ratio_idc, 0);
208+
209+ infer(vui_overscan_info_present_flag, 0);
210+
211+ infer(vui_colour_primaries, 2);
212+ infer(vui_transfer_characteristics, 2);
213+ infer(vui_matrix_coeffs, 2);
214+ infer(vui_full_range_flag, 0);
215+
216+ infer(vui_chroma_sample_loc_type_frame, 6);
217+ infer(vui_chroma_sample_loc_type_top_field, 6);
218+ infer(vui_chroma_sample_loc_type_bottom_field, 6);
219+ return 0;
220+}
221+
222+static int FUNC(vui_parameters) (CodedBitstreamContext *ctx, RWContext *rw,
223+ H266RawVUI *current,
224+ uint8_t chroma_format_idc)
225+{
226+ int err;
227+
228+ flag(vui_progressive_source_flag);
229+ flag(vui_interlaced_source_flag);
230+ flag(vui_non_packed_constraint_flag);
231+ flag(vui_non_projected_constraint_flag);
232+ flag(vui_aspect_ratio_info_present_flag);
233+ if (current->vui_aspect_ratio_info_present_flag) {
234+ flag(vui_aspect_ratio_constant_flag);
235+ ub(8, vui_aspect_ratio_idc);
236+ if (current->vui_aspect_ratio_idc == 255) {
237+ ub(16, vui_sar_width);
238+ ub(16, vui_sar_height);
239+ }
240+ } else {
241+ infer(vui_aspect_ratio_constant_flag, 0);
242+ infer(vui_aspect_ratio_idc, 0);
243+ }
244+ flag(vui_overscan_info_present_flag);
245+ if (current->vui_overscan_info_present_flag)
246+ flag(vui_overscan_appropriate_flag);
247+ flag(vui_colour_description_present_flag);
248+ if (current->vui_colour_description_present_flag) {
249+ ub(8, vui_colour_primaries);
250+ av_log(ctx->log_ctx, AV_LOG_DEBUG, "vui_colour_primaries == %d \n",
251+ current->vui_colour_primaries);
252+ ub(8, vui_transfer_characteristics);
253+ av_log(ctx->log_ctx, AV_LOG_DEBUG,
254+ "vui_transfer_characteristics == %d \n",
255+ current->vui_transfer_characteristics);
256+ ub(8, vui_matrix_coeffs);
257+ av_log(ctx->log_ctx, AV_LOG_DEBUG, "vui_matrix_coeffs == %d \n",
258+ current->vui_matrix_coeffs);
259+ flag(vui_full_range_flag);
260+ } else {
261+ infer(vui_colour_primaries, 2);
262+ infer(vui_transfer_characteristics, 2);
263+ infer(vui_matrix_coeffs, 2);
264+ infer(vui_full_range_flag, 0);
265+ }
266+ flag(vui_chroma_loc_info_present_flag);
267+ if (chroma_format_idc != 1 && current->vui_chroma_loc_info_present_flag) {
268+ av_log(ctx->log_ctx, AV_LOG_ERROR, "chroma_format_idc == %d,"
269+ "vui_chroma_loc_info_present_flag can't not be true",
270+ chroma_format_idc);
271+ return AVERROR_INVALIDDATA;
272+ }
273+ if (current->vui_chroma_loc_info_present_flag) {
274+ if (current->vui_progressive_source_flag &&
275+ !current->vui_interlaced_source_flag) {
276+ ue(vui_chroma_sample_loc_type_frame, 0, 6);
277+ } else {
278+ ue(vui_chroma_sample_loc_type_top_field, 0, 6);
279+ ue(vui_chroma_sample_loc_type_bottom_field, 0, 6);
280+ }
281+ } else {
282+ if (chroma_format_idc == 1) {
283+ infer(vui_chroma_sample_loc_type_frame, 6);
284+ infer(vui_chroma_sample_loc_type_top_field,
285+ current->vui_chroma_sample_loc_type_frame);
286+ infer(vui_chroma_sample_loc_type_bottom_field,
287+ current->vui_chroma_sample_loc_type_frame);
288+ }
289+ }
290+ return 0;
291+}
292+
293+static int FUNC(payload_extension) (CodedBitstreamContext *ctx, RWContext *rw,
294+ H266RawExtensionData *current,
295+ uint32_t payload_size, int cur_pos)
296+{
297+ int err;
298+ size_t byte_length, k;
299+
300+#ifdef READ
301+ GetBitContext tmp;
302+ int bits_left, payload_zero_bits;
303+
304+ if (!cbs_h265_payload_extension_present(rw, payload_size, cur_pos))
305+ return 0;
306+
307+ bits_left = 8 * payload_size - cur_pos;
308+ tmp = *rw;
309+ if (bits_left > 8)
310+ skip_bits_long(&tmp, bits_left - 8);
311+ payload_zero_bits = get_bits(&tmp, FFMIN(bits_left, 8));
312+ if (!payload_zero_bits)
313+ return AVERROR_INVALIDDATA;
314+ payload_zero_bits = ff_ctz(payload_zero_bits);
315+ current->bit_length = bits_left - payload_zero_bits - 1;
316+ allocate(current->data, (current->bit_length + 7) / 8);
317+#endif
318+
319+ byte_length = (current->bit_length + 7) / 8;
320+ for (k = 0; k < byte_length; k++) {
321+ int length = FFMIN(current->bit_length - k * 8, 8);
322+ xu(length, reserved_payload_extension_data, current->data[k],
323+ 0, MAX_UINT_BITS(length), 0);
324+ }
325+
326+ return 0;
327+}
328+
329+static int FUNC(vui_payload) (CodedBitstreamContext *ctx, RWContext *rw,
330+ H266RawVUI *current, uint16_t vui_payload_size,
331+ uint8_t chroma_format_idc)
332+{
333+ int err;
334+ int start_position, current_position;
335+
336+ start_position = bit_position(rw);
337+ CHECK(FUNC(vui_parameters) (ctx, rw, current, chroma_format_idc));
338+ current_position = bit_position(rw) - start_position;
339+
340+ if (current_position < 8 * vui_payload_size) {
341+ CHECK(FUNC(payload_extension) (ctx, rw, &current->extension_data,
342+ vui_payload_size, current_position));
343+ fixed(1, vui_payload_bit_equal_to_one, 1);
344+ while (byte_alignment(rw) != 0)
345+ fixed(1, vui_payload_bit_equal_to_zero, 0);
346+ }
347+ return 0;
348+}
349+
350+static int FUNC(extension_data) (CodedBitstreamContext *ctx, RWContext *rw,
351+ H266RawExtensionData *current)
352+{
353+ int err;
354+ size_t k;
355+#ifdef READ
356+ GetBitContext start;
357+ uint8_t bit;
358+ start = *rw;
359+ for (k = 0; cbs_h2645_read_more_rbsp_data(rw); k++)
360+ skip_bits(rw, 1);
361+ current->bit_length = k;
362+ if (k > 0) {
363+ *rw = start;
364+ allocate(current->data, (current->bit_length + 7) / 8);
365+ for (k = 0; k < current->bit_length; k++) {
366+ xu(1, extension_data, bit, 0, 1, 0);
367+ current->data[k / 8] |= bit << (7 - k % 8);
368+ }
369+ }
370+#else
371+ for (k = 0; k < current->bit_length; k++)
372+ xu(1, extension_data, current->data[k / 8] >> (7 - k % 8) & 1, 0, 1, 0);
373+#endif
374+ return 0;
375+}
376+
377+static int FUNC(dpb_parameters) (CodedBitstreamContext *ctx, RWContext *rw,
378+ H266DpbParameters *current,
379+ uint8_t max_sublayers_minus1,
380+ uint8_t sublayer_info_flag)
381+{
382+ int err, i;
383+ for (i = (sublayer_info_flag ? 0 : max_sublayers_minus1);
384+ i <= max_sublayers_minus1; i++) {
385+ ues(dpb_max_dec_pic_buffering_minus1[i], 0, VVC_MAX_DPB_SIZE - 1, 1, i);
386+ ues(dpb_max_num_reorder_pics[i],
387+ 0, current->dpb_max_dec_pic_buffering_minus1[i], 1, i);
388+ ues(dpb_max_latency_increase_plus1[i], 0, UINT32_MAX - 1, 1, i);
389+ }
390+ return 0;
391+}
392+
393+static int FUNC(ref_pic_list_struct) (CodedBitstreamContext *ctx,
394+ RWContext *rw,
395+ H266RefPicListStruct *current,
396+ uint8_t list_idx, uint8_t rpls_idx,
397+ const H266RawSPS *sps)
398+{
399+ CodedBitstreamH266Context *h266 = ctx->priv_data;
400+ int err, i, j, general_layer_idx = -1, num_direct_ref_layers = 0;
401+ const H266RawVPS *vps = h266->vps[sps->sps_video_parameter_set_id];
402+
403+ if (!vps) {
404+ av_log(ctx->log_ctx, AV_LOG_ERROR,
405+ "VPS id %d not available.\n", sps->sps_video_parameter_set_id);
406+ return AVERROR_INVALIDDATA;
407+ }
408+ //7.4.3.3 (29)
409+ for (i = 0; i <= vps->vps_max_layers_minus1; i++) {
410+ if (sps->nal_unit_header.nuh_layer_id == vps->vps_layer_id[i]) {
411+ general_layer_idx = i;
412+ break;
413+ }
414+ }
415+ if (general_layer_idx < 0) {
416+ av_log(ctx->log_ctx, AV_LOG_ERROR, "vps_layer_id %d not available.\n",
417+ sps->nal_unit_header.nuh_layer_id);
418+ return AVERROR_INVALIDDATA;
419+ }
420+ //7.4.3.3 (28)
421+ for (j = 0; j <= vps->vps_max_layers_minus1; j++) {
422+ if (vps->vps_direct_ref_layer_flag[general_layer_idx][j])
423+ num_direct_ref_layers++;
424+ }
425+
426+ ue(num_ref_entries, 0, VVC_MAX_REF_ENTRIES);
427+ if (sps->sps_long_term_ref_pics_flag &&
428+ rpls_idx < sps->sps_num_ref_pic_lists[list_idx] &&
429+ current->num_ref_entries > 0)
430+ flag(ltrp_in_header_flag);
431+ if (sps->sps_long_term_ref_pics_flag &&
432+ rpls_idx == sps->sps_num_ref_pic_lists[list_idx])
433+ infer(ltrp_in_header_flag, 1);
434+ for (i = 0, j = 0; i < current->num_ref_entries; i++) {
435+ if (sps->sps_inter_layer_prediction_enabled_flag)
436+ flags(inter_layer_ref_pic_flag[i], 1, i);
437+ else
438+ infer(inter_layer_ref_pic_flag[i], 0);
439+
440+ if (!current->inter_layer_ref_pic_flag[i]) {
441+ if (sps->sps_long_term_ref_pics_flag)
442+ flags(st_ref_pic_flag[i], 1, i);
443+ else
444+ infer(st_ref_pic_flag[i], 1);
445+ if (current->st_ref_pic_flag[i]) {
446+ int abs_delta_poc_st;
447+ ues(abs_delta_poc_st[i], 0, MAX_UINT_BITS(15), 1, i);
448+ if ((sps->sps_weighted_pred_flag ||
449+ sps->sps_weighted_bipred_flag) && i != 0)
450+ abs_delta_poc_st = current->abs_delta_poc_st[i];
451+ else
452+ abs_delta_poc_st = current->abs_delta_poc_st[i] + 1;
453+ if (abs_delta_poc_st > 0)
454+ flags(strp_entry_sign_flag[i], 1, i);
455+ } else {
456+ if (!current->ltrp_in_header_flag) {
457+ uint8_t bits = sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4;
458+ ubs(bits, rpls_poc_lsb_lt[j], 1, j);
459+ j++;
460+ }
461+ }
462+ } else {
463+ if (num_direct_ref_layers == 0) {
464+ av_log(ctx->log_ctx, AV_LOG_ERROR,
465+ "num_direct_ref_layers needs > 0.\n");
466+ return AVERROR_INVALIDDATA;
467+ }
468+ ues(ilrp_idx[i], 0, num_direct_ref_layers - 1, 1, i);
469+ }
470+ }
471+ return 0;
472+}
473+
474+static int FUNC(ref_pic_lists) (CodedBitstreamContext *ctx, RWContext *rw,
475+ const H266RawSPS *sps, const H266RawPPS *pps,
476+ H266RefPicLists *current) {
477+ const H266RefPicListStruct * ref_list;
478+ int err, i, j, num_ltrp_entries;
479+ for (i = 0; i < 2; i++) {
480+ if (sps->sps_num_ref_pic_lists[i] > 0 &&
481+ (i == 0 || (i == 1 && pps->pps_rpl1_idx_present_flag))) {
482+ flags(rpl_sps_flag[i], 1, i);
483+ } else {
484+ if (sps->sps_num_ref_pic_lists[i] == 0) {
485+ infer(rpl_sps_flag[i], 0);
486+ } else {
487+ if (!pps->pps_rpl1_idx_present_flag && i == 1)
488+ infer(rpl_sps_flag[1], current->rpl_sps_flag[0]);
489+ }
490+ }
491+ if (current->rpl_sps_flag[i]) {
492+ if (sps->sps_num_ref_pic_lists[i] > 1 &&
493+ (i == 0 || (i == 1 && pps->pps_rpl1_idx_present_flag))) {
494+ uint8_t bits = av_ceil_log2(sps->sps_num_ref_pic_lists[i]);
495+ us(bits, rpl_idx[i], 0, sps->sps_num_ref_pic_lists[i] - 1, 1, i);
496+ } else if (sps->sps_num_ref_pic_lists[i] == 1) {
497+ infer(rpl_idx[i], 0);
498+ } else if (i == 1 && !pps->pps_rpl1_idx_present_flag) {
499+ infer(rpl_idx[1], current->rpl_idx[0]);
500+ } else {
501+ //how to handle this? or never happpend?
502+ av_log(ctx->log_ctx, AV_LOG_ERROR,
503+ "can't infer the rpl_idx[i]\n");
504+ return AVERROR_PATCHWELCOME;
505+ }
506+ memcpy(&current->rpl_ref_list[i],
507+ &sps->sps_ref_pic_list_struct[i][current->rpl_idx[i]],
508+ sizeof(current->rpl_ref_list[i]));
509+ } else {
510+ CHECK(FUNC(ref_pic_list_struct) (ctx, rw, &current->rpl_ref_list[i],
511+ i, sps->sps_num_ref_pic_lists[i],
512+ sps));
513+ }
514+ ref_list = &current->rpl_ref_list[i];
515+
516+ num_ltrp_entries = 0;
517+ for (int i = 0; i < ref_list->num_ref_entries; i++) {
518+ if (!ref_list->inter_layer_ref_pic_flag[i]) {
519+ if (!ref_list->st_ref_pic_flag[i]) {
520+ num_ltrp_entries++;
521+ }
522+ }
523+ }
524+
525+ for (j = 0; j < num_ltrp_entries; j++) {
526+ if (ref_list->ltrp_in_header_flag) {
527+ ubs(sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4,
528+ poc_lsb_lt[i][j], 2, i, j);
529+ }
530+ flags(delta_poc_msb_cycle_present_flag[i][j], 2, i, j);
531+ if (current->delta_poc_msb_cycle_present_flag[i][j]) {
532+ uint32_t max =
533+ 1 << (32 - sps->sps_log2_max_pic_order_cnt_lsb_minus4 - 4);
534+ ues(delta_poc_msb_cycle_lt[i][j], 0, max, 2, i, j);
535+ }
536+ }
537+ }
538+ return 0;
539+}
540+
541+static int FUNC(general_timing_hrd_parameters)(CodedBitstreamContext *ctx,
542+ RWContext *rw,
543+ H266RawGeneralTimingHrdParameters *current)
544+{
545+ int err;
546+ ub(32, num_units_in_tick);
547+ u(32, time_scale, 1, MAX_UINT_BITS(32));
548+ flag(general_nal_hrd_params_present_flag);
549+ flag(general_vcl_hrd_params_present_flag);
550+
551+ if (current->general_nal_hrd_params_present_flag ||
552+ current->general_vcl_hrd_params_present_flag) {
553+ flag(general_same_pic_timing_in_all_ols_flag);
554+ flag(general_du_hrd_params_present_flag);
555+ if (current->general_du_hrd_params_present_flag)
556+ ub(8, tick_divisor_minus2);
557+ ub(4, bit_rate_scale);
558+ ub(4, cpb_size_scale);
559+ if (current->general_du_hrd_params_present_flag)
560+ ub(4, cpb_size_du_scale);
561+ ue(hrd_cpb_cnt_minus1, 0, 31);
562+ } else {
563+ //infer general_same_pic_timing_in_all_ols_flag?
564+ infer(general_du_hrd_params_present_flag, 0);
565+ }
566+ return 0;
567+}
568+
569+static int FUNC(sublayer_hrd_parameters) (CodedBitstreamContext *ctx,
570+ RWContext *rw,
571+ H266RawSubLayerHRDParameters *current,
572+ int sublayer_id,
573+ const H266RawGeneralTimingHrdParameters *general)
574+{
575+ int err, i;
576+ for (i = 0; i <= general->hrd_cpb_cnt_minus1; i++) {
577+ ues(bit_rate_value_minus1[sublayer_id][i], 0, UINT32_MAX - 1, 2,
578+ sublayer_id, i);
579+ ues(cpb_size_value_minus1[sublayer_id][i], 0, UINT32_MAX - 1, 2,
580+ sublayer_id, i);
581+ if (general->general_du_hrd_params_present_flag) {
582+ ues(cpb_size_du_value_minus1[sublayer_id][i],
583+ 0, UINT32_MAX - 1, 2, sublayer_id, i);
584+ ues(bit_rate_du_value_minus1[sublayer_id][i],
585+ 0, UINT32_MAX - 1, 2, sublayer_id, i);
586+ }
587+ flags(cbr_flag[sublayer_id][i], 2, sublayer_id, i);
588+ }
589+ return 0;
590+}
591+
592+static int FUNC(ols_timing_hrd_parameters) (CodedBitstreamContext *ctx,
593+ RWContext *rw, H266RawOlsTimingHrdParameters *current,
594+ uint8_t first_sublayer, uint8_t max_sublayers_minus1,
595+ const H266RawGeneralTimingHrdParameters *general)
596+{
597+ int err, i;
598+ for (i = first_sublayer; i <= max_sublayers_minus1; i++) {
599+ flags(fixed_pic_rate_general_flag[i], 1, i);
600+ if (!current->fixed_pic_rate_general_flag[i])
601+ flags(fixed_pic_rate_within_cvs_flag[i], 1, i);
602+ else
603+ infer(fixed_pic_rate_within_cvs_flag[i], 1);
604+ if (current->fixed_pic_rate_within_cvs_flag[i]) {
605+ ues(elemental_duration_in_tc_minus1[i], 0, 2047, 1, i);
606+ infer(low_delay_hrd_flag[i], 0);
607+ } else if ((general->general_nal_hrd_params_present_flag ||
608+ general->general_vcl_hrd_params_present_flag) &&
609+ general->hrd_cpb_cnt_minus1 == 0) {
610+ flags(low_delay_hrd_flag[i], 1, i);
611+ } else {
612+ infer(low_delay_hrd_flag[i], 0);
613+ }
614+ if (general->general_nal_hrd_params_present_flag)
615+ CHECK(FUNC(sublayer_hrd_parameters) (ctx, rw,
616+ &current->nal_sub_layer_hrd_parameters,
617+ i, general));
618+ if (general->general_vcl_hrd_params_present_flag)
619+ CHECK(FUNC(sublayer_hrd_parameters) (ctx, rw,
620+ &current->nal_sub_layer_hrd_parameters,
621+ i, general));
622+ }
623+ return 0;
624+}
625+
626+static int FUNC(vps) (CodedBitstreamContext *ctx, RWContext *rw,
627+ H266RawVPS *current)
628+{
629+ int err, i, j, k;
630+ uint16_t total_num_olss = 0;
631+ uint8_t ols_mode_idc = 0;
632+ uint16_t num_multi_layer_olss = 0;
633+ uint8_t layer_included_in_ols_flag[VVC_MAX_TOTAL_NUM_OLSS][VVC_MAX_LAYERS];
634+ uint8_t num_ref_layers[VVC_MAX_LAYERS];
635+ uint8_t reference_layer_idx[VVC_MAX_LAYERS][VVC_MAX_LAYERS];
636+
637+ HEADER("Video Parameter Set");
638+
639+ CHECK(FUNC(nal_unit_header) (ctx, rw,
640+ &current->nal_unit_header, VVC_VPS_NUT));
641+
642+ ub(4, vps_video_parameter_set_id);
643+ if (current->vps_video_parameter_set_id == 0) {
644+ av_log(ctx->log_ctx, AV_LOG_ERROR,
645+ "vps_video_parameter_set_id should > 0.\n");
646+ return AVERROR_INVALIDDATA;
647+ }
648+ ub(6, vps_max_layers_minus1);
649+ u(3, vps_max_sublayers_minus1, 0, 6);
650+ if (current->vps_max_layers_minus1 > 0
651+ && current->vps_max_sublayers_minus1 > 0)
652+ flag(vps_default_ptl_dpb_hrd_max_tid_flag);
653+ else
654+ infer(vps_default_ptl_dpb_hrd_max_tid_flag, 1);
655+
656+ if (current->vps_max_layers_minus1 > 0)
657+ flag(vps_all_independent_layers_flag);
658+ else
659+ infer(vps_all_independent_layers_flag, 1);
660+
661+ for (i = 0; i <= current->vps_max_layers_minus1; i++) {
662+ ubs(6, vps_layer_id[i], 1, i);
663+ if (i > 0 && current->vps_layer_id[i] <= current->vps_layer_id[i - 1]) {
664+ av_log(ctx->log_ctx, AV_LOG_ERROR,
665+ "vps_layer_id[%d](%d) should > vps_layer_id[%d](%d).\n",
666+ i, current->vps_layer_id[i], i - 1,
667+ current->vps_layer_id[i - 1]);
668+ return AVERROR_INVALIDDATA;
669+ }
670+ if (i > 0 && !current->vps_all_independent_layers_flag) {
671+ flags(vps_independent_layer_flag[i], 1, i);
672+ if (!current->vps_independent_layer_flag[i]) {
673+ flags(vps_max_tid_ref_present_flag[i], 1, i);
674+ for (j = 0; j < i; j++) {
675+ flags(vps_direct_ref_layer_flag[i][j], 2, i, j);
676+ if (current->vps_max_tid_ref_present_flag[i] &&
677+ current->vps_direct_ref_layer_flag[i][j]) {
678+ ubs(3, vps_max_tid_il_ref_pics_plus1[i][j], 2, i, j);
679+ } else {
680+ infer(vps_max_tid_il_ref_pics_plus1[i][j],
681+ current->vps_max_sublayers_minus1 + 1);
682+ }
683+ }
684+ } else {
685+ for (j = 0; j < i; j++) {
686+ infer(vps_direct_ref_layer_flag[i][j], 0);
687+ }
688+ }
689+ } else {
690+ infer(vps_independent_layer_flag[i], 1);
691+ for (j = 0; j < i; j++) {
692+ infer(vps_direct_ref_layer_flag[i][j], 0);
693+ }
694+ }
695+ }
696+
697+ if (current->vps_max_layers_minus1 > 0) {
698+ if (current->vps_all_independent_layers_flag)
699+ flag(vps_each_layer_is_an_ols_flag);
700+ else
701+ infer(vps_each_layer_is_an_ols_flag, 0);
702+ if (!current->vps_each_layer_is_an_ols_flag) {
703+ if (!current->vps_all_independent_layers_flag)
704+ ub(2, vps_ols_mode_idc);
705+ else
706+ infer(vps_ols_mode_idc, 2);
707+ if (current->vps_ols_mode_idc == 2) {
708+ ub(8, vps_num_output_layer_sets_minus2);
709+ for (i = 1; i <= current->vps_num_output_layer_sets_minus2 + 1;
710+ i++)
711+ for (j = 0; j <= current->vps_max_layers_minus1; j++)
712+ flags(vps_ols_output_layer_flag[i][j], 2, i, j);
713+ }
714+ ols_mode_idc = current->vps_ols_mode_idc;
715+ } else {
716+ ols_mode_idc = 4;
717+ }
718+ if (ols_mode_idc == 4 || ols_mode_idc == 0 || ols_mode_idc == 1)
719+ total_num_olss = current->vps_max_layers_minus1 + 1;
720+ else if (ols_mode_idc = 2)
721+ total_num_olss = current->vps_num_output_layer_sets_minus2 + 2;
722+ else
723+ av_log(ctx->log_ctx, AV_LOG_ERROR,
724+ "ols_mode_idc == 3, patch welcome");
725+ u(8, vps_num_ptls_minus1, 0, total_num_olss - 1);
726+ } else {
727+ infer(vps_each_layer_is_an_ols_flag, 1);
728+ infer(vps_num_ptls_minus1, 0);
729+ }
730+ {
731+ //calc NumMultiLayerOlss
732+ int m;
733+ uint8_t dependency_flag[VVC_MAX_LAYERS][VVC_MAX_LAYERS];
734+ uint16_t num_output_layers_in_ols[VVC_MAX_TOTAL_NUM_OLSS];
735+ uint8_t num_sub_layers_in_layer_in_ols[VVC_MAX_TOTAL_NUM_OLSS][VVC_MAX_TOTAL_NUM_OLSS];
736+ uint8_t output_layer_idx[VVC_MAX_TOTAL_NUM_OLSS][VVC_MAX_LAYERS];
737+
738+ //7.4.3.3 vps_direct_ref_layer_flag section
739+ for (i = 0; i <= current->vps_max_layers_minus1; i++) {
740+ for (j = 0; j <= current->vps_max_layers_minus1; j++) {
741+ dependency_flag[i][j] = current->vps_direct_ref_layer_flag[i][j];
742+ for (k = 0; k < i; k++) {
743+ if (current->vps_direct_ref_layer_flag[i][k] &&
744+ dependency_flag[k][j])
745+ dependency_flag[i][j] = 1;
746+ }
747+ }
748+ }
749+ for (i = 0; i <= current->vps_max_layers_minus1; i++) {
750+ int r;
751+ for (j = 0, r = 0; j <= current->vps_max_layers_minus1; j++) {
752+ if (dependency_flag[i][j])
753+ reference_layer_idx[i][r++] = j;
754+ }
755+ num_ref_layers[i] = r;
756+ }
757+
758+ //7.4.3.3 vps_ols_output_layer_flag section
759+ num_output_layers_in_ols[0] = 1;
760+ num_sub_layers_in_layer_in_ols[0][0] =
761+ current->vps_ptl_max_tid[current->vps_ols_ptl_idx[0]] + 1;
762+ for (i = 1; i < total_num_olss; i++) {
763+ if (ols_mode_idc == 4 || ols_mode_idc == 0) {
764+ num_output_layers_in_ols[i] = 1;
765+ if (current->vps_each_layer_is_an_ols_flag) {
766+ num_sub_layers_in_layer_in_ols[i][0] =
767+ current->vps_ptl_max_tid[current->vps_ols_ptl_idx[i]] + 1;
768+ } else {
769+ num_sub_layers_in_layer_in_ols[i][i] =
770+ current->vps_ptl_max_tid[current->vps_ols_ptl_idx[i]] + 1;
771+ for (k = i - 1; k >= 0; k--) {
772+ num_sub_layers_in_layer_in_ols[i][k] = 0;
773+ for (m = k + 1; m <= i; m++) {
774+ uint8_t max_sublayer_needed =
775+ FFMIN(num_sub_layers_in_layer_in_ols[i][m],
776+ current->vps_max_tid_il_ref_pics_plus1[m][k]);
777+ if (current->vps_direct_ref_layer_flag[m][k] &&
778+ num_sub_layers_in_layer_in_ols[i][k] < max_sublayer_needed)
779+ num_sub_layers_in_layer_in_ols[i][k] = max_sublayer_needed;
780+ }
781+ }
782+ }
783+ } else if (current->vps_ols_mode_idc == 1) {
784+ num_output_layers_in_ols[i] = i + 1;
785+ for (j = 0; j < num_output_layers_in_ols[i]; j++) {
786+ num_sub_layers_in_layer_in_ols[i][j] =
787+ current->vps_ptl_max_tid[current->vps_ols_ptl_idx[i]] + 1;
788+ }
789+ } else if (current->vps_ols_mode_idc == 2) {
790+ uint8_t highest_included_layer = 0;
791+ for (j = 0; j <= current->vps_max_layers_minus1; j++) {
792+ layer_included_in_ols_flag[i][j] = 0;
793+ num_sub_layers_in_layer_in_ols[i][j] = 0;
794+ }
795+ for (k = 0, j = 0; k <= current->vps_max_layers_minus1; k++) {
796+ if (current->vps_ols_output_layer_flag[i][k]) {
797+ layer_included_in_ols_flag[i][k] = 1;
798+ highest_included_layer = k;
799+ output_layer_idx[i][j] = k;
800+ num_sub_layers_in_layer_in_ols[i][k] =
801+ current->vps_ptl_max_tid[current->
802+ vps_ols_ptl_idx[i]] + 1;
803+ j++;
804+ }
805+ }
806+ num_output_layers_in_ols[i] = j;
807+ for (j = 0; j < num_output_layers_in_ols[i]; j++) {
808+ int idx = output_layer_idx[i][j];
809+ for (k = 0; k < num_ref_layers[idx]; k++) {
810+ if (!layer_included_in_ols_flag[i][reference_layer_idx[idx][k]])
811+ layer_included_in_ols_flag[i][reference_layer_idx[idx][k]] = 1;
812+ }
813+ }
814+ for (k = highest_included_layer - 1; k >= 0; k--) {
815+ if (layer_included_in_ols_flag[i][k] &&
816+ !current->vps_ols_output_layer_flag[i][k]) {
817+ for (m = k + 1; m <= highest_included_layer; m++) {
818+ uint8_t max_sublayer_needed =
819+ FFMIN(num_sub_layers_in_layer_in_ols[i][m],
820+ current->vps_max_tid_il_ref_pics_plus1[m][k]);
821+ if (current->vps_direct_ref_layer_flag[m][k] &&
822+ layer_included_in_ols_flag[i][m] &&
823+ num_sub_layers_in_layer_in_ols[i][k] <
824+ max_sublayer_needed)
825+ num_sub_layers_in_layer_in_ols[i][k] =
826+ max_sublayer_needed;
827+ }
828+ }
829+ }
830+ }
831+ }
832+ for (i = 1; i < total_num_olss; i++) {
833+ int num_layers_in_ols = 0;
834+ if (current->vps_each_layer_is_an_ols_flag) {
835+ num_layers_in_ols = 1;
836+ } else if (current->vps_ols_mode_idc == 0 ||
837+ current->vps_ols_mode_idc == 1) {
838+ num_layers_in_ols = i + 1;
839+ } else if (current->vps_ols_mode_idc == 2) {
840+ for (k = 0, j = 0; k <= current->vps_max_layers_minus1; k++) {
841+ if (layer_included_in_ols_flag[i][k])
842+ j++;
843+ num_layers_in_ols = j;
844+ }
845+ }
846+ if (num_layers_in_ols > 1) {
847+ num_multi_layer_olss++;
848+ }
849+ }
850+ }
851+
852+ for (i = 0; i <= current->vps_num_ptls_minus1; i++) {
853+ if (i > 0)
854+ flags(vps_pt_present_flag[i], 1, i);
855+ else
856+ infer(vps_pt_present_flag[i], 1);
857+
858+ if (!current->vps_default_ptl_dpb_hrd_max_tid_flag)
859+ us(3, vps_ptl_max_tid[i], 0, current->vps_max_sublayers_minus1, 1, i);
860+ else
861+ infer(vps_ptl_max_tid[i], current->vps_max_sublayers_minus1);
862+ }
863+ while (byte_alignment(rw) != 0)
864+ fixed(1, vps_ptl_alignment_zero_bit, 0);
865+ for (i = 0; i <= current->vps_num_ptls_minus1; i++) {
866+ CHECK(FUNC(profile_tier_level) (ctx, rw,
867+ current->vps_profile_tier_level + i,
868+ current->vps_pt_present_flag[i],
869+ current->vps_ptl_max_tid[i]));
870+ }
871+ for (i = 0; i < total_num_olss; i++) {
872+ if (current->vps_num_ptls_minus1 > 0 &&
873+ current->vps_num_ptls_minus1 + 1 != total_num_olss) {
874+ us(8, vps_ols_ptl_idx[i], 0, current->vps_num_ptls_minus1, 1, i);
875+ } else if (current->vps_num_ptls_minus1 == 0) {
876+ infer(vps_ols_ptl_idx[i], 0);
877+ } else {
878+ infer(vps_ols_ptl_idx[i], i);
879+ }
880+ }
881+
882+ if (!current->vps_each_layer_is_an_ols_flag) {
883+ uint16_t vps_num_dpb_params;
884+ ue(vps_num_dpb_params_minus1, 0, num_multi_layer_olss - 1);
885+ if (current->vps_each_layer_is_an_ols_flag)
886+ vps_num_dpb_params = 0;
887+ else
888+ vps_num_dpb_params = current->vps_num_dpb_params_minus1 + 1;
889+
890+ if (current->vps_max_sublayers_minus1 > 0)
891+ flag(vps_sublayer_dpb_params_present_flag);
892+ else
893+ infer(vps_sublayer_dpb_params_present_flag, 0);
894+
895+ for (i = 0; i < vps_num_dpb_params; i++) {
896+ if (!current->vps_default_ptl_dpb_hrd_max_tid_flag)
897+ us(3, vps_dpb_max_tid[i], 0, current->vps_max_sublayers_minus1,
898+ 1, i);
899+ else
900+ infer(vps_dpb_max_tid[i], current->vps_max_sublayers_minus1);
901+ CHECK(FUNC(dpb_parameters) (ctx, rw, current->vps_dpb_params + i,
902+ current->vps_dpb_max_tid[i],
903+ current->
904+ vps_sublayer_dpb_params_present_flag));
905+ }
906+ for (i = 0; i < num_multi_layer_olss; i++) {
907+ ues(vps_ols_dpb_pic_width[i], 0, UINT16_MAX, 1, i);
908+ ues(vps_ols_dpb_pic_height[i], 0, UINT16_MAX, 1, i);
909+ ubs(2, vps_ols_dpb_chroma_format[i], 1, i);
910+ ues(vps_ols_dpb_bitdepth_minus8[i], 0, 2, 1, i);
911+ if (vps_num_dpb_params > 1
912+ && vps_num_dpb_params != num_multi_layer_olss)
913+ ues(vps_ols_dpb_params_idx[i], 0, vps_num_dpb_params - 1, 1, i);
914+ else if (vps_num_dpb_params == 1)
915+ infer(vps_ols_dpb_params_idx[i], 0);
916+ else
917+ infer(vps_ols_dpb_params_idx[i], i);
918+ }
919+ flag(vps_timing_hrd_params_present_flag);
920+ if (current->vps_timing_hrd_params_present_flag) {
921+ CHECK(FUNC(general_timing_hrd_parameters) (ctx, rw,
922+ &current->
923+ vps_general_timing_hrd_parameters));
924+ if (current->vps_max_sublayers_minus1 > 0)
925+ flag(vps_sublayer_cpb_params_present_flag);
926+ else
927+ infer(vps_sublayer_cpb_params_present_flag, 0);
928+ ue(vps_num_ols_timing_hrd_params_minus1, 0,
929+ num_multi_layer_olss - 1);
930+ for (i = 0; i <= current->vps_num_ols_timing_hrd_params_minus1; i++) {
931+ uint8_t first_sublayer;
932+ if (!current->vps_default_ptl_dpb_hrd_max_tid_flag)
933+ us(3, vps_hrd_max_tid[i], 0,
934+ current->vps_max_sublayers_minus1, 1, i);
935+ else
936+ infer(vps_hrd_max_tid[i],
937+ current->vps_max_sublayers_minus1);
938+ first_sublayer = current->vps_sublayer_cpb_params_present_flag ?
939+ 0 : current->vps_hrd_max_tid[i];
940+ CHECK(FUNC(ols_timing_hrd_parameters)
941+ (ctx, rw, &current->vps_ols_timing_hrd_parameters,
942+ first_sublayer, current->vps_max_sublayers_minus1,
943+ &current->vps_general_timing_hrd_parameters));
944+
945+ }
946+ if (current->vps_num_ols_timing_hrd_params_minus1 > 0 &&
947+ current->vps_num_ols_timing_hrd_params_minus1 + 1 !=
948+ num_multi_layer_olss) {
949+ for (i = 0; i < num_multi_layer_olss; i++) {
950+ ues(vps_ols_timing_hrd_idx[i], 0,
951+ current->vps_num_ols_timing_hrd_params_minus1, 1, i);
952+ }
953+ } else if (current->vps_num_ols_timing_hrd_params_minus1 == 0) {
954+ for (i = 0; i < num_multi_layer_olss; i++)
955+ infer(vps_ols_timing_hrd_idx[i], 0);
956+ } else {
957+ for (i = 0; i < num_multi_layer_olss; i++)
958+ infer(vps_ols_timing_hrd_idx[i], i);
959+ }
960+ }
961+ }
962+
963+ flag(vps_extension_flag);
964+ if (current->vps_extension_flag)
965+ CHECK(FUNC(extension_data) (ctx, rw, &current->extension_data));
966+ CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
967+
968+ return 0;
969+}
970+
971+
972+static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
973+ H266RawSPS *current)
974+{
975+ CodedBitstreamH266Context *h266 = ctx->priv_data;
976+ int err, i, j;
977+ unsigned int ctb_log2_size_y, min_cb_log2_size_y,
978+ min_qt_log2_size_intra_y, min_qt_log2_size_inter_y,
979+ ctb_size_y, max_num_merge_cand, tmp_width_val, tmp_height_val;
980+ uint8_t qp_bd_offset;
981+
982+ static const uint8_t h266_sub_width_c[] = {
983+ 1, 2, 2, 1
984+ };
985+ static const uint8_t h266_sub_height_c[] = {
986+ 1, 2, 1, 1
987+ };
988+
989+ HEADER("Sequence Parameter Set");
990+
991+ CHECK(FUNC(nal_unit_header) (ctx, rw,
992+ &current->nal_unit_header, VVC_SPS_NUT));
993+
994+ ub(4, sps_seq_parameter_set_id);
995+ ub(4, sps_video_parameter_set_id);
996+ if (current->sps_video_parameter_set_id == 0 && !h266->vps_ref[0]) {
997+ H266RawVPS *vps;
998+ AVBufferRef *ref = av_buffer_allocz(sizeof(H266RawVPS));
999+ if (!ref) {
1000+ return AVERROR(ENOMEM);
1001+ }
1002+ vps = (H266RawVPS *) ref->data;
1003+ vps->vps_max_layers_minus1 = 0;
1004+ vps->vps_independent_layer_flag[0] = 1;
1005+ vps->vps_layer_id[0] = current->nal_unit_header.nuh_layer_id;
1006+ h266->vps_ref[0] = ref;
1007+ h266->vps[0] = vps;
1008+ }
1009+
1010+ u(3, sps_max_sublayers_minus1, 0, VVC_MAX_SUBLAYERS - 1);
1011+ u(2, sps_chroma_format_idc, 0, 3);
1012+ u(2, sps_log2_ctu_size_minus5, 0, 3);
1013+ ctb_log2_size_y = current->sps_log2_ctu_size_minus5 + 5;
1014+ ctb_size_y = 1 << ctb_log2_size_y;
1015+
1016+ flag(sps_ptl_dpb_hrd_params_present_flag);
1017+ if (current->sps_ptl_dpb_hrd_params_present_flag) {
1018+ CHECK(FUNC(profile_tier_level) (ctx, rw, &current->profile_tier_level,
1019+ 1, current->sps_max_sublayers_minus1));
1020+ }
1021+ flag(sps_gdr_enabled_flag);
1022+ flag(sps_ref_pic_resampling_enabled_flag);
1023+ if (current->sps_ref_pic_resampling_enabled_flag)
1024+ flag(sps_res_change_in_clvs_allowed_flag);
1025+ else
1026+ infer(sps_res_change_in_clvs_allowed_flag, 0);
1027+
1028+ ue(sps_pic_width_max_in_luma_samples, 1, VVC_MAX_WIDTH);
1029+ ue(sps_pic_height_max_in_luma_samples, 1, VVC_MAX_HEIGHT);
1030+
1031+ flag(sps_conformance_window_flag);
1032+ if (current->sps_conformance_window_flag) {
1033+ uint8_t sub_width_c = h266_sub_width_c[current->sps_chroma_format_idc];
1034+ uint8_t sub_height_c = h266_sub_height_c[current->sps_chroma_format_idc];
1035+ uint16_t width = current->sps_pic_width_max_in_luma_samples / sub_width_c;
1036+ uint16_t height = current->sps_pic_height_max_in_luma_samples / sub_height_c;
1037+ ue(sps_conf_win_left_offset, 0, width);
1038+ ue(sps_conf_win_right_offset, 0, width - current->sps_conf_win_left_offset);
1039+ ue(sps_conf_win_top_offset, 0, height);
1040+ ue(sps_conf_win_bottom_offset, 0, height - current->sps_conf_win_top_offset);
1041+ } else {
1042+ infer(sps_conf_win_left_offset, 0);
1043+ infer(sps_conf_win_right_offset, 0);
1044+ infer(sps_conf_win_top_offset, 0);
1045+ infer(sps_conf_win_bottom_offset, 0);
1046+ }
1047+
1048+ tmp_width_val = AV_CEIL_RSHIFT(current->sps_pic_width_max_in_luma_samples,
1049+ ctb_log2_size_y);
1050+ tmp_height_val = AV_CEIL_RSHIFT(current->sps_pic_height_max_in_luma_samples,
1051+ ctb_log2_size_y);
1052+
1053+ flag(sps_subpic_info_present_flag);
1054+ if (current->sps_subpic_info_present_flag) {
1055+ ue(sps_num_subpics_minus1, 1, VVC_MAX_SLICES - 1);
1056+ if (current->sps_num_subpics_minus1 > 0) {
1057+ flag(sps_independent_subpics_flag);
1058+ flag(sps_subpic_same_size_flag);
1059+ }
1060+
1061+ if (current->sps_num_subpics_minus1 > 0) {
1062+ int wlen = av_ceil_log2(tmp_width_val);
1063+ int hlen = av_ceil_log2(tmp_height_val);
1064+ if (current->sps_pic_width_max_in_luma_samples > ctb_size_y)
1065+ ubs(wlen, sps_subpic_width_minus1[0], 1, 0);
1066+ else
1067+ infer(sps_subpic_width_minus1[i], tmp_width_val - 1);
1068+ if (current->sps_pic_height_max_in_luma_samples > ctb_size_y)
1069+ ubs(hlen, sps_subpic_height_minus1[0], 1, 0);
1070+ else
1071+ infer(sps_subpic_height_minus1[0], tmp_height_val);
1072+ if (!current->sps_independent_subpics_flag) {
1073+ flags(sps_subpic_treated_as_pic_flag[0], 1, 0);
1074+ flags(sps_loop_filter_across_subpic_enabled_flag[0], 1, 0);
1075+ } else {
1076+ infer(sps_subpic_treated_as_pic_flag[0], 1);
1077+ infer(sps_loop_filter_across_subpic_enabled_flag[0], 1);
1078+ }
1079+ for (i = 1; i <= current->sps_num_subpics_minus1; i++) {
1080+ if (!current->sps_subpic_same_size_flag) {
1081+ if (current->sps_pic_width_max_in_luma_samples > ctb_size_y)
1082+ ubs(wlen, sps_subpic_ctu_top_left_x[i], 1, i);
1083+ else
1084+ infer(sps_subpic_ctu_top_left_x[i], 0);
1085+ if (current->sps_pic_height_max_in_luma_samples >
1086+ ctb_size_y)
1087+ ubs(hlen, sps_subpic_ctu_top_left_y[i], 1, i);
1088+ else
1089+ infer(sps_subpic_ctu_top_left_y[i], 0);
1090+ if (i < current->sps_num_subpics_minus1 &&
1091+ current->sps_pic_width_max_in_luma_samples >
1092+ ctb_size_y) {
1093+ ubs(wlen, sps_subpic_width_minus1[i], 1, i);
1094+ } else {
1095+ infer(sps_subpic_width_minus1[i],
1096+ tmp_width_val -
1097+ current->sps_subpic_ctu_top_left_x[i] - 1);
1098+ }
1099+ if (i < current->sps_num_subpics_minus1 &&
1100+ current->sps_pic_height_max_in_luma_samples >
1101+ ctb_size_y) {
1102+ ubs(hlen, sps_subpic_height_minus1[i], 1, i);
1103+ } else {
1104+ infer(sps_subpic_height_minus1[i],
1105+ tmp_height_val -
1106+ current->sps_subpic_ctu_top_left_y[i] - 1);
1107+ }
1108+ } else {
1109+ int num_subpic_cols = tmp_width_val /
1110+ (current->sps_subpic_width_minus1[0] + 1);
1111+ infer(sps_subpic_ctu_top_left_x[i],
1112+ (i % num_subpic_cols) *
1113+ (current->sps_subpic_width_minus1[0] + 1));
1114+ infer(sps_subpic_ctu_top_left_y[i],
1115+ (i / num_subpic_cols) *
1116+ (current->sps_subpic_height_minus1[0] + 1));
1117+ infer(sps_subpic_width_minus1[i],
1118+ current->sps_subpic_width_minus1[0]);
1119+ infer(sps_subpic_height_minus1[i],
1120+ current->sps_subpic_height_minus1[0]);
1121+ }
1122+ if (!current->sps_independent_subpics_flag) {
1123+ flags(sps_subpic_treated_as_pic_flag[i], 1, i);
1124+ flags(sps_loop_filter_across_subpic_enabled_flag[i], 1, i);
1125+ } else {
1126+ infer(sps_subpic_treated_as_pic_flag[i], 1);
1127+ infer(sps_loop_filter_across_subpic_enabled_flag[i], 0);
1128+ }
1129+ }
1130+ ue(sps_subpic_id_len_minus1, 0, 15);
1131+ if ((1 << (current->sps_subpic_id_len_minus1 + 1)) <
1132+ current->sps_num_subpics_minus1 + 1) {
1133+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1134+ "sps_subpic_id_len_minus1(%d) is too small\n",
1135+ current->sps_subpic_id_len_minus1);
1136+ return AVERROR_INVALIDDATA;
1137+ }
1138+ flag(sps_subpic_id_mapping_explicitly_signalled_flag);
1139+ if (current->sps_subpic_id_mapping_explicitly_signalled_flag) {
1140+ flag(sps_subpic_id_mapping_present_flag);
1141+ if (current->sps_subpic_id_mapping_present_flag) {
1142+ for (i = 0; i <= current->sps_num_subpics_minus1; i++) {
1143+ ubs(current->sps_subpic_id_len_minus1 + 1,
1144+ sps_subpic_id[i], 1, i);
1145+ }
1146+ }
1147+ }
1148+ } else {
1149+ infer(sps_subpic_ctu_top_left_x[0], 0);
1150+ infer(sps_subpic_ctu_top_left_y[0], 0);
1151+ infer(sps_subpic_width_minus1[0], tmp_width_val - 1);
1152+ infer(sps_subpic_height_minus1[0], tmp_height_val - 1);
1153+ }
1154+ } else {
1155+ infer(sps_num_subpics_minus1, 0);
1156+ infer(sps_independent_subpics_flag, 1);
1157+ infer(sps_subpic_same_size_flag, 0);
1158+ infer(sps_subpic_id_mapping_explicitly_signalled_flag, 0);
1159+ infer(sps_subpic_ctu_top_left_x[0], 0);
1160+ infer(sps_subpic_ctu_top_left_y[0], 0);
1161+ infer(sps_subpic_width_minus1[0], tmp_width_val - 1);
1162+ infer(sps_subpic_height_minus1[0], tmp_height_val - 1);
1163+ }
1164+
1165+
1166+ ue(sps_bitdepth_minus8, 0, 2);
1167+ qp_bd_offset = 6 * current->sps_bitdepth_minus8;
1168+
1169+ flag(sps_entropy_coding_sync_enabled_flag);
1170+ flag(sps_entry_point_offsets_present_flag);
1171+
1172+ u(4, sps_log2_max_pic_order_cnt_lsb_minus4, 0, 12);
1173+ flag(sps_poc_msb_cycle_flag);
1174+ if (current->sps_poc_msb_cycle_flag)
1175+ ue(sps_poc_msb_cycle_len_minus1,
1176+ 0, 32 - current->sps_log2_max_pic_order_cnt_lsb_minus4 - 5);
1177+
1178+ u(2, sps_num_extra_ph_bytes, 0, 2);
1179+ for (i = 0; i < (current->sps_num_extra_ph_bytes * 8); i++) {
1180+ flags(sps_extra_ph_bit_present_flag[i], 1, i);
1181+ }
1182+
1183+ u(2, sps_num_extra_sh_bytes, 0, 2);
1184+ for (i = 0; i < (current->sps_num_extra_sh_bytes * 8); i++) {
1185+ flags(sps_extra_sh_bit_present_flag[i], 1, i);
1186+ }
1187+
1188+ if (current->sps_ptl_dpb_hrd_params_present_flag) {
1189+ if (current->sps_max_sublayers_minus1 > 0)
1190+ flag(sps_sublayer_dpb_params_flag);
1191+ else
1192+ infer(sps_sublayer_dpb_params_flag, 0);
1193+ CHECK(FUNC(dpb_parameters) (ctx, rw, &current->sps_dpb_params,
1194+ current->sps_max_sublayers_minus1,
1195+ current->sps_sublayer_dpb_params_flag));
1196+ }
1197+
1198+ ue(sps_log2_min_luma_coding_block_size_minus2,
1199+ 0, FFMIN(4, current->sps_log2_ctu_size_minus5 + 3));
1200+ min_cb_log2_size_y =
1201+ current->sps_log2_min_luma_coding_block_size_minus2 + 2;
1202+
1203+ flag(sps_partition_constraints_override_enabled_flag);
1204+
1205+ ue(sps_log2_diff_min_qt_min_cb_intra_slice_luma,
1206+ 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
1207+ min_qt_log2_size_intra_y =
1208+ current->sps_log2_diff_min_qt_min_cb_intra_slice_luma +
1209+ min_cb_log2_size_y;
1210+
1211+ ue(sps_max_mtt_hierarchy_depth_intra_slice_luma,
1212+ 0, 2 * (ctb_log2_size_y - min_cb_log2_size_y));
1213+
1214+ if (current->sps_max_mtt_hierarchy_depth_intra_slice_luma != 0) {
1215+ ue(sps_log2_diff_max_bt_min_qt_intra_slice_luma,
1216+ 0, ctb_log2_size_y - min_qt_log2_size_intra_y);
1217+ ue(sps_log2_diff_max_tt_min_qt_intra_slice_luma,
1218+ 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_y);
1219+ } else {
1220+ infer(sps_log2_diff_max_bt_min_qt_intra_slice_luma, 0);
1221+ infer(sps_log2_diff_max_tt_min_qt_intra_slice_luma, 0);
1222+ }
1223+
1224+ if (current->sps_chroma_format_idc != 0) {
1225+ flag(sps_qtbtt_dual_tree_intra_flag);
1226+ } else {
1227+ infer(sps_qtbtt_dual_tree_intra_flag, 0);
1228+ }
1229+
1230+ if (current->sps_qtbtt_dual_tree_intra_flag) {
1231+ ue(sps_log2_diff_min_qt_min_cb_intra_slice_chroma,
1232+ 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
1233+ ue(sps_max_mtt_hierarchy_depth_intra_slice_chroma,
1234+ 0, 2 * (ctb_log2_size_y - min_cb_log2_size_y));
1235+ if (current->sps_max_mtt_hierarchy_depth_intra_slice_chroma != 0) {
1236+ unsigned int min_qt_log2_size_intra_c =
1237+ current->sps_log2_diff_min_qt_min_cb_intra_slice_chroma +
1238+ min_cb_log2_size_y;
1239+ ue(sps_log2_diff_max_bt_min_qt_intra_slice_chroma,
1240+ 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c);
1241+ ue(sps_log2_diff_max_tt_min_qt_intra_slice_chroma,
1242+ 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c);
1243+ }
1244+ } else {
1245+ infer(sps_log2_diff_min_qt_min_cb_intra_slice_chroma, 0);
1246+ infer(sps_max_mtt_hierarchy_depth_intra_slice_chroma, 0);
1247+ }
1248+ if (current->sps_max_mtt_hierarchy_depth_intra_slice_chroma == 0) {
1249+ infer(sps_log2_diff_max_bt_min_qt_intra_slice_chroma, 0);
1250+ infer(sps_log2_diff_max_tt_min_qt_intra_slice_chroma, 0);
1251+ }
1252+
1253+ ue(sps_log2_diff_min_qt_min_cb_inter_slice,
1254+ 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
1255+ min_qt_log2_size_inter_y =
1256+ current->sps_log2_diff_min_qt_min_cb_inter_slice + min_cb_log2_size_y;
1257+
1258+ ue(sps_max_mtt_hierarchy_depth_inter_slice,
1259+ 0, 2 * (ctb_log2_size_y - min_cb_log2_size_y));
1260+ if (current->sps_max_mtt_hierarchy_depth_inter_slice != 0) {
1261+ ue(sps_log2_diff_max_bt_min_qt_inter_slice,
1262+ 0, ctb_log2_size_y - min_qt_log2_size_inter_y);
1263+ ue(sps_log2_diff_max_tt_min_qt_inter_slice,
1264+ 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_inter_y);
1265+ } else {
1266+ infer(sps_log2_diff_max_bt_min_qt_inter_slice, 0);
1267+ infer(sps_log2_diff_max_tt_min_qt_inter_slice, 0);
1268+ }
1269+
1270+ if (ctb_size_y > 32)
1271+ flag(sps_max_luma_transform_size_64_flag);
1272+ else
1273+ infer(sps_max_luma_transform_size_64_flag, 0);
1274+
1275+ flag(sps_transform_skip_enabled_flag);
1276+ if (current->sps_transform_skip_enabled_flag) {
1277+ ue(sps_log2_transform_skip_max_size_minus2, 0, 3);
1278+ flag(sps_bdpcm_enabled_flag);
1279+ }
1280+
1281+ flag(sps_mts_enabled_flag);
1282+ if (current->sps_mts_enabled_flag) {
1283+ flag(sps_explicit_mts_intra_enabled_flag);
1284+ flag(sps_explicit_mts_inter_enabled_flag);
1285+ } else {
1286+ infer(sps_explicit_mts_intra_enabled_flag, 0);
1287+ infer(sps_explicit_mts_inter_enabled_flag, 0);
1288+ }
1289+
1290+ flag(sps_lfnst_enabled_flag);
1291+
1292+ if (current->sps_chroma_format_idc != 0) {
1293+ uint8_t num_qp_tables;
1294+ flag(sps_joint_cbcr_enabled_flag);
1295+ flag(sps_same_qp_table_for_chroma_flag);
1296+ num_qp_tables = current->sps_same_qp_table_for_chroma_flag ?
1297+ 1 : (current->sps_joint_cbcr_enabled_flag ? 3 : 2);
1298+ for (i = 0; i < num_qp_tables; i++) {
1299+ ses(sps_qp_table_start_minus26[i], -26 - qp_bd_offset, 36, 1, i);
1300+ ues(sps_num_points_in_qp_table_minus1[i],
1301+ 0, 36 - current->sps_qp_table_start_minus26[i], 1, i);
1302+ for (j = 0; j <= current->sps_num_points_in_qp_table_minus1[i]; j++) {
1303+ uint8_t max = MAX_UINT_BITS(8);
1304+ ues(sps_delta_qp_in_val_minus1[i][j], 0, max, 2, i, j);
1305+ ues(sps_delta_qp_diff_val[i][j], 0, max, 2, i, j);
1306+ }
1307+ }
1308+ } else {
1309+ infer(sps_joint_cbcr_enabled_flag, 0);
1310+ infer(sps_same_qp_table_for_chroma_flag, 0);
1311+ }
1312+
1313+ flag(sps_sao_enabled_flag);
1314+ flag(sps_alf_enabled_flag);
1315+ if (current->sps_alf_enabled_flag && current->sps_chroma_format_idc)
1316+ flag(sps_ccalf_enabled_flag);
1317+ else
1318+ infer(sps_ccalf_enabled_flag, 0);
1319+ flag(sps_lmcs_enabled_flag);
1320+ flag(sps_weighted_pred_flag);
1321+ flag(sps_weighted_bipred_flag);
1322+ flag(sps_long_term_ref_pics_flag);
1323+ if (current->sps_video_parameter_set_id > 0)
1324+ flag(sps_inter_layer_prediction_enabled_flag);
1325+ else
1326+ infer(sps_inter_layer_prediction_enabled_flag, 0);
1327+ flag(sps_idr_rpl_present_flag);
1328+ flag(sps_rpl1_same_as_rpl0_flag);
1329+
1330+ for (i = 0; i < (current->sps_rpl1_same_as_rpl0_flag ? 1 : 2); i++) {
1331+ ues(sps_num_ref_pic_lists[i], 0, VVC_MAX_REF_PIC_LISTS, 1, i);
1332+ for (j = 0; j < current->sps_num_ref_pic_lists[i]; j++)
1333+ CHECK(FUNC(ref_pic_list_struct) (ctx, rw,
1334+ &current->
1335+ sps_ref_pic_list_struct[i][j], i,
1336+ j, current));
1337+ }
1338+
1339+ if (current->sps_rpl1_same_as_rpl0_flag) {
1340+ current->sps_num_ref_pic_lists[1] = current->sps_num_ref_pic_lists[0];
1341+ for (j = 0; j < current->sps_num_ref_pic_lists[0]; j++)
1342+ memcpy(&current->sps_ref_pic_list_struct[1][j],
1343+ &current->sps_ref_pic_list_struct[0][j],
1344+ sizeof(current->sps_ref_pic_list_struct[0][j]));
1345+ }
1346+
1347+ flag(sps_ref_wraparound_enabled_flag);
1348+
1349+ flag(sps_temporal_mvp_enabled_flag);
1350+ if (current->sps_temporal_mvp_enabled_flag)
1351+ flag(sps_sbtmvp_enabled_flag);
1352+ else
1353+ infer(sps_sbtmvp_enabled_flag, 0);
1354+
1355+ flag(sps_amvr_enabled_flag);
1356+ flag(sps_bdof_enabled_flag);
1357+ if (current->sps_bdof_enabled_flag)
1358+ flag(sps_bdof_control_present_in_ph_flag);
1359+ else
1360+ infer(sps_bdof_control_present_in_ph_flag, 0);
1361+
1362+ flag(sps_smvd_enabled_flag);
1363+ flag(sps_dmvr_enabled_flag);
1364+ if (current->sps_dmvr_enabled_flag)
1365+ flag(sps_dmvr_control_present_in_ph_flag);
1366+ else
1367+ infer(sps_dmvr_control_present_in_ph_flag, 0);
1368+
1369+ flag(sps_mmvd_enabled_flag);
1370+ if (current->sps_mmvd_enabled_flag)
1371+ flag(sps_mmvd_fullpel_only_enabled_flag);
1372+ else
1373+ infer(sps_mmvd_fullpel_only_enabled_flag, 0);
1374+
1375+ ue(sps_six_minus_max_num_merge_cand, 0, 5);
1376+ max_num_merge_cand = 6 - current->sps_six_minus_max_num_merge_cand;
1377+
1378+ flag(sps_sbt_enabled_flag);
1379+
1380+ flag(sps_affine_enabled_flag);
1381+ if (current->sps_affine_enabled_flag) {
1382+ ue(sps_five_minus_max_num_subblock_merge_cand,
1383+ 0, 5 - current->sps_sbtmvp_enabled_flag);
1384+ flag(sps_6param_affine_enabled_flag);
1385+ if (current->sps_amvr_enabled_flag)
1386+ flag(sps_affine_amvr_enabled_flag);
1387+ else
1388+ infer(sps_affine_amvr_enabled_flag, 0);
1389+ flag(sps_affine_prof_enabled_flag);
1390+ if (current->sps_affine_prof_enabled_flag)
1391+ flag(sps_prof_control_present_in_ph_flag);
1392+ else
1393+ infer(sps_prof_control_present_in_ph_flag, 0);
1394+ } else {
1395+ infer(sps_6param_affine_enabled_flag, 0);
1396+ infer(sps_affine_amvr_enabled_flag, 0);
1397+ infer(sps_affine_prof_enabled_flag, 0);
1398+ infer(sps_prof_control_present_in_ph_flag, 0);
1399+ }
1400+
1401+ flag(sps_bcw_enabled_flag);
1402+ flag(sps_ciip_enabled_flag);
1403+
1404+ if (max_num_merge_cand >= 2) {
1405+ flag(sps_gpm_enabled_flag);
1406+ if (current->sps_gpm_enabled_flag && max_num_merge_cand >= 3)
1407+ ue(sps_max_num_merge_cand_minus_max_num_gpm_cand,
1408+ 0, max_num_merge_cand - 2);
1409+ } else {
1410+ infer(sps_gpm_enabled_flag, 0);
1411+ }
1412+
1413+ ue(sps_log2_parallel_merge_level_minus2, 0, ctb_log2_size_y - 2);
1414+
1415+ flag(sps_isp_enabled_flag);
1416+ flag(sps_mrl_enabled_flag);
1417+ flag(sps_mip_enabled_flag);
1418+
1419+ if (current->sps_chroma_format_idc != 0)
1420+ flag(sps_cclm_enabled_flag);
1421+ else
1422+ infer(sps_cclm_enabled_flag, 0);
1423+ if (current->sps_chroma_format_idc == 1) {
1424+ flag(sps_chroma_horizontal_collocated_flag);
1425+ flag(sps_chroma_vertical_collocated_flag);
1426+ } else {
1427+ infer(sps_chroma_horizontal_collocated_flag, 1);
1428+ infer(sps_chroma_vertical_collocated_flag, 1);
1429+ }
1430+
1431+ flag(sps_palette_enabled_flag);
1432+ if (current->sps_chroma_format_idc == 3 &&
1433+ !current->sps_max_luma_transform_size_64_flag)
1434+ flag(sps_act_enabled_flag);
1435+ else
1436+ infer(sps_act_enabled_flag, 0);
1437+ if (current->sps_transform_skip_enabled_flag ||
1438+ current->sps_palette_enabled_flag)
1439+ ue(sps_min_qp_prime_ts, 0, 8);
1440+
1441+ flag(sps_ibc_enabled_flag);
1442+ if (current->sps_ibc_enabled_flag)
1443+ ue(sps_six_minus_max_num_ibc_merge_cand, 0, 5);
1444+
1445+ flag(sps_ladf_enabled_flag);
1446+ if (current->sps_ladf_enabled_flag) {
1447+ ub(2, sps_num_ladf_intervals_minus2);
1448+ se(sps_ladf_lowest_interval_qp_offset, -63, 63);
1449+ for (i = 0; i < current->sps_num_ladf_intervals_minus2 + 1; i++) {
1450+ ses(sps_ladf_qp_offset[i], -63, 63, 1, i);
1451+ ues(sps_ladf_delta_threshold_minus1[i],
1452+ 0, (2 << (8 + current->sps_bitdepth_minus8)) - 3, 1, i);
1453+ }
1454+ }
1455+
1456+ flag(sps_explicit_scaling_list_enabled_flag);
1457+ if (current->sps_lfnst_enabled_flag &&
1458+ current->sps_explicit_scaling_list_enabled_flag)
1459+ flag(sps_scaling_matrix_for_lfnst_disabled_flag);
1460+
1461+ if (current->sps_act_enabled_flag &&
1462+ current->sps_explicit_scaling_list_enabled_flag)
1463+ flag(sps_scaling_matrix_for_alternative_colour_space_disabled_flag);
1464+ else
1465+ infer(sps_scaling_matrix_for_alternative_colour_space_disabled_flag, 0);
1466+ if (current->sps_scaling_matrix_for_alternative_colour_space_disabled_flag)
1467+ flag(sps_scaling_matrix_designated_colour_space_flag);
1468+
1469+ flag(sps_dep_quant_enabled_flag);
1470+ flag(sps_sign_data_hiding_enabled_flag);
1471+
1472+ flag(sps_virtual_boundaries_enabled_flag);
1473+ if (current->sps_virtual_boundaries_enabled_flag) {
1474+ flag(sps_virtual_boundaries_present_flag);
1475+ if (current->sps_virtual_boundaries_present_flag) {
1476+ ue(sps_num_ver_virtual_boundaries,
1477+ 0, current->sps_pic_width_max_in_luma_samples <= 8 ? 0 : 3);
1478+ for (i = 0; i < current->sps_num_ver_virtual_boundaries; i++)
1479+ ues(sps_virtual_boundary_pos_x_minus1[i],
1480+ 0, (current->sps_pic_width_max_in_luma_samples + 7) / 8 - 2,
1481+ 1, i);
1482+ ue(sps_num_hor_virtual_boundaries,
1483+ 0, current->sps_pic_height_max_in_luma_samples <= 8 ? 0 : 3);
1484+ for (i = 0; i < current->sps_num_hor_virtual_boundaries; i++)
1485+ ues(sps_virtual_boundary_pos_y_minus1[i],
1486+ 0, (current->sps_pic_height_max_in_luma_samples + 7) /
1487+ 8 - 2, 1, i);
1488+ }
1489+ } else {
1490+ infer(sps_virtual_boundaries_present_flag, 0);
1491+ infer(sps_num_ver_virtual_boundaries, 0);
1492+ infer(sps_num_hor_virtual_boundaries, 0);
1493+ }
1494+
1495+ if (current->sps_ptl_dpb_hrd_params_present_flag) {
1496+ flag(sps_timing_hrd_params_present_flag);
1497+ if (current->sps_timing_hrd_params_present_flag) {
1498+ uint8_t first_sublayer;
1499+ CHECK(FUNC(general_timing_hrd_parameters) (ctx, rw,
1500+ &current->sps_general_timing_hrd_parameters));
1501+ if (current->sps_max_sublayers_minus1 > 0)
1502+ flag(sps_sublayer_cpb_params_present_flag);
1503+ else
1504+ infer(sps_sublayer_cpb_params_present_flag, 0);
1505+ first_sublayer = current->sps_sublayer_cpb_params_present_flag ?
1506+ 0 : current->sps_max_sublayers_minus1;
1507+ CHECK(FUNC(ols_timing_hrd_parameters) (ctx, rw,
1508+ &current->sps_ols_timing_hrd_parameters, first_sublayer,
1509+ current->sps_max_sublayers_minus1,
1510+ &current->sps_general_timing_hrd_parameters));
1511+ }
1512+ }
1513+
1514+ flag(sps_field_seq_flag);
1515+ flag(sps_vui_parameters_present_flag);
1516+ if (current->sps_vui_parameters_present_flag) {
1517+ ue(sps_vui_payload_size_minus1, 0, 1023);
1518+ while (byte_alignment(rw) != 0)
1519+ fixed(1, sps_vui_alignment_zero_bit, 0);
1520+ CHECK(FUNC(vui_payload) (ctx, rw, &current->vui,
1521+ current->sps_vui_payload_size_minus1 + 1,
1522+ current->sps_chroma_format_idc));
1523+ } else {
1524+ CHECK(FUNC(vui_parameters_default) (ctx, rw, &current->vui));
1525+ }
1526+ flag(sps_extension_flag);
1527+ if (current->sps_extension_flag)
1528+ CHECK(FUNC(extension_data) (ctx, rw, &current->extension_data));
1529+
1530+ CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
1531+
1532+ return 0;
1533+}
1534+
1535+static int FUNC(pps) (CodedBitstreamContext *ctx, RWContext *rw,
1536+ H266RawPPS *current)
1537+{
1538+ CodedBitstreamH266Context *h266 = ctx->priv_data;
1539+ const H266RawSPS *sps;
1540+ int err, i;
1541+ unsigned int min_cb_size_y, divisor, ctb_size_y,
1542+ pic_width_in_ctbs_y, pic_height_in_ctbs_y;
1543+ uint8_t sub_width_c, sub_height_c, qp_bd_offset;
1544+
1545+ static const uint8_t h266_sub_width_c[] = {
1546+ 1, 2, 2, 1
1547+ };
1548+ static const uint8_t h266_sub_height_c[] = {
1549+ 1, 2, 1, 1
1550+ };
1551+
1552+ HEADER("Picture Parameter Set");
1553+
1554+ CHECK(FUNC(nal_unit_header) (ctx, rw,
1555+ &current->nal_unit_header, VVC_PPS_NUT));
1556+
1557+ ub(6, pps_pic_parameter_set_id);
1558+ ub(4, pps_seq_parameter_set_id);
1559+ sps = h266->sps[current->pps_seq_parameter_set_id];
1560+ if (!sps) {
1561+ av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
1562+ current->pps_seq_parameter_set_id);
1563+ return AVERROR_INVALIDDATA;
1564+ }
1565+
1566+ flag(pps_mixed_nalu_types_in_pic_flag);
1567+ ue(pps_pic_width_in_luma_samples,
1568+ 1, sps->sps_pic_width_max_in_luma_samples);
1569+ ue(pps_pic_height_in_luma_samples,
1570+ 1, sps->sps_pic_height_max_in_luma_samples);
1571+
1572+ min_cb_size_y = 1 << (sps->sps_log2_min_luma_coding_block_size_minus2 + 2);
1573+ divisor = FFMAX(min_cb_size_y, 8);
1574+ if (current->pps_pic_width_in_luma_samples % divisor ||
1575+ current->pps_pic_height_in_luma_samples % divisor) {
1576+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1577+ "Invalid dimensions: %ux%u not divisible "
1578+ "by %u, MinCbSizeY = %u.\n",
1579+ current->pps_pic_width_in_luma_samples,
1580+ current->pps_pic_height_in_luma_samples, divisor, min_cb_size_y);
1581+ return AVERROR_INVALIDDATA;
1582+ }
1583+ if (!sps->sps_res_change_in_clvs_allowed_flag &&
1584+ (current->pps_pic_width_in_luma_samples !=
1585+ sps->sps_pic_width_max_in_luma_samples ||
1586+ current->pps_pic_height_in_luma_samples !=
1587+ sps->sps_pic_height_max_in_luma_samples)) {
1588+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1589+ "Resoltuion change is not allowed, "
1590+ "in max resolution (%ux%u) mismatched with pps(%ux%u).\n",
1591+ sps->sps_pic_width_max_in_luma_samples,
1592+ sps->sps_pic_height_max_in_luma_samples,
1593+ current->pps_pic_width_in_luma_samples,
1594+ current->pps_pic_height_in_luma_samples);
1595+ return AVERROR_INVALIDDATA;
1596+ }
1597+
1598+ ctb_size_y = 1 << (sps->sps_log2_ctu_size_minus5 + 5);
1599+ if (sps->sps_ref_wraparound_enabled_flag) {
1600+ if ((ctb_size_y / min_cb_size_y + 1) >
1601+ (current->pps_pic_width_in_luma_samples / min_cb_size_y - 1)) {
1602+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1603+ "Invalid width(%u), ctb_size_y = %u, min_cb_size_y = %u.\n",
1604+ current->pps_pic_width_in_luma_samples,
1605+ ctb_size_y, min_cb_size_y);
1606+ return AVERROR_INVALIDDATA;
1607+ }
1608+ }
1609+
1610+ flag(pps_conformance_window_flag);
1611+ if (current->pps_pic_width_in_luma_samples ==
1612+ sps->sps_pic_width_max_in_luma_samples &&
1613+ current->pps_pic_height_in_luma_samples ==
1614+ sps->sps_pic_height_max_in_luma_samples &&
1615+ current->pps_conformance_window_flag) {
1616+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1617+ "Conformance window flag should not true.\n");
1618+ return AVERROR_INVALIDDATA;
1619+ }
1620+
1621+ sub_width_c = h266_sub_width_c[sps->sps_chroma_format_idc];
1622+ sub_height_c = h266_sub_height_c[sps->sps_chroma_format_idc];
1623+ if (current->pps_conformance_window_flag) {
1624+ ue(pps_conf_win_left_offset, 0, current->pps_pic_width_in_luma_samples);
1625+ ue(pps_conf_win_right_offset,
1626+ 0, current->pps_pic_width_in_luma_samples);
1627+ ue(pps_conf_win_top_offset, 0, current->pps_pic_height_in_luma_samples);
1628+ ue(pps_conf_win_bottom_offset,
1629+ 0, current->pps_pic_height_in_luma_samples);
1630+ if (sub_width_c *
1631+ (current->pps_conf_win_left_offset +
1632+ current->pps_conf_win_right_offset) >=
1633+ current->pps_pic_width_in_luma_samples ||
1634+ sub_height_c *
1635+ (current->pps_conf_win_top_offset +
1636+ current->pps_conf_win_bottom_offset) >=
1637+ current->pps_pic_height_in_luma_samples) {
1638+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1639+ "Invalid pps conformance window: (%u, %u, %u, %u), "
1640+ "resolution is %ux%u, sub wxh is %ux%u.\n",
1641+ current->pps_conf_win_left_offset,
1642+ current->pps_conf_win_right_offset,
1643+ current->pps_conf_win_top_offset,
1644+ current->pps_conf_win_bottom_offset,
1645+ current->pps_pic_width_in_luma_samples,
1646+ current->pps_pic_height_in_luma_samples,
1647+ sub_width_c, sub_height_c);
1648+ return AVERROR_INVALIDDATA;
1649+ }
1650+ } else {
1651+ if (current->pps_pic_width_in_luma_samples ==
1652+ sps->sps_pic_width_max_in_luma_samples &&
1653+ current->pps_pic_height_in_luma_samples ==
1654+ sps->sps_pic_height_max_in_luma_samples) {
1655+ infer(pps_conf_win_left_offset, sps->sps_conf_win_left_offset);
1656+ infer(pps_conf_win_right_offset, sps->sps_conf_win_right_offset);
1657+ infer(pps_conf_win_top_offset, sps->sps_conf_win_top_offset);
1658+ infer(pps_conf_win_bottom_offset, sps->sps_conf_win_bottom_offset);
1659+ } else {
1660+ infer(pps_conf_win_left_offset, 0);
1661+ infer(pps_conf_win_right_offset, 0);
1662+ infer(pps_conf_win_top_offset, 0);
1663+ infer(pps_conf_win_bottom_offset, 0);
1664+ }
1665+
1666+ }
1667+
1668+ flag(pps_scaling_window_explicit_signalling_flag);
1669+ if (!sps->sps_ref_pic_resampling_enabled_flag &&
1670+ current->pps_scaling_window_explicit_signalling_flag) {
1671+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1672+ "Invalid data: sps_ref_pic_resampling_enabled_flag is false, "
1673+ "but pps_scaling_window_explicit_signalling_flag is true.\n");
1674+ return AVERROR_INVALIDDATA;
1675+ }
1676+ if (current->pps_scaling_window_explicit_signalling_flag) {
1677+ se(pps_scaling_win_left_offset,
1678+ -current->pps_pic_width_in_luma_samples * 15 / sub_width_c,
1679+ current->pps_pic_width_in_luma_samples / sub_width_c);
1680+ se(pps_scaling_win_right_offset,
1681+ -current->pps_pic_width_in_luma_samples * 15 / sub_width_c,
1682+ current->pps_pic_width_in_luma_samples / sub_width_c);
1683+ se(pps_scaling_win_top_offset,
1684+ -current->pps_pic_height_in_luma_samples * 15 / sub_height_c,
1685+ current->pps_pic_height_in_luma_samples / sub_height_c);
1686+ se(pps_scaling_win_bottom_offset,
1687+ -current->pps_pic_height_in_luma_samples * 15 / sub_height_c,
1688+ current->pps_pic_height_in_luma_samples / sub_height_c);
1689+ } else {
1690+ infer(pps_scaling_win_left_offset, current->pps_conf_win_left_offset);
1691+ infer(pps_scaling_win_right_offset, current->pps_conf_win_right_offset);
1692+ infer(pps_scaling_win_top_offset, current->pps_conf_win_top_offset);
1693+ infer(pps_scaling_win_bottom_offset, current->pps_conf_win_bottom_offset);
1694+ }
1695+
1696+ flag(pps_output_flag_present_flag);
1697+ flag(pps_no_pic_partition_flag);
1698+ flag(pps_subpic_id_mapping_present_flag);
1699+
1700+ if (current->pps_subpic_id_mapping_present_flag) {
1701+ if (!current->pps_no_pic_partition_flag) {
1702+ ue(pps_num_subpics_minus1,
1703+ sps->sps_num_subpics_minus1, sps->sps_num_subpics_minus1);
1704+ } else {
1705+ infer(pps_num_subpics_minus1, 0);
1706+ }
1707+ ue(pps_subpic_id_len_minus1, sps->sps_subpic_id_len_minus1,
1708+ sps->sps_subpic_id_len_minus1);
1709+ for (i = 0; i <= current->pps_num_subpics_minus1; i++) {
1710+ ubs(sps->sps_subpic_id_len_minus1 + 1, pps_subpic_id[i], 1, i);
1711+ }
1712+ }
1713+
1714+ pic_width_in_ctbs_y = AV_CEIL_RSHIFT
1715+ (current->pps_pic_width_in_luma_samples, (sps->sps_log2_ctu_size_minus5 + 5));
1716+ pic_height_in_ctbs_y = AV_CEIL_RSHIFT(
1717+ current->pps_pic_height_in_luma_samples,(sps->sps_log2_ctu_size_minus5 + 5));
1718+ if (!current->pps_no_pic_partition_flag) {
1719+ unsigned int exp_tile_width = 0, exp_tile_height = 0;
1720+ unsigned int unified_size, remaining_size;
1721+
1722+ u(2, pps_log2_ctu_size_minus5,
1723+ sps->sps_log2_ctu_size_minus5, sps->sps_log2_ctu_size_minus5);
1724+ ue(pps_num_exp_tile_columns_minus1,
1725+ 0, FFMIN(pic_width_in_ctbs_y - 1, VVC_MAX_TILE_COLUMNS - 1));
1726+ ue(pps_num_exp_tile_rows_minus1,
1727+ 0, FFMIN(pic_height_in_ctbs_y - 1, VVC_MAX_TILE_ROWS - 1));
1728+
1729+ for (i = 0; i <= current->pps_num_exp_tile_columns_minus1; i++) {
1730+ ues(pps_tile_column_width_minus1[i],
1731+ 0, pic_width_in_ctbs_y - exp_tile_width - 1, 1, i);
1732+ exp_tile_width += current->pps_tile_column_width_minus1[i] + 1;
1733+ }
1734+ for (i = 0; i <= current->pps_num_exp_tile_rows_minus1; i++) {
1735+ ues(pps_tile_row_height_minus1[i],
1736+ 0, pic_height_in_ctbs_y - 1, 1, i);
1737+ exp_tile_height += current->pps_tile_row_height_minus1[i] + 1;
1738+ }
1739+
1740+ remaining_size = pic_width_in_ctbs_y;
1741+ for (i = 0; i <= current->pps_num_exp_tile_columns_minus1; i++) {
1742+ if (current->pps_tile_column_width_minus1[i] >= remaining_size) {
1743+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1744+ "Tile column width(%d) exceeds picture width\n",i);
1745+ return AVERROR_INVALIDDATA;
1746+ }
1747+ remaining_size -= (current->pps_tile_column_width_minus1[i] + 1);
1748+ }
1749+ unified_size = (i == 0 ? pic_width_in_ctbs_y :
1750+ (current->pps_tile_column_width_minus1[i - 1] + 1));
1751+ while (remaining_size > 0) {
1752+ if (current->num_tile_columns > VVC_MAX_TILE_COLUMNS) {
1753+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1754+ "NumTileColumns(%d) > than VVC_MAX_TILE_COLUMNS(%d)\n",
1755+ current->num_tile_columns, VVC_MAX_TILE_COLUMNS);
1756+ return AVERROR_INVALIDDATA;
1757+ }
1758+ unified_size = FFMIN(remaining_size, unified_size);
1759+ current->pps_tile_column_width_minus1[i] = unified_size - 1;
1760+ remaining_size -= unified_size;
1761+ i++;
1762+ }
1763+ current->num_tile_columns = i;
1764+ if (current->num_tile_columns > VVC_MAX_TILE_COLUMNS) {
1765+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1766+ "NumTileColumns(%d) > than VVC_MAX_TILE_COLUMNS(%d)\n",
1767+ current->num_tile_columns, VVC_MAX_TILE_COLUMNS);
1768+ return AVERROR_INVALIDDATA;
1769+ }
1770+
1771+ remaining_size = pic_height_in_ctbs_y;
1772+ for (i = 0; i <= current->pps_num_exp_tile_rows_minus1; i++) {
1773+ if (current->pps_tile_row_height_minus1[i] >= remaining_size) {
1774+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1775+ "Tile row height(%d) exceeds picture height\n",i);
1776+ return AVERROR_INVALIDDATA;
1777+ }
1778+ remaining_size -= (current->pps_tile_row_height_minus1[i] + 1);
1779+ }
1780+ unified_size = (i == 0 ? pic_height_in_ctbs_y :
1781+ (current->pps_tile_row_height_minus1[i - 1] + 1));
1782+
1783+ while (remaining_size > 0) {
1784+ unified_size = FFMIN(remaining_size, unified_size);
1785+ current->pps_tile_row_height_minus1[i] = unified_size - 1;
1786+ remaining_size -= unified_size;
1787+ i++;
1788+ }
1789+ current->num_tile_rows=i;
1790+ if (current->num_tile_rows > VVC_MAX_TILE_ROWS) {
1791+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1792+ "NumTileRows(%d) > than VVC_MAX_TILE_ROWS(%d)\n",
1793+ current->num_tile_rows, VVC_MAX_TILE_ROWS);
1794+ return AVERROR_INVALIDDATA;
1795+ }
1796+
1797+ current->num_tiles_in_pic = current->num_tile_columns *
1798+ current->num_tile_rows;
1799+ if (current->num_tiles_in_pic > VVC_MAX_TILES_PER_AU) {
1800+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1801+ "NumTilesInPic(%d) > than VVC_MAX_TILES_PER_AU(%d)\n",
1802+ current->num_tiles_in_pic, VVC_MAX_TILES_PER_AU);
1803+ return AVERROR_INVALIDDATA;
1804+ }
1805+
1806+ if (current->num_tiles_in_pic > 1) {
1807+ flag(pps_loop_filter_across_tiles_enabled_flag);
1808+ flag(pps_rect_slice_flag);
1809+ } else {
1810+ infer(pps_loop_filter_across_tiles_enabled_flag, 0);
1811+ infer(pps_rect_slice_flag, 1);
1812+ }
1813+ if (current->pps_rect_slice_flag)
1814+ flag(pps_single_slice_per_subpic_flag);
1815+ else
1816+ infer(pps_single_slice_per_subpic_flag, 1);
1817+ if (current->pps_rect_slice_flag &&
1818+ !current->pps_single_slice_per_subpic_flag) {
1819+ int j;
1820+ uint16_t tile_idx = 0, tile_x, tile_y, ctu_x, ctu_y;
1821+ uint16_t slice_top_left_ctu_x[VVC_MAX_SLICES];
1822+ uint16_t slice_top_left_ctu_y[VVC_MAX_SLICES];
1823+ ue(pps_num_slices_in_pic_minus1, 0, VVC_MAX_SLICES - 1);
1824+ if (current->pps_num_slices_in_pic_minus1 > 1)
1825+ flag(pps_tile_idx_delta_present_flag);
1826+ else
1827+ infer(pps_tile_idx_delta_present_flag, 0);
1828+ for (i = 0; i < current->pps_num_slices_in_pic_minus1; i++) {
1829+ tile_x = tile_idx % current->num_tile_columns;
1830+ tile_y = tile_idx / current->num_tile_columns;
1831+ if (tile_x != current->num_tile_columns - 1) {
1832+ ues(pps_slice_width_in_tiles_minus1[i],
1833+ 0, current->num_tile_columns - 1, 1, i);
1834+ } else {
1835+ infer(pps_slice_width_in_tiles_minus1[i], 0);
1836+ }
1837+ if (tile_y != current->num_tile_rows - 1 &&
1838+ (current->pps_tile_idx_delta_present_flag || tile_x == 0)) {
1839+ ues(pps_slice_height_in_tiles_minus1[i],
1840+ 0, current->num_tile_rows - 1, 1, i);
1841+ } else {
1842+ if (tile_y == current->num_tile_rows - 1)
1843+ infer(pps_slice_height_in_tiles_minus1[i], 0);
1844+ else
1845+ infer(pps_slice_height_in_tiles_minus1[i],
1846+ current->pps_slice_height_in_tiles_minus1[i - 1]);
1847+ }
1848+
1849+ ctu_x = ctu_y = 0;
1850+ for (j = 0; j < tile_x; j++) {
1851+ ctu_x += current->pps_tile_column_width_minus1[j] + 1;
1852+ }
1853+ for (j = 0; j < tile_y; j++) {
1854+ ctu_y += current->pps_tile_row_height_minus1[j] + 1;
1855+ }
1856+ if (current->pps_slice_width_in_tiles_minus1[i] == 0 &&
1857+ current->pps_slice_height_in_tiles_minus1[i] == 0 &&
1858+ current->pps_tile_row_height_minus1[tile_y] > 0) {
1859+ int num_slices_in_tile,
1860+ uniform_slice_height, remaining_height_in_ctbs_y;
1861+ remaining_height_in_ctbs_y =
1862+ current->pps_tile_row_height_minus1[tile_y] + 1;
1863+ ues(pps_num_exp_slices_in_tile[i],
1864+ 0, current->pps_tile_row_height_minus1[tile_y], 1, i);
1865+ if (current->pps_num_exp_slices_in_tile[i] == 0) {
1866+ num_slices_in_tile = 1;
1867+ slice_top_left_ctu_x[i] = ctu_x;
1868+ slice_top_left_ctu_y[i] = ctu_y;
1869+ } else {
1870+ uint16_t slice_height_in_ctus;
1871+ for (j = 0; j < current->pps_num_exp_slices_in_tile[i];
1872+ j++) {
1873+ ues(pps_exp_slice_height_in_ctus_minus1[i][j], 0,
1874+ current->pps_tile_row_height_minus1[tile_y], 2,
1875+ i, j);
1876+ slice_height_in_ctus =
1877+ current->
1878+ pps_exp_slice_height_in_ctus_minus1[i][j] + 1;
1879+
1880+ current->slice_height_in_ctus[i + j] =
1881+ slice_height_in_ctus;
1882+ slice_top_left_ctu_x[i + j] = ctu_x;
1883+ slice_top_left_ctu_y[i + j] = ctu_y;
1884+ ctu_y += slice_height_in_ctus;
1885+
1886+ remaining_height_in_ctbs_y -= slice_height_in_ctus;
1887+ }
1888+ uniform_slice_height = 1 +
1889+ (j == 0 ? current->pps_tile_row_height_minus1[tile_y] :
1890+ current->pps_exp_slice_height_in_ctus_minus1[i][j-1]);
1891+ while (remaining_height_in_ctbs_y > uniform_slice_height) {
1892+ current->slice_height_in_ctus[i + j] =
1893+ uniform_slice_height;
1894+ slice_top_left_ctu_x[i + j] = ctu_x;
1895+ slice_top_left_ctu_y[i + j] = ctu_y;
1896+ ctu_y += uniform_slice_height;
1897+
1898+ remaining_height_in_ctbs_y -= uniform_slice_height;
1899+ j++;
1900+ }
1901+ if (remaining_height_in_ctbs_y > 0) {
1902+ current->slice_height_in_ctus[i + j] =
1903+ remaining_height_in_ctbs_y;
1904+ slice_top_left_ctu_x[i + j] = ctu_x;
1905+ slice_top_left_ctu_y[i + j] = ctu_y;
1906+ j++;
1907+ }
1908+ num_slices_in_tile = j;
1909+ }
1910+ i += num_slices_in_tile - 1;
1911+ } else {
1912+ uint16_t height = 0;
1913+ infer(pps_num_exp_slices_in_tile[i], 0);
1914+ for (j = 0;
1915+ j <= current->pps_slice_height_in_tiles_minus1[i];
1916+ j++) {
1917+ height +=
1918+ current->pps_tile_row_height_minus1[tile_y + j] + 1;
1919+ }
1920+ current->slice_height_in_ctus[i] = height;
1921+
1922+ slice_top_left_ctu_x[i] = ctu_x;
1923+ slice_top_left_ctu_y[i] = ctu_y;
1924+ }
1925+ if (i < current->pps_num_slices_in_pic_minus1) {
1926+ if (current->pps_tile_idx_delta_present_flag) {
1927+ ses(pps_tile_idx_delta_val[i],
1928+ -current->num_tiles_in_pic + 1,
1929+ current->num_tiles_in_pic - 1, 1, i);
1930+ if (current->pps_tile_idx_delta_val[i] == 0) {
1931+ av_log(ctx->log_ctx, AV_LOG_ERROR,
1932+ "pps_tile_idx_delta_val[i] shall not be equal to 0.\n");
1933+ }
1934+ tile_idx += current->pps_tile_idx_delta_val[i];
1935+ } else {
1936+ infer(pps_tile_idx_delta_val[i], 0);
1937+ tile_idx +=
1938+ current->pps_slice_width_in_tiles_minus1[i] + 1;
1939+ if (tile_idx % current->num_tile_columns == 0) {
1940+ tile_idx +=
1941+ current->pps_slice_height_in_tiles_minus1[i] *
1942+ current->num_tile_columns;
1943+ }
1944+ }
1945+ }
1946+ }
1947+ if (i == current->pps_num_slices_in_pic_minus1) {
1948+ uint16_t height = 0;
1949+
1950+ tile_x = tile_idx % current->num_tile_columns;
1951+ tile_y = tile_idx / current->num_tile_columns;
1952+
1953+ ctu_x = 0, ctu_y = 0;
1954+ for (j = 0; j < tile_x; j++) {
1955+ ctu_x += current->pps_tile_column_width_minus1[j] + 1;
1956+ }
1957+ for (j = 0; j < tile_y; j++) {
1958+ ctu_y += current->pps_tile_row_height_minus1[j] + 1;
1959+ }
1960+ slice_top_left_ctu_x[i] = ctu_x;
1961+ slice_top_left_ctu_y[i] = ctu_y;
1962+
1963+ current->pps_slice_width_in_tiles_minus1[i] =
1964+ current->num_tile_columns - tile_x - 1;
1965+ current->pps_slice_height_in_tiles_minus1[i] =
1966+ current->num_tile_rows - tile_y - 1;
1967+
1968+ for (j = 0; j <= current->pps_slice_height_in_tiles_minus1[i];
1969+ j++) {
1970+ height +=
1971+ current->pps_tile_row_height_minus1[tile_y + j] + 1;
1972+ }
1973+ current->slice_height_in_ctus[i] = height;
1974+
1975+ infer(pps_num_exp_slices_in_tile[i], 0);
1976+ }
1977+ //now, we got all slice information, let's resolve NumSlicesInSubpic
1978+ for (i = 0; i <= sps->sps_num_subpics_minus1; i++) {
1979+ current->num_slices_in_subpic[i] = 0;
1980+ for (j = 0; j <= current->pps_num_slices_in_pic_minus1; j++) {
1981+ uint16_t pos_x = 0, pos_y = 0;
1982+ pos_x = slice_top_left_ctu_x[j];
1983+ pos_y = slice_top_left_ctu_y[j];
1984+ if ((pos_x >= sps->sps_subpic_ctu_top_left_x[i]) &&
1985+ (pos_x <
1986+ sps->sps_subpic_ctu_top_left_x[i] +
1987+ sps->sps_subpic_width_minus1[i] + 1) &&
1988+ (pos_y >= sps->sps_subpic_ctu_top_left_y[i]) &&
1989+ (pos_y < sps->sps_subpic_ctu_top_left_y[i] +
1990+ sps->sps_subpic_height_minus1[i] + 1)) {
1991+ current->num_slices_in_subpic[i]++;
1992+ }
1993+ }
1994+ }
1995+ } else {
1996+ if (current->pps_no_pic_partition_flag)
1997+ infer(pps_num_slices_in_pic_minus1, 0);
1998+ else if (current->pps_single_slice_per_subpic_flag)
1999+ infer(pps_num_slices_in_pic_minus1,
2000+ sps->sps_num_subpics_minus1);
2001+ // else?
2002+ }
2003+ if (!current->pps_rect_slice_flag ||
2004+ current->pps_single_slice_per_subpic_flag ||
2005+ current->pps_num_slices_in_pic_minus1 > 0)
2006+ flag(pps_loop_filter_across_slices_enabled_flag);
2007+ else
2008+ infer(pps_loop_filter_across_slices_enabled_flag, 0);
2009+ } else {
2010+ infer(pps_num_exp_tile_columns_minus1, 0);
2011+ infer(pps_tile_column_width_minus1[0], pic_width_in_ctbs_y - 1);
2012+ infer(pps_num_exp_tile_rows_minus1, 0);
2013+ infer(pps_tile_row_height_minus1[0], pic_height_in_ctbs_y - 1);
2014+ infer(num_tile_columns, 1);
2015+ infer(num_tile_rows, 1);
2016+ infer(num_tiles_in_pic, 1);
2017+ }
2018+
2019+ flag(pps_cabac_init_present_flag);
2020+ for (i = 0; i < 2; i++)
2021+ ues(pps_num_ref_idx_default_active_minus1[i], 0, 14, 1, i);
2022+ flag(pps_rpl1_idx_present_flag);
2023+ flag(pps_weighted_pred_flag);
2024+ flag(pps_weighted_bipred_flag);
2025+ flag(pps_ref_wraparound_enabled_flag);
2026+ if (current->pps_ref_wraparound_enabled_flag) {
2027+ ue(pps_pic_width_minus_wraparound_offset,
2028+ 0, (current->pps_pic_width_in_luma_samples / min_cb_size_y)
2029+ - (ctb_size_y / min_cb_size_y) - 2);
2030+ }
2031+
2032+ qp_bd_offset = 6 * sps->sps_bitdepth_minus8;
2033+ se(pps_init_qp_minus26, -(26 + qp_bd_offset), 37);
2034+ flag(pps_cu_qp_delta_enabled_flag);
2035+ flag(pps_chroma_tool_offsets_present_flag);
2036+ if (current->pps_chroma_tool_offsets_present_flag) {
2037+ se(pps_cb_qp_offset, -12, 12);
2038+ se(pps_cr_qp_offset, -12, 12);
2039+ flag(pps_joint_cbcr_qp_offset_present_flag);
2040+ if (current->pps_joint_cbcr_qp_offset_present_flag)
2041+ se(pps_joint_cbcr_qp_offset_value, -12, 12);
2042+ else
2043+ infer(pps_joint_cbcr_qp_offset_value, 0);
2044+ flag(pps_slice_chroma_qp_offsets_present_flag);
2045+ flag(pps_cu_chroma_qp_offset_list_enabled_flag);
2046+ if (current->pps_cu_chroma_qp_offset_list_enabled_flag) {
2047+ ue(pps_chroma_qp_offset_list_len_minus1, 0, 5);
2048+ for (i = 0; i <= current->pps_chroma_qp_offset_list_len_minus1; i++) {
2049+ ses(pps_cb_qp_offset_list[i], -12, 12, 1, i);
2050+ ses(pps_cr_qp_offset_list[i], -12, 12, 1, i);
2051+ if (current->pps_joint_cbcr_qp_offset_present_flag)
2052+ ses(pps_joint_cbcr_qp_offset_list[i], -12, 12, 1, i);
2053+ else
2054+ infer(pps_joint_cbcr_qp_offset_list[i], 0);
2055+ }
2056+ }
2057+ } else {
2058+ infer(pps_cb_qp_offset, 0);
2059+ infer(pps_cr_qp_offset, 0);
2060+ infer(pps_joint_cbcr_qp_offset_present_flag, 0);
2061+ infer(pps_joint_cbcr_qp_offset_value, 0);
2062+ infer(pps_slice_chroma_qp_offsets_present_flag, 0);
2063+ infer(pps_cu_chroma_qp_offset_list_enabled_flag, 0);
2064+ }
2065+ flag(pps_deblocking_filter_control_present_flag);
2066+ if (current->pps_deblocking_filter_control_present_flag) {
2067+ flag(pps_deblocking_filter_override_enabled_flag);
2068+ flag(pps_deblocking_filter_disabled_flag);
2069+ if (!current->pps_no_pic_partition_flag &&
2070+ current->pps_deblocking_filter_override_enabled_flag)
2071+ flag(pps_dbf_info_in_ph_flag);
2072+ else
2073+ infer(pps_dbf_info_in_ph_flag, 0);
2074+ if (!current->pps_deblocking_filter_disabled_flag) {
2075+ se(pps_luma_beta_offset_div2, -12, 12);
2076+ se(pps_luma_tc_offset_div2, -12, 12);
2077+ if (current->pps_chroma_tool_offsets_present_flag) {
2078+ se(pps_cb_beta_offset_div2, -12, 12);
2079+ se(pps_cb_tc_offset_div2, -12, 12);
2080+ se(pps_cr_beta_offset_div2, -12, 12);
2081+ se(pps_cr_tc_offset_div2, -12, 12);
2082+ } else {
2083+ infer(pps_cb_beta_offset_div2,
2084+ current->pps_luma_beta_offset_div2);
2085+ infer(pps_cb_tc_offset_div2, current->pps_luma_tc_offset_div2);
2086+ infer(pps_cr_beta_offset_div2,
2087+ current->pps_luma_beta_offset_div2);
2088+ infer(pps_cr_tc_offset_div2, current->pps_luma_tc_offset_div2);
2089+ }
2090+ } else {
2091+ infer(pps_luma_beta_offset_div2, 0);
2092+ infer(pps_luma_tc_offset_div2, 0);
2093+ infer(pps_cb_beta_offset_div2, 0);
2094+ infer(pps_cb_tc_offset_div2, 0);
2095+ infer(pps_cr_beta_offset_div2, 0);
2096+ infer(pps_cr_tc_offset_div2, 0);
2097+ }
2098+ } else {
2099+ infer(pps_deblocking_filter_override_enabled_flag, 0);
2100+ infer(pps_deblocking_filter_disabled_flag, 0);
2101+ infer(pps_dbf_info_in_ph_flag, 0);
2102+ infer(pps_luma_beta_offset_div2, 0);
2103+ infer(pps_luma_tc_offset_div2, 0);
2104+ infer(pps_cb_beta_offset_div2, 0);
2105+ infer(pps_cb_tc_offset_div2, 0);
2106+ infer(pps_cr_beta_offset_div2, 0);
2107+ infer(pps_cr_tc_offset_div2, 0);
2108+ }
2109+
2110+ if (!current->pps_no_pic_partition_flag) {
2111+ flag(pps_rpl_info_in_ph_flag);
2112+ flag(pps_sao_info_in_ph_flag);
2113+ flag(pps_alf_info_in_ph_flag);
2114+ if ((current->pps_weighted_pred_flag ||
2115+ current->pps_weighted_bipred_flag) &&
2116+ current->pps_rpl_info_in_ph_flag)
2117+ flag(pps_wp_info_in_ph_flag);
2118+ flag(pps_qp_delta_info_in_ph_flag);
2119+ }
2120+ flag(pps_picture_header_extension_present_flag);
2121+ flag(pps_slice_header_extension_present_flag);
2122+
2123+ flag(pps_extension_flag);
2124+ if (current->pps_extension_flag)
2125+ CHECK(FUNC(extension_data) (ctx, rw, &current->extension_data));
2126+
2127+ CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
2128+ return 0;
2129+}
2130+
2131+static int FUNC(aud) (CodedBitstreamContext *ctx, RWContext *rw,
2132+ H266RawAUD *current)
2133+{
2134+ int err;
2135+
2136+ HEADER("Access Unit Delimiter");
2137+
2138+ CHECK(FUNC(nal_unit_header) (ctx, rw,
2139+ &current->nal_unit_header, VVC_AUD_NUT));
2140+
2141+ flag(aud_irap_or_gdr_flag);
2142+ u(3, aud_pic_type, 0, 2);
2143+
2144+ CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
2145+ return 0;
2146+}
2147+
2148+static int FUNC(pred_weight_table) (CodedBitstreamContext *ctx, RWContext *rw,
2149+ const H266RawSPS *sps,
2150+ const H266RawPPS *pps,
2151+ const H266RefPicLists *ref_lists,
2152+ uint8_t num_ref_idx_active[2],
2153+ H266RawPredWeightTable *current)
2154+{
2155+ int err, i, j;
2156+ uint8_t num_weights_l0, num_weights_l1;
2157+ ue(luma_log2_weight_denom, 0, 7);
2158+ if (sps->sps_chroma_format_idc != 0) {
2159+ se(delta_chroma_log2_weight_denom,
2160+ -current->luma_log2_weight_denom,
2161+ 7 - current->luma_log2_weight_denom);
2162+ } else {
2163+ infer(delta_chroma_log2_weight_denom, 0);
2164+ }
2165+ if (pps->pps_wp_info_in_ph_flag)
2166+ ue(num_l0_weights, 0,
2167+ FFMIN(15, ref_lists->rpl_ref_list[0].num_ref_entries));
2168+ else
2169+ infer(num_l0_weights, 0);
2170+ num_weights_l0 = pps->pps_wp_info_in_ph_flag ?
2171+ current->num_l0_weights : num_ref_idx_active[0];
2172+ for (i = 0; i < num_weights_l0; i++) {
2173+ flags(luma_weight_l0_flag[i], 1, i);
2174+ }
2175+ if (sps->sps_chroma_format_idc != 0) {
2176+ for (i = 0; i < num_weights_l0; i++)
2177+ flags(chroma_weight_l0_flag[i], 1, i);
2178+ }
2179+ for (i = 0; i < num_weights_l0; i++) {
2180+ if (current->luma_weight_l0_flag[i]) {
2181+ ses(delta_luma_weight_l0[i], -128, 127, 1, i);
2182+ ses(luma_offset_l0[i], -128, 127, 1, i);
2183+ } else {
2184+ infer(delta_luma_weight_l0[i], 0);
2185+ infer(luma_offset_l0[i], 0);
2186+ }
2187+ if (current->chroma_weight_l0_flag[i]) {
2188+ for (j = 0; j < 2; j++) {
2189+ ses(delta_chroma_weight_l0[i][j], -128, 127, 2, i, j);
2190+ ses(delta_chroma_offset_l0[i][j], -4 * 128, 4 * 127, 2, i, j);
2191+ }
2192+ }
2193+ }
2194+
2195+ if (pps->pps_weighted_bipred_flag && pps->pps_wp_info_in_ph_flag &&
2196+ ref_lists->rpl_ref_list[1].num_ref_entries > 0) {
2197+ ue(num_l1_weights, 0,
2198+ FFMIN(15, ref_lists->rpl_ref_list[1].num_ref_entries));
2199+ }
2200+ if (!pps->pps_weighted_bipred_flag ||
2201+ (pps->pps_wp_info_in_ph_flag &&
2202+ ref_lists->rpl_ref_list[1].num_ref_entries == 0)) {
2203+ num_weights_l1 = 0;
2204+ } else if (pps->pps_wp_info_in_ph_flag) {
2205+ num_weights_l1 = current->num_l1_weights;
2206+ } else {
2207+ num_weights_l1 = num_ref_idx_active[1];
2208+ }
2209+
2210+ for (i = 0; i < num_weights_l1; i++)
2211+ flags(luma_weight_l1_flag[i], 1, i);
2212+ if (sps->sps_chroma_format_idc != 0) {
2213+ for (i = 0; i < num_weights_l1; i++)
2214+ flags(chroma_weight_l1_flag[i], 1, i);
2215+ }
2216+ for (i = 0; i < num_weights_l1; i++) {
2217+ if (current->luma_weight_l1_flag[i]) {
2218+ ses(delta_luma_weight_l1[i], -128, 127, 1, i);
2219+ ses(luma_offset_l1[i], -128, 127, 1, i);
2220+ } else {
2221+ infer(delta_luma_weight_l1[i], 0);
2222+ infer(luma_offset_l1[i], 0);
2223+ }
2224+ if (current->chroma_weight_l1_flag[i]) {
2225+ for (j = 0; j < 2; j++) {
2226+ ses(delta_chroma_weight_l1[i][j], -128, 127, 2, i, j);
2227+ ses(delta_chroma_offset_l1[i][j], -4 * 128, 4 * 127, 2, i, j);
2228+ }
2229+ }
2230+ }
2231+ return 0;
2232+}
2233+
2234+static int FUNC(picture_header) (CodedBitstreamContext *ctx, RWContext *rw,
2235+ H266RawPH *current){
2236+ CodedBitstreamH266Context *h266 = ctx->priv_data;
2237+ const H266RawVPS *vps;
2238+ const H266RawSPS *sps;
2239+ const H266RawPPS *pps;
2240+ int err, i;
2241+ unsigned int ctb_log2_size_y, min_cb_log2_size_y,
2242+ min_qt_log2_size_intra_y, min_qt_log2_size_inter_y;
2243+ uint8_t qp_bd_offset;
2244+
2245+ flag(ph_gdr_or_irap_pic_flag);
2246+ flag(ph_non_ref_pic_flag);
2247+ if (current->ph_gdr_or_irap_pic_flag)
2248+ flag(ph_gdr_pic_flag);
2249+ else
2250+ infer(ph_gdr_pic_flag, 0);
2251+ flag(ph_inter_slice_allowed_flag);
2252+ if (current->ph_inter_slice_allowed_flag)
2253+ flag(ph_intra_slice_allowed_flag);
2254+ else
2255+ infer(ph_intra_slice_allowed_flag, 1);
2256+ ue(ph_pic_parameter_set_id, 0, VVC_MAX_PPS_COUNT - 1);
2257+ pps = h266->pps[current->ph_pic_parameter_set_id];
2258+ if (!pps) {
2259+ av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
2260+ current->ph_pic_parameter_set_id);
2261+ return AVERROR_INVALIDDATA;
2262+ }
2263+ sps = h266->sps[pps->pps_seq_parameter_set_id];
2264+ if (!sps) {
2265+ av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
2266+ pps->pps_seq_parameter_set_id);
2267+ return AVERROR_INVALIDDATA;
2268+ }
2269+ vps = h266->vps[sps->sps_video_parameter_set_id];
2270+ if (!vps) {
2271+ av_log(ctx->log_ctx, AV_LOG_ERROR, "VPS id %d not available.\n",
2272+ sps->sps_video_parameter_set_id);
2273+ return AVERROR_INVALIDDATA;
2274+ }
2275+
2276+ ub(sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4, ph_pic_order_cnt_lsb);
2277+ if (current->ph_gdr_pic_flag)
2278+ ue(ph_recovery_poc_cnt, 0,
2279+ 1 << (sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4));
2280+
2281+ for (i = 0; i < sps->sps_num_extra_ph_bytes * 8; i++) {
2282+ if (sps->sps_extra_ph_bit_present_flag[i])
2283+ flags(ph_extra_bit[i], 1, i);
2284+ }
2285+ if (sps->sps_poc_msb_cycle_flag) {
2286+ flag(ph_poc_msb_cycle_present_flag);
2287+ if (current->ph_poc_msb_cycle_present_flag)
2288+ ub(sps->sps_poc_msb_cycle_len_minus1 + 1, ph_poc_msb_cycle_val);
2289+ }
2290+ if (sps->sps_alf_enabled_flag && pps->pps_alf_info_in_ph_flag) {
2291+ flag(ph_alf_enabled_flag);
2292+ if (current->ph_alf_enabled_flag) {
2293+
2294+ ub(3, ph_num_alf_aps_ids_luma);
2295+ for (i = 0; i < current->ph_num_alf_aps_ids_luma; i++)
2296+ ubs(3, ph_alf_aps_id_luma[i], 1, i);
2297+
2298+ if (sps->sps_chroma_format_idc != 0) {
2299+ flag(ph_alf_cb_enabled_flag);
2300+ flag(ph_alf_cr_enabled_flag);
2301+ } else {
2302+ infer(ph_alf_cb_enabled_flag, 0);
2303+ infer(ph_alf_cr_enabled_flag, 0);
2304+ }
2305+
2306+ if (current->ph_alf_cb_enabled_flag
2307+ || current->ph_alf_cr_enabled_flag) {
2308+ ub(3, ph_alf_aps_id_chroma);
2309+ }
2310+
2311+ if (sps->sps_ccalf_enabled_flag) {
2312+ flag(ph_alf_cc_cb_enabled_flag);
2313+ if (current->ph_alf_cc_cb_enabled_flag)
2314+ ub(3, ph_alf_cc_cb_aps_id);
2315+ flag(ph_alf_cc_cr_enabled_flag);
2316+ if (current->ph_alf_cc_cr_enabled_flag)
2317+ ub(3, ph_alf_cc_cr_aps_id);
2318+ }
2319+ }
2320+ } else {
2321+ infer(ph_alf_enabled_flag, 0);
2322+ }
2323+ if (sps->sps_lmcs_enabled_flag) {
2324+ flag(ph_lmcs_enabled_flag);
2325+ if (current->ph_lmcs_enabled_flag) {
2326+ ub(2, ph_lmcs_aps_id);
2327+ if (sps->sps_chroma_format_idc != 0)
2328+ flag(ph_chroma_residual_scale_flag);
2329+ else
2330+ infer(ph_chroma_residual_scale_flag, 0);
2331+ }
2332+ } else {
2333+ infer(ph_lmcs_enabled_flag, 0);
2334+ infer(ph_chroma_residual_scale_flag, 0);
2335+ }
2336+
2337+ if (sps->sps_explicit_scaling_list_enabled_flag) {
2338+ flag(ph_explicit_scaling_list_enabled_flag);
2339+ if (current->ph_explicit_scaling_list_enabled_flag) {
2340+ //todo: check the ph_scaling_list_aps_id range, when aps ready
2341+ ub(3, ph_scaling_list_aps_id);
2342+ }
2343+ } else {
2344+ infer(ph_explicit_scaling_list_enabled_flag, 0);
2345+ }
2346+ if (sps->sps_virtual_boundaries_enabled_flag &&
2347+ !sps->sps_virtual_boundaries_present_flag) {
2348+ flag(ph_virtual_boundaries_present_flag);
2349+ if (current->ph_virtual_boundaries_present_flag) {
2350+ ue(ph_num_ver_virtual_boundaries,
2351+ 0, pps->pps_pic_width_in_luma_samples <= 8 ? 0 : 3);
2352+ for (i = 0; i < current->ph_num_ver_virtual_boundaries; i++) {
2353+ ues(ph_virtual_boundary_pos_x_minus1[i],
2354+ 0, (pps->pps_pic_width_in_luma_samples + 7) / 8 - 2, 1, i);
2355+ }
2356+ ue(ph_num_hor_virtual_boundaries,
2357+ 0, pps->pps_pic_height_in_luma_samples <= 8 ? 0 : 3);
2358+ for (i = 0; i < current->ph_num_hor_virtual_boundaries; i++) {
2359+ ues(ph_virtual_boundary_pos_y_minus1[i],
2360+ 0, (pps->pps_pic_height_in_luma_samples + 7) / 8 - 2, 1, i);
2361+ }
2362+ } else {
2363+ infer(ph_num_ver_virtual_boundaries, 0);
2364+ infer(ph_num_hor_virtual_boundaries, 0);
2365+ }
2366+ }
2367+ if (pps->pps_output_flag_present_flag && !current->ph_non_ref_pic_flag)
2368+ flag(ph_pic_output_flag);
2369+ else
2370+ infer(ph_pic_output_flag, 1);
2371+ if (pps->pps_rpl_info_in_ph_flag) {
2372+ CHECK(FUNC(ref_pic_lists)
2373+ (ctx, rw, sps, pps, &current->ph_ref_pic_lists));
2374+ }
2375+ if (sps->sps_partition_constraints_override_enabled_flag)
2376+ flag(ph_partition_constraints_override_flag);
2377+ else
2378+ infer(ph_partition_constraints_override_flag, 0);
2379+
2380+ ctb_log2_size_y = sps->sps_log2_ctu_size_minus5 + 5;
2381+ min_cb_log2_size_y = sps->sps_log2_min_luma_coding_block_size_minus2 + 2;
2382+ if (current->ph_intra_slice_allowed_flag) {
2383+ if (current->ph_partition_constraints_override_flag) {
2384+ ue(ph_log2_diff_min_qt_min_cb_intra_slice_luma,
2385+ 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
2386+ ue(ph_max_mtt_hierarchy_depth_intra_slice_luma,
2387+ 0, 2 * (ctb_log2_size_y - min_cb_log2_size_y));
2388+ if (current->ph_max_mtt_hierarchy_depth_intra_slice_luma != 0) {
2389+ ue(ph_log2_diff_max_bt_min_qt_intra_slice_luma,
2390+ 0, ctb_log2_size_y - min_qt_log2_size_intra_y);
2391+ ue(ph_log2_diff_max_tt_min_qt_intra_slice_luma,
2392+ 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_y);
2393+ } else {
2394+ infer(ph_log2_diff_max_bt_min_qt_intra_slice_luma,
2395+ sps->sps_log2_diff_max_bt_min_qt_intra_slice_luma);
2396+ infer(ph_log2_diff_max_tt_min_qt_intra_slice_luma,
2397+ sps->sps_log2_diff_max_tt_min_qt_intra_slice_luma);
2398+ }
2399+ if (sps->sps_qtbtt_dual_tree_intra_flag) {
2400+ ue(ph_log2_diff_min_qt_min_cb_intra_slice_chroma,
2401+ 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
2402+ ue(ph_max_mtt_hierarchy_depth_intra_slice_chroma,
2403+ 0, 2 * (ctb_log2_size_y - min_cb_log2_size_y));
2404+ if (sps->sps_max_mtt_hierarchy_depth_intra_slice_chroma != 0) {
2405+ unsigned int min_qt_log2_size_intra_c =
2406+ sps->sps_log2_diff_min_qt_min_cb_intra_slice_chroma +
2407+ min_cb_log2_size_y;
2408+ ue(ph_log2_diff_max_bt_min_qt_intra_slice_chroma,
2409+ 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c);
2410+ ue(ph_log2_diff_max_tt_min_qt_intra_slice_chroma,
2411+ 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_intra_c);
2412+ } else {
2413+ infer(ph_log2_diff_max_bt_min_qt_intra_slice_chroma,
2414+ sps->sps_log2_diff_max_bt_min_qt_intra_slice_chroma);
2415+ infer(ph_log2_diff_max_tt_min_qt_intra_slice_chroma,
2416+ sps->sps_log2_diff_max_tt_min_qt_intra_slice_chroma);
2417+ }
2418+ }
2419+ } else {
2420+ infer(ph_log2_diff_min_qt_min_cb_intra_slice_luma,
2421+ sps->sps_log2_diff_min_qt_min_cb_intra_slice_luma);
2422+ infer(ph_max_mtt_hierarchy_depth_intra_slice_luma,
2423+ sps->sps_max_mtt_hierarchy_depth_intra_slice_luma);
2424+ infer(ph_log2_diff_max_bt_min_qt_intra_slice_luma,
2425+ sps->sps_log2_diff_max_bt_min_qt_intra_slice_luma);
2426+ infer(ph_log2_diff_max_tt_min_qt_intra_slice_luma,
2427+ sps->sps_log2_diff_max_tt_min_qt_intra_slice_luma);
2428+ infer(ph_log2_diff_min_qt_min_cb_intra_slice_chroma,
2429+ sps->sps_log2_diff_min_qt_min_cb_intra_slice_chroma);
2430+ infer(ph_max_mtt_hierarchy_depth_intra_slice_chroma,
2431+ sps->sps_max_mtt_hierarchy_depth_intra_slice_chroma);
2432+ infer(ph_log2_diff_max_bt_min_qt_intra_slice_chroma,
2433+ sps->sps_log2_diff_max_bt_min_qt_intra_slice_chroma);
2434+ infer(ph_log2_diff_max_tt_min_qt_intra_slice_chroma,
2435+ sps->sps_log2_diff_max_tt_min_qt_intra_slice_chroma);
2436+ }
2437+
2438+ min_qt_log2_size_intra_y =
2439+ current->ph_log2_diff_min_qt_min_cb_intra_slice_luma +
2440+ min_cb_log2_size_y;
2441+ if (pps->pps_cu_qp_delta_enabled_flag)
2442+ ue(ph_cu_qp_delta_subdiv_intra_slice, 0,
2443+ 2 * (ctb_log2_size_y - min_qt_log2_size_intra_y +
2444+ current->ph_max_mtt_hierarchy_depth_intra_slice_luma));
2445+ else
2446+ infer(ph_cu_qp_delta_subdiv_intra_slice, 0);
2447+
2448+ if (pps->pps_cu_chroma_qp_offset_list_enabled_flag)
2449+ ue(ph_cu_chroma_qp_offset_subdiv_intra_slice, 0,
2450+ 2 * (ctb_log2_size_y - min_qt_log2_size_intra_y +
2451+ current->ph_max_mtt_hierarchy_depth_intra_slice_luma));
2452+ else
2453+ infer(ph_cu_chroma_qp_offset_subdiv_intra_slice, 0);
2454+ }
2455+ if (current->ph_inter_slice_allowed_flag) {
2456+ if (current->ph_partition_constraints_override_flag) {
2457+ ue(ph_log2_diff_min_qt_min_cb_inter_slice,
2458+ 0, FFMIN(6, ctb_log2_size_y) - min_cb_log2_size_y);
2459+ min_qt_log2_size_inter_y =
2460+ current->ph_log2_diff_min_qt_min_cb_inter_slice +
2461+ min_cb_log2_size_y;
2462+ ue(ph_max_mtt_hierarchy_depth_inter_slice, 0,
2463+ 2 * (ctb_log2_size_y - min_cb_log2_size_y));
2464+ if (current->ph_max_mtt_hierarchy_depth_inter_slice != 0) {
2465+ ue(ph_log2_diff_max_bt_min_qt_inter_slice,
2466+ 0, ctb_log2_size_y - min_qt_log2_size_inter_y);
2467+ ue(ph_log2_diff_max_tt_min_qt_inter_slice,
2468+ 0, FFMIN(6, ctb_log2_size_y) - min_qt_log2_size_inter_y);
2469+ }
2470+ } else {
2471+ infer(ph_log2_diff_min_qt_min_cb_inter_slice,
2472+ sps->sps_log2_diff_min_qt_min_cb_inter_slice);
2473+ min_qt_log2_size_inter_y =
2474+ current->ph_log2_diff_min_qt_min_cb_inter_slice +
2475+ min_cb_log2_size_y;
2476+ infer(ph_max_mtt_hierarchy_depth_inter_slice,
2477+ sps->sps_max_mtt_hierarchy_depth_inter_slice);
2478+ infer(ph_log2_diff_max_bt_min_qt_inter_slice,
2479+ sps->sps_log2_diff_max_bt_min_qt_inter_slice);
2480+ infer(ph_log2_diff_max_tt_min_qt_inter_slice,
2481+ sps->sps_log2_diff_max_tt_min_qt_inter_slice);
2482+ }
2483+
2484+ if (pps->pps_cu_qp_delta_enabled_flag)
2485+ ue(ph_cu_qp_delta_subdiv_inter_slice, 0,
2486+ 2 * (ctb_log2_size_y - min_qt_log2_size_inter_y +
2487+ current->ph_max_mtt_hierarchy_depth_inter_slice));
2488+ else
2489+ infer(ph_cu_qp_delta_subdiv_inter_slice, 0);
2490+
2491+ if (pps->pps_cu_chroma_qp_offset_list_enabled_flag)
2492+ ue(ph_cu_chroma_qp_offset_subdiv_inter_slice, 0,
2493+ 2 * (ctb_log2_size_y - min_qt_log2_size_inter_y +
2494+ current->ph_max_mtt_hierarchy_depth_inter_slice));
2495+ else
2496+ infer(ph_cu_chroma_qp_offset_subdiv_inter_slice, 0);
2497+ if (sps->sps_temporal_mvp_enabled_flag) {
2498+ flag(ph_temporal_mvp_enabled_flag);
2499+ if (current->ph_temporal_mvp_enabled_flag &&
2500+ pps->pps_rpl_info_in_ph_flag) {
2501+ if (current->ph_ref_pic_lists.rpl_ref_list[1].num_ref_entries > 0)
2502+ flag(ph_collocated_from_l0_flag);
2503+ else
2504+ infer(ph_collocated_from_l0_flag, 1);
2505+ if ((current->ph_collocated_from_l0_flag &&
2506+ current->ph_ref_pic_lists.rpl_ref_list[0].num_ref_entries > 1)
2507+ || (!current->ph_collocated_from_l0_flag &&
2508+ current->ph_ref_pic_lists.rpl_ref_list[1].num_ref_entries > 1)) {
2509+ unsigned int idx =
2510+ current->ph_collocated_from_l0_flag ? 0 : 1;
2511+ ue(ph_collocated_ref_idx, 0,
2512+ current->ph_ref_pic_lists.rpl_ref_list[idx].
2513+ num_ref_entries - 1);
2514+ } else {
2515+ infer(ph_collocated_ref_idx, 0);
2516+ }
2517+ }
2518+ }
2519+ if (sps->sps_mmvd_fullpel_only_enabled_flag)
2520+ flag(ph_mmvd_fullpel_only_flag);
2521+ else
2522+ infer(ph_mmvd_fullpel_only_flag, 0);
2523+ if (!pps->pps_rpl_info_in_ph_flag ||
2524+ current->ph_ref_pic_lists.rpl_ref_list[1].num_ref_entries > 0) {
2525+ flag(ph_mvd_l1_zero_flag);
2526+ if (sps->sps_bdof_control_present_in_ph_flag) {
2527+ flag(ph_bdof_disabled_flag);
2528+ } else {
2529+ if (!sps->sps_bdof_control_present_in_ph_flag)
2530+ infer(ph_bdof_disabled_flag,
2531+ 1 - sps->sps_bdof_enabled_flag);
2532+ else
2533+ infer(ph_bdof_disabled_flag, 1);
2534+ }
2535+ if (sps->sps_dmvr_control_present_in_ph_flag) {
2536+ flag(ph_dmvr_disabled_flag);
2537+ } else {
2538+ if (!sps->sps_dmvr_control_present_in_ph_flag)
2539+ infer(ph_dmvr_disabled_flag,
2540+ 1 - sps->sps_dmvr_enabled_flag);
2541+ else
2542+ infer(ph_dmvr_disabled_flag, 1);
2543+ }
2544+ } else {
2545+ infer(ph_mvd_l1_zero_flag, 1);
2546+ }
2547+ if (sps->sps_prof_control_present_in_ph_flag)
2548+ flag(ph_prof_disabled_flag);
2549+ else
2550+ infer(ph_prof_disabled_flag, !sps->sps_affine_prof_enabled_flag);
2551+ if ((pps->pps_weighted_pred_flag ||
2552+ pps->pps_weighted_bipred_flag) && pps->pps_wp_info_in_ph_flag) {
2553+
2554+ // if pps->pps_wp_info_in_ph_fla == 1
2555+ // pred_weight_table will not use num_ref_idx_active
2556+ uint8_t num_ref_idx_active[2] = { 0, 0 };
2557+ CHECK(FUNC(pred_weight_table)
2558+ (ctx, rw, sps, pps, &current->ph_ref_pic_lists,
2559+ num_ref_idx_active, &current->ph_pred_weight_table));
2560+ }
2561+ }
2562+
2563+ qp_bd_offset = 6 * sps->sps_bitdepth_minus8;
2564+ if (pps->pps_qp_delta_info_in_ph_flag)
2565+ se(ph_qp_delta, -qp_bd_offset - (26 + pps->pps_init_qp_minus26),
2566+ 63 - (26 + pps->pps_init_qp_minus26));
2567+
2568+ if (sps->sps_joint_cbcr_enabled_flag)
2569+ flag(ph_joint_cbcr_sign_flag);
2570+ else
2571+ infer(ph_joint_cbcr_sign_flag, 0);
2572+ if (sps->sps_sao_enabled_flag && pps->pps_sao_info_in_ph_flag) {
2573+ flag(ph_sao_luma_enabled_flag);
2574+ if (sps->sps_chroma_format_idc != 0)
2575+ flag(ph_sao_chroma_enabled_flag);
2576+ else
2577+ infer(ph_sao_chroma_enabled_flag, 0);
2578+ } else {
2579+ infer(ph_sao_luma_enabled_flag, 0);
2580+ infer(ph_sao_chroma_enabled_flag, 0);
2581+ }
2582+
2583+ if (pps->pps_dbf_info_in_ph_flag) {
2584+ flag(ph_deblocking_params_present_flag);
2585+ if (current->ph_deblocking_params_present_flag) {
2586+ if (!pps->pps_deblocking_filter_disabled_flag) {
2587+ flag(ph_deblocking_filter_disabled_flag);
2588+ } else {
2589+ if (pps->pps_deblocking_filter_disabled_flag &&
2590+ current->ph_deblocking_params_present_flag) {
2591+ infer(ph_deblocking_filter_disabled_flag, 0);
2592+ } else {
2593+ infer(ph_deblocking_filter_disabled_flag,
2594+ pps->pps_deblocking_filter_disabled_flag);
2595+ }
2596+ }
2597+ if (!current->ph_deblocking_filter_disabled_flag) {
2598+ se(ph_luma_beta_offset_div2, -12, 12);
2599+ se(ph_luma_tc_offset_div2, -12, 12);
2600+ if (pps->pps_chroma_tool_offsets_present_flag) {
2601+ se(ph_cb_beta_offset_div2, -12, 12);
2602+ se(ph_cb_tc_offset_div2, -12, 12);
2603+ se(ph_cr_beta_offset_div2, -12, 12);
2604+ se(ph_cr_tc_offset_div2, -12, 12);
2605+ } else {
2606+ infer(ph_cb_beta_offset_div2,
2607+ current->ph_luma_beta_offset_div2);
2608+ infer(ph_cb_tc_offset_div2,
2609+ current->ph_luma_tc_offset_div2);
2610+ infer(ph_cr_beta_offset_div2,
2611+ current->ph_luma_beta_offset_div2);
2612+ infer(ph_cr_tc_offset_div2,
2613+ current->ph_luma_tc_offset_div2);
2614+ }
2615+ } else {
2616+ infer(ph_luma_beta_offset_div2, pps->pps_luma_beta_offset_div2);
2617+ infer(ph_luma_tc_offset_div2, pps->pps_luma_tc_offset_div2);
2618+ if (pps->pps_chroma_tool_offsets_present_flag) {
2619+ infer(ph_cb_beta_offset_div2, pps->pps_cb_beta_offset_div2);
2620+ infer(ph_cb_tc_offset_div2, pps->pps_cb_tc_offset_div2);
2621+ infer(ph_cr_beta_offset_div2, pps->pps_cr_beta_offset_div2);
2622+ infer(ph_cr_tc_offset_div2, pps->pps_cr_tc_offset_div2);
2623+ } else {
2624+ infer(ph_cb_beta_offset_div2,
2625+ current->ph_luma_beta_offset_div2);
2626+ infer(ph_cb_tc_offset_div2,
2627+ current->ph_luma_tc_offset_div2);
2628+ infer(ph_cr_beta_offset_div2,
2629+ current->ph_luma_beta_offset_div2);
2630+ infer(ph_cr_tc_offset_div2,
2631+ current->ph_luma_tc_offset_div2);
2632+ }
2633+ }
2634+ }
2635+ }
2636+
2637+ if (pps->pps_picture_header_extension_present_flag) {
2638+ ue(ph_extension_length, 0, 256);
2639+ for (i = 0; i < current->ph_extension_length; i++)
2640+ us(8, ph_extension_data_byte[i], 0x00, 0xff, 1, i);
2641+ }
2642+
2643+ return 0;
2644+}
2645+
2646+static int FUNC(ph) (CodedBitstreamContext *ctx, RWContext *rw,
2647+ H266RawPH *current)
2648+{
2649+ int err;
2650+
2651+ HEADER("Picture Header");
2652+
2653+ CHECK(FUNC(nal_unit_header) (ctx, rw, &current->nal_unit_header, VVC_PH_NUT));
2654+ CHECK(FUNC(picture_header) (ctx, rw, current));
2655+ CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
2656+ return 0;
2657+}
2658+
2659+static int FUNC(slice_header) (CodedBitstreamContext *ctx, RWContext *rw,
2660+ H266RawSliceHeader *current)
2661+{
2662+ CodedBitstreamH266Context *h266 = ctx->priv_data;
2663+ const H266RawSPS *sps;
2664+ const H266RawPPS *pps;
2665+ const H266RawPH *ph;
2666+ const H266RefPicLists *ref_pic_lists;
2667+ int err, i;
2668+ uint8_t nal_unit_type, qp_bd_offset;
2669+ uint16_t curr_subpic_idx;
2670+ uint16_t num_slices_in_subpic;
2671+
2672+ HEADER("Slice Header");
2673+
2674+ CHECK(FUNC(nal_unit_header) (ctx, rw, &current->nal_unit_header, -1));
2675+
2676+ flag(sh_picture_header_in_slice_header_flag);
2677+ if (current->sh_picture_header_in_slice_header_flag) {
2678+ CHECK(FUNC(picture_header) (ctx, rw, &current->sh_picture_header));
2679+ ph = &current->sh_picture_header;
2680+ //7.4.8 if sh_picture_header_in_slice_header_flag is true, we do not have PH NAL unit
2681+ h266->priv.ph = NULL;
2682+ } else {
2683+ ph = h266->priv.ph;
2684+ if (!ph) {
2685+ av_log(ctx->log_ctx, AV_LOG_ERROR,
2686+ "Picture header not available.\n");
2687+ return AVERROR_INVALIDDATA;
2688+ }
2689+ }
2690+
2691+ pps = h266->pps[ph->ph_pic_parameter_set_id];
2692+ if (!pps) {
2693+ av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
2694+ ph->ph_pic_parameter_set_id);
2695+ return AVERROR_INVALIDDATA;
2696+ }
2697+ sps = h266->sps[pps->pps_seq_parameter_set_id];
2698+ if (!sps) {
2699+ av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
2700+ pps->pps_seq_parameter_set_id);
2701+ return AVERROR_INVALIDDATA;
2702+ }
2703+
2704+ if (sps->sps_subpic_info_present_flag) {
2705+ ub(sps->sps_subpic_id_len_minus1 + 1, sh_subpic_id);
2706+ if (sps->sps_subpic_id_mapping_explicitly_signalled_flag) {
2707+ for (i = 0; i <= sps->sps_num_subpics_minus1; i++) {
2708+ uint16_t subpic_id_val =
2709+ pps->pps_subpic_id_mapping_present_flag ?
2710+ pps->pps_subpic_id[i] : sps->sps_subpic_id[i];
2711+ if (subpic_id_val == current->sh_subpic_id) {
2712+ curr_subpic_idx = i;
2713+ break;
2714+ }
2715+ }
2716+ } else {
2717+ curr_subpic_idx = current->sh_subpic_id;
2718+ if (curr_subpic_idx > sps->sps_num_subpics_minus1) {
2719+ av_log(ctx->log_ctx, AV_LOG_ERROR,
2720+ "sh_subpic_id(%d) should in range [0, %d]\n",
2721+ curr_subpic_idx, sps->sps_num_subpics_minus1);
2722+ return AVERROR_INVALIDDATA;
2723+ }
2724+ }
2725+ } else {
2726+ curr_subpic_idx = 0;
2727+ }
2728+
2729+ num_slices_in_subpic = pps->num_slices_in_subpic[curr_subpic_idx];
2730+
2731+ if ((pps->pps_rect_slice_flag && num_slices_in_subpic > 1) ||
2732+ (!pps->pps_rect_slice_flag && pps->num_tiles_in_pic > 1)) {
2733+ unsigned int bits, max;
2734+ if (!pps->pps_rect_slice_flag) {
2735+ bits = av_ceil_log2(pps->num_tiles_in_pic);
2736+ max = pps->num_tiles_in_pic - 1;
2737+ } else {
2738+ bits = av_ceil_log2(num_slices_in_subpic);
2739+ max = num_slices_in_subpic - 1;
2740+ }
2741+ u(bits, sh_slice_address, 0, max);
2742+ } else {
2743+ infer(sh_slice_address, 0);
2744+ }
2745+
2746+ for (i = 0; i < sps->sps_num_extra_sh_bytes * 8; i++) {
2747+ if (sps->sps_extra_sh_bit_present_flag[i])
2748+ flags(sh_extra_bit[i], 1, i);
2749+ }
2750+
2751+ if (!pps->pps_rect_slice_flag &&
2752+ pps->num_tiles_in_pic - current->sh_slice_address > 1)
2753+ ue(sh_num_tiles_in_slice_minus1, 0, pps->num_tiles_in_pic - 1);
2754+ else
2755+ infer(sh_num_tiles_in_slice_minus1, 0);
2756+
2757+ if (ph->ph_inter_slice_allowed_flag)
2758+ ue(sh_slice_type, 0, 2);
2759+ else
2760+ infer(sh_slice_type, 2);
2761+
2762+ nal_unit_type = current->nal_unit_header.nal_unit_type;
2763+ if (nal_unit_type == VVC_IDR_W_RADL || nal_unit_type == VVC_IDR_N_LP ||
2764+ nal_unit_type == VVC_CRA_NUT || nal_unit_type == VVC_GDR_NUT)
2765+ flag(sh_no_output_of_prior_pics_flag);
2766+ if (sps->sps_alf_enabled_flag && !pps->pps_alf_info_in_ph_flag) {
2767+ flag(sh_alf_enabled_flag);
2768+ if (current->sh_alf_enabled_flag) {
2769+ ub(3, sh_num_alf_aps_ids_luma);
2770+ for (i = 0; i < current->sh_num_alf_aps_ids_luma; i++)
2771+ ubs(3, sh_alf_aps_id_luma[i], 1, i);
2772+ if (sps->sps_chroma_format_idc != 0) {
2773+ flag(sh_alf_cb_enabled_flag);
2774+ flag(sh_alf_cr_enabled_flag);
2775+ } else {
2776+ infer(sh_alf_cb_enabled_flag, ph->ph_alf_cb_enabled_flag);
2777+ infer(sh_alf_cr_enabled_flag, ph->ph_alf_cr_enabled_flag);
2778+ }
2779+ if (current->sh_alf_cb_enabled_flag ||
2780+ current->sh_alf_cr_enabled_flag)
2781+ ub(3, sh_alf_aps_id_chroma);
2782+ else
2783+ infer(sh_alf_aps_id_chroma, ph->ph_alf_aps_id_chroma);
2784+ if (sps->sps_ccalf_enabled_flag) {
2785+ flag(sh_alf_cc_cb_enabled_flag);
2786+ if (current->sh_alf_cc_cb_enabled_flag)
2787+ ub(3, sh_alf_cc_cb_aps_id);
2788+ else
2789+ infer(sh_alf_cc_cb_aps_id, ph->ph_alf_cc_cb_aps_id);
2790+ flag(sh_alf_cc_cr_enabled_flag);
2791+ if (current->sh_alf_cc_cr_enabled_flag)
2792+ ub(3, sh_alf_cc_cr_aps_id);
2793+ else
2794+ infer(sh_alf_cc_cr_aps_id, ph->ph_alf_cc_cr_aps_id);
2795+ } else {
2796+ infer(sh_alf_cc_cb_enabled_flag, ph->ph_alf_cc_cb_enabled_flag);
2797+ infer(sh_alf_cc_cr_enabled_flag, ph->ph_alf_cc_cr_enabled_flag);
2798+ infer(sh_alf_cc_cb_aps_id, ph->ph_alf_cc_cb_aps_id);
2799+ infer(sh_alf_cc_cr_aps_id, ph->ph_alf_cc_cr_aps_id);
2800+ }
2801+ }
2802+ } else {
2803+ infer(sh_alf_enabled_flag, 0);
2804+ }
2805+
2806+ if (ph->ph_lmcs_enabled_flag &&
2807+ !current->sh_picture_header_in_slice_header_flag)
2808+ flag(sh_lmcs_used_flag);
2809+ else
2810+ infer(sh_lmcs_used_flag, 0);
2811+
2812+ if (ph->ph_explicit_scaling_list_enabled_flag &&
2813+ !current->sh_picture_header_in_slice_header_flag)
2814+ flag(sh_explicit_scaling_list_used_flag);
2815+ else
2816+ infer(sh_explicit_scaling_list_used_flag, 0);
2817+
2818+ if (!pps->pps_rpl_info_in_ph_flag &&
2819+ ((nal_unit_type != VVC_IDR_W_RADL &&
2820+ nal_unit_type != VVC_IDR_N_LP) || sps->sps_idr_rpl_present_flag)) {
2821+ CHECK(FUNC(ref_pic_lists)
2822+ (ctx, rw, sps, pps, &current->sh_ref_pic_lists));
2823+ ref_pic_lists = &current->sh_ref_pic_lists;
2824+ } else {
2825+ ref_pic_lists = &h266->priv.ph->ph_ref_pic_lists;
2826+ }
2827+ if ((current->sh_slice_type != VVC_SLICE_TYPE_I &&
2828+ ref_pic_lists->rpl_ref_list[0].num_ref_entries > 1) ||
2829+ (current->sh_slice_type == VVC_SLICE_TYPE_B &&
2830+ ref_pic_lists->rpl_ref_list[1].num_ref_entries > 1)) {
2831+ flag(sh_num_ref_idx_active_override_flag);
2832+ if (current->sh_num_ref_idx_active_override_flag) {
2833+ for (i = 0;
2834+ i < (current->sh_slice_type == VVC_SLICE_TYPE_B ? 2 : 1); i++)
2835+ if (ref_pic_lists->rpl_ref_list[i].num_ref_entries > 1)
2836+ ues(sh_num_ref_idx_active_minus1[i], 0, 14, 1, i);
2837+ else
2838+ infer(sh_num_ref_idx_active_minus1[i], 0);
2839+ }
2840+ } else {
2841+ infer(sh_num_ref_idx_active_override_flag, 1);
2842+ }
2843+ if (current->sh_slice_type != VVC_SLICE_TYPE_I) {
2844+ if (pps->pps_cabac_init_present_flag)
2845+ flag(sh_cabac_init_flag);
2846+ else
2847+ infer(sh_cabac_init_flag, 0);
2848+ if (ph->ph_temporal_mvp_enabled_flag && !pps->pps_rpl_info_in_ph_flag) {
2849+ uint8_t num_ref_idx_active[2];
2850+ for (i = 0; i < 2; i++) {
2851+ if (current->sh_slice_type == VVC_SLICE_TYPE_B ||
2852+ (current->sh_slice_type == VVC_SLICE_TYPE_P && i == 0)) {
2853+ if (current->sh_num_ref_idx_active_override_flag) {
2854+ num_ref_idx_active[i] =
2855+ current->sh_num_ref_idx_active_minus1[i] + 1;
2856+ } else {
2857+ num_ref_idx_active[i] =
2858+ FFMIN(ref_pic_lists->rpl_ref_list[i].num_ref_entries,
2859+ pps->pps_num_ref_idx_default_active_minus1[i] + 1);
2860+ }
2861+ } else {
2862+ num_ref_idx_active[i] = 0;
2863+ }
2864+ }
2865+
2866+ if (current->sh_slice_type == VVC_SLICE_TYPE_B)
2867+ flag(sh_collocated_from_l0_flag);
2868+ else
2869+ infer(sh_collocated_from_l0_flag, 1);
2870+ if ((current->sh_collocated_from_l0_flag &&
2871+ num_ref_idx_active[0] > 1) ||
2872+ (!current->sh_collocated_from_l0_flag &&
2873+ num_ref_idx_active[1] > 1)) {
2874+ unsigned int idx = current->sh_collocated_from_l0_flag ? 0 : 1;
2875+ ue(sh_collocated_ref_idx, 0, num_ref_idx_active[idx] - 1);
2876+ } else {
2877+ infer(sh_collocated_ref_idx, 0);
2878+ }
2879+ if (!pps->pps_wp_info_in_ph_flag &&
2880+ ((pps->pps_weighted_pred_flag &&
2881+ current->sh_slice_type == VVC_SLICE_TYPE_P) ||
2882+ (pps->pps_weighted_bipred_flag &&
2883+ current->sh_slice_type == VVC_SLICE_TYPE_B))) {
2884+ CHECK(FUNC(pred_weight_table) (ctx, rw, sps, pps, ref_pic_lists,
2885+ num_ref_idx_active,
2886+ &current->sh_pred_weight_table));
2887+ }
2888+ }
2889+
2890+ }
2891+ qp_bd_offset = 6 * sps->sps_bitdepth_minus8;
2892+ if (!pps->pps_qp_delta_info_in_ph_flag)
2893+ se(sh_qp_delta, -qp_bd_offset - (26 + pps->pps_init_qp_minus26),
2894+ 63 - (26 + pps->pps_init_qp_minus26));
2895+ if (pps->pps_slice_chroma_qp_offsets_present_flag) {
2896+ int8_t off;
2897+
2898+ se(sh_cb_qp_offset, -12, 12);
2899+ off = pps->pps_cb_qp_offset + current->sh_cb_qp_offset;
2900+ if (off < -12 || off > 12) {
2901+ av_log(ctx->log_ctx, AV_LOG_ERROR,
2902+ "pps_cb_qp_offset + sh_cb_qp_offset (%d) not in range [-12, 12].\n",
2903+ off);
2904+ return AVERROR_INVALIDDATA;
2905+ }
2906+
2907+ se(sh_cr_qp_offset, -12, 12);
2908+ off = pps->pps_cr_qp_offset + current->sh_cr_qp_offset;
2909+ if (off < -12 || off > 12) {
2910+ av_log(ctx->log_ctx, AV_LOG_ERROR,
2911+ "pps_cr_qp_offset + sh_cr_qp_offset (%d) not in range [-12, 12].\n",
2912+ off);
2913+ return AVERROR_INVALIDDATA;
2914+ }
2915+
2916+ if (sps->sps_joint_cbcr_enabled_flag) {
2917+ se(sh_joint_cbcr_qp_offset, -12, 12);
2918+ off =
2919+ pps->pps_joint_cbcr_qp_offset_value +
2920+ current->sh_joint_cbcr_qp_offset;
2921+ if (off < -12 || off > 12) {
2922+ av_log(ctx->log_ctx, AV_LOG_ERROR,
2923+ "pps_joint_cbcr_qp_offset_value + sh_joint_cbcr_qp_offset (%d)"
2924+ "not in range [-12, 12]. \n", off);
2925+ return AVERROR_INVALIDDATA;
2926+ }
2927+ } else {
2928+ infer(sh_joint_cbcr_qp_offset, 0);
2929+ }
2930+ } else {
2931+ infer(sh_cb_qp_offset, 0);
2932+ infer(sh_cr_qp_offset, 0);
2933+ infer(sh_joint_cbcr_qp_offset, 0);
2934+ }
2935+ if (pps->pps_cu_chroma_qp_offset_list_enabled_flag)
2936+ flag(sh_cu_chroma_qp_offset_enabled_flag);
2937+ else
2938+ infer(sh_cu_chroma_qp_offset_enabled_flag, 0);
2939+ if (sps->sps_sao_enabled_flag && !pps->pps_sao_info_in_ph_flag) {
2940+ flag(sh_sao_luma_used_flag);
2941+ if (sps->sps_chroma_format_idc != 0)
2942+ flag(sh_sao_chroma_used_flag);
2943+ else
2944+ infer(sh_sao_chroma_used_flag, ph->ph_sao_chroma_enabled_flag);
2945+ } else {
2946+ infer(sh_sao_luma_used_flag, ph->ph_sao_luma_enabled_flag);
2947+ infer(sh_sao_chroma_used_flag, ph->ph_sao_chroma_enabled_flag);
2948+ }
2949+
2950+ if (pps->pps_deblocking_filter_override_enabled_flag &&
2951+ !pps->pps_dbf_info_in_ph_flag)
2952+ flag(sh_deblocking_params_present_flag);
2953+ else
2954+ infer(sh_deblocking_params_present_flag, 0);
2955+ if (current->sh_deblocking_params_present_flag) {
2956+ if (!pps->pps_deblocking_filter_disabled_flag)
2957+ flag(sh_deblocking_filter_disabled_flag);
2958+ else
2959+ infer(sh_deblocking_filter_disabled_flag,
2960+ !(pps->pps_deblocking_filter_disabled_flag &&
2961+ current->sh_deblocking_params_present_flag));
2962+ if (!current->sh_deblocking_filter_disabled_flag) {
2963+ se(sh_luma_beta_offset_div2, -12, 12);
2964+ se(sh_luma_tc_offset_div2, -12, 12);
2965+ if (pps->pps_chroma_tool_offsets_present_flag) {
2966+ se(sh_cb_beta_offset_div2, -12, 12);
2967+ se(sh_cb_tc_offset_div2, -12, 12);
2968+ se(sh_cr_beta_offset_div2, -12, 12);
2969+ se(sh_cr_tc_offset_div2, -12, 12);
2970+ } else {
2971+ infer(sh_cb_beta_offset_div2,
2972+ current->sh_luma_beta_offset_div2);
2973+ infer(sh_cb_tc_offset_div2, current->sh_luma_tc_offset_div2);
2974+ infer(sh_cr_beta_offset_div2,
2975+ current->sh_luma_beta_offset_div2);
2976+ infer(sh_cr_tc_offset_div2, current->sh_luma_tc_offset_div2);
2977+ }
2978+ } else {
2979+ infer(sh_luma_beta_offset_div2, ph->ph_luma_beta_offset_div2);
2980+ infer(sh_luma_tc_offset_div2, ph->ph_luma_tc_offset_div2);
2981+ if (pps->pps_chroma_tool_offsets_present_flag) {
2982+ infer(sh_cb_beta_offset_div2, ph->ph_cb_beta_offset_div2);
2983+ infer(sh_cb_tc_offset_div2, ph->ph_cb_tc_offset_div2);
2984+ infer(sh_cr_beta_offset_div2, ph->ph_cr_beta_offset_div2);
2985+ infer(sh_cr_tc_offset_div2, ph->ph_cr_beta_offset_div2);
2986+ } else {
2987+ infer(sh_cb_beta_offset_div2,
2988+ current->sh_luma_beta_offset_div2);
2989+ infer(sh_cb_tc_offset_div2, current->sh_luma_tc_offset_div2);
2990+ infer(sh_cr_beta_offset_div2,
2991+ current->sh_luma_beta_offset_div2);
2992+ infer(sh_cr_tc_offset_div2, current->sh_luma_tc_offset_div2);
2993+ }
2994+ }
2995+ }
2996+
2997+ if (sps->sps_dep_quant_enabled_flag)
2998+ flag(sh_dep_quant_used_flag);
2999+ else
3000+ infer(sh_dep_quant_used_flag, 0);
3001+
3002+ if (sps->sps_sign_data_hiding_enabled_flag &&
3003+ !current->sh_dep_quant_used_flag)
3004+ flag(sh_sign_data_hiding_used_flag);
3005+ else
3006+ infer(sh_sign_data_hiding_used_flag, 0);
3007+
3008+ if (sps->sps_transform_skip_enabled_flag &&
3009+ !current->sh_dep_quant_used_flag &&
3010+ !current->sh_sign_data_hiding_used_flag)
3011+ flag(sh_ts_residual_coding_disabled_flag);
3012+ else
3013+ infer(sh_ts_residual_coding_disabled_flag, 0);
3014+ if (pps->pps_slice_header_extension_present_flag) {
3015+ ue(sh_slice_header_extension_length, 0, 256);
3016+ for (i = 0; i < current->sh_slice_header_extension_length; i++)
3017+ us(8, sh_slice_header_extension_data_byte[i], 0x00, 0xff, 1, i);
3018+ }
3019+ if (sps->sps_entry_point_offsets_present_flag) {
3020+ int num_entry_points = 0;
3021+ uint8_t entropy_sync = sps->sps_entropy_coding_sync_enabled_flag;
3022+ int height;
3023+ if (pps->pps_rect_slice_flag) {
3024+ int width_in_tiles;
3025+ int slice_idx = current->sh_slice_address;
3026+ for (i = 0; i < curr_subpic_idx; i++) {
3027+ slice_idx += pps->num_slices_in_subpic[i];
3028+ }
3029+ width_in_tiles =
3030+ pps->pps_slice_width_in_tiles_minus1[slice_idx] + 1;
3031+
3032+ if (entropy_sync)
3033+ height = pps->slice_height_in_ctus[slice_idx];
3034+ else
3035+ height = pps->pps_slice_height_in_tiles_minus1[slice_idx] + 1;
3036+
3037+ num_entry_points = width_in_tiles * height;
3038+ } else {
3039+ int tile_idx;
3040+ int tile_y;
3041+ for (tile_idx = current->sh_slice_address;
3042+ tile_idx <=
3043+ current->sh_slice_address +
3044+ current->sh_num_tiles_in_slice_minus1; tile_idx++) {
3045+ tile_y = tile_idx / pps->num_tile_rows;
3046+ height = pps->pps_tile_row_height_minus1[tile_y] + 1;
3047+ num_entry_points += (entropy_sync ? height : 1);
3048+ }
3049+ }
3050+ num_entry_points--;
3051+ if (num_entry_points > VVC_MAX_ENTRY_POINTS) {
3052+ av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many entry points: "
3053+ "%" PRIu16 ".\n", num_entry_points);
3054+ return AVERROR_PATCHWELCOME;
3055+ }
3056+ if (num_entry_points > 0) {
3057+ ue(sh_entry_offset_len_minus1, 0, 31);
3058+ for (i = 0; i < num_entry_points; i++) {
3059+ ubs(current->sh_entry_offset_len_minus1 + 1,
3060+ sh_entry_point_offset_minus1[i], 1, i);
3061+ }
3062+ }
3063+ }
3064+ CHECK(FUNC(byte_alignment) (ctx, rw));
3065+
3066+ return 0;
3067+}
3068+
3069+static int FUNC(sei_decoded_picture_hash) (CodedBitstreamContext *ctx,
3070+ RWContext *rw,
3071+ H266RawSEIDecodedPictureHash *
3072+ current)
3073+{
3074+ int err, c_idx, i;
3075+
3076+ HEADER("Decoded Picture Hash");
3077+
3078+ u(8, dph_sei_hash_type, 0, 2);
3079+ flag(dph_sei_single_component_flag);
3080+ ub(7, dph_sei_reserved_zero_7bits);
3081+
3082+ for (c_idx = 0; c_idx < (current->dph_sei_single_component_flag ? 1 : 3);
3083+ c_idx++) {
3084+ if (current->dph_sei_hash_type == 0) {
3085+ for (i = 0; i < 16; i++)
3086+ us(8, dph_sei_picture_md5[c_idx][i], 0x00, 0xff, 2, c_idx, i);
3087+ } else if (current->dph_sei_hash_type == 1) {
3088+ us(16, dph_sei_picture_crc[c_idx], 0x0000, 0xffff, 1, c_idx);
3089+ } else if (current->dph_sei_hash_type == 2) {
3090+ us(32, dph_sei_picture_checksum[c_idx], 0x00000000, 0xffffffff, 1,
3091+ c_idx);
3092+ }
3093+ }
3094+ return 0;
3095+}
3096+
3097+static int FUNC(sei) (CodedBitstreamContext *ctx, RWContext *rw,
3098+ H266RawSEI *current, int prefix)
3099+{
3100+ int err;
3101+
3102+ if (prefix)
3103+ HEADER("Prefix Supplemental Enhancement Information");
3104+ else
3105+ HEADER("Suffix Supplemental Enhancement Information");
3106+
3107+ CHECK(FUNC(nal_unit_header) (ctx, rw, &current->nal_unit_header,
3108+ prefix ? VVC_PREFIX_SEI_NUT
3109+ : VVC_SUFFIX_SEI_NUT));
3110+
3111+ CHECK(FUNC_SEI(message_list) (ctx, rw, &current->message_list, prefix));
3112+
3113+ CHECK(FUNC(rbsp_trailing_bits) (ctx, rw));
3114+
3115+ return 0;
3116+}
libavcodec/cbs_internal.h+1−0
@@ -251,6 +251,7 @@ int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc, · 250 unmodified lines
251251 extern const CodedBitstreamType ff_cbs_type_av1;
252252 extern const CodedBitstreamType ff_cbs_type_h264;
253253 extern const CodedBitstreamType ff_cbs_type_h265;
254+extern const CodedBitstreamType ff_cbs_type_h266;
254255 extern const CodedBitstreamType ff_cbs_type_jpeg;
255256 extern const CodedBitstreamType ff_cbs_type_mpeg2;
256257 extern const CodedBitstreamType ff_cbs_type_vp9;
libavcodec/cbs_sei.c+29−0
@@ -20,6 +20,7 @@ · 19 unmodified lines
2020 #include "cbs_internal.h"
2121 #include "cbs_h264.h"
2222 #include "cbs_h265.h"
23+#include "cbs_h266.h"
2324 #include "cbs_sei.h"
2425
2526 static void cbs_free_user_data_registered(void *opaque, uint8_t *data)
@@ -132,6 +133,13 @@ static int cbs_sei_get_unit(CodedBitstreamContext *ctx, · 106 unmodified lines
132133 else
133134 sei_type = HEVC_NAL_SEI_SUFFIX;
134135 break;
136+ case AV_CODEC_ID_H266:
137+ highest_vcl_type = VVC_RSV_IRAP_11;
138+ if (prefix)
139+ sei_type = VVC_PREFIX_SEI_NUT;
140+ else
141+ sei_type = VVC_SUFFIX_SEI_NUT;
142+ break;
135143 default:
136144 return AVERROR(EINVAL);
137145 }
@@ -207,6 +215,18 @@ static int cbs_sei_get_unit(CodedBitstreamContext *ctx, · 69 unmodified lines
207215 memcpy(unit->content, &sei, sizeof(sei));
208216 }
209217 break;
218+ case AV_CODEC_ID_H266:
219+ {
220+ H266RawSEI sei = {
221+ .nal_unit_header = {
222+ .nal_unit_type = sei_type,
223+ .nuh_layer_id = 0,
224+ .nuh_temporal_id_plus1 = 1,
225+ },
226+ };
227+ memcpy(unit->content, &sei, sizeof(sei));
228+ }
229+ break;
210230 default:
211231 av_assert0(0);
212232 }
@@ -237,6 +257,15 @@ static int cbs_sei_get_message_list(CodedBitstreamContext *ctx, · 24 unmodified lines
237257 *list = &sei->message_list;
238258 }
239259 break;
260+ case AV_CODEC_ID_H266:
261+ {
262+ H266RawSEI *sei = unit->content;
263+ if (unit->type != VVC_PREFIX_SEI_NUT &&
264+ unit->type != VVC_SUFFIX_SEI_NUT)
265+ return AVERROR(EINVAL);
266+ *list = &sei->message_list;
267+ }
268+ break;
240269 default:
241270 return AVERROR(EINVAL);
242271 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t