isom.h 12 KB

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