~ubuntu-branches/ubuntu/lucid/ffmpeg/lucid-updates

« back to all changes in this revision

Viewing changes to libavformat/mov.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2009-01-20 17:51:19 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090120175119-gu6kw1arv5tmf1vr
Tags: 3:0.svn20090119-1ubuntu1+unstripped1
* merge with the ubuntu.jaunty branch
* reenable x264 LP: #303537
* build against vdpau
* enable xvmc support

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
//#define DEBUG
25
25
 
 
26
#include "libavutil/intreadwrite.h"
26
27
#include "avformat.h"
27
28
#include "riff.h"
28
29
#include "isom.h"
30
31
#include "libavcodec/mpeg4audio.h"
31
32
#include "libavcodec/mpegaudiodata.h"
32
33
 
33
 
#ifdef CONFIG_ZLIB
 
34
#if CONFIG_ZLIB
34
35
#include <zlib.h>
35
36
#endif
36
37
 
69
70
    int first;
70
71
    int count;
71
72
    int id;
72
 
} MOV_stsc_t;
 
73
} MOVStsc;
73
74
 
74
75
typedef struct {
75
76
    uint32_t type;
76
77
    char *path;
77
 
} MOV_dref_t;
 
78
} MOVDref;
78
79
 
79
80
typedef struct {
80
81
    uint32_t type;
81
82
    int64_t offset;
82
83
    int64_t size; /* total size (excluding the size and type fields) */
83
 
} MOV_atom_t;
 
84
} MOVAtom;
84
85
 
85
86
struct MOVParseTableEntry;
86
87
 
109
110
    unsigned int chunk_count;
110
111
    int64_t *chunk_offsets;
111
112
    unsigned int stts_count;
112
 
    MOV_stts_t *stts_data;
 
113
    MOVStts *stts_data;
113
114
    unsigned int ctts_count;
114
 
    MOV_stts_t *ctts_data;
 
115
    MOVStts *ctts_data;
115
116
    unsigned int edit_count; /* number of 'edit' (elst atom) */
116
117
    unsigned int sample_to_chunk_sz;
117
 
    MOV_stsc_t *sample_to_chunk;
 
118
    MOVStsc *sample_to_chunk;
118
119
    int sample_to_ctime_index;
119
120
    int sample_to_ctime_sample;
120
121
    unsigned int sample_size;
131
132
    int pseudo_stream_id; ///< -1 means demux all ids
132
133
    int16_t audio_cid; ///< stsd audio compression id
133
134
    unsigned drefs_count;
134
 
    MOV_dref_t *drefs;
 
135
    MOVDref *drefs;
135
136
    int dref_id;
136
137
    int wrong_dts; ///< dts are wrong due to negative ctts
 
138
    int width;  ///< tkhd width
 
139
    int height; ///< tkhd height
137
140
} MOVStreamContext;
138
141
 
139
142
typedef struct MOVContext {
149
152
    MOVFragment fragment; ///< current fragment in moof atom
150
153
    MOVTrackExt *trex_data;
151
154
    unsigned trex_count;
 
155
    int itunes_metadata; ///< metadata are itunes style
152
156
} MOVContext;
153
157
 
154
158
 
162
166
/* links atom IDs to parse functions */
163
167
typedef struct MOVParseTableEntry {
164
168
    uint32_t type;
165
 
    int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
 
169
    int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOVAtom atom);
166
170
} MOVParseTableEntry;
167
171
 
168
172
static const MOVParseTableEntry mov_default_parse_table[];
169
173
 
170
 
static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
174
static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
171
175
{
172
176
    int64_t total_size = 0;
173
 
    MOV_atom_t a;
 
177
    MOVAtom a;
174
178
    int i;
175
179
    int err = 0;
176
180
 
231
235
    return err;
232
236
}
233
237
 
234
 
