Statistics
| Branch: | Revision:

ffmpeg / libavformat / mov.c @ 7caf0cc6

History | View | Annotate | Download (62.8 KB)

1 6cea494e Zdenek Kabelac
/*
2 7fbde343 Aurelien Jacobs
 * MOV demuxer
3 19720f15 Fabrice Bellard
 * Copyright (c) 2001 Fabrice Bellard.
4 6cea494e Zdenek Kabelac
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 6cea494e Zdenek Kabelac
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 6cea494e Zdenek Kabelac
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 6cea494e Zdenek Kabelac
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 6cea494e Zdenek Kabelac
 */
21 d957696f Michael Niedermayer
22
#include <limits.h>
23 115329f1 Diego Biurrun
24 bfc2a19d Baptiste Coudurier
//#define DEBUG
25 4e5ef14f Baptiste Coudurier
26 6cea494e Zdenek Kabelac
#include "avformat.h"
27 9d9f4119 Måns Rullgård
#include "riff.h"
28 e40ee6a2 Baptiste Coudurier
#include "isom.h"
29 b60c0454 Baptiste Coudurier
#include "dv.h"
30 6cea494e Zdenek Kabelac
31 0147f198 François Revol
#ifdef CONFIG_ZLIB
32
#include <zlib.h>
33
#endif
34
35 6cea494e Zdenek Kabelac
/*
36
 * First version by Francois Revol revol@free.fr
37 115329f1 Diego Biurrun
 * Seek function by Gael Chardon gael.dev@4now.net
38 5ca1d879 Zdenek Kabelac
 *
39 6cea494e Zdenek Kabelac
 * Features and limitations:
40 5ca1d879 Zdenek Kabelac
 * - reads most of the QT files I have (at least the structure),
41 6cea494e Zdenek Kabelac
 *   the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
42 0147f198 François Revol
 *   FIXED, Francois Revol, 07/17/2002
43 6cea494e Zdenek Kabelac
 * - ffmpeg has nearly none of the usual QuickTime codecs,
44
 *   although I succesfully dumped raw and mp3 audio tracks off .mov files.
45
 *   Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
46
 * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
47
 *   (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
48
 *   http://mpeg.telecomitalialab.com/faq.htm
49
 * - the code is quite ugly... maybe I won't do it recursive next time :-)
50 baf25c9d Gael Chardon
 * - seek is not supported with files that contain edit list
51 5ca1d879 Zdenek Kabelac
 *
52 6cea494e Zdenek Kabelac
 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
53
 * when coding this :) (it's a writer anyway)
54 5ca1d879 Zdenek Kabelac
 *
55 6cea494e Zdenek Kabelac
 * Reference documents:
56
 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
57
 * Apple:
58 baf25c9d Gael Chardon
 *  http://developer.apple.com/documentation/QuickTime/QTFF/
59 15c8dbe7 Sebastien Bechet
 *  http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
60 6cea494e Zdenek Kabelac
 * QuickTime is a trademark of Apple (AFAIK :))
61
 */
