2
* MP3 encoder and decoder
2
* MP3 muxer and demuxer
3
3
* Copyright (c) 2003 Fabrice Bellard.
5
* This library is free software; you can redistribute it and/or
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
6
8
* modify it under the terms of the GNU Lesser General Public
7
9
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
12
* FFmpeg is distributed in the hope that it will be useful,
11
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
15
* Lesser General Public License for more details.
15
17
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
* License along with FFmpeg; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
21
#include "avformat.h"
22
#include "mpegaudio.h"
21
24
#define ID3_HEADER_SIZE 10
22
25
#define ID3_TAG_SIZE 128
166
169
(buf[9] & 0x80) == 0);
169
static void id3_get_string(char *str, int str_size,
172
static void id3_get_string(char *str, int str_size,
170
173
const uint8_t *buf, int buf_size)
247
static int mp3_read_probe(AVProbeData *p)
249
int max_frames, first_frames;
250
int fsize, frames, sample_rate;
252
uint8_t *buf, *buf2, *end;
253
AVCodecContext avctx;
255
if(p->buf_size < ID3_HEADER_SIZE)
258
if(id3_match(p->buf))
259
return AVPROBE_SCORE_MAX/2+1; // this must be less then mpeg-ps because some retards put id3 tage before mpeg-ps files
263
end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
265
for(; buf < end; buf++) {
268
for(frames = 0; buf2 < end; frames++) {
269
header = (buf2[0] << 24) | (buf2[1] << 16) | (buf2[2] << 8) | buf2[3];
270
fsize = mpa_decode_header(&avctx, header, &sample_rate);
275
max_frames = FFMAX(max_frames, frames);
277
first_frames= frames;
279
if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
280
else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
281
else if(max_frames>=1) return 1;
243
285
static int mp3_read_header(AVFormatContext *s,
244
286
AVFormatParameters *ap)
252
294
return AVERROR_NOMEM;
254
st->codec.codec_type = CODEC_TYPE_AUDIO;
255
st->codec.codec_id = CODEC_ID_MP3;
296
st->codec->codec_type = CODEC_TYPE_AUDIO;
297
st->codec->codec_id = CODEC_ID_MP3;
298
st->need_parsing = 1;
257
300
/* try to get the TAG */
258
301
if (!url_is_streamed(&s->pb)) {
259
302
/* XXX: change that */
260
filesize = url_filesize(url_fileno(&s->pb));
303
filesize = url_fsize(&s->pb);
261
304
if (filesize > 128) {
262
305
url_fseek(&s->pb, filesize - 128, SEEK_SET);
263
306
ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE);
295
338
// AVStream *st = s->streams[0];
297
340
size= MP3_PACKET_SIZE;
299
if (av_new_packet(pkt, size) < 0)
342
ret= av_get_packet(&s->pb, pkt, size);
302
344
pkt->stream_index = 0;
303
ret = get_buffer(&s->pb, pkt->data, size);
308
348
/* note: we need to modify the packet size here to handle the last
319
360
/* simple formats */
320
361
static int mp3_write_header(struct AVFormatContext *s)
325
static int mp3_write_packet(struct AVFormatContext *s, int stream_index,
326
const uint8_t *buf, int size, int64_t pts)
366
static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
328
put_buffer(&s->pb, buf, size);
368
put_buffer(&s->pb, pkt->data, pkt->size);
329
369
put_flush_packet(&s->pb);
385
#endif //CONFIG_MUXERS
346
AVInputFormat mp3_iformat = {
387
#ifdef CONFIG_MP3_DEMUXER
388
AVInputFormat mp3_demuxer = {
354
.extensions = "mp2,mp3", /* XXX: use probe */
396
.flags= AVFMT_GENERIC_INDEX,
397
.extensions = "mp2,mp3,m2a", /* XXX: use probe */
357
AVOutputFormat mp2_oformat = {
400
#ifdef CONFIG_MP2_MUXER
401
AVOutputFormat mp2_muxer = {
359
403
"MPEG audio layer 2",
361
#ifdef CONFIG_MP3LAME
405
#ifdef CONFIG_LIBMP3LAME