00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "avio_internal.h"
00024 #include "id3v1.h"
00025 #include "id3v2.h"
00026 #include "rawenc.h"
00027 #include "libavutil/avstring.h"
00028 #include "libavcodec/mpegaudio.h"
00029 #include "libavcodec/mpegaudiodata.h"
00030 #include "libavcodec/mpegaudiodecheader.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "libavutil/opt.h"
00033 #include "libavcodec/mpegaudio.h"
00034 #include "libavcodec/mpegaudiodata.h"
00035 #include "libavcodec/mpegaudiodecheader.h"
00036 #include "libavformat/avio_internal.h"
00037 #include "libavutil/dict.h"
00038 #include "libavutil/avassert.h"
00039
00040 static int id3v1_set_string(AVFormatContext *s, const char *key,
00041 uint8_t *buf, int buf_size)
00042 {
00043 AVDictionaryEntry *tag;
00044 if ((tag = av_dict_get(s->metadata, key, NULL, 0)))
00045 av_strlcpy(buf, tag->value, buf_size);
00046 return !!tag;
00047 }
00048
00049 static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
00050 {
00051 AVDictionaryEntry *tag;
00052 int i, count = 0;
00053
00054 memset(buf, 0, ID3v1_TAG_SIZE);
00055 buf[0] = 'T';
00056 buf[1] = 'A';
00057 buf[2] = 'G';
00058
00059 count += id3v1_set_string(s, "TIT2", buf + 3, 30 + 1);
00060 count += id3v1_set_string(s, "TPE1", buf + 33, 30 + 1);
00061 count += id3v1_set_string(s, "TALB", buf + 63, 30 + 1);
00062 count += id3v1_set_string(s, "TDRL", buf + 93, 4 + 1);
00063 count += id3v1_set_string(s, "comment", buf + 97, 30 + 1);
00064 if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) {
00065 buf[125] = 0;
00066 buf[126] = atoi(tag->value);
00067 count++;
00068 }
00069 buf[127] = 0xFF;
00070 if ((tag = av_dict_get(s->metadata, "TCON", NULL, 0))) {
00071 for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
00072 if (!av_strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
00073 buf[127] = i;
00074 count++;
00075 break;
00076 }
00077 }
00078 }
00079 return count;
00080 }
00081
00082 #define VBR_NUM_BAGS 400
00083 #define VBR_TOC_SIZE 100
00084
00085 typedef struct MP3Context {
00086 const AVClass *class;
00087 ID3v2EncContext id3;
00088 int id3v2_version;
00089 int write_id3v1;
00090 int64_t frames_offset;
00091 int32_t frames;
00092 int32_t size;
00093 uint32_t want;
00094 uint32_t seen;
00095 uint32_t pos;
00096 uint64_t bag[VBR_NUM_BAGS];
00097 int initial_bitrate;
00098 int has_variable_bitrate;
00099
00100
00101 int audio_stream_idx;
00102
00103 int pics_to_write;
00104
00105
00106 AVPacketList *queue, *queue_end;
00107 } MP3Context;
00108
00109 static const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
00110
00111
00112
00113
00114 static int mp3_write_xing(AVFormatContext *s)
00115 {
00116 MP3Context *mp3 = s->priv_data;
00117 AVCodecContext *codec = s->streams[mp3->audio_stream_idx]->codec;
00118 int bitrate_idx;
00119 int best_bitrate_idx = -1;
00120 int best_bitrate_error= INT_MAX;
00121 int64_t xing_offset;
00122 int32_t header, mask;
00123 MPADecodeHeader c;
00124 int srate_idx, ver = 0, i, channels;
00125 int needed;
00126
00127 if (!s->pb->seekable)
00128 return 0;
00129
00130 for (i = 0; i < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); i++) {
00131 const uint16_t base_freq = avpriv_mpa_freq_tab[i];
00132 if (codec->sample_rate == base_freq) ver = 0x3;
00133 else if (codec->sample_rate == base_freq / 2) ver = 0x2;
00134 else if (codec->sample_rate == base_freq / 4) ver = 0x0;
00135 else continue;
00136 srate_idx = i;
00137 break;
00138 }
00139 if (i == FF_ARRAY_ELEMS(avpriv_mpa_freq_tab)) {
00140 av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n");
00141 return -1;
00142 }
00143
00144 switch (codec->channels) {
00145 case 1: channels = MPA_MONO; break;
00146 case 2: channels = MPA_STEREO; break;
00147 default: av_log(s, AV_LOG_WARNING, "Unsupported number of channels, not writing Xing header.\n"); return -1;
00148 }
00149
00150
00151 header = 0xff << 24;
00152 header |= (0x7 << 5 | ver << 3 | 0x1 << 1 | 0x1) << 16;
00153 header |= (srate_idx << 2) << 8;
00154 header |= channels << 6;
00155
00156 for (bitrate_idx=1; bitrate_idx<15; bitrate_idx++) {
00157 int error;
00158 avpriv_mpegaudio_decode_header(&c, header | (bitrate_idx << (4+8)));
00159 error= FFABS(c.bit_rate - codec->bit_rate);
00160 if(error < best_bitrate_error){
00161 best_bitrate_error= error;
00162 best_bitrate_idx = bitrate_idx;
00163 }
00164 }
00165 av_assert0(best_bitrate_idx >= 0);
00166
00167 for (bitrate_idx= best_bitrate_idx;; bitrate_idx++) {
00168 if (15 == bitrate_idx)
00169 return -1;
00170 mask = bitrate_idx << (4+8);
00171 header |= mask;
00172 avpriv_mpegaudio_decode_header(&c, header);
00173 xing_offset=xing_offtbl[c.lsf == 1][c.nb_channels == 1];
00174 needed = 4
00175 + xing_offset
00176 + 4
00177 + 4
00178 + 4
00179 + 4
00180 + VBR_TOC_SIZE;
00181
00182 if (needed <= c.frame_size)
00183 break;
00184 header &= ~mask;
00185 }
00186
00187 avio_wb32(s->pb, header);
00188 ffio_fill(s->pb, 0, xing_offset);
00189 avio_wb32(s->pb, MKBETAG('X', 'i', 'n', 'g'));
00190 avio_wb32(s->pb, 0x01 | 0x02 | 0x04);
00191
00192 mp3->frames_offset = avio_tell(s->pb);
00193 mp3->size = c.frame_size;
00194 mp3->want=1;
00195 mp3->seen=0;
00196 mp3->pos=0;
00197
00198 avio_wb32(s->pb, 0);
00199 avio_wb32(s->pb, 0);
00200
00201
00202 for (i = 0; i < VBR_TOC_SIZE; ++i)
00203 avio_w8(s->pb, (uint8_t)(255 * i / VBR_TOC_SIZE));
00204
00205 ffio_fill(s->pb, 0, c.frame_size - needed);
00206 avio_flush(s->pb);
00207
00208 return 0;
00209 }
00210
00211
00212
00213
00214
00215 static void mp3_xing_add_frame(AVFormatContext *s, AVPacket *pkt)
00216 {
00217 MP3Context *mp3 = s->priv_data;
00218 int i;
00219
00220 ++mp3->frames;
00221 mp3->size += pkt->size;
00222
00223 if (mp3->want == ++mp3->seen) {
00224 mp3->bag[mp3->pos] = mp3->size;
00225
00226 if (VBR_NUM_BAGS == ++mp3->pos) {
00227
00228 for (i = 1; i < VBR_NUM_BAGS; i += 2)
00229 mp3->bag[i >> 1] = mp3->bag[i];
00230
00231
00232 mp3->want <<= 1;
00233
00234 mp3->pos >>= 1;
00235 }
00236
00237 mp3->seen = 0;
00238 }
00239 }
00240
00241 static void mp3_fix_xing(AVFormatContext *s)
00242 {
00243 MP3Context *mp3 = s->priv_data;
00244 int i;
00245
00246 avio_flush(s->pb);
00247
00248
00249 if (!mp3->has_variable_bitrate) {
00250 int64_t tag_offset = mp3->frames_offset
00251 - 4
00252 - 4;
00253 avio_seek(s->pb, tag_offset, SEEK_SET);
00254 avio_wb32(s->pb, MKBETAG('I', 'n', 'f', 'o'));
00255 }
00256
00257 avio_seek(s->pb, mp3->frames_offset, SEEK_SET);
00258 avio_wb32(s->pb, mp3->frames);
00259 avio_wb32(s->pb, mp3->size);
00260
00261 avio_w8(s->pb, 0);
00262
00263 for (i = 1; i < VBR_TOC_SIZE; ++i) {
00264 int j = i * mp3->pos / VBR_TOC_SIZE;
00265 int seek_point = 256LL * mp3->bag[j] / mp3->size;
00266 avio_w8(s->pb, FFMIN(seek_point, 255));
00267 }
00268
00269 avio_flush(s->pb);
00270 avio_seek(s->pb, 0, SEEK_END);
00271 }
00272
00273 static int mp3_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
00274 {
00275 if (! pkt || ! pkt->data || pkt->size < 4)
00276 return ff_raw_write_packet(s, pkt);
00277 else {
00278 MP3Context *mp3 = s->priv_data;
00279 MPADecodeHeader c;
00280 int av_unused base;
00281
00282 avpriv_mpegaudio_decode_header(&c, AV_RB32(pkt->data));
00283
00284 if (!mp3->initial_bitrate)
00285 mp3->initial_bitrate = c.bit_rate;
00286 if (!mp3->has_variable_bitrate) {
00287 if ((c.bit_rate == 0) || (mp3->initial_bitrate != c.bit_rate))
00288 mp3->has_variable_bitrate = 1;
00289 }
00290
00291 #ifdef FILTER_VBR_HEADERS
00292
00293 base = 4 + xing_offtbl[c.lsf == 1][c.nb_channels == 1];
00294
00295 if (base + 4 <= pkt->size) {
00296 uint32_t v = AV_RB32(pkt->data + base);
00297
00298 if (MKBETAG('X','i','n','g') == v || MKBETAG('I','n','f','o') == v)
00299 return 0;
00300 }
00301
00302
00303 base = 4 + 32;
00304
00305 if (base + 4 <= pkt->size && MKBETAG('V','B','R','I') == AV_RB32(pkt->data + base))
00306 return 0;
00307 #endif
00308
00309 if (mp3->frames_offset)
00310 mp3_xing_add_frame(s, pkt);
00311
00312 return ff_raw_write_packet(s, pkt);
00313 }
00314 }
00315
00316 static int mp3_queue_flush(AVFormatContext *s)
00317 {
00318 MP3Context *mp3 = s->priv_data;
00319 AVPacketList *pktl;
00320 int ret = 0, write = 1;
00321
00322 ff_id3v2_finish(&mp3->id3, s->pb);
00323 mp3_write_xing(s);
00324
00325 while ((pktl = mp3->queue)) {
00326 if (write && (ret = mp3_write_packet_internal(s, &pktl->pkt)) < 0)
00327 write = 0;
00328 av_free_packet(&pktl->pkt);
00329 mp3->queue = pktl->next;
00330 av_freep(&pktl);
00331 }
00332 mp3->queue_end = NULL;
00333 return ret;
00334 }
00335
00336 static int mp2_write_trailer(struct AVFormatContext *s)
00337 {
00338 uint8_t buf[ID3v1_TAG_SIZE];
00339 MP3Context *mp3 = s->priv_data;
00340
00341 if (mp3 && mp3->pics_to_write) {
00342 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
00343 "attached pictures.\n");
00344 mp3_queue_flush(s);
00345 }
00346
00347
00348 if (mp3 && mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
00349 avio_write(s->pb, buf, ID3v1_TAG_SIZE);
00350 }
00351
00352
00353 if (mp3 && mp3->frames_offset) {
00354 avio_seek(s->pb, mp3->frames_offset, SEEK_SET);
00355 avio_wb32(s->pb, s->streams[mp3->audio_stream_idx]->nb_frames);
00356 avio_seek(s->pb, 0, SEEK_END);
00357 }
00358
00359 avio_flush(s->pb);
00360
00361 return 0;
00362 }
00363
00364 #if CONFIG_MP2_MUXER
00365 AVOutputFormat ff_mp2_muxer = {
00366 .name = "mp2",
00367 .long_name = NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
00368 .mime_type = "audio/x-mpeg",
00369 .extensions = "mp2,m2a",
00370 .audio_codec = CODEC_ID_MP2,
00371 .video_codec = CODEC_ID_NONE,
00372 .write_packet = ff_raw_write_packet,
00373 .write_trailer = mp2_write_trailer,
00374 .flags = AVFMT_NOTIMESTAMPS,
00375 };
00376 #endif
00377
00378 #if CONFIG_MP3_MUXER
00379
00380 static const AVOption options[] = {
00381 { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
00382 offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.dbl = 4}, 3, 4, AV_OPT_FLAG_ENCODING_PARAM},
00383 { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
00384 offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
00385 { NULL },
00386 };
00387
00388 static const AVClass mp3_muxer_class = {
00389 .class_name = "MP3 muxer",
00390 .item_name = av_default_item_name,
00391 .option = options,
00392 .version = LIBAVUTIL_VERSION_INT,
00393 };
00394
00395 static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
00396 {
00397 MP3Context *mp3 = s->priv_data;
00398
00399 if (pkt->stream_index == mp3->audio_stream_idx) {
00400 if (mp3->pics_to_write) {
00401
00402 AVPacketList *pktl = av_mallocz(sizeof(*pktl));
00403 if (!pktl)
00404 return AVERROR(ENOMEM);
00405
00406 pktl->pkt = *pkt;
00407 pkt->destruct = NULL;
00408
00409 if (mp3->queue_end)
00410 mp3->queue_end->next = pktl;
00411 else
00412 mp3->queue = pktl;
00413 mp3->queue_end = pktl;
00414 } else
00415 return mp3_write_packet_internal(s, pkt);
00416 } else {
00417 int ret;
00418
00419
00420 if (s->streams[pkt->stream_index]->nb_frames == 1) {
00421 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
00422 " ignoring.\n", pkt->stream_index);
00423 }
00424 if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
00425 return 0;
00426
00427 if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
00428 return ret;
00429 mp3->pics_to_write--;
00430
00431
00432 if (!mp3->pics_to_write &&
00433 (ret = mp3_queue_flush(s)) < 0)
00434 return ret;
00435 }
00436
00437 return 0;
00438 }
00439
00444 static int mp3_write_header(struct AVFormatContext *s)
00445 {
00446 MP3Context *mp3 = s->priv_data;
00447 int ret, i;
00448
00449
00450
00451 mp3->audio_stream_idx = -1;
00452 for (i = 0; i < s->nb_streams; i++) {
00453 AVStream *st = s->streams[i];
00454 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00455 if (mp3->audio_stream_idx >= 0 || st->codec->codec_id != CODEC_ID_MP3) {
00456 av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
00457 "audio stream is required.\n");
00458 return AVERROR(EINVAL);
00459 }
00460 mp3->audio_stream_idx = i;
00461 } else if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO) {
00462 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
00463 return AVERROR(EINVAL);
00464 }
00465 }
00466 if (mp3->audio_stream_idx < 0) {
00467 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
00468 return AVERROR(EINVAL);
00469 }
00470 mp3->pics_to_write = s->nb_streams - 1;
00471
00472 ff_id3v2_start(&mp3->id3, s->pb, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC);
00473 ret = ff_id3v2_write_metadata(s, &mp3->id3);
00474 if (ret < 0)
00475 return ret;
00476
00477 if (!mp3->pics_to_write) {
00478 ff_id3v2_finish(&mp3->id3, s->pb);
00479 mp3_write_xing(s);
00480 }
00481
00482 return 0;
00483 }
00484
00485 static int mp3_write_trailer(AVFormatContext *s)
00486 {
00487 MP3Context *mp3 = s->priv_data;
00488 int ret=mp2_write_trailer(s);
00489
00490 if (ret < 0)
00491 return ret;
00492
00493 if (mp3->frames_offset)
00494 mp3_fix_xing(s);
00495
00496 return 0;
00497 }
00498
00499 AVOutputFormat ff_mp3_muxer = {
00500 .name = "mp3",
00501 .long_name = NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
00502 .mime_type = "audio/x-mpeg",
00503 .extensions = "mp3",
00504 .priv_data_size = sizeof(MP3Context),
00505 .audio_codec = CODEC_ID_MP3,
00506 .video_codec = CODEC_ID_PNG,
00507 .write_header = mp3_write_header,
00508 .write_packet = mp3_write_packet,
00509 .write_trailer = mp3_write_trailer,
00510 .flags = AVFMT_NOTIMESTAMPS,
00511 .priv_class = &mp3_muxer_class,
00512 };
00513 #endif