mov.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182
  1. /*
  2. * MOV demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <limits.h>
  23. //#define DEBUG
  24. //#define MOV_EXPORT_ALL_METADATA
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/audioconvert.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/intfloat.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/dict.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/timecode.h"
  34. #include "libavcodec/ac3tab.h"
  35. #include "avformat.h"
  36. #include "internal.h"
  37. #include "avio_internal.h"
  38. #include "riff.h"
  39. #include "isom.h"
  40. #include "libavcodec/get_bits.h"
  41. #include "id3v1.h"
  42. #include "mov_chan.h"
  43. #if CONFIG_ZLIB
  44. #include <zlib.h>
  45. #endif
  46. /*
  47. * First version by Francois Revol revol@free.fr
  48. * Seek function by Gael Chardon gael.dev@4now.net
  49. */
  50. #include "qtpalette.h"
  51. #undef NDEBUG
  52. #include <assert.h>
  53. /* those functions parse an atom */
  54. /* links atom IDs to parse functions */
  55. typedef struct MOVParseTableEntry {
  56. uint32_t type;
  57. int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
  58. } MOVParseTableEntry;
  59. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
  60. static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
  61. unsigned len, const char *key)
  62. {
  63. char buf[16];
  64. short current, total = 0;
  65. avio_rb16(pb); // unknown
  66. current = avio_rb16(pb);
  67. if (len >= 6)
  68. total = avio_rb16(pb);
  69. if (!total)
  70. snprintf(buf, sizeof(buf), "%d", current);
  71. else
  72. snprintf(buf, sizeof(buf), "%d/%d", current, total);
  73. av_dict_set(&c->fc->metadata, key, buf, 0);
  74. return 0;
  75. }
  76. static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
  77. unsigned len, const char *key)
  78. {
  79. char buf[16];
  80. /* bypass padding bytes */
  81. avio_r8(pb);
  82. avio_r8(pb);
  83. avio_r8(pb);
  84. snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
  85. av_dict_set(&c->fc->metadata, key, buf, 0);
  86. return 0;
  87. }
  88. static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
  89. unsigned len, const char *key)
  90. {
  91. char buf[16];
  92. snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
  93. av_dict_set(&c->fc->metadata, key, buf, 0);
  94. return 0;
  95. }
  96. static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
  97. unsigned len, const char *key)
  98. {
  99. short genre;
  100. char buf[20];
  101. avio_r8(pb); // unknown
  102. genre = avio_r8(pb);
  103. if (genre < 1 || genre > ID3v1_GENRE_MAX)
  104. return 0;
  105. snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
  106. av_dict_set(&c->fc->metadata, key, buf, 0);
  107. return 0;
  108. }
  109. static const uint32_t mac_to_unicode[128] = {
  110. 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
  111. 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
  112. 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
  113. 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
  114. 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
  115. 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
  116. 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
  117. 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
  118. 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
  119. 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
  120. 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
  121. 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
  122. 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
  123. 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
  124. 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
  125. 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
  126. };
  127. static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
  128. char *dst, int dstlen)
  129. {
  130. char *p = dst;
  131. char *end = dst+dstlen-1;
  132. int i;
  133. for (i = 0; i < len; i++) {
  134. uint8_t t, c = avio_r8(pb);
  135. if (c < 0x80 && p < end)
  136. *p++ = c;
  137. else if (p < end)
  138. PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
  139. }
  140. *p = 0;
  141. return p - dst;
  142. }
  143. static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
  144. {
  145. AVPacket pkt;
  146. AVStream *st;
  147. MOVStreamContext *sc;
  148. enum AVCodecID id;
  149. int ret;
  150. switch (type) {
  151. case 0xd: id = AV_CODEC_ID_MJPEG; break;
  152. case 0xe: id = AV_CODEC_ID_PNG; break;
  153. case 0x1b: id = AV_CODEC_ID_BMP; break;
  154. default:
  155. av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
  156. avio_skip(pb, len);
  157. return 0;
  158. }
  159. st = avformat_new_stream(c->fc, NULL);
  160. if (!st)
  161. return AVERROR(ENOMEM);
  162. sc = av_mallocz(sizeof(*sc));
  163. if (!sc)
  164. return AVERROR(ENOMEM);
  165. st->priv_data = sc;
  166. ret = av_get_packet(pb, &pkt, len);
  167. if (ret < 0)
  168. return ret;
  169. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  170. st->attached_pic = pkt;
  171. st->attached_pic.stream_index = st->index;
  172. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  173. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  174. st->codec->codec_id = id;
  175. return 0;
  176. }
  177. static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  178. {
  179. #ifdef MOV_EXPORT_ALL_METADATA
  180. char tmp_key[5];
  181. #endif
  182. char str[1024], key2[16], language[4] = {0};
  183. const char *key = NULL;
  184. uint16_t langcode = 0;
  185. uint32_t data_type = 0, str_size;
  186. int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
  187. switch (atom.type) {
  188. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  189. case MKTAG(0xa9,'a','u','t'):
  190. case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
  191. case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
  192. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  193. case MKTAG( 'c','p','r','t'):
  194. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  195. case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
  196. case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
  197. case MKTAG(0xa9,'c','m','t'):
  198. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  199. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  200. case MKTAG(0xa9,'d','a','y'): key = "date"; break;
  201. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  202. case MKTAG( 'g','n','r','e'): key = "genre";
  203. parse = mov_metadata_gnre; break;
  204. case MKTAG(0xa9,'t','o','o'):
  205. case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
  206. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  207. case MKTAG( 'd','e','s','c'): key = "description";break;
  208. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  209. case MKTAG( 't','v','s','h'): key = "show"; break;
  210. case MKTAG( 't','v','e','n'): key = "episode_id";break;
  211. case MKTAG( 't','v','n','n'): key = "network"; break;
  212. case MKTAG( 't','r','k','n'): key = "track";
  213. parse = mov_metadata_track_or_disc_number; break;
  214. case MKTAG( 'd','i','s','k'): key = "disc";
  215. parse = mov_metadata_track_or_disc_number; break;
  216. case MKTAG( 't','v','e','s'): key = "episode_sort";
  217. parse = mov_metadata_int8_bypass_padding; break;
  218. case MKTAG( 't','v','s','n'): key = "season_number";
  219. parse = mov_metadata_int8_bypass_padding; break;
  220. case MKTAG( 's','t','i','k'): key = "media_type";
  221. parse = mov_metadata_int8_no_padding; break;
  222. case MKTAG( 'h','d','v','d'): key = "hd_video";
  223. parse = mov_metadata_int8_no_padding; break;
  224. case MKTAG( 'p','g','a','p'): key = "gapless_playback";
  225. parse = mov_metadata_int8_no_padding; break;
  226. }
  227. if (c->itunes_metadata && atom.size > 8) {
  228. int data_size = avio_rb32(pb);
  229. int tag = avio_rl32(pb);
  230. if (tag == MKTAG('d','a','t','a')) {
  231. data_type = avio_rb32(pb); // type
  232. avio_rb32(pb); // unknown
  233. str_size = data_size - 16;
  234. atom.size -= 16;
  235. if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
  236. int ret = mov_read_covr(c, pb, data_type, str_size);
  237. if (ret < 0) {
  238. av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
  239. return ret;
  240. }
  241. }
  242. } else return 0;
  243. } else if (atom.size > 4 && key && !c->itunes_metadata) {
  244. str_size = avio_rb16(pb); // string length
  245. langcode = avio_rb16(pb);
  246. ff_mov_lang_to_iso639(langcode, language);
  247. atom.size -= 4;
  248. } else
  249. str_size = atom.size;
  250. #ifdef MOV_EXPORT_ALL_METADATA
  251. if (!key) {
  252. snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
  253. key = tmp_key;
  254. }
  255. #endif
  256. if (!key)
  257. return 0;
  258. if (atom.size < 0)
  259. return AVERROR_INVALIDDATA;
  260. str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
  261. if (parse)
  262. parse(c, pb, str_size, key);
  263. else {
  264. if (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff))) { // MAC Encoded
  265. mov_read_mac_string(c, pb, str_size, str, sizeof(str));
  266. } else {
  267. avio_read(pb, str, str_size);
  268. str[str_size] = 0;
  269. }
  270. av_dict_set(&c->fc->metadata, key, str, 0);
  271. if (*language && strcmp(language, "und")) {
  272. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  273. av_dict_set(&c->fc->metadata, key2, str, 0);
  274. }
  275. }
  276. av_dlog(c->fc, "lang \"%3s\" ", language);
  277. av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
  278. key, str, (char*)&atom.type, str_size, atom.size);
  279. return 0;
  280. }
  281. static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  282. {
  283. int64_t start;
  284. int i, nb_chapters, str_len, version;
  285. char str[256+1];
  286. if ((atom.size -= 5) < 0)
  287. return 0;
  288. version = avio_r8(pb);
  289. avio_rb24(pb);
  290. if (version)
  291. avio_rb32(pb); // ???
  292. nb_chapters = avio_r8(pb);
  293. for (i = 0; i < nb_chapters; i++) {
  294. if (atom.size < 9)
  295. return 0;
  296. start = avio_rb64(pb);
  297. str_len = avio_r8(pb);
  298. if ((atom.size -= 9+str_len) < 0)
  299. return 0;
  300. avio_read(pb, str, str_len);
  301. str[str_len] = 0;
  302. avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
  303. }
  304. return 0;
  305. }
  306. static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  307. {
  308. AVStream *st;
  309. MOVStreamContext *sc;
  310. int entries, i, j;
  311. if (c->fc->nb_streams < 1)
  312. return 0;
  313. st = c->fc->streams[c->fc->nb_streams-1];
  314. sc = st->priv_data;
  315. avio_rb32(pb); // version + flags
  316. entries = avio_rb32(pb);
  317. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  318. return AVERROR_INVALIDDATA;
  319. av_free(sc->drefs);
  320. sc->drefs_count = 0;
  321. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  322. if (!sc->drefs)
  323. return AVERROR(ENOMEM);
  324. sc->drefs_count = entries;
  325. for (i = 0; i < sc->drefs_count; i++) {
  326. MOVDref *dref = &sc->drefs[i];
  327. uint32_t size = avio_rb32(pb);
  328. int64_t next = avio_tell(pb) + size - 4;
  329. if (size < 12)
  330. return AVERROR_INVALIDDATA;
  331. dref->type = avio_rl32(pb);
  332. avio_rb32(pb); // version + flags
  333. av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  334. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  335. /* macintosh alias record */
  336. uint16_t volume_len, len;
  337. int16_t type;
  338. avio_skip(pb, 10);
  339. volume_len = avio_r8(pb);
  340. volume_len = FFMIN(volume_len, 27);
  341. avio_read(pb, dref->volume, 27);
  342. dref->volume[volume_len] = 0;
  343. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  344. avio_skip(pb, 12);
  345. len = avio_r8(pb);
  346. len = FFMIN(len, 63);
  347. avio_read(pb, dref->filename, 63);
  348. dref->filename[len] = 0;
  349. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  350. avio_skip(pb, 16);
  351. /* read next level up_from_alias/down_to_target */
  352. dref->nlvl_from = avio_rb16(pb);
  353. dref->nlvl_to = avio_rb16(pb);
  354. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  355. dref->nlvl_from, dref->nlvl_to);
  356. avio_skip(pb, 16);
  357. for (type = 0; type != -1 && avio_tell(pb) < next; ) {
  358. if(url_feof(pb))
  359. return AVERROR_EOF;
  360. type = avio_rb16(pb);
  361. len = avio_rb16(pb);
  362. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  363. if (len&1)
  364. len += 1;
  365. if (type == 2) { // absolute path
  366. av_free(dref->path);
  367. dref->path = av_mallocz(len+1);
  368. if (!dref->path)
  369. return AVERROR(ENOMEM);
  370. avio_read(pb, dref->path, len);
  371. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  372. len -= volume_len;
  373. memmove(dref->path, dref->path+volume_len, len);
  374. dref->path[len] = 0;
  375. }
  376. for (j = 0; j < len; j++)
  377. if (dref->path[j] == ':')
  378. dref->path[j] = '/';
  379. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  380. } else if (type == 0) { // directory name
  381. av_free(dref->dir);
  382. dref->dir = av_malloc(len+1);
  383. if (!dref->dir)
  384. return AVERROR(ENOMEM);
  385. avio_read(pb, dref->dir, len);
  386. dref->dir[len] = 0;
  387. for (j = 0; j < len; j++)
  388. if (dref->dir[j] == ':')
  389. dref->dir[j] = '/';
  390. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  391. } else
  392. avio_skip(pb, len);
  393. }
  394. }
  395. avio_seek(pb, next, SEEK_SET);
  396. }
  397. return 0;
  398. }
  399. static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  400. {
  401. AVStream *st;
  402. uint32_t type;
  403. uint32_t av_unused ctype;
  404. int title_size;
  405. char *title_str;
  406. if (c->fc->nb_streams < 1) // meta before first trak
  407. return 0;
  408. st = c->fc->streams[c->fc->nb_streams-1];
  409. avio_r8(pb); /* version */
  410. avio_rb24(pb); /* flags */
  411. /* component type */
  412. ctype = avio_rl32(pb);
  413. type = avio_rl32(pb); /* component subtype */
  414. av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
  415. av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
  416. if (type == MKTAG('v','i','d','e'))
  417. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  418. else if (type == MKTAG('s','o','u','n'))
  419. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  420. else if (type == MKTAG('m','1','a',' '))
  421. st->codec->codec_id = AV_CODEC_ID_MP2;
  422. else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
  423. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  424. avio_rb32(pb); /* component manufacture */
  425. avio_rb32(pb); /* component flags */
  426. avio_rb32(pb); /* component flags mask */
  427. title_size = atom.size - 24;
  428. if (title_size > 0) {
  429. title_str = av_malloc(title_size + 1); /* Add null terminator */
  430. if (!title_str)
  431. return AVERROR(ENOMEM);
  432. avio_read(pb, title_str, title_size);
  433. title_str[title_size] = 0;
  434. if (title_str[0])
  435. av_dict_set(&st->metadata, "handler_name", title_str +
  436. (!c->isom && title_str[0] == title_size - 1), 0);
  437. av_freep(&title_str);
  438. }
  439. return 0;
  440. }
  441. int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
  442. {
  443. AVStream *st;
  444. int tag;
  445. if (fc->nb_streams < 1)
  446. return 0;
  447. st = fc->streams[fc->nb_streams-1];
  448. avio_rb32(pb); /* version + flags */
  449. ff_mp4_read_descr(fc, pb, &tag);
  450. if (tag == MP4ESDescrTag) {
  451. ff_mp4_parse_es_descr(pb, NULL);
  452. } else
  453. avio_rb16(pb); /* ID */
  454. ff_mp4_read_descr(fc, pb, &tag);
  455. if (tag == MP4DecConfigDescrTag)
  456. ff_mp4_read_dec_config_descr(fc, st, pb);
  457. return 0;
  458. }
  459. static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  460. {
  461. return ff_mov_read_esds(c->fc, pb, atom);
  462. }
  463. static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  464. {
  465. AVStream *st;
  466. int ac3info, acmod, lfeon, bsmod;
  467. if (c->fc->nb_streams < 1)
  468. return 0;
  469. st = c->fc->streams[c->fc->nb_streams-1];
  470. ac3info = avio_rb24(pb);
  471. bsmod = (ac3info >> 14) & 0x7;
  472. acmod = (ac3info >> 11) & 0x7;
  473. lfeon = (ac3info >> 10) & 0x1;
  474. st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
  475. st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  476. if (lfeon)
  477. st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
  478. st->codec->audio_service_type = bsmod;
  479. if (st->codec->channels > 1 && bsmod == 0x7)
  480. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  481. return 0;
  482. }
  483. static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  484. {
  485. AVStream *st;
  486. int eac3info, acmod, lfeon, bsmod;
  487. if (c->fc->nb_streams < 1)
  488. return 0;
  489. st = c->fc->streams[c->fc->nb_streams-1];
  490. /* No need to parse fields for additional independent substreams and its
  491. * associated dependent substreams since libavcodec's E-AC-3 decoder
  492. * does not support them yet. */
  493. avio_rb16(pb); /* data_rate and num_ind_sub */
  494. eac3info = avio_rb24(pb);
  495. bsmod = (eac3info >> 12) & 0x1f;
  496. acmod = (eac3info >> 9) & 0x7;
  497. lfeon = (eac3info >> 8) & 0x1;
  498. st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  499. if (lfeon)
  500. st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
  501. st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
  502. st->codec->audio_service_type = bsmod;
  503. if (st->codec->channels > 1 && bsmod == 0x7)
  504. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  505. return 0;
  506. }
  507. static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  508. {
  509. AVStream *st;
  510. if (c->fc->nb_streams < 1)
  511. return 0;
  512. st = c->fc->streams[c->fc->nb_streams-1];
  513. if (atom.size < 16)
  514. return 0;
  515. ff_mov_read_chan(c->fc, st, atom.size - 4);
  516. return 0;
  517. }
  518. static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  519. {
  520. AVStream *st;
  521. if (c->fc->nb_streams < 1)
  522. return 0;
  523. st = c->fc->streams[c->fc->nb_streams-1];
  524. ff_get_wav_header(pb, st->codec, atom.size);
  525. return 0;
  526. }
  527. static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  528. {
  529. const int num = avio_rb32(pb);
  530. const int den = avio_rb32(pb);
  531. AVStream *st;
  532. if (c->fc->nb_streams < 1)
  533. return 0;
  534. st = c->fc->streams[c->fc->nb_streams-1];
  535. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  536. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
  537. av_log(c->fc, AV_LOG_WARNING,
  538. "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
  539. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  540. num, den);
  541. } else if (den != 0) {
  542. st->sample_aspect_ratio.num = num;
  543. st->sample_aspect_ratio.den = den;
  544. }
  545. return 0;
  546. }
  547. /* this atom contains actual media data */
  548. static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  549. {
  550. if (atom.size == 0) /* wrong one (MP4) */
  551. return 0;
  552. c->found_mdat=1;
  553. return 0; /* now go for moov */
  554. }
  555. /* read major brand, minor version and compatible brands and store them as metadata */
  556. static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  557. {
  558. uint32_t minor_ver;
  559. int comp_brand_size;
  560. char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
  561. char* comp_brands_str;
  562. uint8_t type[5] = {0};
  563. avio_read(pb, type, 4);
  564. if (strcmp(type, "qt "))
  565. c->isom = 1;
  566. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  567. av_dict_set(&c->fc->metadata, "major_brand", type, 0);
  568. minor_ver = avio_rb32(pb); /* minor version */
  569. snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
  570. av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
  571. comp_brand_size = atom.size - 8;
  572. if (comp_brand_size < 0)
  573. return AVERROR_INVALIDDATA;
  574. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  575. if (!comp_brands_str)
  576. return AVERROR(ENOMEM);
  577. avio_read(pb, comp_brands_str, comp_brand_size);
  578. comp_brands_str[comp_brand_size] = 0;
  579. av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
  580. av_freep(&comp_brands_str);
  581. return 0;
  582. }
  583. /* this atom should contain all header atoms */
  584. static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  585. {
  586. int ret;
  587. if ((ret = mov_read_default(c, pb, atom)) < 0)
  588. return ret;
  589. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  590. /* so we don't parse the whole file if over a network */
  591. c->found_moov=1;
  592. return 0; /* now go for mdat */
  593. }
  594. static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  595. {
  596. c->fragment.moof_offset = avio_tell(pb) - 8;
  597. av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
  598. return mov_read_default(c, pb, atom);
  599. }
  600. static void mov_metadata_creation_time(AVDictionary **metadata, time_t time)
  601. {
  602. char buffer[32];
  603. if (time) {
  604. struct tm *ptm;
  605. if(time >= 2082844800)
  606. time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
  607. ptm = gmtime(&time);
  608. if (!ptm) return;
  609. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
  610. av_dict_set(metadata, "creation_time", buffer, 0);
  611. }
  612. }
  613. static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  614. {
  615. AVStream *st;
  616. MOVStreamContext *sc;
  617. int version;
  618. char language[4] = {0};
  619. unsigned lang;
  620. time_t creation_time;
  621. if (c->fc->nb_streams < 1)
  622. return 0;
  623. st = c->fc->streams[c->fc->nb_streams-1];
  624. sc = st->priv_data;
  625. version = avio_r8(pb);
  626. if (version > 1) {
  627. av_log_ask_for_sample(c, "unsupported version %d\n", version);
  628. return AVERROR_PATCHWELCOME;
  629. }
  630. avio_rb24(pb); /* flags */
  631. if (version == 1) {
  632. creation_time = avio_rb64(pb);
  633. avio_rb64(pb);
  634. } else {
  635. creation_time = avio_rb32(pb);
  636. avio_rb32(pb); /* modification time */
  637. }
  638. mov_metadata_creation_time(&st->metadata, creation_time);
  639. sc->time_scale = avio_rb32(pb);
  640. st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  641. lang = avio_rb16(pb); /* language */
  642. if (ff_mov_lang_to_iso639(lang, language))
  643. av_dict_set(&st->metadata, "language", language, 0);
  644. avio_rb16(pb); /* quality */
  645. return 0;
  646. }
  647. static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  648. {
  649. time_t creation_time;
  650. int version = avio_r8(pb); /* version */
  651. avio_rb24(pb); /* flags */
  652. if (version == 1) {
  653. creation_time = avio_rb64(pb);
  654. avio_rb64(pb);
  655. } else {
  656. creation_time = avio_rb32(pb);
  657. avio_rb32(pb); /* modification time */
  658. }
  659. mov_metadata_creation_time(&c->fc->metadata, creation_time);
  660. c->time_scale = avio_rb32(pb); /* time scale */
  661. av_dlog(c->fc, "time scale = %i\n", c->time_scale);
  662. c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  663. // set the AVCodecContext duration because the duration of individual tracks
  664. // may be inaccurate
  665. if (c->time_scale > 0)
  666. c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
  667. avio_rb32(pb); /* preferred scale */
  668. avio_rb16(pb); /* preferred volume */
  669. avio_skip(pb, 10); /* reserved */
  670. avio_skip(pb, 36); /* display matrix */
  671. avio_rb32(pb); /* preview time */
  672. avio_rb32(pb); /* preview duration */
  673. avio_rb32(pb); /* poster time */
  674. avio_rb32(pb); /* selection time */
  675. avio_rb32(pb); /* selection duration */
  676. avio_rb32(pb); /* current time */
  677. avio_rb32(pb); /* next track ID */
  678. return 0;
  679. }
  680. static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  681. {
  682. AVStream *st;
  683. int little_endian;
  684. if (c->fc->nb_streams < 1)
  685. return 0;
  686. st = c->fc->streams[c->fc->nb_streams-1];
  687. little_endian = avio_rb16(pb) & 0xFF;
  688. av_dlog(c->fc, "enda %d\n", little_endian);
  689. if (little_endian == 1) {
  690. switch (st->codec->codec_id) {
  691. case AV_CODEC_ID_PCM_S24BE:
  692. st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
  693. break;
  694. case AV_CODEC_ID_PCM_S32BE:
  695. st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
  696. break;
  697. case AV_CODEC_ID_PCM_F32BE:
  698. st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
  699. break;
  700. case AV_CODEC_ID_PCM_F64BE:
  701. st->codec->codec_id = AV_CODEC_ID_PCM_F64LE;
  702. break;
  703. default:
  704. break;
  705. }
  706. }
  707. return 0;
  708. }
  709. static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  710. {
  711. AVStream *st;
  712. unsigned mov_field_order;
  713. enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
  714. if (c->fc->nb_streams < 1) // will happen with jp2 files
  715. return 0;
  716. st = c->fc->streams[c->fc->nb_streams-1];
  717. if (atom.size < 2)
  718. return AVERROR_INVALIDDATA;
  719. mov_field_order = avio_rb16(pb);
  720. if ((mov_field_order & 0xFF00) == 0x0100)
  721. decoded_field_order = AV_FIELD_PROGRESSIVE;
  722. else if ((mov_field_order & 0xFF00) == 0x0200) {
  723. switch (mov_field_order & 0xFF) {
  724. case 0x01: decoded_field_order = AV_FIELD_TT;
  725. break;
  726. case 0x06: decoded_field_order = AV_FIELD_BB;
  727. break;
  728. case 0x09: decoded_field_order = AV_FIELD_TB;
  729. break;
  730. case 0x0E: decoded_field_order = AV_FIELD_BT;
  731. break;
  732. }
  733. }
  734. if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
  735. av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
  736. }
  737. st->codec->field_order = decoded_field_order;
  738. return 0;
  739. }
  740. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  741. static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  742. enum AVCodecID codec_id)
  743. {
  744. AVStream *st;
  745. uint64_t size;
  746. uint8_t *buf;
  747. if (c->fc->nb_streams < 1) // will happen with jp2 files
  748. return 0;
  749. st= c->fc->streams[c->fc->nb_streams-1];
  750. if (st->codec->codec_id != codec_id)
  751. return 0; /* unexpected codec_id - don't mess with extradata */
  752. size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  753. if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  754. return AVERROR_INVALIDDATA;
  755. buf= av_realloc(st->codec->extradata, size);
  756. if (!buf)
  757. return AVERROR(ENOMEM);
  758. st->codec->extradata= buf;
  759. buf+= st->codec->extradata_size;
  760. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  761. AV_WB32( buf , atom.size + 8);
  762. AV_WL32( buf + 4, atom.type);
  763. avio_read(pb, buf + 8, atom.size);
  764. return 0;
  765. }
  766. /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
  767. static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  768. {
  769. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
  770. }
  771. static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  772. {
  773. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
  774. }
  775. static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  776. {
  777. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
  778. }
  779. static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  780. {
  781. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
  782. }
  783. static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  784. {
  785. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
  786. }
  787. static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  788. {
  789. AVStream *st;
  790. if (c->fc->nb_streams < 1)
  791. return 0;
  792. st = c->fc->streams[c->fc->nb_streams-1];
  793. if ((uint64_t)atom.size > (1<<30))
  794. return AVERROR_INVALIDDATA;
  795. if (st->codec->codec_id == AV_CODEC_ID_QDM2 || st->codec->codec_id == AV_CODEC_ID_QDMC) {
  796. // pass all frma atom to codec, needed at least for QDMC and QDM2
  797. av_free(st->codec->extradata);
  798. st->codec->extradata_size = 0;
  799. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  800. if (!st->codec->extradata)
  801. return AVERROR(ENOMEM);
  802. st->codec->extradata_size = atom.size;
  803. avio_read(pb, st->codec->extradata, atom.size);
  804. } else if (atom.size > 8) { /* to read frma, esds atoms */
  805. int ret;
  806. if ((ret = mov_read_default(c, pb, atom)) < 0)
  807. return ret;
  808. } else
  809. avio_skip(pb, atom.size);
  810. return 0;
  811. }
  812. /**
  813. * This function reads atom content and puts data in extradata without tag
  814. * nor size unlike mov_read_extradata.
  815. */
  816. static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  817. {
  818. AVStream *st;
  819. if (c->fc->nb_streams < 1)
  820. return 0;
  821. st = c->fc->streams[c->fc->nb_streams-1];
  822. if ((uint64_t)atom.size > (1<<30))
  823. return AVERROR_INVALIDDATA;
  824. if (atom.size >= 10) {
  825. // Broken files created by legacy versions of libavformat will
  826. // wrap a whole fiel atom inside of a glbl atom.
  827. unsigned size = avio_rb32(pb);
  828. unsigned type = avio_rl32(pb);
  829. avio_seek(pb, -8, SEEK_CUR);
  830. if (type == MKTAG('f','i','e','l') && size == atom.size)
  831. return mov_read_default(c, pb, atom);
  832. }
  833. av_free(st->codec->extradata);
  834. st->codec->extradata_size = 0;
  835. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  836. if (!st->codec->extradata)
  837. return AVERROR(ENOMEM);
  838. st->codec->extradata_size = atom.size;
  839. avio_read(pb, st->codec->extradata, atom.size);
  840. return 0;
  841. }
  842. static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  843. {
  844. AVStream *st;
  845. uint8_t profile_level;
  846. if (c->fc->nb_streams < 1)
  847. return 0;
  848. st = c->fc->streams[c->fc->nb_streams-1];
  849. if (atom.size >= (1<<28) || atom.size < 7)
  850. return AVERROR_INVALIDDATA;
  851. profile_level = avio_r8(pb);
  852. if ((profile_level & 0xf0) != 0xc0)
  853. return 0;
  854. av_free(st->codec->extradata);
  855. st->codec->extradata_size = 0;
  856. st->codec->extradata = av_mallocz(atom.size - 7 + FF_INPUT_BUFFER_PADDING_SIZE);
  857. if (!st->codec->extradata)
  858. return AVERROR(ENOMEM);
  859. st->codec->extradata_size = atom.size - 7;
  860. avio_seek(pb, 6, SEEK_CUR);
  861. avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  862. return 0;
  863. }
  864. /**
  865. * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
  866. * but can have extradata appended at the end after the 40 bytes belonging
  867. * to the struct.
  868. */
  869. static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  870. {
  871. AVStream *st;
  872. if (c->fc->nb_streams < 1)
  873. return 0;
  874. if (atom.size <= 40)
  875. return 0;
  876. st = c->fc->streams[c->fc->nb_streams-1];
  877. if ((uint64_t)atom.size > (1<<30))
  878. return AVERROR_INVALIDDATA;
  879. av_free(st->codec->extradata);
  880. st->codec->extradata_size = 0;
  881. st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
  882. if (!st->codec->extradata)
  883. return AVERROR(ENOMEM);
  884. st->codec->extradata_size = atom.size - 40;
  885. avio_skip(pb, 40);
  886. avio_read(pb, st->codec->extradata, atom.size - 40);
  887. return 0;
  888. }
  889. static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  890. {
  891. AVStream *st;
  892. MOVStreamContext *sc;
  893. unsigned int i, entries;
  894. if (c->fc->nb_streams < 1)
  895. return 0;
  896. st = c->fc->streams[c->fc->nb_streams-1];
  897. sc = st->priv_data;
  898. avio_r8(pb); /* version */
  899. avio_rb24(pb); /* flags */
  900. entries = avio_rb32(pb);
  901. if (!entries)
  902. return 0;
  903. if (entries >= UINT_MAX/sizeof(int64_t))
  904. return AVERROR_INVALIDDATA;
  905. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  906. if (!sc->chunk_offsets)
  907. return AVERROR(ENOMEM);
  908. sc->chunk_count = entries;
  909. if (atom.type == MKTAG('s','t','c','o'))
  910. for (i=0; i<entries; i++)
  911. sc->chunk_offsets[i] = avio_rb32(pb);
  912. else if (atom.type == MKTAG('c','o','6','4'))
  913. for (i=0; i<entries; i++)
  914. sc->chunk_offsets[i] = avio_rb64(pb);
  915. else
  916. return AVERROR_INVALIDDATA;
  917. return 0;
  918. }
  919. /**
  920. * Compute codec id for 'lpcm' tag.
  921. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  922. */
  923. enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
  924. {
  925. if (flags & 1) { // floating point
  926. if (flags & 2) { // big endian
  927. if (bps == 32) return AV_CODEC_ID_PCM_F32BE;
  928. else if (bps == 64) return AV_CODEC_ID_PCM_F64BE;
  929. } else {
  930. if (bps == 32) return AV_CODEC_ID_PCM_F32LE;
  931. else if (bps == 64) return AV_CODEC_ID_PCM_F64LE;
  932. }
  933. } else {
  934. if (flags & 2) {
  935. if (bps == 8)
  936. // signed integer
  937. if (flags & 4) return AV_CODEC_ID_PCM_S8;
  938. else return AV_CODEC_ID_PCM_U8;
  939. else if (bps == 16) return AV_CODEC_ID_PCM_S16BE;
  940. else if (bps == 24) return AV_CODEC_ID_PCM_S24BE;
  941. else if (bps == 32) return AV_CODEC_ID_PCM_S32BE;
  942. } else {
  943. if (bps == 8)
  944. if (flags & 4) return AV_CODEC_ID_PCM_S8;
  945. else return AV_CODEC_ID_PCM_U8;
  946. else if (bps == 16) return AV_CODEC_ID_PCM_S16LE;
  947. else if (bps == 24) return AV_CODEC_ID_PCM_S24LE;
  948. else if (bps == 32) return AV_CODEC_ID_PCM_S32LE;
  949. }
  950. }
  951. return AV_CODEC_ID_NONE;
  952. }
  953. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
  954. {
  955. AVStream *st;
  956. MOVStreamContext *sc;
  957. int j, pseudo_stream_id;
  958. if (c->fc->nb_streams < 1)
  959. return 0;
  960. st = c->fc->streams[c->fc->nb_streams-1];
  961. sc = st->priv_data;
  962. for (pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
  963. //Parsing Sample description table
  964. enum AVCodecID id;
  965. int dref_id = 1;
  966. MOVAtom a = { AV_RL32("stsd") };
  967. int64_t start_pos = avio_tell(pb);
  968. int size = avio_rb32(pb); /* size */
  969. uint32_t format = avio_rl32(pb); /* data format */
  970. if (size >= 16) {
  971. avio_rb32(pb); /* reserved */
  972. avio_rb16(pb); /* reserved */
  973. dref_id = avio_rb16(pb);
  974. }else if (size <= 0){
  975. av_log(c->fc, AV_LOG_ERROR, "invalid size %d in stsd\n", size);
  976. return -1;
  977. }
  978. if (st->codec->codec_tag &&
  979. st->codec->codec_tag != format &&
  980. (c->fc->video_codec_id ? ff_codec_get_id(ff_codec_movvideo_tags, format) != c->fc->video_codec_id
  981. : st->codec->codec_tag != MKTAG('j','p','e','g'))
  982. ){
  983. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  984. * export it as a separate AVStream but this needs a few changes
  985. * in the MOV demuxer, patch welcome. */
  986. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  987. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  988. continue;
  989. }
  990. /* we cannot demux concatenated h264 streams because of different extradata */
  991. if (st->codec->codec_tag && st->codec->codec_tag == AV_RL32("avc1"))
  992. av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 might not play corrently.\n");
  993. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  994. sc->dref_id= dref_id;
  995. st->codec->codec_tag = format;
  996. id = ff_codec_get_id(ff_codec_movaudio_tags, format);
  997. if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
  998. id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format)&0xFFFF);
  999. if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
  1000. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1001. } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && /* do not overwrite codec type */
  1002. format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
  1003. id = ff_codec_get_id(ff_codec_movvideo_tags, format);
  1004. if (id <= 0)
  1005. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  1006. if (id > 0)
  1007. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1008. else if (st->codec->codec_type == AVMEDIA_TYPE_DATA ||
  1009. (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  1010. st->codec->codec_id == AV_CODEC_ID_NONE)){
  1011. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  1012. if (id > 0)
  1013. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1014. }
  1015. }
  1016. av_dlog(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
  1017. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  1018. (format >> 24) & 0xff, st->codec->codec_type);
  1019. if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  1020. unsigned int color_depth, len;
  1021. int color_greyscale;
  1022. int color_table_id;
  1023. st->codec->codec_id = id;
  1024. avio_rb16(pb); /* version */
  1025. avio_rb16(pb); /* revision level */
  1026. avio_rb32(pb); /* vendor */
  1027. avio_rb32(pb); /* temporal quality */
  1028. avio_rb32(pb); /* spatial quality */
  1029. st->codec->width = avio_rb16(pb); /* width */
  1030. st->codec->height = avio_rb16(pb); /* height */
  1031. avio_rb32(pb); /* horiz resolution */
  1032. avio_rb32(pb); /* vert resolution */
  1033. avio_rb32(pb); /* data size, always 0 */
  1034. avio_rb16(pb); /* frames per samples */
  1035. len = avio_r8(pb); /* codec name, pascal string */
  1036. if (len > 31)
  1037. len = 31;
  1038. mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
  1039. if (len < 31)
  1040. avio_skip(pb, 31 - len);
  1041. /* codec_tag YV12 triggers an UV swap in rawdec.c */
  1042. if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
  1043. st->codec->codec_tag=MKTAG('I', '4', '2', '0');
  1044. st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
  1045. color_table_id = avio_rb16(pb); /* colortable id */
  1046. av_dlog(c->fc, "depth %d, ctab id %d\n",
  1047. st->codec->bits_per_coded_sample, color_table_id);
  1048. /* figure out the palette situation */
  1049. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  1050. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  1051. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  1052. if ((color_depth == 2) || (color_depth == 4) ||
  1053. (color_depth == 8)) {
  1054. /* for palette traversal */
  1055. unsigned int color_start, color_count, color_end;
  1056. unsigned char a, r, g, b;
  1057. if (color_greyscale) {
  1058. int color_index, color_dec;
  1059. /* compute the greyscale palette */
  1060. st->codec->bits_per_coded_sample = color_depth;
  1061. color_count = 1 << color_depth;
  1062. color_index = 255;
  1063. color_dec = 256 / (color_count - 1);
  1064. for (j = 0; j < color_count; j++) {
  1065. if (id == AV_CODEC_ID_CINEPAK){
  1066. r = g = b = color_count - 1 - color_index;
  1067. }else
  1068. r = g = b = color_index;
  1069. sc->palette[j] =
  1070. (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  1071. color_index -= color_dec;
  1072. if (color_index < 0)
  1073. color_index = 0;
  1074. }
  1075. } else if (color_table_id) {
  1076. const uint8_t *color_table;
  1077. /* if flag bit 3 is set, use the default palette */
  1078. color_count = 1 << color_depth;
  1079. if (color_depth == 2)
  1080. color_table = ff_qt_default_palette_4;
  1081. else if (color_depth == 4)
  1082. color_table = ff_qt_default_palette_16;
  1083. else
  1084. color_table = ff_qt_default_palette_256;
  1085. for (j = 0; j < color_count; j++) {
  1086. r = color_table[j * 3 + 0];
  1087. g = color_table[j * 3 + 1];
  1088. b = color_table[j * 3 + 2];
  1089. sc->palette[j] =
  1090. (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  1091. }
  1092. } else {
  1093. /* load the palette from the file */
  1094. color_start = avio_rb32(pb);
  1095. color_count = avio_rb16(pb);
  1096. color_end = avio_rb16(pb);
  1097. if ((color_start <= 255) &&
  1098. (color_end <= 255)) {
  1099. for (j = color_start; j <= color_end; j++) {
  1100. /* each A, R, G, or B component is 16 bits;
  1101. * only use the top 8 bits */
  1102. a = avio_r8(pb);
  1103. avio_r8(pb);
  1104. r = avio_r8(pb);
  1105. avio_r8(pb);
  1106. g = avio_r8(pb);
  1107. avio_r8(pb);
  1108. b = avio_r8(pb);
  1109. avio_r8(pb);
  1110. sc->palette[j] =
  1111. (a << 24 ) | (r << 16) | (g << 8) | (b);
  1112. }
  1113. }
  1114. }
  1115. sc->has_palette = 1;
  1116. }
  1117. } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
  1118. int bits_per_sample, flags;
  1119. uint16_t version = avio_rb16(pb);
  1120. st->codec->codec_id = id;
  1121. avio_rb16(pb); /* revision level */
  1122. avio_rb32(pb); /* vendor */
  1123. st->codec->channels = avio_rb16(pb); /* channel count */
  1124. av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
  1125. st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
  1126. sc->audio_cid = avio_rb16(pb);
  1127. avio_rb16(pb); /* packet size = 0 */
  1128. st->codec->sample_rate = ((avio_rb32(pb) >> 16));
  1129. //Read QT version 1 fields. In version 0 these do not exist.
  1130. av_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
  1131. if (!c->isom) {
  1132. if (version==1) {
  1133. sc->samples_per_frame = avio_rb32(pb);
  1134. avio_rb32(pb); /* bytes per packet */
  1135. sc->bytes_per_frame = avio_rb32(pb);
  1136. avio_rb32(pb); /* bytes per sample */
  1137. } else if (version==2) {
  1138. avio_rb32(pb); /* sizeof struct only */
  1139. st->codec->sample_rate = av_int2double(avio_rb64(pb)); /* float 64 */
  1140. st->codec->channels = avio_rb32(pb);
  1141. avio_rb32(pb); /* always 0x7F000000 */
  1142. st->codec->bits_per_coded_sample = avio_rb32(pb); /* bits per channel if sound is uncompressed */
  1143. flags = avio_rb32(pb); /* lpcm format specific flag */
  1144. sc->bytes_per_frame = avio_rb32(pb); /* bytes per audio packet if constant */
  1145. sc->samples_per_frame = avio_rb32(pb); /* lpcm frames per audio packet if constant */
  1146. if (format == MKTAG('l','p','c','m'))
  1147. st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  1148. }
  1149. }
  1150. switch (st->codec->codec_id) {
  1151. case AV_CODEC_ID_PCM_S8:
  1152. case AV_CODEC_ID_PCM_U8:
  1153. if (st->codec->bits_per_coded_sample == 16)
  1154. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  1155. break;
  1156. case AV_CODEC_ID_PCM_S16LE:
  1157. case AV_CODEC_ID_PCM_S16BE:
  1158. if (st->codec->bits_per_coded_sample == 8)
  1159. st->codec->codec_id = AV_CODEC_ID_PCM_S8;
  1160. else if (st->codec->bits_per_coded_sample == 24)
  1161. st->codec->codec_id =
  1162. st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
  1163. AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
  1164. break;
  1165. /* set values for old format before stsd version 1 appeared */
  1166. case AV_CODEC_ID_MACE3:
  1167. sc->samples_per_frame = 6;
  1168. sc->bytes_per_frame = 2*st->codec->channels;
  1169. break;
  1170. case AV_CODEC_ID_MACE6:
  1171. sc->samples_per_frame = 6;
  1172. sc->bytes_per_frame = 1*st->codec->channels;
  1173. break;
  1174. case AV_CODEC_ID_ADPCM_IMA_QT:
  1175. sc->samples_per_frame = 64;
  1176. sc->bytes_per_frame = 34*st->codec->channels;
  1177. break;
  1178. case AV_CODEC_ID_GSM:
  1179. sc->samples_per_frame = 160;
  1180. sc->bytes_per_frame = 33;
  1181. break;
  1182. default:
  1183. break;
  1184. }
  1185. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  1186. if (bits_per_sample) {
  1187. st->codec->bits_per_coded_sample = bits_per_sample;
  1188. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  1189. }
  1190. } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
  1191. // ttxt stsd contains display flags, justification, background
  1192. // color, fonts, and default styles, so fake an atom to read it
  1193. MOVAtom fake_atom = { .size = size - (avio_tell(pb) - start_pos) };
  1194. if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
  1195. mov_read_glbl(c, pb, fake_atom);
  1196. st->codec->codec_id= id;
  1197. st->codec->width = sc->width;
  1198. st->codec->height = sc->height;
  1199. } else {
  1200. if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
  1201. MOVStreamContext *tmcd_ctx = st->priv_data;
  1202. int val;
  1203. avio_rb32(pb); /* reserved */
  1204. val = avio_rb32(pb); /* flags */
  1205. tmcd_ctx->tmcd_flags = val;
  1206. if (val & 1)
  1207. st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
  1208. avio_rb32(pb); /* time scale */
  1209. avio_rb32(pb); /* frame duration */
  1210. st->codec->time_base.den = avio_r8(pb); /* number of frame */
  1211. st->codec->time_base.num = 1;
  1212. }
  1213. /* other codec type, just skip (rtp, mp4s, ...) */
  1214. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  1215. }
  1216. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  1217. a.size = size - (avio_tell(pb) - start_pos);
  1218. if (a.size > 8) {
  1219. int ret;
  1220. if ((ret = mov_read_default(c, pb, a)) < 0)
  1221. return ret;
  1222. } else if (a.size > 0)
  1223. avio_skip(pb, a.size);
  1224. }
  1225. if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  1226. st->codec->sample_rate= sc->time_scale;
  1227. /* special codec parameters handling */
  1228. switch (st->codec->codec_id) {
  1229. #if CONFIG_DV_DEMUXER
  1230. case AV_CODEC_ID_DVAUDIO:
  1231. c->dv_fctx = avformat_alloc_context();
  1232. c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
  1233. if (!c->dv_demux) {
  1234. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  1235. return AVERROR(ENOMEM);
  1236. }
  1237. sc->dv_audio_container = 1;
  1238. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  1239. break;
  1240. #endif
  1241. /* no ifdef since parameters are always those */
  1242. case AV_CODEC_ID_QCELP:
  1243. // force sample rate for qcelp when not stored in mov
  1244. if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
  1245. st->codec->sample_rate = 8000;
  1246. st->codec->channels= 1; /* really needed */
  1247. break;
  1248. case AV_CODEC_ID_AMR_NB:
  1249. st->codec->channels= 1; /* really needed */
  1250. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  1251. st->codec->sample_rate = 8000;
  1252. break;
  1253. case AV_CODEC_ID_AMR_WB:
  1254. st->codec->channels = 1;
  1255. st->codec->sample_rate = 16000;
  1256. break;
  1257. case AV_CODEC_ID_MP2:
  1258. case AV_CODEC_ID_MP3:
  1259. st->codec->codec_type = AVMEDIA_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  1260. st->need_parsing = AVSTREAM_PARSE_FULL;
  1261. break;
  1262. case AV_CODEC_ID_GSM:
  1263. case AV_CODEC_ID_ADPCM_MS:
  1264. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1265. case AV_CODEC_ID_ILBC:
  1266. st->codec->block_align = sc->bytes_per_frame;
  1267. break;
  1268. case AV_CODEC_ID_ALAC:
  1269. if (st->codec->extradata_size == 36) {
  1270. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  1271. st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
  1272. }
  1273. break;
  1274. case AV_CODEC_ID_AC3:
  1275. st->need_parsing = AVSTREAM_PARSE_FULL;
  1276. break;
  1277. case AV_CODEC_ID_MPEG1VIDEO:
  1278. st->need_parsing = AVSTREAM_PARSE_FULL;
  1279. break;
  1280. case AV_CODEC_ID_VC1:
  1281. st->need_parsing = AVSTREAM_PARSE_FULL;
  1282. break;
  1283. default:
  1284. break;
  1285. }
  1286. return 0;
  1287. }
  1288. static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1289. {
  1290. int entries;
  1291. avio_r8(pb); /* version */
  1292. avio_rb24(pb); /* flags */
  1293. entries = avio_rb32(pb);
  1294. return ff_mov_read_stsd_entries(c, pb, entries);
  1295. }
  1296. static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1297. {
  1298. AVStream *st;
  1299. MOVStreamContext *sc;
  1300. unsigned int i, entries;
  1301. if (c->fc->nb_streams < 1)
  1302. return 0;
  1303. st = c->fc->streams[c->fc->nb_streams-1];
  1304. sc = st->priv_data;
  1305. avio_r8(pb); /* version */
  1306. avio_rb24(pb); /* flags */
  1307. entries = avio_rb32(pb);
  1308. av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1309. if (!entries)
  1310. return 0;
  1311. if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
  1312. return AVERROR_INVALIDDATA;
  1313. sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
  1314. if (!sc->stsc_data)
  1315. return AVERROR(ENOMEM);
  1316. sc->stsc_count = entries;
  1317. for (i=0; i<entries; i++) {
  1318. sc->stsc_data[i].first = avio_rb32(pb);
  1319. sc->stsc_data[i].count = avio_rb32(pb);
  1320. sc->stsc_data[i].id = avio_rb32(pb);
  1321. }
  1322. return 0;
  1323. }
  1324. static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1325. {
  1326. AVStream *st;
  1327. MOVStreamContext *sc;
  1328. unsigned i, entries;
  1329. if (c->fc->nb_streams < 1)
  1330. return 0;
  1331. st = c->fc->streams[c->fc->nb_streams-1];
  1332. sc = st->priv_data;
  1333. avio_rb32(pb); // version + flags
  1334. entries = avio_rb32(pb);
  1335. if (entries >= UINT_MAX / sizeof(*sc->stps_data))
  1336. return AVERROR_INVALIDDATA;
  1337. sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
  1338. if (!sc->stps_data)
  1339. return AVERROR(ENOMEM);
  1340. sc->stps_count = entries;
  1341. for (i = 0; i < entries; i++) {
  1342. sc->stps_data[i] = avio_rb32(pb);
  1343. //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
  1344. }
  1345. return 0;
  1346. }
  1347. static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1348. {
  1349. AVStream *st;
  1350. MOVStreamContext *sc;
  1351. unsigned int i, entries;
  1352. if (c->fc->nb_streams < 1)
  1353. return 0;
  1354. st = c->fc->streams[c->fc->nb_streams-1];
  1355. sc = st->priv_data;
  1356. avio_r8(pb); /* version */
  1357. avio_rb24(pb); /* flags */
  1358. entries = avio_rb32(pb);
  1359. av_dlog(c->fc, "keyframe_count = %d\n", entries);
  1360. if (!entries)
  1361. {
  1362. sc->keyframe_absent = 1;
  1363. return 0;
  1364. }
  1365. if (entries >= UINT_MAX / sizeof(int))
  1366. return AVERROR_INVALIDDATA;
  1367. sc->keyframes = av_malloc(entries * sizeof(int));
  1368. if (!sc->keyframes)
  1369. return AVERROR(ENOMEM);
  1370. sc->keyframe_count = entries;
  1371. for (i=0; i<entries; i++) {
  1372. sc->keyframes[i] = avio_rb32(pb);
  1373. //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  1374. }
  1375. return 0;
  1376. }
  1377. static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1378. {
  1379. AVStream *st;
  1380. MOVStreamContext *sc;
  1381. unsigned int i, entries, sample_size, field_size, num_bytes;
  1382. GetBitContext gb;
  1383. unsigned char* buf;
  1384. if (c->fc->nb_streams < 1)
  1385. return 0;
  1386. st = c->fc->streams[c->fc->nb_streams-1];
  1387. sc = st->priv_data;
  1388. avio_r8(pb); /* version */
  1389. avio_rb24(pb); /* flags */
  1390. if (atom.type == MKTAG('s','t','s','z')) {
  1391. sample_size = avio_rb32(pb);
  1392. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1393. sc->sample_size = sample_size;
  1394. sc->alt_sample_size = sample_size;
  1395. field_size = 32;
  1396. } else {
  1397. sample_size = 0;
  1398. avio_rb24(pb); /* reserved */
  1399. field_size = avio_r8(pb);
  1400. }
  1401. entries = avio_rb32(pb);
  1402. av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1403. sc->sample_count = entries;
  1404. if (sample_size)
  1405. return 0;
  1406. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  1407. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
  1408. return AVERROR_INVALIDDATA;
  1409. }
  1410. if (!entries)
  1411. return 0;
  1412. if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
  1413. return AVERROR_INVALIDDATA;
  1414. sc->sample_sizes = av_malloc(entries * sizeof(int));
  1415. if (!sc->sample_sizes)
  1416. return AVERROR(ENOMEM);
  1417. num_bytes = (entries*field_size+4)>>3;
  1418. buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
  1419. if (!buf) {
  1420. av_freep(&sc->sample_sizes);
  1421. return AVERROR(ENOMEM);
  1422. }
  1423. if (avio_read(pb, buf, num_bytes) < num_bytes) {
  1424. av_freep(&sc->sample_sizes);
  1425. av_free(buf);
  1426. return AVERROR_INVALIDDATA;
  1427. }
  1428. init_get_bits(&gb, buf, 8*num_bytes);
  1429. for (i = 0; i < entries; i++) {
  1430. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  1431. sc->data_size += sc->sample_sizes[i];
  1432. }
  1433. av_free(buf);
  1434. return 0;
  1435. }
  1436. static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1437. {
  1438. AVStream *st;
  1439. MOVStreamContext *sc;
  1440. unsigned int i, entries;
  1441. int64_t duration=0;
  1442. int64_t total_sample_count=0;
  1443. if (c->fc->nb_streams < 1)
  1444. return 0;
  1445. st = c->fc->streams[c->fc->nb_streams-1];
  1446. sc = st->priv_data;
  1447. avio_r8(pb); /* version */
  1448. avio_rb24(pb); /* flags */
  1449. entries = avio_rb32(pb);
  1450. av_dlog(c->fc, "track[%i].stts.entries = %i\n",
  1451. c->fc->nb_streams-1, entries);
  1452. if (entries >= UINT_MAX / sizeof(*sc->stts_data))
  1453. return -1;
  1454. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1455. if (!sc->stts_data)
  1456. return AVERROR(ENOMEM);
  1457. sc->stts_count = entries;
  1458. for (i=0; i<entries; i++) {
  1459. int sample_duration;
  1460. int sample_count;
  1461. sample_count=avio_rb32(pb);
  1462. sample_duration = avio_rb32(pb);
  1463. /* sample_duration < 0 is invalid based on the spec */
  1464. if (sample_duration < 0) {
  1465. av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta in STTS %d\n", sample_duration);
  1466. sample_duration = 1;
  1467. }
  1468. sc->stts_data[i].count= sample_count;
  1469. sc->stts_data[i].duration= sample_duration;
  1470. av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
  1471. sample_count, sample_duration);
  1472. duration+=(int64_t)sample_duration*sample_count;
  1473. total_sample_count+=sample_count;
  1474. }
  1475. st->nb_frames= total_sample_count;
  1476. if (duration)
  1477. st->duration= duration;
  1478. sc->track_end = duration;
  1479. return 0;
  1480. }
  1481. static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1482. {
  1483. AVStream *st;
  1484. MOVStreamContext *sc;
  1485. unsigned int i, entries;
  1486. if (c->fc->nb_streams < 1)
  1487. return 0;
  1488. st = c->fc->streams[c->fc->nb_streams-1];
  1489. sc = st->priv_data;
  1490. avio_r8(pb); /* version */
  1491. avio_rb24(pb); /* flags */
  1492. entries = avio_rb32(pb);
  1493. av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1494. if (!entries)
  1495. return 0;
  1496. if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1497. return AVERROR_INVALIDDATA;
  1498. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1499. if (!sc->ctts_data)
  1500. return AVERROR(ENOMEM);
  1501. sc->ctts_count = entries;
  1502. for (i=0; i<entries; i++) {
  1503. int count =avio_rb32(pb);
  1504. int duration =avio_rb32(pb);
  1505. sc->ctts_data[i].count = count;
  1506. sc->ctts_data[i].duration= duration;
  1507. if (FFABS(duration) > (1<<28) && i+2<entries) {
  1508. av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
  1509. av_freep(&sc->ctts_data);
  1510. sc->ctts_count = 0;
  1511. return 0;
  1512. }
  1513. if (duration < 0 && i+2<entries)
  1514. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  1515. }
  1516. av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
  1517. return 0;
  1518. }
  1519. static void mov_build_index(MOVContext *mov, AVStream *st)
  1520. {
  1521. MOVStreamContext *sc = st->priv_data;
  1522. int64_t current_offset;
  1523. int64_t current_dts = 0;
  1524. unsigned int stts_index = 0;
  1525. unsigned int stsc_index = 0;
  1526. unsigned int stss_index = 0;
  1527. unsigned int stps_index = 0;
  1528. unsigned int i, j;
  1529. uint64_t stream_size = 0;
  1530. AVIndexEntry *mem;
  1531. /* adjust first dts according to edit list */
  1532. if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) {
  1533. if (sc->empty_duration)
  1534. sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale);
  1535. sc->time_offset = sc->start_time - sc->empty_duration;
  1536. current_dts = -sc->time_offset;
  1537. if (sc->ctts_count>0 && sc->stts_count>0 &&
  1538. sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
  1539. /* more than 16 frames delay, dts are likely wrong
  1540. this happens with files created by iMovie */
  1541. sc->wrong_dts = 1;
  1542. st->codec->has_b_frames = 1;
  1543. }
  1544. }
  1545. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1546. if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1547. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1548. unsigned int current_sample = 0;
  1549. unsigned int stts_sample = 0;
  1550. unsigned int sample_size;
  1551. unsigned int distance = 0;
  1552. int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_data && sc->stps_data[0] > 0);
  1553. current_dts -= sc->dts_shift;
  1554. if (!sc->sample_count || st->nb_index_entries)
  1555. return;
  1556. if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  1557. return;
  1558. mem = av_realloc(st->index_entries, (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries));
  1559. if (!mem)
  1560. return;
  1561. st->index_entries = mem;
  1562. st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
  1563. for (i = 0; i < sc->chunk_count; i++) {
  1564. current_offset = sc->chunk_offsets[i];
  1565. while (stsc_index + 1 < sc->stsc_count &&
  1566. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1567. stsc_index++;
  1568. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  1569. int keyframe = 0;
  1570. if (current_sample >= sc->sample_count) {
  1571. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1572. return;
  1573. }
  1574. if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
  1575. keyframe = 1;
  1576. if (stss_index + 1 < sc->keyframe_count)
  1577. stss_index++;
  1578. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  1579. keyframe = 1;
  1580. if (stps_index + 1 < sc->stps_count)
  1581. stps_index++;
  1582. }
  1583. if (keyframe)
  1584. distance = 0;
  1585. sample_size = sc->alt_sample_size > 0 ? sc->alt_sample_size : sc->sample_sizes[current_sample];
  1586. if (sc->pseudo_stream_id == -1 ||
  1587. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1588. AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
  1589. e->pos = current_offset;
  1590. e->timestamp = current_dts;
  1591. e->size = sample_size;
  1592. e->min_distance = distance;
  1593. e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
  1594. av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1595. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1596. current_offset, current_dts, sample_size, distance, keyframe);
  1597. }
  1598. current_offset += sample_size;
  1599. stream_size += sample_size;
  1600. current_dts += sc->stts_data[stts_index].duration;
  1601. distance++;
  1602. stts_sample++;
  1603. current_sample++;
  1604. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1605. stts_sample = 0;
  1606. stts_index++;
  1607. }
  1608. }
  1609. }
  1610. if (st->duration > 0)
  1611. st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
  1612. } else {
  1613. unsigned chunk_samples, total = 0;
  1614. // compute total chunk count
  1615. for (i = 0; i < sc->stsc_count; i++) {
  1616. unsigned count, chunk_count;
  1617. chunk_samples = sc->stsc_data[i].count;
  1618. if (i != sc->stsc_count - 1 &&
  1619. sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  1620. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  1621. return;
  1622. }
  1623. if (sc->samples_per_frame >= 160) { // gsm
  1624. count = chunk_samples / sc->samples_per_frame;
  1625. } else if (sc->samples_per_frame > 1) {
  1626. unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
  1627. count = (chunk_samples+samples-1) / samples;
  1628. } else {
  1629. count = (chunk_samples+1023) / 1024;
  1630. }
  1631. if (i < sc->stsc_count - 1)
  1632. chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
  1633. else
  1634. chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
  1635. total += chunk_count * count;
  1636. }
  1637. av_dlog(mov->fc, "chunk count %d\n", total);
  1638. if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  1639. return;
  1640. mem = av_realloc(st->index_entries, (st->nb_index_entries + total) * sizeof(*st->index_entries));
  1641. if (!mem)
  1642. return;
  1643. st->index_entries = mem;
  1644. st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
  1645. // populate index
  1646. for (i = 0; i < sc->chunk_count; i++) {
  1647. current_offset = sc->chunk_offsets[i];
  1648. if (stsc_index + 1 < sc->stsc_count &&
  1649. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1650. stsc_index++;
  1651. chunk_samples = sc->stsc_data[stsc_index].count;
  1652. while (chunk_samples > 0) {
  1653. AVIndexEntry *e;
  1654. unsigned size, samples;
  1655. if (sc->samples_per_frame >= 160) { // gsm
  1656. samples = sc->samples_per_frame;
  1657. size = sc->bytes_per_frame;
  1658. } else {
  1659. if (sc->samples_per_frame > 1) {
  1660. samples = FFMIN((1024 / sc->samples_per_frame)*
  1661. sc->samples_per_frame, chunk_samples);
  1662. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  1663. } else {
  1664. samples = FFMIN(1024, chunk_samples);
  1665. size = samples * sc->sample_size;
  1666. }
  1667. }
  1668. if (st->nb_index_entries >= total) {
  1669. av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
  1670. return;
  1671. }
  1672. e = &st->index_entries[st->nb_index_entries++];
  1673. e->pos = current_offset;
  1674. e->timestamp = current_dts;
  1675. e->size = size;
  1676. e->min_distance = 0;
  1677. e->flags = AVINDEX_KEYFRAME;
  1678. av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1679. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1680. size, samples);
  1681. current_offset += size;
  1682. current_dts += samples;
  1683. chunk_samples -= samples;
  1684. }
  1685. }
  1686. }
  1687. }
  1688. static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref,
  1689. AVIOInterruptCB *int_cb, int use_absolute_path, AVFormatContext *fc)
  1690. {
  1691. /* try relative path, we do not try the absolute because it can leak information about our
  1692. system to an attacker */
  1693. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  1694. char filename[1024];
  1695. const char *src_path;
  1696. int i, l;
  1697. /* find a source dir */
  1698. src_path = strrchr(src, '/');
  1699. if (src_path)
  1700. src_path++;
  1701. else
  1702. src_path = src;
  1703. /* find a next level down to target */
  1704. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  1705. if (ref->path[l] == '/') {
  1706. if (i == ref->nlvl_to - 1)
  1707. break;
  1708. else
  1709. i++;
  1710. }
  1711. /* compose filename if next level down to target was found */
  1712. if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
  1713. memcpy(filename, src, src_path - src);
  1714. filename[src_path - src] = 0;
  1715. for (i = 1; i < ref->nlvl_from; i++)
  1716. av_strlcat(filename, "../", 1024);
  1717. av_strlcat(filename, ref->path + l + 1, 1024);
  1718. if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
  1719. return 0;
  1720. }
  1721. } else if (use_absolute_path) {
  1722. av_log(fc, AV_LOG_WARNING, "Using absolute path on user request, "
  1723. "this is a possible security issue\n");
  1724. if (!avio_open2(pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
  1725. return 0;
  1726. }
  1727. return AVERROR(ENOENT);
  1728. }
  1729. static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1730. {
  1731. AVStream *st;
  1732. MOVStreamContext *sc;
  1733. int ret;
  1734. st = avformat_new_stream(c->fc, NULL);
  1735. if (!st) return AVERROR(ENOMEM);
  1736. st->id = c->fc->nb_streams;
  1737. sc = av_mallocz(sizeof(MOVStreamContext));
  1738. if (!sc) return AVERROR(ENOMEM);
  1739. st->priv_data = sc;
  1740. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1741. sc->ffindex = st->index;
  1742. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1743. return ret;
  1744. /* sanity checks */
  1745. if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  1746. (!sc->sample_size && !sc->sample_count))) {
  1747. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1748. st->index);
  1749. return 0;
  1750. }
  1751. if (sc->time_scale <= 0) {
  1752. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
  1753. sc->time_scale = c->time_scale;
  1754. if (sc->time_scale <= 0)
  1755. sc->time_scale = 1;
  1756. }
  1757. avpriv_set_pts_info(st, 64, 1, sc->time_scale);
  1758. mov_build_index(c, st);
  1759. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1760. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  1761. if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback,
  1762. c->use_absolute_path, c->fc) < 0)
  1763. av_log(c->fc, AV_LOG_ERROR,
  1764. "stream %d, error opening alias: path='%s', dir='%s', "
  1765. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  1766. st->index, dref->path, dref->dir, dref->filename,
  1767. dref->volume, dref->nlvl_from, dref->nlvl_to);
  1768. } else
  1769. sc->pb = c->fc->pb;
  1770. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  1771. if (!st->sample_aspect_ratio.num &&
  1772. (st->codec->width != sc->width || st->codec->height != sc->height)) {
  1773. st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
  1774. ((double)st->codec->width * sc->height), INT_MAX);
  1775. }
  1776. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  1777. sc->time_scale*st->nb_frames, st->duration, INT_MAX);
  1778. #if FF_API_R_FRAME_RATE
  1779. if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
  1780. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  1781. sc->time_scale, sc->stts_data[0].duration, INT_MAX);
  1782. #endif
  1783. }
  1784. switch (st->codec->codec_id) {
  1785. #if CONFIG_H261_DECODER
  1786. case AV_CODEC_ID_H261:
  1787. #endif
  1788. #if CONFIG_H263_DECODER
  1789. case AV_CODEC_ID_H263:
  1790. #endif
  1791. #if CONFIG_MPEG4_DECODER
  1792. case AV_CODEC_ID_MPEG4:
  1793. #endif
  1794. st->codec->width = 0; /* let decoder init width/height */
  1795. st->codec->height= 0;
  1796. break;
  1797. }
  1798. /* Do not need those anymore. */
  1799. av_freep(&sc->chunk_offsets);
  1800. av_freep(&sc->stsc_data);
  1801. av_freep(&sc->sample_sizes);
  1802. av_freep(&sc->keyframes);
  1803. av_freep(&sc->stts_data);
  1804. av_freep(&sc->stps_data);
  1805. return 0;
  1806. }
  1807. static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1808. {
  1809. int ret;
  1810. c->itunes_metadata = 1;
  1811. ret = mov_read_default(c, pb, atom);
  1812. c->itunes_metadata = 0;
  1813. return ret;
  1814. }
  1815. static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1816. {
  1817. while (atom.size > 8) {
  1818. uint32_t tag = avio_rl32(pb);
  1819. atom.size -= 4;
  1820. if (tag == MKTAG('h','d','l','r')) {
  1821. avio_seek(pb, -8, SEEK_CUR);
  1822. atom.size += 8;
  1823. return mov_read_default(c, pb, atom);
  1824. }
  1825. }
  1826. return 0;
  1827. }
  1828. static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1829. {
  1830. int i;
  1831. int width;
  1832. int height;
  1833. int64_t disp_transform[2];
  1834. int display_matrix[3][2];
  1835. AVStream *st;
  1836. MOVStreamContext *sc;
  1837. int version;
  1838. if (c->fc->nb_streams < 1)
  1839. return 0;
  1840. st = c->fc->streams[c->fc->nb_streams-1];
  1841. sc = st->priv_data;
  1842. version = avio_r8(pb);
  1843. avio_rb24(pb); /* flags */
  1844. /*
  1845. MOV_TRACK_ENABLED 0x0001
  1846. MOV_TRACK_IN_MOVIE 0x0002
  1847. MOV_TRACK_IN_PREVIEW 0x0004
  1848. MOV_TRACK_IN_POSTER 0x0008
  1849. */
  1850. if (version == 1) {
  1851. avio_rb64(pb);
  1852. avio_rb64(pb);
  1853. } else {
  1854. avio_rb32(pb); /* creation time */
  1855. avio_rb32(pb); /* modification time */
  1856. }
  1857. st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
  1858. avio_rb32(pb); /* reserved */
  1859. /* highlevel (considering edits) duration in movie timebase */
  1860. (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
  1861. avio_rb32(pb); /* reserved */
  1862. avio_rb32(pb); /* reserved */
  1863. avio_rb16(pb); /* layer */
  1864. avio_rb16(pb); /* alternate group */
  1865. avio_rb16(pb); /* volume */
  1866. avio_rb16(pb); /* reserved */
  1867. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1868. // they're kept in fixed point format through all calculations
  1869. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1870. for (i = 0; i < 3; i++) {
  1871. display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  1872. display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  1873. avio_rb32(pb); // 2.30 fixed point (not used)
  1874. }
  1875. width = avio_rb32(pb); // 16.16 fixed point track width
  1876. height = avio_rb32(pb); // 16.16 fixed point track height
  1877. sc->width = width >> 16;
  1878. sc->height = height >> 16;
  1879. //Assign clockwise rotate values based on transform matrix so that
  1880. //we can compensate for iPhone orientation during capture.
  1881. if (display_matrix[1][0] == -65536 && display_matrix[0][1] == 65536) {
  1882. av_dict_set(&st->metadata, "rotate", "90", 0);
  1883. }
  1884. if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) {
  1885. av_dict_set(&st->metadata, "rotate", "180", 0);
  1886. }
  1887. if (display_matrix[1][0] == 65536 && display_matrix[0][1] == -65536) {
  1888. av_dict_set(&st->metadata, "rotate", "270", 0);
  1889. }
  1890. // transform the display width/height according to the matrix
  1891. // skip this if the display matrix is the default identity matrix
  1892. // or if it is rotating the picture, ex iPhone 3GS
  1893. // to keep the same scale, use [width height 1<<16]
  1894. if (width && height &&
  1895. ((display_matrix[0][0] != 65536 ||
  1896. display_matrix[1][1] != 65536) &&
  1897. !display_matrix[0][1] &&
  1898. !display_matrix[1][0] &&
  1899. !display_matrix[2][0] && !display_matrix[2][1])) {
  1900. for (i = 0; i < 2; i++)
  1901. disp_transform[i] =
  1902. (int64_t) width * display_matrix[0][i] +
  1903. (int64_t) height * display_matrix[1][i] +
  1904. ((int64_t) display_matrix[2][i] << 16);
  1905. //sample aspect ratio is new width/height divided by old width/height
  1906. st->sample_aspect_ratio = av_d2q(
  1907. ((double) disp_transform[0] * height) /
  1908. ((double) disp_transform[1] * width), INT_MAX);
  1909. }
  1910. return 0;
  1911. }
  1912. static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1913. {
  1914. MOVFragment *frag = &c->fragment;
  1915. MOVTrackExt *trex = NULL;
  1916. int flags, track_id, i;
  1917. avio_r8(pb); /* version */
  1918. flags = avio_rb24(pb);
  1919. track_id = avio_rb32(pb);
  1920. if (!track_id)
  1921. return AVERROR_INVALIDDATA;
  1922. frag->track_id = track_id;
  1923. for (i = 0; i < c->trex_count; i++)
  1924. if (c->trex_data[i].track_id == frag->track_id) {
  1925. trex = &c->trex_data[i];
  1926. break;
  1927. }
  1928. if (!trex) {
  1929. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1930. return AVERROR_INVALIDDATA;
  1931. }
  1932. frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
  1933. avio_rb64(pb) : frag->moof_offset;
  1934. frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
  1935. frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
  1936. avio_rb32(pb) : trex->duration;
  1937. frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
  1938. avio_rb32(pb) : trex->size;
  1939. frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
  1940. avio_rb32(pb) : trex->flags;
  1941. av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
  1942. return 0;
  1943. }
  1944. static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1945. {
  1946. c->chapter_track = avio_rb32(pb);
  1947. return 0;
  1948. }
  1949. static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1950. {
  1951. MOVTrackExt *trex;
  1952. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1953. return AVERROR_INVALIDDATA;
  1954. trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1955. if (!trex)
  1956. return AVERROR(ENOMEM);
  1957. c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
  1958. c->trex_data = trex;
  1959. trex = &c->trex_data[c->trex_count++];
  1960. avio_r8(pb); /* version */
  1961. avio_rb24(pb); /* flags */
  1962. trex->track_id = avio_rb32(pb);
  1963. trex->stsd_id = avio_rb32(pb);
  1964. trex->duration = avio_rb32(pb);
  1965. trex->size = avio_rb32(pb);
  1966. trex->flags = avio_rb32(pb);
  1967. return 0;
  1968. }
  1969. static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1970. {
  1971. MOVFragment *frag = &c->fragment;
  1972. AVStream *st = NULL;
  1973. MOVStreamContext *sc;
  1974. MOVStts *ctts_data;
  1975. uint64_t offset;
  1976. int64_t dts;
  1977. int data_offset = 0;
  1978. unsigned entries, first_sample_flags = frag->flags;
  1979. int flags, distance, i, found_keyframe = 0;
  1980. for (i = 0; i < c->fc->nb_streams; i++) {
  1981. if (c->fc->streams[i]->id == frag->track_id) {
  1982. st = c->fc->streams[i];
  1983. break;
  1984. }
  1985. }
  1986. if (!st) {
  1987. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  1988. return AVERROR_INVALIDDATA;
  1989. }
  1990. sc = st->priv_data;
  1991. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1992. return 0;
  1993. avio_r8(pb); /* version */
  1994. flags = avio_rb24(pb);
  1995. entries = avio_rb32(pb);
  1996. av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1997. /* Always assume the presence of composition time offsets.
  1998. * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
  1999. * 1) in the initial movie, there are no samples.
  2000. * 2) in the first movie fragment, there is only one sample without composition time offset.
  2001. * 3) in the subsequent movie fragments, there are samples with composition time offset. */
  2002. if (!sc->ctts_count && sc->sample_count)
  2003. {
  2004. /* Complement ctts table if moov atom doesn't have ctts atom. */
  2005. ctts_data = av_malloc(sizeof(*sc->ctts_data));
  2006. if (!ctts_data)
  2007. return AVERROR(ENOMEM);
  2008. sc->ctts_data = ctts_data;
  2009. sc->ctts_data[sc->ctts_count].count = sc->sample_count;
  2010. sc->ctts_data[sc->ctts_count].duration = 0;
  2011. sc->ctts_count++;
  2012. }
  2013. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  2014. return AVERROR_INVALIDDATA;
  2015. ctts_data = av_realloc(sc->ctts_data,
  2016. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  2017. if (!ctts_data)
  2018. return AVERROR(ENOMEM);
  2019. sc->ctts_data = ctts_data;
  2020. if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
  2021. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
  2022. dts = sc->track_end - sc->time_offset;
  2023. offset = frag->base_data_offset + data_offset;
  2024. distance = 0;
  2025. av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  2026. for (i = 0; i < entries; i++) {
  2027. unsigned sample_size = frag->size;
  2028. int sample_flags = i ? frag->flags : first_sample_flags;
  2029. unsigned sample_duration = frag->duration;
  2030. int keyframe = 0;
  2031. if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
  2032. if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
  2033. if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
  2034. sc->ctts_data[sc->ctts_count].count = 1;
  2035. sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
  2036. avio_rb32(pb) : 0;
  2037. sc->ctts_count++;
  2038. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  2039. keyframe = 1;
  2040. else if (!found_keyframe)
  2041. keyframe = found_keyframe =
  2042. !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
  2043. MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
  2044. if (keyframe)
  2045. distance = 0;
  2046. av_add_index_entry(st, offset, dts, sample_size, distance,
  2047. keyframe ? AVINDEX_KEYFRAME : 0);
  2048. av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  2049. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  2050. offset, dts, sample_size, distance, keyframe);
  2051. distance++;
  2052. dts += sample_duration;
  2053. offset += sample_size;
  2054. sc->data_size += sample_size;
  2055. }
  2056. frag->moof_offset = offset;
  2057. st->duration = sc->track_end = dts + sc->time_offset;
  2058. return 0;
  2059. }
  2060. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  2061. /* like the files created with Adobe Premiere 5.0, for samples see */
  2062. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  2063. static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2064. {
  2065. int err;
  2066. if (atom.size < 8)
  2067. return 0; /* continue */
  2068. if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  2069. avio_skip(pb, atom.size - 4);
  2070. return 0;
  2071. }
  2072. atom.type = avio_rl32(pb);
  2073. atom.size -= 8;
  2074. if (atom.type != MKTAG('m','d','a','t')) {
  2075. avio_skip(pb, atom.size);
  2076. return 0;
  2077. }
  2078. err = mov_read_mdat(c, pb, atom);
  2079. return err;
  2080. }
  2081. static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2082. {
  2083. #if CONFIG_ZLIB
  2084. AVIOContext ctx;
  2085. uint8_t *cmov_data;
  2086. uint8_t *moov_data; /* uncompressed data */
  2087. long cmov_len, moov_len;
  2088. int ret = -1;
  2089. avio_rb32(pb); /* dcom atom */
  2090. if (avio_rl32(pb) != MKTAG('d','c','o','m'))
  2091. return AVERROR_INVALIDDATA;
  2092. if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
  2093. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
  2094. return AVERROR_INVALIDDATA;
  2095. }
  2096. avio_rb32(pb); /* cmvd atom */
  2097. if (avio_rl32(pb) != MKTAG('c','m','v','d'))
  2098. return AVERROR_INVALIDDATA;
  2099. moov_len = avio_rb32(pb); /* uncompressed size */
  2100. cmov_len = atom.size - 6 * 4;
  2101. cmov_data = av_malloc(cmov_len);
  2102. if (!cmov_data)
  2103. return AVERROR(ENOMEM);
  2104. moov_data = av_malloc(moov_len);
  2105. if (!moov_data) {
  2106. av_free(cmov_data);
  2107. return AVERROR(ENOMEM);
  2108. }
  2109. avio_read(pb, cmov_data, cmov_len);
  2110. if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  2111. goto free_and_return;
  2112. if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  2113. goto free_and_return;
  2114. atom.type = MKTAG('m','o','o','v');
  2115. atom.size = moov_len;
  2116. ret = mov_read_default(c, &ctx, atom);
  2117. free_and_return:
  2118. av_free(moov_data);
  2119. av_free(cmov_data);
  2120. return ret;
  2121. #else
  2122. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  2123. return AVERROR(ENOSYS);
  2124. #endif
  2125. }
  2126. /* edit list atom */
  2127. static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2128. {
  2129. MOVStreamContext *sc;
  2130. int i, edit_count, version, edit_start_index = 0;
  2131. if (c->fc->nb_streams < 1)
  2132. return 0;
  2133. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  2134. version = avio_r8(pb); /* version */
  2135. avio_rb24(pb); /* flags */
  2136. edit_count = avio_rb32(pb); /* entries */
  2137. if ((uint64_t)edit_count*12+8 > atom.size)
  2138. return AVERROR_INVALIDDATA;
  2139. for (i=0; i<edit_count; i++){
  2140. int64_t time;
  2141. int64_t duration;
  2142. if (version == 1) {
  2143. duration = avio_rb64(pb);
  2144. time = avio_rb64(pb);
  2145. } else {
  2146. duration = avio_rb32(pb); /* segment duration */
  2147. time = (int32_t)avio_rb32(pb); /* media time */
  2148. }
  2149. avio_rb32(pb); /* Media rate */
  2150. if (i == 0 && time == -1) {
  2151. sc->empty_duration = duration;
  2152. edit_start_index = 1;
  2153. } else if (i == edit_start_index && time >= 0)
  2154. sc->start_time = time;
  2155. }
  2156. if (edit_count > 1)
  2157. av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
  2158. "a/v desync might occur, patch welcome\n");
  2159. av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  2160. return 0;
  2161. }
  2162. static int mov_read_chan2(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2163. {
  2164. if (atom.size < 16)
  2165. return 0;
  2166. avio_skip(pb, 4);
  2167. ff_mov_read_chan(c->fc,c->fc->streams[0], atom.size - 4);
  2168. return 0;
  2169. }
  2170. static int mov_read_tref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2171. {
  2172. uint32_t i, size;
  2173. MOVStreamContext *sc;
  2174. if (c->fc->nb_streams < 1)
  2175. return AVERROR_INVALIDDATA;
  2176. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  2177. size = avio_rb32(pb);
  2178. if (size < 12)
  2179. return 0;
  2180. sc->trefs_count = (size - 4) / 8;
  2181. sc->trefs = av_malloc(sc->trefs_count * sizeof(*sc->trefs));
  2182. if (!sc->trefs)
  2183. return AVERROR(ENOMEM);
  2184. sc->tref_type = avio_rl32(pb);
  2185. for (i = 0; i < sc->trefs_count; i++)
  2186. sc->trefs[i] = avio_rb32(pb);
  2187. return 0;
  2188. }
  2189. static const MOVParseTableEntry mov_default_parse_table[] = {
  2190. { MKTAG('A','C','L','R'), mov_read_avid },
  2191. { MKTAG('A','P','R','G'), mov_read_avid },
  2192. { MKTAG('A','R','E','S'), mov_read_avid },
  2193. { MKTAG('a','v','s','s'), mov_read_avss },
  2194. { MKTAG('c','h','p','l'), mov_read_chpl },
  2195. { MKTAG('c','o','6','4'), mov_read_stco },
  2196. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  2197. { MKTAG('d','i','n','f'), mov_read_default },
  2198. { MKTAG('d','r','e','f'), mov_read_dref },
  2199. { MKTAG('e','d','t','s'), mov_read_default },
  2200. { MKTAG('e','l','s','t'), mov_read_elst },
  2201. { MKTAG('e','n','d','a'), mov_read_enda },
  2202. { MKTAG('f','i','e','l'), mov_read_fiel },
  2203. { MKTAG('f','t','y','p'), mov_read_ftyp },
  2204. { MKTAG('g','l','b','l'), mov_read_glbl },
  2205. { MKTAG('h','d','l','r'), mov_read_hdlr },
  2206. { MKTAG('i','l','s','t'), mov_read_ilst },
  2207. { MKTAG('j','p','2','h'), mov_read_jp2h },
  2208. { MKTAG('m','d','a','t'), mov_read_mdat },
  2209. { MKTAG('m','d','h','d'), mov_read_mdhd },
  2210. { MKTAG('m','d','i','a'), mov_read_default },
  2211. { MKTAG('m','e','t','a'), mov_read_meta },
  2212. { MKTAG('m','i','n','f'), mov_read_default },
  2213. { MKTAG('m','o','o','f'), mov_read_moof },
  2214. { MKTAG('m','o','o','v'), mov_read_moov },
  2215. { MKTAG('m','v','e','x'), mov_read_default },
  2216. { MKTAG('m','v','h','d'), mov_read_mvhd },
  2217. { MKTAG('S','M','I',' '), mov_read_svq3 },
  2218. { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
  2219. { MKTAG('a','v','c','C'), mov_read_glbl },
  2220. { MKTAG('p','a','s','p'), mov_read_pasp },
  2221. { MKTAG('s','t','b','l'), mov_read_default },
  2222. { MKTAG('s','t','c','o'), mov_read_stco },
  2223. { MKTAG('s','t','p','s'), mov_read_stps },
  2224. { MKTAG('s','t','r','f'), mov_read_strf },
  2225. { MKTAG('s','t','s','c'), mov_read_stsc },
  2226. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  2227. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  2228. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  2229. { MKTAG('s','t','t','s'), mov_read_stts },
  2230. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  2231. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  2232. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  2233. { MKTAG('t','r','a','k'), mov_read_trak },
  2234. { MKTAG('t','r','a','f'), mov_read_default },
  2235. { MKTAG('t','r','e','f'), mov_read_tref },
  2236. { MKTAG('c','h','a','p'), mov_read_chap },
  2237. { MKTAG('t','r','e','x'), mov_read_trex },
  2238. { MKTAG('t','r','u','n'), mov_read_trun },
  2239. { MKTAG('u','d','t','a'), mov_read_default },
  2240. { MKTAG('w','a','v','e'), mov_read_wave },
  2241. { MKTAG('e','s','d','s'), mov_read_esds },
  2242. { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
  2243. { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
  2244. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  2245. { MKTAG('w','f','e','x'), mov_read_wfex },
  2246. { MKTAG('c','m','o','v'), mov_read_cmov },
  2247. { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
  2248. { MKTAG('d','v','c','1'), mov_read_dvc1 },
  2249. { 0, NULL }
  2250. };
  2251. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2252. {
  2253. int64_t total_size = 0;
  2254. MOVAtom a;
  2255. int i;
  2256. if (atom.size < 0)
  2257. atom.size = INT64_MAX;
  2258. while (total_size + 8 <= atom.size && !url_feof(pb)) {
  2259. int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
  2260. a.size = atom.size;
  2261. a.type=0;
  2262. if (atom.size >= 8) {
  2263. a.size = avio_rb32(pb);
  2264. a.type = avio_rl32(pb);
  2265. if (atom.type != MKTAG('r','o','o','t') &&
  2266. atom.type != MKTAG('m','o','o','v'))
  2267. {
  2268. if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
  2269. {
  2270. av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
  2271. avio_skip(pb, -8);
  2272. return 0;
  2273. }
  2274. }
  2275. total_size += 8;
  2276. if (a.size == 1) { /* 64 bit extended size */
  2277. a.size = avio_rb64(pb) - 8;
  2278. total_size += 8;
  2279. }
  2280. }
  2281. av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
  2282. a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
  2283. if (a.size == 0) {
  2284. a.size = atom.size - total_size + 8;
  2285. }
  2286. a.size -= 8;
  2287. if (a.size < 0)
  2288. break;
  2289. a.size = FFMIN(a.size, atom.size - total_size);
  2290. for (i = 0; mov_default_parse_table[i].type; i++)
  2291. if (mov_default_parse_table[i].type == a.type) {
  2292. parse = mov_default_parse_table[i].parse;
  2293. break;
  2294. }
  2295. // container is user data
  2296. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  2297. atom.type == MKTAG('i','l','s','t')))
  2298. parse = mov_read_udta_string;
  2299. if (!parse) { /* skip leaf atoms data */
  2300. avio_skip(pb, a.size);
  2301. } else {
  2302. int64_t start_pos = avio_tell(pb);
  2303. int64_t left;
  2304. int err = parse(c, pb, a);
  2305. if (err < 0)
  2306. return err;
  2307. if (c->found_moov && c->found_mdat &&
  2308. ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
  2309. start_pos + a.size == avio_size(pb))) {
  2310. if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
  2311. c->next_root_atom = start_pos + a.size;
  2312. return 0;
  2313. }
  2314. left = a.size - avio_tell(pb) + start_pos;
  2315. if (left > 0) /* skip garbage at atom end */
  2316. avio_skip(pb, left);
  2317. }
  2318. total_size += a.size;
  2319. }
  2320. if (total_size < atom.size && atom.size < 0x7ffff)
  2321. avio_skip(pb, atom.size - total_size);
  2322. return 0;
  2323. }
  2324. static int mov_probe(AVProbeData *p)
  2325. {
  2326. unsigned int offset;
  2327. uint32_t tag;
  2328. int score = 0;
  2329. /* check file header */
  2330. offset = 0;
  2331. for (;;) {
  2332. /* ignore invalid offset */
  2333. if ((offset + 8) > (unsigned int)p->buf_size)
  2334. return score;
  2335. tag = AV_RL32(p->buf + offset + 4);
  2336. switch(tag) {
  2337. /* check for obvious tags */
  2338. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  2339. case MKTAG('m','o','o','v'):
  2340. case MKTAG('m','d','a','t'):
  2341. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  2342. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  2343. case MKTAG('f','t','y','p'):
  2344. return AVPROBE_SCORE_MAX;
  2345. /* those are more common words, so rate then a bit less */
  2346. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  2347. case MKTAG('w','i','d','e'):
  2348. case MKTAG('f','r','e','e'):
  2349. case MKTAG('j','u','n','k'):
  2350. case MKTAG('p','i','c','t'):
  2351. return AVPROBE_SCORE_MAX - 5;
  2352. case MKTAG(0x82,0x82,0x7f,0x7d):
  2353. case MKTAG('s','k','i','p'):
  2354. case MKTAG('u','u','i','d'):
  2355. case MKTAG('p','r','f','l'):
  2356. offset = AV_RB32(p->buf+offset) + offset;
  2357. /* if we only find those cause probedata is too small at least rate them */
  2358. score = AVPROBE_SCORE_MAX - 50;
  2359. break;
  2360. default:
  2361. /* unrecognized tag */
  2362. return score;
  2363. }
  2364. }
  2365. }
  2366. // must be done after parsing all trak because there's no order requirement
  2367. static void mov_read_chapters(AVFormatContext *s)
  2368. {
  2369. MOVContext *mov = s->priv_data;
  2370. AVStream *st = NULL;
  2371. MOVStreamContext *sc;
  2372. int64_t cur_pos;
  2373. int i;
  2374. for (i = 0; i < s->nb_streams; i++)
  2375. if (s->streams[i]->id == mov->chapter_track) {
  2376. st = s->streams[i];
  2377. break;
  2378. }
  2379. if (!st) {
  2380. av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
  2381. return;
  2382. }
  2383. st->discard = AVDISCARD_ALL;
  2384. sc = st->priv_data;
  2385. cur_pos = avio_tell(sc->pb);
  2386. for (i = 0; i < st->nb_index_entries; i++) {
  2387. AVIndexEntry *sample = &st->index_entries[i];
  2388. int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
  2389. uint8_t *title;
  2390. uint16_t ch;
  2391. int len, title_len;
  2392. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2393. av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
  2394. goto finish;
  2395. }
  2396. // the first two bytes are the length of the title
  2397. len = avio_rb16(sc->pb);
  2398. if (len > sample->size-2)
  2399. continue;
  2400. title_len = 2*len + 1;
  2401. if (!(title = av_mallocz(title_len)))
  2402. goto finish;
  2403. // The samples could theoretically be in any encoding if there's an encd
  2404. // atom following, but in practice are only utf-8 or utf-16, distinguished
  2405. // instead by the presence of a BOM
  2406. if (!len) {
  2407. title[0] = 0;
  2408. } else {
  2409. ch = avio_rb16(sc->pb);
  2410. if (ch == 0xfeff)
  2411. avio_get_str16be(sc->pb, len, title, title_len);
  2412. else if (ch == 0xfffe)
  2413. avio_get_str16le(sc->pb, len, title, title_len);
  2414. else {
  2415. AV_WB16(title, ch);
  2416. if (len == 1 || len == 2)
  2417. title[len] = 0;
  2418. else
  2419. avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
  2420. }
  2421. }
  2422. avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
  2423. av_freep(&title);
  2424. }
  2425. finish:
  2426. avio_seek(sc->pb, cur_pos, SEEK_SET);
  2427. }
  2428. static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
  2429. uint32_t value, int flags)
  2430. {
  2431. AVTimecode tc;
  2432. char buf[AV_TIMECODE_STR_SIZE];
  2433. AVRational rate = {st->codec->time_base.den,
  2434. st->codec->time_base.num};
  2435. int ret = av_timecode_init(&tc, rate, flags, 0, s);
  2436. if (ret < 0)
  2437. return ret;
  2438. av_dict_set(&st->metadata, "timecode",
  2439. av_timecode_make_string(&tc, buf, value), 0);
  2440. return 0;
  2441. }
  2442. static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
  2443. {
  2444. MOVStreamContext *sc = st->priv_data;
  2445. int flags = 0;
  2446. int64_t cur_pos = avio_tell(sc->pb);
  2447. uint32_t value;
  2448. if (!st->nb_index_entries)
  2449. return -1;
  2450. avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
  2451. value = avio_rb32(s->pb);
  2452. if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
  2453. if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
  2454. if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
  2455. /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
  2456. * not the case) and thus assume "frame number format" instead of QT one.
  2457. * No sample with tmcd track can be found with a QT timecode at the moment,
  2458. * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
  2459. * format). */
  2460. parse_timecode_in_framenum_format(s, st, value, flags);
  2461. avio_seek(sc->pb, cur_pos, SEEK_SET);
  2462. return 0;
  2463. }
  2464. static int mov_read_close(AVFormatContext *s)
  2465. {
  2466. MOVContext *mov = s->priv_data;
  2467. int i, j;
  2468. for (i = 0; i < s->nb_streams; i++) {
  2469. AVStream *st = s->streams[i];
  2470. MOVStreamContext *sc = st->priv_data;
  2471. av_freep(&sc->ctts_data);
  2472. for (j = 0; j < sc->drefs_count; j++) {
  2473. av_freep(&sc->drefs[j].path);
  2474. av_freep(&sc->drefs[j].dir);
  2475. }
  2476. av_freep(&sc->drefs);
  2477. av_freep(&sc->trefs);
  2478. if (sc->pb && sc->pb != s->pb)
  2479. avio_close(sc->pb);
  2480. sc->pb = NULL;
  2481. av_freep(&sc->chunk_offsets);
  2482. av_freep(&sc->keyframes);
  2483. av_freep(&sc->sample_sizes);
  2484. av_freep(&sc->stps_data);
  2485. av_freep(&sc->stsc_data);
  2486. av_freep(&sc->stts_data);
  2487. }
  2488. if (mov->dv_demux) {
  2489. for (i = 0; i < mov->dv_fctx->nb_streams; i++) {
  2490. av_freep(&mov->dv_fctx->streams[i]->codec);
  2491. av_freep(&mov->dv_fctx->streams[i]);
  2492. }
  2493. av_freep(&mov->dv_fctx);
  2494. av_freep(&mov->dv_demux);
  2495. }
  2496. av_freep(&mov->trex_data);
  2497. return 0;
  2498. }
  2499. static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
  2500. {
  2501. int i, j;
  2502. for (i = 0; i < s->nb_streams; i++) {
  2503. AVStream *st = s->streams[i];
  2504. MOVStreamContext *sc = st->priv_data;
  2505. if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
  2506. continue;
  2507. for (j = 0; j < sc->trefs_count; j++)
  2508. if (tmcd_id == sc->trefs[j])
  2509. return 1;
  2510. }
  2511. return 0;
  2512. }
  2513. /* look for a tmcd track not referenced by any video track, and export it globally */
  2514. static void export_orphan_timecode(AVFormatContext *s)
  2515. {
  2516. int i;
  2517. for (i = 0; i < s->nb_streams; i++) {
  2518. AVStream *st = s->streams[i];
  2519. if (st->codec->codec_tag == MKTAG('t','m','c','d') &&
  2520. !tmcd_is_referenced(s, i + 1)) {
  2521. AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
  2522. if (tcr) {
  2523. av_dict_set(&s->metadata, "timecode", tcr->value, 0);
  2524. break;
  2525. }
  2526. }
  2527. }
  2528. }
  2529. static int mov_read_header(AVFormatContext *s)
  2530. {
  2531. MOVContext *mov = s->priv_data;
  2532. AVIOContext *pb = s->pb;
  2533. int i, err;
  2534. MOVAtom atom = { AV_RL32("root") };
  2535. mov->fc = s;
  2536. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  2537. if (pb->seekable)
  2538. atom.size = avio_size(pb);
  2539. else
  2540. atom.size = INT64_MAX;
  2541. /* check MOV header */
  2542. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  2543. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  2544. mov_read_close(s);
  2545. return err;
  2546. }
  2547. if (!mov->found_moov) {
  2548. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  2549. mov_read_close(s);
  2550. return AVERROR_INVALIDDATA;
  2551. }
  2552. av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
  2553. if (pb->seekable) {
  2554. if (mov->chapter_track > 0)
  2555. mov_read_chapters(s);
  2556. for (i = 0; i < s->nb_streams; i++)
  2557. if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
  2558. mov_read_timecode_track(s, s->streams[i]);
  2559. }
  2560. /* copy timecode metadata from tmcd tracks to the related video streams */
  2561. for (i = 0; i < s->nb_streams; i++) {
  2562. AVStream *st = s->streams[i];
  2563. MOVStreamContext *sc = st->priv_data;
  2564. if (sc->tref_type == AV_RL32("tmcd") && sc->trefs_count) {
  2565. AVDictionaryEntry *tcr;
  2566. int tmcd_st_id = sc->trefs[0] - 1;
  2567. if (tmcd_st_id < 0 || tmcd_st_id >= s->nb_streams)
  2568. continue;
  2569. tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
  2570. if (tcr)
  2571. av_dict_set(&st->metadata, "timecode", tcr->value, 0);
  2572. }
  2573. }
  2574. export_orphan_timecode(s);
  2575. for (i = 0; i < s->nb_streams; i++) {
  2576. AVStream *st = s->streams[i];
  2577. MOVStreamContext *sc = st->priv_data;
  2578. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->codec_id == AV_CODEC_ID_AAC) {
  2579. sc->start_pad = 2112;
  2580. st->skip_samples = sc->start_pad;
  2581. }
  2582. }
  2583. if (mov->trex_data) {
  2584. for (i = 0; i < s->nb_streams; i++) {
  2585. AVStream *st = s->streams[i];
  2586. MOVStreamContext *sc = st->priv_data;
  2587. if (st->duration)
  2588. st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
  2589. }
  2590. }
  2591. return 0;
  2592. }
  2593. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  2594. {
  2595. AVIndexEntry *sample = NULL;
  2596. int64_t best_dts = INT64_MAX;
  2597. int i;
  2598. for (i = 0; i < s->nb_streams; i++) {
  2599. AVStream *avst = s->streams[i];
  2600. MOVStreamContext *msc = avst->priv_data;
  2601. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  2602. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  2603. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  2604. av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  2605. if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
  2606. (s->pb->seekable &&
  2607. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  2608. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  2609. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  2610. sample = current_sample;
  2611. best_dts = dts;
  2612. *st = avst;
  2613. }
  2614. }
  2615. }
  2616. return sample;
  2617. }
  2618. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  2619. {
  2620. MOVContext *mov = s->priv_data;
  2621. MOVStreamContext *sc;
  2622. AVIndexEntry *sample;
  2623. AVStream *st = NULL;
  2624. int ret;
  2625. mov->fc = s;
  2626. retry:
  2627. sample = mov_find_next_sample(s, &st);
  2628. if (!sample) {
  2629. mov->found_mdat = 0;
  2630. if (!mov->next_root_atom)
  2631. return AVERROR_EOF;
  2632. avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
  2633. mov->next_root_atom = 0;
  2634. if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
  2635. url_feof(s->pb))
  2636. return AVERROR_EOF;
  2637. av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
  2638. goto retry;
  2639. }
  2640. sc = st->priv_data;
  2641. /* must be done just before reading, to avoid infinite loop on sample */
  2642. sc->current_sample++;
  2643. if (st->discard != AVDISCARD_ALL) {
  2644. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2645. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  2646. sc->ffindex, sample->pos);
  2647. return AVERROR_INVALIDDATA;
  2648. }
  2649. ret = av_get_packet(sc->pb, pkt, sample->size);
  2650. if (ret < 0)
  2651. return ret;
  2652. if (sc->has_palette) {
  2653. uint8_t *pal;
  2654. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  2655. if (!pal) {
  2656. av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
  2657. } else {
  2658. memcpy(pal, sc->palette, AVPALETTE_SIZE);
  2659. sc->has_palette = 0;
  2660. }
  2661. }
  2662. #if CONFIG_DV_DEMUXER
  2663. if (mov->dv_demux && sc->dv_audio_container) {
  2664. avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
  2665. av_free(pkt->data);
  2666. pkt->size = 0;
  2667. ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
  2668. if (ret < 0)
  2669. return ret;
  2670. }
  2671. #endif
  2672. }
  2673. pkt->stream_index = sc->ffindex;
  2674. pkt->dts = sample->timestamp;
  2675. if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
  2676. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  2677. /* update ctts context */
  2678. sc->ctts_sample++;
  2679. if (sc->ctts_index < sc->ctts_count &&
  2680. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  2681. sc->ctts_index++;
  2682. sc->ctts_sample = 0;
  2683. }
  2684. if (sc->wrong_dts)
  2685. pkt->dts = AV_NOPTS_VALUE;
  2686. } else {
  2687. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  2688. st->index_entries[sc->current_sample].timestamp : st->duration;
  2689. pkt->duration = next_dts - pkt->dts;
  2690. pkt->pts = pkt->dts;
  2691. }
  2692. if (st->discard == AVDISCARD_ALL)
  2693. goto retry;
  2694. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
  2695. pkt->pos = sample->pos;
  2696. av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  2697. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  2698. return 0;
  2699. }
  2700. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  2701. {
  2702. MOVStreamContext *sc = st->priv_data;
  2703. int sample, time_sample;
  2704. int i;
  2705. sample = av_index_search_timestamp(st, timestamp, flags);
  2706. av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  2707. if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
  2708. sample = 0;
  2709. if (sample < 0) /* not sure what to do */
  2710. return AVERROR_INVALIDDATA;
  2711. sc->current_sample = sample;
  2712. av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
  2713. /* adjust ctts index */
  2714. if (sc->ctts_data) {
  2715. time_sample = 0;
  2716. for (i = 0; i < sc->ctts_count; i++) {
  2717. int next = time_sample + sc->ctts_data[i].count;
  2718. if (next > sc->current_sample) {
  2719. sc->ctts_index = i;
  2720. sc->ctts_sample = sc->current_sample - time_sample;
  2721. break;
  2722. }
  2723. time_sample = next;
  2724. }
  2725. }
  2726. return sample;
  2727. }
  2728. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  2729. {
  2730. AVStream *st;
  2731. int64_t seek_timestamp, timestamp;
  2732. int sample;
  2733. int i;
  2734. if (stream_index >= s->nb_streams)
  2735. return AVERROR_INVALIDDATA;
  2736. st = s->streams[stream_index];
  2737. sample = mov_seek_stream(s, st, sample_time, flags);
  2738. if (sample < 0)
  2739. return sample;
  2740. /* adjust seek timestamp to found sample timestamp */
  2741. seek_timestamp = st->index_entries[sample].timestamp;
  2742. for (i = 0; i < s->nb_streams; i++) {
  2743. MOVStreamContext *sc = s->streams[i]->priv_data;
  2744. st = s->streams[i];
  2745. st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
  2746. if (stream_index == i)
  2747. continue;
  2748. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  2749. mov_seek_stream(s, st, timestamp, flags);
  2750. }
  2751. return 0;
  2752. }
  2753. static const AVOption options[] = {
  2754. {"use_absolute_path",
  2755. "allow using absolute path when opening alias, this is a possible security issue",
  2756. offsetof(MOVContext, use_absolute_path), FF_OPT_TYPE_INT, {.dbl = 0},
  2757. 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
  2758. {NULL}
  2759. };
  2760. static const AVClass class = {
  2761. .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
  2762. .item_name = av_default_item_name,
  2763. .option = options,
  2764. .version = LIBAVUTIL_VERSION_INT,
  2765. };
  2766. AVInputFormat ff_mov_demuxer = {
  2767. .name = "mov,mp4,m4a,3gp,3g2,mj2",
  2768. .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
  2769. .priv_data_size = sizeof(MOVContext),
  2770. .read_probe = mov_probe,
  2771. .read_header = mov_read_header,
  2772. .read_packet = mov_read_packet,
  2773. .read_close = mov_read_close,
  2774. .read_seek = mov_read_seek,
  2775. .priv_class = &class,
  2776. };