FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/isom.h
Date: 2024-11-20 23:03:26
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 unsigned 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 unsigned tref_flags;
216 int tref_id;
217 int timecode_track;
218 int width; ///< tkhd width
219 int height; ///< tkhd height
220 int h_spacing; ///< pasp hSpacing
221 int v_spacing; ///< pasp vSpacing
222 int dts_shift; ///< dts shift when ctts is negative
223 uint32_t palette[256];
224 int has_palette;
225 int64_t data_size;
226 uint32_t tmcd_flags; ///< tmcd track flags
227 uint8_t tmcd_nb_frames; ///< tmcd number of frames per tick / second
228 int64_t track_end; ///< used for dts generation in fragmented movie files
229 int start_pad; ///< amount of samples to skip due to enc-dec delay
230 unsigned int rap_group_count;
231 MOVSbgp *rap_group;
232 unsigned int sync_group_count;
233 MOVSbgp *sync_group;
234 uint8_t *sgpd_sync;
235 uint32_t sgpd_sync_count;
236 int32_t *sample_offsets;
237 int sample_offsets_count;
238 int *open_key_samples;
239 int open_key_samples_count;
240 uint32_t min_sample_duration;
241
242 int nb_frames_for_fps;
243 int64_t duration_for_fps;
244
245 /** extradata array (and size) for multiple stsd */
246 uint8_t **extradata;
247 int *extradata_size;
248 int last_stsd_index;
249 int stsd_count;
250 int stsd_version;
251
252 int32_t *display_matrix;
253 AVStereo3D *stereo3d;
254 size_t stereo3d_size;
255 AVSphericalMapping *spherical;
256 size_t spherical_size;
257 AVMasteringDisplayMetadata *mastering;
258 size_t mastering_size;
259 AVContentLightMetadata *coll;
260 size_t coll_size;
261 AVAmbientViewingEnvironment *ambient;
262 size_t ambient_size;
263
264 uint32_t format;
265
266 int has_sidx; // If there is an sidx entry for this stream.
267 struct {
268 struct AVAESCTR* aes_ctr;
269 struct AVAES *aes_ctx;
270 unsigned int per_sample_iv_size; // Either 0, 8, or 16.
271 AVEncryptionInfo *default_encrypted_sample;
272 MOVEncryptionIndex *encryption_index;
273 } cenc;
274
275 struct IAMFDemuxContext *iamf;
276 } MOVStreamContext;
277
278 typedef struct HEIFItem {
279 AVStream *st;
280 char *name;
281 int item_id;
282 int64_t extent_length;
283 int64_t extent_offset;
284 int width;
285 int height;
286 int rotation;
287 int hflip;
288 int vflip;
289 int type;
290 int is_idat_relative;
291 uint8_t *icc_profile;
292 size_t icc_profile_size;
293 } HEIFItem;
294
295 typedef struct HEIFGrid {
296 HEIFItem *item;
297 HEIFItem **tile_item_list;
298 int16_t *tile_id_list;
299 int nb_tiles;
300 } HEIFGrid;
301
302 typedef struct MOVContext {
303 const AVClass *class; ///< class for private options
304 AVFormatContext *fc;
305 int time_scale;
306 int64_t duration; ///< duration of the longest track
307 int found_moov; ///< 'moov' atom has been found
308 int found_iloc; ///< 'iloc' atom has been found
309 int found_iinf; ///< 'iinf' atom has been found
310 int found_mdat; ///< 'mdat' atom has been found
311 int found_hdlr_mdta; ///< 'hdlr' atom with type 'mdta' has been found
312 int trak_index; ///< Index of the current 'trak'
313 char **meta_keys;
314 unsigned meta_keys_count;
315 DVDemuxContext *dv_demux;
316 AVFormatContext *dv_fctx;
317 int isom; ///< 1 if file is ISO Media (mp4/3gp)
318 MOVFragment fragment; ///< current fragment in moof atom
319 MOVTrackExt *trex_data;
320 unsigned trex_count;
321 int itunes_metadata; ///< metadata are itunes style
322 int handbrake_version;
323 int *chapter_tracks;
324 unsigned int nb_chapter_tracks;
325 int use_absolute_path;
326 int ignore_editlist;
327 int advanced_editlist;
328 int advanced_editlist_autodisabled;
329 int ignore_chapters;
330 int seek_individually;
331 int64_t next_root_atom; ///< offset of the next root atom
332 int export_all;
333 int export_xmp;
334 int *bitrates; ///< bitrates read before streams creation
335 int bitrates_count;
336 int moov_retry;
337 int use_mfra_for;
338 int has_looked_for_mfra;
339 int use_tfdt;
340 MOVFragmentIndex frag_index;
341 int atom_depth;
342 unsigned int aax_mode; ///< 'aax' file has been detected
343 uint8_t file_key[20];
344 uint8_t file_iv[20];
345 void *activation_bytes;
346 int activation_bytes_size;
347 void *audible_fixed_key;
348 int audible_fixed_key_size;
349 void *audible_key;
350 int audible_key_size;
351 void *audible_iv;
352 int audible_iv_size;
353 struct AVAES *aes_decrypt;
354 uint8_t *decryption_key;
355 int decryption_key_len;
356 int enable_drefs;
357 int32_t movie_display_matrix[3][3]; ///< display matrix from mvhd
358 int have_read_mfra_size;
359 uint32_t mfra_size;
360 uint32_t max_stts_delta;
361 int primary_item_id;
362 int cur_item_id;
363 HEIFItem **heif_item;
364 int nb_heif_item;
365 HEIFGrid *heif_grid;
366 int nb_heif_grid;
367 int thmb_item_id;
368 int64_t idat_offset;
369 int interleaved_read;
370 } MOVContext;
371
372 int ff_mp4_read_descr_len(AVIOContext *pb);
373 int ff_mp4_read_descr(void *logctx, AVIOContext *pb, int *tag);
374 int ff_mp4_read_dec_config_descr(void *logctx, AVStream *st, AVIOContext *pb);
375 void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id);
376
377 #define MP4ODescrTag 0x01
378 #define MP4IODescrTag 0x02
379 #define MP4ESDescrTag 0x03
380 #define MP4DecConfigDescrTag 0x04
381 #define MP4DecSpecificDescrTag 0x05
382 #define MP4SLDescrTag 0x06
383
384 #define MOV_TFHD_BASE_DATA_OFFSET 0x01
385 #define MOV_TFHD_STSD_ID 0x02
386 #define MOV_TFHD_DEFAULT_DURATION 0x08
387 #define MOV_TFHD_DEFAULT_SIZE 0x10
388 #define MOV_TFHD_DEFAULT_FLAGS 0x20
389 #define MOV_TFHD_DURATION_IS_EMPTY 0x010000
390 #define MOV_TFHD_DEFAULT_BASE_IS_MOOF 0x020000
391
392 #define MOV_TRUN_DATA_OFFSET 0x01
393 #define MOV_TRUN_FIRST_SAMPLE_FLAGS 0x04
394 #define MOV_TRUN_SAMPLE_DURATION 0x100
395 #define MOV_TRUN_SAMPLE_SIZE 0x200
396 #define MOV_TRUN_SAMPLE_FLAGS 0x400
397 #define MOV_TRUN_SAMPLE_CTS 0x800
398
399 #define MOV_FRAG_SAMPLE_FLAG_DEGRADATION_PRIORITY_MASK 0x0000ffff
400 #define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC 0x00010000
401 #define MOV_FRAG_SAMPLE_FLAG_PADDING_MASK 0x000e0000
402 #define MOV_FRAG_SAMPLE_FLAG_REDUNDANCY_MASK 0x00300000
403 #define MOV_FRAG_SAMPLE_FLAG_DEPENDED_MASK 0x00c00000
404 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_MASK 0x03000000
405
406 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO 0x02000000
407 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES 0x01000000
408
409 #define MOV_TKHD_FLAG_ENABLED 0x0001
410 #define MOV_TKHD_FLAG_IN_MOVIE 0x0002
411 #define MOV_TKHD_FLAG_IN_PREVIEW 0x0004
412 #define MOV_TKHD_FLAG_IN_POSTER 0x0008
413
414 #define MOV_SAMPLE_DEPENDENCY_UNKNOWN 0x0
415 #define MOV_SAMPLE_DEPENDENCY_YES 0x1
416 #define MOV_SAMPLE_DEPENDENCY_NO 0x2
417
418 #define MOV_TREF_FLAG_ENHANCEMENT 0x1
419
420 #define TAG_IS_AVCI(tag) \
421 ((tag) == MKTAG('a', 'i', '5', 'p') || \
422 (tag) == MKTAG('a', 'i', '5', 'q') || \
423 (tag) == MKTAG('a', 'i', '5', '2') || \
424 (tag) == MKTAG('a', 'i', '5', '3') || \
425 (tag) == MKTAG('a', 'i', '5', '5') || \
426 (tag) == MKTAG('a', 'i', '5', '6') || \
427 (tag) == MKTAG('a', 'i', '1', 'p') || \
428 (tag) == MKTAG('a', 'i', '1', 'q') || \
429 (tag) == MKTAG('a', 'i', '1', '2') || \
430 (tag) == MKTAG('a', 'i', '1', '3') || \
431 (tag) == MKTAG('a', 'i', '1', '5') || \
432 (tag) == MKTAG('a', 'i', '1', '6') || \
433 (tag) == MKTAG('a', 'i', 'v', 'x') || \
434 (tag) == MKTAG('A', 'V', 'i', 'n'))
435
436
437 int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb);
438
439 int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries);
440 void ff_mov_write_chan(AVIOContext *pb, int64_t channel_layout);
441
442 #define FF_MOV_FLAG_MFRA_AUTO -1
443 #define FF_MOV_FLAG_MFRA_DTS 1
444 #define FF_MOV_FLAG_MFRA_PTS 2
445
446 /**
447 * Compute codec id for 'lpcm' tag.
448 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
449 */
450 4 static inline enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
451 {
452 /* lpcm flags:
453 * 0x1 = float
454 * 0x2 = big-endian
455 * 0x4 = signed
456 */
457
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);
458 }
459
460 #define MOV_ISMV_TTML_TAG MKTAG('d', 'f', 'x', 'p')
461 #define MOV_MP4_TTML_TAG MKTAG('s', 't', 'p', 'p')
462 #define MOV_MP4_FPCM_TAG MKTAG('f', 'p', 'c', 'm')
463 #define MOV_MP4_IPCM_TAG MKTAG('i', 'p', 'c', 'm')
464
465 struct MP4TrackKindValueMapping {
466 int disposition;
467 const char *value;
468 };
469
470 struct MP4TrackKindMapping {
471 const char *scheme_uri;
472 const struct MP4TrackKindValueMapping *value_maps;
473 };
474
475 extern const struct MP4TrackKindMapping ff_mov_track_kind_table[];
476
477 #endif /* AVFORMAT_ISOM_H */
478