FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mp3enc.c
Date: 2026-04-27 02:31:40
Exec Total Coverage
Lines: 210 301 69.8%
Functions: 11 13 84.6%
Branches: 93 175 53.1%

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