Line | Branch | Exec | Source |
---|---|---|---|
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 "config_components.h" | ||
22 | |||
23 | #include "libavutil/avassert.h" | ||
24 | #include "libavutil/log.h" | ||
25 | #include "libavutil/mem.h" | ||
26 | #include "libavutil/opt.h" | ||
27 | #include "libavutil/avstring.h" | ||
28 | #include "libavutil/bprint.h" | ||
29 | |||
30 | #include "bsf.h" | ||
31 | #include "bsf_internal.h" | ||
32 | #include "codec_desc.h" | ||
33 | #include "codec_par.h" | ||
34 | |||
35 | #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems) | ||
36 | |||
37 | 847851 | static av_always_inline const FFBitStreamFilter *ff_bsf(const AVBitStreamFilter *bsf) | |
38 | { | ||
39 | 847851 | return (const FFBitStreamFilter*)bsf; | |
40 | } | ||
41 | |||
42 | typedef struct FFBSFContext { | ||
43 | AVBSFContext pub; | ||
44 | AVPacket *buffer_pkt; | ||
45 | int eof; | ||
46 | } FFBSFContext; | ||
47 | |||
48 | 1237321 | static av_always_inline FFBSFContext *ffbsfcontext(AVBSFContext *ctx) | |
49 | { | ||
50 | 1237321 | return (FFBSFContext *)ctx; | |
51 | } | ||
52 | |||
53 | 75112 | void av_bsf_free(AVBSFContext **pctx) | |
54 | { | ||
55 | AVBSFContext *ctx; | ||
56 | FFBSFContext *bsfi; | ||
57 | |||
58 |
3/4✓ Branch 0 taken 75112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60421 times.
✓ Branch 3 taken 14691 times.
|
75112 | if (!pctx || !*pctx) |
59 | 60421 | return; | |
60 | 14691 | ctx = *pctx; | |
61 | 14691 | bsfi = ffbsfcontext(ctx); | |
62 | |||
63 |
2/2✓ Branch 0 taken 1326 times.
✓ Branch 1 taken 13365 times.
|
14691 | if (ctx->priv_data) { |
64 |
2/2✓ Branch 1 taken 1306 times.
✓ Branch 2 taken 20 times.
|
1326 | if (ff_bsf(ctx->filter)->close) |
65 | 1306 | ff_bsf(ctx->filter)->close(ctx); | |
66 |
2/2✓ Branch 0 taken 800 times.
✓ Branch 1 taken 526 times.
|
1326 | if (ctx->filter->priv_class) |
67 | 800 | av_opt_free(ctx->priv_data); | |
68 | 1326 | av_freep(&ctx->priv_data); | |
69 | } | ||
70 | 14691 | av_packet_free(&bsfi->buffer_pkt); | |
71 | |||
72 | 14691 | avcodec_parameters_free(&ctx->par_in); | |
73 | 14691 | avcodec_parameters_free(&ctx->par_out); | |
74 | |||
75 | 14691 | av_freep(pctx); | |
76 | } | ||
77 | |||
78 | ✗ | static void *bsf_child_next(void *obj, void *prev) | |
79 | { | ||
80 | ✗ | AVBSFContext *ctx = obj; | |
81 | ✗ | if (!prev && ctx->filter->priv_class) | |
82 | ✗ | return ctx->priv_data; | |
83 | ✗ | return NULL; | |
84 | } | ||
85 | |||
86 | 5 | static const char *bsf_to_name(void *bsf) | |
87 | { | ||
88 | 5 | return ((AVBSFContext *)bsf)->filter->name; | |
89 | } | ||
90 | |||
91 | static const AVClass bsf_class = { | ||
92 | .class_name = "AVBSFContext", | ||
93 | .item_name = bsf_to_name, | ||
94 | .version = LIBAVUTIL_VERSION_INT, | ||
95 | .child_next = bsf_child_next, | ||
96 | .child_class_iterate = ff_bsf_child_class_iterate, | ||
97 | .category = AV_CLASS_CATEGORY_BITSTREAM_FILTER, | ||
98 | }; | ||
99 | |||
100 | ✗ | const AVClass *av_bsf_get_class(void) | |
101 | { | ||
102 | ✗ | return &bsf_class; | |
103 | } | ||
104 | |||
105 | 14691 | int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx) | |
106 | { | ||
107 | AVBSFContext *ctx; | ||
108 | FFBSFContext *bsfi; | ||
109 | int ret; | ||
110 | |||
111 | 14691 | bsfi = av_mallocz(sizeof(*bsfi)); | |
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14691 times.
|
14691 | if (!bsfi) |
113 | ✗ | return AVERROR(ENOMEM); | |
114 | 14691 | ctx = &bsfi->pub; | |
115 | |||
116 | 14691 | ctx->av_class = &bsf_class; | |
117 | 14691 | ctx->filter = filter; | |
118 | |||
119 | 14691 | ctx->par_in = avcodec_parameters_alloc(); | |
120 | 14691 | ctx->par_out = avcodec_parameters_alloc(); | |
121 |
2/4✓ Branch 0 taken 14691 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14691 times.
|
14691 | if (!ctx->par_in || !ctx->par_out) { |
122 | ✗ | ret = AVERROR(ENOMEM); | |
123 | ✗ | goto fail; | |
124 | } | ||
125 | /* allocate priv data and init private options */ | ||
126 |
2/2✓ Branch 1 taken 1326 times.
✓ Branch 2 taken 13365 times.
|
14691 | if (ff_bsf(filter)->priv_data_size) { |
127 | 1326 | ctx->priv_data = av_mallocz(ff_bsf(filter)->priv_data_size); | |
128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1326 times.
|
1326 | if (!ctx->priv_data) { |
129 | ✗ | ret = AVERROR(ENOMEM); | |
130 | ✗ | goto fail; | |
131 | } | ||
132 |
2/2✓ Branch 0 taken 800 times.
✓ Branch 1 taken 526 times.
|
1326 | if (filter->priv_class) { |
133 | 800 | *(const AVClass **)ctx->priv_data = filter->priv_class; | |
134 | 800 | av_opt_set_defaults(ctx->priv_data); | |
135 | } | ||
136 | } | ||
137 | 14691 | bsfi->buffer_pkt = av_packet_alloc(); | |
138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14691 times.
|
14691 | if (!bsfi->buffer_pkt) { |
139 | ✗ | ret = AVERROR(ENOMEM); | |
140 | ✗ | goto fail; | |
141 | } | ||
142 | |||
143 | 14691 | *pctx = ctx; | |
144 | 14691 | return 0; | |
145 | ✗ | fail: | |
146 | ✗ | av_bsf_free(&ctx); | |
147 | ✗ | return ret; | |
148 | } | ||
149 | |||
150 | 14691 | int av_bsf_init(AVBSFContext *ctx) | |
151 | { | ||
152 | int ret, i; | ||
153 | |||
154 | /* check that the codec is supported */ | ||
155 |
2/2✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 13375 times.
|
14691 | if (ctx->filter->codec_ids) { |
156 |
1/2✓ Branch 0 taken 5053 times.
✗ Branch 1 not taken.
|
5053 | for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) |
157 |
2/2✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 3737 times.
|
5053 | if (ctx->par_in->codec_id == ctx->filter->codec_ids[i]) |
158 | 1316 | break; | |
159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1316 times.
|
1316 | if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) { |
160 | ✗ | const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id); | |
161 | ✗ | av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the " | |
162 | "bitstream filter '%s'. Supported codecs are: ", | ||
163 | ✗ | desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name); | |
164 | ✗ | for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) { | |
165 | ✗ | enum AVCodecID codec_id = ctx->filter->codec_ids[i]; | |
166 | ✗ | av_log(ctx, AV_LOG_ERROR, "%s (%d) ", | |
167 | avcodec_get_name(codec_id), codec_id); | ||
168 | } | ||
169 | ✗ | av_log(ctx, AV_LOG_ERROR, "\n"); | |
170 | ✗ | return AVERROR(EINVAL); | |
171 | } | ||
172 | } | ||
173 | |||
174 | /* initialize output parameters to be the same as input | ||
175 | * init below might overwrite that */ | ||
176 | 14691 | ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in); | |
177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14691 times.
|
14691 | if (ret < 0) |
178 | ✗ | return ret; | |
179 | |||
180 | 14691 | ctx->time_base_out = ctx->time_base_in; | |
181 | |||
182 |
2/2✓ Branch 1 taken 1316 times.
✓ Branch 2 taken 13375 times.
|
14691 | if (ff_bsf(ctx->filter)->init) { |
183 | 1316 | ret = ff_bsf(ctx->filter)->init(ctx); | |
184 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1316 times.
|
1316 | if (ret < 0) |
185 | ✗ | return ret; | |
186 | } | ||
187 | |||
188 | 14691 | return 0; | |
189 | } | ||
190 | |||
191 | 94 | void av_bsf_flush(AVBSFContext *ctx) | |
192 | { | ||
193 | 94 | FFBSFContext *const bsfi = ffbsfcontext(ctx); | |
194 | |||
195 | 94 | bsfi->eof = 0; | |
196 | |||
197 | 94 | av_packet_unref(bsfi->buffer_pkt); | |
198 | |||
199 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 94 times.
|
94 | if (ff_bsf(ctx->filter)->flush) |
200 | ✗ | ff_bsf(ctx->filter)->flush(ctx); | |
201 | 94 | } | |
202 | |||
203 | 409279 | int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt) | |
204 | { | ||
205 | 409279 | FFBSFContext *const bsfi = ffbsfcontext(ctx); | |
206 | int ret; | ||
207 | |||
208 |
5/6✓ Branch 0 taken 409082 times.
✓ Branch 1 taken 197 times.
✓ Branch 2 taken 6319 times.
✓ Branch 3 taken 402763 times.
✓ Branch 4 taken 6319 times.
✗ Branch 5 not taken.
|
409279 | if (!pkt || IS_EMPTY(pkt)) { |
209 |
2/2✓ Branch 0 taken 6319 times.
✓ Branch 1 taken 197 times.
|
6516 | if (pkt) |
210 | 6319 | av_packet_unref(pkt); | |
211 | 6516 | bsfi->eof = 1; | |
212 | 6516 | return 0; | |
213 | } | ||
214 | |||
215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 402763 times.
|
402763 | if (bsfi->eof) { |
216 | ✗ | av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n"); | |
217 | ✗ | return AVERROR(EINVAL); | |
218 | } | ||
219 | |||
220 |
3/4✓ Branch 0 taken 402696 times.
✓ Branch 1 taken 67 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 402696 times.
|
402763 | if (!IS_EMPTY(bsfi->buffer_pkt)) |
221 | 67 | return AVERROR(EAGAIN); | |
222 | |||
223 | 402696 | ret = av_packet_make_refcounted(pkt); | |
224 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 402696 times.
|
402696 | if (ret < 0) |
225 | ✗ | return ret; | |
226 | 402696 | av_packet_move_ref(bsfi->buffer_pkt, pkt); | |
227 | |||
228 | 402696 | return 0; | |
229 | } | ||
230 | |||
231 | 813101 | int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt) | |
232 | { | ||
233 | 813101 | return ff_bsf(ctx->filter)->filter(ctx, pkt); | |
234 | } | ||
235 | |||
236 | 899 | int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt) | |
237 | { | ||
238 | 899 | FFBSFContext *const bsfi = ffbsfcontext(ctx); | |
239 | AVPacket *tmp_pkt; | ||
240 | |||
241 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 891 times.
|
899 | if (bsfi->eof) |
242 | 8 | return AVERROR_EOF; | |
243 | |||
244 |
3/4✓ Branch 0 taken 445 times.
✓ Branch 1 taken 446 times.
✓ Branch 2 taken 445 times.
✗ Branch 3 not taken.
|
891 | if (IS_EMPTY(bsfi->buffer_pkt)) |
245 | 445 | return AVERROR(EAGAIN); | |
246 | |||
247 | 446 | tmp_pkt = av_packet_alloc(); | |
248 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 446 times.
|
446 | if (!tmp_pkt) |
249 | ✗ | return AVERROR(ENOMEM); | |
250 | |||
251 | 446 | *pkt = bsfi->buffer_pkt; | |
252 | 446 | bsfi->buffer_pkt = tmp_pkt; | |
253 | |||
254 | 446 | return 0; | |
255 | } | ||
256 | |||
257 | 812358 | int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt) | |
258 | { | ||
259 | 812358 | FFBSFContext *const bsfi = ffbsfcontext(ctx); | |
260 | |||
261 |
2/2✓ Branch 0 taken 6519 times.
✓ Branch 1 taken 805839 times.
|
812358 | if (bsfi->eof) |
262 | 6519 | return AVERROR_EOF; | |
263 | |||
264 |
3/4✓ Branch 0 taken 403591 times.
✓ Branch 1 taken 402248 times.
✓ Branch 2 taken 403591 times.
✗ Branch 3 not taken.
|
805839 | if (IS_EMPTY(bsfi->buffer_pkt)) |
265 | 403591 | return AVERROR(EAGAIN); | |
266 | |||
267 | 402248 | av_packet_move_ref(pkt, bsfi->buffer_pkt); | |
268 | |||
269 | 402248 | return 0; | |
270 | } | ||
271 | |||
272 | typedef struct BSFListContext { | ||
273 | const AVClass *class; | ||
274 | |||
275 | AVBSFContext **bsfs; | ||
276 | int nb_bsfs; | ||
277 | |||
278 | unsigned idx; // index of currently processed BSF | ||
279 | |||
280 | char * item_name; | ||
281 | } BSFListContext; | ||
282 | |||
283 | |||
284 | 3 | static int bsf_list_init(AVBSFContext *bsf) | |
285 | { | ||
286 | 3 | BSFListContext *lst = bsf->priv_data; | |
287 | int ret, i; | ||
288 | 3 | const AVCodecParameters *cod_par = bsf->par_in; | |
289 | 3 | AVRational tb = bsf->time_base_in; | |
290 | |||
291 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | for (i = 0; i < lst->nb_bsfs; ++i) { |
292 | 6 | ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par); | |
293 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
294 | ✗ | goto fail; | |
295 | |||
296 | 6 | lst->bsfs[i]->time_base_in = tb; | |
297 | |||
298 | 6 | ret = av_bsf_init(lst->bsfs[i]); | |
299 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
300 | ✗ | goto fail; | |
301 | |||
302 | 6 | cod_par = lst->bsfs[i]->par_out; | |
303 | 6 | tb = lst->bsfs[i]->time_base_out; | |
304 | } | ||
305 | |||
306 | 3 | bsf->time_base_out = tb; | |
307 | 3 | ret = avcodec_parameters_copy(bsf->par_out, cod_par); | |
308 | |||
309 | 3 | fail: | |
310 | 3 | return ret; | |
311 | } | ||
312 | |||
313 | 70 | static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out) | |
314 | { | ||
315 | 70 | BSFListContext *lst = bsf->priv_data; | |
316 | 70 | int ret, eof = 0; | |
317 | |||
318 |
1/2✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
|
70 | if (!lst->nb_bsfs) |
319 | ✗ | return ff_bsf_get_packet_ref(bsf, out); | |
320 | |||
321 | while (1) { | ||
322 | /* get a packet from the previous filter up the chain */ | ||
323 |
2/2✓ Branch 0 taken 131 times.
✓ Branch 1 taken 75 times.
|
206 | if (lst->idx) |
324 | 131 | ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out); | |
325 | else | ||
326 | 75 | ret = ff_bsf_get_packet_ref(bsf, out); | |
327 |
2/2✓ Branch 0 taken 101 times.
✓ Branch 1 taken 105 times.
|
206 | if (ret == AVERROR(EAGAIN)) { |
328 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 65 times.
|
101 | if (!lst->idx) |
329 | 36 | return ret; | |
330 | 65 | lst->idx--; | |
331 | 65 | continue; | |
332 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 93 times.
|
105 | } else if (ret == AVERROR_EOF) { |
333 | 12 | eof = 1; | |
334 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | } else if (ret < 0) |
335 | ✗ | return ret; | |
336 | |||
337 | /* send it to the next filter down the chain */ | ||
338 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 34 times.
|
105 | if (lst->idx < lst->nb_bsfs) { |
339 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 6 times.
|
71 | ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out); |
340 | av_assert1(ret != AVERROR(EAGAIN)); | ||
341 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (ret < 0) { |
342 | ✗ | av_packet_unref(out); | |
343 | ✗ | return ret; | |
344 | } | ||
345 | 71 | lst->idx++; | |
346 | 71 | eof = 0; | |
347 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 28 times.
|
34 | } else if (eof) { |
348 | 6 | return ret; | |
349 | } else { | ||
350 | 28 | return 0; | |
351 | } | ||
352 | } | ||
353 | } | ||
354 | |||
355 | ✗ | static void bsf_list_flush(AVBSFContext *bsf) | |
356 | { | ||
357 | ✗ | BSFListContext *lst = bsf->priv_data; | |
358 | |||
359 | ✗ | for (int i = 0; i < lst->nb_bsfs; i++) | |
360 | ✗ | av_bsf_flush(lst->bsfs[i]); | |
361 | ✗ | lst->idx = 0; | |
362 | } | ||
363 | |||
364 | 3 | static void bsf_list_close(AVBSFContext *bsf) | |
365 | { | ||
366 | 3 | BSFListContext *lst = bsf->priv_data; | |
367 | int i; | ||
368 | |||
369 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | for (i = 0; i < lst->nb_bsfs; ++i) |
370 | 6 | av_bsf_free(&lst->bsfs[i]); | |
371 | 3 | av_freep(&lst->bsfs); | |
372 | 3 | av_freep(&lst->item_name); | |
373 | 3 | } | |
374 | |||
375 | ✗ | static const char *bsf_list_item_name(void *ctx) | |
376 | { | ||
377 | static const char *null_filter_name = "null"; | ||
378 | ✗ | AVBSFContext *bsf_ctx = ctx; | |
379 | ✗ | BSFListContext *lst = bsf_ctx->priv_data; | |
380 | |||
381 | ✗ | if (!lst->nb_bsfs) | |
382 | ✗ | return null_filter_name; | |
383 | |||
384 | ✗ | if (!lst->item_name) { | |
385 | int i; | ||
386 | AVBPrint bp; | ||
387 | ✗ | av_bprint_init(&bp, 16, 128); | |
388 | |||
389 | ✗ | av_bprintf(&bp, "bsf_list("); | |
390 | ✗ | for (i = 0; i < lst->nb_bsfs; i++) | |
391 | ✗ | av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name); | |
392 | ✗ | av_bprintf(&bp, ")"); | |
393 | |||
394 | ✗ | av_bprint_finalize(&bp, &lst->item_name); | |
395 | } | ||
396 | |||
397 | ✗ | return lst->item_name; | |
398 | } | ||
399 | |||
400 | static const AVClass bsf_list_class = { | ||
401 | .class_name = "bsf_list", | ||
402 | .item_name = bsf_list_item_name, | ||
403 | .version = LIBAVUTIL_VERSION_INT, | ||
404 | }; | ||
405 | |||
406 | static const FFBitStreamFilter list_bsf = { | ||
407 | .p.name = "bsf_list", | ||
408 | .p.priv_class = &bsf_list_class, | ||
409 | .priv_data_size = sizeof(BSFListContext), | ||
410 | .init = bsf_list_init, | ||
411 | .filter = bsf_list_filter, | ||
412 | .flush = bsf_list_flush, | ||
413 | .close = bsf_list_close, | ||
414 | }; | ||
415 | |||
416 | struct AVBSFList { | ||
417 | AVBSFContext **bsfs; | ||
418 | int nb_bsfs; | ||
419 | }; | ||
420 | |||
421 | 589 | AVBSFList *av_bsf_list_alloc(void) | |
422 | { | ||
423 | 589 | return av_mallocz(sizeof(AVBSFList)); | |
424 | } | ||
425 | |||
426 | ✗ | void av_bsf_list_free(AVBSFList **lst) | |
427 | { | ||
428 | int i; | ||
429 | |||
430 | ✗ | if (!*lst) | |
431 | ✗ | return; | |
432 | |||
433 | ✗ | for (i = 0; i < (*lst)->nb_bsfs; ++i) | |
434 | ✗ | av_bsf_free(&(*lst)->bsfs[i]); | |
435 | ✗ | av_free((*lst)->bsfs); | |
436 | ✗ | av_freep(lst); | |
437 | } | ||
438 | |||
439 | 592 | int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf) | |
440 | { | ||
441 | 592 | return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf); | |
442 | } | ||
443 | |||
444 | 592 | static int bsf_list_append_internal(AVBSFList *lst, const char *bsf_name, const char *options, AVDictionary ** options_dict) | |
445 | { | ||
446 | int ret; | ||
447 | const AVBitStreamFilter *filter; | ||
448 | AVBSFContext *bsf; | ||
449 | |||
450 | 592 | filter = av_bsf_get_by_name(bsf_name); | |
451 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
|
592 | if (!filter) |
452 | ✗ | return AVERROR_BSF_NOT_FOUND; | |
453 | |||
454 | 592 | ret = av_bsf_alloc(filter, &bsf); | |
455 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
|
592 | if (ret < 0) |
456 | ✗ | return ret; | |
457 | |||
458 |
3/4✓ Branch 0 taken 18 times.
✓ Branch 1 taken 574 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
|
592 | if (options && filter->priv_class) { |
459 | 18 | const AVOption *opt = av_opt_next(bsf->priv_data, NULL); | |
460 | 18 | const char * shorthand[2] = {NULL}; | |
461 | |||
462 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | if (opt) |
463 | 18 | shorthand[0] = opt->name; | |
464 | |||
465 | 18 | ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":"); | |
466 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (ret < 0) |
467 | ✗ | goto end; | |
468 | } | ||
469 | |||
470 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
|
592 | if (options_dict) { |
471 | ✗ | ret = av_opt_set_dict2(bsf, options_dict, AV_OPT_SEARCH_CHILDREN); | |
472 | ✗ | if (ret < 0) | |
473 | ✗ | goto end; | |
474 | } | ||
475 | |||
476 | 592 | ret = av_bsf_list_append(lst, bsf); | |
477 | |||
478 | 592 | end: | |
479 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
|
592 | if (ret < 0) |
480 | ✗ | av_bsf_free(&bsf); | |
481 | |||
482 | 592 | return ret; | |
483 | } | ||
484 | |||
485 | ✗ | int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options) | |
486 | { | ||
487 | ✗ | return bsf_list_append_internal(lst, bsf_name, NULL, options); | |
488 | } | ||
489 | |||
490 | 589 | int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf) | |
491 | { | ||
492 | 589 | int ret = 0; | |
493 | BSFListContext *ctx; | ||
494 | |||
495 |
2/2✓ Branch 0 taken 586 times.
✓ Branch 1 taken 3 times.
|
589 | if ((*lst)->nb_bsfs == 1) { |
496 | 586 | *bsf = (*lst)->bsfs[0]; | |
497 | 586 | av_freep(&(*lst)->bsfs); | |
498 | 586 | (*lst)->nb_bsfs = 0; | |
499 | 586 | goto end; | |
500 | } | ||
501 | |||
502 | 3 | ret = av_bsf_alloc(&list_bsf.p, bsf); | |
503 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
504 | ✗ | return ret; | |
505 | |||
506 | 3 | ctx = (*bsf)->priv_data; | |
507 | |||
508 | 3 | ctx->bsfs = (*lst)->bsfs; | |
509 | 3 | ctx->nb_bsfs = (*lst)->nb_bsfs; | |
510 | |||
511 | 589 | end: | |
512 | 589 | av_freep(lst); | |
513 | 589 | return ret; | |
514 | } | ||
515 | |||
516 | 592 | static int bsf_parse_single(char *str, AVBSFList *bsf_lst) | |
517 | { | ||
518 | char *bsf_name, *bsf_options_str; | ||
519 | |||
520 | 592 | bsf_name = av_strtok(str, "=", &bsf_options_str); | |
521 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
|
592 | if (!bsf_name) |
522 | ✗ | return AVERROR(EINVAL); | |
523 | |||
524 | 592 | return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL); | |
525 | } | ||
526 | |||
527 | 13952 | int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst) | |
528 | { | ||
529 | AVBSFList *lst; | ||
530 | int ret; | ||
531 | |||
532 |
2/2✓ Branch 0 taken 13363 times.
✓ Branch 1 taken 589 times.
|
13952 | if (!str) |
533 | 13363 | return av_bsf_get_null_filter(bsf_lst); | |
534 | |||
535 | 589 | lst = av_bsf_list_alloc(); | |
536 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 589 times.
|
589 | if (!lst) |
537 | ✗ | return AVERROR(ENOMEM); | |
538 | |||
539 | do { | ||
540 | 592 | char *bsf_str = av_get_token(&str, ","); | |
541 | 592 | ret = bsf_parse_single(bsf_str, lst); | |
542 | 592 | av_free(bsf_str); | |
543 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
|
592 | if (ret < 0) |
544 | ✗ | goto end; | |
545 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 589 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
592 | } while (*str && *++str); |
546 | |||
547 | 589 | ret = av_bsf_list_finalize(&lst, bsf_lst); | |
548 | 589 | end: | |
549 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 589 times.
|
589 | if (ret < 0) |
550 | ✗ | av_bsf_list_free(&lst); | |
551 | 589 | return ret; | |
552 | } | ||
553 | |||
554 | 13363 | int av_bsf_get_null_filter(AVBSFContext **bsf) | |
555 | { | ||
556 | #if CONFIG_NULL_BSF | ||
557 | extern const FFBitStreamFilter ff_null_bsf; | ||
558 | 13363 | return av_bsf_alloc(&ff_null_bsf.p, bsf); | |
559 | #else | ||
560 | return av_bsf_alloc(&list_bsf.p, bsf); | ||
561 | #endif | ||
562 | } | ||
563 |