2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/atomic.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/opt.h"
30 FF_DISABLE_DEPRECATION_WARNINGS
32 AVBitStreamFilter *av_bitstream_filter_next(const AVBitStreamFilter *f)
34 const AVBitStreamFilter *filter = NULL;
38 filter = av_bsf_next(&opaque);
40 return av_bsf_next(&opaque);
43 void av_register_bitstream_filter(AVBitStreamFilter *bsf)
47 typedef struct BSFCompatContext {
49 int extradata_updated;
52 AVBitStreamFilterContext *av_bitstream_filter_init(const char *name)
54 AVBitStreamFilterContext *ctx = NULL;
55 BSFCompatContext *priv = NULL;
56 const AVBitStreamFilter *bsf;
58 bsf = av_bsf_get_by_name(name);
62 ctx = av_mallocz(sizeof(*ctx));
66 priv = av_mallocz(sizeof(*priv));
72 ctx->priv_data = priv;
78 av_bsf_free(&priv->ctx);
84 void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc)
86 BSFCompatContext *priv;
91 priv = bsfc->priv_data;
93 av_bsf_free(&priv->ctx);
94 av_freep(&bsfc->priv_data);
98 int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
99 AVCodecContext *avctx, const char *args,
100 uint8_t **poutbuf, int *poutbuf_size,
101 const uint8_t *buf, int buf_size, int keyframe)
103 BSFCompatContext *priv = bsfc->priv_data;
104 AVPacket pkt = { 0 };
108 ret = av_bsf_alloc(bsfc->filter, &priv->ctx);
112 ret = avcodec_parameters_from_context(priv->ctx->par_in, avctx);
116 priv->ctx->time_base_in = avctx->time_base;
118 if (bsfc->args && bsfc->filter->priv_class) {
119 const AVOption *opt = av_opt_next(priv->ctx->priv_data, NULL);
120 const char * shorthand[2] = {NULL};
123 shorthand[0] = opt->name;
125 ret = av_opt_set_from_string(priv->ctx->priv_data, bsfc->args, shorthand, "=", ":");
128 ret = av_bsf_init(priv->ctx);
136 ret = av_bsf_send_packet(priv->ctx, &pkt);
143 ret = av_bsf_receive_packet(priv->ctx, &pkt);
144 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
149 *poutbuf = av_malloc(pkt.size + AV_INPUT_BUFFER_PADDING_SIZE);
151 av_packet_unref(&pkt);
152 return AVERROR(ENOMEM);
155 *poutbuf_size = pkt.size;
156 memcpy(*poutbuf, pkt.data, pkt.size);
158 av_packet_unref(&pkt);
160 /* drain all the remaining packets we cannot return */
162 ret = av_bsf_receive_packet(priv->ctx, &pkt);
163 av_packet_unref(&pkt);
166 if (!priv->extradata_updated) {
167 /* update extradata in avctx from the output codec parameters */
168 if (priv->ctx->par_out->extradata_size && (!args || !strstr(args, "private_spspps_buf"))) {
169 av_freep(&avctx->extradata);
170 avctx->extradata_size = 0;
171 avctx->extradata = av_mallocz(priv->ctx->par_out->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
172 if (!avctx->extradata)
173 return AVERROR(ENOMEM);
174 memcpy(avctx->extradata, priv->ctx->par_out->extradata, priv->ctx->par_out->extradata_size);
175 avctx->extradata_size = priv->ctx->par_out->extradata_size;
178 priv->extradata_updated = 1;
183 FF_ENABLE_DEPRECATION_WARNINGS