mov.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /*
  2. * MOV demuxer
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <limits.h>
  22. //#define DEBUG
  23. #include "avformat.h"
  24. #include "riff.h"
  25. #include "isom.h"
  26. #include "dv.h"
  27. #ifdef CONFIG_ZLIB
  28. #include <zlib.h>
  29. #endif
  30. /*
  31. * First version by Francois Revol revol@free.fr
  32. * Seek function by Gael Chardon gael.dev@4now.net
  33. *
  34. * Features and limitations:
  35. * - reads most of the QT files I have (at least the structure),
  36. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  37. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  38. *
  39. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  40. * when coding this :) (it's a writer anyway)
  41. *
  42. * Reference documents:
  43. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  44. * Apple:
  45. * http://developer.apple.com/documentation/QuickTime/QTFF/
  46. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  47. * QuickTime is a trademark of Apple (AFAIK :))
  48. */
  49. #include "qtpalette.h"
  50. #undef NDEBUG
  51. #include <assert.h>
  52. /* the QuickTime file format is quite convoluted...
  53. * it has lots of index tables, each indexing something in another one...
  54. * Here we just use what is needed to read the chunks
  55. */
  56. typedef struct {
  57. int first;
  58. int count;
  59. int id;
  60. } MOV_stsc_t;
  61. typedef struct {
  62. uint32_t type;
  63. char *path;
  64. } MOV_dref_t;
  65. typedef struct {
  66. uint32_t type;
  67. int64_t offset;
  68. int64_t size; /* total size (excluding the size and type fields) */
  69. } MOV_atom_t;
  70. struct MOVParseTableEntry;
  71. typedef struct MOVStreamContext {
  72. ByteIOContext *pb;
  73. int ffindex; /* the ffmpeg stream id */
  74. int next_chunk;
  75. unsigned int chunk_count;
  76. int64_t *chunk_offsets;
  77. unsigned int stts_count;
  78. MOV_stts_t *stts_data;
  79. unsigned int ctts_count;
  80. MOV_stts_t *ctts_data;
  81. unsigned int edit_count; /* number of 'edit' (elst atom) */
  82. unsigned int sample_to_chunk_sz;
  83. MOV_stsc_t *sample_to_chunk;
  84. int sample_to_ctime_index;
  85. int sample_to_ctime_sample;
  86. unsigned int sample_size;
  87. unsigned int sample_count;
  88. int *sample_sizes;
  89. unsigned int keyframe_count;
  90. int *keyframes;
  91. int time_scale;
  92. int time_rate;
  93. int current_sample;
  94. unsigned int bytes_per_frame;
  95. unsigned int samples_per_frame;
  96. int dv_audio_container;
  97. int pseudo_stream_id;
  98. int16_t audio_cid; ///< stsd audio compression id
  99. unsigned drefs_count;
  100. MOV_dref_t *drefs;
  101. int dref_id;
  102. } MOVStreamContext;
  103. typedef struct MOVContext {
  104. AVFormatContext *fc;
  105. int time_scale;
  106. int64_t duration; /* duration of the longest track */
  107. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  108. int found_mdat; /* we suppose we have enough data to read the file */
  109. AVPaletteControl palette_control;
  110. DVDemuxContext *dv_demux;
  111. AVFormatContext *dv_fctx;
  112. int isom; /* 1 if file is ISO Media (mp4/3gp) */
  113. } MOVContext;
  114. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  115. /* those functions parse an atom */
  116. /* return code:
  117. 1: found what I wanted, exit
  118. 0: continue to parse next atom
  119. -1: error occured, exit
  120. */
  121. /* links atom IDs to parse functions */
  122. typedef struct MOVParseTableEntry {
  123. uint32_t type;
  124. int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
  125. } MOVParseTableEntry;
  126. static const MOVParseTableEntry mov_default_parse_table[];
  127. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  128. {
  129. int64_t total_size = 0;
  130. MOV_atom_t a;
  131. int i;
  132. int err = 0;
  133. a.offset = atom.offset;
  134. if (atom.size < 0)
  135. atom.size = INT64_MAX;
  136. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  137. a.size = atom.size;
  138. a.type=0;
  139. if(atom.size >= 8) {
  140. a.size = get_be32(pb);
  141. a.type = get_le32(pb);
  142. }
  143. total_size += 8;
  144. a.offset += 8;
  145. dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n",
  146. a.type, (char*)&a.type, a.size, atom.size, total_size);
  147. if (a.size == 1) { /* 64 bit extended size */
  148. a.size = get_be64(pb) - 8;
  149. a.offset += 8;
  150. total_size += 8;
  151. }
  152. if (a.size == 0) {
  153. a.size = atom.size - total_size;
  154. if (a.size <= 8)
  155. break;
  156. }
  157. a.size -= 8;
  158. if(a.size < 0)
  159. break;
  160. a.size = FFMIN(a.size, atom.size - total_size);
  161. for (i = 0; mov_default_parse_table[i].type != 0
  162. && mov_default_parse_table[i].type != a.type; i++)
  163. /* empty */;
  164. if (mov_default_parse_table[i].type == 0) { /* skip leaf atoms data */
  165. url_fskip(pb, a.size);
  166. } else {
  167. offset_t start_pos = url_ftell(pb);
  168. int64_t left;
  169. err = mov_default_parse_table[i].parse(c, pb, a);
  170. if (c->found_moov && c->found_mdat)
  171. break;
  172. left = a.size - url_ftell(pb) + start_pos;
  173. if (left > 0) /* skip garbage at atom end */
  174. url_fskip(pb, left);
  175. }
  176. a.offset += a.size;
  177. total_size += a.size;
  178. }
  179. if (!err && total_size < atom.size && atom.size < 0x7ffff) {
  180. url_fskip(pb, atom.size - total_size);
  181. }
  182. return err;
  183. }
  184. static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  185. {
  186. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  187. MOVStreamContext *sc = st->priv_data;
  188. int entries, i, j;
  189. get_be32(pb); // version + flags
  190. entries = get_be32(pb);
  191. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  192. return -1;
  193. sc->drefs_count = entries;
  194. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  195. for (i = 0; i < sc->drefs_count; i++) {
  196. MOV_dref_t *dref = &sc->drefs[i];
  197. uint32_t size = get_be32(pb);
  198. offset_t next = url_ftell(pb) + size - 4;
  199. dref->type = get_le32(pb);
  200. get_be32(pb); // version + flags
  201. dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  202. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  203. /* macintosh alias record */
  204. uint16_t volume_len, len;
  205. char volume[28];
  206. int16_t type;
  207. url_fskip(pb, 10);
  208. volume_len = get_byte(pb);
  209. volume_len = FFMIN(volume_len, 27);
  210. get_buffer(pb, volume, 27);
  211. volume[volume_len] = 0;
  212. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", volume, volume_len);
  213. url_fskip(pb, 112);
  214. for (type = 0; type != -1 && url_ftell(pb) < next; ) {
  215. type = get_be16(pb);
  216. len = get_be16(pb);
  217. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  218. if (len&1)
  219. len += 1;
  220. if (type == 2) { // absolute path
  221. av_free(dref->path);
  222. dref->path = av_mallocz(len+1);
  223. if (!dref->path)
  224. return AVERROR(ENOMEM);
  225. get_buffer(pb, dref->path, len);
  226. if (len > volume_len && !strncmp(dref->path, volume, volume_len)) {
  227. len -= volume_len;
  228. memmove(dref->path, dref->path+volume_len, len);
  229. dref->path[len] = 0;
  230. }
  231. for (j = 0; j < len; j++)
  232. if (dref->path[j] == ':')
  233. dref->path[j] = '/';
  234. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  235. } else
  236. url_fskip(pb, len);
  237. }
  238. }
  239. url_fseek(pb, next, SEEK_SET);
  240. }
  241. return 0;
  242. }
  243. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  244. {
  245. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  246. uint32_t type;
  247. uint32_t ctype;
  248. get_byte(pb); /* version */
  249. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  250. /* component type */
  251. ctype = get_le32(pb);
  252. type = get_le32(pb); /* component subtype */
  253. dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1],
  254. ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
  255. dprintf(c->fc, "stype= %c%c%c%c\n",
  256. *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  257. if(!ctype)
  258. c->isom = 1;
  259. if(type == MKTAG('v', 'i', 'd', 'e'))
  260. st->codec->codec_type = CODEC_TYPE_VIDEO;
  261. else if(type == MKTAG('s', 'o', 'u', 'n'))
  262. st->codec->codec_type = CODEC_TYPE_AUDIO;
  263. else if(type == MKTAG('m', '1', 'a', ' '))
  264. st->codec->codec_id = CODEC_ID_MP2;
  265. else if(type == MKTAG('s', 'u', 'b', 'p')) {
  266. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  267. }
  268. get_be32(pb); /* component manufacture */
  269. get_be32(pb); /* component flags */
  270. get_be32(pb); /* component flags mask */
  271. if(atom.size <= 24)
  272. return 0; /* nothing left to read */
  273. url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
  274. return 0;
  275. }
  276. static int mp4_read_descr_len(ByteIOContext *pb)
  277. {
  278. int len = 0;
  279. int count = 4;
  280. while (count--) {
  281. int c = get_byte(pb);
  282. len = (len << 7) | (c & 0x7f);
  283. if (!(c & 0x80))
  284. break;
  285. }
  286. return len;
  287. }
  288. static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
  289. {
  290. int len;
  291. *tag = get_byte(pb);
  292. len = mp4_read_descr_len(pb);
  293. dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  294. return len;
  295. }
  296. #define MP4ESDescrTag 0x03
  297. #define MP4DecConfigDescrTag 0x04
  298. #define MP4DecSpecificDescrTag 0x05
  299. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  300. {
  301. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  302. int tag, len;
  303. get_be32(pb); /* version + flags */
  304. len = mp4_read_descr(c, pb, &tag);
  305. if (tag == MP4ESDescrTag) {
  306. get_be16(pb); /* ID */
  307. get_byte(pb); /* priority */
  308. } else
  309. get_be16(pb); /* ID */
  310. len = mp4_read_descr(c, pb, &tag);
  311. if (tag == MP4DecConfigDescrTag) {
  312. int object_type_id = get_byte(pb);
  313. get_byte(pb); /* stream type */
  314. get_be24(pb); /* buffer size db */
  315. get_be32(pb); /* max bitrate */
  316. get_be32(pb); /* avg bitrate */
  317. st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
  318. dprintf(c->fc, "esds object type id %d\n", object_type_id);
  319. len = mp4_read_descr(c, pb, &tag);
  320. if (tag == MP4DecSpecificDescrTag) {
  321. dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
  322. if((uint64_t)len > (1<<30))
  323. return -1;
  324. st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  325. if (!st->codec->extradata)
  326. return AVERROR(ENOMEM);
  327. get_buffer(pb, st->codec->extradata, len);
  328. st->codec->extradata_size = len;
  329. /* from mplayer */
  330. if ((*st->codec->extradata >> 3) == 29) {
  331. st->codec->codec_id = CODEC_ID_MP3ON4;
  332. }
  333. }
  334. }
  335. return 0;
  336. }
  337. /* this atom contains actual media data */
  338. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  339. {
  340. if(atom.size == 0) /* wrong one (MP4) */
  341. return 0;
  342. c->found_mdat=1;
  343. if(c->found_moov)
  344. return 1; /* found both, just go */
  345. url_fskip(pb, atom.size);
  346. return 0; /* now go for moov */
  347. }
  348. static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  349. {
  350. uint32_t type = get_le32(pb);
  351. if (type != MKTAG('q','t',' ',' '))
  352. c->isom = 1;
  353. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  354. get_be32(pb); /* minor version */
  355. url_fskip(pb, atom.size - 8);
  356. return 0;
  357. }
  358. /* this atom should contain all header atoms */
  359. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  360. {
  361. if (mov_read_default(c, pb, atom) < 0)
  362. return -1;
  363. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  364. /* so we don't parse the whole file if over a network */
  365. c->found_moov=1;
  366. if(c->found_mdat)
  367. return 1; /* found both, just go */
  368. return 0; /* now go for mdat */
  369. }
  370. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  371. {
  372. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  373. MOVStreamContext *sc = st->priv_data;
  374. int version = get_byte(pb);
  375. int lang;
  376. if (version > 1)
  377. return 1; /* unsupported */
  378. get_byte(pb); get_byte(pb);
  379. get_byte(pb); /* flags */
  380. if (version == 1) {
  381. get_be64(pb);
  382. get_be64(pb);
  383. } else {
  384. get_be32(pb); /* creation time */
  385. get_be32(pb); /* modification time */
  386. }
  387. sc->time_scale = get_be32(pb);
  388. st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  389. lang = get_be16(pb); /* language */
  390. ff_mov_lang_to_iso639(lang, st->language);
  391. get_be16(pb); /* quality */
  392. return 0;
  393. }
  394. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  395. {
  396. int version = get_byte(pb); /* version */
  397. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  398. if (version == 1) {
  399. get_be64(pb);
  400. get_be64(pb);
  401. } else {
  402. get_be32(pb); /* creation time */
  403. get_be32(pb); /* modification time */
  404. }
  405. c->time_scale = get_be32(pb); /* time scale */
  406. dprintf(c->fc, "time scale = %i\n", c->time_scale);
  407. c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
  408. get_be32(pb); /* preferred scale */
  409. get_be16(pb); /* preferred volume */
  410. url_fskip(pb, 10); /* reserved */
  411. url_fskip(pb, 36); /* display matrix */
  412. get_be32(pb); /* preview time */
  413. get_be32(pb); /* preview duration */
  414. get_be32(pb); /* poster time */
  415. get_be32(pb); /* selection time */
  416. get_be32(pb); /* selection duration */
  417. get_be32(pb); /* current time */
  418. get_be32(pb); /* next track ID */
  419. return 0;
  420. }
  421. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  422. {
  423. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  424. if((uint64_t)atom.size > (1<<30))
  425. return -1;
  426. // currently SVQ3 decoder expect full STSD header - so let's fake it
  427. // this should be fixed and just SMI header should be passed
  428. av_free(st->codec->extradata);
  429. st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
  430. if (!st->codec->extradata)
  431. return AVERROR(ENOMEM);
  432. st->codec->extradata_size = 0x5a + atom.size;
  433. memcpy(st->codec->extradata, "SVQ3", 4); // fake
  434. get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
  435. dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
  436. return 0;
  437. }
  438. static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  439. {
  440. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  441. int little_endian = get_be16(pb);
  442. if (little_endian) {
  443. switch (st->codec->codec_id) {
  444. case CODEC_ID_PCM_S24BE:
  445. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  446. break;
  447. case CODEC_ID_PCM_S32BE:
  448. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  449. break;
  450. default:
  451. break;
  452. }
  453. }
  454. return 0;
  455. }
  456. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  457. static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  458. {
  459. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  460. uint64_t size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  461. uint8_t *buf;
  462. if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  463. return -1;
  464. buf= av_realloc(st->codec->extradata, size);
  465. if(!buf)
  466. return -1;
  467. st->codec->extradata= buf;
  468. buf+= st->codec->extradata_size;
  469. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  470. AV_WB32( buf , atom.size + 8);
  471. AV_WL32( buf + 4, atom.type);
  472. get_buffer(pb, buf + 8, atom.size);
  473. return 0;
  474. }
  475. static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  476. {
  477. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  478. if((uint64_t)atom.size > (1<<30))
  479. return -1;
  480. if (st->codec->codec_id == CODEC_ID_QDM2) {
  481. // pass all frma atom to codec, needed at least for QDM2
  482. av_free(st->codec->extradata);
  483. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  484. if (!st->codec->extradata)
  485. return AVERROR(ENOMEM);
  486. st->codec->extradata_size = atom.size;
  487. get_buffer(pb, st->codec->extradata, atom.size);
  488. } else if (atom.size > 8) { /* to read frma, esds atoms */
  489. if (mov_read_default(c, pb, atom) < 0)
  490. return -1;
  491. } else
  492. url_fskip(pb, atom.size);
  493. return 0;
  494. }
  495. /**
  496. * This function reads atom content and puts data in extradata without tag
  497. * nor size unlike mov_read_extradata.
  498. */
  499. static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  500. {
  501. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  502. if((uint64_t)atom.size > (1<<30))
  503. return -1;
  504. av_free(st->codec->extradata);
  505. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  506. if (!st->codec->extradata)
  507. return AVERROR(ENOMEM);
  508. st->codec->extradata_size = atom.size;
  509. get_buffer(pb, st->codec->extradata, atom.size);
  510. return 0;
  511. }
  512. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  513. {
  514. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  515. MOVStreamContext *sc = st->priv_data;
  516. unsigned int i, entries;
  517. get_byte(pb); /* version */
  518. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  519. entries = get_be32(pb);
  520. if(entries >= UINT_MAX/sizeof(int64_t))
  521. return -1;
  522. sc->chunk_count = entries;
  523. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  524. if (!sc->chunk_offsets)
  525. return -1;
  526. if (atom.type == MKTAG('s', 't', 'c', 'o')) {
  527. for(i=0; i<entries; i++) {
  528. sc->chunk_offsets[i] = get_be32(pb);
  529. }
  530. } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
  531. for(i=0; i<entries; i++) {
  532. sc->chunk_offsets[i] = get_be64(pb);
  533. }
  534. } else
  535. return -1;
  536. return 0;
  537. }
  538. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  539. {
  540. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  541. MOVStreamContext *sc = st->priv_data;
  542. int entries, frames_per_sample;
  543. uint32_t format;
  544. uint8_t codec_name[32];
  545. /* for palette traversal */
  546. unsigned int color_depth;
  547. unsigned int color_start;
  548. unsigned int color_count;
  549. unsigned int color_end;
  550. int color_index;
  551. int color_dec;
  552. int color_greyscale;
  553. const uint8_t *color_table;
  554. int j, pseudo_stream_id;
  555. unsigned char r, g, b;
  556. get_byte(pb); /* version */
  557. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  558. entries = get_be32(pb);
  559. for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) { //Parsing Sample description table
  560. enum CodecID id;
  561. int dref_id;
  562. MOV_atom_t a = { 0, 0, 0 };
  563. offset_t start_pos = url_ftell(pb);
  564. int size = get_be32(pb); /* size */
  565. format = get_le32(pb); /* data format */
  566. get_be32(pb); /* reserved */
  567. get_be16(pb); /* reserved */
  568. dref_id = get_be16(pb);
  569. if (st->codec->codec_tag &&
  570. (c->fc->video_codec_id ? codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
  571. : st->codec->codec_tag != MKTAG('j', 'p', 'e', 'g'))
  572. ){
  573. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  574. * export it as a separate AVStream but this needs a few changes
  575. * in the MOV demuxer, patch welcome. */
  576. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  577. continue;
  578. }
  579. sc->pseudo_stream_id= pseudo_stream_id;
  580. sc->dref_id= dref_id;
  581. st->codec->codec_tag = format;
  582. id = codec_get_id(codec_movaudio_tags, format);
  583. if (id<=0 && (format&0xFFFF) == 'm' + ('s'<<8))
  584. id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF);
  585. if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
  586. st->codec->codec_type = CODEC_TYPE_AUDIO;
  587. } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
  588. format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
  589. id = codec_get_id(codec_movvideo_tags, format);
  590. if (id <= 0)
  591. id = codec_get_id(codec_bmp_tags, format);
  592. if (id > 0)
  593. st->codec->codec_type = CODEC_TYPE_VIDEO;
  594. else if(st->codec->codec_type == CODEC_TYPE_DATA){
  595. id = codec_get_id(ff_codec_movsubtitle_tags, format);
  596. if(id > 0)
  597. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  598. }
  599. }
  600. dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
  601. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  602. (format >> 24) & 0xff, st->codec->codec_type);
  603. if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
  604. st->codec->codec_id = id;
  605. get_be16(pb); /* version */
  606. get_be16(pb); /* revision level */
  607. get_be32(pb); /* vendor */
  608. get_be32(pb); /* temporal quality */
  609. get_be32(pb); /* spatial quality */
  610. st->codec->width = get_be16(pb); /* width */
  611. st->codec->height = get_be16(pb); /* height */
  612. get_be32(pb); /* horiz resolution */
  613. get_be32(pb); /* vert resolution */
  614. get_be32(pb); /* data size, always 0 */
  615. frames_per_sample = get_be16(pb); /* frames per samples */
  616. dprintf(c->fc, "frames/samples = %d\n", frames_per_sample);
  617. get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
  618. if (codec_name[0] <= 31) {
  619. memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
  620. st->codec->codec_name[codec_name[0]] = 0;
  621. }
  622. st->codec->bits_per_sample = get_be16(pb); /* depth */
  623. st->codec->color_table_id = get_be16(pb); /* colortable id */
  624. dprintf(c->fc, "depth %d, ctab id %d\n",
  625. st->codec->bits_per_sample, st->codec->color_table_id);
  626. /* figure out the palette situation */
  627. color_depth = st->codec->bits_per_sample & 0x1F;
  628. color_greyscale = st->codec->bits_per_sample & 0x20;
  629. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  630. if ((color_depth == 2) || (color_depth == 4) ||
  631. (color_depth == 8)) {
  632. if (color_greyscale) {
  633. /* compute the greyscale palette */
  634. st->codec->bits_per_sample = color_depth;
  635. color_count = 1 << color_depth;
  636. color_index = 255;
  637. color_dec = 256 / (color_count - 1);
  638. for (j = 0; j < color_count; j++) {
  639. r = g = b = color_index;
  640. c->palette_control.palette[j] =
  641. (r << 16) | (g << 8) | (b);
  642. color_index -= color_dec;
  643. if (color_index < 0)
  644. color_index = 0;
  645. }
  646. } else if (st->codec->color_table_id & 0x08) {
  647. /* if flag bit 3 is set, use the default palette */
  648. color_count = 1 << color_depth;
  649. if (color_depth == 2)
  650. color_table = ff_qt_default_palette_4;
  651. else if (color_depth == 4)
  652. color_table = ff_qt_default_palette_16;
  653. else
  654. color_table = ff_qt_default_palette_256;
  655. for (j = 0; j < color_count; j++) {
  656. r = color_table[j * 4 + 0];
  657. g = color_table[j * 4 + 1];
  658. b = color_table[j * 4 + 2];
  659. c->palette_control.palette[j] =
  660. (r << 16) | (g << 8) | (b);
  661. }
  662. } else {
  663. /* load the palette from the file */
  664. color_start = get_be32(pb);
  665. color_count = get_be16(pb);
  666. color_end = get_be16(pb);
  667. if ((color_start <= 255) &&
  668. (color_end <= 255)) {
  669. for (j = color_start; j <= color_end; j++) {
  670. /* each R, G, or B component is 16 bits;
  671. * only use the top 8 bits; skip alpha bytes
  672. * up front */
  673. get_byte(pb);
  674. get_byte(pb);
  675. r = get_byte(pb);
  676. get_byte(pb);
  677. g = get_byte(pb);
  678. get_byte(pb);
  679. b = get_byte(pb);
  680. get_byte(pb);
  681. c->palette_control.palette[j] =
  682. (r << 16) | (g << 8) | (b);
  683. }
  684. }
  685. }
  686. st->codec->palctrl = &c->palette_control;
  687. st->codec->palctrl->palette_changed = 1;
  688. } else
  689. st->codec->palctrl = NULL;
  690. } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
  691. int bits_per_sample;
  692. uint16_t version = get_be16(pb);
  693. st->codec->codec_id = id;
  694. get_be16(pb); /* revision level */
  695. get_be32(pb); /* vendor */
  696. st->codec->channels = get_be16(pb); /* channel count */
  697. dprintf(c->fc, "audio channels %d\n", st->codec->channels);
  698. st->codec->bits_per_sample = get_be16(pb); /* sample size */
  699. sc->audio_cid = get_be16(pb);
  700. get_be16(pb); /* packet size = 0 */
  701. st->codec->sample_rate = ((get_be32(pb) >> 16));
  702. switch (st->codec->codec_id) {
  703. case CODEC_ID_PCM_S8:
  704. case CODEC_ID_PCM_U8:
  705. if (st->codec->bits_per_sample == 16)
  706. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  707. break;
  708. case CODEC_ID_PCM_S16LE:
  709. case CODEC_ID_PCM_S16BE:
  710. if (st->codec->bits_per_sample == 8)
  711. st->codec->codec_id = CODEC_ID_PCM_S8;
  712. else if (st->codec->bits_per_sample == 24)
  713. st->codec->codec_id = CODEC_ID_PCM_S24BE;
  714. break;
  715. /* set values for old format before stsd version 1 appeared */
  716. case CODEC_ID_MACE3:
  717. sc->samples_per_frame = 6;
  718. sc->bytes_per_frame = 2*st->codec->channels;
  719. break;
  720. case CODEC_ID_MACE6:
  721. sc->samples_per_frame = 6;
  722. sc->bytes_per_frame = 1*st->codec->channels;
  723. break;
  724. case CODEC_ID_ADPCM_IMA_QT:
  725. sc->samples_per_frame = 64;
  726. sc->bytes_per_frame = 34*st->codec->channels;
  727. break;
  728. default:
  729. break;
  730. }
  731. //Read QT version 1 fields. In version 0 these do not exist.
  732. dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
  733. if(!c->isom) {
  734. if(version==1) {
  735. sc->samples_per_frame = get_be32(pb);
  736. get_be32(pb); /* bytes per packet */
  737. sc->bytes_per_frame = get_be32(pb);
  738. get_be32(pb); /* bytes per sample */
  739. } else if(version==2) {
  740. get_be32(pb); /* sizeof struct only */
  741. st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
  742. st->codec->channels = get_be32(pb);
  743. get_be32(pb); /* always 0x7F000000 */
  744. get_be32(pb); /* bits per channel if sound is uncompressed */
  745. get_be32(pb); /* lcpm format specific flag */
  746. get_be32(pb); /* bytes per audio packet if constant */
  747. get_be32(pb); /* lpcm frames per audio packet if constant */
  748. }
  749. }
  750. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  751. if (bits_per_sample) {
  752. st->codec->bits_per_sample = bits_per_sample;
  753. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  754. }
  755. } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
  756. st->codec->codec_id= id;
  757. } else {
  758. /* other codec type, just skip (rtp, mp4s, tmcd ...) */
  759. url_fskip(pb, size - (url_ftell(pb) - start_pos));
  760. }
  761. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  762. a.size = size - (url_ftell(pb) - start_pos);
  763. if (a.size > 8) {
  764. if (mov_read_default(c, pb, a) < 0)
  765. return -1;
  766. } else if (a.size > 0)
  767. url_fskip(pb, a.size);
  768. }
  769. if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  770. st->codec->sample_rate= sc->time_scale;
  771. /* special codec parameters handling */
  772. switch (st->codec->codec_id) {
  773. #ifdef CONFIG_DV_DEMUXER
  774. case CODEC_ID_DVAUDIO:
  775. c->dv_fctx = av_alloc_format_context();
  776. c->dv_demux = dv_init_demux(c->dv_fctx);
  777. if (!c->dv_demux) {
  778. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  779. return -1;
  780. }
  781. sc->dv_audio_container = 1;
  782. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  783. break;
  784. #endif
  785. /* no ifdef since parameters are always those */
  786. case CODEC_ID_AMR_WB:
  787. st->codec->sample_rate= 16000;
  788. st->codec->channels= 1; /* really needed */
  789. break;
  790. case CODEC_ID_AMR_NB:
  791. st->codec->sample_rate= 8000;
  792. st->codec->channels= 1; /* really needed */
  793. break;
  794. case CODEC_ID_MP2:
  795. case CODEC_ID_MP3:
  796. st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  797. st->need_parsing = AVSTREAM_PARSE_FULL;
  798. break;
  799. case CODEC_ID_ADPCM_MS:
  800. case CODEC_ID_ADPCM_IMA_WAV:
  801. st->codec->block_align = sc->bytes_per_frame;
  802. break;
  803. default:
  804. break;
  805. }
  806. return 0;
  807. }
  808. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  809. {
  810. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  811. MOVStreamContext *sc = st->priv_data;
  812. unsigned int i, entries;
  813. get_byte(pb); /* version */
  814. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  815. entries = get_be32(pb);
  816. if(entries >= UINT_MAX / sizeof(MOV_stsc_t))
  817. return -1;
  818. dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  819. sc->sample_to_chunk_sz = entries;
  820. sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_stsc_t));
  821. if (!sc->sample_to_chunk)
  822. return -1;
  823. for(i=0; i<entries; i++) {
  824. sc->sample_to_chunk[i].first = get_be32(pb);
  825. sc->sample_to_chunk[i].count = get_be32(pb);
  826. sc->sample_to_chunk[i].id = get_be32(pb);
  827. }
  828. return 0;
  829. }
  830. static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  831. {
  832. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  833. MOVStreamContext *sc = st->priv_data;
  834. unsigned int i, entries;
  835. get_byte(pb); /* version */
  836. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  837. entries = get_be32(pb);
  838. if(entries >= UINT_MAX / sizeof(int))
  839. return -1;
  840. sc->keyframe_count = entries;
  841. dprintf(c->fc, "keyframe_count = %d\n", sc->keyframe_count);
  842. sc->keyframes = av_malloc(entries * sizeof(int));
  843. if (!sc->keyframes)
  844. return -1;
  845. for(i=0; i<entries; i++) {
  846. sc->keyframes[i] = get_be32(pb);
  847. //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  848. }
  849. return 0;
  850. }
  851. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  852. {
  853. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  854. MOVStreamContext *sc = st->priv_data;
  855. unsigned int i, entries, sample_size;
  856. get_byte(pb); /* version */
  857. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  858. sample_size = get_be32(pb);
  859. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  860. sc->sample_size = sample_size;
  861. entries = get_be32(pb);
  862. if(entries >= UINT_MAX / sizeof(int))
  863. return -1;
  864. sc->sample_count = entries;
  865. if (sample_size)
  866. return 0;
  867. dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
  868. sc->sample_sizes = av_malloc(entries * sizeof(int));
  869. if (!sc->sample_sizes)
  870. return -1;
  871. for(i=0; i<entries; i++) {
  872. sc->sample_sizes[i] = get_be32(pb);
  873. dprintf(c->fc, "sample_sizes[]=%d\n", sc->sample_sizes[i]);
  874. }
  875. return 0;
  876. }
  877. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  878. {
  879. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  880. MOVStreamContext *sc = st->priv_data;
  881. unsigned int i, entries;
  882. int64_t duration=0;
  883. int64_t total_sample_count=0;
  884. get_byte(pb); /* version */
  885. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  886. entries = get_be32(pb);
  887. if(entries >= UINT_MAX / sizeof(MOV_stts_t))
  888. return -1;
  889. sc->stts_count = entries;
  890. sc->stts_data = av_malloc(entries * sizeof(MOV_stts_t));
  891. if (!sc->stts_data)
  892. return -1;
  893. dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  894. sc->time_rate=0;
  895. for(i=0; i<entries; i++) {
  896. int sample_duration;
  897. int sample_count;
  898. sample_count=get_be32(pb);
  899. sample_duration = get_be32(pb);
  900. sc->stts_data[i].count= sample_count;
  901. sc->stts_data[i].duration= sample_duration;
  902. sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
  903. dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  904. duration+=(int64_t)sample_duration*sample_count;
  905. total_sample_count+=sample_count;
  906. }
  907. st->nb_frames= total_sample_count;
  908. if(duration)
  909. st->duration= duration;
  910. return 0;
  911. }
  912. static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  913. {
  914. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  915. MOVStreamContext *sc = st->priv_data;
  916. unsigned int i, entries;
  917. get_byte(pb); /* version */
  918. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  919. entries = get_be32(pb);
  920. if(entries >= UINT_MAX / sizeof(MOV_stts_t))
  921. return -1;
  922. sc->ctts_count = entries;
  923. sc->ctts_data = av_malloc(entries * sizeof(MOV_stts_t));
  924. if (!sc->ctts_data)
  925. return -1;
  926. dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  927. for(i=0; i<entries; i++) {
  928. int count =get_be32(pb);
  929. int duration =get_be32(pb);
  930. if (duration < 0) {
  931. av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
  932. sc->ctts_count = 0;
  933. url_fskip(pb, 8 * (entries - i - 1));
  934. break;
  935. }
  936. sc->ctts_data[i].count = count;
  937. sc->ctts_data[i].duration= duration;
  938. sc->time_rate= ff_gcd(sc->time_rate, duration);
  939. }
  940. return 0;
  941. }
  942. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  943. {
  944. AVStream *st;
  945. MOVStreamContext *sc;
  946. st = av_new_stream(c->fc, c->fc->nb_streams);
  947. if (!st) return -2;
  948. sc = av_mallocz(sizeof(MOVStreamContext));
  949. if (!sc) {
  950. av_free(st);
  951. return -1;
  952. }
  953. st->priv_data = sc;
  954. st->codec->codec_type = CODEC_TYPE_DATA;
  955. st->start_time = 0; /* XXX: check */
  956. return mov_read_default(c, pb, atom);
  957. }
  958. static void mov_parse_udta_string(ByteIOContext *pb, char *str, int size)
  959. {
  960. uint16_t str_size = get_be16(pb); /* string length */;
  961. get_be16(pb); /* skip language */
  962. get_buffer(pb, str, FFMIN(size, str_size));
  963. }
  964. static int mov_read_udta(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  965. {
  966. uint64_t end = url_ftell(pb) + atom.size;
  967. while (url_ftell(pb) + 8 < end) {
  968. uint32_t tag_size = get_be32(pb);
  969. uint32_t tag = get_le32(pb);
  970. uint64_t next = url_ftell(pb) + tag_size - 8;
  971. if (next > end) // stop if tag_size is wrong
  972. break;
  973. switch (tag) {
  974. case MKTAG(0xa9,'n','a','m'):
  975. mov_parse_udta_string(pb, c->fc->title, sizeof(c->fc->title));
  976. break;
  977. case MKTAG(0xa9,'w','r','t'):
  978. mov_parse_udta_string(pb, c->fc->author, sizeof(c->fc->author));
  979. break;
  980. case MKTAG(0xa9,'c','p','y'):
  981. mov_parse_udta_string(pb, c->fc->copyright, sizeof(c->fc->copyright));
  982. break;
  983. case MKTAG(0xa9,'i','n','f'):
  984. mov_parse_udta_string(pb, c->fc->comment, sizeof(c->fc->comment));
  985. break;
  986. default:
  987. break;
  988. }
  989. url_fseek(pb, next, SEEK_SET);
  990. }
  991. return 0;
  992. }
  993. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  994. {
  995. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  996. int version = get_byte(pb);
  997. get_byte(pb); get_byte(pb);
  998. get_byte(pb); /* flags */
  999. /*
  1000. MOV_TRACK_ENABLED 0x0001
  1001. MOV_TRACK_IN_MOVIE 0x0002
  1002. MOV_TRACK_IN_PREVIEW 0x0004
  1003. MOV_TRACK_IN_POSTER 0x0008
  1004. */
  1005. if (version == 1) {
  1006. get_be64(pb);
  1007. get_be64(pb);
  1008. } else {
  1009. get_be32(pb); /* creation time */
  1010. get_be32(pb); /* modification time */
  1011. }
  1012. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1013. get_be32(pb); /* reserved */
  1014. st->start_time = 0; /* check */
  1015. (version == 1) ? get_be64(pb) : get_be32(pb); /* highlevel (considering edits) duration in movie timebase */
  1016. get_be32(pb); /* reserved */
  1017. get_be32(pb); /* reserved */
  1018. get_be16(pb); /* layer */
  1019. get_be16(pb); /* alternate group */
  1020. get_be16(pb); /* volume */
  1021. get_be16(pb); /* reserved */
  1022. url_fskip(pb, 36); /* display matrix */
  1023. /* those are fixed-point */
  1024. get_be32(pb); /* track width */
  1025. get_be32(pb); /* track height */
  1026. return 0;
  1027. }
  1028. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1029. /* like the files created with Adobe Premiere 5.0, for samples see */
  1030. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1031. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1032. {
  1033. int err;
  1034. if (atom.size < 8)
  1035. return 0; /* continue */
  1036. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1037. url_fskip(pb, atom.size - 4);
  1038. return 0;
  1039. }
  1040. atom.type = get_le32(pb);
  1041. atom.offset += 8;
  1042. atom.size -= 8;
  1043. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  1044. url_fskip(pb, atom.size);
  1045. return 0;
  1046. }
  1047. err = mov_read_mdat(c, pb, atom);
  1048. return err;
  1049. }
  1050. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1051. {
  1052. #ifdef CONFIG_ZLIB
  1053. ByteIOContext ctx;
  1054. uint8_t *cmov_data;
  1055. uint8_t *moov_data; /* uncompressed data */
  1056. long cmov_len, moov_len;
  1057. int ret;
  1058. get_be32(pb); /* dcom atom */
  1059. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  1060. return -1;
  1061. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  1062. av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1063. return -1;
  1064. }
  1065. get_be32(pb); /* cmvd atom */
  1066. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  1067. return -1;
  1068. moov_len = get_be32(pb); /* uncompressed size */
  1069. cmov_len = atom.size - 6 * 4;
  1070. cmov_data = av_malloc(cmov_len);
  1071. if (!cmov_data)
  1072. return -1;
  1073. moov_data = av_malloc(moov_len);
  1074. if (!moov_data) {
  1075. av_free(cmov_data);
  1076. return -1;
  1077. }
  1078. get_buffer(pb, cmov_data, cmov_len);
  1079. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1080. return -1;
  1081. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1082. return -1;
  1083. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1084. atom.offset = 0;
  1085. atom.size = moov_len;
  1086. #ifdef DEBUG
  1087. // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1088. #endif
  1089. ret = mov_read_default(c, &ctx, atom);
  1090. av_free(moov_data);
  1091. av_free(cmov_data);
  1092. return ret;
  1093. #else
  1094. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1095. return -1;
  1096. #endif
  1097. }
  1098. /* edit list atom */
  1099. static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1100. {
  1101. MOVStreamContext *sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  1102. int i, edit_count;
  1103. get_byte(pb); /* version */
  1104. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1105. edit_count= sc->edit_count = get_be32(pb); /* entries */
  1106. for(i=0; i<edit_count; i++){
  1107. int time;
  1108. get_be32(pb); /* Track duration */
  1109. time = get_be32(pb); /* Media time */
  1110. get_be32(pb); /* Media rate */
  1111. if (time != 0)
  1112. av_log(c->fc, AV_LOG_WARNING, "edit list not starting at 0, "
  1113. "a/v desync might occur, patch welcome\n");
  1114. }
  1115. dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, sc->edit_count);
  1116. return 0;
  1117. }
  1118. static const MOVParseTableEntry mov_default_parse_table[] = {
  1119. /* mp4 atoms */
  1120. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1121. { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
  1122. { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default },
  1123. { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_dref },
  1124. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1125. { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
  1126. { MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
  1127. { MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata },
  1128. { MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
  1129. { MKTAG( 'g', 'l', 'b', 'l' ), mov_read_glbl },
  1130. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1131. { MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata },
  1132. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1133. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1134. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1135. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1136. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1137. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1138. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
  1139. { MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata }, /* alac specific atom */
  1140. { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_glbl },
  1141. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1142. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1143. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1144. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1145. { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
  1146. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1147. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1148. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1149. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1150. { MKTAG( 'u', 'd', 't', 'a' ), mov_read_udta },
  1151. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
  1152. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1153. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1154. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1155. { 0, NULL }
  1156. };
  1157. /* XXX: is it sufficient ? */
  1158. static int mov_probe(AVProbeData *p)
  1159. {
  1160. unsigned int offset;
  1161. uint32_t tag;
  1162. int score = 0;
  1163. /* check file header */
  1164. offset = 0;
  1165. for(;;) {
  1166. /* ignore invalid offset */
  1167. if ((offset + 8) > (unsigned int)p->buf_size)
  1168. return score;
  1169. tag = AV_RL32(p->buf + offset + 4);
  1170. switch(tag) {
  1171. /* check for obvious tags */
  1172. case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
  1173. case MKTAG( 'm', 'o', 'o', 'v' ):
  1174. case MKTAG( 'm', 'd', 'a', 't' ):
  1175. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1176. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1177. return AVPROBE_SCORE_MAX;
  1178. /* those are more common words, so rate then a bit less */
  1179. case MKTAG( 'e', 'd', 'i', 'w' ): /* xdcam files have reverted first tags */
  1180. case MKTAG( 'w', 'i', 'd', 'e' ):
  1181. case MKTAG( 'f', 'r', 'e', 'e' ):
  1182. case MKTAG( 'j', 'u', 'n', 'k' ):
  1183. case MKTAG( 'p', 'i', 'c', 't' ):
  1184. return AVPROBE_SCORE_MAX - 5;
  1185. case MKTAG(0x82,0x82,0x7f,0x7d ):
  1186. case MKTAG( 'f', 't', 'y', 'p' ):
  1187. case MKTAG( 's', 'k', 'i', 'p' ):
  1188. case MKTAG( 'u', 'u', 'i', 'd' ):
  1189. offset = AV_RB32(p->buf+offset) + offset;
  1190. /* if we only find those cause probedata is too small at least rate them */
  1191. score = AVPROBE_SCORE_MAX - 50;
  1192. break;
  1193. default:
  1194. /* unrecognized tag */
  1195. return score;
  1196. }
  1197. }
  1198. return score;
  1199. }
  1200. static void mov_build_index(MOVContext *mov, AVStream *st)
  1201. {
  1202. MOVStreamContext *sc = st->priv_data;
  1203. offset_t current_offset;
  1204. int64_t current_dts = 0;
  1205. unsigned int stts_index = 0;
  1206. unsigned int stsc_index = 0;
  1207. unsigned int stss_index = 0;
  1208. unsigned int i, j;
  1209. if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO ||
  1210. sc->audio_cid == -2) {
  1211. unsigned int current_sample = 0;
  1212. unsigned int stts_sample = 0;
  1213. unsigned int keyframe, sample_size;
  1214. unsigned int distance = 0;
  1215. int key_off = sc->keyframes && sc->keyframes[0] == 1;
  1216. st->nb_frames = sc->sample_count;
  1217. for (i = 0; i < sc->chunk_count; i++) {
  1218. current_offset = sc->chunk_offsets[i];
  1219. if (stsc_index + 1 < sc->sample_to_chunk_sz &&
  1220. i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
  1221. stsc_index++;
  1222. for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
  1223. if (current_sample >= sc->sample_count) {
  1224. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1225. goto out;
  1226. }
  1227. keyframe = !sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index];
  1228. if (keyframe) {
  1229. distance = 0;
  1230. if (stss_index + 1 < sc->keyframe_count)
  1231. stss_index++;
  1232. }
  1233. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1234. dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1235. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1236. current_offset, current_dts, sample_size, distance, keyframe);
  1237. if(sc->sample_to_chunk[stsc_index].id - 1 == sc->pseudo_stream_id)
  1238. av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
  1239. keyframe ? AVINDEX_KEYFRAME : 0);
  1240. current_offset += sample_size;
  1241. assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
  1242. current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
  1243. distance++;
  1244. stts_sample++;
  1245. current_sample++;
  1246. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1247. stts_sample = 0;
  1248. stts_index++;
  1249. }
  1250. }
  1251. }
  1252. } else { /* read whole chunk */
  1253. unsigned int chunk_samples, chunk_size, chunk_duration;
  1254. unsigned int frames = 1;
  1255. for (i = 0; i < sc->chunk_count; i++) {
  1256. current_offset = sc->chunk_offsets[i];
  1257. if (stsc_index + 1 < sc->sample_to_chunk_sz &&
  1258. i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
  1259. stsc_index++;
  1260. chunk_samples = sc->sample_to_chunk[stsc_index].count;
  1261. /* get chunk size, beware of alaw/ulaw/mace */
  1262. if (sc->samples_per_frame > 0 &&
  1263. (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) {
  1264. if (sc->samples_per_frame < 1024)
  1265. chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
  1266. else {
  1267. chunk_size = sc->bytes_per_frame;
  1268. frames = chunk_samples / sc->samples_per_frame;
  1269. chunk_samples = sc->samples_per_frame;
  1270. }
  1271. } else if (sc->sample_size > 1 || st->codec->bits_per_sample == 8) {
  1272. chunk_size = chunk_samples * sc->sample_size;
  1273. } else {
  1274. av_log(mov->fc, AV_LOG_ERROR, "could not determine chunk size, report problem\n");
  1275. goto out;
  1276. }
  1277. for (j = 0; j < frames; j++) {
  1278. av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
  1279. /* get chunk duration */
  1280. chunk_duration = 0;
  1281. while (chunk_samples > 0) {
  1282. if (chunk_samples < sc->stts_data[stts_index].count) {
  1283. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1284. sc->stts_data[stts_index].count -= chunk_samples;
  1285. break;
  1286. } else {
  1287. chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
  1288. chunk_samples -= sc->stts_data[stts_index].count;
  1289. if (stts_index + 1 < sc->stts_count)
  1290. stts_index++;
  1291. }
  1292. }
  1293. current_offset += sc->bytes_per_frame;
  1294. dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, "
  1295. "duration %d\n", st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
  1296. assert(chunk_duration % sc->time_rate == 0);
  1297. current_dts += chunk_duration / sc->time_rate;
  1298. }
  1299. }
  1300. }
  1301. out:
  1302. /* adjust sample count to avindex entries */
  1303. sc->sample_count = st->nb_index_entries;
  1304. }
  1305. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1306. {
  1307. MOVContext *mov = s->priv_data;
  1308. ByteIOContext *pb = s->pb;
  1309. int i, err;
  1310. MOV_atom_t atom = { 0, 0, 0 };
  1311. mov->fc = s;
  1312. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1313. atom.size = url_fsize(pb);
  1314. else
  1315. atom.size = INT64_MAX;
  1316. /* check MOV header */
  1317. err = mov_read_default(mov, pb, atom);
  1318. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1319. av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
  1320. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1321. return -1;
  1322. }
  1323. dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1324. for(i=0; i<s->nb_streams; i++) {
  1325. AVStream *st = s->streams[i];
  1326. MOVStreamContext *sc = st->priv_data;
  1327. /* sanity checks */
  1328. if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz ||
  1329. (!sc->sample_size && !sc->sample_count)){
  1330. av_log(s, AV_LOG_ERROR, "missing mandatory atoms, broken header\n");
  1331. sc->sample_count = 0; //ignore track
  1332. continue;
  1333. }
  1334. if(!sc->time_rate)
  1335. sc->time_rate=1;
  1336. if(!sc->time_scale)
  1337. sc->time_scale= mov->time_scale;
  1338. av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
  1339. if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1)
  1340. st->codec->frame_size = av_rescale(sc->time_rate, st->codec->sample_rate, sc->time_scale);
  1341. if(st->duration != AV_NOPTS_VALUE){
  1342. assert(st->duration % sc->time_rate == 0);
  1343. st->duration /= sc->time_rate;
  1344. }
  1345. sc->ffindex = i;
  1346. mov_build_index(mov, st);
  1347. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1348. if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0)
  1349. av_log(s, AV_LOG_ERROR, "stream %d, error opening external essence: %s\n",
  1350. st->index, strerror(errno));
  1351. } else
  1352. sc->pb = s->pb;
  1353. switch (st->codec->codec_id) {
  1354. #ifdef CONFIG_H261_DECODER
  1355. case CODEC_ID_H261:
  1356. #endif
  1357. #ifdef CONFIG_H263_DECODER
  1358. case CODEC_ID_H263:
  1359. #endif
  1360. #ifdef CONFIG_MPEG4_DECODER
  1361. case CODEC_ID_MPEG4:
  1362. #endif
  1363. st->codec->width= 0; /* let decoder init width/height */
  1364. st->codec->height= 0;
  1365. break;
  1366. #ifdef CONFIG_LIBFAAD
  1367. case CODEC_ID_AAC:
  1368. #endif
  1369. #ifdef CONFIG_VORBIS_DECODER
  1370. case CODEC_ID_VORBIS:
  1371. #endif
  1372. case CODEC_ID_MP3ON4:
  1373. st->codec->sample_rate= 0; /* let decoder init parameters properly */
  1374. break;
  1375. }
  1376. }
  1377. for(i=0; i<s->nb_streams; i++) {
  1378. MOVStreamContext *sc = s->streams[i]->priv_data;
  1379. /* Do not need those anymore. */
  1380. av_freep(&sc->chunk_offsets);
  1381. av_freep(&sc->sample_to_chunk);
  1382. av_freep(&sc->sample_sizes);
  1383. av_freep(&sc->keyframes);
  1384. av_freep(&sc->stts_data);
  1385. }
  1386. return 0;
  1387. }
  1388. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1389. {
  1390. MOVContext *mov = s->priv_data;
  1391. MOVStreamContext *sc = 0;
  1392. AVIndexEntry *sample = 0;
  1393. int64_t best_dts = INT64_MAX;
  1394. int i;
  1395. for (i = 0; i < s->nb_streams; i++) {
  1396. AVStream *st = s->streams[i];
  1397. MOVStreamContext *msc = st->priv_data;
  1398. if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < msc->sample_count) {
  1399. AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
  1400. int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
  1401. AV_TIME_BASE, msc->time_scale);
  1402. dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  1403. if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
  1404. (!url_is_streamed(s->pb) &&
  1405. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  1406. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  1407. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  1408. sample = current_sample;
  1409. best_dts = dts;
  1410. sc = msc;
  1411. }
  1412. }
  1413. }
  1414. if (!sample)
  1415. return -1;
  1416. /* must be done just before reading, to avoid infinite loop on sample */
  1417. sc->current_sample++;
  1418. if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  1419. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  1420. sc->ffindex, sample->pos);
  1421. return -1;
  1422. }
  1423. av_get_packet(sc->pb, pkt, sample->size);
  1424. #ifdef CONFIG_DV_DEMUXER
  1425. if (mov->dv_demux && sc->dv_audio_container) {
  1426. dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
  1427. av_free(pkt->data);
  1428. pkt->size = 0;
  1429. if (dv_get_packet(mov->dv_demux, pkt) < 0)
  1430. return -1;
  1431. }
  1432. #endif
  1433. pkt->stream_index = sc->ffindex;
  1434. pkt->dts = sample->timestamp;
  1435. if (sc->ctts_data) {
  1436. assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
  1437. pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
  1438. /* update ctts context */
  1439. sc->sample_to_ctime_sample++;
  1440. if (sc->sample_to_ctime_index < sc->ctts_count &&
  1441. sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
  1442. sc->sample_to_ctime_index++;
  1443. sc->sample_to_ctime_sample = 0;
  1444. }
  1445. } else {
  1446. pkt->pts = pkt->dts;
  1447. }
  1448. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
  1449. pkt->pos = sample->pos;
  1450. dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  1451. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  1452. return 0;
  1453. }
  1454. static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
  1455. {
  1456. MOVStreamContext *sc = st->priv_data;
  1457. int sample, time_sample;
  1458. int i;
  1459. sample = av_index_search_timestamp(st, timestamp, flags);
  1460. dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  1461. if (sample < 0) /* not sure what to do */
  1462. return -1;
  1463. sc->current_sample = sample;
  1464. dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
  1465. /* adjust ctts index */
  1466. if (sc->ctts_data) {
  1467. time_sample = 0;
  1468. for (i = 0; i < sc->ctts_count; i++) {
  1469. int next = time_sample + sc->ctts_data[i].count;
  1470. if (next > sc->current_sample) {
  1471. sc->sample_to_ctime_index = i;
  1472. sc->sample_to_ctime_sample = sc->current_sample - time_sample;
  1473. break;
  1474. }
  1475. time_sample = next;
  1476. }
  1477. }
  1478. return sample;
  1479. }
  1480. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  1481. {
  1482. AVStream *st;
  1483. int64_t seek_timestamp, timestamp;
  1484. int sample;
  1485. int i;
  1486. if (stream_index >= s->nb_streams)
  1487. return -1;
  1488. st = s->streams[stream_index];
  1489. sample = mov_seek_stream(st, sample_time, flags);
  1490. if (sample < 0)
  1491. return -1;
  1492. /* adjust seek timestamp to found sample timestamp */
  1493. seek_timestamp = st->index_entries[sample].timestamp;
  1494. for (i = 0; i < s->nb_streams; i++) {
  1495. st = s->streams[i];
  1496. if (stream_index == i || st->discard == AVDISCARD_ALL)
  1497. continue;
  1498. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  1499. mov_seek_stream(st, timestamp, flags);
  1500. }
  1501. return 0;
  1502. }
  1503. static int mov_read_close(AVFormatContext *s)
  1504. {
  1505. int i, j;
  1506. MOVContext *mov = s->priv_data;
  1507. for(i=0; i<s->nb_streams; i++) {
  1508. MOVStreamContext *sc = s->streams[i]->priv_data;
  1509. av_freep(&sc->ctts_data);
  1510. for (j=0; j<sc->drefs_count; j++)
  1511. av_freep(&sc->drefs[j].path);
  1512. av_freep(&sc->drefs);
  1513. if (sc->pb && sc->pb != s->pb)
  1514. url_fclose(sc->pb);
  1515. }
  1516. if(mov->dv_demux){
  1517. for(i=0; i<mov->dv_fctx->nb_streams; i++){
  1518. av_freep(&mov->dv_fctx->streams[i]->codec);
  1519. av_freep(&mov->dv_fctx->streams[i]);
  1520. }
  1521. av_freep(&mov->dv_fctx);
  1522. av_freep(&mov->dv_demux);
  1523. }
  1524. return 0;
  1525. }
  1526. AVInputFormat mov_demuxer = {
  1527. "mov,mp4,m4a,3gp,3g2,mj2",
  1528. "QuickTime/MPEG4/Motion JPEG 2000 format",
  1529. sizeof(MOVContext),
  1530. mov_probe,
  1531. mov_read_header,
  1532. mov_read_packet,
  1533. mov_read_close,
  1534. mov_read_seek,
  1535. };