mov.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363
  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 DEBUG_METADATA
  25. //#define MOV_EXPORT_ALL_METADATA
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/avstring.h"
  28. #include "avformat.h"
  29. #include "riff.h"
  30. #include "isom.h"
  31. #include "libavcodec/mpeg4audio.h"
  32. #include "libavcodec/mpegaudiodata.h"
  33. #include "libavcodec/get_bits.h"
  34. #if CONFIG_ZLIB
  35. #include <zlib.h>
  36. #endif
  37. /*
  38. * First version by Francois Revol revol@free.fr
  39. * Seek function by Gael Chardon gael.dev@4now.net
  40. *
  41. * Features and limitations:
  42. * - reads most of the QT files I have (at least the structure),
  43. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  44. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  45. *
  46. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  47. * when coding this :) (it's a writer anyway)
  48. *
  49. * Reference documents:
  50. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  51. * Apple:
  52. * http://developer.apple.com/documentation/QuickTime/QTFF/
  53. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  54. * QuickTime is a trademark of Apple (AFAIK :))
  55. */
  56. #include "qtpalette.h"
  57. #undef NDEBUG
  58. #include <assert.h>
  59. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  60. /* those functions parse an atom */
  61. /* return code:
  62. 0: continue to parse next atom
  63. <0: error occurred, exit
  64. */
  65. /* links atom IDs to parse functions */
  66. typedef struct MOVParseTableEntry {
  67. uint32_t type;
  68. int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOVAtom atom);
  69. } MOVParseTableEntry;
  70. static const MOVParseTableEntry mov_default_parse_table[];
  71. static int mov_metadata_trkn(MOVContext *c, ByteIOContext *pb, unsigned len)
  72. {
  73. char buf[16];
  74. get_be16(pb); // unknown
  75. snprintf(buf, sizeof(buf), "%d", get_be16(pb));
  76. av_metadata_set(&c->fc->metadata, "track", buf);
  77. get_be16(pb); // total tracks
  78. return 0;
  79. }
  80. static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  81. {
  82. #ifdef MOV_EXPORT_ALL_METADATA
  83. char tmp_key[5];
  84. #endif
  85. char str[1024], key2[16], language[4] = {0};
  86. const char *key = NULL;
  87. uint16_t str_size;
  88. int (*parse)(MOVContext*, ByteIOContext*, unsigned) = NULL;
  89. switch (atom.type) {
  90. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  91. case MKTAG(0xa9,'a','u','t'):
  92. case MKTAG(0xa9,'A','R','T'): key = "author"; break;
  93. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  94. case MKTAG( 'c','p','r','t'):
  95. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  96. case MKTAG(0xa9,'c','m','t'):
  97. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  98. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  99. case MKTAG(0xa9,'d','a','y'): key = "year"; break;
  100. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  101. case MKTAG(0xa9,'t','o','o'):
  102. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  103. case MKTAG( 'd','e','s','c'): key = "description";break;
  104. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  105. case MKTAG( 't','v','s','h'): key = "show"; break;
  106. case MKTAG( 't','v','e','n'): key = "episode_id";break;
  107. case MKTAG( 't','v','n','n'): key = "network"; break;
  108. case MKTAG( 't','r','k','n'): key = "track";
  109. parse = mov_metadata_trkn; break;
  110. }
  111. if (c->itunes_metadata && atom.size > 8) {
  112. int data_size = get_be32(pb);
  113. int tag = get_le32(pb);
  114. if (tag == MKTAG('d','a','t','a')) {
  115. get_be32(pb); // type
  116. get_be32(pb); // unknown
  117. str_size = data_size - 16;
  118. atom.size -= 16;
  119. } else return 0;
  120. } else if (atom.size > 4 && key && !c->itunes_metadata) {
  121. str_size = get_be16(pb); // string length
  122. ff_mov_lang_to_iso639(get_be16(pb), language);
  123. atom.size -= 4;
  124. } else
  125. str_size = atom.size;
  126. #ifdef MOV_EXPORT_ALL_METADATA
  127. if (!key) {
  128. snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
  129. key = tmp_key;
  130. }
  131. #endif
  132. if (!key)
  133. return 0;
  134. if (atom.size < 0)
  135. return -1;
  136. str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
  137. if (parse)
  138. parse(c, pb, str_size);
  139. else {
  140. get_buffer(pb, str, str_size);
  141. str[str_size] = 0;
  142. av_metadata_set(&c->fc->metadata, key, str);
  143. if (*language && strcmp(language, "und")) {
  144. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  145. av_metadata_set(&c->fc->metadata, key2, str);
  146. }
  147. }
  148. #ifdef DEBUG_METADATA
  149. av_log(c->fc, AV_LOG_DEBUG, "lang \"%3s\" ", language);
  150. av_log(c->fc, AV_LOG_DEBUG, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %lld\n",
  151. key, str, (char*)&atom.type, str_size, atom.size);
  152. #endif
  153. return 0;
  154. }
  155. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  156. {
  157. int64_t total_size = 0;
  158. MOVAtom a;
  159. int i;
  160. int err = 0;
  161. a.offset = atom.offset;
  162. if (atom.size < 0)
  163. atom.size = INT64_MAX;
  164. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  165. int (*parse)(MOVContext*, ByteIOContext*, MOVAtom) = NULL;
  166. a.size = atom.size;
  167. a.type=0;
  168. if(atom.size >= 8) {
  169. a.size = get_be32(pb);
  170. a.type = get_le32(pb);
  171. }
  172. total_size += 8;
  173. a.offset += 8;
  174. dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n",
  175. a.type, (char*)&a.type, a.size, atom.size, total_size);
  176. if (a.size == 1) { /* 64 bit extended size */
  177. a.size = get_be64(pb) - 8;
  178. a.offset += 8;
  179. total_size += 8;
  180. }
  181. if (a.size == 0) {
  182. a.size = atom.size - total_size;
  183. if (a.size <= 8)
  184. break;
  185. }
  186. a.size -= 8;
  187. if(a.size < 0)
  188. break;
  189. a.size = FFMIN(a.size, atom.size - total_size);
  190. for (i = 0; mov_default_parse_table[i].type; i++)
  191. if (mov_default_parse_table[i].type == a.type) {
  192. parse = mov_default_parse_table[i].parse;
  193. break;
  194. }
  195. // container is user data
  196. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  197. atom.type == MKTAG('i','l','s','t')))
  198. parse = mov_read_udta_string;
  199. if (!parse) { /* skip leaf atoms data */
  200. url_fskip(pb, a.size);
  201. } else {
  202. int64_t start_pos = url_ftell(pb);
  203. int64_t left;
  204. err = parse(c, pb, a);
  205. if (url_is_streamed(pb) && c->found_moov && c->found_mdat)
  206. break;
  207. left = a.size - url_ftell(pb) + start_pos;
  208. if (left > 0) /* skip garbage at atom end */
  209. url_fskip(pb, left);
  210. }
  211. a.offset += a.size;
  212. total_size += a.size;
  213. }
  214. if (!err && total_size < atom.size && atom.size < 0x7ffff)
  215. url_fskip(pb, atom.size - total_size);
  216. return err;
  217. }
  218. static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  219. {
  220. AVStream *st;
  221. MOVStreamContext *sc;
  222. int entries, i, j;
  223. if (c->fc->nb_streams < 1)
  224. return 0;
  225. st = c->fc->streams[c->fc->nb_streams-1];
  226. sc = st->priv_data;
  227. get_be32(pb); // version + flags
  228. entries = get_be32(pb);
  229. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  230. return -1;
  231. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  232. if (!sc->drefs)
  233. return AVERROR(ENOMEM);
  234. sc->drefs_count = entries;
  235. for (i = 0; i < sc->drefs_count; i++) {
  236. MOVDref *dref = &sc->drefs[i];
  237. uint32_t size = get_be32(pb);
  238. int64_t next = url_ftell(pb) + size - 4;
  239. dref->type = get_le32(pb);
  240. get_be32(pb); // version + flags
  241. dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  242. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  243. /* macintosh alias record */
  244. uint16_t volume_len, len;
  245. int16_t type;
  246. url_fskip(pb, 10);
  247. volume_len = get_byte(pb);
  248. volume_len = FFMIN(volume_len, 27);
  249. get_buffer(pb, dref->volume, 27);
  250. dref->volume[volume_len] = 0;
  251. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  252. url_fskip(pb, 12);
  253. len = get_byte(pb);
  254. len = FFMIN(len, 63);
  255. get_buffer(pb, dref->filename, 63);
  256. dref->filename[len] = 0;
  257. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  258. url_fskip(pb, 16);
  259. /* read next level up_from_alias/down_to_target */
  260. dref->nlvl_from = get_be16(pb);
  261. dref->nlvl_to = get_be16(pb);
  262. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  263. dref->nlvl_from, dref->nlvl_to);
  264. url_fskip(pb, 16);
  265. for (type = 0; type != -1 && url_ftell(pb) < next; ) {
  266. type = get_be16(pb);
  267. len = get_be16(pb);
  268. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  269. if (len&1)
  270. len += 1;
  271. if (type == 2) { // absolute path
  272. av_free(dref->path);
  273. dref->path = av_mallocz(len+1);
  274. if (!dref->path)
  275. return AVERROR(ENOMEM);
  276. get_buffer(pb, dref->path, len);
  277. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  278. len -= volume_len;
  279. memmove(dref->path, dref->path+volume_len, len);
  280. dref->path[len] = 0;
  281. }
  282. for (j = 0; j < len; j++)
  283. if (dref->path[j] == ':')
  284. dref->path[j] = '/';
  285. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  286. } else if (type == 0) { // directory name
  287. av_free(dref->dir);
  288. dref->dir = av_malloc(len+1);
  289. if (!dref->dir)
  290. return AVERROR(ENOMEM);
  291. get_buffer(pb, dref->dir, len);
  292. dref->dir[len] = 0;
  293. for (j = 0; j < len; j++)
  294. if (dref->dir[j] == ':')
  295. dref->dir[j] = '/';
  296. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  297. } else
  298. url_fskip(pb, len);
  299. }
  300. }
  301. url_fseek(pb, next, SEEK_SET);
  302. }
  303. return 0;
  304. }
  305. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  306. {
  307. AVStream *st;
  308. uint32_t type;
  309. uint32_t ctype;
  310. if (c->fc->nb_streams < 1) // meta before first trak
  311. return 0;
  312. st = c->fc->streams[c->fc->nb_streams-1];
  313. get_byte(pb); /* version */
  314. get_be24(pb); /* flags */
  315. /* component type */
  316. ctype = get_le32(pb);
  317. type = get_le32(pb); /* component subtype */
  318. dprintf(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
  319. dprintf(c->fc, "stype= %.4s\n", (char*)&type);
  320. if (type == MKTAG('v','i','d','e'))
  321. st->codec->codec_type = CODEC_TYPE_VIDEO;
  322. else if(type == MKTAG('s','o','u','n'))
  323. st->codec->codec_type = CODEC_TYPE_AUDIO;
  324. else if(type == MKTAG('m','1','a',' '))
  325. st->codec->codec_id = CODEC_ID_MP2;
  326. else if(type == MKTAG('s','u','b','p'))
  327. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  328. get_be32(pb); /* component manufacture */
  329. get_be32(pb); /* component flags */
  330. get_be32(pb); /* component flags mask */
  331. if(atom.size <= 24)
  332. return 0; /* nothing left to read */
  333. url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
  334. return 0;
  335. }
  336. int ff_mp4_read_descr_len(ByteIOContext *pb)
  337. {
  338. int len = 0;
  339. int count = 4;
  340. while (count--) {
  341. int c = get_byte(pb);
  342. len = (len << 7) | (c & 0x7f);
  343. if (!(c & 0x80))
  344. break;
  345. }
  346. return len;
  347. }
  348. int mp4_read_descr(AVFormatContext *fc, ByteIOContext *pb, int *tag)
  349. {
  350. int len;
  351. *tag = get_byte(pb);
  352. len = ff_mp4_read_descr_len(pb);
  353. dprintf(fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  354. return len;
  355. }
  356. #define MP4ESDescrTag 0x03
  357. #define MP4DecConfigDescrTag 0x04
  358. #define MP4DecSpecificDescrTag 0x05
  359. static const AVCodecTag mp4_audio_types[] = {
  360. { CODEC_ID_MP3ON4, AOT_PS }, /* old mp3on4 draft */
  361. { CODEC_ID_MP3ON4, AOT_L1 }, /* layer 1 */
  362. { CODEC_ID_MP3ON4, AOT_L2 }, /* layer 2 */
  363. { CODEC_ID_MP3ON4, AOT_L3 }, /* layer 3 */
  364. { CODEC_ID_MP4ALS, AOT_ALS }, /* MPEG-4 ALS */
  365. { CODEC_ID_NONE, AOT_NULL },
  366. };
  367. int ff_mov_read_esds(AVFormatContext *fc, ByteIOContext *pb, MOVAtom atom)
  368. {
  369. AVStream *st;
  370. int tag, len;
  371. if (fc->nb_streams < 1)
  372. return 0;
  373. st = fc->streams[fc->nb_streams-1];
  374. get_be32(pb); /* version + flags */
  375. len = mp4_read_descr(fc, pb, &tag);
  376. if (tag == MP4ESDescrTag) {
  377. get_be16(pb); /* ID */
  378. get_byte(pb); /* priority */
  379. } else
  380. get_be16(pb); /* ID */
  381. len = mp4_read_descr(fc, pb, &tag);
  382. if (tag == MP4DecConfigDescrTag) {
  383. int object_type_id = get_byte(pb);
  384. get_byte(pb); /* stream type */
  385. get_be24(pb); /* buffer size db */
  386. get_be32(pb); /* max bitrate */
  387. get_be32(pb); /* avg bitrate */
  388. st->codec->codec_id= ff_codec_get_id(ff_mp4_obj_type, object_type_id);
  389. dprintf(fc, "esds object type id 0x%02x\n", object_type_id);
  390. len = mp4_read_descr(fc, pb, &tag);
  391. if (tag == MP4DecSpecificDescrTag) {
  392. dprintf(fc, "Specific MPEG4 header len=%d\n", len);
  393. if((uint64_t)len > (1<<30))
  394. return -1;
  395. st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  396. if (!st->codec->extradata)
  397. return AVERROR(ENOMEM);
  398. get_buffer(pb, st->codec->extradata, len);
  399. st->codec->extradata_size = len;
  400. if (st->codec->codec_id == CODEC_ID_AAC) {
  401. MPEG4AudioConfig cfg;
  402. ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
  403. st->codec->extradata_size);
  404. st->codec->channels = cfg.channels;
  405. if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4
  406. st->codec->sample_rate = ff_mpa_freq_tab[cfg.sampling_index];
  407. else
  408. st->codec->sample_rate = cfg.sample_rate; // ext sample rate ?
  409. dprintf(fc, "mp4a config channels %d obj %d ext obj %d "
  410. "sample rate %d ext sample rate %d\n", st->codec->channels,
  411. cfg.object_type, cfg.ext_object_type,
  412. cfg.sample_rate, cfg.ext_sample_rate);
  413. if (!(st->codec->codec_id = ff_codec_get_id(mp4_audio_types,
  414. cfg.object_type)))
  415. st->codec->codec_id = CODEC_ID_AAC;
  416. }
  417. }
  418. }
  419. return 0;
  420. }
  421. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  422. {
  423. return ff_mov_read_esds(c->fc, pb, atom);
  424. }
  425. static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  426. {
  427. const int num = get_be32(pb);
  428. const int den = get_be32(pb);
  429. AVStream *st;
  430. if (c->fc->nb_streams < 1)
  431. return 0;
  432. st = c->fc->streams[c->fc->nb_streams-1];
  433. if (den != 0) {
  434. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  435. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num))
  436. av_log(c->fc, AV_LOG_WARNING,
  437. "sample aspect ratio already set to %d:%d, overriding by 'pasp' atom\n",
  438. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
  439. st->sample_aspect_ratio.num = num;
  440. st->sample_aspect_ratio.den = den;
  441. }
  442. return 0;
  443. }
  444. /* this atom contains actual media data */
  445. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  446. {
  447. if(atom.size == 0) /* wrong one (MP4) */
  448. return 0;
  449. c->found_mdat=1;
  450. return 0; /* now go for moov */
  451. }
  452. /* read major brand, minor version and compatible brands and store them as metadata */
  453. static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  454. {
  455. uint32_t minor_ver;
  456. int comp_brand_size;
  457. char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
  458. char* comp_brands_str;
  459. uint8_t type[5] = {0};
  460. get_buffer(pb, type, 4);
  461. if (strcmp(type, "qt "))
  462. c->isom = 1;
  463. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  464. av_metadata_set(&c->fc->metadata, "major_brand", type);
  465. minor_ver = get_be32(pb); /* minor version */
  466. snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
  467. av_metadata_set(&c->fc->metadata, "minor_version", minor_ver_str);
  468. comp_brand_size = atom.size - 8;
  469. if (comp_brand_size < 0)
  470. return -1;
  471. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  472. if (!comp_brands_str)
  473. return AVERROR(ENOMEM);
  474. get_buffer(pb, comp_brands_str, comp_brand_size);
  475. comp_brands_str[comp_brand_size] = 0;
  476. av_metadata_set(&c->fc->metadata, "compatible_brands", comp_brands_str);
  477. av_freep(&comp_brands_str);
  478. return 0;
  479. }
  480. /* this atom should contain all header atoms */
  481. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  482. {
  483. if (mov_read_default(c, pb, atom) < 0)
  484. return -1;
  485. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  486. /* so we don't parse the whole file if over a network */
  487. c->found_moov=1;
  488. return 0; /* now go for mdat */
  489. }
  490. static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  491. {
  492. c->fragment.moof_offset = url_ftell(pb) - 8;
  493. dprintf(c->fc, "moof offset %llx\n", c->fragment.moof_offset);
  494. return mov_read_default(c, pb, atom);
  495. }
  496. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  497. {
  498. AVStream *st;
  499. MOVStreamContext *sc;
  500. int version;
  501. char language[4] = {0};
  502. unsigned lang;
  503. if (c->fc->nb_streams < 1)
  504. return 0;
  505. st = c->fc->streams[c->fc->nb_streams-1];
  506. sc = st->priv_data;
  507. version = get_byte(pb);
  508. if (version > 1)
  509. return -1; /* unsupported */
  510. get_be24(pb); /* flags */
  511. if (version == 1) {
  512. get_be64(pb);
  513. get_be64(pb);
  514. } else {
  515. get_be32(pb); /* creation time */
  516. get_be32(pb); /* modification time */
  517. }
  518. sc->time_scale = get_be32(pb);
  519. st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  520. lang = get_be16(pb); /* language */
  521. if (ff_mov_lang_to_iso639(lang, language))
  522. av_metadata_set(&st->metadata, "language", language);
  523. get_be16(pb); /* quality */
  524. return 0;
  525. }
  526. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  527. {
  528. int version = get_byte(pb); /* version */
  529. get_be24(pb); /* flags */
  530. if (version == 1) {
  531. get_be64(pb);
  532. get_be64(pb);
  533. } else {
  534. get_be32(pb); /* creation time */
  535. get_be32(pb); /* modification time */
  536. }
  537. c->time_scale = get_be32(pb); /* time scale */
  538. dprintf(c->fc, "time scale = %i\n", c->time_scale);
  539. c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  540. get_be32(pb); /* preferred scale */
  541. get_be16(pb); /* preferred volume */
  542. url_fskip(pb, 10); /* reserved */
  543. url_fskip(pb, 36); /* display matrix */
  544. get_be32(pb); /* preview time */
  545. get_be32(pb); /* preview duration */
  546. get_be32(pb); /* poster time */
  547. get_be32(pb); /* selection time */
  548. get_be32(pb); /* selection duration */
  549. get_be32(pb); /* current time */
  550. get_be32(pb); /* next track ID */
  551. return 0;
  552. }
  553. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  554. {
  555. AVStream *st;
  556. if (c->fc->nb_streams < 1)
  557. return 0;
  558. st = c->fc->streams[c->fc->nb_streams-1];
  559. if((uint64_t)atom.size > (1<<30))
  560. return -1;
  561. // currently SVQ3 decoder expect full STSD header - so let's fake it
  562. // this should be fixed and just SMI header should be passed
  563. av_free(st->codec->extradata);
  564. st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
  565. if (!st->codec->extradata)
  566. return AVERROR(ENOMEM);
  567. st->codec->extradata_size = 0x5a + atom.size;
  568. memcpy(st->codec->extradata, "SVQ3", 4); // fake
  569. get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
  570. dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
  571. return 0;
  572. }
  573. static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  574. {
  575. AVStream *st;
  576. int little_endian;
  577. if (c->fc->nb_streams < 1)
  578. return 0;
  579. st = c->fc->streams[c->fc->nb_streams-1];
  580. little_endian = get_be16(pb);
  581. dprintf(c->fc, "enda %d\n", little_endian);
  582. if (little_endian == 1) {
  583. switch (st->codec->codec_id) {
  584. case CODEC_ID_PCM_S24BE:
  585. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  586. break;
  587. case CODEC_ID_PCM_S32BE:
  588. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  589. break;
  590. case CODEC_ID_PCM_F32BE:
  591. st->codec->codec_id = CODEC_ID_PCM_F32LE;
  592. break;
  593. case CODEC_ID_PCM_F64BE:
  594. st->codec->codec_id = CODEC_ID_PCM_F64LE;
  595. break;
  596. default:
  597. break;
  598. }
  599. }
  600. return 0;
  601. }
  602. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  603. static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  604. {
  605. AVStream *st;
  606. uint64_t size;
  607. uint8_t *buf;
  608. if (c->fc->nb_streams < 1) // will happen with jp2 files
  609. return 0;
  610. st= c->fc->streams[c->fc->nb_streams-1];
  611. size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  612. if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  613. return -1;
  614. buf= av_realloc(st->codec->extradata, size);
  615. if(!buf)
  616. return -1;
  617. st->codec->extradata= buf;
  618. buf+= st->codec->extradata_size;
  619. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  620. AV_WB32( buf , atom.size + 8);
  621. AV_WL32( buf + 4, atom.type);
  622. get_buffer(pb, buf + 8, atom.size);
  623. return 0;
  624. }
  625. static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  626. {
  627. AVStream *st;
  628. if (c->fc->nb_streams < 1)
  629. return 0;
  630. st = c->fc->streams[c->fc->nb_streams-1];
  631. if((uint64_t)atom.size > (1<<30))
  632. return -1;
  633. if (st->codec->codec_id == CODEC_ID_QDM2) {
  634. // pass all frma atom to codec, needed at least for QDM2
  635. av_free(st->codec->extradata);
  636. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  637. if (!st->codec->extradata)
  638. return AVERROR(ENOMEM);
  639. st->codec->extradata_size = atom.size;
  640. get_buffer(pb, st->codec->extradata, atom.size);
  641. } else if (atom.size > 8) { /* to read frma, esds atoms */
  642. if (mov_read_default(c, pb, atom) < 0)
  643. return -1;
  644. } else
  645. url_fskip(pb, atom.size);
  646. return 0;
  647. }
  648. /**
  649. * This function reads atom content and puts data in extradata without tag
  650. * nor size unlike mov_read_extradata.
  651. */
  652. static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  653. {
  654. AVStream *st;
  655. if (c->fc->nb_streams < 1)
  656. return 0;
  657. st = c->fc->streams[c->fc->nb_streams-1];
  658. if((uint64_t)atom.size > (1<<30))
  659. return -1;
  660. av_free(st->codec->extradata);
  661. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  662. if (!st->codec->extradata)
  663. return AVERROR(ENOMEM);
  664. st->codec->extradata_size = atom.size;
  665. get_buffer(pb, st->codec->extradata, atom.size);
  666. return 0;
  667. }
  668. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  669. {
  670. AVStream *st;
  671. MOVStreamContext *sc;
  672. unsigned int i, entries;
  673. if (c->fc->nb_streams < 1)
  674. return 0;
  675. st = c->fc->streams[c->fc->nb_streams-1];
  676. sc = st->priv_data;
  677. get_byte(pb); /* version */
  678. get_be24(pb); /* flags */
  679. entries = get_be32(pb);
  680. if(entries >= UINT_MAX/sizeof(int64_t))
  681. return -1;
  682. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  683. if (!sc->chunk_offsets)
  684. return AVERROR(ENOMEM);
  685. sc->chunk_count = entries;
  686. if (atom.type == MKTAG('s','t','c','o'))
  687. for(i=0; i<entries; i++)
  688. sc->chunk_offsets[i] = get_be32(pb);
  689. else if (atom.type == MKTAG('c','o','6','4'))
  690. for(i=0; i<entries; i++)
  691. sc->chunk_offsets[i] = get_be64(pb);
  692. else
  693. return -1;
  694. return 0;
  695. }
  696. /**
  697. * Compute codec id for 'lpcm' tag.
  698. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  699. */
  700. enum CodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
  701. {
  702. if (flags & 1) { // floating point
  703. if (flags & 2) { // big endian
  704. if (bps == 32) return CODEC_ID_PCM_F32BE;
  705. else if (bps == 64) return CODEC_ID_PCM_F64BE;
  706. } else {
  707. if (bps == 32) return CODEC_ID_PCM_F32LE;
  708. else if (bps == 64) return CODEC_ID_PCM_F64LE;
  709. }
  710. } else {
  711. if (flags & 2) {
  712. if (bps == 8)
  713. // signed integer
  714. if (flags & 4) return CODEC_ID_PCM_S8;
  715. else return CODEC_ID_PCM_U8;
  716. else if (bps == 16) return CODEC_ID_PCM_S16BE;
  717. else if (bps == 24) return CODEC_ID_PCM_S24BE;
  718. else if (bps == 32) return CODEC_ID_PCM_S32BE;
  719. } else {
  720. if (bps == 8)
  721. if (flags & 4) return CODEC_ID_PCM_S8;
  722. else return CODEC_ID_PCM_U8;
  723. else if (bps == 16) return CODEC_ID_PCM_S16LE;
  724. else if (bps == 24) return CODEC_ID_PCM_S24LE;
  725. else if (bps == 32) return CODEC_ID_PCM_S32LE;
  726. }
  727. }
  728. return CODEC_ID_NONE;
  729. }
  730. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  731. {
  732. AVStream *st;
  733. MOVStreamContext *sc;
  734. int j, entries, pseudo_stream_id;
  735. if (c->fc->nb_streams < 1)
  736. return 0;
  737. st = c->fc->streams[c->fc->nb_streams-1];
  738. sc = st->priv_data;
  739. get_byte(pb); /* version */
  740. get_be24(pb); /* flags */
  741. entries = get_be32(pb);
  742. for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
  743. //Parsing Sample description table
  744. enum CodecID id;
  745. int dref_id = 1;
  746. MOVAtom a = { 0, 0, 0 };
  747. int64_t start_pos = url_ftell(pb);
  748. int size = get_be32(pb); /* size */
  749. uint32_t format = get_le32(pb); /* data format */
  750. if (size >= 16) {
  751. get_be32(pb); /* reserved */
  752. get_be16(pb); /* reserved */
  753. dref_id = get_be16(pb);
  754. }
  755. if (st->codec->codec_tag &&
  756. st->codec->codec_tag != format &&
  757. (c->fc->video_codec_id ? ff_codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
  758. : st->codec->codec_tag != MKTAG('j','p','e','g'))
  759. ){
  760. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  761. * export it as a separate AVStream but this needs a few changes
  762. * in the MOV demuxer, patch welcome. */
  763. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  764. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  765. continue;
  766. }
  767. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  768. sc->dref_id= dref_id;
  769. st->codec->codec_tag = format;
  770. id = ff_codec_get_id(codec_movaudio_tags, format);
  771. if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
  772. id = ff_codec_get_id(ff_codec_wav_tags, bswap_32(format)&0xFFFF);
  773. if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
  774. st->codec->codec_type = CODEC_TYPE_AUDIO;
  775. } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
  776. format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
  777. id = ff_codec_get_id(codec_movvideo_tags, format);
  778. if (id <= 0)
  779. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  780. if (id > 0)
  781. st->codec->codec_type = CODEC_TYPE_VIDEO;
  782. else if(st->codec->codec_type == CODEC_TYPE_DATA){
  783. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  784. if(id > 0)
  785. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  786. }
  787. }
  788. dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
  789. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  790. (format >> 24) & 0xff, st->codec->codec_type);
  791. if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
  792. uint8_t codec_name[32];
  793. unsigned int color_depth;
  794. int color_greyscale;
  795. st->codec->codec_id = id;
  796. get_be16(pb); /* version */
  797. get_be16(pb); /* revision level */
  798. get_be32(pb); /* vendor */
  799. get_be32(pb); /* temporal quality */
  800. get_be32(pb); /* spatial quality */
  801. st->codec->width = get_be16(pb); /* width */
  802. st->codec->height = get_be16(pb); /* height */
  803. get_be32(pb); /* horiz resolution */
  804. get_be32(pb); /* vert resolution */
  805. get_be32(pb); /* data size, always 0 */
  806. get_be16(pb); /* frames per samples */
  807. get_buffer(pb, codec_name, 32); /* codec name, pascal string */
  808. if (codec_name[0] <= 31) {
  809. int i;
  810. int pos = 0;
  811. for (i = 0; i < codec_name[0] && pos < sizeof(st->codec->codec_name) - 3; i++) {
  812. uint8_t tmp;
  813. PUT_UTF8(codec_name[i+1], tmp, st->codec->codec_name[pos++] = tmp;)
  814. }
  815. st->codec->codec_name[pos] = 0;
  816. }
  817. st->codec->bits_per_coded_sample = get_be16(pb); /* depth */
  818. st->codec->color_table_id = get_be16(pb); /* colortable id */
  819. dprintf(c->fc, "depth %d, ctab id %d\n",
  820. st->codec->bits_per_coded_sample, st->codec->color_table_id);
  821. /* figure out the palette situation */
  822. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  823. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  824. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  825. if ((color_depth == 2) || (color_depth == 4) ||
  826. (color_depth == 8)) {
  827. /* for palette traversal */
  828. unsigned int color_start, color_count, color_end;
  829. unsigned char r, g, b;
  830. st->codec->palctrl = av_malloc(sizeof(*st->codec->palctrl));
  831. if (color_greyscale) {
  832. int color_index, color_dec;
  833. /* compute the greyscale palette */
  834. st->codec->bits_per_coded_sample = color_depth;
  835. color_count = 1 << color_depth;
  836. color_index = 255;
  837. color_dec = 256 / (color_count - 1);
  838. for (j = 0; j < color_count; j++) {
  839. r = g = b = color_index;
  840. st->codec->palctrl->palette[j] =
  841. (r << 16) | (g << 8) | (b);
  842. color_index -= color_dec;
  843. if (color_index < 0)
  844. color_index = 0;
  845. }
  846. } else if (st->codec->color_table_id) {
  847. const uint8_t *color_table;
  848. /* if flag bit 3 is set, use the default palette */
  849. color_count = 1 << color_depth;
  850. if (color_depth == 2)
  851. color_table = ff_qt_default_palette_4;
  852. else if (color_depth == 4)
  853. color_table = ff_qt_default_palette_16;
  854. else
  855. color_table = ff_qt_default_palette_256;
  856. for (j = 0; j < color_count; j++) {
  857. r = color_table[j * 3 + 0];
  858. g = color_table[j * 3 + 1];
  859. b = color_table[j * 3 + 2];
  860. st->codec->palctrl->palette[j] =
  861. (r << 16) | (g << 8) | (b);
  862. }
  863. } else {
  864. /* load the palette from the file */
  865. color_start = get_be32(pb);
  866. color_count = get_be16(pb);
  867. color_end = get_be16(pb);
  868. if ((color_start <= 255) &&
  869. (color_end <= 255)) {
  870. for (j = color_start; j <= color_end; j++) {
  871. /* each R, G, or B component is 16 bits;
  872. * only use the top 8 bits; skip alpha bytes
  873. * up front */
  874. get_byte(pb);
  875. get_byte(pb);
  876. r = get_byte(pb);
  877. get_byte(pb);
  878. g = get_byte(pb);
  879. get_byte(pb);
  880. b = get_byte(pb);
  881. get_byte(pb);
  882. st->codec->palctrl->palette[j] =
  883. (r << 16) | (g << 8) | (b);
  884. }
  885. }
  886. }
  887. st->codec->palctrl->palette_changed = 1;
  888. }
  889. } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
  890. int bits_per_sample, flags;
  891. uint16_t version = get_be16(pb);
  892. st->codec->codec_id = id;
  893. get_be16(pb); /* revision level */
  894. get_be32(pb); /* vendor */
  895. st->codec->channels = get_be16(pb); /* channel count */
  896. dprintf(c->fc, "audio channels %d\n", st->codec->channels);
  897. st->codec->bits_per_coded_sample = get_be16(pb); /* sample size */
  898. sc->audio_cid = get_be16(pb);
  899. get_be16(pb); /* packet size = 0 */
  900. st->codec->sample_rate = ((get_be32(pb) >> 16));
  901. //Read QT version 1 fields. In version 0 these do not exist.
  902. dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
  903. if(!c->isom) {
  904. if(version==1) {
  905. sc->samples_per_frame = get_be32(pb);
  906. get_be32(pb); /* bytes per packet */
  907. sc->bytes_per_frame = get_be32(pb);
  908. get_be32(pb); /* bytes per sample */
  909. } else if(version==2) {
  910. get_be32(pb); /* sizeof struct only */
  911. st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
  912. st->codec->channels = get_be32(pb);
  913. get_be32(pb); /* always 0x7F000000 */
  914. st->codec->bits_per_coded_sample = get_be32(pb); /* bits per channel if sound is uncompressed */
  915. flags = get_be32(pb); /* lcpm format specific flag */
  916. sc->bytes_per_frame = get_be32(pb); /* bytes per audio packet if constant */
  917. sc->samples_per_frame = get_be32(pb); /* lpcm frames per audio packet if constant */
  918. if (format == MKTAG('l','p','c','m'))
  919. st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  920. }
  921. }
  922. switch (st->codec->codec_id) {
  923. case CODEC_ID_PCM_S8:
  924. case CODEC_ID_PCM_U8:
  925. if (st->codec->bits_per_coded_sample == 16)
  926. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  927. break;
  928. case CODEC_ID_PCM_S16LE:
  929. case CODEC_ID_PCM_S16BE:
  930. if (st->codec->bits_per_coded_sample == 8)
  931. st->codec->codec_id = CODEC_ID_PCM_S8;
  932. else if (st->codec->bits_per_coded_sample == 24)
  933. st->codec->codec_id =
  934. st->codec->codec_id == CODEC_ID_PCM_S16BE ?
  935. CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
  936. break;
  937. /* set values for old format before stsd version 1 appeared */
  938. case CODEC_ID_MACE3:
  939. sc->samples_per_frame = 6;
  940. sc->bytes_per_frame = 2*st->codec->channels;
  941. break;
  942. case CODEC_ID_MACE6:
  943. sc->samples_per_frame = 6;
  944. sc->bytes_per_frame = 1*st->codec->channels;
  945. break;
  946. case CODEC_ID_ADPCM_IMA_QT:
  947. sc->samples_per_frame = 64;
  948. sc->bytes_per_frame = 34*st->codec->channels;
  949. break;
  950. case CODEC_ID_GSM:
  951. sc->samples_per_frame = 160;
  952. sc->bytes_per_frame = 33;
  953. break;
  954. default:
  955. break;
  956. }
  957. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  958. if (bits_per_sample) {
  959. st->codec->bits_per_coded_sample = bits_per_sample;
  960. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  961. }
  962. } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
  963. // ttxt stsd contains display flags, justification, background
  964. // color, fonts, and default styles, so fake an atom to read it
  965. MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) };
  966. if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
  967. mov_read_glbl(c, pb, fake_atom);
  968. st->codec->codec_id= id;
  969. st->codec->width = sc->width;
  970. st->codec->height = sc->height;
  971. } else {
  972. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  973. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  974. }
  975. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  976. a.size = size - (url_ftell(pb) - start_pos);
  977. if (a.size > 8) {
  978. if (mov_read_default(c, pb, a) < 0)
  979. return -1;
  980. } else if (a.size > 0)
  981. url_fskip(pb, a.size);
  982. }
  983. if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  984. st->codec->sample_rate= sc->time_scale;
  985. /* special codec parameters handling */
  986. switch (st->codec->codec_id) {
  987. #if CONFIG_DV_DEMUXER
  988. case CODEC_ID_DVAUDIO:
  989. c->dv_fctx = avformat_alloc_context();
  990. c->dv_demux = dv_init_demux(c->dv_fctx);
  991. if (!c->dv_demux) {
  992. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  993. return -1;
  994. }
  995. sc->dv_audio_container = 1;
  996. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  997. break;
  998. #endif
  999. /* no ifdef since parameters are always those */
  1000. case CODEC_ID_QCELP:
  1001. // force sample rate for qcelp when not stored in mov
  1002. if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
  1003. st->codec->sample_rate = 8000;
  1004. st->codec->frame_size= 160;
  1005. st->codec->channels= 1; /* really needed */
  1006. break;
  1007. case CODEC_ID_AMR_NB:
  1008. case CODEC_ID_AMR_WB:
  1009. st->codec->frame_size= sc->samples_per_frame;
  1010. st->codec->channels= 1; /* really needed */
  1011. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  1012. if (st->codec->codec_id == CODEC_ID_AMR_NB)
  1013. st->codec->sample_rate = 8000;
  1014. else if (st->codec->codec_id == CODEC_ID_AMR_WB)
  1015. st->codec->sample_rate = 16000;
  1016. break;
  1017. case CODEC_ID_MP2:
  1018. case CODEC_ID_MP3:
  1019. st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  1020. st->need_parsing = AVSTREAM_PARSE_FULL;
  1021. break;
  1022. case CODEC_ID_GSM:
  1023. case CODEC_ID_ADPCM_MS:
  1024. case CODEC_ID_ADPCM_IMA_WAV:
  1025. st->codec->block_align = sc->bytes_per_frame;
  1026. break;
  1027. case CODEC_ID_ALAC:
  1028. if (st->codec->extradata_size == 36) {
  1029. st->codec->frame_size = AV_RB32(st->codec->extradata+12);
  1030. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  1031. }
  1032. break;
  1033. default:
  1034. break;
  1035. }
  1036. return 0;
  1037. }
  1038. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1039. {
  1040. AVStream *st;
  1041. MOVStreamContext *sc;
  1042. unsigned int i, entries;
  1043. if (c->fc->nb_streams < 1)
  1044. return 0;
  1045. st = c->fc->streams[c->fc->nb_streams-1];
  1046. sc = st->priv_data;
  1047. get_byte(pb); /* version */
  1048. get_be24(pb); /* flags */
  1049. entries = get_be32(pb);
  1050. dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1051. if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
  1052. return -1;
  1053. sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
  1054. if (!sc->stsc_data)
  1055. return AVERROR(ENOMEM);
  1056. sc->stsc_count = entries;
  1057. for(i=0; i<entries; i++) {
  1058. sc->stsc_data[i].first = get_be32(pb);
  1059. sc->stsc_data[i].count = get_be32(pb);
  1060. sc->stsc_data[i].id = get_be32(pb);
  1061. }
  1062. return 0;
  1063. }
  1064. static int mov_read_stps(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1065. {
  1066. AVStream *st;
  1067. MOVStreamContext *sc;
  1068. unsigned i, entries;
  1069. if (c->fc->nb_streams < 1)
  1070. return 0;
  1071. st = c->fc->streams[c->fc->nb_streams-1];
  1072. sc = st->priv_data;
  1073. get_be32(pb); // version + flags
  1074. entries = get_be32(pb);
  1075. if (entries >= UINT_MAX / sizeof(*sc->stps_data))
  1076. return -1;
  1077. sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
  1078. if (!sc->stps_data)
  1079. return AVERROR(ENOMEM);
  1080. sc->stps_count = entries;
  1081. for (i = 0; i < entries; i++) {
  1082. sc->stps_data[i] = get_be32(pb);
  1083. //dprintf(c->fc, "stps %d\n", sc->stps_data[i]);
  1084. }
  1085. return 0;
  1086. }
  1087. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1088. {
  1089. AVStream *st;
  1090. MOVStreamContext *sc;
  1091. unsigned int i, entries;
  1092. if (c->fc->nb_streams < 1)
  1093. return 0;
  1094. st = c->fc->streams[c->fc->nb_streams-1];
  1095. sc = st->priv_data;
  1096. get_byte(pb); /* version */
  1097. get_be24(pb); /* flags */
  1098. entries = get_be32(pb);
  1099. dprintf(c->fc, "keyframe_count = %d\n", entries);
  1100. if(entries >= UINT_MAX / sizeof(int))
  1101. return -1;
  1102. sc->keyframes = av_malloc(entries * sizeof(int));
  1103. if (!sc->keyframes)
  1104. return AVERROR(ENOMEM);
  1105. sc->keyframe_count = entries;
  1106. for(i=0; i<entries; i++) {
  1107. sc->keyframes[i] = get_be32(pb);
  1108. //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  1109. }
  1110. return 0;
  1111. }
  1112. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1113. {
  1114. AVStream *st;
  1115. MOVStreamContext *sc;
  1116. unsigned int i, entries, sample_size, field_size, num_bytes;
  1117. GetBitContext gb;
  1118. unsigned char* buf;
  1119. if (c->fc->nb_streams < 1)
  1120. return 0;
  1121. st = c->fc->streams[c->fc->nb_streams-1];
  1122. sc = st->priv_data;
  1123. get_byte(pb); /* version */
  1124. get_be24(pb); /* flags */
  1125. if (atom.type == MKTAG('s','t','s','z')) {
  1126. sample_size = get_be32(pb);
  1127. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1128. sc->sample_size = sample_size;
  1129. field_size = 32;
  1130. } else {
  1131. sample_size = 0;
  1132. get_be24(pb); /* reserved */
  1133. field_size = get_byte(pb);
  1134. }
  1135. entries = get_be32(pb);
  1136. dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1137. sc->sample_count = entries;
  1138. if (sample_size)
  1139. return 0;
  1140. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  1141. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
  1142. return -1;
  1143. }
  1144. if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
  1145. return -1;
  1146. sc->sample_sizes = av_malloc(entries * sizeof(int));
  1147. if (!sc->sample_sizes)
  1148. return AVERROR(ENOMEM);
  1149. num_bytes = (entries*field_size+4)>>3;
  1150. buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
  1151. if (!buf) {
  1152. av_freep(&sc->sample_sizes);
  1153. return AVERROR(ENOMEM);
  1154. }
  1155. if (get_buffer(pb, buf, num_bytes) < num_bytes) {
  1156. av_freep(&sc->sample_sizes);
  1157. av_free(buf);
  1158. return -1;
  1159. }
  1160. init_get_bits(&gb, buf, 8*num_bytes);
  1161. for(i=0; i<entries; i++)
  1162. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  1163. av_free(buf);
  1164. return 0;
  1165. }
  1166. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1167. {
  1168. AVStream *st;
  1169. MOVStreamContext *sc;
  1170. unsigned int i, entries;
  1171. int64_t duration=0;
  1172. int64_t total_sample_count=0;
  1173. if (c->fc->nb_streams < 1)
  1174. return 0;
  1175. st = c->fc->streams[c->fc->nb_streams-1];
  1176. sc = st->priv_data;
  1177. get_byte(pb); /* version */
  1178. get_be24(pb); /* flags */
  1179. entries = get_be32(pb);
  1180. dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1181. if(entries >= UINT_MAX / sizeof(*sc->stts_data))
  1182. return -1;
  1183. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1184. if (!sc->stts_data)
  1185. return AVERROR(ENOMEM);
  1186. sc->stts_count = entries;
  1187. for(i=0; i<entries; i++) {
  1188. int sample_duration;
  1189. int sample_count;
  1190. sample_count=get_be32(pb);
  1191. sample_duration = get_be32(pb);
  1192. sc->stts_data[i].count= sample_count;
  1193. sc->stts_data[i].duration= sample_duration;
  1194. dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1195. duration+=(int64_t)sample_duration*sample_count;
  1196. total_sample_count+=sample_count;
  1197. }
  1198. st->nb_frames= total_sample_count;
  1199. if(duration)
  1200. st->duration= duration;
  1201. return 0;
  1202. }
  1203. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1204. {
  1205. AVStream *st;
  1206. MOVStreamContext *sc;
  1207. unsigned int i, entries;
  1208. if (c->fc->nb_streams < 1)
  1209. return 0;
  1210. st = c->fc->streams[c->fc->nb_streams-1];
  1211. sc = st->priv_data;
  1212. get_byte(pb); /* version */
  1213. get_be24(pb); /* flags */
  1214. entries = get_be32(pb);
  1215. dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1216. if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1217. return -1;
  1218. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1219. if (!sc->ctts_data)
  1220. return AVERROR(ENOMEM);
  1221. sc->ctts_count = entries;
  1222. for(i=0; i<entries; i++) {
  1223. int count =get_be32(pb);
  1224. int duration =get_be32(pb);
  1225. sc->ctts_data[i].count = count;
  1226. sc->ctts_data[i].duration= duration;
  1227. if (duration < 0)
  1228. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  1229. }
  1230. dprintf(c->fc, "dts shift %d\n", sc->dts_shift);
  1231. return 0;
  1232. }
  1233. static void mov_build_index(MOVContext *mov, AVStream *st)
  1234. {
  1235. MOVStreamContext *sc = st->priv_data;
  1236. int64_t current_offset;
  1237. int64_t current_dts = 0;
  1238. unsigned int stts_index = 0;
  1239. unsigned int stsc_index = 0;
  1240. unsigned int stss_index = 0;
  1241. unsigned int stps_index = 0;
  1242. unsigned int i, j;
  1243. uint64_t stream_size = 0;
  1244. /* adjust first dts according to edit list */
  1245. if (sc->time_offset) {
  1246. int rescaled = sc->time_offset < 0 ? av_rescale(sc->time_offset, sc->time_scale, mov->time_scale) : sc->time_offset;
  1247. current_dts = -rescaled;
  1248. if (sc->ctts_data && sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
  1249. /* more than 16 frames delay, dts are likely wrong
  1250. this happens with files created by iMovie */
  1251. sc->wrong_dts = 1;
  1252. st->codec->has_b_frames = 1;
  1253. }
  1254. }
  1255. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1256. if (!(st->codec->codec_type == CODEC_TYPE_AUDIO &&
  1257. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1258. unsigned int current_sample = 0;
  1259. unsigned int stts_sample = 0;
  1260. unsigned int sample_size;
  1261. unsigned int distance = 0;
  1262. int key_off = sc->keyframes && sc->keyframes[0] == 1;
  1263. current_dts -= sc->dts_shift;
  1264. for (i = 0; i < sc->chunk_count; i++) {
  1265. current_offset = sc->chunk_offsets[i];
  1266. if (stsc_index + 1 < sc->stsc_count &&
  1267. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1268. stsc_index++;
  1269. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  1270. int keyframe = 0;
  1271. if (current_sample >= sc->sample_count) {
  1272. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1273. return;
  1274. }
  1275. if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) {
  1276. keyframe = 1;
  1277. if (stss_index + 1 < sc->keyframe_count)
  1278. stss_index++;
  1279. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  1280. keyframe = 1;
  1281. if (stps_index + 1 < sc->stps_count)
  1282. stps_index++;
  1283. }
  1284. if (keyframe)
  1285. distance = 0;
  1286. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1287. if(sc->pseudo_stream_id == -1 ||
  1288. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1289. av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
  1290. keyframe ? AVINDEX_KEYFRAME : 0);
  1291. dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1292. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1293. current_offset, current_dts, sample_size, distance, keyframe);
  1294. }
  1295. current_offset += sample_size;
  1296. stream_size += sample_size;
  1297. current_dts += sc->stts_data[stts_index].duration;
  1298. distance++;
  1299. stts_sample++;
  1300. current_sample++;
  1301. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1302. stts_sample = 0;
  1303. stts_index++;
  1304. }
  1305. }
  1306. }
  1307. if (st->duration > 0)
  1308. st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
  1309. } else {
  1310. for (i = 0; i < sc->chunk_count; i++) {
  1311. unsigned chunk_samples;
  1312. current_offset = sc->chunk_offsets[i];
  1313. if (stsc_index + 1 < sc->stsc_count &&
  1314. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1315. stsc_index++;
  1316. chunk_samples = sc->stsc_data[stsc_index].count;
  1317. if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  1318. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  1319. return;
  1320. }
  1321. while (chunk_samples > 0) {
  1322. unsigned size, samples;
  1323. if (sc->samples_per_frame >= 160) { // gsm
  1324. samples = sc->samples_per_frame;
  1325. size = sc->bytes_per_frame;
  1326. } else {
  1327. if (sc->samples_per_frame > 1) {
  1328. samples = FFMIN((1024 / sc->samples_per_frame)*
  1329. sc->samples_per_frame, chunk_samples);
  1330. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  1331. } else {
  1332. samples = FFMIN(1024, chunk_samples);
  1333. size = samples * sc->sample_size;
  1334. }
  1335. }
  1336. av_add_index_entry(st, current_offset, current_dts, size, 0, AVINDEX_KEYFRAME);
  1337. dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1338. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1339. size, samples);
  1340. current_offset += size;
  1341. current_dts += samples;
  1342. chunk_samples -= samples;
  1343. }
  1344. }
  1345. }
  1346. }
  1347. static int mov_open_dref(ByteIOContext **pb, char *src, MOVDref *ref)
  1348. {
  1349. /* try absolute path */
  1350. if (!url_fopen(pb, ref->path, URL_RDONLY))
  1351. return 0;
  1352. /* try relative path */
  1353. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  1354. char filename[1024];
  1355. char *src_path;
  1356. int i, l;
  1357. /* find a source dir */
  1358. src_path = strrchr(src, '/');
  1359. if (src_path)
  1360. src_path++;
  1361. else
  1362. src_path = src;
  1363. /* find a next level down to target */
  1364. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  1365. if (ref->path[l] == '/') {
  1366. if (i == ref->nlvl_to - 1)
  1367. break;
  1368. else
  1369. i++;
  1370. }
  1371. /* compose filename if next level down to target was found */
  1372. if (i == ref->nlvl_to - 1) {
  1373. memcpy(filename, src, src_path - src);
  1374. filename[src_path - src] = 0;
  1375. for (i = 1; i < ref->nlvl_from; i++)
  1376. av_strlcat(filename, "../", 1024);
  1377. av_strlcat(filename, ref->path + l + 1, 1024);
  1378. if (!url_fopen(pb, filename, URL_RDONLY))
  1379. return 0;
  1380. }
  1381. }
  1382. return AVERROR(ENOENT);
  1383. };
  1384. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1385. {
  1386. AVStream *st;
  1387. MOVStreamContext *sc;
  1388. int ret;
  1389. st = av_new_stream(c->fc, c->fc->nb_streams);
  1390. if (!st) return AVERROR(ENOMEM);
  1391. sc = av_mallocz(sizeof(MOVStreamContext));
  1392. if (!sc) return AVERROR(ENOMEM);
  1393. st->priv_data = sc;
  1394. st->codec->codec_type = CODEC_TYPE_DATA;
  1395. sc->ffindex = st->index;
  1396. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1397. return ret;
  1398. /* sanity checks */
  1399. if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  1400. (!sc->sample_size && !sc->sample_count))) {
  1401. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1402. st->index);
  1403. return 0;
  1404. }
  1405. if (!sc->time_scale) {
  1406. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
  1407. sc->time_scale = c->time_scale;
  1408. if (!sc->time_scale)
  1409. sc->time_scale = 1;
  1410. }
  1411. av_set_pts_info(st, 64, 1, sc->time_scale);
  1412. if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
  1413. !st->codec->frame_size && sc->stts_count == 1) {
  1414. st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
  1415. st->codec->sample_rate, sc->time_scale);
  1416. dprintf(c->fc, "frame size %d\n", st->codec->frame_size);
  1417. }
  1418. mov_build_index(c, st);
  1419. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1420. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  1421. if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0)
  1422. av_log(c->fc, AV_LOG_ERROR,
  1423. "stream %d, error opening alias: path='%s', dir='%s', "
  1424. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  1425. st->index, dref->path, dref->dir, dref->filename,
  1426. dref->volume, dref->nlvl_from, dref->nlvl_to);
  1427. } else
  1428. sc->pb = c->fc->pb;
  1429. switch (st->codec->codec_id) {
  1430. #if CONFIG_H261_DECODER
  1431. case CODEC_ID_H261:
  1432. #endif
  1433. #if CONFIG_H263_DECODER
  1434. case CODEC_ID_H263:
  1435. #endif
  1436. #if CONFIG_H264_DECODER
  1437. case CODEC_ID_H264:
  1438. #endif
  1439. #if CONFIG_MPEG4_DECODER
  1440. case CODEC_ID_MPEG4:
  1441. #endif
  1442. st->codec->width = 0; /* let decoder init width/height */
  1443. st->codec->height= 0;
  1444. break;
  1445. }
  1446. /* Do not need those anymore. */
  1447. av_freep(&sc->chunk_offsets);
  1448. av_freep(&sc->stsc_data);
  1449. av_freep(&sc->sample_sizes);
  1450. av_freep(&sc->keyframes);
  1451. av_freep(&sc->stts_data);
  1452. av_freep(&sc->stps_data);
  1453. return 0;
  1454. }
  1455. static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1456. {
  1457. int ret;
  1458. c->itunes_metadata = 1;
  1459. ret = mov_read_default(c, pb, atom);
  1460. c->itunes_metadata = 0;
  1461. return ret;
  1462. }
  1463. static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1464. {
  1465. while (atom.size > 8) {
  1466. uint32_t tag = get_le32(pb);
  1467. atom.size -= 4;
  1468. if (tag == MKTAG('h','d','l','r')) {
  1469. url_fseek(pb, -8, SEEK_CUR);
  1470. atom.size += 8;
  1471. return mov_read_default(c, pb, atom);
  1472. }
  1473. }
  1474. return 0;
  1475. }
  1476. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1477. {
  1478. int i;
  1479. int width;
  1480. int height;
  1481. int64_t disp_transform[2];
  1482. int display_matrix[3][2];
  1483. AVStream *st;
  1484. MOVStreamContext *sc;
  1485. int version;
  1486. if (c->fc->nb_streams < 1)
  1487. return 0;
  1488. st = c->fc->streams[c->fc->nb_streams-1];
  1489. sc = st->priv_data;
  1490. version = get_byte(pb);
  1491. get_be24(pb); /* flags */
  1492. /*
  1493. MOV_TRACK_ENABLED 0x0001
  1494. MOV_TRACK_IN_MOVIE 0x0002
  1495. MOV_TRACK_IN_PREVIEW 0x0004
  1496. MOV_TRACK_IN_POSTER 0x0008
  1497. */
  1498. if (version == 1) {
  1499. get_be64(pb);
  1500. get_be64(pb);
  1501. } else {
  1502. get_be32(pb); /* creation time */
  1503. get_be32(pb); /* modification time */
  1504. }
  1505. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1506. get_be32(pb); /* reserved */
  1507. /* highlevel (considering edits) duration in movie timebase */
  1508. (version == 1) ? get_be64(pb) : get_be32(pb);
  1509. get_be32(pb); /* reserved */
  1510. get_be32(pb); /* reserved */
  1511. get_be16(pb); /* layer */
  1512. get_be16(pb); /* alternate group */
  1513. get_be16(pb); /* volume */
  1514. get_be16(pb); /* reserved */
  1515. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1516. // they're kept in fixed point format through all calculations
  1517. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1518. for (i = 0; i < 3; i++) {
  1519. display_matrix[i][0] = get_be32(pb); // 16.16 fixed point
  1520. display_matrix[i][1] = get_be32(pb); // 16.16 fixed point
  1521. get_be32(pb); // 2.30 fixed point (not used)
  1522. }
  1523. width = get_be32(pb); // 16.16 fixed point track width
  1524. height = get_be32(pb); // 16.16 fixed point track height
  1525. sc->width = width >> 16;
  1526. sc->height = height >> 16;
  1527. // transform the display width/height according to the matrix
  1528. // skip this if the display matrix is the default identity matrix
  1529. // or if it is rotating the picture, ex iPhone 3GS
  1530. // to keep the same scale, use [width height 1<<16]
  1531. if (width && height &&
  1532. ((display_matrix[0][0] != 65536 ||
  1533. display_matrix[1][1] != 65536) &&
  1534. !display_matrix[0][1] &&
  1535. !display_matrix[1][0] &&
  1536. !display_matrix[2][0] && !display_matrix[2][1])) {
  1537. for (i = 0; i < 2; i++)
  1538. disp_transform[i] =
  1539. (int64_t) width * display_matrix[0][i] +
  1540. (int64_t) height * display_matrix[1][i] +
  1541. ((int64_t) display_matrix[2][i] << 16);
  1542. //sample aspect ratio is new width/height divided by old width/height
  1543. st->sample_aspect_ratio = av_d2q(
  1544. ((double) disp_transform[0] * height) /
  1545. ((double) disp_transform[1] * width), INT_MAX);
  1546. }
  1547. return 0;
  1548. }
  1549. static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1550. {
  1551. MOVFragment *frag = &c->fragment;
  1552. MOVTrackExt *trex = NULL;
  1553. int flags, track_id, i;
  1554. get_byte(pb); /* version */
  1555. flags = get_be24(pb);
  1556. track_id = get_be32(pb);
  1557. if (!track_id)
  1558. return -1;
  1559. frag->track_id = track_id;
  1560. for (i = 0; i < c->trex_count; i++)
  1561. if (c->trex_data[i].track_id == frag->track_id) {
  1562. trex = &c->trex_data[i];
  1563. break;
  1564. }
  1565. if (!trex) {
  1566. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1567. return -1;
  1568. }
  1569. if (flags & 0x01) frag->base_data_offset = get_be64(pb);
  1570. else frag->base_data_offset = frag->moof_offset;
  1571. if (flags & 0x02) frag->stsd_id = get_be32(pb);
  1572. else frag->stsd_id = trex->stsd_id;
  1573. frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
  1574. frag->size = flags & 0x10 ? get_be32(pb) : trex->size;
  1575. frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags;
  1576. dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
  1577. return 0;
  1578. }
  1579. static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1580. {
  1581. MOVTrackExt *trex;
  1582. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1583. return -1;
  1584. trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1585. if (!trex)
  1586. return AVERROR(ENOMEM);
  1587. c->trex_data = trex;
  1588. trex = &c->trex_data[c->trex_count++];
  1589. get_byte(pb); /* version */
  1590. get_be24(pb); /* flags */
  1591. trex->track_id = get_be32(pb);
  1592. trex->stsd_id = get_be32(pb);
  1593. trex->duration = get_be32(pb);
  1594. trex->size = get_be32(pb);
  1595. trex->flags = get_be32(pb);
  1596. return 0;
  1597. }
  1598. static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1599. {
  1600. MOVFragment *frag = &c->fragment;
  1601. AVStream *st = NULL;
  1602. MOVStreamContext *sc;
  1603. uint64_t offset;
  1604. int64_t dts;
  1605. int data_offset = 0;
  1606. unsigned entries, first_sample_flags = frag->flags;
  1607. int flags, distance, i;
  1608. for (i = 0; i < c->fc->nb_streams; i++) {
  1609. if (c->fc->streams[i]->id == frag->track_id) {
  1610. st = c->fc->streams[i];
  1611. break;
  1612. }
  1613. }
  1614. if (!st) {
  1615. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  1616. return -1;
  1617. }
  1618. sc = st->priv_data;
  1619. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1620. return 0;
  1621. get_byte(pb); /* version */
  1622. flags = get_be24(pb);
  1623. entries = get_be32(pb);
  1624. dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1625. if (flags & 0x001) data_offset = get_be32(pb);
  1626. if (flags & 0x004) first_sample_flags = get_be32(pb);
  1627. if (flags & 0x800) {
  1628. MOVStts *ctts_data;
  1629. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  1630. return -1;
  1631. ctts_data = av_realloc(sc->ctts_data,
  1632. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  1633. if (!ctts_data)
  1634. return AVERROR(ENOMEM);
  1635. sc->ctts_data = ctts_data;
  1636. }
  1637. dts = st->duration;
  1638. offset = frag->base_data_offset + data_offset;
  1639. distance = 0;
  1640. dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  1641. for (i = 0; i < entries; i++) {
  1642. unsigned sample_size = frag->size;
  1643. int sample_flags = i ? frag->flags : first_sample_flags;
  1644. unsigned sample_duration = frag->duration;
  1645. int keyframe;
  1646. if (flags & 0x100) sample_duration = get_be32(pb);
  1647. if (flags & 0x200) sample_size = get_be32(pb);
  1648. if (flags & 0x400) sample_flags = get_be32(pb);
  1649. if (flags & 0x800) {
  1650. sc->ctts_data[sc->ctts_count].count = 1;
  1651. sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
  1652. sc->ctts_count++;
  1653. }
  1654. if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||
  1655. (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
  1656. distance = 0;
  1657. av_add_index_entry(st, offset, dts, sample_size, distance,
  1658. keyframe ? AVINDEX_KEYFRAME : 0);
  1659. dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1660. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  1661. offset, dts, sample_size, distance, keyframe);
  1662. distance++;
  1663. dts += sample_duration;
  1664. offset += sample_size;
  1665. }
  1666. frag->moof_offset = offset;
  1667. st->duration = dts;
  1668. return 0;
  1669. }
  1670. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1671. /* like the files created with Adobe Premiere 5.0, for samples see */
  1672. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1673. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1674. {
  1675. int err;
  1676. if (atom.size < 8)
  1677. return 0; /* continue */
  1678. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1679. url_fskip(pb, atom.size - 4);
  1680. return 0;
  1681. }
  1682. atom.type = get_le32(pb);
  1683. atom.offset += 8;
  1684. atom.size -= 8;
  1685. if (atom.type != MKTAG('m','d','a','t')) {
  1686. url_fskip(pb, atom.size);
  1687. return 0;
  1688. }
  1689. err = mov_read_mdat(c, pb, atom);
  1690. return err;
  1691. }
  1692. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1693. {
  1694. #if CONFIG_ZLIB
  1695. ByteIOContext ctx;
  1696. uint8_t *cmov_data;
  1697. uint8_t *moov_data; /* uncompressed data */
  1698. long cmov_len, moov_len;
  1699. int ret = -1;
  1700. get_be32(pb); /* dcom atom */
  1701. if (get_le32(pb) != MKTAG('d','c','o','m'))
  1702. return -1;
  1703. if (get_le32(pb) != MKTAG('z','l','i','b')) {
  1704. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1705. return -1;
  1706. }
  1707. get_be32(pb); /* cmvd atom */
  1708. if (get_le32(pb) != MKTAG('c','m','v','d'))
  1709. return -1;
  1710. moov_len = get_be32(pb); /* uncompressed size */
  1711. cmov_len = atom.size - 6 * 4;
  1712. cmov_data = av_malloc(cmov_len);
  1713. if (!cmov_data)
  1714. return AVERROR(ENOMEM);
  1715. moov_data = av_malloc(moov_len);
  1716. if (!moov_data) {
  1717. av_free(cmov_data);
  1718. return AVERROR(ENOMEM);
  1719. }
  1720. get_buffer(pb, cmov_data, cmov_len);
  1721. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1722. goto free_and_return;
  1723. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1724. goto free_and_return;
  1725. atom.type = MKTAG('m','o','o','v');
  1726. atom.offset = 0;
  1727. atom.size = moov_len;
  1728. #ifdef DEBUG
  1729. // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1730. #endif
  1731. ret = mov_read_default(c, &ctx, atom);
  1732. free_and_return:
  1733. av_free(moov_data);
  1734. av_free(cmov_data);
  1735. return ret;
  1736. #else
  1737. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1738. return -1;
  1739. #endif
  1740. }
  1741. /* edit list atom */
  1742. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
  1743. {
  1744. MOVStreamContext *sc;
  1745. int i, edit_count;
  1746. if (c->fc->nb_streams < 1)
  1747. return 0;
  1748. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  1749. get_byte(pb); /* version */
  1750. get_be24(pb); /* flags */
  1751. edit_count = get_be32(pb); /* entries */
  1752. if((uint64_t)edit_count*12+8 > atom.size)
  1753. return -1;
  1754. for(i=0; i<edit_count; i++){
  1755. int time;
  1756. int duration = get_be32(pb); /* Track duration */
  1757. time = get_be32(pb); /* Media time */
  1758. get_be32(pb); /* Media rate */
  1759. if (i == 0 && time >= -1) {
  1760. sc->time_offset = time != -1 ? time : -duration;
  1761. }
  1762. }
  1763. if(edit_count > 1)
  1764. av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
  1765. "a/v desync might occur, patch welcome\n");
  1766. dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  1767. return 0;
  1768. }
  1769. static const MOVParseTableEntry mov_default_parse_table[] = {
  1770. { MKTAG('a','v','s','s'), mov_read_extradata },
  1771. { MKTAG('c','o','6','4'), mov_read_stco },
  1772. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  1773. { MKTAG('d','i','n','f'), mov_read_default },
  1774. { MKTAG('d','r','e','f'), mov_read_dref },
  1775. { MKTAG('e','d','t','s'), mov_read_default },
  1776. { MKTAG('e','l','s','t'), mov_read_elst },
  1777. { MKTAG('e','n','d','a'), mov_read_enda },
  1778. { MKTAG('f','i','e','l'), mov_read_extradata },
  1779. { MKTAG('f','t','y','p'), mov_read_ftyp },
  1780. { MKTAG('g','l','b','l'), mov_read_glbl },
  1781. { MKTAG('h','d','l','r'), mov_read_hdlr },
  1782. { MKTAG('i','l','s','t'), mov_read_ilst },
  1783. { MKTAG('j','p','2','h'), mov_read_extradata },
  1784. { MKTAG('m','d','a','t'), mov_read_mdat },
  1785. { MKTAG('m','d','h','d'), mov_read_mdhd },
  1786. { MKTAG('m','d','i','a'), mov_read_default },
  1787. { MKTAG('m','e','t','a'), mov_read_meta },
  1788. { MKTAG('m','i','n','f'), mov_read_default },
  1789. { MKTAG('m','o','o','f'), mov_read_moof },
  1790. { MKTAG('m','o','o','v'), mov_read_moov },
  1791. { MKTAG('m','v','e','x'), mov_read_default },
  1792. { MKTAG('m','v','h','d'), mov_read_mvhd },
  1793. { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
  1794. { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
  1795. { MKTAG('a','v','c','C'), mov_read_glbl },
  1796. { MKTAG('p','a','s','p'), mov_read_pasp },
  1797. { MKTAG('s','t','b','l'), mov_read_default },
  1798. { MKTAG('s','t','c','o'), mov_read_stco },
  1799. { MKTAG('s','t','p','s'), mov_read_stps },
  1800. { MKTAG('s','t','s','c'), mov_read_stsc },
  1801. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  1802. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  1803. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  1804. { MKTAG('s','t','t','s'), mov_read_stts },
  1805. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  1806. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  1807. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  1808. { MKTAG('t','r','a','k'), mov_read_trak },
  1809. { MKTAG('t','r','a','f'), mov_read_default },
  1810. { MKTAG('t','r','e','x'), mov_read_trex },
  1811. { MKTAG('t','r','u','n'), mov_read_trun },
  1812. { MKTAG('u','d','t','a'), mov_read_default },
  1813. { MKTAG('w','a','v','e'), mov_read_wave },
  1814. { MKTAG('e','s','d','s'), mov_read_esds },
  1815. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  1816. { MKTAG('c','m','o','v'), mov_read_cmov },
  1817. { 0, NULL }
  1818. };
  1819. static int mov_probe(AVProbeData *p)
  1820. {
  1821. unsigned int offset;
  1822. uint32_t tag;
  1823. int score = 0;
  1824. /* check file header */
  1825. offset = 0;
  1826. for(;;) {
  1827. /* ignore invalid offset */
  1828. if ((offset + 8) > (unsigned int)p->buf_size)
  1829. return score;
  1830. tag = AV_RL32(p->buf + offset + 4);
  1831. switch(tag) {
  1832. /* check for obvious tags */
  1833. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  1834. case MKTAG('m','o','o','v'):
  1835. case MKTAG('m','d','a','t'):
  1836. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  1837. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  1838. case MKTAG('f','t','y','p'):
  1839. return AVPROBE_SCORE_MAX;
  1840. /* those are more common words, so rate then a bit less */
  1841. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  1842. case MKTAG('w','i','d','e'):
  1843. case MKTAG('f','r','e','e'):
  1844. case MKTAG('j','u','n','k'):
  1845. case MKTAG('p','i','c','t'):
  1846. return AVPROBE_SCORE_MAX - 5;
  1847. case MKTAG(0x82,0x82,0x7f,0x7d):
  1848. case MKTAG('s','k','i','p'):
  1849. case MKTAG('u','u','i','d'):
  1850. case MKTAG('p','r','f','l'):
  1851. offset = AV_RB32(p->buf+offset) + offset;
  1852. /* if we only find those cause probedata is too small at least rate them */
  1853. score = AVPROBE_SCORE_MAX - 50;
  1854. break;
  1855. default:
  1856. /* unrecognized tag */
  1857. return score;
  1858. }
  1859. }
  1860. return score;
  1861. }
  1862. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1863. {
  1864. MOVContext *mov = s->priv_data;
  1865. ByteIOContext *pb = s->pb;
  1866. int err;
  1867. MOVAtom atom = { 0, 0, 0 };
  1868. mov->fc = s;
  1869. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1870. if(!url_is_streamed(pb))
  1871. atom.size = url_fsize(pb);
  1872. else
  1873. atom.size = INT64_MAX;
  1874. /* check MOV header */
  1875. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  1876. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  1877. return err;
  1878. }
  1879. if (!mov->found_moov) {
  1880. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  1881. return -1;
  1882. }
  1883. dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
  1884. return 0;
  1885. }
  1886. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  1887. {
  1888. AVIndexEntry *sample = NULL;
  1889. int64_t best_dts = INT64_MAX;
  1890. int i;
  1891. for (i = 0; i < s->nb_streams; i++) {
  1892. AVStream *avst = s->streams[i];
  1893. MOVStreamContext *msc = avst->priv_data;
  1894. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  1895. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  1896. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  1897. dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  1898. if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
  1899. (!url_is_streamed(s->pb) &&
  1900. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  1901. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  1902. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  1903. sample = current_sample;
  1904. best_dts = dts;
  1905. *st = avst;
  1906. }
  1907. }
  1908. }
  1909. return sample;
  1910. }
  1911. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1912. {
  1913. MOVContext *mov = s->priv_data;
  1914. MOVStreamContext *sc;
  1915. AVIndexEntry *sample;
  1916. AVStream *st = NULL;
  1917. int ret;
  1918. retry:
  1919. sample = mov_find_next_sample(s, &st);
  1920. if (!sample) {
  1921. mov->found_mdat = 0;
  1922. if (!url_is_streamed(s->pb) ||
  1923. mov_read_default(mov, s->pb, (MOVAtom){ 0, 0, INT64_MAX }) < 0 ||
  1924. url_feof(s->pb))
  1925. return AVERROR_EOF;
  1926. dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
  1927. goto retry;
  1928. }
  1929. sc = st->priv_data;
  1930. /* must be done just before reading, to avoid infinite loop on sample */
  1931. sc->current_sample++;
  1932. if (st->discard != AVDISCARD_ALL) {
  1933. if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  1934. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  1935. sc->ffindex, sample->pos);
  1936. return -1;
  1937. }
  1938. ret = av_get_packet(sc->pb, pkt, sample->size);
  1939. if (ret < 0)
  1940. return ret;
  1941. #if CONFIG_DV_DEMUXER
  1942. if (mov->dv_demux && sc->dv_audio_container) {
  1943. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
  1944. av_free(pkt->data);
  1945. pkt->size = 0;
  1946. ret = dv_get_packet(mov->dv_demux, pkt);
  1947. if (ret < 0)
  1948. return ret;
  1949. }
  1950. #endif
  1951. }
  1952. pkt->stream_index = sc->ffindex;
  1953. pkt->dts = sample->timestamp;
  1954. if (sc->ctts_data) {
  1955. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  1956. /* update ctts context */
  1957. sc->ctts_sample++;
  1958. if (sc->ctts_index < sc->ctts_count &&
  1959. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  1960. sc->ctts_index++;
  1961. sc->ctts_sample = 0;
  1962. }
  1963. if (sc->wrong_dts)
  1964. pkt->dts = AV_NOPTS_VALUE;
  1965. } else {
  1966. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  1967. st->index_entries[sc->current_sample].timestamp : st->duration;
  1968. pkt->duration = next_dts - pkt->dts;
  1969. pkt->pts = pkt->dts;
  1970. }
  1971. if (st->discard == AVDISCARD_ALL)
  1972. goto retry;
  1973. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
  1974. pkt->pos = sample->pos;
  1975. dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  1976. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  1977. return 0;
  1978. }
  1979. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  1980. {
  1981. MOVStreamContext *sc = st->priv_data;
  1982. int sample, time_sample;
  1983. int i;
  1984. sample = av_index_search_timestamp(st, timestamp, flags);
  1985. dprintf(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  1986. if (sample < 0) /* not sure what to do */
  1987. return -1;
  1988. sc->current_sample = sample;
  1989. dprintf(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
  1990. /* adjust ctts index */
  1991. if (sc->ctts_data) {
  1992. time_sample = 0;
  1993. for (i = 0; i < sc->ctts_count; i++) {
  1994. int next = time_sample + sc->ctts_data[i].count;
  1995. if (next > sc->current_sample) {
  1996. sc->ctts_index = i;
  1997. sc->ctts_sample = sc->current_sample - time_sample;
  1998. break;
  1999. }
  2000. time_sample = next;
  2001. }
  2002. }
  2003. return sample;
  2004. }
  2005. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  2006. {
  2007. AVStream *st;
  2008. int64_t seek_timestamp, timestamp;
  2009. int sample;
  2010. int i;
  2011. if (stream_index >= s->nb_streams)
  2012. return -1;
  2013. if (sample_time < 0)
  2014. sample_time = 0;
  2015. st = s->streams[stream_index];
  2016. sample = mov_seek_stream(s, st, sample_time, flags);
  2017. if (sample < 0)
  2018. return -1;
  2019. /* adjust seek timestamp to found sample timestamp */
  2020. seek_timestamp = st->index_entries[sample].timestamp;
  2021. for (i = 0; i < s->nb_streams; i++) {
  2022. st = s->streams[i];
  2023. if (stream_index == i)
  2024. continue;
  2025. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  2026. mov_seek_stream(s, st, timestamp, flags);
  2027. }
  2028. return 0;
  2029. }
  2030. static int mov_read_close(AVFormatContext *s)
  2031. {
  2032. MOVContext *mov = s->priv_data;
  2033. int i, j;
  2034. for (i = 0; i < s->nb_streams; i++) {
  2035. AVStream *st = s->streams[i];
  2036. MOVStreamContext *sc = st->priv_data;
  2037. av_freep(&sc->ctts_data);
  2038. for (j = 0; j < sc->drefs_count; j++) {
  2039. av_freep(&sc->drefs[j].path);
  2040. av_freep(&sc->drefs[j].dir);
  2041. }
  2042. av_freep(&sc->drefs);
  2043. if (sc->pb && sc->pb != s->pb)
  2044. url_fclose(sc->pb);
  2045. av_freep(&st->codec->palctrl);
  2046. }
  2047. if (mov->dv_demux) {
  2048. for(i = 0; i < mov->dv_fctx->nb_streams; i++) {
  2049. av_freep(&mov->dv_fctx->streams[i]->codec);
  2050. av_freep(&mov->dv_fctx->streams[i]);
  2051. }
  2052. av_freep(&mov->dv_fctx);
  2053. av_freep(&mov->dv_demux);
  2054. }
  2055. av_freep(&mov->trex_data);
  2056. return 0;
  2057. }
  2058. AVInputFormat mov_demuxer = {
  2059. "mov,mp4,m4a,3gp,3g2,mj2",
  2060. NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
  2061. sizeof(MOVContext),
  2062. mov_probe,
  2063. mov_read_header,
  2064. mov_read_packet,
  2065. mov_read_close,
  2066. mov_read_seek,
  2067. };