From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH 4/5] avcodec/decode: Extend ff_hwaccel_frame_priv_alloc()'s task Date: Wed, 2 Aug 2023 08:43:31 +0200 Message-ID: <GV1P250MB073757B79BD5C225DC1CC0B88F0BA@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM> (raw) In-Reply-To: <GV1P250MB07372D6E72B1402AB543507C8F0BA@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM> All usages of ff_hwaccel_frame_priv_alloc() have the same pattern: Check for whether a hwaccel is in use; check whether it needs private frame-specific data; allocate the AVBuffer and set it. This commit modifies ff_hwaccel_frame_priv_alloc() to perform this task on its own. (It also seems that the H.264 decoder did not perform proper cleanup in case the buffer could not be allocated. This has been changed.) Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavcodec/av1dec.c | 16 +++++----------- libavcodec/decode.c | 26 +++++++++++++++++++------- libavcodec/decode.h | 17 ++++++++++------- libavcodec/h264_slice.c | 15 +++++---------- libavcodec/hevc_refs.c | 14 ++++---------- libavcodec/mpegpicture.c | 15 ++++----------- libavcodec/vp8.c | 18 ++++++++---------- libavcodec/vp9.c | 18 +++++++----------- 8 files changed, 62 insertions(+), 77 deletions(-) diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c index cc178464b9..09339529f7 100644 --- a/libavcodec/av1dec.c +++ b/libavcodec/av1dec.c @@ -907,17 +907,11 @@ static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f) break; } - if (avctx->hwaccel) { - const AVHWAccel *hwaccel = avctx->hwaccel; - if (hwaccel->frame_priv_data_size) { - f->hwaccel_priv_buf = ff_hwaccel_frame_priv_alloc(avctx, hwaccel); - if (!f->hwaccel_priv_buf) { - ret = AVERROR(ENOMEM); - goto fail; - } - f->hwaccel_picture_private = f->hwaccel_priv_buf->data; - } - } + ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private, + &f->hwaccel_priv_buf); + if (ret < 0) + goto fail; + return 0; fail: diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 53c90cddb7..be761873a3 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -1788,24 +1788,36 @@ int ff_copy_palette(void *dst, const AVPacket *src, void *logctx) return 0; } -AVBufferRef *ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, - const AVHWAccel *hwaccel) +int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private, + AVBufferRef **hwaccel_priv_buf) { + const AVHWAccel *hwaccel = avctx->hwaccel; AVBufferRef *ref; - AVHWFramesContext *frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data; - uint8_t *data = av_mallocz(hwaccel->frame_priv_data_size); + 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 NULL; + 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); - return NULL; + return AVERROR(ENOMEM); } - return ref; + *hwaccel_priv_buf = ref; + *hwaccel_picture_private = ref->data; + + return 0; } void ff_decode_flush_buffers(AVCodecContext *avctx) diff --git a/libavcodec/decode.h b/libavcodec/decode.h index 2b9fe59907..a52152e4a7 100644 --- a/libavcodec/decode.h +++ b/libavcodec/decode.h @@ -140,14 +140,17 @@ int ff_side_data_update_matrix_encoding(AVFrame *frame, enum AVMatrixEncoding matrix_encoding); /** - * Allocate a hwaccel frame private data and create an AVBufferRef - * from it. + * 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). * - * @param avctx The codec context which to attach as an opaque value - * @param hwaccel The hwaccel for which to allocate - * @return The allocated buffer + * @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 */ -AVBufferRef *ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, - const AVHWAccel *hwaccel); +int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private, + AVBufferRef **hwaccel_priv_buf); #endif /* AVCODEC_DECODE_H */ diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c index 790ba644cd..27fbd8d953 100644 --- a/libavcodec/h264_slice.c +++ b/libavcodec/h264_slice.c @@ -209,16 +209,11 @@ static int alloc_picture(H264Context *h, H264Picture *pic) goto fail; } - if (h->avctx->hwaccel) { - const AVHWAccel *hwaccel = h->avctx->hwaccel; - av_assert0(!pic->hwaccel_picture_private); - if (hwaccel->frame_priv_data_size) { - pic->hwaccel_priv_buf = ff_hwaccel_frame_priv_alloc(h->avctx, hwaccel); - if (!pic->hwaccel_priv_buf) - return AVERROR(ENOMEM); - pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data; - } - } + ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private, + &pic->hwaccel_priv_buf); + if (ret < 0) + goto fail; + if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) { int h_chroma_shift, v_chroma_shift; av_pix_fmt_get_chroma_sub_sample(pic->f->format, diff --git a/libavcodec/hevc_refs.c b/libavcodec/hevc_refs.c index 3aa8e19950..c5c1203ef8 100644 --- a/libavcodec/hevc_refs.c +++ b/libavcodec/hevc_refs.c @@ -118,16 +118,10 @@ static HEVCFrame *alloc_frame(HEVCContext *s) (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD)) frame->frame->flags |= AV_FRAME_FLAG_INTERLACED; - if (s->avctx->hwaccel) { - const AVHWAccel *hwaccel = s->avctx->hwaccel; - av_assert0(!frame->hwaccel_picture_private); - if (hwaccel->frame_priv_data_size) { - frame->hwaccel_priv_buf = ff_hwaccel_frame_priv_alloc(s->avctx, hwaccel); - if (!frame->hwaccel_priv_buf) - goto fail; - frame->hwaccel_picture_private = frame->hwaccel_priv_buf->data; - } - } + ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private, + &frame->hwaccel_priv_buf); + if (ret < 0) + goto fail; return frame; fail: diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c index 71c7a3fd70..b7c804c8ec 100644 --- a/libavcodec/mpegpicture.c +++ b/libavcodec/mpegpicture.c @@ -171,17 +171,10 @@ static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic, pic->f->height = avctx->height; } - if (avctx->hwaccel) { - assert(!pic->hwaccel_picture_private); - if (avctx->hwaccel->frame_priv_data_size) { - pic->hwaccel_priv_buf = ff_hwaccel_frame_priv_alloc(avctx, avctx->hwaccel); - if (!pic->hwaccel_priv_buf) { - av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n"); - return -1; - } - pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data; - } - } + ret = ff_hwaccel_frame_priv_alloc(avctx, &pic->hwaccel_picture_private, + &pic->hwaccel_priv_buf); + if (ret < 0) + return ret; if ((linesize && linesize != pic->f->linesize[0]) || (uvlinesize && uvlinesize != pic->f->linesize[1])) { diff --git a/libavcodec/vp8.c b/libavcodec/vp8.c index 50afe19b7a..15a2961f56 100644 --- a/libavcodec/vp8.c +++ b/libavcodec/vp8.c @@ -104,23 +104,21 @@ static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref) if ((ret = ff_thread_get_ext_buffer(s->avctx, &f->tf, ref ? AV_GET_BUFFER_FLAG_REF : 0)) < 0) return ret; - if (!(f->seg_map = av_buffer_allocz(s->mb_width * s->mb_height))) + if (!(f->seg_map = av_buffer_allocz(s->mb_width * s->mb_height))) { + ret = AVERROR(ENOMEM); goto fail; - if (s->avctx->hwaccel) { - const AVHWAccel *hwaccel = s->avctx->hwaccel; - if (hwaccel->frame_priv_data_size) { - f->hwaccel_priv_buf = ff_hwaccel_frame_priv_alloc(s->avctx, hwaccel); - if (!f->hwaccel_priv_buf) - goto fail; - f->hwaccel_picture_private = f->hwaccel_priv_buf->data; - } } + ret = ff_hwaccel_frame_priv_alloc(s->avctx, &f->hwaccel_picture_private, + &f->hwaccel_priv_buf); + if (ret < 0) + goto fail; + return 0; fail: av_buffer_unref(&f->seg_map); ff_thread_release_ext_buffer(s->avctx, &f->tf); - return AVERROR(ENOMEM); + return ret; } static void vp8_release_frame(VP8Context *s, VP8Frame *f) diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c index 4f704ec0dd..751dc7271f 100644 --- a/libavcodec/vp9.c +++ b/libavcodec/vp9.c @@ -119,12 +119,14 @@ static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f) s->frame_extradata_pool = av_buffer_pool_init(sz * (1 + sizeof(VP9mvrefPair)), NULL); if (!s->frame_extradata_pool) { s->frame_extradata_pool_size = 0; + ret = AVERROR(ENOMEM); goto fail; } s->frame_extradata_pool_size = sz; } f->extradata = av_buffer_pool_get(s->frame_extradata_pool); if (!f->extradata) { + ret = AVERROR(ENOMEM); goto fail; } memset(f->extradata->data, 0, f->extradata->size); @@ -132,22 +134,16 @@ static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f) f->segmentation_map = f->extradata->data; f->mv = (VP9mvrefPair *) (f->extradata->data + sz); - if (avctx->hwaccel) { - const AVHWAccel *hwaccel = avctx->hwaccel; - av_assert0(!f->hwaccel_picture_private); - if (hwaccel->frame_priv_data_size) { - f->hwaccel_priv_buf = ff_hwaccel_frame_priv_alloc(avctx, hwaccel); - if (!f->hwaccel_priv_buf) - goto fail; - f->hwaccel_picture_private = f->hwaccel_priv_buf->data; - } - } + ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private, + &f->hwaccel_priv_buf); + if (ret < 0) + goto fail; return 0; fail: vp9_frame_unref(avctx, f); - return AVERROR(ENOMEM); + return ret; } static int vp9_frame_ref(AVCodecContext *avctx, VP9Frame *dst, VP9Frame *src) -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2023-08-02 6:42 UTC|newest] Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-08-02 6:38 [FFmpeg-devel] [PATCH 1/5] avcodec/nvdec_(mjpeg|vp8): Constify AVHWAccels Andreas Rheinhardt 2023-08-02 6:43 ` [FFmpeg-devel] [PATCH 2/5] avcodec/error_resilience, mpeg12dec: Remove always-true checks Andreas Rheinhardt 2023-08-02 6:43 ` [FFmpeg-devel] [PATCH 3/5] avcodec/hwconfig: Move HWACCEL_CAP_* to a new header Andreas Rheinhardt 2023-08-05 10:01 ` Andreas Rheinhardt 2023-08-02 6:43 ` Andreas Rheinhardt [this message] 2023-08-02 6:43 ` [FFmpeg-devel] [PATCH 5/5] avcodec/avcodec: Add FFHWAccel, hide internals of AVHWAccel Andreas Rheinhardt 2023-08-02 10:58 ` [FFmpeg-devel] [PATCH 06/15] avcodec/avcodec: Remove unnecessary forward declaration Andreas Rheinhardt 2023-08-04 10:06 ` Andreas Rheinhardt 2023-08-02 10:58 ` [FFmpeg-devel] [PATCH 07/15] avdevice/pulse_audio_common: Avoid inclusion of avcodec.h Andreas Rheinhardt 2023-08-02 10:58 ` [FFmpeg-devel] [PATCH 08/15] avcodec/tak: Use void* instead of AVCodecContext* for logcontext Andreas Rheinhardt 2023-08-02 10:58 ` [FFmpeg-devel] [PATCH 09/15] avcodec/h264dec: Move inline functions only used by CABAC/CAVLC code Andreas Rheinhardt 2023-08-02 10:58 ` [FFmpeg-devel] [PATCH 10/15] avformat/av1dec: Remove avcodec.h inclusion Andreas Rheinhardt 2023-08-02 10:58 ` [FFmpeg-devel] [PATCH 11/15] avformat/evcdec: Remove unused headers Andreas Rheinhardt 2023-08-02 10:58 ` [FFmpeg-devel] [PATCH 12/15] avformat/internal: Use forward declaration for AVCodecDescriptor Andreas Rheinhardt 2023-08-02 10:58 ` [FFmpeg-devel] [PATCH 13/15] avcodec/dirac: Include used headers directly Andreas Rheinhardt 2023-08-02 10:58 ` [FFmpeg-devel] [PATCH 14/15] avformat/rawdec: Don't include avcodec.h Andreas Rheinhardt 2023-08-02 10:58 ` [FFmpeg-devel] [PATCH 15/15] avcodec/h264_metadata_bsf: Improve included headers Andreas Rheinhardt 2023-08-02 12:18 ` [FFmpeg-devel] [PATCH 1/5] avcodec/nvdec_(mjpeg|vp8): Constify AVHWAccels Timo Rothenpieler 2023-08-02 12:54 ` [FFmpeg-devel] [PATCH 16/19] avcodec/internal: Move FF_MAX_EXTRADATA_SIZE to its only user Andreas Rheinhardt 2023-08-02 12:54 ` [FFmpeg-devel] [PATCH 17/19] avcodec/utils: Move ff_color_frame() " Andreas Rheinhardt 2023-08-02 12:54 ` [FFmpeg-devel] [PATCH 18/19] avcodec/utils: Move ff_int_from_list_or_default() " Andreas Rheinhardt 2023-08-02 12:54 ` [FFmpeg-devel] [PATCH 19/19] avcodec/svq1enc: Remove unnecessary cast Andreas Rheinhardt
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=GV1P250MB073757B79BD5C225DC1CC0B88F0BA@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM \ --to=andreas.rheinhardt@outlook.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git