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 unsigned int duration;
59 } MOVStts;
60
61 typedef struct MOVCtts {
62 unsigned int count;
63 int duration;
64 } MOVCtts;
65
66 typedef struct MOVStsc {
67 int first;
68 int count;
69 int id;
70 } MOVStsc;
71
72 typedef struct MOVElst {
73 int64_t duration;
74 int64_t time;
75 float rate;
76 } MOVElst;
77
78 typedef struct MOVDref {
79 uint32_t type;
80 char *path;
81 char *dir;
82 char volume[28];
83 char filename[64];
84 int16_t nlvl_to, nlvl_from;
85 } MOVDref;
86
87 typedef struct MOVAtom {
88 uint32_t type;
89 int64_t size; /* total size (excluding the size and type fields) */
90 } MOVAtom;
91
92 struct MOVParseTableEntry;
93
94 typedef struct MOVFragment {
95 int found_tfhd;
96 unsigned track_id;
97 uint64_t base_data_offset;
98 uint64_t moof_offset;
99 uint64_t implicit_offset;
100 unsigned stsd_id;
101 unsigned duration;
102 unsigned size;
103 unsigned flags;
104 } MOVFragment;
105
106 typedef struct MOVTrackExt {
107 unsigned track_id;
108 unsigned stsd_id;
109 unsigned duration;
110 unsigned size;
111 unsigned flags;
112 } MOVTrackExt;
113
114 typedef struct MOVSbgp {
115 unsigned int count;
116 unsigned int index;
117 } MOVSbgp;
118
119 typedef struct MOVEncryptionIndex {
120 // Individual encrypted samples. If there are no elements, then the default
121 // settings will be used.
122 unsigned int nb_encrypted_samples;
123 AVEncryptionInfo **encrypted_samples;
124
125 uint8_t* auxiliary_info_sizes;
126 size_t auxiliary_info_sample_count;
127 uint8_t auxiliary_info_default_size;
128 uint64_t* auxiliary_offsets; ///< Absolute seek position
129 size_t auxiliary_offsets_count;
130 } MOVEncryptionIndex;
131
132 typedef struct MOVFragmentStreamInfo {
133 int id;
134 int64_t sidx_pts;
135 int64_t first_tfra_pts;
136 int64_t tfdt_dts;
137 int64_t next_trun_dts;
138 int index_entry;
139 MOVEncryptionIndex *encryption_index;
140 } MOVFragmentStreamInfo;
141
142 typedef struct MOVFragmentIndexItem {
143 int64_t moof_offset;
144 int headers_read;
145 int current;
146 int nb_stream_info;
147 MOVFragmentStreamInfo * stream_info;
148 } MOVFragmentIndexItem;
149
150 typedef struct MOVFragmentIndex {
151 int allocated_size;
152 int complete;
153 int current;
154 int nb_items;
155 MOVFragmentIndexItem * item;
156 } MOVFragmentIndex;
157
158 typedef struct MOVIndexRange {
159 int64_t start;
160 int64_t end;
161 } MOVIndexRange;
162
163 typedef struct MOVStreamContext {
164 AVIOContext *pb;
165 int pb_is_copied;
166 int ffindex; ///< AVStream index
167 int next_chunk;
168 unsigned int chunk_count;
169 int64_t *chunk_offsets;
170 unsigned int stts_count;
171 MOVStts *stts_data;
172 unsigned int sdtp_count;
173 uint8_t *sdtp_data;
174 unsigned int ctts_count;
175 unsigned int ctts_allocated_size;
176 MOVCtts *ctts_data;
177 unsigned int stsc_count;
178 MOVStsc *stsc_data;
179 unsigned int stsc_index;
180 int stsc_sample;
181 unsigned int stps_count;
182 unsigned *stps_data; ///< partial sync sample for mpeg-2 open gop
183 MOVElst *elst_data;
184 unsigned int elst_count;
185 int ctts_index;
186 int ctts_sample;
187 unsigned int sample_size; ///< may contain value calculated from stsd or value from stsz atom
188 unsigned int stsz_sample_size; ///< always contains sample size from stsz atom
189 unsigned int sample_count;
190 int *sample_sizes;
191 int keyframe_absent;
192 unsigned int keyframe_count;
193 int *keyframes;
194 int time_scale;
195 int64_t time_offset; ///< time offset of the edit list entries
196 int64_t min_corrected_pts; ///< minimum Composition time shown by the edits excluding empty edits.
197 int current_sample;
198 int64_t current_index;
199 MOVIndexRange* index_ranges;
200 MOVIndexRange* current_index_range;
201 unsigned int bytes_per_frame;
202 unsigned int samples_per_frame;
203 int dv_audio_container;
204 int pseudo_stream_id; ///< -1 means demux all ids
205 int16_t audio_cid; ///< stsd audio compression id
206 unsigned drefs_count;
207 MOVDref *drefs;
208 int dref_id;
209 int timecode_track;
210 int width; ///< tkhd width
211 int height; ///< tkhd height
212 int dts_shift; ///< dts shift when ctts is negative
213 uint32_t palette[256];
214 int has_palette;
215 int64_t data_size;
216 uint32_t tmcd_flags; ///< tmcd track flags
217 uint8_t tmcd_nb_frames; ///< tmcd number of frames per tick / second
218 int64_t track_end; ///< used for dts generation in fragmented movie files
219 int start_pad; ///< amount of samples to skip due to enc-dec delay
220 unsigned int rap_group_count;
221 MOVSbgp *rap_group;
222 unsigned int sync_group_count;
223 MOVSbgp *sync_group;
224 uint8_t *sgpd_sync;
225 uint32_t sgpd_sync_count;
226 int32_t *sample_offsets;
227 int sample_offsets_count;
228 int *open_key_samples;
229 int open_key_samples_count;
230 uint32_t min_sample_duration;
231
232 int nb_frames_for_fps;
233 int64_t duration_for_fps;
234
235 /** extradata array (and size) for multiple stsd */
236 uint8_t **extradata;
237 int *extradata_size;
238 int last_stsd_index;
239 int stsd_count;
240 int stsd_version;
241
242 int32_t *display_matrix;
243 AVStereo3D *stereo3d;
244 AVSphericalMapping *spherical;
245 size_t spherical_size;
246 AVMasteringDisplayMetadata *mastering;
247 AVContentLightMetadata *coll;
248 size_t coll_size;
249
250 uint32_t format;
251
252 int has_sidx; // If there is an sidx entry for this stream.
253 struct {
254 struct AVAESCTR* aes_ctr;
255 struct AVAES *aes_ctx;
256 unsigned int frag_index_entry_base;
257 unsigned int per_sample_iv_size; // Either 0, 8, or 16.
258 AVEncryptionInfo *default_encrypted_sample;
259 MOVEncryptionIndex *encryption_index;
260 } cenc;
261 } MOVStreamContext;
262
263 typedef struct MOVContext {
264 const AVClass *class; ///< class for private options
265 AVFormatContext *fc;
266 int time_scale;
267 int64_t duration; ///< duration of the longest track
268 int found_moov; ///< 'moov' atom has been found
269 int found_mdat; ///< 'mdat' atom has been found
270 int found_hdlr_mdta; ///< 'hdlr' atom with type 'mdta' has been found
271 int trak_index; ///< Index of the current 'trak'
272 char **meta_keys;
273 unsigned meta_keys_count;
274 DVDemuxContext *dv_demux;
275 AVFormatContext *dv_fctx;
276 int isom; ///< 1 if file is ISO Media (mp4/3gp)
277 MOVFragment fragment; ///< current fragment in moof atom
278 MOVTrackExt *trex_data;
279 unsigned trex_count;
280 int itunes_metadata; ///< metadata are itunes style
281 int handbrake_version;
282 int *chapter_tracks;
283 unsigned int nb_chapter_tracks;
284 int use_absolute_path;
285 int ignore_editlist;
286 int advanced_editlist;
287 int ignore_chapters;
288 int seek_individually;
289 int64_t next_root_atom; ///< offset of the next root atom
290 int export_all;
291 int export_xmp;
292 int *bitrates; ///< bitrates read before streams creation
293 int bitrates_count;
294 int moov_retry;
295 int use_mfra_for;
296 int has_looked_for_mfra;
297 int use_tfdt;
298 MOVFragmentIndex frag_index;
299 int atom_depth;
300 unsigned int aax_mode; ///< 'aax' file has been detected
301 uint8_t file_key[20];
302 uint8_t file_iv[20];
303 void *activation_bytes;
304 int activation_bytes_size;
305 void *audible_fixed_key;
306 int audible_fixed_key_size;
307 void *audible_key;
308 int audible_key_size;
309 void *audible_iv;
310 int audible_iv_size;
311 struct AVAES *aes_decrypt;
312 uint8_t *decryption_key;
313 int decryption_key_len;
314 int enable_drefs;
315 int32_t movie_display_matrix[3][3]; ///< display matrix from mvhd
316 int have_read_mfra_size;
317 uint32_t mfra_size;
318 uint32_t max_stts_delta;
319 int is_still_picture_avif;
320 int primary_item_id;
321 } MOVContext;
322
323 int ff_mp4_read_descr_len(AVIOContext *pb);
324 int ff_mp4_read_descr(AVFormatContext *fc, AVIOContext *pb, int *tag);
325 int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext *pb);
326 void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id);
327
328 #define MP4ODescrTag 0x01
329 #define MP4IODescrTag 0x02
330 #define MP4ESDescrTag 0x03
331 #define MP4DecConfigDescrTag 0x04
332 #define MP4DecSpecificDescrTag 0x05
333 #define MP4SLDescrTag 0x06
334
335 #define MOV_TFHD_BASE_DATA_OFFSET 0x01
336 #define MOV_TFHD_STSD_ID 0x02
337 #define MOV_TFHD_DEFAULT_DURATION 0x08
338 #define MOV_TFHD_DEFAULT_SIZE 0x10
339 #define MOV_TFHD_DEFAULT_FLAGS 0x20
340 #define MOV_TFHD_DURATION_IS_EMPTY 0x010000
341 #define MOV_TFHD_DEFAULT_BASE_IS_MOOF 0x020000
342
343 #define MOV_TRUN_DATA_OFFSET 0x01
344 #define MOV_TRUN_FIRST_SAMPLE_FLAGS 0x04
345 #define MOV_TRUN_SAMPLE_DURATION 0x100
346 #define MOV_TRUN_SAMPLE_SIZE 0x200
347 #define MOV_TRUN_SAMPLE_FLAGS 0x400
348 #define MOV_TRUN_SAMPLE_CTS 0x800
349
350 #define MOV_FRAG_SAMPLE_FLAG_DEGRADATION_PRIORITY_MASK 0x0000ffff
351 #define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC 0x00010000
352 #define MOV_FRAG_SAMPLE_FLAG_PADDING_MASK 0x000e0000
353 #define MOV_FRAG_SAMPLE_FLAG_REDUNDANCY_MASK 0x00300000
354 #define MOV_FRAG_SAMPLE_FLAG_DEPENDED_MASK 0x00c00000
355 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_MASK 0x03000000
356
357 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO 0x02000000
358 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES 0x01000000
359
360 #define MOV_TKHD_FLAG_ENABLED 0x0001
361 #define MOV_TKHD_FLAG_IN_MOVIE 0x0002
362 #define MOV_TKHD_FLAG_IN_PREVIEW 0x0004
363 #define MOV_TKHD_FLAG_IN_POSTER 0x0008
364
365 #define MOV_SAMPLE_DEPENDENCY_UNKNOWN 0x0
366 #define MOV_SAMPLE_DEPENDENCY_YES 0x1
367 #define MOV_SAMPLE_DEPENDENCY_NO 0x2
368 #define MOV_SAMPLE_DEPENDENCY_EXT 0x3
369
370
371 #define TAG_IS_AVCI(tag) \
372 ((tag) == MKTAG('a', 'i', '5', 'p') || \
373 (tag) == MKTAG('a', 'i', '5', 'q') || \
374 (tag) == MKTAG('a', 'i', '5', '2') || \
375 (tag) == MKTAG('a', 'i', '5', '3') || \
376 (tag) == MKTAG('a', 'i', '5', '5') || \
377 (tag) == MKTAG('a', 'i', '5', '6') || \
378 (tag) == MKTAG('a', 'i', '1', 'p') || \
379 (tag) == MKTAG('a', 'i', '1', 'q') || \
380 (tag) == MKTAG('a', 'i', '1', '2') || \
381 (tag) == MKTAG('a', 'i', '1', '3') || \
382 (tag) == MKTAG('a', 'i', '1', '5') || \
383 (tag) == MKTAG('a', 'i', '1', '6') || \
384 (tag) == MKTAG('a', 'i', 'v', 'x') || \
385 (tag) == MKTAG('A', 'V', 'i', 'n'))
386
387
388 int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb);
389
390 int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries);
391 void ff_mov_write_chan(AVIOContext *pb, int64_t channel_layout);
392
393 #define FF_MOV_FLAG_MFRA_AUTO -1
394 #define FF_MOV_FLAG_MFRA_DTS 1
395 #define FF_MOV_FLAG_MFRA_PTS 2
396
397 /**
398 * Compute codec id for 'lpcm' tag.
399 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
400 */
ff_mov_get_lpcm_codec_id(int bps, int flags)401 static inline enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
402 {
403 /* lpcm flags:
404 * 0x1 = float
405 * 0x2 = big-endian
406 * 0x4 = signed
407 */
408 return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
409 }
410
411 #define MOV_ISMV_TTML_TAG MKTAG('d', 'f', 'x', 'p')
412 #define MOV_MP4_TTML_TAG MKTAG('s', 't', 'p', 'p')
413
414 struct MP4TrackKindValueMapping {
415 int disposition;
416 const char *value;
417 };
418
419 struct MP4TrackKindMapping {
420 const char *scheme_uri;
421 const struct MP4TrackKindValueMapping *value_maps;
422 };
423
424 extern const struct MP4TrackKindMapping ff_mov_track_kind_table[];
425
426 #endif /* AVFORMAT_ISOM_H */
427