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>
7 * This file is part of FFmpeg.
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.
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.
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
24 #ifndef AVFORMAT_ISOM_H
25 #define AVFORMAT_ISOM_H
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"
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
[];
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]);
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
57 typedef struct MOVTimeToSample
{
59 unsigned int duration
;
63 typedef struct MOVStts
{
65 unsigned int duration
;
68 typedef struct MOVCtts
{
73 typedef struct MOVStsc
{
79 typedef struct MOVElst
{
85 typedef struct MOVDref
{
91 int16_t nlvl_to
, nlvl_from
;
94 typedef struct MOVAtom
{
96 int64_t size
; /* total size (excluding the size and type fields) */
99 struct MOVParseTableEntry
;
101 typedef struct MOVFragment
{
104 uint64_t base_data_offset
;
105 uint64_t moof_offset
;
106 uint64_t implicit_offset
;
113 typedef struct MOVTrackExt
{
121 typedef struct MOVSbgp
{
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
;
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
;
139 typedef struct MOVFragmentStreamInfo
{
142 int64_t first_tfra_pts
;
144 int64_t next_trun_dts
;
145 // Index of the first sample/trun in the fragment.
148 MOVEncryptionIndex
*encryption_index
;
149 int stsd_id
; // current fragment stsd_id
150 } MOVFragmentStreamInfo
;
152 typedef struct MOVFragmentIndexItem
{
157 MOVFragmentStreamInfo
* stream_info
;
158 } MOVFragmentIndexItem
;
160 typedef struct MOVFragmentIndex
{
165 MOVFragmentIndexItem
* item
;
168 typedef struct MOVIndexRange
{
173 typedef struct MOVStreamContext
{
177 int id
; ///< AVStream id
178 int ffindex
; ///< AVStream index
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
;
188 unsigned int sdtp_count
;
190 unsigned int ctts_count
;
191 unsigned int ctts_allocated_size
;
193 unsigned int stsc_count
;
195 unsigned int stsc_index
;
197 unsigned int stps_count
;
198 unsigned *stps_data
; ///< partial sync sample for mpeg-2 open gop
200 unsigned int elst_count
;
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
;
208 unsigned int keyframe_count
;
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.
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
;
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];
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
;
242 unsigned int sync_group_count
;
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
;
252 int nb_frames_for_fps
;
253 int64_t duration_for_fps
;
255 /** extradata array (and size) for multiple stsd */
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
;
271 AVAmbientViewingEnvironment
*ambient
;
276 int has_sidx
; // If there is an sidx entry for this stream.
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
;
285 struct IAMFDemuxContext
*iamf
;
286 int iamf_stream_offset
;
289 typedef struct HEIFItemRef
{
294 typedef struct HEIFItem
{
296 HEIFItemRef
*iref_list
;
300 int64_t extent_length
;
301 int64_t extent_offset
;
308 int is_idat_relative
;
309 uint8_t *icc_profile
;
310 size_t icc_profile_size
;
313 typedef struct HEIFGrid
{
315 HEIFItem
**tile_item_list
;
316 int16_t *tile_id_list
;
317 unsigned *tile_idx_list
;
321 typedef struct MOVContext
{
322 const AVClass
*class; ///< class for private options
325 int64_t duration
; ///< duration of the longest track
326 int found_moov
; ///< 'moov' atom has been found
327 int found_iloc
; ///< 'iloc' atom has been found
328 int found_iinf
; ///< 'iinf' atom has been found
329 int found_mdat
; ///< 'mdat' atom has been found
330 int found_hdlr_mdta
; ///< 'hdlr' atom with type 'mdta' has been found
331 int trak_index
; ///< Index of the current 'trak'
333 unsigned meta_keys_count
;
334 DVDemuxContext
*dv_demux
;
335 AVFormatContext
*dv_fctx
;
336 int isom
; ///< 1 if file is ISO Media (mp4/3gp)
337 MOVFragment fragment
; ///< current fragment in moof atom
338 MOVTrackExt
*trex_data
;
340 int itunes_metadata
; ///< metadata are itunes style
341 int handbrake_version
;
343 unsigned int nb_chapter_tracks
;
344 int use_absolute_path
;
346 int advanced_editlist
;
347 int advanced_editlist_autodisabled
;
349 int seek_individually
;
350 int64_t next_root_atom
; ///< offset of the next root atom
353 int *bitrates
; ///< bitrates read before streams creation
357 int has_looked_for_mfra
;
359 MOVFragmentIndex frag_index
;
361 unsigned int aax_mode
; ///< 'aax' file has been detected
362 uint8_t file_key
[20];
364 void *activation_bytes
;
365 int activation_bytes_size
;
366 void *audible_fixed_key
;
367 int audible_fixed_key_size
;
369 int audible_key_size
;
372 struct AVAES
*aes_decrypt
;
373 uint8_t *decryption_key
;
374 int decryption_key_len
;
376 int32_t movie_display_matrix
[3][3]; ///< display matrix from mvhd
377 int have_read_mfra_size
;
379 uint32_t max_stts_delta
;
382 HEIFItem
**heif_item
;
387 int interleaved_read
;
390 int ff_mp4_read_descr_len(AVIOContext
*pb
);
391 int ff_mp4_read_descr(void *logctx
, AVIOContext
*pb
, int *tag
);
392 int ff_mp4_read_dec_config_descr(void *logctx
, AVStream
*st
, AVIOContext
*pb
);
393 void ff_mp4_parse_es_descr(AVIOContext
*pb
, int *es_id
);
395 #define MP4ODescrTag 0x01
396 #define MP4IODescrTag 0x02
397 #define MP4ESDescrTag 0x03
398 #define MP4DecConfigDescrTag 0x04
399 #define MP4DecSpecificDescrTag 0x05
400 #define MP4SLDescrTag 0x06
402 #define MOV_TFHD_BASE_DATA_OFFSET 0x01
403 #define MOV_TFHD_STSD_ID 0x02
404 #define MOV_TFHD_DEFAULT_DURATION 0x08
405 #define MOV_TFHD_DEFAULT_SIZE 0x10
406 #define MOV_TFHD_DEFAULT_FLAGS 0x20
407 #define MOV_TFHD_DURATION_IS_EMPTY 0x010000
408 #define MOV_TFHD_DEFAULT_BASE_IS_MOOF 0x020000
410 #define MOV_TRUN_DATA_OFFSET 0x01
411 #define MOV_TRUN_FIRST_SAMPLE_FLAGS 0x04
412 #define MOV_TRUN_SAMPLE_DURATION 0x100
413 #define MOV_TRUN_SAMPLE_SIZE 0x200
414 #define MOV_TRUN_SAMPLE_FLAGS 0x400
415 #define MOV_TRUN_SAMPLE_CTS 0x800
417 #define MOV_FRAG_SAMPLE_FLAG_DEGRADATION_PRIORITY_MASK 0x0000ffff
418 #define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC 0x00010000
419 #define MOV_FRAG_SAMPLE_FLAG_PADDING_MASK 0x000e0000
420 #define MOV_FRAG_SAMPLE_FLAG_REDUNDANCY_MASK 0x00300000
421 #define MOV_FRAG_SAMPLE_FLAG_DEPENDED_MASK 0x00c00000
422 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_MASK 0x03000000
424 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO 0x02000000
425 #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES 0x01000000
427 #define MOV_TKHD_FLAG_ENABLED 0x0001
428 #define MOV_TKHD_FLAG_IN_MOVIE 0x0002
429 #define MOV_TKHD_FLAG_IN_PREVIEW 0x0004
430 #define MOV_TKHD_FLAG_IN_POSTER 0x0008
432 #define MOV_SAMPLE_DEPENDENCY_UNKNOWN 0x0
433 #define MOV_SAMPLE_DEPENDENCY_YES 0x1
434 #define MOV_SAMPLE_DEPENDENCY_NO 0x2
436 #define MOV_TREF_FLAG_ENHANCEMENT 0x1
438 #define TAG_IS_AVCI(tag) \
439 ((tag) == MKTAG('a', 'i', '5', 'p') || \
440 (tag) == MKTAG('a', 'i', '5', 'q') || \
441 (tag) == MKTAG('a', 'i', '5', '2') || \
442 (tag) == MKTAG('a', 'i', '5', '3') || \
443 (tag) == MKTAG('a', 'i', '5', '5') || \
444 (tag) == MKTAG('a', 'i', '5', '6') || \
445 (tag) == MKTAG('a', 'i', '1', 'p') || \
446 (tag) == MKTAG('a', 'i', '1', 'q') || \
447 (tag) == MKTAG('a', 'i', '1', '2') || \
448 (tag) == MKTAG('a', 'i', '1', '3') || \
449 (tag) == MKTAG('a', 'i', '1', '5') || \
450 (tag) == MKTAG('a', 'i', '1', '6') || \
451 (tag) == MKTAG('a', 'i', 'v', 'x') || \
452 (tag) == MKTAG('A', 'V', 'i', 'n'))
455 int ff_mov_read_esds(AVFormatContext
*fc
, AVIOContext
*pb
);
457 int ff_mov_read_stsd_entries(MOVContext
*c
, AVIOContext
*pb
, int entries
);
458 void ff_mov_write_chan(AVIOContext
*pb
, int64_t channel_layout
);
460 #define FF_MOV_FLAG_MFRA_AUTO -1
461 #define FF_MOV_FLAG_MFRA_DTS 1
462 #define FF_MOV_FLAG_MFRA_PTS 2
465 * Compute codec id for 'lpcm' tag.
466 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
468 static inline enum AVCodecID
ff_mov_get_lpcm_codec_id(int bps
, int flags
)
475 return ff_get_pcm_codec_id(bps
, flags
& 1, flags
& 2, flags
& 4 ? -1 : 0);
478 #define MOV_ISMV_TTML_TAG MKTAG('d', 'f', 'x', 'p')
479 #define MOV_MP4_TTML_TAG MKTAG('s', 't', 'p', 'p')
480 #define MOV_MP4_FPCM_TAG MKTAG('f', 'p', 'c', 'm')
481 #define MOV_MP4_IPCM_TAG MKTAG('i', 'p', 'c', 'm')
483 struct MP4TrackKindValueMapping
{
488 struct MP4TrackKindMapping
{
489 const char *scheme_uri
;
490 const struct MP4TrackKindValueMapping
*value_maps
;
493 extern const struct MP4TrackKindMapping ff_mov_track_kind_table
[];
495 #endif /* AVFORMAT_ISOM_H */