• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/mp3dec.c

Go to the documentation of this file.
00001 /*
00002  * MP3 demuxer
00003  * Copyright (c) 2003 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/avstring.h"
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/dict.h"
00025 #include "avformat.h"
00026 #include "id3v2.h"
00027 #include "id3v1.h"
00028 #include "libavcodec/mpegaudiodecheader.h"
00029 
00030 /* mp3 read */
00031 
00032 static int mp3_read_probe(AVProbeData *p)
00033 {
00034     int max_frames, first_frames = 0;
00035     int fsize, frames, sample_rate;
00036     uint32_t header;
00037     uint8_t *buf, *buf0, *buf2, *end;
00038     AVCodecContext avctx;
00039 
00040     buf0 = p->buf;
00041     end = p->buf + p->buf_size - sizeof(uint32_t);
00042     while(buf0 < end && !*buf0)
00043         buf0++;
00044 
00045     max_frames = 0;
00046     buf = buf0;
00047 
00048     for(; buf < end; buf= buf2+1) {
00049         buf2 = buf;
00050 
00051         for(frames = 0; buf2 < end; frames++) {
00052             header = AV_RB32(buf2);
00053             fsize = ff_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
00054             if(fsize < 0)
00055                 break;
00056             buf2 += fsize;
00057         }
00058         max_frames = FFMAX(max_frames, frames);
00059         if(buf == buf0)
00060             first_frames= frames;
00061     }
00062     // keep this in sync with ac3 probe, both need to avoid
00063     // issues with MPEG-files!
00064     if   (first_frames>=4) return AVPROBE_SCORE_MAX/2+1;
00065     else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
00066     else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
00067     else if(max_frames>=1) return 1;
00068     else                   return 0;
00069 //mpegps_mp3_unrecognized_format.mpg has max_frames=3
00070 }
00071 
00075 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
00076 {
00077     uint32_t v, spf;
00078     unsigned frames = 0; /* Total number of frames in file */
00079     unsigned size = 0; /* Total number of bytes in the stream */
00080     const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
00081     MPADecodeHeader c;
00082     int vbrtag_size = 0;
00083 
00084     v = avio_rb32(s->pb);
00085     if(ff_mpa_check_header(v) < 0)
00086       return -1;
00087 
00088     if (ff_mpegaudio_decode_header(&c, v) == 0)
00089         vbrtag_size = c.frame_size;
00090     if(c.layer != 3)
00091         return -1;
00092 
00093     /* Check for Xing / Info tag */
00094     avio_skip(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1]);
00095     v = avio_rb32(s->pb);
00096     if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
00097         v = avio_rb32(s->pb);
00098         if(v & 0x1)
00099             frames = avio_rb32(s->pb);
00100         if(v & 0x2)
00101             size = avio_rb32(s->pb);
00102     }
00103 
00104     /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
00105     avio_seek(s->pb, base + 4 + 32, SEEK_SET);
00106     v = avio_rb32(s->pb);
00107     if(v == MKBETAG('V', 'B', 'R', 'I')) {
00108         /* Check tag version */
00109         if(avio_rb16(s->pb) == 1) {
00110             /* skip delay and quality */
00111             avio_skip(s->pb, 4);
00112             size = avio_rb32(s->pb);
00113             frames = avio_rb32(s->pb);
00114         }
00115     }
00116 
00117     if(!frames && !size)
00118         return -1;
00119 
00120     /* Skip the vbr tag frame */
00121     avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
00122 
00123     spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
00124     if(frames)
00125         st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
00126                                     st->time_base);
00127     if(size && frames)
00128         st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
00129 
00130     return 0;
00131 }
00132 
00133 static int mp3_read_header(AVFormatContext *s,
00134                            AVFormatParameters *ap)
00135 {
00136     AVStream *st;
00137     int64_t off;
00138 
00139     st = av_new_stream(s, 0);
00140     if (!st)
00141         return AVERROR(ENOMEM);
00142 
00143     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00144     st->codec->codec_id = CODEC_ID_MP3;
00145     st->need_parsing = AVSTREAM_PARSE_FULL;
00146     st->start_time = 0;
00147 
00148     // lcm of all mp3 sample rates
00149     av_set_pts_info(st, 64, 1, 14112000);
00150 
00151     off = avio_tell(s->pb);
00152 
00153     if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
00154         ff_id3v1_read(s);
00155 
00156     if (mp3_parse_vbr_tags(s, st, off) < 0)
00157         avio_seek(s->pb, off, SEEK_SET);
00158 
00159     /* the parameters will be extracted from the compressed bitstream */
00160     return 0;
00161 }
00162 
00163 #define MP3_PACKET_SIZE 1024
00164 
00165 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
00166 {
00167     int ret, size;
00168     //    AVStream *st = s->streams[0];
00169 
00170     size= MP3_PACKET_SIZE;
00171 
00172     ret= av_get_packet(s->pb, pkt, size);
00173 
00174     pkt->stream_index = 0;
00175     if (ret <= 0) {
00176         if(ret<0)
00177             return ret;
00178         return AVERROR_EOF;
00179     }
00180 
00181     if (ret > ID3v1_TAG_SIZE &&
00182         memcmp(&pkt->data[ret - ID3v1_TAG_SIZE], "TAG", 3) == 0)
00183         ret -= ID3v1_TAG_SIZE;
00184 
00185     /* note: we need to modify the packet size here to handle the last
00186        packet */
00187     pkt->size = ret;
00188     return ret;
00189 }
00190 
00191 AVInputFormat ff_mp3_demuxer = {
00192     "mp3",
00193     NULL_IF_CONFIG_SMALL("MPEG audio layer 2/3"),
00194     0,
00195     mp3_read_probe,
00196     mp3_read_header,
00197     mp3_read_packet,
00198     .flags= AVFMT_GENERIC_INDEX,
00199     .extensions = "mp2,mp3,m2a", /* XXX: use probe */
00200 };

Generated on Fri Feb 22 2013 07:24:31 for FFmpeg by  doxygen 1.7.1