]> git.sesse.net Git - ffmpeg/blob - libavcodec/bitstream_filter.c
50fd47caac05487d0fafb620df847f179ac97fcf
[ffmpeg] / libavcodec / bitstream_filter.c
1
2 #include "avcodec.h"
3
4 AVBitStreamFilter *first_bitstream_filter= NULL;
5
6 void av_register_bitstream_filter(AVBitStreamFilter *bsf){
7     bsf->next = first_bitstream_filter;
8     first_bitstream_filter= bsf;
9 }
10
11 AVBitStreamFilterContext *av_bitstream_filter_init(const char *name){
12     AVBitStreamFilter *bsf= first_bitstream_filter;
13
14     while(bsf){
15         if(!strcmp(name, bsf->name)){
16             AVBitStreamFilterContext *bsfc= av_mallocz(sizeof(AVBitStreamFilterContext));
17             bsfc->filter= bsf;
18             bsfc->priv_data= av_mallocz(bsf->priv_data_size);
19             return bsfc;
20         }
21         bsf= bsf->next;
22     }
23     return NULL;
24 }
25
26 void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc){
27     av_freep(&bsfc->priv_data);
28     av_parser_close(bsfc->parser);
29     av_free(bsfc);
30 }
31
32 int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
33                                AVCodecContext *avctx, const char *args,
34                      uint8_t **poutbuf, int *poutbuf_size,
35                      const uint8_t *buf, int buf_size, int keyframe){
36     *poutbuf= (uint8_t *) buf;
37     *poutbuf_size= buf_size;
38     return bsfc->filter->filter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, buf_size, keyframe);
39 }
40
41 static int dump_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
42                      uint8_t **poutbuf, int *poutbuf_size,
43                      const uint8_t *buf, int buf_size, int keyframe){
44     int cmd= args ? *args : 0;
45     /* cast to avoid warning about discarding qualifiers */
46     if(avctx->extradata){
47         if(  (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) && cmd=='a')
48            ||(keyframe && (cmd=='k' || !cmd))
49            ||(cmd=='e')
50             /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
51             int size= buf_size + avctx->extradata_size;
52             *poutbuf_size= size;
53             *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
54
55             memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
56             memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
57             return 1;
58         }
59     }
60     return 0;
61 }
62
63 static int remove_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
64                      uint8_t **poutbuf, int *poutbuf_size,
65                      const uint8_t *buf, int buf_size, int keyframe){
66     int cmd= args ? *args : 0;
67     AVCodecParserContext *s;
68
69     if(!bsfc->parser){
70         bsfc->parser= av_parser_init(avctx->codec_id);
71     }
72     s= bsfc->parser;
73
74     if(s && s->parser->split){
75         if(  (((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) && cmd=='a')
76            ||(!keyframe && cmd=='k')
77            ||(cmd=='e' || !cmd)
78           ){
79             int i= s->parser->split(avctx, buf, buf_size);
80             buf += i;
81             buf_size -= i;
82         }
83     }
84     *poutbuf= (uint8_t *) buf;
85     *poutbuf_size= buf_size;
86
87     return 0;
88 }
89
90 static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
91                      uint8_t **poutbuf, int *poutbuf_size,
92                      const uint8_t *buf, int buf_size, int keyframe){
93     int amount= args ? atoi(args) : 10000;
94     unsigned int *state= bsfc->priv_data;
95     int i;
96
97     *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
98
99     memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
100     for(i=0; i<buf_size; i++){
101         (*state) += (*poutbuf)[i] + 1;
102         if(*state % amount == 0)
103             (*poutbuf)[i] = *state;
104     }
105     return 1;
106 }
107
108 AVBitStreamFilter dump_extradata_bsf={
109     "dump_extra",
110     0,
111     dump_extradata,
112 };
113
114 AVBitStreamFilter remove_extradata_bsf={
115     "remove_extra",
116     0,
117     remove_extradata,
118 };
119
120 AVBitStreamFilter noise_bsf={
121     "noise",
122     sizeof(int),
123     noise,
124 };