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