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