You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1764 lines
57KB

  1. /*
  2. * MOV decoder.
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include "avi.h"
  21. #ifdef CONFIG_ZLIB
  22. #include <zlib.h>
  23. #endif
  24. /*
  25. * First version by Francois Revol revol@free.fr
  26. *
  27. * Features and limitations:
  28. * - reads most of the QT files I have (at least the structure),
  29. * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
  30. * FIXED, Francois Revol, 07/17/2002
  31. * - ffmpeg has nearly none of the usual QuickTime codecs,
  32. * although I succesfully dumped raw and mp3 audio tracks off .mov files.
  33. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  34. * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
  35. * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
  36. * http://mpeg.telecomitalialab.com/faq.htm
  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/techpubs/quicktime/qtdevdocs/QTFF/qtff.html
  46. * http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
  47. * QuickTime is a trademark of Apple (AFAIK :))
  48. */
  49. //#define DEBUG
  50. #ifdef DEBUG
  51. #include <stdio.h>
  52. #include <fcntl.h>
  53. #endif
  54. #include "qtpalette.h"
  55. /* allows chunk splitting - should work now... */
  56. /* in case you can't read a file, try commenting */
  57. #define MOV_SPLIT_CHUNKS
  58. /* Special handling for movies created with Minolta Dimaxe Xi*/
  59. /* this fix should not interfere with other .mov files, but just in case*/
  60. #define MOV_MINOLTA_FIX
  61. /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
  62. /* so we first list them as this, then clean up the list of streams we give back, */
  63. /* getting rid of these */
  64. #define CODEC_TYPE_MOV_OTHER (enum CodecType) 2
  65. static const CodecTag mov_video_tags[] = {
  66. /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
  67. /* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
  68. /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
  69. /* { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
  70. /* Graphics */
  71. /* Animation */
  72. /* Apple video */
  73. /* Kodak Photo CD */
  74. { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
  75. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
  76. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
  77. { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
  78. { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
  79. /* { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
  80. /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
  81. /* Sorenson video */
  82. { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
  83. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
  84. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
  85. { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') }, /* Sorenson Video v3 */
  86. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  87. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  88. { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') },
  89. /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
  90. { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
  91. { CODEC_ID_H263, MKTAG('s', '2', '6', '3') }, /* H263 ?? works */
  92. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
  93. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
  94. /* { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
  95. { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
  96. { CODEC_ID_RPZA, MKTAG('r', 'p', 'z', 'a') }, /* Apple Video (RPZA) */
  97. { CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') }, /* Cinepak */
  98. { CODEC_ID_8BPS, MKTAG('8', 'B', 'P', 'S') }, /* Planar RGB (8BPS) */
  99. { CODEC_ID_SMC, MKTAG('s', 'm', 'c', ' ') }, /* Apple Graphics (SMC) */
  100. { CODEC_ID_NONE, 0 },
  101. };
  102. static const CodecTag mov_audio_tags[] = {
  103. /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
  104. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
  105. /* { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') },*/ /* 8 bits */
  106. { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
  107. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
  108. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
  109. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
  110. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
  111. { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
  112. { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
  113. { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
  114. { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
  115. { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
  116. /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
  117. /* MP4 tags */
  118. { CODEC_ID_MPEG4AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG-4 AAC */
  119. /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
  120. { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
  121. { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') }, /* AMR-WB 3gp */
  122. { CODEC_ID_AC3, MKTAG('m', 's', 0x20, 0x00) }, /* Dolby AC-3 */
  123. { CODEC_ID_NONE, 0 },
  124. };
  125. /* the QuickTime file format is quite convoluted...
  126. * it has lots of index tables, each indexing something in another one...
  127. * Here we just use what is needed to read the chunks
  128. */
  129. typedef struct MOV_sample_to_chunk_tbl {
  130. long first;
  131. long count;
  132. long id;
  133. } MOV_sample_to_chunk_tbl;
  134. typedef struct {
  135. uint32_t type;
  136. int64_t offset;
  137. int64_t size; /* total size (excluding the size and type fields) */
  138. } MOV_atom_t;
  139. typedef struct {
  140. int seed;
  141. int flags;
  142. int size;
  143. void* clrs;
  144. } MOV_ctab_t;
  145. typedef struct {
  146. uint8_t version;
  147. uint32_t flags; // 24bit
  148. /* 0x03 ESDescrTag */
  149. uint16_t es_id;
  150. #define MP4ODescrTag 0x01
  151. #define MP4IODescrTag 0x02
  152. #define MP4ESDescrTag 0x03
  153. #define MP4DecConfigDescrTag 0x04
  154. #define MP4DecSpecificDescrTag 0x05
  155. #define MP4SLConfigDescrTag 0x06
  156. #define MP4ContentIdDescrTag 0x07
  157. #define MP4SupplContentIdDescrTag 0x08
  158. #define MP4IPIPtrDescrTag 0x09
  159. #define MP4IPMPPtrDescrTag 0x0A
  160. #define MP4IPMPDescrTag 0x0B
  161. #define MP4RegistrationDescrTag 0x0D
  162. #define MP4ESIDIncDescrTag 0x0E
  163. #define MP4ESIDRefDescrTag 0x0F
  164. #define MP4FileIODescrTag 0x10
  165. #define MP4FileODescrTag 0x11
  166. #define MP4ExtProfileLevelDescrTag 0x13
  167. #define MP4ExtDescrTagsStart 0x80
  168. #define MP4ExtDescrTagsEnd 0xFE
  169. uint8_t stream_priority;
  170. /* 0x04 DecConfigDescrTag */
  171. uint8_t object_type_id;
  172. uint8_t stream_type;
  173. /* XXX: really streamType is
  174. * only 6bit, followed by:
  175. * 1bit upStream
  176. * 1bit reserved
  177. */
  178. uint32_t buffer_size_db; // 24
  179. uint32_t max_bitrate;
  180. uint32_t avg_bitrate;
  181. /* 0x05 DecSpecificDescrTag */
  182. uint8_t decoder_cfg_len;
  183. uint8_t *decoder_cfg;
  184. /* 0x06 SLConfigDescrTag */
  185. uint8_t sl_config_len;
  186. uint8_t *sl_config;
  187. } MOV_esds_t;
  188. struct MOVParseTableEntry;
  189. typedef struct MOVStreamContext {
  190. int ffindex; /* the ffmpeg stream id */
  191. int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
  192. long next_chunk;
  193. long chunk_count;
  194. int64_t *chunk_offsets;
  195. long sample_to_chunk_sz;
  196. MOV_sample_to_chunk_tbl *sample_to_chunk;
  197. long sample_to_chunk_index;
  198. long sample_size;
  199. long sample_count;
  200. long *sample_sizes;
  201. int time_scale;
  202. long current_sample;
  203. long left_in_chunk; /* how many samples before next chunk */
  204. /* specific MPEG4 header which is added at the beginning of the stream */
  205. int header_len;
  206. uint8_t *header_data;
  207. MOV_esds_t esds;
  208. } MOVStreamContext;
  209. typedef struct MOVContext {
  210. int mp4; /* set to 1 as soon as we are sure that the file is an .mp4 file (even some header parsing depends on this) */
  211. AVFormatContext *fc;
  212. int time_scale;
  213. int duration; /* duration of the longest track */
  214. int found_moov; /* when both 'moov' and 'mdat' sections has been found */
  215. int found_mdat; /* we suppose we have enough data to read the file */
  216. int64_t mdat_size;
  217. int64_t mdat_offset;
  218. int total_streams;
  219. /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
  220. * but we need the info to be able to skip data from those streams in the 'mdat' section
  221. */
  222. MOVStreamContext *streams[MAX_STREAMS];
  223. int64_t next_chunk_offset;
  224. MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
  225. int ctab_size;
  226. MOV_ctab_t **ctab; /* color tables */
  227. const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
  228. /* NOTE: for recursion save to/ restore from local variable! */
  229. AVPaletteControl palette_control;
  230. } MOVContext;
  231. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  232. /* those functions parse an atom */
  233. /* return code:
  234. 1: found what I wanted, exit
  235. 0: continue to parse next atom
  236. -1: error occured, exit
  237. */
  238. typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
  239. /* links atom IDs to parse functions */
  240. typedef struct MOVParseTableEntry {
  241. uint32_t type;
  242. mov_parse_function func;
  243. } MOVParseTableEntry;
  244. #ifdef DEBUG
  245. /*
  246. * XXX: static sux, even more in a multithreaded environment...
  247. * Avoid them. This is here just to help debugging.
  248. */
  249. static int debug_indent = 0;
  250. void print_atom(const char *str, MOV_atom_t atom)
  251. {
  252. unsigned int tag, i;
  253. tag = (unsigned int) atom.type;
  254. i=debug_indent;
  255. if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
  256. while(i--)
  257. printf("|");
  258. printf("parse:");
  259. printf(" %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
  260. str, tag & 0xff,
  261. (tag >> 8) & 0xff,
  262. (tag >> 16) & 0xff,
  263. (tag >> 24) & 0xff,
  264. (unsigned int)atom.offset,
  265. (unsigned int)atom.size);
  266. assert((unsigned int)atom.size < 0x7fffffff);// catching errors
  267. }
  268. #else
  269. #define print_atom(a,b)
  270. #endif
  271. static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  272. {
  273. print_atom("leaf", atom);
  274. if (atom.size>1)
  275. url_fskip(pb, atom.size);
  276. /* url_seek(pb, atom_offset+atom.size, SEEK_SET); */
  277. return 0;
  278. }
  279. static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  280. {
  281. int64_t total_size = 0;
  282. MOV_atom_t a;
  283. int i;
  284. int err = 0;
  285. #ifdef DEBUG
  286. print_atom("default", atom);
  287. debug_indent++;
  288. #endif
  289. a.offset = atom.offset;
  290. if (atom.size < 0)
  291. atom.size = 0x7fffffffffffffffLL;
  292. while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
  293. a.size = atom.size;
  294. a.type=0L;
  295. if(atom.size >= 8) {
  296. a.size = get_be32(pb);
  297. a.type = get_le32(pb);
  298. }
  299. total_size += 8;
  300. a.offset += 8;
  301. //printf("type: %08x %.4s sz: %Lx %Lx %Lx\n", type, (char*)&type, size, atom.size, total_size);
  302. if (a.size == 1) { /* 64 bit extended size */
  303. a.size = get_be64(pb) - 8;
  304. a.offset += 8;
  305. total_size += 8;
  306. }
  307. if (a.size == 0) {
  308. a.size = atom.size - total_size;
  309. if (a.size <= 8)
  310. break;
  311. }
  312. for (i = 0; c->parse_table[i].type != 0L
  313. && c->parse_table[i].type != a.type; i++)
  314. /* empty */;
  315. a.size -= 8;
  316. // printf(" i=%ld\n", i);
  317. if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
  318. // url_seek(pb, atom.offset+atom.size, SEEK_SET);
  319. #ifdef DEBUG
  320. print_atom("unknown", a);
  321. #endif
  322. url_fskip(pb, a.size);
  323. } else {
  324. #ifdef DEBUG
  325. //char b[5] = { type & 0xff, (type >> 8) & 0xff, (type >> 16) & 0xff, (type >> 24) & 0xff, 0 };
  326. //print_atom(b, type, offset, size);
  327. #endif
  328. err = (c->parse_table[i].func)(c, pb, a);
  329. }
  330. a.offset += a.size;
  331. total_size += a.size;
  332. }
  333. if (!err && total_size < atom.size && atom.size < 0x7ffff) {
  334. //printf("RESET %Ld %Ld err:%d\n", atom.size, total_size, err);
  335. url_fskip(pb, atom.size - total_size);
  336. }
  337. #ifdef DEBUG
  338. debug_indent--;
  339. #endif
  340. return err;
  341. }
  342. static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  343. {
  344. unsigned int len;
  345. MOV_ctab_t *t;
  346. //url_fskip(pb, atom.size); // for now
  347. c->ctab = av_realloc(c->ctab, ++c->ctab_size);
  348. t = c->ctab[c->ctab_size];
  349. t->seed = get_be32(pb);
  350. t->flags = get_be16(pb);
  351. t->size = get_be16(pb) + 1;
  352. len = 2 * t->size * 4;
  353. if (len > 0) {
  354. t->clrs = av_malloc(len); // 16bit A R G B
  355. if (t->clrs)
  356. get_buffer(pb, t->clrs, len);
  357. }
  358. return 0;
  359. }
  360. static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  361. {
  362. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  363. int len = 0;
  364. uint32_t type;
  365. uint32_t ctype;
  366. print_atom("hdlr", atom);
  367. get_byte(pb); /* version */
  368. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  369. /* component type */
  370. ctype = get_le32(pb);
  371. type = get_le32(pb); /* component subtype */
  372. #ifdef DEBUG
  373. printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
  374. printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
  375. #endif
  376. #ifdef DEBUG
  377. /* XXX: yeah this is ugly... */
  378. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  379. if(type == MKTAG('v', 'i', 'd', 'e'))
  380. puts("hdlr: vide");
  381. else if(type == MKTAG('s', 'o', 'u', 'n'))
  382. puts("hdlr: soun");
  383. } else if(ctype == 0) { /* MP4 */
  384. if(type == MKTAG('v', 'i', 'd', 'e'))
  385. puts("hdlr: vide");
  386. else if(type == MKTAG('s', 'o', 'u', 'n'))
  387. puts("hdlr: soun");
  388. else if(type == MKTAG('o', 'd', 's', 'm'))
  389. puts("hdlr: odsm");
  390. else if(type == MKTAG('s', 'd', 's', 'm'))
  391. puts("hdlr: sdsm");
  392. } else puts("hdlr: meta");
  393. #endif
  394. if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
  395. /* helps parsing the string hereafter... */
  396. c->mp4 = 0;
  397. if(type == MKTAG('v', 'i', 'd', 'e'))
  398. st->codec.codec_type = CODEC_TYPE_VIDEO;
  399. else if(type == MKTAG('s', 'o', 'u', 'n'))
  400. st->codec.codec_type = CODEC_TYPE_AUDIO;
  401. } else if(ctype == 0) { /* MP4 */
  402. /* helps parsing the string hereafter... */
  403. c->mp4 = 1;
  404. if(type == MKTAG('v', 'i', 'd', 'e'))
  405. st->codec.codec_type = CODEC_TYPE_VIDEO;
  406. else if(type == MKTAG('s', 'o', 'u', 'n'))
  407. st->codec.codec_type = CODEC_TYPE_AUDIO;
  408. }
  409. get_be32(pb); /* component manufacture */
  410. get_be32(pb); /* component flags */
  411. get_be32(pb); /* component flags mask */
  412. if(atom.size <= 24)
  413. return 0; /* nothing left to read */
  414. /* XXX: MP4 uses a C string, not a pascal one */
  415. /* component name */
  416. if(c->mp4) {
  417. /* .mp4: C string */
  418. while(get_byte(pb) && (++len < (atom.size - 24)));
  419. } else {
  420. /* .mov: PASCAL string */
  421. #ifdef DEBUG
  422. char* buf;
  423. #endif
  424. len = get_byte(pb);
  425. #ifdef DEBUG
  426. buf = (uint8_t*) av_malloc(len+1);
  427. if (buf) {
  428. get_buffer(pb, buf, len);
  429. buf[len] = '\0';
  430. printf("**buf='%s'\n", buf);
  431. av_free(buf);
  432. } else
  433. #endif
  434. url_fskip(pb, len);
  435. }
  436. return 0;
  437. }
  438. static int mov_mp4_read_descr_len(ByteIOContext *pb)
  439. {
  440. int len = 0;
  441. int count = 4;
  442. while (count--) {
  443. int c = get_byte(pb);
  444. len = (len << 7) | (c & 0x7f);
  445. if (!(c & 0x80))
  446. break;
  447. }
  448. return len;
  449. }
  450. static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
  451. {
  452. int len;
  453. *tag = get_byte(pb);
  454. len = mov_mp4_read_descr_len(pb);
  455. #ifdef DEBUG
  456. printf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
  457. #endif
  458. return len;
  459. }
  460. static inline unsigned int get_be24(ByteIOContext *s)
  461. {
  462. unsigned int val;
  463. val = get_byte(s) << 16;
  464. val |= get_byte(s) << 8;
  465. val |= get_byte(s);
  466. return val;
  467. }
  468. static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  469. {
  470. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  471. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  472. int64_t start_pos = url_ftell(pb);
  473. int tag, len;
  474. print_atom("esds", atom);
  475. /* Well, broken but suffisant for some MP4 streams */
  476. get_be32(pb); /* version + flags */
  477. len = mov_mp4_read_descr(pb, &tag);
  478. if (tag == MP4ESDescrTag) {
  479. get_be16(pb); /* ID */
  480. get_byte(pb); /* priority */
  481. } else
  482. get_be16(pb); /* ID */
  483. len = mov_mp4_read_descr(pb, &tag);
  484. if (tag == MP4DecConfigDescrTag) {
  485. sc->esds.object_type_id = get_byte(pb);
  486. sc->esds.stream_type = get_byte(pb);
  487. sc->esds.buffer_size_db = get_be24(pb);
  488. sc->esds.max_bitrate = get_be32(pb);
  489. sc->esds.avg_bitrate = get_be32(pb);
  490. len = mov_mp4_read_descr(pb, &tag);
  491. //printf("LEN %d TAG %d m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
  492. if (tag == MP4DecSpecificDescrTag) {
  493. #ifdef DEBUG
  494. printf("Specific MPEG4 header len=%d\n", len);
  495. #endif
  496. st->codec.extradata = (uint8_t*) av_mallocz(len);
  497. if (st->codec.extradata) {
  498. get_buffer(pb, st->codec.extradata, len);
  499. st->codec.extradata_size = len;
  500. }
  501. }
  502. }
  503. /* in any case, skip garbage */
  504. url_fskip(pb, atom.size - ((url_ftell(pb) - start_pos)));
  505. return 0;
  506. }
  507. /* this atom contains actual media data */
  508. static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  509. {
  510. print_atom("mdat", atom);
  511. if(atom.size == 0) /* wrong one (MP4) */
  512. return 0;
  513. c->found_mdat=1;
  514. c->mdat_offset = atom.offset;
  515. c->mdat_size = atom.size;
  516. if(c->found_moov)
  517. return 1; /* found both, just go */
  518. url_fskip(pb, atom.size);
  519. return 0; /* now go for moov */
  520. }
  521. /* this atom should contain all header atoms */
  522. static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  523. {
  524. int err;
  525. print_atom("moov", atom);
  526. err = mov_read_default(c, pb, atom);
  527. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  528. /* so we don't parse the whole file if over a network */
  529. c->found_moov=1;
  530. if(c->found_mdat)
  531. return 1; /* found both, just go */
  532. return 0; /* now go for mdat */
  533. }
  534. static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  535. {
  536. print_atom("mdhd", atom);
  537. get_byte(pb); /* version */
  538. get_byte(pb); get_byte(pb);
  539. get_byte(pb); /* flags */
  540. get_be32(pb); /* creation time */
  541. get_be32(pb); /* modification time */
  542. c->streams[c->total_streams]->time_scale = get_be32(pb);
  543. #ifdef DEBUG
  544. printf("track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
  545. #endif
  546. get_be32(pb); /* duration */
  547. get_be16(pb); /* language */
  548. get_be16(pb); /* quality */
  549. return 0;
  550. }
  551. static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  552. {
  553. print_atom("mvhd", atom);
  554. get_byte(pb); /* version */
  555. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  556. get_be32(pb); /* creation time */
  557. get_be32(pb); /* modification time */
  558. c->time_scale = get_be32(pb); /* time scale */
  559. #ifdef DEBUG
  560. printf("time scale = %i\n", c->time_scale);
  561. #endif
  562. c->duration = get_be32(pb); /* duration */
  563. get_be32(pb); /* preferred scale */
  564. get_be16(pb); /* preferred volume */
  565. url_fskip(pb, 10); /* reserved */
  566. url_fskip(pb, 36); /* display matrix */
  567. get_be32(pb); /* preview time */
  568. get_be32(pb); /* preview duration */
  569. get_be32(pb); /* poster time */
  570. get_be32(pb); /* selection time */
  571. get_be32(pb); /* selection duration */
  572. get_be32(pb); /* current time */
  573. get_be32(pb); /* next track ID */
  574. return 0;
  575. }
  576. static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  577. {
  578. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  579. // currently SVQ3 decoder expect full STSD header - so let's fake it
  580. // this should be fixed and just SMI header should be passed
  581. av_free(st->codec.extradata);
  582. st->codec.extradata_size = 0x5a + atom.size;
  583. st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size);
  584. if (st->codec.extradata) {
  585. strcpy(st->codec.extradata, "SVQ3"); // fake
  586. get_buffer(pb, st->codec.extradata + 0x5a, atom.size);
  587. //printf("Reading SMI %Ld %s\n", atom.size, (char*)st->codec.extradata + 0x5a);
  588. } else
  589. url_fskip(pb, atom.size);
  590. return 0;
  591. }
  592. static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  593. {
  594. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  595. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  596. int entries, i;
  597. print_atom("stco", atom);
  598. get_byte(pb); /* version */
  599. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  600. entries = get_be32(pb);
  601. sc->chunk_count = entries;
  602. sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
  603. if (!sc->chunk_offsets)
  604. return -1;
  605. if (atom.type == MKTAG('s', 't', 'c', 'o')) {
  606. for(i=0; i<entries; i++) {
  607. sc->chunk_offsets[i] = get_be32(pb);
  608. }
  609. } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
  610. for(i=0; i<entries; i++) {
  611. sc->chunk_offsets[i] = get_be64(pb);
  612. }
  613. } else
  614. return -1;
  615. #ifdef DEBUG
  616. /*
  617. for(i=0; i<entries; i++) {
  618. printf("chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
  619. }
  620. */
  621. #endif
  622. return 0;
  623. }
  624. static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  625. {
  626. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  627. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  628. int entries, frames_per_sample;
  629. uint32_t format;
  630. /* for palette traversal */
  631. int color_depth;
  632. int color_start;
  633. int color_count;
  634. int color_end;
  635. int color_index;
  636. int color_dec;
  637. int color_greyscale;
  638. unsigned char *color_table;
  639. int j;
  640. unsigned char r, g, b;
  641. print_atom("stsd", atom);
  642. get_byte(pb); /* version */
  643. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  644. entries = get_be32(pb);
  645. while(entries--) { //Parsing Sample description table
  646. enum CodecID id;
  647. int size = get_be32(pb); /* size */
  648. format = get_le32(pb); /* data format */
  649. get_be32(pb); /* reserved */
  650. get_be16(pb); /* reserved */
  651. get_be16(pb); /* index */
  652. /* for MPEG4: set codec type by looking for it */
  653. id = codec_get_id(mov_video_tags, format);
  654. if (id >= 0) {
  655. AVCodec *codec;
  656. codec = avcodec_find_decoder(id);
  657. if (codec)
  658. st->codec.codec_type = codec->type;
  659. }
  660. #ifdef DEBUG
  661. printf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
  662. size,
  663. (format >> 0) & 0xff,
  664. (format >> 8) & 0xff,
  665. (format >> 16) & 0xff,
  666. (format >> 24) & 0xff,
  667. st->codec.codec_type);
  668. #endif
  669. st->codec.codec_tag = format;
  670. if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
  671. MOV_atom_t a = { 0, 0, 0 };
  672. st->codec.codec_id = id;
  673. get_be16(pb); /* version */
  674. get_be16(pb); /* revision level */
  675. get_be32(pb); /* vendor */
  676. get_be32(pb); /* temporal quality */
  677. get_be32(pb); /* spacial quality */
  678. st->codec.width = get_be16(pb); /* width */
  679. st->codec.height = get_be16(pb); /* height */
  680. #if 1
  681. if (st->codec.codec_id == CODEC_ID_MPEG4) {
  682. /* in some MPEG4 the width/height are not correct, so
  683. we ignore this info */
  684. st->codec.width = 0;
  685. st->codec.height = 0;
  686. }
  687. #endif
  688. get_be32(pb); /* horiz resolution */
  689. get_be32(pb); /* vert resolution */
  690. get_be32(pb); /* data size, always 0 */
  691. frames_per_sample = get_be16(pb); /* frames per samples */
  692. #ifdef DEBUG
  693. printf("frames/samples = %d\n", frames_per_sample);
  694. #endif
  695. get_buffer(pb, (uint8_t *)st->codec.codec_name, 32); /* codec name */
  696. st->codec.bits_per_sample = get_be16(pb); /* depth */
  697. st->codec.color_table_id = get_be16(pb); /* colortable id */
  698. /* These are set in mov_read_stts and might already be set!
  699. st->codec.frame_rate = 25;
  700. st->codec.frame_rate_base = 1;
  701. */
  702. size -= (16+8*4+2+32+2*2);
  703. #if 0
  704. while (size >= 8) {
  705. MOV_atom_t a;
  706. int64_t start_pos;
  707. a.size = get_be32(pb);
  708. a.type = get_le32(pb);
  709. size -= 8;
  710. #ifdef DEBUG
  711. printf("VIDEO: atom_type=%c%c%c%c atom.size=%Ld size_left=%d\n",
  712. (a.type >> 0) & 0xff,
  713. (a.type >> 8) & 0xff,
  714. (a.type >> 16) & 0xff,
  715. (a.type >> 24) & 0xff,
  716. a.size, size);
  717. #endif
  718. start_pos = url_ftell(pb);
  719. switch(a.type) {
  720. case MKTAG('e', 's', 'd', 's'):
  721. {
  722. int tag, len;
  723. /* Well, broken but suffisant for some MP4 streams */
  724. get_be32(pb); /* version + flags */
  725. len = mov_mp4_read_descr(pb, &tag);
  726. if (tag == 0x03) {
  727. /* MP4ESDescrTag */
  728. get_be16(pb); /* ID */
  729. get_byte(pb); /* priority */
  730. len = mov_mp4_read_descr(pb, &tag);
  731. if (tag != 0x04)
  732. goto fail;
  733. /* MP4DecConfigDescrTag */
  734. get_byte(pb); /* objectTypeId */
  735. get_be32(pb); /* streamType + buffer size */
  736. get_be32(pb); /* max bit rate */
  737. get_be32(pb); /* avg bit rate */
  738. len = mov_mp4_read_descr(pb, &tag);
  739. if (tag != 0x05)
  740. goto fail;
  741. /* MP4DecSpecificDescrTag */
  742. #ifdef DEBUG
  743. printf("Specific MPEG4 header len=%d\n", len);
  744. #endif
  745. sc->header_data = av_mallocz(len);
  746. if (sc->header_data) {
  747. get_buffer(pb, sc->header_data, len);
  748. sc->header_len = len;
  749. }
  750. }
  751. /* in any case, skip garbage */
  752. }
  753. break;
  754. default:
  755. break;
  756. }
  757. fail:
  758. printf("ATOMENEWSIZE %Ld %d\n", atom.size, url_ftell(pb) - start_pos);
  759. if (atom.size > 8) {
  760. url_fskip(pb, (atom.size - 8) -
  761. ((url_ftell(pb) - start_pos)));
  762. size -= atom.size - 8;
  763. }
  764. }
  765. if (size > 0) {
  766. /* unknown extension */
  767. url_fskip(pb, size);
  768. }
  769. #else
  770. /* figure out the palette situation */
  771. color_depth = st->codec.bits_per_sample & 0x1F;
  772. color_greyscale = st->codec.bits_per_sample & 0x20;
  773. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  774. if ((color_depth == 2) || (color_depth == 4) ||
  775. (color_depth == 8)) {
  776. if (color_greyscale) {
  777. /* compute the greyscale palette */
  778. color_count = 1 << color_depth;
  779. color_index = 255;
  780. color_dec = 256 / (color_count - 1);
  781. for (j = 0; j < color_count; j++) {
  782. r = g = b = color_index;
  783. c->palette_control.palette[j] =
  784. (r << 16) | (g << 8) | (b);
  785. color_index -= color_dec;
  786. if (color_index < 0)
  787. color_index = 0;
  788. }
  789. } else if (st->codec.color_table_id & 0x08) {
  790. /* if flag bit 3 is set, use the default palette */
  791. color_count = 1 << color_depth;
  792. if (color_depth == 2)
  793. color_table = qt_default_palette_4;
  794. else if (color_depth == 4)
  795. color_table = qt_default_palette_16;
  796. else
  797. color_table = qt_default_palette_256;
  798. for (j = 0; j < color_count; j++) {
  799. r = color_table[j * 4 + 0];
  800. g = color_table[j * 4 + 1];
  801. b = color_table[j * 4 + 2];
  802. c->palette_control.palette[j] =
  803. (r << 16) | (g << 8) | (b);
  804. }
  805. } else {
  806. /* load the palette from the file */
  807. color_start = get_be32(pb);
  808. color_count = get_be16(pb);
  809. color_end = get_be16(pb);
  810. for (j = color_start; j <= color_end; j++) {
  811. /* each R, G, or B component is 16 bits;
  812. * only use the top 8 bits; skip alpha bytes
  813. * up front */
  814. get_byte(pb);
  815. get_byte(pb);
  816. r = get_byte(pb);
  817. get_byte(pb);
  818. g = get_byte(pb);
  819. get_byte(pb);
  820. b = get_byte(pb);
  821. get_byte(pb);
  822. c->palette_control.palette[j] =
  823. (r << 16) | (g << 8) | (b);
  824. }
  825. }
  826. st->codec.palctrl = &c->palette_control;
  827. st->codec.palctrl->palette_changed = 1;
  828. } else
  829. st->codec.palctrl = NULL;
  830. a.size = size;
  831. mov_read_default(c, pb, a);
  832. #endif
  833. } else {
  834. st->codec.codec_id = codec_get_id(mov_audio_tags, format);
  835. if(st->codec.codec_id==CODEC_ID_AMR_NB || st->codec.codec_id==CODEC_ID_AMR_WB) //from TS26.244
  836. {
  837. #ifdef DEBUG
  838. printf("AMR-NB or AMR-WB audio identified!!\n");
  839. #endif
  840. get_be32(pb);get_be32(pb); //Reserved_8
  841. get_be16(pb);//Reserved_2
  842. get_be16(pb);//Reserved_2
  843. get_be32(pb);//Reserved_4
  844. get_be16(pb);//TimeScale
  845. get_be16(pb);//Reserved_2
  846. //AMRSpecificBox.(10 bytes)
  847. get_be32(pb); //size
  848. get_be32(pb); //type=='damr'
  849. get_be32(pb); //vendor
  850. get_byte(pb); //decoder version
  851. get_be16(pb); //mode_set
  852. get_byte(pb); //mode_change_period
  853. get_byte(pb); //frames_per_sample
  854. st->duration = AV_NOPTS_VALUE;//Not possible to get from this info, must count number of AMR frames
  855. if(st->codec.codec_id==CODEC_ID_AMR_NB)
  856. {
  857. st->codec.sample_rate=8000;
  858. st->codec.channels=1;
  859. }
  860. else //AMR-WB
  861. {
  862. st->codec.sample_rate=16000;
  863. st->codec.channels=1;
  864. }
  865. st->codec.bits_per_sample=16;
  866. st->codec.bit_rate=0; /*It is not possible to tell this before we have
  867. an audio frame and even then every frame can be different*/
  868. }
  869. else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 's' ))
  870. {
  871. //This is some stuff for the hint track, lets ignore it!
  872. //Do some mp4 auto detect.
  873. c->mp4=1;
  874. size-=(16);
  875. url_fskip(pb, size); /* The mp4s atom also contians a esds atom that we can skip*/
  876. }
  877. else if(size>=(16+20))
  878. {//16 bytes read, reading atleast 20 more
  879. #ifdef DEBUG
  880. printf("audio size=0x%X\n",size);
  881. #endif
  882. uint16_t version = get_be16(pb); /* version */
  883. get_be16(pb); /* revision level */
  884. get_be32(pb); /* vendor */
  885. st->codec.channels = get_be16(pb); /* channel count */
  886. st->codec.bits_per_sample = get_be16(pb); /* sample size */
  887. /* handle specific s8 codec */
  888. get_be16(pb); /* compression id = 0*/
  889. get_be16(pb); /* packet size = 0 */
  890. st->codec.sample_rate = ((get_be32(pb) >> 16));
  891. //printf("CODECID %d %d %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
  892. switch (st->codec.codec_id) {
  893. case CODEC_ID_PCM_S16BE:
  894. if (st->codec.bits_per_sample == 8)
  895. st->codec.codec_id = CODEC_ID_PCM_S8;
  896. /* fall */
  897. case CODEC_ID_PCM_U8:
  898. st->codec.bit_rate = st->codec.sample_rate * 8;
  899. break;
  900. default:
  901. ;
  902. }
  903. //Read QT version 1 fields. In version 0 theese dont exist
  904. #ifdef DEBUG
  905. printf("version =%d mp4=%d\n",version,c->mp4);
  906. printf("size-(16+20+16)=%d\n",size-(16+20+16));
  907. #endif
  908. if((version==1) && size>=(16+20+16))
  909. {
  910. get_be32(pb); /* samples per packet */
  911. get_be32(pb); /* bytes per packet */
  912. get_be32(pb); /* bytes per frame */
  913. get_be32(pb); /* bytes per sample */
  914. if(size>(16+20+16))
  915. {
  916. //Optional, additional atom-based fields
  917. #ifdef DEBUG
  918. printf("offest=0x%X, sizeleft=%d=0x%x,format=%c%c%c%c\n",(int)url_ftell(pb),size - (16 + 20 + 16 ),size - (16 + 20 + 16 ),
  919. (format >> 0) & 0xff,
  920. (format >> 8) & 0xff,
  921. (format >> 16) & 0xff,
  922. (format >> 24) & 0xff);
  923. #endif
  924. MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
  925. mov_read_default(c, pb, a);
  926. }
  927. }
  928. else
  929. {
  930. //We should be down to 0 bytes here, but lets make sure.
  931. size-=(16+20);
  932. #ifdef DEBUG
  933. if(size>0)
  934. printf("skipping 0x%X bytes\n",size-(16+20));
  935. #endif
  936. url_fskip(pb, size);
  937. }
  938. }
  939. else
  940. {
  941. size-=16;
  942. //Unknown size, but lets do our best and skip the rest.
  943. #ifdef DEBUG
  944. printf("Strange size, skipping 0x%X bytes\n",size);
  945. #endif
  946. url_fskip(pb, size);
  947. }
  948. }
  949. }
  950. return 0;
  951. }
  952. static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  953. {
  954. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  955. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  956. int entries, i;
  957. print_atom("stsc", atom);
  958. get_byte(pb); /* version */
  959. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  960. entries = get_be32(pb);
  961. #ifdef DEBUG
  962. printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  963. #endif
  964. sc->sample_to_chunk_sz = entries;
  965. sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
  966. if (!sc->sample_to_chunk)
  967. return -1;
  968. for(i=0; i<entries; i++) {
  969. sc->sample_to_chunk[i].first = get_be32(pb);
  970. sc->sample_to_chunk[i].count = get_be32(pb);
  971. sc->sample_to_chunk[i].id = get_be32(pb);
  972. #ifdef DEBUG
  973. /* printf("sample_to_chunk first=%ld count=%ld, id=%ld\n", sc->sample_to_chunk[i].first, sc->sample_to_chunk[i].count, sc->sample_to_chunk[i].id); */
  974. #endif
  975. }
  976. return 0;
  977. }
  978. static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  979. {
  980. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  981. MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  982. int entries, i;
  983. print_atom("stsz", atom);
  984. get_byte(pb); /* version */
  985. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  986. sc->sample_size = get_be32(pb);
  987. entries = get_be32(pb);
  988. sc->sample_count = entries;
  989. #ifdef DEBUG
  990. printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
  991. #endif
  992. if(sc->sample_size)
  993. return 0; /* there isn't any table following */
  994. sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
  995. if (!sc->sample_sizes)
  996. return -1;
  997. for(i=0; i<entries; i++) {
  998. sc->sample_sizes[i] = get_be32(pb);
  999. #ifdef DEBUG
  1000. /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
  1001. #endif
  1002. }
  1003. return 0;
  1004. }
  1005. static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1006. {
  1007. AVStream *st = c->fc->streams[c->fc->nb_streams-1];
  1008. //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
  1009. int entries, i;
  1010. int duration=0;
  1011. int total_sample_count=0;
  1012. print_atom("stts", atom);
  1013. get_byte(pb); /* version */
  1014. get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
  1015. entries = get_be32(pb);
  1016. #ifdef DEBUG
  1017. printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
  1018. #endif
  1019. for(i=0; i<entries; i++) {
  1020. int sample_duration;
  1021. int sample_count;
  1022. sample_count=get_be32(pb);
  1023. sample_duration = get_be32(pb);
  1024. #ifdef DEBUG
  1025. printf("sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
  1026. #endif
  1027. duration+=sample_duration*sample_count;
  1028. total_sample_count+=sample_count;
  1029. #if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
  1030. if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
  1031. st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
  1032. st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
  1033. #ifdef DEBUG
  1034. printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
  1035. #endif
  1036. }
  1037. #endif
  1038. }
  1039. #define MAX(a,b) a>b?a:b
  1040. #define MIN(a,b) a>b?b:a
  1041. /*The stsd atom which contain codec type sometimes comes after the stts so we cannot check for codec_type*/
  1042. if(duration>0)
  1043. {
  1044. //Find greatest common divisor to avoid overflow using the Euclidean Algorithm...
  1045. uint32_t max=MAX(duration,total_sample_count);
  1046. uint32_t min=MIN(duration,total_sample_count);
  1047. uint32_t spare=max%min;
  1048. while(spare!=0)
  1049. {
  1050. max=min;
  1051. min=spare;
  1052. spare=max%min;
  1053. }
  1054. duration/=min;
  1055. total_sample_count/=min;
  1056. //Only support constant frame rate. But lets calculate the average. Needed by .mp4-files created with nec e606 3g phone.
  1057. //To get better precision, we use the duration as frame_rate_base
  1058. st->codec.frame_rate_base=duration;
  1059. st->codec.frame_rate = c->streams[c->total_streams]->time_scale * total_sample_count;
  1060. #ifdef DEBUG
  1061. printf("FRAME RATE average (video or audio)= %f (tot sample count= %i ,tot dur= %i timescale=%d)\n", (float)st->codec.frame_rate/st->codec.frame_rate_base,total_sample_count,duration,c->streams[c->total_streams]->time_scale);
  1062. #endif
  1063. }
  1064. else
  1065. {
  1066. st->codec.frame_rate_base = 1;
  1067. st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
  1068. }
  1069. #undef MAX
  1070. #undef MIN
  1071. return 0;
  1072. }
  1073. static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1074. {
  1075. AVStream *st;
  1076. MOVStreamContext *sc;
  1077. print_atom("trak", atom);
  1078. st = av_new_stream(c->fc, c->fc->nb_streams);
  1079. if (!st) return -2;
  1080. sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
  1081. if (!sc) {
  1082. av_free(st);
  1083. return -1;
  1084. }
  1085. sc->sample_to_chunk_index = -1;
  1086. st->priv_data = sc;
  1087. st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
  1088. st->start_time = 0; /* XXX: check */
  1089. st->duration = (c->duration * (int64_t)AV_TIME_BASE) / c->time_scale;
  1090. c->streams[c->fc->nb_streams-1] = sc;
  1091. return mov_read_default(c, pb, atom);
  1092. }
  1093. static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1094. {
  1095. AVStream *st;
  1096. print_atom("tkhd", atom);
  1097. st = c->fc->streams[c->fc->nb_streams-1];
  1098. get_byte(pb); /* version */
  1099. get_byte(pb); get_byte(pb);
  1100. get_byte(pb); /* flags */
  1101. /*
  1102. MOV_TRACK_ENABLED 0x0001
  1103. MOV_TRACK_IN_MOVIE 0x0002
  1104. MOV_TRACK_IN_PREVIEW 0x0004
  1105. MOV_TRACK_IN_POSTER 0x0008
  1106. */
  1107. get_be32(pb); /* creation time */
  1108. get_be32(pb); /* modification time */
  1109. st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
  1110. get_be32(pb); /* reserved */
  1111. st->start_time = 0; /* check */
  1112. st->duration = (get_be32(pb) * (int64_t)AV_TIME_BASE) / c->time_scale; /* duration */
  1113. get_be32(pb); /* reserved */
  1114. get_be32(pb); /* reserved */
  1115. get_be16(pb); /* layer */
  1116. get_be16(pb); /* alternate group */
  1117. get_be16(pb); /* volume */
  1118. get_be16(pb); /* reserved */
  1119. url_fskip(pb, 36); /* display matrix */
  1120. /* those are fixed-point */
  1121. st->codec.width = get_be32(pb) >> 16; /* track width */
  1122. st->codec.height = get_be32(pb) >> 16; /* track height */
  1123. return 0;
  1124. }
  1125. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1126. /* like the files created with Adobe Premiere 5.0, for samples see */
  1127. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1128. static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1129. {
  1130. int err;
  1131. #ifdef DEBUG
  1132. print_atom("wide", atom);
  1133. debug_indent++;
  1134. #endif
  1135. if (atom.size < 8)
  1136. return 0; /* continue */
  1137. if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1138. url_fskip(pb, atom.size - 4);
  1139. return 0;
  1140. }
  1141. atom.type = get_le32(pb);
  1142. atom.offset += 8;
  1143. atom.size -= 8;
  1144. if (atom.type != MKTAG('m', 'd', 'a', 't')) {
  1145. url_fskip(pb, atom.size);
  1146. return 0;
  1147. }
  1148. err = mov_read_mdat(c, pb, atom);
  1149. #ifdef DEBUG
  1150. debug_indent--;
  1151. #endif
  1152. return err;
  1153. }
  1154. #ifdef CONFIG_ZLIB
  1155. static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
  1156. {
  1157. return -1;
  1158. }
  1159. static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
  1160. {
  1161. ByteIOContext ctx;
  1162. uint8_t *cmov_data;
  1163. uint8_t *moov_data; /* uncompressed data */
  1164. long cmov_len, moov_len;
  1165. int ret;
  1166. print_atom("cmov", atom);
  1167. get_be32(pb); /* dcom atom */
  1168. if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
  1169. return -1;
  1170. if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
  1171. dprintf("unknown compression for cmov atom !");
  1172. return -1;
  1173. }
  1174. get_be32(pb); /* cmvd atom */
  1175. if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
  1176. return -1;
  1177. moov_len = get_be32(pb); /* uncompressed size */
  1178. cmov_len = atom.size - 6 * 4;
  1179. cmov_data = (uint8_t *) av_malloc(cmov_len);
  1180. if (!cmov_data)
  1181. return -1;
  1182. moov_data = (uint8_t *) av_malloc(moov_len);
  1183. if (!moov_data) {
  1184. av_free(cmov_data);
  1185. return -1;
  1186. }
  1187. get_buffer(pb, cmov_data, cmov_len);
  1188. if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1189. return -1;
  1190. if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
  1191. return -1;
  1192. ctx.buf_end = ctx.buffer + moov_len;
  1193. atom.type = MKTAG( 'm', 'o', 'o', 'v' );
  1194. atom.offset = 0;
  1195. atom.size = moov_len;
  1196. #ifdef DEBUG
  1197. { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
  1198. #endif
  1199. ret = mov_read_default(c, &ctx, atom);
  1200. av_free(moov_data);
  1201. av_free(cmov_data);
  1202. return ret;
  1203. }
  1204. #endif
  1205. static const MOVParseTableEntry mov_default_parse_table[] = {
  1206. /* mp4 atoms */
  1207. { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
  1208. { MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
  1209. { MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
  1210. { MKTAG( 'c', 't', 't', 's' ), mov_read_leaf }, /* composition time to sample */
  1211. { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
  1212. { MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
  1213. { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
  1214. { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
  1215. { MKTAG( 'e', 'l', 's', 't' ), mov_read_leaf },
  1216. { MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
  1217. { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
  1218. { MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
  1219. { MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
  1220. { MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
  1221. { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
  1222. { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
  1223. { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
  1224. { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
  1225. { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
  1226. { MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
  1227. { MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
  1228. { MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
  1229. { MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
  1230. { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
  1231. { MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
  1232. { MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
  1233. { MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
  1234. { MKTAG( 's', 'k', 'i', 'p' ), mov_read_default },
  1235. { MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
  1236. { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorrenson extension ??? */
  1237. { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
  1238. { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
  1239. { MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
  1240. { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
  1241. { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
  1242. { MKTAG( 's', 't', 's', 'h' ), mov_read_default },
  1243. { MKTAG( 's', 't', 's', 's' ), mov_read_leaf }, /* sync sample */
  1244. { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
  1245. { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
  1246. { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
  1247. { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
  1248. { MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
  1249. { MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
  1250. { MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
  1251. { MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
  1252. { MKTAG( 'u', 'u', 'i', 'd' ), mov_read_default },
  1253. { MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
  1254. { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },
  1255. /* extra mp4 */
  1256. { MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
  1257. /* QT atoms */
  1258. { MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
  1259. { MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
  1260. { MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
  1261. { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
  1262. { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
  1263. { MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
  1264. { MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
  1265. { MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
  1266. { MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
  1267. { MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
  1268. { MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
  1269. { MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
  1270. { MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
  1271. { MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
  1272. { MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
  1273. { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
  1274. //{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
  1275. #ifdef CONFIG_ZLIB
  1276. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
  1277. #else
  1278. { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
  1279. #endif
  1280. { 0L, mov_read_leaf }
  1281. };
  1282. static void mov_free_stream_context(MOVStreamContext *sc)
  1283. {
  1284. if(sc) {
  1285. av_free(sc->chunk_offsets);
  1286. av_free(sc->sample_to_chunk);
  1287. av_free(sc->sample_sizes);
  1288. av_free(sc->header_data);
  1289. av_free(sc);
  1290. }
  1291. }
  1292. static inline uint32_t mov_to_tag(uint8_t *buf)
  1293. {
  1294. return MKTAG(buf[0], buf[1], buf[2], buf[3]);
  1295. }
  1296. static inline uint32_t to_be32(uint8_t *buf)
  1297. {
  1298. return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  1299. }
  1300. /* XXX: is it sufficient ? */
  1301. static int mov_probe(AVProbeData *p)
  1302. {
  1303. unsigned int offset;
  1304. uint32_t tag;
  1305. /* check file header */
  1306. if (p->buf_size <= 12)
  1307. return 0;
  1308. offset = 0;
  1309. for(;;) {
  1310. /* ignore invalid offset */
  1311. if ((offset + 8) > (unsigned int)p->buf_size)
  1312. return 0;
  1313. tag = mov_to_tag(p->buf + offset + 4);
  1314. switch(tag) {
  1315. case MKTAG( 'm', 'o', 'o', 'v' ):
  1316. case MKTAG( 'w', 'i', 'd', 'e' ):
  1317. case MKTAG( 'f', 'r', 'e', 'e' ):
  1318. case MKTAG( 'm', 'd', 'a', 't' ):
  1319. case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
  1320. case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
  1321. return AVPROBE_SCORE_MAX;
  1322. case MKTAG( 'f', 't', 'y', 'p' ):
  1323. case MKTAG( 's', 'k', 'i', 'p' ):
  1324. offset = to_be32(p->buf+offset) + offset;
  1325. break;
  1326. default:
  1327. /* unrecognized tag */
  1328. return 0;
  1329. }
  1330. }
  1331. return 0;
  1332. }
  1333. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1334. {
  1335. MOVContext *mov = (MOVContext *) s->priv_data;
  1336. ByteIOContext *pb = &s->pb;
  1337. int i, j, nb, err;
  1338. MOV_atom_t atom = { 0, 0, 0 };
  1339. mov->fc = s;
  1340. mov->parse_table = mov_default_parse_table;
  1341. #if 0
  1342. /* XXX: I think we should auto detect */
  1343. if(s->iformat->name[1] == 'p')
  1344. mov->mp4 = 1;
  1345. #endif
  1346. if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  1347. atom.size = url_filesize(url_fileno(pb));
  1348. else
  1349. atom.size = 0x7FFFFFFFFFFFFFFFLL;
  1350. #ifdef DEBUG
  1351. printf("filesz=%Ld\n", atom.size);
  1352. #endif
  1353. /* check MOV header */
  1354. err = mov_read_default(mov, pb, atom);
  1355. if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
  1356. fprintf(stderr, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%lld\n",
  1357. err, mov->found_moov, mov->found_mdat, url_ftell(pb));
  1358. return -1;
  1359. }
  1360. #ifdef DEBUG
  1361. printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
  1362. #endif
  1363. /* some cleanup : make sure we are on the mdat atom */
  1364. if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
  1365. url_fseek(pb, mov->mdat_offset, SEEK_SET);
  1366. mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
  1367. #ifdef DEBUG
  1368. printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
  1369. #endif
  1370. #ifdef DEBUG
  1371. printf("streams= %d\n", s->nb_streams);
  1372. #endif
  1373. mov->total_streams = nb = s->nb_streams;
  1374. #if 1
  1375. for(i=0; i<s->nb_streams;) {
  1376. if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
  1377. av_free(s->streams[i]);
  1378. for(j=i+1; j<s->nb_streams; j++)
  1379. s->streams[j-1] = s->streams[j];
  1380. s->nb_streams--;
  1381. } else
  1382. i++;
  1383. }
  1384. for(i=0; i<s->nb_streams;i++) {
  1385. MOVStreamContext *sc;
  1386. sc = (MOVStreamContext *)s->streams[i]->priv_data;
  1387. sc->ffindex = i;
  1388. sc->is_ff_stream = 1;
  1389. }
  1390. #endif
  1391. #ifdef DEBUG
  1392. printf("real streams= %d\n", s->nb_streams);
  1393. #endif
  1394. return 0;
  1395. }
  1396. /* Yes, this is ugly... I didn't write the specs of QT :p */
  1397. /* XXX:remove useless commented code sometime */
  1398. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  1399. {
  1400. MOVContext *mov = (MOVContext *) s->priv_data;
  1401. MOVStreamContext *sc;
  1402. int64_t offset = 0x0FFFFFFFFFFFFFFFLL;
  1403. int i;
  1404. int size;
  1405. size = 0x0FFFFFFF;
  1406. #ifdef MOV_SPLIT_CHUNKS
  1407. if (mov->partial) {
  1408. int idx;
  1409. sc = mov->partial;
  1410. idx = sc->sample_to_chunk_index;
  1411. if (idx < 0) return 0;
  1412. size = sc->sample_sizes[sc->current_sample];
  1413. sc->current_sample++;
  1414. sc->left_in_chunk--;
  1415. if (sc->left_in_chunk <= 0)
  1416. mov->partial = 0;
  1417. offset = mov->next_chunk_offset;
  1418. /* extract the sample */
  1419. goto readchunk;
  1420. }
  1421. #endif
  1422. again:
  1423. sc = 0;
  1424. for(i=0; i<mov->total_streams; i++) {
  1425. MOVStreamContext *msc = mov->streams[i];
  1426. //printf("MOCHUNK %ld %d %p pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb));
  1427. if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
  1428. && (msc->chunk_offsets[msc->next_chunk] < offset)) {
  1429. sc = msc;
  1430. offset = msc->chunk_offsets[msc->next_chunk];
  1431. //printf("SELETED %Ld i:%d\n", offset, i);
  1432. }
  1433. }
  1434. if (!sc || offset==0x0FFFFFFFFFFFFFFFLL)
  1435. return -1;
  1436. sc->next_chunk++;
  1437. if(mov->next_chunk_offset < offset) { /* some meta data */
  1438. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1439. mov->next_chunk_offset = offset;
  1440. }
  1441. //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
  1442. if(!sc->is_ff_stream) {
  1443. url_fskip(&s->pb, (offset - mov->next_chunk_offset));
  1444. mov->next_chunk_offset = offset;
  1445. offset = 0x0FFFFFFFFFFFFFFFLL;
  1446. goto again;
  1447. }
  1448. /* now get the chunk size... */
  1449. for(i=0; i<mov->total_streams; i++) {
  1450. MOVStreamContext *msc = mov->streams[i];
  1451. if ((msc->next_chunk < msc->chunk_count)
  1452. && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
  1453. size = msc->chunk_offsets[msc->next_chunk] - offset;
  1454. }
  1455. #ifdef MOV_MINOLTA_FIX
  1456. //Make sure that size is according to sample_size (Needed by .mov files
  1457. //created on a Minolta Dimage Xi where audio chunks contains waste data in the end)
  1458. //Maybe we should really not only check sc->sample_size, but also sc->sample_sizes
  1459. //but I have no such movies
  1460. if (sc->sample_size > 0) {
  1461. int foundsize=0;
  1462. for(i=0; i<(sc->sample_to_chunk_sz); i++) {
  1463. if( (sc->sample_to_chunk[i].first)<=(sc->next_chunk) && (sc->sample_size>0) )
  1464. {
  1465. foundsize=sc->sample_to_chunk[i].count*sc->sample_size;
  1466. }
  1467. #ifdef DEBUG
  1468. /*printf("sample_to_chunk first=%ld count=%ld, id=%ld\n", sc->sample_to_chunk[i].first, sc->sample_to_chunk[i].count, sc->sample_to_chunk[i].id);*/
  1469. #endif
  1470. }
  1471. if( (foundsize>0) && (foundsize<size) )
  1472. {
  1473. #ifdef DEBUG
  1474. /*printf("this size should actually be %d\n",foundsize);*/
  1475. #endif
  1476. size=foundsize;
  1477. }
  1478. }
  1479. #endif //MOV_MINOLTA_FIX
  1480. #ifdef MOV_SPLIT_CHUNKS
  1481. /* split chunks into samples */
  1482. if (sc->sample_size == 0) {
  1483. int idx = sc->sample_to_chunk_index;
  1484. if ((idx + 1 < sc->sample_to_chunk_sz)
  1485. && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
  1486. idx++;
  1487. sc->sample_to_chunk_index = idx;
  1488. if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
  1489. mov->partial = sc;
  1490. /* we'll have to get those samples before next chunk */
  1491. sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
  1492. size = sc->sample_sizes[sc->current_sample];
  1493. }
  1494. sc->current_sample++;
  1495. }
  1496. #endif
  1497. readchunk:
  1498. //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
  1499. if(size == 0x0FFFFFFF)
  1500. size = mov->mdat_size + mov->mdat_offset - offset;
  1501. if(size < 0)
  1502. return -1;
  1503. if(size == 0)
  1504. return -1;
  1505. url_fseek(&s->pb, offset, SEEK_SET);
  1506. //printf("READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
  1507. if (sc->header_len > 0) {
  1508. av_new_packet(pkt, size + sc->header_len);
  1509. memcpy(pkt->data, sc->header_data, sc->header_len);
  1510. get_buffer(&s->pb, pkt->data + sc->header_len, size);
  1511. /* free header */
  1512. av_freep(&sc->header_data);
  1513. sc->header_len = 0;
  1514. } else {
  1515. av_new_packet(pkt, size);
  1516. get_buffer(&s->pb, pkt->data, pkt->size);
  1517. }
  1518. pkt->stream_index = sc->ffindex;
  1519. #ifdef DEBUG
  1520. /*
  1521. printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
  1522. for(i=0; i<8; i++)
  1523. printf("%02x ", pkt->data[i]);
  1524. for(i=0; i<8; i++)
  1525. printf("%c ", (pkt->data[i]) & 0x7F);
  1526. puts("");
  1527. */
  1528. #endif
  1529. mov->next_chunk_offset = offset + size;
  1530. return 0;
  1531. }
  1532. static int mov_read_close(AVFormatContext *s)
  1533. {
  1534. int i;
  1535. MOVContext *mov = (MOVContext *) s->priv_data;
  1536. for(i=0; i<mov->total_streams; i++)
  1537. mov_free_stream_context(mov->streams[i]);
  1538. /* free color tabs */
  1539. for(i=0; i<mov->ctab_size; i++)
  1540. av_freep(&mov->ctab[i]);
  1541. av_freep(&mov->ctab);
  1542. return 0;
  1543. }
  1544. static AVInputFormat mov_iformat = {
  1545. "mov,mp4,m4a,3gp",
  1546. "QuickTime/MPEG4 format",
  1547. sizeof(MOVContext),
  1548. mov_probe,
  1549. mov_read_header,
  1550. mov_read_packet,
  1551. mov_read_close,
  1552. };
  1553. int mov_init(void)
  1554. {
  1555. av_register_input_format(&mov_iformat);
  1556. return 0;
  1557. }