Up until now, the VAAPI encoder uses fake data with the AVBuffer-API: The data pointer does not point to real memory, but is instead just a VABufferID converted to a pointer. This has probably been copied from the VAAPI-hwcontext-API (which presumably does it to avoid allocations). This commit changes this without causing additional allocations by switching to the RefStruct-pool API. This also fixes an unchecked av_buffer_ref().
Unified · Split
| @@ -16,11 +16,11 @@ · 15 unmodified lines | ||
| 16 | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | −#include "config_components.h" | |
| 20 | − | |
| 21 | 19 | #include <inttypes.h> |
| 22 | 20 | #include <string.h> |
| 23 | 21 | |
| 22 | +#include "config.h" | |
| 23 | + | |
| 24 | 24 | #include "libavutil/avassert.h" |
| 25 | 25 | #include "libavutil/common.h" |
| 26 | 26 | #include "libavutil/internal.h" |
| @@ -30,6 +30,7 @@ · 3 unmodified lines | ||
| 30 | 30 | #include "vaapi_encode.h" |
| 31 | 31 | #include "encode.h" |
| 32 | 32 | #include "avcodec.h" |
| 33 | +#include "refstruct.h" | |
| 33 | 34 | |
| 34 | 35 | const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[] = { |
| 35 | 36 | HW_CONFIG_ENCODER_FRAMES(VAAPI, VAAPI), |
| @@ -322,12 +323,12 @@ static int vaapi_encode_issue(AVCodecContext *avctx, · 286 unmodified lines | ||
| 322 | 323 | pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3]; |
| 323 | 324 | av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface); |
| 324 | 325 | |
| 325 | − pic->output_buffer_ref = av_buffer_pool_get(ctx->output_buffer_pool); | |
| 326 | + pic->output_buffer_ref = ff_refstruct_pool_get(ctx->output_buffer_pool); | |
| 326 | 327 | if (!pic->output_buffer_ref) { |
| 327 | 328 | err = AVERROR(ENOMEM); |
| 328 | 329 | goto fail; |
| 329 | 330 | } |
| 330 | − pic->output_buffer = (VABufferID)(uintptr_t)pic->output_buffer_ref->data; | |
| 331 | + pic->output_buffer = *pic->output_buffer_ref; | |
| 331 | 332 | av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n", |
| 332 | 333 | pic->output_buffer); |
| 333 | 334 | |
| @@ -658,7 +659,7 @@ fail_at_end: · 324 unmodified lines | ||
| 658 | 659 | av_freep(&pic->slices); |
| 659 | 660 | av_freep(&pic->roi); |
| 660 | 661 | av_frame_free(&pic->recon_image); |
| 661 | − av_buffer_unref(&pic->output_buffer_ref); | |
| 662 | + ff_refstruct_unref(&pic->output_buffer_ref); | |
| 662 | 663 | pic->output_buffer = VA_INVALID_ID; |
| 663 | 664 | return err; |
| 664 | 665 | } |
| @@ -780,7 +781,7 @@ static int vaapi_encode_get_coded_data(AVCodecContext *avctx, · 115 unmodified lines | ||
| 780 | 781 | int ret; |
| 781 | 782 | |
| 782 | 783 | if (ctx->coded_buffer_ref) { |
| 783 | − output_buffer_prev = (VABufferID)(uintptr_t)ctx->coded_buffer_ref->data; | |
| 784 | + output_buffer_prev = *ctx->coded_buffer_ref; | |
| 784 | 785 | ret = vaapi_encode_get_coded_buffer_size(avctx, output_buffer_prev); |
| 785 | 786 | if (ret < 0) |
| 786 | 787 | goto end; |
| @@ -808,10 +809,8 @@ static int vaapi_encode_get_coded_data(AVCodecContext *avctx, · 21 unmodified lines | ||
| 808 | 809 | goto end; |
| 809 | 810 | |
| 810 | 811 | end: |
| 811 | − if (ctx->coded_buffer_ref) { | |
| 812 | − av_buffer_unref(&ctx->coded_buffer_ref); | |
| 813 | − } | |
| 814 | − av_buffer_unref(&pic->output_buffer_ref); | |
| 812 | + ff_refstruct_unref(&ctx->coded_buffer_ref); | |
| 813 | + ff_refstruct_unref(&pic->output_buffer_ref); | |
| 815 | 814 | pic->output_buffer = VA_INVALID_ID; |
| 816 | 815 | |
| 817 | 816 | return ret; |
| @@ -830,7 +829,7 @@ static int vaapi_encode_output(AVCodecContext *avctx, · 12 unmodified lines | ||
| 830 | 829 | |
| 831 | 830 | if (pic->non_independent_frame) { |
| 832 | 831 | av_assert0(!ctx->coded_buffer_ref); |
| 833 | − ctx->coded_buffer_ref = av_buffer_ref(pic->output_buffer_ref); | |
| 832 | + ctx->coded_buffer_ref = ff_refstruct_ref(pic->output_buffer_ref); | |
| 834 | 833 | |
| 835 | 834 | if (pic->tail_size) { |
| 836 | 835 | if (ctx->tail_pkt->size) { |
| @@ -857,7 +856,7 @@ static int vaapi_encode_output(AVCodecContext *avctx, · 20 unmodified lines | ||
| 857 | 856 | vaapi_encode_set_output_property(avctx, pic, pkt_ptr); |
| 858 | 857 | |
| 859 | 858 | end: |
| 860 | − av_buffer_unref(&pic->output_buffer_ref); | |
| 859 | + ff_refstruct_unref(&pic->output_buffer_ref); | |
| 861 | 860 | pic->output_buffer = VA_INVALID_ID; |
| 862 | 861 | return err; |
| 863 | 862 | } |
| @@ -872,7 +871,7 @@ static int vaapi_encode_discard(AVCodecContext *avctx, · 8 unmodified lines | ||
| 872 | 871 | "%"PRId64"/%"PRId64".\n", |
| 873 | 872 | pic->display_order, pic->encode_order); |
| 874 | 873 | |
| 875 | − av_buffer_unref(&pic->output_buffer_ref); | |
| 874 | + ff_refstruct_unref(&pic->output_buffer_ref); | |
| 876 | 875 | pic->output_buffer = VA_INVALID_ID; |
| 877 | 876 | } |
| 878 | 877 | |
| @@ -2617,28 +2616,25 @@ static av_cold int vaapi_encode_init_roi(AVCodecContext *avctx) · 1738 unmodified lines | ||
| 2617 | 2616 | return 0; |
| 2618 | 2617 | } |
| 2619 | 2618 | |
| 2620 | −static void vaapi_encode_free_output_buffer(void *opaque, | |
| 2621 | − uint8_t *data) | |
| 2619 | +static void vaapi_encode_free_output_buffer(FFRefStructOpaque opaque, | |
| 2620 | + void *obj) | |
| 2622 | 2621 | { |
| 2623 | − AVCodecContext *avctx = opaque; | |
| 2622 | + AVCodecContext *avctx = opaque.nc; | |
| 2624 | 2623 | VAAPIEncodeContext *ctx = avctx->priv_data; |
| 2625 | − VABufferID buffer_id; | |
| 2626 | − | |
| 2627 | − buffer_id = (VABufferID)(uintptr_t)data; | |
| 2624 | + VABufferID *buffer_id_ref = obj; | |
| 2625 | + VABufferID buffer_id = *buffer_id_ref; | |
| 2628 | 2626 | |
| 2629 | 2627 | vaDestroyBuffer(ctx->hwctx->display, buffer_id); |
| 2630 | 2628 | |
| 2631 | 2629 | av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id); |
| 2632 | 2630 | } |
| 2633 | 2631 | |
| 2634 | −static AVBufferRef *vaapi_encode_alloc_output_buffer(void *opaque, | |
| 2635 | − size_t size) | |
| 2632 | +static int vaapi_encode_alloc_output_buffer(FFRefStructOpaque opaque, void *obj) | |
| 2636 | 2633 | { |
| 2637 | − AVCodecContext *avctx = opaque; | |
| 2634 | + AVCodecContext *avctx = opaque.nc; | |
| 2638 | 2635 | VAAPIEncodeContext *ctx = avctx->priv_data; |
| 2639 | − VABufferID buffer_id; | |
| 2636 | + VABufferID *buffer_id = obj; | |
| 2640 | 2637 | VAStatus vas; |
| 2641 | − AVBufferRef *ref; | |
| 2642 | 2638 | |
| 2643 | 2639 | // The output buffer size is fixed, so it needs to be large enough |
| 2644 | 2640 | // to hold the largest possible compressed frame. We assume here |
| @@ -2647,25 +2643,16 @@ static AVBufferRef *vaapi_encode_alloc_output_buffer(void *opaque, · 2 unmodified lines | ||
| 2647 | 2643 | vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, |
| 2648 | 2644 | VAEncCodedBufferType, |
| 2649 | 2645 | 3 * ctx->surface_width * ctx->surface_height + |
| 2650 | − (1 << 16), 1, 0, &buffer_id); | |
| 2646 | + (1 << 16), 1, 0, buffer_id); | |
| 2651 | 2647 | if (vas != VA_STATUS_SUCCESS) { |
| 2652 | 2648 | av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream " |
| 2653 | 2649 | "output buffer: %d (%s).\n", vas, vaErrorStr(vas)); |
| 2654 | − return NULL; | |
| 2650 | + return AVERROR(ENOMEM); | |
| 2655 | 2651 | } |
| 2656 | 2652 | |
| 2657 | − av_log(avctx, AV_LOG_DEBUG, "Allocated output buffer %#x\n", buffer_id); | |
| 2653 | + av_log(avctx, AV_LOG_DEBUG, "Allocated output buffer %#x\n", *buffer_id); | |
| 2658 | 2654 | |
| 2659 | − ref = av_buffer_create((uint8_t*)(uintptr_t)buffer_id, | |
| 2660 | − sizeof(buffer_id), | |
| 2661 | − &vaapi_encode_free_output_buffer, | |
| 2662 | − avctx, AV_BUFFER_FLAG_READONLY); | |
| 2663 | − if (!ref) { | |
| 2664 | − vaDestroyBuffer(ctx->hwctx->display, buffer_id); | |
| 2665 | − return NULL; | |
| 2666 | − } | |
| 2667 | − | |
| 2668 | − return ref; | |
| 2655 | + return 0; | |
| 2669 | 2656 | } |
| 2670 | 2657 | |
| 2671 | 2658 | static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx) |
| @@ -2880,8 +2867,9 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx) · 208 unmodified lines | ||
| 2880 | 2867 | } |
| 2881 | 2868 | |
| 2882 | 2869 | ctx->output_buffer_pool = |
| 2883 | − av_buffer_pool_init2(sizeof(VABufferID), avctx, | |
| 2884 | − &vaapi_encode_alloc_output_buffer, NULL); | |
| 2870 | + ff_refstruct_pool_alloc_ext(sizeof(VABufferID), 0, avctx, | |
| 2871 | + &vaapi_encode_alloc_output_buffer, NULL, | |
| 2872 | + vaapi_encode_free_output_buffer, NULL); | |
| 2885 | 2873 | if (!ctx->output_buffer_pool) { |
| 2886 | 2874 | err = AVERROR(ENOMEM); |
| 2887 | 2875 | goto fail; |
| @@ -2979,7 +2967,7 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx) · 91 unmodified lines | ||
| 2979 | 2967 | vaapi_encode_free(avctx, pic); |
| 2980 | 2968 | } |
| 2981 | 2969 | |
| 2982 | − av_buffer_pool_uninit(&ctx->output_buffer_pool); | |
| 2970 | + ff_refstruct_pool_uninit(&ctx->output_buffer_pool); | |
| 2983 | 2971 | |
| 2984 | 2972 | if (ctx->va_context != VA_INVALID_ID) { |
| 2985 | 2973 | vaDestroyContext(ctx->hwctx->display, ctx->va_context); |
| @@ -103,7 +103,8 @@ typedef struct VAAPIEncodePicture { · 102 unmodified lines | ||
| 103 | 103 | int nb_param_buffers; |
| 104 | 104 | VABufferID *param_buffers; |
| 105 | 105 | |
| 106 | − AVBufferRef *output_buffer_ref; | |
| 106 | + /* Refcounted via the refstruct-API */ | |
| 107 | + VABufferID *output_buffer_ref; | |
| 107 | 108 | VABufferID output_buffer; |
| 108 | 109 | |
| 109 | 110 | void *priv_data; |
| @@ -275,7 +276,7 @@ typedef struct VAAPIEncodeContext { · 165 unmodified lines | ||
| 275 | 276 | AVHWFramesContext *recon_frames; |
| 276 | 277 | |
| 277 | 278 | // Pool of (reusable) bitstream output buffers. |
| 278 | − AVBufferPool *output_buffer_pool; | |
| 279 | + struct FFRefStructPool *output_buffer_pool; | |
| 279 | 280 | |
| 280 | 281 | // Global parameters which will be applied at the start of the |
| 281 | 282 | // sequence (includes rate control parameters below). |
| @@ -383,8 +384,11 @@ typedef struct VAAPIEncodeContext { · 101 unmodified lines | ||
| 383 | 384 | //void *header_data; |
| 384 | 385 | //size_t header_data_size; |
| 385 | 386 | |
| 386 | − /** Buffered coded data of a pic if it is an non-independent frame. */ | |
| 387 | − AVBufferRef *coded_buffer_ref; | |
| 387 | + /** | |
| 388 | + * Buffered coded data of a pic if it is an non-independent frame. | |
| 389 | + * This is a RefStruct reference. | |
| 390 | + */ | |
| 391 | + VABufferID *coded_buffer_ref; | |
| 388 | 392 | |
| 389 | 393 | /** Tail data of a pic, now only used for av1 repeat frame header. */ |
| 390 | 394 | AVPacket *tail_pkt; |