cbs_h266_syntax_template.c 147 KB

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