00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <strings.h>
00023 #include "libavutil/avstring.h"
00024 #include "libavcodec/mpegaudio.h"
00025 #include "libavcodec/mpegaudiodecheader.h"
00026 #include "avformat.h"
00027 #include "id3v2.h"
00028
00029 #define ID3v1_TAG_SIZE 128
00030
00031 #define ID3v1_GENRE_MAX 125
00032
00033 static const char * const id3v1_genre_str[ID3v1_GENRE_MAX + 1] = {
00034 [0] = "Blues",
00035 [1] = "Classic Rock",
00036 [2] = "Country",
00037 [3] = "Dance",
00038 [4] = "Disco",
00039 [5] = "Funk",
00040 [6] = "Grunge",
00041 [7] = "Hip-Hop",
00042 [8] = "Jazz",
00043 [9] = "Metal",
00044 [10] = "New Age",
00045 [11] = "Oldies",
00046 [12] = "Other",
00047 [13] = "Pop",
00048 [14] = "R&B",
00049 [15] = "Rap",
00050 [16] = "Reggae",
00051 [17] = "Rock",
00052 [18] = "Techno",
00053 [19] = "Industrial",
00054 [20] = "Alternative",
00055 [21] = "Ska",
00056 [22] = "Death Metal",
00057 [23] = "Pranks",
00058 [24] = "Soundtrack",
00059 [25] = "Euro-Techno",
00060 [26] = "Ambient",
00061 [27] = "Trip-Hop",
00062 [28] = "Vocal",
00063 [29] = "Jazz+Funk",
00064 [30] = "Fusion",
00065 [31] = "Trance",
00066 [32] = "Classical",
00067 [33] = "Instrumental",
00068 [34] = "Acid",
00069 [35] = "House",
00070 [36] = "Game",
00071 [37] = "Sound Clip",
00072 [38] = "Gospel",
00073 [39] = "Noise",
00074 [40] = "AlternRock",
00075 [41] = "Bass",
00076 [42] = "Soul",
00077 [43] = "Punk",
00078 [44] = "Space",
00079 [45] = "Meditative",
00080 [46] = "Instrumental Pop",
00081 [47] = "Instrumental Rock",
00082 [48] = "Ethnic",
00083 [49] = "Gothic",
00084 [50] = "Darkwave",
00085 [51] = "Techno-Industrial",
00086 [52] = "Electronic",
00087 [53] = "Pop-Folk",
00088 [54] = "Eurodance",
00089 [55] = "Dream",
00090 [56] = "Southern Rock",
00091 [57] = "Comedy",
00092 [58] = "Cult",
00093 [59] = "Gangsta",
00094 [60] = "Top 40",
00095 [61] = "Christian Rap",
00096 [62] = "Pop/Funk",
00097 [63] = "Jungle",
00098 [64] = "Native American",
00099 [65] = "Cabaret",
00100 [66] = "New Wave",
00101 [67] = "Psychadelic",
00102 [68] = "Rave",
00103 [69] = "Showtunes",
00104 [70] = "Trailer",
00105 [71] = "Lo-Fi",
00106 [72] = "Tribal",
00107 [73] = "Acid Punk",
00108 [74] = "Acid Jazz",
00109 [75] = "Polka",
00110 [76] = "Retro",
00111 [77] = "Musical",
00112 [78] = "Rock & Roll",
00113 [79] = "Hard Rock",
00114 [80] = "Folk",
00115 [81] = "Folk-Rock",
00116 [82] = "National Folk",
00117 [83] = "Swing",
00118 [84] = "Fast Fusion",
00119 [85] = "Bebob",
00120 [86] = "Latin",
00121 [87] = "Revival",
00122 [88] = "Celtic",
00123 [89] = "Bluegrass",
00124 [90] = "Avantgarde",
00125 [91] = "Gothic Rock",
00126 [92] = "Progressive Rock",
00127 [93] = "Psychedelic Rock",
00128 [94] = "Symphonic Rock",
00129 [95] = "Slow Rock",
00130 [96] = "Big Band",
00131 [97] = "Chorus",
00132 [98] = "Easy Listening",
00133 [99] = "Acoustic",
00134 [100] = "Humour",
00135 [101] = "Speech",
00136 [102] = "Chanson",
00137 [103] = "Opera",
00138 [104] = "Chamber Music",
00139 [105] = "Sonata",
00140 [106] = "Symphony",
00141 [107] = "Booty Bass",
00142 [108] = "Primus",
00143 [109] = "Porn Groove",
00144 [110] = "Satire",
00145 [111] = "Slow Jam",
00146 [112] = "Club",
00147 [113] = "Tango",
00148 [114] = "Samba",
00149 [115] = "Folklore",
00150 [116] = "Ballad",
00151 [117] = "Power Ballad",
00152 [118] = "Rhythmic Soul",
00153 [119] = "Freestyle",
00154 [120] = "Duet",
00155 [121] = "Punk Rock",
00156 [122] = "Drum Solo",
00157 [123] = "A capella",
00158 [124] = "Euro-House",
00159 [125] = "Dance Hall",
00160 };
00161
00162 static unsigned int id3v2_get_size(ByteIOContext *s, int len)
00163 {
00164 int v=0;
00165 while(len--)
00166 v= (v<<7) + (get_byte(s)&0x7F);
00167 return v;
00168 }
00169
00170 static void id3v2_read_ttag(AVFormatContext *s, int taglen, const char *key)
00171 {
00172 char *q, dst[512];
00173 int len, dstlen = sizeof(dst) - 1;
00174 unsigned genre;
00175
00176 dst[0]= 0;
00177 if(taglen < 1)
00178 return;
00179
00180 taglen--;
00181
00182 switch(get_byte(s->pb)) {
00183
00184 case 0:
00185 q = dst;
00186 while(taglen--) {
00187 uint8_t tmp;
00188 PUT_UTF8(get_byte(s->pb), tmp, if (q - dst < dstlen - 1) *q++ = tmp;)
00189 }
00190 *q = '\0';
00191 break;
00192
00193 case 3:
00194 len = FFMIN(taglen, dstlen-1);
00195 get_buffer(s->pb, dst, len);
00196 dst[len] = 0;
00197 break;
00198 }
00199
00200 if (!strcmp(key, "genre")
00201 && sscanf(dst, "(%d)", &genre) == 1 && genre <= ID3v1_GENRE_MAX)
00202 av_strlcpy(dst, id3v1_genre_str[genre], sizeof(dst));
00203
00204 if (*dst)
00205 av_metadata_set(&s->metadata, key, dst);
00206 }
00207
00215 static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
00216 {
00217 int isv34, tlen;
00218 uint32_t tag;
00219 int64_t next;
00220 int taghdrlen;
00221 const char *reason;
00222
00223 switch(version) {
00224 case 2:
00225 if(flags & 0x40) {
00226 reason = "compression";
00227 goto error;
00228 }
00229 isv34 = 0;
00230 taghdrlen = 6;
00231 break;
00232
00233 case 3:
00234 case 4:
00235 isv34 = 1;
00236 taghdrlen = 10;
00237 break;
00238
00239 default:
00240 reason = "version";
00241 goto error;
00242 }
00243
00244 if(flags & 0x80) {
00245 reason = "unsynchronization";
00246 goto error;
00247 }
00248
00249 if (isv34 && flags & 0x40) {
00250 int extlen = id3v2_get_size(s->pb, 4);
00251 if (version == 4)
00252 extlen -= 4;
00253
00254 if (extlen < 0) {
00255 reason = "invalid extended header length";
00256 goto error;
00257 }
00258 url_fskip(s->pb, extlen);
00259 }
00260
00261 while(len >= taghdrlen) {
00262 if(isv34) {
00263 tag = get_be32(s->pb);
00264 tlen = id3v2_get_size(s->pb, 4);
00265 get_be16(s->pb);
00266 } else {
00267 tag = get_be24(s->pb);
00268 tlen = id3v2_get_size(s->pb, 3);
00269 }
00270 len -= taghdrlen + tlen;
00271
00272 if(len < 0)
00273 break;
00274
00275 next = url_ftell(s->pb) + tlen;
00276
00277 switch(tag) {
00278 case MKBETAG('T', 'I', 'T', '2'):
00279 case MKBETAG(0, 'T', 'T', '2'):
00280 id3v2_read_ttag(s, tlen, "title");
00281 break;
00282 case MKBETAG('T', 'P', 'E', '1'):
00283 case MKBETAG(0, 'T', 'P', '1'):
00284 id3v2_read_ttag(s, tlen, "author");
00285 break;
00286 case MKBETAG('T', 'A', 'L', 'B'):
00287 case MKBETAG(0, 'T', 'A', 'L'):
00288 id3v2_read_ttag(s, tlen, "album");
00289 break;
00290 case MKBETAG('T', 'C', 'O', 'N'):
00291 case MKBETAG(0, 'T', 'C', 'O'):
00292 id3v2_read_ttag(s, tlen, "genre");
00293 break;
00294 case MKBETAG('T', 'C', 'O', 'P'):
00295 case MKBETAG(0, 'T', 'C', 'R'):
00296 id3v2_read_ttag(s, tlen, "copyright");
00297 break;
00298 case MKBETAG('T', 'R', 'C', 'K'):
00299 case MKBETAG(0, 'T', 'R', 'K'):
00300 id3v2_read_ttag(s, tlen, "track");
00301 break;
00302 case 0:
00303
00304 url_fskip(s->pb, len);
00305 len = 0;
00306 continue;
00307 }
00308
00309 url_fseek(s->pb, next, SEEK_SET);
00310 }
00311
00312 if(version == 4 && flags & 0x10)
00313 url_fskip(s->pb, 10);
00314 return;
00315
00316 error:
00317 av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
00318 url_fskip(s->pb, len);
00319 }
00320
00321 static void id3v1_get_string(AVFormatContext *s, const char *key,
00322 const uint8_t *buf, int buf_size)
00323 {
00324 int i, c;
00325 char *q, str[512];
00326
00327 q = str;
00328 for(i = 0; i < buf_size; i++) {
00329 c = buf[i];
00330 if (c == '\0')
00331 break;
00332 if ((q - str) >= sizeof(str) - 1)
00333 break;
00334 *q++ = c;
00335 }
00336 *q = '\0';
00337
00338 if (*str)
00339 av_metadata_set(&s->metadata, key, str);
00340 }
00341
00342
00343 static int id3v1_parse_tag(AVFormatContext *s, const uint8_t *buf)
00344 {
00345 char str[5];
00346 int genre;
00347
00348 if (!(buf[0] == 'T' &&
00349 buf[1] == 'A' &&
00350 buf[2] == 'G'))
00351 return -1;
00352 id3v1_get_string(s, "title", buf + 3, 30);
00353 id3v1_get_string(s, "author", buf + 33, 30);
00354 id3v1_get_string(s, "album", buf + 63, 30);
00355 id3v1_get_string(s, "year", buf + 93, 4);
00356 id3v1_get_string(s, "comment", buf + 97, 30);
00357 if (buf[125] == 0 && buf[126] != 0) {
00358 snprintf(str, sizeof(str), "%d", buf[126]);
00359 av_metadata_set(&s->metadata, "track", str);
00360 }
00361 genre = buf[127];
00362 if (genre <= ID3v1_GENRE_MAX)
00363 av_metadata_set(&s->metadata, "genre", id3v1_genre_str[genre]);
00364 return 0;
00365 }
00366
00367
00368
00369 static int mp3_read_probe(AVProbeData *p)
00370 {
00371 int max_frames, first_frames = 0;
00372 int fsize, frames, sample_rate;
00373 uint32_t header;
00374 uint8_t *buf, *buf0, *buf2, *end;
00375 AVCodecContext avctx;
00376
00377 buf0 = p->buf;
00378 if(ff_id3v2_match(buf0)) {
00379 buf0 += ff_id3v2_tag_len(buf0);
00380 }
00381
00382 max_frames = 0;
00383 buf = buf0;
00384 end = p->buf + p->buf_size - sizeof(uint32_t);
00385
00386 for(; buf < end; buf= buf2+1) {
00387 buf2 = buf;
00388
00389 for(frames = 0; buf2 < end; frames++) {
00390 header = AV_RB32(buf2);
00391 fsize = ff_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
00392 if(fsize < 0)
00393 break;
00394 buf2 += fsize;
00395 }
00396 max_frames = FFMAX(max_frames, frames);
00397 if(buf == buf0)
00398 first_frames= frames;
00399 }
00400 if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
00401 else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
00402 else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
00403 else if(buf0!=p->buf) return AVPROBE_SCORE_MAX/4-1;
00404 else if(max_frames>=1) return 1;
00405 else return 0;
00406 }
00407
00411 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
00412 {
00413 uint32_t v, spf;
00414 int frames = -1;
00415 const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
00416 MPADecodeHeader c;
00417 int vbrtag_size = 0;
00418
00419 v = get_be32(s->pb);
00420 if(ff_mpa_check_header(v) < 0)
00421 return -1;
00422
00423 if (ff_mpegaudio_decode_header(&c, v) == 0)
00424 vbrtag_size = c.frame_size;
00425 if(c.layer != 3)
00426 return -1;
00427
00428
00429 url_fseek(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1], SEEK_CUR);
00430 v = get_be32(s->pb);
00431 if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
00432 v = get_be32(s->pb);
00433 if(v & 0x1)
00434 frames = get_be32(s->pb);
00435 }
00436
00437
00438 url_fseek(s->pb, base + 4 + 32, SEEK_SET);
00439 v = get_be32(s->pb);
00440 if(v == MKBETAG('V', 'B', 'R', 'I')) {
00441
00442 if(get_be16(s->pb) == 1) {
00443
00444 url_fseek(s->pb, 8, SEEK_CUR);
00445 frames = get_be32(s->pb);
00446 }
00447 }
00448
00449 if(frames < 0)
00450 return -1;
00451
00452
00453 url_fseek(s->pb, base + vbrtag_size, SEEK_SET);
00454
00455 spf = c.lsf ? 576 : 1152;
00456 st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
00457 st->time_base);
00458 return 0;
00459 }
00460
00461 static int mp3_read_header(AVFormatContext *s,
00462 AVFormatParameters *ap)
00463 {
00464 AVStream *st;
00465 uint8_t buf[ID3v1_TAG_SIZE];
00466 int len, ret, filesize;
00467 int64_t off;
00468
00469 st = av_new_stream(s, 0);
00470 if (!st)
00471 return AVERROR(ENOMEM);
00472
00473 st->codec->codec_type = CODEC_TYPE_AUDIO;
00474 st->codec->codec_id = CODEC_ID_MP3;
00475 st->need_parsing = AVSTREAM_PARSE_FULL;
00476 st->start_time = 0;
00477
00478
00479 if (!url_is_streamed(s->pb)) {
00480
00481 filesize = url_fsize(s->pb);
00482 if (filesize > 128) {
00483 url_fseek(s->pb, filesize - 128, SEEK_SET);
00484 ret = get_buffer(s->pb, buf, ID3v1_TAG_SIZE);
00485 if (ret == ID3v1_TAG_SIZE) {
00486 id3v1_parse_tag(s, buf);
00487 }
00488 url_fseek(s->pb, 0, SEEK_SET);
00489 }
00490 }
00491
00492
00493 ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
00494 if (ret != ID3v2_HEADER_SIZE)
00495 return -1;
00496 if (ff_id3v2_match(buf)) {
00497
00498 len = ((buf[6] & 0x7f) << 21) |
00499 ((buf[7] & 0x7f) << 14) |
00500 ((buf[8] & 0x7f) << 7) |
00501 (buf[9] & 0x7f);
00502 id3v2_parse(s, len, buf[3], buf[5]);
00503 } else {
00504 url_fseek(s->pb, 0, SEEK_SET);
00505 }
00506
00507 off = url_ftell(s->pb);
00508 if (mp3_parse_vbr_tags(s, st, off) < 0)
00509 url_fseek(s->pb, off, SEEK_SET);
00510
00511
00512 return 0;
00513 }
00514
00515 #define MP3_PACKET_SIZE 1024
00516
00517 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
00518 {
00519 int ret, size;
00520
00521
00522 size= MP3_PACKET_SIZE;
00523
00524 ret= av_get_packet(s->pb, pkt, size);
00525
00526 pkt->stream_index = 0;
00527 if (ret <= 0) {
00528 return AVERROR(EIO);
00529 }
00530
00531
00532 pkt->size = ret;
00533 return ret;
00534 }
00535
00536 #if CONFIG_MP2_MUXER || CONFIG_MP3_MUXER
00537 static int id3v1_set_string(AVFormatContext *s, const char *key,
00538 uint8_t *buf, int buf_size)
00539 {
00540 AVMetadataTag *tag;
00541 if ((tag = av_metadata_get(s->metadata, key, NULL, 0)))
00542 strncpy(buf, tag->value, buf_size);
00543 return !!tag;
00544 }
00545
00546 static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
00547 {
00548 AVMetadataTag *tag;
00549 int i, count = 0;
00550
00551 memset(buf, 0, ID3v1_TAG_SIZE);
00552 buf[0] = 'T';
00553 buf[1] = 'A';
00554 buf[2] = 'G';
00555 count += id3v1_set_string(s, "title", buf + 3, 30);
00556 count += id3v1_set_string(s, "author", buf + 33, 30);
00557 count += id3v1_set_string(s, "album", buf + 63, 30);
00558 count += id3v1_set_string(s, "year", buf + 93, 4);
00559 count += id3v1_set_string(s, "comment", buf + 97, 30);
00560 if ((tag = av_metadata_get(s->metadata, "track", NULL, 0))) {
00561 buf[125] = 0;
00562 buf[126] = atoi(tag->value);
00563 count++;
00564 }
00565 if ((tag = av_metadata_get(s->metadata, "genre", NULL, 0))) {
00566 for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
00567 if (!strcasecmp(tag->value, id3v1_genre_str[i])) {
00568 buf[127] = i;
00569 count++;
00570 break;
00571 }
00572 }
00573 }
00574 return count;
00575 }
00576
00577
00578
00579 static void id3v2_put_size(AVFormatContext *s, int size)
00580 {
00581 put_byte(s->pb, size >> 21 & 0x7f);
00582 put_byte(s->pb, size >> 14 & 0x7f);
00583 put_byte(s->pb, size >> 7 & 0x7f);
00584 put_byte(s->pb, size & 0x7f);
00585 }
00586
00587 static void id3v2_put_ttag(AVFormatContext *s, const char *string, uint32_t tag)
00588 {
00589 int len = strlen(string);
00590 put_be32(s->pb, tag);
00591 id3v2_put_size(s, len + 1);
00592 put_be16(s->pb, 0);
00593 put_byte(s->pb, 3);
00594 put_buffer(s->pb, string, len);
00595 }
00596
00597
00602 static int mp3_write_header(struct AVFormatContext *s)
00603 {
00604 AVMetadataTag *title, *author, *album, *genre, *copyright, *track, *year;
00605 int totlen = 0;
00606
00607 title = av_metadata_get(s->metadata, "title", NULL, 0);
00608 author = av_metadata_get(s->metadata, "author", NULL, 0);
00609 album = av_metadata_get(s->metadata, "album", NULL, 0);
00610 genre = av_metadata_get(s->metadata, "genre", NULL, 0);
00611 copyright = av_metadata_get(s->metadata, "copyright", NULL, 0);
00612 track = av_metadata_get(s->metadata, "track", NULL, 0);
00613 year = av_metadata_get(s->metadata, "year", NULL, 0);
00614
00615 if(title) totlen += 11 + strlen(title->value);
00616 if(author) totlen += 11 + strlen(author->value);
00617 if(album) totlen += 11 + strlen(album->value);
00618 if(genre) totlen += 11 + strlen(genre->value);
00619 if(copyright) totlen += 11 + strlen(copyright->value);
00620 if(track) totlen += 11 + strlen(track->value);
00621 if(year) totlen += 11 + strlen(year->value);
00622 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
00623 totlen += strlen(LIBAVFORMAT_IDENT) + 11;
00624
00625 if(totlen == 0)
00626 return 0;
00627
00628 put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04));
00629 put_byte(s->pb, 0);
00630 put_byte(s->pb, 0);
00631
00632 id3v2_put_size(s, totlen);
00633
00634 if(title) id3v2_put_ttag(s, title->value, MKBETAG('T', 'I', 'T', '2'));
00635 if(author) id3v2_put_ttag(s, author->value, MKBETAG('T', 'P', 'E', '1'));
00636 if(album) id3v2_put_ttag(s, album->value, MKBETAG('T', 'A', 'L', 'B'));
00637 if(genre) id3v2_put_ttag(s, genre->value, MKBETAG('T', 'C', 'O', 'N'));
00638 if(copyright) id3v2_put_ttag(s, copyright->value, MKBETAG('T', 'C', 'O', 'P'));
00639 if(track) id3v2_put_ttag(s, track->value, MKBETAG('T', 'R', 'C', 'K'));
00640 if(year) id3v2_put_ttag(s, year->value, MKBETAG('T', 'Y', 'E', 'R'));
00641 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
00642 id3v2_put_ttag(s, LIBAVFORMAT_IDENT, MKBETAG('T', 'E', 'N', 'C'));
00643 return 0;
00644 }
00645
00646 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
00647 {
00648 put_buffer(s->pb, pkt->data, pkt->size);
00649 put_flush_packet(s->pb);
00650 return 0;
00651 }
00652
00653 static int mp3_write_trailer(struct AVFormatContext *s)
00654 {
00655 uint8_t buf[ID3v1_TAG_SIZE];
00656
00657
00658 if (id3v1_create_tag(s, buf) > 0) {
00659 put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
00660 put_flush_packet(s->pb);
00661 }
00662 return 0;
00663 }
00664 #endif
00665
00666 #if CONFIG_MP3_DEMUXER
00667 AVInputFormat mp3_demuxer = {
00668 "mp3",
00669 NULL_IF_CONFIG_SMALL("MPEG audio layer 2/3"),
00670 0,
00671 mp3_read_probe,
00672 mp3_read_header,
00673 mp3_read_packet,
00674 .flags= AVFMT_GENERIC_INDEX,
00675 .extensions = "mp2,mp3,m2a",
00676 };
00677 #endif
00678 #if CONFIG_MP2_MUXER
00679 AVOutputFormat mp2_muxer = {
00680 "mp2",
00681 NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
00682 "audio/x-mpeg",
00683 "mp2,m2a",
00684 0,
00685 CODEC_ID_MP2,
00686 CODEC_ID_NONE,
00687 NULL,
00688 mp3_write_packet,
00689 mp3_write_trailer,
00690 };
00691 #endif
00692 #if CONFIG_MP3_MUXER
00693 AVOutputFormat mp3_muxer = {
00694 "mp3",
00695 NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
00696 "audio/x-mpeg",
00697 "mp3",
00698 0,
00699 CODEC_ID_MP3,
00700 CODEC_ID_NONE,
00701 mp3_write_header,
00702 mp3_write_packet,
00703 mp3_write_trailer,
00704 };
00705 #endif