libavcodec/vaapi_decode.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 "config_components.h" | ||
| 20 | |||
| 21 | #include "libavutil/avassert.h" | ||
| 22 | #include "libavutil/common.h" | ||
| 23 | #include "libavutil/mem.h" | ||
| 24 | #include "libavutil/pixdesc.h" | ||
| 25 | |||
| 26 | #include "avcodec.h" | ||
| 27 | #include "codec_desc.h" | ||
| 28 | #include "decode.h" | ||
| 29 | #include "internal.h" | ||
| 30 | #include "vaapi_decode.h" | ||
| 31 | #include "vaapi_hevc.h" | ||
| 32 | |||
| 33 | |||
| 34 | ✗ | int ff_vaapi_decode_make_param_buffer(AVCodecContext *avctx, | |
| 35 | VAAPIDecodePicture *pic, | ||
| 36 | int type, | ||
| 37 | const void *data, | ||
| 38 | size_t size) | ||
| 39 | { | ||
| 40 | ✗ | VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 41 | VAStatus vas; | ||
| 42 | |||
| 43 | ✗ | av_assert0(pic->nb_param_buffers <= pic->nb_param_buffers_allocated); | |
| 44 | ✗ | if (pic->nb_param_buffers == pic->nb_param_buffers_allocated) { | |
| 45 | VABufferID *tmp = | ||
| 46 | ✗ | av_realloc_array(pic->param_buffers, | |
| 47 | ✗ | pic->nb_param_buffers_allocated + 16, | |
| 48 | sizeof(*pic->param_buffers)); | ||
| 49 | ✗ | if (!tmp) | |
| 50 | ✗ | return AVERROR(ENOMEM); | |
| 51 | |||
| 52 | ✗ | pic->param_buffers = tmp; | |
| 53 | ✗ | pic->nb_param_buffers_allocated += 16; | |
| 54 | } | ||
| 55 | ✗ | av_assert0(pic->nb_param_buffers + 1 <= pic->nb_param_buffers_allocated); | |
| 56 | |||
| 57 | ✗ | vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, | |
| 58 | type, size, 1, (void*)data, | ||
| 59 | ✗ | &pic->param_buffers[pic->nb_param_buffers]); | |
| 60 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 61 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to create parameter " | |
| 62 | "buffer (type %d): %d (%s).\n", | ||
| 63 | type, vas, vaErrorStr(vas)); | ||
| 64 | ✗ | return AVERROR(EIO); | |
| 65 | } | ||
| 66 | |||
| 67 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes) " | |
| 68 | ✗ | "is %#x.\n", type, size, pic->param_buffers[pic->nb_param_buffers]); | |
| 69 | |||
| 70 | ✗ | ++pic->nb_param_buffers; | |
| 71 | |||
| 72 | ✗ | return 0; | |
| 73 | } | ||
| 74 | |||
| 75 | ✗ | int ff_vaapi_decode_make_slice_buffer(AVCodecContext *avctx, | |
| 76 | VAAPIDecodePicture *pic, | ||
| 77 | const void *params_data, | ||
| 78 | int nb_params, | ||
| 79 | size_t params_size, | ||
| 80 | const void *slice_data, | ||
| 81 | size_t slice_size) | ||
| 82 | { | ||
| 83 | ✗ | VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 84 | VAStatus vas; | ||
| 85 | int index; | ||
| 86 | |||
| 87 | ✗ | av_assert0(pic->nb_slices <= pic->nb_slice_buffers_allocated); | |
| 88 | ✗ | if (pic->nb_slices == pic->nb_slice_buffers_allocated) { | |
| 89 | VABufferID *tmp = | ||
| 90 | ✗ | av_realloc_array(pic->slice_buffers, | |
| 91 | ✗ | pic->nb_slice_buffers_allocated ? pic->nb_slice_buffers_allocated * 2 : 64, | |
| 92 | 2 * sizeof(*pic->slice_buffers)); | ||
| 93 | ✗ | if (!tmp) | |
| 94 | ✗ | return AVERROR(ENOMEM); | |
| 95 | |||
| 96 | ✗ | pic->slice_buffers = tmp; | |
| 97 | ✗ | pic->nb_slice_buffers_allocated = pic->nb_slice_buffers_allocated ? pic->nb_slice_buffers_allocated * 2 : 64; | |
| 98 | } | ||
| 99 | ✗ | av_assert0(pic->nb_slices + 1 <= pic->nb_slice_buffers_allocated); | |
| 100 | |||
| 101 | ✗ | index = 2 * pic->nb_slices; | |
| 102 | |||
| 103 | ✗ | vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, | |
| 104 | VASliceParameterBufferType, | ||
| 105 | params_size, nb_params, (void*)params_data, | ||
| 106 | ✗ | &pic->slice_buffers[index]); | |
| 107 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 108 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to create slice " | |
| 109 | "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas)); | ||
| 110 | ✗ | return AVERROR(EIO); | |
| 111 | } | ||
| 112 | |||
| 113 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Slice %d param buffer (%zu bytes) " | |
| 114 | "is %#x.\n", pic->nb_slices, params_size, | ||
| 115 | ✗ | pic->slice_buffers[index]); | |
| 116 | |||
| 117 | ✗ | vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, | |
| 118 | VASliceDataBufferType, | ||
| 119 | slice_size, 1, (void*)slice_data, | ||
| 120 | ✗ | &pic->slice_buffers[index + 1]); | |
| 121 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 122 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to create slice " | |
| 123 | "data buffer (size %zu): %d (%s).\n", | ||
| 124 | slice_size, vas, vaErrorStr(vas)); | ||
| 125 | ✗ | vaDestroyBuffer(ctx->hwctx->display, | |
| 126 | ✗ | pic->slice_buffers[index]); | |
| 127 | ✗ | return AVERROR(EIO); | |
| 128 | } | ||
| 129 | |||
| 130 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Slice %d data buffer (%zu bytes) " | |
| 131 | "is %#x.\n", pic->nb_slices, slice_size, | ||
| 132 | ✗ | pic->slice_buffers[index + 1]); | |
| 133 | |||
| 134 | ✗ | ++pic->nb_slices; | |
| 135 | ✗ | return 0; | |
| 136 | } | ||
| 137 | |||
| 138 | ✗ | static void ff_vaapi_decode_destroy_buffers(void *logctx, VADisplay display, | |
| 139 | VAAPIDecodePicture *pic) | ||
| 140 | { | ||
| 141 | VAStatus vas; | ||
| 142 | int i; | ||
| 143 | |||
| 144 | ✗ | for (i = 0; i < pic->nb_param_buffers; i++) { | |
| 145 | ✗ | vas = vaDestroyBuffer(display, pic->param_buffers[i]); | |
| 146 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 147 | ✗ | av_log(logctx, AV_LOG_ERROR, "Failed to destroy " | |
| 148 | "parameter buffer %#x: %d (%s).\n", | ||
| 149 | ✗ | pic->param_buffers[i], vas, vaErrorStr(vas)); | |
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | ✗ | for (i = 0; i < 2 * pic->nb_slices; i++) { | |
| 154 | ✗ | vas = vaDestroyBuffer(display, pic->slice_buffers[i]); | |
| 155 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 156 | ✗ | av_log(logctx, AV_LOG_ERROR, "Failed to destroy slice " | |
| 157 | "slice buffer %#x: %d (%s).\n", | ||
| 158 | ✗ | pic->slice_buffers[i], vas, vaErrorStr(vas)); | |
| 159 | } | ||
| 160 | } | ||
| 161 | ✗ | } | |
| 162 | |||
| 163 | ✗ | static void vaapi_decode_free_buffers(void *logctx, VADisplay display, | |
| 164 | VAAPIDecodePicture *pic) | ||
| 165 | { | ||
| 166 | ✗ | ff_vaapi_decode_destroy_buffers(logctx, display, pic); | |
| 167 | |||
| 168 | ✗ | pic->nb_param_buffers = 0; | |
| 169 | ✗ | pic->nb_param_buffers_allocated = 0; | |
| 170 | ✗ | av_freep(&pic->param_buffers); | |
| 171 | ✗ | pic->nb_slices = 0; | |
| 172 | ✗ | pic->nb_slice_buffers_allocated = 0; | |
| 173 | ✗ | av_freep(&pic->slice_buffers); | |
| 174 | ✗ | } | |
| 175 | |||
| 176 | ✗ | int ff_vaapi_decode_issue(AVCodecContext *avctx, | |
| 177 | VAAPIDecodePicture *pic) | ||
| 178 | { | ||
| 179 | ✗ | VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 180 | VAStatus vas; | ||
| 181 | int err; | ||
| 182 | |||
| 183 | ✗ | if (pic->nb_slices <= 0) { | |
| 184 | ✗ | err = AVERROR(EINVAL); | |
| 185 | ✗ | goto fail; | |
| 186 | } | ||
| 187 | |||
| 188 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Decode to surface %#x.\n", | |
| 189 | pic->output_surface); | ||
| 190 | |||
| 191 | ✗ | vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context, | |
| 192 | pic->output_surface); | ||
| 193 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 194 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to begin picture decode " | |
| 195 | "issue: %d (%s).\n", vas, vaErrorStr(vas)); | ||
| 196 | ✗ | err = AVERROR(EIO); | |
| 197 | ✗ | goto fail_with_picture; | |
| 198 | } | ||
| 199 | |||
| 200 | ✗ | vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context, | |
| 201 | pic->param_buffers, pic->nb_param_buffers); | ||
| 202 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 203 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to upload decode " | |
| 204 | "parameters: %d (%s).\n", vas, vaErrorStr(vas)); | ||
| 205 | ✗ | err = AVERROR(EIO); | |
| 206 | ✗ | goto fail_with_picture; | |
| 207 | } | ||
| 208 | |||
| 209 | ✗ | vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context, | |
| 210 | ✗ | pic->slice_buffers, 2 * pic->nb_slices); | |
| 211 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 212 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to upload slices: " | |
| 213 | "%d (%s).\n", vas, vaErrorStr(vas)); | ||
| 214 | ✗ | err = AVERROR(EIO); | |
| 215 | ✗ | goto fail_with_picture; | |
| 216 | } | ||
| 217 | |||
| 218 | ✗ | vas = vaEndPicture(ctx->hwctx->display, ctx->va_context); | |
| 219 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 220 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode " | |
| 221 | "issue: %d (%s).\n", vas, vaErrorStr(vas)); | ||
| 222 | ✗ | err = AVERROR(EIO); | |
| 223 | if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks & | ||
| 224 | AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) | ||
| 225 | ✗ | goto fail; | |
| 226 | else | ||
| 227 | goto fail_at_end; | ||
| 228 | } | ||
| 229 | |||
| 230 | if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks & | ||
| 231 | AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) | ||
| 232 | ✗ | ff_vaapi_decode_destroy_buffers(avctx, ctx->hwctx->display, pic); | |
| 233 | |||
| 234 | ✗ | err = 0; | |
| 235 | ✗ | goto exit; | |
| 236 | |||
| 237 | ✗ | fail_with_picture: | |
| 238 | ✗ | vas = vaEndPicture(ctx->hwctx->display, ctx->va_context); | |
| 239 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 240 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to end picture decode " | |
| 241 | "after error: %d (%s).\n", vas, vaErrorStr(vas)); | ||
| 242 | } | ||
| 243 | ✗ | fail: | |
| 244 | ✗ | ff_vaapi_decode_destroy_buffers(avctx, ctx->hwctx->display, pic); | |
| 245 | ✗ | fail_at_end: | |
| 246 | ✗ | exit: | |
| 247 | ✗ | pic->nb_param_buffers = 0; | |
| 248 | ✗ | pic->nb_param_buffers_allocated = 0; | |
| 249 | ✗ | av_freep(&pic->param_buffers); | |
| 250 | ✗ | pic->nb_slices = 0; | |
| 251 | ✗ | pic->nb_slice_buffers_allocated = 0; | |
| 252 | ✗ | av_freep(&pic->slice_buffers); | |
| 253 | |||
| 254 | ✗ | return err; | |
| 255 | } | ||
| 256 | |||
| 257 | ✗ | int ff_vaapi_decode_cancel(AVCodecContext *avctx, | |
| 258 | VAAPIDecodePicture *pic) | ||
| 259 | { | ||
| 260 | ✗ | VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 261 | |||
| 262 | ✗ | vaapi_decode_free_buffers(avctx, ctx->hwctx->display, pic); | |
| 263 | |||
| 264 | ✗ | return 0; | |
| 265 | } | ||
| 266 | |||
| 267 | ✗ | void ff_vaapi_decode_free_frame_priv(AVRefStructOpaque hwctx, void *data) | |
| 268 | { | ||
| 269 | ✗ | AVHWDeviceContext *device_ctx = hwctx.nc; | |
| 270 | ✗ | AVVAAPIDeviceContext *vactx = device_ctx->hwctx; | |
| 271 | |||
| 272 | ✗ | vaapi_decode_free_buffers(device_ctx, vactx->display, data); | |
| 273 | ✗ | } | |
| 274 | |||
| 275 | static const struct { | ||
| 276 | uint32_t fourcc; | ||
| 277 | enum AVPixelFormat pix_fmt; | ||
| 278 | } vaapi_format_map[] = { | ||
| 279 | #define MAP(va, av) { VA_FOURCC_ ## va, AV_PIX_FMT_ ## av } | ||
| 280 | // 4:0:0 | ||
| 281 | MAP(Y800, GRAY8), | ||
| 282 | // 4:2:0 | ||
| 283 | MAP(NV12, NV12), | ||
| 284 | MAP(YV12, YUV420P), | ||
| 285 | MAP(IYUV, YUV420P), | ||
| 286 | #ifdef VA_FOURCC_I420 | ||
| 287 | MAP(I420, YUV420P), | ||
| 288 | #endif | ||
| 289 | MAP(IMC3, YUV420P), | ||
| 290 | // 4:1:1 | ||
| 291 | MAP(411P, YUV411P), | ||
| 292 | // 4:2:2 | ||
| 293 | MAP(422H, YUV422P), | ||
| 294 | #ifdef VA_FOURCC_YV16 | ||
| 295 | MAP(YV16, YUV422P), | ||
| 296 | #endif | ||
| 297 | MAP(YUY2, YUYV422), | ||
| 298 | #ifdef VA_FOURCC_Y210 | ||
| 299 | MAP(Y210, Y210), | ||
| 300 | #endif | ||
| 301 | #ifdef VA_FOURCC_Y212 | ||
| 302 | MAP(Y212, Y212), | ||
| 303 | #endif | ||
| 304 | // 4:4:0 | ||
| 305 | MAP(422V, YUV440P), | ||
| 306 | // 4:4:4 | ||
| 307 | MAP(444P, YUV444P), | ||
| 308 | #ifdef VA_FOURCC_XYUV | ||
| 309 | MAP(XYUV, VUYX), | ||
| 310 | #endif | ||
| 311 | #ifdef VA_FOURCC_Y410 | ||
| 312 | MAP(Y410, XV30), | ||
| 313 | #endif | ||
| 314 | #ifdef VA_FOURCC_Y412 | ||
| 315 | MAP(Y412, XV36), | ||
| 316 | #endif | ||
| 317 | // 4:2:0 10-bit | ||
| 318 | #ifdef VA_FOURCC_P010 | ||
| 319 | MAP(P010, P010), | ||
| 320 | #endif | ||
| 321 | #ifdef VA_FOURCC_P012 | ||
| 322 | MAP(P012, P012), | ||
| 323 | #endif | ||
| 324 | #ifdef VA_FOURCC_I010 | ||
| 325 | MAP(I010, YUV420P10), | ||
| 326 | #endif | ||
| 327 | #undef MAP | ||
| 328 | }; | ||
| 329 | |||
| 330 | ✗ | static int vaapi_decode_find_best_format(AVCodecContext *avctx, | |
| 331 | AVHWDeviceContext *device, | ||
| 332 | VAConfigID config_id, | ||
| 333 | AVHWFramesContext *frames) | ||
| 334 | { | ||
| 335 | ✗ | AVVAAPIDeviceContext *hwctx = device->hwctx; | |
| 336 | VAStatus vas; | ||
| 337 | VASurfaceAttrib *attr; | ||
| 338 | enum AVPixelFormat source_format, best_format, format; | ||
| 339 | uint32_t best_fourcc, fourcc; | ||
| 340 | int i, j, nb_attr; | ||
| 341 | |||
| 342 | ✗ | source_format = avctx->sw_pix_fmt; | |
| 343 | ✗ | av_assert0(source_format != AV_PIX_FMT_NONE); | |
| 344 | |||
| 345 | ✗ | vas = vaQuerySurfaceAttributes(hwctx->display, config_id, | |
| 346 | NULL, &nb_attr); | ||
| 347 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 348 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: " | |
| 349 | "%d (%s).\n", vas, vaErrorStr(vas)); | ||
| 350 | ✗ | return AVERROR(ENOSYS); | |
| 351 | } | ||
| 352 | |||
| 353 | ✗ | attr = av_malloc_array(nb_attr, sizeof(*attr)); | |
| 354 | ✗ | if (!attr) | |
| 355 | ✗ | return AVERROR(ENOMEM); | |
| 356 | |||
| 357 | ✗ | vas = vaQuerySurfaceAttributes(hwctx->display, config_id, | |
| 358 | attr, &nb_attr); | ||
| 359 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 360 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to query surface attributes: " | |
| 361 | "%d (%s).\n", vas, vaErrorStr(vas)); | ||
| 362 | ✗ | av_freep(&attr); | |
| 363 | ✗ | return AVERROR(ENOSYS); | |
| 364 | } | ||
| 365 | |||
| 366 | ✗ | best_format = AV_PIX_FMT_NONE; | |
| 367 | |||
| 368 | ✗ | for (i = 0; i < nb_attr; i++) { | |
| 369 | ✗ | if (attr[i].type != VASurfaceAttribPixelFormat) | |
| 370 | ✗ | continue; | |
| 371 | |||
| 372 | ✗ | fourcc = attr[i].value.value.i; | |
| 373 | ✗ | for (j = 0; j < FF_ARRAY_ELEMS(vaapi_format_map); j++) { | |
| 374 | ✗ | if (fourcc == vaapi_format_map[j].fourcc) | |
| 375 | ✗ | break; | |
| 376 | } | ||
| 377 | ✗ | if (j >= FF_ARRAY_ELEMS(vaapi_format_map)) { | |
| 378 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Ignoring unknown format %#x.\n", | |
| 379 | fourcc); | ||
| 380 | ✗ | continue; | |
| 381 | } | ||
| 382 | ✗ | format = vaapi_format_map[j].pix_fmt; | |
| 383 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Considering format %#x -> %s.\n", | |
| 384 | fourcc, av_get_pix_fmt_name(format)); | ||
| 385 | |||
| 386 | ✗ | best_format = av_find_best_pix_fmt_of_2(format, best_format, | |
| 387 | source_format, 0, NULL); | ||
| 388 | ✗ | if (format == best_format) | |
| 389 | ✗ | best_fourcc = fourcc; | |
| 390 | } | ||
| 391 | |||
| 392 | ✗ | av_freep(&attr); | |
| 393 | |||
| 394 | ✗ | if (best_format == AV_PIX_FMT_NONE) { | |
| 395 | ✗ | av_log(avctx, AV_LOG_ERROR, "No usable formats for decoding!\n"); | |
| 396 | ✗ | return AVERROR(EINVAL); | |
| 397 | } | ||
| 398 | |||
| 399 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Picked %s (%#x) as best match for %s.\n", | |
| 400 | av_get_pix_fmt_name(best_format), best_fourcc, | ||
| 401 | av_get_pix_fmt_name(source_format)); | ||
| 402 | |||
| 403 | ✗ | frames->sw_format = best_format; | |
| 404 | ✗ | if (avctx->internal->hwaccel_priv_data) { | |
| 405 | ✗ | VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 406 | ✗ | AVVAAPIFramesContext *avfc = frames->hwctx; | |
| 407 | |||
| 408 | ✗ | ctx->pixel_format_attribute = (VASurfaceAttrib) { | |
| 409 | .type = VASurfaceAttribPixelFormat, | ||
| 410 | .flags = VA_SURFACE_ATTRIB_SETTABLE, | ||
| 411 | .value.type = VAGenericValueTypeInteger, | ||
| 412 | .value.value.i = best_fourcc, | ||
| 413 | }; | ||
| 414 | |||
| 415 | ✗ | avfc->attributes = &ctx->pixel_format_attribute; | |
| 416 | ✗ | avfc->nb_attributes = 1; | |
| 417 | } | ||
| 418 | |||
| 419 | ✗ | return 0; | |
| 420 | } | ||
| 421 | |||
| 422 | static const struct { | ||
| 423 | enum AVCodecID codec_id; | ||
| 424 | int codec_profile; | ||
| 425 | VAProfile va_profile; | ||
| 426 | VAProfile (*profile_parser)(AVCodecContext *avctx); | ||
| 427 | } vaapi_profile_map[] = { | ||
| 428 | #define MAP(c, p, v, ...) { AV_CODEC_ID_ ## c, AV_PROFILE_ ## p, VAProfile ## v, __VA_ARGS__ } | ||
| 429 | MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ), | ||
| 430 | MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ), | ||
| 431 | MAP(H263, UNKNOWN, H263Baseline), | ||
| 432 | MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ), | ||
| 433 | MAP(MPEG4, MPEG4_ADVANCED_SIMPLE, | ||
| 434 | MPEG4AdvancedSimple), | ||
| 435 | MAP(MPEG4, MPEG4_MAIN, MPEG4Main ), | ||
| 436 | #if VA_CHECK_VERSION(1, 18, 0) | ||
| 437 | MAP(H264, H264_HIGH_10_INTRA, | ||
| 438 | H264High10 ), | ||
| 439 | MAP(H264, H264_HIGH_10, H264High10 ), | ||
| 440 | #endif | ||
| 441 | MAP(H264, H264_CONSTRAINED_BASELINE, | ||
| 442 | H264ConstrainedBaseline), | ||
| 443 | MAP(H264, H264_MAIN, H264Main ), | ||
| 444 | MAP(H264, H264_HIGH, H264High ), | ||
| 445 | #if VA_CHECK_VERSION(0, 37, 0) | ||
| 446 | MAP(HEVC, HEVC_MAIN, HEVCMain ), | ||
| 447 | MAP(HEVC, HEVC_MAIN_10, HEVCMain10 ), | ||
| 448 | MAP(HEVC, HEVC_MAIN_STILL_PICTURE, | ||
| 449 | HEVCMain ), | ||
| 450 | #endif | ||
| 451 | #if VA_CHECK_VERSION(1, 2, 0) && CONFIG_HEVC_VAAPI_HWACCEL | ||
| 452 | MAP(HEVC, HEVC_REXT, None, | ||
| 453 | ff_vaapi_parse_hevc_rext_scc_profile ), | ||
| 454 | MAP(HEVC, HEVC_SCC, None, | ||
| 455 | ff_vaapi_parse_hevc_rext_scc_profile ), | ||
| 456 | #endif | ||
| 457 | MAP(MJPEG, MJPEG_HUFFMAN_BASELINE_DCT, | ||
| 458 | JPEGBaseline), | ||
| 459 | MAP(WMV3, VC1_SIMPLE, VC1Simple ), | ||
| 460 | MAP(WMV3, VC1_MAIN, VC1Main ), | ||
| 461 | MAP(WMV3, VC1_COMPLEX, VC1Advanced ), | ||
| 462 | MAP(WMV3, VC1_ADVANCED, VC1Advanced ), | ||
| 463 | MAP(VC1, VC1_SIMPLE, VC1Simple ), | ||
| 464 | MAP(VC1, VC1_MAIN, VC1Main ), | ||
| 465 | MAP(VC1, VC1_COMPLEX, VC1Advanced ), | ||
| 466 | MAP(VC1, VC1_ADVANCED, VC1Advanced ), | ||
| 467 | MAP(VP8, UNKNOWN, VP8Version0_3 ), | ||
| 468 | #if VA_CHECK_VERSION(0, 38, 0) | ||
| 469 | MAP(VP9, VP9_0, VP9Profile0 ), | ||
| 470 | #endif | ||
| 471 | #if VA_CHECK_VERSION(0, 39, 0) | ||
| 472 | MAP(VP9, VP9_1, VP9Profile1 ), | ||
| 473 | MAP(VP9, VP9_2, VP9Profile2 ), | ||
| 474 | MAP(VP9, VP9_3, VP9Profile3 ), | ||
| 475 | #endif | ||
| 476 | #if VA_CHECK_VERSION(1, 8, 0) | ||
| 477 | MAP(AV1, AV1_MAIN, AV1Profile0), | ||
| 478 | MAP(AV1, AV1_HIGH, AV1Profile1), | ||
| 479 | #endif | ||
| 480 | #if VA_CHECK_VERSION(1, 22, 0) | ||
| 481 | MAP(H266, VVC_MAIN_10, VVCMain10), | ||
| 482 | #endif | ||
| 483 | |||
| 484 | #undef MAP | ||
| 485 | }; | ||
| 486 | |||
| 487 | /* | ||
| 488 | * Set *va_config and the frames_ref fields from the current codec parameters | ||
| 489 | * in avctx. | ||
| 490 | */ | ||
| 491 | ✗ | static int vaapi_decode_make_config(AVCodecContext *avctx, | |
| 492 | AVBufferRef *device_ref, | ||
| 493 | VAConfigID *va_config, | ||
| 494 | AVBufferRef *frames_ref) | ||
| 495 | { | ||
| 496 | ✗ | AVVAAPIHWConfig *hwconfig = NULL; | |
| 497 | ✗ | AVHWFramesConstraints *constraints = NULL; | |
| 498 | VAStatus vas; | ||
| 499 | int err, i, j; | ||
| 500 | const AVCodecDescriptor *codec_desc; | ||
| 501 | ✗ | VAProfile *profile_list = NULL, matched_va_profile, va_profile; | |
| 502 | int profile_count, exact_match, matched_ff_profile, codec_profile; | ||
| 503 | |||
| 504 | ✗ | AVHWDeviceContext *device = (AVHWDeviceContext*)device_ref->data; | |
| 505 | ✗ | AVVAAPIDeviceContext *hwctx = device->hwctx; | |
| 506 | |||
| 507 | ✗ | codec_desc = avcodec_descriptor_get(avctx->codec_id); | |
| 508 | ✗ | if (!codec_desc) { | |
| 509 | ✗ | err = AVERROR(EINVAL); | |
| 510 | ✗ | goto fail; | |
| 511 | } | ||
| 512 | |||
| 513 | ✗ | profile_count = vaMaxNumProfiles(hwctx->display); | |
| 514 | ✗ | profile_list = av_malloc_array(profile_count, | |
| 515 | sizeof(VAProfile)); | ||
| 516 | ✗ | if (!profile_list) { | |
| 517 | ✗ | err = AVERROR(ENOMEM); | |
| 518 | ✗ | goto fail; | |
| 519 | } | ||
| 520 | |||
| 521 | ✗ | vas = vaQueryConfigProfiles(hwctx->display, | |
| 522 | profile_list, &profile_count); | ||
| 523 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 524 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: " | |
| 525 | "%d (%s).\n", vas, vaErrorStr(vas)); | ||
| 526 | ✗ | err = AVERROR(ENOSYS); | |
| 527 | ✗ | goto fail; | |
| 528 | } | ||
| 529 | |||
| 530 | ✗ | matched_va_profile = VAProfileNone; | |
| 531 | ✗ | exact_match = 0; | |
| 532 | |||
| 533 | ✗ | for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) { | |
| 534 | ✗ | int profile_match = 0; | |
| 535 | ✗ | if (avctx->codec_id != vaapi_profile_map[i].codec_id) | |
| 536 | ✗ | continue; | |
| 537 | ✗ | if (avctx->profile == vaapi_profile_map[i].codec_profile || | |
| 538 | ✗ | vaapi_profile_map[i].codec_profile == AV_PROFILE_UNKNOWN) | |
| 539 | ✗ | profile_match = 1; | |
| 540 | |||
| 541 | ✗ | va_profile = vaapi_profile_map[i].profile_parser ? | |
| 542 | ✗ | vaapi_profile_map[i].profile_parser(avctx) : | |
| 543 | vaapi_profile_map[i].va_profile; | ||
| 544 | ✗ | codec_profile = vaapi_profile_map[i].codec_profile; | |
| 545 | |||
| 546 | ✗ | for (j = 0; j < profile_count; j++) { | |
| 547 | ✗ | if (va_profile == profile_list[j]) { | |
| 548 | ✗ | exact_match = profile_match; | |
| 549 | ✗ | break; | |
| 550 | } | ||
| 551 | } | ||
| 552 | ✗ | if (j < profile_count) { | |
| 553 | ✗ | matched_va_profile = va_profile; | |
| 554 | ✗ | matched_ff_profile = codec_profile; | |
| 555 | ✗ | if (exact_match) | |
| 556 | ✗ | break; | |
| 557 | } | ||
| 558 | } | ||
| 559 | ✗ | av_freep(&profile_list); | |
| 560 | |||
| 561 | ✗ | if (matched_va_profile == VAProfileNone) { | |
| 562 | ✗ | av_log(avctx, AV_LOG_ERROR, "No support for codec %s " | |
| 563 | ✗ | "profile %d.\n", codec_desc->name, avctx->profile); | |
| 564 | ✗ | err = AVERROR(ENOSYS); | |
| 565 | ✗ | goto fail; | |
| 566 | } | ||
| 567 | ✗ | if (!exact_match) { | |
| 568 | ✗ | if (avctx->hwaccel_flags & | |
| 569 | AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH) { | ||
| 570 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not " | |
| 571 | "supported for hardware decode.\n", | ||
| 572 | ✗ | codec_desc->name, avctx->profile); | |
| 573 | ✗ | av_log(avctx, AV_LOG_WARNING, "Using possibly-" | |
| 574 | "incompatible profile %d instead.\n", | ||
| 575 | matched_ff_profile); | ||
| 576 | } else { | ||
| 577 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Codec %s profile %d not " | |
| 578 | "supported for hardware decode.\n", | ||
| 579 | ✗ | codec_desc->name, avctx->profile); | |
| 580 | ✗ | err = AVERROR(EINVAL); | |
| 581 | ✗ | goto fail; | |
| 582 | } | ||
| 583 | } | ||
| 584 | |||
| 585 | ✗ | vas = vaCreateConfig(hwctx->display, matched_va_profile, | |
| 586 | VAEntrypointVLD, NULL, 0, | ||
| 587 | va_config); | ||
| 588 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 589 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to create decode " | |
| 590 | "configuration: %d (%s).\n", vas, vaErrorStr(vas)); | ||
| 591 | ✗ | err = AVERROR(EIO); | |
| 592 | ✗ | goto fail; | |
| 593 | } | ||
| 594 | |||
| 595 | ✗ | hwconfig = av_hwdevice_hwconfig_alloc(device_ref); | |
| 596 | ✗ | if (!hwconfig) { | |
| 597 | ✗ | err = AVERROR(ENOMEM); | |
| 598 | ✗ | goto fail; | |
| 599 | } | ||
| 600 | ✗ | hwconfig->config_id = *va_config; | |
| 601 | |||
| 602 | ✗ | constraints = | |
| 603 | ✗ | av_hwdevice_get_hwframe_constraints(device_ref, hwconfig); | |
| 604 | ✗ | if (!constraints) { | |
| 605 | ✗ | err = AVERROR(ENOMEM); | |
| 606 | ✗ | goto fail; | |
| 607 | } | ||
| 608 | |||
| 609 | ✗ | if (avctx->coded_width < constraints->min_width || | |
| 610 | ✗ | avctx->coded_height < constraints->min_height || | |
| 611 | ✗ | avctx->coded_width > constraints->max_width || | |
| 612 | ✗ | avctx->coded_height > constraints->max_height) { | |
| 613 | ✗ | av_log(avctx, AV_LOG_ERROR, "Hardware does not support image " | |
| 614 | "size %dx%d (constraints: width %d-%d height %d-%d).\n", | ||
| 615 | avctx->coded_width, avctx->coded_height, | ||
| 616 | ✗ | constraints->min_width, constraints->max_width, | |
| 617 | ✗ | constraints->min_height, constraints->max_height); | |
| 618 | ✗ | err = AVERROR(EINVAL); | |
| 619 | ✗ | goto fail; | |
| 620 | } | ||
| 621 | ✗ | if (!constraints->valid_sw_formats || | |
| 622 | ✗ | constraints->valid_sw_formats[0] == AV_PIX_FMT_NONE) { | |
| 623 | ✗ | av_log(avctx, AV_LOG_ERROR, "Hardware does not offer any " | |
| 624 | "usable surface formats.\n"); | ||
| 625 | ✗ | err = AVERROR(EINVAL); | |
| 626 | ✗ | goto fail; | |
| 627 | } | ||
| 628 | |||
| 629 | ✗ | if (frames_ref) { | |
| 630 | ✗ | AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data; | |
| 631 | |||
| 632 | ✗ | frames->format = AV_PIX_FMT_VAAPI; | |
| 633 | ✗ | frames->width = avctx->coded_width; | |
| 634 | ✗ | frames->height = avctx->coded_height; | |
| 635 | |||
| 636 | ✗ | err = vaapi_decode_find_best_format(avctx, device, | |
| 637 | *va_config, frames); | ||
| 638 | ✗ | if (err < 0) | |
| 639 | ✗ | goto fail; | |
| 640 | |||
| 641 | if (CONFIG_VAAPI_1) | ||
| 642 | ✗ | frames->initial_pool_size = 0; | |
| 643 | else { | ||
| 644 | frames->initial_pool_size = 1; | ||
| 645 | // Add per-codec number of surfaces used for storing reference frames. | ||
| 646 | switch (avctx->codec_id) { | ||
| 647 | case AV_CODEC_ID_H264: | ||
| 648 | case AV_CODEC_ID_H266: | ||
| 649 | case AV_CODEC_ID_HEVC: | ||
| 650 | case AV_CODEC_ID_AV1: | ||
| 651 | frames->initial_pool_size += 16; | ||
| 652 | break; | ||
| 653 | case AV_CODEC_ID_VP9: | ||
| 654 | frames->initial_pool_size += 8; | ||
| 655 | break; | ||
| 656 | case AV_CODEC_ID_VP8: | ||
| 657 | frames->initial_pool_size += 3; | ||
| 658 | break; | ||
| 659 | default: | ||
| 660 | frames->initial_pool_size += 2; | ||
| 661 | } | ||
| 662 | } | ||
| 663 | } | ||
| 664 | |||
| 665 | ✗ | av_hwframe_constraints_free(&constraints); | |
| 666 | ✗ | av_freep(&hwconfig); | |
| 667 | |||
| 668 | ✗ | return 0; | |
| 669 | |||
| 670 | ✗ | fail: | |
| 671 | ✗ | av_hwframe_constraints_free(&constraints); | |
| 672 | ✗ | av_freep(&hwconfig); | |
| 673 | ✗ | if (*va_config != VA_INVALID_ID) { | |
| 674 | ✗ | vaDestroyConfig(hwctx->display, *va_config); | |
| 675 | ✗ | *va_config = VA_INVALID_ID; | |
| 676 | } | ||
| 677 | ✗ | av_freep(&profile_list); | |
| 678 | ✗ | return err; | |
| 679 | } | ||
| 680 | |||
| 681 | ✗ | int ff_vaapi_common_frame_params(AVCodecContext *avctx, | |
| 682 | AVBufferRef *hw_frames_ctx) | ||
| 683 | { | ||
| 684 | ✗ | AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data; | |
| 685 | ✗ | AVHWDeviceContext *device_ctx = hw_frames->device_ctx; | |
| 686 | AVVAAPIDeviceContext *hwctx; | ||
| 687 | ✗ | VAConfigID va_config = VA_INVALID_ID; | |
| 688 | int err; | ||
| 689 | |||
| 690 | ✗ | if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI) | |
| 691 | ✗ | return AVERROR(EINVAL); | |
| 692 | ✗ | hwctx = device_ctx->hwctx; | |
| 693 | |||
| 694 | ✗ | err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config, | |
| 695 | hw_frames_ctx); | ||
| 696 | ✗ | if (err) | |
| 697 | ✗ | return err; | |
| 698 | |||
| 699 | ✗ | if (va_config != VA_INVALID_ID) | |
| 700 | ✗ | vaDestroyConfig(hwctx->display, va_config); | |
| 701 | |||
| 702 | ✗ | return 0; | |
| 703 | } | ||
| 704 | |||
| 705 | ✗ | int ff_vaapi_decode_init(AVCodecContext *avctx) | |
| 706 | { | ||
| 707 | ✗ | VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 708 | VAStatus vas; | ||
| 709 | int err; | ||
| 710 | |||
| 711 | ✗ | ctx->va_config = VA_INVALID_ID; | |
| 712 | ✗ | ctx->va_context = VA_INVALID_ID; | |
| 713 | |||
| 714 | ✗ | err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI); | |
| 715 | ✗ | if (err < 0) | |
| 716 | ✗ | goto fail; | |
| 717 | |||
| 718 | ✗ | ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data; | |
| 719 | ✗ | ctx->hwfc = ctx->frames->hwctx; | |
| 720 | ✗ | ctx->device = ctx->frames->device_ctx; | |
| 721 | ✗ | ctx->hwctx = ctx->device->hwctx; | |
| 722 | |||
| 723 | ✗ | err = vaapi_decode_make_config(avctx, ctx->frames->device_ref, | |
| 724 | &ctx->va_config, NULL); | ||
| 725 | ✗ | if (err) | |
| 726 | ✗ | goto fail; | |
| 727 | |||
| 728 | ✗ | vas = vaCreateContext(ctx->hwctx->display, ctx->va_config, | |
| 729 | avctx->coded_width, avctx->coded_height, | ||
| 730 | VA_PROGRESSIVE, | ||
| 731 | ✗ | ctx->hwfc->surface_ids, | |
| 732 | ✗ | ctx->hwfc->nb_surfaces, | |
| 733 | &ctx->va_context); | ||
| 734 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 735 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to create decode " | |
| 736 | "context: %d (%s).\n", vas, vaErrorStr(vas)); | ||
| 737 | ✗ | err = AVERROR(EIO); | |
| 738 | ✗ | goto fail; | |
| 739 | } | ||
| 740 | |||
| 741 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Decode context initialised: " | |
| 742 | "%#x/%#x.\n", ctx->va_config, ctx->va_context); | ||
| 743 | |||
| 744 | ✗ | return 0; | |
| 745 | |||
| 746 | ✗ | fail: | |
| 747 | ✗ | ff_vaapi_decode_uninit(avctx); | |
| 748 | ✗ | return err; | |
| 749 | } | ||
| 750 | |||
| 751 | ✗ | int ff_vaapi_decode_uninit(AVCodecContext *avctx) | |
| 752 | { | ||
| 753 | ✗ | VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 754 | VAStatus vas; | ||
| 755 | |||
| 756 | ✗ | if (ctx->va_context != VA_INVALID_ID) { | |
| 757 | ✗ | vas = vaDestroyContext(ctx->hwctx->display, ctx->va_context); | |
| 758 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 759 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode " | |
| 760 | "context %#x: %d (%s).\n", | ||
| 761 | ctx->va_context, vas, vaErrorStr(vas)); | ||
| 762 | } | ||
| 763 | } | ||
| 764 | ✗ | if (ctx->va_config != VA_INVALID_ID) { | |
| 765 | ✗ | vas = vaDestroyConfig(ctx->hwctx->display, ctx->va_config); | |
| 766 | ✗ | if (vas != VA_STATUS_SUCCESS) { | |
| 767 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to destroy decode " | |
| 768 | "configuration %#x: %d (%s).\n", | ||
| 769 | ctx->va_config, vas, vaErrorStr(vas)); | ||
| 770 | } | ||
| 771 | } | ||
| 772 | |||
| 773 | ✗ | return 0; | |
| 774 | } | ||
| 775 |