summaryrefslogtreecommitdiff
path: root/libavformat/mp3enc.c
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2012-03-01 01:13:16 +0100
committerMichael Niedermayer <michaelni@gmx.at>2012-03-01 03:17:11 +0100
commit79ae084e9b930f8b53ae0499c6a06636d194574d (patch)
treee7d829e566b01ef7e84a12b06a2bcb87a8164059 /libavformat/mp3enc.c
parenta77c8ade2ee20fc6149e4c689a3f196f53e85273 (diff)
parent882abda5a26ffb8e3d1c5852dfa7cdad0a291d2d (diff)
downloadffmpeg-79ae084e9b930f8b53ae0499c6a06636d194574d.tar.gz
Merge remote-tracking branch 'qatar/master'
* qatar/master: (58 commits) amrnbdec: check frame size before decoding. cscd: use negative error values to indicate decode_init() failures. h264: prevent overreads in intra PCM decoding. FATE: do not decode audio in the nuv test. dxa: set audio stream time base using the sample rate psx-str: do not allow seeking by bytes asfdec: Do not set AVCodecContext.frame_size vqf: set packet parameters after av_new_packet() mpegaudiodec: use DSPUtil.butterflies_float(). FATE: add mp3 test for sample that exhibited false overreads fate: add cdxl test for bit line plane arrangement vmnc: return error on decode_init() failure. libvorbis: add/update error messages libvorbis: use AVFifoBuffer for output packet buffer libvorbis: remove unneeded e_o_s check libvorbis: check return values for functions that can return errors libvorbis: use float input instead of s16 libvorbis: do not flush libvorbis analysis if dsp state was not initialized libvorbis: use VBR by default, with default quality of 3 libvorbis: fix use of minrate/maxrate AVOptions ... Conflicts: Changelog doc/APIchanges libavcodec/avcodec.h libavcodec/dpxenc.c libavcodec/libvorbis.c libavcodec/vmnc.c libavformat/asfdec.c libavformat/id3v2enc.c libavformat/internal.h libavformat/mp3enc.c libavformat/utils.c libavformat/version.h libswscale/utils.c tests/fate/video.mak tests/ref/fate/nuv tests/ref/fate/prores-alpha tests/ref/lavf/ffm tests/ref/vsynth1/prores tests/ref/vsynth2/prores Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavformat/mp3enc.c')
-rw-r--r--libavformat/mp3enc.c257
1 files changed, 183 insertions, 74 deletions
diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c
index 3bbc3e84f9..b2c1b4211e 100644
--- a/libavformat/mp3enc.c
+++ b/libavformat/mp3enc.c
@@ -84,6 +84,7 @@ static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
typedef struct MP3Context {
const AVClass *class;
+ ID3v2EncContext id3;
int id3v2_version;
int write_id3v1;
int64_t frames_offset;
@@ -93,60 +94,15 @@ typedef struct MP3Context {
uint32_t seen;
uint32_t pos;
uint64_t bag[VBR_NUM_BAGS];
-} MP3Context;
-
-static int mp2_write_trailer(struct AVFormatContext *s)
-{
- uint8_t buf[ID3v1_TAG_SIZE];
- MP3Context *mp3 = s->priv_data;
-
- /* write the id3v1 tag */
- if (mp3 && mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
- avio_write(s->pb, buf, ID3v1_TAG_SIZE);
- }
- /* write number of frames */
- if (mp3 && mp3->frames_offset) {
- avio_seek(s->pb, mp3->frames_offset, SEEK_SET);
- avio_wb32(s->pb, s->streams[0]->nb_frames);
- avio_seek(s->pb, 0, SEEK_END);
- }
-
- avio_flush(s->pb);
-
- return 0;
-}
-
-#if CONFIG_MP2_MUXER
-AVOutputFormat ff_mp2_muxer = {
- .name = "mp2",
- .long_name = NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
- .mime_type = "audio/x-mpeg",
- .extensions = "mp2,m2a",
- .audio_codec = CODEC_ID_MP2,
- .video_codec = CODEC_ID_NONE,
- .write_packet = ff_raw_write_packet,
- .write_trailer = mp2_write_trailer,
- .flags = AVFMT_NOTIMESTAMPS,
-};
-#endif
-
-#if CONFIG_MP3_MUXER
-
-static const AVOption options[] = {
- { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
- offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.dbl = 4}, 3, 4, AV_OPT_FLAG_ENCODING_PARAM},
- { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
- offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
- { NULL },
-};
+ /* index of the audio stream */
+ int audio_stream_idx;
+ /* number of attached pictures we still need to write */
+ int pics_to_write;
-static const AVClass mp3_muxer_class = {
- .class_name = "MP3 muxer",
- .item_name = av_default_item_name,
- .option = options,
- .version = LIBAVUTIL_VERSION_INT,
-};
+ /* audio packets are queued here until we get all the attached pictures */
+ AVPacketList *queue, *queue_end;
+} MP3Context;
static const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
@@ -155,8 +111,8 @@ static const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
*/
static int mp3_write_xing(AVFormatContext *s)
{
- AVCodecContext *codec = s->streams[0]->codec;
MP3Context *mp3 = s->priv_data;
+ AVCodecContext *codec = s->streams[mp3->audio_stream_idx]->codec;
int bitrate_idx;
int best_bitrate_idx = -1;
int best_bitrate_error= INT_MAX;
@@ -166,6 +122,9 @@ static int mp3_write_xing(AVFormatContext *s)
int srate_idx, i, channels;
int needed;
+ if (!s->pb->seekable)
+ return 0;
+
for (i = 0; i < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); i++)
if (avpriv_mpa_freq_tab[i] == codec->sample_rate) {
srate_idx = i;
@@ -295,26 +254,7 @@ static void mp3_fix_xing(AVFormatContext *s)
avio_seek(s->pb, 0, SEEK_END);
}
-/**
- * Write an ID3v2 header at beginning of stream
- */
-
-static int mp3_write_header(struct AVFormatContext *s)
-{
- MP3Context *mp3 = s->priv_data;
- int ret;
-
- ret = ff_id3v2_write(s, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC);
- if (ret < 0)
- return ret;
-
- if (s->pb->seekable)
- mp3_write_xing(s);
-
- return 0;
-}
-
-static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
+static int mp3_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
{
if (! pkt || ! pkt->data || pkt->size < 4)
return ff_raw_write_packet(s, pkt);
@@ -350,6 +290,175 @@ static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
}
}
+static int mp3_queue_flush(AVFormatContext *s)
+{
+ MP3Context *mp3 = s->priv_data;
+ AVPacketList *pktl;
+ int ret = 0, write = 1;
+
+ ff_id3v2_finish(&mp3->id3, s->pb);
+ mp3_write_xing(s);
+
+ while ((pktl = mp3->queue)) {
+ if (write && (ret = mp3_write_packet_internal(s, &pktl->pkt)) < 0)
+ write = 0;
+ av_free_packet(&pktl->pkt);
+ mp3->queue = pktl->next;
+ av_freep(&pktl);
+ }
+ mp3->queue_end = NULL;
+ return ret;
+}
+
+static int mp2_write_trailer(struct AVFormatContext *s)
+{
+ uint8_t buf[ID3v1_TAG_SIZE];
+ MP3Context *mp3 = s->priv_data;
+
+ if (mp3 && mp3->pics_to_write) {
+ av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
+ "attached pictures.\n");
+ mp3_queue_flush(s);
+ }
+
+ /* write the id3v1 tag */
+ if (mp3 && mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
+ avio_write(s->pb, buf, ID3v1_TAG_SIZE);
+ }
+
+ /* write number of frames */
+ if (mp3 && mp3->frames_offset) {
+ avio_seek(s->pb, mp3->frames_offset, SEEK_SET);
+ avio_wb32(s->pb, s->streams[mp3->audio_stream_idx]->nb_frames);
+ avio_seek(s->pb, 0, SEEK_END);
+ }
+
+ avio_flush(s->pb);
+
+ return 0;
+}
+
+#if CONFIG_MP2_MUXER
+AVOutputFormat ff_mp2_muxer = {
+ .name = "mp2",
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
+ .mime_type = "audio/x-mpeg",
+ .extensions = "mp2,m2a",
+ .audio_codec = CODEC_ID_MP2,
+ .video_codec = CODEC_ID_NONE,
+ .write_packet = ff_raw_write_packet,
+ .write_trailer = mp2_write_trailer,
+ .flags = AVFMT_NOTIMESTAMPS,
+};
+#endif
+
+#if CONFIG_MP3_MUXER
+
+static const AVOption options[] = {
+ { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
+ offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.dbl = 4}, 3, 4, AV_OPT_FLAG_ENCODING_PARAM},
+ { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
+ offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
+ { NULL },
+};
+
+static const AVClass mp3_muxer_class = {
+ .class_name = "MP3 muxer",
+ .item_name = av_default_item_name,
+ .option = options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
+static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ MP3Context *mp3 = s->priv_data;
+
+ if (pkt->stream_index == mp3->audio_stream_idx) {
+ if (mp3->pics_to_write) {
+ /* buffer audio packets until we get all the pictures */
+ AVPacketList *pktl = av_mallocz(sizeof(*pktl));
+ if (!pktl)
+ return AVERROR(ENOMEM);
+
+ pktl->pkt = *pkt;
+ pkt->destruct = NULL;
+
+ if (mp3->queue_end)
+ mp3->queue_end->next = pktl;
+ else
+ mp3->queue = pktl;
+ mp3->queue_end = pktl;
+ } else
+ return mp3_write_packet_internal(s, pkt);
+ } else {
+ int ret;
+
+ /* warn only once for each stream */
+ if (s->streams[pkt->stream_index]->nb_frames == 1) {
+ av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
+ " ignoring.\n", pkt->stream_index);
+ }
+ if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
+ return 0;
+
+ if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
+ return ret;
+ mp3->pics_to_write--;
+
+ /* flush the buffered audio packets */
+ if (!mp3->pics_to_write &&
+ (ret = mp3_queue_flush(s)) < 0)
+ return ret;
+ }
+
+ return 0;
+}
+
+/**
+ * Write an ID3v2 header at beginning of stream
+ */
+
+static int mp3_write_header(struct AVFormatContext *s)
+{
+ MP3Context *mp3 = s->priv_data;
+ int ret, i;
+
+ /* check the streams -- we want exactly one audio and arbitrary number of
+ * video (attached pictures) */
+ mp3->audio_stream_idx = -1;
+ for (i = 0; i < s->nb_streams; i++) {
+ AVStream *st = s->streams[i];
+ if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
+ if (mp3->audio_stream_idx >= 0 || st->codec->codec_id != CODEC_ID_MP3) {
+ av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
+ "audio stream is required.\n");
+ return AVERROR(EINVAL);
+ }
+ mp3->audio_stream_idx = i;
+ } else if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO) {
+ av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
+ return AVERROR(EINVAL);
+ }
+ }
+ if (mp3->audio_stream_idx < 0) {
+ av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
+ return AVERROR(EINVAL);
+ }
+ mp3->pics_to_write = s->nb_streams - 1;
+
+ ff_id3v2_start(&mp3->id3, s->pb, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC);
+ ret = ff_id3v2_write_metadata(s, &mp3->id3);
+ if (ret < 0)
+ return ret;
+
+ if (!mp3->pics_to_write) {
+ ff_id3v2_finish(&mp3->id3, s->pb);
+ mp3_write_xing(s);
+ }
+
+ return 0;
+}
+
static int mp3_write_trailer(AVFormatContext *s)
{
MP3Context *mp3 = s->priv_data;
@@ -371,7 +480,7 @@ AVOutputFormat ff_mp3_muxer = {
.extensions = "mp3",
.priv_data_size = sizeof(MP3Context),
.audio_codec = CODEC_ID_MP3,
- .video_codec = CODEC_ID_NONE,
+ .video_codec = CODEC_ID_PNG,
.write_header = mp3_write_header,
.write_packet = mp3_write_packet,
.write_trailer = mp3_write_trailer,