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.
Showing the changes to libavcodec (2 of 2 files). Show all
Unified · Split
| @@ -32,29 +32,35 @@ · 31 unmodified lines | |||
| 32 | 32 | ||
| 33 | #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems) | 33 | #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems) |
| 34 | 34 | ||
| 35 | struct AVBSFInternal { | 35 | typedef struct FFBSFContext { |
| 36 | AVBSFContext pub; | ||
| 36 | AVPacket *buffer_pkt; | 37 | AVPacket *buffer_pkt; |
| 37 | int eof; | 38 | int eof; |
| 38 | }; | 39 | } FFBSFContext; |
| 40 | |||
| 41 | static av_always_inline FFBSFContext *ffbsfcontext(AVBSFContext *ctx) | ||
| 42 | { | ||
| 43 | return (FFBSFContext *)ctx; | ||
| 44 | } | ||
| 39 | 45 | ||
| 40 | void av_bsf_free(AVBSFContext **pctx) | 46 | void av_bsf_free(AVBSFContext **pctx) |
| 41 | { | 47 | { |
| 42 | AVBSFContext *ctx; | 48 | AVBSFContext *ctx; |
| 49 | FFBSFContext *bsfi; | ||
| 43 | 50 | ||
| 44 | if (!pctx || !*pctx) | 51 | if (!pctx || !*pctx) |
| 45 | return; | 52 | return; |
| 46 | ctx = *pctx; | 53 | ctx = *pctx; |
| 54 | bsfi = ffbsfcontext(ctx); | ||
| 47 | 55 | ||
| 48 | if (ctx->internal) { | 56 | if (ctx->priv_data) { |
| 49 | if (ctx->filter->close) | 57 | if (ctx->filter->close) |
| 50 | ctx->filter->close(ctx); | 58 | ctx->filter->close(ctx); |
| 51 | av_packet_free(&ctx->internal->buffer_pkt); | 59 | if (ctx->filter->priv_class) |
| 52 | av_freep(&ctx->internal); | 60 | av_opt_free(ctx->priv_data); |
| 61 | av_freep(&ctx->priv_data); | ||
| 53 | } | 62 | } |
| 54 | if (ctx->filter->priv_class && ctx->priv_data) | 63 | av_packet_free(&bsfi->buffer_pkt); |
| 55 | av_opt_free(ctx->priv_data); | ||
| 56 | |||
| 57 | av_freep(&ctx->priv_data); | ||
| 58 | 64 | ||
| 59 | avcodec_parameters_free(&ctx->par_in); | 65 | avcodec_parameters_free(&ctx->par_in); |
| 60 | avcodec_parameters_free(&ctx->par_out); | 66 | avcodec_parameters_free(&ctx->par_out); |
| @@ -92,12 +98,13 @@ const AVClass *av_bsf_get_class(void) · 31 unmodified lines | |||
| 92 | int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx) | 98 | int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx) |
| 93 | { | 99 | { |
| 94 | AVBSFContext *ctx; | 100 | AVBSFContext *ctx; |
| 95 | AVBSFInternal *bsfi; | 101 | FFBSFContext *bsfi; |
| 96 | int ret; | 102 | int ret; |
| 97 | 103 | ||
| 98 | ctx = av_mallocz(sizeof(*ctx)); | 104 | bsfi = av_mallocz(sizeof(*bsfi)); |
| 99 | if (!ctx) | 105 | if (!bsfi) |
| 100 | return AVERROR(ENOMEM); | 106 | return AVERROR(ENOMEM); |
| 107 | ctx = &bsfi->pub; | ||
| 101 | 108 | ||
| 102 | ctx->av_class = &bsf_class; | 109 | ctx->av_class = &bsf_class; |
| 103 | ctx->filter = filter; | 110 | ctx->filter = filter; |
| @@ -120,15 +127,6 @@ int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx) · 16 unmodified lines | |||
| 120 | av_opt_set_defaults(ctx->priv_data); | 127 | av_opt_set_defaults(ctx->priv_data); |
| 121 | } | 128 | } |
| 122 | } | 129 | } |
| 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 | |||
| 132 | bsfi->buffer_pkt = av_packet_alloc(); | 130 | bsfi->buffer_pkt = av_packet_alloc(); |
| 133 | if (!bsfi->buffer_pkt) { | 131 | if (!bsfi->buffer_pkt) { |
| 134 | ret = AVERROR(ENOMEM); | 132 | ret = AVERROR(ENOMEM); |
| @@ -185,7 +183,7 @@ int av_bsf_init(AVBSFContext *ctx) · 50 unmodified lines | |||
| 185 | 183 | ||
| 186 | void av_bsf_flush(AVBSFContext *ctx) | 184 | void av_bsf_flush(AVBSFContext *ctx) |
| 187 | { | 185 | { |
| 188 | AVBSFInternal *bsfi = ctx->internal; | 186 | FFBSFContext *const bsfi = ffbsfcontext(ctx); |
| 189 | 187 | ||
| 190 | bsfi->eof = 0; | 188 | bsfi->eof = 0; |
| 191 | 189 | ||
| @@ -197,7 +195,7 @@ void av_bsf_flush(AVBSFContext *ctx) · 5 unmodified lines | |||
| 197 | 195 | ||
| 198 | int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt) | 196 | int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt) |
| 199 | { | 197 | { |
| 200 | AVBSFInternal *bsfi = ctx->internal; | 198 | FFBSFContext *const bsfi = ffbsfcontext(ctx); |
| 201 | int ret; | 199 | int ret; |
| 202 | 200 | ||
| 203 | if (!pkt || IS_EMPTY(pkt)) { | 201 | if (!pkt || IS_EMPTY(pkt)) { |
| @@ -228,7 +226,7 @@ int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt) · 24 unmodified lines | |||
| 228 | 226 | ||
| 229 | int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt) | 227 | int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt) |
| 230 | { | 228 | { |
| 231 | AVBSFInternal *bsfi = ctx->internal; | 229 | FFBSFContext *const bsfi = ffbsfcontext(ctx); |
| 232 | AVPacket *tmp_pkt; | 230 | AVPacket *tmp_pkt; |
| 233 | 231 | ||
| 234 | if (bsfi->eof) | 232 | if (bsfi->eof) |
| @@ -249,7 +247,7 @@ int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt) · 14 unmodified lines | |||
| 249 | 247 | ||
| 250 | int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt) | 248 | int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt) |
| 251 | { | 249 | { |
| 252 | AVBSFInternal *bsfi = ctx->internal; | 250 | FFBSFContext *const bsfi = ffbsfcontext(ctx); |
| 253 | 251 | ||
| 254 | if (bsfi->eof) | 252 | if (bsfi->eof) |
| 255 | return AVERROR_EOF; | 253 | return AVERROR_EOF; |
| @@ -34,8 +34,6 @@ · 33 unmodified lines | |||
| 34 | * @{ | 34 | * @{ |
| 35 | */ | 35 | */ |
| 36 | 36 | ||
| 37 | typedef struct AVBSFInternal AVBSFInternal; | ||
| 38 | |||
| 39 | /** | 37 | /** |
| 40 | * The bitstream filter state. | 38 | * The bitstream filter state. |
| 41 | * | 39 | * |
| @@ -57,12 +55,6 @@ typedef struct AVBSFContext { · 15 unmodified lines | |||
| 57 | */ | 55 | */ |
| 58 | const struct AVBitStreamFilter *filter; | 56 | const struct AVBitStreamFilter *filter; |
| 59 | 57 | ||
| 60 | /** | ||
| 61 | * Opaque libavcodec internal data. Must not be touched by the caller in any | ||
| 62 | * way. | ||
| 63 | */ | ||
| 64 | AVBSFInternal *internal; | ||
| 65 | |||
| 66 | /** | 58 | /** |
| 67 | * Opaque filter-specific private data. If filter->priv_class is non-NULL, | 59 | * Opaque filter-specific private data. If filter->priv_class is non-NULL, |
| 68 | * this is an AVOptions-enabled struct. | 60 | * this is an AVOptions-enabled struct. |