hevcdec.c 158 KB

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