Libav
vaapi_encode.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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.
8  *
9  * Libav 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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <inttypes.h>
20 #include <string.h>
21 
22 #include "libavutil/avassert.h"
23 #include "libavutil/common.h"
24 #include "libavutil/log.h"
25 #include "libavutil/pixdesc.h"
26 
27 #include "vaapi_encode.h"
28 #include "avcodec.h"
29 
30 static const char *picture_type_name[] = { "IDR", "I", "P", "B" };
31 
33  VAAPIEncodePicture *pic,
34  int type, char *data, size_t bit_len)
35 {
37  VAStatus vas;
38  VABufferID param_buffer, data_buffer;
39  VAEncPackedHeaderParameterBuffer params = {
40  .type = type,
41  .bit_length = bit_len,
42  .has_emulation_bytes = 1,
43  };
44 
46 
47  vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
48  VAEncPackedHeaderParameterBufferType,
49  sizeof(params), 1, &params, &param_buffer);
50  if (vas != VA_STATUS_SUCCESS) {
51  av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
52  "for packed header (type %d): %d (%s).\n",
53  type, vas, vaErrorStr(vas));
54  return AVERROR(EIO);
55  }
56  pic->param_buffers[pic->nb_param_buffers++] = param_buffer;
57 
58  vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
59  VAEncPackedHeaderDataBufferType,
60  (bit_len + 7) / 8, 1, data, &data_buffer);
61  if (vas != VA_STATUS_SUCCESS) {
62  av_log(avctx, AV_LOG_ERROR, "Failed to create data buffer "
63  "for packed header (type %d): %d (%s).\n",
64  type, vas, vaErrorStr(vas));
65  return AVERROR(EIO);
66  }
67  pic->param_buffers[pic->nb_param_buffers++] = data_buffer;
68 
69  av_log(avctx, AV_LOG_DEBUG, "Packed header buffer (%d) is %#x/%#x "
70  "(%zu bits).\n", type, param_buffer, data_buffer, bit_len);
71  return 0;
72 }
73 
75  VAAPIEncodePicture *pic,
76  int type, char *data, size_t len)
77 {
79  VAStatus vas;
80  VABufferID buffer;
81 
83 
84  vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
85  type, len, 1, data, &buffer);
86  if (vas != VA_STATUS_SUCCESS) {
87  av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
88  "(type %d): %d (%s).\n", type, vas, vaErrorStr(vas));
89  return AVERROR(EIO);
90  }
91  pic->param_buffers[pic->nb_param_buffers++] = buffer;
92 
93  av_log(avctx, AV_LOG_DEBUG, "Param buffer (%d) is %#x.\n",
94  type, buffer);
95  return 0;
96 }
97 
99  VAAPIEncodePicture *pic)
100 {
101  VAAPIEncodeContext *ctx = avctx->priv_data;
102  VAStatus vas;
103 
105 
106  if (pic->encode_complete) {
107  // Already waited for this picture.
108  return 0;
109  }
110 
111  av_log(avctx, AV_LOG_DEBUG, "Sync to pic %"PRId64"/%"PRId64" "
112  "(recon surface %#x).\n", pic->display_order,
113  pic->encode_order, pic->recon_surface);
114 
115  vas = vaSyncSurface(ctx->hwctx->display, pic->recon_surface);
116  if (vas != VA_STATUS_SUCCESS) {
117  av_log(avctx, AV_LOG_ERROR, "Failed to sync to picture completion: "
118  "%d (%s).\n", vas, vaErrorStr(vas));
119  return AVERROR(EIO);
120  }
121 
122  // Input is definitely finished with now.
123  av_frame_free(&pic->input_image);
124 
125  pic->encode_complete = 1;
126  return 0;
127 }
128 
130  VAAPIEncodePicture *pic)
131 {
132  VAAPIEncodeContext *ctx = avctx->priv_data;
133  VAAPIEncodeSlice *slice;
134  VAStatus vas;
135  int err, i;
137  size_t bit_len;
138 
139  av_log(avctx, AV_LOG_DEBUG, "Issuing encode for pic %"PRId64"/%"PRId64" "
140  "as type %s.\n", pic->display_order, pic->encode_order,
141  picture_type_name[pic->type]);
142  if (pic->nb_refs == 0) {
143  av_log(avctx, AV_LOG_DEBUG, "No reference pictures.\n");
144  } else {
145  av_log(avctx, AV_LOG_DEBUG, "Refers to:");
146  for (i = 0; i < pic->nb_refs; i++) {
147  av_log(avctx, AV_LOG_DEBUG, " %"PRId64"/%"PRId64,
148  pic->refs[i]->display_order, pic->refs[i]->encode_order);
149  }
150  av_log(avctx, AV_LOG_DEBUG, ".\n");
151  }
152 
154  for (i = 0; i < pic->nb_refs; i++) {
155  av_assert0(pic->refs[i]);
156  // If we are serialised then the references must have already
157  // completed. If not, they must have been issued but need not
158  // have completed yet.
160  av_assert0(pic->refs[i]->encode_complete);
161  else
162  av_assert0(pic->refs[i]->encode_issued);
163  }
164 
165  av_log(avctx, AV_LOG_DEBUG, "Input surface is %#x.\n", pic->input_surface);
166 
167  pic->recon_image = av_frame_alloc();
168  if (!pic->recon_image) {
169  err = AVERROR(ENOMEM);
170  goto fail;
171  }
172 
174  if (err < 0) {
175  err = AVERROR(ENOMEM);
176  goto fail;
177  }
178  pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3];
179  av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface);
180 
182  if (!pic->output_buffer_ref) {
183  err = AVERROR(ENOMEM);
184  goto fail;
185  }
186  pic->output_buffer = (VABufferID)(uintptr_t)pic->output_buffer_ref->data;
187  av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n",
188  pic->output_buffer);
189 
190  if (ctx->codec->picture_params_size > 0) {
192  if (!pic->codec_picture_params)
193  goto fail;
194  memcpy(pic->codec_picture_params, ctx->codec_picture_params,
195  ctx->codec->picture_params_size);
196  } else {
198  }
199 
200  pic->nb_param_buffers = 0;
201 
202  if (pic->encode_order == 0) {
203  // Global parameter buffers are set on the first picture only.
204 
205  for (i = 0; i < ctx->nb_global_params; i++) {
206  err = vaapi_encode_make_param_buffer(avctx, pic,
207  VAEncMiscParameterBufferType,
208  (char*)ctx->global_params[i],
209  ctx->global_params_size[i]);
210  if (err < 0)
211  goto fail;
212  }
213  }
214 
215  if (pic->type == PICTURE_TYPE_IDR && ctx->codec->init_sequence_params) {
216  err = vaapi_encode_make_param_buffer(avctx, pic,
217  VAEncSequenceParameterBufferType,
220  if (err < 0)
221  goto fail;
222  }
223 
224  if (ctx->codec->init_picture_params) {
225  err = ctx->codec->init_picture_params(avctx, pic);
226  if (err < 0) {
227  av_log(avctx, AV_LOG_ERROR, "Failed to initialise picture "
228  "parameters: %d.\n", err);
229  goto fail;
230  }
231  err = vaapi_encode_make_param_buffer(avctx, pic,
232  VAEncPictureParameterBufferType,
234  ctx->codec->picture_params_size);
235  if (err < 0)
236  goto fail;
237  }
238 
239  if (pic->type == PICTURE_TYPE_IDR) {
240  if (ctx->codec->write_sequence_header) {
241  bit_len = 8 * sizeof(data);
242  err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
243  if (err < 0) {
244  av_log(avctx, AV_LOG_ERROR, "Failed to write per-sequence "
245  "header: %d.\n", err);
246  goto fail;
247  }
248  err = vaapi_encode_make_packed_header(avctx, pic,
250  data, bit_len);
251  if (err < 0)
252  goto fail;
253  }
254  }
255 
256  if (ctx->codec->write_picture_header) {
257  bit_len = 8 * sizeof(data);
258  err = ctx->codec->write_picture_header(avctx, pic, data, &bit_len);
259  if (err < 0) {
260  av_log(avctx, AV_LOG_ERROR, "Failed to write per-picture "
261  "header: %d.\n", err);
262  goto fail;
263  }
264  err = vaapi_encode_make_packed_header(avctx, pic,
266  data, bit_len);
267  if (err < 0)
268  goto fail;
269  }
270 
271  if (ctx->codec->write_extra_buffer) {
272  for (i = 0;; i++) {
273  size_t len = sizeof(data);
274  int type;
275  err = ctx->codec->write_extra_buffer(avctx, pic, i, &type,
276  data, &len);
277  if (err == AVERROR_EOF)
278  break;
279  if (err < 0) {
280  av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
281  "buffer %d: %d.\n", i, err);
282  goto fail;
283  }
284 
285  err = vaapi_encode_make_param_buffer(avctx, pic, type,
286  data, len);
287  if (err < 0)
288  goto fail;
289  }
290  }
291 
292  if (ctx->codec->write_extra_header) {
293  for (i = 0;; i++) {
294  int type;
295  bit_len = 8 * sizeof(data);
296  err = ctx->codec->write_extra_header(avctx, pic, i, &type,
297  data, &bit_len);
298  if (err == AVERROR_EOF)
299  break;
300  if (err < 0) {
301  av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
302  "header %d: %d.\n", i, err);
303  goto fail;
304  }
305 
306  err = vaapi_encode_make_packed_header(avctx, pic, type,
307  data, bit_len);
308  if (err < 0)
309  goto fail;
310  }
311  }
312 
314  for (i = 0; i < pic->nb_slices; i++) {
315  slice = av_mallocz(sizeof(*slice));
316  if (!slice) {
317  err = AVERROR(ENOMEM);
318  goto fail;
319  }
320  pic->slices[i] = slice;
321 
322  if (ctx->codec->slice_params_size > 0) {
324  if (!slice->codec_slice_params) {
325  err = AVERROR(ENOMEM);
326  goto fail;
327  }
328  }
329 
330  if (ctx->codec->init_slice_params) {
331  err = ctx->codec->init_slice_params(avctx, pic, slice);
332  if (err < 0) {
333  av_log(avctx, AV_LOG_ERROR, "Failed to initalise slice "
334  "parameters: %d.\n", err);
335  goto fail;
336  }
337  }
338 
339  if (ctx->codec->write_slice_header) {
340  bit_len = 8 * sizeof(data);
341  err = ctx->codec->write_slice_header(avctx, pic, slice,
342  data, &bit_len);
343  if (err < 0) {
344  av_log(avctx, AV_LOG_ERROR, "Failed to write per-slice "
345  "header: %d.\n", err);
346  goto fail;
347  }
348  err = vaapi_encode_make_packed_header(avctx, pic,
349  ctx->codec->slice_header_type,
350  data, bit_len);
351  if (err < 0)
352  goto fail;
353  }
354 
355  if (ctx->codec->init_slice_params) {
356  err = vaapi_encode_make_param_buffer(avctx, pic,
357  VAEncSliceParameterBufferType,
358  slice->codec_slice_params,
359  ctx->codec->slice_params_size);
360  if (err < 0)
361  goto fail;
362  }
363  }
364 
365  vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
366  pic->input_surface);
367  if (vas != VA_STATUS_SUCCESS) {
368  av_log(avctx, AV_LOG_ERROR, "Failed to begin picture encode issue: "
369  "%d (%s).\n", vas, vaErrorStr(vas));
370  err = AVERROR(EIO);
371  goto fail_with_picture;
372  }
373 
374  vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
375  pic->param_buffers, pic->nb_param_buffers);
376  if (vas != VA_STATUS_SUCCESS) {
377  av_log(avctx, AV_LOG_ERROR, "Failed to upload encode parameters: "
378  "%d (%s).\n", vas, vaErrorStr(vas));
379  err = AVERROR(EIO);
380  goto fail_with_picture;
381  }
382 
383  vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
384  if (vas != VA_STATUS_SUCCESS) {
385  av_log(avctx, AV_LOG_ERROR, "Failed to end picture encode issue: "
386  "%d (%s).\n", vas, vaErrorStr(vas));
387  err = AVERROR(EIO);
388  // vaRenderPicture() has been called here, so we should not destroy
389  // the parameter buffers unless separate destruction is required.
390  if (ctx->hwctx->driver_quirks &
392  goto fail;
393  else
394  goto fail_at_end;
395  }
396 
397  if (ctx->hwctx->driver_quirks &
399  for (i = 0; i < pic->nb_param_buffers; i++) {
400  vas = vaDestroyBuffer(ctx->hwctx->display,
401  pic->param_buffers[i]);
402  if (vas != VA_STATUS_SUCCESS) {
403  av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
404  "param buffer %#x: %d (%s).\n",
405  pic->param_buffers[i], vas, vaErrorStr(vas));
406  // And ignore.
407  }
408  }
409  }
410 
411  pic->encode_issued = 1;
412 
414  return vaapi_encode_wait(avctx, pic);
415  else
416  return 0;
417 
418 fail_with_picture:
419  vaEndPicture(ctx->hwctx->display, ctx->va_context);
420 fail:
421  for(i = 0; i < pic->nb_param_buffers; i++)
422  vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
423 fail_at_end:
425  av_frame_free(&pic->recon_image);
426  return err;
427 }
428 
430  VAAPIEncodePicture *pic, AVPacket *pkt)
431 {
432  VAAPIEncodeContext *ctx = avctx->priv_data;
433  VACodedBufferSegment *buf_list, *buf;
434  VAStatus vas;
435  int err;
436 
437  err = vaapi_encode_wait(avctx, pic);
438  if (err < 0)
439  return err;
440 
441  buf_list = NULL;
442  vas = vaMapBuffer(ctx->hwctx->display, pic->output_buffer,
443  (void**)&buf_list);
444  if (vas != VA_STATUS_SUCCESS) {
445  av_log(avctx, AV_LOG_ERROR, "Failed to map output buffers: "
446  "%d (%s).\n", vas, vaErrorStr(vas));
447  err = AVERROR(EIO);
448  goto fail;
449  }
450 
451  for (buf = buf_list; buf; buf = buf->next) {
452  av_log(avctx, AV_LOG_DEBUG, "Output buffer: %u bytes "
453  "(status %08x).\n", buf->size, buf->status);
454 
455  err = av_new_packet(pkt, buf->size);
456  if (err < 0)
457  goto fail_mapped;
458 
459  memcpy(pkt->data, buf->buf, buf->size);
460  }
461 
462  if (pic->type == PICTURE_TYPE_IDR)
463  pkt->flags |= AV_PKT_FLAG_KEY;
464 
465  pkt->pts = pic->pts;
466 
467  vas = vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
468  if (vas != VA_STATUS_SUCCESS) {
469  av_log(avctx, AV_LOG_ERROR, "Failed to unmap output buffers: "
470  "%d (%s).\n", vas, vaErrorStr(vas));
471  err = AVERROR(EIO);
472  goto fail;
473  }
474 
476  pic->output_buffer = VA_INVALID_ID;
477 
478  av_log(avctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n",
479  pic->display_order, pic->encode_order);
480  return 0;
481 
482 fail_mapped:
483  vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
484 fail:
486  pic->output_buffer = VA_INVALID_ID;
487  return err;
488 }
489 
491  VAAPIEncodePicture *pic)
492 {
493  vaapi_encode_wait(avctx, pic);
494 
495  if (pic->output_buffer_ref) {
496  av_log(avctx, AV_LOG_DEBUG, "Discard output for pic "
497  "%"PRId64"/%"PRId64".\n",
498  pic->display_order, pic->encode_order);
499 
501  pic->output_buffer = VA_INVALID_ID;
502  }
503 
504  return 0;
505 }
506 
508 {
509  VAAPIEncodePicture *pic;
510 
511  pic = av_mallocz(sizeof(*pic));
512  if (!pic)
513  return NULL;
514 
515  pic->input_surface = VA_INVALID_ID;
516  pic->recon_surface = VA_INVALID_ID;
517  pic->output_buffer = VA_INVALID_ID;
518 
519  return pic;
520 }
521 
523  VAAPIEncodePicture *pic)
524 {
525  int i;
526 
527  if (pic->encode_issued)
528  vaapi_encode_discard(avctx, pic);
529 
530  for (i = 0; i < pic->nb_slices; i++) {
531  av_freep(&pic->slices[i]->priv_data);
533  av_freep(&pic->slices[i]);
534  }
536 
537  av_frame_free(&pic->input_image);
538  av_frame_free(&pic->recon_image);
539 
540  // Output buffer should already be destroyed.
541  av_assert0(pic->output_buffer == VA_INVALID_ID);
542 
543  av_freep(&pic->priv_data);
545 
546  av_free(pic);
547 
548  return 0;
549 }
550 
552  VAAPIEncodePicture *target)
553 {
554  VAAPIEncodeContext *ctx = avctx->priv_data;
555  VAAPIEncodePicture *pic;
556  int i, err;
557 
560  // These two modes are equivalent, except that we wait for
561  // immediate completion on each operation if serialised.
562 
563  if (!target) {
564  // No target, nothing to do yet.
565  return 0;
566  }
567 
568  if (target->encode_complete) {
569  // Already done.
570  return 0;
571  }
572 
573  pic = target;
574  for (i = 0; i < pic->nb_refs; i++) {
575  if (!pic->refs[i]->encode_complete) {
576  err = vaapi_encode_step(avctx, pic->refs[i]);
577  if (err < 0)
578  return err;
579  }
580  }
581 
582  err = vaapi_encode_issue(avctx, pic);
583  if (err < 0)
584  return err;
585 
586  } else if (ctx->issue_mode == ISSUE_MODE_MAXIMISE_THROUGHPUT) {
587  int activity;
588 
589  do {
590  activity = 0;
591  for (pic = ctx->pic_start; pic; pic = pic->next) {
592  if (!pic->input_available || pic->encode_issued)
593  continue;
594  for (i = 0; i < pic->nb_refs; i++) {
595  if (!pic->refs[i]->encode_issued)
596  break;
597  }
598  if (i < pic->nb_refs)
599  continue;
600  err = vaapi_encode_issue(avctx, pic);
601  if (err < 0)
602  return err;
603  activity = 1;
604  }
605  } while(activity);
606 
607  if (target) {
608  av_assert0(target->encode_issued && "broken dependencies?");
609  }
610 
611  } else {
612  av_assert0(0);
613  }
614 
615  return 0;
616 }
617 
619  VAAPIEncodePicture **pic_out)
620 {
621  VAAPIEncodeContext *ctx = avctx->priv_data;
622  VAAPIEncodePicture *start, *end, *pic;
623  int i;
624 
625  for (pic = ctx->pic_start; pic; pic = pic->next) {
626  if (pic->next)
627  av_assert0(pic->display_order + 1 == pic->next->display_order);
628  if (pic->display_order == ctx->input_order) {
629  *pic_out = pic;
630  return 0;
631  }
632  }
633 
634  if (ctx->input_order == 0) {
635  // First frame is always an IDR frame.
636  av_assert0(!ctx->pic_start && !ctx->pic_end);
637 
638  pic = vaapi_encode_alloc();
639  if (!pic)
640  return AVERROR(ENOMEM);
641 
642  pic->type = PICTURE_TYPE_IDR;
643  pic->display_order = 0;
644  pic->encode_order = 0;
645 
646  ctx->pic_start = ctx->pic_end = pic;
647 
648  *pic_out = pic;
649  return 0;
650  }
651 
652  pic = vaapi_encode_alloc();
653  if (!pic)
654  return AVERROR(ENOMEM);
655 
656  if (ctx->p_per_i == 0 || ctx->p_counter == ctx->p_per_i) {
657  if (ctx->i_per_idr == 0 || ctx->i_counter == ctx->i_per_idr) {
658  pic->type = PICTURE_TYPE_IDR;
659  ctx->i_counter = 0;
660  } else {
661  pic->type = PICTURE_TYPE_I;
662  ++ctx->i_counter;
663  }
664  ctx->p_counter = 0;
665  } else {
666  pic->type = PICTURE_TYPE_P;
667  pic->refs[0] = ctx->pic_end;
668  pic->nb_refs = 1;
669  ++ctx->p_counter;
670  }
671  start = end = pic;
672 
673  if (pic->type != PICTURE_TYPE_IDR) {
674  // If that was not an IDR frame, add B-frames display-before and
675  // encode-after it.
676 
677  for (i = 0; i < ctx->b_per_p; i++) {
678  pic = vaapi_encode_alloc();
679  if (!pic)
680  goto fail;
681 
682  pic->type = PICTURE_TYPE_B;
683  pic->refs[0] = ctx->pic_end;
684  pic->refs[1] = end;
685  pic->nb_refs = 2;
686 
687  pic->next = start;
688  pic->display_order = ctx->input_order + ctx->b_per_p - i - 1;
689  pic->encode_order = pic->display_order + 1;
690  start = pic;
691  }
692  }
693 
694  for (i = 0, pic = start; pic; i++, pic = pic->next) {
695  pic->display_order = ctx->input_order + i;
696  if (end->type == PICTURE_TYPE_IDR)
697  pic->encode_order = ctx->input_order + i;
698  else if (pic == end)
699  pic->encode_order = ctx->input_order;
700  else
701  pic->encode_order = ctx->input_order + i + 1;
702  }
703 
704  av_assert0(ctx->pic_end);
705  ctx->pic_end->next = start;
706  ctx->pic_end = end;
707 
708  *pic_out = start;
709 
710  av_log(avctx, AV_LOG_DEBUG, "Pictures:");
711  for (pic = ctx->pic_start; pic; pic = pic->next) {
712  av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
713  picture_type_name[pic->type],
714  pic->display_order, pic->encode_order);
715  }
716  av_log(avctx, AV_LOG_DEBUG, "\n");
717 
718  return 0;
719 
720 fail:
721  while (start) {
722  pic = start->next;
723  vaapi_encode_free(avctx, start);
724  start = pic;
725  }
726  return AVERROR(ENOMEM);
727 }
728 
730 {
731  VAAPIEncodeContext *ctx = avctx->priv_data;
732  VAAPIEncodePicture *pic, *last_pic, *next;
733 
734  // Find the last picture we actually have input for.
735  for (pic = ctx->pic_start; pic; pic = pic->next) {
736  if (!pic->input_available)
737  break;
738  last_pic = pic;
739  }
740 
741  if (pic) {
742  av_assert0(last_pic);
743 
744  if (last_pic->type == PICTURE_TYPE_B) {
745  // Some fixing up is required. Change the type of this
746  // picture to P, then modify preceding B references which
747  // point beyond it to point at it instead.
748 
749  last_pic->type = PICTURE_TYPE_P;
750  last_pic->encode_order = last_pic->refs[1]->encode_order;
751 
752  for (pic = ctx->pic_start; pic != last_pic; pic = pic->next) {
753  if (pic->type == PICTURE_TYPE_B &&
754  pic->refs[1] == last_pic->refs[1])
755  pic->refs[1] = last_pic;
756  }
757 
758  last_pic->nb_refs = 1;
759  last_pic->refs[1] = NULL;
760  } else {
761  // We can use the current structure (no references point
762  // beyond the end), but there are unused pics to discard.
763  }
764 
765  // Discard all following pics, they will never be used.
766  for (pic = last_pic->next; pic; pic = next) {
767  next = pic->next;
768  vaapi_encode_free(avctx, pic);
769  }
770 
771  last_pic->next = NULL;
772  ctx->pic_end = last_pic;
773 
774  } else {
775  // Input is available for all pictures, so we don't need to
776  // mangle anything.
777  }
778 
779  av_log(avctx, AV_LOG_DEBUG, "Pictures at end of stream:");
780  for (pic = ctx->pic_start; pic; pic = pic->next) {
781  av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
782  picture_type_name[pic->type],
783  pic->display_order, pic->encode_order);
784  }
785  av_log(avctx, AV_LOG_DEBUG, "\n");
786 
787  return 0;
788 }
789 
791 {
792  VAAPIEncodeContext *ctx = avctx->priv_data;
793  VAAPIEncodePicture *pic, *old;
794  int i;
795 
796  while (ctx->pic_start != ctx->pic_end) {
797  old = ctx->pic_start;
798  if (old->encode_order > ctx->output_order)
799  break;
800 
801  for (pic = old->next; pic; pic = pic->next) {
802  if (pic->encode_complete)
803  continue;
804  for (i = 0; i < pic->nb_refs; i++) {
805  if (pic->refs[i] == old) {
806  // We still need this picture because it's referred to
807  // directly by a later one, so it and all following
808  // pictures have to stay.
809  return 0;
810  }
811  }
812  }
813 
814  pic = ctx->pic_start;
815  ctx->pic_start = pic->next;
816  vaapi_encode_free(avctx, pic);
817  }
818 
819  return 0;
820 }
821 
823  const AVFrame *input_image, int *got_packet)
824 {
825  VAAPIEncodeContext *ctx = avctx->priv_data;
826  VAAPIEncodePicture *pic;
827  int err;
828 
829  if (input_image) {
830  av_log(avctx, AV_LOG_DEBUG, "Encode frame: %ux%u (%"PRId64").\n",
831  input_image->width, input_image->height, input_image->pts);
832 
833  err = vaapi_encode_get_next(avctx, &pic);
834  if (err) {
835  av_log(avctx, AV_LOG_ERROR, "Input setup failed: %d.\n", err);
836  return err;
837  }
838 
839  pic->input_image = av_frame_alloc();
840  if (!pic->input_image) {
841  err = AVERROR(ENOMEM);
842  goto fail;
843  }
844  err = av_frame_ref(pic->input_image, input_image);
845  if (err < 0)
846  goto fail;
847  pic->input_surface = (VASurfaceID)(uintptr_t)input_image->data[3];
848  pic->pts = input_image->pts;
849 
850  if (ctx->input_order == 0)
851  ctx->first_pts = pic->pts;
852  if (ctx->input_order == ctx->decode_delay)
853  ctx->dts_pts_diff = pic->pts - ctx->first_pts;
854  if (ctx->output_delay > 0)
855  ctx->ts_ring[ctx->input_order % (3 * ctx->output_delay)] = pic->pts;
856 
857  pic->input_available = 1;
858 
859  } else {
860  if (!ctx->end_of_stream) {
861  err = vaapi_encode_mangle_end(avctx);
862  if (err < 0)
863  goto fail;
864  ctx->end_of_stream = 1;
865  }
866  }
867 
868  ++ctx->input_order;
869  ++ctx->output_order;
870  av_assert0(ctx->output_order + ctx->output_delay + 1 == ctx->input_order);
871 
872  for (pic = ctx->pic_start; pic; pic = pic->next)
873  if (pic->encode_order == ctx->output_order)
874  break;
875 
876  // pic can be null here if we don't have a specific target in this
877  // iteration. We might still issue encodes if things can be overlapped,
878  // even though we don't intend to output anything.
879 
880  err = vaapi_encode_step(avctx, pic);
881  if (err < 0) {
882  av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err);
883  goto fail;
884  }
885 
886  if (!pic) {
887  *got_packet = 0;
888  } else {
889  err = vaapi_encode_output(avctx, pic, pkt);
890  if (err < 0) {
891  av_log(avctx, AV_LOG_ERROR, "Output failed: %d.\n", err);
892  goto fail;
893  }
894 
895  if (ctx->output_delay == 0) {
896  pkt->dts = pkt->pts;
897  } else if (ctx->output_order < ctx->decode_delay) {
898  if (ctx->ts_ring[ctx->output_order] < INT64_MIN + ctx->dts_pts_diff)
899  pkt->dts = INT64_MIN;
900  else
901  pkt->dts = ctx->ts_ring[ctx->output_order] - ctx->dts_pts_diff;
902  } else {
903  pkt->dts = ctx->ts_ring[(ctx->output_order - ctx->decode_delay) %
904  (3 * ctx->output_delay)];
905  }
906 
907  *got_packet = 1;
908  }
909 
910  err = vaapi_encode_clear_old(avctx);
911  if (err < 0) {
912  av_log(avctx, AV_LOG_ERROR, "List clearing failed: %d.\n", err);
913  goto fail;
914  }
915 
916  return 0;
917 
918 fail:
919  // Unclear what to clean up on failure. There are probably some things we
920  // could do usefully clean up here, but for now just leave them for uninit()
921  // to do instead.
922  return err;
923 }
924 
926 {
927  VAAPIEncodeContext *ctx = avctx->priv_data;
928  VAStatus vas;
929  int i, n, err;
930  VAProfile *profiles = NULL;
931  VAEntrypoint *entrypoints = NULL;
932  VAConfigAttrib attr[] = {
933  { VAConfigAttribRateControl },
934  { VAConfigAttribEncMaxRefFrames },
935  };
936 
937  n = vaMaxNumProfiles(ctx->hwctx->display);
938  profiles = av_malloc_array(n, sizeof(VAProfile));
939  if (!profiles) {
940  err = AVERROR(ENOMEM);
941  goto fail;
942  }
943  vas = vaQueryConfigProfiles(ctx->hwctx->display, profiles, &n);
944  if (vas != VA_STATUS_SUCCESS) {
945  av_log(ctx, AV_LOG_ERROR, "Failed to query profiles: %d (%s).\n",
946  vas, vaErrorStr(vas));
947  err = AVERROR(ENOSYS);
948  goto fail;
949  }
950  for (i = 0; i < n; i++) {
951  if (profiles[i] == ctx->va_profile)
952  break;
953  }
954  if (i >= n) {
955  av_log(ctx, AV_LOG_ERROR, "Encoding profile not found (%d).\n",
956  ctx->va_profile);
957  err = AVERROR(ENOSYS);
958  goto fail;
959  }
960 
961  n = vaMaxNumEntrypoints(ctx->hwctx->display);
962  entrypoints = av_malloc_array(n, sizeof(VAEntrypoint));
963  if (!entrypoints) {
964  err = AVERROR(ENOMEM);
965  goto fail;
966  }
967  vas = vaQueryConfigEntrypoints(ctx->hwctx->display, ctx->va_profile,
968  entrypoints, &n);
969  if (vas != VA_STATUS_SUCCESS) {
970  av_log(ctx, AV_LOG_ERROR, "Failed to query entrypoints for "
971  "profile %u: %d (%s).\n", ctx->va_profile,
972  vas, vaErrorStr(vas));
973  err = AVERROR(ENOSYS);
974  goto fail;
975  }
976  for (i = 0; i < n; i++) {
977  if (entrypoints[i] == ctx->va_entrypoint)
978  break;
979  }
980  if (i >= n) {
981  av_log(ctx, AV_LOG_ERROR, "Encoding entrypoint not found "
982  "(%d / %d).\n", ctx->va_profile, ctx->va_entrypoint);
983  err = AVERROR(ENOSYS);
984  goto fail;
985  }
986 
987  vas = vaGetConfigAttributes(ctx->hwctx->display,
988  ctx->va_profile, ctx->va_entrypoint,
989  attr, FF_ARRAY_ELEMS(attr));
990  if (vas != VA_STATUS_SUCCESS) {
991  av_log(avctx, AV_LOG_ERROR, "Failed to fetch config "
992  "attributes: %d (%s).\n", vas, vaErrorStr(vas));
993  return AVERROR(EINVAL);
994  }
995 
996  for (i = 0; i < FF_ARRAY_ELEMS(attr); i++) {
997  if (attr[i].value == VA_ATTRIB_NOT_SUPPORTED) {
998  // Unfortunately we have to treat this as "don't know" and hope
999  // for the best, because the Intel MJPEG encoder returns this
1000  // for all the interesting attributes.
1001  continue;
1002  }
1003  switch (attr[i].type) {
1004  case VAConfigAttribRateControl:
1005  if (!(ctx->va_rc_mode & attr[i].value)) {
1006  av_log(avctx, AV_LOG_ERROR, "Rate control mode is not "
1007  "supported: %x\n", attr[i].value);
1008  err = AVERROR(EINVAL);
1009  goto fail;
1010  }
1011  break;
1012  case VAConfigAttribEncMaxRefFrames:
1013  {
1014  unsigned int ref_l0 = attr[i].value & 0xffff;
1015  unsigned int ref_l1 = (attr[i].value >> 16) & 0xffff;
1016 
1017  if (avctx->gop_size > 1 && ref_l0 < 1) {
1018  av_log(avctx, AV_LOG_ERROR, "P frames are not "
1019  "supported (%x).\n", attr[i].value);
1020  err = AVERROR(EINVAL);
1021  goto fail;
1022  }
1023  if (avctx->max_b_frames > 0 && ref_l1 < 1) {
1024  av_log(avctx, AV_LOG_ERROR, "B frames are not "
1025  "supported (%x).\n", attr[i].value);
1026  err = AVERROR(EINVAL);
1027  goto fail;
1028  }
1029  }
1030  break;
1031  }
1032  }
1033 
1034  err = 0;
1035 fail:
1036  av_freep(&profiles);
1037  av_freep(&entrypoints);
1038  return err;
1039 }
1040 
1041 static void vaapi_encode_free_output_buffer(void *opaque,
1042  uint8_t *data)
1043 {
1044  AVCodecContext *avctx = opaque;
1045  VAAPIEncodeContext *ctx = avctx->priv_data;
1046  VABufferID buffer_id;
1047 
1048  buffer_id = (VABufferID)(uintptr_t)data;
1049 
1050  vaDestroyBuffer(ctx->hwctx->display, buffer_id);
1051 
1052  av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id);
1053 }
1054 
1056  int size)
1057 {
1058  AVCodecContext *avctx = opaque;
1059  VAAPIEncodeContext *ctx = avctx->priv_data;
1060  VABufferID buffer_id;
1061  VAStatus vas;
1062  AVBufferRef *ref;
1063 
1064  // The output buffer size is fixed, so it needs to be large enough
1065  // to hold the largest possible compressed frame. We assume here
1066  // that the uncompressed frame plus some header data is an upper
1067  // bound on that.
1068  vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
1069  VAEncCodedBufferType,
1070  3 * ctx->aligned_width * ctx->aligned_height +
1071  (1 << 16), 1, 0, &buffer_id);
1072  if (vas != VA_STATUS_SUCCESS) {
1073  av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream "
1074  "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
1075  return NULL;
1076  }
1077 
1078  av_log(avctx, AV_LOG_DEBUG, "Allocated output buffer %#x\n", buffer_id);
1079 
1080  ref = av_buffer_create((uint8_t*)(uintptr_t)buffer_id,
1081  sizeof(buffer_id),
1083  avctx, AV_BUFFER_FLAG_READONLY);
1084  if (!ref) {
1085  vaDestroyBuffer(ctx->hwctx->display, buffer_id);
1086  return NULL;
1087  }
1088 
1089  return ref;
1090 }
1091 
1093  const VAAPIEncodeType *type)
1094 {
1095  VAAPIEncodeContext *ctx = avctx->priv_data;
1096  AVVAAPIFramesContext *recon_hwctx = NULL;
1097  AVVAAPIHWConfig *hwconfig = NULL;
1098  AVHWFramesConstraints *constraints = NULL;
1099  enum AVPixelFormat recon_format;
1100  VAStatus vas;
1101  int err, i;
1102 
1103  if (!avctx->hw_frames_ctx) {
1104  av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
1105  "required to associate the encoding device.\n");
1106  return AVERROR(EINVAL);
1107  }
1108 
1109  ctx->codec = type;
1110  ctx->codec_options = ctx->codec_options_data;
1111 
1112  ctx->va_config = VA_INVALID_ID;
1113  ctx->va_context = VA_INVALID_ID;
1114 
1115  ctx->priv_data = av_mallocz(type->priv_data_size);
1116  if (!ctx->priv_data) {
1117  err = AVERROR(ENOMEM);
1118  goto fail;
1119  }
1120 
1122  if (!ctx->input_frames_ref) {
1123  err = AVERROR(ENOMEM);
1124  goto fail;
1125  }
1127 
1129  if (!ctx->device_ref) {
1130  err = AVERROR(ENOMEM);
1131  goto fail;
1132  }
1133  ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
1134  ctx->hwctx = ctx->device->hwctx;
1135 
1136  err = ctx->codec->init(avctx);
1137  if (err < 0)
1138  goto fail;
1139 
1140  err = vaapi_encode_check_config(avctx);
1141  if (err < 0)
1142  goto fail;
1143 
1144  vas = vaCreateConfig(ctx->hwctx->display,
1145  ctx->va_profile, ctx->va_entrypoint,
1147  &ctx->va_config);
1148  if (vas != VA_STATUS_SUCCESS) {
1149  av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
1150  "configuration: %d (%s).\n", vas, vaErrorStr(vas));
1151  err = AVERROR(EIO);
1152  goto fail;
1153  }
1154 
1155  hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
1156  if (!hwconfig) {
1157  err = AVERROR(ENOMEM);
1158  goto fail;
1159  }
1160  hwconfig->config_id = ctx->va_config;
1161 
1163  hwconfig);
1164  if (!constraints) {
1165  err = AVERROR(ENOMEM);
1166  goto fail;
1167  }
1168 
1169  // Probably we can use the input surface format as the surface format
1170  // of the reconstructed frames. If not, we just pick the first (only?)
1171  // format in the valid list and hope that it all works.
1172  recon_format = AV_PIX_FMT_NONE;
1173  if (constraints->valid_sw_formats) {
1174  for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
1175  if (ctx->input_frames->sw_format ==
1176  constraints->valid_sw_formats[i]) {
1177  recon_format = ctx->input_frames->sw_format;
1178  break;
1179  }
1180  }
1181  if (recon_format == AV_PIX_FMT_NONE) {
1182  // No match. Just use the first in the supported list and
1183  // hope for the best.
1184  recon_format = constraints->valid_sw_formats[0];
1185  }
1186  } else {
1187  // No idea what to use; copy input format.
1188  recon_format = ctx->input_frames->sw_format;
1189  }
1190  av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
1191  "reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
1192 
1193  if (ctx->aligned_width < constraints->min_width ||
1194  ctx->aligned_height < constraints->min_height ||
1195  ctx->aligned_width > constraints->max_width ||
1196  ctx->aligned_height > constraints->max_height) {
1197  av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at "
1198  "size %dx%d (constraints: width %d-%d height %d-%d).\n",
1199  ctx->aligned_width, ctx->aligned_height,
1200  constraints->min_width, constraints->max_width,
1201  constraints->min_height, constraints->max_height);
1202  err = AVERROR(EINVAL);
1203  goto fail;
1204  }
1205 
1206  av_freep(&hwconfig);
1207  av_hwframe_constraints_free(&constraints);
1208 
1210  if (!ctx->recon_frames_ref) {
1211  err = AVERROR(ENOMEM);
1212  goto fail;
1213  }
1215 
1217  ctx->recon_frames->sw_format = recon_format;
1218  ctx->recon_frames->width = ctx->aligned_width;
1219  ctx->recon_frames->height = ctx->aligned_height;
1221 
1223  if (err < 0) {
1224  av_log(avctx, AV_LOG_ERROR, "Failed to initialise reconstructed "
1225  "frame context: %d.\n", err);
1226  goto fail;
1227  }
1228  recon_hwctx = ctx->recon_frames->hwctx;
1229 
1230  vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
1231  ctx->aligned_width, ctx->aligned_height,
1232  VA_PROGRESSIVE,
1233  recon_hwctx->surface_ids,
1234  recon_hwctx->nb_surfaces,
1235  &ctx->va_context);
1236  if (vas != VA_STATUS_SUCCESS) {
1237  av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
1238  "context: %d (%s).\n", vas, vaErrorStr(vas));
1239  err = AVERROR(EIO);
1240  goto fail;
1241  }
1242 
1243  ctx->input_order = 0;
1244  ctx->output_delay = avctx->max_b_frames;
1245  ctx->decode_delay = 1;
1246  ctx->output_order = - ctx->output_delay - 1;
1247 
1248  if (ctx->codec->sequence_params_size > 0) {
1249  ctx->codec_sequence_params =
1251  if (!ctx->codec_sequence_params) {
1252  err = AVERROR(ENOMEM);
1253  goto fail;
1254  }
1255  }
1256  if (ctx->codec->picture_params_size > 0) {
1257  ctx->codec_picture_params =
1259  if (!ctx->codec_picture_params) {
1260  err = AVERROR(ENOMEM);
1261  goto fail;
1262  }
1263  }
1264 
1265  if (ctx->codec->init_sequence_params) {
1266  err = ctx->codec->init_sequence_params(avctx);
1267  if (err < 0) {
1268  av_log(avctx, AV_LOG_ERROR, "Codec sequence initialisation "
1269  "failed: %d.\n", err);
1270  goto fail;
1271  }
1272  }
1273 
1274  ctx->output_buffer_pool =
1275  av_buffer_pool_init2(sizeof(VABufferID), avctx,
1277  if (!ctx->output_buffer_pool) {
1278  err = AVERROR(ENOMEM);
1279  goto fail;
1280  }
1281 
1282  // All I are IDR for now.
1283  ctx->i_per_idr = 0;
1284  ctx->p_per_i = ((avctx->gop_size + avctx->max_b_frames) /
1285  (avctx->max_b_frames + 1));
1286  ctx->b_per_p = avctx->max_b_frames;
1287 
1288  // This should be configurable somehow. (Needs testing on a machine
1289  // where it actually overlaps properly, though.)
1291 
1292  return 0;
1293 
1294 fail:
1295  av_freep(&hwconfig);
1296  av_hwframe_constraints_free(&constraints);
1297  ff_vaapi_encode_close(avctx);
1298  return err;
1299 }
1300 
1302 {
1303  VAAPIEncodeContext *ctx = avctx->priv_data;
1304  VAAPIEncodePicture *pic, *next;
1305 
1306  for (pic = ctx->pic_start; pic; pic = next) {
1307  next = pic->next;
1308  vaapi_encode_free(avctx, pic);
1309  }
1310 
1311  if (ctx->va_context != VA_INVALID_ID) {
1312  vaDestroyContext(ctx->hwctx->display, ctx->va_context);
1313  ctx->va_context = VA_INVALID_ID;
1314  }
1315 
1316  if (ctx->va_config != VA_INVALID_ID) {
1317  vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
1318  ctx->va_config = VA_INVALID_ID;
1319  }
1320 
1321  if (ctx->codec->close)
1322  ctx->codec->close(avctx);
1323 
1325 
1328 
1331  av_buffer_unref(&ctx->device_ref);
1332 
1333  av_freep(&ctx->priv_data);
1334 
1335  return 0;
1336 }
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:54
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
VASurfaceID input_surface
Definition: vaapi_encode.h:78
VAProfile va_profile
Definition: vaapi_encode.h:108
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:106
int size
VAAPI-specific data associated with a frame pool.
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
VAAPIEncodeSlice * slices[MAX_PICTURE_SLICES]
Definition: vaapi_encode.h:96
VAEntrypoint va_entrypoint
Definition: vaapi_encode.h:109
static const char * picture_type_name[]
Definition: vaapi_encode.c:30
static int vaapi_encode_free(AVCodecContext *avctx, VAAPIEncodePicture *pic)
Definition: vaapi_encode.c:522
char codec_options_data[0]
Definition: vaapi_encode.h:181
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:1679
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
size_t priv_data_size
Definition: vaapi_encode.h:186
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:222
void * av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
Allocate a HW-specific configuration structure for a given HW device.
Definition: hwcontext.c:418
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:202
int(* close)(AVCodecContext *avctx)
Definition: vaapi_encode.h:189
static void vaapi_encode_free_output_buffer(void *opaque, uint8_t *data)
#define FF_ARRAY_ELEMS(a)
void * codec_sequence_params
Definition: vaapi_encode.h:143
AVBufferRef * input_frames_ref
Definition: vaapi_encode.h:119
size_t picture_params_size
Definition: vaapi_encode.h:192
AVHWDeviceContext * device
Definition: vaapi_encode.h:116
int(* init_picture_params)(AVCodecContext *avctx, VAAPIEncodePicture *pic)
Definition: vaapi_encode.h:196
int max_width
The maximum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:399
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
Free an AVHWFrameConstraints structure.
Definition: hwcontext.c:454
static int vaapi_encode_mangle_end(AVCodecContext *avctx)
Definition: vaapi_encode.c:729
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static char buffer[20]
Definition: seek.c:32
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
int(* write_sequence_header)(AVCodecContext *avctx, char *data, size_t *data_len)
Definition: vaapi_encode.h:206
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:199
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
AVBufferRef * output_buffer_ref
Definition: vaapi_encode.h:86
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx, const VAAPIEncodeType *type)
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:85
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
VAContextID va_context
Definition: vaapi_encode.h:111
#define AVERROR_EOF
End of file.
Definition: error.h:51
VASurfaceID recon_surface
Definition: vaapi_encode.h:81
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:113
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES]
Definition: vaapi_encode.h:135
AVHWFramesContext * input_frames
Definition: vaapi_encode.h:120
int width
width and height of the video frame
Definition: frame.h:179
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
VAAPI hardware pipeline configuration details.
#define AVERROR(e)
Definition: error.h:43
static int vaapi_encode_step(AVCodecContext *avctx, VAAPIEncodePicture *target)
Definition: vaapi_encode.c:551
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *input_image, int *got_packet)
Definition: vaapi_encode.c:822
int(* init_sequence_params)(AVCodecContext *avctx)
Definition: vaapi_encode.h:195
int(* write_picture_header)(AVCodecContext *avctx, VAAPIEncodePicture *pic, char *data, size_t *data_len)
Definition: vaapi_encode.h:208
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
VAEncMiscParameterBuffer * global_params[MAX_GLOBAL_PARAMS]
Definition: vaapi_encode.h:138
simple assert() macros that are a bit more flexible than ISO C assert().
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:263
void * codec_picture_params
Definition: vaapi_encode.h:90
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:394
#define fail()
Definition: checkasm.h:80
VAConfigID va_config
Definition: vaapi_encode.h:110
AVHWFramesContext * recon_frames
Definition: vaapi_encode.h:131
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:192
The driver does not destroy parameter buffers when they are used by vaRenderPicture().
static int vaapi_encode_get_next(AVCodecContext *avctx, VAAPIEncodePicture **pic_out)
Definition: vaapi_encode.c:618
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:3102
static int vaapi_encode_make_param_buffer(AVCodecContext *avctx, VAAPIEncodePicture *pic, int type, char *data, size_t len)
Definition: vaapi_encode.c:74
AVFormatContext * ctx
Definition: movenc.c:48
struct VAAPIEncodePicture * next
Definition: vaapi_encode.h:66
static int vaapi_encode_discard(AVCodecContext *avctx, VAAPIEncodePicture *pic)
Definition: vaapi_encode.c:490
int(* write_extra_buffer)(AVCodecContext *avctx, VAAPIEncodePicture *pic, int index, int *type, char *data, size_t *data_len)
Definition: vaapi_encode.h:216
AVFrame * input_image
Definition: vaapi_encode.h:77
void * codec_picture_params
Definition: vaapi_encode.h:147
int64_t ts_ring[MAX_REORDER_DELAY *3]
Definition: vaapi_encode.h:166
AVBufferPool * output_buffer_pool
Definition: vaapi_encode.h:133
AVBufferPool * av_buffer_pool_init2(int size, void *opaque, AVBufferRef *(*alloc)(void *opaque, int size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition: buffer.c:197
VADisplay display
The VADisplay handle, to be filled by the user.
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
Definition: vaapi_encode.h:93
NULL
Definition: eval.c:55
int min_width
The minimum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:392
int(* init_slice_params)(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice)
Definition: vaapi_encode.h:198
const struct VAAPIEncodeType * codec
Definition: vaapi_encode.h:103
Libavcodec external API header.
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:374
VAAPIEncodePicture * pic_start
Definition: vaapi_encode.h:150
main external API structure.
Definition: avcodec.h:1409
AVHWFramesConstraints * av_hwdevice_get_hwframe_constraints(AVBufferRef *ref, const void *hwconfig)
Get the constraints on HW frames given a device and the HW-specific configuration to be used with tha...
Definition: hwcontext.c:429
uint8_t * data
The data buffer.
Definition: buffer.h:89
static VAAPIEncodePicture * vaapi_encode_alloc(void)
Definition: vaapi_encode.c:507
VABufferID param_buffers[MAX_PARAM_BUFFERS]
Definition: vaapi_encode.h:84
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:155
size_t slice_params_size
Definition: vaapi_encode.h:193
unsigned int driver_quirks
Driver quirks to apply - this is filled by av_hwdevice_ctx_init(), with reference to a table of known...
static const AVProfile profiles[]
Definition: libdcadec.c:181
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:254
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:117
int(* write_extra_header)(AVCodecContext *avctx, VAAPIEncodePicture *pic, int index, int *type, char *data, size_t *data_len)
Definition: vaapi_encode.h:220
AVBufferRef * recon_frames_ref
Definition: vaapi_encode.h:130
static av_cold int vaapi_encode_check_config(AVCodecContext *avctx)
Definition: vaapi_encode.c:925
AVBufferRef * device_ref
Definition: vaapi_encode.h:115
VAAPIEncodePicture * pic_end
Definition: vaapi_encode.h:150
static void * av_malloc_array(size_t nmemb, size_t size)
Definition: mem.h:92
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:134
size_t global_params_size[MAX_GLOBAL_PARAMS]
Definition: vaapi_encode.h:139
static int vaapi_encode_issue(AVCodecContext *avctx, VAAPIEncodePicture *pic)
Definition: vaapi_encode.c:129
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1606
A reference to a data buffer.
Definition: buffer.h:81
size_t sequence_params_size
Definition: vaapi_encode.h:191
static int vaapi_encode_clear_old(AVCodecContext *avctx)
Definition: vaapi_encode.c:790
common internal and external API header
static int vaapi_encode_output(AVCodecContext *avctx, VAAPIEncodePicture *pic, AVPacket *pkt)
Definition: vaapi_encode.c:429
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:177
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
void * priv_data
Definition: avcodec.h:1451
int len
enum AVPixelFormat * valid_sw_formats
A list of possible values for sw_format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition: hwcontext.h:386
void * codec_slice_params
Definition: vaapi_encode.h:62
AVFrame * recon_image
Definition: vaapi_encode.h:80
static AVBufferRef * vaapi_encode_alloc_output_buffer(void *opaque, int size)
static int vaapi_encode_wait(AVCodecContext *avctx, VAAPIEncodePicture *pic)
Definition: vaapi_encode.c:98
VAConfigID config_id
ID of a VAAPI pipeline configuration.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
static int vaapi_encode_make_packed_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, int type, char *data, size_t bit_len)
Definition: vaapi_encode.c:32
int height
Definition: frame.h:179
VASurfaceID * surface_ids
The surfaces IDs of all surfaces in the pool after creation.
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:310
int(* init)(AVCodecContext *avctx)
Definition: vaapi_encode.h:188
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:1704
VABufferID output_buffer
Definition: vaapi_encode.h:87
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:215
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
AVVAAPIDeviceContext * hwctx
Definition: vaapi_encode.h:117
int(* write_slice_header)(AVCodecContext *avctx, VAAPIEncodePicture *pic, VAAPIEncodeSlice *slice, char *data, size_t *data_len)
Definition: vaapi_encode.h:211