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