Line | Branch | Exec | Source |
---|---|---|---|
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 "avformat.h" | ||
23 | #include "avio_internal.h" | ||
24 | #include "id3v1.h" | ||
25 | #include "id3v2.h" | ||
26 | #include "mux.h" | ||
27 | #include "rawenc.h" | ||
28 | #include "libavutil/avstring.h" | ||
29 | #include "libavcodec/mpegaudio.h" | ||
30 | #include "libavcodec/mpegaudiodata.h" | ||
31 | #include "libavcodec/mpegaudiodecheader.h" | ||
32 | #include "libavcodec/packet_internal.h" | ||
33 | #include "libavutil/intreadwrite.h" | ||
34 | #include "libavutil/opt.h" | ||
35 | #include "libavutil/dict.h" | ||
36 | #include "libavutil/avassert.h" | ||
37 | #include "libavutil/crc.h" | ||
38 | #include "libavutil/mathematics.h" | ||
39 | #include "libavutil/replaygain.h" | ||
40 | |||
41 | ✗ | static int id3v1_set_string(AVFormatContext *s, const char *key, | |
42 | uint8_t *buf, int buf_size) | ||
43 | { | ||
44 | AVDictionaryEntry *tag; | ||
45 | ✗ | if ((tag = av_dict_get(s->metadata, key, NULL, 0))) | |
46 | ✗ | av_strlcpy(buf, tag->value, buf_size); | |
47 | ✗ | return !!tag; | |
48 | } | ||
49 | |||
50 | // refer to: http://id3.org/ID3v1 | ||
51 | ✗ | static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf) | |
52 | { | ||
53 | AVDictionaryEntry *tag; | ||
54 | ✗ | int i, count = 0; | |
55 | |||
56 | ✗ | memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */ | |
57 | ✗ | buf[0] = 'T'; | |
58 | ✗ | buf[1] = 'A'; | |
59 | ✗ | buf[2] = 'G'; | |
60 | /* we knowingly overspecify each tag length by one byte to compensate for the mandatory null byte added by av_strlcpy */ | ||
61 | ✗ | count += id3v1_set_string(s, "TIT2", buf + 3, 30 + 1); //title | |
62 | ✗ | count += id3v1_set_string(s, "TPE1", buf + 33, 30 + 1); //author|artist | |
63 | ✗ | count += id3v1_set_string(s, "TALB", buf + 63, 30 + 1); //album | |
64 | ✗ | if ((tag = av_dict_get(s->metadata, "TYER", NULL, 0))) { //year | |
65 | ✗ | av_strlcpy(buf + 93, tag->value, 4 + 1); | |
66 | ✗ | count++; | |
67 | ✗ | } else if ((tag = av_dict_get(s->metadata, "TDRC", NULL, 0))) { | |
68 | ✗ | av_strlcpy(buf + 93, tag->value, 4 + 1); | |
69 | ✗ | count++; | |
70 | ✗ | } else if ((tag = av_dict_get(s->metadata, "TDAT", NULL, 0))) { | |
71 | ✗ | av_strlcpy(buf + 93, tag->value, 4 + 1); | |
72 | ✗ | count++; | |
73 | } | ||
74 | |||
75 | ✗ | count += id3v1_set_string(s, "comment", buf + 97, 30 + 1); | |
76 | ✗ | if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) { //track | |
77 | ✗ | buf[125] = 0; | |
78 | ✗ | buf[126] = atoi(tag->value); | |
79 | ✗ | count++; | |
80 | } | ||
81 | ✗ | buf[127] = 0xFF; /* default to unknown genre */ | |
82 | ✗ | if ((tag = av_dict_get(s->metadata, "TCON", NULL, 0))) { //genre | |
83 | ✗ | for(i = 0; i <= ID3v1_GENRE_MAX; i++) { | |
84 | ✗ | if (!av_strcasecmp(tag->value, ff_id3v1_genre_str[i])) { | |
85 | ✗ | buf[127] = i; | |
86 | ✗ | count++; | |
87 | ✗ | break; | |
88 | } | ||
89 | } | ||
90 | } | ||
91 | ✗ | return count; | |
92 | } | ||
93 | |||
94 | #define XING_NUM_BAGS 400 | ||
95 | #define XING_TOC_SIZE 100 | ||
96 | // size of the XING/LAME data, starting from the Xing tag | ||
97 | #define XING_SIZE 156 | ||
98 | |||
99 | typedef struct MP3Context { | ||
100 | const AVClass *class; | ||
101 | ID3v2EncContext id3; | ||
102 | int id3v2_version; | ||
103 | int write_id3v1; | ||
104 | int write_xing; | ||
105 | |||
106 | /* xing header */ | ||
107 | // a buffer containing the whole XING/LAME frame | ||
108 | uint8_t *xing_frame; | ||
109 | int xing_frame_size; | ||
110 | |||
111 | AVCRC audio_crc; // CRC of the audio data | ||
112 | uint32_t audio_size; // total size of the audio data | ||
113 | |||
114 | // offset of the XING/LAME frame in the file | ||
115 | int64_t xing_frame_offset; | ||
116 | // offset of the XING/INFO tag in the frame | ||
117 | int xing_offset; | ||
118 | |||
119 | int32_t frames; | ||
120 | int32_t size; | ||
121 | uint32_t want; | ||
122 | uint32_t seen; | ||
123 | uint32_t pos; | ||
124 | uint64_t bag[XING_NUM_BAGS]; | ||
125 | int initial_bitrate; | ||
126 | int has_variable_bitrate; | ||
127 | int delay; | ||
128 | int padding; | ||
129 | |||
130 | /* index of the audio stream */ | ||
131 | int audio_stream_idx; | ||
132 | /* number of attached pictures we still need to write */ | ||
133 | int pics_to_write; | ||
134 | |||
135 | /* audio packets are queued here until we get all the attached pictures */ | ||
136 | PacketList queue; | ||
137 | } MP3Context; | ||
138 | |||
139 | static const uint8_t xing_offtbl[2][2] = {{32, 17}, {17, 9}}; | ||
140 | |||
141 | /* | ||
142 | * Write an empty XING header and initialize respective data. | ||
143 | */ | ||
144 | 3 | static int mp3_write_xing(AVFormatContext *s) | |
145 | { | ||
146 | 3 | MP3Context *mp3 = s->priv_data; | |
147 | 3 | AVCodecParameters *par = s->streams[mp3->audio_stream_idx]->codecpar; | |
148 | 3 | AVDictionaryEntry *enc = av_dict_get(s->streams[mp3->audio_stream_idx]->metadata, "encoder", NULL, 0); | |
149 | AVIOContext *dyn_ctx; | ||
150 | int32_t header; | ||
151 | MPADecodeHeader mpah; | ||
152 | int srate_idx, i, channels; | ||
153 | int bitrate_idx; | ||
154 | 3 | int best_bitrate_idx = -1; | |
155 | 3 | int best_bitrate_error = INT_MAX; | |
156 | int ret; | ||
157 | 3 | int ver = 0; | |
158 | int bytes_needed; | ||
159 | |||
160 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) || !mp3->write_xing) |
161 | ✗ | return 0; | |
162 | |||
163 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | for (i = 0; i < FF_ARRAY_ELEMS(ff_mpa_freq_tab); i++) { |
164 | 6 | const uint16_t base_freq = ff_mpa_freq_tab[i]; | |
165 | |||
166 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (par->sample_rate == base_freq) ver = 0x3; // MPEG 1 |
167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | else if (par->sample_rate == base_freq / 2) ver = 0x2; // MPEG 2 |
168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | else if (par->sample_rate == base_freq / 4) ver = 0x0; // MPEG 2.5 |
169 | 3 | else continue; | |
170 | |||
171 | 3 | srate_idx = i; | |
172 | 3 | break; | |
173 | } | ||
174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (i == FF_ARRAY_ELEMS(ff_mpa_freq_tab)) { |
175 | ✗ | av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n"); | |
176 | ✗ | return -1; | |
177 | } | ||
178 | |||
179 |
2/3✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
3 | switch (par->ch_layout.nb_channels) { |
180 | 2 | case 1: channels = MPA_MONO; break; | |
181 | 1 | case 2: channels = MPA_STEREO; break; | |
182 | ✗ | default: av_log(s, AV_LOG_WARNING, "Unsupported number of channels, " | |
183 | "not writing Xing header.\n"); | ||
184 | ✗ | return -1; | |
185 | } | ||
186 | |||
187 | /* dummy MPEG audio header */ | ||
188 | 3 | header = 0xffU << 24; // sync | |
189 | 3 | header |= (0x7 << 5 | ver << 3 | 0x1 << 1 | 0x1) << 16; // sync/audio-version/layer 3/no crc*/ | |
190 | 3 | header |= (srate_idx << 2) << 8; | |
191 | 3 | header |= channels << 6; | |
192 | |||
193 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 3 times.
|
45 | for (bitrate_idx = 1; bitrate_idx < 15; bitrate_idx++) { |
194 | 42 | int bit_rate = 1000 * ff_mpa_bitrate_tab[ver != 3][3 - 1][bitrate_idx]; | |
195 | 42 | int error = FFABS(bit_rate - par->bit_rate); | |
196 | |||
197 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 25 times.
|
42 | if (error < best_bitrate_error) { |
198 | 17 | best_bitrate_error = error; | |
199 | 17 | best_bitrate_idx = bitrate_idx; | |
200 | } | ||
201 | } | ||
202 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | av_assert0(best_bitrate_idx >= 0); |
203 | |||
204 | 3 | for (bitrate_idx = best_bitrate_idx; ; bitrate_idx++) { | |
205 | 3 | int32_t mask = bitrate_idx << (4 + 8); | |
206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (15 == bitrate_idx) |
207 | ✗ | return -1; | |
208 | 3 | header |= mask; | |
209 | |||
210 | 3 | ret = avpriv_mpegaudio_decode_header(&mpah, header); | |
211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | av_assert0(ret >= 0); |
212 | 3 | mp3->xing_offset = xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1] + 4; | |
213 | 3 | bytes_needed = mp3->xing_offset + XING_SIZE; | |
214 | |||
215 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (bytes_needed <= mpah.frame_size) |
216 | 3 | break; | |
217 | |||
218 | ✗ | header &= ~mask; | |
219 | } | ||
220 | |||
221 | 3 | ret = avio_open_dyn_buf(&dyn_ctx); | |
222 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
223 | ✗ | return ret; | |
224 | |||
225 | 3 | avio_wb32(dyn_ctx, header); | |
226 | |||
227 | 3 | ffio_fill(dyn_ctx, 0, mp3->xing_offset - 4); | |
228 | 3 | ffio_wfourcc(dyn_ctx, "Xing"); | |
229 | 3 | avio_wb32(dyn_ctx, 0x01 | 0x02 | 0x04 | 0x08); // frames / size / TOC / vbr scale | |
230 | |||
231 | 3 | mp3->size = mpah.frame_size; | |
232 | 3 | mp3->want=1; | |
233 | 3 | mp3->seen=0; | |
234 | 3 | mp3->pos=0; | |
235 | |||
236 | 3 | avio_wb32(dyn_ctx, 0); // frames | |
237 | 3 | avio_wb32(dyn_ctx, 0); // size | |
238 | |||
239 | // TOC | ||
240 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 3 times.
|
303 | for (i = 0; i < XING_TOC_SIZE; i++) |
241 | 300 | avio_w8(dyn_ctx, (uint8_t)(255 * i / XING_TOC_SIZE)); | |
242 | |||
243 | // vbr quality | ||
244 | // we write it, because some (broken) tools always expect it to be present | ||
245 | 3 | avio_wb32(dyn_ctx, 0); | |
246 | |||
247 | // encoder short version string | ||
248 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (enc) { |
249 | ✗ | uint8_t encoder_str[9] = { 0 }; | |
250 | ✗ | if ( strlen(enc->value) > sizeof(encoder_str) | |
251 | ✗ | && !strcmp("Lavc libmp3lame", enc->value)) { | |
252 | ✗ | memcpy(encoder_str, "Lavf lame", 9); | |
253 | } else | ||
254 | ✗ | memcpy(encoder_str, enc->value, FFMIN(strlen(enc->value), sizeof(encoder_str))); | |
255 | |||
256 | ✗ | avio_write(dyn_ctx, encoder_str, sizeof(encoder_str)); | |
257 | } else | ||
258 | 3 | avio_write(dyn_ctx, "Lavf\0\0\0\0\0", 9); | |
259 | |||
260 | 3 | avio_w8(dyn_ctx, 0); // tag revision 0 / unknown vbr method | |
261 | 3 | avio_w8(dyn_ctx, 0); // unknown lowpass filter value | |
262 | 3 | ffio_fill(dyn_ctx, 0, 8); // empty replaygain fields | |
263 | 3 | avio_w8(dyn_ctx, 0); // unknown encoding flags | |
264 | 3 | avio_w8(dyn_ctx, 0); // unknown abr/minimal bitrate | |
265 | 3 | avio_wb24(dyn_ctx, 0); // empty encoder delay/padding | |
266 | |||
267 | 3 | avio_w8(dyn_ctx, 0); // misc | |
268 | 3 | avio_w8(dyn_ctx, 0); // mp3gain | |
269 | 3 | avio_wb16(dyn_ctx, 0); // preset | |
270 | |||
271 | // audio length and CRCs (will be updated later) | ||
272 | 3 | avio_wb32(dyn_ctx, 0); // music length | |
273 | 3 | avio_wb16(dyn_ctx, 0); // music crc | |
274 | 3 | avio_wb16(dyn_ctx, 0); // tag crc | |
275 | |||
276 | 3 | ffio_fill(dyn_ctx, 0, mpah.frame_size - bytes_needed); | |
277 | |||
278 | 3 | mp3->xing_frame_size = avio_close_dyn_buf(dyn_ctx, &mp3->xing_frame); | |
279 | 3 | mp3->xing_frame_offset = avio_tell(s->pb); | |
280 | 3 | avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size); | |
281 | |||
282 | 3 | mp3->audio_size = mp3->xing_frame_size; | |
283 | |||
284 | 3 | return 0; | |
285 | } | ||
286 | |||
287 | /* | ||
288 | * Add a frame to XING data. | ||
289 | * Following lame's "VbrTag.c". | ||
290 | */ | ||
291 | 214 | static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt) | |
292 | { | ||
293 | int i; | ||
294 | |||
295 | 214 | mp3->frames++; | |
296 | 214 | mp3->seen++; | |
297 | 214 | mp3->size += pkt->size; | |
298 | |||
299 |
1/2✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
|
214 | if (mp3->want == mp3->seen) { |
300 | 214 | mp3->bag[mp3->pos] = mp3->size; | |
301 | |||
302 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 214 times.
|
214 | if (XING_NUM_BAGS == ++mp3->pos) { |
303 | /* shrink table to half size by throwing away each second bag. */ | ||
304 | ✗ | for (i = 1; i < XING_NUM_BAGS; i += 2) | |
305 | ✗ | mp3->bag[i >> 1] = mp3->bag[i]; | |
306 | |||
307 | /* double wanted amount per bag. */ | ||
308 | ✗ | mp3->want *= 2; | |
309 | /* adjust current position to half of table size. */ | ||
310 | ✗ | mp3->pos = XING_NUM_BAGS / 2; | |
311 | } | ||
312 | |||
313 | 214 | mp3->seen = 0; | |
314 | } | ||
315 | 214 | } | |
316 | |||
317 | 214 | static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt) | |
318 | { | ||
319 | 214 | MP3Context *mp3 = s->priv_data; | |
320 | |||
321 |
2/4✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 214 times.
✗ Branch 3 not taken.
|
214 | if (pkt->data && pkt->size >= 4) { |
322 | MPADecodeHeader mpah; | ||
323 | int ret; | ||
324 | int av_unused base; | ||
325 | uint32_t h; | ||
326 | |||
327 | 214 | h = AV_RB32(pkt->data); | |
328 | 214 | ret = avpriv_mpegaudio_decode_header(&mpah, h); | |
329 |
1/2✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
|
214 | if (ret >= 0) { |
330 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 211 times.
|
214 | if (!mp3->initial_bitrate) |
331 | 3 | mp3->initial_bitrate = mpah.bit_rate; | |
332 |
3/4✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 74 times.
|
214 | if ((mpah.bit_rate == 0) || (mp3->initial_bitrate != mpah.bit_rate)) |
333 | 140 | mp3->has_variable_bitrate = 1; | |
334 | } else { | ||
335 | ✗ | av_log(s, AV_LOG_WARNING, "Audio packet of size %d (starting with %08"PRIX32"...) " | |
336 | "is invalid, writing it anyway.\n", pkt->size, h); | ||
337 | } | ||
338 | |||
339 | #ifdef FILTER_VBR_HEADERS | ||
340 | /* filter out XING and INFO headers. */ | ||
341 | base = 4 + xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1]; | ||
342 | |||
343 | if (base + 4 <= pkt->size) { | ||
344 | uint32_t v = AV_RB32(pkt->data + base); | ||
345 | |||
346 | if (MKBETAG('X','i','n','g') == v || MKBETAG('I','n','f','o') == v) | ||
347 | return 0; | ||
348 | } | ||
349 | |||
350 | /* filter out VBRI headers. */ | ||
351 | base = 4 + 32; | ||
352 | |||
353 | if (base + 4 <= pkt->size && MKBETAG('V','B','R','I') == AV_RB32(pkt->data + base)) | ||
354 | return 0; | ||
355 | #endif | ||
356 | |||
357 |
1/2✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
|
214 | if (mp3->xing_offset) { |
358 | 214 | uint8_t *side_data = NULL; | |
359 | size_t side_data_size; | ||
360 | |||
361 | 214 | mp3_xing_add_frame(mp3, pkt); | |
362 | 214 | mp3->audio_size += pkt->size; | |
363 | 428 | mp3->audio_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), | |
364 | 214 | mp3->audio_crc, pkt->data, pkt->size); | |
365 | |||
366 | 214 | side_data = av_packet_get_side_data(pkt, | |
367 | AV_PKT_DATA_SKIP_SAMPLES, | ||
368 | &side_data_size); | ||
369 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 213 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
214 | if (side_data && side_data_size >= 10) { |
370 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | mp3->padding = FFMAX(AV_RL32(side_data + 4) + 528 + 1, 0); |
371 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!mp3->delay) |
372 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | mp3->delay = FFMAX(AV_RL32(side_data) - 528 - 1, 0); |
373 | } else { | ||
374 | 213 | mp3->padding = 0; | |
375 | } | ||
376 | } | ||
377 | } | ||
378 | |||
379 | 214 | return ff_raw_write_packet(s, pkt); | |
380 | } | ||
381 | |||
382 | 1 | static int mp3_queue_flush(AVFormatContext *s) | |
383 | { | ||
384 | 1 | MP3Context *mp3 = s->priv_data; | |
385 | 1 | AVPacket *const pkt = ffformatcontext(s)->pkt; | |
386 | 1 | int ret = 0, write = 1; | |
387 | |||
388 | 1 | ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding); | |
389 | 1 | mp3_write_xing(s); | |
390 | |||
391 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | while (mp3->queue.head) { |
392 | 1 | avpriv_packet_list_get(&mp3->queue, pkt); | |
393 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (write && (ret = mp3_write_audio_packet(s, pkt)) < 0) |
394 | ✗ | write = 0; | |
395 | 1 | av_packet_unref(pkt); | |
396 | } | ||
397 | 1 | return ret; | |
398 | } | ||
399 | |||
400 | 3 | static void mp3_update_xing(AVFormatContext *s) | |
401 | { | ||
402 | 3 | MP3Context *mp3 = s->priv_data; | |
403 | const AVPacketSideData *sd; | ||
404 | AVReplayGain *rg; | ||
405 | uint16_t tag_crc; | ||
406 | uint8_t *toc; | ||
407 | int i; | ||
408 | 3 | int64_t old_pos = avio_tell(s->pb); | |
409 | |||
410 | /* replace "Xing" identification string with "Info" for CBR files. */ | ||
411 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (!mp3->has_variable_bitrate) |
412 | 2 | AV_WL32(mp3->xing_frame + mp3->xing_offset, MKTAG('I', 'n', 'f', 'o')); | |
413 | |||
414 | 3 | AV_WB32(mp3->xing_frame + mp3->xing_offset + 8, mp3->frames); | |
415 | 3 | AV_WB32(mp3->xing_frame + mp3->xing_offset + 12, mp3->size); | |
416 | |||
417 | 3 | toc = mp3->xing_frame + mp3->xing_offset + 16; | |
418 | 3 | toc[0] = 0; // first toc entry has to be zero. | |
419 |
2/2✓ Branch 0 taken 297 times.
✓ Branch 1 taken 3 times.
|
300 | for (i = 1; i < XING_TOC_SIZE; ++i) { |
420 | 297 | int j = i * mp3->pos / XING_TOC_SIZE; | |
421 | 297 | int seek_point = 256LL * mp3->bag[j] / mp3->size; | |
422 | 297 | toc[i] = FFMIN(seek_point, 255); | |
423 | } | ||
424 | |||
425 | /* write replaygain */ | ||
426 | 3 | sd = av_packet_side_data_get(s->streams[0]->codecpar->coded_side_data, | |
427 | 3 | s->streams[0]->codecpar->nb_coded_side_data, | |
428 | AV_PKT_DATA_REPLAYGAIN); | ||
429 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
3 | if (sd && sd->size >= sizeof(*rg)) { |
430 | uint16_t val; | ||
431 | |||
432 | ✗ | rg = (AVReplayGain *)sd->data; | |
433 | ✗ | AV_WB32(mp3->xing_frame + mp3->xing_offset + 131, | |
434 | av_rescale(rg->track_peak, 1 << 23, 100000)); | ||
435 | |||
436 | ✗ | if (rg->track_gain != INT32_MIN) { | |
437 | ✗ | val = FFABS(rg->track_gain / 10000) & ((1 << 9) - 1); | |
438 | ✗ | val |= (rg->track_gain < 0) << 9; | |
439 | ✗ | val |= 1 << 13; | |
440 | ✗ | AV_WB16(mp3->xing_frame + mp3->xing_offset + 135, val); | |
441 | } | ||
442 | |||
443 | ✗ | if (rg->album_gain != INT32_MIN) { | |
444 | ✗ | val = FFABS(rg->album_gain / 10000) & ((1 << 9) - 1); | |
445 | ✗ | val |= (rg->album_gain < 0) << 9; | |
446 | ✗ | val |= 1 << 14; | |
447 | ✗ | AV_WB16(mp3->xing_frame + mp3->xing_offset + 137, val); | |
448 | } | ||
449 | } | ||
450 | |||
451 | /* write encoder delay/padding */ | ||
452 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (mp3->delay >= 1 << 12) { |
453 | ✗ | mp3->delay = (1 << 12) - 1; | |
454 | ✗ | av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n"); | |
455 | } | ||
456 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (mp3->padding >= 1 << 12) { |
457 | ✗ | mp3->padding = (1 << 12) - 1; | |
458 | ✗ | av_log(s, AV_LOG_WARNING, "Too many samples of trailing padding.\n"); | |
459 | } | ||
460 | 3 | AV_WB24(mp3->xing_frame + mp3->xing_offset + 141, (mp3->delay << 12) + mp3->padding); | |
461 | |||
462 | 3 | AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size); | |
463 | 3 | AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc); | |
464 | |||
465 | 3 | tag_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), 0, mp3->xing_frame, 190); | |
466 | 3 | AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 2, tag_crc); | |
467 | |||
468 | 3 | avio_seek(s->pb, mp3->xing_frame_offset, SEEK_SET); | |
469 | 3 | avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size); | |
470 | 3 | avio_seek(s->pb, old_pos, SEEK_SET); | |
471 | 3 | } | |
472 | |||
473 | 3 | static int mp3_write_trailer(struct AVFormatContext *s) | |
474 | { | ||
475 | uint8_t buf[ID3v1_TAG_SIZE]; | ||
476 | 3 | MP3Context *mp3 = s->priv_data; | |
477 | |||
478 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (mp3->pics_to_write) { |
479 | ✗ | av_log(s, AV_LOG_WARNING, "No packets were sent for some of the " | |
480 | "attached pictures.\n"); | ||
481 | ✗ | mp3_queue_flush(s); | |
482 | } | ||
483 | |||
484 | /* write the id3v1 tag */ | ||
485 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
3 | if (mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) { |
486 | ✗ | avio_write(s->pb, buf, ID3v1_TAG_SIZE); | |
487 | } | ||
488 | |||
489 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (mp3->xing_offset) |
490 | 3 | mp3_update_xing(s); | |
491 | |||
492 | 3 | return 0; | |
493 | } | ||
494 | |||
495 | 2 | static int query_codec(enum AVCodecID id, int std_compliance) | |
496 | { | ||
497 | 2 | const CodecMime *cm= ff_id3v2_mime_tags; | |
498 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | while(cm->id != AV_CODEC_ID_NONE) { |
499 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if(id == cm->id) |
500 | 2 | return MKTAG('A', 'P', 'I', 'C'); | |
501 | 6 | cm++; | |
502 | } | ||
503 | ✗ | return -1; | |
504 | } | ||
505 | |||
506 | static const AVOption options[] = { | ||
507 | { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.", | ||
508 | offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 0, 4, AV_OPT_FLAG_ENCODING_PARAM}, | ||
509 | { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.", | ||
510 | offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM}, | ||
511 | { "write_xing", "Write the Xing header containing file duration.", | ||
512 | offsetof(MP3Context, write_xing), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM}, | ||
513 | { NULL }, | ||
514 | }; | ||
515 | |||
516 | static const AVClass mp3_muxer_class = { | ||
517 | .class_name = "MP3 muxer", | ||
518 | .item_name = av_default_item_name, | ||
519 | .option = options, | ||
520 | .version = LIBAVUTIL_VERSION_INT, | ||
521 | }; | ||
522 | |||
523 | 217 | static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt) | |
524 | { | ||
525 | 217 | MP3Context *mp3 = s->priv_data; | |
526 | |||
527 |
2/2✓ Branch 0 taken 214 times.
✓ Branch 1 taken 3 times.
|
217 | if (pkt->stream_index == mp3->audio_stream_idx) { |
528 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 213 times.
|
214 | if (mp3->pics_to_write) { |
529 | /* buffer audio packets until we get all the pictures */ | ||
530 | 1 | int ret = avpriv_packet_list_put(&mp3->queue, pkt, NULL, 0); | |
531 | |||
532 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
533 | ✗ | av_log(s, AV_LOG_WARNING, "Not enough memory to buffer audio. Skipping picture streams\n"); | |
534 | ✗ | mp3->pics_to_write = 0; | |
535 | ✗ | mp3_queue_flush(s); | |
536 | ✗ | return mp3_write_audio_packet(s, pkt); | |
537 | } | ||
538 | } else | ||
539 | 213 | return mp3_write_audio_packet(s, pkt); | |
540 | } else { | ||
541 | int ret; | ||
542 | |||
543 | /* warn only once for each stream */ | ||
544 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (s->streams[pkt->stream_index]->nb_frames == 1) { |
545 | ✗ | av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d," | |
546 | " ignoring.\n", pkt->stream_index); | ||
547 | } | ||
548 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1) |
549 | ✗ | return 0; | |
550 | |||
551 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0) |
552 | ✗ | return ret; | |
553 | 3 | mp3->pics_to_write--; | |
554 | |||
555 | /* flush the buffered audio packets */ | ||
556 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
4 | if (!mp3->pics_to_write && |
557 | 1 | (ret = mp3_queue_flush(s)) < 0) | |
558 | ✗ | return ret; | |
559 | } | ||
560 | |||
561 | 4 | return 0; | |
562 | } | ||
563 | |||
564 | /** | ||
565 | * Write an ID3v2 header at beginning of stream | ||
566 | */ | ||
567 | |||
568 | 3 | static int mp3_init(struct AVFormatContext *s) | |
569 | { | ||
570 | 3 | MP3Context *mp3 = s->priv_data; | |
571 | int i; | ||
572 | |||
573 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (mp3->id3v2_version && |
574 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | mp3->id3v2_version != 3 && |
575 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | mp3->id3v2_version != 4) { |
576 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid ID3v2 version requested: %d. Only " | |
577 | "3, 4 or 0 (disabled) are allowed.\n", mp3->id3v2_version); | ||
578 | ✗ | return AVERROR(EINVAL); | |
579 | } | ||
580 | |||
581 | /* check the streams -- we want exactly one audio and arbitrary number of | ||
582 | * video (attached pictures) */ | ||
583 | 3 | mp3->audio_stream_idx = -1; | |
584 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | for (i = 0; i < s->nb_streams; i++) { |
585 | 6 | AVStream *st = s->streams[i]; | |
586 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
587 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (mp3->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_MP3) { |
588 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 " | |
589 | "audio stream is required.\n"); | ||
590 | ✗ | return AVERROR(EINVAL); | |
591 | } | ||
592 | 3 | mp3->audio_stream_idx = i; | |
593 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } else if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) { |
594 | ✗ | av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n"); | |
595 | ✗ | return AVERROR(EINVAL); | |
596 | } | ||
597 | } | ||
598 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (mp3->audio_stream_idx < 0) { |
599 | ✗ | av_log(s, AV_LOG_ERROR, "No audio stream present.\n"); | |
600 | ✗ | return AVERROR(EINVAL); | |
601 | } | ||
602 | 3 | mp3->pics_to_write = s->nb_streams - 1; | |
603 | |||
604 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
3 | if (mp3->pics_to_write && !mp3->id3v2_version) { |
605 | ✗ | av_log(s, AV_LOG_ERROR, "Attached pictures were requested, but the " | |
606 | "ID3v2 header is disabled.\n"); | ||
607 | ✗ | return AVERROR(EINVAL); | |
608 | } | ||
609 | |||
610 | 3 | return 0; | |
611 | } | ||
612 | |||
613 | 3 | static int mp3_write_header(struct AVFormatContext *s) | |
614 | { | ||
615 | 3 | MP3Context *mp3 = s->priv_data; | |
616 | int ret; | ||
617 | |||
618 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (mp3->id3v2_version) { |
619 | 3 | ff_id3v2_start(&mp3->id3, s->pb, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC); | |
620 | 3 | ret = ff_id3v2_write_metadata(s, &mp3->id3); | |
621 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
622 | ✗ | return ret; | |
623 | } | ||
624 | |||
625 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (!mp3->pics_to_write) { |
626 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (mp3->id3v2_version) |
627 | 2 | ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding); | |
628 | 2 | mp3_write_xing(s); | |
629 | } | ||
630 | |||
631 | 3 | return 0; | |
632 | } | ||
633 | |||
634 | 3 | static void mp3_deinit(struct AVFormatContext *s) | |
635 | { | ||
636 | 3 | MP3Context *mp3 = s->priv_data; | |
637 | |||
638 | 3 | avpriv_packet_list_free(&mp3->queue); | |
639 | 3 | av_freep(&mp3->xing_frame); | |
640 | 3 | } | |
641 | |||
642 | const FFOutputFormat ff_mp3_muxer = { | ||
643 | .p.name = "mp3", | ||
644 | .p.long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"), | ||
645 | .p.mime_type = "audio/mpeg", | ||
646 | .p.extensions = "mp3", | ||
647 | .priv_data_size = sizeof(MP3Context), | ||
648 | .p.audio_codec = AV_CODEC_ID_MP3, | ||
649 | .p.video_codec = AV_CODEC_ID_PNG, | ||
650 | .init = mp3_init, | ||
651 | .write_header = mp3_write_header, | ||
652 | .write_packet = mp3_write_packet, | ||
653 | .write_trailer = mp3_write_trailer, | ||
654 | .deinit = mp3_deinit, | ||
655 | .query_codec = query_codec, | ||
656 | .p.flags = AVFMT_NOTIMESTAMPS, | ||
657 | .p.priv_class = &mp3_muxer_class, | ||
658 | }; | ||
659 |