PageRenderTime 67ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/amffmpeg/libavformat/mov.c

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