lavc: add a new bitstream filtering API
[libav.git] / libavcodec / bitstream_filter.c
1 /*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of Libav.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <string.h>
22
23 #include "avcodec.h"
24
25 #include "libavutil/internal.h"
26 #include "libavutil/mem.h"
27
28 #if FF_API_OLD_BSF
29 FF_DISABLE_DEPRECATION_WARNINGS
30
31 AVBitStreamFilter *av_bitstream_filter_next(const AVBitStreamFilter *f)
32 {
33 const AVBitStreamFilter *filter = NULL;
34 void *opaque = NULL;
35
36 while (filter != f)
37 filter = av_bsf_next(&opaque);
38
39 return av_bsf_next(&opaque);
40 }
41
42 void av_register_bitstream_filter(AVBitStreamFilter *bsf)
43 {
44 }
45
46 typedef struct BSFCompatContext {
47 AVBSFContext *ctx;
48 } BSFCompatContext;
49
50 AVBitStreamFilterContext *av_bitstream_filter_init(const char *name)
51 {
52 AVBitStreamFilterContext *ctx = NULL;
53 BSFCompatContext *priv = NULL;
54 const AVBitStreamFilter *bsf;
55
56 bsf = av_bsf_get_by_name(name);
57 if (!bsf)
58 return NULL;
59
60 ctx = av_mallocz(sizeof(*ctx));
61 if (!ctx)
62 return NULL;
63
64 priv = av_mallocz(sizeof(*priv));
65 if (!priv)
66 goto fail;
67
68
69 ctx->filter = bsf;
70 ctx->priv_data = priv;
71
72 return ctx;
73
74 fail:
75 if (priv)
76 av_bsf_free(&priv->ctx);
77 av_freep(&priv);
78 av_freep(&ctx);
79 return NULL;
80 }
81
82 void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc)
83 {
84 BSFCompatContext *priv = bsfc->priv_data;
85
86 av_bsf_free(&priv->ctx);
87 av_freep(&bsfc->priv_data);
88 av_free(bsfc);
89 }
90
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)
95 {
96 BSFCompatContext *priv = bsfc->priv_data;
97 AVPacket pkt = { 0 };
98 int ret;
99
100 if (!priv->ctx) {
101 ret = av_bsf_alloc(bsfc->filter, &priv->ctx);
102 if (ret < 0)
103 return ret;
104
105 ret = avcodec_parameters_from_context(priv->ctx->par_in, avctx);
106 if (ret < 0)
107 return ret;
108
109 priv->ctx->time_base_in = avctx->time_base;
110
111 ret = av_bsf_init(priv->ctx);
112 if (ret < 0)
113 return ret;
114
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;
124 }
125 }
126
127 pkt.data = buf;
128 pkt.size = buf_size;
129
130 ret = av_bsf_send_packet(priv->ctx, &pkt);
131 if (ret < 0)
132 return ret;
133
134 *poutbuf = NULL;
135 *poutbuf_size = 0;
136
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;
142
143 *poutbuf = av_malloc(pkt.size + AV_INPUT_BUFFER_PADDING_SIZE);
144 if (!*poutbuf) {
145 av_packet_unref(&pkt);
146 return AVERROR(ENOMEM);
147 }
148
149 *poutbuf_size = pkt.size;
150 memcpy(*poutbuf, pkt.data, pkt.size);
151
152 av_packet_unref(&pkt);
153
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);
158 }
159
160 return 1;
161 }
162 FF_ENABLE_DEPRECATION_WARNINGS
163 #endif