static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
238
static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
235
239
{
236
240
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
237
241
    MOVStreamContext *sc = st->priv_data;
245
249
    sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
246
250
 
247
251
    for (i = 0; i < sc->drefs_count; i++) {
248
 
        MOV_dref_t *dref = &sc->drefs[i];
 
252
        MOVDref *dref = &sc->drefs[i];
249
253
        uint32_t size = get_be32(pb);
250
254
        int64_t next = url_ftell(pb) + size - 4;
251
255
 
299
303
    return 0;
300
304
}
301
305
 
302
 
static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
306
static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
303
307
{
304
308
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
305
309
    uint32_t type;
372
376
    { CODEC_ID_NONE,    0 },
373
377
};
374
378
 
375
 
static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
379
static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
376
380
{
377
381
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
378
382
    int tag, len;
429
433
    return 0;
430
434
}
431
435
 
 
436
static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 
437
{
 
438
    const int num = get_be32(pb);
 
439
    const int den = get_be32(pb);
 
440
    AVStream * const st = c->fc->streams[c->fc->nb_streams-1];
 
441
    if (den != 0) {
 
442
        if ((st->sample_aspect_ratio.den && den != st->sample_aspect_ratio.den) ||
 
443
            (st->sample_aspect_ratio.num && num != st->sample_aspect_ratio.num))
 
444
            av_log(c->fc, AV_LOG_WARNING,
 
445
                   "sample aspect ratio already set, overriding by 'pasp' atom\n");
 
446
        st->sample_aspect_ratio.num = num;
 
447
        st->sample_aspect_ratio.den = den;
 
448
    }
 
449
    return 0;
 
450
}
 
451
 
432
452
/* this atom contains actual media data */
433
 
static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
453
static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
434
454
{
435
455
    if(atom.size == 0) /* wrong one (MP4) */
436
456
        return 0;
438
458
    return 0; /* now go for moov */
439
459
}
440
460
 
441
 
static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
461
static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
442
462
{
443
463
    uint32_t type = get_le32(pb);
444
464
 
451
471
}
452
472
 
453
473
/* this atom should contain all header atoms */
454
 
static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
474
static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
455
475
{
456
476
    if (mov_read_default(c, pb, atom) < 0)
457
477
        return -1;
461
481
    return 0; /* now go for mdat */
462
482
}
463
483
 
464
 
static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
484
static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
465
485
{
466
486
    c->fragment.moof_offset = url_ftell(pb) - 8;
467
487
    dprintf(c->fc, "moof offset %llx\n", c->fragment.moof_offset);
468
488
    return mov_read_default(c, pb, atom);
469
489
}
470
490
 
471
 
static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
491
static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
472
492
{
473
493
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
474
494
    MOVStreamContext *sc = st->priv_data;
497
517
    return 0;
498
518
}
499
519
 
500
 
static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
520
static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
501
521
{
502
522
    int version = get_byte(pb); /* version */
503
523
    get_be24(pb); /* flags */
533
553
    return 0;
534
554
}
535
555
 
536
 
static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
556
static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
537
557
{
538
558
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
539
559
 
553
573
    return 0;
554
574
}
555
575
 
556
 
static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
576
static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
557
577
{
558
578
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
559
579
    int little_endian = get_be16(pb);
560
580
 
561
 
    if (little_endian) {
 
581
    dprintf(c->fc, "enda %d\n", little_endian);
 
582
    if (little_endian == 1) {
562
583
        switch (st->codec->codec_id) {
563
584
        case CODEC_ID_PCM_S24BE:
564
585
            st->codec->codec_id = CODEC_ID_PCM_S24LE;
580
601
}
581
602
 
582
603
/* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
583
 
static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
604
static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
584
605
{
585
 
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
586
 
    uint64_t size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
 
606
    AVStream *st;
 
607
    uint64_t size;
587
608
    uint8_t *buf;
 
609
 
 
610
    if (c->fc->nb_streams < 1) // will happen with jp2 files
 
611
        return 0;
 
612
    st= c->fc->streams[c->fc->nb_streams-1];
 
613
    size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
588
614
    if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
589
615
        return -1;
590
616
    buf= av_realloc(st->codec->extradata, size);
599
625
    return 0;
600
626
}
601
627
 
602
 
static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
628
static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
603
629
{
604
630
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
605
631
 
626
652
 * This function reads atom content and puts data in extradata without tag
627
653
 * nor size unlike mov_read_extradata.
628
654
 */
629
 
static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
655
static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
630
656
{
631
657
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
632
658
 
642
668
    return 0;
643
669
}
644
670
 
645
 
static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
671
static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
646
672
{
647
673
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
648
674
    MOVStreamContext *sc = st->priv_data;
707
733
    return CODEC_ID_NONE;
708
734
}
709
735
 
710
 
static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
736
static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
711
737
{
712
738
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
713
739
    MOVStreamContext *sc = st->priv_data;
722
748
        //Parsing Sample description table
723
749
        enum CodecID id;
724
750
        int dref_id;
725
 
        MOV_atom_t a = { 0, 0, 0 };
 
751
        MOVAtom a = { 0, 0, 0 };
726
752
        int64_t start_pos = url_ftell(pb);
727
753
        int size = get_be32(pb); /* size */
728
754
        uint32_t format = get_le32(pb); /* data format */
954
980
                sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
955
981
            }
956
982
        } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
 
983
            // ttxt stsd contains display flags, justification, background
 
984
            // color, fonts, and default styles, so fake an atom to read it
 
985
            MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) };
 
986
            mov_read_glbl(c, pb, fake_atom);
957
987
            st->codec->codec_id= id;
 
988
            st->codec->width = sc->width;
 
989
            st->codec->height = sc->height;
958
990
        } else {
959
991
            /* other codec type, just skip (rtp, mp4s, tmcd ...) */
960
992
            url_fskip(pb, size - (url_ftell(pb) - start_pos));
973
1005
 
974
1006
    /* special codec parameters handling */
975
1007
    switch (st->codec->codec_id) {
976
 
#ifdef CONFIG_DV_DEMUXER
 
1008
#if CONFIG_DV_DEMUXER
977
1009
    case CODEC_ID_DVAUDIO:
978
1010
        c->dv_fctx = av_alloc_format_context();
979
1011
        c->dv_demux = dv_init_demux(c->dv_fctx);
987
1019
#endif
988
1020
    /* no ifdef since parameters are always those */
989
1021
    case CODEC_ID_QCELP:
 
1022
        st->codec->frame_size= 160;
 
1023
        st->codec->channels= 1; /* really needed */
 
1024
        break;
990
1025
    case CODEC_ID_AMR_NB:
991
1026
    case CODEC_ID_AMR_WB:
992
1027
        st->codec->frame_size= sc->samples_per_frame;
1008
1043
        st->codec->block_align = sc->bytes_per_frame;
1009
1044
        break;
1010
1045
    case CODEC_ID_ALAC:
1011
 
        if (st->codec->extradata_size == 36)
1012
 
            st->codec->frame_size = AV_RB32((st->codec->extradata+12));
 
1046
        if (st->codec->extradata_size == 36) {
 
1047
            st->codec->frame_size = AV_RB32(st->codec->extradata+12);
 
1048
            st->codec->channels   = AV_RB8 (st->codec->extradata+21);
 
1049
        }
1013
1050
        break;
1014
1051
    default:
1015
1052
        break;
1018
1055
    return 0;
1019
1056
}
1020
1057
 
1021
 
static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1058
static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1022
1059
{
1023
1060
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1024
1061
    MOVStreamContext *sc = st->priv_data;
1029
1066
 
1030
1067
    entries = get_be32(pb);
1031
1068
 
1032
 
    if(entries >= UINT_MAX / sizeof(MOV_stsc_t))
 
1069
    if(entries >= UINT_MAX / sizeof(*sc->sample_to_chunk))
1033
1070
        return -1;
1034
1071
 
1035
1072
    dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1036
1073
 
1037
1074
    sc->sample_to_chunk_sz = entries;
1038
 
    sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_stsc_t));
 
