FFmpeg coverage


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