ffmpeg / libavformat / mp3enc.c @ fe01dd8d
History | View | Annotate | Download (6.09 KB)
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 or a negative error code.
|
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 |
int (*put)(ByteIOContext*, const char*); |
90 |
ByteIOContext *dyn_buf; |
91 |
if (url_open_dyn_buf(&dyn_buf) < 0) |
92 |
return AVERROR(ENOMEM);
|
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 |
} else
|
99 |
put = avio_put_str; |
100 |
|
101 |
put(dyn_buf, str1); |
102 |
if (str2)
|
103 |
put(dyn_buf, str2); |
104 |
len = url_close_dyn_buf(dyn_buf, &pb); |
105 |
|
106 |
put_be32(s->pb, tag); |
107 |
id3v2_put_size(s, len); |
108 |
put_be16(s->pb, 0);
|
109 |
put_buffer(s->pb, pb, len); |
110 |
|
111 |
av_freep(&pb); |
112 |
return len + ID3v2_HEADER_SIZE;
|
113 |
} |
114 |
|
115 |
|
116 |
static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt) |
117 |
{ |
118 |
put_buffer(s->pb, pkt->data, pkt->size); |
119 |
put_flush_packet(s->pb); |
120 |
return 0; |
121 |
} |
122 |
|
123 |
static int mp3_write_trailer(struct AVFormatContext *s) |
124 |
{ |
125 |
uint8_t buf[ID3v1_TAG_SIZE]; |
126 |
|
127 |
/* write the id3v1 tag */
|
128 |
if (id3v1_create_tag(s, buf) > 0) { |
129 |
put_buffer(s->pb, buf, ID3v1_TAG_SIZE); |
130 |
put_flush_packet(s->pb); |
131 |
} |
132 |
return 0; |
133 |
} |
134 |
|
135 |
#if CONFIG_MP2_MUXER
|
136 |
AVOutputFormat mp2_muxer = { |
137 |
"mp2",
|
138 |
NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
|
139 |
"audio/x-mpeg",
|
140 |
"mp2,m2a",
|
141 |
0,
|
142 |
CODEC_ID_MP2, |
143 |
CODEC_ID_NONE, |
144 |
NULL,
|
145 |
mp3_write_packet, |
146 |
mp3_write_trailer, |
147 |
}; |
148 |
#endif
|
149 |
|
150 |
#if CONFIG_MP3_MUXER
|
151 |
static int id3v2_check_write_tag(AVFormatContext *s, AVMetadataTag *t, const char table[][4]) |
152 |
{ |
153 |
uint32_t tag; |
154 |
int i;
|
155 |
|
156 |
if (t->key[0] != 'T' || strlen(t->key) != 4) |
157 |
return -1; |
158 |
tag = AV_RB32(t->key); |
159 |
for (i = 0; *table[i]; i++) |
160 |
if (tag == AV_RB32(table[i]))
|
161 |
return id3v2_put_ttag(s, t->value, NULL, tag, ID3v2_ENCODING_UTF8); |
162 |
return -1; |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Write an ID3v2.4 header at beginning of stream
|
167 |
*/
|
168 |
|
169 |
static int mp3_write_header(struct AVFormatContext *s) |
170 |
{ |
171 |
AVMetadataTag *t = NULL;
|
172 |
int totlen = 0; |
173 |
int64_t size_pos, cur_pos; |
174 |
|
175 |
put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */ |
176 |
put_byte(s->pb, 0);
|
177 |
put_byte(s->pb, 0); /* flags */ |
178 |
|
179 |
/* reserve space for size */
|
180 |
size_pos = url_ftell(s->pb); |
181 |
put_be32(s->pb, 0);
|
182 |
|
183 |
ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
|
184 |
ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
|
185 |
while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) { |
186 |
int ret;
|
187 |
|
188 |
if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_tags)) > 0) { |
189 |
totlen += ret; |
190 |
continue;
|
191 |
} |
192 |
if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_4_tags)) > 0) { |
193 |
totlen += ret; |
194 |
continue;
|
195 |
} |
196 |
|
197 |
/* unknown tag, write as TXXX frame */
|
198 |
if ((ret = id3v2_put_ttag(s, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), |
199 |
ID3v2_ENCODING_UTF8)) < 0)
|
200 |
return ret;
|
201 |
totlen += ret; |
202 |
} |
203 |
|
204 |
cur_pos = url_ftell(s->pb); |
205 |
url_fseek(s->pb, size_pos, SEEK_SET); |
206 |
id3v2_put_size(s, totlen); |
207 |
url_fseek(s->pb, cur_pos, SEEK_SET); |
208 |
|
209 |
return 0; |
210 |
} |
211 |
|
212 |
AVOutputFormat mp3_muxer = { |
213 |
"mp3",
|
214 |
NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
|
215 |
"audio/x-mpeg",
|
216 |
"mp3",
|
217 |
0,
|
218 |
CODEC_ID_MP3, |
219 |
CODEC_ID_NONE, |
220 |
mp3_write_header, |
221 |
mp3_write_packet, |
222 |
mp3_write_trailer, |
223 |
AVFMT_NOTIMESTAMPS, |
224 |
}; |
225 |
#endif
|