Mercurial > libavformat.hg
comparison mp3.c @ 4354:6111dd03464b libavformat
use new metadata API in the mp3 demuxer
author | aurel |
---|---|
date | Mon, 02 Feb 2009 22:11:56 +0000 |
parents | faf0db68fcda |
children | d8a6a01679be |
comparison
equal
deleted
inserted
replaced
4353:e4a1b568b313 | 4354:6111dd03464b |
---|---|
165 while(len--) | 165 while(len--) |
166 v= (v<<7) + (get_byte(s)&0x7F); | 166 v= (v<<7) + (get_byte(s)&0x7F); |
167 return v; | 167 return v; |
168 } | 168 } |
169 | 169 |
170 static void id3v2_read_ttag(AVFormatContext *s, int taglen, char *dst, int dstlen) | 170 static void id3v2_read_ttag(AVFormatContext *s, int taglen, const char *key) |
171 { | 171 { |
172 char *q; | 172 char *q, dst[512]; |
173 int len; | 173 int len, dstlen = sizeof(dst); |
174 unsigned genre; | |
174 | 175 |
175 if(dstlen > 0) | 176 if(dstlen > 0) |
176 dst[0]= 0; | 177 dst[0]= 0; |
177 if(taglen < 1) | 178 if(taglen < 1) |
178 return; | 179 return; |
195 len = FFMIN(taglen, dstlen-1); | 196 len = FFMIN(taglen, dstlen-1); |
196 get_buffer(s->pb, dst, len); | 197 get_buffer(s->pb, dst, len); |
197 dst[len] = 0; | 198 dst[len] = 0; |
198 break; | 199 break; |
199 } | 200 } |
201 | |
202 if (!strcmp(key, "genre") | |
203 && sscanf(dst, "(%d)", &genre) == 1 && genre <= ID3v1_GENRE_MAX) | |
204 av_strlcpy(dst, id3v1_genre_str[genre], sizeof(dst)); | |
205 | |
206 if (*dst) | |
207 av_metadata_set(&s->metadata, key, dst); | |
200 } | 208 } |
201 | 209 |
202 /** | 210 /** |
203 * ID3v2 parser | 211 * ID3v2 parser |
204 * | 212 * |
209 static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags) | 217 static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags) |
210 { | 218 { |
211 int isv34, tlen; | 219 int isv34, tlen; |
212 uint32_t tag; | 220 uint32_t tag; |
213 int64_t next; | 221 int64_t next; |
214 char tmp[16]; | |
215 int taghdrlen; | 222 int taghdrlen; |
216 const char *reason; | 223 const char *reason; |
217 | 224 |
218 switch(version) { | 225 switch(version) { |
219 case 2: | 226 case 2: |
261 next = url_ftell(s->pb) + tlen; | 268 next = url_ftell(s->pb) + tlen; |
262 | 269 |
263 switch(tag) { | 270 switch(tag) { |
264 case MKBETAG('T', 'I', 'T', '2'): | 271 case MKBETAG('T', 'I', 'T', '2'): |
265 case MKBETAG(0, 'T', 'T', '2'): | 272 case MKBETAG(0, 'T', 'T', '2'): |
266 id3v2_read_ttag(s, tlen, s->title, sizeof(s->title)); | 273 id3v2_read_ttag(s, tlen, "title"); |
267 break; | 274 break; |
268 case MKBETAG('T', 'P', 'E', '1'): | 275 case MKBETAG('T', 'P', 'E', '1'): |
269 case MKBETAG(0, 'T', 'P', '1'): | 276 case MKBETAG(0, 'T', 'P', '1'): |
270 id3v2_read_ttag(s, tlen, s->author, sizeof(s->author)); | 277 id3v2_read_ttag(s, tlen, "author"); |
271 break; | 278 break; |
272 case MKBETAG('T', 'A', 'L', 'B'): | 279 case MKBETAG('T', 'A', 'L', 'B'): |
273 case MKBETAG(0, 'T', 'A', 'L'): | 280 case MKBETAG(0, 'T', 'A', 'L'): |
274 id3v2_read_ttag(s, tlen, s->album, sizeof(s->album)); | 281 id3v2_read_ttag(s, tlen, "album"); |
275 break; | 282 break; |
276 case MKBETAG('T', 'C', 'O', 'N'): | 283 case MKBETAG('T', 'C', 'O', 'N'): |
277 case MKBETAG(0, 'T', 'C', 'O'): | 284 case MKBETAG(0, 'T', 'C', 'O'): |
278 id3v2_read_ttag(s, tlen, s->genre, sizeof(s->genre)); | 285 id3v2_read_ttag(s, tlen, "genre"); |
279 break; | 286 break; |
280 case MKBETAG('T', 'C', 'O', 'P'): | 287 case MKBETAG('T', 'C', 'O', 'P'): |
281 case MKBETAG(0, 'T', 'C', 'R'): | 288 case MKBETAG(0, 'T', 'C', 'R'): |
282 id3v2_read_ttag(s, tlen, s->copyright, sizeof(s->copyright)); | 289 id3v2_read_ttag(s, tlen, "copyright"); |
283 break; | 290 break; |
284 case MKBETAG('T', 'R', 'C', 'K'): | 291 case MKBETAG('T', 'R', 'C', 'K'): |
285 case MKBETAG(0, 'T', 'R', 'K'): | 292 case MKBETAG(0, 'T', 'R', 'K'): |
286 id3v2_read_ttag(s, tlen, tmp, sizeof(tmp)); | 293 id3v2_read_ttag(s, tlen, "track"); |
287 s->track = atoi(tmp); | |
288 break; | 294 break; |
289 case 0: | 295 case 0: |
290 /* padding, skip to end */ | 296 /* padding, skip to end */ |
291 url_fskip(s->pb, len); | 297 url_fskip(s->pb, len); |
292 len = 0; | 298 len = 0; |
303 error: | 309 error: |
304 av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason); | 310 av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason); |
305 url_fskip(s->pb, len); | 311 url_fskip(s->pb, len); |
306 } | 312 } |
307 | 313 |
308 static void id3v1_get_string(char *str, int str_size, | 314 static void id3v1_get_string(AVFormatContext *s, const char *key, |
309 const uint8_t *buf, int buf_size) | 315 const uint8_t *buf, int buf_size) |
310 { | 316 { |
311 int i, c; | 317 int i, c; |
312 char *q; | 318 char *q, str[512]; |
313 | 319 |
314 q = str; | 320 q = str; |
315 for(i = 0; i < buf_size; i++) { | 321 for(i = 0; i < buf_size; i++) { |
316 c = buf[i]; | 322 c = buf[i]; |
317 if (c == '\0') | 323 if (c == '\0') |
318 break; | 324 break; |
319 if ((q - str) >= str_size - 1) | 325 if ((q - str) >= sizeof(str) - 1) |
320 break; | 326 break; |
321 *q++ = c; | 327 *q++ = c; |
322 } | 328 } |
323 *q = '\0'; | 329 *q = '\0'; |
330 | |
331 if (*str) | |
332 av_metadata_set(&s->metadata, key, str); | |
324 } | 333 } |
325 | 334 |
326 /* 'buf' must be ID3v1_TAG_SIZE byte long */ | 335 /* 'buf' must be ID3v1_TAG_SIZE byte long */ |
327 static int id3v1_parse_tag(AVFormatContext *s, const uint8_t *buf) | 336 static int id3v1_parse_tag(AVFormatContext *s, const uint8_t *buf) |
328 { | 337 { |
331 | 340 |
332 if (!(buf[0] == 'T' && | 341 if (!(buf[0] == 'T' && |
333 buf[1] == 'A' && | 342 buf[1] == 'A' && |
334 buf[2] == 'G')) | 343 buf[2] == 'G')) |
335 return -1; | 344 return -1; |
336 id3v1_get_string(s->title, sizeof(s->title), buf + 3, 30); | 345 id3v1_get_string(s, "title", buf + 3, 30); |
337 id3v1_get_string(s->author, sizeof(s->author), buf + 33, 30); | 346 id3v1_get_string(s, "author", buf + 33, 30); |
338 id3v1_get_string(s->album, sizeof(s->album), buf + 63, 30); | 347 id3v1_get_string(s, "album", buf + 63, 30); |
339 id3v1_get_string(str, sizeof(str), buf + 93, 4); | 348 id3v1_get_string(s, "year", buf + 93, 4); |
340 s->year = atoi(str); | 349 id3v1_get_string(s, "comment", buf + 97, 30); |
341 id3v1_get_string(s->comment, sizeof(s->comment), buf + 97, 30); | 350 if (buf[125] == 0 && buf[126] != 0) { |
342 if (buf[125] == 0 && buf[126] != 0) | 351 snprintf(str, sizeof(str), "%d", buf[126]); |
343 s->track = buf[126]; | 352 av_metadata_set(&s->metadata, "track", str); |
353 } | |
344 genre = buf[127]; | 354 genre = buf[127]; |
345 if (genre <= ID3v1_GENRE_MAX) | 355 if (genre <= ID3v1_GENRE_MAX) |
346 av_strlcpy(s->genre, id3v1_genre_str[genre], sizeof(s->genre)); | 356 av_metadata_set(&s->metadata, "genre", id3v1_genre_str[genre]); |
347 return 0; | 357 return 0; |
348 } | 358 } |
349 | 359 |
350 /* mp3 read */ | 360 /* mp3 read */ |
351 | 361 |