h264.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488
  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "libavutil/imgutils.h"
  27. #include "internal.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h264.h"
  32. #include "h264data.h"
  33. #include "h264_mvpred.h"
  34. #include "golomb.h"
  35. #include "mathops.h"
  36. #include "rectangle.h"
  37. #include "thread.h"
  38. #include "vdpau_internal.h"
  39. #include "libavutil/avassert.h"
  40. #include "cabac.h"
  41. //#undef NDEBUG
  42. #include <assert.h>
  43. static const uint8_t rem6[QP_MAX_NUM+1]={
  44. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  45. };
  46. static const uint8_t div6[QP_MAX_NUM+1]={
  47. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,10,10,10,10,
  48. };
  49. static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
  50. PIX_FMT_DXVA2_VLD,
  51. PIX_FMT_VAAPI_VLD,
  52. PIX_FMT_YUVJ420P,
  53. PIX_FMT_NONE
  54. };
  55. void ff_h264_write_back_intra_pred_mode(H264Context *h){
  56. int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
  57. AV_COPY32(mode, h->intra4x4_pred_mode_cache + 4 + 8*4);
  58. mode[4]= h->intra4x4_pred_mode_cache[7+8*3];
  59. mode[5]= h->intra4x4_pred_mode_cache[7+8*2];
  60. mode[6]= h->intra4x4_pred_mode_cache[7+8*1];
  61. }
  62. /**
  63. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  64. */
  65. int ff_h264_check_intra4x4_pred_mode(H264Context *h){
  66. MpegEncContext * const s = &h->s;
  67. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  68. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  69. int i;
  70. if(!(h->top_samples_available&0x8000)){
  71. for(i=0; i<4; i++){
  72. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  73. if(status<0){
  74. av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  75. return -1;
  76. } else if(status){
  77. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  78. }
  79. }
  80. }
  81. if((h->left_samples_available&0x8888)!=0x8888){
  82. static const int mask[4]={0x8000,0x2000,0x80,0x20};
  83. for(i=0; i<4; i++){
  84. if(!(h->left_samples_available&mask[i])){
  85. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  86. if(status<0){
  87. av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  88. return -1;
  89. } else if(status){
  90. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  91. }
  92. }
  93. }
  94. }
  95. return 0;
  96. } //FIXME cleanup like ff_h264_check_intra_pred_mode
  97. /**
  98. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  99. */
  100. int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
  101. MpegEncContext * const s = &h->s;
  102. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  103. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  104. if(mode > 6U) {
  105. av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
  106. return -1;
  107. }
  108. if(!(h->top_samples_available&0x8000)){
  109. mode= top[ mode ];
  110. if(mode<0){
  111. av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  112. return -1;
  113. }
  114. }
  115. if((h->left_samples_available&0x8080) != 0x8080){
  116. mode= left[ mode ];
  117. if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
  118. mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
  119. }
  120. if(mode<0){
  121. av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  122. return -1;
  123. }
  124. }
  125. return mode;
  126. }
  127. const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
  128. int i, si, di;
  129. uint8_t *dst;
  130. int bufidx;
  131. // src[0]&0x80; //forbidden bit
  132. h->nal_ref_idc= src[0]>>5;
  133. h->nal_unit_type= src[0]&0x1F;
  134. src++; length--;
  135. #if HAVE_FAST_UNALIGNED
  136. # if HAVE_FAST_64BIT
  137. # define RS 7
  138. for(i=0; i+1<length; i+=9){
  139. if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
  140. # else
  141. # define RS 3
  142. for(i=0; i+1<length; i+=5){
  143. if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
  144. # endif
  145. continue;
  146. if(i>0 && !src[i]) i--;
  147. while(src[i]) i++;
  148. #else
  149. # define RS 0
  150. for(i=0; i+1<length; i+=2){
  151. if(src[i]) continue;
  152. if(i>0 && src[i-1]==0) i--;
  153. #endif
  154. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  155. if(src[i+2]!=3){
  156. /* startcode, so we must be past the end */
  157. length=i;
  158. }
  159. break;
  160. }
  161. i-= RS;
  162. }
  163. if(i>=length-1){ //no escaped 0
  164. *dst_length= length;
  165. *consumed= length+1; //+1 for the header
  166. return src;
  167. }
  168. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
  169. av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
  170. dst= h->rbsp_buffer[bufidx];
  171. if (dst == NULL){
  172. return NULL;
  173. }
  174. //printf("decoding esc\n");
  175. memcpy(dst, src, i);
  176. si=di=i;
  177. while(si+2<length){
  178. //remove escapes (very rare 1:2^22)
  179. if(src[si+2]>3){
  180. dst[di++]= src[si++];
  181. dst[di++]= src[si++];
  182. }else if(src[si]==0 && src[si+1]==0){
  183. if(src[si+2]==3){ //escape
  184. dst[di++]= 0;
  185. dst[di++]= 0;
  186. si+=3;
  187. continue;
  188. }else //next start code
  189. goto nsc;
  190. }
  191. dst[di++]= src[si++];
  192. }
  193. while(si<length)
  194. dst[di++]= src[si++];
  195. nsc:
  196. memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  197. *dst_length= di;
  198. *consumed= si + 1;//+1 for the header
  199. //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
  200. return dst;
  201. }
  202. /**
  203. * Identify the exact end of the bitstream
  204. * @return the length of the trailing, or 0 if damaged
  205. */
  206. static int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
  207. int v= *src;
  208. int r;
  209. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  210. for(r=1; r<9; r++){
  211. if(v&1) return r;
  212. v>>=1;
  213. }
  214. return 0;
  215. }
  216. #if 0
  217. /**
  218. * DCT transforms the 16 dc values.
  219. * @param qp quantization parameter ??? FIXME
  220. */
  221. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  222. // const int qmul= dequant_coeff[qp][0];
  223. int i;
  224. int temp[16]; //FIXME check if this is a good idea
  225. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  226. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  227. for(i=0; i<4; i++){
  228. const int offset= y_offset[i];
  229. const int z0= block[offset+stride*0] + block[offset+stride*4];
  230. const int z1= block[offset+stride*0] - block[offset+stride*4];
  231. const int z2= block[offset+stride*1] - block[offset+stride*5];
  232. const int z3= block[offset+stride*1] + block[offset+stride*5];
  233. temp[4*i+0]= z0+z3;
  234. temp[4*i+1]= z1+z2;
  235. temp[4*i+2]= z1-z2;
  236. temp[4*i+3]= z0-z3;
  237. }
  238. for(i=0; i<4; i++){
  239. const int offset= x_offset[i];
  240. const int z0= temp[4*0+i] + temp[4*2+i];
  241. const int z1= temp[4*0+i] - temp[4*2+i];
  242. const int z2= temp[4*1+i] - temp[4*3+i];
  243. const int z3= temp[4*1+i] + temp[4*3+i];
  244. block[stride*0 +offset]= (z0 + z3)>>1;
  245. block[stride*2 +offset]= (z1 + z2)>>1;
  246. block[stride*8 +offset]= (z1 - z2)>>1;
  247. block[stride*10+offset]= (z0 - z3)>>1;
  248. }
  249. }
  250. #endif
  251. #undef xStride
  252. #undef stride
  253. #if 0
  254. static void chroma_dc_dct_c(DCTELEM *block){
  255. const int stride= 16*2;
  256. const int xStride= 16;
  257. int a,b,c,d,e;
  258. a= block[stride*0 + xStride*0];
  259. b= block[stride*0 + xStride*1];
  260. c= block[stride*1 + xStride*0];
  261. d= block[stride*1 + xStride*1];
  262. e= a-b;
  263. a= a+b;
  264. b= c-d;
  265. c= c+d;
  266. block[stride*0 + xStride*0]= (a+c);
  267. block[stride*0 + xStride*1]= (e+b);
  268. block[stride*1 + xStride*0]= (a-c);
  269. block[stride*1 + xStride*1]= (e-b);
  270. }
  271. #endif
  272. static void free_tables(H264Context *h, int free_rbsp){
  273. int i;
  274. H264Context *hx;
  275. av_freep(&h->intra4x4_pred_mode);
  276. av_freep(&h->chroma_pred_mode_table);
  277. av_freep(&h->cbp_table);
  278. av_freep(&h->mvd_table[0]);
  279. av_freep(&h->mvd_table[1]);
  280. av_freep(&h->direct_table);
  281. av_freep(&h->non_zero_count);
  282. av_freep(&h->slice_table_base);
  283. h->slice_table= NULL;
  284. av_freep(&h->list_counts);
  285. av_freep(&h->mb2b_xy);
  286. av_freep(&h->mb2br_xy);
  287. for(i = 0; i < MAX_THREADS; i++) {
  288. hx = h->thread_context[i];
  289. if(!hx) continue;
  290. av_freep(&hx->top_borders[1]);
  291. av_freep(&hx->top_borders[0]);
  292. av_freep(&hx->s.obmc_scratchpad);
  293. if (free_rbsp){
  294. av_freep(&hx->rbsp_buffer[1]);
  295. av_freep(&hx->rbsp_buffer[0]);
  296. hx->rbsp_buffer_size[0] = 0;
  297. hx->rbsp_buffer_size[1] = 0;
  298. }
  299. if (i) av_freep(&h->thread_context[i]);
  300. }
  301. }
  302. static void init_dequant8_coeff_table(H264Context *h){
  303. int i,q,x;
  304. const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
  305. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  306. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  307. for(i=0; i<2; i++ ){
  308. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  309. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  310. break;
  311. }
  312. for(q=0; q<max_qp+1; q++){
  313. int shift = div6[q];
  314. int idx = rem6[q];
  315. for(x=0; x<64; x++)
  316. h->dequant8_coeff[i][q][(x>>3)|((x&7)<<3)] =
  317. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  318. h->pps.scaling_matrix8[i][x]) << shift;
  319. }
  320. }
  321. }
  322. static void init_dequant4_coeff_table(H264Context *h){
  323. int i,j,q,x;
  324. const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
  325. for(i=0; i<6; i++ ){
  326. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  327. for(j=0; j<i; j++){
  328. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  329. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  330. break;
  331. }
  332. }
  333. if(j<i)
  334. continue;
  335. for(q=0; q<max_qp+1; q++){
  336. int shift = div6[q] + 2;
  337. int idx = rem6[q];
  338. for(x=0; x<16; x++)
  339. h->dequant4_coeff[i][q][(x>>2)|((x<<2)&0xF)] =
  340. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  341. h->pps.scaling_matrix4[i][x]) << shift;
  342. }
  343. }
  344. }
  345. static void init_dequant_tables(H264Context *h){
  346. int i,x;
  347. init_dequant4_coeff_table(h);
  348. if(h->pps.transform_8x8_mode)
  349. init_dequant8_coeff_table(h);
  350. if(h->sps.transform_bypass){
  351. for(i=0; i<6; i++)
  352. for(x=0; x<16; x++)
  353. h->dequant4_coeff[i][0][x] = 1<<6;
  354. if(h->pps.transform_8x8_mode)
  355. for(i=0; i<2; i++)
  356. for(x=0; x<64; x++)
  357. h->dequant8_coeff[i][0][x] = 1<<6;
  358. }
  359. }
  360. int ff_h264_alloc_tables(H264Context *h){
  361. MpegEncContext * const s = &h->s;
  362. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  363. const int row_mb_num= 2*s->mb_stride*s->avctx->thread_count;
  364. int x,y;
  365. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, row_mb_num * 8 * sizeof(uint8_t), fail)
  366. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count , big_mb_num * 32 * sizeof(uint8_t), fail)
  367. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
  368. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
  369. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
  370. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*row_mb_num * sizeof(uint8_t), fail);
  371. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*row_mb_num * sizeof(uint8_t), fail);
  372. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 4*big_mb_num * sizeof(uint8_t) , fail);
  373. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
  374. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
  375. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  376. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy , big_mb_num * sizeof(uint32_t), fail);
  377. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
  378. for(y=0; y<s->mb_height; y++){
  379. for(x=0; x<s->mb_width; x++){
  380. const int mb_xy= x + y*s->mb_stride;
  381. const int b_xy = 4*x + 4*y*h->b_stride;
  382. h->mb2b_xy [mb_xy]= b_xy;
  383. h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
  384. }
  385. }
  386. s->obmc_scratchpad = NULL;
  387. if(!h->dequant4_coeff[0])
  388. init_dequant_tables(h);
  389. return 0;
  390. fail:
  391. free_tables(h, 1);
  392. return -1;
  393. }
  394. /**
  395. * Mimic alloc_tables(), but for every context thread.
  396. */
  397. static void clone_tables(H264Context *dst, H264Context *src, int i){
  398. MpegEncContext * const s = &src->s;
  399. dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i*8*2*s->mb_stride;
  400. dst->non_zero_count = src->non_zero_count;
  401. dst->slice_table = src->slice_table;
  402. dst->cbp_table = src->cbp_table;
  403. dst->mb2b_xy = src->mb2b_xy;
  404. dst->mb2br_xy = src->mb2br_xy;
  405. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  406. dst->mvd_table[0] = src->mvd_table[0] + i*8*2*s->mb_stride;
  407. dst->mvd_table[1] = src->mvd_table[1] + i*8*2*s->mb_stride;
  408. dst->direct_table = src->direct_table;
  409. dst->list_counts = src->list_counts;
  410. dst->s.obmc_scratchpad = NULL;
  411. ff_h264_pred_init(&dst->hpc, src->s.codec_id, src->sps.bit_depth_luma);
  412. }
  413. /**
  414. * Init context
  415. * Allocate buffers which are not shared amongst multiple threads.
  416. */
  417. static int context_init(H264Context *h){
  418. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t)*2, fail)
  419. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t)*2, fail)
  420. h->ref_cache[0][scan8[5 ]+1] = h->ref_cache[0][scan8[7 ]+1] = h->ref_cache[0][scan8[13]+1] =
  421. h->ref_cache[1][scan8[5 ]+1] = h->ref_cache[1][scan8[7 ]+1] = h->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
  422. return 0;
  423. fail:
  424. return -1; // free_tables will clean up for us
  425. }
  426. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
  427. static av_cold void common_init(H264Context *h){
  428. MpegEncContext * const s = &h->s;
  429. s->width = s->avctx->width;
  430. s->height = s->avctx->height;
  431. s->codec_id= s->avctx->codec->id;
  432. ff_h264dsp_init(&h->h264dsp, 8);
  433. ff_h264_pred_init(&h->hpc, s->codec_id, 8);
  434. h->dequant_coeff_pps= -1;
  435. s->unrestricted_mv=1;
  436. s->decode=1; //FIXME
  437. dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
  438. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  439. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  440. }
  441. int ff_h264_decode_extradata(H264Context *h)
  442. {
  443. AVCodecContext *avctx = h->s.avctx;
  444. if(*(char *)avctx->extradata == 1){
  445. int i, cnt, nalsize;
  446. unsigned char *p = avctx->extradata;
  447. h->is_avc = 1;
  448. if(avctx->extradata_size < 7) {
  449. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  450. return -1;
  451. }
  452. /* sps and pps in the avcC always have length coded with 2 bytes,
  453. so put a fake nal_length_size = 2 while parsing them */
  454. h->nal_length_size = 2;
  455. // Decode sps from avcC
  456. cnt = *(p+5) & 0x1f; // Number of sps
  457. p += 6;
  458. for (i = 0; i < cnt; i++) {
  459. nalsize = AV_RB16(p) + 2;
  460. if(decode_nal_units(h, p, nalsize) < 0) {
  461. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  462. return -1;
  463. }
  464. p += nalsize;
  465. }
  466. // Decode pps from avcC
  467. cnt = *(p++); // Number of pps
  468. for (i = 0; i < cnt; i++) {
  469. nalsize = AV_RB16(p) + 2;
  470. if(decode_nal_units(h, p, nalsize) < 0) {
  471. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  472. return -1;
  473. }
  474. p += nalsize;
  475. }
  476. // Now store right nal length size, that will be use to parse all other nals
  477. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  478. } else {
  479. h->is_avc = 0;
  480. if(decode_nal_units(h, avctx->extradata, avctx->extradata_size) < 0)
  481. return -1;
  482. }
  483. return 0;
  484. }
  485. av_cold int ff_h264_decode_init(AVCodecContext *avctx){
  486. H264Context *h= avctx->priv_data;
  487. MpegEncContext * const s = &h->s;
  488. MPV_decode_defaults(s);
  489. s->avctx = avctx;
  490. common_init(h);
  491. s->out_format = FMT_H264;
  492. s->workaround_bugs= avctx->workaround_bugs;
  493. // set defaults
  494. // s->decode_mb= ff_h263_decode_mb;
  495. s->quarter_sample = 1;
  496. if(!avctx->has_b_frames)
  497. s->low_delay= 1;
  498. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  499. ff_h264_decode_init_vlc();
  500. h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
  501. h->pixel_shift = 0;
  502. h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
  503. h->thread_context[0] = h;
  504. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  505. h->prev_poc_msb= 1<<16;
  506. h->x264_build = -1;
  507. ff_h264_reset_sei(h);
  508. if(avctx->codec_id == CODEC_ID_H264){
  509. if(avctx->ticks_per_frame == 1){
  510. s->avctx->time_base.den *=2;
  511. }
  512. avctx->ticks_per_frame = 2;
  513. }
  514. if(avctx->extradata_size > 0 && avctx->extradata &&
  515. ff_h264_decode_extradata(h))
  516. return -1;
  517. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  518. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  519. s->low_delay = 0;
  520. }
  521. return 0;
  522. }
  523. static void copy_picture_range(Picture **to, Picture **from, int count, MpegEncContext *new_base, MpegEncContext *old_base)
  524. {
  525. int i;
  526. for (i=0; i<count; i++){
  527. to[i] = REBASE_PICTURE(from[i], new_base, old_base);
  528. }
  529. }
  530. static void copy_parameter_set(void **to, void **from, int count, int size)
  531. {
  532. int i;
  533. for (i=0; i<count; i++){
  534. if (to[i] && !from[i]) av_freep(&to[i]);
  535. else if (from[i] && !to[i]) to[i] = av_malloc(size);
  536. if (from[i]) memcpy(to[i], from[i], size);
  537. }
  538. }
  539. static int decode_init_thread_copy(AVCodecContext *avctx){
  540. H264Context *h= avctx->priv_data;
  541. if (!avctx->is_copy) return 0;
  542. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  543. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  544. return 0;
  545. }
  546. #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
  547. static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){
  548. H264Context *h= dst->priv_data, *h1= src->priv_data;
  549. MpegEncContext * const s = &h->s, * const s1 = &h1->s;
  550. int inited = s->context_initialized, err;
  551. int i;
  552. if(dst == src || !s1->context_initialized) return 0;
  553. err = ff_mpeg_update_thread_context(dst, src);
  554. if(err) return err;
  555. //FIXME handle width/height changing
  556. if(!inited){
  557. for(i = 0; i < MAX_SPS_COUNT; i++)
  558. av_freep(h->sps_buffers + i);
  559. for(i = 0; i < MAX_PPS_COUNT; i++)
  560. av_freep(h->pps_buffers + i);
  561. memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc
  562. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  563. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  564. ff_h264_alloc_tables(h);
  565. context_init(h);
  566. for(i=0; i<2; i++){
  567. h->rbsp_buffer[i] = NULL;
  568. h->rbsp_buffer_size[i] = 0;
  569. }
  570. h->thread_context[0] = h;
  571. // frame_start may not be called for the next thread (if it's decoding a bottom field)
  572. // so this has to be allocated here
  573. h->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  574. s->dsp.clear_blocks(h->mb);
  575. }
  576. //extradata/NAL handling
  577. h->is_avc = h1->is_avc;
  578. //SPS/PPS
  579. copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));
  580. h->sps = h1->sps;
  581. copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));
  582. h->pps = h1->pps;
  583. //Dequantization matrices
  584. //FIXME these are big - can they be only copied when PPS changes?
  585. copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
  586. for(i=0; i<6; i++)
  587. h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
  588. for(i=0; i<2; i++)
  589. h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
  590. h->dequant_coeff_pps = h1->dequant_coeff_pps;
  591. //POC timing
  592. copy_fields(h, h1, poc_lsb, redundant_pic_count);
  593. //reference lists
  594. copy_fields(h, h1, ref_count, intra_gb);
  595. copy_fields(h, h1, short_ref, cabac_init_idc);
  596. copy_picture_range(h->short_ref, h1->short_ref, 32, s, s1);
  597. copy_picture_range(h->long_ref, h1->long_ref, 32, s, s1);
  598. copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);
  599. h->last_slice_type = h1->last_slice_type;
  600. if(!s->current_picture_ptr) return 0;
  601. if(!s->dropable) {
  602. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  603. h->prev_poc_msb = h->poc_msb;
  604. h->prev_poc_lsb = h->poc_lsb;
  605. }
  606. h->prev_frame_num_offset= h->frame_num_offset;
  607. h->prev_frame_num = h->frame_num;
  608. h->outputed_poc = h->next_outputed_poc;
  609. return 0;
  610. }
  611. int ff_h264_frame_start(H264Context *h){
  612. MpegEncContext * const s = &h->s;
  613. int i;
  614. const int pixel_shift = h->pixel_shift;
  615. if(MPV_frame_start(s, s->avctx) < 0)
  616. return -1;
  617. ff_er_frame_start(s);
  618. /*
  619. * MPV_frame_start uses pict_type to derive key_frame.
  620. * This is incorrect for H.264; IDR markings must be used.
  621. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  622. * See decode_nal_units().
  623. */
  624. s->current_picture_ptr->key_frame= 0;
  625. s->current_picture_ptr->mmco_reset= 0;
  626. assert(s->linesize && s->uvlinesize);
  627. for(i=0; i<16; i++){
  628. h->block_offset[i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  629. h->block_offset[24+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  630. }
  631. for(i=0; i<4; i++){
  632. h->block_offset[16+i]=
  633. h->block_offset[20+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  634. h->block_offset[24+16+i]=
  635. h->block_offset[24+20+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  636. }
  637. /* can't be in alloc_tables because linesize isn't known there.
  638. * FIXME: redo bipred weight to not require extra buffer? */
  639. for(i = 0; i < s->avctx->thread_count; i++)
  640. if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
  641. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  642. /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
  643. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
  644. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  645. // We mark the current picture as non-reference after allocating it, so
  646. // that if we break out due to an error it can be released automatically
  647. // in the next MPV_frame_start().
  648. // SVQ3 as well as most other codecs have only last/next/current and thus
  649. // get released even with set reference, besides SVQ3 and others do not
  650. // mark frames as reference later "naturally".
  651. if(s->codec_id != CODEC_ID_SVQ3)
  652. s->current_picture_ptr->reference= 0;
  653. s->current_picture_ptr->field_poc[0]=
  654. s->current_picture_ptr->field_poc[1]= INT_MAX;
  655. h->next_output_pic = NULL;
  656. assert(s->current_picture_ptr->long_ref==0);
  657. return 0;
  658. }
  659. /**
  660. * Run setup operations that must be run after slice header decoding.
  661. * This includes finding the next displayed frame.
  662. *
  663. * @param h h264 master context
  664. */
  665. static void decode_postinit(H264Context *h){
  666. MpegEncContext * const s = &h->s;
  667. Picture *out = s->current_picture_ptr;
  668. Picture *cur = s->current_picture_ptr;
  669. int i, pics, out_of_order, out_idx;
  670. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  671. s->current_picture_ptr->pict_type= s->pict_type;
  672. if (h->next_output_pic) return;
  673. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  674. //FIXME this allows the next thread to start once we encounter the first field of a PAFF packet
  675. //This works if the next packet contains the second field. It does not work if both fields are
  676. //in the same packet.
  677. //ff_thread_finish_setup(s->avctx);
  678. return;
  679. }
  680. cur->interlaced_frame = 0;
  681. cur->repeat_pict = 0;
  682. /* Signal interlacing information externally. */
  683. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  684. if(h->sps.pic_struct_present_flag){
  685. switch (h->sei_pic_struct)
  686. {
  687. case SEI_PIC_STRUCT_FRAME:
  688. break;
  689. case SEI_PIC_STRUCT_TOP_FIELD:
  690. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  691. cur->interlaced_frame = 1;
  692. break;
  693. case SEI_PIC_STRUCT_TOP_BOTTOM:
  694. case SEI_PIC_STRUCT_BOTTOM_TOP:
  695. if (FIELD_OR_MBAFF_PICTURE)
  696. cur->interlaced_frame = 1;
  697. else
  698. // try to flag soft telecine progressive
  699. cur->interlaced_frame = h->prev_interlaced_frame;
  700. break;
  701. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  702. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  703. // Signal the possibility of telecined film externally (pic_struct 5,6)
  704. // From these hints, let the applications decide if they apply deinterlacing.
  705. cur->repeat_pict = 1;
  706. break;
  707. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  708. // Force progressive here, as doubling interlaced frame is a bad idea.
  709. cur->repeat_pict = 2;
  710. break;
  711. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  712. cur->repeat_pict = 4;
  713. break;
  714. }
  715. if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  716. cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
  717. }else{
  718. /* Derive interlacing flag from used decoding process. */
  719. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  720. }
  721. h->prev_interlaced_frame = cur->interlaced_frame;
  722. if (cur->field_poc[0] != cur->field_poc[1]){
  723. /* Derive top_field_first from field pocs. */
  724. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  725. }else{
  726. if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
  727. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  728. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  729. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  730. cur->top_field_first = 1;
  731. else
  732. cur->top_field_first = 0;
  733. }else{
  734. /* Most likely progressive */
  735. cur->top_field_first = 0;
  736. }
  737. }
  738. //FIXME do something with unavailable reference frames
  739. /* Sort B-frames into display order */
  740. if(h->sps.bitstream_restriction_flag
  741. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  742. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  743. s->low_delay = 0;
  744. }
  745. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  746. && !h->sps.bitstream_restriction_flag){
  747. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  748. s->low_delay= 0;
  749. }
  750. pics = 0;
  751. while(h->delayed_pic[pics]) pics++;
  752. assert(pics <= MAX_DELAYED_PIC_COUNT);
  753. h->delayed_pic[pics++] = cur;
  754. if(cur->reference == 0)
  755. cur->reference = DELAYED_PIC_REF;
  756. out = h->delayed_pic[0];
  757. out_idx = 0;
  758. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  759. if(h->delayed_pic[i]->poc < out->poc){
  760. out = h->delayed_pic[i];
  761. out_idx = i;
  762. }
  763. if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
  764. h->next_outputed_poc= INT_MIN;
  765. out_of_order = out->poc < h->next_outputed_poc;
  766. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  767. { }
  768. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  769. || (s->low_delay &&
  770. ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2)
  771. || cur->pict_type == AV_PICTURE_TYPE_B)))
  772. {
  773. s->low_delay = 0;
  774. s->avctx->has_b_frames++;
  775. }
  776. if(out_of_order || pics > s->avctx->has_b_frames){
  777. out->reference &= ~DELAYED_PIC_REF;
  778. out->owner2 = s; // for frame threading, the owner must be the second field's thread
  779. // or else the first thread can release the picture and reuse it unsafely
  780. for(i=out_idx; h->delayed_pic[i]; i++)
  781. h->delayed_pic[i] = h->delayed_pic[i+1];
  782. }
  783. if(!out_of_order && pics > s->avctx->has_b_frames){
  784. h->next_output_pic = out;
  785. if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
  786. h->next_outputed_poc = INT_MIN;
  787. } else
  788. h->next_outputed_poc = out->poc;
  789. }else{
  790. av_log(s->avctx, AV_LOG_DEBUG, "no picture\n");
  791. }
  792. ff_thread_finish_setup(s->avctx);
  793. }
  794. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple){
  795. MpegEncContext * const s = &h->s;
  796. uint8_t *top_border;
  797. int top_idx = 1;
  798. const int pixel_shift = h->pixel_shift;
  799. src_y -= linesize;
  800. src_cb -= uvlinesize;
  801. src_cr -= uvlinesize;
  802. if(!simple && FRAME_MBAFF){
  803. if(s->mb_y&1){
  804. if(!MB_MBAFF){
  805. top_border = h->top_borders[0][s->mb_x];
  806. AV_COPY128(top_border, src_y + 15*linesize);
  807. if (pixel_shift)
  808. AV_COPY128(top_border+16, src_y+15*linesize+16);
  809. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  810. if (pixel_shift) {
  811. AV_COPY128(top_border+32, src_cb+7*uvlinesize);
  812. AV_COPY128(top_border+48, src_cr+7*uvlinesize);
  813. } else {
  814. AV_COPY64(top_border+16, src_cb+7*uvlinesize);
  815. AV_COPY64(top_border+24, src_cr+7*uvlinesize);
  816. }
  817. }
  818. }
  819. }else if(MB_MBAFF){
  820. top_idx = 0;
  821. }else
  822. return;
  823. }
  824. top_border = h->top_borders[top_idx][s->mb_x];
  825. // There are two lines saved, the line above the the top macroblock of a pair,
  826. // and the line above the bottom macroblock
  827. AV_COPY128(top_border, src_y + 16*linesize);
  828. if (pixel_shift)
  829. AV_COPY128(top_border+16, src_y+16*linesize+16);
  830. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  831. if (pixel_shift) {
  832. AV_COPY128(top_border+32, src_cb+8*uvlinesize);
  833. AV_COPY128(top_border+48, src_cr+8*uvlinesize);
  834. } else {
  835. AV_COPY64(top_border+16, src_cb+8*uvlinesize);
  836. AV_COPY64(top_border+24, src_cr+8*uvlinesize);
  837. }
  838. }
  839. }
  840. static inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
  841. uint8_t *src_cb, uint8_t *src_cr,
  842. int linesize, int uvlinesize,
  843. int xchg, int simple, int pixel_shift){
  844. MpegEncContext * const s = &h->s;
  845. int deblock_topleft;
  846. int deblock_top;
  847. int top_idx = 1;
  848. uint8_t *top_border_m1;
  849. uint8_t *top_border;
  850. if(!simple && FRAME_MBAFF){
  851. if(s->mb_y&1){
  852. if(!MB_MBAFF)
  853. return;
  854. }else{
  855. top_idx = MB_MBAFF ? 0 : 1;
  856. }
  857. }
  858. if(h->deblocking_filter == 2) {
  859. deblock_topleft = h->slice_table[h->mb_xy - 1 - s->mb_stride] == h->slice_num;
  860. deblock_top = h->top_type;
  861. } else {
  862. deblock_topleft = (s->mb_x > 0);
  863. deblock_top = (s->mb_y > !!MB_FIELD);
  864. }
  865. src_y -= linesize + 1 + pixel_shift;
  866. src_cb -= uvlinesize + 1 + pixel_shift;
  867. src_cr -= uvlinesize + 1 + pixel_shift;
  868. top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
  869. top_border = h->top_borders[top_idx][s->mb_x];
  870. #define XCHG(a,b,xchg)\
  871. if (pixel_shift) {\
  872. if (xchg) {\
  873. AV_SWAP64(b+0,a+0);\
  874. AV_SWAP64(b+8,a+8);\
  875. } else {\
  876. AV_COPY128(b,a); \
  877. }\
  878. } else \
  879. if (xchg) AV_SWAP64(b,a);\
  880. else AV_COPY64(b,a);
  881. if(deblock_top){
  882. if(deblock_topleft){
  883. XCHG(top_border_m1 + (8 << pixel_shift), src_y - (7 << pixel_shift), 1);
  884. }
  885. XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
  886. XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
  887. if(s->mb_x+1 < s->mb_width){
  888. XCHG(h->top_borders[top_idx][s->mb_x+1], src_y + (17 << pixel_shift), 1);
  889. }
  890. }
  891. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  892. if(deblock_top){
  893. if(deblock_topleft){
  894. XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  895. XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  896. }
  897. XCHG(top_border + (16 << pixel_shift), src_cb+1+pixel_shift, 1);
  898. XCHG(top_border + (24 << pixel_shift), src_cr+1+pixel_shift, 1);
  899. }
  900. }
  901. }
  902. static av_always_inline int dctcoef_get(DCTELEM *mb, int high_bit_depth, int index) {
  903. if (high_bit_depth) {
  904. return AV_RN32A(((int32_t*)mb) + index);
  905. } else
  906. return AV_RN16A(mb + index);
  907. }
  908. static av_always_inline void dctcoef_set(DCTELEM *mb, int high_bit_depth, int index, int value) {
  909. if (high_bit_depth) {
  910. AV_WN32A(((int32_t*)mb) + index, value);
  911. } else
  912. AV_WN16A(mb + index, value);
  913. }
  914. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple, int pixel_shift){
  915. MpegEncContext * const s = &h->s;
  916. const int mb_x= s->mb_x;
  917. const int mb_y= s->mb_y;
  918. const int mb_xy= h->mb_xy;
  919. const int mb_type= s->current_picture.mb_type[mb_xy];
  920. uint8_t *dest_y, *dest_cb, *dest_cr;
  921. int linesize, uvlinesize /*dct_offset*/;
  922. int i;
  923. int *block_offset = &h->block_offset[0];
  924. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  925. /* is_h264 should always be true if SVQ3 is disabled. */
  926. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  927. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  928. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  929. dest_y = s->current_picture.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize ) * 16;
  930. dest_cb = s->current_picture.data[1] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
  931. dest_cr = s->current_picture.data[2] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
  932. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
  933. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + (64 << pixel_shift), dest_cr - dest_cb, 2);
  934. h->list_counts[mb_xy]= h->list_count;
  935. if (!simple && MB_FIELD) {
  936. linesize = h->mb_linesize = s->linesize * 2;
  937. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  938. block_offset = &h->block_offset[24];
  939. if(mb_y&1){ //FIXME move out of this function?
  940. dest_y -= s->linesize*15;
  941. dest_cb-= s->uvlinesize*7;
  942. dest_cr-= s->uvlinesize*7;
  943. }
  944. if(FRAME_MBAFF) {
  945. int list;
  946. for(list=0; list<h->list_count; list++){
  947. if(!USES_LIST(mb_type, list))
  948. continue;
  949. if(IS_16X16(mb_type)){
  950. int8_t *ref = &h->ref_cache[list][scan8[0]];
  951. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  952. }else{
  953. for(i=0; i<16; i+=4){
  954. int ref = h->ref_cache[list][scan8[i]];
  955. if(ref >= 0)
  956. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  957. }
  958. }
  959. }
  960. }
  961. } else {
  962. linesize = h->mb_linesize = s->linesize;
  963. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  964. // dct_offset = s->linesize * 16;
  965. }
  966. if (!simple && IS_INTRA_PCM(mb_type)) {
  967. if (pixel_shift) {
  968. const int bit_depth = h->sps.bit_depth_luma;
  969. int j;
  970. GetBitContext gb;
  971. init_get_bits(&gb, (uint8_t*)h->mb, 384*bit_depth);
  972. for (i = 0; i < 16; i++) {
  973. uint16_t *tmp_y = (uint16_t*)(dest_y + i*linesize);
  974. for (j = 0; j < 16; j++)
  975. tmp_y[j] = get_bits(&gb, bit_depth);
  976. }
  977. for (i = 0; i < 8; i++) {
  978. uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
  979. for (j = 0; j < 8; j++)
  980. tmp_cb[j] = get_bits(&gb, bit_depth);
  981. }
  982. for (i = 0; i < 8; i++) {
  983. uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
  984. for (j = 0; j < 8; j++)
  985. tmp_cr[j] = get_bits(&gb, bit_depth);
  986. }
  987. } else {
  988. for (i=0; i<16; i++) {
  989. memcpy(dest_y + i* linesize, h->mb + i*8, 16);
  990. }
  991. for (i=0; i<8; i++) {
  992. memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
  993. memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
  994. }
  995. }
  996. } else {
  997. if(IS_INTRA(mb_type)){
  998. if(h->deblocking_filter)
  999. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple, pixel_shift);
  1000. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1001. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  1002. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  1003. }
  1004. if(IS_INTRA4x4(mb_type)){
  1005. if(simple || !s->encoding){
  1006. if(IS_8x8DCT(mb_type)){
  1007. if(transform_bypass){
  1008. idct_dc_add =
  1009. idct_add = s->dsp.add_pixels8;
  1010. }else{
  1011. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  1012. idct_add = h->h264dsp.h264_idct8_add;
  1013. }
  1014. for(i=0; i<16; i+=4){
  1015. uint8_t * const ptr= dest_y + block_offset[i];
  1016. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1017. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1018. h->hpc.pred8x8l_add[dir](ptr, h->mb + (i*16 << pixel_shift), linesize);
  1019. }else{
  1020. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  1021. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  1022. (h->topright_samples_available<<i)&0x4000, linesize);
  1023. if(nnz){
  1024. if(nnz == 1 && dctcoef_get(h->mb, pixel_shift, i*16))
  1025. idct_dc_add(ptr, h->mb + (i*16 << pixel_shift), linesize);
  1026. else
  1027. idct_add (ptr, h->mb + (i*16 << pixel_shift), linesize);
  1028. }
  1029. }
  1030. }
  1031. }else{
  1032. if(transform_bypass){
  1033. idct_dc_add =
  1034. idct_add = s->dsp.add_pixels4;
  1035. }else{
  1036. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  1037. idct_add = h->h264dsp.h264_idct_add;
  1038. }
  1039. for(i=0; i<16; i++){
  1040. uint8_t * const ptr= dest_y + block_offset[i];
  1041. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1042. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1043. h->hpc.pred4x4_add[dir](ptr, h->mb + (i*16 << pixel_shift), linesize);
  1044. }else{
  1045. uint8_t *topright;
  1046. int nnz, tr;
  1047. uint64_t tr_high;
  1048. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  1049. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1050. assert(mb_y || linesize <= block_offset[i]);
  1051. if(!topright_avail){
  1052. if (pixel_shift) {
  1053. tr_high= ((uint16_t*)ptr)[3 - linesize/2]*0x0001000100010001ULL;
  1054. topright= (uint8_t*) &tr_high;
  1055. } else {
  1056. tr= ptr[3 - linesize]*0x01010101;
  1057. topright= (uint8_t*) &tr;
  1058. }
  1059. }else
  1060. topright= ptr + (4 << pixel_shift) - linesize;
  1061. }else
  1062. topright= NULL;
  1063. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  1064. nnz = h->non_zero_count_cache[ scan8[i] ];
  1065. if(nnz){
  1066. if(is_h264){
  1067. if(nnz == 1 && dctcoef_get(h->mb, pixel_shift, i*16))
  1068. idct_dc_add(ptr, h->mb + (i*16 << pixel_shift), linesize);
  1069. else
  1070. idct_add (ptr, h->mb + (i*16<<pixel_shift), linesize);
  1071. }
  1072. #if CONFIG_SVQ3_DECODER
  1073. else
  1074. ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  1075. #endif
  1076. }
  1077. }
  1078. }
  1079. }
  1080. }
  1081. }else{
  1082. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1083. if(is_h264){
  1084. if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX] ]){
  1085. if(!transform_bypass)
  1086. h->h264dsp.h264_luma_dc_dequant_idct(h->mb, h->mb_luma_dc, h->dequant4_coeff[0][s->qscale][0]);
  1087. else{
  1088. static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
  1089. 8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
  1090. for(i = 0; i < 16; i++)
  1091. dctcoef_set(h->mb, pixel_shift, dc_mapping[i], dctcoef_get(h->mb_luma_dc, pixel_shift, i));
  1092. }
  1093. }
  1094. }
  1095. #if CONFIG_SVQ3_DECODER
  1096. else
  1097. ff_svq3_luma_dc_dequant_idct_c(h->mb, h->mb_luma_dc, s->qscale);
  1098. #endif
  1099. }
  1100. if(h->deblocking_filter)
  1101. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple, pixel_shift);
  1102. }else if(is_h264){
  1103. ff_hl_motion(h, dest_y, dest_cb, dest_cr,
  1104. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1105. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1106. h->h264dsp.weight_h264_pixels_tab, h->h264dsp.biweight_h264_pixels_tab);
  1107. }
  1108. if(!IS_INTRA4x4(mb_type)){
  1109. if(is_h264){
  1110. if(IS_INTRA16x16(mb_type)){
  1111. if(transform_bypass){
  1112. if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
  1113. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
  1114. }else{
  1115. for(i=0; i<16; i++){
  1116. if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h->mb, pixel_shift, i*16))
  1117. s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + (i*16 << pixel_shift), linesize);
  1118. }
  1119. }
  1120. }else{
  1121. h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1122. }
  1123. }else if(h->cbp&15){
  1124. if(transform_bypass){
  1125. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1126. idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  1127. for(i=0; i<16; i+=di){
  1128. if(h->non_zero_count_cache[ scan8[i] ]){
  1129. idct_add(dest_y + block_offset[i], h->mb + (i*16 << pixel_shift), linesize);
  1130. }
  1131. }
  1132. }else{
  1133. if(IS_8x8DCT(mb_type)){
  1134. h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1135. }else{
  1136. h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1137. }
  1138. }
  1139. }
  1140. }
  1141. #if CONFIG_SVQ3_DECODER
  1142. else{
  1143. for(i=0; i<16; i++){
  1144. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1145. uint8_t * const ptr= dest_y + block_offset[i];
  1146. ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1147. }
  1148. }
  1149. }
  1150. #endif
  1151. }
  1152. if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
  1153. uint8_t *dest[2] = {dest_cb, dest_cr};
  1154. if(transform_bypass){
  1155. if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
  1156. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + (16*16 << pixel_shift), uvlinesize);
  1157. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + (20*16 << pixel_shift), uvlinesize);
  1158. }else{
  1159. idct_add = s->dsp.add_pixels4;
  1160. for(i=16; i<16+8; i++){
  1161. if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h->mb, pixel_shift, i*16))
  1162. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + (i*16 << pixel_shift), uvlinesize);
  1163. }
  1164. }
  1165. }else{
  1166. if(is_h264){
  1167. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
  1168. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16 << pixel_shift) , h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
  1169. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
  1170. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + ((16*16+4*16) << pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
  1171. h->h264dsp.h264_idct_add8(dest, block_offset,
  1172. h->mb, uvlinesize,
  1173. h->non_zero_count_cache);
  1174. }
  1175. #if CONFIG_SVQ3_DECODER
  1176. else{
  1177. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16 , h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
  1178. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+4*16, h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
  1179. for(i=16; i<16+8; i++){
  1180. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1181. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  1182. ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
  1183. }
  1184. }
  1185. }
  1186. #endif
  1187. }
  1188. }
  1189. }
  1190. if(h->cbp || IS_INTRA(mb_type))
  1191. s->dsp.clear_blocks(h->mb);
  1192. }
  1193. /**
  1194. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  1195. */
  1196. #define hl_decode_mb_simple(sh, bits) \
  1197. static void hl_decode_mb_simple_ ## bits(H264Context *h){ \
  1198. hl_decode_mb_internal(h, 1, sh); \
  1199. }
  1200. hl_decode_mb_simple(0, 8);
  1201. hl_decode_mb_simple(1, 16);
  1202. /**
  1203. * Process a macroblock; this handles edge cases, such as interlacing.
  1204. */
  1205. static void av_noinline hl_decode_mb_complex(H264Context *h){
  1206. hl_decode_mb_internal(h, 0, h->pixel_shift);
  1207. }
  1208. void ff_h264_hl_decode_mb(H264Context *h){
  1209. MpegEncContext * const s = &h->s;
  1210. const int mb_xy= h->mb_xy;
  1211. const int mb_type= s->current_picture.mb_type[mb_xy];
  1212. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  1213. if (is_complex) {
  1214. hl_decode_mb_complex(h);
  1215. } else if (h->pixel_shift) {
  1216. hl_decode_mb_simple_16(h);
  1217. } else
  1218. hl_decode_mb_simple_8(h);
  1219. }
  1220. static int pred_weight_table(H264Context *h){
  1221. MpegEncContext * const s = &h->s;
  1222. int list, i;
  1223. int luma_def, chroma_def;
  1224. h->use_weight= 0;
  1225. h->use_weight_chroma= 0;
  1226. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  1227. if(CHROMA)
  1228. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  1229. luma_def = 1<<h->luma_log2_weight_denom;
  1230. chroma_def = 1<<h->chroma_log2_weight_denom;
  1231. for(list=0; list<2; list++){
  1232. h->luma_weight_flag[list] = 0;
  1233. h->chroma_weight_flag[list] = 0;
  1234. for(i=0; i<h->ref_count[list]; i++){
  1235. int luma_weight_flag, chroma_weight_flag;
  1236. luma_weight_flag= get_bits1(&s->gb);
  1237. if(luma_weight_flag){
  1238. h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
  1239. h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
  1240. if( h->luma_weight[i][list][0] != luma_def
  1241. || h->luma_weight[i][list][1] != 0) {
  1242. h->use_weight= 1;
  1243. h->luma_weight_flag[list]= 1;
  1244. }
  1245. }else{
  1246. h->luma_weight[i][list][0]= luma_def;
  1247. h->luma_weight[i][list][1]= 0;
  1248. }
  1249. if(CHROMA){
  1250. chroma_weight_flag= get_bits1(&s->gb);
  1251. if(chroma_weight_flag){
  1252. int j;
  1253. for(j=0; j<2; j++){
  1254. h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
  1255. h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
  1256. if( h->chroma_weight[i][list][j][0] != chroma_def
  1257. || h->chroma_weight[i][list][j][1] != 0) {
  1258. h->use_weight_chroma= 1;
  1259. h->chroma_weight_flag[list]= 1;
  1260. }
  1261. }
  1262. }else{
  1263. int j;
  1264. for(j=0; j<2; j++){
  1265. h->chroma_weight[i][list][j][0]= chroma_def;
  1266. h->chroma_weight[i][list][j][1]= 0;
  1267. }
  1268. }
  1269. }
  1270. }
  1271. if(h->slice_type_nos != AV_PICTURE_TYPE_B) break;
  1272. }
  1273. h->use_weight= h->use_weight || h->use_weight_chroma;
  1274. return 0;
  1275. }
  1276. /**
  1277. * Initialize implicit_weight table.
  1278. * @param field 0/1 initialize the weight for interlaced MBAFF
  1279. * -1 initializes the rest
  1280. */
  1281. static void implicit_weight_table(H264Context *h, int field){
  1282. MpegEncContext * const s = &h->s;
  1283. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  1284. for (i = 0; i < 2; i++) {
  1285. h->luma_weight_flag[i] = 0;
  1286. h->chroma_weight_flag[i] = 0;
  1287. }
  1288. if(field < 0){
  1289. cur_poc = s->current_picture_ptr->poc;
  1290. if( h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
  1291. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  1292. h->use_weight= 0;
  1293. h->use_weight_chroma= 0;
  1294. return;
  1295. }
  1296. ref_start= 0;
  1297. ref_count0= h->ref_count[0];
  1298. ref_count1= h->ref_count[1];
  1299. }else{
  1300. cur_poc = s->current_picture_ptr->field_poc[field];
  1301. ref_start= 16;
  1302. ref_count0= 16+2*h->ref_count[0];
  1303. ref_count1= 16+2*h->ref_count[1];
  1304. }
  1305. h->use_weight= 2;
  1306. h->use_weight_chroma= 2;
  1307. h->luma_log2_weight_denom= 5;
  1308. h->chroma_log2_weight_denom= 5;
  1309. for(ref0=ref_start; ref0 < ref_count0; ref0++){
  1310. int poc0 = h->ref_list[0][ref0].poc;
  1311. for(ref1=ref_start; ref1 < ref_count1; ref1++){
  1312. int poc1 = h->ref_list[1][ref1].poc;
  1313. int td = av_clip(poc1 - poc0, -128, 127);
  1314. int w= 32;
  1315. if(td){
  1316. int tb = av_clip(cur_poc - poc0, -128, 127);
  1317. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1318. int dist_scale_factor = (tb*tx + 32) >> 8;
  1319. if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
  1320. w = 64 - dist_scale_factor;
  1321. }
  1322. if(field<0){
  1323. h->implicit_weight[ref0][ref1][0]=
  1324. h->implicit_weight[ref0][ref1][1]= w;
  1325. }else{
  1326. h->implicit_weight[ref0][ref1][field]=w;
  1327. }
  1328. }
  1329. }
  1330. }
  1331. /**
  1332. * instantaneous decoder refresh.
  1333. */
  1334. static void idr(H264Context *h){
  1335. ff_h264_remove_all_refs(h);
  1336. h->prev_frame_num= 0;
  1337. h->prev_frame_num_offset= 0;
  1338. h->prev_poc_msb=
  1339. h->prev_poc_lsb= 0;
  1340. }
  1341. /* forget old pics after a seek */
  1342. static void flush_dpb(AVCodecContext *avctx){
  1343. H264Context *h= avctx->priv_data;
  1344. int i;
  1345. for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
  1346. if(h->delayed_pic[i])
  1347. h->delayed_pic[i]->reference= 0;
  1348. h->delayed_pic[i]= NULL;
  1349. }
  1350. h->outputed_poc=h->next_outputed_poc= INT_MIN;
  1351. h->prev_interlaced_frame = 1;
  1352. idr(h);
  1353. if(h->s.current_picture_ptr)
  1354. h->s.current_picture_ptr->reference= 0;
  1355. h->s.first_field= 0;
  1356. ff_h264_reset_sei(h);
  1357. ff_mpeg_flush(avctx);
  1358. }
  1359. static int init_poc(H264Context *h){
  1360. MpegEncContext * const s = &h->s;
  1361. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  1362. int field_poc[2];
  1363. Picture *cur = s->current_picture_ptr;
  1364. h->frame_num_offset= h->prev_frame_num_offset;
  1365. if(h->frame_num < h->prev_frame_num)
  1366. h->frame_num_offset += max_frame_num;
  1367. if(h->sps.poc_type==0){
  1368. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  1369. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  1370. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  1371. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  1372. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  1373. else
  1374. h->poc_msb = h->prev_poc_msb;
  1375. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  1376. field_poc[0] =
  1377. field_poc[1] = h->poc_msb + h->poc_lsb;
  1378. if(s->picture_structure == PICT_FRAME)
  1379. field_poc[1] += h->delta_poc_bottom;
  1380. }else if(h->sps.poc_type==1){
  1381. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  1382. int i;
  1383. if(h->sps.poc_cycle_length != 0)
  1384. abs_frame_num = h->frame_num_offset + h->frame_num;
  1385. else
  1386. abs_frame_num = 0;
  1387. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  1388. abs_frame_num--;
  1389. expected_delta_per_poc_cycle = 0;
  1390. for(i=0; i < h->sps.poc_cycle_length; i++)
  1391. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  1392. if(abs_frame_num > 0){
  1393. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  1394. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  1395. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  1396. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  1397. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  1398. } else
  1399. expectedpoc = 0;
  1400. if(h->nal_ref_idc == 0)
  1401. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  1402. field_poc[0] = expectedpoc + h->delta_poc[0];
  1403. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  1404. if(s->picture_structure == PICT_FRAME)
  1405. field_poc[1] += h->delta_poc[1];
  1406. }else{
  1407. int poc= 2*(h->frame_num_offset + h->frame_num);
  1408. if(!h->nal_ref_idc)
  1409. poc--;
  1410. field_poc[0]= poc;
  1411. field_poc[1]= poc;
  1412. }
  1413. if(s->picture_structure != PICT_BOTTOM_FIELD)
  1414. s->current_picture_ptr->field_poc[0]= field_poc[0];
  1415. if(s->picture_structure != PICT_TOP_FIELD)
  1416. s->current_picture_ptr->field_poc[1]= field_poc[1];
  1417. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  1418. return 0;
  1419. }
  1420. /**
  1421. * initialize scan tables
  1422. */
  1423. static void init_scan_tables(H264Context *h){
  1424. int i;
  1425. for(i=0; i<16; i++){
  1426. #define T(x) (x>>2) | ((x<<2) & 0xF)
  1427. h->zigzag_scan[i] = T(zigzag_scan[i]);
  1428. h-> field_scan[i] = T( field_scan[i]);
  1429. #undef T
  1430. }
  1431. for(i=0; i<64; i++){
  1432. #define T(x) (x>>3) | ((x&7)<<3)
  1433. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  1434. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  1435. h->field_scan8x8[i] = T(field_scan8x8[i]);
  1436. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  1437. #undef T
  1438. }
  1439. if(h->sps.transform_bypass){ //FIXME same ugly
  1440. h->zigzag_scan_q0 = zigzag_scan;
  1441. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  1442. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  1443. h->field_scan_q0 = field_scan;
  1444. h->field_scan8x8_q0 = field_scan8x8;
  1445. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  1446. }else{
  1447. h->zigzag_scan_q0 = h->zigzag_scan;
  1448. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  1449. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  1450. h->field_scan_q0 = h->field_scan;
  1451. h->field_scan8x8_q0 = h->field_scan8x8;
  1452. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  1453. }
  1454. }
  1455. static void field_end(H264Context *h, int in_setup){
  1456. MpegEncContext * const s = &h->s;
  1457. AVCodecContext * const avctx= s->avctx;
  1458. s->mb_y= 0;
  1459. if (!in_setup && !s->dropable)
  1460. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, (16*s->mb_height >> FIELD_PICTURE) - 1,
  1461. s->picture_structure==PICT_BOTTOM_FIELD);
  1462. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1463. ff_vdpau_h264_set_reference_frames(s);
  1464. if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){
  1465. if(!s->dropable) {
  1466. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1467. h->prev_poc_msb= h->poc_msb;
  1468. h->prev_poc_lsb= h->poc_lsb;
  1469. }
  1470. h->prev_frame_num_offset= h->frame_num_offset;
  1471. h->prev_frame_num= h->frame_num;
  1472. h->outputed_poc = h->next_outputed_poc;
  1473. }
  1474. if (avctx->hwaccel) {
  1475. if (avctx->hwaccel->end_frame(avctx) < 0)
  1476. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1477. }
  1478. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1479. ff_vdpau_h264_picture_complete(s);
  1480. /*
  1481. * FIXME: Error handling code does not seem to support interlaced
  1482. * when slices span multiple rows
  1483. * The ff_er_add_slice calls don't work right for bottom
  1484. * fields; they cause massive erroneous error concealing
  1485. * Error marking covers both fields (top and bottom).
  1486. * This causes a mismatched s->error_count
  1487. * and a bad error table. Further, the error count goes to
  1488. * INT_MAX when called for bottom field, because mb_y is
  1489. * past end by one (callers fault) and resync_mb_y != 0
  1490. * causes problems for the first MB line, too.
  1491. */
  1492. if (!FIELD_PICTURE)
  1493. ff_er_frame_end(s);
  1494. MPV_frame_end(s);
  1495. h->current_slice=0;
  1496. }
  1497. /**
  1498. * Replicate H264 "master" context to thread contexts.
  1499. */
  1500. static void clone_slice(H264Context *dst, H264Context *src)
  1501. {
  1502. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  1503. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  1504. dst->s.current_picture = src->s.current_picture;
  1505. dst->s.linesize = src->s.linesize;
  1506. dst->s.uvlinesize = src->s.uvlinesize;
  1507. dst->s.first_field = src->s.first_field;
  1508. dst->prev_poc_msb = src->prev_poc_msb;
  1509. dst->prev_poc_lsb = src->prev_poc_lsb;
  1510. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  1511. dst->prev_frame_num = src->prev_frame_num;
  1512. dst->short_ref_count = src->short_ref_count;
  1513. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  1514. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  1515. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  1516. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  1517. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  1518. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  1519. }
  1520. /**
  1521. * computes profile from profile_idc and constraint_set?_flags
  1522. *
  1523. * @param sps SPS
  1524. *
  1525. * @return profile as defined by FF_PROFILE_H264_*
  1526. */
  1527. int ff_h264_get_profile(SPS *sps)
  1528. {
  1529. int profile = sps->profile_idc;
  1530. switch(sps->profile_idc) {
  1531. case FF_PROFILE_H264_BASELINE:
  1532. // constraint_set1_flag set to 1
  1533. profile |= (sps->constraint_set_flags & 1<<1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  1534. break;
  1535. case FF_PROFILE_H264_HIGH_10:
  1536. case FF_PROFILE_H264_HIGH_422:
  1537. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  1538. // constraint_set3_flag set to 1
  1539. profile |= (sps->constraint_set_flags & 1<<3) ? FF_PROFILE_H264_INTRA : 0;
  1540. break;
  1541. }
  1542. return profile;
  1543. }
  1544. /**
  1545. * decodes a slice header.
  1546. * This will also call MPV_common_init() and frame_start() as needed.
  1547. *
  1548. * @param h h264context
  1549. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  1550. *
  1551. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  1552. */
  1553. static int decode_slice_header(H264Context *h, H264Context *h0){
  1554. MpegEncContext * const s = &h->s;
  1555. MpegEncContext * const s0 = &h0->s;
  1556. unsigned int first_mb_in_slice;
  1557. unsigned int pps_id;
  1558. int num_ref_idx_active_override_flag;
  1559. unsigned int slice_type, tmp, i, j;
  1560. int default_ref_list_done = 0;
  1561. int last_pic_structure;
  1562. s->dropable= h->nal_ref_idc == 0;
  1563. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
  1564. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  1565. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  1566. }else{
  1567. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  1568. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  1569. }
  1570. first_mb_in_slice= get_ue_golomb(&s->gb);
  1571. if(first_mb_in_slice == 0){ //FIXME better field boundary detection
  1572. if(h0->current_slice && FIELD_PICTURE){
  1573. field_end(h, 1);
  1574. }
  1575. h0->current_slice = 0;
  1576. if (!s0->first_field)
  1577. s->current_picture_ptr= NULL;
  1578. }
  1579. slice_type= get_ue_golomb_31(&s->gb);
  1580. if(slice_type > 9){
  1581. av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
  1582. return -1;
  1583. }
  1584. if(slice_type > 4){
  1585. slice_type -= 5;
  1586. h->slice_type_fixed=1;
  1587. }else
  1588. h->slice_type_fixed=0;
  1589. slice_type= golomb_to_pict_type[ slice_type ];
  1590. if (slice_type == AV_PICTURE_TYPE_I
  1591. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  1592. default_ref_list_done = 1;
  1593. }
  1594. h->slice_type= slice_type;
  1595. h->slice_type_nos= slice_type & 3;
  1596. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  1597. pps_id= get_ue_golomb(&s->gb);
  1598. if(pps_id>=MAX_PPS_COUNT){
  1599. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  1600. return -1;
  1601. }
  1602. if(!h0->pps_buffers[pps_id]) {
  1603. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
  1604. return -1;
  1605. }
  1606. h->pps= *h0->pps_buffers[pps_id];
  1607. if(!h0->sps_buffers[h->pps.sps_id]) {
  1608. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
  1609. return -1;
  1610. }
  1611. h->sps = *h0->sps_buffers[h->pps.sps_id];
  1612. s->avctx->profile = ff_h264_get_profile(&h->sps);
  1613. s->avctx->level = h->sps.level_idc;
  1614. s->avctx->refs = h->sps.ref_frame_count;
  1615. if(h == h0 && h->dequant_coeff_pps != pps_id){
  1616. h->dequant_coeff_pps = pps_id;
  1617. init_dequant_tables(h);
  1618. }
  1619. s->mb_width= h->sps.mb_width;
  1620. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  1621. h->b_stride= s->mb_width*4;
  1622. s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
  1623. if(h->sps.frame_mbs_only_flag)
  1624. s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
  1625. else
  1626. s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 7);
  1627. if (s->context_initialized
  1628. && ( s->width != s->avctx->width || s->height != s->avctx->height
  1629. || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
  1630. if(h != h0) {
  1631. av_log_missing_feature(s->avctx, "Width/height changing with threads is", 0);
  1632. return -1; // width / height changed during parallelized decoding
  1633. }
  1634. free_tables(h, 0);
  1635. flush_dpb(s->avctx);
  1636. MPV_common_end(s);
  1637. }
  1638. if (!s->context_initialized) {
  1639. if(h != h0){
  1640. av_log(h->s.avctx, AV_LOG_ERROR, "we cant (re-)initialize context during parallel decoding\n");
  1641. return -1;
  1642. }
  1643. avcodec_set_dimensions(s->avctx, s->width, s->height);
  1644. s->avctx->sample_aspect_ratio= h->sps.sar;
  1645. av_assert0(s->avctx->sample_aspect_ratio.den);
  1646. if(h->sps.video_signal_type_present_flag){
  1647. s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  1648. if(h->sps.colour_description_present_flag){
  1649. s->avctx->color_primaries = h->sps.color_primaries;
  1650. s->avctx->color_trc = h->sps.color_trc;
  1651. s->avctx->colorspace = h->sps.colorspace;
  1652. }
  1653. }
  1654. if(h->sps.timing_info_present_flag){
  1655. int64_t den= h->sps.time_scale;
  1656. if(h->x264_build < 44U)
  1657. den *= 2;
  1658. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  1659. h->sps.num_units_in_tick, den, 1<<30);
  1660. }
  1661. switch (h->sps.bit_depth_luma) {
  1662. case 9 :
  1663. s->avctx->pix_fmt = PIX_FMT_YUV420P9;
  1664. break;
  1665. case 10 :
  1666. s->avctx->pix_fmt = PIX_FMT_YUV420P10;
  1667. break;
  1668. default:
  1669. s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
  1670. s->avctx->codec->pix_fmts ?
  1671. s->avctx->codec->pix_fmts :
  1672. s->avctx->color_range == AVCOL_RANGE_JPEG ?
  1673. hwaccel_pixfmt_list_h264_jpeg_420 :
  1674. ff_hwaccel_pixfmt_list_420);
  1675. }
  1676. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
  1677. if (MPV_common_init(s) < 0){
  1678. av_log(h->s.avctx, AV_LOG_ERROR, "MPV_common_init() failed\n");
  1679. return -1;
  1680. }
  1681. s->first_field = 0;
  1682. h->prev_interlaced_frame = 1;
  1683. init_scan_tables(h);
  1684. ff_h264_alloc_tables(h);
  1685. if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  1686. if (context_init(h) < 0){
  1687. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed\n");
  1688. return -1;
  1689. }
  1690. } else {
  1691. for(i = 1; i < s->avctx->thread_count; i++) {
  1692. H264Context *c;
  1693. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  1694. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  1695. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  1696. c->h264dsp = h->h264dsp;
  1697. c->sps = h->sps;
  1698. c->pps = h->pps;
  1699. c->pixel_shift = h->pixel_shift;
  1700. init_scan_tables(c);
  1701. clone_tables(c, h, i);
  1702. }
  1703. for(i = 0; i < s->avctx->thread_count; i++)
  1704. if(context_init(h->thread_context[i]) < 0){
  1705. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed\n");
  1706. return -1;
  1707. }
  1708. }
  1709. }
  1710. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  1711. h->mb_mbaff = 0;
  1712. h->mb_aff_frame = 0;
  1713. last_pic_structure = s0->picture_structure;
  1714. if(h->sps.frame_mbs_only_flag){
  1715. s->picture_structure= PICT_FRAME;
  1716. }else{
  1717. if(get_bits1(&s->gb)) { //field_pic_flag
  1718. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  1719. } else {
  1720. s->picture_structure= PICT_FRAME;
  1721. h->mb_aff_frame = h->sps.mb_aff;
  1722. }
  1723. }
  1724. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  1725. if(h0->current_slice == 0){
  1726. if(h->frame_num != h->prev_frame_num &&
  1727. (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num) < (h->frame_num - h->sps.ref_frame_count))
  1728. h->prev_frame_num = h->frame_num - h->sps.ref_frame_count - 1;
  1729. while(h->frame_num != h->prev_frame_num &&
  1730. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  1731. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  1732. av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  1733. if (ff_h264_frame_start(h) < 0)
  1734. return -1;
  1735. h->prev_frame_num++;
  1736. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  1737. s->current_picture_ptr->frame_num= h->prev_frame_num;
  1738. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0);
  1739. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1);
  1740. ff_generate_sliding_window_mmcos(h);
  1741. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1742. /* Error concealment: if a ref is missing, copy the previous ref in its place.
  1743. * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
  1744. * about there being no actual duplicates.
  1745. * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
  1746. * concealing a lost frame, this probably isn't noticable by comparison, but it should
  1747. * be fixed. */
  1748. if (h->short_ref_count) {
  1749. if (prev) {
  1750. av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
  1751. (const uint8_t**)prev->data, prev->linesize,
  1752. s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
  1753. h->short_ref[0]->poc = prev->poc+2;
  1754. }
  1755. h->short_ref[0]->frame_num = h->prev_frame_num;
  1756. }
  1757. }
  1758. /* See if we have a decoded first field looking for a pair... */
  1759. if (s0->first_field) {
  1760. assert(s0->current_picture_ptr);
  1761. assert(s0->current_picture_ptr->data[0]);
  1762. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  1763. /* figure out if we have a complementary field pair */
  1764. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  1765. /*
  1766. * Previous field is unmatched. Don't display it, but let it
  1767. * remain for reference if marked as such.
  1768. */
  1769. s0->current_picture_ptr = NULL;
  1770. s0->first_field = FIELD_PICTURE;
  1771. } else {
  1772. if (h->nal_ref_idc &&
  1773. s0->current_picture_ptr->reference &&
  1774. s0->current_picture_ptr->frame_num != h->frame_num) {
  1775. /*
  1776. * This and previous field were reference, but had
  1777. * different frame_nums. Consider this field first in
  1778. * pair. Throw away previous field except for reference
  1779. * purposes.
  1780. */
  1781. s0->first_field = 1;
  1782. s0->current_picture_ptr = NULL;
  1783. } else {
  1784. /* Second field in complementary pair */
  1785. s0->first_field = 0;
  1786. }
  1787. }
  1788. } else {
  1789. /* Frame or first field in a potentially complementary pair */
  1790. assert(!s0->current_picture_ptr);
  1791. s0->first_field = FIELD_PICTURE;
  1792. }
  1793. if(!FIELD_PICTURE || s0->first_field) {
  1794. if (ff_h264_frame_start(h) < 0) {
  1795. s0->first_field = 0;
  1796. return -1;
  1797. }
  1798. } else {
  1799. ff_release_unused_pictures(s, 0);
  1800. }
  1801. }
  1802. if(h != h0)
  1803. clone_slice(h, h0);
  1804. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  1805. assert(s->mb_num == s->mb_width * s->mb_height);
  1806. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  1807. first_mb_in_slice >= s->mb_num){
  1808. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1809. return -1;
  1810. }
  1811. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  1812. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  1813. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1814. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  1815. assert(s->mb_y < s->mb_height);
  1816. if(s->picture_structure==PICT_FRAME){
  1817. h->curr_pic_num= h->frame_num;
  1818. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  1819. }else{
  1820. h->curr_pic_num= 2*h->frame_num + 1;
  1821. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  1822. }
  1823. if(h->nal_unit_type == NAL_IDR_SLICE){
  1824. get_ue_golomb(&s->gb); /* idr_pic_id */
  1825. }
  1826. if(h->sps.poc_type==0){
  1827. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  1828. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  1829. h->delta_poc_bottom= get_se_golomb(&s->gb);
  1830. }
  1831. }
  1832. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  1833. h->delta_poc[0]= get_se_golomb(&s->gb);
  1834. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  1835. h->delta_poc[1]= get_se_golomb(&s->gb);
  1836. }
  1837. init_poc(h);
  1838. if(h->pps.redundant_pic_cnt_present){
  1839. h->redundant_pic_count= get_ue_golomb(&s->gb);
  1840. }
  1841. //set defaults, might be overridden a few lines later
  1842. h->ref_count[0]= h->pps.ref_count[0];
  1843. h->ref_count[1]= h->pps.ref_count[1];
  1844. if(h->slice_type_nos != AV_PICTURE_TYPE_I){
  1845. if(h->slice_type_nos == AV_PICTURE_TYPE_B){
  1846. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  1847. }
  1848. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  1849. if(num_ref_idx_active_override_flag){
  1850. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  1851. if(h->slice_type_nos==AV_PICTURE_TYPE_B)
  1852. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  1853. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  1854. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  1855. h->ref_count[0]= h->ref_count[1]= 1;
  1856. return -1;
  1857. }
  1858. }
  1859. if(h->slice_type_nos == AV_PICTURE_TYPE_B)
  1860. h->list_count= 2;
  1861. else
  1862. h->list_count= 1;
  1863. }else
  1864. h->list_count= 0;
  1865. if(!default_ref_list_done){
  1866. ff_h264_fill_default_ref_list(h);
  1867. }
  1868. if(h->slice_type_nos!=AV_PICTURE_TYPE_I && ff_h264_decode_ref_pic_list_reordering(h) < 0)
  1869. return -1;
  1870. if(h->slice_type_nos!=AV_PICTURE_TYPE_I){
  1871. s->last_picture_ptr= &h->ref_list[0][0];
  1872. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  1873. }
  1874. if(h->slice_type_nos==AV_PICTURE_TYPE_B){
  1875. s->next_picture_ptr= &h->ref_list[1][0];
  1876. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  1877. }
  1878. if( (h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P )
  1879. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== AV_PICTURE_TYPE_B ) )
  1880. pred_weight_table(h);
  1881. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
  1882. implicit_weight_table(h, -1);
  1883. }else {
  1884. h->use_weight = 0;
  1885. for (i = 0; i < 2; i++) {
  1886. h->luma_weight_flag[i] = 0;
  1887. h->chroma_weight_flag[i] = 0;
  1888. }
  1889. }
  1890. if(h->nal_ref_idc)
  1891. ff_h264_decode_ref_pic_marking(h0, &s->gb);
  1892. if(FRAME_MBAFF){
  1893. ff_h264_fill_mbaff_ref_list(h);
  1894. if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
  1895. implicit_weight_table(h, 0);
  1896. implicit_weight_table(h, 1);
  1897. }
  1898. }
  1899. if(h->slice_type_nos==AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
  1900. ff_h264_direct_dist_scale_factor(h);
  1901. ff_h264_direct_ref_list_init(h);
  1902. if( h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac ){
  1903. tmp = get_ue_golomb_31(&s->gb);
  1904. if(tmp > 2){
  1905. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  1906. return -1;
  1907. }
  1908. h->cabac_init_idc= tmp;
  1909. }
  1910. h->last_qscale_diff = 0;
  1911. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  1912. if(tmp>51+6*(h->sps.bit_depth_luma-8)){
  1913. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1914. return -1;
  1915. }
  1916. s->qscale= tmp;
  1917. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  1918. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  1919. //FIXME qscale / qp ... stuff
  1920. if(h->slice_type == AV_PICTURE_TYPE_SP){
  1921. get_bits1(&s->gb); /* sp_for_switch_flag */
  1922. }
  1923. if(h->slice_type==AV_PICTURE_TYPE_SP || h->slice_type == AV_PICTURE_TYPE_SI){
  1924. get_se_golomb(&s->gb); /* slice_qs_delta */
  1925. }
  1926. h->deblocking_filter = 1;
  1927. h->slice_alpha_c0_offset = 52;
  1928. h->slice_beta_offset = 52;
  1929. if( h->pps.deblocking_filter_parameters_present ) {
  1930. tmp= get_ue_golomb_31(&s->gb);
  1931. if(tmp > 2){
  1932. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  1933. return -1;
  1934. }
  1935. h->deblocking_filter= tmp;
  1936. if(h->deblocking_filter < 2)
  1937. h->deblocking_filter^= 1; // 1<->0
  1938. if( h->deblocking_filter ) {
  1939. h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
  1940. h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
  1941. if( h->slice_alpha_c0_offset > 104U
  1942. || h->slice_beta_offset > 104U){
  1943. av_log(s->avctx, AV_LOG_ERROR, "deblocking filter parameters %d %d out of range\n", h->slice_alpha_c0_offset, h->slice_beta_offset);
  1944. return -1;
  1945. }
  1946. }
  1947. }
  1948. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  1949. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != AV_PICTURE_TYPE_I)
  1950. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == AV_PICTURE_TYPE_B)
  1951. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  1952. h->deblocking_filter= 0;
  1953. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  1954. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  1955. /* Cheat slightly for speed:
  1956. Do not bother to deblock across slices. */
  1957. h->deblocking_filter = 2;
  1958. } else {
  1959. h0->max_contexts = 1;
  1960. if(!h0->single_decode_warning) {
  1961. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  1962. h0->single_decode_warning = 1;
  1963. }
  1964. if(h != h0){
  1965. av_log(h->s.avctx, AV_LOG_ERROR, "deblocking switched inside frame\n");
  1966. return 1;
  1967. }
  1968. }
  1969. }
  1970. h->qp_thresh= 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
  1971. #if 0 //FMO
  1972. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  1973. slice_group_change_cycle= get_bits(&s->gb, ?);
  1974. #endif
  1975. h0->last_slice_type = slice_type;
  1976. h->slice_num = ++h0->current_slice;
  1977. if(h->slice_num >= MAX_SLICES){
  1978. av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
  1979. }
  1980. for(j=0; j<2; j++){
  1981. int id_list[16];
  1982. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  1983. for(i=0; i<16; i++){
  1984. id_list[i]= 60;
  1985. if(h->ref_list[j][i].data[0]){
  1986. int k;
  1987. uint8_t *base= h->ref_list[j][i].base[0];
  1988. for(k=0; k<h->short_ref_count; k++)
  1989. if(h->short_ref[k]->base[0] == base){
  1990. id_list[i]= k;
  1991. break;
  1992. }
  1993. for(k=0; k<h->long_ref_count; k++)
  1994. if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
  1995. id_list[i]= h->short_ref_count + k;
  1996. break;
  1997. }
  1998. }
  1999. }
  2000. ref2frm[0]=
  2001. ref2frm[1]= -1;
  2002. for(i=0; i<16; i++)
  2003. ref2frm[i+2]= 4*id_list[i]
  2004. +(h->ref_list[j][i].reference&3);
  2005. ref2frm[18+0]=
  2006. ref2frm[18+1]= -1;
  2007. for(i=16; i<48; i++)
  2008. ref2frm[i+4]= 4*id_list[(i-16)>>1]
  2009. +(h->ref_list[j][i].reference&3);
  2010. }
  2011. //FIXME: fix draw_edges+PAFF+frame threads
  2012. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE || (!h->sps.frame_mbs_only_flag && s->avctx->active_thread_type&FF_THREAD_FRAME)) ? 0 : 16;
  2013. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  2014. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2015. av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
  2016. h->slice_num,
  2017. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  2018. first_mb_in_slice,
  2019. av_get_picture_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  2020. pps_id, h->frame_num,
  2021. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2022. h->ref_count[0], h->ref_count[1],
  2023. s->qscale,
  2024. h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
  2025. h->use_weight,
  2026. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  2027. h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  2028. );
  2029. }
  2030. return 0;
  2031. }
  2032. int ff_h264_get_slice_type(const H264Context *h)
  2033. {
  2034. switch (h->slice_type) {
  2035. case AV_PICTURE_TYPE_P: return 0;
  2036. case AV_PICTURE_TYPE_B: return 1;
  2037. case AV_PICTURE_TYPE_I: return 2;
  2038. case AV_PICTURE_TYPE_SP: return 3;
  2039. case AV_PICTURE_TYPE_SI: return 4;
  2040. default: return -1;
  2041. }
  2042. }
  2043. /**
  2044. *
  2045. * @return non zero if the loop filter can be skiped
  2046. */
  2047. static int fill_filter_caches(H264Context *h, int mb_type){
  2048. MpegEncContext * const s = &h->s;
  2049. const int mb_xy= h->mb_xy;
  2050. int top_xy, left_xy[2];
  2051. int top_type, left_type[2];
  2052. top_xy = mb_xy - (s->mb_stride << MB_FIELD);
  2053. //FIXME deblocking could skip the intra and nnz parts.
  2054. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  2055. * stuff, I can't imagine that these complex rules are worth it. */
  2056. left_xy[1] = left_xy[0] = mb_xy-1;
  2057. if(FRAME_MBAFF){
  2058. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
  2059. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  2060. if(s->mb_y&1){
  2061. if (left_mb_field_flag != curr_mb_field_flag) {
  2062. left_xy[0] -= s->mb_stride;
  2063. }
  2064. }else{
  2065. if(curr_mb_field_flag){
  2066. top_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy ]>>7)&1)-1);
  2067. }
  2068. if (left_mb_field_flag != curr_mb_field_flag) {
  2069. left_xy[1] += s->mb_stride;
  2070. }
  2071. }
  2072. }
  2073. h->top_mb_xy = top_xy;
  2074. h->left_mb_xy[0] = left_xy[0];
  2075. h->left_mb_xy[1] = left_xy[1];
  2076. {
  2077. //for sufficiently low qp, filtering wouldn't do anything
  2078. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  2079. int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
  2080. int qp = s->current_picture.qscale_table[mb_xy];
  2081. if(qp <= qp_thresh
  2082. && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
  2083. && (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){
  2084. if(!FRAME_MBAFF)
  2085. return 1;
  2086. if( (left_xy[0]< 0 || ((qp + s->current_picture.qscale_table[left_xy[1] ] + 1)>>1) <= qp_thresh)
  2087. && (top_xy < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy -s->mb_stride] + 1)>>1) <= qp_thresh))
  2088. return 1;
  2089. }
  2090. }
  2091. top_type = s->current_picture.mb_type[top_xy] ;
  2092. left_type[0] = s->current_picture.mb_type[left_xy[0]];
  2093. left_type[1] = s->current_picture.mb_type[left_xy[1]];
  2094. if(h->deblocking_filter == 2){
  2095. if(h->slice_table[top_xy ] != h->slice_num) top_type= 0;
  2096. if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
  2097. }else{
  2098. if(h->slice_table[top_xy ] == 0xFFFF) top_type= 0;
  2099. if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
  2100. }
  2101. h->top_type = top_type ;
  2102. h->left_type[0]= left_type[0];
  2103. h->left_type[1]= left_type[1];
  2104. if(IS_INTRA(mb_type))
  2105. return 0;
  2106. AV_COPY64(&h->non_zero_count_cache[0+8*1], &h->non_zero_count[mb_xy][ 0]);
  2107. AV_COPY64(&h->non_zero_count_cache[0+8*2], &h->non_zero_count[mb_xy][ 8]);
  2108. AV_COPY32(&h->non_zero_count_cache[0+8*5], &h->non_zero_count[mb_xy][16]);
  2109. AV_COPY32(&h->non_zero_count_cache[4+8*3], &h->non_zero_count[mb_xy][20]);
  2110. AV_COPY64(&h->non_zero_count_cache[0+8*4], &h->non_zero_count[mb_xy][24]);
  2111. h->cbp= h->cbp_table[mb_xy];
  2112. {
  2113. int list;
  2114. for(list=0; list<h->list_count; list++){
  2115. int8_t *ref;
  2116. int y, b_stride;
  2117. int16_t (*mv_dst)[2];
  2118. int16_t (*mv_src)[2];
  2119. if(!USES_LIST(mb_type, list)){
  2120. fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
  2121. AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2122. AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2123. AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2124. AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2125. continue;
  2126. }
  2127. ref = &s->current_picture.ref_index[list][4*mb_xy];
  2128. {
  2129. int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2130. AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2131. AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2132. ref += 2;
  2133. AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2134. AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2135. }
  2136. b_stride = h->b_stride;
  2137. mv_dst = &h->mv_cache[list][scan8[0]];
  2138. mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
  2139. for(y=0; y<4; y++){
  2140. AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
  2141. }
  2142. }
  2143. }
  2144. /*
  2145. 0 . T T. T T T T
  2146. 1 L . .L . . . .
  2147. 2 L . .L . . . .
  2148. 3 . T TL . . . .
  2149. 4 L . .L . . . .
  2150. 5 L . .. . . . .
  2151. */
  2152. //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
  2153. if(top_type){
  2154. AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][4+3*8]);
  2155. }
  2156. if(left_type[0]){
  2157. h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][7+0*8];
  2158. h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][7+1*8];
  2159. h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][7+2*8];
  2160. h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][7+3*8];
  2161. }
  2162. // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
  2163. if(!CABAC && h->pps.transform_8x8_mode){
  2164. if(IS_8x8DCT(top_type)){
  2165. h->non_zero_count_cache[4+8*0]=
  2166. h->non_zero_count_cache[5+8*0]= h->cbp_table[top_xy] & 4;
  2167. h->non_zero_count_cache[6+8*0]=
  2168. h->non_zero_count_cache[7+8*0]= h->cbp_table[top_xy] & 8;
  2169. }
  2170. if(IS_8x8DCT(left_type[0])){
  2171. h->non_zero_count_cache[3+8*1]=
  2172. h->non_zero_count_cache[3+8*2]= h->cbp_table[left_xy[0]]&2; //FIXME check MBAFF
  2173. }
  2174. if(IS_8x8DCT(left_type[1])){
  2175. h->non_zero_count_cache[3+8*3]=
  2176. h->non_zero_count_cache[3+8*4]= h->cbp_table[left_xy[1]]&8; //FIXME check MBAFF
  2177. }
  2178. if(IS_8x8DCT(mb_type)){
  2179. h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
  2180. h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
  2181. h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
  2182. h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
  2183. h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
  2184. h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
  2185. h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
  2186. h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
  2187. }
  2188. }
  2189. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  2190. int list;
  2191. for(list=0; list<h->list_count; list++){
  2192. if(USES_LIST(top_type, list)){
  2193. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  2194. const int b8_xy= 4*top_xy + 2;
  2195. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2196. AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
  2197. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  2198. h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
  2199. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  2200. h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
  2201. }else{
  2202. AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
  2203. AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2204. }
  2205. if(!IS_INTERLACED(mb_type^left_type[0])){
  2206. if(USES_LIST(left_type[0], list)){
  2207. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  2208. const int b8_xy= 4*left_xy[0] + 1;
  2209. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2210. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
  2211. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
  2212. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
  2213. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
  2214. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2215. h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
  2216. h->ref_cache[list][scan8[0] - 1 +16 ]=
  2217. h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
  2218. }else{
  2219. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
  2220. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
  2221. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
  2222. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
  2223. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2224. h->ref_cache[list][scan8[0] - 1 + 8 ]=
  2225. h->ref_cache[list][scan8[0] - 1 + 16 ]=
  2226. h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
  2227. }
  2228. }
  2229. }
  2230. }
  2231. return 0;
  2232. }
  2233. static void loop_filter(H264Context *h, int start_x, int end_x){
  2234. MpegEncContext * const s = &h->s;
  2235. uint8_t *dest_y, *dest_cb, *dest_cr;
  2236. int linesize, uvlinesize, mb_x, mb_y;
  2237. const int end_mb_y= s->mb_y + FRAME_MBAFF;
  2238. const int old_slice_type= h->slice_type;
  2239. const int pixel_shift = h->pixel_shift;
  2240. if(h->deblocking_filter) {
  2241. for(mb_x= start_x; mb_x<end_x; mb_x++){
  2242. for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
  2243. int mb_xy, mb_type;
  2244. mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
  2245. h->slice_num= h->slice_table[mb_xy];
  2246. mb_type= s->current_picture.mb_type[mb_xy];
  2247. h->list_count= h->list_counts[mb_xy];
  2248. if(FRAME_MBAFF)
  2249. h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  2250. s->mb_x= mb_x;
  2251. s->mb_y= mb_y;
  2252. dest_y = s->current_picture.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize ) * 16;
  2253. dest_cb = s->current_picture.data[1] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
  2254. dest_cr = s->current_picture.data[2] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
  2255. //FIXME simplify above
  2256. if (MB_FIELD) {
  2257. linesize = h->mb_linesize = s->linesize * 2;
  2258. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2259. if(mb_y&1){ //FIXME move out of this function?
  2260. dest_y -= s->linesize*15;
  2261. dest_cb-= s->uvlinesize*7;
  2262. dest_cr-= s->uvlinesize*7;
  2263. }
  2264. } else {
  2265. linesize = h->mb_linesize = s->linesize;
  2266. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2267. }
  2268. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  2269. if(fill_filter_caches(h, mb_type))
  2270. continue;
  2271. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  2272. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  2273. if (FRAME_MBAFF) {
  2274. ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2275. } else {
  2276. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2277. }
  2278. }
  2279. }
  2280. }
  2281. h->slice_type= old_slice_type;
  2282. s->mb_x= end_x;
  2283. s->mb_y= end_mb_y - FRAME_MBAFF;
  2284. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2285. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2286. }
  2287. static void predict_field_decoding_flag(H264Context *h){
  2288. MpegEncContext * const s = &h->s;
  2289. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2290. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  2291. ? s->current_picture.mb_type[mb_xy-1]
  2292. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  2293. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  2294. : 0;
  2295. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2296. }
  2297. /**
  2298. * Draw edges and report progress for the last MB row.
  2299. */
  2300. static void decode_finish_row(H264Context *h){
  2301. MpegEncContext * const s = &h->s;
  2302. int top = 16*(s->mb_y >> FIELD_PICTURE);
  2303. int height = 16 << FRAME_MBAFF;
  2304. int deblock_border = (16 + 4) << FRAME_MBAFF;
  2305. int pic_height = 16*s->mb_height >> FIELD_PICTURE;
  2306. if (h->deblocking_filter) {
  2307. if((top + height) >= pic_height)
  2308. height += deblock_border;
  2309. top -= deblock_border;
  2310. }
  2311. if (top >= pic_height || (top + height) < h->emu_edge_height)
  2312. return;
  2313. height = FFMIN(height, pic_height - top);
  2314. if (top < h->emu_edge_height) {
  2315. height = top+height;
  2316. top = 0;
  2317. }
  2318. ff_draw_horiz_band(s, top, height);
  2319. if (s->dropable) return;
  2320. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, top + height - 1,
  2321. s->picture_structure==PICT_BOTTOM_FIELD);
  2322. }
  2323. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  2324. H264Context *h = *(void**)arg;
  2325. MpegEncContext * const s = &h->s;
  2326. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  2327. int lf_x_start = s->mb_x;
  2328. s->mb_skip_run= -1;
  2329. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  2330. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  2331. if( h->pps.cabac ) {
  2332. /* realign */
  2333. align_get_bits( &s->gb );
  2334. /* init cabac */
  2335. ff_init_cabac_states( &h->cabac);
  2336. ff_init_cabac_decoder( &h->cabac,
  2337. s->gb.buffer + get_bits_count(&s->gb)/8,
  2338. (get_bits_left(&s->gb) + 7)/8);
  2339. ff_h264_init_cabac_states(h);
  2340. for(;;){
  2341. //START_TIMER
  2342. int ret = ff_h264_decode_mb_cabac(h);
  2343. int eos;
  2344. //STOP_TIMER("decode_mb_cabac")
  2345. if(ret>=0) ff_h264_hl_decode_mb(h);
  2346. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  2347. s->mb_y++;
  2348. ret = ff_h264_decode_mb_cabac(h);
  2349. if(ret>=0) ff_h264_hl_decode_mb(h);
  2350. s->mb_y--;
  2351. }
  2352. eos = get_cabac_terminate( &h->cabac );
  2353. if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
  2354. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  2355. if (s->mb_x >= lf_x_start) loop_filter(h, lf_x_start, s->mb_x + 1);
  2356. return 0;
  2357. }
  2358. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  2359. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%td)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
  2360. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  2361. return -1;
  2362. }
  2363. if( ++s->mb_x >= s->mb_width ) {
  2364. loop_filter(h, lf_x_start, s->mb_x);
  2365. s->mb_x = lf_x_start = 0;
  2366. decode_finish_row(h);
  2367. ++s->mb_y;
  2368. if(FIELD_OR_MBAFF_PICTURE) {
  2369. ++s->mb_y;
  2370. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2371. predict_field_decoding_flag(h);
  2372. }
  2373. }
  2374. if( eos || s->mb_y >= s->mb_height ) {
  2375. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2376. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  2377. if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);
  2378. return 0;
  2379. }
  2380. }
  2381. } else {
  2382. for(;;){
  2383. int ret = ff_h264_decode_mb_cavlc(h);
  2384. if(ret>=0) ff_h264_hl_decode_mb(h);
  2385. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  2386. s->mb_y++;
  2387. ret = ff_h264_decode_mb_cavlc(h);
  2388. if(ret>=0) ff_h264_hl_decode_mb(h);
  2389. s->mb_y--;
  2390. }
  2391. if(ret<0){
  2392. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2393. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  2394. return -1;
  2395. }
  2396. if(++s->mb_x >= s->mb_width){
  2397. loop_filter(h, lf_x_start, s->mb_x);
  2398. s->mb_x = lf_x_start = 0;
  2399. decode_finish_row(h);
  2400. ++s->mb_y;
  2401. if(FIELD_OR_MBAFF_PICTURE) {
  2402. ++s->mb_y;
  2403. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2404. predict_field_decoding_flag(h);
  2405. }
  2406. if(s->mb_y >= s->mb_height){
  2407. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2408. if( get_bits_count(&s->gb) == s->gb.size_in_bits
  2409. || get_bits_count(&s->gb) < s->gb.size_in_bits && s->avctx->error_recognition < FF_ER_AGGRESSIVE) {
  2410. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  2411. return 0;
  2412. }else{
  2413. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  2414. return -1;
  2415. }
  2416. }
  2417. }
  2418. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  2419. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2420. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  2421. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  2422. if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);
  2423. return 0;
  2424. }else{
  2425. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  2426. return -1;
  2427. }
  2428. }
  2429. }
  2430. }
  2431. #if 0
  2432. for(;s->mb_y < s->mb_height; s->mb_y++){
  2433. for(;s->mb_x < s->mb_width; s->mb_x++){
  2434. int ret= decode_mb(h);
  2435. ff_h264_hl_decode_mb(h);
  2436. if(ret<0){
  2437. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2438. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  2439. return -1;
  2440. }
  2441. if(++s->mb_x >= s->mb_width){
  2442. s->mb_x=0;
  2443. if(++s->mb_y >= s->mb_height){
  2444. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2445. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  2446. return 0;
  2447. }else{
  2448. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  2449. return -1;
  2450. }
  2451. }
  2452. }
  2453. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  2454. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2455. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  2456. return 0;
  2457. }else{
  2458. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  2459. return -1;
  2460. }
  2461. }
  2462. }
  2463. s->mb_x=0;
  2464. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2465. }
  2466. #endif
  2467. return -1; //not reached
  2468. }
  2469. /**
  2470. * Call decode_slice() for each context.
  2471. *
  2472. * @param h h264 master context
  2473. * @param context_count number of contexts to execute
  2474. */
  2475. static void execute_decode_slices(H264Context *h, int context_count){
  2476. MpegEncContext * const s = &h->s;
  2477. AVCodecContext * const avctx= s->avctx;
  2478. H264Context *hx;
  2479. int i;
  2480. if (s->avctx->hwaccel)
  2481. return;
  2482. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2483. return;
  2484. if(context_count == 1) {
  2485. decode_slice(avctx, &h);
  2486. } else {
  2487. for(i = 1; i < context_count; i++) {
  2488. hx = h->thread_context[i];
  2489. hx->s.error_recognition = avctx->error_recognition;
  2490. hx->s.error_count = 0;
  2491. hx->x264_build= h->x264_build;
  2492. }
  2493. avctx->execute(avctx, (void *)decode_slice,
  2494. h->thread_context, NULL, context_count, sizeof(void*));
  2495. /* pull back stuff from slices to master context */
  2496. hx = h->thread_context[context_count - 1];
  2497. s->mb_x = hx->s.mb_x;
  2498. s->mb_y = hx->s.mb_y;
  2499. s->dropable = hx->s.dropable;
  2500. s->picture_structure = hx->s.picture_structure;
  2501. for(i = 1; i < context_count; i++)
  2502. h->s.error_count += h->thread_context[i]->s.error_count;
  2503. }
  2504. }
  2505. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  2506. MpegEncContext * const s = &h->s;
  2507. AVCodecContext * const avctx= s->avctx;
  2508. int buf_index=0;
  2509. H264Context *hx; ///< thread context
  2510. int context_count = 0;
  2511. int next_avc= h->is_avc ? 0 : buf_size;
  2512. h->max_contexts = (HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_SLICE)) ? avctx->thread_count : 1;
  2513. #if 0
  2514. int i;
  2515. for(i=0; i<50; i++){
  2516. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  2517. }
  2518. #endif
  2519. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  2520. h->current_slice = 0;
  2521. if (!s->first_field)
  2522. s->current_picture_ptr= NULL;
  2523. ff_h264_reset_sei(h);
  2524. }
  2525. for(;;){
  2526. int consumed;
  2527. int dst_length;
  2528. int bit_length;
  2529. const uint8_t *ptr;
  2530. int i, nalsize = 0;
  2531. int err;
  2532. if(buf_index >= next_avc) {
  2533. if(buf_index >= buf_size) break;
  2534. nalsize = 0;
  2535. for(i = 0; i < h->nal_length_size; i++)
  2536. nalsize = (nalsize << 8) | buf[buf_index++];
  2537. if(nalsize <= 0 || nalsize > buf_size - buf_index){
  2538. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  2539. break;
  2540. }
  2541. next_avc= buf_index + nalsize;
  2542. } else {
  2543. // start code prefix search
  2544. for(; buf_index + 3 < next_avc; buf_index++){
  2545. // This should always succeed in the first iteration.
  2546. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  2547. break;
  2548. }
  2549. if(buf_index+3 >= buf_size) break;
  2550. buf_index+=3;
  2551. if(buf_index >= next_avc) continue;
  2552. }
  2553. hx = h->thread_context[context_count];
  2554. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
  2555. if (ptr==NULL || dst_length < 0){
  2556. return -1;
  2557. }
  2558. i= buf_index + consumed;
  2559. if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
  2560. buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
  2561. s->workaround_bugs |= FF_BUG_TRUNCATED;
  2562. if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
  2563. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  2564. dst_length--;
  2565. }
  2566. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  2567. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  2568. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d/%d at %d/%d length %d\n", hx->nal_unit_type, hx->nal_ref_idc, buf_index, buf_size, dst_length);
  2569. }
  2570. if (h->is_avc && (nalsize != consumed) && nalsize){
  2571. av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  2572. }
  2573. buf_index += consumed;
  2574. //FIXME do not discard SEI id
  2575. if(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
  2576. continue;
  2577. again:
  2578. err = 0;
  2579. switch(hx->nal_unit_type){
  2580. case NAL_IDR_SLICE:
  2581. if (h->nal_unit_type != NAL_IDR_SLICE) {
  2582. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  2583. return -1;
  2584. }
  2585. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  2586. case NAL_SLICE:
  2587. init_get_bits(&hx->s.gb, ptr, bit_length);
  2588. hx->intra_gb_ptr=
  2589. hx->inter_gb_ptr= &hx->s.gb;
  2590. hx->s.data_partitioning = 0;
  2591. if((err = decode_slice_header(hx, h)))
  2592. break;
  2593. s->current_picture_ptr->key_frame |=
  2594. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  2595. (h->sei_recovery_frame_cnt >= 0);
  2596. if (h->current_slice == 1) {
  2597. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)) {
  2598. decode_postinit(h);
  2599. }
  2600. if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  2601. return -1;
  2602. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2603. ff_vdpau_h264_picture_start(s);
  2604. }
  2605. if(hx->redundant_pic_count==0
  2606. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2607. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=AV_PICTURE_TYPE_B)
  2608. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)
  2609. && avctx->skip_frame < AVDISCARD_ALL){
  2610. if(avctx->hwaccel) {
  2611. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  2612. return -1;
  2613. }else
  2614. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  2615. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  2616. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  2617. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  2618. }else
  2619. context_count++;
  2620. }
  2621. break;
  2622. case NAL_DPA:
  2623. init_get_bits(&hx->s.gb, ptr, bit_length);
  2624. hx->intra_gb_ptr=
  2625. hx->inter_gb_ptr= NULL;
  2626. if ((err = decode_slice_header(hx, h)) < 0)
  2627. break;
  2628. hx->s.data_partitioning = 1;
  2629. break;
  2630. case NAL_DPB:
  2631. init_get_bits(&hx->intra_gb, ptr, bit_length);
  2632. hx->intra_gb_ptr= &hx->intra_gb;
  2633. break;
  2634. case NAL_DPC:
  2635. init_get_bits(&hx->inter_gb, ptr, bit_length);
  2636. hx->inter_gb_ptr= &hx->inter_gb;
  2637. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  2638. && s->context_initialized
  2639. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2640. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=AV_PICTURE_TYPE_B)
  2641. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)
  2642. && avctx->skip_frame < AVDISCARD_ALL)
  2643. context_count++;
  2644. break;
  2645. case NAL_SEI:
  2646. init_get_bits(&s->gb, ptr, bit_length);
  2647. ff_h264_decode_sei(h);
  2648. break;
  2649. case NAL_SPS:
  2650. init_get_bits(&s->gb, ptr, bit_length);
  2651. ff_h264_decode_seq_parameter_set(h);
  2652. if(s->flags& CODEC_FLAG_LOW_DELAY ||
  2653. (h->sps.bitstream_restriction_flag && !h->sps.num_reorder_frames))
  2654. s->low_delay=1;
  2655. if(avctx->has_b_frames < 2)
  2656. avctx->has_b_frames= !s->low_delay;
  2657. if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma) {
  2658. if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
  2659. avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  2660. h->pixel_shift = h->sps.bit_depth_luma > 8;
  2661. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma);
  2662. ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma);
  2663. dsputil_init(&s->dsp, s->avctx);
  2664. } else {
  2665. av_log(avctx, AV_LOG_DEBUG, "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
  2666. return -1;
  2667. }
  2668. }
  2669. break;
  2670. case NAL_PPS:
  2671. init_get_bits(&s->gb, ptr, bit_length);
  2672. ff_h264_decode_picture_parameter_set(h, bit_length);
  2673. break;
  2674. case NAL_AUD:
  2675. case NAL_END_SEQUENCE:
  2676. case NAL_END_STREAM:
  2677. case NAL_FILLER_DATA:
  2678. case NAL_SPS_EXT:
  2679. case NAL_AUXILIARY_SLICE:
  2680. break;
  2681. default:
  2682. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
  2683. }
  2684. if(context_count == h->max_contexts) {
  2685. execute_decode_slices(h, context_count);
  2686. context_count = 0;
  2687. }
  2688. if (err < 0)
  2689. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  2690. else if(err == 1) {
  2691. /* Slice could not be decoded in parallel mode, copy down
  2692. * NAL unit stuff to context 0 and restart. Note that
  2693. * rbsp_buffer is not transferred, but since we no longer
  2694. * run in parallel mode this should not be an issue. */
  2695. h->nal_unit_type = hx->nal_unit_type;
  2696. h->nal_ref_idc = hx->nal_ref_idc;
  2697. hx = h;
  2698. goto again;
  2699. }
  2700. }
  2701. if(context_count)
  2702. execute_decode_slices(h, context_count);
  2703. return buf_index;
  2704. }
  2705. /**
  2706. * returns the number of bytes consumed for building the current frame
  2707. */
  2708. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  2709. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  2710. if(pos+10>buf_size) pos=buf_size; // oops ;)
  2711. return pos;
  2712. }
  2713. static int decode_frame(AVCodecContext *avctx,
  2714. void *data, int *data_size,
  2715. AVPacket *avpkt)
  2716. {
  2717. const uint8_t *buf = avpkt->data;
  2718. int buf_size = avpkt->size;
  2719. H264Context *h = avctx->priv_data;
  2720. MpegEncContext *s = &h->s;
  2721. AVFrame *pict = data;
  2722. int buf_index;
  2723. s->flags= avctx->flags;
  2724. s->flags2= avctx->flags2;
  2725. /* end of stream, output what is still in the buffers */
  2726. out:
  2727. if (buf_size == 0) {
  2728. Picture *out;
  2729. int i, out_idx;
  2730. s->current_picture_ptr = NULL;
  2731. //FIXME factorize this with the output code below
  2732. out = h->delayed_pic[0];
  2733. out_idx = 0;
  2734. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  2735. if(h->delayed_pic[i]->poc < out->poc){
  2736. out = h->delayed_pic[i];
  2737. out_idx = i;
  2738. }
  2739. for(i=out_idx; h->delayed_pic[i]; i++)
  2740. h->delayed_pic[i] = h->delayed_pic[i+1];
  2741. if(out){
  2742. *data_size = sizeof(AVFrame);
  2743. *pict= *(AVFrame*)out;
  2744. }
  2745. return 0;
  2746. }
  2747. buf_index=decode_nal_units(h, buf, buf_size);
  2748. if(buf_index < 0)
  2749. return -1;
  2750. if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  2751. buf_size = 0;
  2752. goto out;
  2753. }
  2754. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  2755. if (avctx->skip_frame >= AVDISCARD_NONREF)
  2756. return 0;
  2757. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  2758. return -1;
  2759. }
  2760. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  2761. if(s->flags2 & CODEC_FLAG2_CHUNKS) decode_postinit(h);
  2762. field_end(h, 0);
  2763. if (!h->next_output_pic) {
  2764. /* Wait for second field. */
  2765. *data_size = 0;
  2766. } else {
  2767. *data_size = sizeof(AVFrame);
  2768. *pict = *(AVFrame*)h->next_output_pic;
  2769. }
  2770. }
  2771. assert(pict->data[0] || !*data_size);
  2772. ff_print_debug_info(s, pict);
  2773. //printf("out %d\n", (int)pict->data[0]);
  2774. return get_consumed_bytes(s, buf_index, buf_size);
  2775. }
  2776. #if 0
  2777. static inline void fill_mb_avail(H264Context *h){
  2778. MpegEncContext * const s = &h->s;
  2779. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2780. if(s->mb_y){
  2781. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  2782. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  2783. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  2784. }else{
  2785. h->mb_avail[0]=
  2786. h->mb_avail[1]=
  2787. h->mb_avail[2]= 0;
  2788. }
  2789. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  2790. h->mb_avail[4]= 1; //FIXME move out
  2791. h->mb_avail[5]= 0; //FIXME move out
  2792. }
  2793. #endif
  2794. #ifdef TEST
  2795. #undef printf
  2796. #undef random
  2797. #define COUNT 8000
  2798. #define SIZE (COUNT*40)
  2799. int main(void){
  2800. int i;
  2801. uint8_t temp[SIZE];
  2802. PutBitContext pb;
  2803. GetBitContext gb;
  2804. // int int_temp[10000];
  2805. DSPContext dsp;
  2806. AVCodecContext avctx;
  2807. dsputil_init(&dsp, &avctx);
  2808. init_put_bits(&pb, temp, SIZE);
  2809. printf("testing unsigned exp golomb\n");
  2810. for(i=0; i<COUNT; i++){
  2811. START_TIMER
  2812. set_ue_golomb(&pb, i);
  2813. STOP_TIMER("set_ue_golomb");
  2814. }
  2815. flush_put_bits(&pb);
  2816. init_get_bits(&gb, temp, 8*SIZE);
  2817. for(i=0; i<COUNT; i++){
  2818. int j, s;
  2819. s= show_bits(&gb, 24);
  2820. START_TIMER
  2821. j= get_ue_golomb(&gb);
  2822. if(j != i){
  2823. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2824. // return -1;
  2825. }
  2826. STOP_TIMER("get_ue_golomb");
  2827. }
  2828. init_put_bits(&pb, temp, SIZE);
  2829. printf("testing signed exp golomb\n");
  2830. for(i=0; i<COUNT; i++){
  2831. START_TIMER
  2832. set_se_golomb(&pb, i - COUNT/2);
  2833. STOP_TIMER("set_se_golomb");
  2834. }
  2835. flush_put_bits(&pb);
  2836. init_get_bits(&gb, temp, 8*SIZE);
  2837. for(i=0; i<COUNT; i++){
  2838. int j, s;
  2839. s= show_bits(&gb, 24);
  2840. START_TIMER
  2841. j= get_se_golomb(&gb);
  2842. if(j != i - COUNT/2){
  2843. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2844. // return -1;
  2845. }
  2846. STOP_TIMER("get_se_golomb");
  2847. }
  2848. #if 0
  2849. printf("testing 4x4 (I)DCT\n");
  2850. DCTELEM block[16];
  2851. uint8_t src[16], ref[16];
  2852. uint64_t error= 0, max_error=0;
  2853. for(i=0; i<COUNT; i++){
  2854. int j;
  2855. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  2856. for(j=0; j<16; j++){
  2857. ref[j]= random()%255;
  2858. src[j]= random()%255;
  2859. }
  2860. h264_diff_dct_c(block, src, ref, 4);
  2861. //normalize
  2862. for(j=0; j<16; j++){
  2863. // printf("%d ", block[j]);
  2864. block[j]= block[j]*4;
  2865. if(j&1) block[j]= (block[j]*4 + 2)/5;
  2866. if(j&4) block[j]= (block[j]*4 + 2)/5;
  2867. }
  2868. // printf("\n");
  2869. h->h264dsp.h264_idct_add(ref, block, 4);
  2870. /* for(j=0; j<16; j++){
  2871. printf("%d ", ref[j]);
  2872. }
  2873. printf("\n");*/
  2874. for(j=0; j<16; j++){
  2875. int diff= FFABS(src[j] - ref[j]);
  2876. error+= diff*diff;
  2877. max_error= FFMAX(max_error, diff);
  2878. }
  2879. }
  2880. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  2881. printf("testing quantizer\n");
  2882. for(qp=0; qp<52; qp++){
  2883. for(i=0; i<16; i++)
  2884. src1_block[i]= src2_block[i]= random()%255;
  2885. }
  2886. printf("Testing NAL layer\n");
  2887. uint8_t bitstream[COUNT];
  2888. uint8_t nal[COUNT*2];
  2889. H264Context h;
  2890. memset(&h, 0, sizeof(H264Context));
  2891. for(i=0; i<COUNT; i++){
  2892. int zeros= i;
  2893. int nal_length;
  2894. int consumed;
  2895. int out_length;
  2896. uint8_t *out;
  2897. int j;
  2898. for(j=0; j<COUNT; j++){
  2899. bitstream[j]= (random() % 255) + 1;
  2900. }
  2901. for(j=0; j<zeros; j++){
  2902. int pos= random() % COUNT;
  2903. while(bitstream[pos] == 0){
  2904. pos++;
  2905. pos %= COUNT;
  2906. }
  2907. bitstream[pos]=0;
  2908. }
  2909. START_TIMER
  2910. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  2911. if(nal_length<0){
  2912. printf("encoding failed\n");
  2913. return -1;
  2914. }
  2915. out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
  2916. STOP_TIMER("NAL")
  2917. if(out_length != COUNT){
  2918. printf("incorrect length %d %d\n", out_length, COUNT);
  2919. return -1;
  2920. }
  2921. if(consumed != nal_length){
  2922. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  2923. return -1;
  2924. }
  2925. if(memcmp(bitstream, out, COUNT)){
  2926. printf("mismatch\n");
  2927. return -1;
  2928. }
  2929. }
  2930. #endif
  2931. printf("Testing RBSP\n");
  2932. return 0;
  2933. }
  2934. #endif /* TEST */
  2935. av_cold void ff_h264_free_context(H264Context *h)
  2936. {
  2937. int i;
  2938. free_tables(h, 1); //FIXME cleanup init stuff perhaps
  2939. for(i = 0; i < MAX_SPS_COUNT; i++)
  2940. av_freep(h->sps_buffers + i);
  2941. for(i = 0; i < MAX_PPS_COUNT; i++)
  2942. av_freep(h->pps_buffers + i);
  2943. }
  2944. av_cold int ff_h264_decode_end(AVCodecContext *avctx)
  2945. {
  2946. H264Context *h = avctx->priv_data;
  2947. MpegEncContext *s = &h->s;
  2948. ff_h264_free_context(h);
  2949. MPV_common_end(s);
  2950. // memset(h, 0, sizeof(H264Context));
  2951. return 0;
  2952. }
  2953. static const AVProfile profiles[] = {
  2954. { FF_PROFILE_H264_BASELINE, "Baseline" },
  2955. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  2956. { FF_PROFILE_H264_MAIN, "Main" },
  2957. { FF_PROFILE_H264_EXTENDED, "Extended" },
  2958. { FF_PROFILE_H264_HIGH, "High" },
  2959. { FF_PROFILE_H264_HIGH_10, "High 10" },
  2960. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  2961. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  2962. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  2963. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  2964. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  2965. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  2966. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  2967. { FF_PROFILE_UNKNOWN },
  2968. };
  2969. AVCodec ff_h264_decoder = {
  2970. "h264",
  2971. AVMEDIA_TYPE_VIDEO,
  2972. CODEC_ID_H264,
  2973. sizeof(H264Context),
  2974. ff_h264_decode_init,
  2975. NULL,
  2976. ff_h264_decode_end,
  2977. decode_frame,
  2978. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY |
  2979. CODEC_CAP_FRAME_THREADS |
  2980. CODEC_CAP_SLICE_THREADS,
  2981. .flush= flush_dpb,
  2982. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  2983. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  2984. .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
  2985. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  2986. };
  2987. #if CONFIG_H264_VDPAU_DECODER
  2988. AVCodec ff_h264_vdpau_decoder = {
  2989. "h264_vdpau",
  2990. AVMEDIA_TYPE_VIDEO,
  2991. CODEC_ID_H264,
  2992. sizeof(H264Context),
  2993. ff_h264_decode_init,
  2994. NULL,
  2995. ff_h264_decode_end,
  2996. decode_frame,
  2997. CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  2998. .flush= flush_dpb,
  2999. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  3000. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
  3001. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3002. };
  3003. #endif