1075
    sc->sample_to_chunk = av_malloc(entries * sizeof(*sc->sample_to_chunk));
1039
1076
    if (!sc->sample_to_chunk)
1040
1077
        return -1;
1041
1078
    for(i=0; i<entries; i++) {
1046
1083
    return 0;
1047
1084
}
1048
1085
 
1049
 
static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1086
static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1050
1087
{
1051
1088
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1052
1089
    MOVStreamContext *sc = st->priv_data;
1074
1111
    return 0;
1075
1112
}
1076
1113
 
1077
 
static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1114
static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1078
1115
{
1079
1116
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1080
1117
    MOVStreamContext *sc = st->priv_data;
1104
1141
    return 0;
1105
1142
}
1106
1143
 
1107
 
static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1144
static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1108
1145
{
1109
1146
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1110
1147
    MOVStreamContext *sc = st->priv_data;
1115
1152
    get_byte(pb); /* version */
1116
1153
    get_be24(pb); /* flags */
1117
1154
    entries = get_be32(pb);
1118
 
    if(entries >= UINT_MAX / sizeof(MOV_stts_t))
 
1155
    if(entries >= UINT_MAX / sizeof(*sc->stts_data))
1119
1156
        return -1;
1120
1157
 
1121
1158
    sc->stts_count = entries;
1122
 
    sc->stts_data = av_malloc(entries * sizeof(MOV_stts_t));
 
1159
    sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
1123
1160
    if (!sc->stts_data)
1124
1161
        return -1;
1125
1162
    dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1135
1172
        sc->stts_data[i].count= sample_count;
1136
1173
        sc->stts_data[i].duration= sample_duration;
1137
1174
 
1138
 
        sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
 
1175
        sc->time_rate= av_gcd(sc->time_rate, sample_duration);
1139
1176
 
1140
1177
        dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
1141
1178
 
1149
1186
    return 0;
1150
1187
}
1151
1188
 
