From 0dc56bfae0991568a325c5776630e0b74f966b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=96man?= Date: Tue, 22 May 2007 08:28:32 +0000 Subject: [PATCH] =?UTF-8?q?id3v2=20writer=20patch=20by=20Andreas=20=C3=83?= =?UTF-8?q?=C2=96man=20andreas=20olebyn=20nu=20original=20thread:=20[FFmpe?= =?UTF-8?q?g-devel]=20[Ffmpeg-devel]=20ID3v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Originally committed as revision 9102 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavformat/mp3.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/libavformat/mp3.c b/libavformat/mp3.c index 7daed9ec55..c0d7f8a11c 100644 --- a/libavformat/mp3.c +++ b/libavformat/mp3.c @@ -498,8 +498,64 @@ static int mp3_read_close(AVFormatContext *s) #ifdef CONFIG_MUXERS /* simple formats */ + +static void id3v2_put_size(AVFormatContext *s, int size) +{ + put_byte(&s->pb, size >> 21 & 0x7f); + put_byte(&s->pb, size >> 14 & 0x7f); + put_byte(&s->pb, size >> 7 & 0x7f); + put_byte(&s->pb, size & 0x7f); +} + +static void id3v2_put_ttag(AVFormatContext *s, char *string, 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); +} + + +/** + * Write an ID3v2.4 header at beginning of stream + */ + static int mp3_write_header(struct AVFormatContext *s) { + int totlen = 0; + char tracktxt[10]; + + if(s->track) + snprintf(tracktxt, sizeof(tracktxt) - 1, "%d", s->track); + + if(s->title[0]) totlen += 11 + strlen(s->title); + if(s->author[0]) totlen += 11 + strlen(s->author); + if(s->album[0]) totlen += 11 + strlen(s->album); + if(s->genre[0]) totlen += 11 + strlen(s->genre); + if(s->copyright[0]) totlen += 11 + strlen(s->copyright); + if(s->track) totlen += 11 + strlen(tracktxt); + 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(s->title[0]) id3v2_put_ttag(s, s->title, MKBETAG('T', 'I', 'T', '2')); + if(s->author[0]) id3v2_put_ttag(s, s->author, MKBETAG('T', 'P', 'E', '1')); + if(s->album[0]) id3v2_put_ttag(s, s->album, MKBETAG('T', 'A', 'L', 'B')); + if(s->genre[0]) id3v2_put_ttag(s, s->genre, MKBETAG('T', 'C', 'O', 'N')); + if(s->copyright[0]) id3v2_put_ttag(s, s->copyright, MKBETAG('T', 'C', 'O', 'P')); + if(s->track) id3v2_put_ttag(s, tracktxt, MKBETAG('T', 'R', 'C', 'K')); + if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) + id3v2_put_ttag(s, LIBAVFORMAT_IDENT, MKBETAG('T', 'E', 'N', 'C')); return 0; }