• 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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 "libavutil/mathematics.h"
00026 #include "avformat.h"
00027 #include "internal.h"
00028 #include "id3v2.h"
00029 #include "id3v1.h"
00030 #include "libavcodec/mpegaudiodecheader.h"
00031 
00032 /* mp3 read */
00033 
00034 static int mp3_read_probe(AVProbeData *p)
00035 {
00036     int max_frames, first_frames = 0;
00037     int fsize, frames, sample_rate;
00038     uint32_t header;
00039     uint8_t *buf, *buf0, *buf2, *end;
00040     AVCodecContext avctx;
00041 
00042     buf0 = p->buf;
00043     end = p->buf + p->buf_size - sizeof(uint32_t);
00044     while(buf0 < end && !*buf0)
00045         buf0++;
00046 
00047     max_frames = 0;
00048     buf = buf0;
00049 
00050     for(; buf < end; buf= buf2+1) {
00051         buf2 = buf;
00052 
00053         for(frames = 0; buf2 < end; frames++) {
00054             header = AV_RB32(buf2);
00055             fsize = avpriv_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
00056             if(fsize < 0)
00057                 break;
00058             buf2 += fsize;
00059         }
00060         max_frames = FFMAX(max_frames, frames);
00061         if(buf == buf0)
00062             first_frames= frames;
00063     }
00064     // keep this in sync with ac3 probe, both need to avoid
00065     // issues with MPEG-files!
00066     if   (first_frames>=4) return AVPROBE_SCORE_MAX/2+1;
00067     else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
00068     else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
00069     else if(max_frames>=1) return 1;
00070     else                   return 0;
00071 //mpegps_mp3_unrecognized_format.mpg has max_frames=3
00072 }
00073 
00077 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
00078 {
00079     uint32_t v, spf;
00080     unsigned frames = 0; /* Total number of frames in file */
00081     unsigned size = 0; /* Total number of bytes in the stream */
00082     const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
00083     MPADecodeHeader c;
00084     int vbrtag_size = 0;
00085 
00086     v = avio_rb32(s->pb);
00087     if(ff_mpa_check_header(v) < 0)
00088       return -1;
00089 
00090     if (avpriv_mpegaudio_decode_header(&c, v) == 0)
00091         vbrtag_size = c.frame_size;
00092     if(c.layer != 3)
00093         return -1;
00094 
00095     /* Check for Xing / Info tag */
00096     avio_skip(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1]);
00097     v = avio_rb32(s->pb);
00098     if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
00099         v = avio_rb32(s->pb);
00100         if(v & 0x1)
00101             frames = avio_rb32(s->pb);
00102         if(v & 0x2)
00103             size = avio_rb32(s->pb);
00104     }
00105 
00106     /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
00107     avio_seek(s->pb, base + 4 + 32, SEEK_SET);
00108     v = avio_rb32(s->pb);
00109     if(v == MKBETAG('V', 'B', 'R', 'I')) {
00110         /* Check tag version */
00111         if(avio_rb16(s->pb) == 1) {
00112             /* skip delay and quality */
00113             avio_skip(s->pb, 4);
00114             frames = avio_rb32(s->pb);
00115             size = avio_rb32(s->pb);
00116         }
00117     }
00118 
00119     if(!frames && !size)
00120         return -1;
00121 
00122     /* Skip the vbr tag frame */
00123     avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
00124 
00125     spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
00126     if(frames)
00127         st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
00128                                     st->time_base);
00129     if(size && frames)
00130         st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
00131 
00132     return 0;
00133 }
00134 
00135 static int mp3_read_header(AVFormatContext *s,
00136                            AVFormatParameters *ap)
00137 {
00138     AVStream *st;
00139     int64_t off;
00140 
00141     st = avformat_new_stream(s, NULL);
00142     if (!st)
00143         return AVERROR(ENOMEM);
00144 
00145     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00146     st->codec->codec_id = CODEC_ID_MP3;
00147     st->need_parsing = AVSTREAM_PARSE_FULL;
00148     st->start_time = 0;
00149 
00150     // lcm of all mp3 sample rates
00151     avpriv_set_pts_info(st, 64, 1, 14112000);
00152 
00153     off = avio_tell(s->pb);
00154 
00155     if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
00156         ff_id3v1_read(s);
00157 
00158     if (mp3_parse_vbr_tags(s, st, off) < 0)
00159         avio_seek(s->pb, off, SEEK_SET);
00160 
00161     /* the parameters will be extracted from the compressed bitstream */
00162     return 0;
00163 }
00164 
00165 #define MP3_PACKET_SIZE 1024
00166 
00167 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
00168 {
00169     int ret, size;
00170     //    AVStream *st = s->streams[0];
00171 
00172     size= MP3_PACKET_SIZE;
00173 
00174     ret= av_get_packet(s->pb, pkt, size);
00175 
00176     pkt->stream_index = 0;
00177     if (ret <= 0) {
00178         return AVERROR(EIO);
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     .name           = "mp3",
00193     .long_name      = NULL_IF_CONFIG_SMALL("MPEG audio layer 2/3"),
00194     .read_probe     = mp3_read_probe,
00195     .read_header    = mp3_read_header,
00196     .read_packet    = mp3_read_packet,
00197     .flags= AVFMT_GENERIC_INDEX,
00198     .extensions = "mp2,mp3,m2a", /* XXX: use probe */
00199 };
Generated on Thu Jul 11 2013 15:38:23 for Libav by doxygen 1.7.1