FFmpeg  4.4.9
vaapi_decode.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #include "libavutil/avassert.h"
20 #include "libavutil/common.h"
21 #include "libavutil/pixdesc.h"
22 
23 #include "avcodec.h"
24 #include "decode.h"
25 #include "internal.h"
26 #include "vaapi_decode.h"
27 #include "vaapi_hevc.h"
28 
29 
31  VAAPIDecodePicture *pic,
32  int type,
33  const void *data,
34  size_t size)
35 {
37  VAStatus vas;
38  VABufferID buffer;
39 
41 
42  vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
43  type, size, 1, (void*)data, &buffer);
44  if (vas != VA_STATUS_SUCCESS) {
45  av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
46  "buffer (type %d): %d (%s).\n",
47  type, vas, vaErrorStr(vas));
48  return AVERROR(EIO);
49  }
50 
51  pic->param_buffers[pic->nb_param_buffers++] = buffer;
52 
53  av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes) "
54  "is %#x.\n", type, size, buffer);
55  return 0;
56 }
57 
58 
60  VAAPIDecodePicture *pic,
61  const void *params_data,
62  size_t params_size,
63  const void *slice_data,
64  size_t slice_size)
65 {
67  VAStatus vas;
68  int index;
69 
71  if (pic->nb_slices == pic->slices_allocated) {
72  if (pic->slices_allocated > 0)
73  pic->slices_allocated *= 2;
74  else
75  pic->slices_allocated = 64;
76 
77  pic->slice_buffers =
79  pic->slices_allocated,
80  2 * sizeof(*pic->slice_buffers));
81  if (!pic->slice_buffers)
82  return AVERROR(ENOMEM);
83  }
84  av_assert0(pic->nb_slices + 1 <= pic->slices_allocated);
85 
86  index = 2 * pic->nb_slices;
87 
88  vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
89  VASliceParameterBufferType,
90  params_size, 1, (void*)params_data,
91  &pic->slice_buffers[index]);
92  if (vas != VA_STATUS_SUCCESS) {
93  av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
94  "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
95  return AVERROR(EIO);
96  }
97 
98  av_log(avctx, AV_LOG_DEBUG, "Slice %d param buffer (%zu bytes) "
99  "is %#x.\n", pic->nb_slices, params_size,
100  pic->slice_buffers[index]);
101 
102  vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
103  VASliceDataBufferType,
104  slice_size, 1, (void*)slice_data,
105  &pic->slice_buffers[index + 1]);
106  if (vas != VA_STATUS_SUCCESS) {
107  av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
108  "data buffer (size %zu): %d (%s).\n",
109  slice_size, vas, vaErrorStr(vas));
110  vaDestroyBuffer(ctx->hwctx->display,
111  pic->slice_buffers[index]);
112  return AVERROR(EIO);
113  }
114 
115  av_log(avctx, AV_LOG_DEBUG, "Slice %d data buffer (%zu bytes) "
116  "is %#x.\n", pic->nb_slices, slice_size,
117  pic->slice_buffers[index + 1]);
118 
119  ++pic->nb_slices;
120  return 0;
121 }
122 
124  VAAPIDecodePicture *pic)
125 {
127  VAStatus vas;
128  int i;
129 
130  for (i = 0; i < pic->nb_param_buffers; i++) {
131  vas = vaDestroyBuffer(ctx->hwctx->display,
132  pic->param_buffers[i]);
133  if (vas != VA_STATUS_SUCCESS) {
134  av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
135  "parameter buffer %#x: %d (%s).\n",
136  pic->param_buffers[i], vas, vaErrorStr(vas));
137  }
138  }
139 
140  for (i = 0; i < 2 * pic->nb_slices; i++) {
141  vas = vaDestroyBuffer(ctx->hwctx->display,
142  pic->slice_buffers[i]);
143  if (vas != VA_STATUS_SUCCESS) {
144  av_log(avctx, AV_LOG_ERROR, "Failed to destroy slice "
145  "slice buffer %#x: %d (%s).\n",
146  pic->slice_buffers[i], vas, vaErrorStr(vas));
147  }
148  }
149 }
150 
152  VAAPIDecodePicture *pic)
153 {
155  VAStatus vas;
156  int err;
157 
158  if (pic->nb_slices <= 0) {
159  err = AVERROR(EINVAL);
160  goto fail;
161  }
162 
163  av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n",
164  pic->output_surface);
165 
166  vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
167  pic->output_surface);
168  if (vas != VA_STATUS_SUCCESS) {
169  av_log(avctx, AV_LOG_ERROR, "Failed to begin picture decode "
170  "issue: %d (%s).\n", vas, vaErrorStr(vas));
171  err = AVERROR(EIO);
172  goto fail_with_picture;
173  }
174 
175  vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
176  pic->param_buffers, pic->nb_param_buffers);
177  if (vas != VA_STATUS_SUCCESS) {
178  av_log(avctx, AV_LOG_ERROR, "Failed to upload decode "
179  "parameters: %d (%s).\n", vas, vaErrorStr(vas));
180  err = AVERROR(EIO);
181  goto fail_with_picture;
182  }
183 
184  vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
185  pic->slice_buffers, 2 * pic->nb_slices);
186  if (vas != VA_STATUS_SUCCESS) {
187  av_log(avctx, AV_LOG_ERROR, "Failed to upload slices: "
188  "%d (%s).\n", vas, vaErrorStr(vas));
189  err = AVERROR(EIO);
190  goto fail_with_picture;
191  }
192 
193  vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
194  if (vas != VA_STATUS_SUCCESS) {
195  av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
196  "issue: %d (%s).\n", vas, vaErrorStr(vas));
197  err = AVERROR(EIO);
198  if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
200  goto fail;
201  else
202  goto fail_at_end;
203  }
204 
205  if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
208 
209  err = 0;
210  goto exit;
211 
212 fail_with_picture:
213  vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
214  if (vas != VA_STATUS_SUCCESS) {
215  av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
216  "after error: %d (%s).\n", vas, vaErrorStr(vas));
217  }
218 fail:
220 fail_at_end:
221 exit:
222  pic->nb_param_buffers = 0;
223  pic->nb_slices = 0;
224  pic->slices_allocated = 0;
225  av_freep(&pic->slice_buffers);
226 
227  return err;
228 }
229 
231  VAAPIDecodePicture *pic)
232 {
234 
235  pic->nb_param_buffers = 0;
236  pic->nb_slices = 0;
237  pic->slices_allocated = 0;
238  av_freep(&pic->slice_buffers);
239 
240  return 0;
241 }
242 
243 static const struct {
244  uint32_t fourcc;
245  enum AVPixelFormat pix_fmt;
246 } vaapi_format_map[] = {
247 #define MAP(va, av) { VA_FOURCC_ ## va, AV_PIX_FMT_ ## av }
248  // 4:0:0
249  MAP(Y800, GRAY8),
250  // 4:2:0
251  MAP(NV12, NV12),
252  MAP(YV12, YUV420P),
253  MAP(IYUV, YUV420P),
254 #ifdef VA_FOURCC_I420
255  MAP(I420, YUV420P),
256 #endif
257  MAP(IMC3, YUV420P),
258  // 4:1:1
259  MAP(411P, YUV411P),
260  // 4:2:2
261  MAP(422H, YUV422P),
262 #ifdef VA_FOURCC_YV16
263  MAP(YV16, YUV422P),
264 #endif
265  MAP(YUY2, YUYV422),
266 #ifdef VA_FOURCC_Y210
267  MAP(Y210, Y210),
268 #endif
269  // 4:4:0
270  MAP(422V, YUV440P),
271  // 4:4:4
272  MAP(444P, YUV444P),
273  // 4:2:0 10-bit
274 #ifdef VA_FOURCC_P010
275  MAP(P010, P010),
276 #endif
277 #ifdef VA_FOURCC_I010
278  MAP(I010, YUV420P10),
279 #endif
280 #undef MAP
281 };
282 
284  AVHWDeviceContext *device,
285  VAConfigID config_id,
287 {
288  AVVAAPIDeviceContext *hwctx = device->hwctx;
289  VAStatus vas;
290  VASurfaceAttrib *attr;
291  enum AVPixelFormat source_format, best_format, format;
292  uint32_t best_fourcc, fourcc;
293  int i, j, nb_attr;
294 
295  source_format = avctx->sw_pix_fmt;
296  av_assert0(source_format != AV_PIX_FMT_NONE);
297 
298  vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
299  NULL, &nb_attr);
300  if (vas != VA_STATUS_SUCCESS) {
301  av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
302  "%d (%s).\n", vas, vaErrorStr(vas));
303  return AVERROR(ENOSYS);
304  }
305 
306  attr = av_malloc_array(nb_attr, sizeof(*attr));
307  if (!attr)
308  return AVERROR(ENOMEM);
309 
310  vas = vaQuerySurfaceAttributes(hwctx->display, config_id,
311  attr, &nb_attr);
312  if (vas != VA_STATUS_SUCCESS) {
313  av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: "
314  "%d (%s).\n", vas, vaErrorStr(vas));
315  av_freep(&attr);
316  return AVERROR(ENOSYS);
317  }
318 
319  best_format = AV_PIX_FMT_NONE;
320 
321  for (i = 0; i < nb_attr; i++) {
322  if (attr[i].type != VASurfaceAttribPixelFormat)
323  continue;
324 
325  fourcc = attr[i].value.value.i;
326  for (j = 0; j < FF_ARRAY_ELEMS(vaapi_format_map); j++) {
327  if (fourcc == vaapi_format_map[j].fourcc)
328  break;
329  }
330  if (j >= FF_ARRAY_ELEMS(vaapi_format_map)) {
331  av_log(avctx, AV_LOG_DEBUG, "Ignoring unknown format %#x.\n",
332  fourcc);
333  continue;
334  }
335  format = vaapi_format_map[j].pix_fmt;
336  av_log(avctx, AV_LOG_DEBUG, "Considering format %#x -> %s.\n",
338 
339  best_format = av_find_best_pix_fmt_of_2(format, best_format,
340  source_format, 0, NULL);
341  if (format == best_format)
342  best_fourcc = fourcc;
343  }
344 
345  av_freep(&attr);
346 
347  if (best_format == AV_PIX_FMT_NONE) {
348  av_log(avctx, AV_LOG_ERROR, "No usable formats for decoding!\n");
349  return AVERROR(EINVAL);
350  }
351 
352  av_log(avctx, AV_LOG_DEBUG, "Picked %s (%#x) as best match for %s.\n",
353  av_get_pix_fmt_name(best_format), best_fourcc,
354  av_get_pix_fmt_name(source_format));
355 
356  frames->sw_format = best_format;
357  if (avctx->internal->hwaccel_priv_data) {
359  AVVAAPIFramesContext *avfc = frames->hwctx;
360 
361  ctx->pixel_format_attribute = (VASurfaceAttrib) {
362  .type = VASurfaceAttribPixelFormat,
363  .value.value.i = best_fourcc,
364  };
365 
366  avfc->attributes = &ctx->pixel_format_attribute;
367  avfc->nb_attributes = 1;
368  }
369 
370  return 0;
371 }
372 
373 static const struct {
374  enum AVCodecID codec_id;
376  VAProfile va_profile;
377  VAProfile (*profile_parser)(AVCodecContext *avctx);
378 } vaapi_profile_map[] = {
379 #define MAP(c, p, v, ...) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v, __VA_ARGS__ }
380  MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ),
381  MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ),
382  MAP(H263, UNKNOWN, H263Baseline),
383  MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ),
384  MAP(MPEG4, MPEG4_ADVANCED_SIMPLE,
385  MPEG4AdvancedSimple),
386  MAP(MPEG4, MPEG4_MAIN, MPEG4Main ),
387  MAP(H264, H264_CONSTRAINED_BASELINE,
388  H264ConstrainedBaseline),
389  MAP(H264, H264_MAIN, H264Main ),
390  MAP(H264, H264_HIGH, H264High ),
391 #if VA_CHECK_VERSION(0, 37, 0)
392  MAP(HEVC, HEVC_MAIN, HEVCMain ),
393  MAP(HEVC, HEVC_MAIN_10, HEVCMain10 ),
394  MAP(HEVC, HEVC_MAIN_STILL_PICTURE,
395  HEVCMain ),
396 #endif
397 #if VA_CHECK_VERSION(1, 2, 0) && CONFIG_HEVC_VAAPI_HWACCEL
398  MAP(HEVC, HEVC_REXT, None,
400 #endif
401  MAP(MJPEG, MJPEG_HUFFMAN_BASELINE_DCT,
402  JPEGBaseline),
403  MAP(WMV3, VC1_SIMPLE, VC1Simple ),
404  MAP(WMV3, VC1_MAIN, VC1Main ),
405  MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
406  MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
407  MAP(VC1, VC1_SIMPLE, VC1Simple ),
408  MAP(VC1, VC1_MAIN, VC1Main ),
409  MAP(VC1, VC1_COMPLEX, VC1Advanced ),
410  MAP(VC1, VC1_ADVANCED, VC1Advanced ),
411  MAP(VP8, UNKNOWN, VP8Version0_3 ),
412 #if VA_CHECK_VERSION(0, 38, 0)
413  MAP(VP9, VP9_0, VP9Profile0 ),
414 #endif
415 #if VA_CHECK_VERSION(0, 39, 0)
416  MAP(VP9, VP9_2, VP9Profile2 ),
417 #endif
418 #if VA_CHECK_VERSION(1, 8, 0)
419  MAP(AV1, AV1_MAIN, AV1Profile0),
420  MAP(AV1, AV1_HIGH, AV1Profile1),
421 #endif
422 
423 #undef MAP
424 };
425 
426 /*
427  * Set *va_config and the frames_ref fields from the current codec parameters
428  * in avctx.
429  */
431  AVBufferRef *device_ref,
432  VAConfigID *va_config,
433  AVBufferRef *frames_ref)
434 {
435  AVVAAPIHWConfig *hwconfig = NULL;
436  AVHWFramesConstraints *constraints = NULL;
437  VAStatus vas;
438  int err, i, j;
439  const AVCodecDescriptor *codec_desc;
440  VAProfile *profile_list = NULL, matched_va_profile, va_profile;
441  int profile_count, exact_match, matched_ff_profile, codec_profile;
442 
443  AVHWDeviceContext *device = (AVHWDeviceContext*)device_ref->data;
444  AVVAAPIDeviceContext *hwctx = device->hwctx;
445 
446  codec_desc = avcodec_descriptor_get(avctx->codec_id);
447  if (!codec_desc) {
448  err = AVERROR(EINVAL);
449  goto fail;
450  }
451 
452  profile_count = vaMaxNumProfiles(hwctx->display);
453  profile_list = av_malloc_array(profile_count,
454  sizeof(VAProfile));
455  if (!profile_list) {
456  err = AVERROR(ENOMEM);
457  goto fail;
458  }
459 
460  vas = vaQueryConfigProfiles(hwctx->display,
461  profile_list, &profile_count);
462  if (vas != VA_STATUS_SUCCESS) {
463  av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
464  "%d (%s).\n", vas, vaErrorStr(vas));
465  err = AVERROR(ENOSYS);
466  goto fail;
467  }
468 
469  matched_va_profile = VAProfileNone;
470  exact_match = 0;
471 
472  for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
473  int profile_match = 0;
474  if (avctx->codec_id != vaapi_profile_map[i].codec_id)
475  continue;
476  if (avctx->profile == vaapi_profile_map[i].codec_profile ||
477  vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
478  profile_match = 1;
479 
480  va_profile = vaapi_profile_map[i].profile_parser ?
481  vaapi_profile_map[i].profile_parser(avctx) :
482  vaapi_profile_map[i].va_profile;
483  codec_profile = vaapi_profile_map[i].codec_profile;
484 
485  for (j = 0; j < profile_count; j++) {
486  if (va_profile == profile_list[j]) {
487  exact_match = profile_match;
488  break;
489  }
490  }
491  if (j < profile_count) {
492  matched_va_profile = va_profile;
493  matched_ff_profile = codec_profile;
494  if (exact_match)
495  break;
496  }
497  }
498  av_freep(&profile_list);
499 
500  if (matched_va_profile == VAProfileNone) {
501  av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
502  "profile %d.\n", codec_desc->name, avctx->profile);
503  err = AVERROR(ENOSYS);
504  goto fail;
505  }
506  if (!exact_match) {
507  if (avctx->hwaccel_flags &
509  av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
510  "supported for hardware decode.\n",
511  codec_desc->name, avctx->profile);
512  av_log(avctx, AV_LOG_WARNING, "Using possibly-"
513  "incompatible profile %d instead.\n",
514  matched_ff_profile);
515  } else {
516  av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
517  "supported for hardware decode.\n",
518  codec_desc->name, avctx->profile);
519  err = AVERROR(EINVAL);
520  goto fail;
521  }
522  }
523 
524  vas = vaCreateConfig(hwctx->display, matched_va_profile,
525  VAEntrypointVLD, NULL, 0,
526  va_config);
527  if (vas != VA_STATUS_SUCCESS) {
528  av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
529  "configuration: %d (%s).\n", vas, vaErrorStr(vas));
530  err = AVERROR(EIO);
531  goto fail;
532  }
533 
534  hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
535  if (!hwconfig) {
536  err = AVERROR(ENOMEM);
537  goto fail;
538  }
539  hwconfig->config_id = *va_config;
540 
541  constraints =
542  av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
543  if (!constraints) {
544  err = AVERROR(ENOMEM);
545  goto fail;
546  }
547 
548  if (avctx->coded_width < constraints->min_width ||
549  avctx->coded_height < constraints->min_height ||
550  avctx->coded_width > constraints->max_width ||
551  avctx->coded_height > constraints->max_height) {
552  av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
553  "size %dx%d (constraints: width %d-%d height %d-%d).\n",
554  avctx->coded_width, avctx->coded_height,
555  constraints->min_width, constraints->max_width,
556  constraints->min_height, constraints->max_height);
557  err = AVERROR(EINVAL);
558  goto fail;
559  }
560  if (!constraints->valid_sw_formats ||
561  constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
562  av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
563  "usable surface formats.\n");
564  err = AVERROR(EINVAL);
565  goto fail;
566  }
567 
568  if (frames_ref) {
570 
571  frames->format = AV_PIX_FMT_VAAPI;
572  frames->width = avctx->coded_width;
573  frames->height = avctx->coded_height;
574 
575  err = vaapi_decode_find_best_format(avctx, device,
576  *va_config, frames);
577  if (err < 0)
578  goto fail;
579 
580  frames->initial_pool_size = 1;
581  // Add per-codec number of surfaces used for storing reference frames.
582  switch (avctx->codec_id) {
583  case AV_CODEC_ID_H264:
584  case AV_CODEC_ID_HEVC:
585  case AV_CODEC_ID_AV1:
586  frames->initial_pool_size += 16;
587  break;
588  case AV_CODEC_ID_VP9:
589  frames->initial_pool_size += 8;
590  break;
591  case AV_CODEC_ID_VP8:
592  frames->initial_pool_size += 3;
593  break;
594  default:
595  frames->initial_pool_size += 2;
596  }
597  }
598 
599  av_hwframe_constraints_free(&constraints);
600  av_freep(&hwconfig);
601 
602  return 0;
603 
604 fail:
605  av_hwframe_constraints_free(&constraints);
606  av_freep(&hwconfig);
607  if (*va_config != VA_INVALID_ID) {
608  vaDestroyConfig(hwctx->display, *va_config);
609  *va_config = VA_INVALID_ID;
610  }
611  av_freep(&profile_list);
612  return err;
613 }
614 
616  AVBufferRef *hw_frames_ctx)
617 {
618  AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
619  AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
620  AVVAAPIDeviceContext *hwctx;
621  VAConfigID va_config = VA_INVALID_ID;
622  int err;
623 
624  if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
625  return AVERROR(EINVAL);
626  hwctx = device_ctx->hwctx;
627 
628  err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
629  hw_frames_ctx);
630  if (err)
631  return err;
632 
633  if (va_config != VA_INVALID_ID)
634  vaDestroyConfig(hwctx->display, va_config);
635 
636  return 0;
637 }
638 
640 {
642  VAStatus vas;
643  int err;
644 
645  ctx->va_config = VA_INVALID_ID;
646  ctx->va_context = VA_INVALID_ID;
647 
648 #if FF_API_STRUCT_VAAPI_CONTEXT
649  if (avctx->hwaccel_context) {
650  av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
651  "vaapi_context in decode.\n");
652 
653  ctx->have_old_context = 1;
654  ctx->old_context = avctx->hwaccel_context;
655 
656  // Really we only want the VAAPI device context, but this
657  // allocates a whole generic device context because we don't
658  // have any other way to determine how big it should be.
659  ctx->device_ref =
661  if (!ctx->device_ref) {
662  err = AVERROR(ENOMEM);
663  goto fail;
664  }
665  ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
666  ctx->hwctx = ctx->device->hwctx;
667 
668  ctx->hwctx->display = ctx->old_context->display;
669 
670  // The old VAAPI decode setup assumed this quirk was always
671  // present, so set it here to avoid the behaviour changing.
672  ctx->hwctx->driver_quirks =
674 
675  }
676 #endif
677 
678 #if FF_API_STRUCT_VAAPI_CONTEXT
679  if (ctx->have_old_context) {
680  ctx->va_config = ctx->old_context->config_id;
681  ctx->va_context = ctx->old_context->context_id;
682 
683  av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
684  "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
685  } else {
686 #endif
687 
689  if (err < 0)
690  goto fail;
691 
692  ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
693  ctx->hwfc = ctx->frames->hwctx;
694  ctx->device = ctx->frames->device_ctx;
695  ctx->hwctx = ctx->device->hwctx;
696 
697  err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
698  &ctx->va_config, avctx->hw_frames_ctx);
699  if (err)
700  goto fail;
701 
702  vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
703  avctx->coded_width, avctx->coded_height,
704  VA_PROGRESSIVE,
705  ctx->hwfc->surface_ids,
706  ctx->hwfc->nb_surfaces,
707  &ctx->va_context);
708  if (vas != VA_STATUS_SUCCESS) {
709  av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
710  "context: %d (%s).\n", vas, vaErrorStr(vas));
711  err = AVERROR(EIO);
712  goto fail;
713  }
714 
715  av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
716  "%#x/%#x.\n", ctx->va_config, ctx->va_context);
717 #if FF_API_STRUCT_VAAPI_CONTEXT
718  }
719 #endif
720 
721  return 0;
722 
723 fail:
724  ff_vaapi_decode_uninit(avctx);
725  return err;
726 }
727 
729 {
731  VAStatus vas;
732 
733 #if FF_API_STRUCT_VAAPI_CONTEXT
734  if (ctx->have_old_context) {
735  av_buffer_unref(&ctx->device_ref);
736  } else {
737 #endif
738 
739  if (ctx->va_context != VA_INVALID_ID) {
740  vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
741  if (vas != VA_STATUS_SUCCESS) {
742  av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
743  "context %#x: %d (%s).\n",
744  ctx->va_context, vas, vaErrorStr(vas));
745  }
746  }
747  if (ctx->va_config != VA_INVALID_ID) {
748  vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
749  if (vas != VA_STATUS_SUCCESS) {
750  av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
751  "configuration %#x: %d (%s).\n",
752  ctx->va_config, vas, vaErrorStr(vas));
753  }
754  }
755 
756 #if FF_API_STRUCT_VAAPI_CONTEXT
757  }
758 #endif
759 
760  return 0;
761 }
static const char *const format[]
Definition: af_aiir.c:457
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1863
#define V
Definition: avdct.c:30
#define fail()
Definition: checkasm.h:133
common internal and external API header
#define CONFIG_VAAPI_1
Definition: config.h:700
#define NULL
Definition: coverity.c:32
int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx, enum AVHWDeviceType dev_type)
Make sure avctx.hw_frames_ctx is set.
Definition: decode.c:1188
@ UNKNOWN
Definition: ftp.c:37
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3501
@ AV_CODEC_ID_H264
Definition: codec_id.h:76
@ AV_CODEC_ID_AV1
Definition: codec_id.h:279
@ AV_CODEC_ID_VP8
Definition: codec_id.h:189
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:223
@ AV_CODEC_ID_VP9
Definition: codec_id.h:217
#define AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH
Hardware acceleration should still be attempted for decoding when the codec profile does not match th...
Definition: avcodec.h:2638
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:125
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array.
Definition: mem.c:198
int index
Definition: gxfenc.c:89
AVBufferRef * av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
Allocate an AVHWDeviceContext for a given hardware type.
Definition: hwcontext.c:142
void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
Free an AVHWFrameConstraints structure.
Definition: hwcontext.c:605
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:580
void * av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
Allocate a HW-specific configuration structure for a given HW device.
Definition: hwcontext.c:569
@ AV_HWDEVICE_TYPE_VAAPI
Definition: hwcontext.h:31
cl_device_type type
@ AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS
The driver does not destroy parameter buffers when they are used by vaRenderPicture().
int i
Definition: input.c:407
common internal API header
#define P
const char data[16]
Definition: mxf.c:142
enum AVPixelFormat av_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2, enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
Compute what kind of losses will occur when converting from one specific pixel format to another.
Definition: pixdesc.c:2864
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:2489
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_VAAPI
Definition: pixfmt.h:122
static char buffer[20]
Definition: seek.c:32
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition: buffer.h:84
uint8_t * data
The data buffer.
Definition: buffer.h:92
main external API structure.
Definition: avcodec.h:536
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active).
Definition: avcodec.h:2283
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:2082
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:2222
int profile
profile
Definition: avcodec.h:1862
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:1696
int coded_height
Definition: avcodec.h:724
enum AVCodecID codec_id
Definition: avcodec.h:546
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:724
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:571
This struct describes the properties of a single codec described by an AVCodecID.
Definition: codec_desc.h:38
const char * name
Name of the codec described by this descriptor.
Definition: codec_desc.h:46
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:180
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:61
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:92
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:453
int max_width
The maximum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:478
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:465
int min_width
The minimum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:471
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:141
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:149
VAAPI connection details.
VADisplay display
The VADisplay handle, to be filled by the user.
VAAPI-specific data associated with a frame pool.
VASurfaceAttrib * attributes
Set by the user to apply surface attributes to all surfaces in the frame pool.
VAAPI hardware pipeline configuration details.
VAConfigID config_id
ID of a VAAPI pipeline configuration.
VABufferID * slice_buffers
Definition: vaapi_decode.h:51
VABufferID param_buffers[MAX_PARAM_BUFFERS]
Definition: vaapi_decode.h:48
VASurfaceID output_surface
Definition: vaapi_decode.h:45
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
int frames
Definition: movenc.c:66
AVFormatContext * ctx
Definition: movenc.c:48
int size
enum AVCodecID codec_id
Definition: vaapi_decode.c:374
int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx, VAAPIDecodePicture *pic, const void *params_data, size_t params_size, const void *slice_data, size_t slice_size)
Definition: vaapi_decode.c:59
static int vaapi_decode_make_config(AVCodecContext *avctx, AVBufferRef *device_ref, VAConfigID *va_config, AVBufferRef *frames_ref)
Definition: vaapi_decode.c:430
int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx, VAAPIDecodePicture *pic, int type, const void *data, size_t size)
Definition: vaapi_decode.c:30
enum AVPixelFormat pix_fmt
Definition: vaapi_decode.c:245
int ff_vaapi_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition: vaapi_decode.c:615
VAProfile va_profile
Definition: vaapi_decode.c:376
#define MAP(va, av)
int ff_vaapi_decode_issue(AVCodecContext *avctx, VAAPIDecodePicture *pic)
Definition: vaapi_decode.c:151
VAProfile(* profile_parser)(AVCodecContext *avctx)
Definition: vaapi_decode.c:377
uint32_t fourcc
Definition: vaapi_decode.c:244
static const struct @149 vaapi_format_map[]
int ff_vaapi_decode_init(AVCodecContext *avctx)
Definition: vaapi_decode.c:639
static const struct @150 vaapi_profile_map[]
int ff_vaapi_decode_uninit(AVCodecContext *avctx)
Definition: vaapi_decode.c:728
static int vaapi_decode_find_best_format(AVCodecContext *avctx, AVHWDeviceContext *device, VAConfigID config_id, AVHWFramesContext *frames)
Definition: vaapi_decode.c:283
int ff_vaapi_decode_cancel(AVCodecContext *avctx, VAAPIDecodePicture *pic)
Definition: vaapi_decode.c:230
static void ff_vaapi_decode_destroy_buffers(AVCodecContext *avctx, VAAPIDecodePicture *pic)
Definition: vaapi_decode.c:123
int codec_profile
Definition: vaapi_decode.c:375
@ MAX_PARAM_BUFFERS
Definition: vaapi_decode.h:41
VAProfile ff_vaapi_parse_hevc_rext_profile(AVCodecContext *avctx)
Definition: vaapi_hevc.c:531
@ H
Definition: vf_addroi.c:26
if(ret< 0)
Definition: vf_mcdeint.c:282