+AVPixelFormat get_vaapi_hw_format(AVCodecContext *ctx, const AVPixelFormat *fmt)
+{
+ for (const AVPixelFormat *fmt_ptr = fmt; *fmt_ptr != -1; ++fmt_ptr) {
+ for (int i = 0;; ++i) { // Termination condition inside loop.
+ const AVCodecHWConfig *config = avcodec_get_hw_config(ctx->codec, i);
+ if (config == nullptr) { // End of list.
+ fprintf(stderr, "Decoder %s does not support device.\n", ctx->codec->name);
+ break;
+ }
+ if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
+ config->device_type == AV_HWDEVICE_TYPE_VAAPI &&
+ config->pix_fmt == *fmt_ptr) {
+ return config->pix_fmt;
+ }
+ }
+ }
+
+ // We found no VA-API formats, so take the best software format.
+ return fmt[0];
+}
+