00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <limits.h>
00024
00025
00026
00027
00028 #include "libavutil/audioconvert.h"
00029 #include "libavutil/intreadwrite.h"
00030 #include "libavutil/intfloat.h"
00031 #include "libavutil/mathematics.h"
00032 #include "libavutil/avstring.h"
00033 #include "libavutil/dict.h"
00034 #include "libavcodec/ac3tab.h"
00035 #include "avformat.h"
00036 #include "internal.h"
00037 #include "avio_internal.h"
00038 #include "riff.h"
00039 #include "isom.h"
00040 #include "libavcodec/get_bits.h"
00041 #include "id3v1.h"
00042 #include "mov_chan.h"
00043
00044 #if CONFIG_ZLIB
00045 #include <zlib.h>
00046 #endif
00047
00048
00049
00050
00051
00052
00053 #include "qtpalette.h"
00054
00055
00056 #undef NDEBUG
00057 #include <assert.h>
00058
00059
00060
00061 typedef struct MOVParseTableEntry {
00062 uint32_t type;
00063 int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
00064 } MOVParseTableEntry;
00065
00066 static const MOVParseTableEntry mov_default_parse_table[];
00067
00068 static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
00069 unsigned len, const char *key)
00070 {
00071 char buf[16];
00072
00073 short current, total;
00074 avio_rb16(pb);
00075 current = avio_rb16(pb);
00076 total = avio_rb16(pb);
00077 if (!total)
00078 snprintf(buf, sizeof(buf), "%d", current);
00079 else
00080 snprintf(buf, sizeof(buf), "%d/%d", current, total);
00081 av_dict_set(&c->fc->metadata, key, buf, 0);
00082
00083 return 0;
00084 }
00085
00086 static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
00087 unsigned len, const char *key)
00088 {
00089 char buf[16];
00090
00091
00092 avio_r8(pb);
00093 avio_r8(pb);
00094 avio_r8(pb);
00095
00096 snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
00097 av_dict_set(&c->fc->metadata, key, buf, 0);
00098
00099 return 0;
00100 }
00101
00102 static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
00103 unsigned len, const char *key)
00104 {
00105 char buf[16];
00106
00107 snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
00108 av_dict_set(&c->fc->metadata, key, buf, 0);
00109
00110 return 0;
00111 }
00112
00113 static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
00114 unsigned len, const char *key)
00115 {
00116 short genre;
00117 char buf[20];
00118
00119 avio_r8(pb);
00120
00121 genre = avio_r8(pb);
00122 if (genre < 1 || genre > ID3v1_GENRE_MAX)
00123 return 0;
00124 snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
00125 av_dict_set(&c->fc->metadata, key, buf, 0);
00126
00127 return 0;
00128 }
00129
00130 static const uint32_t mac_to_unicode[128] = {
00131 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
00132 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
00133 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
00134 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
00135 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
00136 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
00137 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
00138 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
00139 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
00140 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
00141 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
00142 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
00143 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
00144 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
00145 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
00146 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
00147 };
00148
00149 static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
00150 char *dst, int dstlen)
00151 {
00152 char *p = dst;
00153 char *end = dst+dstlen-1;
00154 int i;
00155
00156 for (i = 0; i < len; i++) {
00157 uint8_t t, c = avio_r8(pb);
00158 if (c < 0x80 && p < end)
00159 *p++ = c;
00160 else
00161 PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
00162 }
00163 *p = 0;
00164 return p - dst;
00165 }
00166
00167 static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00168 {
00169 #ifdef MOV_EXPORT_ALL_METADATA
00170 char tmp_key[5];
00171 #endif
00172 char str[1024], key2[16], language[4] = {0};
00173 const char *key = NULL;
00174 uint16_t str_size, langcode = 0;
00175 uint32_t data_type = 0;
00176 int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
00177
00178 switch (atom.type) {
00179 case MKTAG(0xa9,'n','a','m'): key = "title"; break;
00180 case MKTAG(0xa9,'a','u','t'):
00181 case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
00182 case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
00183 case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
00184 case MKTAG( 'c','p','r','t'):
00185 case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
00186 case MKTAG(0xa9,'c','m','t'):
00187 case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
00188 case MKTAG(0xa9,'a','l','b'): key = "album"; break;
00189 case MKTAG(0xa9,'d','a','y'): key = "date"; break;
00190 case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
00191 case MKTAG( 'g','n','r','e'): key = "genre";
00192 parse = mov_metadata_gnre; break;
00193 case MKTAG(0xa9,'t','o','o'):
00194 case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
00195 case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
00196 case MKTAG( 'd','e','s','c'): key = "description";break;
00197 case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
00198 case MKTAG( 't','v','s','h'): key = "show"; break;
00199 case MKTAG( 't','v','e','n'): key = "episode_id";break;
00200 case MKTAG( 't','v','n','n'): key = "network"; break;
00201 case MKTAG( 't','r','k','n'): key = "track";
00202 parse = mov_metadata_track_or_disc_number; break;
00203 case MKTAG( 'd','i','s','k'): key = "disc";
00204 parse = mov_metadata_track_or_disc_number; break;
00205 case MKTAG( 't','v','e','s'): key = "episode_sort";
00206 parse = mov_metadata_int8_bypass_padding; break;
00207 case MKTAG( 't','v','s','n'): key = "season_number";
00208 parse = mov_metadata_int8_bypass_padding; break;
00209 case MKTAG( 's','t','i','k'): key = "media_type";
00210 parse = mov_metadata_int8_no_padding; break;
00211 case MKTAG( 'h','d','v','d'): key = "hd_video";
00212 parse = mov_metadata_int8_no_padding; break;
00213 case MKTAG( 'p','g','a','p'): key = "gapless_playback";
00214 parse = mov_metadata_int8_no_padding; break;
00215 }
00216
00217 if (c->itunes_metadata && atom.size > 8) {
00218 int data_size = avio_rb32(pb);
00219 int tag = avio_rl32(pb);
00220 if (tag == MKTAG('d','a','t','a')) {
00221 data_type = avio_rb32(pb);
00222 avio_rb32(pb);
00223 str_size = data_size - 16;
00224 atom.size -= 16;
00225 } else return 0;
00226 } else if (atom.size > 4 && key && !c->itunes_metadata) {
00227 str_size = avio_rb16(pb);
00228 langcode = avio_rb16(pb);
00229 ff_mov_lang_to_iso639(langcode, language);
00230 atom.size -= 4;
00231 } else
00232 str_size = atom.size;
00233
00234 #ifdef MOV_EXPORT_ALL_METADATA
00235 if (!key) {
00236 snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
00237 key = tmp_key;
00238 }
00239 #endif
00240
00241 if (!key)
00242 return 0;
00243 if (atom.size < 0)
00244 return AVERROR_INVALIDDATA;
00245
00246 str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
00247
00248 if (parse)
00249 parse(c, pb, str_size, key);
00250 else {
00251 if (data_type == 3 || (data_type == 0 && langcode < 0x800)) {
00252 mov_read_mac_string(c, pb, str_size, str, sizeof(str));
00253 } else {
00254 avio_read(pb, str, str_size);
00255 str[str_size] = 0;
00256 }
00257 av_dict_set(&c->fc->metadata, key, str, 0);
00258 if (*language && strcmp(language, "und")) {
00259 snprintf(key2, sizeof(key2), "%s-%s", key, language);
00260 av_dict_set(&c->fc->metadata, key2, str, 0);
00261 }
00262 }
00263 av_dlog(c->fc, "lang \"%3s\" ", language);
00264 av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
00265 key, str, (char*)&atom.type, str_size, atom.size);
00266
00267 return 0;
00268 }
00269
00270 static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00271 {
00272 int64_t start;
00273 int i, nb_chapters, str_len, version;
00274 char str[256+1];
00275
00276 if ((atom.size -= 5) < 0)
00277 return 0;
00278
00279 version = avio_r8(pb);
00280 avio_rb24(pb);
00281 if (version)
00282 avio_rb32(pb);
00283 nb_chapters = avio_r8(pb);
00284
00285 for (i = 0; i < nb_chapters; i++) {
00286 if (atom.size < 9)
00287 return 0;
00288
00289 start = avio_rb64(pb);
00290 str_len = avio_r8(pb);
00291
00292 if ((atom.size -= 9+str_len) < 0)
00293 return 0;
00294
00295 avio_read(pb, str, str_len);
00296 str[str_len] = 0;
00297 avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
00298 }
00299 return 0;
00300 }
00301
00302 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00303 {
00304 int64_t total_size = 0;
00305 MOVAtom a;
00306 int i;
00307
00308 if (atom.size < 0)
00309 atom.size = INT64_MAX;
00310 while (total_size + 8 < atom.size && !pb->eof_reached) {
00311 int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
00312 a.size = atom.size;
00313 a.type=0;
00314 if (atom.size >= 8) {
00315 a.size = avio_rb32(pb);
00316 a.type = avio_rl32(pb);
00317 }
00318 av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
00319 a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
00320 total_size += 8;
00321 if (a.size == 1) {
00322 a.size = avio_rb64(pb) - 8;
00323 total_size += 8;
00324 }
00325 if (a.size == 0) {
00326 a.size = atom.size - total_size;
00327 if (a.size <= 8)
00328 break;
00329 }
00330 a.size -= 8;
00331 if (a.size < 0)
00332 break;
00333 a.size = FFMIN(a.size, atom.size - total_size);
00334
00335 for (i = 0; mov_default_parse_table[i].type; i++)
00336 if (mov_default_parse_table[i].type == a.type) {
00337 parse = mov_default_parse_table[i].parse;
00338 break;
00339 }
00340
00341
00342 if (!parse && (atom.type == MKTAG('u','d','t','a') ||
00343 atom.type == MKTAG('i','l','s','t')))
00344 parse = mov_read_udta_string;
00345
00346 if (!parse) {
00347 avio_skip(pb, a.size);
00348 } else {
00349 int64_t start_pos = avio_tell(pb);
00350 int64_t left;
00351 int err = parse(c, pb, a);
00352 if (err < 0)
00353 return err;
00354 if (c->found_moov && c->found_mdat &&
00355 (!pb->seekable || start_pos + a.size == avio_size(pb)))
00356 return 0;
00357 left = a.size - avio_tell(pb) + start_pos;
00358 if (left > 0)
00359 avio_skip(pb, left);
00360 else if (left < 0) {
00361 av_log(c->fc, AV_LOG_WARNING,
00362 "overread end of atom '%.4s' by %"PRId64" bytes\n",
00363 (char*)&a.type, -left);
00364 avio_seek(pb, left, SEEK_CUR);
00365 }
00366 }
00367
00368 total_size += a.size;
00369 }
00370
00371 if (total_size < atom.size && atom.size < 0x7ffff)
00372 avio_skip(pb, atom.size - total_size);
00373
00374 return 0;
00375 }
00376
00377 static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00378 {
00379 AVStream *st;
00380 MOVStreamContext *sc;
00381 int entries, i, j;
00382
00383 if (c->fc->nb_streams < 1)
00384 return 0;
00385 st = c->fc->streams[c->fc->nb_streams-1];
00386 sc = st->priv_data;
00387
00388 avio_rb32(pb);
00389 entries = avio_rb32(pb);
00390 if (entries >= UINT_MAX / sizeof(*sc->drefs))
00391 return AVERROR_INVALIDDATA;
00392 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
00393 if (!sc->drefs)
00394 return AVERROR(ENOMEM);
00395 sc->drefs_count = entries;
00396
00397 for (i = 0; i < sc->drefs_count; i++) {
00398 MOVDref *dref = &sc->drefs[i];
00399 uint32_t size = avio_rb32(pb);
00400 int64_t next = avio_tell(pb) + size - 4;
00401
00402 if (size < 12)
00403 return AVERROR_INVALIDDATA;
00404
00405 dref->type = avio_rl32(pb);
00406 avio_rb32(pb);
00407 av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
00408
00409 if (dref->type == MKTAG('a','l','i','s') && size > 150) {
00410
00411 uint16_t volume_len, len;
00412 int16_t type;
00413
00414 avio_skip(pb, 10);
00415
00416 volume_len = avio_r8(pb);
00417 volume_len = FFMIN(volume_len, 27);
00418 avio_read(pb, dref->volume, 27);
00419 dref->volume[volume_len] = 0;
00420 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
00421
00422 avio_skip(pb, 12);
00423
00424 len = avio_r8(pb);
00425 len = FFMIN(len, 63);
00426 avio_read(pb, dref->filename, 63);
00427 dref->filename[len] = 0;
00428 av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
00429
00430 avio_skip(pb, 16);
00431
00432
00433 dref->nlvl_from = avio_rb16(pb);
00434 dref->nlvl_to = avio_rb16(pb);
00435 av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
00436 dref->nlvl_from, dref->nlvl_to);
00437
00438 avio_skip(pb, 16);
00439
00440 for (type = 0; type != -1 && avio_tell(pb) < next; ) {
00441 type = avio_rb16(pb);
00442 len = avio_rb16(pb);
00443 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
00444 if (len&1)
00445 len += 1;
00446 if (type == 2) {
00447 av_free(dref->path);
00448 dref->path = av_mallocz(len+1);
00449 if (!dref->path)
00450 return AVERROR(ENOMEM);
00451 avio_read(pb, dref->path, len);
00452 if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
00453 len -= volume_len;
00454 memmove(dref->path, dref->path+volume_len, len);
00455 dref->path[len] = 0;
00456 }
00457 for (j = 0; j < len; j++)
00458 if (dref->path[j] == ':')
00459 dref->path[j] = '/';
00460 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
00461 } else if (type == 0) {
00462 av_free(dref->dir);
00463 dref->dir = av_malloc(len+1);
00464 if (!dref->dir)
00465 return AVERROR(ENOMEM);
00466 avio_read(pb, dref->dir, len);
00467 dref->dir[len] = 0;
00468 for (j = 0; j < len; j++)
00469 if (dref->dir[j] == ':')
00470 dref->dir[j] = '/';
00471 av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
00472 } else
00473 avio_skip(pb, len);
00474 }
00475 }
00476 avio_seek(pb, next, SEEK_SET);
00477 }
00478 return 0;
00479 }
00480
00481 static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00482 {
00483 AVStream *st;
00484 uint32_t type;
00485 uint32_t av_unused ctype;
00486
00487 if (c->fc->nb_streams < 1)
00488 return 0;
00489
00490 st = c->fc->streams[c->fc->nb_streams-1];
00491
00492 avio_r8(pb);
00493 avio_rb24(pb);
00494
00495
00496 ctype = avio_rl32(pb);
00497 type = avio_rl32(pb);
00498
00499 av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
00500 av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
00501
00502 if (type == MKTAG('v','i','d','e'))
00503 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00504 else if (type == MKTAG('s','o','u','n'))
00505 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00506 else if (type == MKTAG('m','1','a',' '))
00507 st->codec->codec_id = CODEC_ID_MP2;
00508 else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
00509 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00510
00511 avio_rb32(pb);
00512 avio_rb32(pb);
00513 avio_rb32(pb);
00514
00515 return 0;
00516 }
00517
00518 int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
00519 {
00520 AVStream *st;
00521 int tag;
00522
00523 if (fc->nb_streams < 1)
00524 return 0;
00525 st = fc->streams[fc->nb_streams-1];
00526
00527 avio_rb32(pb);
00528 ff_mp4_read_descr(fc, pb, &tag);
00529 if (tag == MP4ESDescrTag) {
00530 ff_mp4_parse_es_descr(pb, NULL);
00531 } else
00532 avio_rb16(pb);
00533
00534 ff_mp4_read_descr(fc, pb, &tag);
00535 if (tag == MP4DecConfigDescrTag)
00536 ff_mp4_read_dec_config_descr(fc, st, pb);
00537 return 0;
00538 }
00539
00540 static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00541 {
00542 return ff_mov_read_esds(c->fc, pb, atom);
00543 }
00544
00545 static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00546 {
00547 AVStream *st;
00548 int ac3info, acmod, lfeon, bsmod;
00549
00550 if (c->fc->nb_streams < 1)
00551 return 0;
00552 st = c->fc->streams[c->fc->nb_streams-1];
00553
00554 ac3info = avio_rb24(pb);
00555 bsmod = (ac3info >> 14) & 0x7;
00556 acmod = (ac3info >> 11) & 0x7;
00557 lfeon = (ac3info >> 10) & 0x1;
00558 st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
00559 st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
00560 if (lfeon)
00561 st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
00562 st->codec->audio_service_type = bsmod;
00563 if (st->codec->channels > 1 && bsmod == 0x7)
00564 st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
00565
00566 return 0;
00567 }
00568
00569 static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00570 {
00571 AVStream *st;
00572 uint8_t version;
00573 uint32_t flags, layout_tag, bitmap, num_descr, label_mask;
00574 int i;
00575
00576 if (c->fc->nb_streams < 1)
00577 return 0;
00578 st = c->fc->streams[c->fc->nb_streams-1];
00579
00580 if (atom.size < 16)
00581 return 0;
00582
00583 version = avio_r8(pb);
00584 flags = avio_rb24(pb);
00585
00586 layout_tag = avio_rb32(pb);
00587 bitmap = avio_rb32(pb);
00588 num_descr = avio_rb32(pb);
00589
00590 if (atom.size < 16ULL + num_descr * 20ULL)
00591 return 0;
00592
00593 av_dlog(c->fc, "chan: size=%ld version=%u flags=%u layout=%u bitmap=%u num_descr=%u\n",
00594 atom.size, version, flags, layout_tag, bitmap, num_descr);
00595
00596 label_mask = 0;
00597 for (i = 0; i < num_descr; i++) {
00598 uint32_t label, cflags;
00599 label = avio_rb32(pb);
00600 cflags = avio_rb32(pb);
00601 avio_rl32(pb);
00602 avio_rl32(pb);
00603 avio_rl32(pb);
00604 if (layout_tag == 0) {
00605 uint32_t mask_incr = ff_mov_get_channel_label(label);
00606 if (mask_incr == 0) {
00607 label_mask = 0;
00608 break;
00609 }
00610 label_mask |= mask_incr;
00611 }
00612 }
00613 if (layout_tag == 0)
00614 st->codec->channel_layout = label_mask;
00615 else
00616 st->codec->channel_layout = ff_mov_get_channel_layout(layout_tag, bitmap);
00617
00618 return 0;
00619 }
00620
00621 static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00622 {
00623 AVStream *st;
00624
00625 if (c->fc->nb_streams < 1)
00626 return 0;
00627 st = c->fc->streams[c->fc->nb_streams-1];
00628
00629 ff_get_wav_header(pb, st->codec, atom.size);
00630
00631 return 0;
00632 }
00633
00634 static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00635 {
00636 const int num = avio_rb32(pb);
00637 const int den = avio_rb32(pb);
00638 AVStream *st;
00639
00640 if (c->fc->nb_streams < 1)
00641 return 0;
00642 st = c->fc->streams[c->fc->nb_streams-1];
00643
00644 if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) &&
00645 (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
00646 av_log(c->fc, AV_LOG_WARNING,
00647 "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
00648 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
00649 num, den);
00650 } else if (den != 0) {
00651 st->sample_aspect_ratio.num = num;
00652 st->sample_aspect_ratio.den = den;
00653 }
00654 return 0;
00655 }
00656
00657
00658 static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00659 {
00660 if (atom.size == 0)
00661 return 0;
00662 c->found_mdat=1;
00663 return 0;
00664 }
00665
00666
00667 static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00668 {
00669 uint32_t minor_ver;
00670 int comp_brand_size;
00671 char minor_ver_str[11];
00672 char* comp_brands_str;
00673 uint8_t type[5] = {0};
00674
00675 avio_read(pb, type, 4);
00676 if (strcmp(type, "qt "))
00677 c->isom = 1;
00678 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
00679 av_dict_set(&c->fc->metadata, "major_brand", type, 0);
00680 minor_ver = avio_rb32(pb);
00681 snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
00682 av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
00683
00684 comp_brand_size = atom.size - 8;
00685 if (comp_brand_size < 0)
00686 return AVERROR_INVALIDDATA;
00687 comp_brands_str = av_malloc(comp_brand_size + 1);
00688 if (!comp_brands_str)
00689 return AVERROR(ENOMEM);
00690 avio_read(pb, comp_brands_str, comp_brand_size);
00691 comp_brands_str[comp_brand_size] = 0;
00692 av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
00693 av_freep(&comp_brands_str);
00694
00695 return 0;
00696 }
00697
00698
00699 static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00700 {
00701 int ret;
00702
00703 if ((ret = mov_read_default(c, pb, atom)) < 0)
00704 return ret;
00705
00706
00707 c->found_moov=1;
00708 return 0;
00709 }
00710
00711 static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00712 {
00713 c->fragment.moof_offset = avio_tell(pb) - 8;
00714 av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
00715 return mov_read_default(c, pb, atom);
00716 }
00717
00718 static void mov_metadata_creation_time(AVDictionary **metadata, time_t time)
00719 {
00720 char buffer[32];
00721 if (time) {
00722 struct tm *ptm;
00723 time -= 2082844800;
00724 ptm = gmtime(&time);
00725 if (!ptm) return;
00726 strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
00727 av_dict_set(metadata, "creation_time", buffer, 0);
00728 }
00729 }
00730
00731 static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00732 {
00733 AVStream *st;
00734 MOVStreamContext *sc;
00735 int version;
00736 char language[4] = {0};
00737 unsigned lang;
00738 time_t creation_time;
00739
00740 if (c->fc->nb_streams < 1)
00741 return 0;
00742 st = c->fc->streams[c->fc->nb_streams-1];
00743 sc = st->priv_data;
00744
00745 if (sc->time_scale) {
00746 av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
00747 return AVERROR_INVALIDDATA;
00748 }
00749
00750 version = avio_r8(pb);
00751 if (version > 1) {
00752 av_log_ask_for_sample(c, "unsupported version %d\n", version);
00753 return AVERROR_PATCHWELCOME;
00754 }
00755 avio_rb24(pb);
00756 if (version == 1) {
00757 creation_time = avio_rb64(pb);
00758 avio_rb64(pb);
00759 } else {
00760 creation_time = avio_rb32(pb);
00761 avio_rb32(pb);
00762 }
00763 mov_metadata_creation_time(&st->metadata, creation_time);
00764
00765 sc->time_scale = avio_rb32(pb);
00766 st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
00767
00768 lang = avio_rb16(pb);
00769 if (ff_mov_lang_to_iso639(lang, language))
00770 av_dict_set(&st->metadata, "language", language, 0);
00771 avio_rb16(pb);
00772
00773 return 0;
00774 }
00775
00776 static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00777 {
00778 time_t creation_time;
00779 int version = avio_r8(pb);
00780 avio_rb24(pb);
00781
00782 if (version == 1) {
00783 creation_time = avio_rb64(pb);
00784 avio_rb64(pb);
00785 } else {
00786 creation_time = avio_rb32(pb);
00787 avio_rb32(pb);
00788 }
00789 mov_metadata_creation_time(&c->fc->metadata, creation_time);
00790 c->time_scale = avio_rb32(pb);
00791
00792 av_dlog(c->fc, "time scale = %i\n", c->time_scale);
00793
00794 c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
00795 avio_rb32(pb);
00796
00797 avio_rb16(pb);
00798
00799 avio_skip(pb, 10);
00800
00801 avio_skip(pb, 36);
00802
00803 avio_rb32(pb);
00804 avio_rb32(pb);
00805 avio_rb32(pb);
00806 avio_rb32(pb);
00807 avio_rb32(pb);
00808 avio_rb32(pb);
00809 avio_rb32(pb);
00810
00811 return 0;
00812 }
00813
00814 static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00815 {
00816 AVStream *st;
00817
00818 if (c->fc->nb_streams < 1)
00819 return 0;
00820 st = c->fc->streams[c->fc->nb_streams-1];
00821
00822 if ((uint64_t)atom.size > (1<<30))
00823 return AVERROR_INVALIDDATA;
00824
00825
00826
00827 av_free(st->codec->extradata);
00828 st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
00829 if (!st->codec->extradata)
00830 return AVERROR(ENOMEM);
00831 st->codec->extradata_size = 0x5a + atom.size;
00832 memcpy(st->codec->extradata, "SVQ3", 4);
00833 avio_read(pb, st->codec->extradata + 0x5a, atom.size);
00834 av_dlog(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
00835 return 0;
00836 }
00837
00838 static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00839 {
00840 AVStream *st;
00841 int little_endian;
00842
00843 if (c->fc->nb_streams < 1)
00844 return 0;
00845 st = c->fc->streams[c->fc->nb_streams-1];
00846
00847 little_endian = avio_rb16(pb);
00848 av_dlog(c->fc, "enda %d\n", little_endian);
00849 if (little_endian == 1) {
00850 switch (st->codec->codec_id) {
00851 case CODEC_ID_PCM_S24BE:
00852 st->codec->codec_id = CODEC_ID_PCM_S24LE;
00853 break;
00854 case CODEC_ID_PCM_S32BE:
00855 st->codec->codec_id = CODEC_ID_PCM_S32LE;
00856 break;
00857 case CODEC_ID_PCM_F32BE:
00858 st->codec->codec_id = CODEC_ID_PCM_F32LE;
00859 break;
00860 case CODEC_ID_PCM_F64BE:
00861 st->codec->codec_id = CODEC_ID_PCM_F64LE;
00862 break;
00863 default:
00864 break;
00865 }
00866 }
00867 return 0;
00868 }
00869
00870 static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00871 {
00872 AVStream *st;
00873 unsigned mov_field_order;
00874 enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
00875
00876 if (c->fc->nb_streams < 1)
00877 return 0;
00878 st = c->fc->streams[c->fc->nb_streams-1];
00879 if (atom.size < 2)
00880 return AVERROR_INVALIDDATA;
00881 mov_field_order = avio_rb16(pb);
00882 if ((mov_field_order & 0xFF00) == 0x0100)
00883 decoded_field_order = AV_FIELD_PROGRESSIVE;
00884 else if ((mov_field_order & 0xFF00) == 0x0200) {
00885 switch (mov_field_order & 0xFF) {
00886 case 0x01: decoded_field_order = AV_FIELD_TT;
00887 break;
00888 case 0x06: decoded_field_order = AV_FIELD_BB;
00889 break;
00890 case 0x09: decoded_field_order = AV_FIELD_TB;
00891 break;
00892 case 0x0E: decoded_field_order = AV_FIELD_BT;
00893 break;
00894 }
00895 }
00896 if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
00897 av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
00898 }
00899 st->codec->field_order = decoded_field_order;
00900
00901 return 0;
00902 }
00903
00904
00905 static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00906 {
00907 AVStream *st;
00908 uint64_t size;
00909 uint8_t *buf;
00910
00911 if (c->fc->nb_streams < 1)
00912 return 0;
00913 st= c->fc->streams[c->fc->nb_streams-1];
00914 size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
00915 if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
00916 return AVERROR_INVALIDDATA;
00917 buf= av_realloc(st->codec->extradata, size);
00918 if (!buf)
00919 return AVERROR(ENOMEM);
00920 st->codec->extradata= buf;
00921 buf+= st->codec->extradata_size;
00922 st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
00923 AV_WB32( buf , atom.size + 8);
00924 AV_WL32( buf + 4, atom.type);
00925 avio_read(pb, buf + 8, atom.size);
00926 return 0;
00927 }
00928
00929 static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00930 {
00931 AVStream *st;
00932
00933 if (c->fc->nb_streams < 1)
00934 return 0;
00935 st = c->fc->streams[c->fc->nb_streams-1];
00936
00937 if ((uint64_t)atom.size > (1<<30))
00938 return AVERROR_INVALIDDATA;
00939
00940 if (st->codec->codec_id == CODEC_ID_QDM2 || st->codec->codec_id == CODEC_ID_QDMC) {
00941
00942 av_free(st->codec->extradata);
00943 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
00944 if (!st->codec->extradata)
00945 return AVERROR(ENOMEM);
00946 st->codec->extradata_size = atom.size;
00947 avio_read(pb, st->codec->extradata, atom.size);
00948 } else if (atom.size > 8) {
00949 int ret;
00950 if ((ret = mov_read_default(c, pb, atom)) < 0)
00951 return ret;
00952 } else
00953 avio_skip(pb, atom.size);
00954 return 0;
00955 }
00956
00961 static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00962 {
00963 AVStream *st;
00964
00965 if (c->fc->nb_streams < 1)
00966 return 0;
00967 st = c->fc->streams[c->fc->nb_streams-1];
00968
00969 if ((uint64_t)atom.size > (1<<30))
00970 return AVERROR_INVALIDDATA;
00971
00972 if (atom.size >= 10) {
00973
00974
00975 unsigned size = avio_rb32(pb);
00976 unsigned type = avio_rl32(pb);
00977 avio_seek(pb, -8, SEEK_CUR);
00978 if (type == MKTAG('f','i','e','l') && size == atom.size)
00979 return mov_read_default(c, pb, atom);
00980 }
00981 av_free(st->codec->extradata);
00982 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
00983 if (!st->codec->extradata)
00984 return AVERROR(ENOMEM);
00985 st->codec->extradata_size = atom.size;
00986 avio_read(pb, st->codec->extradata, atom.size);
00987 return 0;
00988 }
00989
00995 static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
00996 {
00997 AVStream *st;
00998
00999 if (c->fc->nb_streams < 1)
01000 return 0;
01001 if (atom.size <= 40)
01002 return 0;
01003 st = c->fc->streams[c->fc->nb_streams-1];
01004
01005 if ((uint64_t)atom.size > (1<<30))
01006 return AVERROR_INVALIDDATA;
01007
01008 av_free(st->codec->extradata);
01009 st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
01010 if (!st->codec->extradata)
01011 return AVERROR(ENOMEM);
01012 st->codec->extradata_size = atom.size - 40;
01013 avio_skip(pb, 40);
01014 avio_read(pb, st->codec->extradata, atom.size - 40);
01015 return 0;
01016 }
01017
01018 static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01019 {
01020 AVStream *st;
01021 MOVStreamContext *sc;
01022 unsigned int i, entries;
01023
01024 if (c->fc->nb_streams < 1)
01025 return 0;
01026 st = c->fc->streams[c->fc->nb_streams-1];
01027 sc = st->priv_data;
01028
01029 avio_r8(pb);
01030 avio_rb24(pb);
01031
01032 entries = avio_rb32(pb);
01033
01034 if (!entries)
01035 return 0;
01036 if (entries >= UINT_MAX/sizeof(int64_t))
01037 return AVERROR_INVALIDDATA;
01038
01039 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
01040 if (!sc->chunk_offsets)
01041 return AVERROR(ENOMEM);
01042 sc->chunk_count = entries;
01043
01044 if (atom.type == MKTAG('s','t','c','o'))
01045 for (i=0; i<entries; i++)
01046 sc->chunk_offsets[i] = avio_rb32(pb);
01047 else if (atom.type == MKTAG('c','o','6','4'))
01048 for (i=0; i<entries; i++)
01049 sc->chunk_offsets[i] = avio_rb64(pb);
01050 else
01051 return AVERROR_INVALIDDATA;
01052
01053 return 0;
01054 }
01055
01060 enum CodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
01061 {
01062 if (flags & 1) {
01063 if (flags & 2) {
01064 if (bps == 32) return CODEC_ID_PCM_F32BE;
01065 else if (bps == 64) return CODEC_ID_PCM_F64BE;
01066 } else {
01067 if (bps == 32) return CODEC_ID_PCM_F32LE;
01068 else if (bps == 64) return CODEC_ID_PCM_F64LE;
01069 }
01070 } else {
01071 if (flags & 2) {
01072 if (bps == 8)
01073
01074 if (flags & 4) return CODEC_ID_PCM_S8;
01075 else return CODEC_ID_PCM_U8;
01076 else if (bps == 16) return CODEC_ID_PCM_S16BE;
01077 else if (bps == 24) return CODEC_ID_PCM_S24BE;
01078 else if (bps == 32) return CODEC_ID_PCM_S32BE;
01079 } else {
01080 if (bps == 8)
01081 if (flags & 4) return CODEC_ID_PCM_S8;
01082 else return CODEC_ID_PCM_U8;
01083 else if (bps == 16) return CODEC_ID_PCM_S16LE;
01084 else if (bps == 24) return CODEC_ID_PCM_S24LE;
01085 else if (bps == 32) return CODEC_ID_PCM_S32LE;
01086 }
01087 }
01088 return CODEC_ID_NONE;
01089 }
01090
01091 int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
01092 {
01093 AVStream *st;
01094 MOVStreamContext *sc;
01095 int j, pseudo_stream_id;
01096
01097 if (c->fc->nb_streams < 1)
01098 return 0;
01099 st = c->fc->streams[c->fc->nb_streams-1];
01100 sc = st->priv_data;
01101
01102 for (pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
01103
01104 enum CodecID id;
01105 int dref_id = 1;
01106 MOVAtom a = { AV_RL32("stsd") };
01107 int64_t start_pos = avio_tell(pb);
01108 int size = avio_rb32(pb);
01109 uint32_t format = avio_rl32(pb);
01110
01111 if (size >= 16) {
01112 avio_rb32(pb);
01113 avio_rb16(pb);
01114 dref_id = avio_rb16(pb);
01115 }
01116
01117 if (st->codec->codec_tag &&
01118 st->codec->codec_tag != format &&
01119 (c->fc->video_codec_id ? ff_codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
01120 : st->codec->codec_tag != MKTAG('j','p','e','g'))
01121 ){
01122
01123
01124
01125 multiple_stsd:
01126 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
01127 avio_skip(pb, size - (avio_tell(pb) - start_pos));
01128 continue;
01129 }
01130
01131 if (st->codec->codec_tag && st->codec->codec_tag == AV_RL32("avc1"))
01132 goto multiple_stsd;
01133 sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
01134 sc->dref_id= dref_id;
01135
01136 st->codec->codec_tag = format;
01137 id = ff_codec_get_id(codec_movaudio_tags, format);
01138 if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
01139 id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format)&0xFFFF);
01140
01141 if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
01142 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01143 } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO &&
01144 format && format != MKTAG('m','p','4','s')) {
01145 id = ff_codec_get_id(codec_movvideo_tags, format);
01146 if (id <= 0)
01147 id = ff_codec_get_id(ff_codec_bmp_tags, format);
01148 if (id > 0)
01149 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01150 else if (st->codec->codec_type == AVMEDIA_TYPE_DATA){
01151 id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
01152 if (id > 0)
01153 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
01154 }
01155 }
01156
01157 av_dlog(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
01158 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
01159 (format >> 24) & 0xff, st->codec->codec_type);
01160
01161 if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
01162 unsigned int color_depth, len;
01163 int color_greyscale;
01164
01165 st->codec->codec_id = id;
01166 avio_rb16(pb);
01167 avio_rb16(pb);
01168 avio_rb32(pb);
01169 avio_rb32(pb);
01170 avio_rb32(pb);
01171
01172 st->codec->width = avio_rb16(pb);
01173 st->codec->height = avio_rb16(pb);
01174
01175 avio_rb32(pb);
01176 avio_rb32(pb);
01177 avio_rb32(pb);
01178 avio_rb16(pb);
01179
01180 len = avio_r8(pb);
01181 if (len > 31)
01182 len = 31;
01183 mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
01184 if (len < 31)
01185 avio_skip(pb, 31 - len);
01186
01187 if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
01188 st->codec->codec_tag=MKTAG('I', '4', '2', '0');
01189
01190 st->codec->bits_per_coded_sample = avio_rb16(pb);
01191 st->codec->color_table_id = avio_rb16(pb);
01192 av_dlog(c->fc, "depth %d, ctab id %d\n",
01193 st->codec->bits_per_coded_sample, st->codec->color_table_id);
01194
01195 color_depth = st->codec->bits_per_coded_sample & 0x1F;
01196 color_greyscale = st->codec->bits_per_coded_sample & 0x20;
01197
01198
01199 if ((color_depth == 2) || (color_depth == 4) ||
01200 (color_depth == 8)) {
01201
01202 unsigned int color_start, color_count, color_end;
01203 unsigned char r, g, b;
01204
01205 if (color_greyscale) {
01206 int color_index, color_dec;
01207
01208 st->codec->bits_per_coded_sample = color_depth;
01209 color_count = 1 << color_depth;
01210 color_index = 255;
01211 color_dec = 256 / (color_count - 1);
01212 for (j = 0; j < color_count; j++) {
01213 r = g = b = color_index;
01214 sc->palette[j] =
01215 (r << 16) | (g << 8) | (b);
01216 color_index -= color_dec;
01217 if (color_index < 0)
01218 color_index = 0;
01219 }
01220 } else if (st->codec->color_table_id) {
01221 const uint8_t *color_table;
01222
01223 color_count = 1 << color_depth;
01224 if (color_depth == 2)
01225 color_table = ff_qt_default_palette_4;
01226 else if (color_depth == 4)
01227 color_table = ff_qt_default_palette_16;
01228 else
01229 color_table = ff_qt_default_palette_256;
01230
01231 for (j = 0; j < color_count; j++) {
01232 r = color_table[j * 3 + 0];
01233 g = color_table[j * 3 + 1];
01234 b = color_table[j * 3 + 2];
01235 sc->palette[j] =
01236 (r << 16) | (g << 8) | (b);
01237 }
01238 } else {
01239
01240 color_start = avio_rb32(pb);
01241 color_count = avio_rb16(pb);
01242 color_end = avio_rb16(pb);
01243 if ((color_start <= 255) &&
01244 (color_end <= 255)) {
01245 for (j = color_start; j <= color_end; j++) {
01246
01247
01248
01249 avio_r8(pb);
01250 avio_r8(pb);
01251 r = avio_r8(pb);
01252 avio_r8(pb);
01253 g = avio_r8(pb);
01254 avio_r8(pb);
01255 b = avio_r8(pb);
01256 avio_r8(pb);
01257 sc->palette[j] =
01258 (r << 16) | (g << 8) | (b);
01259 }
01260 }
01261 }
01262 sc->has_palette = 1;
01263 }
01264 } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
01265 int bits_per_sample, flags;
01266 uint16_t version = avio_rb16(pb);
01267
01268 st->codec->codec_id = id;
01269 avio_rb16(pb);
01270 avio_rb32(pb);
01271
01272 st->codec->channels = avio_rb16(pb);
01273 av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
01274 st->codec->bits_per_coded_sample = avio_rb16(pb);
01275
01276 sc->audio_cid = avio_rb16(pb);
01277 avio_rb16(pb);
01278
01279 st->codec->sample_rate = ((avio_rb32(pb) >> 16));
01280
01281
01282 av_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
01283 if (!c->isom) {
01284 if (version==1) {
01285 sc->samples_per_frame = avio_rb32(pb);
01286 avio_rb32(pb);
01287 sc->bytes_per_frame = avio_rb32(pb);
01288 avio_rb32(pb);
01289 } else if (version==2) {
01290 avio_rb32(pb);
01291 st->codec->sample_rate = av_int2double(avio_rb64(pb));
01292 st->codec->channels = avio_rb32(pb);
01293 avio_rb32(pb);
01294 st->codec->bits_per_coded_sample = avio_rb32(pb);
01295 flags = avio_rb32(pb);
01296 sc->bytes_per_frame = avio_rb32(pb);
01297 sc->samples_per_frame = avio_rb32(pb);
01298 if (format == MKTAG('l','p','c','m'))
01299 st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
01300 }
01301 }
01302
01303 switch (st->codec->codec_id) {
01304 case CODEC_ID_PCM_S8:
01305 case CODEC_ID_PCM_U8:
01306 if (st->codec->bits_per_coded_sample == 16)
01307 st->codec->codec_id = CODEC_ID_PCM_S16BE;
01308 break;
01309 case CODEC_ID_PCM_S16LE:
01310 case CODEC_ID_PCM_S16BE:
01311 if (st->codec->bits_per_coded_sample == 8)
01312 st->codec->codec_id = CODEC_ID_PCM_S8;
01313 else if (st->codec->bits_per_coded_sample == 24)
01314 st->codec->codec_id =
01315 st->codec->codec_id == CODEC_ID_PCM_S16BE ?
01316 CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
01317 break;
01318
01319 case CODEC_ID_MACE3:
01320 sc->samples_per_frame = 6;
01321 sc->bytes_per_frame = 2*st->codec->channels;
01322 break;
01323 case CODEC_ID_MACE6:
01324 sc->samples_per_frame = 6;
01325 sc->bytes_per_frame = 1*st->codec->channels;
01326 break;
01327 case CODEC_ID_ADPCM_IMA_QT:
01328 sc->samples_per_frame = 64;
01329 sc->bytes_per_frame = 34*st->codec->channels;
01330 break;
01331 case CODEC_ID_GSM:
01332 sc->samples_per_frame = 160;
01333 sc->bytes_per_frame = 33;
01334 break;
01335 default:
01336 break;
01337 }
01338
01339 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
01340 if (bits_per_sample) {
01341 st->codec->bits_per_coded_sample = bits_per_sample;
01342 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
01343 }
01344 } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
01345
01346
01347 MOVAtom fake_atom = { .size = size - (avio_tell(pb) - start_pos) };
01348 if (format != AV_RL32("mp4s"))
01349 mov_read_glbl(c, pb, fake_atom);
01350 st->codec->codec_id= id;
01351 st->codec->width = sc->width;
01352 st->codec->height = sc->height;
01353 } else {
01354
01355 avio_skip(pb, size - (avio_tell(pb) - start_pos));
01356 }
01357
01358 a.size = size - (avio_tell(pb) - start_pos);
01359 if (a.size > 8) {
01360 int ret;
01361 if ((ret = mov_read_default(c, pb, a)) < 0)
01362 return ret;
01363 } else if (a.size > 0)
01364 avio_skip(pb, a.size);
01365 }
01366
01367 if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
01368 st->codec->sample_rate= sc->time_scale;
01369
01370
01371 switch (st->codec->codec_id) {
01372 #if CONFIG_DV_DEMUXER
01373 case CODEC_ID_DVAUDIO:
01374 c->dv_fctx = avformat_alloc_context();
01375 c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
01376 if (!c->dv_demux) {
01377 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
01378 return AVERROR(ENOMEM);
01379 }
01380 sc->dv_audio_container = 1;
01381 st->codec->codec_id = CODEC_ID_PCM_S16LE;
01382 break;
01383 #endif
01384
01385 case CODEC_ID_QCELP:
01386
01387 if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
01388 st->codec->sample_rate = 8000;
01389 st->codec->frame_size= 160;
01390 st->codec->channels= 1;
01391 break;
01392 case CODEC_ID_AMR_NB:
01393 st->codec->channels= 1;
01394
01395 st->codec->sample_rate = 8000;
01396
01397 st->codec->frame_size = 160;
01398 break;
01399 case CODEC_ID_AMR_WB:
01400 st->codec->channels = 1;
01401 st->codec->sample_rate = 16000;
01402 st->codec->frame_size = 320;
01403 break;
01404 case CODEC_ID_MP2:
01405 case CODEC_ID_MP3:
01406 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01407 st->need_parsing = AVSTREAM_PARSE_FULL;
01408 break;
01409 case CODEC_ID_GSM:
01410 case CODEC_ID_ADPCM_MS:
01411 case CODEC_ID_ADPCM_IMA_WAV:
01412 st->codec->frame_size = sc->samples_per_frame;
01413 st->codec->block_align = sc->bytes_per_frame;
01414 break;
01415 case CODEC_ID_ALAC:
01416 if (st->codec->extradata_size == 36) {
01417 st->codec->frame_size = AV_RB32(st->codec->extradata+12);
01418 st->codec->channels = AV_RB8 (st->codec->extradata+21);
01419 st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
01420 }
01421 break;
01422 default:
01423 break;
01424 }
01425
01426 return 0;
01427 }
01428
01429 static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01430 {
01431 int entries;
01432
01433 avio_r8(pb);
01434 avio_rb24(pb);
01435 entries = avio_rb32(pb);
01436
01437 return ff_mov_read_stsd_entries(c, pb, entries);
01438 }
01439
01440 static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01441 {
01442 AVStream *st;
01443 MOVStreamContext *sc;
01444 unsigned int i, entries;
01445
01446 if (c->fc->nb_streams < 1)
01447 return 0;
01448 st = c->fc->streams[c->fc->nb_streams-1];
01449 sc = st->priv_data;
01450
01451 avio_r8(pb);
01452 avio_rb24(pb);
01453
01454 entries = avio_rb32(pb);
01455
01456 av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
01457
01458 if (!entries)
01459 return 0;
01460 if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
01461 return AVERROR_INVALIDDATA;
01462 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
01463 if (!sc->stsc_data)
01464 return AVERROR(ENOMEM);
01465 sc->stsc_count = entries;
01466
01467 for (i=0; i<entries; i++) {
01468 sc->stsc_data[i].first = avio_rb32(pb);
01469 sc->stsc_data[i].count = avio_rb32(pb);
01470 sc->stsc_data[i].id = avio_rb32(pb);
01471 }
01472 return 0;
01473 }
01474
01475 static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01476 {
01477 AVStream *st;
01478 MOVStreamContext *sc;
01479 unsigned i, entries;
01480
01481 if (c->fc->nb_streams < 1)
01482 return 0;
01483 st = c->fc->streams[c->fc->nb_streams-1];
01484 sc = st->priv_data;
01485
01486 avio_rb32(pb);
01487
01488 entries = avio_rb32(pb);
01489 if (entries >= UINT_MAX / sizeof(*sc->stps_data))
01490 return AVERROR_INVALIDDATA;
01491 sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
01492 if (!sc->stps_data)
01493 return AVERROR(ENOMEM);
01494 sc->stps_count = entries;
01495
01496 for (i = 0; i < entries; i++) {
01497 sc->stps_data[i] = avio_rb32(pb);
01498
01499 }
01500
01501 return 0;
01502 }
01503
01504 static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01505 {
01506 AVStream *st;
01507 MOVStreamContext *sc;
01508 unsigned int i, entries;
01509
01510 if (c->fc->nb_streams < 1)
01511 return 0;
01512 st = c->fc->streams[c->fc->nb_streams-1];
01513 sc = st->priv_data;
01514
01515 avio_r8(pb);
01516 avio_rb24(pb);
01517
01518 entries = avio_rb32(pb);
01519
01520 av_dlog(c->fc, "keyframe_count = %d\n", entries);
01521
01522 if (!entries)
01523 return 0;
01524 if (entries >= UINT_MAX / sizeof(int))
01525 return AVERROR_INVALIDDATA;
01526 sc->keyframes = av_malloc(entries * sizeof(int));
01527 if (!sc->keyframes)
01528 return AVERROR(ENOMEM);
01529 sc->keyframe_count = entries;
01530
01531 for (i=0; i<entries; i++) {
01532 sc->keyframes[i] = avio_rb32(pb);
01533
01534 }
01535 return 0;
01536 }
01537
01538 static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01539 {
01540 AVStream *st;
01541 MOVStreamContext *sc;
01542 unsigned int i, entries, sample_size, field_size, num_bytes;
01543 GetBitContext gb;
01544 unsigned char* buf;
01545
01546 if (c->fc->nb_streams < 1)
01547 return 0;
01548 st = c->fc->streams[c->fc->nb_streams-1];
01549 sc = st->priv_data;
01550
01551 avio_r8(pb);
01552 avio_rb24(pb);
01553
01554 if (atom.type == MKTAG('s','t','s','z')) {
01555 sample_size = avio_rb32(pb);
01556 if (!sc->sample_size)
01557 sc->sample_size = sample_size;
01558 field_size = 32;
01559 } else {
01560 sample_size = 0;
01561 avio_rb24(pb);
01562 field_size = avio_r8(pb);
01563 }
01564 entries = avio_rb32(pb);
01565
01566 av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
01567
01568 sc->sample_count = entries;
01569 if (sample_size)
01570 return 0;
01571
01572 if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
01573 av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
01574 return AVERROR_INVALIDDATA;
01575 }
01576
01577 if (!entries)
01578 return 0;
01579 if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
01580 return AVERROR_INVALIDDATA;
01581 sc->sample_sizes = av_malloc(entries * sizeof(int));
01582 if (!sc->sample_sizes)
01583 return AVERROR(ENOMEM);
01584
01585 num_bytes = (entries*field_size+4)>>3;
01586
01587 buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
01588 if (!buf) {
01589 av_freep(&sc->sample_sizes);
01590 return AVERROR(ENOMEM);
01591 }
01592
01593 if (avio_read(pb, buf, num_bytes) < num_bytes) {
01594 av_freep(&sc->sample_sizes);
01595 av_free(buf);
01596 return AVERROR_INVALIDDATA;
01597 }
01598
01599 init_get_bits(&gb, buf, 8*num_bytes);
01600
01601 for (i=0; i<entries; i++)
01602 sc->sample_sizes[i] = get_bits_long(&gb, field_size);
01603
01604 av_free(buf);
01605 return 0;
01606 }
01607
01608 static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01609 {
01610 AVStream *st;
01611 MOVStreamContext *sc;
01612 unsigned int i, entries;
01613 int64_t duration=0;
01614 int64_t total_sample_count=0;
01615
01616 if (c->fc->nb_streams < 1)
01617 return 0;
01618 st = c->fc->streams[c->fc->nb_streams-1];
01619 sc = st->priv_data;
01620
01621 avio_r8(pb);
01622 avio_rb24(pb);
01623 entries = avio_rb32(pb);
01624
01625 av_dlog(c->fc, "track[%i].stts.entries = %i\n",
01626 c->fc->nb_streams-1, entries);
01627
01628 if (!entries)
01629 return 0;
01630 if (entries >= UINT_MAX / sizeof(*sc->stts_data))
01631 return AVERROR(EINVAL);
01632
01633 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
01634 if (!sc->stts_data)
01635 return AVERROR(ENOMEM);
01636
01637 sc->stts_count = entries;
01638
01639 for (i=0; i<entries; i++) {
01640 int sample_duration;
01641 int sample_count;
01642
01643 sample_count=avio_rb32(pb);
01644 sample_duration = avio_rb32(pb);
01645 sc->stts_data[i].count= sample_count;
01646 sc->stts_data[i].duration= sample_duration;
01647
01648 av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
01649 sample_count, sample_duration);
01650
01651 duration+=(int64_t)sample_duration*sample_count;
01652 total_sample_count+=sample_count;
01653 }
01654
01655 st->nb_frames= total_sample_count;
01656 if (duration)
01657 st->duration= duration;
01658 return 0;
01659 }
01660
01661 static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01662 {
01663 AVStream *st;
01664 MOVStreamContext *sc;
01665 unsigned int i, entries;
01666
01667 if (c->fc->nb_streams < 1)
01668 return 0;
01669 st = c->fc->streams[c->fc->nb_streams-1];
01670 sc = st->priv_data;
01671
01672 avio_r8(pb);
01673 avio_rb24(pb);
01674 entries = avio_rb32(pb);
01675
01676 av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
01677
01678 if (!entries)
01679 return 0;
01680 if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
01681 return AVERROR_INVALIDDATA;
01682 sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
01683 if (!sc->ctts_data)
01684 return AVERROR(ENOMEM);
01685 sc->ctts_count = entries;
01686
01687 for (i=0; i<entries; i++) {
01688 int count =avio_rb32(pb);
01689 int duration =avio_rb32(pb);
01690
01691 sc->ctts_data[i].count = count;
01692 sc->ctts_data[i].duration= duration;
01693 if (duration < 0)
01694 sc->dts_shift = FFMAX(sc->dts_shift, -duration);
01695 }
01696
01697 av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
01698
01699 return 0;
01700 }
01701
01702 static void mov_build_index(MOVContext *mov, AVStream *st)
01703 {
01704 MOVStreamContext *sc = st->priv_data;
01705 int64_t current_offset;
01706 int64_t current_dts = 0;
01707 unsigned int stts_index = 0;
01708 unsigned int stsc_index = 0;
01709 unsigned int stss_index = 0;
01710 unsigned int stps_index = 0;
01711 unsigned int i, j;
01712 uint64_t stream_size = 0;
01713 AVIndexEntry *mem;
01714
01715
01716 if (sc->time_offset && mov->time_scale > 0) {
01717 if (sc->time_offset < 0)
01718 sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale);
01719 current_dts = -sc->time_offset;
01720 if (sc->ctts_data && sc->stts_data && sc->stts_data[0].duration &&
01721 sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
01722
01723
01724 sc->wrong_dts = 1;
01725 st->codec->has_b_frames = 1;
01726 }
01727 }
01728
01729
01730 if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
01731 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
01732 unsigned int current_sample = 0;
01733 unsigned int stts_sample = 0;
01734 unsigned int sample_size;
01735 unsigned int distance = 0;
01736 int key_off = sc->keyframes && sc->keyframes[0] == 1;
01737
01738 current_dts -= sc->dts_shift;
01739
01740 if (!sc->sample_count)
01741 return;
01742 if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
01743 return;
01744 mem = av_realloc(st->index_entries, (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries));
01745 if (!mem)
01746 return;
01747 st->index_entries = mem;
01748 st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
01749
01750 for (i = 0; i < sc->chunk_count; i++) {
01751 current_offset = sc->chunk_offsets[i];
01752 while (stsc_index + 1 < sc->stsc_count &&
01753 i + 1 == sc->stsc_data[stsc_index + 1].first)
01754 stsc_index++;
01755 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
01756 int keyframe = 0;
01757 if (current_sample >= sc->sample_count) {
01758 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
01759 return;
01760 }
01761
01762 if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) {
01763 keyframe = 1;
01764 if (stss_index + 1 < sc->keyframe_count)
01765 stss_index++;
01766 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
01767 keyframe = 1;
01768 if (stps_index + 1 < sc->stps_count)
01769 stps_index++;
01770 }
01771 if (keyframe)
01772 distance = 0;
01773 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
01774 if (sc->pseudo_stream_id == -1 ||
01775 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
01776 AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
01777 e->pos = current_offset;
01778 e->timestamp = current_dts;
01779 e->size = sample_size;
01780 e->min_distance = distance;
01781 e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
01782 av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
01783 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
01784 current_offset, current_dts, sample_size, distance, keyframe);
01785 }
01786
01787 current_offset += sample_size;
01788 stream_size += sample_size;
01789 current_dts += sc->stts_data[stts_index].duration;
01790 distance++;
01791 stts_sample++;
01792 current_sample++;
01793 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
01794 stts_sample = 0;
01795 stts_index++;
01796 }
01797 }
01798 }
01799 if (st->duration > 0)
01800 st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
01801 } else {
01802 unsigned chunk_samples, total = 0;
01803
01804
01805 for (i = 0; i < sc->stsc_count; i++) {
01806 unsigned count, chunk_count;
01807
01808 chunk_samples = sc->stsc_data[i].count;
01809 if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
01810 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
01811 return;
01812 }
01813
01814 if (sc->samples_per_frame >= 160) {
01815 count = chunk_samples / sc->samples_per_frame;
01816 } else if (sc->samples_per_frame > 1) {
01817 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
01818 count = (chunk_samples+samples-1) / samples;
01819 } else {
01820 count = (chunk_samples+1023) / 1024;
01821 }
01822
01823 if (i < sc->stsc_count - 1)
01824 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
01825 else
01826 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
01827 total += chunk_count * count;
01828 }
01829
01830 av_dlog(mov->fc, "chunk count %d\n", total);
01831 if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
01832 return;
01833 mem = av_realloc(st->index_entries, (st->nb_index_entries + total) * sizeof(*st->index_entries));
01834 if (!mem)
01835 return;
01836 st->index_entries = mem;
01837 st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
01838
01839
01840 for (i = 0; i < sc->chunk_count; i++) {
01841 current_offset = sc->chunk_offsets[i];
01842 if (stsc_index + 1 < sc->stsc_count &&
01843 i + 1 == sc->stsc_data[stsc_index + 1].first)
01844 stsc_index++;
01845 chunk_samples = sc->stsc_data[stsc_index].count;
01846
01847 while (chunk_samples > 0) {
01848 AVIndexEntry *e;
01849 unsigned size, samples;
01850
01851 if (sc->samples_per_frame >= 160) {
01852 samples = sc->samples_per_frame;
01853 size = sc->bytes_per_frame;
01854 } else {
01855 if (sc->samples_per_frame > 1) {
01856 samples = FFMIN((1024 / sc->samples_per_frame)*
01857 sc->samples_per_frame, chunk_samples);
01858 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
01859 } else {
01860 samples = FFMIN(1024, chunk_samples);
01861 size = samples * sc->sample_size;
01862 }
01863 }
01864
01865 if (st->nb_index_entries >= total) {
01866 av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
01867 return;
01868 }
01869 e = &st->index_entries[st->nb_index_entries++];
01870 e->pos = current_offset;
01871 e->timestamp = current_dts;
01872 e->size = size;
01873 e->min_distance = 0;
01874 e->flags = AVINDEX_KEYFRAME;
01875 av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
01876 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
01877 size, samples);
01878
01879 current_offset += size;
01880 current_dts += samples;
01881 chunk_samples -= samples;
01882 }
01883 }
01884 }
01885 }
01886
01887 static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref,
01888 AVIOInterruptCB *int_cb)
01889 {
01890
01891
01892 if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
01893 char filename[1024];
01894 char *src_path;
01895 int i, l;
01896
01897
01898 src_path = strrchr(src, '/');
01899 if (src_path)
01900 src_path++;
01901 else
01902 src_path = src;
01903
01904
01905 for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
01906 if (ref->path[l] == '/') {
01907 if (i == ref->nlvl_to - 1)
01908 break;
01909 else
01910 i++;
01911 }
01912
01913
01914 if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
01915 memcpy(filename, src, src_path - src);
01916 filename[src_path - src] = 0;
01917
01918 for (i = 1; i < ref->nlvl_from; i++)
01919 av_strlcat(filename, "../", 1024);
01920
01921 av_strlcat(filename, ref->path + l + 1, 1024);
01922
01923 if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
01924 return 0;
01925 }
01926 }
01927
01928 return AVERROR(ENOENT);
01929 }
01930
01931 static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
01932 {
01933 AVStream *st;
01934 MOVStreamContext *sc;
01935 int ret;
01936
01937 st = avformat_new_stream(c->fc, NULL);
01938 if (!st) return AVERROR(ENOMEM);
01939 st->id = c->fc->nb_streams;
01940 sc = av_mallocz(sizeof(MOVStreamContext));
01941 if (!sc) return AVERROR(ENOMEM);
01942
01943 st->priv_data = sc;
01944 st->codec->codec_type = AVMEDIA_TYPE_DATA;
01945 sc->ffindex = st->index;
01946
01947 if ((ret = mov_read_default(c, pb, atom)) < 0)
01948 return ret;
01949
01950
01951 if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
01952 (!sc->sample_size && !sc->sample_count))) {
01953 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
01954 st->index);
01955 return 0;
01956 }
01957
01958 if (sc->time_scale <= 0) {
01959 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
01960 sc->time_scale = c->time_scale;
01961 if (sc->time_scale <= 0)
01962 sc->time_scale = 1;
01963 }
01964
01965 avpriv_set_pts_info(st, 64, 1, sc->time_scale);
01966
01967 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
01968 !st->codec->frame_size && sc->stts_count == 1) {
01969 st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
01970 st->codec->sample_rate, sc->time_scale);
01971 av_dlog(c->fc, "frame size %d\n", st->codec->frame_size);
01972 }
01973
01974 mov_build_index(c, st);
01975
01976 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
01977 MOVDref *dref = &sc->drefs[sc->dref_id - 1];
01978 if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback) < 0)
01979 av_log(c->fc, AV_LOG_ERROR,
01980 "stream %d, error opening alias: path='%s', dir='%s', "
01981 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
01982 st->index, dref->path, dref->dir, dref->filename,
01983 dref->volume, dref->nlvl_from, dref->nlvl_to);
01984 } else
01985 sc->pb = c->fc->pb;
01986
01987 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01988 if (!st->sample_aspect_ratio.num &&
01989 (st->codec->width != sc->width || st->codec->height != sc->height)) {
01990 st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
01991 ((double)st->codec->width * sc->height), INT_MAX);
01992 }
01993
01994 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
01995 sc->time_scale*st->nb_frames, st->duration, INT_MAX);
01996
01997 if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
01998 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
01999 sc->time_scale, sc->stts_data[0].duration, INT_MAX);
02000 }
02001
02002 switch (st->codec->codec_id) {
02003 #if CONFIG_H261_DECODER
02004 case CODEC_ID_H261:
02005 #endif
02006 #if CONFIG_H263_DECODER
02007 case CODEC_ID_H263:
02008 #endif
02009 #if CONFIG_MPEG4_DECODER
02010 case CODEC_ID_MPEG4:
02011 #endif
02012 st->codec->width = 0;
02013 st->codec->height= 0;
02014 break;
02015 }
02016
02017
02018 av_freep(&sc->chunk_offsets);
02019 av_freep(&sc->stsc_data);
02020 av_freep(&sc->sample_sizes);
02021 av_freep(&sc->keyframes);
02022 av_freep(&sc->stts_data);
02023 av_freep(&sc->stps_data);
02024
02025 return 0;
02026 }
02027
02028 static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02029 {
02030 int ret;
02031 c->itunes_metadata = 1;
02032 ret = mov_read_default(c, pb, atom);
02033 c->itunes_metadata = 0;
02034 return ret;
02035 }
02036
02037 static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02038 {
02039 while (atom.size > 8) {
02040 uint32_t tag = avio_rl32(pb);
02041 atom.size -= 4;
02042 if (tag == MKTAG('h','d','l','r')) {
02043 avio_seek(pb, -8, SEEK_CUR);
02044 atom.size += 8;
02045 return mov_read_default(c, pb, atom);
02046 }
02047 }
02048 return 0;
02049 }
02050
02051 static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02052 {
02053 int i;
02054 int width;
02055 int height;
02056 int64_t disp_transform[2];
02057 int display_matrix[3][2];
02058 AVStream *st;
02059 MOVStreamContext *sc;
02060 int version;
02061
02062 if (c->fc->nb_streams < 1)
02063 return 0;
02064 st = c->fc->streams[c->fc->nb_streams-1];
02065 sc = st->priv_data;
02066
02067 version = avio_r8(pb);
02068 avio_rb24(pb);
02069
02070
02071
02072
02073
02074
02075
02076 if (version == 1) {
02077 avio_rb64(pb);
02078 avio_rb64(pb);
02079 } else {
02080 avio_rb32(pb);
02081 avio_rb32(pb);
02082 }
02083 st->id = (int)avio_rb32(pb);
02084 avio_rb32(pb);
02085
02086
02087 (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
02088 avio_rb32(pb);
02089 avio_rb32(pb);
02090
02091 avio_rb16(pb);
02092 avio_rb16(pb);
02093 avio_rb16(pb);
02094 avio_rb16(pb);
02095
02096
02097
02098
02099 for (i = 0; i < 3; i++) {
02100 display_matrix[i][0] = avio_rb32(pb);
02101 display_matrix[i][1] = avio_rb32(pb);
02102 avio_rb32(pb);
02103 }
02104
02105 width = avio_rb32(pb);
02106 height = avio_rb32(pb);
02107 sc->width = width >> 16;
02108 sc->height = height >> 16;
02109
02110
02111
02112
02113
02114 if (width && height &&
02115 ((display_matrix[0][0] != 65536 ||
02116 display_matrix[1][1] != 65536) &&
02117 !display_matrix[0][1] &&
02118 !display_matrix[1][0] &&
02119 !display_matrix[2][0] && !display_matrix[2][1])) {
02120 for (i = 0; i < 2; i++)
02121 disp_transform[i] =
02122 (int64_t) width * display_matrix[0][i] +
02123 (int64_t) height * display_matrix[1][i] +
02124 ((int64_t) display_matrix[2][i] << 16);
02125
02126
02127 st->sample_aspect_ratio = av_d2q(
02128 ((double) disp_transform[0] * height) /
02129 ((double) disp_transform[1] * width), INT_MAX);
02130 }
02131 return 0;
02132 }
02133
02134 static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02135 {
02136 MOVFragment *frag = &c->fragment;
02137 MOVTrackExt *trex = NULL;
02138 int flags, track_id, i;
02139
02140 avio_r8(pb);
02141 flags = avio_rb24(pb);
02142
02143 track_id = avio_rb32(pb);
02144 if (!track_id)
02145 return AVERROR_INVALIDDATA;
02146 frag->track_id = track_id;
02147 for (i = 0; i < c->trex_count; i++)
02148 if (c->trex_data[i].track_id == frag->track_id) {
02149 trex = &c->trex_data[i];
02150 break;
02151 }
02152 if (!trex) {
02153 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
02154 return AVERROR_INVALIDDATA;
02155 }
02156
02157 if (flags & 0x01) frag->base_data_offset = avio_rb64(pb);
02158 else frag->base_data_offset = frag->moof_offset;
02159 if (flags & 0x02) frag->stsd_id = avio_rb32(pb);
02160 else frag->stsd_id = trex->stsd_id;
02161
02162 frag->duration = flags & 0x08 ? avio_rb32(pb) : trex->duration;
02163 frag->size = flags & 0x10 ? avio_rb32(pb) : trex->size;
02164 frag->flags = flags & 0x20 ? avio_rb32(pb) : trex->flags;
02165 av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
02166 return 0;
02167 }
02168
02169 static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02170 {
02171 c->chapter_track = avio_rb32(pb);
02172 return 0;
02173 }
02174
02175 static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02176 {
02177 MOVTrackExt *trex;
02178
02179 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
02180 return AVERROR_INVALIDDATA;
02181 trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
02182 if (!trex)
02183 return AVERROR(ENOMEM);
02184 c->trex_data = trex;
02185 trex = &c->trex_data[c->trex_count++];
02186 avio_r8(pb);
02187 avio_rb24(pb);
02188 trex->track_id = avio_rb32(pb);
02189 trex->stsd_id = avio_rb32(pb);
02190 trex->duration = avio_rb32(pb);
02191 trex->size = avio_rb32(pb);
02192 trex->flags = avio_rb32(pb);
02193 return 0;
02194 }
02195
02196 static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02197 {
02198 MOVFragment *frag = &c->fragment;
02199 AVStream *st = NULL;
02200 MOVStreamContext *sc;
02201 MOVStts *ctts_data;
02202 uint64_t offset;
02203 int64_t dts;
02204 int data_offset = 0;
02205 unsigned entries, first_sample_flags = frag->flags;
02206 int flags, distance, i;
02207
02208 for (i = 0; i < c->fc->nb_streams; i++) {
02209 if (c->fc->streams[i]->id == frag->track_id) {
02210 st = c->fc->streams[i];
02211 break;
02212 }
02213 }
02214 if (!st) {
02215 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
02216 return AVERROR_INVALIDDATA;
02217 }
02218 sc = st->priv_data;
02219 if (sc->pseudo_stream_id+1 != frag->stsd_id)
02220 return 0;
02221 avio_r8(pb);
02222 flags = avio_rb24(pb);
02223 entries = avio_rb32(pb);
02224 av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
02225
02226
02227
02228
02229
02230
02231 if (!sc->ctts_count && sc->sample_count)
02232 {
02233
02234 ctts_data = av_malloc(sizeof(*sc->ctts_data));
02235 if (!ctts_data)
02236 return AVERROR(ENOMEM);
02237 sc->ctts_data = ctts_data;
02238 sc->ctts_data[sc->ctts_count].count = sc->sample_count;
02239 sc->ctts_data[sc->ctts_count].duration = 0;
02240 sc->ctts_count++;
02241 }
02242 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
02243 return AVERROR_INVALIDDATA;
02244 ctts_data = av_realloc(sc->ctts_data,
02245 (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
02246 if (!ctts_data)
02247 return AVERROR(ENOMEM);
02248 sc->ctts_data = ctts_data;
02249
02250 if (flags & 0x001) data_offset = avio_rb32(pb);
02251 if (flags & 0x004) first_sample_flags = avio_rb32(pb);
02252 dts = st->duration - sc->time_offset;
02253 offset = frag->base_data_offset + data_offset;
02254 distance = 0;
02255 av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
02256 for (i = 0; i < entries; i++) {
02257 unsigned sample_size = frag->size;
02258 int sample_flags = i ? frag->flags : first_sample_flags;
02259 unsigned sample_duration = frag->duration;
02260 int keyframe;
02261
02262 if (flags & 0x100) sample_duration = avio_rb32(pb);
02263 if (flags & 0x200) sample_size = avio_rb32(pb);
02264 if (flags & 0x400) sample_flags = avio_rb32(pb);
02265 sc->ctts_data[sc->ctts_count].count = 1;
02266 sc->ctts_data[sc->ctts_count].duration = (flags & 0x800) ? avio_rb32(pb) : 0;
02267 sc->ctts_count++;
02268 if ((keyframe = st->codec->codec_type == AVMEDIA_TYPE_AUDIO ||
02269 (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
02270 distance = 0;
02271 av_add_index_entry(st, offset, dts, sample_size, distance,
02272 keyframe ? AVINDEX_KEYFRAME : 0);
02273 av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
02274 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
02275 offset, dts, sample_size, distance, keyframe);
02276 distance++;
02277 dts += sample_duration;
02278 offset += sample_size;
02279 }
02280 frag->moof_offset = offset;
02281 st->duration = dts + sc->time_offset;
02282 return 0;
02283 }
02284
02285
02286
02287
02288 static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02289 {
02290 int err;
02291
02292 if (atom.size < 8)
02293 return 0;
02294 if (avio_rb32(pb) != 0) {
02295 avio_skip(pb, atom.size - 4);
02296 return 0;
02297 }
02298 atom.type = avio_rl32(pb);
02299 atom.size -= 8;
02300 if (atom.type != MKTAG('m','d','a','t')) {
02301 avio_skip(pb, atom.size);
02302 return 0;
02303 }
02304 err = mov_read_mdat(c, pb, atom);
02305 return err;
02306 }
02307
02308 static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02309 {
02310 #if CONFIG_ZLIB
02311 AVIOContext ctx;
02312 uint8_t *cmov_data;
02313 uint8_t *moov_data;
02314 long cmov_len, moov_len;
02315 int ret = -1;
02316
02317 avio_rb32(pb);
02318 if (avio_rl32(pb) != MKTAG('d','c','o','m'))
02319 return AVERROR_INVALIDDATA;
02320 if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
02321 av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
02322 return AVERROR_INVALIDDATA;
02323 }
02324 avio_rb32(pb);
02325 if (avio_rl32(pb) != MKTAG('c','m','v','d'))
02326 return AVERROR_INVALIDDATA;
02327 moov_len = avio_rb32(pb);
02328 cmov_len = atom.size - 6 * 4;
02329
02330 cmov_data = av_malloc(cmov_len);
02331 if (!cmov_data)
02332 return AVERROR(ENOMEM);
02333 moov_data = av_malloc(moov_len);
02334 if (!moov_data) {
02335 av_free(cmov_data);
02336 return AVERROR(ENOMEM);
02337 }
02338 avio_read(pb, cmov_data, cmov_len);
02339 if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
02340 goto free_and_return;
02341 if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
02342 goto free_and_return;
02343 atom.type = MKTAG('m','o','o','v');
02344 atom.size = moov_len;
02345 ret = mov_read_default(c, &ctx, atom);
02346 free_and_return:
02347 av_free(moov_data);
02348 av_free(cmov_data);
02349 return ret;
02350 #else
02351 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
02352 return AVERROR(ENOSYS);
02353 #endif
02354 }
02355
02356
02357 static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
02358 {
02359 MOVStreamContext *sc;
02360 int i, edit_count, version;
02361
02362 if (c->fc->nb_streams < 1)
02363 return 0;
02364 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
02365
02366 version = avio_r8(pb);
02367 avio_rb24(pb);
02368 edit_count = avio_rb32(pb);
02369
02370 if ((uint64_t)edit_count*12+8 > atom.size)
02371 return AVERROR_INVALIDDATA;
02372
02373 for (i=0; i<edit_count; i++){
02374 int64_t time;
02375 int64_t duration;
02376 if (version == 1) {
02377 duration = avio_rb64(pb);
02378 time = avio_rb64(pb);
02379 } else {
02380 duration = avio_rb32(pb);
02381 time = (int32_t)avio_rb32(pb);
02382 }
02383 avio_rb32(pb);
02384 if (i == 0 && time >= -1) {
02385 sc->time_offset = time != -1 ? time : -duration;
02386 }
02387 }
02388
02389 if (edit_count > 1)
02390 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
02391 "a/v desync might occur, patch welcome\n");
02392
02393 av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
02394 return 0;
02395 }
02396
02397 static const MOVParseTableEntry mov_default_parse_table[] = {
02398 { MKTAG('a','v','s','s'), mov_read_extradata },
02399 { MKTAG('c','h','p','l'), mov_read_chpl },
02400 { MKTAG('c','o','6','4'), mov_read_stco },
02401 { MKTAG('c','t','t','s'), mov_read_ctts },
02402 { MKTAG('d','i','n','f'), mov_read_default },
02403 { MKTAG('d','r','e','f'), mov_read_dref },
02404 { MKTAG('e','d','t','s'), mov_read_default },
02405 { MKTAG('e','l','s','t'), mov_read_elst },
02406 { MKTAG('e','n','d','a'), mov_read_enda },
02407 { MKTAG('f','i','e','l'), mov_read_fiel },
02408 { MKTAG('f','t','y','p'), mov_read_ftyp },
02409 { MKTAG('g','l','b','l'), mov_read_glbl },
02410 { MKTAG('h','d','l','r'), mov_read_hdlr },
02411 { MKTAG('i','l','s','t'), mov_read_ilst },
02412 { MKTAG('j','p','2','h'), mov_read_extradata },
02413 { MKTAG('m','d','a','t'), mov_read_mdat },
02414 { MKTAG('m','d','h','d'), mov_read_mdhd },
02415 { MKTAG('m','d','i','a'), mov_read_default },
02416 { MKTAG('m','e','t','a'), mov_read_meta },
02417 { MKTAG('m','i','n','f'), mov_read_default },
02418 { MKTAG('m','o','o','f'), mov_read_moof },
02419 { MKTAG('m','o','o','v'), mov_read_moov },
02420 { MKTAG('m','v','e','x'), mov_read_default },
02421 { MKTAG('m','v','h','d'), mov_read_mvhd },
02422 { MKTAG('S','M','I',' '), mov_read_smi },
02423 { MKTAG('a','l','a','c'), mov_read_extradata },
02424 { MKTAG('a','v','c','C'), mov_read_glbl },
02425 { MKTAG('p','a','s','p'), mov_read_pasp },
02426 { MKTAG('s','t','b','l'), mov_read_default },
02427 { MKTAG('s','t','c','o'), mov_read_stco },
02428 { MKTAG('s','t','p','s'), mov_read_stps },
02429 { MKTAG('s','t','r','f'), mov_read_strf },
02430 { MKTAG('s','t','s','c'), mov_read_stsc },
02431 { MKTAG('s','t','s','d'), mov_read_stsd },
02432 { MKTAG('s','t','s','s'), mov_read_stss },
02433 { MKTAG('s','t','s','z'), mov_read_stsz },
02434 { MKTAG('s','t','t','s'), mov_read_stts },
02435 { MKTAG('s','t','z','2'), mov_read_stsz },
02436 { MKTAG('t','k','h','d'), mov_read_tkhd },
02437 { MKTAG('t','f','h','d'), mov_read_tfhd },
02438 { MKTAG('t','r','a','k'), mov_read_trak },
02439 { MKTAG('t','r','a','f'), mov_read_default },
02440 { MKTAG('t','r','e','f'), mov_read_default },
02441 { MKTAG('c','h','a','p'), mov_read_chap },
02442 { MKTAG('t','r','e','x'), mov_read_trex },
02443 { MKTAG('t','r','u','n'), mov_read_trun },
02444 { MKTAG('u','d','t','a'), mov_read_default },
02445 { MKTAG('w','a','v','e'), mov_read_wave },
02446 { MKTAG('e','s','d','s'), mov_read_esds },
02447 { MKTAG('d','a','c','3'), mov_read_dac3 },
02448 { MKTAG('w','i','d','e'), mov_read_wide },
02449 { MKTAG('w','f','e','x'), mov_read_wfex },
02450 { MKTAG('c','m','o','v'), mov_read_cmov },
02451 { MKTAG('c','h','a','n'), mov_read_chan },
02452 { 0, NULL }
02453 };
02454
02455 static int mov_probe(AVProbeData *p)
02456 {
02457 unsigned int offset;
02458 uint32_t tag;
02459 int score = 0;
02460
02461
02462 offset = 0;
02463 for (;;) {
02464
02465 if ((offset + 8) > (unsigned int)p->buf_size)
02466 return score;
02467 tag = AV_RL32(p->buf + offset + 4);
02468 switch(tag) {
02469
02470 case MKTAG('j','P',' ',' '):
02471 case MKTAG('m','o','o','v'):
02472 case MKTAG('m','d','a','t'):
02473 case MKTAG('p','n','o','t'):
02474 case MKTAG('u','d','t','a'):
02475 case MKTAG('f','t','y','p'):
02476 return AVPROBE_SCORE_MAX;
02477
02478 case MKTAG('e','d','i','w'):
02479 case MKTAG('w','i','d','e'):
02480 case MKTAG('f','r','e','e'):
02481 case MKTAG('j','u','n','k'):
02482 case MKTAG('p','i','c','t'):
02483 return AVPROBE_SCORE_MAX - 5;
02484 case MKTAG(0x82,0x82,0x7f,0x7d):
02485 case MKTAG('s','k','i','p'):
02486 case MKTAG('u','u','i','d'):
02487 case MKTAG('p','r','f','l'):
02488 offset = AV_RB32(p->buf+offset) + offset;
02489
02490 score = AVPROBE_SCORE_MAX - 50;
02491 break;
02492 default:
02493
02494 return score;
02495 }
02496 }
02497 }
02498
02499
02500 static void mov_read_chapters(AVFormatContext *s)
02501 {
02502 MOVContext *mov = s->priv_data;
02503 AVStream *st = NULL;
02504 MOVStreamContext *sc;
02505 int64_t cur_pos;
02506 int i;
02507
02508 for (i = 0; i < s->nb_streams; i++)
02509 if (s->streams[i]->id == mov->chapter_track) {
02510 st = s->streams[i];
02511 break;
02512 }
02513 if (!st) {
02514 av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
02515 return;
02516 }
02517
02518 st->discard = AVDISCARD_ALL;
02519 sc = st->priv_data;
02520 cur_pos = avio_tell(sc->pb);
02521
02522 for (i = 0; i < st->nb_index_entries; i++) {
02523 AVIndexEntry *sample = &st->index_entries[i];
02524 int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
02525 uint8_t *title;
02526 uint16_t ch;
02527 int len, title_len;
02528
02529 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
02530 av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
02531 goto finish;
02532 }
02533
02534
02535 len = avio_rb16(sc->pb);
02536 if (len > sample->size-2)
02537 continue;
02538 title_len = 2*len + 1;
02539 if (!(title = av_mallocz(title_len)))
02540 goto finish;
02541
02542
02543
02544
02545 if (!len) {
02546 title[0] = 0;
02547 } else {
02548 ch = avio_rb16(sc->pb);
02549 if (ch == 0xfeff)
02550 avio_get_str16be(sc->pb, len, title, title_len);
02551 else if (ch == 0xfffe)
02552 avio_get_str16le(sc->pb, len, title, title_len);
02553 else {
02554 AV_WB16(title, ch);
02555 if (len == 1 || len == 2)
02556 title[len] = 0;
02557 else
02558 avio_get_str(sc->pb, len - 2, title + 2, title_len - 2);
02559 }
02560 }
02561
02562 avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
02563 av_freep(&title);
02564 }
02565 finish:
02566 avio_seek(sc->pb, cur_pos, SEEK_SET);
02567 }
02568
02569 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
02570 {
02571 MOVContext *mov = s->priv_data;
02572 AVIOContext *pb = s->pb;
02573 int err;
02574 MOVAtom atom = { AV_RL32("root") };
02575
02576 mov->fc = s;
02577
02578 if (pb->seekable)
02579 atom.size = avio_size(pb);
02580 else
02581 atom.size = INT64_MAX;
02582
02583
02584 if ((err = mov_read_default(mov, pb, atom)) < 0) {
02585 av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
02586 return err;
02587 }
02588 if (!mov->found_moov) {
02589 av_log(s, AV_LOG_ERROR, "moov atom not found\n");
02590 return AVERROR_INVALIDDATA;
02591 }
02592 av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
02593
02594 if (pb->seekable && mov->chapter_track > 0)
02595 mov_read_chapters(s);
02596
02597 return 0;
02598 }
02599
02600 static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
02601 {
02602 AVIndexEntry *sample = NULL;
02603 int64_t best_dts = INT64_MAX;
02604 int i;
02605 for (i = 0; i < s->nb_streams; i++) {
02606 AVStream *avst = s->streams[i];
02607 MOVStreamContext *msc = avst->priv_data;
02608 if (msc->pb && msc->current_sample < avst->nb_index_entries) {
02609 AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
02610 int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
02611 av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
02612 if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
02613 (s->pb->seekable &&
02614 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
02615 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
02616 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
02617 sample = current_sample;
02618 best_dts = dts;
02619 *st = avst;
02620 }
02621 }
02622 }
02623 return sample;
02624 }
02625
02626 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
02627 {
02628 MOVContext *mov = s->priv_data;
02629 MOVStreamContext *sc;
02630 AVIndexEntry *sample;
02631 AVStream *st = NULL;
02632 int ret;
02633 retry:
02634 sample = mov_find_next_sample(s, &st);
02635 if (!sample) {
02636 mov->found_mdat = 0;
02637 if (s->pb->seekable||
02638 mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
02639 s->pb->eof_reached)
02640 return AVERROR_EOF;
02641 av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
02642 goto retry;
02643 }
02644 sc = st->priv_data;
02645
02646 sc->current_sample++;
02647
02648 if (st->discard != AVDISCARD_ALL) {
02649 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
02650 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
02651 sc->ffindex, sample->pos);
02652 return AVERROR_INVALIDDATA;
02653 }
02654 ret = av_get_packet(sc->pb, pkt, sample->size);
02655 if (ret < 0)
02656 return ret;
02657 if (sc->has_palette) {
02658 uint8_t *pal;
02659
02660 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
02661 if (!pal) {
02662 av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
02663 } else {
02664 memcpy(pal, sc->palette, AVPALETTE_SIZE);
02665 sc->has_palette = 0;
02666 }
02667 }
02668 #if CONFIG_DV_DEMUXER
02669 if (mov->dv_demux && sc->dv_audio_container) {
02670 avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
02671 av_free(pkt->data);
02672 pkt->size = 0;
02673 ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
02674 if (ret < 0)
02675 return ret;
02676 }
02677 #endif
02678 }
02679
02680 pkt->stream_index = sc->ffindex;
02681 pkt->dts = sample->timestamp;
02682 if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
02683 pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
02684
02685 sc->ctts_sample++;
02686 if (sc->ctts_index < sc->ctts_count &&
02687 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
02688 sc->ctts_index++;
02689 sc->ctts_sample = 0;
02690 }
02691 if (sc->wrong_dts)
02692 pkt->dts = AV_NOPTS_VALUE;
02693 } else {
02694 int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
02695 st->index_entries[sc->current_sample].timestamp : st->duration;
02696 pkt->duration = next_dts - pkt->dts;
02697 pkt->pts = pkt->dts;
02698 }
02699 if (st->discard == AVDISCARD_ALL)
02700 goto retry;
02701 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
02702 pkt->pos = sample->pos;
02703 av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
02704 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
02705 return 0;
02706 }
02707
02708 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
02709 {
02710 MOVStreamContext *sc = st->priv_data;
02711 int sample, time_sample;
02712 int i;
02713
02714 sample = av_index_search_timestamp(st, timestamp, flags);
02715 av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
02716 if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
02717 sample = 0;
02718 if (sample < 0)
02719 return AVERROR_INVALIDDATA;
02720 sc->current_sample = sample;
02721 av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
02722
02723 if (sc->ctts_data) {
02724 time_sample = 0;
02725 for (i = 0; i < sc->ctts_count; i++) {
02726 int next = time_sample + sc->ctts_data[i].count;
02727 if (next > sc->current_sample) {
02728 sc->ctts_index = i;
02729 sc->ctts_sample = sc->current_sample - time_sample;
02730 break;
02731 }
02732 time_sample = next;
02733 }
02734 }
02735 return sample;
02736 }
02737
02738 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
02739 {
02740 AVStream *st;
02741 int64_t seek_timestamp, timestamp;
02742 int sample;
02743 int i;
02744
02745 if (stream_index >= s->nb_streams)
02746 return AVERROR_INVALIDDATA;
02747 if (sample_time < 0)
02748 sample_time = 0;
02749
02750 st = s->streams[stream_index];
02751 sample = mov_seek_stream(s, st, sample_time, flags);
02752 if (sample < 0)
02753 return sample;
02754
02755
02756 seek_timestamp = st->index_entries[sample].timestamp;
02757
02758 for (i = 0; i < s->nb_streams; i++) {
02759 st = s->streams[i];
02760 if (stream_index == i)
02761 continue;
02762
02763 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
02764 mov_seek_stream(s, st, timestamp, flags);
02765 }
02766 return 0;
02767 }
02768
02769 static int mov_read_close(AVFormatContext *s)
02770 {
02771 MOVContext *mov = s->priv_data;
02772 int i, j;
02773
02774 for (i = 0; i < s->nb_streams; i++) {
02775 AVStream *st = s->streams[i];
02776 MOVStreamContext *sc = st->priv_data;
02777
02778 av_freep(&sc->ctts_data);
02779 for (j = 0; j < sc->drefs_count; j++) {
02780 av_freep(&sc->drefs[j].path);
02781 av_freep(&sc->drefs[j].dir);
02782 }
02783 av_freep(&sc->drefs);
02784 if (sc->pb && sc->pb != s->pb)
02785 avio_close(sc->pb);
02786 }
02787
02788 if (mov->dv_demux) {
02789 for (i = 0; i < mov->dv_fctx->nb_streams; i++) {
02790 av_freep(&mov->dv_fctx->streams[i]->codec);
02791 av_freep(&mov->dv_fctx->streams[i]);
02792 }
02793 av_freep(&mov->dv_fctx);
02794 av_freep(&mov->dv_demux);
02795 }
02796
02797 av_freep(&mov->trex_data);
02798
02799 return 0;
02800 }
02801
02802 AVInputFormat ff_mov_demuxer = {
02803 .name = "mov,mp4,m4a,3gp,3g2,mj2",
02804 .long_name = NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
02805 .priv_data_size = sizeof(MOVContext),
02806 .read_probe = mov_probe,
02807 .read_header = mov_read_header,
02808 .read_packet = mov_read_packet,
02809 .read_close = mov_read_close,
02810 .read_seek = mov_read_seek,
02811 };