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 
31 #ifndef MP4FF_INTERNAL_H
32 #define MP4FF_INTERNAL_H
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif /* __cplusplus */
37 
38 #include "mp4ff_int_types.h"
39 #include <stdlib.h>
40 
41 #define MAX_TRACKS 1024
42 #define TRACK_UNKNOWN 0
43 #define TRACK_AUDIO   1
44 #define TRACK_VIDEO   2
45 #define TRACK_SYSTEM  3
46 
47 
48 #define SUBATOMIC 128
49 
50 /* atoms without subatoms */
51 #define ATOM_FTYP 129
52 #define ATOM_MDAT 130
53 #define ATOM_MVHD 131
54 #define ATOM_TKHD 132
55 #define ATOM_TREF 133
56 #define ATOM_MDHD 134
57 #define ATOM_VMHD 135
58 #define ATOM_SMHD 136
59 #define ATOM_HMHD 137
60 #define ATOM_STSD 138
61 #define ATOM_STTS 139
62 #define ATOM_STSZ 140
63 #define ATOM_STZ2 141
64 #define ATOM_STCO 142
65 #define ATOM_STSC 143
66 #define ATOM_MP4A 144
67 #define ATOM_MP4V 145
68 #define ATOM_MP4S 146
69 #define ATOM_ESDS 147
70 #define ATOM_META 148 /* iTunes Metadata box */
71 #define ATOM_NAME 149 /* iTunes Metadata name box */
72 #define ATOM_DATA 150 /* iTunes Metadata data box */
73 #define ATOM_CTTS 151
74 #define ATOM_FRMA 152
75 #define ATOM_IVIV 153
76 #define ATOM_PRIV 154
77 #define ATOM_USER 155
78 #define ATOM_KEY  156
79 
80 #define ATOM_ALBUM_ARTIST	157
81 #define ATOM_CONTENTGROUP   158
82 #define ATOM_LYRICS         159
83 #define ATOM_DESCRIPTION    160
84 #define ATOM_NETWORK        161
85 #define ATOM_SHOW           162
86 #define ATOM_EPISODENAME    163
87 #define ATOM_SORTTITLE      164
88 #define ATOM_SORTALBUM      165
89 #define ATOM_SORTARTIST     166
90 #define ATOM_SORTALBUMARTIST    167
91 #define ATOM_SORTWRITER     168
92 #define ATOM_SORTSHOW       169
93 #define ATOM_SEASON         170
94 #define ATOM_EPISODE        171
95 #define ATOM_PODCAST        172
96 
97 #define ATOM_UNKNOWN 255
98 #define ATOM_FREE ATOM_UNKNOWN
99 #define ATOM_SKIP ATOM_UNKNOWN
100 
101 /* atoms with subatoms */
102 #define ATOM_MOOV 1
103 #define ATOM_TRAK 2
104 #define ATOM_EDTS 3
105 #define ATOM_MDIA 4
106 #define ATOM_MINF 5
107 #define ATOM_STBL 6
108 #define ATOM_UDTA 7
109 #define ATOM_ILST 8 /* iTunes Metadata list */
110 #define ATOM_TITLE 9
111 #define ATOM_ARTIST 10
112 #define ATOM_WRITER 11
113 #define ATOM_ALBUM 12
114 #define ATOM_DATE 13
115 #define ATOM_TOOL 14
116 #define ATOM_COMMENT 15
117 #define ATOM_GENRE1 16
118 #define ATOM_TRACK 17
119 #define ATOM_DISC 18
120 #define ATOM_COMPILATION 19
121 #define ATOM_GENRE2 20
122 #define ATOM_TEMPO 21
123 #define ATOM_COVER 22
124 #define ATOM_DRMS 23
125 #define ATOM_SINF 24
126 #define ATOM_SCHI 25
127 
128 #ifdef HAVE_CONFIG_H
129 #include "../../config.h"
130 #endif
131 
132 #if !(defined(_WIN32) || defined(_WIN32_WCE))
133 #define stricmp strcasecmp
134 #else
135 #define stricmp _stricmp
136 #define strdup _strdup
137 #endif
138 
139 /* file callback structure */
140 typedef struct
141 {
142     uint32_t (*read)(void *user_data, void *buffer, uint32_t length);
143     uint32_t (*write)(void *udata, void *buffer, uint32_t length);
144     uint32_t (*seek)(void *user_data, uint64_t position);
145     uint32_t (*truncate)(void *user_data);
146     void *user_data;
147 } mp4ff_callback_t;
148 
149 
150 /* metadata tag structure */
151 typedef struct
152 {
153     char *item;
154     char *value;
155 } mp4ff_tag_t;
156 
157 /* metadata list structure */
158 typedef struct
159 {
160     mp4ff_tag_t *tags;
161     uint32_t count;
162 } mp4ff_metadata_t;
163 
164 
165 typedef struct
166 {
167     int32_t type;
168     int32_t channelCount;
169     int32_t sampleSize;
170     uint16_t sampleRate;
171     int32_t audioType;
172 
173     /* stsd */
174     int32_t stsd_entry_count;
175 
176     /* stsz */
177     int32_t stsz_sample_size;
178     int32_t stsz_sample_count;
179     int32_t *stsz_table;
180 
181     /* stts */
182     int32_t stts_entry_count;
183     int32_t *stts_sample_count;
184     int32_t *stts_sample_delta;
185 
186     /* stsc */
187     int32_t stsc_entry_count;
188     int32_t *stsc_first_chunk;
189     int32_t *stsc_samples_per_chunk;
190     int32_t *stsc_sample_desc_index;
191 
192     /* stsc */
193     int32_t stco_entry_count;
194     int32_t *stco_chunk_offset;
195 
196     /* ctts */
197     int32_t ctts_entry_count;
198     int32_t *ctts_sample_count;
199     int32_t *ctts_sample_offset;
200 
201     /* esde */
202     uint8_t *decoderConfig;
203     int32_t decoderConfigLen;
204 
205     uint32_t maxBitrate;
206     uint32_t avgBitrate;
207 
208     uint32_t timeScale;
209     uint64_t duration;
210 
211 } mp4ff_track_t;
212 
213 /* mp4 main file structure */
214 typedef struct
215 {
216     /* stream to read from */
217     mp4ff_callback_t *stream;
218     int64_t current_position;
219 
220     int32_t moov_read;
221     uint64_t moov_offset;
222     uint64_t moov_size;
223     uint8_t last_atom;
224     uint64_t file_size;
225 
226     /* mvhd */
227     int32_t time_scale;
228     int32_t duration;
229 
230     /* incremental track index while reading the file */
231     int32_t total_tracks;
232 
233     /* track data */
234     mp4ff_track_t *track[MAX_TRACKS];
235 
236     /* metadata */
237     mp4ff_metadata_t tags;
238 } mp4ff_t;
239 
240 
241 
242 
243 /* mp4util.c */
244 int32_t mp4ff_read_data(mp4ff_t *f, int8_t *data, uint32_t size);
245 int32_t mp4ff_write_data(mp4ff_t *f, int8_t *data, uint32_t size);
246 uint64_t mp4ff_read_int64(mp4ff_t *f);
247 uint32_t mp4ff_read_int32(mp4ff_t *f);
248 uint32_t mp4ff_read_int24(mp4ff_t *f);
249 uint16_t mp4ff_read_int16(mp4ff_t *f);
250 uint8_t mp4ff_read_char(mp4ff_t *f);
251 int32_t mp4ff_write_int32(mp4ff_t *f,const uint32_t data);
252 uint32_t mp4ff_read_mp4_descr_length(mp4ff_t *f);
253 int64_t mp4ff_position(const mp4ff_t *f);
254 int32_t mp4ff_set_position(mp4ff_t *f, const int64_t position);
255 int32_t mp4ff_truncate(mp4ff_t * f);
256 char * mp4ff_read_string(mp4ff_t * f,uint32_t length);
257 
258 /* mp4atom.c */
259 static int32_t mp4ff_atom_get_size(const int8_t *data);
260 static int32_t mp4ff_atom_compare(const int8_t a1, const int8_t b1, const int8_t c1, const int8_t d1,
261                                   const int8_t a2, const int8_t b2, const int8_t c2, const int8_t d2);
262 static uint8_t mp4ff_atom_name_to_type(const int8_t a, const int8_t b, const int8_t c, const int8_t d);
263 uint64_t mp4ff_atom_read_header(mp4ff_t *f, uint8_t *atom_type, uint8_t *header_size);
264 static int32_t mp4ff_read_stsz(mp4ff_t *f);
265 static int32_t mp4ff_read_esds(mp4ff_t *f);
266 static int32_t mp4ff_read_mp4a(mp4ff_t *f);
267 static int32_t mp4ff_read_stsd(mp4ff_t *f);
268 static int32_t mp4ff_read_stsc(mp4ff_t *f);
269 static int32_t mp4ff_read_stco(mp4ff_t *f);
270 static int32_t mp4ff_read_stts(mp4ff_t *f);
271 #ifdef USE_TAGGING
272 static int32_t mp4ff_read_meta(mp4ff_t *f, const uint64_t size);
273 #endif
274 int32_t mp4ff_atom_read(mp4ff_t *f, const int32_t size, const uint8_t atom_type);
275 
276 /* mp4sample.c */
277 static int32_t mp4ff_chunk_of_sample(const mp4ff_t *f, const int32_t track, const int32_t sample,
278                                      int32_t *chunk_sample, int32_t *chunk);
279 static int32_t mp4ff_chunk_to_offset(const mp4ff_t *f, const int32_t track, const int32_t chunk);
280 static int32_t mp4ff_sample_range_size(const mp4ff_t *f, const int32_t track,
281                                        const int32_t chunk_sample, const int32_t sample);
282 static int32_t mp4ff_sample_to_offset(const mp4ff_t *f, const int32_t track, const int32_t sample);
283 int32_t mp4ff_audio_frame_size(const mp4ff_t *f, const int32_t track, const int32_t sample);
284 int32_t mp4ff_set_sample_position(mp4ff_t *f, const int32_t track, const int32_t sample);
285 
286 #ifdef USE_TAGGING
287 /* mp4meta.c */
288 static int32_t mp4ff_tag_add_field(mp4ff_metadata_t *tags, const char *item, const char *value);
289 static int32_t mp4ff_tag_set_field(mp4ff_metadata_t *tags, const char *item, const char *value);
290 static int32_t mp4ff_set_metadata_name(mp4ff_t *f, const uint8_t atom_type, char **name);
291 static int32_t mp4ff_parse_tag(mp4ff_t *f, const uint8_t parent_atom_type, const int32_t size);
292 static int32_t mp4ff_meta_find_by_name(const mp4ff_t *f, const char *item, char **value);
293 int32_t mp4ff_parse_metadata(mp4ff_t *f, const int32_t size);
294 int32_t mp4ff_tag_delete(mp4ff_metadata_t *tags);
295 int32_t mp4ff_meta_get_num_items(const mp4ff_t *f);
296 int32_t mp4ff_meta_get_by_index(const mp4ff_t *f, uint32_t index,
297                             char **item, char **value);
298 int32_t mp4ff_meta_get_title(const mp4ff_t *f, char **value);
299 int32_t mp4ff_meta_get_artist(const mp4ff_t *f, char **value);
300 int32_t mp4ff_meta_get_writer(const mp4ff_t *f, char **value);
301 int32_t mp4ff_meta_get_album(const mp4ff_t *f, char **value);
302 int32_t mp4ff_meta_get_date(const mp4ff_t *f, char **value);
303 int32_t mp4ff_meta_get_tool(const mp4ff_t *f, char **value);
304 int32_t mp4ff_meta_get_comment(const mp4ff_t *f, char **value);
305 int32_t mp4ff_meta_get_genre(const mp4ff_t *f, char **value);
306 int32_t mp4ff_meta_get_track(const mp4ff_t *f, char **value);
307 int32_t mp4ff_meta_get_disc(const mp4ff_t *f, char **value);
308 int32_t mp4ff_meta_get_compilation(const mp4ff_t *f, char **value);
309 int32_t mp4ff_meta_get_tempo(const mp4ff_t *f, char **value);
310 int32_t mp4ff_meta_get_coverart(const mp4ff_t *f, char **value);
311 #endif
312 
313 /* mp4ff.c */
314 mp4ff_t *mp4ff_open_read(mp4ff_callback_t *f);
315 #ifdef USE_TAGGING
316 mp4ff_t *mp4ff_open_edit(mp4ff_callback_t *f);
317 #endif
318 void mp4ff_close(mp4ff_t *ff);
319 //void mp4ff_track_add(mp4ff_t *f);
320 int32_t parse_sub_atoms(mp4ff_t *f, const uint64_t total_size,int meta_only);
321 int32_t parse_atoms(mp4ff_t *f,int meta_only);
322 
323 int32_t mp4ff_get_sample_duration(const mp4ff_t *f, const int32_t track, const int32_t sample);
324 int64_t mp4ff_get_sample_position(const mp4ff_t *f, const int32_t track, const int32_t sample);
325 int32_t mp4ff_get_sample_offset(const mp4ff_t *f, const int32_t track, const int32_t sample);
326 int32_t mp4ff_find_sample(const mp4ff_t *f, const int32_t track, const int64_t offset,int32_t * toskip);
327 
328 int32_t mp4ff_read_sample(mp4ff_t *f, const int32_t track, const int32_t sample,
329                           uint8_t **audio_buffer,  uint32_t *bytes);
330 int32_t mp4ff_get_decoder_config(const mp4ff_t *f, const int32_t track,
331                                  uint8_t** ppBuf, uint32_t* pBufSize);
332 int32_t mp4ff_total_tracks(const mp4ff_t *f);
333 int32_t mp4ff_time_scale(const mp4ff_t *f, const int32_t track);
334 int32_t mp4ff_num_samples(const mp4ff_t *f, const int32_t track);
335 
336 uint32_t mp4ff_meta_genre_to_index(const char * genrestr);//returns 1-based index, 0 if not found
337 const char * mp4ff_meta_index_to_genre(uint32_t idx);//returns pointer to static string
338 
339 
340 #ifdef __cplusplus
341 }
342 #endif /* __cplusplus */
343 
344 #endif
345