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->type == PICTURE_TYPE_IDR && ctx->codec->init_sequence_params) {
206 err = vaapi_encode_make_param_buffer(avctx, pic,
207 VAEncSequenceParameterBufferType,
208 ctx->codec_sequence_params,
209 ctx->codec->sequence_params_size);
214 if (ctx->codec->init_picture_params) {
215 err = ctx->codec->init_picture_params(avctx, pic);
217 av_log(avctx, AV_LOG_ERROR, "Failed to initialise picture "
218 "parameters: %d.\n", err);
221 err = vaapi_encode_make_param_buffer(avctx, pic,
222 VAEncPictureParameterBufferType,
223 pic->codec_picture_params,
224 ctx->codec->picture_params_size);
229 if (pic->type == PICTURE_TYPE_IDR) {
230 if (ctx->codec->write_sequence_header) {
231 bit_len = 8 * sizeof(data);
232 err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
234 av_log(avctx, AV_LOG_ERROR, "Failed to write per-sequence "
235 "header: %d.\n", err);
238 err = vaapi_encode_make_packed_header(avctx, pic,
239 ctx->codec->sequence_header_type,
246 if (ctx->codec->write_picture_header) {
247 bit_len = 8 * sizeof(data);
248 err = ctx->codec->write_picture_header(avctx, pic, data, &bit_len);
250 av_log(avctx, AV_LOG_ERROR, "Failed to write per-picture "
251 "header: %d.\n", err);
254 err = vaapi_encode_make_packed_header(avctx, pic,
255 ctx->codec->picture_header_type,
261 if (ctx->codec->write_extra_buffer) {
263 size_t len = sizeof(data);
265 err = ctx->codec->write_extra_buffer(avctx, pic, i, &type,
267 if (err == AVERROR_EOF)
270 av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
271 "buffer %d: %d.\n", i, err);
275 err = vaapi_encode_make_param_buffer(avctx, pic, type,
282 av_assert0(pic->nb_slices <= MAX_PICTURE_SLICES);
283 for (i = 0; i < pic->nb_slices; i++) {
284 slice = av_mallocz(sizeof(*slice));
286 err = AVERROR(ENOMEM);
289 pic->slices[i] = slice;
291 if (ctx->codec->slice_params_size > 0) {
292 slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size);
293 if (!slice->codec_slice_params) {
294 err = AVERROR(ENOMEM);
299 if (ctx->codec->init_slice_params) {
300 err = ctx->codec->init_slice_params(avctx, pic, slice);
302 av_log(avctx, AV_LOG_ERROR, "Failed to initalise slice "
303 "parameters: %d.\n", err);
308 if (ctx->codec->write_slice_header) {
309 bit_len = 8 * sizeof(data);
310 err = ctx->codec->write_slice_header(avctx, pic, slice,
313 av_log(avctx, AV_LOG_ERROR, "Failed to write per-slice "
314 "header: %d.\n", err);
317 err = vaapi_encode_make_packed_header(avctx, pic,
318 ctx->codec->slice_header_type,
324 if (ctx->codec->init_slice_params) {
325 err = vaapi_encode_make_param_buffer(avctx, pic,
326 VAEncSliceParameterBufferType,
327 slice->codec_slice_params,
328 ctx->codec->slice_params_size);
334 vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
336 if (vas != VA_STATUS_SUCCESS) {
337 av_log(avctx, AV_LOG_ERROR, "Failed to begin picture encode issue: "
338 "%d (%s).\n", vas, vaErrorStr(vas));
340 goto fail_with_picture;
343 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
344 pic->param_buffers, pic->nb_param_buffers);
345 if (vas != VA_STATUS_SUCCESS) {
346 av_log(avctx, AV_LOG_ERROR, "Failed to upload encode parameters: "
347 "%d (%s).\n", vas, vaErrorStr(vas));
349 goto fail_with_picture;
352 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
353 if (vas != VA_STATUS_SUCCESS) {
354 av_log(avctx, AV_LOG_ERROR, "Failed to end picture encode issue: "
355 "%d (%s).\n", vas, vaErrorStr(vas));
360 pic->encode_issued = 1;
362 if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
363 return vaapi_encode_wait(avctx, pic);
368 vaEndPicture(ctx->hwctx->display, ctx->va_context);
370 for(i = 0; i < pic->nb_param_buffers; i++)
371 vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
373 av_freep(&pic->codec_picture_params);
374 av_frame_free(&pic->recon_image);
378 static int vaapi_encode_output(AVCodecContext *avctx,
379 VAAPIEncodePicture *pic, AVPacket *pkt)
381 VAAPIEncodeContext *ctx = avctx->priv_data;
382 VACodedBufferSegment *buf_list, *buf;
386 err = vaapi_encode_wait(avctx, pic);
391 vas = vaMapBuffer(ctx->hwctx->display, pic->output_buffer,
393 if (vas != VA_STATUS_SUCCESS) {
394 av_log(avctx, AV_LOG_ERROR, "Failed to map output buffers: "
395 "%d (%s).\n", vas, vaErrorStr(vas));
400 for (buf = buf_list; buf; buf = buf->next) {
401 av_log(avctx, AV_LOG_DEBUG, "Output buffer: %u bytes "
402 "(status %08x).\n", buf->size, buf->status);
404 err = av_new_packet(pkt, buf->size);
408 memcpy(pkt->data, buf->buf, buf->size);
411 if (pic->type == PICTURE_TYPE_IDR)
412 pkt->flags |= AV_PKT_FLAG_KEY;
416 vas = vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
417 if (vas != VA_STATUS_SUCCESS) {
418 av_log(avctx, AV_LOG_ERROR, "Failed to unmap output buffers: "
419 "%d (%s).\n", vas, vaErrorStr(vas));
424 vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
425 pic->output_buffer = VA_INVALID_ID;
427 av_log(avctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n",
428 pic->display_order, pic->encode_order);
432 if (pic->output_buffer != VA_INVALID_ID) {
433 vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
434 vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
435 pic->output_buffer = VA_INVALID_ID;
440 static int vaapi_encode_discard(AVCodecContext *avctx,
441 VAAPIEncodePicture *pic)
443 VAAPIEncodeContext *ctx = avctx->priv_data;
445 vaapi_encode_wait(avctx, pic);
447 if (pic->output_buffer != VA_INVALID_ID) {
448 av_log(avctx, AV_LOG_DEBUG, "Discard output for pic "
449 "%"PRId64"/%"PRId64".\n",
450 pic->display_order, pic->encode_order);
452 vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
453 pic->output_buffer = VA_INVALID_ID;
459 static VAAPIEncodePicture *vaapi_encode_alloc(void)
461 VAAPIEncodePicture *pic;
463 pic = av_mallocz(sizeof(*pic));
467 pic->input_surface = VA_INVALID_ID;
468 pic->recon_surface = VA_INVALID_ID;
469 pic->output_buffer = VA_INVALID_ID;
474 static int vaapi_encode_free(AVCodecContext *avctx,
475 VAAPIEncodePicture *pic)
479 if (pic->encode_issued)
480 vaapi_encode_discard(avctx, pic);
482 for (i = 0; i < pic->nb_slices; i++) {
483 av_freep(&pic->slices[i]->priv_data);
484 av_freep(&pic->slices[i]->codec_slice_params);
485 av_freep(&pic->slices[i]);
487 av_freep(&pic->codec_picture_params);
489 av_frame_free(&pic->input_image);
490 av_frame_free(&pic->recon_image);
492 // Output buffer should already be destroyed.
493 av_assert0(pic->output_buffer == VA_INVALID_ID);
495 av_freep(&pic->priv_data);
496 av_freep(&pic->codec_picture_params);
503 static int vaapi_encode_step(AVCodecContext *avctx,
504 VAAPIEncodePicture *target)
506 VAAPIEncodeContext *ctx = avctx->priv_data;
507 VAAPIEncodePicture *pic;
510 if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING ||
511 ctx->issue_mode == ISSUE_MODE_MINIMISE_LATENCY) {
512 // These two modes are equivalent, except that we wait for
513 // immediate completion on each operation if serialised.
516 // No target, nothing to do yet.
520 if (target->encode_complete) {
526 for (i = 0; i < pic->nb_refs; i++) {
527 if (!pic->refs[i]->encode_complete) {
528 err = vaapi_encode_step(avctx, pic->refs[i]);
534 err = vaapi_encode_issue(avctx, pic);
538 } else if (ctx->issue_mode == ISSUE_MODE_MAXIMISE_THROUGHPUT) {
543 for (pic = ctx->pic_start; pic; pic = pic->next) {
544 if (!pic->input_available || pic->encode_issued)
546 for (i = 0; i < pic->nb_refs; i++) {
547 if (!pic->refs[i]->encode_issued)
550 if (i < pic->nb_refs)
552 err = vaapi_encode_issue(avctx, pic);
560 av_assert0(target->encode_issued && "broken dependencies?");
570 static int vaapi_encode_get_next(AVCodecContext *avctx,
571 VAAPIEncodePicture **pic_out)
573 VAAPIEncodeContext *ctx = avctx->priv_data;
574 VAAPIEncodePicture *start, *end, *pic;
577 for (pic = ctx->pic_start; pic; pic = pic->next) {
579 av_assert0(pic->display_order + 1 == pic->next->display_order);
580 if (pic->display_order == ctx->input_order) {
586 if (ctx->input_order == 0) {
587 // First frame is always an IDR frame.
588 av_assert0(!ctx->pic_start && !ctx->pic_end);
590 pic = vaapi_encode_alloc();
592 return AVERROR(ENOMEM);
594 pic->type = PICTURE_TYPE_IDR;
595 pic->display_order = 0;
596 pic->encode_order = 0;
598 ctx->pic_start = ctx->pic_end = pic;
604 pic = vaapi_encode_alloc();
606 return AVERROR(ENOMEM);
608 if (ctx->p_per_i == 0 || ctx->p_counter == ctx->p_per_i) {
609 if (ctx->i_per_idr == 0 || ctx->i_counter == ctx->i_per_idr) {
610 pic->type = PICTURE_TYPE_IDR;
613 pic->type = PICTURE_TYPE_I;
618 pic->type = PICTURE_TYPE_P;
619 pic->refs[0] = ctx->pic_end;
625 if (pic->type != PICTURE_TYPE_IDR) {
626 // If that was not an IDR frame, add B frames display-before and
629 for (i = 0; i < ctx->b_per_p; i++) {
630 pic = vaapi_encode_alloc();
634 pic->type = PICTURE_TYPE_B;
635 pic->refs[0] = ctx->pic_end;
640 pic->display_order = ctx->input_order + ctx->b_per_p - i - 1;
641 pic->encode_order = pic->display_order + 1;
646 for (i = 0, pic = start; pic; i++, pic = pic->next) {
647 pic->display_order = ctx->input_order + i;
648 if (end->type == PICTURE_TYPE_IDR)
649 pic->encode_order = ctx->input_order + i;
651 pic->encode_order = ctx->input_order;
653 pic->encode_order = ctx->input_order + i + 1;
656 av_assert0(ctx->pic_end);
657 ctx->pic_end->next = start;
662 av_log(avctx, AV_LOG_DEBUG, "Pictures:");
663 for (pic = ctx->pic_start; pic; pic = pic->next) {
664 av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
665 picture_type_name[pic->type],
666 pic->display_order, pic->encode_order);
668 av_log(avctx, AV_LOG_DEBUG, "\n");
675 vaapi_encode_free(avctx, start);
678 return AVERROR(ENOMEM);
681 static int vaapi_encode_mangle_end(AVCodecContext *avctx)
683 VAAPIEncodeContext *ctx = avctx->priv_data;
684 VAAPIEncodePicture *pic, *last_pic, *next;
686 // Find the last picture we actually have input for.
687 for (pic = ctx->pic_start; pic; pic = pic->next) {
688 if (!pic->input_available)
694 av_assert0(last_pic);
696 if (last_pic->type == PICTURE_TYPE_B) {
697 // Some fixing up is required. Change the type of this
698 // picture to P, then modify preceeding B references which
699 // point beyond it to point at it instead.
701 last_pic->type = PICTURE_TYPE_P;
702 last_pic->encode_order = last_pic->refs[1]->encode_order;
704 for (pic = ctx->pic_start; pic != last_pic; pic = pic->next) {
705 if (pic->type == PICTURE_TYPE_B &&
706 pic->refs[1] == last_pic->refs[1])
707 pic->refs[1] = last_pic;
710 last_pic->nb_refs = 1;
711 last_pic->refs[1] = NULL;
713 // We can use the current structure (no references point
714 // beyond the end), but there are unused pics to discard.
717 // Discard all following pics, they will never be used.
718 for (pic = last_pic->next; pic; pic = next) {
720 vaapi_encode_free(avctx, pic);
723 last_pic->next = NULL;
724 ctx->pic_end = last_pic;
727 // Input is available for all pictures, so we don't need to
731 av_log(avctx, AV_LOG_DEBUG, "Pictures at end of stream:");
732 for (pic = ctx->pic_start; pic; pic = pic->next) {
733 av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
734 picture_type_name[pic->type],
735 pic->display_order, pic->encode_order);
737 av_log(avctx, AV_LOG_DEBUG, "\n");
742 static int vaapi_encode_clear_old(AVCodecContext *avctx)
744 VAAPIEncodeContext *ctx = avctx->priv_data;
745 VAAPIEncodePicture *pic, *old;
748 while (ctx->pic_start != ctx->pic_end) {
749 old = ctx->pic_start;
750 if (old->encode_order > ctx->output_order)
753 for (pic = old->next; pic; pic = pic->next) {
754 if (pic->encode_complete)
756 for (i = 0; i < pic->nb_refs; i++) {
757 if (pic->refs[i] == old) {
758 // We still need this picture because it's referred to
759 // directly by a later one, so it and all following
760 // pictures have to stay.
766 pic = ctx->pic_start;
767 ctx->pic_start = pic->next;
768 vaapi_encode_free(avctx, pic);
774 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
775 const AVFrame *input_image, int *got_packet)
777 VAAPIEncodeContext *ctx = avctx->priv_data;
778 VAAPIEncodePicture *pic;
782 av_log(avctx, AV_LOG_DEBUG, "Encode frame: %ux%u (%"PRId64").\n",
783 input_image->width, input_image->height, input_image->pts);
785 err = vaapi_encode_get_next(avctx, &pic);
787 av_log(avctx, AV_LOG_ERROR, "Input setup failed: %d.\n", err);
791 pic->input_image = av_frame_alloc();
792 if (!pic->input_image) {
793 err = AVERROR(ENOMEM);
796 err = av_frame_ref(pic->input_image, input_image);
799 pic->input_surface = (VASurfaceID)(uintptr_t)input_image->data[3];
800 pic->pts = input_image->pts;
802 if (ctx->input_order == 0)
803 ctx->first_pts = pic->pts;
804 if (ctx->input_order == ctx->decode_delay)
805 ctx->dts_pts_diff = pic->pts - ctx->first_pts;
806 if (ctx->output_delay > 0)
807 ctx->ts_ring[ctx->input_order % (3 * ctx->output_delay)] = pic->pts;
809 pic->input_available = 1;
812 if (!ctx->end_of_stream) {
813 err = vaapi_encode_mangle_end(avctx);
816 ctx->end_of_stream = 1;
822 av_assert0(ctx->output_order + ctx->output_delay + 1 == ctx->input_order);
824 for (pic = ctx->pic_start; pic; pic = pic->next)
825 if (pic->encode_order == ctx->output_order)
828 // pic can be null here if we don't have a specific target in this
829 // iteration. We might still issue encodes if things can be overlapped,
830 // even though we don't intend to output anything.
832 err = vaapi_encode_step(avctx, pic);
834 av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err);
841 err = vaapi_encode_output(avctx, pic, pkt);
843 av_log(avctx, AV_LOG_ERROR, "Output failed: %d.\n", err);
847 if (ctx->output_delay == 0) {
849 } else if (ctx->output_order < ctx->decode_delay) {
850 if (ctx->ts_ring[ctx->output_order] < INT64_MIN + ctx->dts_pts_diff)
851 pkt->dts = INT64_MIN;
853 pkt->dts = ctx->ts_ring[ctx->output_order] - ctx->dts_pts_diff;
855 pkt->dts = ctx->ts_ring[(ctx->output_order - ctx->decode_delay) %
856 (3 * ctx->output_delay)];
862 err = vaapi_encode_clear_old(avctx);
864 av_log(avctx, AV_LOG_ERROR, "List clearing failed: %d.\n", err);
871 // Unclear what to clean up on failure. There are probably some things we
872 // could do usefully clean up here, but for now just leave them for uninit()
877 av_cold int ff_vaapi_encode_init(AVCodecContext *avctx,
878 const VAAPIEncodeType *type)
880 VAAPIEncodeContext *ctx = avctx->priv_data;
881 AVVAAPIFramesContext *recon_hwctx = NULL;
882 AVVAAPIHWConfig *hwconfig = NULL;
883 AVHWFramesConstraints *constraints = NULL;
884 enum AVPixelFormat recon_format;
888 if (!avctx->hw_frames_ctx) {
889 av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
890 "required to associate the encoding device.\n");
891 return AVERROR(EINVAL);
896 ctx->priv_data = av_mallocz(type->priv_data_size);
897 if (!ctx->priv_data) {
898 err = AVERROR(ENOMEM);
902 ctx->input_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
903 if (!ctx->input_frames_ref) {
904 err = AVERROR(ENOMEM);
907 ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
909 ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
910 if (!ctx->device_ref) {
911 err = AVERROR(ENOMEM);
914 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
915 ctx->hwctx = ctx->device->hwctx;
917 err = ctx->codec->init(avctx);
921 vas = vaCreateConfig(ctx->hwctx->display,
922 ctx->va_profile, ctx->va_entrypoint,
923 ctx->config_attributes, ctx->nb_config_attributes,
925 if (vas != VA_STATUS_SUCCESS) {
926 av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
927 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
932 hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
934 err = AVERROR(ENOMEM);
937 hwconfig->config_id = ctx->va_config;
939 constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
942 err = AVERROR(ENOMEM);
946 // Probably we can use the input surface format as the surface format
947 // of the reconstructed frames. If not, we just pick the first (only?)
948 // format in the valid list and hope that it all works.
949 recon_format = AV_PIX_FMT_NONE;
950 if (constraints->valid_sw_formats) {
951 for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
952 if (ctx->input_frames->sw_format ==
953 constraints->valid_sw_formats[i]) {
954 recon_format = ctx->input_frames->sw_format;
958 if (recon_format == AV_PIX_FMT_NONE)
959 recon_format = constraints->valid_sw_formats[i];
961 // No idea what to use; copy input format.
962 recon_format = ctx->input_frames->sw_format;
964 av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
965 "reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
967 if (ctx->aligned_width < constraints->min_width ||
968 ctx->aligned_height < constraints->min_height ||
969 ctx->aligned_width > constraints->max_width ||
970 ctx->aligned_height > constraints->max_height) {
971 av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at "
972 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
973 ctx->aligned_width, ctx->aligned_height,
974 constraints->min_width, constraints->max_width,
975 constraints->min_height, constraints->max_height);
976 err = AVERROR(EINVAL);
981 av_hwframe_constraints_free(&constraints);
983 ctx->recon_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
984 if (!ctx->recon_frames_ref) {
985 err = AVERROR(ENOMEM);
988 ctx->recon_frames = (AVHWFramesContext*)ctx->recon_frames_ref->data;
990 ctx->recon_frames->format = AV_PIX_FMT_VAAPI;
991 ctx->recon_frames->sw_format = recon_format;
992 ctx->recon_frames->width = ctx->aligned_width;
993 ctx->recon_frames->height = ctx->aligned_height;
994 ctx->recon_frames->initial_pool_size = ctx->nb_recon_frames;
996 err = av_hwframe_ctx_init(ctx->recon_frames_ref);
998 av_log(avctx, AV_LOG_ERROR, "Failed to initialise reconstructed "
999 "frame context: %d.\n", err);
1002 recon_hwctx = ctx->recon_frames->hwctx;
1004 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
1005 ctx->aligned_width, ctx->aligned_height,
1007 recon_hwctx->surface_ids,
1008 recon_hwctx->nb_surfaces,
1010 if (vas != VA_STATUS_SUCCESS) {
1011 av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
1012 "context: %d (%s).\n", vas, vaErrorStr(vas));
1017 ctx->input_order = 0;
1018 ctx->output_delay = avctx->max_b_frames;
1019 ctx->decode_delay = 1;
1020 ctx->output_order = - ctx->output_delay - 1;
1022 if (ctx->codec->sequence_params_size > 0) {
1023 ctx->codec_sequence_params =
1024 av_mallocz(ctx->codec->sequence_params_size);
1025 if (!ctx->codec_sequence_params) {
1026 err = AVERROR(ENOMEM);
1030 if (ctx->codec->picture_params_size > 0) {
1031 ctx->codec_picture_params =
1032 av_mallocz(ctx->codec->picture_params_size);
1033 if (!ctx->codec_picture_params) {
1034 err = AVERROR(ENOMEM);
1039 if (ctx->codec->init_sequence_params) {
1040 err = ctx->codec->init_sequence_params(avctx);
1042 av_log(avctx, AV_LOG_ERROR, "Codec sequence initialisation "
1043 "failed: %d.\n", err);
1048 // All I are IDR for now.
1050 ctx->p_per_i = ((avctx->gop_size + avctx->max_b_frames) /
1051 (avctx->max_b_frames + 1));
1052 ctx->b_per_p = avctx->max_b_frames;
1054 // This should be configurable somehow. (Needs testing on a machine
1055 // where it actually overlaps properly, though.)
1056 ctx->issue_mode = ISSUE_MODE_MAXIMISE_THROUGHPUT;
1061 av_freep(&hwconfig);
1062 av_hwframe_constraints_free(&constraints);
1063 ff_vaapi_encode_close(avctx);
1067 av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
1069 VAAPIEncodeContext *ctx = avctx->priv_data;
1070 VAAPIEncodePicture *pic, *next;
1072 for (pic = ctx->pic_start; pic; pic = next) {
1074 vaapi_encode_free(avctx, pic);
1077 if (ctx->va_context != VA_INVALID_ID)
1078 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
1080 if (ctx->va_config != VA_INVALID_ID)
1081 vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
1083 if (ctx->codec->close)
1084 ctx->codec->close(avctx);
1086 av_freep(&ctx->codec_sequence_params);
1087 av_freep(&ctx->codec_picture_params);
1089 av_buffer_unref(&ctx->recon_frames_ref);
1090 av_buffer_unref(&ctx->input_frames_ref);
1091 av_buffer_unref(&ctx->device_ref);
1093 av_freep(&ctx->priv_data);