aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / bitstream_filter.c
blobab608a99cdb3ce9b6d34028c79ca02e82b783565
1 /*
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
21 #include <string.h>
23 #include "avcodec.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/mem.h"
28 #if FF_API_OLD_BSF
29 FF_DISABLE_DEPRECATION_WARNINGS
31 AVBitStreamFilter *av_bitstream_filter_next(const AVBitStreamFilter *f)
33 const AVBitStreamFilter *filter = NULL;
34 void *opaque = NULL;
36 while (filter != f)
37 filter = av_bsf_next(&opaque);
39 return av_bsf_next(&opaque);
42 void av_register_bitstream_filter(AVBitStreamFilter *bsf)
46 typedef struct BSFCompatContext {
47 AVBSFContext *ctx;
48 } 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);
57 if (!bsf)
58 return NULL;
60 ctx = av_mallocz(sizeof(*ctx));
61 if (!ctx)
62 return NULL;
64 priv = av_mallocz(sizeof(*priv));
65 if (!priv)
66 goto fail;
69 ctx->filter = bsf;
70 ctx->priv_data = priv;
72 return ctx;
74 fail:
75 if (priv)
76 av_bsf_free(&priv->ctx);
77 av_freep(&priv);
78 av_freep(&ctx);
79 return NULL;
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);
88 av_free(bsfc);
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;
97 AVPacket pkt = { 0 };
98 int ret;
100 if (!priv->ctx) {
101 ret = av_bsf_alloc(bsfc->filter, &priv->ctx);
102 if (ret < 0)
103 return ret;
105 ret = avcodec_parameters_from_context(priv->ctx->par_in, avctx);
106 if (ret < 0)
107 return ret;
109 priv->ctx->time_base_in = avctx->time_base;
111 ret = av_bsf_init(priv->ctx);
112 if (ret < 0)
113 return ret;
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;
127 pkt.data = buf;
128 pkt.size = buf_size;
130 ret = av_bsf_send_packet(priv->ctx, &pkt);
131 if (ret < 0)
132 return ret;
134 *poutbuf = NULL;
135 *poutbuf_size = 0;
137 ret = av_bsf_receive_packet(priv->ctx, &pkt);
138 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
139 return 0;
140 else if (ret < 0)
141 return ret;
143 *poutbuf = av_malloc(pkt.size + AV_INPUT_BUFFER_PADDING_SIZE);
144 if (!*poutbuf) {
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 */
155 while (ret >= 0) {
156 ret = av_bsf_receive_packet(priv->ctx, &pkt);
157 av_packet_unref(&pkt);
160 return 1;
162 FF_ENABLE_DEPRECATION_WARNINGS
163 #endif