00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <strings.h>
00023 #include "libavutil/avstring.h"
00024 #include "libavutil/intreadwrite.h"
00025 #include "avformat.h"
00026 #include "id3v2.h"
00027 #include "id3v1.h"
00028
00029 #if CONFIG_MP3_DEMUXER
00030
00031 #include "libavcodec/mpegaudio.h"
00032 #include "libavcodec/mpegaudiodecheader.h"
00033
00034
00035
00036 static int mp3_read_probe(AVProbeData *p)
00037 {
00038 int max_frames, first_frames = 0;
00039 int fsize, frames, sample_rate;
00040 uint32_t header;
00041 uint8_t *buf, *buf0, *buf2, *end;
00042 AVCodecContext avctx;
00043
00044 buf0 = p->buf;
00045 end = p->buf + p->buf_size - sizeof(uint32_t);
00046 while(buf0 < end && !*buf0)
00047 buf0++;
00048
00049 max_frames = 0;
00050 buf = buf0;
00051
00052 for(; buf < end; buf= buf2+1) {
00053 buf2 = buf;
00054
00055 for(frames = 0; buf2 < end; frames++) {
00056 header = AV_RB32(buf2);
00057 fsize = ff_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
00058 if(fsize < 0)
00059 break;
00060 buf2 += fsize;
00061 }
00062 max_frames = FFMAX(max_frames, frames);
00063 if(buf == buf0)
00064 first_frames= frames;
00065 }
00066
00067
00068 if (first_frames>=4) return AVPROBE_SCORE_MAX/2+1;
00069 else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
00070 else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
00071 else if(max_frames>=1) return 1;
00072 else return 0;
00073
00074 }
00075
00079 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
00080 {
00081 uint32_t v, spf;
00082 unsigned frames = 0;
00083 unsigned size = 0;
00084 const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
00085 MPADecodeHeader c;
00086 int vbrtag_size = 0;
00087
00088 v = get_be32(s->pb);
00089 if(ff_mpa_check_header(v) < 0)
00090 return -1;
00091
00092 if (ff_mpegaudio_decode_header(&c, v) == 0)
00093 vbrtag_size = c.frame_size;
00094 if(c.layer != 3)
00095 return -1;
00096
00097
00098 url_fseek(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1], SEEK_CUR);
00099 v = get_be32(s->pb);
00100 if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
00101 v = get_be32(s->pb);
00102 if(v & 0x1)
00103 frames = get_be32(s->pb);
00104 if(v & 0x2)
00105 size = get_be32(s->pb);
00106 }
00107
00108
00109 url_fseek(s->pb, base + 4 + 32, SEEK_SET);
00110 v = get_be32(s->pb);
00111 if(v == MKBETAG('V', 'B', 'R', 'I')) {
00112
00113 if(get_be16(s->pb) == 1) {
00114
00115 url_fseek(s->pb, 4, SEEK_CUR);
00116 frames = get_be32(s->pb);
00117 size = get_be32(s->pb);
00118 }
00119 }
00120
00121 if(!frames && !size)
00122 return -1;
00123
00124
00125 url_fseek(s->pb, base + vbrtag_size, SEEK_SET);
00126
00127 spf = c.lsf ? 576 : 1152;
00128 if(frames)
00129 st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
00130 st->time_base);
00131 if(size && frames)
00132 st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
00133
00134 return 0;
00135 }
00136
00137 static int mp3_read_header(AVFormatContext *s,
00138 AVFormatParameters *ap)
00139 {
00140 AVStream *st;
00141 int64_t off;
00142
00143 st = av_new_stream(s, 0);
00144 if (!st)
00145 return AVERROR(ENOMEM);
00146
00147 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00148 st->codec->codec_id = CODEC_ID_MP3;
00149 st->need_parsing = AVSTREAM_PARSE_FULL;
00150 st->start_time = 0;
00151
00152
00153 av_set_pts_info(st, 64, 1, 14112000);
00154
00155 off = url_ftell(s->pb);
00156
00157 if (!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
00158 ff_id3v1_read(s);
00159
00160 if (mp3_parse_vbr_tags(s, st, off) < 0)
00161 url_fseek(s->pb, off, SEEK_SET);
00162
00163
00164 return 0;
00165 }
00166
00167 #define MP3_PACKET_SIZE 1024
00168
00169 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
00170 {
00171 int ret, size;
00172
00173
00174 size= MP3_PACKET_SIZE;
00175
00176 ret= av_get_packet(s->pb, pkt, size);
00177
00178 pkt->stream_index = 0;
00179 if (ret <= 0) {
00180 return AVERROR(EIO);
00181 }
00182
00183
00184 pkt->size = ret;
00185 return ret;
00186 }
00187
00188 AVInputFormat mp3_demuxer = {
00189 "mp3",
00190 NULL_IF_CONFIG_SMALL("MPEG audio layer 2/3"),
00191 0,
00192 mp3_read_probe,
00193 mp3_read_header,
00194 mp3_read_packet,
00195 .flags= AVFMT_GENERIC_INDEX,
00196 .extensions = "mp2,mp3,m2a",
00197 };
00198 #endif
00199
00200 #if CONFIG_MP2_MUXER || CONFIG_MP3_MUXER
00201 static int id3v1_set_string(AVFormatContext *s, const char *key,
00202 uint8_t *buf, int buf_size)
00203 {
00204 AVMetadataTag *tag;
00205 if ((tag = av_metadata_get(s->metadata, key, NULL, 0)))
00206 strncpy(buf, tag->value, buf_size);
00207 return !!tag;
00208 }
00209
00210 static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
00211 {
00212 AVMetadataTag *tag;
00213 int i, count = 0;
00214
00215 memset(buf, 0, ID3v1_TAG_SIZE);
00216 buf[0] = 'T';
00217 buf[1] = 'A';
00218 buf[2] = 'G';
00219 count += id3v1_set_string(s, "TIT2", buf + 3, 30);
00220 count += id3v1_set_string(s, "TPE1", buf + 33, 30);
00221 count += id3v1_set_string(s, "TALB", buf + 63, 30);
00222 count += id3v1_set_string(s, "TDRL", buf + 93, 4);
00223 count += id3v1_set_string(s, "comment", buf + 97, 30);
00224 if ((tag = av_metadata_get(s->metadata, "TRCK", NULL, 0))) {
00225 buf[125] = 0;
00226 buf[126] = atoi(tag->value);
00227 count++;
00228 }
00229 buf[127] = 0xFF;
00230 if ((tag = av_metadata_get(s->metadata, "TCON", NULL, 0))) {
00231 for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
00232 if (!strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
00233 buf[127] = i;
00234 count++;
00235 break;
00236 }
00237 }
00238 }
00239 return count;
00240 }
00241
00242
00243
00244 static void id3v2_put_size(AVFormatContext *s, int size)
00245 {
00246 put_byte(s->pb, size >> 21 & 0x7f);
00247 put_byte(s->pb, size >> 14 & 0x7f);
00248 put_byte(s->pb, size >> 7 & 0x7f);
00249 put_byte(s->pb, size & 0x7f);
00250 }
00251
00252 static void id3v2_put_ttag(AVFormatContext *s, const char *buf, int len,
00253 uint32_t tag)
00254 {
00255 put_be32(s->pb, tag);
00256 id3v2_put_size(s, len + 1);
00257 put_be16(s->pb, 0);
00258 put_byte(s->pb, 3);
00259 put_buffer(s->pb, buf, len);
00260 }
00261
00262
00263 static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
00264 {
00265 put_buffer(s->pb, pkt->data, pkt->size);
00266 put_flush_packet(s->pb);
00267 return 0;
00268 }
00269
00270 static int mp3_write_trailer(struct AVFormatContext *s)
00271 {
00272 uint8_t buf[ID3v1_TAG_SIZE];
00273
00274
00275 if (id3v1_create_tag(s, buf) > 0) {
00276 put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
00277 put_flush_packet(s->pb);
00278 }
00279 return 0;
00280 }
00281 #endif
00282
00283 #if CONFIG_MP2_MUXER
00284 AVOutputFormat mp2_muxer = {
00285 "mp2",
00286 NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
00287 "audio/x-mpeg",
00288 "mp2,m2a",
00289 0,
00290 CODEC_ID_MP2,
00291 CODEC_ID_NONE,
00292 NULL,
00293 mp3_write_packet,
00294 mp3_write_trailer,
00295 };
00296 #endif
00297
00298 #if CONFIG_MP3_MUXER
00299
00303 static int mp3_write_header(struct AVFormatContext *s)
00304 {
00305 AVMetadataTag *t = NULL;
00306 int totlen = 0;
00307 int64_t size_pos, cur_pos;
00308
00309 put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04));
00310 put_byte(s->pb, 0);
00311 put_byte(s->pb, 0);
00312
00313
00314 size_pos = url_ftell(s->pb);
00315 put_be32(s->pb, 0);
00316
00317 ff_metadata_conv(&s->metadata, ff_id3v2_metadata_conv, NULL);
00318 while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) {
00319 uint32_t tag = 0;
00320
00321 if (t->key[0] == 'T' && strlen(t->key) == 4) {
00322 int i;
00323 for (i = 0; *ff_id3v2_tags[i]; i++)
00324 if (AV_RB32(t->key) == AV_RB32(ff_id3v2_tags[i])) {
00325 int len = strlen(t->value);
00326 tag = AV_RB32(t->key);
00327 totlen += len + ID3v2_HEADER_SIZE + 2;
00328 id3v2_put_ttag(s, t->value, len + 1, tag);
00329 break;
00330 }
00331 }
00332
00333 if (!tag) {
00334 int len = strlen(t->key), len1 = strlen(t->value);
00335 char *buf = av_malloc(len + len1 + 2);
00336 if (!buf)
00337 return AVERROR(ENOMEM);
00338 tag = MKBETAG('T', 'X', 'X', 'X');
00339 strcpy(buf, t->key);
00340 strcpy(buf + len + 1, t->value);
00341 id3v2_put_ttag(s, buf, len + len1 + 2, tag);
00342 totlen += len + len1 + ID3v2_HEADER_SIZE + 3;
00343 av_free(buf);
00344 }
00345 }
00346
00347 cur_pos = url_ftell(s->pb);
00348 url_fseek(s->pb, size_pos, SEEK_SET);
00349 id3v2_put_size(s, totlen);
00350 url_fseek(s->pb, cur_pos, SEEK_SET);
00351
00352 return 0;
00353 }
00354
00355 AVOutputFormat mp3_muxer = {
00356 "mp3",
00357 NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
00358 "audio/x-mpeg",
00359 "mp3",
00360 0,
00361 CODEC_ID_MP3,
00362 CODEC_ID_NONE,
00363 mp3_write_header,
00364 mp3_write_packet,
00365 mp3_write_trailer,
00366 AVFMT_NOTIMESTAMPS,
00367 };
00368 #endif