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 XING_NUM_BAGS 400
00083 #define XING_TOC_SIZE 100
00084
00085 #define XING_MAX_SIZE (32 + 4 + 4 + 4 + 4 + XING_TOC_SIZE)
00086
00087 typedef struct MP3Context {
00088 const AVClass *class;
00089 ID3v2EncContext id3;
00090 int id3v2_version;
00091 int write_id3v1;
00092
00093
00094 int64_t xing_offset;
00095 int32_t frames;
00096 int32_t size;
00097 uint32_t want;
00098 uint32_t seen;
00099 uint32_t pos;
00100 uint64_t bag[XING_NUM_BAGS];
00101 int initial_bitrate;
00102 int has_variable_bitrate;
00103
00104
00105 int audio_stream_idx;
00106
00107 int pics_to_write;
00108
00109
00110 AVPacketList *queue, *queue_end;
00111 } MP3Context;
00112
00113 static const uint8_t xing_offtbl[2][2] = {{32, 17}, {17, 9}};
00114
00115
00116
00117
00118 static int mp3_write_xing(AVFormatContext *s)
00119 {
00120 MP3Context *mp3 = s->priv_data;
00121 AVCodecContext *codec = s->streams[mp3->audio_stream_idx]->codec;
00122 int bitrate_idx;
00123 int best_bitrate_idx = -1;
00124 int best_bitrate_error= INT_MAX;
00125 int xing_offset;
00126 int32_t header, mask;
00127 MPADecodeHeader c;
00128 int srate_idx, ver = 0, i, channels;
00129 int needed;
00130 const char *vendor = (codec->flags & CODEC_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT;
00131
00132 if (!s->pb->seekable)
00133 return 0;
00134
00135 for (i = 0; i < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); i++) {
00136 const uint16_t base_freq = avpriv_mpa_freq_tab[i];
00137
00138 if (codec->sample_rate == base_freq) ver = 0x3;
00139 else if (codec->sample_rate == base_freq / 2) ver = 0x2;
00140 else if (codec->sample_rate == base_freq / 4) ver = 0x0;
00141 else continue;
00142
00143 srate_idx = i;
00144 break;
00145 }
00146 if (i == FF_ARRAY_ELEMS(avpriv_mpa_freq_tab)) {
00147 av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n");
00148 return -1;
00149 }
00150
00151 switch (codec->channels) {
00152 case 1: channels = MPA_MONO; break;
00153 case 2: channels = MPA_STEREO; break;
00154 default: av_log(s, AV_LOG_WARNING, "Unsupported number of channels, "
00155 "not writing Xing header.\n");
00156 return -1;
00157 }
00158
00159
00160 header = 0xff << 24;
00161 header |= (0x7 << 5 | ver << 3 | 0x1 << 1 | 0x1) << 16;
00162 header |= (srate_idx << 2) << 8;
00163 header |= channels << 6;
00164
00165 for (bitrate_idx=1; bitrate_idx<15; bitrate_idx++) {
00166 int error;
00167 avpriv_mpegaudio_decode_header(&c, header | (bitrate_idx << (4+8)));
00168 error= FFABS(c.bit_rate - codec->bit_rate);
00169 if(error < best_bitrate_error){
00170 best_bitrate_error= error;
00171 best_bitrate_idx = bitrate_idx;
00172 }
00173 }
00174 av_assert0(best_bitrate_idx >= 0);
00175
00176 for (bitrate_idx= best_bitrate_idx;; bitrate_idx++) {
00177 if (15 == bitrate_idx)
00178 return -1;
00179 mask = bitrate_idx << (4+8);
00180 header |= mask;
00181 avpriv_mpegaudio_decode_header(&c, header);
00182 xing_offset=xing_offtbl[c.lsf == 1][c.nb_channels == 1];
00183 needed = 4
00184 + xing_offset
00185 + 4
00186 + 4
00187 + 4
00188 + 4
00189 + XING_TOC_SIZE
00190 + 24
00191 ;
00192
00193 if (needed <= c.frame_size)
00194 break;
00195 header &= ~mask;
00196 }
00197
00198 avio_wb32(s->pb, header);
00199
00200 ffio_fill(s->pb, 0, xing_offset);
00201 mp3->xing_offset = avio_tell(s->pb);
00202 ffio_wfourcc(s->pb, "Xing");
00203 avio_wb32(s->pb, 0x01 | 0x02 | 0x04);
00204
00205 mp3->size = c.frame_size;
00206 mp3->want=1;
00207 mp3->seen=0;
00208 mp3->pos=0;
00209
00210 avio_wb32(s->pb, 0);
00211 avio_wb32(s->pb, 0);
00212
00213
00214 for (i = 0; i < XING_TOC_SIZE; ++i)
00215 avio_w8(s->pb, (uint8_t)(255 * i / XING_TOC_SIZE));
00216
00217 for (i = 0; i < strlen(vendor); ++i)
00218 avio_w8(s->pb, vendor[i]);
00219 for (; i < 21; ++i)
00220 avio_w8(s->pb, 0);
00221 avio_wb24(s->pb, FFMAX(codec->delay - 528 - 1, 0)<<12);
00222
00223 ffio_fill(s->pb, 0, c.frame_size - needed);
00224
00225 return 0;
00226 }
00227
00228
00229
00230
00231
00232 static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt)
00233 {
00234 int i;
00235
00236 mp3->frames++;
00237 mp3->seen++;
00238 mp3->size += pkt->size;
00239
00240 if (mp3->want == mp3->seen) {
00241 mp3->bag[mp3->pos] = mp3->size;
00242
00243 if (XING_NUM_BAGS == ++mp3->pos) {
00244
00245 for (i = 1; i < XING_NUM_BAGS; i += 2)
00246 mp3->bag[i >> 1] = mp3->bag[i];
00247
00248
00249 mp3->want *= 2;
00250
00251 mp3->pos = XING_NUM_BAGS / 2;
00252 }
00253
00254 mp3->seen = 0;
00255 }
00256 }
00257
00258 static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt)
00259 {
00260 MP3Context *mp3 = s->priv_data;
00261
00262 if (pkt && pkt->data && pkt->size >= 4) {
00263 MPADecodeHeader c;
00264 int av_unused base;
00265
00266 avpriv_mpegaudio_decode_header(&c, AV_RB32(pkt->data));
00267
00268 if (!mp3->initial_bitrate)
00269 mp3->initial_bitrate = c.bit_rate;
00270 if ((c.bit_rate == 0) || (mp3->initial_bitrate != c.bit_rate))
00271 mp3->has_variable_bitrate = 1;
00272
00273 #ifdef FILTER_VBR_HEADERS
00274
00275 base = 4 + xing_offtbl[c.lsf == 1][c.nb_channels == 1];
00276
00277 if (base + 4 <= pkt->size) {
00278 uint32_t v = AV_RB32(pkt->data + base);
00279
00280 if (MKBETAG('X','i','n','g') == v || MKBETAG('I','n','f','o') == v)
00281 return 0;
00282 }
00283
00284
00285 base = 4 + 32;
00286
00287 if (base + 4 <= pkt->size && MKBETAG('V','B','R','I') == AV_RB32(pkt->data + base))
00288 return 0;
00289 #endif
00290
00291 if (mp3->xing_offset)
00292 mp3_xing_add_frame(mp3, pkt);
00293 }
00294
00295 return ff_raw_write_packet(s, pkt);
00296 }
00297
00298 static int mp3_queue_flush(AVFormatContext *s)
00299 {
00300 MP3Context *mp3 = s->priv_data;
00301 AVPacketList *pktl;
00302 int ret = 0, write = 1;
00303
00304 ff_id3v2_finish(&mp3->id3, s->pb);
00305 mp3_write_xing(s);
00306
00307 while ((pktl = mp3->queue)) {
00308 if (write && (ret = mp3_write_audio_packet(s, &pktl->pkt)) < 0)
00309 write = 0;
00310 av_free_packet(&pktl->pkt);
00311 mp3->queue = pktl->next;
00312 av_freep(&pktl);
00313 }
00314 mp3->queue_end = NULL;
00315 return ret;
00316 }
00317
00318 static void mp3_update_xing(AVFormatContext *s)
00319 {
00320 MP3Context *mp3 = s->priv_data;
00321 int i;
00322
00323
00324 if (!mp3->has_variable_bitrate) {
00325 avio_seek(s->pb, mp3->xing_offset, SEEK_SET);
00326 ffio_wfourcc(s->pb, "Info");
00327 }
00328
00329 avio_seek(s->pb, mp3->xing_offset + 8, SEEK_SET);
00330 avio_wb32(s->pb, mp3->frames);
00331 avio_wb32(s->pb, mp3->size);
00332
00333 avio_w8(s->pb, 0);
00334
00335 for (i = 1; i < XING_TOC_SIZE; ++i) {
00336 int j = i * mp3->pos / XING_TOC_SIZE;
00337 int seek_point = 256LL * mp3->bag[j] / mp3->size;
00338 avio_w8(s->pb, FFMIN(seek_point, 255));
00339 }
00340
00341 avio_seek(s->pb, 0, SEEK_END);
00342 }
00343
00344 static int mp3_write_trailer(struct AVFormatContext *s)
00345 {
00346 uint8_t buf[ID3v1_TAG_SIZE];
00347 MP3Context *mp3 = s->priv_data;
00348
00349 if (mp3->pics_to_write) {
00350 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
00351 "attached pictures.\n");
00352 mp3_queue_flush(s);
00353 }
00354
00355
00356 if (mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
00357 avio_write(s->pb, buf, ID3v1_TAG_SIZE);
00358 }
00359
00360 if (mp3->xing_offset)
00361 mp3_update_xing(s);
00362
00363 return 0;
00364 }
00365
00366 static int query_codec(enum AVCodecID id, int std_compliance)
00367 {
00368 const CodecMime *cm= ff_id3v2_mime_tags;
00369 while(cm->id != AV_CODEC_ID_NONE) {
00370 if(id == cm->id)
00371 return MKTAG('A', 'P', 'I', 'C');
00372 cm++;
00373 }
00374 return -1;
00375 }
00376
00377 #if CONFIG_MP2_MUXER
00378 AVOutputFormat ff_mp2_muxer = {
00379 .name = "mp2",
00380 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
00381 .mime_type = "audio/x-mpeg",
00382 .extensions = "mp2,m2a",
00383 .audio_codec = AV_CODEC_ID_MP2,
00384 .video_codec = AV_CODEC_ID_NONE,
00385 .write_packet = ff_raw_write_packet,
00386 .flags = AVFMT_NOTIMESTAMPS,
00387 };
00388 #endif
00389
00390 #if CONFIG_MP3_MUXER
00391
00392 static const AVOption options[] = {
00393 { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
00394 offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 3, 4, AV_OPT_FLAG_ENCODING_PARAM},
00395 { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
00396 offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
00397 { NULL },
00398 };
00399
00400 static const AVClass mp3_muxer_class = {
00401 .class_name = "MP3 muxer",
00402 .item_name = av_default_item_name,
00403 .option = options,
00404 .version = LIBAVUTIL_VERSION_INT,
00405 };
00406
00407 static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
00408 {
00409 MP3Context *mp3 = s->priv_data;
00410
00411 if (pkt->stream_index == mp3->audio_stream_idx) {
00412 if (mp3->pics_to_write) {
00413
00414 AVPacketList *pktl = av_mallocz(sizeof(*pktl));
00415 if (!pktl)
00416 return AVERROR(ENOMEM);
00417
00418 pktl->pkt = *pkt;
00419 pkt->destruct = NULL;
00420
00421 if (mp3->queue_end)
00422 mp3->queue_end->next = pktl;
00423 else
00424 mp3->queue = pktl;
00425 mp3->queue_end = pktl;
00426 } else
00427 return mp3_write_audio_packet(s, pkt);
00428 } else {
00429 int ret;
00430
00431
00432 if (s->streams[pkt->stream_index]->nb_frames == 1) {
00433 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
00434 " ignoring.\n", pkt->stream_index);
00435 }
00436 if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
00437 return 0;
00438
00439 if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
00440 return ret;
00441 mp3->pics_to_write--;
00442
00443
00444 if (!mp3->pics_to_write &&
00445 (ret = mp3_queue_flush(s)) < 0)
00446 return ret;
00447 }
00448
00449 return 0;
00450 }
00451
00456 static int mp3_write_header(struct AVFormatContext *s)
00457 {
00458 MP3Context *mp3 = s->priv_data;
00459 int ret, i;
00460
00461
00462
00463 mp3->audio_stream_idx = -1;
00464 for (i = 0; i < s->nb_streams; i++) {
00465 AVStream *st = s->streams[i];
00466 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00467 if (mp3->audio_stream_idx >= 0 || st->codec->codec_id != AV_CODEC_ID_MP3) {
00468 av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
00469 "audio stream is required.\n");
00470 return AVERROR(EINVAL);
00471 }
00472 mp3->audio_stream_idx = i;
00473 } else if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO) {
00474 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
00475 return AVERROR(EINVAL);
00476 }
00477 }
00478 if (mp3->audio_stream_idx < 0) {
00479 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
00480 return AVERROR(EINVAL);
00481 }
00482 mp3->pics_to_write = s->nb_streams - 1;
00483
00484 ff_id3v2_start(&mp3->id3, s->pb, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC);
00485 ret = ff_id3v2_write_metadata(s, &mp3->id3);
00486 if (ret < 0)
00487 return ret;
00488
00489 if (!mp3->pics_to_write) {
00490 ff_id3v2_finish(&mp3->id3, s->pb);
00491 mp3_write_xing(s);
00492 }
00493
00494 return 0;
00495 }
00496
00497 AVOutputFormat ff_mp3_muxer = {
00498 .name = "mp3",
00499 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
00500 .mime_type = "audio/x-mpeg",
00501 .extensions = "mp3",
00502 .priv_data_size = sizeof(MP3Context),
00503 .audio_codec = AV_CODEC_ID_MP3,
00504 .video_codec = AV_CODEC_ID_PNG,
00505 .write_header = mp3_write_header,
00506 .write_packet = mp3_write_packet,
00507 .write_trailer = mp3_write_trailer,
00508 .query_codec = query_codec,
00509 .flags = AVFMT_NOTIMESTAMPS,
00510 .priv_class = &mp3_muxer_class,
00511 };
00512 #endif