2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avassert.h"
23 #include "libavutil/common.h"
24 #include "libavutil/log.h"
25 #include "libavutil/pixdesc.h"
27 #include "vaapi_encode.h"
31 const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[] = {
32 HW_CONFIG_ENCODER_FRAMES(VAAPI, VAAPI),
36 static const char * const picture_type_name[] = { "IDR", "I", "P", "B" };
38 static int vaapi_encode_make_packed_header(AVCodecContext *avctx,
39 VAAPIEncodePicture *pic,
40 int type, char *data, size_t bit_len)
42 VAAPIEncodeContext *ctx = avctx->priv_data;
44 VABufferID param_buffer, data_buffer;
46 VAEncPackedHeaderParameterBuffer params = {
48 .bit_length = bit_len,
49 .has_emulation_bytes = 1,
52 tmp = av_realloc_array(pic->param_buffers, sizeof(*tmp), pic->nb_param_buffers + 2);
54 return AVERROR(ENOMEM);
55 pic->param_buffers = tmp;
57 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
58 VAEncPackedHeaderParameterBufferType,
59 sizeof(params), 1, ¶ms, ¶m_buffer);
60 if (vas != VA_STATUS_SUCCESS) {
61 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
62 "for packed header (type %d): %d (%s).\n",
63 type, vas, vaErrorStr(vas));
66 pic->param_buffers[pic->nb_param_buffers++] = param_buffer;
68 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
69 VAEncPackedHeaderDataBufferType,
70 (bit_len + 7) / 8, 1, data, &data_buffer);
71 if (vas != VA_STATUS_SUCCESS) {
72 av_log(avctx, AV_LOG_ERROR, "Failed to create data buffer "
73 "for packed header (type %d): %d (%s).\n",
74 type, vas, vaErrorStr(vas));
77 pic->param_buffers[pic->nb_param_buffers++] = data_buffer;
79 av_log(avctx, AV_LOG_DEBUG, "Packed header buffer (%d) is %#x/%#x "
80 "(%zu bits).\n", type, param_buffer, data_buffer, bit_len);
84 static int vaapi_encode_make_param_buffer(AVCodecContext *avctx,
85 VAAPIEncodePicture *pic,
86 int type, char *data, size_t len)
88 VAAPIEncodeContext *ctx = avctx->priv_data;
93 tmp = av_realloc_array(pic->param_buffers, sizeof(*tmp), pic->nb_param_buffers + 1);
95 return AVERROR(ENOMEM);
96 pic->param_buffers = tmp;
98 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
99 type, len, 1, data, &buffer);
100 if (vas != VA_STATUS_SUCCESS) {
101 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
102 "(type %d): %d (%s).\n", type, vas, vaErrorStr(vas));
105 pic->param_buffers[pic->nb_param_buffers++] = buffer;
107 av_log(avctx, AV_LOG_DEBUG, "Param buffer (%d) is %#x.\n",
112 static int vaapi_encode_make_misc_param_buffer(AVCodecContext *avctx,
113 VAAPIEncodePicture *pic,
115 const void *data, size_t len)
117 // Construct the buffer on the stack - 1KB is much larger than any
118 // current misc parameter buffer type (the largest is EncQuality at
120 uint8_t buffer[1024];
121 VAEncMiscParameterBuffer header = {
124 size_t buffer_size = sizeof(header) + len;
125 av_assert0(buffer_size <= sizeof(buffer));
127 memcpy(buffer, &header, sizeof(header));
128 memcpy(buffer + sizeof(header), data, len);
130 return vaapi_encode_make_param_buffer(avctx, pic,
131 VAEncMiscParameterBufferType,
132 buffer, buffer_size);
135 static int vaapi_encode_wait(AVCodecContext *avctx,
136 VAAPIEncodePicture *pic)
138 VAAPIEncodeContext *ctx = avctx->priv_data;
141 av_assert0(pic->encode_issued);
143 if (pic->encode_complete) {
144 // Already waited for this picture.
148 av_log(avctx, AV_LOG_DEBUG, "Sync to pic %"PRId64"/%"PRId64" "
149 "(input surface %#x).\n", pic->display_order,
150 pic->encode_order, pic->input_surface);
152 vas = vaSyncSurface(ctx->hwctx->display, pic->input_surface);
153 if (vas != VA_STATUS_SUCCESS) {
154 av_log(avctx, AV_LOG_ERROR, "Failed to sync to picture completion: "
155 "%d (%s).\n", vas, vaErrorStr(vas));
159 // Input is definitely finished with now.
160 av_frame_free(&pic->input_image);
162 pic->encode_complete = 1;
166 static int vaapi_encode_make_row_slice(AVCodecContext *avctx,
167 VAAPIEncodePicture *pic)
169 VAAPIEncodeContext *ctx = avctx->priv_data;
170 VAAPIEncodeSlice *slice;
173 for (i = 0; i < pic->nb_slices; i++)
174 pic->slices[i].row_size = ctx->slice_size;
176 rounding = ctx->slice_block_rows - ctx->nb_slices * ctx->slice_size;
178 // Place rounding error at top and bottom of frame.
179 av_assert0(rounding < pic->nb_slices);
180 // Some Intel drivers contain a bug where the encoder will fail
181 // if the last slice is smaller than the one before it. Since
182 // that's straightforward to avoid here, just do so.
184 for (i = 0; i < rounding; i++)
185 ++pic->slices[i].row_size;
187 for (i = 0; i < (rounding + 1) / 2; i++)
188 ++pic->slices[pic->nb_slices - i - 1].row_size;
189 for (i = 0; i < rounding / 2; i++)
190 ++pic->slices[i].row_size;
192 } else if (rounding < 0) {
193 // Remove rounding error from last slice only.
194 av_assert0(rounding < ctx->slice_size);
195 pic->slices[pic->nb_slices - 1].row_size += rounding;
198 for (i = 0; i < pic->nb_slices; i++) {
199 slice = &pic->slices[i];
202 slice->row_start = 0;
203 slice->block_start = 0;
205 const VAAPIEncodeSlice *prev = &pic->slices[i - 1];
206 slice->row_start = prev->row_start + prev->row_size;
207 slice->block_start = prev->block_start + prev->block_size;
209 slice->block_size = slice->row_size * ctx->slice_block_cols;
211 av_log(avctx, AV_LOG_DEBUG, "Slice %d: %d-%d (%d rows), "
212 "%d-%d (%d blocks).\n", i, slice->row_start,
213 slice->row_start + slice->row_size - 1, slice->row_size,
214 slice->block_start, slice->block_start + slice->block_size - 1,
221 static int vaapi_encode_make_tile_slice(AVCodecContext *avctx,
222 VAAPIEncodePicture *pic)
224 VAAPIEncodeContext *ctx = avctx->priv_data;
225 VAAPIEncodeSlice *slice;
228 for (i = 0; i < ctx->tile_cols; i++) {
229 for (j = 0; j < ctx->tile_rows; j++) {
230 index = j * ctx->tile_cols + i;
231 slice = &pic->slices[index];
232 slice->index = index;
234 pic->slices[index].block_start = ctx->col_bd[i] +
235 ctx->row_bd[j] * ctx->slice_block_cols;
236 pic->slices[index].block_size = ctx->row_height[j] * ctx->col_width[i];
238 av_log(avctx, AV_LOG_DEBUG, "Slice %2d: (%2d, %2d) start at: %4d "
239 "width:%2d height:%2d (%d blocks).\n", index, ctx->col_bd[i],
240 ctx->row_bd[j], slice->block_start, ctx->col_width[i],
241 ctx->row_height[j], slice->block_size);
248 static int vaapi_encode_issue(AVCodecContext *avctx,
249 VAAPIEncodePicture *pic)
251 VAAPIEncodeContext *ctx = avctx->priv_data;
252 VAAPIEncodeSlice *slice;
255 char data[MAX_PARAM_BUFFER_SIZE];
257 av_unused AVFrameSideData *sd;
259 av_log(avctx, AV_LOG_DEBUG, "Issuing encode for pic %"PRId64"/%"PRId64" "
260 "as type %s.\n", pic->display_order, pic->encode_order,
261 picture_type_name[pic->type]);
262 if (pic->nb_refs == 0) {
263 av_log(avctx, AV_LOG_DEBUG, "No reference pictures.\n");
265 av_log(avctx, AV_LOG_DEBUG, "Refers to:");
266 for (i = 0; i < pic->nb_refs; i++) {
267 av_log(avctx, AV_LOG_DEBUG, " %"PRId64"/%"PRId64,
268 pic->refs[i]->display_order, pic->refs[i]->encode_order);
270 av_log(avctx, AV_LOG_DEBUG, ".\n");
273 av_assert0(!pic->encode_issued);
274 for (i = 0; i < pic->nb_refs; i++) {
275 av_assert0(pic->refs[i]);
276 av_assert0(pic->refs[i]->encode_issued);
279 av_log(avctx, AV_LOG_DEBUG, "Input surface is %#x.\n", pic->input_surface);
281 pic->recon_image = av_frame_alloc();
282 if (!pic->recon_image) {
283 err = AVERROR(ENOMEM);
287 err = av_hwframe_get_buffer(ctx->recon_frames_ref, pic->recon_image, 0);
289 err = AVERROR(ENOMEM);
292 pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3];
293 av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface);
295 pic->output_buffer_ref = av_buffer_pool_get(ctx->output_buffer_pool);
296 if (!pic->output_buffer_ref) {
297 err = AVERROR(ENOMEM);
300 pic->output_buffer = (VABufferID)(uintptr_t)pic->output_buffer_ref->data;
301 av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n",
304 if (ctx->codec->picture_params_size > 0) {
305 pic->codec_picture_params = av_malloc(ctx->codec->picture_params_size);
306 if (!pic->codec_picture_params)
308 memcpy(pic->codec_picture_params, ctx->codec_picture_params,
309 ctx->codec->picture_params_size);
311 av_assert0(!ctx->codec_picture_params);
314 pic->nb_param_buffers = 0;
316 if (pic->type == PICTURE_TYPE_IDR && ctx->codec->init_sequence_params) {
317 err = vaapi_encode_make_param_buffer(avctx, pic,
318 VAEncSequenceParameterBufferType,
319 ctx->codec_sequence_params,
320 ctx->codec->sequence_params_size);
325 if (pic->type == PICTURE_TYPE_IDR) {
326 for (i = 0; i < ctx->nb_global_params; i++) {
327 err = vaapi_encode_make_misc_param_buffer(avctx, pic,
328 ctx->global_params_type[i],
329 ctx->global_params[i],
330 ctx->global_params_size[i]);
336 if (ctx->codec->init_picture_params) {
337 err = ctx->codec->init_picture_params(avctx, pic);
339 av_log(avctx, AV_LOG_ERROR, "Failed to initialise picture "
340 "parameters: %d.\n", err);
343 err = vaapi_encode_make_param_buffer(avctx, pic,
344 VAEncPictureParameterBufferType,
345 pic->codec_picture_params,
346 ctx->codec->picture_params_size);
351 if (pic->type == PICTURE_TYPE_IDR) {
352 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
353 ctx->codec->write_sequence_header) {
354 bit_len = 8 * sizeof(data);
355 err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
357 av_log(avctx, AV_LOG_ERROR, "Failed to write per-sequence "
358 "header: %d.\n", err);
361 err = vaapi_encode_make_packed_header(avctx, pic,
362 ctx->codec->sequence_header_type,
369 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_PICTURE &&
370 ctx->codec->write_picture_header) {
371 bit_len = 8 * sizeof(data);
372 err = ctx->codec->write_picture_header(avctx, pic, data, &bit_len);
374 av_log(avctx, AV_LOG_ERROR, "Failed to write per-picture "
375 "header: %d.\n", err);
378 err = vaapi_encode_make_packed_header(avctx, pic,
379 ctx->codec->picture_header_type,
385 if (ctx->codec->write_extra_buffer) {
387 size_t len = sizeof(data);
389 err = ctx->codec->write_extra_buffer(avctx, pic, i, &type,
391 if (err == AVERROR_EOF)
394 av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
395 "buffer %d: %d.\n", i, err);
399 err = vaapi_encode_make_param_buffer(avctx, pic, type,
406 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_MISC &&
407 ctx->codec->write_extra_header) {
410 bit_len = 8 * sizeof(data);
411 err = ctx->codec->write_extra_header(avctx, pic, i, &type,
413 if (err == AVERROR_EOF)
416 av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
417 "header %d: %d.\n", i, err);
421 err = vaapi_encode_make_packed_header(avctx, pic, type,
428 if (pic->nb_slices == 0)
429 pic->nb_slices = ctx->nb_slices;
430 if (pic->nb_slices > 0) {
431 pic->slices = av_mallocz_array(pic->nb_slices, sizeof(*pic->slices));
433 err = AVERROR(ENOMEM);
437 if (ctx->tile_rows && ctx->tile_cols)
438 vaapi_encode_make_tile_slice(avctx, pic);
440 vaapi_encode_make_row_slice(avctx, pic);
443 for (i = 0; i < pic->nb_slices; i++) {
444 slice = &pic->slices[i];
446 if (ctx->codec->slice_params_size > 0) {
447 slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size);
448 if (!slice->codec_slice_params) {
449 err = AVERROR(ENOMEM);
454 if (ctx->codec->init_slice_params) {
455 err = ctx->codec->init_slice_params(avctx, pic, slice);
457 av_log(avctx, AV_LOG_ERROR, "Failed to initialise slice "
458 "parameters: %d.\n", err);
463 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SLICE &&
464 ctx->codec->write_slice_header) {
465 bit_len = 8 * sizeof(data);
466 err = ctx->codec->write_slice_header(avctx, pic, slice,
469 av_log(avctx, AV_LOG_ERROR, "Failed to write per-slice "
470 "header: %d.\n", err);
473 err = vaapi_encode_make_packed_header(avctx, pic,
474 ctx->codec->slice_header_type,
480 if (ctx->codec->init_slice_params) {
481 err = vaapi_encode_make_param_buffer(avctx, pic,
482 VAEncSliceParameterBufferType,
483 slice->codec_slice_params,
484 ctx->codec->slice_params_size);
490 #if VA_CHECK_VERSION(1, 0, 0)
491 sd = av_frame_get_side_data(pic->input_image,
492 AV_FRAME_DATA_REGIONS_OF_INTEREST);
493 if (sd && ctx->roi_allowed) {
494 const AVRegionOfInterest *roi;
496 VAEncMiscParameterBufferROI param_roi;
499 roi = (const AVRegionOfInterest*)sd->data;
500 roi_size = roi->self_size;
501 av_assert0(roi_size && sd->size % roi_size == 0);
502 nb_roi = sd->size / roi_size;
503 if (nb_roi > ctx->roi_max_regions) {
504 if (!ctx->roi_warned) {
505 av_log(avctx, AV_LOG_WARNING, "More ROIs set than "
506 "supported by driver (%d > %d).\n",
507 nb_roi, ctx->roi_max_regions);
510 nb_roi = ctx->roi_max_regions;
513 pic->roi = av_mallocz_array(nb_roi, sizeof(*pic->roi));
515 err = AVERROR(ENOMEM);
518 // For overlapping regions, the first in the array takes priority.
519 for (i = 0; i < nb_roi; i++) {
520 roi = (const AVRegionOfInterest*)(sd->data + roi_size * i);
522 av_assert0(roi->qoffset.den != 0);
523 v = roi->qoffset.num * ctx->roi_quant_range / roi->qoffset.den;
524 av_log(avctx, AV_LOG_DEBUG, "ROI: (%d,%d)-(%d,%d) -> %+d.\n",
525 roi->top, roi->left, roi->bottom, roi->right, v);
527 pic->roi[i] = (VAEncROI) {
531 .width = roi->right - roi->left,
532 .height = roi->bottom - roi->top,
534 .roi_value = av_clip_int8(v),
538 param_roi = (VAEncMiscParameterBufferROI) {
540 .max_delta_qp = INT8_MAX,
541 .min_delta_qp = INT8_MIN,
543 .roi_flags.bits.roi_value_is_qp_delta = 1,
546 err = vaapi_encode_make_misc_param_buffer(avctx, pic,
547 VAEncMiscParameterTypeROI,
555 vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
557 if (vas != VA_STATUS_SUCCESS) {
558 av_log(avctx, AV_LOG_ERROR, "Failed to begin picture encode issue: "
559 "%d (%s).\n", vas, vaErrorStr(vas));
561 goto fail_with_picture;
564 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
565 pic->param_buffers, pic->nb_param_buffers);
566 if (vas != VA_STATUS_SUCCESS) {
567 av_log(avctx, AV_LOG_ERROR, "Failed to upload encode parameters: "
568 "%d (%s).\n", vas, vaErrorStr(vas));
570 goto fail_with_picture;
573 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
574 if (vas != VA_STATUS_SUCCESS) {
575 av_log(avctx, AV_LOG_ERROR, "Failed to end picture encode issue: "
576 "%d (%s).\n", vas, vaErrorStr(vas));
578 // vaRenderPicture() has been called here, so we should not destroy
579 // the parameter buffers unless separate destruction is required.
580 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
581 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
587 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
588 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
589 for (i = 0; i < pic->nb_param_buffers; i++) {
590 vas = vaDestroyBuffer(ctx->hwctx->display,
591 pic->param_buffers[i]);
592 if (vas != VA_STATUS_SUCCESS) {
593 av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
594 "param buffer %#x: %d (%s).\n",
595 pic->param_buffers[i], vas, vaErrorStr(vas));
601 pic->encode_issued = 1;
606 vaEndPicture(ctx->hwctx->display, ctx->va_context);
608 for(i = 0; i < pic->nb_param_buffers; i++)
609 vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
611 for (i = 0; i < pic->nb_slices; i++)
612 av_freep(&pic->slices[i].codec_slice_params);
615 av_freep(&pic->codec_picture_params);
616 av_freep(&pic->param_buffers);
617 av_freep(&pic->slices);
619 av_frame_free(&pic->recon_image);
620 av_buffer_unref(&pic->output_buffer_ref);
621 pic->output_buffer = VA_INVALID_ID;
625 static int vaapi_encode_output(AVCodecContext *avctx,
626 VAAPIEncodePicture *pic, AVPacket *pkt)
628 VAAPIEncodeContext *ctx = avctx->priv_data;
629 VACodedBufferSegment *buf_list, *buf;
635 err = vaapi_encode_wait(avctx, pic);
640 vas = vaMapBuffer(ctx->hwctx->display, pic->output_buffer,
642 if (vas != VA_STATUS_SUCCESS) {
643 av_log(avctx, AV_LOG_ERROR, "Failed to map output buffers: "
644 "%d (%s).\n", vas, vaErrorStr(vas));
649 for (buf = buf_list; buf; buf = buf->next)
650 total_size += buf->size;
652 err = av_new_packet(pkt, total_size);
658 for (buf = buf_list; buf; buf = buf->next) {
659 av_log(avctx, AV_LOG_DEBUG, "Output buffer: %u bytes "
660 "(status %08x).\n", buf->size, buf->status);
662 memcpy(ptr, buf->buf, buf->size);
666 if (pic->type == PICTURE_TYPE_IDR)
667 pkt->flags |= AV_PKT_FLAG_KEY;
671 vas = vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
672 if (vas != VA_STATUS_SUCCESS) {
673 av_log(avctx, AV_LOG_ERROR, "Failed to unmap output buffers: "
674 "%d (%s).\n", vas, vaErrorStr(vas));
679 av_buffer_unref(&pic->output_buffer_ref);
680 pic->output_buffer = VA_INVALID_ID;
682 av_log(avctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n",
683 pic->display_order, pic->encode_order);
687 vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
689 av_buffer_unref(&pic->output_buffer_ref);
690 pic->output_buffer = VA_INVALID_ID;
694 static int vaapi_encode_discard(AVCodecContext *avctx,
695 VAAPIEncodePicture *pic)
697 vaapi_encode_wait(avctx, pic);
699 if (pic->output_buffer_ref) {
700 av_log(avctx, AV_LOG_DEBUG, "Discard output for pic "
701 "%"PRId64"/%"PRId64".\n",
702 pic->display_order, pic->encode_order);
704 av_buffer_unref(&pic->output_buffer_ref);
705 pic->output_buffer = VA_INVALID_ID;
711 static VAAPIEncodePicture *vaapi_encode_alloc(AVCodecContext *avctx)
713 VAAPIEncodeContext *ctx = avctx->priv_data;
714 VAAPIEncodePicture *pic;
716 pic = av_mallocz(sizeof(*pic));
720 if (ctx->codec->picture_priv_data_size > 0) {
721 pic->priv_data = av_mallocz(ctx->codec->picture_priv_data_size);
722 if (!pic->priv_data) {
728 pic->input_surface = VA_INVALID_ID;
729 pic->recon_surface = VA_INVALID_ID;
730 pic->output_buffer = VA_INVALID_ID;
735 static int vaapi_encode_free(AVCodecContext *avctx,
736 VAAPIEncodePicture *pic)
740 if (pic->encode_issued)
741 vaapi_encode_discard(avctx, pic);
744 for (i = 0; i < pic->nb_slices; i++)
745 av_freep(&pic->slices[i].codec_slice_params);
747 av_freep(&pic->codec_picture_params);
749 av_frame_free(&pic->input_image);
750 av_frame_free(&pic->recon_image);
752 av_freep(&pic->param_buffers);
753 av_freep(&pic->slices);
754 // Output buffer should already be destroyed.
755 av_assert0(pic->output_buffer == VA_INVALID_ID);
757 av_freep(&pic->priv_data);
758 av_freep(&pic->codec_picture_params);
766 static void vaapi_encode_add_ref(AVCodecContext *avctx,
767 VAAPIEncodePicture *pic,
768 VAAPIEncodePicture *target,
769 int is_ref, int in_dpb, int prev)
774 av_assert0(pic != target);
775 av_assert0(pic->nb_refs < MAX_PICTURE_REFERENCES);
776 pic->refs[pic->nb_refs++] = target;
781 av_assert0(pic->nb_dpb_pics < MAX_DPB_SIZE);
782 pic->dpb[pic->nb_dpb_pics++] = target;
787 av_assert0(!pic->prev);
792 target->ref_count[0] += refs;
793 target->ref_count[1] += refs;
796 static void vaapi_encode_remove_refs(AVCodecContext *avctx,
797 VAAPIEncodePicture *pic,
802 if (pic->ref_removed[level])
805 for (i = 0; i < pic->nb_refs; i++) {
806 av_assert0(pic->refs[i]);
807 --pic->refs[i]->ref_count[level];
808 av_assert0(pic->refs[i]->ref_count[level] >= 0);
811 for (i = 0; i < pic->nb_dpb_pics; i++) {
812 av_assert0(pic->dpb[i]);
813 --pic->dpb[i]->ref_count[level];
814 av_assert0(pic->dpb[i]->ref_count[level] >= 0);
817 av_assert0(pic->prev || pic->type == PICTURE_TYPE_IDR);
819 --pic->prev->ref_count[level];
820 av_assert0(pic->prev->ref_count[level] >= 0);
823 pic->ref_removed[level] = 1;
826 static void vaapi_encode_set_b_pictures(AVCodecContext *avctx,
827 VAAPIEncodePicture *start,
828 VAAPIEncodePicture *end,
829 VAAPIEncodePicture *prev,
831 VAAPIEncodePicture **last)
833 VAAPIEncodeContext *ctx = avctx->priv_data;
834 VAAPIEncodePicture *pic, *next, *ref;
837 av_assert0(start && end && start != end && start->next != end);
839 // If we are at the maximum depth then encode all pictures as
840 // non-referenced B-pictures. Also do this if there is exactly one
841 // picture left, since there will be nothing to reference it.
842 if (current_depth == ctx->max_b_depth || start->next->next == end) {
843 for (pic = start->next; pic; pic = pic->next) {
846 pic->type = PICTURE_TYPE_B;
847 pic->b_depth = current_depth;
849 vaapi_encode_add_ref(avctx, pic, start, 1, 1, 0);
850 vaapi_encode_add_ref(avctx, pic, end, 1, 1, 0);
851 vaapi_encode_add_ref(avctx, pic, prev, 0, 0, 1);
853 for (ref = end->refs[1]; ref; ref = ref->refs[1])
854 vaapi_encode_add_ref(avctx, pic, ref, 0, 1, 0);
859 // Split the current list at the midpoint with a referenced
860 // B-picture, then descend into each side separately.
862 for (pic = start->next; pic != end; pic = pic->next)
864 for (pic = start->next, i = 1; 2 * i < len; pic = pic->next, i++);
866 pic->type = PICTURE_TYPE_B;
867 pic->b_depth = current_depth;
869 pic->is_reference = 1;
871 vaapi_encode_add_ref(avctx, pic, pic, 0, 1, 0);
872 vaapi_encode_add_ref(avctx, pic, start, 1, 1, 0);
873 vaapi_encode_add_ref(avctx, pic, end, 1, 1, 0);
874 vaapi_encode_add_ref(avctx, pic, prev, 0, 0, 1);
876 for (ref = end->refs[1]; ref; ref = ref->refs[1])
877 vaapi_encode_add_ref(avctx, pic, ref, 0, 1, 0);
880 vaapi_encode_set_b_pictures(avctx, start, pic, pic,
881 current_depth + 1, &next);
885 vaapi_encode_set_b_pictures(avctx, pic, end, next,
886 current_depth + 1, last);
890 static int vaapi_encode_pick_next(AVCodecContext *avctx,
891 VAAPIEncodePicture **pic_out)
893 VAAPIEncodeContext *ctx = avctx->priv_data;
894 VAAPIEncodePicture *pic = NULL, *next, *start;
895 int i, b_counter, closed_gop_end;
897 // If there are any B-frames already queued, the next one to encode
898 // is the earliest not-yet-issued frame for which all references are
900 for (pic = ctx->pic_start; pic; pic = pic->next) {
901 if (pic->encode_issued)
903 if (pic->type != PICTURE_TYPE_B)
905 for (i = 0; i < pic->nb_refs; i++) {
906 if (!pic->refs[i]->encode_issued)
909 if (i == pic->nb_refs)
914 av_log(avctx, AV_LOG_DEBUG, "Pick B-picture at depth %d to "
915 "encode next.\n", pic->b_depth);
920 // Find the B-per-Pth available picture to become the next picture
924 closed_gop_end = ctx->closed_gop ||
925 ctx->idr_counter == ctx->gop_per_idr;
926 for (pic = ctx->pic_start; pic; pic = next) {
928 if (pic->encode_issued) {
932 // If the next available picture is force-IDR, encode it to start
933 // a new GOP immediately.
936 if (b_counter == ctx->b_per_p)
938 // If this picture ends a closed GOP or starts a new GOP then it
939 // needs to be in the top layer.
940 if (ctx->gop_counter + b_counter + closed_gop_end >= ctx->gop_size)
942 // If the picture after this one is force-IDR, we need to encode
943 // this one in the top layer.
944 if (next && next->force_idr)
949 // At the end of the stream the last picture must be in the top layer.
950 if (!pic && ctx->end_of_stream) {
953 if (pic->encode_issued)
958 av_log(avctx, AV_LOG_DEBUG, "Pick nothing to encode next - "
959 "need more input for reference pictures.\n");
960 return AVERROR(EAGAIN);
962 if (ctx->input_order <= ctx->decode_delay && !ctx->end_of_stream) {
963 av_log(avctx, AV_LOG_DEBUG, "Pick nothing to encode next - "
964 "need more input for timestamps.\n");
965 return AVERROR(EAGAIN);
968 if (pic->force_idr) {
969 av_log(avctx, AV_LOG_DEBUG, "Pick forced IDR-picture to "
971 pic->type = PICTURE_TYPE_IDR;
972 ctx->idr_counter = 1;
973 ctx->gop_counter = 1;
975 } else if (ctx->gop_counter + b_counter >= ctx->gop_size) {
976 if (ctx->idr_counter == ctx->gop_per_idr) {
977 av_log(avctx, AV_LOG_DEBUG, "Pick new-GOP IDR-picture to "
979 pic->type = PICTURE_TYPE_IDR;
980 ctx->idr_counter = 1;
982 av_log(avctx, AV_LOG_DEBUG, "Pick new-GOP I-picture to "
984 pic->type = PICTURE_TYPE_I;
987 ctx->gop_counter = 1;
990 if (ctx->gop_counter + b_counter + closed_gop_end == ctx->gop_size) {
991 av_log(avctx, AV_LOG_DEBUG, "Pick group-end P-picture to "
994 av_log(avctx, AV_LOG_DEBUG, "Pick normal P-picture to "
997 pic->type = PICTURE_TYPE_P;
999 ctx->gop_counter += 1 + b_counter;
1001 pic->is_reference = 1;
1004 vaapi_encode_add_ref(avctx, pic, pic, 0, 1, 0);
1005 if (pic->type != PICTURE_TYPE_IDR) {
1006 vaapi_encode_add_ref(avctx, pic, start,
1007 pic->type == PICTURE_TYPE_P,
1009 vaapi_encode_add_ref(avctx, pic, ctx->next_prev, 0, 0, 1);
1012 --ctx->next_prev->ref_count[0];
1014 if (b_counter > 0) {
1015 vaapi_encode_set_b_pictures(avctx, start, pic, pic, 1,
1018 ctx->next_prev = pic;
1020 ++ctx->next_prev->ref_count[0];
1024 static int vaapi_encode_clear_old(AVCodecContext *avctx)
1026 VAAPIEncodeContext *ctx = avctx->priv_data;
1027 VAAPIEncodePicture *pic, *prev, *next;
1029 av_assert0(ctx->pic_start);
1031 // Remove direct references once each picture is complete.
1032 for (pic = ctx->pic_start; pic; pic = pic->next) {
1033 if (pic->encode_complete && pic->next)
1034 vaapi_encode_remove_refs(avctx, pic, 0);
1037 // Remove indirect references once a picture has no direct references.
1038 for (pic = ctx->pic_start; pic; pic = pic->next) {
1039 if (pic->encode_complete && pic->ref_count[0] == 0)
1040 vaapi_encode_remove_refs(avctx, pic, 1);
1043 // Clear out all complete pictures with no remaining references.
1045 for (pic = ctx->pic_start; pic; pic = next) {
1047 if (pic->encode_complete && pic->ref_count[1] == 0) {
1048 av_assert0(pic->ref_removed[0] && pic->ref_removed[1]);
1052 ctx->pic_start = next;
1053 vaapi_encode_free(avctx, pic);
1062 static int vaapi_encode_check_frame(AVCodecContext *avctx,
1063 const AVFrame *frame)
1065 VAAPIEncodeContext *ctx = avctx->priv_data;
1067 if ((frame->crop_top || frame->crop_bottom ||
1068 frame->crop_left || frame->crop_right) && !ctx->crop_warned) {
1069 av_log(avctx, AV_LOG_WARNING, "Cropping information on input "
1070 "frames ignored due to lack of API support.\n");
1071 ctx->crop_warned = 1;
1074 if (!ctx->roi_allowed) {
1075 AVFrameSideData *sd =
1076 av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
1078 if (sd && !ctx->roi_warned) {
1079 av_log(avctx, AV_LOG_WARNING, "ROI side data on input "
1080 "frames ignored due to lack of driver support.\n");
1081 ctx->roi_warned = 1;
1088 static int vaapi_encode_send_frame(AVCodecContext *avctx, AVFrame *frame)
1090 VAAPIEncodeContext *ctx = avctx->priv_data;
1091 VAAPIEncodePicture *pic;
1095 av_log(avctx, AV_LOG_DEBUG, "Input frame: %ux%u (%"PRId64").\n",
1096 frame->width, frame->height, frame->pts);
1098 err = vaapi_encode_check_frame(avctx, frame);
1102 pic = vaapi_encode_alloc(avctx);
1104 return AVERROR(ENOMEM);
1106 pic->input_image = av_frame_alloc();
1107 if (!pic->input_image) {
1108 err = AVERROR(ENOMEM);
1112 if (ctx->input_order == 0 || frame->pict_type == AV_PICTURE_TYPE_I)
1115 pic->input_surface = (VASurfaceID)(uintptr_t)frame->data[3];
1116 pic->pts = frame->pts;
1118 av_frame_move_ref(pic->input_image, frame);
1120 if (ctx->input_order == 0)
1121 ctx->first_pts = pic->pts;
1122 if (ctx->input_order == ctx->decode_delay)
1123 ctx->dts_pts_diff = pic->pts - ctx->first_pts;
1124 if (ctx->output_delay > 0)
1125 ctx->ts_ring[ctx->input_order % (3 * ctx->output_delay)] = pic->pts;
1127 pic->display_order = ctx->input_order;
1130 if (ctx->pic_start) {
1131 ctx->pic_end->next = pic;
1134 ctx->pic_start = pic;
1139 ctx->end_of_stream = 1;
1141 // Fix timestamps if we hit end-of-stream before the initial decode
1142 // delay has elapsed.
1143 if (ctx->input_order < ctx->decode_delay)
1144 ctx->dts_pts_diff = ctx->pic_end->pts - ctx->first_pts;
1150 vaapi_encode_free(avctx, pic);
1154 int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
1156 VAAPIEncodeContext *ctx = avctx->priv_data;
1157 VAAPIEncodePicture *pic;
1158 AVFrame *frame = ctx->frame;
1161 err = ff_encode_get_frame(avctx, frame);
1162 if (err < 0 && err != AVERROR_EOF)
1165 if (err == AVERROR_EOF)
1168 err = vaapi_encode_send_frame(avctx, frame);
1172 if (!ctx->pic_start) {
1173 if (ctx->end_of_stream)
1176 return AVERROR(EAGAIN);
1180 err = vaapi_encode_pick_next(avctx, &pic);
1185 pic->encode_order = ctx->encode_order++;
1187 err = vaapi_encode_issue(avctx, pic);
1189 av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err);
1193 err = vaapi_encode_output(avctx, pic, pkt);
1195 av_log(avctx, AV_LOG_ERROR, "Output failed: %d.\n", err);
1199 if (ctx->output_delay == 0) {
1200 pkt->dts = pkt->pts;
1201 } else if (pic->encode_order < ctx->decode_delay) {
1202 if (ctx->ts_ring[pic->encode_order] < INT64_MIN + ctx->dts_pts_diff)
1203 pkt->dts = INT64_MIN;
1205 pkt->dts = ctx->ts_ring[pic->encode_order] - ctx->dts_pts_diff;
1207 pkt->dts = ctx->ts_ring[(pic->encode_order - ctx->decode_delay) %
1208 (3 * ctx->output_delay)];
1210 av_log(avctx, AV_LOG_DEBUG, "Output packet: pts %"PRId64" dts %"PRId64".\n",
1211 pkt->pts, pkt->dts);
1213 ctx->output_order = pic->encode_order;
1214 vaapi_encode_clear_old(avctx);
1220 static av_cold void vaapi_encode_add_global_param(AVCodecContext *avctx, int type,
1221 void *buffer, size_t size)
1223 VAAPIEncodeContext *ctx = avctx->priv_data;
1225 av_assert0(ctx->nb_global_params < MAX_GLOBAL_PARAMS);
1227 ctx->global_params_type[ctx->nb_global_params] = type;
1228 ctx->global_params [ctx->nb_global_params] = buffer;
1229 ctx->global_params_size[ctx->nb_global_params] = size;
1231 ++ctx->nb_global_params;
1234 typedef struct VAAPIEncodeRTFormat {
1241 } VAAPIEncodeRTFormat;
1243 static const VAAPIEncodeRTFormat vaapi_encode_rt_formats[] = {
1244 { "YUV400", VA_RT_FORMAT_YUV400, 8, 1, },
1245 { "YUV420", VA_RT_FORMAT_YUV420, 8, 3, 1, 1 },
1246 { "YUV422", VA_RT_FORMAT_YUV422, 8, 3, 1, 0 },
1247 #if VA_CHECK_VERSION(1, 2, 0)
1248 { "YUV422_10", VA_RT_FORMAT_YUV422_10, 10, 3, 1, 0 },
1250 { "YUV444", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
1251 { "YUV411", VA_RT_FORMAT_YUV411, 8, 3, 2, 0 },
1252 #if VA_CHECK_VERSION(0, 38, 1)
1253 { "YUV420_10", VA_RT_FORMAT_YUV420_10BPP, 10, 3, 1, 1 },
1257 static const VAEntrypoint vaapi_encode_entrypoints_normal[] = {
1258 VAEntrypointEncSlice,
1259 VAEntrypointEncPicture,
1260 #if VA_CHECK_VERSION(0, 39, 2)
1261 VAEntrypointEncSliceLP,
1265 #if VA_CHECK_VERSION(0, 39, 2)
1266 static const VAEntrypoint vaapi_encode_entrypoints_low_power[] = {
1267 VAEntrypointEncSliceLP,
1272 static av_cold int vaapi_encode_profile_entrypoint(AVCodecContext *avctx)
1274 VAAPIEncodeContext *ctx = avctx->priv_data;
1275 VAProfile *va_profiles = NULL;
1276 VAEntrypoint *va_entrypoints = NULL;
1278 const VAEntrypoint *usable_entrypoints;
1279 const VAAPIEncodeProfile *profile;
1280 const AVPixFmtDescriptor *desc;
1281 VAConfigAttrib rt_format_attr;
1282 const VAAPIEncodeRTFormat *rt_format;
1283 const char *profile_string, *entrypoint_string;
1284 int i, j, n, depth, err;
1287 if (ctx->low_power) {
1288 #if VA_CHECK_VERSION(0, 39, 2)
1289 usable_entrypoints = vaapi_encode_entrypoints_low_power;
1291 av_log(avctx, AV_LOG_ERROR, "Low-power encoding is not "
1292 "supported with this VAAPI version.\n");
1293 return AVERROR(EINVAL);
1296 usable_entrypoints = vaapi_encode_entrypoints_normal;
1299 desc = av_pix_fmt_desc_get(ctx->input_frames->sw_format);
1301 av_log(avctx, AV_LOG_ERROR, "Invalid input pixfmt (%d).\n",
1302 ctx->input_frames->sw_format);
1303 return AVERROR(EINVAL);
1305 depth = desc->comp[0].depth;
1306 for (i = 1; i < desc->nb_components; i++) {
1307 if (desc->comp[i].depth != depth) {
1308 av_log(avctx, AV_LOG_ERROR, "Invalid input pixfmt (%s).\n",
1310 return AVERROR(EINVAL);
1313 av_log(avctx, AV_LOG_VERBOSE, "Input surface format is %s.\n",
1316 n = vaMaxNumProfiles(ctx->hwctx->display);
1317 va_profiles = av_malloc_array(n, sizeof(VAProfile));
1319 err = AVERROR(ENOMEM);
1322 vas = vaQueryConfigProfiles(ctx->hwctx->display, va_profiles, &n);
1323 if (vas != VA_STATUS_SUCCESS) {
1324 av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: %d (%s).\n",
1325 vas, vaErrorStr(vas));
1326 err = AVERROR_EXTERNAL;
1330 av_assert0(ctx->codec->profiles);
1331 for (i = 0; (ctx->codec->profiles[i].av_profile !=
1332 FF_PROFILE_UNKNOWN); i++) {
1333 profile = &ctx->codec->profiles[i];
1334 if (depth != profile->depth ||
1335 desc->nb_components != profile->nb_components)
1337 if (desc->nb_components > 1 &&
1338 (desc->log2_chroma_w != profile->log2_chroma_w ||
1339 desc->log2_chroma_h != profile->log2_chroma_h))
1341 if (avctx->profile != profile->av_profile &&
1342 avctx->profile != FF_PROFILE_UNKNOWN)
1345 #if VA_CHECK_VERSION(1, 0, 0)
1346 profile_string = vaProfileStr(profile->va_profile);
1348 profile_string = "(no profile names)";
1351 for (j = 0; j < n; j++) {
1352 if (va_profiles[j] == profile->va_profile)
1356 av_log(avctx, AV_LOG_VERBOSE, "Compatible profile %s (%d) "
1357 "is not supported by driver.\n", profile_string,
1358 profile->va_profile);
1362 ctx->profile = profile;
1365 if (!ctx->profile) {
1366 av_log(avctx, AV_LOG_ERROR, "No usable encoding profile found.\n");
1367 err = AVERROR(ENOSYS);
1371 avctx->profile = profile->av_profile;
1372 ctx->va_profile = profile->va_profile;
1373 av_log(avctx, AV_LOG_VERBOSE, "Using VAAPI profile %s (%d).\n",
1374 profile_string, ctx->va_profile);
1376 n = vaMaxNumEntrypoints(ctx->hwctx->display);
1377 va_entrypoints = av_malloc_array(n, sizeof(VAEntrypoint));
1378 if (!va_entrypoints) {
1379 err = AVERROR(ENOMEM);
1382 vas = vaQueryConfigEntrypoints(ctx->hwctx->display, ctx->va_profile,
1383 va_entrypoints, &n);
1384 if (vas != VA_STATUS_SUCCESS) {
1385 av_log(avctx, AV_LOG_ERROR, "Failed to query entrypoints for "
1386 "profile %s (%d): %d (%s).\n", profile_string,
1387 ctx->va_profile, vas, vaErrorStr(vas));
1388 err = AVERROR_EXTERNAL;
1392 for (i = 0; i < n; i++) {
1393 for (j = 0; usable_entrypoints[j]; j++) {
1394 if (va_entrypoints[i] == usable_entrypoints[j])
1397 if (usable_entrypoints[j])
1401 av_log(avctx, AV_LOG_ERROR, "No usable encoding entrypoint found "
1402 "for profile %s (%d).\n", profile_string, ctx->va_profile);
1403 err = AVERROR(ENOSYS);
1407 ctx->va_entrypoint = va_entrypoints[i];
1408 #if VA_CHECK_VERSION(1, 0, 0)
1409 entrypoint_string = vaEntrypointStr(ctx->va_entrypoint);
1411 entrypoint_string = "(no entrypoint names)";
1413 av_log(avctx, AV_LOG_VERBOSE, "Using VAAPI entrypoint %s (%d).\n",
1414 entrypoint_string, ctx->va_entrypoint);
1416 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_encode_rt_formats); i++) {
1417 rt_format = &vaapi_encode_rt_formats[i];
1418 if (rt_format->depth == depth &&
1419 rt_format->nb_components == profile->nb_components &&
1420 rt_format->log2_chroma_w == profile->log2_chroma_w &&
1421 rt_format->log2_chroma_h == profile->log2_chroma_h)
1424 if (i >= FF_ARRAY_ELEMS(vaapi_encode_rt_formats)) {
1425 av_log(avctx, AV_LOG_ERROR, "No usable render target format "
1426 "found for profile %s (%d) entrypoint %s (%d).\n",
1427 profile_string, ctx->va_profile,
1428 entrypoint_string, ctx->va_entrypoint);
1429 err = AVERROR(ENOSYS);
1433 rt_format_attr = (VAConfigAttrib) { VAConfigAttribRTFormat };
1434 vas = vaGetConfigAttributes(ctx->hwctx->display,
1435 ctx->va_profile, ctx->va_entrypoint,
1436 &rt_format_attr, 1);
1437 if (vas != VA_STATUS_SUCCESS) {
1438 av_log(avctx, AV_LOG_ERROR, "Failed to query RT format "
1439 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1440 err = AVERROR_EXTERNAL;
1444 if (rt_format_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1445 av_log(avctx, AV_LOG_VERBOSE, "RT format config attribute not "
1446 "supported by driver: assuming surface RT format %s "
1447 "is valid.\n", rt_format->name);
1448 } else if (!(rt_format_attr.value & rt_format->value)) {
1449 av_log(avctx, AV_LOG_ERROR, "Surface RT format %s not supported "
1450 "by driver for encoding profile %s (%d) entrypoint %s (%d).\n",
1451 rt_format->name, profile_string, ctx->va_profile,
1452 entrypoint_string, ctx->va_entrypoint);
1453 err = AVERROR(ENOSYS);
1456 av_log(avctx, AV_LOG_VERBOSE, "Using VAAPI render target "
1457 "format %s (%#x).\n", rt_format->name, rt_format->value);
1458 ctx->config_attributes[ctx->nb_config_attributes++] =
1460 .type = VAConfigAttribRTFormat,
1461 .value = rt_format->value,
1467 av_freep(&va_profiles);
1468 av_freep(&va_entrypoints);
1472 static const VAAPIEncodeRCMode vaapi_encode_rc_modes[] = {
1474 // | Maxrate | HRD/VBV
1476 { RC_MODE_CQP, "CQP", 1, VA_RC_CQP, 0, 0, 1, 0 },
1477 { RC_MODE_CBR, "CBR", 1, VA_RC_CBR, 1, 0, 0, 1 },
1478 { RC_MODE_VBR, "VBR", 1, VA_RC_VBR, 1, 1, 0, 1 },
1479 #if VA_CHECK_VERSION(1, 1, 0)
1480 { RC_MODE_ICQ, "ICQ", 1, VA_RC_ICQ, 0, 0, 1, 0 },
1482 { RC_MODE_ICQ, "ICQ", 0 },
1484 #if VA_CHECK_VERSION(1, 3, 0)
1485 { RC_MODE_QVBR, "QVBR", 1, VA_RC_QVBR, 1, 1, 1, 1 },
1486 { RC_MODE_AVBR, "AVBR", 0, VA_RC_AVBR, 1, 0, 0, 0 },
1488 { RC_MODE_QVBR, "QVBR", 0 },
1489 { RC_MODE_AVBR, "AVBR", 0 },
1493 static av_cold int vaapi_encode_init_rate_control(AVCodecContext *avctx)
1495 VAAPIEncodeContext *ctx = avctx->priv_data;
1496 uint32_t supported_va_rc_modes;
1497 const VAAPIEncodeRCMode *rc_mode;
1498 int64_t rc_bits_per_second;
1499 int rc_target_percentage;
1502 int64_t hrd_buffer_size;
1503 int64_t hrd_initial_buffer_fullness;
1505 VAConfigAttrib rc_attr = { VAConfigAttribRateControl };
1507 char supported_rc_modes_string[64];
1509 vas = vaGetConfigAttributes(ctx->hwctx->display,
1510 ctx->va_profile, ctx->va_entrypoint,
1512 if (vas != VA_STATUS_SUCCESS) {
1513 av_log(avctx, AV_LOG_ERROR, "Failed to query rate control "
1514 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
1515 return AVERROR_EXTERNAL;
1517 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1518 av_log(avctx, AV_LOG_VERBOSE, "Driver does not report any "
1519 "supported rate control modes: assuming CQP only.\n");
1520 supported_va_rc_modes = VA_RC_CQP;
1521 strcpy(supported_rc_modes_string, "unknown");
1523 char *str = supported_rc_modes_string;
1524 size_t len = sizeof(supported_rc_modes_string);
1525 int i, first = 1, res;
1527 supported_va_rc_modes = rc_attr.value;
1528 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_encode_rc_modes); i++) {
1529 rc_mode = &vaapi_encode_rc_modes[i];
1530 if (supported_va_rc_modes & rc_mode->va_mode) {
1531 res = snprintf(str, len, "%s%s",
1532 first ? "" : ", ", rc_mode->name);
1545 av_log(avctx, AV_LOG_DEBUG, "Driver supports RC modes %s.\n",
1546 supported_rc_modes_string);
1549 // Rate control mode selection:
1550 // * If the user has set a mode explicitly with the rc_mode option,
1551 // use it and fail if it is not available.
1552 // * If an explicit QP option has been set, use CQP.
1553 // * If the codec is CQ-only, use CQP.
1554 // * If the QSCALE avcodec option is set, use CQP.
1555 // * If bitrate and quality are both set, try QVBR.
1556 // * If quality is set, try ICQ, then CQP.
1557 // * If bitrate and maxrate are set and have the same value, try CBR.
1558 // * If a bitrate is set, try AVBR, then VBR, then CBR.
1559 // * If no bitrate is set, try ICQ, then CQP.
1561 #define TRY_RC_MODE(mode, fail) do { \
1562 rc_mode = &vaapi_encode_rc_modes[mode]; \
1563 if (!(rc_mode->va_mode & supported_va_rc_modes)) { \
1565 av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \
1566 "RC mode (supported modes: %s).\n", rc_mode->name, \
1567 supported_rc_modes_string); \
1568 return AVERROR(EINVAL); \
1570 av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \
1571 "RC mode.\n", rc_mode->name); \
1574 goto rc_mode_found; \
1578 if (ctx->explicit_rc_mode)
1579 TRY_RC_MODE(ctx->explicit_rc_mode, 1);
1581 if (ctx->explicit_qp)
1582 TRY_RC_MODE(RC_MODE_CQP, 1);
1584 if (ctx->codec->flags & FLAG_CONSTANT_QUALITY_ONLY)
1585 TRY_RC_MODE(RC_MODE_CQP, 1);
1587 if (avctx->flags & AV_CODEC_FLAG_QSCALE)
1588 TRY_RC_MODE(RC_MODE_CQP, 1);
1590 if (avctx->bit_rate > 0 && avctx->global_quality > 0)
1591 TRY_RC_MODE(RC_MODE_QVBR, 0);
1593 if (avctx->global_quality > 0) {
1594 TRY_RC_MODE(RC_MODE_ICQ, 0);
1595 TRY_RC_MODE(RC_MODE_CQP, 0);
1598 if (avctx->bit_rate > 0 && avctx->rc_max_rate == avctx->bit_rate)
1599 TRY_RC_MODE(RC_MODE_CBR, 0);
1601 if (avctx->bit_rate > 0) {
1602 TRY_RC_MODE(RC_MODE_AVBR, 0);
1603 TRY_RC_MODE(RC_MODE_VBR, 0);
1604 TRY_RC_MODE(RC_MODE_CBR, 0);
1606 TRY_RC_MODE(RC_MODE_ICQ, 0);
1607 TRY_RC_MODE(RC_MODE_CQP, 0);
1610 av_log(avctx, AV_LOG_ERROR, "Driver does not support any "
1611 "RC mode compatible with selected options "
1612 "(supported modes: %s).\n", supported_rc_modes_string);
1613 return AVERROR(EINVAL);
1616 if (rc_mode->bitrate) {
1617 if (avctx->bit_rate <= 0) {
1618 av_log(avctx, AV_LOG_ERROR, "Bitrate must be set for %s "
1619 "RC mode.\n", rc_mode->name);
1620 return AVERROR(EINVAL);
1623 if (rc_mode->mode == RC_MODE_AVBR) {
1624 // For maximum confusion AVBR is hacked into the existing API
1625 // by overloading some of the fields with completely different
1628 // Target percentage does not apply in AVBR mode.
1629 rc_bits_per_second = avctx->bit_rate;
1631 // Accuracy tolerance range for meeting the specified target
1632 // bitrate. It's very unclear how this is actually intended
1633 // to work - since we do want to get the specified bitrate,
1634 // set the accuracy to 100% for now.
1635 rc_target_percentage = 100;
1637 // Convergence period in frames. The GOP size reflects the
1638 // user's intended block size for cutting, so reusing that
1639 // as the convergence period seems a reasonable default.
1640 rc_window_size = avctx->gop_size > 0 ? avctx->gop_size : 60;
1642 } else if (rc_mode->maxrate) {
1643 if (avctx->rc_max_rate > 0) {
1644 if (avctx->rc_max_rate < avctx->bit_rate) {
1645 av_log(avctx, AV_LOG_ERROR, "Invalid bitrate settings: "
1646 "bitrate (%"PRId64") must not be greater than "
1647 "maxrate (%"PRId64").\n", avctx->bit_rate,
1648 avctx->rc_max_rate);
1649 return AVERROR(EINVAL);
1651 rc_bits_per_second = avctx->rc_max_rate;
1652 rc_target_percentage = (avctx->bit_rate * 100) /
1655 // We only have a target bitrate, but this mode requires
1656 // that a maximum rate be supplied as well. Since the
1657 // user does not want this to be a constraint, arbitrarily
1658 // pick a maximum rate of double the target rate.
1659 rc_bits_per_second = 2 * avctx->bit_rate;
1660 rc_target_percentage = 50;
1663 if (avctx->rc_max_rate > avctx->bit_rate) {
1664 av_log(avctx, AV_LOG_WARNING, "Max bitrate is ignored "
1665 "in %s RC mode.\n", rc_mode->name);
1667 rc_bits_per_second = avctx->bit_rate;
1668 rc_target_percentage = 100;
1671 rc_bits_per_second = 0;
1672 rc_target_percentage = 100;
1675 if (rc_mode->quality) {
1676 if (ctx->explicit_qp) {
1677 rc_quality = ctx->explicit_qp;
1678 } else if (avctx->global_quality > 0) {
1679 rc_quality = avctx->global_quality;
1681 rc_quality = ctx->codec->default_quality;
1682 av_log(avctx, AV_LOG_WARNING, "No quality level set; "
1683 "using default (%d).\n", rc_quality);
1690 if (avctx->rc_buffer_size)
1691 hrd_buffer_size = avctx->rc_buffer_size;
1692 else if (avctx->rc_max_rate > 0)
1693 hrd_buffer_size = avctx->rc_max_rate;
1695 hrd_buffer_size = avctx->bit_rate;
1696 if (avctx->rc_initial_buffer_occupancy) {
1697 if (avctx->rc_initial_buffer_occupancy > hrd_buffer_size) {
1698 av_log(avctx, AV_LOG_ERROR, "Invalid RC buffer settings: "
1699 "must have initial buffer size (%d) <= "
1700 "buffer size (%"PRId64").\n",
1701 avctx->rc_initial_buffer_occupancy, hrd_buffer_size);
1702 return AVERROR(EINVAL);
1704 hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy;
1706 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1709 rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second;
1711 if (avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
1712 av_log(avctx, AV_LOG_WARNING, "Buffering settings are ignored "
1713 "in %s RC mode.\n", rc_mode->name);
1716 hrd_buffer_size = 0;
1717 hrd_initial_buffer_fullness = 0;
1719 if (rc_mode->mode != RC_MODE_AVBR) {
1720 // Already set (with completely different meaning) for AVBR.
1721 rc_window_size = 1000;
1725 if (rc_bits_per_second > UINT32_MAX ||
1726 hrd_buffer_size > UINT32_MAX ||
1727 hrd_initial_buffer_fullness > UINT32_MAX) {
1728 av_log(avctx, AV_LOG_ERROR, "RC parameters of 2^32 or "
1729 "greater are not supported by VAAPI.\n");
1730 return AVERROR(EINVAL);
1733 ctx->rc_mode = rc_mode;
1734 ctx->rc_quality = rc_quality;
1735 ctx->va_rc_mode = rc_mode->va_mode;
1736 ctx->va_bit_rate = rc_bits_per_second;
1738 av_log(avctx, AV_LOG_VERBOSE, "RC mode: %s.\n", rc_mode->name);
1739 if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1740 // This driver does not want the RC mode attribute to be set.
1742 ctx->config_attributes[ctx->nb_config_attributes++] =
1744 .type = VAConfigAttribRateControl,
1745 .value = ctx->va_rc_mode,
1749 if (rc_mode->quality)
1750 av_log(avctx, AV_LOG_VERBOSE, "RC quality: %d.\n", rc_quality);
1752 if (rc_mode->va_mode != VA_RC_CQP) {
1753 if (rc_mode->mode == RC_MODE_AVBR) {
1754 av_log(avctx, AV_LOG_VERBOSE, "RC target: %"PRId64" bps "
1755 "converging in %d frames with %d%% accuracy.\n",
1756 rc_bits_per_second, rc_window_size,
1757 rc_target_percentage);
1758 } else if (rc_mode->bitrate) {
1759 av_log(avctx, AV_LOG_VERBOSE, "RC target: %d%% of "
1760 "%"PRId64" bps over %d ms.\n", rc_target_percentage,
1761 rc_bits_per_second, rc_window_size);
1764 ctx->rc_params = (VAEncMiscParameterRateControl) {
1765 .bits_per_second = rc_bits_per_second,
1766 .target_percentage = rc_target_percentage,
1767 .window_size = rc_window_size,
1769 .min_qp = (avctx->qmin > 0 ? avctx->qmin : 0),
1770 .basic_unit_size = 0,
1771 #if VA_CHECK_VERSION(1, 1, 0)
1772 .ICQ_quality_factor = av_clip(rc_quality, 1, 51),
1773 .max_qp = (avctx->qmax > 0 ? avctx->qmax : 0),
1775 #if VA_CHECK_VERSION(1, 3, 0)
1776 .quality_factor = rc_quality,
1779 vaapi_encode_add_global_param(avctx,
1780 VAEncMiscParameterTypeRateControl,
1782 sizeof(ctx->rc_params));
1786 av_log(avctx, AV_LOG_VERBOSE, "RC buffer: %"PRId64" bits, "
1787 "initial fullness %"PRId64" bits.\n",
1788 hrd_buffer_size, hrd_initial_buffer_fullness);
1790 ctx->hrd_params = (VAEncMiscParameterHRD) {
1791 .initial_buffer_fullness = hrd_initial_buffer_fullness,
1792 .buffer_size = hrd_buffer_size,
1794 vaapi_encode_add_global_param(avctx,
1795 VAEncMiscParameterTypeHRD,
1797 sizeof(ctx->hrd_params));
1800 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1801 av_reduce(&fr_num, &fr_den,
1802 avctx->framerate.num, avctx->framerate.den, 65535);
1804 av_reduce(&fr_num, &fr_den,
1805 avctx->time_base.den, avctx->time_base.num, 65535);
1807 av_log(avctx, AV_LOG_VERBOSE, "RC framerate: %d/%d (%.2f fps).\n",
1808 fr_num, fr_den, (double)fr_num / fr_den);
1810 ctx->fr_params = (VAEncMiscParameterFrameRate) {
1811 .framerate = (unsigned int)fr_den << 16 | fr_num,
1813 #if VA_CHECK_VERSION(0, 40, 0)
1814 vaapi_encode_add_global_param(avctx,
1815 VAEncMiscParameterTypeFrameRate,
1817 sizeof(ctx->fr_params));
1823 static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx)
1825 VAAPIEncodeContext *ctx = avctx->priv_data;
1827 VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames };
1828 uint32_t ref_l0, ref_l1;
1830 vas = vaGetConfigAttributes(ctx->hwctx->display,
1834 if (vas != VA_STATUS_SUCCESS) {
1835 av_log(avctx, AV_LOG_ERROR, "Failed to query reference frames "
1836 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
1837 return AVERROR_EXTERNAL;
1840 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
1841 ref_l0 = ref_l1 = 0;
1843 ref_l0 = attr.value & 0xffff;
1844 ref_l1 = attr.value >> 16 & 0xffff;
1847 if (ctx->codec->flags & FLAG_INTRA_ONLY ||
1848 avctx->gop_size <= 1) {
1849 av_log(avctx, AV_LOG_VERBOSE, "Using intra frames only.\n");
1851 } else if (ref_l0 < 1) {
1852 av_log(avctx, AV_LOG_ERROR, "Driver does not support any "
1853 "reference frames.\n");
1854 return AVERROR(EINVAL);
1855 } else if (!(ctx->codec->flags & FLAG_B_PICTURES) ||
1856 ref_l1 < 1 || avctx->max_b_frames < 1) {
1857 av_log(avctx, AV_LOG_VERBOSE, "Using intra and P-frames "
1858 "(supported references: %d / %d).\n", ref_l0, ref_l1);
1859 ctx->gop_size = avctx->gop_size;
1860 ctx->p_per_i = INT_MAX;
1863 av_log(avctx, AV_LOG_VERBOSE, "Using intra, P- and B-frames "
1864 "(supported references: %d / %d).\n", ref_l0, ref_l1);
1865 ctx->gop_size = avctx->gop_size;
1866 ctx->p_per_i = INT_MAX;
1867 ctx->b_per_p = avctx->max_b_frames;
1868 if (ctx->codec->flags & FLAG_B_PICTURE_REFERENCES) {
1869 ctx->max_b_depth = FFMIN(ctx->desired_b_depth,
1870 av_log2(ctx->b_per_p) + 1);
1872 ctx->max_b_depth = 1;
1876 if (ctx->codec->flags & FLAG_NON_IDR_KEY_PICTURES) {
1877 ctx->closed_gop = !!(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP);
1878 ctx->gop_per_idr = ctx->idr_interval + 1;
1880 ctx->closed_gop = 1;
1881 ctx->gop_per_idr = 1;
1887 static av_cold int vaapi_encode_init_row_slice_structure(AVCodecContext *avctx,
1888 uint32_t slice_structure)
1890 VAAPIEncodeContext *ctx = avctx->priv_data;
1893 // For fixed-size slices currently we only support whole rows, making
1894 // rectangular slices. This could be extended to arbitrary runs of
1895 // blocks, but since slices tend to be a conformance requirement and
1896 // most cases (such as broadcast or bluray) want rectangular slices
1897 // only it would need to be gated behind another option.
1898 if (avctx->slices > ctx->slice_block_rows) {
1899 av_log(avctx, AV_LOG_WARNING, "Not enough rows to use "
1900 "configured number of slices (%d < %d); using "
1901 "maximum.\n", ctx->slice_block_rows, avctx->slices);
1902 req_slices = ctx->slice_block_rows;
1904 req_slices = avctx->slices;
1906 if (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS ||
1907 slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS) {
1908 ctx->nb_slices = req_slices;
1909 ctx->slice_size = ctx->slice_block_rows / ctx->nb_slices;
1910 } else if (slice_structure & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS) {
1912 for (k = 1;; k *= 2) {
1913 if (2 * k * (req_slices - 1) + 1 >= ctx->slice_block_rows)
1916 ctx->nb_slices = (ctx->slice_block_rows + k - 1) / k;
1917 ctx->slice_size = k;
1918 #if VA_CHECK_VERSION(1, 0, 0)
1919 } else if (slice_structure & VA_ENC_SLICE_STRUCTURE_EQUAL_ROWS) {
1920 ctx->nb_slices = ctx->slice_block_rows;
1921 ctx->slice_size = 1;
1924 av_log(avctx, AV_LOG_ERROR, "Driver does not support any usable "
1925 "slice structure modes (%#x).\n", slice_structure);
1926 return AVERROR(EINVAL);
1932 static av_cold int vaapi_encode_init_tile_slice_structure(AVCodecContext *avctx,
1933 uint32_t slice_structure)
1935 VAAPIEncodeContext *ctx = avctx->priv_data;
1938 if (!(slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS ||
1939 (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS &&
1940 ctx->tile_cols == 1))) {
1941 av_log(avctx, AV_LOG_ERROR, "Supported slice structure (%#x) doesn't work for "
1942 "current tile requirement.\n", slice_structure);
1943 return AVERROR(EINVAL);
1946 if (ctx->tile_rows > ctx->slice_block_rows ||
1947 ctx->tile_cols > ctx->slice_block_cols) {
1948 av_log(avctx, AV_LOG_WARNING, "Not enough block rows/cols (%d x %d) "
1949 "for configured number of tile (%d x %d); ",
1950 ctx->slice_block_rows, ctx->slice_block_cols,
1951 ctx->tile_rows, ctx->tile_cols);
1952 ctx->tile_rows = ctx->tile_rows > ctx->slice_block_rows ?
1953 ctx->slice_block_rows : ctx->tile_rows;
1954 ctx->tile_cols = ctx->tile_cols > ctx->slice_block_cols ?
1955 ctx->slice_block_cols : ctx->tile_cols;
1956 av_log(avctx, AV_LOG_WARNING, "using allowed maximum (%d x %d).\n",
1957 ctx->tile_rows, ctx->tile_cols);
1960 req_tiles = ctx->tile_rows * ctx->tile_cols;
1962 // Tile slice is not allowed to cross the boundary of a tile due to
1963 // the constraints of media-driver. Currently we support one slice
1964 // per tile. This could be extended to multiple slices per tile.
1965 if (avctx->slices != req_tiles)
1966 av_log(avctx, AV_LOG_WARNING, "The number of requested slices "
1967 "mismatches with configured number of tile (%d != %d); "
1968 "using requested tile number for slice.\n",
1969 avctx->slices, req_tiles);
1971 ctx->nb_slices = req_tiles;
1973 // Default in uniform spacing
1975 for (i = 0; i < ctx->tile_cols; i++) {
1976 ctx->col_width[i] = ( i + 1 ) * ctx->slice_block_cols / ctx->tile_cols -
1977 i * ctx->slice_block_cols / ctx->tile_cols;
1978 ctx->col_bd[i + 1] = ctx->col_bd[i] + ctx->col_width[i];
1981 for (i = 0; i < ctx->tile_rows; i++) {
1982 ctx->row_height[i] = ( i + 1 ) * ctx->slice_block_rows / ctx->tile_rows -
1983 i * ctx->slice_block_rows / ctx->tile_rows;
1984 ctx->row_bd[i + 1] = ctx->row_bd[i] + ctx->row_height[i];
1987 av_log(avctx, AV_LOG_VERBOSE, "Encoding pictures with %d x %d tile.\n",
1988 ctx->tile_rows, ctx->tile_cols);
1993 static av_cold int vaapi_encode_init_slice_structure(AVCodecContext *avctx)
1995 VAAPIEncodeContext *ctx = avctx->priv_data;
1996 VAConfigAttrib attr[3] = { { VAConfigAttribEncMaxSlices },
1997 { VAConfigAttribEncSliceStructure },
1998 #if VA_CHECK_VERSION(1, 1, 0)
1999 { VAConfigAttribEncTileSupport },
2003 uint32_t max_slices, slice_structure;
2006 if (!(ctx->codec->flags & FLAG_SLICE_CONTROL)) {
2007 if (avctx->slices > 0) {
2008 av_log(avctx, AV_LOG_WARNING, "Multiple slices were requested "
2009 "but this codec does not support controlling slices.\n");
2014 ctx->slice_block_rows = (avctx->height + ctx->slice_block_height - 1) /
2015 ctx->slice_block_height;
2016 ctx->slice_block_cols = (avctx->width + ctx->slice_block_width - 1) /
2017 ctx->slice_block_width;
2019 if (avctx->slices <= 1 && !ctx->tile_rows && !ctx->tile_cols) {
2021 ctx->slice_size = ctx->slice_block_rows;
2025 vas = vaGetConfigAttributes(ctx->hwctx->display,
2028 attr, FF_ARRAY_ELEMS(attr));
2029 if (vas != VA_STATUS_SUCCESS) {
2030 av_log(avctx, AV_LOG_ERROR, "Failed to query slice "
2031 "attributes: %d (%s).\n", vas, vaErrorStr(vas));
2032 return AVERROR_EXTERNAL;
2034 max_slices = attr[0].value;
2035 slice_structure = attr[1].value;
2036 if (max_slices == VA_ATTRIB_NOT_SUPPORTED ||
2037 slice_structure == VA_ATTRIB_NOT_SUPPORTED) {
2038 av_log(avctx, AV_LOG_ERROR, "Driver does not support encoding "
2039 "pictures as multiple slices.\n.");
2040 return AVERROR(EINVAL);
2043 if (ctx->tile_rows && ctx->tile_cols) {
2044 #if VA_CHECK_VERSION(1, 1, 0)
2045 uint32_t tile_support = attr[2].value;
2046 if (tile_support == VA_ATTRIB_NOT_SUPPORTED) {
2047 av_log(avctx, AV_LOG_ERROR, "Driver does not support encoding "
2048 "pictures as multiple tiles.\n.");
2049 return AVERROR(EINVAL);
2052 av_log(avctx, AV_LOG_ERROR, "Tile encoding option is "
2053 "not supported with this VAAPI version.\n");
2054 return AVERROR(EINVAL);
2058 if (ctx->tile_rows && ctx->tile_cols)
2059 ret = vaapi_encode_init_tile_slice_structure(avctx, slice_structure);
2061 ret = vaapi_encode_init_row_slice_structure(avctx, slice_structure);
2065 if (ctx->nb_slices > avctx->slices) {
2066 av_log(avctx, AV_LOG_WARNING, "Slice count rounded up to "
2067 "%d (from %d) due to driver constraints on slice "
2068 "structure.\n", ctx->nb_slices, avctx->slices);
2070 if (ctx->nb_slices > max_slices) {
2071 av_log(avctx, AV_LOG_ERROR, "Driver does not support "
2072 "encoding with %d slices (max %"PRIu32").\n",
2073 ctx->nb_slices, max_slices);
2074 return AVERROR(EINVAL);
2077 av_log(avctx, AV_LOG_VERBOSE, "Encoding pictures with %d slices.\n",
2082 static av_cold int vaapi_encode_init_packed_headers(AVCodecContext *avctx)
2084 VAAPIEncodeContext *ctx = avctx->priv_data;
2086 VAConfigAttrib attr = { VAConfigAttribEncPackedHeaders };
2088 vas = vaGetConfigAttributes(ctx->hwctx->display,
2092 if (vas != VA_STATUS_SUCCESS) {
2093 av_log(avctx, AV_LOG_ERROR, "Failed to query packed headers "
2094 "attribute: %d (%s).\n", vas, vaErrorStr(vas));
2095 return AVERROR_EXTERNAL;
2098 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2099 if (ctx->desired_packed_headers) {
2100 av_log(avctx, AV_LOG_WARNING, "Driver does not support any "
2101 "packed headers (wanted %#x).\n",
2102 ctx->desired_packed_headers);
2104 av_log(avctx, AV_LOG_VERBOSE, "Driver does not support any "
2105 "packed headers (none wanted).\n");
2107 ctx->va_packed_headers = 0;
2109 if (ctx->desired_packed_headers & ~attr.value) {
2110 av_log(avctx, AV_LOG_WARNING, "Driver does not support some "
2111 "wanted packed headers (wanted %#x, found %#x).\n",
2112 ctx->desired_packed_headers, attr.value);
2114 av_log(avctx, AV_LOG_VERBOSE, "All wanted packed headers "
2115 "available (wanted %#x, found %#x).\n",
2116 ctx->desired_packed_headers, attr.value);
2118 ctx->va_packed_headers = ctx->desired_packed_headers & attr.value;
2121 if (ctx->va_packed_headers) {
2122 ctx->config_attributes[ctx->nb_config_attributes++] =
2124 .type = VAConfigAttribEncPackedHeaders,
2125 .value = ctx->va_packed_headers,
2129 if ( (ctx->desired_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2130 !(ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
2131 (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
2132 av_log(avctx, AV_LOG_WARNING, "Driver does not support packed "
2133 "sequence headers, but a global header is requested.\n");
2134 av_log(avctx, AV_LOG_WARNING, "No global header will be written: "
2135 "this may result in a stream which is not usable for some "
2136 "purposes (e.g. not muxable to some containers).\n");
2142 static av_cold int vaapi_encode_init_quality(AVCodecContext *avctx)
2144 #if VA_CHECK_VERSION(0, 36, 0)
2145 VAAPIEncodeContext *ctx = avctx->priv_data;
2147 VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
2148 int quality = avctx->compression_level;
2150 vas = vaGetConfigAttributes(ctx->hwctx->display,
2154 if (vas != VA_STATUS_SUCCESS) {
2155 av_log(avctx, AV_LOG_ERROR, "Failed to query quality "
2156 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2157 return AVERROR_EXTERNAL;
2160 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2162 av_log(avctx, AV_LOG_WARNING, "Quality attribute is not "
2163 "supported: will use default quality level.\n");
2166 if (quality > attr.value) {
2167 av_log(avctx, AV_LOG_WARNING, "Invalid quality level: "
2168 "valid range is 0-%d, using %d.\n",
2169 attr.value, attr.value);
2170 quality = attr.value;
2173 ctx->quality_params = (VAEncMiscParameterBufferQualityLevel) {
2174 .quality_level = quality,
2176 vaapi_encode_add_global_param(avctx,
2177 VAEncMiscParameterTypeQualityLevel,
2178 &ctx->quality_params,
2179 sizeof(ctx->quality_params));
2182 av_log(avctx, AV_LOG_WARNING, "The encode quality option is "
2183 "not supported with this VAAPI version.\n");
2189 static av_cold int vaapi_encode_init_roi(AVCodecContext *avctx)
2191 #if VA_CHECK_VERSION(1, 0, 0)
2192 VAAPIEncodeContext *ctx = avctx->priv_data;
2194 VAConfigAttrib attr = { VAConfigAttribEncROI };
2196 vas = vaGetConfigAttributes(ctx->hwctx->display,
2200 if (vas != VA_STATUS_SUCCESS) {
2201 av_log(avctx, AV_LOG_ERROR, "Failed to query ROI "
2202 "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
2203 return AVERROR_EXTERNAL;
2206 if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
2207 ctx->roi_allowed = 0;
2209 VAConfigAttribValEncROI roi = {
2210 .value = attr.value,
2213 ctx->roi_max_regions = roi.bits.num_roi_regions;
2214 ctx->roi_allowed = ctx->roi_max_regions > 0 &&
2215 (ctx->va_rc_mode == VA_RC_CQP ||
2216 roi.bits.roi_rc_qp_delta_support);
2222 static void vaapi_encode_free_output_buffer(void *opaque,
2225 AVCodecContext *avctx = opaque;
2226 VAAPIEncodeContext *ctx = avctx->priv_data;
2227 VABufferID buffer_id;
2229 buffer_id = (VABufferID)(uintptr_t)data;
2231 vaDestroyBuffer(ctx->hwctx->display, buffer_id);
2233 av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id);
2236 static AVBufferRef *vaapi_encode_alloc_output_buffer(void *opaque,
2239 AVCodecContext *avctx = opaque;
2240 VAAPIEncodeContext *ctx = avctx->priv_data;
2241 VABufferID buffer_id;
2245 // The output buffer size is fixed, so it needs to be large enough
2246 // to hold the largest possible compressed frame. We assume here
2247 // that the uncompressed frame plus some header data is an upper
2249 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
2250 VAEncCodedBufferType,
2251 3 * ctx->surface_width * ctx->surface_height +
2252 (1 << 16), 1, 0, &buffer_id);
2253 if (vas != VA_STATUS_SUCCESS) {
2254 av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream "
2255 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
2259 av_log(avctx, AV_LOG_DEBUG, "Allocated output buffer %#x\n", buffer_id);
2261 ref = av_buffer_create((uint8_t*)(uintptr_t)buffer_id,
2263 &vaapi_encode_free_output_buffer,
2264 avctx, AV_BUFFER_FLAG_READONLY);
2266 vaDestroyBuffer(ctx->hwctx->display, buffer_id);
2273 static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx)
2275 VAAPIEncodeContext *ctx = avctx->priv_data;
2276 AVVAAPIHWConfig *hwconfig = NULL;
2277 AVHWFramesConstraints *constraints = NULL;
2278 enum AVPixelFormat recon_format;
2281 hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
2283 err = AVERROR(ENOMEM);
2286 hwconfig->config_id = ctx->va_config;
2288 constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
2291 err = AVERROR(ENOMEM);
2295 // Probably we can use the input surface format as the surface format
2296 // of the reconstructed frames. If not, we just pick the first (only?)
2297 // format in the valid list and hope that it all works.
2298 recon_format = AV_PIX_FMT_NONE;
2299 if (constraints->valid_sw_formats) {
2300 for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
2301 if (ctx->input_frames->sw_format ==
2302 constraints->valid_sw_formats[i]) {
2303 recon_format = ctx->input_frames->sw_format;
2307 if (recon_format == AV_PIX_FMT_NONE) {
2308 // No match. Just use the first in the supported list and
2309 // hope for the best.
2310 recon_format = constraints->valid_sw_formats[0];
2313 // No idea what to use; copy input format.
2314 recon_format = ctx->input_frames->sw_format;
2316 av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
2317 "reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
2319 if (ctx->surface_width < constraints->min_width ||
2320 ctx->surface_height < constraints->min_height ||
2321 ctx->surface_width > constraints->max_width ||
2322 ctx->surface_height > constraints->max_height) {
2323 av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at "
2324 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
2325 ctx->surface_width, ctx->surface_height,
2326 constraints->min_width, constraints->max_width,
2327 constraints->min_height, constraints->max_height);
2328 err = AVERROR(EINVAL);
2332 av_freep(&hwconfig);
2333 av_hwframe_constraints_free(&constraints);
2335 ctx->recon_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
2336 if (!ctx->recon_frames_ref) {
2337 err = AVERROR(ENOMEM);
2340 ctx->recon_frames = (AVHWFramesContext*)ctx->recon_frames_ref->data;
2342 ctx->recon_frames->format = AV_PIX_FMT_VAAPI;
2343 ctx->recon_frames->sw_format = recon_format;
2344 ctx->recon_frames->width = ctx->surface_width;
2345 ctx->recon_frames->height = ctx->surface_height;
2347 err = av_hwframe_ctx_init(ctx->recon_frames_ref);
2349 av_log(avctx, AV_LOG_ERROR, "Failed to initialise reconstructed "
2350 "frame context: %d.\n", err);
2356 av_freep(&hwconfig);
2357 av_hwframe_constraints_free(&constraints);
2361 av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
2363 VAAPIEncodeContext *ctx = avctx->priv_data;
2364 AVVAAPIFramesContext *recon_hwctx = NULL;
2368 ctx->frame = av_frame_alloc();
2370 return AVERROR(ENOMEM);
2373 if (!avctx->hw_frames_ctx) {
2374 av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
2375 "required to associate the encoding device.\n");
2376 return AVERROR(EINVAL);
2379 ctx->va_config = VA_INVALID_ID;
2380 ctx->va_context = VA_INVALID_ID;
2382 ctx->input_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
2383 if (!ctx->input_frames_ref) {
2384 err = AVERROR(ENOMEM);
2387 ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
2389 ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
2390 if (!ctx->device_ref) {
2391 err = AVERROR(ENOMEM);
2394 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
2395 ctx->hwctx = ctx->device->hwctx;
2397 err = vaapi_encode_profile_entrypoint(avctx);
2401 err = vaapi_encode_init_rate_control(avctx);
2405 err = vaapi_encode_init_gop_structure(avctx);
2409 err = vaapi_encode_init_slice_structure(avctx);
2413 err = vaapi_encode_init_packed_headers(avctx);
2417 err = vaapi_encode_init_roi(avctx);
2421 if (avctx->compression_level >= 0) {
2422 err = vaapi_encode_init_quality(avctx);
2427 vas = vaCreateConfig(ctx->hwctx->display,
2428 ctx->va_profile, ctx->va_entrypoint,
2429 ctx->config_attributes, ctx->nb_config_attributes,
2431 if (vas != VA_STATUS_SUCCESS) {
2432 av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
2433 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
2438 err = vaapi_encode_create_recon_frames(avctx);
2442 recon_hwctx = ctx->recon_frames->hwctx;
2443 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
2444 ctx->surface_width, ctx->surface_height,
2446 recon_hwctx->surface_ids,
2447 recon_hwctx->nb_surfaces,
2449 if (vas != VA_STATUS_SUCCESS) {
2450 av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
2451 "context: %d (%s).\n", vas, vaErrorStr(vas));
2456 ctx->output_buffer_pool =
2457 av_buffer_pool_init2(sizeof(VABufferID), avctx,
2458 &vaapi_encode_alloc_output_buffer, NULL);
2459 if (!ctx->output_buffer_pool) {
2460 err = AVERROR(ENOMEM);
2464 if (ctx->codec->configure) {
2465 err = ctx->codec->configure(avctx);
2470 ctx->output_delay = ctx->b_per_p;
2471 ctx->decode_delay = ctx->max_b_depth;
2473 if (ctx->codec->sequence_params_size > 0) {
2474 ctx->codec_sequence_params =
2475 av_mallocz(ctx->codec->sequence_params_size);
2476 if (!ctx->codec_sequence_params) {
2477 err = AVERROR(ENOMEM);
2481 if (ctx->codec->picture_params_size > 0) {
2482 ctx->codec_picture_params =
2483 av_mallocz(ctx->codec->picture_params_size);
2484 if (!ctx->codec_picture_params) {
2485 err = AVERROR(ENOMEM);
2490 if (ctx->codec->init_sequence_params) {
2491 err = ctx->codec->init_sequence_params(avctx);
2493 av_log(avctx, AV_LOG_ERROR, "Codec sequence initialisation "
2494 "failed: %d.\n", err);
2499 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
2500 ctx->codec->write_sequence_header &&
2501 avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
2502 char data[MAX_PARAM_BUFFER_SIZE];
2503 size_t bit_len = 8 * sizeof(data);
2505 err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
2507 av_log(avctx, AV_LOG_ERROR, "Failed to write sequence header "
2508 "for extradata: %d.\n", err);
2511 avctx->extradata_size = (bit_len + 7) / 8;
2512 avctx->extradata = av_mallocz(avctx->extradata_size +
2513 AV_INPUT_BUFFER_PADDING_SIZE);
2514 if (!avctx->extradata) {
2515 err = AVERROR(ENOMEM);
2518 memcpy(avctx->extradata, data, avctx->extradata_size);
2528 av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
2530 VAAPIEncodeContext *ctx = avctx->priv_data;
2531 VAAPIEncodePicture *pic, *next;
2533 for (pic = ctx->pic_start; pic; pic = next) {
2535 vaapi_encode_free(avctx, pic);
2538 av_buffer_pool_uninit(&ctx->output_buffer_pool);
2540 if (ctx->va_context != VA_INVALID_ID) {
2541 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
2542 ctx->va_context = VA_INVALID_ID;
2545 if (ctx->va_config != VA_INVALID_ID) {
2546 vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
2547 ctx->va_config = VA_INVALID_ID;
2550 av_frame_free(&ctx->frame);
2552 av_freep(&ctx->codec_sequence_params);
2553 av_freep(&ctx->codec_picture_params);
2555 av_buffer_unref(&ctx->recon_frames_ref);
2556 av_buffer_unref(&ctx->input_frames_ref);
2557 av_buffer_unref(&ctx->device_ref);