2013-11-23 10:43:13 +00:00
|
|
|
/*
|
2016-04-17 17:47:25 +00:00
|
|
|
* This file is part of FFmpeg.
|
2013-11-23 10:43:13 +00:00
|
|
|
*
|
2016-04-17 17:47:25 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2013-11-23 10:43:13 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2016-04-17 17:47:25 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2013-11-23 10:43:13 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2016-04-17 17:47:25 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2013-11-23 10:43:13 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-02-23 12:56:49 +00:00
|
|
|
#include "config_components.h"
|
2021-09-06 17:39:19 +00:00
|
|
|
|
2020-03-29 23:31:43 +00:00
|
|
|
#include "libavutil/avassert.h"
|
2013-11-23 10:43:13 +00:00
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/mem.h"
|
|
|
|
#include "libavutil/opt.h"
|
2016-08-15 10:38:35 +00:00
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/bprint.h"
|
2013-11-23 10:43:13 +00:00
|
|
|
|
2020-04-07 14:41:48 +00:00
|
|
|
#include "bsf.h"
|
2020-04-07 14:49:28 +00:00
|
|
|
#include "bsf_internal.h"
|
2020-04-07 14:41:48 +00:00
|
|
|
#include "codec_desc.h"
|
|
|
|
#include "codec_par.h"
|
2023-06-24 11:10:08 +00:00
|
|
|
#include "packet_internal.h"
|
2020-04-19 19:18:51 +00:00
|
|
|
|
2022-03-21 21:25:27 +00:00
|
|
|
static av_always_inline const FFBitStreamFilter *ff_bsf(const AVBitStreamFilter *bsf)
|
|
|
|
{
|
|
|
|
return (const FFBitStreamFilter*)bsf;
|
|
|
|
}
|
|
|
|
|
2021-08-05 03:13:32 +00:00
|
|
|
typedef struct FFBSFContext {
|
|
|
|
AVBSFContext pub;
|
2013-11-23 10:43:13 +00:00
|
|
|
AVPacket *buffer_pkt;
|
|
|
|
int eof;
|
2021-08-05 03:13:32 +00:00
|
|
|
} FFBSFContext;
|
|
|
|
|
|
|
|
static av_always_inline FFBSFContext *ffbsfcontext(AVBSFContext *ctx)
|
|
|
|
{
|
|
|
|
return (FFBSFContext *)ctx;
|
|
|
|
}
|
2013-11-23 10:43:13 +00:00
|
|
|
|
|
|
|
void av_bsf_free(AVBSFContext **pctx)
|
|
|
|
{
|
|
|
|
AVBSFContext *ctx;
|
2021-08-05 03:13:32 +00:00
|
|
|
FFBSFContext *bsfi;
|
2013-11-23 10:43:13 +00:00
|
|
|
|
|
|
|
if (!pctx || !*pctx)
|
|
|
|
return;
|
2021-08-05 03:13:32 +00:00
|
|
|
ctx = *pctx;
|
|
|
|
bsfi = ffbsfcontext(ctx);
|
2013-11-23 10:43:13 +00:00
|
|
|
|
2021-08-05 03:13:32 +00:00
|
|
|
if (ctx->priv_data) {
|
2022-03-21 21:25:27 +00:00
|
|
|
if (ff_bsf(ctx->filter)->close)
|
|
|
|
ff_bsf(ctx->filter)->close(ctx);
|
2021-08-05 03:13:32 +00:00
|
|
|
if (ctx->filter->priv_class)
|
|
|
|
av_opt_free(ctx->priv_data);
|
|
|
|
av_freep(&ctx->priv_data);
|
2021-04-02 12:17:56 +00:00
|
|
|
}
|
2021-08-05 03:13:32 +00:00
|
|
|
av_packet_free(&bsfi->buffer_pkt);
|
2013-11-23 10:43:13 +00:00
|
|
|
|
|
|
|
avcodec_parameters_free(&ctx->par_in);
|
|
|
|
avcodec_parameters_free(&ctx->par_out);
|
|
|
|
|
|
|
|
av_freep(pctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *bsf_child_next(void *obj, void *prev)
|
|
|
|
{
|
|
|
|
AVBSFContext *ctx = obj;
|
|
|
|
if (!prev && ctx->filter->priv_class)
|
|
|
|
return ctx->priv_data;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:31:47 +00:00
|
|
|
static const char *bsf_to_name(void *bsf)
|
|
|
|
{
|
|
|
|
return ((AVBSFContext *)bsf)->filter->name;
|
|
|
|
}
|
|
|
|
|
2013-11-23 10:43:13 +00:00
|
|
|
static const AVClass bsf_class = {
|
|
|
|
.class_name = "AVBSFContext",
|
2020-03-17 21:31:47 +00:00
|
|
|
.item_name = bsf_to_name,
|
2013-11-23 10:43:13 +00:00
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
.child_next = bsf_child_next,
|
2020-05-27 13:34:07 +00:00
|
|
|
.child_class_iterate = ff_bsf_child_class_iterate,
|
2020-03-17 21:31:47 +00:00
|
|
|
.category = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
|
2013-11-23 10:43:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const AVClass *av_bsf_get_class(void)
|
|
|
|
{
|
|
|
|
return &bsf_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
|
|
|
|
{
|
|
|
|
AVBSFContext *ctx;
|
2021-08-05 03:13:32 +00:00
|
|
|
FFBSFContext *bsfi;
|
2013-11-23 10:43:13 +00:00
|
|
|
int ret;
|
|
|
|
|
2021-08-05 03:13:32 +00:00
|
|
|
bsfi = av_mallocz(sizeof(*bsfi));
|
|
|
|
if (!bsfi)
|
2013-11-23 10:43:13 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2021-08-05 03:13:32 +00:00
|
|
|
ctx = &bsfi->pub;
|
2013-11-23 10:43:13 +00:00
|
|
|
|
|
|
|
ctx->av_class = &bsf_class;
|
|
|
|
ctx->filter = filter;
|
|
|
|
|
|
|
|
ctx->par_in = avcodec_parameters_alloc();
|
|
|
|
ctx->par_out = avcodec_parameters_alloc();
|
|
|
|
if (!ctx->par_in || !ctx->par_out) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* allocate priv data and init private options */
|
2022-03-21 21:25:27 +00:00
|
|
|
if (ff_bsf(filter)->priv_data_size) {
|
|
|
|
ctx->priv_data = av_mallocz(ff_bsf(filter)->priv_data_size);
|
2013-11-23 10:43:13 +00:00
|
|
|
if (!ctx->priv_data) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (filter->priv_class) {
|
|
|
|
*(const AVClass **)ctx->priv_data = filter->priv_class;
|
|
|
|
av_opt_set_defaults(ctx->priv_data);
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 12:17:56 +00:00
|
|
|
bsfi->buffer_pkt = av_packet_alloc();
|
|
|
|
if (!bsfi->buffer_pkt) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-11-23 10:43:13 +00:00
|
|
|
|
|
|
|
*pctx = ctx;
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
av_bsf_free(&ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_bsf_init(AVBSFContext *ctx)
|
|
|
|
{
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
/* check that the codec is supported */
|
|
|
|
if (ctx->filter->codec_ids) {
|
|
|
|
for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
|
|
|
|
if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
|
|
|
|
break;
|
|
|
|
if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
|
|
|
|
const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
|
|
|
|
"bitstream filter '%s'. Supported codecs are: ",
|
|
|
|
desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
|
|
|
|
for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
|
2021-04-02 13:21:51 +00:00
|
|
|
enum AVCodecID codec_id = ctx->filter->codec_ids[i];
|
2013-11-23 10:43:13 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
|
2021-04-02 13:21:51 +00:00
|
|
|
avcodec_get_name(codec_id), codec_id);
|
2013-11-23 10:43:13 +00:00
|
|
|
}
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize output parameters to be the same as input
|
|
|
|
* init below might overwrite that */
|
|
|
|
ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ctx->time_base_out = ctx->time_base_in;
|
|
|
|
|
2022-03-21 21:25:27 +00:00
|
|
|
if (ff_bsf(ctx->filter)->init) {
|
|
|
|
ret = ff_bsf(ctx->filter)->init(ctx);
|
2013-11-23 10:43:13 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-26 23:32:13 +00:00
|
|
|
void av_bsf_flush(AVBSFContext *ctx)
|
|
|
|
{
|
2021-08-05 03:13:32 +00:00
|
|
|
FFBSFContext *const bsfi = ffbsfcontext(ctx);
|
2018-07-26 23:32:13 +00:00
|
|
|
|
2019-12-16 05:01:49 +00:00
|
|
|
bsfi->eof = 0;
|
|
|
|
|
|
|
|
av_packet_unref(bsfi->buffer_pkt);
|
2018-07-26 23:32:13 +00:00
|
|
|
|
2022-03-21 21:25:27 +00:00
|
|
|
if (ff_bsf(ctx->filter)->flush)
|
|
|
|
ff_bsf(ctx->filter)->flush(ctx);
|
2018-07-26 23:32:13 +00:00
|
|
|
}
|
|
|
|
|
2013-11-23 10:43:13 +00:00
|
|
|
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
|
|
|
|
{
|
2021-08-05 03:13:32 +00:00
|
|
|
FFBSFContext *const bsfi = ffbsfcontext(ctx);
|
2018-03-25 03:23:58 +00:00
|
|
|
int ret;
|
|
|
|
|
2023-06-24 11:10:08 +00:00
|
|
|
if (!pkt || AVPACKET_IS_EMPTY(pkt)) {
|
2021-09-06 20:31:38 +00:00
|
|
|
if (pkt)
|
|
|
|
av_packet_unref(pkt);
|
2019-12-16 05:01:49 +00:00
|
|
|
bsfi->eof = 1;
|
2013-11-23 10:43:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-16 05:01:49 +00:00
|
|
|
if (bsfi->eof) {
|
2013-11-23 10:43:13 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2023-06-24 11:10:08 +00:00
|
|
|
if (!AVPACKET_IS_EMPTY(bsfi->buffer_pkt))
|
2013-11-23 10:43:13 +00:00
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
|
2018-03-25 03:23:58 +00:00
|
|
|
ret = av_packet_make_refcounted(pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2019-12-16 05:01:49 +00:00
|
|
|
av_packet_move_ref(bsfi->buffer_pkt, pkt);
|
2013-11-23 10:43:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
|
|
|
|
{
|
2022-03-21 21:25:27 +00:00
|
|
|
return ff_bsf(ctx->filter)->filter(ctx, pkt);
|
2013-11-23 10:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
|
|
|
|
{
|
2021-08-05 03:13:32 +00:00
|
|
|
FFBSFContext *const bsfi = ffbsfcontext(ctx);
|
2013-11-23 10:43:13 +00:00
|
|
|
AVPacket *tmp_pkt;
|
|
|
|
|
2019-12-16 05:01:49 +00:00
|
|
|
if (bsfi->eof)
|
2013-11-23 10:43:13 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2023-06-24 11:10:08 +00:00
|
|
|
if (AVPACKET_IS_EMPTY(bsfi->buffer_pkt))
|
2013-11-23 10:43:13 +00:00
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
|
|
|
|
tmp_pkt = av_packet_alloc();
|
|
|
|
if (!tmp_pkt)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2019-12-16 05:01:49 +00:00
|
|
|
*pkt = bsfi->buffer_pkt;
|
|
|
|
bsfi->buffer_pkt = tmp_pkt;
|
2013-11-23 10:43:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-28 16:18:13 +00:00
|
|
|
|
|
|
|
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
|
|
|
|
{
|
2021-08-05 03:13:32 +00:00
|
|
|
FFBSFContext *const bsfi = ffbsfcontext(ctx);
|
2016-07-28 16:18:13 +00:00
|
|
|
|
2019-12-16 05:01:49 +00:00
|
|
|
if (bsfi->eof)
|
2016-07-28 16:18:13 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2023-06-24 11:10:08 +00:00
|
|
|
if (AVPACKET_IS_EMPTY(bsfi->buffer_pkt))
|
2016-07-28 16:18:13 +00:00
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
|
2019-12-16 05:01:49 +00:00
|
|
|
av_packet_move_ref(pkt, bsfi->buffer_pkt);
|
2016-07-28 16:18:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-08-15 10:38:35 +00:00
|
|
|
|
|
|
|
typedef struct BSFListContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
AVBSFContext **bsfs;
|
|
|
|
int nb_bsfs;
|
|
|
|
|
|
|
|
unsigned idx; // index of currently processed BSF
|
|
|
|
|
2016-08-21 08:36:12 +00:00
|
|
|
char * item_name;
|
2016-08-15 10:38:35 +00:00
|
|
|
} BSFListContext;
|
|
|
|
|
|
|
|
|
|
|
|
static int bsf_list_init(AVBSFContext *bsf)
|
|
|
|
{
|
|
|
|
BSFListContext *lst = bsf->priv_data;
|
|
|
|
int ret, i;
|
|
|
|
const AVCodecParameters *cod_par = bsf->par_in;
|
|
|
|
AVRational tb = bsf->time_base_in;
|
|
|
|
|
|
|
|
for (i = 0; i < lst->nb_bsfs; ++i) {
|
|
|
|
ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
lst->bsfs[i]->time_base_in = tb;
|
|
|
|
|
|
|
|
ret = av_bsf_init(lst->bsfs[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
cod_par = lst->bsfs[i]->par_out;
|
|
|
|
tb = lst->bsfs[i]->time_base_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bsf->time_base_out = tb;
|
|
|
|
ret = avcodec_parameters_copy(bsf->par_out, cod_par);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
|
|
|
|
{
|
|
|
|
BSFListContext *lst = bsf->priv_data;
|
2020-03-29 23:31:43 +00:00
|
|
|
int ret, eof = 0;
|
2016-08-15 10:38:35 +00:00
|
|
|
|
|
|
|
if (!lst->nb_bsfs)
|
|
|
|
return ff_bsf_get_packet_ref(bsf, out);
|
|
|
|
|
|
|
|
while (1) {
|
2020-03-29 23:31:43 +00:00
|
|
|
/* get a packet from the previous filter up the chain */
|
|
|
|
if (lst->idx)
|
2016-08-15 10:38:35 +00:00
|
|
|
ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
|
2020-03-29 23:31:43 +00:00
|
|
|
else
|
2016-08-15 10:38:35 +00:00
|
|
|
ret = ff_bsf_get_packet_ref(bsf, out);
|
2020-03-29 23:31:43 +00:00
|
|
|
if (ret == AVERROR(EAGAIN)) {
|
|
|
|
if (!lst->idx)
|
|
|
|
return ret;
|
|
|
|
lst->idx--;
|
|
|
|
continue;
|
|
|
|
} else if (ret == AVERROR_EOF) {
|
|
|
|
eof = 1;
|
|
|
|
} else if (ret < 0)
|
|
|
|
return ret;
|
2016-08-15 10:38:35 +00:00
|
|
|
|
2020-03-29 23:31:43 +00:00
|
|
|
/* send it to the next filter down the chain */
|
2016-08-15 10:38:35 +00:00
|
|
|
if (lst->idx < lst->nb_bsfs) {
|
2020-03-29 23:31:43 +00:00
|
|
|
ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out);
|
|
|
|
av_assert1(ret != AVERROR(EAGAIN));
|
|
|
|
if (ret < 0) {
|
|
|
|
av_packet_unref(out);
|
|
|
|
return ret;
|
2016-08-15 10:38:35 +00:00
|
|
|
}
|
|
|
|
lst->idx++;
|
2020-03-29 23:31:43 +00:00
|
|
|
eof = 0;
|
|
|
|
} else if (eof) {
|
|
|
|
return ret;
|
2016-08-15 10:38:35 +00:00
|
|
|
} else {
|
2020-03-29 23:31:43 +00:00
|
|
|
return 0;
|
2016-08-15 10:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 03:31:47 +00:00
|
|
|
static void bsf_list_flush(AVBSFContext *bsf)
|
|
|
|
{
|
|
|
|
BSFListContext *lst = bsf->priv_data;
|
|
|
|
|
|
|
|
for (int i = 0; i < lst->nb_bsfs; i++)
|
|
|
|
av_bsf_flush(lst->bsfs[i]);
|
2020-03-29 23:31:43 +00:00
|
|
|
lst->idx = 0;
|
2019-01-06 03:31:47 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 10:38:35 +00:00
|
|
|
static void bsf_list_close(AVBSFContext *bsf)
|
|
|
|
{
|
|
|
|
BSFListContext *lst = bsf->priv_data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < lst->nb_bsfs; ++i)
|
|
|
|
av_bsf_free(&lst->bsfs[i]);
|
|
|
|
av_freep(&lst->bsfs);
|
2016-08-21 08:36:12 +00:00
|
|
|
av_freep(&lst->item_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *bsf_list_item_name(void *ctx)
|
|
|
|
{
|
|
|
|
static const char *null_filter_name = "null";
|
|
|
|
AVBSFContext *bsf_ctx = ctx;
|
|
|
|
BSFListContext *lst = bsf_ctx->priv_data;
|
|
|
|
|
|
|
|
if (!lst->nb_bsfs)
|
|
|
|
return null_filter_name;
|
|
|
|
|
|
|
|
if (!lst->item_name) {
|
|
|
|
int i;
|
|
|
|
AVBPrint bp;
|
|
|
|
av_bprint_init(&bp, 16, 128);
|
|
|
|
|
|
|
|
av_bprintf(&bp, "bsf_list(");
|
|
|
|
for (i = 0; i < lst->nb_bsfs; i++)
|
|
|
|
av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
|
|
|
|
av_bprintf(&bp, ")");
|
|
|
|
|
|
|
|
av_bprint_finalize(&bp, &lst->item_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return lst->item_name;
|
2016-08-15 10:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass bsf_list_class = {
|
|
|
|
.class_name = "bsf_list",
|
2016-08-21 08:36:12 +00:00
|
|
|
.item_name = bsf_list_item_name,
|
2016-08-15 10:38:35 +00:00
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2022-03-21 21:25:27 +00:00
|
|
|
static const FFBitStreamFilter list_bsf = {
|
|
|
|
.p.name = "bsf_list",
|
|
|
|
.p.priv_class = &bsf_list_class,
|
2016-08-15 10:38:35 +00:00
|
|
|
.priv_data_size = sizeof(BSFListContext),
|
|
|
|
.init = bsf_list_init,
|
|
|
|
.filter = bsf_list_filter,
|
2019-01-06 03:31:47 +00:00
|
|
|
.flush = bsf_list_flush,
|
2016-08-15 10:38:35 +00:00
|
|
|
.close = bsf_list_close,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AVBSFList {
|
|
|
|
AVBSFContext **bsfs;
|
|
|
|
int nb_bsfs;
|
|
|
|
};
|
|
|
|
|
|
|
|
AVBSFList *av_bsf_list_alloc(void)
|
|
|
|
{
|
|
|
|
return av_mallocz(sizeof(AVBSFList));
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_bsf_list_free(AVBSFList **lst)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-12-26 22:45:53 +00:00
|
|
|
if (!*lst)
|
2016-08-15 10:38:35 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < (*lst)->nb_bsfs; ++i)
|
|
|
|
av_bsf_free(&(*lst)->bsfs[i]);
|
|
|
|
av_free((*lst)->bsfs);
|
|
|
|
av_freep(lst);
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
|
|
|
|
{
|
|
|
|
return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:58:18 +00:00
|
|
|
static int bsf_list_append_internal(AVBSFList *lst, const char *bsf_name, const char *options, AVDictionary ** options_dict)
|
2016-08-15 10:38:35 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const AVBitStreamFilter *filter;
|
|
|
|
AVBSFContext *bsf;
|
|
|
|
|
|
|
|
filter = av_bsf_get_by_name(bsf_name);
|
|
|
|
if (!filter)
|
|
|
|
return AVERROR_BSF_NOT_FOUND;
|
|
|
|
|
|
|
|
ret = av_bsf_alloc(filter, &bsf);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2020-04-17 22:58:18 +00:00
|
|
|
if (options && filter->priv_class) {
|
|
|
|
const AVOption *opt = av_opt_next(bsf->priv_data, NULL);
|
|
|
|
const char * shorthand[2] = {NULL};
|
|
|
|
|
|
|
|
if (opt)
|
|
|
|
shorthand[0] = opt->name;
|
|
|
|
|
|
|
|
ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":");
|
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options_dict) {
|
|
|
|
ret = av_opt_set_dict2(bsf, options_dict, AV_OPT_SEARCH_CHILDREN);
|
2016-08-15 10:38:35 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_bsf_list_append(lst, bsf);
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (ret < 0)
|
|
|
|
av_bsf_free(&bsf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:58:18 +00:00
|
|
|
int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
|
|
|
|
{
|
|
|
|
return bsf_list_append_internal(lst, bsf_name, NULL, options);
|
|
|
|
}
|
|
|
|
|
2016-08-15 10:38:35 +00:00
|
|
|
int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
BSFListContext *ctx;
|
|
|
|
|
|
|
|
if ((*lst)->nb_bsfs == 1) {
|
|
|
|
*bsf = (*lst)->bsfs[0];
|
|
|
|
av_freep(&(*lst)->bsfs);
|
|
|
|
(*lst)->nb_bsfs = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-03-21 21:25:27 +00:00
|
|
|
ret = av_bsf_alloc(&list_bsf.p, bsf);
|
2016-08-15 10:38:35 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ctx = (*bsf)->priv_data;
|
|
|
|
|
|
|
|
ctx->bsfs = (*lst)->bsfs;
|
|
|
|
ctx->nb_bsfs = (*lst)->nb_bsfs;
|
|
|
|
|
|
|
|
end:
|
|
|
|
av_freep(lst);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:58:18 +00:00
|
|
|
static int bsf_parse_single(char *str, AVBSFList *bsf_lst)
|
2016-08-15 10:38:35 +00:00
|
|
|
{
|
2020-04-17 22:58:18 +00:00
|
|
|
char *bsf_name, *bsf_options_str;
|
2016-08-15 10:38:35 +00:00
|
|
|
|
2020-04-17 22:58:18 +00:00
|
|
|
bsf_name = av_strtok(str, "=", &bsf_options_str);
|
|
|
|
if (!bsf_name)
|
|
|
|
return AVERROR(EINVAL);
|
2016-08-15 10:38:35 +00:00
|
|
|
|
2020-04-17 22:58:18 +00:00
|
|
|
return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL);
|
2016-08-15 10:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
|
|
|
|
{
|
|
|
|
AVBSFList *lst;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return av_bsf_get_null_filter(bsf_lst);
|
|
|
|
|
|
|
|
lst = av_bsf_list_alloc();
|
|
|
|
if (!lst)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2021-07-03 11:32:59 +00:00
|
|
|
do {
|
|
|
|
char *bsf_str = av_get_token(&str, ",");
|
2016-08-15 10:38:35 +00:00
|
|
|
ret = bsf_parse_single(bsf_str, lst);
|
2021-07-03 11:32:59 +00:00
|
|
|
av_free(bsf_str);
|
2016-08-15 10:38:35 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
2021-07-03 11:32:59 +00:00
|
|
|
} while (*str && *++str);
|
2016-08-15 10:38:35 +00:00
|
|
|
|
|
|
|
ret = av_bsf_list_finalize(&lst, bsf_lst);
|
|
|
|
end:
|
|
|
|
if (ret < 0)
|
|
|
|
av_bsf_list_free(&lst);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_bsf_get_null_filter(AVBSFContext **bsf)
|
|
|
|
{
|
2021-09-06 17:39:19 +00:00
|
|
|
#if CONFIG_NULL_BSF
|
2022-03-21 21:25:27 +00:00
|
|
|
extern const FFBitStreamFilter ff_null_bsf;
|
|
|
|
return av_bsf_alloc(&ff_null_bsf.p, bsf);
|
2021-09-06 17:39:19 +00:00
|
|
|
#else
|
2022-03-21 21:25:27 +00:00
|
|
|
return av_bsf_alloc(&list_bsf.p, bsf);
|
2021-09-06 17:39:19 +00:00
|
|
|
#endif
|
2016-08-15 10:38:35 +00:00
|
|
|
}
|