]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_decode.c
avcodec/videotoolbox: remove unnecessary if statement
[ffmpeg] / libavcodec / vaapi_decode.c
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
28
29 int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx,
30                                       VAAPIDecodePicture *pic,
31                                       int type,
32                                       const void *data,
33                                       size_t size)
34 {
35     VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
36     VAStatus vas;
37     VABufferID buffer;
38
39     av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
40
41     vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
42                          type, size, 1, (void*)data, &buffer);
43     if (vas != VA_STATUS_SUCCESS) {
44         av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
45                "buffer (type %d): %d (%s).\n",
46                type, vas, vaErrorStr(vas));
47         return AVERROR(EIO);
48     }
49
50     pic->param_buffers[pic->nb_param_buffers++] = buffer;
51
52     av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes) "
53            "is %#x.\n", type, size, buffer);
54     return 0;
55 }
56
57
58 int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx,
59                                       VAAPIDecodePicture *pic,
60                                       const void *params_data,
61                                       size_t params_size,
62                                       const void *slice_data,
63                                       size_t slice_size)
64 {
65     VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
66     VAStatus vas;
67     int index;
68
69     av_assert0(pic->nb_slices <= pic->slices_allocated);
70     if (pic->nb_slices == pic->slices_allocated) {
71         if (pic->slices_allocated > 0)
72             pic->slices_allocated *= 2;
73         else
74             pic->slices_allocated = 64;
75
76         pic->slice_buffers =
77             av_realloc_array(pic->slice_buffers,
78                              pic->slices_allocated,
79                              2 * sizeof(*pic->slice_buffers));
80         if (!pic->slice_buffers)
81             return AVERROR(ENOMEM);
82     }
83     av_assert0(pic->nb_slices + 1 <= pic->slices_allocated);
84
85     index = 2 * pic->nb_slices;
86
87     vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
88                          VASliceParameterBufferType,
89                          params_size, 1, (void*)params_data,
90                          &pic->slice_buffers[index]);
91     if (vas != VA_STATUS_SUCCESS) {
92         av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
93                "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
94         return AVERROR(EIO);
95     }
96
97     av_log(avctx, AV_LOG_DEBUG, "Slice %d param buffer (%zu bytes) "
98            "is %#x.\n", pic->nb_slices, params_size,
99            pic->slice_buffers[index]);
100
101     vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
102                          VASliceDataBufferType,
103                          slice_size, 1, (void*)slice_data,
104                          &pic->slice_buffers[index + 1]);
105     if (vas != VA_STATUS_SUCCESS) {
106         av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
107                "data buffer (size %zu): %d (%s).\n",
108                slice_size, vas, vaErrorStr(vas));
109         vaDestroyBuffer(ctx->hwctx->display,
110                         pic->slice_buffers[index]);
111         return AVERROR(EIO);
112     }
113
114     av_log(avctx, AV_LOG_DEBUG, "Slice %d data buffer (%zu bytes) "
115            "is %#x.\n", pic->nb_slices, slice_size,
116            pic->slice_buffers[index + 1]);
117
118     ++pic->nb_slices;
119     return 0;
120 }
121
122 static void ff_vaapi_decode_destroy_buffers(AVCodecContext *avctx,
123                                             VAAPIDecodePicture *pic)
124 {
125     VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
126     VAStatus vas;
127     int i;
128
129     for (i = 0; i < pic->nb_param_buffers; i++) {
130         vas = vaDestroyBuffer(ctx->hwctx->display,
131                               pic->param_buffers[i]);
132         if (vas != VA_STATUS_SUCCESS) {
133             av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
134                    "parameter buffer %#x: %d (%s).\n",
135                    pic->param_buffers[i], vas, vaErrorStr(vas));
136         }
137     }
138
139     for (i = 0; i < 2 * pic->nb_slices; i++) {
140         vas = vaDestroyBuffer(ctx->hwctx->display,
141                               pic->slice_buffers[i]);
142         if (vas != VA_STATUS_SUCCESS) {
143             av_log(avctx, AV_LOG_ERROR, "Failed to destroy slice "
144                    "slice buffer %#x: %d (%s).\n",
145                    pic->slice_buffers[i], vas, vaErrorStr(vas));
146         }
147     }
148 }
149
150 int ff_vaapi_decode_issue(AVCodecContext *avctx,
151                           VAAPIDecodePicture *pic)
152 {
153     VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
154     VAStatus vas;
155     int err;
156
157     av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n",
158            pic->output_surface);
159
160     vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
161                          pic->output_surface);
162     if (vas != VA_STATUS_SUCCESS) {
163         av_log(avctx, AV_LOG_ERROR, "Failed to begin picture decode "
164                "issue: %d (%s).\n", vas, vaErrorStr(vas));
165         err = AVERROR(EIO);
166         goto fail_with_picture;
167     }
168
169     vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
170                           pic->param_buffers, pic->nb_param_buffers);
171     if (vas != VA_STATUS_SUCCESS) {
172         av_log(avctx, AV_LOG_ERROR, "Failed to upload decode "
173                "parameters: %d (%s).\n", vas, vaErrorStr(vas));
174         err = AVERROR(EIO);
175         goto fail_with_picture;
176     }
177
178     vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
179                           pic->slice_buffers, 2 * pic->nb_slices);
180     if (vas != VA_STATUS_SUCCESS) {
181         av_log(avctx, AV_LOG_ERROR, "Failed to upload slices: "
182                "%d (%s).\n", vas, vaErrorStr(vas));
183         err = AVERROR(EIO);
184         goto fail_with_picture;
185     }
186
187     vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
188     if (vas != VA_STATUS_SUCCESS) {
189         av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
190                "issue: %d (%s).\n", vas, vaErrorStr(vas));
191         err = AVERROR(EIO);
192         if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
193             AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
194             goto fail;
195         else
196             goto fail_at_end;
197     }
198
199     if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
200         AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
201         ff_vaapi_decode_destroy_buffers(avctx, pic);
202
203     pic->nb_param_buffers = 0;
204     pic->nb_slices        = 0;
205     pic->slices_allocated = 0;
206     av_freep(&pic->slice_buffers);
207
208     return 0;
209
210 fail_with_picture:
211     vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
212     if (vas != VA_STATUS_SUCCESS) {
213         av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
214                "after error: %d (%s).\n", vas, vaErrorStr(vas));
215     }
216 fail:
217     ff_vaapi_decode_destroy_buffers(avctx, pic);
218 fail_at_end:
219     return err;
220 }
221
222 int ff_vaapi_decode_cancel(AVCodecContext *avctx,
223                            VAAPIDecodePicture *pic)
224 {
225     ff_vaapi_decode_destroy_buffers(avctx, pic);
226
227     pic->nb_param_buffers = 0;
228     pic->nb_slices        = 0;
229     pic->slices_allocated = 0;
230     av_freep(&pic->slice_buffers);
231
232     return 0;
233 }
234
235 static const struct {
236     enum AVCodecID codec_id;
237     int codec_profile;
238     VAProfile va_profile;
239 } vaapi_profile_map[] = {
240 #define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v }
241     MAP(MPEG2VIDEO,  MPEG2_SIMPLE,    MPEG2Simple ),
242     MAP(MPEG2VIDEO,  MPEG2_MAIN,      MPEG2Main   ),
243     MAP(H263,        UNKNOWN,         H263Baseline),
244     MAP(MPEG4,       MPEG4_SIMPLE,    MPEG4Simple ),
245     MAP(MPEG4,       MPEG4_ADVANCED_SIMPLE,
246                                MPEG4AdvancedSimple),
247     MAP(MPEG4,       MPEG4_MAIN,      MPEG4Main   ),
248     MAP(H264,        H264_CONSTRAINED_BASELINE,
249                            H264ConstrainedBaseline),
250     MAP(H264,        H264_MAIN,       H264Main    ),
251     MAP(H264,        H264_HIGH,       H264High    ),
252 #if VA_CHECK_VERSION(0, 37, 0)
253     MAP(HEVC,        HEVC_MAIN,       HEVCMain    ),
254     MAP(HEVC,        HEVC_MAIN_10,    HEVCMain10  ),
255 #endif
256     MAP(WMV3,        VC1_SIMPLE,      VC1Simple   ),
257     MAP(WMV3,        VC1_MAIN,        VC1Main     ),
258     MAP(WMV3,        VC1_COMPLEX,     VC1Advanced ),
259     MAP(WMV3,        VC1_ADVANCED,    VC1Advanced ),
260     MAP(VC1,         VC1_SIMPLE,      VC1Simple   ),
261     MAP(VC1,         VC1_MAIN,        VC1Main     ),
262     MAP(VC1,         VC1_COMPLEX,     VC1Advanced ),
263     MAP(VC1,         VC1_ADVANCED,    VC1Advanced ),
264 #if VA_CHECK_VERSION(0, 35, 0)
265     MAP(VP8,         UNKNOWN,       VP8Version0_3 ),
266 #endif
267 #if VA_CHECK_VERSION(0, 38, 0)
268     MAP(VP9,         VP9_0,           VP9Profile0 ),
269 #endif
270 #if VA_CHECK_VERSION(0, 39, 0)
271     MAP(VP9,         VP9_2,           VP9Profile2 ),
272 #endif
273 #undef MAP
274 };
275
276 /*
277  * Set *va_config and the frames_ref fields from the current codec parameters
278  * in avctx.
279  */
280 static int vaapi_decode_make_config(AVCodecContext *avctx,
281                                     AVBufferRef *device_ref,
282                                     VAConfigID *va_config,
283                                     AVBufferRef *frames_ref)
284 {
285     AVVAAPIHWConfig       *hwconfig    = NULL;
286     AVHWFramesConstraints *constraints = NULL;
287     VAStatus vas;
288     int err, i, j;
289     const AVCodecDescriptor *codec_desc;
290     VAProfile profile, va_profile, *profile_list = NULL;
291     int profile_count, exact_match, alt_profile;
292     const AVPixFmtDescriptor *sw_desc, *desc;
293
294     AVHWDeviceContext    *device = (AVHWDeviceContext*)device_ref->data;
295     AVVAAPIDeviceContext *hwctx = device->hwctx;
296
297     codec_desc = avcodec_descriptor_get(avctx->codec_id);
298     if (!codec_desc) {
299         err = AVERROR(EINVAL);
300         goto fail;
301     }
302
303     profile_count = vaMaxNumProfiles(hwctx->display);
304     profile_list  = av_malloc_array(profile_count,
305                                     sizeof(VAProfile));
306     if (!profile_list) {
307         err = AVERROR(ENOMEM);
308         goto fail;
309     }
310
311     vas = vaQueryConfigProfiles(hwctx->display,
312                                 profile_list, &profile_count);
313     if (vas != VA_STATUS_SUCCESS) {
314         av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
315                "%d (%s).\n", vas, vaErrorStr(vas));
316         err = AVERROR(ENOSYS);
317         goto fail;
318     }
319
320     profile = VAProfileNone;
321     exact_match = 0;
322
323     for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
324         int profile_match = 0;
325         if (avctx->codec_id != vaapi_profile_map[i].codec_id)
326             continue;
327         if (avctx->profile == vaapi_profile_map[i].codec_profile)
328             profile_match = 1;
329         profile = vaapi_profile_map[i].va_profile;
330         for (j = 0; j < profile_count; j++) {
331             if (profile == profile_list[j]) {
332                 exact_match = profile_match;
333                 break;
334             }
335         }
336         if (j < profile_count) {
337             if (exact_match)
338                 break;
339             alt_profile = vaapi_profile_map[i].codec_profile;
340             va_profile = vaapi_profile_map[i].va_profile;
341         }
342     }
343     av_freep(&profile_list);
344
345     if (profile == VAProfileNone) {
346         av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
347                "profile %d.\n", codec_desc->name, avctx->profile);
348         err = AVERROR(ENOSYS);
349         goto fail;
350     }
351     if (!exact_match) {
352         if (avctx->hwaccel_flags &
353             AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
354             av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
355                    "supported for hardware decode.\n",
356                    codec_desc->name, avctx->profile);
357             av_log(avctx, AV_LOG_WARNING, "Using possibly-"
358                    "incompatible profile %d instead.\n",
359                    alt_profile);
360             profile = va_profile;
361         } else {
362             av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
363                    "supported for hardware decode.\n",
364                    codec_desc->name, avctx->profile);
365             err = AVERROR(EINVAL);
366             goto fail;
367         }
368     }
369
370     vas = vaCreateConfig(hwctx->display, profile,
371                          VAEntrypointVLD, NULL, 0,
372                          va_config);
373     if (vas != VA_STATUS_SUCCESS) {
374         av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
375                "configuration: %d (%s).\n", vas, vaErrorStr(vas));
376         err = AVERROR(EIO);
377         goto fail;
378     }
379
380     hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
381     if (!hwconfig) {
382         err = AVERROR(ENOMEM);
383         goto fail;
384     }
385     hwconfig->config_id = *va_config;
386
387     constraints =
388         av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
389     if (!constraints) {
390         err = AVERROR(ENOMEM);
391         goto fail;
392     }
393
394     if (avctx->coded_width  < constraints->min_width  ||
395         avctx->coded_height < constraints->min_height ||
396         avctx->coded_width  > constraints->max_width  ||
397         avctx->coded_height > constraints->max_height) {
398         av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
399                "size %dx%d (constraints: width %d-%d height %d-%d).\n",
400                avctx->coded_width, avctx->coded_height,
401                constraints->min_width,  constraints->max_width,
402                constraints->min_height, constraints->max_height);
403         err = AVERROR(EINVAL);
404         goto fail;
405     }
406     if (!constraints->valid_sw_formats ||
407         constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
408         av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
409                "usable surface formats.\n");
410         err = AVERROR(EINVAL);
411         goto fail;
412     }
413
414     if (frames_ref) {
415         AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data;
416
417         frames->format = AV_PIX_FMT_VAAPI;
418         frames->width = avctx->coded_width;
419         frames->height = avctx->coded_height;
420
421         // Find the first format in the list which matches the expected
422         // bit depth and subsampling.  If none are found (this can happen
423         // when 10-bit streams are decoded to 8-bit surfaces, for example)
424         // then just take the first format on the list.
425         frames->sw_format = constraints->valid_sw_formats[0];
426         sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
427         for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
428             desc = av_pix_fmt_desc_get(constraints->valid_sw_formats[i]);
429             if (desc->nb_components != sw_desc->nb_components ||
430                 desc->log2_chroma_w != sw_desc->log2_chroma_w ||
431                 desc->log2_chroma_h != sw_desc->log2_chroma_h)
432                 continue;
433             for (j = 0; j < desc->nb_components; j++) {
434                 if (desc->comp[j].depth != sw_desc->comp[j].depth)
435                     break;
436             }
437             if (j < desc->nb_components)
438                 continue;
439             frames->sw_format = constraints->valid_sw_formats[i];
440             break;
441         }
442
443         frames->initial_pool_size = 1;
444         // Add per-codec number of surfaces used for storing reference frames.
445         switch (avctx->codec_id) {
446         case AV_CODEC_ID_H264:
447         case AV_CODEC_ID_HEVC:
448             frames->initial_pool_size += 16;
449             break;
450         case AV_CODEC_ID_VP9:
451             frames->initial_pool_size += 8;
452             break;
453         case AV_CODEC_ID_VP8:
454             frames->initial_pool_size += 3;
455             break;
456         default:
457             frames->initial_pool_size += 2;
458         }
459     }
460
461     av_hwframe_constraints_free(&constraints);
462     av_freep(&hwconfig);
463
464     return 0;
465
466 fail:
467     av_hwframe_constraints_free(&constraints);
468     av_freep(&hwconfig);
469     if (*va_config != VA_INVALID_ID) {
470         vaDestroyConfig(hwctx->display, *va_config);
471         *va_config = VA_INVALID_ID;
472     }
473     av_freep(&profile_list);
474     return err;
475 }
476
477 int ff_vaapi_common_frame_params(AVCodecContext *avctx,
478                                  AVBufferRef *hw_frames_ctx)
479 {
480     AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
481     AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
482     AVVAAPIDeviceContext *hwctx;
483     VAConfigID va_config = VA_INVALID_ID;
484     int err;
485
486     if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
487         return AVERROR(EINVAL);
488     hwctx = device_ctx->hwctx;
489
490     err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
491                                    hw_frames_ctx);
492     if (err)
493         return err;
494
495     if (va_config != VA_INVALID_ID)
496         vaDestroyConfig(hwctx->display, va_config);
497
498     return 0;
499 }
500
501 int ff_vaapi_decode_init(AVCodecContext *avctx)
502 {
503     VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
504     VAStatus vas;
505     int err;
506
507     ctx->va_config  = VA_INVALID_ID;
508     ctx->va_context = VA_INVALID_ID;
509
510 #if FF_API_STRUCT_VAAPI_CONTEXT
511     if (avctx->hwaccel_context) {
512         av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
513                "vaapi_context in decode.\n");
514
515         ctx->have_old_context = 1;
516         ctx->old_context = avctx->hwaccel_context;
517
518         // Really we only want the VAAPI device context, but this
519         // allocates a whole generic device context because we don't
520         // have any other way to determine how big it should be.
521         ctx->device_ref =
522             av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
523         if (!ctx->device_ref) {
524             err = AVERROR(ENOMEM);
525             goto fail;
526         }
527         ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
528         ctx->hwctx  = ctx->device->hwctx;
529
530         ctx->hwctx->display = ctx->old_context->display;
531
532         // The old VAAPI decode setup assumed this quirk was always
533         // present, so set it here to avoid the behaviour changing.
534         ctx->hwctx->driver_quirks =
535             AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
536
537     }
538 #endif
539
540 #if FF_API_STRUCT_VAAPI_CONTEXT
541     if (ctx->have_old_context) {
542         ctx->va_config  = ctx->old_context->config_id;
543         ctx->va_context = ctx->old_context->context_id;
544
545         av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
546                "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
547     } else {
548 #endif
549
550     err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI);
551     if (err < 0)
552         goto fail;
553
554     ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
555     ctx->hwfc   = ctx->frames->hwctx;
556     ctx->device = ctx->frames->device_ctx;
557     ctx->hwctx  = ctx->device->hwctx;
558
559     err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
560                                    &ctx->va_config, avctx->hw_frames_ctx);
561     if (err)
562         goto fail;
563
564     vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
565                           avctx->coded_width, avctx->coded_height,
566                           VA_PROGRESSIVE,
567                           ctx->hwfc->surface_ids,
568                           ctx->hwfc->nb_surfaces,
569                           &ctx->va_context);
570     if (vas != VA_STATUS_SUCCESS) {
571         av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
572                "context: %d (%s).\n", vas, vaErrorStr(vas));
573         err = AVERROR(EIO);
574         goto fail;
575     }
576
577     av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
578            "%#x/%#x.\n", ctx->va_config, ctx->va_context);
579 #if FF_API_STRUCT_VAAPI_CONTEXT
580     }
581 #endif
582
583     return 0;
584
585 fail:
586     ff_vaapi_decode_uninit(avctx);
587     return err;
588 }
589
590 int ff_vaapi_decode_uninit(AVCodecContext *avctx)
591 {
592     VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
593     VAStatus vas;
594
595 #if FF_API_STRUCT_VAAPI_CONTEXT
596     if (ctx->have_old_context) {
597         av_buffer_unref(&ctx->device_ref);
598     } else {
599 #endif
600
601     if (ctx->va_context != VA_INVALID_ID) {
602         vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
603         if (vas != VA_STATUS_SUCCESS) {
604             av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
605                    "context %#x: %d (%s).\n",
606                    ctx->va_context, vas, vaErrorStr(vas));
607         }
608     }
609     if (ctx->va_config != VA_INVALID_ID) {
610         vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
611         if (vas != VA_STATUS_SUCCESS) {
612             av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
613                    "configuration %#x: %d (%s).\n",
614                    ctx->va_config, vas, vaErrorStr(vas));
615         }
616     }
617
618 #if FF_API_STRUCT_VAAPI_CONTEXT
619     }
620 #endif
621
622     return 0;
623 }