1152
 
static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1189
static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1153
1190
{
1154
1191
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1155
1192
    MOVStreamContext *sc = st->priv_data;
1158
1195
    get_byte(pb); /* version */
1159
1196
    get_be24(pb); /* flags */
1160
1197
    entries = get_be32(pb);
1161
 
    if(entries >= UINT_MAX / sizeof(MOV_stts_t))
 
1198
    if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
1162
1199
        return -1;
1163
1200
 
1164
1201
    sc->ctts_count = entries;
1165
 
    sc->ctts_data = av_malloc(entries * sizeof(MOV_stts_t));
 
1202
    sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
1166
1203
    if (!sc->ctts_data)
1167
1204
        return -1;
1168
1205
    dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
1178
1215
        sc->ctts_data[i].count   = count;
1179
1216
        sc->ctts_data[i].duration= duration;
1180
1217
 
1181
 
        sc->time_rate= ff_gcd(sc->time_rate, FFABS(duration));
 
1218
        sc->time_rate= av_gcd(sc->time_rate, FFABS(duration));
1182
1219
    }
1183
1220
    return 0;
1184
1221
}
1291
1328
    sc->sample_count = st->nb_index_entries;
1292
1329
}
1293
1330
 
1294
 
static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1331
static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1295
1332
{
1296
1333
    AVStream *st;
1297
1334
    MOVStreamContext *sc;
1305
1342
    st->priv_data = sc;
1306
1343
    st->codec->codec_type = CODEC_TYPE_DATA;
1307
1344
    st->start_time = 0; /* XXX: check */
 
1345
    sc->ffindex = st->index;
1308
1346
 
1309
1347
    if ((ret = mov_read_default(c, pb, atom)) < 0)
1310
1348
        return ret;
1331
1369
        assert(st->duration % sc->time_rate == 0);
1332
1370
        st->duration /= sc->time_rate;
1333
1371
    }
1334
 
    sc->ffindex = st->index;
 
1372
 
1335
1373
    mov_build_index(c, st);
1336
1374
 
1337
1375
    if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
1342
1380
        sc->pb = c->fc->pb;
1343
1381
 
