00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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);
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;
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 };