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 <string.h>
22 
23 #include "avcodec.h"
24 #include "libavutil/atomic.h"
25 #include "libavutil/mem.h"
26 
27 /* type pun fix */
28 typedef union {
29     AVBitStreamFilter *t_AVBSF;
30     void * volatile t_vvoid;
31 } AVBSF_t;
32 
33 static AVBSF_t first_bitstream_filter = {NULL};
34 
av_bitstream_filter_next(const AVBitStreamFilter * f)35 AVBitStreamFilter *av_bitstream_filter_next(const AVBitStreamFilter *f)
36 {
37     if (f)
38         return f->next;
39     else
40         return first_bitstream_filter.t_AVBSF;
41 }
42 
av_register_bitstream_filter(AVBitStreamFilter * bsf)43 void av_register_bitstream_filter(AVBitStreamFilter *bsf)
44 {
45     do {
46         bsf->next = first_bitstream_filter.t_AVBSF;
47     } while(bsf->next != avpriv_atomic_ptr_cas((void * volatile *)&first_bitstream_filter, bsf->next, bsf));
48 }
49 
av_bitstream_filter_init(const char * name)50 AVBitStreamFilterContext *av_bitstream_filter_init(const char *name)
51 {
52     AVBitStreamFilter *bsf = first_bitstream_filter.t_AVBSF;
53 
54     while (bsf) {
55         if (!strcmp(name, bsf->name)) {
56             AVBitStreamFilterContext *bsfc =
57                 av_mallocz(sizeof(AVBitStreamFilterContext));
58             bsfc->filter    = bsf;
59             bsfc->priv_data =
60                 bsf->priv_data_size ? av_mallocz(bsf->priv_data_size) : NULL;
61             return bsfc;
62         }
63         bsf = bsf->next;
64     }
65     return NULL;
66 }
67 
av_bitstream_filter_close(AVBitStreamFilterContext * bsfc)68 void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc)
69 {
70     if (!bsfc)
71         return;
72     if (bsfc->filter->close)
73         bsfc->filter->close(bsfc);
74     av_freep(&bsfc->priv_data);
75     av_parser_close(bsfc->parser);
76     av_free(bsfc);
77 }
78 
av_bitstream_filter_filter(AVBitStreamFilterContext * bsfc,AVCodecContext * avctx,const char * args,uint8_t ** poutbuf,int * poutbuf_size,const uint8_t * buf,int buf_size,int keyframe)79 int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
80                                AVCodecContext *avctx, const char *args,
81                                uint8_t **poutbuf, int *poutbuf_size,
82                                const uint8_t *buf, int buf_size, int keyframe)
83 {
84     *poutbuf      = (uint8_t *)buf;
85     *poutbuf_size = buf_size;
86     return bsfc->filter->filter(bsfc, avctx, args, poutbuf, poutbuf_size,
87                                 buf, buf_size, keyframe);
88 }
89