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 = 0xffU << 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->data && pkt->size >= 4) {
00263 MPADecodeHeader c;
00264 int av_unused base;
00265 uint32_t head = AV_RB32(pkt->data);
00266
00267 if (ff_mpa_check_header(head) < 0) {
00268 av_log(s, AV_LOG_WARNING, "Audio packet of size %d (starting with %08X...) "
00269 "is invalid, writing it anyway.\n", pkt->size, head);
00270 return ff_raw_write_packet(s, pkt);
00271 }
00272 avpriv_mpegaudio_decode_header(&c, head);
00273
00274 if (!mp3->initial_bitrate)
00275 mp3->initial_bitrate = c.bit_rate;
00276 if ((c.bit_rate == 0) || (mp3->initial_bitrate != c.bit_rate))
00277 mp3->has_variable_bitrate = 1;
00278
00279 #ifdef FILTER_VBR_HEADERS
00280
00281 base = 4 + xing_offtbl[c.lsf == 1][c.nb_channels == 1];
00282
00283 if (base + 4 <= pkt->size) {
00284 uint32_t v = AV_RB32(pkt->data + base);
00285
00286 if (MKBETAG('X','i','n','g') == v || MKBETAG('I','n','f','o') == v)
00287 return 0;
00288 }
00289
00290
00291 base = 4 + 32;
00292
00293 if (base + 4 <= pkt->size && MKBETAG('V','B','R','I') == AV_RB32(pkt->data + base))
00294 return 0;
00295 #endif
00296
00297 if (mp3->xing_offset)
00298 mp3_xing_add_frame(mp3, pkt);
00299 }
00300
00301 return ff_raw_write_packet(s, pkt);
00302 }
00303
00304 static int mp3_queue_flush(AVFormatContext *s)
00305 {
00306 MP3Context *mp3 = s->priv_data;
00307 AVPacketList *pktl;
00308 int ret = 0, write = 1;
00309
00310 ff_id3v2_finish(&mp3->id3, s->pb);
00311 mp3_write_xing(s);
00312
00313 while ((pktl = mp3->queue)) {
00314 if (write && (ret = mp3_write_audio_packet(s, &pktl->pkt)) < 0)
00315 write = 0;
00316 av_free_packet(&pktl->pkt);
00317 mp3->queue = pktl->next;
00318 av_freep(&pktl);
00319 }
00320 mp3->queue_end = NULL;
00321 return ret;
00322 }
00323
00324 static void mp3_update_xing(AVFormatContext *s)
00325 {
00326 MP3Context *mp3 = s->priv_data;
00327 int i;
00328
00329
00330 if (!mp3->has_variable_bitrate) {
00331 avio_seek(s->pb, mp3->xing_offset, SEEK_SET);
00332 ffio_wfourcc(s->pb, "Info");
00333 }
00334
00335 avio_seek(s->pb, mp3->xing_offset + 8, SEEK_SET);
00336 avio_wb32(s->pb, mp3->frames);
00337 avio_wb32(s->pb, mp3->size);
00338
00339 avio_w8(s->pb, 0);
00340
00341 for (i = 1; i < XING_TOC_SIZE; ++i) {
00342 int j = i * mp3->pos / XING_TOC_SIZE;
00343 int seek_point = 256LL * mp3->bag[j] / mp3->size;
00344 avio_w8(s->pb, FFMIN(seek_point, 255));
00345 }
00346
00347 avio_seek(s->pb, 0, SEEK_END);
00348 }
00349
00350 static int mp3_write_trailer(struct AVFormatContext *s)
00351 {
00352 uint8_t buf[ID3v1_TAG_SIZE];
00353 MP3Context *mp3 = s->priv_data;
00354
00355 if (mp3->pics_to_write) {
00356 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
00357 "attached pictures.\n");
00358 mp3_queue_flush(s);
00359 }
00360
00361
00362 if (mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
00363 avio_write(s->pb, buf, ID3v1_TAG_SIZE);
00364 }
00365
00366 if (mp3->xing_offset)
00367 mp3_update_xing(s);
00368
00369 return 0;
00370 }
00371
00372 static int query_codec(enum AVCodecID id, int std_compliance)
00373 {
00374 const CodecMime *cm= ff_id3v2_mime_tags;
00375 while(cm->id != AV_CODEC_ID_NONE) {
00376 if(id == cm->id)
00377 return MKTAG('A', 'P', 'I', 'C');
00378 cm++;
00379 }
00380 return -1;
00381 }
00382
00383 #if CONFIG_MP2_MUXER
00384 AVOutputFormat ff_mp2_muxer = {
00385 .name = "mp2",
00386 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
00387 .mime_type = "audio/x-mpeg",
00388 .extensions = "mp2,m2a",
00389 .audio_codec = AV_CODEC_ID_MP2,
00390 .video_codec = AV_CODEC_ID_NONE,
00391 .write_packet = ff_raw_write_packet,
00392 .flags = AVFMT_NOTIMESTAMPS,
00393 };
00394 #endif
00395
00396 #if CONFIG_MP3_MUXER
00397
00398 static const AVOption options[] = {
00399 { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
00400 offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 3, 4, AV_OPT_FLAG_ENCODING_PARAM},
00401 { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
00402 offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
00403 { NULL },
00404 };
00405
00406 static const AVClass mp3_muxer_class = {
00407 .class_name = "MP3 muxer",
00408 .item_name = av_default_item_name,
00409 .option = options,
00410 .version = LIBAVUTIL_VERSION_INT,
00411 };
00412
00413 static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
00414 {
00415 MP3Context *mp3 = s->priv_data;
00416
00417 if (pkt->stream_index == mp3->audio_stream_idx) {
00418 if (mp3->pics_to_write) {
00419
00420 AVPacketList *pktl = av_mallocz(sizeof(*pktl));
00421 if (!pktl)
00422 return AVERROR(ENOMEM);
00423
00424 pktl->pkt = *pkt;
00425 pkt->destruct = NULL;
00426
00427 if (mp3->queue_end)
00428 mp3->queue_end->next = pktl;
00429 else
00430 mp3->queue = pktl;
00431 mp3->queue_end = pktl;
00432 } else
00433 return mp3_write_audio_packet(s, pkt);
00434 } else {
00435 int ret;
00436
00437
00438 if (s->streams[pkt->stream_index]->nb_frames == 1) {
00439 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
00440 " ignoring.\n", pkt->stream_index);
00441 }
00442 if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
00443 return 0;
00444
00445 if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
00446 return ret;
00447 mp3->pics_to_write--;
00448
00449
00450 if (!mp3->pics_to_write &&
00451 (ret = mp3_queue_flush(s)) < 0)
00452 return ret;
00453 }
00454
00455 return 0;
00456 }
00457
00462 static int mp3_write_header(struct AVFormatContext *s)
00463 {
00464 MP3Context *mp3 = s->priv_data;
00465 int ret, i;
00466
00467
00468
00469 mp3->audio_stream_idx = -1;
00470 for (i = 0; i < s->nb_streams; i++) {
00471 AVStream *st = s->streams[i];
00472 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00473 if (mp3->audio_stream_idx >= 0 || st->codec->codec_id != AV_CODEC_ID_MP3) {
00474 av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
00475 "audio stream is required.\n");
00476 return AVERROR(EINVAL);
00477 }
00478 mp3->audio_stream_idx = i;
00479 } else if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO) {
00480 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
00481 return AVERROR(EINVAL);
00482 }
00483 }
00484 if (mp3->audio_stream_idx < 0) {
00485 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
00486 return AVERROR(EINVAL);
00487 }
00488 mp3->pics_to_write = s->nb_streams - 1;
00489
00490 ff_id3v2_start(&mp3->id3, s->pb, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC);
00491 ret = ff_id3v2_write_metadata(s, &mp3->id3);
00492 if (ret < 0)
00493 return ret;
00494
00495 if (!mp3->pics_to_write) {
00496 ff_id3v2_finish(&mp3->id3, s->pb);
00497 mp3_write_xing(s);
00498 }
00499
00500 return 0;
00501 }
00502
00503 AVOutputFormat ff_mp3_muxer = {
00504 .name = "mp3",
00505 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
00506 .mime_type = "audio/x-mpeg",
00507 .extensions = "mp3",
00508 .priv_data_size = sizeof(MP3Context),
00509 .audio_codec = AV_CODEC_ID_MP3,
00510 .video_codec = AV_CODEC_ID_PNG,
00511 .write_header = mp3_write_header,
00512 .write_packet = mp3_write_packet,
00513 .write_trailer = mp3_write_trailer,
00514 .query_codec = query_codec,
00515 .flags = AVFMT_NOTIMESTAMPS,
00516 .priv_class = &mp3_muxer_class,
00517 };
00518 #endif