You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
12KB

  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. int index_entry;
  113. MOVEncryptionIndex *encryption_index;
  114. } MOVFragmentStreamInfo;
  115. typedef struct MOVFragmentIndexItem {
  116. int64_t moof_offset;
  117. int headers_read;
  118. int current;
  119. int nb_stream_info;
  120. MOVFragmentStreamInfo * stream_info;
  121. } MOVFragmentIndexItem;
  122. typedef struct MOVFragmentIndex {
  123. int allocated_size;
  124. int complete;
  125. int current;
  126. int nb_items;
  127. MOVFragmentIndexItem * item;
  128. } MOVFragmentIndex;
  129. typedef struct MOVIndexRange {
  130. int64_t start;
  131. int64_t end;
  132. } MOVIndexRange;
  133. typedef struct MOVStreamContext {
  134. AVIOContext *pb;
  135. int pb_is_copied;
  136. int ffindex; ///< AVStream index
  137. int next_chunk;
  138. unsigned int chunk_count;
  139. int64_t *chunk_offsets;
  140. unsigned int stts_count;
  141. MOVStts *stts_data;
  142. unsigned int ctts_count;
  143. unsigned int ctts_allocated_size;
  144. MOVStts *ctts_data;
  145. unsigned int stsc_count;
  146. MOVStsc *stsc_data;
  147. unsigned int stsc_index;
  148. int stsc_sample;
  149. unsigned int stps_count;
  150. unsigned *stps_data; ///< partial sync sample for mpeg-2 open gop
  151. MOVElst *elst_data;
  152. unsigned int elst_count;
  153. int ctts_index;
  154. int ctts_sample;
  155. unsigned int sample_size; ///< may contain value calculated from stsd or value from stsz atom
  156. unsigned int stsz_sample_size; ///< always contains sample size from stsz atom
  157. unsigned int sample_count;
  158. int *sample_sizes;
  159. int keyframe_absent;
  160. unsigned int keyframe_count;
  161. int *keyframes;
  162. int time_scale;
  163. int64_t time_offset; ///< time offset of the edit list entries
  164. int64_t min_corrected_pts; ///< minimum Composition time shown by the edits excluding empty edits.
  165. int current_sample;
  166. int64_t current_index;
  167. MOVIndexRange* index_ranges;
  168. MOVIndexRange* current_index_range;
  169. unsigned int bytes_per_frame;
  170. unsigned int samples_per_frame;
  171. int dv_audio_container;
  172. int pseudo_stream_id; ///< -1 means demux all ids
  173. int16_t audio_cid; ///< stsd audio compression id
  174. unsigned drefs_count;
  175. MOVDref *drefs;
  176. int dref_id;
  177. int timecode_track;
  178. int width; ///< tkhd width
  179. int height; ///< tkhd height
  180. int dts_shift; ///< dts shift when ctts is negative
  181. uint32_t palette[256];
  182. int has_palette;
  183. int64_t data_size;
  184. uint32_t tmcd_flags; ///< tmcd track flags
  185. int64_t track_end; ///< used for dts generation in fragmented movie files
  186. int start_pad; ///< amount of samples to skip due to enc-dec delay
  187. unsigned int rap_group_count;
  188. MOVSbgp *rap_group;
  189. int nb_frames_for_fps;
  190. int64_t duration_for_fps;
  191. /** extradata array (and size) for multiple stsd */
  192. uint8_t **extradata;
  193. int *extradata_size;
  194. int last_stsd_index;
  195. int stsd_count;
  196. int stsd_version;
  197. int32_t *display_matrix;
  198. AVStereo3D *stereo3d;
  199. AVSphericalMapping *spherical;
  200. size_t spherical_size;
  201. AVMasteringDisplayMetadata *mastering;
  202. AVContentLightMetadata *coll;
  203. size_t coll_size;
  204. uint32_t format;
  205. int has_sidx; // If there is an sidx entry for this stream.
  206. struct {
  207. struct AVAESCTR* aes_ctr;
  208. unsigned int per_sample_iv_size; // Either 0, 8, or 16.
  209. AVEncryptionInfo *default_encrypted_sample;
  210. MOVEncryptionIndex *encryption_index;
  211. } cenc;
  212. } MOVStreamContext;
  213. typedef struct MOVContext {
  214. const AVClass *class; ///< class for private options
  215. AVFormatContext *fc;
  216. int time_scale;
  217. int64_t duration; ///< duration of the longest track
  218. int found_moov; ///< 'moov' atom has been found
  219. int found_mdat; ///< 'mdat' atom has been found
  220. int found_hdlr_mdta; ///< 'hdlr' atom with type 'mdta' has been found
  221. int trak_index; ///< Index of the current 'trak'
  222. char **meta_keys;
  223. unsigned meta_keys_count;
  224. DVDemuxContext *dv_demux;
  225. AVFormatContext *dv_fctx;
  226. int isom; ///< 1 if file is ISO Media (mp4/3gp)
  227. MOVFragment fragment; ///< current fragment in moof atom
  228. MOVTrackExt *trex_data;
  229. unsigned trex_count;
  230. int itunes_metadata; ///< metadata are itunes style
  231. int handbrake_version;
  232. int *chapter_tracks;
  233. unsigned int nb_chapter_tracks;
  234. int use_absolute_path;
  235. int ignore_editlist;
  236. int advanced_editlist;
  237. int ignore_chapters;
  238. int seek_individually;
  239. int64_t next_root_atom; ///< offset of the next root atom
  240. int export_all;
  241. int export_xmp;
  242. int *bitrates; ///< bitrates read before streams creation
  243. int bitrates_count;
  244. int moov_retry;
  245. int use_mfra_for;
  246. int has_looked_for_mfra;
  247. MOVFragmentIndex frag_index;
  248. int atom_depth;
  249. unsigned int aax_mode; ///< 'aax' file has been detected
  250. uint8_t file_key[20];
  251. uint8_t file_iv[20];
  252. void *activation_bytes;
  253. int activation_bytes_size;
  254. void *audible_fixed_key;
  255. int audible_fixed_key_size;
  256. struct AVAES *aes_decrypt;
  257. uint8_t *decryption_key;
  258. int decryption_key_len;
  259. int enable_drefs;
  260. int32_t movie_display_matrix[3][3]; ///< display matrix from mvhd
  261. } MOVContext;
  262. int ff_mp4_read_descr_len(AVIOContext *pb);
  263. int ff_mp4_read_descr(AVFormatContext *fc, AVIOContext *pb, int *tag);
  264. int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext *pb);
  265. void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id);
  266. #define MP4ODescrTag 0x01
  267. #define MP4IODescrTag 0x02
  268. #define MP4ESDescrTag 0x03
  269. #define MP4DecConfigDescrTag 0x04
  270. #define MP4DecSpecificDescrTag 0x05
  271. #define MP4SLDescrTag 0x06
  272. #define MOV_TFHD_BASE_DATA_OFFSET 0x01
  273. #define MOV_TFHD_STSD_ID 0x02
  274. #define MOV_TFHD_DEFAULT_DURATION 0x08
  275. #define MOV_TFHD_DEFAULT_SIZE 0x10
  276. #define MOV_TFHD_DEFAULT_FLAGS 0x20
  277. #define MOV_TFHD_DURATION_IS_EMPTY 0x010000
  278. #define MOV_TFHD_DEFAULT_BASE_IS_MOOF 0x020000
  279. #define MOV_TRUN_DATA_OFFSET 0x01
  280. #define MOV_TRUN_FIRST_SAMPLE_FLAGS 0x04
  281. #define MOV_TRUN_SAMPLE_DURATION 0x100
  282. #define MOV_TRUN_SAMPLE_SIZE 0x200
  283. #define MOV_TRUN_SAMPLE_FLAGS 0x400
  284. #define MOV_TRUN_SAMPLE_CTS 0x800
  285. #define MOV_FRAG_SAMPLE_FLAG_DEGRADATION_PRIORITY_MASK 0x0000ffff
  286. #define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC 0x00010000
  287. #define MOV_FRAG_SAMPLE_FLAG_PADDING_MASK 0x000e0000
  288. #define MOV_FRAG_SAMPLE_FLAG_REDUNDANCY_MASK 0x00300000
  289. #define MOV_FRAG_SAMPLE_FLAG_DEPENDED_MASK 0x00c00000
  290. #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_MASK 0x03000000
  291. #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO 0x02000000
  292. #define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES 0x01000000
  293. #define MOV_TKHD_FLAG_ENABLED 0x0001
  294. #define MOV_TKHD_FLAG_IN_MOVIE 0x0002
  295. #define MOV_TKHD_FLAG_IN_PREVIEW 0x0004
  296. #define MOV_TKHD_FLAG_IN_POSTER 0x0008
  297. #define MOV_SAMPLE_DEPENDENCY_UNKNOWN 0x0
  298. #define MOV_SAMPLE_DEPENDENCY_YES 0x1
  299. #define MOV_SAMPLE_DEPENDENCY_NO 0x2
  300. #define TAG_IS_AVCI(tag) \
  301. ((tag) == MKTAG('a', 'i', '5', 'p') || \
  302. (tag) == MKTAG('a', 'i', '5', 'q') || \
  303. (tag) == MKTAG('a', 'i', '5', '2') || \
  304. (tag) == MKTAG('a', 'i', '5', '3') || \
  305. (tag) == MKTAG('a', 'i', '5', '5') || \
  306. (tag) == MKTAG('a', 'i', '5', '6') || \
  307. (tag) == MKTAG('a', 'i', '1', 'p') || \
  308. (tag) == MKTAG('a', 'i', '1', 'q') || \
  309. (tag) == MKTAG('a', 'i', '1', '2') || \
  310. (tag) == MKTAG('a', 'i', '1', '3') || \
  311. (tag) == MKTAG('a', 'i', '1', '5') || \
  312. (tag) == MKTAG('a', 'i', '1', '6') || \
  313. (tag) == MKTAG('a', 'i', 'v', 'x') || \
  314. (tag) == MKTAG('A', 'V', 'i', 'n'))
  315. int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb);
  316. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries);
  317. void ff_mov_write_chan(AVIOContext *pb, int64_t channel_layout);
  318. #define FF_MOV_FLAG_MFRA_AUTO -1
  319. #define FF_MOV_FLAG_MFRA_DTS 1
  320. #define FF_MOV_FLAG_MFRA_PTS 2
  321. /**
  322. * Compute codec id for 'lpcm' tag.
  323. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  324. */
  325. static inline enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
  326. {
  327. /* lpcm flags:
  328. * 0x1 = float
  329. * 0x2 = big-endian
  330. * 0x4 = signed
  331. */
  332. return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
  333. }
  334. #endif /* AVFORMAT_ISOM_H */