1
0

macroblock.c 187 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475
  1. /*!
  2. ***********************************************************************
  3. * \file macroblock.c
  4. *
  5. * \brief
  6. * Decode a Macroblock
  7. *
  8. * \author
  9. * Main contributors (see contributors.h for copyright, address and affiliation details)
  10. * - Inge Lille-Langøy <[email protected]>
  11. * - Rickard Sjoberg <[email protected]>
  12. * - Jani Lainema <[email protected]>
  13. * - Sebastian Purreiter <[email protected]>
  14. * - Thomas Wedi <[email protected]>
  15. * - Detlev Marpe <[email protected]>
  16. * - Gabi Blaettermann
  17. * - Ye-Kui Wang <[email protected]>
  18. * - Lowell Winger <[email protected]>
  19. * - Alexis Michael Tourapis <[email protected]>
  20. ***********************************************************************
  21. */
  22. #include "contributors.h"
  23. #include <math.h>
  24. #include "block.h"
  25. #include "global.h"
  26. #include "mbuffer.h"
  27. #include "elements.h"
  28. #include "errorconcealment.h"
  29. #include "macroblock.h"
  30. #include "fmo.h"
  31. #include "cabac.h"
  32. #include "vlc.h"
  33. #include "image.h"
  34. #include "mb_access.h"
  35. #include "biaridecod.h"
  36. #include "transform8x8.h"
  37. #include "transform.h"
  38. #include "mc_prediction.h"
  39. #include "quant.h"
  40. #include "intra4x4_pred.h"
  41. #include "intra8x8_pred.h"
  42. #include "intra16x16_pred.h"
  43. #include "mv_prediction.h"
  44. #include "optim.h"
  45. #include "mb_prediction.h"
  46. #include <emmintrin.h>
  47. #include <smmintrin.h>
  48. #if TRACE
  49. #define TRACE_STRING(s) strncpy(currSE.tracestring, s, TRACESTRING_SIZE)
  50. #define TRACE_DECBITS(i) dectracebitcnt(1)
  51. #define TRACE_PRINTF(s) sprintf(type, "%s", s);
  52. #define TRACE_STRING_P(s) strncpy(currSE->tracestring, s, TRACESTRING_SIZE)
  53. #else
  54. #define TRACE_STRING(s)
  55. #define TRACE_DECBITS(i)
  56. #define TRACE_PRINTF(s)
  57. #define TRACE_STRING_P(s)
  58. #endif
  59. //! look up tables for FRExt_chroma support
  60. void dectracebitcnt(int count);
  61. static void read_motion_info_from_NAL_p_slice (Macroblock *currMB);
  62. static void read_motion_info_from_NAL_b_slice (Macroblock *currMB);
  63. static void read_ipred_modes (Macroblock *currMB);
  64. static void read_CBP_and_coeffs_from_NAL_CABAC (Macroblock *currMB);
  65. static void read_CBP_and_coeffs_from_NAL_CAVLC (Macroblock *currMB);
  66. static void read_IPCM_coeffs_from_NAL (Slice *currSlice, struct datapartition *dP);
  67. static void read_one_macroblock_i_slice (Macroblock *currMB);
  68. static void read_one_macroblock_p_slice (Macroblock *currMB);
  69. static void read_one_macroblock_b_slice (Macroblock *currMB);
  70. static int decode_one_component_i_slice (Macroblock *currMB, ColorPlane curr_plane, struct video_image *image, StorablePicture *dec_picture);
  71. static int decode_one_component_p_slice (Macroblock *currMB, ColorPlane curr_plane, struct video_image *image, StorablePicture *dec_picture);
  72. static int decode_one_component_b_slice (Macroblock *currMB, ColorPlane curr_plane, struct video_image *image, StorablePicture *dec_picture);
  73. static int decode_one_component_sp_slice (Macroblock *currMB, ColorPlane curr_plane, struct video_image *image, StorablePicture *dec_picture);
  74. static inline void or_bits(int64 *x, int mask, int position)
  75. {
  76. #ifdef _M_IX86
  77. __m64 mmx_x = *(__m64 *)x;
  78. __m64 mmx_mask = _mm_cvtsi32_si64(mask);
  79. mmx_mask=_mm_slli_si64(mmx_mask, position);
  80. mmx_x = _mm_or_si64(mmx_x, mmx_mask);
  81. *(__m64 *)x = mmx_x;
  82. #else
  83. *x |= ((int64) mask << position);
  84. #endif
  85. }
  86. /*!
  87. ************************************************************************
  88. * \brief
  89. * Set context for reference frames
  90. ************************************************************************
  91. */
  92. static inline int BType2CtxRef (int btype)
  93. {
  94. return (btype >= 4);
  95. }
  96. /*!
  97. ************************************************************************
  98. * \brief
  99. * Function for reading the reference picture indices using VLC
  100. ************************************************************************
  101. */
  102. static char readRefPictureIdx_VLC(SyntaxElement *currSE, DataPartition *dP, int list)
  103. {
  104. #if TRACE
  105. char tstring[20];
  106. sprintf( tstring, "ref_idx_l%d", list);
  107. strncpy(currSE->tracestring, tstring, TRACESTRING_SIZE);
  108. #endif
  109. currSE->value2 = list;
  110. readSyntaxElement_UVLC(currSE, dP);
  111. return (char) currSE->value1;
  112. }
  113. /*!
  114. ************************************************************************
  115. * \brief
  116. * Function for reading the reference picture indices using FLC
  117. ************************************************************************
  118. */
  119. static char readRefPictureIdx_FLC(SyntaxElement *currSE, DataPartition *dP, int list)
  120. {
  121. #if TRACE
  122. char tstring[20];
  123. sprintf( tstring, "ref_idx_l%d", list);
  124. strncpy(currSE->tracestring, tstring, TRACESTRING_SIZE);
  125. #endif
  126. //currSE->len = 1;
  127. currSE->value1 = 1 - readSyntaxElement_FLC(dP->bitstream, 1);
  128. return (char) currSE->value1;
  129. }
  130. /*!
  131. ************************************************************************
  132. * \brief
  133. * Dummy Function for reading the reference picture indices
  134. ************************************************************************
  135. */
  136. static char readRefPictureIdx_Null(SyntaxElement *currSE, DataPartition *dP, int list)
  137. {
  138. return 0;
  139. }
  140. /*!
  141. ************************************************************************
  142. * \brief
  143. * Function to prepare reference picture indice function pointer
  144. ************************************************************************
  145. */
  146. static void prepareListforRefIdx ( Macroblock *currMB, SyntaxElement *currSE, int num_ref_idx_active, int refidx_present)
  147. {
  148. currMB->readRefPictureIdx = readRefPictureIdx_Null; // Initialize readRefPictureIdx
  149. if(num_ref_idx_active > 1)
  150. {
  151. currSE->mapping = linfo_ue;
  152. if (refidx_present)
  153. {
  154. if (num_ref_idx_active == 2)
  155. currMB->readRefPictureIdx = readRefPictureIdx_FLC;
  156. else
  157. currMB->readRefPictureIdx = readRefPictureIdx_VLC;
  158. }
  159. }
  160. }
  161. #if defined(_DEBUG) || defined(_M_X64)
  162. void set_chroma_qp(Macroblock* currMB)
  163. {
  164. // TODO: benski> we could use MMX for this if we could find a formula for QP_SCALE_CR
  165. VideoParameters *p_Vid = currMB->p_Vid;
  166. StorablePicture *dec_picture = p_Vid->dec_picture;
  167. int i;
  168. for (i=0; i<2; ++i)
  169. {
  170. currMB->qpc[i] = iClip3 ( -p_Vid->bitdepth_chroma_qp_scale, 51, currMB->qp + dec_picture->chroma_qp_offset[i] );
  171. currMB->qpc[i] = currMB->qpc[i] < 0 ? currMB->qpc[i] : QP_SCALE_CR[currMB->qpc[i]];
  172. currMB->qp_scaled[i + 1] = currMB->qpc[i] + p_Vid->bitdepth_chroma_qp_scale;
  173. }
  174. }
  175. #else
  176. void set_chroma_qp(Macroblock* currMB);
  177. #endif
  178. /*!
  179. ************************************************************************
  180. * \brief
  181. * updates chroma QP according to luma QP and bit depth
  182. ************************************************************************
  183. */
  184. static inline void update_qp(Macroblock *currMB, int qp)
  185. {
  186. VideoParameters *p_Vid = currMB->p_Vid;
  187. currMB->qp = qp;
  188. currMB->qp_scaled[0] = qp + p_Vid->bitdepth_luma_qp_scale;
  189. set_chroma_qp(currMB);
  190. currMB->is_lossless = (Boolean) ((currMB->qp_scaled[0] == 0) && (p_Vid->lossless_qpprime_flag == 1));
  191. }
  192. static void read_delta_quant_CAVLC(SyntaxElement *currSE, DataPartition *dP, Macroblock *currMB, const byte *partMap, int type)
  193. {
  194. Slice *currSlice = currMB->p_Slice;
  195. VideoParameters *p_Vid = currMB->p_Vid;
  196. dP = &(currSlice->partArr[partMap[type]]);
  197. currSE->mapping = linfo_se;
  198. readSyntaxElement_UVLC(currSE, dP);
  199. currMB->delta_quant = (short) currSE->value1;
  200. if ((currMB->delta_quant < -(26 + p_Vid->bitdepth_luma_qp_scale/2)) || (currMB->delta_quant > (25 + p_Vid->bitdepth_luma_qp_scale/2)))
  201. error ("mb_qp_delta is out of range", 500);
  202. p_Vid->qp = ((p_Vid->qp + currMB->delta_quant + 52 + 2*p_Vid->bitdepth_luma_qp_scale)%(52+p_Vid->bitdepth_luma_qp_scale)) -
  203. p_Vid->bitdepth_luma_qp_scale;
  204. update_qp(currMB, p_Vid->qp);
  205. }
  206. static void inline read_delta_quant_CABAC(SyntaxElement *currSE, DataPartition *dP, Macroblock *currMB, const byte *partMap, int type)
  207. {
  208. Slice *currSlice = currMB->p_Slice;
  209. VideoParameters *p_Vid = currMB->p_Vid;
  210. dP = &(currSlice->partArr[partMap[type]]);
  211. currMB->delta_quant = readDquant_CABAC(currSlice, &dP->de_cabac);
  212. if ((currMB->delta_quant < -(26 + p_Vid->bitdepth_luma_qp_scale/2)) || (currMB->delta_quant > (25 + p_Vid->bitdepth_luma_qp_scale/2)))
  213. error ("mb_qp_delta is out of range", 500);
  214. p_Vid->qp = ((p_Vid->qp + currMB->delta_quant + 52 + 2*p_Vid->bitdepth_luma_qp_scale)%(52+p_Vid->bitdepth_luma_qp_scale)) - p_Vid->bitdepth_luma_qp_scale;
  215. update_qp(currMB, p_Vid->qp);
  216. }
  217. /*!
  218. ************************************************************************
  219. * \brief
  220. * Function to read reference picture indice values
  221. ************************************************************************
  222. */
  223. static void readMBRefPictureIdx(SyntaxElement *currSE, DataPartition *dP, Macroblock *currMB, PicMotion **motion, int list, int step_v0, int step_h0)
  224. {
  225. int k, j, j0, i0, i;
  226. char refframe;
  227. for (j0 = 0; j0 < 4; j0 += step_v0)
  228. {
  229. currMB->subblock_y = j0 << 2;
  230. for (i0 = 0; i0 < 4; i0 += step_h0)
  231. {
  232. currMB->subblock_x = i0 << 2;
  233. k = 2 * (j0 >> 1) + (i0 >> 1);
  234. if ((currMB->b8pdir[k] == list || currMB->b8pdir[k] == BI_PRED) && currMB->b8mode[k] != 0)
  235. {
  236. refframe = currMB->readRefPictureIdx(currSE, dP, list);
  237. for (j = j0; j < j0 + step_v0; ++j)
  238. {
  239. for (i=0;i<step_h0;i++)
  240. {
  241. motion[j][currMB->block_x + i0 + i].ref_idx = refframe;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. static void readMBRefPictureIdx_CABAC1(DataPartition *dP, Macroblock *currMB, PicMotion **motion, int list, int step_v0)
  249. {
  250. int k, j, j0, i0;
  251. char refframe;
  252. for (j0 = 0; j0 < 4; j0 += step_v0)
  253. {
  254. currMB->subblock_y = j0 << 2;
  255. for (i0 = 0; i0 < 4; i0 += 1)
  256. {
  257. currMB->subblock_x = i0 << 2;
  258. k = 2 * (j0 >> 1) + (i0 >> 1);
  259. if ((currMB->b8pdir[k] == list || currMB->b8pdir[k] == BI_PRED) && currMB->b8mode[k] != 0)
  260. {
  261. refframe = readRefFrame_CABAC(currMB, &dP->de_cabac, list, i0<<2, j0<<2);
  262. for (j = j0; j < j0 + step_v0; ++j)
  263. motion[j][currMB->block_x + i0].ref_idx=refframe;
  264. }
  265. }
  266. }
  267. }
  268. static void readMBRefPictureIdx_CABAC2(DataPartition *dP, Macroblock *currMB, PicMotion **motion, int list, int step_v0)
  269. {
  270. int k, j, j0;
  271. char refframe;
  272. for (j0 = 0; j0 < 4; j0 += step_v0)
  273. {
  274. currMB->subblock_y = j0 << 2;
  275. currMB->subblock_x = 0 << 2;
  276. k = 2 * (j0 >> 1) + (0 >> 1);
  277. if ((currMB->b8pdir[k] == list || currMB->b8pdir[k] == BI_PRED) && currMB->b8mode[k] != 0)
  278. {
  279. refframe = readRefFrame_CABAC0(currMB, &dP->de_cabac, list, j0<<2);
  280. for (j = j0; j < j0 + step_v0; ++j)
  281. {
  282. motion[j][currMB->block_x + 0].ref_idx=refframe;
  283. motion[j][currMB->block_x + 1].ref_idx=refframe;
  284. }
  285. }
  286. //
  287. currMB->subblock_x = 2 << 2;
  288. k = 2 * (j0 >> 1) + (2 >> 1);
  289. if ((currMB->b8pdir[k] == list || currMB->b8pdir[k] == BI_PRED) && currMB->b8mode[k] != 0)
  290. {
  291. refframe = readRefFrame_CABAC(currMB, &dP->de_cabac, list, 8, j0<<2);
  292. for (j = j0; j < j0 + step_v0; ++j)
  293. {
  294. motion[j][currMB->block_x + 2].ref_idx=refframe;
  295. motion[j][currMB->block_x + 3].ref_idx=refframe;
  296. }
  297. }
  298. }
  299. }
  300. static void readMBRefPictureIdx_CABAC4(DataPartition *dP, Macroblock *currMB, PicMotion **motion, int list, int step_v0)
  301. {
  302. int k, j, j0;
  303. char refframe;
  304. for (j0 = 0; j0 < 4; j0 += step_v0)
  305. {
  306. currMB->subblock_y = j0 << 2;
  307. currMB->subblock_x = 0;
  308. k = j0 & ~1;
  309. if ((currMB->b8pdir[k] == list || currMB->b8pdir[k] == BI_PRED) && currMB->b8mode[k] != 0)
  310. {
  311. refframe = readRefFrame_CABAC0(currMB, &dP->de_cabac, list, j0<<2);
  312. for (j = j0; j < j0 + step_v0; ++j)
  313. {
  314. motion[j][currMB->block_x + 0].ref_idx=refframe;
  315. motion[j][currMB->block_x + 1].ref_idx=refframe;
  316. motion[j][currMB->block_x + 2].ref_idx=refframe;
  317. motion[j][currMB->block_x + 3].ref_idx=refframe;
  318. }
  319. }
  320. }
  321. }
  322. static void readMBRefPictureIdx_CABAC(DataPartition *dP, Macroblock *currMB, PicMotion **motion, int list, int step_v0, int step_h0)
  323. {
  324. switch(step_h0)
  325. {
  326. case 1:
  327. readMBRefPictureIdx_CABAC1(dP, currMB, motion, list, step_v0);
  328. break;
  329. case 2:
  330. readMBRefPictureIdx_CABAC2(dP, currMB, motion, list, step_v0);
  331. break;
  332. case 4:
  333. readMBRefPictureIdx_CABAC4(dP, currMB, motion, list, step_v0);
  334. break;
  335. }
  336. }
  337. static void readMBRefPictureIdx_CABAC_NoReference(Macroblock *currMB, PicMotion **motion, int list, int step_v0, int step_h0)
  338. {
  339. int k, j, j0, i0, i;
  340. for (j0 = 0; j0 < 4; j0 += step_v0)
  341. {
  342. for (i0 = 0; i0 < 4; i0 += step_h0)
  343. {
  344. k = 2 * (j0 >> 1) + (i0 >> 1);
  345. if ((currMB->b8pdir[k] == list || currMB->b8pdir[k] == BI_PRED) && currMB->b8mode[k] != 0)
  346. {
  347. for (j = j0; j < j0 + step_v0; ++j)
  348. {
  349. for (i=0;i<step_h0;i++)
  350. {
  351. motion[j][currMB->block_x + i0 + i].ref_idx=0;
  352. }
  353. }
  354. }
  355. }
  356. }
  357. }
  358. /*!
  359. ************************************************************************
  360. * \brief
  361. * Function to read reference picture indice values
  362. ************************************************************************
  363. */
  364. static void readMBMotionVectors(SyntaxElement *currSE, DataPartition *dP, Macroblock *currMB, int list, int step_h0, int step_v0)
  365. {
  366. int i, j, k, i4, j4, ii, jj, kk, i0, j0;
  367. short curr_mvd[2], curr_mv[2], pred_mv[2];
  368. MotionVector (*mvd)[4];
  369. //MotionVector **mv;
  370. int mv_mode, step_h, step_v;
  371. char cur_ref_idx;
  372. VideoParameters *p_Vid = currMB->p_Vid;
  373. StorablePicture *dec_picture = p_Vid->dec_picture;
  374. PicMotionParams *motion = &dec_picture->motion;
  375. PixelPos block[4]; // neighbor blocks
  376. for (j0=0; j0<4; j0+=step_v0)
  377. {
  378. for (i0=0; i0<4; i0+=step_h0)
  379. {
  380. kk = 2 * (j0 >> 1) + (i0 >> 1);
  381. if ((currMB->b8pdir[kk]== list || currMB->b8pdir[kk]== BI_PRED) && (currMB->b8mode[kk] !=0))//has forward vector
  382. {
  383. PicMotion **list_motion = motion->motion[list];
  384. cur_ref_idx = list_motion[currMB->block_y+j0][currMB->block_x+i0].ref_idx;
  385. mv_mode = currMB->b8mode[kk];
  386. step_h = BLOCK_STEP [mv_mode][0];
  387. step_v = BLOCK_STEP [mv_mode][1];
  388. for (j = j0; j < j0 + step_v0; j += step_v)
  389. {
  390. PicMotion **mv;
  391. currMB->subblock_y = j << 2; // position used for context determination
  392. j4 = currMB->block_y + j;
  393. mv = &list_motion[j4];
  394. mvd = &currMB->mvd [list][j];
  395. for (i = i0; i < i0 + step_h0; i += step_h)
  396. {
  397. currMB->subblock_x = i << 2; // position used for context determination
  398. i4 = currMB->block_x + i;
  399. get_neighbors(currMB, block, BLOCK_SIZE * i, BLOCK_SIZE * j, 4 * step_h);
  400. // first make mv-prediction
  401. currMB->GetMVPredictor (currMB, block, pred_mv, cur_ref_idx, list_motion, BLOCK_SIZE * i, BLOCK_SIZE * j, 4 * step_h, 4 * step_v);
  402. for (k=0; k < 2; ++k)
  403. {
  404. currSE->value2 = (k << 1) + list; // identifies the component; only used for context determination
  405. readSyntaxElement_UVLC(currSE, dP);
  406. curr_mvd[k] = (short) currSE->value1;
  407. curr_mv [k] = (short)(curr_mvd[k] + pred_mv[k]); // compute motion vector
  408. }
  409. // Init motion vectors
  410. for(jj = 0; jj < step_v; ++jj)
  411. {
  412. for(ii = i4; ii < i4 + step_h; ++ii)
  413. {
  414. memcpy(&mv[jj][ii].mv, curr_mv, sizeof(MotionVector));
  415. }
  416. }
  417. // Init first line (mvd)
  418. for(ii = i; ii < i + step_h; ++ii)
  419. {
  420. memcpy(mvd[0][ii], curr_mvd, sizeof(MotionVector));
  421. }
  422. // now copy all other lines
  423. for(jj = 1; jj < step_v; ++jj)
  424. {
  425. memcpy(mvd[jj][i], mvd[0][i], step_h * sizeof(MotionVector));
  426. }
  427. }
  428. }
  429. }
  430. }
  431. }
  432. }
  433. static void readMBMotionVectors_CABAC(DataPartition *dP, Macroblock *currMB, int list, int step_h0, int step_v0)
  434. {
  435. int i, j, k, i4, j4, ii, jj, kk, i0, j0;
  436. short curr_mvd[2], curr_mv[2], pred_mv[2];
  437. MotionVector (*mvd)[4];
  438. //MotionVector **mv;
  439. int mv_mode, step_h, step_v;
  440. char cur_ref_idx;
  441. VideoParameters *p_Vid = currMB->p_Vid;
  442. StorablePicture *dec_picture = p_Vid->dec_picture;
  443. PicMotionParams *motion = &dec_picture->motion;
  444. PixelPos block[4]; // neighbor blocks
  445. for (j0=0; j0<4; j0+=step_v0)
  446. {
  447. for (i0=0; i0<4; i0+=step_h0)
  448. {
  449. kk = (j0 & ~1) + (i0 >> 1);
  450. if ((currMB->b8pdir[kk]== list || currMB->b8pdir[kk]== BI_PRED) && (currMB->b8mode[kk] !=0))//has forward vector
  451. {
  452. PicMotion **list_motion = motion->motion[list];
  453. cur_ref_idx = list_motion[currMB->block_y+j0][currMB->block_x+i0].ref_idx;
  454. mv_mode = currMB->b8mode[kk];
  455. step_h = BLOCK_STEP [mv_mode][0];
  456. step_v = BLOCK_STEP [mv_mode][1];
  457. for (j = j0; j < j0 + step_v0; j += step_v)
  458. {
  459. PicMotion **mv;
  460. int block_j = j << 2;
  461. currMB->subblock_y = block_j; // position used for context determination
  462. j4 = currMB->block_y + j;
  463. mv = &list_motion[j4];
  464. mvd = &currMB->mvd [list][j];
  465. for (i = i0; i < i0 + step_h0; i += step_h)
  466. {
  467. int block_i=i << 2;
  468. currMB->subblock_x = block_i; // position used for context determination
  469. i4 = currMB->block_x + i;
  470. get_neighbors(currMB, block, block_i, block_j, 4 * step_h);
  471. // first make mv-prediction
  472. currMB->GetMVPredictor (currMB, block, pred_mv, cur_ref_idx, list_motion, block_i, block_j, 4 * step_h, 4 * step_v);
  473. for (k=0; k < 2; ++k)
  474. {
  475. //currSE.value2 = (k << 1) + list; // identifies the component; only used for context determination
  476. curr_mvd[k] = (short)readMVD_CABAC(currMB, &dP->de_cabac, k, list, block_i, block_j);
  477. curr_mv [k] = (short)(curr_mvd[k] + pred_mv[k]); // compute motion vector
  478. }
  479. // Init motion vectors
  480. for(jj = 0; jj < step_v; ++jj)
  481. {
  482. for(ii = i4; ii < i4 + step_h; ++ii)
  483. {
  484. *(int32_t *)(&mv[jj][ii].mv) = *(int32_t *)curr_mv;
  485. }
  486. }
  487. // Init first line (mvd)
  488. for(ii = i; ii < i + step_h; ++ii)
  489. {
  490. *(int32_t *)(mvd[0][ii]) = *(int32_t *)curr_mvd;
  491. }
  492. // now copy all other lines
  493. for(jj = 1; jj < step_v; ++jj)
  494. {
  495. memcpy_amd(mvd[jj][i], mvd[0][i], step_h * sizeof(MotionVector));
  496. }
  497. }
  498. }
  499. }
  500. }
  501. }
  502. }
  503. /*!
  504. ************************************************************************
  505. * \brief
  506. * initializes the current macroblock
  507. ************************************************************************
  508. */
  509. void start_macroblock(Slice *currSlice, Macroblock **currMB)
  510. {
  511. VideoParameters *p_Vid = currSlice->p_Vid;
  512. StorablePicture *dec_picture = p_Vid->dec_picture;
  513. int mb_nr = p_Vid->current_mb_nr;
  514. Macroblock *mb = &p_Vid->mb_data[mb_nr]; // intialization code deleted, see below, StW
  515. *currMB = mb;
  516. mb->p_Vid = p_Vid;
  517. mb->p_Slice = currSlice;
  518. mb->mbAddrX = mb_nr;
  519. //assert (mb_nr < (int) p_Vid->PicSizeInMbs);
  520. /* Update coordinates of the current macroblock */
  521. if (currSlice->mb_aff_frame_flag)
  522. {
  523. mb->mb_x = (mb_nr) % ((2*p_Vid->width) / MB_BLOCK_SIZE);
  524. mb->mb_y = 2*((mb_nr) / ((2*p_Vid->width) / MB_BLOCK_SIZE));
  525. mb->mb_y += (mb->mb_x & 0x01);
  526. mb->mb_x >>= 1;
  527. }
  528. else
  529. {
  530. mb->mb_x = p_Vid->PicPos[mb_nr][0];
  531. mb->mb_y = p_Vid->PicPos[mb_nr][1];
  532. }
  533. /* Define vertical positions */
  534. mb->block_y = mb->mb_y * BLOCK_SIZE; /* luma block position */
  535. mb->block_y_aff = mb->block_y;
  536. mb->pix_y = mb->mb_y * MB_BLOCK_SIZE; /* luma macroblock position */
  537. mb->pix_c_y = mb->mb_y * p_Vid->mb_cr_size_y; /* chroma macroblock position */
  538. /* Define horizontal positions */
  539. mb->block_x = mb->mb_x * BLOCK_SIZE; /* luma block position */
  540. mb->pix_x = mb->mb_x * MB_BLOCK_SIZE; /* luma pixel position */
  541. mb->pix_c_x = mb->mb_x * p_Vid->mb_cr_size_x; /* chroma pixel position */
  542. // Save the slice number of this macroblock. When the macroblock below
  543. // is coded it will use this to decide if prediction for above is possible
  544. mb->slice_nr = (short) p_Vid->current_slice_nr;
  545. if (p_Vid->current_slice_nr >= MAX_NUM_SLICES)
  546. {
  547. error ("Maximum number of supported slices exceeded. \nPlease recompile with increased value for MAX_NUM_SLICES", 200);
  548. }
  549. dec_picture->slice_id[mb->mb_y][mb->mb_x] = (short) p_Vid->current_slice_nr;
  550. dec_picture->max_slice_id = (short) imax(p_Vid->current_slice_nr, dec_picture->max_slice_id);
  551. CheckAvailabilityOfNeighbors(mb);
  552. // Select appropriate MV predictor function
  553. init_motion_vector_prediction(*currMB, currSlice->mb_aff_frame_flag);
  554. set_read_and_store_CBP(currMB, currSlice->active_sps->chroma_format_idc);
  555. // Reset syntax element entries in MB struct
  556. update_qp(*currMB, p_Vid->qp);
  557. mb->mb_type = 0;
  558. mb->delta_quant = 0;
  559. mb->cbp = 0;
  560. mb->c_ipred_mode = DC_PRED_8; //GB
  561. if (currSlice->slice_type != I_SLICE)
  562. {
  563. if (currSlice->slice_type != B_SLICE)
  564. memzero64(mb->mvd);//, BLOCK_MULTIPLE * BLOCK_MULTIPLE * 2 * sizeof(short));
  565. else
  566. memzero128(mb->mvd);//, 2 * BLOCK_MULTIPLE * BLOCK_MULTIPLE * 2 * sizeof(short));
  567. }
  568. memzero24(mb->cbp_blk);
  569. memzero24(mb->cbp_bits);
  570. memzero24(mb->cbp_bits_8x8);
  571. // initialize currSlice->mb_rres
  572. memset(currSlice->mb_rres8, 0, sizeof(currSlice->mb_rres8));
  573. // store filtering parameters for this MB
  574. mb->DFDisableIdc = currSlice->DFDisableIdc;
  575. mb->DFAlphaC0Offset = currSlice->DFAlphaC0Offset;
  576. mb->DFBetaOffset = currSlice->DFBetaOffset;
  577. }
  578. /*!
  579. ************************************************************************
  580. * \brief
  581. * set coordinates of the next macroblock
  582. * check end_of_slice condition
  583. ************************************************************************
  584. */
  585. Boolean exit_macroblock(Slice *currSlice, int eos_bit)
  586. {
  587. VideoParameters *p_Vid = currSlice->p_Vid;
  588. //! The if() statement below resembles the original code, which tested
  589. //! p_Vid->current_mb_nr == p_Vid->PicSizeInMbs. Both is, of course, nonsense
  590. //! In an error prone environment, one can only be sure to have a new
  591. //! picture by checking the tr of the next slice header!
  592. // printf ("exit_macroblock: FmoGetLastMBOfPicture %d, p_Vid->current_mb_nr %d\n", FmoGetLastMBOfPicture(), p_Vid->current_mb_nr);
  593. ++(p_Vid->num_dec_mb);
  594. if (p_Vid->num_dec_mb == p_Vid->PicSizeInMbs)
  595. {
  596. return TRUE;
  597. }
  598. // ask for last mb in the slice CAVLC
  599. else
  600. {
  601. p_Vid->current_mb_nr = FmoGetNextMBNr (p_Vid, p_Vid->current_mb_nr);
  602. if (p_Vid->current_mb_nr == -1) // End of Slice group, MUST be end of slice
  603. {
  604. assert (currSlice->nal_startcode_follows (currSlice, eos_bit) == TRUE);
  605. return TRUE;
  606. }
  607. if(currSlice->nal_startcode_follows(currSlice, eos_bit) == FALSE)
  608. return FALSE;
  609. if(currSlice->slice_type == I_SLICE || currSlice->slice_type == SI_SLICE || p_Vid->active_pps->entropy_coding_mode_flag == CABAC)
  610. return TRUE;
  611. if(p_Vid->cod_counter <= 0)
  612. return TRUE;
  613. return FALSE;
  614. }
  615. }
  616. /*!
  617. ************************************************************************
  618. * \brief
  619. * Interpret the mb mode for P-Frames
  620. ************************************************************************
  621. */
  622. static void interpret_mb_mode_P(Macroblock *currMB)
  623. {
  624. VideoParameters *p_Vid = currMB->p_Vid;
  625. static const int ICBPTAB[6] = {0,16,32,15,31,47};
  626. int mbmode = currMB->mb_type;
  627. #define ZERO_P8x8 (mbmode==5)
  628. #define MODE_IS_P8x8 (mbmode==4 || mbmode==5)
  629. #define MODE_IS_I4x4 (mbmode==6)
  630. #define I16OFFSET (mbmode-7)
  631. #define MODE_IS_IPCM (mbmode==31)
  632. if(mbmode <4)
  633. {
  634. currMB->mb_type = mbmode;
  635. memset(&currMB->b8mode[0],mbmode,4 * sizeof(char));
  636. memset(&currMB->b8pdir[0], 0, 4 * sizeof(char));
  637. }
  638. else if(MODE_IS_P8x8)
  639. {
  640. currMB->mb_type = P8x8;
  641. p_Vid->allrefzero = ZERO_P8x8;
  642. }
  643. else if(MODE_IS_I4x4)
  644. {
  645. currMB->mb_type = I4MB;
  646. memset(&currMB->b8mode[0],IBLOCK, 4 * sizeof(char));
  647. memset(&currMB->b8pdir[0], -1, 4 * sizeof(char));
  648. }
  649. else if(MODE_IS_IPCM)
  650. {
  651. currMB->mb_type = IPCM;
  652. currMB->cbp = -1;
  653. currMB->i16mode = 0;
  654. memset(&currMB->b8mode[0], 0, 4 * sizeof(char));
  655. memset(&currMB->b8pdir[0],-1, 4 * sizeof(char));
  656. }
  657. else
  658. {
  659. currMB->mb_type = I16MB;
  660. currMB->cbp = ICBPTAB[(I16OFFSET)>>2];
  661. currMB->i16mode = (I16OFFSET) & 0x03;
  662. memset(&currMB->b8mode[0], 0, 4 * sizeof(char));
  663. memset(&currMB->b8pdir[0],-1, 4 * sizeof(char));
  664. }
  665. }
  666. /*!
  667. ************************************************************************
  668. * \brief
  669. * Interpret the mb mode for I-Frames
  670. ************************************************************************
  671. */
  672. static void interpret_mb_mode_I(Macroblock *currMB)
  673. {
  674. static const int ICBPTAB[6] = {0,16,32,15,31,47};
  675. int mbmode = currMB->mb_type;
  676. if (mbmode==0)
  677. {
  678. currMB->mb_type = I4MB;
  679. memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
  680. memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
  681. }
  682. else if(mbmode==25)
  683. {
  684. currMB->mb_type=IPCM;
  685. currMB->cbp= -1;
  686. currMB->i16mode = 0;
  687. memset(&currMB->b8mode[0],0,4 * sizeof(char));
  688. memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
  689. }
  690. else
  691. {
  692. currMB->mb_type = I16MB;
  693. currMB->cbp= ICBPTAB[(mbmode-1)>>2];
  694. currMB->i16mode = (mbmode-1) & 0x03;
  695. memset(&currMB->b8mode[0], 0, 4 * sizeof(char));
  696. memset(&currMB->b8pdir[0],-1, 4 * sizeof(char));
  697. }
  698. }
  699. /*!
  700. ************************************************************************
  701. * \brief
  702. * Interpret the mb mode for B-Frames
  703. ************************************************************************
  704. */
  705. static void interpret_mb_mode_B(Macroblock *currMB)
  706. {
  707. static const int offset2pdir16x16[12] = {0, 0, 1, 2, 0,0,0,0,0,0,0,0};
  708. static const int offset2pdir16x8[22][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{1,1},{0,0},{0,1},{0,0},{1,0},
  709. {0,0},{0,2},{0,0},{1,2},{0,0},{2,0},{0,0},{2,1},{0,0},{2,2},{0,0}};
  710. static const int offset2pdir8x16[22][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{1,1},{0,0},{0,1},{0,0},
  711. {1,0},{0,0},{0,2},{0,0},{1,2},{0,0},{2,0},{0,0},{2,1},{0,0},{2,2}};
  712. static const int ICBPTAB[6] = {0,16,32,15,31,47};
  713. int i, mbmode;
  714. int mbtype = currMB->mb_type;
  715. //--- set mbtype, b8type, and b8pdir ---
  716. if (mbtype==0) // direct
  717. {
  718. mbmode=0;
  719. memset(&currMB->b8mode[0],0,4 * sizeof(char));
  720. memset(&currMB->b8pdir[0],2,4 * sizeof(char));
  721. }
  722. else if (mbtype==23) // intra4x4
  723. {
  724. mbmode=I4MB;
  725. memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
  726. memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
  727. }
  728. else if ((mbtype>23) && (mbtype<48) ) // intra16x16
  729. {
  730. mbmode=I16MB;
  731. memset(&currMB->b8mode[0],0,4 * sizeof(char));
  732. memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
  733. currMB->cbp = ICBPTAB[(mbtype-24)>>2];
  734. currMB->i16mode = (mbtype-24) & 0x03;
  735. }
  736. else if (mbtype==22) // 8x8(+split)
  737. {
  738. mbmode=P8x8; // b8mode and pdir is transmitted in additional codewords
  739. }
  740. else if (mbtype<4) // 16x16
  741. {
  742. mbmode=1;
  743. memset(&currMB->b8mode[0], 1,4 * sizeof(char));
  744. memset(&currMB->b8pdir[0],offset2pdir16x16[mbtype],4 * sizeof(char));
  745. }
  746. else if(mbtype==48)
  747. {
  748. mbmode=IPCM;
  749. memset(&currMB->b8mode[0], 0,4 * sizeof(char));
  750. memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
  751. currMB->cbp= -1;
  752. currMB->i16mode = 0;
  753. }
  754. else if ((mbtype&0x01)==0) // 16x8
  755. {
  756. mbmode=2;
  757. memset(&currMB->b8mode[0], 2,4 * sizeof(char));
  758. for(i=0;i<4;++i)
  759. {
  760. currMB->b8pdir[i] = (char) offset2pdir16x8 [mbtype][i>>1];
  761. }
  762. }
  763. else
  764. {
  765. mbmode=3;
  766. memset(&currMB->b8mode[0], 3,4 * sizeof(char));
  767. for(i=0;i<4; ++i)
  768. {
  769. currMB->b8pdir[i] = (char) offset2pdir8x16 [mbtype][i&0x01];
  770. }
  771. }
  772. currMB->mb_type = mbmode;
  773. }
  774. /*!
  775. ************************************************************************
  776. * \brief
  777. * Interpret the mb mode for SI-Frames
  778. ************************************************************************
  779. */
  780. static void interpret_mb_mode_SI(Macroblock *currMB)
  781. {
  782. VideoParameters *p_Vid = currMB->p_Vid;
  783. const int ICBPTAB[6] = {0,16,32,15,31,47};
  784. int mbmode = currMB->mb_type;
  785. if (mbmode==0)
  786. {
  787. currMB->mb_type = SI4MB;
  788. memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
  789. memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
  790. p_Vid->siblock[currMB->mb_y][currMB->mb_x]=1;
  791. }
  792. else if (mbmode==1)
  793. {
  794. currMB->mb_type = I4MB;
  795. memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
  796. memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
  797. }
  798. else if(mbmode==26)
  799. {
  800. currMB->mb_type=IPCM;
  801. currMB->cbp= -1;
  802. currMB->i16mode = 0;
  803. memset(&currMB->b8mode[0],0,4 * sizeof(char));
  804. memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
  805. }
  806. else
  807. {
  808. currMB->mb_type = I16MB;
  809. currMB->cbp= ICBPTAB[(mbmode-2)>>2];
  810. currMB->i16mode = (mbmode-2) & 0x03;
  811. memset(&currMB->b8mode[0],0,4 * sizeof(char));
  812. memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
  813. }
  814. }
  815. /*!
  816. ************************************************************************
  817. * \brief
  818. * Set mode interpretation based on slice type
  819. ************************************************************************
  820. */
  821. void setup_slice_methods(Slice *currSlice)
  822. {
  823. switch (currSlice->slice_type)
  824. {
  825. case P_SLICE:
  826. currSlice->interpret_mb_mode = interpret_mb_mode_P;
  827. currSlice->read_motion_info_from_NAL = read_motion_info_from_NAL_p_slice;
  828. currSlice->read_one_macroblock = read_one_macroblock_p_slice;
  829. currSlice->decode_one_component = decode_one_component_p_slice;
  830. break;
  831. case SP_SLICE:
  832. currSlice->interpret_mb_mode = interpret_mb_mode_P;
  833. currSlice->read_motion_info_from_NAL = read_motion_info_from_NAL_p_slice;
  834. currSlice->read_one_macroblock = read_one_macroblock_p_slice;
  835. currSlice->decode_one_component = decode_one_component_sp_slice;
  836. break;
  837. case B_SLICE:
  838. currSlice->interpret_mb_mode = interpret_mb_mode_B;
  839. currSlice->read_motion_info_from_NAL = read_motion_info_from_NAL_b_slice;
  840. currSlice->read_one_macroblock = read_one_macroblock_b_slice;
  841. currSlice->decode_one_component = decode_one_component_b_slice;
  842. break;
  843. case I_SLICE:
  844. currSlice->interpret_mb_mode = interpret_mb_mode_I;
  845. currSlice->read_motion_info_from_NAL = NULL;
  846. currSlice->read_one_macroblock = read_one_macroblock_i_slice;
  847. currSlice->decode_one_component = decode_one_component_i_slice;
  848. break;
  849. case SI_SLICE:
  850. currSlice->interpret_mb_mode = interpret_mb_mode_SI;
  851. currSlice->read_motion_info_from_NAL = NULL;
  852. currSlice->read_one_macroblock = read_one_macroblock_i_slice;
  853. currSlice->decode_one_component = decode_one_component_i_slice;
  854. break;
  855. default:
  856. printf("Unsupported slice type\n");
  857. break;
  858. }
  859. if( IS_INDEPENDENT(currSlice->p_Vid) )
  860. currSlice->compute_colocated = compute_colocated_JV;
  861. else
  862. {
  863. if (currSlice->active_sps->frame_mbs_only_flag)
  864. currSlice->compute_colocated = compute_colocated;
  865. else
  866. currSlice->compute_colocated = compute_colocated_frames_mbs;
  867. }
  868. switch(currSlice->p_Vid->active_pps->entropy_coding_mode_flag)
  869. {
  870. case CABAC:
  871. currSlice->read_CBP_and_coeffs_from_NAL = read_CBP_and_coeffs_from_NAL_CABAC;
  872. break;
  873. case CAVLC:
  874. currSlice->read_CBP_and_coeffs_from_NAL = read_CBP_and_coeffs_from_NAL_CAVLC;
  875. break;
  876. default:
  877. printf("Unsupported entropy coding mode\n");
  878. break;
  879. }
  880. }
  881. void macroblock_set_dc_pred(VideoParameters *p_Vid, int block_x, int block_y)
  882. {
  883. int32_t dc_pred = 2 + (2 << 8) + (2 << 16) + (2 << 24);
  884. int32_t *pred = (int32_t *)&p_Vid->ipredmode[block_y][block_x];
  885. int stride = p_Vid->PicWidthInMbs;
  886. int i;
  887. for (i=0;i<BLOCK_SIZE;i++)
  888. {
  889. *pred = dc_pred;
  890. pred += stride;
  891. }
  892. }
  893. /*!
  894. ************************************************************************
  895. * \brief
  896. * init macroblock I and P frames
  897. ************************************************************************
  898. */
  899. #ifdef _M_IX86
  900. static void init_macroblock(Macroblock *currMB)
  901. {
  902. VideoParameters *p_Vid = currMB->p_Vid;
  903. int j;
  904. int block_x = currMB->block_x, block_y = currMB->block_y;
  905. PicMotionParams *motion = &p_Vid->dec_picture->motion;
  906. PicMotion **list_motion0, **list_motion1;
  907. __m64 const_0_minus_1 = _mm_setr_pi32(0, -1);
  908. macroblock_set_dc_pred(p_Vid, block_x, block_y);
  909. // reset vectors and pred. modes
  910. list_motion0 = motion->motion[LIST_0];
  911. for(j = 0; j < BLOCK_SIZE; j++)
  912. {
  913. PicMotion *block = &list_motion0[block_y+j][block_x];
  914. block[0].ref_pic_id = UNDEFINED_REFERENCE;
  915. *(__m64 *)&block[0].mv = const_0_minus_1;
  916. block[1].ref_pic_id = UNDEFINED_REFERENCE;
  917. *(__m64 *)&block[1].mv = const_0_minus_1;
  918. block[2].ref_pic_id = UNDEFINED_REFERENCE;
  919. *(__m64 *)&block[2].mv = const_0_minus_1;
  920. block[3].ref_pic_id = UNDEFINED_REFERENCE;
  921. *(__m64 *)&block[3].mv = const_0_minus_1;
  922. }
  923. list_motion1 = motion->motion[LIST_1];
  924. for(j = 0; j < BLOCK_SIZE; j++)
  925. {
  926. PicMotion *block = &list_motion1[block_y+j][block_x];
  927. block[0].ref_pic_id = UNDEFINED_REFERENCE;
  928. *(__m64 *)&block[0].mv = const_0_minus_1;
  929. block[1].ref_pic_id = UNDEFINED_REFERENCE;
  930. *(__m64 *)&block[1].mv = const_0_minus_1;
  931. block[2].ref_pic_id = UNDEFINED_REFERENCE;
  932. *(__m64 *)&block[2].mv = const_0_minus_1;
  933. block[3].ref_pic_id = UNDEFINED_REFERENCE;
  934. *(__m64 *)&block[3].mv = const_0_minus_1;
  935. }
  936. }
  937. #else
  938. static void init_macroblock(Macroblock *currMB)
  939. {
  940. VideoParameters *p_Vid = currMB->p_Vid;
  941. int i, j;
  942. int block_x = currMB->block_x, block_y = currMB->block_y;
  943. PicMotionParams *motion = &p_Vid->dec_picture->motion;
  944. PicMotion **list_motion0, **list_motion1;
  945. macroblock_set_dc_pred(p_Vid, block_x, block_y);
  946. // reset vectors and pred. modes
  947. list_motion0 = motion->motion[LIST_0];
  948. for(j = 0; j < BLOCK_SIZE; j++)
  949. {
  950. PicMotion *block0 = &list_motion0[block_y+j][block_x];
  951. block0[0].ref_pic_id = UNDEFINED_REFERENCE;
  952. memset(block0[0].mv, 0, sizeof(MotionVector));
  953. block0[0].ref_idx = -1;
  954. block0[1].ref_pic_id = UNDEFINED_REFERENCE;
  955. memset(block0[1].mv, 0, sizeof(MotionVector));
  956. block0[1].ref_idx = -1;
  957. block0[2].ref_pic_id = UNDEFINED_REFERENCE;
  958. memset(block0[2].mv, 0, sizeof(MotionVector));
  959. block0[2].ref_idx = -1;
  960. block0[3].ref_pic_id = UNDEFINED_REFERENCE;
  961. memset(block0[3].mv, 0, sizeof(MotionVector));
  962. block0[3].ref_idx = -1;
  963. }
  964. list_motion1 = motion->motion[LIST_1];
  965. for(j = 0; j < BLOCK_SIZE; j++)
  966. {
  967. PicMotion *block1 = &list_motion1[block_y+j][block_x];
  968. block1[0].ref_pic_id = UNDEFINED_REFERENCE;
  969. memset(block1[0].mv, 0, sizeof(MotionVector));
  970. block1[0].ref_idx = -1;
  971. block1[1].ref_pic_id = UNDEFINED_REFERENCE;
  972. memset(block1[1].mv, 0, sizeof(MotionVector));
  973. block1[1].ref_idx = -1;
  974. block1[2].ref_pic_id = UNDEFINED_REFERENCE;
  975. memset(block1[2].mv, 0, sizeof(MotionVector));
  976. block1[2].ref_idx = -1;
  977. block1[3].ref_pic_id = UNDEFINED_REFERENCE;
  978. memset(block1[3].mv, 0, sizeof(MotionVector));
  979. block1[3].ref_idx = -1;
  980. }
  981. }
  982. #endif
  983. /*!
  984. ************************************************************************
  985. * \brief
  986. * Sets mode for 8x8 block
  987. ************************************************************************
  988. */
  989. void SetB8Mode (Macroblock* currMB, int value, int i)
  990. {
  991. Slice* currSlice = currMB->p_Slice;
  992. static const char p_v2b8 [ 5] = {4, 5, 6, 7, IBLOCK};
  993. static const char p_v2pd [ 5] = {0, 0, 0, 0, -1};
  994. static const char b_v2b8 [14] = {0, 4, 4, 4, 5, 6, 5, 6, 5, 6, 7, 7, 7, IBLOCK};
  995. static const char b_v2pd [14] = {2, 0, 1, 2, 0, 0, 1, 1, 2, 2, 0, 1, 2, -1};
  996. if (currSlice->slice_type==B_SLICE)
  997. {
  998. currMB->b8mode[i] = b_v2b8[value];
  999. currMB->b8pdir[i] = b_v2pd[value];
  1000. }
  1001. else
  1002. {
  1003. currMB->b8mode[i] = p_v2b8[value];
  1004. currMB->b8pdir[i] = p_v2pd[value];
  1005. }
  1006. }
  1007. void reset_coeffs(Slice *currSlice)
  1008. {
  1009. VideoParameters *p_Vid = currSlice->p_Vid;
  1010. // reset all coeffs
  1011. #ifdef _DEBUG
  1012. {
  1013. int m;
  1014. for (m=0;m<3;m++)
  1015. {
  1016. int z;
  1017. short *b = &currSlice->cof[m][0][0];
  1018. for (z=0;z<256;z++)
  1019. {
  1020. if (b[z] != 0)
  1021. {
  1022. DebugBreak();
  1023. }
  1024. }
  1025. }
  1026. }
  1027. #endif
  1028. // benski> don't think this is necessary... enable check above to be sure
  1029. // memset(currSlice->cof, 0, sizeof(currSlice->cof));
  1030. // CAVLC
  1031. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  1032. memzero48(p_Vid->nz_coeff[p_Vid->current_mb_nr]);
  1033. }
  1034. void field_flag_inference(Macroblock *currMB)
  1035. {
  1036. VideoParameters *p_Vid = currMB->p_Vid;
  1037. if (currMB->mb_avail_left)
  1038. {
  1039. currMB->mb_field = p_Vid->mb_data[currMB->mb_addr_left].mb_field;
  1040. }
  1041. else
  1042. {
  1043. // check top macroblock pair
  1044. currMB->mb_field = currMB->mb_avail_up ? p_Vid->mb_data[currMB->mb_addr_up].mb_field : FALSE;
  1045. }
  1046. }
  1047. static void skip_macroblock(Macroblock *currMB)
  1048. {
  1049. short pred_mv[2];
  1050. int zeroMotionAbove;
  1051. int zeroMotionLeft;
  1052. PixelPos mb[4]; // neighbor blocks
  1053. int i, j;
  1054. int a_mv_y = 0;
  1055. int a_ref_idx = 0;
  1056. int b_mv_y = 0;
  1057. int b_ref_idx = 0;
  1058. int img_block_y = currMB->block_y;
  1059. VideoParameters *p_Vid = currMB->p_Vid;
  1060. Slice *currSlice = currMB->p_Slice;
  1061. int list_offset = ((currSlice->mb_aff_frame_flag) && (currMB->mb_field)) ? (currMB->mbAddrX & 0x01) ? 4 : 2 : 0;
  1062. StorablePicture *dec_picture = p_Vid->dec_picture;
  1063. PicMotionParams *motion = &dec_picture->motion;
  1064. short *a_mv = NULL;
  1065. short *b_mv = NULL;
  1066. get_neighbors0016(currMB, mb);
  1067. if (mb[0].available)
  1068. {
  1069. a_mv = motion->motion[LIST_0][mb[0].pos_y][mb[0].pos_x].mv;
  1070. a_mv_y = a_mv[1];
  1071. a_ref_idx = motion->motion[LIST_0][mb[0].pos_y][mb[0].pos_x].ref_idx;
  1072. if (currMB->mb_field && !p_Vid->mb_data[mb[0].mb_addr].mb_field)
  1073. {
  1074. a_mv_y /=2;
  1075. a_ref_idx *=2;
  1076. }
  1077. if (!currMB->mb_field && p_Vid->mb_data[mb[0].mb_addr].mb_field)
  1078. {
  1079. a_mv_y *=2;
  1080. a_ref_idx >>=1;
  1081. }
  1082. }
  1083. if (mb[1].available)
  1084. {
  1085. b_mv = motion->motion[LIST_0][mb[1].pos_y][mb[1].pos_x].mv;
  1086. b_mv_y = b_mv[1];
  1087. b_ref_idx = motion->motion[LIST_0][mb[1].pos_y][mb[1].pos_x].ref_idx;
  1088. if (currMB->mb_field && !p_Vid->mb_data[mb[1].mb_addr].mb_field)
  1089. {
  1090. b_mv_y /=2;
  1091. b_ref_idx *=2;
  1092. }
  1093. if (!currMB->mb_field && p_Vid->mb_data[mb[1].mb_addr].mb_field)
  1094. {
  1095. b_mv_y *=2;
  1096. b_ref_idx >>=1;
  1097. }
  1098. }
  1099. zeroMotionLeft = !mb[0].available ? 1 : a_ref_idx==0 && a_mv[0]==0 && a_mv_y==0 ? 1 : 0;
  1100. zeroMotionAbove = !mb[1].available ? 1 : b_ref_idx==0 && b_mv[0]==0 && b_mv_y==0 ? 1 : 0;
  1101. currMB->cbp = 0;
  1102. reset_coeffs(currSlice);
  1103. if (zeroMotionAbove || zeroMotionLeft)
  1104. {
  1105. for(j = img_block_y; j < img_block_y + BLOCK_SIZE; ++j)
  1106. {
  1107. for(i=currMB->block_x;i<currMB->block_x + BLOCK_SIZE; ++i)
  1108. {
  1109. memset(&motion->motion[LIST_0][j][i].mv, 0, sizeof(MotionVector));
  1110. motion->motion[LIST_0][j][i].ref_idx=0;
  1111. motion->motion[LIST_0][j][i].ref_pic_id = dec_picture->ref_pic_num[p_Vid->current_slice_nr][LIST_0 + list_offset][0];
  1112. }
  1113. }
  1114. }
  1115. else
  1116. {
  1117. currMB->GetMVPredictor (currMB, mb, pred_mv, 0, motion->motion[LIST_0], 0, 0, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
  1118. // Set first block line (position img_block_y)
  1119. for(j=img_block_y; j < img_block_y + BLOCK_SIZE; ++j)
  1120. {
  1121. for(i=currMB->block_x;i<currMB->block_x + BLOCK_SIZE; ++i)
  1122. {
  1123. memcpy(&motion->motion[LIST_0][j][i].mv, pred_mv, sizeof(MotionVector));
  1124. motion->motion[LIST_0][j][i].ref_idx=0;
  1125. motion->motion[LIST_0][j][i].ref_pic_id = dec_picture->ref_pic_num[p_Vid->current_slice_nr][LIST_0 + list_offset][0];
  1126. }
  1127. }
  1128. }
  1129. }
  1130. static void concealIPCMcoeffs(Macroblock *currMB)
  1131. {
  1132. Slice *currSlice = currMB->p_Slice;
  1133. VideoParameters *p_Vid = currMB->p_Vid;
  1134. StorablePicture *dec_picture = p_Vid->dec_picture;
  1135. int i, j, k;
  1136. for(i=0;i<MB_BLOCK_SIZE;++i)
  1137. {
  1138. for(j=0;j<MB_BLOCK_SIZE;++j)
  1139. {
  1140. currSlice->ipcm[0][i][j] = p_Vid->dc_pred_value_comp[0];
  1141. }
  1142. }
  1143. if ((dec_picture->chroma_format_idc != YUV400) && !IS_INDEPENDENT(p_Vid))
  1144. {
  1145. for (k = 0; k < 2; ++k)
  1146. {
  1147. for(i=0;i<p_Vid->mb_cr_size_y;++i)
  1148. {
  1149. for(j=0;j<p_Vid->mb_cr_size_x;++j)
  1150. {
  1151. currSlice->ipcm[k][i][j] = p_Vid->dc_pred_value_comp[k];
  1152. }
  1153. }
  1154. }
  1155. }
  1156. }
  1157. /*!
  1158. ************************************************************************
  1159. * \brief
  1160. * Get the syntax elements from the NAL
  1161. ************************************************************************
  1162. */
  1163. static void read_one_macroblock_i_slice(Macroblock *currMB)
  1164. {
  1165. Slice *currSlice = currMB->p_Slice;
  1166. VideoParameters *p_Vid = currMB->p_Vid;
  1167. SyntaxElement currSE;
  1168. int mb_nr = currMB->mbAddrX;
  1169. DataPartition *dP;
  1170. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  1171. StorablePicture *dec_picture = p_Vid->dec_picture;
  1172. PicMotionParams *motion = &dec_picture->motion;
  1173. currMB->mb_field = ((mb_nr&0x01) == 0)? FALSE : p_Vid->mb_data[mb_nr-1].mb_field;
  1174. update_qp(currMB, p_Vid->qp);
  1175. // read MB mode *****************************************************************
  1176. dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
  1177. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  1178. currSE.mapping = linfo_ue;
  1179. // read MB aff
  1180. if (currSlice->mb_aff_frame_flag && (mb_nr&0x01)==0)
  1181. {
  1182. TRACE_STRING("mb_field_decoding_flag");
  1183. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  1184. {
  1185. currMB->mb_field = readSyntaxElement_FLC(dP->bitstream, 1);
  1186. }
  1187. else
  1188. {
  1189. currMB->mb_field = readFieldModeInfo_CABAC(currMB, &dP->de_cabac);
  1190. }
  1191. }
  1192. if(p_Vid->active_pps->entropy_coding_mode_flag == CABAC)
  1193. {
  1194. CheckAvailabilityOfNeighborsCABAC(currMB);
  1195. // read MB type
  1196. currMB->mb_type = readMB_typeInfo_CABAC(currMB, &dP->de_cabac);
  1197. }
  1198. else
  1199. { // CAVLC
  1200. // read MB type
  1201. readSyntaxElement_UVLC(&currSE, dP);
  1202. currMB->mb_type = currSE.value1;
  1203. }
  1204. currMB->ei_flag = 0;
  1205. motion->mb_field[mb_nr] = (byte) currMB->mb_field;
  1206. currMB->block_y_aff = ((currSlice->mb_aff_frame_flag) && (currMB->mb_field)) ? (mb_nr&0x01) ? (currMB->block_y - 4)>>1 : currMB->block_y >> 1 : currMB->block_y;
  1207. p_Vid->siblock[currMB->mb_y][currMB->mb_x] = 0;
  1208. currSlice->interpret_mb_mode(currMB);
  1209. //init NoMbPartLessThan8x8Flag
  1210. currMB->NoMbPartLessThan8x8Flag = TRUE;
  1211. //============= Transform Size Flag for INTRA MBs =============
  1212. //-------------------------------------------------------------
  1213. //transform size flag for INTRA_4x4 and INTRA_8x8 modes
  1214. if (currMB->mb_type == I4MB && p_Vid->Transform8x8Mode)
  1215. {
  1216. dP = &(currSlice->partArr[partMap[SE_HEADER]]);
  1217. TRACE_STRING("transform_size_8x8_flag");
  1218. // read CAVLC transform_size_8x8_flag
  1219. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  1220. {
  1221. currMB->luma_transform_size_8x8_flag = readSyntaxElement_FLC(dP->bitstream, 1);
  1222. }
  1223. else
  1224. {
  1225. currMB->luma_transform_size_8x8_flag = readMB_transform_size_flag_CABAC(currMB, &dP->de_cabac);
  1226. }
  1227. if (currMB->luma_transform_size_8x8_flag)
  1228. {
  1229. currMB->mb_type = I8MB;
  1230. memset(&currMB->b8mode, I8MB, 4 * sizeof(char));
  1231. memset(&currMB->b8pdir, -1, 4 * sizeof(char));
  1232. }
  1233. }
  1234. else
  1235. {
  1236. currMB->luma_transform_size_8x8_flag = FALSE;
  1237. }
  1238. //--- init macroblock data ---
  1239. init_macroblock(currMB);
  1240. if(currMB->mb_type != IPCM)
  1241. {
  1242. // intra prediction modes for a macroblock 4x4 **********************************************
  1243. read_ipred_modes(currMB);
  1244. // read CBP and Coeffs ***************************************************************
  1245. currSlice->read_CBP_and_coeffs_from_NAL (currMB);
  1246. }
  1247. else
  1248. {
  1249. //read pcm_alignment_zero_bit and pcm_byte[i]
  1250. // here dP is assigned with the same dP as SE_MBTYPE, because IPCM syntax is in the
  1251. // same category as MBTYPE
  1252. if ( currSlice->dp_mode && currSlice->dpB_NotPresent )
  1253. {
  1254. concealIPCMcoeffs(currMB);
  1255. }
  1256. else
  1257. {
  1258. dP = &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]);
  1259. read_IPCM_coeffs_from_NAL(currSlice, dP);
  1260. }
  1261. }
  1262. return;
  1263. }
  1264. /*!
  1265. ************************************************************************
  1266. * \brief
  1267. * Get the syntax elements from the NAL
  1268. ************************************************************************
  1269. */
  1270. static void read_one_macroblock_p_slice(Macroblock *currMB)
  1271. {
  1272. Slice *currSlice = currMB->p_Slice;
  1273. VideoParameters *p_Vid = currMB->p_Vid;
  1274. int i;
  1275. SyntaxElement currSE;
  1276. int mb_nr = currMB->mbAddrX;
  1277. DataPartition *dP;
  1278. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  1279. Macroblock *topMB = NULL;
  1280. int prevMbSkipped = 0;
  1281. int check_bottom, read_bottom, read_top;
  1282. StorablePicture *dec_picture = p_Vid->dec_picture;
  1283. PicMotionParams *motion = &dec_picture->motion;
  1284. if (currSlice->mb_aff_frame_flag)
  1285. {
  1286. if (mb_nr&0x01)
  1287. {
  1288. topMB= &p_Vid->mb_data[mb_nr-1];
  1289. prevMbSkipped = (topMB->mb_type == 0);
  1290. }
  1291. else
  1292. prevMbSkipped = 0;
  1293. }
  1294. currMB->mb_field = ((mb_nr&0x01) == 0)? FALSE : p_Vid->mb_data[mb_nr-1].mb_field;
  1295. update_qp(currMB, p_Vid->qp);
  1296. // read MB mode *****************************************************************
  1297. dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
  1298. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  1299. currSE.mapping = linfo_ue;
  1300. if (p_Vid->active_pps->entropy_coding_mode_flag == CABAC)
  1301. {
  1302. int skip;
  1303. // read MB skip_flag
  1304. if (currSlice->mb_aff_frame_flag && ((mb_nr&0x01) == 0||prevMbSkipped))
  1305. field_flag_inference(currMB);
  1306. CheckAvailabilityOfNeighborsCABAC(currMB);
  1307. TRACE_STRING("mb_skip_flag");
  1308. skip = readMB_skip_flagInfo_CABAC(currMB, &dP->de_cabac);
  1309. currMB->mb_type = !skip;
  1310. currMB->skip_flag = skip;
  1311. currMB->ei_flag = 0;
  1312. // read MB AFF
  1313. if (currSlice->mb_aff_frame_flag)
  1314. {
  1315. check_bottom=read_bottom=read_top=0;
  1316. if ((mb_nr&0x01)==0)
  1317. {
  1318. check_bottom = currMB->skip_flag;
  1319. read_top = !check_bottom;
  1320. }
  1321. else
  1322. {
  1323. read_bottom = (topMB->skip_flag && (!currMB->skip_flag));
  1324. }
  1325. if (read_bottom || read_top)
  1326. {
  1327. TRACE_STRING("mb_field_decoding_flag");
  1328. currMB->mb_field = readFieldModeInfo_CABAC(currMB, &dP->de_cabac);
  1329. }
  1330. if (check_bottom)
  1331. check_next_mb_and_get_field_mode_CABAC(currSlice, dP);
  1332. CheckAvailabilityOfNeighborsCABAC(currMB);
  1333. }
  1334. // read MB type
  1335. if (currMB->mb_type != 0 )
  1336. {
  1337. TRACE_STRING("mb_type");
  1338. currMB->mb_type = readMB_typeInfo_CABAC(currMB, &dP->de_cabac);
  1339. currMB->ei_flag = 0;
  1340. }
  1341. }
  1342. // VLC Non-Intra
  1343. else
  1344. {
  1345. if(p_Vid->cod_counter == -1)
  1346. {
  1347. TRACE_STRING("mb_skip_run");
  1348. readSyntaxElement_UVLC(&currSE, dP);
  1349. p_Vid->cod_counter = currSE.value1;
  1350. }
  1351. if (p_Vid->cod_counter==0)
  1352. {
  1353. // read MB aff
  1354. if ((currSlice->mb_aff_frame_flag) && (((mb_nr&0x01)==0) || ((mb_nr&0x01) && prevMbSkipped)))
  1355. {
  1356. TRACE_STRING("mb_field_decoding_flag");
  1357. currMB->mb_field = (Boolean) readSyntaxElement_FLC(dP->bitstream, 1);
  1358. }
  1359. // read MB type
  1360. TRACE_STRING("mb_type");
  1361. readSyntaxElement_UVLC(&currSE, dP);
  1362. if(currSlice->slice_type == P_SLICE || currSlice->slice_type == SP_SLICE)
  1363. ++(currSE.value1);
  1364. currMB->mb_type = currSE.value1;
  1365. currMB->ei_flag = 0;
  1366. p_Vid->cod_counter--;
  1367. currMB->skip_flag = 0;
  1368. }
  1369. else
  1370. {
  1371. p_Vid->cod_counter--;
  1372. currMB->mb_type = 0;
  1373. currMB->ei_flag = 0;
  1374. currMB->skip_flag = 1;
  1375. // read field flag of bottom block
  1376. if(currSlice->mb_aff_frame_flag)
  1377. {
  1378. if(p_Vid->cod_counter == 0 && ((mb_nr&0x01) == 0))
  1379. {
  1380. TRACE_STRING("mb_field_decoding_flag (of coded bottom mb)");
  1381. currMB->mb_field = (Boolean) readSyntaxElement_FLC(dP->bitstream, 1);
  1382. dP->bitstream->frame_bitoffset--;
  1383. TRACE_DECBITS(1);
  1384. }
  1385. else if (p_Vid->cod_counter > 0 && ((mb_nr & 0x01) == 0))
  1386. {
  1387. // check left macroblock pair first
  1388. if (mb_is_available(mb_nr - 2, currMB) && ((mb_nr % (p_Vid->PicWidthInMbs * 2))!=0))
  1389. {
  1390. currMB->mb_field = p_Vid->mb_data[mb_nr-2].mb_field;
  1391. }
  1392. else
  1393. {
  1394. // check top macroblock pair
  1395. if (mb_is_available(mb_nr - 2*p_Vid->PicWidthInMbs, currMB))
  1396. {
  1397. currMB->mb_field = p_Vid->mb_data[mb_nr-2*p_Vid->PicWidthInMbs].mb_field;
  1398. }
  1399. else
  1400. currMB->mb_field = FALSE;
  1401. }
  1402. }
  1403. }
  1404. }
  1405. }
  1406. motion->mb_field[mb_nr] = (byte) currMB->mb_field;
  1407. currMB->block_y_aff = ((currSlice->mb_aff_frame_flag) && (currMB->mb_field)) ? (mb_nr&0x01) ? (currMB->block_y - 4)>>1 : currMB->block_y >> 1 : currMB->block_y;
  1408. p_Vid->siblock[currMB->mb_y][currMB->mb_x] = 0;
  1409. currSlice->interpret_mb_mode(currMB);
  1410. if(currSlice->mb_aff_frame_flag)
  1411. {
  1412. if(currMB->mb_field)
  1413. {
  1414. currSlice->num_ref_idx_l0_active <<=1;
  1415. currSlice->num_ref_idx_l1_active <<=1;
  1416. }
  1417. }
  1418. //init NoMbPartLessThan8x8Flag
  1419. currMB->NoMbPartLessThan8x8Flag = (IS_DIRECT(currMB) && !(p_Vid->active_sps->direct_8x8_inference_flag))? FALSE: TRUE;
  1420. //====== READ 8x8 SUB-PARTITION MODES (modes of 8x8 blocks) and Intra VBST block modes ======
  1421. if (currMB->mb_type == P8x8)
  1422. {
  1423. dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
  1424. if (p_Vid->active_pps->entropy_coding_mode_flag ==CAVLC)
  1425. {
  1426. currSE.mapping = linfo_ue;
  1427. for (i = 0; i < 4; ++i)
  1428. {
  1429. TRACE_STRING("sub_mb_type");
  1430. readSyntaxElement_UVLC(&currSE, dP);
  1431. SetB8Mode (currMB, currSE.value1, i);
  1432. //set NoMbPartLessThan8x8Flag for P8x8 mode
  1433. currMB->NoMbPartLessThan8x8Flag &= (currMB->b8mode[i]==0 && p_Vid->active_sps->direct_8x8_inference_flag) ||
  1434. (currMB->b8mode[i]==4);
  1435. }
  1436. }
  1437. else
  1438. {
  1439. for (i = 0; i < 4; ++i)
  1440. {
  1441. int value = readB8_typeInfo_CABAC(currSlice, &dP->de_cabac);
  1442. SetB8Mode (currMB, value, i);
  1443. //set NoMbPartLessThan8x8Flag for P8x8 mode
  1444. currMB->NoMbPartLessThan8x8Flag &= (currMB->b8mode[i]==0 && p_Vid->active_sps->direct_8x8_inference_flag) ||
  1445. (currMB->b8mode[i]==4);
  1446. }
  1447. }
  1448. //--- init macroblock data ---
  1449. init_macroblock (currMB);
  1450. currSlice->read_motion_info_from_NAL (currMB);
  1451. }
  1452. //============= Transform Size Flag for INTRA MBs =============
  1453. //-------------------------------------------------------------
  1454. //transform size flag for INTRA_4x4 and INTRA_8x8 modes
  1455. if (currMB->mb_type == I4MB && p_Vid->Transform8x8Mode)
  1456. {
  1457. dP = &(currSlice->partArr[partMap[SE_HEADER]]);
  1458. TRACE_STRING("transform_size_8x8_flag");
  1459. // read CAVLC transform_size_8x8_flag
  1460. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  1461. {
  1462. currMB->luma_transform_size_8x8_flag = (Boolean) readSyntaxElement_FLC(dP->bitstream, 1);
  1463. }
  1464. else
  1465. {
  1466. currMB->luma_transform_size_8x8_flag = readMB_transform_size_flag_CABAC(currMB, &dP->de_cabac);
  1467. }
  1468. if (currMB->luma_transform_size_8x8_flag)
  1469. {
  1470. currMB->mb_type = I8MB;
  1471. memset(&currMB->b8mode, I8MB, 4 * sizeof(char));
  1472. memset(&currMB->b8pdir, -1, 4 * sizeof(char));
  1473. }
  1474. }
  1475. else
  1476. {
  1477. currMB->luma_transform_size_8x8_flag = FALSE;
  1478. }
  1479. if(p_Vid->active_pps->constrained_intra_pred_flag)
  1480. {
  1481. if( !IS_INTRA(currMB) )
  1482. {
  1483. p_Vid->intra_block[mb_nr] = 0;
  1484. }
  1485. }
  1486. //--- init macroblock data ---
  1487. if (currMB->mb_type != P8x8)
  1488. init_macroblock(currMB);
  1489. if (IS_SKIP (currMB)) //keep last macroblock
  1490. {
  1491. skip_macroblock(currMB);
  1492. }
  1493. else if(currMB->mb_type != IPCM)
  1494. {
  1495. // intra prediction modes for a macroblock 4x4 **********************************************
  1496. if (IS_INTRA(currMB))
  1497. read_ipred_modes(currMB);
  1498. // read inter frame vector data *********************************************************
  1499. if (IS_INTERMV (currMB) && (currMB->mb_type != P8x8))
  1500. {
  1501. currSlice->read_motion_info_from_NAL (currMB);
  1502. }
  1503. // read CBP and Coeffs ***************************************************************
  1504. currSlice->read_CBP_and_coeffs_from_NAL (currMB);
  1505. }
  1506. else
  1507. {
  1508. //read pcm_alignment_zero_bit and pcm_byte[i]
  1509. // here dP is assigned with the same dP as SE_MBTYPE, because IPCM syntax is in the
  1510. // same category as MBTYPE
  1511. if ( currSlice->dp_mode && currSlice->dpB_NotPresent )
  1512. {
  1513. concealIPCMcoeffs(currMB);
  1514. }
  1515. else
  1516. {
  1517. dP = &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]);
  1518. read_IPCM_coeffs_from_NAL(currSlice, dP);
  1519. }
  1520. }
  1521. return;
  1522. }
  1523. /*!
  1524. ************************************************************************
  1525. * \brief
  1526. * Get the syntax elements from the NAL
  1527. ************************************************************************
  1528. */
  1529. static void read_one_macroblock_b_slice(Macroblock *currMB)
  1530. {
  1531. Slice *currSlice = currMB->p_Slice;
  1532. VideoParameters *p_Vid = currMB->p_Vid;
  1533. int i;
  1534. SyntaxElement currSE;
  1535. int mb_nr = currMB->mbAddrX;
  1536. DataPartition *dP;
  1537. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  1538. Macroblock *topMB = NULL;
  1539. int prevMbSkipped = 0;
  1540. int check_bottom, read_bottom, read_top;
  1541. StorablePicture *dec_picture = p_Vid->dec_picture;
  1542. PicMotionParams *motion = &dec_picture->motion;
  1543. if (currSlice->mb_aff_frame_flag)
  1544. {
  1545. if (mb_nr&0x01)
  1546. {
  1547. topMB= &p_Vid->mb_data[mb_nr-1];
  1548. prevMbSkipped = topMB->skip_flag;
  1549. }
  1550. else
  1551. prevMbSkipped = 0;
  1552. }
  1553. currMB->mb_field = ((mb_nr&0x01) == 0)? FALSE : p_Vid->mb_data[mb_nr-1].mb_field;
  1554. update_qp(currMB, p_Vid->qp);
  1555. // read MB mode *****************************************************************
  1556. dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
  1557. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  1558. currSE.mapping = linfo_ue;
  1559. if (p_Vid->active_pps->entropy_coding_mode_flag == CABAC)
  1560. {
  1561. // read MB skip_flag
  1562. int skip;
  1563. if (currSlice->mb_aff_frame_flag && ((mb_nr&0x01) == 0||prevMbSkipped))
  1564. field_flag_inference(currMB);
  1565. CheckAvailabilityOfNeighborsCABAC(currMB);
  1566. TRACE_STRING("mb_skip_flag");
  1567. skip = readMB_skip_flagInfo_CABAC(currMB, &dP->de_cabac);
  1568. currMB->mb_type = !skip;
  1569. currMB->skip_flag = skip;
  1570. currMB->cbp = !skip;
  1571. currMB->ei_flag = 0;
  1572. if (skip)
  1573. p_Vid->cod_counter=0;
  1574. // read MB AFF
  1575. if (currSlice->mb_aff_frame_flag)
  1576. {
  1577. check_bottom=read_bottom=read_top=0;
  1578. if ((mb_nr&0x01)==0)
  1579. {
  1580. check_bottom = currMB->skip_flag;
  1581. read_top = !check_bottom;
  1582. }
  1583. else
  1584. {
  1585. read_bottom = (topMB->skip_flag && (!currMB->skip_flag));
  1586. }
  1587. if (read_bottom || read_top)
  1588. {
  1589. TRACE_STRING("mb_field_decoding_flag");
  1590. currMB->mb_field = readFieldModeInfo_CABAC(currMB, &dP->de_cabac);
  1591. }
  1592. if (check_bottom)
  1593. check_next_mb_and_get_field_mode_CABAC(currSlice,dP);
  1594. CheckAvailabilityOfNeighborsCABAC(currMB);
  1595. }
  1596. // read MB type
  1597. if (currMB->mb_type != 0 )
  1598. {
  1599. TRACE_STRING("mb_type");
  1600. currMB->mb_type = readMB_typeInfo_CABAC(currMB, &dP->de_cabac);
  1601. currMB->ei_flag = 0;
  1602. }
  1603. }
  1604. // VLC Non-Intra
  1605. else
  1606. {
  1607. if(p_Vid->cod_counter == -1)
  1608. {
  1609. TRACE_STRING("mb_skip_run");
  1610. readSyntaxElement_UVLC(&currSE, dP);
  1611. p_Vid->cod_counter = currSE.value1;
  1612. }
  1613. if (p_Vid->cod_counter==0)
  1614. {
  1615. // read MB aff
  1616. if ((currSlice->mb_aff_frame_flag) && (((mb_nr&0x01)==0) || ((mb_nr&0x01) && prevMbSkipped)))
  1617. {
  1618. TRACE_STRING("mb_field_decoding_flag");
  1619. currMB->mb_field = (Boolean) readSyntaxElement_FLC(dP->bitstream, 1);
  1620. }
  1621. // read MB type
  1622. TRACE_STRING("mb_type");
  1623. readSyntaxElement_UVLC(&currSE, dP);
  1624. if(currSlice->slice_type == P_SLICE || currSlice->slice_type == SP_SLICE)
  1625. ++(currSE.value1);
  1626. currMB->mb_type = currSE.value1;
  1627. currMB->ei_flag = 0;
  1628. p_Vid->cod_counter--;
  1629. currMB->skip_flag = 0;
  1630. }
  1631. else
  1632. {
  1633. p_Vid->cod_counter--;
  1634. currMB->mb_type = 0;
  1635. currMB->ei_flag = 0;
  1636. currMB->skip_flag = 1;
  1637. // read field flag of bottom block
  1638. if(currSlice->mb_aff_frame_flag)
  1639. {
  1640. if(p_Vid->cod_counter == 0 && ((mb_nr&0x01) == 0))
  1641. {
  1642. TRACE_STRING("mb_field_decoding_flag (of coded bottom mb)");
  1643. currMB->mb_field = (Boolean) readSyntaxElement_FLC(dP->bitstream, 1);
  1644. dP->bitstream->frame_bitoffset--;
  1645. TRACE_DECBITS(1);
  1646. }
  1647. else if (p_Vid->cod_counter > 0 && ((mb_nr & 0x01) == 0))
  1648. {
  1649. // check left macroblock pair first
  1650. if (mb_is_available(mb_nr - 2, currMB) && ((mb_nr % (p_Vid->PicWidthInMbs * 2))!=0))
  1651. {
  1652. currMB->mb_field = p_Vid->mb_data[mb_nr-2].mb_field;
  1653. }
  1654. else
  1655. {
  1656. // check top macroblock pair
  1657. if (mb_is_available(mb_nr - 2*p_Vid->PicWidthInMbs, currMB))
  1658. {
  1659. currMB->mb_field = p_Vid->mb_data[mb_nr-2*p_Vid->PicWidthInMbs].mb_field;
  1660. }
  1661. else
  1662. currMB->mb_field = FALSE;
  1663. }
  1664. }
  1665. }
  1666. }
  1667. }
  1668. motion->mb_field[mb_nr] = (byte) currMB->mb_field;
  1669. currMB->block_y_aff = ((currSlice->mb_aff_frame_flag) && (currMB->mb_field)) ? (mb_nr&0x01) ? (currMB->block_y - 4)>>1 : currMB->block_y >> 1 : currMB->block_y;
  1670. p_Vid->siblock[currMB->mb_y][currMB->mb_x] = 0;
  1671. currSlice->interpret_mb_mode(currMB);
  1672. if(currSlice->mb_aff_frame_flag)
  1673. {
  1674. if(currMB->mb_field)
  1675. {
  1676. currSlice->num_ref_idx_l0_active <<=1;
  1677. currSlice->num_ref_idx_l1_active <<=1;
  1678. }
  1679. }
  1680. //init NoMbPartLessThan8x8Flag
  1681. currMB->NoMbPartLessThan8x8Flag = (IS_DIRECT(currMB) && !(p_Vid->active_sps->direct_8x8_inference_flag))? FALSE: TRUE;
  1682. //====== READ 8x8 SUB-PARTITION MODES (modes of 8x8 blocks) and Intra VBST block modes ======
  1683. if (currMB->mb_type == P8x8)
  1684. {
  1685. dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
  1686. if (p_Vid->active_pps->entropy_coding_mode_flag ==CAVLC)
  1687. {
  1688. currSE.mapping = linfo_ue;
  1689. for (i = 0; i < 4; ++i)
  1690. {
  1691. TRACE_STRING("sub_mb_type");
  1692. readSyntaxElement_UVLC(&currSE, dP);
  1693. SetB8Mode (currMB, currSE.value1, i);
  1694. //set NoMbPartLessThan8x8Flag for P8x8 mode
  1695. currMB->NoMbPartLessThan8x8Flag &= (currMB->b8mode[i]==0 && p_Vid->active_sps->direct_8x8_inference_flag) ||
  1696. (currMB->b8mode[i]==4);
  1697. }
  1698. }
  1699. else
  1700. {
  1701. for (i = 0; i < 4; ++i)
  1702. {
  1703. int value = readB8_typeInfo_CABAC(currSlice, &dP->de_cabac);
  1704. SetB8Mode (currMB, value, i);
  1705. //set NoMbPartLessThan8x8Flag for P8x8 mode
  1706. currMB->NoMbPartLessThan8x8Flag &= (currMB->b8mode[i]==0 && p_Vid->active_sps->direct_8x8_inference_flag) ||
  1707. (currMB->b8mode[i]==4);
  1708. }
  1709. }
  1710. //--- init macroblock data ---
  1711. init_macroblock (currMB);
  1712. currSlice->read_motion_info_from_NAL (currMB);
  1713. }
  1714. //============= Transform Size Flag for INTRA MBs =============
  1715. //-------------------------------------------------------------
  1716. //transform size flag for INTRA_4x4 and INTRA_8x8 modes
  1717. if (currMB->mb_type == I4MB && p_Vid->Transform8x8Mode)
  1718. {
  1719. dP = &(currSlice->partArr[partMap[SE_HEADER]]);
  1720. TRACE_STRING("transform_size_8x8_flag");
  1721. // read CAVLC transform_size_8x8_flag
  1722. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  1723. {
  1724. currMB->luma_transform_size_8x8_flag = (Boolean) readSyntaxElement_FLC(dP->bitstream, 1);
  1725. }
  1726. else
  1727. {
  1728. currMB->luma_transform_size_8x8_flag = readMB_transform_size_flag_CABAC(currMB, &dP->de_cabac);
  1729. }
  1730. if (currMB->luma_transform_size_8x8_flag)
  1731. {
  1732. currMB->mb_type = I8MB;
  1733. memset(&currMB->b8mode, I8MB, 4 * sizeof(char));
  1734. memset(&currMB->b8pdir, -1, 4 * sizeof(char));
  1735. }
  1736. }
  1737. else
  1738. {
  1739. currMB->luma_transform_size_8x8_flag = FALSE;
  1740. }
  1741. if(p_Vid->active_pps->constrained_intra_pred_flag) // inter frame
  1742. {
  1743. if( !IS_INTRA(currMB) )
  1744. {
  1745. p_Vid->intra_block[mb_nr] = 0;
  1746. }
  1747. }
  1748. //--- init macroblock data ---
  1749. if (currMB->mb_type != P8x8)
  1750. init_macroblock(currMB);
  1751. if (IS_DIRECT (currMB) && p_Vid->cod_counter >= 0)
  1752. {
  1753. currMB->cbp = 0;
  1754. reset_coeffs(currSlice);
  1755. if (p_Vid->active_pps->entropy_coding_mode_flag ==CABAC)
  1756. p_Vid->cod_counter=-1;
  1757. }
  1758. else if (IS_SKIP (currMB)) //keep last macroblock
  1759. {
  1760. skip_macroblock(currMB);
  1761. }
  1762. else if(currMB->mb_type != IPCM)
  1763. {
  1764. // intra prediction modes for a macroblock 4x4 **********************************************
  1765. if (IS_INTRA(currMB))
  1766. read_ipred_modes(currMB);
  1767. // read inter frame vector data *********************************************************
  1768. if (IS_INTERMV (currMB) && (currMB->mb_type != P8x8))
  1769. {
  1770. currSlice->read_motion_info_from_NAL (currMB);
  1771. }
  1772. // read CBP and Coeffs ***************************************************************
  1773. currSlice->read_CBP_and_coeffs_from_NAL (currMB);
  1774. }
  1775. else
  1776. {
  1777. //read pcm_alignment_zero_bit and pcm_byte[i]
  1778. // here dP is assigned with the same dP as SE_MBTYPE, because IPCM syntax is in the
  1779. // same category as MBTYPE
  1780. if ( currSlice->dp_mode && currSlice->dpB_NotPresent )
  1781. {
  1782. concealIPCMcoeffs(currMB);
  1783. }
  1784. else
  1785. {
  1786. dP = &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]);
  1787. read_IPCM_coeffs_from_NAL(currSlice, dP);
  1788. }
  1789. }
  1790. return;
  1791. }
  1792. /*!
  1793. ************************************************************************
  1794. * \brief
  1795. * Initialize decoding engine after decoding an IPCM macroblock
  1796. * (for IPCM CABAC 28/11/2003)
  1797. *
  1798. * \author
  1799. * Dong Wang <[email protected]>
  1800. ************************************************************************
  1801. */
  1802. static void init_decoding_engine_IPCM(Slice *currSlice)
  1803. {
  1804. Bitstream *currStream;
  1805. int ByteStartPosition;
  1806. int PartitionNumber;
  1807. int i;
  1808. if(currSlice->dp_mode==PAR_DP_1)
  1809. PartitionNumber=1;
  1810. else if(currSlice->dp_mode==PAR_DP_3)
  1811. PartitionNumber=3;
  1812. else
  1813. {
  1814. printf("Partition Mode is not supported\n");
  1815. exit(1);
  1816. }
  1817. for(i=0;i<PartitionNumber;++i)
  1818. {
  1819. currStream = currSlice->partArr[i].bitstream;
  1820. ByteStartPosition = currStream->read_len;
  1821. arideco_start_decoding (&currSlice->partArr[i].de_cabac, currStream->streamBuffer, ByteStartPosition, &currStream->read_len);
  1822. }
  1823. }
  1824. /*!
  1825. ************************************************************************
  1826. * \brief
  1827. * Read IPCM pcm_alignment_zero_bit and pcm_byte[i] from stream to currSlice->ipcm
  1828. * (for IPCM CABAC and IPCM CAVLC)
  1829. *
  1830. * \author
  1831. * Dong Wang <[email protected]>
  1832. ************************************************************************
  1833. */
  1834. static void read_IPCM_coeffs_from_NAL(Slice *currSlice, struct datapartition *dP)
  1835. {
  1836. VideoParameters *p_Vid = currSlice->p_Vid;
  1837. StorablePicture *dec_picture = p_Vid->dec_picture;
  1838. int i,j;
  1839. //For CABAC, we don't need to read bits to let stream byte aligned
  1840. // because we have variable for integer bytes position
  1841. if(p_Vid->active_pps->entropy_coding_mode_flag == CABAC)
  1842. {
  1843. readIPCM_CABAC(currSlice, dP);
  1844. init_decoding_engine_IPCM(currSlice);
  1845. }
  1846. else
  1847. {
  1848. //read bits to let stream byte aligned
  1849. if(((dP->bitstream->frame_bitoffset) & 0x07) != 0)
  1850. {
  1851. TRACE_STRING("pcm_alignment_zero_bit");
  1852. readSyntaxElement_FLC(dP->bitstream, (8 - ((dP->bitstream->frame_bitoffset) & 0x07)));
  1853. }
  1854. //read luma and chroma IPCM coefficients
  1855. TRACE_STRING("pcm_sample_luma");
  1856. for(i=0;i<MB_BLOCK_SIZE;++i)
  1857. {
  1858. for(j=0;j<MB_BLOCK_SIZE;++j)
  1859. {
  1860. currSlice->ipcm[0][i][j] = readSyntaxElement_FLC(dP->bitstream, p_Vid->bitdepth_luma);
  1861. }
  1862. }
  1863. if ((dec_picture->chroma_format_idc != YUV400) && !IS_INDEPENDENT(p_Vid))
  1864. {
  1865. TRACE_STRING("pcm_sample_chroma (u)");
  1866. for(i=0;i<p_Vid->mb_cr_size_y;++i)
  1867. {
  1868. for(j=0;j<p_Vid->mb_cr_size_x;++j)
  1869. {
  1870. currSlice->ipcm[1][i][j] = readSyntaxElement_FLC(dP->bitstream, p_Vid->bitdepth_chroma);
  1871. }
  1872. }
  1873. TRACE_STRING("pcm_sample_chroma (v)");
  1874. for(i=0;i<p_Vid->mb_cr_size_y;++i)
  1875. {
  1876. for(j=0;j<p_Vid->mb_cr_size_x;++j)
  1877. {
  1878. currSlice->ipcm[2][i][j] = readSyntaxElement_FLC(dP->bitstream, p_Vid->bitdepth_chroma);
  1879. }
  1880. }
  1881. }
  1882. }
  1883. }
  1884. /*!
  1885. ************************************************************************
  1886. * \brief
  1887. * If data partition B is lost, conceal PCM sample values with DC.
  1888. *
  1889. ************************************************************************
  1890. */
  1891. static void __forceinline read_ipred_iblock(VideoParameters *p_Vid, Macroblock *currMB, Slice *currSlice, DataPartition *dP, int b8)
  1892. {
  1893. int i, j;
  1894. int mostProbableIntraPredMode;
  1895. int upIntraPredMode;
  1896. int leftIntraPredMode;
  1897. int bx, by, bi, bj;
  1898. SyntaxElement currSE;
  1899. int ts, ls;
  1900. PixelPos left_block, top_block;
  1901. int dec;
  1902. for(j=0;j<2;j++) //loop subblocks
  1903. {
  1904. by = (b8&2) + j;
  1905. bj = currMB->block_y + by;
  1906. for(i=0;i<2;i++)
  1907. {
  1908. int pred_mode;
  1909. bx = ((b8&1)<<1) + i;
  1910. bi = currMB->block_x + bx;
  1911. //get from stream
  1912. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  1913. {
  1914. readSyntaxElement_Intra4x4PredictionMode(&currSE, dP->bitstream);
  1915. pred_mode = currSE.value1;
  1916. }
  1917. else
  1918. {
  1919. pred_mode = readIntraPredMode_CABAC(currSlice, &dP->de_cabac);
  1920. }
  1921. p_Vid->getNeighbourXPLumaNB(currMB, (bx<<2) - 1, (by<<2), &left_block);
  1922. p_Vid->getNeighbourPXLumaNB(currMB, (bx<<2), (by<<2) - 1, &top_block );
  1923. //get from array and decode
  1924. if (p_Vid->active_pps->constrained_intra_pred_flag)
  1925. {
  1926. left_block.available = left_block.available ? p_Vid->intra_block[left_block.mb_addr] : 0;
  1927. top_block.available = top_block.available ? p_Vid->intra_block[top_block.mb_addr] : 0;
  1928. }
  1929. // !! KS: not sure if the following is still correct...
  1930. ts = ls = 0; // Check to see if the neighboring block is SI
  1931. if (currMB->mb_type == I4MB && currSlice->slice_type == SI_SLICE) // need support for MBINTLC1
  1932. {
  1933. if (left_block.available)
  1934. if (p_Vid->siblock [left_block.mb_addr / p_Vid->PicWidthInMbs][left_block.mb_addr % p_Vid->PicWidthInMbs])
  1935. ls=1;
  1936. if (top_block.available)
  1937. if (p_Vid->siblock [top_block.mb_addr / p_Vid->PicWidthInMbs][top_block.mb_addr % p_Vid->PicWidthInMbs])
  1938. ts=1;
  1939. }
  1940. upIntraPredMode = (top_block.available &&(ts == 0)) ? p_Vid->ipredmode[top_block.pos_y>>2 ][top_block.pos_x>>2 ] : -1;
  1941. leftIntraPredMode = (left_block.available &&(ls == 0)) ? p_Vid->ipredmode[left_block.pos_y>>2][left_block.pos_x>>2] : -1;
  1942. mostProbableIntraPredMode = (upIntraPredMode < 0 || leftIntraPredMode < 0) ? DC_PRED : upIntraPredMode < leftIntraPredMode ? upIntraPredMode : leftIntraPredMode;
  1943. dec = (pred_mode == -1) ? mostProbableIntraPredMode : pred_mode + (pred_mode >= mostProbableIntraPredMode);
  1944. p_Vid->ipredmode[bj][bi] = dec;
  1945. }
  1946. }
  1947. }
  1948. static void __forceinline read_ipred_i8mb(VideoParameters *p_Vid, Macroblock *currMB, Slice *currSlice, DataPartition *dP, int b8)
  1949. {
  1950. int mostProbableIntraPredMode;
  1951. int upIntraPredMode;
  1952. int leftIntraPredMode;
  1953. int bx, by, bi, bj;
  1954. int pred_mode;
  1955. SyntaxElement currSE;
  1956. int ts, ls;
  1957. PixelPos left_block, top_block;
  1958. int dec;
  1959. by = (b8&2);
  1960. bj = currMB->block_y + by;
  1961. bx = ((b8&1)<<1);
  1962. bi = currMB->block_x + bx;
  1963. //get from stream
  1964. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  1965. {
  1966. readSyntaxElement_Intra4x4PredictionMode(&currSE, dP->bitstream);
  1967. pred_mode = currSE.value1;
  1968. }
  1969. else
  1970. {
  1971. pred_mode = readIntraPredMode_CABAC(currSlice, &dP->de_cabac);
  1972. }
  1973. p_Vid->getNeighbourXPLumaNB(currMB, (bx<<2) - 1, (by<<2), &left_block);
  1974. p_Vid->getNeighbourPXLumaNB(currMB, (bx<<2), (by<<2) - 1, &top_block );
  1975. //get from array and decode
  1976. if (p_Vid->active_pps->constrained_intra_pred_flag)
  1977. {
  1978. left_block.available = left_block.available ? p_Vid->intra_block[left_block.mb_addr] : 0;
  1979. top_block.available = top_block.available ? p_Vid->intra_block[top_block.mb_addr] : 0;
  1980. }
  1981. // !! KS: not sure if the following is still correct...
  1982. ts = ls = 0; // Check to see if the neighboring block is SI
  1983. if (currMB->mb_type == I4MB && currSlice->slice_type == SI_SLICE) // need support for MBINTLC1
  1984. {
  1985. if (left_block.available)
  1986. if (p_Vid->siblock [left_block.mb_addr / p_Vid->PicWidthInMbs][left_block.mb_addr % p_Vid->PicWidthInMbs])
  1987. ls=1;
  1988. if (top_block.available)
  1989. if (p_Vid->siblock [top_block.mb_addr / p_Vid->PicWidthInMbs][top_block.mb_addr % p_Vid->PicWidthInMbs])
  1990. ts=1;
  1991. }
  1992. upIntraPredMode = (top_block.available &&(ts == 0)) ? p_Vid->ipredmode[top_block.pos_y>>2 ][top_block.pos_x>>2 ] : -1;
  1993. leftIntraPredMode = (left_block.available &&(ls == 0)) ? p_Vid->ipredmode[left_block.pos_y>>2][left_block.pos_x>>2] : -1;
  1994. mostProbableIntraPredMode = (upIntraPredMode < 0 || leftIntraPredMode < 0) ? DC_PRED : upIntraPredMode < leftIntraPredMode ? upIntraPredMode : leftIntraPredMode;
  1995. dec = (pred_mode == -1) ? mostProbableIntraPredMode : pred_mode + (pred_mode >= mostProbableIntraPredMode);
  1996. //set
  1997. p_Vid->ipredmode[bj][bi] = dec;
  1998. p_Vid->ipredmode[bj][bi+1] = dec;
  1999. p_Vid->ipredmode[bj+1][bi] = dec;
  2000. p_Vid->ipredmode[bj+1][bi+1] = dec;
  2001. }
  2002. static void read_ipred_modes(Macroblock *currMB)
  2003. {
  2004. int b8;
  2005. SyntaxElement currSE;
  2006. DataPartition *dP;
  2007. Slice *currSlice = currMB->p_Slice;
  2008. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  2009. VideoParameters *p_Vid = currMB->p_Vid;
  2010. StorablePicture *dec_picture = p_Vid->dec_picture;
  2011. char IntraChromaPredModeFlag = IS_INTRA(currMB);
  2012. dP = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
  2013. for(b8 = 0; b8 < 4; ++b8) //loop 8x8 blocks
  2014. {
  2015. if (currMB->b8mode[b8]==IBLOCK)
  2016. {
  2017. IntraChromaPredModeFlag = 1;
  2018. read_ipred_iblock(p_Vid, currMB, currSlice, dP, b8);
  2019. }
  2020. else if (currMB->b8mode[b8]==I8MB)
  2021. {
  2022. IntraChromaPredModeFlag = 1;
  2023. read_ipred_i8mb(p_Vid, currMB, currSlice, dP, b8);
  2024. }
  2025. }
  2026. if (IntraChromaPredModeFlag && (dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
  2027. {
  2028. TRACE_STRING("intra_chroma_pred_mode");
  2029. dP = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
  2030. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  2031. {
  2032. currSE.mapping = linfo_ue;
  2033. readSyntaxElement_UVLC(&currSE, dP);
  2034. currMB->c_ipred_mode = (char) currSE.value1;
  2035. }
  2036. else
  2037. {
  2038. currMB->c_ipred_mode = readCIPredMode_CABAC(currMB, &dP->de_cabac);
  2039. }
  2040. if (currMB->c_ipred_mode < DC_PRED_8 || currMB->c_ipred_mode > PLANE_8)
  2041. {
  2042. error("illegal chroma intra pred mode!\n", 600);
  2043. }
  2044. }
  2045. }
  2046. /*!
  2047. ************************************************************************
  2048. * \brief
  2049. * Get current block spatial neighbors
  2050. ************************************************************************
  2051. */
  2052. void get_neighbors(Macroblock *currMB, // <-- current Macroblock
  2053. PixelPos *block, // <--> neighbor blocks
  2054. int mb_x, // <-- block x position
  2055. int mb_y, // <-- block y position
  2056. int blockshape_x // <-- block width
  2057. )
  2058. {
  2059. VideoParameters *p_Vid = currMB->p_Vid;
  2060. int i;
  2061. p_Vid->getNeighbourXPLumaNB(currMB, mb_x - 1, mb_y , &block[0]); // left
  2062. p_Vid->getNeighbourPXLumaNB(currMB, mb_x, mb_y - 1, &block[1]); // up
  2063. p_Vid->getNeighbourPXLuma(currMB, mb_x + blockshape_x, mb_y - 1, &block[2]); // upper right
  2064. p_Vid->getNeighbourLuma(currMB, mb_x - 1, mb_y - 1, &block[3]); // upper left
  2065. for (i = 0; i < 4; i++)
  2066. {
  2067. block[i].pos_x >>= 2;
  2068. block[i].pos_y >>= 2;
  2069. }
  2070. if (mb_y > 0)
  2071. {
  2072. if (mb_x < 8) // first column of 8x8 blocks
  2073. {
  2074. if (mb_y == 8 )
  2075. {
  2076. if (blockshape_x == MB_BLOCK_SIZE)
  2077. block[2].available = 0;
  2078. }
  2079. else if (mb_x+blockshape_x == 8)
  2080. {
  2081. block[2].available = 0;
  2082. }
  2083. }
  2084. else if (mb_x + blockshape_x == MB_BLOCK_SIZE)
  2085. {
  2086. block[2].available = 0;
  2087. }
  2088. }
  2089. if (!block[2].available)
  2090. {
  2091. block[2] = block[3];
  2092. }
  2093. }
  2094. /* this version is for mb_x == 0, mb_y == 0 and blockshape_x == 16 */
  2095. void get_neighbors0016(Macroblock *currMB, // <-- current Macroblock
  2096. PixelPos *block // <--> neighbor blocks
  2097. )
  2098. {
  2099. VideoParameters *p_Vid = currMB->p_Vid;
  2100. int i;
  2101. p_Vid->getNeighbourLeftLuma(currMB, &block[0]); // left
  2102. p_Vid->getNeighbourPXLumaNB(currMB, 0, -1, &block[1]); // up
  2103. p_Vid->getNeighbourPXLuma(currMB, 16, -1, &block[2]); // upper right
  2104. p_Vid->getNeighbourLuma(currMB, -1, -1, &block[3]); // upper left
  2105. for (i = 0; i < 4; i++)
  2106. {
  2107. if (block[i].available)
  2108. {
  2109. block[i].pos_x >>= 2;
  2110. block[i].pos_y >>= 2;
  2111. }
  2112. }
  2113. if (!block[2].available)
  2114. {
  2115. block[2] = block[3];
  2116. }
  2117. }
  2118. /*!
  2119. ************************************************************************
  2120. * \brief
  2121. * Read motion info
  2122. ************************************************************************
  2123. */
  2124. static void read_motion_info_from_NAL_p_slice(Macroblock *currMB)
  2125. {
  2126. VideoParameters *p_Vid = currMB->p_Vid;
  2127. Slice *currSlice = currMB->p_Slice;
  2128. int mb_nr = currMB->mbAddrX;
  2129. DataPartition *dP = NULL;
  2130. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  2131. int partmode = ((currMB->mb_type == P8x8) ? 4 : currMB->mb_type);
  2132. int step_h0 = BLOCK_STEP [partmode][0];
  2133. int step_v0 = BLOCK_STEP [partmode][1];
  2134. h264_ref_t *pic_num;
  2135. int j4;
  2136. StorablePicture *dec_picture = p_Vid->dec_picture;
  2137. PicMotionParams *motion = &dec_picture->motion;
  2138. int list_offset = ((currSlice->mb_aff_frame_flag)&&(currMB->mb_field))? (mb_nr&0x01) ? 4 : 2 : 0;
  2139. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  2140. {
  2141. SyntaxElement currSE;
  2142. //===== READ REFERENCE PICTURE INDICES =====
  2143. dP = &(currSlice->partArr[partMap[SE_REFFRAME]]);
  2144. // For LIST_0, if multiple ref. pictures, read LIST_0 reference picture indices for the MB ***********
  2145. prepareListforRefIdx (currMB, &currSE, currSlice->num_ref_idx_l0_active, (currMB->mb_type != P8x8) || (!p_Vid->allrefzero));
  2146. readMBRefPictureIdx (&currSE, dP, currMB, &motion->motion[LIST_0][currMB->block_y], LIST_0, step_v0, step_h0);
  2147. // For LIST_1, if multiple ref. pictures, read LIST_1 reference picture indices for the MB ***********
  2148. prepareListforRefIdx (currMB, &currSE, currSlice->num_ref_idx_l1_active, (currMB->mb_type != P8x8) || (!p_Vid->allrefzero));
  2149. readMBRefPictureIdx (&currSE, dP, currMB, &motion->motion[LIST_1][currMB->block_y], LIST_1, step_v0, step_h0);
  2150. //===== READ MOTION VECTORS =====
  2151. dP = &(currSlice->partArr[partMap[SE_MVD]]);
  2152. currSE.mapping = linfo_se;
  2153. readMBMotionVectors (&currSE, dP, currMB, LIST_0, step_h0, step_v0);
  2154. }
  2155. else
  2156. {
  2157. if (currMB->mb_type != P8x8 || !p_Vid->allrefzero)
  2158. {
  2159. //===== READ REFERENCE PICTURE INDICES =====
  2160. dP = &(currSlice->partArr[partMap[SE_REFFRAME]]);
  2161. if (currSlice->num_ref_idx_l0_active > 1)
  2162. {
  2163. // For LIST_0, if multiple ref. pictures, read LIST_0 reference picture indices for the MB ***********
  2164. readMBRefPictureIdx_CABAC(dP, currMB, &motion->motion[LIST_0][currMB->block_y], LIST_0, step_v0, step_h0);
  2165. }
  2166. else
  2167. {
  2168. readMBRefPictureIdx_CABAC_NoReference(currMB, &motion->motion[LIST_0][currMB->block_y], LIST_0, step_v0, step_h0);
  2169. }
  2170. if (currSlice->num_ref_idx_l1_active > 1)
  2171. {
  2172. // For LIST_1, if multiple ref. pictures, read LIST_1 reference picture indices for the MB ***********
  2173. readMBRefPictureIdx_CABAC(dP, currMB, &motion->motion[LIST_1][currMB->block_y], LIST_1, step_v0, step_h0);
  2174. }
  2175. else
  2176. {
  2177. readMBRefPictureIdx_CABAC_NoReference(currMB, &motion->motion[LIST_1][currMB->block_y], LIST_1, step_v0, step_h0);
  2178. }
  2179. }
  2180. else
  2181. {
  2182. readMBRefPictureIdx_CABAC_NoReference(currMB, &motion->motion[LIST_0][currMB->block_y], LIST_0, step_v0, step_h0);
  2183. readMBRefPictureIdx_CABAC_NoReference(currMB, &motion->motion[LIST_1][currMB->block_y], LIST_1, step_v0, step_h0);
  2184. }
  2185. //===== READ MOTION VECTORS =====
  2186. dP = &(currSlice->partArr[partMap[SE_MVD]]);
  2187. readMBMotionVectors_CABAC(dP, currMB, LIST_0, step_h0, step_v0);
  2188. }
  2189. // LIST_0 Motion vectors
  2190. // record reference picture Ids for deblocking decisions
  2191. pic_num = dec_picture->ref_pic_num[p_Vid->current_slice_nr][LIST_0 + list_offset];
  2192. for(j4 = currMB->block_y; j4 < (currMB->block_y +4);++j4)
  2193. {
  2194. PicMotion *ref = &motion->motion[LIST_0][j4][currMB->block_x];
  2195. ref[0].ref_pic_id = (ref[0].ref_idx >= 0)?pic_num[(short)ref[0].ref_idx]:UNDEFINED_REFERENCE;
  2196. ref[1].ref_pic_id = (ref[1].ref_idx >= 0)?pic_num[(short)ref[1].ref_idx]:UNDEFINED_REFERENCE;
  2197. ref[2].ref_pic_id = (ref[2].ref_idx >= 0)?pic_num[(short)ref[2].ref_idx]:UNDEFINED_REFERENCE;
  2198. ref[3].ref_pic_id = (ref[3].ref_idx >= 0)?pic_num[(short)ref[3].ref_idx]:UNDEFINED_REFERENCE;
  2199. }
  2200. }
  2201. /*!
  2202. ************************************************************************
  2203. * \brief
  2204. * Read motion info
  2205. ************************************************************************
  2206. */
  2207. static void read_motion_info_from_NAL_b_slice (Macroblock *currMB)
  2208. {
  2209. VideoParameters *p_Vid = currMB->p_Vid;
  2210. Slice *currSlice = currMB->p_Slice;
  2211. int i,j,k;
  2212. int mb_nr = currMB->mbAddrX;
  2213. DataPartition *dP = NULL;
  2214. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  2215. int partmode = ((currMB->mb_type == P8x8) ? 4 : currMB->mb_type);
  2216. int step_h0 = BLOCK_STEP [partmode][0];
  2217. int step_v0 = BLOCK_STEP [partmode][1];
  2218. int i0, j0, j6;
  2219. int j4, i4, ii;
  2220. StorablePicture *dec_picture = p_Vid->dec_picture;
  2221. PicMotionParams *motion = &dec_picture->motion;
  2222. MotionParams *colocated;
  2223. int mv_scale = 0;
  2224. int list_offset = ((currSlice->mb_aff_frame_flag)&&(currMB->mb_field))? (mb_nr&0x01) ? 4 : 2 : 0;
  2225. if ((currSlice->mb_aff_frame_flag) && (currMB->mb_field))
  2226. {
  2227. if(mb_nr&0x01)
  2228. {
  2229. colocated = &currSlice->p_colocated->bottom;
  2230. }
  2231. else
  2232. {
  2233. colocated = &currSlice->p_colocated->top;
  2234. }
  2235. }
  2236. else
  2237. {
  2238. colocated = &currSlice->p_colocated->frame;
  2239. }
  2240. if (currMB->mb_type == P8x8)
  2241. {
  2242. if (currSlice->direct_spatial_mv_pred_flag)
  2243. {
  2244. char l0_rFrame, l1_rFrame;
  2245. short pmvl0[2]={0,0}, pmvl1[2]={0,0};
  2246. prepare_direct_params(currMB, dec_picture, pmvl0, pmvl1, &l0_rFrame, &l1_rFrame);
  2247. for (k = 0; k < 4; ++k)
  2248. {
  2249. if (currMB->b8mode[k] == 0)
  2250. {
  2251. i = currMB->block_x + 2 * (k & 0x01);
  2252. for(j = 2 * (k >> 1); j < 2 * (k >> 1)+2;++j)
  2253. {
  2254. j6 = currMB->block_y_aff + j;
  2255. j4 = currMB->block_y + j;
  2256. for(i4 = i; i4 < i + 2; ++i4)
  2257. {
  2258. if (l0_rFrame >= 0)
  2259. {
  2260. if (!l0_rFrame && ((!colocated->moving_block[j6][i4]) && (!p_Vid->listX[LIST_1 + list_offset][0]->is_long_term)))
  2261. {
  2262. motion->motion[LIST_0][j4][i4].mv[0] = 0;
  2263. motion->motion[LIST_0][j4][i4].mv[1] = 0;
  2264. motion->motion[LIST_0][j4][i4].ref_idx = 0;
  2265. }
  2266. else
  2267. {
  2268. motion->motion[LIST_0][j4][i4].mv[0] = pmvl0[0];
  2269. motion->motion[LIST_0][j4][i4].mv[1] = pmvl0[1];
  2270. motion->motion[LIST_0][j4][i4].ref_idx = l0_rFrame;
  2271. }
  2272. }
  2273. else
  2274. {
  2275. motion->motion[LIST_0][j4][i4].mv[0] = 0;
  2276. motion->motion[LIST_0][j4][i4].mv[1] = 0;
  2277. motion->motion[LIST_0][j4][i4].ref_idx = -1;
  2278. }
  2279. if (l1_rFrame >= 0)
  2280. {
  2281. if (l1_rFrame==0 && ((!colocated->moving_block[j6][i4])&& (!p_Vid->listX[LIST_1 + list_offset][0]->is_long_term)))
  2282. {
  2283. motion->motion[LIST_1][j4][i4].mv[0] = 0;
  2284. motion->motion[LIST_1][j4][i4].mv[1] = 0;
  2285. motion->motion[LIST_1][j4][i4].ref_idx = 0;
  2286. }
  2287. else
  2288. {
  2289. motion->motion[LIST_1][j4][i4].mv[0] = pmvl1[0];
  2290. motion->motion[LIST_1][j4][i4].mv[1] = pmvl1[1];
  2291. motion->motion[LIST_1][j4][i4].ref_idx = l1_rFrame;
  2292. }
  2293. }
  2294. else
  2295. {
  2296. motion->motion[LIST_1][j4][i4].mv[0] = 0;
  2297. motion->motion[LIST_1][j4][i4].mv[1] = 0;
  2298. motion->motion[LIST_1][j4][i4].ref_idx = -1;
  2299. }
  2300. if (l0_rFrame <0 && l1_rFrame <0)
  2301. {
  2302. motion->motion[LIST_0][j4][i4].ref_idx = 0;
  2303. motion->motion[LIST_1][j4][i4].ref_idx = 0;
  2304. }
  2305. }
  2306. }
  2307. }
  2308. }
  2309. }
  2310. else
  2311. {
  2312. for (k = 0; k < 4; ++k) // Scan all blocks
  2313. {
  2314. if (currMB->b8mode[k] == 0)
  2315. {
  2316. for(j0 = 2 * (k >> 1); j0 < 2 * (k >> 1) + 2; j0 += step_v0)
  2317. {
  2318. for(i0 = currMB->block_x + 2*(k & 0x01); i0 < currMB->block_x + 2 * (k & 0x01)+2; i0 += step_h0)
  2319. {
  2320. int refList = colocated->motion[LIST_0 ][currMB->block_y_aff + j0][i0].ref_idx== -1 ? LIST_1 : LIST_0;
  2321. int ref_idx = colocated->motion[refList][currMB->block_y_aff + j0][i0].ref_idx;
  2322. int mapped_idx = -1, iref;
  2323. if (ref_idx == -1)
  2324. {
  2325. for (j4 = currMB->block_y + j0; j4 < currMB->block_y + j0 + step_v0; ++j4)
  2326. {
  2327. int h;
  2328. for (h=0;h<step_h0;h++)
  2329. {
  2330. PicMotion *m0 = &motion->motion[LIST_0][j4][i0+h];
  2331. PicMotion *m1 = &motion->motion[LIST_1][j4][i0+h];
  2332. m0->ref_idx = 0;
  2333. m1->ref_idx = 0;
  2334. memset(&m0->mv, 0, sizeof(MotionVector));
  2335. memset(&m1->mv, 0, sizeof(MotionVector));
  2336. }
  2337. }
  2338. }
  2339. else
  2340. {
  2341. for (iref = 0; iref < imin(currSlice->num_ref_idx_l0_active, p_Vid->listXsize[LIST_0 + list_offset]); ++iref)
  2342. {
  2343. int curr_mb_field = ((currSlice->mb_aff_frame_flag)&&(currMB->mb_field));
  2344. if(p_Vid->structure==0 && curr_mb_field==0)
  2345. {
  2346. // If the current MB is a frame MB and the colocated is from a field picture,
  2347. // then the colocated->ref_pic_id may have been generated from the wrong value of
  2348. // frame_poc if it references it's complementary field, so test both POC values
  2349. if(p_Vid->listX[0][iref]->top_poc * 2 == colocated->motion[refList][currMB->block_y_aff + j0][i0].ref_pic_id
  2350. || p_Vid->listX[0][iref]->bottom_poc * 2 == colocated->motion[refList][currMB->block_y_aff + j0][i0].ref_pic_id)
  2351. {
  2352. mapped_idx=iref;
  2353. break;
  2354. }
  2355. else //! invalid index. Default to zero even though this case should not happen
  2356. mapped_idx=INVALIDINDEX;
  2357. continue;
  2358. }
  2359. if (dec_picture->ref_pic_num[p_Vid->current_slice_nr][LIST_0 + list_offset][iref]==colocated->motion[refList][currMB->block_y_aff + j0][i0].ref_pic_id)
  2360. {
  2361. mapped_idx=iref;
  2362. break;
  2363. }
  2364. else //! invalid index. Default to zero even though this case should not happen
  2365. mapped_idx=INVALIDINDEX;
  2366. }
  2367. if (INVALIDINDEX == mapped_idx)
  2368. {
  2369. error("temporal direct error: colocated block has ref that is unavailable",-1111);
  2370. }
  2371. for (j = j0; j < j0 + step_v0; ++j)
  2372. {
  2373. j4 = currMB->block_y + j;
  2374. j6 = currMB->block_y_aff + j;
  2375. for (i4 = i0; i4 < i0 + step_h0; ++i4)
  2376. {
  2377. mv_scale = currSlice->mvscale[LIST_0 + list_offset][mapped_idx];
  2378. motion->motion[LIST_0][j4][i4].ref_idx = (char) mapped_idx;
  2379. motion->motion[LIST_1][j4][i4].ref_idx = 0;
  2380. if (mv_scale == 9999 || p_Vid->listX[LIST_0+list_offset][mapped_idx]->is_long_term)
  2381. {
  2382. for (ii=0; ii < 2; ++ii)
  2383. {
  2384. motion->motion[LIST_0][j4][i4].mv[ii] = colocated->motion[refList][j6][i4].mv[ii];
  2385. motion->motion[LIST_1][j4][i4].mv[ii] = 0;
  2386. }
  2387. }
  2388. else
  2389. {
  2390. for (ii=0; ii < 2; ++ii)
  2391. {
  2392. motion->motion[LIST_0][j4][i4].mv[ii] = (short) ((mv_scale * colocated->motion[refList][j6][i4].mv[ii] + 128 ) >> 8);
  2393. motion->motion[LIST_1][j4][i4].mv[ii] = (short) (motion->motion[LIST_0][j4][i4].mv[ii] - colocated->motion[refList][j6][i4].mv[ii]);
  2394. }
  2395. }
  2396. }
  2397. }
  2398. }
  2399. }
  2400. }
  2401. }
  2402. }
  2403. }
  2404. }
  2405. if (p_Vid->active_pps->entropy_coding_mode_flag == CAVLC)
  2406. {
  2407. SyntaxElement currSE;
  2408. //===== READ REFERENCE PICTURE INDICES =====
  2409. dP = &(currSlice->partArr[partMap[SE_REFFRAME]]);
  2410. // For LIST_0, if multiple ref. pictures, read LIST_0 reference picture indices for the MB ***********
  2411. prepareListforRefIdx (currMB, &currSE, currSlice->num_ref_idx_l0_active, TRUE);
  2412. readMBRefPictureIdx (&currSE, dP, currMB, &motion->motion[LIST_0][currMB->block_y], LIST_0, step_v0, step_h0);
  2413. // For LIST_1, if multiple ref. pictures, read LIST_1 reference picture indices for the MB ***********
  2414. prepareListforRefIdx (currMB, &currSE, currSlice->num_ref_idx_l1_active, TRUE);
  2415. readMBRefPictureIdx (&currSE, dP, currMB, &motion->motion[LIST_1][currMB->block_y], LIST_1, step_v0, step_h0);
  2416. //===== READ MOTION VECTORS =====
  2417. dP = &(currSlice->partArr[partMap[SE_MVD]]);
  2418. currSE.mapping = linfo_se;
  2419. // LIST_0 Motion vectors
  2420. readMBMotionVectors (&currSE, dP, currMB, LIST_0, step_h0, step_v0);
  2421. // LIST_1 Motion vectors
  2422. readMBMotionVectors (&currSE, dP, currMB, LIST_1, step_h0, step_v0);
  2423. }
  2424. else
  2425. {
  2426. //===== READ REFERENCE PICTURE INDICES =====
  2427. dP = &(currSlice->partArr[partMap[SE_REFFRAME]]);
  2428. if (currSlice->num_ref_idx_l0_active>1)
  2429. {
  2430. // For LIST_0, if multiple ref. pictures, read LIST_0 reference picture indices for the MB ***********
  2431. readMBRefPictureIdx_CABAC(dP, currMB, &motion->motion[LIST_0][currMB->block_y], LIST_0, step_v0, step_h0);
  2432. }
  2433. else
  2434. {
  2435. readMBRefPictureIdx_CABAC_NoReference(currMB, &motion->motion[LIST_0][currMB->block_y], LIST_0, step_v0, step_h0);
  2436. }
  2437. if (currSlice->num_ref_idx_l1_active > 1)
  2438. {
  2439. // For LIST_1, if multiple ref. pictures, read LIST_1 reference picture indices for the MB ***********
  2440. readMBRefPictureIdx_CABAC(dP, currMB, &motion->motion[LIST_1][currMB->block_y], LIST_1, step_v0, step_h0);
  2441. }
  2442. else
  2443. {
  2444. readMBRefPictureIdx_CABAC_NoReference(currMB, &motion->motion[LIST_1][currMB->block_y], LIST_1, step_v0, step_h0);
  2445. }
  2446. //===== READ MOTION VECTORS =====
  2447. dP = &(currSlice->partArr[partMap[SE_MVD]]);
  2448. // LIST_0 Motion vectors
  2449. readMBMotionVectors_CABAC(dP, currMB, LIST_0, step_h0, step_v0);
  2450. // LIST_1 Motion vectors
  2451. readMBMotionVectors_CABAC(dP, currMB, LIST_1, step_h0, step_v0);
  2452. }
  2453. // record reference picture Ids for deblocking decisions
  2454. for (k = LIST_0; k <= LIST_1; ++k)
  2455. {
  2456. const h264_ref_t *rec_pic_num = dec_picture->ref_pic_num[p_Vid->current_slice_nr][k+list_offset];
  2457. PicMotion **list_motion = &motion->motion[k][currMB->block_y];
  2458. for(j4 = 0; j4 < 4 ;++j4)
  2459. {
  2460. PicMotion *m = &list_motion[j4][currMB->block_x];
  2461. m[0].ref_pic_id = (m[0].ref_idx>=0)?rec_pic_num[(short)m[0].ref_idx]:UNDEFINED_REFERENCE;
  2462. m[1].ref_pic_id = (m[1].ref_idx>=0)?rec_pic_num[(short)m[1].ref_idx]:UNDEFINED_REFERENCE;
  2463. m[2].ref_pic_id = (m[2].ref_idx>=0)?rec_pic_num[(short)m[2].ref_idx]:UNDEFINED_REFERENCE;
  2464. m[3].ref_pic_id = (m[3].ref_idx>=0)?rec_pic_num[(short)m[3].ref_idx]:UNDEFINED_REFERENCE;
  2465. }
  2466. }
  2467. }
  2468. /*!
  2469. ************************************************************************
  2470. * \brief
  2471. * Get the Prediction from the Neighboring Blocks for Number of
  2472. * Nonzero Coefficients
  2473. *
  2474. * Luma Blocks
  2475. ************************************************************************
  2476. */
  2477. static int predict_nnz_cb(Macroblock *currMB, int i,int j)
  2478. {
  2479. VideoParameters *p_Vid = currMB->p_Vid;
  2480. PixelPos pix;
  2481. int pred_nnz = 0;
  2482. int cnt = 0;
  2483. // left block
  2484. p_Vid->getNeighbourLuma(currMB, i - 1, j, &pix);
  2485. if (IS_INTRA(currMB) && pix.available && p_Vid->active_pps->constrained_intra_pred_flag && (p_Vid->currentSlice->dp_mode==PAR_DP_3))
  2486. {
  2487. pix.available &= p_Vid->intra_block[pix.mb_addr];
  2488. if (!pix.available)
  2489. ++cnt;
  2490. }
  2491. if (pix.available)
  2492. {
  2493. pred_nnz = p_Vid->nz_coeff [pix.mb_addr ][1][pix.y>>2][pix.x>>2];
  2494. ++cnt;
  2495. }
  2496. // top block
  2497. p_Vid->getNeighbourLuma(currMB, i, j - 1, &pix);
  2498. if (IS_INTRA(currMB) && pix.available && p_Vid->active_pps->constrained_intra_pred_flag && (p_Vid->currentSlice->dp_mode==PAR_DP_3))
  2499. {
  2500. pix.available &= p_Vid->intra_block[pix.mb_addr];
  2501. if (!pix.available)
  2502. ++cnt;
  2503. }
  2504. if (pix.available)
  2505. {
  2506. pred_nnz += p_Vid->nz_coeff [pix.mb_addr ][1][pix.y>>2][pix.x>>2];
  2507. ++cnt;
  2508. }
  2509. if (cnt==2)
  2510. {
  2511. ++pred_nnz;
  2512. pred_nnz>>=1;
  2513. }
  2514. return pred_nnz;
  2515. }
  2516. static int predict_nnz_cr(Macroblock *currMB, int i,int j)
  2517. {
  2518. VideoParameters *p_Vid = currMB->p_Vid;
  2519. PixelPos pix;
  2520. int pred_nnz = 0;
  2521. int cnt = 0;
  2522. // left block
  2523. p_Vid->getNeighbourLuma(currMB, i - 1, j, &pix);
  2524. if (IS_INTRA(currMB) && pix.available && p_Vid->active_pps->constrained_intra_pred_flag && (p_Vid->currentSlice->dp_mode==PAR_DP_3))
  2525. {
  2526. pix.available &= p_Vid->intra_block[pix.mb_addr];
  2527. if (!pix.available)
  2528. ++cnt;
  2529. }
  2530. if (pix.available)
  2531. {
  2532. pred_nnz = p_Vid->nz_coeff [pix.mb_addr ][2][pix.y>>2][pix.x>>2];
  2533. ++cnt;
  2534. }
  2535. // top block
  2536. p_Vid->getNeighbourLuma(currMB, i, j - 1, &pix);
  2537. if (IS_INTRA(currMB) && pix.available && p_Vid->active_pps->constrained_intra_pred_flag && (p_Vid->currentSlice->dp_mode==PAR_DP_3))
  2538. {
  2539. pix.available &= p_Vid->intra_block[pix.mb_addr];
  2540. if (!pix.available)
  2541. ++cnt;
  2542. }
  2543. if (pix.available)
  2544. {
  2545. pred_nnz += p_Vid->nz_coeff [pix.mb_addr ][2][pix.y>>2][pix.x>>2];
  2546. ++cnt;
  2547. }
  2548. if (cnt==2)
  2549. {
  2550. ++pred_nnz;
  2551. pred_nnz>>=1;
  2552. }
  2553. return pred_nnz;
  2554. }
  2555. static int predict_nnz_luma(Macroblock *currMB, int i,int j)
  2556. {
  2557. VideoParameters *p_Vid = currMB->p_Vid;
  2558. PixelPos pix;
  2559. int pred_nnz = 0;
  2560. int cnt = 0;
  2561. // left block
  2562. p_Vid->getNeighbourXPLuma(currMB, i - 1, j, &pix);
  2563. if (pix.available)
  2564. {
  2565. pred_nnz = p_Vid->nz_coeff [pix.mb_addr ][0][pix.y>>2][pix.x>>2];
  2566. ++cnt;
  2567. }
  2568. // top block
  2569. p_Vid->getNeighbourPXLuma(currMB, i, j - 1, &pix);
  2570. if (pix.available)
  2571. {
  2572. pred_nnz += p_Vid->nz_coeff [pix.mb_addr ][0][pix.y>>2][pix.x>>2];
  2573. ++cnt;
  2574. }
  2575. if (cnt==2)
  2576. {
  2577. ++pred_nnz;
  2578. pred_nnz>>=1;
  2579. }
  2580. return pred_nnz;
  2581. }
  2582. static int predict_nnz_luma_intra(Macroblock *currMB, int i,int j)
  2583. {
  2584. VideoParameters *p_Vid = currMB->p_Vid;
  2585. PixelPos pix;
  2586. int pred_nnz = 0;
  2587. int cnt = 0;
  2588. // left block
  2589. p_Vid->getNeighbourXPLuma(currMB, i - 1, j, &pix);
  2590. if (pix.available && p_Vid->active_pps->constrained_intra_pred_flag && (p_Vid->currentSlice->dp_mode==PAR_DP_3))
  2591. {
  2592. pix.available &= p_Vid->intra_block[pix.mb_addr];
  2593. if (!pix.available)
  2594. ++cnt;
  2595. }
  2596. if (pix.available)
  2597. {
  2598. pred_nnz = p_Vid->nz_coeff [pix.mb_addr ][0][pix.y>>2][pix.x>>2];
  2599. ++cnt;
  2600. }
  2601. // top block
  2602. p_Vid->getNeighbourPXLuma(currMB, i, j - 1, &pix);
  2603. if (pix.available && p_Vid->active_pps->constrained_intra_pred_flag && (p_Vid->currentSlice->dp_mode==PAR_DP_3))
  2604. {
  2605. pix.available &= p_Vid->intra_block[pix.mb_addr];
  2606. if (!pix.available)
  2607. ++cnt;
  2608. }
  2609. if (pix.available)
  2610. {
  2611. pred_nnz += p_Vid->nz_coeff [pix.mb_addr ][0][pix.y>>2][pix.x>>2];
  2612. ++cnt;
  2613. }
  2614. if (cnt==2)
  2615. {
  2616. ++pred_nnz;
  2617. pred_nnz>>=1;
  2618. }
  2619. return pred_nnz;
  2620. }
  2621. /*!
  2622. ************************************************************************
  2623. * \brief
  2624. * Get the Prediction from the Neighboring Blocks for Number of
  2625. * Nonzero Coefficients
  2626. *
  2627. * Chroma Blocks
  2628. ************************************************************************
  2629. */
  2630. static int predict_nnz_chroma_inter(Macroblock *currMB, int i,int j)
  2631. {
  2632. VideoParameters *p_Vid = currMB->p_Vid;
  2633. StorablePicture *dec_picture = p_Vid->dec_picture;
  2634. PixelPos pix;
  2635. int pred_nnz = 0;
  2636. int cnt = 0;
  2637. if (dec_picture->chroma_format_idc != YUV444)
  2638. {
  2639. //YUV420 and YUV422
  2640. // left block
  2641. p_Vid->getNeighbour(currMB, ((i&0x01)<<2) - 1, j, p_Vid->mb_size[IS_CHROMA], &pix);
  2642. if (pix.available)
  2643. {
  2644. pred_nnz = p_Vid->nz_coeff [pix.mb_addr ][1][pix.y>>2][2 * (i>>1) + (pix.x>>2)];
  2645. ++cnt;
  2646. }
  2647. // top block
  2648. p_Vid->getNeighbour(currMB, ((i&0x01)<<2), j - 1, p_Vid->mb_size[IS_CHROMA], &pix);
  2649. if (pix.available)
  2650. {
  2651. pred_nnz += p_Vid->nz_coeff [pix.mb_addr ][1][pix.y>>2][2 * (i>>1) + (pix.x>>2)];
  2652. ++cnt;
  2653. }
  2654. if (cnt==2)
  2655. {
  2656. ++pred_nnz;
  2657. pred_nnz >>= 1;
  2658. }
  2659. }
  2660. return pred_nnz;
  2661. }
  2662. static int predict_nnz_chroma_intra(Macroblock *currMB, int i,int j)
  2663. {
  2664. VideoParameters *p_Vid = currMB->p_Vid;
  2665. StorablePicture *dec_picture = p_Vid->dec_picture;
  2666. PixelPos pix;
  2667. int pred_nnz = 0;
  2668. int cnt = 0;
  2669. if (dec_picture->chroma_format_idc != YUV444)
  2670. {
  2671. //YUV420 and YUV422
  2672. // left block
  2673. p_Vid->getNeighbour(currMB, ((i&0x01)<<2) - 1, j, p_Vid->mb_size[IS_CHROMA], &pix);
  2674. if (pix.available && p_Vid->active_pps->constrained_intra_pred_flag && (p_Vid->currentSlice->dp_mode==PAR_DP_3))
  2675. {
  2676. pix.available &= p_Vid->intra_block[pix.mb_addr];
  2677. if (!pix.available)
  2678. ++cnt;
  2679. }
  2680. if (pix.available)
  2681. {
  2682. pred_nnz = p_Vid->nz_coeff [pix.mb_addr ][1][pix.y>>2][2 * (i>>1) + (pix.x>>2)];
  2683. ++cnt;
  2684. }
  2685. // top block
  2686. p_Vid->getNeighbour(currMB, ((i&0x01)<<2), j - 1, p_Vid->mb_size[IS_CHROMA], &pix);
  2687. if (pix.available && p_Vid->active_pps->constrained_intra_pred_flag && (p_Vid->currentSlice->dp_mode==PAR_DP_3))
  2688. {
  2689. pix.available &= p_Vid->intra_block[pix.mb_addr];
  2690. if (!pix.available)
  2691. ++cnt;
  2692. }
  2693. if (pix.available)
  2694. {
  2695. pred_nnz += p_Vid->nz_coeff [pix.mb_addr ][1][pix.y>>2][2 * (i>>1) + (pix.x>>2)];
  2696. ++cnt;
  2697. }
  2698. if (cnt==2)
  2699. {
  2700. ++pred_nnz;
  2701. pred_nnz >>= 1;
  2702. }
  2703. }
  2704. return pred_nnz;
  2705. }
  2706. /*!
  2707. ************************************************************************
  2708. * \brief
  2709. * Reads coeff of an 4x4 block (CAVLC)
  2710. *
  2711. * \author
  2712. * Karl Lillevold <[email protected]>
  2713. * contributions by James Au <[email protected]>
  2714. ************************************************************************
  2715. */
  2716. static void readCoeff4x4_CAVLC_Luma (Macroblock *currMB,
  2717. int i, int j, int levarr[16], int runarr[16],
  2718. int *number_coefficients)
  2719. {
  2720. Slice *currSlice = currMB->p_Slice;
  2721. VideoParameters *p_Vid = currMB->p_Vid;
  2722. int mb_nr = currMB->mbAddrX;
  2723. SyntaxElement currSE;
  2724. DataPartition *dP;
  2725. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  2726. Bitstream *currStream;
  2727. int k, code, vlcnum;
  2728. int numcoeff = 0, numtrailingones, numcoeff_vlc;
  2729. int level_two_or_higher;
  2730. int numones, totzeros, abslevel;
  2731. int zerosleft;
  2732. int nnz;
  2733. static const int incVlc[] = {0,3,6,12,24,48,32768}; // maximum vlc = 6
  2734. p_Vid->nz_coeff[mb_nr][0][j][i] = 0;
  2735. if (IS_INTRA (currMB))
  2736. {
  2737. dP = &(currSlice->partArr[partMap[SE_LUM_AC_INTRA]]);
  2738. nnz = predict_nnz_luma_intra(currMB, i<<2, j<<2);
  2739. }
  2740. else
  2741. {
  2742. dP = &(currSlice->partArr[partMap[SE_LUM_AC_INTER]]);
  2743. nnz = predict_nnz_luma(currMB, i<<2, j<<2);
  2744. }
  2745. if (nnz < 2)
  2746. {
  2747. numcoeff_vlc = 0;
  2748. }
  2749. else if (nnz < 4)
  2750. {
  2751. numcoeff_vlc = 1;
  2752. }
  2753. else if (nnz < 8)
  2754. {
  2755. numcoeff_vlc = 2;
  2756. }
  2757. else //
  2758. {
  2759. numcoeff_vlc = 3;
  2760. }
  2761. currStream = dP->bitstream;
  2762. readSyntaxElement_NumCoeffTrailingOnes(&currSE, currStream, numcoeff_vlc);
  2763. numcoeff = currSE.value1;
  2764. numtrailingones = currSE.value2;
  2765. p_Vid->nz_coeff[mb_nr][0][j][i] = (byte) numcoeff;
  2766. memzero64(levarr);
  2767. memzero64(runarr);
  2768. numones = numtrailingones;
  2769. *number_coefficients = numcoeff;
  2770. if (numcoeff)
  2771. {
  2772. if (numtrailingones)
  2773. {
  2774. code = readSyntaxElement_FLC(currStream, numtrailingones);
  2775. for (k=0;k<numtrailingones;k++)
  2776. {
  2777. #ifdef _M_IX86
  2778. levarr[k+numcoeff-numtrailingones] = ((_bittest((const long *)&code, k)<<1) ^ 0xFFFFFFFF) + 2;
  2779. #else
  2780. levarr[k+numcoeff-numtrailingones] = (code>>k)&1 ? -1:1;
  2781. #endif
  2782. }
  2783. }
  2784. // decode levels
  2785. level_two_or_higher = (numcoeff > 3 && numtrailingones == 3)? 0 : 1;
  2786. vlcnum = (numcoeff > 10 && numtrailingones < 3) ? 1 : 0;
  2787. for (k = numcoeff - 1 - numtrailingones; k >= 0; k--)
  2788. {
  2789. int level;
  2790. if (vlcnum == 0)
  2791. level=readSyntaxElement_Level_VLC0(currStream);
  2792. else
  2793. level=readSyntaxElement_Level_VLCN(vlcnum, currStream);
  2794. if (level_two_or_higher)
  2795. {
  2796. level += (level > 0) ? 1 : -1;
  2797. level_two_or_higher = 0;
  2798. }
  2799. levarr[k] = level;
  2800. abslevel = iabs(levarr[k]);
  2801. if (abslevel == 1)
  2802. ++numones;
  2803. // update VLC table
  2804. if (abslevel > incVlc[vlcnum])
  2805. ++vlcnum;
  2806. if (k == numcoeff - 1 - numtrailingones && abslevel >3)
  2807. vlcnum = 2;
  2808. }
  2809. if (numcoeff < 16)
  2810. {
  2811. // decode total run
  2812. vlcnum = numcoeff - 1;
  2813. totzeros = readSyntaxElement_TotalZeros(currStream, vlcnum);
  2814. }
  2815. else
  2816. {
  2817. totzeros = 0;
  2818. }
  2819. // decode run before each coefficient
  2820. zerosleft = totzeros;
  2821. i = numcoeff - 1;
  2822. if (zerosleft > 0 && i > 0)
  2823. {
  2824. do
  2825. {
  2826. // select VLC for runbefore
  2827. vlcnum = imin(zerosleft - 1, RUNBEFORE_NUM_M1);
  2828. runarr[i] = readSyntaxElement_Run(currStream, vlcnum);
  2829. zerosleft -= runarr[i];
  2830. i --;
  2831. } while (zerosleft != 0 && i != 0);
  2832. }
  2833. runarr[i] = zerosleft;
  2834. } // if numcoeff
  2835. }
  2836. static void readCoeff4x4_CAVLC_ChromaAC(Macroblock *currMB,
  2837. int i, int j, int levarr[16], int runarr[16],
  2838. int *number_coefficients)
  2839. {
  2840. Slice *currSlice = currMB->p_Slice;
  2841. VideoParameters *p_Vid = currMB->p_Vid;
  2842. int mb_nr = currMB->mbAddrX;
  2843. SyntaxElement currSE;
  2844. DataPartition *dP;
  2845. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  2846. Bitstream *currStream;
  2847. int k, code, vlcnum;
  2848. int numcoeff = 0, numtrailingones, numcoeff_vlc;
  2849. int level_two_or_higher;
  2850. int numones, totzeros, abslevel;
  2851. int zerosleft, ntr;
  2852. int nnz;
  2853. static const int incVlc[] = {0,3,6,12,24,48,32768}; // maximum vlc = 6
  2854. TRACE_PRINTF("ChrDC");
  2855. p_Vid->nz_coeff[mb_nr][0][j][i] = 0;
  2856. if (IS_INTRA (currMB))
  2857. {
  2858. dP = &(currSlice->partArr[partMap[SE_CHR_AC_INTRA]]);
  2859. nnz = predict_nnz_chroma_intra(currMB, i, ((j-4)<<2));
  2860. }
  2861. else
  2862. {
  2863. dP = &(currSlice->partArr[partMap[SE_CHR_AC_INTER]]);
  2864. nnz = predict_nnz_chroma_inter(currMB, i, ((j-4)<<2));
  2865. }
  2866. currStream = dP->bitstream;
  2867. // luma or chroma AC
  2868. if (nnz < 2)
  2869. {
  2870. numcoeff_vlc = 0;
  2871. }
  2872. else if (nnz < 4)
  2873. {
  2874. numcoeff_vlc = 1;
  2875. }
  2876. else if (nnz < 8)
  2877. {
  2878. numcoeff_vlc = 2;
  2879. }
  2880. else //
  2881. {
  2882. numcoeff_vlc = 3;
  2883. }
  2884. readSyntaxElement_NumCoeffTrailingOnes(&currSE, currStream, numcoeff_vlc);
  2885. numcoeff = currSE.value1;
  2886. numtrailingones = currSE.value2;
  2887. p_Vid->nz_coeff[mb_nr][0][j][i] = (byte) numcoeff;
  2888. memzero64(levarr);
  2889. memzero64(runarr);
  2890. numones = numtrailingones;
  2891. *number_coefficients = numcoeff;
  2892. if (numcoeff)
  2893. {
  2894. if (numtrailingones)
  2895. {
  2896. code = readSyntaxElement_FLC (currStream, numtrailingones);
  2897. ntr = numtrailingones;
  2898. for (k = numcoeff - 1; k > numcoeff - 1 - numtrailingones; k--)
  2899. {
  2900. ntr --;
  2901. levarr[k] = (code>>ntr)&1 ? -1 : 1;
  2902. }
  2903. }
  2904. // decode levels
  2905. level_two_or_higher = (numcoeff > 3 && numtrailingones == 3)? 0 : 1;
  2906. vlcnum = (numcoeff > 10 && numtrailingones < 3) ? 1 : 0;
  2907. for (k = numcoeff - 1 - numtrailingones; k >= 0; k--)
  2908. {
  2909. #if TRACE
  2910. snprintf(currSE.tracestring,
  2911. TRACESTRING_SIZE, "%s lev (%d,%d) k=%d vlc=%d ", type, i, j, k, vlcnum);
  2912. #endif
  2913. int level;
  2914. if (vlcnum == 0)
  2915. level=readSyntaxElement_Level_VLC0(currStream);
  2916. else
  2917. level=readSyntaxElement_Level_VLCN(vlcnum, currStream);
  2918. if (level_two_or_higher)
  2919. {
  2920. level += (level > 0) ? 1 : -1;
  2921. level_two_or_higher = 0;
  2922. }
  2923. levarr[k] = level;
  2924. abslevel = iabs(levarr[k]);
  2925. if (abslevel == 1)
  2926. ++numones;
  2927. // update VLC table
  2928. if (abslevel > incVlc[vlcnum])
  2929. ++vlcnum;
  2930. if (k == numcoeff - 1 - numtrailingones && abslevel >3)
  2931. vlcnum = 2;
  2932. }
  2933. if (numcoeff < 15)
  2934. {
  2935. // decode total run
  2936. vlcnum = numcoeff - 1;
  2937. totzeros = readSyntaxElement_TotalZeros(currStream, vlcnum);
  2938. }
  2939. else
  2940. {
  2941. totzeros = 0;
  2942. }
  2943. // decode run before each coefficient
  2944. zerosleft = totzeros;
  2945. i = numcoeff - 1;
  2946. if (zerosleft > 0 && i > 0)
  2947. {
  2948. do
  2949. {
  2950. // select VLC for runbefore
  2951. vlcnum = imin(zerosleft - 1, RUNBEFORE_NUM_M1);
  2952. runarr[i] = readSyntaxElement_Run(currStream, vlcnum);
  2953. zerosleft -= runarr[i];
  2954. i --;
  2955. } while (zerosleft != 0 && i != 0);
  2956. }
  2957. runarr[i] = zerosleft;
  2958. } // if numcoeff
  2959. }
  2960. static void readCoeff4x4_CAVLC_ChromaDC(Macroblock *currMB, int i, int j, int levarr[16], int runarr[16], int *number_coefficients)
  2961. {
  2962. Slice *currSlice = currMB->p_Slice;
  2963. VideoParameters *p_Vid = currMB->p_Vid;
  2964. int mb_nr = currMB->mbAddrX;
  2965. SyntaxElement currSE;
  2966. DataPartition *dP;
  2967. Bitstream *currStream;
  2968. int k, code, vlcnum;
  2969. int numcoeff = 0, numtrailingones;
  2970. int level_two_or_higher;
  2971. int numones, totzeros, abslevel;
  2972. int zerosleft, ntr;
  2973. int max_coeff_num;
  2974. static const int incVlc[] = {0,3,6,12,24,48,32768}; // maximum vlc = 6
  2975. max_coeff_num = p_Vid->num_cdc_coeff;
  2976. TRACE_PRINTF("ChrDC");
  2977. p_Vid->nz_coeff[mb_nr][0][j][i] = 0;
  2978. if (IS_INTRA (currMB))
  2979. dP = &(currSlice->partArr[assignSE2partition[currSlice->dp_mode][SE_CHR_DC_INTRA]]);
  2980. else
  2981. dP = &(currSlice->partArr[assignSE2partition[currSlice->dp_mode][SE_CHR_DC_INTER]]);
  2982. currStream = dP->bitstream;
  2983. readSyntaxElement_NumCoeffTrailingOnesChromaDC(p_Vid, &currSE, currStream);
  2984. numcoeff = currSE.value1;
  2985. numtrailingones = currSE.value2;
  2986. memzero64(levarr);
  2987. memzero64(runarr);
  2988. numones = numtrailingones;
  2989. *number_coefficients = numcoeff;
  2990. if (numcoeff)
  2991. {
  2992. if (numtrailingones)
  2993. {
  2994. code = readSyntaxElement_FLC (currStream, numtrailingones);
  2995. ntr = numtrailingones;
  2996. for (k = numcoeff - 1; k > numcoeff - 1 - numtrailingones; k--)
  2997. {
  2998. ntr --;
  2999. levarr[k] = (code>>ntr)&1 ? -1 : 1;
  3000. }
  3001. }
  3002. // decode levels
  3003. level_two_or_higher = (numcoeff > 3 && numtrailingones == 3)? 0 : 1;
  3004. vlcnum = (numcoeff > 10 && numtrailingones < 3) ? 1 : 0;
  3005. for (k = numcoeff - 1 - numtrailingones; k >= 0; k--)
  3006. {
  3007. int level;
  3008. if (vlcnum == 0)
  3009. level=readSyntaxElement_Level_VLC0(currStream);
  3010. else
  3011. level=readSyntaxElement_Level_VLCN(vlcnum, currStream);
  3012. if (level_two_or_higher)
  3013. {
  3014. level += (level > 0) ? 1 : -1;
  3015. level_two_or_higher = 0;
  3016. }
  3017. levarr[k] = level;
  3018. abslevel = iabs(levarr[k]);
  3019. if (abslevel == 1)
  3020. ++numones;
  3021. // update VLC table
  3022. if (abslevel > incVlc[vlcnum])
  3023. ++vlcnum;
  3024. if (k == numcoeff - 1 - numtrailingones && abslevel >3)
  3025. vlcnum = 2;
  3026. }
  3027. if (numcoeff < max_coeff_num)
  3028. {
  3029. // decode total run
  3030. vlcnum = numcoeff - 1;
  3031. totzeros = readSyntaxElement_TotalZerosChromaDC(p_Vid, currStream, vlcnum);
  3032. }
  3033. else
  3034. {
  3035. totzeros = 0;
  3036. }
  3037. // decode run before each coefficient
  3038. zerosleft = totzeros;
  3039. i = numcoeff - 1;
  3040. if (zerosleft > 0 && i > 0)
  3041. {
  3042. do
  3043. {
  3044. // select VLC for runbefore
  3045. vlcnum = imin(zerosleft - 1, RUNBEFORE_NUM_M1);
  3046. runarr[i] = readSyntaxElement_Run(currStream, vlcnum);
  3047. zerosleft -= runarr[i];
  3048. i --;
  3049. } while (zerosleft != 0 && i != 0);
  3050. }
  3051. runarr[i] = zerosleft;
  3052. } // if numcoeff
  3053. }
  3054. static void readCoeff4x4_CAVLC(Macroblock *currMB, int block_type, int i, int j, int levarr[16], int runarr[16], int *number_coefficients)
  3055. {
  3056. Slice *currSlice = currMB->p_Slice;
  3057. VideoParameters *p_Vid = currMB->p_Vid;
  3058. int mb_nr = currMB->mbAddrX;
  3059. SyntaxElement currSE;
  3060. DataPartition *dP;
  3061. Bitstream *currStream;
  3062. int k, code, vlcnum;
  3063. int numcoeff = 0, numtrailingones, numcoeff_vlc;
  3064. int level_two_or_higher;
  3065. int numones, totzeros, abslevel;
  3066. int zerosleft, ntr, dptype = 0;
  3067. int max_coeff_num, nnz;
  3068. static const int incVlc[] = {0,3,6,12,24,48,32768}; // maximum vlc = 6
  3069. switch (block_type)
  3070. {
  3071. case LUMA:
  3072. readCoeff4x4_CAVLC_Luma(currMB, i, j, levarr, runarr, number_coefficients);
  3073. return;
  3074. case LUMA_INTRA16x16DC:
  3075. max_coeff_num = 16;
  3076. TRACE_PRINTF("Lum16DC");
  3077. dptype = SE_LUM_DC_INTRA;
  3078. p_Vid->nz_coeff[mb_nr][0][j][i] = 0;
  3079. break;
  3080. case LUMA_INTRA16x16AC:
  3081. max_coeff_num = 15;
  3082. TRACE_PRINTF("Lum16AC");
  3083. dptype = SE_LUM_AC_INTRA;
  3084. p_Vid->nz_coeff[mb_nr][0][j][i] = 0;
  3085. break;
  3086. case CB:
  3087. max_coeff_num = 16;
  3088. TRACE_PRINTF("Luma_add1");
  3089. dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
  3090. p_Vid->nz_coeff[mb_nr][1][j][i] = 0;
  3091. break;
  3092. case CB_INTRA16x16DC:
  3093. max_coeff_num = 16;
  3094. TRACE_PRINTF("Luma_add1_16DC");
  3095. dptype = SE_LUM_DC_INTRA;
  3096. p_Vid->nz_coeff[mb_nr][1][j][i] = 0;
  3097. break;
  3098. case CB_INTRA16x16AC:
  3099. max_coeff_num = 15;
  3100. TRACE_PRINTF("Luma_add1_16AC");
  3101. dptype = SE_LUM_AC_INTRA;
  3102. p_Vid->nz_coeff[mb_nr][1][j][i] = 0;
  3103. break;
  3104. case CR:
  3105. max_coeff_num = 16;
  3106. TRACE_PRINTF("Luma_add2");
  3107. dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
  3108. p_Vid->nz_coeff[mb_nr][2][j][i] = 0;
  3109. break;
  3110. case CR_INTRA16x16DC:
  3111. max_coeff_num = 16;
  3112. TRACE_PRINTF("Luma_add2_16DC");
  3113. dptype = SE_LUM_DC_INTRA;
  3114. p_Vid->nz_coeff[mb_nr][2][j][i] = 0;
  3115. break;
  3116. case CR_INTRA16x16AC:
  3117. max_coeff_num = 15;
  3118. TRACE_PRINTF("Luma_add1_16AC");
  3119. dptype = SE_LUM_AC_INTRA;
  3120. p_Vid->nz_coeff[mb_nr][2][j][i] = 0;
  3121. break;
  3122. case CHROMA_DC:
  3123. readCoeff4x4_CAVLC_ChromaDC(currMB, i, j, levarr, runarr, number_coefficients);
  3124. return;
  3125. case CHROMA_AC:
  3126. readCoeff4x4_CAVLC_ChromaAC(currMB, i, j, levarr, runarr, number_coefficients);
  3127. return;
  3128. default:
  3129. error ("readCoeff4x4_CAVLC: invalid block type", 600);
  3130. p_Vid->nz_coeff[mb_nr][0][j][i] = 0;
  3131. break;
  3132. }
  3133. dP = &(currSlice->partArr[assignSE2partition[currSlice->dp_mode][dptype]]);
  3134. currStream = dP->bitstream;
  3135. // luma or chroma AC
  3136. if(block_type==LUMA_INTRA16x16DC || block_type==LUMA_INTRA16x16AC)
  3137. {
  3138. nnz = predict_nnz_luma_intra(currMB, i<<2, j<<2);
  3139. }
  3140. else if (block_type==CB || block_type==CB_INTRA16x16DC || block_type==CB_INTRA16x16AC)
  3141. {
  3142. nnz = predict_nnz_cb(currMB, i<<2, j<<2);
  3143. }
  3144. else
  3145. {
  3146. nnz = predict_nnz_cr(currMB, i<<2, j<<2);
  3147. }
  3148. if (nnz < 2)
  3149. {
  3150. numcoeff_vlc = 0;
  3151. }
  3152. else if (nnz < 4)
  3153. {
  3154. numcoeff_vlc = 1;
  3155. }
  3156. else if (nnz < 8)
  3157. {
  3158. numcoeff_vlc = 2;
  3159. }
  3160. else //
  3161. {
  3162. numcoeff_vlc = 3;
  3163. }
  3164. readSyntaxElement_NumCoeffTrailingOnes(&currSE, currStream, numcoeff_vlc);
  3165. numcoeff = currSE.value1;
  3166. numtrailingones = currSE.value2;
  3167. if(block_type==LUMA_INTRA16x16DC || block_type==LUMA_INTRA16x16AC)
  3168. p_Vid->nz_coeff[mb_nr][0][j][i] = (byte) numcoeff;
  3169. else if (block_type==CB || block_type==CB_INTRA16x16DC || block_type==CB_INTRA16x16AC)
  3170. p_Vid->nz_coeff[mb_nr][1][j][i] = (byte) numcoeff;
  3171. else
  3172. p_Vid->nz_coeff[mb_nr][2][j][i] = (byte) numcoeff;
  3173. memzero64(levarr);
  3174. memzero64(runarr);
  3175. numones = numtrailingones;
  3176. *number_coefficients = numcoeff;
  3177. if (numcoeff)
  3178. {
  3179. if (numtrailingones)
  3180. {
  3181. code = readSyntaxElement_FLC(currStream, numtrailingones);
  3182. ntr = numtrailingones;
  3183. for (k = numcoeff - 1; k > numcoeff - 1 - numtrailingones; k--)
  3184. {
  3185. ntr --;
  3186. levarr[k] = (code>>ntr)&1 ? -1 : 1;
  3187. }
  3188. }
  3189. // decode levels
  3190. level_two_or_higher = (numcoeff > 3 && numtrailingones == 3)? 0 : 1;
  3191. vlcnum = (numcoeff > 10 && numtrailingones < 3) ? 1 : 0;
  3192. for (k = numcoeff - 1 - numtrailingones; k >= 0; k--)
  3193. {
  3194. int level;
  3195. if (vlcnum == 0)
  3196. level=readSyntaxElement_Level_VLC0(currStream);
  3197. else
  3198. level=readSyntaxElement_Level_VLCN(vlcnum, currStream);
  3199. if (level_two_or_higher)
  3200. {
  3201. level += (level > 0) ? 1 : -1;
  3202. level_two_or_higher = 0;
  3203. }
  3204. levarr[k] = level;
  3205. abslevel = iabs(levarr[k]);
  3206. if (abslevel == 1)
  3207. ++numones;
  3208. // update VLC table
  3209. if (abslevel > incVlc[vlcnum])
  3210. ++vlcnum;
  3211. if (k == numcoeff - 1 - numtrailingones && abslevel >3)
  3212. vlcnum = 2;
  3213. }
  3214. if (numcoeff < max_coeff_num)
  3215. {
  3216. // decode total run
  3217. vlcnum = numcoeff - 1;
  3218. totzeros = readSyntaxElement_TotalZeros(currStream, vlcnum);
  3219. }
  3220. else
  3221. {
  3222. totzeros = 0;
  3223. }
  3224. // decode run before each coefficient
  3225. zerosleft = totzeros;
  3226. i = numcoeff - 1;
  3227. if (zerosleft > 0 && i > 0)
  3228. {
  3229. do
  3230. {
  3231. // select VLC for runbefore
  3232. vlcnum = imin(zerosleft - 1, RUNBEFORE_NUM_M1);
  3233. runarr[i] = readSyntaxElement_Run(currStream, vlcnum);
  3234. zerosleft -= runarr[i];
  3235. i --;
  3236. } while (zerosleft != 0 && i != 0);
  3237. }
  3238. runarr[i] = zerosleft;
  3239. } // if numcoeff
  3240. }
  3241. /*!
  3242. ************************************************************************
  3243. * \brief
  3244. * Get coefficients (run/level) of 4x4 blocks in a SMB
  3245. * from the NAL (CABAC Mode)
  3246. ************************************************************************
  3247. */
  3248. static void readCompCoeff4x4SMB_I16MB_CABAC(Macroblock *currMB, int context, h264_short_block_t *blocks, int block_y, int block_x, int64 *cbp_blk)
  3249. {
  3250. // start_scan == 1
  3251. int i,j,k;
  3252. RunLevel rl;
  3253. VideoParameters *p_Vid = currMB->p_Vid;
  3254. Slice *currSlice = currMB->p_Slice;
  3255. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  3256. const byte *pos_scan4x4 = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN_1D : FIELD_SCAN_1D;
  3257. const byte *pos_scan_4x4;
  3258. // make distinction between INTRA and INTER coded luminance coefficients
  3259. int type = (currMB->is_intra_block ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER);
  3260. DecodingEnvironment *de_cabac = &currSlice->partArr[partMap[type]].de_cabac;
  3261. for (j = 0; j < BLOCK_SIZE_8x8; j += BLOCK_SIZE)
  3262. {
  3263. currMB->subblock_y = block_y + j; // position for coeff_count ctx
  3264. for (i = 0; i < BLOCK_SIZE_8x8; i += BLOCK_SIZE)
  3265. {
  3266. int16_t *block = (int16_t *)(*blocks++);
  3267. currMB->subblock_x = block_x + i; // position for coeff_count ctx
  3268. pos_scan_4x4 = &pos_scan4x4[1];
  3269. for(k = 0; k < 16; k++)
  3270. {
  3271. rl = readRunLevel_CABAC(currMB, de_cabac, context);
  3272. if (rl.level != 0) /* leave if level == 0 */
  3273. {
  3274. pos_scan_4x4 += rl.run;
  3275. block[*pos_scan_4x4++] = rl.level;
  3276. }
  3277. else
  3278. break;
  3279. }
  3280. }
  3281. }
  3282. }
  3283. #ifdef _M_IX86
  3284. static void readCompCoeff4x4SMB_CABAC(Macroblock *currMB, int context, h264_short_block_t *blocks, int block_y, int block_x, int64_t *cbp_blk64)
  3285. #else
  3286. static void readCompCoeff4x4SMB_CABAC(Macroblock *currMB, int context, h264_short_block_t *blocks, int block_y, int block_x, int64_t *cbp_blk)
  3287. #endif
  3288. {
  3289. int k;
  3290. RunLevel rl;
  3291. VideoParameters *p_Vid = currMB->p_Vid;
  3292. Slice *currSlice = currMB->p_Slice;
  3293. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  3294. const byte *pos_scan4x4 = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN_1D : FIELD_SCAN_1D;
  3295. const byte *pos_scan_4x4;
  3296. int16_t *block;
  3297. #ifdef _M_IX86
  3298. int32_t *cbp_blk = (int32_t *)cbp_blk64;
  3299. #endif
  3300. //h264_short_block_t *blocks = &currSlice->cof4[pl][cof4_pos_to_subblock[block_y>>2][block_x>>2]];
  3301. DecodingEnvironment *de_cabac_dc, *de_cabac_ac;
  3302. /*
  3303. * make distinction between INTRA and INTER coded
  3304. * luminance coefficients
  3305. */
  3306. if (currMB->is_intra_block)
  3307. {
  3308. de_cabac_dc = &currSlice->partArr[partMap[SE_LUM_DC_INTRA]].de_cabac;
  3309. de_cabac_ac = &currSlice->partArr[partMap[SE_LUM_AC_INTRA]].de_cabac;
  3310. }
  3311. else
  3312. {
  3313. de_cabac_dc = &currSlice->partArr[partMap[SE_LUM_DC_INTER]].de_cabac;
  3314. de_cabac_ac = &currSlice->partArr[partMap[SE_LUM_AC_INTER]].de_cabac;
  3315. }
  3316. // for (j = block_y; j < (block_y+BLOCK_SIZE_8x8); j += 4)
  3317. block = (int16_t *)(*blocks++);
  3318. currMB->subblock_y = block_y; // position for coeff_count ctx
  3319. currMB->subblock_x = block_x; // position for coeff_count ctx
  3320. pos_scan_4x4 = pos_scan4x4;
  3321. rl = readRunLevel_CABAC(currMB, de_cabac_dc, context);
  3322. if (rl.level != 0) /* leave if level == 0 */
  3323. {
  3324. pos_scan_4x4 += rl.run;
  3325. *cbp_blk |= 1 << (block_y + (block_x >> 2)) ;
  3326. block[*pos_scan_4x4++] = rl.level;
  3327. for(k = 0; k < 16; ++k)
  3328. {
  3329. rl = readRunLevel_CABAC(currMB, de_cabac_ac, context);
  3330. if (rl.level != 0) /* leave if level == 0 */
  3331. {
  3332. pos_scan_4x4 += rl.run;
  3333. block[*pos_scan_4x4++] = rl.level;
  3334. }
  3335. else
  3336. break;
  3337. }
  3338. }
  3339. block = (int16_t *)(*blocks++);
  3340. currMB->subblock_x += 4; // position for coeff_count ctx
  3341. pos_scan_4x4 = pos_scan4x4;
  3342. rl = readRunLevel_CABAC(currMB, de_cabac_dc, context);
  3343. if (rl.level != 0) /* leave if level == 0 */
  3344. {
  3345. pos_scan_4x4 += rl.run;
  3346. *cbp_blk |= 2 << (block_y + (block_x >> 2)) ;
  3347. block[*pos_scan_4x4++] = rl.level;
  3348. for(k = 0; k < 16; ++k)
  3349. {
  3350. rl = readRunLevel_CABAC(currMB, de_cabac_ac, context);
  3351. if (rl.level != 0) /* leave if level == 0 */
  3352. {
  3353. pos_scan_4x4 += rl.run;
  3354. block[*pos_scan_4x4++] = rl.level;
  3355. }
  3356. else
  3357. break;
  3358. }
  3359. }
  3360. /* ---- */
  3361. block = (int16_t *)(*blocks++);
  3362. currMB->subblock_y += 4; // position for coeff_count ctx
  3363. currMB->subblock_x = block_x; // position for coeff_count ctx
  3364. pos_scan_4x4 = pos_scan4x4;
  3365. rl = readRunLevel_CABAC(currMB, de_cabac_dc, context);
  3366. if (rl.level != 0) /* leave if level == 0 */
  3367. {
  3368. pos_scan_4x4 += rl.run;
  3369. *cbp_blk |= 16 << (block_y + (block_x >> 2)) ;
  3370. block[*pos_scan_4x4++] = rl.level;
  3371. for(k = 0; k < 16; ++k)
  3372. {
  3373. rl = readRunLevel_CABAC(currMB, de_cabac_ac, context);
  3374. if (rl.level != 0) /* leave if level == 0 */
  3375. {
  3376. pos_scan_4x4 += rl.run;
  3377. block[*pos_scan_4x4++] = rl.level;
  3378. }
  3379. else
  3380. break;
  3381. }
  3382. }
  3383. block = (int16_t *)(*blocks++);
  3384. currMB->subblock_x += 4; // position for coeff_count ctx
  3385. pos_scan_4x4 = pos_scan4x4;
  3386. rl = readRunLevel_CABAC(currMB, de_cabac_dc, context);
  3387. if (rl.level != 0) /* leave if level == 0 */
  3388. {
  3389. pos_scan_4x4 += rl.run;
  3390. *cbp_blk |= 32 << (block_y + (block_x >> 2)) ;
  3391. block[*pos_scan_4x4++] = rl.level;
  3392. for(k = 0; k < 16; ++k)
  3393. {
  3394. rl = readRunLevel_CABAC(currMB, de_cabac_ac, context);
  3395. if (rl.level != 0) /* leave if level == 0 */
  3396. {
  3397. pos_scan_4x4 += rl.run;
  3398. block[*pos_scan_4x4++] = rl.level;
  3399. }
  3400. else
  3401. break;
  3402. }
  3403. }
  3404. }
  3405. #if defined(_DEBUG) || defined(_M_IX64)
  3406. static void inv_level_coefficients(h264_short_block_t *blocks, const int (*InvLevelScale)[4], int qp_per)
  3407. {
  3408. int j, b;
  3409. for (b = 0;b<4;b++)
  3410. {
  3411. h264_short_block_row_t *block = blocks[b];
  3412. for (j = 0; j < 4; ++j)
  3413. {
  3414. if (block[j][0]) block[j][0]= rshift_rnd_sf((block[j][0] * InvLevelScale[j][0]) << qp_per, 4);
  3415. if (block[j][1]) block[j][1]= rshift_rnd_sf((block[j][1] * InvLevelScale[j][1]) << qp_per, 4);
  3416. if (block[j][2]) block[j][2]= rshift_rnd_sf((block[j][2] * InvLevelScale[j][2]) << qp_per, 4);
  3417. if (block[j][3]) block[j][3]= rshift_rnd_sf((block[j][3] * InvLevelScale[j][3]) << qp_per, 4);
  3418. }
  3419. }
  3420. }
  3421. #else
  3422. void inv_level_coefficients(h264_short_block_t *blocks, const int (*InvLevelScale)[4], int qp_per);
  3423. #endif
  3424. static void inv_level_coefficients_AC(h264_short_block_t *blocks, const int (*InvLevelScale)[4], int qp_per)
  3425. {
  3426. int b;
  3427. for (b = 0;b<4;b++)
  3428. {
  3429. h264_short_block_row_t *block = blocks[b];
  3430. if (block[0][1]) block[0][1]= rshift_rnd_sf((block[0][1] * InvLevelScale[0][1]) << qp_per, 4);
  3431. if (block[0][2]) block[0][2]= rshift_rnd_sf((block[0][2] * InvLevelScale[0][2]) << qp_per, 4);
  3432. if (block[0][3]) block[0][3]= rshift_rnd_sf((block[0][3] * InvLevelScale[0][3]) << qp_per, 4);
  3433. if (block[1][0]) block[1][0]= rshift_rnd_sf((block[1][0] * InvLevelScale[1][0]) << qp_per, 4);
  3434. if (block[1][1]) block[1][1]= rshift_rnd_sf((block[1][1] * InvLevelScale[1][1]) << qp_per, 4);
  3435. if (block[1][2]) block[1][2]= rshift_rnd_sf((block[1][2] * InvLevelScale[1][2]) << qp_per, 4);
  3436. if (block[1][3]) block[1][3]= rshift_rnd_sf((block[1][3] * InvLevelScale[1][3]) << qp_per, 4);
  3437. if (block[2][0]) block[2][0]= rshift_rnd_sf((block[2][0] * InvLevelScale[2][0]) << qp_per, 4);
  3438. if (block[2][1]) block[2][1]= rshift_rnd_sf((block[2][1] * InvLevelScale[2][1]) << qp_per, 4);
  3439. if (block[2][2]) block[2][2]= rshift_rnd_sf((block[2][2] * InvLevelScale[2][2]) << qp_per, 4);
  3440. if (block[2][3]) block[2][3]= rshift_rnd_sf((block[2][3] * InvLevelScale[2][3]) << qp_per, 4);
  3441. if (block[3][0]) block[3][0]= rshift_rnd_sf((block[3][0] * InvLevelScale[3][0]) << qp_per, 4);
  3442. if (block[3][1]) block[3][1]= rshift_rnd_sf((block[3][1] * InvLevelScale[3][1]) << qp_per, 4);
  3443. if (block[3][2]) block[3][2]= rshift_rnd_sf((block[3][2] * InvLevelScale[3][2]) << qp_per, 4);
  3444. if (block[3][3]) block[3][3]= rshift_rnd_sf((block[3][3] * InvLevelScale[3][3]) << qp_per, 4);
  3445. }
  3446. }
  3447. /*!
  3448. ************************************************************************
  3449. * \brief
  3450. * Get coefficients (run/level) of all 4x4 blocks in a MB
  3451. * from the NAL (CABAC Mode)
  3452. ************************************************************************
  3453. */
  3454. static void readCompCoeff4x4MB_CABAC(Macroblock *currMB, ColorPlane pl, int intra, int (*InvLevelScale4x4)[4], int qp_per, int cbp)
  3455. {
  3456. Slice *currSlice = currMB->p_Slice;
  3457. VideoParameters *p_Vid = currMB->p_Vid;
  3458. int start_scan = IS_I16MB (currMB)? 1 : 0;
  3459. int64 *cbp_blk = &currMB->cbp_blk[pl];
  3460. int context;
  3461. h264_short_block_t *blocks = currSlice->cof4[pl];
  3462. currMB->is_intra_block = intra;
  3463. if( pl == PLANE_Y || IS_INDEPENDENT(p_Vid) )
  3464. context = (IS_I16MB(currMB) ? LUMA_16AC: LUMA_4x4);
  3465. else if (pl == PLANE_U)
  3466. context = (IS_I16MB(currMB) ? CB_16AC: CB_4x4);
  3467. else
  3468. context = (IS_I16MB(currMB) ? CR_16AC: CR_4x4);
  3469. if (start_scan == 0)
  3470. {
  3471. if (currMB->is_lossless == FALSE)
  3472. {
  3473. if (cbp & 1)
  3474. {
  3475. readCompCoeff4x4SMB_CABAC(currMB, context, &blocks[0], 0, 0, cbp_blk);
  3476. inv_level_coefficients(&blocks[0], InvLevelScale4x4, qp_per);
  3477. }
  3478. if (cbp & 2)
  3479. {
  3480. readCompCoeff4x4SMB_CABAC(currMB, context, &blocks[4], 0, 8, cbp_blk);
  3481. inv_level_coefficients(&blocks[4], InvLevelScale4x4, qp_per);
  3482. }
  3483. if (cbp & 4)
  3484. {
  3485. readCompCoeff4x4SMB_CABAC(currMB, context, &blocks[8], 8, 0, cbp_blk);
  3486. inv_level_coefficients(&blocks[8], InvLevelScale4x4, qp_per);
  3487. }
  3488. if (cbp & 8)
  3489. {
  3490. readCompCoeff4x4SMB_CABAC(currMB, context, &blocks[12], 8, 8, cbp_blk);
  3491. inv_level_coefficients(&blocks[12], InvLevelScale4x4, qp_per);
  3492. }
  3493. }
  3494. else
  3495. {
  3496. if (cbp & 1)
  3497. readCompCoeff4x4SMB_CABAC(currMB, context, &blocks[0], 0, 0, cbp_blk);
  3498. if (cbp & 2)
  3499. readCompCoeff4x4SMB_CABAC(currMB, context, &blocks[4], 0, 8, cbp_blk);
  3500. if (cbp & 4)
  3501. readCompCoeff4x4SMB_CABAC(currMB, context, &blocks[8], 8, 0, cbp_blk);
  3502. if (cbp & 8)
  3503. readCompCoeff4x4SMB_CABAC(currMB, context, &blocks[12], 8, 8, cbp_blk);
  3504. }
  3505. }
  3506. else
  3507. {
  3508. if (currMB->is_lossless == FALSE)
  3509. {
  3510. if (cbp & 1) // are there any coeff in current block at all
  3511. {
  3512. readCompCoeff4x4SMB_I16MB_CABAC(currMB, context, &blocks[0], 0, 0, cbp_blk);
  3513. inv_level_coefficients_AC(&blocks[0], InvLevelScale4x4, qp_per);
  3514. }
  3515. if (cbp & 2) // are there any coeff in current block at all
  3516. {
  3517. readCompCoeff4x4SMB_I16MB_CABAC(currMB, context, &blocks[4], 0, 8, cbp_blk);
  3518. inv_level_coefficients_AC(&blocks[4], InvLevelScale4x4, qp_per);
  3519. }
  3520. if (cbp & 4) // are there any coeff in current block at all
  3521. {
  3522. readCompCoeff4x4SMB_I16MB_CABAC(currMB, context, &blocks[8], 8, 0, cbp_blk);
  3523. inv_level_coefficients_AC(&blocks[8], InvLevelScale4x4, qp_per);
  3524. }
  3525. if (cbp & 8) // are there any coeff in current block at all
  3526. {
  3527. readCompCoeff4x4SMB_I16MB_CABAC(currMB, context, &blocks[12], 8, 8, cbp_blk);
  3528. inv_level_coefficients_AC(&blocks[12], InvLevelScale4x4, qp_per);
  3529. }
  3530. }
  3531. else
  3532. {
  3533. if (cbp & 1)
  3534. readCompCoeff4x4SMB_I16MB_CABAC(currMB, context, &blocks[0], 0, 0, cbp_blk);
  3535. if (cbp & 2)
  3536. readCompCoeff4x4SMB_I16MB_CABAC(currMB, context, &blocks[4], 0, 8, cbp_blk);
  3537. if (cbp & 4)
  3538. readCompCoeff4x4SMB_I16MB_CABAC(currMB, context, &blocks[8], 8, 0, cbp_blk);
  3539. if (cbp & 8)
  3540. readCompCoeff4x4SMB_I16MB_CABAC(currMB, context, &blocks[12], 8, 8, cbp_blk);
  3541. }
  3542. }
  3543. }
  3544. /*!
  3545. ************************************************************************
  3546. * \brief
  3547. * Get coefficients (run/level) of one 8x8 block
  3548. * from the NAL (CABAC Mode)
  3549. ************************************************************************
  3550. */
  3551. static void readCompCoeff8x8_CABAC_Lossless(Macroblock *currMB, ColorPlane pl, int b8)
  3552. {
  3553. if (currMB->cbp & (1<<b8)) // are there any coefficients in the current block
  3554. {
  3555. VideoParameters *p_Vid = currMB->p_Vid;
  3556. int transform_pl = IS_INDEPENDENT(p_Vid) ? p_Vid->colour_plane_id : pl;
  3557. int scan;
  3558. short *tcoeffs;
  3559. int k;
  3560. RunLevel rl;
  3561. int context;
  3562. DataPartition *dP;
  3563. Slice *currSlice = currMB->p_Slice;
  3564. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  3565. int cbp_mask = (int64) 51 << (4 * b8 - 2 * (b8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position
  3566. int64 *cur_cbp = &currMB->cbp_blk[pl];
  3567. // select scan type
  3568. const byte *pos_scan8x8 = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8_1D : FIELD_SCAN8x8_1D;
  3569. int qp_per = p_Vid->qp_per_matrix[ currMB->qp_scaled[pl] ];
  3570. int qp_rem = p_Vid->qp_rem_matrix[ currMB->qp_scaled[pl] ];
  3571. const int *InvLevelScale8x8 = IS_INTRA(currMB)? currSlice->InvLevelScale8x8_Intra[transform_pl][qp_rem] : currSlice->InvLevelScale8x8_Inter[transform_pl][qp_rem];
  3572. currMB->is_intra_block = IS_INTRA(currMB);
  3573. // === set offset in current macroblock ===
  3574. tcoeffs = (short *)(currSlice->mb_rres8[pl][b8]);
  3575. currMB->subblock_x = (b8&0x01) << 3; // position for coeff_count ctx
  3576. currMB->subblock_y = (b8 >> 1) << 3; // position for coeff_count ctx
  3577. if (pl==PLANE_Y || IS_INDEPENDENT(p_Vid))
  3578. context = LUMA_8x8;
  3579. else if (pl==PLANE_U)
  3580. context = CB_8x8;
  3581. else
  3582. context = CR_8x8;
  3583. for(k=0; (k < 65);++k)
  3584. {
  3585. //============ read =============
  3586. /*
  3587. * make distinction between INTRA and INTER coded
  3588. * luminance coefficients
  3589. */
  3590. int type = ((currMB->is_intra_block == 1)
  3591. ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA)
  3592. : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));
  3593. dP = &(currSlice->partArr[partMap[type]]);
  3594. rl = readRunLevel_CABAC(currMB, &(dP->de_cabac), context);
  3595. //============ decode =============
  3596. if (rl.level != 0) /* leave if level == 0 */
  3597. {
  3598. pos_scan8x8 += rl.run;
  3599. scan = *pos_scan8x8++;
  3600. *cur_cbp |= cbp_mask;
  3601. tcoeffs[scan] = rl.level;
  3602. }
  3603. else
  3604. break;
  3605. }
  3606. }
  3607. }
  3608. static void readCompCoeff8x8_CABAC_Intra(Macroblock *currMB, ColorPlane pl, int b8)
  3609. {
  3610. if (currMB->cbp & (1<<b8)) // are there any coefficients in the current block
  3611. {
  3612. VideoParameters *p_Vid = currMB->p_Vid;
  3613. int transform_pl = IS_INDEPENDENT(p_Vid) ? p_Vid->colour_plane_id : pl;
  3614. int scan;
  3615. short *tcoeffs;
  3616. RunLevel rl;
  3617. int k;
  3618. int context;
  3619. DecodingEnvironment *cabac;
  3620. Slice *currSlice = currMB->p_Slice;
  3621. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  3622. int cbp_mask = (int64) 51 << (4 * b8 - 2 * (b8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position
  3623. int64 *cur_cbp = &currMB->cbp_blk[pl];
  3624. // select scan type
  3625. const byte *pos_scan8x8 = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8_1D : FIELD_SCAN8x8_1D;
  3626. int qp_per = p_Vid->qp_per_matrix[ currMB->qp_scaled[pl] ];
  3627. int qp_rem = p_Vid->qp_rem_matrix[ currMB->qp_scaled[pl] ];
  3628. const int *InvLevelScale8x8 = currSlice->InvLevelScale8x8_Intra[transform_pl][qp_rem];
  3629. currMB->is_intra_block = 1;
  3630. // === set offset in current macroblock ===
  3631. tcoeffs = (short *)(currSlice->mb_rres8[pl][b8]);
  3632. currMB->subblock_x = (b8&0x01) << 3; // position for coeff_count ctx
  3633. currMB->subblock_y = (b8 >> 1) << 3; // position for coeff_count ctx
  3634. if (pl==PLANE_Y || IS_INDEPENDENT(p_Vid))
  3635. context = LUMA_8x8;
  3636. else if (pl==PLANE_U)
  3637. context = CB_8x8;
  3638. else
  3639. context = CR_8x8;
  3640. // Read DC
  3641. cabac = &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]].de_cabac);
  3642. rl = readRunLevel_CABAC(currMB, cabac, context);
  3643. //============ decode =============
  3644. if (rl.level != 0) /* leave if level == 0 */
  3645. {
  3646. *cur_cbp |= cbp_mask;
  3647. pos_scan8x8 += rl.run;
  3648. scan = *pos_scan8x8++;
  3649. tcoeffs[scan] = rshift_rnd_sf((rl.level * InvLevelScale8x8[scan]) << qp_per, 6); // dequantization
  3650. // AC coefficients
  3651. cabac = &(currSlice->partArr[partMap[SE_LUM_AC_INTRA]].de_cabac);
  3652. k = 64;
  3653. do
  3654. {
  3655. rl = readRunLevel_CABAC(currMB, cabac, context);
  3656. //============ decode =============
  3657. if (rl.level != 0) /* leave if level == 0 */
  3658. {
  3659. pos_scan8x8 += rl.run;
  3660. scan = *pos_scan8x8++;
  3661. tcoeffs[scan] = rshift_rnd_sf((rl.level * InvLevelScale8x8[scan]) << qp_per, 6); // dequantization
  3662. }
  3663. else
  3664. break;
  3665. } while (--k);
  3666. }
  3667. }
  3668. }
  3669. static void readCompCoeff8x8_CABAC_Inter(Macroblock *currMB, ColorPlane pl, int b8)
  3670. {
  3671. if (currMB->cbp & (1<<b8)) // are there any coefficients in the current block
  3672. {
  3673. VideoParameters *p_Vid = currMB->p_Vid;
  3674. int transform_pl = IS_INDEPENDENT(p_Vid) ? p_Vid->colour_plane_id : pl;
  3675. int scan;
  3676. short *tcoeffs;
  3677. int k;
  3678. RunLevel rl;
  3679. int context;
  3680. DecodingEnvironment *cabac;
  3681. Slice *currSlice = currMB->p_Slice;
  3682. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  3683. int cbp_mask = (int64) 51 << (4 * b8 - 2 * (b8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position
  3684. int64 *cur_cbp = &currMB->cbp_blk[pl];
  3685. // select scan type
  3686. const byte *pos_scan8x8 = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8_1D : FIELD_SCAN8x8_1D;
  3687. int qp_per = p_Vid->qp_per_matrix[ currMB->qp_scaled[pl] ];
  3688. int qp_rem = p_Vid->qp_rem_matrix[ currMB->qp_scaled[pl] ];
  3689. const int *InvLevelScale8x8 = currSlice->InvLevelScale8x8_Inter[transform_pl][qp_rem];
  3690. currMB->is_intra_block = 0;
  3691. // === set offset in current macroblock ===
  3692. tcoeffs = (short *)(currSlice->mb_rres8[pl][b8]);
  3693. currMB->subblock_x = (b8&0x01) << 3; // position for coeff_count ctx
  3694. currMB->subblock_y = (b8 >> 1) << 3; // position for coeff_count ctx
  3695. if (pl==PLANE_Y || IS_INDEPENDENT(p_Vid))
  3696. context = LUMA_8x8;
  3697. else if (pl==PLANE_U)
  3698. context = CB_8x8;
  3699. else
  3700. context = CR_8x8;
  3701. // Read DC
  3702. cabac = &(currSlice->partArr[partMap[SE_LUM_DC_INTER]].de_cabac);
  3703. rl = readRunLevel_CABAC(currMB, cabac, context);
  3704. //============ decode =============
  3705. if (rl.level != 0) /* leave if level == 0 */
  3706. {
  3707. *cur_cbp |= cbp_mask;
  3708. pos_scan8x8 += rl.run;
  3709. scan = *pos_scan8x8++;
  3710. tcoeffs[scan] = rshift_rnd_sf((rl.level * InvLevelScale8x8[scan]) << qp_per, 6); // dequantization
  3711. // AC coefficients
  3712. cabac = &(currSlice->partArr[partMap[SE_LUM_AC_INTER]].de_cabac);
  3713. k=64;
  3714. do
  3715. {
  3716. rl = readRunLevel_CABAC(currMB, cabac, context);
  3717. //============ decode =============
  3718. if (rl.level != 0) /* leave if level == 0 */
  3719. {
  3720. pos_scan8x8 += rl.run;
  3721. scan = *pos_scan8x8++;
  3722. tcoeffs[scan] = rshift_rnd_sf((rl.level * InvLevelScale8x8[scan]) << qp_per, 6); // dequantization
  3723. }
  3724. else
  3725. break;
  3726. } while (--k);
  3727. }
  3728. }
  3729. }
  3730. /*!
  3731. ************************************************************************
  3732. * \brief
  3733. * Get coefficients (run/level) of 8x8 blocks in a MB
  3734. * from the NAL (CABAC Mode)
  3735. ************************************************************************
  3736. */
  3737. static void readCompCoeff8x8MB_CABAC(Macroblock *currMB, ColorPlane pl)
  3738. {
  3739. //======= 8x8 transform size & CABAC ========
  3740. if(currMB->is_lossless == FALSE)
  3741. {
  3742. if (IS_INTRA(currMB))
  3743. {
  3744. readCompCoeff8x8_CABAC_Intra(currMB, pl, 0);
  3745. readCompCoeff8x8_CABAC_Intra(currMB, pl, 1);
  3746. readCompCoeff8x8_CABAC_Intra(currMB, pl, 2);
  3747. readCompCoeff8x8_CABAC_Intra(currMB, pl, 3);
  3748. }
  3749. else
  3750. {
  3751. readCompCoeff8x8_CABAC_Inter(currMB, pl, 0);
  3752. readCompCoeff8x8_CABAC_Inter(currMB, pl, 1);
  3753. readCompCoeff8x8_CABAC_Inter(currMB, pl, 2);
  3754. readCompCoeff8x8_CABAC_Inter(currMB, pl, 3);
  3755. }
  3756. }
  3757. else
  3758. {
  3759. readCompCoeff8x8_CABAC_Lossless(currMB, pl, 0);
  3760. readCompCoeff8x8_CABAC_Lossless(currMB, pl, 1);
  3761. readCompCoeff8x8_CABAC_Lossless(currMB, pl, 2);
  3762. readCompCoeff8x8_CABAC_Lossless(currMB, pl, 3);
  3763. }
  3764. }
  3765. /*!
  3766. ************************************************************************
  3767. * \brief
  3768. * Get coefficients (run/level) of 4x4 blocks in a MB
  3769. * from the NAL (CABAC Mode)
  3770. ************************************************************************
  3771. */
  3772. static void readCompCoeff4x4MB_CAVLC (Macroblock *currMB, ColorPlane pl, int (*InvLevelScale4x4)[4], int qp_per, int cbp, h264_4x4_byte nzcoeff)
  3773. {
  3774. int block_y, block_x, b8;
  3775. int i, j, k;
  3776. int i0, j0;
  3777. __declspec(align(32)) int levarr[16], runarr[16];
  3778. int numcoeff;
  3779. Slice *currSlice = currMB->p_Slice;
  3780. VideoParameters *p_Vid = currMB->p_Vid;
  3781. const byte (*pos_scan4x4)[2] = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
  3782. const byte *pos_scan_4x4 = pos_scan4x4[0];
  3783. int start_scan = IS_I16MB(currMB) ? 1 : 0;
  3784. int64 *cur_cbp = &currMB->cbp_blk[pl];
  3785. int coef_ctr, cur_context;
  3786. memzero64(levarr);
  3787. memzero64(runarr);
  3788. if (IS_I16MB(currMB))
  3789. {
  3790. if (pl == PLANE_Y)
  3791. cur_context = LUMA_INTRA16x16AC;
  3792. else if (pl == PLANE_U)
  3793. cur_context = CB_INTRA16x16AC;
  3794. else
  3795. cur_context = CR_INTRA16x16AC;
  3796. }
  3797. else
  3798. {
  3799. if (pl == PLANE_Y)
  3800. cur_context = LUMA;
  3801. else if (pl == PLANE_U)
  3802. cur_context = CB;
  3803. else
  3804. cur_context = CR;
  3805. }
  3806. if (currMB->is_lossless == FALSE)
  3807. {
  3808. for (block_y = 0; block_y < 4; block_y += 2) /* all modes */
  3809. {
  3810. for (block_x = 0; block_x < 4; block_x += 2)
  3811. {
  3812. b8 = (block_y + (block_x >> 1));
  3813. if (cbp & (1 << b8)) // test if the block contains any coefficients
  3814. {
  3815. for (j=block_y << 2; j < (block_y + 2) << 2; j += BLOCK_SIZE)
  3816. {
  3817. for (i=block_x << 2; i < (block_x + 2) << 2; i += BLOCK_SIZE)
  3818. {
  3819. readCoeff4x4_CAVLC(currMB, cur_context, i >> 2, j >> 2, levarr, runarr, &numcoeff);
  3820. pos_scan_4x4 = pos_scan4x4[start_scan];
  3821. for (k = 0; k < numcoeff; ++k)
  3822. {
  3823. if (levarr[k] != 0)
  3824. {
  3825. pos_scan_4x4 += (runarr[k] << 1);
  3826. i0 = *pos_scan_4x4++;
  3827. j0 = *pos_scan_4x4++;
  3828. // inverse quant for 4x4 transform only
  3829. *cur_cbp |= (int64) 1 << (j + (i >> 2));
  3830. currSlice->cof4[pl][cof4_pos_to_subblock[j>>2][i>>2]][j0][i0]= rshift_rnd_sf((levarr[k] * InvLevelScale4x4[j0][i0])<<qp_per, 4);
  3831. }
  3832. }
  3833. }
  3834. }
  3835. }
  3836. else
  3837. {
  3838. for (j=0; j < 2; j++)
  3839. {
  3840. for (i=0;i<2;i++)
  3841. {
  3842. nzcoeff[block_y+j][block_x+i]=0;
  3843. }
  3844. }
  3845. }
  3846. }
  3847. }
  3848. }
  3849. else
  3850. {
  3851. for (block_y=0; block_y < 4; block_y += 2) /* all modes */
  3852. {
  3853. for (block_x=0; block_x < 4; block_x += 2)
  3854. {
  3855. b8 = 2*(block_y>>1) + (block_x>>1);
  3856. if (cbp & (1<<b8)) /* are there any coeff in current block at all */
  3857. {
  3858. for (j=block_y; j < block_y+2; ++j)
  3859. {
  3860. for (i=block_x; i < block_x+2; ++i)
  3861. {
  3862. readCoeff4x4_CAVLC(currMB, cur_context, i, j, levarr, runarr, &numcoeff);
  3863. coef_ctr = start_scan - 1;
  3864. for (k = 0; k < numcoeff; ++k)
  3865. {
  3866. if (levarr[k] != 0)
  3867. {
  3868. coef_ctr += runarr[k]+1;
  3869. i0=pos_scan4x4[coef_ctr][0];
  3870. j0=pos_scan4x4[coef_ctr][1];
  3871. *cur_cbp |= (int64) 1 << ((j<<2) + i);
  3872. currSlice->cof4[pl][cof4_pos_to_subblock[j>>2][i>>2]][j0][i0]= levarr[k];
  3873. }
  3874. }
  3875. }
  3876. }
  3877. }
  3878. else
  3879. {
  3880. for (j=0; j < 2; j++)
  3881. {
  3882. for (i=0;i<2;i++)
  3883. {
  3884. nzcoeff[block_y+j][block_x+i]=0;
  3885. }
  3886. }
  3887. }
  3888. }
  3889. }
  3890. }
  3891. }
  3892. /*!
  3893. ************************************************************************
  3894. * \brief
  3895. * Get coefficients (run/level) of 4x4 blocks in a MB
  3896. * from the NAL (CABAC Mode)
  3897. ************************************************************************
  3898. */
  3899. static void readCompCoeff8x8MB_CAVLC (Macroblock *currMB, ColorPlane pl, const int *InvLevelScale8x8, int qp_per, int cbp, h264_4x4_byte nzcoeff)
  3900. {
  3901. int block_y, block_x, b4, b8;
  3902. int i,j,k;
  3903. int scan;
  3904. __declspec(align(32)) int levarr[16] = {0}, runarr[16] = {0};
  3905. int numcoeff;
  3906. Slice *currSlice = currMB->p_Slice;
  3907. VideoParameters *p_Vid = currMB->p_Vid;
  3908. const byte *pos_scan8x8 = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8_1D : FIELD_SCAN8x8_1D;
  3909. int start_scan = IS_I16MB(currMB) ? 1 : 0;
  3910. int64 *cur_cbp = &currMB->cbp_blk[pl];
  3911. int coef_ctr, cur_context;
  3912. short *coefficients;
  3913. if (IS_I16MB(currMB))
  3914. {
  3915. if (pl == PLANE_Y)
  3916. cur_context = LUMA_INTRA16x16AC;
  3917. else if (pl == PLANE_U)
  3918. cur_context = CB_INTRA16x16AC;
  3919. else
  3920. cur_context = CR_INTRA16x16AC;
  3921. }
  3922. else
  3923. {
  3924. if (pl == PLANE_Y)
  3925. cur_context = LUMA;
  3926. else if (pl == PLANE_U)
  3927. cur_context = CB;
  3928. else
  3929. cur_context = CR;
  3930. }
  3931. if (currMB->is_lossless == FALSE)
  3932. {
  3933. for (block_y=0; block_y < 4; block_y += 2) /* all modes */
  3934. {
  3935. for (block_x=0; block_x < 4; block_x += 2)
  3936. {
  3937. b8 = block_y + (block_x>>1);
  3938. coefficients =(short *)(currSlice->mb_rres8[pl][b8]);
  3939. if (cbp & (1<<b8)) /* are there any coeff in current block at all */
  3940. {
  3941. for (j=block_y; j < block_y+2; ++j)
  3942. {
  3943. for (i=block_x; i < block_x+2; ++i)
  3944. {
  3945. readCoeff4x4_CAVLC(currMB, cur_context, i, j, levarr, runarr, &numcoeff);
  3946. coef_ctr = start_scan - 1;
  3947. for (k = 0; k < numcoeff; ++k)
  3948. {
  3949. if (levarr[k] != 0)
  3950. {
  3951. coef_ctr += runarr[k]+1;
  3952. // do same as CABAC for deblocking: any coeff in the 8x8 marks all the 4x4s
  3953. //as containing coefficients
  3954. *cur_cbp |= 51 << ((block_y<<2) + block_x);
  3955. b4 = (coef_ctr << 2) + 2*(j - block_y)+(i - block_x);
  3956. scan = pos_scan8x8[b4];
  3957. coefficients[scan] = rshift_rnd_sf((levarr[k] * InvLevelScale8x8[scan])<<qp_per, 6); // dequantization
  3958. }
  3959. }//else (!currMB->luma_transform_size_8x8_flag)
  3960. }
  3961. }
  3962. }
  3963. else
  3964. {
  3965. for (j=block_y; j < block_y+2; ++j)
  3966. {
  3967. memset(&nzcoeff[j][block_x], 0, 2 * sizeof(byte));
  3968. }
  3969. }
  3970. }
  3971. }
  3972. }
  3973. else // inverse quant for 8x8 transform
  3974. {
  3975. for (block_y=0; block_y < 4; block_y += 2) /* all modes */
  3976. {
  3977. for (block_x=0; block_x < 4; block_x += 2)
  3978. {
  3979. b8 = 2*(block_y>>1) + (block_x>>1);
  3980. coefficients =(short *)(currSlice->mb_rres8[pl][b8]);
  3981. if (cbp & (1<<b8)) /* are there any coeff in current block at all */
  3982. {
  3983. for (j=block_y; j < block_y+2; ++j)
  3984. {
  3985. for (i=block_x; i < block_x+2; ++i)
  3986. {
  3987. readCoeff4x4_CAVLC(currMB, cur_context, i, j, levarr, runarr, &numcoeff);
  3988. coef_ctr = start_scan - 1;
  3989. for (k = 0; k < numcoeff; ++k)
  3990. {
  3991. if (levarr[k] != 0)
  3992. {
  3993. coef_ctr += runarr[k]+1;
  3994. // do same as CABAC for deblocking: any coeff in the 8x8 marks all the 4x4s
  3995. //as containing coefficients
  3996. *cur_cbp |= 51 << ((block_y<<2) + block_x);
  3997. b4 = 2*(j-block_y)+(i-block_x);
  3998. scan=pos_scan8x8[coef_ctr*4+b4];
  3999. coefficients[scan] = levarr[k];
  4000. }
  4001. }
  4002. }
  4003. }
  4004. }
  4005. else
  4006. {
  4007. for (j=block_y; j < block_y+2; ++j)
  4008. {
  4009. memset(&nzcoeff[j][block_x], 0, 2 * sizeof(byte));
  4010. }
  4011. }
  4012. }
  4013. }
  4014. }
  4015. }
  4016. /*!
  4017. ************************************************************************
  4018. * \brief
  4019. * Data partitioning: Check if neighboring macroblock is needed for
  4020. * CAVLC context decoding, and disable current MB if data partition
  4021. * is missing.
  4022. ************************************************************************
  4023. */
  4024. static void check_dp_neighbors (Macroblock *currMB)
  4025. {
  4026. VideoParameters *p_Vid = currMB->p_Vid;
  4027. if (IS_INTER (currMB) || (IS_INTRA (currMB) && !(p_Vid->active_pps->constrained_intra_pred_flag)) )
  4028. {
  4029. PixelPos up, left;
  4030. p_Vid->getNeighbourLeft(currMB, p_Vid->mb_size[1], &left);
  4031. p_Vid->getNeighbourUp(currMB, p_Vid->mb_size[1], &up);
  4032. if (left.available)
  4033. {
  4034. currMB->dpl_flag |= p_Vid->mb_data[left.mb_addr].dpl_flag;
  4035. }
  4036. if (up.available)
  4037. {
  4038. currMB->dpl_flag |= p_Vid->mb_data[up.mb_addr].dpl_flag;
  4039. }
  4040. }
  4041. }
  4042. /*!
  4043. ************************************************************************
  4044. * \brief
  4045. * Get coded block pattern and coefficients (run/level)
  4046. * from the NAL
  4047. ************************************************************************
  4048. */
  4049. static void read_CBP_and_coeffs_from_NAL_CABAC(Macroblock *currMB)
  4050. {
  4051. int i,j,k;
  4052. int cbp;
  4053. SyntaxElement currSE;
  4054. DataPartition *dP = NULL;
  4055. Slice *currSlice = currMB->p_Slice;
  4056. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  4057. int coef_ctr, i0, j0, b8;
  4058. int ll;
  4059. RunLevel rl;
  4060. int qp_per, qp_rem;
  4061. VideoParameters *p_Vid = currMB->p_Vid;
  4062. int intra = IS_INTRA (currMB);
  4063. int smb = ((p_Vid->type==SP_SLICE) && !intra) || (p_Vid->type == SI_SLICE && currMB->mb_type == SI4MB);
  4064. int uv;
  4065. int qp_per_uv[2];
  4066. int qp_rem_uv[2];
  4067. int temp[4];
  4068. int b4;
  4069. StorablePicture *dec_picture = p_Vid->dec_picture;
  4070. int yuv = dec_picture->chroma_format_idc - 1;
  4071. int m6[4];
  4072. int need_transform_size_flag;
  4073. int (*InvLevelScale4x4)[4] = NULL;
  4074. // select scan type
  4075. const byte (*pos_scan4x4)[2] = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
  4076. const byte *pos_scan4x4_1d = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN_1D : FIELD_SCAN_1D;
  4077. const byte *pos_scan4x4_dc = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN_DC : FIELD_SCAN_DC;
  4078. const byte *pos_scan_4x4;
  4079. // QPI
  4080. //init constants for every chroma qp offset
  4081. if (dec_picture->chroma_format_idc != YUV400)
  4082. {
  4083. for (i=0; i<2; ++i)
  4084. {
  4085. qp_per_uv[i] = p_Vid->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
  4086. qp_rem_uv[i] = p_Vid->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
  4087. }
  4088. }
  4089. // read CBP if not new intra mode
  4090. if (!IS_I16MB (currMB))
  4091. {
  4092. //===== C B P =====
  4093. //---------------------
  4094. int type = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
  4095. ? SE_CBP_INTRA
  4096. : SE_CBP_INTER;
  4097. dP = &(currSlice->partArr[partMap[type]]);
  4098. currMB->cbp = cbp = readCBP_CABAC(currMB, &(dP->de_cabac));
  4099. TRACE_STRING("coded_block_pattern");
  4100. //============= Transform size flag for INTER MBs =============
  4101. //-------------------------------------------------------------
  4102. need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
  4103. (IS_DIRECT(currMB) && p_Vid->active_sps->direct_8x8_inference_flag) ||
  4104. (currMB->NoMbPartLessThan8x8Flag))
  4105. && currMB->mb_type != I8MB && currMB->mb_type != I4MB
  4106. && (currMB->cbp&15)
  4107. && p_Vid->Transform8x8Mode);
  4108. if (need_transform_size_flag)
  4109. {
  4110. dP = &(currSlice->partArr[partMap[SE_HEADER]]);
  4111. TRACE_STRING("transform_size_8x8_flag");
  4112. // read CAVLC transform_size_8x8_flag
  4113. currMB->luma_transform_size_8x8_flag = readMB_transform_size_flag_CABAC(currMB, &(dP->de_cabac));
  4114. }
  4115. //===== DQUANT =====
  4116. //----------------------
  4117. // Delta quant only if nonzero coeffs
  4118. if (cbp !=0)
  4119. {
  4120. read_delta_quant_CABAC(&currSE, dP, currMB, partMap, (!intra) ? SE_DELTA_QUANT_INTER : SE_DELTA_QUANT_INTRA);
  4121. if (currSlice->dp_mode)
  4122. {
  4123. if (!intra && currSlice->dpC_NotPresent )
  4124. currMB->dpl_flag = 1;
  4125. if( intra && currSlice->dpB_NotPresent )
  4126. {
  4127. currMB->ei_flag = 1;
  4128. currMB->dpl_flag = 1;
  4129. }
  4130. // check for prediction from neighbours
  4131. check_dp_neighbors (currMB);
  4132. if (currMB->dpl_flag)
  4133. {
  4134. cbp = 0;
  4135. currMB->cbp = cbp;
  4136. }
  4137. }
  4138. }
  4139. }
  4140. else
  4141. {
  4142. cbp = currMB->cbp;
  4143. }
  4144. if (IS_I16MB (currMB)) // read DC coeffs for new intra modes
  4145. {
  4146. read_delta_quant_CABAC(&currSE, dP, currMB, partMap, SE_DELTA_QUANT_INTRA);
  4147. macroblock_set_dc_pred(p_Vid, currMB->block_x, currMB->block_y);
  4148. if (currSlice->dp_mode)
  4149. {
  4150. if (currSlice->dpB_NotPresent)
  4151. {
  4152. currMB->ei_flag = 1;
  4153. currMB->dpl_flag = 1;
  4154. }
  4155. check_dp_neighbors (currMB);
  4156. if (currMB->dpl_flag)
  4157. {
  4158. currMB->cbp = cbp = 0;
  4159. }
  4160. }
  4161. if (!currMB->dpl_flag)
  4162. {
  4163. pos_scan_4x4 = pos_scan4x4_dc;
  4164. {
  4165. dP = &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]);
  4166. currMB->is_intra_block = 1;
  4167. for(k = 0; k < 17 ; k++)
  4168. {
  4169. rl = readRunLevel_CABAC(currMB, &(dP->de_cabac), LUMA_16DC);
  4170. if (rl.level != 0) /* leave if level == 0 */
  4171. {
  4172. pos_scan_4x4 += rl.run;
  4173. currSlice->cof4[0][*pos_scan_4x4++][0][0] = rl.level;// add new intra DC coeff
  4174. }
  4175. else
  4176. break;
  4177. }
  4178. }
  4179. if(currMB->is_lossless == FALSE)
  4180. itrans_2(currMB, (ColorPlane) p_Vid->colour_plane_id);// transform new intra DC
  4181. }
  4182. }
  4183. update_qp(currMB, p_Vid->qp);
  4184. qp_per = p_Vid->qp_per_matrix[ currMB->qp_scaled[p_Vid->colour_plane_id] ];
  4185. qp_rem = p_Vid->qp_rem_matrix[ currMB->qp_scaled[p_Vid->colour_plane_id] ];
  4186. //init quant parameters for chroma
  4187. if (dec_picture->chroma_format_idc != YUV400)
  4188. {
  4189. for(i=0; i < 2; ++i)
  4190. {
  4191. qp_per_uv[i] = p_Vid->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
  4192. qp_rem_uv[i] = p_Vid->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
  4193. }
  4194. }
  4195. InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[p_Vid->colour_plane_id][qp_rem] : currSlice->InvLevelScale4x4_Inter[p_Vid->colour_plane_id][qp_rem];
  4196. // luma coefficients
  4197. {
  4198. //======= Other Modes & CABAC ========
  4199. //------------------------------------
  4200. if (cbp)
  4201. {
  4202. if(currMB->luma_transform_size_8x8_flag)
  4203. {
  4204. //======= 8x8 transform size & CABAC ========
  4205. readCompCoeff8x8MB_CABAC (currMB, PLANE_Y);
  4206. }
  4207. else
  4208. {
  4209. readCompCoeff4x4MB_CABAC (currMB, PLANE_Y, intra, InvLevelScale4x4, qp_per, cbp);
  4210. }
  4211. }
  4212. }
  4213. if ( p_Vid->active_sps->chroma_format_idc==YUV444 && !IS_INDEPENDENT(p_Vid) )
  4214. {
  4215. for (uv = 0; uv < 2; ++uv )
  4216. {
  4217. /*----------------------16x16DC Luma_Add----------------------*/
  4218. if (IS_I16MB (currMB)) // read DC coeffs for new intra modes
  4219. {
  4220. macroblock_set_dc_pred(p_Vid, currMB->block_x, currMB->block_y);
  4221. {
  4222. int context;
  4223. dP = &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]);
  4224. if( IS_INDEPENDENT(p_Vid) )
  4225. context = LUMA_16DC;
  4226. else
  4227. context = (uv==0) ? CB_16DC : CR_16DC;
  4228. currMB->is_intra_block = 1;
  4229. coef_ctr = -1;
  4230. for(k=0;k<17;++k)
  4231. {
  4232. rl = readRunLevel_CABAC(currMB, &dP->de_cabac, context);
  4233. if (rl.level != 0) // leave if level == 0
  4234. {
  4235. coef_ctr += rl.run + 1;
  4236. currSlice->cof4[uv + 1][pos_scan4x4_1d[coef_ctr]][0][0] = rl.level;
  4237. }
  4238. else
  4239. break;
  4240. } //k loop
  4241. } // else CAVLC
  4242. if(currMB->is_lossless == FALSE)
  4243. {
  4244. itrans_2(currMB, (ColorPlane) (uv + 1)); // transform new intra DC
  4245. }
  4246. } //IS_I16MB
  4247. update_qp(currMB, p_Vid->qp);
  4248. qp_per = p_Vid->qp_per_matrix[ (p_Vid->qp + p_Vid->bitdepth_luma_qp_scale) ];
  4249. qp_rem = p_Vid->qp_rem_matrix[ (p_Vid->qp + p_Vid->bitdepth_luma_qp_scale) ];
  4250. //init constants for every chroma qp offset
  4251. qp_per_uv[uv] = p_Vid->qp_per_matrix[ (currMB->qpc[uv] + p_Vid->bitdepth_chroma_qp_scale) ];
  4252. qp_rem_uv[uv] = p_Vid->qp_rem_matrix[ (currMB->qpc[uv] + p_Vid->bitdepth_chroma_qp_scale) ];
  4253. InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  4254. {
  4255. if (cbp)
  4256. {
  4257. if(currMB->luma_transform_size_8x8_flag)
  4258. {
  4259. //======= 8x8 transform size & CABAC ========
  4260. readCompCoeff8x8MB_CABAC(currMB, (ColorPlane) (PLANE_U + uv));
  4261. }
  4262. else //4x4
  4263. {
  4264. readCompCoeff4x4MB_CABAC(currMB, (ColorPlane) (PLANE_U + uv), intra, InvLevelScale4x4, qp_per_uv[uv], cbp);
  4265. }
  4266. }
  4267. }
  4268. }
  4269. } //444
  4270. else if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
  4271. {
  4272. //========================== CHROMA DC ============================
  4273. //-----------------------------------------------------------------
  4274. // chroma DC coeff
  4275. if(cbp>15)
  4276. {
  4277. if (dec_picture->chroma_format_idc == YUV420)
  4278. {
  4279. for (ll=0;ll<3;ll+=2)
  4280. {
  4281. uv = ll>>1;
  4282. InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  4283. //===================== CHROMA DC YUV420 ======================
  4284. memzero16(&currSlice->cofu[0]);
  4285. coef_ctr=-1;
  4286. {
  4287. int type = (intra ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
  4288. currMB->is_intra_block = intra;
  4289. currMB->is_v_block = ll;
  4290. dP = &(currSlice->partArr[partMap[type]]);
  4291. for(k = 0; k < (p_Vid->num_cdc_coeff + 1);++k)
  4292. {
  4293. rl = readRunLevel_CABAC(currMB, &(dP->de_cabac), CHROMA_DC);
  4294. if (rl.level != 0)
  4295. {
  4296. currMB->cbp_blk[0] |= 0xf0000 << (ll<<1) ;
  4297. coef_ctr += rl.run + 1;
  4298. // Bug: currSlice->cofu has only 4 entries, hence coef_ctr MUST be <4 (which is
  4299. // caught by the assert(). If it is bigger than 4, it starts patching the
  4300. // p_Vid->predmode pointer, which leads to bugs later on.
  4301. //
  4302. // This assert() should be left in the code, because it captures a very likely
  4303. // bug early when testing in error prone environments (or when testing NAL
  4304. // functionality).
  4305. assert (coef_ctr < p_Vid->num_cdc_coeff);
  4306. currSlice->cofu[coef_ctr&3]=rl.level;
  4307. }
  4308. else
  4309. break;
  4310. }
  4311. }
  4312. if (smb || (currMB->is_lossless == TRUE)) // check to see if MB type is SPred or SIntra4x4
  4313. {
  4314. currSlice->cof4[uv + 1][0][0][0] = currSlice->cofu[0];
  4315. currSlice->cof4[uv + 1][1][0][0] = currSlice->cofu[1];
  4316. currSlice->cof4[uv + 1][2][0][0] = currSlice->cofu[2];
  4317. currSlice->cof4[uv + 1][3][0][0] = currSlice->cofu[3];
  4318. }
  4319. else
  4320. {
  4321. ihadamard2x2(currSlice->cofu, temp);
  4322. currSlice->cof4[uv + 1][0][0][0] = (((temp[0] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
  4323. currSlice->cof4[uv + 1][1][0][0] = (((temp[1] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
  4324. currSlice->cof4[uv + 1][2][0][0] = (((temp[2] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
  4325. currSlice->cof4[uv + 1][3][0][0] = (((temp[3] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
  4326. }
  4327. }
  4328. }
  4329. else if (dec_picture->chroma_format_idc == YUV422)
  4330. {
  4331. for (ll=0;ll<3;ll+=2)
  4332. {
  4333. int (*InvLevelScale4x4)[4] = NULL;
  4334. uv = ll>>1;
  4335. {
  4336. h264_short_block_t *imgcof = currSlice->cof4[uv + 1];
  4337. int m3[2][4] = {{0,0,0,0},{0,0,0,0}};
  4338. int m4[2][4] = {{0,0,0,0},{0,0,0,0}};
  4339. int qp_per_uv_dc = p_Vid->qp_per_matrix[ (currMB->qpc[uv] + 3 + p_Vid->bitdepth_chroma_qp_scale) ]; //for YUV422 only
  4340. int qp_rem_uv_dc = p_Vid->qp_rem_matrix[ (currMB->qpc[uv] + 3 + p_Vid->bitdepth_chroma_qp_scale) ]; //for YUV422 only
  4341. if (intra)
  4342. InvLevelScale4x4 = currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv_dc];
  4343. else
  4344. InvLevelScale4x4 = currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv_dc];
  4345. //===================== CHROMA DC YUV422 ======================
  4346. {
  4347. coef_ctr=-1;
  4348. for(k=0;k<9;++k)
  4349. {
  4350. int type = (intra ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
  4351. currMB->is_intra_block = intra;
  4352. currMB->is_v_block = ll;
  4353. dP = &(currSlice->partArr[partMap[type]]);
  4354. rl = readRunLevel_CABAC(currMB, &dP->de_cabac, CHROMA_DC_2x4);
  4355. if (rl.level != 0)
  4356. {
  4357. currMB->cbp_blk[0] |= ((int64)0xff0000) << (ll<<2) ;
  4358. coef_ctr += rl.run + 1;
  4359. assert (coef_ctr < p_Vid->num_cdc_coeff);
  4360. i0=SCAN_YUV422[coef_ctr][0];
  4361. j0=SCAN_YUV422[coef_ctr][1];
  4362. m3[i0][j0]=rl.level;
  4363. }
  4364. else
  4365. break;
  4366. }
  4367. }
  4368. // inverse CHROMA DC YUV422 transform
  4369. // horizontal
  4370. if(currMB->is_lossless == FALSE)
  4371. {
  4372. m4[0][0] = m3[0][0] + m3[1][0];
  4373. m4[0][1] = m3[0][1] + m3[1][1];
  4374. m4[0][2] = m3[0][2] + m3[1][2];
  4375. m4[0][3] = m3[0][3] + m3[1][3];
  4376. m4[1][0] = m3[0][0] - m3[1][0];
  4377. m4[1][1] = m3[0][1] - m3[1][1];
  4378. m4[1][2] = m3[0][2] - m3[1][2];
  4379. m4[1][3] = m3[0][3] - m3[1][3];
  4380. for (i = 0; i < 2; ++i)
  4381. {
  4382. m6[0] = m4[i][0] + m4[i][2];
  4383. m6[1] = m4[i][0] - m4[i][2];
  4384. m6[2] = m4[i][1] - m4[i][3];
  4385. m6[3] = m4[i][1] + m4[i][3];
  4386. imgcof[cof4_pos_to_subblock[0][i]][0][0] = m6[0] + m6[3];
  4387. imgcof[cof4_pos_to_subblock[1][i]][0][0] = m6[1] + m6[2];
  4388. imgcof[cof4_pos_to_subblock[2][i]][0][0] = m6[1] - m6[2];
  4389. imgcof[cof4_pos_to_subblock[3][i]][0][0]= m6[0] - m6[3];
  4390. }//for (i=0;i<2;++i)
  4391. }
  4392. else
  4393. {
  4394. for(j=0;j<4;++j)
  4395. {
  4396. for(i=0;i<2;++i)
  4397. {
  4398. currSlice->cof4[uv + 1][cof4_pos_to_subblock[j][i]][0][0] = m3[i][j];
  4399. }
  4400. }
  4401. }
  4402. for(j = 0;j < p_Vid->mb_cr_size_y; j += BLOCK_SIZE)
  4403. {
  4404. for(i=0;i < p_Vid->mb_cr_size_x;i+=BLOCK_SIZE)
  4405. {
  4406. imgcof[cof4_pos_to_subblock[j>>2][i>>2]][0][0] = rshift_rnd_sf((imgcof[cof4_pos_to_subblock[j>>2][i>>2]][0][0] * InvLevelScale4x4[0][0]) << qp_per_uv_dc, 6);
  4407. }
  4408. }
  4409. }
  4410. }//for (ll=0;ll<3;ll+=2)
  4411. }//else if (dec_picture->chroma_format_idc == YUV422)
  4412. }
  4413. //========================== CHROMA AC ============================
  4414. //-----------------------------------------------------------------
  4415. // chroma AC coeff, all zero fram start_scan
  4416. if (cbp<=31)
  4417. {
  4418. }
  4419. else
  4420. {
  4421. {
  4422. int type;
  4423. currMB->is_intra_block = intra;
  4424. type = (intra ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER);
  4425. dP = &(currSlice->partArr[partMap[type]]);
  4426. if(currMB->is_lossless == FALSE)
  4427. {
  4428. for (b8=0; b8 < p_Vid->num_blk8x8_uv; ++b8)
  4429. {
  4430. currMB->is_v_block = uv = (b8 > ((p_Vid->num_uv_blocks) - 1 ));
  4431. InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  4432. for (b4 = 0; b4 < 4; ++b4)
  4433. {
  4434. int *scale = &InvLevelScale4x4[0][0];
  4435. i = cofuv_blk_x[yuv][b8][b4];
  4436. j = cofuv_blk_y[yuv][b8][b4];
  4437. currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
  4438. currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
  4439. pos_scan_4x4 = &pos_scan4x4_1d[1];
  4440. for(k = 0; k < 16;++k)
  4441. {
  4442. rl = readRunLevel_CABAC(currMB, &(dP->de_cabac), CHROMA_AC);
  4443. if (rl.level != 0)
  4444. {
  4445. byte position;
  4446. currMB->cbp_blk[0] |= ((int64)1) << cbp_blk_chroma[b8][b4];
  4447. pos_scan_4x4 += rl.run;
  4448. position = *pos_scan_4x4++;
  4449. ((int16_t *)currSlice->cof4[uv + 1][cof4_pos_to_subblock[j][i]])[position] = rshift_rnd_sf((rl.level * scale[position])<<qp_per_uv[uv], 4);
  4450. }
  4451. else
  4452. break;
  4453. } //for(k=0;(k<16)&&(level!=0);++k)
  4454. }
  4455. }
  4456. }
  4457. else
  4458. {
  4459. for (b8=0; b8 < p_Vid->num_blk8x8_uv; ++b8)
  4460. {
  4461. currMB->is_v_block = uv = (b8 > ((p_Vid->num_uv_blocks) - 1 ));
  4462. for (b4=0; b4 < 4; ++b4)
  4463. {
  4464. i = cofuv_blk_x[yuv][b8][b4];
  4465. j = cofuv_blk_y[yuv][b8][b4];
  4466. pos_scan_4x4 = &pos_scan4x4_1d[1];
  4467. currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
  4468. currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
  4469. for(k=0;k<16;++k)
  4470. {
  4471. rl = readRunLevel_CABAC(currMB, &dP->de_cabac, CHROMA_AC);
  4472. if (rl.level != 0)
  4473. {
  4474. currMB->cbp_blk[0] |= ((int64)1) << cbp_blk_chroma[b8][b4];
  4475. pos_scan_4x4 += rl.run;
  4476. ((int16_t *)currSlice->cof4[uv + 1][cof4_pos_to_subblock[j][i]])[*pos_scan_4x4++] = rl.level;
  4477. }
  4478. else
  4479. break;
  4480. }
  4481. }
  4482. }
  4483. } //for (b4=0; b4 < 4; b4++)
  4484. } //for (b8=0; b8 < p_Vid->num_blk8x8_uv; b8++)
  4485. } //if (dec_picture->chroma_format_idc != YUV400)
  4486. }
  4487. }
  4488. /*!
  4489. ************************************************************************
  4490. * \brief
  4491. * Get coded block pattern and coefficients (run/level)
  4492. * from the NAL
  4493. ************************************************************************
  4494. */
  4495. static void read_CBP_and_coeffs_from_NAL_CAVLC(Macroblock *currMB)
  4496. {
  4497. int i,j,k;
  4498. int level;
  4499. int mb_nr = currMB->mbAddrX;
  4500. int cbp;
  4501. SyntaxElement currSE;
  4502. DataPartition *dP = NULL;
  4503. Slice *currSlice = currMB->p_Slice;
  4504. const byte *partMap = assignSE2partition[currSlice->dp_mode];
  4505. int coef_ctr, i0, j0, b8;
  4506. int ll;
  4507. __declspec(align(32)) int levarr[16], runarr[16];
  4508. int numcoeff;
  4509. int qp_per, qp_rem;
  4510. VideoParameters *p_Vid = currMB->p_Vid;
  4511. int smb = ((p_Vid->type==SP_SLICE) && IS_INTER (currMB)) || (p_Vid->type == SI_SLICE && currMB->mb_type == SI4MB);
  4512. int uv;
  4513. int qp_per_uv[2];
  4514. int qp_rem_uv[2];
  4515. int intra = IS_INTRA (currMB);
  4516. int temp[4];
  4517. int b4;
  4518. StorablePicture *dec_picture = p_Vid->dec_picture;
  4519. int yuv = dec_picture->chroma_format_idc - 1;
  4520. int m6[4];
  4521. int need_transform_size_flag;
  4522. int (*InvLevelScale4x4)[4] = NULL;
  4523. const int *InvLevelScale8x8 = NULL;
  4524. // select scan type
  4525. const byte (*pos_scan4x4)[2] = ((p_Vid->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
  4526. const byte *pos_scan_4x4 = pos_scan4x4[0];
  4527. // QPI
  4528. //init constants for every chroma qp offset
  4529. if (dec_picture->chroma_format_idc != YUV400)
  4530. {
  4531. for (i=0; i<2; ++i)
  4532. {
  4533. qp_per_uv[i] = p_Vid->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
  4534. qp_rem_uv[i] = p_Vid->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
  4535. }
  4536. }
  4537. // read CBP if not new intra mode
  4538. if (!IS_I16MB (currMB))
  4539. {
  4540. //===== C B P =====
  4541. //---------------------
  4542. int type = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
  4543. ? SE_CBP_INTRA
  4544. : SE_CBP_INTER;
  4545. dP = &(currSlice->partArr[partMap[type]]);
  4546. currSE.mapping = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
  4547. ? currSlice->linfo_cbp_intra
  4548. : currSlice->linfo_cbp_inter;
  4549. TRACE_STRING("coded_block_pattern");
  4550. readSyntaxElement_UVLC(&currSE, dP);
  4551. currMB->cbp = cbp = currSE.value1;
  4552. //============= Transform size flag for INTER MBs =============
  4553. //-------------------------------------------------------------
  4554. need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
  4555. (IS_DIRECT(currMB) && p_Vid->active_sps->direct_8x8_inference_flag) ||
  4556. (currMB->NoMbPartLessThan8x8Flag))
  4557. && currMB->mb_type != I8MB && currMB->mb_type != I4MB
  4558. && (currMB->cbp&15)
  4559. && p_Vid->Transform8x8Mode);
  4560. if (need_transform_size_flag)
  4561. {
  4562. dP = &(currSlice->partArr[partMap[SE_HEADER]]);
  4563. TRACE_STRING("transform_size_8x8_flag");
  4564. // read CAVLC transform_size_8x8_flag
  4565. currMB->luma_transform_size_8x8_flag = (Boolean) readSyntaxElement_FLC(dP->bitstream, 1);
  4566. }
  4567. //===== DQUANT =====
  4568. //----------------------
  4569. // Delta quant only if nonzero coeffs
  4570. if (cbp !=0)
  4571. {
  4572. read_delta_quant_CAVLC(&currSE, dP, currMB, partMap, (IS_INTER (currMB)) ? SE_DELTA_QUANT_INTER : SE_DELTA_QUANT_INTRA);
  4573. if (currSlice->dp_mode)
  4574. {
  4575. if (IS_INTER (currMB) && currSlice->dpC_NotPresent )
  4576. currMB->dpl_flag = 1;
  4577. if( intra && currSlice->dpB_NotPresent )
  4578. {
  4579. currMB->ei_flag = 1;
  4580. currMB->dpl_flag = 1;
  4581. }
  4582. // check for prediction from neighbours
  4583. check_dp_neighbors (currMB);
  4584. if (currMB->dpl_flag)
  4585. {
  4586. cbp = 0;
  4587. currMB->cbp = cbp;
  4588. }
  4589. }
  4590. }
  4591. }
  4592. else
  4593. {
  4594. cbp = currMB->cbp;
  4595. }
  4596. if (IS_I16MB (currMB)) // read DC coeffs for new intra modes
  4597. {
  4598. read_delta_quant_CAVLC(&currSE, dP, currMB, partMap, SE_DELTA_QUANT_INTRA);
  4599. macroblock_set_dc_pred(p_Vid, currMB->block_x, currMB->block_y);
  4600. if (currSlice->dp_mode)
  4601. {
  4602. if (currSlice->dpB_NotPresent)
  4603. {
  4604. currMB->ei_flag = 1;
  4605. currMB->dpl_flag = 1;
  4606. }
  4607. check_dp_neighbors (currMB);
  4608. if (currMB->dpl_flag)
  4609. {
  4610. currMB->cbp = cbp = 0;
  4611. }
  4612. }
  4613. if (!currMB->dpl_flag)
  4614. {
  4615. pos_scan_4x4 = pos_scan4x4[0];
  4616. readCoeff4x4_CAVLC(currMB, LUMA_INTRA16x16DC, 0, 0, levarr, runarr, &numcoeff);
  4617. for(k = 0; k < numcoeff; ++k)
  4618. {
  4619. if (levarr[k] != 0) // leave if level == 0
  4620. {
  4621. pos_scan_4x4 += 2 * runarr[k];
  4622. i0 = (*pos_scan_4x4++);
  4623. j0 = (*pos_scan_4x4++);
  4624. currSlice->cof4[0][cof4_pos_to_subblock[j0][i0]][0][0] = levarr[k];// add new intra DC coeff
  4625. }
  4626. }
  4627. if(currMB->is_lossless == FALSE)
  4628. itrans_2(currMB, (ColorPlane) p_Vid->colour_plane_id);// transform new intra DC
  4629. }
  4630. }
  4631. update_qp(currMB, p_Vid->qp);
  4632. qp_per = p_Vid->qp_per_matrix[ currMB->qp_scaled[p_Vid->colour_plane_id] ];
  4633. qp_rem = p_Vid->qp_rem_matrix[ currMB->qp_scaled[p_Vid->colour_plane_id] ];
  4634. //init quant parameters for chroma
  4635. if (dec_picture->chroma_format_idc != YUV400)
  4636. {
  4637. for(i=0; i < 2; ++i)
  4638. {
  4639. qp_per_uv[i] = p_Vid->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
  4640. qp_rem_uv[i] = p_Vid->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
  4641. }
  4642. }
  4643. InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[p_Vid->colour_plane_id][qp_rem] : currSlice->InvLevelScale4x4_Inter[p_Vid->colour_plane_id][qp_rem];
  4644. InvLevelScale8x8 = intra? currSlice->InvLevelScale8x8_Intra[p_Vid->colour_plane_id][qp_rem] : currSlice->InvLevelScale8x8_Inter[p_Vid->colour_plane_id][qp_rem];
  4645. // luma coefficients
  4646. if (cbp)
  4647. {
  4648. if (!currMB->luma_transform_size_8x8_flag) // 4x4 transform
  4649. {
  4650. readCompCoeff4x4MB_CAVLC(currMB, PLANE_Y, InvLevelScale4x4, qp_per, cbp, p_Vid->nz_coeff[mb_nr][PLANE_Y]);
  4651. }
  4652. else // 8x8 transform
  4653. {
  4654. readCompCoeff8x8MB_CAVLC(currMB, PLANE_Y, InvLevelScale8x8, qp_per, cbp, p_Vid->nz_coeff[mb_nr][PLANE_Y]);
  4655. }
  4656. }
  4657. else
  4658. {
  4659. memset(&p_Vid->nz_coeff[mb_nr][0][0][0], 0, BLOCK_SIZE * BLOCK_SIZE * sizeof(byte));
  4660. }
  4661. if ( p_Vid->active_sps->chroma_format_idc==YUV444 && !IS_INDEPENDENT(p_Vid) )
  4662. {
  4663. for (uv = 0; uv < 2; ++uv )
  4664. {
  4665. /*----------------------16x16DC Luma_Add----------------------*/
  4666. if (IS_I16MB (currMB)) // read DC coeffs for new intra modes
  4667. {
  4668. macroblock_set_dc_pred(p_Vid, currMB->block_x, currMB->block_y);
  4669. if (uv == 0)
  4670. readCoeff4x4_CAVLC(currMB, CB_INTRA16x16DC, 0, 0, levarr, runarr, &numcoeff);
  4671. else
  4672. readCoeff4x4_CAVLC(currMB, CR_INTRA16x16DC, 0, 0, levarr, runarr, &numcoeff);
  4673. coef_ctr=-1;
  4674. level = 1; // just to get inside the loop
  4675. for(k = 0; k < numcoeff; ++k)
  4676. {
  4677. if (levarr[k] != 0) // leave if level == 0
  4678. {
  4679. coef_ctr += runarr[k] + 1;
  4680. i0 = pos_scan4x4[coef_ctr][0];
  4681. j0 = pos_scan4x4[coef_ctr][1];
  4682. currSlice->cof4[uv + 1][cof4_pos_to_subblock[j0][i0]][0][0] = levarr[k];// add new intra DC coeff
  4683. } //if leavarr[k]
  4684. } //k loop
  4685. if(currMB->is_lossless == FALSE)
  4686. {
  4687. itrans_2(currMB, (ColorPlane) (uv + 1)); // transform new intra DC
  4688. }
  4689. } //IS_I16MB
  4690. update_qp(currMB, p_Vid->qp);
  4691. qp_per = p_Vid->qp_per_matrix[ (p_Vid->qp + p_Vid->bitdepth_luma_qp_scale) ];
  4692. qp_rem = p_Vid->qp_rem_matrix[ (p_Vid->qp + p_Vid->bitdepth_luma_qp_scale) ];
  4693. //init constants for every chroma qp offset
  4694. qp_per_uv[uv] = p_Vid->qp_per_matrix[ (currMB->qpc[uv] + p_Vid->bitdepth_chroma_qp_scale) ];
  4695. qp_rem_uv[uv] = p_Vid->qp_rem_matrix[ (currMB->qpc[uv] + p_Vid->bitdepth_chroma_qp_scale) ];
  4696. InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  4697. InvLevelScale8x8 = intra? currSlice->InvLevelScale8x8_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale8x8_Inter[uv + 1][qp_rem_uv[uv]];
  4698. if (!currMB->luma_transform_size_8x8_flag) // 4x4 transform
  4699. {
  4700. readCompCoeff4x4MB_CAVLC(currMB, (ColorPlane) (PLANE_U + uv), InvLevelScale4x4, qp_per_uv[uv], cbp, p_Vid->nz_coeff[mb_nr][PLANE_U + uv]);
  4701. }
  4702. else // 8x8 transform
  4703. {
  4704. readCompCoeff8x8MB_CAVLC(currMB, (ColorPlane) (PLANE_U + uv), InvLevelScale8x8, qp_per_uv[uv], cbp, p_Vid->nz_coeff[mb_nr][PLANE_U + uv]);
  4705. }
  4706. }
  4707. } //444
  4708. else if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
  4709. {
  4710. //========================== CHROMA DC ============================
  4711. //-----------------------------------------------------------------
  4712. // chroma DC coeff
  4713. if(cbp>15)
  4714. {
  4715. if (dec_picture->chroma_format_idc == YUV420)
  4716. {
  4717. for (ll=0;ll<3;ll+=2)
  4718. {
  4719. uv = ll>>1;
  4720. InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  4721. //===================== CHROMA DC YUV420 ======================
  4722. memset(&currSlice->cofu[0], 0, 4 *sizeof(int));
  4723. coef_ctr=-1;
  4724. readCoeff4x4_CAVLC(currMB, CHROMA_DC, 0, 0, levarr, runarr, &numcoeff);
  4725. for(k = 0; k < numcoeff; ++k)
  4726. {
  4727. if (levarr[k] != 0)
  4728. {
  4729. currMB->cbp_blk[0] |= 0xf0000 << (ll<<1) ;
  4730. coef_ctr += runarr[k] + 1;
  4731. currSlice->cofu[coef_ctr]=levarr[k];
  4732. }
  4733. }
  4734. if (smb || (currMB->is_lossless == TRUE)) // check to see if MB type is SPred or SIntra4x4
  4735. {
  4736. currSlice->cof4[uv + 1][0][0][0] = currSlice->cofu[0];
  4737. currSlice->cof4[uv + 1][1][0][0] = currSlice->cofu[1];
  4738. currSlice->cof4[uv + 1][2][0][0] = currSlice->cofu[2];
  4739. currSlice->cof4[uv + 1][3][0][0] = currSlice->cofu[3];
  4740. }
  4741. else
  4742. {
  4743. ihadamard2x2(currSlice->cofu, temp);
  4744. currSlice->cof4[uv + 1][0][0][0] = (((temp[0] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
  4745. currSlice->cof4[uv + 1][1][0][0] = (((temp[1] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
  4746. currSlice->cof4[uv + 1][2][0][0] = (((temp[2] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
  4747. currSlice->cof4[uv + 1][3][0][0] = (((temp[3] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
  4748. }
  4749. }
  4750. }
  4751. else if (dec_picture->chroma_format_idc == YUV422)
  4752. {
  4753. for (ll=0;ll<3;ll+=2)
  4754. {
  4755. int (*InvLevelScale4x4)[4] = NULL;
  4756. uv = ll>>1;
  4757. {
  4758. h264_short_block_t *imgcof = currSlice->cof4[uv + 1];
  4759. int m3[2][4] = {{0,0,0,0},{0,0,0,0}};
  4760. int m4[2][4] = {{0,0,0,0},{0,0,0,0}};
  4761. int qp_per_uv_dc = p_Vid->qp_per_matrix[ (currMB->qpc[uv] + 3 + p_Vid->bitdepth_chroma_qp_scale) ]; //for YUV422 only
  4762. int qp_rem_uv_dc = p_Vid->qp_rem_matrix[ (currMB->qpc[uv] + 3 + p_Vid->bitdepth_chroma_qp_scale) ]; //for YUV422 only
  4763. if (intra)
  4764. InvLevelScale4x4 = currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv_dc];
  4765. else
  4766. InvLevelScale4x4 = currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv_dc];
  4767. //===================== CHROMA DC YUV422 ======================
  4768. readCoeff4x4_CAVLC(currMB, CHROMA_DC, 0, 0, levarr, runarr, &numcoeff);
  4769. coef_ctr=-1;
  4770. level=1;
  4771. for(k = 0; k < numcoeff; ++k)
  4772. {
  4773. if (levarr[k] != 0)
  4774. {
  4775. currMB->cbp_blk[0] |= ((int64)0xff0000) << (ll<<2);
  4776. coef_ctr += runarr[k]+1;
  4777. i0 = SCAN_YUV422[coef_ctr][0];
  4778. j0 = SCAN_YUV422[coef_ctr][1];
  4779. m3[i0][j0]=levarr[k];
  4780. }
  4781. }
  4782. // inverse CHROMA DC YUV422 transform
  4783. // horizontal
  4784. if(currMB->is_lossless == FALSE)
  4785. {
  4786. m4[0][0] = m3[0][0] + m3[1][0];
  4787. m4[0][1] = m3[0][1] + m3[1][1];
  4788. m4[0][2] = m3[0][2] + m3[1][2];
  4789. m4[0][3] = m3[0][3] + m3[1][3];
  4790. m4[1][0] = m3[0][0] - m3[1][0];
  4791. m4[1][1] = m3[0][1] - m3[1][1];
  4792. m4[1][2] = m3[0][2] - m3[1][2];
  4793. m4[1][3] = m3[0][3] - m3[1][3];
  4794. for (i = 0; i < 2; ++i)
  4795. {
  4796. m6[0] = m4[i][0] + m4[i][2];
  4797. m6[1] = m4[i][0] - m4[i][2];
  4798. m6[2] = m4[i][1] - m4[i][3];
  4799. m6[3] = m4[i][1] + m4[i][3];
  4800. imgcof[cof4_pos_to_subblock[0][i]][0][0] = m6[0] + m6[3];
  4801. imgcof[cof4_pos_to_subblock[1][i]][0][0] = m6[1] + m6[2];
  4802. imgcof[cof4_pos_to_subblock[2][i]][0][0] = m6[1] - m6[2];
  4803. imgcof[cof4_pos_to_subblock[3][i]][0][0] = m6[0] - m6[3];
  4804. }//for (i=0;i<2;++i)
  4805. }
  4806. else
  4807. {
  4808. currSlice->cof4[uv + 1][0][0][0] = m3[0][0];
  4809. currSlice->cof4[uv + 1][1][0][0] = m3[1][0];
  4810. currSlice->cof4[uv + 1][2][0][0] = m3[0][1];
  4811. currSlice->cof4[uv + 1][3][0][0] = m3[1][1];
  4812. currSlice->cof4[uv + 1][8][0][0] = m3[0][2];
  4813. currSlice->cof4[uv + 1][9][0][0] = m3[1][2];
  4814. currSlice->cof4[uv + 1][10][0][0] = m3[0][3];
  4815. currSlice->cof4[uv + 1][11][0][0] = m3[1][3];
  4816. }
  4817. for(j = 0;j < 16; j += BLOCK_SIZE)
  4818. {
  4819. for(i=0;i < 8;i+=BLOCK_SIZE)
  4820. {
  4821. imgcof[cof4_pos_to_subblock[j>>2][i>>2]][0][0] = rshift_rnd_sf((imgcof[cof4_pos_to_subblock[j>>2][i>>2]][0][0] * InvLevelScale4x4[0][0]) << qp_per_uv_dc, 6);
  4822. }
  4823. }
  4824. }
  4825. }//for (ll=0;ll<3;ll+=2)
  4826. }//else if (dec_picture->chroma_format_idc == YUV422)
  4827. }
  4828. //========================== CHROMA AC ============================
  4829. //-----------------------------------------------------------------
  4830. // chroma AC coeff, all zero fram start_scan
  4831. if (cbp<=31)
  4832. {
  4833. memset(&p_Vid->nz_coeff [mb_nr ][1][0][0], 0, 2 * BLOCK_SIZE * BLOCK_SIZE * sizeof(byte));
  4834. }
  4835. else
  4836. {
  4837. if(currMB->is_lossless == FALSE)
  4838. {
  4839. for (b8=0; b8 < p_Vid->num_blk8x8_uv; ++b8)
  4840. {
  4841. currMB->is_v_block = uv = (b8 > ((p_Vid->num_uv_blocks) - 1 ));
  4842. InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  4843. for (b4=0; b4 < 4; ++b4)
  4844. {
  4845. i = cofuv_blk_x[yuv][b8][b4];
  4846. j = cofuv_blk_y[yuv][b8][b4];
  4847. readCoeff4x4_CAVLC(currMB, CHROMA_AC, i + 2*uv, j + 4, levarr, runarr, &numcoeff);
  4848. coef_ctr = 0;
  4849. for(k = 0; k < numcoeff;++k)
  4850. {
  4851. if (levarr[k] != 0)
  4852. {
  4853. currMB->cbp_blk[0] |= ((int64)1) << cbp_blk_chroma[b8][b4];
  4854. coef_ctr += runarr[k] + 1;
  4855. i0=pos_scan4x4[coef_ctr][0];
  4856. j0=pos_scan4x4[coef_ctr][1];
  4857. currSlice->cof4[uv + 1][cof4_pos_to_subblock[j][i]][j0][i0] = rshift_rnd_sf((levarr[k] * InvLevelScale4x4[j0][i0])<<qp_per_uv[uv], 4);
  4858. }
  4859. }
  4860. }
  4861. }
  4862. }
  4863. else
  4864. {
  4865. int type;
  4866. currMB->is_intra_block = IS_INTRA(currMB);
  4867. type = (currMB->is_intra_block ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER);
  4868. dP = &(currSlice->partArr[partMap[type]]);
  4869. currSE.mapping = linfo_levrun_inter;
  4870. if(currMB->is_lossless == FALSE)
  4871. {
  4872. for (b8=0; b8 < p_Vid->num_blk8x8_uv; ++b8)
  4873. {
  4874. currMB->is_v_block = uv = (b8 > ((p_Vid->num_uv_blocks) - 1 ));
  4875. InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
  4876. for (b4 = 0; b4 < 4; ++b4)
  4877. {
  4878. i = cofuv_blk_x[yuv][b8][b4];
  4879. j = cofuv_blk_y[yuv][b8][b4];
  4880. currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
  4881. currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
  4882. pos_scan_4x4 = pos_scan4x4[1];
  4883. for(k = 0; k < 16;k++)
  4884. {
  4885. readSyntaxElement_UVLC(&currSE, dP);
  4886. level = currSE.value1;
  4887. if (level != 0)
  4888. {
  4889. currMB->cbp_blk[0] |= ((int64)1) << cbp_blk_chroma[b8][b4];
  4890. pos_scan_4x4 += (currSE.value2 << 1);
  4891. i0 = *pos_scan_4x4++;
  4892. j0 = *pos_scan_4x4++;
  4893. currSlice->cof4[uv + 1][cof4_pos_to_subblock[j][i]][j0][i0] = rshift_rnd_sf((level * InvLevelScale4x4[j0][i0])<<qp_per_uv[uv], 4);
  4894. }
  4895. else
  4896. break;
  4897. } //for(k=0;(k<16)&&(level!=0);++k)
  4898. }
  4899. }
  4900. }
  4901. else
  4902. {
  4903. for (b8=0; b8 < p_Vid->num_blk8x8_uv; ++b8)
  4904. {
  4905. currMB->is_v_block = uv = (b8 > ((p_Vid->num_uv_blocks) - 1 ));
  4906. for (b4=0; b4 < 4; ++b4)
  4907. {
  4908. i = cofuv_blk_x[yuv][b8][b4];
  4909. j = cofuv_blk_y[yuv][b8][b4];
  4910. pos_scan_4x4 = pos_scan4x4[1];
  4911. currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
  4912. currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
  4913. for(k=0;k<16;++k)
  4914. {
  4915. readSyntaxElement_UVLC(&currSE, dP);
  4916. level = currSE.value1;
  4917. if (level != 0)
  4918. {
  4919. currMB->cbp_blk[0] |= ((int64)1) << cbp_blk_chroma[b8][b4];
  4920. pos_scan_4x4 += (currSE.value2 << 1);
  4921. i0 = *pos_scan_4x4++;
  4922. j0 = *pos_scan_4x4++;
  4923. currSlice->cof4[uv + 1][cof4_pos_to_subblock[j][i]][j0][i0] = level;
  4924. }
  4925. else
  4926. break;
  4927. }
  4928. }
  4929. }
  4930. } //for (b4=0; b4 < 4; b4++)
  4931. } //for (b8=0; b8 < p_Vid->num_blk8x8_uv; b8++)
  4932. } //if (dec_picture->chroma_format_idc != YUV400)
  4933. }
  4934. }
  4935. /*!
  4936. ************************************************************************
  4937. * \brief
  4938. * decode one color component in an I slice
  4939. ************************************************************************
  4940. */
  4941. static int decode_one_component_i_slice(Macroblock *currMB, ColorPlane curr_plane, VideoImage *image, StorablePicture *dec_picture)
  4942. {
  4943. //For residual DPCM
  4944. currMB->ipmode_DPCM = NO_INTRA_PMODE;
  4945. if(currMB->mb_type == IPCM)
  4946. mb_pred_ipcm(currMB);
  4947. else if (IS_I16MB (currMB)) // get prediction for INTRA_MB_16x16
  4948. mb_pred_intra16x16(currMB, curr_plane, image, dec_picture);
  4949. else if (currMB->mb_type == I4MB)
  4950. mb_pred_intra4x4(currMB, curr_plane, image, dec_picture);
  4951. else if (currMB->mb_type == I8MB)
  4952. mb_pred_intra8x8(currMB, curr_plane, image, dec_picture);
  4953. return 1;
  4954. }
  4955. /*!
  4956. ************************************************************************
  4957. * \brief
  4958. * decode one color component for a p slice
  4959. ************************************************************************
  4960. */
  4961. static int decode_one_component_p_slice(Macroblock *currMB, ColorPlane curr_plane, VideoImage *image, StorablePicture *dec_picture)
  4962. {
  4963. //For residual DPCM
  4964. currMB->ipmode_DPCM = NO_INTRA_PMODE;
  4965. if(currMB->mb_type == IPCM)
  4966. mb_pred_ipcm(currMB);
  4967. else if (IS_I16MB (currMB)) // get prediction for INTRA_MB_16x16
  4968. mb_pred_intra16x16(currMB, curr_plane, image, dec_picture);
  4969. else if (currMB->mb_type == I4MB)
  4970. mb_pred_intra4x4(currMB, curr_plane, image, dec_picture);
  4971. else if (currMB->mb_type == I8MB)
  4972. mb_pred_intra8x8(currMB, curr_plane, image, dec_picture);
  4973. else if (currMB->mb_type == PSKIP)
  4974. mb_pred_skip(currMB, curr_plane, image, dec_picture);
  4975. else if (currMB->mb_type == P16x16)
  4976. mb_pred_p_inter16x16(currMB, curr_plane, image, dec_picture);
  4977. else if (currMB->mb_type == P16x8)
  4978. mb_pred_p_inter16x8(currMB, curr_plane, image, dec_picture);
  4979. else if (currMB->mb_type == P8x16)
  4980. mb_pred_p_inter8x16(currMB, curr_plane, image, dec_picture);
  4981. else
  4982. mb_pred_p_inter8x8(currMB, curr_plane, image, dec_picture);
  4983. return 1;
  4984. }
  4985. /*!
  4986. ************************************************************************
  4987. * \brief
  4988. * decode one color component for a sp slice
  4989. ************************************************************************
  4990. */
  4991. static int decode_one_component_sp_slice(Macroblock *currMB, ColorPlane curr_plane, VideoImage *image, StorablePicture *dec_picture)
  4992. {
  4993. //For residual DPCM
  4994. currMB->ipmode_DPCM = NO_INTRA_PMODE;
  4995. if(currMB->mb_type == IPCM)
  4996. mb_pred_ipcm(currMB);
  4997. else if (IS_I16MB (currMB)) // get prediction for INTRA_MB_16x16
  4998. mb_pred_intra16x16(currMB, curr_plane, image, dec_picture);
  4999. else if (currMB->mb_type == I4MB)
  5000. mb_pred_intra4x4(currMB, curr_plane, image, dec_picture);
  5001. else if (currMB->mb_type == I8MB)
  5002. mb_pred_intra8x8(currMB, curr_plane, image, dec_picture);
  5003. else if (currMB->mb_type == PSKIP)
  5004. mb_pred_sp_skip(currMB, curr_plane, image, dec_picture);
  5005. else if (currMB->mb_type == P16x16)
  5006. mb_pred_p_inter16x16(currMB, curr_plane, image, dec_picture);
  5007. else if (currMB->mb_type == P16x8)
  5008. mb_pred_p_inter16x8(currMB, curr_plane, image, dec_picture);
  5009. else if (currMB->mb_type == P8x16)
  5010. mb_pred_p_inter8x16(currMB, curr_plane, image, dec_picture);
  5011. else
  5012. mb_pred_p_inter8x8(currMB, curr_plane, image, dec_picture);
  5013. return 1;
  5014. }
  5015. static void set_chroma_vector(Macroblock *currMB, int *list_offset)
  5016. {
  5017. Slice *currSlice = currMB->p_Slice;
  5018. VideoParameters *p_Vid = currMB->p_Vid;
  5019. if (!currSlice->mb_aff_frame_flag)
  5020. {
  5021. if(p_Vid->structure == TOP_FIELD)
  5022. {
  5023. int k,l;
  5024. for (l = LIST_0; l <= (LIST_1); l++)
  5025. {
  5026. for(k = 0; k < p_Vid->listXsize[l]; k++)
  5027. {
  5028. if(p_Vid->structure != p_Vid->listX[l][k]->structure)
  5029. p_Vid->listX[l][k]->chroma_vector_adjustment = -2;
  5030. else
  5031. p_Vid->listX[l][k]->chroma_vector_adjustment= 0;
  5032. }
  5033. }
  5034. }
  5035. else if(p_Vid->structure == BOTTOM_FIELD)
  5036. {
  5037. int k,l;
  5038. for (l = LIST_0; l <= (LIST_1); l++)
  5039. {
  5040. for(k = 0; k < p_Vid->listXsize[l]; k++)
  5041. {
  5042. if (p_Vid->structure != p_Vid->listX[l][k]->structure)
  5043. p_Vid->listX[l][k]->chroma_vector_adjustment = 2;
  5044. else
  5045. p_Vid->listX[l][k]->chroma_vector_adjustment= 0;
  5046. }
  5047. }
  5048. }
  5049. else
  5050. {
  5051. int k,l;
  5052. for (l = LIST_0; l <= (LIST_1); l++)
  5053. {
  5054. for(k = 0; k < p_Vid->listXsize[l]; k++)
  5055. {
  5056. p_Vid->listX[l][k]->chroma_vector_adjustment= 0;
  5057. }
  5058. }
  5059. }
  5060. }
  5061. else
  5062. {
  5063. int mb_nr = (currMB->mbAddrX & 0x01);
  5064. int k,l;
  5065. //////////////////////////
  5066. // find out the correct list offsets
  5067. if (currMB->mb_field)
  5068. {
  5069. *list_offset = mb_nr ? 4 : 2;
  5070. for (l = LIST_0 + *list_offset; l <= (LIST_1 + *list_offset); l++)
  5071. {
  5072. for(k = 0; k < p_Vid->listXsize[l]; k++)
  5073. {
  5074. if(mb_nr == 0 && p_Vid->listX[l][k]->structure == BOTTOM_FIELD)
  5075. p_Vid->listX[l][k]->chroma_vector_adjustment = -2;
  5076. else if(mb_nr == 1 && p_Vid->listX[l][k]->structure == TOP_FIELD)
  5077. p_Vid->listX[l][k]->chroma_vector_adjustment = 2;
  5078. else
  5079. p_Vid->listX[l][k]->chroma_vector_adjustment= 0;
  5080. }
  5081. }
  5082. }
  5083. else
  5084. {
  5085. for (l = LIST_0; l <= (LIST_1); l++)
  5086. {
  5087. for(k = 0; k < p_Vid->listXsize[l]; k++)
  5088. {
  5089. p_Vid->listX[l][k]->chroma_vector_adjustment= 0;
  5090. }
  5091. }
  5092. }
  5093. }
  5094. p_Vid->max_mb_vmv_r = (p_Vid->structure != FRAME || (currSlice->mb_aff_frame_flag && currMB->mb_field)) ? p_Vid->max_vmv_r >> 1 : p_Vid->max_vmv_r;
  5095. }
  5096. static void mb_pred_b_dspatial(Macroblock *currMB, ColorPlane curr_plane, VideoImage *image, StorablePicture *dec_picture)
  5097. {
  5098. char l0_rFrame = -1, l1_rFrame = -1;
  5099. PicMotionParams *motion = &dec_picture->motion;
  5100. MotionVector pmvl0={0,0}, pmvl1={0,0};
  5101. int k;
  5102. int block8x8;
  5103. Slice *currSlice = currMB->p_Slice;
  5104. VideoParameters *p_Vid = currMB->p_Vid;
  5105. int curr_mb_field = ((currSlice->mb_aff_frame_flag)&&(currMB->mb_field));
  5106. MotionParams *colocated = &currSlice->p_colocated->frame;
  5107. int list_offset = 0;
  5108. int pred_dir = 0;
  5109. Boolean has_zero_partitions = FALSE;
  5110. h264_ref_t *ref_pic_num_l0, *ref_pic_num_l1;
  5111. set_chroma_vector(currMB, &list_offset);
  5112. if (currMB->mb_field)
  5113. {
  5114. if(currMB->mbAddrX & 0x01)
  5115. {
  5116. colocated = &currSlice->p_colocated->bottom;
  5117. }
  5118. else
  5119. {
  5120. colocated = &currSlice->p_colocated->top;
  5121. }
  5122. }
  5123. prepare_direct_params(currMB, dec_picture, pmvl0, pmvl1, &l0_rFrame, &l1_rFrame);
  5124. ref_pic_num_l0 = dec_picture->ref_pic_num[p_Vid->current_slice_nr][LIST_0 + list_offset];
  5125. ref_pic_num_l1 = dec_picture->ref_pic_num[p_Vid->current_slice_nr][LIST_1 + list_offset];
  5126. if (p_Vid->active_sps->direct_8x8_inference_flag)
  5127. {
  5128. if (l0_rFrame >=0 && l1_rFrame >=0)
  5129. {
  5130. PicMotion **motion0 = &motion->motion[LIST_0][currMB->block_y];
  5131. PicMotion **motion1 = &motion->motion[LIST_1][currMB->block_y];
  5132. int block_x = currMB->block_x;
  5133. has_zero_partitions = TRUE;
  5134. pred_dir = 2;
  5135. if (p_Vid->listX[LIST_1 + list_offset][0]->is_long_term)
  5136. { // long term
  5137. //---
  5138. memcpy(motion0[0][block_x + 0].mv, pmvl0, sizeof(MotionVector));
  5139. motion0[0][block_x + 0].ref_idx = l0_rFrame;
  5140. memcpy(motion1[0][block_x + 0].mv, pmvl1, sizeof(MotionVector));
  5141. motion1[0][block_x + 0].ref_idx = l1_rFrame;
  5142. motion0[0][block_x + 0].ref_pic_id = ref_pic_num_l0[(short)motion0[0][block_x + 0].ref_idx];
  5143. motion1[0][block_x + 0].ref_pic_id = ref_pic_num_l1[(short)motion1[0][block_x + 0].ref_idx];
  5144. memcpy(motion0[0][block_x + 1].mv, pmvl0, sizeof(MotionVector));
  5145. motion0[0][block_x + 1].ref_idx = l0_rFrame;
  5146. memcpy(motion1[0][block_x + 1].mv, pmvl1, sizeof(MotionVector));
  5147. motion1[0][block_x + 1].ref_idx = l1_rFrame;
  5148. motion0[0][block_x + 1].ref_pic_id = ref_pic_num_l0[(short)motion0[0][block_x + 1].ref_idx];
  5149. motion1[0][block_x + 1].ref_pic_id = ref_pic_num_l1[(short)motion1[0][block_x + 1].ref_idx];
  5150. memcpy(motion0[1][block_x + 0].mv, pmvl0, sizeof(MotionVector));
  5151. motion0[1][block_x + 0].ref_idx = l0_rFrame;
  5152. memcpy(motion1[1][block_x + 0].mv, pmvl1, sizeof(MotionVector));
  5153. motion1[1][block_x + 0].ref_idx = l1_rFrame;
  5154. motion0[1][block_x + 0].ref_pic_id = ref_pic_num_l0[(short)motion0[1][block_x + 0].ref_idx];
  5155. motion1[1][block_x + 0].ref_pic_id = ref_pic_num_l1[(short)motion1[1][block_x + 0].ref_idx];
  5156. memcpy(motion0[1][block_x + 1].mv, pmvl0, sizeof(MotionVector));
  5157. motion0[1][block_x + 1].ref_idx = l0_rFrame;
  5158. memcpy(motion1[1][block_x + 1].mv, pmvl1, sizeof(MotionVector));
  5159. motion1[1][block_x + 1].ref_idx = l1_rFrame;
  5160. motion0[1][block_x + 1].ref_pic_id = ref_pic_num_l0[(short)motion0[1][block_x + 1].ref_idx];
  5161. motion1[1][block_x + 1].ref_pic_id = ref_pic_num_l1[(short)motion1[1][block_x + 1].ref_idx];
  5162. perform_mc8x8(currMB, curr_plane, dec_picture, pred_dir, 0, 0, list_offset, curr_mb_field);
  5163. //---
  5164. memcpy(motion0[0][block_x + 2].mv, pmvl0, sizeof(MotionVector));
  5165. motion0[0][block_x + 2].ref_idx = l0_rFrame;
  5166. memcpy(motion1[0][block_x + 2].mv, pmvl1, sizeof(MotionVector));
  5167. motion1[0][block_x + 2].ref_idx = l1_rFrame;
  5168. motion0[0][block_x + 2].ref_pic_id = ref_pic_num_l0[(short)motion0[0][block_x + 2].ref_idx];
  5169. motion1[0][block_x + 2].ref_pic_id = ref_pic_num_l1[(short)motion1[0][block_x + 2].ref_idx];
  5170. memcpy(motion0[0][block_x + 3].mv, pmvl0, sizeof(MotionVector));
  5171. motion0[0][block_x + 3].ref_idx = l0_rFrame;
  5172. memcpy(motion1[0][block_x + 3].mv, pmvl1, sizeof(MotionVector));
  5173. motion1[0][block_x + 3].ref_idx = l1_rFrame;
  5174. motion0[0][block_x + 3].ref_pic_id = ref_pic_num_l0[(short)motion0[0][block_x + 3].ref_idx];
  5175. motion1[0][block_x + 3].ref_pic_id = ref_pic_num_l1[(short)motion1[0][block_x + 3].ref_idx];
  5176. memcpy(motion0[1][block_x + 2].mv, pmvl0, sizeof(MotionVector));
  5177. motion0[1][block_x + 2].ref_idx = l0_rFrame;
  5178. memcpy(motion1[1][block_x + 2].mv, pmvl1, sizeof(MotionVector));
  5179. motion1[1][block_x + 2].ref_idx = l1_rFrame;
  5180. motion0[1][block_x + 2].ref_pic_id = ref_pic_num_l0[(short)motion0[1][block_x + 2].ref_idx];
  5181. motion1[1][block_x + 2].ref_pic_id = ref_pic_num_l1[(short)motion1[1][block_x + 2].ref_idx];
  5182. memcpy(motion0[1][block_x + 3].mv, pmvl0, sizeof(MotionVector));
  5183. motion0[1][block_x + 3].ref_idx = l0_rFrame;
  5184. memcpy(motion1[1][block_x + 3].mv, pmvl1, sizeof(MotionVector));
  5185. motion1[1][block_x + 3].ref_idx = l1_rFrame;
  5186. motion0[1][block_x + 3].ref_pic_id = ref_pic_num_l0[(short)motion0[1][block_x + 3].ref_idx];
  5187. motion1[1][block_x + 3].ref_pic_id = ref_pic_num_l1[(short)motion1[1][block_x + 3].ref_idx];
  5188. perform_mc8x8(currMB, curr_plane, dec_picture, pred_dir, 2, 0, list_offset, curr_mb_field);
  5189. //---
  5190. memcpy(motion0[2][block_x + 0].mv, pmvl0, sizeof(MotionVector));
  5191. motion0[2][block_x + 0].ref_idx = l0_rFrame;
  5192. memcpy(motion1[2][block_x + 0].mv, pmvl1, sizeof(MotionVector));
  5193. motion1[2][block_x + 0].ref_idx = l1_rFrame;
  5194. motion0[2][block_x + 0].ref_pic_id = ref_pic_num_l0[(short)motion0[2][block_x + 0].ref_idx];
  5195. motion1[2][block_x + 0].ref_pic_id = ref_pic_num_l1[(short)motion1[2][block_x + 0].ref_idx];
  5196. memcpy(motion0[2][block_x + 1].mv, pmvl0, sizeof(MotionVector));
  5197. motion0[2][block_x + 1].ref_idx = l0_rFrame;
  5198. memcpy(motion1[2][block_x + 1].mv, pmvl1, sizeof(MotionVector));
  5199. motion1[2][block_x + 1].ref_idx = l1_rFrame;
  5200. motion0[2][block_x + 1].ref_pic_id = ref_pic_num_l0[(short)motion0[2][block_x + 1].ref_idx];
  5201. motion1[2][block_x + 1].ref_pic_id = ref_pic_num_l1[(short)motion1[2][block_x + 1].ref_idx];
  5202. memcpy(motion0[3][block_x + 0].mv, pmvl0, sizeof(MotionVector));
  5203. motion0[3][block_x + 0].ref_idx = l0_rFrame;
  5204. memcpy(motion1[3][block_x + 0].mv, pmvl1, sizeof(MotionVector));
  5205. motion1[3][block_x + 0].ref_idx = l1_rFrame;
  5206. motion0[3][block_x + 0].ref_pic_id = ref_pic_num_l0[(short)motion0[3][block_x + 0].ref_idx];
  5207. motion1[3][block_x + 0].ref_pic_id = ref_pic_num_l1[(short)motion1[3][block_x + 0].ref_idx];
  5208. memcpy(motion0[3][block_x + 1].mv, pmvl0, sizeof(MotionVector));
  5209. motion0[3][block_x + 1].ref_idx = l0_rFrame;
  5210. memcpy(motion1[3][block_x + 1].mv, pmvl1, sizeof(MotionVector));
  5211. motion1[3][block_x + 1].ref_idx = l1_rFrame;
  5212. motion0[3][block_x + 1].ref_pic_id = ref_pic_num_l0[(short)motion0[3][block_x + 1].ref_idx];
  5213. motion1[3][block_x + 1].ref_pic_id = ref_pic_num_l1[(short)motion1[3][block_x + 1].ref_idx];
  5214. perform_mc8x8(currMB, curr_plane, dec_picture, pred_dir, 0, 2, list_offset, curr_mb_field);
  5215. //---
  5216. memcpy(motion0[2][block_x + 2].mv, pmvl0, sizeof(MotionVector));
  5217. motion0[2][block_x + 2].ref_idx = l0_rFrame;
  5218. memcpy(motion1[2][block_x + 2].mv, pmvl1, sizeof(MotionVector));
  5219. motion1[2][block_x + 2].ref_idx = l1_rFrame;
  5220. motion0[2][block_x + 2].ref_pic_id = ref_pic_num_l0[(short)motion0[2][block_x + 2].ref_idx];
  5221. motion1[2][block_x + 2].ref_pic_id = ref_pic_num_l1[(short)motion1[2][block_x + 2].ref_idx];
  5222. memcpy(motion0[2][block_x + 3].mv, pmvl0, sizeof(MotionVector));
  5223. motion0[2][block_x + 3].ref_idx = l0_rFrame;
  5224. memcpy(motion1[2][block_x + 3].mv, pmvl1, sizeof(MotionVector));
  5225. motion1[2][block_x + 3].ref_idx = l1_rFrame;
  5226. motion0[2][block_x + 3].ref_pic_id = ref_pic_num_l0[(short)motion0[2][block_x + 3].ref_idx];
  5227. motion1[2][block_x + 3].ref_pic_id = ref_pic_num_l1[(short)motion1[2][block_x + 3].ref_idx];
  5228. memcpy(motion0[3][block_x + 2].mv, pmvl0, sizeof(MotionVector));
  5229. motion0[3][block_x + 2].ref_idx = l0_rFrame;
  5230. memcpy(motion1[3][block_x + 2].mv, pmvl1, sizeof(MotionVector));
  5231. motion1[3][block_x + 2].ref_idx = l1_rFrame;
  5232. motion0[3][block_x + 2].ref_pic_id = ref_pic_num_l0[(short)motion0[3][block_x + 2].ref_idx];
  5233. motion1[3][block_x + 2].ref_pic_id = ref_pic_num_l1[(short)motion1[3][block_x + 2].ref_idx];
  5234. memcpy(motion0[3][block_x + 3].mv, pmvl0, sizeof(MotionVector));
  5235. motion0[3][block_x + 3].ref_idx = l0_rFrame;
  5236. memcpy(motion1[3][block_x + 3].mv, pmvl1, sizeof(MotionVector));
  5237. motion1[3][block_x + 3].ref_idx = l1_rFrame;
  5238. motion0[3][block_x + 3].ref_pic_id = ref_pic_num_l0[(short)motion0[3][block_x + 3].ref_idx];
  5239. motion1[3][block_x + 3].ref_pic_id = ref_pic_num_l1[(short)motion1[3][block_x + 3].ref_idx];
  5240. perform_mc8x8(currMB, curr_plane, dec_picture, pred_dir, 2, 2, list_offset, curr_mb_field);
  5241. }
  5242. else
  5243. { // not long term
  5244. const byte **colocated_moving_block = &colocated->moving_block[currMB->block_y_aff];
  5245. for (block8x8 = 0; block8x8 < 4; block8x8++)
  5246. {
  5247. int k_start = (block8x8 << 2);
  5248. for (k = k_start; k < k_start + BLOCK_MULTIPLE; k ++)
  5249. {
  5250. int i = (decode_block_scan[k] & 3);
  5251. int j = ((decode_block_scan[k] >> 2) & 3);
  5252. int i4 = currMB->block_x + i;
  5253. //===== DIRECT PREDICTION =====
  5254. if (!l0_rFrame && !colocated_moving_block[j][i4])
  5255. {
  5256. motion0[j][i4].mv[0] = 0;
  5257. motion0[j][i4].mv[1] = 0;
  5258. motion0[j][i4].ref_idx = 0;
  5259. }
  5260. else
  5261. {
  5262. motion0[j][i4].mv[0] = pmvl0[0];
  5263. motion0[j][i4].mv[1] = pmvl0[1];
  5264. motion0[j][i4].ref_idx = l0_rFrame;
  5265. }
  5266. if (l1_rFrame == 0 && !colocated_moving_block[j][i4])
  5267. {
  5268. motion1[j][i4].mv[0] = 0;
  5269. motion1[j][i4].mv[1] = 0;
  5270. motion1[j][i4].ref_idx = 0;
  5271. }
  5272. else
  5273. {
  5274. motion1[j][i4].mv[0] = pmvl1[0];
  5275. motion1[j][i4].mv[1] = pmvl1[1];
  5276. motion1[j][i4].ref_idx = l1_rFrame;
  5277. }
  5278. motion0[j][i4].ref_pic_id = ref_pic_num_l0[(short)motion0[j][i4].ref_idx];
  5279. motion1[j][i4].ref_pic_id = ref_pic_num_l1[(short)motion1[j][i4].ref_idx];
  5280. }
  5281. perform_mc8x8(currMB, curr_plane, dec_picture, pred_dir, (decode_block_scan[k_start] & 3), ((decode_block_scan[k_start] >> 2) & 3), list_offset, curr_mb_field);
  5282. }
  5283. }
  5284. }
  5285. else
  5286. {
  5287. for (block8x8 = 0; block8x8 < 4; block8x8++)
  5288. {
  5289. int k_start = (block8x8 << 2);
  5290. for (k = k_start; k < k_start + BLOCK_MULTIPLE; k ++)
  5291. {
  5292. int i = (decode_block_scan[k] & 3);
  5293. int j = ((decode_block_scan[k] >> 2) & 3);
  5294. int i4 = currMB->block_x + i;
  5295. int j4 = currMB->block_y + j;
  5296. int j6 = currMB->block_y_aff + j;
  5297. //printf("%d %d\n", i, j);
  5298. //===== DIRECT PREDICTION =====
  5299. if (l0_rFrame >=0)
  5300. {
  5301. if (!l0_rFrame && ((!colocated->moving_block[j6][i4]) && (!p_Vid->listX[LIST_1 + list_offset][0]->is_long_term)))
  5302. {
  5303. has_zero_partitions = TRUE;
  5304. motion->motion[LIST_0][j4][i4].mv[0] = 0;
  5305. motion->motion[LIST_0][j4][i4].mv[1] = 0;
  5306. motion->motion[LIST_0][j4][i4].ref_idx = 0;
  5307. }
  5308. else
  5309. {
  5310. has_zero_partitions = TRUE;
  5311. motion->motion[LIST_0][j4][i4].mv[0] = pmvl0[0];
  5312. motion->motion[LIST_0][j4][i4].mv[1] = pmvl0[1];
  5313. motion->motion[LIST_0][j4][i4].ref_idx = l0_rFrame;
  5314. }
  5315. }
  5316. else
  5317. {
  5318. motion->motion[LIST_0][j4][i4].mv[0] = 0;
  5319. motion->motion[LIST_0][j4][i4].mv[1] = 0;
  5320. motion->motion[LIST_0][j4][i4].ref_idx = -1;
  5321. }
  5322. if (l1_rFrame >=0)
  5323. {
  5324. if (l1_rFrame == 0 && ((!colocated->moving_block[j6][i4]) && (!p_Vid->listX[LIST_1 + list_offset][0]->is_long_term)))
  5325. {
  5326. has_zero_partitions = TRUE;
  5327. motion->motion[LIST_1][j4][i4].mv[0] = 0;
  5328. motion->motion[LIST_1][j4][i4].mv[1] = 0;
  5329. motion->motion[LIST_1][j4][i4].ref_idx = 0;
  5330. }
  5331. else
  5332. {
  5333. has_zero_partitions = TRUE;
  5334. motion->motion[LIST_1][j4][i4].mv[0] = pmvl1[0];
  5335. motion->motion[LIST_1][j4][i4].mv[1] = pmvl1[1];
  5336. motion->motion[LIST_1][j4][i4].ref_idx = l1_rFrame;
  5337. }
  5338. }
  5339. else
  5340. {
  5341. motion->motion[LIST_1][j4][i4].mv[0] = 0;
  5342. motion->motion[LIST_1][j4][i4].mv[1] = 0;
  5343. motion->motion[LIST_1][j4][i4].ref_idx = -1;
  5344. }
  5345. if (l1_rFrame == -1)
  5346. pred_dir = 0;
  5347. else if (l0_rFrame == -1)
  5348. pred_dir = 1;
  5349. else
  5350. pred_dir = 2;
  5351. if (l0_rFrame < 0 && l1_rFrame < 0)
  5352. {
  5353. motion->motion[LIST_0][j4][i4].ref_idx = 0;
  5354. motion->motion[LIST_1][j4][i4].ref_idx = 0;
  5355. pred_dir = 2;
  5356. }
  5357. motion->motion[LIST_0][j4][i4].ref_pic_id = ref_pic_num_l0[(short)motion->motion[LIST_0][j4][i4].ref_idx];
  5358. motion->motion[LIST_1][j4][i4].ref_pic_id = ref_pic_num_l1[(short)motion->motion[LIST_1][j4][i4].ref_idx];
  5359. }
  5360. if (has_zero_partitions == TRUE)
  5361. {
  5362. int i = (decode_block_scan[k_start] & 3);
  5363. int j = ((decode_block_scan[k_start] >> 2) & 3);
  5364. perform_mc8x8(currMB, curr_plane, dec_picture, pred_dir, i, j, list_offset, curr_mb_field);
  5365. }
  5366. }
  5367. }
  5368. }
  5369. else
  5370. {
  5371. for (block8x8 = 0; block8x8 < 4; block8x8++)
  5372. {
  5373. int k_start = (block8x8 << 2);
  5374. int k_end = k_start + BLOCK_MULTIPLE;
  5375. for (k = k_start; k < k_end; k ++)
  5376. {
  5377. int i = (decode_block_scan[k] & 3);
  5378. int j = ((decode_block_scan[k] >> 2) & 3);
  5379. int i4 = currMB->block_x + i;
  5380. int j4 = currMB->block_y + j;
  5381. int j6 = currMB->block_y_aff + j;
  5382. //===== DIRECT PREDICTION =====
  5383. if (l0_rFrame >=0)
  5384. {
  5385. if (!l0_rFrame && ((!colocated->moving_block[j6][i4]) && (!p_Vid->listX[LIST_1 + list_offset][0]->is_long_term)))
  5386. {
  5387. has_zero_partitions = TRUE;
  5388. motion->motion[LIST_0][j4][i4].mv[0] = 0;
  5389. motion->motion[LIST_0][j4][i4].mv[1] = 0;
  5390. motion->motion[LIST_0][j4][i4].ref_idx = 0;
  5391. }
  5392. else
  5393. {
  5394. has_zero_partitions = TRUE;
  5395. motion->motion[LIST_0][j4][i4].mv[0] = pmvl0[0];
  5396. motion->motion[LIST_0][j4][i4].mv[1] = pmvl0[1];
  5397. motion->motion[LIST_0][j4][i4].ref_idx = l0_rFrame;
  5398. }
  5399. }
  5400. else
  5401. {
  5402. motion->motion[LIST_0][j4][i4].mv[0] = 0;
  5403. motion->motion[LIST_0][j4][i4].mv[1] = 0;
  5404. motion->motion[LIST_0][j4][i4].ref_idx = -1;
  5405. }
  5406. if (l1_rFrame >=0)
  5407. {
  5408. if (l1_rFrame == 0 && ((!colocated->moving_block[j6][i4]) && (!p_Vid->listX[LIST_1 + list_offset][0]->is_long_term)))
  5409. {
  5410. has_zero_partitions = TRUE;
  5411. motion->motion[LIST_1][j4][i4].mv[0] = 0;
  5412. motion->motion[LIST_1][j4][i4].mv[1] = 0;
  5413. motion->motion[LIST_1][j4][i4].ref_idx = 0;
  5414. }
  5415. else
  5416. {
  5417. has_zero_partitions = TRUE;
  5418. motion->motion[LIST_1][j4][i4].mv[0] = pmvl1[0];
  5419. motion->motion[LIST_1][j4][i4].mv[1] = pmvl1[1];
  5420. motion->motion[LIST_1][j4][i4].ref_idx = l1_rFrame;
  5421. }
  5422. }
  5423. else
  5424. {
  5425. motion->motion[LIST_1][j4][i4].mv[0] = 0;
  5426. motion->motion[LIST_1][j4][i4].mv[1] = 0;
  5427. motion->motion[LIST_1][j4][i4].ref_idx = -1;
  5428. }
  5429. if (l0_rFrame < 0 && l1_rFrame < 0)
  5430. {
  5431. motion->motion[LIST_0][j4][i4].ref_idx = 0;
  5432. motion->motion[LIST_1][j4][i4].ref_idx = 0;
  5433. }
  5434. if (l1_rFrame == -1)
  5435. {
  5436. if (l0_rFrame == -1)
  5437. pred_dir = 2;
  5438. else
  5439. pred_dir = 0;
  5440. }
  5441. else if (l0_rFrame == -1)
  5442. {
  5443. pred_dir = 1;
  5444. }
  5445. else
  5446. pred_dir = 2;
  5447. motion->motion[LIST_0][j4][i4].ref_pic_id = ref_pic_num_l0[(short)motion->motion[LIST_0][j4][i4].ref_idx];
  5448. motion->motion[LIST_1][j4][i4].ref_pic_id = ref_pic_num_l1[(short)motion->motion[LIST_1][j4][i4].ref_idx];
  5449. }
  5450. if (has_zero_partitions == TRUE)
  5451. {
  5452. for (k = k_start; k < k_end; k ++)
  5453. {
  5454. int i = (decode_block_scan[k] & 3);
  5455. int j = ((decode_block_scan[k] >> 2) & 3);
  5456. perform_mc(currMB, curr_plane, dec_picture, pred_dir, i, j, list_offset, BLOCK_SIZE, BLOCK_SIZE, curr_mb_field);
  5457. }
  5458. }
  5459. }
  5460. }
  5461. if (has_zero_partitions == FALSE)
  5462. {
  5463. perform_mc16x16(currMB, curr_plane, dec_picture, pred_dir, list_offset, curr_mb_field);
  5464. }
  5465. if (currMB->cbp == 0)
  5466. {
  5467. opt_copy_image_data_16x16_stride(image, currMB->pix_x, currMB->pix_y, currSlice->mb_pred[curr_plane]);
  5468. if (dec_picture->chroma_format_idc == YUV420)
  5469. {
  5470. copy_image_data_8x8_stride(dec_picture->imgUV[0], currMB->pix_c_x, currMB->pix_c_y, currSlice->mb_pred[1]);
  5471. copy_image_data_8x8_stride(dec_picture->imgUV[1], currMB->pix_c_x, currMB->pix_c_y, currSlice->mb_pred[2]);
  5472. }
  5473. else if (dec_picture->chroma_format_idc == YUV422)
  5474. {
  5475. copy_image_data_stride(dec_picture->imgUV[0], currMB->pix_c_x, currMB->pix_c_y, currSlice->mb_pred[1], 8, 16);
  5476. copy_image_data_stride(dec_picture->imgUV[1], currMB->pix_c_x, currMB->pix_c_y, currSlice->mb_pred[2], 8, 16);
  5477. }
  5478. }
  5479. else
  5480. iTransform(currMB, curr_plane, 0);
  5481. }
  5482. /*!
  5483. ************************************************************************
  5484. * \brief
  5485. * decode one color component for a b slice
  5486. ************************************************************************
  5487. */
  5488. static int decode_one_component_b_slice(Macroblock *currMB, ColorPlane curr_plane, VideoImage *image, StorablePicture *dec_picture)
  5489. {
  5490. //For residual DPCM
  5491. currMB->ipmode_DPCM = NO_INTRA_PMODE;
  5492. if(currMB->mb_type == IPCM)
  5493. mb_pred_ipcm(currMB);
  5494. else if (IS_I16MB (currMB)) // get prediction for INTRA_MB_16x16
  5495. mb_pred_intra16x16(currMB, curr_plane, image, dec_picture);
  5496. else if (currMB->mb_type == I4MB)
  5497. mb_pred_intra4x4(currMB, curr_plane, image, dec_picture);
  5498. else if (currMB->mb_type == I8MB)
  5499. mb_pred_intra8x8(currMB, curr_plane, image, dec_picture);
  5500. else if (currMB->mb_type == P16x16)
  5501. mb_pred_p_inter16x16(currMB, curr_plane, image, dec_picture);
  5502. else if (currMB->mb_type == P16x8)
  5503. mb_pred_p_inter16x8(currMB, curr_plane, image, dec_picture);
  5504. else if (currMB->mb_type == P8x16)
  5505. mb_pred_p_inter8x16(currMB, curr_plane, image, dec_picture);
  5506. else if (currMB->mb_type == BSKIP_DIRECT)
  5507. {
  5508. if (currMB->p_Slice->direct_spatial_mv_pred_flag == 0)
  5509. mb_pred_b_dtemporal (currMB, curr_plane, image, dec_picture);
  5510. else
  5511. mb_pred_b_dspatial (currMB, curr_plane, image, dec_picture);
  5512. }
  5513. else
  5514. mb_pred_b_inter8x8 (currMB, curr_plane, image, dec_picture);
  5515. return 1;
  5516. }
  5517. /*!
  5518. ************************************************************************
  5519. * \brief
  5520. * decode one macroblock
  5521. ************************************************************************
  5522. */
  5523. int decode_one_macroblock(Macroblock *currMB, StorablePicture *dec_picture)
  5524. {
  5525. Slice *currSlice = currMB->p_Slice;
  5526. VideoParameters *p_Vid = currMB->p_Vid;
  5527. // luma decoding **************************************************
  5528. currSlice->decode_one_component(currMB, PLANE_Y, dec_picture->imgY, dec_picture);
  5529. if ((p_Vid->active_sps->chroma_format_idc==YUV444)&&(!IS_INDEPENDENT(p_Vid)))
  5530. {
  5531. currSlice->decode_one_component(currMB, PLANE_U, dec_picture->imgUV[0], dec_picture);
  5532. currSlice->decode_one_component(currMB, PLANE_V, dec_picture->imgUV[1], dec_picture);
  5533. }
  5534. return 0;
  5535. }
  5536. /*!
  5537. ************************************************************************
  5538. * \brief
  5539. * change target plane
  5540. * for 4:4:4 Independent mode
  5541. ************************************************************************
  5542. */
  5543. void change_plane_JV( VideoParameters *p_Vid, int nplane )
  5544. {
  5545. Slice *currSlice = p_Vid->currentSlice;
  5546. p_Vid->colour_plane_id = nplane;
  5547. p_Vid->mb_data = p_Vid->mb_data_JV[nplane];
  5548. p_Vid->dec_picture = p_Vid->dec_picture_JV[nplane];
  5549. currSlice->p_colocated = currSlice->Co_located_JV[nplane];
  5550. }
  5551. /*!
  5552. ************************************************************************
  5553. * \brief
  5554. * make frame picture from each plane data
  5555. * for 4:4:4 Independent mode
  5556. ************************************************************************
  5557. */
  5558. void make_frame_picture_JV(VideoParameters *p_Vid)
  5559. {
  5560. int uv, line;
  5561. int nsize;
  5562. int nplane;
  5563. p_Vid->dec_picture = p_Vid->dec_picture_JV[0];
  5564. // Copy Storable Params
  5565. for( nplane=0; nplane<MAX_PLANE; nplane++ )
  5566. {
  5567. copy_storable_param_JV( p_Vid, &p_Vid->dec_picture->JVmotion[nplane], &p_Vid->dec_picture_JV[nplane]->motion );
  5568. }
  5569. // This could be done with pointers and seems not necessary
  5570. for( uv=0; uv<2; uv++ )
  5571. {
  5572. for( line=0; line<p_Vid->height; line++ )
  5573. {
  5574. nsize = sizeof(imgpel) * p_Vid->width;
  5575. memcpy( p_Vid->dec_picture->imgUV[uv]->img[line], p_Vid->dec_picture_JV[uv+1]->imgY->img[line], nsize );
  5576. }
  5577. free_storable_picture(p_Vid, p_Vid->dec_picture_JV[uv+1]);
  5578. }
  5579. }