FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mp3enc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 206 301 68.4%
Functions: 11 13 84.6%
Branches: 88 175 50.3%

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 3 static int mp3_write_xing(AVFormatContext *s)
146 {
147 3 MP3Context *mp3 = s->priv_data;
148 3 AVCodecParameters *par = s->streams[mp3->audio_stream_idx]->codecpar;
149 3 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 3 int best_bitrate_idx = -1;
156 3 int best_bitrate_error = INT_MAX;
157 int ret;
158 3 int ver = 0;
159 int bytes_needed;
160
161
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)
162 return 0;
163
164
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpa_freq_tab); i++) {
165 6 const uint16_t base_freq = ff_mpa_freq_tab[i];
166
167
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 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 3 srate_idx = i;
173 3 break;
174 }
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 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 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
3 switch (par->ch_layout.nb_channels) {
181 2 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 3 header = 0xffU << 24; // sync
190 3 header |= (0x7 << 5 | ver << 3 | 0x1 << 1 | 0x1) << 16; // sync/audio-version/layer 3/no crc*/
191 3 header |= (srate_idx << 2) << 8;
192 3 header |= channels << 6;
193
194
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 3 times.
45 for (bitrate_idx = 1; bitrate_idx < 15; bitrate_idx++) {
195 42 int bit_rate = 1000 * ff_mpa_bitrate_tab[ver != 3][3 - 1][bitrate_idx];
196 42 int error = FFABS(bit_rate - par->bit_rate);
197
198
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 25 times.
42 if (error < best_bitrate_error) {
199 17 best_bitrate_error = error;
200 17 best_bitrate_idx = bitrate_idx;
201 }
202 }
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(best_bitrate_idx >= 0);
204
205 3 for (bitrate_idx = best_bitrate_idx; ; bitrate_idx++) {
206 3 int32_t mask = bitrate_idx << (4 + 8);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (15 == bitrate_idx)
208 return -1;
209 3 header |= mask;
210
211 3 ret = avpriv_mpegaudio_decode_header(&mpah, header);
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(ret >= 0);
213 3 mp3->xing_offset = xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1] + 4;
214 3 bytes_needed = mp3->xing_offset + XING_SIZE;
215
216
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (bytes_needed <= mpah.frame_size)
217 3 break;
218
219 header &= ~mask;
220 }
221
222 3 ret = avio_open_dyn_buf(&dyn_ctx);
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
224 return ret;
225
226 3 avio_wb32(dyn_ctx, header);
227
228 3 ffio_fill(dyn_ctx, 0, mp3->xing_offset - 4);
229 3 ffio_wfourcc(dyn_ctx, "Xing");
230 3 avio_wb32(dyn_ctx, 0x01 | 0x02 | 0x04 | 0x08); // frames / size / TOC / vbr scale
231
232 3 mp3->size = mpah.frame_size;
233 3 mp3->want=1;
234 3 mp3->seen=0;
235 3 mp3->pos=0;
236
237 3 avio_wb32(dyn_ctx, 0); // frames
238 3 avio_wb32(dyn_ctx, 0); // size
239
240 // TOC
241
2/2
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 3 times.
303 for (i = 0; i < XING_TOC_SIZE; i++)
242 300 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 3 avio_wb32(dyn_ctx, 0);
247
248 // encoder short version string
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (enc) {
250 uint8_t encoder_str[9] = { 0 };
251 if ( strlen(enc->value) > sizeof(encoder_str)
252 && !strcmp("Lavc libmp3lame", enc->value)) {
253 memcpy(encoder_str, "Lavf lame", 9);
254 } else
255 memcpy(encoder_str, enc->value, FFMIN(strlen(enc->value), sizeof(encoder_str)));
256
257 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 3 avio_w8(dyn_ctx, 0); // tag revision 0 / unknown vbr method
262 3 avio_w8(dyn_ctx, 0); // unknown lowpass filter value
263 3 ffio_fill(dyn_ctx, 0, 8); // empty replaygain fields
264 3 avio_w8(dyn_ctx, 0); // unknown encoding flags
265 3 avio_w8(dyn_ctx, 0); // unknown abr/minimal bitrate
266 3 avio_wb24(dyn_ctx, 0); // empty encoder delay/padding
267
268 3 avio_w8(dyn_ctx, 0); // misc
269 3 avio_w8(dyn_ctx, 0); // mp3gain
270 3 avio_wb16(dyn_ctx, 0); // preset
271
272 // audio length and CRCs (will be updated later)
273 3 avio_wb32(dyn_ctx, 0); // music length
274 3 avio_wb16(dyn_ctx, 0); // music crc
275 3 avio_wb16(dyn_ctx, 0); // tag crc
276
277 3 ffio_fill(dyn_ctx, 0, mpah.frame_size - bytes_needed);
278
279 3 mp3->xing_frame_size = avio_close_dyn_buf(dyn_ctx, &mp3->xing_frame);
280 3 mp3->xing_frame_offset = avio_tell(s->pb);
281 3 avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
282
283 3 mp3->audio_size = mp3->xing_frame_size;
284
285 3 return 0;
286 }
287
288 /*
289 * Add a frame to XING data.
290 * Following lame's "VbrTag.c".
291 */
292 214 static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt)
293 {
294 int i;
295
296 214 mp3->frames++;
297 214 mp3->seen++;
298 214 mp3->size += pkt->size;
299
300
1/2
✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
214 if (mp3->want == mp3->seen) {
301 214 mp3->bag[mp3->pos] = mp3->size;
302
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 214 times.
214 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 214 mp3->seen = 0;
315 }
316 214 }
317
318 214 static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt)
319 {
320 214 MP3Context *mp3 = s->priv_data;
321
322
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) {
323 MPADecodeHeader mpah;
324 int ret;
325 int av_unused base;
326 uint32_t h;
327
328 214 h = AV_RB32(pkt->data);
329 214 ret = avpriv_mpegaudio_decode_header(&mpah, h);
330
1/2
✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
214 if (ret >= 0) {
331
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 211 times.
214 if (!mp3->initial_bitrate)
332 3 mp3->initial_bitrate = mpah.bit_rate;
333
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))
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 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
1/2
✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
214 if (mp3->xing_offset) {
359 214 uint8_t *side_data = NULL;
360 size_t side_data_size;
361
362 214 mp3_xing_add_frame(mp3, pkt);
363 214 mp3->audio_size += pkt->size;
364 428 mp3->audio_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE),
365 214 mp3->audio_crc, pkt->data, pkt->size);
366
367 214 side_data = av_packet_get_side_data(pkt,
368 AV_PKT_DATA_SKIP_SAMPLES,
369 &side_data_size);
370
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) {
371
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 mp3->padding = FFMAX(AV_RL32(side_data + 4) + 528 + 1, 0);
372
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!mp3->delay)
373
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 mp3->delay = FFMAX(AV_RL32(side_data) - 528 - 1, 0);
374 } else {
375 213 mp3->padding = 0;
376 }
377 }
378 }
379
380 214 return ff_raw_write_packet(s, pkt);
381 }
382
383 1 static int mp3_queue_flush(AVFormatContext *s)
384 {
385 1 MP3Context *mp3 = s->priv_data;
386 1 AVPacket *const pkt = ffformatcontext(s)->pkt;
387 1 int ret = 0, write = 1;
388
389 1 ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
390 1 mp3_write_xing(s);
391
392
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 while (mp3->queue.head) {
393 1 avpriv_packet_list_get(&mp3->queue, pkt);
394
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)
395 write = 0;
396 1 av_packet_unref(pkt);
397 }
398 1 return ret;
399 }
400
401 3 static void mp3_update_xing(AVFormatContext *s)
402 {
403 3 MP3Context *mp3 = s->priv_data;
404 const AVPacketSideData *sd;
405 AVReplayGain *rg;
406 uint16_t tag_crc;
407 uint8_t *toc;
408 int i;
409 3 int64_t old_pos = avio_tell(s->pb);
410
411 /* replace "Xing" identification string with "Info" for CBR files. */
412
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (!mp3->has_variable_bitrate)
413 2 AV_WL32(mp3->xing_frame + mp3->xing_offset, MKTAG('I', 'n', 'f', 'o'));
414
415 3 AV_WB32(mp3->xing_frame + mp3->xing_offset + 8, mp3->frames);
416 3 AV_WB32(mp3->xing_frame + mp3->xing_offset + 12, mp3->size);
417
418 3 toc = mp3->xing_frame + mp3->xing_offset + 16;
419 3 toc[0] = 0; // first toc entry has to be zero.
420
2/2
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 3 times.
300 for (i = 1; i < XING_TOC_SIZE; ++i) {
421 297 int j = i * mp3->pos / XING_TOC_SIZE;
422 297 int seek_point = 256LL * mp3->bag[j] / mp3->size;
423 297 toc[i] = FFMIN(seek_point, 255);
424 }
425
426 /* write replaygain */
427 3 sd = av_packet_side_data_get(s->streams[0]->codecpar->coded_side_data,
428 3 s->streams[0]->codecpar->nb_coded_side_data,
429 AV_PKT_DATA_REPLAYGAIN);
430
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)) {
431 uint16_t val;
432
433 rg = (AVReplayGain *)sd->data;
434 AV_WB32(mp3->xing_frame + mp3->xing_offset + 131,
435 av_rescale(rg->track_peak, 1 << 23, 100000));
436
437 if (rg->track_gain != INT32_MIN) {
438 val = FFABS(rg->track_gain / 10000) & ((1 << 9) - 1);
439 val |= (rg->track_gain < 0) << 9;
440 val |= 1 << 13;
441 AV_WB16(mp3->xing_frame + mp3->xing_offset + 135, val);
442 }
443
444 if (rg->album_gain != INT32_MIN) {
445 val = FFABS(rg->album_gain / 10000) & ((1 << 9) - 1);
446 val |= (rg->album_gain < 0) << 9;
447 val |= 1 << 14;
448 AV_WB16(mp3->xing_frame + mp3->xing_offset + 137, val);
449 }
450 }
451
452 /* write encoder delay/padding */
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (mp3->delay >= 1 << 12) {
454 mp3->delay = (1 << 12) - 1;
455 av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n");
456 }
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (mp3->padding >= 1 << 12) {
458 mp3->padding = (1 << 12) - 1;
459 av_log(s, AV_LOG_WARNING, "Too many samples of trailing padding.\n");
460 }
461 3 AV_WB24(mp3->xing_frame + mp3->xing_offset + 141, (mp3->delay << 12) + mp3->padding);
462
463 3 AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size);
464 3 AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc);
465
466 3 tag_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), 0, mp3->xing_frame, 190);
467 3 AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 2, tag_crc);
468
469 3 avio_seek(s->pb, mp3->xing_frame_offset, SEEK_SET);
470 3 avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
471 3 avio_seek(s->pb, old_pos, SEEK_SET);
472 3 }
473
474 3 static int mp3_write_trailer(struct AVFormatContext *s)
475 {
476 uint8_t buf[ID3v1_TAG_SIZE];
477 3 MP3Context *mp3 = s->priv_data;
478
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (mp3->pics_to_write) {
480 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
481 "attached pictures.\n");
482 mp3_queue_flush(s);
483 }
484
485 /* write the id3v1 tag */
486
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) {
487 avio_write(s->pb, buf, ID3v1_TAG_SIZE);
488 }
489
490
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (mp3->xing_offset)
491 3 mp3_update_xing(s);
492
493 3 return 0;
494 }
495
496 2 static int query_codec(enum AVCodecID id, int std_compliance)
497 {
498 2 const CodecMime *cm= ff_id3v2_mime_tags;
499
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (id == AV_CODEC_ID_MP3)
501 return 1;
502
503
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 while(cm->id != AV_CODEC_ID_NONE) {
504
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if(id == cm->id)
505 2 return MKTAG('A', 'P', 'I', 'C');
506 6 cm++;
507 }
508 return 0;
509 }
510
511 static const AVOption options[] = {
512 { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
513 offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 0, 4, AV_OPT_FLAG_ENCODING_PARAM},
514 { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
515 offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
516 { "write_xing", "Write the Xing header containing file duration.",
517 offsetof(MP3Context, write_xing), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
518 { NULL },
519 };
520
521 static const AVClass mp3_muxer_class = {
522 .class_name = "MP3 muxer",
523 .item_name = av_default_item_name,
524 .option = options,
525 .version = LIBAVUTIL_VERSION_INT,
526 };
527
528 217 static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
529 {
530 217 MP3Context *mp3 = s->priv_data;
531
532
2/2
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 3 times.
217 if (pkt->stream_index == mp3->audio_stream_idx) {
533
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 213 times.
214 if (mp3->pics_to_write) {
534 /* buffer audio packets until we get all the pictures */
535 1 int ret = avpriv_packet_list_put(&mp3->queue, pkt, NULL, 0);
536
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
538 av_log(s, AV_LOG_WARNING, "Not enough memory to buffer audio. Skipping picture streams\n");
539 mp3->pics_to_write = 0;
540 mp3_queue_flush(s);
541 return mp3_write_audio_packet(s, pkt);
542 }
543 } else
544 213 return mp3_write_audio_packet(s, pkt);
545 } else {
546 int ret;
547
548 /* warn only once for each stream */
549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->streams[pkt->stream_index]->nb_frames == 1) {
550 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
551 " ignoring.\n", pkt->stream_index);
552 }
553
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)
554 return 0;
555
556
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
557 return ret;
558 3 mp3->pics_to_write--;
559
560 /* flush the buffered audio packets */
561
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 &&
562 1 (ret = mp3_queue_flush(s)) < 0)
563 return ret;
564 }
565
566 4 return 0;
567 }
568
569 /**
570 * Write an ID3v2 header at beginning of stream
571 */
572
573 3 static int mp3_init(struct AVFormatContext *s)
574 {
575 3 MP3Context *mp3 = s->priv_data;
576 int i;
577
578
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (mp3->id3v2_version &&
579
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 mp3->id3v2_version != 3 &&
580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 mp3->id3v2_version != 4) {
581 av_log(s, AV_LOG_ERROR, "Invalid ID3v2 version requested: %d. Only "
582 "3, 4 or 0 (disabled) are allowed.\n", mp3->id3v2_version);
583 return AVERROR(EINVAL);
584 }
585
586 /* check the streams -- we want exactly one audio and arbitrary number of
587 * video (attached pictures) */
588 3 mp3->audio_stream_idx = -1;
589
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (i = 0; i < s->nb_streams; i++) {
590 6 AVStream *st = s->streams[i];
591
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
592
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) {
593 av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
594 "audio stream is required.\n");
595 return AVERROR(EINVAL);
596 }
597 3 mp3->audio_stream_idx = i;
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
599 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
600 return AVERROR(EINVAL);
601 }
602 }
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (mp3->audio_stream_idx < 0) {
604 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
605 return AVERROR(EINVAL);
606 }
607 3 mp3->pics_to_write = s->nb_streams - 1;
608
609
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) {
610 av_log(s, AV_LOG_ERROR, "Attached pictures were requested, but the "
611 "ID3v2 header is disabled.\n");
612 return AVERROR(EINVAL);
613 }
614
615 3 return 0;
616 }
617
618 3 static int mp3_write_header(struct AVFormatContext *s)
619 {
620 3 MP3Context *mp3 = s->priv_data;
621 int ret;
622
623
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (mp3->id3v2_version) {
624 3 ff_id3v2_start(&mp3->id3, s->pb, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC);
625 3 ret = ff_id3v2_write_metadata(s, &mp3->id3);
626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
627 return ret;
628 }
629
630
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (!mp3->pics_to_write) {
631
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (mp3->id3v2_version)
632 2 ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
633 2 mp3_write_xing(s);
634 }
635
636 3 return 0;
637 }
638
639 3 static void mp3_deinit(struct AVFormatContext *s)
640 {
641 3 MP3Context *mp3 = s->priv_data;
642
643 3 avpriv_packet_list_free(&mp3->queue);
644 3 av_freep(&mp3->xing_frame);
645 3 }
646
647 const FFOutputFormat ff_mp3_muxer = {
648 .p.name = "mp3",
649 .p.long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
650 .p.mime_type = "audio/mpeg",
651 .p.extensions = "mp3",
652 .priv_data_size = sizeof(MP3Context),
653 .p.audio_codec = AV_CODEC_ID_MP3,
654 .p.video_codec = AV_CODEC_ID_PNG,
655 .init = mp3_init,
656 .write_header = mp3_write_header,
657 .write_packet = mp3_write_packet,
658 .write_trailer = mp3_write_trailer,
659 .deinit = mp3_deinit,
660 .query_codec = query_codec,
661 .p.flags = AVFMT_NOTIMESTAMPS,
662 .p.priv_class = &mp3_muxer_class,
663 };
664