3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
23 #include "avio_internal.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"
39 static int id3v1_set_string(AVFormatContext
*s
, const char *key
,
40 uint8_t *buf
, int buf_size
)
42 AVDictionaryEntry
*tag
;
43 if ((tag
= av_dict_get(s
->metadata
, key
, NULL
, 0)))
44 av_strlcpy(buf
, tag
->value
, buf_size
);
48 static int id3v1_create_tag(AVFormatContext
*s
, uint8_t *buf
)
50 AVDictionaryEntry
*tag
;
53 memset(buf
, 0, ID3v1_TAG_SIZE
); /* fail safe */
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
65 buf
[126] = atoi(tag
->value
);
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
])) {
81 #define XING_NUM_BAGS 400
82 #define XING_TOC_SIZE 100
83 // size of the XING/LAME data, starting from the Xing tag
86 typedef struct MP3Context
{
94 // a buffer containing the whole XING/LAME frame
98 AVCRC audio_crc
; // CRC of the audio data
99 uint32_t audio_size
; // total size of the audio data
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
111 uint64_t bag
[XING_NUM_BAGS
];
113 int has_variable_bitrate
;
115 /* index of the audio stream */
116 int audio_stream_idx
;
117 /* number of attached pictures we still need to write */
120 /* audio packets are queued here until we get all the attached pictures */
121 AVPacketList
*queue
, *queue_end
;
124 static const uint8_t xing_offtbl
[2][2] = {{32, 17}, {17, 9}};
127 * Write an empty XING header and initialize respective data.
129 static int mp3_write_xing(AVFormatContext
*s
)
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
;
136 MPADecodeHeader mpah
;
137 int srate_idx
, i
, channels
;
139 int best_bitrate_idx
= -1;
140 int best_bitrate_error
= INT_MAX
;
145 if (!s
->pb
->seekable
|| !mp3
->write_xing
)
148 for (i
= 0; i
< FF_ARRAY_ELEMS(avpriv_mpa_freq_tab
); i
++) {
149 const uint16_t base_freq
= avpriv_mpa_freq_tab
[i
];
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
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");
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");
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;
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
);
182 if (error
< best_bitrate_error
) {
183 best_bitrate_error
= error
;
184 best_bitrate_idx
= bitrate_idx
;
187 av_assert0(best_bitrate_idx
>= 0);
189 for (bitrate_idx
= best_bitrate_idx
; ; bitrate_idx
++) {
190 int32_t mask
= bitrate_idx
<< (4 + 8);
191 if (15 == bitrate_idx
)
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
;
199 if (bytes_needed
<= mpah
.frame_size
)
205 ret
= avio_open_dyn_buf(&dyn_ctx
);
209 avio_wb32(dyn_ctx
, header
);
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
215 mp3
->size
= mpah
.frame_size
;
220 avio_wb32(dyn_ctx
, 0); // frames
221 avio_wb32(dyn_ctx
, 0); // size
224 for (i
= 0; i
< XING_TOC_SIZE
; i
++)
225 avio_w8(dyn_ctx
, (uint8_t)(255 * i
/ XING_TOC_SIZE
));
228 // we write it, because some (broken) tools always expect it to be present
229 avio_wb32(dyn_ctx
, 0);
231 // encoder short version string
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);
238 memcpy(encoder_str
, enc
->value
, FFMIN(strlen(enc
->value
), sizeof(encoder_str
)));
240 avio_write(dyn_ctx
, encoder_str
, sizeof(encoder_str
));
242 avio_write(dyn_ctx
, "Lavf\0\0\0\0\0", 9);
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
251 if (codec
->initial_padding
- 528 - 1 >= 1 << 12) {
252 av_log(s
, AV_LOG_WARNING
, "Too many samples of initial padding.\n");
254 avio_wb24(dyn_ctx
, FFMAX(codec
->initial_padding
- 528 - 1, 0)<<12);
256 avio_w8(dyn_ctx
, 0); // misc
257 avio_w8(dyn_ctx
, 0); // mp3gain
258 avio_wb16(dyn_ctx
, 0); // preset
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
265 ffio_fill(dyn_ctx
, 0, mpah
.frame_size
- bytes_needed
);
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
);
271 mp3
->audio_size
= mp3
->xing_frame_size
;
277 * Add a frame to XING data.
278 * Following lame's "VbrTag.c".
280 static void mp3_xing_add_frame(MP3Context
*mp3
, AVPacket
*pkt
)
286 mp3
->size
+= pkt
->size
;
288 if (mp3
->want
== mp3
->seen
) {
289 mp3
->bag
[mp3
->pos
] = mp3
->size
;
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
];
296 /* double wanted amount per bag. */
298 /* adjust current position to half of table size. */
299 mp3
->pos
= XING_NUM_BAGS
/ 2;
306 static int mp3_write_audio_packet(AVFormatContext
*s
, AVPacket
*pkt
)
308 MP3Context
*mp3
= s
->priv_data
;
310 if (pkt
->data
&& pkt
->size
>= 4) {
311 MPADecodeHeader mpah
;
315 h
= AV_RB32(pkt
->data
);
316 if (ff_mpa_check_header(h
) == 0) {
317 avpriv_mpegaudio_decode_header(&mpah
, h
);
318 if (!mp3
->initial_bitrate
)
319 mp3
->initial_bitrate
= mpah
.bit_rate
;
320 if ((mpah
.bit_rate
== 0) || (mp3
->initial_bitrate
!= mpah
.bit_rate
))
321 mp3
->has_variable_bitrate
= 1;
323 av_log(s
, AV_LOG_WARNING
, "Audio packet of size %d (starting with %08X...) "
324 "is invalid, writing it anyway.\n", pkt
->size
, h
);
327 #ifdef FILTER_VBR_HEADERS
328 /* filter out XING and INFO headers. */
329 base
= 4 + xing_offtbl
[mpah
.lsf
== 1][mpah
.nb_channels
== 1];
331 if (base
+ 4 <= pkt
->size
) {
332 uint32_t v
= AV_RB32(pkt
->data
+ base
);
334 if (MKBETAG('X','i','n','g') == v
|| MKBETAG('I','n','f','o') == v
)
338 /* filter out VBRI headers. */
341 if (base
+ 4 <= pkt
->size
&& MKBETAG('V','B','R','I') == AV_RB32(pkt
->data
+ base
))
345 if (mp3
->xing_offset
) {
346 mp3_xing_add_frame(mp3
, pkt
);
347 mp3
->audio_size
+= pkt
->size
;
348 mp3
->audio_crc
= av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE
),
349 mp3
->audio_crc
, pkt
->data
, pkt
->size
);
353 return ff_raw_write_packet(s
, pkt
);
356 static int mp3_queue_flush(AVFormatContext
*s
)
358 MP3Context
*mp3
= s
->priv_data
;
360 int ret
= 0, write
= 1;
362 ff_id3v2_finish(&mp3
->id3
, s
->pb
, s
->metadata_header_padding
);
365 while ((pktl
= mp3
->queue
)) {
366 if (write
&& (ret
= mp3_write_audio_packet(s
, &pktl
->pkt
)) < 0)
368 av_free_packet(&pktl
->pkt
);
369 mp3
->queue
= pktl
->next
;
372 mp3
->queue_end
= NULL
;
376 static void mp3_update_xing(AVFormatContext
*s
)
378 MP3Context
*mp3
= s
->priv_data
;
384 /* replace "Xing" identification string with "Info" for CBR files. */
385 if (!mp3
->has_variable_bitrate
)
386 AV_WL32(mp3
->xing_frame
+ mp3
->xing_offset
, MKTAG('I', 'n', 'f', 'o'));
388 AV_WB32(mp3
->xing_frame
+ mp3
->xing_offset
+ 8, mp3
->frames
);
389 AV_WB32(mp3
->xing_frame
+ mp3
->xing_offset
+ 12, mp3
->size
);
391 toc
= mp3
->xing_frame
+ mp3
->xing_offset
+ 16;
392 toc
[0] = 0; // first toc entry has to be zero.
393 for (i
= 1; i
< XING_TOC_SIZE
; ++i
) {
394 int j
= i
* mp3
->pos
/ XING_TOC_SIZE
;
395 int seek_point
= 256LL * mp3
->bag
[j
] / mp3
->size
;
396 toc
[i
] = FFMIN(seek_point
, 255);
399 /* write replaygain */
400 rg
= (AVReplayGain
*)av_stream_get_side_data(s
->streams
[0], AV_PKT_DATA_REPLAYGAIN
,
402 if (rg
&& rg_size
>= sizeof(*rg
)) {
405 AV_WB32(mp3
->xing_frame
+ mp3
->xing_offset
+ 131,
406 av_rescale(rg
->track_peak
, 1 << 23, 100000));
408 if (rg
->track_gain
!= INT32_MIN
) {
409 val
= FFABS(rg
->track_gain
/ 10000) & ((1 << 9) - 1);
410 val
|= (rg
->track_gain
< 0) << 9;
412 AV_WB16(mp3
->xing_frame
+ mp3
->xing_offset
+ 135, val
);
415 if (rg
->album_gain
!= INT32_MIN
) {
416 val
= FFABS(rg
->album_gain
/ 10000) & ((1 << 9) - 1);
417 val
|= (rg
->album_gain
< 0) << 9;
419 AV_WB16(mp3
->xing_frame
+ mp3
->xing_offset
+ 137, val
);
423 AV_WB32(mp3
->xing_frame
+ mp3
->xing_offset
+ XING_SIZE
- 8, mp3
->audio_size
);
424 AV_WB16(mp3
->xing_frame
+ mp3
->xing_offset
+ XING_SIZE
- 4, mp3
->audio_crc
);
426 tag_crc
= av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE
), 0, mp3
->xing_frame
, 190);
427 AV_WB16(mp3
->xing_frame
+ mp3
->xing_offset
+ XING_SIZE
- 2, tag_crc
);
429 avio_seek(s
->pb
, mp3
->xing_frame_offset
, SEEK_SET
);
430 avio_write(s
->pb
, mp3
->xing_frame
, mp3
->xing_frame_size
);
431 avio_seek(s
->pb
, 0, SEEK_END
);
434 static int mp3_write_trailer(struct AVFormatContext
*s
)
436 uint8_t buf
[ID3v1_TAG_SIZE
];
437 MP3Context
*mp3
= s
->priv_data
;
439 if (mp3
->pics_to_write
) {
440 av_log(s
, AV_LOG_WARNING
, "No packets were sent for some of the "
441 "attached pictures.\n");
445 /* write the id3v1 tag */
446 if (mp3
->write_id3v1
&& id3v1_create_tag(s
, buf
) > 0) {
447 avio_write(s
->pb
, buf
, ID3v1_TAG_SIZE
);
450 if (mp3
->xing_offset
)
453 av_freep(&mp3
->xing_frame
);
458 static int query_codec(enum AVCodecID id
, int std_compliance
)
460 const CodecMime
*cm
= ff_id3v2_mime_tags
;
461 while(cm
->id
!= AV_CODEC_ID_NONE
) {
463 return MKTAG('A', 'P', 'I', 'C');
470 AVOutputFormat ff_mp2_muxer
= {
472 .long_name
= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
473 .mime_type
= "audio/mpeg",
474 .extensions
= "mp2,m2a,mpa",
475 .audio_codec
= AV_CODEC_ID_MP2
,
476 .video_codec
= AV_CODEC_ID_NONE
,
477 .write_packet
= ff_raw_write_packet
,
478 .flags
= AVFMT_NOTIMESTAMPS
,
484 static const AVOption options
[] = {
485 { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
486 offsetof(MP3Context
, id3v2_version
), AV_OPT_TYPE_INT
, {.i64
= 4}, 0, 4, AV_OPT_FLAG_ENCODING_PARAM
},
487 { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
488 offsetof(MP3Context
, write_id3v1
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM
},
489 { "write_xing", "Write the Xing header containing file duration.",
490 offsetof(MP3Context
, write_xing
), AV_OPT_TYPE_INT
, {.i64
= 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM
},
494 static const AVClass mp3_muxer_class
= {
495 .class_name
= "MP3 muxer",
496 .item_name
= av_default_item_name
,
498 .version
= LIBAVUTIL_VERSION_INT
,
501 static int mp3_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
503 MP3Context
*mp3
= s
->priv_data
;
505 if (pkt
->stream_index
== mp3
->audio_stream_idx
) {
506 if (mp3
->pics_to_write
) {
507 /* buffer audio packets until we get all the pictures */
508 AVPacketList
*pktl
= av_mallocz(sizeof(*pktl
));
511 av_log(s
, AV_LOG_WARNING
, "Not enough memory to buffer audio. Skipping picture streams\n");
512 mp3
->pics_to_write
= 0;
514 return mp3_write_audio_packet(s
, pkt
);
517 ret
= av_copy_packet(&pktl
->pkt
, pkt
);
524 mp3
->queue_end
->next
= pktl
;
527 mp3
->queue_end
= pktl
;
529 return mp3_write_audio_packet(s
, pkt
);
533 /* warn only once for each stream */
534 if (s
->streams
[pkt
->stream_index
]->nb_frames
== 1) {
535 av_log(s
, AV_LOG_WARNING
, "Got more than one picture in stream %d,"
536 " ignoring.\n", pkt
->stream_index
);
538 if (!mp3
->pics_to_write
|| s
->streams
[pkt
->stream_index
]->nb_frames
>= 1)
541 if ((ret
= ff_id3v2_write_apic(s
, &mp3
->id3
, pkt
)) < 0)
543 mp3
->pics_to_write
--;
545 /* flush the buffered audio packets */
546 if (!mp3
->pics_to_write
&&
547 (ret
= mp3_queue_flush(s
)) < 0)
555 * Write an ID3v2 header at beginning of stream
558 static int mp3_write_header(struct AVFormatContext
*s
)
560 MP3Context
*mp3
= s
->priv_data
;
563 if (mp3
->id3v2_version
&&
564 mp3
->id3v2_version
!= 3 &&
565 mp3
->id3v2_version
!= 4) {
566 av_log(s
, AV_LOG_ERROR
, "Invalid ID3v2 version requested: %d. Only "
567 "3, 4 or 0 (disabled) are allowed.\n", mp3
->id3v2_version
);
568 return AVERROR(EINVAL
);
571 /* check the streams -- we want exactly one audio and arbitrary number of
572 * video (attached pictures) */
573 mp3
->audio_stream_idx
= -1;
574 for (i
= 0; i
< s
->nb_streams
; i
++) {
575 AVStream
*st
= s
->streams
[i
];
576 if (st
->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
577 if (mp3
->audio_stream_idx
>= 0 || st
->codec
->codec_id
!= AV_CODEC_ID_MP3
) {
578 av_log(s
, AV_LOG_ERROR
, "Invalid audio stream. Exactly one MP3 "
579 "audio stream is required.\n");
580 return AVERROR(EINVAL
);
582 mp3
->audio_stream_idx
= i
;
583 } else if (st
->codec
->codec_type
!= AVMEDIA_TYPE_VIDEO
) {
584 av_log(s
, AV_LOG_ERROR
, "Only audio streams and pictures are allowed in MP3.\n");
585 return AVERROR(EINVAL
);
588 if (mp3
->audio_stream_idx
< 0) {
589 av_log(s
, AV_LOG_ERROR
, "No audio stream present.\n");
590 return AVERROR(EINVAL
);
592 mp3
->pics_to_write
= s
->nb_streams
- 1;
594 if (mp3
->pics_to_write
&& !mp3
->id3v2_version
) {
595 av_log(s
, AV_LOG_ERROR
, "Attached pictures were requested, but the "
596 "ID3v2 header is disabled.\n");
597 return AVERROR(EINVAL
);
600 if (mp3
->id3v2_version
) {
601 ff_id3v2_start(&mp3
->id3
, s
->pb
, mp3
->id3v2_version
, ID3v2_DEFAULT_MAGIC
);
602 ret
= ff_id3v2_write_metadata(s
, &mp3
->id3
);
607 if (!mp3
->pics_to_write
) {
608 if (mp3
->id3v2_version
)
609 ff_id3v2_finish(&mp3
->id3
, s
->pb
, s
->metadata_header_padding
);
616 AVOutputFormat ff_mp3_muxer
= {
618 .long_name
= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
619 .mime_type
= "audio/mpeg",
621 .priv_data_size
= sizeof(MP3Context
),
622 .audio_codec
= AV_CODEC_ID_MP3
,
623 .video_codec
= AV_CODEC_ID_PNG
,
624 .write_header
= mp3_write_header
,
625 .write_packet
= mp3_write_packet
,
626 .write_trailer
= mp3_write_trailer
,
627 .query_codec
= query_codec
,
628 .flags
= AVFMT_NOTIMESTAMPS
,
629 .priv_class
= &mp3_muxer_class
,