1344
1382
    switch (st->codec->codec_id) {
1345
 
#ifdef CONFIG_H261_DECODER
 
1383
#if CONFIG_H261_DECODER
1346
1384
    case CODEC_ID_H261:
1347
1385
#endif
1348
 
#ifdef CONFIG_H263_DECODER
 
1386
#if CONFIG_H263_DECODER
1349
1387
    case CODEC_ID_H263:
1350
1388
#endif
1351
 
#ifdef CONFIG_MPEG4_DECODER
 
1389
#if CONFIG_MPEG4_DECODER
1352
1390
    case CODEC_ID_MPEG4:
1353
1391
#endif
1354
1392
        st->codec->width= 0; /* let decoder init width/height */
1366
1404
    return 0;
1367
1405
}
1368
1406
 
1369
 
static void mov_parse_udta_string(ByteIOContext *pb, char *str, int size)
1370
 
{
1371
 
    uint16_t str_size = get_be16(pb); /* string length */;
1372
 
 
1373
 
    get_be16(pb); /* skip language */
 
1407
static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 
1408
{
 
1409
    int ret;
 
1410
    c->itunes_metadata = 1;
 
1411
    ret = mov_read_default(c, pb, atom);
 
1412
    c->itunes_metadata = 0;
 
1413
    return ret;
 
1414
}
 
1415
 
 
1416
static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 
1417
{
 
1418
    url_fskip(pb, 4); // version + flags
 
1419
    atom.size -= 4;
 
1420
    return mov_read_default(c, pb, atom);
 
1421
}
 
1422
 
 
1423
static int mov_read_trkn(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 
1424
{
 
1425
    get_be32(pb); // type
 
1426
    get_be32(pb); // unknown
 
1427
    c->fc->track = get_be32(pb);
 
1428
    dprintf(c->fc, "%.4s %d\n", (char*)&atom.type, c->fc->track);
 
1429
    return 0;
 
1430
}
 
1431
 
 
1432
static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
 
1433
{
 
1434
    char *str = NULL;
 
1435
    int size;
 
1436
    uint16_t str_size;
 
1437
 
 
1438
    if (c->itunes_metadata) {
 
1439
        int data_size = get_be32(pb);
 
1440
        int tag = get_le32(pb);
 
1441
        if (tag == MKTAG('d','a','t','a')) {
 
1442
            get_be32(pb); // type
 
1443
            get_be32(pb); // unknown
 
1444
            str_size = data_size - 16;
 
1445
        } else return 0;
 
1446
    } else {
 
1447
        str_size = get_be16(pb); // string length
 
1448
        get_be16(pb); // language
 
1449
    }
 
1450
    switch (atom.type) {
 
1451
    case MKTAG(0xa9,'n','a','m'):
 
1452
        str = c->fc->title; size = sizeof(c->fc->title); break;
 
1453
    case MKTAG(0xa9,'A','R','T'):
 
1454
    case MKTAG(0xa9,'w','r','t'):
 
1455
        str = c->fc->author; size = sizeof(c->fc->author); break;
 
1456
    case MKTAG(0xa9,'c','p','y'):
 
1457
        str = c->fc->copyright; size = sizeof(c->fc->copyright); break;
 
1458
    case MKTAG(0xa9,'c','m','t'):
 
1459
    case MKTAG(0xa9,'i','n','f'):
 
1460
        str = c->fc->comment; size = sizeof(c->fc->comment); break;
 
1461
    case MKTAG(0xa9,'a','l','b'):
 
1462
        str = c->fc->album; size = sizeof(c->fc->album); break;
 
1463
    }
 
1464
    if (!str)
 
1465
        return 0;
1374
1466
    get_buffer(pb, str, FFMIN(size, str_size));
1375
 
}
1376
 
 
1377
 
static int mov_read_udta(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1378
 
{
1379
 
    uint64_t end = url_ftell(pb) + atom.size;
1380
 
 
1381
 
    while (url_ftell(pb) + 8 < end) {
1382
 
        uint32_t tag_size = get_be32(pb);
1383
 
        uint32_t tag      = get_le32(pb);
1384
 
        uint64_t next     = url_ftell(pb) + tag_size - 8;
1385
 
 
1386
 
        if (tag_size < 8 || next > end) // stop if tag_size is wrong
1387
 
            break;
1388
 
 
1389
 
        switch (tag) {
1390
 
        case MKTAG(0xa9,'n','a','m'):
1391
 
            mov_parse_udta_string(pb, c->fc->title,     sizeof(c->fc->title));
1392
 
            break;
1393
 
        case MKTAG(0xa9,'w','r','t'):
1394
 
            mov_parse_udta_string(pb, c->fc->author,    sizeof(c->fc->author));
1395
 
            break;
1396
 
        case MKTAG(0xa9,'c','p','y'):
1397
 
            mov_parse_udta_string(pb, c->fc->copyright, sizeof(c->fc->copyright));
1398
 
            break;
1399
 
        case MKTAG(0xa9,'i','n','f'):
1400
 
            mov_parse_udta_string(pb, c->fc->comment,   sizeof(c->fc->comment));
1401
 
            break;
1402
 
        default:
1403
 
            break;
1404
 
        }
1405
 
 
1406
 
        url_fseek(pb, next, SEEK_SET);
1407
 
    }
1408
 
 
 
1467
    dprintf(c->fc, "%.4s %s\n", (char*)&atom.type, str);
1409
1468
    return 0;
1410
1469
}
1411
1470
 
1412
 
static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1471
static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1413
1472
{
1414
1473
    int i;
1415
1474
    int width;
1417
1476
    int64_t disp_transform[2];
1418
1477
    int display_matrix[3][2];
1419
1478
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
 
1479
    MOVStreamContext *sc = st->priv_data;
1420
1480
    int version = get_byte(pb);
1421
1481
 
1422
1482
    get_be24(pb); /* flags */
1458
1518
 
1459
1519
    width = get_be32(pb);       // 16.16 fixed point track width
1460
1520
    height = get_be32(pb);      // 16.16 fixed point track height
 
1521
    sc->width = width >> 16;
 
1522
    sc->height = height >> 16;
1461
1523
 
1462
1524
    //transform the display width/height according to the matrix
1463
1525
    // skip this if the display matrix is the default identity matrix
1480
1542
    return 0;
1481
1543
}
1482
1544
 
1483
 
static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1545
static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1484
1546
{
1485
1547
    MOVFragment *frag = &c->fragment;
1486
1548
    MOVTrackExt *trex = NULL;
1515
1577
    return 0;
1516
1578
}
1517
1579
 
1518
 
static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1580
static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1519
1581
{
1520
1582
    MOVTrackExt *trex;
1521
1583
 
1535
1597
    return 0;
1536
1598
}
1537
1599
 
1538
 
static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1600
static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1539
1601
{
1540
1602
    MOVFragment *frag = &c->fragment;
1541
1603
    AVStream *st;
1606
1668
/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1607
1669
/* like the files created with Adobe Premiere 5.0, for samples see */
1608
1670
/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1609
 
static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1671
static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1610
1672
{
1611
1673
    int err;
1612
1674
 
1627
1689
    return err;
1628
1690
}
1629
1691
 
1630
 
static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1692
static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1631
1693
{
1632
 
#ifdef CONFIG_ZLIB
 
1694
#if CONFIG_ZLIB
1633
1695
    ByteIOContext ctx;
1634
1696
    uint8_t *cmov_data;
1635
1697
    uint8_t *moov_data; /* uncompressed data */
1680
1742
}
1681
1743
 
1682
1744
/* edit list atom */
1683
 
static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
 
1745
static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1684
1746
{
1685
1747
    MOVStreamContext *sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
1686
1748
    int i, edit_count;
1715
1777
{ MKTAG('f','t','y','p'), mov_read_ftyp },
1716
1778
{ MKTAG('g','l','b','l'), mov_read_glbl },
1717
1779
{ MKTAG('h','d','l','r'), mov_read_hdlr },
 
1780
{ MKTAG('i','l','s','t'), mov_read_ilst },
1718
1781
{ MKTAG('j','p','2','h'), mov_read_extradata },
1719
1782
{ MKTAG('m','d','a','t'), mov_read_mdat },
1720
1783
{ MKTAG('m','d','h','d'), mov_read_mdhd },
1721
1784
{ MKTAG('m','d','i','a'), mov_read_default },
 
1785
{ MKTAG('m','e','t','a'), mov_read_meta },
1722
1786
{ MKTAG('m','i','n','f'), mov_read_default },
1723
1787
{ MKTAG('m','o','o','f'), mov_read_moof },
1724
1788
{ MKTAG('m','o','o','v'), mov_read_moov },
1727
1791
{ MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
1728
1792
{ MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
1729
1793
{ MKTAG('a','v','c','C'), mov_read_glbl },
 
1794
{ MKTAG('p','a','s','p'), mov_read_pasp },
1730
1795
{ MKTAG('s','t','b','l'), mov_read_default },
1731
1796
{ MKTAG('s','t','c','o'), mov_read_stco },
1732
1797
{ MKTAG('s','t','s','c'), mov_read_stsc },
1739
1804
{ MKTAG('t','r','a','k'), mov_read_trak },
1740
1805
{ MKTAG('t','r','a','f'), mov_read_default },
1741
1806
{ MKTAG('t','r','e','x'), mov_read_trex },
 
1807
{ MKTAG('t','r','k','n'), mov_read_trkn },
1742
1808
{ MKTAG('t','r','u','n'), mov_read_trun },
1743
 
{ MKTAG('u','d','t','a'), mov_read_udta },
 
1809
{ MKTAG('u','d','t','a'), mov_read_default },
1744
1810
{ MKTAG('w','a','v','e'), mov_read_wave },
1745
1811
{ MKTAG('e','s','d','s'), mov_read_esds },
1746
1812
{ MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
1747
1813
{ MKTAG('c','m','o','v'), mov_read_cmov },
 
1814
{ MKTAG(0xa9,'n','a','m'), mov_read_udta_string },
 
1815
{ MKTAG(0xa9,'w','r','t'), mov_read_udta_string },
 
1816
{ MKTAG(0xa9,'c','p','y'), mov_read_udta_string },
 
1817
{ MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
 
1818
{ MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
 
1819
{ MKTAG(0xa9,'A','R','T'), mov_read_udta_string },
 
1820
{ MKTAG(0xa9,'a','l','b'), mov_read_udta_string },
 
1821
{ MKTAG(0xa9,'c','m','t'), mov_read_udta_string },
1748
1822
{ 0, NULL }
1749
1823
};
1750
1824
 
1798
1872
    MOVContext *mov = s->priv_data;
1799
1873
    ByteIOContext *pb = s->pb;
1800
1874
    int err;
1801
 
    MOV_atom_t atom = { 0, 0, 0 };
 
1875
    MOVAtom atom = { 0, 0, 0 };
1802
1876
 
1803
1877
    mov->fc = s;
1804
1878
    /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1851
1925
    if (!sample) {
1852
1926
        mov->found_mdat = 0;
1853
1927
        if (!url_is_streamed(s->pb) ||
1854
 
            mov_read_default(mov, s->pb, (MOV_atom_t){ 0, 0, INT64_MAX }) < 0 ||
 
1928
            mov_read_default(mov, s->pb, (MOVAtom){ 0, 0, INT64_MAX }) < 0 ||
1855
1929
            url_feof(s->pb))
1856
1930
            return -1;
1857
1931
        dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
1865
1939
        return -1;
1866
1940
    }
1867
1941
    av_get_packet(sc->pb, pkt, sample->size);
1868
 
#ifdef CONFIG_DV_DEMUXER
 
1942
#if CONFIG_DV_DEMUXER
1869
1943
    if (mov->dv_demux && sc->dv_audio_container) {
1870
1944
        dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
1871
1945
        av_free(pkt->data);