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.

820 lines
24 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*******************************************************************************
  2. mp4_io.h - A library for general MPEG4 I/O.
  3. Copyright (C) 2007-2009 CodeShop B.V.
  4. http://www.code-shop.com
  5. For licensing see the LICENSE file
  6. ******************************************************************************/
  7. #ifndef MP4_IO_H_AKW
  8. #define MP4_IO_H_AKW
  9. #include "mod_streaming_export.h"
  10. #ifndef _MSC_VER
  11. #include <inttypes.h>
  12. #else
  13. #include "inttypes.h"
  14. #endif
  15. #include <stdio.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #ifdef UNUSED
  20. #elif defined(__GNUC__)
  21. # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
  22. #elif defined(__LCLINT__)
  23. # define UNUSED(x) /*@unused@*/ x
  24. #else
  25. # define UNUSED(x) x
  26. #endif
  27. #ifdef WIN32
  28. #define ftello _ftelli64
  29. #define fseeko _fseeki64
  30. // #define strdup _strdup
  31. #endif
  32. #define ATOM_PREAMBLE_SIZE 8
  33. #define MAX_TRACKS 8
  34. #define FOURCC(a, b, c, d) ((uint32_t)(a) << 24) + \
  35. ((uint32_t)(b) << 16) + \
  36. ((uint32_t)(c) << 8) + \
  37. ((uint32_t)(d))
  38. #define MP4_INFO(fmt, ...) \
  39. if(mp4_context->verbose_ > 2) \
  40. { \
  41. mp4_log_trace("%s.%d: (info) " fmt, remove_path(__FILE__), __LINE__, __VA_ARGS__); \
  42. }
  43. #define MP4_WARNING(fmt, ...) \
  44. if(mp4_context->verbose_ > 1) \
  45. { \
  46. mp4_log_trace("%s.%d: (warning) " fmt, remove_path(__FILE__), __LINE__, __VA_ARGS__); \
  47. }
  48. #define MP4_ERROR(fmt, ...) \
  49. if(mp4_context->verbose_ > 0) \
  50. { \
  51. mp4_log_trace("%s.%d: (error) " fmt, remove_path(__FILE__), __LINE__, __VA_ARGS__); \
  52. }
  53. MOD_STREAMING_DLL_LOCAL extern uint64_t atoi64(const char* val);
  54. MOD_STREAMING_DLL_LOCAL extern const char* remove_path(const char *path);
  55. MOD_STREAMING_DLL_LOCAL extern void mp4_log_trace(const char* fmt, ...);
  56. MOD_STREAMING_DLL_LOCAL extern unsigned int read_8(unsigned char const* buffer);
  57. MOD_STREAMING_DLL_LOCAL extern unsigned char* write_8(unsigned char* buffer, unsigned int v);
  58. MOD_STREAMING_DLL_LOCAL extern uint16_t read_16(unsigned char const* buffer);
  59. MOD_STREAMING_DLL_LOCAL extern unsigned char* write_16(unsigned char* buffer, unsigned int v);
  60. MOD_STREAMING_DLL_LOCAL extern unsigned int read_24(unsigned char const* buffer);
  61. MOD_STREAMING_DLL_LOCAL extern unsigned char* write_24(unsigned char* buffer, unsigned int v);
  62. MOD_STREAMING_DLL_LOCAL extern uint32_t read_32(unsigned char const* buffer);
  63. MOD_STREAMING_DLL_LOCAL extern unsigned char* write_32(unsigned char* buffer, uint32_t v);
  64. MOD_STREAMING_DLL_LOCAL extern uint64_t read_64(unsigned char const* buffer);
  65. MOD_STREAMING_DLL_LOCAL extern unsigned char* write_64(unsigned char* buffer, uint64_t v);
  66. MOD_STREAMING_DLL_LOCAL extern uint32_t read_n(unsigned char const* buffer, unsigned int n);
  67. MOD_STREAMING_DLL_LOCAL extern unsigned char* write_n(unsigned char* buffer, unsigned int n, uint32_t v);
  68. struct mem_range_t
  69. {
  70. int read_only_;
  71. uint64_t filesize_;
  72. int fd_;
  73. // original base mapping
  74. void* mmap_addr_;
  75. uint64_t mmap_offset_;
  76. uint64_t mmap_size_;
  77. #ifdef WIN32
  78. void* fileMapHandle_;
  79. #endif
  80. };
  81. typedef struct mem_range_t mem_range_t;
  82. MOD_STREAMING_DLL_LOCAL extern mem_range_t* mem_range_init_read(char const* filename);
  83. MOD_STREAMING_DLL_LOCAL extern mem_range_t* mem_range_init_write(char const* filename,
  84. uint64_t offset, uint64_t len);
  85. MOD_STREAMING_DLL_LOCAL extern void* mem_range_map(mem_range_t* mem_range, uint64_t offset, uint32_t len);
  86. MOD_STREAMING_DLL_LOCAL extern void mem_range_exit(mem_range_t* mem_range);
  87. struct mp4_atom_t
  88. {
  89. uint32_t type_;
  90. uint32_t short_size_;
  91. uint64_t size_;
  92. uint64_t start_;
  93. uint64_t end_;
  94. };
  95. typedef struct mp4_atom_t mp4_atom_t;
  96. struct mp4_context_t;
  97. MOD_STREAMING_DLL_LOCAL extern
  98. int mp4_atom_read_header(struct mp4_context_t const* mp4_context,
  99. FILE* infile, mp4_atom_t* atom);
  100. MOD_STREAMING_DLL_LOCAL extern
  101. int mp4_atom_write_header(unsigned char* outbuffer,
  102. mp4_atom_t const* atom);
  103. struct unknown_atom_t
  104. {
  105. void* atom_;
  106. struct unknown_atom_t* next_;
  107. };
  108. typedef struct unknown_atom_t unknown_atom_t;
  109. MOD_STREAMING_DLL_LOCAL extern unknown_atom_t* unknown_atom_init(void);
  110. MOD_STREAMING_DLL_LOCAL extern void unknown_atom_exit(unknown_atom_t* atom);
  111. struct moov_t
  112. {
  113. struct unknown_atom_t* unknown_atoms_;
  114. struct mvhd_t* mvhd_;
  115. unsigned int tracks_;
  116. struct trak_t* traks_[MAX_TRACKS];
  117. struct mvex_t* mvex_;
  118. int is_indexed_;
  119. };
  120. typedef struct moov_t moov_t;
  121. MOD_STREAMING_DLL_LOCAL extern moov_t* moov_init(void);
  122. MOD_STREAMING_DLL_LOCAL extern void moov_exit(moov_t* atom);
  123. struct mvhd_t
  124. {
  125. unsigned int version_;
  126. unsigned int flags_;
  127. uint64_t creation_time_; // seconds since midnite, Jan .1 1904 (UTC)
  128. uint64_t modification_time_; // seconds since midnite, Jan .1 1904 (UTC)
  129. uint32_t timescale_; // time units that pass in one second
  130. uint64_t duration_; // duration of the longest track
  131. uint32_t rate_; // preferred playback rate (16.16)
  132. uint16_t volume_; // preferred playback volume (8.8)
  133. uint16_t reserved1_;
  134. uint32_t reserved2_[2];
  135. uint32_t matrix_[9];
  136. uint32_t predefined_[6];
  137. uint32_t next_track_id_;
  138. };
  139. typedef struct mvhd_t mvhd_t;
  140. MOD_STREAMING_DLL_LOCAL extern mvhd_t* mvhd_init(void);
  141. MOD_STREAMING_DLL_LOCAL extern mvhd_t* mvhd_copy(mvhd_t const* rhs);
  142. MOD_STREAMING_DLL_LOCAL extern void mvhd_exit(mvhd_t* atom);
  143. struct trak_t
  144. {
  145. struct unknown_atom_t* unknown_atoms_;
  146. struct tkhd_t* tkhd_;
  147. struct mdia_t* mdia_;
  148. struct edts_t* edts_;
  149. unsigned int chunks_size_;
  150. struct chunks_t* chunks_;
  151. unsigned int samples_size_;
  152. struct samples_t* samples_;
  153. // current pts when reading fragments
  154. // uint64_t fragment_pts_;
  155. };
  156. typedef struct trak_t trak_t;
  157. MOD_STREAMING_DLL_LOCAL extern trak_t* trak_init(void);
  158. MOD_STREAMING_DLL_LOCAL extern unsigned int trak_bitrate(trak_t const* trak);
  159. MOD_STREAMING_DLL_LOCAL extern void trak_exit(trak_t* trak);
  160. struct tkhd_t
  161. {
  162. unsigned int version_;
  163. unsigned int flags_;
  164. uint64_t creation_time_; // seconds since midnite, Jan .1 1904 (UTC)
  165. uint64_t modification_time_; // seconds since midnite, Jan .1 1904 (UTC)
  166. uint32_t track_id_;
  167. uint32_t reserved_;
  168. uint64_t duration_; // duration of this track (mvhd.timescale)
  169. uint32_t reserved2_[2];
  170. uint16_t layer_; // front-to-back ordering
  171. uint16_t predefined_;
  172. uint16_t volume_; // relative audio volume (8.8)
  173. uint16_t reserved3_;
  174. uint32_t matrix_[9]; // transformation matrix
  175. uint32_t width_; // visual presentation width (16.16)
  176. uint32_t height_; // visual presentation height (16.16)
  177. };
  178. typedef struct tkhd_t tkhd_t;
  179. MOD_STREAMING_DLL_LOCAL extern tkhd_t* tkhd_init(void);
  180. MOD_STREAMING_DLL_LOCAL extern tkhd_t* tkhd_copy(tkhd_t const* rhs);
  181. MOD_STREAMING_DLL_LOCAL extern void tkhd_exit(tkhd_t* tkhd);
  182. struct mdia_t
  183. {
  184. struct unknown_atom_t* unknown_atoms_;
  185. struct mdhd_t* mdhd_;
  186. struct hdlr_t* hdlr_;
  187. struct minf_t* minf_;
  188. };
  189. typedef struct mdia_t mdia_t;
  190. MOD_STREAMING_DLL_LOCAL extern mdia_t* mdia_init(void);
  191. MOD_STREAMING_DLL_LOCAL extern void mdia_exit(mdia_t* atom);
  192. struct elst_table_t
  193. {
  194. uint64_t segment_duration_;
  195. int64_t media_time_;
  196. int16_t media_rate_integer_;
  197. int16_t media_rate_fraction_;
  198. };
  199. typedef struct elst_table_t elst_table_t;
  200. struct elst_t
  201. {
  202. unsigned int version_;
  203. unsigned int flags_;
  204. uint32_t entry_count_;
  205. struct elst_table_t* table_;
  206. };
  207. typedef struct elst_t elst_t;
  208. MOD_STREAMING_DLL_LOCAL extern elst_t* elst_init(void);
  209. MOD_STREAMING_DLL_LOCAL extern void elst_exit(elst_t* atom);
  210. struct edts_t
  211. {
  212. struct unknown_atom_t* unknown_atoms_;
  213. struct elst_t* elst_;
  214. };
  215. typedef struct edts_t edts_t;
  216. MOD_STREAMING_DLL_LOCAL extern edts_t* edts_init(void);
  217. MOD_STREAMING_DLL_LOCAL extern void edts_exit(edts_t* atom);
  218. struct mdhd_t
  219. {
  220. unsigned int version_;
  221. unsigned int flags_;
  222. uint64_t creation_time_; // seconds since midnite, Jan .1 1904 (UTC)
  223. uint64_t modification_time_; // seconds since midnite, Jan .1 1904 (UTC)
  224. uint32_t timescale_; // time units that pass in one second
  225. uint64_t duration_; // duration of this media
  226. unsigned int language_[3]; // language code for this media (ISO 639-2/T)
  227. uint16_t predefined_;
  228. };
  229. typedef struct mdhd_t mdhd_t;
  230. MOD_STREAMING_DLL_LOCAL extern struct mdhd_t* mdhd_init(void);
  231. MOD_STREAMING_DLL_LOCAL extern mdhd_t* mdhd_copy(mdhd_t const* rhs);
  232. MOD_STREAMING_DLL_LOCAL extern void mdhd_exit(struct mdhd_t* mdhd);
  233. struct hdlr_t
  234. {
  235. unsigned int version_;
  236. unsigned int flags_;
  237. uint32_t predefined_;
  238. uint32_t handler_type_; // format of the contents ('vide', 'soun', ...)
  239. uint32_t reserved1_;
  240. uint32_t reserved2_;
  241. uint32_t reserved3_;
  242. char* name_; // human-readable name for the track type (UTF8)
  243. };
  244. typedef struct hdlr_t hdlr_t;
  245. MOD_STREAMING_DLL_LOCAL extern hdlr_t* hdlr_init(void);
  246. MOD_STREAMING_DLL_LOCAL extern hdlr_t* hdlr_copy(hdlr_t const* rhs);
  247. MOD_STREAMING_DLL_LOCAL extern void hdlr_exit(hdlr_t* atom);
  248. struct minf_t
  249. {
  250. struct unknown_atom_t* unknown_atoms_;
  251. struct vmhd_t* vmhd_;
  252. struct smhd_t* smhd_;
  253. struct dinf_t* dinf_;
  254. struct stbl_t* stbl_;
  255. };
  256. typedef struct minf_t minf_t;
  257. MOD_STREAMING_DLL_LOCAL extern minf_t* minf_init(void);
  258. MOD_STREAMING_DLL_LOCAL extern void minf_exit(minf_t* atom);
  259. struct vmhd_t
  260. {
  261. unsigned int version_;
  262. unsigned int flags_;
  263. uint16_t graphics_mode_; // composition mode (0=copy)
  264. uint16_t opcolor_[3];
  265. };
  266. typedef struct vmhd_t vmhd_t;
  267. MOD_STREAMING_DLL_LOCAL extern vmhd_t* vmhd_init(void);
  268. MOD_STREAMING_DLL_LOCAL extern vmhd_t* vmhd_copy(vmhd_t* rhs);
  269. MOD_STREAMING_DLL_LOCAL extern void vmhd_exit(vmhd_t* atom);
  270. struct smhd_t
  271. {
  272. unsigned int version_;
  273. unsigned int flags_;
  274. uint16_t balance_; // place mono audio tracks in stereo space (8.8)
  275. uint16_t reserved_;
  276. };
  277. typedef struct smhd_t smhd_t;
  278. MOD_STREAMING_DLL_LOCAL extern smhd_t* smhd_init(void);
  279. MOD_STREAMING_DLL_LOCAL extern smhd_t* smhd_copy(smhd_t* rhs);
  280. MOD_STREAMING_DLL_LOCAL extern void smhd_exit(smhd_t* atom);
  281. struct dinf_t
  282. {
  283. struct dref_t* dref_; // declares the location of the media info
  284. };
  285. typedef struct dinf_t dinf_t;
  286. MOD_STREAMING_DLL_LOCAL extern dinf_t* dinf_init(void);
  287. MOD_STREAMING_DLL_LOCAL extern dinf_t* dinf_copy(dinf_t* rhs);
  288. MOD_STREAMING_DLL_LOCAL extern void dinf_exit(dinf_t* atom);
  289. struct dref_table_t
  290. {
  291. unsigned int flags_; // 0x000001 is self contained
  292. char* name_; // name is a URN
  293. char* location_; // location is a URL
  294. };
  295. typedef struct dref_table_t dref_table_t;
  296. MOD_STREAMING_DLL_LOCAL extern void dref_table_init(dref_table_t* entry);
  297. MOD_STREAMING_DLL_LOCAL extern void dref_table_assign(dref_table_t* lhs, dref_table_t const* rhs);
  298. MOD_STREAMING_DLL_LOCAL extern void dref_table_exit(dref_table_t* entry);
  299. struct dref_t
  300. {
  301. unsigned int version_;
  302. unsigned int flags_;
  303. unsigned int entry_count_;
  304. dref_table_t* table_;
  305. };
  306. typedef struct dref_t dref_t;
  307. MOD_STREAMING_DLL_LOCAL extern dref_t* dref_init(void);
  308. MOD_STREAMING_DLL_LOCAL extern dref_t* dref_copy(dref_t const* rhs);
  309. MOD_STREAMING_DLL_LOCAL extern void dref_exit(dref_t* atom);
  310. struct stbl_t
  311. {
  312. struct unknown_atom_t* unknown_atoms_;
  313. struct stsd_t* stsd_; // sample description
  314. struct stts_t* stts_; // decoding time-to-sample
  315. struct stss_t* stss_; // sync sample
  316. struct stsc_t* stsc_; // sample-to-chunk
  317. struct stsz_t* stsz_; // sample size
  318. struct stco_t* stco_; // chunk offset
  319. struct ctts_t* ctts_; // composition time-to-sample
  320. };
  321. typedef struct stbl_t stbl_t;
  322. MOD_STREAMING_DLL_LOCAL extern stbl_t* stbl_init(void);
  323. MOD_STREAMING_DLL_LOCAL extern void stbl_exit(stbl_t* atom);
  324. MOD_STREAMING_DLL_LOCAL extern
  325. unsigned int stbl_get_nearest_keyframe(stbl_t const* stbl, unsigned int sample);
  326. struct stsd_t
  327. {
  328. unsigned int version_;
  329. unsigned int flags_;
  330. uint32_t entries_;
  331. struct sample_entry_t* sample_entries_;
  332. };
  333. typedef struct stsd_t stsd_t;
  334. MOD_STREAMING_DLL_LOCAL extern stsd_t* stsd_init(void);
  335. MOD_STREAMING_DLL_LOCAL extern stsd_t* stsd_copy(stsd_t const* rhs);
  336. MOD_STREAMING_DLL_LOCAL extern void stsd_exit(stsd_t* atom);
  337. struct video_sample_entry_t
  338. {
  339. uint16_t version_;
  340. uint16_t revision_level_;
  341. uint32_t vendor_;
  342. uint32_t temporal_quality_;
  343. uint32_t spatial_quality_;
  344. uint16_t width_;
  345. uint16_t height_;
  346. uint32_t horiz_resolution_; // pixels per inch (16.16)
  347. uint32_t vert_resolution_; // pixels per inch (16.16)
  348. uint32_t data_size_;
  349. uint16_t frame_count_; // number of frames in each sample
  350. uint8_t compressor_name_[32]; // informative purposes (pascal string)
  351. uint16_t depth_; // images are in colour with no alpha (24)
  352. int16_t color_table_id_;
  353. };
  354. typedef struct video_sample_entry_t video_sample_entry_t;
  355. MOD_STREAMING_DLL_LOCAL extern video_sample_entry_t* video_sample_entry_init(void);
  356. struct audio_sample_entry_t
  357. {
  358. uint16_t version_;
  359. uint16_t revision_;
  360. uint32_t vendor_;
  361. uint16_t channel_count_; // mono(1), stereo(2)
  362. uint16_t sample_size_; // (bits)
  363. uint16_t compression_id_;
  364. uint16_t packet_size_;
  365. uint32_t samplerate_; // sampling rate (16.16)
  366. };
  367. typedef struct audio_sample_entry_t audio_sample_entry_t;
  368. MOD_STREAMING_DLL_LOCAL extern audio_sample_entry_t* audio_sample_entry_init(void);
  369. struct sample_entry_t
  370. {
  371. unsigned int len_;
  372. uint32_t fourcc_;
  373. unsigned char* buf_;
  374. struct video_sample_entry_t* video_;
  375. struct audio_sample_entry_t* audio_;
  376. //struct hint_sample_entry_t* hint_;
  377. unsigned int codec_private_data_length_;
  378. unsigned char const* codec_private_data_;
  379. // avcC
  380. unsigned int nal_unit_length_;
  381. unsigned int sps_length_;
  382. unsigned char* sps_;
  383. unsigned int pps_length_;
  384. unsigned char* pps_;
  385. // sound (WAVEFORMATEX) structure
  386. uint16_t wFormatTag;
  387. uint16_t nChannels;
  388. uint32_t nSamplesPerSec;
  389. uint32_t nAvgBytesPerSec;
  390. uint16_t nBlockAlign;
  391. uint16_t wBitsPerSample;
  392. unsigned int samplerate_hi_;
  393. unsigned int samplerate_lo_;
  394. // esds
  395. unsigned int max_bitrate_;
  396. unsigned int avg_bitrate_;
  397. };
  398. typedef struct sample_entry_t sample_entry_t;
  399. MOD_STREAMING_DLL_LOCAL extern
  400. void sample_entry_init(sample_entry_t* sample_entry);
  401. MOD_STREAMING_DLL_LOCAL extern
  402. void sample_entry_assign(sample_entry_t* lhs, sample_entry_t const* rhs);
  403. MOD_STREAMING_DLL_LOCAL extern
  404. void sample_entry_exit(sample_entry_t* sample_entry);
  405. MOD_STREAMING_DLL_LOCAL extern
  406. void sample_entry_get_adts(sample_entry_t const* sample_entry,
  407. unsigned int sample_size, uint8_t* buf);
  408. struct stts_t
  409. {
  410. unsigned int version_;
  411. unsigned int flags_;
  412. uint32_t entries_;
  413. struct stts_table_t* table_;
  414. };
  415. typedef struct stts_t stts_t;
  416. MOD_STREAMING_DLL_LOCAL extern stts_t* stts_init(void);
  417. MOD_STREAMING_DLL_LOCAL extern void stts_exit(stts_t* atom);
  418. MOD_STREAMING_DLL_LOCAL extern unsigned int stts_get_sample(stts_t const* stts, uint64_t time);
  419. MOD_STREAMING_DLL_LOCAL extern uint64_t stts_get_time(stts_t const* stts, unsigned int sample);
  420. MOD_STREAMING_DLL_LOCAL extern uint64_t stts_get_duration(stts_t const* stts);
  421. MOD_STREAMING_DLL_LOCAL extern unsigned int stts_get_samples(stts_t const* stts);
  422. struct stts_table_t
  423. {
  424. uint32_t sample_count_;
  425. uint32_t sample_duration_;
  426. };
  427. typedef struct stts_table_t stts_table_t;
  428. struct stss_t
  429. {
  430. unsigned int version_;
  431. unsigned int flags_;
  432. uint32_t entries_;
  433. uint32_t* sample_numbers_;
  434. };
  435. typedef struct stss_t stss_t;
  436. MOD_STREAMING_DLL_LOCAL extern stss_t* stss_init(void);
  437. MOD_STREAMING_DLL_LOCAL extern void stss_exit(stss_t* atom);
  438. MOD_STREAMING_DLL_LOCAL extern
  439. unsigned int stss_get_nearest_keyframe(stss_t const* stss, unsigned int sample);
  440. struct stsc_t
  441. {
  442. unsigned int version_;
  443. unsigned int flags_;
  444. uint32_t entries_;
  445. struct stsc_table_t* table_;
  446. };
  447. typedef struct stsc_t stsc_t;
  448. MOD_STREAMING_DLL_LOCAL extern stsc_t* stsc_init(void);
  449. MOD_STREAMING_DLL_LOCAL extern void stsc_exit(stsc_t* atom);
  450. struct stsc_table_t
  451. {
  452. uint32_t chunk_;
  453. uint32_t samples_;
  454. uint32_t id_;
  455. };
  456. typedef struct stsc_table_t stsc_table_t;
  457. struct stsz_t
  458. {
  459. unsigned int version_;
  460. unsigned int flags_;
  461. uint32_t sample_size_;
  462. uint32_t entries_;
  463. uint32_t* sample_sizes_;
  464. };
  465. typedef struct stsz_t stsz_t;
  466. MOD_STREAMING_DLL_LOCAL extern stsz_t* stsz_init(void);
  467. MOD_STREAMING_DLL_LOCAL extern void stsz_exit(stsz_t* atom);
  468. struct stco_t
  469. {
  470. unsigned int version_;
  471. unsigned int flags_;
  472. uint32_t entries_;
  473. uint64_t* chunk_offsets_;
  474. void* stco_inplace_; // newly generated stco (patched inplace)
  475. };
  476. typedef struct stco_t stco_t;
  477. MOD_STREAMING_DLL_LOCAL extern stco_t* stco_init(void);
  478. MOD_STREAMING_DLL_LOCAL extern void stco_exit(stco_t* atom);
  479. struct ctts_t
  480. {
  481. unsigned int version_;
  482. unsigned int flags_;
  483. uint32_t entries_;
  484. struct ctts_table_t* table_;
  485. };
  486. typedef struct ctts_t ctts_t;
  487. MOD_STREAMING_DLL_LOCAL extern ctts_t* ctts_init(void);
  488. MOD_STREAMING_DLL_LOCAL extern void ctts_exit(ctts_t* atom);
  489. MOD_STREAMING_DLL_LOCAL extern unsigned int ctts_get_samples(ctts_t const* ctts);
  490. struct ctts_table_t
  491. {
  492. uint32_t sample_count_;
  493. uint32_t sample_offset_;
  494. };
  495. typedef struct ctts_table_t ctts_table_t;
  496. struct samples_t
  497. {
  498. uint64_t pts_; // decoding/presentation time
  499. unsigned int size_; // size in bytes
  500. uint64_t pos_; // byte offset
  501. unsigned int cto_; // composition time offset
  502. unsigned int is_ss_:1; // sync sample
  503. unsigned int is_smooth_ss_:1; // sync sample for smooth streaming
  504. };
  505. typedef struct samples_t samples_t;
  506. struct chunks_t
  507. {
  508. unsigned int sample_; // number of the first sample in the chunk
  509. unsigned int size_; // number of samples in the chunk
  510. int id_; // not used
  511. uint64_t pos_; // start byte position of chunk
  512. };
  513. typedef struct chunks_t chunks_t;
  514. MOD_STREAMING_DLL_LOCAL extern
  515. uint64_t moov_time_to_trak_time(uint64_t t, long moov_time_scale,
  516. long trak_time_scale);
  517. MOD_STREAMING_DLL_LOCAL extern
  518. uint64_t trak_time_to_moov_time(uint64_t t, long moov_time_scale,
  519. long trak_time_scale);
  520. struct mvex_t
  521. {
  522. struct unknown_atom_t* unknown_atoms_;
  523. unsigned int tracks_;
  524. struct trex_t* trexs_[MAX_TRACKS];
  525. };
  526. typedef struct mvex_t mvex_t;
  527. MOD_STREAMING_DLL_LOCAL extern mvex_t* mvex_init(void);
  528. MOD_STREAMING_DLL_LOCAL extern void mvex_exit(mvex_t* mvex);
  529. struct trex_t
  530. {
  531. unsigned int version_;
  532. unsigned int flags_;
  533. uint32_t track_id_;
  534. uint32_t default_sample_description_index_;
  535. uint32_t default_sample_duration_;
  536. uint32_t default_sample_size_;
  537. uint32_t default_sample_flags_;
  538. };
  539. typedef struct trex_t trex_t;
  540. MOD_STREAMING_DLL_LOCAL extern trex_t* trex_init(void);
  541. MOD_STREAMING_DLL_LOCAL extern void trex_exit(trex_t* trex);
  542. struct moof_t
  543. {
  544. struct unknown_atom_t* unknown_atoms_;
  545. struct mfhd_t* mfhd_;
  546. unsigned int tracks_;
  547. struct traf_t* trafs_[MAX_TRACKS];
  548. };
  549. typedef struct moof_t moof_t;
  550. MOD_STREAMING_DLL_LOCAL extern moof_t* moof_init(void);
  551. MOD_STREAMING_DLL_LOCAL extern void moof_exit(moof_t* atom);
  552. struct mfhd_t
  553. {
  554. unsigned int version_;
  555. unsigned int flags_;
  556. // the ordinal number of this fragment, in increasing order
  557. uint32_t sequence_number_;
  558. };
  559. typedef struct mfhd_t mfhd_t;
  560. MOD_STREAMING_DLL_LOCAL extern mfhd_t* mfhd_init(void);
  561. MOD_STREAMING_DLL_LOCAL extern void mfhd_exit(mfhd_t* atom);
  562. struct traf_t
  563. {
  564. struct unknown_atom_t* unknown_atoms_;
  565. struct tfhd_t* tfhd_;
  566. struct trun_t* trun_;
  567. struct uuid0_t* uuid0_;
  568. struct uuid1_t* uuid1_;
  569. };
  570. typedef struct traf_t traf_t;
  571. MOD_STREAMING_DLL_LOCAL extern traf_t* traf_init(void);
  572. MOD_STREAMING_DLL_LOCAL extern void traf_exit(traf_t* atom);
  573. struct tfhd_t
  574. {
  575. unsigned int version_;
  576. unsigned int flags_;
  577. uint32_t track_id_;
  578. // all the following are optional fields
  579. uint64_t base_data_offset_;
  580. uint32_t sample_description_index_;
  581. uint32_t default_sample_duration_;
  582. uint32_t default_sample_size_;
  583. uint32_t default_sample_flags_;
  584. };
  585. typedef struct tfhd_t tfhd_t;
  586. MOD_STREAMING_DLL_LOCAL extern tfhd_t* tfhd_init(void);
  587. MOD_STREAMING_DLL_LOCAL extern void tfhd_exit(tfhd_t* atom);
  588. struct tfra_table_t
  589. {
  590. uint64_t time_;
  591. uint64_t moof_offset_;
  592. uint32_t traf_number_;
  593. uint32_t trun_number_;
  594. uint32_t sample_number_;
  595. };
  596. typedef struct tfra_table_t tfra_table_t;
  597. struct tfra_t
  598. {
  599. unsigned int version_;
  600. unsigned int flags_;
  601. uint32_t track_id_;
  602. unsigned int length_size_of_traf_num_;
  603. unsigned int length_size_of_trun_num_;
  604. unsigned int length_size_of_sample_num_;
  605. uint32_t number_of_entry_;
  606. struct tfra_table_t* table_;
  607. };
  608. typedef struct tfra_t tfra_t;
  609. MOD_STREAMING_DLL_LOCAL extern tfra_t* tfra_init(void);
  610. MOD_STREAMING_DLL_LOCAL extern void tfra_exit(tfra_t* tfra);
  611. MOD_STREAMING_DLL_LOCAL extern void tfra_add(tfra_t* tfra, tfra_table_t const* table);
  612. struct mfra_t
  613. {
  614. struct unknown_atom_t* unknown_atoms_;
  615. unsigned int tracks_;
  616. struct tfra_t* tfras_[MAX_TRACKS];
  617. };
  618. typedef struct mfra_t mfra_t;
  619. MOD_STREAMING_DLL_LOCAL extern mfra_t* mfra_init(void);
  620. MOD_STREAMING_DLL_LOCAL extern void mfra_exit(mfra_t* atom);
  621. struct trun_table_t
  622. {
  623. uint32_t sample_duration_;
  624. uint32_t sample_size_;
  625. uint32_t sample_flags_;
  626. uint32_t sample_composition_time_offset_;
  627. };
  628. typedef struct trun_table_t trun_table_t;
  629. struct trun_t
  630. {
  631. unsigned int version_;
  632. unsigned int flags_;
  633. // the number of samples being added in this fragment; also the number of rows
  634. // in the following table (the rows can be empty)
  635. uint32_t sample_count_;
  636. // is added to the implicit or explicit data_offset established in the track
  637. // fragment header
  638. int32_t data_offset_;
  639. // provides a set of flags for the first sample only of this run
  640. uint32_t first_sample_flags_;
  641. trun_table_t* table_;
  642. // additional info for uuid
  643. // trak_t const* trak_;
  644. // unsigned int start_;
  645. struct trun_t* next_;
  646. };
  647. typedef struct trun_t trun_t;
  648. MOD_STREAMING_DLL_LOCAL extern struct trun_t* trun_init(void);
  649. MOD_STREAMING_DLL_LOCAL extern void trun_exit(struct trun_t* atom);
  650. struct uuid0_t
  651. {
  652. uint64_t pts_;
  653. uint64_t duration_;
  654. };
  655. typedef struct uuid0_t uuid0_t;
  656. MOD_STREAMING_DLL_LOCAL extern uuid0_t* uuid0_init(void);
  657. MOD_STREAMING_DLL_LOCAL extern void uuid0_exit(uuid0_t* atom);
  658. struct uuid1_t
  659. {
  660. unsigned int entries_;
  661. uint64_t pts_[2];
  662. uint64_t duration_[2];
  663. };
  664. typedef struct uuid1_t uuid1_t;
  665. MOD_STREAMING_DLL_LOCAL extern uuid1_t* uuid1_init(void);
  666. MOD_STREAMING_DLL_LOCAL extern void uuid1_exit(uuid1_t* atom);
  667. // random access structure similar to mfra, but with size field
  668. struct rxs_t
  669. {
  670. uint64_t time_;
  671. uint64_t offset_;
  672. uint64_t size_;
  673. };
  674. typedef struct rxs_t rxs_t;
  675. #define MP4_ELEMENTARY_STREAM_DESCRIPTOR_TAG 3
  676. #define MP4_DECODER_CONFIG_DESCRIPTOR_TAG 4
  677. #define MP4_DECODER_SPECIFIC_DESCRIPTOR_TAG 5
  678. #define MP4_MPEG4Audio 0x40
  679. #define MP4_MPEG2AudioMain 0x66
  680. #define MP4_MPEG2AudioLowComplexity 0x67
  681. #define MP4_MPEG2AudioScaleableSamplingRate 0x68
  682. #define MP4_MPEG2AudioPart3 0x69
  683. #define MP4_MPEG1Audio 0x6b
  684. struct mp4_context_t
  685. {
  686. char* filename_;
  687. FILE* infile;
  688. int verbose_;
  689. // the atoms as found in the stream
  690. mp4_atom_t ftyp_atom;
  691. mp4_atom_t moov_atom;
  692. mp4_atom_t mdat_atom;
  693. mp4_atom_t mfra_atom;
  694. // the actual binary data
  695. unsigned char* moov_data;
  696. unsigned char* mfra_data;
  697. // the parsed atoms
  698. moov_t* moov;
  699. // the file offset of a moof atom, used for base_data_offset
  700. uint64_t moof_offset_;
  701. };
  702. typedef struct mp4_context_t mp4_context_t;
  703. enum mp4_open_flags
  704. {
  705. MP4_OPEN_MOOV = 0x00000001,
  706. MP4_OPEN_MOOF = 0x00000002,
  707. MP4_OPEN_MDAT = 0x00000004,
  708. MP4_OPEN_MFRA = 0x00000008,
  709. MP4_OPEN_ALL = 0x0000000f
  710. };
  711. typedef enum mp4_open_flags mp4_open_flags;
  712. MOD_STREAMING_DLL_LOCAL extern
  713. mp4_context_t* mp4_open(const char* filename, int64_t filesize,
  714. mp4_open_flags flags, int verbose);
  715. MOD_STREAMING_DLL_LOCAL extern void mp4_close(mp4_context_t* mp4_context);
  716. #ifdef __cplusplus
  717. } /* extern C definitions */
  718. #endif
  719. #endif // MP4_IO_H_AKW
  720. // End Of File