62
63 b595afaa Mike Melanson
#include "qtpalette.h"
64
65 baf25c9d Gael Chardon
66 cd7352d5 Michael Niedermayer
#undef NDEBUG
67
#include <assert.h>
68
69 7caf0cc6 Michael Niedermayer
static const AVCodecTag mov_video_tags[] = {
70 6cea494e Zdenek Kabelac
/*  { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
71 0147f198 François Revol
/*  { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
72
/*  { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
73 53e27dd5 Sebastien Bechet
/*    { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
74 6cea494e Zdenek Kabelac
/* Graphics */
75
/* Animation */
76
/* Apple video */
77
/* Kodak Photo CD */
78 3ffe3793 François Revol
    { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
79 6cea494e Zdenek Kabelac
    { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
80 3ffe3793 François Revol
    { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
81 750b4aaa Baptiste Coudurier
    { CODEC_ID_MJPEGB, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
82 53e27dd5 Sebastien Bechet
    { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
83
/*    { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
84 20da94e8 Baptiste Coudurier
    { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, /* embedded gif files as frames (usually one "click to play movie" frame) */
85 6cea494e Zdenek Kabelac
/* Sorenson video */
86 0147f198 François Revol
    { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
87
    { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
88 3ffe3793 François Revol
    { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
89 8b82a956 Michael Niedermayer
    { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') }, /* Sorenson Video v3 */
90 0e7eed09 Fabrice Bellard
    { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
91 6cea494e Zdenek Kabelac
    { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
92 8dafdb88 Mike Melanson
    { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') },
93 fcc87242 François Revol
    { CODEC_ID_MPEG4, MKTAG('3', 'I', 'V', '2') }, /* experimental: 3IVX files before ivx D4 4.5.1 */
94 6cea494e Zdenek Kabelac
/*    { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
95 0147f198 François Revol
    { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
96 25fa62e1 Zdenek Kabelac
    { CODEC_ID_H263, MKTAG('s', '2', '6', '3') }, /* H263 ?? works */
97 e1687cc0 Fabrice Bellard
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
98
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
99 d86053a4 Mike Melanson
    { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
100 2fdf638b Mike Melanson
    { CODEC_ID_RPZA, MKTAG('r', 'p', 'z', 'a') }, /* Apple Video (RPZA) */
101
    { CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') }, /* Cinepak */
102 1dc1ed99 Roberto Togni
    { CODEC_ID_8BPS, MKTAG('8', 'B', 'P', 'S') }, /* Planar RGB (8BPS) */
103 b595afaa Mike Melanson
    { CODEC_ID_SMC, MKTAG('s', 'm', 'c', ' ') }, /* Apple Graphics (SMC) */
104 070ed1bc Mike Melanson
    { CODEC_ID_QTRLE, MKTAG('r', 'l', 'e', ' ') }, /* Apple Animation (RLE) */
105 429088dc Baptiste Coudurier
    { CODEC_ID_MSRLE, MKTAG('W', 'R', 'L', 'E') },
106 d08d7142 Mike Melanson
    { CODEC_ID_QDRAW, MKTAG('q', 'd', 'r', 'w') }, /* QuickDraw */
107 169eb021 Mike Melanson
    { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') }, /* AVC-1/H.264 */
108 a3075830 Aurelien Jacobs
    { CODEC_ID_MPEG2VIDEO, MKTAG('h', 'd', 'v', '2') }, /* MPEG2 produced by Sony HD camera */
109 dcadd216 Michael Niedermayer
    { CODEC_ID_MPEG2VIDEO, MKTAG('h', 'd', 'v', '3') }, /* HDV produced by FCP */
110 79c343c0 Baptiste Coudurier
    { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'x', '5', 'n') }, /* MPEG2 IMX NTSC 525/60 50mb/s produced by FCP */
111
    { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'x', '5', 'p') }, /* MPEG2 IMX PAL 625/50 50mb/s produced by FCP */
112
    { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'x', '3', 'n') }, /* MPEG2 IMX NTSC 525/60 30mb/s produced by FCP */
113
    { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'x', '3', 'p') }, /* MPEG2 IMX PAL 625/50 30mb/s produced by FCP */
114
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'p', 'p') }, /* DVCPRO PAL produced by FCP */
115
    //{ CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', '5') }, /* DVCPRO HD 50i produced by FCP */
116
    //{ CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', '6') }, /* DVCPRO HD 60i produced by FCP */
117
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'p') }, /* DVCPRO50 PAL produced by FCP */
118
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'n') }, /* DVCPRO50 NTSC produced by FCP */
119 b6c1bfe0 Baptiste Coudurier
    { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, /* AVID DV */
120 79c343c0 Baptiste Coudurier
    //{ CODEC_ID_JPEG2000, MKTAG('m', 'j', 'p', '2') }, /* JPEG 2000 produced by FCP */
121 3689cf16 Kostya Shishkov
    { CODEC_ID_TARGA, MKTAG('t', 'g', 'a', ' ') }, /* Truevision Targa */
122 a991b1fe Kostya Shishkov
    { CODEC_ID_TIFF, MKTAG('t', 'i', 'f', 'f') }, /* TIFF embedded in MOV */
123 ed70a509 Baptiste Coudurier
    { CODEC_ID_RAWVIDEO, MKTAG('2', 'v', 'u', 'y') }, /* UNCOMPRESSED 8BIT 4:2:2 */
124 c3135e11 Baptiste Coudurier
    { CODEC_ID_VC1,  MKTAG('v', 'c', '-', '1') }, /* SMPTE RP 2025 */
125
    { CODEC_ID_WMV3, MKTAG('v', 'c', '-', '1') }, /* SMPTE RP 2025 */
126 69d987a6 Baptiste Coudurier
    { CODEC_ID_PNG,  MKTAG('p', 'n', 'g', ' ') },
127 5ca1d879 Zdenek Kabelac
    { CODEC_ID_NONE, 0 },
128 6cea494e Zdenek Kabelac
};
129
130 7caf0cc6 Michael Niedermayer
static const AVCodecTag mov_audio_tags[] = {
131 bd12d3e9 Baptiste Coudurier
    { CODEC_ID_PCM_S32BE, MKTAG('i', 'n', '3', '2') },
132 dcadd216 Michael Niedermayer
    { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4') },
133 61e3dd78 Baptiste Coudurier
    { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, /* uncompressed */
134 6cea494e Zdenek Kabelac
    { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
135 5cd62665 Zdenek Kabelac
    { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
136 6cea494e Zdenek Kabelac
    { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /*  */
137
    { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /*  */
138
    { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /*  */
139 0147f198 François Revol
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
140 e8d2507c Baptiste Coudurier
    { CODEC_ID_ADPCM_MS, MKTAG('m', 's', 0x00, 0x02) }, /* MS ADPCM */
141 3f95e843 François Revol
    { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
142
    { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
143 6cea494e Zdenek Kabelac
144 2ec7b29b Baptiste Coudurier
    { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
145 6cea494e Zdenek Kabelac
    { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
146
    { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
147
/*    { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
148
/* MP4 tags */
149 f2196640 François Revol
    { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG-4 AAC */
150 5ca1d879 Zdenek Kabelac
    /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
151 891f64b3 joca@rixmail.se
    { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
152 d663a1fd Michael Niedermayer
    { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') }, /* AMR-WB 3gp */
153 8dafdb88 Mike Melanson
    { CODEC_ID_AC3, MKTAG('m', 's', 0x20, 0x00) }, /* Dolby AC-3 */
154 6d6d7970 Mike Melanson
    { CODEC_ID_ALAC,MKTAG('a', 'l', 'a', 'c') }, /* Apple Lossless */
155 d9b1c197 Roberto Togni
    { CODEC_ID_QDM2,MKTAG('Q', 'D', 'M', '2') }, /* QDM2 */
156 b60c0454 Baptiste Coudurier
    { CODEC_ID_DVAUDIO, MKTAG('v', 'd', 'v', 'a') },
157
    { CODEC_ID_DVAUDIO, MKTAG('d', 'v', 'c', 'a') },
158 e6f4c806 Baptiste Coudurier
    { CODEC_ID_PCM_S16LE, MKTAG('l', 'p', 'c', 'm') },
159 5ca1d879 Zdenek Kabelac
    { CODEC_ID_NONE, 0 },
160 6cea494e Zdenek Kabelac
};
161
162
/* the QuickTime file format is quite convoluted...
163
 * it has lots of index tables, each indexing something in another one...
164
 * Here we just use what is needed to read the chunks
165
 */
166
167
typedef struct MOV_sample_to_chunk_tbl {
168
    long first;
169
    long count;
170
    long id;
171
} MOV_sample_to_chunk_tbl;
172
173 5cd62665 Zdenek Kabelac
typedef struct {
174 5ca1d879 Zdenek Kabelac
    uint32_t type;
175
    int64_t offset;
176
    int64_t size; /* total size (excluding the size and type fields) */
177
} MOV_atom_t;
178
179
typedef struct {
180
    int seed;
181
    int flags;
182
    int size;
183
    void* clrs;
184
} MOV_ctab_t;
185
186 b72708f8 Baptiste Coudurier
typedef struct MOV_mdat_atom_s {
187
    offset_t offset;
188
    int64_t size;
189
} MOV_mdat_atom_t;
190
191 5ca1d879 Zdenek Kabelac
typedef struct {
192 5cd62665 Zdenek Kabelac
    uint8_t  version;
193
    uint32_t flags; // 24bit
194
195
    /* 0x03 ESDescrTag */
196
    uint16_t es_id;
197 bb270c08 Diego Biurrun
#define MP4ODescrTag                    0x01
198
#define MP4IODescrTag                   0x02
199
#define MP4ESDescrTag                   0x03
200
#define MP4DecConfigDescrTag            0x04
201
#define MP4DecSpecificDescrTag          0x05
202
#define MP4SLConfigDescrTag             0x06
203
#define MP4ContentIdDescrTag            0x07
204
#define MP4SupplContentIdDescrTag       0x08
205
#define MP4IPIPtrDescrTag               0x09
206
#define MP4IPMPPtrDescrTag              0x0A
207
#define MP4IPMPDescrTag                 0x0B
208
#define MP4RegistrationDescrTag         0x0D
209
#define MP4ESIDIncDescrTag              0x0E
210
#define MP4ESIDRefDescrTag              0x0F
211
#define MP4FileIODescrTag               0x10
212
#define MP4FileODescrTag                0x11
213
#define MP4ExtProfileLevelDescrTag      0x13
214
#define MP4ExtDescrTagsStart            0x80
215
#define MP4ExtDescrTagsEnd              0xFE
216 5cd62665 Zdenek Kabelac
    uint8_t  stream_priority;
217
218
    /* 0x04 DecConfigDescrTag */
219
    uint8_t  object_type_id;
220
    uint8_t  stream_type;
221
    /* XXX: really streamType is
222
     * only 6bit, followed by:
223
     * 1bit  upStream
224
     * 1bit  reserved
225
     */
226
    uint32_t buffer_size_db; // 24
227
    uint32_t max_bitrate;
228
    uint32_t avg_bitrate;
229
230
    /* 0x05 DecSpecificDescrTag */
231
    uint8_t  decoder_cfg_len;
232
    uint8_t *decoder_cfg;
233
234
    /* 0x06 SLConfigDescrTag */
235
    uint8_t  sl_config_len;
236
    uint8_t *sl_config;
237
} MOV_esds_t;
238
239 b6a17df4 Zdenek Kabelac
struct MOVParseTableEntry;
240
241 6cea494e Zdenek Kabelac
typedef struct MOVStreamContext {
242
    int ffindex; /* the ffmpeg stream id */
243
    long next_chunk;
244
    long chunk_count;
245 0c1a9eda Zdenek Kabelac
    int64_t *chunk_offsets;
246 cd461d48 Michael Niedermayer
    int stts_count;
247
    Time2Sample *stts_data;
248 c4ac052b Michael Niedermayer
    int ctts_count;
249
    Time2Sample *ctts_data;
250 cd461d48 Michael Niedermayer
    int edit_count;             /* number of 'edit' (elst atom) */
251 6cea494e Zdenek Kabelac
    long sample_to_chunk_sz;
252
    MOV_sample_to_chunk_tbl *sample_to_chunk;
253 c4ac052b Michael Niedermayer
    int sample_to_ctime_index;
254
    int sample_to_ctime_sample;
255 6cea494e Zdenek Kabelac
    long sample_size;
256
    long sample_count;
257
    long *sample_sizes;
258 247d56f5 Brian Becker
    long keyframe_count;
259
    long *keyframes;
260 5cd62665 Zdenek Kabelac
    int time_scale;
261 cd7352d5 Michael Niedermayer
    int time_rate;
262 3ffe3793 François Revol
    long current_sample;
263 5ca1d879 Zdenek Kabelac
    MOV_esds_t esds;
264 b72708f8 Baptiste Coudurier
    AVRational sample_size_v1;
265 b60c0454 Baptiste Coudurier
    int dv_audio_container;
266 6cea494e Zdenek Kabelac
} MOVStreamContext;
267
268
typedef struct MOVContext {
269
    AVFormatContext *fc;
270 5cd62665 Zdenek Kabelac
    int time_scale;
271 7e815047 Baptiste Coudurier
    int64_t duration; /* duration of the longest track */
272 6cea494e Zdenek Kabelac
    int found_moov; /* when both 'moov' and 'mdat' sections has been found */
273
    int found_mdat; /* we suppose we have enough data to read the file */
274 0c1a9eda Zdenek Kabelac
    int64_t mdat_size;
275
    int64_t mdat_offset;
276 6cea494e Zdenek Kabelac
    int total_streams;
277
    /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
278
     * but we need the info to be able to skip data from those streams in the 'mdat' section
279
     */
280
    MOVStreamContext *streams[MAX_STREAMS];
281 5cd62665 Zdenek Kabelac
282 5ca1d879 Zdenek Kabelac
    int ctab_size;
283
    MOV_ctab_t **ctab;           /* color tables */
284 b6a17df4 Zdenek Kabelac
    const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
285
    /* NOTE: for recursion save to/ restore from local variable! */
286 b595afaa Mike Melanson
287
    AVPaletteControl palette_control;
288 b72708f8 Baptiste Coudurier
    MOV_mdat_atom_t *mdat_list;
289
    int mdat_count;
290 b60c0454 Baptiste Coudurier
    DVDemuxContext *dv_demux;
291
    AVFormatContext *dv_fctx;
292 152e9a43 Baptiste Coudurier
    int isom; /* 1 if file is ISO Media (mp4/3gp) */
293 6cea494e Zdenek Kabelac
} MOVContext;
294
295
296
/* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
297
298
/* those functions parse an atom */
299
/* return code:
300
 1: found what I wanted, exit
301
 0: continue to parse next atom
302
 -1: error occured, exit
303
 */
304 5ca1d879 Zdenek Kabelac
typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
305 6cea494e Zdenek Kabelac
306
/* links atom IDs to parse functions */
307
typedef struct MOVParseTableEntry {
308 0c1a9eda Zdenek Kabelac
    uint32_t type;
309 6cea494e Zdenek Kabelac
    mov_parse_function func;
310
} MOVParseTableEntry;
311
312 5ca1d879 Zdenek Kabelac
static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
313 6cea494e Zdenek Kabelac
{
314 b6a17df4 Zdenek Kabelac
    int64_t total_size = 0;
315 5ca1d879 Zdenek Kabelac
    MOV_atom_t a;
316 6cea494e Zdenek Kabelac
    int i;
317
    int err = 0;
318 5ca1d879 Zdenek Kabelac
319
    a.offset = atom.offset;
320 6cea494e Zdenek Kabelac
321 9ed83b0a Zdenek Kabelac
    if (atom.size < 0)
322 bb270c08 Diego Biurrun
        atom.size = 0x7fffffffffffffffLL;
323 5ca1d879 Zdenek Kabelac
    while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
324 bb270c08 Diego Biurrun
        a.size = atom.size;
325
        a.type=0L;
326 5ca1d879 Zdenek Kabelac
        if(atom.size >= 8) {
327 bb270c08 Diego Biurrun
            a.size = get_be32(pb);
328 5ca1d879 Zdenek Kabelac
            a.type = get_le32(pb);
329 6cea494e Zdenek Kabelac
        }
330 bb270c08 Diego Biurrun
        total_size += 8;
331 5ca1d879 Zdenek Kabelac
        a.offset += 8;
332 6f2741f4 Baptiste Coudurier
        dprintf("type: %08x  %.4s  sz: %"PRIx64"  %"PRIx64"   %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size);
333 5ca1d879 Zdenek Kabelac
        if (a.size == 1) { /* 64 bit extended size */
334 bb270c08 Diego Biurrun
            a.size = get_be64(pb) - 8;
335 5ca1d879 Zdenek Kabelac
            a.offset += 8;
336
            total_size += 8;
337 6cea494e Zdenek Kabelac
        }
338 bb270c08 Diego Biurrun
        if (a.size == 0) {
339
            a.size = atom.size - total_size;
340
            if (a.size <= 8)
341 5cd62665 Zdenek Kabelac
                break;
342 bb270c08 Diego Biurrun
        }
343
        for (i = 0; c->parse_table[i].type != 0L
344
             && c->parse_table[i].type != a.type; i++)
345
            /* empty */;
346 5cd62665 Zdenek Kabelac
347 bb270c08 Diego Biurrun
        a.size -= 8;
348 115329f1 Diego Biurrun
349 568e18b1 Michael Niedermayer
        if(a.size < 0)
350
            break;
351 115329f1 Diego Biurrun
352 bb270c08 Diego Biurrun
        if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
353 5ca1d879 Zdenek Kabelac
            url_fskip(pb, a.size);
354 bb270c08 Diego Biurrun
        } else {
355 5c72cad8 Baptiste Coudurier
            offset_t start_pos = url_ftell(pb);
356
            int64_t left;
357 bb270c08 Diego Biurrun
            err = (c->parse_table[i].func)(c, pb, a);
358 5c72cad8 Baptiste Coudurier
            left = a.size - url_ftell(pb) + start_pos;
359
            if (left > 0) /* skip garbage at atom end */
360
                url_fskip(pb, left);
361 bb270c08 Diego Biurrun
        }
362 6cea494e Zdenek Kabelac
363 bb270c08 Diego Biurrun
        a.offset += a.size;
364 5ca1d879 Zdenek Kabelac
        total_size += a.size;
365 6cea494e Zdenek Kabelac
    }
366
367 5ca1d879 Zdenek Kabelac
    if (!err && total_size < atom.size && atom.size < 0x7ffff) {
368
        url_fskip(pb, atom.size - total_size);
369 5cd62665 Zdenek Kabelac
    }
370
371 6cea494e Zdenek Kabelac
    return err;
372
}
373
374 5ca1d879 Zdenek Kabelac
static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
375 6cea494e Zdenek Kabelac
{
376 568e18b1 Michael Niedermayer
#if 1
377
    url_fskip(pb, atom.size); // for now
378
#else
379
    VERY VERY BROKEN, NEVER execute this, needs rewrite
380 88730be6 Måns Rullgård
    unsigned int len;
381
    MOV_ctab_t *t;
382 5ca1d879 Zdenek Kabelac
    c->ctab = av_realloc(c->ctab, ++c->ctab_size);
383
    t = c->ctab[c->ctab_size];
384
    t->seed = get_be32(pb);
385
    t->flags = get_be16(pb);
386
    t->size = get_be16(pb) + 1;
387
    len = 2 * t->size * 4;
388
    if (len > 0) {
389 bb270c08 Diego Biurrun
        t->clrs = av_malloc(len); // 16bit A R G B
390
        if (t->clrs)
391
            get_buffer(pb, t->clrs, len);
392 3ffe3793 François Revol
    }
393 568e18b1 Michael Niedermayer
#endif
394 5cd62665 Zdenek Kabelac
395 0147f198 François Revol
    return 0;
396
}
397
398 5ca1d879 Zdenek Kabelac
static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
399 6cea494e Zdenek Kabelac
{
400 5ca1d879 Zdenek Kabelac
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
401 0c1a9eda Zdenek Kabelac
    uint32_t type;
402
    uint32_t ctype;
403 b6a17df4 Zdenek Kabelac
404 6cea494e Zdenek Kabelac
    get_byte(pb); /* version */
405
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
406
407
    /* component type */
408
    ctype = get_le32(pb);
409
    type = get_le32(pb); /* component subtype */
410
411 4e5ef14f Baptiste Coudurier
    dprintf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
412
    dprintf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
413 152e9a43 Baptiste Coudurier
    if(!ctype)
414
        c->isom = 1;
415 73920f07 Baptiste Coudurier
    if(type == MKTAG('v', 'i', 'd', 'e'))
416
        st->codec->codec_type = CODEC_TYPE_VIDEO;
417
    else if(type == MKTAG('s', 'o', 'u', 'n'))
418
        st->codec->codec_type = CODEC_TYPE_AUDIO;
419 95a07973 Baptiste Coudurier
    else if(type == MKTAG('m', '1', 'a', ' '))
420
        st->codec->codec_id = CODEC_ID_MP2;
421 8cb97693 Baptiste Coudurier
    else if(type == MKTAG('s', 'u', 'b', 'p')) {
422
        st->codec->codec_type = CODEC_TYPE_SUBTITLE;
423
        st->codec->codec_id = CODEC_ID_DVD_SUBTITLE;
424
    }
425 6cea494e Zdenek Kabelac
    get_be32(pb); /* component  manufacture */
426
    get_be32(pb); /* component flags */
427
    get_be32(pb); /* component flags mask */
428
429 5ca1d879 Zdenek Kabelac
    if(atom.size <= 24)
430 6cea494e Zdenek Kabelac
        return 0; /* nothing left to read */
431 5cd62665 Zdenek Kabelac
432 3c13647a Cedric Vincent
    url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
433 6cea494e Zdenek Kabelac
    return 0;
434
}
435
436 5ca1d879 Zdenek Kabelac
static int mov_mp4_read_descr_len(ByteIOContext *pb)
437 0e7eed09 Fabrice Bellard
{
438 5cd62665 Zdenek Kabelac
    int len = 0;
439 5ca1d879 Zdenek Kabelac
    int count = 4;
440
    while (count--) {
441 5cd62665 Zdenek Kabelac
        int c = get_byte(pb);
442 bb270c08 Diego Biurrun
        len = (len << 7) | (c & 0x7f);
443
        if (!(c & 0x80))
444
            break;
445 0e7eed09 Fabrice Bellard
    }
446
    return len;
447
}
448
449 5ca1d879 Zdenek Kabelac
static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
450 0e7eed09 Fabrice Bellard
{
451
    int len;
452
    *tag = get_byte(pb);
453 5ca1d879 Zdenek Kabelac
    len = mov_mp4_read_descr_len(pb);
454 4e5ef14f Baptiste Coudurier
    dprintf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
455 0e7eed09 Fabrice Bellard
    return len;
456
}
457
458 5ca1d879 Zdenek Kabelac
static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
459 5cd62665 Zdenek Kabelac
{
460
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
461
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
462
    int tag, len;
463 b6a17df4 Zdenek Kabelac
464 5cd62665 Zdenek Kabelac
    /* Well, broken but suffisant for some MP4 streams */
465
    get_be32(pb); /* version + flags */
466 5ca1d879 Zdenek Kabelac
    len = mov_mp4_read_descr(pb, &tag);
467 5cd62665 Zdenek Kabelac
    if (tag == MP4ESDescrTag) {
468 bb270c08 Diego Biurrun
        get_be16(pb); /* ID */
469
        get_byte(pb); /* priority */
470 5cd62665 Zdenek Kabelac
    } else
471 bb270c08 Diego Biurrun
        get_be16(pb); /* ID */
472 5cd62665 Zdenek Kabelac
473 5ca1d879 Zdenek Kabelac
    len = mov_mp4_read_descr(pb, &tag);
474 5cd62665 Zdenek Kabelac
    if (tag == MP4DecConfigDescrTag) {
475 bb270c08 Diego Biurrun
        sc->esds.object_type_id = get_byte(pb);
476
        sc->esds.stream_type = get_byte(pb);
477
        sc->esds.buffer_size_db = get_be24(pb);
478
        sc->esds.max_bitrate = get_be32(pb);
479
        sc->esds.avg_bitrate = get_be32(pb);
480
481 e60b4ced Baptiste Coudurier
        st->codec->codec_id= codec_get_id(ff_mov_obj_type, sc->esds.object_type_id);
482 986c2ccb Baptiste Coudurier
        dprintf("esds object type id %d\n", sc->esds.object_type_id);
483 bb270c08 Diego Biurrun
        len = mov_mp4_read_descr(pb, &tag);
484
        if (tag == MP4DecSpecificDescrTag) {
485 4e5ef14f Baptiste Coudurier
            dprintf("Specific MPEG4 header len=%d\n", len);
486 9a630c25 Baptiste Coudurier
            st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
487 bb270c08 Diego Biurrun
            if (st->codec->extradata) {
488
                get_buffer(pb, st->codec->extradata, len);
489
                st->codec->extradata_size = len;
490 e31bd3e3 Baptiste Coudurier
                /* from mplayer */
491 b95d44b6 Baptiste Coudurier
                if ((*st->codec->extradata >> 3) == 29) {
492 e31bd3e3 Baptiste Coudurier
                    st->codec->codec_id = CODEC_ID_MP3ON4;
493
                }
494 bb270c08 Diego Biurrun
            }
495
        }
496 5cd62665 Zdenek Kabelac
    }
497
    return 0;
498
}
499
500 5ca1d879 Zdenek Kabelac
/* this atom contains actual media data */
501
static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
502 6cea494e Zdenek Kabelac
{
503 5ca1d879 Zdenek Kabelac
    if(atom.size == 0) /* wrong one (MP4) */
504
        return 0;
505 b72708f8 Baptiste Coudurier
    c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
506
    c->mdat_list[c->mdat_count].offset = atom.offset;
507
    c->mdat_list[c->mdat_count].size = atom.size;
508
    c->mdat_count++;
509 5ca1d879 Zdenek Kabelac
    c->found_mdat=1;
510
    c->mdat_offset = atom.offset;
511
    c->mdat_size = atom.size;
512
    if(c->found_moov)
513
        return 1; /* found both, just go */
514
    url_fskip(pb, atom.size);
515
    return 0; /* now go for moov */
516
}
517
518 4ea28253 Baptiste Coudurier
static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
519
{
520
    uint32_t type = get_le32(pb);
521
522 152e9a43 Baptiste Coudurier
    if (type != MKTAG('q','t',' ',' '))
523
        c->isom = 1;
524 a512446e Baptiste Coudurier
    av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
525 4ea28253 Baptiste Coudurier
    get_be32(pb); /* minor version */
526
    url_fskip(pb, atom.size - 8);
527
    return 0;
528
}
529
530 5ca1d879 Zdenek Kabelac
/* this atom should contain all header atoms */
531
static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
532
{
533
    int err;
534 b6a17df4 Zdenek Kabelac
535 5ca1d879 Zdenek Kabelac
    err = mov_read_default(c, pb, atom);
536
    /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
537
    /* so we don't parse the whole file if over a network */
538
    c->found_moov=1;
539
    if(c->found_mdat)
540
        return 1; /* found both, just go */
541
    return 0; /* now go for mdat */
542
}
543
544
545
static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
546
{
547 f444b977 Baptiste Coudurier
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
548
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
549
    int version = get_byte(pb);
550 b9a87c4d François Revol
    int lang;
551 5ca1d879 Zdenek Kabelac
552 b9a87c4d François Revol
    if (version > 1)
553
        return 1; /* unsupported */
554 5ca1d879 Zdenek Kabelac
555
    get_byte(pb); get_byte(pb);
556
    get_byte(pb); /* flags */
557
558 f444b977 Baptiste Coudurier
    if (version == 1) {
559
        get_be64(pb);
560
        get_be64(pb);
561
    } else {
562
        get_be32(pb); /* creation time */
563
        get_be32(pb); /* modification time */
564
    }
565 5ca1d879 Zdenek Kabelac
566 f444b977 Baptiste Coudurier
    sc->time_scale = get_be32(pb);
567
    st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
568 5ca1d879 Zdenek Kabelac
569 b9a87c4d François Revol
    lang = get_be16(pb); /* language */
570 f444b977 Baptiste Coudurier
    ff_mov_lang_to_iso639(lang, st->language);
571 5ca1d879 Zdenek Kabelac
    get_be16(pb); /* quality */
572
573
    return 0;
574
}
575
576
static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
577
{
578 1175561e Baptiste Coudurier
    int version = get_byte(pb); /* version */
579 5ca1d879 Zdenek Kabelac
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
580
581 1175561e Baptiste Coudurier
    if (version == 1) {
582
        get_be64(pb);
583
        get_be64(pb);
584
    } else {
585
        get_be32(pb); /* creation time */
586
        get_be32(pb); /* modification time */
587
    }
588 5ca1d879 Zdenek Kabelac
    c->time_scale = get_be32(pb); /* time scale */
589
#ifdef DEBUG
590 baf25c9d Gael Chardon
    av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
591 5ca1d879 Zdenek Kabelac
#endif
592 1175561e Baptiste Coudurier
    c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
593 5ca1d879 Zdenek Kabelac
    get_be32(pb); /* preferred scale */
594
595
    get_be16(pb); /* preferred volume */
596
597
    url_fskip(pb, 10); /* reserved */
598
599
    url_fskip(pb, 36); /* display matrix */
600
601
    get_be32(pb); /* preview time */
602
    get_be32(pb); /* preview duration */
603
    get_be32(pb); /* poster time */
604
    get_be32(pb); /* selection time */
605
    get_be32(pb); /* selection duration */
606
    get_be32(pb); /* current time */
607
    get_be32(pb); /* next track ID */
608
609
    return 0;
610
}
611
612 9ed83b0a Zdenek Kabelac
static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
613
{
614
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
615
616 568e18b1 Michael Niedermayer
    if((uint64_t)atom.size > (1<<30))
617
        return -1;
618 115329f1 Diego Biurrun
619 9ed83b0a Zdenek Kabelac
    // currently SVQ3 decoder expect full STSD header - so let's fake it
620
    // this should be fixed and just SMI header should be passed
621 01f4895c Michael Niedermayer
    av_free(st->codec->extradata);
622
    st->codec->extradata_size = 0x5a + atom.size;
623 9a630c25 Baptiste Coudurier
    st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
624 01f4895c Michael Niedermayer
625
    if (st->codec->extradata) {
626 bb270c08 Diego Biurrun
        strcpy(st->codec->extradata, "SVQ3"); // fake
627 b95d44b6 Baptiste Coudurier
        get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
628
        dprintf("Reading SMI %"PRId64"  %s\n", atom.size, st->codec->extradata + 0x5a);
629 9ed83b0a Zdenek Kabelac
    } else
630 bb270c08 Diego Biurrun
        url_fskip(pb, atom.size);
631 9ed83b0a Zdenek Kabelac
632
    return 0;
633
}
634 5ca1d879 Zdenek Kabelac
635 de23f234 Baptiste Coudurier
static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
636
{
637
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
638
    int little_endian = get_be16(pb);
639
640
    if (little_endian) {
641
        switch (st->codec->codec_id) {
642
        case CODEC_ID_PCM_S24BE:
643
            st->codec->codec_id = CODEC_ID_PCM_S24LE;
644
            break;
645
        case CODEC_ID_PCM_S32BE:
646
            st->codec->codec_id = CODEC_ID_PCM_S32LE;
647
            break;
648
        default:
649
            break;
650
        }
651
    }
652
    return 0;
653
}
654
655
static int mov_read_alac(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
656
{
657
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
658
659
    // currently ALAC decoder expect full atom header - so let's fake it
660
    // this should be fixed and just ALAC header should be passed
661
662
    av_free(st->codec->extradata);
663
    st->codec->extradata_size = 36;
664 9a630c25 Baptiste Coudurier
    st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
665 de23f234 Baptiste Coudurier
666
    if (st->codec->extradata) {
667 b95d44b6 Baptiste Coudurier
        strcpy(st->codec->extradata + 4, "alac"); // fake
668
        get_buffer(pb, st->codec->extradata + 8, 36 - 8);
669
        dprintf("Reading alac %d  %s\n", st->codec->extradata_size, st->codec->extradata);
670 de23f234 Baptiste Coudurier
    } else
671
        url_fskip(pb, atom.size);
672
    return 0;
673
}
674
675 d9b1c197 Roberto Togni
static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
676
{
677
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
678
679
    if((uint64_t)atom.size > (1<<30))
680
        return -1;
681 115329f1 Diego Biurrun
682 3840147e Michael Niedermayer
    if (st->codec->codec_id == CODEC_ID_QDM2) {
683
        // pass all frma atom to codec, needed at least for QDM2
684
        av_free(st->codec->extradata);
685
        st->codec->extradata_size = atom.size;
686 9a630c25 Baptiste Coudurier
        st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
687 3840147e Michael Niedermayer
688
        if (st->codec->extradata) {
689
            get_buffer(pb, st->codec->extradata, atom.size);
690
        } else
691
            url_fskip(pb, atom.size);
692
    } else if (atom.size > 8) { /* to read frma, esds atoms */
693
        mov_read_default(c, pb, atom);
694 5c72cad8 Baptiste Coudurier
    } else
695 bb270c08 Diego Biurrun
        url_fskip(pb, atom.size);
696 d9b1c197 Roberto Togni
    return 0;
697
}
698
699 fc5188f3 Baptiste Coudurier
static int mov_read_jp2h(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
700
{
701
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
702
703
    if((uint64_t)atom.size > (1<<30))
704
        return -1;
705
706
    av_free(st->codec->extradata);
707
708
    st->codec->extradata_size = atom.size + 8;
709 9a630c25 Baptiste Coudurier
    st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
710 fc5188f3 Baptiste Coudurier
711
    /* pass all jp2h atom to codec */
712
    if (st->codec->extradata) {
713 b95d44b6 Baptiste Coudurier
        strcpy(st->codec->extradata + 4, "jp2h");
714
        get_buffer(pb, st->codec->extradata + 8, atom.size);
715 fc5188f3 Baptiste Coudurier
    } else
716
        url_fskip(pb, atom.size);
717
    return 0;
718
}
719
720 169eb021 Mike Melanson
static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
721
{
722
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
723
724 568e18b1 Michael Niedermayer
    if((uint64_t)atom.size > (1<<30))
725
        return -1;
726
727 01f4895c Michael Niedermayer
    av_free(st->codec->extradata);
728 169eb021 Mike Melanson
729 01f4895c Michael Niedermayer
    st->codec->extradata_size = atom.size;
730 9a630c25 Baptiste Coudurier
    st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
731 169eb021 Mike Melanson
732 01f4895c Michael Niedermayer
    if (st->codec->extradata) {
733 bb270c08 Diego Biurrun
        get_buffer(pb, st->codec->extradata, atom.size);
734 169eb021 Mike Melanson
    } else
735 bb270c08 Diego Biurrun
        url_fskip(pb, atom.size);
736 169eb021 Mike Melanson
737
    return 0;
738
}
739
740 5ca1d879 Zdenek Kabelac
static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
741
{
742
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
743
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
744 568e18b1 Michael Niedermayer
    unsigned int i, entries;
745 5ca1d879 Zdenek Kabelac
746
    get_byte(pb); /* version */
747
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
748
749
    entries = get_be32(pb);
750 115329f1 Diego Biurrun
751 568e18b1 Michael Niedermayer
    if(entries >= UINT_MAX/sizeof(int64_t))
752
        return -1;
753 115329f1 Diego Biurrun
754 5ca1d879 Zdenek Kabelac
    sc->chunk_count = entries;
755 9a630c25 Baptiste Coudurier
    sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
756 5ca1d879 Zdenek Kabelac
    if (!sc->chunk_offsets)
757
        return -1;
758
    if (atom.type == MKTAG('s', 't', 'c', 'o')) {
759
        for(i=0; i<entries; i++) {
760
            sc->chunk_offsets[i] = get_be32(pb);
761
        }
762
    } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
763
        for(i=0; i<entries; i++) {
764
            sc->chunk_offsets[i] = get_be64(pb);
765
        }
766
    } else
767
        return -1;
768 115329f1 Diego Biurrun
769 5ca1d879 Zdenek Kabelac
    return 0;
770
}
771
772
static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
773
{
774
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
775 302c389e Michael Niedermayer
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
776 5ca1d879 Zdenek Kabelac
    int entries, frames_per_sample;
777
    uint32_t format;
778 53ffdd14 Roine Gustafsson
    uint8_t codec_name[32];
779 5ca1d879 Zdenek Kabelac
780 b595afaa Mike Melanson
    /* for palette traversal */
781
    int color_depth;
782
    int color_start;
783
    int color_count;
784
    int color_end;
785
    int color_index;
786
    int color_dec;
787
    int color_greyscale;
788
    unsigned char *color_table;
789
    int j;
790
    unsigned char r, g, b;
791
792 6cea494e Zdenek Kabelac
    get_byte(pb); /* version */
793
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
794
795
    entries = get_be32(pb);
796
797 891f64b3 joca@rixmail.se
    while(entries--) { //Parsing Sample description table
798 b6a17df4 Zdenek Kabelac
        enum CodecID id;
799 de23f234 Baptiste Coudurier
        MOV_atom_t a = { 0, 0, 0 };
800 3840147e Michael Niedermayer
        offset_t start_pos = url_ftell(pb);
801 bb270c08 Diego Biurrun
        int size = get_be32(pb); /* size */
802 6cea494e Zdenek Kabelac
        format = get_le32(pb); /* data format */
803 5cd62665 Zdenek Kabelac
804 6cea494e Zdenek Kabelac
        get_be32(pb); /* reserved */
805
        get_be16(pb); /* reserved */
806
        get_be16(pb); /* index */
807 0e7eed09 Fabrice Bellard
808 e7cc4b52 Baptiste Coudurier
        if (st->codec->codec_tag) {
809
            /* multiple fourcc, just skip for now */
810
            url_fskip(pb, size - (url_ftell(pb) - start_pos));
811
            continue;
812
        }
813
814 bb270c08 Diego Biurrun
        st->codec->codec_tag = format;
815 99487f42 Baptiste Coudurier
        id = codec_get_id(mov_audio_tags, format);
816 48855b26 Baptiste Coudurier
        if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
817 99487f42 Baptiste Coudurier
            st->codec->codec_type = CODEC_TYPE_AUDIO;
818 48855b26 Baptiste Coudurier
        } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
819
                   format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
820 de23f234 Baptiste Coudurier
            id = codec_get_id(mov_video_tags, format);
821 99487f42 Baptiste Coudurier
            if (id <= 0)
822 de23f234 Baptiste Coudurier
                id = codec_get_id(codec_bmp_tags, format);
823 99487f42 Baptiste Coudurier
            if (id > 0)
824
                st->codec->codec_type = CODEC_TYPE_VIDEO;
825
        }
826
827 a20da52c Baptiste Coudurier
        dprintf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
828
                size,
829
                (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff,
830
                st->codec->codec_type);
831
832 99487f42 Baptiste Coudurier
        if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
833 01f4895c Michael Niedermayer
            st->codec->codec_id = id;
834 6cea494e Zdenek Kabelac
            get_be16(pb); /* version */
835
            get_be16(pb); /* revision level */
836
            get_be32(pb); /* vendor */
837
            get_be32(pb); /* temporal quality */
838
            get_be32(pb); /* spacial quality */
839 3cb4ee51 Baptiste Coudurier
840
            st->codec->width = get_be16(pb); /* width */
841
            st->codec->height = get_be16(pb); /* height */
842
843 6cea494e Zdenek Kabelac
            get_be32(pb); /* horiz resolution */
844
            get_be32(pb); /* vert resolution */
845
            get_be32(pb); /* data size, always 0 */
846 5cd62665 Zdenek Kabelac
            frames_per_sample = get_be16(pb); /* frames per samples */
847 6cea494e Zdenek Kabelac
#ifdef DEBUG
848 bb270c08 Diego Biurrun
            av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
849 6cea494e Zdenek Kabelac
#endif
850 576f1445 Baptiste Coudurier
            get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
851
            if (codec_name[0] <= 31) {
852
                memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
853
                st->codec->codec_name[codec_name[0]] = 0;
854
            }
855 5cd62665 Zdenek Kabelac
856 bb270c08 Diego Biurrun
            st->codec->bits_per_sample = get_be16(pb); /* depth */
857 01f4895c Michael Niedermayer
            st->codec->color_table_id = get_be16(pb); /* colortable id */
858 6cea494e Zdenek Kabelac
859 b595afaa Mike Melanson
            /* figure out the palette situation */
860 01f4895c Michael Niedermayer
            color_depth = st->codec->bits_per_sample & 0x1F;
861
            color_greyscale = st->codec->bits_per_sample & 0x20;
862 b595afaa Mike Melanson
863
            /* if the depth is 2, 4, or 8 bpp, file is palettized */
864 115329f1 Diego Biurrun
            if ((color_depth == 2) || (color_depth == 4) ||
865 b595afaa Mike Melanson
                (color_depth == 8)) {
866
867
                if (color_greyscale) {
868
869
                    /* compute the greyscale palette */
870
                    color_count = 1 << color_depth;
871
                    color_index = 255;
872
                    color_dec = 256 / (color_count - 1);
873
                    for (j = 0; j < color_count; j++) {
874
                        r = g = b = color_index;
875
                        c->palette_control.palette[j] =
876
                            (r << 16) | (g << 8) | (b);
877
                        color_index -= color_dec;
878
                        if (color_index < 0)
879
                            color_index = 0;
880
                    }
881
882 01f4895c Michael Niedermayer
                } else if (st->codec->color_table_id & 0x08) {
883 b595afaa Mike Melanson
884
                    /* if flag bit 3 is set, use the default palette */
885
                    color_count = 1 << color_depth;
886
                    if (color_depth == 2)
887 a90466f7 Michael Niedermayer
                        color_table = ff_qt_default_palette_4;
888 b595afaa Mike Melanson
                    else if (color_depth == 4)
889 a90466f7 Michael Niedermayer
                        color_table = ff_qt_default_palette_16;
890 b595afaa Mike Melanson
                    else
891 a90466f7 Michael Niedermayer
                        color_table = ff_qt_default_palette_256;
892 b595afaa Mike Melanson
893
                    for (j = 0; j < color_count; j++) {
894
                        r = color_table[j * 4 + 0];
895
                        g = color_table[j * 4 + 1];
896
                        b = color_table[j * 4 + 2];
897
                        c->palette_control.palette[j] =
898
                            (r << 16) | (g << 8) | (b);
899
                    }
900
901
                } else {
902
903
                    /* load the palette from the file */
904
                    color_start = get_be32(pb);
905
                    color_count = get_be16(pb);
906
                    color_end = get_be16(pb);
907
                    for (j = color_start; j <= color_end; j++) {
908
                        /* each R, G, or B component is 16 bits;
909
                         * only use the top 8 bits; skip alpha bytes
910
                         * up front */
911
                        get_byte(pb);
912
                        get_byte(pb);
913
                        r = get_byte(pb);
914
                        get_byte(pb);
915
                        g = get_byte(pb);
916
                        get_byte(pb);
917
                        b = get_byte(pb);
918
                        get_byte(pb);
919
                        c->palette_control.palette[j] =
920
                            (r << 16) | (g << 8) | (b);
921
                    }
922
                }
923
924 01f4895c Michael Niedermayer
                st->codec->palctrl = &c->palette_control;
925
                st->codec->palctrl->palette_changed = 1;
926 b595afaa Mike Melanson
            } else
927 01f4895c Michael Niedermayer
                st->codec->palctrl = NULL;
928 de23f234 Baptiste Coudurier
        } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
929 b72708f8 Baptiste Coudurier
            int bits_per_sample;
930 de23f234 Baptiste Coudurier
            uint16_t version = get_be16(pb);
931 b595afaa Mike Melanson
932 99487f42 Baptiste Coudurier
            st->codec->codec_id = id;
933 de23f234 Baptiste Coudurier
            get_be16(pb); /* revision level */
934
            get_be32(pb); /* vendor */
935 6d6d7970 Mike Melanson
936 de23f234 Baptiste Coudurier
            st->codec->channels = get_be16(pb);             /* channel count */
937 986c2ccb Baptiste Coudurier
            dprintf("audio channels %d\n", st->codec->channels);
938 de23f234 Baptiste Coudurier
            st->codec->bits_per_sample = get_be16(pb);      /* sample size */
939
            /* do we need to force to 16 for AMR ? */
940
941
            /* handle specific s8 codec */
942
            get_be16(pb); /* compression id = 0*/
943
            get_be16(pb); /* packet size = 0 */
944
945
            st->codec->sample_rate = ((get_be32(pb) >> 16));
946
947
            switch (st->codec->codec_id) {
948 b72708f8 Baptiste Coudurier
            case CODEC_ID_PCM_S8:
949
            case CODEC_ID_PCM_U8:
950
                if (st->codec->bits_per_sample == 16)
951
                    st->codec->codec_id = CODEC_ID_PCM_S16BE;
952
                break;
953 0dd39bfe Baptiste Coudurier
            case CODEC_ID_PCM_S16LE:
954 de23f234 Baptiste Coudurier
            case CODEC_ID_PCM_S16BE:
955
                if (st->codec->bits_per_sample == 8)
956
                    st->codec->codec_id = CODEC_ID_PCM_S8;
957 0299a87c Baptiste Coudurier
                else if (st->codec->bits_per_sample == 24)
958
                    st->codec->codec_id = CODEC_ID_PCM_S24BE;
959 de23f234 Baptiste Coudurier
                break;
960
            default:
961
                break;
962 6d6d7970 Mike Melanson
            }
963 14342fd5 Johannes Carlsson
964 de23f234 Baptiste Coudurier
            //Read QT version 1 fields. In version 0 theese dont exist
965 152e9a43 Baptiste Coudurier
            dprintf("version =%d, isom =%d\n",version,c->isom);
966
            if(!c->isom) {
967 80c5b9a1 Baptiste Coudurier
                if(version==1) {
968
                    sc->sample_size_v1.den = get_be32(pb); /* samples per packet */
969
                    get_be32(pb); /* bytes per packet */
970
                    sc->sample_size_v1.num = get_be32(pb); /* bytes per frame */
971
                    get_be32(pb); /* bytes per sample */
972
                } else if(version==2) {
973
                    get_be32(pb); /* sizeof struct only */
974
                    st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
975
                    st->codec->channels = get_be32(pb);
976
                    get_be32(pb); /* always 0x7F000000 */
977
                    get_be32(pb); /* bits per channel if sound is uncompressed */
978
                    get_be32(pb); /* lcpm format specific flag */
979
                    get_be32(pb); /* bytes per audio packet if constant */
980
                    get_be32(pb); /* lpcm frames per audio packet if constant */
981
                }
982 152e9a43 Baptiste Coudurier
            }
983 9770089d Baptiste Coudurier
984
            bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
985
            if (bits_per_sample) {
986
                st->codec->bits_per_sample = bits_per_sample;
987
                sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
988
            }
989 de23f234 Baptiste Coudurier
        } else {
990
            /* other codec type, just skip (rtp, mp4s, tmcd ...) */
991
            url_fskip(pb, size - (url_ftell(pb) - start_pos));
992 6cea494e Zdenek Kabelac
        }
993 de23f234 Baptiste Coudurier
        /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
994
        a.size = size - (url_ftell(pb) - start_pos);
995
        if (a.size > 8)
996
            mov_read_default(c, pb, a);
997
        else if (a.size > 0)
998
            url_fskip(pb, a.size);
999 6cea494e Zdenek Kabelac
    }
1000 115329f1 Diego Biurrun
1001 302c389e Michael Niedermayer
    if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) {
1002
        st->codec->sample_rate= sc->time_scale;
1003
    }
1004 e31bd3e3 Baptiste Coudurier
1005 d00f8e17 Baptiste Coudurier
    /* special codec parameters handling */
1006 e31bd3e3 Baptiste Coudurier
    switch (st->codec->codec_id) {
1007 d00f8e17 Baptiste Coudurier
#ifdef CONFIG_H261_DECODER
1008
    case CODEC_ID_H261:
1009
#endif
1010
#ifdef CONFIG_H263_DECODER
1011
    case CODEC_ID_H263:
1012
#endif
1013
#ifdef CONFIG_MPEG4_DECODER
1014
    case CODEC_ID_MPEG4:
1015
#endif
1016
        st->codec->width= 0; /* let decoder init width/height */
1017
        st->codec->height= 0;
1018
        break;
1019 2eed5a46 Diego Biurrun
#ifdef CONFIG_LIBFAAD
1020 e31bd3e3 Baptiste Coudurier
    case CODEC_ID_AAC:
1021 ec04d244 Baptiste Coudurier
#endif
1022 3ed73c46 Baptiste Coudurier
#ifdef CONFIG_VORBIS_DECODER
1023
    case CODEC_ID_VORBIS:
1024
#endif
1025 e31bd3e3 Baptiste Coudurier
    case CODEC_ID_MP3ON4:
1026
        st->codec->sample_rate= 0; /* let decoder init parameters properly */
1027
        break;
1028 989ac5a6 Baptiste Coudurier
#ifdef CONFIG_DV_DEMUXER
1029 b60c0454 Baptiste Coudurier
    case CODEC_ID_DVAUDIO:
1030
        c->dv_fctx = av_alloc_format_context();
1031
        c->dv_demux = dv_init_demux(c->dv_fctx);
1032
        if (!c->dv_demux) {
1033
            av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
1034
            return -1;
1035
        }
1036
        sc->dv_audio_container = 1;
1037
        st->codec->codec_id = CODEC_ID_PCM_S16LE;
1038
        break;
1039 989ac5a6 Baptiste Coudurier
#endif
1040 b95319a2 Baptiste Coudurier
    /* no ifdef since parameters are always those */
1041
    case CODEC_ID_AMR_WB:
1042
        st->codec->sample_rate= 16000;
1043
        st->codec->channels= 1; /* really needed */
1044
        break;
1045
    case CODEC_ID_AMR_NB:
1046
        st->codec->sample_rate= 8000;
1047
        st->codec->channels= 1; /* really needed */
1048
        break;
1049 a41104f8 Michael Niedermayer
    case CODEC_ID_MP2:
1050 95a07973 Baptiste Coudurier
        st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
1051 a41104f8 Michael Niedermayer
        st->need_parsing = 1;
1052
        break;
1053 e31bd3e3 Baptiste Coudurier
    default:
1054
        break;
1055
    }
1056 5cd62665 Zdenek Kabelac
1057 6cea494e Zdenek Kabelac
    return 0;
1058
}
1059
1060 5ca1d879 Zdenek Kabelac
static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1061 6cea494e Zdenek Kabelac
{
1062 5ca1d879 Zdenek Kabelac
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1063
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1064 568e18b1 Michael Niedermayer
    unsigned int i, entries;
1065 b6a17df4 Zdenek Kabelac
1066 6cea494e Zdenek Kabelac
    get_byte(pb); /* version */
1067
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1068
1069
    entries = get_be32(pb);
1070 115329f1 Diego Biurrun
1071 568e18b1 Michael Niedermayer
    if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl))
1072
        return -1;
1073 115329f1 Diego Biurrun
1074 3ffe3793 François Revol
#ifdef DEBUG
1075 baf25c9d Gael Chardon
av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1076 3ffe3793 François Revol
#endif
1077 6cea494e Zdenek Kabelac
    sc->sample_to_chunk_sz = entries;
1078 9a630c25 Baptiste Coudurier
    sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
1079 b6a17df4 Zdenek Kabelac
    if (!sc->sample_to_chunk)
1080
        return -1;
1081 6cea494e Zdenek Kabelac
    for(i=0; i<entries; i++) {
1082
        sc->sample_to_chunk[i].first = get_be32(pb);
1083
        sc->sample_to_chunk[i].count = get_be32(pb);
1084
        sc->sample_to_chunk[i].id = get_be32(pb);
1085
    }
1086
    return 0;
1087
}
1088
1089 247d56f5 Brian Becker
static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1090
{
1091
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1092
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1093 568e18b1 Michael Niedermayer
    unsigned int i, entries;
1094 247d56f5 Brian Becker
1095
    get_byte(pb); /* version */
1096
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1097
1098
    entries = get_be32(pb);
1099 115329f1 Diego Biurrun
1100 568e18b1 Michael Niedermayer
    if(entries >= UINT_MAX / sizeof(long))
1101
        return -1;
1102 115329f1 Diego Biurrun
1103 247d56f5 Brian Becker
    sc->keyframe_count = entries;
1104
#ifdef DEBUG
1105
    av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %ld\n", sc->keyframe_count);
1106
#endif
1107 9a630c25 Baptiste Coudurier
    sc->keyframes = av_malloc(entries * sizeof(long));
1108 247d56f5 Brian Becker
    if (!sc->keyframes)
1109
        return -1;
1110
    for(i=0; i<entries; i++) {
1111
        sc->keyframes[i] = get_be32(pb);
1112
#ifdef DEBUG
1113
/*        av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
1114
#endif
1115
    }
1116
    return 0;
1117
}
1118
1119 5ca1d879 Zdenek Kabelac
static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1120 6cea494e Zdenek Kabelac
{
1121 5ca1d879 Zdenek Kabelac
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1122
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1123 b72708f8 Baptiste Coudurier
    unsigned int i, entries, sample_size;
1124 b6a17df4 Zdenek Kabelac
1125 6cea494e Zdenek Kabelac
    get_byte(pb); /* version */
1126
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1127 5cd62665 Zdenek Kabelac
1128 b72708f8 Baptiste Coudurier
    sample_size = get_be32(pb);
1129
    if (!sc->sample_size) /* do not overwrite value computed in stsd */
1130
        sc->sample_size = sample_size;
1131 6cea494e Zdenek Kabelac
    entries = get_be32(pb);
1132 568e18b1 Michael Niedermayer
    if(entries >= UINT_MAX / sizeof(long))
1133
        return -1;
1134
1135 6cea494e Zdenek Kabelac
    sc->sample_count = entries;
1136 b72708f8 Baptiste Coudurier
    if (sample_size)
1137
        return 0;
1138
1139 0147f198 François Revol
#ifdef DEBUG
1140 baf25c9d Gael Chardon
    av_log(NULL, AV_LOG_DEBUG, "sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
1141 0147f198 François Revol
#endif
1142 9a630c25 Baptiste Coudurier
    sc->sample_sizes = av_malloc(entries * sizeof(long));
1143 b6a17df4 Zdenek Kabelac
    if (!sc->sample_sizes)
1144
        return -1;
1145 6cea494e Zdenek Kabelac
    for(i=0; i<entries; i++) {
1146
        sc->sample_sizes[i] = get_be32(pb);
1147
#ifdef DEBUG
1148 7dbab4a9 François Revol
        av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]);
1149 6cea494e Zdenek Kabelac
#endif
1150
    }
1151
    return 0;
1152
}
1153
1154 5ca1d879 Zdenek Kabelac
static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1155 0147f198 François Revol
{
1156 5ca1d879 Zdenek Kabelac
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1157 cd7352d5 Michael Niedermayer
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1158 568e18b1 Michael Niedermayer
    unsigned int i, entries;
1159 d957696f Michael Niedermayer
    int64_t duration=0;
1160
    int64_t total_sample_count=0;
1161 b6a17df4 Zdenek Kabelac
1162 0147f198 François Revol
    get_byte(pb); /* version */
1163
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1164
    entries = get_be32(pb);
1165 cd461d48 Michael Niedermayer
    if(entries >= UINT_MAX / sizeof(Time2Sample))
1166 568e18b1 Michael Niedermayer
        return -1;
1167 891f64b3 joca@rixmail.se
1168 cd7352d5 Michael Niedermayer
    sc->stts_count = entries;
1169
    sc->stts_data = av_malloc(entries * sizeof(Time2Sample));
1170 891f64b3 joca@rixmail.se
1171 3ffe3793 François Revol
#ifdef DEBUG
1172 baf25c9d Gael Chardon
av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1173 3ffe3793 François Revol
#endif
1174 cd7352d5 Michael Niedermayer
1175
    sc->time_rate=0;
1176
1177 0147f198 François Revol
    for(i=0; i<entries; i++) {
1178 cd461d48 Michael Niedermayer
        int sample_duration;
1179
        int sample_count;
1180 0147f198 François Revol
1181 891f64b3 joca@rixmail.se
        sample_count=get_be32(pb);
1182 0147f198 François Revol
        sample_duration = get_be32(pb);
1183 cd7352d5 Michael Niedermayer
        sc->stts_data[i].count= sample_count;
1184
        sc->stts_data[i].duration= sample_duration;
1185
1186
        sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
1187 961e0ccd Michael Niedermayer
1188 4e5ef14f Baptiste Coudurier
        dprintf("sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
1189
1190 7e815047 Baptiste Coudurier
        duration+=(int64_t)sample_duration*sample_count;
1191 891f64b3 joca@rixmail.se
        total_sample_count+=sample_count;
1192
    }
1193
1194 961e0ccd Michael Niedermayer
    st->nb_frames= total_sample_count;
1195
    if(duration)
1196
        st->duration= duration;
1197 0147f198 François Revol
    return 0;
1198
}
1199
1200 c4ac052b Michael Niedermayer
static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1201
{
1202 70a61ed4 Michael Niedermayer
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1203
    MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1204 c4ac052b Michael Niedermayer
    unsigned int i, entries;
1205
1206
    get_byte(pb); /* version */
1207
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1208
    entries = get_be32(pb);
1209
    if(entries >= UINT_MAX / sizeof(Time2Sample))
1210
        return -1;
1211
1212 70a61ed4 Michael Niedermayer
    sc->ctts_count = entries;
1213
    sc->ctts_data = av_malloc(entries * sizeof(Time2Sample));
1214 c4ac052b Michael Niedermayer
1215 4e5ef14f Baptiste Coudurier
    dprintf("track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
1216
1217 c4ac052b Michael Niedermayer
    for(i=0; i<entries; i++) {
1218 70a61ed4 Michael Niedermayer
        int count    =get_be32(pb);
1219
        int duration =get_be32(pb);
1220
1221 b0519015 Baptiste Coudurier
        if (duration < 0) {
1222
            av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
1223
            sc->ctts_count = 0;
1224
            url_fskip(pb, 8 * (entries - i - 1));
1225
            break;
1226
        }
1227 70a61ed4 Michael Niedermayer
        sc->ctts_data[i].count   = count;
1228
        sc->ctts_data[i].duration= duration;
1229
1230
        sc->time_rate= ff_gcd(sc->time_rate, duration);
1231 c4ac052b Michael Niedermayer
    }
1232
    return 0;
1233
}
1234
1235 5ca1d879 Zdenek Kabelac
static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1236
{
1237
    AVStream *st;
1238
    MOVStreamContext *sc;
1239
1240
    st = av_new_stream(c->fc, c->fc->nb_streams);
1241
    if (!st) return -2;
1242 9a630c25 Baptiste Coudurier
    sc = av_mallocz(sizeof(MOVStreamContext));
1243 5ca1d879 Zdenek Kabelac
    if (!sc) {
1244 bb270c08 Diego Biurrun
        av_free(st);
1245 5ca1d879 Zdenek Kabelac
        return -1;
1246
    }
1247
1248
    st->priv_data = sc;
1249 05edc1a7 Baptiste Coudurier
    st->codec->codec_type = CODEC_TYPE_DATA;
1250 25c4950e Fabrice Bellard
    st->start_time = 0; /* XXX: check */
1251 5ca1d879 Zdenek Kabelac
    c->streams[c->fc->nb_streams-1] = sc;
1252
1253
    return mov_read_default(c, pb, atom);
1254
}
1255
1256
static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1257
{
1258 f444b977 Baptiste Coudurier
    AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1259
    int version = get_byte(pb);
1260 5ca1d879 Zdenek Kabelac
1261
    get_byte(pb); get_byte(pb);
1262
    get_byte(pb); /* flags */
1263
    /*
1264
    MOV_TRACK_ENABLED 0x0001
1265
    MOV_TRACK_IN_MOVIE 0x0002
1266
    MOV_TRACK_IN_PREVIEW 0x0004
1267
    MOV_TRACK_IN_POSTER 0x0008
1268
    */
1269
1270 1175561e Baptiste Coudurier
    if (version == 1) {
1271
        get_be64(pb);
1272
        get_be64(pb);
1273
    } else {
1274
        get_be32(pb); /* creation time */
1275
        get_be32(pb); /* modification time */
1276
    }
1277 5ca1d879 Zdenek Kabelac
    st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1278
    get_be32(pb); /* reserved */
1279 25c4950e Fabrice Bellard
    st->start_time = 0; /* check */
1280 1175561e Baptiste Coudurier
    (version == 1) ? get_be64(pb) : get_be32(pb); /* highlevel (considering edits) duration in movie timebase */
1281 5ca1d879 Zdenek Kabelac
    get_be32(pb); /* reserved */
1282
    get_be32(pb); /* reserved */
1283
1284
    get_be16(pb); /* layer */
1285
    get_be16(pb); /* alternate group */
1286
    get_be16(pb); /* volume */
1287
    get_be16(pb); /* reserved */
1288
1289
    url_fskip(pb, 36); /* display matrix */
1290
1291
    /* those are fixed-point */
1292 c3daf8d8 Baptiste Coudurier
    get_be32(pb); /* track width */
1293
    get_be32(pb); /* track height */
1294 5ca1d879 Zdenek Kabelac
1295
    return 0;
1296
}
1297
1298
/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1299
/* like the files created with Adobe Premiere 5.0, for samples see */
1300
/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1301
static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1302
{
1303
    int err;
1304
1305
    if (atom.size < 8)
1306
        return 0; /* continue */
1307
    if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1308
        url_fskip(pb, atom.size - 4);
1309
        return 0;
1310
    }
1311
    atom.type = get_le32(pb);
1312
    atom.offset += 8;
1313
    atom.size -= 8;
1314 fd6e513e Zdenek Kabelac
    if (atom.type != MKTAG('m', 'd', 'a', 't')) {
1315 5ca1d879 Zdenek Kabelac
        url_fskip(pb, atom.size);
1316
        return 0;
1317
    }
1318
    err = mov_read_mdat(c, pb, atom);
1319
    return err;
1320
}
1321
1322
static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1323 0147f198 François Revol
{
1324 d966b2f0 Baptiste Coudurier
#ifdef CONFIG_ZLIB
1325 0147f198 François Revol
    ByteIOContext ctx;
1326 b6a17df4 Zdenek Kabelac
    uint8_t *cmov_data;
1327
    uint8_t *moov_data; /* uncompressed data */
1328 0147f198 François Revol
    long cmov_len, moov_len;
1329
    int ret;
1330 b6a17df4 Zdenek Kabelac
1331 0147f198 François Revol
    get_be32(pb); /* dcom atom */
1332
    if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1333
        return -1;
1334
    if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
1335 4e5ef14f Baptiste Coudurier
        av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
1336 0147f198 François Revol
        return -1;
1337
    }
1338
    get_be32(pb); /* cmvd atom */
1339
    if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1340
        return -1;
1341
    moov_len = get_be32(pb); /* uncompressed size */
1342 5ca1d879 Zdenek Kabelac
    cmov_len = atom.size - 6 * 4;
1343 5cd62665 Zdenek Kabelac
1344 9a630c25 Baptiste Coudurier
    cmov_data = av_malloc(cmov_len);
1345 0147f198 François Revol
    if (!cmov_data)
1346
        return -1;
1347 9a630c25 Baptiste Coudurier
    moov_data = av_malloc(moov_len);
1348 0147f198 François Revol
    if (!moov_data) {
1349
        av_free(cmov_data);
1350
        return -1;
1351
    }
1352
    get_buffer(pb, cmov_data, cmov_len);
1353 b6a17df4 Zdenek Kabelac
    if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1354 0147f198 François Revol
        return -1;
1355 942f3bb5 Michael Niedermayer
    if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
1356 0147f198 François Revol
        return -1;
1357 5ca1d879 Zdenek Kabelac
    atom.type = MKTAG( 'm', 'o', 'o', 'v' );
1358
    atom.offset = 0;
1359
    atom.size = moov_len;
1360 9ed83b0a Zdenek Kabelac
#ifdef DEBUG
1361 dbb4f00a Michael Niedermayer
//    { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
1362 9ed83b0a Zdenek Kabelac
#endif
1363 5ca1d879 Zdenek Kabelac
    ret = mov_read_default(c, &ctx, atom);
1364 0147f198 François Revol
    av_free(moov_data);
1365
    av_free(cmov_data);
1366
    return ret;
1367 d966b2f0 Baptiste Coudurier
#else
1368
    av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
1369
    return -1;
1370 0147f198 François Revol
#endif
1371 d966b2f0 Baptiste Coudurier
}
1372 0147f198 François Revol
1373 baf25c9d Gael Chardon
/* edit list atom */
1374
static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1375
{
1376 e0977c80 Baptiste Coudurier
    int i, edit_count;
1377
1378
    get_byte(pb); /* version */
1379
    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1380
    edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb);     /* entries */
1381
1382
    for(i=0; i<edit_count; i++){
1383
        get_be32(pb); /* Track duration */
1384
        get_be32(pb); /* Media time */
1385
        get_be32(pb); /* Media rate */
1386
    }
1387
    dprintf("track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count);
1388
    return 0;
1389 baf25c9d Gael Chardon
}
1390
1391 6cea494e Zdenek Kabelac
static const MOVParseTableEntry mov_default_parse_table[] = {
1392
/* mp4 atoms */
1393 5ca1d879 Zdenek Kabelac
{ MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
1394 c4ac052b Michael Niedermayer
{ MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
1395 5ca1d879 Zdenek Kabelac
{ MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
1396 58e555d4 Michael Niedermayer
{ MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
1397 de23f234 Baptiste Coudurier
{ MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
1398 4ea28253 Baptiste Coudurier
{ MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
1399 5ca1d879 Zdenek Kabelac
{ MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
1400 fc5188f3 Baptiste Coudurier
{ MKTAG( 'j', 'p', '2', 'h' ), mov_read_jp2h },
1401 5ca1d879 Zdenek Kabelac
{ MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
1402
{ MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
1403
{ MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
1404
{ MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
1405
{ MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
1406
{ MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
1407 169eb021 Mike Melanson
{ MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
1408 de23f234 Baptiste Coudurier
{ MKTAG( 'a', 'l', 'a', 'c' ), mov_read_alac }, /* alac specific atom */
1409 115329f1 Diego Biurrun
{ MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
1410 5ca1d879 Zdenek Kabelac
{ MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
1411
{ MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
1412
{ MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
1413
{ MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
1414 247d56f5 Brian Becker
{ MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
1415 5ca1d879 Zdenek Kabelac
{ MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
1416
{ MKTAG( 's', 't', 't', 's' ), mov_read_stts },
1417
{ MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
1418
{ MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
1419 d9b1c197 Roberto Togni
{ MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
1420 5ca1d879 Zdenek Kabelac
{ MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
1421
{ MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
1422
{ MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
1423
{ MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
1424 b59cb456 Baptiste Coudurier
{ 0L, NULL }
1425 6cea494e Zdenek Kabelac
};
1426
1427
static void mov_free_stream_context(MOVStreamContext *sc)
1428
{
1429
    if(sc) {
1430 115329f1 Diego Biurrun
        av_freep(&sc->ctts_data);
1431 c4ac052b Michael Niedermayer
        av_freep(&sc);
1432 6cea494e Zdenek Kabelac
    }
1433
}
1434
1435 b6a17df4 Zdenek Kabelac
/* XXX: is it sufficient ? */
1436 c9a65ca8 Fabrice Bellard
static int mov_probe(AVProbeData *p)
1437
{
1438 0e7eed09 Fabrice Bellard
    unsigned int offset;
1439
    uint32_t tag;
1440 2497479f François Revol
    int score = 0;
1441 3ffe3793 François Revol
1442 c9a65ca8 Fabrice Bellard
    /* check file header */
1443
    if (p->buf_size <= 12)
1444
        return 0;
1445 0e7eed09 Fabrice Bellard
    offset = 0;
1446
    for(;;) {
1447
        /* ignore invalid offset */
1448
        if ((offset + 8) > (unsigned int)p->buf_size)
1449 2497479f François Revol
            return score;
1450 fead30d4 Alex Beregszaszi
        tag = AV_RL32(p->buf + offset + 4);
1451 0e7eed09 Fabrice Bellard
        switch(tag) {
1452 2497479f François Revol
        /* check for obvious tags */
1453 fc5188f3 Baptiste Coudurier
        case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
1454 0e7eed09 Fabrice Bellard
        case MKTAG( 'm', 'o', 'o', 'v' ):
1455 8b879f18 François Revol
        case MKTAG( 'm', 'd', 'a', 't' ):
1456
        case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
1457 bc634f6f Zdenek Kabelac
        case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
1458 3ffe3793 François Revol
            return AVPROBE_SCORE_MAX;
1459 2497479f François Revol
        /* those are more common words, so rate then a bit less */
1460
        case MKTAG( 'w', 'i', 'd', 'e' ):
1461
        case MKTAG( 'f', 'r', 'e', 'e' ):
1462
        case MKTAG( 'j', 'u', 'n', 'k' ):
1463
        case MKTAG( 'p', 'i', 'c', 't' ):
1464
            return AVPROBE_SCORE_MAX - 5;
1465 0e7eed09 Fabrice Bellard
        case MKTAG( 'f', 't', 'y', 'p' ):
1466 5ca1d879 Zdenek Kabelac
        case MKTAG( 's', 'k', 'i', 'p' ):
1467 0d23cb84 François Revol
        case MKTAG( 'u', 'u', 'i', 'd' ):
1468 fead30d4 Alex Beregszaszi
            offset = AV_RB32(p->buf+offset) + offset;
1469 2497479f François Revol
            /* if we only find those cause probedata is too small at least rate them */
1470
            score = AVPROBE_SCORE_MAX - 50;
1471 0e7eed09 Fabrice Bellard
            break;
1472
        default:
1473
            /* unrecognized tag */
1474 2497479f François Revol
            return score;
1475 0e7eed09 Fabrice Bellard
        }
1476 3ffe3793 François Revol
    }
1477 2497479f François Revol
    return score;
1478 c9a65ca8 Fabrice Bellard
}
1479
1480 b72708f8 Baptiste Coudurier
static void mov_build_index(MOVContext *mov, AVStream *st)
1481
{
1482
    MOVStreamContext *sc = st->priv_data;
1483
    offset_t current_offset;
1484
    int64_t current_dts = 0;
1485
    int stts_index = 0;
1486
    int stsc_index = 0;
1487
    int stss_index = 0;
1488
    int i, j, k;
1489
1490 b60c0454 Baptiste Coudurier
    if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) {
1491 b72708f8 Baptiste Coudurier
        int keyframe, sample_size;
1492
        int current_sample = 0;
1493
        int stts_sample = 0;
1494
        int distance = 0;
1495
1496
        st->nb_frames = sc->sample_count;
1497
        for (i = 0; i < sc->chunk_count; i++) {
1498
            current_offset = sc->chunk_offsets[i];
1499
            if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1500
                stsc_index++;
1501
            for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
1502
                keyframe = !sc->keyframe_count || current_sample + 1 == sc->keyframes[stss_index];
1503
                if (keyframe) {
1504
                    distance = 0;
1505
                    if (stss_index + 1 < sc->keyframe_count)
1506
                        stss_index++;
1507
                }
1508
                sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
1509 949b1a13 Steve L'Homme
                dprintf("AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", size %d, distance %d, keyframe %d\n",
1510 b72708f8 Baptiste Coudurier
                        st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe);
1511
                av_add_index_entry(st, current_offset, current_dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0);
1512
                current_offset += sample_size;
1513
                assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
1514
                current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
1515
                distance++;
1516
                stts_sample++;
1517
                if (current_sample + 1 < sc->sample_count)
1518
                    current_sample++;
1519
                if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
1520
                    stts_sample = 0;
1521
                    stts_index++;
1522
                }
1523
            }
1524
        }
1525
    } else { /* read whole chunk */
1526
        int chunk_samples, chunk_size, chunk_duration;
1527
1528
        for (i = 0; i < sc->chunk_count; i++) {
1529
            current_offset = sc->chunk_offsets[i];
1530
            if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1531
                stsc_index++;
1532
            chunk_samples = sc->sample_to_chunk[stsc_index].count;
1533
            /* get chunk size */
1534 c61900c6 Baptiste Coudurier
            if (sc->sample_size > 1 || st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8)
1535 b72708f8 Baptiste Coudurier
                chunk_size = chunk_samples * sc->sample_size;
1536
            else if (sc->sample_size_v1.den > 0 && (chunk_samples * sc->sample_size_v1.num % sc->sample_size_v1.den == 0))
1537
                chunk_size = chunk_samples * sc->sample_size_v1.num / sc->sample_size_v1.den;
1538
            else { /* workaround to find nearest next chunk offset */
1539
                chunk_size = INT_MAX;
1540
                for (j = 0; j < mov->total_streams; j++) {
1541
                    MOVStreamContext *msc = mov->streams[j];
1542
1543
                    for (k = msc->next_chunk; k < msc->chunk_count; k++) {
1544
                        if (msc->chunk_offsets[k] > current_offset && msc->chunk_offsets[k] - current_offset < chunk_size) {
1545
                            chunk_size = msc->chunk_offsets[k] - current_offset;
1546
                            msc->next_chunk = k;
1547
                            break;
1548
                        }
1549
                    }
1550
                }
1551
                /* check for last chunk */
1552
                if (chunk_size == INT_MAX)
1553
                    for (j = 0; j < mov->mdat_count; j++) {
1554 949b1a13 Steve L'Homme
                        dprintf("mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n",
1555 b72708f8 Baptiste Coudurier
                                j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset);
1556
                        if (mov->mdat_list[j].offset <= current_offset && mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset)
1557
                            chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset;
1558
                    }
1559
                assert(chunk_size != INT_MAX);
1560
                for (j = 0; j < mov->total_streams; j++) {
1561
                    mov->streams[j]->next_chunk = 0;
1562
                }
1563
            }
1564
            av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
1565
            /* get chunk duration */
1566
            chunk_duration = 0;
1567
            while (chunk_samples > 0) {
1568
                if (chunk_samples < sc->stts_data[stts_index].count) {
1569
                    chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1570
                    sc->stts_data[stts_index].count -= chunk_samples;
1571
                    break;
1572
                } else {
1573
                    chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1574
                    chunk_samples -= sc->stts_data[stts_index].count;
1575
                    if (stts_index + 1 < sc->stts_count) {
1576
                        stts_index++;
1577
                    }
1578
                }
1579
            }
1580 949b1a13 Steve L'Homme
            dprintf("AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, duration %d\n",
1581 b72708f8 Baptiste Coudurier
                    st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
1582
            assert(chunk_duration % sc->time_rate == 0);
1583
            current_dts += chunk_duration / sc->time_rate;
1584
        }
1585
    }
1586 1f1890c7 Baptiste Coudurier
    /* adjust sample count to avindex entries */
1587
    sc->sample_count = st->nb_index_entries;
1588 b72708f8 Baptiste Coudurier
}
1589
1590 a266644f Zdenek Kabelac
static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1591 6cea494e Zdenek Kabelac
{
1592 b6a17df4 Zdenek Kabelac
    MOVContext *mov = (MOVContext *) s->priv_data;
1593 6cea494e Zdenek Kabelac
    ByteIOContext *pb = &s->pb;
1594 05edc1a7 Baptiste Coudurier
    int i, err;
1595 5ca1d879 Zdenek Kabelac
    MOV_atom_t atom = { 0, 0, 0 };
1596 6cea494e Zdenek Kabelac
1597
    mov->fc = s;
1598 b6a17df4 Zdenek Kabelac
    mov->parse_table = mov_default_parse_table;
1599 3253c51f Michael Niedermayer
1600 6cea494e Zdenek Kabelac
    if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1601 bb270c08 Diego Biurrun
        atom.size = url_fsize(pb);
1602 6cea494e Zdenek Kabelac
    else
1603 bb270c08 Diego Biurrun
        atom.size = 0x7FFFFFFFFFFFFFFFLL;
1604 6cea494e Zdenek Kabelac
1605
    /* check MOV header */
1606 5ca1d879 Zdenek Kabelac
    err = mov_read_default(mov, pb, atom);
1607 25fa62e1 Zdenek Kabelac
    if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
1608 bb270c08 Diego Biurrun
        av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
1609
                err, mov->found_moov, mov->found_mdat, url_ftell(pb));
1610
        return -1;
1611 6cea494e Zdenek Kabelac
    }
1612 4e5ef14f Baptiste Coudurier
    dprintf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1613
1614 6cea494e Zdenek Kabelac
    /* some cleanup : make sure we are on the mdat atom */
1615
    if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1616
        url_fseek(pb, mov->mdat_offset, SEEK_SET);
1617
1618 05edc1a7 Baptiste Coudurier
    mov->total_streams = s->nb_streams;
1619 5ca1d879 Zdenek Kabelac
1620 05edc1a7 Baptiste Coudurier
    for(i=0; i<mov->total_streams; i++) {
1621
        MOVStreamContext *sc = mov->streams[i];
1622 a2fe3b58 Baptiste Coudurier
1623 05edc1a7 Baptiste Coudurier
        if(!sc->time_rate)
1624
            sc->time_rate=1;
1625
        if(!sc->time_scale)
1626
            sc->time_scale= mov->time_scale;
1627
        av_set_pts_info(s->streams[i], 64, sc->time_rate, sc->time_scale);
1628
1629
        if(s->streams[i]->duration != AV_NOPTS_VALUE){
1630
            assert(s->streams[i]->duration % sc->time_rate == 0);
1631
            s->streams[i]->duration /= sc->time_rate;
1632 75b5b631 Michael Niedermayer
        }
1633 05edc1a7 Baptiste Coudurier
        sc->ffindex = i;
1634 b72708f8 Baptiste Coudurier
        mov_build_index(mov, s->streams[i]);
1635 6cea494e Zdenek Kabelac
    }
1636 a2fe3b58 Baptiste Coudurier
1637 b72708f8 Baptiste Coudurier
    for(i=0; i<mov->total_streams; i++) {
1638
        /* dont need those anymore */
1639
        av_freep(&mov->streams[i]->chunk_offsets);
1640
        av_freep(&mov->streams[i]->sample_to_chunk);
1641
        av_freep(&mov->streams[i]->sample_sizes);
1642
        av_freep(&mov->streams[i]->keyframes);
1643
        av_freep(&mov->streams[i]->stts_data);
1644
    }
1645
    av_freep(&mov->mdat_list);
1646 6cea494e Zdenek Kabelac
    return 0;
1647
}
1648
1649 a266644f Zdenek Kabelac
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1650 6cea494e Zdenek Kabelac
{
1651 b72708f8 Baptiste Coudurier
    MOVContext *mov = s->priv_data;
1652
    MOVStreamContext *sc = 0;
1653
    AVIndexEntry *sample = 0;
1654 86d8602f Michael Niedermayer
    int64_t best_dts = INT64_MAX;
1655 b72708f8 Baptiste Coudurier
    int i;
1656 3ffe3793 François Revol
1657 b72708f8 Baptiste Coudurier
    for (i = 0; i < mov->total_streams; i++) {
1658 bb270c08 Diego Biurrun
        MOVStreamContext *msc = mov->streams[i];
1659 86d8602f Michael Niedermayer
1660 b72708f8 Baptiste Coudurier
        if (s->streams[i]->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) {
1661
            AVIndexEntry *current_sample = &s->streams[i]->index_entries[msc->current_sample];
1662
            int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale);
1663
1664 949b1a13 Steve L'Homme
            dprintf("stream %d, sample %ld, dts %"PRId64"\n", i, msc->current_sample, dts);
1665 b72708f8 Baptiste Coudurier
            if (dts < best_dts) {
1666
                sample = current_sample;
1667
                best_dts = dts;
1668
                sc = msc;
1669 86d8602f Michael Niedermayer
            }
1670 6cea494e Zdenek Kabelac
        }
1671
    }
1672 b72708f8 Baptiste Coudurier
    if (!sample)
1673
        return -1;
1674
    /* must be done just before reading, to avoid infinite loop on sample */
1675
    sc->current_sample++;
1676 1f1890c7 Baptiste Coudurier
    if (sample->pos >= url_fsize(&s->pb)) {
1677 ccd3228e Dominik Mierzejewski
        av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos);
1678 bb270c08 Diego Biurrun
        return -1;
1679 6cea494e Zdenek Kabelac
    }
1680 312954f0 Baptiste Coudurier
#ifdef CONFIG_DV_DEMUXER
1681 b60c0454 Baptiste Coudurier
    if (sc->dv_audio_container) {
1682
        dv_get_packet(mov->dv_demux, pkt);
1683
        dprintf("dv audio pkt size %d\n", pkt->size);
1684
    } else {
1685 312954f0 Baptiste Coudurier
#endif
1686 b60c0454 Baptiste Coudurier
        url_fseek(&s->pb, sample->pos, SEEK_SET);
1687
        av_get_packet(&s->pb, pkt, sample->size);
1688 312954f0 Baptiste Coudurier
#ifdef CONFIG_DV_DEMUXER
1689 b60c0454 Baptiste Coudurier
        if (mov->dv_demux) {
1690
            void *pkt_destruct_func = pkt->destruct;
1691
            dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
1692
            pkt->destruct = pkt_destruct_func;
1693
        }
1694
    }
1695 312954f0 Baptiste Coudurier
#endif
1696 b72708f8 Baptiste Coudurier
    pkt->stream_index = sc->ffindex;
1697
    pkt->dts = sample->timestamp;
1698
    if (sc->ctts_data) {
1699
        assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
1700
        pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
1701
        /* update ctts context */
1702
        sc->sample_to_ctime_sample++;
1703
        if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
1704
            sc->sample_to_ctime_index++;
1705
            sc->sample_to_ctime_sample = 0;
1706 b565ea09 Baptiste Coudurier
        }
1707 b72708f8 Baptiste Coudurier
    } else {
1708
        pkt->pts = pkt->dts;
1709 b565ea09 Baptiste Coudurier
    }
1710 b72708f8 Baptiste Coudurier
    pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
1711
    pkt->pos = sample->pos;
1712 949b1a13 Steve L'Homme
    dprintf("stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n", pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
1713 b72708f8 Baptiste Coudurier
    return 0;
1714
}
1715 3ffe3793 François Revol
1716 b72708f8 Baptiste Coudurier
static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
1717
{
1718
    MOVStreamContext *sc = st->priv_data;
1719
    int sample, time_sample;
1720
    int i;
1721 115329f1 Diego Biurrun
1722 b72708f8 Baptiste Coudurier
    sample = av_index_search_timestamp(st, timestamp, flags);
1723 949b1a13 Steve L'Homme
    dprintf("stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
1724 b72708f8 Baptiste Coudurier
    if (sample < 0) /* not sure what to do */
1725
        return -1;
1726
    sc->current_sample = sample;
1727
    dprintf("stream %d, found sample %ld\n", st->index, sc->current_sample);
1728
    /* adjust ctts index */
1729
    if (sc->ctts_data) {
1730
        time_sample = 0;
1731
        for (i = 0; i < sc->ctts_count; i++) {
1732
            time_sample += sc->ctts_data[i].count;
1733
            if (time_sample >= sc->current_sample) {
1734
                sc->sample_to_ctime_index = i;
1735
                sc->sample_to_ctime_sample = time_sample - sc->current_sample;
1736
                break;
1737 115329f1 Diego Biurrun
            }
1738 247d56f5 Brian Becker
        }
1739
    }
1740 b72708f8 Baptiste Coudurier
    return sample;
1741 6cea494e Zdenek Kabelac
}
1742
1743 961e0ccd Michael Niedermayer
static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
1744 baf25c9d Gael Chardon
{
1745 b72708f8 Baptiste Coudurier
    AVStream *st;
1746
    int64_t seek_timestamp, timestamp;
1747
    int sample;
1748
    int i;
1749 baf25c9d Gael Chardon
1750 b72708f8 Baptiste Coudurier
    if (stream_index >= s->nb_streams)
1751 baf25c9d Gael Chardon
        return -1;
1752
1753 b72708f8 Baptiste Coudurier
    st = s->streams[stream_index];
1754
    sample = mov_seek_stream(st, sample_time, flags);
1755
    if (sample < 0)
1756 baf25c9d Gael Chardon
        return -1;
1757
1758 b72708f8 Baptiste Coudurier
    /* adjust seek timestamp to found sample timestamp */
1759
    seek_timestamp = st->index_entries[sample].timestamp;
1760 baf25c9d Gael Chardon
1761 b72708f8 Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++) {
1762
        st = s->streams[i];
1763
        if (stream_index == i || st->discard == AVDISCARD_ALL)
1764
            continue;
1765 baf25c9d Gael Chardon
1766 b72708f8 Baptiste Coudurier
        timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
1767
        mov_seek_stream(st, timestamp, flags);
1768 115329f1 Diego Biurrun
    }
1769 baf25c9d Gael Chardon
    return 0;
1770
}
1771
1772 a266644f Zdenek Kabelac
static int mov_read_close(AVFormatContext *s)
1773 6cea494e Zdenek Kabelac
{
1774
    int i;
1775 b6a17df4 Zdenek Kabelac
    MOVContext *mov = (MOVContext *) s->priv_data;
1776 6cea494e Zdenek Kabelac
    for(i=0; i<mov->total_streams; i++)
1777
        mov_free_stream_context(mov->streams[i]);
1778 5ca1d879 Zdenek Kabelac
    /* free color tabs */
1779
    for(i=0; i<mov->ctab_size; i++)
1780 bb270c08 Diego Biurrun
        av_freep(&mov->ctab[i]);
1781 b60c0454 Baptiste Coudurier
    if(mov->dv_demux){
1782
        for(i=0; i<mov->dv_fctx->nb_streams; i++){
1783
            av_freep(&mov->dv_fctx->streams[i]->codec);
1784
            av_freep(&mov->dv_fctx->streams[i]);
1785
        }
1786
        av_freep(&mov->dv_fctx);
1787
        av_freep(&mov->dv_demux);
1788
    }
1789 5ca1d879 Zdenek Kabelac
    av_freep(&mov->ctab);
1790 6cea494e Zdenek Kabelac
    return 0;
1791
}
1792
1793 ff70e601 Måns Rullgård
AVInputFormat mov_demuxer = {
1794 fc5188f3 Baptiste Coudurier
    "mov,mp4,m4a,3gp,3g2,mj2",
1795
    "QuickTime/MPEG4/Motion JPEG 2000 format",
1796 c9a65ca8 Fabrice Bellard
    sizeof(MOVContext),
1797
    mov_probe,
1798 6cea494e Zdenek Kabelac
    mov_read_header,
1799
    mov_read_packet,
1800
    mov_read_close,
1801 baf25c9d Gael Chardon
    mov_read_seek,
1802 6cea494e Zdenek Kabelac
};