1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avcodec/decode: Use RefStruct API for hwaccel_picture_private

Avoids allocations and therefore error checks: Syncing
hwaccel_picture_private across threads can't fail any more.
Also gets rid of an unnecessary pointer in structures and
in the parameter list of ff_hwaccel_frame_priv_alloc().

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Reviewed-by: Lynne <dev@lynne.ee>
Tested-by: Lynne <dev@lynne.ee>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2022-08-04 16:21:03 +02:00
parent 3ba4f9c21e
commit f8252d6ce3
20 changed files with 57 additions and 135 deletions

View File

@ -27,7 +27,6 @@
#include "libavutil/opt.h"
#include "avcodec.h"
#include "av1_parse.h"
#include "decode.h"
#include "av1dec.h"
#include "atsc_a53.h"
#include "bytestream.h"
@ -640,8 +639,7 @@ static int get_pixel_format(AVCodecContext *avctx)
static void av1_frame_unref(AVCodecContext *avctx, AV1Frame *f)
{
ff_thread_release_buffer(avctx, f->f);
av_buffer_unref(&f->hwaccel_priv_buf);
f->hwaccel_picture_private = NULL;
ff_refstruct_unref(&f->hwaccel_picture_private);
ff_refstruct_unref(&f->header_ref);
f->raw_frame_header = NULL;
f->spatial_id = f->temporal_id = 0;
@ -666,12 +664,8 @@ static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *s
if (ret < 0)
goto fail;
if (src->hwaccel_picture_private) {
dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
if (!dst->hwaccel_priv_buf)
goto fail;
dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
}
ff_refstruct_replace(&dst->hwaccel_picture_private,
src->hwaccel_picture_private);
dst->spatial_id = src->spatial_id;
dst->temporal_id = src->temporal_id;
@ -915,8 +909,7 @@ static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f)
break;
}
ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private,
&f->hwaccel_priv_buf);
ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private);
if (ret < 0)
goto fail;

View File

@ -24,7 +24,6 @@
#include <stdint.h>
#include "libavutil/fifo.h"
#include "libavutil/buffer.h"
#include "libavutil/frame.h"
#include "libavutil/pixfmt.h"
#include "avcodec.h"
@ -35,8 +34,7 @@
typedef struct AV1Frame {
AVFrame *f;
AVBufferRef *hwaccel_priv_buf;
void *hwaccel_picture_private;
void *hwaccel_picture_private; ///< RefStruct reference
AV1RawOBU *header_ref; ///< RefStruct reference backing raw_frame_header.
AV1RawFrameHeader *raw_frame_header;

View File

@ -28,18 +28,13 @@
#endif
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/emms.h"
#include "libavutil/fifo.h"
#include "libavutil/frame.h"
#include "libavutil/hwcontext.h"
#include "libavutil/imgutils.h"
#include "libavutil/internal.h"
#include "libavutil/intmath.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "avcodec_internal.h"
@ -52,6 +47,7 @@
#include "hwconfig.h"
#include "internal.h"
#include "packet_internal.h"
#include "refstruct.h"
#include "thread.h"
typedef struct DecodeContext {
@ -1839,34 +1835,22 @@ int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
return 0;
}
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private,
AVBufferRef **hwaccel_priv_buf)
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
{
const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
AVBufferRef *ref;
AVHWFramesContext *frames_ctx;
uint8_t *data;
if (!hwaccel || !hwaccel->frame_priv_data_size)
return 0;
av_assert0(!*hwaccel_picture_private);
data = av_mallocz(hwaccel->frame_priv_data_size);
if (!data)
return AVERROR(ENOMEM);
frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
ref = av_buffer_create(data, hwaccel->frame_priv_data_size,
hwaccel->free_frame_priv,
frames_ctx->device_ctx, 0);
if (!ref) {
av_free(data);
*hwaccel_picture_private = ff_refstruct_alloc_ext(hwaccel->frame_priv_data_size, 0,
frames_ctx->device_ctx,
hwaccel->free_frame_priv);
if (!*hwaccel_picture_private)
return AVERROR(ENOMEM);
}
*hwaccel_priv_buf = ref;
*hwaccel_picture_private = ref->data;
return 0;
}

View File

