FFmpeg coverage


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