hevcdec.c 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751
  1. /*
  2. * HEVC video Decoder
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. * Copyright (C) 2012 - 2013 Mickael Raulet
  6. * Copyright (C) 2012 - 2013 Gildas Cocherel
  7. * Copyright (C) 2012 - 2013 Wassim Hamidouche
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include "config_components.h"
  26. #include "libavutil/attributes.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/film_grain_params.h"
  30. #include "libavutil/internal.h"
  31. #include "libavutil/md5.h"
  32. #include "libavutil/mem.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/pixdesc.h"
  35. #include "libavutil/timecode.h"
  36. #include "aom_film_grain.h"
  37. #include "bswapdsp.h"
  38. #include "cabac_functions.h"
  39. #include "codec_internal.h"
  40. #include "decode.h"
  41. #include "golomb.h"
  42. #include "hevc.h"
  43. #include "hevc_parse.h"
  44. #include "hevcdec.h"
  45. #include "hwaccel_internal.h"
  46. #include "hwconfig.h"
  47. #include "internal.h"
  48. #include "profiles.h"
  49. #include "refstruct.h"
  50. #include "thread.h"
  51. #include "threadframe.h"
  52. static const uint8_t hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
  53. /**
  54. * NOTE: Each function hls_foo correspond to the function foo in the
  55. * specification (HLS stands for High Level Syntax).
  56. */
  57. /**
  58. * Section 5.7
  59. */
  60. /* free everything allocated by pic_arrays_init() */
  61. static void pic_arrays_free(HEVCContext *s)
  62. {
  63. av_freep(&s->sao);
  64. av_freep(&s->deblock);
  65. av_freep(&s->skip_flag);
  66. av_freep(&s->tab_ct_depth);
  67. av_freep(&s->tab_ipm);
  68. av_freep(&s->cbf_luma);
  69. av_freep(&s->is_pcm);
  70. av_freep(&s->qp_y_tab);
  71. av_freep(&s->tab_slice_address);
  72. av_freep(&s->filter_slice_edges);
  73. av_freep(&s->horizontal_bs);
  74. av_freep(&s->vertical_bs);
  75. av_freep(&s->sh.entry_point_offset);
  76. av_freep(&s->sh.size);
  77. av_freep(&s->sh.offset);
  78. ff_refstruct_pool_uninit(&s->tab_mvf_pool);
  79. ff_refstruct_pool_uninit(&s->rpl_tab_pool);
  80. }
  81. /* allocate arrays that depend on frame dimensions */
  82. static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
  83. {
  84. int log2_min_cb_size = sps->log2_min_cb_size;
  85. int width = sps->width;
  86. int height = sps->height;
  87. int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
  88. ((height >> log2_min_cb_size) + 1);
  89. int ctb_count = sps->ctb_width * sps->ctb_height;
  90. int min_pu_size = sps->min_pu_width * sps->min_pu_height;
  91. s->bs_width = (width >> 2) + 1;
  92. s->bs_height = (height >> 2) + 1;
  93. s->sao = av_calloc(ctb_count, sizeof(*s->sao));
  94. s->deblock = av_calloc(ctb_count, sizeof(*s->deblock));
  95. if (!s->sao || !s->deblock)
  96. goto fail;
  97. s->skip_flag = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
  98. s->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
  99. if (!s->skip_flag || !s->tab_ct_depth)
  100. goto fail;
  101. s->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
  102. s->tab_ipm = av_mallocz(min_pu_size);
  103. s->is_pcm = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
  104. if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
  105. goto fail;
  106. s->filter_slice_edges = av_mallocz(ctb_count);
  107. s->tab_slice_address = av_malloc_array(pic_size_in_ctb,
  108. sizeof(*s->tab_slice_address));
  109. s->qp_y_tab = av_malloc_array(pic_size_in_ctb,
  110. sizeof(*s->qp_y_tab));
  111. if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
  112. goto fail;
  113. s->horizontal_bs = av_calloc(s->bs_width, s->bs_height);
  114. s->vertical_bs = av_calloc(s->bs_width, s->bs_height);
  115. if (!s->horizontal_bs || !s->vertical_bs)
  116. goto fail;
  117. s->tab_mvf_pool = ff_refstruct_pool_alloc(min_pu_size * sizeof(MvField), 0);
  118. s->rpl_tab_pool = ff_refstruct_pool_alloc(ctb_count * sizeof(RefPicListTab), 0);
  119. if (!s->tab_mvf_pool || !s->rpl_tab_pool)
  120. goto fail;
  121. return 0;
  122. fail:
  123. pic_arrays_free(s);
  124. return AVERROR(ENOMEM);
  125. }
  126. static int pred_weight_table(HEVCContext *s, GetBitContext *gb)
  127. {
  128. int i = 0;
  129. int j = 0;
  130. uint8_t luma_weight_l0_flag[16];
  131. uint8_t chroma_weight_l0_flag[16];
  132. uint8_t luma_weight_l1_flag[16];
  133. uint8_t chroma_weight_l1_flag[16];
  134. int luma_log2_weight_denom;
  135. luma_log2_weight_denom = get_ue_golomb_long(gb);
  136. if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7) {
  137. av_log(s->avctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is invalid\n", luma_log2_weight_denom);
  138. return AVERROR_INVALIDDATA;
  139. }
  140. s->sh.luma_log2_weight_denom = av_clip_uintp2(luma_log2_weight_denom, 3);
  141. if (s->ps.sps->chroma_format_idc != 0) {
  142. int64_t chroma_log2_weight_denom = luma_log2_weight_denom + (int64_t)get_se_golomb(gb);
  143. if (chroma_log2_weight_denom < 0 || chroma_log2_weight_denom > 7) {
  144. av_log(s->avctx, AV_LOG_ERROR, "chroma_log2_weight_denom %"PRId64" is invalid\n", chroma_log2_weight_denom);
  145. return AVERROR_INVALIDDATA;
  146. }
  147. s->sh.chroma_log2_weight_denom = chroma_log2_weight_denom;
  148. }
  149. for (i = 0; i < s->sh.nb_refs[L0]; i++) {
  150. luma_weight_l0_flag[i] = get_bits1(gb);
  151. if (!luma_weight_l0_flag[i]) {
  152. s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
  153. s->sh.luma_offset_l0[i] = 0;
  154. }
  155. }
  156. if (s->ps.sps->chroma_format_idc != 0) {
  157. for (i = 0; i < s->sh.nb_refs[L0]; i++)
  158. chroma_weight_l0_flag[i] = get_bits1(gb);
  159. } else {
  160. for (i = 0; i < s->sh.nb_refs[L0]; i++)
  161. chroma_weight_l0_flag[i] = 0;
  162. }
  163. for (i = 0; i < s->sh.nb_refs[L0]; i++) {
  164. if (luma_weight_l0_flag[i]) {
  165. int delta_luma_weight_l0 = get_se_golomb(gb);
  166. if ((int8_t)delta_luma_weight_l0 != delta_luma_weight_l0)
  167. return AVERROR_INVALIDDATA;
  168. s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
  169. s->sh.luma_offset_l0[i] = get_se_golomb(gb);
  170. }
  171. if (chroma_weight_l0_flag[i]) {
  172. for (j = 0; j < 2; j++) {
  173. int delta_chroma_weight_l0 = get_se_golomb(gb);
  174. int delta_chroma_offset_l0 = get_se_golomb(gb);
  175. if ( (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
  176. || delta_chroma_offset_l0 < -(1<<17) || delta_chroma_offset_l0 > (1<<17)) {
  177. return AVERROR_INVALIDDATA;
  178. }
  179. s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
  180. s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
  181. >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
  182. }
  183. } else {
  184. s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
  185. s->sh.chroma_offset_l0[i][0] = 0;
  186. s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
  187. s->sh.chroma_offset_l0[i][1] = 0;
  188. }
  189. }
  190. if (s->sh.slice_type == HEVC_SLICE_B) {
  191. for (i = 0; i < s->sh.nb_refs[L1]; i++) {
  192. luma_weight_l1_flag[i] = get_bits1(gb);
  193. if (!luma_weight_l1_flag[i]) {
  194. s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
  195. s->sh.luma_offset_l1[i] = 0;
  196. }
  197. }
  198. if (s->ps.sps->chroma_format_idc != 0) {
  199. for (i = 0; i < s->sh.nb_refs[L1]; i++)
  200. chroma_weight_l1_flag[i] = get_bits1(gb);
  201. } else {
  202. for (i = 0; i < s->sh.nb_refs[L1]; i++)
  203. chroma_weight_l1_flag[i] = 0;
  204. }
  205. for (i = 0; i < s->sh.nb_refs[L1]; i++) {
  206. if (luma_weight_l1_flag[i]) {
  207. int delta_luma_weight_l1 = get_se_golomb(gb);
  208. if ((int8_t)delta_luma_weight_l1 != delta_luma_weight_l1)
  209. return AVERROR_INVALIDDATA;
  210. s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
  211. s->sh.luma_offset_l1[i] = get_se_golomb(gb);
  212. }
  213. if (chroma_weight_l1_flag[i]) {
  214. for (j = 0; j < 2; j++) {
  215. int delta_chroma_weight_l1 = get_se_golomb(gb);
  216. int delta_chroma_offset_l1 = get_se_golomb(gb);
  217. if ( (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
  218. || delta_chroma_offset_l1 < -(1<<17) || delta_chroma_offset_l1 > (1<<17)) {
  219. return AVERROR_INVALIDDATA;
  220. }
  221. s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
  222. s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
  223. >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
  224. }
  225. } else {
  226. s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
  227. s->sh.chroma_offset_l1[i][0] = 0;
  228. s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
  229. s->sh.chroma_offset_l1[i][1] = 0;
  230. }
  231. }
  232. }
  233. return 0;
  234. }
  235. static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
  236. {
  237. const HEVCSPS *sps = s->ps.sps;
  238. int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
  239. int prev_delta_msb = 0;
  240. unsigned int nb_sps = 0, nb_sh;
  241. int i;
  242. rps->nb_refs = 0;
  243. if (!sps->long_term_ref_pics_present_flag)
  244. return 0;
  245. if (sps->num_long_term_ref_pics_sps > 0)
  246. nb_sps = get_ue_golomb_long(gb);
  247. nb_sh = get_ue_golomb_long(gb);
  248. if (nb_sps > sps->num_long_term_ref_pics_sps)
  249. return AVERROR_INVALIDDATA;
  250. if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
  251. return AVERROR_INVALIDDATA;
  252. rps->nb_refs = nb_sh + nb_sps;
  253. for (i = 0; i < rps->nb_refs; i++) {
  254. if (i < nb_sps) {
  255. uint8_t lt_idx_sps = 0;
  256. if (sps->num_long_term_ref_pics_sps > 1)
  257. lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
  258. rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
  259. rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
  260. } else {
  261. rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
  262. rps->used[i] = get_bits1(gb);
  263. }
  264. rps->poc_msb_present[i] = get_bits1(gb);
  265. if (rps->poc_msb_present[i]) {
  266. int64_t delta = get_ue_golomb_long(gb);
  267. int64_t poc;
  268. if (i && i != nb_sps)
  269. delta += prev_delta_msb;
  270. poc = rps->poc[i] + s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
  271. if (poc != (int32_t)poc)
  272. return AVERROR_INVALIDDATA;
  273. rps->poc[i] = poc;
  274. prev_delta_msb = delta;
  275. }
  276. }
  277. return 0;
  278. }
  279. static void export_stream_params(HEVCContext *s, const HEVCSPS *sps)
  280. {
  281. AVCodecContext *avctx = s->avctx;
  282. const HEVCParamSets *ps = &s->ps;
  283. const HEVCVPS *vps = ps->vps_list[sps->vps_id];
  284. const HEVCWindow *ow = &sps->output_window;
  285. unsigned int num = 0, den = 0;
  286. avctx->pix_fmt = sps->pix_fmt;
  287. avctx->coded_width = sps->width;
  288. avctx->coded_height = sps->height;
  289. avctx->width = sps->width - ow->left_offset - ow->right_offset;
  290. avctx->height = sps->height - ow->top_offset - ow->bottom_offset;
  291. avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
  292. avctx->profile = sps->ptl.general_ptl.profile_idc;
  293. avctx->level = sps->ptl.general_ptl.level_idc;
  294. ff_set_sar(avctx, sps->vui.common.sar);
  295. if (sps->vui.common.video_signal_type_present_flag)
  296. avctx->color_range = sps->vui.common.video_full_range_flag ? AVCOL_RANGE_JPEG
  297. : AVCOL_RANGE_MPEG;
  298. else
  299. avctx->color_range = AVCOL_RANGE_MPEG;
  300. if (sps->vui.common.colour_description_present_flag) {
  301. avctx->color_primaries = sps->vui.common.colour_primaries;
  302. avctx->color_trc = sps->vui.common.transfer_characteristics;
  303. avctx->colorspace = sps->vui.common.matrix_coeffs;
  304. } else {
  305. avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
  306. avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
  307. avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
  308. }
  309. avctx->chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
  310. if (sps->chroma_format_idc == 1) {
  311. if (sps->vui.common.chroma_loc_info_present_flag) {
  312. if (sps->vui.common.chroma_sample_loc_type_top_field <= 5)
  313. avctx->chroma_sample_location = sps->vui.common.chroma_sample_loc_type_top_field + 1;
  314. } else
  315. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  316. }
  317. if (vps->vps_timing_info_present_flag) {
  318. num = vps->vps_num_units_in_tick;
  319. den = vps->vps_time_scale;
  320. } else if (sps->vui.vui_timing_info_present_flag) {
  321. num = sps->vui.vui_num_units_in_tick;
  322. den = sps->vui.vui_time_scale;
  323. }
  324. if (num != 0 && den != 0)
  325. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  326. num, den, 1 << 30);
  327. }
  328. static int export_stream_params_from_sei(HEVCContext *s)
  329. {
  330. AVCodecContext *avctx = s->avctx;
  331. if (s->sei.common.a53_caption.buf_ref)
  332. s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
  333. if (s->sei.common.alternative_transfer.present &&
  334. av_color_transfer_name(s->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
  335. s->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
  336. avctx->color_trc = s->sei.common.alternative_transfer.preferred_transfer_characteristics;
  337. }
  338. if (s->sei.common.film_grain_characteristics.present ||
  339. s->sei.common.aom_film_grain.enable)
  340. avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
  341. return 0;
  342. }
  343. static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
  344. {
  345. #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
  346. CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
  347. CONFIG_HEVC_D3D12VA_HWACCEL + \
  348. CONFIG_HEVC_NVDEC_HWACCEL + \
  349. CONFIG_HEVC_VAAPI_HWACCEL + \
  350. CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
  351. CONFIG_HEVC_VDPAU_HWACCEL + \
  352. CONFIG_HEVC_VULKAN_HWACCEL)
  353. enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
  354. switch (sps->pix_fmt) {
  355. case AV_PIX_FMT_YUV420P:
  356. case AV_PIX_FMT_YUVJ420P:
  357. #if CONFIG_HEVC_DXVA2_HWACCEL
  358. *fmt++ = AV_PIX_FMT_DXVA2_VLD;
  359. #endif
  360. #if CONFIG_HEVC_D3D11VA_HWACCEL
  361. *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
  362. *fmt++ = AV_PIX_FMT_D3D11;
  363. #endif
  364. #if CONFIG_HEVC_D3D12VA_HWACCEL
  365. *fmt++ = AV_PIX_FMT_D3D12;
  366. #endif
  367. #if CONFIG_HEVC_VAAPI_HWACCEL
  368. *fmt++ = AV_PIX_FMT_VAAPI;
  369. #endif
  370. #if CONFIG_HEVC_VDPAU_HWACCEL
  371. *fmt++ = AV_PIX_FMT_VDPAU;
  372. #endif
  373. #if CONFIG_HEVC_NVDEC_HWACCEL
  374. *fmt++ = AV_PIX_FMT_CUDA;
  375. #endif
  376. #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
  377. *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
  378. #endif
  379. #if CONFIG_HEVC_VULKAN_HWACCEL
  380. *fmt++ = AV_PIX_FMT_VULKAN;
  381. #endif
  382. break;
  383. case AV_PIX_FMT_YUV420P10:
  384. #if CONFIG_HEVC_DXVA2_HWACCEL
  385. *fmt++ = AV_PIX_FMT_DXVA2_VLD;
  386. #endif
  387. #if CONFIG_HEVC_D3D11VA_HWACCEL
  388. *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
  389. *fmt++ = AV_PIX_FMT_D3D11;
  390. #endif
  391. #if CONFIG_HEVC_D3D12VA_HWACCEL
  392. *fmt++ = AV_PIX_FMT_D3D12;
  393. #endif
  394. #if CONFIG_HEVC_VAAPI_HWACCEL
  395. *fmt++ = AV_PIX_FMT_VAAPI;
  396. #endif
  397. #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
  398. *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
  399. #endif
  400. #if CONFIG_HEVC_VULKAN_HWACCEL
  401. *fmt++ = AV_PIX_FMT_VULKAN;
  402. #endif
  403. #if CONFIG_HEVC_VDPAU_HWACCEL
  404. *fmt++ = AV_PIX_FMT_VDPAU;
  405. #endif
  406. #if CONFIG_HEVC_NVDEC_HWACCEL
  407. *fmt++ = AV_PIX_FMT_CUDA;
  408. #endif
  409. break;
  410. case AV_PIX_FMT_YUV444P:
  411. #if CONFIG_HEVC_VAAPI_HWACCEL
  412. *fmt++ = AV_PIX_FMT_VAAPI;
  413. #endif
  414. #if CONFIG_HEVC_VDPAU_HWACCEL
  415. *fmt++ = AV_PIX_FMT_VDPAU;
  416. #endif
  417. #if CONFIG_HEVC_NVDEC_HWACCEL
  418. *fmt++ = AV_PIX_FMT_CUDA;
  419. #endif
  420. #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
  421. *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
  422. #endif
  423. #if CONFIG_HEVC_VULKAN_HWACCEL
  424. *fmt++ = AV_PIX_FMT_VULKAN;
  425. #endif
  426. break;
  427. case AV_PIX_FMT_YUV422P:
  428. case AV_PIX_FMT_YUV422P10LE:
  429. #if CONFIG_HEVC_VAAPI_HWACCEL
  430. *fmt++ = AV_PIX_FMT_VAAPI;
  431. #endif
  432. #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
  433. *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
  434. #endif
  435. #if CONFIG_HEVC_VULKAN_HWACCEL
  436. *fmt++ = AV_PIX_FMT_VULKAN;
  437. #endif
  438. break;
  439. case AV_PIX_FMT_YUV444P10:
  440. #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
  441. *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
  442. #endif
  443. /* NOTE: fallthrough */
  444. case AV_PIX_FMT_YUV420P12:
  445. case AV_PIX_FMT_YUV444P12:
  446. #if CONFIG_HEVC_VAAPI_HWACCEL
  447. *fmt++ = AV_PIX_FMT_VAAPI;
  448. #endif
  449. #if CONFIG_HEVC_VDPAU_HWACCEL
  450. *fmt++ = AV_PIX_FMT_VDPAU;
  451. #endif
  452. #if CONFIG_HEVC_VULKAN_HWACCEL
  453. *fmt++ = AV_PIX_FMT_VULKAN;
  454. #endif
  455. #if CONFIG_HEVC_NVDEC_HWACCEL
  456. *fmt++ = AV_PIX_FMT_CUDA;
  457. #endif
  458. break;
  459. case AV_PIX_FMT_YUV422P12:
  460. #if CONFIG_HEVC_VAAPI_HWACCEL
  461. *fmt++ = AV_PIX_FMT_VAAPI;
  462. #endif
  463. #if CONFIG_HEVC_VULKAN_HWACCEL
  464. *fmt++ = AV_PIX_FMT_VULKAN;
  465. #endif
  466. break;
  467. }
  468. *fmt++ = sps->pix_fmt;
  469. *fmt = AV_PIX_FMT_NONE;
  470. return ff_get_format(s->avctx, pix_fmts);
  471. }
  472. static int set_sps(HEVCContext *s, const HEVCSPS *sps,
  473. enum AVPixelFormat pix_fmt)
  474. {
  475. int ret, i;
  476. pic_arrays_free(s);
  477. s->ps.sps = NULL;
  478. s->ps.vps = NULL;
  479. if (!sps)
  480. return 0;
  481. ret = pic_arrays_init(s, sps);
  482. if (ret < 0)
  483. goto fail;
  484. export_stream_params(s, sps);
  485. s->avctx->pix_fmt = pix_fmt;
  486. ff_hevc_pred_init(&s->hpc, sps->bit_depth);
  487. ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
  488. ff_videodsp_init (&s->vdsp, sps->bit_depth);
  489. for (i = 0; i < 3; i++) {
  490. av_freep(&s->sao_pixel_buffer_h[i]);
  491. av_freep(&s->sao_pixel_buffer_v[i]);
  492. }
  493. if (sps->sao_enabled && !s->avctx->hwaccel) {
  494. int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
  495. int c_idx;
  496. for(c_idx = 0; c_idx < c_count; c_idx++) {
  497. int w = sps->width >> sps->hshift[c_idx];
  498. int h = sps->height >> sps->vshift[c_idx];
  499. s->sao_pixel_buffer_h[c_idx] =
  500. av_malloc((w * 2 * sps->ctb_height) <<
  501. sps->pixel_shift);
  502. s->sao_pixel_buffer_v[c_idx] =
  503. av_malloc((h * 2 * sps->ctb_width) <<
  504. sps->pixel_shift);
  505. if (!s->sao_pixel_buffer_h[c_idx] ||
  506. !s->sao_pixel_buffer_v[c_idx])
  507. goto fail;
  508. }
  509. }
  510. s->ps.sps = sps;
  511. s->ps.vps = s->ps.vps_list[s->ps.sps->vps_id];
  512. return 0;
  513. fail:
  514. pic_arrays_free(s);
  515. for (i = 0; i < 3; i++) {
  516. av_freep(&s->sao_pixel_buffer_h[i]);
  517. av_freep(&s->sao_pixel_buffer_v[i]);
  518. }
  519. s->ps.sps = NULL;
  520. return ret;
  521. }
  522. static int hls_slice_header(HEVCContext *s)
  523. {
  524. GetBitContext *gb = &s->HEVClc->gb;
  525. SliceHeader *sh = &s->sh;
  526. int i, ret;
  527. // Coded parameters
  528. sh->first_slice_in_pic_flag = get_bits1(gb);
  529. if (s->ref && sh->first_slice_in_pic_flag) {
  530. av_log(s->avctx, AV_LOG_ERROR, "Two slices reporting being the first in the same frame.\n");
  531. return 1; // This slice will be skipped later, do not corrupt state
  532. }
  533. if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
  534. s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
  535. s->max_ra = INT_MAX;
  536. if (IS_IDR(s))
  537. ff_hevc_clear_refs(s);
  538. }
  539. sh->no_output_of_prior_pics_flag = 0;
  540. if (IS_IRAP(s))
  541. sh->no_output_of_prior_pics_flag = get_bits1(gb);
  542. sh->pps_id = get_ue_golomb_long(gb);
  543. if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
  544. av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
  545. return AVERROR_INVALIDDATA;
  546. }
  547. if (!sh->first_slice_in_pic_flag &&
  548. s->ps.pps != s->ps.pps_list[sh->pps_id]) {
  549. av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
  550. return AVERROR_INVALIDDATA;
  551. }
  552. s->ps.pps = s->ps.pps_list[sh->pps_id];
  553. if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
  554. sh->no_output_of_prior_pics_flag = 1;
  555. if (s->ps.sps != s->ps.sps_list[s->ps.pps->sps_id]) {
  556. const HEVCSPS *sps = s->ps.sps_list[s->ps.pps->sps_id];
  557. enum AVPixelFormat pix_fmt;
  558. ff_hevc_clear_refs(s);
  559. ret = set_sps(s, sps, sps->pix_fmt);
  560. if (ret < 0)
  561. return ret;
  562. pix_fmt = get_format(s, sps);
  563. if (pix_fmt < 0)
  564. return pix_fmt;
  565. s->avctx->pix_fmt = pix_fmt;
  566. s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
  567. s->max_ra = INT_MAX;
  568. }
  569. ret = export_stream_params_from_sei(s);
  570. if (ret < 0)
  571. return ret;
  572. sh->dependent_slice_segment_flag = 0;
  573. if (!sh->first_slice_in_pic_flag) {
  574. int slice_address_length;
  575. if (s->ps.pps->dependent_slice_segments_enabled_flag)
  576. sh->dependent_slice_segment_flag = get_bits1(gb);
  577. slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
  578. s->ps.sps->ctb_height);
  579. sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
  580. if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
  581. av_log(s->avctx, AV_LOG_ERROR,
  582. "Invalid slice segment address: %u.\n",
  583. sh->slice_segment_addr);
  584. return AVERROR_INVALIDDATA;
  585. }
  586. if (!sh->dependent_slice_segment_flag) {
  587. sh->slice_addr = sh->slice_segment_addr;
  588. s->slice_idx++;
  589. }
  590. } else {
  591. sh->slice_segment_addr = sh->slice_addr = 0;
  592. s->slice_idx = 0;
  593. s->slice_initialized = 0;
  594. }
  595. if (!sh->dependent_slice_segment_flag) {
  596. s->slice_initialized = 0;
  597. for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
  598. skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
  599. sh->slice_type = get_ue_golomb_long(gb);
  600. if (!(sh->slice_type == HEVC_SLICE_I ||
  601. sh->slice_type == HEVC_SLICE_P ||
  602. sh->slice_type == HEVC_SLICE_B)) {
  603. av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
  604. sh->slice_type);
  605. return AVERROR_INVALIDDATA;
  606. }
  607. if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I &&
  608. !s->ps.pps->pps_curr_pic_ref_enabled_flag) {
  609. av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
  610. return AVERROR_INVALIDDATA;
  611. }
  612. // when flag is not present, picture is inferred to be output
  613. sh->pic_output_flag = 1;
  614. if (s->ps.pps->output_flag_present_flag)
  615. sh->pic_output_flag = get_bits1(gb);
  616. if (s->ps.sps->separate_colour_plane_flag)
  617. sh->colour_plane_id = get_bits(gb, 2);
  618. if (!IS_IDR(s)) {
  619. int poc, pos;
  620. sh->pic_order_cnt_lsb = get_bits(gb, s->ps.sps->log2_max_poc_lsb);
  621. poc = ff_hevc_compute_poc(s->ps.sps, s->pocTid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
  622. if (!sh->first_slice_in_pic_flag && poc != s->poc) {
  623. av_log(s->avctx, AV_LOG_WARNING,
  624. "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
  625. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  626. return AVERROR_INVALIDDATA;
  627. poc = s->poc;
  628. }
  629. s->poc = poc;
  630. sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
  631. pos = get_bits_left(gb);
  632. if (!sh->short_term_ref_pic_set_sps_flag) {
  633. ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
  634. if (ret < 0)
  635. return ret;
  636. sh->short_term_rps = &sh->slice_rps;
  637. } else {
  638. int numbits, rps_idx;
  639. if (!s->ps.sps->nb_st_rps) {
  640. av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
  641. return AVERROR_INVALIDDATA;
  642. }
  643. numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
  644. rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
  645. sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
  646. }
  647. sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
  648. pos = get_bits_left(gb);
  649. ret = decode_lt_rps(s, &sh->long_term_rps, gb);
  650. if (ret < 0) {
  651. av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
  652. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  653. return AVERROR_INVALIDDATA;
  654. }
  655. sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
  656. if (s->ps.sps->sps_temporal_mvp_enabled_flag)
  657. sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
  658. else
  659. sh->slice_temporal_mvp_enabled_flag = 0;
  660. } else {
  661. s->poc = 0;
  662. sh->pic_order_cnt_lsb = 0;
  663. sh->short_term_ref_pic_set_sps_flag = 0;
  664. sh->short_term_ref_pic_set_size = 0;
  665. sh->short_term_rps = NULL;
  666. sh->long_term_ref_pic_set_size = 0;
  667. sh->slice_temporal_mvp_enabled_flag = 0;
  668. }
  669. /* 8.3.1 */
  670. if (sh->first_slice_in_pic_flag && s->temporal_id == 0 &&
  671. s->nal_unit_type != HEVC_NAL_TRAIL_N &&
  672. s->nal_unit_type != HEVC_NAL_TSA_N &&
  673. s->nal_unit_type != HEVC_NAL_STSA_N &&
  674. s->nal_unit_type != HEVC_NAL_RADL_N &&
  675. s->nal_unit_type != HEVC_NAL_RADL_R &&
  676. s->nal_unit_type != HEVC_NAL_RASL_N &&
  677. s->nal_unit_type != HEVC_NAL_RASL_R)
  678. s->pocTid0 = s->poc;
  679. if (s->ps.sps->sao_enabled) {
  680. sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
  681. if (s->ps.sps->chroma_format_idc) {
  682. sh->slice_sample_adaptive_offset_flag[1] =
  683. sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
  684. }
  685. } else {
  686. sh->slice_sample_adaptive_offset_flag[0] = 0;
  687. sh->slice_sample_adaptive_offset_flag[1] = 0;
  688. sh->slice_sample_adaptive_offset_flag[2] = 0;
  689. }
  690. sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
  691. if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
  692. int nb_refs;
  693. sh->nb_refs[L0] = s->ps.pps->num_ref_idx_l0_default_active;
  694. if (sh->slice_type == HEVC_SLICE_B)
  695. sh->nb_refs[L1] = s->ps.pps->num_ref_idx_l1_default_active;
  696. if (get_bits1(gb)) { // num_ref_idx_active_override_flag
  697. sh->nb_refs[L0] = get_ue_golomb_31(gb) + 1;
  698. if (sh->slice_type == HEVC_SLICE_B)
  699. sh->nb_refs[L1] = get_ue_golomb_31(gb) + 1;
  700. }
  701. if (sh->nb_refs[L0] >= HEVC_MAX_REFS || sh->nb_refs[L1] >= HEVC_MAX_REFS) {
  702. av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
  703. sh->nb_refs[L0], sh->nb_refs[L1]);
  704. return AVERROR_INVALIDDATA;
  705. }
  706. sh->rpl_modification_flag[0] = 0;
  707. sh->rpl_modification_flag[1] = 0;
  708. nb_refs = ff_hevc_frame_nb_refs(s);
  709. if (!nb_refs) {
  710. av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
  711. return AVERROR_INVALIDDATA;
  712. }
  713. if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
  714. sh->rpl_modification_flag[0] = get_bits1(gb);
  715. if (sh->rpl_modification_flag[0]) {
  716. for (i = 0; i < sh->nb_refs[L0]; i++)
  717. sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
  718. }
  719. if (sh->slice_type == HEVC_SLICE_B) {
  720. sh->rpl_modification_flag[1] = get_bits1(gb);
  721. if (sh->rpl_modification_flag[1] == 1)
  722. for (i = 0; i < sh->nb_refs[L1]; i++)
  723. sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
  724. }
  725. }
  726. if (sh->slice_type == HEVC_SLICE_B)
  727. sh->mvd_l1_zero_flag = get_bits1(gb);
  728. if (s->ps.pps->cabac_init_present_flag)
  729. sh->cabac_init_flag = get_bits1(gb);
  730. else
  731. sh->cabac_init_flag = 0;
  732. sh->collocated_ref_idx = 0;
  733. if (sh->slice_temporal_mvp_enabled_flag) {
  734. sh->collocated_list = L0;
  735. if (sh->slice_type == HEVC_SLICE_B)
  736. sh->collocated_list = !get_bits1(gb);
  737. if (sh->nb_refs[sh->collocated_list] > 1) {
  738. sh->collocated_ref_idx = get_ue_golomb_long(gb);
  739. if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
  740. av_log(s->avctx, AV_LOG_ERROR,
  741. "Invalid collocated_ref_idx: %d.\n",
  742. sh->collocated_ref_idx);
  743. return AVERROR_INVALIDDATA;
  744. }
  745. }
  746. }
  747. if ((s->ps.pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
  748. (s->ps.pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
  749. int ret = pred_weight_table(s, gb);
  750. if (ret < 0)
  751. return ret;
  752. }
  753. sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
  754. if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
  755. av_log(s->avctx, AV_LOG_ERROR,
  756. "Invalid number of merging MVP candidates: %d.\n",
  757. sh->max_num_merge_cand);
  758. return AVERROR_INVALIDDATA;
  759. }
  760. // Syntax in 7.3.6.1
  761. if (s->ps.sps->motion_vector_resolution_control_idc == 2)
  762. sh->use_integer_mv_flag = get_bits1(gb);
  763. else
  764. // Inferred to be equal to motion_vector_resolution_control_idc if not present
  765. sh->use_integer_mv_flag = s->ps.sps->motion_vector_resolution_control_idc;
  766. }
  767. sh->slice_qp_delta = get_se_golomb(gb);
  768. if (s->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag) {
  769. sh->slice_cb_qp_offset = get_se_golomb(gb);
  770. sh->slice_cr_qp_offset = get_se_golomb(gb);
  771. if (sh->slice_cb_qp_offset < -12 || sh->slice_cb_qp_offset > 12 ||
  772. sh->slice_cr_qp_offset < -12 || sh->slice_cr_qp_offset > 12) {
  773. av_log(s->avctx, AV_LOG_ERROR, "Invalid slice cx qp offset.\n");
  774. return AVERROR_INVALIDDATA;
  775. }
  776. } else {
  777. sh->slice_cb_qp_offset = 0;
  778. sh->slice_cr_qp_offset = 0;
  779. }
  780. if (s->ps.pps->pps_slice_act_qp_offsets_present_flag) {
  781. sh->slice_act_y_qp_offset = get_se_golomb(gb);
  782. sh->slice_act_cb_qp_offset = get_se_golomb(gb);
  783. sh->slice_act_cr_qp_offset = get_se_golomb(gb);
  784. }
  785. if (s->ps.pps->chroma_qp_offset_list_enabled_flag)
  786. sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
  787. else
  788. sh->cu_chroma_qp_offset_enabled_flag = 0;
  789. if (s->ps.pps->deblocking_filter_control_present_flag) {
  790. int deblocking_filter_override_flag = 0;
  791. if (s->ps.pps->deblocking_filter_override_enabled_flag)
  792. deblocking_filter_override_flag = get_bits1(gb);
  793. if (deblocking_filter_override_flag) {
  794. sh->disable_deblocking_filter_flag = get_bits1(gb);
  795. if (!sh->disable_deblocking_filter_flag) {
  796. int beta_offset_div2 = get_se_golomb(gb);
  797. int tc_offset_div2 = get_se_golomb(gb) ;
  798. if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
  799. tc_offset_div2 < -6 || tc_offset_div2 > 6) {
  800. av_log(s->avctx, AV_LOG_ERROR,
  801. "Invalid deblock filter offsets: %d, %d\n",
  802. beta_offset_div2, tc_offset_div2);
  803. return AVERROR_INVALIDDATA;
  804. }
  805. sh->beta_offset = beta_offset_div2 * 2;
  806. sh->tc_offset = tc_offset_div2 * 2;
  807. }
  808. } else {
  809. sh->disable_deblocking_filter_flag = s->ps.pps->disable_dbf;
  810. sh->beta_offset = s->ps.pps->beta_offset;
  811. sh->tc_offset = s->ps.pps->tc_offset;
  812. }
  813. } else {
  814. sh->disable_deblocking_filter_flag = 0;
  815. sh->beta_offset = 0;
  816. sh->tc_offset = 0;
  817. }
  818. if (s->ps.pps->seq_loop_filter_across_slices_enabled_flag &&
  819. (sh->slice_sample_adaptive_offset_flag[0] ||
  820. sh->slice_sample_adaptive_offset_flag[1] ||
  821. !sh->disable_deblocking_filter_flag)) {
  822. sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
  823. } else {
  824. sh->slice_loop_filter_across_slices_enabled_flag = s->ps.pps->seq_loop_filter_across_slices_enabled_flag;
  825. }
  826. } else if (!s->slice_initialized) {
  827. av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
  828. return AVERROR_INVALIDDATA;
  829. }
  830. sh->num_entry_point_offsets = 0;
  831. if (s->ps.pps->tiles_enabled_flag || s->ps.pps->entropy_coding_sync_enabled_flag) {
  832. unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
  833. // It would be possible to bound this tighter but this here is simpler
  834. if (num_entry_point_offsets > get_bits_left(gb)) {
  835. av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
  836. return AVERROR_INVALIDDATA;
  837. }
  838. sh->num_entry_point_offsets = num_entry_point_offsets;
  839. if (sh->num_entry_point_offsets > 0) {
  840. int offset_len = get_ue_golomb_long(gb) + 1;
  841. if (offset_len < 1 || offset_len > 32) {
  842. sh->num_entry_point_offsets = 0;
  843. av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
  844. return AVERROR_INVALIDDATA;
  845. }
  846. av_freep(&sh->entry_point_offset);
  847. av_freep(&sh->offset);
  848. av_freep(&sh->size);
  849. sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
  850. sh->offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
  851. sh->size = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
  852. if (!sh->entry_point_offset || !sh->offset || !sh->size) {
  853. sh->num_entry_point_offsets = 0;
  854. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
  855. return AVERROR(ENOMEM);
  856. }
  857. for (i = 0; i < sh->num_entry_point_offsets; i++) {
  858. unsigned val = get_bits_long(gb, offset_len);
  859. sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
  860. }
  861. if (s->threads_number > 1 && (s->ps.pps->num_tile_rows > 1 || s->ps.pps->num_tile_columns > 1)) {
  862. s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
  863. s->threads_number = 1;
  864. } else
  865. s->enable_parallel_tiles = 0;
  866. } else
  867. s->enable_parallel_tiles = 0;
  868. }
  869. if (s->ps.pps->slice_header_extension_present_flag) {
  870. unsigned int length = get_ue_golomb_long(gb);
  871. if (length*8LL > get_bits_left(gb)) {
  872. av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
  873. return AVERROR_INVALIDDATA;
  874. }
  875. for (i = 0; i < length; i++)
  876. skip_bits(gb, 8); // slice_header_extension_data_byte
  877. }
  878. // Inferred parameters
  879. sh->slice_qp = 26U + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
  880. if (sh->slice_qp > 51 ||
  881. sh->slice_qp < -s->ps.sps->qp_bd_offset) {
  882. av_log(s->avctx, AV_LOG_ERROR,
  883. "The slice_qp %d is outside the valid range "
  884. "[%d, 51].\n",
  885. sh->slice_qp,
  886. -s->ps.sps->qp_bd_offset);
  887. return AVERROR_INVALIDDATA;
  888. }
  889. sh->slice_ctb_addr_rs = sh->slice_segment_addr;
  890. if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
  891. av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
  892. return AVERROR_INVALIDDATA;
  893. }
  894. if (get_bits_left(gb) < 0) {
  895. av_log(s->avctx, AV_LOG_ERROR,
  896. "Overread slice header by %d bits\n", -get_bits_left(gb));
  897. return AVERROR_INVALIDDATA;
  898. }
  899. s->HEVClc->first_qp_group = !s->sh.dependent_slice_segment_flag;
  900. if (!s->ps.pps->cu_qp_delta_enabled_flag)
  901. s->HEVClc->qp_y = s->sh.slice_qp;
  902. s->slice_initialized = 1;
  903. s->HEVClc->tu.cu_qp_offset_cb = 0;
  904. s->HEVClc->tu.cu_qp_offset_cr = 0;
  905. return 0;
  906. }
  907. #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
  908. #define SET_SAO(elem, value) \
  909. do { \
  910. if (!sao_merge_up_flag && !sao_merge_left_flag) \
  911. sao->elem = value; \
  912. else if (sao_merge_left_flag) \
  913. sao->elem = CTB(s->sao, rx-1, ry).elem; \
  914. else if (sao_merge_up_flag) \
  915. sao->elem = CTB(s->sao, rx, ry-1).elem; \
  916. else \
  917. sao->elem = 0; \
  918. } while (0)
  919. static void hls_sao_param(HEVCLocalContext *lc, int rx, int ry)
  920. {
  921. const HEVCContext *const s = lc->parent;
  922. int sao_merge_left_flag = 0;
  923. int sao_merge_up_flag = 0;
  924. SAOParams *sao = &CTB(s->sao, rx, ry);
  925. int c_idx, i;
  926. if (s->sh.slice_sample_adaptive_offset_flag[0] ||
  927. s->sh.slice_sample_adaptive_offset_flag[1]) {
  928. if (rx > 0) {
  929. if (lc->ctb_left_flag)
  930. sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(lc);
  931. }
  932. if (ry > 0 && !sao_merge_left_flag) {
  933. if (lc->ctb_up_flag)
  934. sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(lc);
  935. }
  936. }
  937. for (c_idx = 0; c_idx < (s->ps.sps->chroma_format_idc ? 3 : 1); c_idx++) {
  938. int log2_sao_offset_scale = c_idx == 0 ? s->ps.pps->log2_sao_offset_scale_luma :
  939. s->ps.pps->log2_sao_offset_scale_chroma;
  940. if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
  941. sao->type_idx[c_idx] = SAO_NOT_APPLIED;
  942. continue;
  943. }
  944. if (c_idx == 2) {
  945. sao->type_idx[2] = sao->type_idx[1];
  946. sao->eo_class[2] = sao->eo_class[1];
  947. } else {
  948. SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(lc));
  949. }
  950. if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
  951. continue;
  952. for (i = 0; i < 4; i++)
  953. SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(lc));
  954. if (sao->type_idx[c_idx] == SAO_BAND) {
  955. for (i = 0; i < 4; i++) {
  956. if (sao->offset_abs[c_idx][i]) {
  957. SET_SAO(offset_sign[c_idx][i],
  958. ff_hevc_sao_offset_sign_decode(lc));
  959. } else {
  960. sao->offset_sign[c_idx][i] = 0;
  961. }
  962. }
  963. SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(lc));
  964. } else if (c_idx != 2) {
  965. SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(lc));
  966. }
  967. // Inferred parameters
  968. sao->offset_val[c_idx][0] = 0;
  969. for (i = 0; i < 4; i++) {
  970. sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
  971. if (sao->type_idx[c_idx] == SAO_EDGE) {
  972. if (i > 1)
  973. sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
  974. } else if (sao->offset_sign[c_idx][i]) {
  975. sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
  976. }
  977. sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
  978. }
  979. }
  980. }
  981. #undef SET_SAO
  982. #undef CTB
  983. static int hls_cross_component_pred(HEVCLocalContext *lc, int idx)
  984. {
  985. int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(lc, idx);
  986. if (log2_res_scale_abs_plus1 != 0) {
  987. int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(lc, idx);
  988. lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
  989. (1 - 2 * res_scale_sign_flag);
  990. } else {
  991. lc->tu.res_scale_val = 0;
  992. }
  993. return 0;
  994. }
  995. static int hls_transform_unit(HEVCLocalContext *lc, int x0, int y0,
  996. int xBase, int yBase, int cb_xBase, int cb_yBase,
  997. int log2_cb_size, int log2_trafo_size,
  998. int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
  999. {
  1000. const HEVCContext *const s = lc->parent;
  1001. const int log2_trafo_size_c = log2_trafo_size - s->ps.sps->hshift[1];
  1002. int i;
  1003. if (lc->cu.pred_mode == MODE_INTRA) {
  1004. int trafo_size = 1 << log2_trafo_size;
  1005. ff_hevc_set_neighbour_available(lc, x0, y0, trafo_size, trafo_size);
  1006. s->hpc.intra_pred[log2_trafo_size - 2](lc, x0, y0, 0);
  1007. }
  1008. if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
  1009. (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
  1010. int scan_idx = SCAN_DIAG;
  1011. int scan_idx_c = SCAN_DIAG;
  1012. int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
  1013. (s->ps.sps->chroma_format_idc == 2 &&
  1014. (cbf_cb[1] || cbf_cr[1]));
  1015. if (s->ps.pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
  1016. lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(lc);
  1017. if (lc->tu.cu_qp_delta != 0)
  1018. if (ff_hevc_cu_qp_delta_sign_flag(lc) == 1)
  1019. lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
  1020. lc->tu.is_cu_qp_delta_coded = 1;
  1021. if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
  1022. lc->tu.cu_qp_delta > (25 + s->ps.sps->qp_bd_offset / 2)) {
  1023. av_log(s->avctx, AV_LOG_ERROR,
  1024. "The cu_qp_delta %d is outside the valid range "
  1025. "[%d, %d].\n",
  1026. lc->tu.cu_qp_delta,
  1027. -(26 + s->ps.sps->qp_bd_offset / 2),
  1028. (25 + s->ps.sps->qp_bd_offset / 2));
  1029. return AVERROR_INVALIDDATA;
  1030. }
  1031. ff_hevc_set_qPy(lc, cb_xBase, cb_yBase, log2_cb_size);
  1032. }
  1033. if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
  1034. !lc->cu.cu_transquant_bypass_flag && !lc->tu.is_cu_chroma_qp_offset_coded) {
  1035. int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(lc);
  1036. if (cu_chroma_qp_offset_flag) {
  1037. int cu_chroma_qp_offset_idx = 0;
  1038. if (s->ps.pps->chroma_qp_offset_list_len_minus1 > 0) {
  1039. cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(lc);
  1040. av_log(s->avctx, AV_LOG_ERROR,
  1041. "cu_chroma_qp_offset_idx not yet tested.\n");
  1042. }
  1043. lc->tu.cu_qp_offset_cb = s->ps.pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
  1044. lc->tu.cu_qp_offset_cr = s->ps.pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
  1045. } else {
  1046. lc->tu.cu_qp_offset_cb = 0;
  1047. lc->tu.cu_qp_offset_cr = 0;
  1048. }
  1049. lc->tu.is_cu_chroma_qp_offset_coded = 1;
  1050. }
  1051. if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
  1052. if (lc->tu.intra_pred_mode >= 6 &&
  1053. lc->tu.intra_pred_mode <= 14) {
  1054. scan_idx = SCAN_VERT;
  1055. } else if (lc->tu.intra_pred_mode >= 22 &&
  1056. lc->tu.intra_pred_mode <= 30) {
  1057. scan_idx = SCAN_HORIZ;
  1058. }
  1059. if (lc->tu.intra_pred_mode_c >= 6 &&
  1060. lc->tu.intra_pred_mode_c <= 14) {
  1061. scan_idx_c = SCAN_VERT;
  1062. } else if (lc->tu.intra_pred_mode_c >= 22 &&
  1063. lc->tu.intra_pred_mode_c <= 30) {
  1064. scan_idx_c = SCAN_HORIZ;
  1065. }
  1066. }
  1067. lc->tu.cross_pf = 0;
  1068. if (cbf_luma)
  1069. ff_hevc_hls_residual_coding(lc, x0, y0, log2_trafo_size, scan_idx, 0);
  1070. if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
  1071. int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
  1072. int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
  1073. lc->tu.cross_pf = (s->ps.pps->cross_component_prediction_enabled_flag && cbf_luma &&
  1074. (lc->cu.pred_mode == MODE_INTER ||
  1075. (lc->tu.chroma_mode_c == 4)));
  1076. if (lc->tu.cross_pf) {
  1077. hls_cross_component_pred(lc, 0);
  1078. }
  1079. for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  1080. if (lc->cu.pred_mode == MODE_INTRA) {
  1081. ff_hevc_set_neighbour_available(lc, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
  1082. s->hpc.intra_pred[log2_trafo_size_c - 2](lc, x0, y0 + (i << log2_trafo_size_c), 1);
  1083. }
  1084. if (cbf_cb[i])
  1085. ff_hevc_hls_residual_coding(lc, x0, y0 + (i << log2_trafo_size_c),
  1086. log2_trafo_size_c, scan_idx_c, 1);
  1087. else
  1088. if (lc->tu.cross_pf) {
  1089. ptrdiff_t stride = s->frame->linesize[1];
  1090. int hshift = s->ps.sps->hshift[1];
  1091. int vshift = s->ps.sps->vshift[1];
  1092. const int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
  1093. int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
  1094. int size = 1 << log2_trafo_size_c;
  1095. uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
  1096. ((x0 >> hshift) << s->ps.sps->pixel_shift)];
  1097. for (i = 0; i < (size * size); i++) {
  1098. coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
  1099. }
  1100. s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
  1101. }
  1102. }
  1103. if (lc->tu.cross_pf) {
  1104. hls_cross_component_pred(lc, 1);
  1105. }
  1106. for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  1107. if (lc->cu.pred_mode == MODE_INTRA) {
  1108. ff_hevc_set_neighbour_available(lc, x0, y0 + (i << log2_trafo_size_c),
  1109. trafo_size_h, trafo_size_v);
  1110. s->hpc.intra_pred[log2_trafo_size_c - 2](lc, x0, y0 + (i << log2_trafo_size_c), 2);
  1111. }
  1112. if (cbf_cr[i])
  1113. ff_hevc_hls_residual_coding(lc, x0, y0 + (i << log2_trafo_size_c),
  1114. log2_trafo_size_c, scan_idx_c, 2);
  1115. else
  1116. if (lc->tu.cross_pf) {
  1117. ptrdiff_t stride = s->frame->linesize[2];
  1118. int hshift = s->ps.sps->hshift[2];
  1119. int vshift = s->ps.sps->vshift[2];
  1120. const int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
  1121. int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
  1122. int size = 1 << log2_trafo_size_c;
  1123. uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
  1124. ((x0 >> hshift) << s->ps.sps->pixel_shift)];
  1125. for (i = 0; i < (size * size); i++) {
  1126. coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
  1127. }
  1128. s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
  1129. }
  1130. }
  1131. } else if (s->ps.sps->chroma_format_idc && blk_idx == 3) {
  1132. int trafo_size_h = 1 << (log2_trafo_size + 1);
  1133. int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
  1134. for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  1135. if (lc->cu.pred_mode == MODE_INTRA) {
  1136. ff_hevc_set_neighbour_available(lc, xBase, yBase + (i << log2_trafo_size),
  1137. trafo_size_h, trafo_size_v);
  1138. s->hpc.intra_pred[log2_trafo_size - 2](lc, xBase, yBase + (i << log2_trafo_size), 1);
  1139. }
  1140. if (cbf_cb[i])
  1141. ff_hevc_hls_residual_coding(lc, xBase, yBase + (i << log2_trafo_size),
  1142. log2_trafo_size, scan_idx_c, 1);
  1143. }
  1144. for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
  1145. if (lc->cu.pred_mode == MODE_INTRA) {
  1146. ff_hevc_set_neighbour_available(lc, xBase, yBase + (i << log2_trafo_size),
  1147. trafo_size_h, trafo_size_v);
  1148. s->hpc.intra_pred[log2_trafo_size - 2](lc, xBase, yBase + (i << log2_trafo_size), 2);
  1149. }
  1150. if (cbf_cr[i])
  1151. ff_hevc_hls_residual_coding(lc, xBase, yBase + (i << log2_trafo_size),
  1152. log2_trafo_size, scan_idx_c, 2);
  1153. }
  1154. }
  1155. } else if (s->ps.sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
  1156. if (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3) {
  1157. int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
  1158. int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
  1159. ff_hevc_set_neighbour_available(lc, x0, y0, trafo_size_h, trafo_size_v);
  1160. s->hpc.intra_pred[log2_trafo_size_c - 2](lc, x0, y0, 1);
  1161. s->hpc.intra_pred[log2_trafo_size_c - 2](lc, x0, y0, 2);
  1162. if (s->ps.sps->chroma_format_idc == 2) {
  1163. ff_hevc_set_neighbour_available(lc, x0, y0 + (1 << log2_trafo_size_c),
  1164. trafo_size_h, trafo_size_v);
  1165. s->hpc.intra_pred[log2_trafo_size_c - 2](lc, x0, y0 + (1 << log2_trafo_size_c), 1);
  1166. s->hpc.intra_pred[log2_trafo_size_c - 2](lc, x0, y0 + (1 << log2_trafo_size_c), 2);
  1167. }
  1168. } else if (blk_idx == 3) {
  1169. int trafo_size_h = 1 << (log2_trafo_size + 1);
  1170. int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
  1171. ff_hevc_set_neighbour_available(lc, xBase, yBase,
  1172. trafo_size_h, trafo_size_v);
  1173. s->hpc.intra_pred[log2_trafo_size - 2](lc, xBase, yBase, 1);
  1174. s->hpc.intra_pred[log2_trafo_size - 2](lc, xBase, yBase, 2);
  1175. if (s->ps.sps->chroma_format_idc == 2) {
  1176. ff_hevc_set_neighbour_available(lc, xBase, yBase + (1 << log2_trafo_size),
  1177. trafo_size_h, trafo_size_v);
  1178. s->hpc.intra_pred[log2_trafo_size - 2](lc, xBase, yBase + (1 << log2_trafo_size), 1);
  1179. s->hpc.intra_pred[log2_trafo_size - 2](lc, xBase, yBase + (1 << log2_trafo_size), 2);
  1180. }
  1181. }
  1182. }
  1183. return 0;
  1184. }
  1185. static void set_deblocking_bypass(const HEVCContext *s, int x0, int y0, int log2_cb_size)
  1186. {
  1187. int cb_size = 1 << log2_cb_size;
  1188. int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
  1189. int min_pu_width = s->ps.sps->min_pu_width;
  1190. int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
  1191. int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
  1192. int i, j;
  1193. for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
  1194. for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
  1195. s->is_pcm[i + j * min_pu_width] = 2;
  1196. }
  1197. static int hls_transform_tree(HEVCLocalContext *lc, int x0, int y0,
  1198. int xBase, int yBase, int cb_xBase, int cb_yBase,
  1199. int log2_cb_size, int log2_trafo_size,
  1200. int trafo_depth, int blk_idx,
  1201. const int *base_cbf_cb, const int *base_cbf_cr)
  1202. {
  1203. const HEVCContext *const s = lc->parent;
  1204. uint8_t split_transform_flag;
  1205. int cbf_cb[2];
  1206. int cbf_cr[2];
  1207. int ret;
  1208. cbf_cb[0] = base_cbf_cb[0];
  1209. cbf_cb[1] = base_cbf_cb[1];
  1210. cbf_cr[0] = base_cbf_cr[0];
  1211. cbf_cr[1] = base_cbf_cr[1];
  1212. if (lc->cu.intra_split_flag) {
  1213. if (trafo_depth == 1) {
  1214. lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
  1215. if (s->ps.sps->chroma_format_idc == 3) {
  1216. lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
  1217. lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
  1218. } else {
  1219. lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
  1220. lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
  1221. }
  1222. }
  1223. } else {
  1224. lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
  1225. lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
  1226. lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
  1227. }
  1228. if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
  1229. log2_trafo_size > s->ps.sps->log2_min_tb_size &&
  1230. trafo_depth < lc->cu.max_trafo_depth &&
  1231. !(lc->cu.intra_split_flag && trafo_depth == 0)) {
  1232. split_transform_flag = ff_hevc_split_transform_flag_decode(lc, log2_trafo_size);
  1233. } else {
  1234. int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
  1235. lc->cu.pred_mode == MODE_INTER &&
  1236. lc->cu.part_mode != PART_2Nx2N &&
  1237. trafo_depth == 0;
  1238. split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
  1239. (lc->cu.intra_split_flag && trafo_depth == 0) ||
  1240. inter_split;
  1241. }
  1242. if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
  1243. if (trafo_depth == 0 || cbf_cb[0]) {
  1244. cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
  1245. if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
  1246. cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
  1247. }
  1248. }
  1249. if (trafo_depth == 0 || cbf_cr[0]) {
  1250. cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
  1251. if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
  1252. cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
  1253. }
  1254. }
  1255. }
  1256. if (split_transform_flag) {
  1257. const int trafo_size_split = 1 << (log2_trafo_size - 1);
  1258. const int x1 = x0 + trafo_size_split;
  1259. const int y1 = y0 + trafo_size_split;
  1260. #define SUBDIVIDE(x, y, idx) \
  1261. do { \
  1262. ret = hls_transform_tree(lc, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size,\
  1263. log2_trafo_size - 1, trafo_depth + 1, idx, \
  1264. cbf_cb, cbf_cr); \
  1265. if (ret < 0) \
  1266. return ret; \
  1267. } while (0)
  1268. SUBDIVIDE(x0, y0, 0);
  1269. SUBDIVIDE(x1, y0, 1);
  1270. SUBDIVIDE(x0, y1, 2);
  1271. SUBDIVIDE(x1, y1, 3);
  1272. #undef SUBDIVIDE
  1273. } else {
  1274. int min_tu_size = 1 << s->ps.sps->log2_min_tb_size;
  1275. int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
  1276. int min_tu_width = s->ps.sps->min_tb_width;
  1277. int cbf_luma = 1;
  1278. if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
  1279. cbf_cb[0] || cbf_cr[0] ||
  1280. (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
  1281. cbf_luma = ff_hevc_cbf_luma_decode(lc, trafo_depth);
  1282. }
  1283. ret = hls_transform_unit(lc, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
  1284. log2_cb_size, log2_trafo_size,
  1285. blk_idx, cbf_luma, cbf_cb, cbf_cr);
  1286. if (ret < 0)
  1287. return ret;
  1288. // TODO: store cbf_luma somewhere else
  1289. if (cbf_luma) {
  1290. int i, j;
  1291. for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
  1292. for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
  1293. int x_tu = (x0 + j) >> log2_min_tu_size;
  1294. int y_tu = (y0 + i) >> log2_min_tu_size;
  1295. s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
  1296. }
  1297. }
  1298. if (!s->sh.disable_deblocking_filter_flag) {
  1299. ff_hevc_deblocking_boundary_strengths(lc, x0, y0, log2_trafo_size);
  1300. if (s->ps.pps->transquant_bypass_enable_flag &&
  1301. lc->cu.cu_transquant_bypass_flag)
  1302. set_deblocking_bypass(s, x0, y0, log2_trafo_size);
  1303. }
  1304. }
  1305. return 0;
  1306. }
  1307. static int hls_pcm_sample(HEVCLocalContext *lc, int x0, int y0, int log2_cb_size)
  1308. {
  1309. const HEVCContext *const s = lc->parent;
  1310. GetBitContext gb;
  1311. int cb_size = 1 << log2_cb_size;
  1312. ptrdiff_t stride0 = s->frame->linesize[0];
  1313. ptrdiff_t stride1 = s->frame->linesize[1];
  1314. ptrdiff_t stride2 = s->frame->linesize[2];
  1315. uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
  1316. uint8_t *dst1 = &s->frame->data[1][(y0 >> s->ps.sps->vshift[1]) * stride1 + ((x0 >> s->ps.sps->hshift[1]) << s->ps.sps->pixel_shift)];
  1317. uint8_t *dst2 = &s->frame->data[2][(y0 >> s->ps.sps->vshift[2]) * stride2 + ((x0 >> s->ps.sps->hshift[2]) << s->ps.sps->pixel_shift)];
  1318. int length = cb_size * cb_size * s->ps.sps->pcm.bit_depth +
  1319. (((cb_size >> s->ps.sps->hshift[1]) * (cb_size >> s->ps.sps->vshift[1])) +
  1320. ((cb_size >> s->ps.sps->hshift[2]) * (cb_size >> s->ps.sps->vshift[2]))) *
  1321. s->ps.sps->pcm.bit_depth_chroma;
  1322. const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
  1323. int ret;
  1324. if (!s->sh.disable_deblocking_filter_flag)
  1325. ff_hevc_deblocking_boundary_strengths(lc, x0, y0, log2_cb_size);
  1326. ret = init_get_bits(&gb, pcm, length);
  1327. if (ret < 0)
  1328. return ret;
  1329. s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, s->ps.sps->pcm.bit_depth);
  1330. if (s->ps.sps->chroma_format_idc) {
  1331. s->hevcdsp.put_pcm(dst1, stride1,
  1332. cb_size >> s->ps.sps->hshift[1],
  1333. cb_size >> s->ps.sps->vshift[1],
  1334. &gb, s->ps.sps->pcm.bit_depth_chroma);
  1335. s->hevcdsp.put_pcm(dst2, stride2,
  1336. cb_size >> s->ps.sps->hshift[2],
  1337. cb_size >> s->ps.sps->vshift[2],
  1338. &gb, s->ps.sps->pcm.bit_depth_chroma);
  1339. }
  1340. return 0;
  1341. }
  1342. /**
  1343. * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
  1344. *
  1345. * @param s HEVC decoding context
  1346. * @param dst target buffer for block data at block position
  1347. * @param dststride stride of the dst buffer
  1348. * @param ref reference picture buffer at origin (0, 0)
  1349. * @param mv motion vector (relative to block position) to get pixel data from
  1350. * @param x_off horizontal position of block from origin (0, 0)
  1351. * @param y_off vertical position of block from origin (0, 0)
  1352. * @param block_w width of block
  1353. * @param block_h height of block
  1354. * @param luma_weight weighting factor applied to the luma prediction
  1355. * @param luma_offset additive offset applied to the luma prediction value
  1356. */
  1357. static void luma_mc_uni(HEVCLocalContext *lc, uint8_t *dst, ptrdiff_t dststride,
  1358. const AVFrame *ref, const Mv *mv, int x_off, int y_off,
  1359. int block_w, int block_h, int luma_weight, int luma_offset)
  1360. {
  1361. const HEVCContext *const s = lc->parent;
  1362. const uint8_t *src = ref->data[0];
  1363. ptrdiff_t srcstride = ref->linesize[0];
  1364. int pic_width = s->ps.sps->width;
  1365. int pic_height = s->ps.sps->height;
  1366. int mx = mv->x & 3;
  1367. int my = mv->y & 3;
  1368. int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
  1369. (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
  1370. int idx = hevc_pel_weight[block_w];
  1371. x_off += mv->x >> 2;
  1372. y_off += mv->y >> 2;
  1373. src += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
  1374. if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
  1375. x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  1376. y_off >= pic_height - block_h - QPEL_EXTRA_AFTER ||
  1377. ref == s->frame) {
  1378. const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
  1379. int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
  1380. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
  1381. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
  1382. edge_emu_stride, srcstride,
  1383. block_w + QPEL_EXTRA,
  1384. block_h + QPEL_EXTRA,
  1385. x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
  1386. pic_width, pic_height);
  1387. src = lc->edge_emu_buffer + buf_offset;
  1388. srcstride = edge_emu_stride;
  1389. }
  1390. if (!weight_flag)
  1391. s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
  1392. block_h, mx, my, block_w);
  1393. else
  1394. s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
  1395. block_h, s->sh.luma_log2_weight_denom,
  1396. luma_weight, luma_offset, mx, my, block_w);
  1397. }
  1398. /**
  1399. * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
  1400. *
  1401. * @param s HEVC decoding context
  1402. * @param dst target buffer for block data at block position
  1403. * @param dststride stride of the dst buffer
  1404. * @param ref0 reference picture0 buffer at origin (0, 0)
  1405. * @param mv0 motion vector0 (relative to block position) to get pixel data from
  1406. * @param x_off horizontal position of block from origin (0, 0)
  1407. * @param y_off vertical position of block from origin (0, 0)
  1408. * @param block_w width of block
  1409. * @param block_h height of block
  1410. * @param ref1 reference picture1 buffer at origin (0, 0)
  1411. * @param mv1 motion vector1 (relative to block position) to get pixel data from
  1412. * @param current_mv current motion vector structure
  1413. */
  1414. static void luma_mc_bi(HEVCLocalContext *lc, uint8_t *dst, ptrdiff_t dststride,
  1415. const AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
  1416. int block_w, int block_h, const AVFrame *ref1,
  1417. const Mv *mv1, struct MvField *current_mv)
  1418. {
  1419. const HEVCContext *const s = lc->parent;
  1420. ptrdiff_t src0stride = ref0->linesize[0];
  1421. ptrdiff_t src1stride = ref1->linesize[0];
  1422. int pic_width = s->ps.sps->width;
  1423. int pic_height = s->ps.sps->height;
  1424. int mx0 = mv0->x & 3;
  1425. int my0 = mv0->y & 3;
  1426. int mx1 = mv1->x & 3;
  1427. int my1 = mv1->y & 3;
  1428. int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
  1429. (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
  1430. int x_off0 = x_off + (mv0->x >> 2);
  1431. int y_off0 = y_off + (mv0->y >> 2);
  1432. int x_off1 = x_off + (mv1->x >> 2);
  1433. int y_off1 = y_off + (mv1->y >> 2);
  1434. int idx = hevc_pel_weight[block_w];
  1435. const uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
  1436. const uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
  1437. if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
  1438. x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  1439. y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  1440. const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
  1441. int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
  1442. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
  1443. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
  1444. edge_emu_stride, src0stride,
  1445. block_w + QPEL_EXTRA,
  1446. block_h + QPEL_EXTRA,
  1447. x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
  1448. pic_width, pic_height);
  1449. src0 = lc->edge_emu_buffer + buf_offset;
  1450. src0stride = edge_emu_stride;
  1451. }
  1452. if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
  1453. x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
  1454. y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
  1455. const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
  1456. int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
  1457. int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
  1458. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
  1459. edge_emu_stride, src1stride,
  1460. block_w + QPEL_EXTRA,
  1461. block_h + QPEL_EXTRA,
  1462. x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
  1463. pic_width, pic_height);
  1464. src1 = lc->edge_emu_buffer2 + buf_offset;
  1465. src1stride = edge_emu_stride;
  1466. }
  1467. s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
  1468. block_h, mx0, my0, block_w);
  1469. if (!weight_flag)
  1470. s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
  1471. block_h, mx1, my1, block_w);
  1472. else
  1473. s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
  1474. block_h, s->sh.luma_log2_weight_denom,
  1475. s->sh.luma_weight_l0[current_mv->ref_idx[0]],
  1476. s->sh.luma_weight_l1[current_mv->ref_idx[1]],
  1477. s->sh.luma_offset_l0[current_mv->ref_idx[0]],
  1478. s->sh.luma_offset_l1[current_mv->ref_idx[1]],
  1479. mx1, my1, block_w);
  1480. }
  1481. /**
  1482. * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
  1483. *
  1484. * @param s HEVC decoding context
  1485. * @param dst1 target buffer for block data at block position (U plane)
  1486. * @param dst2 target buffer for block data at block position (V plane)
  1487. * @param dststride stride of the dst1 and dst2 buffers
  1488. * @param ref reference picture buffer at origin (0, 0)
  1489. * @param mv motion vector (relative to block position) to get pixel data from
  1490. * @param x_off horizontal position of block from origin (0, 0)
  1491. * @param y_off vertical position of block from origin (0, 0)
  1492. * @param block_w width of block
  1493. * @param block_h height of block
  1494. * @param chroma_weight weighting factor applied to the chroma prediction
  1495. * @param chroma_offset additive offset applied to the chroma prediction value
  1496. */
  1497. static void chroma_mc_uni(HEVCLocalContext *lc, uint8_t *dst0,
  1498. ptrdiff_t dststride, const uint8_t *src0, ptrdiff_t srcstride, int reflist,
  1499. int x_off, int y_off, int block_w, int block_h,
  1500. const struct MvField *current_mv, int chroma_weight, int chroma_offset)
  1501. {
  1502. const HEVCContext *const s = lc->parent;
  1503. int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
  1504. int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
  1505. const Mv *mv = &current_mv->mv[reflist];
  1506. int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
  1507. (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
  1508. int idx = hevc_pel_weight[block_w];
  1509. int hshift = s->ps.sps->hshift[1];
  1510. int vshift = s->ps.sps->vshift[1];
  1511. intptr_t mx = av_mod_uintp2(mv->x, 2 + hshift);
  1512. intptr_t my = av_mod_uintp2(mv->y, 2 + vshift);
  1513. intptr_t _mx = mx << (1 - hshift);
  1514. intptr_t _my = my << (1 - vshift);
  1515. int emu = src0 == s->frame->data[1] || src0 == s->frame->data[2];
  1516. x_off += mv->x >> (2 + hshift);
  1517. y_off += mv->y >> (2 + vshift);
  1518. src0 += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
  1519. if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
  1520. x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1521. y_off >= pic_height - block_h - EPEL_EXTRA_AFTER ||
  1522. emu) {
  1523. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
  1524. int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->ps.sps->pixel_shift));
  1525. int buf_offset0 = EPEL_EXTRA_BEFORE *
  1526. (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
  1527. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
  1528. edge_emu_stride, srcstride,
  1529. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1530. x_off - EPEL_EXTRA_BEFORE,
  1531. y_off - EPEL_EXTRA_BEFORE,
  1532. pic_width, pic_height);
  1533. src0 = lc->edge_emu_buffer + buf_offset0;
  1534. srcstride = edge_emu_stride;
  1535. }
  1536. if (!weight_flag)
  1537. s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
  1538. block_h, _mx, _my, block_w);
  1539. else
  1540. s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
  1541. block_h, s->sh.chroma_log2_weight_denom,
  1542. chroma_weight, chroma_offset, _mx, _my, block_w);
  1543. }
  1544. /**
  1545. * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
  1546. *
  1547. * @param s HEVC decoding context
  1548. * @param dst target buffer for block data at block position
  1549. * @param dststride stride of the dst buffer
  1550. * @param ref0 reference picture0 buffer at origin (0, 0)
  1551. * @param mv0 motion vector0 (relative to block position) to get pixel data from
  1552. * @param x_off horizontal position of block from origin (0, 0)
  1553. * @param y_off vertical position of block from origin (0, 0)
  1554. * @param block_w width of block
  1555. * @param block_h height of block
  1556. * @param ref1 reference picture1 buffer at origin (0, 0)
  1557. * @param mv1 motion vector1 (relative to block position) to get pixel data from
  1558. * @param current_mv current motion vector structure
  1559. * @param cidx chroma component(cb, cr)
  1560. */
  1561. static void chroma_mc_bi(HEVCLocalContext *lc, uint8_t *dst0, ptrdiff_t dststride,
  1562. const AVFrame *ref0, const AVFrame *ref1,
  1563. int x_off, int y_off, int block_w, int block_h, const MvField *current_mv, int cidx)
  1564. {
  1565. const HEVCContext *const s = lc->parent;
  1566. const uint8_t *src1 = ref0->data[cidx+1];
  1567. const uint8_t *src2 = ref1->data[cidx+1];
  1568. ptrdiff_t src1stride = ref0->linesize[cidx+1];
  1569. ptrdiff_t src2stride = ref1->linesize[cidx+1];
  1570. int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
  1571. (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
  1572. int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
  1573. int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
  1574. const Mv *const mv0 = &current_mv->mv[0];
  1575. const Mv *const mv1 = &current_mv->mv[1];
  1576. int hshift = s->ps.sps->hshift[1];
  1577. int vshift = s->ps.sps->vshift[1];
  1578. intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
  1579. intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
  1580. intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
  1581. intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
  1582. intptr_t _mx0 = mx0 << (1 - hshift);
  1583. intptr_t _my0 = my0 << (1 - vshift);
  1584. intptr_t _mx1 = mx1 << (1 - hshift);
  1585. intptr_t _my1 = my1 << (1 - vshift);
  1586. int x_off0 = x_off + (mv0->x >> (2 + hshift));
  1587. int y_off0 = y_off + (mv0->y >> (2 + vshift));
  1588. int x_off1 = x_off + (mv1->x >> (2 + hshift));
  1589. int y_off1 = y_off + (mv1->y >> (2 + vshift));
  1590. int idx = hevc_pel_weight[block_w];
  1591. src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
  1592. src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
  1593. if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
  1594. x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1595. y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
  1596. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
  1597. int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
  1598. int buf_offset1 = EPEL_EXTRA_BEFORE *
  1599. (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
  1600. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
  1601. edge_emu_stride, src1stride,
  1602. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1603. x_off0 - EPEL_EXTRA_BEFORE,
  1604. y_off0 - EPEL_EXTRA_BEFORE,
  1605. pic_width, pic_height);
  1606. src1 = lc->edge_emu_buffer + buf_offset1;
  1607. src1stride = edge_emu_stride;
  1608. }
  1609. if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
  1610. x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
  1611. y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
  1612. const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
  1613. int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
  1614. int buf_offset1 = EPEL_EXTRA_BEFORE *
  1615. (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
  1616. s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
  1617. edge_emu_stride, src2stride,
  1618. block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
  1619. x_off1 - EPEL_EXTRA_BEFORE,
  1620. y_off1 - EPEL_EXTRA_BEFORE,
  1621. pic_width, pic_height);
  1622. src2 = lc->edge_emu_buffer2 + buf_offset1;
  1623. src2stride = edge_emu_stride;
  1624. }
  1625. s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
  1626. block_h, _mx0, _my0, block_w);
  1627. if (!weight_flag)
  1628. s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
  1629. src2, src2stride, lc->tmp,
  1630. block_h, _mx1, _my1, block_w);
  1631. else
  1632. s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
  1633. src2, src2stride, lc->tmp,
  1634. block_h,
  1635. s->sh.chroma_log2_weight_denom,
  1636. s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
  1637. s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
  1638. s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
  1639. s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
  1640. _mx1, _my1, block_w);
  1641. }
  1642. static void hevc_await_progress(const HEVCContext *s, const HEVCFrame *ref,
  1643. const Mv *mv, int y0, int height)
  1644. {
  1645. if (s->threads_type == FF_THREAD_FRAME ) {
  1646. int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
  1647. ff_thread_await_progress(&ref->tf, y, 0);
  1648. }
  1649. }
  1650. static void hevc_luma_mv_mvp_mode(HEVCLocalContext *lc, int x0, int y0, int nPbW,
  1651. int nPbH, int log2_cb_size, int part_idx,
  1652. int merge_idx, MvField *mv)
  1653. {
  1654. const HEVCContext *const s = lc->parent;
  1655. enum InterPredIdc inter_pred_idc = PRED_L0;
  1656. int mvp_flag;
  1657. ff_hevc_set_neighbour_available(lc, x0, y0, nPbW, nPbH);
  1658. mv->pred_flag = 0;
  1659. if (s->sh.slice_type == HEVC_SLICE_B)
  1660. inter_pred_idc = ff_hevc_inter_pred_idc_decode(lc, nPbW, nPbH);
  1661. if (inter_pred_idc != PRED_L1) {
  1662. if (s->sh.nb_refs[L0])
  1663. mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(lc, s->sh.nb_refs[L0]);
  1664. mv->pred_flag = PF_L0;
  1665. ff_hevc_hls_mvd_coding(lc, x0, y0, 0);
  1666. mvp_flag = ff_hevc_mvp_lx_flag_decode(lc);
  1667. ff_hevc_luma_mv_mvp_mode(lc, x0, y0, nPbW, nPbH, log2_cb_size,
  1668. part_idx, merge_idx, mv, mvp_flag, 0);
  1669. mv->mv[0].x += lc->pu.mvd.x;
  1670. mv->mv[0].y += lc->pu.mvd.y;
  1671. }
  1672. if (inter_pred_idc != PRED_L0) {
  1673. if (s->sh.nb_refs[L1])
  1674. mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(lc, s->sh.nb_refs[L1]);
  1675. if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
  1676. AV_ZERO32(&lc->pu.mvd);
  1677. } else {
  1678. ff_hevc_hls_mvd_coding(lc, x0, y0, 1);
  1679. }
  1680. mv->pred_flag += PF_L1;
  1681. mvp_flag = ff_hevc_mvp_lx_flag_decode(lc);
  1682. ff_hevc_luma_mv_mvp_mode(lc, x0, y0, nPbW, nPbH, log2_cb_size,
  1683. part_idx, merge_idx, mv, mvp_flag, 1);
  1684. mv->mv[1].x += lc->pu.mvd.x;
  1685. mv->mv[1].y += lc->pu.mvd.y;
  1686. }
  1687. }
  1688. static void hls_prediction_unit(HEVCLocalContext *lc, int x0, int y0,
  1689. int nPbW, int nPbH,
  1690. int log2_cb_size, int partIdx, int idx)
  1691. {
  1692. #define POS(c_idx, x, y) \
  1693. &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
  1694. (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
  1695. const HEVCContext *const s = lc->parent;
  1696. int merge_idx = 0;
  1697. struct MvField current_mv = {{{ 0 }}};
  1698. int min_pu_width = s->ps.sps->min_pu_width;
  1699. MvField *tab_mvf = s->ref->tab_mvf;
  1700. const RefPicList *refPicList = s->ref->refPicList;
  1701. const HEVCFrame *ref0 = NULL, *ref1 = NULL;
  1702. uint8_t *dst0 = POS(0, x0, y0);
  1703. uint8_t *dst1 = POS(1, x0, y0);
  1704. uint8_t *dst2 = POS(2, x0, y0);
  1705. int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
  1706. int min_cb_width = s->ps.sps->min_cb_width;
  1707. int x_cb = x0 >> log2_min_cb_size;
  1708. int y_cb = y0 >> log2_min_cb_size;
  1709. int x_pu, y_pu;
  1710. int i, j;
  1711. int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
  1712. if (!skip_flag)
  1713. lc->pu.merge_flag = ff_hevc_merge_flag_decode(lc);
  1714. if (skip_flag || lc->pu.merge_flag) {
  1715. if (s->sh.max_num_merge_cand > 1)
  1716. merge_idx = ff_hevc_merge_idx_decode(lc);
  1717. else
  1718. merge_idx = 0;
  1719. ff_hevc_luma_mv_merge_mode(lc, x0, y0, nPbW, nPbH, log2_cb_size,
  1720. partIdx, merge_idx, &current_mv);
  1721. } else {
  1722. hevc_luma_mv_mvp_mode(lc, x0, y0, nPbW, nPbH, log2_cb_size,
  1723. partIdx, merge_idx, &current_mv);
  1724. }
  1725. x_pu = x0 >> s->ps.sps->log2_min_pu_size;
  1726. y_pu = y0 >> s->ps.sps->log2_min_pu_size;
  1727. for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
  1728. for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
  1729. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
  1730. if (current_mv.pred_flag & PF_L0) {
  1731. ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
  1732. if (!ref0 || !ref0->frame->data[0])
  1733. return;
  1734. hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
  1735. }
  1736. if (current_mv.pred_flag & PF_L1) {
  1737. ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
  1738. if (!ref1 || !ref1->frame->data[0])
  1739. return;
  1740. hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
  1741. }
  1742. if (current_mv.pred_flag == PF_L0) {
  1743. int x0_c = x0 >> s->ps.sps->hshift[1];
  1744. int y0_c = y0 >> s->ps.sps->vshift[1];
  1745. int nPbW_c = nPbW >> s->ps.sps->hshift[1];
  1746. int nPbH_c = nPbH >> s->ps.sps->vshift[1];
  1747. luma_mc_uni(lc, dst0, s->frame->linesize[0], ref0->frame,
  1748. &current_mv.mv[0], x0, y0, nPbW, nPbH,
  1749. s->sh.luma_weight_l0[current_mv.ref_idx[0]],
  1750. s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
  1751. if (s->ps.sps->chroma_format_idc) {
  1752. chroma_mc_uni(lc, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
  1753. 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1754. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
  1755. chroma_mc_uni(lc, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
  1756. 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1757. s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
  1758. }
  1759. } else if (current_mv.pred_flag == PF_L1) {
  1760. int x0_c = x0 >> s->ps.sps->hshift[1];
  1761. int y0_c = y0 >> s->ps.sps->vshift[1];
  1762. int nPbW_c = nPbW >> s->ps.sps->hshift[1];
  1763. int nPbH_c = nPbH >> s->ps.sps->vshift[1];
  1764. luma_mc_uni(lc, dst0, s->frame->linesize[0], ref1->frame,
  1765. &current_mv.mv[1], x0, y0, nPbW, nPbH,
  1766. s->sh.luma_weight_l1[current_mv.ref_idx[1]],
  1767. s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
  1768. if (s->ps.sps->chroma_format_idc) {
  1769. chroma_mc_uni(lc, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
  1770. 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1771. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
  1772. chroma_mc_uni(lc, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
  1773. 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
  1774. s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
  1775. }
  1776. } else if (current_mv.pred_flag == PF_BI) {
  1777. int x0_c = x0 >> s->ps.sps->hshift[1];
  1778. int y0_c = y0 >> s->ps.sps->vshift[1];
  1779. int nPbW_c = nPbW >> s->ps.sps->hshift[1];
  1780. int nPbH_c = nPbH >> s->ps.sps->vshift[1];
  1781. luma_mc_bi(lc, dst0, s->frame->linesize[0], ref0->frame,
  1782. &current_mv.mv[0], x0, y0, nPbW, nPbH,
  1783. ref1->frame, &current_mv.mv[1], &current_mv);
  1784. if (s->ps.sps->chroma_format_idc) {
  1785. chroma_mc_bi(lc, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
  1786. x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
  1787. chroma_mc_bi(lc, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
  1788. x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
  1789. }
  1790. }
  1791. }
  1792. /**
  1793. * 8.4.1
  1794. */
  1795. static int luma_intra_pred_mode(HEVCLocalContext *lc, int x0, int y0, int pu_size,
  1796. int prev_intra_luma_pred_flag)
  1797. {
  1798. const HEVCContext *const s = lc->parent;
  1799. int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
  1800. int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
  1801. int min_pu_width = s->ps.sps->min_pu_width;
  1802. int size_in_pus = pu_size >> s->ps.sps->log2_min_pu_size;
  1803. int x0b = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
  1804. int y0b = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
  1805. int cand_up = (lc->ctb_up_flag || y0b) ?
  1806. s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
  1807. int cand_left = (lc->ctb_left_flag || x0b) ?
  1808. s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
  1809. int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
  1810. MvField *tab_mvf = s->ref->tab_mvf;
  1811. int intra_pred_mode;
  1812. int candidate[3];
  1813. int i, j;
  1814. // intra_pred_mode prediction does not cross vertical CTB boundaries
  1815. if ((y0 - 1) < y_ctb)
  1816. cand_up = INTRA_DC;
  1817. if (cand_left == cand_up) {
  1818. if (cand_left < 2) {
  1819. candidate[0] = INTRA_PLANAR;
  1820. candidate[1] = INTRA_DC;
  1821. candidate[2] = INTRA_ANGULAR_26;
  1822. } else {
  1823. candidate[0] = cand_left;
  1824. candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
  1825. candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
  1826. }
  1827. } else {
  1828. candidate[0] = cand_left;
  1829. candidate[1] = cand_up;
  1830. if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
  1831. candidate[2] = INTRA_PLANAR;
  1832. } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
  1833. candidate[2] = INTRA_DC;
  1834. } else {
  1835. candidate[2] = INTRA_ANGULAR_26;
  1836. }
  1837. }
  1838. if (prev_intra_luma_pred_flag) {
  1839. intra_pred_mode = candidate[lc->pu.mpm_idx];
  1840. } else {
  1841. if (candidate[0] > candidate[1])
  1842. FFSWAP(uint8_t, candidate[0], candidate[1]);
  1843. if (candidate[0] > candidate[2])
  1844. FFSWAP(uint8_t, candidate[0], candidate[2]);
  1845. if (candidate[1] > candidate[2])
  1846. FFSWAP(uint8_t, candidate[1], candidate[2]);
  1847. intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
  1848. for (i = 0; i < 3; i++)
  1849. if (intra_pred_mode >= candidate[i])
  1850. intra_pred_mode++;
  1851. }
  1852. /* write the intra prediction units into the mv array */
  1853. if (!size_in_pus)
  1854. size_in_pus = 1;
  1855. for (i = 0; i < size_in_pus; i++) {
  1856. memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
  1857. intra_pred_mode, size_in_pus);
  1858. for (j = 0; j < size_in_pus; j++) {
  1859. tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
  1860. }
  1861. }
  1862. return intra_pred_mode;
  1863. }
  1864. static av_always_inline void set_ct_depth(const HEVCContext *s, int x0, int y0,
  1865. int log2_cb_size, int ct_depth)
  1866. {
  1867. int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
  1868. int x_cb = x0 >> s->ps.sps->log2_min_cb_size;
  1869. int y_cb = y0 >> s->ps.sps->log2_min_cb_size;
  1870. int y;
  1871. for (y = 0; y < length; y++)
  1872. memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
  1873. ct_depth, length);
  1874. }
  1875. static const uint8_t tab_mode_idx[] = {
  1876. 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
  1877. 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
  1878. static void intra_prediction_unit(HEVCLocalContext *lc, int x0, int y0,
  1879. int log2_cb_size)
  1880. {
  1881. const HEVCContext *const s = lc->parent;
  1882. static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
  1883. uint8_t prev_intra_luma_pred_flag[4];
  1884. int split = lc->cu.part_mode == PART_NxN;
  1885. int pb_size = (1 << log2_cb_size) >> split;
  1886. int side = split + 1;
  1887. int chroma_mode;
  1888. int i, j;
  1889. for (i = 0; i < side; i++)
  1890. for (j = 0; j < side; j++)
  1891. prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(lc);
  1892. for (i = 0; i < side; i++) {
  1893. for (j = 0; j < side; j++) {
  1894. if (prev_intra_luma_pred_flag[2 * i + j])
  1895. lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(lc);
  1896. else
  1897. lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(lc);
  1898. lc->pu.intra_pred_mode[2 * i + j] =
  1899. luma_intra_pred_mode(lc, x0 + pb_size * j, y0 + pb_size * i, pb_size,
  1900. prev_intra_luma_pred_flag[2 * i + j]);
  1901. }
  1902. }
  1903. if (s->ps.sps->chroma_format_idc == 3) {
  1904. for (i = 0; i < side; i++) {
  1905. for (j = 0; j < side; j++) {
  1906. lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
  1907. if (chroma_mode != 4) {
  1908. if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
  1909. lc->pu.intra_pred_mode_c[2 * i + j] = 34;
  1910. else
  1911. lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
  1912. } else {
  1913. lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
  1914. }
  1915. }
  1916. }
  1917. } else if (s->ps.sps->chroma_format_idc == 2) {
  1918. int mode_idx;
  1919. lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
  1920. if (chroma_mode != 4) {
  1921. if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
  1922. mode_idx = 34;
  1923. else
  1924. mode_idx = intra_chroma_table[chroma_mode];
  1925. } else {
  1926. mode_idx = lc->pu.intra_pred_mode[0];
  1927. }
  1928. lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
  1929. } else if (s->ps.sps->chroma_format_idc != 0) {
  1930. chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
  1931. if (chroma_mode != 4) {
  1932. if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
  1933. lc->pu.intra_pred_mode_c[0] = 34;
  1934. else
  1935. lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
  1936. } else {
  1937. lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
  1938. }
  1939. }
  1940. }
  1941. static void intra_prediction_unit_default_value(HEVCLocalContext *lc,
  1942. int x0, int y0,
  1943. int log2_cb_size)
  1944. {
  1945. const HEVCContext *const s = lc->parent;
  1946. int pb_size = 1 << log2_cb_size;
  1947. int size_in_pus = pb_size >> s->ps.sps->log2_min_pu_size;
  1948. int min_pu_width = s->ps.sps->min_pu_width;
  1949. MvField *tab_mvf = s->ref->tab_mvf;
  1950. int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
  1951. int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
  1952. int j, k;
  1953. if (size_in_pus == 0)
  1954. size_in_pus = 1;
  1955. for (j = 0; j < size_in_pus; j++)
  1956. memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
  1957. if (lc->cu.pred_mode == MODE_INTRA)
  1958. for (j = 0; j < size_in_pus; j++)
  1959. for (k = 0; k < size_in_pus; k++)
  1960. tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
  1961. }
  1962. static int hls_coding_unit(HEVCLocalContext *lc, const HEVCContext *s, int x0, int y0, int log2_cb_size)
  1963. {
  1964. int cb_size = 1 << log2_cb_size;
  1965. int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
  1966. int length = cb_size >> log2_min_cb_size;
  1967. int min_cb_width = s->ps.sps->min_cb_width;
  1968. int x_cb = x0 >> log2_min_cb_size;
  1969. int y_cb = y0 >> log2_min_cb_size;
  1970. int idx = log2_cb_size - 2;
  1971. int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
  1972. int x, y, ret;
  1973. lc->cu.x = x0;
  1974. lc->cu.y = y0;
  1975. lc->cu.pred_mode = MODE_INTRA;
  1976. lc->cu.part_mode = PART_2Nx2N;
  1977. lc->cu.intra_split_flag = 0;
  1978. SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
  1979. for (x = 0; x < 4; x++)
  1980. lc->pu.intra_pred_mode[x] = 1;
  1981. if (s->ps.pps->transquant_bypass_enable_flag) {
  1982. lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(lc);
  1983. if (lc->cu.cu_transquant_bypass_flag)
  1984. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  1985. } else
  1986. lc->cu.cu_transquant_bypass_flag = 0;
  1987. if (s->sh.slice_type != HEVC_SLICE_I) {
  1988. uint8_t skip_flag = ff_hevc_skip_flag_decode(lc, x0, y0, x_cb, y_cb);
  1989. x = y_cb * min_cb_width + x_cb;
  1990. for (y = 0; y < length; y++) {
  1991. memset(&s->skip_flag[x], skip_flag, length);
  1992. x += min_cb_width;
  1993. }
  1994. lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
  1995. } else {
  1996. x = y_cb * min_cb_width + x_cb;
  1997. for (y = 0; y < length; y++) {
  1998. memset(&s->skip_flag[x], 0, length);
  1999. x += min_cb_width;
  2000. }
  2001. }
  2002. if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
  2003. hls_prediction_unit(lc, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
  2004. intra_prediction_unit_default_value(lc, x0, y0, log2_cb_size);
  2005. if (!s->sh.disable_deblocking_filter_flag)
  2006. ff_hevc_deblocking_boundary_strengths(lc, x0, y0, log2_cb_size);
  2007. } else {
  2008. int pcm_flag = 0;
  2009. if (s->sh.slice_type != HEVC_SLICE_I)
  2010. lc->cu.pred_mode = ff_hevc_pred_mode_decode(lc);
  2011. if (lc->cu.pred_mode != MODE_INTRA ||
  2012. log2_cb_size == s->ps.sps->log2_min_cb_size) {
  2013. lc->cu.part_mode = ff_hevc_part_mode_decode(lc, log2_cb_size);
  2014. lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
  2015. lc->cu.pred_mode == MODE_INTRA;
  2016. }
  2017. if (lc->cu.pred_mode == MODE_INTRA) {
  2018. if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
  2019. log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
  2020. log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
  2021. pcm_flag = ff_hevc_pcm_flag_decode(lc);
  2022. }
  2023. if (pcm_flag) {
  2024. intra_prediction_unit_default_value(lc, x0, y0, log2_cb_size);
  2025. ret = hls_pcm_sample(lc, x0, y0, log2_cb_size);
  2026. if (s->ps.sps->pcm.loop_filter_disable_flag)
  2027. set_deblocking_bypass(s, x0, y0, log2_cb_size);
  2028. if (ret < 0)
  2029. return ret;
  2030. } else {
  2031. intra_prediction_unit(lc, x0, y0, log2_cb_size);
  2032. }
  2033. } else {
  2034. intra_prediction_unit_default_value(lc, x0, y0, log2_cb_size);
  2035. switch (lc->cu.part_mode) {
  2036. case PART_2Nx2N:
  2037. hls_prediction_unit(lc, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
  2038. break;
  2039. case PART_2NxN:
  2040. hls_prediction_unit(lc, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
  2041. hls_prediction_unit(lc, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
  2042. break;
  2043. case PART_Nx2N:
  2044. hls_prediction_unit(lc, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
  2045. hls_prediction_unit(lc, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
  2046. break;
  2047. case PART_2NxnU:
  2048. hls_prediction_unit(lc, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
  2049. hls_prediction_unit(lc, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
  2050. break;
  2051. case PART_2NxnD:
  2052. hls_prediction_unit(lc, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
  2053. hls_prediction_unit(lc, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
  2054. break;
  2055. case PART_nLx2N:
  2056. hls_prediction_unit(lc, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
  2057. hls_prediction_unit(lc, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
  2058. break;
  2059. case PART_nRx2N:
  2060. hls_prediction_unit(lc, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
  2061. hls_prediction_unit(lc, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
  2062. break;
  2063. case PART_NxN:
  2064. hls_prediction_unit(lc, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
  2065. hls_prediction_unit(lc, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
  2066. hls_prediction_unit(lc, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
  2067. hls_prediction_unit(lc, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
  2068. break;
  2069. }
  2070. }
  2071. if (!pcm_flag) {
  2072. int rqt_root_cbf = 1;
  2073. if (lc->cu.pred_mode != MODE_INTRA &&
  2074. !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
  2075. rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(lc);
  2076. }
  2077. if (rqt_root_cbf) {
  2078. const static int cbf[2] = { 0 };
  2079. lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
  2080. s->ps.sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
  2081. s->ps.sps->max_transform_hierarchy_depth_inter;
  2082. ret = hls_transform_tree(lc, x0, y0, x0, y0, x0, y0,
  2083. log2_cb_size,
  2084. log2_cb_size, 0, 0, cbf, cbf);
  2085. if (ret < 0)
  2086. return ret;
  2087. } else {
  2088. if (!s->sh.disable_deblocking_filter_flag)
  2089. ff_hevc_deblocking_boundary_strengths(lc, x0, y0, log2_cb_size);
  2090. }
  2091. }
  2092. }
  2093. if (s->ps.pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
  2094. ff_hevc_set_qPy(lc, x0, y0, log2_cb_size);
  2095. x = y_cb * min_cb_width + x_cb;
  2096. for (y = 0; y < length; y++) {
  2097. memset(&s->qp_y_tab[x], lc->qp_y, length);
  2098. x += min_cb_width;
  2099. }
  2100. if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
  2101. ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
  2102. lc->qPy_pred = lc->qp_y;
  2103. }
  2104. set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
  2105. return 0;
  2106. }
  2107. static int hls_coding_quadtree(HEVCLocalContext *lc, int x0, int y0,
  2108. int log2_cb_size, int cb_depth)
  2109. {
  2110. const HEVCContext *const s = lc->parent;
  2111. const int cb_size = 1 << log2_cb_size;
  2112. int ret;
  2113. int split_cu;
  2114. lc->ct_depth = cb_depth;
  2115. if (x0 + cb_size <= s->ps.sps->width &&
  2116. y0 + cb_size <= s->ps.sps->height &&
  2117. log2_cb_size > s->ps.sps->log2_min_cb_size) {
  2118. split_cu = ff_hevc_split_coding_unit_flag_decode(lc, cb_depth, x0, y0);
  2119. } else {
  2120. split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
  2121. }
  2122. if (s->ps.pps->cu_qp_delta_enabled_flag &&
  2123. log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
  2124. lc->tu.is_cu_qp_delta_coded = 0;
  2125. lc->tu.cu_qp_delta = 0;
  2126. }
  2127. if (s->sh.cu_chroma_qp_offset_enabled_flag &&
  2128. log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_chroma_qp_offset_depth) {
  2129. lc->tu.is_cu_chroma_qp_offset_coded = 0;
  2130. }
  2131. if (split_cu) {
  2132. int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
  2133. const int cb_size_split = cb_size >> 1;
  2134. const int x1 = x0 + cb_size_split;
  2135. const int y1 = y0 + cb_size_split;
  2136. int more_data = 0;
  2137. more_data = hls_coding_quadtree(lc, x0, y0, log2_cb_size - 1, cb_depth + 1);
  2138. if (more_data < 0)
  2139. return more_data;
  2140. if (more_data && x1 < s->ps.sps->width) {
  2141. more_data = hls_coding_quadtree(lc, x1, y0, log2_cb_size - 1, cb_depth + 1);
  2142. if (more_data < 0)
  2143. return more_data;
  2144. }
  2145. if (more_data && y1 < s->ps.sps->height) {
  2146. more_data = hls_coding_quadtree(lc, x0, y1, log2_cb_size - 1, cb_depth + 1);
  2147. if (more_data < 0)
  2148. return more_data;
  2149. }
  2150. if (more_data && x1 < s->ps.sps->width &&
  2151. y1 < s->ps.sps->height) {
  2152. more_data = hls_coding_quadtree(lc, x1, y1, log2_cb_size - 1, cb_depth + 1);
  2153. if (more_data < 0)
  2154. return more_data;
  2155. }
  2156. if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
  2157. ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
  2158. lc->qPy_pred = lc->qp_y;
  2159. if (more_data)
  2160. return ((x1 + cb_size_split) < s->ps.sps->width ||
  2161. (y1 + cb_size_split) < s->ps.sps->height);
  2162. else
  2163. return 0;
  2164. } else {
  2165. ret = hls_coding_unit(lc, s, x0, y0, log2_cb_size);
  2166. if (ret < 0)
  2167. return ret;
  2168. if ((!((x0 + cb_size) %
  2169. (1 << (s->ps.sps->log2_ctb_size))) ||
  2170. (x0 + cb_size >= s->ps.sps->width)) &&
  2171. (!((y0 + cb_size) %
  2172. (1 << (s->ps.sps->log2_ctb_size))) ||
  2173. (y0 + cb_size >= s->ps.sps->height))) {
  2174. int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(lc);
  2175. return !end_of_slice_flag;
  2176. } else {
  2177. return 1;
  2178. }
  2179. }
  2180. return 0;
  2181. }
  2182. static void hls_decode_neighbour(HEVCLocalContext *lc, int x_ctb, int y_ctb,
  2183. int ctb_addr_ts)
  2184. {
  2185. const HEVCContext *const s = lc->parent;
  2186. int ctb_size = 1 << s->ps.sps->log2_ctb_size;
  2187. int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  2188. int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
  2189. s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
  2190. if (s->ps.pps->entropy_coding_sync_enabled_flag) {
  2191. if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
  2192. lc->first_qp_group = 1;
  2193. lc->end_of_tiles_x = s->ps.sps->width;
  2194. } else if (s->ps.pps->tiles_enabled_flag) {
  2195. if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
  2196. int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
  2197. lc->end_of_tiles_x = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
  2198. lc->first_qp_group = 1;
  2199. }
  2200. } else {
  2201. lc->end_of_tiles_x = s->ps.sps->width;
  2202. }
  2203. lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
  2204. lc->boundary_flags = 0;
  2205. if (s->ps.pps->tiles_enabled_flag) {
  2206. if (x_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
  2207. lc->boundary_flags |= BOUNDARY_LEFT_TILE;
  2208. if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
  2209. lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
  2210. if (y_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->ps.sps->ctb_width]])
  2211. lc->boundary_flags |= BOUNDARY_UPPER_TILE;
  2212. if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
  2213. lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
  2214. } else {
  2215. if (ctb_addr_in_slice <= 0)
  2216. lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
  2217. if (ctb_addr_in_slice < s->ps.sps->ctb_width)
  2218. lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
  2219. }
  2220. lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
  2221. lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
  2222. lc->ctb_up_right_flag = ((y_ctb > 0) && (ctb_addr_in_slice+1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - s->ps.sps->ctb_width]]));
  2223. lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->ps.sps->ctb_width]]));
  2224. }
  2225. static int hls_decode_entry(AVCodecContext *avctxt, void *arg)
  2226. {
  2227. HEVCContext *s = avctxt->priv_data;
  2228. HEVCLocalContext *const lc = s->HEVClc;
  2229. int ctb_size = 1 << s->ps.sps->log2_ctb_size;
  2230. int more_data = 1;
  2231. int x_ctb = 0;
  2232. int y_ctb = 0;
  2233. int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
  2234. int ret;
  2235. if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
  2236. av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
  2237. return AVERROR_INVALIDDATA;
  2238. }
  2239. if (s->sh.dependent_slice_segment_flag) {
  2240. int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
  2241. if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
  2242. av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
  2243. return AVERROR_INVALIDDATA;
  2244. }
  2245. }
  2246. while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
  2247. int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  2248. x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
  2249. y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
  2250. hls_decode_neighbour(lc, x_ctb, y_ctb, ctb_addr_ts);
  2251. ret = ff_hevc_cabac_init(lc, ctb_addr_ts);
  2252. if (ret < 0) {
  2253. s->tab_slice_address[ctb_addr_rs] = -1;
  2254. return ret;
  2255. }
  2256. hls_sao_param(lc, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
  2257. s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
  2258. s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
  2259. s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
  2260. more_data = hls_coding_quadtree(lc, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
  2261. if (more_data < 0) {
  2262. s->tab_slice_address[ctb_addr_rs] = -1;
  2263. return more_data;
  2264. }
  2265. ctb_addr_ts++;
  2266. ff_hevc_save_states(lc, ctb_addr_ts);
  2267. ff_hevc_hls_filters(lc, x_ctb, y_ctb, ctb_size);
  2268. }
  2269. if (x_ctb + ctb_size >= s->ps.sps->width &&
  2270. y_ctb + ctb_size >= s->ps.sps->height)
  2271. ff_hevc_hls_filter(lc, x_ctb, y_ctb, ctb_size);
  2272. return ctb_addr_ts;
  2273. }
  2274. static int hls_slice_data(HEVCContext *s)
  2275. {
  2276. int ret = 0;
  2277. s->avctx->execute(s->avctx, hls_decode_entry, NULL, &ret , 1, 0);
  2278. return ret;
  2279. }
  2280. static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *hevc_lclist,
  2281. int job, int self_id)
  2282. {
  2283. HEVCLocalContext *lc = ((HEVCLocalContext**)hevc_lclist)[self_id];
  2284. const HEVCContext *const s = lc->parent;
  2285. int ctb_size = 1 << s->ps.sps->log2_ctb_size;
  2286. int more_data = 1;
  2287. int ctb_row = job;
  2288. int ctb_addr_rs = s->sh.slice_ctb_addr_rs + ctb_row * ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size);
  2289. int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
  2290. int thread = ctb_row % s->threads_number;
  2291. int ret;
  2292. if(ctb_row) {
  2293. ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
  2294. if (ret < 0)
  2295. goto error;
  2296. ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
  2297. }
  2298. while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
  2299. int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
  2300. int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
  2301. hls_decode_neighbour(lc, x_ctb, y_ctb, ctb_addr_ts);
  2302. ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
  2303. /* atomic_load's prototype requires a pointer to non-const atomic variable
  2304. * (due to implementations via mutexes, where reads involve writes).
  2305. * Of course, casting const away here is nevertheless safe. */
  2306. if (atomic_load((atomic_int*)&s->wpp_err)) {
  2307. ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
  2308. return 0;
  2309. }
  2310. ret = ff_hevc_cabac_init(lc, ctb_addr_ts);
  2311. if (ret < 0)
  2312. goto error;
  2313. hls_sao_param(lc, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
  2314. more_data = hls_coding_quadtree(lc, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
  2315. if (more_data < 0) {
  2316. ret = more_data;
  2317. goto error;
  2318. }
  2319. ctb_addr_ts++;
  2320. ff_hevc_save_states(lc, ctb_addr_ts);
  2321. ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
  2322. ff_hevc_hls_filters(lc, x_ctb, y_ctb, ctb_size);
  2323. if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
  2324. /* Casting const away here is safe, because it is an atomic operation. */
  2325. atomic_store((atomic_int*)&s->wpp_err, 1);
  2326. ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
  2327. return 0;
  2328. }
  2329. if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
  2330. ff_hevc_hls_filter(lc, x_ctb, y_ctb, ctb_size);
  2331. ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
  2332. return ctb_addr_ts;
  2333. }
  2334. ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
  2335. x_ctb+=ctb_size;
  2336. if(x_ctb >= s->ps.sps->width) {
  2337. break;
  2338. }
  2339. }
  2340. ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
  2341. return 0;
  2342. error:
  2343. s->tab_slice_address[ctb_addr_rs] = -1;
  2344. /* Casting const away here is safe, because it is an atomic operation. */
  2345. atomic_store((atomic_int*)&s->wpp_err, 1);
  2346. ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
  2347. return ret;
  2348. }
  2349. static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
  2350. {
  2351. const uint8_t *data = nal->data;
  2352. int length = nal->size;
  2353. HEVCLocalContext *lc = s->HEVClc;
  2354. int *ret;
  2355. int64_t offset;
  2356. int64_t startheader, cmpt = 0;
  2357. int i, j, res = 0;
  2358. if (s->sh.slice_ctb_addr_rs + s->sh.num_entry_point_offsets * s->ps.sps->ctb_width >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
  2359. av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
  2360. s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
  2361. s->ps.sps->ctb_width, s->ps.sps->ctb_height
  2362. );
  2363. return AVERROR_INVALIDDATA;
  2364. }
  2365. for (i = 1; i < s->threads_number; i++) {
  2366. if (s->HEVClcList[i])
  2367. continue;
  2368. s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
  2369. if (!s->HEVClcList[i])
  2370. return AVERROR(ENOMEM);
  2371. s->HEVClcList[i]->logctx = s->avctx;
  2372. s->HEVClcList[i]->parent = s;
  2373. s->HEVClcList[i]->common_cabac_state = &s->cabac;
  2374. }
  2375. offset = (lc->gb.index >> 3);
  2376. for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
  2377. if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
  2378. startheader--;
  2379. cmpt++;
  2380. }
  2381. }
  2382. for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
  2383. offset += (s->sh.entry_point_offset[i - 1] - cmpt);
  2384. for (j = 0, cmpt = 0, startheader = offset
  2385. + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
  2386. if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
  2387. startheader--;
  2388. cmpt++;
  2389. }
  2390. }
  2391. s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
  2392. s->sh.offset[i - 1] = offset;
  2393. }
  2394. if (s->sh.num_entry_point_offsets != 0) {
  2395. offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
  2396. if (length < offset) {
  2397. av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
  2398. return AVERROR_INVALIDDATA;
  2399. }
  2400. s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
  2401. s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
  2402. }
  2403. s->data = data;
  2404. for (i = 1; i < s->threads_number; i++) {
  2405. s->HEVClcList[i]->first_qp_group = 1;
  2406. s->HEVClcList[i]->qp_y = s->HEVClc->qp_y;
  2407. }
  2408. atomic_store(&s->wpp_err, 0);
  2409. res = ff_slice_thread_allocz_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
  2410. if (res < 0)
  2411. return res;
  2412. ret = av_calloc(s->sh.num_entry_point_offsets + 1, sizeof(*ret));
  2413. if (!ret)
  2414. return AVERROR(ENOMEM);
  2415. if (s->ps.pps->entropy_coding_sync_enabled_flag)
  2416. s->avctx->execute2(s->avctx, hls_decode_entry_wpp, s->HEVClcList, ret, s->sh.num_entry_point_offsets + 1);
  2417. for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
  2418. res += ret[i];
  2419. av_free(ret);
  2420. return res;
  2421. }
  2422. static int set_side_data(HEVCContext *s)
  2423. {
  2424. AVFrame *out = s->ref->frame;
  2425. int ret;
  2426. // Decrement the mastering display and content light level flag when IRAP
  2427. // frame has no_rasl_output_flag=1 so the side data persists for the entire
  2428. // coded video sequence.
  2429. if (IS_IRAP(s) && s->no_rasl_output_flag) {
  2430. if (s->sei.common.mastering_display.present > 0)
  2431. s->sei.common.mastering_display.present--;
  2432. if (s->sei.common.content_light.present > 0)
  2433. s->sei.common.content_light.present--;
  2434. }
  2435. ret = ff_h2645_sei_to_frame(out, &s->sei.common, AV_CODEC_ID_HEVC, s->avctx,
  2436. &s->ps.sps->vui.common,
  2437. s->ps.sps->bit_depth, s->ps.sps->bit_depth_chroma,
  2438. s->ref->poc /* no poc_offset in HEVC */);
  2439. if (ret < 0)
  2440. return ret;
  2441. if (s->sei.timecode.present) {
  2442. uint32_t *tc_sd;
  2443. char tcbuf[AV_TIMECODE_STR_SIZE];
  2444. AVFrameSideData *tcside;
  2445. ret = ff_frame_new_side_data(s->avctx, out, AV_FRAME_DATA_S12M_TIMECODE,
  2446. sizeof(uint32_t) * 4, &tcside);
  2447. if (ret < 0)
  2448. return ret;
  2449. if (tcside) {
  2450. tc_sd = (uint32_t*)tcside->data;
  2451. tc_sd[0] = s->sei.timecode.num_clock_ts;
  2452. for (int i = 0; i < tc_sd[0]; i++) {
  2453. int drop = s->sei.timecode.cnt_dropped_flag[i];
  2454. int hh = s->sei.timecode.hours_value[i];
  2455. int mm = s->sei.timecode.minutes_value[i];
  2456. int ss = s->sei.timecode.seconds_value[i];
  2457. int ff = s->sei.timecode.n_frames[i];
  2458. tc_sd[i + 1] = av_timecode_get_smpte(s->avctx->framerate, drop, hh, mm, ss, ff);
  2459. av_timecode_make_smpte_tc_string2(tcbuf, s->avctx->framerate, tc_sd[i + 1], 0, 0);
  2460. av_dict_set(&out->metadata, "timecode", tcbuf, 0);
  2461. }
  2462. }
  2463. s->sei.timecode.num_clock_ts = 0;
  2464. }
  2465. if (s->sei.common.dynamic_hdr_plus.info) {
  2466. AVBufferRef *info_ref = av_buffer_ref(s->sei.common.dynamic_hdr_plus.info);
  2467. if (!info_ref)
  2468. return AVERROR(ENOMEM);
  2469. ret = ff_frame_new_side_data_from_buf(s->avctx, out, AV_FRAME_DATA_DYNAMIC_HDR_PLUS, &info_ref, NULL);
  2470. if (ret < 0)
  2471. return ret;
  2472. }
  2473. if (s->rpu_buf) {
  2474. AVFrameSideData *rpu = av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_DOVI_RPU_BUFFER, s->rpu_buf);
  2475. if (!rpu)
  2476. return AVERROR(ENOMEM);
  2477. s->rpu_buf = NULL;
  2478. }
  2479. if ((ret = ff_dovi_attach_side_data(&s->dovi_ctx, out)) < 0)
  2480. return ret;
  2481. if (s->sei.common.dynamic_hdr_vivid.info) {
  2482. AVBufferRef *info_ref = av_buffer_ref(s->sei.common.dynamic_hdr_vivid.info);
  2483. if (!info_ref)
  2484. return AVERROR(ENOMEM);
  2485. if (!av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_DYNAMIC_HDR_VIVID, info_ref)) {
  2486. av_buffer_unref(&info_ref);
  2487. return AVERROR(ENOMEM);
  2488. }
  2489. }
  2490. return 0;
  2491. }
  2492. static int hevc_frame_start(HEVCContext *s)
  2493. {
  2494. HEVCLocalContext *lc = s->HEVClc;
  2495. int pic_size_in_ctb = ((s->ps.sps->width >> s->ps.sps->log2_min_cb_size) + 1) *
  2496. ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
  2497. int ret;
  2498. memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
  2499. memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
  2500. memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
  2501. memset(s->is_pcm, 0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
  2502. memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
  2503. s->is_decoded = 0;
  2504. s->first_nal_type = s->nal_unit_type;
  2505. s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
  2506. if (s->ps.pps->tiles_enabled_flag)
  2507. lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
  2508. ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
  2509. if (ret < 0)
  2510. goto fail;
  2511. ret = ff_hevc_frame_rps(s);
  2512. if (ret < 0) {
  2513. av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
  2514. goto fail;
  2515. }
  2516. if (IS_IRAP(s))
  2517. s->ref->frame->flags |= AV_FRAME_FLAG_KEY;
  2518. else
  2519. s->ref->frame->flags &= ~AV_FRAME_FLAG_KEY;
  2520. s->ref->needs_fg = (s->sei.common.film_grain_characteristics.present ||
  2521. s->sei.common.aom_film_grain.enable) &&
  2522. !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) &&
  2523. !s->avctx->hwaccel;
  2524. ret = set_side_data(s);
  2525. if (ret < 0)
  2526. goto fail;
  2527. if (s->ref->needs_fg &&
  2528. ( s->sei.common.film_grain_characteristics.present &&
  2529. !ff_h274_film_grain_params_supported(s->sei.common.film_grain_characteristics.model_id,
  2530. s->ref->frame->format))
  2531. || !av_film_grain_params_select(s->ref->frame)) {
  2532. av_log_once(s->avctx, AV_LOG_WARNING, AV_LOG_DEBUG, &s->film_grain_warning_shown,
  2533. "Unsupported film grain parameters. Ignoring film grain.\n");
  2534. s->ref->needs_fg = 0;
  2535. }
  2536. if (s->ref->needs_fg) {
  2537. s->ref->frame_grain->format = s->ref->frame->format;
  2538. s->ref->frame_grain->width = s->ref->frame->width;
  2539. s->ref->frame_grain->height = s->ref->frame->height;
  2540. if ((ret = ff_thread_get_buffer(s->avctx, s->ref->frame_grain, 0)) < 0)
  2541. goto fail;
  2542. }
  2543. s->frame->pict_type = 3 - s->sh.slice_type;
  2544. if (!IS_IRAP(s))
  2545. ff_hevc_bump_frame(s);
  2546. av_frame_unref(s->output_frame);
  2547. ret = ff_hevc_output_frame(s, s->output_frame, 0);
  2548. if (ret < 0)
  2549. goto fail;
  2550. if (!s->avctx->hwaccel)
  2551. ff_thread_finish_setup(s->avctx);
  2552. return 0;
  2553. fail:
  2554. if (s->ref)
  2555. ff_hevc_unref_frame(s->ref, ~0);
  2556. s->ref = s->collocated_ref = NULL;
  2557. return ret;
  2558. }
  2559. static int hevc_frame_end(HEVCContext *s)
  2560. {
  2561. HEVCFrame *out = s->ref;
  2562. const AVFilmGrainParams *fgp;
  2563. av_unused int ret;
  2564. if (out->needs_fg) {
  2565. av_assert0(out->frame_grain->buf[0]);
  2566. fgp = av_film_grain_params_select(out->frame);
  2567. switch (fgp->type) {
  2568. case AV_FILM_GRAIN_PARAMS_NONE:
  2569. av_assert0(0);
  2570. return AVERROR_BUG;
  2571. case AV_FILM_GRAIN_PARAMS_H274:
  2572. ret = ff_h274_apply_film_grain(out->frame_grain, out->frame,
  2573. &s->h274db, fgp);
  2574. break;
  2575. case AV_FILM_GRAIN_PARAMS_AV1:
  2576. ret = ff_aom_apply_film_grain(out->frame_grain, out->frame, fgp);
  2577. break;
  2578. }
  2579. av_assert1(ret >= 0);
  2580. }
  2581. return 0;
  2582. }
  2583. static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
  2584. {
  2585. HEVCLocalContext *lc = s->HEVClc;
  2586. GetBitContext *gb = &lc->gb;
  2587. int ctb_addr_ts, ret;
  2588. *gb = nal->gb;
  2589. s->nal_unit_type = nal->type;
  2590. s->temporal_id = nal->temporal_id;
  2591. switch (s->nal_unit_type) {
  2592. case HEVC_NAL_VPS:
  2593. if (FF_HW_HAS_CB(s->avctx, decode_params)) {
  2594. ret = FF_HW_CALL(s->avctx, decode_params,
  2595. nal->type, nal->raw_data, nal->raw_size);
  2596. if (ret < 0)
  2597. goto fail;
  2598. }
  2599. ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
  2600. if (ret < 0)
  2601. goto fail;
  2602. break;
  2603. case HEVC_NAL_SPS:
  2604. if (FF_HW_HAS_CB(s->avctx, decode_params)) {
  2605. ret = FF_HW_CALL(s->avctx, decode_params,
  2606. nal->type, nal->raw_data, nal->raw_size);
  2607. if (ret < 0)
  2608. goto fail;
  2609. }
  2610. ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
  2611. s->apply_defdispwin);
  2612. if (ret < 0)
  2613. goto fail;
  2614. break;
  2615. case HEVC_NAL_PPS:
  2616. if (FF_HW_HAS_CB(s->avctx, decode_params)) {
  2617. ret = FF_HW_CALL(s->avctx, decode_params,
  2618. nal->type, nal->raw_data, nal->raw_size);
  2619. if (ret < 0)
  2620. goto fail;
  2621. }
  2622. ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
  2623. if (ret < 0)
  2624. goto fail;
  2625. break;
  2626. case HEVC_NAL_SEI_PREFIX:
  2627. case HEVC_NAL_SEI_SUFFIX:
  2628. if (FF_HW_HAS_CB(s->avctx, decode_params)) {
  2629. ret = FF_HW_CALL(s->avctx, decode_params,
  2630. nal->type, nal->raw_data, nal->raw_size);
  2631. if (ret < 0)
  2632. goto fail;
  2633. }
  2634. ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
  2635. if (ret < 0)
  2636. goto fail;
  2637. break;
  2638. case HEVC_NAL_TRAIL_R:
  2639. case HEVC_NAL_TRAIL_N:
  2640. case HEVC_NAL_TSA_N:
  2641. case HEVC_NAL_TSA_R:
  2642. case HEVC_NAL_STSA_N:
  2643. case HEVC_NAL_STSA_R:
  2644. case HEVC_NAL_BLA_W_LP:
  2645. case HEVC_NAL_BLA_W_RADL:
  2646. case HEVC_NAL_BLA_N_LP:
  2647. case HEVC_NAL_IDR_W_RADL:
  2648. case HEVC_NAL_IDR_N_LP:
  2649. case HEVC_NAL_CRA_NUT:
  2650. case HEVC_NAL_RADL_N:
  2651. case HEVC_NAL_RADL_R:
  2652. case HEVC_NAL_RASL_N:
  2653. case HEVC_NAL_RASL_R:
  2654. ret = hls_slice_header(s);
  2655. if (ret < 0)
  2656. return ret;
  2657. if (ret == 1) {
  2658. ret = AVERROR_INVALIDDATA;
  2659. goto fail;
  2660. }
  2661. if (
  2662. (s->avctx->skip_frame >= AVDISCARD_BIDIR && s->sh.slice_type == HEVC_SLICE_B) ||
  2663. (s->avctx->skip_frame >= AVDISCARD_NONINTRA && s->sh.slice_type != HEVC_SLICE_I) ||
  2664. (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IRAP(s))) {
  2665. break;
  2666. }
  2667. if (s->sh.first_slice_in_pic_flag) {
  2668. if (s->max_ra == INT_MAX) {
  2669. if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
  2670. s->max_ra = s->poc;
  2671. } else {
  2672. if (IS_IDR(s))
  2673. s->max_ra = INT_MIN;
  2674. }
  2675. }
  2676. if ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
  2677. s->poc <= s->max_ra) {
  2678. s->is_decoded = 0;
  2679. break;
  2680. } else {
  2681. if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
  2682. s->max_ra = INT_MIN;
  2683. }
  2684. s->overlap ++;
  2685. ret = hevc_frame_start(s);
  2686. if (ret < 0)
  2687. return ret;
  2688. } else if (!s->ref) {
  2689. av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
  2690. goto fail;
  2691. }
  2692. if (s->nal_unit_type != s->first_nal_type) {
  2693. av_log(s->avctx, AV_LOG_ERROR,
  2694. "Non-matching NAL types of the VCL NALUs: %d %d\n",
  2695. s->first_nal_type, s->nal_unit_type);
  2696. return AVERROR_INVALIDDATA;
  2697. }
  2698. if (!s->sh.dependent_slice_segment_flag &&
  2699. s->sh.slice_type != HEVC_SLICE_I) {
  2700. ret = ff_hevc_slice_rpl(s);
  2701. if (ret < 0) {
  2702. av_log(s->avctx, AV_LOG_WARNING,
  2703. "Error constructing the reference lists for the current slice.\n");
  2704. goto fail;
  2705. }
  2706. }
  2707. if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
  2708. ret = FF_HW_CALL(s->avctx, start_frame, NULL, 0);
  2709. if (ret < 0)
  2710. goto fail;
  2711. }
  2712. if (s->avctx->hwaccel) {
  2713. ret = FF_HW_CALL(s->avctx, decode_slice, nal->raw_data, nal->raw_size);
  2714. if (ret < 0)
  2715. goto fail;
  2716. } else {
  2717. if (s->avctx->profile == AV_PROFILE_HEVC_SCC) {
  2718. av_log(s->avctx, AV_LOG_ERROR,
  2719. "SCC profile is not yet implemented in hevc native decoder.\n");
  2720. ret = AVERROR_PATCHWELCOME;
  2721. goto fail;
  2722. }
  2723. if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
  2724. ctb_addr_ts = hls_slice_data_wpp(s, nal);
  2725. else
  2726. ctb_addr_ts = hls_slice_data(s);
  2727. if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
  2728. ret = hevc_frame_end(s);
  2729. if (ret < 0)
  2730. goto fail;
  2731. s->is_decoded = 1;
  2732. }
  2733. if (ctb_addr_ts < 0) {
  2734. ret = ctb_addr_ts;
  2735. goto fail;
  2736. }
  2737. }
  2738. break;
  2739. case HEVC_NAL_EOS_NUT:
  2740. case HEVC_NAL_EOB_NUT:
  2741. s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
  2742. s->max_ra = INT_MAX;
  2743. break;
  2744. case HEVC_NAL_AUD:
  2745. case HEVC_NAL_FD_NUT:
  2746. case HEVC_NAL_UNSPEC62:
  2747. break;
  2748. default:
  2749. av_log(s->avctx, AV_LOG_INFO,
  2750. "Skipping NAL unit %d\n", s->nal_unit_type);
  2751. }
  2752. return 0;
  2753. fail:
  2754. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  2755. return ret;
  2756. return 0;
  2757. }
  2758. static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
  2759. {
  2760. int i, ret = 0;
  2761. int eos_at_start = 1;
  2762. s->ref = s->collocated_ref = NULL;
  2763. s->last_eos = s->eos;
  2764. s->eos = 0;
  2765. s->overlap = 0;
  2766. /* split the input packet into NAL units, so we know the upper bound on the
  2767. * number of slices in the frame */
  2768. ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
  2769. s->nal_length_size, s->avctx->codec_id, 1, 0);
  2770. if (ret < 0) {
  2771. av_log(s->avctx, AV_LOG_ERROR,
  2772. "Error splitting the input into NAL units.\n");
  2773. return ret;
  2774. }
  2775. for (i = 0; i < s->pkt.nb_nals; i++) {
  2776. if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
  2777. s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
  2778. if (eos_at_start) {
  2779. s->last_eos = 1;
  2780. } else {
  2781. s->eos = 1;
  2782. }
  2783. } else {
  2784. eos_at_start = 0;
  2785. }
  2786. }
  2787. /*
  2788. * Check for RPU delimiter.
  2789. *
  2790. * Dolby Vision RPUs masquerade as unregistered NALs of type 62.
  2791. *
  2792. * We have to do this check here an create the rpu buffer, since RPUs are appended
  2793. * to the end of an AU; they are the last non-EOB/EOS NAL in the AU.
  2794. */
  2795. if (s->pkt.nb_nals > 1 && s->pkt.nals[s->pkt.nb_nals - 1].type == HEVC_NAL_UNSPEC62 &&
  2796. s->pkt.nals[s->pkt.nb_nals - 1].size > 2 && !s->pkt.nals[s->pkt.nb_nals - 1].nuh_layer_id
  2797. && !s->pkt.nals[s->pkt.nb_nals - 1].temporal_id) {
  2798. H2645NAL *nal = &s->pkt.nals[s->pkt.nb_nals - 1];
  2799. if (s->rpu_buf) {
  2800. av_buffer_unref(&s->rpu_buf);
  2801. av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found in one AU. Skipping previous.\n");
  2802. }
  2803. s->rpu_buf = av_buffer_alloc(nal->raw_size - 2);
  2804. if (!s->rpu_buf)
  2805. return AVERROR(ENOMEM);
  2806. memcpy(s->rpu_buf->data, nal->raw_data + 2, nal->raw_size - 2);
  2807. ret = ff_dovi_rpu_parse(&s->dovi_ctx, nal->data + 2, nal->size - 2,
  2808. s->avctx->err_recognition);
  2809. if (ret < 0) {
  2810. av_buffer_unref(&s->rpu_buf);
  2811. av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");
  2812. /* ignore */
  2813. }
  2814. }
  2815. /* decode the NAL units */
  2816. for (i = 0; i < s->pkt.nb_nals; i++) {
  2817. H2645NAL *nal = &s->pkt.nals[i];
  2818. if (s->avctx->skip_frame >= AVDISCARD_ALL ||
  2819. (s->avctx->skip_frame >= AVDISCARD_NONREF
  2820. && ff_hevc_nal_is_nonref(nal->type)) || nal->nuh_layer_id > 0)
  2821. continue;
  2822. ret = decode_nal_unit(s, nal);
  2823. if (ret >= 0 && s->overlap > 2)
  2824. ret = AVERROR_INVALIDDATA;
  2825. if (ret < 0) {
  2826. av_log(s->avctx, AV_LOG_WARNING,
  2827. "Error parsing NAL unit #%d.\n", i);
  2828. goto fail;
  2829. }
  2830. }
  2831. fail:
  2832. if (s->ref && s->threads_type == FF_THREAD_FRAME)
  2833. ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
  2834. return ret;
  2835. }
  2836. static int verify_md5(HEVCContext *s, AVFrame *frame)
  2837. {
  2838. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  2839. char msg_buf[4 * (50 + 2 * 2 * 16 /* MD5-size */)];
  2840. int pixel_shift;
  2841. int err = 0;
  2842. int i, j;
  2843. if (!desc)
  2844. return AVERROR(EINVAL);
  2845. pixel_shift = desc->comp[0].depth > 8;
  2846. /* the checksums are LE, so we have to byteswap for >8bpp formats
  2847. * on BE arches */
  2848. #if HAVE_BIGENDIAN
  2849. if (pixel_shift && !s->checksum_buf) {
  2850. av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
  2851. FFMAX3(frame->linesize[0], frame->linesize[1],
  2852. frame->linesize[2]));
  2853. if (!s->checksum_buf)
  2854. return AVERROR(ENOMEM);
  2855. }
  2856. #endif
  2857. msg_buf[0] = '\0';
  2858. for (i = 0; frame->data[i]; i++) {
  2859. int width = s->avctx->coded_width;
  2860. int height = s->avctx->coded_height;
  2861. int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
  2862. int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
  2863. uint8_t md5[16];
  2864. av_md5_init(s->md5_ctx);
  2865. for (j = 0; j < h; j++) {
  2866. const uint8_t *src = frame->data[i] + j * frame->linesize[i];
  2867. #if HAVE_BIGENDIAN
  2868. if (pixel_shift) {
  2869. s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
  2870. (const uint16_t *) src, w);
  2871. src = s->checksum_buf;
  2872. }
  2873. #endif
  2874. av_md5_update(s->md5_ctx, src, w << pixel_shift);
  2875. }
  2876. av_md5_final(s->md5_ctx, md5);
  2877. #define MD5_PRI "%016" PRIx64 "%016" PRIx64
  2878. #define MD5_PRI_ARG(buf) AV_RB64(buf), AV_RB64((const uint8_t*)(buf) + 8)
  2879. if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
  2880. av_strlcatf(msg_buf, sizeof(msg_buf),
  2881. "plane %d - correct " MD5_PRI "; ",
  2882. i, MD5_PRI_ARG(md5));
  2883. } else {
  2884. av_strlcatf(msg_buf, sizeof(msg_buf),
  2885. "mismatching checksum of plane %d - " MD5_PRI " != " MD5_PRI "; ",
  2886. i, MD5_PRI_ARG(md5), MD5_PRI_ARG(s->sei.picture_hash.md5[i]));
  2887. err = AVERROR_INVALIDDATA;
  2888. }
  2889. }
  2890. av_log(s->avctx, err < 0 ? AV_LOG_ERROR : AV_LOG_DEBUG,
  2891. "Verifying checksum for frame with POC %d: %s\n",
  2892. s->poc, msg_buf);
  2893. return err;
  2894. }
  2895. static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
  2896. {
  2897. int ret, i;
  2898. ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
  2899. &s->nal_length_size, s->avctx->err_recognition,
  2900. s->apply_defdispwin, s->avctx);
  2901. if (ret < 0)
  2902. return ret;
  2903. /* export stream parameters from the first SPS */
  2904. for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
  2905. if (first && s->ps.sps_list[i]) {
  2906. const HEVCSPS *sps = s->ps.sps_list[i];
  2907. export_stream_params(s, sps);
  2908. break;
  2909. }
  2910. }
  2911. /* export stream parameters from SEI */
  2912. ret = export_stream_params_from_sei(s);
  2913. if (ret < 0)
  2914. return ret;
  2915. return 0;
  2916. }
  2917. static int hevc_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
  2918. int *got_output, AVPacket *avpkt)
  2919. {
  2920. int ret;
  2921. uint8_t *sd;
  2922. size_t sd_size;
  2923. HEVCContext *s = avctx->priv_data;
  2924. if (!avpkt->size) {
  2925. ret = ff_hevc_output_frame(s, rframe, 1);
  2926. if (ret < 0)
  2927. return ret;
  2928. *got_output = ret;
  2929. return 0;
  2930. }
  2931. sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &sd_size);
  2932. if (sd && sd_size > 0) {
  2933. ret = hevc_decode_extradata(s, sd, sd_size, 0);
  2934. if (ret < 0)
  2935. return ret;
  2936. }
  2937. sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_DOVI_CONF, &sd_size);
  2938. if (sd && sd_size > 0) {
  2939. int old = s->dovi_ctx.dv_profile;
  2940. ff_dovi_update_cfg(&s->dovi_ctx, (AVDOVIDecoderConfigurationRecord *) sd);
  2941. if (old)
  2942. av_log(avctx, AV_LOG_DEBUG,
  2943. "New DOVI configuration record from input packet (profile %d -> %u).\n",
  2944. old, s->dovi_ctx.dv_profile);
  2945. }
  2946. s->ref = s->collocated_ref = NULL;
  2947. ret = decode_nal_units(s, avpkt->data, avpkt->size);
  2948. if (ret < 0)
  2949. return ret;
  2950. if (avctx->hwaccel) {
  2951. if (s->ref && (ret = FF_HW_SIMPLE_CALL(avctx, end_frame)) < 0) {
  2952. av_log(avctx, AV_LOG_ERROR,
  2953. "hardware accelerator failed to decode picture\n");
  2954. ff_hevc_unref_frame(s->ref, ~0);
  2955. return ret;
  2956. }
  2957. } else {
  2958. /* verify the SEI checksum */
  2959. if (avctx->err_recognition & AV_EF_CRCCHECK && s->ref && s->is_decoded &&
  2960. s->sei.picture_hash.is_md5) {
  2961. ret = verify_md5(s, s->ref->frame);
  2962. if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
  2963. ff_hevc_unref_frame(s->ref, ~0);
  2964. return ret;
  2965. }
  2966. }
  2967. }
  2968. s->sei.picture_hash.is_md5 = 0;
  2969. if (s->is_decoded) {
  2970. av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
  2971. s->is_decoded = 0;
  2972. }
  2973. if (s->output_frame->buf[0]) {
  2974. av_frame_move_ref(rframe, s->output_frame);
  2975. *got_output = 1;
  2976. }
  2977. return avpkt->size;
  2978. }
  2979. static int hevc_ref_frame(HEVCFrame *dst, HEVCFrame *src)
  2980. {
  2981. int ret;
  2982. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  2983. if (ret < 0)
  2984. return ret;
  2985. if (src->needs_fg) {
  2986. ret = av_frame_ref(dst->frame_grain, src->frame_grain);
  2987. if (ret < 0)
  2988. return ret;
  2989. dst->needs_fg = 1;
  2990. }
  2991. dst->tab_mvf = ff_refstruct_ref(src->tab_mvf);
  2992. dst->rpl_tab = ff_refstruct_ref(src->rpl_tab);
  2993. dst->rpl = ff_refstruct_ref(src->rpl);
  2994. dst->nb_rpl_elems = src->nb_rpl_elems;
  2995. dst->poc = src->poc;
  2996. dst->ctb_count = src->ctb_count;
  2997. dst->flags = src->flags;
  2998. dst->sequence = src->sequence;
  2999. ff_refstruct_replace(&dst->hwaccel_picture_private,
  3000. src->hwaccel_picture_private);
  3001. return 0;
  3002. }
  3003. static av_cold int hevc_decode_free(AVCodecContext *avctx)
  3004. {
  3005. HEVCContext *s = avctx->priv_data;
  3006. int i;
  3007. pic_arrays_free(s);
  3008. ff_dovi_ctx_unref(&s->dovi_ctx);
  3009. av_buffer_unref(&s->rpu_buf);
  3010. av_freep(&s->md5_ctx);
  3011. for (i = 0; i < 3; i++) {
  3012. av_freep(&s->sao_pixel_buffer_h[i]);
  3013. av_freep(&s->sao_pixel_buffer_v[i]);
  3014. }
  3015. av_frame_free(&s->output_frame);
  3016. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  3017. ff_hevc_unref_frame(&s->DPB[i], ~0);
  3018. av_frame_free(&s->DPB[i].frame);
  3019. av_frame_free(&s->DPB[i].frame_grain);
  3020. }
  3021. ff_hevc_ps_uninit(&s->ps);
  3022. av_freep(&s->sh.entry_point_offset);
  3023. av_freep(&s->sh.offset);
  3024. av_freep(&s->sh.size);
  3025. if (s->HEVClcList) {
  3026. for (i = 1; i < s->threads_number; i++) {
  3027. av_freep(&s->HEVClcList[i]);
  3028. }
  3029. }
  3030. av_freep(&s->HEVClc);
  3031. av_freep(&s->HEVClcList);
  3032. ff_h2645_packet_uninit(&s->pkt);
  3033. ff_hevc_reset_sei(&s->sei);
  3034. return 0;
  3035. }
  3036. static av_cold int hevc_init_context(AVCodecContext *avctx)
  3037. {
  3038. HEVCContext *s = avctx->priv_data;
  3039. int i;
  3040. s->avctx = avctx;
  3041. s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
  3042. s->HEVClcList = av_mallocz(sizeof(HEVCLocalContext*) * s->threads_number);
  3043. if (!s->HEVClc || !s->HEVClcList)
  3044. return AVERROR(ENOMEM);
  3045. s->HEVClc->parent = s;
  3046. s->HEVClc->logctx = avctx;
  3047. s->HEVClc->common_cabac_state = &s->cabac;
  3048. s->HEVClcList[0] = s->HEVClc;
  3049. s->output_frame = av_frame_alloc();
  3050. if (!s->output_frame)
  3051. return AVERROR(ENOMEM);
  3052. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  3053. s->DPB[i].frame = av_frame_alloc();
  3054. if (!s->DPB[i].frame)
  3055. return AVERROR(ENOMEM);
  3056. s->DPB[i].tf.f = s->DPB[i].frame;
  3057. s->DPB[i].frame_grain = av_frame_alloc();
  3058. if (!s->DPB[i].frame_grain)
  3059. return AVERROR(ENOMEM);
  3060. }
  3061. s->max_ra = INT_MAX;
  3062. s->md5_ctx = av_md5_alloc();
  3063. if (!s->md5_ctx)
  3064. return AVERROR(ENOMEM);
  3065. ff_bswapdsp_init(&s->bdsp);
  3066. s->dovi_ctx.logctx = avctx;
  3067. s->eos = 0;
  3068. ff_hevc_reset_sei(&s->sei);
  3069. return 0;
  3070. }
  3071. #if HAVE_THREADS
  3072. static int hevc_update_thread_context(AVCodecContext *dst,
  3073. const AVCodecContext *src)
  3074. {
  3075. HEVCContext *s = dst->priv_data;
  3076. HEVCContext *s0 = src->priv_data;
  3077. int i, ret;
  3078. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  3079. ff_hevc_unref_frame(&s->DPB[i], ~0);
  3080. if (s0->DPB[i].frame->buf[0]) {
  3081. ret = hevc_ref_frame(&s->DPB[i], &s0->DPB[i]);
  3082. if (ret < 0)
  3083. return ret;
  3084. }
  3085. }
  3086. if (s->ps.sps != s0->ps.sps)
  3087. s->ps.sps = NULL;
  3088. for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
  3089. ff_refstruct_replace(&s->ps.vps_list[i], s0->ps.vps_list[i]);
  3090. for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
  3091. ff_refstruct_replace(&s->ps.sps_list[i], s0->ps.sps_list[i]);
  3092. for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
  3093. ff_refstruct_replace(&s->ps.pps_list[i], s0->ps.pps_list[i]);
  3094. if (s->ps.sps != s0->ps.sps)
  3095. if ((ret = set_sps(s, s0->ps.sps, src->pix_fmt)) < 0)
  3096. return ret;
  3097. s->seq_decode = s0->seq_decode;
  3098. s->seq_output = s0->seq_output;
  3099. s->pocTid0 = s0->pocTid0;
  3100. s->max_ra = s0->max_ra;
  3101. s->eos = s0->eos;
  3102. s->no_rasl_output_flag = s0->no_rasl_output_flag;
  3103. s->is_nalff = s0->is_nalff;
  3104. s->nal_length_size = s0->nal_length_size;
  3105. s->threads_number = s0->threads_number;
  3106. s->threads_type = s0->threads_type;
  3107. s->film_grain_warning_shown = s0->film_grain_warning_shown;
  3108. if (s0->eos) {
  3109. s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
  3110. s->max_ra = INT_MAX;
  3111. }
  3112. ret = ff_h2645_sei_ctx_replace(&s->sei.common, &s0->sei.common);
  3113. if (ret < 0)
  3114. return ret;
  3115. ret = av_buffer_replace(&s->sei.common.dynamic_hdr_plus.info,
  3116. s0->sei.common.dynamic_hdr_plus.info);
  3117. if (ret < 0)
  3118. return ret;
  3119. ret = av_buffer_replace(&s->rpu_buf, s0->rpu_buf);
  3120. if (ret < 0)
  3121. return ret;
  3122. ff_dovi_ctx_replace(&s->dovi_ctx, &s0->dovi_ctx);
  3123. ret = av_buffer_replace(&s->sei.common.dynamic_hdr_vivid.info,
  3124. s0->sei.common.dynamic_hdr_vivid.info);
  3125. if (ret < 0)
  3126. return ret;
  3127. s->sei.common.frame_packing = s0->sei.common.frame_packing;
  3128. s->sei.common.display_orientation = s0->sei.common.display_orientation;
  3129. s->sei.common.alternative_transfer = s0->sei.common.alternative_transfer;
  3130. s->sei.common.mastering_display = s0->sei.common.mastering_display;
  3131. s->sei.common.content_light = s0->sei.common.content_light;
  3132. s->sei.common.aom_film_grain = s0->sei.common.aom_film_grain;
  3133. ret = export_stream_params_from_sei(s);
  3134. if (ret < 0)
  3135. return ret;
  3136. return 0;
  3137. }
  3138. #endif
  3139. static av_cold int hevc_decode_init(AVCodecContext *avctx)
  3140. {
  3141. HEVCContext *s = avctx->priv_data;
  3142. int ret;
  3143. if (avctx->active_thread_type & FF_THREAD_SLICE) {
  3144. s->threads_number = avctx->thread_count;
  3145. ret = ff_slice_thread_init_progress(avctx);
  3146. if (ret < 0)
  3147. return ret;
  3148. } else
  3149. s->threads_number = 1;
  3150. if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
  3151. s->threads_type = FF_THREAD_FRAME;
  3152. else
  3153. s->threads_type = FF_THREAD_SLICE;
  3154. ret = hevc_init_context(avctx);
  3155. if (ret < 0)
  3156. return ret;
  3157. s->enable_parallel_tiles = 0;
  3158. s->sei.picture_timing.picture_struct = 0;
  3159. s->eos = 1;
  3160. atomic_init(&s->wpp_err, 0);
  3161. if (!avctx->internal->is_copy) {
  3162. const AVPacketSideData *sd;
  3163. if (avctx->extradata_size > 0 && avctx->extradata) {
  3164. ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
  3165. if (ret < 0) {
  3166. return ret;
  3167. }
  3168. }
  3169. sd = ff_get_coded_side_data(avctx, AV_PKT_DATA_DOVI_CONF);
  3170. if (sd && sd->size > 0)
  3171. ff_dovi_update_cfg(&s->dovi_ctx, (AVDOVIDecoderConfigurationRecord *) sd->data);
  3172. }
  3173. return 0;
  3174. }
  3175. static void hevc_decode_flush(AVCodecContext *avctx)
  3176. {
  3177. HEVCContext *s = avctx->priv_data;
  3178. ff_hevc_flush_dpb(s);
  3179. ff_hevc_reset_sei(&s->sei);
  3180. ff_dovi_ctx_flush(&s->dovi_ctx);
  3181. av_buffer_unref(&s->rpu_buf);
  3182. s->max_ra = INT_MAX;
  3183. s->eos = 1;
  3184. if (FF_HW_HAS_CB(avctx, flush))
  3185. FF_HW_SIMPLE_CALL(avctx, flush);
  3186. }
  3187. #define OFFSET(x) offsetof(HEVCContext, x)
  3188. #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  3189. static const AVOption options[] = {
  3190. { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
  3191. AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
  3192. { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
  3193. AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
  3194. { NULL },
  3195. };
  3196. static const AVClass hevc_decoder_class = {
  3197. .class_name = "HEVC decoder",
  3198. .item_name = av_default_item_name,
  3199. .option = options,
  3200. .version = LIBAVUTIL_VERSION_INT,
  3201. };
  3202. const FFCodec ff_hevc_decoder = {
  3203. .p.name = "hevc",
  3204. CODEC_LONG_NAME("HEVC (High Efficiency Video Coding)"),
  3205. .p.type = AVMEDIA_TYPE_VIDEO,
  3206. .p.id = AV_CODEC_ID_HEVC,
  3207. .priv_data_size = sizeof(HEVCContext),
  3208. .p.priv_class = &hevc_decoder_class,
  3209. .init = hevc_decode_init,
  3210. .close = hevc_decode_free,
  3211. FF_CODEC_DECODE_CB(hevc_decode_frame),
  3212. .flush = hevc_decode_flush,
  3213. UPDATE_THREAD_CONTEXT(hevc_update_thread_context),
  3214. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
  3215. AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
  3216. .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
  3217. FF_CODEC_CAP_ALLOCATE_PROGRESS | FF_CODEC_CAP_INIT_CLEANUP,
  3218. .p.profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
  3219. .hw_configs = (const AVCodecHWConfigInternal *const []) {
  3220. #if CONFIG_HEVC_DXVA2_HWACCEL
  3221. HWACCEL_DXVA2(hevc),
  3222. #endif
  3223. #if CONFIG_HEVC_D3D11VA_HWACCEL
  3224. HWACCEL_D3D11VA(hevc),
  3225. #endif
  3226. #if CONFIG_HEVC_D3D11VA2_HWACCEL
  3227. HWACCEL_D3D11VA2(hevc),
  3228. #endif
  3229. #if CONFIG_HEVC_D3D12VA_HWACCEL
  3230. HWACCEL_D3D12VA(hevc),
  3231. #endif
  3232. #if CONFIG_HEVC_NVDEC_HWACCEL
  3233. HWACCEL_NVDEC(hevc),
  3234. #endif
  3235. #if CONFIG_HEVC_VAAPI_HWACCEL
  3236. HWACCEL_VAAPI(hevc),
  3237. #endif
  3238. #if CONFIG_HEVC_VDPAU_HWACCEL
  3239. HWACCEL_VDPAU(hevc),
  3240. #endif
  3241. #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
  3242. HWACCEL_VIDEOTOOLBOX(hevc),
  3243. #endif
  3244. #if CONFIG_HEVC_VULKAN_HWACCEL
  3245. HWACCEL_VULKAN(hevc),
  3246. #endif
  3247. NULL
  3248. },
  3249. };