mov.c 112 KB

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