PageRenderTime 62ms CodeModel.GetById 21ms 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

Large files files are truncated, but you can click here to view the full file

  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))

Large files files are truncated, but you can click here to view the full file