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