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