Libav
|
00001 /* 00002 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> 00003 * 00004 * This file is part of FFmpeg. 00005 * 00006 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #include "avcodec.h" 00022 #include "mpegaudio.h" 00023 #include "mpegaudiodata.h" 00024 00025 00026 static int mp3_header_decompress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, 00027 uint8_t **poutbuf, int *poutbuf_size, 00028 const uint8_t *buf, int buf_size, int keyframe){ 00029 uint32_t header; 00030 int sample_rate= avctx->sample_rate; 00031 int sample_rate_index=0; 00032 int lsf, mpeg25, bitrate_index, frame_size; 00033 00034 header = AV_RB32(buf); 00035 if(ff_mpa_check_header(header) >= 0){ 00036 *poutbuf= (uint8_t *) buf; 00037 *poutbuf_size= buf_size; 00038 00039 return 0; 00040 } 00041 00042 if(avctx->extradata_size != 15 || strcmp(avctx->extradata, "FFCMP3 0.0")){ 00043 av_log(avctx, AV_LOG_ERROR, "Extradata invalid %d\n", avctx->extradata_size); 00044 return -1; 00045 } 00046 00047 header= AV_RB32(avctx->extradata+11) & MP3_MASK; 00048 00049 lsf = sample_rate < (24000+32000)/2; 00050 mpeg25 = sample_rate < (12000+16000)/2; 00051 sample_rate_index= (header>>10)&3; 00052 sample_rate= ff_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); //in case sample rate is a little off 00053 00054 for(bitrate_index=2; bitrate_index<30; bitrate_index++){ 00055 frame_size = ff_mpa_bitrate_tab[lsf][2][bitrate_index>>1]; 00056 frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1); 00057 if(frame_size == buf_size + 4) 00058 break; 00059 if(frame_size == buf_size + 6) 00060 break; 00061 } 00062 if(bitrate_index == 30){ 00063 av_log(avctx, AV_LOG_ERROR, "Could not find bitrate_index.\n"); 00064 return -1; 00065 } 00066 00067 header |= (bitrate_index&1)<<9; 00068 header |= (bitrate_index>>1)<<12; 00069 header |= (frame_size == buf_size + 4)<<16; //FIXME actually set a correct crc instead of 0 00070 00071 *poutbuf_size= frame_size; 00072 *poutbuf= av_malloc(frame_size + FF_INPUT_BUFFER_PADDING_SIZE); 00073 memcpy(*poutbuf + frame_size - buf_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); 00074 00075 if(avctx->channels==2){ 00076 uint8_t *p= *poutbuf + frame_size - buf_size; 00077 if(lsf){ 00078 FFSWAP(int, p[1], p[2]); 00079 header |= (p[1] & 0xC0)>>2; 00080 p[1] &= 0x3F; 00081 }else{ 00082 header |= p[1] & 0x30; 00083 p[1] &= 0xCF; 00084 } 00085 } 00086 00087 AV_WB32(*poutbuf, header); 00088 00089 return 1; 00090 } 00091 00092 AVBitStreamFilter mp3_header_decompress_bsf={ 00093 "mp3decomp", 00094 0, 00095 mp3_header_decompress, 00096 };