History

avcodec/vaapi_encode: Use RefStruct pool API, stop abusing AVBuffer API

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().

Andreas Rheinhardt committed 4 years ago

Commit 92abc721 parent 8c0350f2 files · +36 −44Browse files

Unified · Split

2 files
  1. libavcodec/vaapi_encode.c +28 −40
  2. libavcodec/vaapi_encode.h +8 −4
libavcodec/vaapi_encode.c+28−40
@@ -16,11 +16,11 @@ · 15 unmodified lines
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */17 */
18 18
19#include "config_components.h"
20
21#include <inttypes.h>19#include <inttypes.h>
22#include <string.h>20#include <string.h>
23 21
22#include "config.h"
23
24#include "libavutil/avassert.h"24#include "libavutil/avassert.h"
25#include "libavutil/common.h"25#include "libavutil/common.h"
26#include "libavutil/internal.h"26#include "libavutil/internal.h"
@@ -30,6 +30,7 @@ · 3 unmodified lines
30#include "vaapi_encode.h"30#include "vaapi_encode.h"
31#include "encode.h"31#include "encode.h"
32#include "avcodec.h"32#include "avcodec.h"
33#include "refstruct.h"
33 34
34const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[] = {35const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[] = {
35 HW_CONFIG_ENCODER_FRAMES(VAAPI, VAAPI),36 HW_CONFIG_ENCODER_FRAMES(VAAPI, VAAPI),
@@ -322,12 +323,12 @@ static int vaapi_encode_issue(AVCodecContext *avctx, · 286 unmodified lines
322 pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3];323 pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3];
323 av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface);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 if (!pic->output_buffer_ref) {327 if (!pic->output_buffer_ref) {
327 err = AVERROR(ENOMEM);328 err = AVERROR(ENOMEM);
328 goto fail;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 av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n",332 av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n",
332 pic->output_buffer);333 pic->output_buffer);
333 334
@@ -658,7 +659,7 @@ fail_at_end: · 324 unmodified lines
658 av_freep(&pic->slices);659 av_freep(&pic->slices);
659 av_freep(&pic->roi);660 av_freep(&pic->roi);
660 av_frame_free(&pic->recon_image);661 av_frame_free(&pic->recon_image);
661 av_buffer_unref(&pic->output_buffer_ref);662 ff_refstruct_unref(&pic->output_buffer_ref);
662 pic->output_buffer = VA_INVALID_ID;663 pic->output_buffer = VA_INVALID_ID;
663 return err;664 return err;
664}665}
@@ -780,7 +781,7 @@ static int vaapi_encode_get_coded_data(AVCodecContext *avctx, · 115 unmodified lines
780 int ret;781 int ret;
781 782
782 if (ctx->coded_buffer_ref) {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 ret = vaapi_encode_get_coded_buffer_size(avctx, output_buffer_prev);785 ret = vaapi_encode_get_coded_buffer_size(avctx, output_buffer_prev);
785 if (ret < 0)786 if (ret < 0)
786 goto end;787 goto end;
@@ -808,10 +809,8 @@ static int vaapi_encode_get_coded_data(AVCodecContext *avctx, · 21 unmodified lines
808 goto end;809 goto end;
809 810
810end:811end:
811 if (ctx->coded_buffer_ref) {812 ff_refstruct_unref(&ctx->coded_buffer_ref);
812 av_buffer_unref(&ctx->coded_buffer_ref);813 ff_refstruct_unref(&pic->output_buffer_ref);
813 }
814 av_buffer_unref(&pic->output_buffer_ref);
815 pic->output_buffer = VA_INVALID_ID;814 pic->output_buffer = VA_INVALID_ID;
816 815
817 return ret;816 return ret;
@@ -830,7 +829,7 @@ static int vaapi_encode_output(AVCodecContext *avctx, · 12 unmodified lines
830 829
831 if (pic->non_independent_frame) {830 if (pic->non_independent_frame) {
832 av_assert0(!ctx->coded_buffer_ref);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 if (pic->tail_size) {834 if (pic->tail_size) {
836 if (ctx->tail_pkt->size) {835 if (ctx->tail_pkt->size) {
@@ -857,7 +856,7 @@ static int vaapi_encode_output(AVCodecContext *avctx, · 20 unmodified lines
857 vaapi_encode_set_output_property(avctx, pic, pkt_ptr);856 vaapi_encode_set_output_property(avctx, pic, pkt_ptr);
858 857
859end:858end:
860 av_buffer_unref(&pic->output_buffer_ref);859 ff_refstruct_unref(&pic->output_buffer_ref);
861 pic->output_buffer = VA_INVALID_ID;860 pic->output_buffer = VA_INVALID_ID;
862 return err;861 return err;
863}862}
@@ -872,7 +871,7 @@ static int vaapi_encode_discard(AVCodecContext *avctx, · 8 unmodified lines
872 "%"PRId64"/%"PRId64".\n",871 "%"PRId64"/%"PRId64".\n",
873 pic->display_order, pic->encode_order);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 pic->output_buffer = VA_INVALID_ID;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 return 0;2616 return 0;
2618}2617}
2619 2618
2620static void vaapi_encode_free_output_buffer(void *opaque,2619static void vaapi_encode_free_output_buffer(FFRefStructOpaque opaque,
2621 uint8_t *data)2620 void *obj)
2622{2621{
2623 AVCodecContext *avctx = opaque;2622 AVCodecContext *avctx = opaque.nc;
2624 VAAPIEncodeContext *ctx = avctx->priv_data;2623 VAAPIEncodeContext *ctx = avctx->priv_data;
2625 VABufferID buffer_id;2624 VABufferID *buffer_id_ref = obj;
2626 2625 VABufferID buffer_id = *buffer_id_ref;
2627 buffer_id = (VABufferID)(uintptr_t)data;
2628 2626
2629 vaDestroyBuffer(ctx->hwctx->display, buffer_id);2627 vaDestroyBuffer(ctx->hwctx->display, buffer_id);
2630 2628
2631 av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id);2629 av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id);
2632}2630}
2633 2631
2634static AVBufferRef *vaapi_encode_alloc_output_buffer(void *opaque,2632static int vaapi_encode_alloc_output_buffer(FFRefStructOpaque opaque, void *obj)
2635 size_t size)
2636{2633{
2637 AVCodecContext *avctx = opaque;2634 AVCodecContext *avctx = opaque.nc;
2638 VAAPIEncodeContext *ctx = avctx->priv_data;2635 VAAPIEncodeContext *ctx = avctx->priv_data;
2639 VABufferID buffer_id;2636 VABufferID *buffer_id = obj;
2640 VAStatus vas;2637 VAStatus vas;
2641 AVBufferRef *ref;
2642 2638
2643 // The output buffer size is fixed, so it needs to be large enough2639 // The output buffer size is fixed, so it needs to be large enough
2644 // to hold the largest possible compressed frame. We assume here2640 // 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 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,2643 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
2648 VAEncCodedBufferType,2644 VAEncCodedBufferType,
2649 3 * ctx->surface_width * ctx->surface_height +2645 3 * ctx->surface_width * ctx->surface_height +
2650 (1 << 16), 1, 0, &buffer_id);2646 (1 << 16), 1, 0, buffer_id);
2651 if (vas != VA_STATUS_SUCCESS) {2647 if (vas != VA_STATUS_SUCCESS) {
2652 av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream "2648 av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream "
2653 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));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,2655 return 0;
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;
2669}2656}
2670 2657
2671static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx)2658static 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 ctx->output_buffer_pool =2869 ctx->output_buffer_pool =
2883 av_buffer_pool_init2(sizeof(VABufferID), avctx,2870 ff_refstruct_pool_alloc_ext(sizeof(VABufferID), 0, avctx,
2884 &vaapi_encode_alloc_output_buffer, NULL);2871 &vaapi_encode_alloc_output_buffer, NULL,
2872 vaapi_encode_free_output_buffer, NULL);
2885 if (!ctx->output_buffer_pool) {2873 if (!ctx->output_buffer_pool) {
2886 err = AVERROR(ENOMEM);2874 err = AVERROR(ENOMEM);
2887 goto fail;2875 goto fail;
@@ -2979,7 +2967,7 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx) · 91 unmodified lines
2979 vaapi_encode_free(avctx, pic);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 if (ctx->va_context != VA_INVALID_ID) {2972 if (ctx->va_context != VA_INVALID_ID) {
2985 vaDestroyContext(ctx->hwctx->display, ctx->va_context);2973 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
libavcodec/vaapi_encode.h+8−4
@@ -103,7 +103,8 @@ typedef struct VAAPIEncodePicture { · 102 unmodified lines
103 int nb_param_buffers;103 int nb_param_buffers;
104 VABufferID *param_buffers;104 VABufferID *param_buffers;
105 105
106 AVBufferRef *output_buffer_ref;106 /* Refcounted via the refstruct-API */
107 VABufferID *output_buffer_ref;
107 VABufferID output_buffer;108 VABufferID output_buffer;
108 109
109 void *priv_data;110 void *priv_data;
@@ -275,7 +276,7 @@ typedef struct VAAPIEncodeContext { · 165 unmodified lines
275 AVHWFramesContext *recon_frames;276 AVHWFramesContext *recon_frames;
276 277
277 // Pool of (reusable) bitstream output buffers.278 // Pool of (reusable) bitstream output buffers.
278 AVBufferPool *output_buffer_pool;279 struct FFRefStructPool *output_buffer_pool;
279 280
280 // Global parameters which will be applied at the start of the281 // Global parameters which will be applied at the start of the
281 // sequence (includes rate control parameters below).282 // sequence (includes rate control parameters below).
@@ -383,8 +384,11 @@ typedef struct VAAPIEncodeContext { · 101 unmodified lines
383 //void *header_data;384 //void *header_data;
384 //size_t header_data_size;385 //size_t header_data_size;
385 386
386 /** Buffered coded data of a pic if it is an non-independent frame. */387 /**
387 AVBufferRef *coded_buffer_ref;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 /** Tail data of a pic, now only used for av1 repeat frame header. */393 /** Tail data of a pic, now only used for av1 repeat frame header. */
390 AVPacket *tail_pkt;394 AVPacket *tail_pkt;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t