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>
pull/390/head
Andreas Rheinhardt 2 years ago
parent 9281dcb801
commit 78c9ed26b6
  1. 3
      libavcodec/avcodec.c
  2. 44
      libavcodec/get_buffer.c
  3. 2
      libavcodec/internal.h
  4. 7
      libavcodec/pthread_frame.c

@ -44,6 +44,7 @@
#include "frame_thread_encoder.h" #include "frame_thread_encoder.h"
#include "hwconfig.h" #include "hwconfig.h"
#include "internal.h" #include "internal.h"
#include "refstruct.h"
#include "thread.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->in_frame);
av_frame_free(&avci->recon_frame); av_frame_free(&avci->recon_frame);
av_buffer_unref(&avci->pool); ff_refstruct_unref(&avci->pool);
ff_hwaccel_uninit(avctx); ff_hwaccel_uninit(avctx);

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

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

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

Loading…
Cancel
Save