2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/log.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/avassert.h"
29 struct AVBSFInternal {
34 void av_bsf_free(AVBSFContext **pctx)
42 if (ctx->filter->close)
43 ctx->filter->close(ctx);
44 if (ctx->filter->priv_class && ctx->priv_data)
45 av_opt_free(ctx->priv_data);
49 av_packet_free(&ctx->internal->buffer_pkt);
50 av_freep(&ctx->internal);
51 av_freep(&ctx->priv_data);
53 avcodec_parameters_free(&ctx->par_in);
54 avcodec_parameters_free(&ctx->par_out);
59 static void *bsf_child_next(void *obj, void *prev)
61 AVBSFContext *ctx = obj;
62 if (!prev && ctx->filter->priv_class)
63 return ctx->priv_data;
67 static const AVClass bsf_class = {
68 .class_name = "AVBSFContext",
69 .item_name = av_default_item_name,
70 .version = LIBAVUTIL_VERSION_INT,
71 .child_next = bsf_child_next,
72 .child_class_next = ff_bsf_child_class_next,
75 const AVClass *av_bsf_get_class(void)
80 int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
85 ctx = av_mallocz(sizeof(*ctx));
87 return AVERROR(ENOMEM);
89 ctx->av_class = &bsf_class;
92 ctx->par_in = avcodec_parameters_alloc();
93 ctx->par_out = avcodec_parameters_alloc();
94 if (!ctx->par_in || !ctx->par_out) {
95 ret = AVERROR(ENOMEM);
99 ctx->internal = av_mallocz(sizeof(*ctx->internal));
100 if (!ctx->internal) {
101 ret = AVERROR(ENOMEM);
105 ctx->internal->buffer_pkt = av_packet_alloc();
106 if (!ctx->internal->buffer_pkt) {
107 ret = AVERROR(ENOMEM);
111 av_opt_set_defaults(ctx);
113 /* allocate priv data and init private options */
114 if (filter->priv_data_size) {
115 ctx->priv_data = av_mallocz(filter->priv_data_size);
116 if (!ctx->priv_data) {
117 ret = AVERROR(ENOMEM);
120 if (filter->priv_class) {
121 *(const AVClass **)ctx->priv_data = filter->priv_class;
122 av_opt_set_defaults(ctx->priv_data);
133 int av_bsf_init(AVBSFContext *ctx)
137 /* check that the codec is supported */
138 if (ctx->filter->codec_ids) {
139 for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
140 if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
142 if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
143 const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
144 av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
145 "bitstream filter '%s'. Supported codecs are: ",
146 desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
147 for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
148 desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
149 av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
150 desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
152 av_log(ctx, AV_LOG_ERROR, "\n");
153 return AVERROR(EINVAL);
157 /* initialize output parameters to be the same as input
158 * init below might overwrite that */
159 ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
163 ctx->time_base_out = ctx->time_base_in;
165 if (ctx->filter->init) {
166 ret = ctx->filter->init(ctx);
174 int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
177 ctx->internal->eof = 1;
181 av_assert0(pkt->data || pkt->side_data);
183 if (ctx->internal->eof) {
184 av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
185 return AVERROR(EINVAL);
188 if (ctx->internal->buffer_pkt->data ||
189 ctx->internal->buffer_pkt->side_data_elems)
190 return AVERROR(EAGAIN);
192 av_packet_move_ref(ctx->internal->buffer_pkt, pkt);
197 int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
199 return ctx->filter->filter(ctx, pkt);
202 int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
204 AVBSFInternal *in = ctx->internal;
210 if (!ctx->internal->buffer_pkt->data &&
211 !ctx->internal->buffer_pkt->side_data_elems)
212 return AVERROR(EAGAIN);
214 tmp_pkt = av_packet_alloc();
216 return AVERROR(ENOMEM);
218 *pkt = ctx->internal->buffer_pkt;
219 ctx->internal->buffer_pkt = tmp_pkt;