]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_decode.c
avcodec: add Dolby E decoder
[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 "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 #if VA_CHECK_VERSION(0, 39, 0)
271     MAP(VP9,         VP9_2,           VP9Profile2 ),
272 #endif
273 #undef MAP
274 };
275
276 static int vaapi_decode_make_config(AVCodecContext *avctx)
277 {
278     VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
279
280     AVVAAPIHWConfig       *hwconfig    = NULL;
281     AVHWFramesConstraints *constraints = NULL;
282     VAStatus vas;
283     int err, i, j;
284     const AVCodecDescriptor *codec_desc;
285     VAProfile profile, *profile_list = NULL;
286     int profile_count, exact_match, alt_profile;
287     const AVPixFmtDescriptor *sw_desc, *desc;
288
289     codec_desc = avcodec_descriptor_get(avctx->codec_id);
290     if (!codec_desc) {
291         err = AVERROR(EINVAL);
292         goto fail;
293     }
294
295     profile_count = vaMaxNumProfiles(ctx->hwctx->display);
296     profile_list  = av_malloc_array(profile_count,
297                                     sizeof(VAProfile));
298     if (!profile_list) {
299         err = AVERROR(ENOMEM);
300         goto fail;
301     }
302
303     vas = vaQueryConfigProfiles(ctx->hwctx->display,
304                                 profile_list, &profile_count);
305     if (vas != VA_STATUS_SUCCESS) {
306         av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
307                "%d (%s).\n", vas, vaErrorStr(vas));
308         err = AVERROR(ENOSYS);
309         goto fail;
310     }
311
312     profile = VAProfileNone;
313     exact_match = 0;
314
315     for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
316         int profile_match = 0;
317         if (avctx->codec_id != vaapi_profile_map[i].codec_id)
318             continue;
319         if (avctx->profile == vaapi_profile_map[i].codec_profile)
320             profile_match = 1;
321         profile = vaapi_profile_map[i].va_profile;
322         for (j = 0; j < profile_count; j++) {
323             if (profile == profile_list[j]) {
324                 exact_match = profile_match;
325                 break;
326             }
327         }
328         if (j < profile_count) {
329             if (exact_match)
330                 break;
331             alt_profile = vaapi_profile_map[i].codec_profile;
332         }
333     }
334     av_freep(&profile_list);
335
336     if (profile == VAProfileNone) {
337         av_log(avctx, AV_LOG_ERROR, "No support for codec %s "
338                "profile %d.\n", codec_desc->name, avctx->profile);
339         err = AVERROR(ENOSYS);
340         goto fail;
341     }
342     if (!exact_match) {
343         if (avctx->hwaccel_flags &
344             AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) {
345             av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
346                    "supported for hardware decode.\n",
347                    codec_desc->name, avctx->profile);
348             av_log(avctx, AV_LOG_WARNING, "Using possibly-"
349                    "incompatible profile %d instead.\n",
350                    alt_profile);
351         } else {
352             av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
353                    "supported for hardware decode.\n",
354                    codec_desc->name, avctx->profile);
355             err = AVERROR(EINVAL);
356             goto fail;
357         }
358     }
359
360     ctx->va_profile    = profile;
361     ctx->va_entrypoint = VAEntrypointVLD;
362
363     vas = vaCreateConfig(ctx->hwctx->display, ctx->va_profile,
364                          ctx->va_entrypoint, NULL, 0,
365                          &ctx->va_config);
366     if (vas != VA_STATUS_SUCCESS) {
367         av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
368                "configuration: %d (%s).\n", vas, vaErrorStr(vas));
369         err = AVERROR(EIO);
370         goto fail;
371     }
372
373     hwconfig = av_hwdevice_hwconfig_alloc(avctx->hw_device_ctx ?
374                                           avctx->hw_device_ctx :
375                                           ctx->frames->device_ref);
376     if (!hwconfig) {
377         err = AVERROR(ENOMEM);
378         goto fail;
379     }
380     hwconfig->config_id = ctx->va_config;
381
382     constraints =
383         av_hwdevice_get_hwframe_constraints(avctx->hw_device_ctx ?
384                                             avctx->hw_device_ctx :
385                                             ctx->frames->device_ref,
386                                             hwconfig);
387     if (!constraints) {
388         err = AVERROR(ENOMEM);
389         goto fail;
390     }
391
392     if (avctx->coded_width  < constraints->min_width  ||
393         avctx->coded_height < constraints->min_height ||
394         avctx->coded_width  > constraints->max_width  ||
395         avctx->coded_height > constraints->max_height) {
396         av_log(avctx, AV_LOG_ERROR, "Hardware does not support image "
397                "size %dx%d (constraints: width %d-%d height %d-%d).\n",
398                avctx->coded_width, avctx->coded_height,
399                constraints->min_width,  constraints->max_width,
400                constraints->min_height, constraints->max_height);
401         err = AVERROR(EINVAL);
402         goto fail;
403     }
404     if (!constraints->valid_sw_formats ||
405         constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) {
406         av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any "
407                "usable surface formats.\n");
408         err = AVERROR(EINVAL);
409         goto fail;
410     }
411
412     // Find the first format in the list which matches the expected
413     // bit depth and subsampling.  If none are found (this can happen
414     // when 10-bit streams are decoded to 8-bit surfaces, for example)
415     // then just take the first format on the list.
416     ctx->surface_format = constraints->valid_sw_formats[0];
417     sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
418     for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
419         desc = av_pix_fmt_desc_get(constraints->valid_sw_formats[i]);
420         if (desc->nb_components != sw_desc->nb_components ||
421             desc->log2_chroma_w != sw_desc->log2_chroma_w ||
422             desc->log2_chroma_h != sw_desc->log2_chroma_h)
423             continue;
424         for (j = 0; j < desc->nb_components; j++) {
425             if (desc->comp[j].depth != sw_desc->comp[j].depth)
426                 break;
427         }
428         if (j < desc->nb_components)
429             continue;
430         ctx->surface_format = constraints->valid_sw_formats[i];
431         break;
432     }
433
434     // Start with at least four surfaces.
435     ctx->surface_count = 4;
436     // Add per-codec number of surfaces used for storing reference frames.
437     switch (avctx->codec_id) {
438     case AV_CODEC_ID_H264:
439     case AV_CODEC_ID_HEVC:
440         ctx->surface_count += 16;
441         break;
442     case AV_CODEC_ID_VP9:
443         ctx->surface_count += 8;
444         break;
445     case AV_CODEC_ID_VP8:
446         ctx->surface_count += 3;
447         break;
448     default:
449         ctx->surface_count += 2;
450     }
451     // Add an additional surface per thread is frame threading is enabled.
452     if (avctx->active_thread_type & FF_THREAD_FRAME)
453         ctx->surface_count += avctx->thread_count;
454
455     av_hwframe_constraints_free(&constraints);
456     av_freep(&hwconfig);
457
458     return 0;
459
460 fail:
461     av_hwframe_constraints_free(&constraints);
462     av_freep(&hwconfig);
463     if (ctx->va_config != VA_INVALID_ID) {
464         vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
465         ctx->va_config = VA_INVALID_ID;
466     }
467     av_freep(&profile_list);
468     return err;
469 }
470
471 int ff_vaapi_decode_init(AVCodecContext *avctx)
472 {
473     VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
474     VAStatus vas;
475     int err;
476
477     ctx->va_config  = VA_INVALID_ID;
478     ctx->va_context = VA_INVALID_ID;
479
480 #if FF_API_STRUCT_VAAPI_CONTEXT
481     if (avctx->hwaccel_context) {
482         av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
483                "vaapi_context in decode.\n");
484
485         ctx->have_old_context = 1;
486         ctx->old_context = avctx->hwaccel_context;
487
488         // Really we only want the VAAPI device context, but this
489         // allocates a whole generic device context because we don't
490         // have any other way to determine how big it should be.
491         ctx->device_ref =
492             av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
493         if (!ctx->device_ref) {
494             err = AVERROR(ENOMEM);
495             goto fail;
496         }
497         ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
498         ctx->hwctx  = ctx->device->hwctx;
499
500         ctx->hwctx->display = ctx->old_context->display;
501
502         // The old VAAPI decode setup assumed this quirk was always
503         // present, so set it here to avoid the behaviour changing.
504         ctx->hwctx->driver_quirks =
505             AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
506
507     } else
508 #endif
509     if (avctx->hw_frames_ctx) {
510         // This structure has a shorter lifetime than the enclosing
511         // AVCodecContext, so we inherit the references from there
512         // and do not need to make separate ones.
513
514         ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
515         ctx->hwfc   = ctx->frames->hwctx;
516         ctx->device = ctx->frames->device_ctx;
517         ctx->hwctx  = ctx->device->hwctx;
518
519     } else if (avctx->hw_device_ctx) {
520         ctx->device = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
521         ctx->hwctx  = ctx->device->hwctx;
522
523         if (ctx->device->type != AV_HWDEVICE_TYPE_VAAPI) {
524             av_log(avctx, AV_LOG_ERROR, "Device supplied for VAAPI "
525                    "decoding must be a VAAPI device (not %d).\n",
526                    ctx->device->type);
527             err = AVERROR(EINVAL);
528             goto fail;
529         }
530
531     } else {
532         av_log(avctx, AV_LOG_ERROR, "A hardware device or frames context "
533                "is required for VAAPI decoding.\n");
534         err = AVERROR(EINVAL);
535         goto fail;
536     }
537
538 #if FF_API_STRUCT_VAAPI_CONTEXT
539     if (ctx->have_old_context) {
540         ctx->va_config  = ctx->old_context->config_id;
541         ctx->va_context = ctx->old_context->context_id;
542
543         av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
544                "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
545     } else {
546 #endif
547
548     err = vaapi_decode_make_config(avctx);
549     if (err)
550         goto fail;
551
552     if (!avctx->hw_frames_ctx) {
553         avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
554         if (!avctx->hw_frames_ctx) {
555             err = AVERROR(ENOMEM);
556             goto fail;
557         }
558         ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
559
560         ctx->frames->format = AV_PIX_FMT_VAAPI;
561         ctx->frames->width  = avctx->coded_width;
562         ctx->frames->height = avctx->coded_height;
563
564         ctx->frames->sw_format         = ctx->surface_format;
565         ctx->frames->initial_pool_size = ctx->surface_count;
566
567         err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
568         if (err < 0) {
569             av_log(avctx, AV_LOG_ERROR, "Failed to initialise internal "
570                    "frames context: %d.\n", err);
571             goto fail;
572         }
573
574         ctx->hwfc = ctx->frames->hwctx;
575     }
576
577     vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
578                           avctx->coded_width, avctx->coded_height,
579                           VA_PROGRESSIVE,
580                           ctx->hwfc->surface_ids,
581                           ctx->hwfc->nb_surfaces,
582                           &ctx->va_context);
583     if (vas != VA_STATUS_SUCCESS) {
584         av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
585                "context: %d (%s).\n", vas, vaErrorStr(vas));
586         err = AVERROR(EIO);
587         goto fail;
588     }
589
590     av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
591            "%#x/%#x.\n", ctx->va_config, ctx->va_context);
592 #if FF_API_STRUCT_VAAPI_CONTEXT
593     }
594 #endif
595
596     return 0;
597
598 fail:
599     ff_vaapi_decode_uninit(avctx);
600     return err;
601 }
602
603 int ff_vaapi_decode_uninit(AVCodecContext *avctx)
604 {
605     VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
606     VAStatus vas;
607
608 #if FF_API_STRUCT_VAAPI_CONTEXT
609     if (ctx->have_old_context) {
610         av_buffer_unref(&ctx->device_ref);
611     } else {
612 #endif
613
614     if (ctx->va_context != VA_INVALID_ID) {
615         vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
616         if (vas != VA_STATUS_SUCCESS) {
617             av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
618                    "context %#x: %d (%s).\n",
619                    ctx->va_context, vas, vaErrorStr(vas));
620         }
621     }
622     if (ctx->va_config != VA_INVALID_ID) {
623         vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
624         if (vas != VA_STATUS_SUCCESS) {
625             av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
626                    "configuration %#x: %d (%s).\n",
627                    ctx->va_config, vas, vaErrorStr(vas));
628         }
629     }
630
631 #if FF_API_STRUCT_VAAPI_CONTEXT
632     }
633 #endif
634
635     return 0;
636 }