Commit c8e135ea authored by Jun Zhao's avatar Jun Zhao Committed by Mark Thompson

vaapi_encode: Allocate slice structures and parameter buffers dynamically

This removes the arbitrary limit on the allowed number of slices and
parameter buffers.

From ffmpeg commit e4a6eb70.
Signed-off-by: 's avatarMark Thompson <sw@jkqxz.net>
parent 254e728d
...@@ -36,13 +36,17 @@ static int vaapi_encode_make_packed_header(AVCodecContext *avctx, ...@@ -36,13 +36,17 @@ static int vaapi_encode_make_packed_header(AVCodecContext *avctx,
VAAPIEncodeContext *ctx = avctx->priv_data; VAAPIEncodeContext *ctx = avctx->priv_data;
VAStatus vas; VAStatus vas;
VABufferID param_buffer, data_buffer; VABufferID param_buffer, data_buffer;
VABufferID *tmp;
VAEncPackedHeaderParameterBuffer params = { VAEncPackedHeaderParameterBuffer params = {
.type = type, .type = type,
.bit_length = bit_len, .bit_length = bit_len,
.has_emulation_bytes = 1, .has_emulation_bytes = 1,
}; };
av_assert0(pic->nb_param_buffers + 2 <= MAX_PARAM_BUFFERS); tmp = av_realloc_array(pic->param_buffers, sizeof(*tmp), pic->nb_param_buffers + 2);
if (!tmp)
return AVERROR(ENOMEM);
pic->param_buffers = tmp;
vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
VAEncPackedHeaderParameterBufferType, VAEncPackedHeaderParameterBufferType,
...@@ -77,9 +81,13 @@ static int vaapi_encode_make_param_buffer(AVCodecContext *avctx, ...@@ -77,9 +81,13 @@ static int vaapi_encode_make_param_buffer(AVCodecContext *avctx,
{ {
VAAPIEncodeContext *ctx = avctx->priv_data; VAAPIEncodeContext *ctx = avctx->priv_data;
VAStatus vas; VAStatus vas;
VABufferID *tmp;
VABufferID buffer; VABufferID buffer;
av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS); tmp = av_realloc_array(pic->param_buffers, sizeof(*tmp), pic->nb_param_buffers + 1);
if (!tmp)
return AVERROR(ENOMEM);
pic->param_buffers = tmp;
vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
type, len, 1, data, &buffer); type, len, 1, data, &buffer);
...@@ -313,15 +321,16 @@ static int vaapi_encode_issue(AVCodecContext *avctx, ...@@ -313,15 +321,16 @@ static int vaapi_encode_issue(AVCodecContext *avctx,
} }
} }
av_assert0(pic->nb_slices <= MAX_PICTURE_SLICES); if (pic->nb_slices > 0) {
for (i = 0; i < pic->nb_slices; i++) { pic->slices = av_mallocz_array(pic->nb_slices, sizeof(*pic->slices));
slice = av_mallocz(sizeof(*slice)); if (!pic->slices) {
if (!slice) {
err = AVERROR(ENOMEM); err = AVERROR(ENOMEM);
goto fail; goto fail;
} }
}
for (i = 0; i < pic->nb_slices; i++) {
slice = &pic->slices[i];
slice->index = i; slice->index = i;
pic->slices[i] = slice;
if (ctx->codec->slice_params_size > 0) { if (ctx->codec->slice_params_size > 0) {
slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size); slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size);
...@@ -425,8 +434,16 @@ fail_with_picture: ...@@ -425,8 +434,16 @@ fail_with_picture:
fail: fail:
for(i = 0; i < pic->nb_param_buffers; i++) for(i = 0; i < pic->nb_param_buffers; i++)
vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]); vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
for (i = 0; i < pic->nb_slices; i++) {
if (pic->slices) {
av_freep(&pic->slices[i].priv_data);
av_freep(&pic->slices[i].codec_slice_params);
}
}
fail_at_end: fail_at_end:
av_freep(&pic->codec_picture_params); av_freep(&pic->codec_picture_params);
av_freep(&pic->param_buffers);
av_freep(&pic->slices);
av_frame_free(&pic->recon_image); av_frame_free(&pic->recon_image);
av_buffer_unref(&pic->output_buffer_ref); av_buffer_unref(&pic->output_buffer_ref);
pic->output_buffer = VA_INVALID_ID; pic->output_buffer = VA_INVALID_ID;
...@@ -535,15 +552,18 @@ static int vaapi_encode_free(AVCodecContext *avctx, ...@@ -535,15 +552,18 @@ static int vaapi_encode_free(AVCodecContext *avctx,
vaapi_encode_discard(avctx, pic); vaapi_encode_discard(avctx, pic);
for (i = 0; i < pic->nb_slices; i++) { for (i = 0; i < pic->nb_slices; i++) {
av_freep(&pic->slices[i]->priv_data); if (pic->slices) {
av_freep(&pic->slices[i]->codec_slice_params); av_freep(&pic->slices[i].priv_data);
av_freep(&pic->slices[i]); av_freep(&pic->slices[i].codec_slice_params);
}
} }
av_freep(&pic->codec_picture_params); av_freep(&pic->codec_picture_params);
av_frame_free(&pic->input_image); av_frame_free(&pic->input_image);
av_frame_free(&pic->recon_image); av_frame_free(&pic->recon_image);
av_freep(&pic->param_buffers);
av_freep(&pic->slices);
// Output buffer should already be destroyed. // Output buffer should already be destroyed.
av_assert0(pic->output_buffer == VA_INVALID_ID); av_assert0(pic->output_buffer == VA_INVALID_ID);
......
...@@ -35,8 +35,6 @@ enum { ...@@ -35,8 +35,6 @@ enum {
MAX_CONFIG_ATTRIBUTES = 4, MAX_CONFIG_ATTRIBUTES = 4,
MAX_GLOBAL_PARAMS = 4, MAX_GLOBAL_PARAMS = 4,
MAX_PICTURE_REFERENCES = 2, MAX_PICTURE_REFERENCES = 2,
MAX_PICTURE_SLICES = 112,
MAX_PARAM_BUFFERS = 128,
MAX_REORDER_DELAY = 16, MAX_REORDER_DELAY = 16,
MAX_PARAM_BUFFER_SIZE = 1024, MAX_PARAM_BUFFER_SIZE = 1024,
}; };
...@@ -73,7 +71,7 @@ typedef struct VAAPIEncodePicture { ...@@ -73,7 +71,7 @@ typedef struct VAAPIEncodePicture {
VASurfaceID recon_surface; VASurfaceID recon_surface;
int nb_param_buffers; int nb_param_buffers;
VABufferID param_buffers[MAX_PARAM_BUFFERS]; VABufferID *param_buffers;
AVBufferRef *output_buffer_ref; AVBufferRef *output_buffer_ref;
VABufferID output_buffer; VABufferID output_buffer;
...@@ -85,7 +83,7 @@ typedef struct VAAPIEncodePicture { ...@@ -85,7 +83,7 @@ typedef struct VAAPIEncodePicture {
struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES]; struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
int nb_slices; int nb_slices;
VAAPIEncodeSlice *slices[MAX_PICTURE_SLICES]; VAAPIEncodeSlice *slices;
} VAAPIEncodePicture; } VAAPIEncodePicture;
typedef struct VAAPIEncodeContext { typedef struct VAAPIEncodeContext {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment