FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/isom.h
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 2 2 100.0%
Functions: 1 1 100.0%
Branches: 1 2 50.0%

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