]> git.sesse.net Git - ffmpeg/blob - libavcodec/bsf.c
avcodec/bsf: Forbid packet without payload in av_bsf_send_packet
[ffmpeg] / libavcodec / bsf.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <string.h>
20
21 #include "libavutil/log.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/avassert.h"
25
26 #include "avcodec.h"
27 #include "bsf.h"
28
29 struct AVBSFInternal {
30     AVPacket *buffer_pkt;
31     int eof;
32 };
33
34 void av_bsf_free(AVBSFContext **pctx)
35 {
36     AVBSFContext *ctx;
37
38     if (!pctx || !*pctx)
39         return;
40     ctx = *pctx;
41
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);
46
47     av_opt_free(ctx);
48
49     av_packet_free(&ctx->internal->buffer_pkt);
50     av_freep(&ctx->internal);
51     av_freep(&ctx->priv_data);
52
53     avcodec_parameters_free(&ctx->par_in);
54     avcodec_parameters_free(&ctx->par_out);
55
56     av_freep(pctx);
57 }
58
59 static void *bsf_child_next(void *obj, void *prev)
60 {
61     AVBSFContext *ctx = obj;
62     if (!prev && ctx->filter->priv_class)
63         return ctx->priv_data;
64     return NULL;
65 }
66
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,
73 };
74
75 const AVClass *av_bsf_get_class(void)
76 {
77     return &bsf_class;
78 }
79
80 int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
81 {
82     AVBSFContext *ctx;
83     int ret;
84
85     ctx = av_mallocz(sizeof(*ctx));
86     if (!ctx)
87         return AVERROR(ENOMEM);
88
89     ctx->av_class = &bsf_class;
90     ctx->filter   = filter;
91
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);
96         goto fail;
97     }
98
99     ctx->internal = av_mallocz(sizeof(*ctx->internal));
100     if (!ctx->internal) {
101         ret = AVERROR(ENOMEM);
102         goto fail;
103     }
104
105     ctx->internal->buffer_pkt = av_packet_alloc();
106     if (!ctx->internal->buffer_pkt) {
107         ret = AVERROR(ENOMEM);
108         goto fail;
109     }
110
111     av_opt_set_defaults(ctx);
112
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);
118             goto fail;
119         }
120         if (filter->priv_class) {
121             *(const AVClass **)ctx->priv_data = filter->priv_class;
122             av_opt_set_defaults(ctx->priv_data);
123         }
124     }
125
126     *pctx = ctx;
127     return 0;
128 fail:
129     av_bsf_free(&ctx);
130     return ret;
131 }
132
133 int av_bsf_init(AVBSFContext *ctx)
134 {
135     int ret, i;
136
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])
141                 break;
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]);
151             }
152             av_log(ctx, AV_LOG_ERROR, "\n");
153             return AVERROR(EINVAL);
154         }
155     }
156
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);
160     if (ret < 0)
161         return ret;
162
163     ctx->time_base_out = ctx->time_base_in;
164
165     if (ctx->filter->init) {
166         ret = ctx->filter->init(ctx);
167         if (ret < 0)
168             return ret;
169     }
170
171     return 0;
172 }
173
174 int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
175 {
176     if (!pkt) {
177         ctx->internal->eof = 1;
178         return 0;
179     }
180
181     av_assert0(pkt->data || pkt->side_data);
182
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);
186     }
187
188     if (ctx->internal->buffer_pkt->data ||
189         ctx->internal->buffer_pkt->side_data_elems)
190         return AVERROR(EAGAIN);
191
192     av_packet_move_ref(ctx->internal->buffer_pkt, pkt);
193
194     return 0;
195 }
196
197 int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
198 {
199     return ctx->filter->filter(ctx, pkt);
200 }
201
202 int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
203 {
204     AVBSFInternal *in = ctx->internal;
205     AVPacket *tmp_pkt;
206
207     if (in->eof)
208         return AVERROR_EOF;
209
210     if (!ctx->internal->buffer_pkt->data &&
211         !ctx->internal->buffer_pkt->side_data_elems)
212         return AVERROR(EAGAIN);
213
214     tmp_pkt = av_packet_alloc();
215     if (!tmp_pkt)
216         return AVERROR(ENOMEM);
217
218     *pkt = ctx->internal->buffer_pkt;
219     ctx->internal->buffer_pkt = tmp_pkt;
220
221     return 0;
222 }