~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/bitstream_filter.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc, Andrew Starr-Bochicchio, Lionel Le Folgoc
  • Date: 2008-12-26 00:10:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081226001006-2040ls9680bd1blt
Tags: 1.1.7-0.2ubuntu1
[ Andrew Starr-Bochicchio ]
* Merge from debian-multimedia (LP: #298547), Ubuntu Changes:
 - For ffmpeg-related build-deps, fix versionized dependencies
   as the ubuntu versioning is different than debian-multimedia's.

[ Lionel Le Folgoc ]
* LP: #311412 is fixed since the 1.1.7~rc1-0.1 revision.
* debian/patches/03_ffmpeg.diff: updated to fix FTBFS due to libswscale API
  change (cherry-pick from Gentoo #234383).

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
#include "avcodec.h"
22
 
#include "mpegaudio.h"
23
22
 
24
23
AVBitStreamFilter *first_bitstream_filter= NULL;
25
24
 
 
25
AVBitStreamFilter *av_bitstream_filter_next(AVBitStreamFilter *f){
 
26
    if(f) return f->next;
 
27
    else  return first_bitstream_filter;
 
28
}
 
29
 
26
30
void av_register_bitstream_filter(AVBitStreamFilter *bsf){
27
31
    bsf->next = first_bitstream_filter;
28
32
    first_bitstream_filter= bsf;
44
48
}
45
49
 
46
50
void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc){
 
51
    if(bsfc->filter->close)
 
52
        bsfc->filter->close(bsfc);
47
53
    av_freep(&bsfc->priv_data);
48
54
    av_parser_close(bsfc->parser);
49
55
    av_free(bsfc);
57
63
    *poutbuf_size= buf_size;
58
64
    return bsfc->filter->filter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, buf_size, keyframe);
59
65
}
60
 
 
61
 
static int dump_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
62
 
                     uint8_t **poutbuf, int *poutbuf_size,
63
 
                     const uint8_t *buf, int buf_size, int keyframe){
64
 
    int cmd= args ? *args : 0;
65
 
    /* cast to avoid warning about discarding qualifiers */
66
 
    if(avctx->extradata){
67
 
        if(  (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) && cmd=='a')
68
 
           ||(keyframe && (cmd=='k' || !cmd))
69
 
           ||(cmd=='e')
70
 
            /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
71
 
            int size= buf_size + avctx->extradata_size;
72
 
            *poutbuf_size= size;
73
 
            *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
74
 
 
75
 
            memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
76
 
            memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
77
 
            return 1;
78
 
        }
79
 
    }
80
 
    return 0;
81
 
}
82
 
 
83
 
static int remove_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
84
 
                     uint8_t **poutbuf, int *poutbuf_size,
85
 
                     const uint8_t *buf, int buf_size, int keyframe){
86
 
    int cmd= args ? *args : 0;
87
 
    AVCodecParserContext *s;
88
 
 
89
 
    if(!bsfc->parser){
90
 
        bsfc->parser= av_parser_init(avctx->codec_id);
91
 
    }
92
 
    s= bsfc->parser;
93
 
 
94
 
    if(s && s->parser->split){
95
 
        if(  (((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) && cmd=='a')
96
 
           ||(!keyframe && cmd=='k')
97
 
           ||(cmd=='e' || !cmd)
98
 
          ){
99
 
            int i= s->parser->split(avctx, buf, buf_size);
100
 
            buf += i;
101
 
            buf_size -= i;
102
 
        }
103
 
    }
104
 
    *poutbuf= (uint8_t *) buf;
105
 
    *poutbuf_size= buf_size;
106
 
 
107
 
    return 0;
108
 
}
109
 
 
110
 
static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
111
 
                     uint8_t **poutbuf, int *poutbuf_size,
112
 
                     const uint8_t *buf, int buf_size, int keyframe){
113
 
    int amount= args ? atoi(args) : 10000;
114
 
    unsigned int *state= bsfc->priv_data;
115
 
    int i;
116
 
 
117
 
    *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
118
 
 
119
 
    memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
120
 
    for(i=0; i<buf_size; i++){
121
 
        (*state) += (*poutbuf)[i] + 1;
122
 
        if(*state % amount == 0)
123
 
            (*poutbuf)[i] = *state;
124
 
    }
125
 
    return 1;
126
 
}
127
 
 
128
 
#define MP3_MASK 0xFFFE0CCF
129
 
 
130
 
static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
131
 
                     uint8_t **poutbuf, int *poutbuf_size,
132
 
                     const uint8_t *buf, int buf_size, int keyframe){
133
 
    uint32_t header, extraheader;
134
 
    int mode_extension, header_size;
135
 
 
136
 
    if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
137
 
        av_log(avctx, AV_LOG_ERROR, "not standards compliant\n");
138
 
        return -1;
139
 
    }
140
 
 
141
 
    header = AV_RB32(buf);
142
 
    mode_extension= (header>>4)&3;
143
 
 
144
 
    if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){
145
 
output_unchanged:
146
 
        *poutbuf= (uint8_t *) buf;
147
 
        *poutbuf_size= buf_size;
148
 
 
149
 
        av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header);
150
 
        return 0;
151
 
    }
