1/*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "avcodec.h"
22#include "mpegaudio.h"
23
24
25static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
26                     uint8_t **poutbuf, int *poutbuf_size,
27                     const uint8_t *buf, int buf_size, int keyframe){
28    uint32_t header, extraheader;
29    int mode_extension, header_size;
30
31    if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
32        av_log(avctx, AV_LOG_ERROR, "not standards compliant\n");
33        return -1;
34    }
35
36    header = AV_RB32(buf);
37    mode_extension= (header>>4)&3;
38
39    if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){
40output_unchanged:
41        *poutbuf= (uint8_t *) buf;
42        *poutbuf_size= buf_size;
43
44        av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header);
45        return 0;
46    }
47
48    if(avctx->extradata_size == 0){
49        avctx->extradata_size=15;
50        avctx->extradata= av_malloc(avctx->extradata_size);
51        strcpy(avctx->extradata, "FFCMP3 0.0");
52        memcpy(avctx->extradata+11, buf, 4);
53    }
54    if(avctx->extradata_size != 15){
55        av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n");
56        return -1;
57    }
58    extraheader = AV_RB32(avctx->extradata+11);
59    if((extraheader&MP3_MASK) != (header&MP3_MASK))
60        goto output_unchanged;
61
62    header_size= (header&0x10000) ? 4 : 6;
63
64    *poutbuf_size= buf_size - header_size;
65    *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
66    memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
67
68    if(avctx->channels==2){
69        if((header & (3<<19)) != 3<<19){
70            (*poutbuf)[1] &= 0x3F;
71            (*poutbuf)[1] |= mode_extension<<6;
72            FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);
73        }else{
74            (*poutbuf)[1] &= 0x8F;
75            (*poutbuf)[1] |= mode_extension<<4;
76        }
77    }
78
79    return 1;
80}
81
82AVBitStreamFilter mp3_header_compress_bsf={
83    "mp3comp",
84    0,
85    mp3_header_compress,
86};
87