FFmpeg coverage


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