00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/common.h"
00022 #include "libavutil/intreadwrite.h"
00023 #include "avcodec.h"
00024 #include "mpegaudiodecheader.h"
00025 #include "mpegaudiodata.h"
00026
00027
00028 static int mp3_header_decompress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
00029 uint8_t **poutbuf, int *poutbuf_size,
00030 const uint8_t *buf, int buf_size, int keyframe){
00031 uint32_t header;
00032 int sample_rate= avctx->sample_rate;
00033 int sample_rate_index=0;
00034 int lsf, mpeg25, bitrate_index, frame_size;
00035
00036 header = AV_RB32(buf);
00037 if(ff_mpa_check_header(header) >= 0){
00038 *poutbuf= (uint8_t *) buf;
00039 *poutbuf_size= buf_size;
00040
00041 return 0;
00042 }
00043
00044 if(avctx->extradata_size != 15 || strcmp(avctx->extradata, "FFCMP3 0.0")){
00045 av_log(avctx, AV_LOG_ERROR, "Extradata invalid %d\n", avctx->extradata_size);
00046 return -1;
00047 }
00048
00049 header= AV_RB32(avctx->extradata+11) & MP3_MASK;
00050
00051 lsf = sample_rate < (24000+32000)/2;
00052 mpeg25 = sample_rate < (12000+16000)/2;
00053 sample_rate_index= (header>>10)&3;
00054 sample_rate= avpriv_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25);
00055
00056 for(bitrate_index=2; bitrate_index<30; bitrate_index++){
00057 frame_size = avpriv_mpa_bitrate_tab[lsf][2][bitrate_index>>1];
00058 frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1);
00059 if(frame_size == buf_size + 4)
00060 break;
00061 if(frame_size == buf_size + 6)
00062 break;
00063 }
00064 if(bitrate_index == 30){
00065 av_log(avctx, AV_LOG_ERROR, "Could not find bitrate_index.\n");
00066 return -1;
00067 }
00068
00069 header |= (bitrate_index&1)<<9;
00070 header |= (bitrate_index>>1)<<12;
00071 header |= (frame_size == buf_size + 4)<<16;
00072
00073 *poutbuf_size= frame_size;
00074 *poutbuf= av_malloc(frame_size + FF_INPUT_BUFFER_PADDING_SIZE);
00075 memcpy(*poutbuf + frame_size - buf_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00076
00077 if(avctx->channels==2){
00078 uint8_t *p= *poutbuf + frame_size - buf_size;
00079 if(lsf){
00080 FFSWAP(int, p[1], p[2]);
00081 header |= (p[1] & 0xC0)>>2;
00082 p[1] &= 0x3F;
00083 }else{
00084 header |= p[1] & 0x30;
00085 p[1] &= 0xCF;
00086 }
00087 }
00088
00089 AV_WB32(*poutbuf, header);
00090
00091 return 1;
00092 }
00093
00094 AVBitStreamFilter ff_mp3_header_decompress_bsf={
00095 "mp3decomp",
00096 0,
00097 mp3_header_decompress,
00098 };