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.

302 lines
9.4KB

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