Libav 0.7.1
|
00001 /* 00002 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> 00003 * 00004 * This file is part of Libav. 00005 * 00006 * Libav is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * Libav is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with Libav; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #include "libavutil/intreadwrite.h" 00022 #include "avcodec.h" 00023 #include "mpegaudiodecheader.h" 00024 #include "mpegaudiodata.h" 00025 00026 00027 static int mp3_header_decompress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, 00028 uint8_t **poutbuf, int *poutbuf_size, 00029 const uint8_t *buf, int buf_size, int keyframe){ 00030 uint32_t header; 00031 int sample_rate= avctx->sample_rate; 00032 int sample_rate_index=0; 00033 int lsf, mpeg25, bitrate_index, frame_size; 00034 00035 header = AV_RB32(buf); 00036 if(ff_mpa_check_header(header) >= 0){ 00037 *poutbuf= (uint8_t *) buf; 00038 *poutbuf_size= buf_size; 00039 00040 return 0; 00041 } 00042 00043 if(avctx->extradata_size != 15 || strcmp(avctx->extradata, "FFCMP3 0.0")){ 00044 av_log(avctx, AV_LOG_ERROR, "Extradata invalid %d\n", avctx->extradata_size); 00045 return -1; 00046 } 00047 00048 header= AV_RB32(avctx->extradata+11) & MP3_MASK; 00049 00050 lsf = sample_rate < (24000+32000)/2; 00051 mpeg25 = sample_rate < (12000+16000)/2; 00052 sample_rate_index= (header>>10)&3; 00053 sample_rate= ff_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); //in case sample rate is a little off 00054 00055 for(bitrate_index=2; bitrate_index<30; bitrate_index++){ 00056 frame_size = ff_mpa_bitrate_tab[lsf][2][bitrate_index>>1]; 00057 frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1); 00058 if(frame_size == buf_size + 4) 00059 break; 00060 if(frame_size == buf_size + 6) 00061 break; 00062 } 00063 if(bitrate_index == 30){ 00064 av_log(avctx, AV_LOG_ERROR, "Could not find bitrate_index.\n"); 00065 return -1; 00066 } 00067 00068 header |= (bitrate_index&1)<<9; 00069 header |= (bitrate_index>>1)<<12; 00070 header |= (frame_size == buf_size + 4)<<16; //FIXME actually set a correct crc instead of 0 00071 00072 *poutbuf_size= frame_size; 00073 *poutbuf= av_malloc(frame_size + FF_INPUT_BUFFER_PADDING_SIZE); 00074 memcpy(*poutbuf + frame_size - buf_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); 00075 00076 if(avctx->channels==2){ 00077 uint8_t *p= *poutbuf + frame_size - buf_size; 00078 if(lsf){ 00079 FFSWAP(int, p[1], p[2]); 00080 header |= (p[1] & 0xC0)>>2; 00081 p[1] &= 0x3F; 00082 }else{ 00083 header |= p[1] & 0x30; 00084 p[1] &= 0xCF; 00085 } 00086 } 00087 00088 AV_WB32(*poutbuf, header); 00089 00090 return 1; 00091 } 00092 00093 AVBitStreamFilter ff_mp3_header_decompress_bsf={ 00094 "mp3decomp", 00095 0, 00096 mp3_header_decompress, 00097 };