Mercurial > audlegacy
annotate Plugins/Input/aac/mp4ff/mp4meta.c @ 1637:5261e37b4d55 trunk
[svn] - fully working CoreAudio plugin, based on the OSS plugin and an incomplete xmms coreaudio plugin (fink)
author | nenolod |
---|---|
date | Thu, 07 Sep 2006 11:32:59 -0700 |
parents | 705d4c089fce |
children |
rev | line source |
---|---|
1020 | 1 /* |
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding | |
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software 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 | |
1459 | 17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
1020 | 18 ** |
19 ** Any non-GPL usage of this software or parts of this software is strictly | |
20 ** forbidden. | |
21 ** | |
22 ** Commercial non-GPL licensing of this software is possible. | |
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. | |
24 ** | |
25 ** $Id: mp4meta.c,v 1.13 2004/01/11 15:52:18 menno Exp $ | |
26 **/ | |
27 | |
28 #ifdef USE_TAGGING | |
29 | |
30 #include <stdlib.h> | |
31 #include <stdio.h> | |
32 #include <string.h> | |
33 #include "mp4ffint.h" | |
34 | |
35 int32_t mp4ff_tag_add_field(mp4ff_metadata_t *tags, const char *item, const char *value) | |
36 { | |
37 void *backup = (void *)tags->tags; | |
38 | |
39 if (!item || (item && !*item) || !value) return 0; | |
40 | |
41 tags->tags = (mp4ff_tag_t*)realloc(tags->tags, (tags->count+1) * sizeof(mp4ff_tag_t)); | |
42 if (!tags->tags) | |
43 { | |
44 if (backup) free(backup); | |
45 return 0; | |
46 } else { | |
47 tags->tags[tags->count].item = strdup(item); | |
48 tags->tags[tags->count].value = strdup(value); | |
49 | |
50 if (!tags->tags[tags->count].item || !tags->tags[tags->count].value) | |
51 { | |
52 if (!tags->tags[tags->count].item) free (tags->tags[tags->count].item); | |
53 if (!tags->tags[tags->count].value) free (tags->tags[tags->count].value); | |
54 tags->tags[tags->count].item = NULL; | |
55 tags->tags[tags->count].value = NULL; | |
56 return 0; | |
57 } | |
58 | |
59 tags->count++; | |
60 return 1; | |
61 } | |
62 } | |
63 | |
64 int32_t mp4ff_tag_set_field(mp4ff_metadata_t *tags, const char *item, const char *value) | |
65 { | |
66 unsigned int i; | |
67 | |
68 if (!item || (item && !*item) || !value) return 0; | |
69 | |
70 for (i = 0; i < tags->count; i++) | |
71 { | |
72 if (!stricmp(tags->tags[i].item, item)) | |
73 { | |
74 free(tags->tags[i].value); | |
75 tags->tags[i].value = strdup(value); | |
76 return 1; | |
77 } | |
78 } | |
79 | |
80 return mp4ff_tag_add_field(tags, item, value); | |
81 } | |
82 | |
83 int32_t mp4ff_tag_delete(mp4ff_metadata_t *tags) | |
84 { | |
85 uint32_t i; | |
86 | |
87 for (i = 0; i < tags->count; i++) | |
88 { | |
89 if (tags->tags[i].item) free(tags->tags[i].item); | |
90 if (tags->tags[i].value) free(tags->tags[i].value); | |
91 } | |
92 | |
93 if (tags->tags) free(tags->tags); | |
94 | |
95 tags->tags = NULL; | |
96 tags->count = 0; | |
97 | |
98 return 0; | |
99 } | |
100 | |
101 const char* ID3v1GenreList[] = { | |
102 "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", | |
103 "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", | |
104 "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", | |
105 "Techno", "Industrial", "Alternative", "Ska", "Death Metal", "Pranks", | |
106 "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", | |
107 "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", | |
108 "Game", "Sound Clip", "Gospel", "Noise", "AlternRock", "Bass", | |
109 "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock", | |
110 "Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", | |
111 "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta", | |
112 "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret", | |
113 "New Wave", "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", | |
114 "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical", | |
115 "Rock & Roll", "Hard Rock", "Folk", "Folk/Rock", "National Folk", "Swing", | |
116 "Fast-Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", | |
117 "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band", | |
118 "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson", | |
119 "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus", | |
120 "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba", | |
121 "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet", | |
122 "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall", | |
123 "Goa", "Drum & Bass", "Club House", "Hardcore", "Terror", | |
124 "Indie", "BritPop", "NegerPunk", "Polsk Punk", "Beat", | |
125 "Christian Gangsta", "Heavy Metal", "Black Metal", "Crossover", "Contemporary C", | |
126 "Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "JPop", | |
127 "SynthPop", | |
128 }; | |
129 | |
130 uint32_t mp4ff_meta_genre_to_index(const char * genrestr) | |
131 { | |
132 unsigned n; | |
133 for(n=0;n<sizeof(ID3v1GenreList)/sizeof(ID3v1GenreList[0]);n++) | |
134 { | |
135 if (!stricmp(genrestr,ID3v1GenreList[n])) return n+1; | |
136 } | |
137 return 0; | |
138 } | |
139 | |
140 const char * mp4ff_meta_index_to_genre(uint32_t idx) | |
141 { | |
142 if (idx>0 && idx<=sizeof(ID3v1GenreList)/sizeof(ID3v1GenreList[0])) | |
143 { | |
144 return ID3v1GenreList[idx-1]; | |
145 } | |
146 else | |
147 { | |
148 return 0; | |
149 } | |
150 } | |
151 | |
152 | |
153 int32_t TrackToString(char** str, const uint16_t track, const uint16_t totalTracks) | |
154 { | |
155 char temp[32]; | |
156 sprintf(temp, "%.5u of %.5u", track, totalTracks); | |
157 *str = strdup(temp); | |
158 return 0; | |
159 } | |
160 | |
1129
c59fe7000c95
[svn] Fully respect pointer signedness in libmp4ff.
chainsaw
parents:
1020
diff
changeset
|
161 int32_t mp4ff_set_metadata_name(mp4ff_t *f, const uint8_t atom_type, unsigned char **name) |
1020 | 162 { |
163 char *tag_names[] = { | |
164 "unknown", "title", "artist", "writer", "album", | |
165 "date", "tool", "comment", "genre", "track", | |
166 "disc", "compilation", "genre", "tempo", "cover" | |
167 }; | |
168 uint8_t tag_idx = 0; | |
169 | |
170 switch (atom_type) | |
171 { | |
172 case ATOM_TITLE: tag_idx = 1; break; | |
173 case ATOM_ARTIST: tag_idx = 2; break; | |
174 case ATOM_WRITER: tag_idx = 3; break; | |
175 case ATOM_ALBUM: tag_idx = 4; break; | |
176 case ATOM_DATE: tag_idx = 5; break; | |
177 case ATOM_TOOL: tag_idx = 6; break; | |
178 case ATOM_COMMENT: tag_idx = 7; break; | |
179 case ATOM_GENRE1: tag_idx = 8; break; | |
180 case ATOM_TRACK: tag_idx = 9; break; | |
181 case ATOM_DISC: tag_idx = 10; break; | |
182 case ATOM_COMPILATION: tag_idx = 11; break; | |
183 case ATOM_GENRE2: tag_idx = 12; break; | |
184 case ATOM_TEMPO: tag_idx = 13; break; | |
185 case ATOM_COVER: tag_idx = 14; break; | |
186 default: tag_idx = 0; break; | |
187 } | |
188 | |
1129
c59fe7000c95
[svn] Fully respect pointer signedness in libmp4ff.
chainsaw
parents:
1020
diff
changeset
|
189 *name = (unsigned char*)strdup(tag_names[tag_idx]); |
1020 | 190 |
191 return 0; | |
192 } | |
193 | |
194 int32_t mp4ff_parse_tag(mp4ff_t *f, const uint8_t parent_atom_type, const int32_t size) | |
195 { | |
196 uint8_t atom_type; | |
197 uint8_t header_size = 0; | |
198 uint64_t subsize, sumsize = 0; | |
1129
c59fe7000c95
[svn] Fully respect pointer signedness in libmp4ff.
chainsaw
parents:
1020
diff
changeset
|
199 unsigned char * name = NULL; |
c59fe7000c95
[svn] Fully respect pointer signedness in libmp4ff.
chainsaw
parents:
1020
diff
changeset
|
200 unsigned char * data = NULL; |
c59fe7000c95
[svn] Fully respect pointer signedness in libmp4ff.
chainsaw
parents:
1020
diff
changeset
|
201 uint32_t done = 0; |
1020 | 202 |
203 while (sumsize < size) | |
204 { | |
205 uint64_t destpos; | |
206 subsize = mp4ff_atom_read_header(f, &atom_type, &header_size); | |
207 destpos = mp4ff_position(f)+subsize-header_size; | |
208 if (!done) | |
209 { | |
210 if (atom_type == ATOM_DATA) | |
211 { | |
212 mp4ff_read_char(f); /* version */ | |
213 mp4ff_read_int24(f); /* flags */ | |
214 mp4ff_read_int32(f); /* reserved */ | |
215 | |
216 /* some need special attention */ | |
217 if (parent_atom_type == ATOM_GENRE2 || parent_atom_type == ATOM_TEMPO) | |
218 { | |
219 if (subsize - header_size >= 8 + 2) | |
220 { | |
221 uint16_t val = mp4ff_read_int16(f); | |
222 | |
223 if (parent_atom_type == ATOM_TEMPO) | |
224 { | |
225 char temp[16]; | |
226 sprintf(temp, "%.5u BPM", val); | |
227 mp4ff_tag_add_field(&(f->tags), "tempo", temp); | |
228 } | |
229 else | |
230 { | |
231 const char * temp = mp4ff_meta_index_to_genre(val); | |
232 if (temp) | |
233 { | |
234 mp4ff_tag_add_field(&(f->tags), "genre", temp); | |
235 } | |
236 } | |
237 done = 1; | |
238 } | |
239 } else if (parent_atom_type == ATOM_TRACK || parent_atom_type == ATOM_DISC) { | |
240 if (!done && subsize - header_size >= 8 + 8) | |
241 { | |
242 uint16_t index,total; | |
243 char temp[32]; | |
244 mp4ff_read_int16(f); | |
245 index = mp4ff_read_int16(f); | |
246 total = mp4ff_read_int16(f); | |
247 mp4ff_read_int16(f); | |
248 | |
249 sprintf(temp,"%d",index); | |
250 mp4ff_tag_add_field(&(f->tags), parent_atom_type == ATOM_TRACK ? "track" : "disc", temp); | |
251 if (total>0) | |
252 { | |
253 sprintf(temp,"%d",total); | |
254 mp4ff_tag_add_field(&(f->tags), parent_atom_type == ATOM_TRACK ? "totaltracks" : "totaldiscs", temp); | |
255 } | |
256 done = 1; | |
257 } | |
258 } else | |
259 { | |
260 if (data) {free(data);data = NULL;} | |
261 data = mp4ff_read_string(f,(uint32_t)(subsize-(header_size+8))); | |
262 } | |
263 } else if (atom_type == ATOM_NAME) { | |
264 if (!done) | |
265 { | |
266 mp4ff_read_char(f); /* version */ | |
267 mp4ff_read_int24(f); /* flags */ | |
268 if (name) free(name); | |
269 name = mp4ff_read_string(f,(uint32_t)(subsize-(header_size+4))); | |
270 } | |
271 } | |
272 mp4ff_set_position(f, destpos); | |
273 sumsize += subsize; | |
274 } | |
275 } | |
276 | |
277 if (data) | |
278 { | |
279 if (!done) | |
280 { | |
281 if (name == NULL) mp4ff_set_metadata_name(f, parent_atom_type, &name); | |
1129
c59fe7000c95
[svn] Fully respect pointer signedness in libmp4ff.
chainsaw
parents:
1020
diff
changeset
|
282 if (name) mp4ff_tag_add_field(&(f->tags), (char*)name, (char*)data); |
1020 | 283 } |
284 | |
285 free(data); | |
286 } | |
287 if (name) free(name); | |
288 return 1; | |
289 } | |
290 | |
291 int32_t mp4ff_parse_metadata(mp4ff_t *f, const int32_t size) | |
292 { | |
293 uint64_t subsize, sumsize = 0; | |
294 uint8_t atom_type; | |
295 uint8_t header_size = 0; | |
296 | |
297 while (sumsize < size) | |
298 { | |
299 subsize = mp4ff_atom_read_header(f, &atom_type, &header_size); | |
300 mp4ff_parse_tag(f, atom_type, (uint32_t)(subsize-header_size)); | |
301 sumsize += subsize; | |
302 } | |
303 | |
304 return 0; | |
305 } | |
306 | |
307 /* find a metadata item by name */ | |
308 /* returns 0 if item found, 1 if no such item */ | |
309 int32_t mp4ff_meta_find_by_name(const mp4ff_t *f, const char *item, char **value) | |
310 { | |
311 uint32_t i; | |
312 | |
313 for (i = 0; i < f->tags.count; i++) | |
314 { | |
315 if (!stricmp(f->tags.tags[i].item, item)) | |
316 { | |
317 *value = strdup(f->tags.tags[i].value); | |
318 return 1; | |
319 } | |
320 } | |
321 | |
322 *value = NULL; | |
323 | |
324 /* not found */ | |
325 return 0; | |
326 } | |
327 | |
328 int32_t mp4ff_meta_get_num_items(const mp4ff_t *f) | |
329 { | |
330 return f->tags.count; | |
331 } | |
332 | |
333 int32_t mp4ff_meta_get_by_index(const mp4ff_t *f, uint32_t index, | |
334 char **item, char **value) | |
335 { | |
336 if (index >= f->tags.count) | |
337 { | |
338 *item = NULL; | |
339 *value = NULL; | |
340 return 0; | |
341 } else { | |
342 *item = strdup(f->tags.tags[index].item); | |
343 *value = strdup(f->tags.tags[index].value); | |
344 return 1; | |
345 } | |
346 } | |
347 | |
348 int32_t mp4ff_meta_get_title(const mp4ff_t *f, char **value) | |
349 { | |
350 return mp4ff_meta_find_by_name(f, "title", value); | |
351 } | |
352 | |
353 int32_t mp4ff_meta_get_artist(const mp4ff_t *f, char **value) | |
354 { | |
355 return mp4ff_meta_find_by_name(f, "artist", value); | |
356 } | |
357 | |
358 int32_t mp4ff_meta_get_writer(const mp4ff_t *f, char **value) | |
359 { | |
360 return mp4ff_meta_find_by_name(f, "writer", value); | |
361 } | |
362 | |
363 int32_t mp4ff_meta_get_album(const mp4ff_t *f, char **value) | |
364 { | |
365 return mp4ff_meta_find_by_name(f, "album", value); | |
366 } | |
367 | |
368 int32_t mp4ff_meta_get_date(const mp4ff_t *f, char **value) | |
369 { | |
370 return mp4ff_meta_find_by_name(f, "date", value); | |
371 } | |
372 | |
373 int32_t mp4ff_meta_get_tool(const mp4ff_t *f, char **value) | |
374 { | |
375 return mp4ff_meta_find_by_name(f, "tool", value); | |
376 } | |
377 | |
378 int32_t mp4ff_meta_get_comment(const mp4ff_t *f, char **value) | |
379 { | |
380 return mp4ff_meta_find_by_name(f, "comment", value); | |
381 } | |
382 | |
383 int32_t mp4ff_meta_get_genre(const mp4ff_t *f, char **value) | |
384 { | |
385 return mp4ff_meta_find_by_name(f, "genre", value); | |
386 } | |
387 | |
388 int32_t mp4ff_meta_get_track(const mp4ff_t *f, char **value) | |
389 { | |
390 return mp4ff_meta_find_by_name(f, "track", value); | |
391 } | |
392 | |
393 int32_t mp4ff_meta_get_disc(const mp4ff_t *f, char **value) | |
394 { | |
395 return mp4ff_meta_find_by_name(f, "disc", value); | |
396 } | |
397 | |
398 int32_t mp4ff_meta_get_compilation(const mp4ff_t *f, char **value) | |
399 { | |
400 return mp4ff_meta_find_by_name(f, "compilation", value); | |
401 } | |
402 | |
403 int32_t mp4ff_meta_get_tempo(const mp4ff_t *f, char **value) | |
404 { | |
405 return mp4ff_meta_find_by_name(f, "tempo", value); | |
406 } | |
407 | |
408 int32_t mp4ff_meta_get_coverart(const mp4ff_t *f, char **value) | |
409 { | |
410 return mp4ff_meta_find_by_name(f, "cover", value); | |
411 } | |
412 | |
413 #endif |