FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mp3enc.c
Date: 2026-09-10 12:58:17
Exec Total Coverage
Lines: 244 336 72.6%
Functions: 12 14 85.7%
Branches: 120 201 59.7%

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