]> git.sesse.net Git - ffmpeg/blob - libavformat/mp3enc.c
mp3enc: add support for writing UTF-16 tags
[ffmpeg] / libavformat / mp3enc.c
1 /*
2  * MP3 muxer
3  * Copyright (c) 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <strings.h>
23 #include "avformat.h"
24 #include "id3v1.h"
25 #include "id3v2.h"
26 #include "libavutil/intreadwrite.h"
27
28 static int id3v1_set_string(AVFormatContext *s, const char *key,
29                             uint8_t *buf, int buf_size)
30 {
31     AVMetadataTag *tag;
32     if ((tag = av_metadata_get(s->metadata, key, NULL, 0)))
33         strncpy(buf, tag->value, buf_size);
34     return !!tag;
35 }
36
37 static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
38 {
39     AVMetadataTag *tag;
40     int i, count = 0;
41
42     memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
43     buf[0] = 'T';
44     buf[1] = 'A';
45     buf[2] = 'G';
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
52         buf[125] = 0;
53         buf[126] = atoi(tag->value);
54         count++;
55     }
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])) {
60                 buf[127] = i;
61                 count++;
62                 break;
63             }
64         }
65     }
66     return count;
67 }
68
69 /* simple formats */
70
71 static void id3v2_put_size(AVFormatContext *s, int size)
72 {
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);
77 }
78
79 /**
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.
83  */
84 static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
85                            uint32_t tag, enum ID3v2Encoding enc)
86 {
87     int len;
88     uint8_t *pb;
89     void (*put)(ByteIOContext*, const char*) = avio_put_str;
90     ByteIOContext *dyn_buf;
91     if (url_open_dyn_buf(&dyn_buf) < 0)
92         return 0;
93
94     put_byte(dyn_buf, enc);
95     if (enc == ID3v2_ENCODING_UTF16BOM) {
96         put_le16(dyn_buf, 0xFEFF);      /* BOM */
97         put = avio_put_str16le;
98     }
99     put(dyn_buf, str1);
100     if (str2)
101         put(dyn_buf, str2);
102     len = url_close_dyn_buf(dyn_buf, &pb);
103
104     put_be32(s->pb, tag);
105     id3v2_put_size(s, len);
106     put_be16(s->pb, 0);
107     put_buffer(s->pb, pb, len);
108
109     av_freep(&pb);
110     return len + ID3v2_HEADER_SIZE;
111 }
112
113
114 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
115 {
116     put_buffer(s->pb, pkt->data, pkt->size);
117     put_flush_packet(s->pb);
118     return 0;
119 }
120
121 static int mp3_write_trailer(struct AVFormatContext *s)
122 {
123     uint8_t buf[ID3v1_TAG_SIZE];
124
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);
129     }
130     return 0;
131 }
132
133 #if CONFIG_MP2_MUXER
134 AVOutputFormat mp2_muxer = {
135     "mp2",
136     NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
137     "audio/x-mpeg",
138     "mp2,m2a",
139     0,
140     CODEC_ID_MP2,
141     CODEC_ID_NONE,
142     NULL,
143     mp3_write_packet,
144     mp3_write_trailer,
145 };
146 #endif
147
148 #if CONFIG_MP3_MUXER
149 /**
150  * Write an ID3v2.4 header at beginning of stream
151  */
152
153 static int mp3_write_header(struct AVFormatContext *s)
154 {
155     AVMetadataTag *t = NULL;
156     int totlen = 0;
157     int64_t size_pos, cur_pos;
158
159     put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
160     put_byte(s->pb, 0);
161     put_byte(s->pb, 0); /* flags */
162
163     /* reserve space for size */
164     size_pos = url_ftell(s->pb);
165     put_be32(s->pb, 0);
166
167     ff_metadata_conv(&s->metadata, ff_id3v2_metadata_conv, NULL);
168     while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) {
169         uint32_t tag = 0;
170
171         if (t->key[0] == 'T' && strlen(t->key) == 4) {
172             int i;
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);
177                     break;
178                 }
179         }
180
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);
184         }
185     }
186
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);
191
192     return 0;
193 }
194
195 AVOutputFormat mp3_muxer = {
196     "mp3",
197     NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
198     "audio/x-mpeg",
199     "mp3",
200     0,
201     CODEC_ID_MP3,
202     CODEC_ID_NONE,
203     mp3_write_header,
204     mp3_write_packet,
205     mp3_write_trailer,
206     AVFMT_NOTIMESTAMPS,
207 };
208 #endif