3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/intreadwrite.h"
28 static int id3v1_set_string(AVFormatContext *s, const char *key,
29 uint8_t *buf, int buf_size)
32 if ((tag = av_metadata_get(s->metadata, key, NULL, 0)))
33 strncpy(buf, tag->value, buf_size);
37 static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
42 memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
46 count += id3v1_set_string(s, "TIT2", buf + 3, 30); //title
47 count += id3v1_set_string(s, "TPE1", buf + 33, 30); //author|artist
48 count += id3v1_set_string(s, "TALB", buf + 63, 30); //album
49 count += id3v1_set_string(s, "TDRL", buf + 93, 4); //date
50 count += id3v1_set_string(s, "comment", buf + 97, 30);
51 if ((tag = av_metadata_get(s->metadata, "TRCK", NULL, 0))) { //track
53 buf[126] = atoi(tag->value);
56 buf[127] = 0xFF; /* default to unknown genre */
57 if ((tag = av_metadata_get(s->metadata, "TCON", NULL, 0))) { //genre
58 for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
59 if (!strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
71 static void id3v2_put_size(AVFormatContext *s, int size)
73 put_byte(s->pb, size >> 21 & 0x7f);
74 put_byte(s->pb, size >> 14 & 0x7f);
75 put_byte(s->pb, size >> 7 & 0x7f);
76 put_byte(s->pb, size & 0x7f);
80 * Write a text frame with one (normal frames) or two (TXXX frames) strings
81 * according to encoding (only UTF-8 or UTF-16+BOM supported).
82 * @return number of bytes written.
84 static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
85 uint32_t tag, enum ID3v2Encoding enc)
89 void (*put)(ByteIOContext*, const char*) = avio_put_str;
90 ByteIOContext *dyn_buf;
91 if (url_open_dyn_buf(&dyn_buf) < 0)
94 put_byte(dyn_buf, enc);
95 if (enc == ID3v2_ENCODING_UTF16BOM) {
96 put_le16(dyn_buf, 0xFEFF); /* BOM */
97 put = avio_put_str16le;
102 len = url_close_dyn_buf(dyn_buf, &pb);
104 put_be32(s->pb, tag);
105 id3v2_put_size(s, len);
107 put_buffer(s->pb, pb, len);
110 return len + ID3v2_HEADER_SIZE;
114 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
116 put_buffer(s->pb, pkt->data, pkt->size);
117 put_flush_packet(s->pb);
121 static int mp3_write_trailer(struct AVFormatContext *s)
123 uint8_t buf[ID3v1_TAG_SIZE];
125 /* write the id3v1 tag */
126 if (id3v1_create_tag(s, buf) > 0) {
127 put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
128 put_flush_packet(s->pb);
134 AVOutputFormat mp2_muxer = {
136 NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
150 * Write an ID3v2.4 header at beginning of stream
153 static int mp3_write_header(struct AVFormatContext *s)
155 AVMetadataTag *t = NULL;
157 int64_t size_pos, cur_pos;
159 put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
161 put_byte(s->pb, 0); /* flags */
163 /* reserve space for size */
164 size_pos = url_ftell(s->pb);
167 ff_metadata_conv(&s->metadata, ff_id3v2_metadata_conv, NULL);
168 while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) {
171 if (t->key[0] == 'T' && strlen(t->key) == 4) {
173 for (i = 0; *ff_id3v2_tags[i]; i++)
174 if (AV_RB32(t->key) == AV_RB32(ff_id3v2_tags[i])) {
175 tag = AV_RB32(t->key);
176 totlen += id3v2_put_ttag(s, t->value, NULL, tag, ID3v2_ENCODING_UTF8);
181 if (!tag) { /* unknown tag, write as TXXX frame */
182 tag = MKBETAG('T', 'X', 'X', 'X');
183 totlen += id3v2_put_ttag(s, t->key, t->value, tag, ID3v2_ENCODING_UTF8);
187 cur_pos = url_ftell(s->pb);
188 url_fseek(s->pb, size_pos, SEEK_SET);
189 id3v2_put_size(s, totlen);
190 url_fseek(s->pb, cur_pos, SEEK_SET);
195 AVOutputFormat mp3_muxer = {
197 NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),