h264.c 315 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160
  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/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 libavcodec/h264.c
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "internal.h"
  27. #include "dsputil.h"
  28. #include "avcodec.h"
  29. #include "mpegvideo.h"
  30. #include "h264.h"
  31. #include "h264data.h"
  32. #include "h264_parser.h"
  33. #include "golomb.h"
  34. #include "mathops.h"
  35. #include "rectangle.h"
  36. #include "vdpau_internal.h"
  37. #include "cabac.h"
  38. #if ARCH_X86
  39. #include "x86/h264_i386.h"
  40. #endif
  41. //#undef NDEBUG
  42. #include <assert.h>
  43. /**
  44. * Value of Picture.reference when Picture is not a reference picture, but
  45. * is held for delayed output.
  46. */
  47. #define DELAYED_PIC_REF 4
  48. static VLC coeff_token_vlc[4];
  49. static VLC_TYPE coeff_token_vlc_tables[520+332+280+256][2];
  50. static const int coeff_token_vlc_tables_size[4]={520,332,280,256};
  51. static VLC chroma_dc_coeff_token_vlc;
  52. static VLC_TYPE chroma_dc_coeff_token_vlc_table[256][2];
  53. static const int chroma_dc_coeff_token_vlc_table_size = 256;
  54. static VLC total_zeros_vlc[15];
  55. static VLC_TYPE total_zeros_vlc_tables[15][512][2];
  56. static const int total_zeros_vlc_tables_size = 512;
  57. static VLC chroma_dc_total_zeros_vlc[3];
  58. static VLC_TYPE chroma_dc_total_zeros_vlc_tables[3][8][2];
  59. static const int chroma_dc_total_zeros_vlc_tables_size = 8;
  60. static VLC run_vlc[6];
  61. static VLC_TYPE run_vlc_tables[6][8][2];
  62. static const int run_vlc_tables_size = 8;
  63. static VLC run7_vlc;
  64. static VLC_TYPE run7_vlc_table[96][2];
  65. static const int run7_vlc_table_size = 96;
  66. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  67. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  68. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  69. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  70. static Picture * remove_long(H264Context *h, int i, int ref_mask);
  71. static av_always_inline uint32_t pack16to32(int a, int b){
  72. #ifdef WORDS_BIGENDIAN
  73. return (b&0xFFFF) + (a<<16);
  74. #else
  75. return (a&0xFFFF) + (b<<16);
  76. #endif
  77. }
  78. static const uint8_t rem6[52]={
  79. 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,
  80. };
  81. static const uint8_t div6[52]={
  82. 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,
  83. };
  84. static const uint8_t left_block_options[4][8]={
  85. {0,1,2,3,7,10,8,11},
  86. {2,2,3,3,8,11,8,11},
  87. {0,0,1,1,7,10,7,10},
  88. {0,2,0,2,7,10,7,10}
  89. };
  90. #define LEVEL_TAB_BITS 8
  91. static int8_t cavlc_level_tab[7][1<<LEVEL_TAB_BITS][2];
  92. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  93. MpegEncContext * const s = &h->s;
  94. const int mb_xy= h->mb_xy;
  95. int topleft_xy, top_xy, topright_xy, left_xy[2];
  96. int topleft_type, top_type, topright_type, left_type[2];
  97. const uint8_t * left_block;
  98. int topleft_partition= -1;
  99. int i;
  100. top_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
  101. //FIXME deblocking could skip the intra and nnz parts.
  102. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[top_xy]) && !FRAME_MBAFF)
  103. return;
  104. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  105. * stuff, I can't imagine that these complex rules are worth it. */
  106. topleft_xy = top_xy - 1;
  107. topright_xy= top_xy + 1;
  108. left_xy[1] = left_xy[0] = mb_xy-1;
  109. left_block = left_block_options[0];
  110. if(FRAME_MBAFF){
  111. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  112. const int top_pair_xy = pair_xy - s->mb_stride;
  113. const int topleft_pair_xy = top_pair_xy - 1;
  114. const int topright_pair_xy = top_pair_xy + 1;
  115. const int topleft_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  116. const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  117. const int topright_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  118. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  119. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  120. const int bottom = (s->mb_y & 1);
  121. tprintf(s->avctx, "fill_caches: curr_mb_field_flag:%d, left_mb_field_flag:%d, topleft_mb_field_flag:%d, top_mb_field_flag:%d, topright_mb_field_flag:%d\n", curr_mb_field_flag, left_mb_field_flag, topleft_mb_field_flag, top_mb_field_flag, topright_mb_field_flag);
  122. if (curr_mb_field_flag && (bottom || top_mb_field_flag)){
  123. top_xy -= s->mb_stride;
  124. }
  125. if (curr_mb_field_flag && (bottom || topleft_mb_field_flag)){
  126. topleft_xy -= s->mb_stride;
  127. } else if(bottom && !curr_mb_field_flag && left_mb_field_flag) {
  128. topleft_xy += s->mb_stride;
  129. // take top left mv from the middle of the mb, as opposed to all other modes which use the bottom right partition
  130. topleft_partition = 0;
  131. }
  132. if (curr_mb_field_flag && (bottom || topright_mb_field_flag)){
  133. topright_xy -= s->mb_stride;
  134. }
  135. if (left_mb_field_flag != curr_mb_field_flag) {
  136. left_xy[1] = left_xy[0] = pair_xy - 1;
  137. if (curr_mb_field_flag) {
  138. left_xy[1] += s->mb_stride;
  139. left_block = left_block_options[3];
  140. } else {
  141. left_block= left_block_options[2 - bottom];
  142. }
  143. }
  144. }
  145. h->top_mb_xy = top_xy;
  146. h->left_mb_xy[0] = left_xy[0];
  147. h->left_mb_xy[1] = left_xy[1];
  148. if(for_deblock){
  149. topleft_type = 0;
  150. topright_type = 0;
  151. top_type = h->slice_table[top_xy ] < 0xFFFF ? s->current_picture.mb_type[top_xy] : 0;
  152. left_type[0] = h->slice_table[left_xy[0] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[0]] : 0;
  153. left_type[1] = h->slice_table[left_xy[1] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[1]] : 0;
  154. if(MB_MBAFF && !IS_INTRA(mb_type)){
  155. int list;
  156. for(list=0; list<h->list_count; list++){
  157. //These values where changed for ease of performing MC, we need to change them back
  158. //FIXME maybe we can make MC and loop filter use the same values or prevent
  159. //the MC code from changing ref_cache and rather use a temporary array.
  160. if(USES_LIST(mb_type,list)){
  161. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  162. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  163. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = (pack16to32(ref[0],ref[1])&0x00FF00FF)*0x0101;
  164. ref += h->b8_stride;
  165. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  166. *(uint32_t*)&h->ref_cache[list][scan8[10]] = (pack16to32(ref[0],ref[1])&0x00FF00FF)*0x0101;
  167. }
  168. }
  169. }
  170. }else{
  171. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  172. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  173. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  174. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  175. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  176. if(IS_INTRA(mb_type)){
  177. int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
  178. h->topleft_samples_available=
  179. h->top_samples_available=
  180. h->left_samples_available= 0xFFFF;
  181. h->topright_samples_available= 0xEEEA;
  182. if(!(top_type & type_mask)){
  183. h->topleft_samples_available= 0xB3FF;
  184. h->top_samples_available= 0x33FF;
  185. h->topright_samples_available= 0x26EA;
  186. }
  187. if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[0])){
  188. if(IS_INTERLACED(mb_type)){
  189. if(!(left_type[0] & type_mask)){
  190. h->topleft_samples_available&= 0xDFFF;
  191. h->left_samples_available&= 0x5FFF;
  192. }
  193. if(!(left_type[1] & type_mask)){
  194. h->topleft_samples_available&= 0xFF5F;
  195. h->left_samples_available&= 0xFF5F;
  196. }
  197. }else{
  198. int left_typei = h->slice_table[left_xy[0] + s->mb_stride ] == h->slice_num
  199. ? s->current_picture.mb_type[left_xy[0] + s->mb_stride] : 0;
  200. assert(left_xy[0] == left_xy[1]);
  201. if(!((left_typei & type_mask) && (left_type[0] & type_mask))){
  202. h->topleft_samples_available&= 0xDF5F;
  203. h->left_samples_available&= 0x5F5F;
  204. }
  205. }
  206. }else{
  207. if(!(left_type[0] & type_mask)){
  208. h->topleft_samples_available&= 0xDF5F;
  209. h->left_samples_available&= 0x5F5F;
  210. }
  211. }
  212. if(!(topleft_type & type_mask))
  213. h->topleft_samples_available&= 0x7FFF;
  214. if(!(topright_type & type_mask))
  215. h->topright_samples_available&= 0xFBFF;
  216. if(IS_INTRA4x4(mb_type)){
  217. if(IS_INTRA4x4(top_type)){
  218. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  219. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  220. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  221. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  222. }else{
  223. int pred;
  224. if(!(top_type & type_mask))
  225. pred= -1;
  226. else{
  227. pred= 2;
  228. }
  229. h->intra4x4_pred_mode_cache[4+8*0]=
  230. h->intra4x4_pred_mode_cache[5+8*0]=
  231. h->intra4x4_pred_mode_cache[6+8*0]=
  232. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  233. }
  234. for(i=0; i<2; i++){
  235. if(IS_INTRA4x4(left_type[i])){
  236. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  237. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  238. }else{
  239. int pred;
  240. if(!(left_type[i] & type_mask))
  241. pred= -1;
  242. else{
  243. pred= 2;
  244. }
  245. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  246. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. /*
  253. 0 . T T. T T T T
  254. 1 L . .L . . . .
  255. 2 L . .L . . . .
  256. 3 . T TL . . . .
  257. 4 L . .L . . . .
  258. 5 L . .. . . . .
  259. */
  260. //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
  261. if(top_type){
  262. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  263. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  264. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  265. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  266. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  267. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  268. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  269. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  270. }else{
  271. h->non_zero_count_cache[4+8*0]=
  272. h->non_zero_count_cache[5+8*0]=
  273. h->non_zero_count_cache[6+8*0]=
  274. h->non_zero_count_cache[7+8*0]=
  275. h->non_zero_count_cache[1+8*0]=
  276. h->non_zero_count_cache[2+8*0]=
  277. h->non_zero_count_cache[1+8*3]=
  278. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  279. }
  280. for (i=0; i<2; i++) {
  281. if(left_type[i]){
  282. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  283. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  284. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  285. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  286. }else{
  287. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  288. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  289. h->non_zero_count_cache[0+8*1 + 8*i]=
  290. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  291. }
  292. }
  293. if( h->pps.cabac ) {
  294. // top_cbp
  295. if(top_type) {
  296. h->top_cbp = h->cbp_table[top_xy];
  297. } else if(IS_INTRA(mb_type)) {
  298. h->top_cbp = 0x1C0;
  299. } else {
  300. h->top_cbp = 0;
  301. }
  302. // left_cbp
  303. if (left_type[0]) {
  304. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  305. } else if(IS_INTRA(mb_type)) {
  306. h->left_cbp = 0x1C0;
  307. } else {
  308. h->left_cbp = 0;
  309. }
  310. if (left_type[0]) {
  311. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  312. }
  313. if (left_type[1]) {
  314. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  315. }
  316. }
  317. #if 1
  318. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  319. int list;
  320. for(list=0; list<h->list_count; list++){
  321. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  322. /*if(!h->mv_cache_clean[list]){
  323. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  324. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  325. h->mv_cache_clean[list]= 1;
  326. }*/
  327. continue;
  328. }
  329. h->mv_cache_clean[list]= 0;
  330. if(USES_LIST(top_type, list)){
  331. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  332. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  333. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  334. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  335. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  336. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  337. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  338. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  339. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  340. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  341. }else{
  342. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  343. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  344. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  345. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  346. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  347. }
  348. for(i=0; i<2; i++){
  349. int cache_idx = scan8[0] - 1 + i*2*8;
  350. if(USES_LIST(left_type[i], list)){
  351. const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
  352. const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
  353. *(uint32_t*)h->mv_cache[list][cache_idx ]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]];
  354. *(uint32_t*)h->mv_cache[list][cache_idx+8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]];
  355. h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
  356. h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
  357. }else{
  358. *(uint32_t*)h->mv_cache [list][cache_idx ]=
  359. *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
  360. h->ref_cache[list][cache_idx ]=
  361. h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  362. }
  363. }
  364. if(for_deblock || ((IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred) && !FRAME_MBAFF))
  365. continue;
  366. if(USES_LIST(topleft_type, list)){
  367. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + h->b_stride + (topleft_partition & 2*h->b_stride);
  368. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + (topleft_partition & h->b8_stride);
  369. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  370. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  371. }else{
  372. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  373. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  374. }
  375. if(USES_LIST(topright_type, list)){
  376. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  377. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  378. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  379. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  380. }else{
  381. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  382. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  383. }
  384. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  385. continue;
  386. h->ref_cache[list][scan8[5 ]+1] =
  387. h->ref_cache[list][scan8[7 ]+1] =
  388. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  389. h->ref_cache[list][scan8[4 ]] =
  390. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  391. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  392. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  393. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  394. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  395. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  396. if( h->pps.cabac ) {
  397. /* XXX beurk, Load mvd */
  398. if(USES_LIST(top_type, list)){
  399. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  400. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  401. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  402. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  403. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  404. }else{
  405. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  406. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  407. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  408. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  409. }
  410. if(USES_LIST(left_type[0], list)){
  411. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  412. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
  413. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
  414. }else{
  415. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  416. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  417. }
  418. if(USES_LIST(left_type[1], list)){
  419. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  420. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
  421. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
  422. }else{
  423. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  424. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  425. }
  426. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  427. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  428. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  429. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  430. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  431. if(h->slice_type_nos == FF_B_TYPE){
  432. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  433. if(IS_DIRECT(top_type)){
  434. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  435. }else if(IS_8X8(top_type)){
  436. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  437. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  438. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  439. }else{
  440. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  441. }
  442. if(IS_DIRECT(left_type[0]))
  443. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  444. else if(IS_8X8(left_type[0]))
  445. h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];
  446. else
  447. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  448. if(IS_DIRECT(left_type[1]))
  449. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  450. else if(IS_8X8(left_type[1]))
  451. h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];
  452. else
  453. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  454. }
  455. }
  456. if(FRAME_MBAFF){
  457. #define MAP_MVS\
  458. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  459. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  460. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  461. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  462. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  463. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  464. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  465. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  466. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  467. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  468. if(MB_FIELD){
  469. #define MAP_F2F(idx, mb_type)\
  470. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  471. h->ref_cache[list][idx] <<= 1;\
  472. h->mv_cache[list][idx][1] /= 2;\
  473. h->mvd_cache[list][idx][1] /= 2;\
  474. }
  475. MAP_MVS
  476. #undef MAP_F2F
  477. }else{
  478. #define MAP_F2F(idx, mb_type)\
  479. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  480. h->ref_cache[list][idx] >>= 1;\
  481. h->mv_cache[list][idx][1] <<= 1;\
  482. h->mvd_cache[list][idx][1] <<= 1;\
  483. }
  484. MAP_MVS
  485. #undef MAP_F2F
  486. }
  487. }
  488. }
  489. }
  490. #endif
  491. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  492. }
  493. static inline void write_back_intra_pred_mode(H264Context *h){
  494. const int mb_xy= h->mb_xy;
  495. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  496. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  497. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  498. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  499. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  500. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  501. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  502. }
  503. /**
  504. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  505. */
  506. static inline int check_intra4x4_pred_mode(H264Context *h){
  507. MpegEncContext * const s = &h->s;
  508. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  509. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  510. int i;
  511. if(!(h->top_samples_available&0x8000)){
  512. for(i=0; i<4; i++){
  513. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  514. if(status<0){
  515. 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);
  516. return -1;
  517. } else if(status){
  518. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  519. }
  520. }
  521. }
  522. if((h->left_samples_available&0x8888)!=0x8888){
  523. static const int mask[4]={0x8000,0x2000,0x80,0x20};
  524. for(i=0; i<4; i++){
  525. if(!(h->left_samples_available&mask[i])){
  526. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  527. if(status<0){
  528. 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);
  529. return -1;
  530. } else if(status){
  531. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  532. }
  533. }
  534. }
  535. }
  536. return 0;
  537. } //FIXME cleanup like next
  538. /**
  539. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  540. */
  541. static inline int check_intra_pred_mode(H264Context *h, int mode){
  542. MpegEncContext * const s = &h->s;
  543. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  544. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  545. if(mode > 6U) {
  546. 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);
  547. return -1;
  548. }
  549. if(!(h->top_samples_available&0x8000)){
  550. mode= top[ mode ];
  551. if(mode<0){
  552. 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);
  553. return -1;
  554. }
  555. }
  556. if((h->left_samples_available&0x8080) != 0x8080){
  557. mode= left[ mode ];
  558. if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
  559. mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
  560. }
  561. if(mode<0){
  562. 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);
  563. return -1;
  564. }
  565. }
  566. return mode;
  567. }
  568. /**
  569. * gets the predicted intra4x4 prediction mode.
  570. */
  571. static inline int pred_intra_mode(H264Context *h, int n){
  572. const int index8= scan8[n];
  573. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  574. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  575. const int min= FFMIN(left, top);
  576. tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
  577. if(min<0) return DC_PRED;
  578. else return min;
  579. }
  580. static inline void write_back_non_zero_count(H264Context *h){
  581. const int mb_xy= h->mb_xy;
  582. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  583. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  584. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  585. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  586. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  587. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  588. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  589. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  590. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  591. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  592. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  593. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  594. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  595. }
  596. /**
  597. * gets the predicted number of non-zero coefficients.
  598. * @param n block index
  599. */
  600. static inline int pred_non_zero_count(H264Context *h, int n){
  601. const int index8= scan8[n];
  602. const int left= h->non_zero_count_cache[index8 - 1];
  603. const int top = h->non_zero_count_cache[index8 - 8];
  604. int i= left + top;
  605. if(i<64) i= (i+1)>>1;
  606. tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  607. return i&31;
  608. }
  609. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  610. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  611. MpegEncContext *s = &h->s;
  612. /* there is no consistent mapping of mvs to neighboring locations that will
  613. * make mbaff happy, so we can't move all this logic to fill_caches */
  614. if(FRAME_MBAFF){
  615. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  616. const int16_t *mv;
  617. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  618. *C = h->mv_cache[list][scan8[0]-2];
  619. if(!MB_FIELD
  620. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  621. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  622. if(IS_INTERLACED(mb_types[topright_xy])){
  623. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  624. const int x4 = X4, y4 = Y4;\
  625. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  626. if(!USES_LIST(mb_type,list))\
  627. return LIST_NOT_USED;\
  628. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  629. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  630. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  631. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  632. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  633. }
  634. }
  635. if(topright_ref == PART_NOT_AVAILABLE
  636. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  637. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  638. if(!MB_FIELD
  639. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  640. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  641. }
  642. if(MB_FIELD
  643. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  644. && i >= scan8[0]+8){
  645. // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
  646. SET_DIAG_MV(/2, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  647. }
  648. }
  649. #undef SET_DIAG_MV
  650. }
  651. if(topright_ref != PART_NOT_AVAILABLE){
  652. *C= h->mv_cache[list][ i - 8 + part_width ];
  653. return topright_ref;
  654. }else{
  655. tprintf(s->avctx, "topright MV not available\n");
  656. *C= h->mv_cache[list][ i - 8 - 1 ];
  657. return h->ref_cache[list][ i - 8 - 1 ];
  658. }
  659. }
  660. /**
  661. * gets the predicted MV.
  662. * @param n the block index
  663. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  664. * @param mx the x component of the predicted motion vector
  665. * @param my the y component of the predicted motion vector
  666. */
  667. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  668. const int index8= scan8[n];
  669. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  670. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  671. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  672. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  673. const int16_t * C;
  674. int diagonal_ref, match_count;
  675. assert(part_width==1 || part_width==2 || part_width==4);
  676. /* mv_cache
  677. B . . A T T T T
  678. U . . L . . , .
  679. U . . L . . . .
  680. U . . L . . , .
  681. . . . L . . . .
  682. */
  683. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  684. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  685. tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
  686. if(match_count > 1){ //most common
  687. *mx= mid_pred(A[0], B[0], C[0]);
  688. *my= mid_pred(A[1], B[1], C[1]);
  689. }else if(match_count==1){
  690. if(left_ref==ref){
  691. *mx= A[0];
  692. *my= A[1];
  693. }else if(top_ref==ref){
  694. *mx= B[0];
  695. *my= B[1];
  696. }else{
  697. *mx= C[0];
  698. *my= C[1];
  699. }
  700. }else{
  701. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  702. *mx= A[0];
  703. *my= A[1];
  704. }else{
  705. *mx= mid_pred(A[0], B[0], C[0]);
  706. *my= mid_pred(A[1], B[1], C[1]);
  707. }
  708. }
  709. tprintf(h->s.avctx, "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
  710. }
  711. /**
  712. * gets the directionally predicted 16x8 MV.
  713. * @param n the block index
  714. * @param mx the x component of the predicted motion vector
  715. * @param my the y component of the predicted motion vector
  716. */
  717. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  718. if(n==0){
  719. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  720. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  721. tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
  722. if(top_ref == ref){
  723. *mx= B[0];
  724. *my= B[1];
  725. return;
  726. }
  727. }else{
  728. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  729. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  730. tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  731. if(left_ref == ref){
  732. *mx= A[0];
  733. *my= A[1];
  734. return;
  735. }
  736. }
  737. //RARE
  738. pred_motion(h, n, 4, list, ref, mx, my);
  739. }
  740. /**
  741. * gets the directionally predicted 8x16 MV.
  742. * @param n the block index
  743. * @param mx the x component of the predicted motion vector
  744. * @param my the y component of the predicted motion vector
  745. */
  746. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  747. if(n==0){
  748. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  749. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  750. tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  751. if(left_ref == ref){
  752. *mx= A[0];
  753. *my= A[1];
  754. return;
  755. }
  756. }else{
  757. const int16_t * C;
  758. int diagonal_ref;
  759. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  760. tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
  761. if(diagonal_ref == ref){
  762. *mx= C[0];
  763. *my= C[1];
  764. return;
  765. }
  766. }
  767. //RARE
  768. pred_motion(h, n, 2, list, ref, mx, my);
  769. }
  770. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  771. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  772. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  773. tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  774. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  775. || !( top_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ])
  776. || !(left_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ])){
  777. *mx = *my = 0;
  778. return;
  779. }
  780. pred_motion(h, 0, 4, 0, 0, mx, my);
  781. return;
  782. }
  783. static int get_scale_factor(H264Context * const h, int poc, int poc1, int i){
  784. int poc0 = h->ref_list[0][i].poc;
  785. int td = av_clip(poc1 - poc0, -128, 127);
  786. if(td == 0 || h->ref_list[0][i].long_ref){
  787. return 256;
  788. }else{
  789. int tb = av_clip(poc - poc0, -128, 127);
  790. int tx = (16384 + (FFABS(td) >> 1)) / td;
  791. return av_clip((tb*tx + 32) >> 6, -1024, 1023);
  792. }
  793. }
  794. static inline void direct_dist_scale_factor(H264Context * const h){
  795. MpegEncContext * const s = &h->s;
  796. const int poc = h->s.current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
  797. const int poc1 = h->ref_list[1][0].poc;
  798. int i, field;
  799. for(field=0; field<2; field++){
  800. const int poc = h->s.current_picture_ptr->field_poc[field];
  801. const int poc1 = h->ref_list[1][0].field_poc[field];
  802. for(i=0; i < 2*h->ref_count[0]; i++)
  803. h->dist_scale_factor_field[field][i^field] = get_scale_factor(h, poc, poc1, i+16);
  804. }
  805. for(i=0; i<h->ref_count[0]; i++){
  806. h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
  807. }
  808. }
  809. static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
  810. MpegEncContext * const s = &h->s;
  811. Picture * const ref1 = &h->ref_list[1][0];
  812. int j, old_ref, rfield;
  813. int start= mbafi ? 16 : 0;
  814. int end = mbafi ? 16+2*h->ref_count[list] : h->ref_count[list];
  815. int interl= mbafi || s->picture_structure != PICT_FRAME;
  816. /* bogus; fills in for missing frames */
  817. memset(map[list], 0, sizeof(map[list]));
  818. for(rfield=0; rfield<2; rfield++){
  819. for(old_ref=0; old_ref<ref1->ref_count[colfield][list]; old_ref++){
  820. int poc = ref1->ref_poc[colfield][list][old_ref];
  821. if (!interl)
  822. poc |= 3;
  823. else if( interl && (poc&3) == 3) //FIXME store all MBAFF references so this isnt needed
  824. poc= (poc&~3) + rfield + 1;
  825. for(j=start; j<end; j++){
  826. if(4*h->ref_list[list][j].frame_num + (h->ref_list[list][j].reference&3) == poc){
  827. int cur_ref= mbafi ? (j-16)^field : j;
  828. map[list][2*old_ref + (rfield^field) + 16] = cur_ref;
  829. if(rfield == field)
  830. map[list][old_ref] = cur_ref;
  831. break;
  832. }
  833. }
  834. }
  835. }
  836. }
  837. static inline void direct_ref_list_init(H264Context * const h){
  838. MpegEncContext * const s = &h->s;
  839. Picture * const ref1 = &h->ref_list[1][0];
  840. Picture * const cur = s->current_picture_ptr;
  841. int list, j, field;
  842. int sidx= (s->picture_structure&1)^1;
  843. int ref1sidx= (ref1->reference&1)^1;
  844. for(list=0; list<2; list++){
  845. cur->ref_count[sidx][list] = h->ref_count[list];
  846. for(j=0; j<h->ref_count[list]; j++)
  847. cur->ref_poc[sidx][list][j] = 4*h->ref_list[list][j].frame_num + (h->ref_list[list][j].reference&3);
  848. }
  849. if(s->picture_structure == PICT_FRAME){
  850. memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
  851. memcpy(cur->ref_poc [1], cur->ref_poc [0], sizeof(cur->ref_poc [0]));
  852. }
  853. cur->mbaff= FRAME_MBAFF;
  854. if(cur->pict_type != FF_B_TYPE || h->direct_spatial_mv_pred)
  855. return;
  856. for(list=0; list<2; list++){
  857. fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
  858. for(field=0; field<2; field++)
  859. fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
  860. }
  861. }
  862. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  863. MpegEncContext * const s = &h->s;
  864. int b8_stride = h->b8_stride;
  865. int b4_stride = h->b_stride;
  866. int mb_xy = h->mb_xy;
  867. int mb_type_col[2];
  868. const int16_t (*l1mv0)[2], (*l1mv1)[2];
  869. const int8_t *l1ref0, *l1ref1;
  870. const int is_b8x8 = IS_8X8(*mb_type);
  871. unsigned int sub_mb_type;
  872. int i8, i4;
  873. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  874. if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
  875. if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL
  876. int cur_poc = s->current_picture_ptr->poc;
  877. int *col_poc = h->ref_list[1]->field_poc;
  878. int col_parity = FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc);
  879. mb_xy= s->mb_x + ((s->mb_y&~1) + col_parity)*s->mb_stride;
  880. b8_stride = 0;
  881. }else if(!(s->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff){// FL -> FL & differ parity
  882. int fieldoff= 2*(h->ref_list[1][0].reference)-3;
  883. mb_xy += s->mb_stride*fieldoff;
  884. }
  885. goto single_col;
  886. }else{ // AFL/AFR/FR/FL -> AFR/FR
  887. if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
  888. mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
  889. mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
  890. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
  891. b8_stride *= 3;
  892. b4_stride *= 6;
  893. //FIXME IS_8X8(mb_type_col[0]) && !h->sps.direct_8x8_inference_flag
  894. if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
  895. && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
  896. && !is_b8x8){
  897. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  898. *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
  899. }else{
  900. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  901. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  902. }
  903. }else{ // AFR/FR -> AFR/FR
  904. single_col:
  905. mb_type_col[0] =
  906. mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
  907. if(IS_8X8(mb_type_col[0]) && !h->sps.direct_8x8_inference_flag){
  908. /* FIXME save sub mb types from previous frames (or derive from MVs)
  909. * so we know exactly what block size to use */
  910. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  911. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  912. }else if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
  913. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  914. *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  915. }else{
  916. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  917. *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
  918. }
  919. }
  920. }
  921. l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
  922. l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
  923. l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]];
  924. l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]];
  925. if(!b8_stride){
  926. if(s->mb_y&1){
  927. l1ref0 += h->b8_stride;
  928. l1ref1 += h->b8_stride;
  929. l1mv0 += 2*b4_stride;
  930. l1mv1 += 2*b4_stride;
  931. }
  932. }
  933. if(h->direct_spatial_mv_pred){
  934. int ref[2];
  935. int mv[2][2];
  936. int list;
  937. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  938. /* ref = min(neighbors) */
  939. for(list=0; list<2; list++){
  940. int refa = h->ref_cache[list][scan8[0] - 1];
  941. int refb = h->ref_cache[list][scan8[0] - 8];
  942. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  943. if(refc == PART_NOT_AVAILABLE)
  944. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  945. ref[list] = FFMIN3((unsigned)refa, (unsigned)refb, (unsigned)refc);
  946. if(ref[list] < 0)
  947. ref[list] = -1;
  948. }
  949. if(ref[0] < 0 && ref[1] < 0){
  950. ref[0] = ref[1] = 0;
  951. mv[0][0] = mv[0][1] =
  952. mv[1][0] = mv[1][1] = 0;
  953. }else{
  954. for(list=0; list<2; list++){
  955. if(ref[list] >= 0)
  956. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  957. else
  958. mv[list][0] = mv[list][1] = 0;
  959. }
  960. }
  961. if(ref[1] < 0){
  962. if(!is_b8x8)
  963. *mb_type &= ~MB_TYPE_L1;
  964. sub_mb_type &= ~MB_TYPE_L1;
  965. }else if(ref[0] < 0){
  966. if(!is_b8x8)
  967. *mb_type &= ~MB_TYPE_L0;
  968. sub_mb_type &= ~MB_TYPE_L0;
  969. }
  970. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  971. for(i8=0; i8<4; i8++){
  972. int x8 = i8&1;
  973. int y8 = i8>>1;
  974. int xy8 = x8+y8*b8_stride;
  975. int xy4 = 3*x8+y8*b4_stride;
  976. int a=0, b=0;
  977. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  978. continue;
  979. h->sub_mb_type[i8] = sub_mb_type;
  980. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  981. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  982. if(!IS_INTRA(mb_type_col[y8])
  983. && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
  984. || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
  985. if(ref[0] > 0)
  986. a= pack16to32(mv[0][0],mv[0][1]);
  987. if(ref[1] > 0)
  988. b= pack16to32(mv[1][0],mv[1][1]);
  989. }else{
  990. a= pack16to32(mv[0][0],mv[0][1]);
  991. b= pack16to32(mv[1][0],mv[1][1]);
  992. }
  993. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
  994. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
  995. }
  996. }else if(IS_16X16(*mb_type)){
  997. int a=0, b=0;
  998. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  999. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1000. if(!IS_INTRA(mb_type_col[0])
  1001. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  1002. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1003. && (h->x264_build>33 || !h->x264_build)))){
  1004. if(ref[0] > 0)
  1005. a= pack16to32(mv[0][0],mv[0][1]);
  1006. if(ref[1] > 0)
  1007. b= pack16to32(mv[1][0],mv[1][1]);
  1008. }else{
  1009. a= pack16to32(mv[0][0],mv[0][1]);
  1010. b= pack16to32(mv[1][0],mv[1][1]);
  1011. }
  1012. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  1013. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  1014. }else{
  1015. for(i8=0; i8<4; i8++){
  1016. const int x8 = i8&1;
  1017. const int y8 = i8>>1;
  1018. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1019. continue;
  1020. h->sub_mb_type[i8] = sub_mb_type;
  1021. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1022. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1023. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1024. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1025. /* col_zero_flag */
  1026. if(!IS_INTRA(mb_type_col[0]) && ( l1ref0[x8 + y8*b8_stride] == 0
  1027. || (l1ref0[x8 + y8*b8_stride] < 0 && l1ref1[x8 + y8*b8_stride] == 0
  1028. && (h->x264_build>33 || !h->x264_build)))){
  1029. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*b8_stride] == 0 ? l1mv0 : l1mv1;
  1030. if(IS_SUB_8X8(sub_mb_type)){
  1031. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  1032. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1033. if(ref[0] == 0)
  1034. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1035. if(ref[1] == 0)
  1036. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1037. }
  1038. }else
  1039. for(i4=0; i4<4; i4++){
  1040. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  1041. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1042. if(ref[0] == 0)
  1043. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1044. if(ref[1] == 0)
  1045. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1046. }
  1047. }
  1048. }
  1049. }
  1050. }
  1051. }else{ /* direct temporal mv pred */
  1052. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1053. const int *dist_scale_factor = h->dist_scale_factor;
  1054. int ref_offset= 0;
  1055. if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
  1056. map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
  1057. map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
  1058. dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
  1059. }
  1060. if(h->ref_list[1][0].mbaff && IS_INTERLACED(mb_type_col[0]))
  1061. ref_offset += 16;
  1062. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
  1063. /* FIXME assumes direct_8x8_inference == 1 */
  1064. int y_shift = 2*!IS_INTERLACED(*mb_type);
  1065. for(i8=0; i8<4; i8++){
  1066. const int x8 = i8&1;
  1067. const int y8 = i8>>1;
  1068. int ref0, scale;
  1069. const int16_t (*l1mv)[2]= l1mv0;
  1070. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1071. continue;
  1072. h->sub_mb_type[i8] = sub_mb_type;
  1073. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1074. if(IS_INTRA(mb_type_col[y8])){
  1075. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1076. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1077. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1078. continue;
  1079. }
  1080. ref0 = l1ref0[x8 + y8*b8_stride];
  1081. if(ref0 >= 0)
  1082. ref0 = map_col_to_list0[0][ref0 + ref_offset];
  1083. else{
  1084. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
  1085. l1mv= l1mv1;
  1086. }
  1087. scale = dist_scale_factor[ref0];
  1088. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1089. {
  1090. const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
  1091. int my_col = (mv_col[1]<<y_shift)/2;
  1092. int mx = (scale * mv_col[0] + 128) >> 8;
  1093. int my = (scale * my_col + 128) >> 8;
  1094. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1095. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1096. }
  1097. }
  1098. return;
  1099. }
  1100. /* one-to-one mv scaling */
  1101. if(IS_16X16(*mb_type)){
  1102. int ref, mv0, mv1;
  1103. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1104. if(IS_INTRA(mb_type_col[0])){
  1105. ref=mv0=mv1=0;
  1106. }else{
  1107. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
  1108. : map_col_to_list0[1][l1ref1[0] + ref_offset];
  1109. const int scale = dist_scale_factor[ref0];
  1110. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1111. int mv_l0[2];
  1112. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1113. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1114. ref= ref0;
  1115. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  1116. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1117. }
  1118. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  1119. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  1120. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  1121. }else{
  1122. for(i8=0; i8<4; i8++){
  1123. const int x8 = i8&1;
  1124. const int y8 = i8>>1;
  1125. int ref0, scale;
  1126. const int16_t (*l1mv)[2]= l1mv0;
  1127. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1128. continue;
  1129. h->sub_mb_type[i8] = sub_mb_type;
  1130. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1131. if(IS_INTRA(mb_type_col[0])){
  1132. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1133. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1134. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1135. continue;
  1136. }
  1137. ref0 = l1ref0[x8 + y8*b8_stride] + ref_offset;
  1138. if(ref0 >= 0)
  1139. ref0 = map_col_to_list0[0][ref0];
  1140. else{
  1141. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
  1142. l1mv= l1mv1;
  1143. }
  1144. scale = dist_scale_factor[ref0];
  1145. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1146. if(IS_SUB_8X8(sub_mb_type)){
  1147. const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
  1148. int mx = (scale * mv_col[0] + 128) >> 8;
  1149. int my = (scale * mv_col[1] + 128) >> 8;
  1150. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1151. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1152. }else
  1153. for(i4=0; i4<4; i4++){
  1154. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
  1155. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1156. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1157. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1158. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1159. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. }
  1165. static inline void write_back_motion(H264Context *h, int mb_type){
  1166. MpegEncContext * const s = &h->s;
  1167. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1168. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1169. int list;
  1170. if(!USES_LIST(mb_type, 0))
  1171. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1172. for(list=0; list<h->list_count; list++){
  1173. int y;
  1174. if(!USES_LIST(mb_type, list))
  1175. continue;
  1176. for(y=0; y<4; y++){
  1177. *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y];
  1178. *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y];
  1179. }
  1180. if( h->pps.cabac ) {
  1181. if(IS_SKIP(mb_type))
  1182. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1183. else
  1184. for(y=0; y<4; y++){
  1185. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1186. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1187. }
  1188. }
  1189. {
  1190. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1191. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1192. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1193. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1194. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1195. }
  1196. }
  1197. if(h->slice_type_nos == FF_B_TYPE && h->pps.cabac){
  1198. if(IS_8X8(mb_type)){
  1199. uint8_t *direct_table = &h->direct_table[b8_xy];
  1200. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1201. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1202. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1203. }
  1204. }
  1205. }
  1206. const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
  1207. int i, si, di;
  1208. uint8_t *dst;
  1209. int bufidx;
  1210. // src[0]&0x80; //forbidden bit
  1211. h->nal_ref_idc= src[0]>>5;
  1212. h->nal_unit_type= src[0]&0x1F;
  1213. src++; length--;
  1214. #if 0
  1215. for(i=0; i<length; i++)
  1216. printf("%2X ", src[i]);
  1217. #endif
  1218. #if HAVE_FAST_UNALIGNED
  1219. # if HAVE_FAST_64BIT
  1220. # define RS 7
  1221. for(i=0; i+1<length; i+=9){
  1222. if(!((~*(const uint64_t*)(src+i) & (*(const uint64_t*)(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
  1223. # else
  1224. # define RS 3
  1225. for(i=0; i+1<length; i+=5){
  1226. if(!((~*(const uint32_t*)(src+i) & (*(const uint32_t*)(src+i) - 0x01000101U)) & 0x80008080U))
  1227. # endif
  1228. continue;
  1229. if(i>0 && !src[i]) i--;
  1230. while(src[i]) i++;
  1231. #else
  1232. # define RS 0
  1233. for(i=0; i+1<length; i+=2){
  1234. if(src[i]) continue;
  1235. if(i>0 && src[i-1]==0) i--;
  1236. #endif
  1237. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1238. if(src[i+2]!=3){
  1239. /* startcode, so we must be past the end */
  1240. length=i;
  1241. }
  1242. break;
  1243. }
  1244. i-= RS;
  1245. }
  1246. if(i>=length-1){ //no escaped 0
  1247. *dst_length= length;
  1248. *consumed= length+1; //+1 for the header
  1249. return src;
  1250. }
  1251. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
  1252. h->rbsp_buffer[bufidx]= av_fast_realloc(h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
  1253. dst= h->rbsp_buffer[bufidx];
  1254. if (dst == NULL){
  1255. return NULL;
  1256. }
  1257. //printf("decoding esc\n");
  1258. memcpy(dst, src, i);
  1259. si=di=i;
  1260. while(si+2<length){
  1261. //remove escapes (very rare 1:2^22)
  1262. if(src[si+2]>3){
  1263. dst[di++]= src[si++];
  1264. dst[di++]= src[si++];
  1265. }else if(src[si]==0 && src[si+1]==0){
  1266. if(src[si+2]==3){ //escape
  1267. dst[di++]= 0;
  1268. dst[di++]= 0;
  1269. si+=3;
  1270. continue;
  1271. }else //next start code
  1272. goto nsc;
  1273. }
  1274. dst[di++]= src[si++];
  1275. }
  1276. while(si<length)
  1277. dst[di++]= src[si++];
  1278. nsc:
  1279. memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  1280. *dst_length= di;
  1281. *consumed= si + 1;//+1 for the header
  1282. //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
  1283. return dst;
  1284. }
  1285. int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
  1286. int v= *src;
  1287. int r;
  1288. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  1289. for(r=1; r<9; r++){
  1290. if(v&1) return r;
  1291. v>>=1;
  1292. }
  1293. return 0;
  1294. }
  1295. /**
  1296. * IDCT transforms the 16 dc values and dequantizes them.
  1297. * @param qp quantization parameter
  1298. */
  1299. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1300. #define stride 16
  1301. int i;
  1302. int temp[16]; //FIXME check if this is a good idea
  1303. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1304. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1305. //memset(block, 64, 2*256);
  1306. //return;
  1307. for(i=0; i<4; i++){
  1308. const int offset= y_offset[i];
  1309. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1310. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1311. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1312. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1313. temp[4*i+0]= z0+z3;
  1314. temp[4*i+1]= z1+z2;
  1315. temp[4*i+2]= z1-z2;
  1316. temp[4*i+3]= z0-z3;
  1317. }
  1318. for(i=0; i<4; i++){
  1319. const int offset= x_offset[i];
  1320. const int z0= temp[4*0+i] + temp[4*2+i];
  1321. const int z1= temp[4*0+i] - temp[4*2+i];
  1322. const int z2= temp[4*1+i] - temp[4*3+i];
  1323. const int z3= temp[4*1+i] + temp[4*3+i];
  1324. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_residual
  1325. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1326. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1327. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1328. }
  1329. }
  1330. #if 0
  1331. /**
  1332. * DCT transforms the 16 dc values.
  1333. * @param qp quantization parameter ??? FIXME
  1334. */
  1335. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1336. // const int qmul= dequant_coeff[qp][0];
  1337. int i;
  1338. int temp[16]; //FIXME check if this is a good idea
  1339. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1340. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1341. for(i=0; i<4; i++){
  1342. const int offset= y_offset[i];
  1343. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1344. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1345. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1346. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1347. temp[4*i+0]= z0+z3;
  1348. temp[4*i+1]= z1+z2;
  1349. temp[4*i+2]= z1-z2;
  1350. temp[4*i+3]= z0-z3;
  1351. }
  1352. for(i=0; i<4; i++){
  1353. const int offset= x_offset[i];
  1354. const int z0= temp[4*0+i] + temp[4*2+i];
  1355. const int z1= temp[4*0+i] - temp[4*2+i];
  1356. const int z2= temp[4*1+i] - temp[4*3+i];
  1357. const int z3= temp[4*1+i] + temp[4*3+i];
  1358. block[stride*0 +offset]= (z0 + z3)>>1;
  1359. block[stride*2 +offset]= (z1 + z2)>>1;
  1360. block[stride*8 +offset]= (z1 - z2)>>1;
  1361. block[stride*10+offset]= (z0 - z3)>>1;
  1362. }
  1363. }
  1364. #endif
  1365. #undef xStride
  1366. #undef stride
  1367. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1368. const int stride= 16*2;
  1369. const int xStride= 16;
  1370. int a,b,c,d,e;
  1371. a= block[stride*0 + xStride*0];
  1372. b= block[stride*0 + xStride*1];
  1373. c= block[stride*1 + xStride*0];
  1374. d= block[stride*1 + xStride*1];
  1375. e= a-b;
  1376. a= a+b;
  1377. b= c-d;
  1378. c= c+d;
  1379. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1380. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1381. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1382. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1383. }
  1384. #if 0
  1385. static void chroma_dc_dct_c(DCTELEM *block){
  1386. const int stride= 16*2;
  1387. const int xStride= 16;
  1388. int a,b,c,d,e;
  1389. a= block[stride*0 + xStride*0];
  1390. b= block[stride*0 + xStride*1];
  1391. c= block[stride*1 + xStride*0];
  1392. d= block[stride*1 + xStride*1];
  1393. e= a-b;
  1394. a= a+b;
  1395. b= c-d;
  1396. c= c+d;
  1397. block[stride*0 + xStride*0]= (a+c);
  1398. block[stride*0 + xStride*1]= (e+b);
  1399. block[stride*1 + xStride*0]= (a-c);
  1400. block[stride*1 + xStride*1]= (e-b);
  1401. }
  1402. #endif
  1403. /**
  1404. * gets the chroma qp.
  1405. */
  1406. static inline int get_chroma_qp(H264Context *h, int t, int qscale){
  1407. return h->pps.chroma_qp_table[t][qscale];
  1408. }
  1409. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  1410. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1411. int src_x_offset, int src_y_offset,
  1412. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  1413. MpegEncContext * const s = &h->s;
  1414. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  1415. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  1416. const int luma_xy= (mx&3) + ((my&3)<<2);
  1417. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  1418. uint8_t * src_cb, * src_cr;
  1419. int extra_width= h->emu_edge_width;
  1420. int extra_height= h->emu_edge_height;
  1421. int emu=0;
  1422. const int full_mx= mx>>2;
  1423. const int full_my= my>>2;
  1424. const int pic_width = 16*s->mb_width;
  1425. const int pic_height = 16*s->mb_height >> MB_FIELD;
  1426. if(mx&7) extra_width -= 3;
  1427. if(my&7) extra_height -= 3;
  1428. if( full_mx < 0-extra_width
  1429. || full_my < 0-extra_height
  1430. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  1431. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  1432. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
  1433. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  1434. emu=1;
  1435. }
  1436. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  1437. if(!square){
  1438. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  1439. }
  1440. if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
  1441. if(MB_FIELD){
  1442. // chroma offset when predicting from a field of opposite parity
  1443. my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
  1444. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  1445. }
  1446. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  1447. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  1448. if(emu){
  1449. ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  1450. src_cb= s->edge_emu_buffer;
  1451. }
  1452. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  1453. if(emu){
  1454. ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  1455. src_cr= s->edge_emu_buffer;
  1456. }
  1457. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  1458. }
  1459. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  1460. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1461. int x_offset, int y_offset,
  1462. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1463. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  1464. int list0, int list1){
  1465. MpegEncContext * const s = &h->s;
  1466. qpel_mc_func *qpix_op= qpix_put;
  1467. h264_chroma_mc_func chroma_op= chroma_put;
  1468. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  1469. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  1470. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  1471. x_offset += 8*s->mb_x;
  1472. y_offset += 8*(s->mb_y >> MB_FIELD);
  1473. if(list0){
  1474. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  1475. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  1476. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1477. qpix_op, chroma_op);
  1478. qpix_op= qpix_avg;
  1479. chroma_op= chroma_avg;
  1480. }
  1481. if(list1){
  1482. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  1483. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  1484. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1485. qpix_op, chroma_op);
  1486. }
  1487. }
  1488. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  1489. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1490. int x_offset, int y_offset,
  1491. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1492. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  1493. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  1494. int list0, int list1){
  1495. MpegEncContext * const s = &h->s;
  1496. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  1497. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  1498. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  1499. x_offset += 8*s->mb_x;
  1500. y_offset += 8*(s->mb_y >> MB_FIELD);
  1501. if(list0 && list1){
  1502. /* don't optimize for luma-only case, since B-frames usually
  1503. * use implicit weights => chroma too. */
  1504. uint8_t *tmp_cb = s->obmc_scratchpad;
  1505. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  1506. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  1507. int refn0 = h->ref_cache[0][ scan8[n] ];
  1508. int refn1 = h->ref_cache[1][ scan8[n] ];
  1509. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  1510. dest_y, dest_cb, dest_cr,
  1511. x_offset, y_offset, qpix_put, chroma_put);
  1512. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  1513. tmp_y, tmp_cb, tmp_cr,
  1514. x_offset, y_offset, qpix_put, chroma_put);
  1515. if(h->use_weight == 2){
  1516. int weight0 = h->implicit_weight[refn0][refn1];
  1517. int weight1 = 64 - weight0;
  1518. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  1519. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  1520. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  1521. }else{
  1522. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  1523. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  1524. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  1525. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1526. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  1527. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  1528. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1529. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  1530. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  1531. }
  1532. }else{
  1533. int list = list1 ? 1 : 0;
  1534. int refn = h->ref_cache[list][ scan8[n] ];
  1535. Picture *ref= &h->ref_list[list][refn];
  1536. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  1537. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1538. qpix_put, chroma_put);
  1539. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  1540. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  1541. if(h->use_weight_chroma){
  1542. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1543. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  1544. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1545. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  1546. }
  1547. }
  1548. }
  1549. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  1550. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1551. int x_offset, int y_offset,
  1552. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1553. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  1554. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  1555. int list0, int list1){
  1556. if((h->use_weight==2 && list0 && list1
  1557. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  1558. || h->use_weight==1)
  1559. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  1560. x_offset, y_offset, qpix_put, chroma_put,
  1561. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  1562. else
  1563. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  1564. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  1565. }
  1566. static inline void prefetch_motion(H264Context *h, int list){
  1567. /* fetch pixels for estimated mv 4 macroblocks ahead
  1568. * optimized for 64byte cache lines */
  1569. MpegEncContext * const s = &h->s;
  1570. const int refn = h->ref_cache[list][scan8[0]];
  1571. if(refn >= 0){
  1572. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  1573. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  1574. uint8_t **src= h->ref_list[list][refn].data;
  1575. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  1576. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  1577. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  1578. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  1579. }
  1580. }
  1581. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1582. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  1583. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  1584. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  1585. MpegEncContext * const s = &h->s;
  1586. const int mb_xy= h->mb_xy;
  1587. const int mb_type= s->current_picture.mb_type[mb_xy];
  1588. assert(IS_INTER(mb_type));
  1589. prefetch_motion(h, 0);
  1590. if(IS_16X16(mb_type)){
  1591. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  1592. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  1593. &weight_op[0], &weight_avg[0],
  1594. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1595. }else if(IS_16X8(mb_type)){
  1596. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  1597. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1598. &weight_op[1], &weight_avg[1],
  1599. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1600. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  1601. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1602. &weight_op[1], &weight_avg[1],
  1603. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1604. }else if(IS_8X16(mb_type)){
  1605. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  1606. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1607. &weight_op[2], &weight_avg[2],
  1608. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1609. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  1610. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1611. &weight_op[2], &weight_avg[2],
  1612. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1613. }else{
  1614. int i;
  1615. assert(IS_8X8(mb_type));
  1616. for(i=0; i<4; i++){
  1617. const int sub_mb_type= h->sub_mb_type[i];
  1618. const int n= 4*i;
  1619. int x_offset= (i&1)<<2;
  1620. int y_offset= (i&2)<<1;
  1621. if(IS_SUB_8X8(sub_mb_type)){
  1622. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1623. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1624. &weight_op[3], &weight_avg[3],
  1625. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1626. }else if(IS_SUB_8X4(sub_mb_type)){
  1627. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1628. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1629. &weight_op[4], &weight_avg[4],
  1630. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1631. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  1632. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1633. &weight_op[4], &weight_avg[4],
  1634. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1635. }else if(IS_SUB_4X8(sub_mb_type)){
  1636. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1637. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1638. &weight_op[5], &weight_avg[5],
  1639. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1640. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  1641. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1642. &weight_op[5], &weight_avg[5],
  1643. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1644. }else{
  1645. int j;
  1646. assert(IS_SUB_4X4(sub_mb_type));
  1647. for(j=0; j<4; j++){
  1648. int sub_x_offset= x_offset + 2*(j&1);
  1649. int sub_y_offset= y_offset + (j&2);
  1650. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  1651. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1652. &weight_op[6], &weight_avg[6],
  1653. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1654. }
  1655. }
  1656. }
  1657. }
  1658. prefetch_motion(h, 1);
  1659. }
  1660. static av_cold void init_cavlc_level_tab(void){
  1661. int suffix_length, mask;
  1662. unsigned int i;
  1663. for(suffix_length=0; suffix_length<7; suffix_length++){
  1664. for(i=0; i<(1<<LEVEL_TAB_BITS); i++){
  1665. int prefix= LEVEL_TAB_BITS - av_log2(2*i);
  1666. int level_code= (prefix<<suffix_length) + (i>>(LEVEL_TAB_BITS-prefix-1-suffix_length)) - (1<<suffix_length);
  1667. mask= -(level_code&1);
  1668. level_code= (((2+level_code)>>1) ^ mask) - mask;
  1669. if(prefix + 1 + suffix_length <= LEVEL_TAB_BITS){
  1670. cavlc_level_tab[suffix_length][i][0]= level_code;
  1671. cavlc_level_tab[suffix_length][i][1]= prefix + 1 + suffix_length;
  1672. }else if(prefix + 1 <= LEVEL_TAB_BITS){
  1673. cavlc_level_tab[suffix_length][i][0]= prefix+100;
  1674. cavlc_level_tab[suffix_length][i][1]= prefix + 1;
  1675. }else{
  1676. cavlc_level_tab[suffix_length][i][0]= LEVEL_TAB_BITS+100;
  1677. cavlc_level_tab[suffix_length][i][1]= LEVEL_TAB_BITS;
  1678. }
  1679. }
  1680. }
  1681. }
  1682. static av_cold void decode_init_vlc(void){
  1683. static int done = 0;
  1684. if (!done) {
  1685. int i;
  1686. int offset;
  1687. done = 1;
  1688. chroma_dc_coeff_token_vlc.table = chroma_dc_coeff_token_vlc_table;
  1689. chroma_dc_coeff_token_vlc.table_allocated = chroma_dc_coeff_token_vlc_table_size;
  1690. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  1691. &chroma_dc_coeff_token_len [0], 1, 1,
  1692. &chroma_dc_coeff_token_bits[0], 1, 1,
  1693. INIT_VLC_USE_NEW_STATIC);
  1694. offset = 0;
  1695. for(i=0; i<4; i++){
  1696. coeff_token_vlc[i].table = coeff_token_vlc_tables+offset;
  1697. coeff_token_vlc[i].table_allocated = coeff_token_vlc_tables_size[i];
  1698. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  1699. &coeff_token_len [i][0], 1, 1,
  1700. &coeff_token_bits[i][0], 1, 1,
  1701. INIT_VLC_USE_NEW_STATIC);
  1702. offset += coeff_token_vlc_tables_size[i];
  1703. }
  1704. /*
  1705. * This is a one time safety check to make sure that
  1706. * the packed static coeff_token_vlc table sizes
  1707. * were initialized correctly.
  1708. */
  1709. assert(offset == FF_ARRAY_ELEMS(coeff_token_vlc_tables));
  1710. for(i=0; i<3; i++){
  1711. chroma_dc_total_zeros_vlc[i].table = chroma_dc_total_zeros_vlc_tables[i];
  1712. chroma_dc_total_zeros_vlc[i].table_allocated = chroma_dc_total_zeros_vlc_tables_size;
  1713. init_vlc(&chroma_dc_total_zeros_vlc[i],
  1714. CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  1715. &chroma_dc_total_zeros_len [i][0], 1, 1,
  1716. &chroma_dc_total_zeros_bits[i][0], 1, 1,
  1717. INIT_VLC_USE_NEW_STATIC);
  1718. }
  1719. for(i=0; i<15; i++){
  1720. total_zeros_vlc[i].table = total_zeros_vlc_tables[i];
  1721. total_zeros_vlc[i].table_allocated = total_zeros_vlc_tables_size;
  1722. init_vlc(&total_zeros_vlc[i],
  1723. TOTAL_ZEROS_VLC_BITS, 16,
  1724. &total_zeros_len [i][0], 1, 1,
  1725. &total_zeros_bits[i][0], 1, 1,
  1726. INIT_VLC_USE_NEW_STATIC);
  1727. }
  1728. for(i=0; i<6; i++){
  1729. run_vlc[i].table = run_vlc_tables[i];
  1730. run_vlc[i].table_allocated = run_vlc_tables_size;
  1731. init_vlc(&run_vlc[i],
  1732. RUN_VLC_BITS, 7,
  1733. &run_len [i][0], 1, 1,
  1734. &run_bits[i][0], 1, 1,
  1735. INIT_VLC_USE_NEW_STATIC);
  1736. }
  1737. run7_vlc.table = run7_vlc_table,
  1738. run7_vlc.table_allocated = run7_vlc_table_size;
  1739. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  1740. &run_len [6][0], 1, 1,
  1741. &run_bits[6][0], 1, 1,
  1742. INIT_VLC_USE_NEW_STATIC);
  1743. init_cavlc_level_tab();
  1744. }
  1745. }
  1746. static void free_tables(H264Context *h){
  1747. int i;
  1748. H264Context *hx;
  1749. av_freep(&h->intra4x4_pred_mode);
  1750. av_freep(&h->chroma_pred_mode_table);
  1751. av_freep(&h->cbp_table);
  1752. av_freep(&h->mvd_table[0]);
  1753. av_freep(&h->mvd_table[1]);
  1754. av_freep(&h->direct_table);
  1755. av_freep(&h->non_zero_count);
  1756. av_freep(&h->slice_table_base);
  1757. h->slice_table= NULL;
  1758. av_freep(&h->mb2b_xy);
  1759. av_freep(&h->mb2b8_xy);
  1760. for(i = 0; i < h->s.avctx->thread_count; i++) {
  1761. hx = h->thread_context[i];
  1762. if(!hx) continue;
  1763. av_freep(&hx->top_borders[1]);
  1764. av_freep(&hx->top_borders[0]);
  1765. av_freep(&hx->s.obmc_scratchpad);
  1766. }
  1767. }
  1768. static void init_dequant8_coeff_table(H264Context *h){
  1769. int i,q,x;
  1770. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  1771. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  1772. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  1773. for(i=0; i<2; i++ ){
  1774. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  1775. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  1776. break;
  1777. }
  1778. for(q=0; q<52; q++){
  1779. int shift = div6[q];
  1780. int idx = rem6[q];
  1781. for(x=0; x<64; x++)
  1782. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  1783. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  1784. h->pps.scaling_matrix8[i][x]) << shift;
  1785. }
  1786. }
  1787. }
  1788. static void init_dequant4_coeff_table(H264Context *h){
  1789. int i,j,q,x;
  1790. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  1791. for(i=0; i<6; i++ ){
  1792. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  1793. for(j=0; j<i; j++){
  1794. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  1795. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  1796. break;
  1797. }
  1798. }
  1799. if(j<i)
  1800. continue;
  1801. for(q=0; q<52; q++){
  1802. int shift = div6[q] + 2;
  1803. int idx = rem6[q];
  1804. for(x=0; x<16; x++)
  1805. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  1806. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  1807. h->pps.scaling_matrix4[i][x]) << shift;
  1808. }
  1809. }
  1810. }
  1811. static void init_dequant_tables(H264Context *h){
  1812. int i,x;
  1813. init_dequant4_coeff_table(h);
  1814. if(h->pps.transform_8x8_mode)
  1815. init_dequant8_coeff_table(h);
  1816. if(h->sps.transform_bypass){
  1817. for(i=0; i<6; i++)
  1818. for(x=0; x<16; x++)
  1819. h->dequant4_coeff[i][0][x] = 1<<6;
  1820. if(h->pps.transform_8x8_mode)
  1821. for(i=0; i<2; i++)
  1822. for(x=0; x<64; x++)
  1823. h->dequant8_coeff[i][0][x] = 1<<6;
  1824. }
  1825. }
  1826. /**
  1827. * allocates tables.
  1828. * needs width/height
  1829. */
  1830. static int alloc_tables(H264Context *h){
  1831. MpegEncContext * const s = &h->s;
  1832. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  1833. int x,y;
  1834. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  1835. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  1836. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base))
  1837. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  1838. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  1839. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  1840. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  1841. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  1842. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
  1843. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  1844. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  1845. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  1846. for(y=0; y<s->mb_height; y++){
  1847. for(x=0; x<s->mb_width; x++){
  1848. const int mb_xy= x + y*s->mb_stride;
  1849. const int b_xy = 4*x + 4*y*h->b_stride;
  1850. const int b8_xy= 2*x + 2*y*h->b8_stride;
  1851. h->mb2b_xy [mb_xy]= b_xy;
  1852. h->mb2b8_xy[mb_xy]= b8_xy;
  1853. }
  1854. }
  1855. s->obmc_scratchpad = NULL;
  1856. if(!h->dequant4_coeff[0])
  1857. init_dequant_tables(h);
  1858. return 0;
  1859. fail:
  1860. free_tables(h);
  1861. return -1;
  1862. }
  1863. /**
  1864. * Mimic alloc_tables(), but for every context thread.
  1865. */
  1866. static void clone_tables(H264Context *dst, H264Context *src){
  1867. dst->intra4x4_pred_mode = src->intra4x4_pred_mode;
  1868. dst->non_zero_count = src->non_zero_count;
  1869. dst->slice_table = src->slice_table;
  1870. dst->cbp_table = src->cbp_table;
  1871. dst->mb2b_xy = src->mb2b_xy;
  1872. dst->mb2b8_xy = src->mb2b8_xy;
  1873. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  1874. dst->mvd_table[0] = src->mvd_table[0];
  1875. dst->mvd_table[1] = src->mvd_table[1];
  1876. dst->direct_table = src->direct_table;
  1877. dst->s.obmc_scratchpad = NULL;
  1878. ff_h264_pred_init(&dst->hpc, src->s.codec_id);
  1879. }
  1880. /**
  1881. * Init context
  1882. * Allocate buffers which are not shared amongst multiple threads.
  1883. */
  1884. static int context_init(H264Context *h){
  1885. CHECKED_ALLOCZ(h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t))
  1886. CHECKED_ALLOCZ(h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t))
  1887. return 0;
  1888. fail:
  1889. return -1; // free_tables will clean up for us
  1890. }
  1891. static av_cold void common_init(H264Context *h){
  1892. MpegEncContext * const s = &h->s;
  1893. s->width = s->avctx->width;
  1894. s->height = s->avctx->height;
  1895. s->codec_id= s->avctx->codec->id;
  1896. ff_h264_pred_init(&h->hpc, s->codec_id);
  1897. h->dequant_coeff_pps= -1;
  1898. s->unrestricted_mv=1;
  1899. s->decode=1; //FIXME
  1900. dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
  1901. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  1902. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  1903. }
  1904. /**
  1905. * Reset SEI values at the beginning of the frame.
  1906. *
  1907. * @param h H.264 context.
  1908. */
  1909. static void reset_sei(H264Context *h) {
  1910. h->sei_recovery_frame_cnt = -1;
  1911. h->sei_dpb_output_delay = 0;
  1912. h->sei_cpb_removal_delay = -1;
  1913. h->sei_buffering_period_present = 0;
  1914. }
  1915. static av_cold int decode_init(AVCodecContext *avctx){
  1916. H264Context *h= avctx->priv_data;
  1917. MpegEncContext * const s = &h->s;
  1918. MPV_decode_defaults(s);
  1919. s->avctx = avctx;
  1920. common_init(h);
  1921. s->out_format = FMT_H264;
  1922. s->workaround_bugs= avctx->workaround_bugs;
  1923. // set defaults
  1924. // s->decode_mb= ff_h263_decode_mb;
  1925. s->quarter_sample = 1;
  1926. if(!avctx->has_b_frames)
  1927. s->low_delay= 1;
  1928. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1929. avctx->pix_fmt= PIX_FMT_VDPAU_H264;
  1930. else
  1931. avctx->pix_fmt= avctx->get_format(avctx, avctx->codec->pix_fmts);
  1932. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1933. decode_init_vlc();
  1934. if(avctx->extradata_size > 0 && avctx->extradata &&
  1935. *(char *)avctx->extradata == 1){
  1936. h->is_avc = 1;
  1937. h->got_avcC = 0;
  1938. } else {
  1939. h->is_avc = 0;
  1940. }
  1941. h->thread_context[0] = h;
  1942. h->outputed_poc = INT_MIN;
  1943. h->prev_poc_msb= 1<<16;
  1944. reset_sei(h);
  1945. if(avctx->codec_id == CODEC_ID_H264){
  1946. if(avctx->ticks_per_frame == 1){
  1947. s->avctx->time_base.den *=2;
  1948. }
  1949. avctx->ticks_per_frame = 2;
  1950. }
  1951. return 0;
  1952. }
  1953. static int frame_start(H264Context *h){
  1954. MpegEncContext * const s = &h->s;
  1955. int i;
  1956. if(MPV_frame_start(s, s->avctx) < 0)
  1957. return -1;
  1958. ff_er_frame_start(s);
  1959. /*
  1960. * MPV_frame_start uses pict_type to derive key_frame.
  1961. * This is incorrect for H.264; IDR markings must be used.
  1962. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  1963. * See decode_nal_units().
  1964. */
  1965. s->current_picture_ptr->key_frame= 0;
  1966. assert(s->linesize && s->uvlinesize);
  1967. for(i=0; i<16; i++){
  1968. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  1969. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  1970. }
  1971. for(i=0; i<4; i++){
  1972. h->block_offset[16+i]=
  1973. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1974. h->block_offset[24+16+i]=
  1975. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1976. }
  1977. /* can't be in alloc_tables because linesize isn't known there.
  1978. * FIXME: redo bipred weight to not require extra buffer? */
  1979. for(i = 0; i < s->avctx->thread_count; i++)
  1980. if(!h->thread_context[i]->s.obmc_scratchpad)
  1981. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  1982. /* some macroblocks will be accessed before they're available */
  1983. if(FRAME_MBAFF || s->avctx->thread_count > 1)
  1984. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
  1985. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  1986. // We mark the current picture as non-reference after allocating it, so
  1987. // that if we break out due to an error it can be released automatically
  1988. // in the next MPV_frame_start().
  1989. // SVQ3 as well as most other codecs have only last/next/current and thus
  1990. // get released even with set reference, besides SVQ3 and others do not
  1991. // mark frames as reference later "naturally".
  1992. if(s->codec_id != CODEC_ID_SVQ3)
  1993. s->current_picture_ptr->reference= 0;
  1994. s->current_picture_ptr->field_poc[0]=
  1995. s->current_picture_ptr->field_poc[1]= INT_MAX;
  1996. assert(s->current_picture_ptr->long_ref==0);
  1997. return 0;
  1998. }
  1999. 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){
  2000. MpegEncContext * const s = &h->s;
  2001. int i;
  2002. int step = 1;
  2003. int offset = 1;
  2004. int uvoffset= 1;
  2005. int top_idx = 1;
  2006. int skiplast= 0;
  2007. src_y -= linesize;
  2008. src_cb -= uvlinesize;
  2009. src_cr -= uvlinesize;
  2010. if(!simple && FRAME_MBAFF){
  2011. if(s->mb_y&1){
  2012. offset = MB_MBAFF ? 1 : 17;
  2013. uvoffset= MB_MBAFF ? 1 : 9;
  2014. if(!MB_MBAFF){
  2015. *(uint64_t*)(h->top_borders[0][s->mb_x]+ 0)= *(uint64_t*)(src_y + 15*linesize);
  2016. *(uint64_t*)(h->top_borders[0][s->mb_x]+ 8)= *(uint64_t*)(src_y +8+15*linesize);
  2017. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2018. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+7*uvlinesize);
  2019. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+7*uvlinesize);
  2020. }
  2021. }
  2022. }else{
  2023. if(!MB_MBAFF){
  2024. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2025. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2026. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7 ];
  2027. h->left_border[34+18]= h->top_borders[0][s->mb_x][16+8+7];
  2028. }
  2029. skiplast= 1;
  2030. }
  2031. offset =
  2032. uvoffset=
  2033. top_idx = MB_MBAFF ? 0 : 1;
  2034. }
  2035. step= MB_MBAFF ? 2 : 1;
  2036. }
  2037. // There are two lines saved, the line above the the top macroblock of a pair,
  2038. // and the line above the bottom macroblock
  2039. h->left_border[offset]= h->top_borders[top_idx][s->mb_x][15];
  2040. for(i=1; i<17 - skiplast; i++){
  2041. h->left_border[offset+i*step]= src_y[15+i* linesize];
  2042. }
  2043. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2044. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2045. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2046. h->left_border[uvoffset+34 ]= h->top_borders[top_idx][s->mb_x][16+7];
  2047. h->left_border[uvoffset+34+18]= h->top_borders[top_idx][s->mb_x][24+7];
  2048. for(i=1; i<9 - skiplast; i++){
  2049. h->left_border[uvoffset+34 +i*step]= src_cb[7+i*uvlinesize];
  2050. h->left_border[uvoffset+34+18+i*step]= src_cr[7+i*uvlinesize];
  2051. }
  2052. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2053. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2054. }
  2055. }
  2056. static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int simple){
  2057. MpegEncContext * const s = &h->s;
  2058. int temp8, i;
  2059. uint64_t temp64;
  2060. int deblock_left;
  2061. int deblock_top;
  2062. int mb_xy;
  2063. int step = 1;
  2064. int offset = 1;
  2065. int uvoffset= 1;
  2066. int top_idx = 1;
  2067. if(!simple && FRAME_MBAFF){
  2068. if(s->mb_y&1){
  2069. offset = MB_MBAFF ? 1 : 17;
  2070. uvoffset= MB_MBAFF ? 1 : 9;
  2071. }else{
  2072. offset =
  2073. uvoffset=
  2074. top_idx = MB_MBAFF ? 0 : 1;
  2075. }
  2076. step= MB_MBAFF ? 2 : 1;
  2077. }
  2078. if(h->deblocking_filter == 2) {
  2079. mb_xy = h->mb_xy;
  2080. deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1];
  2081. deblock_top = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy];
  2082. } else {
  2083. deblock_left = (s->mb_x > 0);
  2084. deblock_top = (s->mb_y > !!MB_FIELD);
  2085. }
  2086. src_y -= linesize + 1;
  2087. src_cb -= uvlinesize + 1;
  2088. src_cr -= uvlinesize + 1;
  2089. #define XCHG(a,b,t,xchg)\
  2090. t= a;\
  2091. if(xchg)\
  2092. a= b;\
  2093. b= t;
  2094. if(deblock_left){
  2095. for(i = !deblock_top; i<16; i++){
  2096. XCHG(h->left_border[offset+i*step], src_y [i* linesize], temp8, xchg);
  2097. }
  2098. XCHG(h->left_border[offset+i*step], src_y [i* linesize], temp8, 1);
  2099. }
  2100. if(deblock_top){
  2101. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2102. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2103. if(s->mb_x+1 < s->mb_width){
  2104. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2105. }
  2106. }
  2107. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2108. if(deblock_left){
  2109. for(i = !deblock_top; i<8; i++){
  2110. XCHG(h->left_border[uvoffset+34 +i*step], src_cb[i*uvlinesize], temp8, xchg);
  2111. XCHG(h->left_border[uvoffset+34+18+i*step], src_cr[i*uvlinesize], temp8, xchg);
  2112. }
  2113. XCHG(h->left_border[uvoffset+34 +i*step], src_cb[i*uvlinesize], temp8, 1);
  2114. XCHG(h->left_border[uvoffset+34+18+i*step], src_cr[i*uvlinesize], temp8, 1);
  2115. }
  2116. if(deblock_top){
  2117. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2118. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2119. }
  2120. }
  2121. }
  2122. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
  2123. MpegEncContext * const s = &h->s;
  2124. const int mb_x= s->mb_x;
  2125. const int mb_y= s->mb_y;
  2126. const int mb_xy= h->mb_xy;
  2127. const int mb_type= s->current_picture.mb_type[mb_xy];
  2128. uint8_t *dest_y, *dest_cb, *dest_cr;
  2129. int linesize, uvlinesize /*dct_offset*/;
  2130. int i;
  2131. int *block_offset = &h->block_offset[0];
  2132. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  2133. /* is_h264 should always be true if SVQ3 is disabled. */
  2134. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  2135. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  2136. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  2137. dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
  2138. dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
  2139. dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
  2140. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  2141. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  2142. if (!simple && MB_FIELD) {
  2143. linesize = h->mb_linesize = s->linesize * 2;
  2144. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2145. block_offset = &h->block_offset[24];
  2146. if(mb_y&1){ //FIXME move out of this function?
  2147. dest_y -= s->linesize*15;
  2148. dest_cb-= s->uvlinesize*7;
  2149. dest_cr-= s->uvlinesize*7;
  2150. }
  2151. if(FRAME_MBAFF) {
  2152. int list;
  2153. for(list=0; list<h->list_count; list++){
  2154. if(!USES_LIST(mb_type, list))
  2155. continue;
  2156. if(IS_16X16(mb_type)){
  2157. int8_t *ref = &h->ref_cache[list][scan8[0]];
  2158. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  2159. }else{
  2160. for(i=0; i<16; i+=4){
  2161. int ref = h->ref_cache[list][scan8[i]];
  2162. if(ref >= 0)
  2163. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  2164. }
  2165. }
  2166. }
  2167. }
  2168. } else {
  2169. linesize = h->mb_linesize = s->linesize;
  2170. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2171. // dct_offset = s->linesize * 16;
  2172. }
  2173. if (!simple && IS_INTRA_PCM(mb_type)) {
  2174. for (i=0; i<16; i++) {
  2175. memcpy(dest_y + i* linesize, h->mb + i*8, 16);
  2176. }
  2177. for (i=0; i<8; i++) {
  2178. memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
  2179. memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
  2180. }
  2181. } else {
  2182. if(IS_INTRA(mb_type)){
  2183. if(h->deblocking_filter)
  2184. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
  2185. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2186. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  2187. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  2188. }
  2189. if(IS_INTRA4x4(mb_type)){
  2190. if(simple || !s->encoding){
  2191. if(IS_8x8DCT(mb_type)){
  2192. if(transform_bypass){
  2193. idct_dc_add =
  2194. idct_add = s->dsp.add_pixels8;
  2195. }else{
  2196. idct_dc_add = s->dsp.h264_idct8_dc_add;
  2197. idct_add = s->dsp.h264_idct8_add;
  2198. }
  2199. for(i=0; i<16; i+=4){
  2200. uint8_t * const ptr= dest_y + block_offset[i];
  2201. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2202. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  2203. h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);
  2204. }else{
  2205. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  2206. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  2207. (h->topright_samples_available<<i)&0x4000, linesize);
  2208. if(nnz){
  2209. if(nnz == 1 && h->mb[i*16])
  2210. idct_dc_add(ptr, h->mb + i*16, linesize);
  2211. else
  2212. idct_add (ptr, h->mb + i*16, linesize);
  2213. }
  2214. }
  2215. }
  2216. }else{
  2217. if(transform_bypass){
  2218. idct_dc_add =
  2219. idct_add = s->dsp.add_pixels4;
  2220. }else{
  2221. idct_dc_add = s->dsp.h264_idct_dc_add;
  2222. idct_add = s->dsp.h264_idct_add;
  2223. }
  2224. for(i=0; i<16; i++){
  2225. uint8_t * const ptr= dest_y + block_offset[i];
  2226. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2227. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  2228. h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);
  2229. }else{
  2230. uint8_t *topright;
  2231. int nnz, tr;
  2232. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  2233. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  2234. assert(mb_y || linesize <= block_offset[i]);
  2235. if(!topright_avail){
  2236. tr= ptr[3 - linesize]*0x01010101;
  2237. topright= (uint8_t*) &tr;
  2238. }else
  2239. topright= ptr + 4 - linesize;
  2240. }else
  2241. topright= NULL;
  2242. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  2243. nnz = h->non_zero_count_cache[ scan8[i] ];
  2244. if(nnz){
  2245. if(is_h264){
  2246. if(nnz == 1 && h->mb[i*16])
  2247. idct_dc_add(ptr, h->mb + i*16, linesize);
  2248. else
  2249. idct_add (ptr, h->mb + i*16, linesize);
  2250. }else
  2251. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  2252. }
  2253. }
  2254. }
  2255. }
  2256. }
  2257. }else{
  2258. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  2259. if(is_h264){
  2260. if(!transform_bypass)
  2261. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
  2262. }else
  2263. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  2264. }
  2265. if(h->deblocking_filter)
  2266. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
  2267. }else if(is_h264){
  2268. hl_motion(h, dest_y, dest_cb, dest_cr,
  2269. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  2270. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  2271. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  2272. }
  2273. if(!IS_INTRA4x4(mb_type)){
  2274. if(is_h264){
  2275. if(IS_INTRA16x16(mb_type)){
  2276. if(transform_bypass){
  2277. if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
  2278. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
  2279. }else{
  2280. for(i=0; i<16; i++){
  2281. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
  2282. s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);
  2283. }
  2284. }
  2285. }else{
  2286. s->dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  2287. }
  2288. }else if(h->cbp&15){
  2289. if(transform_bypass){
  2290. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  2291. idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  2292. for(i=0; i<16; i+=di){
  2293. if(h->non_zero_count_cache[ scan8[i] ]){
  2294. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2295. }
  2296. }
  2297. }else{
  2298. if(IS_8x8DCT(mb_type)){
  2299. s->dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  2300. }else{
  2301. s->dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  2302. }
  2303. }
  2304. }
  2305. }else{
  2306. for(i=0; i<16; i++){
  2307. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  2308. uint8_t * const ptr= dest_y + block_offset[i];
  2309. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  2310. }
  2311. }
  2312. }
  2313. }
  2314. if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
  2315. uint8_t *dest[2] = {dest_cb, dest_cr};
  2316. if(transform_bypass){
  2317. if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
  2318. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);
  2319. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);
  2320. }else{
  2321. idct_add = s->dsp.add_pixels4;
  2322. for(i=16; i<16+8; i++){
  2323. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
  2324. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  2325. }
  2326. }
  2327. }else{
  2328. chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp[0], h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
  2329. chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp[1], h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
  2330. if(is_h264){
  2331. idct_add = s->dsp.h264_idct_add;
  2332. idct_dc_add = s->dsp.h264_idct_dc_add;
  2333. for(i=16; i<16+8; i++){
  2334. if(h->non_zero_count_cache[ scan8[i] ])
  2335. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  2336. else if(h->mb[i*16])
  2337. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  2338. }
  2339. }else{
  2340. for(i=16; i<16+8; i++){
  2341. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2342. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  2343. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2344. }
  2345. }
  2346. }
  2347. }
  2348. }
  2349. }
  2350. if(h->cbp || IS_INTRA(mb_type))
  2351. s->dsp.clear_blocks(h->mb);
  2352. if(h->deblocking_filter) {
  2353. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple);
  2354. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  2355. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  2356. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  2357. if (!simple && FRAME_MBAFF) {
  2358. filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2359. } else {
  2360. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2361. }
  2362. }
  2363. }
  2364. /**
  2365. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  2366. */
  2367. static void hl_decode_mb_simple(H264Context *h){
  2368. hl_decode_mb_internal(h, 1);
  2369. }
  2370. /**
  2371. * Process a macroblock; this handles edge cases, such as interlacing.
  2372. */
  2373. static void av_noinline hl_decode_mb_complex(H264Context *h){
  2374. hl_decode_mb_internal(h, 0);
  2375. }
  2376. static void hl_decode_mb(H264Context *h){
  2377. MpegEncContext * const s = &h->s;
  2378. const int mb_xy= h->mb_xy;
  2379. const int mb_type= s->current_picture.mb_type[mb_xy];
  2380. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  2381. if (is_complex)
  2382. hl_decode_mb_complex(h);
  2383. else hl_decode_mb_simple(h);
  2384. }
  2385. static void pic_as_field(Picture *pic, const int parity){
  2386. int i;
  2387. for (i = 0; i < 4; ++i) {
  2388. if (parity == PICT_BOTTOM_FIELD)
  2389. pic->data[i] += pic->linesize[i];
  2390. pic->reference = parity;
  2391. pic->linesize[i] *= 2;
  2392. }
  2393. pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
  2394. }
  2395. static int split_field_copy(Picture *dest, Picture *src,
  2396. int parity, int id_add){
  2397. int match = !!(src->reference & parity);
  2398. if (match) {
  2399. *dest = *src;
  2400. if(parity != PICT_FRAME){
  2401. pic_as_field(dest, parity);
  2402. dest->pic_id *= 2;
  2403. dest->pic_id += id_add;
  2404. }
  2405. }
  2406. return match;
  2407. }
  2408. static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
  2409. int i[2]={0};
  2410. int index=0;
  2411. while(i[0]<len || i[1]<len){
  2412. while(i[0]<len && !(in[ i[0] ] && (in[ i[0] ]->reference & sel)))
  2413. i[0]++;
  2414. while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->reference & (sel^3))))
  2415. i[1]++;
  2416. if(i[0] < len){
  2417. in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
  2418. split_field_copy(&def[index++], in[ i[0]++ ], sel , 1);
  2419. }
  2420. if(i[1] < len){
  2421. in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
  2422. split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
  2423. }
  2424. }
  2425. return index;
  2426. }
  2427. static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
  2428. int i, best_poc;
  2429. int out_i= 0;
  2430. for(;;){
  2431. best_poc= dir ? INT_MIN : INT_MAX;
  2432. for(i=0; i<len; i++){
  2433. const int poc= src[i]->poc;
  2434. if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
  2435. best_poc= poc;
  2436. sorted[out_i]= src[i];
  2437. }
  2438. }
  2439. if(best_poc == (dir ? INT_MIN : INT_MAX))
  2440. break;
  2441. limit= sorted[out_i++]->poc - dir;
  2442. }
  2443. return out_i;
  2444. }
  2445. /**
  2446. * fills the default_ref_list.
  2447. */
  2448. static int fill_default_ref_list(H264Context *h){
  2449. MpegEncContext * const s = &h->s;
  2450. int i, len;
  2451. if(h->slice_type_nos==FF_B_TYPE){
  2452. Picture *sorted[32];
  2453. int cur_poc, list;
  2454. int lens[2];
  2455. if(FIELD_PICTURE)
  2456. cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
  2457. else
  2458. cur_poc= s->current_picture_ptr->poc;
  2459. for(list= 0; list<2; list++){
  2460. len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
  2461. len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
  2462. assert(len<=32);
  2463. len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);
  2464. len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
  2465. assert(len<=32);
  2466. if(len < h->ref_count[list])
  2467. memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
  2468. lens[list]= len;
  2469. }
  2470. if(lens[0] == lens[1] && lens[1] > 1){
  2471. for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);
  2472. if(i == lens[0])
  2473. FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
  2474. }
  2475. }else{
  2476. len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);
  2477. len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);
  2478. assert(len <= 32);
  2479. if(len < h->ref_count[0])
  2480. memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
  2481. }
  2482. #ifdef TRACE
  2483. for (i=0; i<h->ref_count[0]; i++) {
  2484. tprintf(h->s.avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
  2485. }
  2486. if(h->slice_type_nos==FF_B_TYPE){
  2487. for (i=0; i<h->ref_count[1]; i++) {
  2488. tprintf(h->s.avctx, "List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[1][i].data[0]);
  2489. }
  2490. }
  2491. #endif
  2492. return 0;
  2493. }
  2494. static void print_short_term(H264Context *h);
  2495. static void print_long_term(H264Context *h);
  2496. /**
  2497. * Extract structure information about the picture described by pic_num in
  2498. * the current decoding context (frame or field). Note that pic_num is
  2499. * picture number without wrapping (so, 0<=pic_num<max_pic_num).
  2500. * @param pic_num picture number for which to extract structure information
  2501. * @param structure one of PICT_XXX describing structure of picture
  2502. * with pic_num
  2503. * @return frame number (short term) or long term index of picture
  2504. * described by pic_num
  2505. */
  2506. static int pic_num_extract(H264Context *h, int pic_num, int *structure){
  2507. MpegEncContext * const s = &h->s;
  2508. *structure = s->picture_structure;
  2509. if(FIELD_PICTURE){
  2510. if (!(pic_num & 1))
  2511. /* opposite field */
  2512. *structure ^= PICT_FRAME;
  2513. pic_num >>= 1;
  2514. }
  2515. return pic_num;
  2516. }
  2517. static int decode_ref_pic_list_reordering(H264Context *h){
  2518. MpegEncContext * const s = &h->s;
  2519. int list, index, pic_structure;
  2520. print_short_term(h);
  2521. print_long_term(h);
  2522. for(list=0; list<h->list_count; list++){
  2523. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  2524. if(get_bits1(&s->gb)){
  2525. int pred= h->curr_pic_num;
  2526. for(index=0; ; index++){
  2527. unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
  2528. unsigned int pic_id;
  2529. int i;
  2530. Picture *ref = NULL;
  2531. if(reordering_of_pic_nums_idc==3)
  2532. break;
  2533. if(index >= h->ref_count[list]){
  2534. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  2535. return -1;
  2536. }
  2537. if(reordering_of_pic_nums_idc<3){
  2538. if(reordering_of_pic_nums_idc<2){
  2539. const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  2540. int frame_num;
  2541. if(abs_diff_pic_num > h->max_pic_num){
  2542. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  2543. return -1;
  2544. }
  2545. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  2546. else pred+= abs_diff_pic_num;
  2547. pred &= h->max_pic_num - 1;
  2548. frame_num = pic_num_extract(h, pred, &pic_structure);
  2549. for(i= h->short_ref_count-1; i>=0; i--){
  2550. ref = h->short_ref[i];
  2551. assert(ref->reference);
  2552. assert(!ref->long_ref);
  2553. if(
  2554. ref->frame_num == frame_num &&
  2555. (ref->reference & pic_structure)
  2556. )
  2557. break;
  2558. }
  2559. if(i>=0)
  2560. ref->pic_id= pred;
  2561. }else{
  2562. int long_idx;
  2563. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  2564. long_idx= pic_num_extract(h, pic_id, &pic_structure);
  2565. if(long_idx>31){
  2566. av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  2567. return -1;
  2568. }
  2569. ref = h->long_ref[long_idx];
  2570. assert(!(ref && !ref->reference));
  2571. if(ref && (ref->reference & pic_structure)){
  2572. ref->pic_id= pic_id;
  2573. assert(ref->long_ref);
  2574. i=0;
  2575. }else{
  2576. i=-1;
  2577. }
  2578. }
  2579. if (i < 0) {
  2580. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  2581. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  2582. } else {
  2583. for(i=index; i+1<h->ref_count[list]; i++){
  2584. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  2585. break;
  2586. }
  2587. for(; i > index; i--){
  2588. h->ref_list[list][i]= h->ref_list[list][i-1];
  2589. }
  2590. h->ref_list[list][index]= *ref;
  2591. if (FIELD_PICTURE){
  2592. pic_as_field(&h->ref_list[list][index], pic_structure);
  2593. }
  2594. }
  2595. }else{
  2596. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  2597. return -1;
  2598. }
  2599. }
  2600. }
  2601. }
  2602. for(list=0; list<h->list_count; list++){
  2603. for(index= 0; index < h->ref_count[list]; index++){
  2604. if(!h->ref_list[list][index].data[0]){
  2605. av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
  2606. h->ref_list[list][index]= s->current_picture; //FIXME this is not a sensible solution
  2607. }
  2608. }
  2609. }
  2610. return 0;
  2611. }
  2612. static void fill_mbaff_ref_list(H264Context *h){
  2613. int list, i, j;
  2614. for(list=0; list<2; list++){ //FIXME try list_count
  2615. for(i=0; i<h->ref_count[list]; i++){
  2616. Picture *frame = &h->ref_list[list][i];
  2617. Picture *field = &h->ref_list[list][16+2*i];
  2618. field[0] = *frame;
  2619. for(j=0; j<3; j++)
  2620. field[0].linesize[j] <<= 1;
  2621. field[0].reference = PICT_TOP_FIELD;
  2622. field[0].poc= field[0].field_poc[0];
  2623. field[1] = field[0];
  2624. for(j=0; j<3; j++)
  2625. field[1].data[j] += frame->linesize[j];
  2626. field[1].reference = PICT_BOTTOM_FIELD;
  2627. field[1].poc= field[1].field_poc[1];
  2628. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  2629. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  2630. for(j=0; j<2; j++){
  2631. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  2632. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  2633. }
  2634. }
  2635. }
  2636. for(j=0; j<h->ref_count[1]; j++){
  2637. for(i=0; i<h->ref_count[0]; i++)
  2638. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  2639. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  2640. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  2641. }
  2642. }
  2643. static int pred_weight_table(H264Context *h){
  2644. MpegEncContext * const s = &h->s;
  2645. int list, i;
  2646. int luma_def, chroma_def;
  2647. h->use_weight= 0;
  2648. h->use_weight_chroma= 0;
  2649. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  2650. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  2651. luma_def = 1<<h->luma_log2_weight_denom;
  2652. chroma_def = 1<<h->chroma_log2_weight_denom;
  2653. for(list=0; list<2; list++){
  2654. h->luma_weight_flag[list] = 0;
  2655. h->chroma_weight_flag[list] = 0;
  2656. for(i=0; i<h->ref_count[list]; i++){
  2657. int luma_weight_flag, chroma_weight_flag;
  2658. luma_weight_flag= get_bits1(&s->gb);
  2659. if(luma_weight_flag){
  2660. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  2661. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  2662. if( h->luma_weight[list][i] != luma_def
  2663. || h->luma_offset[list][i] != 0) {
  2664. h->use_weight= 1;
  2665. h->luma_weight_flag[list]= 1;
  2666. }
  2667. }else{
  2668. h->luma_weight[list][i]= luma_def;
  2669. h->luma_offset[list][i]= 0;
  2670. }
  2671. if(CHROMA){
  2672. chroma_weight_flag= get_bits1(&s->gb);
  2673. if(chroma_weight_flag){
  2674. int j;
  2675. for(j=0; j<2; j++){
  2676. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  2677. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  2678. if( h->chroma_weight[list][i][j] != chroma_def
  2679. || h->chroma_offset[list][i][j] != 0) {
  2680. h->use_weight_chroma= 1;
  2681. h->chroma_weight_flag[list]= 1;
  2682. }
  2683. }
  2684. }else{
  2685. int j;
  2686. for(j=0; j<2; j++){
  2687. h->chroma_weight[list][i][j]= chroma_def;
  2688. h->chroma_offset[list][i][j]= 0;
  2689. }
  2690. }
  2691. }
  2692. }
  2693. if(h->slice_type_nos != FF_B_TYPE) break;
  2694. }
  2695. h->use_weight= h->use_weight || h->use_weight_chroma;
  2696. return 0;
  2697. }
  2698. static void implicit_weight_table(H264Context *h){
  2699. MpegEncContext * const s = &h->s;
  2700. int ref0, ref1, i;
  2701. int cur_poc = s->current_picture_ptr->poc;
  2702. for (i = 0; i < 2; i++) {
  2703. h->luma_weight_flag[i] = 0;
  2704. h->chroma_weight_flag[i] = 0;
  2705. }
  2706. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  2707. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  2708. h->use_weight= 0;
  2709. h->use_weight_chroma= 0;
  2710. return;
  2711. }
  2712. h->use_weight= 2;
  2713. h->use_weight_chroma= 2;
  2714. h->luma_log2_weight_denom= 5;
  2715. h->chroma_log2_weight_denom= 5;
  2716. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  2717. int poc0 = h->ref_list[0][ref0].poc;
  2718. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  2719. int poc1 = h->ref_list[1][ref1].poc;
  2720. int td = av_clip(poc1 - poc0, -128, 127);
  2721. if(td){
  2722. int tb = av_clip(cur_poc - poc0, -128, 127);
  2723. int tx = (16384 + (FFABS(td) >> 1)) / td;
  2724. int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  2725. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  2726. h->implicit_weight[ref0][ref1] = 32;
  2727. else
  2728. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  2729. }else
  2730. h->implicit_weight[ref0][ref1] = 32;
  2731. }
  2732. }
  2733. }
  2734. /**
  2735. * Mark a picture as no longer needed for reference. The refmask
  2736. * argument allows unreferencing of individual fields or the whole frame.
  2737. * If the picture becomes entirely unreferenced, but is being held for
  2738. * display purposes, it is marked as such.
  2739. * @param refmask mask of fields to unreference; the mask is bitwise
  2740. * anded with the reference marking of pic
  2741. * @return non-zero if pic becomes entirely unreferenced (except possibly
  2742. * for display purposes) zero if one of the fields remains in
  2743. * reference
  2744. */
  2745. static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
  2746. int i;
  2747. if (pic->reference &= refmask) {
  2748. return 0;
  2749. } else {
  2750. for(i = 0; h->delayed_pic[i]; i++)
  2751. if(pic == h->delayed_pic[i]){
  2752. pic->reference=DELAYED_PIC_REF;
  2753. break;
  2754. }
  2755. return 1;
  2756. }
  2757. }
  2758. /**
  2759. * instantaneous decoder refresh.
  2760. */
  2761. static void idr(H264Context *h){
  2762. int i;
  2763. for(i=0; i<16; i++){
  2764. remove_long(h, i, 0);
  2765. }
  2766. assert(h->long_ref_count==0);
  2767. for(i=0; i<h->short_ref_count; i++){
  2768. unreference_pic(h, h->short_ref[i], 0);
  2769. h->short_ref[i]= NULL;
  2770. }
  2771. h->short_ref_count=0;
  2772. h->prev_frame_num= 0;
  2773. h->prev_frame_num_offset= 0;
  2774. h->prev_poc_msb=
  2775. h->prev_poc_lsb= 0;
  2776. }
  2777. /* forget old pics after a seek */
  2778. static void flush_dpb(AVCodecContext *avctx){
  2779. H264Context *h= avctx->priv_data;
  2780. int i;
  2781. for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
  2782. if(h->delayed_pic[i])
  2783. h->delayed_pic[i]->reference= 0;
  2784. h->delayed_pic[i]= NULL;
  2785. }
  2786. h->outputed_poc= INT_MIN;
  2787. idr(h);
  2788. if(h->s.current_picture_ptr)
  2789. h->s.current_picture_ptr->reference= 0;
  2790. h->s.first_field= 0;
  2791. reset_sei(h);
  2792. ff_mpeg_flush(avctx);
  2793. }
  2794. /**
  2795. * Find a Picture in the short term reference list by frame number.
  2796. * @param frame_num frame number to search for
  2797. * @param idx the index into h->short_ref where returned picture is found
  2798. * undefined if no picture found.
  2799. * @return pointer to the found picture, or NULL if no pic with the provided
  2800. * frame number is found
  2801. */
  2802. static Picture * find_short(H264Context *h, int frame_num, int *idx){
  2803. MpegEncContext * const s = &h->s;
  2804. int i;
  2805. for(i=0; i<h->short_ref_count; i++){
  2806. Picture *pic= h->short_ref[i];
  2807. if(s->avctx->debug&FF_DEBUG_MMCO)
  2808. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  2809. if(pic->frame_num == frame_num) {
  2810. *idx = i;
  2811. return pic;
  2812. }
  2813. }
  2814. return NULL;
  2815. }
  2816. /**
  2817. * Remove a picture from the short term reference list by its index in
  2818. * that list. This does no checking on the provided index; it is assumed
  2819. * to be valid. Other list entries are shifted down.
  2820. * @param i index into h->short_ref of picture to remove.
  2821. */
  2822. static void remove_short_at_index(H264Context *h, int i){
  2823. assert(i >= 0 && i < h->short_ref_count);
  2824. h->short_ref[i]= NULL;
  2825. if (--h->short_ref_count)
  2826. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
  2827. }
  2828. /**
  2829. *
  2830. * @return the removed picture or NULL if an error occurs
  2831. */
  2832. static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
  2833. MpegEncContext * const s = &h->s;
  2834. Picture *pic;
  2835. int i;
  2836. if(s->avctx->debug&FF_DEBUG_MMCO)
  2837. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  2838. pic = find_short(h, frame_num, &i);
  2839. if (pic){
  2840. if(unreference_pic(h, pic, ref_mask))
  2841. remove_short_at_index(h, i);
  2842. }
  2843. return pic;
  2844. }
  2845. /**
  2846. * Remove a picture from the long term reference list by its index in
  2847. * that list.
  2848. * @return the removed picture or NULL if an error occurs
  2849. */
  2850. static Picture * remove_long(H264Context *h, int i, int ref_mask){
  2851. Picture *pic;
  2852. pic= h->long_ref[i];
  2853. if (pic){
  2854. if(unreference_pic(h, pic, ref_mask)){
  2855. assert(h->long_ref[i]->long_ref == 1);
  2856. h->long_ref[i]->long_ref= 0;
  2857. h->long_ref[i]= NULL;
  2858. h->long_ref_count--;
  2859. }
  2860. }
  2861. return pic;
  2862. }
  2863. /**
  2864. * print short term list
  2865. */
  2866. static void print_short_term(H264Context *h) {
  2867. uint32_t i;
  2868. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  2869. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  2870. for(i=0; i<h->short_ref_count; i++){
  2871. Picture *pic= h->short_ref[i];
  2872. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  2873. }
  2874. }
  2875. }
  2876. /**
  2877. * print long term list
  2878. */
  2879. static void print_long_term(H264Context *h) {
  2880. uint32_t i;
  2881. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  2882. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  2883. for(i = 0; i < 16; i++){
  2884. Picture *pic= h->long_ref[i];
  2885. if (pic) {
  2886. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  2887. }
  2888. }
  2889. }
  2890. }
  2891. /**
  2892. * Executes the reference picture marking (memory management control operations).
  2893. */
  2894. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  2895. MpegEncContext * const s = &h->s;
  2896. int i, j;
  2897. int current_ref_assigned=0;
  2898. Picture *av_uninit(pic);
  2899. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  2900. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  2901. for(i=0; i<mmco_count; i++){
  2902. int structure, av_uninit(frame_num);
  2903. if(s->avctx->debug&FF_DEBUG_MMCO)
  2904. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_pic_num, h->mmco[i].long_arg);
  2905. if( mmco[i].opcode == MMCO_SHORT2UNUSED
  2906. || mmco[i].opcode == MMCO_SHORT2LONG){
  2907. frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
  2908. pic = find_short(h, frame_num, &j);
  2909. if(!pic){
  2910. if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
  2911. || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)
  2912. av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
  2913. continue;
  2914. }
  2915. }
  2916. switch(mmco[i].opcode){
  2917. case MMCO_SHORT2UNUSED:
  2918. if(s->avctx->debug&FF_DEBUG_MMCO)
  2919. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n", h->mmco[i].short_pic_num, h->short_ref_count);
  2920. remove_short(h, frame_num, structure ^ PICT_FRAME);
  2921. break;
  2922. case MMCO_SHORT2LONG:
  2923. if (h->long_ref[mmco[i].long_arg] != pic)
  2924. remove_long(h, mmco[i].long_arg, 0);
  2925. remove_short_at_index(h, j);
  2926. h->long_ref[ mmco[i].long_arg ]= pic;
  2927. if (h->long_ref[ mmco[i].long_arg ]){
  2928. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  2929. h->long_ref_count++;
  2930. }
  2931. break;
  2932. case MMCO_LONG2UNUSED:
  2933. j = pic_num_extract(h, mmco[i].long_arg, &structure);
  2934. pic = h->long_ref[j];
  2935. if (pic) {
  2936. remove_long(h, j, structure ^ PICT_FRAME);
  2937. } else if(s->avctx->debug&FF_DEBUG_MMCO)
  2938. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
  2939. break;
  2940. case MMCO_LONG:
  2941. // Comment below left from previous code as it is an interresting note.
  2942. /* First field in pair is in short term list or
  2943. * at a different long term index.
  2944. * This is not allowed; see 7.4.3.3, notes 2 and 3.
  2945. * Report the problem and keep the pair where it is,
  2946. * and mark this field valid.
  2947. */
  2948. if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
  2949. remove_long(h, mmco[i].long_arg, 0);
  2950. h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
  2951. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  2952. h->long_ref_count++;
  2953. }
  2954. s->current_picture_ptr->reference |= s->picture_structure;
  2955. current_ref_assigned=1;
  2956. break;
  2957. case MMCO_SET_MAX_LONG:
  2958. assert(mmco[i].long_arg <= 16);
  2959. // just remove the long term which index is greater than new max
  2960. for(j = mmco[i].long_arg; j<16; j++){
  2961. remove_long(h, j, 0);
  2962. }
  2963. break;
  2964. case MMCO_RESET:
  2965. while(h->short_ref_count){
  2966. remove_short(h, h->short_ref[0]->frame_num, 0);
  2967. }
  2968. for(j = 0; j < 16; j++) {
  2969. remove_long(h, j, 0);
  2970. }
  2971. s->current_picture_ptr->poc=
  2972. s->current_picture_ptr->field_poc[0]=
  2973. s->current_picture_ptr->field_poc[1]=
  2974. h->poc_lsb=
  2975. h->poc_msb=
  2976. h->frame_num=
  2977. s->current_picture_ptr->frame_num= 0;
  2978. break;
  2979. default: assert(0);
  2980. }
  2981. }
  2982. if (!current_ref_assigned) {
  2983. /* Second field of complementary field pair; the first field of
  2984. * which is already referenced. If short referenced, it
  2985. * should be first entry in short_ref. If not, it must exist
  2986. * in long_ref; trying to put it on the short list here is an
  2987. * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
  2988. */
  2989. if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
  2990. /* Just mark the second field valid */
  2991. s->current_picture_ptr->reference = PICT_FRAME;
  2992. } else if (s->current_picture_ptr->long_ref) {
  2993. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
  2994. "assignment for second field "
  2995. "in complementary field pair "
  2996. "(first field is long term)\n");
  2997. } else {
  2998. pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
  2999. if(pic){
  3000. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3001. }
  3002. if(h->short_ref_count)
  3003. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3004. h->short_ref[0]= s->current_picture_ptr;
  3005. h->short_ref_count++;
  3006. s->current_picture_ptr->reference |= s->picture_structure;
  3007. }
  3008. }
  3009. if (h->long_ref_count + h->short_ref_count > h->sps.ref_frame_count){
  3010. /* We have too many reference frames, probably due to corrupted
  3011. * stream. Need to discard one frame. Prevents overrun of the
  3012. * short_ref and long_ref buffers.
  3013. */
  3014. av_log(h->s.avctx, AV_LOG_ERROR,
  3015. "number of reference frames exceeds max (probably "
  3016. "corrupt input), discarding one\n");
  3017. if (h->long_ref_count && !h->short_ref_count) {
  3018. for (i = 0; i < 16; ++i)
  3019. if (h->long_ref[i])
  3020. break;
  3021. assert(i < 16);
  3022. remove_long(h, i, 0);
  3023. } else {
  3024. pic = h->short_ref[h->short_ref_count - 1];
  3025. remove_short(h, pic->frame_num, 0);
  3026. }
  3027. }
  3028. print_short_term(h);
  3029. print_long_term(h);
  3030. return 0;
  3031. }
  3032. static int decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
  3033. MpegEncContext * const s = &h->s;
  3034. int i;
  3035. h->mmco_index= 0;
  3036. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3037. s->broken_link= get_bits1(gb) -1;
  3038. if(get_bits1(gb)){
  3039. h->mmco[0].opcode= MMCO_LONG;
  3040. h->mmco[0].long_arg= 0;
  3041. h->mmco_index= 1;
  3042. }
  3043. }else{
  3044. if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
  3045. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3046. MMCOOpcode opcode= get_ue_golomb_31(gb);
  3047. h->mmco[i].opcode= opcode;
  3048. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3049. h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
  3050. /* if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
  3051. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3052. return -1;
  3053. }*/
  3054. }
  3055. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3056. unsigned int long_arg= get_ue_golomb_31(gb);
  3057. if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
  3058. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3059. return -1;
  3060. }
  3061. h->mmco[i].long_arg= long_arg;
  3062. }
  3063. if(opcode > (unsigned)MMCO_LONG){
  3064. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3065. return -1;
  3066. }
  3067. if(opcode == MMCO_END)
  3068. break;
  3069. }
  3070. h->mmco_index= i;
  3071. }else{
  3072. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3073. if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
  3074. !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
  3075. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3076. h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3077. h->mmco_index= 1;
  3078. if (FIELD_PICTURE) {
  3079. h->mmco[0].short_pic_num *= 2;
  3080. h->mmco[1].opcode= MMCO_SHORT2UNUSED;
  3081. h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
  3082. h->mmco_index= 2;
  3083. }
  3084. }
  3085. }
  3086. }
  3087. return 0;
  3088. }
  3089. static int init_poc(H264Context *h){
  3090. MpegEncContext * const s = &h->s;
  3091. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3092. int field_poc[2];
  3093. Picture *cur = s->current_picture_ptr;
  3094. h->frame_num_offset= h->prev_frame_num_offset;
  3095. if(h->frame_num < h->prev_frame_num)
  3096. h->frame_num_offset += max_frame_num;
  3097. if(h->sps.poc_type==0){
  3098. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3099. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3100. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3101. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3102. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3103. else
  3104. h->poc_msb = h->prev_poc_msb;
  3105. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3106. field_poc[0] =
  3107. field_poc[1] = h->poc_msb + h->poc_lsb;
  3108. if(s->picture_structure == PICT_FRAME)
  3109. field_poc[1] += h->delta_poc_bottom;
  3110. }else if(h->sps.poc_type==1){
  3111. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3112. int i;
  3113. if(h->sps.poc_cycle_length != 0)
  3114. abs_frame_num = h->frame_num_offset + h->frame_num;
  3115. else
  3116. abs_frame_num = 0;
  3117. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3118. abs_frame_num--;
  3119. expected_delta_per_poc_cycle = 0;
  3120. for(i=0; i < h->sps.poc_cycle_length; i++)
  3121. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3122. if(abs_frame_num > 0){
  3123. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3124. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3125. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3126. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3127. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3128. } else
  3129. expectedpoc = 0;
  3130. if(h->nal_ref_idc == 0)
  3131. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3132. field_poc[0] = expectedpoc + h->delta_poc[0];
  3133. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3134. if(s->picture_structure == PICT_FRAME)
  3135. field_poc[1] += h->delta_poc[1];
  3136. }else{
  3137. int poc= 2*(h->frame_num_offset + h->frame_num);
  3138. if(!h->nal_ref_idc)
  3139. poc--;
  3140. field_poc[0]= poc;
  3141. field_poc[1]= poc;
  3142. }
  3143. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3144. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3145. if(s->picture_structure != PICT_TOP_FIELD)
  3146. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3147. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  3148. return 0;
  3149. }
  3150. /**
  3151. * initialize scan tables
  3152. */
  3153. static void init_scan_tables(H264Context *h){
  3154. MpegEncContext * const s = &h->s;
  3155. int i;
  3156. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  3157. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  3158. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  3159. }else{
  3160. for(i=0; i<16; i++){
  3161. #define T(x) (x>>2) | ((x<<2) & 0xF)
  3162. h->zigzag_scan[i] = T(zigzag_scan[i]);
  3163. h-> field_scan[i] = T( field_scan[i]);
  3164. #undef T
  3165. }
  3166. }
  3167. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  3168. memcpy(h->zigzag_scan8x8, ff_zigzag_direct, 64*sizeof(uint8_t));
  3169. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  3170. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  3171. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  3172. }else{
  3173. for(i=0; i<64; i++){
  3174. #define T(x) (x>>3) | ((x&7)<<3)
  3175. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  3176. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  3177. h->field_scan8x8[i] = T(field_scan8x8[i]);
  3178. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  3179. #undef T
  3180. }
  3181. }
  3182. if(h->sps.transform_bypass){ //FIXME same ugly
  3183. h->zigzag_scan_q0 = zigzag_scan;
  3184. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  3185. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  3186. h->field_scan_q0 = field_scan;
  3187. h->field_scan8x8_q0 = field_scan8x8;
  3188. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  3189. }else{
  3190. h->zigzag_scan_q0 = h->zigzag_scan;
  3191. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  3192. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  3193. h->field_scan_q0 = h->field_scan;
  3194. h->field_scan8x8_q0 = h->field_scan8x8;
  3195. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  3196. }
  3197. }
  3198. /**
  3199. * Replicates H264 "master" context to thread contexts.
  3200. */
  3201. static void clone_slice(H264Context *dst, H264Context *src)
  3202. {
  3203. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  3204. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  3205. dst->s.current_picture = src->s.current_picture;
  3206. dst->s.linesize = src->s.linesize;
  3207. dst->s.uvlinesize = src->s.uvlinesize;
  3208. dst->s.first_field = src->s.first_field;
  3209. dst->prev_poc_msb = src->prev_poc_msb;
  3210. dst->prev_poc_lsb = src->prev_poc_lsb;
  3211. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  3212. dst->prev_frame_num = src->prev_frame_num;
  3213. dst->short_ref_count = src->short_ref_count;
  3214. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  3215. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  3216. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  3217. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  3218. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  3219. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  3220. }
  3221. /**
  3222. * decodes a slice header.
  3223. * This will also call MPV_common_init() and frame_start() as needed.
  3224. *
  3225. * @param h h264context
  3226. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  3227. *
  3228. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  3229. */
  3230. static int decode_slice_header(H264Context *h, H264Context *h0){
  3231. MpegEncContext * const s = &h->s;
  3232. MpegEncContext * const s0 = &h0->s;
  3233. unsigned int first_mb_in_slice;
  3234. unsigned int pps_id;
  3235. int num_ref_idx_active_override_flag;
  3236. unsigned int slice_type, tmp, i, j;
  3237. int default_ref_list_done = 0;
  3238. int last_pic_structure;
  3239. s->dropable= h->nal_ref_idc == 0;
  3240. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
  3241. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  3242. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  3243. }else{
  3244. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  3245. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  3246. }
  3247. first_mb_in_slice= get_ue_golomb(&s->gb);
  3248. if((s->flags2 & CODEC_FLAG2_CHUNKS) && first_mb_in_slice == 0){
  3249. h0->current_slice = 0;
  3250. if (!s0->first_field)
  3251. s->current_picture_ptr= NULL;
  3252. }
  3253. slice_type= get_ue_golomb_31(&s->gb);
  3254. if(slice_type > 9){
  3255. 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);
  3256. return -1;
  3257. }
  3258. if(slice_type > 4){
  3259. slice_type -= 5;
  3260. h->slice_type_fixed=1;
  3261. }else
  3262. h->slice_type_fixed=0;
  3263. slice_type= golomb_to_pict_type[ slice_type ];
  3264. if (slice_type == FF_I_TYPE
  3265. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  3266. default_ref_list_done = 1;
  3267. }
  3268. h->slice_type= slice_type;
  3269. h->slice_type_nos= slice_type & 3;
  3270. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  3271. if (s->pict_type == FF_B_TYPE && s0->last_picture_ptr == NULL) {
  3272. av_log(h->s.avctx, AV_LOG_ERROR,
  3273. "B picture before any references, skipping\n");
  3274. return -1;
  3275. }
  3276. pps_id= get_ue_golomb(&s->gb);
  3277. if(pps_id>=MAX_PPS_COUNT){
  3278. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  3279. return -1;
  3280. }
  3281. if(!h0->pps_buffers[pps_id]) {
  3282. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
  3283. return -1;
  3284. }
  3285. h->pps= *h0->pps_buffers[pps_id];
  3286. if(!h0->sps_buffers[h->pps.sps_id]) {
  3287. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
  3288. return -1;
  3289. }
  3290. h->sps = *h0->sps_buffers[h->pps.sps_id];
  3291. if(h == h0 && h->dequant_coeff_pps != pps_id){
  3292. h->dequant_coeff_pps = pps_id;
  3293. init_dequant_tables(h);
  3294. }
  3295. s->mb_width= h->sps.mb_width;
  3296. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  3297. h->b_stride= s->mb_width*4;
  3298. h->b8_stride= s->mb_width*2;
  3299. s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
  3300. if(h->sps.frame_mbs_only_flag)
  3301. s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
  3302. else
  3303. s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 3);
  3304. if (s->context_initialized
  3305. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  3306. if(h != h0)
  3307. return -1; // width / height changed during parallelized decoding
  3308. free_tables(h);
  3309. flush_dpb(s->avctx);
  3310. MPV_common_end(s);
  3311. h->list_count = 0;
  3312. }
  3313. if (!s->context_initialized) {
  3314. if(h != h0)
  3315. return -1; // we cant (re-)initialize context during parallel decoding
  3316. if (MPV_common_init(s) < 0)
  3317. return -1;
  3318. s->first_field = 0;
  3319. init_scan_tables(h);
  3320. alloc_tables(h);
  3321. for(i = 1; i < s->avctx->thread_count; i++) {
  3322. H264Context *c;
  3323. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  3324. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  3325. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  3326. c->sps = h->sps;
  3327. c->pps = h->pps;
  3328. init_scan_tables(c);
  3329. clone_tables(c, h);
  3330. }
  3331. for(i = 0; i < s->avctx->thread_count; i++)
  3332. if(context_init(h->thread_context[i]) < 0)
  3333. return -1;
  3334. s->avctx->width = s->width;
  3335. s->avctx->height = s->height;
  3336. s->avctx->sample_aspect_ratio= h->sps.sar;
  3337. if(!s->avctx->sample_aspect_ratio.den)
  3338. s->avctx->sample_aspect_ratio.den = 1;
  3339. if(h->sps.timing_info_present_flag){
  3340. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick, h->sps.time_scale};
  3341. if(h->x264_build > 0 && h->x264_build < 44)
  3342. s->avctx->time_base.den *= 2;
  3343. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  3344. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  3345. }
  3346. }
  3347. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  3348. h->mb_mbaff = 0;
  3349. h->mb_aff_frame = 0;
  3350. last_pic_structure = s0->picture_structure;
  3351. if(h->sps.frame_mbs_only_flag){
  3352. s->picture_structure= PICT_FRAME;
  3353. }else{
  3354. if(get_bits1(&s->gb)) { //field_pic_flag
  3355. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  3356. } else {
  3357. s->picture_structure= PICT_FRAME;
  3358. h->mb_aff_frame = h->sps.mb_aff;
  3359. }
  3360. }
  3361. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  3362. if(h0->current_slice == 0){
  3363. while(h->frame_num != h->prev_frame_num &&
  3364. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  3365. av_log(NULL, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  3366. if (frame_start(h) < 0)
  3367. return -1;
  3368. h->prev_frame_num++;
  3369. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  3370. s->current_picture_ptr->frame_num= h->prev_frame_num;
  3371. execute_ref_pic_marking(h, NULL, 0);
  3372. }
  3373. /* See if we have a decoded first field looking for a pair... */
  3374. if (s0->first_field) {
  3375. assert(s0->current_picture_ptr);
  3376. assert(s0->current_picture_ptr->data[0]);
  3377. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  3378. /* figure out if we have a complementary field pair */
  3379. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  3380. /*
  3381. * Previous field is unmatched. Don't display it, but let it
  3382. * remain for reference if marked as such.
  3383. */
  3384. s0->current_picture_ptr = NULL;
  3385. s0->first_field = FIELD_PICTURE;
  3386. } else {
  3387. if (h->nal_ref_idc &&
  3388. s0->current_picture_ptr->reference &&
  3389. s0->current_picture_ptr->frame_num != h->frame_num) {
  3390. /*
  3391. * This and previous field were reference, but had
  3392. * different frame_nums. Consider this field first in
  3393. * pair. Throw away previous field except for reference
  3394. * purposes.
  3395. */
  3396. s0->first_field = 1;
  3397. s0->current_picture_ptr = NULL;
  3398. } else {
  3399. /* Second field in complementary pair */
  3400. s0->first_field = 0;
  3401. }
  3402. }
  3403. } else {
  3404. /* Frame or first field in a potentially complementary pair */
  3405. assert(!s0->current_picture_ptr);
  3406. s0->first_field = FIELD_PICTURE;
  3407. }
  3408. if((!FIELD_PICTURE || s0->first_field) && frame_start(h) < 0) {
  3409. s0->first_field = 0;
  3410. return -1;
  3411. }
  3412. }
  3413. if(h != h0)
  3414. clone_slice(h, h0);
  3415. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  3416. assert(s->mb_num == s->mb_width * s->mb_height);
  3417. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  3418. first_mb_in_slice >= s->mb_num){
  3419. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  3420. return -1;
  3421. }
  3422. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  3423. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  3424. if (s->picture_structure == PICT_BOTTOM_FIELD)
  3425. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  3426. assert(s->mb_y < s->mb_height);
  3427. if(s->picture_structure==PICT_FRAME){
  3428. h->curr_pic_num= h->frame_num;
  3429. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  3430. }else{
  3431. h->curr_pic_num= 2*h->frame_num + 1;
  3432. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  3433. }
  3434. if(h->nal_unit_type == NAL_IDR_SLICE){
  3435. get_ue_golomb(&s->gb); /* idr_pic_id */
  3436. }
  3437. if(h->sps.poc_type==0){
  3438. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  3439. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  3440. h->delta_poc_bottom= get_se_golomb(&s->gb);
  3441. }
  3442. }
  3443. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  3444. h->delta_poc[0]= get_se_golomb(&s->gb);
  3445. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  3446. h->delta_poc[1]= get_se_golomb(&s->gb);
  3447. }
  3448. init_poc(h);
  3449. if(h->pps.redundant_pic_cnt_present){
  3450. h->redundant_pic_count= get_ue_golomb(&s->gb);
  3451. }
  3452. //set defaults, might be overridden a few lines later
  3453. h->ref_count[0]= h->pps.ref_count[0];
  3454. h->ref_count[1]= h->pps.ref_count[1];
  3455. if(h->slice_type_nos != FF_I_TYPE){
  3456. if(h->slice_type_nos == FF_B_TYPE){
  3457. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  3458. }
  3459. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  3460. if(num_ref_idx_active_override_flag){
  3461. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3462. if(h->slice_type_nos==FF_B_TYPE)
  3463. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3464. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  3465. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  3466. h->ref_count[0]= h->ref_count[1]= 1;
  3467. return -1;
  3468. }
  3469. }
  3470. if(h->slice_type_nos == FF_B_TYPE)
  3471. h->list_count= 2;
  3472. else
  3473. h->list_count= 1;
  3474. }else
  3475. h->list_count= 0;
  3476. if(!default_ref_list_done){
  3477. fill_default_ref_list(h);
  3478. }
  3479. if(h->slice_type_nos!=FF_I_TYPE && decode_ref_pic_list_reordering(h) < 0) {
  3480. h->ref_count[1]= h->ref_count[0]= 0;
  3481. return -1;
  3482. }
  3483. if(h->slice_type_nos!=FF_I_TYPE){
  3484. s->last_picture_ptr= &h->ref_list[0][0];
  3485. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  3486. }
  3487. if(h->slice_type_nos==FF_B_TYPE){
  3488. s->next_picture_ptr= &h->ref_list[1][0];
  3489. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  3490. }
  3491. if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
  3492. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
  3493. pred_weight_table(h);
  3494. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE)
  3495. implicit_weight_table(h);
  3496. else {
  3497. h->use_weight = 0;
  3498. for (i = 0; i < 2; i++) {
  3499. h->luma_weight_flag[i] = 0;
  3500. h->chroma_weight_flag[i] = 0;
  3501. }
  3502. }
  3503. if(h->nal_ref_idc)
  3504. decode_ref_pic_marking(h0, &s->gb);
  3505. if(FRAME_MBAFF)
  3506. fill_mbaff_ref_list(h);
  3507. if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
  3508. direct_dist_scale_factor(h);
  3509. direct_ref_list_init(h);
  3510. if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
  3511. tmp = get_ue_golomb_31(&s->gb);
  3512. if(tmp > 2){
  3513. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  3514. return -1;
  3515. }
  3516. h->cabac_init_idc= tmp;
  3517. }
  3518. h->last_qscale_diff = 0;
  3519. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  3520. if(tmp>51){
  3521. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  3522. return -1;
  3523. }
  3524. s->qscale= tmp;
  3525. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  3526. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  3527. //FIXME qscale / qp ... stuff
  3528. if(h->slice_type == FF_SP_TYPE){
  3529. get_bits1(&s->gb); /* sp_for_switch_flag */
  3530. }
  3531. if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
  3532. get_se_golomb(&s->gb); /* slice_qs_delta */
  3533. }
  3534. h->deblocking_filter = 1;
  3535. h->slice_alpha_c0_offset = 0;
  3536. h->slice_beta_offset = 0;
  3537. if( h->pps.deblocking_filter_parameters_present ) {
  3538. tmp= get_ue_golomb_31(&s->gb);
  3539. if(tmp > 2){
  3540. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  3541. return -1;
  3542. }
  3543. h->deblocking_filter= tmp;
  3544. if(h->deblocking_filter < 2)
  3545. h->deblocking_filter^= 1; // 1<->0
  3546. if( h->deblocking_filter ) {
  3547. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  3548. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  3549. }
  3550. }
  3551. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  3552. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
  3553. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
  3554. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  3555. h->deblocking_filter= 0;
  3556. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  3557. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  3558. /* Cheat slightly for speed:
  3559. Do not bother to deblock across slices. */
  3560. h->deblocking_filter = 2;
  3561. } else {
  3562. h0->max_contexts = 1;
  3563. if(!h0->single_decode_warning) {
  3564. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  3565. h0->single_decode_warning = 1;
  3566. }
  3567. if(h != h0)
  3568. return 1; // deblocking switched inside frame
  3569. }
  3570. }
  3571. #if 0 //FMO
  3572. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  3573. slice_group_change_cycle= get_bits(&s->gb, ?);
  3574. #endif
  3575. h0->last_slice_type = slice_type;
  3576. h->slice_num = ++h0->current_slice;
  3577. if(h->slice_num >= MAX_SLICES){
  3578. av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
  3579. }
  3580. for(j=0; j<2; j++){
  3581. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  3582. ref2frm[0]=
  3583. ref2frm[1]= -1;
  3584. for(i=0; i<16; i++)
  3585. ref2frm[i+2]= 4*h->ref_list[j][i].frame_num
  3586. +(h->ref_list[j][i].reference&3);
  3587. ref2frm[18+0]=
  3588. ref2frm[18+1]= -1;
  3589. for(i=16; i<48; i++)
  3590. ref2frm[i+4]= 4*h->ref_list[j][i].frame_num
  3591. +(h->ref_list[j][i].reference&3);
  3592. }
  3593. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  3594. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  3595. s->avctx->refs= h->sps.ref_frame_count;
  3596. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3597. 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",
  3598. h->slice_num,
  3599. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  3600. first_mb_in_slice,
  3601. av_get_pict_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  3602. pps_id, h->frame_num,
  3603. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  3604. h->ref_count[0], h->ref_count[1],
  3605. s->qscale,
  3606. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  3607. h->use_weight,
  3608. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  3609. h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  3610. );
  3611. }
  3612. return 0;
  3613. }
  3614. /**
  3615. *
  3616. */
  3617. static inline int get_level_prefix(GetBitContext *gb){
  3618. unsigned int buf;
  3619. int log;
  3620. OPEN_READER(re, gb);
  3621. UPDATE_CACHE(re, gb);
  3622. buf=GET_CACHE(re, gb);
  3623. log= 32 - av_log2(buf);
  3624. #ifdef TRACE
  3625. print_bin(buf>>(32-log), log);
  3626. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
  3627. #endif
  3628. LAST_SKIP_BITS(re, gb, log);
  3629. CLOSE_READER(re, gb);
  3630. return log-1;
  3631. }
  3632. static inline int get_dct8x8_allowed(H264Context *h){
  3633. if(h->sps.direct_8x8_inference_flag)
  3634. return !(*(uint64_t*)h->sub_mb_type & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8 )*0x0001000100010001ULL));
  3635. else
  3636. return !(*(uint64_t*)h->sub_mb_type & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8|MB_TYPE_DIRECT2)*0x0001000100010001ULL));
  3637. }
  3638. /**
  3639. * decodes a residual block.
  3640. * @param n block index
  3641. * @param scantable scantable
  3642. * @param max_coeff number of coefficients in the block
  3643. * @return <0 if an error occurred
  3644. */
  3645. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  3646. MpegEncContext * const s = &h->s;
  3647. static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  3648. int level[16];
  3649. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  3650. //FIXME put trailing_onex into the context
  3651. if(n == CHROMA_DC_BLOCK_INDEX){
  3652. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  3653. total_coeff= coeff_token>>2;
  3654. }else{
  3655. if(n == LUMA_DC_BLOCK_INDEX){
  3656. total_coeff= pred_non_zero_count(h, 0);
  3657. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3658. total_coeff= coeff_token>>2;
  3659. }else{
  3660. total_coeff= pred_non_zero_count(h, n);
  3661. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3662. total_coeff= coeff_token>>2;
  3663. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  3664. }
  3665. }
  3666. //FIXME set last_non_zero?
  3667. if(total_coeff==0)
  3668. return 0;
  3669. if(total_coeff > (unsigned)max_coeff) {
  3670. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  3671. return -1;
  3672. }
  3673. trailing_ones= coeff_token&3;
  3674. tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
  3675. assert(total_coeff<=16);
  3676. i = show_bits(gb, 3);
  3677. skip_bits(gb, trailing_ones);
  3678. level[0] = 1-((i&4)>>1);
  3679. level[1] = 1-((i&2) );
  3680. level[2] = 1-((i&1)<<1);
  3681. if(trailing_ones<total_coeff) {
  3682. int mask, prefix;
  3683. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  3684. int bitsi= show_bits(gb, LEVEL_TAB_BITS);
  3685. int level_code= cavlc_level_tab[suffix_length][bitsi][0];
  3686. skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
  3687. if(level_code >= 100){
  3688. prefix= level_code - 100;
  3689. if(prefix == LEVEL_TAB_BITS)
  3690. prefix += get_level_prefix(gb);
  3691. //first coefficient has suffix_length equal to 0 or 1
  3692. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  3693. if(suffix_length)
  3694. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  3695. else
  3696. level_code= (prefix<<suffix_length); //part
  3697. }else if(prefix==14){
  3698. if(suffix_length)
  3699. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  3700. else
  3701. level_code= prefix + get_bits(gb, 4); //part
  3702. }else{
  3703. level_code= (15<<suffix_length) + get_bits(gb, prefix-3); //part
  3704. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  3705. if(prefix>=16)
  3706. level_code += (1<<(prefix-3))-4096;
  3707. }
  3708. if(trailing_ones < 3) level_code += 2;
  3709. suffix_length = 2;
  3710. mask= -(level_code&1);
  3711. level[trailing_ones]= (((2+level_code)>>1) ^ mask) - mask;
  3712. }else{
  3713. if(trailing_ones < 3) level_code += (level_code>>31)|1;
  3714. suffix_length = 1;
  3715. if(level_code + 3U > 6U)
  3716. suffix_length++;
  3717. level[trailing_ones]= level_code;
  3718. }
  3719. //remaining coefficients have suffix_length > 0
  3720. for(i=trailing_ones+1;i<total_coeff;i++) {
  3721. static const unsigned int suffix_limit[7] = {0,3,6,12,24,48,INT_MAX };
  3722. int bitsi= show_bits(gb, LEVEL_TAB_BITS);
  3723. level_code= cavlc_level_tab[suffix_length][bitsi][0];
  3724. skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
  3725. if(level_code >= 100){
  3726. prefix= level_code - 100;
  3727. if(prefix == LEVEL_TAB_BITS){
  3728. prefix += get_level_prefix(gb);
  3729. }
  3730. if(prefix<15){
  3731. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  3732. }else{
  3733. level_code = (15<<suffix_length) + get_bits(gb, prefix-3);
  3734. if(prefix>=16)
  3735. level_code += (1<<(prefix-3))-4096;
  3736. }
  3737. mask= -(level_code&1);
  3738. level_code= (((2+level_code)>>1) ^ mask) - mask;
  3739. }
  3740. level[i]= level_code;
  3741. if(suffix_limit[suffix_length] + level_code > 2U*suffix_limit[suffix_length])
  3742. suffix_length++;
  3743. }
  3744. }
  3745. if(total_coeff == max_coeff)
  3746. zeros_left=0;
  3747. else{
  3748. if(n == CHROMA_DC_BLOCK_INDEX)
  3749. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  3750. else
  3751. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  3752. }
  3753. coeff_num = zeros_left + total_coeff - 1;
  3754. j = scantable[coeff_num];
  3755. if(n > 24){
  3756. block[j] = level[0];
  3757. for(i=1;i<total_coeff;i++) {
  3758. if(zeros_left <= 0)
  3759. run_before = 0;
  3760. else if(zeros_left < 7){
  3761. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  3762. }else{
  3763. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  3764. }
  3765. zeros_left -= run_before;
  3766. coeff_num -= 1 + run_before;
  3767. j= scantable[ coeff_num ];
  3768. block[j]= level[i];
  3769. }
  3770. }else{
  3771. block[j] = (level[0] * qmul[j] + 32)>>6;
  3772. for(i=1;i<total_coeff;i++) {
  3773. if(zeros_left <= 0)
  3774. run_before = 0;
  3775. else if(zeros_left < 7){
  3776. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  3777. }else{
  3778. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  3779. }
  3780. zeros_left -= run_before;
  3781. coeff_num -= 1 + run_before;
  3782. j= scantable[ coeff_num ];
  3783. block[j]= (level[i] * qmul[j] + 32)>>6;
  3784. }
  3785. }
  3786. if(zeros_left<0){
  3787. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  3788. return -1;
  3789. }
  3790. return 0;
  3791. }
  3792. static void predict_field_decoding_flag(H264Context *h){
  3793. MpegEncContext * const s = &h->s;
  3794. const int mb_xy= h->mb_xy;
  3795. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  3796. ? s->current_picture.mb_type[mb_xy-1]
  3797. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  3798. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  3799. : 0;
  3800. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  3801. }
  3802. /**
  3803. * decodes a P_SKIP or B_SKIP macroblock
  3804. */
  3805. static void decode_mb_skip(H264Context *h){
  3806. MpegEncContext * const s = &h->s;
  3807. const int mb_xy= h->mb_xy;
  3808. int mb_type=0;
  3809. memset(h->non_zero_count[mb_xy], 0, 16);
  3810. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  3811. if(MB_FIELD)
  3812. mb_type|= MB_TYPE_INTERLACED;
  3813. if( h->slice_type_nos == FF_B_TYPE )
  3814. {
  3815. // just for fill_caches. pred_direct_motion will set the real mb_type
  3816. mb_type|= MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  3817. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  3818. pred_direct_motion(h, &mb_type);
  3819. mb_type|= MB_TYPE_SKIP;
  3820. }
  3821. else
  3822. {
  3823. int mx, my;
  3824. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  3825. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  3826. pred_pskip_motion(h, &mx, &my);
  3827. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  3828. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  3829. }
  3830. write_back_motion(h, mb_type);
  3831. s->current_picture.mb_type[mb_xy]= mb_type;
  3832. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3833. h->slice_table[ mb_xy ]= h->slice_num;
  3834. h->prev_mb_skipped= 1;
  3835. }
  3836. /**
  3837. * decodes a macroblock
  3838. * @returns 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  3839. */
  3840. static int decode_mb_cavlc(H264Context *h){
  3841. MpegEncContext * const s = &h->s;
  3842. int mb_xy;
  3843. int partition_count;
  3844. unsigned int mb_type, cbp;
  3845. int dct8x8_allowed= h->pps.transform_8x8_mode;
  3846. mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3847. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  3848. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  3849. down the code */
  3850. if(h->slice_type_nos != FF_I_TYPE){
  3851. if(s->mb_skip_run==-1)
  3852. s->mb_skip_run= get_ue_golomb(&s->gb);
  3853. if (s->mb_skip_run--) {
  3854. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  3855. if(s->mb_skip_run==0)
  3856. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  3857. else
  3858. predict_field_decoding_flag(h);
  3859. }
  3860. decode_mb_skip(h);
  3861. return 0;
  3862. }
  3863. }
  3864. if(FRAME_MBAFF){
  3865. if( (s->mb_y&1) == 0 )
  3866. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  3867. }
  3868. h->prev_mb_skipped= 0;
  3869. mb_type= get_ue_golomb(&s->gb);
  3870. if(h->slice_type_nos == FF_B_TYPE){
  3871. if(mb_type < 23){
  3872. partition_count= b_mb_type_info[mb_type].partition_count;
  3873. mb_type= b_mb_type_info[mb_type].type;
  3874. }else{
  3875. mb_type -= 23;
  3876. goto decode_intra_mb;
  3877. }
  3878. }else if(h->slice_type_nos == FF_P_TYPE){
  3879. if(mb_type < 5){
  3880. partition_count= p_mb_type_info[mb_type].partition_count;
  3881. mb_type= p_mb_type_info[mb_type].type;
  3882. }else{
  3883. mb_type -= 5;
  3884. goto decode_intra_mb;
  3885. }
  3886. }else{
  3887. assert(h->slice_type_nos == FF_I_TYPE);
  3888. if(h->slice_type == FF_SI_TYPE && mb_type)
  3889. mb_type--;
  3890. decode_intra_mb:
  3891. if(mb_type > 25){
  3892. av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
  3893. return -1;
  3894. }
  3895. partition_count=0;
  3896. cbp= i_mb_type_info[mb_type].cbp;
  3897. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  3898. mb_type= i_mb_type_info[mb_type].type;
  3899. }
  3900. if(MB_FIELD)
  3901. mb_type |= MB_TYPE_INTERLACED;
  3902. h->slice_table[ mb_xy ]= h->slice_num;
  3903. if(IS_INTRA_PCM(mb_type)){
  3904. unsigned int x;
  3905. // We assume these blocks are very rare so we do not optimize it.
  3906. align_get_bits(&s->gb);
  3907. // The pixels are stored in the same order as levels in h->mb array.
  3908. for(x=0; x < (CHROMA ? 384 : 256); x++){
  3909. ((uint8_t*)h->mb)[x]= get_bits(&s->gb, 8);
  3910. }
  3911. // In deblocking, the quantizer is 0
  3912. s->current_picture.qscale_table[mb_xy]= 0;
  3913. // All coeffs are present
  3914. memset(h->non_zero_count[mb_xy], 16, 16);
  3915. s->current_picture.mb_type[mb_xy]= mb_type;
  3916. return 0;
  3917. }
  3918. if(MB_MBAFF){
  3919. h->ref_count[0] <<= 1;
  3920. h->ref_count[1] <<= 1;
  3921. }
  3922. fill_caches(h, mb_type, 0);
  3923. //mb_pred
  3924. if(IS_INTRA(mb_type)){
  3925. int pred_mode;
  3926. // init_top_left_availability(h);
  3927. if(IS_INTRA4x4(mb_type)){
  3928. int i;
  3929. int di = 1;
  3930. if(dct8x8_allowed && get_bits1(&s->gb)){
  3931. mb_type |= MB_TYPE_8x8DCT;
  3932. di = 4;
  3933. }
  3934. // fill_intra4x4_pred_table(h);
  3935. for(i=0; i<16; i+=di){
  3936. int mode= pred_intra_mode(h, i);
  3937. if(!get_bits1(&s->gb)){
  3938. const int rem_mode= get_bits(&s->gb, 3);
  3939. mode = rem_mode + (rem_mode >= mode);
  3940. }
  3941. if(di==4)
  3942. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  3943. else
  3944. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  3945. }
  3946. write_back_intra_pred_mode(h);
  3947. if( check_intra4x4_pred_mode(h) < 0)
  3948. return -1;
  3949. }else{
  3950. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  3951. if(h->intra16x16_pred_mode < 0)
  3952. return -1;
  3953. }
  3954. if(CHROMA){
  3955. pred_mode= check_intra_pred_mode(h, get_ue_golomb_31(&s->gb));
  3956. if(pred_mode < 0)
  3957. return -1;
  3958. h->chroma_pred_mode= pred_mode;
  3959. }
  3960. }else if(partition_count==4){
  3961. int i, j, sub_partition_count[4], list, ref[2][4];
  3962. if(h->slice_type_nos == FF_B_TYPE){
  3963. for(i=0; i<4; i++){
  3964. h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
  3965. if(h->sub_mb_type[i] >=13){
  3966. av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  3967. return -1;
  3968. }
  3969. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3970. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3971. }
  3972. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  3973. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  3974. pred_direct_motion(h, &mb_type);
  3975. h->ref_cache[0][scan8[4]] =
  3976. h->ref_cache[1][scan8[4]] =
  3977. h->ref_cache[0][scan8[12]] =
  3978. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  3979. }
  3980. }else{
  3981. assert(h->slice_type_nos == FF_P_TYPE); //FIXME SP correct ?
  3982. for(i=0; i<4; i++){
  3983. h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
  3984. if(h->sub_mb_type[i] >=4){
  3985. av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  3986. return -1;
  3987. }
  3988. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3989. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3990. }
  3991. }
  3992. for(list=0; list<h->list_count; list++){
  3993. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  3994. for(i=0; i<4; i++){
  3995. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  3996. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  3997. unsigned int tmp;
  3998. if(ref_count == 1){
  3999. tmp= 0;
  4000. }else if(ref_count == 2){
  4001. tmp= get_bits1(&s->gb)^1;
  4002. }else{
  4003. tmp= get_ue_golomb_31(&s->gb);
  4004. if(tmp>=ref_count){
  4005. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  4006. return -1;
  4007. }
  4008. }
  4009. ref[list][i]= tmp;
  4010. }else{
  4011. //FIXME
  4012. ref[list][i] = -1;
  4013. }
  4014. }
  4015. }
  4016. if(dct8x8_allowed)
  4017. dct8x8_allowed = get_dct8x8_allowed(h);
  4018. for(list=0; list<h->list_count; list++){
  4019. for(i=0; i<4; i++){
  4020. if(IS_DIRECT(h->sub_mb_type[i])) {
  4021. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4022. continue;
  4023. }
  4024. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4025. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4026. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4027. const int sub_mb_type= h->sub_mb_type[i];
  4028. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4029. for(j=0; j<sub_partition_count[i]; j++){
  4030. int mx, my;
  4031. const int index= 4*i + block_width*j;
  4032. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4033. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4034. mx += get_se_golomb(&s->gb);
  4035. my += get_se_golomb(&s->gb);
  4036. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4037. if(IS_SUB_8X8(sub_mb_type)){
  4038. mv_cache[ 1 ][0]=
  4039. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4040. mv_cache[ 1 ][1]=
  4041. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4042. }else if(IS_SUB_8X4(sub_mb_type)){
  4043. mv_cache[ 1 ][0]= mx;
  4044. mv_cache[ 1 ][1]= my;
  4045. }else if(IS_SUB_4X8(sub_mb_type)){
  4046. mv_cache[ 8 ][0]= mx;
  4047. mv_cache[ 8 ][1]= my;
  4048. }
  4049. mv_cache[ 0 ][0]= mx;
  4050. mv_cache[ 0 ][1]= my;
  4051. }
  4052. }else{
  4053. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4054. p[0] = p[1]=
  4055. p[8] = p[9]= 0;
  4056. }
  4057. }
  4058. }
  4059. }else if(IS_DIRECT(mb_type)){
  4060. pred_direct_motion(h, &mb_type);
  4061. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4062. }else{
  4063. int list, mx, my, i;
  4064. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4065. if(IS_16X16(mb_type)){
  4066. for(list=0; list<h->list_count; list++){
  4067. unsigned int val;
  4068. if(IS_DIR(mb_type, 0, list)){
  4069. if(h->ref_count[list]==1){
  4070. val= 0;
  4071. }else if(h->ref_count[list]==2){
  4072. val= get_bits1(&s->gb)^1;
  4073. }else{
  4074. val= get_ue_golomb_31(&s->gb);
  4075. if(val >= h->ref_count[list]){
  4076. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4077. return -1;
  4078. }
  4079. }
  4080. }else
  4081. val= LIST_NOT_USED&0xFF;
  4082. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4083. }
  4084. for(list=0; list<h->list_count; list++){
  4085. unsigned int val;
  4086. if(IS_DIR(mb_type, 0, list)){
  4087. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4088. mx += get_se_golomb(&s->gb);
  4089. my += get_se_golomb(&s->gb);
  4090. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4091. val= pack16to32(mx,my);
  4092. }else
  4093. val=0;
  4094. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
  4095. }
  4096. }
  4097. else if(IS_16X8(mb_type)){
  4098. for(list=0; list<h->list_count; list++){
  4099. for(i=0; i<2; i++){
  4100. unsigned int val;
  4101. if(IS_DIR(mb_type, i, list)){
  4102. if(h->ref_count[list] == 1){
  4103. val= 0;
  4104. }else if(h->ref_count[list] == 2){
  4105. val= get_bits1(&s->gb)^1;
  4106. }else{
  4107. val= get_ue_golomb_31(&s->gb);
  4108. if(val >= h->ref_count[list]){
  4109. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4110. return -1;
  4111. }
  4112. }
  4113. }else
  4114. val= LIST_NOT_USED&0xFF;
  4115. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4116. }
  4117. }
  4118. for(list=0; list<h->list_count; list++){
  4119. for(i=0; i<2; i++){
  4120. unsigned int val;
  4121. if(IS_DIR(mb_type, i, list)){
  4122. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4123. mx += get_se_golomb(&s->gb);
  4124. my += get_se_golomb(&s->gb);
  4125. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4126. val= pack16to32(mx,my);
  4127. }else
  4128. val=0;
  4129. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
  4130. }
  4131. }
  4132. }else{
  4133. assert(IS_8X16(mb_type));
  4134. for(list=0; list<h->list_count; list++){
  4135. for(i=0; i<2; i++){
  4136. unsigned int val;
  4137. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4138. if(h->ref_count[list]==1){
  4139. val= 0;
  4140. }else if(h->ref_count[list]==2){
  4141. val= get_bits1(&s->gb)^1;
  4142. }else{
  4143. val= get_ue_golomb_31(&s->gb);
  4144. if(val >= h->ref_count[list]){
  4145. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4146. return -1;
  4147. }
  4148. }
  4149. }else
  4150. val= LIST_NOT_USED&0xFF;
  4151. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4152. }
  4153. }
  4154. for(list=0; list<h->list_count; list++){
  4155. for(i=0; i<2; i++){
  4156. unsigned int val;
  4157. if(IS_DIR(mb_type, i, list)){
  4158. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4159. mx += get_se_golomb(&s->gb);
  4160. my += get_se_golomb(&s->gb);
  4161. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4162. val= pack16to32(mx,my);
  4163. }else
  4164. val=0;
  4165. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
  4166. }
  4167. }
  4168. }
  4169. }
  4170. if(IS_INTER(mb_type))
  4171. write_back_motion(h, mb_type);
  4172. if(!IS_INTRA16x16(mb_type)){
  4173. cbp= get_ue_golomb(&s->gb);
  4174. if(cbp > 47){
  4175. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4176. return -1;
  4177. }
  4178. if(CHROMA){
  4179. if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp];
  4180. else cbp= golomb_to_inter_cbp [cbp];
  4181. }else{
  4182. if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp];
  4183. else cbp= golomb_to_inter_cbp_gray[cbp];
  4184. }
  4185. }
  4186. h->cbp = cbp;
  4187. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4188. if(get_bits1(&s->gb)){
  4189. mb_type |= MB_TYPE_8x8DCT;
  4190. h->cbp_table[mb_xy]= cbp;
  4191. }
  4192. }
  4193. s->current_picture.mb_type[mb_xy]= mb_type;
  4194. if(cbp || IS_INTRA16x16(mb_type)){
  4195. int i8x8, i4x4, chroma_idx;
  4196. int dquant;
  4197. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4198. const uint8_t *scan, *scan8x8, *dc_scan;
  4199. // fill_non_zero_count_cache(h);
  4200. if(IS_INTERLACED(mb_type)){
  4201. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4202. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4203. dc_scan= luma_dc_field_scan;
  4204. }else{
  4205. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4206. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4207. dc_scan= luma_dc_zigzag_scan;
  4208. }
  4209. dquant= get_se_golomb(&s->gb);
  4210. if( dquant > 25 || dquant < -26 ){
  4211. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4212. return -1;
  4213. }
  4214. s->qscale += dquant;
  4215. if(((unsigned)s->qscale) > 51){
  4216. if(s->qscale<0) s->qscale+= 52;
  4217. else s->qscale-= 52;
  4218. }
  4219. h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
  4220. h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
  4221. if(IS_INTRA16x16(mb_type)){
  4222. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4223. return -1; //FIXME continue if partitioned and other return -1 too
  4224. }
  4225. assert((cbp&15) == 0 || (cbp&15) == 15);
  4226. if(cbp&15){
  4227. for(i8x8=0; i8x8<4; i8x8++){
  4228. for(i4x4=0; i4x4<4; i4x4++){
  4229. const int index= i4x4 + 4*i8x8;
  4230. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4231. return -1;
  4232. }
  4233. }
  4234. }
  4235. }else{
  4236. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4237. }
  4238. }else{
  4239. for(i8x8=0; i8x8<4; i8x8++){
  4240. if(cbp & (1<<i8x8)){
  4241. if(IS_8x8DCT(mb_type)){
  4242. DCTELEM *buf = &h->mb[64*i8x8];
  4243. uint8_t *nnz;
  4244. for(i4x4=0; i4x4<4; i4x4++){
  4245. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4246. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4247. return -1;
  4248. }
  4249. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4250. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4251. }else{
  4252. for(i4x4=0; i4x4<4; i4x4++){
  4253. const int index= i4x4 + 4*i8x8;
  4254. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4255. return -1;
  4256. }
  4257. }
  4258. }
  4259. }else{
  4260. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4261. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4262. }
  4263. }
  4264. }
  4265. if(cbp&0x30){
  4266. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4267. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4268. return -1;
  4269. }
  4270. }
  4271. if(cbp&0x20){
  4272. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4273. const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
  4274. for(i4x4=0; i4x4<4; i4x4++){
  4275. const int index= 16 + 4*chroma_idx + i4x4;
  4276. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, qmul, 15) < 0){
  4277. return -1;
  4278. }
  4279. }
  4280. }
  4281. }else{
  4282. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4283. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4284. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4285. }
  4286. }else{
  4287. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4288. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4289. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4290. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4291. }
  4292. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4293. write_back_non_zero_count(h);
  4294. if(MB_MBAFF){
  4295. h->ref_count[0] >>= 1;
  4296. h->ref_count[1] >>= 1;
  4297. }
  4298. return 0;
  4299. }
  4300. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4301. MpegEncContext * const s = &h->s;
  4302. const int mb_x = s->mb_x;
  4303. const int mb_y = s->mb_y & ~1;
  4304. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4305. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4306. unsigned int ctx = 0;
  4307. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4308. ctx += 1;
  4309. }
  4310. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4311. ctx += 1;
  4312. }
  4313. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4314. }
  4315. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4316. uint8_t *state= &h->cabac_state[ctx_base];
  4317. int mb_type;
  4318. if(intra_slice){
  4319. MpegEncContext * const s = &h->s;
  4320. const int mba_xy = h->left_mb_xy[0];
  4321. const int mbb_xy = h->top_mb_xy;
  4322. int ctx=0;
  4323. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4324. ctx++;
  4325. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4326. ctx++;
  4327. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4328. return 0; /* I4x4 */
  4329. state += 2;
  4330. }else{
  4331. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4332. return 0; /* I4x4 */
  4333. }
  4334. if( get_cabac_terminate( &h->cabac ) )
  4335. return 25; /* PCM */
  4336. mb_type = 1; /* I16x16 */
  4337. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4338. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4339. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4340. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4341. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4342. return mb_type;
  4343. }
  4344. static int decode_cabac_mb_type_b( H264Context *h ) {
  4345. MpegEncContext * const s = &h->s;
  4346. const int mba_xy = h->left_mb_xy[0];
  4347. const int mbb_xy = h->top_mb_xy;
  4348. int ctx = 0;
  4349. int bits;
  4350. assert(h->slice_type_nos == FF_B_TYPE);
  4351. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4352. ctx++;
  4353. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4354. ctx++;
  4355. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  4356. return 0; /* B_Direct_16x16 */
  4357. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  4358. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4359. }
  4360. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4361. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4362. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4363. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4364. if( bits < 8 )
  4365. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4366. else if( bits == 13 ) {
  4367. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4368. } else if( bits == 14 )
  4369. return 11; /* B_L1_L0_8x16 */
  4370. else if( bits == 15 )
  4371. return 22; /* B_8x8 */
  4372. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4373. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4374. }
  4375. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  4376. MpegEncContext * const s = &h->s;
  4377. int mba_xy, mbb_xy;
  4378. int ctx = 0;
  4379. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  4380. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  4381. mba_xy = mb_xy - 1;
  4382. if( (mb_y&1)
  4383. && h->slice_table[mba_xy] == h->slice_num
  4384. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  4385. mba_xy += s->mb_stride;
  4386. if( MB_FIELD ){
  4387. mbb_xy = mb_xy - s->mb_stride;
  4388. if( !(mb_y&1)
  4389. && h->slice_table[mbb_xy] == h->slice_num
  4390. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  4391. mbb_xy -= s->mb_stride;
  4392. }else
  4393. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  4394. }else{
  4395. int mb_xy = h->mb_xy;
  4396. mba_xy = mb_xy - 1;
  4397. mbb_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
  4398. }
  4399. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  4400. ctx++;
  4401. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  4402. ctx++;
  4403. if( h->slice_type_nos == FF_B_TYPE )
  4404. ctx += 13;
  4405. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  4406. }
  4407. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  4408. int mode = 0;
  4409. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  4410. return pred_mode;
  4411. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4412. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4413. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4414. if( mode >= pred_mode )
  4415. return mode + 1;
  4416. else
  4417. return mode;
  4418. }
  4419. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  4420. const int mba_xy = h->left_mb_xy[0];
  4421. const int mbb_xy = h->top_mb_xy;
  4422. int ctx = 0;
  4423. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  4424. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  4425. ctx++;
  4426. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  4427. ctx++;
  4428. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  4429. return 0;
  4430. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4431. return 1;
  4432. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4433. return 2;
  4434. else
  4435. return 3;
  4436. }
  4437. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  4438. int cbp_b, cbp_a, ctx, cbp = 0;
  4439. cbp_a = h->slice_table[h->left_mb_xy[0]] == h->slice_num ? h->left_cbp : -1;
  4440. cbp_b = h->slice_table[h->top_mb_xy] == h->slice_num ? h->top_cbp : -1;
  4441. ctx = !(cbp_a & 0x02) + 2 * !(cbp_b & 0x04);
  4442. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]);
  4443. ctx = !(cbp & 0x01) + 2 * !(cbp_b & 0x08);
  4444. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 1;
  4445. ctx = !(cbp_a & 0x08) + 2 * !(cbp & 0x01);
  4446. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 2;
  4447. ctx = !(cbp & 0x04) + 2 * !(cbp & 0x02);
  4448. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 3;
  4449. return cbp;
  4450. }
  4451. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  4452. int ctx;
  4453. int cbp_a, cbp_b;
  4454. cbp_a = (h->left_cbp>>4)&0x03;
  4455. cbp_b = (h-> top_cbp>>4)&0x03;
  4456. ctx = 0;
  4457. if( cbp_a > 0 ) ctx++;
  4458. if( cbp_b > 0 ) ctx += 2;
  4459. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  4460. return 0;
  4461. ctx = 4;
  4462. if( cbp_a == 2 ) ctx++;
  4463. if( cbp_b == 2 ) ctx += 2;
  4464. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  4465. }
  4466. static int decode_cabac_mb_dqp( H264Context *h) {
  4467. int ctx= h->last_qscale_diff != 0;
  4468. int val = 0;
  4469. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  4470. ctx= 2+(ctx>>1);
  4471. val++;
  4472. if(val > 102) //prevent infinite loop
  4473. return INT_MIN;
  4474. }
  4475. if( val&0x01 )
  4476. return (val + 1)>>1 ;
  4477. else
  4478. return -((val + 1)>>1);
  4479. }
  4480. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  4481. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  4482. return 0; /* 8x8 */
  4483. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  4484. return 1; /* 8x4 */
  4485. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  4486. return 2; /* 4x8 */
  4487. return 3; /* 4x4 */
  4488. }
  4489. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  4490. int type;
  4491. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  4492. return 0; /* B_Direct_8x8 */
  4493. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  4494. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  4495. type = 3;
  4496. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  4497. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  4498. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  4499. type += 4;
  4500. }
  4501. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  4502. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  4503. return type;
  4504. }
  4505. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  4506. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  4507. }
  4508. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  4509. int refa = h->ref_cache[list][scan8[n] - 1];
  4510. int refb = h->ref_cache[list][scan8[n] - 8];
  4511. int ref = 0;
  4512. int ctx = 0;
  4513. if( h->slice_type_nos == FF_B_TYPE) {
  4514. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  4515. ctx++;
  4516. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  4517. ctx += 2;
  4518. } else {
  4519. if( refa > 0 )
  4520. ctx++;
  4521. if( refb > 0 )
  4522. ctx += 2;
  4523. }
  4524. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  4525. ref++;
  4526. ctx = (ctx>>2)+4;
  4527. if(ref >= 32 /*h->ref_list[list]*/){
  4528. return -1;
  4529. }
  4530. }
  4531. return ref;
  4532. }
  4533. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  4534. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  4535. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  4536. int ctxbase = (l == 0) ? 40 : 47;
  4537. int mvd;
  4538. int ctx = (amvd>2) + (amvd>32);
  4539. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  4540. return 0;
  4541. mvd= 1;
  4542. ctx= 3;
  4543. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  4544. mvd++;
  4545. if( ctx < 6 )
  4546. ctx++;
  4547. }
  4548. if( mvd >= 9 ) {
  4549. int k = 3;
  4550. while( get_cabac_bypass( &h->cabac ) ) {
  4551. mvd += 1 << k;
  4552. k++;
  4553. if(k>24){
  4554. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
  4555. return INT_MIN;
  4556. }
  4557. }
  4558. while( k-- ) {
  4559. if( get_cabac_bypass( &h->cabac ) )
  4560. mvd += 1 << k;
  4561. }
  4562. }
  4563. return get_cabac_bypass_sign( &h->cabac, -mvd );
  4564. }
  4565. static av_always_inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx, int is_dc ) {
  4566. int nza, nzb;
  4567. int ctx = 0;
  4568. if( is_dc ) {
  4569. if( cat == 0 ) {
  4570. nza = h->left_cbp&0x100;
  4571. nzb = h-> top_cbp&0x100;
  4572. } else {
  4573. nza = (h->left_cbp>>(6+idx))&0x01;
  4574. nzb = (h-> top_cbp>>(6+idx))&0x01;
  4575. }
  4576. } else {
  4577. assert(cat == 1 || cat == 2 || cat == 4);
  4578. nza = h->non_zero_count_cache[scan8[idx] - 1];
  4579. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  4580. }
  4581. if( nza > 0 )
  4582. ctx++;
  4583. if( nzb > 0 )
  4584. ctx += 2;
  4585. return ctx + 4 * cat;
  4586. }
  4587. DECLARE_ASM_CONST(1, uint8_t, last_coeff_flag_offset_8x8[63]) = {
  4588. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  4589. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  4590. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  4591. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  4592. };
  4593. static av_always_inline void decode_cabac_residual_internal( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff, int is_dc ) {
  4594. static const int significant_coeff_flag_offset[2][6] = {
  4595. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  4596. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  4597. };
  4598. static const int last_coeff_flag_offset[2][6] = {
  4599. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  4600. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  4601. };
  4602. static const int coeff_abs_level_m1_offset[6] = {
  4603. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  4604. };
  4605. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  4606. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  4607. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  4608. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  4609. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  4610. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  4611. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  4612. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  4613. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  4614. };
  4615. /* node ctx: 0..3: abslevel1 (with abslevelgt1 == 0).
  4616. * 4..7: abslevelgt1 + 3 (and abslevel1 doesn't matter).
  4617. * map node ctx => cabac ctx for level=1 */
  4618. static const uint8_t coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };
  4619. /* map node ctx => cabac ctx for level>1 */
  4620. static const uint8_t coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };
  4621. static const uint8_t coeff_abs_level_transition[2][8] = {
  4622. /* update node ctx after decoding a level=1 */
  4623. { 1, 2, 3, 3, 4, 5, 6, 7 },
  4624. /* update node ctx after decoding a level>1 */
  4625. { 4, 4, 4, 4, 5, 6, 7, 7 }
  4626. };
  4627. int index[64];
  4628. int av_unused last;
  4629. int coeff_count = 0;
  4630. int node_ctx = 0;
  4631. uint8_t *significant_coeff_ctx_base;
  4632. uint8_t *last_coeff_ctx_base;
  4633. uint8_t *abs_level_m1_ctx_base;
  4634. #if !ARCH_X86
  4635. #define CABAC_ON_STACK
  4636. #endif
  4637. #ifdef CABAC_ON_STACK
  4638. #define CC &cc
  4639. CABACContext cc;
  4640. cc.range = h->cabac.range;
  4641. cc.low = h->cabac.low;
  4642. cc.bytestream= h->cabac.bytestream;
  4643. #else
  4644. #define CC &h->cabac
  4645. #endif
  4646. /* cat: 0-> DC 16x16 n = 0
  4647. * 1-> AC 16x16 n = luma4x4idx
  4648. * 2-> Luma4x4 n = luma4x4idx
  4649. * 3-> DC Chroma n = iCbCr
  4650. * 4-> AC Chroma n = 16 + 4 * iCbCr + chroma4x4idx
  4651. * 5-> Luma8x8 n = 4 * luma8x8idx
  4652. */
  4653. /* read coded block flag */
  4654. if( is_dc || cat != 5 ) {
  4655. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n, is_dc ) ] ) == 0 ) {
  4656. if( !is_dc )
  4657. h->non_zero_count_cache[scan8[n]] = 0;
  4658. #ifdef CABAC_ON_STACK
  4659. h->cabac.range = cc.range ;
  4660. h->cabac.low = cc.low ;
  4661. h->cabac.bytestream= cc.bytestream;
  4662. #endif
  4663. return;
  4664. }
  4665. }
  4666. significant_coeff_ctx_base = h->cabac_state
  4667. + significant_coeff_flag_offset[MB_FIELD][cat];
  4668. last_coeff_ctx_base = h->cabac_state
  4669. + last_coeff_flag_offset[MB_FIELD][cat];
  4670. abs_level_m1_ctx_base = h->cabac_state
  4671. + coeff_abs_level_m1_offset[cat];
  4672. if( !is_dc && cat == 5 ) {
  4673. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  4674. for(last= 0; last < coefs; last++) { \
  4675. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  4676. if( get_cabac( CC, sig_ctx )) { \
  4677. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  4678. index[coeff_count++] = last; \
  4679. if( get_cabac( CC, last_ctx ) ) { \
  4680. last= max_coeff; \
  4681. break; \
  4682. } \
  4683. } \
  4684. }\
  4685. if( last == max_coeff -1 ) {\
  4686. index[coeff_count++] = last;\
  4687. }
  4688. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  4689. #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS)
  4690. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  4691. } else {
  4692. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  4693. #else
  4694. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  4695. } else {
  4696. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  4697. #endif
  4698. }
  4699. assert(coeff_count > 0);
  4700. if( is_dc ) {
  4701. if( cat == 0 )
  4702. h->cbp_table[h->mb_xy] |= 0x100;
  4703. else
  4704. h->cbp_table[h->mb_xy] |= 0x40 << n;
  4705. } else {
  4706. if( cat == 5 )
  4707. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  4708. else {
  4709. assert( cat == 1 || cat == 2 || cat == 4 );
  4710. h->non_zero_count_cache[scan8[n]] = coeff_count;
  4711. }
  4712. }
  4713. do {
  4714. uint8_t *ctx = coeff_abs_level1_ctx[node_ctx] + abs_level_m1_ctx_base;
  4715. int j= scantable[index[--coeff_count]];
  4716. if( get_cabac( CC, ctx ) == 0 ) {
  4717. node_ctx = coeff_abs_level_transition[0][node_ctx];
  4718. if( is_dc ) {
  4719. block[j] = get_cabac_bypass_sign( CC, -1);
  4720. }else{
  4721. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;
  4722. }
  4723. } else {
  4724. int coeff_abs = 2;
  4725. ctx = coeff_abs_levelgt1_ctx[node_ctx] + abs_level_m1_ctx_base;
  4726. node_ctx = coeff_abs_level_transition[1][node_ctx];
  4727. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  4728. coeff_abs++;
  4729. }
  4730. if( coeff_abs >= 15 ) {
  4731. int j = 0;
  4732. while( get_cabac_bypass( CC ) ) {
  4733. j++;
  4734. }
  4735. coeff_abs=1;
  4736. while( j-- ) {
  4737. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  4738. }
  4739. coeff_abs+= 14;
  4740. }
  4741. if( is_dc ) {
  4742. block[j] = get_cabac_bypass_sign( CC, -coeff_abs );
  4743. }else{
  4744. block[j] = (get_cabac_bypass_sign( CC, -coeff_abs ) * qmul[j] + 32) >> 6;
  4745. }
  4746. }
  4747. } while( coeff_count );
  4748. #ifdef CABAC_ON_STACK
  4749. h->cabac.range = cc.range ;
  4750. h->cabac.low = cc.low ;
  4751. h->cabac.bytestream= cc.bytestream;
  4752. #endif
  4753. }
  4754. #if !CONFIG_SMALL
  4755. static void decode_cabac_residual_dc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
  4756. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 1);
  4757. }
  4758. static void decode_cabac_residual_nondc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
  4759. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 0);
  4760. }
  4761. #endif
  4762. static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
  4763. #if CONFIG_SMALL
  4764. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, cat == 0 || cat == 3);
  4765. #else
  4766. if( cat == 0 || cat == 3 ) decode_cabac_residual_dc(h, block, cat, n, scantable, qmul, max_coeff);
  4767. else decode_cabac_residual_nondc(h, block, cat, n, scantable, qmul, max_coeff);
  4768. #endif
  4769. }
  4770. static inline void compute_mb_neighbors(H264Context *h)
  4771. {
  4772. MpegEncContext * const s = &h->s;
  4773. const int mb_xy = h->mb_xy;
  4774. h->top_mb_xy = mb_xy - s->mb_stride;
  4775. h->left_mb_xy[0] = mb_xy - 1;
  4776. if(FRAME_MBAFF){
  4777. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  4778. const int top_pair_xy = pair_xy - s->mb_stride;
  4779. const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  4780. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  4781. const int curr_mb_field_flag = MB_FIELD;
  4782. const int bottom = (s->mb_y & 1);
  4783. if (curr_mb_field_flag && (bottom || top_mb_field_flag)){
  4784. h->top_mb_xy -= s->mb_stride;
  4785. }
  4786. if (!left_mb_field_flag == curr_mb_field_flag) {
  4787. h->left_mb_xy[0] = pair_xy - 1;
  4788. }
  4789. } else if (FIELD_PICTURE) {
  4790. h->top_mb_xy -= s->mb_stride;
  4791. }
  4792. return;
  4793. }
  4794. /**
  4795. * decodes a macroblock
  4796. * @returns 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4797. */
  4798. static int decode_mb_cabac(H264Context *h) {
  4799. MpegEncContext * const s = &h->s;
  4800. int mb_xy;
  4801. int mb_type, partition_count, cbp = 0;
  4802. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4803. mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  4804. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4805. if( h->slice_type_nos != FF_I_TYPE ) {
  4806. int skip;
  4807. /* a skipped mb needs the aff flag from the following mb */
  4808. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  4809. predict_field_decoding_flag(h);
  4810. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  4811. skip = h->next_mb_skipped;
  4812. else
  4813. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  4814. /* read skip flags */
  4815. if( skip ) {
  4816. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  4817. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  4818. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  4819. if(!h->next_mb_skipped)
  4820. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  4821. }
  4822. decode_mb_skip(h);
  4823. h->cbp_table[mb_xy] = 0;
  4824. h->chroma_pred_mode_table[mb_xy] = 0;
  4825. h->last_qscale_diff = 0;
  4826. return 0;
  4827. }
  4828. }
  4829. if(FRAME_MBAFF){
  4830. if( (s->mb_y&1) == 0 )
  4831. h->mb_mbaff =
  4832. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  4833. }
  4834. h->prev_mb_skipped = 0;
  4835. compute_mb_neighbors(h);
  4836. if( h->slice_type_nos == FF_B_TYPE ) {
  4837. mb_type = decode_cabac_mb_type_b( h );
  4838. if( mb_type < 23 ){
  4839. partition_count= b_mb_type_info[mb_type].partition_count;
  4840. mb_type= b_mb_type_info[mb_type].type;
  4841. }else{
  4842. mb_type -= 23;
  4843. goto decode_intra_mb;
  4844. }
  4845. } else if( h->slice_type_nos == FF_P_TYPE ) {
  4846. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4847. /* P-type */
  4848. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4849. /* P_L0_D16x16, P_8x8 */
  4850. mb_type= 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4851. } else {
  4852. /* P_L0_D8x16, P_L0_D16x8 */
  4853. mb_type= 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4854. }
  4855. partition_count= p_mb_type_info[mb_type].partition_count;
  4856. mb_type= p_mb_type_info[mb_type].type;
  4857. } else {
  4858. mb_type= decode_cabac_intra_mb_type(h, 17, 0);
  4859. goto decode_intra_mb;
  4860. }
  4861. } else {
  4862. mb_type= decode_cabac_intra_mb_type(h, 3, 1);
  4863. if(h->slice_type == FF_SI_TYPE && mb_type)
  4864. mb_type--;
  4865. assert(h->slice_type_nos == FF_I_TYPE);
  4866. decode_intra_mb:
  4867. partition_count = 0;
  4868. cbp= i_mb_type_info[mb_type].cbp;
  4869. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4870. mb_type= i_mb_type_info[mb_type].type;
  4871. }
  4872. if(MB_FIELD)
  4873. mb_type |= MB_TYPE_INTERLACED;
  4874. h->slice_table[ mb_xy ]= h->slice_num;
  4875. if(IS_INTRA_PCM(mb_type)) {
  4876. const uint8_t *ptr;
  4877. // We assume these blocks are very rare so we do not optimize it.
  4878. // FIXME The two following lines get the bitstream position in the cabac
  4879. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  4880. ptr= h->cabac.bytestream;
  4881. if(h->cabac.low&0x1) ptr--;
  4882. if(CABAC_BITS==16){
  4883. if(h->cabac.low&0x1FF) ptr--;
  4884. }
  4885. // The pixels are stored in the same order as levels in h->mb array.
  4886. memcpy(h->mb, ptr, 256); ptr+=256;
  4887. if(CHROMA){
  4888. memcpy(h->mb+128, ptr, 128); ptr+=128;
  4889. }
  4890. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  4891. // All blocks are present
  4892. h->cbp_table[mb_xy] = 0x1ef;
  4893. h->chroma_pred_mode_table[mb_xy] = 0;
  4894. // In deblocking, the quantizer is 0
  4895. s->current_picture.qscale_table[mb_xy]= 0;
  4896. // All coeffs are present
  4897. memset(h->non_zero_count[mb_xy], 16, 16);
  4898. s->current_picture.mb_type[mb_xy]= mb_type;
  4899. h->last_qscale_diff = 0;
  4900. return 0;
  4901. }
  4902. if(MB_MBAFF){
  4903. h->ref_count[0] <<= 1;
  4904. h->ref_count[1] <<= 1;
  4905. }
  4906. fill_caches(h, mb_type, 0);
  4907. if( IS_INTRA( mb_type ) ) {
  4908. int i, pred_mode;
  4909. if( IS_INTRA4x4( mb_type ) ) {
  4910. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  4911. mb_type |= MB_TYPE_8x8DCT;
  4912. for( i = 0; i < 16; i+=4 ) {
  4913. int pred = pred_intra_mode( h, i );
  4914. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  4915. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4916. }
  4917. } else {
  4918. for( i = 0; i < 16; i++ ) {
  4919. int pred = pred_intra_mode( h, i );
  4920. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  4921. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  4922. }
  4923. }
  4924. write_back_intra_pred_mode(h);
  4925. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  4926. } else {
  4927. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  4928. if( h->intra16x16_pred_mode < 0 ) return -1;
  4929. }
  4930. if(CHROMA){
  4931. h->chroma_pred_mode_table[mb_xy] =
  4932. pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  4933. pred_mode= check_intra_pred_mode( h, pred_mode );
  4934. if( pred_mode < 0 ) return -1;
  4935. h->chroma_pred_mode= pred_mode;
  4936. }
  4937. } else if( partition_count == 4 ) {
  4938. int i, j, sub_partition_count[4], list, ref[2][4];
  4939. if( h->slice_type_nos == FF_B_TYPE ) {
  4940. for( i = 0; i < 4; i++ ) {
  4941. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  4942. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4943. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4944. }
  4945. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  4946. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  4947. pred_direct_motion(h, &mb_type);
  4948. h->ref_cache[0][scan8[4]] =
  4949. h->ref_cache[1][scan8[4]] =
  4950. h->ref_cache[0][scan8[12]] =
  4951. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4952. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  4953. for( i = 0; i < 4; i++ )
  4954. if( IS_DIRECT(h->sub_mb_type[i]) )
  4955. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  4956. }
  4957. }
  4958. } else {
  4959. for( i = 0; i < 4; i++ ) {
  4960. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  4961. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4962. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4963. }
  4964. }
  4965. for( list = 0; list < h->list_count; list++ ) {
  4966. for( i = 0; i < 4; i++ ) {
  4967. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4968. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4969. if( h->ref_count[list] > 1 ){
  4970. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  4971. if(ref[list][i] >= (unsigned)h->ref_count[list]){
  4972. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref[list][i], h->ref_count[list]);
  4973. return -1;
  4974. }
  4975. }else
  4976. ref[list][i] = 0;
  4977. } else {
  4978. ref[list][i] = -1;
  4979. }
  4980. h->ref_cache[list][ scan8[4*i]+1 ]=
  4981. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4982. }
  4983. }
  4984. if(dct8x8_allowed)
  4985. dct8x8_allowed = get_dct8x8_allowed(h);
  4986. for(list=0; list<h->list_count; list++){
  4987. for(i=0; i<4; i++){
  4988. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  4989. if(IS_DIRECT(h->sub_mb_type[i])){
  4990. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  4991. continue;
  4992. }
  4993. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  4994. const int sub_mb_type= h->sub_mb_type[i];
  4995. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4996. for(j=0; j<sub_partition_count[i]; j++){
  4997. int mpx, mpy;
  4998. int mx, my;
  4999. const int index= 4*i + block_width*j;
  5000. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5001. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5002. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5003. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5004. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5005. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5006. if(IS_SUB_8X8(sub_mb_type)){
  5007. mv_cache[ 1 ][0]=
  5008. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5009. mv_cache[ 1 ][1]=
  5010. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5011. mvd_cache[ 1 ][0]=
  5012. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5013. mvd_cache[ 1 ][1]=
  5014. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5015. }else if(IS_SUB_8X4(sub_mb_type)){
  5016. mv_cache[ 1 ][0]= mx;
  5017. mv_cache[ 1 ][1]= my;
  5018. mvd_cache[ 1 ][0]= mx - mpx;
  5019. mvd_cache[ 1 ][1]= my - mpy;
  5020. }else if(IS_SUB_4X8(sub_mb_type)){
  5021. mv_cache[ 8 ][0]= mx;
  5022. mv_cache[ 8 ][1]= my;
  5023. mvd_cache[ 8 ][0]= mx - mpx;
  5024. mvd_cache[ 8 ][1]= my - mpy;
  5025. }
  5026. mv_cache[ 0 ][0]= mx;
  5027. mv_cache[ 0 ][1]= my;
  5028. mvd_cache[ 0 ][0]= mx - mpx;
  5029. mvd_cache[ 0 ][1]= my - mpy;
  5030. }
  5031. }else{
  5032. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5033. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5034. p[0] = p[1] = p[8] = p[9] = 0;
  5035. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5036. }
  5037. }
  5038. }
  5039. } else if( IS_DIRECT(mb_type) ) {
  5040. pred_direct_motion(h, &mb_type);
  5041. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5042. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5043. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5044. } else {
  5045. int list, mx, my, i, mpx, mpy;
  5046. if(IS_16X16(mb_type)){
  5047. for(list=0; list<h->list_count; list++){
  5048. if(IS_DIR(mb_type, 0, list)){
  5049. int ref;
  5050. if(h->ref_count[list] > 1){
  5051. ref= decode_cabac_mb_ref(h, list, 0);
  5052. if(ref >= (unsigned)h->ref_count[list]){
  5053. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, h->ref_count[list]);
  5054. return -1;
  5055. }
  5056. }else
  5057. ref=0;
  5058. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5059. }else
  5060. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1); //FIXME factorize and the other fill_rect below too
  5061. }
  5062. for(list=0; list<h->list_count; list++){
  5063. if(IS_DIR(mb_type, 0, list)){
  5064. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5065. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5066. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5067. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5068. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5069. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5070. }else
  5071. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5072. }
  5073. }
  5074. else if(IS_16X8(mb_type)){
  5075. for(list=0; list<h->list_count; list++){
  5076. for(i=0; i<2; i++){
  5077. if(IS_DIR(mb_type, i, list)){
  5078. int ref;
  5079. if(h->ref_count[list] > 1){
  5080. ref= decode_cabac_mb_ref( h, list, 8*i );
  5081. if(ref >= (unsigned)h->ref_count[list]){
  5082. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, h->ref_count[list]);
  5083. return -1;
  5084. }
  5085. }else
  5086. ref=0;
  5087. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5088. }else
  5089. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5090. }
  5091. }
  5092. for(list=0; list<h->list_count; list++){
  5093. for(i=0; i<2; i++){
  5094. if(IS_DIR(mb_type, i, list)){
  5095. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5096. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5097. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5098. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5099. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5100. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5101. }else{
  5102. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5103. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5104. }
  5105. }
  5106. }
  5107. }else{
  5108. assert(IS_8X16(mb_type));
  5109. for(list=0; list<h->list_count; list++){
  5110. for(i=0; i<2; i++){
  5111. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5112. int ref;
  5113. if(h->ref_count[list] > 1){
  5114. ref= decode_cabac_mb_ref( h, list, 4*i );
  5115. if(ref >= (unsigned)h->ref_count[list]){
  5116. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, h->ref_count[list]);
  5117. return -1;
  5118. }
  5119. }else
  5120. ref=0;
  5121. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5122. }else
  5123. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5124. }
  5125. }
  5126. for(list=0; list<h->list_count; list++){
  5127. for(i=0; i<2; i++){
  5128. if(IS_DIR(mb_type, i, list)){
  5129. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5130. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5131. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5132. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5133. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5134. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5135. }else{
  5136. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5137. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5138. }
  5139. }
  5140. }
  5141. }
  5142. }
  5143. if( IS_INTER( mb_type ) ) {
  5144. h->chroma_pred_mode_table[mb_xy] = 0;
  5145. write_back_motion( h, mb_type );
  5146. }
  5147. if( !IS_INTRA16x16( mb_type ) ) {
  5148. cbp = decode_cabac_mb_cbp_luma( h );
  5149. if(CHROMA)
  5150. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5151. }
  5152. h->cbp_table[mb_xy] = h->cbp = cbp;
  5153. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5154. if( decode_cabac_mb_transform_size( h ) )
  5155. mb_type |= MB_TYPE_8x8DCT;
  5156. }
  5157. s->current_picture.mb_type[mb_xy]= mb_type;
  5158. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5159. const uint8_t *scan, *scan8x8, *dc_scan;
  5160. const uint32_t *qmul;
  5161. int dqp;
  5162. if(IS_INTERLACED(mb_type)){
  5163. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5164. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5165. dc_scan= luma_dc_field_scan;
  5166. }else{
  5167. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5168. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5169. dc_scan= luma_dc_zigzag_scan;
  5170. }
  5171. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5172. if( dqp == INT_MIN ){
  5173. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5174. return -1;
  5175. }
  5176. s->qscale += dqp;
  5177. if(((unsigned)s->qscale) > 51){
  5178. if(s->qscale<0) s->qscale+= 52;
  5179. else s->qscale-= 52;
  5180. }
  5181. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  5182. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  5183. if( IS_INTRA16x16( mb_type ) ) {
  5184. int i;
  5185. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5186. decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16);
  5187. if( cbp&15 ) {
  5188. qmul = h->dequant4_coeff[0][s->qscale];
  5189. for( i = 0; i < 16; i++ ) {
  5190. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5191. decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, qmul, 15);
  5192. }
  5193. } else {
  5194. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5195. }
  5196. } else {
  5197. int i8x8, i4x4;
  5198. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5199. if( cbp & (1<<i8x8) ) {
  5200. if( IS_8x8DCT(mb_type) ) {
  5201. decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5202. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64);
  5203. } else {
  5204. qmul = h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale];
  5205. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5206. const int index = 4*i8x8 + i4x4;
  5207. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5208. //START_TIMER
  5209. decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, qmul, 16);
  5210. //STOP_TIMER("decode_residual")
  5211. }
  5212. }
  5213. } else {
  5214. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5215. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5216. }
  5217. }
  5218. }
  5219. if( cbp&0x30 ){
  5220. int c;
  5221. for( c = 0; c < 2; c++ ) {
  5222. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5223. decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4);
  5224. }
  5225. }
  5226. if( cbp&0x20 ) {
  5227. int c, i;
  5228. for( c = 0; c < 2; c++ ) {
  5229. qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];
  5230. for( i = 0; i < 4; i++ ) {
  5231. const int index = 16 + 4 * c + i;
  5232. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5233. decode_cabac_residual(h, h->mb + 16*index, 4, index, scan + 1, qmul, 15);
  5234. }
  5235. }
  5236. } else {
  5237. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5238. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5239. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5240. }
  5241. } else {
  5242. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5243. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5244. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5245. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5246. h->last_qscale_diff = 0;
  5247. }
  5248. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5249. write_back_non_zero_count(h);
  5250. if(MB_MBAFF){
  5251. h->ref_count[0] >>= 1;
  5252. h->ref_count[1] >>= 1;
  5253. }
  5254. return 0;
  5255. }
  5256. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5257. const int index_a = qp + h->slice_alpha_c0_offset;
  5258. const int alpha = (alpha_table+52)[index_a];
  5259. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5260. if( bS[0] < 4 ) {
  5261. int8_t tc[4];
  5262. tc[0] = (tc0_table+52)[index_a][bS[0]];
  5263. tc[1] = (tc0_table+52)[index_a][bS[1]];
  5264. tc[2] = (tc0_table+52)[index_a][bS[2]];
  5265. tc[3] = (tc0_table+52)[index_a][bS[3]];
  5266. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5267. } else {
  5268. h->s.dsp.h264_h_loop_filter_luma_intra(pix, stride, alpha, beta);
  5269. }
  5270. }
  5271. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5272. const int index_a = qp + h->slice_alpha_c0_offset;
  5273. const int alpha = (alpha_table+52)[index_a];
  5274. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5275. if( bS[0] < 4 ) {
  5276. int8_t tc[4];
  5277. tc[0] = (tc0_table+52)[index_a][bS[0]]+1;
  5278. tc[1] = (tc0_table+52)[index_a][bS[1]]+1;
  5279. tc[2] = (tc0_table+52)[index_a][bS[2]]+1;
  5280. tc[3] = (tc0_table+52)[index_a][bS[3]]+1;
  5281. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5282. } else {
  5283. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5284. }
  5285. }
  5286. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5287. int i;
  5288. for( i = 0; i < 16; i++, pix += stride) {
  5289. int index_a;
  5290. int alpha;
  5291. int beta;
  5292. int qp_index;
  5293. int bS_index = (i >> 1);
  5294. if (!MB_FIELD) {
  5295. bS_index &= ~1;
  5296. bS_index |= (i & 1);
  5297. }
  5298. if( bS[bS_index] == 0 ) {
  5299. continue;
  5300. }
  5301. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  5302. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5303. alpha = (alpha_table+52)[index_a];
  5304. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5305. if( bS[bS_index] < 4 ) {
  5306. const int tc0 = (tc0_table+52)[index_a][bS[bS_index]];
  5307. const int p0 = pix[-1];
  5308. const int p1 = pix[-2];
  5309. const int p2 = pix[-3];
  5310. const int q0 = pix[0];
  5311. const int q1 = pix[1];
  5312. const int q2 = pix[2];
  5313. if( FFABS( p0 - q0 ) < alpha &&
  5314. FFABS( p1 - p0 ) < beta &&
  5315. FFABS( q1 - q0 ) < beta ) {
  5316. int tc = tc0;
  5317. int i_delta;
  5318. if( FFABS( p2 - p0 ) < beta ) {
  5319. pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5320. tc++;
  5321. }
  5322. if( FFABS( q2 - q0 ) < beta ) {
  5323. pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5324. tc++;
  5325. }
  5326. i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5327. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  5328. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  5329. tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  5330. }
  5331. }else{
  5332. const int p0 = pix[-1];
  5333. const int p1 = pix[-2];
  5334. const int p2 = pix[-3];
  5335. const int q0 = pix[0];
  5336. const int q1 = pix[1];
  5337. const int q2 = pix[2];
  5338. if( FFABS( p0 - q0 ) < alpha &&
  5339. FFABS( p1 - p0 ) < beta &&
  5340. FFABS( q1 - q0 ) < beta ) {
  5341. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5342. if( FFABS( p2 - p0 ) < beta)
  5343. {
  5344. const int p3 = pix[-4];
  5345. /* p0', p1', p2' */
  5346. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5347. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5348. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5349. } else {
  5350. /* p0' */
  5351. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5352. }
  5353. if( FFABS( q2 - q0 ) < beta)
  5354. {
  5355. const int q3 = pix[3];
  5356. /* q0', q1', q2' */
  5357. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5358. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5359. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5360. } else {
  5361. /* q0' */
  5362. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5363. }
  5364. }else{
  5365. /* p0', q0' */
  5366. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5367. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5368. }
  5369. tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
  5370. }
  5371. }
  5372. }
  5373. }
  5374. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5375. int i;
  5376. for( i = 0; i < 8; i++, pix += stride) {
  5377. int index_a;
  5378. int alpha;
  5379. int beta;
  5380. int qp_index;
  5381. int bS_index = i;
  5382. if( bS[bS_index] == 0 ) {
  5383. continue;
  5384. }
  5385. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  5386. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5387. alpha = (alpha_table+52)[index_a];
  5388. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5389. if( bS[bS_index] < 4 ) {
  5390. const int tc = (tc0_table+52)[index_a][bS[bS_index]] + 1;
  5391. const int p0 = pix[-1];
  5392. const int p1 = pix[-2];
  5393. const int q0 = pix[0];
  5394. const int q1 = pix[1];
  5395. if( FFABS( p0 - q0 ) < alpha &&
  5396. FFABS( p1 - p0 ) < beta &&
  5397. FFABS( q1 - q0 ) < beta ) {
  5398. const int i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5399. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  5400. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  5401. tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  5402. }
  5403. }else{
  5404. const int p0 = pix[-1];
  5405. const int p1 = pix[-2];
  5406. const int q0 = pix[0];
  5407. const int q1 = pix[1];
  5408. if( FFABS( p0 - q0 ) < alpha &&
  5409. FFABS( p1 - p0 ) < beta &&
  5410. FFABS( q1 - q0 ) < beta ) {
  5411. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  5412. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  5413. tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, pix[-3], p1, p0, q0, q1, pix[2], pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
  5414. }
  5415. }
  5416. }
  5417. }
  5418. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5419. const int index_a = qp + h->slice_alpha_c0_offset;
  5420. const int alpha = (alpha_table+52)[index_a];
  5421. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5422. if( bS[0] < 4 ) {
  5423. int8_t tc[4];
  5424. tc[0] = (tc0_table+52)[index_a][bS[0]];
  5425. tc[1] = (tc0_table+52)[index_a][bS[1]];
  5426. tc[2] = (tc0_table+52)[index_a][bS[2]];
  5427. tc[3] = (tc0_table+52)[index_a][bS[3]];
  5428. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  5429. } else {
  5430. h->s.dsp.h264_v_loop_filter_luma_intra(pix, stride, alpha, beta);
  5431. }
  5432. }
  5433. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5434. const int index_a = qp + h->slice_alpha_c0_offset;
  5435. const int alpha = (alpha_table+52)[index_a];
  5436. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5437. if( bS[0] < 4 ) {
  5438. int8_t tc[4];
  5439. tc[0] = (tc0_table+52)[index_a][bS[0]]+1;
  5440. tc[1] = (tc0_table+52)[index_a][bS[1]]+1;
  5441. tc[2] = (tc0_table+52)[index_a][bS[2]]+1;
  5442. tc[3] = (tc0_table+52)[index_a][bS[3]]+1;
  5443. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5444. } else {
  5445. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5446. }
  5447. }
  5448. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
  5449. MpegEncContext * const s = &h->s;
  5450. int mb_y_firstrow = s->picture_structure == PICT_BOTTOM_FIELD;
  5451. int mb_xy, mb_type;
  5452. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  5453. mb_xy = h->mb_xy;
  5454. if(mb_x==0 || mb_y==mb_y_firstrow || !s->dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff ||
  5455. !(s->flags2 & CODEC_FLAG2_FAST) || //FIXME filter_mb_fast is broken, thus hasto be, but should not under CODEC_FLAG2_FAST
  5456. (h->deblocking_filter == 2 && (h->slice_table[mb_xy] != h->slice_table[h->top_mb_xy] ||
  5457. h->slice_table[mb_xy] != h->slice_table[mb_xy - 1]))) {
  5458. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  5459. return;
  5460. }
  5461. assert(!FRAME_MBAFF);
  5462. mb_type = s->current_picture.mb_type[mb_xy];
  5463. qp = s->current_picture.qscale_table[mb_xy];
  5464. qp0 = s->current_picture.qscale_table[mb_xy-1];
  5465. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  5466. qpc = get_chroma_qp( h, 0, qp );
  5467. qpc0 = get_chroma_qp( h, 0, qp0 );
  5468. qpc1 = get_chroma_qp( h, 0, qp1 );
  5469. qp0 = (qp + qp0 + 1) >> 1;
  5470. qp1 = (qp + qp1 + 1) >> 1;
  5471. qpc0 = (qpc + qpc0 + 1) >> 1;
  5472. qpc1 = (qpc + qpc1 + 1) >> 1;
  5473. qp_thresh = 15 - h->slice_alpha_c0_offset;
  5474. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  5475. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  5476. return;
  5477. if( IS_INTRA(mb_type) ) {
  5478. int16_t bS4[4] = {4,4,4,4};
  5479. int16_t bS3[4] = {3,3,3,3};
  5480. int16_t *bSH = FIELD_PICTURE ? bS3 : bS4;
  5481. if( IS_8x8DCT(mb_type) ) {
  5482. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  5483. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  5484. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
  5485. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  5486. } else {
  5487. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  5488. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  5489. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  5490. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  5491. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
  5492. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  5493. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  5494. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  5495. }
  5496. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  5497. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  5498. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  5499. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  5500. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
  5501. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  5502. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
  5503. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  5504. return;
  5505. } else {
  5506. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  5507. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  5508. int edges;
  5509. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  5510. edges = 4;
  5511. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  5512. } else {
  5513. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  5514. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  5515. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  5516. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  5517. ? 3 : 0;
  5518. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  5519. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  5520. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  5521. (h->slice_type_nos == FF_B_TYPE), edges, step, mask_edge0, mask_edge1, FIELD_PICTURE);
  5522. }
  5523. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  5524. bSv[0][0] = 0x0004000400040004ULL;
  5525. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  5526. bSv[1][0] = FIELD_PICTURE ? 0x0003000300030003ULL : 0x0004000400040004ULL;
  5527. #define FILTER(hv,dir,edge)\
  5528. if(bSv[dir][edge]) {\
  5529. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  5530. if(!(edge&1)) {\
  5531. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  5532. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  5533. }\
  5534. }
  5535. if( edges == 1 ) {
  5536. FILTER(v,0,0);
  5537. FILTER(h,1,0);
  5538. } else if( IS_8x8DCT(mb_type) ) {
  5539. FILTER(v,0,0);
  5540. FILTER(v,0,2);
  5541. FILTER(h,1,0);
  5542. FILTER(h,1,2);
  5543. } else {
  5544. FILTER(v,0,0);
  5545. FILTER(v,0,1);
  5546. FILTER(v,0,2);
  5547. FILTER(v,0,3);
  5548. FILTER(h,1,0);
  5549. FILTER(h,1,1);
  5550. FILTER(h,1,2);
  5551. FILTER(h,1,3);
  5552. }
  5553. #undef FILTER
  5554. }
  5555. }
  5556. static av_always_inline void filter_mb_dir(H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize, int mb_xy, int mb_type, int mvy_limit, int first_vertical_edge_done, int dir) {
  5557. MpegEncContext * const s = &h->s;
  5558. int edge;
  5559. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  5560. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  5561. int (*ref2frm) [64] = h->ref2frm[ h->slice_num &(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  5562. int (*ref2frmm)[64] = h->ref2frm[ h->slice_table[mbm_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  5563. int start = h->slice_table[mbm_xy] == 0xFFFF ? 1 : 0;
  5564. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  5565. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  5566. // how often to recheck mv-based bS when iterating between edges
  5567. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  5568. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  5569. // how often to recheck mv-based bS when iterating along each edge
  5570. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  5571. if (first_vertical_edge_done) {
  5572. start = 1;
  5573. }
  5574. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  5575. start = 1;
  5576. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  5577. && !IS_INTERLACED(mb_type)
  5578. && IS_INTERLACED(mbm_type)
  5579. ) {
  5580. // This is a special case in the norm where the filtering must
  5581. // be done twice (one each of the field) even if we are in a
  5582. // frame macroblock.
  5583. //
  5584. static const int nnz_idx[4] = {4,5,6,3};
  5585. unsigned int tmp_linesize = 2 * linesize;
  5586. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  5587. int mbn_xy = mb_xy - 2 * s->mb_stride;
  5588. int qp;
  5589. int i, j;
  5590. int16_t bS[4];
  5591. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  5592. if( IS_INTRA(mb_type) ||
  5593. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  5594. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  5595. } else {
  5596. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  5597. for( i = 0; i < 4; i++ ) {
  5598. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  5599. mbn_nnz[nnz_idx[i]] != 0 )
  5600. bS[i] = 2;
  5601. else
  5602. bS[i] = 1;
  5603. }
  5604. }
  5605. // Do not use s->qscale as luma quantizer because it has not the same
  5606. // value in IPCM macroblocks.
  5607. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5608. tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
  5609. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  5610. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  5611. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS,
  5612. ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5613. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS,
  5614. ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5615. }
  5616. start = 1;
  5617. }
  5618. /* Calculate bS */
  5619. for( edge = start; edge < edges; edge++ ) {
  5620. /* mbn_xy: neighbor macroblock */
  5621. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  5622. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  5623. int (*ref2frmn)[64] = edge > 0 ? ref2frm : ref2frmm;
  5624. int16_t bS[4];
  5625. int qp;
  5626. if( (edge&1) && IS_8x8DCT(mb_type) )
  5627. continue;
  5628. if( IS_INTRA(mb_type) ||
  5629. IS_INTRA(mbn_type) ) {
  5630. int value;
  5631. if (edge == 0) {
  5632. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  5633. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  5634. ) {
  5635. value = 4;
  5636. } else {
  5637. value = 3;
  5638. }
  5639. } else {
  5640. value = 3;
  5641. }
  5642. bS[0] = bS[1] = bS[2] = bS[3] = value;
  5643. } else {
  5644. int i, l;
  5645. int mv_done;
  5646. if( edge & mask_edge ) {
  5647. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  5648. mv_done = 1;
  5649. }
  5650. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  5651. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  5652. mv_done = 1;
  5653. }
  5654. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  5655. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  5656. int bn_idx= b_idx - (dir ? 8:1);
  5657. int v = 0;
  5658. for( l = 0; !v && l < 1 + (h->slice_type_nos == FF_B_TYPE); l++ ) {
  5659. v |= ref2frm[l][h->ref_cache[l][b_idx]] != ref2frmn[l][h->ref_cache[l][bn_idx]] ||
  5660. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  5661. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  5662. }
  5663. if(h->slice_type_nos == FF_B_TYPE && v){
  5664. v=0;
  5665. for( l = 0; !v && l < 2; l++ ) {
  5666. int ln= 1-l;
  5667. v |= ref2frm[l][h->ref_cache[l][b_idx]] != ref2frmn[ln][h->ref_cache[ln][bn_idx]] ||
  5668. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[ln][bn_idx][0] ) >= 4 ||
  5669. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[ln][bn_idx][1] ) >= mvy_limit;
  5670. }
  5671. }
  5672. bS[0] = bS[1] = bS[2] = bS[3] = v;
  5673. mv_done = 1;
  5674. }
  5675. else
  5676. mv_done = 0;
  5677. for( i = 0; i < 4; i++ ) {
  5678. int x = dir == 0 ? edge : i;
  5679. int y = dir == 0 ? i : edge;
  5680. int b_idx= 8 + 4 + x + 8*y;
  5681. int bn_idx= b_idx - (dir ? 8:1);
  5682. if( h->non_zero_count_cache[b_idx] |
  5683. h->non_zero_count_cache[bn_idx] ) {
  5684. bS[i] = 2;
  5685. }
  5686. else if(!mv_done)
  5687. {
  5688. bS[i] = 0;
  5689. for( l = 0; l < 1 + (h->slice_type_nos == FF_B_TYPE); l++ ) {
  5690. if( ref2frm[l][h->ref_cache[l][b_idx]] != ref2frmn[l][h->ref_cache[l][bn_idx]] ||
  5691. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  5692. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  5693. bS[i] = 1;
  5694. break;
  5695. }
  5696. }
  5697. if(h->slice_type_nos == FF_B_TYPE && bS[i]){
  5698. bS[i] = 0;
  5699. for( l = 0; l < 2; l++ ) {
  5700. int ln= 1-l;
  5701. if( ref2frm[l][h->ref_cache[l][b_idx]] != ref2frmn[ln][h->ref_cache[ln][bn_idx]] ||
  5702. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[ln][bn_idx][0] ) >= 4 ||
  5703. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[ln][bn_idx][1] ) >= mvy_limit ) {
  5704. bS[i] = 1;
  5705. break;
  5706. }
  5707. }
  5708. }
  5709. }
  5710. }
  5711. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  5712. continue;
  5713. }
  5714. /* Filter edge */
  5715. // Do not use s->qscale as luma quantizer because it has not the same
  5716. // value in IPCM macroblocks.
  5717. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5718. //tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp, s->current_picture.qscale_table[mbn_xy]);
  5719. tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  5720. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  5721. if( dir == 0 ) {
  5722. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  5723. if( (edge&1) == 0 ) {
  5724. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS,
  5725. ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5726. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS,
  5727. ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5728. }
  5729. } else {
  5730. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  5731. if( (edge&1) == 0 ) {
  5732. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS,
  5733. ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5734. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS,
  5735. ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
  5736. }
  5737. }
  5738. }
  5739. }
  5740. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
  5741. MpegEncContext * const s = &h->s;
  5742. const int mb_xy= mb_x + mb_y*s->mb_stride;
  5743. const int mb_type = s->current_picture.mb_type[mb_xy];
  5744. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  5745. int first_vertical_edge_done = 0;
  5746. av_unused int dir;
  5747. //for sufficiently low qp, filtering wouldn't do anything
  5748. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  5749. if(!FRAME_MBAFF){
  5750. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
  5751. int qp = s->current_picture.qscale_table[mb_xy];
  5752. if(qp <= qp_thresh
  5753. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  5754. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  5755. return;
  5756. }
  5757. }
  5758. // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
  5759. if(!h->pps.cabac && h->pps.transform_8x8_mode){
  5760. int top_type, left_type[2];
  5761. top_type = s->current_picture.mb_type[h->top_mb_xy] ;
  5762. left_type[0] = s->current_picture.mb_type[h->left_mb_xy[0]];
  5763. left_type[1] = s->current_picture.mb_type[h->left_mb_xy[1]];
  5764. if(IS_8x8DCT(top_type)){
  5765. h->non_zero_count_cache[4+8*0]=
  5766. h->non_zero_count_cache[5+8*0]= h->cbp_table[h->top_mb_xy] & 4;
  5767. h->non_zero_count_cache[6+8*0]=
  5768. h->non_zero_count_cache[7+8*0]= h->cbp_table[h->top_mb_xy] & 8;
  5769. }
  5770. if(IS_8x8DCT(left_type[0])){
  5771. h->non_zero_count_cache[3+8*1]=
  5772. h->non_zero_count_cache[3+8*2]= h->cbp_table[h->left_mb_xy[0]]&2; //FIXME check MBAFF
  5773. }
  5774. if(IS_8x8DCT(left_type[1])){
  5775. h->non_zero_count_cache[3+8*3]=
  5776. h->non_zero_count_cache[3+8*4]= h->cbp_table[h->left_mb_xy[1]]&8; //FIXME check MBAFF
  5777. }
  5778. if(IS_8x8DCT(mb_type)){
  5779. h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
  5780. h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
  5781. h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
  5782. h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
  5783. h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
  5784. h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
  5785. h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
  5786. h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
  5787. }
  5788. }
  5789. if (FRAME_MBAFF
  5790. // left mb is in picture
  5791. && h->slice_table[mb_xy-1] != 0xFFFF
  5792. // and current and left pair do not have the same interlaced type
  5793. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  5794. // and left mb is in the same slice if deblocking_filter == 2
  5795. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  5796. /* First vertical edge is different in MBAFF frames
  5797. * There are 8 different bS to compute and 2 different Qp
  5798. */
  5799. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  5800. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  5801. int16_t bS[8];
  5802. int qp[2];
  5803. int bqp[2];
  5804. int rqp[2];
  5805. int mb_qp, mbn0_qp, mbn1_qp;
  5806. int i;
  5807. first_vertical_edge_done = 1;
  5808. if( IS_INTRA(mb_type) )
  5809. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  5810. else {
  5811. for( i = 0; i < 8; i++ ) {
  5812. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  5813. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  5814. bS[i] = 4;
  5815. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  5816. ((!h->pps.cabac && IS_8x8DCT(s->current_picture.mb_type[mbn_xy])) ?
  5817. (h->cbp_table[mbn_xy] & ((MB_FIELD ? (i&2) : (mb_y&1)) ? 8 : 2))
  5818. :
  5819. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2]))
  5820. bS[i] = 2;
  5821. else
  5822. bS[i] = 1;
  5823. }
  5824. }
  5825. mb_qp = s->current_picture.qscale_table[mb_xy];
  5826. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  5827. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  5828. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  5829. bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +
  5830. get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;
  5831. rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) +
  5832. get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1;
  5833. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  5834. bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) +
  5835. get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1;
  5836. rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) +
  5837. get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1;
  5838. /* Filter edge */
  5839. tprintf(s->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPb:%d/%d QPr:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], bqp[0], bqp[1], rqp[0], rqp[1], linesize, uvlinesize);
  5840. { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  5841. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  5842. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, bqp );
  5843. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, rqp );
  5844. }
  5845. #if CONFIG_SMALL
  5846. for( dir = 0; dir < 2; dir++ )
  5847. filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, dir ? 0 : first_vertical_edge_done, dir);
  5848. #else
  5849. filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, first_vertical_edge_done, 0);
  5850. filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, 0, 1);
  5851. #endif
  5852. }
  5853. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  5854. H264Context *h = *(void**)arg;
  5855. MpegEncContext * const s = &h->s;
  5856. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  5857. s->mb_skip_run= -1;
  5858. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  5859. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  5860. if( h->pps.cabac ) {
  5861. int i;
  5862. /* realign */
  5863. align_get_bits( &s->gb );
  5864. /* init cabac */
  5865. ff_init_cabac_states( &h->cabac);
  5866. ff_init_cabac_decoder( &h->cabac,
  5867. s->gb.buffer + get_bits_count(&s->gb)/8,
  5868. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  5869. /* calculate pre-state */
  5870. for( i= 0; i < 460; i++ ) {
  5871. int pre;
  5872. if( h->slice_type_nos == FF_I_TYPE )
  5873. pre = av_clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  5874. else
  5875. pre = av_clip( ((cabac_context_init_PB[h->cabac_init_idc][i][0] * s->qscale) >>4 ) + cabac_context_init_PB[h->cabac_init_idc][i][1], 1, 126 );
  5876. if( pre <= 63 )
  5877. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  5878. else
  5879. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  5880. }
  5881. for(;;){
  5882. //START_TIMER
  5883. int ret = decode_mb_cabac(h);
  5884. int eos;
  5885. //STOP_TIMER("decode_mb_cabac")
  5886. if(ret>=0) hl_decode_mb(h);
  5887. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  5888. s->mb_y++;
  5889. ret = decode_mb_cabac(h);
  5890. if(ret>=0) hl_decode_mb(h);
  5891. s->mb_y--;
  5892. }
  5893. eos = get_cabac_terminate( &h->cabac );
  5894. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  5895. 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);
  5896. 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);
  5897. return -1;
  5898. }
  5899. if( ++s->mb_x >= s->mb_width ) {
  5900. s->mb_x = 0;
  5901. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5902. ++s->mb_y;
  5903. if(FIELD_OR_MBAFF_PICTURE) {
  5904. ++s->mb_y;
  5905. }
  5906. }
  5907. if( eos || s->mb_y >= s->mb_height ) {
  5908. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5909. 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);
  5910. return 0;
  5911. }
  5912. }
  5913. } else {
  5914. for(;;){
  5915. int ret = decode_mb_cavlc(h);
  5916. if(ret>=0) hl_decode_mb(h);
  5917. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  5918. s->mb_y++;
  5919. ret = decode_mb_cavlc(h);
  5920. if(ret>=0) hl_decode_mb(h);
  5921. s->mb_y--;
  5922. }
  5923. if(ret<0){
  5924. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  5925. 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);
  5926. return -1;
  5927. }
  5928. if(++s->mb_x >= s->mb_width){
  5929. s->mb_x=0;
  5930. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5931. ++s->mb_y;
  5932. if(FIELD_OR_MBAFF_PICTURE) {
  5933. ++s->mb_y;
  5934. }
  5935. if(s->mb_y >= s->mb_height){
  5936. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5937. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  5938. 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);
  5939. return 0;
  5940. }else{
  5941. 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);
  5942. return -1;
  5943. }
  5944. }
  5945. }
  5946. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  5947. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5948. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  5949. 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);
  5950. return 0;
  5951. }else{
  5952. 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);
  5953. return -1;
  5954. }
  5955. }
  5956. }
  5957. }
  5958. #if 0
  5959. for(;s->mb_y < s->mb_height; s->mb_y++){
  5960. for(;s->mb_x < s->mb_width; s->mb_x++){
  5961. int ret= decode_mb(h);
  5962. hl_decode_mb(h);
  5963. if(ret<0){
  5964. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  5965. 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);
  5966. return -1;
  5967. }
  5968. if(++s->mb_x >= s->mb_width){
  5969. s->mb_x=0;
  5970. if(++s->mb_y >= s->mb_height){
  5971. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  5972. 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);
  5973. return 0;
  5974. }else{
  5975. 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);
  5976. return -1;
  5977. }
  5978. }
  5979. }
  5980. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  5981. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  5982. 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);
  5983. return 0;
  5984. }else{
  5985. 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);
  5986. return -1;
  5987. }
  5988. }
  5989. }
  5990. s->mb_x=0;
  5991. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5992. }
  5993. #endif
  5994. return -1; //not reached
  5995. }
  5996. static int decode_picture_timing(H264Context *h){
  5997. MpegEncContext * const s = &h->s;
  5998. if(h->sps.nal_hrd_parameters_present_flag || h->sps.vcl_hrd_parameters_present_flag){
  5999. h->sei_cpb_removal_delay = get_bits(&s->gb, h->sps.cpb_removal_delay_length);
  6000. h->sei_dpb_output_delay = get_bits(&s->gb, h->sps.dpb_output_delay_length);
  6001. }
  6002. if(h->sps.pic_struct_present_flag){
  6003. unsigned int i, num_clock_ts;
  6004. h->sei_pic_struct = get_bits(&s->gb, 4);
  6005. if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
  6006. return -1;
  6007. num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
  6008. for (i = 0 ; i < num_clock_ts ; i++){
  6009. if(get_bits(&s->gb, 1)){ /* clock_timestamp_flag */
  6010. unsigned int full_timestamp_flag;
  6011. skip_bits(&s->gb, 2); /* ct_type */
  6012. skip_bits(&s->gb, 1); /* nuit_field_based_flag */
  6013. skip_bits(&s->gb, 5); /* counting_type */
  6014. full_timestamp_flag = get_bits(&s->gb, 1);
  6015. skip_bits(&s->gb, 1); /* discontinuity_flag */
  6016. skip_bits(&s->gb, 1); /* cnt_dropped_flag */
  6017. skip_bits(&s->gb, 8); /* n_frames */
  6018. if(full_timestamp_flag){
  6019. skip_bits(&s->gb, 6); /* seconds_value 0..59 */
  6020. skip_bits(&s->gb, 6); /* minutes_value 0..59 */
  6021. skip_bits(&s->gb, 5); /* hours_value 0..23 */
  6022. }else{
  6023. if(get_bits(&s->gb, 1)){ /* seconds_flag */
  6024. skip_bits(&s->gb, 6); /* seconds_value range 0..59 */
  6025. if(get_bits(&s->gb, 1)){ /* minutes_flag */
  6026. skip_bits(&s->gb, 6); /* minutes_value 0..59 */
  6027. if(get_bits(&s->gb, 1)) /* hours_flag */
  6028. skip_bits(&s->gb, 5); /* hours_value 0..23 */
  6029. }
  6030. }
  6031. }
  6032. if(h->sps.time_offset_length > 0)
  6033. skip_bits(&s->gb, h->sps.time_offset_length); /* time_offset */
  6034. }
  6035. }
  6036. }
  6037. return 0;
  6038. }
  6039. static int decode_unregistered_user_data(H264Context *h, int size){
  6040. MpegEncContext * const s = &h->s;
  6041. uint8_t user_data[16+256];
  6042. int e, build, i;
  6043. if(size<16)
  6044. return -1;
  6045. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6046. user_data[i]= get_bits(&s->gb, 8);
  6047. }
  6048. user_data[i]= 0;
  6049. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6050. if(e==1 && build>=0)
  6051. h->x264_build= build;
  6052. if(s->avctx->debug & FF_DEBUG_BUGS)
  6053. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6054. for(; i<size; i++)
  6055. skip_bits(&s->gb, 8);
  6056. return 0;
  6057. }
  6058. static int decode_recovery_point(H264Context *h){
  6059. MpegEncContext * const s = &h->s;
  6060. h->sei_recovery_frame_cnt = get_ue_golomb(&s->gb);
  6061. skip_bits(&s->gb, 4); /* 1b exact_match_flag, 1b broken_link_flag, 2b changing_slice_group_idc */
  6062. return 0;
  6063. }
  6064. static int decode_buffering_period(H264Context *h){
  6065. MpegEncContext * const s = &h->s;
  6066. unsigned int sps_id;
  6067. int sched_sel_idx;
  6068. SPS *sps;
  6069. sps_id = get_ue_golomb_31(&s->gb);
  6070. if(sps_id > 31 || !h->sps_buffers[sps_id]) {
  6071. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %d referenced in buffering period\n", sps_id);
  6072. return -1;
  6073. }
  6074. sps = h->sps_buffers[sps_id];
  6075. // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
  6076. if (sps->nal_hrd_parameters_present_flag) {
  6077. for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
  6078. h->initial_cpb_removal_delay[sched_sel_idx] = get_bits(&s->gb, sps->initial_cpb_removal_delay_length);
  6079. skip_bits(&s->gb, sps->initial_cpb_removal_delay_length); // initial_cpb_removal_delay_offset
  6080. }
  6081. }
  6082. if (sps->vcl_hrd_parameters_present_flag) {
  6083. for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
  6084. h->initial_cpb_removal_delay[sched_sel_idx] = get_bits(&s->gb, sps->initial_cpb_removal_delay_length);
  6085. skip_bits(&s->gb, sps->initial_cpb_removal_delay_length); // initial_cpb_removal_delay_offset
  6086. }
  6087. }
  6088. h->sei_buffering_period_present = 1;
  6089. return 0;
  6090. }
  6091. int ff_h264_decode_sei(H264Context *h){
  6092. MpegEncContext * const s = &h->s;
  6093. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6094. int size, type;
  6095. type=0;
  6096. do{
  6097. type+= show_bits(&s->gb, 8);
  6098. }while(get_bits(&s->gb, 8) == 255);
  6099. size=0;
  6100. do{
  6101. size+= show_bits(&s->gb, 8);
  6102. }while(get_bits(&s->gb, 8) == 255);
  6103. switch(type){
  6104. case SEI_TYPE_PIC_TIMING: // Picture timing SEI
  6105. if(decode_picture_timing(h) < 0)
  6106. return -1;
  6107. break;
  6108. case SEI_TYPE_USER_DATA_UNREGISTERED:
  6109. if(decode_unregistered_user_data(h, size) < 0)
  6110. return -1;
  6111. break;
  6112. case SEI_TYPE_RECOVERY_POINT:
  6113. if(decode_recovery_point(h) < 0)
  6114. return -1;
  6115. break;
  6116. case SEI_BUFFERING_PERIOD:
  6117. if(decode_buffering_period(h) < 0)
  6118. return -1;
  6119. break;
  6120. default:
  6121. skip_bits(&s->gb, 8*size);
  6122. }
  6123. //FIXME check bits here
  6124. align_get_bits(&s->gb);
  6125. }
  6126. return 0;
  6127. }
  6128. static inline int decode_hrd_parameters(H264Context *h, SPS *sps){
  6129. MpegEncContext * const s = &h->s;
  6130. int cpb_count, i;
  6131. cpb_count = get_ue_golomb_31(&s->gb) + 1;
  6132. if(cpb_count > 32U){
  6133. av_log(h->s.avctx, AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count);
  6134. return -1;
  6135. }
  6136. get_bits(&s->gb, 4); /* bit_rate_scale */
  6137. get_bits(&s->gb, 4); /* cpb_size_scale */
  6138. for(i=0; i<cpb_count; i++){
  6139. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6140. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6141. get_bits1(&s->gb); /* cbr_flag */
  6142. }
  6143. sps->initial_cpb_removal_delay_length = get_bits(&s->gb, 5) + 1;
  6144. sps->cpb_removal_delay_length = get_bits(&s->gb, 5) + 1;
  6145. sps->dpb_output_delay_length = get_bits(&s->gb, 5) + 1;
  6146. sps->time_offset_length = get_bits(&s->gb, 5);
  6147. sps->cpb_cnt = cpb_count;
  6148. return 0;
  6149. }
  6150. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6151. MpegEncContext * const s = &h->s;
  6152. int aspect_ratio_info_present_flag;
  6153. unsigned int aspect_ratio_idc;
  6154. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6155. if( aspect_ratio_info_present_flag ) {
  6156. aspect_ratio_idc= get_bits(&s->gb, 8);
  6157. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6158. sps->sar.num= get_bits(&s->gb, 16);
  6159. sps->sar.den= get_bits(&s->gb, 16);
  6160. }else if(aspect_ratio_idc < FF_ARRAY_ELEMS(pixel_aspect)){
  6161. sps->sar= pixel_aspect[aspect_ratio_idc];
  6162. }else{
  6163. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6164. return -1;
  6165. }
  6166. }else{
  6167. sps->sar.num=
  6168. sps->sar.den= 0;
  6169. }
  6170. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6171. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6172. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6173. }
  6174. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6175. get_bits(&s->gb, 3); /* video_format */
  6176. get_bits1(&s->gb); /* video_full_range_flag */
  6177. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6178. get_bits(&s->gb, 8); /* colour_primaries */
  6179. get_bits(&s->gb, 8); /* transfer_characteristics */
  6180. get_bits(&s->gb, 8); /* matrix_coefficients */
  6181. }
  6182. }
  6183. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6184. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6185. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6186. }
  6187. sps->timing_info_present_flag = get_bits1(&s->gb);
  6188. if(sps->timing_info_present_flag){
  6189. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6190. sps->time_scale = get_bits_long(&s->gb, 32);
  6191. if(sps->num_units_in_tick-1 > 0x7FFFFFFEU || sps->time_scale-1 > 0x7FFFFFFEU){
  6192. av_log(h->s.avctx, AV_LOG_ERROR, "time_scale/num_units_in_tick inavlid or unsupported (%d/%d)\n", sps->time_scale, sps->num_units_in_tick);
  6193. return -1;
  6194. }
  6195. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6196. }
  6197. sps->nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6198. if(sps->nal_hrd_parameters_present_flag)
  6199. if(decode_hrd_parameters(h, sps) < 0)
  6200. return -1;
  6201. sps->vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6202. if(sps->vcl_hrd_parameters_present_flag)
  6203. if(decode_hrd_parameters(h, sps) < 0)
  6204. return -1;
  6205. if(sps->nal_hrd_parameters_present_flag || sps->vcl_hrd_parameters_present_flag)
  6206. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6207. sps->pic_struct_present_flag = get_bits1(&s->gb);
  6208. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6209. if(sps->bitstream_restriction_flag){
  6210. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6211. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6212. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6213. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6214. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6215. sps->num_reorder_frames= get_ue_golomb(&s->gb);
  6216. get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/
  6217. if(sps->num_reorder_frames > 16U /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
  6218. av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", sps->num_reorder_frames);
  6219. return -1;
  6220. }
  6221. }
  6222. return 0;
  6223. }
  6224. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6225. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6226. MpegEncContext * const s = &h->s;
  6227. int i, last = 8, next = 8;
  6228. const uint8_t *scan = size == 16 ? zigzag_scan : ff_zigzag_direct;
  6229. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6230. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6231. else
  6232. for(i=0;i<size;i++){
  6233. if(next)
  6234. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6235. if(!i && !next){ /* matrix not written, we use the preset one */
  6236. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6237. break;
  6238. }
  6239. last = factors[scan[i]] = next ? next : last;
  6240. }
  6241. }
  6242. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6243. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6244. MpegEncContext * const s = &h->s;
  6245. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6246. const uint8_t *fallback[4] = {
  6247. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6248. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6249. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6250. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6251. };
  6252. if(get_bits1(&s->gb)){
  6253. sps->scaling_matrix_present |= is_sps;
  6254. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6255. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6256. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6257. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6258. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6259. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6260. if(is_sps || pps->transform_8x8_mode){
  6261. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6262. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6263. }
  6264. }
  6265. }
  6266. int ff_h264_decode_seq_parameter_set(H264Context *h){
  6267. MpegEncContext * const s = &h->s;
  6268. int profile_idc, level_idc;
  6269. unsigned int sps_id;
  6270. int i;
  6271. SPS *sps;
  6272. profile_idc= get_bits(&s->gb, 8);
  6273. get_bits1(&s->gb); //constraint_set0_flag
  6274. get_bits1(&s->gb); //constraint_set1_flag
  6275. get_bits1(&s->gb); //constraint_set2_flag
  6276. get_bits1(&s->gb); //constraint_set3_flag
  6277. get_bits(&s->gb, 4); // reserved
  6278. level_idc= get_bits(&s->gb, 8);
  6279. sps_id= get_ue_golomb_31(&s->gb);
  6280. if(sps_id >= MAX_SPS_COUNT) {
  6281. av_log(h->s.avctx, AV_LOG_ERROR, "sps_id (%d) out of range\n", sps_id);
  6282. return -1;
  6283. }
  6284. sps= av_mallocz(sizeof(SPS));
  6285. if(sps == NULL)
  6286. return -1;
  6287. sps->profile_idc= profile_idc;
  6288. sps->level_idc= level_idc;
  6289. memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
  6290. memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
  6291. sps->scaling_matrix_present = 0;
  6292. if(sps->profile_idc >= 100){ //high profile
  6293. sps->chroma_format_idc= get_ue_golomb_31(&s->gb);
  6294. if(sps->chroma_format_idc > 3) {
  6295. av_log(h->s.avctx, AV_LOG_ERROR, "chroma_format_idc (%u) out of range\n", sps->chroma_format_idc);
  6296. return -1;
  6297. } else if(sps->chroma_format_idc == 3) {
  6298. sps->residual_color_transform_flag = get_bits1(&s->gb);
  6299. }
  6300. sps->bit_depth_luma = get_ue_golomb(&s->gb) + 8;
  6301. sps->bit_depth_chroma = get_ue_golomb(&s->gb) + 8;
  6302. sps->transform_bypass = get_bits1(&s->gb);
  6303. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6304. }else{
  6305. sps->chroma_format_idc= 1;
  6306. }
  6307. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6308. sps->poc_type= get_ue_golomb_31(&s->gb);
  6309. if(sps->poc_type == 0){ //FIXME #define
  6310. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6311. } else if(sps->poc_type == 1){//FIXME #define
  6312. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6313. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6314. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6315. sps->poc_cycle_length = get_ue_golomb(&s->gb);
  6316. if((unsigned)sps->poc_cycle_length >= FF_ARRAY_ELEMS(sps->offset_for_ref_frame)){
  6317. av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", sps->poc_cycle_length);
  6318. goto fail;
  6319. }
  6320. for(i=0; i<sps->poc_cycle_length; i++)
  6321. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6322. }else if(sps->poc_type != 2){
  6323. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6324. goto fail;
  6325. }
  6326. sps->ref_frame_count= get_ue_golomb_31(&s->gb);
  6327. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2 || sps->ref_frame_count > 16U){
  6328. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6329. goto fail;
  6330. }
  6331. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6332. sps->mb_width = get_ue_golomb(&s->gb) + 1;
  6333. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  6334. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  6335. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height)){
  6336. av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
  6337. goto fail;
  6338. }
  6339. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6340. if(!sps->frame_mbs_only_flag)
  6341. sps->mb_aff= get_bits1(&s->gb);
  6342. else
  6343. sps->mb_aff= 0;
  6344. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6345. #ifndef ALLOW_INTERLACE
  6346. if(sps->mb_aff)
  6347. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6348. #endif
  6349. sps->crop= get_bits1(&s->gb);
  6350. if(sps->crop){
  6351. sps->crop_left = get_ue_golomb(&s->gb);
  6352. sps->crop_right = get_ue_golomb(&s->gb);
  6353. sps->crop_top = get_ue_golomb(&s->gb);
  6354. sps->crop_bottom= get_ue_golomb(&s->gb);
  6355. if(sps->crop_left || sps->crop_top){
  6356. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6357. }
  6358. if(sps->crop_right >= 8 || sps->crop_bottom >= (8>> !sps->frame_mbs_only_flag)){
  6359. av_log(h->s.avctx, AV_LOG_ERROR, "brainfart cropping not supported, this could look slightly wrong ...\n");
  6360. }
  6361. }else{
  6362. sps->crop_left =
  6363. sps->crop_right =
  6364. sps->crop_top =
  6365. sps->crop_bottom= 0;
  6366. }
  6367. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6368. if( sps->vui_parameters_present_flag )
  6369. decode_vui_parameters(h, sps);
  6370. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6371. av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s %s\n",
  6372. sps_id, sps->profile_idc, sps->level_idc,
  6373. sps->poc_type,
  6374. sps->ref_frame_count,
  6375. sps->mb_width, sps->mb_height,
  6376. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6377. sps->direct_8x8_inference_flag ? "8B8" : "",
  6378. sps->crop_left, sps->crop_right,
  6379. sps->crop_top, sps->crop_bottom,
  6380. sps->vui_parameters_present_flag ? "VUI" : "",
  6381. ((const char*[]){"Gray","420","422","444"})[sps->chroma_format_idc]
  6382. );
  6383. }
  6384. av_free(h->sps_buffers[sps_id]);
  6385. h->sps_buffers[sps_id]= sps;
  6386. h->sps = *sps;
  6387. return 0;
  6388. fail:
  6389. av_free(sps);
  6390. return -1;
  6391. }
  6392. static void
  6393. build_qp_table(PPS *pps, int t, int index)
  6394. {
  6395. int i;
  6396. for(i = 0; i < 52; i++)
  6397. pps->chroma_qp_table[t][i] = chroma_qp[av_clip(i + index, 0, 51)];
  6398. }
  6399. int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length){
  6400. MpegEncContext * const s = &h->s;
  6401. unsigned int pps_id= get_ue_golomb(&s->gb);
  6402. PPS *pps;
  6403. if(pps_id >= MAX_PPS_COUNT) {
  6404. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id (%d) out of range\n", pps_id);
  6405. return -1;
  6406. }
  6407. pps= av_mallocz(sizeof(PPS));
  6408. if(pps == NULL)
  6409. return -1;
  6410. pps->sps_id= get_ue_golomb_31(&s->gb);
  6411. if((unsigned)pps->sps_id>=MAX_SPS_COUNT || h->sps_buffers[pps->sps_id] == NULL){
  6412. av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
  6413. goto fail;
  6414. }
  6415. pps->cabac= get_bits1(&s->gb);
  6416. pps->pic_order_present= get_bits1(&s->gb);
  6417. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6418. if(pps->slice_group_count > 1 ){
  6419. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6420. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6421. switch(pps->mb_slice_group_map_type){
  6422. case 0:
  6423. #if 0
  6424. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6425. | run_length[ i ] |1 |ue(v) |
  6426. #endif
  6427. break;
  6428. case 2:
  6429. #if 0
  6430. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6431. |{ | | |
  6432. | top_left_mb[ i ] |1 |ue(v) |
  6433. | bottom_right_mb[ i ] |1 |ue(v) |
  6434. | } | | |
  6435. #endif
  6436. break;
  6437. case 3:
  6438. case 4:
  6439. case 5:
  6440. #if 0
  6441. | slice_group_change_direction_flag |1 |u(1) |
  6442. | slice_group_change_rate_minus1 |1 |ue(v) |
  6443. #endif
  6444. break;
  6445. case 6:
  6446. #if 0
  6447. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6448. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6449. |) | | |
  6450. | slice_group_id[ i ] |1 |u(v) |
  6451. #endif
  6452. break;
  6453. }
  6454. }
  6455. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6456. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6457. if(pps->ref_count[0]-1 > 32-1 || pps->ref_count[1]-1 > 32-1){
  6458. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6459. goto fail;
  6460. }
  6461. pps->weighted_pred= get_bits1(&s->gb);
  6462. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6463. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6464. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6465. pps->chroma_qp_index_offset[0]= get_se_golomb(&s->gb);
  6466. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6467. pps->constrained_intra_pred= get_bits1(&s->gb);
  6468. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6469. pps->transform_8x8_mode= 0;
  6470. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  6471. memcpy(pps->scaling_matrix4, h->sps_buffers[pps->sps_id]->scaling_matrix4, sizeof(pps->scaling_matrix4));
  6472. memcpy(pps->scaling_matrix8, h->sps_buffers[pps->sps_id]->scaling_matrix8, sizeof(pps->scaling_matrix8));
  6473. if(get_bits_count(&s->gb) < bit_length){
  6474. pps->transform_8x8_mode= get_bits1(&s->gb);
  6475. decode_scaling_matrices(h, h->sps_buffers[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  6476. pps->chroma_qp_index_offset[1]= get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  6477. } else {
  6478. pps->chroma_qp_index_offset[1]= pps->chroma_qp_index_offset[0];
  6479. }
  6480. build_qp_table(pps, 0, pps->chroma_qp_index_offset[0]);
  6481. build_qp_table(pps, 1, pps->chroma_qp_index_offset[1]);
  6482. if(pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
  6483. h->pps.chroma_qp_diff= 1;
  6484. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6485. av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%u sps:%u %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d/%d %s %s %s %s\n",
  6486. pps_id, pps->sps_id,
  6487. pps->cabac ? "CABAC" : "CAVLC",
  6488. pps->slice_group_count,
  6489. pps->ref_count[0], pps->ref_count[1],
  6490. pps->weighted_pred ? "weighted" : "",
  6491. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
  6492. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  6493. pps->constrained_intra_pred ? "CONSTR" : "",
  6494. pps->redundant_pic_cnt_present ? "REDU" : "",
  6495. pps->transform_8x8_mode ? "8x8DCT" : ""
  6496. );
  6497. }
  6498. av_free(h->pps_buffers[pps_id]);
  6499. h->pps_buffers[pps_id]= pps;
  6500. return 0;
  6501. fail:
  6502. av_free(pps);
  6503. return -1;
  6504. }
  6505. /**
  6506. * Call decode_slice() for each context.
  6507. *
  6508. * @param h h264 master context
  6509. * @param context_count number of contexts to execute
  6510. */
  6511. static void execute_decode_slices(H264Context *h, int context_count){
  6512. MpegEncContext * const s = &h->s;
  6513. AVCodecContext * const avctx= s->avctx;
  6514. H264Context *hx;
  6515. int i;
  6516. if (s->avctx->hwaccel)
  6517. return;
  6518. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  6519. return;
  6520. if(context_count == 1) {
  6521. decode_slice(avctx, &h);
  6522. } else {
  6523. for(i = 1; i < context_count; i++) {
  6524. hx = h->thread_context[i];
  6525. hx->s.error_recognition = avctx->error_recognition;
  6526. hx->s.error_count = 0;
  6527. }
  6528. avctx->execute(avctx, (void *)decode_slice,
  6529. (void **)h->thread_context, NULL, context_count, sizeof(void*));
  6530. /* pull back stuff from slices to master context */
  6531. hx = h->thread_context[context_count - 1];
  6532. s->mb_x = hx->s.mb_x;
  6533. s->mb_y = hx->s.mb_y;
  6534. s->dropable = hx->s.dropable;
  6535. s->picture_structure = hx->s.picture_structure;
  6536. for(i = 1; i < context_count; i++)
  6537. h->s.error_count += h->thread_context[i]->s.error_count;
  6538. }
  6539. }
  6540. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  6541. MpegEncContext * const s = &h->s;
  6542. AVCodecContext * const avctx= s->avctx;
  6543. int buf_index=0;
  6544. H264Context *hx; ///< thread context
  6545. int context_count = 0;
  6546. h->max_contexts = avctx->thread_count;
  6547. #if 0
  6548. int i;
  6549. for(i=0; i<50; i++){
  6550. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  6551. }
  6552. #endif
  6553. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  6554. h->current_slice = 0;
  6555. if (!s->first_field)
  6556. s->current_picture_ptr= NULL;
  6557. reset_sei(h);
  6558. }
  6559. for(;;){
  6560. int consumed;
  6561. int dst_length;
  6562. int bit_length;
  6563. const uint8_t *ptr;
  6564. int i, nalsize = 0;
  6565. int err;
  6566. if(h->is_avc) {
  6567. if(buf_index >= buf_size) break;
  6568. nalsize = 0;
  6569. for(i = 0; i < h->nal_length_size; i++)
  6570. nalsize = (nalsize << 8) | buf[buf_index++];
  6571. if(nalsize <= 1 || (nalsize+buf_index > buf_size)){
  6572. if(nalsize == 1){
  6573. buf_index++;
  6574. continue;
  6575. }else{
  6576. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  6577. break;
  6578. }
  6579. }
  6580. } else {
  6581. // start code prefix search
  6582. for(; buf_index + 3 < buf_size; buf_index++){
  6583. // This should always succeed in the first iteration.
  6584. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  6585. break;
  6586. }
  6587. if (buf_index + 3 >= buf_size) {
  6588. buf_index = buf_size;
  6589. break;
  6590. }
  6591. buf_index+=3;
  6592. }
  6593. hx = h->thread_context[context_count];
  6594. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  6595. if (ptr==NULL || dst_length < 0){
  6596. return -1;
  6597. }
  6598. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  6599. dst_length--;
  6600. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  6601. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  6602. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", hx->nal_unit_type, buf_index, buf_size, dst_length);
  6603. }
  6604. if (h->is_avc && (nalsize != consumed)){
  6605. int i, debug_level = AV_LOG_DEBUG;
  6606. for (i = consumed; i < nalsize; i++)
  6607. if (buf[buf_index+i])
  6608. debug_level = AV_LOG_ERROR;
  6609. av_log(h->s.avctx, debug_level, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  6610. consumed= nalsize;
  6611. }
  6612. buf_index += consumed;
  6613. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME do not discard SEI id
  6614. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  6615. continue;
  6616. again:
  6617. err = 0;
  6618. switch(hx->nal_unit_type){
  6619. case NAL_IDR_SLICE:
  6620. if (h->nal_unit_type != NAL_IDR_SLICE) {
  6621. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  6622. return -1;
  6623. }
  6624. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  6625. case NAL_SLICE:
  6626. init_get_bits(&hx->s.gb, ptr, bit_length);
  6627. hx->intra_gb_ptr=
  6628. hx->inter_gb_ptr= &hx->s.gb;
  6629. hx->s.data_partitioning = 0;
  6630. if((err = decode_slice_header(hx, h)))
  6631. break;
  6632. if (s->avctx->hwaccel && h->current_slice == 1) {
  6633. if (s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  6634. return -1;
  6635. }
  6636. s->current_picture_ptr->key_frame |=
  6637. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  6638. (h->sei_recovery_frame_cnt >= 0);
  6639. if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
  6640. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  6641. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  6642. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  6643. && avctx->skip_frame < AVDISCARD_ALL){
  6644. if(avctx->hwaccel) {
  6645. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  6646. return -1;
  6647. }else
  6648. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  6649. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  6650. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  6651. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  6652. }else
  6653. context_count++;
  6654. }
  6655. break;
  6656. case NAL_DPA:
  6657. init_get_bits(&hx->s.gb, ptr, bit_length);
  6658. hx->intra_gb_ptr=
  6659. hx->inter_gb_ptr= NULL;
  6660. hx->s.data_partitioning = 1;
  6661. err = decode_slice_header(hx, h);
  6662. break;
  6663. case NAL_DPB:
  6664. init_get_bits(&hx->intra_gb, ptr, bit_length);
  6665. hx->intra_gb_ptr= &hx->intra_gb;
  6666. break;
  6667. case NAL_DPC:
  6668. init_get_bits(&hx->inter_gb, ptr, bit_length);
  6669. hx->inter_gb_ptr= &hx->inter_gb;
  6670. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  6671. && s->current_picture_ptr
  6672. && s->context_initialized
  6673. && s->hurry_up < 5
  6674. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  6675. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  6676. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  6677. && avctx->skip_frame < AVDISCARD_ALL)
  6678. context_count++;
  6679. break;
  6680. case NAL_SEI:
  6681. init_get_bits(&s->gb, ptr, bit_length);
  6682. ff_h264_decode_sei(h);
  6683. break;
  6684. case NAL_SPS:
  6685. init_get_bits(&s->gb, ptr, bit_length);
  6686. ff_h264_decode_seq_parameter_set(h);
  6687. if(s->flags& CODEC_FLAG_LOW_DELAY)
  6688. s->low_delay=1;
  6689. if(avctx->has_b_frames < 2)
  6690. avctx->has_b_frames= !s->low_delay;
  6691. break;
  6692. case NAL_PPS:
  6693. init_get_bits(&s->gb, ptr, bit_length);
  6694. ff_h264_decode_picture_parameter_set(h, bit_length);
  6695. break;
  6696. case NAL_AUD:
  6697. case NAL_END_SEQUENCE:
  6698. case NAL_END_STREAM:
  6699. case NAL_FILLER_DATA:
  6700. case NAL_SPS_EXT:
  6701. case NAL_AUXILIARY_SLICE:
  6702. break;
  6703. default:
  6704. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", h->nal_unit_type, bit_length);
  6705. }
  6706. if(context_count == h->max_contexts) {
  6707. execute_decode_slices(h, context_count);
  6708. context_count = 0;
  6709. }
  6710. if (err < 0)
  6711. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6712. else if(err == 1) {
  6713. /* Slice could not be decoded in parallel mode, copy down
  6714. * NAL unit stuff to context 0 and restart. Note that
  6715. * rbsp_buffer is not transferred, but since we no longer
  6716. * run in parallel mode this should not be an issue. */
  6717. h->nal_unit_type = hx->nal_unit_type;
  6718. h->nal_ref_idc = hx->nal_ref_idc;
  6719. hx = h;
  6720. goto again;
  6721. }
  6722. }
  6723. if(context_count)
  6724. execute_decode_slices(h, context_count);
  6725. return buf_index;
  6726. }
  6727. /**
  6728. * returns the number of bytes consumed for building the current frame
  6729. */
  6730. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  6731. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  6732. if(pos+10>buf_size) pos=buf_size; // oops ;)
  6733. return pos;
  6734. }
  6735. static int decode_frame(AVCodecContext *avctx,
  6736. void *data, int *data_size,
  6737. const uint8_t *buf, int buf_size)
  6738. {
  6739. H264Context *h = avctx->priv_data;
  6740. MpegEncContext *s = &h->s;
  6741. AVFrame *pict = data;
  6742. int buf_index;
  6743. s->flags= avctx->flags;
  6744. s->flags2= avctx->flags2;
  6745. /* end of stream, output what is still in the buffers */
  6746. if (buf_size == 0) {
  6747. Picture *out;
  6748. int i, out_idx;
  6749. //FIXME factorize this with the output code below
  6750. out = h->delayed_pic[0];
  6751. out_idx = 0;
  6752. for(i=1; h->delayed_pic[i] && (h->delayed_pic[i]->poc && !h->delayed_pic[i]->key_frame); i++)
  6753. if(h->delayed_pic[i]->poc < out->poc){
  6754. out = h->delayed_pic[i];
  6755. out_idx = i;
  6756. }
  6757. for(i=out_idx; h->delayed_pic[i]; i++)
  6758. h->delayed_pic[i] = h->delayed_pic[i+1];
  6759. if(out){
  6760. *data_size = sizeof(AVFrame);
  6761. *pict= *(AVFrame*)out;
  6762. }
  6763. return 0;
  6764. }
  6765. if(h->is_avc && !h->got_avcC) {
  6766. int i, cnt, nalsize;
  6767. unsigned char *p = avctx->extradata;
  6768. if(avctx->extradata_size < 7) {
  6769. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  6770. return -1;
  6771. }
  6772. if(*p != 1) {
  6773. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  6774. return -1;
  6775. }
  6776. /* sps and pps in the avcC always have length coded with 2 bytes,
  6777. so put a fake nal_length_size = 2 while parsing them */
  6778. h->nal_length_size = 2;
  6779. // Decode sps from avcC
  6780. cnt = *(p+5) & 0x1f; // Number of sps
  6781. p += 6;
  6782. for (i = 0; i < cnt; i++) {
  6783. nalsize = AV_RB16(p) + 2;
  6784. if(decode_nal_units(h, p, nalsize) < 0) {
  6785. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  6786. return -1;
  6787. }
  6788. p += nalsize;
  6789. }
  6790. // Decode pps from avcC
  6791. cnt = *(p++); // Number of pps
  6792. for (i = 0; i < cnt; i++) {
  6793. nalsize = AV_RB16(p) + 2;
  6794. if(decode_nal_units(h, p, nalsize) != nalsize) {
  6795. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  6796. return -1;
  6797. }
  6798. p += nalsize;
  6799. }
  6800. // Now store right nal length size, that will be use to parse all other nals
  6801. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  6802. // Do not reparse avcC
  6803. h->got_avcC = 1;
  6804. }
  6805. if(!h->got_avcC && !h->is_avc && s->avctx->extradata_size){
  6806. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  6807. return -1;
  6808. h->got_avcC = 1;
  6809. }
  6810. buf_index=decode_nal_units(h, buf, buf_size);
  6811. if(buf_index < 0)
  6812. return -1;
  6813. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  6814. if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
  6815. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  6816. return -1;
  6817. }
  6818. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  6819. Picture *out = s->current_picture_ptr;
  6820. Picture *cur = s->current_picture_ptr;
  6821. int i, pics, cross_idr, out_of_order, out_idx;
  6822. s->mb_y= 0;
  6823. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  6824. s->current_picture_ptr->pict_type= s->pict_type;
  6825. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  6826. ff_vdpau_h264_set_reference_frames(s);
  6827. if(!s->dropable) {
  6828. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  6829. h->prev_poc_msb= h->poc_msb;
  6830. h->prev_poc_lsb= h->poc_lsb;
  6831. }
  6832. h->prev_frame_num_offset= h->frame_num_offset;
  6833. h->prev_frame_num= h->frame_num;
  6834. if (avctx->hwaccel) {
  6835. if (avctx->hwaccel->end_frame(avctx) < 0)
  6836. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  6837. }
  6838. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  6839. ff_vdpau_h264_picture_complete(s);
  6840. /*
  6841. * FIXME: Error handling code does not seem to support interlaced
  6842. * when slices span multiple rows
  6843. * The ff_er_add_slice calls don't work right for bottom
  6844. * fields; they cause massive erroneous error concealing
  6845. * Error marking covers both fields (top and bottom).
  6846. * This causes a mismatched s->error_count
  6847. * and a bad error table. Further, the error count goes to
  6848. * INT_MAX when called for bottom field, because mb_y is
  6849. * past end by one (callers fault) and resync_mb_y != 0
  6850. * causes problems for the first MB line, too.
  6851. */
  6852. if (!FIELD_PICTURE)
  6853. ff_er_frame_end(s);
  6854. MPV_frame_end(s);
  6855. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  6856. /* Wait for second field. */
  6857. *data_size = 0;
  6858. } else {
  6859. cur->repeat_pict = 0;
  6860. /* Signal interlacing information externally. */
  6861. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  6862. if(h->sps.pic_struct_present_flag){
  6863. switch (h->sei_pic_struct)
  6864. {
  6865. case SEI_PIC_STRUCT_FRAME:
  6866. cur->interlaced_frame = 0;
  6867. break;
  6868. case SEI_PIC_STRUCT_TOP_FIELD:
  6869. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  6870. case SEI_PIC_STRUCT_TOP_BOTTOM:
  6871. case SEI_PIC_STRUCT_BOTTOM_TOP:
  6872. cur->interlaced_frame = 1;
  6873. break;
  6874. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  6875. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  6876. // Signal the possibility of telecined film externally (pic_struct 5,6)
  6877. // From these hints, let the applications decide if they apply deinterlacing.
  6878. cur->repeat_pict = 1;
  6879. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  6880. break;
  6881. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  6882. // Force progressive here, as doubling interlaced frame is a bad idea.
  6883. cur->interlaced_frame = 0;
  6884. cur->repeat_pict = 2;
  6885. break;
  6886. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  6887. cur->interlaced_frame = 0;
  6888. cur->repeat_pict = 4;
  6889. break;
  6890. }
  6891. }else{
  6892. /* Derive interlacing flag from used decoding process. */
  6893. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  6894. }
  6895. if (cur->field_poc[0] != cur->field_poc[1]){
  6896. /* Derive top_field_first from field pocs. */
  6897. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  6898. }else{
  6899. if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
  6900. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  6901. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  6902. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  6903. cur->top_field_first = 1;
  6904. else
  6905. cur->top_field_first = 0;
  6906. }else{
  6907. /* Most likely progressive */
  6908. cur->top_field_first = 0;
  6909. }
  6910. }
  6911. //FIXME do something with unavailable reference frames
  6912. /* Sort B-frames into display order */
  6913. if(h->sps.bitstream_restriction_flag
  6914. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  6915. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  6916. s->low_delay = 0;
  6917. }
  6918. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  6919. && !h->sps.bitstream_restriction_flag){
  6920. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  6921. s->low_delay= 0;
  6922. }
  6923. pics = 0;
  6924. while(h->delayed_pic[pics]) pics++;
  6925. assert(pics <= MAX_DELAYED_PIC_COUNT);
  6926. h->delayed_pic[pics++] = cur;
  6927. if(cur->reference == 0)
  6928. cur->reference = DELAYED_PIC_REF;
  6929. out = h->delayed_pic[0];
  6930. out_idx = 0;
  6931. for(i=1; h->delayed_pic[i] && (h->delayed_pic[i]->poc && !h->delayed_pic[i]->key_frame); i++)
  6932. if(h->delayed_pic[i]->poc < out->poc){
  6933. out = h->delayed_pic[i];
  6934. out_idx = i;
  6935. }
  6936. cross_idr = !h->delayed_pic[0]->poc || !!h->delayed_pic[i] || h->delayed_pic[0]->key_frame;
  6937. out_of_order = !cross_idr && out->poc < h->outputed_poc;
  6938. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  6939. { }
  6940. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  6941. || (s->low_delay &&
  6942. ((!cross_idr && out->poc > h->outputed_poc + 2)
  6943. || cur->pict_type == FF_B_TYPE)))
  6944. {
  6945. s->low_delay = 0;
  6946. s->avctx->has_b_frames++;
  6947. }
  6948. if(out_of_order || pics > s->avctx->has_b_frames){
  6949. out->reference &= ~DELAYED_PIC_REF;
  6950. for(i=out_idx; h->delayed_pic[i]; i++)
  6951. h->delayed_pic[i] = h->delayed_pic[i+1];
  6952. }
  6953. if(!out_of_order && pics > s->avctx->has_b_frames){
  6954. *data_size = sizeof(AVFrame);
  6955. h->outputed_poc = out->poc;
  6956. *pict= *(AVFrame*)out;
  6957. }else{
  6958. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  6959. }
  6960. }
  6961. }
  6962. assert(pict->data[0] || !*data_size);
  6963. ff_print_debug_info(s, pict);
  6964. //printf("out %d\n", (int)pict->data[0]);
  6965. #if 0 //?
  6966. /* Return the Picture timestamp as the frame number */
  6967. /* we subtract 1 because it is added on utils.c */
  6968. avctx->frame_number = s->picture_number - 1;
  6969. #endif
  6970. return get_consumed_bytes(s, buf_index, buf_size);
  6971. }
  6972. #if 0
  6973. static inline void fill_mb_avail(H264Context *h){
  6974. MpegEncContext * const s = &h->s;
  6975. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  6976. if(s->mb_y){
  6977. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  6978. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  6979. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  6980. }else{
  6981. h->mb_avail[0]=
  6982. h->mb_avail[1]=
  6983. h->mb_avail[2]= 0;
  6984. }
  6985. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  6986. h->mb_avail[4]= 1; //FIXME move out
  6987. h->mb_avail[5]= 0; //FIXME move out
  6988. }
  6989. #endif
  6990. #ifdef TEST
  6991. #undef printf
  6992. #undef random
  6993. #define COUNT 8000
  6994. #define SIZE (COUNT*40)
  6995. int main(void){
  6996. int i;
  6997. uint8_t temp[SIZE];
  6998. PutBitContext pb;
  6999. GetBitContext gb;
  7000. // int int_temp[10000];
  7001. DSPContext dsp;
  7002. AVCodecContext avctx;
  7003. dsputil_init(&dsp, &avctx);
  7004. init_put_bits(&pb, temp, SIZE);
  7005. printf("testing unsigned exp golomb\n");
  7006. for(i=0; i<COUNT; i++){
  7007. START_TIMER
  7008. set_ue_golomb(&pb, i);
  7009. STOP_TIMER("set_ue_golomb");
  7010. }
  7011. flush_put_bits(&pb);
  7012. init_get_bits(&gb, temp, 8*SIZE);
  7013. for(i=0; i<COUNT; i++){
  7014. int j, s;
  7015. s= show_bits(&gb, 24);
  7016. START_TIMER
  7017. j= get_ue_golomb(&gb);
  7018. if(j != i){
  7019. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7020. // return -1;
  7021. }
  7022. STOP_TIMER("get_ue_golomb");
  7023. }
  7024. init_put_bits(&pb, temp, SIZE);
  7025. printf("testing signed exp golomb\n");
  7026. for(i=0; i<COUNT; i++){
  7027. START_TIMER
  7028. set_se_golomb(&pb, i - COUNT/2);
  7029. STOP_TIMER("set_se_golomb");
  7030. }
  7031. flush_put_bits(&pb);
  7032. init_get_bits(&gb, temp, 8*SIZE);
  7033. for(i=0; i<COUNT; i++){
  7034. int j, s;
  7035. s= show_bits(&gb, 24);
  7036. START_TIMER
  7037. j= get_se_golomb(&gb);
  7038. if(j != i - COUNT/2){
  7039. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7040. // return -1;
  7041. }
  7042. STOP_TIMER("get_se_golomb");
  7043. }
  7044. #if 0
  7045. printf("testing 4x4 (I)DCT\n");
  7046. DCTELEM block[16];
  7047. uint8_t src[16], ref[16];
  7048. uint64_t error= 0, max_error=0;
  7049. for(i=0; i<COUNT; i++){
  7050. int j;
  7051. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7052. for(j=0; j<16; j++){
  7053. ref[j]= random()%255;
  7054. src[j]= random()%255;
  7055. }
  7056. h264_diff_dct_c(block, src, ref, 4);
  7057. //normalize
  7058. for(j=0; j<16; j++){
  7059. // printf("%d ", block[j]);
  7060. block[j]= block[j]*4;
  7061. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7062. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7063. }
  7064. // printf("\n");
  7065. s->dsp.h264_idct_add(ref, block, 4);
  7066. /* for(j=0; j<16; j++){
  7067. printf("%d ", ref[j]);
  7068. }
  7069. printf("\n");*/
  7070. for(j=0; j<16; j++){
  7071. int diff= FFABS(src[j] - ref[j]);
  7072. error+= diff*diff;
  7073. max_error= FFMAX(max_error, diff);
  7074. }
  7075. }
  7076. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7077. printf("testing quantizer\n");
  7078. for(qp=0; qp<52; qp++){
  7079. for(i=0; i<16; i++)
  7080. src1_block[i]= src2_block[i]= random()%255;
  7081. }
  7082. printf("Testing NAL layer\n");
  7083. uint8_t bitstream[COUNT];
  7084. uint8_t nal[COUNT*2];
  7085. H264Context h;
  7086. memset(&h, 0, sizeof(H264Context));
  7087. for(i=0; i<COUNT; i++){
  7088. int zeros= i;
  7089. int nal_length;
  7090. int consumed;
  7091. int out_length;
  7092. uint8_t *out;
  7093. int j;
  7094. for(j=0; j<COUNT; j++){
  7095. bitstream[j]= (random() % 255) + 1;
  7096. }
  7097. for(j=0; j<zeros; j++){
  7098. int pos= random() % COUNT;
  7099. while(bitstream[pos] == 0){
  7100. pos++;
  7101. pos %= COUNT;
  7102. }
  7103. bitstream[pos]=0;
  7104. }
  7105. START_TIMER
  7106. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7107. if(nal_length<0){
  7108. printf("encoding failed\n");
  7109. return -1;
  7110. }
  7111. out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7112. STOP_TIMER("NAL")
  7113. if(out_length != COUNT){
  7114. printf("incorrect length %d %d\n", out_length, COUNT);
  7115. return -1;
  7116. }
  7117. if(consumed != nal_length){
  7118. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7119. return -1;
  7120. }
  7121. if(memcmp(bitstream, out, COUNT)){
  7122. printf("mismatch\n");
  7123. return -1;
  7124. }
  7125. }
  7126. #endif
  7127. printf("Testing RBSP\n");
  7128. return 0;
  7129. }
  7130. #endif /* TEST */
  7131. static av_cold int decode_end(AVCodecContext *avctx)
  7132. {
  7133. H264Context *h = avctx->priv_data;
  7134. MpegEncContext *s = &h->s;
  7135. int i;
  7136. av_freep(&h->rbsp_buffer[0]);
  7137. av_freep(&h->rbsp_buffer[1]);
  7138. free_tables(h); //FIXME cleanup init stuff perhaps
  7139. for(i = 0; i < MAX_SPS_COUNT; i++)
  7140. av_freep(h->sps_buffers + i);
  7141. for(i = 0; i < MAX_PPS_COUNT; i++)
  7142. av_freep(h->pps_buffers + i);
  7143. MPV_common_end(s);
  7144. // memset(h, 0, sizeof(H264Context));
  7145. return 0;
  7146. }
  7147. AVCodec h264_decoder = {
  7148. "h264",
  7149. CODEC_TYPE_VIDEO,
  7150. CODEC_ID_H264,
  7151. sizeof(H264Context),
  7152. decode_init,
  7153. NULL,
  7154. decode_end,
  7155. decode_frame,
  7156. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  7157. .flush= flush_dpb,
  7158. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  7159. .pix_fmts= ff_hwaccel_pixfmt_list_420,
  7160. };
  7161. #if CONFIG_H264_VDPAU_DECODER
  7162. AVCodec h264_vdpau_decoder = {
  7163. "h264_vdpau",
  7164. CODEC_TYPE_VIDEO,
  7165. CODEC_ID_H264,
  7166. sizeof(H264Context),
  7167. decode_init,
  7168. NULL,
  7169. decode_end,
  7170. decode_frame,
  7171. CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  7172. .flush= flush_dpb,
  7173. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  7174. };
  7175. #endif
  7176. #if CONFIG_SVQ3_DECODER
  7177. #include "svq3.c"
  7178. #endif