snow.c 180 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839
  1. /*
  2. * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avcodec.h"
  21. #include "dsputil.h"
  22. #include "snow.h"
  23. #include "rangecoder.h"
  24. #include "mathops.h"
  25. #include "mpegvideo.h"
  26. #undef NDEBUG
  27. #include <assert.h>
  28. static const int8_t quant3[256]={
  29. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  30. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  31. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  32. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  33. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  34. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  35. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  36. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  37. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  38. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  39. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  40. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  41. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  42. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  43. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  44. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,
  45. };
  46. static const int8_t quant3b[256]={
  47. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  48. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  49. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  50. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  51. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  52. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  53. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  54. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  55. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  56. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  57. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  58. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  59. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  60. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  61. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  62. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  63. };
  64. static const int8_t quant3bA[256]={
  65. 0, 0, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  66. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  67. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  68. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  69. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  70. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  71. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  72. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  73. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  74. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  75. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  76. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  77. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  78. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  79. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  80. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  81. };
  82. static const int8_t quant5[256]={
  83. 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  84. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  85. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  86. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  87. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  88. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  89. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  90. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  91. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  92. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  93. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  94. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  95. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  96. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  97. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  98. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
  99. };
  100. static const int8_t quant7[256]={
  101. 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  102. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  103. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  104. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  105. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  106. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  107. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  108. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  109. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  110. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  111. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  112. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  113. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  114. -3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,
  115. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  116. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
  117. };
  118. static const int8_t quant9[256]={
  119. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  120. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  121. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  122. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  123. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  124. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  125. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  126. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  127. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  128. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  129. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  130. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  131. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  132. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  133. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
  134. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  135. };
  136. static const int8_t quant11[256]={
  137. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  138. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  139. 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  140. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  141. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  142. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  143. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  144. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  145. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  146. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  147. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  148. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  149. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  150. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
  151. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  152. -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
  153. };
  154. static const int8_t quant13[256]={
  155. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  156. 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  157. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  158. 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  159. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  160. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  161. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  162. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  163. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  164. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  165. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  166. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  167. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  168. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  169. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  170. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  171. };
  172. #if 0 //64*cubic
  173. static const uint8_t obmc32[1024]={
  174. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  175. 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
  176. 0, 0, 0, 4, 4, 4, 4, 8, 8, 12, 12, 12, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12, 8, 8, 4, 4, 4, 4, 0, 0, 0,
  177. 0, 0, 4, 4, 8, 8, 12, 16, 16, 20, 24, 24, 28, 28, 32, 32, 32, 32, 28, 28, 24, 24, 20, 16, 16, 12, 8, 8, 4, 4, 0, 0,
  178. 0, 0, 4, 8, 8, 12, 16, 24, 28, 32, 36, 40, 44, 48, 48, 48, 48, 48, 48, 44, 40, 36, 32, 28, 24, 16, 12, 8, 8, 4, 0, 0,
  179. 0, 4, 4, 8, 12, 20, 24, 32, 40, 44, 52, 56, 60, 64, 68, 72, 72, 68, 64, 60, 56, 52, 44, 40, 32, 24, 20, 12, 8, 4, 4, 0,
  180. 0, 4, 4, 12, 16, 24, 32, 40, 52, 60, 68, 76, 80, 88, 88, 92, 92, 88, 88, 80, 76, 68, 60, 52, 40, 32, 24, 16, 12, 4, 4, 0,
  181. 0, 4, 8, 16, 24, 32, 40, 52, 64, 76, 84, 92,100,108,112,116,116,112,108,100, 92, 84, 76, 64, 52, 40, 32, 24, 16, 8, 4, 0,
  182. 0, 4, 8, 16, 28, 40, 52, 64, 76, 88,100,112,124,132,136,140,140,136,132,124,112,100, 88, 76, 64, 52, 40, 28, 16, 8, 4, 0,
  183. 0, 4, 12, 20, 32, 44, 60, 76, 88,104,120,132,144,152,160,164,164,160,152,144,132,120,104, 88, 76, 60, 44, 32, 20, 12, 4, 0,
  184. 0, 4, 12, 24, 36, 48, 68, 84,100,120,136,152,164,176,180,184,184,180,176,164,152,136,120,100, 84, 68, 48, 36, 24, 12, 4, 0,
  185. 0, 4, 12, 24, 40, 56, 76, 92,112,132,152,168,180,192,204,208,208,204,192,180,168,152,132,112, 92, 76, 56, 40, 24, 12, 4, 0,
  186. 0, 4, 16, 28, 44, 60, 80,100,124,144,164,180,196,208,220,224,224,220,208,196,180,164,144,124,100, 80, 60, 44, 28, 16, 4, 0,
  187. 0, 8, 16, 28, 48, 64, 88,108,132,152,176,192,208,224,232,240,240,232,224,208,192,176,152,132,108, 88, 64, 48, 28, 16, 8, 0,
  188. 0, 4, 16, 32, 48, 68, 88,112,136,160,180,204,220,232,244,248,248,244,232,220,204,180,160,136,112, 88, 68, 48, 32, 16, 4, 0,
  189. 1, 8, 16, 32, 48, 72, 92,116,140,164,184,208,224,240,248,255,255,248,240,224,208,184,164,140,116, 92, 72, 48, 32, 16, 8, 1,
  190. 1, 8, 16, 32, 48, 72, 92,116,140,164,184,208,224,240,248,255,255,248,240,224,208,184,164,140,116, 92, 72, 48, 32, 16, 8, 1,
  191. 0, 4, 16, 32, 48, 68, 88,112,136,160,180,204,220,232,244,248,248,244,232,220,204,180,160,136,112, 88, 68, 48, 32, 16, 4, 0,
  192. 0, 8, 16, 28, 48, 64, 88,108,132,152,176,192,208,224,232,240,240,232,224,208,192,176,152,132,108, 88, 64, 48, 28, 16, 8, 0,
  193. 0, 4, 16, 28, 44, 60, 80,100,124,144,164,180,196,208,220,224,224,220,208,196,180,164,144,124,100, 80, 60, 44, 28, 16, 4, 0,
  194. 0, 4, 12, 24, 40, 56, 76, 92,112,132,152,168,180,192,204,208,208,204,192,180,168,152,132,112, 92, 76, 56, 40, 24, 12, 4, 0,
  195. 0, 4, 12, 24, 36, 48, 68, 84,100,120,136,152,164,176,180,184,184,180,176,164,152,136,120,100, 84, 68, 48, 36, 24, 12, 4, 0,
  196. 0, 4, 12, 20, 32, 44, 60, 76, 88,104,120,132,144,152,160,164,164,160,152,144,132,120,104, 88, 76, 60, 44, 32, 20, 12, 4, 0,
  197. 0, 4, 8, 16, 28, 40, 52, 64, 76, 88,100,112,124,132,136,140,140,136,132,124,112,100, 88, 76, 64, 52, 40, 28, 16, 8, 4, 0,
  198. 0, 4, 8, 16, 24, 32, 40, 52, 64, 76, 84, 92,100,108,112,116,116,112,108,100, 92, 84, 76, 64, 52, 40, 32, 24, 16, 8, 4, 0,
  199. 0, 4, 4, 12, 16, 24, 32, 40, 52, 60, 68, 76, 80, 88, 88, 92, 92, 88, 88, 80, 76, 68, 60, 52, 40, 32, 24, 16, 12, 4, 4, 0,
  200. 0, 4, 4, 8, 12, 20, 24, 32, 40, 44, 52, 56, 60, 64, 68, 72, 72, 68, 64, 60, 56, 52, 44, 40, 32, 24, 20, 12, 8, 4, 4, 0,
  201. 0, 0, 4, 8, 8, 12, 16, 24, 28, 32, 36, 40, 44, 48, 48, 48, 48, 48, 48, 44, 40, 36, 32, 28, 24, 16, 12, 8, 8, 4, 0, 0,
  202. 0, 0, 4, 4, 8, 8, 12, 16, 16, 20, 24, 24, 28, 28, 32, 32, 32, 32, 28, 28, 24, 24, 20, 16, 16, 12, 8, 8, 4, 4, 0, 0,
  203. 0, 0, 0, 4, 4, 4, 4, 8, 8, 12, 12, 12, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12, 8, 8, 4, 4, 4, 4, 0, 0, 0,
  204. 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
  205. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  206. //error:0.000022
  207. };
  208. static const uint8_t obmc16[256]={
  209. 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0,
  210. 0, 4, 4, 8, 16, 20, 20, 24, 24, 20, 20, 16, 8, 4, 4, 0,
  211. 0, 4, 16, 24, 36, 44, 52, 60, 60, 52, 44, 36, 24, 16, 4, 0,
  212. 0, 8, 24, 44, 60, 80, 96,104,104, 96, 80, 60, 44, 24, 8, 0,
  213. 0, 16, 36, 60, 92,116,136,152,152,136,116, 92, 60, 36, 16, 0,
  214. 0, 20, 44, 80,116,152,180,196,196,180,152,116, 80, 44, 20, 0,
  215. 4, 20, 52, 96,136,180,212,228,228,212,180,136, 96, 52, 20, 4,
  216. 4, 24, 60,104,152,196,228,248,248,228,196,152,104, 60, 24, 4,
  217. 4, 24, 60,104,152,196,228,248,248,228,196,152,104, 60, 24, 4,
  218. 4, 20, 52, 96,136,180,212,228,228,212,180,136, 96, 52, 20, 4,
  219. 0, 20, 44, 80,116,152,180,196,196,180,152,116, 80, 44, 20, 0,
  220. 0, 16, 36, 60, 92,116,136,152,152,136,116, 92, 60, 36, 16, 0,
  221. 0, 8, 24, 44, 60, 80, 96,104,104, 96, 80, 60, 44, 24, 8, 0,
  222. 0, 4, 16, 24, 36, 44, 52, 60, 60, 52, 44, 36, 24, 16, 4, 0,
  223. 0, 4, 4, 8, 16, 20, 20, 24, 24, 20, 20, 16, 8, 4, 4, 0,
  224. 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0,
  225. //error:0.000033
  226. };
  227. #elif 1 // 64*linear
  228. static const uint8_t obmc32[1024]={
  229. 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  230. 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  231. 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  232. 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  233. 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  234. 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  235. 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  236. 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  237. 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  238. 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  239. 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  240. 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  241. 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  242. 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  243. 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  244. 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  245. 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  246. 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  247. 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  248. 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  249. 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  250. 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  251. 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  252. 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  253. 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  254. 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  255. 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  256. 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  257. 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  258. 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  259. 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  260. 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  261. //error:0.000020
  262. };
  263. static const uint8_t obmc16[256]={
  264. 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  265. 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  266. 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  267. 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  268. 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  269. 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  270. 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  271. 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  272. 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  273. 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  274. 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  275. 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  276. 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  277. 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  278. 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  279. 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  280. //error:0.000015
  281. };
  282. #else //64*cos
  283. static const uint8_t obmc32[1024]={
  284. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  285. 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0,
  286. 0, 0, 0, 4, 4, 4, 4, 8, 8, 12, 12, 12, 12, 16, 16, 16, 16, 16, 16, 12, 12, 12, 12, 8, 8, 4, 4, 4, 4, 0, 0, 0,
  287. 0, 0, 4, 4, 4, 8, 8, 12, 16, 20, 20, 24, 28, 28, 28, 28, 28, 28, 28, 28, 24, 20, 20, 16, 12, 8, 8, 4, 4, 4, 0, 0,
  288. 0, 0, 4, 4, 8, 12, 16, 20, 24, 28, 36, 40, 44, 44, 48, 48, 48, 48, 44, 44, 40, 36, 28, 24, 20, 16, 12, 8, 4, 4, 0, 0,
  289. 0, 0, 4, 8, 12, 20, 24, 32, 36, 44, 48, 56, 60, 64, 68, 68, 68, 68, 64, 60, 56, 48, 44, 36, 32, 24, 20, 12, 8, 4, 0, 0,
  290. 0, 4, 4, 8, 16, 24, 32, 40, 48, 60, 68, 76, 80, 84, 88, 92, 92, 88, 84, 80, 76, 68, 60, 48, 40, 32, 24, 16, 8, 4, 4, 0,
  291. 0, 4, 8, 12, 20, 32, 40, 52, 64, 76, 84, 96,104,108,112,116,116,112,108,104, 96, 84, 76, 64, 52, 40, 32, 20, 12, 8, 4, 0,
  292. 0, 4, 8, 16, 24, 36, 48, 64, 76, 92,104,116,124,132,136,140,140,136,132,124,116,104, 92, 76, 64, 48, 36, 24, 16, 8, 4, 0,
  293. 0, 4, 12, 20, 28, 44, 60, 76, 92,104,120,136,148,156,160,164,164,160,156,148,136,120,104, 92, 76, 60, 44, 28, 20, 12, 4, 0,
  294. 0, 4, 12, 20, 36, 48, 68, 84,104,120,140,152,168,176,184,188,188,184,176,168,152,140,120,104, 84, 68, 48, 36, 20, 12, 4, 0,
  295. 0, 4, 12, 24, 36, 56, 76, 96,116,136,152,172,184,196,204,208,208,204,196,184,172,152,136,116, 96, 76, 56, 36, 24, 12, 4, 0,
  296. 0, 4, 12, 24, 44, 60, 80,104,124,148,168,184,200,212,224,228,228,224,212,200,184,168,148,124,104, 80, 60, 44, 24, 12, 4, 0,
  297. 0, 4, 12, 28, 44, 64, 84,108,132,156,176,196,212,228,236,240,240,236,228,212,196,176,156,132,108, 84, 64, 44, 28, 12, 4, 0,
  298. 0, 4, 16, 28, 48, 68, 88,112,136,160,184,204,224,236,244,252,252,244,236,224,204,184,160,136,112, 88, 68, 48, 28, 16, 4, 0,
  299. 1, 4, 16, 28, 48, 68, 92,116,140,164,188,208,228,240,252,255,255,252,240,228,208,188,164,140,116, 92, 68, 48, 28, 16, 4, 1,
  300. 1, 4, 16, 28, 48, 68, 92,116,140,164,188,208,228,240,252,255,255,252,240,228,208,188,164,140,116, 92, 68, 48, 28, 16, 4, 1,
  301. 0, 4, 16, 28, 48, 68, 88,112,136,160,184,204,224,236,244,252,252,244,236,224,204,184,160,136,112, 88, 68, 48, 28, 16, 4, 0,
  302. 0, 4, 12, 28, 44, 64, 84,108,132,156,176,196,212,228,236,240,240,236,228,212,196,176,156,132,108, 84, 64, 44, 28, 12, 4, 0,
  303. 0, 4, 12, 24, 44, 60, 80,104,124,148,168,184,200,212,224,228,228,224,212,200,184,168,148,124,104, 80, 60, 44, 24, 12, 4, 0,
  304. 0, 4, 12, 24, 36, 56, 76, 96,116,136,152,172,184,196,204,208,208,204,196,184,172,152,136,116, 96, 76, 56, 36, 24, 12, 4, 0,
  305. 0, 4, 12, 20, 36, 48, 68, 84,104,120,140,152,168,176,184,188,188,184,176,168,152,140,120,104, 84, 68, 48, 36, 20, 12, 4, 0,
  306. 0, 4, 12, 20, 28, 44, 60, 76, 92,104,120,136,148,156,160,164,164,160,156,148,136,120,104, 92, 76, 60, 44, 28, 20, 12, 4, 0,
  307. 0, 4, 8, 16, 24, 36, 48, 64, 76, 92,104,116,124,132,136,140,140,136,132,124,116,104, 92, 76, 64, 48, 36, 24, 16, 8, 4, 0,
  308. 0, 4, 8, 12, 20, 32, 40, 52, 64, 76, 84, 96,104,108,112,116,116,112,108,104, 96, 84, 76, 64, 52, 40, 32, 20, 12, 8, 4, 0,
  309. 0, 4, 4, 8, 16, 24, 32, 40, 48, 60, 68, 76, 80, 84, 88, 92, 92, 88, 84, 80, 76, 68, 60, 48, 40, 32, 24, 16, 8, 4, 4, 0,
  310. 0, 0, 4, 8, 12, 20, 24, 32, 36, 44, 48, 56, 60, 64, 68, 68, 68, 68, 64, 60, 56, 48, 44, 36, 32, 24, 20, 12, 8, 4, 0, 0,
  311. 0, 0, 4, 4, 8, 12, 16, 20, 24, 28, 36, 40, 44, 44, 48, 48, 48, 48, 44, 44, 40, 36, 28, 24, 20, 16, 12, 8, 4, 4, 0, 0,
  312. 0, 0, 4, 4, 4, 8, 8, 12, 16, 20, 20, 24, 28, 28, 28, 28, 28, 28, 28, 28, 24, 20, 20, 16, 12, 8, 8, 4, 4, 4, 0, 0,
  313. 0, 0, 0, 4, 4, 4, 4, 8, 8, 12, 12, 12, 12, 16, 16, 16, 16, 16, 16, 12, 12, 12, 12, 8, 8, 4, 4, 4, 4, 0, 0, 0,
  314. 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0,
  315. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  316. //error:0.000022
  317. };
  318. static const uint8_t obmc16[256]={
  319. 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
  320. 0, 0, 4, 8, 12, 16, 20, 20, 20, 20, 16, 12, 8, 4, 0, 0,
  321. 0, 4, 12, 24, 32, 44, 52, 56, 56, 52, 44, 32, 24, 12, 4, 0,
  322. 0, 8, 24, 40, 60, 80, 96,104,104, 96, 80, 60, 40, 24, 8, 0,
  323. 0, 12, 32, 64, 92,120,140,152,152,140,120, 92, 64, 32, 12, 0,
  324. 4, 16, 44, 80,120,156,184,196,196,184,156,120, 80, 44, 16, 4,
  325. 4, 20, 52, 96,140,184,216,232,232,216,184,140, 96, 52, 20, 4,
  326. 0, 20, 56,104,152,196,232,252,252,232,196,152,104, 56, 20, 0,
  327. 0, 20, 56,104,152,196,232,252,252,232,196,152,104, 56, 20, 0,
  328. 4, 20, 52, 96,140,184,216,232,232,216,184,140, 96, 52, 20, 4,
  329. 4, 16, 44, 80,120,156,184,196,196,184,156,120, 80, 44, 16, 4,
  330. 0, 12, 32, 64, 92,120,140,152,152,140,120, 92, 64, 32, 12, 0,
  331. 0, 8, 24, 40, 60, 80, 96,104,104, 96, 80, 60, 40, 24, 8, 0,
  332. 0, 4, 12, 24, 32, 44, 52, 56, 56, 52, 44, 32, 24, 12, 4, 0,
  333. 0, 0, 4, 8, 12, 16, 20, 20, 20, 20, 16, 12, 8, 4, 0, 0,
  334. 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
  335. //error:0.000022
  336. };
  337. #endif /* 0 */
  338. //linear *64
  339. static const uint8_t obmc8[64]={
  340. 4, 12, 20, 28, 28, 20, 12, 4,
  341. 12, 36, 60, 84, 84, 60, 36, 12,
  342. 20, 60,100,140,140,100, 60, 20,
  343. 28, 84,140,196,196,140, 84, 28,
  344. 28, 84,140,196,196,140, 84, 28,
  345. 20, 60,100,140,140,100, 60, 20,
  346. 12, 36, 60, 84, 84, 60, 36, 12,
  347. 4, 12, 20, 28, 28, 20, 12, 4,
  348. //error:0.000000
  349. };
  350. //linear *64
  351. static const uint8_t obmc4[16]={
  352. 16, 48, 48, 16,
  353. 48,144,144, 48,
  354. 48,144,144, 48,
  355. 16, 48, 48, 16,
  356. //error:0.000000
  357. };
  358. static const uint8_t * const obmc_tab[4]={
  359. obmc32, obmc16, obmc8, obmc4
  360. };
  361. static int scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
  362. typedef struct BlockNode{
  363. int16_t mx;
  364. int16_t my;
  365. uint8_t ref;
  366. uint8_t color[3];
  367. uint8_t type;
  368. //#define TYPE_SPLIT 1
  369. #define BLOCK_INTRA 1
  370. #define BLOCK_OPT 2
  371. //#define TYPE_NOCOLOR 4
  372. uint8_t level; //FIXME merge into type?
  373. }BlockNode;
  374. static const BlockNode null_block= { //FIXME add border maybe
  375. .color= {128,128,128},
  376. .mx= 0,
  377. .my= 0,
  378. .ref= 0,
  379. .type= 0,
  380. .level= 0,
  381. };
  382. #define LOG2_MB_SIZE 4
  383. #define MB_SIZE (1<<LOG2_MB_SIZE)
  384. #define ENCODER_EXTRA_BITS 4
  385. #define HTAPS_MAX 8
  386. typedef struct x_and_coeff{
  387. int16_t x;
  388. uint16_t coeff;
  389. } x_and_coeff;
  390. typedef struct SubBand{
  391. int level;
  392. int stride;
  393. int width;
  394. int height;
  395. int qlog; ///< log(qscale)/log[2^(1/6)]
  396. DWTELEM *buf;
  397. IDWTELEM *ibuf;
  398. int buf_x_offset;
  399. int buf_y_offset;
  400. int stride_line; ///< Stride measured in lines, not pixels.
  401. x_and_coeff * x_coeff;
  402. struct SubBand *parent;
  403. uint8_t state[/*7*2*/ 7 + 512][32];
  404. }SubBand;
  405. typedef struct Plane{
  406. int width;
  407. int height;
  408. SubBand band[MAX_DECOMPOSITIONS][4];
  409. int htaps;
  410. int8_t hcoeff[HTAPS_MAX/2];
  411. int diag_mc;
  412. int fast_mc;
  413. int last_htaps;
  414. int8_t last_hcoeff[HTAPS_MAX/2];
  415. int last_diag_mc;
  416. }Plane;
  417. typedef struct SnowContext{
  418. // MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX)
  419. AVCodecContext *avctx;
  420. RangeCoder c;
  421. DSPContext dsp;
  422. AVFrame new_picture;
  423. AVFrame input_picture; ///< new_picture with the internal linesizes
  424. AVFrame current_picture;
  425. AVFrame last_picture[MAX_REF_FRAMES];
  426. uint8_t *halfpel_plane[MAX_REF_FRAMES][4][4];
  427. AVFrame mconly_picture;
  428. // uint8_t q_context[16];
  429. uint8_t header_state[32];
  430. uint8_t block_state[128 + 32*128];
  431. int keyframe;
  432. int always_reset;
  433. int version;
  434. int spatial_decomposition_type;
  435. int last_spatial_decomposition_type;
  436. int temporal_decomposition_type;
  437. int spatial_decomposition_count;
  438. int last_spatial_decomposition_count;
  439. int temporal_decomposition_count;
  440. int max_ref_frames;
  441. int ref_frames;
  442. int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
  443. uint32_t *ref_scores[MAX_REF_FRAMES];
  444. DWTELEM *spatial_dwt_buffer;
  445. IDWTELEM *spatial_idwt_buffer;
  446. int colorspace_type;
  447. int chroma_h_shift;
  448. int chroma_v_shift;
  449. int spatial_scalability;
  450. int qlog;
  451. int last_qlog;
  452. int lambda;
  453. int lambda2;
  454. int pass1_rc;
  455. int mv_scale;
  456. int last_mv_scale;
  457. int qbias;
  458. int last_qbias;
  459. #define QBIAS_SHIFT 3
  460. int b_width;
  461. int b_height;
  462. int block_max_depth;
  463. int last_block_max_depth;
  464. Plane plane[MAX_PLANES];
  465. BlockNode *block;
  466. #define ME_CACHE_SIZE 1024
  467. int me_cache[ME_CACHE_SIZE];
  468. int me_cache_generation;
  469. slice_buffer sb;
  470. MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX)
  471. uint8_t *scratchbuf;
  472. }SnowContext;
  473. typedef struct {
  474. IDWTELEM *b0;
  475. IDWTELEM *b1;
  476. IDWTELEM *b2;
  477. IDWTELEM *b3;
  478. int y;
  479. } DWTCompose;
  480. #define slice_buffer_get_line(slice_buf, line_num) ((slice_buf)->line[line_num] ? (slice_buf)->line[line_num] : slice_buffer_load_line((slice_buf), (line_num)))
  481. //#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num)))
  482. static void iterative_me(SnowContext *s);
  483. static void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer)
  484. {
  485. int i;
  486. buf->base_buffer = base_buffer;
  487. buf->line_count = line_count;
  488. buf->line_width = line_width;
  489. buf->data_count = max_allocated_lines;
  490. buf->line = av_mallocz (sizeof(IDWTELEM *) * line_count);
  491. buf->data_stack = av_malloc (sizeof(IDWTELEM *) * max_allocated_lines);
  492. for(i = 0; i < max_allocated_lines; i++){
  493. buf->data_stack[i] = av_malloc (sizeof(IDWTELEM) * line_width);
  494. }
  495. buf->data_stack_top = max_allocated_lines - 1;
  496. }
  497. static IDWTELEM * slice_buffer_load_line(slice_buffer * buf, int line)
  498. {
  499. int offset;
  500. IDWTELEM * buffer;
  501. assert(buf->data_stack_top >= 0);
  502. // assert(!buf->line[line]);
  503. if (buf->line[line])
  504. return buf->line[line];
  505. offset = buf->line_width * line;
  506. buffer = buf->data_stack[buf->data_stack_top];
  507. buf->data_stack_top--;
  508. buf->line[line] = buffer;
  509. return buffer;
  510. }
  511. static void slice_buffer_release(slice_buffer * buf, int line)
  512. {
  513. int offset;
  514. IDWTELEM * buffer;
  515. assert(line >= 0 && line < buf->line_count);
  516. assert(buf->line[line]);
  517. offset = buf->line_width * line;
  518. buffer = buf->line[line];
  519. buf->data_stack_top++;
  520. buf->data_stack[buf->data_stack_top] = buffer;
  521. buf->line[line] = NULL;
  522. }
  523. static void slice_buffer_flush(slice_buffer * buf)
  524. {
  525. int i;
  526. for(i = 0; i < buf->line_count; i++){
  527. if (buf->line[i])
  528. slice_buffer_release(buf, i);
  529. }
  530. }
  531. static void slice_buffer_destroy(slice_buffer * buf)
  532. {
  533. int i;
  534. slice_buffer_flush(buf);
  535. for(i = buf->data_count - 1; i >= 0; i--){
  536. av_freep(&buf->data_stack[i]);
  537. }
  538. av_freep(&buf->data_stack);
  539. av_freep(&buf->line);
  540. }
  541. #ifdef __sgi
  542. // Avoid a name clash on SGI IRIX
  543. #undef qexp
  544. #endif
  545. #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
  546. static uint8_t qexp[QROOT];
  547. static inline int mirror(int v, int m){
  548. while((unsigned)v > (unsigned)m){
  549. v=-v;
  550. if(v<0) v+= 2*m;
  551. }
  552. return v;
  553. }
  554. static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  555. int i;
  556. if(v){
  557. const int a= FFABS(v);
  558. const int e= av_log2(a);
  559. #if 1
  560. const int el= FFMIN(e, 10);
  561. put_rac(c, state+0, 0);
  562. for(i=0; i<el; i++){
  563. put_rac(c, state+1+i, 1); //1..10
  564. }
  565. for(; i<e; i++){
  566. put_rac(c, state+1+9, 1); //1..10
  567. }
  568. put_rac(c, state+1+FFMIN(i,9), 0);
  569. for(i=e-1; i>=el; i--){
  570. put_rac(c, state+22+9, (a>>i)&1); //22..31
  571. }
  572. for(; i>=0; i--){
  573. put_rac(c, state+22+i, (a>>i)&1); //22..31
  574. }
  575. if(is_signed)
  576. put_rac(c, state+11 + el, v < 0); //11..21
  577. #else
  578. put_rac(c, state+0, 0);
  579. if(e<=9){
  580. for(i=0; i<e; i++){
  581. put_rac(c, state+1+i, 1); //1..10
  582. }
  583. put_rac(c, state+1+i, 0);
  584. for(i=e-1; i>=0; i--){
  585. put_rac(c, state+22+i, (a>>i)&1); //22..31
  586. }
  587. if(is_signed)
  588. put_rac(c, state+11 + e, v < 0); //11..21
  589. }else{
  590. for(i=0; i<e; i++){
  591. put_rac(c, state+1+FFMIN(i,9), 1); //1..10
  592. }
  593. put_rac(c, state+1+FFMIN(i,9), 0);
  594. for(i=e-1; i>=0; i--){
  595. put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
  596. }
  597. if(is_signed)
  598. put_rac(c, state+11 + FFMIN(e,10), v < 0); //11..21
  599. }
  600. #endif /* 1 */
  601. }else{
  602. put_rac(c, state+0, 1);
  603. }
  604. }
  605. static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  606. if(get_rac(c, state+0))
  607. return 0;
  608. else{
  609. int i, e, a;
  610. e= 0;
  611. while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
  612. e++;
  613. }
  614. a= 1;
  615. for(i=e-1; i>=0; i--){
  616. a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
  617. }
  618. if(is_signed && get_rac(c, state+11 + FFMIN(e,10))) //11..21
  619. return -a;
  620. else
  621. return a;
  622. }
  623. }
  624. static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
  625. int i;
  626. int r= log2>=0 ? 1<<log2 : 1;
  627. assert(v>=0);
  628. assert(log2>=-4);
  629. while(v >= r){
  630. put_rac(c, state+4+log2, 1);
  631. v -= r;
  632. log2++;
  633. if(log2>0) r+=r;
  634. }
  635. put_rac(c, state+4+log2, 0);
  636. for(i=log2-1; i>=0; i--){
  637. put_rac(c, state+31-i, (v>>i)&1);
  638. }
  639. }
  640. static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
  641. int i;
  642. int r= log2>=0 ? 1<<log2 : 1;
  643. int v=0;
  644. assert(log2>=-4);
  645. while(get_rac(c, state+4+log2)){
  646. v+= r;
  647. log2++;
  648. if(log2>0) r+=r;
  649. }
  650. for(i=log2-1; i>=0; i--){
  651. v+= get_rac(c, state+31-i)<<i;
  652. }
  653. return v;
  654. }
  655. static av_always_inline void
  656. lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  657. int dst_step, int src_step, int ref_step,
  658. int width, int mul, int add, int shift,
  659. int highpass, int inverse){
  660. const int mirror_left= !highpass;
  661. const int mirror_right= (width&1) ^ highpass;
  662. const int w= (width>>1) - 1 + (highpass & width);
  663. int i;
  664. #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
  665. if(mirror_left){
  666. dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
  667. dst += dst_step;
  668. src += src_step;
  669. }
  670. for(i=0; i<w; i++){
  671. dst[i*dst_step] =
  672. LIFT(src[i*src_step],
  673. ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
  674. inverse);
  675. }
  676. if(mirror_right){
  677. dst[w*dst_step] =
  678. LIFT(src[w*src_step],
  679. ((mul*2*ref[w*ref_step]+add)>>shift),
  680. inverse);
  681. }
  682. }
  683. static av_always_inline void
  684. inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
  685. int dst_step, int src_step, int ref_step,
  686. int width, int mul, int add, int shift,
  687. int highpass, int inverse){
  688. const int mirror_left= !highpass;
  689. const int mirror_right= (width&1) ^ highpass;
  690. const int w= (width>>1) - 1 + (highpass & width);
  691. int i;
  692. #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
  693. if(mirror_left){
  694. dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
  695. dst += dst_step;
  696. src += src_step;
  697. }
  698. for(i=0; i<w; i++){
  699. dst[i*dst_step] =
  700. LIFT(src[i*src_step],
  701. ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
  702. inverse);
  703. }
  704. if(mirror_right){
  705. dst[w*dst_step] =
  706. LIFT(src[w*src_step],
  707. ((mul*2*ref[w*ref_step]+add)>>shift),
  708. inverse);
  709. }
  710. }
  711. #ifndef liftS
  712. static av_always_inline void
  713. liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  714. int dst_step, int src_step, int ref_step,
  715. int width, int mul, int add, int shift,
  716. int highpass, int inverse){
  717. const int mirror_left= !highpass;
  718. const int mirror_right= (width&1) ^ highpass;
  719. const int w= (width>>1) - 1 + (highpass & width);
  720. int i;
  721. assert(shift == 4);
  722. #define LIFTS(src, ref, inv) \
  723. ((inv) ? \
  724. (src) + (((ref) + 4*(src))>>shift): \
  725. -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
  726. if(mirror_left){
  727. dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
  728. dst += dst_step;
  729. src += src_step;
  730. }
  731. for(i=0; i<w; i++){
  732. dst[i*dst_step] =
  733. LIFTS(src[i*src_step],
  734. mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
  735. inverse);
  736. }
  737. if(mirror_right){
  738. dst[w*dst_step] =
  739. LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
  740. }
  741. }
  742. static av_always_inline void
  743. inv_liftS(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
  744. int dst_step, int src_step, int ref_step,
  745. int width, int mul, int add, int shift,
  746. int highpass, int inverse){
  747. const int mirror_left= !highpass;
  748. const int mirror_right= (width&1) ^ highpass;
  749. const int w= (width>>1) - 1 + (highpass & width);
  750. int i;
  751. assert(shift == 4);
  752. #define LIFTS(src, ref, inv) \
  753. ((inv) ? \
  754. (src) + (((ref) + 4*(src))>>shift): \
  755. -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
  756. if(mirror_left){
  757. dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
  758. dst += dst_step;
  759. src += src_step;
  760. }
  761. for(i=0; i<w; i++){
  762. dst[i*dst_step] =
  763. LIFTS(src[i*src_step],
  764. mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
  765. inverse);
  766. }
  767. if(mirror_right){
  768. dst[w*dst_step] =
  769. LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
  770. }
  771. }
  772. #endif /* ! liftS */
  773. static void horizontal_decompose53i(DWTELEM *b, int width){
  774. DWTELEM temp[width];
  775. const int width2= width>>1;
  776. int x;
  777. const int w2= (width+1)>>1;
  778. for(x=0; x<width2; x++){
  779. temp[x ]= b[2*x ];
  780. temp[x+w2]= b[2*x + 1];
  781. }
  782. if(width&1)
  783. temp[x ]= b[2*x ];
  784. #if 0
  785. {
  786. int A1,A2,A3,A4;
  787. A2= temp[1 ];
  788. A4= temp[0 ];
  789. A1= temp[0+width2];
  790. A1 -= (A2 + A4)>>1;
  791. A4 += (A1 + 1)>>1;
  792. b[0+width2] = A1;
  793. b[0 ] = A4;
  794. for(x=1; x+1<width2; x+=2){
  795. A3= temp[x+width2];
  796. A4= temp[x+1 ];
  797. A3 -= (A2 + A4)>>1;
  798. A2 += (A1 + A3 + 2)>>2;
  799. b[x+width2] = A3;
  800. b[x ] = A2;
  801. A1= temp[x+1+width2];
  802. A2= temp[x+2 ];
  803. A1 -= (A2 + A4)>>1;
  804. A4 += (A1 + A3 + 2)>>2;
  805. b[x+1+width2] = A1;
  806. b[x+1 ] = A4;
  807. }
  808. A3= temp[width-1];
  809. A3 -= A2;
  810. A2 += (A1 + A3 + 2)>>2;
  811. b[width -1] = A3;
  812. b[width2-1] = A2;
  813. }
  814. #else
  815. lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
  816. lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
  817. #endif /* 0 */
  818. }
  819. static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  820. int i;
  821. for(i=0; i<width; i++){
  822. b1[i] -= (b0[i] + b2[i])>>1;
  823. }
  824. }
  825. static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  826. int i;
  827. for(i=0; i<width; i++){
  828. b1[i] += (b0[i] + b2[i] + 2)>>2;
  829. }
  830. }
  831. static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
  832. int y;
  833. DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
  834. DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
  835. for(y=-2; y<height; y+=2){
  836. DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  837. DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  838. if(y+1<(unsigned)height) horizontal_decompose53i(b2, width);
  839. if(y+2<(unsigned)height) horizontal_decompose53i(b3, width);
  840. if(y+1<(unsigned)height) vertical_decompose53iH0(b1, b2, b3, width);
  841. if(y+0<(unsigned)height) vertical_decompose53iL0(b0, b1, b2, width);
  842. b0=b2;
  843. b1=b3;
  844. }
  845. }
  846. static void horizontal_decompose97i(DWTELEM *b, int width){
  847. DWTELEM temp[width];
  848. const int w2= (width+1)>>1;
  849. lift (temp+w2, b +1, b , 1, 2, 2, width, W_AM, W_AO, W_AS, 1, 1);
  850. liftS(temp , b , temp+w2, 1, 2, 1, width, W_BM, W_BO, W_BS, 0, 0);
  851. lift (b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
  852. lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
  853. }
  854. static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  855. int i;
  856. for(i=0; i<width; i++){
  857. b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  858. }
  859. }
  860. static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  861. int i;
  862. for(i=0; i<width; i++){
  863. b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  864. }
  865. }
  866. static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  867. int i;
  868. for(i=0; i<width; i++){
  869. #ifdef liftS
  870. b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  871. #else
  872. b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + W_BO*5 + (5<<27)) / (5*16) - (1<<23);
  873. #endif
  874. }
  875. }
  876. static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  877. int i;
  878. for(i=0; i<width; i++){
  879. b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  880. }
  881. }
  882. static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
  883. int y;
  884. DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
  885. DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
  886. DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
  887. DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
  888. for(y=-4; y<height; y+=2){
  889. DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  890. DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  891. if(y+3<(unsigned)height) horizontal_decompose97i(b4, width);
  892. if(y+4<(unsigned)height) horizontal_decompose97i(b5, width);
  893. if(y+3<(unsigned)height) vertical_decompose97iH0(b3, b4, b5, width);
  894. if(y+2<(unsigned)height) vertical_decompose97iL0(b2, b3, b4, width);
  895. if(y+1<(unsigned)height) vertical_decompose97iH1(b1, b2, b3, width);
  896. if(y+0<(unsigned)height) vertical_decompose97iL1(b0, b1, b2, width);
  897. b0=b2;
  898. b1=b3;
  899. b2=b4;
  900. b3=b5;
  901. }
  902. }
  903. void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  904. int level;
  905. for(level=0; level<decomposition_count; level++){
  906. switch(type){
  907. case DWT_97: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
  908. case DWT_53: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
  909. }
  910. }
  911. }
  912. static void horizontal_compose53i(IDWTELEM *b, int width){
  913. IDWTELEM temp[width];
  914. const int width2= width>>1;
  915. const int w2= (width+1)>>1;
  916. int x;
  917. #if 0
  918. int A1,A2,A3,A4;
  919. A2= temp[1 ];
  920. A4= temp[0 ];
  921. A1= temp[0+width2];
  922. A1 -= (A2 + A4)>>1;
  923. A4 += (A1 + 1)>>1;
  924. b[0+width2] = A1;
  925. b[0 ] = A4;
  926. for(x=1; x+1<width2; x+=2){
  927. A3= temp[x+width2];
  928. A4= temp[x+1 ];
  929. A3 -= (A2 + A4)>>1;
  930. A2 += (A1 + A3 + 2)>>2;
  931. b[x+width2] = A3;
  932. b[x ] = A2;
  933. A1= temp[x+1+width2];
  934. A2= temp[x+2 ];
  935. A1 -= (A2 + A4)>>1;
  936. A4 += (A1 + A3 + 2)>>2;
  937. b[x+1+width2] = A1;
  938. b[x+1 ] = A4;
  939. }
  940. A3= temp[width-1];
  941. A3 -= A2;
  942. A2 += (A1 + A3 + 2)>>2;
  943. b[width -1] = A3;
  944. b[width2-1] = A2;
  945. #else
  946. inv_lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1);
  947. inv_lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
  948. #endif /* 0 */
  949. for(x=0; x<width2; x++){
  950. b[2*x ]= temp[x ];
  951. b[2*x + 1]= temp[x+w2];
  952. }
  953. if(width&1)
  954. b[2*x ]= temp[x ];
  955. }
  956. static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  957. int i;
  958. for(i=0; i<width; i++){
  959. b1[i] += (b0[i] + b2[i])>>1;
  960. }
  961. }
  962. static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  963. int i;
  964. for(i=0; i<width; i++){
  965. b1[i] -= (b0[i] + b2[i] + 2)>>2;
  966. }
  967. }
  968. static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
  969. cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
  970. cs->b1 = slice_buffer_get_line(sb, mirror(-1 , height-1) * stride_line);
  971. cs->y = -1;
  972. }
  973. static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
  974. cs->b0 = buffer + mirror(-1-1, height-1)*stride;
  975. cs->b1 = buffer + mirror(-1 , height-1)*stride;
  976. cs->y = -1;
  977. }
  978. static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
  979. int y= cs->y;
  980. IDWTELEM *b0= cs->b0;
  981. IDWTELEM *b1= cs->b1;
  982. IDWTELEM *b2= slice_buffer_get_line(sb, mirror(y+1, height-1) * stride_line);
  983. IDWTELEM *b3= slice_buffer_get_line(sb, mirror(y+2, height-1) * stride_line);
  984. if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
  985. if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
  986. if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
  987. if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
  988. cs->b0 = b2;
  989. cs->b1 = b3;
  990. cs->y += 2;
  991. }
  992. static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
  993. int y= cs->y;
  994. IDWTELEM *b0= cs->b0;
  995. IDWTELEM *b1= cs->b1;
  996. IDWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  997. IDWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  998. if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
  999. if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
  1000. if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
  1001. if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
  1002. cs->b0 = b2;
  1003. cs->b1 = b3;
  1004. cs->y += 2;
  1005. }
  1006. static void av_unused spatial_compose53i(IDWTELEM *buffer, int width, int height, int stride){
  1007. DWTCompose cs;
  1008. spatial_compose53i_init(&cs, buffer, height, stride);
  1009. while(cs.y <= height)
  1010. spatial_compose53i_dy(&cs, buffer, width, height, stride);
  1011. }
  1012. void ff_snow_horizontal_compose97i(IDWTELEM *b, int width){
  1013. IDWTELEM temp[width];
  1014. const int w2= (width+1)>>1;
  1015. inv_lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
  1016. inv_lift (temp+w2, b +w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 1);
  1017. inv_liftS(b , temp , temp+w2, 2, 1, 1, width, W_BM, W_BO, W_BS, 0, 1);
  1018. inv_lift (b+1 , temp+w2, b , 2, 1, 2, width, W_AM, W_AO, W_AS, 1, 0);
  1019. }
  1020. static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  1021. int i;
  1022. for(i=0; i<width; i++){
  1023. b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  1024. }
  1025. }
  1026. static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  1027. int i;
  1028. for(i=0; i<width; i++){
  1029. b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  1030. }
  1031. }
  1032. static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  1033. int i;
  1034. for(i=0; i<width; i++){
  1035. #ifdef liftS
  1036. b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  1037. #else
  1038. b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
  1039. #endif
  1040. }
  1041. }
  1042. static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  1043. int i;
  1044. for(i=0; i<width; i++){
  1045. b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  1046. }
  1047. }
  1048. void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width){
  1049. int i;
  1050. for(i=0; i<width; i++){
  1051. b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
  1052. b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
  1053. #ifdef liftS
  1054. b2[i] += (W_BM*(b1[i] + b3[i])+W_BO)>>W_BS;
  1055. #else
  1056. b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
  1057. #endif
  1058. b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  1059. }
  1060. }
  1061. static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
  1062. cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
  1063. cs->b1 = slice_buffer_get_line(sb, mirror(-3 , height-1) * stride_line);
  1064. cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
  1065. cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
  1066. cs->y = -3;
  1067. }
  1068. static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
  1069. cs->b0 = buffer + mirror(-3-1, height-1)*stride;
  1070. cs->b1 = buffer + mirror(-3 , height-1)*stride;
  1071. cs->b2 = buffer + mirror(-3+1, height-1)*stride;
  1072. cs->b3 = buffer + mirror(-3+2, height-1)*stride;
  1073. cs->y = -3;
  1074. }
  1075. static void spatial_compose97i_dy_buffered(DSPContext *dsp, DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
  1076. int y = cs->y;
  1077. IDWTELEM *b0= cs->b0;
  1078. IDWTELEM *b1= cs->b1;
  1079. IDWTELEM *b2= cs->b2;
  1080. IDWTELEM *b3= cs->b3;
  1081. IDWTELEM *b4= slice_buffer_get_line(sb, mirror(y + 3, height - 1) * stride_line);
  1082. IDWTELEM *b5= slice_buffer_get_line(sb, mirror(y + 4, height - 1) * stride_line);
  1083. if(y>0 && y+4<height){
  1084. dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
  1085. }else{
  1086. if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
  1087. if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
  1088. if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
  1089. if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
  1090. }
  1091. if(y-1<(unsigned)height) dsp->horizontal_compose97i(b0, width);
  1092. if(y+0<(unsigned)height) dsp->horizontal_compose97i(b1, width);
  1093. cs->b0=b2;
  1094. cs->b1=b3;
  1095. cs->b2=b4;
  1096. cs->b3=b5;
  1097. cs->y += 2;
  1098. }
  1099. static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
  1100. int y = cs->y;
  1101. IDWTELEM *b0= cs->b0;
  1102. IDWTELEM *b1= cs->b1;
  1103. IDWTELEM *b2= cs->b2;
  1104. IDWTELEM *b3= cs->b3;
  1105. IDWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  1106. IDWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  1107. if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
  1108. if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
  1109. if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
  1110. if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
  1111. if(y-1<(unsigned)height) ff_snow_horizontal_compose97i(b0, width);
  1112. if(y+0<(unsigned)height) ff_snow_horizontal_compose97i(b1, width);
  1113. cs->b0=b2;
  1114. cs->b1=b3;
  1115. cs->b2=b4;
  1116. cs->b3=b5;
  1117. cs->y += 2;
  1118. }
  1119. static void av_unused spatial_compose97i(IDWTELEM *buffer, int width, int height, int stride){
  1120. DWTCompose cs;
  1121. spatial_compose97i_init(&cs, buffer, height, stride);
  1122. while(cs.y <= height)
  1123. spatial_compose97i_dy(&cs, buffer, width, height, stride);
  1124. }
  1125. static void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count){
  1126. int level;
  1127. for(level=decomposition_count-1; level>=0; level--){
  1128. switch(type){
  1129. case DWT_97: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
  1130. case DWT_53: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
  1131. }
  1132. }
  1133. }
  1134. static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  1135. int level;
  1136. for(level=decomposition_count-1; level>=0; level--){
  1137. switch(type){
  1138. case DWT_97: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
  1139. case DWT_53: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
  1140. }
  1141. }
  1142. }
  1143. static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
  1144. const int support = type==1 ? 3 : 5;
  1145. int level;
  1146. if(type==2) return;
  1147. for(level=decomposition_count-1; level>=0; level--){
  1148. while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
  1149. switch(type){
  1150. case DWT_97: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
  1151. break;
  1152. case DWT_53: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
  1153. break;
  1154. }
  1155. }
  1156. }
  1157. }
  1158. static void ff_spatial_idwt_buffered_slice(DSPContext *dsp, DWTCompose *cs, slice_buffer * slice_buf, int width, int height, int stride_line, int type, int decomposition_count, int y){
  1159. const int support = type==1 ? 3 : 5;
  1160. int level;
  1161. if(type==2) return;
  1162. for(level=decomposition_count-1; level>=0; level--){
  1163. while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
  1164. switch(type){
  1165. case DWT_97: spatial_compose97i_dy_buffered(dsp, cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
  1166. break;
  1167. case DWT_53: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. }
  1173. static void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  1174. DWTCompose cs[MAX_DECOMPOSITIONS];
  1175. int y;
  1176. ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
  1177. for(y=0; y<height; y+=4)
  1178. ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
  1179. }
  1180. static int encode_subband_c0run(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
  1181. const int w= b->width;
  1182. const int h= b->height;
  1183. int x, y;
  1184. if(1){
  1185. int run=0;
  1186. int runs[w*h];
  1187. int run_index=0;
  1188. int max_index;
  1189. for(y=0; y<h; y++){
  1190. for(x=0; x<w; x++){
  1191. int v, p=0;
  1192. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1193. v= src[x + y*stride];
  1194. if(y){
  1195. t= src[x + (y-1)*stride];
  1196. if(x){
  1197. lt= src[x - 1 + (y-1)*stride];
  1198. }
  1199. if(x + 1 < w){
  1200. rt= src[x + 1 + (y-1)*stride];
  1201. }
  1202. }
  1203. if(x){
  1204. l= src[x - 1 + y*stride];
  1205. /*if(x > 1){
  1206. if(orientation==1) ll= src[y + (x-2)*stride];
  1207. else ll= src[x - 2 + y*stride];
  1208. }*/
  1209. }
  1210. if(parent){
  1211. int px= x>>1;
  1212. int py= y>>1;
  1213. if(px<b->parent->width && py<b->parent->height)
  1214. p= parent[px + py*2*stride];
  1215. }
  1216. if(!(/*ll|*/l|lt|t|rt|p)){
  1217. if(v){
  1218. runs[run_index++]= run;
  1219. run=0;
  1220. }else{
  1221. run++;
  1222. }
  1223. }
  1224. }
  1225. }
  1226. max_index= run_index;
  1227. runs[run_index++]= run;
  1228. run_index=0;
  1229. run= runs[run_index++];
  1230. put_symbol2(&s->c, b->state[30], max_index, 0);
  1231. if(run_index <= max_index)
  1232. put_symbol2(&s->c, b->state[1], run, 3);
  1233. for(y=0; y<h; y++){
  1234. if(s->c.bytestream_end - s->c.bytestream < w*40){
  1235. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  1236. return -1;
  1237. }
  1238. for(x=0; x<w; x++){
  1239. int v, p=0;
  1240. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1241. v= src[x + y*stride];
  1242. if(y){
  1243. t= src[x + (y-1)*stride];
  1244. if(x){
  1245. lt= src[x - 1 + (y-1)*stride];
  1246. }
  1247. if(x + 1 < w){
  1248. rt= src[x + 1 + (y-1)*stride];
  1249. }
  1250. }
  1251. if(x){
  1252. l= src[x - 1 + y*stride];
  1253. /*if(x > 1){
  1254. if(orientation==1) ll= src[y + (x-2)*stride];
  1255. else ll= src[x - 2 + y*stride];
  1256. }*/
  1257. }
  1258. if(parent){
  1259. int px= x>>1;
  1260. int py= y>>1;
  1261. if(px<b->parent->width && py<b->parent->height)
  1262. p= parent[px + py*2*stride];
  1263. }
  1264. if(/*ll|*/l|lt|t|rt|p){
  1265. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  1266. put_rac(&s->c, &b->state[0][context], !!v);
  1267. }else{
  1268. if(!run){
  1269. run= runs[run_index++];
  1270. if(run_index <= max_index)
  1271. put_symbol2(&s->c, b->state[1], run, 3);
  1272. assert(v);
  1273. }else{
  1274. run--;
  1275. assert(!v);
  1276. }
  1277. }
  1278. if(v){
  1279. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  1280. int l2= 2*FFABS(l) + (l<0);
  1281. int t2= 2*FFABS(t) + (t<0);
  1282. put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
  1283. put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l2&0xFF] + 3*quant3bA[t2&0xFF]], v<0);
  1284. }
  1285. }
  1286. }
  1287. }
  1288. return 0;
  1289. }
  1290. static int encode_subband(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
  1291. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  1292. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  1293. return encode_subband_c0run(s, b, src, parent, stride, orientation);
  1294. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  1295. }
  1296. static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
  1297. const int w= b->width;
  1298. const int h= b->height;
  1299. int x,y;
  1300. if(1){
  1301. int run, runs;
  1302. x_and_coeff *xc= b->x_coeff;
  1303. x_and_coeff *prev_xc= NULL;
  1304. x_and_coeff *prev2_xc= xc;
  1305. x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL;
  1306. x_and_coeff *prev_parent_xc= parent_xc;
  1307. runs= get_symbol2(&s->c, b->state[30], 0);
  1308. if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
  1309. else run= INT_MAX;
  1310. for(y=0; y<h; y++){
  1311. int v=0;
  1312. int lt=0, t=0, rt=0;
  1313. if(y && prev_xc->x == 0){
  1314. rt= prev_xc->coeff;
  1315. }
  1316. for(x=0; x<w; x++){
  1317. int p=0;
  1318. const int l= v;
  1319. lt= t; t= rt;
  1320. if(y){
  1321. if(prev_xc->x <= x)
  1322. prev_xc++;
  1323. if(prev_xc->x == x + 1)
  1324. rt= prev_xc->coeff;
  1325. else
  1326. rt=0;
  1327. }
  1328. if(parent_xc){
  1329. if(x>>1 > parent_xc->x){
  1330. parent_xc++;
  1331. }
  1332. if(x>>1 == parent_xc->x){
  1333. p= parent_xc->coeff;
  1334. }
  1335. }
  1336. if(/*ll|*/l|lt|t|rt|p){
  1337. int context= av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
  1338. v=get_rac(&s->c, &b->state[0][context]);
  1339. if(v){
  1340. v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
  1341. v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l&0xFF] + 3*quant3bA[t&0xFF]]);
  1342. xc->x=x;
  1343. (xc++)->coeff= v;
  1344. }
  1345. }else{
  1346. if(!run){
  1347. if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
  1348. else run= INT_MAX;
  1349. v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
  1350. v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
  1351. xc->x=x;
  1352. (xc++)->coeff= v;
  1353. }else{
  1354. int max_run;
  1355. run--;
  1356. v=0;
  1357. if(y) max_run= FFMIN(run, prev_xc->x - x - 2);
  1358. else max_run= FFMIN(run, w-x-1);
  1359. if(parent_xc)
  1360. max_run= FFMIN(max_run, 2*parent_xc->x - x - 1);
  1361. x+= max_run;
  1362. run-= max_run;
  1363. }
  1364. }
  1365. }
  1366. (xc++)->x= w+1; //end marker
  1367. prev_xc= prev2_xc;
  1368. prev2_xc= xc;
  1369. if(parent_xc){
  1370. if(y&1){
  1371. while(parent_xc->x != parent->width+1)
  1372. parent_xc++;
  1373. parent_xc++;
  1374. prev_parent_xc= parent_xc;
  1375. }else{
  1376. parent_xc= prev_parent_xc;
  1377. }
  1378. }
  1379. }
  1380. (xc++)->x= w+1; //end marker
  1381. }
  1382. }
  1383. static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
  1384. const int w= b->width;
  1385. int y;
  1386. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  1387. int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  1388. int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  1389. int new_index = 0;
  1390. if(b->ibuf == s->spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){
  1391. qadd= 0;
  1392. qmul= 1<<QEXPSHIFT;
  1393. }
  1394. /* If we are on the second or later slice, restore our index. */
  1395. if (start_y != 0)
  1396. new_index = save_state[0];
  1397. for(y=start_y; y<h; y++){
  1398. int x = 0;
  1399. int v;
  1400. IDWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
  1401. memset(line, 0, b->width*sizeof(IDWTELEM));
  1402. v = b->x_coeff[new_index].coeff;
  1403. x = b->x_coeff[new_index++].x;
  1404. while(x < w){
  1405. register int t= ( (v>>1)*qmul + qadd)>>QEXPSHIFT;
  1406. register int u= -(v&1);
  1407. line[x] = (t^u) - u;
  1408. v = b->x_coeff[new_index].coeff;
  1409. x = b->x_coeff[new_index++].x;
  1410. }
  1411. }
  1412. /* Save our variables for the next slice. */
  1413. save_state[0] = new_index;
  1414. return;
  1415. }
  1416. static void reset_contexts(SnowContext *s){ //FIXME better initial contexts
  1417. int plane_index, level, orientation;
  1418. for(plane_index=0; plane_index<3; plane_index++){
  1419. for(level=0; level<MAX_DECOMPOSITIONS; level++){
  1420. for(orientation=level ? 1:0; orientation<4; orientation++){
  1421. memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
  1422. }
  1423. }
  1424. }
  1425. memset(s->header_state, MID_STATE, sizeof(s->header_state));
  1426. memset(s->block_state, MID_STATE, sizeof(s->block_state));
  1427. }
  1428. static int alloc_blocks(SnowContext *s){
  1429. int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
  1430. int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
  1431. s->b_width = w;
  1432. s->b_height= h;
  1433. av_free(s->block);
  1434. s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
  1435. return 0;
  1436. }
  1437. static inline void copy_rac_state(RangeCoder *d, RangeCoder *s){
  1438. uint8_t *bytestream= d->bytestream;
  1439. uint8_t *bytestream_start= d->bytestream_start;
  1440. *d= *s;
  1441. d->bytestream= bytestream;
  1442. d->bytestream_start= bytestream_start;
  1443. }
  1444. //near copy & paste from dsputil, FIXME
  1445. static int pix_sum(uint8_t * pix, int line_size, int w)
  1446. {
  1447. int s, i, j;
  1448. s = 0;
  1449. for (i = 0; i < w; i++) {
  1450. for (j = 0; j < w; j++) {
  1451. s += pix[0];
  1452. pix ++;
  1453. }
  1454. pix += line_size - w;
  1455. }
  1456. return s;
  1457. }
  1458. //near copy & paste from dsputil, FIXME
  1459. static int pix_norm1(uint8_t * pix, int line_size, int w)
  1460. {
  1461. int s, i, j;
  1462. uint32_t *sq = ff_squareTbl + 256;
  1463. s = 0;
  1464. for (i = 0; i < w; i++) {
  1465. for (j = 0; j < w; j ++) {
  1466. s += sq[pix[0]];
  1467. pix ++;
  1468. }
  1469. pix += line_size - w;
  1470. }
  1471. return s;
  1472. }
  1473. static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type){
  1474. const int w= s->b_width << s->block_max_depth;
  1475. const int rem_depth= s->block_max_depth - level;
  1476. const int index= (x + y*w) << rem_depth;
  1477. const int block_w= 1<<rem_depth;
  1478. BlockNode block;
  1479. int i,j;
  1480. block.color[0]= l;
  1481. block.color[1]= cb;
  1482. block.color[2]= cr;
  1483. block.mx= mx;
  1484. block.my= my;
  1485. block.ref= ref;
  1486. block.type= type;
  1487. block.level= level;
  1488. for(j=0; j<block_w; j++){
  1489. for(i=0; i<block_w; i++){
  1490. s->block[index + i + j*w]= block;
  1491. }
  1492. }
  1493. }
  1494. static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
  1495. const int offset[3]= {
  1496. y*c-> stride + x,
  1497. ((y*c->uvstride + x)>>1),
  1498. ((y*c->uvstride + x)>>1),
  1499. };
  1500. int i;
  1501. for(i=0; i<3; i++){
  1502. c->src[0][i]= src [i];
  1503. c->ref[0][i]= ref [i] + offset[i];
  1504. }
  1505. assert(!ref_index);
  1506. }
  1507. static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
  1508. const BlockNode *left, const BlockNode *top, const BlockNode *tr){
  1509. if(s->ref_frames == 1){
  1510. *mx = mid_pred(left->mx, top->mx, tr->mx);
  1511. *my = mid_pred(left->my, top->my, tr->my);
  1512. }else{
  1513. const int *scale = scale_mv_ref[ref];
  1514. *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
  1515. (top ->mx * scale[top ->ref] + 128) >>8,
  1516. (tr ->mx * scale[tr ->ref] + 128) >>8);
  1517. *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
  1518. (top ->my * scale[top ->ref] + 128) >>8,
  1519. (tr ->my * scale[tr ->ref] + 128) >>8);
  1520. }
  1521. }
  1522. //FIXME copy&paste
  1523. #define P_LEFT P[1]
  1524. #define P_TOP P[2]
  1525. #define P_TOPRIGHT P[3]
  1526. #define P_MEDIAN P[4]
  1527. #define P_MV1 P[9]
  1528. #define FLAG_QPEL 1 //must be 1
  1529. static int encode_q_branch(SnowContext *s, int level, int x, int y){
  1530. uint8_t p_buffer[1024];
  1531. uint8_t i_buffer[1024];
  1532. uint8_t p_state[sizeof(s->block_state)];
  1533. uint8_t i_state[sizeof(s->block_state)];
  1534. RangeCoder pc, ic;
  1535. uint8_t *pbbak= s->c.bytestream;
  1536. uint8_t *pbbak_start= s->c.bytestream_start;
  1537. int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
  1538. const int w= s->b_width << s->block_max_depth;
  1539. const int h= s->b_height << s->block_max_depth;
  1540. const int rem_depth= s->block_max_depth - level;
  1541. const int index= (x + y*w) << rem_depth;
  1542. const int block_w= 1<<(LOG2_MB_SIZE - level);
  1543. int trx= (x+1)<<rem_depth;
  1544. int try= (y+1)<<rem_depth;
  1545. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  1546. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  1547. const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  1548. const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  1549. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1550. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1551. int pl = left->color[0];
  1552. int pcb= left->color[1];
  1553. int pcr= left->color[2];
  1554. int pmx, pmy;
  1555. int mx=0, my=0;
  1556. int l,cr,cb;
  1557. const int stride= s->current_picture.linesize[0];
  1558. const int uvstride= s->current_picture.linesize[1];
  1559. uint8_t *current_data[3]= { s->input_picture.data[0] + (x + y* stride)*block_w,
  1560. s->input_picture.data[1] + (x + y*uvstride)*block_w/2,
  1561. s->input_picture.data[2] + (x + y*uvstride)*block_w/2};
  1562. int P[10][2];
  1563. int16_t last_mv[3][2];
  1564. int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
  1565. const int shift= 1+qpel;
  1566. MotionEstContext *c= &s->m.me;
  1567. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  1568. int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  1569. int my_context= av_log2(2*FFABS(left->my - top->my));
  1570. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1571. int ref, best_ref, ref_score, ref_mx, ref_my;
  1572. assert(sizeof(s->block_state) >= 256);
  1573. if(s->keyframe){
  1574. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  1575. return 0;
  1576. }
  1577. // clip predictors / edge ?
  1578. P_LEFT[0]= left->mx;
  1579. P_LEFT[1]= left->my;
  1580. P_TOP [0]= top->mx;
  1581. P_TOP [1]= top->my;
  1582. P_TOPRIGHT[0]= tr->mx;
  1583. P_TOPRIGHT[1]= tr->my;
  1584. last_mv[0][0]= s->block[index].mx;
  1585. last_mv[0][1]= s->block[index].my;
  1586. last_mv[1][0]= right->mx;
  1587. last_mv[1][1]= right->my;
  1588. last_mv[2][0]= bottom->mx;
  1589. last_mv[2][1]= bottom->my;
  1590. s->m.mb_stride=2;
  1591. s->m.mb_x=
  1592. s->m.mb_y= 0;
  1593. c->skip= 0;
  1594. assert(c-> stride == stride);
  1595. assert(c->uvstride == uvstride);
  1596. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1597. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1598. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1599. c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
  1600. c->xmin = - x*block_w - 16+2;
  1601. c->ymin = - y*block_w - 16+2;
  1602. c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
  1603. c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
  1604. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1605. if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  1606. if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  1607. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  1608. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1609. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  1610. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1611. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1612. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1613. if (!y) {
  1614. c->pred_x= P_LEFT[0];
  1615. c->pred_y= P_LEFT[1];
  1616. } else {
  1617. c->pred_x = P_MEDIAN[0];
  1618. c->pred_y = P_MEDIAN[1];
  1619. }
  1620. score= INT_MAX;
  1621. best_ref= 0;
  1622. for(ref=0; ref<s->ref_frames; ref++){
  1623. init_ref(c, current_data, s->last_picture[ref].data, NULL, block_w*x, block_w*y, 0);
  1624. ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
  1625. (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  1626. assert(ref_mx >= c->xmin);
  1627. assert(ref_mx <= c->xmax);
  1628. assert(ref_my >= c->ymin);
  1629. assert(ref_my <= c->ymax);
  1630. ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  1631. ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  1632. ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
  1633. if(s->ref_mvs[ref]){
  1634. s->ref_mvs[ref][index][0]= ref_mx;
  1635. s->ref_mvs[ref][index][1]= ref_my;
  1636. s->ref_scores[ref][index]= ref_score;
  1637. }
  1638. if(score > ref_score){
  1639. score= ref_score;
  1640. best_ref= ref;
  1641. mx= ref_mx;
  1642. my= ref_my;
  1643. }
  1644. }
  1645. //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
  1646. // subpel search
  1647. base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
  1648. pc= s->c;
  1649. pc.bytestream_start=
  1650. pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
  1651. memcpy(p_state, s->block_state, sizeof(s->block_state));
  1652. if(level!=s->block_max_depth)
  1653. put_rac(&pc, &p_state[4 + s_context], 1);
  1654. put_rac(&pc, &p_state[1 + left->type + top->type], 0);
  1655. if(s->ref_frames > 1)
  1656. put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
  1657. pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
  1658. put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
  1659. put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
  1660. p_len= pc.bytestream - pc.bytestream_start;
  1661. score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
  1662. block_s= block_w*block_w;
  1663. sum = pix_sum(current_data[0], stride, block_w);
  1664. l= (sum + block_s/2)/block_s;
  1665. iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
  1666. block_s= block_w*block_w>>2;
  1667. sum = pix_sum(current_data[1], uvstride, block_w>>1);
  1668. cb= (sum + block_s/2)/block_s;
  1669. // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  1670. sum = pix_sum(current_data[2], uvstride, block_w>>1);
  1671. cr= (sum + block_s/2)/block_s;
  1672. // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  1673. ic= s->c;
  1674. ic.bytestream_start=
  1675. ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
  1676. memcpy(i_state, s->block_state, sizeof(s->block_state));
  1677. if(level!=s->block_max_depth)
  1678. put_rac(&ic, &i_state[4 + s_context], 1);
  1679. put_rac(&ic, &i_state[1 + left->type + top->type], 1);
  1680. put_symbol(&ic, &i_state[32], l-pl , 1);
  1681. put_symbol(&ic, &i_state[64], cb-pcb, 1);
  1682. put_symbol(&ic, &i_state[96], cr-pcr, 1);
  1683. i_len= ic.bytestream - ic.bytestream_start;
  1684. iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
  1685. // assert(score==256*256*256*64-1);
  1686. assert(iscore < 255*255*256 + s->lambda2*10);
  1687. assert(iscore >= 0);
  1688. assert(l>=0 && l<=255);
  1689. assert(pl>=0 && pl<=255);
  1690. if(level==0){
  1691. int varc= iscore >> 8;
  1692. int vard= score >> 8;
  1693. if (vard <= 64 || vard < varc)
  1694. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1695. else
  1696. c->scene_change_score+= s->m.qscale;
  1697. }
  1698. if(level!=s->block_max_depth){
  1699. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  1700. score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  1701. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  1702. score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  1703. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  1704. score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  1705. if(score2 < score && score2 < iscore)
  1706. return score2;
  1707. }
  1708. if(iscore < score){
  1709. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  1710. memcpy(pbbak, i_buffer, i_len);
  1711. s->c= ic;
  1712. s->c.bytestream_start= pbbak_start;
  1713. s->c.bytestream= pbbak + i_len;
  1714. set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
  1715. memcpy(s->block_state, i_state, sizeof(s->block_state));
  1716. return iscore;
  1717. }else{
  1718. memcpy(pbbak, p_buffer, p_len);
  1719. s->c= pc;
  1720. s->c.bytestream_start= pbbak_start;
  1721. s->c.bytestream= pbbak + p_len;
  1722. set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
  1723. memcpy(s->block_state, p_state, sizeof(s->block_state));
  1724. return score;
  1725. }
  1726. }
  1727. static av_always_inline int same_block(BlockNode *a, BlockNode *b){
  1728. if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
  1729. return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
  1730. }else{
  1731. return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
  1732. }
  1733. }
  1734. static void encode_q_branch2(SnowContext *s, int level, int x, int y){
  1735. const int w= s->b_width << s->block_max_depth;
  1736. const int rem_depth= s->block_max_depth - level;
  1737. const int index= (x + y*w) << rem_depth;
  1738. int trx= (x+1)<<rem_depth;
  1739. BlockNode *b= &s->block[index];
  1740. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  1741. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  1742. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1743. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1744. int pl = left->color[0];
  1745. int pcb= left->color[1];
  1746. int pcr= left->color[2];
  1747. int pmx, pmy;
  1748. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  1749. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
  1750. int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
  1751. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1752. if(s->keyframe){
  1753. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  1754. return;
  1755. }
  1756. if(level!=s->block_max_depth){
  1757. if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
  1758. put_rac(&s->c, &s->block_state[4 + s_context], 1);
  1759. }else{
  1760. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  1761. encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
  1762. encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
  1763. encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
  1764. encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
  1765. return;
  1766. }
  1767. }
  1768. if(b->type & BLOCK_INTRA){
  1769. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  1770. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
  1771. put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
  1772. put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
  1773. put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
  1774. set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
  1775. }else{
  1776. pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
  1777. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
  1778. if(s->ref_frames > 1)
  1779. put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
  1780. put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
  1781. put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
  1782. set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
  1783. }
  1784. }
  1785. static void decode_q_branch(SnowContext *s, int level, int x, int y){
  1786. const int w= s->b_width << s->block_max_depth;
  1787. const int rem_depth= s->block_max_depth - level;
  1788. const int index= (x + y*w) << rem_depth;
  1789. int trx= (x+1)<<rem_depth;
  1790. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  1791. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  1792. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1793. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1794. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1795. if(s->keyframe){
  1796. set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, null_block.ref, BLOCK_INTRA);
  1797. return;
  1798. }
  1799. if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
  1800. int type, mx, my;
  1801. int l = left->color[0];
  1802. int cb= left->color[1];
  1803. int cr= left->color[2];
  1804. int ref = 0;
  1805. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  1806. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
  1807. int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
  1808. type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
  1809. if(type){
  1810. pred_mv(s, &mx, &my, 0, left, top, tr);
  1811. l += get_symbol(&s->c, &s->block_state[32], 1);
  1812. cb+= get_symbol(&s->c, &s->block_state[64], 1);
  1813. cr+= get_symbol(&s->c, &s->block_state[96], 1);
  1814. }else{
  1815. if(s->ref_frames > 1)
  1816. ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
  1817. pred_mv(s, &mx, &my, ref, left, top, tr);
  1818. mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
  1819. my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
  1820. }
  1821. set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
  1822. }else{
  1823. decode_q_branch(s, level+1, 2*x+0, 2*y+0);
  1824. decode_q_branch(s, level+1, 2*x+1, 2*y+0);
  1825. decode_q_branch(s, level+1, 2*x+0, 2*y+1);
  1826. decode_q_branch(s, level+1, 2*x+1, 2*y+1);
  1827. }
  1828. }
  1829. static void encode_blocks(SnowContext *s, int search){
  1830. int x, y;
  1831. int w= s->b_width;
  1832. int h= s->b_height;
  1833. if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
  1834. iterative_me(s);
  1835. for(y=0; y<h; y++){
  1836. if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  1837. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  1838. return;
  1839. }
  1840. for(x=0; x<w; x++){
  1841. if(s->avctx->me_method == ME_ITER || !search)
  1842. encode_q_branch2(s, 0, x, y);
  1843. else
  1844. encode_q_branch (s, 0, x, y);
  1845. }
  1846. }
  1847. }
  1848. static void decode_blocks(SnowContext *s){
  1849. int x, y;
  1850. int w= s->b_width;
  1851. int h= s->b_height;
  1852. for(y=0; y<h; y++){
  1853. for(x=0; x<w; x++){
  1854. decode_q_branch(s, 0, x, y);
  1855. }
  1856. }
  1857. }
  1858. static void mc_block(Plane *p, uint8_t *dst, const uint8_t *src, uint8_t *tmp, int stride, int b_w, int b_h, int dx, int dy){
  1859. static const uint8_t weight[64]={
  1860. 8,7,6,5,4,3,2,1,
  1861. 7,7,0,0,0,0,0,1,
  1862. 6,0,6,0,0,0,2,0,
  1863. 5,0,0,5,0,3,0,0,
  1864. 4,0,0,0,4,0,0,0,
  1865. 3,0,0,5,0,3,0,0,
  1866. 2,0,6,0,0,0,2,0,
  1867. 1,7,0,0,0,0,0,1,
  1868. };
  1869. static const uint8_t brane[256]={
  1870. 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x11,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
  1871. 0x04,0x05,0xcc,0xcc,0xcc,0xcc,0xcc,0x41,0x15,0x16,0xcc,0xcc,0xcc,0xcc,0xcc,0x52,
  1872. 0x04,0xcc,0x05,0xcc,0xcc,0xcc,0x41,0xcc,0x15,0xcc,0x16,0xcc,0xcc,0xcc,0x52,0xcc,
  1873. 0x04,0xcc,0xcc,0x05,0xcc,0x41,0xcc,0xcc,0x15,0xcc,0xcc,0x16,0xcc,0x52,0xcc,0xcc,
  1874. 0x04,0xcc,0xcc,0xcc,0x41,0xcc,0xcc,0xcc,0x15,0xcc,0xcc,0xcc,0x16,0xcc,0xcc,0xcc,
  1875. 0x04,0xcc,0xcc,0x41,0xcc,0x05,0xcc,0xcc,0x15,0xcc,0xcc,0x52,0xcc,0x16,0xcc,0xcc,
  1876. 0x04,0xcc,0x41,0xcc,0xcc,0xcc,0x05,0xcc,0x15,0xcc,0x52,0xcc,0xcc,0xcc,0x16,0xcc,
  1877. 0x04,0x41,0xcc,0xcc,0xcc,0xcc,0xcc,0x05,0x15,0x52,0xcc,0xcc,0xcc,0xcc,0xcc,0x16,
  1878. 0x44,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x55,0x56,0x56,0x56,0x56,0x56,0x56,0x56,
  1879. 0x48,0x49,0xcc,0xcc,0xcc,0xcc,0xcc,0x85,0x59,0x5A,0xcc,0xcc,0xcc,0xcc,0xcc,0x96,
  1880. 0x48,0xcc,0x49,0xcc,0xcc,0xcc,0x85,0xcc,0x59,0xcc,0x5A,0xcc,0xcc,0xcc,0x96,0xcc,
  1881. 0x48,0xcc,0xcc,0x49,0xcc,0x85,0xcc,0xcc,0x59,0xcc,0xcc,0x5A,0xcc,0x96,0xcc,0xcc,
  1882. 0x48,0xcc,0xcc,0xcc,0x49,0xcc,0xcc,0xcc,0x59,0xcc,0xcc,0xcc,0x96,0xcc,0xcc,0xcc,
  1883. 0x48,0xcc,0xcc,0x85,0xcc,0x49,0xcc,0xcc,0x59,0xcc,0xcc,0x96,0xcc,0x5A,0xcc,0xcc,
  1884. 0x48,0xcc,0x85,0xcc,0xcc,0xcc,0x49,0xcc,0x59,0xcc,0x96,0xcc,0xcc,0xcc,0x5A,0xcc,
  1885. 0x48,0x85,0xcc,0xcc,0xcc,0xcc,0xcc,0x49,0x59,0x96,0xcc,0xcc,0xcc,0xcc,0xcc,0x5A,
  1886. };
  1887. static const uint8_t needs[16]={
  1888. 0,1,0,0,
  1889. 2,4,2,0,
  1890. 0,1,0,0,
  1891. 15
  1892. };
  1893. int x, y, b, r, l;
  1894. int16_t tmpIt [64*(32+HTAPS_MAX)];
  1895. uint8_t tmp2t[3][stride*(32+HTAPS_MAX)];
  1896. int16_t *tmpI= tmpIt;
  1897. uint8_t *tmp2= tmp2t[0];
  1898. const uint8_t *hpel[11];
  1899. assert(dx<16 && dy<16);
  1900. r= brane[dx + 16*dy]&15;
  1901. l= brane[dx + 16*dy]>>4;
  1902. b= needs[l] | needs[r];
  1903. if(p && !p->diag_mc)
  1904. b= 15;
  1905. if(b&5){
  1906. for(y=0; y < b_h+HTAPS_MAX-1; y++){
  1907. for(x=0; x < b_w; x++){
  1908. int a_1=src[x + HTAPS_MAX/2-4];
  1909. int a0= src[x + HTAPS_MAX/2-3];
  1910. int a1= src[x + HTAPS_MAX/2-2];
  1911. int a2= src[x + HTAPS_MAX/2-1];
  1912. int a3= src[x + HTAPS_MAX/2+0];
  1913. int a4= src[x + HTAPS_MAX/2+1];
  1914. int a5= src[x + HTAPS_MAX/2+2];
  1915. int a6= src[x + HTAPS_MAX/2+3];
  1916. int am=0;
  1917. if(!p || p->fast_mc){
  1918. am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  1919. tmpI[x]= am;
  1920. am= (am+16)>>5;
  1921. }else{
  1922. am= p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6);
  1923. tmpI[x]= am;
  1924. am= (am+32)>>6;
  1925. }
  1926. if(am&(~255)) am= ~(am>>31);
  1927. tmp2[x]= am;
  1928. }
  1929. tmpI+= 64;
  1930. tmp2+= stride;
  1931. src += stride;
  1932. }
  1933. src -= stride*y;
  1934. }
  1935. src += HTAPS_MAX/2 - 1;
  1936. tmp2= tmp2t[1];
  1937. if(b&2){
  1938. for(y=0; y < b_h; y++){
  1939. for(x=0; x < b_w+1; x++){
  1940. int a_1=src[x + (HTAPS_MAX/2-4)*stride];
  1941. int a0= src[x + (HTAPS_MAX/2-3)*stride];
  1942. int a1= src[x + (HTAPS_MAX/2-2)*stride];
  1943. int a2= src[x + (HTAPS_MAX/2-1)*stride];
  1944. int a3= src[x + (HTAPS_MAX/2+0)*stride];
  1945. int a4= src[x + (HTAPS_MAX/2+1)*stride];
  1946. int a5= src[x + (HTAPS_MAX/2+2)*stride];
  1947. int a6= src[x + (HTAPS_MAX/2+3)*stride];
  1948. int am=0;
  1949. if(!p || p->fast_mc)
  1950. am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 16)>>5;
  1951. else
  1952. am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 32)>>6;
  1953. if(am&(~255)) am= ~(am>>31);
  1954. tmp2[x]= am;
  1955. }
  1956. src += stride;
  1957. tmp2+= stride;
  1958. }
  1959. src -= stride*y;
  1960. }
  1961. src += stride*(HTAPS_MAX/2 - 1);
  1962. tmp2= tmp2t[2];
  1963. tmpI= tmpIt;
  1964. if(b&4){
  1965. for(y=0; y < b_h; y++){
  1966. for(x=0; x < b_w; x++){
  1967. int a_1=tmpI[x + (HTAPS_MAX/2-4)*64];
  1968. int a0= tmpI[x + (HTAPS_MAX/2-3)*64];
  1969. int a1= tmpI[x + (HTAPS_MAX/2-2)*64];
  1970. int a2= tmpI[x + (HTAPS_MAX/2-1)*64];
  1971. int a3= tmpI[x + (HTAPS_MAX/2+0)*64];
  1972. int a4= tmpI[x + (HTAPS_MAX/2+1)*64];
  1973. int a5= tmpI[x + (HTAPS_MAX/2+2)*64];
  1974. int a6= tmpI[x + (HTAPS_MAX/2+3)*64];
  1975. int am=0;
  1976. if(!p || p->fast_mc)
  1977. am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 512)>>10;
  1978. else
  1979. am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 2048)>>12;
  1980. if(am&(~255)) am= ~(am>>31);
  1981. tmp2[x]= am;
  1982. }
  1983. tmpI+= 64;
  1984. tmp2+= stride;
  1985. }
  1986. }
  1987. hpel[ 0]= src;
  1988. hpel[ 1]= tmp2t[0] + stride*(HTAPS_MAX/2-1);
  1989. hpel[ 2]= src + 1;
  1990. hpel[ 4]= tmp2t[1];
  1991. hpel[ 5]= tmp2t[2];
  1992. hpel[ 6]= tmp2t[1] + 1;
  1993. hpel[ 8]= src + stride;
  1994. hpel[ 9]= hpel[1] + stride;
  1995. hpel[10]= hpel[8] + 1;
  1996. if(b==15){
  1997. const uint8_t *src1= hpel[dx/8 + dy/8*4 ];
  1998. const uint8_t *src2= hpel[dx/8 + dy/8*4+1];
  1999. const uint8_t *src3= hpel[dx/8 + dy/8*4+4];
  2000. const uint8_t *src4= hpel[dx/8 + dy/8*4+5];
  2001. dx&=7;
  2002. dy&=7;
  2003. for(y=0; y < b_h; y++){
  2004. for(x=0; x < b_w; x++){
  2005. dst[x]= ((8-dx)*(8-dy)*src1[x] + dx*(8-dy)*src2[x]+
  2006. (8-dx)* dy *src3[x] + dx* dy *src4[x]+32)>>6;
  2007. }
  2008. src1+=stride;
  2009. src2+=stride;
  2010. src3+=stride;
  2011. src4+=stride;
  2012. dst +=stride;
  2013. }
  2014. }else{
  2015. const uint8_t *src1= hpel[l];
  2016. const uint8_t *src2= hpel[r];
  2017. int a= weight[((dx&7) + (8*(dy&7)))];
  2018. int b= 8-a;
  2019. for(y=0; y < b_h; y++){
  2020. for(x=0; x < b_w; x++){
  2021. dst[x]= (a*src1[x] + b*src2[x] + 4)>>3;
  2022. }
  2023. src1+=stride;
  2024. src2+=stride;
  2025. dst +=stride;
  2026. }
  2027. }
  2028. }
  2029. #define mca(dx,dy,b_w)\
  2030. static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, const uint8_t *src, int stride, int h){\
  2031. uint8_t tmp[stride*(b_w+HTAPS_MAX-1)];\
  2032. assert(h==b_w);\
  2033. mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, tmp, stride, b_w, b_w, dx, dy);\
  2034. }
  2035. mca( 0, 0,16)
  2036. mca( 8, 0,16)
  2037. mca( 0, 8,16)
  2038. mca( 8, 8,16)
  2039. mca( 0, 0,8)
  2040. mca( 8, 0,8)
  2041. mca( 0, 8,8)
  2042. mca( 8, 8,8)
  2043. static void pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){
  2044. if(block->type & BLOCK_INTRA){
  2045. int x, y;
  2046. const int color = block->color[plane_index];
  2047. const int color4= color*0x01010101;
  2048. if(b_w==32){
  2049. for(y=0; y < b_h; y++){
  2050. *(uint32_t*)&dst[0 + y*stride]= color4;
  2051. *(uint32_t*)&dst[4 + y*stride]= color4;
  2052. *(uint32_t*)&dst[8 + y*stride]= color4;
  2053. *(uint32_t*)&dst[12+ y*stride]= color4;
  2054. *(uint32_t*)&dst[16+ y*stride]= color4;
  2055. *(uint32_t*)&dst[20+ y*stride]= color4;
  2056. *(uint32_t*)&dst[24+ y*stride]= color4;
  2057. *(uint32_t*)&dst[28+ y*stride]= color4;
  2058. }
  2059. }else if(b_w==16){
  2060. for(y=0; y < b_h; y++){
  2061. *(uint32_t*)&dst[0 + y*stride]= color4;
  2062. *(uint32_t*)&dst[4 + y*stride]= color4;
  2063. *(uint32_t*)&dst[8 + y*stride]= color4;
  2064. *(uint32_t*)&dst[12+ y*stride]= color4;
  2065. }
  2066. }else if(b_w==8){
  2067. for(y=0; y < b_h; y++){
  2068. *(uint32_t*)&dst[0 + y*stride]= color4;
  2069. *(uint32_t*)&dst[4 + y*stride]= color4;
  2070. }
  2071. }else if(b_w==4){
  2072. for(y=0; y < b_h; y++){
  2073. *(uint32_t*)&dst[0 + y*stride]= color4;
  2074. }
  2075. }else{
  2076. for(y=0; y < b_h; y++){
  2077. for(x=0; x < b_w; x++){
  2078. dst[x + y*stride]= color;
  2079. }
  2080. }
  2081. }
  2082. }else{
  2083. uint8_t *src= s->last_picture[block->ref].data[plane_index];
  2084. const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
  2085. int mx= block->mx*scale;
  2086. int my= block->my*scale;
  2087. const int dx= mx&15;
  2088. const int dy= my&15;
  2089. const int tab_index= 3 - (b_w>>2) + (b_w>>4);
  2090. sx += (mx>>4) - (HTAPS_MAX/2-1);
  2091. sy += (my>>4) - (HTAPS_MAX/2-1);
  2092. src += sx + sy*stride;
  2093. if( (unsigned)sx >= w - b_w - (HTAPS_MAX-2)
  2094. || (unsigned)sy >= h - b_h - (HTAPS_MAX-2)){
  2095. ff_emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1, sx, sy, w, h);
  2096. src= tmp + MB_SIZE;
  2097. }
  2098. // assert(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h);
  2099. // assert(!(b_w&(b_w-1)));
  2100. assert(b_w>1 && b_h>1);
  2101. assert((tab_index>=0 && tab_index<4) || b_w==32);
  2102. if((dx&3) || (dy&3) || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h) || (b_w&(b_w-1)) || !s->plane[plane_index].fast_mc )
  2103. mc_block(&s->plane[plane_index], dst, src, tmp, stride, b_w, b_h, dx, dy);
  2104. else if(b_w==32){
  2105. int y;
  2106. for(y=0; y<b_h; y+=16){
  2107. s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
  2108. s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
  2109. }
  2110. }else if(b_w==b_h)
  2111. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
  2112. else if(b_w==2*b_h){
  2113. s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
  2114. s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
  2115. }else{
  2116. assert(2*b_w==b_h);
  2117. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
  2118. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
  2119. }
  2120. }
  2121. }
  2122. void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
  2123. int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8){
  2124. int y, x;
  2125. IDWTELEM * dst;
  2126. for(y=0; y<b_h; y++){
  2127. //FIXME ugly misuse of obmc_stride
  2128. const uint8_t *obmc1= obmc + y*obmc_stride;
  2129. const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  2130. const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  2131. const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2132. dst = slice_buffer_get_line(sb, src_y + y);
  2133. for(x=0; x<b_w; x++){
  2134. int v= obmc1[x] * block[3][x + y*src_stride]
  2135. +obmc2[x] * block[2][x + y*src_stride]
  2136. +obmc3[x] * block[1][x + y*src_stride]
  2137. +obmc4[x] * block[0][x + y*src_stride];
  2138. v <<= 8 - LOG2_OBMC_MAX;
  2139. if(FRAC_BITS != 8){
  2140. v >>= 8 - FRAC_BITS;
  2141. }
  2142. if(add){
  2143. v += dst[x + src_x];
  2144. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  2145. if(v&(~255)) v= ~(v>>31);
  2146. dst8[x + y*src_stride] = v;
  2147. }else{
  2148. dst[x + src_x] -= v;
  2149. }
  2150. }
  2151. }
  2152. }
  2153. //FIXME name cleanup (b_w, block_w, b_width stuff)
  2154. static av_always_inline void add_yblock(SnowContext *s, int sliced, slice_buffer *sb, IDWTELEM *dst, uint8_t *dst8, const uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int offset_dst, int plane_index){
  2155. const int b_width = s->b_width << s->block_max_depth;
  2156. const int b_height= s->b_height << s->block_max_depth;
  2157. const int b_stride= b_width;
  2158. BlockNode *lt= &s->block[b_x + b_y*b_stride];
  2159. BlockNode *rt= lt+1;
  2160. BlockNode *lb= lt+b_stride;
  2161. BlockNode *rb= lb+1;
  2162. uint8_t *block[4];
  2163. int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
  2164. uint8_t *tmp = s->scratchbuf;
  2165. uint8_t *ptmp;
  2166. int x,y;
  2167. if(b_x<0){
  2168. lt= rt;
  2169. lb= rb;
  2170. }else if(b_x + 1 >= b_width){
  2171. rt= lt;
  2172. rb= lb;
  2173. }
  2174. if(b_y<0){
  2175. lt= lb;
  2176. rt= rb;
  2177. }else if(b_y + 1 >= b_height){
  2178. lb= lt;
  2179. rb= rt;
  2180. }
  2181. if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
  2182. obmc -= src_x;
  2183. b_w += src_x;
  2184. if(!sliced && !offset_dst)
  2185. dst -= src_x;
  2186. src_x=0;
  2187. }else if(src_x + b_w > w){
  2188. b_w = w - src_x;
  2189. }
  2190. if(src_y<0){
  2191. obmc -= src_y*obmc_stride;
  2192. b_h += src_y;
  2193. if(!sliced && !offset_dst)
  2194. dst -= src_y*dst_stride;
  2195. src_y=0;
  2196. }else if(src_y + b_h> h){
  2197. b_h = h - src_y;
  2198. }
  2199. if(b_w<=0 || b_h<=0) return;
  2200. assert(src_stride > 2*MB_SIZE + 5);
  2201. if(!sliced && offset_dst)
  2202. dst += src_x + src_y*dst_stride;
  2203. dst8+= src_x + src_y*src_stride;
  2204. // src += src_x + src_y*src_stride;
  2205. ptmp= tmp + 3*tmp_step;
  2206. block[0]= ptmp;
  2207. ptmp+=tmp_step;
  2208. pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  2209. if(same_block(lt, rt)){
  2210. block[1]= block[0];
  2211. }else{
  2212. block[1]= ptmp;
  2213. ptmp+=tmp_step;
  2214. pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  2215. }
  2216. if(same_block(lt, lb)){
  2217. block[2]= block[0];
  2218. }else if(same_block(rt, lb)){
  2219. block[2]= block[1];
  2220. }else{
  2221. block[2]= ptmp;
  2222. ptmp+=tmp_step;
  2223. pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  2224. }
  2225. if(same_block(lt, rb) ){
  2226. block[3]= block[0];
  2227. }else if(same_block(rt, rb)){
  2228. block[3]= block[1];
  2229. }else if(same_block(lb, rb)){
  2230. block[3]= block[2];
  2231. }else{
  2232. block[3]= ptmp;
  2233. pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  2234. }
  2235. #if 0
  2236. for(y=0; y<b_h; y++){
  2237. for(x=0; x<b_w; x++){
  2238. int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
  2239. if(add) dst[x + y*dst_stride] += v;
  2240. else dst[x + y*dst_stride] -= v;
  2241. }
  2242. }
  2243. for(y=0; y<b_h; y++){
  2244. uint8_t *obmc2= obmc + (obmc_stride>>1);
  2245. for(x=0; x<b_w; x++){
  2246. int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
  2247. if(add) dst[x + y*dst_stride] += v;
  2248. else dst[x + y*dst_stride] -= v;
  2249. }
  2250. }
  2251. for(y=0; y<b_h; y++){
  2252. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2253. for(x=0; x<b_w; x++){
  2254. int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
  2255. if(add) dst[x + y*dst_stride] += v;
  2256. else dst[x + y*dst_stride] -= v;
  2257. }
  2258. }
  2259. for(y=0; y<b_h; y++){
  2260. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2261. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2262. for(x=0; x<b_w; x++){
  2263. int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
  2264. if(add) dst[x + y*dst_stride] += v;
  2265. else dst[x + y*dst_stride] -= v;
  2266. }
  2267. }
  2268. #else
  2269. if(sliced){
  2270. s->dsp.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
  2271. }else{
  2272. for(y=0; y<b_h; y++){
  2273. //FIXME ugly misuse of obmc_stride
  2274. const uint8_t *obmc1= obmc + y*obmc_stride;
  2275. const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  2276. const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  2277. const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2278. for(x=0; x<b_w; x++){
  2279. int v= obmc1[x] * block[3][x + y*src_stride]
  2280. +obmc2[x] * block[2][x + y*src_stride]
  2281. +obmc3[x] * block[1][x + y*src_stride]
  2282. +obmc4[x] * block[0][x + y*src_stride];
  2283. v <<= 8 - LOG2_OBMC_MAX;
  2284. if(FRAC_BITS != 8){
  2285. v >>= 8 - FRAC_BITS;
  2286. }
  2287. if(add){
  2288. v += dst[x + y*dst_stride];
  2289. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  2290. if(v&(~255)) v= ~(v>>31);
  2291. dst8[x + y*src_stride] = v;
  2292. }else{
  2293. dst[x + y*dst_stride] -= v;
  2294. }
  2295. }
  2296. }
  2297. }
  2298. #endif /* 0 */
  2299. }
  2300. static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){
  2301. Plane *p= &s->plane[plane_index];
  2302. const int mb_w= s->b_width << s->block_max_depth;
  2303. const int mb_h= s->b_height << s->block_max_depth;
  2304. int x, y, mb_x;
  2305. int block_size = MB_SIZE >> s->block_max_depth;
  2306. int block_w = plane_index ? block_size/2 : block_size;
  2307. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2308. int obmc_stride= plane_index ? block_size : 2*block_size;
  2309. int ref_stride= s->current_picture.linesize[plane_index];
  2310. uint8_t *dst8= s->current_picture.data[plane_index];
  2311. int w= p->width;
  2312. int h= p->height;
  2313. if(s->keyframe || (s->avctx->debug&512)){
  2314. if(mb_y==mb_h)
  2315. return;
  2316. if(add){
  2317. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  2318. // DWTELEM * line = slice_buffer_get_line(sb, y);
  2319. IDWTELEM * line = sb->line[y];
  2320. for(x=0; x<w; x++){
  2321. // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2322. int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2323. v >>= FRAC_BITS;
  2324. if(v&(~255)) v= ~(v>>31);
  2325. dst8[x + y*ref_stride]= v;
  2326. }
  2327. }
  2328. }else{
  2329. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  2330. // DWTELEM * line = slice_buffer_get_line(sb, y);
  2331. IDWTELEM * line = sb->line[y];
  2332. for(x=0; x<w; x++){
  2333. line[x] -= 128 << FRAC_BITS;
  2334. // buf[x + y*w]-= 128<<FRAC_BITS;
  2335. }
  2336. }
  2337. }
  2338. return;
  2339. }
  2340. for(mb_x=0; mb_x<=mb_w; mb_x++){
  2341. add_yblock(s, 1, sb, old_buffer, dst8, obmc,
  2342. block_w*mb_x - block_w/2,
  2343. block_w*mb_y - block_w/2,
  2344. block_w, block_w,
  2345. w, h,
  2346. w, ref_stride, obmc_stride,
  2347. mb_x - 1, mb_y - 1,
  2348. add, 0, plane_index);
  2349. }
  2350. }
  2351. static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
  2352. Plane *p= &s->plane[plane_index];
  2353. const int mb_w= s->b_width << s->block_max_depth;
  2354. const int mb_h= s->b_height << s->block_max_depth;
  2355. int x, y, mb_x;
  2356. int block_size = MB_SIZE >> s->block_max_depth;
  2357. int block_w = plane_index ? block_size/2 : block_size;
  2358. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2359. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2360. int ref_stride= s->current_picture.linesize[plane_index];
  2361. uint8_t *dst8= s->current_picture.data[plane_index];
  2362. int w= p->width;
  2363. int h= p->height;
  2364. if(s->keyframe || (s->avctx->debug&512)){
  2365. if(mb_y==mb_h)
  2366. return;
  2367. if(add){
  2368. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  2369. for(x=0; x<w; x++){
  2370. int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2371. v >>= FRAC_BITS;
  2372. if(v&(~255)) v= ~(v>>31);
  2373. dst8[x + y*ref_stride]= v;
  2374. }
  2375. }
  2376. }else{
  2377. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  2378. for(x=0; x<w; x++){
  2379. buf[x + y*w]-= 128<<FRAC_BITS;
  2380. }
  2381. }
  2382. }
  2383. return;
  2384. }
  2385. for(mb_x=0; mb_x<=mb_w; mb_x++){
  2386. add_yblock(s, 0, NULL, buf, dst8, obmc,
  2387. block_w*mb_x - block_w/2,
  2388. block_w*mb_y - block_w/2,
  2389. block_w, block_w,
  2390. w, h,
  2391. w, ref_stride, obmc_stride,
  2392. mb_x - 1, mb_y - 1,
  2393. add, 1, plane_index);
  2394. }
  2395. }
  2396. static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
  2397. const int mb_h= s->b_height << s->block_max_depth;
  2398. int mb_y;
  2399. for(mb_y=0; mb_y<=mb_h; mb_y++)
  2400. predict_slice(s, buf, plane_index, add, mb_y);
  2401. }
  2402. static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2403. int i, x2, y2;
  2404. Plane *p= &s->plane[plane_index];
  2405. const int block_size = MB_SIZE >> s->block_max_depth;
  2406. const int block_w = plane_index ? block_size/2 : block_size;
  2407. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2408. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2409. const int ref_stride= s->current_picture.linesize[plane_index];
  2410. uint8_t *src= s-> input_picture.data[plane_index];
  2411. IDWTELEM *dst= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
  2412. const int b_stride = s->b_width << s->block_max_depth;
  2413. const int w= p->width;
  2414. const int h= p->height;
  2415. int index= mb_x + mb_y*b_stride;
  2416. BlockNode *b= &s->block[index];
  2417. BlockNode backup= *b;
  2418. int ab=0;
  2419. int aa=0;
  2420. b->type|= BLOCK_INTRA;
  2421. b->color[plane_index]= 0;
  2422. memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
  2423. for(i=0; i<4; i++){
  2424. int mb_x2= mb_x + (i &1) - 1;
  2425. int mb_y2= mb_y + (i>>1) - 1;
  2426. int x= block_w*mb_x2 + block_w/2;
  2427. int y= block_w*mb_y2 + block_w/2;
  2428. add_yblock(s, 0, NULL, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
  2429. x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
  2430. for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){
  2431. for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
  2432. int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride;
  2433. int obmc_v= obmc[index];
  2434. int d;
  2435. if(y<0) obmc_v += obmc[index + block_w*obmc_stride];
  2436. if(x<0) obmc_v += obmc[index + block_w];
  2437. if(y+block_w>h) obmc_v += obmc[index - block_w*obmc_stride];
  2438. if(x+block_w>w) obmc_v += obmc[index - block_w];
  2439. //FIXME precalculate this or simplify it somehow else
  2440. d = -dst[index] + (1<<(FRAC_BITS-1));
  2441. dst[index] = d;
  2442. ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
  2443. aa += obmc_v * obmc_v; //FIXME precalculate this
  2444. }
  2445. }
  2446. }
  2447. *b= backup;
  2448. return av_clip(((ab<<LOG2_OBMC_MAX) + aa/2)/aa, 0, 255); //FIXME we should not need clipping
  2449. }
  2450. static inline int get_block_bits(SnowContext *s, int x, int y, int w){
  2451. const int b_stride = s->b_width << s->block_max_depth;
  2452. const int b_height = s->b_height<< s->block_max_depth;
  2453. int index= x + y*b_stride;
  2454. const BlockNode *b = &s->block[index];
  2455. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2456. const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
  2457. const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
  2458. const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
  2459. int dmx, dmy;
  2460. // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  2461. // int my_context= av_log2(2*FFABS(left->my - top->my));
  2462. if(x<0 || x>=b_stride || y>=b_height)
  2463. return 0;
  2464. /*
  2465. 1 0 0
  2466. 01X 1-2 1
  2467. 001XX 3-6 2-3
  2468. 0001XXX 7-14 4-7
  2469. 00001XXXX 15-30 8-15
  2470. */
  2471. //FIXME try accurate rate
  2472. //FIXME intra and inter predictors if surrounding blocks are not the same type
  2473. if(b->type & BLOCK_INTRA){
  2474. return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
  2475. + av_log2(2*FFABS(left->color[1] - b->color[1]))
  2476. + av_log2(2*FFABS(left->color[2] - b->color[2])));
  2477. }else{
  2478. pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
  2479. dmx-= b->mx;
  2480. dmy-= b->my;
  2481. return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
  2482. + av_log2(2*FFABS(dmy))
  2483. + av_log2(2*b->ref));
  2484. }
  2485. }
  2486. static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){
  2487. Plane *p= &s->plane[plane_index];
  2488. const int block_size = MB_SIZE >> s->block_max_depth;
  2489. const int block_w = plane_index ? block_size/2 : block_size;
  2490. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2491. const int ref_stride= s->current_picture.linesize[plane_index];
  2492. uint8_t *dst= s->current_picture.data[plane_index];
  2493. uint8_t *src= s-> input_picture.data[plane_index];
  2494. IDWTELEM *pred= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
  2495. uint8_t *cur = s->scratchbuf;
  2496. uint8_t tmp[ref_stride*(2*MB_SIZE+HTAPS_MAX-1)];
  2497. const int b_stride = s->b_width << s->block_max_depth;
  2498. const int b_height = s->b_height<< s->block_max_depth;
  2499. const int w= p->width;
  2500. const int h= p->height;
  2501. int distortion;
  2502. int rate= 0;
  2503. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2504. int sx= block_w*mb_x - block_w/2;
  2505. int sy= block_w*mb_y - block_w/2;
  2506. int x0= FFMAX(0,-sx);
  2507. int y0= FFMAX(0,-sy);
  2508. int x1= FFMIN(block_w*2, w-sx);
  2509. int y1= FFMIN(block_w*2, h-sy);
  2510. int i,x,y;
  2511. pred_block(s, cur, tmp, ref_stride, sx, sy, block_w*2, block_w*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h);
  2512. for(y=y0; y<y1; y++){
  2513. const uint8_t *obmc1= obmc_edged + y*obmc_stride;
  2514. const IDWTELEM *pred1 = pred + y*obmc_stride;
  2515. uint8_t *cur1 = cur + y*ref_stride;
  2516. uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  2517. for(x=x0; x<x1; x++){
  2518. #if FRAC_BITS >= LOG2_OBMC_MAX
  2519. int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  2520. #else
  2521. int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
  2522. #endif
  2523. v = (v + pred1[x]) >> FRAC_BITS;
  2524. if(v&(~255)) v= ~(v>>31);
  2525. dst1[x] = v;
  2526. }
  2527. }
  2528. /* copy the regions where obmc[] = (uint8_t)256 */
  2529. if(LOG2_OBMC_MAX == 8
  2530. && (mb_x == 0 || mb_x == b_stride-1)
  2531. && (mb_y == 0 || mb_y == b_height-1)){
  2532. if(mb_x == 0)
  2533. x1 = block_w;
  2534. else
  2535. x0 = block_w;
  2536. if(mb_y == 0)
  2537. y1 = block_w;
  2538. else
  2539. y0 = block_w;
  2540. for(y=y0; y<y1; y++)
  2541. memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  2542. }
  2543. if(block_w==16){
  2544. /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  2545. /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  2546. /* FIXME cmps overlap but do not cover the wavelet's whole support.
  2547. * So improving the score of one block is not strictly guaranteed
  2548. * to improve the score of the whole frame, thus iterative motion
  2549. * estimation does not always converge. */
  2550. if(s->avctx->me_cmp == FF_CMP_W97)
  2551. distortion = w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2552. else if(s->avctx->me_cmp == FF_CMP_W53)
  2553. distortion = w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2554. else{
  2555. distortion = 0;
  2556. for(i=0; i<4; i++){
  2557. int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  2558. distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  2559. }
  2560. }
  2561. }else{
  2562. assert(block_w==8);
  2563. distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  2564. }
  2565. if(plane_index==0){
  2566. for(i=0; i<4; i++){
  2567. /* ..RRr
  2568. * .RXx.
  2569. * rxx..
  2570. */
  2571. rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  2572. }
  2573. if(mb_x == b_stride-2)
  2574. rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  2575. }
  2576. return distortion + rate*penalty_factor;
  2577. }
  2578. static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2579. int i, y2;
  2580. Plane *p= &s->plane[plane_index];
  2581. const int block_size = MB_SIZE >> s->block_max_depth;
  2582. const int block_w = plane_index ? block_size/2 : block_size;
  2583. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2584. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2585. const int ref_stride= s->current_picture.linesize[plane_index];
  2586. uint8_t *dst= s->current_picture.data[plane_index];
  2587. uint8_t *src= s-> input_picture.data[plane_index];
  2588. //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
  2589. // const has only been removed from zero_dst to suppress a warning
  2590. static IDWTELEM zero_dst[4096]; //FIXME
  2591. const int b_stride = s->b_width << s->block_max_depth;
  2592. const int w= p->width;
  2593. const int h= p->height;
  2594. int distortion= 0;
  2595. int rate= 0;
  2596. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2597. for(i=0; i<9; i++){
  2598. int mb_x2= mb_x + (i%3) - 1;
  2599. int mb_y2= mb_y + (i/3) - 1;
  2600. int x= block_w*mb_x2 + block_w/2;
  2601. int y= block_w*mb_y2 + block_w/2;
  2602. add_yblock(s, 0, NULL, zero_dst, dst, obmc,
  2603. x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  2604. //FIXME find a cleaner/simpler way to skip the outside stuff
  2605. for(y2= y; y2<0; y2++)
  2606. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2607. for(y2= h; y2<y+block_w; y2++)
  2608. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2609. if(x<0){
  2610. for(y2= y; y2<y+block_w; y2++)
  2611. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  2612. }
  2613. if(x+block_w > w){
  2614. for(y2= y; y2<y+block_w; y2++)
  2615. memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  2616. }
  2617. assert(block_w== 8 || block_w==16);
  2618. distortion += s->dsp.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_w);
  2619. }
  2620. if(plane_index==0){
  2621. BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  2622. int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  2623. /* ..RRRr
  2624. * .RXXx.
  2625. * .RXXx.
  2626. * rxxx.
  2627. */
  2628. if(merged)
  2629. rate = get_block_bits(s, mb_x, mb_y, 2);
  2630. for(i=merged?4:0; i<9; i++){
  2631. static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  2632. rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  2633. }
  2634. }
  2635. return distortion + rate*penalty_factor;
  2636. }
  2637. static av_always_inline int check_block(SnowContext *s, int mb_x, int mb_y, int p[3], int intra, const uint8_t *obmc_edged, int *best_rd){
  2638. const int b_stride= s->b_width << s->block_max_depth;
  2639. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2640. BlockNode backup= *block;
  2641. int rd, index, value;
  2642. assert(mb_x>=0 && mb_y>=0);
  2643. assert(mb_x<b_stride);
  2644. if(intra){
  2645. block->color[0] = p[0];
  2646. block->color[1] = p[1];
  2647. block->color[2] = p[2];
  2648. block->type |= BLOCK_INTRA;
  2649. }else{
  2650. index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  2651. value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  2652. if(s->me_cache[index] == value)
  2653. return 0;
  2654. s->me_cache[index]= value;
  2655. block->mx= p[0];
  2656. block->my= p[1];
  2657. block->type &= ~BLOCK_INTRA;
  2658. }
  2659. rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged);
  2660. //FIXME chroma
  2661. if(rd < *best_rd){
  2662. *best_rd= rd;
  2663. return 1;
  2664. }else{
  2665. *block= backup;
  2666. return 0;
  2667. }
  2668. }
  2669. /* special case for int[2] args we discard afterwards,
  2670. * fixes compilation problem with gcc 2.95 */
  2671. static av_always_inline int check_block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, const uint8_t *obmc_edged, int *best_rd){
  2672. int p[2] = {p0, p1};
  2673. return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  2674. }
  2675. static av_always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, int ref, int *best_rd){
  2676. const int b_stride= s->b_width << s->block_max_depth;
  2677. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2678. BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
  2679. int rd, index, value;
  2680. assert(mb_x>=0 && mb_y>=0);
  2681. assert(mb_x<b_stride);
  2682. assert(((mb_x|mb_y)&1) == 0);
  2683. index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  2684. value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  2685. if(s->me_cache[index] == value)
  2686. return 0;
  2687. s->me_cache[index]= value;
  2688. block->mx= p0;
  2689. block->my= p1;
  2690. block->ref= ref;
  2691. block->type &= ~BLOCK_INTRA;
  2692. block[1]= block[b_stride]= block[b_stride+1]= *block;
  2693. rd= get_4block_rd(s, mb_x, mb_y, 0);
  2694. //FIXME chroma
  2695. if(rd < *best_rd){
  2696. *best_rd= rd;
  2697. return 1;
  2698. }else{
  2699. block[0]= backup[0];
  2700. block[1]= backup[1];
  2701. block[b_stride]= backup[2];
  2702. block[b_stride+1]= backup[3];
  2703. return 0;
  2704. }
  2705. }
  2706. static void iterative_me(SnowContext *s){
  2707. int pass, mb_x, mb_y;
  2708. const int b_width = s->b_width << s->block_max_depth;
  2709. const int b_height= s->b_height << s->block_max_depth;
  2710. const int b_stride= b_width;
  2711. int color[3];
  2712. {
  2713. RangeCoder r = s->c;
  2714. uint8_t state[sizeof(s->block_state)];
  2715. memcpy(state, s->block_state, sizeof(s->block_state));
  2716. for(mb_y= 0; mb_y<s->b_height; mb_y++)
  2717. for(mb_x= 0; mb_x<s->b_width; mb_x++)
  2718. encode_q_branch(s, 0, mb_x, mb_y);
  2719. s->c = r;
  2720. memcpy(s->block_state, state, sizeof(s->block_state));
  2721. }
  2722. for(pass=0; pass<25; pass++){
  2723. int change= 0;
  2724. for(mb_y= 0; mb_y<b_height; mb_y++){
  2725. for(mb_x= 0; mb_x<b_width; mb_x++){
  2726. int dia_change, i, j, ref;
  2727. int best_rd= INT_MAX, ref_rd;
  2728. BlockNode backup, ref_b;
  2729. const int index= mb_x + mb_y * b_stride;
  2730. BlockNode *block= &s->block[index];
  2731. BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  2732. BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  2733. BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  2734. BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  2735. BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  2736. BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  2737. BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  2738. BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  2739. const int b_w= (MB_SIZE >> s->block_max_depth);
  2740. uint8_t obmc_edged[b_w*2][b_w*2];
  2741. if(pass && (block->type & BLOCK_OPT))
  2742. continue;
  2743. block->type |= BLOCK_OPT;
  2744. backup= *block;
  2745. if(!s->me_cache_generation)
  2746. memset(s->me_cache, 0, sizeof(s->me_cache));
  2747. s->me_cache_generation += 1<<22;
  2748. //FIXME precalculate
  2749. {
  2750. int x, y;
  2751. memcpy(obmc_edged, obmc_tab[s->block_max_depth], b_w*b_w*4);
  2752. if(mb_x==0)
  2753. for(y=0; y<b_w*2; y++)
  2754. memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  2755. if(mb_x==b_stride-1)
  2756. for(y=0; y<b_w*2; y++)
  2757. memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  2758. if(mb_y==0){
  2759. for(x=0; x<b_w*2; x++)
  2760. obmc_edged[0][x] += obmc_edged[b_w-1][x];
  2761. for(y=1; y<b_w; y++)
  2762. memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  2763. }
  2764. if(mb_y==b_height-1){
  2765. for(x=0; x<b_w*2; x++)
  2766. obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  2767. for(y=b_w; y<b_w*2-1; y++)
  2768. memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  2769. }
  2770. }
  2771. //skip stuff outside the picture
  2772. if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
  2773. uint8_t *src= s-> input_picture.data[0];
  2774. uint8_t *dst= s->current_picture.data[0];
  2775. const int stride= s->current_picture.linesize[0];
  2776. const int block_w= MB_SIZE >> s->block_max_depth;
  2777. const int sx= block_w*mb_x - block_w/2;
  2778. const int sy= block_w*mb_y - block_w/2;
  2779. const int w= s->plane[0].width;
  2780. const int h= s->plane[0].height;
  2781. int y;
  2782. for(y=sy; y<0; y++)
  2783. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2784. for(y=h; y<sy+block_w*2; y++)
  2785. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2786. if(sx<0){
  2787. for(y=sy; y<sy+block_w*2; y++)
  2788. memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  2789. }
  2790. if(sx+block_w*2 > w){
  2791. for(y=sy; y<sy+block_w*2; y++)
  2792. memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  2793. }
  2794. }
  2795. // intra(black) = neighbors' contribution to the current block
  2796. for(i=0; i<3; i++)
  2797. color[i]= get_dc(s, mb_x, mb_y, i);
  2798. // get previous score (cannot be cached due to OBMC)
  2799. if(pass > 0 && (block->type&BLOCK_INTRA)){
  2800. int color0[3]= {block->color[0], block->color[1], block->color[2]};
  2801. check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
  2802. }else
  2803. check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
  2804. ref_b= *block;
  2805. ref_rd= best_rd;
  2806. for(ref=0; ref < s->ref_frames; ref++){
  2807. int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  2808. if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  2809. continue;
  2810. block->ref= ref;
  2811. best_rd= INT_MAX;
  2812. check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
  2813. check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
  2814. if(tb)
  2815. check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
  2816. if(lb)
  2817. check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
  2818. if(rb)
  2819. check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
  2820. if(bb)
  2821. check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
  2822. /* fullpel ME */
  2823. //FIXME avoid subpel interpolation / round to nearest integer
  2824. do{
  2825. dia_change=0;
  2826. for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
  2827. for(j=0; j<i; j++){
  2828. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  2829. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  2830. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  2831. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  2832. }
  2833. }
  2834. }while(dia_change);
  2835. /* subpel ME */
  2836. do{
  2837. static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  2838. dia_change=0;
  2839. for(i=0; i<8; i++)
  2840. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
  2841. }while(dia_change);
  2842. //FIXME or try the standard 2 pass qpel or similar
  2843. mvr[0][0]= block->mx;
  2844. mvr[0][1]= block->my;
  2845. if(ref_rd > best_rd){
  2846. ref_rd= best_rd;
  2847. ref_b= *block;
  2848. }
  2849. }
  2850. best_rd= ref_rd;
  2851. *block= ref_b;
  2852. #if 1
  2853. check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
  2854. //FIXME RD style color selection
  2855. #endif
  2856. if(!same_block(block, &backup)){
  2857. if(tb ) tb ->type &= ~BLOCK_OPT;
  2858. if(lb ) lb ->type &= ~BLOCK_OPT;
  2859. if(rb ) rb ->type &= ~BLOCK_OPT;
  2860. if(bb ) bb ->type &= ~BLOCK_OPT;
  2861. if(tlb) tlb->type &= ~BLOCK_OPT;
  2862. if(trb) trb->type &= ~BLOCK_OPT;
  2863. if(blb) blb->type &= ~BLOCK_OPT;
  2864. if(brb) brb->type &= ~BLOCK_OPT;
  2865. change ++;
  2866. }
  2867. }
  2868. }
  2869. av_log(NULL, AV_LOG_ERROR, "pass:%d changed:%d\n", pass, change);
  2870. if(!change)
  2871. break;
  2872. }
  2873. if(s->block_max_depth == 1){
  2874. int change= 0;
  2875. for(mb_y= 0; mb_y<b_height; mb_y+=2){
  2876. for(mb_x= 0; mb_x<b_width; mb_x+=2){
  2877. int i;
  2878. int best_rd, init_rd;
  2879. const int index= mb_x + mb_y * b_stride;
  2880. BlockNode *b[4];
  2881. b[0]= &s->block[index];
  2882. b[1]= b[0]+1;
  2883. b[2]= b[0]+b_stride;
  2884. b[3]= b[2]+1;
  2885. if(same_block(b[0], b[1]) &&
  2886. same_block(b[0], b[2]) &&
  2887. same_block(b[0], b[3]))
  2888. continue;
  2889. if(!s->me_cache_generation)
  2890. memset(s->me_cache, 0, sizeof(s->me_cache));
  2891. s->me_cache_generation += 1<<22;
  2892. init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  2893. //FIXME more multiref search?
  2894. check_4block_inter(s, mb_x, mb_y,
  2895. (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  2896. (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  2897. for(i=0; i<4; i++)
  2898. if(!(b[i]->type&BLOCK_INTRA))
  2899. check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  2900. if(init_rd != best_rd)
  2901. change++;
  2902. }
  2903. }
  2904. av_log(NULL, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
  2905. }
  2906. }
  2907. static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
  2908. const int w= b->width;
  2909. const int h= b->height;
  2910. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  2911. const int qmul= qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
  2912. int x,y, thres1, thres2;
  2913. if(s->qlog == LOSSLESS_QLOG){
  2914. for(y=0; y<h; y++)
  2915. for(x=0; x<w; x++)
  2916. dst[x + y*stride]= src[x + y*stride];
  2917. return;
  2918. }
  2919. bias= bias ? 0 : (3*qmul)>>3;
  2920. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  2921. thres2= 2*thres1;
  2922. if(!bias){
  2923. for(y=0; y<h; y++){
  2924. for(x=0; x<w; x++){
  2925. int i= src[x + y*stride];
  2926. if((unsigned)(i+thres1) > thres2){
  2927. if(i>=0){
  2928. i<<= QEXPSHIFT;
  2929. i/= qmul; //FIXME optimize
  2930. dst[x + y*stride]= i;
  2931. }else{
  2932. i= -i;
  2933. i<<= QEXPSHIFT;
  2934. i/= qmul; //FIXME optimize
  2935. dst[x + y*stride]= -i;
  2936. }
  2937. }else
  2938. dst[x + y*stride]= 0;
  2939. }
  2940. }
  2941. }else{
  2942. for(y=0; y<h; y++){
  2943. for(x=0; x<w; x++){
  2944. int i= src[x + y*stride];
  2945. if((unsigned)(i+thres1) > thres2){
  2946. if(i>=0){
  2947. i<<= QEXPSHIFT;
  2948. i= (i + bias) / qmul; //FIXME optimize
  2949. dst[x + y*stride]= i;
  2950. }else{
  2951. i= -i;
  2952. i<<= QEXPSHIFT;
  2953. i= (i + bias) / qmul; //FIXME optimize
  2954. dst[x + y*stride]= -i;
  2955. }
  2956. }else
  2957. dst[x + y*stride]= 0;
  2958. }
  2959. }
  2960. }
  2961. }
  2962. static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int start_y, int end_y){
  2963. const int w= b->width;
  2964. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  2965. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  2966. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  2967. int x,y;
  2968. if(s->qlog == LOSSLESS_QLOG) return;
  2969. for(y=start_y; y<end_y; y++){
  2970. // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  2971. IDWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  2972. for(x=0; x<w; x++){
  2973. int i= line[x];
  2974. if(i<0){
  2975. line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  2976. }else if(i>0){
  2977. line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
  2978. }
  2979. }
  2980. }
  2981. }
  2982. static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
  2983. const int w= b->width;
  2984. const int h= b->height;
  2985. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  2986. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  2987. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  2988. int x,y;
  2989. if(s->qlog == LOSSLESS_QLOG) return;
  2990. for(y=0; y<h; y++){
  2991. for(x=0; x<w; x++){
  2992. int i= src[x + y*stride];
  2993. if(i<0){
  2994. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  2995. }else if(i>0){
  2996. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  2997. }
  2998. }
  2999. }
  3000. }
  3001. static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  3002. const int w= b->width;
  3003. const int h= b->height;
  3004. int x,y;
  3005. for(y=h-1; y>=0; y--){
  3006. for(x=w-1; x>=0; x--){
  3007. int i= x + y*stride;
  3008. if(x){
  3009. if(use_median){
  3010. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  3011. else src[i] -= src[i - 1];
  3012. }else{
  3013. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  3014. else src[i] -= src[i - 1];
  3015. }
  3016. }else{
  3017. if(y) src[i] -= src[i - stride];
  3018. }
  3019. }
  3020. }
  3021. }
  3022. static void correlate_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median, int start_y, int end_y){
  3023. const int w= b->width;
  3024. int x,y;
  3025. IDWTELEM * line=0; // silence silly "could be used without having been initialized" warning
  3026. IDWTELEM * prev;
  3027. if (start_y != 0)
  3028. line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  3029. for(y=start_y; y<end_y; y++){
  3030. prev = line;
  3031. // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  3032. line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  3033. for(x=0; x<w; x++){
  3034. if(x){
  3035. if(use_median){
  3036. if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
  3037. else line[x] += line[x - 1];
  3038. }else{
  3039. if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
  3040. else line[x] += line[x - 1];
  3041. }
  3042. }else{
  3043. if(y) line[x] += prev[x];
  3044. }
  3045. }
  3046. }
  3047. }
  3048. static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  3049. const int w= b->width;
  3050. const int h= b->height;
  3051. int x,y;
  3052. for(y=0; y<h; y++){
  3053. for(x=0; x<w; x++){
  3054. int i= x + y*stride;
  3055. if(x){
  3056. if(use_median){
  3057. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  3058. else src[i] += src[i - 1];
  3059. }else{
  3060. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  3061. else src[i] += src[i - 1];
  3062. }
  3063. }else{
  3064. if(y) src[i] += src[i - stride];
  3065. }
  3066. }
  3067. }
  3068. }
  3069. static void encode_qlogs(SnowContext *s){
  3070. int plane_index, level, orientation;
  3071. for(plane_index=0; plane_index<2; plane_index++){
  3072. for(level=0; level<s->spatial_decomposition_count; level++){
  3073. for(orientation=level ? 1:0; orientation<4; orientation++){
  3074. if(orientation==2) continue;
  3075. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  3076. }
  3077. }
  3078. }
  3079. }
  3080. static void encode_header(SnowContext *s){
  3081. int plane_index, i;
  3082. uint8_t kstate[32];
  3083. memset(kstate, MID_STATE, sizeof(kstate));
  3084. put_rac(&s->c, kstate, s->keyframe);
  3085. if(s->keyframe || s->always_reset){
  3086. reset_contexts(s);
  3087. s->last_spatial_decomposition_type=
  3088. s->last_qlog=
  3089. s->last_qbias=
  3090. s->last_mv_scale=
  3091. s->last_block_max_depth= 0;
  3092. for(plane_index=0; plane_index<2; plane_index++){
  3093. Plane *p= &s->plane[plane_index];
  3094. p->last_htaps=0;
  3095. p->last_diag_mc=0;
  3096. memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
  3097. }
  3098. }
  3099. if(s->keyframe){
  3100. put_symbol(&s->c, s->header_state, s->version, 0);
  3101. put_rac(&s->c, s->header_state, s->always_reset);
  3102. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  3103. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  3104. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  3105. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  3106. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  3107. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  3108. put_rac(&s->c, s->header_state, s->spatial_scalability);
  3109. // put_rac(&s->c, s->header_state, s->rate_scalability);
  3110. put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
  3111. encode_qlogs(s);
  3112. }
  3113. if(!s->keyframe){
  3114. int update_mc=0;
  3115. for(plane_index=0; plane_index<2; plane_index++){
  3116. Plane *p= &s->plane[plane_index];
  3117. update_mc |= p->last_htaps != p->htaps;
  3118. update_mc |= p->last_diag_mc != p->diag_mc;
  3119. update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  3120. }
  3121. put_rac(&s->c, s->header_state, update_mc);
  3122. if(update_mc){
  3123. for(plane_index=0; plane_index<2; plane_index++){
  3124. Plane *p= &s->plane[plane_index];
  3125. put_rac(&s->c, s->header_state, p->diag_mc);
  3126. put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
  3127. for(i= p->htaps/2; i; i--)
  3128. put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
  3129. }
  3130. }
  3131. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  3132. put_rac(&s->c, s->header_state, 1);
  3133. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  3134. encode_qlogs(s);
  3135. }else
  3136. put_rac(&s->c, s->header_state, 0);
  3137. }
  3138. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
  3139. put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
  3140. put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
  3141. put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
  3142. put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
  3143. }
  3144. static void update_last_header_values(SnowContext *s){
  3145. int plane_index;
  3146. if(!s->keyframe){
  3147. for(plane_index=0; plane_index<2; plane_index++){
  3148. Plane *p= &s->plane[plane_index];
  3149. p->last_diag_mc= p->diag_mc;
  3150. p->last_htaps = p->htaps;
  3151. memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  3152. }
  3153. }
  3154. s->last_spatial_decomposition_type = s->spatial_decomposition_type;
  3155. s->last_qlog = s->qlog;
  3156. s->last_qbias = s->qbias;
  3157. s->last_mv_scale = s->mv_scale;
  3158. s->last_block_max_depth = s->block_max_depth;
  3159. s->last_spatial_decomposition_count = s->spatial_decomposition_count;
  3160. }
  3161. static void decode_qlogs(SnowContext *s){
  3162. int plane_index, level, orientation;
  3163. for(plane_index=0; plane_index<3; plane_index++){
  3164. for(level=0; level<s->spatial_decomposition_count; level++){
  3165. for(orientation=level ? 1:0; orientation<4; orientation++){
  3166. int q;
  3167. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  3168. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  3169. else q= get_symbol(&s->c, s->header_state, 1);
  3170. s->plane[plane_index].band[level][orientation].qlog= q;
  3171. }
  3172. }
  3173. }
  3174. }
  3175. static int decode_header(SnowContext *s){
  3176. int plane_index, tmp;
  3177. uint8_t kstate[32];
  3178. memset(kstate, MID_STATE, sizeof(kstate));
  3179. s->keyframe= get_rac(&s->c, kstate);
  3180. if(s->keyframe || s->always_reset){
  3181. reset_contexts(s);
  3182. s->spatial_decomposition_type=
  3183. s->qlog=
  3184. s->qbias=
  3185. s->mv_scale=
  3186. s->block_max_depth= 0;
  3187. }
  3188. if(s->keyframe){
  3189. s->version= get_symbol(&s->c, s->header_state, 0);
  3190. if(s->version>0){
  3191. av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version);
  3192. return -1;
  3193. }
  3194. s->always_reset= get_rac(&s->c, s->header_state);
  3195. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  3196. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  3197. s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  3198. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  3199. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  3200. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  3201. s->spatial_scalability= get_rac(&s->c, s->header_state);
  3202. // s->rate_scalability= get_rac(&s->c, s->header_state);
  3203. tmp= get_symbol(&s->c, s->header_state, 0)+1;
  3204. if(tmp < 1 || tmp > MAX_REF_FRAMES){
  3205. av_log(s->avctx, AV_LOG_ERROR, "reference frame count is %d\n", tmp);
  3206. return -1;
  3207. }
  3208. s->max_ref_frames= tmp;
  3209. decode_qlogs(s);
  3210. }
  3211. if(!s->keyframe){
  3212. if(get_rac(&s->c, s->header_state)){
  3213. for(plane_index=0; plane_index<2; plane_index++){
  3214. int htaps, i, sum=0;
  3215. Plane *p= &s->plane[plane_index];
  3216. p->diag_mc= get_rac(&s->c, s->header_state);
  3217. htaps= get_symbol(&s->c, s->header_state, 0)*2 + 2;
  3218. if((unsigned)htaps > HTAPS_MAX || htaps==0)
  3219. return -1;
  3220. p->htaps= htaps;
  3221. for(i= htaps/2; i; i--){
  3222. p->hcoeff[i]= get_symbol(&s->c, s->header_state, 0) * (1-2*(i&1));
  3223. sum += p->hcoeff[i];
  3224. }
  3225. p->hcoeff[0]= 32-sum;
  3226. }
  3227. s->plane[2].diag_mc= s->plane[1].diag_mc;
  3228. s->plane[2].htaps = s->plane[1].htaps;
  3229. memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff));
  3230. }
  3231. if(get_rac(&s->c, s->header_state)){
  3232. s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  3233. decode_qlogs(s);
  3234. }
  3235. }
  3236. s->spatial_decomposition_type+= get_symbol(&s->c, s->header_state, 1);
  3237. if(s->spatial_decomposition_type > 1){
  3238. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
  3239. return -1;
  3240. }
  3241. s->qlog += get_symbol(&s->c, s->header_state, 1);
  3242. s->mv_scale += get_symbol(&s->c, s->header_state, 1);
  3243. s->qbias += get_symbol(&s->c, s->header_state, 1);
  3244. s->block_max_depth+= get_symbol(&s->c, s->header_state, 1);
  3245. if(s->block_max_depth > 1 || s->block_max_depth < 0){
  3246. av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large", s->block_max_depth);
  3247. s->block_max_depth= 0;
  3248. return -1;
  3249. }
  3250. return 0;
  3251. }
  3252. static void init_qexp(void){
  3253. int i;
  3254. double v=128;
  3255. for(i=0; i<QROOT; i++){
  3256. qexp[i]= lrintf(v);
  3257. v *= pow(2, 1.0 / QROOT);
  3258. }
  3259. }
  3260. static av_cold int common_init(AVCodecContext *avctx){
  3261. SnowContext *s = avctx->priv_data;
  3262. int width, height;
  3263. int i, j;
  3264. s->avctx= avctx;
  3265. s->max_ref_frames=1; //just make sure its not an invalid value in case of no initial keyframe
  3266. dsputil_init(&s->dsp, avctx);
  3267. #define mcf(dx,dy)\
  3268. s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  3269. s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  3270. s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
  3271. s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
  3272. s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
  3273. s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
  3274. mcf( 0, 0)
  3275. mcf( 4, 0)
  3276. mcf( 8, 0)
  3277. mcf(12, 0)
  3278. mcf( 0, 4)
  3279. mcf( 4, 4)
  3280. mcf( 8, 4)
  3281. mcf(12, 4)
  3282. mcf( 0, 8)
  3283. mcf( 4, 8)
  3284. mcf( 8, 8)
  3285. mcf(12, 8)
  3286. mcf( 0,12)
  3287. mcf( 4,12)
  3288. mcf( 8,12)
  3289. mcf(12,12)
  3290. #define mcfh(dx,dy)\
  3291. s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
  3292. s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  3293. mc_block_hpel ## dx ## dy ## 16;\
  3294. s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
  3295. s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
  3296. mc_block_hpel ## dx ## dy ## 8;
  3297. mcfh(0, 0)
  3298. mcfh(8, 0)
  3299. mcfh(0, 8)
  3300. mcfh(8, 8)
  3301. if(!qexp[0])
  3302. init_qexp();
  3303. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  3304. width= s->avctx->width;
  3305. height= s->avctx->height;
  3306. s->spatial_idwt_buffer= av_mallocz(width*height*sizeof(IDWTELEM));
  3307. s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM)); //FIXME this does not belong here
  3308. for(i=0; i<MAX_REF_FRAMES; i++)
  3309. for(j=0; j<MAX_REF_FRAMES; j++)
  3310. scale_mv_ref[i][j] = 256*(i+1)/(j+1);
  3311. s->avctx->get_buffer(s->avctx, &s->mconly_picture);
  3312. s->scratchbuf = av_malloc(s->mconly_picture.linesize[0]*7*MB_SIZE);
  3313. return 0;
  3314. }
  3315. static int common_init_after_header(AVCodecContext *avctx){
  3316. SnowContext *s = avctx->priv_data;
  3317. int plane_index, level, orientation;
  3318. for(plane_index=0; plane_index<3; plane_index++){
  3319. int w= s->avctx->width;
  3320. int h= s->avctx->height;
  3321. if(plane_index){
  3322. w>>= s->chroma_h_shift;
  3323. h>>= s->chroma_v_shift;
  3324. }
  3325. s->plane[plane_index].width = w;
  3326. s->plane[plane_index].height= h;
  3327. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  3328. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3329. SubBand *b= &s->plane[plane_index].band[level][orientation];
  3330. b->buf= s->spatial_dwt_buffer;
  3331. b->level= level;
  3332. b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
  3333. b->width = (w + !(orientation&1))>>1;
  3334. b->height= (h + !(orientation>1))>>1;
  3335. b->stride_line = 1 << (s->spatial_decomposition_count - level);
  3336. b->buf_x_offset = 0;
  3337. b->buf_y_offset = 0;
  3338. if(orientation&1){
  3339. b->buf += (w+1)>>1;
  3340. b->buf_x_offset = (w+1)>>1;
  3341. }
  3342. if(orientation>1){
  3343. b->buf += b->stride>>1;
  3344. b->buf_y_offset = b->stride_line >> 1;
  3345. }
  3346. b->ibuf= s->spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
  3347. if(level)
  3348. b->parent= &s->plane[plane_index].band[level-1][orientation];
  3349. //FIXME avoid this realloc
  3350. av_freep(&b->x_coeff);
  3351. b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
  3352. }
  3353. w= (w+1)>>1;
  3354. h= (h+1)>>1;
  3355. }
  3356. }
  3357. return 0;
  3358. }
  3359. static int qscale2qlog(int qscale){
  3360. return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
  3361. + 61*QROOT/8; //<64 >60
  3362. }
  3363. static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
  3364. {
  3365. /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
  3366. * FIXME we know exact mv bits at this point,
  3367. * but ratecontrol isn't set up to include them. */
  3368. uint32_t coef_sum= 0;
  3369. int level, orientation, delta_qlog;
  3370. for(level=0; level<s->spatial_decomposition_count; level++){
  3371. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3372. SubBand *b= &s->plane[0].band[level][orientation];
  3373. IDWTELEM *buf= b->ibuf;
  3374. const int w= b->width;
  3375. const int h= b->height;
  3376. const int stride= b->stride;
  3377. const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
  3378. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3379. const int qdiv= (1<<16)/qmul;
  3380. int x, y;
  3381. //FIXME this is ugly
  3382. for(y=0; y<h; y++)
  3383. for(x=0; x<w; x++)
  3384. buf[x+y*stride]= b->buf[x+y*stride];
  3385. if(orientation==0)
  3386. decorrelate(s, b, buf, stride, 1, 0);
  3387. for(y=0; y<h; y++)
  3388. for(x=0; x<w; x++)
  3389. coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
  3390. }
  3391. }
  3392. /* ugly, ratecontrol just takes a sqrt again */
  3393. coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
  3394. assert(coef_sum < INT_MAX);
  3395. if(pict->pict_type == FF_I_TYPE){
  3396. s->m.current_picture.mb_var_sum= coef_sum;
  3397. s->m.current_picture.mc_mb_var_sum= 0;
  3398. }else{
  3399. s->m.current_picture.mc_mb_var_sum= coef_sum;
  3400. s->m.current_picture.mb_var_sum= 0;
  3401. }
  3402. pict->quality= ff_rate_estimate_qscale(&s->m, 1);
  3403. if (pict->quality < 0)
  3404. return INT_MIN;
  3405. s->lambda= pict->quality * 3/2;
  3406. delta_qlog= qscale2qlog(pict->quality) - s->qlog;
  3407. s->qlog+= delta_qlog;
  3408. return delta_qlog;
  3409. }
  3410. static void calculate_visual_weight(SnowContext *s, Plane *p){
  3411. int width = p->width;
  3412. int height= p->height;
  3413. int level, orientation, x, y;
  3414. for(level=0; level<s->spatial_decomposition_count; level++){
  3415. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3416. SubBand *b= &p->band[level][orientation];
  3417. IDWTELEM *ibuf= b->ibuf;
  3418. int64_t error=0;
  3419. memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
  3420. ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
  3421. ff_spatial_idwt(s->spatial_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3422. for(y=0; y<height; y++){
  3423. for(x=0; x<width; x++){
  3424. int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
  3425. error += d*d;
  3426. }
  3427. }
  3428. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  3429. }
  3430. }
  3431. }
  3432. #define QUANTIZE2 0
  3433. #if QUANTIZE2==1
  3434. #define Q2_STEP 8
  3435. static void find_sse(SnowContext *s, Plane *p, int *score, int score_stride, IDWTELEM *r0, IDWTELEM *r1, int level, int orientation){
  3436. SubBand *b= &p->band[level][orientation];
  3437. int x, y;
  3438. int xo=0;
  3439. int yo=0;
  3440. int step= 1 << (s->spatial_decomposition_count - level);
  3441. if(orientation&1)
  3442. xo= step>>1;
  3443. if(orientation&2)
  3444. yo= step>>1;
  3445. //FIXME bias for nonzero ?
  3446. //FIXME optimize
  3447. memset(score, 0, sizeof(*score)*score_stride*((p->height + Q2_STEP-1)/Q2_STEP));
  3448. for(y=0; y<p->height; y++){
  3449. for(x=0; x<p->width; x++){
  3450. int sx= (x-xo + step/2) / step / Q2_STEP;
  3451. int sy= (y-yo + step/2) / step / Q2_STEP;
  3452. int v= r0[x + y*p->width] - r1[x + y*p->width];
  3453. assert(sx>=0 && sy>=0 && sx < score_stride);
  3454. v= ((v+8)>>4)<<4;
  3455. score[sx + sy*score_stride] += v*v;
  3456. assert(score[sx + sy*score_stride] >= 0);
  3457. }
  3458. }
  3459. }
  3460. static void dequantize_all(SnowContext *s, Plane *p, IDWTELEM *buffer, int width, int height){
  3461. int level, orientation;
  3462. for(level=0; level<s->spatial_decomposition_count; level++){
  3463. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3464. SubBand *b= &p->band[level][orientation];
  3465. IDWTELEM *dst= buffer + (b->ibuf - s->spatial_idwt_buffer);
  3466. dequantize(s, b, dst, b->stride);
  3467. }
  3468. }
  3469. }
  3470. static void dwt_quantize(SnowContext *s, Plane *p, DWTELEM *buffer, int width, int height, int stride, int type){
  3471. int level, orientation, ys, xs, x, y, pass;
  3472. IDWTELEM best_dequant[height * stride];
  3473. IDWTELEM idwt2_buffer[height * stride];
  3474. const int score_stride= (width + 10)/Q2_STEP;
  3475. int best_score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP]; //FIXME size
  3476. int score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP]; //FIXME size
  3477. int threshold= (s->m.lambda * s->m.lambda) >> 6;
  3478. //FIXME pass the copy cleanly ?
  3479. // memcpy(dwt_buffer, buffer, height * stride * sizeof(DWTELEM));
  3480. ff_spatial_dwt(buffer, width, height, stride, type, s->spatial_decomposition_count);
  3481. for(level=0; level<s->spatial_decomposition_count; level++){
  3482. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3483. SubBand *b= &p->band[level][orientation];
  3484. IDWTELEM *dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
  3485. DWTELEM *src= buffer + (b-> buf - s->spatial_dwt_buffer);
  3486. assert(src == b->buf); // code does not depend on this but it is true currently
  3487. quantize(s, b, dst, src, b->stride, s->qbias);
  3488. }
  3489. }
  3490. for(pass=0; pass<1; pass++){
  3491. if(s->qbias == 0) //keyframe
  3492. continue;
  3493. for(level=0; level<s->spatial_decomposition_count; level++){
  3494. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3495. SubBand *b= &p->band[level][orientation];
  3496. IDWTELEM *dst= idwt2_buffer + (b->ibuf - s->spatial_idwt_buffer);
  3497. IDWTELEM *best_dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
  3498. for(ys= 0; ys<Q2_STEP; ys++){
  3499. for(xs= 0; xs<Q2_STEP; xs++){
  3500. memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
  3501. dequantize_all(s, p, idwt2_buffer, width, height);
  3502. ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
  3503. find_sse(s, p, best_score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
  3504. memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
  3505. for(y=ys; y<b->height; y+= Q2_STEP){
  3506. for(x=xs; x<b->width; x+= Q2_STEP){
  3507. if(dst[x + y*b->stride]<0) dst[x + y*b->stride]++;
  3508. if(dst[x + y*b->stride]>0) dst[x + y*b->stride]--;
  3509. //FIXME try more than just --
  3510. }
  3511. }
  3512. dequantize_all(s, p, idwt2_buffer, width, height);
  3513. ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
  3514. find_sse(s, p, score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
  3515. for(y=ys; y<b->height; y+= Q2_STEP){
  3516. for(x=xs; x<b->width; x+= Q2_STEP){
  3517. int score_idx= x/Q2_STEP + (y/Q2_STEP)*score_stride;
  3518. if(score[score_idx] <= best_score[score_idx] + threshold){
  3519. best_score[score_idx]= score[score_idx];
  3520. if(best_dst[x + y*b->stride]<0) best_dst[x + y*b->stride]++;
  3521. if(best_dst[x + y*b->stride]>0) best_dst[x + y*b->stride]--;
  3522. //FIXME copy instead
  3523. }
  3524. }
  3525. }
  3526. }
  3527. }
  3528. }
  3529. }
  3530. }
  3531. memcpy(s->spatial_idwt_buffer, best_dequant, height * stride * sizeof(IDWTELEM)); //FIXME work with that directly instead of copy at the end
  3532. }
  3533. #endif /* QUANTIZE2==1 */
  3534. static av_cold int encode_init(AVCodecContext *avctx)
  3535. {
  3536. SnowContext *s = avctx->priv_data;
  3537. int plane_index;
  3538. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  3539. av_log(avctx, AV_LOG_ERROR, "This codec is under development, files encoded with it may not be decodable with future versions!!!\n"
  3540. "Use vstrict=-2 / -strict -2 to use it anyway.\n");
  3541. return -1;
  3542. }
  3543. if(avctx->prediction_method == DWT_97
  3544. && (avctx->flags & CODEC_FLAG_QSCALE)
  3545. && avctx->global_quality == 0){
  3546. av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
  3547. return -1;
  3548. }
  3549. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  3550. s->chroma_h_shift= 1; //FIXME XXX
  3551. s->chroma_v_shift= 1;
  3552. s->mv_scale = (avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  3553. s->block_max_depth= (avctx->flags & CODEC_FLAG_4MV ) ? 1 : 0;
  3554. for(plane_index=0; plane_index<3; plane_index++){
  3555. s->plane[plane_index].diag_mc= 1;
  3556. s->plane[plane_index].htaps= 6;
  3557. s->plane[plane_index].hcoeff[0]= 40;
  3558. s->plane[plane_index].hcoeff[1]= -10;
  3559. s->plane[plane_index].hcoeff[2]= 2;
  3560. s->plane[plane_index].fast_mc= 1;
  3561. }
  3562. common_init(avctx);
  3563. alloc_blocks(s);
  3564. s->version=0;
  3565. s->m.avctx = avctx;
  3566. s->m.flags = avctx->flags;
  3567. s->m.bit_rate= avctx->bit_rate;
  3568. s->m.me.temp =
  3569. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  3570. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  3571. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  3572. s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
  3573. h263_encode_init(&s->m); //mv_penalty
  3574. s->max_ref_frames = FFMAX(FFMIN(avctx->refs, MAX_REF_FRAMES), 1);
  3575. if(avctx->flags&CODEC_FLAG_PASS1){
  3576. if(!avctx->stats_out)
  3577. avctx->stats_out = av_mallocz(256);
  3578. }
  3579. if((avctx->flags&CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
  3580. if(ff_rate_control_init(&s->m) < 0)
  3581. return -1;
  3582. }
  3583. s->pass1_rc= !(avctx->flags & (CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
  3584. avctx->coded_frame= &s->current_picture;
  3585. switch(avctx->pix_fmt){
  3586. // case PIX_FMT_YUV444P:
  3587. // case PIX_FMT_YUV422P:
  3588. case PIX_FMT_YUV420P:
  3589. case PIX_FMT_GRAY8:
  3590. // case PIX_FMT_YUV411P:
  3591. // case PIX_FMT_YUV410P:
  3592. s->colorspace_type= 0;
  3593. break;
  3594. /* case PIX_FMT_RGB32:
  3595. s->colorspace= 1;
  3596. break;*/
  3597. default:
  3598. av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
  3599. return -1;
  3600. }
  3601. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  3602. s->chroma_h_shift= 1;
  3603. s->chroma_v_shift= 1;
  3604. ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
  3605. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
  3606. s->avctx->get_buffer(s->avctx, &s->input_picture);
  3607. if(s->avctx->me_method == ME_ITER){
  3608. int i;
  3609. int size= s->b_width * s->b_height << 2*s->block_max_depth;
  3610. for(i=0; i<s->max_ref_frames; i++){
  3611. s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
  3612. s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
  3613. }
  3614. }
  3615. return 0;
  3616. }
  3617. #define USE_HALFPEL_PLANE 0
  3618. static void halfpel_interpol(SnowContext *s, uint8_t *halfpel[4][4], AVFrame *frame){
  3619. int p,x,y;
  3620. assert(!(s->avctx->flags & CODEC_FLAG_EMU_EDGE));
  3621. for(p=0; p<3; p++){
  3622. int is_chroma= !!p;
  3623. int w= s->avctx->width >>is_chroma;
  3624. int h= s->avctx->height >>is_chroma;
  3625. int ls= frame->linesize[p];
  3626. uint8_t *src= frame->data[p];
  3627. halfpel[1][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls);
  3628. halfpel[2][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls);
  3629. halfpel[3][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls);
  3630. halfpel[0][p]= src;
  3631. for(y=0; y<h; y++){
  3632. for(x=0; x<w; x++){
  3633. int i= y*ls + x;
  3634. halfpel[1][p][i]= (20*(src[i] + src[i+1]) - 5*(src[i-1] + src[i+2]) + (src[i-2] + src[i+3]) + 16 )>>5;
  3635. }
  3636. }
  3637. for(y=0; y<h; y++){
  3638. for(x=0; x<w; x++){
  3639. int i= y*ls + x;
  3640. halfpel[2][p][i]= (20*(src[i] + src[i+ls]) - 5*(src[i-ls] + src[i+2*ls]) + (src[i-2*ls] + src[i+3*ls]) + 16 )>>5;
  3641. }
  3642. }
  3643. src= halfpel[1][p];
  3644. for(y=0; y<h; y++){
  3645. for(x=0; x<w; x++){
  3646. int i= y*ls + x;
  3647. halfpel[3][p][i]= (20*(src[i] + src[i+ls]) - 5*(src[i-ls] + src[i+2*ls]) + (src[i-2*ls] + src[i+3*ls]) + 16 )>>5;
  3648. }
  3649. }
  3650. //FIXME border!
  3651. }
  3652. }
  3653. static int frame_start(SnowContext *s){
  3654. AVFrame tmp;
  3655. int w= s->avctx->width; //FIXME round up to x16 ?
  3656. int h= s->avctx->height;
  3657. if(s->current_picture.data[0]){
  3658. s->dsp.draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
  3659. s->dsp.draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
  3660. s->dsp.draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
  3661. }
  3662. tmp= s->last_picture[s->max_ref_frames-1];
  3663. memmove(s->last_picture+1, s->last_picture, (s->max_ref_frames-1)*sizeof(AVFrame));
  3664. memmove(s->halfpel_plane+1, s->halfpel_plane, (s->max_ref_frames-1)*sizeof(void*)*4*4);
  3665. if(USE_HALFPEL_PLANE && s->current_picture.data[0])
  3666. halfpel_interpol(s, s->halfpel_plane[0], &s->current_picture);
  3667. s->last_picture[0]= s->current_picture;
  3668. s->current_picture= tmp;
  3669. if(s->keyframe){
  3670. s->ref_frames= 0;
  3671. }else{
  3672. int i;
  3673. for(i=0; i<s->max_ref_frames && s->last_picture[i].data[0]; i++)
  3674. if(i && s->last_picture[i-1].key_frame)
  3675. break;
  3676. s->ref_frames= i;
  3677. }
  3678. s->current_picture.reference= 1;
  3679. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  3680. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  3681. return -1;
  3682. }
  3683. s->current_picture.key_frame= s->keyframe;
  3684. return 0;
  3685. }
  3686. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  3687. SnowContext *s = avctx->priv_data;
  3688. RangeCoder * const c= &s->c;
  3689. AVFrame *pict = data;
  3690. const int width= s->avctx->width;
  3691. const int height= s->avctx->height;
  3692. int level, orientation, plane_index, i, y;
  3693. uint8_t rc_header_bak[sizeof(s->header_state)];
  3694. uint8_t rc_block_bak[sizeof(s->block_state)];
  3695. ff_init_range_encoder(c, buf, buf_size);
  3696. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3697. for(i=0; i<3; i++){
  3698. int shift= !!i;
  3699. for(y=0; y<(height>>shift); y++)
  3700. memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
  3701. &pict->data[i][y * pict->linesize[i]],
  3702. width>>shift);
  3703. }
  3704. s->new_picture = *pict;
  3705. s->m.picture_number= avctx->frame_number;
  3706. if(avctx->flags&CODEC_FLAG_PASS2){
  3707. s->m.pict_type =
  3708. pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
  3709. s->keyframe= pict->pict_type==FF_I_TYPE;
  3710. if(!(avctx->flags&CODEC_FLAG_QSCALE)) {
  3711. pict->quality= ff_rate_estimate_qscale(&s->m, 0);
  3712. if (pict->quality < 0)
  3713. return -1;
  3714. }
  3715. }else{
  3716. s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  3717. s->m.pict_type=
  3718. pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  3719. }
  3720. if(s->pass1_rc && avctx->frame_number == 0)
  3721. pict->quality= 2*FF_QP2LAMBDA;
  3722. if(pict->quality){
  3723. s->qlog= qscale2qlog(pict->quality);
  3724. s->lambda = pict->quality * 3/2;
  3725. }
  3726. if(s->qlog < 0 || (!pict->quality && (avctx->flags & CODEC_FLAG_QSCALE))){
  3727. s->qlog= LOSSLESS_QLOG;
  3728. s->lambda = 0;
  3729. }//else keep previous frame's qlog until after motion estimation
  3730. frame_start(s);
  3731. s->m.current_picture_ptr= &s->m.current_picture;
  3732. if(pict->pict_type == FF_P_TYPE){
  3733. int block_width = (width +15)>>4;
  3734. int block_height= (height+15)>>4;
  3735. int stride= s->current_picture.linesize[0];
  3736. assert(s->current_picture.data[0]);
  3737. assert(s->last_picture[0].data[0]);
  3738. s->m.avctx= s->avctx;
  3739. s->m.current_picture.data[0]= s->current_picture.data[0];
  3740. s->m. last_picture.data[0]= s->last_picture[0].data[0];
  3741. s->m. new_picture.data[0]= s-> input_picture.data[0];
  3742. s->m. last_picture_ptr= &s->m. last_picture;
  3743. s->m.linesize=
  3744. s->m. last_picture.linesize[0]=
  3745. s->m. new_picture.linesize[0]=
  3746. s->m.current_picture.linesize[0]= stride;
  3747. s->m.uvlinesize= s->current_picture.linesize[1];
  3748. s->m.width = width;
  3749. s->m.height= height;
  3750. s->m.mb_width = block_width;
  3751. s->m.mb_height= block_height;
  3752. s->m.mb_stride= s->m.mb_width+1;
  3753. s->m.b8_stride= 2*s->m.mb_width+1;
  3754. s->m.f_code=1;
  3755. s->m.pict_type= pict->pict_type;
  3756. s->m.me_method= s->avctx->me_method;
  3757. s->m.me.scene_change_score=0;
  3758. s->m.flags= s->avctx->flags;
  3759. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  3760. s->m.out_format= FMT_H263;
  3761. s->m.unrestricted_mv= 1;
  3762. s->m.lambda = s->lambda;
  3763. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  3764. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  3765. s->m.dsp= s->dsp; //move
  3766. ff_init_me(&s->m);
  3767. s->dsp= s->m.dsp;
  3768. }
  3769. if(s->pass1_rc){
  3770. memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
  3771. memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
  3772. }
  3773. redo_frame:
  3774. if(pict->pict_type == FF_I_TYPE)
  3775. s->spatial_decomposition_count= 5;
  3776. else
  3777. s->spatial_decomposition_count= 5;
  3778. s->m.pict_type = pict->pict_type;
  3779. s->qbias= pict->pict_type == FF_P_TYPE ? 2 : 0;
  3780. common_init_after_header(avctx);
  3781. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  3782. for(plane_index=0; plane_index<3; plane_index++){
  3783. calculate_visual_weight(s, &s->plane[plane_index]);
  3784. }
  3785. }
  3786. encode_header(s);
  3787. s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3788. encode_blocks(s, 1);
  3789. s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
  3790. for(plane_index=0; plane_index<3; plane_index++){
  3791. Plane *p= &s->plane[plane_index];
  3792. int w= p->width;
  3793. int h= p->height;
  3794. int x, y;
  3795. // int bits= put_bits_count(&s->c.pb);
  3796. if(!(avctx->flags2 & CODEC_FLAG2_MEMC_ONLY)){
  3797. //FIXME optimize
  3798. if(pict->data[plane_index]) //FIXME gray hack
  3799. for(y=0; y<h; y++){
  3800. for(x=0; x<w; x++){
  3801. s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  3802. }
  3803. }
  3804. predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
  3805. if( plane_index==0
  3806. && pict->pict_type == FF_P_TYPE
  3807. && !(avctx->flags&CODEC_FLAG_PASS2)
  3808. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  3809. ff_init_range_encoder(c, buf, buf_size);
  3810. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3811. pict->pict_type= FF_I_TYPE;
  3812. s->keyframe=1;
  3813. s->current_picture.key_frame=1;
  3814. goto redo_frame;
  3815. }
  3816. if(s->qlog == LOSSLESS_QLOG){
  3817. for(y=0; y<h; y++){
  3818. for(x=0; x<w; x++){
  3819. s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  3820. }
  3821. }
  3822. }else{
  3823. for(y=0; y<h; y++){
  3824. for(x=0; x<w; x++){
  3825. s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
  3826. }
  3827. }
  3828. }
  3829. /* if(QUANTIZE2)
  3830. dwt_quantize(s, p, s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type);
  3831. else*/
  3832. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3833. if(s->pass1_rc && plane_index==0){
  3834. int delta_qlog = ratecontrol_1pass(s, pict);
  3835. if (delta_qlog <= INT_MIN)
  3836. return -1;
  3837. if(delta_qlog){
  3838. //reordering qlog in the bitstream would eliminate this reset
  3839. ff_init_range_encoder(c, buf, buf_size);
  3840. memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
  3841. memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
  3842. encode_header(s);
  3843. encode_blocks(s, 0);
  3844. }
  3845. }
  3846. for(level=0; level<s->spatial_decomposition_count; level++){
  3847. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3848. SubBand *b= &p->band[level][orientation];
  3849. if(!QUANTIZE2)
  3850. quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
  3851. if(orientation==0)
  3852. decorrelate(s, b, b->ibuf, b->stride, pict->pict_type == FF_P_TYPE, 0);
  3853. encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
  3854. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  3855. if(orientation==0)
  3856. correlate(s, b, b->ibuf, b->stride, 1, 0);
  3857. }
  3858. }
  3859. for(level=0; level<s->spatial_decomposition_count; level++){
  3860. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3861. SubBand *b= &p->band[level][orientation];
  3862. dequantize(s, b, b->ibuf, b->stride);
  3863. }
  3864. }
  3865. ff_spatial_idwt(s->spatial_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3866. if(s->qlog == LOSSLESS_QLOG){
  3867. for(y=0; y<h; y++){
  3868. for(x=0; x<w; x++){
  3869. s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
  3870. }
  3871. }
  3872. }
  3873. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  3874. }else{
  3875. //ME/MC only
  3876. if(pict->pict_type == FF_I_TYPE){
  3877. for(y=0; y<h; y++){
  3878. for(x=0; x<w; x++){
  3879. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
  3880. pict->data[plane_index][y*pict->linesize[plane_index] + x];
  3881. }
  3882. }
  3883. }else{
  3884. memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
  3885. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  3886. }
  3887. }
  3888. if(s->avctx->flags&CODEC_FLAG_PSNR){
  3889. int64_t error= 0;
  3890. if(pict->data[plane_index]) //FIXME gray hack
  3891. for(y=0; y<h; y++){
  3892. for(x=0; x<w; x++){
  3893. int d= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];
  3894. error += d*d;
  3895. }
  3896. }
  3897. s->avctx->error[plane_index] += error;
  3898. s->current_picture.error[plane_index] = error;
  3899. }
  3900. }
  3901. update_last_header_values(s);
  3902. if(s->last_picture[s->max_ref_frames-1].data[0]){
  3903. avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
  3904. for(i=0; i<9; i++)
  3905. if(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3])
  3906. av_free(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] - EDGE_WIDTH*(1+s->current_picture.linesize[i%3]));
  3907. }
  3908. s->current_picture.coded_picture_number = avctx->frame_number;
  3909. s->current_picture.pict_type = pict->pict_type;
  3910. s->current_picture.quality = pict->quality;
  3911. s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3912. s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
  3913. s->m.current_picture.display_picture_number =
  3914. s->m.current_picture.coded_picture_number = avctx->frame_number;
  3915. s->m.current_picture.quality = pict->quality;
  3916. s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  3917. if(s->pass1_rc)
  3918. if (ff_rate_estimate_qscale(&s->m, 0) < 0)
  3919. return -1;
  3920. if(avctx->flags&CODEC_FLAG_PASS1)
  3921. ff_write_pass1_stats(&s->m);
  3922. s->m.last_pict_type = s->m.pict_type;
  3923. avctx->frame_bits = s->m.frame_bits;
  3924. avctx->mv_bits = s->m.mv_bits;
  3925. avctx->misc_bits = s->m.misc_bits;
  3926. avctx->p_tex_bits = s->m.p_tex_bits;
  3927. emms_c();
  3928. return ff_rac_terminate(c);
  3929. }
  3930. static av_cold void common_end(SnowContext *s){
  3931. int plane_index, level, orientation, i;
  3932. av_freep(&s->spatial_dwt_buffer);
  3933. av_freep(&s->spatial_idwt_buffer);
  3934. s->m.me.temp= NULL;
  3935. av_freep(&s->m.me.scratchpad);
  3936. av_freep(&s->m.me.map);
  3937. av_freep(&s->m.me.score_map);
  3938. av_freep(&s->m.obmc_scratchpad);
  3939. av_freep(&s->block);
  3940. av_freep(&s->scratchbuf);
  3941. for(i=0; i<MAX_REF_FRAMES; i++){
  3942. av_freep(&s->ref_mvs[i]);
  3943. av_freep(&s->ref_scores[i]);
  3944. if(s->last_picture[i].data[0])
  3945. s->avctx->release_buffer(s->avctx, &s->last_picture[i]);
  3946. }
  3947. for(plane_index=0; plane_index<3; plane_index++){
  3948. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  3949. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3950. SubBand *b= &s->plane[plane_index].band[level][orientation];
  3951. av_freep(&b->x_coeff);
  3952. }
  3953. }
  3954. }
  3955. }
  3956. static av_cold int encode_end(AVCodecContext *avctx)
  3957. {
  3958. SnowContext *s = avctx->priv_data;
  3959. common_end(s);
  3960. av_free(avctx->stats_out);
  3961. return 0;
  3962. }
  3963. static av_cold int decode_init(AVCodecContext *avctx)
  3964. {
  3965. avctx->pix_fmt= PIX_FMT_YUV420P;
  3966. common_init(avctx);
  3967. return 0;
  3968. }
  3969. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size){
  3970. SnowContext *s = avctx->priv_data;
  3971. RangeCoder * const c= &s->c;
  3972. int bytes_read;
  3973. AVFrame *picture = data;
  3974. int level, orientation, plane_index, i;
  3975. ff_init_range_decoder(c, buf, buf_size);
  3976. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3977. s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
  3978. if(decode_header(s)<0)
  3979. return -1;
  3980. common_init_after_header(avctx);
  3981. // realloc slice buffer for the case that spatial_decomposition_count changed
  3982. slice_buffer_destroy(&s->sb);
  3983. slice_buffer_init(&s->sb, s->plane[0].height, (MB_SIZE >> s->block_max_depth) + s->spatial_decomposition_count * 8 + 1, s->plane[0].width, s->spatial_idwt_buffer);
  3984. for(plane_index=0; plane_index<3; plane_index++){
  3985. Plane *p= &s->plane[plane_index];
  3986. p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40
  3987. && p->hcoeff[1]==-10
  3988. && p->hcoeff[2]==2;
  3989. }
  3990. alloc_blocks(s);
  3991. frame_start(s);
  3992. //keyframe flag duplication mess FIXME
  3993. if(avctx->debug&FF_DEBUG_PICT_INFO)
  3994. av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
  3995. decode_blocks(s);
  3996. for(plane_index=0; plane_index<3; plane_index++){
  3997. Plane *p= &s->plane[plane_index];
  3998. int w= p->width;
  3999. int h= p->height;
  4000. int x, y;
  4001. int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
  4002. if(s->avctx->debug&2048){
  4003. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  4004. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  4005. for(y=0; y<h; y++){
  4006. for(x=0; x<w; x++){
  4007. int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
  4008. s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
  4009. }
  4010. }
  4011. }
  4012. {
  4013. for(level=0; level<s->spatial_decomposition_count; level++){
  4014. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  4015. SubBand *b= &p->band[level][orientation];
  4016. unpack_coeffs(s, b, b->parent, orientation);
  4017. }
  4018. }
  4019. }
  4020. {
  4021. const int mb_h= s->b_height << s->block_max_depth;
  4022. const int block_size = MB_SIZE >> s->block_max_depth;
  4023. const int block_w = plane_index ? block_size/2 : block_size;
  4024. int mb_y;
  4025. DWTCompose cs[MAX_DECOMPOSITIONS];
  4026. int yd=0, yq=0;
  4027. int y;
  4028. int end_y;
  4029. ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
  4030. for(mb_y=0; mb_y<=mb_h; mb_y++){
  4031. int slice_starty = block_w*mb_y;
  4032. int slice_h = block_w*(mb_y+1);
  4033. if (!(s->keyframe || s->avctx->debug&512)){
  4034. slice_starty = FFMAX(0, slice_starty - (block_w >> 1));
  4035. slice_h -= (block_w >> 1);
  4036. }
  4037. for(level=0; level<s->spatial_decomposition_count; level++){
  4038. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  4039. SubBand *b= &p->band[level][orientation];
  4040. int start_y;
  4041. int end_y;
  4042. int our_mb_start = mb_y;
  4043. int our_mb_end = (mb_y + 1);
  4044. const int extra= 3;
  4045. start_y = (mb_y ? ((block_w * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
  4046. end_y = (((block_w * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
  4047. if (!(s->keyframe || s->avctx->debug&512)){
  4048. start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  4049. end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  4050. }
  4051. start_y = FFMIN(b->height, start_y);
  4052. end_y = FFMIN(b->height, end_y);
  4053. if (start_y != end_y){
  4054. if (orientation == 0){
  4055. SubBand * correlate_band = &p->band[0][0];
  4056. int correlate_end_y = FFMIN(b->height, end_y + 1);
  4057. int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
  4058. decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
  4059. correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
  4060. dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, start_y, end_y);
  4061. }
  4062. else
  4063. decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
  4064. }
  4065. }
  4066. }
  4067. for(; yd<slice_h; yd+=4){
  4068. ff_spatial_idwt_buffered_slice(&s->dsp, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
  4069. }
  4070. if(s->qlog == LOSSLESS_QLOG){
  4071. for(; yq<slice_h && yq<h; yq++){
  4072. IDWTELEM * line = slice_buffer_get_line(&s->sb, yq);
  4073. for(x=0; x<w; x++){
  4074. line[x] <<= FRAC_BITS;
  4075. }
  4076. }
  4077. }
  4078. predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y);
  4079. y = FFMIN(p->height, slice_starty);
  4080. end_y = FFMIN(p->height, slice_h);
  4081. while(y < end_y)
  4082. slice_buffer_release(&s->sb, y++);
  4083. }
  4084. slice_buffer_flush(&s->sb);
  4085. }
  4086. }
  4087. emms_c();
  4088. if(s->last_picture[s->max_ref_frames-1].data[0]){
  4089. avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
  4090. for(i=0; i<9; i++)
  4091. if(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3])
  4092. av_free(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] - EDGE_WIDTH*(1+s->current_picture.linesize[i%3]));
  4093. }
  4094. if(!(s->avctx->debug&2048))
  4095. *picture= s->current_picture;
  4096. else
  4097. *picture= s->mconly_picture;
  4098. *data_size = sizeof(AVFrame);
  4099. bytes_read= c->bytestream - c->bytestream_start;
  4100. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
  4101. return bytes_read;
  4102. }
  4103. static av_cold int decode_end(AVCodecContext *avctx)
  4104. {
  4105. SnowContext *s = avctx->priv_data;
  4106. slice_buffer_destroy(&s->sb);
  4107. common_end(s);
  4108. return 0;
  4109. }
  4110. AVCodec snow_decoder = {
  4111. "snow",
  4112. CODEC_TYPE_VIDEO,
  4113. CODEC_ID_SNOW,
  4114. sizeof(SnowContext),
  4115. decode_init,
  4116. NULL,
  4117. decode_end,
  4118. decode_frame,
  4119. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  4120. NULL,
  4121. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  4122. };
  4123. #if CONFIG_SNOW_ENCODER
  4124. AVCodec snow_encoder = {
  4125. "snow",
  4126. CODEC_TYPE_VIDEO,
  4127. CODEC_ID_SNOW,
  4128. sizeof(SnowContext),
  4129. encode_init,
  4130. encode_frame,
  4131. encode_end,
  4132. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  4133. };
  4134. #endif
  4135. #ifdef TEST
  4136. #undef malloc
  4137. #undef free
  4138. #undef printf
  4139. #undef random
  4140. int main(void){
  4141. int width=256;
  4142. int height=256;
  4143. int buffer[2][width*height];
  4144. SnowContext s;
  4145. int i;
  4146. s.spatial_decomposition_count=6;
  4147. s.spatial_decomposition_type=1;
  4148. printf("testing 5/3 DWT\n");
  4149. for(i=0; i<width*height; i++)
  4150. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  4151. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4152. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4153. for(i=0; i<width*height; i++)
  4154. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  4155. printf("testing 9/7 DWT\n");
  4156. s.spatial_decomposition_type=0;
  4157. for(i=0; i<width*height; i++)
  4158. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  4159. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4160. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4161. for(i=0; i<width*height; i++)
  4162. if(FFABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  4163. #if 0
  4164. printf("testing AC coder\n");
  4165. memset(s.header_state, 0, sizeof(s.header_state));
  4166. ff_init_range_encoder(&s.c, buffer[0], 256*256);
  4167. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  4168. for(i=-256; i<256; i++){
  4169. put_symbol(&s.c, s.header_state, i*i*i/3*FFABS(i), 1);
  4170. }
  4171. ff_rac_terminate(&s.c);
  4172. memset(s.header_state, 0, sizeof(s.header_state));
  4173. ff_init_range_decoder(&s.c, buffer[0], 256*256);
  4174. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  4175. for(i=-256; i<256; i++){
  4176. int j;
  4177. j= get_symbol(&s.c, s.header_state, 1);
  4178. if(j!=i*i*i/3*FFABS(i)) printf("fsck: %d != %d\n", i, j);
  4179. }
  4180. #endif
  4181. {
  4182. int level, orientation, x, y;
  4183. int64_t errors[8][4];
  4184. int64_t g=0;
  4185. memset(errors, 0, sizeof(errors));
  4186. s.spatial_decomposition_count=3;
  4187. s.spatial_decomposition_type=0;
  4188. for(level=0; level<s.spatial_decomposition_count; level++){
  4189. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  4190. int w= width >> (s.spatial_decomposition_count-level);
  4191. int h= height >> (s.spatial_decomposition_count-level);
  4192. int stride= width << (s.spatial_decomposition_count-level);
  4193. DWTELEM *buf= buffer[0];
  4194. int64_t error=0;
  4195. if(orientation&1) buf+=w;
  4196. if(orientation>1) buf+=stride>>1;
  4197. memset(buffer[0], 0, sizeof(int)*width*height);
  4198. buf[w/2 + h/2*stride]= 256*256;
  4199. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4200. for(y=0; y<height; y++){
  4201. for(x=0; x<width; x++){
  4202. int64_t d= buffer[0][x + y*width];
  4203. error += d*d;
  4204. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
  4205. }
  4206. if(FFABS(height/2-y)<9 && level==2) printf("\n");
  4207. }
  4208. error= (int)(sqrt(error)+0.5);
  4209. errors[level][orientation]= error;
  4210. if(g) g=av_gcd(g, error);
  4211. else g= error;
  4212. }
  4213. }
  4214. printf("static int const visual_weight[][4]={\n");
  4215. for(level=0; level<s.spatial_decomposition_count; level++){
  4216. printf(" {");
  4217. for(orientation=0; orientation<4; orientation++){
  4218. printf("%8"PRId64",", errors[level][orientation]/g);
  4219. }
  4220. printf("},\n");
  4221. }
  4222. printf("};\n");
  4223. {
  4224. int level=2;
  4225. int w= width >> (s.spatial_decomposition_count-level);
  4226. //int h= height >> (s.spatial_decomposition_count-level);
  4227. int stride= width << (s.spatial_decomposition_count-level);
  4228. DWTELEM *buf= buffer[0];
  4229. int64_t error=0;
  4230. buf+=w;
  4231. buf+=stride>>1;
  4232. memset(buffer[0], 0, sizeof(int)*width*height);
  4233. #if 1
  4234. for(y=0; y<height; y++){
  4235. for(x=0; x<width; x++){
  4236. int tab[4]={0,2,3,1};
  4237. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  4238. }
  4239. }
  4240. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4241. #else
  4242. for(y=0; y<h; y++){
  4243. for(x=0; x<w; x++){
  4244. buf[x + y*stride ]=169;
  4245. buf[x + y*stride-w]=64;
  4246. }
  4247. }
  4248. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4249. #endif
  4250. for(y=0; y<height; y++){
  4251. for(x=0; x<width; x++){
  4252. int64_t d= buffer[0][x + y*width];
  4253. error += d*d;
  4254. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
  4255. }
  4256. if(FFABS(height/2-y)<9) printf("\n");
  4257. }
  4258. }
  4259. }
  4260. return 0;
  4261. }
  4262. #endif /* TEST */