vaapi_decode: Ignore the profile when not useful
[libav.git] / 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
22 #include "avcodec.h"
23 #include "internal.h"
24 #include "vaapi_decode.h"
25
26
27 int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx,
28 VAAPIDecodePicture *pic,
29 int type,
30 const void *data,
31 size_t size)
32 {
33 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
34 VAStatus vas;
35 VABufferID buffer;
36
37 av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
38
39 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
40 type, size, 1, (void*)data, &buffer);
41 if (vas != VA_STATUS_SUCCESS) {
42 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
43 "buffer (type %d): %d (%s).\n",
44 type, vas, vaErrorStr(vas));
45 return AVERROR(EIO);
46 }
47
48 pic->param_buffers[pic->nb_param_buffers++] = buffer;
49
50 av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes) "
51 "is %#x.\n", type, size, buffer);
52 return 0;
53 }
54
55
56 int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx,
57 VAAPIDecodePicture *pic,
58 const void *params_data,
59 size_t params_size,
60 const void *slice_data,
61 size_t slice_size)
62 {
63 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
64 VAStatus vas;
65 int index;
66
67 av_assert0(pic->nb_slices <= pic->slices_allocated);
68 if (pic->nb_slices == pic->slices_allocated) {
69 if (pic->slices_allocated > 0)
70 pic->slices_allocated *= 2;
71 else
72 pic->slices_allocated = 64;
73
74 pic->slice_buffers =
75 av_realloc_array(pic->slice_buffers,
76 pic->slices_allocated,
77 2 * sizeof(*pic->slice_buffers));
78 if (!pic->slice_buffers)
79 return AVERROR(ENOMEM);
80 }
81 av_assert0(pic->nb_slices + 1 <= pic->slices_allocated);
82
83 index = 2 * pic->nb_slices;
84
85 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
86 VASliceParameterBufferType,
87 params_size, 1, (void*)params_data,
88 &pic->slice_buffers[index]);
89 if (vas != VA_STATUS_SUCCESS) {
90 av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
91 "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
92 return AVERROR(EIO);
93 }
94
95 av_log(avctx, AV_LOG_DEBUG, "Slice %d param buffer (%zu bytes) "
96 "is %#x.\n", pic->nb_slices, params_size,
97 pic->slice_buffers[index]);
98
99 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
100 VASliceDataBufferType,
101 slice_size, 1, (void*)slice_data,
102 &pic->slice_buffers[index + 1]);
103 if (vas != VA_STATUS_SUCCESS) {
104 av_log(avctx, AV_LOG_ERROR, "Failed to create slice "
105 "data buffer (size %zu): %d (%s).\n",
106 slice_size, vas, vaErrorStr(vas));
107 vaDestroyBuffer(ctx->hwctx->display,
108 pic->slice_buffers[index]);
109 return AVERROR(EIO);
110 }
111
112 av_log(avctx, AV_LOG_DEBUG, "Slice %d data buffer (%zu bytes) "
113 "is %#x.\n", pic->nb_slices, slice_size,
114 pic->slice_buffers[index + 1]);
115
116 ++pic->nb_slices;
117 return 0;
118 }
119
120 static void ff_vaapi_decode_destroy_buffers(AVCodecContext *avctx,
121 VAAPIDecodePicture *pic)
122 {
123 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
124 VAStatus vas;
125 int i;
126
127 for (i = 0; i < pic->nb_param_buffers; i++) {
128 vas = vaDestroyBuffer(ctx->hwctx->display,
129 pic->param_buffers[i]);
130 if (vas != VA_STATUS_SUCCESS) {
131 av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
132 "parameter buffer %#x: %d (%s).\n",
133 pic->param_buffers[i], vas, vaErrorStr(vas));
134 }
135 }
136
137 for (i = 0; i < 2 * pic->nb_slices; i++) {
138 vas = vaDestroyBuffer(ctx->hwctx->display,
139 pic->slice_buffers[i]);
140 if (vas != VA_STATUS_SUCCESS) {
141 av_log(avctx, AV_LOG_ERROR, "Failed to destroy slice "
142 "slice buffer %#x: %d (%s).\n",
143 pic->slice_buffers[i], vas, vaErrorStr(vas));
144 }
145 }
146 }
147
148 int ff_vaapi_decode_issue(AVCodecContext *avctx,
149 VAAPIDecodePicture *pic)
150 {
151 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
152 VAStatus vas;
153 int err, i;
154
155 av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n",
156 pic->output_surface);
157
158 for (i = 0; i < pic->nb_param_buffers; i++)
159 vaUnmapBuffer(ctx->hwctx->display, pic->param_buffers[i]);
160
161 vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
162 pic->output_surface);
163 if (vas != VA_STATUS_SUCCESS) {
164 av_log(avctx, AV_LOG_ERROR, "Failed to begin picture decode "
165 "issue: %d (%s).\n", vas, vaErrorStr(vas));
166 err = AVERROR(EIO);
167 goto fail_with_picture;
168 }
169
170 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
171 pic->param_buffers, pic->nb_param_buffers);
172 if (vas != VA_STATUS_SUCCESS) {
173 av_log(avctx, AV_LOG_ERROR, "Failed to upload decode "
174 "parameters: %d (%s).\n", vas, vaErrorStr(vas));
175 err = AVERROR(EIO);
176 goto fail_with_picture;
177 }
178
179 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
180 pic->slice_buffers, 2 * pic->nb_slices);
181 if (vas != VA_STATUS_SUCCESS) {
182 av_log(avctx, AV_LOG_ERROR, "Failed to upload slices: "
183 "%d (%s).\n", vas, vaErrorStr(vas));
184 err = AVERROR(EIO);
185 goto fail_with_picture;
186 }
187
188 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
189 if (vas != VA_STATUS_SUCCESS) {
190 av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode "
191 "issue: %d (%s).\n", vas, vaErrorStr(vas));
192 err = AVERROR(EIO);
193 if (ctx->hwctx->driver_quirks &
194 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
195 goto fail;
196 else
197 goto fail_at_end;
198 }
199
200 if (ctx->hwctx->driver_quirks &
201 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
202 ff_vaapi_decode_destroy_buffers(avctx, pic);
203
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_BASELINE, H264Baseline),
251 MAP(H264, H264_MAIN, H264Main ),
252 MAP(H264, H264_HIGH, H264High ),
253 #if VA_CHECK_VERSION(0, 37, 0)
254 MAP(HEVC, HEVC_MAIN, HEVCMain ),
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
285 // Allowing a profile mismatch can be useful because streams may
286 // over-declare their required capabilities - in particular, many
287 // H.264 baseline profile streams (notably some of those in FATE)
288 // only use the feature set of constrained baseline. This flag
289 // would have to be be set by some external means in order to
290 // actually be useful. (AV_HWACCEL_FLAG_IGNORE_PROFILE?)
291 int allow_profile_mismatch = 0;
292
293 codec_desc = avcodec_descriptor_get(avctx->codec_id);
294 if (!codec_desc) {
295 err = AVERROR(EINVAL);
296 goto fail;
297 }
298
299 profile_count = vaMaxNumProfiles(ctx->hwctx->display);
300 profile_list = av_malloc_array(profile_count,
301 sizeof(VAProfile));
302 if (!profile_list) {
303 err = AVERROR(ENOMEM);
304 goto fail;
305 }
306
307 vas = vaQueryConfigProfiles(ctx->hwctx->display,
308 profile_list, &profile_count);
309 if (vas != VA_STATUS_SUCCESS) {
310 av_log(ctx, AV_LOG_ERROR, "Failed to query profiles: "
311 "%d (%s).\n", vas, vaErrorStr(vas));
312 err = AVERROR(ENOSYS);
313 goto fail;
314 }
315
316 profile = VAProfileNone;
317 exact_match = 0;
318
319 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
320 int profile_match = 0;
321 if (avctx->codec_id != vaapi_profile_map[i].codec_id)
322 continue;
323 if (avctx->profile == vaapi_profile_map[i].codec_profile ||
324 vaapi_profile_map[i].codec_profile == FF_PROFILE_UNKNOWN)
325 profile_match = 1;
326 profile = vaapi_profile_map[i].va_profile;
327 for (j = 0; j < profile_count; j++) {
328 if (profile == profile_list[j]) {
329 exact_match = profile_match;
330 break;
331 }
332 }
333 if (j < profile_count) {
334 if (exact_match)
335 break;
336 alt_profile = vaapi_profile_map[i].codec_profile;
337 }
338 }
339 av_freep(&profile_list);
340
341 if (profile == VAProfileNone) {
342 av_log(ctx, AV_LOG_ERROR, "No support for codec %s "
343 "profile %d.\n", codec_desc->name, avctx->profile);
344 err = AVERROR(ENOSYS);
345 goto fail;
346 }
347 if (!exact_match) {
348 if (allow_profile_mismatch) {
349 av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
350 "supported for hardware decode.\n",
351 codec_desc->name, avctx->profile);
352 av_log(avctx, AV_LOG_WARNING, "Using possibly-"
353 "incompatible profile %d instead.\n",
354 alt_profile);
355 } else {
356 av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not "
357 "supported for hardware decode.\n",
358 codec_desc->name, avctx->profile);
359 err = AVERROR(EINVAL);
360 goto fail;
361 }
362 }
363
364 ctx->va_profile = profile;
365 ctx->va_entrypoint = VAEntrypointVLD;
366
367 vas = vaCreateConfig(ctx->hwctx->display, ctx->va_profile,
368 ctx->va_entrypoint, NULL, 0,
369 &ctx->va_config);
370 if (vas != VA_STATUS_SUCCESS) {
371 av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
372 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
373 err = AVERROR(EIO);
374 goto fail;
375 }
376
377 hwconfig = av_hwdevice_hwconfig_alloc(ctx->frames->device_ref);
378 if (!hwconfig) {
379 err = AVERROR(ENOMEM);
380 goto fail;
381 }
382 hwconfig->config_id = ctx->va_config;
383
384 constraints =
385 av_hwdevice_get_hwframe_constraints(ctx->frames->device_ref,
386 hwconfig);
387 if (!constraints) {
388 // Ignore.
389 } else {
390 if (avctx->coded_width < constraints->min_width ||
391 avctx->coded_height < constraints->min_height ||
392 avctx->coded_width > constraints->max_width ||
393 avctx->coded_height > constraints->max_height) {
394 av_log(ctx, AV_LOG_ERROR, "Hardware does not support image "
395 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
396 avctx->coded_width, avctx->coded_height,
397 constraints->min_width, constraints->max_width,
398 constraints->min_height, constraints->max_height);
399 err = AVERROR(EINVAL);
400 goto fail;
401 }
402 }
403
404 av_hwframe_constraints_free(&constraints);
405 av_freep(&hwconfig);
406
407 return 0;
408
409 fail:
410 av_hwframe_constraints_free(&constraints);
411 av_freep(&hwconfig);
412 if (ctx->va_config != VA_INVALID_ID) {
413 vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
414 ctx->va_config = VA_INVALID_ID;
415 }
416 av_freep(&profile_list);
417 return err;
418 }
419
420 int ff_vaapi_decode_init(AVCodecContext *avctx)
421 {
422 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
423 VAStatus vas;
424 int err;
425
426 ctx->va_config = VA_INVALID_ID;
427 ctx->va_context = VA_INVALID_ID;
428
429 #if FF_API_VAAPI_CONTEXT
430 if (avctx->hwaccel_context) {
431 av_log(avctx, AV_LOG_WARNING, "Using deprecated struct "
432 "vaapi_context in decode.\n");
433
434 ctx->have_old_context = 1;
435 ctx->old_context = avctx->hwaccel_context;
436
437 // Really we only want the VAAPI device context, but this
438 // allocates a whole generic device context because we don't
439 // have any other way to determine how big it should be.
440 ctx->device_ref =
441 av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
442 if (!ctx->device_ref) {
443 err = AVERROR(ENOMEM);
444 goto fail;
445 }
446 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
447 ctx->hwctx = ctx->device->hwctx;
448
449 ctx->hwctx->display = ctx->old_context->display;
450
451 // The old VAAPI decode setup assumed this quirk was always
452 // present, so set it here to avoid the behaviour changing.
453 ctx->hwctx->driver_quirks =
454 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
455
456 } else
457 #endif
458 if (avctx->hw_frames_ctx) {
459 // This structure has a shorter lifetime than the enclosing
460 // AVCodecContext, so we inherit the references from there
461 // and do not need to make separate ones.
462
463 ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
464 ctx->hwfc = ctx->frames->hwctx;
465
466 ctx->device = ctx->frames->device_ctx;
467 ctx->hwctx = ctx->device->hwctx;
468
469 } else {
470 av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
471 "required for VAAPI decoding.\n");
472 err = AVERROR(EINVAL);
473 goto fail;
474 }
475
476 #if FF_API_VAAPI_CONTEXT
477 if (ctx->have_old_context) {
478 ctx->va_config = ctx->old_context->config_id;
479 ctx->va_context = ctx->old_context->context_id;
480
481 av_log(avctx, AV_LOG_DEBUG, "Using user-supplied decoder "
482 "context: %#x/%#x.\n", ctx->va_config, ctx->va_context);
483 } else {
484 #endif
485
486 err = vaapi_decode_make_config(avctx);
487 if (err)
488 goto fail;
489
490 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
491 avctx->coded_width, avctx->coded_height,
492 VA_PROGRESSIVE,
493 ctx->hwfc->surface_ids,
494 ctx->hwfc->nb_surfaces,
495 &ctx->va_context);
496 if (vas != VA_STATUS_SUCCESS) {
497 av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
498 "context: %d (%s).\n", vas, vaErrorStr(vas));
499 err = AVERROR(EIO);
500 goto fail;
501 }
502
503 av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: "
504 "%#x/%#x.\n", ctx->va_config, ctx->va_context);
505 #if FF_API_VAAPI_CONTEXT
506 }
507 #endif
508
509 return 0;
510
511 fail:
512 ff_vaapi_decode_uninit(avctx);
513 return err;
514 }
515
516 int ff_vaapi_decode_uninit(AVCodecContext *avctx)
517 {
518 VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
519 VAStatus vas;
520
521 #if FF_API_VAAPI_CONTEXT
522 if (ctx->have_old_context) {
523 av_buffer_unref(&ctx->device_ref);
524 } else {
525 #endif
526
527 if (ctx->va_context != VA_INVALID_ID) {
528 vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context);
529 if (vas != VA_STATUS_SUCCESS) {
530 av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
531 "context %#x: %d (%s).\n",
532 ctx->va_context, vas, vaErrorStr(vas));
533 }
534 }
535 if (ctx->va_config != VA_INVALID_ID) {
536 vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
537 if (vas != VA_STATUS_SUCCESS) {
538 av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode "
539 "configuration %#x: %d (%s).\n",
540 ctx->va_config, vas, vaErrorStr(vas));
541 }
542 }
543
544 #if FF_API_VAAPI_CONTEXT
545 }
546 #endif
547
548 return 0;
549 }