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);
79 static void id3v2_put_ttag(AVFormatContext *s, const char *buf, int len,
83 id3v2_put_size(s, len + 1);
85 put_byte(s->pb, 3); /* UTF-8 */
86 put_buffer(s->pb, buf, len);
90 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
92 put_buffer(s->pb, pkt->data, pkt->size);
93 put_flush_packet(s->pb);
97 static int mp3_write_trailer(struct AVFormatContext *s)
99 uint8_t buf[ID3v1_TAG_SIZE];
101 /* write the id3v1 tag */
102 if (id3v1_create_tag(s, buf) > 0) {
103 put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
104 put_flush_packet(s->pb);
110 AVOutputFormat mp2_muxer = {
112 NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
126 * Write an ID3v2.4 header at beginning of stream
129 static int mp3_write_header(struct AVFormatContext *s)
131 AVMetadataTag *t = NULL;
133 int64_t size_pos, cur_pos;
135 put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
137 put_byte(s->pb, 0); /* flags */
139 /* reserve space for size */
140 size_pos = url_ftell(s->pb);
143 ff_metadata_conv(&s->metadata, ff_id3v2_metadata_conv, NULL);
144 while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) {
147 if (t->key[0] == 'T' && strlen(t->key) == 4) {
149 for (i = 0; *ff_id3v2_tags[i]; i++)
150 if (AV_RB32(t->key) == AV_RB32(ff_id3v2_tags[i])) {
151 int len = strlen(t->value);
152 tag = AV_RB32(t->key);
153 totlen += len + ID3v2_HEADER_SIZE + 2;
154 id3v2_put_ttag(s, t->value, len + 1, tag);
159 if (!tag) { /* unknown tag, write as TXXX frame */
160 int len = strlen(t->key), len1 = strlen(t->value);
161 char *buf = av_malloc(len + len1 + 2);
163 return AVERROR(ENOMEM);
164 tag = MKBETAG('T', 'X', 'X', 'X');
166 strcpy(buf + len + 1, t->value);
167 id3v2_put_ttag(s, buf, len + len1 + 2, tag);
168 totlen += len + len1 + ID3v2_HEADER_SIZE + 3;
173 cur_pos = url_ftell(s->pb);
174 url_fseek(s->pb, size_pos, SEEK_SET);
175 id3v2_put_size(s, totlen);
176 url_fseek(s->pb, cur_pos, SEEK_SET);
181 AVOutputFormat mp3_muxer = {
183 NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),