1 /*
2 * ISO Media common code
3 * copyright (c) 2001 Fabrice Bellard
4 * copyright (c) 2002 Francois Revol <revol@free.fr>
5 * copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@free.fr>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef AVFORMAT_ISOM_H
25 #define AVFORMAT_ISOM_H
26
27 #include <stddef.h>
28 #include <stdint.h>
29
30 #include "libavutil/encryption_info.h"
31 #include "libavutil/mastering_display_metadata.h"
32 #include "libavutil/spherical.h"
33 #include "libavutil/stereo3d.h"
34
35 #include "avio.h"
36 #include "internal.h"
37 #include "dv.h"
38
39 /* isom.c */
40 extern const AVCodecTag ff_mp4_obj_type[];
41 extern const AVCodecTag ff_codec_movvideo_tags[];
42 extern const AVCodecTag ff_codec_movaudio_tags[];
43 extern const AVCodecTag ff_codec_movsubtitle_tags[];
44 extern const AVCodecTag ff_codec_movdata_tags[];
45
46 int ff_mov_iso639_to_lang(const char lang[4], int mp4);
47 int ff_mov_lang_to_iso639(unsigned code, char to[4]);
48
49 struct AVAESCTR;
50
51 /* the QuickTime file format is quite convoluted...
52 * it has lots of index tables, each indexing something in another one...
53 * Here we just use what is needed to read the chunks
54 */
55
56 typedef struct MOVStts {
57 unsigned int count;
58 int duration;
59 } MOVStts;
60
61 typedef struct MOVStsc {
62 int first;
63 int count;
64 int id;
65 } MOVStsc;
66
67 typedef struct MOVElst {
68 int64_t duration;
69 int64_t time;
70 float rate;
71 } MOVElst;
72
73 typedef struct MOVDref {
74 uint32_t type;
75 char *path;
76 char *dir;
77 char volume[28];
78 char filename[64];
79 int16_t nlvl_to, nlvl_from;
80 } MOVDref;
81
82 typedef struct MOVAtom {
83 uint32_t type;
84 int64_t size; /* total size (excluding the size and type fields) */
85 } MOVAtom;
86
87 struct MOVParseTableEntry;
88
89 typedef struct MOVFragment {
90 int found_tfhd;
91 unsigned track_id;
92 uint64_t base_data_offset;
93 uint64_t moof_offset;
94 uint64_t implicit_offset;
95 unsigned stsd_id;
96 unsigned duration;
97 unsigned size;
98 unsigned flags;
99 } MOVFragment;
100
101 typedef struct MOVTrackExt {
102 unsigned track_id;
103 unsigned stsd_id;
104 unsigned duration;
105 unsigned size;
106 unsigned flags;
107 } MOVTrackExt;
108
109 typedef struct MOVSbgp {
110 unsigned int count;
111 unsigned int index;
112 } MOVSbgp;
113
114 typedef struct MOVEncryptionIndex {
115 // Individual encrypted samples. If there are no elements, then the default
116 // settings will be used.
117 unsigned int nb_encrypted_samples;
118 AVEncryptionInfo **encrypted_samples;
119
120 uint8_t* auxiliary_info_sizes;
121 size_t auxiliary_info_sample_count;
122 uint8_t auxiliary_info_default_size;
123 uint64_t* auxiliary_offsets; ///< Absolute seek position
124 size_t auxiliary_offsets_count;
125 } MOVEncryptionIndex;
126
127 typedef struct MOVFragmentStreamInfo {
128 int id;
129 int64_t sidx_pts;
130 int64_t first_tfra_pts;
131 int64_t tfdt_dts;
132 int64_t next_trun_dts;
133 int index_entry;
134 MOVEncryptionIndex *encryption_index;
135 } MOVFragmentStreamInfo;
136
137 typedef struct MOVFragmentIndexItem {
138 int64_t moof_offset;
139 int headers_read;
140 int current;
141 int nb_stream_info;
142 MOVFragmentStreamInfo * stream_info;
143 } MOVFragmentIndexItem;
144
145 typedef struct MOVFragmentIndex {
146 int allocated_size;
147 int complete;
148 int current;
149 int nb_items;
150 MOVFragmentIndexItem * item;
151 } MOVFragmentIndex;
152
153 typedef struct MOVIndexRange {
154 int64_t start;
155 int64_t end;
156 } MOVIndexRange;
157
158 typedef struct MOVStreamContext {
159 AVIOContext *pb;
160 int pb_is_copied;
161 int ffindex; ///< AVStream index
162 int next_chunk;
163 unsigned int chunk_count;
164 int64_t *chunk_offsets;
165 unsigned int stts_count;
166 MOVStts *stts_data;
167 unsigned int sdtp_count;
168 uint8_t *sdtp_data;
169 unsigned int ctts_count;
170 unsigned int ctts_allocated_size;
171 MOVStts *ctts_data;
172 unsigned int stsc_count;
173 MOVStsc *stsc_data;
174 unsigned int stsc_index;
175 int stsc_sample;
176 unsigned int stps_count;
177 unsigned *stps_data; ///< partial sync sample for mpeg-2 open gop
178 MOVElst *elst_data;
179 unsigned int elst_count;
180 int ctts_index;
181 int ctts_sample;
182 unsigned int sample_size; ///< may contain value calculated from stsd or value from stsz atom
183 unsigned int stsz_sample_size; ///< always contains sample size from stsz atom
184 unsigned int sample_count;
185 int *sample_sizes;
186 int keyframe_absent;
187 unsigned int keyframe_count;
188 int *keyframes;
189 int time_scale;
190 int64_t time_offset; ///< time offset of the edit list entries
191 int64_t min_corrected_pts; ///< minimum Composition time shown by the edits excluding empty edits.
192 int current_sample;
193 int64_t current_index;
194 MOVIndexRange* index_ranges;
195 MOVIndexRange* current_index_range;
196 unsigned int bytes_per_frame;
197 unsigned int samples_per_frame;
198 int dv_audio_container;
199 int pseudo_stream_id; ///< -1 means demux all ids
200 int16_t audio_cid; ///< stsd audio compression id
201 unsigned drefs_count;
202 MOVDref *drefs;
203 int dref_id;
204 int timecode_track;
205 int width; ///< tkhd width
206 int height; ///< tkhd height
207 int dts_shift; ///< dts shift when ctts is negative
208 uint32_t palette[256];
209 int has_palette;
210 int64_t data_size;
211 uint32_t tmcd_flags; ///< tmcd track flags
212 int64_t track_end; ///< used for dts generation in fragmented movie files
213 int start_pad; ///< amount of samples to skip due to enc-dec delay
214 unsigned int rap_group_count;
215 MOVSbgp *rap_group;
216
217 int nb_frames_for_fps;
218 int64_t duration_for_fps;
219
220 /** extradata array (and size) for multiple stsd */
221 uint8_t **extradata;
222 int *extradata_size;
223 int last_stsd_index;
224 int stsd_count;
225 int stsd_version;
226
227 int32_t *display_matrix;
228 AVStereo3D *stereo3d;
229 AVSphericalMapping *spherical;
230 size_t spherical_size;
231 AVMasteringDisplayMetadata *mastering;
232 AVContentLightMetadata *coll;
233 size_t coll_size;
234
235 uint32_t format;
236
237 int has_sidx; // If there is an sidx entry for this stream.
238 struct {
239 struct AVAESCTR* aes_ctr;
240 unsigned int per_sample_iv_size; // Either 0, 8, or 16.
241 AVEncryptionInfo *default_encrypted_sample;
242 MOVEncryptionIndex *encryption_index;
243 } cenc;
244 } MOVStreamContext;
245
246 typedef struct MOVContext {
247 const AVClass *class; ///< class for private options
248 AVFormatContext *fc;
249 int time_scale;
250 int64_t duration; ///< duration of the longest track
251 int found_moov; ///< 'moov' atom has been found
252 int found_mdat; ///< 'mdat' atom has been found
253 int found_hdlr_mdta; ///< 'hdlr' atom with type 'mdta' has been found
254 int trak_index; ///< Index of the current 'trak'
255 char **meta_keys;
256 unsigned meta_keys_count;
257 DVDemuxContext *dv_demux;
258 AVFormatContext *dv_fctx;
259 int isom; ///< 1 if file is ISO Media (mp4/3gp)
260 MOVFragment fragment; ///< current fragment in moof atom
261 MOVTrackExt *trex_data;
262 unsigned trex_count;
263 int itunes_metadata; ///< metadata are itunes style
264 int handbrake_version;
265 int *chapter_tracks;
266 unsigned int nb_chapter_tracks;
267 int use_absolute_path;
268 int ignore_editlist;
269 int advanced_editlist;
270 int ignore_chapters;
271 int seek_individually;
272 int64_t next_root_atom; ///< offset of the next root atom
273 int export_all;
274 int export_xmp;
275 int *bitrates; ///< bitrates read before streams creation
276 int bitrates_count;
277 int moov_retry;
278 int use_mfra_for;
279 int has_looked_for_mfra;
280 MOVFragmentIndex frag_index;
281 int atom_depth;
282 unsigned int aax_mode; ///< 'aax' file has been detected
283 uint8_t file_key[20];
284 uint8_t file_iv[20];
285 void *activation_bytes;
286 int activation_bytes_size;
287 void *audible_fixed_key;
288 int audible_fixed_key_size;
289 void *audible_key;
290 int audible_key_size;
291 void *audible_iv;
292 int audible_iv_size;
293 struct AVAES *aes_decrypt;
294 uint8_t *decryption_key;
295 int decryption_key_len;
296 int enable_drefs;
297 int32_t movie_display_matrix[3][3]; ///< display matrix from mvhd
298 int have_read_mfra_size;
299 uint32_t mfra_size;
300 } MOVContext;
301
302 int ff_mp4_read_descr_len(AVIOContext *pb);
303 int ff_mp4_read_descr(AVFormatContext *fc, AVIOContext *pb, int *tag);
304 int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext *pb);
305 void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id);
306
307 #define MP4ODescrTag 0x01
308 #define MP4IODescrTag 0x02
309 #define MP4ESDescrTag 0x03
310 #define MP4DecConfigDescrTag 0x04
311 #define MP4DecSpecificDescrTag 0x05
312 #define MP4SLDescrTag 0x06
313
314 #define MOV_TFHD_BASE_DATA_OFFSET 0x01
315 #define MOV_TFHD_STSD_ID 0x02
316 #define MOV_TFHD_DEFAULT_DURATION 0x08
317 #define MOV_TFHD_DEFAULT_SIZE 0x10
318 #define MOV_TFHD_DEFAULT_FLAGS 0x20
319 #define MOV_TFHD_DURATION_IS_EMPTY 0x010000
320 #define MOV_TFHD_DEFAULT_BASE_IS_MOOF 0x020000
321
322 #define MOV_TRUN_DATA_OFFSET 0x01
323 #define MOV_TRUN_FIRST_SAMPLE_FLAGS 0x04
324 #define MOV_TRUN_SAMPLE_DURATION 0x100
325 #define MOV_TRUN_SAMPLE_SIZE 0x200
326 #define MOV_TRUN_SAMPLE_FLAGS 0x400
327 #define MOV_TRUN_SAMPLE_CTS 0x800
328
329 #define MOV_FRAG_SAMPLE_FLAG_DEGRADATION_PRIORITY_MASK 0x0000ffff
330 #define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC 0x00010000
331 #define MOV_FRAG_SAMPLE_FLAG_PADDING_MASK 0x000e0000
332 #define MOV_FRAG_SAMPLE_FLAG_REDUNDANCY_MASK 0x00300000
333 #define MOV_FRAG_SAMPLE_FLAG_DEPENDED_MASK 0x00c00000
334 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_MASK 0x03000000
335
336 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO 0x02000000
337 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES 0x01000000
338
339 #define MOV_TKHD_FLAG_ENABLED 0x0001
340 #define MOV_TKHD_FLAG_IN_MOVIE 0x0002
341 #define MOV_TKHD_FLAG_IN_PREVIEW 0x0004
342 #define MOV_TKHD_FLAG_IN_POSTER 0x0008
343
344 #define MOV_SAMPLE_DEPENDENCY_UNKNOWN 0x0
345 #define MOV_SAMPLE_DEPENDENCY_YES 0x1
346 #define MOV_SAMPLE_DEPENDENCY_NO 0x2
347
348
349 #define TAG_IS_AVCI(tag) \
350 ((tag) == MKTAG('a', 'i', '5', 'p') || \
351 (tag) == MKTAG('a', 'i', '5', 'q') || \
352 (tag) == MKTAG('a', 'i', '5', '2') || \
353 (tag) == MKTAG('a', 'i', '5', '3') || \
354 (tag) == MKTAG('a', 'i', '5', '5') || \
355 (tag) == MKTAG('a', 'i', '5', '6') || \
356 (tag) == MKTAG('a', 'i', '1', 'p') || \
357 (tag) == MKTAG('a', 'i', '1', 'q') || \
358 (tag) == MKTAG('a', 'i', '1', '2') || \
359 (tag) == MKTAG('a', 'i', '1', '3') || \
360 (tag) == MKTAG('a', 'i', '1', '5') || \
361 (tag) == MKTAG('a', 'i', '1', '6') || \
362 (tag) == MKTAG('a', 'i', 'v', 'x') || \
363 (tag) == MKTAG('A', 'V', 'i', 'n'))
364
365
366 int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb);
367
368 int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries);
369 void ff_mov_write_chan(AVIOContext *pb, int64_t channel_layout);
370
371 #define FF_MOV_FLAG_MFRA_AUTO -1
372 #define FF_MOV_FLAG_MFRA_DTS 1
373 #define FF_MOV_FLAG_MFRA_PTS 2
374
375 /**
376 * Compute codec id for 'lpcm' tag.
377 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
378 */
ff_mov_get_lpcm_codec_id(int bps,int flags)379 static inline enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
380 {
381 /* lpcm flags:
382 * 0x1 = float
383 * 0x2 = big-endian
384 * 0x4 = signed
385 */
386 return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
387 }
388
389 #endif /* AVFORMAT_ISOM_H */
390