movenc.c 139 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008
  1. /*
  2. * MOV, 3GP, MP4 muxer
  3. * Copyright (c) 2003 Thomas Raivio
  4. * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
  5. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "movenc.h"
  24. #include "avformat.h"
  25. #include "avio_internal.h"
  26. #include "riff.h"
  27. #include "avio.h"
  28. #include "isom.h"
  29. #include "avc.h"
  30. #include "libavcodec/get_bits.h"
  31. #include "libavcodec/put_bits.h"
  32. #include "libavcodec/vc1.h"
  33. #include "internal.h"
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/intfloat.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/opt.h"
  38. #include "libavutil/dict.h"
  39. #include "rtpenc.h"
  40. #include "mov_chan.h"
  41. #undef NDEBUG
  42. #include <assert.h>
  43. static const AVOption options[] = {
  44. { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  45. { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  46. { "moov_size", "maximum moov size so it can be placed at the begin", offsetof(MOVMuxContext, reserved_moov_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
  47. { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  48. { "frag_keyframe", "Fragment at video keyframes", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_KEYFRAME}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  49. { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  50. { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  51. { "isml", "Create a live smooth streaming feed (for pushing to a publishing point)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_ISML}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  52. { "faststart", "Run a second pass to put the moov at the beginning of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FASTSTART}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  53. FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
  54. { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  55. { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
  56. { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
  57. { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  58. { "min_frag_duration", "Minimum fragment duration", offsetof(MOVMuxContext, min_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  59. { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  60. { "ism_lookahead", "Number of lookahead entries for ISM files", offsetof(MOVMuxContext, ism_lookahead), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  61. { NULL },
  62. };
  63. #define MOV_CLASS(flavor)\
  64. static const AVClass flavor ## _muxer_class = {\
  65. .class_name = #flavor " muxer",\
  66. .item_name = av_default_item_name,\
  67. .option = options,\
  68. .version = LIBAVUTIL_VERSION_INT,\
  69. };
  70. //FIXME support 64 bit variant with wide placeholders
  71. static int64_t update_size(AVIOContext *pb, int64_t pos)
  72. {
  73. int64_t curpos = avio_tell(pb);
  74. avio_seek(pb, pos, SEEK_SET);
  75. avio_wb32(pb, curpos - pos); /* rewrite size */
  76. avio_seek(pb, curpos, SEEK_SET);
  77. return curpos - pos;
  78. }
  79. static int is_co64_required(const MOVTrack *track)
  80. {
  81. int i;
  82. for (i = 0; i < track->entry; i++) {
  83. if (!track->cluster[i].chunkNum)
  84. continue;
  85. if (track->cluster[i].pos + track->data_offset > UINT32_MAX)
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. /* Chunk offset atom */
  91. static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
  92. {
  93. int i;
  94. int mode64 = is_co64_required(track); // use 32 bit size variant if possible
  95. int64_t pos = avio_tell(pb);
  96. avio_wb32(pb, 0); /* size */
  97. if (mode64)
  98. ffio_wfourcc(pb, "co64");
  99. else
  100. ffio_wfourcc(pb, "stco");
  101. avio_wb32(pb, 0); /* version & flags */
  102. avio_wb32(pb, track->chunkCount); /* entry count */
  103. for (i=0; i<track->entry; i++) {
  104. if(!track->cluster[i].chunkNum)
  105. continue;
  106. if(mode64 == 1)
  107. avio_wb64(pb, track->cluster[i].pos + track->data_offset);
  108. else
  109. avio_wb32(pb, track->cluster[i].pos + track->data_offset);
  110. }
  111. return update_size(pb, pos);
  112. }
  113. /* Sample size atom */
  114. static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
  115. {
  116. int equalChunks = 1;
  117. int i, j, entries = 0, tst = -1, oldtst = -1;
  118. int64_t pos = avio_tell(pb);
  119. avio_wb32(pb, 0); /* size */
  120. ffio_wfourcc(pb, "stsz");
  121. avio_wb32(pb, 0); /* version & flags */
  122. for (i=0; i<track->entry; i++) {
  123. tst = track->cluster[i].size/track->cluster[i].entries;
  124. if(oldtst != -1 && tst != oldtst) {
  125. equalChunks = 0;
  126. }
  127. oldtst = tst;
  128. entries += track->cluster[i].entries;
  129. }
  130. if (equalChunks && track->entry) {
  131. int sSize = track->entry ? track->cluster[0].size/track->cluster[0].entries : 0;
  132. sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
  133. avio_wb32(pb, sSize); // sample size
  134. avio_wb32(pb, entries); // sample count
  135. }
  136. else {
  137. avio_wb32(pb, 0); // sample size
  138. avio_wb32(pb, entries); // sample count
  139. for (i=0; i<track->entry; i++) {
  140. for (j=0; j<track->cluster[i].entries; j++) {
  141. avio_wb32(pb, track->cluster[i].size /
  142. track->cluster[i].entries);
  143. }
  144. }
  145. }
  146. return update_size(pb, pos);
  147. }
  148. /* Sample to chunk atom */
  149. static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
  150. {
  151. int index = 0, oldval = -1, i;
  152. int64_t entryPos, curpos;
  153. int64_t pos = avio_tell(pb);
  154. avio_wb32(pb, 0); /* size */
  155. ffio_wfourcc(pb, "stsc");
  156. avio_wb32(pb, 0); // version & flags
  157. entryPos = avio_tell(pb);
  158. avio_wb32(pb, track->chunkCount); // entry count
  159. for (i=0; i<track->entry; i++) {
  160. if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum)
  161. {
  162. avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
  163. avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
  164. avio_wb32(pb, 0x1); // sample description index
  165. oldval = track->cluster[i].samples_in_chunk;
  166. index++;
  167. }
  168. }
  169. curpos = avio_tell(pb);
  170. avio_seek(pb, entryPos, SEEK_SET);
  171. avio_wb32(pb, index); // rewrite size
  172. avio_seek(pb, curpos, SEEK_SET);
  173. return update_size(pb, pos);
  174. }
  175. /* Sync sample atom */
  176. static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
  177. {
  178. int64_t curpos, entryPos;
  179. int i, index = 0;
  180. int64_t pos = avio_tell(pb);
  181. avio_wb32(pb, 0); // size
  182. ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
  183. avio_wb32(pb, 0); // version & flags
  184. entryPos = avio_tell(pb);
  185. avio_wb32(pb, track->entry); // entry count
  186. for (i=0; i<track->entry; i++) {
  187. if (track->cluster[i].flags & flag) {
  188. avio_wb32(pb, i+1);
  189. index++;
  190. }
  191. }
  192. curpos = avio_tell(pb);
  193. avio_seek(pb, entryPos, SEEK_SET);
  194. avio_wb32(pb, index); // rewrite size
  195. avio_seek(pb, curpos, SEEK_SET);
  196. return update_size(pb, pos);
  197. }
  198. static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
  199. {
  200. avio_wb32(pb, 0x11); /* size */
  201. if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
  202. else ffio_wfourcc(pb, "damr");
  203. ffio_wfourcc(pb, "FFMP");
  204. avio_w8(pb, 0); /* decoder version */
  205. avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
  206. avio_w8(pb, 0x00); /* Mode change period (no restriction) */
  207. avio_w8(pb, 0x01); /* Frames per sample */
  208. return 0x11;
  209. }
  210. static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
  211. {
  212. GetBitContext gbc;
  213. PutBitContext pbc;
  214. uint8_t buf[3];
  215. int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
  216. if (track->vos_len < 7)
  217. return -1;
  218. avio_wb32(pb, 11);
  219. ffio_wfourcc(pb, "dac3");
  220. init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
  221. fscod = get_bits(&gbc, 2);
  222. frmsizecod = get_bits(&gbc, 6);
  223. bsid = get_bits(&gbc, 5);
  224. bsmod = get_bits(&gbc, 3);
  225. acmod = get_bits(&gbc, 3);
  226. if (acmod == 2) {
  227. skip_bits(&gbc, 2); // dsurmod
  228. } else {
  229. if ((acmod & 1) && acmod != 1)
  230. skip_bits(&gbc, 2); // cmixlev
  231. if (acmod & 4)
  232. skip_bits(&gbc, 2); // surmixlev
  233. }
  234. lfeon = get_bits1(&gbc);
  235. init_put_bits(&pbc, buf, sizeof(buf));
  236. put_bits(&pbc, 2, fscod);
  237. put_bits(&pbc, 5, bsid);
  238. put_bits(&pbc, 3, bsmod);
  239. put_bits(&pbc, 3, acmod);
  240. put_bits(&pbc, 1, lfeon);
  241. put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
  242. put_bits(&pbc, 5, 0); // reserved
  243. flush_put_bits(&pbc);
  244. avio_write(pb, buf, sizeof(buf));
  245. return 11;
  246. }
  247. /**
  248. * This function writes extradata "as is".
  249. * Extradata must be formatted like a valid atom (with size and tag).
  250. */
  251. static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
  252. {
  253. avio_write(pb, track->enc->extradata, track->enc->extradata_size);
  254. return track->enc->extradata_size;
  255. }
  256. static int mov_write_enda_tag(AVIOContext *pb)
  257. {
  258. avio_wb32(pb, 10);
  259. ffio_wfourcc(pb, "enda");
  260. avio_wb16(pb, 1); /* little endian */
  261. return 10;
  262. }
  263. static int mov_write_enda_tag_be(AVIOContext *pb)
  264. {
  265. avio_wb32(pb, 10);
  266. ffio_wfourcc(pb, "enda");
  267. avio_wb16(pb, 0); /* big endian */
  268. return 10;
  269. }
  270. static void put_descr(AVIOContext *pb, int tag, unsigned int size)
  271. {
  272. int i = 3;
  273. avio_w8(pb, tag);
  274. for(; i>0; i--)
  275. avio_w8(pb, (size>>(7*i)) | 0x80);
  276. avio_w8(pb, size & 0x7F);
  277. }
  278. static unsigned compute_avg_bitrate(MOVTrack *track)
  279. {
  280. uint64_t size = 0;
  281. int i;
  282. if (!track->track_duration)
  283. return 0;
  284. for (i = 0; i < track->entry; i++)
  285. size += track->cluster[i].size;
  286. return size * 8 * track->timescale / track->track_duration;
  287. }
  288. static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
  289. {
  290. int64_t pos = avio_tell(pb);
  291. int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
  292. unsigned avg_bitrate;
  293. avio_wb32(pb, 0); // size
  294. ffio_wfourcc(pb, "esds");
  295. avio_wb32(pb, 0); // Version
  296. // ES descriptor
  297. put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
  298. avio_wb16(pb, track->track_id);
  299. avio_w8(pb, 0x00); // flags (= no flags)
  300. // DecoderConfig descriptor
  301. put_descr(pb, 0x04, 13 + decoder_specific_info_len);
  302. // Object type indication
  303. if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
  304. track->enc->codec_id == AV_CODEC_ID_MP3) &&
  305. track->enc->sample_rate > 24000)
  306. avio_w8(pb, 0x6B); // 11172-3
  307. else
  308. avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
  309. // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
  310. // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
  311. if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  312. avio_w8(pb, 0x15); // flags (= Audiostream)
  313. else
  314. avio_w8(pb, 0x11); // flags (= Visualstream)
  315. avio_w8(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
  316. avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
  317. avg_bitrate = compute_avg_bitrate(track);
  318. // maxbitrate (FIXME should be max rate in any 1 sec window)
  319. avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
  320. avio_wb32(pb, avg_bitrate);
  321. if (track->vos_len) {
  322. // DecoderSpecific info descriptor
  323. put_descr(pb, 0x05, track->vos_len);
  324. avio_write(pb, track->vos_data, track->vos_len);
  325. }
  326. // SL descriptor
  327. put_descr(pb, 0x06, 1);
  328. avio_w8(pb, 0x02);
  329. return update_size(pb, pos);
  330. }
  331. static int mov_pcm_le_gt16(enum AVCodecID codec_id)
  332. {
  333. return codec_id == AV_CODEC_ID_PCM_S24LE ||
  334. codec_id == AV_CODEC_ID_PCM_S32LE ||
  335. codec_id == AV_CODEC_ID_PCM_F32LE ||
  336. codec_id == AV_CODEC_ID_PCM_F64LE;
  337. }
  338. static int mov_pcm_be_gt16(enum AVCodecID codec_id)
  339. {
  340. return codec_id == AV_CODEC_ID_PCM_S24BE ||
  341. codec_id == AV_CODEC_ID_PCM_S32BE ||
  342. codec_id == AV_CODEC_ID_PCM_F32BE ||
  343. codec_id == AV_CODEC_ID_PCM_F64BE;
  344. }
  345. static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
  346. {
  347. int64_t pos = avio_tell(pb);
  348. avio_wb32(pb, 0);
  349. avio_wl32(pb, track->tag); // store it byteswapped
  350. track->enc->codec_tag = av_bswap16(track->tag >> 16);
  351. ff_put_wav_header(pb, track->enc);
  352. return update_size(pb, pos);
  353. }
  354. static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
  355. {
  356. int64_t pos = avio_tell(pb);
  357. avio_wb32(pb, 0);
  358. ffio_wfourcc(pb, "wfex");
  359. ff_put_wav_header(pb, track->enc);
  360. return update_size(pb, pos);
  361. }
  362. static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
  363. {
  364. uint32_t layout_tag, bitmap;
  365. int64_t pos = avio_tell(pb);
  366. layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
  367. track->enc->channel_layout,
  368. &bitmap);
  369. if (!layout_tag) {
  370. av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
  371. "lack of channel information\n");
  372. return 0;
  373. }
  374. avio_wb32(pb, 0); // Size
  375. ffio_wfourcc(pb, "chan"); // Type
  376. avio_w8(pb, 0); // Version
  377. avio_wb24(pb, 0); // Flags
  378. avio_wb32(pb, layout_tag); // mChannelLayoutTag
  379. avio_wb32(pb, bitmap); // mChannelBitmap
  380. avio_wb32(pb, 0); // mNumberChannelDescriptions
  381. return update_size(pb, pos);
  382. }
  383. static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
  384. {
  385. int64_t pos = avio_tell(pb);
  386. avio_wb32(pb, 0); /* size */
  387. ffio_wfourcc(pb, "wave");
  388. if (track->enc->codec_id != AV_CODEC_ID_QDM2) {
  389. avio_wb32(pb, 12); /* size */
  390. ffio_wfourcc(pb, "frma");
  391. avio_wl32(pb, track->tag);
  392. }
  393. if (track->enc->codec_id == AV_CODEC_ID_AAC) {
  394. /* useless atom needed by mplayer, ipod, not needed by quicktime */
  395. avio_wb32(pb, 12); /* size */
  396. ffio_wfourcc(pb, "mp4a");
  397. avio_wb32(pb, 0);
  398. mov_write_esds_tag(pb, track);
  399. } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
  400. mov_write_enda_tag(pb);
  401. } else if (mov_pcm_be_gt16(track->enc->codec_id)) {
  402. mov_write_enda_tag_be(pb);
  403. } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
  404. mov_write_amr_tag(pb, track);
  405. } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
  406. mov_write_ac3_tag(pb, track);
  407. } else if (track->enc->codec_id == AV_CODEC_ID_ALAC ||
  408. track->enc->codec_id == AV_CODEC_ID_QDM2) {
  409. mov_write_extradata_tag(pb, track);
  410. } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  411. track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  412. mov_write_ms_tag(pb, track);
  413. }
  414. avio_wb32(pb, 8); /* size */
  415. avio_wb32(pb, 0); /* null tag */
  416. return update_size(pb, pos);
  417. }
  418. static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
  419. {
  420. uint8_t *unescaped;
  421. const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
  422. int unescaped_size, seq_found = 0;
  423. int level = 0, interlace = 0;
  424. int packet_seq = track->vc1_info.packet_seq;
  425. int packet_entry = track->vc1_info.packet_entry;
  426. int slices = track->vc1_info.slices;
  427. PutBitContext pbc;
  428. if (track->start_dts == AV_NOPTS_VALUE) {
  429. /* No packets written yet, vc1_info isn't authoritative yet. */
  430. /* Assume inline sequence and entry headers. This will be
  431. * overwritten at the end if the file is seekable. */
  432. packet_seq = packet_entry = 1;
  433. }
  434. unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
  435. if (!unescaped)
  436. return AVERROR(ENOMEM);
  437. start = find_next_marker(track->vos_data, end);
  438. for (next = start; next < end; start = next) {
  439. GetBitContext gb;
  440. int size;
  441. next = find_next_marker(start + 4, end);
  442. size = next - start - 4;
  443. if (size <= 0)
  444. continue;
  445. unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
  446. init_get_bits(&gb, unescaped, 8 * unescaped_size);
  447. if (AV_RB32(start) == VC1_CODE_SEQHDR) {
  448. int profile = get_bits(&gb, 2);
  449. if (profile != PROFILE_ADVANCED) {
  450. av_free(unescaped);
  451. return AVERROR(ENOSYS);
  452. }
  453. seq_found = 1;
  454. level = get_bits(&gb, 3);
  455. /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
  456. * width, height */
  457. skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
  458. skip_bits(&gb, 1); /* broadcast */
  459. interlace = get_bits1(&gb);
  460. skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
  461. }
  462. }
  463. if (!seq_found) {
  464. av_free(unescaped);
  465. return AVERROR(ENOSYS);
  466. }
  467. init_put_bits(&pbc, buf, 7);
  468. /* VC1DecSpecStruc */
  469. put_bits(&pbc, 4, 12); /* profile - advanced */
  470. put_bits(&pbc, 3, level);
  471. put_bits(&pbc, 1, 0); /* reserved */
  472. /* VC1AdvDecSpecStruc */
  473. put_bits(&pbc, 3, level);
  474. put_bits(&pbc, 1, 0); /* cbr */
  475. put_bits(&pbc, 6, 0); /* reserved */
  476. put_bits(&pbc, 1, !interlace); /* no interlace */
  477. put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
  478. put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
  479. put_bits(&pbc, 1, !slices); /* no slice code */
  480. put_bits(&pbc, 1, 0); /* no bframe */
  481. put_bits(&pbc, 1, 0); /* reserved */
  482. put_bits32(&pbc, track->enc->time_base.den); /* framerate */
  483. flush_put_bits(&pbc);
  484. av_free(unescaped);
  485. return 0;
  486. }
  487. static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
  488. {
  489. uint8_t buf[7] = { 0 };
  490. int ret;
  491. if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
  492. return ret;
  493. avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
  494. ffio_wfourcc(pb, "dvc1");
  495. track->vc1_info.struct_offset = avio_tell(pb);
  496. avio_write(pb, buf, sizeof(buf));
  497. avio_write(pb, track->vos_data, track->vos_len);
  498. return 0;
  499. }
  500. static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
  501. {
  502. avio_wb32(pb, track->vos_len + 8);
  503. ffio_wfourcc(pb, "glbl");
  504. avio_write(pb, track->vos_data, track->vos_len);
  505. return 8 + track->vos_len;
  506. }
  507. /**
  508. * Compute flags for 'lpcm' tag.
  509. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  510. */
  511. static int mov_get_lpcm_flags(enum AVCodecID codec_id)
  512. {
  513. switch (codec_id) {
  514. case AV_CODEC_ID_PCM_F32BE:
  515. case AV_CODEC_ID_PCM_F64BE:
  516. return 11;
  517. case AV_CODEC_ID_PCM_F32LE:
  518. case AV_CODEC_ID_PCM_F64LE:
  519. return 9;
  520. case AV_CODEC_ID_PCM_U8:
  521. return 10;
  522. case AV_CODEC_ID_PCM_S16BE:
  523. case AV_CODEC_ID_PCM_S24BE:
  524. case AV_CODEC_ID_PCM_S32BE:
  525. return 14;
  526. case AV_CODEC_ID_PCM_S8:
  527. case AV_CODEC_ID_PCM_S16LE:
  528. case AV_CODEC_ID_PCM_S24LE:
  529. case AV_CODEC_ID_PCM_S32LE:
  530. return 12;
  531. default:
  532. return 0;
  533. }
  534. }
  535. static int get_cluster_duration(MOVTrack *track, int cluster_idx)
  536. {
  537. int64_t next_dts;
  538. if (cluster_idx >= track->entry)
  539. return 0;
  540. if (cluster_idx + 1 == track->entry)
  541. next_dts = track->track_duration + track->start_dts;
  542. else
  543. next_dts = track->cluster[cluster_idx + 1].dts;
  544. return next_dts - track->cluster[cluster_idx].dts;
  545. }
  546. static int get_samples_per_packet(MOVTrack *track)
  547. {
  548. int i, first_duration;
  549. // return track->enc->frame_size;
  550. /* use 1 for raw PCM */
  551. if (!track->audio_vbr)
  552. return 1;
  553. /* check to see if duration is constant for all clusters */
  554. if (!track->entry)
  555. return 0;
  556. first_duration = get_cluster_duration(track, 0);
  557. for (i = 1; i < track->entry; i++) {
  558. if (get_cluster_duration(track, i) != first_duration)
  559. return 0;
  560. }
  561. return first_duration;
  562. }
  563. static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
  564. {
  565. int64_t pos = avio_tell(pb);
  566. int version = 0;
  567. uint32_t tag = track->tag;
  568. if (track->mode == MODE_MOV) {
  569. if (track->timescale > UINT16_MAX) {
  570. if (mov_get_lpcm_flags(track->enc->codec_id))
  571. tag = AV_RL32("lpcm");
  572. version = 2;
  573. } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
  574. mov_pcm_be_gt16(track->enc->codec_id) ||
  575. track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  576. track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
  577. track->enc->codec_id == AV_CODEC_ID_QDM2) {
  578. version = 1;
  579. }
  580. }
  581. avio_wb32(pb, 0); /* size */
  582. avio_wl32(pb, tag); // store it byteswapped
  583. avio_wb32(pb, 0); /* Reserved */
  584. avio_wb16(pb, 0); /* Reserved */
  585. avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
  586. /* SoundDescription */
  587. avio_wb16(pb, version); /* Version */
  588. avio_wb16(pb, 0); /* Revision level */
  589. avio_wb32(pb, 0); /* Reserved */
  590. if (version == 2) {
  591. avio_wb16(pb, 3);
  592. avio_wb16(pb, 16);
  593. avio_wb16(pb, 0xfffe);
  594. avio_wb16(pb, 0);
  595. avio_wb32(pb, 0x00010000);
  596. avio_wb32(pb, 72);
  597. avio_wb64(pb, av_double2int(track->enc->sample_rate));
  598. avio_wb32(pb, track->enc->channels);
  599. avio_wb32(pb, 0x7F000000);
  600. avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
  601. avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
  602. avio_wb32(pb, track->sample_size);
  603. avio_wb32(pb, get_samples_per_packet(track));
  604. } else {
  605. if (track->mode == MODE_MOV) {
  606. avio_wb16(pb, track->enc->channels);
  607. if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
  608. track->enc->codec_id == AV_CODEC_ID_PCM_S8)
  609. avio_wb16(pb, 8); /* bits per sample */
  610. else
  611. avio_wb16(pb, 16);
  612. avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
  613. } else { /* reserved for mp4/3gp */
  614. avio_wb16(pb, 2);
  615. avio_wb16(pb, 16);
  616. avio_wb16(pb, 0);
  617. }
  618. avio_wb16(pb, 0); /* packet size (= 0) */
  619. avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
  620. track->enc->sample_rate : 0);
  621. avio_wb16(pb, 0); /* Reserved */
  622. }
  623. if(version == 1) { /* SoundDescription V1 extended info */
  624. avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
  625. avio_wb32(pb, track->sample_size / track->enc->channels); /* Bytes per packet */
  626. avio_wb32(pb, track->sample_size); /* Bytes per frame */
  627. avio_wb32(pb, 2); /* Bytes per sample */
  628. }
  629. if(track->mode == MODE_MOV &&
  630. (track->enc->codec_id == AV_CODEC_ID_AAC ||
  631. track->enc->codec_id == AV_CODEC_ID_AC3 ||
  632. track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
  633. track->enc->codec_id == AV_CODEC_ID_ALAC ||
  634. track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  635. track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
  636. track->enc->codec_id == AV_CODEC_ID_QDM2 ||
  637. (mov_pcm_le_gt16(track->enc->codec_id) && version==1) ||
  638. (mov_pcm_be_gt16(track->enc->codec_id) && version==1)))
  639. mov_write_wave_tag(pb, track);
  640. else if(track->tag == MKTAG('m','p','4','a'))
  641. mov_write_esds_tag(pb, track);
  642. else if(track->enc->codec_id == AV_CODEC_ID_AMR_NB)
  643. mov_write_amr_tag(pb, track);
  644. else if(track->enc->codec_id == AV_CODEC_ID_AC3)
  645. mov_write_ac3_tag(pb, track);
  646. else if(track->enc->codec_id == AV_CODEC_ID_ALAC)
  647. mov_write_extradata_tag(pb, track);
  648. else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
  649. mov_write_wfex_tag(pb, track);
  650. else if (track->vos_len > 0)
  651. mov_write_glbl_tag(pb, track);
  652. if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  653. mov_write_chan_tag(pb, track);
  654. return update_size(pb, pos);
  655. }
  656. static int mov_write_d263_tag(AVIOContext *pb)
  657. {
  658. avio_wb32(pb, 0xf); /* size */
  659. ffio_wfourcc(pb, "d263");
  660. ffio_wfourcc(pb, "FFMP");
  661. avio_w8(pb, 0); /* decoder version */
  662. /* FIXME use AVCodecContext level/profile, when encoder will set values */
  663. avio_w8(pb, 0xa); /* level */
  664. avio_w8(pb, 0); /* profile */
  665. return 0xf;
  666. }
  667. static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
  668. {
  669. int64_t pos = avio_tell(pb);
  670. avio_wb32(pb, 0);
  671. ffio_wfourcc(pb, "avcC");
  672. ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
  673. return update_size(pb, pos);
  674. }
  675. /* also used by all avid codecs (dv, imx, meridien) and their variants */
  676. static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
  677. {
  678. int i;
  679. avio_wb32(pb, 24); /* size */
  680. ffio_wfourcc(pb, "ACLR");
  681. ffio_wfourcc(pb, "ACLR");
  682. ffio_wfourcc(pb, "0001");
  683. avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
  684. avio_wb32(pb, 0); /* unknown */
  685. avio_wb32(pb, 24); /* size */
  686. ffio_wfourcc(pb, "APRG");
  687. ffio_wfourcc(pb, "APRG");
  688. ffio_wfourcc(pb, "0001");
  689. avio_wb32(pb, 1); /* unknown */
  690. avio_wb32(pb, 0); /* unknown */
  691. avio_wb32(pb, 120); /* size */
  692. ffio_wfourcc(pb, "ARES");
  693. ffio_wfourcc(pb, "ARES");
  694. ffio_wfourcc(pb, "0001");
  695. avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
  696. avio_wb32(pb, track->enc->width);
  697. /* values below are based on samples created with quicktime and avid codecs */
  698. if (track->vos_data[5] & 2) { // interlaced
  699. avio_wb32(pb, track->enc->height/2);
  700. avio_wb32(pb, 2); /* unknown */
  701. avio_wb32(pb, 0); /* unknown */
  702. avio_wb32(pb, 4); /* unknown */
  703. } else {
  704. avio_wb32(pb, track->enc->height);
  705. avio_wb32(pb, 1); /* unknown */
  706. avio_wb32(pb, 0); /* unknown */
  707. if (track->enc->height == 1080)
  708. avio_wb32(pb, 5); /* unknown */
  709. else
  710. avio_wb32(pb, 6); /* unknown */
  711. }
  712. /* padding */
  713. for (i = 0; i < 10; i++)
  714. avio_wb64(pb, 0);
  715. /* extra padding for stsd needed */
  716. avio_wb32(pb, 0);
  717. return 0;
  718. }
  719. static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  720. {
  721. int tag = track->enc->codec_tag;
  722. if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
  723. return 0;
  724. if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
  725. else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
  726. else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
  727. else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
  728. else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
  729. else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
  730. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
  731. return tag;
  732. }
  733. static const AVCodecTag codec_ipod_tags[] = {
  734. { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
  735. { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
  736. { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
  737. { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
  738. { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
  739. { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
  740. { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
  741. { AV_CODEC_ID_NONE, 0 },
  742. };
  743. static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  744. {
  745. int tag = track->enc->codec_tag;
  746. // keep original tag for subs, ipod supports both formats
  747. if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  748. (tag == MKTAG('t','x','3','g') ||
  749. tag == MKTAG('t','e','x','t'))))
  750. tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
  751. if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
  752. av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
  753. "Quicktime/Ipod might not play the file\n");
  754. return tag;
  755. }
  756. static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
  757. {
  758. int tag;
  759. if (track->enc->width == 720) /* SD */
  760. if (track->enc->height == 480) /* NTSC */
  761. if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
  762. else tag = MKTAG('d','v','c',' ');
  763. else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
  764. else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
  765. else tag = MKTAG('d','v','p','p');
  766. else if (track->enc->height == 720) /* HD 720 line */
  767. if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
  768. else tag = MKTAG('d','v','h','p');
  769. else if (track->enc->height == 1080) /* HD 1080 line */
  770. if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
  771. else tag = MKTAG('d','v','h','6');
  772. else {
  773. av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
  774. return 0;
  775. }
  776. return tag;
  777. }
  778. static const struct {
  779. enum PixelFormat pix_fmt;
  780. uint32_t tag;
  781. unsigned bps;
  782. } mov_pix_fmt_tags[] = {
  783. { PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
  784. { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
  785. { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
  786. { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
  787. { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
  788. { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
  789. { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
  790. { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
  791. { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
  792. { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
  793. { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
  794. { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
  795. { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
  796. { PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
  797. { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
  798. };
  799. static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
  800. {
  801. int tag = track->enc->codec_tag;
  802. int i;
  803. for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
  804. if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
  805. tag = mov_pix_fmt_tags[i].tag;
  806. track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
  807. if (track->enc->codec_tag == mov_pix_fmt_tags[i].tag)
  808. break;
  809. }
  810. }
  811. return tag;
  812. }
  813. static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  814. {
  815. int tag = track->enc->codec_tag;
  816. if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
  817. (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
  818. track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
  819. track->enc->codec_id == AV_CODEC_ID_H263 ||
  820. av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
  821. if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
  822. tag = mov_get_dv_codec_tag(s, track);
  823. else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
  824. tag = mov_get_rawvideo_codec_tag(s, track);
  825. else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  826. tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
  827. if (!tag) { // if no mac fcc found, try with Microsoft tags
  828. tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
  829. if (tag)
  830. av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
  831. "the file may be unplayable!\n");
  832. }
  833. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  834. tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
  835. if (!tag) { // if no mac fcc found, try with Microsoft tags
  836. int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
  837. if (ms_tag) {
  838. tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
  839. av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
  840. "the file may be unplayable!\n");
  841. }
  842. }
  843. } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
  844. tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
  845. }
  846. return tag;
  847. }
  848. static const AVCodecTag codec_3gp_tags[] = {
  849. { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
  850. { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
  851. { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
  852. { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
  853. { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
  854. { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
  855. { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
  856. { AV_CODEC_ID_NONE, 0 },
  857. };
  858. static const AVCodecTag codec_f4v_tags[] = { // XXX: add GIF/PNG/JPEG?
  859. { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
  860. { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
  861. { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
  862. { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
  863. { AV_CODEC_ID_NONE, 0 },
  864. };
  865. static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
  866. {
  867. int tag;
  868. if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
  869. tag = mp4_get_codec_tag(s, track);
  870. else if (track->mode == MODE_ISM) {
  871. tag = mp4_get_codec_tag(s, track);
  872. if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
  873. tag = MKTAG('w', 'm', 'a', ' ');
  874. } else if (track->mode == MODE_IPOD)
  875. tag = ipod_get_codec_tag(s, track);
  876. else if (track->mode & MODE_3GP)
  877. tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
  878. else if (track->mode & MODE_F4V)
  879. tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
  880. else
  881. tag = mov_get_codec_tag(s, track);
  882. return tag;
  883. }
  884. /** Write uuid atom.
  885. * Needed to make file play in iPods running newest firmware
  886. * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
  887. */
  888. static int mov_write_uuid_tag_ipod(AVIOContext *pb)
  889. {
  890. avio_wb32(pb, 28);
  891. ffio_wfourcc(pb, "uuid");
  892. avio_wb32(pb, 0x6b6840f2);
  893. avio_wb32(pb, 0x5f244fc5);
  894. avio_wb32(pb, 0xba39a51b);
  895. avio_wb32(pb, 0xcf0323f3);
  896. avio_wb32(pb, 0x0);
  897. return 28;
  898. }
  899. static const uint16_t fiel_data[] = {
  900. 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
  901. };
  902. static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
  903. {
  904. unsigned mov_field_order = 0;
  905. if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
  906. mov_field_order = fiel_data[track->enc->field_order];
  907. else
  908. return 0;
  909. avio_wb32(pb, 10);
  910. ffio_wfourcc(pb, "fiel");
  911. avio_wb16(pb, mov_field_order);
  912. return 10;
  913. }
  914. static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
  915. {
  916. int64_t pos = avio_tell(pb);
  917. avio_wb32(pb, 0); /* size */
  918. avio_wl32(pb, track->tag); // store it byteswapped
  919. avio_wb32(pb, 0); /* Reserved */
  920. avio_wb16(pb, 0); /* Reserved */
  921. avio_wb16(pb, 1); /* Data-reference index */
  922. if (track->enc->extradata_size)
  923. avio_write(pb, track->enc->extradata, track->enc->extradata_size);
  924. return update_size(pb, pos);
  925. }
  926. static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
  927. {
  928. AVRational sar;
  929. av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
  930. track->enc->sample_aspect_ratio.den, INT_MAX);
  931. avio_wb32(pb, 16);
  932. ffio_wfourcc(pb, "pasp");
  933. avio_wb32(pb, sar.num);
  934. avio_wb32(pb, sar.den);
  935. return 16;
  936. }
  937. static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
  938. {
  939. int64_t pos = avio_tell(pb);
  940. char compressor_name[32] = { 0 };
  941. avio_wb32(pb, 0); /* size */
  942. avio_wl32(pb, track->tag); // store it byteswapped
  943. avio_wb32(pb, 0); /* Reserved */
  944. avio_wb16(pb, 0); /* Reserved */
  945. avio_wb16(pb, 1); /* Data-reference index */
  946. avio_wb16(pb, 0); /* Codec stream version */
  947. avio_wb16(pb, 0); /* Codec stream revision (=0) */
  948. if (track->mode == MODE_MOV) {
  949. ffio_wfourcc(pb, "FFMP"); /* Vendor */
  950. if(track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
  951. avio_wb32(pb, 0); /* Temporal Quality */
  952. avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
  953. } else {
  954. avio_wb32(pb, 0x200); /* Temporal Quality = normal */
  955. avio_wb32(pb, 0x200); /* Spatial Quality = normal */
  956. }
  957. } else {
  958. avio_wb32(pb, 0); /* Reserved */
  959. avio_wb32(pb, 0); /* Reserved */
  960. avio_wb32(pb, 0); /* Reserved */
  961. }
  962. avio_wb16(pb, track->enc->width); /* Video width */
  963. avio_wb16(pb, track->height); /* Video height */
  964. avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
  965. avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
  966. avio_wb32(pb, 0); /* Data size (= 0) */
  967. avio_wb16(pb, 1); /* Frame count (= 1) */
  968. /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
  969. if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
  970. av_strlcpy(compressor_name,track->enc->codec->name,32);
  971. avio_w8(pb, strlen(compressor_name));
  972. avio_write(pb, compressor_name, 31);
  973. if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
  974. avio_wb16(pb, track->enc->bits_per_coded_sample);
  975. else
  976. avio_wb16(pb, 0x18); /* Reserved */
  977. avio_wb16(pb, 0xffff); /* Reserved */
  978. if(track->tag == MKTAG('m','p','4','v'))
  979. mov_write_esds_tag(pb, track);
  980. else if(track->enc->codec_id == AV_CODEC_ID_H263)
  981. mov_write_d263_tag(pb);
  982. else if(track->enc->codec_id == AV_CODEC_ID_AVUI ||
  983. track->enc->codec_id == AV_CODEC_ID_SVQ3) {
  984. mov_write_extradata_tag(pb, track);
  985. avio_wb32(pb, 0);
  986. } else if(track->enc->codec_id == AV_CODEC_ID_DNXHD)
  987. mov_write_avid_tag(pb, track);
  988. else if(track->enc->codec_id == AV_CODEC_ID_H264) {
  989. mov_write_avcc_tag(pb, track);
  990. if(track->mode == MODE_IPOD)
  991. mov_write_uuid_tag_ipod(pb);
  992. } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
  993. mov_write_fiel_tag(pb, track);
  994. else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
  995. mov_write_dvc1_tag(pb, track);
  996. else if (track->vos_len > 0)
  997. mov_write_glbl_tag(pb, track);
  998. if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
  999. track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
  1000. mov_write_pasp_tag(pb, track);
  1001. }
  1002. return update_size(pb, pos);
  1003. }
  1004. static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
  1005. {
  1006. int64_t pos = avio_tell(pb);
  1007. int frame_duration = track->enc->time_base.num;
  1008. int nb_frames = (track->timescale + frame_duration/2) / frame_duration;
  1009. avio_wb32(pb, 0); /* size */
  1010. ffio_wfourcc(pb, "tmcd"); /* Data format */
  1011. avio_wb32(pb, 0); /* Reserved */
  1012. avio_wb32(pb, 1); /* Data reference index */
  1013. avio_wb32(pb, 0); /* Flags */
  1014. avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
  1015. avio_wb32(pb, track->timescale); /* Timescale */
  1016. avio_wb32(pb, frame_duration); /* Frame duration */
  1017. avio_w8(pb, nb_frames); /* Number of frames */
  1018. avio_wb24(pb, 0); /* Reserved */
  1019. /* TODO: source reference string */
  1020. return update_size(pb, pos);
  1021. }
  1022. static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
  1023. {
  1024. int64_t pos = avio_tell(pb);
  1025. avio_wb32(pb, 0); /* size */
  1026. ffio_wfourcc(pb, "rtp ");
  1027. avio_wb32(pb, 0); /* Reserved */
  1028. avio_wb16(pb, 0); /* Reserved */
  1029. avio_wb16(pb, 1); /* Data-reference index */
  1030. avio_wb16(pb, 1); /* Hint track version */
  1031. avio_wb16(pb, 1); /* Highest compatible version */
  1032. avio_wb32(pb, track->max_packet_size); /* Max packet size */
  1033. avio_wb32(pb, 12); /* size */
  1034. ffio_wfourcc(pb, "tims");
  1035. avio_wb32(pb, track->timescale);
  1036. return update_size(pb, pos);
  1037. }
  1038. static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
  1039. {
  1040. int64_t pos = avio_tell(pb);
  1041. avio_wb32(pb, 0); /* size */
  1042. ffio_wfourcc(pb, "stsd");
  1043. avio_wb32(pb, 0); /* version & flags */
  1044. avio_wb32(pb, 1); /* entry count */
  1045. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
  1046. mov_write_video_tag(pb, track);
  1047. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1048. mov_write_audio_tag(pb, track);
  1049. else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
  1050. mov_write_subtitle_tag(pb, track);
  1051. else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
  1052. mov_write_rtp_tag(pb, track);
  1053. else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
  1054. mov_write_tmcd_tag(pb, track);
  1055. return update_size(pb, pos);
  1056. }
  1057. static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
  1058. {
  1059. MOVStts *ctts_entries;
  1060. uint32_t entries = 0;
  1061. uint32_t atom_size;
  1062. int i;
  1063. ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
  1064. ctts_entries[0].count = 1;
  1065. ctts_entries[0].duration = track->cluster[0].cts;
  1066. for (i=1; i<track->entry; i++) {
  1067. if (track->cluster[i].cts == ctts_entries[entries].duration) {
  1068. ctts_entries[entries].count++; /* compress */
  1069. } else {
  1070. entries++;
  1071. ctts_entries[entries].duration = track->cluster[i].cts;
  1072. ctts_entries[entries].count = 1;
  1073. }
  1074. }
  1075. entries++; /* last one */
  1076. atom_size = 16 + (entries * 8);
  1077. avio_wb32(pb, atom_size); /* size */
  1078. ffio_wfourcc(pb, "ctts");
  1079. avio_wb32(pb, 0); /* version & flags */
  1080. avio_wb32(pb, entries); /* entry count */
  1081. for (i=0; i<entries; i++) {
  1082. avio_wb32(pb, ctts_entries[i].count);
  1083. avio_wb32(pb, ctts_entries[i].duration);
  1084. }
  1085. av_free(ctts_entries);
  1086. return atom_size;
  1087. }
  1088. /* Time to sample atom */
  1089. static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
  1090. {
  1091. MOVStts *stts_entries;
  1092. uint32_t entries = -1;
  1093. uint32_t atom_size;
  1094. int i;
  1095. if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
  1096. stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
  1097. stts_entries[0].count = track->sample_count;
  1098. stts_entries[0].duration = 1;
  1099. entries = 1;
  1100. } else {
  1101. stts_entries = track->entry ?
  1102. av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
  1103. NULL;
  1104. for (i=0; i<track->entry; i++) {
  1105. int duration = get_cluster_duration(track, i);
  1106. if (i && duration == stts_entries[entries].duration) {
  1107. stts_entries[entries].count++; /* compress */
  1108. } else {
  1109. entries++;
  1110. stts_entries[entries].duration = duration;
  1111. stts_entries[entries].count = 1;
  1112. }
  1113. }
  1114. entries++; /* last one */
  1115. }
  1116. atom_size = 16 + (entries * 8);
  1117. avio_wb32(pb, atom_size); /* size */
  1118. ffio_wfourcc(pb, "stts");
  1119. avio_wb32(pb, 0); /* version & flags */
  1120. avio_wb32(pb, entries); /* entry count */
  1121. for (i=0; i<entries; i++) {
  1122. avio_wb32(pb, stts_entries[i].count);
  1123. avio_wb32(pb, stts_entries[i].duration);
  1124. }
  1125. av_free(stts_entries);
  1126. return atom_size;
  1127. }
  1128. static int mov_write_dref_tag(AVIOContext *pb)
  1129. {
  1130. avio_wb32(pb, 28); /* size */
  1131. ffio_wfourcc(pb, "dref");
  1132. avio_wb32(pb, 0); /* version & flags */
  1133. avio_wb32(pb, 1); /* entry count */
  1134. avio_wb32(pb, 0xc); /* size */
  1135. //FIXME add the alis and rsrc atom
  1136. ffio_wfourcc(pb, "url ");
  1137. avio_wb32(pb, 1); /* version & flags */
  1138. return 28;
  1139. }
  1140. static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
  1141. {
  1142. int64_t pos = avio_tell(pb);
  1143. avio_wb32(pb, 0); /* size */
  1144. ffio_wfourcc(pb, "stbl");
  1145. mov_write_stsd_tag(pb, track);
  1146. mov_write_stts_tag(pb, track);
  1147. if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
  1148. track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
  1149. track->has_keyframes && track->has_keyframes < track->entry)
  1150. mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
  1151. if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
  1152. mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
  1153. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  1154. track->flags & MOV_TRACK_CTTS)
  1155. mov_write_ctts_tag(pb, track);
  1156. mov_write_stsc_tag(pb, track);
  1157. mov_write_stsz_tag(pb, track);
  1158. mov_write_stco_tag(pb, track);
  1159. return update_size(pb, pos);
  1160. }
  1161. static int mov_write_dinf_tag(AVIOContext *pb)
  1162. {
  1163. int64_t pos = avio_tell(pb);
  1164. avio_wb32(pb, 0); /* size */
  1165. ffio_wfourcc(pb, "dinf");
  1166. mov_write_dref_tag(pb);
  1167. return update_size(pb, pos);
  1168. }
  1169. static int mov_write_nmhd_tag(AVIOContext *pb)
  1170. {
  1171. avio_wb32(pb, 12);
  1172. ffio_wfourcc(pb, "nmhd");
  1173. avio_wb32(pb, 0);
  1174. return 12;
  1175. }
  1176. static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
  1177. {
  1178. int64_t pos = avio_tell(pb);
  1179. const char *font = "Lucida Grande";
  1180. avio_wb32(pb, 0); /* size */
  1181. ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
  1182. avio_wb32(pb, 0); /* version & flags */
  1183. avio_wb16(pb, 0); /* text font */
  1184. avio_wb16(pb, 0); /* text face */
  1185. avio_wb16(pb, 12); /* text size */
  1186. avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
  1187. avio_wb16(pb, 0x0000); /* text color (red) */
  1188. avio_wb16(pb, 0x0000); /* text color (green) */
  1189. avio_wb16(pb, 0x0000); /* text color (blue) */
  1190. avio_wb16(pb, 0xffff); /* background color (red) */
  1191. avio_wb16(pb, 0xffff); /* background color (green) */
  1192. avio_wb16(pb, 0xffff); /* background color (blue) */
  1193. avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
  1194. avio_write(pb, font, strlen(font)); /* font name */
  1195. return update_size(pb, pos);
  1196. }
  1197. static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
  1198. {
  1199. int64_t pos = avio_tell(pb);
  1200. avio_wb32(pb, 0); /* size */
  1201. ffio_wfourcc(pb, "gmhd");
  1202. avio_wb32(pb, 0x18); /* gmin size */
  1203. ffio_wfourcc(pb, "gmin");/* generic media info */
  1204. avio_wb32(pb, 0); /* version & flags */
  1205. avio_wb16(pb, 0x40); /* graphics mode = */
  1206. avio_wb16(pb, 0x8000); /* opColor (r?) */
  1207. avio_wb16(pb, 0x8000); /* opColor (g?) */
  1208. avio_wb16(pb, 0x8000); /* opColor (b?) */
  1209. avio_wb16(pb, 0); /* balance */
  1210. avio_wb16(pb, 0); /* reserved */
  1211. /*
  1212. * This special text atom is required for
  1213. * Apple Quicktime chapters. The contents
  1214. * don't appear to be documented, so the
  1215. * bytes are copied verbatim.
  1216. */
  1217. avio_wb32(pb, 0x2C); /* size */
  1218. ffio_wfourcc(pb, "text");
  1219. avio_wb16(pb, 0x01);
  1220. avio_wb32(pb, 0x00);
  1221. avio_wb32(pb, 0x00);
  1222. avio_wb32(pb, 0x00);
  1223. avio_wb32(pb, 0x01);
  1224. avio_wb32(pb, 0x00);
  1225. avio_wb32(pb, 0x00);
  1226. avio_wb32(pb, 0x00);
  1227. avio_wb32(pb, 0x00004000);
  1228. avio_wb16(pb, 0x0000);
  1229. if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
  1230. int64_t tmcd_pos = avio_tell(pb);
  1231. avio_wb32(pb, 0); /* size */
  1232. ffio_wfourcc(pb, "tmcd");
  1233. mov_write_tcmi_tag(pb, track);
  1234. update_size(pb, tmcd_pos);
  1235. }
  1236. return update_size(pb, pos);
  1237. }
  1238. static int mov_write_smhd_tag(AVIOContext *pb)
  1239. {
  1240. avio_wb32(pb, 16); /* size */
  1241. ffio_wfourcc(pb, "smhd");
  1242. avio_wb32(pb, 0); /* version & flags */
  1243. avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
  1244. avio_wb16(pb, 0); /* reserved */
  1245. return 16;
  1246. }
  1247. static int mov_write_vmhd_tag(AVIOContext *pb)
  1248. {
  1249. avio_wb32(pb, 0x14); /* size (always 0x14) */
  1250. ffio_wfourcc(pb, "vmhd");
  1251. avio_wb32(pb, 0x01); /* version & flags */
  1252. avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
  1253. return 0x14;
  1254. }
  1255. static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
  1256. {
  1257. const char *hdlr, *descr = NULL, *hdlr_type = NULL;
  1258. int64_t pos = avio_tell(pb);
  1259. if (!track) { /* no media --> data handler */
  1260. hdlr = "dhlr";
  1261. hdlr_type = "url ";
  1262. descr = "DataHandler";
  1263. } else {
  1264. hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
  1265. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  1266. hdlr_type = "vide";
  1267. descr = "VideoHandler";
  1268. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  1269. hdlr_type = "soun";
  1270. descr = "SoundHandler";
  1271. } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  1272. if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
  1273. else hdlr_type = "text";
  1274. descr = "SubtitleHandler";
  1275. } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
  1276. hdlr_type = "tmcd";
  1277. descr = "TimeCodeHandler";
  1278. } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
  1279. hdlr_type = "hint";
  1280. descr = "HintHandler";
  1281. } else {
  1282. hdlr = "dhlr";
  1283. hdlr_type = "url ";
  1284. descr = "DataHandler";
  1285. }
  1286. }
  1287. avio_wb32(pb, 0); /* size */
  1288. ffio_wfourcc(pb, "hdlr");
  1289. avio_wb32(pb, 0); /* Version & flags */
  1290. avio_write(pb, hdlr, 4); /* handler */
  1291. ffio_wfourcc(pb, hdlr_type); /* handler type */
  1292. avio_wb32(pb ,0); /* reserved */
  1293. avio_wb32(pb ,0); /* reserved */
  1294. avio_wb32(pb ,0); /* reserved */
  1295. if (!track || track->mode == MODE_MOV)
  1296. avio_w8(pb, strlen(descr)); /* pascal string */
  1297. avio_write(pb, descr, strlen(descr)); /* handler description */
  1298. if (track && track->mode != MODE_MOV)
  1299. avio_w8(pb, 0); /* c string */
  1300. return update_size(pb, pos);
  1301. }
  1302. static int mov_write_hmhd_tag(AVIOContext *pb)
  1303. {
  1304. /* This atom must be present, but leaving the values at zero
  1305. * seems harmless. */
  1306. avio_wb32(pb, 28); /* size */
  1307. ffio_wfourcc(pb, "hmhd");
  1308. avio_wb32(pb, 0); /* version, flags */
  1309. avio_wb16(pb, 0); /* maxPDUsize */
  1310. avio_wb16(pb, 0); /* avgPDUsize */
  1311. avio_wb32(pb, 0); /* maxbitrate */
  1312. avio_wb32(pb, 0); /* avgbitrate */
  1313. avio_wb32(pb, 0); /* reserved */
  1314. return 28;
  1315. }
  1316. static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
  1317. {
  1318. int64_t pos = avio_tell(pb);
  1319. avio_wb32(pb, 0); /* size */
  1320. ffio_wfourcc(pb, "minf");
  1321. if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
  1322. mov_write_vmhd_tag(pb);
  1323. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1324. mov_write_smhd_tag(pb);
  1325. else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  1326. if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb, track);
  1327. else mov_write_nmhd_tag(pb);
  1328. } else if (track->tag == MKTAG('t','m','c','d')) {
  1329. mov_write_gmhd_tag(pb, track);
  1330. } else if (track->tag == MKTAG('r','t','p',' ')) {
  1331. mov_write_hmhd_tag(pb);
  1332. }
  1333. if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
  1334. mov_write_hdlr_tag(pb, NULL);
  1335. mov_write_dinf_tag(pb);
  1336. mov_write_stbl_tag(pb, track);
  1337. return update_size(pb, pos);
  1338. }
  1339. static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
  1340. {
  1341. int version = track->track_duration < INT32_MAX ? 0 : 1;
  1342. if (track->mode == MODE_ISM)
  1343. version = 1;
  1344. (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
  1345. ffio_wfourcc(pb, "mdhd");
  1346. avio_w8(pb, version);
  1347. avio_wb24(pb, 0); /* flags */
  1348. if (version == 1) {
  1349. avio_wb64(pb, track->time);
  1350. avio_wb64(pb, track->time);
  1351. } else {
  1352. avio_wb32(pb, track->time); /* creation time */
  1353. avio_wb32(pb, track->time); /* modification time */
  1354. }
  1355. avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
  1356. if (!track->entry)
  1357. (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
  1358. else
  1359. (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
  1360. avio_wb16(pb, track->language); /* language */
  1361. avio_wb16(pb, 0); /* reserved (quality) */
  1362. if(version!=0 && track->mode == MODE_MOV){
  1363. av_log(NULL, AV_LOG_ERROR,
  1364. "FATAL error, file duration too long for timebase, this file will not be\n"
  1365. "playable with quicktime. Choose a different timebase or a different\n"
  1366. "container format\n");
  1367. }
  1368. return 32;
  1369. }
  1370. static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
  1371. {
  1372. int64_t pos = avio_tell(pb);
  1373. avio_wb32(pb, 0); /* size */
  1374. ffio_wfourcc(pb, "mdia");
  1375. mov_write_mdhd_tag(pb, track);
  1376. mov_write_hdlr_tag(pb, track);
  1377. mov_write_minf_tag(pb, track);
  1378. return update_size(pb, pos);
  1379. }
  1380. /* transformation matrix
  1381. |a b u|
  1382. |c d v|
  1383. |tx ty w| */
  1384. static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
  1385. int16_t d, int16_t tx, int16_t ty)
  1386. {
  1387. avio_wb32(pb, a << 16); /* 16.16 format */
  1388. avio_wb32(pb, b << 16); /* 16.16 format */
  1389. avio_wb32(pb, 0); /* u in 2.30 format */
  1390. avio_wb32(pb, c << 16); /* 16.16 format */
  1391. avio_wb32(pb, d << 16); /* 16.16 format */
  1392. avio_wb32(pb, 0); /* v in 2.30 format */
  1393. avio_wb32(pb, tx << 16); /* 16.16 format */
  1394. avio_wb32(pb, ty << 16); /* 16.16 format */
  1395. avio_wb32(pb, 1 << 30); /* w in 2.30 format */
  1396. }
  1397. static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
  1398. {
  1399. int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
  1400. track->timescale, AV_ROUND_UP);
  1401. int version = duration < INT32_MAX ? 0 : 1;
  1402. int rotation = 0;
  1403. if (track->mode == MODE_ISM)
  1404. version = 1;
  1405. (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
  1406. ffio_wfourcc(pb, "tkhd");
  1407. avio_w8(pb, version);
  1408. avio_wb24(pb, 0xf); /* flags (track enabled) */
  1409. if (version == 1) {
  1410. avio_wb64(pb, track->time);
  1411. avio_wb64(pb, track->time);
  1412. } else {
  1413. avio_wb32(pb, track->time); /* creation time */
  1414. avio_wb32(pb, track->time); /* modification time */
  1415. }
  1416. avio_wb32(pb, track->track_id); /* track-id */
  1417. avio_wb32(pb, 0); /* reserved */
  1418. if (!track->entry)
  1419. (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
  1420. else
  1421. (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
  1422. avio_wb32(pb, 0); /* reserved */
  1423. avio_wb32(pb, 0); /* reserved */
  1424. avio_wb16(pb, 0); /* layer */
  1425. avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
  1426. /* Volume, only for audio */
  1427. if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1428. avio_wb16(pb, 0x0100);
  1429. else
  1430. avio_wb16(pb, 0);
  1431. avio_wb16(pb, 0); /* reserved */
  1432. /* Matrix structure */
  1433. if (st && st->metadata) {
  1434. AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
  1435. rotation = (rot && rot->value) ? atoi(rot->value) : 0;
  1436. }
  1437. if (rotation == 90) {
  1438. write_matrix(pb, 0, 1, -1, 0, track->enc->height, 0);
  1439. } else if (rotation == 180) {
  1440. write_matrix(pb, -1, 0, 0, -1, track->enc->width, track->enc->height);
  1441. } else if (rotation == 270) {
  1442. write_matrix(pb, 0, -1, 1, 0, 0, track->enc->width);
  1443. } else {
  1444. write_matrix(pb, 1, 0, 0, 1, 0, 0);
  1445. }
  1446. /* Track width and height, for visual only */
  1447. if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
  1448. track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
  1449. if(track->mode == MODE_MOV) {
  1450. avio_wb32(pb, track->enc->width << 16);
  1451. avio_wb32(pb, track->height << 16);
  1452. } else {
  1453. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  1454. if(!sample_aspect_ratio || track->height != track->enc->height)
  1455. sample_aspect_ratio = 1;
  1456. avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
  1457. avio_wb32(pb, track->height*0x10000);
  1458. }
  1459. }
  1460. else {
  1461. avio_wb32(pb, 0);
  1462. avio_wb32(pb, 0);
  1463. }
  1464. return 0x5c;
  1465. }
  1466. static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
  1467. {
  1468. int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
  1469. track->enc->sample_aspect_ratio.den);
  1470. int64_t pos = avio_tell(pb);
  1471. avio_wb32(pb, 0); /* size */
  1472. ffio_wfourcc(pb, "tapt");
  1473. avio_wb32(pb, 20);
  1474. ffio_wfourcc(pb, "clef");
  1475. avio_wb32(pb, 0);
  1476. avio_wb32(pb, width << 16);
  1477. avio_wb32(pb, track->enc->height << 16);
  1478. avio_wb32(pb, 20);
  1479. ffio_wfourcc(pb, "enof");
  1480. avio_wb32(pb, 0);
  1481. avio_wb32(pb, track->enc->width << 16);
  1482. avio_wb32(pb, track->enc->height << 16);
  1483. return update_size(pb, pos);
  1484. }
  1485. // This box seems important for the psp playback ... without it the movie seems to hang
  1486. static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
  1487. {
  1488. int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
  1489. track->timescale, AV_ROUND_UP);
  1490. int version = duration < INT32_MAX ? 0 : 1;
  1491. int entry_size, entry_count, size;
  1492. int64_t delay, start_ct = track->cluster[0].cts;
  1493. delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
  1494. track->timescale, AV_ROUND_DOWN);
  1495. version |= delay < INT32_MAX ? 0 : 1;
  1496. entry_size = (version == 1) ? 20 : 12;
  1497. entry_count = 1 + (delay > 0);
  1498. size = 24 + entry_count * entry_size;
  1499. /* write the atom data */
  1500. avio_wb32(pb, size);
  1501. ffio_wfourcc(pb, "edts");
  1502. avio_wb32(pb, size - 8);
  1503. ffio_wfourcc(pb, "elst");
  1504. avio_w8(pb, version);
  1505. avio_wb24(pb, 0); /* flags */
  1506. avio_wb32(pb, entry_count);
  1507. if (delay > 0) { /* add an empty edit to delay presentation */
  1508. if (version == 1) {
  1509. avio_wb64(pb, delay);
  1510. avio_wb64(pb, -1);
  1511. } else {
  1512. avio_wb32(pb, delay);
  1513. avio_wb32(pb, -1);
  1514. }
  1515. avio_wb32(pb, 0x00010000);
  1516. } else {
  1517. av_assert0(track->cluster[0].dts <= 0);
  1518. start_ct = -track->cluster[0].dts;
  1519. duration += delay;
  1520. }
  1521. /* duration */
  1522. if (version == 1) {
  1523. avio_wb64(pb, duration);
  1524. avio_wb64(pb, start_ct);
  1525. } else {
  1526. avio_wb32(pb, duration);
  1527. avio_wb32(pb, start_ct);
  1528. }
  1529. avio_wb32(pb, 0x00010000);
  1530. return size;
  1531. }
  1532. static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
  1533. {
  1534. avio_wb32(pb, 20); // size
  1535. ffio_wfourcc(pb, "tref");
  1536. avio_wb32(pb, 12); // size (subatom)
  1537. avio_wl32(pb, track->tref_tag);
  1538. avio_wb32(pb, track->tref_id);
  1539. return 20;
  1540. }
  1541. // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
  1542. static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
  1543. {
  1544. avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
  1545. ffio_wfourcc(pb, "uuid");
  1546. ffio_wfourcc(pb, "USMT");
  1547. avio_wb32(pb, 0x21d24fce);
  1548. avio_wb32(pb, 0xbb88695c);
  1549. avio_wb32(pb, 0xfac9c740);
  1550. avio_wb32(pb, 0x1c); // another size here!
  1551. ffio_wfourcc(pb, "MTDT");
  1552. avio_wb32(pb, 0x00010012);
  1553. avio_wb32(pb, 0x0a);
  1554. avio_wb32(pb, 0x55c40000);
  1555. avio_wb32(pb, 0x1);
  1556. avio_wb32(pb, 0x0);
  1557. return 0x34;
  1558. }
  1559. static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index)
  1560. {
  1561. char buf[1000] = "";
  1562. int len;
  1563. ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
  1564. av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
  1565. len = strlen(buf);
  1566. avio_wb32(pb, len + 24);
  1567. ffio_wfourcc(pb, "udta");
  1568. avio_wb32(pb, len + 16);
  1569. ffio_wfourcc(pb, "hnti");
  1570. avio_wb32(pb, len + 8);
  1571. ffio_wfourcc(pb, "sdp ");
  1572. avio_write(pb, buf, len);
  1573. return len + 24;
  1574. }
  1575. static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
  1576. MOVTrack *track, AVStream *st)
  1577. {
  1578. int64_t pos = avio_tell(pb);
  1579. avio_wb32(pb, 0); /* size */
  1580. ffio_wfourcc(pb, "trak");
  1581. mov_write_tkhd_tag(pb, track, st);
  1582. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) // EDTS with fragments is tricky as we dont know the duration when its written
  1583. mov_write_edts_tag(pb, track); // PSP Movies and several other cases require edts box
  1584. if (track->tref_tag)
  1585. mov_write_tref_tag(pb, track);
  1586. mov_write_mdia_tag(pb, track);
  1587. if (track->mode == MODE_PSP)
  1588. mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
  1589. if (track->tag == MKTAG('r','t','p',' '))
  1590. mov_write_udta_sdp(pb, track->rtp_ctx, track->track_id);
  1591. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
  1592. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  1593. if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio)
  1594. mov_write_tapt_tag(pb, track);
  1595. };
  1596. return update_size(pb, pos);
  1597. }
  1598. static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
  1599. {
  1600. int i, has_audio = 0, has_video = 0;
  1601. int64_t pos = avio_tell(pb);
  1602. int audio_profile = mov->iods_audio_profile;
  1603. int video_profile = mov->iods_video_profile;
  1604. for (i = 0; i < mov->nb_streams; i++) {
  1605. if(mov->tracks[i].entry > 0) {
  1606. has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
  1607. has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
  1608. }
  1609. }
  1610. if (audio_profile < 0)
  1611. audio_profile = 0xFF - has_audio;
  1612. if (video_profile < 0)
  1613. video_profile = 0xFF - has_video;
  1614. avio_wb32(pb, 0x0); /* size */
  1615. ffio_wfourcc(pb, "iods");
  1616. avio_wb32(pb, 0); /* version & flags */
  1617. put_descr(pb, 0x10, 7);
  1618. avio_wb16(pb, 0x004f);
  1619. avio_w8(pb, 0xff);
  1620. avio_w8(pb, 0xff);
  1621. avio_w8(pb, audio_profile);
  1622. avio_w8(pb, video_profile);
  1623. avio_w8(pb, 0xff);
  1624. return update_size(pb, pos);
  1625. }
  1626. static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
  1627. {
  1628. avio_wb32(pb, 0x20); /* size */
  1629. ffio_wfourcc(pb, "trex");
  1630. avio_wb32(pb, 0); /* version & flags */
  1631. avio_wb32(pb, track->track_id); /* track ID */
  1632. avio_wb32(pb, 1); /* default sample description index */
  1633. avio_wb32(pb, 0); /* default sample duration */
  1634. avio_wb32(pb, 0); /* default sample size */
  1635. avio_wb32(pb, 0); /* default sample flags */
  1636. return 0;
  1637. }
  1638. static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
  1639. {
  1640. int64_t pos = avio_tell(pb);
  1641. int i;
  1642. avio_wb32(pb, 0x0); /* size */
  1643. ffio_wfourcc(pb, "mvex");
  1644. for (i = 0; i < mov->nb_streams; i++)
  1645. mov_write_trex_tag(pb, &mov->tracks[i]);
  1646. return update_size(pb, pos);
  1647. }
  1648. static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
  1649. {
  1650. int max_track_id = 1, i;
  1651. int64_t max_track_len_temp, max_track_len = 0;
  1652. int version;
  1653. for (i=0; i<mov->nb_streams; i++) {
  1654. if(mov->tracks[i].entry > 0) {
  1655. max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
  1656. MOV_TIMESCALE,
  1657. mov->tracks[i].timescale,
  1658. AV_ROUND_UP);
  1659. if (max_track_len < max_track_len_temp)
  1660. max_track_len = max_track_len_temp;
  1661. if (max_track_id < mov->tracks[i].track_id)
  1662. max_track_id = mov->tracks[i].track_id;
  1663. }
  1664. }
  1665. version = max_track_len < UINT32_MAX ? 0 : 1;
  1666. (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
  1667. ffio_wfourcc(pb, "mvhd");
  1668. avio_w8(pb, version);
  1669. avio_wb24(pb, 0); /* flags */
  1670. if (version == 1) {
  1671. avio_wb64(pb, mov->time);
  1672. avio_wb64(pb, mov->time);
  1673. } else {
  1674. avio_wb32(pb, mov->time); /* creation time */
  1675. avio_wb32(pb, mov->time); /* modification time */
  1676. }
  1677. avio_wb32(pb, MOV_TIMESCALE);
  1678. (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
  1679. avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  1680. avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  1681. avio_wb16(pb, 0); /* reserved */
  1682. avio_wb32(pb, 0); /* reserved */
  1683. avio_wb32(pb, 0); /* reserved */
  1684. /* Matrix structure */
  1685. write_matrix(pb, 1, 0, 0, 1, 0, 0);
  1686. avio_wb32(pb, 0); /* reserved (preview time) */
  1687. avio_wb32(pb, 0); /* reserved (preview duration) */
  1688. avio_wb32(pb, 0); /* reserved (poster time) */
  1689. avio_wb32(pb, 0); /* reserved (selection time) */
  1690. avio_wb32(pb, 0); /* reserved (selection duration) */
  1691. avio_wb32(pb, 0); /* reserved (current time) */
  1692. avio_wb32(pb, max_track_id + 1); /* Next track id */
  1693. return 0x6c;
  1694. }
  1695. static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
  1696. AVFormatContext *s)
  1697. {
  1698. avio_wb32(pb, 33); /* size */
  1699. ffio_wfourcc(pb, "hdlr");
  1700. avio_wb32(pb, 0);
  1701. avio_wb32(pb, 0);
  1702. ffio_wfourcc(pb, "mdir");
  1703. ffio_wfourcc(pb, "appl");
  1704. avio_wb32(pb, 0);
  1705. avio_wb32(pb, 0);
  1706. avio_w8(pb, 0);
  1707. return 33;
  1708. }
  1709. /* helper function to write a data tag with the specified string as data */
  1710. static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
  1711. {
  1712. if(long_style){
  1713. int size = 16 + strlen(data);
  1714. avio_wb32(pb, size); /* size */
  1715. ffio_wfourcc(pb, "data");
  1716. avio_wb32(pb, 1);
  1717. avio_wb32(pb, 0);
  1718. avio_write(pb, data, strlen(data));
  1719. return size;
  1720. }else{
  1721. if (!lang)
  1722. lang = ff_mov_iso639_to_lang("und", 1);
  1723. avio_wb16(pb, strlen(data)); /* string length */
  1724. avio_wb16(pb, lang);
  1725. avio_write(pb, data, strlen(data));
  1726. return strlen(data) + 4;
  1727. }
  1728. }
  1729. static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
  1730. int size = 0;
  1731. if (value && value[0]) {
  1732. int64_t pos = avio_tell(pb);
  1733. avio_wb32(pb, 0); /* size */
  1734. ffio_wfourcc(pb, name);
  1735. mov_write_string_data_tag(pb, value, lang, long_style);
  1736. size = update_size(pb, pos);
  1737. }
  1738. return size;
  1739. }
  1740. static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
  1741. const char *name, const char *tag,
  1742. int long_style)
  1743. {
  1744. int l, lang = 0, len, len2;
  1745. AVDictionaryEntry *t, *t2 = NULL;
  1746. char tag2[16];
  1747. if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
  1748. return 0;
  1749. len = strlen(t->key);
  1750. snprintf(tag2, sizeof(tag2), "%s-", tag);
  1751. while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
  1752. len2 = strlen(t2->key);
  1753. if (len2 == len+4 && !strcmp(t->value, t2->value)
  1754. && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
  1755. lang = l;
  1756. break;
  1757. }
  1758. }
  1759. return mov_write_string_tag(pb, name, t->value, lang, long_style);
  1760. }
  1761. /* iTunes bpm number */
  1762. static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
  1763. {
  1764. AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
  1765. int size = 0, tmpo = t ? atoi(t->value) : 0;
  1766. if (tmpo) {
  1767. size = 26;
  1768. avio_wb32(pb, size);
  1769. ffio_wfourcc(pb, "tmpo");
  1770. avio_wb32(pb, size-8); /* size */
  1771. ffio_wfourcc(pb, "data");
  1772. avio_wb32(pb, 0x15); //type specifier
  1773. avio_wb32(pb, 0);
  1774. avio_wb16(pb, tmpo); // data
  1775. }
  1776. return size;
  1777. }
  1778. /* iTunes track number */
  1779. static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
  1780. AVFormatContext *s)
  1781. {
  1782. AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
  1783. int size = 0, track = t ? atoi(t->value) : 0;
  1784. if (track) {
  1785. avio_wb32(pb, 32); /* size */
  1786. ffio_wfourcc(pb, "trkn");
  1787. avio_wb32(pb, 24); /* size */
  1788. ffio_wfourcc(pb, "data");
  1789. avio_wb32(pb, 0); // 8 bytes empty
  1790. avio_wb32(pb, 0);
  1791. avio_wb16(pb, 0); // empty
  1792. avio_wb16(pb, track); // track number
  1793. avio_wb16(pb, 0); // total track number
  1794. avio_wb16(pb, 0); // empty
  1795. size = 32;
  1796. }
  1797. return size;
  1798. }
  1799. /* iTunes meta data list */
  1800. static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
  1801. AVFormatContext *s)
  1802. {
  1803. int64_t pos = avio_tell(pb);
  1804. avio_wb32(pb, 0); /* size */
  1805. ffio_wfourcc(pb, "ilst");
  1806. mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
  1807. mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
  1808. mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
  1809. mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
  1810. mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
  1811. mov_write_string_metadata(s, pb, "\251day", "date" , 1);
  1812. mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
  1813. mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
  1814. mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
  1815. mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
  1816. mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
  1817. mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
  1818. mov_write_string_metadata(s, pb, "desc", "description",1);
  1819. mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
  1820. mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
  1821. mov_write_string_metadata(s, pb, "tven", "episode_id",1);
  1822. mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
  1823. mov_write_trkn_tag(pb, mov, s);
  1824. mov_write_tmpo_tag(pb, s);
  1825. return update_size(pb, pos);
  1826. }
  1827. /* iTunes meta data tag */
  1828. static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
  1829. AVFormatContext *s)
  1830. {
  1831. int size = 0;
  1832. int64_t pos = avio_tell(pb);
  1833. avio_wb32(pb, 0); /* size */
  1834. ffio_wfourcc(pb, "meta");
  1835. avio_wb32(pb, 0);
  1836. mov_write_itunes_hdlr_tag(pb, mov, s);
  1837. mov_write_ilst_tag(pb, mov, s);
  1838. size = update_size(pb, pos);
  1839. return size;
  1840. }
  1841. static int utf8len(const uint8_t *b)
  1842. {
  1843. int len=0;
  1844. int val;
  1845. while(*b){
  1846. GET_UTF8(val, *b++, return -1;)
  1847. len++;
  1848. }
  1849. return len;
  1850. }
  1851. static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
  1852. {
  1853. int val;
  1854. while(*b){
  1855. GET_UTF8(val, *b++, return -1;)
  1856. avio_wb16(pb, val);
  1857. }
  1858. avio_wb16(pb, 0x00);
  1859. return 0;
  1860. }
  1861. static uint16_t language_code(const char *str)
  1862. {
  1863. return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
  1864. }
  1865. static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
  1866. const char *tag, const char *str)
  1867. {
  1868. int64_t pos = avio_tell(pb);
  1869. AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
  1870. if (!t || !utf8len(t->value))
  1871. return 0;
  1872. avio_wb32(pb, 0); /* size */
  1873. ffio_wfourcc(pb, tag); /* type */
  1874. avio_wb32(pb, 0); /* version + flags */
  1875. if (!strcmp(tag, "yrrc"))
  1876. avio_wb16(pb, atoi(t->value));
  1877. else {
  1878. avio_wb16(pb, language_code("eng")); /* language */
  1879. avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
  1880. if (!strcmp(tag, "albm") &&
  1881. (t = av_dict_get(s->metadata, "track", NULL, 0)))
  1882. avio_w8(pb, atoi(t->value));
  1883. }
  1884. return update_size(pb, pos);
  1885. }
  1886. static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
  1887. {
  1888. int64_t pos = avio_tell(pb);
  1889. int i, nb_chapters = FFMIN(s->nb_chapters, 255);
  1890. avio_wb32(pb, 0); // size
  1891. ffio_wfourcc(pb, "chpl");
  1892. avio_wb32(pb, 0x01000000); // version + flags
  1893. avio_wb32(pb, 0); // unknown
  1894. avio_w8(pb, nb_chapters);
  1895. for (i = 0; i < nb_chapters; i++) {
  1896. AVChapter *c = s->chapters[i];
  1897. AVDictionaryEntry *t;
  1898. avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
  1899. if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
  1900. int len = FFMIN(strlen(t->value), 255);
  1901. avio_w8(pb, len);
  1902. avio_write(pb, t->value, len);
  1903. } else
  1904. avio_w8(pb, 0);
  1905. }
  1906. return update_size(pb, pos);
  1907. }
  1908. static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
  1909. AVFormatContext *s)
  1910. {
  1911. AVIOContext *pb_buf;
  1912. int i, ret, size;
  1913. uint8_t *buf;
  1914. for (i = 0; i < s->nb_streams; i++)
  1915. if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
  1916. return 0;
  1917. }
  1918. ret = avio_open_dyn_buf(&pb_buf);
  1919. if(ret < 0)
  1920. return ret;
  1921. if (mov->mode & MODE_3GP) {
  1922. mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
  1923. mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
  1924. mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
  1925. mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
  1926. mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
  1927. mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
  1928. mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
  1929. mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
  1930. } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
  1931. mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
  1932. mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
  1933. mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
  1934. mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
  1935. mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
  1936. mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
  1937. // currently ignored by mov.c
  1938. mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
  1939. // add support for libquicktime, this atom is also actually read by mov.c
  1940. mov_write_string_metadata(s, pb_buf, "\251cmt", "comment" , 0);
  1941. mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
  1942. mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
  1943. } else {
  1944. /* iTunes meta data */
  1945. mov_write_meta_tag(pb_buf, mov, s);
  1946. }
  1947. if (s->nb_chapters)
  1948. mov_write_chpl_tag(pb_buf, s);
  1949. if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
  1950. avio_wb32(pb, size+8);
  1951. ffio_wfourcc(pb, "udta");
  1952. avio_write(pb, buf, size);
  1953. }
  1954. av_free(buf);
  1955. return 0;
  1956. }
  1957. static void mov_write_psp_udta_tag(AVIOContext *pb,
  1958. const char *str, const char *lang, int type)
  1959. {
  1960. int len = utf8len(str)+1;
  1961. if(len<=0)
  1962. return;
  1963. avio_wb16(pb, len*2+10); /* size */
  1964. avio_wb32(pb, type); /* type */
  1965. avio_wb16(pb, language_code(lang)); /* language */
  1966. avio_wb16(pb, 0x01); /* ? */
  1967. ascii_to_wc(pb, str);
  1968. }
  1969. static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
  1970. {
  1971. AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
  1972. int64_t pos, pos2;
  1973. if (title) {
  1974. pos = avio_tell(pb);
  1975. avio_wb32(pb, 0); /* size placeholder*/
  1976. ffio_wfourcc(pb, "uuid");
  1977. ffio_wfourcc(pb, "USMT");
  1978. avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
  1979. avio_wb32(pb, 0xbb88695c);
  1980. avio_wb32(pb, 0xfac9c740);
  1981. pos2 = avio_tell(pb);
  1982. avio_wb32(pb, 0); /* size placeholder*/
  1983. ffio_wfourcc(pb, "MTDT");
  1984. avio_wb16(pb, 4);
  1985. // ?
  1986. avio_wb16(pb, 0x0C); /* size */
  1987. avio_wb32(pb, 0x0B); /* type */
  1988. avio_wb16(pb, language_code("und")); /* language */
  1989. avio_wb16(pb, 0x0); /* ? */
  1990. avio_wb16(pb, 0x021C); /* data */
  1991. mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
  1992. mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
  1993. mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
  1994. update_size(pb, pos2);
  1995. return update_size(pb, pos);
  1996. }
  1997. return 0;
  1998. }
  1999. static void build_chunks(MOVTrack *trk)
  2000. {
  2001. int i;
  2002. MOVIentry *chunk= &trk->cluster[0];
  2003. uint64_t chunkSize = chunk->size;
  2004. chunk->chunkNum= 1;
  2005. if (trk->chunkCount)
  2006. return;
  2007. trk->chunkCount= 1;
  2008. for(i=1; i<trk->entry; i++){
  2009. if(chunk->pos + chunkSize == trk->cluster[i].pos &&
  2010. chunkSize + trk->cluster[i].size < (1<<20)){
  2011. chunkSize += trk->cluster[i].size;
  2012. chunk->samples_in_chunk += trk->cluster[i].entries;
  2013. }else{
  2014. trk->cluster[i].chunkNum = chunk->chunkNum+1;
  2015. chunk=&trk->cluster[i];
  2016. chunkSize = chunk->size;
  2017. trk->chunkCount++;
  2018. }
  2019. }
  2020. }
  2021. static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
  2022. AVFormatContext *s)
  2023. {
  2024. int i;
  2025. int64_t pos = avio_tell(pb);
  2026. avio_wb32(pb, 0); /* size placeholder*/
  2027. ffio_wfourcc(pb, "moov");
  2028. for (i=0; i<mov->nb_streams; i++) {
  2029. if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
  2030. continue;
  2031. mov->tracks[i].time = mov->time;
  2032. mov->tracks[i].track_id = i+1;
  2033. if (mov->tracks[i].entry)
  2034. build_chunks(&mov->tracks[i]);
  2035. }
  2036. if (mov->chapter_track)
  2037. for (i=0; i<s->nb_streams; i++) {
  2038. mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
  2039. mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
  2040. }
  2041. for (i = 0; i < mov->nb_streams; i++) {
  2042. if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
  2043. mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
  2044. mov->tracks[i].tref_id =
  2045. mov->tracks[mov->tracks[i].src_track].track_id;
  2046. }
  2047. }
  2048. for (i = 0; i < mov->nb_streams; i++) {
  2049. if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
  2050. int src_trk = mov->tracks[i].src_track;
  2051. mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
  2052. mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
  2053. mov->tracks[i].track_duration = mov->tracks[src_trk].track_duration;
  2054. }
  2055. }
  2056. mov_write_mvhd_tag(pb, mov);
  2057. if (mov->mode != MODE_MOV && !mov->iods_skip)
  2058. mov_write_iods_tag(pb, mov);
  2059. for (i=0; i<mov->nb_streams; i++) {
  2060. if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
  2061. mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
  2062. }
  2063. }
  2064. if (mov->flags & FF_MOV_FLAG_FRAGMENT)
  2065. mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
  2066. if (mov->mode == MODE_PSP)
  2067. mov_write_uuidusmt_tag(pb, s);
  2068. else
  2069. mov_write_udta_tag(pb, mov, s);
  2070. return update_size(pb, pos);
  2071. }
  2072. static void param_write_int(AVIOContext *pb, const char *name, int value)
  2073. {
  2074. avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
  2075. }
  2076. static void param_write_string(AVIOContext *pb, const char *name, const char *value)
  2077. {
  2078. avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
  2079. }
  2080. static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
  2081. {
  2082. char buf[150];
  2083. len = FFMIN(sizeof(buf)/2 - 1, len);
  2084. ff_data_to_hex(buf, value, len, 0);
  2085. buf[2*len] = '\0';
  2086. avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
  2087. }
  2088. static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
  2089. {
  2090. int64_t pos = avio_tell(pb);
  2091. int i;
  2092. const uint8_t uuid[] = {
  2093. 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
  2094. 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
  2095. };
  2096. avio_wb32(pb, 0);
  2097. ffio_wfourcc(pb, "uuid");
  2098. avio_write(pb, uuid, sizeof(uuid));
  2099. avio_wb32(pb, 0);
  2100. avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  2101. avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
  2102. avio_printf(pb, "<head>\n");
  2103. avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
  2104. LIBAVFORMAT_IDENT);
  2105. avio_printf(pb, "</head>\n");
  2106. avio_printf(pb, "<body>\n");
  2107. avio_printf(pb, "<switch>\n");
  2108. for (i = 0; i < mov->nb_streams; i++) {
  2109. MOVTrack *track = &mov->tracks[i];
  2110. const char *type;
  2111. /* track->track_id is initialized in write_moov, and thus isn't known
  2112. * here yet */
  2113. int track_id = i + 1;
  2114. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  2115. type = "video";
  2116. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  2117. type = "audio";
  2118. } else {
  2119. continue;
  2120. }
  2121. avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
  2122. track->enc->bit_rate);
  2123. param_write_int(pb, "systemBitrate", track->enc->bit_rate);
  2124. param_write_int(pb, "trackID", track_id);
  2125. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  2126. if (track->enc->codec_id == AV_CODEC_ID_H264) {
  2127. uint8_t *ptr;
  2128. int size = track->enc->extradata_size;
  2129. if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
  2130. &size)) {
  2131. param_write_hex(pb, "CodecPrivateData",
  2132. ptr ? ptr : track->enc->extradata,
  2133. size);
  2134. av_free(ptr);
  2135. }
  2136. param_write_string(pb, "FourCC", "H264");
  2137. } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
  2138. param_write_string(pb, "FourCC", "WVC1");
  2139. param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
  2140. track->enc->extradata_size);
  2141. }
  2142. param_write_int(pb, "MaxWidth", track->enc->width);
  2143. param_write_int(pb, "MaxHeight", track->enc->height);
  2144. param_write_int(pb, "DisplayWidth", track->enc->width);
  2145. param_write_int(pb, "DisplayHeight", track->enc->height);
  2146. } else {
  2147. if (track->enc->codec_id == AV_CODEC_ID_AAC) {
  2148. param_write_string(pb, "FourCC", "AACL");
  2149. } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
  2150. param_write_string(pb, "FourCC", "WMAP");
  2151. }
  2152. param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
  2153. track->enc->extradata_size);
  2154. param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
  2155. track->enc->codec_id));
  2156. param_write_int(pb, "Channels", track->enc->channels);
  2157. param_write_int(pb, "SamplingRate", track->enc->sample_rate);
  2158. param_write_int(pb, "BitsPerSample", 16);
  2159. param_write_int(pb, "PacketSize", track->enc->block_align ?
  2160. track->enc->block_align : 4);
  2161. }
  2162. avio_printf(pb, "</%s>\n", type);
  2163. }
  2164. avio_printf(pb, "</switch>\n");
  2165. avio_printf(pb, "</body>\n");
  2166. avio_printf(pb, "</smil>\n");
  2167. return update_size(pb, pos);
  2168. }
  2169. static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
  2170. {
  2171. avio_wb32(pb, 16);
  2172. ffio_wfourcc(pb, "mfhd");
  2173. avio_wb32(pb, 0);
  2174. avio_wb32(pb, mov->fragments);
  2175. return 0;
  2176. }
  2177. static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
  2178. int64_t moof_offset)
  2179. {
  2180. int64_t pos = avio_tell(pb);
  2181. uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
  2182. MOV_TFHD_BASE_DATA_OFFSET;
  2183. if (!track->entry) {
  2184. flags |= MOV_TFHD_DURATION_IS_EMPTY;
  2185. } else {
  2186. flags |= MOV_TFHD_DEFAULT_FLAGS;
  2187. }
  2188. /* Don't set a default sample size, the silverlight player refuses
  2189. * to play files with that set. Don't set a default sample duration,
  2190. * WMP freaks out if it is set. */
  2191. if (track->mode == MODE_ISM)
  2192. flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION);
  2193. avio_wb32(pb, 0); /* size placeholder */
  2194. ffio_wfourcc(pb, "tfhd");
  2195. avio_w8(pb, 0); /* version */
  2196. avio_wb24(pb, flags);
  2197. avio_wb32(pb, track->track_id); /* track-id */
  2198. if (flags & MOV_TFHD_BASE_DATA_OFFSET)
  2199. avio_wb64(pb, moof_offset);
  2200. if (flags & MOV_TFHD_DEFAULT_DURATION) {
  2201. track->default_duration = get_cluster_duration(track, 0);
  2202. avio_wb32(pb, track->default_duration);
  2203. }
  2204. if (flags & MOV_TFHD_DEFAULT_SIZE) {
  2205. track->default_size = track->entry ? track->cluster[0].size : 1;
  2206. avio_wb32(pb, track->default_size);
  2207. } else
  2208. track->default_size = -1;
  2209. if (flags & MOV_TFHD_DEFAULT_FLAGS) {
  2210. track->default_sample_flags =
  2211. track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
  2212. (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
  2213. MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
  2214. avio_wb32(pb, track->default_sample_flags);
  2215. }
  2216. return update_size(pb, pos);
  2217. }
  2218. static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
  2219. {
  2220. return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
  2221. (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
  2222. }
  2223. static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
  2224. {
  2225. int64_t pos = avio_tell(pb);
  2226. uint32_t flags = MOV_TRUN_DATA_OFFSET;
  2227. int i;
  2228. for (i = 0; i < track->entry; i++) {
  2229. if (get_cluster_duration(track, i) != track->default_duration)
  2230. flags |= MOV_TRUN_SAMPLE_DURATION;
  2231. if (track->cluster[i].size != track->default_size)
  2232. flags |= MOV_TRUN_SAMPLE_SIZE;
  2233. if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
  2234. flags |= MOV_TRUN_SAMPLE_FLAGS;
  2235. }
  2236. if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
  2237. flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
  2238. if (track->flags & MOV_TRACK_CTTS)
  2239. flags |= MOV_TRUN_SAMPLE_CTS;
  2240. avio_wb32(pb, 0); /* size placeholder */
  2241. ffio_wfourcc(pb, "trun");
  2242. avio_w8(pb, 0); /* version */
  2243. avio_wb24(pb, flags);
  2244. avio_wb32(pb, track->entry); /* sample count */
  2245. track->moof_size_offset = avio_tell(pb);
  2246. avio_wb32(pb, 0); /* data offset */
  2247. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
  2248. avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
  2249. for (i = 0; i < track->entry; i++) {
  2250. if (flags & MOV_TRUN_SAMPLE_DURATION)
  2251. avio_wb32(pb, get_cluster_duration(track, i));
  2252. if (flags & MOV_TRUN_SAMPLE_SIZE)
  2253. avio_wb32(pb, track->cluster[i].size);
  2254. if (flags & MOV_TRUN_SAMPLE_FLAGS)
  2255. avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
  2256. if (flags & MOV_TRUN_SAMPLE_CTS)
  2257. avio_wb32(pb, track->cluster[i].cts);
  2258. }
  2259. return update_size(pb, pos);
  2260. }
  2261. static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
  2262. {
  2263. int64_t pos = avio_tell(pb);
  2264. const uint8_t uuid[] = {
  2265. 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
  2266. 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
  2267. };
  2268. avio_wb32(pb, 0); /* size placeholder */
  2269. ffio_wfourcc(pb, "uuid");
  2270. avio_write(pb, uuid, sizeof(uuid));
  2271. avio_w8(pb, 1);
  2272. avio_wb24(pb, 0);
  2273. avio_wb64(pb, track->frag_start);
  2274. avio_wb64(pb, track->start_dts + track->track_duration -
  2275. track->cluster[0].dts);
  2276. return update_size(pb, pos);
  2277. }
  2278. static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
  2279. MOVTrack *track, int entry)
  2280. {
  2281. int n = track->nb_frag_info - 1 - entry, i;
  2282. int size = 8 + 16 + 4 + 1 + 16*n;
  2283. const uint8_t uuid[] = {
  2284. 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
  2285. 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
  2286. };
  2287. if (entry < 0)
  2288. return 0;
  2289. avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
  2290. avio_wb32(pb, size);
  2291. ffio_wfourcc(pb, "uuid");
  2292. avio_write(pb, uuid, sizeof(uuid));
  2293. avio_w8(pb, 1);
  2294. avio_wb24(pb, 0);
  2295. avio_w8(pb, n);
  2296. for (i = 0; i < n; i++) {
  2297. int index = entry + 1 + i;
  2298. avio_wb64(pb, track->frag_info[index].time);
  2299. avio_wb64(pb, track->frag_info[index].duration);
  2300. }
  2301. if (n < mov->ism_lookahead) {
  2302. int free_size = 16*(mov->ism_lookahead - n);
  2303. avio_wb32(pb, free_size);
  2304. ffio_wfourcc(pb, "free");
  2305. for (i = 0; i < free_size - 8; i++)
  2306. avio_w8(pb, 0);
  2307. }
  2308. return 0;
  2309. }
  2310. static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
  2311. MOVTrack *track)
  2312. {
  2313. int64_t pos = avio_tell(pb);
  2314. int i;
  2315. for (i = 0; i < mov->ism_lookahead; i++) {
  2316. /* Update the tfrf tag for the last ism_lookahead fragments,
  2317. * nb_frag_info - 1 is the next fragment to be written. */
  2318. mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
  2319. }
  2320. avio_seek(pb, pos, SEEK_SET);
  2321. return 0;
  2322. }
  2323. static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
  2324. MOVTrack *track, int64_t moof_offset)
  2325. {
  2326. int64_t pos = avio_tell(pb);
  2327. avio_wb32(pb, 0); /* size placeholder */
  2328. ffio_wfourcc(pb, "traf");
  2329. mov_write_tfhd_tag(pb, track, moof_offset);
  2330. mov_write_trun_tag(pb, track);
  2331. if (mov->mode == MODE_ISM) {
  2332. mov_write_tfxd_tag(pb, track);
  2333. if (mov->ism_lookahead) {
  2334. int i, size = 16 + 4 + 1 + 16*mov->ism_lookahead;
  2335. track->tfrf_offset = avio_tell(pb);
  2336. avio_wb32(pb, 8 + size);
  2337. ffio_wfourcc(pb, "free");
  2338. for (i = 0; i < size; i++)
  2339. avio_w8(pb, 0);
  2340. }
  2341. }
  2342. return update_size(pb, pos);
  2343. }
  2344. static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
  2345. {
  2346. int64_t pos = avio_tell(pb), end;
  2347. int i, moof_size;
  2348. avio_wb32(pb, 0); /* size placeholder */
  2349. ffio_wfourcc(pb, "moof");
  2350. mov_write_mfhd_tag(pb, mov);
  2351. for (i = 0; i < mov->nb_streams; i++) {
  2352. MOVTrack *track = &mov->tracks[i];
  2353. if (tracks >= 0 && i != tracks)
  2354. continue;
  2355. if (!track->entry)
  2356. continue;
  2357. mov_write_traf_tag(pb, mov, track, pos);
  2358. }
  2359. end = avio_tell(pb);
  2360. moof_size = end - pos;
  2361. for (i = 0; i < mov->nb_streams; i++) {
  2362. MOVTrack *track = &mov->tracks[i];
  2363. if (tracks >= 0 && i != tracks)
  2364. continue;
  2365. if (!track->entry)
  2366. continue;
  2367. avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
  2368. avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
  2369. }
  2370. avio_seek(pb, end, SEEK_SET);
  2371. return update_size(pb, pos);
  2372. }
  2373. static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
  2374. {
  2375. int64_t pos = avio_tell(pb);
  2376. int i;
  2377. avio_wb32(pb, 0); /* size placeholder */
  2378. ffio_wfourcc(pb, "tfra");
  2379. avio_w8(pb, 1); /* version */
  2380. avio_wb24(pb, 0);
  2381. avio_wb32(pb, track->track_id);
  2382. avio_wb32(pb, 0); /* length of traf/trun/sample num */
  2383. avio_wb32(pb, track->nb_frag_info);
  2384. for (i = 0; i < track->nb_frag_info; i++) {
  2385. avio_wb64(pb, track->frag_info[i].time);
  2386. avio_wb64(pb, track->frag_info[i].offset);
  2387. avio_w8(pb, 1); /* traf number */
  2388. avio_w8(pb, 1); /* trun number */
  2389. avio_w8(pb, 1); /* sample number */
  2390. }
  2391. return update_size(pb, pos);
  2392. }
  2393. static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
  2394. {
  2395. int64_t pos = avio_tell(pb);
  2396. int i;
  2397. avio_wb32(pb, 0); /* size placeholder */
  2398. ffio_wfourcc(pb, "mfra");
  2399. /* An empty mfra atom is enough to indicate to the publishing point that
  2400. * the stream has ended. */
  2401. if (mov->flags & FF_MOV_FLAG_ISML)
  2402. return update_size(pb, pos);
  2403. for (i = 0; i < mov->nb_streams; i++) {
  2404. MOVTrack *track = &mov->tracks[i];
  2405. if (track->nb_frag_info)
  2406. mov_write_tfra_tag(pb, track);
  2407. }
  2408. avio_wb32(pb, 16);
  2409. ffio_wfourcc(pb, "mfro");
  2410. avio_wb32(pb, 0); /* version + flags */
  2411. avio_wb32(pb, avio_tell(pb) + 4 - pos);
  2412. return update_size(pb, pos);
  2413. }
  2414. static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
  2415. {
  2416. avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
  2417. ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
  2418. mov->mdat_pos = avio_tell(pb);
  2419. avio_wb32(pb, 0); /* size placeholder*/
  2420. ffio_wfourcc(pb, "mdat");
  2421. return 0;
  2422. }
  2423. /* TODO: This needs to be more general */
  2424. static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
  2425. {
  2426. MOVMuxContext *mov = s->priv_data;
  2427. int64_t pos = avio_tell(pb);
  2428. int has_h264 = 0, has_video = 0;
  2429. int minor = 0x200;
  2430. int i;
  2431. for (i = 0; i < s->nb_streams; i++) {
  2432. AVStream *st = s->streams[i];
  2433. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  2434. has_video = 1;
  2435. if (st->codec->codec_id == AV_CODEC_ID_H264)
  2436. has_h264 = 1;
  2437. }
  2438. avio_wb32(pb, 0); /* size */
  2439. ffio_wfourcc(pb, "ftyp");
  2440. if (mov->mode == MODE_3GP) {
  2441. ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
  2442. minor = has_h264 ? 0x100 : 0x200;
  2443. } else if (mov->mode & MODE_3G2) {
  2444. ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
  2445. minor = has_h264 ? 0x20000 : 0x10000;
  2446. }else if (mov->mode == MODE_PSP)
  2447. ffio_wfourcc(pb, "MSNV");
  2448. else if (mov->mode == MODE_MP4)
  2449. ffio_wfourcc(pb, "isom");
  2450. else if (mov->mode == MODE_IPOD)
  2451. ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
  2452. else if (mov->mode == MODE_ISM)
  2453. ffio_wfourcc(pb, "isml");
  2454. else if (mov->mode == MODE_F4V)
  2455. ffio_wfourcc(pb, "f4v ");
  2456. else
  2457. ffio_wfourcc(pb, "qt ");
  2458. avio_wb32(pb, minor);
  2459. if(mov->mode == MODE_MOV)
  2460. ffio_wfourcc(pb, "qt ");
  2461. else if (mov->mode == MODE_ISM) {
  2462. ffio_wfourcc(pb, "piff");
  2463. ffio_wfourcc(pb, "iso2");
  2464. } else {
  2465. ffio_wfourcc(pb, "isom");
  2466. ffio_wfourcc(pb, "iso2");
  2467. if(has_h264)
  2468. ffio_wfourcc(pb, "avc1");
  2469. }
  2470. if (mov->mode == MODE_3GP)
  2471. ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
  2472. else if (mov->mode & MODE_3G2)
  2473. ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
  2474. else if (mov->mode == MODE_PSP)
  2475. ffio_wfourcc(pb, "MSNV");
  2476. else if (mov->mode == MODE_MP4)
  2477. ffio_wfourcc(pb, "mp41");
  2478. return update_size(pb, pos);
  2479. }
  2480. static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
  2481. {
  2482. AVCodecContext *video_codec = s->streams[0]->codec;
  2483. AVCodecContext *audio_codec = s->streams[1]->codec;
  2484. int audio_rate = audio_codec->sample_rate;
  2485. int frame_rate = ((video_codec->time_base.den) * (0x10000))/ (video_codec->time_base.num);
  2486. int audio_kbitrate = audio_codec->bit_rate / 1000;
  2487. int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
  2488. avio_wb32(pb, 0x94); /* size */
  2489. ffio_wfourcc(pb, "uuid");
  2490. ffio_wfourcc(pb, "PROF");
  2491. avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
  2492. avio_wb32(pb, 0xbb88695c);
  2493. avio_wb32(pb, 0xfac9c740);
  2494. avio_wb32(pb, 0x0); /* ? */
  2495. avio_wb32(pb, 0x3); /* 3 sections ? */
  2496. avio_wb32(pb, 0x14); /* size */
  2497. ffio_wfourcc(pb, "FPRF");
  2498. avio_wb32(pb, 0x0); /* ? */
  2499. avio_wb32(pb, 0x0); /* ? */
  2500. avio_wb32(pb, 0x0); /* ? */
  2501. avio_wb32(pb, 0x2c); /* size */
  2502. ffio_wfourcc(pb, "APRF");/* audio */
  2503. avio_wb32(pb, 0x0);
  2504. avio_wb32(pb, 0x2); /* TrackID */
  2505. ffio_wfourcc(pb, "mp4a");
  2506. avio_wb32(pb, 0x20f);
  2507. avio_wb32(pb, 0x0);
  2508. avio_wb32(pb, audio_kbitrate);
  2509. avio_wb32(pb, audio_kbitrate);
  2510. avio_wb32(pb, audio_rate);
  2511. avio_wb32(pb, audio_codec->channels);
  2512. avio_wb32(pb, 0x34); /* size */
  2513. ffio_wfourcc(pb, "VPRF"); /* video */
  2514. avio_wb32(pb, 0x0);
  2515. avio_wb32(pb, 0x1); /* TrackID */
  2516. if (video_codec->codec_id == AV_CODEC_ID_H264) {
  2517. ffio_wfourcc(pb, "avc1");
  2518. avio_wb16(pb, 0x014D);
  2519. avio_wb16(pb, 0x0015);
  2520. } else {
  2521. ffio_wfourcc(pb, "mp4v");
  2522. avio_wb16(pb, 0x0000);
  2523. avio_wb16(pb, 0x0103);
  2524. }
  2525. avio_wb32(pb, 0x0);
  2526. avio_wb32(pb, video_kbitrate);
  2527. avio_wb32(pb, video_kbitrate);
  2528. avio_wb32(pb, frame_rate);
  2529. avio_wb32(pb, frame_rate);
  2530. avio_wb16(pb, video_codec->width);
  2531. avio_wb16(pb, video_codec->height);
  2532. avio_wb32(pb, 0x010001); /* ? */
  2533. }
  2534. static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
  2535. {
  2536. uint32_t c = -1;
  2537. int i, closed_gop = 0;
  2538. for (i = 0; i < pkt->size - 4; i++) {
  2539. c = (c<<8) + pkt->data[i];
  2540. if (c == 0x1b8) { // gop
  2541. closed_gop = pkt->data[i+4]>>6 & 0x01;
  2542. } else if (c == 0x100) { // pic
  2543. int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
  2544. if (!temp_ref || closed_gop) // I picture is not reordered
  2545. *flags = MOV_SYNC_SAMPLE;
  2546. else
  2547. *flags = MOV_PARTIAL_SYNC_SAMPLE;
  2548. break;
  2549. }
  2550. }
  2551. return 0;
  2552. }
  2553. static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
  2554. {
  2555. const uint8_t *start, *next, *end = pkt->data + pkt->size;
  2556. int seq = 0, entry = 0;
  2557. int key = pkt->flags & AV_PKT_FLAG_KEY;
  2558. start = find_next_marker(pkt->data, end);
  2559. for (next = start; next < end; start = next) {
  2560. next = find_next_marker(start + 4, end);
  2561. switch (AV_RB32(start)) {
  2562. case VC1_CODE_SEQHDR:
  2563. seq = 1;
  2564. break;
  2565. case VC1_CODE_ENTRYPOINT:
  2566. entry = 1;
  2567. break;
  2568. case VC1_CODE_SLICE:
  2569. trk->vc1_info.slices = 1;
  2570. break;
  2571. }
  2572. }
  2573. if (!trk->entry && !fragment) {
  2574. /* First packet in first fragment */
  2575. trk->vc1_info.first_packet_seq = seq;
  2576. trk->vc1_info.first_packet_entry = entry;
  2577. } else if ((seq && !trk->vc1_info.packet_seq) ||
  2578. (entry && !trk->vc1_info.packet_entry)) {
  2579. int i;
  2580. for (i = 0; i < trk->entry; i++)
  2581. trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
  2582. trk->has_keyframes = 0;
  2583. if (seq)
  2584. trk->vc1_info.packet_seq = 1;
  2585. if (entry)
  2586. trk->vc1_info.packet_entry = 1;
  2587. if (!fragment) {
  2588. /* First fragment */
  2589. if ((!seq || trk->vc1_info.first_packet_seq) &&
  2590. (!entry || trk->vc1_info.first_packet_entry)) {
  2591. /* First packet had the same headers as this one, readd the
  2592. * sync sample flag. */
  2593. trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
  2594. trk->has_keyframes = 1;
  2595. }
  2596. }
  2597. }
  2598. if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
  2599. key = seq && entry;
  2600. else if (trk->vc1_info.packet_seq)
  2601. key = seq;
  2602. else if (trk->vc1_info.packet_entry)
  2603. key = entry;
  2604. if (key) {
  2605. trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
  2606. trk->has_keyframes++;
  2607. }
  2608. }
  2609. static int get_moov_size(AVFormatContext *s)
  2610. {
  2611. int ret;
  2612. uint8_t *buf;
  2613. AVIOContext *moov_buf;
  2614. MOVMuxContext *mov = s->priv_data;
  2615. if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
  2616. return ret;
  2617. mov_write_moov_tag(moov_buf, mov, s);
  2618. ret = avio_close_dyn_buf(moov_buf, &buf);
  2619. av_free(buf);
  2620. return ret;
  2621. }
  2622. static int mov_flush_fragment(AVFormatContext *s)
  2623. {
  2624. MOVMuxContext *mov = s->priv_data;
  2625. int i, first_track = -1;
  2626. int64_t mdat_size = 0;
  2627. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
  2628. return 0;
  2629. if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
  2630. int64_t pos = avio_tell(s->pb);
  2631. uint8_t *buf;
  2632. int buf_size, moov_size;
  2633. for (i = 0; i < mov->nb_streams; i++)
  2634. if (!mov->tracks[i].entry)
  2635. break;
  2636. /* Don't write the initial moov unless all tracks have data */
  2637. if (i < mov->nb_streams)
  2638. return 0;
  2639. moov_size = get_moov_size(s);
  2640. for (i = 0; i < mov->nb_streams; i++)
  2641. mov->tracks[i].data_offset = pos + moov_size + 8;
  2642. mov_write_moov_tag(s->pb, mov, s);
  2643. buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
  2644. mov->mdat_buf = NULL;
  2645. avio_wb32(s->pb, buf_size + 8);
  2646. ffio_wfourcc(s->pb, "mdat");
  2647. avio_write(s->pb, buf, buf_size);
  2648. av_free(buf);
  2649. mov->fragments++;
  2650. mov->mdat_size = 0;
  2651. for (i = 0; i < mov->nb_streams; i++) {
  2652. if (mov->tracks[i].entry)
  2653. mov->tracks[i].frag_start += mov->tracks[i].start_dts +
  2654. mov->tracks[i].track_duration -
  2655. mov->tracks[i].cluster[0].dts;
  2656. mov->tracks[i].entry = 0;
  2657. }
  2658. avio_flush(s->pb);
  2659. return 0;
  2660. }
  2661. for (i = 0; i < mov->nb_streams; i++) {
  2662. MOVTrack *track = &mov->tracks[i];
  2663. if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
  2664. track->data_offset = 0;
  2665. else
  2666. track->data_offset = mdat_size;
  2667. if (!track->mdat_buf)
  2668. continue;
  2669. mdat_size += avio_tell(track->mdat_buf);
  2670. if (first_track < 0)
  2671. first_track = i;
  2672. }
  2673. if (!mdat_size)
  2674. return 0;
  2675. for (i = 0; i < mov->nb_streams; i++) {
  2676. MOVTrack *track = &mov->tracks[i];
  2677. int buf_size, write_moof = 1, moof_tracks = -1;
  2678. uint8_t *buf;
  2679. int64_t duration = 0;
  2680. if (track->entry)
  2681. duration = track->start_dts + track->track_duration -
  2682. track->cluster[0].dts;
  2683. if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
  2684. if (!track->mdat_buf)
  2685. continue;
  2686. mdat_size = avio_tell(track->mdat_buf);
  2687. moof_tracks = i;
  2688. } else {
  2689. write_moof = i == first_track;
  2690. }
  2691. if (write_moof) {
  2692. MOVFragmentInfo *info;
  2693. avio_flush(s->pb);
  2694. track->nb_frag_info++;
  2695. track->frag_info = av_realloc(track->frag_info,
  2696. sizeof(*track->frag_info) *
  2697. track->nb_frag_info);
  2698. info = &track->frag_info[track->nb_frag_info - 1];
  2699. info->offset = avio_tell(s->pb);
  2700. info->time = mov->tracks[i].frag_start;
  2701. info->duration = duration;
  2702. mov_write_tfrf_tags(s->pb, mov, track);
  2703. mov_write_moof_tag(s->pb, mov, moof_tracks);
  2704. info->tfrf_offset = track->tfrf_offset;
  2705. mov->fragments++;
  2706. avio_wb32(s->pb, mdat_size + 8);
  2707. ffio_wfourcc(s->pb, "mdat");
  2708. }
  2709. if (track->entry)
  2710. track->frag_start += duration;
  2711. track->entry = 0;
  2712. if (!track->mdat_buf)
  2713. continue;
  2714. buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
  2715. track->mdat_buf = NULL;
  2716. avio_write(s->pb, buf, buf_size);
  2717. av_free(buf);
  2718. }
  2719. mov->mdat_size = 0;
  2720. avio_flush(s->pb);
  2721. return 0;
  2722. }
  2723. int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  2724. {
  2725. MOVMuxContext *mov = s->priv_data;
  2726. AVIOContext *pb = s->pb;
  2727. MOVTrack *trk = &mov->tracks[pkt->stream_index];
  2728. AVCodecContext *enc = trk->enc;
  2729. unsigned int samples_in_chunk = 0;
  2730. int size= pkt->size;
  2731. uint8_t *reformatted_data = NULL;
  2732. if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
  2733. int ret;
  2734. if (mov->fragments > 0) {
  2735. if (!trk->mdat_buf) {
  2736. if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
  2737. return ret;
  2738. }
  2739. pb = trk->mdat_buf;
  2740. } else {
  2741. if (!mov->mdat_buf) {
  2742. if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
  2743. return ret;
  2744. }
  2745. pb = mov->mdat_buf;
  2746. }
  2747. }
  2748. if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
  2749. /* We must find out how many AMR blocks there are in one packet */
  2750. static uint16_t packed_size[16] =
  2751. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
  2752. int len = 0;
  2753. while (len < size && samples_in_chunk < 100) {
  2754. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  2755. samples_in_chunk++;
  2756. }
  2757. if (samples_in_chunk > 1) {
  2758. av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
  2759. return -1;
  2760. }
  2761. } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  2762. enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  2763. samples_in_chunk = enc->frame_size;
  2764. } else if (trk->sample_size)
  2765. samples_in_chunk = size / trk->sample_size;
  2766. else
  2767. samples_in_chunk = 1;
  2768. /* copy extradata if it exists */
  2769. if (trk->vos_len == 0 && enc->extradata_size > 0) {
  2770. trk->vos_len = enc->extradata_size;
  2771. trk->vos_data = av_malloc(trk->vos_len);
  2772. memcpy(trk->vos_data, enc->extradata, trk->vos_len);
  2773. }
  2774. if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
  2775. /* from x264 or from bytestream h264 */
  2776. /* nal reformating needed */
  2777. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
  2778. ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
  2779. &size);
  2780. avio_write(pb, reformatted_data, size);
  2781. } else {
  2782. size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
  2783. }
  2784. } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
  2785. (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
  2786. av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
  2787. return -1;
  2788. } else {
  2789. avio_write(pb, pkt->data, size);
  2790. }
  2791. if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
  2792. enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
  2793. /* copy frame to create needed atoms */
  2794. trk->vos_len = size;
  2795. trk->vos_data = av_malloc(size);
  2796. if (!trk->vos_data)
  2797. return AVERROR(ENOMEM);
  2798. memcpy(trk->vos_data, pkt->data, size);
  2799. }
  2800. if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
  2801. trk->cluster = av_realloc_f(trk->cluster, sizeof(*trk->cluster), (trk->entry + MOV_INDEX_CLUSTER_SIZE));
  2802. if (!trk->cluster)
  2803. return -1;
  2804. }
  2805. trk->cluster[trk->entry].pos = avio_tell(pb) - size;
  2806. trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
  2807. trk->cluster[trk->entry].chunkNum = 0;
  2808. trk->cluster[trk->entry].size = size;
  2809. trk->cluster[trk->entry].entries = samples_in_chunk;
  2810. trk->cluster[trk->entry].dts = pkt->dts;
  2811. if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
  2812. /* First packet of a new fragment. We already wrote the duration
  2813. * of the last packet of the previous fragment based on track_duration,
  2814. * which might not exactly match our dts. Therefore adjust the dts
  2815. * of this packet to be what the previous packets duration implies. */
  2816. trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
  2817. }
  2818. if (trk->start_dts == AV_NOPTS_VALUE)
  2819. trk->start_dts = pkt->dts;
  2820. trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
  2821. trk->last_sample_is_subtitle_end = 0;
  2822. if (pkt->pts == AV_NOPTS_VALUE) {
  2823. av_log(s, AV_LOG_WARNING, "pts has no value\n");
  2824. pkt->pts = pkt->dts;
  2825. }
  2826. if (pkt->dts != pkt->pts)
  2827. trk->flags |= MOV_TRACK_CTTS;
  2828. trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
  2829. trk->cluster[trk->entry].flags = 0;
  2830. if (enc->codec_id == AV_CODEC_ID_VC1) {
  2831. mov_parse_vc1_frame(pkt, trk, mov->fragments);
  2832. } else if (pkt->flags & AV_PKT_FLAG_KEY) {
  2833. if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
  2834. trk->entry > 0) { // force sync sample for the first key frame
  2835. mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
  2836. if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
  2837. trk->flags |= MOV_TRACK_STPS;
  2838. } else {
  2839. trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
  2840. }
  2841. if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
  2842. trk->has_keyframes++;
  2843. }
  2844. trk->entry++;
  2845. trk->sample_count += samples_in_chunk;
  2846. mov->mdat_size += size;
  2847. avio_flush(pb);
  2848. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
  2849. ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
  2850. reformatted_data, size);
  2851. av_free(reformatted_data);
  2852. return 0;
  2853. }
  2854. static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
  2855. {
  2856. MOVMuxContext *mov = s->priv_data;
  2857. MOVTrack *trk = &mov->tracks[pkt->stream_index];
  2858. AVCodecContext *enc = trk->enc;
  2859. int64_t frag_duration = 0;
  2860. int size = pkt->size;
  2861. if (!pkt->size) return 0; /* Discard 0 sized packets */
  2862. if (trk->entry && pkt->stream_index < s->nb_streams)
  2863. frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
  2864. s->streams[pkt->stream_index]->time_base,
  2865. AV_TIME_BASE_Q);
  2866. if ((mov->max_fragment_duration &&
  2867. frag_duration >= mov->max_fragment_duration) ||
  2868. (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
  2869. (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
  2870. enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  2871. trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
  2872. if (frag_duration >= mov->min_fragment_duration)
  2873. mov_flush_fragment(s);
  2874. }
  2875. return ff_mov_write_packet(s, pkt);
  2876. }
  2877. static int mov_write_subtitle_end_packet(AVFormatContext *s,
  2878. int stream_index,
  2879. int64_t dts) {
  2880. AVPacket end;
  2881. uint8_t data[2] = {0};
  2882. int ret;
  2883. av_init_packet(&end);
  2884. end.size = sizeof(data);
  2885. end.data = data;
  2886. end.pts = dts;
  2887. end.dts = dts;
  2888. end.duration = 0;
  2889. end.stream_index = stream_index;
  2890. ret = mov_write_single_packet(s, &end);
  2891. av_free_packet(&end);
  2892. return ret;
  2893. }
  2894. static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  2895. {
  2896. if (!pkt) {
  2897. mov_flush_fragment(s);
  2898. return 1;
  2899. } else {
  2900. int i;
  2901. MOVMuxContext *mov = s->priv_data;
  2902. if (!pkt->size) return 0; /* Discard 0 sized packets */
  2903. /*
  2904. * Subtitles require special handling.
  2905. *
  2906. * 1) For full complaince, every track must have a sample at
  2907. * dts == 0, which is rarely true for subtitles. So, as soon
  2908. * as we see any packet with dts > 0, write an empty subtitle
  2909. * at dts == 0 for any subtitle track with no samples in it.
  2910. *
  2911. * 2) For each subtitle track, check if the current packet's
  2912. * dts is past the duration of the last subtitle sample. If
  2913. * so, we now need to write an end sample for that subtitle.
  2914. *
  2915. * This must be done conditionally to allow for subtitles that
  2916. * immediately replace each other, in which case an end sample
  2917. * is not needed, and is, in fact, actively harmful.
  2918. *
  2919. * 3) See mov_write_trailer for how the final end sample is
  2920. * handled.
  2921. */
  2922. for (i = 0; i < mov->nb_streams; i++) {
  2923. MOVTrack *trk = &mov->tracks[i];
  2924. int ret;
  2925. if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
  2926. trk->track_duration < pkt->dts &&
  2927. (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
  2928. ret = mov_write_subtitle_end_packet(s, i, trk->track_duration);
  2929. if (ret < 0) return ret;
  2930. trk->last_sample_is_subtitle_end = 1;
  2931. }
  2932. }
  2933. return mov_write_single_packet(s, pkt);
  2934. }
  2935. }
  2936. // QuickTime chapters involve an additional text track with the chapter names
  2937. // as samples, and a tref pointing from the other tracks to the chapter one.
  2938. static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
  2939. {
  2940. AVIOContext *pb;
  2941. MOVMuxContext *mov = s->priv_data;
  2942. MOVTrack *track = &mov->tracks[tracknum];
  2943. AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
  2944. int i, len;
  2945. track->mode = mov->mode;
  2946. track->tag = MKTAG('t','e','x','t');
  2947. track->timescale = MOV_TIMESCALE;
  2948. track->enc = avcodec_alloc_context3(NULL);
  2949. track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
  2950. if (avio_open_dyn_buf(&pb) >= 0) {
  2951. int size;
  2952. uint8_t *buf;
  2953. /* Stub header (usually for Quicktime chapter track) */
  2954. // TextSampleEntry
  2955. avio_wb32(pb, 0x01); // displayFlags
  2956. avio_w8(pb, 0x00); // horizontal justification
  2957. avio_w8(pb, 0x00); // vertical justification
  2958. avio_w8(pb, 0x00); // bgColourRed
  2959. avio_w8(pb, 0x00); // bgColourGreen
  2960. avio_w8(pb, 0x00); // bgColourBlue
  2961. avio_w8(pb, 0x00); // bgColourAlpha
  2962. // BoxRecord
  2963. avio_wb16(pb, 0x00); // defTextBoxTop
  2964. avio_wb16(pb, 0x00); // defTextBoxLeft
  2965. avio_wb16(pb, 0x00); // defTextBoxBottom
  2966. avio_wb16(pb, 0x00); // defTextBoxRight
  2967. // StyleRecord
  2968. avio_wb16(pb, 0x00); // startChar
  2969. avio_wb16(pb, 0x00); // endChar
  2970. avio_wb16(pb, 0x01); // fontID
  2971. avio_w8(pb, 0x00); // fontStyleFlags
  2972. avio_w8(pb, 0x00); // fontSize
  2973. avio_w8(pb, 0x00); // fgColourRed
  2974. avio_w8(pb, 0x00); // fgColourGreen
  2975. avio_w8(pb, 0x00); // fgColourBlue
  2976. avio_w8(pb, 0x00); // fgColourAlpha
  2977. // FontTableBox
  2978. avio_wb32(pb, 0x0D); // box size
  2979. ffio_wfourcc(pb, "ftab"); // box atom name
  2980. avio_wb16(pb, 0x01); // entry count
  2981. // FontRecord
  2982. avio_wb16(pb, 0x01); // font ID
  2983. avio_w8(pb, 0x00); // font name length
  2984. if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
  2985. track->enc->extradata = buf;
  2986. track->enc->extradata_size = size;
  2987. } else {
  2988. av_free(&buf);
  2989. }
  2990. }
  2991. for (i = 0; i < s->nb_chapters; i++) {
  2992. AVChapter *c = s->chapters[i];
  2993. AVDictionaryEntry *t;
  2994. int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
  2995. pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
  2996. pkt.duration = end - pkt.dts;
  2997. if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
  2998. len = strlen(t->value);
  2999. pkt.size = len+2;
  3000. pkt.data = av_malloc(pkt.size);
  3001. AV_WB16(pkt.data, len);
  3002. memcpy(pkt.data+2, t->value, len);
  3003. ff_mov_write_packet(s, &pkt);
  3004. av_freep(&pkt.data);
  3005. }
  3006. }
  3007. }
  3008. static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
  3009. {
  3010. int ret;
  3011. MOVMuxContext *mov = s->priv_data;
  3012. MOVTrack *track = &mov->tracks[index];
  3013. AVStream *src_st = s->streams[src_index];
  3014. AVTimecode tc;
  3015. AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
  3016. AVRational rate = {src_st->codec->time_base.den, src_st->codec->time_base.num};
  3017. /* if the codec time base makes no sense, try to fallback on stream frame rate */
  3018. if (av_timecode_check_frame_rate(rate) < 0) {
  3019. av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
  3020. rate.num, rate.den, src_st->avg_frame_rate.num, src_st->avg_frame_rate.den);
  3021. rate = src_st->avg_frame_rate;
  3022. }
  3023. /* compute the frame number */
  3024. ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
  3025. if (ret < 0)
  3026. return ret;
  3027. /* tmcd track based on video stream */
  3028. track->mode = mov->mode;
  3029. track->tag = MKTAG('t','m','c','d');
  3030. track->src_track = src_index;
  3031. track->timescale = src_st->codec->time_base.den;
  3032. if (tc.flags & AV_TIMECODE_FLAG_DROPFRAME)
  3033. track->timecode_flags |= MOV_TIMECODE_FLAG_DROPFRAME;
  3034. /* encode context: tmcd data stream */
  3035. track->enc = avcodec_alloc_context3(NULL);
  3036. track->enc->codec_type = AVMEDIA_TYPE_DATA;
  3037. track->enc->codec_tag = track->tag;
  3038. track->enc->time_base = src_st->codec->time_base;
  3039. /* the tmcd track just contains one packet with the frame number */
  3040. pkt.data = av_malloc(pkt.size);
  3041. AV_WB32(pkt.data, tc.start);
  3042. ret = ff_mov_write_packet(s, &pkt);
  3043. av_free(pkt.data);
  3044. return ret;
  3045. }
  3046. static int mov_write_header(AVFormatContext *s)
  3047. {
  3048. AVIOContext *pb = s->pb;
  3049. MOVMuxContext *mov = s->priv_data;
  3050. AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
  3051. int i, hint_track = 0, tmcd_track = 0;
  3052. /* Set the FRAGMENT flag if any of the fragmentation methods are
  3053. * enabled. */
  3054. if (mov->max_fragment_duration || mov->max_fragment_size ||
  3055. mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
  3056. FF_MOV_FLAG_FRAG_KEYFRAME |
  3057. FF_MOV_FLAG_FRAG_CUSTOM))
  3058. mov->flags |= FF_MOV_FLAG_FRAGMENT;
  3059. /* faststart: moov at the beginning of the file, if supported */
  3060. if (mov->flags & FF_MOV_FLAG_FASTSTART) {
  3061. if (mov->flags & FF_MOV_FLAG_FRAGMENT)
  3062. mov->flags &= ~FF_MOV_FLAG_FASTSTART;
  3063. else
  3064. mov->reserved_moov_size = -1;
  3065. }
  3066. /* Non-seekable output is ok if using fragmentation. If ism_lookahead
  3067. * is enabled, we don't support non-seekable output at all. */
  3068. if (!s->pb->seekable &&
  3069. ((!(mov->flags & FF_MOV_FLAG_FRAGMENT) &&
  3070. !(s->oformat && !strcmp(s->oformat->name, "ismv")))
  3071. || mov->ism_lookahead)) {
  3072. av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
  3073. return -1;
  3074. }
  3075. /* Default mode == MP4 */
  3076. mov->mode = MODE_MP4;
  3077. if (s->oformat != NULL) {
  3078. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  3079. else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
  3080. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  3081. else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
  3082. else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
  3083. else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
  3084. else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
  3085. mov_write_ftyp_tag(pb,s);
  3086. if (mov->mode == MODE_PSP) {
  3087. if (s->nb_streams != 2) {
  3088. av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
  3089. return -1;
  3090. }
  3091. mov_write_uuidprof_tag(pb,s);
  3092. }
  3093. }
  3094. mov->nb_streams = s->nb_streams;
  3095. if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
  3096. mov->chapter_track = mov->nb_streams++;
  3097. if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
  3098. /* Add hint tracks for each audio and video stream */
  3099. hint_track = mov->nb_streams;
  3100. for (i = 0; i < s->nb_streams; i++) {
  3101. AVStream *st = s->streams[i];
  3102. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  3103. st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  3104. mov->nb_streams++;
  3105. }
  3106. }
  3107. }
  3108. if (mov->mode == MODE_MOV) {
  3109. tmcd_track = mov->nb_streams;
  3110. /* +1 tmcd track for each video stream with a timecode */
  3111. for (i = 0; i < s->nb_streams; i++) {
  3112. AVStream *st = s->streams[i];
  3113. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  3114. (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
  3115. mov->nb_meta_tmcd++;
  3116. }
  3117. /* check if there is already a tmcd track to remux */
  3118. if (mov->nb_meta_tmcd) {
  3119. for (i = 0; i < s->nb_streams; i++) {
  3120. AVStream *st = s->streams[i];
  3121. if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
  3122. av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
  3123. "so timecode metadata are now ignored\n");
  3124. mov->nb_meta_tmcd = 0;
  3125. }
  3126. }
  3127. }
  3128. mov->nb_streams += mov->nb_meta_tmcd;
  3129. }
  3130. mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
  3131. if (!mov->tracks)
  3132. return AVERROR(ENOMEM);
  3133. for(i=0; i<s->nb_streams; i++){
  3134. AVStream *st= s->streams[i];
  3135. MOVTrack *track= &mov->tracks[i];
  3136. AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
  3137. track->enc = st->codec;
  3138. track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
  3139. if (track->language < 0)
  3140. track->language = 0;
  3141. track->mode = mov->mode;
  3142. track->tag = mov_find_codec_tag(s, track);
  3143. if (!track->tag) {
  3144. av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
  3145. "codec not currently supported in container\n", i);
  3146. goto error;
  3147. }
  3148. /* If hinting of this track is enabled by a later hint track,
  3149. * this is updated. */
  3150. track->hint_track = -1;
  3151. track->start_dts = AV_NOPTS_VALUE;
  3152. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
  3153. if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
  3154. track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
  3155. track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
  3156. if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
  3157. av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
  3158. goto error;
  3159. }
  3160. track->height = track->tag>>24 == 'n' ? 486 : 576;
  3161. }
  3162. track->timescale = st->codec->time_base.den;
  3163. while(track->timescale < 10000)
  3164. track->timescale *= 2;
  3165. if (track->mode == MODE_MOV && track->timescale > 100000)
  3166. av_log(s, AV_LOG_WARNING,
  3167. "WARNING codec timebase is very high. If duration is too long,\n"
  3168. "file may not be playable by quicktime. Specify a shorter timebase\n"
  3169. "or choose different container.\n");
  3170. }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
  3171. track->timescale = st->codec->sample_rate;
  3172. if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
  3173. av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
  3174. track->audio_vbr = 1;
  3175. }else if(st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
  3176. st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
  3177. st->codec->codec_id == AV_CODEC_ID_ILBC){
  3178. if (!st->codec->block_align) {
  3179. av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
  3180. goto error;
  3181. }
  3182. track->sample_size = st->codec->block_align;
  3183. }else if(st->codec->frame_size > 1){ /* assume compressed audio */
  3184. track->audio_vbr = 1;
  3185. }else{
  3186. track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
  3187. }
  3188. if (track->mode != MODE_MOV &&
  3189. track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
  3190. av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
  3191. i, track->enc->sample_rate);
  3192. goto error;
  3193. }
  3194. }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
  3195. track->timescale = st->codec->time_base.den;
  3196. }else{
  3197. track->timescale = MOV_TIMESCALE;
  3198. }
  3199. if (!track->height)
  3200. track->height = st->codec->height;
  3201. /* The ism specific timescale isn't mandatory, but is assumed by
  3202. * some tools, such as mp4split. */
  3203. if (mov->mode == MODE_ISM)
  3204. track->timescale = 10000000;
  3205. avpriv_set_pts_info(st, 64, 1, track->timescale);
  3206. /* copy extradata if it exists */
  3207. if (st->codec->extradata_size) {
  3208. track->vos_len = st->codec->extradata_size;
  3209. track->vos_data = av_malloc(track->vos_len);
  3210. memcpy(track->vos_data, st->codec->extradata, track->vos_len);
  3211. }
  3212. }
  3213. if (mov->mode == MODE_ISM) {
  3214. /* If no fragmentation options have been set, set a default. */
  3215. if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
  3216. FF_MOV_FLAG_FRAG_CUSTOM)) &&
  3217. !mov->max_fragment_duration && !mov->max_fragment_size)
  3218. mov->max_fragment_duration = 5000000;
  3219. mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
  3220. FF_MOV_FLAG_FRAGMENT;
  3221. }
  3222. if(mov->reserved_moov_size){
  3223. mov->reserved_moov_pos= avio_tell(pb);
  3224. if (mov->reserved_moov_size > 0)
  3225. avio_skip(pb, mov->reserved_moov_size);
  3226. }
  3227. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
  3228. mov_write_mdat_tag(pb, mov);
  3229. if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
  3230. mov->time = ff_iso8601_to_unix_time(t->value);
  3231. if (mov->time)
  3232. mov->time += 0x7C25B080; // 1970 based -> 1904 based
  3233. if (mov->chapter_track)
  3234. mov_create_chapter_track(s, mov->chapter_track);
  3235. if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
  3236. /* Initialize the hint tracks for each audio and video stream */
  3237. for (i = 0; i < s->nb_streams; i++) {
  3238. AVStream *st = s->streams[i];
  3239. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  3240. st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  3241. ff_mov_init_hinting(s, hint_track, i);
  3242. hint_track++;
  3243. }
  3244. }
  3245. }
  3246. if (mov->nb_meta_tmcd) {
  3247. /* Initialize the tmcd tracks */
  3248. for (i = 0; i < s->nb_streams; i++) {
  3249. AVStream *st = s->streams[i];
  3250. t = global_tcr;
  3251. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  3252. if (!t)
  3253. t = av_dict_get(st->metadata, "timecode", NULL, 0);
  3254. if (!t)
  3255. continue;
  3256. if (mov_create_timecode_track(s, tmcd_track, i, t->value) < 0)
  3257. goto error;
  3258. tmcd_track++;
  3259. }
  3260. }
  3261. }
  3262. avio_flush(pb);
  3263. if (mov->flags & FF_MOV_FLAG_ISML)
  3264. mov_write_isml_manifest(pb, mov);
  3265. if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
  3266. mov_write_moov_tag(pb, mov, s);
  3267. mov->fragments++;
  3268. }
  3269. return 0;
  3270. error:
  3271. av_freep(&mov->tracks);
  3272. return -1;
  3273. }
  3274. /**
  3275. * This function gets the moov size if moved to the top of the file: the chunk
  3276. * offset table can switch between stco (32-bit entries) to co64 (64-bit
  3277. * entries) when the moov is moved to the top, so the size of the moov would
  3278. * change. It also updates the chunk offset tables.
  3279. */
  3280. static int compute_moov_size(AVFormatContext *s)
  3281. {
  3282. int i, moov_size, moov_size2;
  3283. MOVMuxContext *mov = s->priv_data;
  3284. moov_size = get_moov_size(s);
  3285. if (moov_size < 0)
  3286. return moov_size;
  3287. for (i = 0; i < mov->nb_streams; i++)
  3288. mov->tracks[i].data_offset += moov_size;
  3289. moov_size2 = get_moov_size(s);
  3290. if (moov_size2 < 0)
  3291. return moov_size2;
  3292. /* if the size changed, we just switched from stco to co64 and needs to
  3293. * update the offsets */
  3294. if (moov_size2 != moov_size)
  3295. for (i = 0; i < mov->nb_streams; i++)
  3296. mov->tracks[i].data_offset += moov_size2 - moov_size;
  3297. return moov_size2;
  3298. }
  3299. static int shift_data(AVFormatContext *s)
  3300. {
  3301. int ret = 0, moov_size;
  3302. MOVMuxContext *mov = s->priv_data;
  3303. int64_t pos, pos_end = avio_tell(s->pb);
  3304. uint8_t *buf, *read_buf[2];
  3305. int read_buf_id = 0;
  3306. int read_size[2];
  3307. AVIOContext *read_pb;
  3308. moov_size = compute_moov_size(s);
  3309. if (moov_size < 0)
  3310. return moov_size;
  3311. buf = av_malloc(moov_size * 2);
  3312. if (!buf)
  3313. return AVERROR(ENOMEM);
  3314. read_buf[0] = buf;
  3315. read_buf[1] = buf + moov_size;
  3316. /* Shift the data: the AVIO context of the output can only be used for
  3317. * writing, so we re-open the same output, but for reading. It also avoids
  3318. * a read/seek/write/seek back and forth. */
  3319. avio_flush(s->pb);
  3320. ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
  3321. if (ret < 0) {
  3322. av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
  3323. "the second pass (faststart)\n", s->filename);
  3324. goto end;
  3325. }
  3326. /* mark the end of the shift to up to the last data we wrote, and get ready
  3327. * for writing */
  3328. pos_end = avio_tell(s->pb);
  3329. avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
  3330. /* start reading at where the new moov will be placed */
  3331. avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
  3332. pos = avio_tell(read_pb);
  3333. #define READ_BLOCK do { \
  3334. read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
  3335. read_buf_id ^= 1; \
  3336. } while (0)
  3337. /* shift data by chunk of at most moov_size */
  3338. READ_BLOCK;
  3339. do {
  3340. int n;
  3341. READ_BLOCK;
  3342. n = read_size[read_buf_id];
  3343. if (n <= 0)
  3344. break;
  3345. avio_write(s->pb, read_buf[read_buf_id], n);
  3346. pos += n;
  3347. } while (pos < pos_end);
  3348. avio_close(read_pb);
  3349. end:
  3350. av_free(buf);
  3351. return ret;
  3352. }
  3353. static int mov_write_trailer(AVFormatContext *s)
  3354. {
  3355. MOVMuxContext *mov = s->priv_data;
  3356. AVIOContext *pb = s->pb;
  3357. int64_t moov_pos;
  3358. int res = 0;
  3359. int i;
  3360. /*
  3361. * Before actually writing the trailer, make sure that there are no
  3362. * dangling subtitles, that need a terminating sample.
  3363. */
  3364. for (i = 0; i < mov->nb_streams; i++) {
  3365. MOVTrack *trk = &mov->tracks[i];
  3366. if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
  3367. !trk->last_sample_is_subtitle_end) {
  3368. mov_write_subtitle_end_packet(s, i, trk->track_duration);
  3369. trk->last_sample_is_subtitle_end = 1;
  3370. }
  3371. }
  3372. moov_pos = avio_tell(pb);
  3373. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
  3374. /* Write size of mdat tag */
  3375. if (mov->mdat_size + 8 <= UINT32_MAX) {
  3376. avio_seek(pb, mov->mdat_pos, SEEK_SET);
  3377. avio_wb32(pb, mov->mdat_size + 8);
  3378. } else {
  3379. /* overwrite 'wide' placeholder atom */
  3380. avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
  3381. /* special value: real atom size will be 64 bit value after
  3382. * tag field */
  3383. avio_wb32(pb, 1);
  3384. ffio_wfourcc(pb, "mdat");
  3385. avio_wb64(pb, mov->mdat_size + 16);
  3386. }
  3387. avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
  3388. if (mov->reserved_moov_size == -1) {
  3389. av_log(s, AV_LOG_INFO, "Starting second pass: moving header on top of the file\n");
  3390. res = shift_data(s);
  3391. if (res == 0) {
  3392. avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
  3393. mov_write_moov_tag(pb, mov, s);
  3394. }
  3395. } else if (mov->reserved_moov_size > 0) {
  3396. int64_t size;
  3397. mov_write_moov_tag(pb, mov, s);
  3398. size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
  3399. if(size < 8){
  3400. av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
  3401. return -1;
  3402. }
  3403. avio_wb32(pb, size);
  3404. ffio_wfourcc(pb, "free");
  3405. for(i=0; i<size; i++)
  3406. avio_w8(pb, 0);
  3407. avio_seek(pb, moov_pos, SEEK_SET);
  3408. } else {
  3409. mov_write_moov_tag(pb, mov, s);
  3410. }
  3411. } else {
  3412. mov_flush_fragment(s);
  3413. mov_write_mfra_tag(pb, mov);
  3414. }
  3415. if (mov->chapter_track)
  3416. av_freep(&mov->tracks[mov->chapter_track].enc);
  3417. for (i=0; i<mov->nb_streams; i++) {
  3418. if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
  3419. ff_mov_close_hinting(&mov->tracks[i]);
  3420. else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
  3421. av_freep(&mov->tracks[i].enc);
  3422. if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
  3423. mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
  3424. int64_t off = avio_tell(pb);
  3425. uint8_t buf[7];
  3426. if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
  3427. avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
  3428. avio_write(pb, buf, 7);
  3429. avio_seek(pb, off, SEEK_SET);
  3430. }
  3431. }
  3432. av_freep(&mov->tracks[i].cluster);
  3433. av_freep(&mov->tracks[i].frag_info);
  3434. if (mov->tracks[i].vos_len)
  3435. av_free(mov->tracks[i].vos_data);
  3436. }
  3437. av_freep(&mov->tracks);
  3438. return res;
  3439. }
  3440. #if CONFIG_MOV_MUXER
  3441. MOV_CLASS(mov)
  3442. AVOutputFormat ff_mov_muxer = {
  3443. .name = "mov",
  3444. .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
  3445. .extensions = "mov",
  3446. .priv_data_size = sizeof(MOVMuxContext),
  3447. .audio_codec = AV_CODEC_ID_AAC,
  3448. .video_codec = CONFIG_LIBX264_ENCODER ?
  3449. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  3450. .write_header = mov_write_header,
  3451. .write_packet = mov_write_packet,
  3452. .write_trailer = mov_write_trailer,
  3453. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3454. .codec_tag = (const AVCodecTag* const []){
  3455. ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
  3456. },
  3457. .priv_class = &mov_muxer_class,
  3458. };
  3459. #endif
  3460. #if CONFIG_TGP_MUXER
  3461. MOV_CLASS(tgp)
  3462. AVOutputFormat ff_tgp_muxer = {
  3463. .name = "3gp",
  3464. .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
  3465. .extensions = "3gp",
  3466. .priv_data_size = sizeof(MOVMuxContext),
  3467. .audio_codec = AV_CODEC_ID_AMR_NB,
  3468. .video_codec = AV_CODEC_ID_H263,
  3469. .write_header = mov_write_header,
  3470. .write_packet = mov_write_packet,
  3471. .write_trailer = mov_write_trailer,
  3472. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3473. .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
  3474. .priv_class = &tgp_muxer_class,
  3475. };
  3476. #endif
  3477. #if CONFIG_MP4_MUXER
  3478. MOV_CLASS(mp4)
  3479. AVOutputFormat ff_mp4_muxer = {
  3480. .name = "mp4",
  3481. .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
  3482. .mime_type = "application/mp4",
  3483. .extensions = "mp4",
  3484. .priv_data_size = sizeof(MOVMuxContext),
  3485. .audio_codec = AV_CODEC_ID_AAC,
  3486. .video_codec = CONFIG_LIBX264_ENCODER ?
  3487. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  3488. .write_header = mov_write_header,
  3489. .write_packet = mov_write_packet,
  3490. .write_trailer = mov_write_trailer,
  3491. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3492. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  3493. .priv_class = &mp4_muxer_class,
  3494. };
  3495. #endif
  3496. #if CONFIG_PSP_MUXER
  3497. MOV_CLASS(psp)
  3498. AVOutputFormat ff_psp_muxer = {
  3499. .name = "psp",
  3500. .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
  3501. .extensions = "mp4,psp",
  3502. .priv_data_size = sizeof(MOVMuxContext),
  3503. .audio_codec = AV_CODEC_ID_AAC,
  3504. .video_codec = CONFIG_LIBX264_ENCODER ?
  3505. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  3506. .write_header = mov_write_header,
  3507. .write_packet = mov_write_packet,
  3508. .write_trailer = mov_write_trailer,
  3509. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3510. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  3511. .priv_class = &psp_muxer_class,
  3512. };
  3513. #endif
  3514. #if CONFIG_TG2_MUXER
  3515. MOV_CLASS(tg2)
  3516. AVOutputFormat ff_tg2_muxer = {
  3517. .name = "3g2",
  3518. .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
  3519. .extensions = "3g2",
  3520. .priv_data_size = sizeof(MOVMuxContext),
  3521. .audio_codec = AV_CODEC_ID_AMR_NB,
  3522. .video_codec = AV_CODEC_ID_H263,
  3523. .write_header = mov_write_header,
  3524. .write_packet = mov_write_packet,
  3525. .write_trailer = mov_write_trailer,
  3526. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3527. .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
  3528. .priv_class = &tg2_muxer_class,
  3529. };
  3530. #endif
  3531. #if CONFIG_IPOD_MUXER
  3532. MOV_CLASS(ipod)
  3533. AVOutputFormat ff_ipod_muxer = {
  3534. .name = "ipod",
  3535. .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
  3536. .mime_type = "application/mp4",
  3537. .extensions = "m4v,m4a",
  3538. .priv_data_size = sizeof(MOVMuxContext),
  3539. .audio_codec = AV_CODEC_ID_AAC,
  3540. .video_codec = AV_CODEC_ID_H264,
  3541. .write_header = mov_write_header,
  3542. .write_packet = mov_write_packet,
  3543. .write_trailer = mov_write_trailer,
  3544. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3545. .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
  3546. .priv_class = &ipod_muxer_class,
  3547. };
  3548. #endif
  3549. #if CONFIG_ISMV_MUXER
  3550. MOV_CLASS(ismv)
  3551. AVOutputFormat ff_ismv_muxer = {
  3552. .name = "ismv",
  3553. .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
  3554. .mime_type = "application/mp4",
  3555. .extensions = "ismv,isma",
  3556. .priv_data_size = sizeof(MOVMuxContext),
  3557. .audio_codec = AV_CODEC_ID_AAC,
  3558. .video_codec = AV_CODEC_ID_H264,
  3559. .write_header = mov_write_header,
  3560. .write_packet = mov_write_packet,
  3561. .write_trailer = mov_write_trailer,
  3562. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3563. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  3564. .priv_class = &ismv_muxer_class,
  3565. };
  3566. #endif
  3567. #if CONFIG_F4V_MUXER
  3568. MOV_CLASS(f4v)
  3569. AVOutputFormat ff_f4v_muxer = {
  3570. .name = "f4v",
  3571. .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
  3572. .mime_type = "application/f4v",
  3573. .extensions = "f4v",
  3574. .priv_data_size = sizeof(MOVMuxContext),
  3575. .audio_codec = AV_CODEC_ID_AAC,
  3576. .video_codec = AV_CODEC_ID_H264,
  3577. .write_header = mov_write_header,
  3578. .write_packet = mov_write_packet,
  3579. .write_trailer = mov_write_trailer,
  3580. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3581. .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
  3582. .priv_class = &f4v_muxer_class,
  3583. };
  3584. #endif