169
171
(buf[9] & 0x80) == 0);
172
static void id3_get_string(char *str, int str_size,
173
const uint8_t *buf, int buf_size)
174
static unsigned int id3v2_get_size(ByteIOContext *s, int len)
178
v= (v<<7) + (get_byte(s)&0x7F);
182
static void id3v2_read_ttag(AVFormatContext *s, int taglen, char *dst, int dstlen)
190
taglen--; /* account for encoding type byte */
191
dstlen--; /* Leave space for zero terminator */
193
switch(get_byte(s->pb)) { /* encoding type */
195
case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */
199
PUT_UTF8(get_byte(s->pb), tmp, if (q - dst < dstlen - 1) *q++ = tmp;)
205
len = FFMIN(taglen, dstlen);
206
get_buffer(s->pb, dst, len);
215
* Handles ID3v2.2, 2.3 and 2.4.
219
static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
231
reason = "compression";
250
reason = "unsynchronization";
254
if(isv34 && flags & 0x40) /* Extended header present, just skip over it */
255
url_fskip(s->pb, id3v2_get_size(s->pb, 4));
257
while(len >= taghdrlen) {
259
tag = get_be32(s->pb);
260
tlen = id3v2_get_size(s->pb, 4);
261
get_be16(s->pb); /* flags */
263
tag = get_be24(s->pb);
264
tlen = id3v2_get_size(s->pb, 3);
266
len -= taghdrlen + tlen;
271
next = url_ftell(s->pb) + tlen;
274
case MKBETAG('T', 'I', 'T', '2'):
275
case MKBETAG(0, 'T', 'T', '2'):
276
id3v2_read_ttag(s, tlen, s->title, sizeof(s->title));
278
case MKBETAG('T', 'P', 'E', '1'):
279
case MKBETAG(0, 'T', 'P', '1'):
280
id3v2_read_ttag(s, tlen, s->author, sizeof(s->author));
282
case MKBETAG('T', 'A', 'L', 'B'):
283
case MKBETAG(0, 'T', 'A', 'L'):
284
id3v2_read_ttag(s, tlen, s->album, sizeof(s->album));
286
case MKBETAG('T', 'C', 'O', 'N'):
287
case MKBETAG(0, 'T', 'C', 'O'):
288
id3v2_read_ttag(s, tlen, s->genre, sizeof(s->genre));
290
case MKBETAG('T', 'C', 'O', 'P'):
291
case MKBETAG(0, 'T', 'C', 'R'):
292
id3v2_read_ttag(s, tlen, s->copyright, sizeof(s->copyright));
294
case MKBETAG('T', 'R', 'C', 'K'):
295
case MKBETAG(0, 'T', 'R', 'K'):
296
id3v2_read_ttag(s, tlen, tmp, sizeof(tmp));
297
s->track = atoi(tmp);
300
/* padding, skip to end */
301
url_fskip(s->pb, len);
305
/* Skip to end of tag */
306
url_fseek(s->pb, next, SEEK_SET);
309
if(version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
310
url_fskip(s->pb, 10);
314
av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
315
url_fskip(s->pb, len);
318
static void id3v1_get_string(char *str, int str_size,
319
const uint8_t *buf, int buf_size)
200
id3_get_string(s->title, sizeof(s->title), buf + 3, 30);
201
id3_get_string(s->author, sizeof(s->author), buf + 33, 30);
202
id3_get_string(s->album, sizeof(s->album), buf + 63, 30);
203
id3_get_string(str, sizeof(str), buf + 93, 4);
346
id3v1_get_string(s->title, sizeof(s->title), buf + 3, 30);
347
id3v1_get_string(s->author, sizeof(s->author), buf + 33, 30);
348
id3v1_get_string(s->album, sizeof(s->album), buf + 63, 30);
349
id3v1_get_string(str, sizeof(str), buf + 93, 4);
204
350
s->year = atoi(str);
205
id3_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
351
id3v1_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
206
352
if (buf[125] == 0 && buf[126] != 0)
207
353
s->track = buf[126];
208
354
genre = buf[127];
209
if (genre <= ID3_GENRE_MAX)
210
pstrcpy(s->genre, sizeof(s->genre), id3_genre_str[genre]);
355
if (genre <= ID3v1_GENRE_MAX)
356
av_strlcpy(s->genre, id3v1_genre_str[genre], sizeof(s->genre));
214
static void id3_create_tag(AVFormatContext *s, uint8_t *buf)
360
static void id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
218
memset(buf, 0, ID3_TAG_SIZE); /* fail safe */
364
memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
247
393
static int mp3_read_probe(AVProbeData *p)
249
int max_frames, first_frames;
395
int max_frames, first_frames = 0;
250
396
int fsize, frames, sample_rate;
252
398
uint8_t *buf, *buf2, *end;
253
399
AVCodecContext avctx;
255
if(p->buf_size < ID3_HEADER_SIZE)
258
if(id3_match(p->buf))
259
return AVPROBE_SCORE_MAX/2+1; // this must be less then mpeg-ps because some retards put id3 tage before mpeg-ps files
401
if(id3v2_match(p->buf))
402
return AVPROBE_SCORE_MAX/2+1; // this must be less than mpeg-ps because some retards put id3v2 tags before mpeg-ps files
263
end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
406
end = buf + p->buf_size - sizeof(uint32_t);
265
for(; buf < end; buf++) {
408
for(; buf < end; buf= buf2+1) {
268
411
for(frames = 0; buf2 < end; frames++) {
269
header = (buf2[0] << 24) | (buf2[1] << 16) | (buf2[2] << 8) | buf2[3];
270
fsize = mpa_decode_header(&avctx, header, &sample_rate);
412
header = AV_RB32(buf2);
413
fsize = ff_mpa_decode_header(&avctx, header, &sample_rate);
277
420
first_frames= frames;
279
422
if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
423
else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
280
424
else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
281
425
else if(max_frames>=1) return 1;
430
* Try to find Xing/Info/VBRI tags and compute duration from info therein
432
static void mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, offset_t base)
435
int frames = -1; /* Total number of frames in file */
436
const offset_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
440
if(ff_mpa_check_header(v) < 0)
443
ff_mpegaudio_decode_header(&c, v);
447
/* Check for Xing / Info tag */
448
url_fseek(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1], SEEK_CUR);
450
if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
453
frames = get_be32(s->pb);
456
/* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
457
url_fseek(s->pb, base + 4 + 32, SEEK_SET);
459
if(v == MKBETAG('V', 'B', 'R', 'I')) {
460
/* Check tag version */
461
if(get_be16(s->pb) == 1) {
462
/* skip delay, quality and total bytes */
463
url_fseek(s->pb, 8, SEEK_CUR);
464
frames = get_be32(s->pb);
471
spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
472
st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
285
476
static int mp3_read_header(AVFormatContext *s,
286
477
AVFormatParameters *ap)
289
uint8_t buf[ID3_TAG_SIZE];
480
uint8_t buf[ID3v1_TAG_SIZE];
290
481
int len, ret, filesize;
292
484
st = av_new_stream(s, 0);
294
return AVERROR_NOMEM;
486
return AVERROR(ENOMEM);
296
488
st->codec->codec_type = CODEC_TYPE_AUDIO;
297
489
st->codec->codec_id = CODEC_ID_MP3;
298
st->need_parsing = 1;
490
st->need_parsing = AVSTREAM_PARSE_FULL;
300
493
/* try to get the TAG */
301
if (!url_is_streamed(&s->pb)) {
494
if (!url_is_streamed(s->pb)) {
302
495
/* XXX: change that */
303
filesize = url_fsize(&s->pb);
496
filesize = url_fsize(s->pb);
304
497
if (filesize > 128) {
305
url_fseek(&s->pb, filesize - 128, SEEK_SET);
306
ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE);
307
if (ret == ID3_TAG_SIZE) {
308
id3_parse_tag(s, buf);
498
url_fseek(s->pb, filesize - 128, SEEK_SET);
499
ret = get_buffer(s->pb, buf, ID3v1_TAG_SIZE);
500
if (ret == ID3v1_TAG_SIZE) {
501
id3v1_parse_tag(s, buf);
310
url_fseek(&s->pb, 0, SEEK_SET);
503
url_fseek(s->pb, 0, SEEK_SET);
314
/* if ID3 header found, skip it */
315
ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE);
316
if (ret != ID3_HEADER_SIZE)
507
/* if ID3v2 header found, skip it */
508
ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
509
if (ret != ID3v2_HEADER_SIZE)
318
if (id3_match(buf)) {
319
/* skip ID3 header */
511
if (id3v2_match(buf)) {
512
/* parse ID3v2 header */
320
513
len = ((buf[6] & 0x7f) << 21) |
321
514
((buf[7] & 0x7f) << 14) |
322
515
((buf[8] & 0x7f) << 7) |
324
url_fskip(&s->pb, len);
517
id3v2_parse(s, len, buf[3], buf[5]);
326
url_fseek(&s->pb, 0, SEEK_SET);
519
url_fseek(s->pb, 0, SEEK_SET);
522
off = url_ftell(s->pb);
523
mp3_parse_vbr_tags(s, st, off);
524
url_fseek(s->pb, off, SEEK_SET);
329
526
/* the parameters will be extracted from the compressed bitstream */
359
556
#ifdef CONFIG_MUXERS
360
557
/* simple formats */
559
static void id3v2_put_size(AVFormatContext *s, int size)
561
put_byte(s->pb, size >> 21 & 0x7f);
562
put_byte(s->pb, size >> 14 & 0x7f);
563
put_byte(s->pb, size >> 7 & 0x7f);
564
put_byte(s->pb, size & 0x7f);
567
static void id3v2_put_ttag(AVFormatContext *s, const char *string, uint32_t tag)
569
int len = strlen(string);
570
put_be32(s->pb, tag);
571
id3v2_put_size(s, len + 1);
573
put_byte(s->pb, 3); /* UTF-8 */
574
put_buffer(s->pb, string, len);
579
* Write an ID3v2.4 header at beginning of stream
361
582
static int mp3_write_header(struct AVFormatContext *s)
589
snprintf(tracktxt, sizeof(tracktxt), "%d", s->track);
591
snprintf( yeartxt, sizeof(yeartxt) , "%d", s->year );
593
if(s->title[0]) totlen += 11 + strlen(s->title);
594
if(s->author[0]) totlen += 11 + strlen(s->author);
595
if(s->album[0]) totlen += 11 + strlen(s->album);
596
if(s->genre[0]) totlen += 11 + strlen(s->genre);
597
if(s->copyright[0]) totlen += 11 + strlen(s->copyright);
598
if(s->track) totlen += 11 + strlen(tracktxt);
599
if(s->year) totlen += 11 + strlen(yeartxt);
600
if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
601
totlen += strlen(LIBAVFORMAT_IDENT) + 11;
606
put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
608
put_byte(s->pb, 0); /* flags */
610
id3v2_put_size(s, totlen);
612
if(s->title[0]) id3v2_put_ttag(s, s->title, MKBETAG('T', 'I', 'T', '2'));
613
if(s->author[0]) id3v2_put_ttag(s, s->author, MKBETAG('T', 'P', 'E', '1'));
614
if(s->album[0]) id3v2_put_ttag(s, s->album, MKBETAG('T', 'A', 'L', 'B'));
615
if(s->genre[0]) id3v2_put_ttag(s, s->genre, MKBETAG('T', 'C', 'O', 'N'));
616
if(s->copyright[0]) id3v2_put_ttag(s, s->copyright, MKBETAG('T', 'C', 'O', 'P'));
617
if(s->track) id3v2_put_ttag(s, tracktxt, MKBETAG('T', 'R', 'C', 'K'));
618
if(s->year) id3v2_put_ttag(s, yeartxt, MKBETAG('T', 'Y', 'E', 'R'));
619
if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
620
id3v2_put_ttag(s, LIBAVFORMAT_IDENT, MKBETAG('T', 'E', 'N', 'C'));
366
624
static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
368
put_buffer(&s->pb, pkt->data, pkt->size);
369
put_flush_packet(&s->pb);
626
put_buffer(s->pb, pkt->data, pkt->size);
627
put_flush_packet(s->pb);
373
631
static int mp3_write_trailer(struct AVFormatContext *s)
375
uint8_t buf[ID3_TAG_SIZE];
633
uint8_t buf[ID3v1_TAG_SIZE];
377
/* write the id3 header */
635
/* write the id3v1 tag */
378
636
if (s->title[0] != '\0') {
379
id3_create_tag(s, buf);
380
put_buffer(&s->pb, buf, ID3_TAG_SIZE);
381
put_flush_packet(&s->pb);
637
id3v1_create_tag(s, buf);
638
put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
639
put_flush_packet(s->pb);