Libav 0.7.1
libavcodec/mp3_header_compress_bsf.c
Go to the documentation of this file.
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 
00025 
00026 static int mp3_header_compress(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, extraheader;
00030     int mode_extension, header_size;
00031 
00032     if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
00033         av_log(avctx, AV_LOG_ERROR, "not standards compliant\n");
00034         return -1;
00035     }
00036 
00037     header = AV_RB32(buf);
00038     mode_extension= (header>>4)&3;
00039 
00040     if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){
00041 output_unchanged:
00042         *poutbuf= (uint8_t *) buf;
00043         *poutbuf_size= buf_size;
00044 
00045         av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header);
00046         return 0;
00047     }
00048 
00049     if(avctx->extradata_size == 0){
00050         avctx->extradata_size=15;
00051         avctx->extradata= av_malloc(avctx->extradata_size);
00052         strcpy(avctx->extradata, "FFCMP3 0.0");
00053         memcpy(avctx->extradata+11, buf, 4);
00054     }
00055     if(avctx->extradata_size != 15){
00056         av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n");
00057         return -1;
00058     }
00059     extraheader = AV_RB32(avctx->extradata+11);
00060     if((extraheader&MP3_MASK) != (header&MP3_MASK))
00061         goto output_unchanged;
00062 
00063     header_size= (header&0x10000) ? 4 : 6;
00064 
00065     *poutbuf_size= buf_size - header_size;
00066     *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
00067     memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
00068 
00069     if(avctx->channels==2){
00070         if((header & (3<<19)) != 3<<19){
00071             (*poutbuf)[1] &= 0x3F;
00072             (*poutbuf)[1] |= mode_extension<<6;
00073             FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);
00074         }else{
00075             (*poutbuf)[1] &= 0x8F;
00076             (*poutbuf)[1] |= mode_extension<<4;
00077         }
00078     }
00079 
00080     return 1;
00081 }
00082 
00083 AVBitStreamFilter ff_mp3_header_compress_bsf={
00084     "mp3comp",
00085     0,
00086     mp3_header_compress,
00087 };