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/log.h"
24 #include "libavutil/pixdesc.h"
26 #include "vaapi_encode.h"
29 static const char *picture_type_name[] = { "IDR", "I", "P", "B" };
31 static int vaapi_encode_make_packed_header(AVCodecContext *avctx,
32 VAAPIEncodePicture *pic,
33 int type, char *data, size_t bit_len)
35 VAAPIEncodeContext *ctx = avctx->priv_data;
37 VABufferID param_buffer, data_buffer;
38 VAEncPackedHeaderParameterBuffer params = {
40 .bit_length = bit_len,
41 .has_emulation_bytes = 1,
44 av_assert0(pic->nb_param_buffers + 2 <= MAX_PARAM_BUFFERS);
46 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
47 VAEncPackedHeaderParameterBufferType,
48 sizeof(params), 1, ¶ms, ¶m_buffer);
49 if (vas != VA_STATUS_SUCCESS) {
50 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
51 "for packed header (type %d): %d (%s).\n",
52 type, vas, vaErrorStr(vas));
55 pic->param_buffers[pic->nb_param_buffers++] = param_buffer;
57 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
58 VAEncPackedHeaderDataBufferType,
59 (bit_len + 7) / 8, 1, data, &data_buffer);
60 if (vas != VA_STATUS_SUCCESS) {
61 av_log(avctx, AV_LOG_ERROR, "Failed to create data buffer "
62 "for packed header (type %d): %d (%s).\n",
63 type, vas, vaErrorStr(vas));
66 pic->param_buffers[pic->nb_param_buffers++] = data_buffer;
68 av_log(avctx, AV_LOG_DEBUG, "Packed header buffer (%d) is %#x/%#x "
69 "(%zu bits).\n", type, param_buffer, data_buffer, bit_len);
73 static int vaapi_encode_make_param_buffer(AVCodecContext *avctx,
74 VAAPIEncodePicture *pic,
75 int type, char *data, size_t len)
77 VAAPIEncodeContext *ctx = avctx->priv_data;
81 av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
83 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
84 type, len, 1, data, &buffer);
85 if (vas != VA_STATUS_SUCCESS) {
86 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
87 "(type %d): %d (%s).\n", type, vas, vaErrorStr(vas));
90 pic->param_buffers[pic->nb_param_buffers++] = buffer;
92 av_log(avctx, AV_LOG_DEBUG, "Param buffer (%d) is %#x.\n",
97 static int vaapi_encode_wait(AVCodecContext *avctx,
98 VAAPIEncodePicture *pic)
100 VAAPIEncodeContext *ctx = avctx->priv_data;
103 av_assert0(pic->encode_issued);
105 if (pic->encode_complete) {
106 // Already waited for this picture.
110 av_log(avctx, AV_LOG_DEBUG, "Sync to pic %"PRId64"/%"PRId64" "
111 "(recon surface %#x).\n", pic->display_order,
112 pic->encode_order, pic->recon_surface);
114 vas = vaSyncSurface(ctx->hwctx->display, pic->recon_surface);
115 if (vas != VA_STATUS_SUCCESS) {
116 av_log(avctx, AV_LOG_ERROR, "Failed to sync to picture completion: "
117 "%d (%s).\n", vas, vaErrorStr(vas));
121 // Input is definitely finished with now.
122 av_frame_free(&pic->input_image);
124 pic->encode_complete = 1;
128 static int vaapi_encode_issue(AVCodecContext *avctx,
129 VAAPIEncodePicture *pic)
131 VAAPIEncodeContext *ctx = avctx->priv_data;
132 VAAPIEncodeSlice *slice;
135 char data[MAX_PARAM_BUFFER_SIZE];
138 av_log(avctx, AV_LOG_DEBUG, "Issuing encode for pic %"PRId64"/%"PRId64" "
139 "as type %s.\n", pic->display_order, pic->encode_order,
140 picture_type_name[pic->type]);
141 if (pic->nb_refs == 0) {
142 av_log(avctx, AV_LOG_DEBUG, "No reference pictures.\n");
144 av_log(avctx, AV_LOG_DEBUG, "Refers to:");
145 for (i = 0; i < pic->nb_refs; i++) {
146 av_log(avctx, AV_LOG_DEBUG, " %"PRId64"/%"PRId64,
147 pic->refs[i]->display_order, pic->refs[i]->encode_order);
149 av_log(avctx, AV_LOG_DEBUG, ".\n");
152 av_assert0(pic->input_available && !pic->encode_issued);
153 for (i = 0; i < pic->nb_refs; i++) {
154 av_assert0(pic->refs[i]);
155 // If we are serialised then the references must have already
156 // completed. If not, they must have been issued but need not
157 // have completed yet.
158 if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
159 av_assert0(pic->refs[i]->encode_complete);
161 av_assert0(pic->refs[i]->encode_issued);
164 av_log(avctx, AV_LOG_DEBUG, "Input surface is %#x.\n", pic->input_surface);
166 pic->recon_image = av_frame_alloc();
167 if (!pic->recon_image) {
168 err = AVERROR(ENOMEM);
172 err = av_hwframe_get_buffer(ctx->recon_frames_ref, pic->recon_image, 0);
174 err = AVERROR(ENOMEM);
177 pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3];
178 av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface);
180 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
181 VAEncCodedBufferType,
182 MAX_OUTPUT_BUFFER_SIZE, 1, 0,
183 &pic->output_buffer);
184 if (vas != VA_STATUS_SUCCESS) {
185 av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream "
186 "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
187 err = AVERROR(ENOMEM);
190 av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n",
193 if (ctx->codec->picture_params_size > 0) {
194 pic->codec_picture_params = av_malloc(ctx->codec->picture_params_size);
195 if (!pic->codec_picture_params)
197 memcpy(pic->codec_picture_params, ctx->codec_picture_params,
198 ctx->codec->picture_params_size);
200 av_assert0(!ctx->codec_picture_params);
203 pic->nb_param_buffers = 0;
205 if (pic->encode_order == 0) {
206 // Global parameter buffers are set on the first picture only.
208 for (i = 0; i < ctx->nb_global_params; i++) {
209 err = vaapi_encode_make_param_buffer(avctx, pic,
210 VAEncMiscParameterBufferType,
211 (char*)ctx->global_params[i],
212 ctx->global_params_size[i]);
218 if (pic->type == PICTURE_TYPE_IDR && ctx->codec->init_sequence_params) {
219 err = vaapi_encode_make_param_buffer(avctx, pic,
220 VAEncSequenceParameterBufferType,
221 ctx->codec_sequence_params,
222 ctx->codec->sequence_params_size);
227 if (ctx->codec->init_picture_params) {
228 err = ctx->codec->init_picture_params(avctx, pic);
230 av_log(avctx, AV_LOG_ERROR, "Failed to initialise picture "
231 "parameters: %d.\n", err);
234 err = vaapi_encode_make_param_buffer(avctx, pic,
235 VAEncPictureParameterBufferType,
236 pic->codec_picture_params,
237 ctx->codec->picture_params_size);
242 if (pic->type == PICTURE_TYPE_IDR) {
243 if (ctx->codec->write_sequence_header) {
244 bit_len = 8 * sizeof(data);
245 err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
247 av_log(avctx, AV_LOG_ERROR, "Failed to write per-sequence "
248 "header: %d.\n", err);
251 err = vaapi_encode_make_packed_header(avctx, pic,
252 ctx->codec->sequence_header_type,
259 if (ctx->codec->write_picture_header) {
260 bit_len = 8 * sizeof(data);
261 err = ctx->codec->write_picture_header(avctx, pic, data, &bit_len);
263 av_log(avctx, AV_LOG_ERROR, "Failed to write per-picture "
264 "header: %d.\n", err);
267 err = vaapi_encode_make_packed_header(avctx, pic,
268 ctx->codec->picture_header_type,
274 if (ctx->codec->write_extra_buffer) {
276 size_t len = sizeof(data);
278 err = ctx->codec->write_extra_buffer(avctx, pic, i, &type,
280 if (err == AVERROR_EOF)
283 av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
284 "buffer %d: %d.\n", i, err);
288 err = vaapi_encode_make_param_buffer(avctx, pic, type,
295 av_assert0(pic->nb_slices <= MAX_PICTURE_SLICES);
296 for (i = 0; i < pic->nb_slices; i++) {
297 slice = av_mallocz(sizeof(*slice));
299 err = AVERROR(ENOMEM);
302 pic->slices[i] = slice;
304 if (ctx->codec->slice_params_size > 0) {
305 slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size);
306 if (!slice->codec_slice_params) {
307 err = AVERROR(ENOMEM);
312 if (ctx->codec->init_slice_params) {
313 err = ctx->codec->init_slice_params(avctx, pic, slice);
315 av_log(avctx, AV_LOG_ERROR, "Failed to initalise slice "
316 "parameters: %d.\n", err);
321 if (ctx->codec->write_slice_header) {
322 bit_len = 8 * sizeof(data);
323 err = ctx->codec->write_slice_header(avctx, pic, slice,
326 av_log(avctx, AV_LOG_ERROR, "Failed to write per-slice "
327 "header: %d.\n", err);
330 err = vaapi_encode_make_packed_header(avctx, pic,
331 ctx->codec->slice_header_type,
337 if (ctx->codec->init_slice_params) {
338 err = vaapi_encode_make_param_buffer(avctx, pic,
339 VAEncSliceParameterBufferType,
340 slice->codec_slice_params,
341 ctx->codec->slice_params_size);
347 vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
349 if (vas != VA_STATUS_SUCCESS) {
350 av_log(avctx, AV_LOG_ERROR, "Failed to begin picture encode issue: "
351 "%d (%s).\n", vas, vaErrorStr(vas));
353 goto fail_with_picture;
356 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
357 pic->param_buffers, pic->nb_param_buffers);
358 if (vas != VA_STATUS_SUCCESS) {
359 av_log(avctx, AV_LOG_ERROR, "Failed to upload encode parameters: "
360 "%d (%s).\n", vas, vaErrorStr(vas));
362 goto fail_with_picture;
365 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
366 if (vas != VA_STATUS_SUCCESS) {
367 av_log(avctx, AV_LOG_ERROR, "Failed to end picture encode issue: "
368 "%d (%s).\n", vas, vaErrorStr(vas));
373 pic->encode_issued = 1;
375 if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
376 return vaapi_encode_wait(avctx, pic);
381 vaEndPicture(ctx->hwctx->display, ctx->va_context);
383 for(i = 0; i < pic->nb_param_buffers; i++)
384 vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
386 av_freep(&pic->codec_picture_params);
387 av_frame_free(&pic->recon_image);
391 static int vaapi_encode_output(AVCodecContext *avctx,
392 VAAPIEncodePicture *pic, AVPacket *pkt)
394 VAAPIEncodeContext *ctx = avctx->priv_data;
395 VACodedBufferSegment *buf_list, *buf;
399 err = vaapi_encode_wait(avctx, pic);
404 vas = vaMapBuffer(ctx->hwctx->display, pic->output_buffer,
406 if (vas != VA_STATUS_SUCCESS) {
407 av_log(avctx, AV_LOG_ERROR, "Failed to map output buffers: "
408 "%d (%s).\n", vas, vaErrorStr(vas));
413 for (buf = buf_list; buf; buf = buf->next) {
414 av_log(avctx, AV_LOG_DEBUG, "Output buffer: %u bytes "
415 "(status %08x).\n", buf->size, buf->status);
417 err = av_new_packet(pkt, buf->size);
421 memcpy(pkt->data, buf->buf, buf->size);
424 if (pic->type == PICTURE_TYPE_IDR)
425 pkt->flags |= AV_PKT_FLAG_KEY;
429 vas = vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
430 if (vas != VA_STATUS_SUCCESS) {
431 av_log(avctx, AV_LOG_ERROR, "Failed to unmap output buffers: "
432 "%d (%s).\n", vas, vaErrorStr(vas));
437 vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
438 pic->output_buffer = VA_INVALID_ID;
440 av_log(avctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n",
441 pic->display_order, pic->encode_order);
445 if (pic->output_buffer != VA_INVALID_ID) {
446 vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
447 vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
448 pic->output_buffer = VA_INVALID_ID;
453 static int vaapi_encode_discard(AVCodecContext *avctx,
454 VAAPIEncodePicture *pic)
456 VAAPIEncodeContext *ctx = avctx->priv_data;
458 vaapi_encode_wait(avctx, pic);
460 if (pic->output_buffer != VA_INVALID_ID) {
461 av_log(avctx, AV_LOG_DEBUG, "Discard output for pic "
462 "%"PRId64"/%"PRId64".\n",
463 pic->display_order, pic->encode_order);
465 vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
466 pic->output_buffer = VA_INVALID_ID;
472 static VAAPIEncodePicture *vaapi_encode_alloc(void)
474 VAAPIEncodePicture *pic;
476 pic = av_mallocz(sizeof(*pic));
480 pic->input_surface = VA_INVALID_ID;
481 pic->recon_surface = VA_INVALID_ID;
482 pic->output_buffer = VA_INVALID_ID;
487 static int vaapi_encode_free(AVCodecContext *avctx,
488 VAAPIEncodePicture *pic)
492 if (pic->encode_issued)
493 vaapi_encode_discard(avctx, pic);
495 for (i = 0; i < pic->nb_slices; i++) {
496 av_freep(&pic->slices[i]->priv_data);
497 av_freep(&pic->slices[i]->codec_slice_params);
498 av_freep(&pic->slices[i]);
500 av_freep(&pic->codec_picture_params);
502 av_frame_free(&pic->input_image);
503 av_frame_free(&pic->recon_image);
505 // Output buffer should already be destroyed.
506 av_assert0(pic->output_buffer == VA_INVALID_ID);
508 av_freep(&pic->priv_data);
509 av_freep(&pic->codec_picture_params);
516 static int vaapi_encode_step(AVCodecContext *avctx,
517 VAAPIEncodePicture *target)
519 VAAPIEncodeContext *ctx = avctx->priv_data;
520 VAAPIEncodePicture *pic;
523 if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING ||
524 ctx->issue_mode == ISSUE_MODE_MINIMISE_LATENCY) {
525 // These two modes are equivalent, except that we wait for
526 // immediate completion on each operation if serialised.
529 // No target, nothing to do yet.
533 if (target->encode_complete) {
539 for (i = 0; i < pic->nb_refs; i++) {
540 if (!pic->refs[i]->encode_complete) {
541 err = vaapi_encode_step(avctx, pic->refs[i]);
547 err = vaapi_encode_issue(avctx, pic);
551 } else if (ctx->issue_mode == ISSUE_MODE_MAXIMISE_THROUGHPUT) {
556 for (pic = ctx->pic_start; pic; pic = pic->next) {
557 if (!pic->input_available || pic->encode_issued)
559 for (i = 0; i < pic->nb_refs; i++) {
560 if (!pic->refs[i]->encode_issued)
563 if (i < pic->nb_refs)
565 err = vaapi_encode_issue(avctx, pic);
573 av_assert0(target->encode_issued && "broken dependencies?");
583 static int vaapi_encode_get_next(AVCodecContext *avctx,
584 VAAPIEncodePicture **pic_out)
586 VAAPIEncodeContext *ctx = avctx->priv_data;
587 VAAPIEncodePicture *start, *end, *pic;
590 for (pic = ctx->pic_start; pic; pic = pic->next) {
592 av_assert0(pic->display_order + 1 == pic->next->display_order);
593 if (pic->display_order == ctx->input_order) {
599 if (ctx->input_order == 0) {
600 // First frame is always an IDR frame.
601 av_assert0(!ctx->pic_start && !ctx->pic_end);
603 pic = vaapi_encode_alloc();
605 return AVERROR(ENOMEM);
607 pic->type = PICTURE_TYPE_IDR;
608 pic->display_order = 0;
609 pic->encode_order = 0;
611 ctx->pic_start = ctx->pic_end = pic;
617 pic = vaapi_encode_alloc();
619 return AVERROR(ENOMEM);
621 if (ctx->p_per_i == 0 || ctx->p_counter == ctx->p_per_i) {
622 if (ctx->i_per_idr == 0 || ctx->i_counter == ctx->i_per_idr) {
623 pic->type = PICTURE_TYPE_IDR;
626 pic->type = PICTURE_TYPE_I;
631 pic->type = PICTURE_TYPE_P;
632 pic->refs[0] = ctx->pic_end;
638 if (pic->type != PICTURE_TYPE_IDR) {
639 // If that was not an IDR frame, add B-frames display-before and
642 for (i = 0; i < ctx->b_per_p; i++) {
643 pic = vaapi_encode_alloc();
647 pic->type = PICTURE_TYPE_B;
648 pic->refs[0] = ctx->pic_end;
653 pic->display_order = ctx->input_order + ctx->b_per_p - i - 1;
654 pic->encode_order = pic->display_order + 1;
659 for (i = 0, pic = start; pic; i++, pic = pic->next) {
660 pic->display_order = ctx->input_order + i;
661 if (end->type == PICTURE_TYPE_IDR)
662 pic->encode_order = ctx->input_order + i;
664 pic->encode_order = ctx->input_order;
666 pic->encode_order = ctx->input_order + i + 1;
669 av_assert0(ctx->pic_end);
670 ctx->pic_end->next = start;
675 av_log(avctx, AV_LOG_DEBUG, "Pictures:");
676 for (pic = ctx->pic_start; pic; pic = pic->next) {
677 av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
678 picture_type_name[pic->type],
679 pic->display_order, pic->encode_order);
681 av_log(avctx, AV_LOG_DEBUG, "\n");
688 vaapi_encode_free(avctx, start);
691 return AVERROR(ENOMEM);
694 static int vaapi_encode_mangle_end(AVCodecContext *avctx)
696 VAAPIEncodeContext *ctx = avctx->priv_data;
697 VAAPIEncodePicture *pic, *last_pic, *next;
699 // Find the last picture we actually have input for.
700 for (pic = ctx->pic_start; pic; pic = pic->next) {
701 if (!pic->input_available)
707 av_assert0(last_pic);
709 if (last_pic->type == PICTURE_TYPE_B) {
710 // Some fixing up is required. Change the type of this
711 // picture to P, then modify preceding B references which
712 // point beyond it to point at it instead.
714 last_pic->type = PICTURE_TYPE_P;
715 last_pic->encode_order = last_pic->refs[1]->encode_order;
717 for (pic = ctx->pic_start; pic != last_pic; pic = pic->next) {
718 if (pic->type == PICTURE_TYPE_B &&
719 pic->refs[1] == last_pic->refs[1])
720 pic->refs[1] = last_pic;
723 last_pic->nb_refs = 1;
724 last_pic->refs[1] = NULL;
726 // We can use the current structure (no references point
727 // beyond the end), but there are unused pics to discard.
730 // Discard all following pics, they will never be used.
731 for (pic = last_pic->next; pic; pic = next) {
733 vaapi_encode_free(avctx, pic);
736 last_pic->next = NULL;
737 ctx->pic_end = last_pic;
740 // Input is available for all pictures, so we don't need to
744 av_log(avctx, AV_LOG_DEBUG, "Pictures at end of stream:");
745 for (pic = ctx->pic_start; pic; pic = pic->next) {
746 av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
747 picture_type_name[pic->type],
748 pic->display_order, pic->encode_order);
750 av_log(avctx, AV_LOG_DEBUG, "\n");
755 static int vaapi_encode_clear_old(AVCodecContext *avctx)
757 VAAPIEncodeContext *ctx = avctx->priv_data;
758 VAAPIEncodePicture *pic, *old;
761 while (ctx->pic_start != ctx->pic_end) {
762 old = ctx->pic_start;
763 if (old->encode_order > ctx->output_order)
766 for (pic = old->next; pic; pic = pic->next) {
767 if (pic->encode_complete)
769 for (i = 0; i < pic->nb_refs; i++) {
770 if (pic->refs[i] == old) {
771 // We still need this picture because it's referred to
772 // directly by a later one, so it and all following
773 // pictures have to stay.
779 pic = ctx->pic_start;
780 ctx->pic_start = pic->next;
781 vaapi_encode_free(avctx, pic);
787 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
788 const AVFrame *input_image, int *got_packet)
790 VAAPIEncodeContext *ctx = avctx->priv_data;
791 VAAPIEncodePicture *pic;
795 av_log(avctx, AV_LOG_DEBUG, "Encode frame: %ux%u (%"PRId64").\n",
796 input_image->width, input_image->height, input_image->pts);
798 err = vaapi_encode_get_next(avctx, &pic);
800 av_log(avctx, AV_LOG_ERROR, "Input setup failed: %d.\n", err);
804 pic->input_image = av_frame_alloc();
805 if (!pic->input_image) {
806 err = AVERROR(ENOMEM);
809 err = av_frame_ref(pic->input_image, input_image);
812 pic->input_surface = (VASurfaceID)(uintptr_t)input_image->data[3];
813 pic->pts = input_image->pts;
815 if (ctx->input_order == 0)
816 ctx->first_pts = pic->pts;
817 if (ctx->input_order == ctx->decode_delay)
818 ctx->dts_pts_diff = pic->pts - ctx->first_pts;
819 if (ctx->output_delay > 0)
820 ctx->ts_ring[ctx->input_order % (3 * ctx->output_delay)] = pic->pts;
822 pic->input_available = 1;
825 if (!ctx->end_of_stream) {
826 err = vaapi_encode_mangle_end(avctx);
829 ctx->end_of_stream = 1;
835 av_assert0(ctx->output_order + ctx->output_delay + 1 == ctx->input_order);
837 for (pic = ctx->pic_start; pic; pic = pic->next)
838 if (pic->encode_order == ctx->output_order)
841 // pic can be null here if we don't have a specific target in this
842 // iteration. We might still issue encodes if things can be overlapped,
843 // even though we don't intend to output anything.
845 err = vaapi_encode_step(avctx, pic);
847 av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err);
854 err = vaapi_encode_output(avctx, pic, pkt);
856 av_log(avctx, AV_LOG_ERROR, "Output failed: %d.\n", err);
860 if (ctx->output_delay == 0) {
862 } else if (ctx->output_order < ctx->decode_delay) {
863 if (ctx->ts_ring[ctx->output_order] < INT64_MIN + ctx->dts_pts_diff)
864 pkt->dts = INT64_MIN;
866 pkt->dts = ctx->ts_ring[ctx->output_order] - ctx->dts_pts_diff;
868 pkt->dts = ctx->ts_ring[(ctx->output_order - ctx->decode_delay) %
869 (3 * ctx->output_delay)];
875 err = vaapi_encode_clear_old(avctx);
877 av_log(avctx, AV_LOG_ERROR, "List clearing failed: %d.\n", err);
884 // Unclear what to clean up on failure. There are probably some things we
885 // could do usefully clean up here, but for now just leave them for uninit()
890 av_cold int ff_vaapi_encode_init(AVCodecContext *avctx,
891 const VAAPIEncodeType *type)
893 VAAPIEncodeContext *ctx = avctx->priv_data;
894 AVVAAPIFramesContext *recon_hwctx = NULL;
895 AVVAAPIHWConfig *hwconfig = NULL;
896 AVHWFramesConstraints *constraints = NULL;
897 enum AVPixelFormat recon_format;
901 if (!avctx->hw_frames_ctx) {
902 av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
903 "required to associate the encoding device.\n");
904 return AVERROR(EINVAL);
908 ctx->codec_options = ctx->codec_options_data;
910 ctx->priv_data = av_mallocz(type->priv_data_size);
911 if (!ctx->priv_data) {
912 err = AVERROR(ENOMEM);
916 ctx->input_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
917 if (!ctx->input_frames_ref) {
918 err = AVERROR(ENOMEM);
921 ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
923 ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
924 if (!ctx->device_ref) {
925 err = AVERROR(ENOMEM);
928 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
929 ctx->hwctx = ctx->device->hwctx;
931 err = ctx->codec->init(avctx);
935 vas = vaCreateConfig(ctx->hwctx->display,
936 ctx->va_profile, ctx->va_entrypoint,
937 ctx->config_attributes, ctx->nb_config_attributes,
939 if (vas != VA_STATUS_SUCCESS) {
940 av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
941 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
946 hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
948 err = AVERROR(ENOMEM);
951 hwconfig->config_id = ctx->va_config;
953 constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
956 err = AVERROR(ENOMEM);
960 // Probably we can use the input surface format as the surface format
961 // of the reconstructed frames. If not, we just pick the first (only?)
962 // format in the valid list and hope that it all works.
963 recon_format = AV_PIX_FMT_NONE;
964 if (constraints->valid_sw_formats) {
965 for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
966 if (ctx->input_frames->sw_format ==
967 constraints->valid_sw_formats[i]) {
968 recon_format = ctx->input_frames->sw_format;
972 if (recon_format == AV_PIX_FMT_NONE)
973 recon_format = constraints->valid_sw_formats[i];
975 // No idea what to use; copy input format.
976 recon_format = ctx->input_frames->sw_format;
978 av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
979 "reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
981 if (ctx->aligned_width < constraints->min_width ||
982 ctx->aligned_height < constraints->min_height ||
983 ctx->aligned_width > constraints->max_width ||
984 ctx->aligned_height > constraints->max_height) {
985 av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at "
986 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
987 ctx->aligned_width, ctx->aligned_height,
988 constraints->min_width, constraints->max_width,
989 constraints->min_height, constraints->max_height);
990 err = AVERROR(EINVAL);
995 av_hwframe_constraints_free(&constraints);
997 ctx->recon_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
998 if (!ctx->recon_frames_ref) {
999 err = AVERROR(ENOMEM);
1002 ctx->recon_frames = (AVHWFramesContext*)ctx->recon_frames_ref->data;
1004 ctx->recon_frames->format = AV_PIX_FMT_VAAPI;
1005 ctx->recon_frames->sw_format = recon_format;
1006 ctx->recon_frames->width = ctx->aligned_width;
1007 ctx->recon_frames->height = ctx->aligned_height;
1008 ctx->recon_frames->initial_pool_size = ctx->nb_recon_frames;
1010 err = av_hwframe_ctx_init(ctx->recon_frames_ref);
1012 av_log(avctx, AV_LOG_ERROR, "Failed to initialise reconstructed "
1013 "frame context: %d.\n", err);
1016 recon_hwctx = ctx->recon_frames->hwctx;
1018 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
1019 ctx->aligned_width, ctx->aligned_height,
1021 recon_hwctx->surface_ids,
1022 recon_hwctx->nb_surfaces,
1024 if (vas != VA_STATUS_SUCCESS) {
1025 av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
1026 "context: %d (%s).\n", vas, vaErrorStr(vas));
1031 ctx->input_order = 0;
1032 ctx->output_delay = avctx->max_b_frames;
1033 ctx->decode_delay = 1;
1034 ctx->output_order = - ctx->output_delay - 1;
1036 if (ctx->codec->sequence_params_size > 0) {
1037 ctx->codec_sequence_params =
1038 av_mallocz(ctx->codec->sequence_params_size);
1039 if (!ctx->codec_sequence_params) {
1040 err = AVERROR(ENOMEM);
1044 if (ctx->codec->picture_params_size > 0) {
1045 ctx->codec_picture_params =
1046 av_mallocz(ctx->codec->picture_params_size);
1047 if (!ctx->codec_picture_params) {
1048 err = AVERROR(ENOMEM);
1053 if (ctx->codec->init_sequence_params) {
1054 err = ctx->codec->init_sequence_params(avctx);
1056 av_log(avctx, AV_LOG_ERROR, "Codec sequence initialisation "
1057 "failed: %d.\n", err);
1062 // All I are IDR for now.
1064 ctx->p_per_i = ((avctx->gop_size + avctx->max_b_frames) /
1065 (avctx->max_b_frames + 1));
1066 ctx->b_per_p = avctx->max_b_frames;
1068 // This should be configurable somehow. (Needs testing on a machine
1069 // where it actually overlaps properly, though.)
1070 ctx->issue_mode = ISSUE_MODE_MAXIMISE_THROUGHPUT;
1075 av_freep(&hwconfig);
1076 av_hwframe_constraints_free(&constraints);
1077 ff_vaapi_encode_close(avctx);
1081 av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
1083 VAAPIEncodeContext *ctx = avctx->priv_data;
1084 VAAPIEncodePicture *pic, *next;
1086 for (pic = ctx->pic_start; pic; pic = next) {
1088 vaapi_encode_free(avctx, pic);
1091 if (ctx->va_context != VA_INVALID_ID)
1092 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
1094 if (ctx->va_config != VA_INVALID_ID)
1095 vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
1097 if (ctx->codec->close)
1098 ctx->codec->close(avctx);
1100 av_freep(&ctx->codec_sequence_params);
1101 av_freep(&ctx->codec_picture_params);
1103 av_buffer_unref(&ctx->recon_frames_ref);
1104 av_buffer_unref(&ctx->input_frames_ref);
1105 av_buffer_unref(&ctx->device_ref);
1107 av_freep(&ctx->priv_data);