FFmpeg coverage


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