avcodec/bsf: Avoid allocation for AVBSFInternal

Do this by allocating AVBSFContext together with the data that is
currently in AVBSFInternal; or rather: Put AVBSFContext at the beginning
of a new structure called FFBSFContext (which encompasses more than just
the internal fields and is a proper context in its own right, hence the
name) and remove the AVBSFInternal altogether.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2021-08-05 05:13:32 +02:00
parent 52bd399972
commit afeefb306e
2 changed files with 24 additions and 34 deletions

View File

@ -32,29 +32,35 @@
#define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems) #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems)
struct AVBSFInternal { typedef struct FFBSFContext {
AVBSFContext pub;
AVPacket *buffer_pkt; AVPacket *buffer_pkt;
int eof; int eof;
}; } FFBSFContext;
static av_always_inline FFBSFContext *ffbsfcontext(AVBSFContext *ctx)
{
return (FFBSFContext *)ctx;
}
void av_bsf_free(AVBSFContext **pctx) void av_bsf_free(AVBSFContext **pctx)
{ {
AVBSFContext *ctx; AVBSFContext *ctx;
FFBSFContext *bsfi;
if (!pctx || !*pctx) if (!pctx || !*pctx)
return; return;
ctx = *pctx; ctx = *pctx;
bsfi = ffbsfcontext(ctx);
if (ctx->internal) { if (ctx->priv_data) {
if (ctx->filter->close) if (ctx->filter->close)
ctx->filter->close(ctx); ctx->filter->close(ctx);
av_packet_free(&ctx->internal->buffer_pkt); if (ctx->filter->priv_class)
av_freep(&ctx->internal); av_opt_free(ctx->priv_data);
av_freep(&ctx->priv_data);
} }
if (ctx->filter->priv_class && ctx->priv_data) av_packet_free(&bsfi->buffer_pkt);
av_opt_free(ctx->priv_data);
av_freep(&ctx->priv_data);
avcodec_parameters_free(&ctx->par_in); avcodec_parameters_free(&ctx->par_in);
avcodec_parameters_free(&ctx->par_out); avcodec_parameters_free(&ctx->par_out);
@ -92,12 +98,13 @@ const AVClass *av_bsf_get_class(void)
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx) int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
{ {
AVBSFContext *ctx; AVBSFContext *ctx;
AVBSFInternal *bsfi; FFBSFContext *bsfi;
int ret; int ret;
ctx = av_mallocz(sizeof(*ctx)); bsfi = av_mallocz(sizeof(*bsfi));
if (!ctx) if (!bsfi)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
ctx = &bsfi->pub;
ctx->av_class = &bsf_class; ctx->av_class = &bsf_class;
ctx->filter = filter; ctx->filter = filter;
@ -120,15 +127,6 @@ int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
av_opt_set_defaults(ctx->priv_data); av_opt_set_defaults(ctx->priv_data);
} }
} }
/* Allocate AVBSFInternal; must happen after priv_data has been allocated
* so that a filter->close needing priv_data is never called without. */
bsfi = av_mallocz(sizeof(*bsfi));
if (!bsfi) {
ret = AVERROR(ENOMEM);
goto fail;
}
ctx->internal = bsfi;
bsfi->buffer_pkt = av_packet_alloc(); bsfi->buffer_pkt = av_packet_alloc();
if (!bsfi->buffer_pkt) { if (!bsfi->buffer_pkt) {
ret = AVERROR(ENOMEM); ret = AVERROR(ENOMEM);
@ -185,7 +183,7 @@ int av_bsf_init(AVBSFContext *ctx)
void av_bsf_flush(AVBSFContext *ctx) void av_bsf_flush(AVBSFContext *ctx)
{ {
AVBSFInternal *bsfi = ctx->internal; FFBSFContext *const bsfi = ffbsfcontext(ctx);
bsfi->eof = 0; bsfi->eof = 0;
@ -197,7 +195,7 @@ void av_bsf_flush(AVBSFContext *ctx)
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt) int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
{ {
AVBSFInternal *bsfi = ctx->internal; FFBSFContext *const bsfi = ffbsfcontext(ctx);
int ret; int ret;
if (!pkt || IS_EMPTY(pkt)) { if (!pkt || IS_EMPTY(pkt)) {
@ -228,7 +226,7 @@ int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt) int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
{ {
AVBSFInternal *bsfi = ctx->internal; FFBSFContext *const bsfi = ffbsfcontext(ctx);
AVPacket *tmp_pkt; AVPacket *tmp_pkt;
if (bsfi->eof) if (bsfi->eof)
@ -249,7 +247,7 @@ int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt) int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
{ {
AVBSFInternal *bsfi = ctx->internal; FFBSFContext *const bsfi = ffbsfcontext(ctx);
if (bsfi->eof) if (bsfi->eof)
return AVERROR_EOF; return AVERROR_EOF;

View File

@ -34,8 +34,6 @@
* @{ * @{
*/ */
typedef struct AVBSFInternal AVBSFInternal;
/** /**
* The bitstream filter state. * The bitstream filter state.
* *
@ -57,12 +55,6 @@ typedef struct AVBSFContext {
*/ */
const struct AVBitStreamFilter *filter; const struct AVBitStreamFilter *filter;
/**
* Opaque libavcodec internal data. Must not be touched by the caller in any
* way.
*/
AVBSFInternal *internal;
/** /**
* Opaque filter-specific private data. If filter->priv_class is non-NULL, * Opaque filter-specific private data. If filter->priv_class is non-NULL,
* this is an AVOptions-enabled struct. * this is an AVOptions-enabled struct.