~ubuntu-dev/mplayer/ubuntu-feisty

« back to all changes in this revision

Viewing changes to libavcodec/bitstream_filter.c

  • Committer: William Grant
  • Date: 2007-02-03 03:16:07 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: william.grant@ubuntu.org.au-20070203031607-08gc2ompbz6spt9i
Update to 1.0rc1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
23
AVBitStreamFilter *first_bitstream_filter= NULL;
 
24
 
 
25
void av_register_bitstream_filter(AVBitStreamFilter *bsf){
 
26
    bsf->next = first_bitstream_filter;
 
27
    first_bitstream_filter= bsf;
 
28
}
 
29
 
 
30
AVBitStreamFilterContext *av_bitstream_filter_init(const char *name){
 
31
    AVBitStreamFilter *bsf= first_bitstream_filter;
 
32
 
 
33
    while(bsf){
 
34
        if(!strcmp(name, bsf->name)){
 
35
            AVBitStreamFilterContext *bsfc= av_mallocz(sizeof(AVBitStreamFilterContext));
 
36
            bsfc->filter= bsf;
 
37
            bsfc->priv_data= av_mallocz(bsf->priv_data_size);
 
38
            return bsfc;
 
39
        }
 
40
        bsf= bsf->next;
 
41
    }
 
42
    return NULL;
 
43
}
 
44
 
 
45
void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc){
 
46
    av_freep(&bsfc->priv_data);
 
47
    av_parser_close(bsfc->parser);
 
48
    av_free(bsfc);
 
49
}
 
50
 
 
51
int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
 
52
                               AVCodecContext *avctx, const char *args,
 
53
                     uint8_t **poutbuf, int *poutbuf_size,
 
54
                     const uint8_t *buf, int buf_size, int keyframe){
 
55
    *poutbuf= (uint8_t *) buf;
 
56
    *poutbuf_size= buf_size;
 
57
    return bsfc->filter->filter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, buf_size, keyframe);
 
58
}
 
59
 
 
60
static int dump_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
 
61
                     uint8_t **poutbuf, int *poutbuf_size,
 
62
                     const uint8_t *buf, int buf_size, int keyframe){
 
63
    int cmd= args ? *args : 0;
 
64
    /* cast to avoid warning about discarding qualifiers */
 
65
    if(avctx->extradata){
 
66
        if(  (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) && cmd=='a')
 
67
           ||(keyframe && (cmd=='k' || !cmd))
 
68
           ||(cmd=='e')
 
69
            /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
 
70
            int size= buf_size + avctx->extradata_size;
 
71
            *poutbuf_size= size;
 
72
            *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
 
73
 
 
74
            memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
 
75
            memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
 
76
            return 1;
 
77
        }
 
78
    }
 
79
    return 0;
 
80
}
 
81
 
 
82
static int remove_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
 
83
                     uint8_t **poutbuf, int *poutbuf_size,
 
84
                     const uint8_t *buf, int buf_size, int keyframe){
 
85
    int cmd= args ? *args : 0;
 
86
    AVCodecParserContext *s;
 
87
 
 
88
    if(!bsfc->parser){
 
89
        bsfc->parser= av_parser_init(avctx->codec_id);
 
90
    }
 
91
    s= bsfc->parser;
 
92
 
 
93
    if(s && s->parser->split){
 
94
        if(  (((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) && cmd=='a')
 
95
           ||(!keyframe && cmd=='k')
 
96
           ||(cmd=='e' || !cmd)
 
97
          ){
 
98
            int i= s->parser->split(avctx, buf, buf_size);
 
99
            buf += i;
 
100
            buf_size -= i;
 
101
        }
 
102
    }
 
103
    *poutbuf= (uint8_t *) buf;
 
104
    *poutbuf_size= buf_size;
 
105
 
 
106
    return 0;
 
107
}
 
108
 
 
109
static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
 
110
                     uint8_t **poutbuf, int *poutbuf_size,
 
111
                     const uint8_t *buf, int buf_size, int keyframe){
 
112
    int amount= args ? atoi(args) : 10000;
 
113
    unsigned int *state= bsfc->priv_data;
 
114
    int i;
 
115
 
 
116
    *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
 
117
 
 
118
    memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
 
119
    for(i=0; i<buf_size; i++){
 
120
        (*state) += (*poutbuf)[i] + 1;
 
121
        if(*state % amount == 0)
 
122
            (*poutbuf)[i] = *state;
 
123
    }
 
124
    return 1;
 
125
}
 
126
 
 
127
AVBitStreamFilter dump_extradata_bsf={
 
128
    "dump_extra",
 
129
    0,
 
130
    dump_extradata,
 
131
};
 
132
 
 
133
AVBitStreamFilter remove_extradata_bsf={
 
134
    "remove_extra",
 
135
    0,
 
136
    remove_extradata,
 
137
};
 
138
 
 
139
AVBitStreamFilter noise_bsf={
 
140
    "noise",
 
141
    sizeof(int),
 
142
    noise,
 
143
};