avcodec/get_buffer: Use RefStruct API for FramePool

Avoids allocations and frees and error checks for said allocations;
also avoids a few indirections and casts.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2022-08-04 15:57:41 +02:00
parent 9281dcb801
commit 78c9ed26b6
4 changed files with 17 additions and 39 deletions

View File

@ -44,6 +44,7 @@
#include "frame_thread_encoder.h"
#include "hwconfig.h"
#include "internal.h"
#include "refstruct.h"
#include "thread.h"
/**
@ -459,7 +460,7 @@ av_cold int avcodec_close(AVCodecContext *avctx)
av_frame_free(&avci->in_frame);
av_frame_free(&avci->recon_frame);
av_buffer_unref(&avci->pool);
ff_refstruct_unref(&avci->pool);
ff_hwaccel_uninit(avctx);

View File

@ -32,6 +32,7 @@
#include "avcodec.h"
#include "internal.h"
#include "refstruct.h"
typedef struct FramePool {
/**
@ -52,40 +53,18 @@ typedef struct FramePool {
int samples;
} FramePool;
static void frame_pool_free(void *opaque, uint8_t *data)
static void frame_pool_free(FFRefStructOpaque unused, void *obj)
{
FramePool *pool = (FramePool*)data;
FramePool *pool = obj;
int i;
for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
av_buffer_pool_uninit(&pool->pools[i]);
av_freep(&data);
}
static AVBufferRef *frame_pool_alloc(void)
{
FramePool *pool = av_mallocz(sizeof(*pool));
AVBufferRef *buf;
if (!pool)
return NULL;
buf = av_buffer_create((uint8_t*)pool, sizeof(*pool),
frame_pool_free, NULL, 0);
if (!buf) {
av_freep(&pool);
return NULL;
}
return buf;
}
static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
{
FramePool *pool = avctx->internal->pool ?
(FramePool*)avctx->internal->pool->data : NULL;
AVBufferRef *pool_buf;
FramePool *pool = avctx->internal->pool;
int i, ret, ch, planes;
if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
@ -109,10 +88,9 @@ FF_ENABLE_DEPRECATION_WARNINGS
return 0;
}
pool_buf = frame_pool_alloc();
if (!pool_buf)
pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL, frame_pool_free);
if (!pool)
return AVERROR(ENOMEM);
pool = (FramePool*)pool_buf->data;
switch (avctx->codec_type) {
case AVMEDIA_TYPE_VIDEO: {
@ -189,18 +167,18 @@ FF_ENABLE_DEPRECATION_WARNINGS
default: av_assert0(0);
}
av_buffer_unref(&avctx->internal->pool);
avctx->internal->pool = pool_buf;
ff_refstruct_unref(&avctx->internal->pool);
avctx->internal->pool = pool;
return 0;
fail:
av_buffer_unref(&pool_buf);
ff_refstruct_unref(&pool);
return ret;
}
static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
{
FramePool *pool = (FramePool*)avctx->internal->pool->data;
FramePool *pool = avctx->internal->pool;
int planes = pool->planes;
int i;
@ -245,7 +223,7 @@ fail:
static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
{
FramePool *pool = (FramePool*)s->internal->pool->data;
FramePool *pool = s->internal->pool;
int i;
if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {

View File

@ -62,7 +62,7 @@ typedef struct AVCodecInternal {
*/
int pad_samples;
AVBufferRef *pool;
struct FramePool *pool;
void *thread_ctx;

View File

@ -35,6 +35,7 @@
#include "hwconfig.h"
#include "internal.h"
#include "pthread_internal.h"
#include "refstruct.h"
#include "thread.h"
#include "threadframe.h"
#include "version_major.h"
@ -329,9 +330,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
dst->hwaccel_flags = src->hwaccel_flags;
err = av_buffer_replace(&dst->internal->pool, src->internal->pool);
if (err < 0)
return err;
ff_refstruct_replace(&dst->internal->pool, src->internal->pool);
}
if (for_user) {
@ -740,7 +739,7 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
av_freep(&ctx->priv_data);
}
av_buffer_unref(&ctx->internal->pool);
ff_refstruct_unref(&ctx->internal->pool);
av_packet_free(&ctx->internal->last_pkt_props);
av_freep(&ctx->internal);
av_buffer_unref(&ctx->hw_frames_ctx);