mov.c 351 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315
  1. /*
  2. * MOV demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  5. *
  6. * first version by Francois Revol <revol@free.fr>
  7. * seek function by Gael Chardon <gael.dev@4now.net>
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include "config_components.h"
  26. #include <inttypes.h>
  27. #include <limits.h>
  28. #include <stdint.h>
  29. #include "libavutil/attributes.h"
  30. #include "libavutil/bprint.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/dict_internal.h"
  33. #include "libavutil/internal.h"
  34. #include "libavutil/intreadwrite.h"
  35. #include "libavutil/intfloat.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/avassert.h"
  38. #include "libavutil/avstring.h"
  39. #include "libavutil/dict.h"
  40. #include "libavutil/mem.h"
  41. #include "libavutil/opt.h"
  42. #include "libavutil/aes.h"
  43. #include "libavutil/aes_ctr.h"
  44. #include "libavutil/pixdesc.h"
  45. #include "libavutil/sha.h"
  46. #include "libavutil/spherical.h"
  47. #include "libavutil/stereo3d.h"
  48. #include "libavutil/timecode.h"
  49. #include "libavutil/uuid.h"
  50. #include "libavcodec/ac3tab.h"
  51. #include "libavcodec/flac.h"
  52. #include "libavcodec/hevc.h"
  53. #include "libavcodec/mpegaudiodecheader.h"
  54. #include "libavcodec/mlp_parse.h"
  55. #include "avformat.h"
  56. #include "internal.h"
  57. #include "avio_internal.h"
  58. #include "demux.h"
  59. #include "iamf_parse.h"
  60. #include "iamf_reader.h"
  61. #include "dovi_isom.h"
  62. #include "riff.h"
  63. #include "isom.h"
  64. #include "libavcodec/get_bits.h"
  65. #include "id3v1.h"
  66. #include "mov_chan.h"
  67. #include "replaygain.h"
  68. #if CONFIG_ZLIB
  69. #include <zlib.h>
  70. #endif
  71. #include "qtpalette.h"
  72. /* those functions parse an atom */
  73. /* links atom IDs to parse functions */
  74. typedef struct MOVParseTableEntry {
  75. uint32_t type;
  76. int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
  77. } MOVParseTableEntry;
  78. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
  79. static int mov_read_mfra(MOVContext *c, AVIOContext *f);
  80. static int64_t add_ctts_entry(MOVCtts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
  81. int count, int duration);
  82. static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
  83. unsigned len, const char *key)
  84. {
  85. char buf[16];
  86. short current, total = 0;
  87. avio_rb16(pb); // unknown
  88. current = avio_rb16(pb);
  89. if (len >= 6)
  90. total = avio_rb16(pb);
  91. if (!total)
  92. snprintf(buf, sizeof(buf), "%d", current);
  93. else
  94. snprintf(buf, sizeof(buf), "%d/%d", current, total);
  95. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  96. av_dict_set(&c->fc->metadata, key, buf, 0);
  97. return 0;
  98. }
  99. static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
  100. unsigned len, const char *key)
  101. {
  102. /* bypass padding bytes */
  103. avio_r8(pb);
  104. avio_r8(pb);
  105. avio_r8(pb);
  106. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  107. av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
  108. return 0;
  109. }
  110. static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
  111. unsigned len, const char *key)
  112. {
  113. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  114. av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
  115. return 0;
  116. }
  117. static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
  118. unsigned len, const char *key)
  119. {
  120. short genre;
  121. avio_r8(pb); // unknown
  122. genre = avio_r8(pb);
  123. if (genre < 1 || genre > ID3v1_GENRE_MAX)
  124. return 0;
  125. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  126. av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
  127. return 0;
  128. }
  129. static const uint32_t mac_to_unicode[128] = {
  130. 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
  131. 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
  132. 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
  133. 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
  134. 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
  135. 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
  136. 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
  137. 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
  138. 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
  139. 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
  140. 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
  141. 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
  142. 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
  143. 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
  144. 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
  145. 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
  146. };
  147. static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
  148. char *dst, int dstlen)
  149. {
  150. char *p = dst;
  151. char *end = dst+dstlen-1;
  152. int i;
  153. for (i = 0; i < len; i++) {
  154. uint8_t t, c = avio_r8(pb);
  155. if (p >= end)
  156. continue;
  157. if (c < 0x80)
  158. *p++ = c;
  159. else if (p < end)
  160. PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
  161. }
  162. *p = 0;
  163. return p - dst;
  164. }
  165. static AVStream *get_curr_st(MOVContext *c)
  166. {
  167. AVStream *st = NULL;
  168. if (c->fc->nb_streams < 1)
  169. return NULL;
  170. for (int i = 0; i < c->nb_heif_item; i++) {
  171. HEIFItem *item = &c->heif_item[i];
  172. if (!item->st)
  173. continue;
  174. if (item->st->id != c->cur_item_id)
  175. continue;
  176. st = item->st;
  177. break;
  178. }
  179. if (!st)
  180. st = c->fc->streams[c->fc->nb_streams-1];
  181. return st;
  182. }
  183. static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
  184. {
  185. AVStream *st;
  186. MOVStreamContext *sc;
  187. enum AVCodecID id;
  188. int ret;
  189. switch (type) {
  190. case 0xd: id = AV_CODEC_ID_MJPEG; break;
  191. case 0xe: id = AV_CODEC_ID_PNG; break;
  192. case 0x1b: id = AV_CODEC_ID_BMP; break;
  193. default:
  194. av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
  195. avio_skip(pb, len);
  196. return 0;
  197. }
  198. sc = av_mallocz(sizeof(*sc));
  199. if (!sc)
  200. return AVERROR(ENOMEM);
  201. ret = ff_add_attached_pic(c->fc, NULL, pb, NULL, len);
  202. if (ret < 0) {
  203. av_free(sc);
  204. return ret;
  205. }
  206. st = c->fc->streams[c->fc->nb_streams - 1];
  207. st->priv_data = sc;
  208. sc->id = st->id;
  209. sc->refcount = 1;
  210. if (st->attached_pic.size >= 8 && id != AV_CODEC_ID_BMP) {
  211. if (AV_RB64(st->attached_pic.data) == 0x89504e470d0a1a0a) {
  212. id = AV_CODEC_ID_PNG;
  213. } else {
  214. id = AV_CODEC_ID_MJPEG;
  215. }
  216. }
  217. st->codecpar->codec_id = id;
  218. return 0;
  219. }
  220. // 3GPP TS 26.244
  221. static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
  222. {
  223. char language[4] = { 0 };
  224. char buf[200], place[100];
  225. uint16_t langcode = 0;
  226. double longitude, latitude, altitude;
  227. const char *key = "location";
  228. if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
  229. av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
  230. return AVERROR_INVALIDDATA;
  231. }
  232. avio_skip(pb, 4); // version+flags
  233. langcode = avio_rb16(pb);
  234. ff_mov_lang_to_iso639(langcode, language);
  235. len -= 6;
  236. len -= avio_get_str(pb, len, place, sizeof(place));
  237. if (len < 1) {
  238. av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
  239. return AVERROR_INVALIDDATA;
  240. }
  241. avio_skip(pb, 1); // role
  242. len -= 1;
  243. if (len < 12) {
  244. av_log(c->fc, AV_LOG_ERROR,
  245. "loci too short (%u bytes left, need at least %d)\n", len, 12);
  246. return AVERROR_INVALIDDATA;
  247. }
  248. longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
  249. latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
  250. altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
  251. // Try to output in the same format as the ?xyz field
  252. snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude);
  253. if (altitude)
  254. av_strlcatf(buf, sizeof(buf), "%+f", altitude);
  255. av_strlcatf(buf, sizeof(buf), "/%s", place);
  256. if (*language && strcmp(language, "und")) {
  257. char key2[16];
  258. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  259. av_dict_set(&c->fc->metadata, key2, buf, 0);
  260. }
  261. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  262. return av_dict_set(&c->fc->metadata, key, buf, 0);
  263. }
  264. static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
  265. {
  266. int i, n_hmmt;
  267. if (len < 2)
  268. return 0;
  269. if (c->ignore_chapters)
  270. return 0;
  271. n_hmmt = avio_rb32(pb);
  272. if (n_hmmt > len / 4)
  273. return AVERROR_INVALIDDATA;
  274. for (i = 0; i < n_hmmt && !pb->eof_reached; i++) {
  275. int moment_time = avio_rb32(pb);
  276. avpriv_new_chapter(c->fc, i, av_make_q(1, 1000), moment_time, AV_NOPTS_VALUE, NULL);
  277. }
  278. if (avio_feof(pb))
  279. return AVERROR_INVALIDDATA;
  280. return 0;
  281. }
  282. static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  283. {
  284. char tmp_key[AV_FOURCC_MAX_STRING_SIZE] = {0};
  285. char key2[32], language[4] = {0};
  286. char *str = NULL;
  287. const char *key = NULL;
  288. uint16_t langcode = 0;
  289. uint32_t data_type = 0, str_size, str_size_alloc;
  290. int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
  291. int raw = 0;
  292. int num = 0;
  293. switch (atom.type) {
  294. case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
  295. case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
  296. case MKTAG( 'X','M','P','_'):
  297. if (c->export_xmp) { key = "xmp"; raw = 1; } break;
  298. case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
  299. case MKTAG( 'a','k','I','D'): key = "account_type";
  300. parse = mov_metadata_int8_no_padding; break;
  301. case MKTAG( 'a','p','I','D'): key = "account_id"; break;
  302. case MKTAG( 'c','a','t','g'): key = "category"; break;
  303. case MKTAG( 'c','p','i','l'): key = "compilation";
  304. parse = mov_metadata_int8_no_padding; break;
  305. case MKTAG( 'c','p','r','t'): key = "copyright"; break;
  306. case MKTAG( 'd','e','s','c'): key = "description"; break;
  307. case MKTAG( 'd','i','s','k'): key = "disc";
  308. parse = mov_metadata_track_or_disc_number; break;
  309. case MKTAG( 'e','g','i','d'): key = "episode_uid";
  310. parse = mov_metadata_int8_no_padding; break;
  311. case MKTAG( 'F','I','R','M'): key = "firmware"; raw = 1; break;
  312. case MKTAG( 'g','n','r','e'): key = "genre";
  313. parse = mov_metadata_gnre; break;
  314. case MKTAG( 'h','d','v','d'): key = "hd_video";
  315. parse = mov_metadata_int8_no_padding; break;
  316. case MKTAG( 'H','M','M','T'):
  317. return mov_metadata_hmmt(c, pb, atom.size);
  318. case MKTAG( 'k','e','y','w'): key = "keywords"; break;
  319. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  320. case MKTAG( 'l','o','c','i'):
  321. return mov_metadata_loci(c, pb, atom.size);
  322. case MKTAG( 'm','a','n','u'): key = "make"; break;
  323. case MKTAG( 'm','o','d','l'): key = "model"; break;
  324. case MKTAG( 'p','c','s','t'): key = "podcast";
  325. parse = mov_metadata_int8_no_padding; break;
  326. case MKTAG( 'p','g','a','p'): key = "gapless_playback";
  327. parse = mov_metadata_int8_no_padding; break;
  328. case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
  329. case MKTAG( 'r','t','n','g'): key = "rating";
  330. parse = mov_metadata_int8_no_padding; break;
  331. case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
  332. case MKTAG( 's','o','a','l'): key = "sort_album"; break;
  333. case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
  334. case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
  335. case MKTAG( 's','o','n','m'): key = "sort_name"; break;
  336. case MKTAG( 's','o','s','n'): key = "sort_show"; break;
  337. case MKTAG( 's','t','i','k'): key = "media_type";
  338. parse = mov_metadata_int8_no_padding; break;
  339. case MKTAG( 't','r','k','n'): key = "track";
  340. parse = mov_metadata_track_or_disc_number; break;
  341. case MKTAG( 't','v','e','n'): key = "episode_id"; break;
  342. case MKTAG( 't','v','e','s'): key = "episode_sort";
  343. parse = mov_metadata_int8_bypass_padding; break;
  344. case MKTAG( 't','v','n','n'): key = "network"; break;
  345. case MKTAG( 't','v','s','h'): key = "show"; break;
  346. case MKTAG( 't','v','s','n'): key = "season_number";
  347. parse = mov_metadata_int8_bypass_padding; break;
  348. case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
  349. case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
  350. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  351. case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
  352. case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
  353. case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
  354. case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
  355. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  356. case MKTAG(0xa9,'d','a','y'): key = "date"; break;
  357. case MKTAG(0xa9,'d','i','r'): key = "director"; break;
  358. case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
  359. case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
  360. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  361. case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
  362. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  363. case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
  364. case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
  365. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  366. case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
  367. case MKTAG(0xa9,'m','a','k'): key = "make"; break;
  368. case MKTAG(0xa9,'m','o','d'): key = "model"; break;
  369. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  370. case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
  371. case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
  372. case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
  373. case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
  374. case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
  375. case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
  376. case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
  377. case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
  378. case MKTAG(0xa9,'t','r','k'): key = "track"; break;
  379. case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
  380. case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
  381. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  382. case MKTAG(0xa9,'x','y','z'): key = "location"; break;
  383. }
  384. retry:
  385. if (c->itunes_metadata && atom.size > 8) {
  386. int data_size = avio_rb32(pb);
  387. int tag = avio_rl32(pb);
  388. if (tag == MKTAG('d','a','t','a') && data_size <= atom.size && data_size >= 16) {
  389. data_type = avio_rb32(pb); // type
  390. avio_rb32(pb); // unknown
  391. str_size = data_size - 16;
  392. atom.size -= 16;
  393. if (!key && c->found_hdlr_mdta && c->meta_keys) {
  394. uint32_t index = av_bswap32(atom.type); // BE number has been read as LE
  395. if (index < c->meta_keys_count && index > 0) {
  396. key = c->meta_keys[index];
  397. } else if (atom.type != MKTAG('c', 'o', 'v', 'r')) {
  398. av_log(c->fc, AV_LOG_WARNING,
  399. "The index of 'data' is out of range: %"PRId32" < 1 or >= %d.\n",
  400. index, c->meta_keys_count);
  401. }
  402. }
  403. if (atom.type == MKTAG('c', 'o', 'v', 'r') ||
  404. (key && !strcmp(key, "com.apple.quicktime.artwork"))) {
  405. int ret = mov_read_covr(c, pb, data_type, str_size);
  406. if (ret < 0) {
  407. av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
  408. return ret;
  409. }
  410. atom.size -= str_size;
  411. if (atom.size > 8)
  412. goto retry;
  413. return ret;
  414. }
  415. } else return 0;
  416. } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
  417. str_size = avio_rb16(pb); // string length
  418. if (str_size > atom.size) {
  419. raw = 1;
  420. avio_seek(pb, -2, SEEK_CUR);
  421. av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
  422. goto retry;
  423. }
  424. langcode = avio_rb16(pb);
  425. ff_mov_lang_to_iso639(langcode, language);
  426. atom.size -= 4;
  427. } else
  428. str_size = atom.size;
  429. if (c->export_all && !key) {
  430. key = av_fourcc_make_string(tmp_key, atom.type);
  431. }
  432. if (!key)
  433. return 0;
  434. if (atom.size < 0 || str_size >= INT_MAX/2)
  435. return AVERROR_INVALIDDATA;
  436. // Allocates enough space if data_type is a int32 or float32 number, otherwise
  437. // worst-case requirement for output string in case of utf8 coded input
  438. num = (data_type >= 21 && data_type <= 23);
  439. str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1;
  440. str = av_mallocz(str_size_alloc);
  441. if (!str)
  442. return AVERROR(ENOMEM);
  443. if (parse)
  444. parse(c, pb, str_size, key);
  445. else {
  446. if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
  447. mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
  448. } else if (data_type == 21) { // BE signed integer, variable size
  449. int val = 0;
  450. if (str_size == 1)
  451. val = (int8_t)avio_r8(pb);
  452. else if (str_size == 2)
  453. val = (int16_t)avio_rb16(pb);
  454. else if (str_size == 3)
  455. val = ((int32_t)(avio_rb24(pb)<<8))>>8;
  456. else if (str_size == 4)
  457. val = (int32_t)avio_rb32(pb);
  458. if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) {
  459. av_log(c->fc, AV_LOG_ERROR,
  460. "Failed to store the number (%d) in string.\n", val);
  461. av_free(str);
  462. return AVERROR_INVALIDDATA;
  463. }
  464. } else if (data_type == 22) { // BE unsigned integer, variable size
  465. unsigned int val = 0;
  466. if (str_size == 1)
  467. val = avio_r8(pb);
  468. else if (str_size == 2)
  469. val = avio_rb16(pb);
  470. else if (str_size == 3)
  471. val = avio_rb24(pb);
  472. else if (str_size == 4)
  473. val = avio_rb32(pb);
  474. if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) {
  475. av_log(c->fc, AV_LOG_ERROR,
  476. "Failed to store the number (%u) in string.\n", val);
  477. av_free(str);
  478. return AVERROR_INVALIDDATA;
  479. }
  480. } else if (data_type == 23 && str_size >= 4) { // BE float32
  481. float val = av_int2float(avio_rb32(pb));
  482. if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {
  483. av_log(c->fc, AV_LOG_ERROR,
  484. "Failed to store the float32 number (%f) in string.\n", val);
  485. av_free(str);
  486. return AVERROR_INVALIDDATA;
  487. }
  488. } else if (data_type > 1 && data_type != 4) {
  489. // data_type can be 0 if not set at all above. data_type 1 means
  490. // UTF8 and 4 means "UTF8 sort". For any other type (UTF16 or e.g.
  491. // a picture), don't return it blindly in a string that is supposed
  492. // to be UTF8 text.
  493. av_log(c->fc, AV_LOG_WARNING, "Skipping unhandled metadata %s of type %d\n", key, data_type);
  494. av_free(str);
  495. return 0;
  496. } else {
  497. int ret = ffio_read_size(pb, str, str_size);
  498. if (ret < 0) {
  499. av_free(str);
  500. return ret;
  501. }
  502. str[str_size] = 0;
  503. }
  504. c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  505. av_dict_set(&c->fc->metadata, key, str, 0);
  506. if (*language && strcmp(language, "und")) {
  507. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  508. av_dict_set(&c->fc->metadata, key2, str, 0);
  509. }
  510. if (!strcmp(key, "encoder")) {
  511. int major, minor, micro;
  512. if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
  513. c->handbrake_version = 1000000*major + 1000*minor + micro;
  514. }
  515. }
  516. }
  517. av_freep(&str);
  518. return 0;
  519. }
  520. static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  521. {
  522. int64_t start;
  523. int i, nb_chapters, str_len, version;
  524. char str[256+1];
  525. int ret;
  526. if (c->ignore_chapters)
  527. return 0;
  528. if ((atom.size -= 5) < 0)
  529. return 0;
  530. version = avio_r8(pb);
  531. avio_rb24(pb);
  532. if (version)
  533. avio_rb32(pb); // ???
  534. nb_chapters = avio_r8(pb);
  535. for (i = 0; i < nb_chapters; i++) {
  536. if (atom.size < 9)
  537. return 0;
  538. start = avio_rb64(pb);
  539. str_len = avio_r8(pb);
  540. if ((atom.size -= 9+str_len) < 0)
  541. return 0;
  542. ret = ffio_read_size(pb, str, str_len);
  543. if (ret < 0)
  544. return ret;
  545. str[str_len] = 0;
  546. avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
  547. }
  548. return 0;
  549. }
  550. #define MIN_DATA_ENTRY_BOX_SIZE 12
  551. static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  552. {
  553. AVStream *st;
  554. MOVStreamContext *sc;
  555. int entries, i, j;
  556. if (c->fc->nb_streams < 1)
  557. return 0;
  558. st = c->fc->streams[c->fc->nb_streams-1];
  559. sc = st->priv_data;
  560. avio_rb32(pb); // version + flags
  561. entries = avio_rb32(pb);
  562. if (!entries ||
  563. entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
  564. entries >= UINT_MAX / sizeof(*sc->drefs))
  565. return AVERROR_INVALIDDATA;
  566. for (i = 0; i < sc->drefs_count; i++) {
  567. MOVDref *dref = &sc->drefs[i];
  568. av_freep(&dref->path);
  569. av_freep(&dref->dir);
  570. }
  571. av_free(sc->drefs);
  572. sc->drefs_count = 0;
  573. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  574. if (!sc->drefs)
  575. return AVERROR(ENOMEM);
  576. sc->drefs_count = entries;
  577. for (i = 0; i < entries; i++) {
  578. MOVDref *dref = &sc->drefs[i];
  579. uint32_t size = avio_rb32(pb);
  580. int64_t next = avio_tell(pb);
  581. if (size < 12 || next < 0 || next > INT64_MAX - size)
  582. return AVERROR_INVALIDDATA;
  583. next += size - 4;
  584. dref->type = avio_rl32(pb);
  585. avio_rb32(pb); // version + flags
  586. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  587. /* macintosh alias record */
  588. uint16_t volume_len, len;
  589. int16_t type;
  590. int ret;
  591. avio_skip(pb, 10);
  592. volume_len = avio_r8(pb);
  593. volume_len = FFMIN(volume_len, 27);
  594. ret = ffio_read_size(pb, dref->volume, 27);
  595. if (ret < 0)
  596. return ret;
  597. dref->volume[volume_len] = 0;
  598. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  599. avio_skip(pb, 12);
  600. len = avio_r8(pb);
  601. len = FFMIN(len, 63);
  602. ret = ffio_read_size(pb, dref->filename, 63);
  603. if (ret < 0)
  604. return ret;
  605. dref->filename[len] = 0;
  606. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  607. avio_skip(pb, 16);
  608. /* read next level up_from_alias/down_to_target */
  609. dref->nlvl_from = avio_rb16(pb);
  610. dref->nlvl_to = avio_rb16(pb);
  611. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  612. dref->nlvl_from, dref->nlvl_to);
  613. avio_skip(pb, 16);
  614. for (type = 0; type != -1 && avio_tell(pb) < next; ) {
  615. if (avio_feof(pb))
  616. return AVERROR_EOF;
  617. type = avio_rb16(pb);
  618. len = avio_rb16(pb);
  619. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  620. if (len&1)
  621. len += 1;
  622. if (type == 2) { // absolute path
  623. av_free(dref->path);
  624. dref->path = av_mallocz(len+1);
  625. if (!dref->path)
  626. return AVERROR(ENOMEM);
  627. ret = ffio_read_size(pb, dref->path, len);
  628. if (ret < 0) {
  629. av_freep(&dref->path);
  630. return ret;
  631. }
  632. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  633. len -= volume_len;
  634. memmove(dref->path, dref->path+volume_len, len);
  635. dref->path[len] = 0;
  636. }
  637. // trim string of any ending zeros
  638. for (j = len - 1; j >= 0; j--) {
  639. if (dref->path[j] == 0)
  640. len--;
  641. else
  642. break;
  643. }
  644. for (j = 0; j < len; j++)
  645. if (dref->path[j] == ':' || dref->path[j] == 0)
  646. dref->path[j] = '/';
  647. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  648. } else if (type == 0) { // directory name
  649. av_free(dref->dir);
  650. dref->dir = av_malloc(len+1);
  651. if (!dref->dir)
  652. return AVERROR(ENOMEM);
  653. ret = ffio_read_size(pb, dref->dir, len);
  654. if (ret < 0) {
  655. av_freep(&dref->dir);
  656. return ret;
  657. }
  658. dref->dir[len] = 0;
  659. for (j = 0; j < len; j++)
  660. if (dref->dir[j] == ':')
  661. dref->dir[j] = '/';
  662. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  663. } else
  664. avio_skip(pb, len);
  665. }
  666. } else {
  667. av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x%08"PRIx32" size %"PRIu32"\n",
  668. dref->type, size);
  669. entries--;
  670. i--;
  671. }
  672. avio_seek(pb, next, SEEK_SET);
  673. }
  674. return 0;
  675. }
  676. static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  677. {
  678. AVStream *st;
  679. uint32_t type;
  680. uint32_t ctype;
  681. int64_t title_size;
  682. char *title_str;
  683. int ret;
  684. avio_r8(pb); /* version */
  685. avio_rb24(pb); /* flags */
  686. /* component type */
  687. ctype = avio_rl32(pb);
  688. type = avio_rl32(pb); /* component subtype */
  689. av_log(c->fc, AV_LOG_TRACE, "ctype=%s\n", av_fourcc2str(ctype));
  690. av_log(c->fc, AV_LOG_TRACE, "stype=%s\n", av_fourcc2str(type));
  691. if (c->trak_index < 0) { // meta not inside a trak
  692. if (type == MKTAG('m','d','t','a')) {
  693. c->found_hdlr_mdta = 1;
  694. }
  695. return 0;
  696. }
  697. st = c->fc->streams[c->fc->nb_streams-1];
  698. if (type == MKTAG('v','i','d','e'))
  699. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  700. else if (type == MKTAG('s','o','u','n'))
  701. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  702. else if (type == MKTAG('m','1','a',' '))
  703. st->codecpar->codec_id = AV_CODEC_ID_MP2;
  704. else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
  705. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  706. avio_rb32(pb); /* component manufacture */
  707. avio_rb32(pb); /* component flags */
  708. avio_rb32(pb); /* component flags mask */
  709. title_size = atom.size - 24;
  710. if (title_size > 0) {
  711. if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
  712. return AVERROR_INVALIDDATA;
  713. title_str = av_malloc(title_size + 1); /* Add null terminator */
  714. if (!title_str)
  715. return AVERROR(ENOMEM);
  716. ret = ffio_read_size(pb, title_str, title_size);
  717. if (ret < 0) {
  718. av_freep(&title_str);
  719. return ret;
  720. }
  721. title_str[title_size] = 0;
  722. if (title_str[0]) {
  723. int off = (!c->isom && title_str[0] == title_size - 1);
  724. // flag added so as to not set stream handler name if already set from mdia->hdlr
  725. av_dict_set(&st->metadata, "handler_name", title_str + off, AV_DICT_DONT_OVERWRITE);
  726. }
  727. av_freep(&title_str);
  728. }
  729. return 0;
  730. }
  731. static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  732. {
  733. return ff_mov_read_esds(c->fc, pb);
  734. }
  735. static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  736. {
  737. AVStream *st;
  738. AVPacketSideData *sd;
  739. enum AVAudioServiceType *ast;
  740. int ac3info, acmod, lfeon, bsmod;
  741. uint64_t mask;
  742. if (c->fc->nb_streams < 1)
  743. return 0;
  744. st = c->fc->streams[c->fc->nb_streams-1];
  745. sd = av_packet_side_data_new(&st->codecpar->coded_side_data,
  746. &st->codecpar->nb_coded_side_data,
  747. AV_PKT_DATA_AUDIO_SERVICE_TYPE,
  748. sizeof(*ast), 0);
  749. if (!sd)
  750. return AVERROR(ENOMEM);
  751. ast = (enum AVAudioServiceType*)sd->data;
  752. ac3info = avio_rb24(pb);
  753. bsmod = (ac3info >> 14) & 0x7;
  754. acmod = (ac3info >> 11) & 0x7;
  755. lfeon = (ac3info >> 10) & 0x1;
  756. mask = ff_ac3_channel_layout_tab[acmod];
  757. if (lfeon)
  758. mask |= AV_CH_LOW_FREQUENCY;
  759. av_channel_layout_uninit(&st->codecpar->ch_layout);
  760. av_channel_layout_from_mask(&st->codecpar->ch_layout, mask);
  761. *ast = bsmod;
  762. if (st->codecpar->ch_layout.nb_channels > 1 && bsmod == 0x7)
  763. *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  764. return 0;
  765. }
  766. #if CONFIG_IAMFDEC
  767. static int mov_read_iacb(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  768. {
  769. AVStream *st;
  770. MOVStreamContext *sc;
  771. FFIOContext b;
  772. AVIOContext *descriptor_pb;
  773. AVDictionary *metadata;
  774. IAMFContext *iamf;
  775. int64_t start_time, duration;
  776. unsigned descriptors_size;
  777. int nb_frames, disposition;
  778. int version, ret;
  779. if (atom.size < 5)
  780. return AVERROR_INVALIDDATA;
  781. if (c->fc->nb_streams < 1)
  782. return 0;
  783. version = avio_r8(pb);
  784. if (version != 1) {
  785. av_log(c->fc, AV_LOG_ERROR, "%s configurationVersion %d",
  786. version < 1 ? "invalid" : "unsupported", version);
  787. return AVERROR_INVALIDDATA;
  788. }
  789. descriptors_size = ffio_read_leb(pb);
  790. if (!descriptors_size || descriptors_size > INT_MAX)
  791. return AVERROR_INVALIDDATA;
  792. st = c->fc->streams[c->fc->nb_streams - 1];
  793. sc = st->priv_data;
  794. sc->iamf = av_mallocz(sizeof(*sc->iamf));
  795. if (!sc->iamf)
  796. return AVERROR(ENOMEM);
  797. iamf = &sc->iamf->iamf;
  798. st->codecpar->extradata = av_malloc(descriptors_size);
  799. if (!st->codecpar->extradata)
  800. return AVERROR(ENOMEM);
  801. st->codecpar->extradata_size = descriptors_size;
  802. ret = avio_read(pb, st->codecpar->extradata, descriptors_size);
  803. if (ret != descriptors_size)
  804. return ret < 0 ? ret : AVERROR_INVALIDDATA;
  805. ffio_init_read_context(&b, st->codecpar->extradata, descriptors_size);
  806. descriptor_pb = &b.pub;
  807. ret = ff_iamfdec_read_descriptors(iamf, descriptor_pb, descriptors_size, c->fc);
  808. if (ret < 0)
  809. return ret;
  810. metadata = st->metadata;
  811. st->metadata = NULL;
  812. start_time = st->start_time;
  813. nb_frames = st->nb_frames;
  814. duration = st->duration;
  815. disposition = st->disposition;
  816. for (int i = 0; i < iamf->nb_audio_elements; i++) {
  817. IAMFAudioElement *audio_element = iamf->audio_elements[i];
  818. const AVIAMFAudioElement *element;
  819. AVStreamGroup *stg =
  820. avformat_stream_group_create(c->fc, AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT, NULL);
  821. if (!stg) {
  822. ret = AVERROR(ENOMEM);
  823. goto fail;
  824. }
  825. av_iamf_audio_element_free(&stg->params.iamf_audio_element);
  826. stg->id = audio_element->audio_element_id;
  827. /* Transfer ownership */
  828. element = stg->params.iamf_audio_element = audio_element->element;
  829. audio_element->element = NULL;
  830. for (int j = 0; j < audio_element->nb_substreams; j++) {
  831. IAMFSubStream *substream = &audio_element->substreams[j];
  832. AVStream *stream;
  833. if (!i && !j) {
  834. if (audio_element->layers[0].substream_count != 1)
  835. disposition &= ~AV_DISPOSITION_DEFAULT;
  836. stream = st;
  837. } else
  838. stream = avformat_new_stream(c->fc, NULL);
  839. if (!stream) {
  840. ret = AVERROR(ENOMEM);
  841. goto fail;
  842. }
  843. stream->start_time = start_time;
  844. stream->nb_frames = nb_frames;
  845. stream->duration = duration;
  846. stream->disposition = disposition;
  847. if (stream != st) {
  848. stream->priv_data = sc;
  849. sc->refcount++;
  850. }
  851. if (element->audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE)
  852. stream->disposition |= AV_DISPOSITION_DEPENDENT;
  853. if (i || j) {
  854. stream->disposition |= AV_DISPOSITION_DEPENDENT;
  855. if (audio_element->layers[0].substream_count == 1)
  856. stream->disposition &= ~AV_DISPOSITION_DEFAULT;
  857. }
  858. ret = avcodec_parameters_copy(stream->codecpar, substream->codecpar);
  859. if (ret < 0)
  860. goto fail;
  861. stream->id = substream->audio_substream_id;
  862. avpriv_set_pts_info(st, 64, 1, sc->time_scale);
  863. ret = avformat_stream_group_add_stream(stg, stream);
  864. if (ret < 0)
  865. goto fail;
  866. }
  867. ret = av_dict_copy(&stg->metadata, metadata, 0);
  868. if (ret < 0)
  869. goto fail;
  870. }
  871. for (int i = 0; i < iamf->nb_mix_presentations; i++) {
  872. IAMFMixPresentation *mix_presentation = iamf->mix_presentations[i];
  873. const AVIAMFMixPresentation *mix = mix_presentation->cmix;
  874. AVStreamGroup *stg =
  875. avformat_stream_group_create(c->fc, AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION, NULL);
  876. if (!stg) {
  877. ret = AVERROR(ENOMEM);
  878. goto fail;
  879. }
  880. av_iamf_mix_presentation_free(&stg->params.iamf_mix_presentation);
  881. stg->id = mix_presentation->mix_presentation_id;
  882. /* Transfer ownership */
  883. stg->params.iamf_mix_presentation = mix_presentation->mix;
  884. mix_presentation->mix = NULL;
  885. for (int j = 0; j < mix->nb_submixes; j++) {
  886. const AVIAMFSubmix *submix = mix->submixes[j];
  887. for (int k = 0; k < submix->nb_elements; k++) {
  888. const AVIAMFSubmixElement *submix_element = submix->elements[k];
  889. const AVStreamGroup *audio_element = NULL;
  890. for (int l = 0; l < c->fc->nb_stream_groups; l++)
  891. if (c->fc->stream_groups[l]->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT &&
  892. c->fc->stream_groups[l]->id == submix_element->audio_element_id) {
  893. audio_element = c->fc->stream_groups[l];
  894. break;
  895. }
  896. av_assert0(audio_element);
  897. for (int l = 0; l < audio_element->nb_streams; l++) {
  898. ret = avformat_stream_group_add_stream(stg, audio_element->streams[l]);
  899. if (ret < 0 && ret != AVERROR(EEXIST))
  900. goto fail;
  901. }
  902. }
  903. }
  904. ret = av_dict_copy(&stg->metadata, metadata, 0);
  905. if (ret < 0)
  906. goto fail;
  907. }
  908. ret = 0;
  909. fail:
  910. av_dict_free(&metadata);
  911. return ret;
  912. }
  913. #endif
  914. static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  915. {
  916. AVStream *st;
  917. AVPacketSideData *sd;
  918. enum AVAudioServiceType *ast;
  919. int eac3info, acmod, lfeon, bsmod;
  920. uint64_t mask;
  921. if (c->fc->nb_streams < 1)
  922. return 0;
  923. st = c->fc->streams[c->fc->nb_streams-1];
  924. sd = av_packet_side_data_new(&st->codecpar->coded_side_data,
  925. &st->codecpar->nb_coded_side_data,
  926. AV_PKT_DATA_AUDIO_SERVICE_TYPE,
  927. sizeof(*ast), 0);
  928. if (!sd)
  929. return AVERROR(ENOMEM);
  930. ast = (enum AVAudioServiceType*)sd->data;
  931. /* No need to parse fields for additional independent substreams and its
  932. * associated dependent substreams since libavcodec's E-AC-3 decoder
  933. * does not support them yet. */
  934. avio_rb16(pb); /* data_rate and num_ind_sub */
  935. eac3info = avio_rb24(pb);
  936. bsmod = (eac3info >> 12) & 0x1f;
  937. acmod = (eac3info >> 9) & 0x7;
  938. lfeon = (eac3info >> 8) & 0x1;
  939. mask = ff_ac3_channel_layout_tab[acmod];
  940. if (lfeon)
  941. mask |= AV_CH_LOW_FREQUENCY;
  942. av_channel_layout_uninit(&st->codecpar->ch_layout);
  943. av_channel_layout_from_mask(&st->codecpar->ch_layout, mask);
  944. *ast = bsmod;
  945. if (st->codecpar->ch_layout.nb_channels > 1 && bsmod == 0x7)
  946. *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  947. return 0;
  948. }
  949. static int mov_read_ddts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  950. {
  951. #define DDTS_SIZE 20
  952. uint8_t buf[DDTS_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  953. AVStream *st = NULL;
  954. uint32_t frame_duration_code = 0;
  955. uint32_t channel_layout_code = 0;
  956. GetBitContext gb;
  957. int ret;
  958. if ((ret = ffio_read_size(pb, buf, DDTS_SIZE)) < 0)
  959. return ret;
  960. init_get_bits(&gb, buf, 8 * DDTS_SIZE);
  961. if (c->fc->nb_streams < 1) {
  962. return 0;
  963. }
  964. st = c->fc->streams[c->fc->nb_streams-1];
  965. st->codecpar->sample_rate = get_bits_long(&gb, 32);
  966. if (st->codecpar->sample_rate <= 0) {
  967. av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
  968. return AVERROR_INVALIDDATA;
  969. }
  970. skip_bits_long(&gb, 32); /* max bitrate */
  971. st->codecpar->bit_rate = get_bits_long(&gb, 32);
  972. st->codecpar->bits_per_coded_sample = get_bits(&gb, 8);
  973. frame_duration_code = get_bits(&gb, 2);
  974. skip_bits(&gb, 30); /* various fields */
  975. channel_layout_code = get_bits(&gb, 16);
  976. st->codecpar->frame_size =
  977. (frame_duration_code == 0) ? 512 :
  978. (frame_duration_code == 1) ? 1024 :
  979. (frame_duration_code == 2) ? 2048 :
  980. (frame_duration_code == 3) ? 4096 : 0;
  981. if (channel_layout_code > 0xff) {
  982. av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout\n");
  983. }
  984. av_channel_layout_uninit(&st->codecpar->ch_layout);
  985. av_channel_layout_from_mask(&st->codecpar->ch_layout,
  986. ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) |
  987. ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) |
  988. ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) |
  989. ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) |
  990. ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) |
  991. ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0));
  992. return 0;
  993. }
  994. static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  995. {
  996. AVStream *st;
  997. if (c->fc->nb_streams < 1)
  998. return 0;
  999. st = c->fc->streams[c->fc->nb_streams-1];
  1000. if (atom.size < 16)
  1001. return 0;
  1002. /* skip version and flags */
  1003. avio_skip(pb, 4);
  1004. ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
  1005. return 0;
  1006. }
  1007. static int mov_read_chnl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1008. {
  1009. int64_t end = av_sat_add64(avio_tell(pb), atom.size);
  1010. int version, flags;
  1011. int ret;
  1012. AVStream *st;
  1013. if (c->fc->nb_streams < 1)
  1014. return 0;
  1015. st = c->fc->streams[c->fc->nb_streams-1];
  1016. version = avio_r8(pb);
  1017. flags = avio_rb24(pb);
  1018. if (version != 0 || flags != 0) {
  1019. av_log(c->fc, AV_LOG_ERROR,
  1020. "Unsupported 'chnl' box with version %d, flags: %#x",
  1021. version, flags);
  1022. return AVERROR_INVALIDDATA;
  1023. }
  1024. ret = ff_mov_read_chnl(c->fc, pb, st);
  1025. if (ret < 0)
  1026. return ret;
  1027. if (avio_tell(pb) != end) {
  1028. av_log(c->fc, AV_LOG_WARNING, "skip %" PRId64 " bytes of unknown data inside chnl\n",
  1029. end - avio_tell(pb));
  1030. avio_seek(pb, end, SEEK_SET);
  1031. }
  1032. return ret;
  1033. }
  1034. static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1035. {
  1036. AVStream *st;
  1037. int ret;
  1038. if (c->fc->nb_streams < 1)
  1039. return 0;
  1040. st = c->fc->streams[c->fc->nb_streams-1];
  1041. if ((ret = ff_get_wav_header(c->fc, pb, st->codecpar, atom.size, 0)) < 0)
  1042. av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
  1043. return ret;
  1044. }
  1045. /* This atom overrides any previously set aspect ratio */
  1046. static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1047. {
  1048. const int num = avio_rb32(pb);
  1049. const int den = avio_rb32(pb);
  1050. AVStream *st;
  1051. if (c->fc->nb_streams < 1)
  1052. return 0;
  1053. st = c->fc->streams[c->fc->nb_streams-1];
  1054. if (den != 0) {
  1055. av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
  1056. num, den, 32767);
  1057. }
  1058. return 0;
  1059. }
  1060. /* this atom contains actual media data */
  1061. static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1062. {
  1063. if (atom.size == 0) /* wrong one (MP4) */
  1064. return 0;
  1065. c->found_mdat=1;
  1066. return 0; /* now go for moov */
  1067. }
  1068. #define DRM_BLOB_SIZE 56
  1069. static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1070. {
  1071. uint8_t intermediate_key[20];
  1072. uint8_t intermediate_iv[20];
  1073. uint8_t input[64];
  1074. uint8_t output[64];
  1075. uint8_t file_checksum[20];
  1076. uint8_t calculated_checksum[20];
  1077. char checksum_string[2 * sizeof(file_checksum) + 1];
  1078. struct AVSHA *sha;
  1079. int i;
  1080. int ret = 0;
  1081. uint8_t *activation_bytes = c->activation_bytes;
  1082. uint8_t *fixed_key = c->audible_fixed_key;
  1083. c->aax_mode = 1;
  1084. sha = av_sha_alloc();
  1085. if (!sha)
  1086. return AVERROR(ENOMEM);
  1087. av_free(c->aes_decrypt);
  1088. c->aes_decrypt = av_aes_alloc();
  1089. if (!c->aes_decrypt) {
  1090. ret = AVERROR(ENOMEM);
  1091. goto fail;
  1092. }
  1093. /* drm blob processing */
  1094. avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
  1095. avio_read(pb, input, DRM_BLOB_SIZE);
  1096. avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
  1097. avio_read(pb, file_checksum, 20);
  1098. // required by external tools
  1099. ff_data_to_hex(checksum_string, file_checksum, sizeof(file_checksum), 1);
  1100. av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == %s\n", checksum_string);
  1101. /* verify activation data */
  1102. if (!activation_bytes) {
  1103. av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
  1104. ret = 0; /* allow ffprobe to continue working on .aax files */
  1105. goto fail;
  1106. }
  1107. if (c->activation_bytes_size != 4) {
  1108. av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
  1109. ret = AVERROR(EINVAL);
  1110. goto fail;
  1111. }
  1112. /* verify fixed key */
  1113. if (c->audible_fixed_key_size != 16) {
  1114. av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
  1115. ret = AVERROR(EINVAL);
  1116. goto fail;
  1117. }
  1118. /* AAX (and AAX+) key derivation */
  1119. av_sha_init(sha, 160);
  1120. av_sha_update(sha, fixed_key, 16);
  1121. av_sha_update(sha, activation_bytes, 4);
  1122. av_sha_final(sha, intermediate_key);
  1123. av_sha_init(sha, 160);
  1124. av_sha_update(sha, fixed_key, 16);
  1125. av_sha_update(sha, intermediate_key, 20);
  1126. av_sha_update(sha, activation_bytes, 4);
  1127. av_sha_final(sha, intermediate_iv);
  1128. av_sha_init(sha, 160);
  1129. av_sha_update(sha, intermediate_key, 16);
  1130. av_sha_update(sha, intermediate_iv, 16);
  1131. av_sha_final(sha, calculated_checksum);
  1132. if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
  1133. av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
  1134. ret = AVERROR_INVALIDDATA;
  1135. goto fail;
  1136. }
  1137. av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
  1138. av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
  1139. for (i = 0; i < 4; i++) {
  1140. // file data (in output) is stored in big-endian mode
  1141. if (activation_bytes[i] != output[3 - i]) { // critical error
  1142. av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
  1143. ret = AVERROR_INVALIDDATA;
  1144. goto fail;
  1145. }
  1146. }
  1147. memcpy(c->file_key, output + 8, 16);
  1148. memcpy(input, output + 26, 16);
  1149. av_sha_init(sha, 160);
  1150. av_sha_update(sha, input, 16);
  1151. av_sha_update(sha, c->file_key, 16);
  1152. av_sha_update(sha, fixed_key, 16);
  1153. av_sha_final(sha, c->file_iv);
  1154. fail:
  1155. av_free(sha);
  1156. return ret;
  1157. }
  1158. static int mov_aaxc_crypto(MOVContext *c)
  1159. {
  1160. if (c->audible_key_size != 16) {
  1161. av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_key value needs to be 16 bytes!\n");
  1162. return AVERROR(EINVAL);
  1163. }
  1164. if (c->audible_iv_size != 16) {
  1165. av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_iv value needs to be 16 bytes!\n");
  1166. return AVERROR(EINVAL);
  1167. }
  1168. c->aes_decrypt = av_aes_alloc();
  1169. if (!c->aes_decrypt) {
  1170. return AVERROR(ENOMEM);
  1171. }
  1172. memcpy(c->file_key, c->audible_key, 16);
  1173. memcpy(c->file_iv, c->audible_iv, 16);
  1174. c->aax_mode = 1;
  1175. return 0;
  1176. }
  1177. // Audible AAX (and AAX+) bytestream decryption
  1178. static int aax_filter(uint8_t *input, int size, MOVContext *c)
  1179. {
  1180. int blocks = 0;
  1181. unsigned char iv[16];
  1182. memcpy(iv, c->file_iv, 16); // iv is overwritten
  1183. blocks = size >> 4; // trailing bytes are not encrypted!
  1184. av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
  1185. av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
  1186. return 0;
  1187. }
  1188. /* read major brand, minor version and compatible brands and store them as metadata */
  1189. static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1190. {
  1191. uint32_t minor_ver;
  1192. int comp_brand_size;
  1193. char* comp_brands_str;
  1194. uint8_t type[5] = {0};
  1195. int ret = ffio_read_size(pb, type, 4);
  1196. if (ret < 0)
  1197. return ret;
  1198. if (c->fc->nb_streams) {
  1199. if (c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT)
  1200. return AVERROR_INVALIDDATA;
  1201. av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate FTYP\n");
  1202. return 0;
  1203. }
  1204. if (strcmp(type, "qt "))
  1205. c->isom = 1;
  1206. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  1207. av_dict_set(&c->fc->metadata, "major_brand", type, 0);
  1208. minor_ver = avio_rb32(pb); /* minor version */
  1209. av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
  1210. comp_brand_size = atom.size - 8;
  1211. if (comp_brand_size < 0 || comp_brand_size == INT_MAX)
  1212. return AVERROR_INVALIDDATA;
  1213. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  1214. if (!comp_brands_str)
  1215. return AVERROR(ENOMEM);
  1216. ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
  1217. if (ret < 0) {
  1218. av_freep(&comp_brands_str);
  1219. return ret;
  1220. }
  1221. comp_brands_str[comp_brand_size] = 0;
  1222. av_dict_set(&c->fc->metadata, "compatible_brands",
  1223. comp_brands_str, AV_DICT_DONT_STRDUP_VAL);
  1224. // Logic for handling Audible's .aaxc files
  1225. if (!strcmp(type, "aaxc")) {
  1226. mov_aaxc_crypto(c);
  1227. }
  1228. return 0;
  1229. }
  1230. /* this atom should contain all header atoms */
  1231. static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1232. {
  1233. int ret;
  1234. if (c->found_moov) {
  1235. av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
  1236. avio_skip(pb, atom.size);
  1237. return 0;
  1238. }
  1239. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1240. return ret;
  1241. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  1242. /* so we don't parse the whole file if over a network */
  1243. c->found_moov=1;
  1244. return 0; /* now go for mdat */
  1245. }
  1246. static MOVFragmentStreamInfo * get_frag_stream_info(
  1247. MOVFragmentIndex *frag_index,
  1248. int index,
  1249. int id)
  1250. {
  1251. int i;
  1252. MOVFragmentIndexItem * item;
  1253. if (index < 0 || index >= frag_index->nb_items)
  1254. return NULL;
  1255. item = &frag_index->item[index];
  1256. for (i = 0; i < item->nb_stream_info; i++)
  1257. if (item->stream_info[i].id == id)
  1258. return &item->stream_info[i];
  1259. // This shouldn't happen
  1260. return NULL;
  1261. }
  1262. static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
  1263. {
  1264. int i;
  1265. MOVFragmentIndexItem * item;
  1266. if (frag_index->current < 0 ||
  1267. frag_index->current >= frag_index->nb_items)
  1268. return;
  1269. item = &frag_index->item[frag_index->current];
  1270. for (i = 0; i < item->nb_stream_info; i++)
  1271. if (item->stream_info[i].id == id) {
  1272. item->current = i;
  1273. return;
  1274. }
  1275. // id not found. This shouldn't happen.
  1276. item->current = -1;
  1277. }
  1278. static MOVFragmentStreamInfo * get_current_frag_stream_info(
  1279. MOVFragmentIndex *frag_index)
  1280. {
  1281. MOVFragmentIndexItem *item;
  1282. if (frag_index->current < 0 ||
  1283. frag_index->current >= frag_index->nb_items)
  1284. return NULL;
  1285. item = &frag_index->item[frag_index->current];
  1286. if (item->current >= 0 && item->current < item->nb_stream_info)
  1287. return &item->stream_info[item->current];
  1288. // This shouldn't happen
  1289. return NULL;
  1290. }
  1291. static int search_frag_moof_offset(MOVFragmentIndex *frag_index, int64_t offset)
  1292. {
  1293. int a, b, m;
  1294. int64_t moof_offset;
  1295. // Optimize for appending new entries
  1296. if (!frag_index->nb_items ||
  1297. frag_index->item[frag_index->nb_items - 1].moof_offset < offset)
  1298. return frag_index->nb_items;
  1299. a = -1;
  1300. b = frag_index->nb_items;
  1301. while (b - a > 1) {
  1302. m = (a + b) >> 1;
  1303. moof_offset = frag_index->item[m].moof_offset;
  1304. if (moof_offset >= offset)
  1305. b = m;
  1306. if (moof_offset <= offset)
  1307. a = m;
  1308. }
  1309. return b;
  1310. }
  1311. static int64_t get_stream_info_time(MOVFragmentStreamInfo * frag_stream_info)
  1312. {
  1313. av_assert0(frag_stream_info);
  1314. if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
  1315. return frag_stream_info->sidx_pts;
  1316. if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
  1317. return frag_stream_info->first_tfra_pts;
  1318. return frag_stream_info->tfdt_dts;
  1319. }
  1320. static int64_t get_frag_time(AVFormatContext *s, AVStream *dst_st,
  1321. MOVFragmentIndex *frag_index, int index)
  1322. {
  1323. MOVFragmentStreamInfo * frag_stream_info;
  1324. MOVStreamContext *sc = dst_st->priv_data;
  1325. int64_t timestamp;
  1326. int i, j;
  1327. // If the stream is referenced by any sidx, limit the search
  1328. // to fragments that referenced this stream in the sidx
  1329. if (sc->has_sidx) {
  1330. frag_stream_info = get_frag_stream_info(frag_index, index, sc->id);
  1331. if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
  1332. return frag_stream_info->sidx_pts;
  1333. if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
  1334. return frag_stream_info->first_tfra_pts;
  1335. return frag_stream_info->sidx_pts;
  1336. }
  1337. for (i = 0; i < frag_index->item[index].nb_stream_info; i++) {
  1338. AVStream *frag_stream = NULL;
  1339. frag_stream_info = &frag_index->item[index].stream_info[i];
  1340. for (j = 0; j < s->nb_streams; j++) {
  1341. MOVStreamContext *sc2 = s->streams[j]->priv_data;
  1342. if (sc2->id == frag_stream_info->id)
  1343. frag_stream = s->streams[j];
  1344. }
  1345. if (!frag_stream) {
  1346. av_log(s, AV_LOG_WARNING, "No stream matching sidx ID found.\n");
  1347. continue;
  1348. }
  1349. timestamp = get_stream_info_time(frag_stream_info);
  1350. if (timestamp != AV_NOPTS_VALUE)
  1351. return av_rescale_q(timestamp, frag_stream->time_base, dst_st->time_base);
  1352. }
  1353. return AV_NOPTS_VALUE;
  1354. }
  1355. static int search_frag_timestamp(AVFormatContext *s, MOVFragmentIndex *frag_index,
  1356. AVStream *st, int64_t timestamp)
  1357. {
  1358. int a, b, m, m0;
  1359. int64_t frag_time;
  1360. a = -1;
  1361. b = frag_index->nb_items;
  1362. while (b - a > 1) {
  1363. m0 = m = (a + b) >> 1;
  1364. while (m < b &&
  1365. (frag_time = get_frag_time(s, st, frag_index, m)) == AV_NOPTS_VALUE)
  1366. m++;
  1367. if (m < b && frag_time <= timestamp)
  1368. a = m;
  1369. else
  1370. b = m0;
  1371. }
  1372. return a;
  1373. }
  1374. static int update_frag_index(MOVContext *c, int64_t offset)
  1375. {
  1376. int index, i;
  1377. MOVFragmentIndexItem * item;
  1378. MOVFragmentStreamInfo * frag_stream_info;
  1379. // If moof_offset already exists in frag_index, return index to it
  1380. index = search_frag_moof_offset(&c->frag_index, offset);
  1381. if (index < c->frag_index.nb_items &&
  1382. c->frag_index.item[index].moof_offset == offset)
  1383. return index;
  1384. // offset is not yet in frag index.
  1385. // Insert new item at index (sorted by moof offset)
  1386. item = av_fast_realloc(c->frag_index.item,
  1387. &c->frag_index.allocated_size,
  1388. (c->frag_index.nb_items + 1) *
  1389. sizeof(*c->frag_index.item));
  1390. if (!item)
  1391. return -1;
  1392. c->frag_index.item = item;
  1393. frag_stream_info = av_realloc_array(NULL, c->fc->nb_streams,
  1394. sizeof(*item->stream_info));
  1395. if (!frag_stream_info)
  1396. return -1;
  1397. for (i = 0; i < c->fc->nb_streams; i++) {
  1398. // Avoid building frag index if streams lack track id.
  1399. MOVStreamContext *sc = c->fc->streams[i]->priv_data;
  1400. if (sc->id < 0) {
  1401. av_free(frag_stream_info);
  1402. return AVERROR_INVALIDDATA;
  1403. }
  1404. frag_stream_info[i].id = sc->id;
  1405. frag_stream_info[i].sidx_pts = AV_NOPTS_VALUE;
  1406. frag_stream_info[i].tfdt_dts = AV_NOPTS_VALUE;
  1407. frag_stream_info[i].next_trun_dts = AV_NOPTS_VALUE;
  1408. frag_stream_info[i].first_tfra_pts = AV_NOPTS_VALUE;
  1409. frag_stream_info[i].index_base = -1;
  1410. frag_stream_info[i].index_entry = -1;
  1411. frag_stream_info[i].encryption_index = NULL;
  1412. frag_stream_info[i].stsd_id = -1;
  1413. }
  1414. if (index < c->frag_index.nb_items)
  1415. memmove(c->frag_index.item + index + 1, c->frag_index.item + index,
  1416. (c->frag_index.nb_items - index) * sizeof(*c->frag_index.item));
  1417. item = &c->frag_index.item[index];
  1418. item->headers_read = 0;
  1419. item->current = 0;
  1420. item->nb_stream_info = c->fc->nb_streams;
  1421. item->moof_offset = offset;
  1422. item->stream_info = frag_stream_info;
  1423. c->frag_index.nb_items++;
  1424. return index;
  1425. }
  1426. static void fix_frag_index_entries(MOVFragmentIndex *frag_index, int index,
  1427. int id, int entries)
  1428. {
  1429. int i;
  1430. MOVFragmentStreamInfo * frag_stream_info;
  1431. if (index < 0)
  1432. return;
  1433. for (i = index; i < frag_index->nb_items; i++) {
  1434. frag_stream_info = get_frag_stream_info(frag_index, i, id);
  1435. if (frag_stream_info && frag_stream_info->index_entry >= 0)
  1436. frag_stream_info->index_entry += entries;
  1437. }
  1438. }
  1439. static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1440. {
  1441. // Set by mov_read_tfhd(). mov_read_trun() will reject files missing tfhd.
  1442. c->fragment.found_tfhd = 0;
  1443. if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
  1444. c->has_looked_for_mfra = 1;
  1445. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  1446. int ret;
  1447. av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
  1448. "for a mfra\n");
  1449. if ((ret = mov_read_mfra(c, pb)) < 0) {
  1450. av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
  1451. "read the mfra (may be a live ismv)\n");
  1452. }
  1453. } else {
  1454. av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
  1455. "seekable, can not look for mfra\n");
  1456. }
  1457. }
  1458. c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
  1459. av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
  1460. c->frag_index.current = update_frag_index(c, c->fragment.moof_offset);
  1461. return mov_read_default(c, pb, atom);
  1462. }
  1463. static void mov_metadata_creation_time(MOVContext *c, AVIOContext *pb, AVDictionary **metadata, int version)
  1464. {
  1465. int64_t time;
  1466. if (version == 1) {
  1467. time = avio_rb64(pb);
  1468. avio_rb64(pb);
  1469. if (time < 0) {
  1470. av_log(c->fc, AV_LOG_DEBUG, "creation_time is negative\n");
  1471. return;
  1472. }
  1473. } else {
  1474. time = avio_rb32(pb);
  1475. avio_rb32(pb); /* modification time */
  1476. if (time > 0 && time < 2082844800) {
  1477. av_log(c->fc, AV_LOG_WARNING, "Detected creation time before 1970, parsing as unix timestamp.\n");
  1478. time += 2082844800;
  1479. }
  1480. }
  1481. if (time) {
  1482. time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
  1483. if ((int64_t)(time * 1000000ULL) / 1000000 != time) {
  1484. av_log(c->fc, AV_LOG_DEBUG, "creation_time is not representable\n");
  1485. return;
  1486. }
  1487. avpriv_dict_set_timestamp(metadata, "creation_time", time * 1000000);
  1488. }
  1489. }
  1490. static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1491. {
  1492. AVStream *st;
  1493. MOVStreamContext *sc;
  1494. int version;
  1495. char language[4] = {0};
  1496. unsigned lang;
  1497. if (c->fc->nb_streams < 1)
  1498. return 0;
  1499. st = c->fc->streams[c->fc->nb_streams-1];
  1500. sc = st->priv_data;
  1501. if (sc->time_scale) {
  1502. av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
  1503. return AVERROR_INVALIDDATA;
  1504. }
  1505. version = avio_r8(pb);
  1506. if (version > 1) {
  1507. avpriv_request_sample(c->fc, "Version %d", version);
  1508. return AVERROR_PATCHWELCOME;
  1509. }
  1510. avio_rb24(pb); /* flags */
  1511. mov_metadata_creation_time(c, pb, &st->metadata, version);
  1512. sc->time_scale = avio_rb32(pb);
  1513. if (sc->time_scale <= 0) {
  1514. av_log(c->fc, AV_LOG_ERROR, "Invalid mdhd time scale %d, defaulting to 1\n", sc->time_scale);
  1515. sc->time_scale = 1;
  1516. }
  1517. st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  1518. if ((version == 1 && st->duration == UINT64_MAX) ||
  1519. (version != 1 && st->duration == UINT32_MAX)) {
  1520. st->duration = 0;
  1521. }
  1522. lang = avio_rb16(pb); /* language */
  1523. if (ff_mov_lang_to_iso639(lang, language))
  1524. av_dict_set(&st->metadata, "language", language, 0);
  1525. avio_rb16(pb); /* quality */
  1526. return 0;
  1527. }
  1528. static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1529. {
  1530. int i;
  1531. int version = avio_r8(pb); /* version */
  1532. avio_rb24(pb); /* flags */
  1533. mov_metadata_creation_time(c, pb, &c->fc->metadata, version);
  1534. c->time_scale = avio_rb32(pb); /* time scale */
  1535. if (c->time_scale <= 0) {
  1536. av_log(c->fc, AV_LOG_ERROR, "Invalid mvhd time scale %d, defaulting to 1\n", c->time_scale);
  1537. c->time_scale = 1;
  1538. }
  1539. av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
  1540. c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  1541. avio_rb32(pb); /* preferred scale */
  1542. avio_rb16(pb); /* preferred volume */
  1543. avio_skip(pb, 10); /* reserved */
  1544. /* movie display matrix, store it in main context and use it later on */
  1545. for (i = 0; i < 3; i++) {
  1546. c->movie_display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  1547. c->movie_display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  1548. c->movie_display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
  1549. }
  1550. avio_rb32(pb); /* preview time */
  1551. avio_rb32(pb); /* preview duration */
  1552. avio_rb32(pb); /* poster time */
  1553. avio_rb32(pb); /* selection time */
  1554. avio_rb32(pb); /* selection duration */
  1555. avio_rb32(pb); /* current time */
  1556. avio_rb32(pb); /* next track ID */
  1557. return 0;
  1558. }
  1559. static void set_last_stream_little_endian(AVFormatContext *fc)
  1560. {
  1561. AVStream *st;
  1562. if (fc->nb_streams < 1)
  1563. return;
  1564. st = fc->streams[fc->nb_streams-1];
  1565. switch (st->codecpar->codec_id) {
  1566. case AV_CODEC_ID_PCM_S16BE:
  1567. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  1568. break;
  1569. case AV_CODEC_ID_PCM_S24BE:
  1570. st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
  1571. break;
  1572. case AV_CODEC_ID_PCM_S32BE:
  1573. st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
  1574. break;
  1575. case AV_CODEC_ID_PCM_F32BE:
  1576. st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
  1577. break;
  1578. case AV_CODEC_ID_PCM_F64BE:
  1579. st->codecpar->codec_id = AV_CODEC_ID_PCM_F64LE;
  1580. break;
  1581. default:
  1582. break;
  1583. }
  1584. }
  1585. static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1586. {
  1587. int little_endian = avio_rb16(pb) & 0xFF;
  1588. av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
  1589. if (little_endian == 1)
  1590. set_last_stream_little_endian(c->fc);
  1591. return 0;
  1592. }
  1593. static int mov_read_pcmc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1594. {
  1595. int format_flags;
  1596. int version, flags;
  1597. int pcm_sample_size;
  1598. AVFormatContext *fc = c->fc;
  1599. AVStream *st;
  1600. MOVStreamContext *sc;
  1601. if (atom.size < 6) {
  1602. av_log(c->fc, AV_LOG_ERROR, "Empty pcmC box\n");
  1603. return AVERROR_INVALIDDATA;
  1604. }
  1605. version = avio_r8(pb);
  1606. flags = avio_rb24(pb);
  1607. if (version != 0 || flags != 0) {
  1608. av_log(c->fc, AV_LOG_ERROR,
  1609. "Unsupported 'pcmC' box with version %d, flags: %x",
  1610. version, flags);
  1611. return AVERROR_INVALIDDATA;
  1612. }
  1613. format_flags = avio_r8(pb);
  1614. pcm_sample_size = avio_r8(pb);
  1615. if (fc->nb_streams < 1)
  1616. return AVERROR_INVALIDDATA;
  1617. st = fc->streams[fc->nb_streams - 1];
  1618. sc = st->priv_data;
  1619. if (sc->format == MOV_MP4_FPCM_TAG) {
  1620. switch (pcm_sample_size) {
  1621. case 32:
  1622. st->codecpar->codec_id = AV_CODEC_ID_PCM_F32BE;
  1623. break;
  1624. case 64:
  1625. st->codecpar->codec_id = AV_CODEC_ID_PCM_F64BE;
  1626. break;
  1627. default:
  1628. av_log(fc, AV_LOG_ERROR, "invalid pcm_sample_size %d for %s\n",
  1629. pcm_sample_size,
  1630. av_fourcc2str(sc->format));
  1631. return AVERROR_INVALIDDATA;
  1632. }
  1633. } else if (sc->format == MOV_MP4_IPCM_TAG) {
  1634. switch (pcm_sample_size) {
  1635. case 16:
  1636. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
  1637. break;
  1638. case 24:
  1639. st->codecpar->codec_id = AV_CODEC_ID_PCM_S24BE;
  1640. break;
  1641. case 32:
  1642. st->codecpar->codec_id = AV_CODEC_ID_PCM_S32BE;
  1643. break;
  1644. default:
  1645. av_log(fc, AV_LOG_ERROR, "invalid pcm_sample_size %d for %s\n",
  1646. pcm_sample_size,
  1647. av_fourcc2str(sc->format));
  1648. return AVERROR_INVALIDDATA;
  1649. }
  1650. } else {
  1651. av_log(fc, AV_LOG_ERROR, "'pcmC' with invalid sample entry '%s'\n",
  1652. av_fourcc2str(sc->format));
  1653. return AVERROR_INVALIDDATA;
  1654. }
  1655. if (format_flags & 1) // indicates little-endian format. If not present, big-endian format is used
  1656. set_last_stream_little_endian(c->fc);
  1657. st->codecpar->bits_per_coded_sample = av_get_bits_per_sample(st->codecpar->codec_id);
  1658. return 0;
  1659. }
  1660. static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1661. {
  1662. AVStream *st;
  1663. char color_parameter_type[5] = { 0 };
  1664. uint16_t color_primaries, color_trc, color_matrix;
  1665. int ret;
  1666. st = get_curr_st(c);
  1667. if (!st)
  1668. return 0;
  1669. ret = ffio_read_size(pb, color_parameter_type, 4);
  1670. if (ret < 0)
  1671. return ret;
  1672. if (strncmp(color_parameter_type, "nclx", 4) &&
  1673. strncmp(color_parameter_type, "nclc", 4) &&
  1674. strncmp(color_parameter_type, "prof", 4)) {
  1675. av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
  1676. color_parameter_type);
  1677. return 0;
  1678. }
  1679. if (!strncmp(color_parameter_type, "prof", 4)) {
  1680. AVPacketSideData *sd = av_packet_side_data_new(&st->codecpar->coded_side_data,
  1681. &st->codecpar->nb_coded_side_data,
  1682. AV_PKT_DATA_ICC_PROFILE,
  1683. atom.size - 4, 0);
  1684. if (!sd)
  1685. return AVERROR(ENOMEM);
  1686. ret = ffio_read_size(pb, sd->data, atom.size - 4);
  1687. if (ret < 0)
  1688. return ret;
  1689. } else {
  1690. color_primaries = avio_rb16(pb);
  1691. color_trc = avio_rb16(pb);
  1692. color_matrix = avio_rb16(pb);
  1693. av_log(c->fc, AV_LOG_TRACE,
  1694. "%s: pri %d trc %d matrix %d",
  1695. color_parameter_type, color_primaries, color_trc, color_matrix);
  1696. if (!strncmp(color_parameter_type, "nclx", 4)) {
  1697. uint8_t color_range = avio_r8(pb) >> 7;
  1698. av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
  1699. if (color_range)
  1700. st->codecpar->color_range = AVCOL_RANGE_JPEG;
  1701. else
  1702. st->codecpar->color_range = AVCOL_RANGE_MPEG;
  1703. }
  1704. if (!av_color_primaries_name(color_primaries))
  1705. color_primaries = AVCOL_PRI_UNSPECIFIED;
  1706. if (!av_color_transfer_name(color_trc))
  1707. color_trc = AVCOL_TRC_UNSPECIFIED;
  1708. if (!av_color_space_name(color_matrix))
  1709. color_matrix = AVCOL_SPC_UNSPECIFIED;
  1710. st->codecpar->color_primaries = color_primaries;
  1711. st->codecpar->color_trc = color_trc;
  1712. st->codecpar->color_space = color_matrix;
  1713. av_log(c->fc, AV_LOG_TRACE, "\n");
  1714. }
  1715. return 0;
  1716. }
  1717. static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1718. {
  1719. AVStream *st;
  1720. unsigned mov_field_order;
  1721. enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
  1722. if (c->fc->nb_streams < 1) // will happen with jp2 files
  1723. return 0;
  1724. st = c->fc->streams[c->fc->nb_streams-1];
  1725. if (atom.size < 2)
  1726. return AVERROR_INVALIDDATA;
  1727. mov_field_order = avio_rb16(pb);
  1728. if ((mov_field_order & 0xFF00) == 0x0100)
  1729. decoded_field_order = AV_FIELD_PROGRESSIVE;
  1730. else if ((mov_field_order & 0xFF00) == 0x0200) {
  1731. switch (mov_field_order & 0xFF) {
  1732. case 0x01: decoded_field_order = AV_FIELD_TT;
  1733. break;
  1734. case 0x06: decoded_field_order = AV_FIELD_BB;
  1735. break;
  1736. case 0x09: decoded_field_order = AV_FIELD_TB;
  1737. break;
  1738. case 0x0E: decoded_field_order = AV_FIELD_BT;
  1739. break;
  1740. }
  1741. }
  1742. if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
  1743. av_log(c->fc, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
  1744. }
  1745. st->codecpar->field_order = decoded_field_order;
  1746. return 0;
  1747. }
  1748. static int mov_realloc_extradata(AVCodecParameters *par, MOVAtom atom)
  1749. {
  1750. int err = 0;
  1751. uint64_t size = (uint64_t)par->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
  1752. if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  1753. return AVERROR_INVALIDDATA;
  1754. if ((err = av_reallocp(&par->extradata, size)) < 0) {
  1755. par->extradata_size = 0;
  1756. return err;
  1757. }
  1758. par->extradata_size = size - AV_INPUT_BUFFER_PADDING_SIZE;
  1759. return 0;
  1760. }
  1761. /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
  1762. static int64_t mov_read_atom_into_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  1763. AVCodecParameters *par, uint8_t *buf)
  1764. {
  1765. int64_t result = atom.size;
  1766. int err;
  1767. AV_WB32(buf , atom.size + 8);
  1768. AV_WL32(buf + 4, atom.type);
  1769. err = ffio_read_size(pb, buf + 8, atom.size);
  1770. if (err < 0) {
  1771. par->extradata_size -= atom.size;
  1772. return err;
  1773. } else if (err < atom.size) {
  1774. av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
  1775. par->extradata_size -= atom.size - err;
  1776. result = err;
  1777. }
  1778. memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  1779. return result;
  1780. }
  1781. /* FIXME modify QDM2/SVQ3/H.264 decoders to take full atom as extradata */
  1782. static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  1783. enum AVCodecID codec_id)
  1784. {
  1785. AVStream *st;
  1786. uint64_t original_size;
  1787. int err;
  1788. if (c->fc->nb_streams < 1) // will happen with jp2 files
  1789. return 0;
  1790. st = c->fc->streams[c->fc->nb_streams-1];
  1791. if (st->codecpar->codec_id != codec_id)
  1792. return 0; /* unexpected codec_id - don't mess with extradata */
  1793. original_size = st->codecpar->extradata_size;
  1794. err = mov_realloc_extradata(st->codecpar, atom);
  1795. if (err)
  1796. return err;
  1797. err = mov_read_atom_into_extradata(c, pb, atom, st->codecpar, st->codecpar->extradata + original_size);
  1798. if (err < 0)
  1799. return err;
  1800. return 0; // Note: this is the original behavior to ignore truncation.
  1801. }
  1802. /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
  1803. static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1804. {
  1805. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
  1806. }
  1807. static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1808. {
  1809. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_CAVS);
  1810. }
  1811. static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1812. {
  1813. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
  1814. }
  1815. static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1816. {
  1817. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
  1818. }
  1819. static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1820. {
  1821. int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
  1822. if (!ret)
  1823. ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
  1824. return ret;
  1825. }
  1826. static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1827. {
  1828. int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
  1829. if (!ret && c->fc->nb_streams >= 1) {
  1830. AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
  1831. if (par->extradata_size >= 40) {
  1832. par->height = AV_RB16(&par->extradata[36]);
  1833. par->width = AV_RB16(&par->extradata[38]);
  1834. }
  1835. }
  1836. return ret;
  1837. }
  1838. static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1839. {
  1840. if (c->fc->nb_streams >= 1) {
  1841. AVStream *const st = c->fc->streams[c->fc->nb_streams - 1];
  1842. FFStream *const sti = ffstream(st);
  1843. AVCodecParameters *par = st->codecpar;
  1844. if (par->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
  1845. par->codec_id == AV_CODEC_ID_H264 &&
  1846. atom.size > 11) {
  1847. int cid;
  1848. avio_skip(pb, 10);
  1849. cid = avio_rb16(pb);
  1850. /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
  1851. if (cid == 0xd4d || cid == 0xd4e)
  1852. par->width = 1440;
  1853. return 0;
  1854. } else if ((par->codec_tag == MKTAG('A', 'V', 'd', '1') ||
  1855. par->codec_tag == MKTAG('A', 'V', 'j', '2') ||
  1856. par->codec_tag == MKTAG('A', 'V', 'd', 'n')) &&
  1857. atom.size >= 24) {
  1858. int num, den;
  1859. avio_skip(pb, 12);
  1860. num = avio_rb32(pb);
  1861. den = avio_rb32(pb);
  1862. if (num <= 0 || den <= 0)
  1863. return 0;
  1864. switch (avio_rb32(pb)) {
  1865. case 2:
  1866. if (den >= INT_MAX / 2)
  1867. return 0;
  1868. den *= 2;
  1869. case 1:
  1870. sti->display_aspect_ratio = (AVRational){ num, den };
  1871. default:
  1872. return 0;
  1873. }
  1874. }
  1875. }
  1876. return mov_read_avid(c, pb, atom);
  1877. }
  1878. static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1879. {
  1880. int ret = 0;
  1881. int length = 0;
  1882. uint64_t original_size;
  1883. if (c->fc->nb_streams >= 1) {
  1884. AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
  1885. if (par->codec_id == AV_CODEC_ID_H264)
  1886. return 0;
  1887. if (atom.size == 16) {
  1888. original_size = par->extradata_size;
  1889. ret = mov_realloc_extradata(par, atom);
  1890. if (!ret) {
  1891. length = mov_read_atom_into_extradata(c, pb, atom, par, par->extradata + original_size);
  1892. if (length == atom.size) {
  1893. const uint8_t range_value = par->extradata[original_size + 19];
  1894. switch (range_value) {
  1895. case 1:
  1896. par->color_range = AVCOL_RANGE_MPEG;
  1897. break;
  1898. case 2:
  1899. par->color_range = AVCOL_RANGE_JPEG;
  1900. break;
  1901. default:
  1902. av_log(c->fc, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
  1903. break;
  1904. }
  1905. ff_dlog(c->fc, "color_range: %d\n", par->color_range);
  1906. } else {
  1907. /* For some reason the whole atom was not added to the extradata */
  1908. av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
  1909. }
  1910. } else {
  1911. av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
  1912. }
  1913. } else {
  1914. av_log(c->fc, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
  1915. }
  1916. }
  1917. return ret;
  1918. }
  1919. static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1920. {
  1921. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
  1922. }
  1923. static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1924. {
  1925. AVStream *st;
  1926. int ret;
  1927. if (c->fc->nb_streams < 1)
  1928. return 0;
  1929. st = c->fc->streams[c->fc->nb_streams-1];
  1930. if ((uint64_t)atom.size > (1<<30))
  1931. return AVERROR_INVALIDDATA;
  1932. if (st->codecpar->codec_id == AV_CODEC_ID_QDM2 ||
  1933. st->codecpar->codec_id == AV_CODEC_ID_QDMC ||
  1934. st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
  1935. // pass all frma atom to codec, needed at least for QDMC and QDM2
  1936. ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
  1937. if (ret < 0)
  1938. return ret;
  1939. } else if (atom.size > 8) { /* to read frma, esds atoms */
  1940. if (st->codecpar->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
  1941. uint64_t buffer;
  1942. ret = ffio_ensure_seekback(pb, 8);
  1943. if (ret < 0)
  1944. return ret;
  1945. buffer = avio_rb64(pb);
  1946. atom.size -= 8;
  1947. if ( (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
  1948. && buffer >> 32 <= atom.size
  1949. && buffer >> 32 >= 8) {
  1950. avio_skip(pb, -8);
  1951. atom.size += 8;
  1952. } else if (!st->codecpar->extradata_size) {
  1953. #define ALAC_EXTRADATA_SIZE 36
  1954. st->codecpar->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
  1955. if (!st->codecpar->extradata)
  1956. return AVERROR(ENOMEM);
  1957. st->codecpar->extradata_size = ALAC_EXTRADATA_SIZE;
  1958. AV_WB32(st->codecpar->extradata , ALAC_EXTRADATA_SIZE);
  1959. AV_WB32(st->codecpar->extradata + 4, MKTAG('a','l','a','c'));
  1960. AV_WB64(st->codecpar->extradata + 12, buffer);
  1961. avio_read(pb, st->codecpar->extradata + 20, 16);
  1962. avio_skip(pb, atom.size - 24);
  1963. return 0;
  1964. }
  1965. }
  1966. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1967. return ret;
  1968. } else
  1969. avio_skip(pb, atom.size);
  1970. return 0;
  1971. }
  1972. /**
  1973. * This function reads atom content and puts data in extradata without tag
  1974. * nor size unlike mov_read_extradata.
  1975. */
  1976. static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1977. {
  1978. AVStream *st;
  1979. int ret;
  1980. st = get_curr_st(c);
  1981. if (!st)
  1982. return 0;
  1983. if ((uint64_t)atom.size > (1<<30))
  1984. return AVERROR_INVALIDDATA;
  1985. if (atom.type == MKTAG('v','v','c','C')) {
  1986. avio_skip(pb, 4);
  1987. atom.size -= 4;
  1988. }
  1989. if (atom.size >= 10) {
  1990. // Broken files created by legacy versions of libavformat will
  1991. // wrap a whole fiel atom inside of a glbl atom.
  1992. unsigned size = avio_rb32(pb);
  1993. unsigned type = avio_rl32(pb);
  1994. if (avio_feof(pb))
  1995. return AVERROR_INVALIDDATA;
  1996. avio_seek(pb, -8, SEEK_CUR);
  1997. if (type == MKTAG('f','i','e','l') && size == atom.size)
  1998. return mov_read_default(c, pb, atom);
  1999. }
  2000. if (st->codecpar->extradata_size > 1 && st->codecpar->extradata) {
  2001. av_log(c->fc, AV_LOG_WARNING, "ignoring multiple glbl\n");
  2002. return 0;
  2003. }
  2004. ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
  2005. if (ret < 0)
  2006. return ret;
  2007. if (atom.type == MKTAG('h','v','c','C') && st->codecpar->codec_tag == MKTAG('d','v','h','1'))
  2008. /* HEVC-based Dolby Vision derived from hvc1.
  2009. Happens to match with an identifier
  2010. previously utilized for DV. Thus, if we have
  2011. the hvcC extradata box available as specified,
  2012. set codec to HEVC */
  2013. st->codecpar->codec_id = AV_CODEC_ID_HEVC;
  2014. return 0;
  2015. }
  2016. static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2017. {
  2018. AVStream *st;
  2019. uint8_t profile_level;
  2020. int ret;
  2021. if (c->fc->nb_streams < 1)
  2022. return 0;
  2023. st = c->fc->streams[c->fc->nb_streams-1];
  2024. if (atom.size >= (1<<28) || atom.size < 7)
  2025. return AVERROR_INVALIDDATA;
  2026. profile_level = avio_r8(pb);
  2027. if ((profile_level & 0xf0) != 0xc0)
  2028. return 0;
  2029. avio_seek(pb, 6, SEEK_CUR);
  2030. ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 7);
  2031. if (ret < 0)
  2032. return ret;
  2033. return 0;
  2034. }
  2035. /**
  2036. * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
  2037. * but can have extradata appended at the end after the 40 bytes belonging
  2038. * to the struct.
  2039. */
  2040. static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2041. {
  2042. AVStream *st;
  2043. int ret;
  2044. if (c->fc->nb_streams < 1)
  2045. return 0;
  2046. if (atom.size <= 40)
  2047. return 0;
  2048. st = c->fc->streams[c->fc->nb_streams-1];
  2049. if ((uint64_t)atom.size > (1<<30))
  2050. return AVERROR_INVALIDDATA;
  2051. avio_skip(pb, 40);
  2052. ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 40);
  2053. if (ret < 0)
  2054. return ret;
  2055. return 0;
  2056. }
  2057. static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2058. {
  2059. AVStream *st;
  2060. MOVStreamContext *sc;
  2061. unsigned int i, entries;
  2062. if (c->trak_index < 0) {
  2063. av_log(c->fc, AV_LOG_WARNING, "STCO outside TRAK\n");
  2064. return 0;
  2065. }
  2066. if (c->fc->nb_streams < 1)
  2067. return 0;
  2068. st = c->fc->streams[c->fc->nb_streams-1];
  2069. sc = st->priv_data;
  2070. avio_r8(pb); /* version */
  2071. avio_rb24(pb); /* flags */
  2072. // Clamp allocation size for `chunk_offsets` -- don't throw an error for an
  2073. // invalid count since the EOF path doesn't throw either.
  2074. entries = avio_rb32(pb);
  2075. entries =
  2076. FFMIN(entries,
  2077. FFMAX(0, (atom.size - 8) /
  2078. (atom.type == MKTAG('s', 't', 'c', 'o') ? 4 : 8)));
  2079. if (!entries)
  2080. return 0;
  2081. if (sc->chunk_offsets) {
  2082. av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicated STCO atom\n");
  2083. return 0;
  2084. }
  2085. av_free(sc->chunk_offsets);
  2086. sc->chunk_count = 0;
  2087. sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
  2088. if (!sc->chunk_offsets)
  2089. return AVERROR(ENOMEM);
  2090. sc->chunk_count = entries;
  2091. if (atom.type == MKTAG('s','t','c','o'))
  2092. for (i = 0; i < entries && !pb->eof_reached; i++)
  2093. sc->chunk_offsets[i] = avio_rb32(pb);
  2094. else if (atom.type == MKTAG('c','o','6','4'))
  2095. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2096. sc->chunk_offsets[i] = avio_rb64(pb);
  2097. if (sc->chunk_offsets[i] < 0) {
  2098. av_log(c->fc, AV_LOG_WARNING, "Impossible chunk_offset\n");
  2099. sc->chunk_offsets[i] = 0;
  2100. }
  2101. }
  2102. else
  2103. return AVERROR_INVALIDDATA;
  2104. sc->chunk_count = i;
  2105. if (pb->eof_reached) {
  2106. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STCO atom\n");
  2107. return AVERROR_EOF;
  2108. }
  2109. return 0;
  2110. }
  2111. static int mov_codec_id(AVStream *st, uint32_t format)
  2112. {
  2113. int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
  2114. if (id <= 0 &&
  2115. ((format & 0xFFFF) == 'm' + ('s' << 8) ||
  2116. (format & 0xFFFF) == 'T' + ('S' << 8)))
  2117. id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
  2118. if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
  2119. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  2120. } else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
  2121. /* skip old ASF MPEG-4 tag */
  2122. format && format != MKTAG('m','p','4','s')) {
  2123. id = ff_codec_get_id(ff_codec_movvideo_tags, format);
  2124. if (id <= 0)
  2125. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  2126. if (id > 0)
  2127. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  2128. else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA ||
  2129. (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  2130. st->codecpar->codec_id == AV_CODEC_ID_NONE)) {
  2131. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  2132. if (id <= 0) {
  2133. id = (format == MOV_MP4_TTML_TAG || format == MOV_ISMV_TTML_TAG) ?
  2134. AV_CODEC_ID_TTML : id;
  2135. }
  2136. if (id > 0)
  2137. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  2138. else
  2139. id = ff_codec_get_id(ff_codec_movdata_tags, format);
  2140. }
  2141. }
  2142. st->codecpar->codec_tag = format;
  2143. return id;
  2144. }
  2145. static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
  2146. AVStream *st, MOVStreamContext *sc)
  2147. {
  2148. uint8_t codec_name[32] = { 0 };
  2149. int64_t stsd_start;
  2150. unsigned int len;
  2151. uint32_t id = 0;
  2152. /* The first 16 bytes of the video sample description are already
  2153. * read in ff_mov_read_stsd_entries() */
  2154. stsd_start = avio_tell(pb) - 16;
  2155. avio_rb16(pb); /* version */
  2156. avio_rb16(pb); /* revision level */
  2157. id = avio_rl32(pb); /* vendor */
  2158. av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
  2159. avio_rb32(pb); /* temporal quality */
  2160. avio_rb32(pb); /* spatial quality */
  2161. st->codecpar->width = avio_rb16(pb); /* width */
  2162. st->codecpar->height = avio_rb16(pb); /* height */
  2163. avio_rb32(pb); /* horiz resolution */
  2164. avio_rb32(pb); /* vert resolution */
  2165. avio_rb32(pb); /* data size, always 0 */
  2166. avio_rb16(pb); /* frames per samples */
  2167. len = avio_r8(pb); /* codec name, pascal string */
  2168. if (len > 31)
  2169. len = 31;
  2170. mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
  2171. if (len < 31)
  2172. avio_skip(pb, 31 - len);
  2173. if (codec_name[0])
  2174. av_dict_set(&st->metadata, "encoder", codec_name, 0);
  2175. /* codec_tag YV12 triggers an UV swap in rawdec.c */
  2176. if (!strncmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
  2177. st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
  2178. st->codecpar->width &= ~1;
  2179. st->codecpar->height &= ~1;
  2180. }
  2181. /* Flash Media Server uses tag H.263 with Sorenson Spark */
  2182. if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
  2183. !strncmp(codec_name, "Sorenson H263", 13))
  2184. st->codecpar->codec_id = AV_CODEC_ID_FLV1;
  2185. st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
  2186. avio_seek(pb, stsd_start, SEEK_SET);
  2187. if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
  2188. st->codecpar->bits_per_coded_sample &= 0x1F;
  2189. sc->has_palette = 1;
  2190. }
  2191. }
  2192. static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
  2193. AVStream *st, MOVStreamContext *sc)
  2194. {
  2195. int bits_per_sample, flags;
  2196. uint16_t version = avio_rb16(pb);
  2197. uint32_t id = 0;
  2198. AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
  2199. int channel_count;
  2200. avio_rb16(pb); /* revision level */
  2201. id = avio_rl32(pb); /* vendor */
  2202. av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
  2203. channel_count = avio_rb16(pb);
  2204. st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
  2205. st->codecpar->ch_layout.nb_channels = channel_count;
  2206. st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
  2207. av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", channel_count);
  2208. sc->audio_cid = avio_rb16(pb);
  2209. avio_rb16(pb); /* packet size = 0 */
  2210. st->codecpar->sample_rate = ((avio_rb32(pb) >> 16));
  2211. // Read QT version 1 fields. In version 0 these do not exist.
  2212. av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
  2213. if (!c->isom ||
  2214. (compatible_brands && strstr(compatible_brands->value, "qt ")) ||
  2215. (sc->stsd_version == 0 && version > 0)) {
  2216. if (version == 1) {
  2217. sc->samples_per_frame = avio_rb32(pb);
  2218. avio_rb32(pb); /* bytes per packet */
  2219. sc->bytes_per_frame = avio_rb32(pb);
  2220. avio_rb32(pb); /* bytes per sample */
  2221. } else if (version == 2) {
  2222. avio_rb32(pb); /* sizeof struct only */
  2223. st->codecpar->sample_rate = av_int2double(avio_rb64(pb));
  2224. channel_count = avio_rb32(pb);
  2225. st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
  2226. st->codecpar->ch_layout.nb_channels = channel_count;
  2227. avio_rb32(pb); /* always 0x7F000000 */
  2228. st->codecpar->bits_per_coded_sample = avio_rb32(pb);
  2229. flags = avio_rb32(pb); /* lpcm format specific flag */
  2230. sc->bytes_per_frame = avio_rb32(pb);
  2231. sc->samples_per_frame = avio_rb32(pb);
  2232. if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
  2233. st->codecpar->codec_id =
  2234. ff_mov_get_lpcm_codec_id(st->codecpar->bits_per_coded_sample,
  2235. flags);
  2236. }
  2237. if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
  2238. /* can't correctly handle variable sized packet as audio unit */
  2239. switch (st->codecpar->codec_id) {
  2240. case AV_CODEC_ID_MP2:
  2241. case AV_CODEC_ID_MP3:
  2242. ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
  2243. break;
  2244. }
  2245. }
  2246. }
  2247. if (sc->format == 0) {
  2248. if (st->codecpar->bits_per_coded_sample == 8)
  2249. st->codecpar->codec_id = mov_codec_id(st, MKTAG('r','a','w',' '));
  2250. else if (st->codecpar->bits_per_coded_sample == 16)
  2251. st->codecpar->codec_id = mov_codec_id(st, MKTAG('t','w','o','s'));
  2252. }
  2253. switch (st->codecpar->codec_id) {
  2254. case AV_CODEC_ID_PCM_S8:
  2255. case AV_CODEC_ID_PCM_U8:
  2256. if (st->codecpar->bits_per_coded_sample == 16)
  2257. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
  2258. break;
  2259. case AV_CODEC_ID_PCM_S16LE:
  2260. case AV_CODEC_ID_PCM_S16BE:
  2261. if (st->codecpar->bits_per_coded_sample == 8)
  2262. st->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
  2263. else if (st->codecpar->bits_per_coded_sample == 24)
  2264. st->codecpar->codec_id =
  2265. st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
  2266. AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
  2267. else if (st->codecpar->bits_per_coded_sample == 32)
  2268. st->codecpar->codec_id =
  2269. st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
  2270. AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
  2271. break;
  2272. /* set values for old format before stsd version 1 appeared */
  2273. case AV_CODEC_ID_MACE3:
  2274. sc->samples_per_frame = 6;
  2275. sc->bytes_per_frame = 2 * st->codecpar->ch_layout.nb_channels;
  2276. break;
  2277. case AV_CODEC_ID_MACE6:
  2278. sc->samples_per_frame = 6;
  2279. sc->bytes_per_frame = 1 * st->codecpar->ch_layout.nb_channels;
  2280. break;
  2281. case AV_CODEC_ID_ADPCM_IMA_QT:
  2282. sc->samples_per_frame = 64;
  2283. sc->bytes_per_frame = 34 * st->codecpar->ch_layout.nb_channels;
  2284. break;
  2285. case AV_CODEC_ID_GSM:
  2286. sc->samples_per_frame = 160;
  2287. sc->bytes_per_frame = 33;
  2288. break;
  2289. default:
  2290. break;
  2291. }
  2292. bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id);
  2293. if (bits_per_sample && (bits_per_sample >> 3) * (uint64_t)st->codecpar->ch_layout.nb_channels <= INT_MAX) {
  2294. st->codecpar->bits_per_coded_sample = bits_per_sample;
  2295. sc->sample_size = (bits_per_sample >> 3) * st->codecpar->ch_layout.nb_channels;
  2296. }
  2297. }
  2298. static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
  2299. AVStream *st, MOVStreamContext *sc,
  2300. int64_t size)
  2301. {
  2302. // ttxt stsd contains display flags, justification, background
  2303. // color, fonts, and default styles, so fake an atom to read it
  2304. MOVAtom fake_atom = { .size = size };
  2305. // mp4s contains a regular esds atom, dfxp ISMV TTML has no content
  2306. // in extradata unlike stpp MP4 TTML.
  2307. if (st->codecpar->codec_tag != AV_RL32("mp4s") &&
  2308. st->codecpar->codec_tag != MOV_ISMV_TTML_TAG)
  2309. mov_read_glbl(c, pb, fake_atom);
  2310. st->codecpar->width = sc->width;
  2311. st->codecpar->height = sc->height;
  2312. }
  2313. static uint32_t yuv_to_rgba(uint32_t ycbcr)
  2314. {
  2315. uint8_t r, g, b;
  2316. int y, cb, cr;
  2317. y = (ycbcr >> 16) & 0xFF;
  2318. cr = (ycbcr >> 8) & 0xFF;
  2319. cb = ycbcr & 0xFF;
  2320. b = av_clip_uint8((1164 * (y - 16) + 2018 * (cb - 128)) / 1000);
  2321. g = av_clip_uint8((1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000);
  2322. r = av_clip_uint8((1164 * (y - 16) + 1596 * (cr - 128) ) / 1000);
  2323. return (r << 16) | (g << 8) | b;
  2324. }
  2325. static int mov_rewrite_dvd_sub_extradata(AVStream *st)
  2326. {
  2327. char buf[256] = {0};
  2328. uint8_t *src = st->codecpar->extradata;
  2329. int i, ret;
  2330. if (st->codecpar->extradata_size != 64)
  2331. return 0;
  2332. if (st->codecpar->width > 0 && st->codecpar->height > 0)
  2333. snprintf(buf, sizeof(buf), "size: %dx%d\n",
  2334. st->codecpar->width, st->codecpar->height);
  2335. av_strlcat(buf, "palette: ", sizeof(buf));
  2336. for (i = 0; i < 16; i++) {
  2337. uint32_t yuv = AV_RB32(src + i * 4);
  2338. uint32_t rgba = yuv_to_rgba(yuv);
  2339. av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
  2340. }
  2341. if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
  2342. return 0;
  2343. ret = ff_alloc_extradata(st->codecpar, strlen(buf));
  2344. if (ret < 0)
  2345. return ret;
  2346. memcpy(st->codecpar->extradata, buf, st->codecpar->extradata_size);
  2347. return 0;
  2348. }
  2349. static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
  2350. AVStream *st, MOVStreamContext *sc,
  2351. int64_t size)
  2352. {
  2353. int ret;
  2354. if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
  2355. if ((int)size != size)
  2356. return AVERROR(ENOMEM);
  2357. ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
  2358. if (ret < 0)
  2359. return ret;
  2360. if (size > 16) {
  2361. MOVStreamContext *tmcd_ctx = st->priv_data;
  2362. int val;
  2363. val = AV_RB32(st->codecpar->extradata + 4);
  2364. tmcd_ctx->tmcd_flags = val;
  2365. st->avg_frame_rate.num = AV_RB32(st->codecpar->extradata + 8); /* timescale */
  2366. st->avg_frame_rate.den = AV_RB32(st->codecpar->extradata + 12); /* frameDuration */
  2367. tmcd_ctx->tmcd_nb_frames = st->codecpar->extradata[16]; /* number of frames */
  2368. if (size > 30) {
  2369. uint32_t len = AV_RB32(st->codecpar->extradata + 18); /* name atom length */
  2370. uint32_t format = AV_RB32(st->codecpar->extradata + 22);
  2371. if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
  2372. uint16_t str_size = AV_RB16(st->codecpar->extradata + 26); /* string length */
  2373. if (str_size > 0 && size >= (int)str_size + 30 &&
  2374. st->codecpar->extradata[30] /* Don't add empty string */) {
  2375. char *reel_name = av_malloc(str_size + 1);
  2376. if (!reel_name)
  2377. return AVERROR(ENOMEM);
  2378. memcpy(reel_name, st->codecpar->extradata + 30, str_size);
  2379. reel_name[str_size] = 0; /* Add null terminator */
  2380. av_dict_set(&st->metadata, "reel_name", reel_name,
  2381. AV_DICT_DONT_STRDUP_VAL);
  2382. }
  2383. }
  2384. }
  2385. }
  2386. } else {
  2387. /* other codec type, just skip (rtp, mp4s ...) */
  2388. avio_skip(pb, size);
  2389. }
  2390. return 0;
  2391. }
  2392. static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
  2393. AVStream *st, MOVStreamContext *sc)
  2394. {
  2395. FFStream *const sti = ffstream(st);
  2396. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  2397. !st->codecpar->sample_rate && sc->time_scale > 1)
  2398. st->codecpar->sample_rate = sc->time_scale;
  2399. /* special codec parameters handling */
  2400. switch (st->codecpar->codec_id) {
  2401. #if CONFIG_DV_DEMUXER
  2402. case AV_CODEC_ID_DVAUDIO:
  2403. if (c->dv_fctx) {
  2404. avpriv_request_sample(c->fc, "multiple DV audio streams");
  2405. return AVERROR(ENOSYS);
  2406. }
  2407. c->dv_fctx = avformat_alloc_context();
  2408. if (!c->dv_fctx) {
  2409. av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
  2410. return AVERROR(ENOMEM);
  2411. }
  2412. c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
  2413. if (!c->dv_demux) {
  2414. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  2415. return AVERROR(ENOMEM);
  2416. }
  2417. sc->dv_audio_container = 1;
  2418. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  2419. break;
  2420. #endif
  2421. /* no ifdef since parameters are always those */
  2422. case AV_CODEC_ID_QCELP:
  2423. av_channel_layout_uninit(&st->codecpar->ch_layout);
  2424. st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
  2425. // force sample rate for qcelp when not stored in mov
  2426. if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
  2427. st->codecpar->sample_rate = 8000;
  2428. // FIXME: Why is the following needed for some files?
  2429. sc->samples_per_frame = 160;
  2430. if (!sc->bytes_per_frame)
  2431. sc->bytes_per_frame = 35;
  2432. break;
  2433. case AV_CODEC_ID_AMR_NB:
  2434. av_channel_layout_uninit(&st->codecpar->ch_layout);
  2435. st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
  2436. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  2437. st->codecpar->sample_rate = 8000;
  2438. break;
  2439. case AV_CODEC_ID_AMR_WB:
  2440. av_channel_layout_uninit(&st->codecpar->ch_layout);
  2441. st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
  2442. st->codecpar->sample_rate = 16000;
  2443. break;
  2444. case AV_CODEC_ID_MP2:
  2445. case AV_CODEC_ID_MP3:
  2446. /* force type after stsd for m1a hdlr */
  2447. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  2448. break;
  2449. case AV_CODEC_ID_GSM:
  2450. case AV_CODEC_ID_ADPCM_MS:
  2451. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2452. case AV_CODEC_ID_ILBC:
  2453. case AV_CODEC_ID_MACE3:
  2454. case AV_CODEC_ID_MACE6:
  2455. case AV_CODEC_ID_QDM2:
  2456. st->codecpar->block_align = sc->bytes_per_frame;
  2457. break;
  2458. case AV_CODEC_ID_ALAC:
  2459. if (st->codecpar->extradata_size == 36) {
  2460. int channel_count = AV_RB8(st->codecpar->extradata + 21);
  2461. if (st->codecpar->ch_layout.nb_channels != channel_count) {
  2462. av_channel_layout_uninit(&st->codecpar->ch_layout);
  2463. st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
  2464. st->codecpar->ch_layout.nb_channels = channel_count;
  2465. }
  2466. st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
  2467. }
  2468. break;
  2469. case AV_CODEC_ID_AC3:
  2470. case AV_CODEC_ID_EAC3:
  2471. case AV_CODEC_ID_MPEG1VIDEO:
  2472. case AV_CODEC_ID_VC1:
  2473. case AV_CODEC_ID_VP8:
  2474. case AV_CODEC_ID_VP9:
  2475. sti->need_parsing = AVSTREAM_PARSE_FULL;
  2476. break;
  2477. case AV_CODEC_ID_EVC:
  2478. case AV_CODEC_ID_AV1:
  2479. /* field_order detection of H264 requires parsing */
  2480. case AV_CODEC_ID_H264:
  2481. sti->need_parsing = AVSTREAM_PARSE_HEADERS;
  2482. break;
  2483. default:
  2484. break;
  2485. }
  2486. return 0;
  2487. }
  2488. static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
  2489. int codec_tag, int format,
  2490. int64_t size)
  2491. {
  2492. if (codec_tag &&
  2493. (codec_tag != format &&
  2494. // AVID 1:1 samples with differing data format and codec tag exist
  2495. (codec_tag != AV_RL32("AV1x") || format != AV_RL32("AVup")) &&
  2496. // prores is allowed to have differing data format and codec tag
  2497. codec_tag != AV_RL32("apcn") && codec_tag != AV_RL32("apch") &&
  2498. // so is dv (sigh)
  2499. codec_tag != AV_RL32("dvpp") && codec_tag != AV_RL32("dvcp") &&
  2500. (c->fc->video_codec_id ? ff_codec_get_id(ff_codec_movvideo_tags, format) != c->fc->video_codec_id
  2501. : codec_tag != MKTAG('j','p','e','g')))) {
  2502. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  2503. * export it as a separate AVStream but this needs a few changes
  2504. * in the MOV demuxer, patch welcome. */
  2505. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  2506. avio_skip(pb, size);
  2507. return 1;
  2508. }
  2509. return 0;
  2510. }
  2511. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
  2512. {
  2513. AVStream *st;
  2514. MOVStreamContext *sc;
  2515. int pseudo_stream_id;
  2516. av_assert0 (c->fc->nb_streams >= 1);
  2517. st = c->fc->streams[c->fc->nb_streams-1];
  2518. sc = st->priv_data;
  2519. for (pseudo_stream_id = 0;
  2520. pseudo_stream_id < entries && !pb->eof_reached;
  2521. pseudo_stream_id++) {
  2522. //Parsing Sample description table
  2523. enum AVCodecID id;
  2524. int ret, dref_id = 1;
  2525. MOVAtom a = { AV_RL32("stsd") };
  2526. int64_t start_pos = avio_tell(pb);
  2527. int64_t size = avio_rb32(pb); /* size */
  2528. uint32_t format = avio_rl32(pb); /* data format */
  2529. if (size >= 16) {
  2530. avio_rb32(pb); /* reserved */
  2531. avio_rb16(pb); /* reserved */
  2532. dref_id = avio_rb16(pb);
  2533. } else if (size <= 7) {
  2534. av_log(c->fc, AV_LOG_ERROR,
  2535. "invalid size %"PRId64" in stsd\n", size);
  2536. return AVERROR_INVALIDDATA;
  2537. }
  2538. if (mov_skip_multiple_stsd(c, pb, st->codecpar->codec_tag, format,
  2539. size - (avio_tell(pb) - start_pos))) {
  2540. sc->stsd_count++;
  2541. continue;
  2542. }
  2543. sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
  2544. sc->dref_id= dref_id;
  2545. sc->format = format;
  2546. id = mov_codec_id(st, format);
  2547. av_log(c->fc, AV_LOG_TRACE,
  2548. "size=%"PRId64" 4CC=%s codec_type=%d\n", size,
  2549. av_fourcc2str(format), st->codecpar->codec_type);
  2550. st->codecpar->codec_id = id;
  2551. if (st->codecpar->codec_type==AVMEDIA_TYPE_VIDEO) {
  2552. mov_parse_stsd_video(c, pb, st, sc);
  2553. } else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
  2554. mov_parse_stsd_audio(c, pb, st, sc);
  2555. if (st->codecpar->sample_rate < 0) {
  2556. av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
  2557. return AVERROR_INVALIDDATA;
  2558. }
  2559. if (st->codecpar->ch_layout.nb_channels < 0) {
  2560. av_log(c->fc, AV_LOG_ERROR, "Invalid channels %d\n", st->codecpar->ch_layout.nb_channels);
  2561. return AVERROR_INVALIDDATA;
  2562. }
  2563. } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
  2564. mov_parse_stsd_subtitle(c, pb, st, sc,
  2565. size - (avio_tell(pb) - start_pos));
  2566. } else {
  2567. ret = mov_parse_stsd_data(c, pb, st, sc,
  2568. size - (avio_tell(pb) - start_pos));
  2569. if (ret < 0)
  2570. return ret;
  2571. }
  2572. /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
  2573. a.size = size - (avio_tell(pb) - start_pos);
  2574. if (a.size > 8) {
  2575. if ((ret = mov_read_default(c, pb, a)) < 0)
  2576. return ret;
  2577. } else if (a.size > 0)
  2578. avio_skip(pb, a.size);
  2579. if (sc->extradata && st->codecpar->extradata) {
  2580. int extra_size = st->codecpar->extradata_size;
  2581. /* Move the current stream extradata to the stream context one. */
  2582. sc->extradata_size[pseudo_stream_id] = extra_size;
  2583. sc->extradata[pseudo_stream_id] = st->codecpar->extradata;
  2584. st->codecpar->extradata = NULL;
  2585. st->codecpar->extradata_size = 0;
  2586. }
  2587. sc->stsd_count++;
  2588. }
  2589. if (pb->eof_reached) {
  2590. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSD atom\n");
  2591. return AVERROR_EOF;
  2592. }
  2593. return 0;
  2594. }
  2595. static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2596. {
  2597. AVStream *st;
  2598. MOVStreamContext *sc;
  2599. int ret, entries;
  2600. if (c->fc->nb_streams < 1)
  2601. return 0;
  2602. st = c->fc->streams[c->fc->nb_streams - 1];
  2603. sc = st->priv_data;
  2604. sc->stsd_version = avio_r8(pb);
  2605. avio_rb24(pb); /* flags */
  2606. entries = avio_rb32(pb);
  2607. /* Each entry contains a size (4 bytes) and format (4 bytes). */
  2608. if (entries <= 0 || entries > atom.size / 8 || entries > 1024) {
  2609. av_log(c->fc, AV_LOG_ERROR, "invalid STSD entries %d\n", entries);
  2610. return AVERROR_INVALIDDATA;
  2611. }
  2612. if (sc->extradata) {
  2613. av_log(c->fc, AV_LOG_ERROR,
  2614. "Duplicate stsd found in this track.\n");
  2615. return AVERROR_INVALIDDATA;
  2616. }
  2617. /* Prepare space for hosting multiple extradata. */
  2618. sc->extradata = av_calloc(entries, sizeof(*sc->extradata));
  2619. if (!sc->extradata)
  2620. return AVERROR(ENOMEM);
  2621. sc->extradata_size = av_calloc(entries, sizeof(*sc->extradata_size));
  2622. if (!sc->extradata_size) {
  2623. ret = AVERROR(ENOMEM);
  2624. goto fail;
  2625. }
  2626. ret = ff_mov_read_stsd_entries(c, pb, entries);
  2627. if (ret < 0)
  2628. goto fail;
  2629. /* Restore back the primary extradata. */
  2630. av_freep(&st->codecpar->extradata);
  2631. st->codecpar->extradata_size = sc->extradata_size[0];
  2632. if (sc->extradata_size[0]) {
  2633. st->codecpar->extradata = av_mallocz(sc->extradata_size[0] + AV_INPUT_BUFFER_PADDING_SIZE);
  2634. if (!st->codecpar->extradata)
  2635. return AVERROR(ENOMEM);
  2636. memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
  2637. }
  2638. return mov_finalize_stsd_codec(c, pb, st, sc);
  2639. fail:
  2640. if (sc->extradata) {
  2641. int j;
  2642. for (j = 0; j < sc->stsd_count; j++)
  2643. av_freep(&sc->extradata[j]);
  2644. }
  2645. av_freep(&sc->extradata);
  2646. av_freep(&sc->extradata_size);
  2647. return ret;
  2648. }
  2649. static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2650. {
  2651. AVStream *st;
  2652. MOVStreamContext *sc;
  2653. unsigned int i, entries;
  2654. if (c->fc->nb_streams < 1)
  2655. return 0;
  2656. st = c->fc->streams[c->fc->nb_streams-1];
  2657. sc = st->priv_data;
  2658. avio_r8(pb); /* version */
  2659. avio_rb24(pb); /* flags */
  2660. entries = avio_rb32(pb);
  2661. if ((uint64_t)entries * 12 + 4 > atom.size)
  2662. return AVERROR_INVALIDDATA;
  2663. av_log(c->fc, AV_LOG_TRACE, "track[%u].stsc.entries = %u\n", c->fc->nb_streams - 1, entries);
  2664. if (!entries)
  2665. return 0;
  2666. if (sc->stsc_data) {
  2667. av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicated STSC atom\n");
  2668. return 0;
  2669. }
  2670. av_free(sc->stsc_data);
  2671. sc->stsc_count = 0;
  2672. sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
  2673. if (!sc->stsc_data)
  2674. return AVERROR(ENOMEM);
  2675. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2676. sc->stsc_data[i].first = avio_rb32(pb);
  2677. sc->stsc_data[i].count = avio_rb32(pb);
  2678. sc->stsc_data[i].id = avio_rb32(pb);
  2679. }
  2680. sc->stsc_count = i;
  2681. for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
  2682. int64_t first_min = i + 1;
  2683. if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
  2684. (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
  2685. sc->stsc_data[i].first < first_min ||
  2686. sc->stsc_data[i].count < 1 ||
  2687. sc->stsc_data[i].id < 1) {
  2688. av_log(c->fc, AV_LOG_WARNING, "STSC entry %d is invalid (first=%d count=%d id=%d)\n", i, sc->stsc_data[i].first, sc->stsc_data[i].count, sc->stsc_data[i].id);
  2689. if (i+1 >= sc->stsc_count) {
  2690. if (sc->stsc_data[i].count == 0 && i > 0) {
  2691. sc->stsc_count --;
  2692. continue;
  2693. }
  2694. sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
  2695. if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
  2696. sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
  2697. sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
  2698. sc->stsc_data[i].id = FFMAX(sc->stsc_data[i].id, 1);
  2699. continue;
  2700. }
  2701. av_assert0(sc->stsc_data[i+1].first >= 2);
  2702. // We replace this entry by the next valid
  2703. sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
  2704. sc->stsc_data[i].count = sc->stsc_data[i+1].count;
  2705. sc->stsc_data[i].id = sc->stsc_data[i+1].id;
  2706. }
  2707. }
  2708. if (pb->eof_reached) {
  2709. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSC atom\n");
  2710. return AVERROR_EOF;
  2711. }
  2712. return 0;
  2713. }
  2714. static inline int mov_stsc_index_valid(unsigned int index, unsigned int count)
  2715. {
  2716. return index < count - 1;
  2717. }
  2718. /* Compute the samples value for the stsc entry at the given index. */
  2719. static inline int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
  2720. {
  2721. int chunk_count;
  2722. if (mov_stsc_index_valid(index, sc->stsc_count))
  2723. chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
  2724. else {
  2725. // Validation for stsc / stco happens earlier in mov_read_stsc + mov_read_trak.
  2726. av_assert0(sc->stsc_data[index].first <= sc->chunk_count);
  2727. chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
  2728. }
  2729. return sc->stsc_data[index].count * (int64_t)chunk_count;
  2730. }
  2731. static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2732. {
  2733. AVStream *st;
  2734. MOVStreamContext *sc;
  2735. unsigned i, entries;
  2736. if (c->fc->nb_streams < 1)
  2737. return 0;
  2738. st = c->fc->streams[c->fc->nb_streams-1];
  2739. sc = st->priv_data;
  2740. avio_rb32(pb); // version + flags
  2741. entries = avio_rb32(pb);
  2742. if (sc->stps_data)
  2743. av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
  2744. av_free(sc->stps_data);
  2745. sc->stps_count = 0;
  2746. sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
  2747. if (!sc->stps_data)
  2748. return AVERROR(ENOMEM);
  2749. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2750. sc->stps_data[i] = avio_rb32(pb);
  2751. }
  2752. sc->stps_count = i;
  2753. if (pb->eof_reached) {
  2754. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STPS atom\n");
  2755. return AVERROR_EOF;
  2756. }
  2757. return 0;
  2758. }
  2759. static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2760. {
  2761. AVStream *st;
  2762. FFStream *sti;
  2763. MOVStreamContext *sc;
  2764. unsigned int i, entries;
  2765. if (c->fc->nb_streams < 1)
  2766. return 0;
  2767. st = c->fc->streams[c->fc->nb_streams-1];
  2768. sti = ffstream(st);
  2769. sc = st->priv_data;
  2770. avio_r8(pb); /* version */
  2771. avio_rb24(pb); /* flags */
  2772. entries = avio_rb32(pb);
  2773. av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %u\n", entries);
  2774. if (!entries) {
  2775. sc->keyframe_absent = 1;
  2776. if (!sti->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  2777. sti->need_parsing = AVSTREAM_PARSE_HEADERS;
  2778. return 0;
  2779. }
  2780. if (sc->keyframes)
  2781. av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
  2782. if (entries >= UINT_MAX / sizeof(int))
  2783. return AVERROR_INVALIDDATA;
  2784. av_freep(&sc->keyframes);
  2785. sc->keyframe_count = 0;
  2786. sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
  2787. if (!sc->keyframes)
  2788. return AVERROR(ENOMEM);
  2789. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2790. sc->keyframes[i] = avio_rb32(pb);
  2791. }
  2792. sc->keyframe_count = i;
  2793. if (pb->eof_reached) {
  2794. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSS atom\n");
  2795. return AVERROR_EOF;
  2796. }
  2797. return 0;
  2798. }
  2799. static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2800. {
  2801. AVStream *st;
  2802. MOVStreamContext *sc;
  2803. unsigned int i, entries, sample_size, field_size, num_bytes;
  2804. GetBitContext gb;
  2805. unsigned char* buf;
  2806. int ret;
  2807. if (c->fc->nb_streams < 1)
  2808. return 0;
  2809. st = c->fc->streams[c->fc->nb_streams-1];
  2810. sc = st->priv_data;
  2811. avio_r8(pb); /* version */
  2812. avio_rb24(pb); /* flags */
  2813. if (atom.type == MKTAG('s','t','s','z')) {
  2814. sample_size = avio_rb32(pb);
  2815. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  2816. sc->sample_size = sample_size;
  2817. sc->stsz_sample_size = sample_size;
  2818. field_size = 32;
  2819. } else {
  2820. sample_size = 0;
  2821. avio_rb24(pb); /* reserved */
  2822. field_size = avio_r8(pb);
  2823. }
  2824. entries = avio_rb32(pb);
  2825. av_log(c->fc, AV_LOG_TRACE, "sample_size = %u sample_count = %u\n", sc->sample_size, entries);
  2826. sc->sample_count = entries;
  2827. if (sample_size)
  2828. return 0;
  2829. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  2830. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %u\n", field_size);
  2831. return AVERROR_INVALIDDATA;
  2832. }
  2833. if (!entries)
  2834. return 0;
  2835. if (entries >= (INT_MAX - 4 - 8 * AV_INPUT_BUFFER_PADDING_SIZE) / field_size)
  2836. return AVERROR_INVALIDDATA;
  2837. if (sc->sample_sizes)
  2838. av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
  2839. av_free(sc->sample_sizes);
  2840. sc->sample_count = 0;
  2841. sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
  2842. if (!sc->sample_sizes)
  2843. return AVERROR(ENOMEM);
  2844. num_bytes = (entries*field_size+4)>>3;
  2845. buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
  2846. if (!buf) {
  2847. av_freep(&sc->sample_sizes);
  2848. return AVERROR(ENOMEM);
  2849. }
  2850. ret = ffio_read_size(pb, buf, num_bytes);
  2851. if (ret < 0) {
  2852. av_freep(&sc->sample_sizes);
  2853. av_free(buf);
  2854. av_log(c->fc, AV_LOG_WARNING, "STSZ atom truncated\n");
  2855. return 0;
  2856. }
  2857. init_get_bits(&gb, buf, 8*num_bytes);
  2858. for (i = 0; i < entries; i++) {
  2859. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  2860. if (sc->sample_sizes[i] < 0) {
  2861. av_free(buf);
  2862. av_log(c->fc, AV_LOG_ERROR, "Invalid sample size %d\n", sc->sample_sizes[i]);
  2863. return AVERROR_INVALIDDATA;
  2864. }
  2865. sc->data_size += sc->sample_sizes[i];
  2866. }
  2867. sc->sample_count = i;
  2868. av_free(buf);
  2869. return 0;
  2870. }
  2871. static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2872. {
  2873. AVStream *st;
  2874. MOVStreamContext *sc;
  2875. unsigned int i, entries, alloc_size = 0;
  2876. int64_t duration = 0;
  2877. int64_t total_sample_count = 0;
  2878. int64_t current_dts = 0;
  2879. int64_t corrected_dts = 0;
  2880. if (c->fc->nb_streams < 1)
  2881. return 0;
  2882. st = c->fc->streams[c->fc->nb_streams-1];
  2883. sc = st->priv_data;
  2884. avio_r8(pb); /* version */
  2885. avio_rb24(pb); /* flags */
  2886. entries = avio_rb32(pb);
  2887. av_log(c->fc, AV_LOG_TRACE, "track[%u].stts.entries = %u\n",
  2888. c->fc->nb_streams-1, entries);
  2889. if (sc->stts_data)
  2890. av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
  2891. av_freep(&sc->stts_data);
  2892. sc->stts_count = 0;
  2893. if (entries >= INT_MAX / sizeof(*sc->stts_data))
  2894. return AVERROR(ENOMEM);
  2895. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2896. unsigned int sample_duration;
  2897. unsigned int sample_count;
  2898. unsigned int min_entries = FFMIN(FFMAX(i + 1, 1024 * 1024), entries);
  2899. MOVStts *stts_data = av_fast_realloc(sc->stts_data, &alloc_size,
  2900. min_entries * sizeof(*sc->stts_data));
  2901. if (!stts_data) {
  2902. av_freep(&sc->stts_data);
  2903. sc->stts_count = 0;
  2904. return AVERROR(ENOMEM);
  2905. }
  2906. sc->stts_count = min_entries;
  2907. sc->stts_data = stts_data;
  2908. sample_count = avio_rb32(pb);
  2909. sample_duration = avio_rb32(pb);
  2910. sc->stts_data[i].count= sample_count;
  2911. sc->stts_data[i].duration= sample_duration;
  2912. av_log(c->fc, AV_LOG_TRACE, "sample_count=%u, sample_duration=%u\n",
  2913. sample_count, sample_duration);
  2914. /* STTS sample offsets are uint32 but some files store it as int32
  2915. * with negative values used to correct DTS delays.
  2916. There may be abnormally large values as well. */
  2917. if (sample_duration > c->max_stts_delta) {
  2918. // assume high delta is a correction if negative when cast as int32
  2919. int32_t delta_magnitude = (int32_t)sample_duration;
  2920. av_log(c->fc, AV_LOG_WARNING, "Too large sample offset %u in stts entry %u with count %u in st:%d. Clipping to 1.\n",
  2921. sample_duration, i, sample_count, st->index);
  2922. sc->stts_data[i].duration = 1;
  2923. corrected_dts += (delta_magnitude < 0 ? (int64_t)delta_magnitude : 1) * sample_count;
  2924. } else {
  2925. corrected_dts += sample_duration * sample_count;
  2926. }
  2927. current_dts += sc->stts_data[i].duration * sample_count;
  2928. if (current_dts > corrected_dts) {
  2929. int64_t drift = (current_dts - corrected_dts)/FFMAX(sample_count, 1);
  2930. uint32_t correction = (sc->stts_data[i].duration > drift) ? drift : sc->stts_data[i].duration - 1;
  2931. current_dts -= correction * sample_count;
  2932. sc->stts_data[i].duration -= correction;
  2933. }
  2934. duration+=(int64_t)sc->stts_data[i].duration*(uint64_t)sc->stts_data[i].count;
  2935. total_sample_count+=sc->stts_data[i].count;
  2936. }
  2937. sc->stts_count = i;
  2938. if (duration > 0 &&
  2939. duration <= INT64_MAX - sc->duration_for_fps &&
  2940. total_sample_count <= INT_MAX - sc->nb_frames_for_fps) {
  2941. sc->duration_for_fps += duration;
  2942. sc->nb_frames_for_fps += total_sample_count;
  2943. }
  2944. if (pb->eof_reached) {
  2945. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STTS atom\n");
  2946. return AVERROR_EOF;
  2947. }
  2948. st->nb_frames= total_sample_count;
  2949. if (duration)
  2950. st->duration= FFMIN(st->duration, duration);
  2951. // All samples have zero duration. They have higher chance be chose by
  2952. // mov_find_next_sample, which leads to seek again and again.
  2953. //
  2954. // It's AVERROR_INVALIDDATA actually, but such files exist in the wild.
  2955. // So only mark data stream as discarded for safety.
  2956. if (!duration && sc->stts_count &&
  2957. st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
  2958. av_log(c->fc, AV_LOG_WARNING,
  2959. "All samples in data stream index:id [%d:%d] have zero "
  2960. "duration, stream set to be discarded by default. Override "
  2961. "using AVStream->discard or -discard for ffmpeg command.\n",
  2962. st->index, sc->id);
  2963. st->discard = AVDISCARD_ALL;
  2964. }
  2965. sc->track_end = duration;
  2966. return 0;
  2967. }
  2968. static int mov_read_sdtp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2969. {
  2970. AVStream *st;
  2971. MOVStreamContext *sc;
  2972. int64_t i, entries;
  2973. if (c->fc->nb_streams < 1)
  2974. return 0;
  2975. st = c->fc->streams[c->fc->nb_streams - 1];
  2976. sc = st->priv_data;
  2977. avio_r8(pb); /* version */
  2978. avio_rb24(pb); /* flags */
  2979. entries = atom.size - 4;
  2980. av_log(c->fc, AV_LOG_TRACE, "track[%u].sdtp.entries = %" PRId64 "\n",
  2981. c->fc->nb_streams - 1, entries);
  2982. if (sc->sdtp_data)
  2983. av_log(c->fc, AV_LOG_WARNING, "Duplicated SDTP atom\n");
  2984. av_freep(&sc->sdtp_data);
  2985. sc->sdtp_count = 0;
  2986. sc->sdtp_data = av_malloc(entries);
  2987. if (!sc->sdtp_data)
  2988. return AVERROR(ENOMEM);
  2989. for (i = 0; i < entries && !pb->eof_reached; i++)
  2990. sc->sdtp_data[i] = avio_r8(pb);
  2991. sc->sdtp_count = i;
  2992. return 0;
  2993. }
  2994. static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
  2995. {
  2996. if (duration < 0) {
  2997. if (duration == INT_MIN) {
  2998. av_log(logctx, AV_LOG_WARNING, "mov_update_dts_shift(): dts_shift set to %d\n", INT_MAX);
  2999. duration++;
  3000. }
  3001. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  3002. }
  3003. }
  3004. static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3005. {
  3006. AVStream *st;
  3007. MOVStreamContext *sc;
  3008. unsigned int i, entries, ctts_count = 0;
  3009. if (c->fc->nb_streams < 1)
  3010. return 0;
  3011. st = c->fc->streams[c->fc->nb_streams-1];
  3012. sc = st->priv_data;
  3013. avio_r8(pb); /* version */
  3014. avio_rb24(pb); /* flags */
  3015. entries = avio_rb32(pb);
  3016. av_log(c->fc, AV_LOG_TRACE, "track[%u].ctts.entries = %u\n", c->fc->nb_streams - 1, entries);
  3017. if (!entries)
  3018. return 0;
  3019. if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
  3020. return AVERROR_INVALIDDATA;
  3021. av_freep(&sc->ctts_data);
  3022. sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size, entries * sizeof(*sc->ctts_data));
  3023. if (!sc->ctts_data)
  3024. return AVERROR(ENOMEM);
  3025. for (i = 0; i < entries && !pb->eof_reached; i++) {
  3026. int count = avio_rb32(pb);
  3027. int duration = avio_rb32(pb);
  3028. if (count <= 0) {
  3029. av_log(c->fc, AV_LOG_TRACE,
  3030. "ignoring CTTS entry with count=%d duration=%d\n",
  3031. count, duration);
  3032. continue;
  3033. }
  3034. add_ctts_entry(&sc->ctts_data, &ctts_count, &sc->ctts_allocated_size,
  3035. count, duration);
  3036. av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
  3037. count, duration);
  3038. if (FFNABS(duration) < -(1<<28) && i+2<entries) {
  3039. av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
  3040. av_freep(&sc->ctts_data);
  3041. sc->ctts_count = 0;
  3042. return 0;
  3043. }
  3044. if (i+2<entries)
  3045. mov_update_dts_shift(sc, duration, c->fc);
  3046. }
  3047. sc->ctts_count = ctts_count;
  3048. if (pb->eof_reached) {
  3049. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted CTTS atom\n");
  3050. return AVERROR_EOF;
  3051. }
  3052. av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
  3053. return 0;
  3054. }
  3055. static int mov_read_sgpd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3056. {
  3057. AVStream *st;
  3058. MOVStreamContext *sc;
  3059. uint8_t version;
  3060. uint32_t grouping_type;
  3061. uint32_t default_length;
  3062. av_unused uint32_t default_group_description_index;
  3063. uint32_t entry_count;
  3064. if (c->fc->nb_streams < 1)
  3065. return 0;
  3066. st = c->fc->streams[c->fc->nb_streams - 1];
  3067. sc = st->priv_data;
  3068. version = avio_r8(pb); /* version */
  3069. avio_rb24(pb); /* flags */
  3070. grouping_type = avio_rl32(pb);
  3071. /*
  3072. * This function only supports "sync" boxes, but the code is able to parse
  3073. * other boxes (such as "tscl", "tsas" and "stsa")
  3074. */
  3075. if (grouping_type != MKTAG('s','y','n','c'))
  3076. return 0;
  3077. default_length = version >= 1 ? avio_rb32(pb) : 0;
  3078. default_group_description_index = version >= 2 ? avio_rb32(pb) : 0;
  3079. entry_count = avio_rb32(pb);
  3080. av_freep(&sc->sgpd_sync);
  3081. sc->sgpd_sync_count = entry_count;
  3082. sc->sgpd_sync = av_calloc(entry_count, sizeof(*sc->sgpd_sync));
  3083. if (!sc->sgpd_sync)
  3084. return AVERROR(ENOMEM);
  3085. for (uint32_t i = 0; i < entry_count && !pb->eof_reached; i++) {
  3086. uint32_t description_length = default_length;
  3087. if (version >= 1 && default_length == 0)
  3088. description_length = avio_rb32(pb);
  3089. if (grouping_type == MKTAG('s','y','n','c')) {
  3090. const uint8_t nal_unit_type = avio_r8(pb) & 0x3f;
  3091. sc->sgpd_sync[i] = nal_unit_type;
  3092. description_length -= 1;
  3093. }
  3094. avio_skip(pb, description_length);
  3095. }
  3096. if (pb->eof_reached) {
  3097. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SGPD atom\n");
  3098. return AVERROR_EOF;
  3099. }
  3100. return 0;
  3101. }
  3102. static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  3103. {
  3104. AVStream *st;
  3105. MOVStreamContext *sc;
  3106. unsigned int i, entries;
  3107. uint8_t version;
  3108. uint32_t grouping_type;
  3109. MOVSbgp *table, **tablep;
  3110. int *table_count;
  3111. if (c->fc->nb_streams < 1)
  3112. return 0;
  3113. st = c->fc->streams[c->fc->nb_streams-1];
  3114. sc = st->priv_data;
  3115. version = avio_r8(pb); /* version */
  3116. avio_rb24(pb); /* flags */
  3117. grouping_type = avio_rl32(pb);
  3118. if (grouping_type == MKTAG('r','a','p',' ')) {
  3119. tablep = &sc->rap_group;
  3120. table_count = &sc->rap_group_count;
  3121. } else if (grouping_type == MKTAG('s','y','n','c')) {
  3122. tablep = &sc->sync_group;
  3123. table_count = &sc->sync_group_count;
  3124. } else {
  3125. return 0;
  3126. }
  3127. if (version == 1)
  3128. avio_rb32(pb); /* grouping_type_parameter */
  3129. entries = avio_rb32(pb);
  3130. if (!entries)
  3131. return 0;
  3132. if (*tablep)
  3133. av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP %s atom\n", av_fourcc2str(grouping_type));
  3134. av_freep(tablep);
  3135. table = av_malloc_array(entries, sizeof(*table));
  3136. if (!table)
  3137. return AVERROR(ENOMEM);
  3138. *tablep = table;
  3139. for (i = 0; i < entries && !pb->eof_reached; i++) {
  3140. table[i].count = avio_rb32(pb); /* sample_count */
  3141. table[i].index = avio_rb32(pb); /* group_description_index */
  3142. }
  3143. *table_count = i;
  3144. if (pb->eof_reached) {
  3145. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SBGP atom\n");
  3146. return AVERROR_EOF;
  3147. }
  3148. return 0;
  3149. }
  3150. /**
  3151. * Get ith edit list entry (media time, duration).
  3152. */
  3153. static int get_edit_list_entry(MOVContext *mov,
  3154. const MOVStreamContext *msc,
  3155. unsigned int edit_list_index,
  3156. int64_t *edit_list_media_time,
  3157. int64_t *edit_list_duration,
  3158. int64_t global_timescale)
  3159. {
  3160. if (edit_list_index == msc->elst_count) {
  3161. return 0;
  3162. }
  3163. *edit_list_media_time = msc->elst_data[edit_list_index].time;
  3164. *edit_list_duration = msc->elst_data[edit_list_index].duration;
  3165. /* duration is in global timescale units;convert to msc timescale */
  3166. if (global_timescale == 0) {
  3167. avpriv_request_sample(mov->fc, "Support for mvhd.timescale = 0 with editlists");
  3168. return 0;
  3169. }
  3170. *edit_list_duration = av_rescale(*edit_list_duration, msc->time_scale,
  3171. global_timescale);
  3172. return 1;
  3173. }
  3174. /**
  3175. * Find the closest previous frame to the timestamp_pts, in e_old index
  3176. * entries. Searching for just any frame / just key frames can be controlled by
  3177. * last argument 'flag'.
  3178. * Note that if ctts_data is not NULL, we will always search for a key frame
  3179. * irrespective of the value of 'flag'. If we don't find any keyframe, we will
  3180. * return the first frame of the video.
  3181. *
  3182. * Here the timestamp_pts is considered to be a presentation timestamp and
  3183. * the timestamp of index entries are considered to be decoding timestamps.
  3184. *
  3185. * Returns 0 if successful in finding a frame, else returns -1.
  3186. * Places the found index corresponding output arg.
  3187. *
  3188. * If ctts_old is not NULL, then refines the searched entry by searching
  3189. * backwards from the found timestamp, to find the frame with correct PTS.
  3190. *
  3191. * Places the found ctts_index and ctts_sample in corresponding output args.
  3192. */
  3193. static int find_prev_closest_index(AVStream *st,
  3194. AVIndexEntry *e_old,
  3195. int nb_old,
  3196. MOVCtts* ctts_data,
  3197. int64_t ctts_count,
  3198. int64_t timestamp_pts,
  3199. int flag,
  3200. int64_t* index,
  3201. int64_t* ctts_index,
  3202. int64_t* ctts_sample)
  3203. {
  3204. MOVStreamContext *msc = st->priv_data;
  3205. FFStream *const sti = ffstream(st);
  3206. AVIndexEntry *e_keep = sti->index_entries;
  3207. int nb_keep = sti->nb_index_entries;
  3208. int64_t i = 0;
  3209. int64_t index_ctts_count;
  3210. av_assert0(index);
  3211. // If dts_shift > 0, then all the index timestamps will have to be offset by
  3212. // at least dts_shift amount to obtain PTS.
  3213. // Hence we decrement the searched timestamp_pts by dts_shift to find the closest index element.
  3214. if (msc->dts_shift > 0) {
  3215. timestamp_pts -= msc->dts_shift;
  3216. }
  3217. sti->index_entries = e_old;
  3218. sti->nb_index_entries = nb_old;
  3219. *index = av_index_search_timestamp(st, timestamp_pts, flag | AVSEEK_FLAG_BACKWARD);
  3220. // Keep going backwards in the index entries until the timestamp is the same.
  3221. if (*index >= 0) {
  3222. for (i = *index; i > 0 && e_old[i].timestamp == e_old[i - 1].timestamp;
  3223. i--) {
  3224. if ((flag & AVSEEK_FLAG_ANY) ||
  3225. (e_old[i - 1].flags & AVINDEX_KEYFRAME)) {
  3226. *index = i - 1;
  3227. }
  3228. }
  3229. }
  3230. // If we have CTTS then refine the search, by searching backwards over PTS
  3231. // computed by adding corresponding CTTS durations to index timestamps.
  3232. if (ctts_data && *index >= 0) {
  3233. av_assert0(ctts_index);
  3234. av_assert0(ctts_sample);
  3235. // Find out the ctts_index for the found frame.
  3236. *ctts_index = 0;
  3237. *ctts_sample = 0;
  3238. for (index_ctts_count = 0; index_ctts_count < *index; index_ctts_count++) {
  3239. if (*ctts_index < ctts_count) {
  3240. (*ctts_sample)++;
  3241. if (ctts_data[*ctts_index].count == *ctts_sample) {
  3242. (*ctts_index)++;
  3243. *ctts_sample = 0;
  3244. }
  3245. }
  3246. }
  3247. while (*index >= 0 && (*ctts_index) >= 0 && (*ctts_index) < ctts_count) {
  3248. // Find a "key frame" with PTS <= timestamp_pts (So that we can decode B-frames correctly).
  3249. // No need to add dts_shift to the timestamp here becase timestamp_pts has already been
  3250. // compensated by dts_shift above.
  3251. if ((e_old[*index].timestamp + ctts_data[*ctts_index].duration) <= timestamp_pts &&
  3252. (e_old[*index].flags & AVINDEX_KEYFRAME)) {
  3253. break;
  3254. }
  3255. (*index)--;
  3256. if (*ctts_sample == 0) {
  3257. (*ctts_index)--;
  3258. if (*ctts_index >= 0)
  3259. *ctts_sample = ctts_data[*ctts_index].count - 1;
  3260. } else {
  3261. (*ctts_sample)--;
  3262. }
  3263. }
  3264. }
  3265. /* restore AVStream state*/
  3266. sti->index_entries = e_keep;
  3267. sti->nb_index_entries = nb_keep;
  3268. return *index >= 0 ? 0 : -1;
  3269. }
  3270. /**
  3271. * Add index entry with the given values, to the end of ffstream(st)->index_entries.
  3272. * Returns the new size ffstream(st)->index_entries if successful, else returns -1.
  3273. *
  3274. * This function is similar to ff_add_index_entry in libavformat/utils.c
  3275. * except that here we are always unconditionally adding an index entry to
  3276. * the end, instead of searching the entries list and skipping the add if
  3277. * there is an existing entry with the same timestamp.
  3278. * This is needed because the mov_fix_index calls this func with the same
  3279. * unincremented timestamp for successive discarded frames.
  3280. */
  3281. static int64_t add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
  3282. int size, int distance, int flags)
  3283. {
  3284. FFStream *const sti = ffstream(st);
  3285. AVIndexEntry *entries, *ie;
  3286. int64_t index = -1;
  3287. const size_t min_size_needed = (sti->nb_index_entries + 1) * sizeof(AVIndexEntry);
  3288. // Double the allocation each time, to lower memory fragmentation.
  3289. // Another difference from ff_add_index_entry function.
  3290. const size_t requested_size =
  3291. min_size_needed > sti->index_entries_allocated_size ?
  3292. FFMAX(min_size_needed, 2 * sti->index_entries_allocated_size) :
  3293. min_size_needed;
  3294. if (sti->nb_index_entries + 1U >= UINT_MAX / sizeof(AVIndexEntry))
  3295. return -1;
  3296. entries = av_fast_realloc(sti->index_entries,
  3297. &sti->index_entries_allocated_size,
  3298. requested_size);
  3299. if (!entries)
  3300. return -1;
  3301. sti->index_entries = entries;
  3302. index = sti->nb_index_entries++;
  3303. ie= &entries[index];
  3304. ie->pos = pos;
  3305. ie->timestamp = timestamp;
  3306. ie->min_distance= distance;
  3307. ie->size= size;
  3308. ie->flags = flags;
  3309. return index;
  3310. }
  3311. /**
  3312. * Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size, end_index)
  3313. * by subtracting end_ts successively by the amounts given in frame_duration_buffer.
  3314. */
  3315. static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_ts,
  3316. int64_t* frame_duration_buffer,
  3317. int frame_duration_buffer_size) {
  3318. FFStream *const sti = ffstream(st);
  3319. int i = 0;
  3320. av_assert0(end_index >= 0 && end_index <= sti->nb_index_entries);
  3321. for (i = 0; i < frame_duration_buffer_size; i++) {
  3322. end_ts -= frame_duration_buffer[frame_duration_buffer_size - 1 - i];
  3323. sti->index_entries[end_index - 1 - i].timestamp = end_ts;
  3324. }
  3325. }
  3326. /**
  3327. * Append a new ctts entry to ctts_data.
  3328. * Returns the new ctts_count if successful, else returns -1.
  3329. */
  3330. static int64_t add_ctts_entry(MOVCtts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
  3331. int count, int duration)
  3332. {
  3333. MOVCtts *ctts_buf_new;
  3334. const size_t min_size_needed = (*ctts_count + 1) * sizeof(MOVCtts);
  3335. const size_t requested_size =
  3336. min_size_needed > *allocated_size ?
  3337. FFMAX(min_size_needed, 2 * (*allocated_size)) :
  3338. min_size_needed;
  3339. if ((unsigned)(*ctts_count) >= UINT_MAX / sizeof(MOVCtts) - 1)
  3340. return -1;
  3341. ctts_buf_new = av_fast_realloc(*ctts_data, allocated_size, requested_size);
  3342. if (!ctts_buf_new)
  3343. return -1;
  3344. *ctts_data = ctts_buf_new;
  3345. ctts_buf_new[*ctts_count].count = count;
  3346. ctts_buf_new[*ctts_count].duration = duration;
  3347. *ctts_count = (*ctts_count) + 1;
  3348. return *ctts_count;
  3349. }
  3350. #define MAX_REORDER_DELAY 16
  3351. static void mov_estimate_video_delay(MOVContext *c, AVStream* st)
  3352. {
  3353. MOVStreamContext *msc = st->priv_data;
  3354. FFStream *const sti = ffstream(st);
  3355. int ctts_ind = 0;
  3356. int ctts_sample = 0;
  3357. int64_t pts_buf[MAX_REORDER_DELAY + 1]; // Circular buffer to sort pts.
  3358. int buf_start = 0;
  3359. int j, r, num_swaps;
  3360. for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
  3361. pts_buf[j] = INT64_MIN;
  3362. if (st->codecpar->video_delay <= 0 && msc->ctts_data &&
  3363. st->codecpar->codec_id == AV_CODEC_ID_H264) {
  3364. st->codecpar->video_delay = 0;
  3365. for (int ind = 0; ind < sti->nb_index_entries && ctts_ind < msc->ctts_count; ++ind) {
  3366. // Point j to the last elem of the buffer and insert the current pts there.
  3367. j = buf_start;
  3368. buf_start = (buf_start + 1);
  3369. if (buf_start == MAX_REORDER_DELAY + 1)
  3370. buf_start = 0;
  3371. pts_buf[j] = sti->index_entries[ind].timestamp + msc->ctts_data[ctts_ind].duration;
  3372. // The timestamps that are already in the sorted buffer, and are greater than the
  3373. // current pts, are exactly the timestamps that need to be buffered to output PTS
  3374. // in correct sorted order.
  3375. // Hence the video delay (which is the buffer size used to sort DTS and output PTS),
  3376. // can be computed as the maximum no. of swaps any particular timestamp needs to
  3377. // go through, to keep this buffer in sorted order.
  3378. num_swaps = 0;
  3379. while (j != buf_start) {
  3380. r = j - 1;
  3381. if (r < 0) r = MAX_REORDER_DELAY;
  3382. if (pts_buf[j] < pts_buf[r]) {
  3383. FFSWAP(int64_t, pts_buf[j], pts_buf[r]);
  3384. ++num_swaps;
  3385. } else {
  3386. break;
  3387. }
  3388. j = r;
  3389. }
  3390. st->codecpar->video_delay = FFMAX(st->codecpar->video_delay, num_swaps);
  3391. ctts_sample++;
  3392. if (ctts_sample == msc->ctts_data[ctts_ind].count) {
  3393. ctts_ind++;
  3394. ctts_sample = 0;
  3395. }
  3396. }
  3397. av_log(c->fc, AV_LOG_DEBUG, "Setting codecpar->delay to %d for stream st: %d\n",
  3398. st->codecpar->video_delay, st->index);
  3399. }
  3400. }
  3401. static void mov_current_sample_inc(MOVStreamContext *sc)
  3402. {
  3403. sc->current_sample++;
  3404. sc->current_index++;
  3405. if (sc->index_ranges &&
  3406. sc->current_index >= sc->current_index_range->end &&
  3407. sc->current_index_range->end) {
  3408. sc->current_index_range++;
  3409. sc->current_index = sc->current_index_range->start;
  3410. }
  3411. }
  3412. static void mov_current_sample_dec(MOVStreamContext *sc)
  3413. {
  3414. sc->current_sample--;
  3415. sc->current_index--;
  3416. if (sc->index_ranges &&
  3417. sc->current_index < sc->current_index_range->start &&
  3418. sc->current_index_range > sc->index_ranges) {
  3419. sc->current_index_range--;
  3420. sc->current_index = sc->current_index_range->end - 1;
  3421. }
  3422. }
  3423. static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
  3424. {
  3425. int64_t range_size;
  3426. sc->current_sample = current_sample;
  3427. sc->current_index = current_sample;
  3428. if (!sc->index_ranges) {
  3429. return;
  3430. }
  3431. for (sc->current_index_range = sc->index_ranges;
  3432. sc->current_index_range->end;
  3433. sc->current_index_range++) {
  3434. range_size = sc->current_index_range->end - sc->current_index_range->start;
  3435. if (range_size > current_sample) {
  3436. sc->current_index = sc->current_index_range->start + current_sample;
  3437. break;
  3438. }
  3439. current_sample -= range_size;
  3440. }
  3441. }
  3442. /**
  3443. * Fix ffstream(st)->index_entries, so that it contains only the entries (and the entries
  3444. * which are needed to decode them) that fall in the edit list time ranges.
  3445. * Also fixes the timestamps of the index entries to match the timeline
  3446. * specified the edit lists.
  3447. */
  3448. static void mov_fix_index(MOVContext *mov, AVStream *st)
  3449. {
  3450. MOVStreamContext *msc = st->priv_data;
  3451. FFStream *const sti = ffstream(st);
  3452. AVIndexEntry *e_old = sti->index_entries;
  3453. int nb_old = sti->nb_index_entries;
  3454. const AVIndexEntry *e_old_end = e_old + nb_old;
  3455. const AVIndexEntry *current = NULL;
  3456. MOVCtts *ctts_data_old = msc->ctts_data;
  3457. int64_t ctts_index_old = 0;
  3458. int64_t ctts_sample_old = 0;
  3459. int64_t ctts_count_old = msc->ctts_count;
  3460. int64_t edit_list_media_time = 0;
  3461. int64_t edit_list_duration = 0;
  3462. int64_t frame_duration = 0;
  3463. int64_t edit_list_dts_counter = 0;
  3464. int64_t edit_list_dts_entry_end = 0;
  3465. int64_t edit_list_start_ctts_sample = 0;
  3466. int64_t curr_cts;
  3467. int64_t curr_ctts = 0;
  3468. int64_t empty_edits_sum_duration = 0;
  3469. int64_t edit_list_index = 0;
  3470. int64_t index;
  3471. int flags;
  3472. int64_t start_dts = 0;
  3473. int64_t edit_list_start_encountered = 0;
  3474. int64_t search_timestamp = 0;
  3475. int64_t* frame_duration_buffer = NULL;
  3476. int num_discarded_begin = 0;
  3477. int first_non_zero_audio_edit = -1;
  3478. int packet_skip_samples = 0;
  3479. MOVIndexRange *current_index_range = NULL;
  3480. int found_keyframe_after_edit = 0;
  3481. int found_non_empty_edit = 0;
  3482. if (!msc->elst_data || msc->elst_count <= 0 || nb_old <= 0) {
  3483. return;
  3484. }
  3485. // allocate the index ranges array
  3486. msc->index_ranges = av_malloc_array(msc->elst_count + 1,
  3487. sizeof(msc->index_ranges[0]));
  3488. if (!msc->index_ranges) {
  3489. av_log(mov->fc, AV_LOG_ERROR, "Cannot allocate index ranges buffer\n");
  3490. return;
  3491. }
  3492. msc->current_index_range = msc->index_ranges;
  3493. // Clean AVStream from traces of old index
  3494. sti->index_entries = NULL;
  3495. sti->index_entries_allocated_size = 0;
  3496. sti->nb_index_entries = 0;
  3497. // Clean ctts fields of MOVStreamContext
  3498. msc->ctts_data = NULL;
  3499. msc->ctts_count = 0;
  3500. msc->ctts_index = 0;
  3501. msc->ctts_sample = 0;
  3502. msc->ctts_allocated_size = 0;
  3503. // Reinitialize min_corrected_pts so that it can be computed again.
  3504. msc->min_corrected_pts = -1;
  3505. // If the dts_shift is positive (in case of negative ctts values in mov),
  3506. // then negate the DTS by dts_shift
  3507. if (msc->dts_shift > 0) {
  3508. edit_list_dts_entry_end -= msc->dts_shift;
  3509. av_log(mov->fc, AV_LOG_DEBUG, "Shifting DTS by %d because of negative CTTS.\n", msc->dts_shift);
  3510. }
  3511. start_dts = edit_list_dts_entry_end;
  3512. while (get_edit_list_entry(mov, msc, edit_list_index, &edit_list_media_time,
  3513. &edit_list_duration, mov->time_scale)) {
  3514. av_log(mov->fc, AV_LOG_DEBUG, "Processing st: %d, edit list %"PRId64" - media time: %"PRId64", duration: %"PRId64"\n",
  3515. st->index, edit_list_index, edit_list_media_time, edit_list_duration);
  3516. edit_list_index++;
  3517. edit_list_dts_counter = edit_list_dts_entry_end;
  3518. edit_list_dts_entry_end += edit_list_duration;
  3519. num_discarded_begin = 0;
  3520. if (!found_non_empty_edit && edit_list_media_time == -1) {
  3521. empty_edits_sum_duration += edit_list_duration;
  3522. continue;
  3523. }
  3524. found_non_empty_edit = 1;
  3525. // If we encounter a non-negative edit list reset the skip_samples/start_pad fields and set them
  3526. // according to the edit list below.
  3527. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  3528. if (first_non_zero_audio_edit < 0) {
  3529. first_non_zero_audio_edit = 1;
  3530. } else {
  3531. first_non_zero_audio_edit = 0;
  3532. }
  3533. if (first_non_zero_audio_edit > 0)
  3534. sti->skip_samples = msc->start_pad = 0;
  3535. }
  3536. // While reordering frame index according to edit list we must handle properly
  3537. // the scenario when edit list entry starts from none key frame.
  3538. // We find closest previous key frame and preserve it and consequent frames in index.
  3539. // All frames which are outside edit list entry time boundaries will be dropped after decoding.
  3540. search_timestamp = edit_list_media_time;
  3541. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  3542. // Audio decoders like AAC need need a decoder delay samples previous to the current sample,
  3543. // to correctly decode this frame. Hence for audio we seek to a frame 1 sec. before the
  3544. // edit_list_media_time to cover the decoder delay.
  3545. search_timestamp = FFMAX(search_timestamp - msc->time_scale, e_old[0].timestamp);
  3546. }
  3547. if (find_prev_closest_index(st, e_old, nb_old, ctts_data_old, ctts_count_old, search_timestamp, 0,
  3548. &index, &ctts_index_old, &ctts_sample_old) < 0) {
  3549. av_log(mov->fc, AV_LOG_WARNING,
  3550. "st: %d edit list: %"PRId64" Missing key frame while searching for timestamp: %"PRId64"\n",
  3551. st->index, edit_list_index, search_timestamp);
  3552. if (find_prev_closest_index(st, e_old, nb_old, ctts_data_old, ctts_count_old, search_timestamp, AVSEEK_FLAG_ANY,
  3553. &index, &ctts_index_old, &ctts_sample_old) < 0) {
  3554. av_log(mov->fc, AV_LOG_WARNING,
  3555. "st: %d edit list %"PRId64" Cannot find an index entry before timestamp: %"PRId64".\n",
  3556. st->index, edit_list_index, search_timestamp);
  3557. index = 0;
  3558. ctts_index_old = 0;
  3559. ctts_sample_old = 0;
  3560. }
  3561. }
  3562. current = e_old + index;
  3563. edit_list_start_ctts_sample = ctts_sample_old;
  3564. // Iterate over index and arrange it according to edit list
  3565. edit_list_start_encountered = 0;
  3566. found_keyframe_after_edit = 0;
  3567. for (; current < e_old_end; current++, index++) {
  3568. // check if frame outside edit list mark it for discard
  3569. frame_duration = (current + 1 < e_old_end) ?
  3570. ((current + 1)->timestamp - current->timestamp) : edit_list_duration;
  3571. flags = current->flags;
  3572. // frames (pts) before or after edit list
  3573. curr_cts = current->timestamp + msc->dts_shift;
  3574. curr_ctts = 0;
  3575. if (ctts_data_old && ctts_index_old < ctts_count_old) {
  3576. curr_ctts = ctts_data_old[ctts_index_old].duration;
  3577. av_log(mov->fc, AV_LOG_TRACE, "stts: %"PRId64" ctts: %"PRId64", ctts_index: %"PRId64", ctts_count: %"PRId64"\n",
  3578. curr_cts, curr_ctts, ctts_index_old, ctts_count_old);
  3579. curr_cts += curr_ctts;
  3580. ctts_sample_old++;
  3581. if (ctts_sample_old == ctts_data_old[ctts_index_old].count) {
  3582. if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
  3583. &msc->ctts_allocated_size,
  3584. ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
  3585. ctts_data_old[ctts_index_old].duration) == -1) {
  3586. av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
  3587. ctts_index_old,
  3588. ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
  3589. ctts_data_old[ctts_index_old].duration);
  3590. break;
  3591. }
  3592. ctts_index_old++;
  3593. ctts_sample_old = 0;
  3594. edit_list_start_ctts_sample = 0;
  3595. }
  3596. }
  3597. if (curr_cts < edit_list_media_time || curr_cts >= (edit_list_duration + edit_list_media_time)) {
  3598. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->codec_id != AV_CODEC_ID_VORBIS &&
  3599. curr_cts < edit_list_media_time && curr_cts + frame_duration > edit_list_media_time &&
  3600. first_non_zero_audio_edit > 0) {
  3601. packet_skip_samples = edit_list_media_time - curr_cts;
  3602. sti->skip_samples += packet_skip_samples;
  3603. // Shift the index entry timestamp by packet_skip_samples to be correct.
  3604. edit_list_dts_counter -= packet_skip_samples;
  3605. if (edit_list_start_encountered == 0) {
  3606. edit_list_start_encountered = 1;
  3607. // Make timestamps strictly monotonically increasing for audio, by rewriting timestamps for
  3608. // discarded packets.
  3609. if (frame_duration_buffer) {
  3610. fix_index_entry_timestamps(st, sti->nb_index_entries, edit_list_dts_counter,
  3611. frame_duration_buffer, num_discarded_begin);
  3612. av_freep(&frame_duration_buffer);
  3613. }
  3614. }
  3615. av_log(mov->fc, AV_LOG_DEBUG, "skip %d audio samples from curr_cts: %"PRId64"\n", packet_skip_samples, curr_cts);
  3616. } else {
  3617. flags |= AVINDEX_DISCARD_FRAME;
  3618. av_log(mov->fc, AV_LOG_DEBUG, "drop a frame at curr_cts: %"PRId64" @ %"PRId64"\n", curr_cts, index);
  3619. if (edit_list_start_encountered == 0) {
  3620. num_discarded_begin++;
  3621. frame_duration_buffer = av_realloc(frame_duration_buffer,
  3622. num_discarded_begin * sizeof(int64_t));
  3623. if (!frame_duration_buffer) {
  3624. av_log(mov->fc, AV_LOG_ERROR, "Cannot reallocate frame duration buffer\n");
  3625. break;
  3626. }
  3627. frame_duration_buffer[num_discarded_begin - 1] = frame_duration;
  3628. // Increment skip_samples for the first non-zero audio edit list
  3629. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  3630. first_non_zero_audio_edit > 0 && st->codecpar->codec_id != AV_CODEC_ID_VORBIS) {
  3631. sti->skip_samples += frame_duration;
  3632. }
  3633. }
  3634. }
  3635. } else {
  3636. if (msc->min_corrected_pts < 0) {
  3637. msc->min_corrected_pts = edit_list_dts_counter + curr_ctts + msc->dts_shift;
  3638. } else {
  3639. msc->min_corrected_pts = FFMIN(msc->min_corrected_pts, edit_list_dts_counter + curr_ctts + msc->dts_shift);
  3640. }
  3641. if (edit_list_start_encountered == 0) {
  3642. edit_list_start_encountered = 1;
  3643. // Make timestamps strictly monotonically increasing by rewriting timestamps for
  3644. // discarded packets.
  3645. if (frame_duration_buffer) {
  3646. fix_index_entry_timestamps(st, sti->nb_index_entries, edit_list_dts_counter,
  3647. frame_duration_buffer, num_discarded_begin);
  3648. av_freep(&frame_duration_buffer);
  3649. }
  3650. }
  3651. }
  3652. if (add_index_entry(st, current->pos, edit_list_dts_counter, current->size,
  3653. current->min_distance, flags) == -1) {
  3654. av_log(mov->fc, AV_LOG_ERROR, "Cannot add index entry\n");
  3655. break;
  3656. }
  3657. // Update the index ranges array
  3658. if (!current_index_range || index != current_index_range->end) {
  3659. current_index_range = current_index_range ? current_index_range + 1
  3660. : msc->index_ranges;
  3661. current_index_range->start = index;
  3662. }
  3663. current_index_range->end = index + 1;
  3664. // Only start incrementing DTS in frame_duration amounts, when we encounter a frame in edit list.
  3665. if (edit_list_start_encountered > 0) {
  3666. edit_list_dts_counter = edit_list_dts_counter + frame_duration;
  3667. }
  3668. // Break when found first key frame after edit entry completion
  3669. if ((curr_cts + frame_duration >= (edit_list_duration + edit_list_media_time)) &&
  3670. ((flags & AVINDEX_KEYFRAME) || ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)))) {
  3671. if (ctts_data_old) {
  3672. // If we have CTTS and this is the first keyframe after edit elist,
  3673. // wait for one more, because there might be trailing B-frames after this I-frame
  3674. // that do belong to the edit.
  3675. if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO && found_keyframe_after_edit == 0) {
  3676. found_keyframe_after_edit = 1;
  3677. continue;
  3678. }
  3679. if (ctts_sample_old != 0) {
  3680. if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
  3681. &msc->ctts_allocated_size,
  3682. ctts_sample_old - edit_list_start_ctts_sample,
  3683. ctts_data_old[ctts_index_old].duration) == -1) {
  3684. av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
  3685. ctts_index_old, ctts_sample_old - edit_list_start_ctts_sample,
  3686. ctts_data_old[ctts_index_old].duration);
  3687. break;
  3688. }
  3689. }
  3690. }
  3691. break;
  3692. }
  3693. }
  3694. }
  3695. // If there are empty edits, then msc->min_corrected_pts might be positive
  3696. // intentionally. So we subtract the sum duration of emtpy edits here.
  3697. msc->min_corrected_pts -= empty_edits_sum_duration;
  3698. // If the minimum pts turns out to be greater than zero after fixing the index, then we subtract the
  3699. // dts by that amount to make the first pts zero.
  3700. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  3701. if (msc->min_corrected_pts > 0) {
  3702. av_log(mov->fc, AV_LOG_DEBUG, "Offset DTS by %"PRId64" to make first pts zero.\n", msc->min_corrected_pts);
  3703. for (int i = 0; i < sti->nb_index_entries; ++i)
  3704. sti->index_entries[i].timestamp -= msc->min_corrected_pts;
  3705. }
  3706. }
  3707. // Start time should be equal to zero or the duration of any empty edits.
  3708. st->start_time = empty_edits_sum_duration;
  3709. // Update av stream length, if it ends up shorter than the track's media duration
  3710. st->duration = FFMIN(st->duration, edit_list_dts_entry_end - start_dts);
  3711. msc->start_pad = sti->skip_samples;
  3712. // Free the old index and the old CTTS structures
  3713. av_free(e_old);
  3714. av_free(ctts_data_old);
  3715. av_freep(&frame_duration_buffer);
  3716. // Null terminate the index ranges array
  3717. current_index_range = current_index_range ? current_index_range + 1
  3718. : msc->index_ranges;
  3719. current_index_range->start = 0;
  3720. current_index_range->end = 0;
  3721. msc->current_index = msc->index_ranges[0].start;
  3722. }
  3723. static uint32_t get_sgpd_sync_index(const MOVStreamContext *sc, int nal_unit_type)
  3724. {
  3725. for (uint32_t i = 0; i < sc->sgpd_sync_count; i++)
  3726. if (sc->sgpd_sync[i] == HEVC_NAL_CRA_NUT)
  3727. return i + 1;
  3728. return 0;
  3729. }
  3730. static int build_open_gop_key_points(AVStream *st)
  3731. {
  3732. int k;
  3733. int sample_id = 0;
  3734. uint32_t cra_index;
  3735. MOVStreamContext *sc = st->priv_data;
  3736. if (st->codecpar->codec_id != AV_CODEC_ID_HEVC || !sc->sync_group_count)
  3737. return 0;
  3738. /* Build an unrolled index of the samples */
  3739. sc->sample_offsets_count = 0;
  3740. for (uint32_t i = 0; i < sc->ctts_count; i++) {
  3741. if (sc->ctts_data[i].count > INT_MAX - sc->sample_offsets_count)
  3742. return AVERROR(ENOMEM);
  3743. sc->sample_offsets_count += sc->ctts_data[i].count;
  3744. }
  3745. av_freep(&sc->sample_offsets);
  3746. sc->sample_offsets = av_calloc(sc->sample_offsets_count, sizeof(*sc->sample_offsets));
  3747. if (!sc->sample_offsets)
  3748. return AVERROR(ENOMEM);
  3749. k = 0;
  3750. for (uint32_t i = 0; i < sc->ctts_count; i++)
  3751. for (int j = 0; j < sc->ctts_data[i].count; j++)
  3752. sc->sample_offsets[k++] = sc->ctts_data[i].duration;
  3753. /* The following HEVC NAL type reveal the use of open GOP sync points
  3754. * (TODO: BLA types may also be concerned) */
  3755. cra_index = get_sgpd_sync_index(sc, HEVC_NAL_CRA_NUT); /* Clean Random Access */
  3756. if (!cra_index)
  3757. return 0;
  3758. /* Build a list of open-GOP key samples */
  3759. sc->open_key_samples_count = 0;
  3760. for (uint32_t i = 0; i < sc->sync_group_count; i++)
  3761. if (sc->sync_group[i].index == cra_index) {
  3762. if (sc->sync_group[i].count > INT_MAX - sc->open_key_samples_count)
  3763. return AVERROR(ENOMEM);
  3764. sc->open_key_samples_count += sc->sync_group[i].count;
  3765. }
  3766. av_freep(&sc->open_key_samples);
  3767. sc->open_key_samples = av_calloc(sc->open_key_samples_count, sizeof(*sc->open_key_samples));
  3768. if (!sc->open_key_samples)
  3769. return AVERROR(ENOMEM);
  3770. k = 0;
  3771. for (uint32_t i = 0; i < sc->sync_group_count; i++) {
  3772. const MOVSbgp *sg = &sc->sync_group[i];
  3773. if (sg->index == cra_index)
  3774. for (uint32_t j = 0; j < sg->count; j++)
  3775. sc->open_key_samples[k++] = sample_id;
  3776. if (sg->count > INT_MAX - sample_id)
  3777. return AVERROR_PATCHWELCOME;
  3778. sample_id += sg->count;
  3779. }
  3780. /* Identify the minimal time step between samples */
  3781. sc->min_sample_duration = UINT_MAX;
  3782. for (uint32_t i = 0; i < sc->stts_count; i++)
  3783. sc->min_sample_duration = FFMIN(sc->min_sample_duration, sc->stts_data[i].duration);
  3784. return 0;
  3785. }
  3786. static void mov_build_index(MOVContext *mov, AVStream *st)
  3787. {
  3788. MOVStreamContext *sc = st->priv_data;
  3789. FFStream *const sti = ffstream(st);
  3790. int64_t current_offset;
  3791. int64_t current_dts = 0;
  3792. unsigned int stts_index = 0;
  3793. unsigned int stsc_index = 0;
  3794. unsigned int stss_index = 0;
  3795. unsigned int stps_index = 0;
  3796. unsigned int i, j;
  3797. uint64_t stream_size = 0;
  3798. MOVCtts *ctts_data_old = sc->ctts_data;
  3799. unsigned int ctts_count_old = sc->ctts_count;
  3800. int ret = build_open_gop_key_points(st);
  3801. if (ret < 0)
  3802. return;
  3803. if (sc->elst_count) {
  3804. int i, edit_start_index = 0, multiple_edits = 0;
  3805. int64_t empty_duration = 0; // empty duration of the first edit list entry
  3806. int64_t start_time = 0; // start time of the media
  3807. for (i = 0; i < sc->elst_count; i++) {
  3808. const MOVElst *e = &sc->elst_data[i];
  3809. if (i == 0 && e->time == -1) {
  3810. /* if empty, the first entry is the start time of the stream
  3811. * relative to the presentation itself */
  3812. empty_duration = e->duration;
  3813. edit_start_index = 1;
  3814. } else if (i == edit_start_index && e->time >= 0) {
  3815. start_time = e->time;
  3816. } else {
  3817. multiple_edits = 1;
  3818. }
  3819. }
  3820. if (multiple_edits && !mov->advanced_editlist) {
  3821. if (mov->advanced_editlist_autodisabled)
  3822. av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
  3823. "not supported in fragmented MP4 files\n");
  3824. else
  3825. av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
  3826. "Use -advanced_editlist to correctly decode otherwise "
  3827. "a/v desync might occur\n");
  3828. }
  3829. /* adjust first dts according to edit list */
  3830. if ((empty_duration || start_time) && mov->time_scale > 0) {
  3831. if (empty_duration)
  3832. empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
  3833. if (av_sat_sub64(start_time, empty_duration) != start_time - (uint64_t)empty_duration)
  3834. av_log(mov->fc, AV_LOG_WARNING, "start_time - empty_duration is not representable\n");
  3835. sc->time_offset = start_time - (uint64_t)empty_duration;
  3836. sc->min_corrected_pts = start_time;
  3837. if (!mov->advanced_editlist)
  3838. current_dts = -sc->time_offset;
  3839. }
  3840. if (!multiple_edits && !mov->advanced_editlist &&
  3841. st->codecpar->codec_id == AV_CODEC_ID_AAC && start_time > 0)
  3842. sc->start_pad = start_time;
  3843. }
  3844. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  3845. if (!(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  3846. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  3847. unsigned int current_sample = 0;
  3848. unsigned int stts_sample = 0;
  3849. unsigned int sample_size;
  3850. unsigned int distance = 0;
  3851. unsigned int rap_group_index = 0;
  3852. unsigned int rap_group_sample = 0;
  3853. int rap_group_present = sc->rap_group_count && sc->rap_group;
  3854. int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
  3855. current_dts -= sc->dts_shift;
  3856. if (!sc->sample_count || sti->nb_index_entries)
  3857. return;
  3858. if (sc->sample_count >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
  3859. return;
  3860. if (av_reallocp_array(&sti->index_entries,
  3861. sti->nb_index_entries + sc->sample_count,
  3862. sizeof(*sti->index_entries)) < 0) {
  3863. sti->nb_index_entries = 0;
  3864. return;
  3865. }
  3866. sti->index_entries_allocated_size = (sti->nb_index_entries + sc->sample_count) * sizeof(*sti->index_entries);
  3867. if (ctts_data_old) {
  3868. // Expand ctts entries such that we have a 1-1 mapping with samples
  3869. if (sc->sample_count >= UINT_MAX / sizeof(*sc->ctts_data))
  3870. return;
  3871. sc->ctts_count = 0;
  3872. sc->ctts_allocated_size = 0;
  3873. sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size,
  3874. sc->sample_count * sizeof(*sc->ctts_data));
  3875. if (!sc->ctts_data) {
  3876. av_free(ctts_data_old);
  3877. return;
  3878. }
  3879. memset((uint8_t*)(sc->ctts_data), 0, sc->ctts_allocated_size);
  3880. for (i = 0; i < ctts_count_old &&
  3881. sc->ctts_count < sc->sample_count; i++)
  3882. for (j = 0; j < ctts_data_old[i].count &&
  3883. sc->ctts_count < sc->sample_count; j++)
  3884. add_ctts_entry(&sc->ctts_data, &sc->ctts_count,
  3885. &sc->ctts_allocated_size, 1,
  3886. ctts_data_old[i].duration);
  3887. av_free(ctts_data_old);
  3888. }
  3889. for (i = 0; i < sc->chunk_count; i++) {
  3890. int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
  3891. current_offset = sc->chunk_offsets[i];
  3892. while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
  3893. i + 1 == sc->stsc_data[stsc_index + 1].first)
  3894. stsc_index++;
  3895. if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
  3896. sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
  3897. av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
  3898. sc->stsz_sample_size = sc->sample_size;
  3899. }
  3900. if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
  3901. av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
  3902. sc->stsz_sample_size = sc->sample_size;
  3903. }
  3904. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  3905. int keyframe = 0;
  3906. if (current_sample >= sc->sample_count) {
  3907. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  3908. return;
  3909. }
  3910. if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
  3911. keyframe = 1;
  3912. if (stss_index + 1 < sc->keyframe_count)
  3913. stss_index++;
  3914. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  3915. keyframe = 1;
  3916. if (stps_index + 1 < sc->stps_count)
  3917. stps_index++;
  3918. }
  3919. if (rap_group_present && rap_group_index < sc->rap_group_count) {
  3920. if (sc->rap_group[rap_group_index].index > 0)
  3921. keyframe = 1;
  3922. if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
  3923. rap_group_sample = 0;
  3924. rap_group_index++;
  3925. }
  3926. }
  3927. if (sc->keyframe_absent
  3928. && !sc->stps_count
  3929. && !rap_group_present
  3930. && (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
  3931. keyframe = 1;
  3932. if (keyframe)
  3933. distance = 0;
  3934. sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
  3935. if (current_offset > INT64_MAX - sample_size) {
  3936. av_log(mov->fc, AV_LOG_ERROR, "Current offset %"PRId64" or sample size %u is too large\n",
  3937. current_offset,
  3938. sample_size);
  3939. return;
  3940. }
  3941. if (sc->pseudo_stream_id == -1 ||
  3942. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  3943. AVIndexEntry *e;
  3944. if (sample_size > 0x3FFFFFFF) {
  3945. av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
  3946. return;
  3947. }
  3948. e = &sti->index_entries[sti->nb_index_entries++];
  3949. e->pos = current_offset;
  3950. e->timestamp = current_dts;
  3951. e->size = sample_size;
  3952. e->min_distance = distance;
  3953. e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
  3954. av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
  3955. "size %u, distance %u, keyframe %d\n", st->index, current_sample,
  3956. current_offset, current_dts, sample_size, distance, keyframe);
  3957. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->nb_index_entries < 100)
  3958. ff_rfps_add_frame(mov->fc, st, current_dts);
  3959. }
  3960. current_offset += sample_size;
  3961. stream_size += sample_size;
  3962. current_dts += sc->stts_data[stts_index].duration;
  3963. distance++;
  3964. stts_sample++;
  3965. current_sample++;
  3966. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  3967. stts_sample = 0;
  3968. stts_index++;
  3969. }
  3970. }
  3971. }
  3972. if (st->duration > 0)
  3973. st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
  3974. } else {
  3975. unsigned chunk_samples, total = 0;
  3976. if (!sc->chunk_count)
  3977. return;
  3978. // compute total chunk count
  3979. for (i = 0; i < sc->stsc_count; i++) {
  3980. unsigned count, chunk_count;
  3981. chunk_samples = sc->stsc_data[i].count;
  3982. if (i != sc->stsc_count - 1 &&
  3983. sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  3984. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  3985. return;
  3986. }
  3987. if (sc->samples_per_frame >= 160) { // gsm
  3988. count = chunk_samples / sc->samples_per_frame;
  3989. } else if (sc->samples_per_frame > 1) {
  3990. unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
  3991. count = (chunk_samples+samples-1) / samples;
  3992. } else {
  3993. count = (chunk_samples+1023) / 1024;
  3994. }
  3995. if (mov_stsc_index_valid(i, sc->stsc_count))
  3996. chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
  3997. else
  3998. chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
  3999. total += chunk_count * count;
  4000. }
  4001. av_log(mov->fc, AV_LOG_TRACE, "chunk count %u\n", total);
  4002. if (total >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
  4003. return;
  4004. if (av_reallocp_array(&sti->index_entries,
  4005. sti->nb_index_entries + total,
  4006. sizeof(*sti->index_entries)) < 0) {
  4007. sti->nb_index_entries = 0;
  4008. return;
  4009. }
  4010. sti->index_entries_allocated_size = (sti->nb_index_entries + total) * sizeof(*sti->index_entries);
  4011. // populate index
  4012. for (i = 0; i < sc->chunk_count; i++) {
  4013. current_offset = sc->chunk_offsets[i];
  4014. if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
  4015. i + 1 == sc->stsc_data[stsc_index + 1].first)
  4016. stsc_index++;
  4017. chunk_samples = sc->stsc_data[stsc_index].count;
  4018. while (chunk_samples > 0) {
  4019. AVIndexEntry *e;
  4020. unsigned size, samples;
  4021. if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
  4022. avpriv_request_sample(mov->fc,
  4023. "Zero bytes per frame, but %d samples per frame",
  4024. sc->samples_per_frame);
  4025. return;
  4026. }
  4027. if (sc->samples_per_frame >= 160) { // gsm
  4028. samples = sc->samples_per_frame;
  4029. size = sc->bytes_per_frame;
  4030. } else {
  4031. if (sc->samples_per_frame > 1) {
  4032. samples = FFMIN((1024 / sc->samples_per_frame)*
  4033. sc->samples_per_frame, chunk_samples);
  4034. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  4035. } else {
  4036. samples = FFMIN(1024, chunk_samples);
  4037. size = samples * sc->sample_size;
  4038. }
  4039. }
  4040. if (sti->nb_index_entries >= total) {
  4041. av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %u\n", total);
  4042. return;
  4043. }
  4044. if (size > 0x3FFFFFFF) {
  4045. av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
  4046. return;
  4047. }
  4048. e = &sti->index_entries[sti->nb_index_entries++];
  4049. e->pos = current_offset;
  4050. e->timestamp = current_dts;
  4051. e->size = size;
  4052. e->min_distance = 0;
  4053. e->flags = AVINDEX_KEYFRAME;
  4054. av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %u, offset %"PRIx64", dts %"PRId64", "
  4055. "size %u, duration %u\n", st->index, i, current_offset, current_dts,
  4056. size, samples);
  4057. current_offset += size;
  4058. current_dts += samples;
  4059. chunk_samples -= samples;
  4060. }
  4061. }
  4062. }
  4063. if (!mov->ignore_editlist && mov->advanced_editlist) {
  4064. // Fix index according to edit lists.
  4065. mov_fix_index(mov, st);
  4066. }
  4067. // Update start time of the stream.
  4068. if (st->start_time == AV_NOPTS_VALUE && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->nb_index_entries > 0) {
  4069. st->start_time = sti->index_entries[0].timestamp + sc->dts_shift;
  4070. if (sc->ctts_data) {
  4071. st->start_time += sc->ctts_data[0].duration;
  4072. }
  4073. }
  4074. mov_estimate_video_delay(mov, st);
  4075. }
  4076. static int test_same_origin(const char *src, const char *ref) {
  4077. char src_proto[64];
  4078. char ref_proto[64];
  4079. char src_auth[256];
  4080. char ref_auth[256];
  4081. char src_host[256];
  4082. char ref_host[256];
  4083. int src_port=-1;
  4084. int ref_port=-1;
  4085. av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
  4086. av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
  4087. if (strlen(src) == 0) {
  4088. return -1;
  4089. } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
  4090. strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
  4091. strlen(src_host) + 1 >= sizeof(src_host) ||
  4092. strlen(ref_host) + 1 >= sizeof(ref_host)) {
  4093. return 0;
  4094. } else if (strcmp(src_proto, ref_proto) ||
  4095. strcmp(src_auth, ref_auth) ||
  4096. strcmp(src_host, ref_host) ||
  4097. src_port != ref_port) {
  4098. return 0;
  4099. } else
  4100. return 1;
  4101. }
  4102. static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
  4103. {
  4104. /* try relative path, we do not try the absolute because it can leak information about our
  4105. system to an attacker */
  4106. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  4107. char filename[1025];
  4108. const char *src_path;
  4109. int i, l;
  4110. /* find a source dir */
  4111. src_path = strrchr(src, '/');
  4112. if (src_path)
  4113. src_path++;
  4114. else
  4115. src_path = src;
  4116. /* find a next level down to target */
  4117. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  4118. if (ref->path[l] == '/') {
  4119. if (i == ref->nlvl_to - 1)
  4120. break;
  4121. else
  4122. i++;
  4123. }
  4124. /* compose filename if next level down to target was found */
  4125. if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
  4126. memcpy(filename, src, src_path - src);
  4127. filename[src_path - src] = 0;
  4128. for (i = 1; i < ref->nlvl_from; i++)
  4129. av_strlcat(filename, "../", sizeof(filename));
  4130. av_strlcat(filename, ref->path + l + 1, sizeof(filename));
  4131. if (!c->use_absolute_path) {
  4132. int same_origin = test_same_origin(src, filename);
  4133. if (!same_origin) {
  4134. av_log(c->fc, AV_LOG_ERROR,
  4135. "Reference with mismatching origin, %s not tried for security reasons, "
  4136. "set demuxer option use_absolute_path to allow it anyway\n",
  4137. ref->path);
  4138. return AVERROR(ENOENT);
  4139. }
  4140. if (strstr(ref->path + l + 1, "..") ||
  4141. strstr(ref->path + l + 1, ":") ||
  4142. (ref->nlvl_from > 1 && same_origin < 0) ||
  4143. (filename[0] == '/' && src_path == src))
  4144. return AVERROR(ENOENT);
  4145. }
  4146. if (strlen(filename) + 1 == sizeof(filename))
  4147. return AVERROR(ENOENT);
  4148. if (!c->fc->io_open(c->fc, pb, filename, AVIO_FLAG_READ, NULL))
  4149. return 0;
  4150. }
  4151. } else if (c->use_absolute_path) {
  4152. av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
  4153. "this is a possible security issue\n");
  4154. if (!c->fc->io_open(c->fc, pb, ref->path, AVIO_FLAG_READ, NULL))
  4155. return 0;
  4156. } else {
  4157. av_log(c->fc, AV_LOG_ERROR,
  4158. "Absolute path %s not tried for security reasons, "
  4159. "set demuxer option use_absolute_path to allow absolute paths\n",
  4160. ref->path);
  4161. }
  4162. return AVERROR(ENOENT);
  4163. }
  4164. static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
  4165. {
  4166. if (sc->time_scale <= 0) {
  4167. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
  4168. sc->time_scale = c->time_scale;
  4169. if (sc->time_scale <= 0)
  4170. sc->time_scale = 1;
  4171. }
  4172. }
  4173. #if CONFIG_IAMFDEC
  4174. static int mov_update_iamf_streams(MOVContext *c, const AVStream *st)
  4175. {
  4176. const MOVStreamContext *sc = st->priv_data;
  4177. const IAMFContext *iamf = &sc->iamf->iamf;
  4178. for (int i = 0; i < iamf->nb_audio_elements; i++) {
  4179. const AVStreamGroup *stg = NULL;
  4180. for (int j = 0; j < c->fc->nb_stream_groups; j++)
  4181. if (c->fc->stream_groups[j]->id == iamf->audio_elements[i]->audio_element_id)
  4182. stg = c->fc->stream_groups[j];
  4183. av_assert0(stg);
  4184. for (int j = 0; j < stg->nb_streams; j++) {
  4185. const FFStream *sti = cffstream(st);
  4186. AVStream *out = stg->streams[j];
  4187. FFStream *out_sti = ffstream(stg->streams[j]);
  4188. out->codecpar->bit_rate = 0;
  4189. if (out == st)
  4190. continue;
  4191. out->time_base = st->time_base;
  4192. out->start_time = st->start_time;
  4193. out->duration = st->duration;
  4194. out->nb_frames = st->nb_frames;
  4195. out->discard = st->discard;
  4196. av_assert0(!out_sti->index_entries);
  4197. out_sti->index_entries = av_malloc(sti->index_entries_allocated_size);
  4198. if (!out_sti->index_entries)
  4199. return AVERROR(ENOMEM);
  4200. out_sti->index_entries_allocated_size = sti->index_entries_allocated_size;
  4201. out_sti->nb_index_entries = sti->nb_index_entries;
  4202. out_sti->skip_samples = sti->skip_samples;
  4203. memcpy(out_sti->index_entries, sti->index_entries, sti->index_entries_allocated_size);
  4204. }
  4205. }
  4206. return 0;
  4207. }
  4208. #endif
  4209. static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4210. {
  4211. AVStream *st;
  4212. MOVStreamContext *sc;
  4213. int ret;
  4214. st = avformat_new_stream(c->fc, NULL);
  4215. if (!st) return AVERROR(ENOMEM);
  4216. st->id = -1;
  4217. sc = av_mallocz(sizeof(MOVStreamContext));
  4218. if (!sc) return AVERROR(ENOMEM);
  4219. st->priv_data = sc;
  4220. st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
  4221. sc->ffindex = st->index;
  4222. c->trak_index = st->index;
  4223. sc->refcount = 1;
  4224. if ((ret = mov_read_default(c, pb, atom)) < 0)
  4225. return ret;
  4226. c->trak_index = -1;
  4227. // Here stsc refers to a chunk not described in stco. This is technically invalid,
  4228. // but we can overlook it (clearing stsc) whenever stts_count == 0 (indicating no samples).
  4229. if (!sc->chunk_count && !sc->stts_count && sc->stsc_count) {
  4230. sc->stsc_count = 0;
  4231. av_freep(&sc->stsc_data);
  4232. }
  4233. /* sanity checks */
  4234. if ((sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  4235. (!sc->sample_size && !sc->sample_count))) ||
  4236. (!sc->chunk_count && sc->sample_count)) {
  4237. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  4238. st->index);
  4239. return 0;
  4240. }
  4241. if (sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
  4242. av_log(c->fc, AV_LOG_ERROR, "stream %d, contradictionary STSC and STCO\n",
  4243. st->index);
  4244. return AVERROR_INVALIDDATA;
  4245. }
  4246. fix_timescale(c, sc);
  4247. avpriv_set_pts_info(st, 64, 1, sc->time_scale);
  4248. /*
  4249. * Advanced edit list support does not work with fragemented MP4s, which
  4250. * have stsc, stsz, stco, and stts with zero entries in the moov atom.
  4251. * In these files, trun atoms may be streamed in.
  4252. */
  4253. if (!sc->stts_count && c->advanced_editlist) {
  4254. av_log(c->fc, AV_LOG_VERBOSE, "advanced_editlist does not work with fragmented "
  4255. "MP4. disabling.\n");
  4256. c->advanced_editlist = 0;
  4257. c->advanced_editlist_autodisabled = 1;
  4258. }
  4259. mov_build_index(c, st);
  4260. #if CONFIG_IAMFDEC
  4261. if (sc->iamf) {
  4262. ret = mov_update_iamf_streams(c, st);
  4263. if (ret < 0)
  4264. return ret;
  4265. }
  4266. #endif
  4267. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  4268. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  4269. if (c->enable_drefs) {
  4270. if (mov_open_dref(c, &sc->pb, c->fc->url, dref) < 0)
  4271. av_log(c->fc, AV_LOG_ERROR,
  4272. "stream %d, error opening alias: path='%s', dir='%s', "
  4273. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  4274. st->index, dref->path, dref->dir, dref->filename,
  4275. dref->volume, dref->nlvl_from, dref->nlvl_to);
  4276. } else {
  4277. av_log(c->fc, AV_LOG_WARNING,
  4278. "Skipped opening external track: "
  4279. "stream %d, alias: path='%s', dir='%s', "
  4280. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
  4281. "Set enable_drefs to allow this.\n",
  4282. st->index, dref->path, dref->dir, dref->filename,
  4283. dref->volume, dref->nlvl_from, dref->nlvl_to);
  4284. }
  4285. } else {
  4286. sc->pb = c->fc->pb;
  4287. sc->pb_is_copied = 1;
  4288. }
  4289. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  4290. if (!st->sample_aspect_ratio.num && st->codecpar->width && st->codecpar->height &&
  4291. sc->height && sc->width &&
  4292. (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
  4293. st->sample_aspect_ratio = av_d2q(((double)st->codecpar->height * sc->width) /
  4294. ((double)st->codecpar->width * sc->height), INT_MAX);
  4295. }
  4296. #if FF_API_R_FRAME_RATE
  4297. if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
  4298. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  4299. sc->time_scale, sc->stts_data[0].duration, INT_MAX);
  4300. #endif
  4301. }
  4302. // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
  4303. if (!st->codecpar->extradata_size && st->codecpar->codec_id == AV_CODEC_ID_H264 &&
  4304. TAG_IS_AVCI(st->codecpar->codec_tag)) {
  4305. ret = ff_generate_avci_extradata(st);
  4306. if (ret < 0)
  4307. return ret;
  4308. }
  4309. switch (st->codecpar->codec_id) {
  4310. #if CONFIG_H261_DECODER
  4311. case AV_CODEC_ID_H261:
  4312. #endif
  4313. #if CONFIG_H263_DECODER
  4314. case AV_CODEC_ID_H263:
  4315. #endif
  4316. #if CONFIG_MPEG4_DECODER
  4317. case AV_CODEC_ID_MPEG4:
  4318. #endif
  4319. st->codecpar->width = 0; /* let decoder init width/height */
  4320. st->codecpar->height= 0;
  4321. break;
  4322. }
  4323. // If the duration of the mp3 packets is not constant, then they could need a parser
  4324. if (st->codecpar->codec_id == AV_CODEC_ID_MP3
  4325. && sc->stts_count > 3
  4326. && sc->stts_count*10 > st->nb_frames
  4327. && sc->time_scale == st->codecpar->sample_rate) {
  4328. ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
  4329. }
  4330. /* Do not need those anymore. */
  4331. av_freep(&sc->chunk_offsets);
  4332. av_freep(&sc->sample_sizes);
  4333. av_freep(&sc->keyframes);
  4334. av_freep(&sc->stts_data);
  4335. av_freep(&sc->stps_data);
  4336. av_freep(&sc->elst_data);
  4337. av_freep(&sc->rap_group);
  4338. av_freep(&sc->sync_group);
  4339. av_freep(&sc->sgpd_sync);
  4340. return 0;
  4341. }
  4342. static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4343. {
  4344. int ret;
  4345. c->itunes_metadata = 1;
  4346. ret = mov_read_default(c, pb, atom);
  4347. c->itunes_metadata = 0;
  4348. return ret;
  4349. }
  4350. static int mov_read_keys(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4351. {
  4352. uint32_t count;
  4353. uint32_t i;
  4354. if (atom.size < 8)
  4355. return 0;
  4356. avio_skip(pb, 4);
  4357. count = avio_rb32(pb);
  4358. atom.size -= 8;
  4359. if (count >= UINT_MAX / sizeof(*c->meta_keys)) {
  4360. av_log(c->fc, AV_LOG_ERROR,
  4361. "The 'keys' atom with the invalid key count: %"PRIu32"\n", count);
  4362. return AVERROR_INVALIDDATA;
  4363. }
  4364. c->meta_keys_count = count + 1;
  4365. c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
  4366. if (!c->meta_keys)
  4367. return AVERROR(ENOMEM);
  4368. for (i = 1; i <= count; ++i) {
  4369. uint32_t key_size = avio_rb32(pb);
  4370. uint32_t type = avio_rl32(pb);
  4371. if (key_size < 8 || key_size > atom.size) {
  4372. av_log(c->fc, AV_LOG_ERROR,
  4373. "The key# %"PRIu32" in meta has invalid size:"
  4374. "%"PRIu32"\n", i, key_size);
  4375. return AVERROR_INVALIDDATA;
  4376. }
  4377. atom.size -= key_size;
  4378. key_size -= 8;
  4379. if (type != MKTAG('m','d','t','a')) {
  4380. avio_skip(pb, key_size);
  4381. continue;
  4382. }
  4383. c->meta_keys[i] = av_mallocz(key_size + 1);
  4384. if (!c->meta_keys[i])
  4385. return AVERROR(ENOMEM);
  4386. avio_read(pb, c->meta_keys[i], key_size);
  4387. }
  4388. return 0;
  4389. }
  4390. static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4391. {
  4392. int64_t end = av_sat_add64(avio_tell(pb), atom.size);
  4393. uint8_t *key = NULL, *val = NULL, *mean = NULL;
  4394. int i;
  4395. int ret = 0;
  4396. AVStream *st;
  4397. MOVStreamContext *sc;
  4398. if (c->fc->nb_streams < 1)
  4399. return 0;
  4400. st = c->fc->streams[c->fc->nb_streams-1];
  4401. sc = st->priv_data;
  4402. for (i = 0; i < 3; i++) {
  4403. uint8_t **p;
  4404. uint32_t len, tag;
  4405. if (end - avio_tell(pb) <= 12)
  4406. break;
  4407. len = avio_rb32(pb);
  4408. tag = avio_rl32(pb);
  4409. avio_skip(pb, 4); // flags
  4410. if (len < 12 || len - 12 > end - avio_tell(pb))
  4411. break;
  4412. len -= 12;
  4413. if (tag == MKTAG('m', 'e', 'a', 'n'))
  4414. p = &mean;
  4415. else if (tag == MKTAG('n', 'a', 'm', 'e'))
  4416. p = &key;
  4417. else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
  4418. avio_skip(pb, 4);
  4419. len -= 4;
  4420. p = &val;
  4421. } else
  4422. break;
  4423. if (*p)
  4424. break;
  4425. *p = av_malloc(len + 1);
  4426. if (!*p) {
  4427. ret = AVERROR(ENOMEM);
  4428. break;
  4429. }
  4430. ret = ffio_read_size(pb, *p, len);
  4431. if (ret < 0) {
  4432. av_freep(p);
  4433. break;
  4434. }
  4435. (*p)[len] = 0;
  4436. }
  4437. if (mean && key && val) {
  4438. if (strcmp(key, "iTunSMPB") == 0) {
  4439. int priming, remainder, samples;
  4440. if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
  4441. if(priming>0 && priming<16384)
  4442. sc->start_pad = priming;
  4443. }
  4444. }
  4445. if (strcmp(key, "cdec") != 0) {
  4446. av_dict_set(&c->fc->metadata, key, val,
  4447. AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  4448. key = val = NULL;
  4449. }
  4450. } else {
  4451. av_log(c->fc, AV_LOG_VERBOSE,
  4452. "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
  4453. }
  4454. avio_seek(pb, end, SEEK_SET);
  4455. av_freep(&key);
  4456. av_freep(&val);
  4457. av_freep(&mean);
  4458. return ret;
  4459. }
  4460. static int heif_add_stream(MOVContext *c, HEIFItem *item)
  4461. {
  4462. MOVStreamContext *sc;
  4463. AVStream *st;
  4464. st = avformat_new_stream(c->fc, NULL);
  4465. if (!st)
  4466. return AVERROR(ENOMEM);
  4467. sc = av_mallocz(sizeof(MOVStreamContext));
  4468. if (!sc)
  4469. return AVERROR(ENOMEM);
  4470. item->st = st;
  4471. st->id = item->item_id;
  4472. st->priv_data = sc;
  4473. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  4474. st->codecpar->codec_id = mov_codec_id(st, item->type);
  4475. sc->id = st->id;
  4476. sc->ffindex = st->index;
  4477. st->avg_frame_rate.num = st->avg_frame_rate.den = 1;
  4478. st->time_base.num = st->time_base.den = 1;
  4479. st->nb_frames = 1;
  4480. sc->time_scale = 1;
  4481. sc->pb = c->fc->pb;
  4482. sc->pb_is_copied = 1;
  4483. sc->refcount = 1;
  4484. if (item->name)
  4485. av_dict_set(&st->metadata, "title", item->name, 0);
  4486. // Populate the necessary fields used by mov_build_index.
  4487. sc->stsc_count = 1;
  4488. sc->stsc_data = av_malloc_array(1, sizeof(*sc->stsc_data));
  4489. if (!sc->stsc_data)
  4490. return AVERROR(ENOMEM);
  4491. sc->stsc_data[0].first = 1;
  4492. sc->stsc_data[0].count = 1;
  4493. sc->stsc_data[0].id = 1;
  4494. sc->chunk_count = 1;
  4495. sc->chunk_offsets = av_malloc_array(1, sizeof(*sc->chunk_offsets));
  4496. if (!sc->chunk_offsets)
  4497. return AVERROR(ENOMEM);
  4498. sc->sample_count = 1;
  4499. sc->sample_sizes = av_malloc_array(1, sizeof(*sc->sample_sizes));
  4500. if (!sc->sample_sizes)
  4501. return AVERROR(ENOMEM);
  4502. sc->stts_count = 1;
  4503. sc->stts_data = av_malloc_array(1, sizeof(*sc->stts_data));
  4504. if (!sc->stts_data)
  4505. return AVERROR(ENOMEM);
  4506. sc->stts_data[0].count = 1;
  4507. // Not used for still images. But needed by mov_build_index.
  4508. sc->stts_data[0].duration = 0;
  4509. return 0;
  4510. }
  4511. static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4512. {
  4513. while (atom.size > 8) {
  4514. uint32_t tag;
  4515. if (avio_feof(pb))
  4516. return AVERROR_EOF;
  4517. tag = avio_rl32(pb);
  4518. atom.size -= 4;
  4519. if (tag == MKTAG('h','d','l','r')) {
  4520. avio_seek(pb, -8, SEEK_CUR);
  4521. atom.size += 8;
  4522. return mov_read_default(c, pb, atom);
  4523. }
  4524. }
  4525. return 0;
  4526. }
  4527. // return 1 when matrix is identity, 0 otherwise
  4528. #define IS_MATRIX_IDENT(matrix) \
  4529. ( (matrix)[0][0] == (1 << 16) && \
  4530. (matrix)[1][1] == (1 << 16) && \
  4531. (matrix)[2][2] == (1 << 30) && \
  4532. !(matrix)[0][1] && !(matrix)[0][2] && \
  4533. !(matrix)[1][0] && !(matrix)[1][2] && \
  4534. !(matrix)[2][0] && !(matrix)[2][1])
  4535. static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4536. {
  4537. int i, j, e;
  4538. int width;
  4539. int height;
  4540. int display_matrix[3][3];
  4541. int res_display_matrix[3][3] = { { 0 } };
  4542. AVStream *st;
  4543. MOVStreamContext *sc;
  4544. int version;
  4545. int flags;
  4546. if (c->fc->nb_streams < 1)
  4547. return 0;
  4548. st = c->fc->streams[c->fc->nb_streams-1];
  4549. sc = st->priv_data;
  4550. // Each stream (trak) should have exactly 1 tkhd. This catches bad files and
  4551. // avoids corrupting AVStreams mapped to an earlier tkhd.
  4552. if (st->id != -1)
  4553. return AVERROR_INVALIDDATA;
  4554. version = avio_r8(pb);
  4555. flags = avio_rb24(pb);
  4556. st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
  4557. if (version == 1) {
  4558. avio_rb64(pb);
  4559. avio_rb64(pb);
  4560. } else {
  4561. avio_rb32(pb); /* creation time */
  4562. avio_rb32(pb); /* modification time */
  4563. }
  4564. st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
  4565. sc->id = st->id;
  4566. avio_rb32(pb); /* reserved */
  4567. /* highlevel (considering edits) duration in movie timebase */
  4568. (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
  4569. avio_rb32(pb); /* reserved */
  4570. avio_rb32(pb); /* reserved */
  4571. avio_rb16(pb); /* layer */
  4572. avio_rb16(pb); /* alternate group */
  4573. avio_rb16(pb); /* volume */
  4574. avio_rb16(pb); /* reserved */
  4575. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  4576. // they're kept in fixed point format through all calculations
  4577. // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
  4578. // side data, but the scale factor is not needed to calculate aspect ratio
  4579. for (i = 0; i < 3; i++) {
  4580. display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  4581. display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  4582. display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
  4583. }
  4584. width = avio_rb32(pb); // 16.16 fixed point track width
  4585. height = avio_rb32(pb); // 16.16 fixed point track height
  4586. sc->width = width >> 16;
  4587. sc->height = height >> 16;
  4588. // apply the moov display matrix (after the tkhd one)
  4589. for (i = 0; i < 3; i++) {
  4590. const int sh[3] = { 16, 16, 30 };
  4591. for (j = 0; j < 3; j++) {
  4592. for (e = 0; e < 3; e++) {
  4593. res_display_matrix[i][j] +=
  4594. ((int64_t) display_matrix[i][e] *
  4595. c->movie_display_matrix[e][j]) >> sh[e];
  4596. }
  4597. }
  4598. }
  4599. // save the matrix when it is not the default identity
  4600. if (!IS_MATRIX_IDENT(res_display_matrix)) {
  4601. av_freep(&sc->display_matrix);
  4602. sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
  4603. if (!sc->display_matrix)
  4604. return AVERROR(ENOMEM);
  4605. for (i = 0; i < 3; i++)
  4606. for (j = 0; j < 3; j++)
  4607. sc->display_matrix[i * 3 + j] = res_display_matrix[i][j];
  4608. }
  4609. // transform the display width/height according to the matrix
  4610. // to keep the same scale, use [width height 1<<16]
  4611. if (width && height && sc->display_matrix) {
  4612. double disp_transform[2];
  4613. for (i = 0; i < 2; i++)
  4614. disp_transform[i] = hypot(sc->display_matrix[0 + i],
  4615. sc->display_matrix[3 + i]);
  4616. if (disp_transform[0] > 1 && disp_transform[1] > 1 &&
  4617. disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
  4618. fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
  4619. st->sample_aspect_ratio = av_d2q(
  4620. disp_transform[0] / disp_transform[1],
  4621. INT_MAX);
  4622. }
  4623. return 0;
  4624. }
  4625. static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4626. {
  4627. MOVFragment *frag = &c->fragment;
  4628. MOVTrackExt *trex = NULL;
  4629. int flags, track_id, i;
  4630. MOVFragmentStreamInfo * frag_stream_info;
  4631. avio_r8(pb); /* version */
  4632. flags = avio_rb24(pb);
  4633. track_id = avio_rb32(pb);
  4634. if (!track_id)
  4635. return AVERROR_INVALIDDATA;
  4636. for (i = 0; i < c->trex_count; i++)
  4637. if (c->trex_data[i].track_id == track_id) {
  4638. trex = &c->trex_data[i];
  4639. break;
  4640. }
  4641. if (!trex) {
  4642. av_log(c->fc, AV_LOG_WARNING, "could not find corresponding trex (id %u)\n", track_id);
  4643. return 0;
  4644. }
  4645. c->fragment.found_tfhd = 1;
  4646. frag->track_id = track_id;
  4647. set_frag_stream(&c->frag_index, track_id);
  4648. frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
  4649. avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ?
  4650. frag->moof_offset : frag->implicit_offset;
  4651. frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
  4652. frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
  4653. avio_rb32(pb) : trex->duration;
  4654. frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
  4655. avio_rb32(pb) : trex->size;
  4656. frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
  4657. avio_rb32(pb) : trex->flags;
  4658. av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
  4659. frag_stream_info = get_current_frag_stream_info(&c->frag_index);
  4660. if (frag_stream_info) {
  4661. frag_stream_info->next_trun_dts = AV_NOPTS_VALUE;
  4662. frag_stream_info->stsd_id = frag->stsd_id;
  4663. }
  4664. return 0;
  4665. }
  4666. static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4667. {
  4668. unsigned i, num;
  4669. void *new_tracks;
  4670. num = atom.size / 4;
  4671. if (!(new_tracks = av_malloc_array(num, sizeof(int))))
  4672. return AVERROR(ENOMEM);
  4673. av_free(c->chapter_tracks);
  4674. c->chapter_tracks = new_tracks;
  4675. c->nb_chapter_tracks = num;
  4676. for (i = 0; i < num && !pb->eof_reached; i++)
  4677. c->chapter_tracks[i] = avio_rb32(pb);
  4678. c->nb_chapter_tracks = i;
  4679. return 0;
  4680. }
  4681. static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4682. {
  4683. MOVTrackExt *trex;
  4684. int err;
  4685. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  4686. return AVERROR_INVALIDDATA;
  4687. if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
  4688. sizeof(*c->trex_data))) < 0) {
  4689. c->trex_count = 0;
  4690. return err;
  4691. }
  4692. c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
  4693. trex = &c->trex_data[c->trex_count++];
  4694. avio_r8(pb); /* version */
  4695. avio_rb24(pb); /* flags */
  4696. trex->track_id = avio_rb32(pb);
  4697. trex->stsd_id = avio_rb32(pb);
  4698. trex->duration = avio_rb32(pb);
  4699. trex->size = avio_rb32(pb);
  4700. trex->flags = avio_rb32(pb);
  4701. return 0;
  4702. }
  4703. static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4704. {
  4705. MOVFragment *frag = &c->fragment;
  4706. AVStream *st = NULL;
  4707. MOVStreamContext *sc;
  4708. int version, i;
  4709. MOVFragmentStreamInfo * frag_stream_info;
  4710. int64_t base_media_decode_time;
  4711. for (i = 0; i < c->fc->nb_streams; i++) {
  4712. sc = c->fc->streams[i]->priv_data;
  4713. if (sc->id == frag->track_id) {
  4714. st = c->fc->streams[i];
  4715. break;
  4716. }
  4717. }
  4718. if (!st) {
  4719. av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
  4720. return 0;
  4721. }
  4722. sc = st->priv_data;
  4723. if (sc->pseudo_stream_id + 1 != frag->stsd_id && sc->pseudo_stream_id != -1)
  4724. return 0;
  4725. version = avio_r8(pb);
  4726. avio_rb24(pb); /* flags */
  4727. if (version) {
  4728. base_media_decode_time = avio_rb64(pb);
  4729. } else {
  4730. base_media_decode_time = avio_rb32(pb);
  4731. }
  4732. frag_stream_info = get_current_frag_stream_info(&c->frag_index);
  4733. if (frag_stream_info)
  4734. frag_stream_info->tfdt_dts = base_media_decode_time;
  4735. sc->track_end = base_media_decode_time;
  4736. return 0;
  4737. }
  4738. static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  4739. {
  4740. MOVFragment *frag = &c->fragment;
  4741. AVStream *st = NULL;
  4742. FFStream *sti = NULL;
  4743. MOVStreamContext *sc;
  4744. MOVCtts *ctts_data;
  4745. uint64_t offset;
  4746. int64_t dts, pts = AV_NOPTS_VALUE;
  4747. int data_offset = 0;
  4748. unsigned entries, first_sample_flags = frag->flags;
  4749. int flags, distance, i;
  4750. int64_t prev_dts = AV_NOPTS_VALUE;
  4751. int next_frag_index = -1, index_entry_pos;
  4752. size_t requested_size;
  4753. size_t old_ctts_allocated_size;
  4754. AVIndexEntry *new_entries;
  4755. MOVFragmentStreamInfo * frag_stream_info;
  4756. if (!frag->found_tfhd) {
  4757. av_log(c->fc, AV_LOG_ERROR, "trun track id unknown, no tfhd was found\n");
  4758. return AVERROR_INVALIDDATA;
  4759. }
  4760. for (i = 0; i < c->fc->nb_streams; i++) {
  4761. sc = c->fc->streams[i]->priv_data;
  4762. if (sc->id == frag->track_id) {
  4763. st = c->fc->streams[i];
  4764. sti = ffstream(st);
  4765. break;
  4766. }
  4767. }
  4768. if (!st) {
  4769. av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
  4770. return 0;
  4771. }
  4772. sc = st->priv_data;
  4773. if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
  4774. return 0;
  4775. // Find the next frag_index index that has a valid index_entry for
  4776. // the current track_id.
  4777. //
  4778. // A valid index_entry means the trun for the fragment was read
  4779. // and it's samples are in index_entries at the given position.
  4780. // New index entries will be inserted before the index_entry found.
  4781. index_entry_pos = sti->nb_index_entries;
  4782. for (i = c->frag_index.current + 1; i < c->frag_index.nb_items; i++) {
  4783. frag_stream_info = get_frag_stream_info(&c->frag_index, i, frag->track_id);
  4784. if (frag_stream_info && frag_stream_info->index_entry >= 0) {
  4785. next_frag_index = i;
  4786. index_entry_pos = frag_stream_info->index_entry;
  4787. break;
  4788. }
  4789. }
  4790. av_assert0(index_entry_pos <= sti->nb_index_entries);
  4791. avio_r8(pb); /* version */
  4792. flags = avio_rb24(pb);
  4793. entries = avio_rb32(pb);
  4794. av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %u\n", flags, entries);
  4795. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  4796. return AVERROR_INVALIDDATA;
  4797. if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
  4798. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
  4799. frag_stream_info = get_current_frag_stream_info(&c->frag_index);
  4800. if (frag_stream_info) {
  4801. if (frag_stream_info->next_trun_dts != AV_NOPTS_VALUE) {
  4802. dts = frag_stream_info->next_trun_dts - sc->time_offset;
  4803. } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
  4804. c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
  4805. pts = frag_stream_info->first_tfra_pts;
  4806. av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
  4807. ", using it for pts\n", pts);
  4808. } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
  4809. c->use_mfra_for == FF_MOV_FLAG_MFRA_DTS) {
  4810. dts = frag_stream_info->first_tfra_pts;
  4811. av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
  4812. ", using it for dts\n", pts);
  4813. } else {
  4814. int has_tfdt = frag_stream_info->tfdt_dts != AV_NOPTS_VALUE;
  4815. int has_sidx = frag_stream_info->sidx_pts != AV_NOPTS_VALUE;
  4816. int fallback_tfdt = !c->use_tfdt && !has_sidx && has_tfdt;
  4817. int fallback_sidx = c->use_tfdt && !has_tfdt && has_sidx;
  4818. if (fallback_sidx) {
  4819. av_log(c->fc, AV_LOG_DEBUG, "use_tfdt set but no tfdt found, using sidx instead\n");
  4820. }
  4821. if (fallback_tfdt) {
  4822. av_log(c->fc, AV_LOG_DEBUG, "use_tfdt not set but no sidx found, using tfdt instead\n");
  4823. }
  4824. if (has_tfdt && c->use_tfdt || fallback_tfdt) {
  4825. dts = frag_stream_info->tfdt_dts - sc->time_offset;
  4826. av_log(c->fc, AV_LOG_DEBUG, "found tfdt time %"PRId64
  4827. ", using it for dts\n", dts);
  4828. } else if (has_sidx && !c->use_tfdt || fallback_sidx) {
  4829. // FIXME: sidx earliest_presentation_time is *PTS*, s.b.
  4830. // pts = frag_stream_info->sidx_pts;
  4831. dts = frag_stream_info->sidx_pts - sc->time_offset;
  4832. av_log(c->fc, AV_LOG_DEBUG, "found sidx time %"PRId64
  4833. ", using it for dts\n", frag_stream_info->sidx_pts);
  4834. } else {
  4835. dts = sc->track_end - sc->time_offset;
  4836. av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
  4837. ", using it for dts\n", dts);
  4838. }
  4839. }
  4840. } else {
  4841. dts = sc->track_end - sc->time_offset;
  4842. av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
  4843. ", using it for dts\n", dts);
  4844. }
  4845. offset = frag->base_data_offset + data_offset;
  4846. distance = 0;
  4847. av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
  4848. // realloc space for new index entries
  4849. if ((uint64_t)sti->nb_index_entries + entries >= UINT_MAX / sizeof(AVIndexEntry)) {
  4850. entries = UINT_MAX / sizeof(AVIndexEntry) - sti->nb_index_entries;
  4851. av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
  4852. }
  4853. if (entries == 0)
  4854. return 0;
  4855. requested_size = (sti->nb_index_entries + entries) * sizeof(AVIndexEntry);
  4856. new_entries = av_fast_realloc(sti->index_entries,
  4857. &sti->index_entries_allocated_size,
  4858. requested_size);
  4859. if (!new_entries)
  4860. return AVERROR(ENOMEM);
  4861. sti->index_entries= new_entries;
  4862. requested_size = (sti->nb_index_entries + entries) * sizeof(*sc->ctts_data);
  4863. old_ctts_allocated_size = sc->ctts_allocated_size;
  4864. ctts_data = av_fast_realloc(sc->ctts_data, &sc->ctts_allocated_size,
  4865. requested_size);
  4866. if (!ctts_data)
  4867. return AVERROR(ENOMEM);
  4868. sc->ctts_data = ctts_data;
  4869. // In case there were samples without ctts entries, ensure they get
  4870. // zero valued entries. This ensures clips which mix boxes with and
  4871. // without ctts entries don't pickup uninitialized data.
  4872. memset((uint8_t*)(sc->ctts_data) + old_ctts_allocated_size, 0,
  4873. sc->ctts_allocated_size - old_ctts_allocated_size);
  4874. if (index_entry_pos < sti->nb_index_entries) {
  4875. // Make hole in index_entries and ctts_data for new samples
  4876. memmove(sti->index_entries + index_entry_pos + entries,
  4877. sti->index_entries + index_entry_pos,
  4878. sizeof(*sti->index_entries) *
  4879. (sti->nb_index_entries - index_entry_pos));
  4880. memmove(sc->ctts_data + index_entry_pos + entries,
  4881. sc->ctts_data + index_entry_pos,
  4882. sizeof(*sc->ctts_data) * (sc->ctts_count - index_entry_pos));
  4883. if (index_entry_pos < sc->current_sample) {
  4884. sc->current_sample += entries;
  4885. }
  4886. }
  4887. sti->nb_index_entries += entries;
  4888. sc->ctts_count = sti->nb_index_entries;
  4889. // Record the index_entry position in frag_index of this fragment
  4890. if (frag_stream_info) {
  4891. frag_stream_info->index_entry = index_entry_pos;
  4892. if (frag_stream_info->index_base < 0)
  4893. frag_stream_info->index_base = index_entry_pos;
  4894. }
  4895. if (index_entry_pos > 0)
  4896. prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
  4897. for (i = 0; i < entries && !pb->eof_reached; i++) {
  4898. unsigned sample_size = frag->size;
  4899. int sample_flags = i ? frag->flags : first_sample_flags;
  4900. unsigned sample_duration = frag->duration;
  4901. unsigned ctts_duration = 0;
  4902. int keyframe = 0;
  4903. int index_entry_flags = 0;
  4904. if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
  4905. if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
  4906. if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
  4907. if (flags & MOV_TRUN_SAMPLE_CTS) ctts_duration = avio_rb32(pb);
  4908. mov_update_dts_shift(sc, ctts_duration, c->fc);
  4909. if (pts != AV_NOPTS_VALUE) {
  4910. dts = pts - sc->dts_shift;
  4911. if (flags & MOV_TRUN_SAMPLE_CTS) {
  4912. dts -= ctts_duration;
  4913. } else {
  4914. dts -= sc->time_offset;
  4915. }
  4916. av_log(c->fc, AV_LOG_DEBUG,
  4917. "pts %"PRId64" calculated dts %"PRId64
  4918. " sc->dts_shift %d ctts.duration %d"
  4919. " sc->time_offset %"PRId64
  4920. " flags & MOV_TRUN_SAMPLE_CTS %d\n",
  4921. pts, dts,
  4922. sc->dts_shift, ctts_duration,
  4923. sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS);
  4924. pts = AV_NOPTS_VALUE;
  4925. }
  4926. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
  4927. keyframe = 1;
  4928. else
  4929. keyframe =
  4930. !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
  4931. MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
  4932. if (keyframe) {
  4933. distance = 0;
  4934. index_entry_flags |= AVINDEX_KEYFRAME;
  4935. }
  4936. // Fragments can overlap in time. Discard overlapping frames after
  4937. // decoding.
  4938. if (prev_dts >= dts)
  4939. index_entry_flags |= AVINDEX_DISCARD_FRAME;
  4940. sti->index_entries[index_entry_pos].pos = offset;
  4941. sti->index_entries[index_entry_pos].timestamp = dts;
  4942. sti->index_entries[index_entry_pos].size = sample_size;
  4943. sti->index_entries[index_entry_pos].min_distance = distance;
  4944. sti->index_entries[index_entry_pos].flags = index_entry_flags;
  4945. sc->ctts_data[index_entry_pos].count = 1;
  4946. sc->ctts_data[index_entry_pos].duration = ctts_duration;
  4947. index_entry_pos++;
  4948. av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  4949. "size %u, distance %d, keyframe %d\n", st->index,
  4950. index_entry_pos, offset, dts, sample_size, distance, keyframe);
  4951. distance++;
  4952. if (av_sat_add64(dts, sample_duration) != dts + (uint64_t)sample_duration)
  4953. return AVERROR_INVALIDDATA;
  4954. if (!sample_size)
  4955. return AVERROR_INVALIDDATA;
  4956. dts += sample_duration;
  4957. offset += sample_size;
  4958. sc->data_size += sample_size;
  4959. if (sample_duration <= INT64_MAX - sc->duration_for_fps &&
  4960. 1 <= INT_MAX - sc->nb_frames_for_fps
  4961. ) {
  4962. sc->duration_for_fps += sample_duration;
  4963. sc->nb_frames_for_fps ++;
  4964. }
  4965. }
  4966. if (frag_stream_info)
  4967. frag_stream_info->next_trun_dts = dts + sc->time_offset;
  4968. if (i < entries) {
  4969. // EOF found before reading all entries. Fix the hole this would
  4970. // leave in index_entries and ctts_data
  4971. int gap = entries - i;
  4972. memmove(sti->index_entries + index_entry_pos,
  4973. sti->index_entries + index_entry_pos + gap,
  4974. sizeof(*sti->index_entries) *
  4975. (sti->nb_index_entries - (index_entry_pos + gap)));
  4976. memmove(sc->ctts_data + index_entry_pos,
  4977. sc->ctts_data + index_entry_pos + gap,
  4978. sizeof(*sc->ctts_data) *
  4979. (sc->ctts_count - (index_entry_pos + gap)));
  4980. sti->nb_index_entries -= gap;
  4981. sc->ctts_count -= gap;
  4982. if (index_entry_pos < sc->current_sample) {
  4983. sc->current_sample -= gap;
  4984. }
  4985. entries = i;
  4986. }
  4987. // The end of this new fragment may overlap in time with the start
  4988. // of the next fragment in index_entries. Mark the samples in the next
  4989. // fragment that overlap with AVINDEX_DISCARD_FRAME
  4990. prev_dts = AV_NOPTS_VALUE;
  4991. if (index_entry_pos > 0)
  4992. prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
  4993. for (int i = index_entry_pos; i < sti->nb_index_entries; i++) {
  4994. if (prev_dts < sti->index_entries[i].timestamp)
  4995. break;
  4996. sti->index_entries[i].flags |= AVINDEX_DISCARD_FRAME;
  4997. }
  4998. // If a hole was created to insert the new index_entries into,
  4999. // the index_entry recorded for all subsequent moof must
  5000. // be incremented by the number of entries inserted.
  5001. fix_frag_index_entries(&c->frag_index, next_frag_index,
  5002. frag->track_id, entries);
  5003. if (pb->eof_reached) {
  5004. av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted TRUN atom\n");
  5005. return AVERROR_EOF;
  5006. }
  5007. frag->implicit_offset = offset;
  5008. sc->track_end = dts + sc->time_offset;
  5009. if (st->duration < sc->track_end)
  5010. st->duration = sc->track_end;
  5011. return 0;
  5012. }
  5013. static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5014. {
  5015. int64_t stream_size = avio_size(pb);
  5016. int64_t offset = av_sat_add64(avio_tell(pb), atom.size), pts, timestamp;
  5017. uint8_t version, is_complete;
  5018. int64_t offadd;
  5019. unsigned i, j, track_id, item_count;
  5020. AVStream *st = NULL;
  5021. AVStream *ref_st = NULL;
  5022. MOVStreamContext *sc, *ref_sc = NULL;
  5023. AVRational timescale;
  5024. version = avio_r8(pb);
  5025. if (version > 1) {
  5026. avpriv_request_sample(c->fc, "sidx version %u", version);
  5027. return 0;
  5028. }
  5029. avio_rb24(pb); // flags
  5030. track_id = avio_rb32(pb); // Reference ID
  5031. for (i = 0; i < c->fc->nb_streams; i++) {
  5032. sc = c->fc->streams[i]->priv_data;
  5033. if (sc->id == track_id) {
  5034. st = c->fc->streams[i];
  5035. break;
  5036. }
  5037. }
  5038. if (!st) {
  5039. av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
  5040. return 0;
  5041. }
  5042. sc = st->priv_data;
  5043. timescale = av_make_q(1, avio_rb32(pb));
  5044. if (timescale.den <= 0) {
  5045. av_log(c->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);
  5046. return AVERROR_INVALIDDATA;
  5047. }
  5048. if (version == 0) {
  5049. pts = avio_rb32(pb);
  5050. offadd= avio_rb32(pb);
  5051. } else {
  5052. pts = avio_rb64(pb);
  5053. offadd= avio_rb64(pb);
  5054. }
  5055. if (av_sat_add64(offset, offadd) != offset + (uint64_t)offadd)
  5056. return AVERROR_INVALIDDATA;
  5057. offset += (uint64_t)offadd;
  5058. avio_rb16(pb); // reserved
  5059. item_count = avio_rb16(pb);
  5060. if (item_count == 0)
  5061. return AVERROR_INVALIDDATA;
  5062. for (i = 0; i < item_count; i++) {
  5063. int index;
  5064. MOVFragmentStreamInfo * frag_stream_info;
  5065. uint32_t size = avio_rb32(pb);
  5066. uint32_t duration = avio_rb32(pb);
  5067. if (size & 0x80000000) {
  5068. avpriv_request_sample(c->fc, "sidx reference_type 1");
  5069. return AVERROR_PATCHWELCOME;
  5070. }
  5071. avio_rb32(pb); // sap_flags
  5072. timestamp = av_rescale_q(pts, timescale, st->time_base);
  5073. index = update_frag_index(c, offset);
  5074. frag_stream_info = get_frag_stream_info(&c->frag_index, index, track_id);
  5075. if (frag_stream_info)
  5076. frag_stream_info->sidx_pts = timestamp;
  5077. if (av_sat_add64(offset, size) != offset + (uint64_t)size ||
  5078. av_sat_add64(pts, duration) != pts + (uint64_t)duration
  5079. )
  5080. return AVERROR_INVALIDDATA;
  5081. offset += size;
  5082. pts += duration;
  5083. }
  5084. st->duration = sc->track_end = pts;
  5085. sc->has_sidx = 1;
  5086. // See if the remaining bytes are just an mfra which we can ignore.
  5087. is_complete = offset == stream_size;
  5088. if (!is_complete && (pb->seekable & AVIO_SEEKABLE_NORMAL) && stream_size > 0 ) {
  5089. int64_t ret;
  5090. int64_t original_pos = avio_tell(pb);
  5091. if (!c->have_read_mfra_size) {
  5092. if ((ret = avio_seek(pb, stream_size - 4, SEEK_SET)) < 0)
  5093. return ret;
  5094. c->mfra_size = avio_rb32(pb);
  5095. c->have_read_mfra_size = 1;
  5096. if ((ret = avio_seek(pb, original_pos, SEEK_SET)) < 0)
  5097. return ret;
  5098. }
  5099. if (offset == stream_size - c->mfra_size)
  5100. is_complete = 1;
  5101. }
  5102. if (is_complete) {
  5103. // Find first entry in fragment index that came from an sidx.
  5104. // This will pretty much always be the first entry.
  5105. for (i = 0; i < c->frag_index.nb_items; i++) {
  5106. MOVFragmentIndexItem * item = &c->frag_index.item[i];
  5107. for (j = 0; ref_st == NULL && j < item->nb_stream_info; j++) {
  5108. MOVFragmentStreamInfo * si;
  5109. si = &item->stream_info[j];
  5110. if (si->sidx_pts != AV_NOPTS_VALUE) {
  5111. ref_st = c->fc->streams[j];
  5112. ref_sc = ref_st->priv_data;
  5113. break;
  5114. }
  5115. }
  5116. }
  5117. if (ref_st) for (i = 0; i < c->fc->nb_streams; i++) {
  5118. st = c->fc->streams[i];
  5119. sc = st->priv_data;
  5120. if (!sc->has_sidx) {
  5121. st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);
  5122. }
  5123. }
  5124. c->frag_index.complete = 1;
  5125. }
  5126. return 0;
  5127. }
  5128. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  5129. /* like the files created with Adobe Premiere 5.0, for samples see */
  5130. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  5131. static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5132. {
  5133. int err;
  5134. if (atom.size < 8)
  5135. return 0; /* continue */
  5136. if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  5137. avio_skip(pb, atom.size - 4);
  5138. return 0;
  5139. }
  5140. atom.type = avio_rl32(pb);
  5141. atom.size -= 8;
  5142. if (atom.type != MKTAG('m','d','a','t')) {
  5143. avio_skip(pb, atom.size);
  5144. return 0;
  5145. }
  5146. err = mov_read_mdat(c, pb, atom);
  5147. return err;
  5148. }
  5149. static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5150. {
  5151. #if CONFIG_ZLIB
  5152. FFIOContext ctx;
  5153. uint8_t *cmov_data;
  5154. uint8_t *moov_data; /* uncompressed data */
  5155. long cmov_len, moov_len;
  5156. int ret = -1;
  5157. avio_rb32(pb); /* dcom atom */
  5158. if (avio_rl32(pb) != MKTAG('d','c','o','m'))
  5159. return AVERROR_INVALIDDATA;
  5160. if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
  5161. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
  5162. return AVERROR_INVALIDDATA;
  5163. }
  5164. avio_rb32(pb); /* cmvd atom */
  5165. if (avio_rl32(pb) != MKTAG('c','m','v','d'))
  5166. return AVERROR_INVALIDDATA;
  5167. moov_len = avio_rb32(pb); /* uncompressed size */
  5168. cmov_len = atom.size - 6 * 4;
  5169. cmov_data = av_malloc(cmov_len);
  5170. if (!cmov_data)
  5171. return AVERROR(ENOMEM);
  5172. moov_data = av_malloc(moov_len);
  5173. if (!moov_data) {
  5174. av_free(cmov_data);
  5175. return AVERROR(ENOMEM);
  5176. }
  5177. ret = ffio_read_size(pb, cmov_data, cmov_len);
  5178. if (ret < 0)
  5179. goto free_and_return;
  5180. ret = AVERROR_INVALIDDATA;
  5181. if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  5182. goto free_and_return;
  5183. ffio_init_read_context(&ctx, moov_data, moov_len);
  5184. ctx.pub.seekable = AVIO_SEEKABLE_NORMAL;
  5185. atom.type = MKTAG('m','o','o','v');
  5186. atom.size = moov_len;
  5187. ret = mov_read_default(c, &ctx.pub, atom);
  5188. free_and_return:
  5189. av_free(moov_data);
  5190. av_free(cmov_data);
  5191. return ret;
  5192. #else
  5193. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  5194. return AVERROR(ENOSYS);
  5195. #endif
  5196. }
  5197. /* edit list atom */
  5198. static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5199. {
  5200. MOVStreamContext *sc;
  5201. int i, edit_count, version;
  5202. int64_t elst_entry_size;
  5203. if (c->fc->nb_streams < 1 || c->ignore_editlist)
  5204. return 0;
  5205. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  5206. version = avio_r8(pb); /* version */
  5207. avio_rb24(pb); /* flags */
  5208. edit_count = avio_rb32(pb); /* entries */
  5209. atom.size -= 8;
  5210. elst_entry_size = version == 1 ? 20 : 12;
  5211. if (atom.size != edit_count * elst_entry_size) {
  5212. if (c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
  5213. av_log(c->fc, AV_LOG_ERROR, "Invalid edit list entry_count: %d for elst atom of size: %"PRId64" bytes.\n",
  5214. edit_count, atom.size + 8);
  5215. return AVERROR_INVALIDDATA;
  5216. } else {
  5217. edit_count = atom.size / elst_entry_size;
  5218. if (edit_count * elst_entry_size != atom.size) {
  5219. av_log(c->fc, AV_LOG_WARNING, "ELST atom of %"PRId64" bytes, bigger than %d entries.\n", atom.size, edit_count);
  5220. }
  5221. }
  5222. }
  5223. if (!edit_count)
  5224. return 0;
  5225. if (sc->elst_data)
  5226. av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
  5227. av_free(sc->elst_data);
  5228. sc->elst_count = 0;
  5229. sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
  5230. if (!sc->elst_data)
  5231. return AVERROR(ENOMEM);
  5232. av_log(c->fc, AV_LOG_TRACE, "track[%u].edit_count = %i\n", c->fc->nb_streams - 1, edit_count);
  5233. for (i = 0; i < edit_count && atom.size > 0 && !pb->eof_reached; i++) {
  5234. MOVElst *e = &sc->elst_data[i];
  5235. if (version == 1) {
  5236. e->duration = avio_rb64(pb);
  5237. e->time = avio_rb64(pb);
  5238. atom.size -= 16;
  5239. } else {
  5240. e->duration = avio_rb32(pb); /* segment duration */
  5241. e->time = (int32_t)avio_rb32(pb); /* media time */
  5242. atom.size -= 8;
  5243. }
  5244. e->rate = avio_rb32(pb) / 65536.0;
  5245. atom.size -= 4;
  5246. av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
  5247. e->duration, e->time, e->rate);
  5248. if (e->time < 0 && e->time != -1 &&
  5249. c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
  5250. av_log(c->fc, AV_LOG_ERROR, "Track %d, edit %d: Invalid edit list media time=%"PRId64"\n",
  5251. c->fc->nb_streams-1, i, e->time);
  5252. return AVERROR_INVALIDDATA;
  5253. }
  5254. }
  5255. sc->elst_count = i;
  5256. return 0;
  5257. }
  5258. static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5259. {
  5260. MOVStreamContext *sc;
  5261. if (c->fc->nb_streams < 1)
  5262. return AVERROR_INVALIDDATA;
  5263. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  5264. sc->timecode_track = avio_rb32(pb);
  5265. return 0;
  5266. }
  5267. static int mov_read_vpcc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5268. {
  5269. AVStream *st;
  5270. int version, color_range, color_primaries, color_trc, color_space;
  5271. if (c->fc->nb_streams < 1)
  5272. return 0;
  5273. st = c->fc->streams[c->fc->nb_streams - 1];
  5274. if (atom.size < 5) {
  5275. av_log(c->fc, AV_LOG_ERROR, "Empty VP Codec Configuration box\n");
  5276. return AVERROR_INVALIDDATA;
  5277. }
  5278. version = avio_r8(pb);
  5279. if (version != 1) {
  5280. av_log(c->fc, AV_LOG_WARNING, "Unsupported VP Codec Configuration box version %d\n", version);
  5281. return 0;
  5282. }
  5283. avio_skip(pb, 3); /* flags */
  5284. avio_skip(pb, 2); /* profile + level */
  5285. color_range = avio_r8(pb); /* bitDepth, chromaSubsampling, videoFullRangeFlag */
  5286. color_primaries = avio_r8(pb);
  5287. color_trc = avio_r8(pb);
  5288. color_space = avio_r8(pb);
  5289. if (avio_rb16(pb)) /* codecIntializationDataSize */
  5290. return AVERROR_INVALIDDATA;
  5291. if (!av_color_primaries_name(color_primaries))
  5292. color_primaries = AVCOL_PRI_UNSPECIFIED;
  5293. if (!av_color_transfer_name(color_trc))
  5294. color_trc = AVCOL_TRC_UNSPECIFIED;
  5295. if (!av_color_space_name(color_space))
  5296. color_space = AVCOL_SPC_UNSPECIFIED;
  5297. st->codecpar->color_range = (color_range & 1) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  5298. st->codecpar->color_primaries = color_primaries;
  5299. st->codecpar->color_trc = color_trc;
  5300. st->codecpar->color_space = color_space;
  5301. return 0;
  5302. }
  5303. static int mov_read_smdm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5304. {
  5305. MOVStreamContext *sc;
  5306. int i, version;
  5307. if (c->fc->nb_streams < 1)
  5308. return AVERROR_INVALIDDATA;
  5309. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  5310. if (atom.size < 5) {
  5311. av_log(c->fc, AV_LOG_ERROR, "Empty Mastering Display Metadata box\n");
  5312. return AVERROR_INVALIDDATA;
  5313. }
  5314. version = avio_r8(pb);
  5315. if (version) {
  5316. av_log(c->fc, AV_LOG_WARNING, "Unsupported Mastering Display Metadata box version %d\n", version);
  5317. return 0;
  5318. }
  5319. if (sc->mastering) {
  5320. av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate Mastering Display Metadata\n");
  5321. return 0;
  5322. }
  5323. avio_skip(pb, 3); /* flags */
  5324. sc->mastering = av_mastering_display_metadata_alloc();
  5325. if (!sc->mastering)
  5326. return AVERROR(ENOMEM);
  5327. for (i = 0; i < 3; i++) {
  5328. sc->mastering->display_primaries[i][0] = av_make_q(avio_rb16(pb), 1 << 16);
  5329. sc->mastering->display_primaries[i][1] = av_make_q(avio_rb16(pb), 1 << 16);
  5330. }
  5331. sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), 1 << 16);
  5332. sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), 1 << 16);
  5333. sc->mastering->max_luminance = av_make_q(avio_rb32(pb), 1 << 8);
  5334. sc->mastering->min_luminance = av_make_q(avio_rb32(pb), 1 << 14);
  5335. sc->mastering->has_primaries = 1;
  5336. sc->mastering->has_luminance = 1;
  5337. return 0;
  5338. }
  5339. static int mov_read_mdcv(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5340. {
  5341. MOVStreamContext *sc;
  5342. const int mapping[3] = {1, 2, 0};
  5343. const int chroma_den = 50000;
  5344. const int luma_den = 10000;
  5345. int i;
  5346. if (c->fc->nb_streams < 1)
  5347. return AVERROR_INVALIDDATA;
  5348. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  5349. if (atom.size < 24) {
  5350. av_log(c->fc, AV_LOG_ERROR, "Invalid Mastering Display Color Volume box\n");
  5351. return AVERROR_INVALIDDATA;
  5352. }
  5353. if (sc->mastering) {
  5354. av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate Mastering Display Color Volume\n");
  5355. return 0;
  5356. }
  5357. sc->mastering = av_mastering_display_metadata_alloc();
  5358. if (!sc->mastering)
  5359. return AVERROR(ENOMEM);
  5360. for (i = 0; i < 3; i++) {
  5361. const int j = mapping[i];
  5362. sc->mastering->display_primaries[j][0] = av_make_q(avio_rb16(pb), chroma_den);
  5363. sc->mastering->display_primaries[j][1] = av_make_q(avio_rb16(pb), chroma_den);
  5364. }
  5365. sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), chroma_den);
  5366. sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), chroma_den);
  5367. sc->mastering->max_luminance = av_make_q(avio_rb32(pb), luma_den);
  5368. sc->mastering->min_luminance = av_make_q(avio_rb32(pb), luma_den);
  5369. sc->mastering->has_luminance = 1;
  5370. sc->mastering->has_primaries = 1;
  5371. return 0;
  5372. }
  5373. static int mov_read_coll(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5374. {
  5375. MOVStreamContext *sc;
  5376. int version;
  5377. if (c->fc->nb_streams < 1)
  5378. return AVERROR_INVALIDDATA;
  5379. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  5380. if (atom.size < 5) {
  5381. av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level box\n");
  5382. return AVERROR_INVALIDDATA;
  5383. }
  5384. version = avio_r8(pb);
  5385. if (version) {
  5386. av_log(c->fc, AV_LOG_WARNING, "Unsupported Content Light Level box version %d\n", version);
  5387. return 0;
  5388. }
  5389. avio_skip(pb, 3); /* flags */
  5390. if (sc->coll){
  5391. av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate COLL\n");
  5392. return 0;
  5393. }
  5394. sc->coll = av_content_light_metadata_alloc(&sc->coll_size);
  5395. if (!sc->coll)
  5396. return AVERROR(ENOMEM);
  5397. sc->coll->MaxCLL = avio_rb16(pb);
  5398. sc->coll->MaxFALL = avio_rb16(pb);
  5399. return 0;
  5400. }
  5401. static int mov_read_clli(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5402. {
  5403. MOVStreamContext *sc;
  5404. if (c->fc->nb_streams < 1)
  5405. return AVERROR_INVALIDDATA;
  5406. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  5407. if (atom.size < 4) {
  5408. av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level Info box\n");
  5409. return AVERROR_INVALIDDATA;
  5410. }
  5411. if (sc->coll){
  5412. av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate CLLI/COLL\n");
  5413. return 0;
  5414. }
  5415. sc->coll = av_content_light_metadata_alloc(&sc->coll_size);
  5416. if (!sc->coll)
  5417. return AVERROR(ENOMEM);
  5418. sc->coll->MaxCLL = avio_rb16(pb);
  5419. sc->coll->MaxFALL = avio_rb16(pb);
  5420. return 0;
  5421. }
  5422. static int mov_read_amve(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5423. {
  5424. MOVStreamContext *sc;
  5425. const int illuminance_den = 10000;
  5426. const int ambient_den = 50000;
  5427. if (c->fc->nb_streams < 1)
  5428. return AVERROR_INVALIDDATA;
  5429. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  5430. if (atom.size < 6) {
  5431. av_log(c->fc, AV_LOG_ERROR, "Empty Ambient Viewing Environment Info box\n");
  5432. return AVERROR_INVALIDDATA;
  5433. }
  5434. if (sc->ambient){
  5435. av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate AMVE\n");
  5436. return 0;
  5437. }
  5438. sc->ambient = av_ambient_viewing_environment_alloc(&sc->ambient_size);
  5439. if (!sc->ambient)
  5440. return AVERROR(ENOMEM);
  5441. sc->ambient->ambient_illuminance = av_make_q(avio_rb32(pb), illuminance_den);
  5442. sc->ambient->ambient_light_x = av_make_q(avio_rb16(pb), ambient_den);
  5443. sc->ambient->ambient_light_y = av_make_q(avio_rb16(pb), ambient_den);
  5444. return 0;
  5445. }
  5446. static int mov_read_st3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5447. {
  5448. AVStream *st;
  5449. MOVStreamContext *sc;
  5450. enum AVStereo3DType type;
  5451. int mode;
  5452. if (c->fc->nb_streams < 1)
  5453. return 0;
  5454. st = c->fc->streams[c->fc->nb_streams - 1];
  5455. sc = st->priv_data;
  5456. if (atom.size < 5) {
  5457. av_log(c->fc, AV_LOG_ERROR, "Empty stereoscopic video box\n");
  5458. return AVERROR_INVALIDDATA;
  5459. }
  5460. if (sc->stereo3d)
  5461. return AVERROR_INVALIDDATA;
  5462. avio_skip(pb, 4); /* version + flags */
  5463. mode = avio_r8(pb);
  5464. switch (mode) {
  5465. case 0:
  5466. type = AV_STEREO3D_2D;
  5467. break;
  5468. case 1:
  5469. type = AV_STEREO3D_TOPBOTTOM;
  5470. break;
  5471. case 2:
  5472. type = AV_STEREO3D_SIDEBYSIDE;
  5473. break;
  5474. default:
  5475. av_log(c->fc, AV_LOG_WARNING, "Unknown st3d mode value %d\n", mode);
  5476. return 0;
  5477. }
  5478. sc->stereo3d = av_stereo3d_alloc();
  5479. if (!sc->stereo3d)
  5480. return AVERROR(ENOMEM);
  5481. sc->stereo3d->type = type;
  5482. return 0;
  5483. }
  5484. static int mov_read_sv3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5485. {
  5486. AVStream *st;
  5487. MOVStreamContext *sc;
  5488. int size, version, layout;
  5489. int32_t yaw, pitch, roll;
  5490. uint32_t l = 0, t = 0, r = 0, b = 0;
  5491. uint32_t tag, padding = 0;
  5492. enum AVSphericalProjection projection;
  5493. if (c->fc->nb_streams < 1)
  5494. return 0;
  5495. st = c->fc->streams[c->fc->nb_streams - 1];
  5496. sc = st->priv_data;
  5497. if (atom.size < 8) {
  5498. av_log(c->fc, AV_LOG_ERROR, "Empty spherical video box\n");
  5499. return AVERROR_INVALIDDATA;
  5500. }
  5501. size = avio_rb32(pb);
  5502. if (size <= 12 || size > atom.size)
  5503. return AVERROR_INVALIDDATA;
  5504. tag = avio_rl32(pb);
  5505. if (tag != MKTAG('s','v','h','d')) {
  5506. av_log(c->fc, AV_LOG_ERROR, "Missing spherical video header\n");
  5507. return 0;
  5508. }
  5509. version = avio_r8(pb);
  5510. if (version != 0) {
  5511. av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
  5512. version);
  5513. return 0;
  5514. }
  5515. avio_skip(pb, 3); /* flags */
  5516. avio_skip(pb, size - 12); /* metadata_source */
  5517. size = avio_rb32(pb);
  5518. if (size > atom.size)
  5519. return AVERROR_INVALIDDATA;
  5520. tag = avio_rl32(pb);
  5521. if (tag != MKTAG('p','r','o','j')) {
  5522. av_log(c->fc, AV_LOG_ERROR, "Missing projection box\n");
  5523. return 0;
  5524. }
  5525. size = avio_rb32(pb);
  5526. if (size > atom.size)
  5527. return AVERROR_INVALIDDATA;
  5528. tag = avio_rl32(pb);
  5529. if (tag != MKTAG('p','r','h','d')) {
  5530. av_log(c->fc, AV_LOG_ERROR, "Missing projection header box\n");
  5531. return 0;
  5532. }
  5533. version = avio_r8(pb);
  5534. if (version != 0) {
  5535. av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
  5536. version);
  5537. return 0;
  5538. }
  5539. avio_skip(pb, 3); /* flags */
  5540. /* 16.16 fixed point */
  5541. yaw = avio_rb32(pb);
  5542. pitch = avio_rb32(pb);
  5543. roll = avio_rb32(pb);
  5544. size = avio_rb32(pb);
  5545. if (size > atom.size)
  5546. return AVERROR_INVALIDDATA;
  5547. tag = avio_rl32(pb);
  5548. version = avio_r8(pb);
  5549. if (version != 0) {
  5550. av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
  5551. version);
  5552. return 0;
  5553. }
  5554. avio_skip(pb, 3); /* flags */
  5555. switch (tag) {
  5556. case MKTAG('c','b','m','p'):
  5557. layout = avio_rb32(pb);
  5558. if (layout) {
  5559. av_log(c->fc, AV_LOG_WARNING,
  5560. "Unsupported cubemap layout %d\n", layout);
  5561. return 0;
  5562. }
  5563. projection = AV_SPHERICAL_CUBEMAP;
  5564. padding = avio_rb32(pb);
  5565. break;
  5566. case MKTAG('e','q','u','i'):
  5567. t = avio_rb32(pb);
  5568. b = avio_rb32(pb);
  5569. l = avio_rb32(pb);
  5570. r = avio_rb32(pb);
  5571. if (b >= UINT_MAX - t || r >= UINT_MAX - l) {
  5572. av_log(c->fc, AV_LOG_ERROR,
  5573. "Invalid bounding rectangle coordinates "
  5574. "%"PRIu32",%"PRIu32",%"PRIu32",%"PRIu32"\n", l, t, r, b);
  5575. return AVERROR_INVALIDDATA;
  5576. }
  5577. if (l || t || r || b)
  5578. projection = AV_SPHERICAL_EQUIRECTANGULAR_TILE;
  5579. else
  5580. projection = AV_SPHERICAL_EQUIRECTANGULAR;
  5581. break;
  5582. default:
  5583. av_log(c->fc, AV_LOG_ERROR, "Unknown projection type: %s\n", av_fourcc2str(tag));
  5584. return 0;
  5585. }
  5586. sc->spherical = av_spherical_alloc(&sc->spherical_size);
  5587. if (!sc->spherical)
  5588. return AVERROR(ENOMEM);
  5589. sc->spherical->projection = projection;
  5590. sc->spherical->yaw = yaw;
  5591. sc->spherical->pitch = pitch;
  5592. sc->spherical->roll = roll;
  5593. sc->spherical->padding = padding;
  5594. sc->spherical->bound_left = l;
  5595. sc->spherical->bound_top = t;
  5596. sc->spherical->bound_right = r;
  5597. sc->spherical->bound_bottom = b;
  5598. return 0;
  5599. }
  5600. static int mov_parse_uuid_spherical(MOVStreamContext *sc, AVIOContext *pb, size_t len)
  5601. {
  5602. int ret = 0;
  5603. uint8_t *buffer = av_malloc(len + 1);
  5604. const char *val;
  5605. if (!buffer)
  5606. return AVERROR(ENOMEM);
  5607. buffer[len] = '\0';
  5608. ret = ffio_read_size(pb, buffer, len);
  5609. if (ret < 0)
  5610. goto out;
  5611. /* Check for mandatory keys and values, try to support XML as best-effort */
  5612. if (!sc->spherical &&
  5613. av_stristr(buffer, "<GSpherical:StitchingSoftware>") &&
  5614. (val = av_stristr(buffer, "<GSpherical:Spherical>")) &&
  5615. av_stristr(val, "true") &&
  5616. (val = av_stristr(buffer, "<GSpherical:Stitched>")) &&
  5617. av_stristr(val, "true") &&
  5618. (val = av_stristr(buffer, "<GSpherical:ProjectionType>")) &&
  5619. av_stristr(val, "equirectangular")) {
  5620. sc->spherical = av_spherical_alloc(&sc->spherical_size);
  5621. if (!sc->spherical)
  5622. goto out;
  5623. sc->spherical->projection = AV_SPHERICAL_EQUIRECTANGULAR;
  5624. if (av_stristr(buffer, "<GSpherical:StereoMode>") && !sc->stereo3d) {
  5625. enum AVStereo3DType mode;
  5626. if (av_stristr(buffer, "left-right"))
  5627. mode = AV_STEREO3D_SIDEBYSIDE;
  5628. else if (av_stristr(buffer, "top-bottom"))
  5629. mode = AV_STEREO3D_TOPBOTTOM;
  5630. else
  5631. mode = AV_STEREO3D_2D;
  5632. sc->stereo3d = av_stereo3d_alloc();
  5633. if (!sc->stereo3d)
  5634. goto out;
  5635. sc->stereo3d->type = mode;
  5636. }
  5637. /* orientation */
  5638. val = av_stristr(buffer, "<GSpherical:InitialViewHeadingDegrees>");
  5639. if (val)
  5640. sc->spherical->yaw = strtol(val, NULL, 10) * (1 << 16);
  5641. val = av_stristr(buffer, "<GSpherical:InitialViewPitchDegrees>");
  5642. if (val)
  5643. sc->spherical->pitch = strtol(val, NULL, 10) * (1 << 16);
  5644. val = av_stristr(buffer, "<GSpherical:InitialViewRollDegrees>");
  5645. if (val)
  5646. sc->spherical->roll = strtol(val, NULL, 10) * (1 << 16);
  5647. }
  5648. out:
  5649. av_free(buffer);
  5650. return ret;
  5651. }
  5652. static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5653. {
  5654. AVStream *st;
  5655. MOVStreamContext *sc;
  5656. int64_t ret;
  5657. AVUUID uuid;
  5658. static const AVUUID uuid_isml_manifest = {
  5659. 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
  5660. 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
  5661. };
  5662. static const AVUUID uuid_xmp = {
  5663. 0xbe, 0x7a, 0xcf, 0xcb, 0x97, 0xa9, 0x42, 0xe8,
  5664. 0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac
  5665. };
  5666. static const AVUUID uuid_spherical = {
  5667. 0xff, 0xcc, 0x82, 0x63, 0xf8, 0x55, 0x4a, 0x93,
  5668. 0x88, 0x14, 0x58, 0x7a, 0x02, 0x52, 0x1f, 0xdd,
  5669. };
  5670. if (atom.size < AV_UUID_LEN || atom.size >= FFMIN(INT_MAX, SIZE_MAX))
  5671. return AVERROR_INVALIDDATA;
  5672. if (c->fc->nb_streams < 1)
  5673. return 0;
  5674. st = c->fc->streams[c->fc->nb_streams - 1];
  5675. sc = st->priv_data;
  5676. ret = ffio_read_size(pb, uuid, AV_UUID_LEN);
  5677. if (ret < 0)
  5678. return ret;
  5679. if (av_uuid_equal(uuid, uuid_isml_manifest)) {
  5680. uint8_t *buffer, *ptr;
  5681. char *endptr;
  5682. size_t len = atom.size - AV_UUID_LEN;
  5683. if (len < 4) {
  5684. return AVERROR_INVALIDDATA;
  5685. }
  5686. ret = avio_skip(pb, 4); // zeroes
  5687. len -= 4;
  5688. buffer = av_mallocz(len + 1);
  5689. if (!buffer) {
  5690. return AVERROR(ENOMEM);
  5691. }
  5692. ret = ffio_read_size(pb, buffer, len);
  5693. if (ret < 0) {
  5694. av_free(buffer);
  5695. return ret;
  5696. }
  5697. ptr = buffer;
  5698. while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
  5699. ptr += sizeof("systemBitrate=\"") - 1;
  5700. c->bitrates_count++;
  5701. c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
  5702. if (!c->bitrates) {
  5703. c->bitrates_count = 0;
  5704. av_free(buffer);
  5705. return AVERROR(ENOMEM);
  5706. }
  5707. errno = 0;
  5708. ret = strtol(ptr, &endptr, 10);
  5709. if (ret < 0 || errno || *endptr != '"') {
  5710. c->bitrates[c->bitrates_count - 1] = 0;
  5711. } else {
  5712. c->bitrates[c->bitrates_count - 1] = ret;
  5713. }
  5714. }
  5715. av_free(buffer);
  5716. } else if (av_uuid_equal(uuid, uuid_xmp)) {
  5717. uint8_t *buffer;
  5718. size_t len = atom.size - AV_UUID_LEN;
  5719. if (c->export_xmp) {
  5720. buffer = av_mallocz(len + 1);
  5721. if (!buffer) {
  5722. return AVERROR(ENOMEM);
  5723. }
  5724. ret = ffio_read_size(pb, buffer, len);
  5725. if (ret < 0) {
  5726. av_free(buffer);
  5727. return ret;
  5728. }
  5729. buffer[len] = '\0';
  5730. av_dict_set(&c->fc->metadata, "xmp",
  5731. buffer, AV_DICT_DONT_STRDUP_VAL);
  5732. } else {
  5733. // skip all uuid atom, which makes it fast for long uuid-xmp file
  5734. ret = avio_skip(pb, len);
  5735. if (ret < 0)
  5736. return ret;
  5737. }
  5738. } else if (av_uuid_equal(uuid, uuid_spherical)) {
  5739. size_t len = atom.size - AV_UUID_LEN;
  5740. ret = mov_parse_uuid_spherical(sc, pb, len);
  5741. if (ret < 0)
  5742. return ret;
  5743. if (!sc->spherical)
  5744. av_log(c->fc, AV_LOG_WARNING, "Invalid spherical metadata found\n");
  5745. }
  5746. return 0;
  5747. }
  5748. static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5749. {
  5750. int ret;
  5751. uint8_t content[16];
  5752. if (atom.size < 8)
  5753. return 0;
  5754. ret = avio_read(pb, content, FFMIN(sizeof(content), atom.size));
  5755. if (ret < 0)
  5756. return ret;
  5757. if ( !c->found_moov
  5758. && !c->found_mdat
  5759. && !memcmp(content, "Anevia\x1A\x1A", 8)
  5760. && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
  5761. c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
  5762. }
  5763. return 0;
  5764. }
  5765. static int mov_read_frma(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5766. {
  5767. uint32_t format = avio_rl32(pb);
  5768. MOVStreamContext *sc;
  5769. enum AVCodecID id;
  5770. AVStream *st;
  5771. if (c->fc->nb_streams < 1)
  5772. return 0;
  5773. st = c->fc->streams[c->fc->nb_streams - 1];
  5774. sc = st->priv_data;
  5775. switch (sc->format)
  5776. {
  5777. case MKTAG('e','n','c','v'): // encrypted video
  5778. case MKTAG('e','n','c','a'): // encrypted audio
  5779. id = mov_codec_id(st, format);
  5780. if (st->codecpar->codec_id != AV_CODEC_ID_NONE &&
  5781. st->codecpar->codec_id != id) {
  5782. av_log(c->fc, AV_LOG_WARNING,
  5783. "ignoring 'frma' atom of '%.4s', stream has codec id %d\n",
  5784. (char*)&format, st->codecpar->codec_id);
  5785. break;
  5786. }
  5787. st->codecpar->codec_id = id;
  5788. sc->format = format;
  5789. break;
  5790. default:
  5791. if (format != sc->format) {
  5792. av_log(c->fc, AV_LOG_WARNING,
  5793. "ignoring 'frma' atom of '%.4s', stream format is '%.4s'\n",
  5794. (char*)&format, (char*)&sc->format);
  5795. }
  5796. break;
  5797. }
  5798. return 0;
  5799. }
  5800. /**
  5801. * Gets the current encryption info and associated current stream context. If
  5802. * we are parsing a track fragment, this will return the specific encryption
  5803. * info for this fragment; otherwise this will return the global encryption
  5804. * info for the current stream.
  5805. */
  5806. static int get_current_encryption_info(MOVContext *c, MOVEncryptionIndex **encryption_index, MOVStreamContext **sc)
  5807. {
  5808. MOVFragmentStreamInfo *frag_stream_info;
  5809. AVStream *st;
  5810. int i;
  5811. frag_stream_info = get_current_frag_stream_info(&c->frag_index);
  5812. if (frag_stream_info) {
  5813. for (i = 0; i < c->fc->nb_streams; i++) {
  5814. *sc = c->fc->streams[i]->priv_data;
  5815. if ((*sc)->id == frag_stream_info->id) {
  5816. st = c->fc->streams[i];
  5817. break;
  5818. }
  5819. }
  5820. if (i == c->fc->nb_streams)
  5821. return 0;
  5822. *sc = st->priv_data;
  5823. if (!frag_stream_info->encryption_index) {
  5824. // If this stream isn't encrypted, don't create the index.
  5825. if (!(*sc)->cenc.default_encrypted_sample)
  5826. return 0;
  5827. frag_stream_info->encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
  5828. if (!frag_stream_info->encryption_index)
  5829. return AVERROR(ENOMEM);
  5830. }
  5831. *encryption_index = frag_stream_info->encryption_index;
  5832. return 1;
  5833. } else {
  5834. // No current track fragment, using stream level encryption info.
  5835. if (c->fc->nb_streams < 1)
  5836. return 0;
  5837. st = c->fc->streams[c->fc->nb_streams - 1];
  5838. *sc = st->priv_data;
  5839. if (!(*sc)->cenc.encryption_index) {
  5840. // If this stream isn't encrypted, don't create the index.
  5841. if (!(*sc)->cenc.default_encrypted_sample)
  5842. return 0;
  5843. (*sc)->cenc.encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
  5844. if (!(*sc)->cenc.encryption_index)
  5845. return AVERROR(ENOMEM);
  5846. }
  5847. *encryption_index = (*sc)->cenc.encryption_index;
  5848. return 1;
  5849. }
  5850. }
  5851. static int mov_read_sample_encryption_info(MOVContext *c, AVIOContext *pb, MOVStreamContext *sc, AVEncryptionInfo **sample, int use_subsamples)
  5852. {
  5853. int i, ret;
  5854. unsigned int subsample_count;
  5855. AVSubsampleEncryptionInfo *subsamples;
  5856. if (!sc->cenc.default_encrypted_sample) {
  5857. av_log(c->fc, AV_LOG_ERROR, "Missing schm or tenc\n");
  5858. return AVERROR_INVALIDDATA;
  5859. }
  5860. if (sc->cenc.per_sample_iv_size || use_subsamples) {
  5861. *sample = av_encryption_info_clone(sc->cenc.default_encrypted_sample);
  5862. if (!*sample)
  5863. return AVERROR(ENOMEM);
  5864. } else
  5865. *sample = NULL;
  5866. if (sc->cenc.per_sample_iv_size != 0) {
  5867. if ((ret = ffio_read_size(pb, (*sample)->iv, sc->cenc.per_sample_iv_size)) < 0) {
  5868. av_log(c->fc, AV_LOG_ERROR, "failed to read the initialization vector\n");
  5869. av_encryption_info_free(*sample);
  5870. *sample = NULL;
  5871. return ret;
  5872. }
  5873. }
  5874. if (use_subsamples) {
  5875. subsample_count = avio_rb16(pb);
  5876. av_free((*sample)->subsamples);
  5877. (*sample)->subsamples = av_calloc(subsample_count, sizeof(*subsamples));
  5878. if (!(*sample)->subsamples) {
  5879. av_encryption_info_free(*sample);
  5880. *sample = NULL;
  5881. return AVERROR(ENOMEM);
  5882. }
  5883. for (i = 0; i < subsample_count && !pb->eof_reached; i++) {
  5884. (*sample)->subsamples[i].bytes_of_clear_data = avio_rb16(pb);
  5885. (*sample)->subsamples[i].bytes_of_protected_data = avio_rb32(pb);
  5886. }
  5887. if (pb->eof_reached) {
  5888. av_log(c->fc, AV_LOG_ERROR, "hit EOF while reading sub-sample encryption info\n");
  5889. av_encryption_info_free(*sample);
  5890. *sample = NULL;
  5891. return AVERROR_INVALIDDATA;
  5892. }
  5893. (*sample)->subsample_count = subsample_count;
  5894. }
  5895. return 0;
  5896. }
  5897. static int mov_read_senc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  5898. {
  5899. AVEncryptionInfo **encrypted_samples;
  5900. MOVEncryptionIndex *encryption_index;
  5901. MOVStreamContext *sc;
  5902. int use_subsamples, ret;
  5903. unsigned int sample_count, i, alloc_size = 0;
  5904. ret = get_current_encryption_info(c, &encryption_index, &sc);
  5905. if (ret != 1)
  5906. return ret;
  5907. if (encryption_index->nb_encrypted_samples) {
  5908. // This can happen if we have both saio/saiz and senc atoms.
  5909. av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in senc\n");
  5910. return 0;
  5911. }
  5912. avio_r8(pb); /* version */
  5913. use_subsamples = avio_rb24(pb) & 0x02; /* flags */
  5914. sample_count = avio_rb32(pb);
  5915. if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
  5916. return AVERROR(ENOMEM);
  5917. for (i = 0; i < sample_count; i++) {
  5918. unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
  5919. encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
  5920. min_samples * sizeof(*encrypted_samples));
  5921. if (encrypted_samples) {
  5922. encryption_index->encrypted_samples = encrypted_samples;
  5923. ret = mov_read_sample_encryption_info(
  5924. c, pb, sc, &encryption_index->encrypted_samples[i], use_subsamples);
  5925. } else {
  5926. ret = AVERROR(ENOMEM);
  5927. }
  5928. if (pb->eof_reached) {
  5929. av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading senc\n");
  5930. if (ret >= 0)
  5931. av_encryption_info_free(encryption_index->encrypted_samples[i]);
  5932. ret = AVERROR_INVALIDDATA;
  5933. }
  5934. if (ret < 0) {
  5935. for (; i > 0; i--)
  5936. av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
  5937. av_freep(&encryption_index->encrypted_samples);
  5938. return ret;
  5939. }
  5940. }
  5941. encryption_index->nb_encrypted_samples = sample_count;
  5942. return 0;
  5943. }
  5944. static int mov_parse_auxiliary_info(MOVContext *c, MOVStreamContext *sc, AVIOContext *pb, MOVEncryptionIndex *encryption_index)
  5945. {
  5946. AVEncryptionInfo **sample, **encrypted_samples;
  5947. int64_t prev_pos;
  5948. size_t sample_count, sample_info_size, i;
  5949. int ret = 0;
  5950. unsigned int alloc_size = 0;
  5951. if (encryption_index->nb_encrypted_samples)
  5952. return 0;
  5953. sample_count = encryption_index->auxiliary_info_sample_count;
  5954. if (encryption_index->auxiliary_offsets_count != 1) {
  5955. av_log(c->fc, AV_LOG_ERROR, "Multiple auxiliary info chunks are not supported\n");
  5956. return AVERROR_PATCHWELCOME;
  5957. }
  5958. if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
  5959. return AVERROR(ENOMEM);
  5960. prev_pos = avio_tell(pb);
  5961. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) ||
  5962. avio_seek(pb, encryption_index->auxiliary_offsets[0], SEEK_SET) != encryption_index->auxiliary_offsets[0]) {
  5963. av_log(c->fc, AV_LOG_INFO, "Failed to seek for auxiliary info, will only parse senc atoms for encryption info\n");
  5964. goto finish;
  5965. }
  5966. for (i = 0; i < sample_count && !pb->eof_reached; i++) {
  5967. unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
  5968. encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
  5969. min_samples * sizeof(*encrypted_samples));
  5970. if (!encrypted_samples) {
  5971. ret = AVERROR(ENOMEM);
  5972. goto finish;
  5973. }
  5974. encryption_index->encrypted_samples = encrypted_samples;
  5975. sample = &encryption_index->encrypted_samples[i];
  5976. sample_info_size = encryption_index->auxiliary_info_default_size
  5977. ? encryption_index->auxiliary_info_default_size
  5978. : encryption_index->auxiliary_info_sizes[i];
  5979. ret = mov_read_sample_encryption_info(c, pb, sc, sample, sample_info_size > sc->cenc.per_sample_iv_size);
  5980. if (ret < 0)
  5981. goto finish;
  5982. }
  5983. if (pb->eof_reached) {
  5984. av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading auxiliary info\n");
  5985. ret = AVERROR_INVALIDDATA;
  5986. } else {
  5987. encryption_index->nb_encrypted_samples = sample_count;
  5988. }
  5989. finish:
  5990. avio_seek(pb, prev_pos, SEEK_SET);
  5991. if (ret < 0) {
  5992. for (; i > 0; i--) {
  5993. av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
  5994. }
  5995. av_freep(&encryption_index->encrypted_samples);
  5996. }
  5997. return ret;
  5998. }
  5999. static int mov_read_saiz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6000. {
  6001. MOVEncryptionIndex *encryption_index;
  6002. MOVStreamContext *sc;
  6003. int ret;
  6004. unsigned int sample_count, aux_info_type, aux_info_param;
  6005. ret = get_current_encryption_info(c, &encryption_index, &sc);
  6006. if (ret != 1)
  6007. return ret;
  6008. if (encryption_index->nb_encrypted_samples) {
  6009. // This can happen if we have both saio/saiz and senc atoms.
  6010. av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saiz\n");
  6011. return 0;
  6012. }
  6013. if (encryption_index->auxiliary_info_sample_count) {
  6014. av_log(c->fc, AV_LOG_ERROR, "Duplicate saiz atom\n");
  6015. return AVERROR_INVALIDDATA;
  6016. }
  6017. avio_r8(pb); /* version */
  6018. if (avio_rb24(pb) & 0x01) { /* flags */
  6019. aux_info_type = avio_rb32(pb);
  6020. aux_info_param = avio_rb32(pb);
  6021. if (sc->cenc.default_encrypted_sample) {
  6022. if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
  6023. av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type\n");
  6024. return 0;
  6025. }
  6026. if (aux_info_param != 0) {
  6027. av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type_parameter\n");
  6028. return 0;
  6029. }
  6030. } else {
  6031. // Didn't see 'schm' or 'tenc', so this isn't encrypted.
  6032. if ((aux_info_type == MKBETAG('c','e','n','c') ||
  6033. aux_info_type == MKBETAG('c','e','n','s') ||
  6034. aux_info_type == MKBETAG('c','b','c','1') ||
  6035. aux_info_type == MKBETAG('c','b','c','s')) &&
  6036. aux_info_param == 0) {
  6037. av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saiz without schm/tenc\n");
  6038. return AVERROR_INVALIDDATA;
  6039. } else {
  6040. return 0;
  6041. }
  6042. }
  6043. } else if (!sc->cenc.default_encrypted_sample) {
  6044. // Didn't see 'schm' or 'tenc', so this isn't encrypted.
  6045. return 0;
  6046. }
  6047. encryption_index->auxiliary_info_default_size = avio_r8(pb);
  6048. sample_count = avio_rb32(pb);
  6049. if (encryption_index->auxiliary_info_default_size == 0) {
  6050. if (sample_count == 0)
  6051. return AVERROR_INVALIDDATA;
  6052. encryption_index->auxiliary_info_sizes = av_malloc(sample_count);
  6053. if (!encryption_index->auxiliary_info_sizes)
  6054. return AVERROR(ENOMEM);
  6055. ret = avio_read(pb, encryption_index->auxiliary_info_sizes, sample_count);
  6056. if (ret != sample_count) {
  6057. av_freep(&encryption_index->auxiliary_info_sizes);
  6058. if (ret >= 0)
  6059. ret = AVERROR_INVALIDDATA;
  6060. av_log(c->fc, AV_LOG_ERROR, "Failed to read the auxiliary info, %s\n",
  6061. av_err2str(ret));
  6062. return ret;
  6063. }
  6064. }
  6065. encryption_index->auxiliary_info_sample_count = sample_count;
  6066. if (encryption_index->auxiliary_offsets_count) {
  6067. return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
  6068. }
  6069. return 0;
  6070. }
  6071. static int mov_read_saio(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6072. {
  6073. uint64_t *auxiliary_offsets;
  6074. MOVEncryptionIndex *encryption_index;
  6075. MOVStreamContext *sc;
  6076. int i, ret;
  6077. unsigned int version, entry_count, aux_info_type, aux_info_param;
  6078. unsigned int alloc_size = 0;
  6079. ret = get_current_encryption_info(c, &encryption_index, &sc);
  6080. if (ret != 1)
  6081. return ret;
  6082. if (encryption_index->nb_encrypted_samples) {
  6083. // This can happen if we have both saio/saiz and senc atoms.
  6084. av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saio\n");
  6085. return 0;
  6086. }
  6087. if (encryption_index->auxiliary_offsets_count) {
  6088. av_log(c->fc, AV_LOG_ERROR, "Duplicate saio atom\n");
  6089. return AVERROR_INVALIDDATA;
  6090. }
  6091. version = avio_r8(pb); /* version */
  6092. if (avio_rb24(pb) & 0x01) { /* flags */
  6093. aux_info_type = avio_rb32(pb);
  6094. aux_info_param = avio_rb32(pb);
  6095. if (sc->cenc.default_encrypted_sample) {
  6096. if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
  6097. av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type\n");
  6098. return 0;
  6099. }
  6100. if (aux_info_param != 0) {
  6101. av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type_parameter\n");
  6102. return 0;
  6103. }
  6104. } else {
  6105. // Didn't see 'schm' or 'tenc', so this isn't encrypted.
  6106. if ((aux_info_type == MKBETAG('c','e','n','c') ||
  6107. aux_info_type == MKBETAG('c','e','n','s') ||
  6108. aux_info_type == MKBETAG('c','b','c','1') ||
  6109. aux_info_type == MKBETAG('c','b','c','s')) &&
  6110. aux_info_param == 0) {
  6111. av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saio without schm/tenc\n");
  6112. return AVERROR_INVALIDDATA;
  6113. } else {
  6114. return 0;
  6115. }
  6116. }
  6117. } else if (!sc->cenc.default_encrypted_sample) {
  6118. // Didn't see 'schm' or 'tenc', so this isn't encrypted.
  6119. return 0;
  6120. }
  6121. entry_count = avio_rb32(pb);
  6122. if (entry_count >= INT_MAX / sizeof(*auxiliary_offsets))
  6123. return AVERROR(ENOMEM);
  6124. for (i = 0; i < entry_count && !pb->eof_reached; i++) {
  6125. unsigned int min_offsets = FFMIN(FFMAX(i + 1, 1024), entry_count);
  6126. auxiliary_offsets = av_fast_realloc(
  6127. encryption_index->auxiliary_offsets, &alloc_size,
  6128. min_offsets * sizeof(*auxiliary_offsets));
  6129. if (!auxiliary_offsets) {
  6130. av_freep(&encryption_index->auxiliary_offsets);
  6131. return AVERROR(ENOMEM);
  6132. }
  6133. encryption_index->auxiliary_offsets = auxiliary_offsets;
  6134. if (version == 0) {
  6135. encryption_index->auxiliary_offsets[i] = avio_rb32(pb);
  6136. } else {
  6137. encryption_index->auxiliary_offsets[i] = avio_rb64(pb);
  6138. }
  6139. if (c->frag_index.current >= 0) {
  6140. encryption_index->auxiliary_offsets[i] += c->fragment.base_data_offset;
  6141. }
  6142. }
  6143. if (pb->eof_reached) {
  6144. av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading saio\n");
  6145. av_freep(&encryption_index->auxiliary_offsets);
  6146. return AVERROR_INVALIDDATA;
  6147. }
  6148. encryption_index->auxiliary_offsets_count = entry_count;
  6149. if (encryption_index->auxiliary_info_sample_count) {
  6150. return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
  6151. }
  6152. return 0;
  6153. }
  6154. static int mov_read_pssh(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6155. {
  6156. AVEncryptionInitInfo *info, *old_init_info;
  6157. uint8_t **key_ids;
  6158. AVStream *st;
  6159. const AVPacketSideData *old_side_data;
  6160. uint8_t *side_data, *extra_data;
  6161. size_t side_data_size;
  6162. int ret = 0;
  6163. unsigned int version, kid_count, extra_data_size, alloc_size = 0;
  6164. if (c->fc->nb_streams < 1)
  6165. return 0;
  6166. st = c->fc->streams[c->fc->nb_streams-1];
  6167. version = avio_r8(pb); /* version */
  6168. avio_rb24(pb); /* flags */
  6169. info = av_encryption_init_info_alloc(/* system_id_size */ 16, /* num_key_ids */ 0,
  6170. /* key_id_size */ 16, /* data_size */ 0);
  6171. if (!info)
  6172. return AVERROR(ENOMEM);
  6173. if ((ret = ffio_read_size(pb, info->system_id, 16)) < 0) {
  6174. av_log(c->fc, AV_LOG_ERROR, "Failed to read the system id\n");
  6175. goto finish;
  6176. }
  6177. if (version > 0) {
  6178. kid_count = avio_rb32(pb);
  6179. if (kid_count >= INT_MAX / sizeof(*key_ids)) {
  6180. ret = AVERROR(ENOMEM);
  6181. goto finish;
  6182. }
  6183. for (unsigned int i = 0; i < kid_count && !pb->eof_reached; i++) {
  6184. unsigned int min_kid_count = FFMIN(FFMAX(i + 1, 1024), kid_count);
  6185. key_ids = av_fast_realloc(info->key_ids, &alloc_size,
  6186. min_kid_count * sizeof(*key_ids));
  6187. if (!key_ids) {
  6188. ret = AVERROR(ENOMEM);
  6189. goto finish;
  6190. }
  6191. info->key_ids = key_ids;
  6192. info->key_ids[i] = av_mallocz(16);
  6193. if (!info->key_ids[i]) {
  6194. ret = AVERROR(ENOMEM);
  6195. goto finish;
  6196. }
  6197. info->num_key_ids = i + 1;
  6198. if ((ret = ffio_read_size(pb, info->key_ids[i], 16)) < 0) {
  6199. av_log(c->fc, AV_LOG_ERROR, "Failed to read the key id\n");
  6200. goto finish;
  6201. }
  6202. }
  6203. if (pb->eof_reached) {
  6204. av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading pssh\n");
  6205. ret = AVERROR_INVALIDDATA;
  6206. goto finish;
  6207. }
  6208. }
  6209. extra_data_size = avio_rb32(pb);
  6210. extra_data = av_malloc(extra_data_size);
  6211. if (!extra_data) {
  6212. ret = AVERROR(ENOMEM);
  6213. goto finish;
  6214. }
  6215. ret = avio_read(pb, extra_data, extra_data_size);
  6216. if (ret != extra_data_size) {
  6217. av_free(extra_data);
  6218. if (ret >= 0)
  6219. ret = AVERROR_INVALIDDATA;
  6220. goto finish;
  6221. }
  6222. av_freep(&info->data); // malloc(0) may still allocate something.
  6223. info->data = extra_data;
  6224. info->data_size = extra_data_size;
  6225. // If there is existing initialization data, append to the list.
  6226. old_side_data = av_packet_side_data_get(st->codecpar->coded_side_data, st->codecpar->nb_coded_side_data,
  6227. AV_PKT_DATA_ENCRYPTION_INIT_INFO);
  6228. if (old_side_data) {
  6229. old_init_info = av_encryption_init_info_get_side_data(old_side_data->data, old_side_data->size);
  6230. if (old_init_info) {
  6231. // Append to the end of the list.
  6232. for (AVEncryptionInitInfo *cur = old_init_info;; cur = cur->next) {
  6233. if (!cur->next) {
  6234. cur->next = info;
  6235. break;
  6236. }
  6237. }
  6238. info = old_init_info;
  6239. } else {
  6240. // Assume existing side-data will be valid, so the only error we could get is OOM.
  6241. ret = AVERROR(ENOMEM);
  6242. goto finish;
  6243. }
  6244. }
  6245. side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
  6246. if (!side_data) {
  6247. ret = AVERROR(ENOMEM);
  6248. goto finish;
  6249. }
  6250. if (!av_packet_side_data_add(&st->codecpar->coded_side_data,
  6251. &st->codecpar->nb_coded_side_data,
  6252. AV_PKT_DATA_ENCRYPTION_INIT_INFO,
  6253. side_data, side_data_size, 0))
  6254. av_free(side_data);
  6255. finish:
  6256. av_encryption_init_info_free(info);
  6257. return ret;
  6258. }
  6259. static int mov_read_schm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6260. {
  6261. AVStream *st;
  6262. MOVStreamContext *sc;
  6263. if (c->fc->nb_streams < 1)
  6264. return 0;
  6265. st = c->fc->streams[c->fc->nb_streams-1];
  6266. sc = st->priv_data;
  6267. if (sc->pseudo_stream_id != 0) {
  6268. av_log(c->fc, AV_LOG_ERROR, "schm boxes are only supported in first sample descriptor\n");
  6269. return AVERROR_PATCHWELCOME;
  6270. }
  6271. if (atom.size < 8)
  6272. return AVERROR_INVALIDDATA;
  6273. avio_rb32(pb); /* version and flags */
  6274. if (!sc->cenc.default_encrypted_sample) {
  6275. sc->cenc.default_encrypted_sample = av_encryption_info_alloc(0, 16, 16);
  6276. if (!sc->cenc.default_encrypted_sample) {
  6277. return AVERROR(ENOMEM);
  6278. }
  6279. }
  6280. sc->cenc.default_encrypted_sample->scheme = avio_rb32(pb);
  6281. return 0;
  6282. }
  6283. static int mov_read_tenc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6284. {
  6285. AVStream *st;
  6286. MOVStreamContext *sc;
  6287. unsigned int version, pattern, is_protected, iv_size;
  6288. if (c->fc->nb_streams < 1)
  6289. return 0;
  6290. st = c->fc->streams[c->fc->nb_streams-1];
  6291. sc = st->priv_data;
  6292. if (sc->pseudo_stream_id != 0) {
  6293. av_log(c->fc, AV_LOG_ERROR, "tenc atom are only supported in first sample descriptor\n");
  6294. return AVERROR_PATCHWELCOME;
  6295. }
  6296. if (!sc->cenc.default_encrypted_sample) {
  6297. sc->cenc.default_encrypted_sample = av_encryption_info_alloc(0, 16, 16);
  6298. if (!sc->cenc.default_encrypted_sample) {
  6299. return AVERROR(ENOMEM);
  6300. }
  6301. }
  6302. if (atom.size < 20)
  6303. return AVERROR_INVALIDDATA;
  6304. version = avio_r8(pb); /* version */
  6305. avio_rb24(pb); /* flags */
  6306. avio_r8(pb); /* reserved */
  6307. pattern = avio_r8(pb);
  6308. if (version > 0) {
  6309. sc->cenc.default_encrypted_sample->crypt_byte_block = pattern >> 4;
  6310. sc->cenc.default_encrypted_sample->skip_byte_block = pattern & 0xf;
  6311. }
  6312. is_protected = avio_r8(pb);
  6313. if (is_protected && !sc->cenc.encryption_index) {
  6314. // The whole stream should be by-default encrypted.
  6315. sc->cenc.encryption_index = av_mallocz(sizeof(MOVEncryptionIndex));
  6316. if (!sc->cenc.encryption_index)
  6317. return AVERROR(ENOMEM);
  6318. }
  6319. sc->cenc.per_sample_iv_size = avio_r8(pb);
  6320. if (sc->cenc.per_sample_iv_size != 0 && sc->cenc.per_sample_iv_size != 8 &&
  6321. sc->cenc.per_sample_iv_size != 16) {
  6322. av_log(c->fc, AV_LOG_ERROR, "invalid per-sample IV size value\n");
  6323. return AVERROR_INVALIDDATA;
  6324. }
  6325. if (avio_read(pb, sc->cenc.default_encrypted_sample->key_id, 16) != 16) {
  6326. av_log(c->fc, AV_LOG_ERROR, "failed to read the default key ID\n");
  6327. return AVERROR_INVALIDDATA;
  6328. }
  6329. if (is_protected && !sc->cenc.per_sample_iv_size) {
  6330. iv_size = avio_r8(pb);
  6331. if (iv_size != 8 && iv_size != 16) {
  6332. av_log(c->fc, AV_LOG_ERROR, "invalid default_constant_IV_size in tenc atom\n");
  6333. return AVERROR_INVALIDDATA;
  6334. }
  6335. if (avio_read(pb, sc->cenc.default_encrypted_sample->iv, iv_size) != iv_size) {
  6336. av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
  6337. return AVERROR_INVALIDDATA;
  6338. }
  6339. }
  6340. return 0;
  6341. }
  6342. static int mov_read_dfla(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6343. {
  6344. AVStream *st;
  6345. int last, type, size, ret;
  6346. uint8_t buf[4];
  6347. if (c->fc->nb_streams < 1)
  6348. return 0;
  6349. st = c->fc->streams[c->fc->nb_streams-1];
  6350. if ((uint64_t)atom.size > (1<<30) || atom.size < 42)
  6351. return AVERROR_INVALIDDATA;
  6352. /* Check FlacSpecificBox version. */
  6353. if (avio_r8(pb) != 0)
  6354. return AVERROR_INVALIDDATA;
  6355. avio_rb24(pb); /* Flags */
  6356. if (avio_read(pb, buf, sizeof(buf)) != sizeof(buf)) {
  6357. av_log(c->fc, AV_LOG_ERROR, "failed to read FLAC metadata block header\n");
  6358. return pb->error < 0 ? pb->error : AVERROR_INVALIDDATA;
  6359. }
  6360. flac_parse_block_header(buf, &last, &type, &size);
  6361. if (type != FLAC_METADATA_TYPE_STREAMINFO || size != FLAC_STREAMINFO_SIZE) {
  6362. av_log(c->fc, AV_LOG_ERROR, "STREAMINFO must be first FLACMetadataBlock\n");
  6363. return AVERROR_INVALIDDATA;
  6364. }
  6365. ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
  6366. if (ret < 0)
  6367. return ret;
  6368. if (!last)
  6369. av_log(c->fc, AV_LOG_WARNING, "non-STREAMINFO FLACMetadataBlock(s) ignored\n");
  6370. return 0;
  6371. }
  6372. static int cenc_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
  6373. {
  6374. int i, ret;
  6375. int bytes_of_protected_data;
  6376. if (!sc->cenc.aes_ctr) {
  6377. /* initialize the cipher */
  6378. sc->cenc.aes_ctr = av_aes_ctr_alloc();
  6379. if (!sc->cenc.aes_ctr) {
  6380. return AVERROR(ENOMEM);
  6381. }
  6382. ret = av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
  6383. if (ret < 0) {
  6384. return ret;
  6385. }
  6386. }
  6387. av_aes_ctr_set_full_iv(sc->cenc.aes_ctr, sample->iv);
  6388. if (!sample->subsample_count) {
  6389. /* decrypt the whole packet */
  6390. av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, size);
  6391. return 0;
  6392. }
  6393. for (i = 0; i < sample->subsample_count; i++) {
  6394. if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
  6395. av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
  6396. return AVERROR_INVALIDDATA;
  6397. }
  6398. /* skip the clear bytes */
  6399. input += sample->subsamples[i].bytes_of_clear_data;
  6400. size -= sample->subsamples[i].bytes_of_clear_data;
  6401. /* decrypt the encrypted bytes */
  6402. bytes_of_protected_data = sample->subsamples[i].bytes_of_protected_data;
  6403. av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, bytes_of_protected_data);
  6404. input += bytes_of_protected_data;
  6405. size -= bytes_of_protected_data;
  6406. }
  6407. if (size > 0) {
  6408. av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
  6409. return AVERROR_INVALIDDATA;
  6410. }
  6411. return 0;
  6412. }
  6413. static int cbc1_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
  6414. {
  6415. int i, ret;
  6416. int num_of_encrypted_blocks;
  6417. uint8_t iv[16];
  6418. if (!sc->cenc.aes_ctx) {
  6419. /* initialize the cipher */
  6420. sc->cenc.aes_ctx = av_aes_alloc();
  6421. if (!sc->cenc.aes_ctx) {
  6422. return AVERROR(ENOMEM);
  6423. }
  6424. ret = av_aes_init(sc->cenc.aes_ctx, c->decryption_key, 16 * 8, 1);
  6425. if (ret < 0) {
  6426. return ret;
  6427. }
  6428. }
  6429. memcpy(iv, sample->iv, 16);
  6430. /* whole-block full sample encryption */
  6431. if (!sample->subsample_count) {
  6432. /* decrypt the whole packet */
  6433. av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
  6434. return 0;
  6435. }
  6436. for (i = 0; i < sample->subsample_count; i++) {
  6437. if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
  6438. av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
  6439. return AVERROR_INVALIDDATA;
  6440. }
  6441. if (sample->subsamples[i].bytes_of_protected_data % 16) {
  6442. av_log(c->fc, AV_LOG_ERROR, "subsample BytesOfProtectedData is not a multiple of 16\n");
  6443. return AVERROR_INVALIDDATA;
  6444. }
  6445. /* skip the clear bytes */
  6446. input += sample->subsamples[i].bytes_of_clear_data;
  6447. size -= sample->subsamples[i].bytes_of_clear_data;
  6448. /* decrypt the encrypted bytes */
  6449. num_of_encrypted_blocks = sample->subsamples[i].bytes_of_protected_data/16;
  6450. if (num_of_encrypted_blocks > 0) {
  6451. av_aes_crypt(sc->cenc.aes_ctx, input, input, num_of_encrypted_blocks, iv, 1);
  6452. }
  6453. input += sample->subsamples[i].bytes_of_protected_data;
  6454. size -= sample->subsamples[i].bytes_of_protected_data;
  6455. }
  6456. if (size > 0) {
  6457. av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
  6458. return AVERROR_INVALIDDATA;
  6459. }
  6460. return 0;
  6461. }
  6462. static int cens_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
  6463. {
  6464. int i, ret, rem_bytes;
  6465. uint8_t *data;
  6466. if (!sc->cenc.aes_ctr) {
  6467. /* initialize the cipher */
  6468. sc->cenc.aes_ctr = av_aes_ctr_alloc();
  6469. if (!sc->cenc.aes_ctr) {
  6470. return AVERROR(ENOMEM);
  6471. }
  6472. ret = av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
  6473. if (ret < 0) {
  6474. return ret;
  6475. }
  6476. }
  6477. av_aes_ctr_set_full_iv(sc->cenc.aes_ctr, sample->iv);
  6478. /* whole-block full sample encryption */
  6479. if (!sample->subsample_count) {
  6480. /* decrypt the whole packet */
  6481. av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, size);
  6482. return 0;
  6483. } else if (!sample->crypt_byte_block && !sample->skip_byte_block) {
  6484. av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 'cens' scheme\n");
  6485. return AVERROR_INVALIDDATA;
  6486. }
  6487. for (i = 0; i < sample->subsample_count; i++) {
  6488. if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
  6489. av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
  6490. return AVERROR_INVALIDDATA;
  6491. }
  6492. /* skip the clear bytes */
  6493. input += sample->subsamples[i].bytes_of_clear_data;
  6494. size -= sample->subsamples[i].bytes_of_clear_data;
  6495. /* decrypt the encrypted bytes */
  6496. data = input;
  6497. rem_bytes = sample->subsamples[i].bytes_of_protected_data;
  6498. while (rem_bytes > 0) {
  6499. if (rem_bytes < 16*sample->crypt_byte_block) {
  6500. break;
  6501. }
  6502. av_aes_ctr_crypt(sc->cenc.aes_ctr, data, data, 16*sample->crypt_byte_block);
  6503. data += 16*sample->crypt_byte_block;
  6504. rem_bytes -= 16*sample->crypt_byte_block;
  6505. data += FFMIN(16*sample->skip_byte_block, rem_bytes);
  6506. rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes);
  6507. }
  6508. input += sample->subsamples[i].bytes_of_protected_data;
  6509. size -= sample->subsamples[i].bytes_of_protected_data;
  6510. }
  6511. if (size > 0) {
  6512. av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
  6513. return AVERROR_INVALIDDATA;
  6514. }
  6515. return 0;
  6516. }
  6517. static int cbcs_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
  6518. {
  6519. int i, ret, rem_bytes;
  6520. uint8_t iv[16];
  6521. uint8_t *data;
  6522. if (!sc->cenc.aes_ctx) {
  6523. /* initialize the cipher */
  6524. sc->cenc.aes_ctx = av_aes_alloc();
  6525. if (!sc->cenc.aes_ctx) {
  6526. return AVERROR(ENOMEM);
  6527. }
  6528. ret = av_aes_init(sc->cenc.aes_ctx, c->decryption_key, 16 * 8, 1);
  6529. if (ret < 0) {
  6530. return ret;
  6531. }
  6532. }
  6533. /* whole-block full sample encryption */
  6534. if (!sample->subsample_count) {
  6535. /* decrypt the whole packet */
  6536. memcpy(iv, sample->iv, 16);
  6537. av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
  6538. return 0;
  6539. } else if (!sample->crypt_byte_block && !sample->skip_byte_block) {
  6540. av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 'cbcs' scheme\n");
  6541. return AVERROR_INVALIDDATA;
  6542. }
  6543. for (i = 0; i < sample->subsample_count; i++) {
  6544. if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
  6545. av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
  6546. return AVERROR_INVALIDDATA;
  6547. }
  6548. /* skip the clear bytes */
  6549. input += sample->subsamples[i].bytes_of_clear_data;
  6550. size -= sample->subsamples[i].bytes_of_clear_data;
  6551. /* decrypt the encrypted bytes */
  6552. memcpy(iv, sample->iv, 16);
  6553. data = input;
  6554. rem_bytes = sample->subsamples[i].bytes_of_protected_data;
  6555. while (rem_bytes > 0) {
  6556. if (rem_bytes < 16*sample->crypt_byte_block) {
  6557. break;
  6558. }
  6559. av_aes_crypt(sc->cenc.aes_ctx, data, data, sample->crypt_byte_block, iv, 1);
  6560. data += 16*sample->crypt_byte_block;
  6561. rem_bytes -= 16*sample->crypt_byte_block;
  6562. data += FFMIN(16*sample->skip_byte_block, rem_bytes);
  6563. rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes);
  6564. }
  6565. input += sample->subsamples[i].bytes_of_protected_data;
  6566. size -= sample->subsamples[i].bytes_of_protected_data;
  6567. }
  6568. if (size > 0) {
  6569. av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
  6570. return AVERROR_INVALIDDATA;
  6571. }
  6572. return 0;
  6573. }
  6574. static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
  6575. {
  6576. if (sample->scheme == MKBETAG('c','e','n','c') && !sample->crypt_byte_block && !sample->skip_byte_block) {
  6577. return cenc_scheme_decrypt(c, sc, sample, input, size);
  6578. } else if (sample->scheme == MKBETAG('c','b','c','1') && !sample->crypt_byte_block && !sample->skip_byte_block) {
  6579. return cbc1_scheme_decrypt(c, sc, sample, input, size);
  6580. } else if (sample->scheme == MKBETAG('c','e','n','s')) {
  6581. return cens_scheme_decrypt(c, sc, sample, input, size);
  6582. } else if (sample->scheme == MKBETAG('c','b','c','s')) {
  6583. return cbcs_scheme_decrypt(c, sc, sample, input, size);
  6584. } else {
  6585. av_log(c->fc, AV_LOG_ERROR, "invalid encryption scheme\n");
  6586. return AVERROR_INVALIDDATA;
  6587. }
  6588. }
  6589. static MOVFragmentStreamInfo *get_frag_stream_info_from_pkt(MOVFragmentIndex *frag_index, AVPacket *pkt, int id)
  6590. {
  6591. int current = frag_index->current;
  6592. if (!frag_index->nb_items)
  6593. return NULL;
  6594. // Check frag_index->current is the right one for pkt. It can out of sync.
  6595. if (current >= 0 && current < frag_index->nb_items) {
  6596. if (frag_index->item[current].moof_offset < pkt->pos &&
  6597. (current + 1 == frag_index->nb_items ||
  6598. frag_index->item[current + 1].moof_offset > pkt->pos))
  6599. return get_frag_stream_info(frag_index, current, id);
  6600. }
  6601. for (int i = 0; i < frag_index->nb_items; i++) {
  6602. if (frag_index->item[i].moof_offset > pkt->pos)
  6603. break;
  6604. current = i;
  6605. }
  6606. frag_index->current = current;
  6607. return get_frag_stream_info(frag_index, current, id);
  6608. }
  6609. static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
  6610. {
  6611. MOVFragmentStreamInfo *frag_stream_info;
  6612. MOVEncryptionIndex *encryption_index;
  6613. AVEncryptionInfo *encrypted_sample;
  6614. int encrypted_index, ret;
  6615. frag_stream_info = get_frag_stream_info_from_pkt(&mov->frag_index, pkt, sc->id);
  6616. encrypted_index = current_index;
  6617. encryption_index = NULL;
  6618. if (frag_stream_info) {
  6619. // Note this only supports encryption info in the first sample descriptor.
  6620. if (frag_stream_info->stsd_id == 1) {
  6621. if (frag_stream_info->encryption_index) {
  6622. encrypted_index = current_index - frag_stream_info->index_base;
  6623. encryption_index = frag_stream_info->encryption_index;
  6624. } else {
  6625. encryption_index = sc->cenc.encryption_index;
  6626. }
  6627. }
  6628. } else {
  6629. encryption_index = sc->cenc.encryption_index;
  6630. }
  6631. if (encryption_index) {
  6632. if (encryption_index->auxiliary_info_sample_count &&
  6633. !encryption_index->nb_encrypted_samples) {
  6634. av_log(mov->fc, AV_LOG_ERROR, "saiz atom found without saio\n");
  6635. return AVERROR_INVALIDDATA;
  6636. }
  6637. if (encryption_index->auxiliary_offsets_count &&
  6638. !encryption_index->nb_encrypted_samples) {
  6639. av_log(mov->fc, AV_LOG_ERROR, "saio atom found without saiz\n");
  6640. return AVERROR_INVALIDDATA;
  6641. }
  6642. if (!encryption_index->nb_encrypted_samples) {
  6643. // Full-sample encryption with default settings.
  6644. encrypted_sample = sc->cenc.default_encrypted_sample;
  6645. } else if (encrypted_index >= 0 && encrypted_index < encryption_index->nb_encrypted_samples) {
  6646. // Per-sample setting override.
  6647. encrypted_sample = encryption_index->encrypted_samples[encrypted_index];
  6648. if (!encrypted_sample)
  6649. encrypted_sample = sc->cenc.default_encrypted_sample;
  6650. } else {
  6651. av_log(mov->fc, AV_LOG_ERROR, "Incorrect number of samples in encryption info\n");
  6652. return AVERROR_INVALIDDATA;
  6653. }
  6654. if (mov->decryption_key) {
  6655. return cenc_decrypt(mov, sc, encrypted_sample, pkt->data, pkt->size);
  6656. } else {
  6657. size_t size;
  6658. uint8_t *side_data = av_encryption_info_add_side_data(encrypted_sample, &size);
  6659. if (!side_data)
  6660. return AVERROR(ENOMEM);
  6661. ret = av_packet_add_side_data(pkt, AV_PKT_DATA_ENCRYPTION_INFO, side_data, size);
  6662. if (ret < 0)
  6663. av_free(side_data);
  6664. return ret;
  6665. }
  6666. }
  6667. return 0;
  6668. }
  6669. static int mov_read_dops(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6670. {
  6671. const int OPUS_SEEK_PREROLL_MS = 80;
  6672. int ret;
  6673. AVStream *st;
  6674. size_t size;
  6675. uint16_t pre_skip;
  6676. if (c->fc->nb_streams < 1)
  6677. return 0;
  6678. st = c->fc->streams[c->fc->nb_streams-1];
  6679. if ((uint64_t)atom.size > (1<<30) || atom.size < 11)
  6680. return AVERROR_INVALIDDATA;
  6681. /* Check OpusSpecificBox version. */
  6682. if (avio_r8(pb) != 0) {
  6683. av_log(c->fc, AV_LOG_ERROR, "unsupported OpusSpecificBox version\n");
  6684. return AVERROR_INVALIDDATA;
  6685. }
  6686. /* OpusSpecificBox size plus magic for Ogg OpusHead header. */
  6687. size = atom.size + 8;
  6688. if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
  6689. return ret;
  6690. AV_WL32(st->codecpar->extradata, MKTAG('O','p','u','s'));
  6691. AV_WL32(st->codecpar->extradata + 4, MKTAG('H','e','a','d'));
  6692. AV_WB8(st->codecpar->extradata + 8, 1); /* OpusHead version */
  6693. avio_read(pb, st->codecpar->extradata + 9, size - 9);
  6694. /* OpusSpecificBox is stored in big-endian, but OpusHead is
  6695. little-endian; aside from the preceeding magic and version they're
  6696. otherwise currently identical. Data after output gain at offset 16
  6697. doesn't need to be bytewapped. */
  6698. pre_skip = AV_RB16(st->codecpar->extradata + 10);
  6699. AV_WL16(st->codecpar->extradata + 10, pre_skip);
  6700. AV_WL32(st->codecpar->extradata + 12, AV_RB32(st->codecpar->extradata + 12));
  6701. AV_WL16(st->codecpar->extradata + 16, AV_RB16(st->codecpar->extradata + 16));
  6702. st->codecpar->initial_padding = pre_skip;
  6703. st->codecpar->seek_preroll = av_rescale_q(OPUS_SEEK_PREROLL_MS,
  6704. (AVRational){1, 1000},
  6705. (AVRational){1, 48000});
  6706. return 0;
  6707. }
  6708. static int mov_read_dmlp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6709. {
  6710. AVStream *st;
  6711. unsigned format_info;
  6712. int channel_assignment, channel_assignment1, channel_assignment2;
  6713. int ratebits;
  6714. uint64_t chmask;
  6715. if (c->fc->nb_streams < 1)
  6716. return 0;
  6717. st = c->fc->streams[c->fc->nb_streams-1];
  6718. if (atom.size < 10)
  6719. return AVERROR_INVALIDDATA;
  6720. format_info = avio_rb32(pb);
  6721. ratebits = (format_info >> 28) & 0xF;
  6722. channel_assignment1 = (format_info >> 15) & 0x1F;
  6723. channel_assignment2 = format_info & 0x1FFF;
  6724. if (channel_assignment2)
  6725. channel_assignment = channel_assignment2;
  6726. else
  6727. channel_assignment = channel_assignment1;
  6728. st->codecpar->frame_size = 40 << (ratebits & 0x7);
  6729. st->codecpar->sample_rate = mlp_samplerate(ratebits);
  6730. av_channel_layout_uninit(&st->codecpar->ch_layout);
  6731. chmask = truehd_layout(channel_assignment);
  6732. av_channel_layout_from_mask(&st->codecpar->ch_layout, chmask);
  6733. return 0;
  6734. }
  6735. static int mov_read_dvcc_dvvc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6736. {
  6737. AVStream *st;
  6738. uint8_t buf[ISOM_DVCC_DVVC_SIZE];
  6739. int ret;
  6740. int64_t read_size = atom.size;
  6741. if (c->fc->nb_streams < 1)
  6742. return 0;
  6743. st = c->fc->streams[c->fc->nb_streams-1];
  6744. // At most 24 bytes
  6745. read_size = FFMIN(read_size, ISOM_DVCC_DVVC_SIZE);
  6746. if ((ret = ffio_read_size(pb, buf, read_size)) < 0)
  6747. return ret;
  6748. return ff_isom_parse_dvcc_dvvc(c->fc, st, buf, read_size);
  6749. }
  6750. static int mov_read_kind(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6751. {
  6752. AVFormatContext *ctx = c->fc;
  6753. AVStream *st = NULL;
  6754. AVBPrint scheme_buf, value_buf;
  6755. int64_t scheme_str_len = 0, value_str_len = 0;
  6756. int version, flags, ret = AVERROR_BUG;
  6757. int64_t size = atom.size;
  6758. if (atom.size < 6)
  6759. // 4 bytes for version + flags, 2x 1 byte for null
  6760. return AVERROR_INVALIDDATA;
  6761. if (c->fc->nb_streams < 1)
  6762. return 0;
  6763. st = c->fc->streams[c->fc->nb_streams-1];
  6764. version = avio_r8(pb);
  6765. flags = avio_rb24(pb);
  6766. size -= 4;
  6767. if (version != 0 || flags != 0) {
  6768. av_log(ctx, AV_LOG_ERROR,
  6769. "Unsupported 'kind' box with version %d, flags: %x",
  6770. version, flags);
  6771. return AVERROR_INVALIDDATA;
  6772. }
  6773. av_bprint_init(&scheme_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  6774. av_bprint_init(&value_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  6775. if ((scheme_str_len = ff_read_string_to_bprint_overwrite(pb, &scheme_buf,
  6776. size)) < 0) {
  6777. ret = scheme_str_len;
  6778. goto cleanup;
  6779. }
  6780. if (scheme_str_len + 1 >= size) {
  6781. // we need to have another string, even if nullptr.
  6782. // we check with + 1 since we expect that if size was not hit,
  6783. // an additional null was read.
  6784. ret = AVERROR_INVALIDDATA;
  6785. goto cleanup;
  6786. }
  6787. size -= scheme_str_len + 1;
  6788. if ((value_str_len = ff_read_string_to_bprint_overwrite(pb, &value_buf,
  6789. size)) < 0) {
  6790. ret = value_str_len;
  6791. goto cleanup;
  6792. }
  6793. if (value_str_len == size) {
  6794. // in case of no trailing null, box is not valid.
  6795. ret = AVERROR_INVALIDDATA;
  6796. goto cleanup;
  6797. }
  6798. av_log(ctx, AV_LOG_TRACE,
  6799. "%s stream %d KindBox(scheme: %s, value: %s)\n",
  6800. av_get_media_type_string(st->codecpar->codec_type),
  6801. st->index,
  6802. scheme_buf.str, value_buf.str);
  6803. for (int i = 0; ff_mov_track_kind_table[i].scheme_uri; i++) {
  6804. const struct MP4TrackKindMapping map = ff_mov_track_kind_table[i];
  6805. if (!av_strstart(scheme_buf.str, map.scheme_uri, NULL))
  6806. continue;
  6807. for (int j = 0; map.value_maps[j].disposition; j++) {
  6808. const struct MP4TrackKindValueMapping value_map = map.value_maps[j];
  6809. if (!av_strstart(value_buf.str, value_map.value, NULL))
  6810. continue;
  6811. st->disposition |= value_map.disposition;
  6812. }
  6813. }
  6814. ret = 0;
  6815. cleanup:
  6816. av_bprint_finalize(&scheme_buf, NULL);
  6817. av_bprint_finalize(&value_buf, NULL);
  6818. return ret;
  6819. }
  6820. static int mov_read_SA3D(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6821. {
  6822. AVStream *st;
  6823. int i, version, type;
  6824. int ambisonic_order, channel_order, normalization, channel_count;
  6825. if (c->fc->nb_streams < 1)
  6826. return 0;
  6827. st = c->fc->streams[c->fc->nb_streams - 1];
  6828. if (atom.size < 16) {
  6829. av_log(c->fc, AV_LOG_ERROR, "SA3D audio box too small\n");
  6830. return AVERROR_INVALIDDATA;
  6831. }
  6832. version = avio_r8(pb);
  6833. if (version) {
  6834. av_log(c->fc, AV_LOG_WARNING, "Unsupported SA3D box version %d\n", version);
  6835. return 0;
  6836. }
  6837. type = avio_r8(pb);
  6838. if (type) {
  6839. av_log(c->fc, AV_LOG_WARNING,
  6840. "Unsupported ambisonic type %d\n", type);
  6841. return 0;
  6842. }
  6843. ambisonic_order = avio_rb32(pb);
  6844. channel_order = avio_r8(pb);
  6845. if (channel_order) {
  6846. av_log(c->fc, AV_LOG_WARNING,
  6847. "Unsupported channel_order %d\n", channel_order);
  6848. return 0;
  6849. }
  6850. normalization = avio_r8(pb);
  6851. if (normalization) {
  6852. av_log(c->fc, AV_LOG_WARNING,
  6853. "Unsupported normalization %d\n", normalization);
  6854. return 0;
  6855. }
  6856. channel_count = avio_rb32(pb);
  6857. if (ambisonic_order < 0 || channel_count != (ambisonic_order + 1LL) * (ambisonic_order + 1LL)) {
  6858. av_log(c->fc, AV_LOG_ERROR,
  6859. "Invalid number of channels (%d / %d)\n",
  6860. channel_count, ambisonic_order);
  6861. return 0;
  6862. }
  6863. for (i = 0; i < channel_count; i++) {
  6864. if (i != avio_rb32(pb)) {
  6865. av_log(c->fc, AV_LOG_WARNING,
  6866. "Ambisonic channel reordering is not supported\n");
  6867. return 0;
  6868. }
  6869. }
  6870. av_channel_layout_uninit(&st->codecpar->ch_layout);
  6871. st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_AMBISONIC;
  6872. st->codecpar->ch_layout.nb_channels = channel_count;
  6873. return 0;
  6874. }
  6875. static int mov_read_SAND(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6876. {
  6877. AVStream *st;
  6878. int version;
  6879. if (c->fc->nb_streams < 1)
  6880. return 0;
  6881. st = c->fc->streams[c->fc->nb_streams - 1];
  6882. if (atom.size < 5) {
  6883. av_log(c->fc, AV_LOG_ERROR, "Empty SAND audio box\n");
  6884. return AVERROR_INVALIDDATA;
  6885. }
  6886. version = avio_r8(pb);
  6887. if (version) {
  6888. av_log(c->fc, AV_LOG_WARNING, "Unsupported SAND box version %d\n", version);
  6889. return 0;
  6890. }
  6891. st->disposition |= AV_DISPOSITION_NON_DIEGETIC;
  6892. return 0;
  6893. }
  6894. static int rb_size(AVIOContext *pb, uint64_t* value, int size)
  6895. {
  6896. if (size == 0)
  6897. *value = 0;
  6898. else if (size == 1)
  6899. *value = avio_r8(pb);
  6900. else if (size == 2)
  6901. *value = avio_rb16(pb);
  6902. else if (size == 4)
  6903. *value = avio_rb32(pb);
  6904. else if (size == 8)
  6905. *value = avio_rb64(pb);
  6906. else
  6907. return -1;
  6908. return size;
  6909. }
  6910. static int mov_read_pitm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6911. {
  6912. avio_rb32(pb); // version & flags.
  6913. c->primary_item_id = avio_rb16(pb);
  6914. av_log(c->fc, AV_LOG_TRACE, "pitm: primary_item_id %d\n", c->primary_item_id);
  6915. return atom.size;
  6916. }
  6917. static int mov_read_idat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6918. {
  6919. c->idat_offset = avio_tell(pb);
  6920. return 0;
  6921. }
  6922. static int mov_read_iloc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  6923. {
  6924. HEIFItem *heif_item;
  6925. int version, offset_size, length_size, base_offset_size, index_size;
  6926. int item_count, extent_count;
  6927. int64_t base_offset, extent_offset, extent_length;
  6928. uint8_t value;
  6929. if (c->found_iloc) {
  6930. av_log(c->fc, AV_LOG_INFO, "Duplicate iloc box found\n");
  6931. return 0;
  6932. }
  6933. version = avio_r8(pb);
  6934. avio_rb24(pb); // flags.
  6935. value = avio_r8(pb);
  6936. offset_size = (value >> 4) & 0xF;
  6937. length_size = value & 0xF;
  6938. value = avio_r8(pb);
  6939. base_offset_size = (value >> 4) & 0xF;
  6940. index_size = !version ? 0 : (value & 0xF);
  6941. if (index_size) {
  6942. avpriv_report_missing_feature(c->fc, "iloc: index_size != 0");
  6943. return AVERROR_PATCHWELCOME;
  6944. }
  6945. item_count = (version < 2) ? avio_rb16(pb) : avio_rb32(pb);
  6946. heif_item = av_realloc_array(c->heif_item, FFMAX(item_count, c->nb_heif_item), sizeof(*c->heif_item));
  6947. if (!heif_item)
  6948. return AVERROR(ENOMEM);
  6949. c->heif_item = heif_item;
  6950. if (item_count > c->nb_heif_item)
  6951. memset(c->heif_item + c->nb_heif_item, 0,
  6952. sizeof(*c->heif_item) * (item_count - c->nb_heif_item));
  6953. c->nb_heif_item = FFMAX(c->nb_heif_item, item_count);
  6954. av_log(c->fc, AV_LOG_TRACE, "iloc: item_count %d\n", item_count);
  6955. for (int i = 0; i < item_count; i++) {
  6956. int item_id = (version < 2) ? avio_rb16(pb) : avio_rb32(pb);
  6957. int offset_type = (version > 0) ? avio_rb16(pb) & 0xf : 0;
  6958. if (avio_feof(pb))
  6959. return AVERROR_INVALIDDATA;
  6960. if (offset_type > 1) {
  6961. avpriv_report_missing_feature(c->fc, "iloc offset type %d", offset_type);
  6962. return AVERROR_PATCHWELCOME;
  6963. }
  6964. c->heif_item[i].item_id = item_id;
  6965. avio_rb16(pb); // data_reference_index.
  6966. if (rb_size(pb, &base_offset, base_offset_size) < 0)
  6967. return AVERROR_INVALIDDATA;
  6968. extent_count = avio_rb16(pb);
  6969. if (extent_count > 1) {
  6970. // For still AVIF images, we only support one extent item.
  6971. avpriv_report_missing_feature(c->fc, "iloc: extent_count > 1");
  6972. return AVERROR_PATCHWELCOME;
  6973. }
  6974. for (int j = 0; j < extent_count; j++) {
  6975. if (rb_size(pb, &extent_offset, offset_size) < 0 ||
  6976. rb_size(pb, &extent_length, length_size) < 0)
  6977. return AVERROR_INVALIDDATA;
  6978. if (offset_type == 1)
  6979. c->heif_item[i].is_idat_relative = 1;
  6980. c->heif_item[i].extent_length = extent_length;
  6981. c->heif_item[i].extent_offset = base_offset + extent_offset;
  6982. av_log(c->fc, AV_LOG_TRACE, "iloc: item_idx %d, offset_type %d, "
  6983. "extent_offset %"PRId64", extent_length %"PRId64"\n",
  6984. i, offset_type, c->heif_item[i].extent_offset, c->heif_item[i].extent_length);
  6985. }
  6986. }
  6987. c->found_iloc = 1;
  6988. return atom.size;
  6989. }
  6990. static int mov_read_infe(MOVContext *c, AVIOContext *pb, MOVAtom atom, int idx)
  6991. {
  6992. AVBPrint item_name;
  6993. int64_t size = atom.size;
  6994. uint32_t item_type;
  6995. int item_id;
  6996. int version, ret;
  6997. version = avio_r8(pb);
  6998. avio_rb24(pb); // flags.
  6999. size -= 4;
  7000. if (version < 2) {
  7001. av_log(c->fc, AV_LOG_ERROR, "infe: version < 2 not supported\n");
  7002. return AVERROR_PATCHWELCOME;
  7003. }
  7004. item_id = version > 2 ? avio_rb32(pb) : avio_rb16(pb);
  7005. avio_rb16(pb); // item_protection_index
  7006. item_type = avio_rl32(pb);
  7007. size -= 8;
  7008. av_bprint_init(&item_name, 0, AV_BPRINT_SIZE_UNLIMITED);
  7009. ret = ff_read_string_to_bprint_overwrite(pb, &item_name, size);
  7010. if (ret < 0) {
  7011. av_bprint_finalize(&item_name, NULL);
  7012. return ret;
  7013. }
  7014. av_log(c->fc, AV_LOG_TRACE, "infe: item_id %d, item_type %s, item_name %s\n",
  7015. item_id, av_fourcc2str(item_type), item_name.str);
  7016. size -= ret + 1;
  7017. if (size > 0)
  7018. avio_skip(pb, size);
  7019. if (ret)
  7020. av_bprint_finalize(&item_name, &c->heif_item[idx].name);
  7021. c->heif_item[idx].item_id = item_id;
  7022. c->heif_item[idx].type = item_type;
  7023. switch (item_type) {
  7024. case MKTAG('a','v','0','1'):
  7025. case MKTAG('h','v','c','1'):
  7026. ret = heif_add_stream(c, &c->heif_item[idx]);
  7027. if (ret < 0)
  7028. return ret;
  7029. break;
  7030. }
  7031. return 0;
  7032. }
  7033. static int mov_read_iinf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  7034. {
  7035. HEIFItem *heif_item;
  7036. int entry_count;
  7037. int version, ret;
  7038. if (c->found_iinf) {
  7039. av_log(c->fc, AV_LOG_WARNING, "Duplicate iinf box found\n");
  7040. return 0;
  7041. }
  7042. version = avio_r8(pb);
  7043. avio_rb24(pb); // flags.
  7044. entry_count = version ? avio_rb32(pb) : avio_rb16(pb);
  7045. heif_item = av_realloc_array(c->heif_item, FFMAX(entry_count, c->nb_heif_item), sizeof(*c->heif_item));
  7046. if (!heif_item)
  7047. return AVERROR(ENOMEM);
  7048. c->heif_item = heif_item;
  7049. if (entry_count > c->nb_heif_item)
  7050. memset(c->heif_item + c->nb_heif_item, 0,
  7051. sizeof(*c->heif_item) * (entry_count - c->nb_heif_item));
  7052. c->nb_heif_item = FFMAX(c->nb_heif_item, entry_count);
  7053. for (int i = 0; i < entry_count; i++) {
  7054. MOVAtom infe;
  7055. infe.size = avio_rb32(pb) - 8;
  7056. infe.type = avio_rl32(pb);
  7057. ret = mov_read_infe(c, pb, infe, i);
  7058. if (ret < 0)
  7059. return ret;
  7060. }
  7061. c->found_iinf = 1;
  7062. return 0;
  7063. }
  7064. static int mov_read_iref_dimg(MOVContext *c, AVIOContext *pb, int version)
  7065. {
  7066. HEIFItem *item = NULL;
  7067. HEIFGrid *grid;
  7068. int entries, i;
  7069. int from_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
  7070. for (int i = 0; i < c->nb_heif_grid; i++) {
  7071. if (c->heif_grid[i].item->item_id == from_item_id) {
  7072. av_log(c->fc, AV_LOG_ERROR, "More than one 'dimg' box "
  7073. "referencing the same Derived Image item\n");
  7074. return AVERROR_INVALIDDATA;
  7075. }
  7076. }
  7077. for (int i = 0; i < c->nb_heif_item; i++) {
  7078. if (c->heif_item[i].item_id != from_item_id)
  7079. continue;
  7080. item = &c->heif_item[i];
  7081. switch (item->type) {
  7082. case MKTAG('g','r','i','d'):
  7083. case MKTAG('i','o','v','l'):
  7084. break;
  7085. default:
  7086. avpriv_report_missing_feature(c->fc, "Derived Image item of type %s",
  7087. av_fourcc2str(item->type));
  7088. return 0;
  7089. }
  7090. break;
  7091. }
  7092. if (!item) {
  7093. av_log(c->fc, AV_LOG_ERROR, "Missing grid information\n");
  7094. return AVERROR_INVALIDDATA;
  7095. }
  7096. grid = av_realloc_array(c->heif_grid, c->nb_heif_grid + 1U,
  7097. sizeof(*c->heif_grid));
  7098. if (!grid)
  7099. return AVERROR(ENOMEM);
  7100. c->heif_grid = grid;
  7101. grid = &grid[c->nb_heif_grid++];
  7102. entries = avio_rb16(pb);
  7103. grid->tile_id_list = av_malloc_array(entries, sizeof(*grid->tile_id_list));
  7104. grid->tile_item_list = av_calloc(entries, sizeof(*grid->tile_item_list));
  7105. if (!grid->tile_id_list || !grid->tile_item_list)
  7106. return AVERROR(ENOMEM);
  7107. /* 'to' item ids */
  7108. for (i = 0; i < entries; i++)
  7109. grid->tile_id_list[i] = version ? avio_rb32(pb) : avio_rb16(pb);
  7110. grid->nb_tiles = entries;
  7111. grid->item = item;
  7112. av_log(c->fc, AV_LOG_TRACE, "dimg: from_item_id %d, entries %d\n",
  7113. from_item_id, entries);
  7114. return 0;
  7115. }
  7116. static int mov_read_iref_thmb(MOVContext *c, AVIOContext *pb, int version)
  7117. {
  7118. int entries;
  7119. int to_item_id, from_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
  7120. entries = avio_rb16(pb);
  7121. if (entries > 1) {
  7122. avpriv_request_sample(c->fc, "thmb in iref referencing several items");
  7123. return AVERROR_PATCHWELCOME;
  7124. }
  7125. /* 'to' item ids */
  7126. to_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
  7127. if (to_item_id != c->primary_item_id)
  7128. return 0;
  7129. c->thmb_item_id = from_item_id;
  7130. av_log(c->fc, AV_LOG_TRACE, "thmb: from_item_id %d, entries %d\n",
  7131. from_item_id, entries);
  7132. return 0;
  7133. }
  7134. static int mov_read_iref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  7135. {
  7136. int version = avio_r8(pb);
  7137. avio_rb24(pb); // flags
  7138. atom.size -= 4;
  7139. if (version > 1) {
  7140. av_log(c->fc, AV_LOG_WARNING, "Unknown iref box version %d\n", version);
  7141. return 0;
  7142. }
  7143. while (atom.size) {
  7144. uint32_t type, size = avio_rb32(pb);
  7145. int64_t next = avio_tell(pb);
  7146. if (size < 14 || next < 0 || next > INT64_MAX - size)
  7147. return AVERROR_INVALIDDATA;
  7148. next += size - 4;
  7149. type = avio_rl32(pb);
  7150. switch (type) {
  7151. case MKTAG('d','i','m','g'):
  7152. mov_read_iref_dimg(c, pb, version);
  7153. break;
  7154. case MKTAG('t','h','m','b'):
  7155. mov_read_iref_thmb(c, pb, version);
  7156. break;
  7157. default:
  7158. av_log(c->fc, AV_LOG_DEBUG, "Unknown iref type %s size %"PRIu32"\n",
  7159. av_fourcc2str(type), size);
  7160. }
  7161. atom.size -= size;
  7162. avio_seek(pb, next, SEEK_SET);
  7163. }
  7164. return 0;
  7165. }
  7166. static int mov_read_ispe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  7167. {
  7168. uint32_t width, height;
  7169. avio_r8(pb); /* version */
  7170. avio_rb24(pb); /* flags */
  7171. width = avio_rb32(pb);
  7172. height = avio_rb32(pb);
  7173. av_log(c->fc, AV_LOG_TRACE, "ispe: item_id %d, width %u, height %u\n",
  7174. c->cur_item_id, width, height);
  7175. for (int i = 0; i < c->nb_heif_item; i++) {
  7176. if (c->heif_item[i].item_id == c->cur_item_id) {
  7177. c->heif_item[i].width = width;
  7178. c->heif_item[i].height = height;
  7179. break;
  7180. }
  7181. }
  7182. return 0;
  7183. }
  7184. static int mov_read_iprp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  7185. {
  7186. typedef struct MOVAtoms {
  7187. FFIOContext b;
  7188. uint32_t type;
  7189. int64_t size;
  7190. uint8_t *data;
  7191. } MOVAtoms;
  7192. MOVAtoms *atoms = NULL;
  7193. MOVAtom a;
  7194. unsigned count;
  7195. int nb_atoms = 0;
  7196. int version, flags;
  7197. int ret;
  7198. a.size = avio_rb32(pb);
  7199. a.type = avio_rl32(pb);
  7200. if (a.size < 8 || a.type != MKTAG('i','p','c','o'))
  7201. return AVERROR_INVALIDDATA;
  7202. a.size -= 8;
  7203. while (a.size >= 8) {
  7204. MOVAtoms *ref = av_dynarray2_add((void**)&atoms, &nb_atoms, sizeof(MOVAtoms), NULL);
  7205. if (!ref) {
  7206. ret = AVERROR(ENOMEM);
  7207. goto fail;
  7208. }
  7209. ref->data = NULL;
  7210. ref->size = avio_rb32(pb);
  7211. ref->type = avio_rl32(pb);
  7212. if (ref->size > a.size || ref->size < 8)
  7213. break;
  7214. ref->data = av_malloc(ref->size);
  7215. if (!ref->data) {
  7216. ret = AVERROR_INVALIDDATA;
  7217. goto fail;
  7218. }
  7219. av_log(c->fc, AV_LOG_TRACE, "ipco: index %d, box type %s\n", nb_atoms, av_fourcc2str(ref->type));
  7220. avio_seek(pb, -8, SEEK_CUR);
  7221. if (avio_read(pb, ref->data, ref->size) != ref->size) {
  7222. ret = AVERROR_INVALIDDATA;
  7223. goto fail;
  7224. }
  7225. ffio_init_read_context(&ref->b, ref->data, ref->size);
  7226. a.size -= ref->size;
  7227. }
  7228. if (a.size) {
  7229. ret = AVERROR_INVALIDDATA;
  7230. goto fail;
  7231. }
  7232. a.size = avio_rb32(pb);
  7233. a.type = avio_rl32(pb);
  7234. if (a.size < 8 || a.type != MKTAG('i','p','m','a')) {
  7235. ret = AVERROR_INVALIDDATA;
  7236. goto fail;
  7237. }
  7238. version = avio_r8(pb);
  7239. flags = avio_rb24(pb);
  7240. count = avio_rb32(pb);
  7241. for (int i = 0; i < count; i++) {
  7242. int item_id = version ? avio_rb32(pb) : avio_rb16(pb);
  7243. int assoc_count = avio_r8(pb);
  7244. for (int j = 0; j < assoc_count; j++) {
  7245. MOVAtoms *ref;
  7246. int index = avio_r8(pb) & 0x7f;
  7247. if (flags & 1) {
  7248. index <<= 8;
  7249. index |= avio_r8(pb);
  7250. }
  7251. if (index > nb_atoms || index <= 0) {
  7252. ret = AVERROR_INVALIDDATA;
  7253. goto fail;
  7254. }
  7255. ref = &atoms[--index];
  7256. av_log(c->fc, AV_LOG_TRACE, "ipma: property_index %d, item_id %d, item_type %s\n",
  7257. index + 1, item_id, av_fourcc2str(ref->type));
  7258. c->cur_item_id = item_id;
  7259. ret = mov_read_default(c, &ref->b.pub,
  7260. (MOVAtom) { .size = ref->size,
  7261. .type = MKTAG('i','p','c','o') });
  7262. if (ret < 0)
  7263. goto fail;
  7264. ffio_init_read_context(&ref->b, ref->data, ref->size);
  7265. }
  7266. }
  7267. ret = 0;
  7268. fail:
  7269. c->cur_item_id = -1;
  7270. for (int i = 0; i < nb_atoms; i++)
  7271. av_free(atoms[i].data);
  7272. av_free(atoms);
  7273. return ret;
  7274. }
  7275. static const MOVParseTableEntry mov_default_parse_table[] = {
  7276. { MKTAG('A','C','L','R'), mov_read_aclr },
  7277. { MKTAG('A','P','R','G'), mov_read_avid },
  7278. { MKTAG('A','A','L','P'), mov_read_avid },
  7279. { MKTAG('A','R','E','S'), mov_read_ares },
  7280. { MKTAG('a','v','s','s'), mov_read_avss },
  7281. { MKTAG('a','v','1','C'), mov_read_glbl },
  7282. { MKTAG('c','h','p','l'), mov_read_chpl },
  7283. { MKTAG('c','o','6','4'), mov_read_stco },
  7284. { MKTAG('c','o','l','r'), mov_read_colr },
  7285. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  7286. { MKTAG('d','i','n','f'), mov_read_default },
  7287. { MKTAG('D','p','x','E'), mov_read_dpxe },
  7288. { MKTAG('d','r','e','f'), mov_read_dref },
  7289. { MKTAG('e','d','t','s'), mov_read_default },
  7290. { MKTAG('e','l','s','t'), mov_read_elst },
  7291. { MKTAG('e','n','d','a'), mov_read_enda },
  7292. { MKTAG('f','i','e','l'), mov_read_fiel },
  7293. { MKTAG('a','d','r','m'), mov_read_adrm },
  7294. { MKTAG('f','t','y','p'), mov_read_ftyp },
  7295. { MKTAG('g','l','b','l'), mov_read_glbl },
  7296. { MKTAG('h','d','l','r'), mov_read_hdlr },
  7297. { MKTAG('i','l','s','t'), mov_read_ilst },
  7298. { MKTAG('j','p','2','h'), mov_read_jp2h },
  7299. { MKTAG('m','d','a','t'), mov_read_mdat },
  7300. { MKTAG('m','d','h','d'), mov_read_mdhd },
  7301. { MKTAG('m','d','i','a'), mov_read_default },
  7302. { MKTAG('m','e','t','a'), mov_read_meta },
  7303. { MKTAG('m','i','n','f'), mov_read_default },
  7304. { MKTAG('m','o','o','f'), mov_read_moof },
  7305. { MKTAG('m','o','o','v'), mov_read_moov },
  7306. { MKTAG('m','v','e','x'), mov_read_default },
  7307. { MKTAG('m','v','h','d'), mov_read_mvhd },
  7308. { MKTAG('S','M','I',' '), mov_read_svq3 },
  7309. { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
  7310. { MKTAG('a','v','c','C'), mov_read_glbl },
  7311. { MKTAG('p','a','s','p'), mov_read_pasp },
  7312. { MKTAG('s','i','d','x'), mov_read_sidx },
  7313. { MKTAG('s','t','b','l'), mov_read_default },
  7314. { MKTAG('s','t','c','o'), mov_read_stco },
  7315. { MKTAG('s','t','p','s'), mov_read_stps },
  7316. { MKTAG('s','t','r','f'), mov_read_strf },
  7317. { MKTAG('s','t','s','c'), mov_read_stsc },
  7318. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  7319. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  7320. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  7321. { MKTAG('s','t','t','s'), mov_read_stts },
  7322. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  7323. { MKTAG('s','d','t','p'), mov_read_sdtp }, /* independent and disposable samples */
  7324. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  7325. { MKTAG('t','f','d','t'), mov_read_tfdt },
  7326. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  7327. { MKTAG('t','r','a','k'), mov_read_trak },
  7328. { MKTAG('t','r','a','f'), mov_read_default },
  7329. { MKTAG('t','r','e','f'), mov_read_default },
  7330. { MKTAG('t','m','c','d'), mov_read_tmcd },
  7331. { MKTAG('c','h','a','p'), mov_read_chap },
  7332. { MKTAG('t','r','e','x'), mov_read_trex },
  7333. { MKTAG('t','r','u','n'), mov_read_trun },
  7334. { MKTAG('u','d','t','a'), mov_read_default },
  7335. { MKTAG('w','a','v','e'), mov_read_wave },
  7336. { MKTAG('e','s','d','s'), mov_read_esds },
  7337. { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
  7338. { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
  7339. { MKTAG('d','d','t','s'), mov_read_ddts }, /* DTS audio descriptor */
  7340. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  7341. { MKTAG('w','f','e','x'), mov_read_wfex },
  7342. { MKTAG('c','m','o','v'), mov_read_cmov },
  7343. { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout from quicktime */
  7344. { MKTAG('c','h','n','l'), mov_read_chnl }, /* channel layout from ISO-14496-12 */
  7345. { MKTAG('d','v','c','1'), mov_read_dvc1 },
  7346. { MKTAG('s','g','p','d'), mov_read_sgpd },
  7347. { MKTAG('s','b','g','p'), mov_read_sbgp },
  7348. { MKTAG('h','v','c','C'), mov_read_glbl },
  7349. { MKTAG('v','v','c','C'), mov_read_glbl },
  7350. { MKTAG('u','u','i','d'), mov_read_uuid },
  7351. { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
  7352. { MKTAG('f','r','e','e'), mov_read_free },
  7353. { MKTAG('-','-','-','-'), mov_read_custom },
  7354. { MKTAG('s','i','n','f'), mov_read_default },
  7355. { MKTAG('f','r','m','a'), mov_read_frma },
  7356. { MKTAG('s','e','n','c'), mov_read_senc },
  7357. { MKTAG('s','a','i','z'), mov_read_saiz },
  7358. { MKTAG('s','a','i','o'), mov_read_saio },
  7359. { MKTAG('p','s','s','h'), mov_read_pssh },
  7360. { MKTAG('s','c','h','m'), mov_read_schm },
  7361. { MKTAG('s','c','h','i'), mov_read_default },
  7362. { MKTAG('t','e','n','c'), mov_read_tenc },
  7363. { MKTAG('d','f','L','a'), mov_read_dfla },
  7364. { MKTAG('s','t','3','d'), mov_read_st3d }, /* stereoscopic 3D video box */
  7365. { MKTAG('s','v','3','d'), mov_read_sv3d }, /* spherical video box */
  7366. { MKTAG('d','O','p','s'), mov_read_dops },
  7367. { MKTAG('d','m','l','p'), mov_read_dmlp },
  7368. { MKTAG('S','m','D','m'), mov_read_smdm },
  7369. { MKTAG('C','o','L','L'), mov_read_coll },
  7370. { MKTAG('v','p','c','C'), mov_read_vpcc },
  7371. { MKTAG('m','d','c','v'), mov_read_mdcv },
  7372. { MKTAG('c','l','l','i'), mov_read_clli },
  7373. { MKTAG('d','v','c','C'), mov_read_dvcc_dvvc },
  7374. { MKTAG('d','v','v','C'), mov_read_dvcc_dvvc },
  7375. { MKTAG('d','v','w','C'), mov_read_dvcc_dvvc },
  7376. { MKTAG('k','i','n','d'), mov_read_kind },
  7377. { MKTAG('S','A','3','D'), mov_read_SA3D }, /* ambisonic audio box */
  7378. { MKTAG('S','A','N','D'), mov_read_SAND }, /* non diegetic audio box */
  7379. { MKTAG('i','l','o','c'), mov_read_iloc },
  7380. { MKTAG('p','c','m','C'), mov_read_pcmc }, /* PCM configuration box */
  7381. { MKTAG('p','i','t','m'), mov_read_pitm },
  7382. { MKTAG('e','v','c','C'), mov_read_glbl },
  7383. { MKTAG('i','d','a','t'), mov_read_idat },
  7384. { MKTAG('i','r','e','f'), mov_read_iref },
  7385. { MKTAG('i','s','p','e'), mov_read_ispe },
  7386. { MKTAG('i','p','r','p'), mov_read_iprp },
  7387. { MKTAG('i','i','n','f'), mov_read_iinf },
  7388. { MKTAG('a','m','v','e'), mov_read_amve }, /* ambient viewing environment box */
  7389. #if CONFIG_IAMFDEC
  7390. { MKTAG('i','a','c','b'), mov_read_iacb },
  7391. #endif
  7392. { 0, NULL }
  7393. };
  7394. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  7395. {
  7396. int64_t total_size = 0;
  7397. MOVAtom a;
  7398. int i;
  7399. if (c->atom_depth > 10) {
  7400. av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
  7401. return AVERROR_INVALIDDATA;
  7402. }
  7403. c->atom_depth ++;
  7404. if (atom.size < 0)
  7405. atom.size = INT64_MAX;
  7406. while (total_size <= atom.size - 8) {
  7407. int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
  7408. a.size = avio_rb32(pb);
  7409. a.type = avio_rl32(pb);
  7410. if (avio_feof(pb))
  7411. break;
  7412. if (((a.type == MKTAG('f','r','e','e') && c->moov_retry) ||
  7413. a.type == MKTAG('h','o','o','v')) &&
  7414. a.size >= 8 &&
  7415. c->fc->strict_std_compliance < FF_COMPLIANCE_STRICT) {
  7416. uint32_t type;
  7417. avio_skip(pb, 4);
  7418. type = avio_rl32(pb);
  7419. if (avio_feof(pb))
  7420. break;
  7421. avio_seek(pb, -8, SEEK_CUR);
  7422. if (type == MKTAG('m','v','h','d') ||
  7423. type == MKTAG('c','m','o','v')) {
  7424. av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free or hoov atom.\n");
  7425. a.type = MKTAG('m','o','o','v');
  7426. }
  7427. }
  7428. if (atom.type != MKTAG('r','o','o','t') &&
  7429. atom.type != MKTAG('m','o','o','v')) {
  7430. if (a.type == MKTAG('t','r','a','k') ||
  7431. a.type == MKTAG('m','d','a','t')) {
  7432. av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
  7433. avio_skip(pb, -8);
  7434. c->atom_depth --;
  7435. return 0;
  7436. }
  7437. }
  7438. total_size += 8;
  7439. if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
  7440. a.size = avio_rb64(pb) - 8;
  7441. total_size += 8;
  7442. }
  7443. av_log(c->fc, AV_LOG_TRACE, "type:'%s' parent:'%s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
  7444. av_fourcc2str(a.type), av_fourcc2str(atom.type), a.size, total_size, atom.size);
  7445. if (a.size == 0) {
  7446. a.size = atom.size - total_size + 8;
  7447. }
  7448. if (a.size < 0)
  7449. break;
  7450. a.size -= 8;
  7451. if (a.size < 0)
  7452. break;
  7453. a.size = FFMIN(a.size, atom.size - total_size);
  7454. for (i = 0; mov_default_parse_table[i].type; i++)
  7455. if (mov_default_parse_table[i].type == a.type) {
  7456. parse = mov_default_parse_table[i].parse;
  7457. break;
  7458. }
  7459. // container is user data
  7460. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  7461. atom.type == MKTAG('i','l','s','t')))
  7462. parse = mov_read_udta_string;
  7463. // Supports parsing the QuickTime Metadata Keys.
  7464. // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
  7465. if (!parse && c->found_hdlr_mdta &&
  7466. atom.type == MKTAG('m','e','t','a') &&
  7467. a.type == MKTAG('k','e','y','s') &&
  7468. c->meta_keys_count == 0) {
  7469. parse = mov_read_keys;
  7470. }
  7471. if (!parse) { /* skip leaf atoms data */
  7472. avio_skip(pb, a.size);
  7473. } else {
  7474. int64_t start_pos = avio_tell(pb);
  7475. int64_t left;
  7476. int err = parse(c, pb, a);
  7477. if (err < 0) {
  7478. c->atom_depth --;
  7479. return err;
  7480. }
  7481. if (c->found_moov && c->found_mdat && a.size <= INT64_MAX - start_pos &&
  7482. ((!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete) ||
  7483. start_pos + a.size == avio_size(pb))) {
  7484. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete)
  7485. c->next_root_atom = start_pos + a.size;
  7486. c->atom_depth --;
  7487. return 0;
  7488. }
  7489. left = a.size - avio_tell(pb) + start_pos;
  7490. if (left > 0) /* skip garbage at atom end */
  7491. avio_skip(pb, left);
  7492. else if (left < 0) {
  7493. av_log(c->fc, AV_LOG_WARNING,
  7494. "overread end of atom '%s' by %"PRId64" bytes\n",
  7495. av_fourcc2str(a.type), -left);
  7496. avio_seek(pb, left, SEEK_CUR);
  7497. }
  7498. }
  7499. total_size += a.size;
  7500. }
  7501. if (total_size < atom.size && atom.size < 0x7ffff)
  7502. avio_skip(pb, atom.size - total_size);
  7503. c->atom_depth --;
  7504. return 0;
  7505. }
  7506. static int mov_probe(const AVProbeData *p)
  7507. {
  7508. int64_t offset;
  7509. uint32_t tag;
  7510. int score = 0;
  7511. int moov_offset = -1;
  7512. /* check file header */
  7513. offset = 0;
  7514. for (;;) {
  7515. int64_t size;
  7516. int minsize = 8;
  7517. /* ignore invalid offset */
  7518. if ((offset + 8ULL) > (unsigned int)p->buf_size)
  7519. break;
  7520. size = AV_RB32(p->buf + offset);
  7521. if (size == 1 && offset + 16 <= (unsigned int)p->buf_size) {
  7522. size = AV_RB64(p->buf+offset + 8);
  7523. minsize = 16;
  7524. } else if (size == 0) {
  7525. size = p->buf_size - offset;
  7526. }
  7527. if (size < minsize) {
  7528. offset += 4;
  7529. continue;
  7530. }
  7531. tag = AV_RL32(p->buf + offset + 4);
  7532. switch(tag) {
  7533. /* check for obvious tags */
  7534. case MKTAG('m','o','o','v'):
  7535. moov_offset = offset + 4;
  7536. case MKTAG('m','d','a','t'):
  7537. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  7538. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  7539. case MKTAG('f','t','y','p'):
  7540. if (tag == MKTAG('f','t','y','p') &&
  7541. ( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
  7542. || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
  7543. || AV_RL32(p->buf + offset + 8) == MKTAG('j','x','l',' ')
  7544. )) {
  7545. score = FFMAX(score, 5);
  7546. } else {
  7547. score = AVPROBE_SCORE_MAX;
  7548. }
  7549. break;
  7550. /* those are more common words, so rate then a bit less */
  7551. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  7552. case MKTAG('w','i','d','e'):
  7553. case MKTAG('f','r','e','e'):
  7554. case MKTAG('j','u','n','k'):
  7555. case MKTAG('p','i','c','t'):
  7556. score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
  7557. break;
  7558. case MKTAG(0x82,0x82,0x7f,0x7d):
  7559. score = FFMAX(score, AVPROBE_SCORE_EXTENSION - 5);
  7560. break;
  7561. case MKTAG('s','k','i','p'):
  7562. case MKTAG('u','u','i','d'):
  7563. case MKTAG('p','r','f','l'):
  7564. /* if we only find those cause probedata is too small at least rate them */
  7565. score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
  7566. break;
  7567. }
  7568. if (size > INT64_MAX - offset)
  7569. break;
  7570. offset += size;
  7571. }
  7572. if (score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
  7573. /* moov atom in the header - we should make sure that this is not a
  7574. * MOV-packed MPEG-PS */
  7575. offset = moov_offset;
  7576. while (offset < (p->buf_size - 16)) { /* Sufficient space */
  7577. /* We found an actual hdlr atom */
  7578. if (AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
  7579. AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
  7580. AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')) {
  7581. av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
  7582. /* We found a media handler reference atom describing an
  7583. * MPEG-PS-in-MOV, return a
  7584. * low score to force expanding the probe window until
  7585. * mpegps_probe finds what it needs */
  7586. return 5;
  7587. } else {
  7588. /* Keep looking */
  7589. offset += 2;
  7590. }
  7591. }
  7592. }
  7593. return score;
  7594. }
  7595. // must be done after parsing all trak because there's no order requirement
  7596. static void mov_read_chapters(AVFormatContext *s)
  7597. {
  7598. MOVContext *mov = s->priv_data;
  7599. MOVStreamContext *sc;
  7600. int64_t cur_pos;
  7601. int i, j;
  7602. int chapter_track;
  7603. for (j = 0; j < mov->nb_chapter_tracks; j++) {
  7604. AVStream *st = NULL;
  7605. FFStream *sti = NULL;
  7606. chapter_track = mov->chapter_tracks[j];
  7607. for (i = 0; i < s->nb_streams; i++) {
  7608. sc = mov->fc->streams[i]->priv_data;
  7609. if (sc->id == chapter_track) {
  7610. st = s->streams[i];
  7611. break;
  7612. }
  7613. }
  7614. if (!st) {
  7615. av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
  7616. continue;
  7617. }
  7618. sti = ffstream(st);
  7619. sc = st->priv_data;
  7620. cur_pos = avio_tell(sc->pb);
  7621. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  7622. st->disposition |= AV_DISPOSITION_ATTACHED_PIC | AV_DISPOSITION_TIMED_THUMBNAILS;
  7623. if (!st->attached_pic.data && sti->nb_index_entries) {
  7624. // Retrieve the first frame, if possible
  7625. AVIndexEntry *sample = &sti->index_entries[0];
  7626. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  7627. av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n");
  7628. goto finish;
  7629. }
  7630. if (ff_add_attached_pic(s, st, sc->pb, NULL, sample->size) < 0)
  7631. goto finish;
  7632. }
  7633. } else {
  7634. st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
  7635. st->codecpar->codec_id = AV_CODEC_ID_BIN_DATA;
  7636. st->discard = AVDISCARD_ALL;
  7637. for (int i = 0; i < sti->nb_index_entries; i++) {
  7638. AVIndexEntry *sample = &sti->index_entries[i];
  7639. int64_t end = i+1 < sti->nb_index_entries ? sti->index_entries[i+1].timestamp : st->duration;
  7640. uint8_t *title;
  7641. uint16_t ch;
  7642. int len, title_len;
  7643. if (end < sample->timestamp) {
  7644. av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
  7645. end = AV_NOPTS_VALUE;
  7646. }
  7647. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  7648. av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
  7649. goto finish;
  7650. }
  7651. // the first two bytes are the length of the title
  7652. len = avio_rb16(sc->pb);
  7653. if (len > sample->size-2)
  7654. continue;
  7655. title_len = 2*len + 1;
  7656. if (!(title = av_mallocz(title_len)))
  7657. goto finish;
  7658. // The samples could theoretically be in any encoding if there's an encd
  7659. // atom following, but in practice are only utf-8 or utf-16, distinguished
  7660. // instead by the presence of a BOM
  7661. if (!len) {
  7662. title[0] = 0;
  7663. } else {
  7664. ch = avio_rb16(sc->pb);
  7665. if (ch == 0xfeff)
  7666. avio_get_str16be(sc->pb, len, title, title_len);
  7667. else if (ch == 0xfffe)
  7668. avio_get_str16le(sc->pb, len, title, title_len);
  7669. else {
  7670. AV_WB16(title, ch);
  7671. if (len == 1 || len == 2)
  7672. title[len] = 0;
  7673. else
  7674. avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
  7675. }
  7676. }
  7677. avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
  7678. av_freep(&title);
  7679. }
  7680. }
  7681. finish:
  7682. avio_seek(sc->pb, cur_pos, SEEK_SET);
  7683. }
  7684. }
  7685. static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
  7686. int64_t value, int flags)
  7687. {
  7688. AVTimecode tc;
  7689. char buf[AV_TIMECODE_STR_SIZE];
  7690. AVRational rate = st->avg_frame_rate;
  7691. int ret = av_timecode_init(&tc, rate, flags, 0, s);
  7692. if (ret < 0)
  7693. return ret;
  7694. av_dict_set(&st->metadata, "timecode",
  7695. av_timecode_make_string(&tc, buf, value), 0);
  7696. return 0;
  7697. }
  7698. static int mov_read_rtmd_track(AVFormatContext *s, AVStream *st)
  7699. {
  7700. MOVStreamContext *sc = st->priv_data;
  7701. FFStream *const sti = ffstream(st);
  7702. char buf[AV_TIMECODE_STR_SIZE];
  7703. int64_t cur_pos = avio_tell(sc->pb);
  7704. int hh, mm, ss, ff, drop;
  7705. if (!sti->nb_index_entries)
  7706. return -1;
  7707. avio_seek(sc->pb, sti->index_entries->pos, SEEK_SET);
  7708. avio_skip(s->pb, 13);
  7709. hh = avio_r8(s->pb);
  7710. mm = avio_r8(s->pb);
  7711. ss = avio_r8(s->pb);
  7712. drop = avio_r8(s->pb);
  7713. ff = avio_r8(s->pb);
  7714. snprintf(buf, AV_TIMECODE_STR_SIZE, "%02d:%02d:%02d%c%02d",
  7715. hh, mm, ss, drop ? ';' : ':', ff);
  7716. av_dict_set(&st->metadata, "timecode", buf, 0);
  7717. avio_seek(sc->pb, cur_pos, SEEK_SET);
  7718. return 0;
  7719. }
  7720. static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
  7721. {
  7722. MOVStreamContext *sc = st->priv_data;
  7723. FFStream *const sti = ffstream(st);
  7724. int flags = 0;
  7725. int64_t cur_pos = avio_tell(sc->pb);
  7726. int64_t value;
  7727. AVRational tc_rate = st->avg_frame_rate;
  7728. int tmcd_nb_frames = sc->tmcd_nb_frames;
  7729. int rounded_tc_rate;
  7730. if (!sti->nb_index_entries)
  7731. return -1;
  7732. if (!tc_rate.num || !tc_rate.den || !tmcd_nb_frames)
  7733. return -1;
  7734. avio_seek(sc->pb, sti->index_entries->pos, SEEK_SET);
  7735. value = avio_rb32(s->pb);
  7736. if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
  7737. if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
  7738. if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
  7739. /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
  7740. * not the case) and thus assume "frame number format" instead of QT one.
  7741. * No sample with tmcd track can be found with a QT timecode at the moment,
  7742. * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
  7743. * format). */
  7744. /* 60 fps content have tmcd_nb_frames set to 30 but tc_rate set to 60, so
  7745. * we multiply the frame number with the quotient.
  7746. * See tickets #9492, #9710. */
  7747. rounded_tc_rate = (tc_rate.num + tc_rate.den / 2LL) / tc_rate.den;
  7748. /* Work around files where tmcd_nb_frames is rounded down from frame rate
  7749. * instead of up. See ticket #5978. */
  7750. if (tmcd_nb_frames == tc_rate.num / tc_rate.den &&
  7751. s->strict_std_compliance < FF_COMPLIANCE_STRICT)
  7752. tmcd_nb_frames = rounded_tc_rate;
  7753. value = av_rescale(value, rounded_tc_rate, tmcd_nb_frames);
  7754. parse_timecode_in_framenum_format(s, st, value, flags);
  7755. avio_seek(sc->pb, cur_pos, SEEK_SET);
  7756. return 0;
  7757. }
  7758. static void mov_free_encryption_index(MOVEncryptionIndex **index) {
  7759. int i;
  7760. if (!index || !*index) return;
  7761. for (i = 0; i < (*index)->nb_encrypted_samples; i++) {
  7762. av_encryption_info_free((*index)->encrypted_samples[i]);
  7763. }
  7764. av_freep(&(*index)->encrypted_samples);
  7765. av_freep(&(*index)->auxiliary_info_sizes);
  7766. av_freep(&(*index)->auxiliary_offsets);
  7767. av_freep(index);
  7768. }
  7769. static void mov_free_stream_context(AVFormatContext *s, AVStream *st)
  7770. {
  7771. MOVStreamContext *sc = st->priv_data;
  7772. if (!sc || --sc->refcount) {
  7773. st->priv_data = NULL;
  7774. return;
  7775. }
  7776. av_freep(&sc->ctts_data);
  7777. for (int i = 0; i < sc->drefs_count; i++) {
  7778. av_freep(&sc->drefs[i].path);
  7779. av_freep(&sc->drefs[i].dir);
  7780. }
  7781. av_freep(&sc->drefs);
  7782. sc->drefs_count = 0;
  7783. if (!sc->pb_is_copied)
  7784. ff_format_io_close(s, &sc->pb);
  7785. sc->pb = NULL;
  7786. av_freep(&sc->chunk_offsets);
  7787. av_freep(&sc->stsc_data);
  7788. av_freep(&sc->sample_sizes);
  7789. av_freep(&sc->keyframes);
  7790. av_freep(&sc->stts_data);
  7791. av_freep(&sc->sdtp_data);
  7792. av_freep(&sc->stps_data);
  7793. av_freep(&sc->elst_data);
  7794. av_freep(&sc->rap_group);
  7795. av_freep(&sc->sync_group);
  7796. av_freep(&sc->sgpd_sync);
  7797. av_freep(&sc->sample_offsets);
  7798. av_freep(&sc->open_key_samples);
  7799. av_freep(&sc->display_matrix);
  7800. av_freep(&sc->index_ranges);
  7801. if (sc->extradata)
  7802. for (int i = 0; i < sc->stsd_count; i++)
  7803. av_free(sc->extradata[i]);
  7804. av_freep(&sc->extradata);
  7805. av_freep(&sc->extradata_size);
  7806. mov_free_encryption_index(&sc->cenc.encryption_index);
  7807. av_encryption_info_free(sc->cenc.default_encrypted_sample);
  7808. av_aes_ctr_free(sc->cenc.aes_ctr);
  7809. av_freep(&sc->stereo3d);
  7810. av_freep(&sc->spherical);
  7811. av_freep(&sc->mastering);
  7812. av_freep(&sc->coll);
  7813. av_freep(&sc->ambient);
  7814. #if CONFIG_IAMFDEC
  7815. if (sc->iamf)
  7816. ff_iamf_read_deinit(sc->iamf);
  7817. #endif
  7818. av_freep(&sc->iamf);
  7819. }
  7820. static int mov_read_close(AVFormatContext *s)
  7821. {
  7822. MOVContext *mov = s->priv_data;
  7823. int i, j;
  7824. for (i = 0; i < s->nb_streams; i++) {
  7825. AVStream *st = s->streams[i];
  7826. mov_free_stream_context(s, st);
  7827. }
  7828. av_freep(&mov->dv_demux);
  7829. avformat_free_context(mov->dv_fctx);
  7830. mov->dv_fctx = NULL;
  7831. if (mov->meta_keys) {
  7832. for (i = 1; i < mov->meta_keys_count; i++) {
  7833. av_freep(&mov->meta_keys[i]);
  7834. }
  7835. av_freep(&mov->meta_keys);
  7836. }
  7837. av_freep(&mov->trex_data);
  7838. av_freep(&mov->bitrates);
  7839. for (i = 0; i < mov->frag_index.nb_items; i++) {
  7840. MOVFragmentStreamInfo *frag = mov->frag_index.item[i].stream_info;
  7841. for (j = 0; j < mov->frag_index.item[i].nb_stream_info; j++) {
  7842. mov_free_encryption_index(&frag[j].encryption_index);
  7843. }
  7844. av_freep(&mov->frag_index.item[i].stream_info);
  7845. }
  7846. av_freep(&mov->frag_index.item);
  7847. av_freep(&mov->aes_decrypt);
  7848. av_freep(&mov->chapter_tracks);
  7849. for (i = 0; i < mov->nb_heif_item; i++)
  7850. av_freep(&mov->heif_item[i].name);
  7851. av_freep(&mov->heif_item);
  7852. for (i = 0; i < mov->nb_heif_grid; i++) {
  7853. av_freep(&mov->heif_grid[i].tile_id_list);
  7854. av_freep(&mov->heif_grid[i].tile_item_list);
  7855. }
  7856. av_freep(&mov->heif_grid);
  7857. return 0;
  7858. }
  7859. static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
  7860. {
  7861. int i;
  7862. for (i = 0; i < s->nb_streams; i++) {
  7863. AVStream *st = s->streams[i];
  7864. MOVStreamContext *sc = st->priv_data;
  7865. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  7866. sc->timecode_track == tmcd_id)
  7867. return 1;
  7868. }
  7869. return 0;
  7870. }
  7871. /* look for a tmcd track not referenced by any video track, and export it globally */
  7872. static void export_orphan_timecode(AVFormatContext *s)
  7873. {
  7874. int i;
  7875. for (i = 0; i < s->nb_streams; i++) {
  7876. AVStream *st = s->streams[i];
  7877. if (st->codecpar->codec_tag == MKTAG('t','m','c','d') &&
  7878. !tmcd_is_referenced(s, i + 1)) {
  7879. AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
  7880. if (tcr) {
  7881. av_dict_set(&s->metadata, "timecode", tcr->value, 0);
  7882. break;
  7883. }
  7884. }
  7885. }
  7886. }
  7887. static int read_tfra(MOVContext *mov, AVIOContext *f)
  7888. {
  7889. int version, fieldlength, i, j;
  7890. int64_t pos = avio_tell(f);
  7891. uint32_t size = avio_rb32(f);
  7892. unsigned track_id, item_count;
  7893. if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
  7894. return 1;
  7895. }
  7896. av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
  7897. version = avio_r8(f);
  7898. avio_rb24(f);
  7899. track_id = avio_rb32(f);
  7900. fieldlength = avio_rb32(f);
  7901. item_count = avio_rb32(f);
  7902. for (i = 0; i < item_count; i++) {
  7903. int64_t time, offset;
  7904. int index;
  7905. MOVFragmentStreamInfo * frag_stream_info;
  7906. if (avio_feof(f)) {
  7907. return AVERROR_INVALIDDATA;
  7908. }
  7909. if (version == 1) {
  7910. time = avio_rb64(f);
  7911. offset = avio_rb64(f);
  7912. } else {
  7913. time = avio_rb32(f);
  7914. offset = avio_rb32(f);
  7915. }
  7916. // The first sample of each stream in a fragment is always a random
  7917. // access sample. So it's entry in the tfra can be used as the
  7918. // initial PTS of the fragment.
  7919. index = update_frag_index(mov, offset);
  7920. frag_stream_info = get_frag_stream_info(&mov->frag_index, index, track_id);
  7921. if (frag_stream_info &&
  7922. frag_stream_info->first_tfra_pts == AV_NOPTS_VALUE)
  7923. frag_stream_info->first_tfra_pts = time;
  7924. for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
  7925. avio_r8(f);
  7926. for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
  7927. avio_r8(f);
  7928. for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
  7929. avio_r8(f);
  7930. }
  7931. avio_seek(f, pos + size, SEEK_SET);
  7932. return 0;
  7933. }
  7934. static int mov_read_mfra(MOVContext *c, AVIOContext *f)
  7935. {
  7936. int64_t stream_size = avio_size(f);
  7937. int64_t original_pos = avio_tell(f);
  7938. int64_t seek_ret;
  7939. int ret = -1;
  7940. if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
  7941. ret = seek_ret;
  7942. goto fail;
  7943. }
  7944. c->mfra_size = avio_rb32(f);
  7945. c->have_read_mfra_size = 1;
  7946. if (!c->mfra_size || c->mfra_size > stream_size) {
  7947. av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
  7948. goto fail;
  7949. }
  7950. if ((seek_ret = avio_seek(f, -((int64_t) c->mfra_size), SEEK_CUR)) < 0) {
  7951. ret = seek_ret;
  7952. goto fail;
  7953. }
  7954. if (avio_rb32(f) != c->mfra_size) {
  7955. av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
  7956. goto fail;
  7957. }
  7958. if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
  7959. av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
  7960. goto fail;
  7961. }
  7962. av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
  7963. do {
  7964. ret = read_tfra(c, f);
  7965. if (ret < 0)
  7966. goto fail;
  7967. } while (!ret);
  7968. ret = 0;
  7969. c->frag_index.complete = 1;
  7970. fail:
  7971. seek_ret = avio_seek(f, original_pos, SEEK_SET);
  7972. if (seek_ret < 0) {
  7973. av_log(c->fc, AV_LOG_ERROR,
  7974. "failed to seek back after looking for mfra\n");
  7975. ret = seek_ret;
  7976. }
  7977. return ret;
  7978. }
  7979. static int read_image_grid(AVFormatContext *s, const HEIFGrid *grid,
  7980. AVStreamGroupTileGrid *tile_grid)
  7981. {
  7982. MOVContext *c = s->priv_data;
  7983. const HEIFItem *item = grid->item;
  7984. int64_t offset = 0, pos = avio_tell(s->pb);
  7985. int x = 0, y = 0, i = 0;
  7986. int tile_rows, tile_cols;
  7987. int flags, size;
  7988. if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
  7989. av_log(c->fc, AV_LOG_INFO, "grid box with non seekable input\n");
  7990. return AVERROR_PATCHWELCOME;
  7991. }
  7992. if (item->is_idat_relative) {
  7993. if (!c->idat_offset) {
  7994. av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the image grid\n");
  7995. return AVERROR_INVALIDDATA;
  7996. }
  7997. offset = c->idat_offset;
  7998. }
  7999. avio_seek(s->pb, item->extent_offset + offset, SEEK_SET);
  8000. avio_r8(s->pb); /* version */
  8001. flags = avio_r8(s->pb);
  8002. tile_rows = avio_r8(s->pb) + 1;
  8003. tile_cols = avio_r8(s->pb) + 1;
  8004. /* actual width and height of output image */
  8005. tile_grid->width = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
  8006. tile_grid->height = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
  8007. av_log(c->fc, AV_LOG_TRACE, "grid: grid_rows %d grid_cols %d output_width %d output_height %d\n",
  8008. tile_rows, tile_cols, tile_grid->width, tile_grid->height);
  8009. avio_seek(s->pb, pos, SEEK_SET);
  8010. size = tile_rows * tile_cols;
  8011. tile_grid->nb_tiles = grid->nb_tiles;
  8012. if (tile_grid->nb_tiles != size)
  8013. return AVERROR_INVALIDDATA;
  8014. for (int i = 0; i < tile_cols; i++)
  8015. tile_grid->coded_width += grid->tile_item_list[i]->width;
  8016. for (int i = 0; i < size; i += tile_cols)
  8017. tile_grid->coded_height += grid->tile_item_list[i]->height;
  8018. tile_grid->offsets = av_calloc(tile_grid->nb_tiles, sizeof(*tile_grid->offsets));
  8019. if (!tile_grid->offsets)
  8020. return AVERROR(ENOMEM);
  8021. while (y < tile_grid->coded_height) {
  8022. int left_col = i;
  8023. while (x < tile_grid->coded_width) {
  8024. if (i == tile_grid->nb_tiles)
  8025. return AVERROR_INVALIDDATA;
  8026. tile_grid->offsets[i].idx = i;
  8027. tile_grid->offsets[i].horizontal = x;
  8028. tile_grid->offsets[i].vertical = y;
  8029. x += grid->tile_item_list[i++]->width;
  8030. }
  8031. if (x > tile_grid->coded_width) {
  8032. av_log(c->fc, AV_LOG_ERROR, "Non uniform HEIF tiles\n");
  8033. return AVERROR_INVALIDDATA;
  8034. }
  8035. x = 0;
  8036. y += grid->tile_item_list[left_col]->height;
  8037. }
  8038. if (y > tile_grid->coded_height || i != tile_grid->nb_tiles) {
  8039. av_log(c->fc, AV_LOG_ERROR, "Non uniform HEIF tiles\n");
  8040. return AVERROR_INVALIDDATA;
  8041. }
  8042. return 0;
  8043. }
  8044. static int read_image_iovl(AVFormatContext *s, const HEIFGrid *grid,
  8045. AVStreamGroupTileGrid *tile_grid)
  8046. {
  8047. MOVContext *c = s->priv_data;
  8048. const HEIFItem *item = grid->item;
  8049. uint16_t canvas_fill_value[4];
  8050. int64_t offset = 0, pos = avio_tell(s->pb);
  8051. int ret = 0, flags;
  8052. if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
  8053. av_log(c->fc, AV_LOG_INFO, "iovl box with non seekable input\n");
  8054. return AVERROR_PATCHWELCOME;
  8055. }
  8056. if (item->is_idat_relative) {
  8057. if (!c->idat_offset) {
  8058. av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the image overlay\n");
  8059. return AVERROR_INVALIDDATA;
  8060. }
  8061. offset = c->idat_offset;
  8062. }
  8063. avio_seek(s->pb, item->extent_offset + offset, SEEK_SET);
  8064. avio_r8(s->pb); /* version */
  8065. flags = avio_r8(s->pb);
  8066. for (int i = 0; i < 4; i++)
  8067. canvas_fill_value[i] = avio_rb16(s->pb);
  8068. av_log(c->fc, AV_LOG_TRACE, "iovl: canvas_fill_value { %u, %u, %u, %u }\n",
  8069. canvas_fill_value[0], canvas_fill_value[1],
  8070. canvas_fill_value[2], canvas_fill_value[3]);
  8071. for (int i = 0; i < 4; i++)
  8072. tile_grid->background[i] = canvas_fill_value[i];
  8073. /* actual width and height of output image */
  8074. tile_grid->width =
  8075. tile_grid->coded_width = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
  8076. tile_grid->height =
  8077. tile_grid->coded_height = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
  8078. av_log(c->fc, AV_LOG_TRACE, "iovl: output_width %d, output_height %d\n",
  8079. tile_grid->width, tile_grid->height);
  8080. tile_grid->nb_tiles = grid->nb_tiles;
  8081. tile_grid->offsets = av_malloc_array(tile_grid->nb_tiles, sizeof(*tile_grid->offsets));
  8082. if (!tile_grid->offsets) {
  8083. ret = AVERROR(ENOMEM);
  8084. goto fail;
  8085. }
  8086. for (int i = 0; i < tile_grid->nb_tiles; i++) {
  8087. tile_grid->offsets[i].idx = grid->tile_item_list[i]->st->index;
  8088. tile_grid->offsets[i].horizontal = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
  8089. tile_grid->offsets[i].vertical = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
  8090. av_log(c->fc, AV_LOG_TRACE, "iovl: stream_idx[%d] %u, "
  8091. "horizontal_offset[%d] %d, vertical_offset[%d] %d\n",
  8092. i, tile_grid->offsets[i].idx,
  8093. i, tile_grid->offsets[i].horizontal, i, tile_grid->offsets[i].vertical);
  8094. }
  8095. fail:
  8096. avio_seek(s->pb, pos, SEEK_SET);
  8097. return ret;
  8098. }
  8099. static int mov_parse_tiles(AVFormatContext *s)
  8100. {
  8101. MOVContext *mov = s->priv_data;
  8102. for (int i = 0; i < mov->nb_heif_grid; i++) {
  8103. AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_TILE_GRID, NULL);
  8104. AVStreamGroupTileGrid *tile_grid;
  8105. const HEIFGrid *grid = &mov->heif_grid[i];
  8106. int err, loop = 1;
  8107. if (!stg)
  8108. return AVERROR(ENOMEM);
  8109. stg->id = grid->item->item_id;
  8110. tile_grid = stg->params.tile_grid;
  8111. for (int j = 0; j < grid->nb_tiles; j++) {
  8112. int tile_id = grid->tile_id_list[j];
  8113. int k;
  8114. for (k = 0; k < mov->nb_heif_item; k++) {
  8115. HEIFItem *item = &mov->heif_item[k];
  8116. AVStream *st = item->st;
  8117. if (item->item_id != tile_id)
  8118. continue;
  8119. if (!st) {
  8120. av_log(s, AV_LOG_WARNING, "HEIF item id %d from grid id %d doesn't "
  8121. "reference a stream\n",
  8122. tile_id, grid->item->item_id);
  8123. ff_remove_stream_group(s, stg);
  8124. loop = 0;
  8125. break;
  8126. }
  8127. grid->tile_item_list[j] = item;
  8128. err = avformat_stream_group_add_stream(stg, st);
  8129. if (err < 0 && err != AVERROR(EEXIST))
  8130. return err;
  8131. if (item->item_id != mov->primary_item_id)
  8132. st->disposition |= AV_DISPOSITION_DEPENDENT;
  8133. break;
  8134. }
  8135. if (k == grid->nb_tiles) {
  8136. av_log(s, AV_LOG_WARNING, "HEIF item id %d referenced by grid id %d doesn't "
  8137. "exist\n",
  8138. tile_id, grid->item->item_id);
  8139. ff_remove_stream_group(s, stg);
  8140. loop = 0;
  8141. }
  8142. if (!loop)
  8143. break;
  8144. }
  8145. if (!loop)
  8146. continue;
  8147. switch (grid->item->type) {
  8148. case MKTAG('g','r','i','d'):
  8149. err = read_image_grid(s, grid, tile_grid);
  8150. break;
  8151. case MKTAG('i','o','v','l'):
  8152. err = read_image_iovl(s, grid, tile_grid);
  8153. break;
  8154. default:
  8155. av_assert0(0);
  8156. }
  8157. if (err < 0)
  8158. return err;
  8159. if (grid->item->name)
  8160. av_dict_set(&stg->metadata, "title", grid->item->name, 0);
  8161. if (grid->item->item_id == mov->primary_item_id)
  8162. stg->disposition |= AV_DISPOSITION_DEFAULT;
  8163. }
  8164. return 0;
  8165. }
  8166. static int mov_read_header(AVFormatContext *s)
  8167. {
  8168. MOVContext *mov = s->priv_data;
  8169. AVIOContext *pb = s->pb;
  8170. int j, err;
  8171. MOVAtom atom = { AV_RL32("root") };
  8172. int i;
  8173. if (mov->decryption_key_len != 0 && mov->decryption_key_len != AES_CTR_KEY_SIZE) {
  8174. av_log(s, AV_LOG_ERROR, "Invalid decryption key len %d expected %d\n",
  8175. mov->decryption_key_len, AES_CTR_KEY_SIZE);
  8176. return AVERROR(EINVAL);
  8177. }
  8178. mov->fc = s;
  8179. mov->trak_index = -1;
  8180. mov->thmb_item_id = -1;
  8181. mov->primary_item_id = -1;
  8182. mov->cur_item_id = -1;
  8183. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  8184. if (pb->seekable & AVIO_SEEKABLE_NORMAL)
  8185. atom.size = avio_size(pb);
  8186. else
  8187. atom.size = INT64_MAX;
  8188. /* check MOV header */
  8189. do {
  8190. if (mov->moov_retry)
  8191. avio_seek(pb, 0, SEEK_SET);
  8192. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  8193. av_log(s, AV_LOG_ERROR, "error reading header\n");
  8194. return err;
  8195. }
  8196. } while ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !mov->found_moov && !mov->found_iloc && !mov->moov_retry++);
  8197. if (!mov->found_moov && !mov->found_iloc) {
  8198. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  8199. return AVERROR_INVALIDDATA;
  8200. }
  8201. av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
  8202. if (mov->found_iloc) {
  8203. for (i = 0; i < mov->nb_heif_item; i++) {
  8204. HEIFItem *item = &mov->heif_item[i];
  8205. MOVStreamContext *sc;
  8206. AVStream *st;
  8207. int64_t offset = 0;
  8208. if (!item->st) {
  8209. if (item->item_id == mov->thmb_item_id) {
  8210. av_log(s, AV_LOG_ERROR, "HEIF thumbnail doesn't reference a stream\n");
  8211. return AVERROR_INVALIDDATA;
  8212. }
  8213. continue;
  8214. }
  8215. if (item->is_idat_relative) {
  8216. if (!mov->idat_offset) {
  8217. av_log(s, AV_LOG_ERROR, "Missing idat box for item %d\n", item->item_id);
  8218. return AVERROR_INVALIDDATA;
  8219. }
  8220. offset = mov->idat_offset;
  8221. }
  8222. st = item->st;
  8223. sc = st->priv_data;
  8224. st->codecpar->width = item->width;
  8225. st->codecpar->height = item->height;
  8226. sc->sample_sizes[0] = item->extent_length;
  8227. sc->chunk_offsets[0] = item->extent_offset + offset;
  8228. if (item->item_id == mov->primary_item_id)
  8229. st->disposition |= AV_DISPOSITION_DEFAULT;
  8230. mov_build_index(mov, st);
  8231. }
  8232. if (mov->nb_heif_grid) {
  8233. err = mov_parse_tiles(s);
  8234. if (err < 0)
  8235. return err;
  8236. }
  8237. }
  8238. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  8239. if (mov->nb_chapter_tracks > 0 && !mov->ignore_chapters)
  8240. mov_read_chapters(s);
  8241. for (i = 0; i < s->nb_streams; i++)
  8242. if (s->streams[i]->codecpar->codec_tag == AV_RL32("tmcd")) {
  8243. mov_read_timecode_track(s, s->streams[i]);
  8244. } else if (s->streams[i]->codecpar->codec_tag == AV_RL32("rtmd")) {
  8245. mov_read_rtmd_track(s, s->streams[i]);
  8246. }
  8247. }
  8248. /* copy timecode metadata from tmcd tracks to the related video streams */
  8249. for (i = 0; i < s->nb_streams; i++) {
  8250. AVStream *st = s->streams[i];
  8251. MOVStreamContext *sc = st->priv_data;
  8252. if (sc->timecode_track > 0) {
  8253. AVDictionaryEntry *tcr;
  8254. int tmcd_st_id = -1;
  8255. for (j = 0; j < s->nb_streams; j++) {
  8256. MOVStreamContext *sc2 = s->streams[j]->priv_data;
  8257. if (sc2->id == sc->timecode_track)
  8258. tmcd_st_id = j;
  8259. }
  8260. if (tmcd_st_id < 0 || tmcd_st_id == i)
  8261. continue;
  8262. tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
  8263. if (tcr)
  8264. av_dict_set(&st->metadata, "timecode", tcr->value, 0);
  8265. }
  8266. }
  8267. export_orphan_timecode(s);
  8268. for (i = 0; i < s->nb_streams; i++) {
  8269. AVStream *st = s->streams[i];
  8270. FFStream *const sti = ffstream(st);
  8271. MOVStreamContext *sc = st->priv_data;
  8272. fix_timescale(mov, sc);
  8273. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
  8274. st->codecpar->codec_id == AV_CODEC_ID_AAC) {
  8275. sti->skip_samples = sc->start_pad;
  8276. }
  8277. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
  8278. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  8279. sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
  8280. if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  8281. if (st->codecpar->width <= 0 || st->codecpar->height <= 0) {
  8282. st->codecpar->width = sc->width;
  8283. st->codecpar->height = sc->height;
  8284. }
  8285. if (st->codecpar->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
  8286. if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
  8287. return err;
  8288. }
  8289. }
  8290. if (mov->handbrake_version &&
  8291. mov->handbrake_version <= 1000000*0 + 1000*10 + 2 && // 0.10.2
  8292. st->codecpar->codec_id == AV_CODEC_ID_MP3) {
  8293. av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
  8294. sti->need_parsing = AVSTREAM_PARSE_FULL;
  8295. }
  8296. }
  8297. if (mov->trex_data) {
  8298. for (i = 0; i < s->nb_streams; i++) {
  8299. AVStream *st = s->streams[i];
  8300. MOVStreamContext *sc = st->priv_data;
  8301. if (st->duration > 0) {
  8302. /* Akin to sc->data_size * 8 * sc->time_scale / st->duration but accounting for overflows. */
  8303. st->codecpar->bit_rate = av_rescale(sc->data_size, ((int64_t) sc->time_scale) * 8, st->duration);
  8304. if (st->codecpar->bit_rate == INT64_MIN) {
  8305. av_log(s, AV_LOG_WARNING, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
  8306. sc->data_size, sc->time_scale);
  8307. st->codecpar->bit_rate = 0;
  8308. if (s->error_recognition & AV_EF_EXPLODE)
  8309. return AVERROR_INVALIDDATA;
  8310. }
  8311. }
  8312. }
  8313. }
  8314. if (mov->use_mfra_for > 0) {
  8315. for (i = 0; i < s->nb_streams; i++) {
  8316. AVStream *st = s->streams[i];
  8317. MOVStreamContext *sc = st->priv_data;
  8318. if (sc->duration_for_fps > 0) {
  8319. /* Akin to sc->data_size * 8 * sc->time_scale / sc->duration_for_fps but accounting for overflows. */
  8320. st->codecpar->bit_rate = av_rescale(sc->data_size, ((int64_t) sc->time_scale) * 8, sc->duration_for_fps);
  8321. if (st->codecpar->bit_rate == INT64_MIN) {
  8322. av_log(s, AV_LOG_WARNING, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
  8323. sc->data_size, sc->time_scale);
  8324. st->codecpar->bit_rate = 0;
  8325. if (s->error_recognition & AV_EF_EXPLODE)
  8326. return AVERROR_INVALIDDATA;
  8327. }
  8328. }
  8329. }
  8330. }
  8331. for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
  8332. if (mov->bitrates[i]) {
  8333. s->streams[i]->codecpar->bit_rate = mov->bitrates[i];
  8334. }
  8335. }
  8336. ff_rfps_calculate(s);
  8337. for (i = 0; i < s->nb_streams; i++) {
  8338. AVStream *st = s->streams[i];
  8339. MOVStreamContext *sc = st->priv_data;
  8340. switch (st->codecpar->codec_type) {
  8341. case AVMEDIA_TYPE_AUDIO:
  8342. err = ff_replaygain_export(st, s->metadata);
  8343. if (err < 0)
  8344. return err;
  8345. break;
  8346. case AVMEDIA_TYPE_VIDEO:
  8347. if (sc->display_matrix) {
  8348. if (!av_packet_side_data_add(&st->codecpar->coded_side_data, &st->codecpar->nb_coded_side_data,
  8349. AV_PKT_DATA_DISPLAYMATRIX,
  8350. (uint8_t*)sc->display_matrix, sizeof(int32_t) * 9, 0))
  8351. return AVERROR(ENOMEM);
  8352. sc->display_matrix = NULL;
  8353. }
  8354. if (sc->stereo3d) {
  8355. if (!av_packet_side_data_add(&st->codecpar->coded_side_data, &st->codecpar->nb_coded_side_data,
  8356. AV_PKT_DATA_STEREO3D,
  8357. (uint8_t *)sc->stereo3d, sizeof(*sc->stereo3d), 0))
  8358. return AVERROR(ENOMEM);
  8359. sc->stereo3d = NULL;
  8360. }
  8361. if (sc->spherical) {
  8362. if (!av_packet_side_data_add(&st->codecpar->coded_side_data, &st->codecpar->nb_coded_side_data,
  8363. AV_PKT_DATA_SPHERICAL,
  8364. (uint8_t *)sc->spherical, sc->spherical_size, 0))
  8365. return AVERROR(ENOMEM);
  8366. sc->spherical = NULL;
  8367. }
  8368. if (sc->mastering) {
  8369. if (!av_packet_side_data_add(&st->codecpar->coded_side_data, &st->codecpar->nb_coded_side_data,
  8370. AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
  8371. (uint8_t *)sc->mastering, sizeof(*sc->mastering), 0))
  8372. return AVERROR(ENOMEM);
  8373. sc->mastering = NULL;
  8374. }
  8375. if (sc->coll) {
  8376. if (!av_packet_side_data_add(&st->codecpar->coded_side_data, &st->codecpar->nb_coded_side_data,
  8377. AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
  8378. (uint8_t *)sc->coll, sc->coll_size, 0))
  8379. return AVERROR(ENOMEM);
  8380. sc->coll = NULL;
  8381. }
  8382. if (sc->ambient) {
  8383. if (!av_packet_side_data_add(&st->codecpar->coded_side_data, &st->codecpar->nb_coded_side_data,
  8384. AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT,
  8385. (uint8_t *) sc->ambient, sc->ambient_size, 0))
  8386. return AVERROR(ENOMEM);
  8387. sc->ambient = NULL;
  8388. }
  8389. break;
  8390. }
  8391. }
  8392. ff_configure_buffers_for_index(s, AV_TIME_BASE);
  8393. for (i = 0; i < mov->frag_index.nb_items; i++)
  8394. if (mov->frag_index.item[i].moof_offset <= mov->fragment.moof_offset)
  8395. mov->frag_index.item[i].headers_read = 1;
  8396. return 0;
  8397. }
  8398. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  8399. {
  8400. AVIndexEntry *sample = NULL;
  8401. int64_t best_dts = INT64_MAX;
  8402. int i;
  8403. MOVContext *mov = s->priv_data;
  8404. int no_interleave = !mov->interleaved_read || !(s->pb->seekable & AVIO_SEEKABLE_NORMAL);
  8405. for (i = 0; i < s->nb_streams; i++) {
  8406. AVStream *avst = s->streams[i];
  8407. FFStream *const avsti = ffstream(avst);
  8408. MOVStreamContext *msc = avst->priv_data;
  8409. if (msc->pb && msc->current_sample < avsti->nb_index_entries) {
  8410. AVIndexEntry *current_sample = &avsti->index_entries[msc->current_sample];
  8411. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  8412. uint64_t dtsdiff = best_dts > dts ? best_dts - (uint64_t)dts : ((uint64_t)dts - best_dts);
  8413. av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  8414. if (!sample || (no_interleave && current_sample->pos < sample->pos) ||
  8415. ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
  8416. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb && dts != AV_NOPTS_VALUE &&
  8417. ((dtsdiff <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  8418. (dtsdiff > AV_TIME_BASE && dts < best_dts)))))) {
  8419. sample = current_sample;
  8420. best_dts = dts;
  8421. *st = avst;
  8422. }
  8423. }
  8424. }
  8425. return sample;
  8426. }
  8427. static int should_retry(AVIOContext *pb, int error_code) {
  8428. if (error_code == AVERROR_EOF || avio_feof(pb))
  8429. return 0;
  8430. return 1;
  8431. }
  8432. static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
  8433. {
  8434. int ret;
  8435. MOVContext *mov = s->priv_data;
  8436. if (index >= 0 && index < mov->frag_index.nb_items)
  8437. target = mov->frag_index.item[index].moof_offset;
  8438. if (avio_seek(s->pb, target, SEEK_SET) != target) {
  8439. av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
  8440. return AVERROR_INVALIDDATA;
  8441. }
  8442. mov->next_root_atom = 0;
  8443. if (index < 0 || index >= mov->frag_index.nb_items)
  8444. index = search_frag_moof_offset(&mov->frag_index, target);
  8445. if (index < mov->frag_index.nb_items &&
  8446. mov->frag_index.item[index].moof_offset == target) {
  8447. if (index + 1 < mov->frag_index.nb_items)
  8448. mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
  8449. if (mov->frag_index.item[index].headers_read)
  8450. return 0;
  8451. mov->frag_index.item[index].headers_read = 1;
  8452. }
  8453. mov->found_mdat = 0;
  8454. ret = mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX });
  8455. if (ret < 0)
  8456. return ret;
  8457. if (avio_feof(s->pb))
  8458. return AVERROR_EOF;
  8459. av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
  8460. return 1;
  8461. }
  8462. static int mov_change_extradata(AVStream *st, AVPacket *pkt)
  8463. {
  8464. MOVStreamContext *sc = st->priv_data;
  8465. uint8_t *side, *extradata;
  8466. int extradata_size;
  8467. /* Save the current index. */
  8468. sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
  8469. /* Notify the decoder that extradata changed. */
  8470. extradata_size = sc->extradata_size[sc->last_stsd_index];
  8471. extradata = sc->extradata[sc->last_stsd_index];
  8472. if (st->discard != AVDISCARD_ALL && extradata_size > 0 && extradata) {
  8473. side = av_packet_new_side_data(pkt,
  8474. AV_PKT_DATA_NEW_EXTRADATA,
  8475. extradata_size);
  8476. if (!side)
  8477. return AVERROR(ENOMEM);
  8478. memcpy(side, extradata, extradata_size);
  8479. }
  8480. return 0;
  8481. }
  8482. static int get_eia608_packet(AVIOContext *pb, AVPacket *pkt, int size)
  8483. {
  8484. int new_size, ret;
  8485. if (size <= 8)
  8486. return AVERROR_INVALIDDATA;
  8487. new_size = ((size - 8) / 2) * 3;
  8488. ret = av_new_packet(pkt, new_size);
  8489. if (ret < 0)
  8490. return ret;
  8491. avio_skip(pb, 8);
  8492. for (int j = 0; j < new_size; j += 3) {
  8493. pkt->data[j] = 0xFC;
  8494. pkt->data[j+1] = avio_r8(pb);
  8495. pkt->data[j+2] = avio_r8(pb);
  8496. }
  8497. return 0;
  8498. }
  8499. static int mov_finalize_packet(AVFormatContext *s, AVStream *st, AVIndexEntry *sample,
  8500. int64_t current_index, AVPacket *pkt)
  8501. {
  8502. MOVStreamContext *sc = st->priv_data;
  8503. pkt->stream_index = sc->ffindex;
  8504. pkt->dts = sample->timestamp;
  8505. if (sample->flags & AVINDEX_DISCARD_FRAME) {
  8506. pkt->flags |= AV_PKT_FLAG_DISCARD;
  8507. }
  8508. if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
  8509. pkt->pts = av_sat_add64(pkt->dts, av_sat_add64(sc->dts_shift, sc->ctts_data[sc->ctts_index].duration));
  8510. /* update ctts context */
  8511. sc->ctts_sample++;
  8512. if (sc->ctts_index < sc->ctts_count &&
  8513. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  8514. sc->ctts_index++;
  8515. sc->ctts_sample = 0;
  8516. }
  8517. } else {
  8518. int64_t next_dts = (sc->current_sample < ffstream(st)->nb_index_entries) ?
  8519. ffstream(st)->index_entries[sc->current_sample].timestamp : st->duration;
  8520. if (next_dts >= pkt->dts)
  8521. pkt->duration = next_dts - pkt->dts;
  8522. pkt->pts = pkt->dts;
  8523. }
  8524. if (sc->sdtp_data && sc->current_sample <= sc->sdtp_count) {
  8525. uint8_t sample_flags = sc->sdtp_data[sc->current_sample - 1];
  8526. uint8_t sample_is_depended_on = (sample_flags >> 2) & 0x3;
  8527. pkt->flags |= sample_is_depended_on == MOV_SAMPLE_DEPENDENCY_NO ? AV_PKT_FLAG_DISPOSABLE : 0;
  8528. }
  8529. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
  8530. pkt->pos = sample->pos;
  8531. /* Multiple stsd handling. */
  8532. if (sc->stsc_data) {
  8533. if (sc->stsc_data[sc->stsc_index].id > 0 &&
  8534. sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count &&
  8535. sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
  8536. int ret = mov_change_extradata(st, pkt);
  8537. if (ret < 0)
  8538. return ret;
  8539. }
  8540. /* Update the stsc index for the next sample */
  8541. sc->stsc_sample++;
  8542. if (mov_stsc_index_valid(sc->stsc_index, sc->stsc_count) &&
  8543. mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) {
  8544. sc->stsc_index++;
  8545. sc->stsc_sample = 0;
  8546. }
  8547. }
  8548. return 0;
  8549. }
  8550. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  8551. {
  8552. MOVContext *mov = s->priv_data;
  8553. MOVStreamContext *sc;
  8554. AVIndexEntry *sample;
  8555. AVStream *st = NULL;
  8556. int64_t current_index;
  8557. int ret;
  8558. mov->fc = s;
  8559. retry:
  8560. sample = mov_find_next_sample(s, &st);
  8561. if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
  8562. if (!mov->next_root_atom)
  8563. return AVERROR_EOF;
  8564. if ((ret = mov_switch_root(s, mov->next_root_atom, -1)) < 0)
  8565. return ret;
  8566. goto retry;
  8567. }
  8568. sc = st->priv_data;
  8569. /* must be done just before reading, to avoid infinite loop on sample */
  8570. current_index = sc->current_index;
  8571. mov_current_sample_inc(sc);
  8572. if (mov->next_root_atom) {
  8573. sample->pos = FFMIN(sample->pos, mov->next_root_atom);
  8574. sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
  8575. }
  8576. if (st->discard != AVDISCARD_ALL) {
  8577. int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
  8578. if (ret64 != sample->pos) {
  8579. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  8580. sc->ffindex, sample->pos);
  8581. if (should_retry(sc->pb, ret64)) {
  8582. mov_current_sample_dec(sc);
  8583. } else if (ret64 < 0) {
  8584. return (int)ret64;
  8585. }
  8586. return AVERROR_INVALIDDATA;
  8587. }
  8588. if (st->discard == AVDISCARD_NONKEY && !(sample->flags & AVINDEX_KEYFRAME)) {
  8589. av_log(mov->fc, AV_LOG_DEBUG, "Nonkey frame from stream %d discarded due to AVDISCARD_NONKEY\n", sc->ffindex);
  8590. goto retry;
  8591. }
  8592. if (st->codecpar->codec_id == AV_CODEC_ID_EIA_608 && sample->size > 8)
  8593. ret = get_eia608_packet(sc->pb, pkt, sample->size);
  8594. #if CONFIG_IAMFDEC
  8595. else if (sc->iamf) {
  8596. int64_t pts, dts, pos, duration;
  8597. int flags, size = sample->size;
  8598. ret = mov_finalize_packet(s, st, sample, current_index, pkt);
  8599. pts = pkt->pts; dts = pkt->dts;
  8600. pos = pkt->pos; flags = pkt->flags;
  8601. duration = pkt->duration;
  8602. while (!ret && size > 0) {
  8603. ret = ff_iamf_read_packet(s, sc->iamf, sc->pb, size, pkt);
  8604. if (ret < 0) {
  8605. if (should_retry(sc->pb, ret))
  8606. mov_current_sample_dec(sc);
  8607. return ret;
  8608. }
  8609. size -= ret;
  8610. pkt->pts = pts; pkt->dts = dts;
  8611. pkt->pos = pos; pkt->flags |= flags;
  8612. pkt->duration = duration;
  8613. ret = ff_buffer_packet(s, pkt);
  8614. }
  8615. if (!ret)
  8616. return FFERROR_REDO;
  8617. }
  8618. #endif
  8619. else
  8620. ret = av_get_packet(sc->pb, pkt, sample->size);
  8621. if (ret < 0) {
  8622. if (should_retry(sc->pb, ret)) {
  8623. mov_current_sample_dec(sc);
  8624. }
  8625. return ret;
  8626. }
  8627. #if CONFIG_DV_DEMUXER
  8628. if (mov->dv_demux && sc->dv_audio_container) {
  8629. ret = avpriv_dv_produce_packet(mov->dv_demux, NULL, pkt->data, pkt->size, pkt->pos);
  8630. av_packet_unref(pkt);
  8631. if (ret < 0)
  8632. return ret;
  8633. ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
  8634. if (ret < 0)
  8635. return ret;
  8636. }
  8637. #endif
  8638. if (sc->has_palette) {
  8639. uint8_t *pal;
  8640. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  8641. if (!pal) {
  8642. av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
  8643. } else {
  8644. memcpy(pal, sc->palette, AVPALETTE_SIZE);
  8645. sc->has_palette = 0;
  8646. }
  8647. }
  8648. if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && !ffstream(st)->need_parsing && pkt->size > 4) {
  8649. if (ff_mpa_check_header(AV_RB32(pkt->data)) < 0)
  8650. ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
  8651. }
  8652. }
  8653. ret = mov_finalize_packet(s, st, sample, current_index, pkt);
  8654. if (ret < 0)
  8655. return ret;
  8656. if (st->discard == AVDISCARD_ALL)
  8657. goto retry;
  8658. if (mov->aax_mode)
  8659. aax_filter(pkt->data, pkt->size, mov);
  8660. ret = cenc_filter(mov, st, sc, pkt, current_index);
  8661. if (ret < 0) {
  8662. return ret;
  8663. }
  8664. return 0;
  8665. }
  8666. static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
  8667. {
  8668. MOVContext *mov = s->priv_data;
  8669. int index;
  8670. if (!mov->frag_index.complete)
  8671. return 0;
  8672. index = search_frag_timestamp(s, &mov->frag_index, st, timestamp);
  8673. if (index < 0)
  8674. index = 0;
  8675. if (!mov->frag_index.item[index].headers_read)
  8676. return mov_switch_root(s, -1, index);
  8677. if (index + 1 < mov->frag_index.nb_items)
  8678. mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
  8679. return 0;
  8680. }
  8681. static int is_open_key_sample(const MOVStreamContext *sc, int sample)
  8682. {
  8683. // TODO: a bisect search would scale much better
  8684. for (int i = 0; i < sc->open_key_samples_count; i++) {
  8685. const int oks = sc->open_key_samples[i];
  8686. if (oks == sample)
  8687. return 1;
  8688. if (oks > sample) /* list is monotically increasing so we can stop early */
  8689. break;
  8690. }
  8691. return 0;
  8692. }
  8693. /*
  8694. * Some key sample may be key frames but not IDR frames, so a random access to
  8695. * them may not be allowed.
  8696. */
  8697. static int can_seek_to_key_sample(AVStream *st, int sample, int64_t requested_pts)
  8698. {
  8699. MOVStreamContext *sc = st->priv_data;
  8700. FFStream *const sti = ffstream(st);
  8701. int64_t key_sample_dts, key_sample_pts;
  8702. if (st->codecpar->codec_id != AV_CODEC_ID_HEVC)
  8703. return 1;
  8704. if (sample >= sc->sample_offsets_count)
  8705. return 1;
  8706. key_sample_dts = sti->index_entries[sample].timestamp;
  8707. key_sample_pts = key_sample_dts + sc->sample_offsets[sample] + sc->dts_shift;
  8708. /*
  8709. * If the sample needs to be presented before an open key sample, they may
  8710. * not be decodable properly, even though they come after in decoding
  8711. * order.
  8712. */
  8713. if (is_open_key_sample(sc, sample) && key_sample_pts > requested_pts)
  8714. return 0;
  8715. return 1;
  8716. }
  8717. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  8718. {
  8719. MOVStreamContext *sc = st->priv_data;
  8720. FFStream *const sti = ffstream(st);
  8721. int sample, time_sample, ret;
  8722. unsigned int i;
  8723. // Here we consider timestamp to be PTS, hence try to offset it so that we
  8724. // can search over the DTS timeline.
  8725. timestamp -= (sc->min_corrected_pts + sc->dts_shift);
  8726. ret = mov_seek_fragment(s, st, timestamp);
  8727. if (ret < 0)
  8728. return ret;
  8729. for (;;) {
  8730. sample = av_index_search_timestamp(st, timestamp, flags);
  8731. av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  8732. if (sample < 0 && sti->nb_index_entries && timestamp < sti->index_entries[0].timestamp)
  8733. sample = 0;
  8734. if (sample < 0) /* not sure what to do */
  8735. return AVERROR_INVALIDDATA;
  8736. if (!sample || can_seek_to_key_sample(st, sample, timestamp))
  8737. break;
  8738. timestamp -= FFMAX(sc->min_sample_duration, 1);
  8739. }
  8740. mov_current_sample_set(sc, sample);
  8741. av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
  8742. /* adjust ctts index */
  8743. if (sc->ctts_data) {
  8744. time_sample = 0;
  8745. for (i = 0; i < sc->ctts_count; i++) {
  8746. int next = time_sample + sc->ctts_data[i].count;
  8747. if (next > sc->current_sample) {
  8748. sc->ctts_index = i;
  8749. sc->ctts_sample = sc->current_sample - time_sample;
  8750. break;
  8751. }
  8752. time_sample = next;
  8753. }
  8754. }
  8755. /* adjust stsd index */
  8756. if (sc->chunk_count) {
  8757. time_sample = 0;
  8758. for (i = 0; i < sc->stsc_count; i++) {
  8759. int64_t next = time_sample + mov_get_stsc_samples(sc, i);
  8760. if (next > sc->current_sample) {
  8761. sc->stsc_index = i;
  8762. sc->stsc_sample = sc->current_sample - time_sample;
  8763. break;
  8764. }
  8765. av_assert0(next == (int)next);
  8766. time_sample = next;
  8767. }
  8768. }
  8769. return sample;
  8770. }
  8771. static int64_t mov_get_skip_samples(AVStream *st, int sample)
  8772. {
  8773. MOVStreamContext *sc = st->priv_data;
  8774. FFStream *const sti = ffstream(st);
  8775. int64_t first_ts = sti->index_entries[0].timestamp;
  8776. int64_t ts = sti->index_entries[sample].timestamp;
  8777. int64_t off;
  8778. if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
  8779. return 0;
  8780. /* compute skip samples according to stream start_pad, seek ts and first ts */
  8781. off = av_rescale_q(ts - first_ts, st->time_base,
  8782. (AVRational){1, st->codecpar->sample_rate});
  8783. return FFMAX(sc->start_pad - off, 0);
  8784. }
  8785. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  8786. {
  8787. MOVContext *mc = s->priv_data;
  8788. AVStream *st;
  8789. FFStream *sti;
  8790. int sample;
  8791. int i;
  8792. if (stream_index >= s->nb_streams)
  8793. return AVERROR_INVALIDDATA;
  8794. st = s->streams[stream_index];
  8795. sti = ffstream(st);
  8796. sample = mov_seek_stream(s, st, sample_time, flags);
  8797. if (sample < 0)
  8798. return sample;
  8799. if (mc->seek_individually) {
  8800. /* adjust seek timestamp to found sample timestamp */
  8801. int64_t seek_timestamp = sti->index_entries[sample].timestamp;
  8802. sti->skip_samples = mov_get_skip_samples(st, sample);
  8803. for (i = 0; i < s->nb_streams; i++) {
  8804. AVStream *const st = s->streams[i];
  8805. FFStream *const sti = ffstream(st);
  8806. int64_t timestamp;
  8807. if (stream_index == i)
  8808. continue;
  8809. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  8810. sample = mov_seek_stream(s, st, timestamp, flags);
  8811. if (sample >= 0)
  8812. sti->skip_samples = mov_get_skip_samples(st, sample);
  8813. }
  8814. } else {
  8815. for (i = 0; i < s->nb_streams; i++) {
  8816. MOVStreamContext *sc;
  8817. st = s->streams[i];
  8818. sc = st->priv_data;
  8819. mov_current_sample_set(sc, 0);
  8820. }
  8821. while (1) {
  8822. MOVStreamContext *sc;
  8823. AVIndexEntry *entry = mov_find_next_sample(s, &st);
  8824. if (!entry)
  8825. return AVERROR_INVALIDDATA;
  8826. sc = st->priv_data;
  8827. if (sc->ffindex == stream_index && sc->current_sample == sample)
  8828. break;
  8829. mov_current_sample_inc(sc);
  8830. }
  8831. }
  8832. return 0;
  8833. }
  8834. #define OFFSET(x) offsetof(MOVContext, x)
  8835. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  8836. static const AVOption mov_options[] = {
  8837. {"use_absolute_path",
  8838. "allow using absolute path when opening alias, this is a possible security issue",
  8839. OFFSET(use_absolute_path), AV_OPT_TYPE_BOOL, {.i64 = 0},
  8840. 0, 1, FLAGS},
  8841. {"seek_streams_individually",
  8842. "Seek each stream individually to the closest point",
  8843. OFFSET(seek_individually), AV_OPT_TYPE_BOOL, { .i64 = 1 },
  8844. 0, 1, FLAGS},
  8845. {"ignore_editlist", "Ignore the edit list atom.", OFFSET(ignore_editlist), AV_OPT_TYPE_BOOL, {.i64 = 0},
  8846. 0, 1, FLAGS},
  8847. {"advanced_editlist",
  8848. "Modify the AVIndex according to the editlists. Use this option to decode in the order specified by the edits.",
  8849. OFFSET(advanced_editlist), AV_OPT_TYPE_BOOL, {.i64 = 1},
  8850. 0, 1, FLAGS},
  8851. {"ignore_chapters", "", OFFSET(ignore_chapters), AV_OPT_TYPE_BOOL, {.i64 = 0},
  8852. 0, 1, FLAGS},
  8853. {"use_mfra_for",
  8854. "use mfra for fragment timestamps",
  8855. OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
  8856. -1, FF_MOV_FLAG_MFRA_PTS, FLAGS,
  8857. .unit = "use_mfra_for"},
  8858. {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
  8859. FLAGS, .unit = "use_mfra_for" },
  8860. {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
  8861. FLAGS, .unit = "use_mfra_for" },
  8862. {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
  8863. FLAGS, .unit = "use_mfra_for" },
  8864. {"use_tfdt", "use tfdt for fragment timestamps", OFFSET(use_tfdt), AV_OPT_TYPE_BOOL, {.i64 = 1},
  8865. 0, 1, FLAGS},
  8866. { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
  8867. AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
  8868. { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
  8869. AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
  8870. { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
  8871. AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
  8872. { "audible_key", "AES-128 Key for Audible AAXC files", OFFSET(audible_key),
  8873. AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
  8874. { "audible_iv", "AES-128 IV for Audible AAXC files", OFFSET(audible_iv),
  8875. AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
  8876. { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
  8877. "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
  8878. AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
  8879. .flags = AV_OPT_FLAG_DECODING_PARAM },
  8880. { "decryption_key", "The media decryption key (hex)", OFFSET(decryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
  8881. { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
  8882. {.i64 = 0}, 0, 1, FLAGS },
  8883. { "max_stts_delta", "treat offsets above this value as invalid", OFFSET(max_stts_delta), AV_OPT_TYPE_INT, {.i64 = UINT_MAX-48000*10 }, 0, UINT_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM },
  8884. { "interleaved_read", "Interleave packets from multiple tracks at demuxer level", OFFSET(interleaved_read), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, .flags = AV_OPT_FLAG_DECODING_PARAM },
  8885. { NULL },
  8886. };
  8887. static const AVClass mov_class = {
  8888. .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
  8889. .item_name = av_default_item_name,
  8890. .option = mov_options,
  8891. .version = LIBAVUTIL_VERSION_INT,
  8892. };
  8893. const FFInputFormat ff_mov_demuxer = {
  8894. .p.name = "mov,mp4,m4a,3gp,3g2,mj2",
  8895. .p.long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
  8896. .p.priv_class = &mov_class,
  8897. .p.extensions = "mov,mp4,m4a,3gp,3g2,mj2,psp,m4b,ism,ismv,isma,f4v,avif,heic,heif",
  8898. .p.flags = AVFMT_NO_BYTE_SEEK | AVFMT_SEEK_TO_PTS | AVFMT_SHOW_IDS,
  8899. .priv_data_size = sizeof(MOVContext),
  8900. .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
  8901. .read_probe = mov_probe,
  8902. .read_header = mov_read_header,
  8903. .read_packet = mov_read_packet,
  8904. .read_close = mov_read_close,
  8905. .read_seek = mov_read_seek,
  8906. };