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.

330 lines
14KB

  1. #ifndef _mov_internal_h_
  2. #define _mov_internal_h_
  3. #include "mov-box.h"
  4. #include "mov-atom.h"
  5. #include "mov-format.h"
  6. #include "mov-buffer.h"
  7. #include "mov-ioutil.h"
  8. #define MOV_APP "ireader/media-server"
  9. #define MOV_TAG(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  10. #define MOV_MOOV MOV_TAG('m', 'o', 'o', 'v')
  11. #define MOV_ROOT MOV_TAG('r', 'o', 'o', 't')
  12. #define MOV_TRAK MOV_TAG('t', 'r', 'a', 'k')
  13. #define MOV_MDIA MOV_TAG('m', 'd', 'i', 'a')
  14. #define MOV_EDTS MOV_TAG('e', 'd', 't', 's')
  15. #define MOV_MINF MOV_TAG('m', 'i', 'n', 'f')
  16. #define MOV_GMHD MOV_TAG('g', 'm', 'h', 'd') // Apple QuickTime gmhd(text media)
  17. #define MOV_DINF MOV_TAG('d', 'i', 'n', 'f')
  18. #define MOV_STBL MOV_TAG('s', 't', 'b', 'l')
  19. #define MOV_MVEX MOV_TAG('m', 'v', 'e', 'x')
  20. #define MOV_MOOF MOV_TAG('m', 'o', 'o', 'f')
  21. #define MOV_TRAF MOV_TAG('t', 'r', 'a', 'f')
  22. #define MOV_MFRA MOV_TAG('m', 'f', 'r', 'a')
  23. #define MOV_VIDEO MOV_TAG('v', 'i', 'd', 'e') // ISO/IEC 14496-12:2015(E) 12.1 Video media (p169)
  24. #define MOV_AUDIO MOV_TAG('s', 'o', 'u', 'n') // ISO/IEC 14496-12:2015(E) 12.2 Audio media (p173)
  25. #define MOV_META MOV_TAG('m', 'e', 't', 'a') // ISO/IEC 14496-12:2015(E) 12.3 Metadata media (p181)
  26. #define MOV_HINT MOV_TAG('h', 'i', 'n', 't') // ISO/IEC 14496-12:2015(E) 12.4 Hint media (p183)
  27. #define MOV_TEXT MOV_TAG('t', 'e', 'x', 't') // ISO/IEC 14496-12:2015(E) 12.5 Text media (p184)
  28. #define MOV_SUBT MOV_TAG('s', 'u', 'b', 't') // ISO/IEC 14496-12:2015(E) 12.6 Subtitle media (p185)
  29. #define MOV_FONT MOV_TAG('f', 'd', 's', 'm') // ISO/IEC 14496-12:2015(E) 12.7 Font media (p186)
  30. #define MOV_CLCP MOV_TAG('c', 'l', 'c', 'p') // ClosedCaptionHandler
  31. #define MOV_ALIS MOV_TAG('a', 'l', 'i', 's') // Apple QuickTime Macintosh alias
  32. #define MOV_SBTL MOV_TAG('s', 'b', 't', 'l') // text/tx3g
  33. // https://developer.apple.com/library/content/documentation/General/Reference/HLSAuthoringSpec/Requirements.html#//apple_ref/doc/uid/TP40016596-CH2-SW1
  34. // Video encoding requirements 1.10: Use 'avc1', 'hvc1', or 'dvh1' rather than 'avc3', 'hev1', or 'dvhe'
  35. #define MOV_H264 MOV_TAG('a', 'v', 'c', '1') // H.264 ISO/IEC 14496-15:2010(E) 5.3.4 AVC Video Stream Definition (18)
  36. #define MOV_HEVC MOV_TAG('h', 'v', 'c', '1') // H.265
  37. #define MOV_MP4V MOV_TAG('m', 'p', '4', 'v') // MPEG-4 Video
  38. #define MOV_MP4A MOV_TAG('m', 'p', '4', 'a') // AAC
  39. #define MOV_MP4S MOV_TAG('m', 'p', '4', 's') // ISO/IEC 14496-14:2003(E) 5.6 Sample Description Boxes (p14)
  40. #define MOV_OPUS MOV_TAG('O', 'p', 'u', 's') // http://www.opus-codec.org/docs/opus_in_isobmff.html
  41. #define MOV_VP8 MOV_TAG('v', 'p', '0', '8')
  42. #define MOV_VP9 MOV_TAG('v', 'p', '0', '9') // https://www.webmproject.org/vp9/mp4/
  43. #define MOV_VP10 MOV_TAG('v', 'p', '1', '0')
  44. #define MOV_AV1 MOV_TAG('a', 'v', '0', '1') // https://aomediacodec.github.io/av1-isobmff
  45. #define MOV_VC1 MOV_TAG('v', 'c', '-', '1')
  46. #define MOV_DIRAC MOV_TAG('d', 'r', 'a', 'c')
  47. #define MOV_AC3 MOV_TAG('a', 'c', '-', '3')
  48. #define MOV_DTS MOV_TAG('d', 't', 's', 'c') // DTS-HD
  49. // ISO/IEC 14496-1:2010(E) 7.2.6.6 DecoderConfigDescriptor
  50. // Table 6 - streamType Values (p51)
  51. enum
  52. {
  53. MP4_STREAM_ODS = 0x01, /* ObjectDescriptorStream */
  54. MP4_STREAM_CRS = 0x02, /* ClockReferenceStream */
  55. MP4_STREAM_SDS = 0x03, /* SceneDescriptionStream */
  56. MP4_STREAM_VISUAL = 0x04, /* VisualStream */
  57. MP4_STREAM_AUDIO = 0x05, /* AudioStream */
  58. MP4_STREAM_MP7 = 0x06, /* MPEG7Stream */
  59. MP4_STREAM_IPMP = 0x07, /* IPMPStream */
  60. MP4_STREAM_OCIS = 0x08, /* ObjectContentInfoStream */
  61. MP4_STREAM_MPEGJ = 0x09, /* MPEGJStream */
  62. MP4_STREAM_IS = 0x0A, /* Interaction Stream */
  63. MP4_STREAM_IPMPTOOL = 0x0B, /* IPMPToolStream */
  64. };
  65. enum
  66. {
  67. MOV_BRAND_ISOM = MOV_TAG('i', 's', 'o', 'm'),
  68. MOV_BRAND_AVC1 = MOV_TAG('a', 'v', 'c', '1'),
  69. MOV_BRAND_ISO2 = MOV_TAG('i', 's', 'o', '2'),
  70. MOV_BRAND_MP71 = MOV_TAG('m', 'p', '7', '1'),
  71. MOV_BRAND_ISO3 = MOV_TAG('i', 's', 'o', '3'),
  72. MOV_BRAND_ISO4 = MOV_TAG('i', 's', 'o', '4'),
  73. MOV_BRAND_ISO5 = MOV_TAG('i', 's', 'o', '5'),
  74. MOV_BRAND_ISO6 = MOV_TAG('i', 's', 'o', '6'),
  75. MOV_BRAND_MP41 = MOV_TAG('m', 'p', '4', '1'), // ISO/IEC 14496-1:2001 MP4 File Format v1
  76. MOV_BRAND_MP42 = MOV_TAG('m', 'p', '4', '2'), // ISO/IEC 14496-14:2003 MP4 File Format v2
  77. MOV_BRAND_MOV = MOV_TAG('q', 't', ' ', ' '), // Apple Quick-Time File Format
  78. MOV_BRAND_DASH = MOV_TAG('d', 'a', 's', 'h'), // MPEG-DASH
  79. MOV_BRAND_MSDH = MOV_TAG('m', 's', 'd', 'h'), // MPEG-DASH
  80. MOV_BRAND_MSIX = MOV_TAG('m', 's', 'i', 'x'), // MPEG-DASH
  81. };
  82. #define MOV_TREX_FLAG_IS_LEADING_MASK 0x0C000000
  83. #define MOV_TREX_FLAG_SAMPLE_DEPENDS_ON_MASK 0x03000000
  84. #define MOV_TREX_FLAG_SAMPLE_IS_DEPENDED_ON_MASK 0x00C00000
  85. #define MOV_TREX_FLAG_SAMPLE_HAS_REDUNDANCY_MASK 0x00300000
  86. #define MOV_TREX_FLAG_SAMPLE_PADDING_VALUE_MASK 0x000E0000
  87. #define MOV_TREX_FLAG_SAMPLE_IS_NO_SYNC_SAMPLE 0x00010000
  88. #define MOV_TREX_FLAG_SAMPLE_DEGRADATION_PRIORITY_MASK 0x0000FFFF
  89. // 8.6.4 Independent and Disposable Samples Box (p55)
  90. #define MOV_TREX_FLAG_SAMPLE_DEPENDS_ON_I_PICTURE 0x02000000
  91. #define MOV_TREX_FLAG_SAMPLE_DEPENDS_ON_NOT_I_PICTURE 0x01000000
  92. #define MOV_TFHD_FLAG_BASE_DATA_OFFSET 0x00000001
  93. #define MOV_TFHD_FLAG_SAMPLE_DESCRIPTION_INDEX 0x00000002
  94. #define MOV_TFHD_FLAG_DEFAULT_DURATION 0x00000008
  95. #define MOV_TFHD_FLAG_DEFAULT_SIZE 0x00000010
  96. #define MOV_TFHD_FLAG_DEFAULT_FLAGS 0x00000020
  97. #define MOV_TFHD_FLAG_DURATION_IS_EMPTY 0x00010000
  98. #define MOV_TFHD_FLAG_DEFAULT_BASE_IS_MOOF 0x00020000
  99. #define MOV_TRUN_FLAG_DATA_OFFSET_PRESENT 0x0001
  100. #define MOV_TRUN_FLAG_FIRST_SAMPLE_FLAGS_PRESENT 0x0004
  101. #define MOV_TRUN_FLAG_SAMPLE_DURATION_PRESENT 0x0100
  102. #define MOV_TRUN_FLAG_SAMPLE_SIZE_PRESENT 0x0200
  103. #define MOV_TRUN_FLAG_SAMPLE_FLAGS_PRESENT 0x0400
  104. #define MOV_TRUN_FLAG_SAMPLE_COMPOSITION_TIME_OFFSET_PRESENT 0x0800
  105. #define MOV_TRACK_FLAG_CTTS_V1 0x0001 //ctts version 1
  106. struct mov_stbl_t
  107. {
  108. struct mov_stsc_t* stsc;
  109. size_t stsc_count;
  110. uint64_t* stco;
  111. uint32_t stco_count;
  112. struct mov_stts_t* stts;
  113. size_t stts_count;
  114. struct mov_stts_t* ctts;
  115. size_t ctts_count;
  116. uint32_t* stss; // sample_number, start from 1
  117. size_t stss_count;
  118. };
  119. struct mov_sample_t
  120. {
  121. int flags; // MOV_AV_FLAG_KEYFREAME
  122. int64_t pts; // track mdhd timescale
  123. int64_t dts;
  124. void* data;
  125. uint64_t offset; // is a 32 or 64 bit integer that gives the offset of the start of a chunk into its containing media file.
  126. uint32_t bytes;
  127. uint32_t sample_description_index;
  128. uint32_t samples_per_chunk; // write only
  129. uint32_t first_chunk; // write only
  130. };
  131. struct mov_fragment_t
  132. {
  133. uint64_t time;
  134. uint64_t offset; // moof offset
  135. };
  136. struct mov_track_t
  137. {
  138. uint32_t tag; // MOV_H264/MOV_MP4A
  139. uint32_t handler_type; // MOV_VIDEO/MOV_AUDIO
  140. const char* handler_descr; // VideoHandler/SoundHandler/SubtitleHandler
  141. struct mov_tkhd_t tkhd;
  142. struct mov_mdhd_t mdhd;
  143. struct mov_stbl_t stbl;
  144. // 8.8 Movie Fragments
  145. struct mov_trex_t trex;
  146. struct mov_tfhd_t tfhd;
  147. struct mov_fragment_t* frags;
  148. uint32_t frag_count, frag_capacity /*offset for read*/;
  149. struct mov_stsd_t stsd;
  150. struct mov_elst_t* elst;
  151. size_t elst_count;
  152. struct mov_sample_t* samples;
  153. uint32_t sample_count;
  154. size_t sample_offset; // sample_capacity
  155. int64_t tfdt_dts; // tfdt baseMediaDecodeTime
  156. int64_t start_dts; // write fmp4 only
  157. uint64_t offset; // write only
  158. int64_t last_dts; // write fmp4 only
  159. int64_t turn_last_duration; // write fmp4 only
  160. unsigned int flags;
  161. };
  162. struct mov_t
  163. {
  164. struct mov_ioutil_t io;
  165. struct mov_ftyp_t ftyp;
  166. struct mov_mvhd_t mvhd;
  167. int flags;
  168. int header;
  169. uint32_t mfro; // mfro size
  170. uint64_t moof_offset; // last moof offset(from file begin)
  171. uint64_t implicit_offset;
  172. struct mov_track_t* track; // current stream
  173. struct mov_track_t* tracks;
  174. int track_count;
  175. const void* udta;
  176. uint64_t udta_size;
  177. };
  178. int mov_reader_root(struct mov_t* mov);
  179. int mov_reader_box(struct mov_t* mov, const struct mov_box_t* parent);
  180. int mp4_read_extra(struct mov_t* mov, const struct mov_box_t* parent);
  181. int mov_read_ftyp(struct mov_t* mov, const struct mov_box_t* box);
  182. int mov_read_mvhd(struct mov_t* mov, const struct mov_box_t* box);
  183. int mov_read_tkhd(struct mov_t* mov, const struct mov_box_t* box);
  184. int mov_read_hdlr(struct mov_t* mov, const struct mov_box_t* box);
  185. int mov_read_mdhd(struct mov_t* mov, const struct mov_box_t* box);
  186. int mov_read_vmhd(struct mov_t* mov, const struct mov_box_t* box);
  187. int mov_read_smhd(struct mov_t* mov, const struct mov_box_t* box);
  188. int mov_read_nmhd(struct mov_t* mov, const struct mov_box_t* box);
  189. int mov_read_esds(struct mov_t* mov, const struct mov_box_t* box);
  190. int mov_read_elst(struct mov_t* mov, const struct mov_box_t* box);
  191. int mov_read_stsd(struct mov_t* mov, const struct mov_box_t* box);
  192. int mov_read_stsz(struct mov_t* mov, const struct mov_box_t* box);
  193. int mov_read_stz2(struct mov_t* mov, const struct mov_box_t* box);
  194. int mov_read_stsc(struct mov_t* mov, const struct mov_box_t* box);
  195. int mov_read_stco(struct mov_t* mov, const struct mov_box_t* box);
  196. int mov_read_stts(struct mov_t* mov, const struct mov_box_t* box);
  197. int mov_read_ctts(struct mov_t* mov, const struct mov_box_t* box);
  198. int mov_read_cslg(struct mov_t* mov, const struct mov_box_t* box);
  199. int mov_read_stss(struct mov_t* mov, const struct mov_box_t* box);
  200. int mov_read_avcc(struct mov_t* mov, const struct mov_box_t* box);
  201. int mov_read_hvcc(struct mov_t* mov, const struct mov_box_t* box);
  202. int mov_read_av1c(struct mov_t* mov, const struct mov_box_t* box);
  203. int mov_read_vpcc(struct mov_t* mov, const struct mov_box_t* box);
  204. int mov_read_tx3g(struct mov_t* mov, const struct mov_box_t* box);
  205. int mov_read_trex(struct mov_t* mov, const struct mov_box_t* box);
  206. int mov_read_leva(struct mov_t* mov, const struct mov_box_t* box);
  207. int mov_read_tfhd(struct mov_t* mov, const struct mov_box_t* box);
  208. int mov_read_trun(struct mov_t* mov, const struct mov_box_t* box);
  209. int mov_read_tfra(struct mov_t* mov, const struct mov_box_t* box);
  210. int mov_read_sidx(struct mov_t* mov, const struct mov_box_t* box);
  211. int mov_read_mfhd(struct mov_t* mov, const struct mov_box_t* box);
  212. int mov_read_tfdt(struct mov_t* mov, const struct mov_box_t* box);
  213. int mov_read_mehd(struct mov_t* mov, const struct mov_box_t* box);
  214. int mov_read_dops(struct mov_t* mov, const struct mov_box_t* box);
  215. int mov_read_pasp(struct mov_t* mov, const struct mov_box_t* box);
  216. int mov_read_gmin(struct mov_t* mov, const struct mov_box_t* box);
  217. int mov_read_text(struct mov_t* mov, const struct mov_box_t* box);
  218. int mov_read_smdm(struct mov_t* mov, const struct mov_box_t* box);
  219. int mov_read_coll(struct mov_t* mov, const struct mov_box_t* box);
  220. int mov_read_udta(struct mov_t* mov, const struct mov_box_t* box);
  221. size_t mov_write_ftyp(const struct mov_t* mov);
  222. size_t mov_write_mvhd(const struct mov_t* mov);
  223. size_t mov_write_mdhd(const struct mov_t* mov);
  224. size_t mov_write_tkhd(const struct mov_t* mov);
  225. size_t mov_write_hdlr(const struct mov_t* mov);
  226. size_t mov_write_vmhd(const struct mov_t* mov);
  227. size_t mov_write_smhd(const struct mov_t* mov);
  228. size_t mov_write_nmhd(const struct mov_t* mov);
  229. size_t mov_write_sthd(const struct mov_t* mov);
  230. size_t mov_write_dinf(const struct mov_t* mov);
  231. size_t mov_write_dref(const struct mov_t* mov);
  232. size_t mov_write_elst(const struct mov_t* mov);
  233. size_t mov_write_stsd(const struct mov_t* mov);
  234. size_t mov_write_stts(const struct mov_t* mov, uint32_t count);
  235. size_t mov_write_ctts(const struct mov_t* mov, uint32_t count);
  236. size_t mov_write_stco(const struct mov_t* mov, uint32_t count);
  237. size_t mov_write_stss(const struct mov_t* mov);
  238. size_t mov_write_stsc(const struct mov_t* mov);
  239. size_t mov_write_stsz(const struct mov_t* mov);
  240. size_t mov_write_esds(const struct mov_t* mov);
  241. size_t mov_write_avcc(const struct mov_t* mov);
  242. size_t mov_write_hvcc(const struct mov_t* mov);
  243. size_t mov_write_av1c(const struct mov_t* mov);
  244. size_t mov_write_vpcc(const struct mov_t* mov);
  245. size_t mov_write_tx3g(const struct mov_t* mov);
  246. size_t mov_write_trex(const struct mov_t* mov);
  247. size_t mov_write_tfhd(const struct mov_t* mov);
  248. size_t mov_write_trun(const struct mov_t* mov, uint32_t from, uint32_t count, uint32_t offset);
  249. size_t mov_write_tfra(const struct mov_t* mov);
  250. size_t mov_write_styp(const struct mov_t* mov);
  251. size_t mov_write_tfdt(const struct mov_t* mov);
  252. size_t mov_write_sidx(const struct mov_t* mov, uint64_t offset);
  253. size_t mov_write_mfhd(const struct mov_t* mov, uint32_t fragment);
  254. size_t mov_write_edts(const struct mov_t* mov);
  255. size_t mov_write_stbl(const struct mov_t* mov);
  256. size_t mov_write_minf(const struct mov_t* mov);
  257. size_t mov_write_mdia(const struct mov_t* mov);
  258. size_t mov_write_trak(const struct mov_t* mov);
  259. size_t mov_write_dops(const struct mov_t* mov);
  260. size_t mov_write_udta(const struct mov_t* mov);
  261. uint32_t mov_build_stts(struct mov_track_t* track);
  262. uint32_t mov_build_ctts(struct mov_track_t* track);
  263. uint32_t mov_build_stco(struct mov_track_t* track);
  264. void mov_apply_stco(struct mov_track_t* track);
  265. void mov_apply_elst(struct mov_track_t *track);
  266. void mov_apply_stts(struct mov_track_t* track);
  267. void mov_apply_ctts(struct mov_track_t* track);
  268. void mov_apply_stss(struct mov_track_t* track);
  269. void mov_apply_elst_tfdt(struct mov_track_t *track);
  270. void mov_write_size(const struct mov_t* mov, uint64_t offset, size_t size);
  271. size_t mov_stco_size(const struct mov_track_t* track, uint64_t offset);
  272. int mov_fragment_read_next_moof(struct mov_t* mov);
  273. int mov_fragment_seek_read_mfra(struct mov_t* mov);
  274. int mov_fragment_seek(struct mov_t* mov, int64_t* timestamp);
  275. uint8_t mov_tag_to_object(uint32_t tag);
  276. uint32_t mov_object_to_tag(uint8_t object);
  277. void mov_free_track(struct mov_track_t* track);
  278. struct mov_track_t* mov_add_track(struct mov_t* mov);
  279. struct mov_track_t* mov_find_track(const struct mov_t* mov, uint32_t track);
  280. struct mov_track_t* mov_fetch_track(struct mov_t* mov, uint32_t track); // find and add
  281. int mov_add_audio(struct mov_track_t* track, const struct mov_mvhd_t* mvhd, uint32_t timescale, uint8_t object, int channel_count, int bits_per_sample, int sample_rate, const void* extra_data, size_t extra_data_size);
  282. int mov_add_video(struct mov_track_t* track, const struct mov_mvhd_t* mvhd, uint32_t timescale, uint8_t object, int width, int height, const void* extra_data, size_t extra_data_size);
  283. int mov_add_subtitle(struct mov_track_t* track, const struct mov_mvhd_t* mvhd, uint32_t timescale, uint8_t object, const void* extra_data, size_t extra_data_size);
  284. #endif /* !_mov_internal_h_ */