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 "libavutil/dict.h"
00034
00035 static int id3v1_set_string(AVFormatContext *s, const char *key,
00036 uint8_t *buf, int buf_size)
00037 {
00038 AVDictionaryEntry *tag;
00039 if ((tag = av_dict_get(s->metadata, key, NULL, 0)))
00040 av_strlcpy(buf, tag->value, buf_size);
00041 return !!tag;
00042 }
00043
00044 static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
00045 {
00046 AVDictionaryEntry *tag;
00047 int i, count = 0;
00048
00049 memset(buf, 0, ID3v1_TAG_SIZE);
00050 buf[0] = 'T';
00051 buf[1] = 'A';
00052 buf[2] = 'G';
00053 count += id3v1_set_string(s, "TIT2", buf + 3, 30);
00054 count += id3v1_set_string(s, "TPE1", buf + 33, 30);
00055 count += id3v1_set_string(s, "TALB", buf + 63, 30);
00056 count += id3v1_set_string(s, "TDRL", buf + 93, 4);
00057 count += id3v1_set_string(s, "comment", buf + 97, 30);
00058 if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) {
00059 buf[125] = 0;
00060 buf[126] = atoi(tag->value);
00061 count++;
00062 }
00063 buf[127] = 0xFF;
00064 if ((tag = av_dict_get(s->metadata, "TCON", NULL, 0))) {
00065 for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
00066 if (!av_strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
00067 buf[127] = i;
00068 count++;
00069 break;
00070 }
00071 }
00072 }
00073 return count;
00074 }
00075
00076 typedef struct MP3Context {
00077 const AVClass *class;
00078 int id3v2_version;
00079 int write_id3v1;
00080 int64_t nb_frames_offset;
00081 } MP3Context;
00082
00083 static int mp3_write_trailer(struct AVFormatContext *s)
00084 {
00085 uint8_t buf[ID3v1_TAG_SIZE];
00086 MP3Context *mp3 = s->priv_data;
00087
00088
00089 if (mp3 && mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
00090 avio_write(s->pb, buf, ID3v1_TAG_SIZE);
00091 }
00092
00093
00094 if (mp3 && mp3->nb_frames_offset) {
00095 avio_seek(s->pb, mp3->nb_frames_offset, SEEK_SET);
00096 avio_wb32(s->pb, s->streams[0]->nb_frames);
00097 avio_seek(s->pb, 0, SEEK_END);
00098 }
00099
00100 avio_flush(s->pb);
00101
00102 return 0;
00103 }
00104
00105 #if CONFIG_MP2_MUXER
00106 AVOutputFormat ff_mp2_muxer = {
00107 .name = "mp2",
00108 .long_name = NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
00109 .mime_type = "audio/x-mpeg",
00110 .extensions = "mp2,m2a",
00111 .audio_codec = CODEC_ID_MP2,
00112 .video_codec = CODEC_ID_NONE,
00113 .write_packet = ff_raw_write_packet,
00114 .write_trailer = mp3_write_trailer,
00115 };
00116 #endif
00117
00118 #if CONFIG_MP3_MUXER
00119
00120 static const AVOption options[] = {
00121 { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
00122 offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.dbl = 4}, 3, 4, AV_OPT_FLAG_ENCODING_PARAM},
00123 { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
00124 offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
00125 { NULL },
00126 };
00127
00128 static const AVClass mp3_muxer_class = {
00129 .class_name = "MP3 muxer",
00130 .item_name = av_default_item_name,
00131 .option = options,
00132 .version = LIBAVUTIL_VERSION_INT,
00133 };
00134
00135
00136 static void mp3_write_xing(AVFormatContext *s)
00137 {
00138 AVCodecContext *codec = s->streams[0]->codec;
00139 MP3Context *mp3 = s->priv_data;
00140 int bitrate_idx = 1;
00141 int64_t xing_offset = (codec->channels == 2) ? 32 : 17;
00142 int32_t header;
00143 MPADecodeHeader mpah;
00144 int srate_idx, i, channels;
00145
00146 for (i = 0; i < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); i++)
00147 if (avpriv_mpa_freq_tab[i] == codec->sample_rate) {
00148 srate_idx = i;
00149 break;
00150 }
00151 if (i == FF_ARRAY_ELEMS(avpriv_mpa_freq_tab)) {
00152 av_log(s, AV_LOG_ERROR, "Unsupported sample rate.\n");
00153 return;
00154 }
00155
00156 switch (codec->channels) {
00157 case 1: channels = MPA_MONO; break;
00158 case 2: channels = MPA_STEREO; break;
00159 default: av_log(s, AV_LOG_ERROR, "Unsupported number of channels.\n"); return;
00160 }
00161
00162
00163 header = 0xff << 24;
00164 header |= (0x7 << 5 | 0x3 << 3 | 0x1 << 1 | 0x1) << 16;
00165 header |= (bitrate_idx << 4 | srate_idx << 2) << 8;
00166 header |= channels << 6;
00167 avio_wb32(s->pb, header);
00168
00169 avpriv_mpegaudio_decode_header(&mpah, header);
00170
00171 ffio_fill(s->pb, 0, xing_offset);
00172 ffio_wfourcc(s->pb, "Xing");
00173 avio_wb32(s->pb, 0x1);
00174 mp3->nb_frames_offset = avio_tell(s->pb);
00175 avio_wb32(s->pb, 0);
00176
00177 mpah.frame_size -= 4 + xing_offset + 4 + 4 + 4;
00178 ffio_fill(s->pb, 0, mpah.frame_size);
00179 }
00180
00185 static int mp3_write_header(struct AVFormatContext *s)
00186 {
00187 MP3Context *mp3 = s->priv_data;
00188 int ret;
00189
00190 ret = ff_id3v2_write(s, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC);
00191 if (ret < 0)
00192 return ret;
00193
00194 if (s->pb->seekable)
00195 mp3_write_xing(s);
00196
00197 return 0;
00198 }
00199
00200 AVOutputFormat ff_mp3_muxer = {
00201 .name = "mp3",
00202 .long_name = NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
00203 .mime_type = "audio/x-mpeg",
00204 .extensions = "mp3",
00205 .priv_data_size = sizeof(MP3Context),
00206 .audio_codec = CODEC_ID_MP3,
00207 .video_codec = CODEC_ID_NONE,
00208 .write_header = mp3_write_header,
00209 .write_packet = ff_raw_write_packet,
00210 .write_trailer = mp3_write_trailer,
00211 .flags = AVFMT_NOTIMESTAMPS,
00212 .priv_class = &mp3_muxer_class,
00213 };
00214 #endif