2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/internal.h"
26 #include "libavutil/mem.h"
29 FF_DISABLE_DEPRECATION_WARNINGS
31 AVBitStreamFilter *av_bitstream_filter_next(const AVBitStreamFilter *f)
33 const AVBitStreamFilter *filter = NULL;
37 filter = av_bsf_next(&opaque);
39 return av_bsf_next(&opaque);
42 void av_register_bitstream_filter(AVBitStreamFilter *bsf)
46 typedef struct BSFCompatContext {
50 AVBitStreamFilterContext *av_bitstream_filter_init(const char *name)
52 AVBitStreamFilterContext *ctx = NULL;
53 BSFCompatContext *priv = NULL;
54 const AVBitStreamFilter *bsf;
56 bsf = av_bsf_get_by_name(name);
60 ctx = av_mallocz(sizeof(*ctx));
64 priv = av_mallocz(sizeof(*priv));
70 ctx->priv_data = priv;
76 av_bsf_free(&priv->ctx);
82 void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc)
84 BSFCompatContext *priv = bsfc->priv_data;
86 av_bsf_free(&priv->ctx);
87 av_freep(&bsfc->priv_data);
91 int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
92 AVCodecContext *avctx, const char *args,
93 uint8_t **poutbuf, int *poutbuf_size,
94 const uint8_t *buf, int buf_size, int keyframe)
96 BSFCompatContext *priv = bsfc->priv_data;
101 ret = av_bsf_alloc(bsfc->filter, &priv->ctx);
105 ret = avcodec_parameters_from_context(priv->ctx->par_in, avctx);
109 priv->ctx->time_base_in = avctx->time_base;
111 ret = av_bsf_init(priv->ctx);
115 if (priv->ctx->par_out->extradata_size) {
116 av_freep(&avctx->extradata);
117 avctx->extradata_size = 0;
118 avctx->extradata = av_mallocz(priv->ctx->par_out->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
119 if (!avctx->extradata)
120 return AVERROR(ENOMEM);
121 memcpy(avctx->extradata, priv->ctx->par_out->extradata,
122 priv->ctx->par_out->extradata_size);
123 avctx->extradata_size = priv->ctx->par_out->extradata_size;
130 ret = av_bsf_send_packet(priv->ctx, &pkt);
137 ret = av_bsf_receive_packet(priv->ctx, &pkt);
138 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
143 *poutbuf = av_malloc(pkt.size + AV_INPUT_BUFFER_PADDING_SIZE);
145 av_packet_unref(&pkt);
146 return AVERROR(ENOMEM);
149 *poutbuf_size = pkt.size;
150 memcpy(*poutbuf, pkt.data, pkt.size);
152 av_packet_unref(&pkt);
154 /* drain all the remaining packets we cannot return */
156 ret = av_bsf_receive_packet(priv->ctx, &pkt);
157 av_packet_unref(&pkt);
162 FF_ENABLE_DEPRECATION_WARNINGS