152
 
 
153
 
    if(avctx->extradata_size == 0){
154
 
        avctx->extradata_size=15;
155
 
        avctx->extradata= av_malloc(avctx->extradata_size);
156
 
        strcpy(avctx->extradata, "FFCMP3 0.0");
157
 
        memcpy(avctx->extradata+11, buf, 4);
158
 
    }
159
 
    if(avctx->extradata_size != 15){
160
 
        av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n");
161
 
        return -1;
162
 
    }
163
 
    extraheader = AV_RB32(avctx->extradata+11);
164
 
    if((extraheader&MP3_MASK) != (header&MP3_MASK))
165
 
        goto output_unchanged;
166
 
 
167
 
    header_size= (header&0x10000) ? 4 : 6;
168
 
 
169
 
    *poutbuf_size= buf_size - header_size;
170
 
    *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
171
 
    memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
172
 
 
173
 
    if(avctx->channels==2){
174
 
        if((header & (3<<19)) != 3<<19){
175
 
            (*poutbuf)[1] &= 0x3F;
176
 
            (*poutbuf)[1] |= mode_extension<<6;
177
 
            FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);
178
 
        }else{
179
 
            (*poutbuf)[1] &= 0x8F;
180
 
            (*poutbuf)[1] |= mode_extension<<4;
181
 
        }
182
 
    }
183
 
 
184
 
    return 1;
185
 
}
186
 
 
187
 
static int mp3_header_decompress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
188
 
                     uint8_t **poutbuf, int *poutbuf_size,
189
 
                     const uint8_t *buf, int buf_size, int keyframe){
190
 
    uint32_t header;
191
 
    int sample_rate= avctx->sample_rate;
192
 
    int sample_rate_index=0;
193
 
    int lsf, mpeg25, bitrate_index, frame_size;
194
 
 
195
 
    header = AV_RB32(buf);
196
 
    if(ff_mpa_check_header(header) >= 0){
197
 
        *poutbuf= (uint8_t *) buf;
198
 
        *poutbuf_size= buf_size;
199
 
 
200
 
        return 0;
201
 
    }
202
 
 
203
 
    if(avctx->extradata_size != 15 || strcmp(avctx->extradata, "FFCMP3 0.0")){
204
 
        av_log(avctx, AV_LOG_ERROR, "Extradata invalid %d\n", avctx->extradata_size);
205
 
        return -1;
206
 
    }
207
 
 
208
 
    header= AV_RB32(avctx->extradata+11) & MP3_MASK;
209
 
 
210
 
    lsf     = sample_rate < (24000+32000)/2;
211
 
    mpeg25  = sample_rate < (12000+16000)/2;
212
 
    sample_rate_index= (header>>10)&3;
213
 
    sample_rate= mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); //in case sample rate is a little off
214
 
 
215
 
    for(bitrate_index=2; bitrate_index<30; bitrate_index++){
216
 
        frame_size = mpa_bitrate_tab[lsf][2][bitrate_index>>1];
217
 
        frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1);
218
 
        if(frame_size == buf_size + 4)
219
 
            break;
220
 
        if(frame_size == buf_size + 6)
221
 
            break;
222
 
    }
223
 
    if(bitrate_index == 30){
224
 
        av_log(avctx, AV_LOG_ERROR, "couldnt find bitrate_index\n");
225
 
        return -1;
226
 
    }
227
 
 
228
 
    header |= (bitrate_index&1)<<9;
229
 
    header |= (bitrate_index>>1)<<12;
230
 
    header |= (frame_size == buf_size + 4)<<16; //FIXME actually set a correct crc instead of 0
231
 
 
232
 
    *poutbuf_size= frame_size;
233
 
    *poutbuf= av_malloc(frame_size + FF_INPUT_BUFFER_PADDING_SIZE);
234
 
    memcpy(*poutbuf + frame_size - buf_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
235
 
 
236
 
    if(avctx->channels==2){
237
 
        uint8_t *p= *poutbuf + frame_size - buf_size;
238
 
        if(lsf){
239
 
            FFSWAP(int, p[1], p[2]);
240
 
            header |= (p[1] & 0xC0)>>2;
241
 
            p[1] &= 0x3F;
242
 
        }else{
243
 
            header |= p[1] & 0x30;
244
 
            p[1] &= 0xCF;
245
 
        }
246
 
    }
247
 
 
248
 
    (*poutbuf)[0]= header>>24;
249
 
    (*poutbuf)[1]= header>>16;
250
 
    (*poutbuf)[2]= header>> 8;
251
 
    (*poutbuf)[3]= header    ;
252
 
 
253
 
    return 1;
254
 
}
255
 
 
256
 
AVBitStreamFilter dump_extradata_bsf={
257
 
    "dump_extra",
258
 
    0,
259
 
    dump_extradata,
260
 
};
261
 
 
262
 
AVBitStreamFilter remove_extradata_bsf={
263
 
    "remove_extra",
264
 
    0,
265
 
    remove_extradata,
266
 
};
267
 
 
268
 
AVBitStreamFilter noise_bsf={
269
 
    "noise",
270
 
    sizeof(int),
271
 
    noise,
272
 
};
273
 
 
274
 
AVBitStreamFilter mp3_header_compress_bsf={
275
 
    "mp3comp",
276
 
    0,
277
 
    mp3_header_compress,
278
 
};
279
 
 
280
 
AVBitStreamFilter mp3_header_decompress_bsf={
281
 
    "mp3decomp",
282
 
    0,
283
 
    mp3_header_decompress,
284
 
};