mp4ffint.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
  3. ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
  4. **
  5. ** This program is free software; you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation; either version 2 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. **
  19. ** Any non-GPL usage of this software or parts of this software is strictly
  20. ** forbidden.
  21. **
  22. ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
  23. ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
  24. **
  25. ** Commercial non-GPL licensing of this software is possible.
  26. ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
  27. **
  28. ** $Id: mp4ffint.h,v 1.26 2009/01/25 20:14:34 menno Exp $
  29. **/
  30. #ifndef MP4FF_INTERNAL_H
  31. #define MP4FF_INTERNAL_H
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif /* __cplusplus */
  35. #include "mp4ff_int_types.h"
  36. #include <stdlib.h>
  37. #define MAX_TRACKS 1024
  38. #define TRACK_UNKNOWN 0
  39. #define TRACK_AUDIO 1
  40. #define TRACK_VIDEO 2
  41. #define TRACK_SYSTEM 3
  42. #define SUBATOMIC 128
  43. /* atoms without subatoms */
  44. #define ATOM_FTYP 129
  45. #define ATOM_MDAT 130
  46. #define ATOM_MVHD 131
  47. #define ATOM_TKHD 132
  48. #define ATOM_TREF 133
  49. #define ATOM_MDHD 134
  50. #define ATOM_VMHD 135
  51. #define ATOM_SMHD 136
  52. #define ATOM_HMHD 137
  53. #define ATOM_STSD 138
  54. #define ATOM_STTS 139
  55. #define ATOM_STSZ 140
  56. #define ATOM_STZ2 141
  57. #define ATOM_STCO 142
  58. #define ATOM_STSC 143
  59. #define ATOM_MP4A 144
  60. #define ATOM_MP4V 145
  61. #define ATOM_MP4S 146
  62. #define ATOM_ESDS 147
  63. #define ATOM_META 148 /* iTunes Metadata box */
  64. #define ATOM_NAME 149 /* iTunes Metadata name box */
  65. #define ATOM_DATA 150 /* iTunes Metadata data box */
  66. #define ATOM_CTTS 151
  67. #define ATOM_FRMA 152
  68. #define ATOM_IVIV 153
  69. #define ATOM_PRIV 154
  70. #define ATOM_USER 155
  71. #define ATOM_KEY 156
  72. #define ATOM_ALBUM_ARTIST 157
  73. #define ATOM_CONTENTGROUP 158
  74. #define ATOM_LYRICS 159
  75. #define ATOM_DESCRIPTION 160
  76. #define ATOM_NETWORK 161
  77. #define ATOM_SHOW 162
  78. #define ATOM_EPISODENAME 163
  79. #define ATOM_SORTTITLE 164
  80. #define ATOM_SORTALBUM 165
  81. #define ATOM_SORTARTIST 166
  82. #define ATOM_SORTALBUMARTIST 167
  83. #define ATOM_SORTWRITER 168
  84. #define ATOM_SORTSHOW 169
  85. #define ATOM_SEASON 170
  86. #define ATOM_EPISODE 171
  87. #define ATOM_PODCAST 172
  88. #define ATOM_UNKNOWN 255
  89. #define ATOM_FREE ATOM_UNKNOWN
  90. #define ATOM_SKIP ATOM_UNKNOWN
  91. /* atoms with subatoms */
  92. #define ATOM_MOOV 1
  93. #define ATOM_TRAK 2
  94. #define ATOM_EDTS 3
  95. #define ATOM_MDIA 4
  96. #define ATOM_MINF 5
  97. #define ATOM_STBL 6
  98. #define ATOM_UDTA 7
  99. #define ATOM_ILST 8 /* iTunes Metadata list */
  100. #define ATOM_TITLE 9
  101. #define ATOM_ARTIST 10
  102. #define ATOM_WRITER 11
  103. #define ATOM_ALBUM 12
  104. #define ATOM_DATE 13
  105. #define ATOM_TOOL 14
  106. #define ATOM_COMMENT 15
  107. #define ATOM_GENRE1 16
  108. #define ATOM_TRACK 17
  109. #define ATOM_DISC 18
  110. #define ATOM_COMPILATION 19
  111. #define ATOM_GENRE2 20
  112. #define ATOM_TEMPO 21
  113. #define ATOM_COVER 22
  114. #define ATOM_DRMS 23
  115. #define ATOM_SINF 24
  116. #define ATOM_SCHI 25
  117. #ifdef HAVE_CONFIG_H
  118. #include "../../config.h"
  119. #endif
  120. #if !(defined(_WIN32) || defined(_WIN32_WCE))
  121. #define stricmp strcasecmp
  122. #else
  123. #define stricmp _stricmp
  124. #define strdup _strdup
  125. #endif
  126. /* file callback structure */
  127. typedef struct
  128. {
  129. uint32_t (*read)(void *user_data, void *buffer, uint32_t length);
  130. uint32_t (*write)(void *udata, void *buffer, uint32_t length);
  131. uint32_t (*seek)(void *user_data, uint64_t position);
  132. uint32_t (*truncate)(void *user_data);
  133. void *user_data;
  134. } mp4ff_callback_t;
  135. /* metadata tag structure */
  136. typedef struct
  137. {
  138. char *item;
  139. char *value;
  140. } mp4ff_tag_t;
  141. /* metadata list structure */
  142. typedef struct
  143. {
  144. mp4ff_tag_t *tags;
  145. uint32_t count;
  146. } mp4ff_metadata_t;
  147. typedef struct
  148. {
  149. int32_t type;
  150. int32_t channelCount;
  151. int32_t sampleSize;
  152. uint16_t sampleRate;
  153. int32_t audioType;
  154. /* stsd */
  155. int32_t stsd_entry_count;
  156. /* stsz */
  157. int32_t stsz_sample_size;
  158. int32_t stsz_sample_count;
  159. int32_t *stsz_table;
  160. /* stts */
  161. int32_t stts_entry_count;
  162. int32_t *stts_sample_count;
  163. int32_t *stts_sample_delta;
  164. /* stsc */
  165. int32_t stsc_entry_count;
  166. int32_t *stsc_first_chunk;
  167. int32_t *stsc_samples_per_chunk;
  168. int32_t *stsc_sample_desc_index;
  169. /* stsc */
  170. int32_t stco_entry_count;
  171. int32_t *stco_chunk_offset;
  172. /* ctts */
  173. int32_t ctts_entry_count;
  174. int32_t *ctts_sample_count;
  175. int32_t *ctts_sample_offset;
  176. /* esde */
  177. uint8_t *decoderConfig;
  178. int32_t decoderConfigLen;
  179. uint32_t maxBitrate;
  180. uint32_t avgBitrate;
  181. uint32_t timeScale;
  182. uint64_t duration;
  183. } mp4ff_track_t;
  184. /* mp4 main file structure */
  185. typedef struct
  186. {
  187. /* stream to read from */
  188. mp4ff_callback_t *stream;
  189. int64_t current_position;
  190. int32_t moov_read;
  191. uint64_t moov_offset;
  192. uint64_t moov_size;
  193. uint8_t last_atom;
  194. uint64_t file_size;
  195. /* mvhd */
  196. int32_t time_scale;
  197. int32_t duration;
  198. /* incremental track index while reading the file */
  199. int32_t total_tracks;
  200. /* track data */
  201. mp4ff_track_t *track[MAX_TRACKS];
  202. /* metadata */
  203. mp4ff_metadata_t tags;
  204. } mp4ff_t;
  205. /* mp4util.c */
  206. int32_t mp4ff_read_data(mp4ff_t *f, int8_t *data, uint32_t size);
  207. int32_t mp4ff_write_data(mp4ff_t *f, int8_t *data, uint32_t size);
  208. uint64_t mp4ff_read_int64(mp4ff_t *f);
  209. uint32_t mp4ff_read_int32(mp4ff_t *f);
  210. uint32_t mp4ff_read_int24(mp4ff_t *f);
  211. uint16_t mp4ff_read_int16(mp4ff_t *f);
  212. uint8_t mp4ff_read_char(mp4ff_t *f);
  213. int32_t mp4ff_write_int32(mp4ff_t *f,const uint32_t data);
  214. uint32_t mp4ff_read_mp4_descr_length(mp4ff_t *f);
  215. int64_t mp4ff_position(const mp4ff_t *f);
  216. int32_t mp4ff_set_position(mp4ff_t *f, const int64_t position);
  217. int32_t mp4ff_truncate(mp4ff_t * f);
  218. char * mp4ff_read_string(mp4ff_t * f,uint32_t length);
  219. /* mp4atom.c */
  220. static int32_t mp4ff_atom_get_size(const int8_t *data);
  221. static int32_t mp4ff_atom_compare(const int8_t a1, const int8_t b1, const int8_t c1, const int8_t d1,
  222. const int8_t a2, const int8_t b2, const int8_t c2, const int8_t d2);
  223. static uint8_t mp4ff_atom_name_to_type(const int8_t a, const int8_t b, const int8_t c, const int8_t d);
  224. uint64_t mp4ff_atom_read_header(mp4ff_t *f, uint8_t *atom_type, uint8_t *header_size);
  225. static int32_t mp4ff_read_stsz(mp4ff_t *f);
  226. static int32_t mp4ff_read_esds(mp4ff_t *f);
  227. static int32_t mp4ff_read_mp4a(mp4ff_t *f);
  228. static int32_t mp4ff_read_stsd(mp4ff_t *f);
  229. static int32_t mp4ff_read_stsc(mp4ff_t *f);
  230. static int32_t mp4ff_read_stco(mp4ff_t *f);
  231. static int32_t mp4ff_read_stts(mp4ff_t *f);
  232. #ifdef USE_TAGGING
  233. static int32_t mp4ff_read_meta(mp4ff_t *f, const uint64_t size);
  234. #endif
  235. int32_t mp4ff_atom_read(mp4ff_t *f, const int32_t size, const uint8_t atom_type);
  236. /* mp4sample.c */
  237. static int32_t mp4ff_chunk_of_sample(const mp4ff_t *f, const int32_t track, const int32_t sample,
  238. int32_t *chunk_sample, int32_t *chunk);
  239. static int32_t mp4ff_chunk_to_offset(const mp4ff_t *f, const int32_t track, const int32_t chunk);
  240. static int32_t mp4ff_sample_range_size(const mp4ff_t *f, const int32_t track,
  241. const int32_t chunk_sample, const int32_t sample);
  242. static int32_t mp4ff_sample_to_offset(const mp4ff_t *f, const int32_t track, const int32_t sample);
  243. int32_t mp4ff_audio_frame_size(const mp4ff_t *f, const int32_t track, const int32_t sample);
  244. int32_t mp4ff_set_sample_position(mp4ff_t *f, const int32_t track, const int32_t sample);
  245. #ifdef USE_TAGGING
  246. /* mp4meta.c */
  247. static int32_t mp4ff_tag_add_field(mp4ff_metadata_t *tags, const char *item, const char *value);
  248. static int32_t mp4ff_tag_set_field(mp4ff_metadata_t *tags, const char *item, const char *value);
  249. static int32_t mp4ff_set_metadata_name(mp4ff_t *f, const uint8_t atom_type, char **name);
  250. static int32_t mp4ff_parse_tag(mp4ff_t *f, const uint8_t parent_atom_type, const int32_t size);
  251. static int32_t mp4ff_meta_find_by_name(const mp4ff_t *f, const char *item, char **value);
  252. int32_t mp4ff_parse_metadata(mp4ff_t *f, const int32_t size);
  253. int32_t mp4ff_tag_delete(mp4ff_metadata_t *tags);
  254. int32_t mp4ff_meta_get_num_items(const mp4ff_t *f);
  255. int32_t mp4ff_meta_get_by_index(const mp4ff_t *f, uint32_t index,
  256. char **item, char **value);
  257. int32_t mp4ff_meta_get_title(const mp4ff_t *f, char **value);
  258. int32_t mp4ff_meta_get_artist(const mp4ff_t *f, char **value);
  259. int32_t mp4ff_meta_get_writer(const mp4ff_t *f, char **value);
  260. int32_t mp4ff_meta_get_album(const mp4ff_t *f, char **value);
  261. int32_t mp4ff_meta_get_date(const mp4ff_t *f, char **value);
  262. int32_t mp4ff_meta_get_tool(const mp4ff_t *f, char **value);
  263. int32_t mp4ff_meta_get_comment(const mp4ff_t *f, char **value);
  264. int32_t mp4ff_meta_get_genre(const mp4ff_t *f, char **value);
  265. int32_t mp4ff_meta_get_track(const mp4ff_t *f, char **value);
  266. int32_t mp4ff_meta_get_disc(const mp4ff_t *f, char **value);
  267. int32_t mp4ff_meta_get_compilation(const mp4ff_t *f, char **value);
  268. int32_t mp4ff_meta_get_tempo(const mp4ff_t *f, char **value);
  269. int32_t mp4ff_meta_get_coverart(const mp4ff_t *f, char **value);
  270. #endif
  271. /* mp4ff.c */
  272. mp4ff_t *mp4ff_open_read(mp4ff_callback_t *f);
  273. #ifdef USE_TAGGING
  274. mp4ff_t *mp4ff_open_edit(mp4ff_callback_t *f);
  275. #endif
  276. void mp4ff_close(mp4ff_t *ff);
  277. //void mp4ff_track_add(mp4ff_t *f);
  278. int32_t parse_sub_atoms(mp4ff_t *f, const uint64_t total_size,int meta_only);
  279. int32_t parse_atoms(mp4ff_t *f,int meta_only);
  280. int32_t mp4ff_get_sample_duration(const mp4ff_t *f, const int32_t track, const int32_t sample);
  281. int64_t mp4ff_get_sample_position(const mp4ff_t *f, const int32_t track, const int32_t sample);
  282. int32_t mp4ff_get_sample_offset(const mp4ff_t *f, const int32_t track, const int32_t sample);
  283. int32_t mp4ff_find_sample(const mp4ff_t *f, const int32_t track, const int64_t offset,int32_t * toskip);
  284. int32_t mp4ff_read_sample(mp4ff_t *f, const int32_t track, const int32_t sample,
  285. uint8_t **audio_buffer, uint32_t *bytes);
  286. int32_t mp4ff_get_decoder_config(const mp4ff_t *f, const int32_t track,
  287. uint8_t** ppBuf, uint32_t* pBufSize);
  288. int32_t mp4ff_total_tracks(const mp4ff_t *f);
  289. int32_t mp4ff_time_scale(const mp4ff_t *f, const int32_t track);
  290. int32_t mp4ff_num_samples(const mp4ff_t *f, const int32_t track);
  291. uint32_t mp4ff_meta_genre_to_index(const char * genrestr);//returns 1-based index, 0 if not found
  292. const char * mp4ff_meta_index_to_genre(uint32_t idx);//returns pointer to static string
  293. #ifdef __cplusplus
  294. }
  295. #endif /* __cplusplus */
  296. #endif