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"
30 static const char * const picture_type_name[] = { "IDR", "I", "P", "B" };
32 static int vaapi_encode_make_packed_header(AVCodecContext *avctx,
33 VAAPIEncodePicture *pic,
34 int type, char *data, size_t bit_len)
36 VAAPIEncodeContext *ctx = avctx->priv_data;
38 VABufferID param_buffer, data_buffer;
40 VAEncPackedHeaderParameterBuffer params = {
42 .bit_length = bit_len,
43 .has_emulation_bytes = 1,
46 tmp = av_realloc_array(pic->param_buffers, sizeof(*tmp), pic->nb_param_buffers + 2);
48 return AVERROR(ENOMEM);
49 pic->param_buffers = tmp;
51 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
52 VAEncPackedHeaderParameterBufferType,
53 sizeof(params), 1, ¶ms, ¶m_buffer);
54 if (vas != VA_STATUS_SUCCESS) {
55 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
56 "for packed header (type %d): %d (%s).\n",
57 type, vas, vaErrorStr(vas));
60 pic->param_buffers[pic->nb_param_buffers++] = param_buffer;
62 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
63 VAEncPackedHeaderDataBufferType,
64 (bit_len + 7) / 8, 1, data, &data_buffer);
65 if (vas != VA_STATUS_SUCCESS) {
66 av_log(avctx, AV_LOG_ERROR, "Failed to create data buffer "
67 "for packed header (type %d): %d (%s).\n",
68 type, vas, vaErrorStr(vas));
71 pic->param_buffers[pic->nb_param_buffers++] = data_buffer;
73 av_log(avctx, AV_LOG_DEBUG, "Packed header buffer (%d) is %#x/%#x "
74 "(%zu bits).\n", type, param_buffer, data_buffer, bit_len);
78 static int vaapi_encode_make_param_buffer(AVCodecContext *avctx,
79 VAAPIEncodePicture *pic,
80 int type, char *data, size_t len)
82 VAAPIEncodeContext *ctx = avctx->priv_data;
87 tmp = av_realloc_array(pic->param_buffers, sizeof(*tmp), pic->nb_param_buffers + 1);
89 return AVERROR(ENOMEM);
90 pic->param_buffers = tmp;
92 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
93 type, len, 1, data, &buffer);
94 if (vas != VA_STATUS_SUCCESS) {
95 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
96 "(type %d): %d (%s).\n", type, vas, vaErrorStr(vas));
99 pic->param_buffers[pic->nb_param_buffers++] = buffer;
101 av_log(avctx, AV_LOG_DEBUG, "Param buffer (%d) is %#x.\n",
106 static int vaapi_encode_wait(AVCodecContext *avctx,
107 VAAPIEncodePicture *pic)
109 VAAPIEncodeContext *ctx = avctx->priv_data;
112 av_assert0(pic->encode_issued);
114 if (pic->encode_complete) {
115 // Already waited for this picture.
119 av_log(avctx, AV_LOG_DEBUG, "Sync to pic %"PRId64"/%"PRId64" "
120 "(input surface %#x).\n", pic->display_order,
121 pic->encode_order, pic->input_surface);
123 vas = vaSyncSurface(ctx->hwctx->display, pic->input_surface);
124 if (vas != VA_STATUS_SUCCESS) {
125 av_log(avctx, AV_LOG_ERROR, "Failed to sync to picture completion: "
126 "%d (%s).\n", vas, vaErrorStr(vas));
130 // Input is definitely finished with now.
131 av_frame_free(&pic->input_image);
133 pic->encode_complete = 1;
137 static int vaapi_encode_issue(AVCodecContext *avctx,
138 VAAPIEncodePicture *pic)
140 VAAPIEncodeContext *ctx = avctx->priv_data;
141 VAAPIEncodeSlice *slice;
144 char data[MAX_PARAM_BUFFER_SIZE];
147 av_log(avctx, AV_LOG_DEBUG, "Issuing encode for pic %"PRId64"/%"PRId64" "
148 "as type %s.\n", pic->display_order, pic->encode_order,
149 picture_type_name[pic->type]);
150 if (pic->nb_refs == 0) {
151 av_log(avctx, AV_LOG_DEBUG, "No reference pictures.\n");
153 av_log(avctx, AV_LOG_DEBUG, "Refers to:");
154 for (i = 0; i < pic->nb_refs; i++) {
155 av_log(avctx, AV_LOG_DEBUG, " %"PRId64"/%"PRId64,
156 pic->refs[i]->display_order, pic->refs[i]->encode_order);
158 av_log(avctx, AV_LOG_DEBUG, ".\n");
161 av_assert0(pic->input_available && !pic->encode_issued);
162 for (i = 0; i < pic->nb_refs; i++) {
163 av_assert0(pic->refs[i]);
164 // If we are serialised then the references must have already
165 // completed. If not, they must have been issued but need not
166 // have completed yet.
167 if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
168 av_assert0(pic->refs[i]->encode_complete);
170 av_assert0(pic->refs[i]->encode_issued);
173 av_log(avctx, AV_LOG_DEBUG, "Input surface is %#x.\n", pic->input_surface);
175 pic->recon_image = av_frame_alloc();
176 if (!pic->recon_image) {
177 err = AVERROR(ENOMEM);
181 err = av_hwframe_get_buffer(ctx->recon_frames_ref, pic->recon_image, 0);
183 err = AVERROR(ENOMEM);
186 pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3];
187 av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface);
189 pic->output_buffer_ref = av_buffer_pool_get(ctx->output_buffer_pool);
190 if (!pic->output_buffer_ref) {
191 err = AVERROR(ENOMEM);
194 pic->output_buffer = (VABufferID)(uintptr_t)pic->output_buffer_ref->data;
195 av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n",
198 if (ctx->codec->picture_params_size > 0) {
199 pic->codec_picture_params = av_malloc(ctx->codec->picture_params_size);
200 if (!pic->codec_picture_params)
202 memcpy(pic->codec_picture_params, ctx->codec_picture_params,
203 ctx->codec->picture_params_size);
205 av_assert0(!ctx->codec_picture_params);
208 pic->nb_param_buffers = 0;
210 if (pic->encode_order == 0) {
211 // Global parameter buffers are set on the first picture only.
213 for (i = 0; i < ctx->nb_global_params; i++) {
214 err = vaapi_encode_make_param_buffer(avctx, pic,
215 VAEncMiscParameterBufferType,
216 (char*)ctx->global_params[i],
217 ctx->global_params_size[i]);
223 if (pic->type == PICTURE_TYPE_IDR && ctx->codec->init_sequence_params) {
224 err = vaapi_encode_make_param_buffer(avctx, pic,
225 VAEncSequenceParameterBufferType,
226 ctx->codec_sequence_params,
227 ctx->codec->sequence_params_size);
232 if (ctx->codec->init_picture_params) {
233 err = ctx->codec->init_picture_params(avctx, pic);
235 av_log(avctx, AV_LOG_ERROR, "Failed to initialise picture "
236 "parameters: %d.\n", err);
239 err = vaapi_encode_make_param_buffer(avctx, pic,
240 VAEncPictureParameterBufferType,
241 pic->codec_picture_params,
242 ctx->codec->picture_params_size);
247 if (pic->type == PICTURE_TYPE_IDR) {
248 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
249 ctx->codec->write_sequence_header) {
250 bit_len = 8 * sizeof(data);
251 err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
253 av_log(avctx, AV_LOG_ERROR, "Failed to write per-sequence "
254 "header: %d.\n", err);
257 err = vaapi_encode_make_packed_header(avctx, pic,
258 ctx->codec->sequence_header_type,
265 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_PICTURE &&
266 ctx->codec->write_picture_header) {
267 bit_len = 8 * sizeof(data);
268 err = ctx->codec->write_picture_header(avctx, pic, data, &bit_len);
270 av_log(avctx, AV_LOG_ERROR, "Failed to write per-picture "
271 "header: %d.\n", err);
274 err = vaapi_encode_make_packed_header(avctx, pic,
275 ctx->codec->picture_header_type,
281 if (ctx->codec->write_extra_buffer) {
283 size_t len = sizeof(data);
285 err = ctx->codec->write_extra_buffer(avctx, pic, i, &type,
287 if (err == AVERROR_EOF)
290 av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
291 "buffer %d: %d.\n", i, err);
295 err = vaapi_encode_make_param_buffer(avctx, pic, type,
302 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_MISC &&
303 ctx->codec->write_extra_header) {
306 bit_len = 8 * sizeof(data);
307 err = ctx->codec->write_extra_header(avctx, pic, i, &type,
309 if (err == AVERROR_EOF)
312 av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
313 "header %d: %d.\n", i, err);
317 err = vaapi_encode_make_packed_header(avctx, pic, type,
324 pic->slices = av_mallocz_array(pic->nb_slices, sizeof(*pic->slices));
326 err = AVERROR(ENOMEM);
329 for (i = 0; i < pic->nb_slices; i++) {
330 slice = &pic->slices[i];
333 if (ctx->codec->slice_params_size > 0) {
334 slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size);
335 if (!slice->codec_slice_params) {
336 err = AVERROR(ENOMEM);
341 if (ctx->codec->init_slice_params) {
342 err = ctx->codec->init_slice_params(avctx, pic, slice);
344 av_log(avctx, AV_LOG_ERROR, "Failed to initialise slice "
345 "parameters: %d.\n", err);
350 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SLICE &&
351 ctx->codec->write_slice_header) {
352 bit_len = 8 * sizeof(data);
353 err = ctx->codec->write_slice_header(avctx, pic, slice,
356 av_log(avctx, AV_LOG_ERROR, "Failed to write per-slice "
357 "header: %d.\n", err);
360 err = vaapi_encode_make_packed_header(avctx, pic,
361 ctx->codec->slice_header_type,
367 if (ctx->codec->init_slice_params) {
368 err = vaapi_encode_make_param_buffer(avctx, pic,
369 VAEncSliceParameterBufferType,
370 slice->codec_slice_params,
371 ctx->codec->slice_params_size);
377 vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
379 if (vas != VA_STATUS_SUCCESS) {
380 av_log(avctx, AV_LOG_ERROR, "Failed to begin picture encode issue: "
381 "%d (%s).\n", vas, vaErrorStr(vas));
383 goto fail_with_picture;
386 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
387 pic->param_buffers, pic->nb_param_buffers);
388 if (vas != VA_STATUS_SUCCESS) {
389 av_log(avctx, AV_LOG_ERROR, "Failed to upload encode parameters: "
390 "%d (%s).\n", vas, vaErrorStr(vas));
392 goto fail_with_picture;
395 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
396 if (vas != VA_STATUS_SUCCESS) {
397 av_log(avctx, AV_LOG_ERROR, "Failed to end picture encode issue: "
398 "%d (%s).\n", vas, vaErrorStr(vas));
400 // vaRenderPicture() has been called here, so we should not destroy
401 // the parameter buffers unless separate destruction is required.
402 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
403 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
409 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
410 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
411 for (i = 0; i < pic->nb_param_buffers; i++) {
412 vas = vaDestroyBuffer(ctx->hwctx->display,
413 pic->param_buffers[i]);
414 if (vas != VA_STATUS_SUCCESS) {
415 av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
416 "param buffer %#x: %d (%s).\n",
417 pic->param_buffers[i], vas, vaErrorStr(vas));
423 pic->encode_issued = 1;
425 if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
426 return vaapi_encode_wait(avctx, pic);
431 vaEndPicture(ctx->hwctx->display, ctx->va_context);
433 for(i = 0; i < pic->nb_param_buffers; i++)
434 vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
435 for (i = 0; i < pic->nb_slices; i++) {
437 av_freep(&pic->slices[i].priv_data);
438 av_freep(&pic->slices[i].codec_slice_params);
442 av_freep(&pic->codec_picture_params);
443 av_freep(&pic->param_buffers);
444 av_freep(&pic->slices);
445 av_frame_free(&pic->recon_image);
446 av_buffer_unref(&pic->output_buffer_ref);
447 pic->output_buffer = VA_INVALID_ID;
451 static int vaapi_encode_output(AVCodecContext *avctx,
452 VAAPIEncodePicture *pic, AVPacket *pkt)
454 VAAPIEncodeContext *ctx = avctx->priv_data;
455 VACodedBufferSegment *buf_list, *buf;
459 err = vaapi_encode_wait(avctx, pic);
464 vas = vaMapBuffer(ctx->hwctx->display, pic->output_buffer,
466 if (vas != VA_STATUS_SUCCESS) {
467 av_log(avctx, AV_LOG_ERROR, "Failed to map output buffers: "
468 "%d (%s).\n", vas, vaErrorStr(vas));
473 for (buf = buf_list; buf; buf = buf->next) {
474 av_log(avctx, AV_LOG_DEBUG, "Output buffer: %u bytes "
475 "(status %08x).\n", buf->size, buf->status);
477 err = av_new_packet(pkt, buf->size);
481 memcpy(pkt->data, buf->buf, buf->size);
484 if (pic->type == PICTURE_TYPE_IDR)
485 pkt->flags |= AV_PKT_FLAG_KEY;
489 vas = vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
490 if (vas != VA_STATUS_SUCCESS) {
491 av_log(avctx, AV_LOG_ERROR, "Failed to unmap output buffers: "
492 "%d (%s).\n", vas, vaErrorStr(vas));
497 av_buffer_unref(&pic->output_buffer_ref);
498 pic->output_buffer = VA_INVALID_ID;
500 av_log(avctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n",
501 pic->display_order, pic->encode_order);
505 vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
507 av_buffer_unref(&pic->output_buffer_ref);
508 pic->output_buffer = VA_INVALID_ID;
512 static int vaapi_encode_discard(AVCodecContext *avctx,
513 VAAPIEncodePicture *pic)
515 vaapi_encode_wait(avctx, pic);
517 if (pic->output_buffer_ref) {
518 av_log(avctx, AV_LOG_DEBUG, "Discard output for pic "
519 "%"PRId64"/%"PRId64".\n",
520 pic->display_order, pic->encode_order);
522 av_buffer_unref(&pic->output_buffer_ref);
523 pic->output_buffer = VA_INVALID_ID;
529 static VAAPIEncodePicture *vaapi_encode_alloc(void)
531 VAAPIEncodePicture *pic;
533 pic = av_mallocz(sizeof(*pic));
537 pic->input_surface = VA_INVALID_ID;
538 pic->recon_surface = VA_INVALID_ID;
539 pic->output_buffer = VA_INVALID_ID;
544 static int vaapi_encode_free(AVCodecContext *avctx,
545 VAAPIEncodePicture *pic)
549 if (pic->encode_issued)
550 vaapi_encode_discard(avctx, pic);
552 for (i = 0; i < pic->nb_slices; i++) {
554 av_freep(&pic->slices[i].priv_data);
555 av_freep(&pic->slices[i].codec_slice_params);
558 av_freep(&pic->codec_picture_params);
560 av_frame_free(&pic->input_image);
561 av_frame_free(&pic->recon_image);
563 av_freep(&pic->param_buffers);
564 av_freep(&pic->slices);
565 // Output buffer should already be destroyed.
566 av_assert0(pic->output_buffer == VA_INVALID_ID);
568 av_freep(&pic->priv_data);
569 av_freep(&pic->codec_picture_params);
576 static int vaapi_encode_step(AVCodecContext *avctx,
577 VAAPIEncodePicture *target)
579 VAAPIEncodeContext *ctx = avctx->priv_data;
580 VAAPIEncodePicture *pic;
583 if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING ||
584 ctx->issue_mode == ISSUE_MODE_MINIMISE_LATENCY) {
585 // These two modes are equivalent, except that we wait for
586 // immediate completion on each operation if serialised.
589 // No target, nothing to do yet.
593 if (target->encode_complete) {
599 for (i = 0; i < pic->nb_refs; i++) {
600 if (!pic->refs[i]->encode_complete) {
601 err = vaapi_encode_step(avctx, pic->refs[i]);
607 err = vaapi_encode_issue(avctx, pic);
611 } else if (ctx->issue_mode == ISSUE_MODE_MAXIMISE_THROUGHPUT) {
614 // Run through the list of all available pictures repeatedly
615 // and issue the first one found which has all dependencies
616 // available (including previously-issued but not necessarily
617 // completed pictures).
620 for (pic = ctx->pic_start; pic; pic = pic->next) {
621 if (!pic->input_available || pic->encode_issued)
623 for (i = 0; i < pic->nb_refs; i++) {
624 if (!pic->refs[i]->encode_issued)
627 if (i < pic->nb_refs)
629 err = vaapi_encode_issue(avctx, pic);
633 // Start again from the beginning of the list,
634 // because issuing this picture may have satisfied
635 // forward dependencies of earlier ones.
640 // If we had a defined target for this step then it will
641 // always have been issued by now.
643 av_assert0(target->encode_issued && "broken dependencies?");
653 static int vaapi_encode_get_next(AVCodecContext *avctx,
654 VAAPIEncodePicture **pic_out)
656 VAAPIEncodeContext *ctx = avctx->priv_data;
657 VAAPIEncodePicture *start, *end, *pic;
660 for (pic = ctx->pic_start; pic; pic = pic->next) {
662 av_assert0(pic->display_order + 1 == pic->next->display_order);
663 if (pic->display_order == ctx->input_order) {
669 pic = vaapi_encode_alloc();
671 return AVERROR(ENOMEM);
673 if (ctx->input_order == 0 || ctx->force_idr ||
674 ctx->gop_counter >= avctx->gop_size) {
675 pic->type = PICTURE_TYPE_IDR;
677 ctx->gop_counter = 1;
679 } else if (ctx->p_counter >= ctx->p_per_i) {
680 pic->type = PICTURE_TYPE_I;
684 pic->type = PICTURE_TYPE_P;
685 pic->refs[0] = ctx->pic_end;
692 if (pic->type != PICTURE_TYPE_IDR) {
693 // If that was not an IDR frame, add B-frames display-before and
694 // encode-after it, but not exceeding the GOP size.
696 for (i = 0; i < ctx->b_per_p &&
697 ctx->gop_counter < avctx->gop_size; i++) {
698 pic = vaapi_encode_alloc();
702 pic->type = PICTURE_TYPE_B;
703 pic->refs[0] = ctx->pic_end;
708 pic->display_order = ctx->input_order + ctx->b_per_p - i - 1;
709 pic->encode_order = pic->display_order + 1;
716 if (ctx->input_order == 0) {
717 pic->display_order = 0;
718 pic->encode_order = 0;
720 ctx->pic_start = ctx->pic_end = pic;
723 for (i = 0, pic = start; pic; i++, pic = pic->next) {
724 pic->display_order = ctx->input_order + i;
725 if (end->type == PICTURE_TYPE_IDR)
726 pic->encode_order = ctx->input_order + i;
728 pic->encode_order = ctx->input_order;
730 pic->encode_order = ctx->input_order + i + 1;
733 av_assert0(ctx->pic_end);
734 ctx->pic_end->next = start;
739 av_log(avctx, AV_LOG_DEBUG, "Pictures:");
740 for (pic = ctx->pic_start; pic; pic = pic->next) {
741 av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
742 picture_type_name[pic->type],
743 pic->display_order, pic->encode_order);
745 av_log(avctx, AV_LOG_DEBUG, "\n");
752 vaapi_encode_free(avctx, start);
755 return AVERROR(ENOMEM);
758 static int vaapi_encode_truncate_gop(AVCodecContext *avctx)
760 VAAPIEncodeContext *ctx = avctx->priv_data;
761 VAAPIEncodePicture *pic, *last_pic, *next;
763 // Find the last picture we actually have input for.
764 for (pic = ctx->pic_start; pic; pic = pic->next) {
765 if (!pic->input_available)
771 av_assert0(last_pic);
773 if (last_pic->type == PICTURE_TYPE_B) {
774 // Some fixing up is required. Change the type of this
775 // picture to P, then modify preceding B references which
776 // point beyond it to point at it instead.
778 last_pic->type = PICTURE_TYPE_P;
779 last_pic->encode_order = last_pic->refs[1]->encode_order;
781 for (pic = ctx->pic_start; pic != last_pic; pic = pic->next) {
782 if (pic->type == PICTURE_TYPE_B &&
783 pic->refs[1] == last_pic->refs[1])
784 pic->refs[1] = last_pic;
787 last_pic->nb_refs = 1;
788 last_pic->refs[1] = NULL;
790 // We can use the current structure (no references point
791 // beyond the end), but there are unused pics to discard.
794 // Discard all following pics, they will never be used.
795 for (pic = last_pic->next; pic; pic = next) {
797 vaapi_encode_free(avctx, pic);
800 last_pic->next = NULL;
801 ctx->pic_end = last_pic;
804 // Input is available for all pictures, so we don't need to
808 av_log(avctx, AV_LOG_DEBUG, "Pictures ending truncated GOP:");
809 for (pic = ctx->pic_start; pic; pic = pic->next) {
810 av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
811 picture_type_name[pic->type],
812 pic->display_order, pic->encode_order);
814 av_log(avctx, AV_LOG_DEBUG, "\n");
819 static int vaapi_encode_clear_old(AVCodecContext *avctx)
821 VAAPIEncodeContext *ctx = avctx->priv_data;
822 VAAPIEncodePicture *pic, *old;
825 while (ctx->pic_start != ctx->pic_end) {
826 old = ctx->pic_start;
827 if (old->encode_order > ctx->output_order)
830 for (pic = old->next; pic; pic = pic->next) {
831 if (pic->encode_complete)
833 for (i = 0; i < pic->nb_refs; i++) {
834 if (pic->refs[i] == old) {
835 // We still need this picture because it's referred to
836 // directly by a later one, so it and all following
837 // pictures have to stay.
843 pic = ctx->pic_start;
844 ctx->pic_start = pic->next;
845 vaapi_encode_free(avctx, pic);
851 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
852 const AVFrame *input_image, int *got_packet)
854 VAAPIEncodeContext *ctx = avctx->priv_data;
855 VAAPIEncodePicture *pic;
859 av_log(avctx, AV_LOG_DEBUG, "Encode frame: %ux%u (%"PRId64").\n",
860 input_image->width, input_image->height, input_image->pts);
862 if (input_image->pict_type == AV_PICTURE_TYPE_I) {
863 err = vaapi_encode_truncate_gop(avctx);
869 err = vaapi_encode_get_next(avctx, &pic);
871 av_log(avctx, AV_LOG_ERROR, "Input setup failed: %d.\n", err);
875 pic->input_image = av_frame_alloc();
876 if (!pic->input_image) {
877 err = AVERROR(ENOMEM);
880 err = av_frame_ref(pic->input_image, input_image);
883 pic->input_surface = (VASurfaceID)(uintptr_t)input_image->data[3];
884 pic->pts = input_image->pts;
886 if (ctx->input_order == 0)
887 ctx->first_pts = pic->pts;
888 if (ctx->input_order == ctx->decode_delay)
889 ctx->dts_pts_diff = pic->pts - ctx->first_pts;
890 if (ctx->output_delay > 0)
891 ctx->ts_ring[ctx->input_order % (3 * ctx->output_delay)] = pic->pts;
893 pic->input_available = 1;
896 if (!ctx->end_of_stream) {
897 err = vaapi_encode_truncate_gop(avctx);
900 ctx->end_of_stream = 1;
906 av_assert0(ctx->output_order + ctx->output_delay + 1 == ctx->input_order);
908 for (pic = ctx->pic_start; pic; pic = pic->next)
909 if (pic->encode_order == ctx->output_order)
912 // pic can be null here if we don't have a specific target in this
913 // iteration. We might still issue encodes if things can be overlapped,
914 // even though we don't intend to output anything.
916 err = vaapi_encode_step(avctx, pic);
918 av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err);
925 err = vaapi_encode_output(avctx, pic, pkt);
927 av_log(avctx, AV_LOG_ERROR, "Output failed: %d.\n", err);
931 if (ctx->output_delay == 0) {
933 } else if (ctx->output_order < ctx->decode_delay) {
934 if (ctx->ts_ring[ctx->output_order] < INT64_MIN + ctx->dts_pts_diff)
935 pkt->dts = INT64_MIN;
937 pkt->dts = ctx->ts_ring[ctx->output_order] - ctx->dts_pts_diff;
939 pkt->dts = ctx->ts_ring[(ctx->output_order - ctx->decode_delay) %
940 (3 * ctx->output_delay)];
946 err = vaapi_encode_clear_old(avctx);
948 av_log(avctx, AV_LOG_ERROR, "List clearing failed: %d.\n", err);
955 // Unclear what to clean up on failure. There are probably some things we
956 // could do usefully clean up here, but for now just leave them for uninit()
961 static av_cold int vaapi_encode_config_attributes(AVCodecContext *avctx)
963 VAAPIEncodeContext *ctx = avctx->priv_data;
966 VAProfile *profiles = NULL;
967 VAEntrypoint *entrypoints = NULL;
968 VAConfigAttrib attr[] = {
969 { VAConfigAttribRTFormat },
970 { VAConfigAttribRateControl },
971 { VAConfigAttribEncMaxRefFrames },
972 { VAConfigAttribEncPackedHeaders },
975 n = vaMaxNumProfiles(ctx->hwctx->display);
976 profiles = av_malloc_array(n, sizeof(VAProfile));
978 err = AVERROR(ENOMEM);
981 vas = vaQueryConfigProfiles(ctx->hwctx->display, profiles, &n);
982 if (vas != VA_STATUS_SUCCESS) {
983 av_log(ctx, AV_LOG_ERROR, "Failed to query profiles: %d (%s).\n",
984 vas, vaErrorStr(vas));
985 err = AVERROR(ENOSYS);
988 for (i = 0; i < n; i++) {
989 if (profiles[i] == ctx->va_profile)
993 av_log(ctx, AV_LOG_ERROR, "Encoding profile not found (%d).\n",
995 err = AVERROR(ENOSYS);
999 n = vaMaxNumEntrypoints(ctx->hwctx->display);
1000 entrypoints = av_malloc_array(n, sizeof(VAEntrypoint));
1002 err = AVERROR(ENOMEM);
1005 vas = vaQueryConfigEntrypoints(ctx->hwctx->display, ctx->va_profile,
1007 if (vas != VA_STATUS_SUCCESS) {
1008 av_log(ctx, AV_LOG_ERROR, "Failed to query entrypoints for "
1009 "profile %u: %d (%s).\n", ctx->va_profile,
1010 vas, vaErrorStr(vas));
1011 err = AVERROR(ENOSYS);
1014 for (i = 0; i < n; i++) {
1015 if (entrypoints[i] == ctx->va_entrypoint)
1019 av_log(ctx, AV_LOG_ERROR, "Encoding entrypoint not found "
1020 "(%d / %d).\n", ctx->va_profile, ctx->va_entrypoint);
1021 err = AVERROR(ENOSYS);
1025 vas = vaGetConfigAttributes(ctx->hwctx->display,
1026 ctx->va_profile, ctx->va_entrypoint,
1027 attr, FF_ARRAY_ELEMS(attr));
1028 if (vas != VA_STATUS_SUCCESS) {
1029 av_log(avctx, AV_LOG_ERROR, "Failed to fetch config "
1030 "attributes: %d (%s).\n", vas, vaErrorStr(vas));
1031 return AVERROR(EINVAL);
1034 for (i = 0; i < FF_ARRAY_ELEMS(attr); i++) {
1035 if (attr[i].value == VA_ATTRIB_NOT_SUPPORTED) {
1036 // Unfortunately we have to treat this as "don't know" and hope
1037 // for the best, because the Intel MJPEG encoder returns this
1038 // for all the interesting attributes.
1041 switch (attr[i].type) {
1042 case VAConfigAttribRTFormat:
1043 if (!(ctx->va_rt_format & attr[i].value)) {
1044 av_log(avctx, AV_LOG_ERROR, "Surface RT format %#x "
1045 "is not supported (mask %#x).\n",
1046 ctx->va_rt_format, attr[i].value);
1047 err = AVERROR(EINVAL);
1050 ctx->config_attributes[ctx->nb_config_attributes++] =
1052 .type = VAConfigAttribRTFormat,
1053 .value = ctx->va_rt_format,
1056 case VAConfigAttribRateControl:
1057 // Hack for backward compatibility: CBR was the only
1058 // usable RC mode for a long time, so old drivers will
1059 // only have it. Normal default options may now choose
1060 // VBR and then fail, however, so override it here with
1061 // CBR if that is the only supported mode.
1062 if (ctx->va_rc_mode == VA_RC_VBR &&
1063 !(attr[i].value & VA_RC_VBR) &&
1064 (attr[i].value & VA_RC_CBR)) {
1065 av_log(avctx, AV_LOG_WARNING, "VBR rate control is "
1066 "not supported with this driver version; "
1067 "using CBR instead.\n");
1068 ctx->va_rc_mode = VA_RC_CBR;
1070 if (!(ctx->va_rc_mode & attr[i].value)) {
1071 av_log(avctx, AV_LOG_ERROR, "Rate control mode %#x "
1072 "is not supported (mask: %#x).\n",
1073 ctx->va_rc_mode, attr[i].value);
1074 err = AVERROR(EINVAL);
1077 ctx->config_attributes[ctx->nb_config_attributes++] =
1079 .type = VAConfigAttribRateControl,
1080 .value = ctx->va_rc_mode,
1083 case VAConfigAttribEncMaxRefFrames:
1085 unsigned int ref_l0 = attr[i].value & 0xffff;
1086 unsigned int ref_l1 = (attr[i].value >> 16) & 0xffff;
1088 if (avctx->gop_size > 1 && ref_l0 < 1) {
1089 av_log(avctx, AV_LOG_ERROR, "P frames are not "
1090 "supported (%#x).\n", attr[i].value);
1091 err = AVERROR(EINVAL);
1094 if (avctx->max_b_frames > 0 && ref_l1 < 1) {
1095 av_log(avctx, AV_LOG_ERROR, "B frames are not "
1096 "supported (%#x).\n", attr[i].value);
1097 err = AVERROR(EINVAL);
1102 case VAConfigAttribEncPackedHeaders:
1103 if (ctx->va_packed_headers & ~attr[i].value) {
1104 // This isn't fatal, but packed headers are always
1105 // preferable because they are under our control.
1106 // When absent, the driver is generating them and some
1107 // features may not work (e.g. VUI or SEI in H.264).
1108 av_log(avctx, AV_LOG_WARNING, "Warning: some packed "
1109 "headers are not supported (want %#x, got %#x).\n",
1110 ctx->va_packed_headers, attr[i].value);
1111 ctx->va_packed_headers &= attr[i].value;
1113 ctx->config_attributes[ctx->nb_config_attributes++] =
1115 .type = VAConfigAttribEncPackedHeaders,
1116 .value = ctx->va_packed_headers,
1120 av_assert0(0 && "Unexpected config attribute.");
1126 av_freep(&profiles);
1127 av_freep(&entrypoints);
1131 static av_cold int vaapi_encode_init_rate_control(AVCodecContext *avctx)
1133 VAAPIEncodeContext *ctx = avctx->priv_data;
1134 int rc_bits_per_second;
1135 int rc_target_percentage;
1137 int hrd_buffer_size;
1138 int hrd_initial_buffer_fullness;
1141 if (avctx->bit_rate > INT32_MAX) {
1142 av_log(avctx, AV_LOG_ERROR, "Target bitrate of 2^31 bps or "
1143 "higher is not supported.\n");
1144 return AVERROR(EINVAL);
1147 if (avctx->rc_buffer_size)
1148 hrd_buffer_size = avctx->rc_buffer_size;
1150 hrd_buffer_size = avctx->bit_rate;
1151 if (avctx->rc_initial_buffer_occupancy)
1152 hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy;
1154 hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
1156 if (ctx->va_rc_mode == VA_RC_CBR) {
1157 rc_bits_per_second = avctx->bit_rate;
1158 rc_target_percentage = 100;
1159 rc_window_size = 1000;
1161 if (avctx->rc_max_rate < avctx->bit_rate) {
1162 // Max rate is unset or invalid, just use the normal bitrate.
1163 rc_bits_per_second = avctx->bit_rate;
1164 rc_target_percentage = 100;
1166 rc_bits_per_second = avctx->rc_max_rate;
1167 rc_target_percentage = (avctx->bit_rate * 100) / rc_bits_per_second;
1169 rc_window_size = (hrd_buffer_size * 1000) / avctx->bit_rate;
1172 ctx->rc_params.misc.type = VAEncMiscParameterTypeRateControl;
1173 ctx->rc_params.rc = (VAEncMiscParameterRateControl) {
1174 .bits_per_second = rc_bits_per_second,
1175 .target_percentage = rc_target_percentage,
1176 .window_size = rc_window_size,
1178 .min_qp = (avctx->qmin > 0 ? avctx->qmin : 0),
1179 .basic_unit_size = 0,
1181 ctx->global_params[ctx->nb_global_params] =
1182 &ctx->rc_params.misc;
1183 ctx->global_params_size[ctx->nb_global_params++] =
1184 sizeof(ctx->rc_params);
1186 ctx->hrd_params.misc.type = VAEncMiscParameterTypeHRD;
1187 ctx->hrd_params.hrd = (VAEncMiscParameterHRD) {
1188 .initial_buffer_fullness = hrd_initial_buffer_fullness,
1189 .buffer_size = hrd_buffer_size,
1191 ctx->global_params[ctx->nb_global_params] =
1192 &ctx->hrd_params.misc;
1193 ctx->global_params_size[ctx->nb_global_params++] =
1194 sizeof(ctx->hrd_params);
1196 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1197 av_reduce(&fr_num, &fr_den,
1198 avctx->framerate.num, avctx->framerate.den, 65535);
1200 av_reduce(&fr_num, &fr_den,
1201 avctx->time_base.den, avctx->time_base.num, 65535);
1203 ctx->fr_params.misc.type = VAEncMiscParameterTypeFrameRate;
1204 ctx->fr_params.fr.framerate = (unsigned int)fr_den << 16 | fr_num;
1206 #if VA_CHECK_VERSION(0, 40, 0)
1207 ctx->global_params[ctx->nb_global_params] =
1208 &ctx->fr_params.misc;
1209 ctx->global_params_size[ctx->nb_global_params++] =
1210 sizeof(ctx->fr_params);
1216 static void vaapi_encode_free_output_buffer(void *opaque,
1219 AVCodecContext *avctx = opaque;
1220 VAAPIEncodeContext *ctx = avctx->priv_data;
1221 VABufferID buffer_id;
1223 buffer_id = (VABufferID)(uintptr_t)data;
1225 vaDestroyBuffer(ctx->hwctx->display, buffer_id);
1227 av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id);
1230 static AVBufferRef *vaapi_encode_alloc_output_buffer(void *opaque,
1233 AVCodecContext *avctx = opaque;
1234 VAAPIEncodeContext *ctx = avctx->priv_data;
1235 VABufferID buffer_id;
1239 // The output buffer size is fixed, so it needs to be large enough
1240 // to hold the largest possible compressed frame. We assume here
1241 // that the uncompressed frame plus some header data is an upper
1243 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
1244 VAEncCodedBufferType,
1245 3 * ctx->surface_width * ctx->surface_height +
1246 (1 << 16), 1, 0, &buffer_id);
1247 if (vas != VA_STATUS_SUCCESS) {
1248 av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream "
1249 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
1253 av_log(avctx, AV_LOG_DEBUG, "Allocated output buffer %#x\n", buffer_id);
1255 ref = av_buffer_create((uint8_t*)(uintptr_t)buffer_id,
1257 &vaapi_encode_free_output_buffer,
1258 avctx, AV_BUFFER_FLAG_READONLY);
1260 vaDestroyBuffer(ctx->hwctx->display, buffer_id);
1267 static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx)
1269 VAAPIEncodeContext *ctx = avctx->priv_data;
1270 AVVAAPIHWConfig *hwconfig = NULL;
1271 AVHWFramesConstraints *constraints = NULL;
1272 enum AVPixelFormat recon_format;
1275 hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
1277 err = AVERROR(ENOMEM);
1280 hwconfig->config_id = ctx->va_config;
1282 constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
1285 err = AVERROR(ENOMEM);
1289 // Probably we can use the input surface format as the surface format
1290 // of the reconstructed frames. If not, we just pick the first (only?)
1291 // format in the valid list and hope that it all works.
1292 recon_format = AV_PIX_FMT_NONE;
1293 if (constraints->valid_sw_formats) {
1294 for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
1295 if (ctx->input_frames->sw_format ==
1296 constraints->valid_sw_formats[i]) {
1297 recon_format = ctx->input_frames->sw_format;
1301 if (recon_format == AV_PIX_FMT_NONE) {
1302 // No match. Just use the first in the supported list and
1303 // hope for the best.
1304 recon_format = constraints->valid_sw_formats[0];
1307 // No idea what to use; copy input format.
1308 recon_format = ctx->input_frames->sw_format;
1310 av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
1311 "reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
1313 if (ctx->surface_width < constraints->min_width ||
1314 ctx->surface_height < constraints->min_height ||
1315 ctx->surface_width > constraints->max_width ||
1316 ctx->surface_height > constraints->max_height) {
1317 av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at "
1318 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
1319 ctx->surface_width, ctx->surface_height,
1320 constraints->min_width, constraints->max_width,
1321 constraints->min_height, constraints->max_height);
1322 err = AVERROR(EINVAL);
1326 av_freep(&hwconfig);
1327 av_hwframe_constraints_free(&constraints);
1329 ctx->recon_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
1330 if (!ctx->recon_frames_ref) {
1331 err = AVERROR(ENOMEM);
1334 ctx->recon_frames = (AVHWFramesContext*)ctx->recon_frames_ref->data;
1336 ctx->recon_frames->format = AV_PIX_FMT_VAAPI;
1337 ctx->recon_frames->sw_format = recon_format;
1338 ctx->recon_frames->width = ctx->surface_width;
1339 ctx->recon_frames->height = ctx->surface_height;
1340 // At most three IDR/I/P frames and two runs of B frames can be in
1341 // flight at any one time.
1342 ctx->recon_frames->initial_pool_size = 3 + 2 * avctx->max_b_frames;
1344 err = av_hwframe_ctx_init(ctx->recon_frames_ref);
1346 av_log(avctx, AV_LOG_ERROR, "Failed to initialise reconstructed "
1347 "frame context: %d.\n", err);
1353 av_freep(&hwconfig);
1354 av_hwframe_constraints_free(&constraints);
1358 av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
1360 VAAPIEncodeContext *ctx = avctx->priv_data;
1361 AVVAAPIFramesContext *recon_hwctx = NULL;
1365 if (!avctx->hw_frames_ctx) {
1366 av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
1367 "required to associate the encoding device.\n");
1368 return AVERROR(EINVAL);
1371 ctx->codec_options = ctx->codec_options_data;
1373 ctx->va_config = VA_INVALID_ID;
1374 ctx->va_context = VA_INVALID_ID;
1376 ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
1377 if (!ctx->priv_data) {
1378 err = AVERROR(ENOMEM);
1382 ctx->input_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
1383 if (!ctx->input_frames_ref) {
1384 err = AVERROR(ENOMEM);
1387 ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
1389 ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
1390 if (!ctx->device_ref) {
1391 err = AVERROR(ENOMEM);
1394 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
1395 ctx->hwctx = ctx->device->hwctx;
1397 err = vaapi_encode_config_attributes(avctx);
1401 vas = vaCreateConfig(ctx->hwctx->display,
1402 ctx->va_profile, ctx->va_entrypoint,
1403 ctx->config_attributes, ctx->nb_config_attributes,
1405 if (vas != VA_STATUS_SUCCESS) {
1406 av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
1407 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
1412 err = vaapi_encode_create_recon_frames(avctx);
1416 recon_hwctx = ctx->recon_frames->hwctx;
1417 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
1418 ctx->surface_width, ctx->surface_height,
1420 recon_hwctx->surface_ids,
1421 recon_hwctx->nb_surfaces,
1423 if (vas != VA_STATUS_SUCCESS) {
1424 av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
1425 "context: %d (%s).\n", vas, vaErrorStr(vas));
1430 ctx->output_buffer_pool =
1431 av_buffer_pool_init2(sizeof(VABufferID), avctx,
1432 &vaapi_encode_alloc_output_buffer, NULL);
1433 if (!ctx->output_buffer_pool) {
1434 err = AVERROR(ENOMEM);
1438 if (ctx->va_rc_mode & ~VA_RC_CQP) {
1439 err = vaapi_encode_init_rate_control(avctx);
1444 if (ctx->codec->configure) {
1445 err = ctx->codec->configure(avctx);
1450 if (avctx->compression_level >= 0) {
1451 #if VA_CHECK_VERSION(0, 36, 0)
1452 VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
1454 vas = vaGetConfigAttributes(ctx->hwctx->display,
1458 if (vas != VA_STATUS_SUCCESS) {
1459 av_log(avctx, AV_LOG_WARNING, "Failed to query quality "
1460 "attribute: will use default compression level.\n");
1462 if (avctx->compression_level > attr.value) {
1463 av_log(avctx, AV_LOG_WARNING, "Invalid compression "
1464 "level: valid range is 0-%d, using %d.\n",
1465 attr.value, attr.value);
1466 avctx->compression_level = attr.value;
1469 ctx->quality_params.misc.type =
1470 VAEncMiscParameterTypeQualityLevel;
1471 ctx->quality_params.quality.quality_level =
1472 avctx->compression_level;
1474 ctx->global_params[ctx->nb_global_params] =
1475 &ctx->quality_params.misc;
1476 ctx->global_params_size[ctx->nb_global_params++] =
1477 sizeof(ctx->quality_params);
1480 av_log(avctx, AV_LOG_WARNING, "The encode compression level "
1481 "option is not supported with this VAAPI version.\n");
1485 ctx->input_order = 0;
1486 ctx->output_delay = avctx->max_b_frames;
1487 ctx->decode_delay = 1;
1488 ctx->output_order = - ctx->output_delay - 1;
1490 // Currently we never generate I frames, only IDR.
1491 ctx->p_per_i = INT_MAX;
1492 ctx->b_per_p = avctx->max_b_frames;
1494 if (ctx->codec->sequence_params_size > 0) {
1495 ctx->codec_sequence_params =
1496 av_mallocz(ctx->codec->sequence_params_size);
1497 if (!ctx->codec_sequence_params) {
1498 err = AVERROR(ENOMEM);
1502 if (ctx->codec->picture_params_size > 0) {
1503 ctx->codec_picture_params =
1504 av_mallocz(ctx->codec->picture_params_size);
1505 if (!ctx->codec_picture_params) {
1506 err = AVERROR(ENOMEM);
1511 if (ctx->codec->init_sequence_params) {
1512 err = ctx->codec->init_sequence_params(avctx);
1514 av_log(avctx, AV_LOG_ERROR, "Codec sequence initialisation "
1515 "failed: %d.\n", err);
1520 // This should be configurable somehow. (Needs testing on a machine
1521 // where it actually overlaps properly, though.)
1522 ctx->issue_mode = ISSUE_MODE_MAXIMISE_THROUGHPUT;
1524 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
1525 ctx->codec->write_sequence_header) {
1526 char data[MAX_PARAM_BUFFER_SIZE];
1527 size_t bit_len = 8 * sizeof(data);
1529 err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
1531 av_log(avctx, AV_LOG_ERROR, "Failed to write sequence header "
1532 "for extradata: %d.\n", err);
1535 avctx->extradata_size = (bit_len + 7) / 8;
1536 avctx->extradata = av_mallocz(avctx->extradata_size +
1537 AV_INPUT_BUFFER_PADDING_SIZE);
1538 if (!avctx->extradata) {
1539 err = AVERROR(ENOMEM);
1542 memcpy(avctx->extradata, data, avctx->extradata_size);
1549 ff_vaapi_encode_close(avctx);
1553 av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
1555 VAAPIEncodeContext *ctx = avctx->priv_data;
1556 VAAPIEncodePicture *pic, *next;
1558 for (pic = ctx->pic_start; pic; pic = next) {
1560 vaapi_encode_free(avctx, pic);
1563 if (ctx->va_context != VA_INVALID_ID) {
1564 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
1565 ctx->va_context = VA_INVALID_ID;
1568 if (ctx->va_config != VA_INVALID_ID) {
1569 vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
1570 ctx->va_config = VA_INVALID_ID;
1573 av_buffer_pool_uninit(&ctx->output_buffer_pool);
1575 av_freep(&ctx->codec_sequence_params);
1576 av_freep(&ctx->codec_picture_params);
1578 av_buffer_unref(&ctx->recon_frames_ref);
1579 av_buffer_unref(&ctx->input_frames_ref);
1580 av_buffer_unref(&ctx->device_ref);
1582 av_freep(&ctx->priv_data);