History

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.

Andreas Rheinhardt committed 5 years ago

Commit afeefb31 parent 52bd3992 files · +24 −34Browse files

Unified · Split

2 files
  1. libavcodec/bsf.c +24 −26
  2. libavcodec/bsf.h +0 −8
libavcodec/bsf.c+24−26
@@ -32,29 +32,35 @@ · 31 unmodified lines
3232
3333 #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems)
3434
35−struct AVBSFInternal {
35+typedef struct FFBSFContext {
36+ AVBSFContext pub;
3637 AVPacket *buffer_pkt;
3738 int eof;
38−};
39+} FFBSFContext;
40+
41+static av_always_inline FFBSFContext *ffbsfcontext(AVBSFContext *ctx)
42+{
43+ return (FFBSFContext *)ctx;
44+}
3945
4046 void av_bsf_free(AVBSFContext **pctx)
4147 {
4248 AVBSFContext *ctx;
49+ FFBSFContext *bsfi;
4350
4451 if (!pctx || !*pctx)
4552 return;
46− ctx = *pctx;
53+ ctx = *pctx;
54+ bsfi = ffbsfcontext(ctx);
4755
48− if (ctx->internal) {
56+ if (ctx->priv_data) {
4957 if (ctx->filter->close)
5058 ctx->filter->close(ctx);
51− av_packet_free(&ctx->internal->buffer_pkt);
52− av_freep(&ctx->internal);
59+ if (ctx->filter->priv_class)
60+ av_opt_free(ctx->priv_data);
61+ av_freep(&ctx->priv_data);
5362 }
54− if (ctx->filter->priv_class && ctx->priv_data)
55− av_opt_free(ctx->priv_data);
56−
57− av_freep(&ctx->priv_data);
63+ av_packet_free(&bsfi->buffer_pkt);
5864
5965 avcodec_parameters_free(&ctx->par_in);
6066 avcodec_parameters_free(&ctx->par_out);
@@ -92,12 +98,13 @@ const AVClass *av_bsf_get_class(void) · 31 unmodified lines
9298 int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
9399 {
94100 AVBSFContext *ctx;
95− AVBSFInternal *bsfi;
101+ FFBSFContext *bsfi;
96102 int ret;
97103
98− ctx = av_mallocz(sizeof(*ctx));
99− if (!ctx)
104+ bsfi = av_mallocz(sizeof(*bsfi));
105+ if (!bsfi)
100106 return AVERROR(ENOMEM);
107+ ctx = &bsfi->pub;
101108
102109 ctx->av_class = &bsf_class;
103110 ctx->filter = filter;
@@ -120,15 +127,6 @@ int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx) · 16 unmodified lines
120127 av_opt_set_defaults(ctx->priv_data);
121128 }
122129 }
123− /* Allocate AVBSFInternal; must happen after priv_data has been allocated
124− * so that a filter->close needing priv_data is never called without. */
125− bsfi = av_mallocz(sizeof(*bsfi));
126− if (!bsfi) {
127− ret = AVERROR(ENOMEM);
128− goto fail;
129− }
130− ctx->internal = bsfi;
131−
132130 bsfi->buffer_pkt = av_packet_alloc();
133131 if (!bsfi->buffer_pkt) {
134132 ret = AVERROR(ENOMEM);
@@ -185,7 +183,7 @@ int av_bsf_init(AVBSFContext *ctx) · 50 unmodified lines
185183
186184 void av_bsf_flush(AVBSFContext *ctx)
187185 {
188− AVBSFInternal *bsfi = ctx->internal;
186+ FFBSFContext *const bsfi = ffbsfcontext(ctx);
189187
190188 bsfi->eof = 0;
191189
@@ -197,7 +195,7 @@ void av_bsf_flush(AVBSFContext *ctx) · 5 unmodified lines
197195
198196 int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
199197 {
200− AVBSFInternal *bsfi = ctx->internal;
198+ FFBSFContext *const bsfi = ffbsfcontext(ctx);
201199 int ret;
202200
203201 if (!pkt || IS_EMPTY(pkt)) {
@@ -228,7 +226,7 @@ int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt) · 24 unmodified lines
228226
229227 int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
230228 {
231− AVBSFInternal *bsfi = ctx->internal;
229+ FFBSFContext *const bsfi = ffbsfcontext(ctx);
232230 AVPacket *tmp_pkt;
233231
234232 if (bsfi->eof)
@@ -249,7 +247,7 @@ int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt) · 14 unmodified lines
249247
250248 int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
251249 {
252− AVBSFInternal *bsfi = ctx->internal;
250+ FFBSFContext *const bsfi = ffbsfcontext(ctx);
253251
254252 if (bsfi->eof)
255253 return AVERROR_EOF;
libavcodec/bsf.h+0−8
@@ -34,8 +34,6 @@ · 33 unmodified lines
3434 * @{
3535 */
3636
37−typedef struct AVBSFInternal AVBSFInternal;
38−
3937 /**
4038 * The bitstream filter state.
4139 *
@@ -57,12 +55,6 @@ typedef struct AVBSFContext { · 15 unmodified lines
5755 */
5856 const struct AVBitStreamFilter *filter;
5957
60− /**
61− * Opaque libavcodec internal data. Must not be touched by the caller in any
62− * way.
63− */
64− AVBSFInternal *internal;
65−
6658 /**
6759 * Opaque filter-specific private data. If filter->priv_class is non-NULL,
6860 * this is an AVOptions-enabled struct.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t