@ -21,7 +21,6 @@
#ifndef AVCODEC_DECODE_H
#define AVCODEC_DECODE_H
#include "libavutil/buffer.h"
#include "libavutil/frame.h"
#include "libavutil/hwcontext.h"
@ -141,17 +140,14 @@ int ff_side_data_update_matrix_encoding(AVFrame *frame,
/**
* Allocate a hwaccel frame private data if the provided avctx
* uses a hwaccel method that needs it. The private data will
* be refcounted via the AVBuffer API (if allocated).
* uses a hwaccel method that needs it. The returned data is
* a RefStruct reference (if allocated).
*
* @param avctx The codec context
* @param hwaccel_picture_private Pointer to return hwaccel_picture_private
* @param hwaccel_priv_buf Pointer to return the AVBufferRef owning
* hwaccel_picture_private
* @return 0 on success, < 0 on error
*/
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private,
AVBufferRef **hwaccel_priv_buf);
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private);
/**
* Get side data of the given type from a decoding context.

View File

@ -46,7 +46,7 @@ void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
ff_thread_release_ext_buffer(h->avctx, &pic->tf);
ff_thread_release_buffer(h->avctx, pic->f_grain);
av_buffer_unref(&pic->hwaccel_priv_buf);
ff_refstruct_unref(&pic->hwaccel_picture_private);
av_buffer_unref(&pic->qscale_table_buf);
av_buffer_unref(&pic->mb_type_buf);
@ -129,14 +129,8 @@ int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
}
}
if (src->hwaccel_picture_private) {
dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
if (!dst->hwaccel_priv_buf) {
ret = AVERROR(ENOMEM);
goto fail;
}
dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
}
ff_refstruct_replace(&dst->hwaccel_picture_private,
src->hwaccel_picture_private);
ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
if (ret < 0)
@ -185,11 +179,8 @@ int ff_h264_replace_picture(H264Context *h, H264Picture *dst, const H264Picture
goto fail;
}
ret = av_buffer_replace(&dst->hwaccel_priv_buf, src->hwaccel_priv_buf);
if (ret < 0)
goto fail;
dst->hwaccel_picture_private = src->hwaccel_picture_private;
ff_refstruct_replace(&dst->hwaccel_picture_private,
src->hwaccel_picture_private);
ret = av_buffer_replace(&dst->decode_error_flags, src->decode_error_flags);
if (ret < 0)

View File

@ -206,8 +206,7 @@ static int alloc_picture(H264Context *h, H264Picture *pic)
goto fail;
}
ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private,
&pic->hwaccel_priv_buf);
ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private);
if (ret < 0)
goto fail;

View File

@ -117,8 +117,8 @@ typedef struct H264Picture {
AVBufferRef *mb_type_buf;
uint32_t *mb_type;
AVBufferRef *hwaccel_priv_buf;
void *hwaccel_picture_private; ///< hardware accelerator private data
/// RefStruct reference for hardware accelerator private data
void *hwaccel_picture_private;
AVBufferRef *ref_index_buf[2];
int8_t *ref_index[2];

View File

@ -27,6 +27,7 @@
#include "thread.h"
#include "hevc.h"
#include "hevcdec.h"
#include "refstruct.h"
#include "threadframe.h"
void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
@ -51,8 +52,7 @@ void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
frame->collocated_ref = NULL;
av_buffer_unref(&frame->hwaccel_priv_buf);
frame->hwaccel_picture_private = NULL;
ff_refstruct_unref(&frame->hwaccel_picture_private);
}
}
@ -118,8 +118,7 @@ static HEVCFrame *alloc_frame(HEVCContext *s)
(s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD))
frame->frame->flags |= AV_FRAME_FLAG_INTERLACED;
ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private,
&frame->hwaccel_priv_buf);
ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
if (ret < 0)
goto fail;

View File

@ -28,7 +28,6 @@
#include "libavutil/attributes.h"
#include "libavutil/avstring.h"
#include "libavutil/common.h"
#include "libavutil/display.h"
#include "libavutil/film_grain_params.h"
#include "libavutil/internal.h"
#include "libavutil/md5.h"
@ -37,13 +36,11 @@
#include "libavutil/timecode.h"
#include "bswapdsp.h"
#include "bytestream.h"
#include "cabac_functions.h"
#include "codec_internal.h"
#include "decode.h"
#include "golomb.h"
#include "hevc.h"
#include "hevc_data.h"
#include "hevc_parse.h"
#include "hevcdec.h"
#include "hwaccel_internal.h"
@ -3426,12 +3423,8 @@ static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
dst->flags = src->flags;
dst->sequence = src->sequence;
if (src->hwaccel_picture_private) {
dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
if (!dst->hwaccel_priv_buf)
goto fail;
dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
}
ff_refstruct_replace(&dst->hwaccel_picture_private,
src->hwaccel_picture_private);
return 0;
fail:

View File

@ -419,8 +419,7 @@ typedef struct HEVCFrame {
AVBufferRef *rpl_tab_buf;
AVBufferRef *rpl_buf;
AVBufferRef *hwaccel_priv_buf;
void *hwaccel_picture_private;
void *hwaccel_picture_private; ///< RefStruct reference
/**
* A sequence counter, so that old frames are output first

View File

@ -26,6 +26,7 @@
#include <stdint.h>
#include "avcodec.h"
#include "refstruct.h"
#define HWACCEL_CAP_ASYNC_SAFE (1 << 0)
#define HWACCEL_CAP_THREAD_SAFE (1 << 1)
@ -154,7 +155,7 @@ typedef struct FFHWAccel {
* @param hwctx a pointer to an AVHWDeviceContext.
* @param data the per-frame hardware accelerator private data to be freed.
*/
void (*free_frame_priv)(void *hwctx, uint8_t *data);
void (*free_frame_priv)(FFRefStructOpaque hwctx, void *data);
/**
* Callback to flush the hwaccel state.

View File

@ -27,11 +27,11 @@
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "decode.h"
#include "motion_est.h"
#include "mpegpicture.h"
#include "mpegutils.h"
#include "refstruct.h"
#include "threadframe.h"
static void av_noinline free_picture_tables(Picture *pic)
@ -171,8 +171,7 @@ static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic,
pic->f->height = avctx->height;
}
ret = ff_hwaccel_frame_priv_alloc(avctx, &pic->hwaccel_picture_private,
&pic->hwaccel_priv_buf);
ret = ff_hwaccel_frame_priv_alloc(avctx, &pic->hwaccel_picture_private);
if (ret < 0)
return ret;
@ -316,12 +315,11 @@ void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
else if (pic->f)
av_frame_unref(pic->f);
av_buffer_unref(&pic->hwaccel_priv_buf);
ff_refstruct_unref(&pic->hwaccel_picture_private);
if (pic->needs_realloc)
free_picture_tables(pic);
pic->hwaccel_picture_private = NULL;
pic->field_picture = 0;
pic->b_frame_score = 0;
pic->needs_realloc = 0;
@ -380,14 +378,8 @@ int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
if (ret < 0)
goto fail;
if (src->hwaccel_picture_private) {
dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
if (!dst->hwaccel_priv_buf) {
ret = AVERROR(ENOMEM);
goto fail;
}
dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
}
ff_refstruct_replace(&dst->hwaccel_picture_private,
src->hwaccel_picture_private);
dst->field_picture = src->field_picture;
dst->b_frame_score = src->b_frame_score;

View File

@ -66,8 +66,8 @@ typedef struct Picture {
int alloc_mb_height; ///< mb_height used to allocate tables
int alloc_mb_stride; ///< mb_stride used to allocate tables
AVBufferRef *hwaccel_priv_buf;
void *hwaccel_picture_private; ///< Hardware accelerator private data
/// RefStruct reference for hardware accelerator private data
void *hwaccel_picture_private;
int field_picture; ///< whether or not the picture was encoded in separate fields

View File

@ -108,8 +108,7 @@ static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
return ret;
if (!(f->seg_map = ff_refstruct_allocz(s->mb_width * s->mb_height)))
goto fail;
ret = ff_hwaccel_frame_priv_alloc(s->avctx, &f->hwaccel_picture_private,
&f->hwaccel_priv_buf);
ret = ff_hwaccel_frame_priv_alloc(s->avctx, &f->hwaccel_picture_private);
if (ret < 0)
goto fail;
@ -124,8 +123,7 @@ fail:
static void vp8_release_frame(VP8Context *s, VP8Frame *f)
{
ff_refstruct_unref(&f->seg_map);
av_buffer_unref(&f->hwaccel_priv_buf);
f->hwaccel_picture_private = NULL;
ff_refstruct_unref(&f->hwaccel_picture_private);
ff_thread_release_ext_buffer(s->avctx, &f->tf);
}
@ -139,12 +137,8 @@ static int vp8_ref_frame(VP8Context *s, VP8Frame *dst, const VP8Frame *src)
if ((ret = ff_thread_ref_frame(&dst->tf, &src->tf)) < 0)
return ret;
ff_refstruct_replace(&dst->seg_map, src->seg_map);
if (src->hwaccel_picture_private) {
dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
if (!dst->hwaccel_priv_buf)
return AVERROR(ENOMEM);
dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
}
ff_refstruct_replace(&dst->hwaccel_picture_private,
src->hwaccel_picture_private);
return 0;
}

View File

@ -28,7 +28,6 @@
#include <stdatomic.h>
#include "libavutil/buffer.h"
#include "libavutil/mem_internal.h"
#include "libavutil/thread.h"
@ -154,8 +153,7 @@ typedef struct VP8Frame {
ThreadFrame tf;
uint8_t *seg_map; ///< RefStruct reference
AVBufferRef *hwaccel_priv_buf;
void *hwaccel_picture_private;
void *hwaccel_picture_private; ///< RefStruct reference
} VP8Frame;
#define MAX_THREADS 8

View File

@ -30,6 +30,7 @@
#include "hwaccel_internal.h"
#include "hwconfig.h"
#include "profiles.h"
#include "refstruct.h"
#include "thread.h"
#include "threadframe.h"
#include "pthread_internal.h"
@ -100,9 +101,8 @@ static void vp9_frame_unref(AVCodecContext *avctx, VP9Frame *f)
{
ff_thread_release_ext_buffer(avctx, &f->tf);
av_buffer_unref(&f->extradata);
av_buffer_unref(&f->hwaccel_priv_buf);
ff_refstruct_unref(&f->hwaccel_picture_private);
f->segmentation_map = NULL;
f->hwaccel_picture_private = NULL;
}
static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
@ -135,8 +135,7 @@ static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
f->segmentation_map = f->extradata->data;
f->mv = (VP9mvrefPair *) (f->extradata->data + sz);
ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private,
&f->hwaccel_priv_buf);
ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private);
if (ret < 0)
goto fail;
@ -163,12 +162,8 @@ static int vp9_frame_ref(AVCodecContext *avctx, VP9Frame *dst, VP9Frame *src)
dst->mv = src->mv;
dst->uses_2pass = src->uses_2pass;
if (src->hwaccel_picture_private) {
dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
if (!dst->hwaccel_priv_buf)
goto fail;
dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
}
ff_refstruct_replace(&dst->hwaccel_picture_private,
src->hwaccel_picture_private);
return 0;

View File

@ -69,8 +69,7 @@ typedef struct VP9Frame {
VP9mvrefPair *mv;
int uses_2pass;
AVBufferRef *hwaccel_priv_buf;
void *hwaccel_picture_private;
void *hwaccel_picture_private; ///< RefStruct reference
} VP9Frame;
enum BlockLevel {

View File

@ -536,10 +536,10 @@ static int vk_av1_end_frame(AVCodecContext *avctx)
return ff_vk_decode_frame(avctx, pic->f, vp, rav, rvp);
}
static void vk_av1_free_frame_priv(void *_hwctx, uint8_t *data)
static void vk_av1_free_frame_priv(FFRefStructOpaque _hwctx, void *data)
{
AVHWDeviceContext *hwctx = _hwctx;
AV1VulkanDecodePicture *ap = (AV1VulkanDecodePicture *)data;
AVHWDeviceContext *hwctx = _hwctx.nc;
AV1VulkanDecodePicture *ap = data;
/* Workaround for a spec issue. */
if (ap->frame_id_set)
@ -547,9 +547,6 @@ static void vk_av1_free_frame_priv(void *_hwctx, uint8_t *data)
/* Free frame resources, this also destroys the session parameters. */
ff_vk_decode_free_frame(hwctx, &ap->vp);
/* Free frame context */
av_free(ap);
}
const FFHWAccel ff_av1_vulkan_hwaccel = {

View File

@ -530,16 +530,13 @@ static int vk_h264_end_frame(AVCodecContext *avctx)
return ff_vk_decode_frame(avctx, pic->f, vp, rav, rvp);
}
static void vk_h264_free_frame_priv(void *_hwctx, uint8_t *data)
static void vk_h264_free_frame_priv(FFRefStructOpaque _hwctx, void *data)
{
AVHWDeviceContext *hwctx = _hwctx;
H264VulkanDecodePicture *hp = (H264VulkanDecodePicture *)data;
AVHWDeviceContext *hwctx = _hwctx.nc;
H264VulkanDecodePicture *hp = data;
/* Free frame resources, this also destroys the session parameters. */
ff_vk_decode_free_frame(hwctx, &hp->vp);
/* Free frame context */
av_free(hp);
}
const FFHWAccel ff_h264_vulkan_hwaccel = {

View File

@ -909,16 +909,13 @@ static int vk_hevc_end_frame(AVCodecContext *avctx)
return ff_vk_decode_frame(avctx, pic->frame, vp, rav, rvp);
}
static void vk_hevc_free_frame_priv(void *_hwctx, uint8_t *data)
static void vk_hevc_free_frame_priv(FFRefStructOpaque _hwctx, void *data)
{
AVHWDeviceContext *hwctx = _hwctx;
HEVCVulkanDecodePicture *hp = (HEVCVulkanDecodePicture *)data;
AVHWDeviceContext *hwctx = _hwctx.nc;
HEVCVulkanDecodePicture *hp = data;
/* Free frame resources */
ff_vk_decode_free_frame(hwctx, &hp->vp);
/* Free frame context */
av_free(hp);
}
const FFHWAccel ff_hevc_vulkan_hwaccel = {