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.

367 lines
11KB

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