#include <strings.h>
#include "libavutil/avstring.h"
+#include "libavutil/intreadwrite.h"
#include "avformat.h"
#include "id3v2.h"
#include "id3v1.h"
AVCodecContext avctx;
buf0 = p->buf;
- if(ff_id3v2_match(buf0)) {
- buf0 += ff_id3v2_tag_len(buf0);
- }
+ end = p->buf + p->buf_size - sizeof(uint32_t);
+ while(buf0 < end && !*buf0)
+ buf0++;
max_frames = 0;
buf = buf0;
- end = p->buf + p->buf_size - sizeof(uint32_t);
for(; buf < end; buf= buf2+1) {
buf2 = buf;
static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
{
uint32_t v, spf;
- int frames = -1; /* Total number of frames in file */
+ unsigned frames = 0; /* Total number of frames in file */
+ unsigned size = 0; /* Total number of bytes in the stream */
const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
MPADecodeHeader c;
int vbrtag_size = 0;
v = get_be32(s->pb);
if(v & 0x1)
frames = get_be32(s->pb);
+ if(v & 0x2)
+ size = get_be32(s->pb);
}
/* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
if(v == MKBETAG('V', 'B', 'R', 'I')) {
/* Check tag version */
if(get_be16(s->pb) == 1) {
- /* skip delay, quality and total bytes */
- url_fseek(s->pb, 8, SEEK_CUR);
+ /* skip delay and quality */
+ url_fseek(s->pb, 4, SEEK_CUR);
frames = get_be32(s->pb);
+ size = get_be32(s->pb);
}
}
- if(frames < 0)
+ if(!frames && !size)
return -1;
/* Skip the vbr tag frame */
url_fseek(s->pb, base + vbrtag_size, SEEK_SET);
spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
- st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
- st->time_base);
+ if(frames)
+ st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
+ st->time_base);
+ if(size)
+ st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
+
return 0;
}
if (!st)
return AVERROR(ENOMEM);
- st->codec->codec_type = CODEC_TYPE_AUDIO;
+ st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codec->codec_id = CODEC_ID_MP3;
st->need_parsing = AVSTREAM_PARSE_FULL;
st->start_time = 0;
- ff_id3v1_read(s);
- ff_id3v2_read(s);
+ // lcm of all mp3 sample rates
+ av_set_pts_info(st, 64, 1, 14112000);
off = url_ftell(s->pb);
+
+ if (!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
+ ff_id3v1_read(s);
+
if (mp3_parse_vbr_tags(s, st, off) < 0)
url_fseek(s->pb, off, SEEK_SET);
buf[0] = 'T';
buf[1] = 'A';
buf[2] = 'G';
- count += id3v1_set_string(s, "title", buf + 3, 30);
- count += id3v1_set_string(s, "author", buf + 33, 30);
- count += id3v1_set_string(s, "album", buf + 63, 30);
- count += id3v1_set_string(s, "year", buf + 93, 4);
+ count += id3v1_set_string(s, "TIT2", buf + 3, 30); //title
+ count += id3v1_set_string(s, "TPE1", buf + 33, 30); //author|artist
+ count += id3v1_set_string(s, "TALB", buf + 63, 30); //album
+ count += id3v1_set_string(s, "TDRL", buf + 93, 4); //date
count += id3v1_set_string(s, "comment", buf + 97, 30);
- if ((tag = av_metadata_get(s->metadata, "track", NULL, 0))) {
+ if ((tag = av_metadata_get(s->metadata, "TRCK", NULL, 0))) { //track
buf[125] = 0;
buf[126] = atoi(tag->value);
count++;
}
buf[127] = 0xFF; /* default to unknown genre */
- if ((tag = av_metadata_get(s->metadata, "genre", NULL, 0))) {
+ if ((tag = av_metadata_get(s->metadata, "TCON", NULL, 0))) { //genre
for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
if (!strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
buf[127] = i;
put_byte(s->pb, size & 0x7f);
}
-static void id3v2_put_ttag(AVFormatContext *s, const char *string, uint32_t tag)
+static void id3v2_put_ttag(AVFormatContext *s, const char *buf, int len,
+ uint32_t tag)
{
- int len = strlen(string);
put_be32(s->pb, tag);
id3v2_put_size(s, len + 1);
put_be16(s->pb, 0);
put_byte(s->pb, 3); /* UTF-8 */
- put_buffer(s->pb, string, len);
+ put_buffer(s->pb, buf, len);
}
-/**
- * Write an ID3v2.4 header at beginning of stream
- */
-
-static int mp3_write_header(struct AVFormatContext *s)
-{
- AVMetadataTag *title, *author, *album, *genre, *copyright, *track, *year;
- int totlen = 0;
-
- title = av_metadata_get(s->metadata, "title", NULL, 0);
- author = av_metadata_get(s->metadata, "author", NULL, 0);
- album = av_metadata_get(s->metadata, "album", NULL, 0);
- genre = av_metadata_get(s->metadata, "genre", NULL, 0);
- copyright = av_metadata_get(s->metadata, "copyright", NULL, 0);
- track = av_metadata_get(s->metadata, "track", NULL, 0);
- year = av_metadata_get(s->metadata, "year", NULL, 0);
-
- if(title) totlen += 11 + strlen(title->value);
- if(author) totlen += 11 + strlen(author->value);
- if(album) totlen += 11 + strlen(album->value);
- if(genre) totlen += 11 + strlen(genre->value);
- if(copyright) totlen += 11 + strlen(copyright->value);
- if(track) totlen += 11 + strlen(track->value);
- if(year) totlen += 11 + strlen(year->value);
- if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
- totlen += strlen(LIBAVFORMAT_IDENT) + 11;
-
- if(totlen == 0)
- return 0;
-
- put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
- put_byte(s->pb, 0);
- put_byte(s->pb, 0); /* flags */
-
- id3v2_put_size(s, totlen);
-
- if(title) id3v2_put_ttag(s, title->value, MKBETAG('T', 'I', 'T', '2'));
- if(author) id3v2_put_ttag(s, author->value, MKBETAG('T', 'P', 'E', '1'));
- if(album) id3v2_put_ttag(s, album->value, MKBETAG('T', 'A', 'L', 'B'));
- if(genre) id3v2_put_ttag(s, genre->value, MKBETAG('T', 'C', 'O', 'N'));
- if(copyright) id3v2_put_ttag(s, copyright->value, MKBETAG('T', 'C', 'O', 'P'));
- if(track) id3v2_put_ttag(s, track->value, MKBETAG('T', 'R', 'C', 'K'));
- if(year) id3v2_put_ttag(s, year->value, MKBETAG('T', 'Y', 'E', 'R'));
- if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
- id3v2_put_ttag(s, LIBAVFORMAT_IDENT, MKBETAG('T', 'E', 'N', 'C'));
- return 0;
-}
-
static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
{
put_buffer(s->pb, pkt->data, pkt->size);
mp3_write_trailer,
};
#endif
+
#if CONFIG_MP3_MUXER
+/**
+ * Write an ID3v2.4 header at beginning of stream
+ */
+
+static int mp3_write_header(struct AVFormatContext *s)
+{
+ AVMetadataTag *t = NULL;
+ int totlen = 0;
+ int64_t size_pos, cur_pos;
+
+ put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
+ put_byte(s->pb, 0);
+ put_byte(s->pb, 0); /* flags */
+
+ /* reserve space for size */
+ size_pos = url_ftell(s->pb);
+ put_be32(s->pb, 0);
+
+ ff_metadata_conv(&s->metadata, ff_id3v2_metadata_conv, NULL);
+ while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) {
+ uint32_t tag = 0;
+
+ if (t->key[0] == 'T' && strlen(t->key) == 4) {
+ int i;
+ for (i = 0; *ff_id3v2_tags[i]; i++)
+ if (AV_RB32(t->key) == AV_RB32(ff_id3v2_tags[i])) {
+ int len = strlen(t->value);
+ tag = AV_RB32(t->key);
+ totlen += len + ID3v2_HEADER_SIZE + 2;
+ id3v2_put_ttag(s, t->value, len + 1, tag);
+ break;
+ }
+ }
+
+ if (!tag) { /* unknown tag, write as TXXX frame */
+ int len = strlen(t->key), len1 = strlen(t->value);
+ char *buf = av_malloc(len + len1 + 2);
+ if (!buf)
+ return AVERROR(ENOMEM);
+ tag = MKBETAG('T', 'X', 'X', 'X');
+ strcpy(buf, t->key);
+ strcpy(buf + len + 1, t->value);
+ id3v2_put_ttag(s, buf, len + len1 + 2, tag);
+ totlen += len + len1 + ID3v2_HEADER_SIZE + 3;
+ av_free(buf);
+ }
+ }
+
+ cur_pos = url_ftell(s->pb);
+ url_fseek(s->pb, size_pos, SEEK_SET);
+ id3v2_put_size(s, totlen);
+ url_fseek(s->pb, cur_pos, SEEK_SET);
+
+ return 0;
+}
+
AVOutputFormat mp3_muxer = {
"mp3",
NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
mp3_write_header,
mp3_write_packet,
mp3_write_trailer,
- .metadata_conv = ff_id3v2_metadata_conv,
+ AVFMT_NOTIMESTAMPS,
};
#endif