]> git.sesse.net Git - ffmpeg/blob - libavcodec/bitstream_filter.c
Switch idct_mmx_xvid.c from GPL to LGPL as permitted by the
[ffmpeg] / libavcodec / bitstream_filter.c
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "avcodec.h"
20
21 AVBitStreamFilter *first_bitstream_filter= NULL;
22
23 void av_register_bitstream_filter(AVBitStreamFilter *bsf){
24     bsf->next = first_bitstream_filter;
25     first_bitstream_filter= bsf;
26 }
27
28 AVBitStreamFilterContext *av_bitstream_filter_init(const char *name){
29     AVBitStreamFilter *bsf= first_bitstream_filter;
30
31     while(bsf){
32         if(!strcmp(name, bsf->name)){
33             AVBitStreamFilterContext *bsfc= av_mallocz(sizeof(AVBitStreamFilterContext));
34             bsfc->filter= bsf;
35             bsfc->priv_data= av_mallocz(bsf->priv_data_size);
36             return bsfc;
37         }
38         bsf= bsf->next;
39     }
40     return NULL;
41 }
42
43 void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc){
44     av_freep(&bsfc->priv_data);
45     av_parser_close(bsfc->parser);
46     av_free(bsfc);
47 }
48
49 int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
50                                AVCodecContext *avctx, const char *args,
51                      uint8_t **poutbuf, int *poutbuf_size,
52                      const uint8_t *buf, int buf_size, int keyframe){
53     *poutbuf= (uint8_t *) buf;
54     *poutbuf_size= buf_size;
55     return bsfc->filter->filter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, buf_size, keyframe);
56 }
57
58 static int dump_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
59                      uint8_t **poutbuf, int *poutbuf_size,
60                      const uint8_t *buf, int buf_size, int keyframe){
61     int cmd= args ? *args : 0;
62     /* cast to avoid warning about discarding qualifiers */
63     if(avctx->extradata){
64         if(  (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) && cmd=='a')
65            ||(keyframe && (cmd=='k' || !cmd))
66            ||(cmd=='e')
67             /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
68             int size= buf_size + avctx->extradata_size;
69             *poutbuf_size= size;
70             *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
71
72             memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
73             memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
74             return 1;
75         }
76     }
77     return 0;
78 }
79
80 static int remove_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
81                      uint8_t **poutbuf, int *poutbuf_size,
82                      const uint8_t *buf, int buf_size, int keyframe){
83     int cmd= args ? *args : 0;
84     AVCodecParserContext *s;
85
86     if(!bsfc->parser){
87         bsfc->parser= av_parser_init(avctx->codec_id);
88     }
89     s= bsfc->parser;
90
91     if(s && s->parser->split){
92         if(  (((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) && cmd=='a')
93            ||(!keyframe && cmd=='k')
94            ||(cmd=='e' || !cmd)
95           ){
96             int i= s->parser->split(avctx, buf, buf_size);
97             buf += i;
98             buf_size -= i;
99         }
100     }
101     *poutbuf= (uint8_t *) buf;
102     *poutbuf_size= buf_size;
103
104     return 0;
105 }
106
107 static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
108                      uint8_t **poutbuf, int *poutbuf_size,
109                      const uint8_t *buf, int buf_size, int keyframe){
110     int amount= args ? atoi(args) : 10000;
111     unsigned int *state= bsfc->priv_data;
112     int i;
113
114     *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
115
116     memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
117     for(i=0; i<buf_size; i++){
118         (*state) += (*poutbuf)[i] + 1;
119         if(*state % amount == 0)
120             (*poutbuf)[i] = *state;
121     }
122     return 1;
123 }
124
125 AVBitStreamFilter dump_extradata_bsf={
126     "dump_extra",
127     0,
128     dump_extradata,
129 };
130
131 AVBitStreamFilter remove_extradata_bsf={
132     "remove_extra",
133     0,
134     remove_extradata,
135 };
136
137 AVBitStreamFilter noise_bsf={
138     "noise",
139     sizeof(int),
140     noise,
141 };