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
1616 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1717 */
1818
19−#include "config_components.h"
20−
2119 #include <inttypes.h>
2220 #include <string.h>
2321
22+#include "config.h"
23+
2424 #include "libavutil/avassert.h"
2525 #include "libavutil/common.h"
2626 #include "libavutil/internal.h"
@@ -30,6 +30,7 @@ · 3 unmodified lines
3030 #include "vaapi_encode.h"
3131 #include "encode.h"
3232 #include "avcodec.h"
33+#include "refstruct.h"
3334
3435 const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[] = {
3536 HW_CONFIG_ENCODER_FRAMES(VAAPI, VAAPI),
@@ -322,12 +323,12 @@ static int vaapi_encode_issue(AVCodecContext *avctx, · 286 unmodified lines
322323 pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3];
323324 av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface);
324325
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);
326327 if (!pic->output_buffer_ref) {
327328 err = AVERROR(ENOMEM);
328329 goto fail;
329330 }
330− pic->output_buffer = (VABufferID)(uintptr_t)pic->output_buffer_ref->data;
331+ pic->output_buffer = *pic->output_buffer_ref;
331332 av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n",
332333 pic->output_buffer);
333334
@@ -658,7 +659,7 @@ fail_at_end: · 324 unmodified lines
658659 av_freep(&pic->slices);
659660 av_freep(&pic->roi);
660661 av_frame_free(&pic->recon_image);
661− av_buffer_unref(&pic->output_buffer_ref);
662+ ff_refstruct_unref(&pic->output_buffer_ref);
662663 pic->output_buffer = VA_INVALID_ID;
663664 return err;
664665 }
@@ -780,7 +781,7 @@ static int vaapi_encode_get_coded_data(AVCodecContext *avctx, · 115 unmodified lines
780781 int ret;
781782
782783 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;
784785 ret = vaapi_encode_get_coded_buffer_size(avctx, output_buffer_prev);
785786 if (ret < 0)
786787 goto end;
@@ -808,10 +809,8 @@ static int vaapi_encode_get_coded_data(AVCodecContext *avctx, · 21 unmodified lines
808809 goto end;
809810
810811 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);
815814 pic->output_buffer = VA_INVALID_ID;
816815
817816 return ret;
@@ -830,7 +829,7 @@ static int vaapi_encode_output(AVCodecContext *avctx, · 12 unmodified lines
830829
831830 if (pic->non_independent_frame) {
832831 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);
834833
835834 if (pic->tail_size) {
836835 if (ctx->tail_pkt->size) {
@@ -857,7 +856,7 @@ static int vaapi_encode_output(AVCodecContext *avctx, · 20 unmodified lines
857856 vaapi_encode_set_output_property(avctx, pic, pkt_ptr);
858857
859858 end:
860− av_buffer_unref(&pic->output_buffer_ref);
859+ ff_refstruct_unref(&pic->output_buffer_ref);
861860 pic->output_buffer = VA_INVALID_ID;
862861 return err;
863862 }
@@ -872,7 +871,7 @@ static int vaapi_encode_discard(AVCodecContext *avctx, · 8 unmodified lines
872871 "%"PRId64"/%"PRId64".\n",
873872 pic->display_order, pic->encode_order);
874873
875− av_buffer_unref(&pic->output_buffer_ref);
874+ ff_refstruct_unref(&pic->output_buffer_ref);
876875 pic->output_buffer = VA_INVALID_ID;
877876 }
878877
@@ -2617,28 +2616,25 @@ static av_cold int vaapi_encode_init_roi(AVCodecContext *avctx) · 1738 unmodified lines
26172616 return 0;
26182617 }
26192618
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)
26222621 {
2623− AVCodecContext *avctx = opaque;
2622+ AVCodecContext *avctx = opaque.nc;
26242623 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;
26282626
26292627 vaDestroyBuffer(ctx->hwctx->display, buffer_id);
26302628
26312629 av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id);
26322630 }
26332631
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)
26362633 {
2637− AVCodecContext *avctx = opaque;
2634+ AVCodecContext *avctx = opaque.nc;
26382635 VAAPIEncodeContext *ctx = avctx->priv_data;
2639− VABufferID buffer_id;
2636+ VABufferID *buffer_id = obj;
26402637 VAStatus vas;
2641− AVBufferRef *ref;
26422638
26432639 // The output buffer size is fixed, so it needs to be large enough
26442640 // 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
26472643 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
26482644 VAEncCodedBufferType,
26492645 3 * ctx->surface_width * ctx->surface_height +
2650− (1 << 16), 1, 0, &buffer_id);
2646+ (1 << 16), 1, 0, buffer_id);
26512647 if (vas != VA_STATUS_SUCCESS) {
26522648 av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream "
26532649 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
2654− return NULL;
2650+ return AVERROR(ENOMEM);
26552651 }
26562652
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);
26582654
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;
26692656 }
26702657
26712658 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
28802867 }
28812868
28822869 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);
28852873 if (!ctx->output_buffer_pool) {
28862874 err = AVERROR(ENOMEM);
28872875 goto fail;
@@ -2979,7 +2967,7 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx) · 91 unmodified lines
29792967 vaapi_encode_free(avctx, pic);
29802968 }
29812969
2982− av_buffer_pool_uninit(&ctx->output_buffer_pool);
2970+ ff_refstruct_pool_uninit(&ctx->output_buffer_pool);
29832971
29842972 if (ctx->va_context != VA_INVALID_ID) {
29852973 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
libavcodec/vaapi_encode.h+8−4
@@ -103,7 +103,8 @@ typedef struct VAAPIEncodePicture { · 102 unmodified lines
103103 int nb_param_buffers;
104104 VABufferID *param_buffers;
105105
106− AVBufferRef *output_buffer_ref;
106+ /* Refcounted via the refstruct-API */
107+ VABufferID *output_buffer_ref;
107108 VABufferID output_buffer;
108109
109110 void *priv_data;
@@ -275,7 +276,7 @@ typedef struct VAAPIEncodeContext { · 165 unmodified lines
275276 AVHWFramesContext *recon_frames;
276277
277278 // Pool of (reusable) bitstream output buffers.
278− AVBufferPool *output_buffer_pool;
279+ struct FFRefStructPool *output_buffer_pool;
279280
280281 // Global parameters which will be applied at the start of the
281282 // sequence (includes rate control parameters below).
@@ -383,8 +384,11 @@ typedef struct VAAPIEncodeContext { · 101 unmodified lines
383384 //void *header_data;
384385 //size_t header_data_size;
385386
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;
388392
389393 /** Tail data of a pic, now only used for av1 repeat frame header. */
390394 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