Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1 | /* |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 2 | * H.264/HEVC/AV1 hardware encoding using nvidia nvenc |
Timo Rothenpieler | cac2df2 | 2016-08-28 17:51:22 | [diff] [blame] | 3 | * Copyright (c) 2016 Timo Rothenpieler <[email protected]> |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 4 | * |
| 5 | * This file is part of FFmpeg. |
| 6 | * |
| 7 | * FFmpeg is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * FFmpeg is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with FFmpeg; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Andrey Turkin | 7aa16d5 | 2016-05-25 13:04:28 | [diff] [blame] | 22 | #include "config.h" |
Martin Storsjö | a78f136 | 2022-02-23 12:56:49 | [diff] [blame] | 23 | #include "config_components.h" |
Andrey Turkin | 7aa16d5 | 2016-05-25 13:04:28 | [diff] [blame] | 24 | |
Timo Rothenpieler | a66835b | 2016-10-10 10:55:59 | [diff] [blame] | 25 | #include "nvenc.h" |
Limin Wang | fdead2a | 2020-06-28 02:34:12 | [diff] [blame] | 26 | #include "hevc_sei.h" |
Timo Rothenpieler | 6362b8c | 2022-11-05 20:18:36 | [diff] [blame] | 27 | #if CONFIG_AV1_NVENC_ENCODER |
| 28 | #include "av1.h" |
| 29 | #endif |
Timo Rothenpieler | 19e75fd | 2016-08-28 15:54:29 | [diff] [blame] | 30 | |
Timo Rothenpieler | a66835b | 2016-10-10 10:55:59 | [diff] [blame] | 31 | #include "libavutil/hwcontext_cuda.h" |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 32 | #include "libavutil/hwcontext.h" |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 33 | #include "libavutil/cuda_check.h" |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 34 | #include "libavutil/imgutils.h" |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 35 | #include "libavutil/mem.h" |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 36 | #include "libavutil/pixdesc.h" |
Timo Rothenpieler | e7fbdda | 2022-11-10 12:05:59 | [diff] [blame] | 37 | #include "libavutil/mathematics.h" |
James Almer | 0de01da | 2020-08-09 17:01:16 | [diff] [blame] | 38 | #include "atsc_a53.h" |
James Almer | 827d6fe | 2020-06-09 21:31:32 | [diff] [blame] | 39 | #include "encode.h" |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 40 | #include "internal.h" |
James Almer | 6e19039 | 2020-06-02 21:38:33 | [diff] [blame] | 41 | #include "packet_internal.h" |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 42 | |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 43 | #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x) |
| 44 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 45 | #define NVENC_CAP 0x30 |
Timo Rothenpieler | d2b46c1 | 2023-06-01 21:46:46 | [diff] [blame] | 46 | |
| 47 | #ifndef NVENC_NO_DEPRECATED_RC |
Timo Rothenpieler | cfbebe9 | 2017-05-09 11:57:39 | [diff] [blame] | 48 | #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \ |
| 49 | rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \ |
| 50 | rc == NV_ENC_PARAMS_RC_CBR_HQ) |
Timo Rothenpieler | d2b46c1 | 2023-06-01 21:46:46 | [diff] [blame] | 51 | #else |
| 52 | #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR) |
| 53 | #endif |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 54 | |
Andrey Turkin | 7aa16d5 | 2016-05-25 13:04:28 | [diff] [blame] | 55 | const enum AVPixelFormat ff_nvenc_pix_fmts[] = { |
| 56 | AV_PIX_FMT_YUV420P, |
| 57 | AV_PIX_FMT_NV12, |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 58 | AV_PIX_FMT_P010, |
Andrey Turkin | 7aa16d5 | 2016-05-25 13:04:28 | [diff] [blame] | 59 | AV_PIX_FMT_YUV444P, |
Philip Langdale | 6a89cdc | 2018-02-25 17:08:06 | [diff] [blame] | 60 | AV_PIX_FMT_P016, // Truncated to 10bits |
| 61 | AV_PIX_FMT_YUV444P16, // Truncated to 10bits |
Sven C. Dack | 4aeb7a8 | 2016-09-07 13:35:14 | [diff] [blame] | 62 | AV_PIX_FMT_0RGB32, |
Timo Rothenpieler | 85bc6b5 | 2022-07-04 23:58:10 | [diff] [blame] | 63 | AV_PIX_FMT_RGB32, |
Sven C. Dack | 4aeb7a8 | 2016-09-07 13:35:14 | [diff] [blame] | 64 | AV_PIX_FMT_0BGR32, |
Timo Rothenpieler | 85bc6b5 | 2022-07-04 23:58:10 | [diff] [blame] | 65 | AV_PIX_FMT_BGR32, |
| 66 | AV_PIX_FMT_X2RGB10, |
| 67 | AV_PIX_FMT_X2BGR10, |
Timo Rothenpieler | 7555d6f | 2021-03-30 20:08:49 | [diff] [blame] | 68 | AV_PIX_FMT_GBRP, |
| 69 | AV_PIX_FMT_GBRP16, // Truncated to 10bits |
Andrey Turkin | 7aa16d5 | 2016-05-25 13:04:28 | [diff] [blame] | 70 | AV_PIX_FMT_CUDA, |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 71 | #if CONFIG_D3D11VA |
| 72 | AV_PIX_FMT_D3D11, |
| 73 | #endif |
Andrey Turkin | 7aa16d5 | 2016-05-25 13:04:28 | [diff] [blame] | 74 | AV_PIX_FMT_NONE |
| 75 | }; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 76 | |
Mark Thompson | cd32279 | 2020-11-08 18:51:15 | [diff] [blame] | 77 | const AVCodecHWConfigInternal *const ff_nvenc_hw_configs[] = { |
Mark Thompson | 1dff97b | 2020-04-13 15:33:18 | [diff] [blame] | 78 | HW_CONFIG_ENCODER_FRAMES(CUDA, CUDA), |
| 79 | HW_CONFIG_ENCODER_DEVICE(NONE, CUDA), |
| 80 | #if CONFIG_D3D11VA |
| 81 | HW_CONFIG_ENCODER_FRAMES(D3D11, D3D11VA), |
| 82 | HW_CONFIG_ENCODER_DEVICE(NONE, D3D11VA), |
| 83 | #endif |
| 84 | NULL, |
| 85 | }; |
| 86 | |
Timo Rothenpieler | 7555d6f | 2021-03-30 20:08:49 | [diff] [blame] | 87 | #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \ |
| 88 | pix_fmt == AV_PIX_FMT_P016 || \ |
| 89 | pix_fmt == AV_PIX_FMT_YUV444P16 || \ |
Timo Rothenpieler | c7a6012 | 2022-11-14 22:10:20 | [diff] [blame] | 90 | pix_fmt == AV_PIX_FMT_X2RGB10 || \ |
| 91 | pix_fmt == AV_PIX_FMT_X2BGR10 || \ |
Timo Rothenpieler | 7555d6f | 2021-03-30 20:08:49 | [diff] [blame] | 92 | pix_fmt == AV_PIX_FMT_GBRP16) |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 93 | |
Timo Rothenpieler | 7555d6f | 2021-03-30 20:08:49 | [diff] [blame] | 94 | #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \ |
| 95 | pix_fmt == AV_PIX_FMT_YUV444P16 || \ |
| 96 | pix_fmt == AV_PIX_FMT_GBRP || \ |
| 97 | pix_fmt == AV_PIX_FMT_GBRP16) |
| 98 | |
| 99 | #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \ |
| 100 | pix_fmt == AV_PIX_FMT_GBRP16) |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 101 | |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 102 | static const struct { |
| 103 | NVENCSTATUS nverr; |
| 104 | int averr; |
| 105 | const char *desc; |
| 106 | } nvenc_errors[] = { |
| 107 | { NV_ENC_SUCCESS, 0, "success" }, |
| 108 | { NV_ENC_ERR_NO_ENCODE_DEVICE, AVERROR(ENOENT), "no encode device" }, |
| 109 | { NV_ENC_ERR_UNSUPPORTED_DEVICE, AVERROR(ENOSYS), "unsupported device" }, |
| 110 | { NV_ENC_ERR_INVALID_ENCODERDEVICE, AVERROR(EINVAL), "invalid encoder device" }, |
| 111 | { NV_ENC_ERR_INVALID_DEVICE, AVERROR(EINVAL), "invalid device" }, |
| 112 | { NV_ENC_ERR_DEVICE_NOT_EXIST, AVERROR(EIO), "device does not exist" }, |
| 113 | { NV_ENC_ERR_INVALID_PTR, AVERROR(EFAULT), "invalid ptr" }, |
| 114 | { NV_ENC_ERR_INVALID_EVENT, AVERROR(EINVAL), "invalid event" }, |
| 115 | { NV_ENC_ERR_INVALID_PARAM, AVERROR(EINVAL), "invalid param" }, |
| 116 | { NV_ENC_ERR_INVALID_CALL, AVERROR(EINVAL), "invalid call" }, |
| 117 | { NV_ENC_ERR_OUT_OF_MEMORY, AVERROR(ENOMEM), "out of memory" }, |
| 118 | { NV_ENC_ERR_ENCODER_NOT_INITIALIZED, AVERROR(EINVAL), "encoder not initialized" }, |
| 119 | { NV_ENC_ERR_UNSUPPORTED_PARAM, AVERROR(ENOSYS), "unsupported param" }, |
| 120 | { NV_ENC_ERR_LOCK_BUSY, AVERROR(EAGAIN), "lock busy" }, |
James Almer | dc48248 | 2016-09-24 18:31:00 | [diff] [blame] | 121 | { NV_ENC_ERR_NOT_ENOUGH_BUFFER, AVERROR_BUFFER_TOO_SMALL, "not enough buffer"}, |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 122 | { NV_ENC_ERR_INVALID_VERSION, AVERROR(EINVAL), "invalid version" }, |
| 123 | { NV_ENC_ERR_MAP_FAILED, AVERROR(EIO), "map failed" }, |
| 124 | { NV_ENC_ERR_NEED_MORE_INPUT, AVERROR(EAGAIN), "need more input" }, |
| 125 | { NV_ENC_ERR_ENCODER_BUSY, AVERROR(EAGAIN), "encoder busy" }, |
| 126 | { NV_ENC_ERR_EVENT_NOT_REGISTERD, AVERROR(EBADF), "event not registered" }, |
| 127 | { NV_ENC_ERR_GENERIC, AVERROR_UNKNOWN, "generic error" }, |
| 128 | { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, AVERROR(EINVAL), "incompatible client key" }, |
| 129 | { NV_ENC_ERR_UNIMPLEMENTED, AVERROR(ENOSYS), "unimplemented" }, |
| 130 | { NV_ENC_ERR_RESOURCE_REGISTER_FAILED, AVERROR(EIO), "resource register failed" }, |
| 131 | { NV_ENC_ERR_RESOURCE_NOT_REGISTERED, AVERROR(EBADF), "resource not registered" }, |
| 132 | { NV_ENC_ERR_RESOURCE_NOT_MAPPED, AVERROR(EBADF), "resource not mapped" }, |
| 133 | }; |
| 134 | |
| 135 | static int nvenc_map_error(NVENCSTATUS err, const char **desc) |
| 136 | { |
| 137 | int i; |
| 138 | for (i = 0; i < FF_ARRAY_ELEMS(nvenc_errors); i++) { |
| 139 | if (nvenc_errors[i].nverr == err) { |
| 140 | if (desc) |
| 141 | *desc = nvenc_errors[i].desc; |
| 142 | return nvenc_errors[i].averr; |
| 143 | } |
| 144 | } |
| 145 | if (desc) |
| 146 | *desc = "unknown error"; |
| 147 | return AVERROR_UNKNOWN; |
| 148 | } |
| 149 | |
Timo Rothenpieler | ab0ef1a | 2019-09-27 17:09:11 | [diff] [blame] | 150 | static int nvenc_print_error(AVCodecContext *avctx, NVENCSTATUS err, |
Clément Bœsch | b7cc4eb | 2017-03-20 22:04:28 | [diff] [blame] | 151 | const char *error_string) |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 152 | { |
| 153 | const char *desc; |
Timo Rothenpieler | ab0ef1a | 2019-09-27 17:09:11 | [diff] [blame] | 154 | const char *details = "(no details)"; |
| 155 | int ret = nvenc_map_error(err, &desc); |
| 156 | |
| 157 | #ifdef NVENC_HAVE_GETLASTERRORSTRING |
| 158 | NvencContext *ctx = avctx->priv_data; |
| 159 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs; |
| 160 | |
| 161 | if (p_nvenc && ctx->nvencoder) |
| 162 | details = p_nvenc->nvEncGetLastErrorString(ctx->nvencoder); |
| 163 | #endif |
| 164 | |
| 165 | av_log(avctx, AV_LOG_ERROR, "%s: %s (%d): %s\n", error_string, desc, err, details); |
| 166 | |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
Timo Rothenpieler | 988f2e9 | 2021-04-18 09:17:54 | [diff] [blame] | 170 | typedef struct GUIDTuple { |
| 171 | const GUID guid; |
| 172 | int flags; |
| 173 | } GUIDTuple; |
| 174 | |
| 175 | #define PRESET_ALIAS(alias, name, ...) \ |
| 176 | [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ } |
| 177 | |
| 178 | #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__) |
| 179 | |
| 180 | static void nvenc_map_preset(NvencContext *ctx) |
| 181 | { |
| 182 | GUIDTuple presets[] = { |
| 183 | #ifdef NVENC_HAVE_NEW_PRESETS |
| 184 | PRESET(P1), |
| 185 | PRESET(P2), |
| 186 | PRESET(P3), |
| 187 | PRESET(P4), |
| 188 | PRESET(P5), |
| 189 | PRESET(P6), |
| 190 | PRESET(P7), |
| 191 | PRESET_ALIAS(SLOW, P7, NVENC_TWO_PASSES), |
| 192 | PRESET_ALIAS(MEDIUM, P4, NVENC_ONE_PASS), |
| 193 | PRESET_ALIAS(FAST, P1, NVENC_ONE_PASS), |
| 194 | // Compat aliases |
| 195 | PRESET_ALIAS(DEFAULT, P4, NVENC_DEPRECATED_PRESET), |
| 196 | PRESET_ALIAS(HP, P1, NVENC_DEPRECATED_PRESET), |
| 197 | PRESET_ALIAS(HQ, P7, NVENC_DEPRECATED_PRESET), |
| 198 | PRESET_ALIAS(BD, P5, NVENC_DEPRECATED_PRESET), |
| 199 | PRESET_ALIAS(LOW_LATENCY_DEFAULT, P4, NVENC_DEPRECATED_PRESET | NVENC_LOWLATENCY), |
| 200 | PRESET_ALIAS(LOW_LATENCY_HP, P1, NVENC_DEPRECATED_PRESET | NVENC_LOWLATENCY), |
| 201 | PRESET_ALIAS(LOW_LATENCY_HQ, P7, NVENC_DEPRECATED_PRESET | NVENC_LOWLATENCY), |
| 202 | PRESET_ALIAS(LOSSLESS_DEFAULT, P4, NVENC_DEPRECATED_PRESET | NVENC_LOSSLESS), |
| 203 | PRESET_ALIAS(LOSSLESS_HP, P1, NVENC_DEPRECATED_PRESET | NVENC_LOSSLESS), |
| 204 | #else |
| 205 | PRESET(DEFAULT), |
| 206 | PRESET(HP), |
| 207 | PRESET(HQ), |
| 208 | PRESET(BD), |
| 209 | PRESET_ALIAS(SLOW, HQ, NVENC_TWO_PASSES), |
| 210 | PRESET_ALIAS(MEDIUM, HQ, NVENC_ONE_PASS), |
| 211 | PRESET_ALIAS(FAST, HP, NVENC_ONE_PASS), |
| 212 | PRESET(LOW_LATENCY_DEFAULT, NVENC_LOWLATENCY), |
| 213 | PRESET(LOW_LATENCY_HP, NVENC_LOWLATENCY), |
| 214 | PRESET(LOW_LATENCY_HQ, NVENC_LOWLATENCY), |
| 215 | PRESET(LOSSLESS_DEFAULT, NVENC_LOSSLESS), |
| 216 | PRESET(LOSSLESS_HP, NVENC_LOSSLESS), |
| 217 | #endif |
| 218 | }; |
| 219 | |
| 220 | GUIDTuple *t = &presets[ctx->preset]; |
| 221 | |
| 222 | ctx->init_encode_params.presetGUID = t->guid; |
| 223 | ctx->flags = t->flags; |
| 224 | |
| 225 | #ifdef NVENC_HAVE_NEW_PRESETS |
| 226 | if (ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS) |
| 227 | ctx->flags |= NVENC_LOSSLESS; |
| 228 | #endif |
| 229 | } |
| 230 | |
| 231 | #undef PRESET |
| 232 | #undef PRESET_ALIAS |
| 233 | |
Timo Rothenpieler | cb3358b | 2017-06-01 09:55:25 | [diff] [blame] | 234 | static void nvenc_print_driver_requirement(AVCodecContext *avctx, int level) |
| 235 | { |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 236 | #if NVENCAPI_CHECK_VERSION(12, 1) |
Timo Rothenpieler | 648f5c9 | 2019-09-23 13:47:27 | [diff] [blame] | 237 | const char *minver = "(unknown)"; |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 238 | #elif NVENCAPI_CHECK_VERSION(12, 0) |
| 239 | # if defined(_WIN32) || defined(__CYGWIN__) |
| 240 | const char *minver = "522.25"; |
| 241 | # else |
| 242 | const char *minver = "520.56.06"; |
| 243 | # endif |
Timo Rothenpieler | ac04085 | 2021-08-08 00:00:38 | [diff] [blame] | 244 | #elif NVENCAPI_CHECK_VERSION(11, 1) |
| 245 | # if defined(_WIN32) || defined(__CYGWIN__) |
| 246 | const char *minver = "471.41"; |
| 247 | # else |
| 248 | const char *minver = "470.57.02"; |
| 249 | # endif |
Timo Rothenpieler | 62073cf | 2020-10-17 21:10:42 | [diff] [blame] | 250 | #elif NVENCAPI_CHECK_VERSION(11, 0) |
| 251 | # if defined(_WIN32) || defined(__CYGWIN__) |
| 252 | const char *minver = "456.71"; |
| 253 | # else |
| 254 | const char *minver = "455.28"; |
| 255 | # endif |
Roman Arzumanyan | 9115d77 | 2020-03-19 08:35:29 | [diff] [blame] | 256 | #elif NVENCAPI_CHECK_VERSION(10, 0) |
| 257 | # if defined(_WIN32) || defined(__CYGWIN__) |
| 258 | const char *minver = "450.51"; |
| 259 | # else |
| 260 | const char *minver = "445.87"; |
| 261 | # endif |
Timo Rothenpieler | 648f5c9 | 2019-09-23 13:47:27 | [diff] [blame] | 262 | #elif NVENCAPI_CHECK_VERSION(9, 1) |
| 263 | # if defined(_WIN32) || defined(__CYGWIN__) |
Timo Rothenpieler | 89cbbe9 | 2019-09-24 10:00:00 | [diff] [blame] | 264 | const char *minver = "436.15"; |
Timo Rothenpieler | 648f5c9 | 2019-09-23 13:47:27 | [diff] [blame] | 265 | # else |
| 266 | const char *minver = "435.21"; |
| 267 | # endif |
| 268 | #elif NVENCAPI_CHECK_VERSION(9, 0) |
Timo Rothenpieler | 1144d5c | 2019-03-09 23:25:31 | [diff] [blame] | 269 | # if defined(_WIN32) || defined(__CYGWIN__) |
| 270 | const char *minver = "418.81"; |
| 271 | # else |
| 272 | const char *minver = "418.30"; |
| 273 | # endif |
| 274 | #elif NVENCAPI_CHECK_VERSION(8, 2) |
| 275 | # if defined(_WIN32) || defined(__CYGWIN__) |
| 276 | const char *minver = "397.93"; |
| 277 | # else |
| 278 | const char *minver = "396.24"; |
| 279 | #endif |
| 280 | #elif NVENCAPI_CHECK_VERSION(8, 1) |
Timo Rothenpieler | 2108a67 | 2018-04-11 12:28:36 | [diff] [blame] | 281 | # if defined(_WIN32) || defined(__CYGWIN__) |
| 282 | const char *minver = "390.77"; |
| 283 | # else |
| 284 | const char *minver = "390.25"; |
| 285 | # endif |
Timo Rothenpieler | cb3358b | 2017-06-01 09:55:25 | [diff] [blame] | 286 | #else |
Timo Rothenpieler | 2108a67 | 2018-04-11 12:28:36 | [diff] [blame] | 287 | # if defined(_WIN32) || defined(__CYGWIN__) |
| 288 | const char *minver = "378.66"; |
| 289 | # else |
Timo Rothenpieler | cb3358b | 2017-06-01 09:55:25 | [diff] [blame] | 290 | const char *minver = "378.13"; |
Timo Rothenpieler | 2108a67 | 2018-04-11 12:28:36 | [diff] [blame] | 291 | # endif |
Timo Rothenpieler | cb3358b | 2017-06-01 09:55:25 | [diff] [blame] | 292 | #endif |
| 293 | av_log(avctx, level, "The minimum required Nvidia driver for nvenc is %s or newer\n", minver); |
| 294 | } |
| 295 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 296 | static av_cold int nvenc_load_libraries(AVCodecContext *avctx) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 297 | { |
Clément Bœsch | b7cc4eb | 2017-03-20 22:04:28 | [diff] [blame] | 298 | NvencContext *ctx = avctx->priv_data; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 299 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 300 | NVENCSTATUS err; |
Timo Rothenpieler | df615ef | 2016-08-28 17:46:44 | [diff] [blame] | 301 | uint32_t nvenc_max_ver; |
Timo Rothenpieler | a66835b | 2016-10-10 10:55:59 | [diff] [blame] | 302 | int ret; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 303 | |
Mark Thompson | 1dc483a | 2017-11-18 17:16:14 | [diff] [blame] | 304 | ret = cuda_load_functions(&dl_fn->cuda_dl, avctx); |
Timo Rothenpieler | a66835b | 2016-10-10 10:55:59 | [diff] [blame] | 305 | if (ret < 0) |
| 306 | return ret; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 307 | |
Mark Thompson | 1dc483a | 2017-11-18 17:16:14 | [diff] [blame] | 308 | ret = nvenc_load_functions(&dl_fn->nvenc_dl, avctx); |
Timo Rothenpieler | cb3358b | 2017-06-01 09:55:25 | [diff] [blame] | 309 | if (ret < 0) { |
| 310 | nvenc_print_driver_requirement(avctx, AV_LOG_ERROR); |
Timo Rothenpieler | a66835b | 2016-10-10 10:55:59 | [diff] [blame] | 311 | return ret; |
Timo Rothenpieler | cb3358b | 2017-06-01 09:55:25 | [diff] [blame] | 312 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 313 | |
Timo Rothenpieler | a66835b | 2016-10-10 10:55:59 | [diff] [blame] | 314 | err = dl_fn->nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver); |
Timo Rothenpieler | df615ef | 2016-08-28 17:46:44 | [diff] [blame] | 315 | if (err != NV_ENC_SUCCESS) |
| 316 | return nvenc_print_error(avctx, err, "Failed to query nvenc max version"); |
| 317 | |
| 318 | av_log(avctx, AV_LOG_VERBOSE, "Loaded Nvenc version %d.%d\n", nvenc_max_ver >> 4, nvenc_max_ver & 0xf); |
| 319 | |
| 320 | if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) { |
| 321 | av_log(avctx, AV_LOG_ERROR, "Driver does not support the required nvenc API version. " |
| 322 | "Required: %d.%d Found: %d.%d\n", |
| 323 | NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION, |
| 324 | nvenc_max_ver >> 4, nvenc_max_ver & 0xf); |
Timo Rothenpieler | cb3358b | 2017-06-01 09:55:25 | [diff] [blame] | 325 | nvenc_print_driver_requirement(avctx, AV_LOG_ERROR); |
Timo Rothenpieler | df615ef | 2016-08-28 17:46:44 | [diff] [blame] | 326 | return AVERROR(ENOSYS); |
| 327 | } |
| 328 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 329 | dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER; |
| 330 | |
Timo Rothenpieler | a66835b | 2016-10-10 10:55:59 | [diff] [blame] | 331 | err = dl_fn->nvenc_dl->NvEncodeAPICreateInstance(&dl_fn->nvenc_funcs); |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 332 | if (err != NV_ENC_SUCCESS) |
| 333 | return nvenc_print_error(avctx, err, "Failed to create nvenc instance"); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 334 | |
| 335 | av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n"); |
| 336 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 337 | return 0; |
| 338 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 339 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 340 | static int nvenc_push_context(AVCodecContext *avctx) |
| 341 | { |
| 342 | NvencContext *ctx = avctx->priv_data; |
| 343 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 344 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 345 | if (ctx->d3d11_device) |
| 346 | return 0; |
| 347 | |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 348 | return CHECK_CU(dl_fn->cuda_dl->cuCtxPushCurrent(ctx->cu_context)); |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int nvenc_pop_context(AVCodecContext *avctx) |
| 352 | { |
| 353 | NvencContext *ctx = avctx->priv_data; |
| 354 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 355 | CUcontext dummy; |
| 356 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 357 | if (ctx->d3d11_device) |
| 358 | return 0; |
| 359 | |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 360 | return CHECK_CU(dl_fn->cuda_dl->cuCtxPopCurrent(&dummy)); |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 361 | } |
| 362 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 363 | static av_cold int nvenc_open_session(AVCodecContext *avctx) |
| 364 | { |
| 365 | NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 }; |
| 366 | NvencContext *ctx = avctx->priv_data; |
| 367 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs; |
| 368 | NVENCSTATUS ret; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 369 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 370 | params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER; |
| 371 | params.apiVersion = NVENCAPI_VERSION; |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 372 | if (ctx->d3d11_device) { |
| 373 | params.device = ctx->d3d11_device; |
| 374 | params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX; |
| 375 | } else { |
| 376 | params.device = ctx->cu_context; |
| 377 | params.deviceType = NV_ENC_DEVICE_TYPE_CUDA; |
| 378 | } |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 379 | |
| 380 | ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &ctx->nvencoder); |
| 381 | if (ret != NV_ENC_SUCCESS) { |
| 382 | ctx->nvencoder = NULL; |
| 383 | return nvenc_print_error(avctx, ret, "OpenEncodeSessionEx failed"); |
| 384 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 389 | static int nvenc_check_codec_support(AVCodecContext *avctx) |
| 390 | { |
Clément Bœsch | b7cc4eb | 2017-03-20 22:04:28 | [diff] [blame] | 391 | NvencContext *ctx = avctx->priv_data; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 392 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs; |
| 393 | int i, ret, count = 0; |
| 394 | GUID *guids = NULL; |
| 395 | |
| 396 | ret = p_nvenc->nvEncGetEncodeGUIDCount(ctx->nvencoder, &count); |
| 397 | |
| 398 | if (ret != NV_ENC_SUCCESS || !count) |
| 399 | return AVERROR(ENOSYS); |
| 400 | |
| 401 | guids = av_malloc(count * sizeof(GUID)); |
| 402 | if (!guids) |
| 403 | return AVERROR(ENOMEM); |
| 404 | |
| 405 | ret = p_nvenc->nvEncGetEncodeGUIDs(ctx->nvencoder, guids, count, &count); |
| 406 | if (ret != NV_ENC_SUCCESS) { |
| 407 | ret = AVERROR(ENOSYS); |
| 408 | goto fail; |
| 409 | } |
| 410 | |
| 411 | ret = AVERROR(ENOSYS); |
| 412 | for (i = 0; i < count; i++) { |
| 413 | if (!memcmp(&guids[i], &ctx->init_encode_params.encodeGUID, sizeof(*guids))) { |
| 414 | ret = 0; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | fail: |
| 420 | av_free(guids); |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap) |
| 426 | { |
| 427 | NvencContext *ctx = avctx->priv_data; |
| 428 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs; |
| 429 | NV_ENC_CAPS_PARAM params = { 0 }; |
| 430 | int ret, val = 0; |
| 431 | |
| 432 | params.version = NV_ENC_CAPS_PARAM_VER; |
| 433 | params.capsToQuery = cap; |
| 434 | |
| 435 | ret = p_nvenc->nvEncGetEncodeCaps(ctx->nvencoder, ctx->init_encode_params.encodeGUID, ¶ms, &val); |
| 436 | |
| 437 | if (ret == NV_ENC_SUCCESS) |
| 438 | return val; |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int nvenc_check_capabilities(AVCodecContext *avctx) |
| 443 | { |
| 444 | NvencContext *ctx = avctx->priv_data; |
Timo Rothenpieler | 7a8560c | 2023-03-10 12:09:10 | [diff] [blame] | 445 | int tmp, ret; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 446 | |
| 447 | ret = nvenc_check_codec_support(avctx); |
| 448 | if (ret < 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 449 | av_log(avctx, AV_LOG_WARNING, "Codec not supported\n"); |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_YUV444_ENCODE); |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 454 | if (IS_YUV444(ctx->data_pix_fmt) && ret <= 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 455 | av_log(avctx, AV_LOG_WARNING, "YUV444P not supported\n"); |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 456 | return AVERROR(ENOSYS); |
| 457 | } |
| 458 | |
| 459 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE); |
Timo Rothenpieler | 988f2e9 | 2021-04-18 09:17:54 | [diff] [blame] | 460 | if (ctx->flags & NVENC_LOSSLESS && ret <= 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 461 | av_log(avctx, AV_LOG_WARNING, "Lossless encoding not supported\n"); |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 462 | return AVERROR(ENOSYS); |
| 463 | } |
| 464 | |
| 465 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_WIDTH_MAX); |
| 466 | if (ret < avctx->width) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 467 | av_log(avctx, AV_LOG_WARNING, "Width %d exceeds %d\n", |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 468 | avctx->width, ret); |
| 469 | return AVERROR(ENOSYS); |
| 470 | } |
| 471 | |
| 472 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_HEIGHT_MAX); |
| 473 | if (ret < avctx->height) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 474 | av_log(avctx, AV_LOG_WARNING, "Height %d exceeds %d\n", |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 475 | avctx->height, ret); |
| 476 | return AVERROR(ENOSYS); |
| 477 | } |
| 478 | |
| 479 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_NUM_MAX_BFRAMES); |
| 480 | if (ret < avctx->max_b_frames) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 481 | av_log(avctx, AV_LOG_WARNING, "Max B-frames %d exceed %d\n", |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 482 | avctx->max_b_frames, ret); |
| 483 | |
| 484 | return AVERROR(ENOSYS); |
| 485 | } |
| 486 | |
Timo Rothenpieler | 808356c | 2016-06-06 19:19:29 | [diff] [blame] | 487 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_FIELD_ENCODING); |
| 488 | if (ret < 1 && avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 489 | av_log(avctx, AV_LOG_WARNING, |
Timo Rothenpieler | 808356c | 2016-06-06 19:19:29 | [diff] [blame] | 490 | "Interlaced encoding is not supported. Supported level: %d\n", |
| 491 | ret); |
| 492 | return AVERROR(ENOSYS); |
| 493 | } |
| 494 | |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 495 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_10BIT_ENCODE); |
| 496 | if (IS_10BIT(ctx->data_pix_fmt) && ret <= 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 497 | av_log(avctx, AV_LOG_WARNING, "10 bit encode not supported\n"); |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 498 | return AVERROR(ENOSYS); |
| 499 | } |
| 500 | |
Oliver Collyer | a81b398 | 2016-08-25 15:20:03 | [diff] [blame] | 501 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOOKAHEAD); |
| 502 | if (ctx->rc_lookahead > 0 && ret <= 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 503 | av_log(avctx, AV_LOG_WARNING, "RC lookahead not supported\n"); |
Oliver Collyer | a81b398 | 2016-08-25 15:20:03 | [diff] [blame] | 504 | return AVERROR(ENOSYS); |
| 505 | } |
| 506 | |
Sven C. Dack | da4d0fa | 2016-10-14 15:02:54 | [diff] [blame] | 507 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ); |
| 508 | if (ctx->temporal_aq > 0 && ret <= 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 509 | av_log(avctx, AV_LOG_WARNING, "Temporal AQ not supported\n"); |
Sven C. Dack | da4d0fa | 2016-10-14 15:02:54 | [diff] [blame] | 510 | return AVERROR(ENOSYS); |
| 511 | } |
| 512 | |
Sumit Agarwal | 0177573 | 2017-05-09 14:24:54 | [diff] [blame] | 513 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION); |
| 514 | if (ctx->weighted_pred > 0 && ret <= 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 515 | av_log (avctx, AV_LOG_WARNING, "Weighted Prediction not supported\n"); |
Sumit Agarwal | 0177573 | 2017-05-09 14:24:54 | [diff] [blame] | 516 | return AVERROR(ENOSYS); |
| 517 | } |
| 518 | |
Timo Rothenpieler | a0b69e2 | 2017-08-30 19:06:25 | [diff] [blame] | 519 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_CABAC); |
| 520 | if (ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC && ret <= 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 521 | av_log(avctx, AV_LOG_WARNING, "CABAC entropy coding not supported\n"); |
Timo Rothenpieler | a0b69e2 | 2017-08-30 19:06:25 | [diff] [blame] | 522 | return AVERROR(ENOSYS); |
| 523 | } |
| 524 | |
Timo Rothenpieler | 86e9dba | 2018-04-11 12:22:20 | [diff] [blame] | 525 | #ifdef NVENC_HAVE_BFRAME_REF_MODE |
Timo Rothenpieler | 7a8560c | 2023-03-10 12:09:10 | [diff] [blame] | 526 | tmp = (ctx->b_ref_mode >= 0) ? ctx->b_ref_mode : NV_ENC_BFRAME_REF_MODE_DISABLED; |
Timo Rothenpieler | 86e9dba | 2018-04-11 12:22:20 | [diff] [blame] | 527 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE); |
Timo Rothenpieler | 7a8560c | 2023-03-10 12:09:10 | [diff] [blame] | 528 | if (tmp == NV_ENC_BFRAME_REF_MODE_EACH && ret != 1 && ret != 3) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 529 | av_log(avctx, AV_LOG_WARNING, "Each B frame as reference is not supported\n"); |
Timo Rothenpieler | 86e9dba | 2018-04-11 12:22:20 | [diff] [blame] | 530 | return AVERROR(ENOSYS); |
Timo Rothenpieler | 7a8560c | 2023-03-10 12:09:10 | [diff] [blame] | 531 | } else if (tmp != NV_ENC_BFRAME_REF_MODE_DISABLED && ret == 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 532 | av_log(avctx, AV_LOG_WARNING, "B frames as references are not supported\n"); |
Timo Rothenpieler | 86e9dba | 2018-04-11 12:22:20 | [diff] [blame] | 533 | return AVERROR(ENOSYS); |
| 534 | } |
| 535 | #else |
Timo Rothenpieler | 7a8560c | 2023-03-10 12:09:10 | [diff] [blame] | 536 | tmp = (ctx->b_ref_mode >= 0) ? ctx->b_ref_mode : 0; |
| 537 | if (tmp > 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 538 | av_log(avctx, AV_LOG_WARNING, "B frames as references need SDK 8.1 at build time\n"); |
Timo Rothenpieler | 86e9dba | 2018-04-11 12:22:20 | [diff] [blame] | 539 | return AVERROR(ENOSYS); |
| 540 | } |
| 541 | #endif |
| 542 | |
Roman Arzumanyan | 567b5e3 | 2019-09-27 15:56:11 | [diff] [blame] | 543 | #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES |
| 544 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES); |
| 545 | if(avctx->refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT && ret <= 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 546 | av_log(avctx, AV_LOG_WARNING, "Multiple reference frames are not supported by the device\n"); |
Roman Arzumanyan | 567b5e3 | 2019-09-27 15:56:11 | [diff] [blame] | 547 | return AVERROR(ENOSYS); |
| 548 | } |
| 549 | #else |
| 550 | if(avctx->refs != 0) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 551 | av_log(avctx, AV_LOG_WARNING, "Multiple reference frames need SDK 9.1 at build time\n"); |
Roman Arzumanyan | 567b5e3 | 2019-09-27 15:56:11 | [diff] [blame] | 552 | return AVERROR(ENOSYS); |
| 553 | } |
| 554 | #endif |
| 555 | |
Limin Wang | 3756525 | 2021-09-02 10:38:57 | [diff] [blame] | 556 | #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH |
| 557 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SINGLE_SLICE_INTRA_REFRESH); |
| 558 | if(ctx->single_slice_intra_refresh && ret <= 0) { |
| 559 | av_log(avctx, AV_LOG_WARNING, "Single slice intra refresh not supported by the device\n"); |
| 560 | return AVERROR(ENOSYS); |
| 561 | } |
| 562 | #else |
| 563 | if(ctx->single_slice_intra_refresh) { |
| 564 | av_log(avctx, AV_LOG_WARNING, "Single slice intra refresh needs SDK 11.1 at build time\n"); |
| 565 | return AVERROR(ENOSYS); |
| 566 | } |
| 567 | #endif |
| 568 | |
Limin Wang | e6bd517 | 2021-09-06 02:01:58 | [diff] [blame] | 569 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_INTRA_REFRESH); |
Limin Wang | 3756525 | 2021-09-02 10:38:57 | [diff] [blame] | 570 | if((ctx->intra_refresh || ctx->single_slice_intra_refresh) && ret <= 0) { |
Limin Wang | e6bd517 | 2021-09-06 02:01:58 | [diff] [blame] | 571 | av_log(avctx, AV_LOG_WARNING, "Intra refresh not supported by the device\n"); |
| 572 | return AVERROR(ENOSYS); |
| 573 | } |
| 574 | |
Limin Wang | 75001ae | 2021-09-02 10:38:58 | [diff] [blame] | 575 | #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING |
| 576 | if (ctx->constrained_encoding && avctx->codec->id == AV_CODEC_ID_HEVC) { |
| 577 | av_log(avctx, AV_LOG_WARNING, "HEVC constrained encoding needs SDK 10.0 at build time\n"); |
| 578 | return AVERROR(ENOSYS); |
| 579 | } |
| 580 | #endif |
| 581 | |
| 582 | ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING); |
| 583 | if(ctx->constrained_encoding && ret <= 0) { |
| 584 | av_log(avctx, AV_LOG_WARNING, "Constrained encoding not supported by the device\n"); |
| 585 | return AVERROR(ENOSYS); |
| 586 | } |
| 587 | |
pkviet | 1553751 | 2018-05-03 00:15:52 | [diff] [blame] | 588 | ctx->support_dyn_bitrate = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE); |
| 589 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx) |
| 594 | { |
| 595 | NvencContext *ctx = avctx->priv_data; |
| 596 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 597 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 598 | char name[128] = { 0}; |
| 599 | int major, minor, ret; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 600 | CUdevice cu_device; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 601 | int loglevel = AV_LOG_VERBOSE; |
| 602 | |
| 603 | if (ctx->device == LIST_DEVICES) |
| 604 | loglevel = AV_LOG_INFO; |
| 605 | |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 606 | ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceGet(&cu_device, idx)); |
| 607 | if (ret < 0) |
| 608 | return ret; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 609 | |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 610 | ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceGetName(name, sizeof(name), cu_device)); |
| 611 | if (ret < 0) |
| 612 | return ret; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 613 | |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 614 | ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceComputeCapability(&major, &minor, cu_device)); |
| 615 | if (ret < 0) |
| 616 | return ret; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 617 | |
| 618 | av_log(avctx, loglevel, "[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx, name, major, minor); |
| 619 | if (((major << 4) | minor) < NVENC_CAP) { |
| 620 | av_log(avctx, loglevel, "does not support NVENC\n"); |
| 621 | goto fail; |
| 622 | } |
| 623 | |
Timo Rothenpieler | 5403d90 | 2017-01-18 22:01:28 | [diff] [blame] | 624 | if (ctx->device != idx && ctx->device != ANY_DEVICE) |
| 625 | return -1; |
| 626 | |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 627 | ret = CHECK_CU(dl_fn->cuda_dl->cuCtxCreate(&ctx->cu_context_internal, 0, cu_device)); |
| 628 | if (ret < 0) |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 629 | goto fail; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 630 | |
| 631 | ctx->cu_context = ctx->cu_context_internal; |
Timo Rothenpieler | 51a2334 | 2019-09-27 16:30:10 | [diff] [blame] | 632 | ctx->cu_stream = NULL; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 633 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 634 | if ((ret = nvenc_pop_context(avctx)) < 0) |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 635 | goto fail2; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 636 | |
| 637 | if ((ret = nvenc_open_session(avctx)) < 0) |
| 638 | goto fail2; |
| 639 | |
| 640 | if ((ret = nvenc_check_capabilities(avctx)) < 0) |
| 641 | goto fail3; |
| 642 | |
| 643 | av_log(avctx, loglevel, "supports NVENC\n"); |
| 644 | |
| 645 | dl_fn->nvenc_device_count++; |
| 646 | |
Timo Rothenpieler | 5403d90 | 2017-01-18 22:01:28 | [diff] [blame] | 647 | if (ctx->device == idx || ctx->device == ANY_DEVICE) |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 648 | return 0; |
| 649 | |
| 650 | fail3: |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 651 | if ((ret = nvenc_push_context(avctx)) < 0) |
| 652 | return ret; |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 653 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 654 | p_nvenc->nvEncDestroyEncoder(ctx->nvencoder); |
| 655 | ctx->nvencoder = NULL; |
| 656 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 657 | if ((ret = nvenc_pop_context(avctx)) < 0) |
| 658 | return ret; |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 659 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 660 | fail2: |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 661 | CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal)); |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 662 | ctx->cu_context_internal = NULL; |
| 663 | |
| 664 | fail: |
| 665 | return AVERROR(ENOSYS); |
| 666 | } |
| 667 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 668 | static av_cold int nvenc_setup_device(AVCodecContext *avctx) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 669 | { |
Clément Bœsch | b7cc4eb | 2017-03-20 22:04:28 | [diff] [blame] | 670 | NvencContext *ctx = avctx->priv_data; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 671 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 672 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 673 | switch (avctx->codec->id) { |
| 674 | case AV_CODEC_ID_H264: |
| 675 | ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID; |
| 676 | break; |
| 677 | case AV_CODEC_ID_HEVC: |
| 678 | ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID; |
| 679 | break; |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 680 | #if CONFIG_AV1_NVENC_ENCODER |
| 681 | case AV_CODEC_ID_AV1: |
| 682 | ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_AV1_GUID; |
| 683 | break; |
| 684 | #endif |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 685 | default: |
| 686 | return AVERROR_BUG; |
| 687 | } |
| 688 | |
Timo Rothenpieler | 988f2e9 | 2021-04-18 09:17:54 | [diff] [blame] | 689 | nvenc_map_preset(ctx); |
| 690 | |
| 691 | if (ctx->flags & NVENC_DEPRECATED_PRESET) |
| 692 | av_log(avctx, AV_LOG_WARNING, "The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n"); |
| 693 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 694 | if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11 || avctx->hw_frames_ctx || avctx->hw_device_ctx) { |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 695 | AVHWFramesContext *frames_ctx; |
Timo Rothenpieler | dad6f44 | 2017-05-07 11:35:25 | [diff] [blame] | 696 | AVHWDeviceContext *hwdev_ctx; |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 697 | AVCUDADeviceContext *cuda_device_hwctx = NULL; |
| 698 | #if CONFIG_D3D11VA |
| 699 | AVD3D11VADeviceContext *d3d11_device_hwctx = NULL; |
| 700 | #endif |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 701 | int ret; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 702 | |
Timo Rothenpieler | dad6f44 | 2017-05-07 11:35:25 | [diff] [blame] | 703 | if (avctx->hw_frames_ctx) { |
| 704 | frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 705 | if (frames_ctx->format == AV_PIX_FMT_CUDA) |
| 706 | cuda_device_hwctx = frames_ctx->device_ctx->hwctx; |
| 707 | #if CONFIG_D3D11VA |
| 708 | else if (frames_ctx->format == AV_PIX_FMT_D3D11) |
| 709 | d3d11_device_hwctx = frames_ctx->device_ctx->hwctx; |
| 710 | #endif |
| 711 | else |
| 712 | return AVERROR(EINVAL); |
Timo Rothenpieler | dad6f44 | 2017-05-07 11:35:25 | [diff] [blame] | 713 | } else if (avctx->hw_device_ctx) { |
| 714 | hwdev_ctx = (AVHWDeviceContext*)avctx->hw_device_ctx->data; |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 715 | if (hwdev_ctx->type == AV_HWDEVICE_TYPE_CUDA) |
| 716 | cuda_device_hwctx = hwdev_ctx->hwctx; |
| 717 | #if CONFIG_D3D11VA |
| 718 | else if (hwdev_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) |
| 719 | d3d11_device_hwctx = hwdev_ctx->hwctx; |
| 720 | #endif |
| 721 | else |
| 722 | return AVERROR(EINVAL); |
Timo Rothenpieler | dad6f44 | 2017-05-07 11:35:25 | [diff] [blame] | 723 | } else { |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 724 | return AVERROR(EINVAL); |
Timo Rothenpieler | dad6f44 | 2017-05-07 11:35:25 | [diff] [blame] | 725 | } |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 726 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 727 | if (cuda_device_hwctx) { |
| 728 | ctx->cu_context = cuda_device_hwctx->cuda_ctx; |
Timo Rothenpieler | 51a2334 | 2019-09-27 16:30:10 | [diff] [blame] | 729 | ctx->cu_stream = cuda_device_hwctx->stream; |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 730 | } |
| 731 | #if CONFIG_D3D11VA |
| 732 | else if (d3d11_device_hwctx) { |
| 733 | ctx->d3d11_device = d3d11_device_hwctx->device; |
| 734 | ID3D11Device_AddRef(ctx->d3d11_device); |
| 735 | } |
| 736 | #endif |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 737 | |
| 738 | ret = nvenc_open_session(avctx); |
| 739 | if (ret < 0) |
| 740 | return ret; |
| 741 | |
| 742 | ret = nvenc_check_capabilities(avctx); |
| 743 | if (ret < 0) { |
| 744 | av_log(avctx, AV_LOG_FATAL, "Provided device doesn't support required NVENC features\n"); |
| 745 | return ret; |
| 746 | } |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 747 | } else { |
| 748 | int i, nb_devices = 0; |
| 749 | |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 750 | if (CHECK_CU(dl_fn->cuda_dl->cuInit(0)) < 0) |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 751 | return AVERROR_UNKNOWN; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 752 | |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 753 | if (CHECK_CU(dl_fn->cuda_dl->cuDeviceGetCount(&nb_devices)) < 0) |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 754 | return AVERROR_UNKNOWN; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 755 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 756 | if (!nb_devices) { |
| 757 | av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n"); |
| 758 | return AVERROR_EXTERNAL; |
| 759 | } |
| 760 | |
| 761 | av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", nb_devices); |
| 762 | |
| 763 | dl_fn->nvenc_device_count = 0; |
| 764 | for (i = 0; i < nb_devices; ++i) { |
| 765 | if ((nvenc_check_device(avctx, i)) >= 0 && ctx->device != LIST_DEVICES) |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | if (ctx->device == LIST_DEVICES) |
| 770 | return AVERROR_EXIT; |
| 771 | |
| 772 | if (!dl_fn->nvenc_device_count) { |
hydra3333 | ba6b20d | 2019-10-27 02:30:54 | [diff] [blame] | 773 | av_log(avctx, AV_LOG_FATAL, "No capable devices found\n"); |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 774 | return AVERROR_EXTERNAL; |
| 775 | } |
| 776 | |
Timo Rothenpieler | 5403d90 | 2017-01-18 22:01:28 | [diff] [blame] | 777 | av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->device, nb_devices); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 778 | return AVERROR(EINVAL); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 779 | } |
| 780 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 784 | static av_cold void set_constqp(AVCodecContext *avctx) |
| 785 | { |
| 786 | NvencContext *ctx = avctx->priv_data; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 787 | NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams; |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 788 | #if CONFIG_AV1_NVENC_ENCODER |
| 789 | int qmax = avctx->codec->id == AV_CODEC_ID_AV1 ? 255 : 51; |
| 790 | #else |
| 791 | int qmax = 51; |
| 792 | #endif |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 793 | |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 794 | rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; |
Konda Raju | 2db5ab7 | 2017-03-17 04:12:25 | [diff] [blame] | 795 | |
| 796 | if (ctx->init_qp_p >= 0) { |
| 797 | rc->constQP.qpInterP = ctx->init_qp_p; |
| 798 | if (ctx->init_qp_i >= 0 && ctx->init_qp_b >= 0) { |
| 799 | rc->constQP.qpIntra = ctx->init_qp_i; |
| 800 | rc->constQP.qpInterB = ctx->init_qp_b; |
| 801 | } else if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) { |
| 802 | rc->constQP.qpIntra = av_clip( |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 803 | rc->constQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, qmax); |
Konda Raju | 2db5ab7 | 2017-03-17 04:12:25 | [diff] [blame] | 804 | rc->constQP.qpInterB = av_clip( |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 805 | rc->constQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, qmax); |
Konda Raju | 2db5ab7 | 2017-03-17 04:12:25 | [diff] [blame] | 806 | } else { |
| 807 | rc->constQP.qpIntra = rc->constQP.qpInterP; |
| 808 | rc->constQP.qpInterB = rc->constQP.qpInterP; |
| 809 | } |
Timo Rothenpieler | 7fb2a7a | 2017-03-23 16:01:40 | [diff] [blame] | 810 | } else if (ctx->cqp >= 0) { |
Timo Rothenpieler | d84c229 | 2017-03-23 16:10:25 | [diff] [blame] | 811 | rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra = ctx->cqp; |
| 812 | if (avctx->b_quant_factor != 0.0) |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 813 | rc->constQP.qpInterB = av_clip(ctx->cqp * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, qmax); |
Timo Rothenpieler | d84c229 | 2017-03-23 16:10:25 | [diff] [blame] | 814 | if (avctx->i_quant_factor != 0.0) |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 815 | rc->constQP.qpIntra = av_clip(ctx->cqp * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, qmax); |
Konda Raju | 2db5ab7 | 2017-03-17 04:12:25 | [diff] [blame] | 816 | } |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 817 | |
| 818 | avctx->qmin = -1; |
| 819 | avctx->qmax = -1; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static av_cold void set_vbr(AVCodecContext *avctx) |
| 823 | { |
| 824 | NvencContext *ctx = avctx->priv_data; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 825 | NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams; |
| 826 | int qp_inter_p; |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 827 | #if CONFIG_AV1_NVENC_ENCODER |
| 828 | int qmax = avctx->codec->id == AV_CODEC_ID_AV1 ? 255 : 51; |
| 829 | #else |
| 830 | int qmax = 51; |
| 831 | #endif |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 832 | |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 833 | if (avctx->qmin >= 0 && avctx->qmax >= 0) { |
| 834 | rc->enableMinQP = 1; |
| 835 | rc->enableMaxQP = 1; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 836 | |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 837 | rc->minQP.qpInterB = avctx->qmin; |
| 838 | rc->minQP.qpInterP = avctx->qmin; |
Clément Bœsch | b7cc4eb | 2017-03-20 22:04:28 | [diff] [blame] | 839 | rc->minQP.qpIntra = avctx->qmin; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 840 | |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 841 | rc->maxQP.qpInterB = avctx->qmax; |
| 842 | rc->maxQP.qpInterP = avctx->qmax; |
| 843 | rc->maxQP.qpIntra = avctx->qmax; |
| 844 | |
| 845 | qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin |
Timo Rothenpieler | 971351b | 2016-05-31 15:00:07 | [diff] [blame] | 846 | } else if (avctx->qmin >= 0) { |
| 847 | rc->enableMinQP = 1; |
| 848 | |
| 849 | rc->minQP.qpInterB = avctx->qmin; |
| 850 | rc->minQP.qpInterP = avctx->qmin; |
| 851 | rc->minQP.qpIntra = avctx->qmin; |
| 852 | |
| 853 | qp_inter_p = avctx->qmin; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 854 | } else { |
| 855 | qp_inter_p = 26; // default to 26 |
| 856 | } |
| 857 | |
| 858 | rc->enableInitialRCQP = 1; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 859 | |
Konda Raju | 5f44a4a | 2017-02-28 05:39:12 | [diff] [blame] | 860 | if (ctx->init_qp_p < 0) { |
| 861 | rc->initialRCQP.qpInterP = qp_inter_p; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 862 | } else { |
Konda Raju | 5f44a4a | 2017-02-28 05:39:12 | [diff] [blame] | 863 | rc->initialRCQP.qpInterP = ctx->init_qp_p; |
| 864 | } |
| 865 | |
| 866 | if (ctx->init_qp_i < 0) { |
| 867 | if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) { |
| 868 | rc->initialRCQP.qpIntra = av_clip( |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 869 | rc->initialRCQP.qpInterP * fabs(avctx->i_quant_factor) + avctx->i_quant_offset + 0.5, 0, qmax); |
Konda Raju | 5f44a4a | 2017-02-28 05:39:12 | [diff] [blame] | 870 | } else { |
| 871 | rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP; |
| 872 | } |
| 873 | } else { |
| 874 | rc->initialRCQP.qpIntra = ctx->init_qp_i; |
| 875 | } |
| 876 | |
| 877 | if (ctx->init_qp_b < 0) { |
| 878 | if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) { |
| 879 | rc->initialRCQP.qpInterB = av_clip( |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 880 | rc->initialRCQP.qpInterP * fabs(avctx->b_quant_factor) + avctx->b_quant_offset + 0.5, 0, qmax); |
Konda Raju | 5f44a4a | 2017-02-28 05:39:12 | [diff] [blame] | 881 | } else { |
| 882 | rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP; |
| 883 | } |
| 884 | } else { |
| 885 | rc->initialRCQP.qpInterB = ctx->init_qp_b; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 886 | } |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | static av_cold void set_lossless(AVCodecContext *avctx) |
| 890 | { |
| 891 | NvencContext *ctx = avctx->priv_data; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 892 | NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 893 | |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 894 | rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; |
| 895 | rc->constQP.qpInterB = 0; |
| 896 | rc->constQP.qpInterP = 0; |
Clément Bœsch | b7cc4eb | 2017-03-20 22:04:28 | [diff] [blame] | 897 | rc->constQP.qpIntra = 0; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 898 | |
| 899 | avctx->qmin = -1; |
| 900 | avctx->qmax = -1; |
| 901 | } |
| 902 | |
| 903 | static void nvenc_override_rate_control(AVCodecContext *avctx) |
| 904 | { |
| 905 | NvencContext *ctx = avctx->priv_data; |
| 906 | NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams; |
| 907 | |
| 908 | switch (ctx->rc) { |
| 909 | case NV_ENC_PARAMS_RC_CONSTQP: |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 910 | set_constqp(avctx); |
| 911 | return; |
Timo Rothenpieler | d2b46c1 | 2023-06-01 21:46:46 | [diff] [blame] | 912 | #ifndef NVENC_NO_DEPRECATED_RC |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 913 | case NV_ENC_PARAMS_RC_VBR_MINQP: |
| 914 | if (avctx->qmin < 0) { |
| 915 | av_log(avctx, AV_LOG_WARNING, |
| 916 | "The variable bitrate rate-control requires " |
| 917 | "the 'qmin' option set.\n"); |
| 918 | set_vbr(avctx); |
| 919 | return; |
| 920 | } |
Ganapathy Raman Kasi | a549243 | 2017-02-24 01:42:27 | [diff] [blame] | 921 | /* fall through */ |
Timo Rothenpieler | cfbebe9 | 2017-05-09 11:57:39 | [diff] [blame] | 922 | case NV_ENC_PARAMS_RC_VBR_HQ: |
Timo Rothenpieler | d2b46c1 | 2023-06-01 21:46:46 | [diff] [blame] | 923 | #endif |
Ganapathy Raman Kasi | a549243 | 2017-02-24 01:42:27 | [diff] [blame] | 924 | case NV_ENC_PARAMS_RC_VBR: |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 925 | set_vbr(avctx); |
| 926 | break; |
| 927 | case NV_ENC_PARAMS_RC_CBR: |
Timo Rothenpieler | d2b46c1 | 2023-06-01 21:46:46 | [diff] [blame] | 928 | #ifndef NVENC_NO_DEPRECATED_RC |
Timo Rothenpieler | cfbebe9 | 2017-05-09 11:57:39 | [diff] [blame] | 929 | case NV_ENC_PARAMS_RC_CBR_HQ: |
| 930 | case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ: |
Timo Rothenpieler | d2b46c1 | 2023-06-01 21:46:46 | [diff] [blame] | 931 | #endif |
Timo Rothenpieler | eae4eba | 2016-05-31 14:55:24 | [diff] [blame] | 932 | break; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | rc->rateControlMode = ctx->rc; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 936 | } |
| 937 | |
Miroslav Slugeň | de2faec | 2016-11-21 11:17:43 | [diff] [blame] | 938 | static av_cold int nvenc_recalc_surfaces(AVCodecContext *avctx) |
| 939 | { |
| 940 | NvencContext *ctx = avctx->priv_data; |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 941 | // default minimum of 4 surfaces |
| 942 | // multiply by 2 for number of NVENCs on gpu (hardcode to 2) |
| 943 | // another multiply by 2 to avoid blocking next PBB group |
| 944 | int nb_surfaces = FFMAX(4, ctx->encode_config.frameIntervalP * 2 * 2); |
Miroslav Slugeň | de2faec | 2016-11-21 11:17:43 | [diff] [blame] | 945 | |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 946 | // lookahead enabled |
Miroslav Slugeň | de2faec | 2016-11-21 11:17:43 | [diff] [blame] | 947 | if (ctx->rc_lookahead > 0) { |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 948 | // +1 is to account for lkd_bound calculation later |
| 949 | // +4 is to allow sufficient pipelining with lookahead |
| 950 | nb_surfaces = FFMAX(1, FFMAX(nb_surfaces, ctx->rc_lookahead + ctx->encode_config.frameIntervalP + 1 + 4)); |
| 951 | if (nb_surfaces > ctx->nb_surfaces && ctx->nb_surfaces > 0) |
| 952 | { |
Miroslav Slugeň | de2faec | 2016-11-21 11:17:43 | [diff] [blame] | 953 | av_log(avctx, AV_LOG_WARNING, |
| 954 | "Defined rc_lookahead requires more surfaces, " |
| 955 | "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces); |
Miroslav Slugeň | de2faec | 2016-11-21 11:17:43 | [diff] [blame] | 956 | } |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 957 | ctx->nb_surfaces = FFMAX(nb_surfaces, ctx->nb_surfaces); |
| 958 | } else { |
| 959 | if (ctx->encode_config.frameIntervalP > 1 && ctx->nb_surfaces < nb_surfaces && ctx->nb_surfaces > 0) |
| 960 | { |
| 961 | av_log(avctx, AV_LOG_WARNING, |
| 962 | "Defined b-frame requires more surfaces, " |
| 963 | "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces); |
| 964 | ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, nb_surfaces); |
| 965 | } |
| 966 | else if (ctx->nb_surfaces <= 0) |
| 967 | ctx->nb_surfaces = nb_surfaces; |
| 968 | // otherwise use user specified value |
Miroslav Slugeň | de2faec | 2016-11-21 11:17:43 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | ctx->nb_surfaces = FFMAX(1, FFMIN(MAX_REGISTERED_FRAMES, ctx->nb_surfaces)); |
| 972 | ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1); |
| 973 | |
Timo Rothenpieler | 16fdb48 | 2023-06-16 19:35:45 | [diff] [blame] | 974 | // Output in the worst case will only start when the surface buffer is completely full. |
| 975 | // Hence we need to keep at least the max amount of surfaces plus the max reorder delay around. |
| 976 | ctx->frame_data_array_nb = ctx->nb_surfaces + ctx->encode_config.frameIntervalP - 1; |
| 977 | |
Miroslav Slugeň | de2faec | 2016-11-21 11:17:43 | [diff] [blame] | 978 | return 0; |
| 979 | } |
| 980 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 981 | static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx) |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 982 | { |
| 983 | NvencContext *ctx = avctx->priv_data; |
| 984 | |
Timo Rothenpieler | 7fb2a7a | 2017-03-23 16:01:40 | [diff] [blame] | 985 | if (avctx->global_quality > 0) |
| 986 | av_log(avctx, AV_LOG_WARNING, "Using global_quality with nvenc is deprecated. Use qp instead.\n"); |
| 987 | |
| 988 | if (ctx->cqp < 0 && avctx->global_quality > 0) |
| 989 | ctx->cqp = avctx->global_quality; |
| 990 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 991 | if (avctx->bit_rate > 0) { |
| 992 | ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate; |
| 993 | } else if (ctx->encode_config.rcParams.averageBitRate > 0) { |
| 994 | ctx->encode_config.rcParams.maxBitRate = ctx->encode_config.rcParams.averageBitRate; |
| 995 | } |
| 996 | |
| 997 | if (avctx->rc_max_rate > 0) |
| 998 | ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate; |
| 999 | |
Roman Arzumanyan | 9115d77 | 2020-03-19 08:35:29 | [diff] [blame] | 1000 | #ifdef NVENC_HAVE_MULTIPASS |
| 1001 | ctx->encode_config.rcParams.multiPass = ctx->multipass; |
Roman Arzumanyan | 9115d77 | 2020-03-19 08:35:29 | [diff] [blame] | 1002 | |
Timo Rothenpieler | e0c8e51 | 2020-10-21 16:45:52 | [diff] [blame] | 1003 | if (ctx->flags & NVENC_ONE_PASS) |
| 1004 | ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED; |
Timo Rothenpieler | 91b8e00 | 2020-10-30 16:26:32 | [diff] [blame] | 1005 | if (ctx->flags & NVENC_TWO_PASSES || ctx->twopass > 0) |
Timo Rothenpieler | e0c8e51 | 2020-10-21 16:45:52 | [diff] [blame] | 1006 | ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION; |
| 1007 | |
| 1008 | if (ctx->rc < 0) { |
| 1009 | if (ctx->cbr) { |
| 1010 | ctx->rc = NV_ENC_PARAMS_RC_CBR; |
| 1011 | } else if (ctx->cqp >= 0) { |
| 1012 | ctx->rc = NV_ENC_PARAMS_RC_CONSTQP; |
Timo Rothenpieler | d5b0a8e | 2020-10-30 16:21:13 | [diff] [blame] | 1013 | } else if (ctx->quality >= 0.0f) { |
Timo Rothenpieler | e0c8e51 | 2020-10-21 16:45:52 | [diff] [blame] | 1014 | ctx->rc = NV_ENC_PARAMS_RC_VBR; |
| 1015 | } |
| 1016 | } |
| 1017 | #else |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1018 | if (ctx->rc < 0) { |
| 1019 | if (ctx->flags & NVENC_ONE_PASS) |
| 1020 | ctx->twopass = 0; |
| 1021 | if (ctx->flags & NVENC_TWO_PASSES) |
| 1022 | ctx->twopass = 1; |
| 1023 | |
| 1024 | if (ctx->twopass < 0) |
| 1025 | ctx->twopass = (ctx->flags & NVENC_LOWLATENCY) != 0; |
| 1026 | |
| 1027 | if (ctx->cbr) { |
| 1028 | if (ctx->twopass) { |
Timo Rothenpieler | cfbebe9 | 2017-05-09 11:57:39 | [diff] [blame] | 1029 | ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1030 | } else { |
| 1031 | ctx->rc = NV_ENC_PARAMS_RC_CBR; |
| 1032 | } |
Timo Rothenpieler | 7fb2a7a | 2017-03-23 16:01:40 | [diff] [blame] | 1033 | } else if (ctx->cqp >= 0) { |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1034 | ctx->rc = NV_ENC_PARAMS_RC_CONSTQP; |
| 1035 | } else if (ctx->twopass) { |
Timo Rothenpieler | cfbebe9 | 2017-05-09 11:57:39 | [diff] [blame] | 1036 | ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1037 | } else if (avctx->qmin >= 0 && avctx->qmax >= 0) { |
| 1038 | ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP; |
| 1039 | } |
| 1040 | } |
Timo Rothenpieler | e0c8e51 | 2020-10-21 16:45:52 | [diff] [blame] | 1041 | #endif |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1042 | |
Timo Rothenpieler | cfbebe9 | 2017-05-09 11:57:39 | [diff] [blame] | 1043 | if (ctx->rc >= 0 && ctx->rc & RC_MODE_DEPRECATED) { |
| 1044 | av_log(avctx, AV_LOG_WARNING, "Specified rc mode is deprecated.\n"); |
Timo Rothenpieler | cde3c08 | 2020-10-21 16:17:08 | [diff] [blame] | 1045 | av_log(avctx, AV_LOG_WARNING, "Use -rc constqp/cbr/vbr, -tune and -multipass instead.\n"); |
Timo Rothenpieler | cfbebe9 | 2017-05-09 11:57:39 | [diff] [blame] | 1046 | |
| 1047 | ctx->rc &= ~RC_MODE_DEPRECATED; |
| 1048 | } |
| 1049 | |
Timo Rothenpieler | 00b5798 | 2021-08-09 13:16:58 | [diff] [blame] | 1050 | #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS |
| 1051 | ctx->encode_config.rcParams.cbQPIndexOffset = ctx->qp_cb_offset; |
| 1052 | ctx->encode_config.rcParams.crQPIndexOffset = ctx->qp_cr_offset; |
| 1053 | #else |
| 1054 | if (ctx->qp_cb_offset || ctx->qp_cr_offset) |
| 1055 | av_log(avctx, AV_LOG_WARNING, "Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n"); |
| 1056 | #endif |
| 1057 | |
Roman Arzumanyan | 9115d77 | 2020-03-19 08:35:29 | [diff] [blame] | 1058 | #ifdef NVENC_HAVE_LDKFS |
| 1059 | if (ctx->ldkfs) |
| 1060 | ctx->encode_config.rcParams.lowDelayKeyFrameScale = ctx->ldkfs; |
| 1061 | #endif |
| 1062 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1063 | if (ctx->flags & NVENC_LOSSLESS) { |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1064 | set_lossless(avctx); |
Timo Rothenpieler | 1330a0f | 2016-05-31 14:53:38 | [diff] [blame] | 1065 | } else if (ctx->rc >= 0) { |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1066 | nvenc_override_rate_control(avctx); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1067 | } else { |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1068 | ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR; |
| 1069 | set_vbr(avctx); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | if (avctx->rc_buffer_size > 0) { |
| 1073 | ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size; |
| 1074 | } else if (ctx->encode_config.rcParams.averageBitRate > 0) { |
pkviet | 1553751 | 2018-05-03 00:15:52 | [diff] [blame] | 1075 | avctx->rc_buffer_size = ctx->encode_config.rcParams.vbvBufferSize = 2 * ctx->encode_config.rcParams.averageBitRate; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1076 | } |
Oliver Collyer | a81b398 | 2016-08-25 15:20:03 | [diff] [blame] | 1077 | |
Yogender Gupta | facc19e | 2016-09-24 15:55:00 | [diff] [blame] | 1078 | if (ctx->aq) { |
| 1079 | ctx->encode_config.rcParams.enableAQ = 1; |
| 1080 | ctx->encode_config.rcParams.aqStrength = ctx->aq_strength; |
| 1081 | av_log(avctx, AV_LOG_VERBOSE, "AQ enabled.\n"); |
Oliver Collyer | a81b398 | 2016-08-25 15:20:03 | [diff] [blame] | 1082 | } |
Yogender Gupta | facc19e | 2016-09-24 15:55:00 | [diff] [blame] | 1083 | |
| 1084 | if (ctx->temporal_aq) { |
| 1085 | ctx->encode_config.rcParams.enableTemporalAQ = 1; |
| 1086 | av_log(avctx, AV_LOG_VERBOSE, "Temporal AQ enabled.\n"); |
| 1087 | } |
| 1088 | |
Ruta Gadkari | 67db4ff | 2016-12-26 07:49:16 | [diff] [blame] | 1089 | if (ctx->rc_lookahead > 0) { |
Yogender Gupta | facc19e | 2016-09-24 15:55:00 | [diff] [blame] | 1090 | int lkd_bound = FFMIN(ctx->nb_surfaces, ctx->async_depth) - |
| 1091 | ctx->encode_config.frameIntervalP - 4; |
| 1092 | |
| 1093 | if (lkd_bound < 0) { |
Timo Rothenpieler | ac0f42d | 2022-12-05 20:10:36 | [diff] [blame] | 1094 | ctx->encode_config.rcParams.enableLookahead = 0; |
Yogender Gupta | facc19e | 2016-09-24 15:55:00 | [diff] [blame] | 1095 | av_log(avctx, AV_LOG_WARNING, |
| 1096 | "Lookahead not enabled. Increase buffer delay (-delay).\n"); |
| 1097 | } else { |
| 1098 | ctx->encode_config.rcParams.enableLookahead = 1; |
| 1099 | ctx->encode_config.rcParams.lookaheadDepth = av_clip(ctx->rc_lookahead, 0, lkd_bound); |
| 1100 | ctx->encode_config.rcParams.disableIadapt = ctx->no_scenecut; |
| 1101 | ctx->encode_config.rcParams.disableBadapt = !ctx->b_adapt; |
| 1102 | av_log(avctx, AV_LOG_VERBOSE, |
| 1103 | "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n", |
| 1104 | ctx->encode_config.rcParams.lookaheadDepth, |
| 1105 | ctx->encode_config.rcParams.disableIadapt ? "disabled" : "enabled", |
| 1106 | ctx->encode_config.rcParams.disableBadapt ? "disabled" : "enabled"); |
Timo Rothenpieler | 45216e3 | 2022-12-05 20:18:42 | [diff] [blame] | 1107 | if (ctx->encode_config.rcParams.lookaheadDepth < ctx->rc_lookahead) |
| 1108 | av_log(avctx, AV_LOG_WARNING, "Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay", |
| 1109 | ctx->encode_config.rcParams.lookaheadDepth, ctx->rc_lookahead); |
Yogender Gupta | facc19e | 2016-09-24 15:55:00 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | if (ctx->strict_gop) { |
| 1114 | ctx->encode_config.rcParams.strictGOPTarget = 1; |
| 1115 | av_log(avctx, AV_LOG_VERBOSE, "Strict GOP target enabled.\n"); |
| 1116 | } |
| 1117 | |
| 1118 | if (ctx->nonref_p) |
| 1119 | ctx->encode_config.rcParams.enableNonRefP = 1; |
| 1120 | |
| 1121 | if (ctx->zerolatency) |
| 1122 | ctx->encode_config.rcParams.zeroReorderDelay = 1; |
| 1123 | |
Roman Arzumanyan | 0842fd2 | 2020-04-20 10:53:36 | [diff] [blame] | 1124 | if (ctx->quality) { |
Ben Chang | 18a659d | 2017-05-10 06:41:17 | [diff] [blame] | 1125 | //convert from float to fixed point 8.8 |
| 1126 | int tmp_quality = (int)(ctx->quality * 256.0f); |
| 1127 | ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8); |
| 1128 | ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff); |
Roman Arzumanyan | 0842fd2 | 2020-04-20 10:53:36 | [diff] [blame] | 1129 | |
| 1130 | av_log(avctx, AV_LOG_VERBOSE, "CQ(%d) mode enabled.\n", tmp_quality); |
| 1131 | |
Timo Rothenpieler | 9ca139b | 2022-12-08 11:31:00 | [diff] [blame] | 1132 | // CQ mode shall discard avg bitrate/vbv buffer size and honor only max bitrate |
Roman Arzumanyan | 470bbf6 | 2020-06-03 13:12:12 | [diff] [blame] | 1133 | ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate = 0; |
Timo Rothenpieler | 9ca139b | 2022-12-08 11:31:00 | [diff] [blame] | 1134 | ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size = 0; |
Roman Arzumanyan | 470bbf6 | 2020-06-03 13:12:12 | [diff] [blame] | 1135 | ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate; |
Ben Chang | 18a659d | 2017-05-10 06:41:17 | [diff] [blame] | 1136 | } |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1137 | } |
| 1138 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1139 | static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx) |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1140 | { |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1141 | NvencContext *ctx = avctx->priv_data; |
| 1142 | NV_ENC_CONFIG *cc = &ctx->encode_config; |
| 1143 | NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config; |
| 1144 | NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1145 | |
Timo Rothenpieler | b7106987 | 2022-08-06 18:58:48 | [diff] [blame] | 1146 | const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt); |
| 1147 | |
| 1148 | if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) { |
| 1149 | vui->colourMatrix = AVCOL_SPC_BT470BG; |
| 1150 | vui->colourPrimaries = avctx->color_primaries; |
| 1151 | vui->transferCharacteristics = avctx->color_trc; |
| 1152 | vui->videoFullRangeFlag = 0; |
| 1153 | } else { |
| 1154 | vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace; |
| 1155 | vui->colourPrimaries = avctx->color_primaries; |
| 1156 | vui->transferCharacteristics = avctx->color_trc; |
| 1157 | vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG |
| 1158 | || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); |
| 1159 | } |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1160 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1161 | vui->colourDescriptionPresentFlag = |
Timo Rothenpieler | 7555d6f | 2021-03-30 20:08:49 | [diff] [blame] | 1162 | (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1163 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1164 | vui->videoSignalTypePresentFlag = |
| 1165 | (vui->colourDescriptionPresentFlag |
| 1166 | || vui->videoFormat != 5 |
| 1167 | || vui->videoFullRangeFlag != 0); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1168 | |
Aleksoid | 7eb0d9e | 2023-07-27 02:26:23 | [diff] [blame] | 1169 | if (ctx->max_slice_size > 0) { |
| 1170 | h264->sliceMode = 1; |
| 1171 | h264->sliceModeData = ctx->max_slice_size; |
| 1172 | } else { |
| 1173 | h264->sliceMode = 3; |
| 1174 | h264->sliceModeData = avctx->slices > 0 ? avctx->slices : 1; |
| 1175 | } |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1176 | |
Limin Wang | e6bd517 | 2021-09-06 02:01:58 | [diff] [blame] | 1177 | if (ctx->intra_refresh) { |
| 1178 | h264->enableIntraRefresh = 1; |
Ricardo Monteiro | 99dfdb4 | 2023-06-22 10:18:19 | [diff] [blame] | 1179 | h264->intraRefreshPeriod = cc->gopLength; |
| 1180 | h264->intraRefreshCnt = cc->gopLength - 1; |
Zhao Zhili | 0d0c398 | 2023-07-06 12:00:48 | [diff] [blame] | 1181 | cc->gopLength = NVENC_INFINITE_GOPLENGTH; |
Limin Wang | 3756525 | 2021-09-02 10:38:57 | [diff] [blame] | 1182 | #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH |
| 1183 | h264->singleSliceIntraRefresh = ctx->single_slice_intra_refresh; |
| 1184 | #endif |
Limin Wang | e6bd517 | 2021-09-06 02:01:58 | [diff] [blame] | 1185 | } |
| 1186 | |
Limin Wang | 75001ae | 2021-09-02 10:38:58 | [diff] [blame] | 1187 | if (ctx->constrained_encoding) |
| 1188 | h264->enableConstrainedEncoding = 1; |
| 1189 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1190 | h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1191 | h264->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1; |
Miroslav Slugen | 1841eda | 2016-12-30 21:02:42 | [diff] [blame] | 1192 | h264->outputAUD = ctx->aud; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1193 | |
Timo Rothenpieler | e929b2f | 2019-09-27 15:55:23 | [diff] [blame] | 1194 | if (ctx->dpb_size >= 0) { |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1195 | /* 0 means "let the hardware decide" */ |
Timo Rothenpieler | e929b2f | 2019-09-27 15:55:23 | [diff] [blame] | 1196 | h264->maxNumRefFrames = ctx->dpb_size; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1197 | } |
Limin Wang | e6bd517 | 2021-09-06 02:01:58 | [diff] [blame] | 1198 | |
Zhao Zhili | 7eeef72 | 2023-07-06 19:57:38 | [diff] [blame] | 1199 | h264->idrPeriod = cc->gopLength; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1200 | |
| 1201 | if (IS_CBR(cc->rcParams.rateControlMode)) { |
| 1202 | h264->outputBufferingPeriodSEI = 1; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1203 | } |
| 1204 | |
Timo Rothenpieler | 4e6638a | 2017-09-02 13:39:24 | [diff] [blame] | 1205 | h264->outputPictureTimingSEI = 1; |
| 1206 | |
Timo Rothenpieler | d2b46c1 | 2023-06-01 21:46:46 | [diff] [blame] | 1207 | #ifndef NVENC_NO_DEPRECATED_RC |
Timo Rothenpieler | cfbebe9 | 2017-05-09 11:57:39 | [diff] [blame] | 1208 | if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || |
| 1209 | cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ || |
| 1210 | cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) { |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1211 | h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE; |
| 1212 | h264->fmoMode = NV_ENC_H264_FMO_DISABLE; |
| 1213 | } |
Timo Rothenpieler | d2b46c1 | 2023-06-01 21:46:46 | [diff] [blame] | 1214 | #endif |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1215 | |
Andrey Turkin | 9824321 | 2016-05-25 14:26:25 | [diff] [blame] | 1216 | if (ctx->flags & NVENC_LOSSLESS) { |
| 1217 | h264->qpPrimeYZeroTransformBypassFlag = 1; |
| 1218 | } else { |
| 1219 | switch(ctx->profile) { |
| 1220 | case NV_ENC_H264_PROFILE_BASELINE: |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1221 | cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1222 | avctx->profile = AV_PROFILE_H264_BASELINE; |
Andrey Turkin | 9824321 | 2016-05-25 14:26:25 | [diff] [blame] | 1223 | break; |
| 1224 | case NV_ENC_H264_PROFILE_MAIN: |
| 1225 | cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1226 | avctx->profile = AV_PROFILE_H264_MAIN; |
Andrey Turkin | 9824321 | 2016-05-25 14:26:25 | [diff] [blame] | 1227 | break; |
| 1228 | case NV_ENC_H264_PROFILE_HIGH: |
| 1229 | cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1230 | avctx->profile = AV_PROFILE_H264_HIGH; |
Andrey Turkin | 9824321 | 2016-05-25 14:26:25 | [diff] [blame] | 1231 | break; |
| 1232 | case NV_ENC_H264_PROFILE_HIGH_444P: |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1233 | cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1234 | avctx->profile = AV_PROFILE_H264_HIGH_444_PREDICTIVE; |
Andrey Turkin | 9824321 | 2016-05-25 14:26:25 | [diff] [blame] | 1235 | break; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | // force setting profile as high444p if input is AV_PIX_FMT_YUV444P |
Timo Rothenpieler | 7555d6f | 2021-03-30 20:08:49 | [diff] [blame] | 1240 | if (IS_YUV444(ctx->data_pix_fmt)) { |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1241 | cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1242 | avctx->profile = AV_PROFILE_H264_HIGH_444_PREDICTIVE; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1243 | } |
| 1244 | |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1245 | h264->chromaFormatIDC = avctx->profile == AV_PROFILE_H264_HIGH_444_PREDICTIVE ? 3 : 1; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1246 | |
Andrey Turkin | b017287 | 2016-05-25 15:00:52 | [diff] [blame] | 1247 | h264->level = ctx->level; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1248 | |
Timo Rothenpieler | a0b69e2 | 2017-08-30 19:06:25 | [diff] [blame] | 1249 | if (ctx->coder >= 0) |
| 1250 | h264->entropyCodingMode = ctx->coder; |
| 1251 | |
Timo Rothenpieler | 86e9dba | 2018-04-11 12:22:20 | [diff] [blame] | 1252 | #ifdef NVENC_HAVE_BFRAME_REF_MODE |
Timo Rothenpieler | 28680e2 | 2022-11-02 18:02:56 | [diff] [blame] | 1253 | if (ctx->b_ref_mode >= 0) |
| 1254 | h264->useBFramesAsRef = ctx->b_ref_mode; |
Timo Rothenpieler | 86e9dba | 2018-04-11 12:22:20 | [diff] [blame] | 1255 | #endif |
| 1256 | |
Roman Arzumanyan | 567b5e3 | 2019-09-27 15:56:11 | [diff] [blame] | 1257 | #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES |
| 1258 | h264->numRefL0 = avctx->refs; |
| 1259 | h264->numRefL1 = avctx->refs; |
| 1260 | #endif |
| 1261 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx) |
| 1266 | { |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1267 | NvencContext *ctx = avctx->priv_data; |
| 1268 | NV_ENC_CONFIG *cc = &ctx->encode_config; |
| 1269 | NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig; |
| 1270 | NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1271 | |
Timo Rothenpieler | b7106987 | 2022-08-06 18:58:48 | [diff] [blame] | 1272 | const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt); |
| 1273 | |
| 1274 | if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) { |
| 1275 | vui->colourMatrix = AVCOL_SPC_BT470BG; |
| 1276 | vui->colourPrimaries = avctx->color_primaries; |
| 1277 | vui->transferCharacteristics = avctx->color_trc; |
| 1278 | vui->videoFullRangeFlag = 0; |
| 1279 | } else { |
| 1280 | vui->colourMatrix = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace; |
| 1281 | vui->colourPrimaries = avctx->color_primaries; |
| 1282 | vui->transferCharacteristics = avctx->color_trc; |
| 1283 | vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG |
| 1284 | || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); |
| 1285 | } |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1286 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1287 | vui->colourDescriptionPresentFlag = |
Timo Rothenpieler | 7555d6f | 2021-03-30 20:08:49 | [diff] [blame] | 1288 | (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1289 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1290 | vui->videoSignalTypePresentFlag = |
| 1291 | (vui->colourDescriptionPresentFlag |
| 1292 | || vui->videoFormat != 5 |
| 1293 | || vui->videoFullRangeFlag != 0); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1294 | |
Aleksoid | 7eb0d9e | 2023-07-27 02:26:23 | [diff] [blame] | 1295 | if (ctx->max_slice_size > 0) { |
| 1296 | hevc->sliceMode = 1; |
| 1297 | hevc->sliceModeData = ctx->max_slice_size; |
| 1298 | } else { |
| 1299 | hevc->sliceMode = 3; |
| 1300 | hevc->sliceModeData = avctx->slices > 0 ? avctx->slices : 1; |
| 1301 | } |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1302 | |
Limin Wang | e6bd517 | 2021-09-06 02:01:58 | [diff] [blame] | 1303 | if (ctx->intra_refresh) { |
| 1304 | hevc->enableIntraRefresh = 1; |
Ricardo Monteiro | 99dfdb4 | 2023-06-22 10:18:19 | [diff] [blame] | 1305 | hevc->intraRefreshPeriod = cc->gopLength; |
| 1306 | hevc->intraRefreshCnt = cc->gopLength - 1; |
Zhao Zhili | 0d0c398 | 2023-07-06 12:00:48 | [diff] [blame] | 1307 | cc->gopLength = NVENC_INFINITE_GOPLENGTH; |
Limin Wang | 3756525 | 2021-09-02 10:38:57 | [diff] [blame] | 1308 | #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH |
| 1309 | hevc->singleSliceIntraRefresh = ctx->single_slice_intra_refresh; |
| 1310 | #endif |
Limin Wang | e6bd517 | 2021-09-06 02:01:58 | [diff] [blame] | 1311 | } |
| 1312 | |
Limin Wang | 75001ae | 2021-09-02 10:38:58 | [diff] [blame] | 1313 | #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING |
| 1314 | if (ctx->constrained_encoding) |
| 1315 | hevc->enableConstrainedEncoding = 1; |
| 1316 | #endif |
| 1317 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1318 | hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1319 | hevc->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1; |
Miroslav Slugen | 1841eda | 2016-12-30 21:02:42 | [diff] [blame] | 1320 | hevc->outputAUD = ctx->aud; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1321 | |
Timo Rothenpieler | e929b2f | 2019-09-27 15:55:23 | [diff] [blame] | 1322 | if (ctx->dpb_size >= 0) { |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1323 | /* 0 means "let the hardware decide" */ |
Timo Rothenpieler | e929b2f | 2019-09-27 15:55:23 | [diff] [blame] | 1324 | hevc->maxNumRefFramesInDPB = ctx->dpb_size; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1325 | } |
Limin Wang | e6bd517 | 2021-09-06 02:01:58 | [diff] [blame] | 1326 | |
Zhao Zhili | 7eeef72 | 2023-07-06 19:57:38 | [diff] [blame] | 1327 | hevc->idrPeriod = cc->gopLength; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1328 | |
| 1329 | if (IS_CBR(cc->rcParams.rateControlMode)) { |
| 1330 | hevc->outputBufferingPeriodSEI = 1; |
Andrey Turkin | f84dfbc | 2016-05-25 16:39:54 | [diff] [blame] | 1331 | } |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1332 | |
Timo Rothenpieler | 4e6638a | 2017-09-02 13:39:24 | [diff] [blame] | 1333 | hevc->outputPictureTimingSEI = 1; |
| 1334 | |
Clément Bœsch | b7cc4eb | 2017-03-20 22:04:28 | [diff] [blame] | 1335 | switch (ctx->profile) { |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 1336 | case NV_ENC_HEVC_PROFILE_MAIN: |
| 1337 | cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1338 | avctx->profile = AV_PROFILE_HEVC_MAIN; |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 1339 | break; |
| 1340 | case NV_ENC_HEVC_PROFILE_MAIN_10: |
| 1341 | cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1342 | avctx->profile = AV_PROFILE_HEVC_MAIN_10; |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 1343 | break; |
Timo Rothenpieler | 033f98c | 2016-09-28 12:21:03 | [diff] [blame] | 1344 | case NV_ENC_HEVC_PROFILE_REXT: |
| 1345 | cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1346 | avctx->profile = AV_PROFILE_HEVC_REXT; |
Timo Rothenpieler | 033f98c | 2016-09-28 12:21:03 | [diff] [blame] | 1347 | break; |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | // force setting profile as main10 if input is 10 bit |
| 1351 | if (IS_10BIT(ctx->data_pix_fmt)) { |
| 1352 | cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1353 | avctx->profile = AV_PROFILE_HEVC_MAIN_10; |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 1354 | } |
| 1355 | |
Timo Rothenpieler | 033f98c | 2016-09-28 12:21:03 | [diff] [blame] | 1356 | // force setting profile as rext if input is yuv444 |
| 1357 | if (IS_YUV444(ctx->data_pix_fmt)) { |
| 1358 | cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1359 | avctx->profile = AV_PROFILE_HEVC_REXT; |
Timo Rothenpieler | 033f98c | 2016-09-28 12:21:03 | [diff] [blame] | 1360 | } |
| 1361 | |
Oliver Collyer | d1bf8a3 | 2016-08-25 15:18:03 | [diff] [blame] | 1362 | hevc->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1; |
| 1363 | |
| 1364 | hevc->pixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1365 | |
Andrey Turkin | b017287 | 2016-05-25 15:00:52 | [diff] [blame] | 1366 | hevc->level = ctx->level; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1367 | |
Andrey Turkin | 40df468 | 2016-05-25 15:06:02 | [diff] [blame] | 1368 | hevc->tier = ctx->tier; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1369 | |
Roman Arzumanyan | f1f66df | 2019-02-14 12:20:25 | [diff] [blame] | 1370 | #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE |
Timo Rothenpieler | 28680e2 | 2022-11-02 18:02:56 | [diff] [blame] | 1371 | if (ctx->b_ref_mode >= 0) |
| 1372 | hevc->useBFramesAsRef = ctx->b_ref_mode; |
Roman Arzumanyan | f1f66df | 2019-02-14 12:20:25 | [diff] [blame] | 1373 | #endif |
| 1374 | |
Roman Arzumanyan | 567b5e3 | 2019-09-27 15:56:11 | [diff] [blame] | 1375 | #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES |
| 1376 | hevc->numRefL0 = avctx->refs; |
| 1377 | hevc->numRefL1 = avctx->refs; |
| 1378 | #endif |
| 1379 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1380 | return 0; |
| 1381 | } |
| 1382 | |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 1383 | #if CONFIG_AV1_NVENC_ENCODER |
| 1384 | static av_cold int nvenc_setup_av1_config(AVCodecContext *avctx) |
| 1385 | { |
| 1386 | NvencContext *ctx = avctx->priv_data; |
| 1387 | NV_ENC_CONFIG *cc = &ctx->encode_config; |
| 1388 | NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config; |
| 1389 | |
| 1390 | const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->data_pix_fmt); |
| 1391 | |
| 1392 | if ((pixdesc->flags & AV_PIX_FMT_FLAG_RGB) && !IS_GBRP(ctx->data_pix_fmt)) { |
| 1393 | av1->matrixCoefficients = AVCOL_SPC_BT470BG; |
| 1394 | av1->colorPrimaries = avctx->color_primaries; |
| 1395 | av1->transferCharacteristics = avctx->color_trc; |
| 1396 | av1->colorRange = 0; |
| 1397 | } else { |
| 1398 | av1->matrixCoefficients = IS_GBRP(ctx->data_pix_fmt) ? AVCOL_SPC_RGB : avctx->colorspace; |
| 1399 | av1->colorPrimaries = avctx->color_primaries; |
| 1400 | av1->transferCharacteristics = avctx->color_trc; |
| 1401 | av1->colorRange = (avctx->color_range == AVCOL_RANGE_JPEG |
| 1402 | || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ420P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ422P || ctx->data_pix_fmt == AV_PIX_FMT_YUVJ444P); |
| 1403 | } |
| 1404 | |
| 1405 | if (IS_YUV444(ctx->data_pix_fmt)) { |
Timo Rothenpieler | 939273d | 2022-11-10 14:26:43 | [diff] [blame] | 1406 | av_log(avctx, AV_LOG_ERROR, "AV1 High Profile not supported, required for 4:4:4 encoding\n"); |
| 1407 | return AVERROR(ENOTSUP); |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 1408 | } else { |
| 1409 | cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID; |
Andreas Rheinhardt | 8238bc0 | 2023-09-02 12:57:41 | [diff] [blame^] | 1410 | avctx->profile = AV_PROFILE_AV1_MAIN; |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | if (ctx->dpb_size >= 0) { |
| 1414 | /* 0 means "let the hardware decide" */ |
| 1415 | av1->maxNumRefFramesInDPB = ctx->dpb_size; |
| 1416 | } |
| 1417 | |
| 1418 | if (ctx->intra_refresh) { |
| 1419 | av1->enableIntraRefresh = 1; |
Ricardo Monteiro | 99dfdb4 | 2023-06-22 10:18:19 | [diff] [blame] | 1420 | av1->intraRefreshPeriod = cc->gopLength; |
| 1421 | av1->intraRefreshCnt = cc->gopLength - 1; |
Zhao Zhili | 0d0c398 | 2023-07-06 12:00:48 | [diff] [blame] | 1422 | cc->gopLength = NVENC_INFINITE_GOPLENGTH; |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 1423 | } |
| 1424 | |
Zhao Zhili | 7eeef72 | 2023-07-06 19:57:38 | [diff] [blame] | 1425 | av1->idrPeriod = cc->gopLength; |
| 1426 | |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 1427 | if (IS_CBR(cc->rcParams.rateControlMode)) { |
| 1428 | av1->enableBitstreamPadding = 1; |
| 1429 | } |
| 1430 | |
| 1431 | if (ctx->tile_cols >= 0) |
| 1432 | av1->numTileColumns = ctx->tile_cols; |
| 1433 | if (ctx->tile_rows >= 0) |
| 1434 | av1->numTileRows = ctx->tile_rows; |
| 1435 | |
| 1436 | av1->outputAnnexBFormat = 0; |
| 1437 | |
| 1438 | av1->level = ctx->level; |
| 1439 | av1->tier = ctx->tier; |
| 1440 | |
| 1441 | av1->enableTimingInfo = ctx->timing_info; |
| 1442 | |
| 1443 | /* mp4 encapsulation requires sequence headers to be present on all keyframes for AV1 */ |
| 1444 | av1->disableSeqHdr = 0; |
| 1445 | av1->repeatSeqHdr = 1; |
| 1446 | |
| 1447 | av1->chromaFormatIDC = IS_YUV444(ctx->data_pix_fmt) ? 3 : 1; |
| 1448 | |
| 1449 | av1->inputPixelBitDepthMinus8 = IS_10BIT(ctx->data_pix_fmt) ? 2 : 0; |
| 1450 | av1->pixelBitDepthMinus8 = (IS_10BIT(ctx->data_pix_fmt) || ctx->highbitdepth) ? 2 : 0; |
| 1451 | |
| 1452 | if (ctx->b_ref_mode >= 0) |
| 1453 | av1->useBFramesAsRef = ctx->b_ref_mode; |
| 1454 | |
| 1455 | av1->numFwdRefs = avctx->refs; |
| 1456 | av1->numBwdRefs = avctx->refs; |
| 1457 | |
| 1458 | return 0; |
| 1459 | } |
| 1460 | #endif |
| 1461 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1462 | static av_cold int nvenc_setup_codec_config(AVCodecContext *avctx) |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1463 | { |
| 1464 | switch (avctx->codec->id) { |
| 1465 | case AV_CODEC_ID_H264: |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1466 | return nvenc_setup_h264_config(avctx); |
| 1467 | case AV_CODEC_ID_HEVC: |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1468 | return nvenc_setup_hevc_config(avctx); |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 1469 | #if CONFIG_AV1_NVENC_ENCODER |
| 1470 | case AV_CODEC_ID_AV1: |
| 1471 | return nvenc_setup_av1_config(avctx); |
| 1472 | #endif |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1473 | /* Earlier switch/case will return if unknown codec is passed. */ |
| 1474 | } |
| 1475 | |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
Miroslav Slugeň | 952421c | 2017-02-12 18:53:58 | [diff] [blame] | 1479 | static void compute_dar(AVCodecContext *avctx, int *dw, int *dh) { |
| 1480 | int sw, sh; |
| 1481 | |
| 1482 | sw = avctx->width; |
| 1483 | sh = avctx->height; |
| 1484 | |
Timo Rothenpieler | e7fbdda | 2022-11-10 12:05:59 | [diff] [blame] | 1485 | #if CONFIG_AV1_NVENC_ENCODER |
| 1486 | if (avctx->codec->id == AV_CODEC_ID_AV1) { |
| 1487 | /* For AV1 we actually need to calculate the render width/height, not the dar */ |
| 1488 | if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0 |
| 1489 | && avctx->sample_aspect_ratio.num != avctx->sample_aspect_ratio.den) |
| 1490 | { |
| 1491 | if (avctx->sample_aspect_ratio.num > avctx->sample_aspect_ratio.den) { |
| 1492 | sw = av_rescale(sw, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den); |
| 1493 | } else { |
| 1494 | sh = av_rescale(sh, avctx->sample_aspect_ratio.den, avctx->sample_aspect_ratio.num); |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | *dw = sw; |
| 1499 | *dh = sh; |
| 1500 | return; |
| 1501 | } |
| 1502 | #endif |
| 1503 | |
Miroslav Slugeň | 952421c | 2017-02-12 18:53:58 | [diff] [blame] | 1504 | if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) { |
| 1505 | sw *= avctx->sample_aspect_ratio.num; |
| 1506 | sh *= avctx->sample_aspect_ratio.den; |
| 1507 | } |
| 1508 | |
| 1509 | av_reduce(dw, dh, sw, sh, 1024 * 1024); |
| 1510 | } |
| 1511 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1512 | static av_cold int nvenc_setup_encoder(AVCodecContext *avctx) |
| 1513 | { |
| 1514 | NvencContext *ctx = avctx->priv_data; |
| 1515 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 1516 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 1517 | |
| 1518 | NV_ENC_PRESET_CONFIG preset_config = { 0 }; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1519 | NVENCSTATUS nv_status = NV_ENC_SUCCESS; |
| 1520 | AVCPBProperties *cpb_props; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1521 | int res = 0; |
| 1522 | int dw, dh; |
| 1523 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1524 | ctx->encode_config.version = NV_ENC_CONFIG_VER; |
| 1525 | ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER; |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1526 | |
| 1527 | ctx->init_encode_params.encodeHeight = avctx->height; |
| 1528 | ctx->init_encode_params.encodeWidth = avctx->width; |
| 1529 | |
| 1530 | ctx->init_encode_params.encodeConfig = &ctx->encode_config; |
| 1531 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1532 | preset_config.version = NV_ENC_PRESET_CONFIG_VER; |
| 1533 | preset_config.presetCfg.version = NV_ENC_CONFIG_VER; |
| 1534 | |
Roman Arzumanyan | 9115d77 | 2020-03-19 08:35:29 | [diff] [blame] | 1535 | #ifdef NVENC_HAVE_NEW_PRESETS |
Timo Rothenpieler | bb6edf6 | 2020-10-29 20:59:37 | [diff] [blame] | 1536 | ctx->init_encode_params.tuningInfo = ctx->tuning_info; |
Roman Arzumanyan | 9115d77 | 2020-03-19 08:35:29 | [diff] [blame] | 1537 | |
Timo Rothenpieler | 988f2e9 | 2021-04-18 09:17:54 | [diff] [blame] | 1538 | if (ctx->flags & NVENC_LOSSLESS) |
| 1539 | ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS; |
| 1540 | else if (ctx->flags & NVENC_LOWLATENCY) |
Timo Rothenpieler | bb6edf6 | 2020-10-29 20:59:37 | [diff] [blame] | 1541 | ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY; |
Timo Rothenpieler | e0c8e51 | 2020-10-21 16:45:52 | [diff] [blame] | 1542 | |
Timo Rothenpieler | bb6edf6 | 2020-10-29 20:59:37 | [diff] [blame] | 1543 | nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(ctx->nvencoder, |
| 1544 | ctx->init_encode_params.encodeGUID, |
| 1545 | ctx->init_encode_params.presetGUID, |
| 1546 | ctx->init_encode_params.tuningInfo, |
| 1547 | &preset_config); |
| 1548 | #else |
Timo Rothenpieler | bb6edf6 | 2020-10-29 20:59:37 | [diff] [blame] | 1549 | nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, |
| 1550 | ctx->init_encode_params.encodeGUID, |
| 1551 | ctx->init_encode_params.presetGUID, |
| 1552 | &preset_config); |
Roman Arzumanyan | 9115d77 | 2020-03-19 08:35:29 | [diff] [blame] | 1553 | #endif |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1554 | if (nv_status != NV_ENC_SUCCESS) |
| 1555 | return nvenc_print_error(avctx, nv_status, "Cannot get the preset configuration"); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1556 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1557 | memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config)); |
Agatha Hu | 4904658 | 2015-09-11 09:07:10 | [diff] [blame] | 1558 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1559 | ctx->encode_config.version = NV_ENC_CONFIG_VER; |
Timo Rothenpieler | fb34c58 | 2015-01-26 12:28:22 | [diff] [blame] | 1560 | |
Miroslav Slugeň | 952421c | 2017-02-12 18:53:58 | [diff] [blame] | 1561 | compute_dar(avctx, &dw, &dh); |
Miroslav Slugeň | f2dd6ae | 2016-11-27 00:46:06 | [diff] [blame] | 1562 | ctx->init_encode_params.darHeight = dh; |
| 1563 | ctx->init_encode_params.darWidth = dw; |
Timo Rothenpieler | fb34c58 | 2015-01-26 12:28:22 | [diff] [blame] | 1564 | |
Zachariah Brown | b18fd2b | 2020-05-14 18:15:33 | [diff] [blame] | 1565 | if (avctx->framerate.num > 0 && avctx->framerate.den > 0) { |
| 1566 | ctx->init_encode_params.frameRateNum = avctx->framerate.num; |
| 1567 | ctx->init_encode_params.frameRateDen = avctx->framerate.den; |
| 1568 | } else { |
| 1569 | ctx->init_encode_params.frameRateNum = avctx->time_base.den; |
Anton Khirnov | 7d1d61c | 2023-05-04 09:50:48 | [diff] [blame] | 1570 | FF_DISABLE_DEPRECATION_WARNINGS |
| 1571 | ctx->init_encode_params.frameRateDen = avctx->time_base.num |
| 1572 | #if FF_API_TICKS_PER_FRAME |
| 1573 | * avctx->ticks_per_frame |
| 1574 | #endif |
| 1575 | ; |
| 1576 | FF_ENABLE_DEPRECATION_WARNINGS |
Zachariah Brown | b18fd2b | 2020-05-14 18:15:33 | [diff] [blame] | 1577 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1578 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1579 | ctx->init_encode_params.enableEncodeAsync = 0; |
| 1580 | ctx->init_encode_params.enablePTD = 1; |
| 1581 | |
Roman Arzumanyan | 9115d77 | 2020-03-19 08:35:29 | [diff] [blame] | 1582 | #ifdef NVENC_HAVE_NEW_PRESETS |
| 1583 | /* If lookahead isn't set from CLI, use value from preset. |
| 1584 | * P6 & P7 presets may enable lookahead for better quality. |
| 1585 | * */ |
| 1586 | if (ctx->rc_lookahead == 0 && ctx->encode_config.rcParams.enableLookahead) |
| 1587 | ctx->rc_lookahead = ctx->encode_config.rcParams.lookaheadDepth; |
Roman Arzumanyan | 9115d77 | 2020-03-19 08:35:29 | [diff] [blame] | 1588 | #endif |
| 1589 | |
Sumit Agarwal | 0177573 | 2017-05-09 14:24:54 | [diff] [blame] | 1590 | if (ctx->weighted_pred == 1) |
| 1591 | ctx->init_encode_params.enableWeightedPrediction = 1; |
| 1592 | |
Miroslav Slugen | 9b425bd | 2016-12-30 21:04:31 | [diff] [blame] | 1593 | if (ctx->bluray_compat) { |
| 1594 | ctx->aud = 1; |
Timo Rothenpieler | e929b2f | 2019-09-27 15:55:23 | [diff] [blame] | 1595 | ctx->dpb_size = FFMIN(FFMAX(avctx->refs, 0), 6); |
Miroslav Slugen | 9b425bd | 2016-12-30 21:04:31 | [diff] [blame] | 1596 | avctx->max_b_frames = FFMIN(avctx->max_b_frames, 3); |
| 1597 | switch (avctx->codec->id) { |
| 1598 | case AV_CODEC_ID_H264: |
| 1599 | /* maximum level depends on used resolution */ |
| 1600 | break; |
| 1601 | case AV_CODEC_ID_HEVC: |
| 1602 | ctx->level = NV_ENC_LEVEL_HEVC_51; |
| 1603 | ctx->tier = NV_ENC_TIER_HEVC_HIGH; |
| 1604 | break; |
| 1605 | } |
| 1606 | } |
| 1607 | |
Timo Rothenpieler | 914fd42 | 2015-01-26 12:28:21 | [diff] [blame] | 1608 | if (avctx->gop_size > 0) { |
Ricardo Monteiro | 99dfdb4 | 2023-06-22 10:18:19 | [diff] [blame] | 1609 | // only overwrite preset if a GOP size was selected as input |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1610 | ctx->encode_config.gopLength = avctx->gop_size; |
Timo Rothenpieler | 914fd42 | 2015-01-26 12:28:21 | [diff] [blame] | 1611 | } else if (avctx->gop_size == 0) { |
| 1612 | ctx->encode_config.frameIntervalP = 0; |
| 1613 | ctx->encode_config.gopLength = 1; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1614 | } |
| 1615 | |
Ricardo Monteiro | 99dfdb4 | 2023-06-22 10:18:19 | [diff] [blame] | 1616 | if (avctx->max_b_frames >= 0 && ctx->encode_config.gopLength > 1) { |
| 1617 | /* 0 is intra-only, 1 is I/P only, 2 is one B-Frame, 3 two B-frames, and so on. */ |
| 1618 | ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1; |
| 1619 | } |
| 1620 | |
Limin Wang | 3756525 | 2021-09-02 10:38:57 | [diff] [blame] | 1621 | /* force to enable intra refresh */ |
| 1622 | if(ctx->single_slice_intra_refresh) |
| 1623 | ctx->intra_refresh = 1; |
| 1624 | |
Miroslav Slugeň | de2faec | 2016-11-21 11:17:43 | [diff] [blame] | 1625 | nvenc_recalc_surfaces(avctx); |
| 1626 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1627 | nvenc_setup_rate_control(avctx); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1628 | |
Vittorio Giovara | 7c6eb0a | 2015-06-29 19:59:37 | [diff] [blame] | 1629 | if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1630 | ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD; |
| 1631 | } else { |
| 1632 | ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME; |
| 1633 | } |
| 1634 | |
Andrey Turkin | faffff8 | 2016-05-25 14:05:50 | [diff] [blame] | 1635 | res = nvenc_setup_codec_config(avctx); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1636 | if (res) |
| 1637 | return res; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1638 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 1639 | res = nvenc_push_context(avctx); |
| 1640 | if (res < 0) |
| 1641 | return res; |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 1642 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1643 | nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params); |
Timo Rothenpieler | 51a2334 | 2019-09-27 16:30:10 | [diff] [blame] | 1644 | if (nv_status != NV_ENC_SUCCESS) { |
| 1645 | nvenc_pop_context(avctx); |
| 1646 | return nvenc_print_error(avctx, nv_status, "InitializeEncoder failed"); |
| 1647 | } |
| 1648 | |
| 1649 | #ifdef NVENC_HAVE_CUSTREAM_PTR |
| 1650 | if (ctx->cu_context) { |
| 1651 | nv_status = p_nvenc->nvEncSetIOCudaStreams(ctx->nvencoder, &ctx->cu_stream, &ctx->cu_stream); |
| 1652 | if (nv_status != NV_ENC_SUCCESS) { |
| 1653 | nvenc_pop_context(avctx); |
| 1654 | return nvenc_print_error(avctx, nv_status, "SetIOCudaStreams failed"); |
| 1655 | } |
| 1656 | } |
| 1657 | #endif |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 1658 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 1659 | res = nvenc_pop_context(avctx); |
| 1660 | if (res < 0) |
| 1661 | return res; |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 1662 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1663 | if (ctx->encode_config.frameIntervalP > 1) |
| 1664 | avctx->has_b_frames = 2; |
| 1665 | |
| 1666 | if (ctx->encode_config.rcParams.averageBitRate > 0) |
| 1667 | avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate; |
| 1668 | |
James Almer | 0231df5 | 2023-09-04 12:39:07 | [diff] [blame] | 1669 | cpb_props = ff_encode_add_cpb_side_data(avctx); |
Anton Khirnov | 1520c6f | 2015-10-03 13:19:10 | [diff] [blame] | 1670 | if (!cpb_props) |
| 1671 | return AVERROR(ENOMEM); |
Hendrik Leppkes | 5fc17ed | 2015-12-17 12:41:29 | [diff] [blame] | 1672 | cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate; |
Anton Khirnov | 1520c6f | 2015-10-03 13:19:10 | [diff] [blame] | 1673 | cpb_props->avg_bitrate = avctx->bit_rate; |
Hendrik Leppkes | 5fc17ed | 2015-12-17 12:41:29 | [diff] [blame] | 1674 | cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize; |
Anton Khirnov | 1520c6f | 2015-10-03 13:19:10 | [diff] [blame] | 1675 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1676 | return 0; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1677 | } |
| 1678 | |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 1679 | static NV_ENC_BUFFER_FORMAT nvenc_map_buffer_format(enum AVPixelFormat pix_fmt) |
| 1680 | { |
| 1681 | switch (pix_fmt) { |
| 1682 | case AV_PIX_FMT_YUV420P: |
| 1683 | return NV_ENC_BUFFER_FORMAT_YV12_PL; |
| 1684 | case AV_PIX_FMT_NV12: |
| 1685 | return NV_ENC_BUFFER_FORMAT_NV12_PL; |
| 1686 | case AV_PIX_FMT_P010: |
Philip Langdale | 6a89cdc | 2018-02-25 17:08:06 | [diff] [blame] | 1687 | case AV_PIX_FMT_P016: |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 1688 | return NV_ENC_BUFFER_FORMAT_YUV420_10BIT; |
Timo Rothenpieler | 7555d6f | 2021-03-30 20:08:49 | [diff] [blame] | 1689 | case AV_PIX_FMT_GBRP: |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 1690 | case AV_PIX_FMT_YUV444P: |
| 1691 | return NV_ENC_BUFFER_FORMAT_YUV444_PL; |
Timo Rothenpieler | 7555d6f | 2021-03-30 20:08:49 | [diff] [blame] | 1692 | case AV_PIX_FMT_GBRP16: |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 1693 | case AV_PIX_FMT_YUV444P16: |
| 1694 | return NV_ENC_BUFFER_FORMAT_YUV444_10BIT; |
| 1695 | case AV_PIX_FMT_0RGB32: |
Timo Rothenpieler | 85bc6b5 | 2022-07-04 23:58:10 | [diff] [blame] | 1696 | case AV_PIX_FMT_RGB32: |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 1697 | return NV_ENC_BUFFER_FORMAT_ARGB; |
| 1698 | case AV_PIX_FMT_0BGR32: |
Timo Rothenpieler | 85bc6b5 | 2022-07-04 23:58:10 | [diff] [blame] | 1699 | case AV_PIX_FMT_BGR32: |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 1700 | return NV_ENC_BUFFER_FORMAT_ABGR; |
Timo Rothenpieler | 85bc6b5 | 2022-07-04 23:58:10 | [diff] [blame] | 1701 | case AV_PIX_FMT_X2RGB10: |
| 1702 | return NV_ENC_BUFFER_FORMAT_ARGB10; |
| 1703 | case AV_PIX_FMT_X2BGR10: |
| 1704 | return NV_ENC_BUFFER_FORMAT_ABGR10; |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 1705 | default: |
| 1706 | return NV_ENC_BUFFER_FORMAT_UNDEFINED; |
| 1707 | } |
| 1708 | } |
| 1709 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1710 | static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx) |
| 1711 | { |
| 1712 | NvencContext *ctx = avctx->priv_data; |
| 1713 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 1714 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 1715 | NvencSurface* tmp_surface = &ctx->surfaces[idx]; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1716 | |
| 1717 | NVENCSTATUS nv_status; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1718 | NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 }; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1719 | allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER; |
| 1720 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 1721 | if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) { |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 1722 | ctx->surfaces[idx].in_ref = av_frame_alloc(); |
| 1723 | if (!ctx->surfaces[idx].in_ref) |
| 1724 | return AVERROR(ENOMEM); |
| 1725 | } else { |
| 1726 | NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 }; |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 1727 | |
| 1728 | ctx->surfaces[idx].format = nvenc_map_buffer_format(ctx->data_pix_fmt); |
| 1729 | if (ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) { |
| 1730 | av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n", |
| 1731 | av_get_pix_fmt_name(ctx->data_pix_fmt)); |
| 1732 | return AVERROR(EINVAL); |
| 1733 | } |
| 1734 | |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 1735 | allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER; |
Timo Rothenpieler | a1652ac | 2017-05-23 09:24:40 | [diff] [blame] | 1736 | allocSurf.width = avctx->width; |
| 1737 | allocSurf.height = avctx->height; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 1738 | allocSurf.bufferFmt = ctx->surfaces[idx].format; |
| 1739 | |
| 1740 | nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf); |
| 1741 | if (nv_status != NV_ENC_SUCCESS) { |
| 1742 | return nvenc_print_error(avctx, nv_status, "CreateInputBuffer failed"); |
| 1743 | } |
| 1744 | |
| 1745 | ctx->surfaces[idx].input_surface = allocSurf.inputBuffer; |
| 1746 | ctx->surfaces[idx].width = allocSurf.width; |
| 1747 | ctx->surfaces[idx].height = allocSurf.height; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1748 | } |
| 1749 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1750 | nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut); |
| 1751 | if (nv_status != NV_ENC_SUCCESS) { |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 1752 | int err = nvenc_print_error(avctx, nv_status, "CreateBitstreamBuffer failed"); |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 1753 | if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11) |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 1754 | p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[idx].input_surface); |
| 1755 | av_frame_free(&ctx->surfaces[idx].in_ref); |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 1756 | return err; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1757 | } |
| 1758 | |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 1759 | ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1760 | |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 1761 | av_fifo_write(ctx->unused_surface_queue, &tmp_surface, 1); |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 1762 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1763 | return 0; |
| 1764 | } |
| 1765 | |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1766 | static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx) |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1767 | { |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1768 | NvencContext *ctx = avctx->priv_data; |
Timo Rothenpieler | 4e93f00 | 2017-11-15 17:33:31 | [diff] [blame] | 1769 | int i, res = 0, res2; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1770 | |
Andreas Rheinhardt | 1ea3650 | 2021-09-14 19:31:53 | [diff] [blame] | 1771 | ctx->surfaces = av_calloc(ctx->nb_surfaces, sizeof(*ctx->surfaces)); |
Andrey Turkin | f052ef3 | 2016-05-29 10:07:34 | [diff] [blame] | 1772 | if (!ctx->surfaces) |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1773 | return AVERROR(ENOMEM); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1774 | |
Timo Rothenpieler | 16fdb48 | 2023-06-16 19:35:45 | [diff] [blame] | 1775 | ctx->frame_data_array = av_calloc(ctx->frame_data_array_nb, sizeof(*ctx->frame_data_array)); |
| 1776 | if (!ctx->frame_data_array) |
| 1777 | return AVERROR(ENOMEM); |
| 1778 | |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 1779 | ctx->timestamp_list = av_fifo_alloc2(ctx->nb_surfaces, sizeof(int64_t), 0); |
| 1780 | if (!ctx->timestamp_list) |
Andrey Turkin | cfb49fc | 2016-05-20 16:13:20 | [diff] [blame] | 1781 | return AVERROR(ENOMEM); |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 1782 | |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 1783 | ctx->unused_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0); |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 1784 | if (!ctx->unused_surface_queue) |
| 1785 | return AVERROR(ENOMEM); |
| 1786 | |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 1787 | ctx->output_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0); |
Andrey Turkin | cfb49fc | 2016-05-20 16:13:20 | [diff] [blame] | 1788 | if (!ctx->output_surface_queue) |
| 1789 | return AVERROR(ENOMEM); |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 1790 | ctx->output_surface_ready_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0); |
Andrey Turkin | cfb49fc | 2016-05-20 16:13:20 | [diff] [blame] | 1791 | if (!ctx->output_surface_ready_queue) |
| 1792 | return AVERROR(ENOMEM); |
| 1793 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 1794 | res = nvenc_push_context(avctx); |
| 1795 | if (res < 0) |
| 1796 | return res; |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 1797 | |
Andrey Turkin | f052ef3 | 2016-05-29 10:07:34 | [diff] [blame] | 1798 | for (i = 0; i < ctx->nb_surfaces; i++) { |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1799 | if ((res = nvenc_alloc_surface(avctx, i)) < 0) |
Timo Rothenpieler | 4e93f00 | 2017-11-15 17:33:31 | [diff] [blame] | 1800 | goto fail; |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 1801 | } |
| 1802 | |
Timo Rothenpieler | 4e93f00 | 2017-11-15 17:33:31 | [diff] [blame] | 1803 | fail: |
| 1804 | res2 = nvenc_pop_context(avctx); |
| 1805 | if (res2 < 0) |
| 1806 | return res2; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1807 | |
Timo Rothenpieler | 4e93f00 | 2017-11-15 17:33:31 | [diff] [blame] | 1808 | return res; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | static av_cold int nvenc_setup_extradata(AVCodecContext *avctx) |
| 1812 | { |
| 1813 | NvencContext *ctx = avctx->priv_data; |
| 1814 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 1815 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 1816 | |
| 1817 | NVENCSTATUS nv_status; |
| 1818 | uint32_t outSize = 0; |
Timo Rothenpieler | eb1e359 | 2022-11-15 00:32:56 | [diff] [blame] | 1819 | char tmpHeader[NV_MAX_SEQ_HDR_LEN]; |
| 1820 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1821 | NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 }; |
| 1822 | payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER; |
| 1823 | |
| 1824 | payload.spsppsBuffer = tmpHeader; |
| 1825 | payload.inBufferSize = sizeof(tmpHeader); |
| 1826 | payload.outSPSPPSPayloadSize = &outSize; |
| 1827 | |
| 1828 | nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload); |
| 1829 | if (nv_status != NV_ENC_SUCCESS) { |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 1830 | return nvenc_print_error(avctx, nv_status, "GetSequenceParams failed"); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | avctx->extradata_size = outSize; |
| 1834 | avctx->extradata = av_mallocz(outSize + AV_INPUT_BUFFER_PADDING_SIZE); |
| 1835 | |
| 1836 | if (!avctx->extradata) { |
| 1837 | return AVERROR(ENOMEM); |
| 1838 | } |
| 1839 | |
| 1840 | memcpy(avctx->extradata, tmpHeader, outSize); |
| 1841 | |
| 1842 | return 0; |
| 1843 | } |
| 1844 | |
Andrey Turkin | 7aa16d5 | 2016-05-25 13:04:28 | [diff] [blame] | 1845 | av_cold int ff_nvenc_encode_close(AVCodecContext *avctx) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1846 | { |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1847 | NvencContext *ctx = avctx->priv_data; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1848 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 1849 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 1850 | int i, res; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1851 | |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1852 | /* the encoder has to be flushed before it can be closed */ |
| 1853 | if (ctx->nvencoder) { |
| 1854 | NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER, |
| 1855 | .encodePicFlags = NV_ENC_PIC_FLAG_EOS }; |
| 1856 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 1857 | res = nvenc_push_context(avctx); |
| 1858 | if (res < 0) |
| 1859 | return res; |
Timo Rothenpieler | 0e995ea | 2017-08-30 19:12:23 | [diff] [blame] | 1860 | |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1861 | p_nvenc->nvEncEncodePicture(ctx->nvencoder, ¶ms); |
| 1862 | } |
| 1863 | |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 1864 | av_fifo_freep2(&ctx->timestamp_list); |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 1865 | av_fifo_freep2(&ctx->output_surface_ready_queue); |
| 1866 | av_fifo_freep2(&ctx->output_surface_queue); |
| 1867 | av_fifo_freep2(&ctx->unused_surface_queue); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1868 | |
Timo Rothenpieler | 16fdb48 | 2023-06-16 19:35:45 | [diff] [blame] | 1869 | if (ctx->frame_data_array) { |
| 1870 | for (i = 0; i < ctx->nb_surfaces; i++) |
| 1871 | av_buffer_unref(&ctx->frame_data_array[i].frame_opaque_ref); |
| 1872 | av_freep(&ctx->frame_data_array); |
| 1873 | } |
| 1874 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 1875 | if (ctx->surfaces && (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11)) { |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 1876 | for (i = 0; i < ctx->nb_registered_frames; i++) { |
Timo Rothenpieler | bbe1b21 | 2018-01-26 19:16:53 | [diff] [blame] | 1877 | if (ctx->registered_frames[i].mapped) |
| 1878 | p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[i].in_map.mappedResource); |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 1879 | if (ctx->registered_frames[i].regptr) |
| 1880 | p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr); |
| 1881 | } |
| 1882 | ctx->nb_registered_frames = 0; |
| 1883 | } |
| 1884 | |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1885 | if (ctx->surfaces) { |
Andrey Turkin | f052ef3 | 2016-05-29 10:07:34 | [diff] [blame] | 1886 | for (i = 0; i < ctx->nb_surfaces; ++i) { |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 1887 | if (avctx->pix_fmt != AV_PIX_FMT_CUDA && avctx->pix_fmt != AV_PIX_FMT_D3D11) |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1888 | p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[i].input_surface); |
| 1889 | av_frame_free(&ctx->surfaces[i].in_ref); |
| 1890 | p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->surfaces[i].output_surface); |
| 1891 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1892 | } |
Andrey Turkin | cfb49fc | 2016-05-20 16:13:20 | [diff] [blame] | 1893 | av_freep(&ctx->surfaces); |
Andrey Turkin | f052ef3 | 2016-05-29 10:07:34 | [diff] [blame] | 1894 | ctx->nb_surfaces = 0; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1895 | |
James Almer | 827d6fe | 2020-06-09 21:31:32 | [diff] [blame] | 1896 | av_frame_free(&ctx->frame); |
| 1897 | |
Brad Hards | 63948a6 | 2021-05-25 10:11:57 | [diff] [blame] | 1898 | av_freep(&ctx->sei_data); |
| 1899 | |
Timo Rothenpieler | 0e995ea | 2017-08-30 19:12:23 | [diff] [blame] | 1900 | if (ctx->nvencoder) { |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1901 | p_nvenc->nvEncDestroyEncoder(ctx->nvencoder); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1902 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 1903 | res = nvenc_pop_context(avctx); |
| 1904 | if (res < 0) |
| 1905 | return res; |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 1906 | } |
Timo Rothenpieler | 0e995ea | 2017-08-30 19:12:23 | [diff] [blame] | 1907 | ctx->nvencoder = NULL; |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 1908 | |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 1909 | if (ctx->cu_context_internal) |
Philip Langdale | 19d3d0c | 2018-11-11 06:47:28 | [diff] [blame] | 1910 | CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal)); |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 1911 | ctx->cu_context = ctx->cu_context_internal = NULL; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1912 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 1913 | #if CONFIG_D3D11VA |
| 1914 | if (ctx->d3d11_device) { |
| 1915 | ID3D11Device_Release(ctx->d3d11_device); |
| 1916 | ctx->d3d11_device = NULL; |
| 1917 | } |
| 1918 | #endif |
| 1919 | |
Timo Rothenpieler | a66835b | 2016-10-10 10:55:59 | [diff] [blame] | 1920 | nvenc_free_functions(&dl_fn->nvenc_dl); |
| 1921 | cuda_free_functions(&dl_fn->cuda_dl); |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1922 | |
| 1923 | dl_fn->nvenc_device_count = 0; |
| 1924 | |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1925 | av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n"); |
| 1926 | |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
| 1930 | av_cold int ff_nvenc_encode_init(AVCodecContext *avctx) |
| 1931 | { |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 1932 | NvencContext *ctx = avctx->priv_data; |
| 1933 | int ret; |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1934 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 1935 | if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) { |
Timo Rothenpieler | 2e700b0 | 2018-05-07 20:39:20 | [diff] [blame] | 1936 | AVHWFramesContext *frames_ctx; |
| 1937 | if (!avctx->hw_frames_ctx) { |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 1938 | av_log(avctx, AV_LOG_ERROR, |
Timo Rothenpieler | 2e700b0 | 2018-05-07 20:39:20 | [diff] [blame] | 1939 | "hw_frames_ctx must be set when using GPU frames as input\n"); |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 1940 | return AVERROR(EINVAL); |
| 1941 | } |
Timo Rothenpieler | 2e700b0 | 2018-05-07 20:39:20 | [diff] [blame] | 1942 | frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; |
| 1943 | if (frames_ctx->format != avctx->pix_fmt) { |
| 1944 | av_log(avctx, AV_LOG_ERROR, |
| 1945 | "hw_frames_ctx must match the GPU frame type\n"); |
| 1946 | return AVERROR(EINVAL); |
| 1947 | } |
| 1948 | ctx->data_pix_fmt = frames_ctx->sw_format; |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 1949 | } else { |
| 1950 | ctx->data_pix_fmt = avctx->pix_fmt; |
| 1951 | } |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1952 | |
James Almer | 827d6fe | 2020-06-09 21:31:32 | [diff] [blame] | 1953 | ctx->frame = av_frame_alloc(); |
| 1954 | if (!ctx->frame) |
| 1955 | return AVERROR(ENOMEM); |
| 1956 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 1957 | if ((ret = nvenc_load_libraries(avctx)) < 0) |
| 1958 | return ret; |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1959 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 1960 | if ((ret = nvenc_setup_device(avctx)) < 0) |
| 1961 | return ret; |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1962 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 1963 | if ((ret = nvenc_setup_encoder(avctx)) < 0) |
| 1964 | return ret; |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1965 | |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 1966 | if ((ret = nvenc_setup_surfaces(avctx)) < 0) |
| 1967 | return ret; |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1968 | |
| 1969 | if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) { |
Andrey Turkin | 0d021cc | 2016-05-29 12:23:26 | [diff] [blame] | 1970 | if ((ret = nvenc_setup_extradata(avctx)) < 0) |
| 1971 | return ret; |
Andrey Turkin | b693353 | 2016-05-25 16:57:11 | [diff] [blame] | 1972 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1973 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1974 | return 0; |
| 1975 | } |
| 1976 | |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 1977 | static NvencSurface *get_free_frame(NvencContext *ctx) |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1978 | { |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 1979 | NvencSurface *tmp_surf; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1980 | |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 1981 | if (av_fifo_read(ctx->unused_surface_queue, &tmp_surf, 1) < 0) |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 1982 | // queue empty |
| 1983 | return NULL; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1984 | |
Ben Chang | 8de3458 | 2017-04-25 21:57:56 | [diff] [blame] | 1985 | return tmp_surf; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1986 | } |
| 1987 | |
Timo Rothenpieler | 96cba1c | 2016-09-07 13:49:28 | [diff] [blame] | 1988 | static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *nv_surface, |
| 1989 | NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params, const AVFrame *frame) |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1990 | { |
Timo Rothenpieler | 96cba1c | 2016-09-07 13:49:28 | [diff] [blame] | 1991 | int dst_linesize[4] = { |
| 1992 | lock_buffer_params->pitch, |
| 1993 | lock_buffer_params->pitch, |
| 1994 | lock_buffer_params->pitch, |
| 1995 | lock_buffer_params->pitch |
| 1996 | }; |
| 1997 | uint8_t *dst_data[4]; |
| 1998 | int ret; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 1999 | |
Timo Rothenpieler | 96cba1c | 2016-09-07 13:49:28 | [diff] [blame] | 2000 | if (frame->format == AV_PIX_FMT_YUV420P) |
| 2001 | dst_linesize[1] = dst_linesize[2] >>= 1; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2002 | |
Timo Rothenpieler | 96cba1c | 2016-09-07 13:49:28 | [diff] [blame] | 2003 | ret = av_image_fill_pointers(dst_data, frame->format, nv_surface->height, |
| 2004 | lock_buffer_params->bufferDataPtr, dst_linesize); |
| 2005 | if (ret < 0) |
| 2006 | return ret; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2007 | |
Timo Rothenpieler | 96cba1c | 2016-09-07 13:49:28 | [diff] [blame] | 2008 | if (frame->format == AV_PIX_FMT_YUV420P) |
| 2009 | FFSWAP(uint8_t*, dst_data[1], dst_data[2]); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2010 | |
Timo Rothenpieler | 96cba1c | 2016-09-07 13:49:28 | [diff] [blame] | 2011 | av_image_copy(dst_data, dst_linesize, |
| 2012 | (const uint8_t**)frame->data, frame->linesize, frame->format, |
Timo Rothenpieler | 8ebe1dd | 2016-09-08 17:08:31 | [diff] [blame] | 2013 | avctx->width, avctx->height); |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2014 | |
| 2015 | return 0; |
| 2016 | } |
| 2017 | |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2018 | static int nvenc_find_free_reg_resource(AVCodecContext *avctx) |
| 2019 | { |
| 2020 | NvencContext *ctx = avctx->priv_data; |
| 2021 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 2022 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
Timo Rothenpieler | 48e52e4 | 2018-01-28 11:51:20 | [diff] [blame] | 2023 | NVENCSTATUS nv_status; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2024 | |
Timo Rothenpieler | 23ed147 | 2019-04-24 17:00:25 | [diff] [blame] | 2025 | int i, first_round; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2026 | |
| 2027 | if (ctx->nb_registered_frames == FF_ARRAY_ELEMS(ctx->registered_frames)) { |
Timo Rothenpieler | 23ed147 | 2019-04-24 17:00:25 | [diff] [blame] | 2028 | for (first_round = 1; first_round >= 0; first_round--) { |
Timo Rothenpieler | 2e254bb | 2019-04-24 20:47:24 | [diff] [blame] | 2029 | for (i = 0; i < ctx->nb_registered_frames; i++) { |
| 2030 | if (!ctx->registered_frames[i].mapped) { |
| 2031 | if (ctx->registered_frames[i].regptr) { |
| 2032 | if (first_round) |
| 2033 | continue; |
| 2034 | nv_status = p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr); |
| 2035 | if (nv_status != NV_ENC_SUCCESS) |
| 2036 | return nvenc_print_error(avctx, nv_status, "Failed unregistering unused input resource"); |
| 2037 | ctx->registered_frames[i].ptr = NULL; |
| 2038 | ctx->registered_frames[i].regptr = NULL; |
| 2039 | } |
| 2040 | return i; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2041 | } |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | } else { |
| 2045 | return ctx->nb_registered_frames++; |
| 2046 | } |
| 2047 | |
| 2048 | av_log(avctx, AV_LOG_ERROR, "Too many registered CUDA frames\n"); |
| 2049 | return AVERROR(ENOMEM); |
| 2050 | } |
| 2051 | |
| 2052 | static int nvenc_register_frame(AVCodecContext *avctx, const AVFrame *frame) |
| 2053 | { |
| 2054 | NvencContext *ctx = avctx->priv_data; |
| 2055 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 2056 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 2057 | |
Timo Rothenpieler | 2e700b0 | 2018-05-07 20:39:20 | [diff] [blame] | 2058 | AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frame->hw_frames_ctx->data; |
Timo Rothenpieler | 0b13c34 | 2022-01-10 14:41:50 | [diff] [blame] | 2059 | NV_ENC_REGISTER_RESOURCE reg = { 0 }; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2060 | int i, idx, ret; |
| 2061 | |
| 2062 | for (i = 0; i < ctx->nb_registered_frames; i++) { |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 2063 | if (avctx->pix_fmt == AV_PIX_FMT_CUDA && ctx->registered_frames[i].ptr == frame->data[0]) |
| 2064 | return i; |
| 2065 | else if (avctx->pix_fmt == AV_PIX_FMT_D3D11 && ctx->registered_frames[i].ptr == frame->data[0] && ctx->registered_frames[i].ptr_index == (intptr_t)frame->data[1]) |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2066 | return i; |
| 2067 | } |
| 2068 | |
| 2069 | idx = nvenc_find_free_reg_resource(avctx); |
| 2070 | if (idx < 0) |
| 2071 | return idx; |
| 2072 | |
| 2073 | reg.version = NV_ENC_REGISTER_RESOURCE_VER; |
Timo Rothenpieler | 2e700b0 | 2018-05-07 20:39:20 | [diff] [blame] | 2074 | reg.width = frames_ctx->width; |
| 2075 | reg.height = frames_ctx->height; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2076 | reg.pitch = frame->linesize[0]; |
| 2077 | reg.resourceToRegister = frame->data[0]; |
| 2078 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 2079 | if (avctx->pix_fmt == AV_PIX_FMT_CUDA) { |
| 2080 | reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR; |
| 2081 | } |
| 2082 | else if (avctx->pix_fmt == AV_PIX_FMT_D3D11) { |
| 2083 | reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX; |
| 2084 | reg.subResourceIndex = (intptr_t)frame->data[1]; |
| 2085 | } |
| 2086 | |
Timo Rothenpieler | 2e700b0 | 2018-05-07 20:39:20 | [diff] [blame] | 2087 | reg.bufferFormat = nvenc_map_buffer_format(frames_ctx->sw_format); |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 2088 | if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) { |
| 2089 | av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format: %s\n", |
Timo Rothenpieler | 2e700b0 | 2018-05-07 20:39:20 | [diff] [blame] | 2090 | av_get_pix_fmt_name(frames_ctx->sw_format)); |
Philip Langdale | 2703869 | 2016-11-25 19:11:45 | [diff] [blame] | 2091 | return AVERROR(EINVAL); |
| 2092 | } |
| 2093 | |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2094 | ret = p_nvenc->nvEncRegisterResource(ctx->nvencoder, ®); |
| 2095 | if (ret != NV_ENC_SUCCESS) { |
| 2096 | nvenc_print_error(avctx, ret, "Error registering an input resource"); |
| 2097 | return AVERROR_UNKNOWN; |
| 2098 | } |
| 2099 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 2100 | ctx->registered_frames[idx].ptr = frame->data[0]; |
| 2101 | ctx->registered_frames[idx].ptr_index = reg.subResourceIndex; |
| 2102 | ctx->registered_frames[idx].regptr = reg.registeredResource; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2103 | return idx; |
| 2104 | } |
| 2105 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2106 | static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame, |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 2107 | NvencSurface *nvenc_frame) |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2108 | { |
| 2109 | NvencContext *ctx = avctx->priv_data; |
| 2110 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 2111 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 2112 | |
| 2113 | int res; |
| 2114 | NVENCSTATUS nv_status; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2115 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 2116 | if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) { |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2117 | int reg_idx = nvenc_register_frame(avctx, frame); |
| 2118 | if (reg_idx < 0) { |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 2119 | av_log(avctx, AV_LOG_ERROR, "Could not register an input HW frame\n"); |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2120 | return reg_idx; |
| 2121 | } |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2122 | |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2123 | res = av_frame_ref(nvenc_frame->in_ref, frame); |
| 2124 | if (res < 0) |
| 2125 | return res; |
| 2126 | |
Timo Rothenpieler | bbe1b21 | 2018-01-26 19:16:53 | [diff] [blame] | 2127 | if (!ctx->registered_frames[reg_idx].mapped) { |
| 2128 | ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER; |
| 2129 | ctx->registered_frames[reg_idx].in_map.registeredResource = ctx->registered_frames[reg_idx].regptr; |
| 2130 | nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &ctx->registered_frames[reg_idx].in_map); |
| 2131 | if (nv_status != NV_ENC_SUCCESS) { |
| 2132 | av_frame_unref(nvenc_frame->in_ref); |
| 2133 | return nvenc_print_error(avctx, nv_status, "Error mapping an input resource"); |
| 2134 | } |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2135 | } |
| 2136 | |
Timo Rothenpieler | bbe1b21 | 2018-01-26 19:16:53 | [diff] [blame] | 2137 | ctx->registered_frames[reg_idx].mapped += 1; |
| 2138 | |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2139 | nvenc_frame->reg_idx = reg_idx; |
Timo Rothenpieler | bbe1b21 | 2018-01-26 19:16:53 | [diff] [blame] | 2140 | nvenc_frame->input_surface = ctx->registered_frames[reg_idx].in_map.mappedResource; |
| 2141 | nvenc_frame->format = ctx->registered_frames[reg_idx].in_map.mappedBufferFmt; |
Timo Rothenpieler | fa3ecad | 2016-09-07 16:15:22 | [diff] [blame] | 2142 | nvenc_frame->pitch = frame->linesize[0]; |
Timo Rothenpieler | bbe1b21 | 2018-01-26 19:16:53 | [diff] [blame] | 2143 | |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2144 | return 0; |
| 2145 | } else { |
| 2146 | NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 }; |
| 2147 | |
| 2148 | lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER; |
| 2149 | lockBufferParams.inputBuffer = nvenc_frame->input_surface; |
| 2150 | |
| 2151 | nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams); |
| 2152 | if (nv_status != NV_ENC_SUCCESS) { |
| 2153 | return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer"); |
| 2154 | } |
| 2155 | |
Timo Rothenpieler | fa3ecad | 2016-09-07 16:15:22 | [diff] [blame] | 2156 | nvenc_frame->pitch = lockBufferParams.pitch; |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2157 | res = nvenc_copy_frame(avctx, nvenc_frame, &lockBufferParams, frame); |
| 2158 | |
| 2159 | nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, nvenc_frame->input_surface); |
| 2160 | if (nv_status != NV_ENC_SUCCESS) { |
| 2161 | return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!"); |
| 2162 | } |
| 2163 | |
| 2164 | return res; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2165 | } |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | static void nvenc_codec_specific_pic_params(AVCodecContext *avctx, |
Roman Arzumanyan | 5a88e8c | 2018-03-22 09:30:06 | [diff] [blame] | 2169 | NV_ENC_PIC_PARAMS *params, |
Limin Wang | 4ef766e | 2020-06-15 14:08:40 | [diff] [blame] | 2170 | NV_ENC_SEI_PAYLOAD *sei_data, |
| 2171 | int sei_count) |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2172 | { |
| 2173 | NvencContext *ctx = avctx->priv_data; |
| 2174 | |
| 2175 | switch (avctx->codec->id) { |
| 2176 | case AV_CODEC_ID_H264: |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2177 | params->codecPicParams.h264PicParams.sliceMode = |
| 2178 | ctx->encode_config.encodeCodecConfig.h264Config.sliceMode; |
| 2179 | params->codecPicParams.h264PicParams.sliceModeData = |
| 2180 | ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData; |
Limin Wang | 4ef766e | 2020-06-15 14:08:40 | [diff] [blame] | 2181 | if (sei_count > 0) { |
Roman Arzumanyan | 5a88e8c | 2018-03-22 09:30:06 | [diff] [blame] | 2182 | params->codecPicParams.h264PicParams.seiPayloadArray = sei_data; |
Limin Wang | 4ef766e | 2020-06-15 14:08:40 | [diff] [blame] | 2183 | params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count; |
Roman Arzumanyan | 5a88e8c | 2018-03-22 09:30:06 | [diff] [blame] | 2184 | } |
| 2185 | |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2186 | break; |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2187 | case AV_CODEC_ID_HEVC: |
| 2188 | params->codecPicParams.hevcPicParams.sliceMode = |
| 2189 | ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode; |
| 2190 | params->codecPicParams.hevcPicParams.sliceModeData = |
| 2191 | ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData; |
Limin Wang | 4ef766e | 2020-06-15 14:08:40 | [diff] [blame] | 2192 | if (sei_count > 0) { |
Roman Arzumanyan | 5a88e8c | 2018-03-22 09:30:06 | [diff] [blame] | 2193 | params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data; |
Limin Wang | 4ef766e | 2020-06-15 14:08:40 | [diff] [blame] | 2194 | params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count; |
Roman Arzumanyan | 5a88e8c | 2018-03-22 09:30:06 | [diff] [blame] | 2195 | } |
| 2196 | |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2197 | break; |
Timo Rothenpieler | 5c288a4 | 2022-11-05 20:17:37 | [diff] [blame] | 2198 | #if CONFIG_AV1_NVENC_ENCODER |
| 2199 | case AV_CODEC_ID_AV1: |
| 2200 | params->codecPicParams.av1PicParams.numTileColumns = |
| 2201 | ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns; |
| 2202 | params->codecPicParams.av1PicParams.numTileRows = |
| 2203 | ctx->encode_config.encodeCodecConfig.av1Config.numTileRows; |
| 2204 | if (sei_count > 0) { |
| 2205 | params->codecPicParams.av1PicParams.obuPayloadArray = sei_data; |
| 2206 | params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count; |
| 2207 | } |
| 2208 | |
| 2209 | break; |
| 2210 | #endif |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2211 | } |
| 2212 | } |
| 2213 | |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2214 | static inline void timestamp_queue_enqueue(AVFifo *queue, int64_t timestamp) |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2215 | { |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2216 | av_fifo_write(queue, ×tamp, 1); |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2217 | } |
| 2218 | |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2219 | static inline int64_t timestamp_queue_dequeue(AVFifo *queue) |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2220 | { |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2221 | int64_t timestamp = AV_NOPTS_VALUE; |
Anton Khirnov | 3553809 | 2022-07-12 09:25:09 | [diff] [blame] | 2222 | // The following call might fail if the queue is empty. |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2223 | av_fifo_read(queue, ×tamp, 1); |
Anton Khirnov | 3553809 | 2022-07-12 09:25:09 | [diff] [blame] | 2224 | |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2225 | return timestamp; |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2226 | } |
| 2227 | |
Andrey Turkin | d346391 | 2016-05-29 12:34:38 | [diff] [blame] | 2228 | static int nvenc_set_timestamp(AVCodecContext *avctx, |
| 2229 | NV_ENC_LOCK_BITSTREAM *params, |
| 2230 | AVPacket *pkt) |
| 2231 | { |
| 2232 | NvencContext *ctx = avctx->priv_data; |
| 2233 | |
| 2234 | pkt->pts = params->outputTimeStamp; |
Timo Rothenpieler | e5babcc | 2016-05-31 16:59:35 | [diff] [blame] | 2235 | |
Timo Rothenpieler | aa3d982 | 2022-11-05 20:20:06 | [diff] [blame] | 2236 | if (avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) { |
Anton Khirnov | 7d1d61c | 2023-05-04 09:50:48 | [diff] [blame] | 2237 | FF_DISABLE_DEPRECATION_WARNINGS |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2238 | pkt->dts = timestamp_queue_dequeue(ctx->timestamp_list) - |
Anton Khirnov | 7d1d61c | 2023-05-04 09:50:48 | [diff] [blame] | 2239 | #if FF_API_TICKS_PER_FRAME |
Timo Rothenpieler | ac7c265 | 2023-06-14 22:45:44 | [diff] [blame] | 2240 | FFMAX(avctx->ticks_per_frame, 1) * |
Anton Khirnov | 7d1d61c | 2023-05-04 09:50:48 | [diff] [blame] | 2241 | #endif |
Timo Rothenpieler | ac7c265 | 2023-06-14 22:45:44 | [diff] [blame] | 2242 | FFMAX(ctx->encode_config.frameIntervalP - 1, 0); |
Anton Khirnov | 7d1d61c | 2023-05-04 09:50:48 | [diff] [blame] | 2243 | FF_ENABLE_DEPRECATION_WARNINGS |
Timo Rothenpieler | aa3d982 | 2022-11-05 20:20:06 | [diff] [blame] | 2244 | } else { |
| 2245 | pkt->dts = pkt->pts; |
| 2246 | } |
Andrey Turkin | d346391 | 2016-05-29 12:34:38 | [diff] [blame] | 2247 | |
Andrey Turkin | d346391 | 2016-05-29 12:34:38 | [diff] [blame] | 2248 | return 0; |
| 2249 | } |
| 2250 | |
Timo Rothenpieler | 16fdb48 | 2023-06-16 19:35:45 | [diff] [blame] | 2251 | static int nvenc_store_frame_data(AVCodecContext *avctx, NV_ENC_PIC_PARAMS *pic_params, const AVFrame *frame) |
| 2252 | { |
| 2253 | NvencContext *ctx = avctx->priv_data; |
| 2254 | int res = 0; |
| 2255 | |
| 2256 | int idx = ctx->frame_data_array_pos; |
| 2257 | NvencFrameData *frame_data = &ctx->frame_data_array[idx]; |
| 2258 | |
| 2259 | // in case the encoder got reconfigured, there might be leftovers |
| 2260 | av_buffer_unref(&frame_data->frame_opaque_ref); |
| 2261 | |
Andreas Rheinhardt | 2d7c147 | 2023-07-31 09:50:21 | [diff] [blame] | 2262 | if (frame->opaque_ref && avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) { |
Timo Rothenpieler | 16fdb48 | 2023-06-16 19:35:45 | [diff] [blame] | 2263 | frame_data->frame_opaque_ref = av_buffer_ref(frame->opaque_ref); |
| 2264 | if (!frame_data->frame_opaque_ref) |
| 2265 | return AVERROR(ENOMEM); |
| 2266 | } |
| 2267 | |
| 2268 | frame_data->duration = frame->duration; |
| 2269 | frame_data->frame_opaque = frame->opaque; |
| 2270 | |
| 2271 | #if FF_API_REORDERED_OPAQUE |
| 2272 | FF_DISABLE_DEPRECATION_WARNINGS |
| 2273 | frame_data->reordered_opaque = frame->reordered_opaque; |
| 2274 | FF_ENABLE_DEPRECATION_WARNINGS |
| 2275 | #endif |
| 2276 | |
| 2277 | ctx->frame_data_array_pos = (ctx->frame_data_array_pos + 1) % ctx->frame_data_array_nb; |
| 2278 | pic_params->inputDuration = idx; |
| 2279 | |
| 2280 | return res; |
| 2281 | } |
| 2282 | |
| 2283 | static int nvenc_retrieve_frame_data(AVCodecContext *avctx, NV_ENC_LOCK_BITSTREAM *lock_params, AVPacket *pkt) |
| 2284 | { |
| 2285 | NvencContext *ctx = avctx->priv_data; |
| 2286 | int res = 0; |
| 2287 | |
| 2288 | int idx = lock_params->outputDuration; |
| 2289 | NvencFrameData *frame_data = &ctx->frame_data_array[idx]; |
| 2290 | |
| 2291 | pkt->duration = frame_data->duration; |
| 2292 | |
| 2293 | #if FF_API_REORDERED_OPAQUE |
| 2294 | FF_DISABLE_DEPRECATION_WARNINGS |
| 2295 | avctx->reordered_opaque = frame_data->reordered_opaque; |
| 2296 | FF_ENABLE_DEPRECATION_WARNINGS |
| 2297 | #endif |
| 2298 | |
| 2299 | if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) { |
| 2300 | pkt->opaque = frame_data->frame_opaque; |
| 2301 | pkt->opaque_ref = frame_data->frame_opaque_ref; |
| 2302 | frame_data->frame_opaque_ref = NULL; |
| 2303 | } |
| 2304 | |
| 2305 | av_buffer_unref(&frame_data->frame_opaque_ref); |
| 2306 | |
| 2307 | return res; |
| 2308 | } |
| 2309 | |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 2310 | static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2311 | { |
| 2312 | NvencContext *ctx = avctx->priv_data; |
| 2313 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 2314 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 2315 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2316 | NV_ENC_LOCK_BITSTREAM lock_params = { 0 }; |
| 2317 | NVENCSTATUS nv_status; |
| 2318 | int res = 0; |
| 2319 | |
Lucas Cooper | fd55470 | 2016-03-07 23:47:56 | [diff] [blame] | 2320 | enum AVPictureType pict_type; |
| 2321 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2322 | lock_params.version = NV_ENC_LOCK_BITSTREAM_VER; |
| 2323 | |
| 2324 | lock_params.doNotWait = 0; |
| 2325 | lock_params.outputBitstream = tmpoutsurf->output_surface; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2326 | |
| 2327 | nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params); |
| 2328 | if (nv_status != NV_ENC_SUCCESS) { |
Andrey Turkin | e1691c4 | 2016-05-20 15:37:00 | [diff] [blame] | 2329 | res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer"); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2330 | goto error; |
| 2331 | } |
| 2332 | |
James Almer | 238e08b | 2021-03-12 23:50:01 | [diff] [blame] | 2333 | res = ff_get_encode_buffer(avctx, pkt, lock_params.bitstreamSizeInBytes, 0); |
Timo Rothenpieler | 4fb6ce2 | 2019-11-17 00:27:44 | [diff] [blame] | 2334 | |
| 2335 | if (res < 0) { |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2336 | p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface); |
| 2337 | goto error; |
| 2338 | } |
| 2339 | |
| 2340 | memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes); |
| 2341 | |
| 2342 | nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface); |
Timo Rothenpieler | 48e52e4 | 2018-01-28 11:51:20 | [diff] [blame] | 2343 | if (nv_status != NV_ENC_SUCCESS) { |
| 2344 | res = nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open"); |
| 2345 | goto error; |
| 2346 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2347 | |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2348 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 2349 | if (avctx->pix_fmt == AV_PIX_FMT_CUDA || avctx->pix_fmt == AV_PIX_FMT_D3D11) { |
Timo Rothenpieler | bbe1b21 | 2018-01-26 19:16:53 | [diff] [blame] | 2350 | ctx->registered_frames[tmpoutsurf->reg_idx].mapped -= 1; |
| 2351 | if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped == 0) { |
Timo Rothenpieler | 48e52e4 | 2018-01-28 11:51:20 | [diff] [blame] | 2352 | nv_status = p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->registered_frames[tmpoutsurf->reg_idx].in_map.mappedResource); |
| 2353 | if (nv_status != NV_ENC_SUCCESS) { |
| 2354 | res = nvenc_print_error(avctx, nv_status, "Failed unmapping input resource"); |
| 2355 | goto error; |
| 2356 | } |
Timo Rothenpieler | bbe1b21 | 2018-01-26 19:16:53 | [diff] [blame] | 2357 | } else if (ctx->registered_frames[tmpoutsurf->reg_idx].mapped < 0) { |
| 2358 | res = AVERROR_BUG; |
| 2359 | goto error; |
| 2360 | } |
| 2361 | |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2362 | av_frame_unref(tmpoutsurf->in_ref); |
Andrey Turkin | a8cf25d | 2016-05-20 22:08:06 | [diff] [blame] | 2363 | |
| 2364 | tmpoutsurf->input_surface = NULL; |
| 2365 | } |
| 2366 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2367 | switch (lock_params.pictureType) { |
| 2368 | case NV_ENC_PIC_TYPE_IDR: |
| 2369 | pkt->flags |= AV_PKT_FLAG_KEY; |
| 2370 | case NV_ENC_PIC_TYPE_I: |
Lucas Cooper | fd55470 | 2016-03-07 23:47:56 | [diff] [blame] | 2371 | pict_type = AV_PICTURE_TYPE_I; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2372 | break; |
| 2373 | case NV_ENC_PIC_TYPE_P: |
Lucas Cooper | fd55470 | 2016-03-07 23:47:56 | [diff] [blame] | 2374 | pict_type = AV_PICTURE_TYPE_P; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2375 | break; |
| 2376 | case NV_ENC_PIC_TYPE_B: |
Lucas Cooper | fd55470 | 2016-03-07 23:47:56 | [diff] [blame] | 2377 | pict_type = AV_PICTURE_TYPE_B; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2378 | break; |
| 2379 | case NV_ENC_PIC_TYPE_BI: |
Lucas Cooper | fd55470 | 2016-03-07 23:47:56 | [diff] [blame] | 2380 | pict_type = AV_PICTURE_TYPE_BI; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2381 | break; |
| 2382 | default: |
| 2383 | av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n"); |
| 2384 | av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n"); |
| 2385 | res = AVERROR_EXTERNAL; |
| 2386 | goto error; |
Lucas Cooper | fd55470 | 2016-03-07 23:47:56 | [diff] [blame] | 2387 | } |
| 2388 | |
Lucas Cooper | fd55470 | 2016-03-07 23:47:56 | [diff] [blame] | 2389 | ff_side_data_set_encoder_stats(pkt, |
| 2390 | (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, pict_type); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2391 | |
Andrey Turkin | d346391 | 2016-05-29 12:34:38 | [diff] [blame] | 2392 | res = nvenc_set_timestamp(avctx, &lock_params, pkt); |
| 2393 | if (res < 0) |
| 2394 | goto error2; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2395 | |
Timo Rothenpieler | 16fdb48 | 2023-06-16 19:35:45 | [diff] [blame] | 2396 | res = nvenc_retrieve_frame_data(avctx, &lock_params, pkt); |
| 2397 | if (res < 0) |
| 2398 | goto error2; |
| 2399 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2400 | return 0; |
| 2401 | |
| 2402 | error: |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2403 | timestamp_queue_dequeue(ctx->timestamp_list); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2404 | |
Andrey Turkin | d346391 | 2016-05-29 12:34:38 | [diff] [blame] | 2405 | error2: |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2406 | return res; |
| 2407 | } |
| 2408 | |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2409 | static int output_ready(AVCodecContext *avctx, int flush) |
Andrey Turkin | cfb49fc | 2016-05-20 16:13:20 | [diff] [blame] | 2410 | { |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2411 | NvencContext *ctx = avctx->priv_data; |
Andrey Turkin | cfb49fc | 2016-05-20 16:13:20 | [diff] [blame] | 2412 | int nb_ready, nb_pending; |
| 2413 | |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 2414 | nb_ready = av_fifo_can_read(ctx->output_surface_ready_queue); |
| 2415 | nb_pending = av_fifo_can_read(ctx->output_surface_queue); |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2416 | if (flush) |
| 2417 | return nb_ready > 0; |
| 2418 | return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth); |
Andrey Turkin | cfb49fc | 2016-05-20 16:13:20 | [diff] [blame] | 2419 | } |
| 2420 | |
Timo Rothenpieler | 57de806 | 2021-06-04 16:40:17 | [diff] [blame] | 2421 | static int prepare_sei_data_array(AVCodecContext *avctx, const AVFrame *frame) |
| 2422 | { |
| 2423 | NvencContext *ctx = avctx->priv_data; |
| 2424 | int sei_count = 0; |
| 2425 | int i, res; |
| 2426 | |
| 2427 | if (ctx->a53_cc && av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC)) { |
| 2428 | void *a53_data = NULL; |
| 2429 | size_t a53_size = 0; |
| 2430 | |
| 2431 | if (ff_alloc_a53_sei(frame, 0, &a53_data, &a53_size) < 0) { |
| 2432 | av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n"); |
| 2433 | } |
| 2434 | |
| 2435 | if (a53_data) { |
| 2436 | void *tmp = av_fast_realloc(ctx->sei_data, |
| 2437 | &ctx->sei_data_size, |
| 2438 | (sei_count + 1) * sizeof(*ctx->sei_data)); |
| 2439 | if (!tmp) { |
| 2440 | av_free(a53_data); |
| 2441 | res = AVERROR(ENOMEM); |
| 2442 | goto error; |
| 2443 | } else { |
| 2444 | ctx->sei_data = tmp; |
| 2445 | ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size; |
Timo Rothenpieler | 57de806 | 2021-06-04 16:40:17 | [diff] [blame] | 2446 | ctx->sei_data[sei_count].payload = (uint8_t*)a53_data; |
Timo Rothenpieler | 6362b8c | 2022-11-05 20:18:36 | [diff] [blame] | 2447 | |
| 2448 | #if CONFIG_AV1_NVENC_ENCODER |
| 2449 | if (avctx->codec->id == AV_CODEC_ID_AV1) |
| 2450 | ctx->sei_data[sei_count].payloadType = AV1_METADATA_TYPE_ITUT_T35; |
| 2451 | else |
| 2452 | #endif |
| 2453 | ctx->sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35; |
| 2454 | |
Timo Rothenpieler | 57de806 | 2021-06-04 16:40:17 | [diff] [blame] | 2455 | sei_count++; |
| 2456 | } |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | if (ctx->s12m_tc && av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE)) { |
| 2461 | void *tc_data = NULL; |
| 2462 | size_t tc_size = 0; |
| 2463 | |
| 2464 | if (ff_alloc_timecode_sei(frame, avctx->framerate, 0, &tc_data, &tc_size) < 0) { |
| 2465 | av_log(ctx, AV_LOG_ERROR, "Not enough memory for timecode sei, skipping\n"); |
| 2466 | } |
| 2467 | |
| 2468 | if (tc_data) { |
| 2469 | void *tmp = av_fast_realloc(ctx->sei_data, |
| 2470 | &ctx->sei_data_size, |
| 2471 | (sei_count + 1) * sizeof(*ctx->sei_data)); |
| 2472 | if (!tmp) { |
| 2473 | av_free(tc_data); |
| 2474 | res = AVERROR(ENOMEM); |
| 2475 | goto error; |
| 2476 | } else { |
| 2477 | ctx->sei_data = tmp; |
| 2478 | ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size; |
Timo Rothenpieler | 57de806 | 2021-06-04 16:40:17 | [diff] [blame] | 2479 | ctx->sei_data[sei_count].payload = (uint8_t*)tc_data; |
Timo Rothenpieler | 6362b8c | 2022-11-05 20:18:36 | [diff] [blame] | 2480 | |
| 2481 | #if CONFIG_AV1_NVENC_ENCODER |
| 2482 | if (avctx->codec->id == AV_CODEC_ID_AV1) |
| 2483 | ctx->sei_data[sei_count].payloadType = AV1_METADATA_TYPE_TIMECODE; |
| 2484 | else |
| 2485 | #endif |
| 2486 | ctx->sei_data[sei_count].payloadType = SEI_TYPE_TIME_CODE; |
| 2487 | |
Timo Rothenpieler | 57de806 | 2021-06-04 16:40:17 | [diff] [blame] | 2488 | sei_count++; |
| 2489 | } |
| 2490 | } |
| 2491 | } |
| 2492 | |
Limin Wang | 0c8741f | 2021-12-24 04:32:47 | [diff] [blame] | 2493 | if (!ctx->udu_sei) |
| 2494 | return sei_count; |
| 2495 | |
Timo Rothenpieler | 57de806 | 2021-06-04 16:40:17 | [diff] [blame] | 2496 | for (i = 0; i < frame->nb_side_data; i++) { |
| 2497 | AVFrameSideData *side_data = frame->side_data[i]; |
| 2498 | void *tmp; |
| 2499 | |
| 2500 | if (side_data->type != AV_FRAME_DATA_SEI_UNREGISTERED) |
| 2501 | continue; |
| 2502 | |
| 2503 | tmp = av_fast_realloc(ctx->sei_data, |
| 2504 | &ctx->sei_data_size, |
| 2505 | (sei_count + 1) * sizeof(*ctx->sei_data)); |
| 2506 | if (!tmp) { |
| 2507 | res = AVERROR(ENOMEM); |
| 2508 | goto error; |
| 2509 | } else { |
| 2510 | ctx->sei_data = tmp; |
| 2511 | ctx->sei_data[sei_count].payloadSize = side_data->size; |
| 2512 | ctx->sei_data[sei_count].payloadType = SEI_TYPE_USER_DATA_UNREGISTERED; |
| 2513 | ctx->sei_data[sei_count].payload = av_memdup(side_data->data, side_data->size); |
| 2514 | |
| 2515 | if (!ctx->sei_data[sei_count].payload) { |
| 2516 | res = AVERROR(ENOMEM); |
| 2517 | goto error; |
| 2518 | } |
| 2519 | |
| 2520 | sei_count++; |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | return sei_count; |
| 2525 | |
| 2526 | error: |
| 2527 | for (i = 0; i < sei_count; i++) |
| 2528 | av_freep(&(ctx->sei_data[i].payload)); |
| 2529 | |
| 2530 | return res; |
| 2531 | } |
| 2532 | |
Timo Rothenpieler | fdbb4b9 | 2018-05-03 16:57:40 | [diff] [blame] | 2533 | static void reconfig_encoder(AVCodecContext *avctx, const AVFrame *frame) |
Miroslav Slugeň | 952421c | 2017-02-12 18:53:58 | [diff] [blame] | 2534 | { |
| 2535 | NvencContext *ctx = avctx->priv_data; |
| 2536 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs; |
| 2537 | NVENCSTATUS ret; |
| 2538 | |
| 2539 | NV_ENC_RECONFIGURE_PARAMS params = { 0 }; |
| 2540 | int needs_reconfig = 0; |
| 2541 | int needs_encode_config = 0; |
pkviet | 1553751 | 2018-05-03 00:15:52 | [diff] [blame] | 2542 | int reconfig_bitrate = 0, reconfig_dar = 0; |
Miroslav Slugeň | 952421c | 2017-02-12 18:53:58 | [diff] [blame] | 2543 | int dw, dh; |
| 2544 | |
| 2545 | params.version = NV_ENC_RECONFIGURE_PARAMS_VER; |
| 2546 | params.reInitEncodeParams = ctx->init_encode_params; |
| 2547 | |
| 2548 | compute_dar(avctx, &dw, &dh); |
| 2549 | if (dw != ctx->init_encode_params.darWidth || dh != ctx->init_encode_params.darHeight) { |
| 2550 | av_log(avctx, AV_LOG_VERBOSE, |
| 2551 | "aspect ratio change (DAR): %d:%d -> %d:%d\n", |
| 2552 | ctx->init_encode_params.darWidth, |
| 2553 | ctx->init_encode_params.darHeight, dw, dh); |
| 2554 | |
| 2555 | params.reInitEncodeParams.darHeight = dh; |
| 2556 | params.reInitEncodeParams.darWidth = dw; |
| 2557 | |
| 2558 | needs_reconfig = 1; |
pkviet | 1553751 | 2018-05-03 00:15:52 | [diff] [blame] | 2559 | reconfig_dar = 1; |
| 2560 | } |
| 2561 | |
| 2562 | if (ctx->rc != NV_ENC_PARAMS_RC_CONSTQP && ctx->support_dyn_bitrate) { |
| 2563 | if (avctx->bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->bit_rate) { |
| 2564 | av_log(avctx, AV_LOG_VERBOSE, |
| 2565 | "avg bitrate change: %d -> %d\n", |
| 2566 | params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate, |
| 2567 | (uint32_t)avctx->bit_rate); |
| 2568 | |
| 2569 | params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->bit_rate; |
| 2570 | reconfig_bitrate = 1; |
| 2571 | } |
| 2572 | |
| 2573 | if (avctx->rc_max_rate > 0 && ctx->encode_config.rcParams.maxBitRate != avctx->rc_max_rate) { |
| 2574 | av_log(avctx, AV_LOG_VERBOSE, |
| 2575 | "max bitrate change: %d -> %d\n", |
| 2576 | params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate, |
| 2577 | (uint32_t)avctx->rc_max_rate); |
| 2578 | |
| 2579 | params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->rc_max_rate; |
| 2580 | reconfig_bitrate = 1; |
| 2581 | } |
| 2582 | |
| 2583 | if (avctx->rc_buffer_size > 0 && ctx->encode_config.rcParams.vbvBufferSize != avctx->rc_buffer_size) { |
| 2584 | av_log(avctx, AV_LOG_VERBOSE, |
| 2585 | "vbv buffer size change: %d -> %d\n", |
| 2586 | params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize, |
| 2587 | avctx->rc_buffer_size); |
| 2588 | |
| 2589 | params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->rc_buffer_size; |
| 2590 | reconfig_bitrate = 1; |
| 2591 | } |
| 2592 | |
| 2593 | if (reconfig_bitrate) { |
| 2594 | params.resetEncoder = 1; |
| 2595 | params.forceIDR = 1; |
| 2596 | |
| 2597 | needs_encode_config = 1; |
| 2598 | needs_reconfig = 1; |
| 2599 | } |
Miroslav Slugeň | 952421c | 2017-02-12 18:53:58 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | if (!needs_encode_config) |
| 2603 | params.reInitEncodeParams.encodeConfig = NULL; |
| 2604 | |
| 2605 | if (needs_reconfig) { |
| 2606 | ret = p_nvenc->nvEncReconfigureEncoder(ctx->nvencoder, ¶ms); |
| 2607 | if (ret != NV_ENC_SUCCESS) { |
| 2608 | nvenc_print_error(avctx, ret, "failed to reconfigure nvenc"); |
| 2609 | } else { |
pkviet | 1553751 | 2018-05-03 00:15:52 | [diff] [blame] | 2610 | if (reconfig_dar) { |
| 2611 | ctx->init_encode_params.darHeight = dh; |
| 2612 | ctx->init_encode_params.darWidth = dw; |
| 2613 | } |
| 2614 | |
| 2615 | if (reconfig_bitrate) { |
| 2616 | ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate; |
| 2617 | ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate; |
| 2618 | ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize; |
| 2619 | } |
| 2620 | |
Miroslav Slugeň | 952421c | 2017-02-12 18:53:58 | [diff] [blame] | 2621 | } |
| 2622 | } |
Miroslav Slugeň | 952421c | 2017-02-12 18:53:58 | [diff] [blame] | 2623 | } |
| 2624 | |
James Almer | 827d6fe | 2020-06-09 21:31:32 | [diff] [blame] | 2625 | static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2626 | { |
| 2627 | NVENCSTATUS nv_status; |
Timo Rothenpieler | d0961d3 | 2017-09-02 17:42:13 | [diff] [blame] | 2628 | NvencSurface *tmp_out_surf, *in_surf; |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 2629 | int res, res2; |
Limin Wang | 4ef766e | 2020-06-15 14:08:40 | [diff] [blame] | 2630 | int sei_count = 0; |
| 2631 | int i; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2632 | |
| 2633 | NvencContext *ctx = avctx->priv_data; |
| 2634 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 2635 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 2636 | |
| 2637 | NV_ENC_PIC_PARAMS pic_params = { 0 }; |
| 2638 | pic_params.version = NV_ENC_PIC_PARAMS_VER; |
| 2639 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 2640 | if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder) |
Timo Rothenpieler | a56d049 | 2017-09-02 17:30:21 | [diff] [blame] | 2641 | return AVERROR(EINVAL); |
| 2642 | |
James Almer | 827d6fe | 2020-06-09 21:31:32 | [diff] [blame] | 2643 | if (frame && frame->buf[0]) { |
Timo Rothenpieler | d0961d3 | 2017-09-02 17:42:13 | [diff] [blame] | 2644 | in_surf = get_free_frame(ctx); |
| 2645 | if (!in_surf) |
Timo Rothenpieler | a56d049 | 2017-09-02 17:30:21 | [diff] [blame] | 2646 | return AVERROR(EAGAIN); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2647 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 2648 | res = nvenc_push_context(avctx); |
| 2649 | if (res < 0) |
| 2650 | return res; |
Timo Rothenpieler | be74ba6 | 2017-02-13 21:59:46 | [diff] [blame] | 2651 | |
Timo Rothenpieler | fdbb4b9 | 2018-05-03 16:57:40 | [diff] [blame] | 2652 | reconfig_encoder(avctx, frame); |
| 2653 | |
Timo Rothenpieler | d0961d3 | 2017-09-02 17:42:13 | [diff] [blame] | 2654 | res = nvenc_upload_frame(avctx, frame, in_surf); |
Timo Rothenpieler | be74ba6 | 2017-02-13 21:59:46 | [diff] [blame] | 2655 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 2656 | res2 = nvenc_pop_context(avctx); |
| 2657 | if (res2 < 0) |
| 2658 | return res2; |
Timo Rothenpieler | be74ba6 | 2017-02-13 21:59:46 | [diff] [blame] | 2659 | |
Timo Rothenpieler | a56d049 | 2017-09-02 17:30:21 | [diff] [blame] | 2660 | if (res) |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2661 | return res; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2662 | |
Timo Rothenpieler | d0961d3 | 2017-09-02 17:42:13 | [diff] [blame] | 2663 | pic_params.inputBuffer = in_surf->input_surface; |
| 2664 | pic_params.bufferFmt = in_surf->format; |
| 2665 | pic_params.inputWidth = in_surf->width; |
| 2666 | pic_params.inputHeight = in_surf->height; |
| 2667 | pic_params.inputPitch = in_surf->pitch; |
| 2668 | pic_params.outputBitstream = in_surf->output_surface; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2669 | |
Michael Niedermayer | 94d68a4 | 2015-07-27 19:14:31 | [diff] [blame] | 2670 | if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { |
James Almer | 2f561ba | 2023-04-11 18:02:14 | [diff] [blame] | 2671 | if (frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2672 | pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM; |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2673 | else |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2674 | pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2675 | } else { |
| 2676 | pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME; |
| 2677 | } |
| 2678 | |
Timo Rothenpieler | 30c5587 | 2016-10-12 18:51:43 | [diff] [blame] | 2679 | if (ctx->forced_idr >= 0 && frame->pict_type == AV_PICTURE_TYPE_I) { |
| 2680 | pic_params.encodePicFlags = |
| 2681 | ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA; |
| 2682 | } else { |
| 2683 | pic_params.encodePicFlags = 0; |
| 2684 | } |
| 2685 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2686 | pic_params.inputTimeStamp = frame->pts; |
Andrey Turkin | 82d705e | 2016-05-20 14:49:24 | [diff] [blame] | 2687 | |
Timo Rothenpieler | 8bcce56 | 2021-06-06 19:53:53 | [diff] [blame] | 2688 | if (ctx->extra_sei) { |
| 2689 | res = prepare_sei_data_array(avctx, frame); |
| 2690 | if (res < 0) |
| 2691 | return res; |
| 2692 | sei_count = res; |
| 2693 | } |
Brad Hards | cee9f96 | 2021-06-04 14:06:38 | [diff] [blame] | 2694 | |
Timo Rothenpieler | 16fdb48 | 2023-06-16 19:35:45 | [diff] [blame] | 2695 | res = nvenc_store_frame_data(avctx, &pic_params, frame); |
| 2696 | if (res < 0) |
| 2697 | return res; |
| 2698 | |
Brad Hards | 63948a6 | 2021-05-25 10:11:57 | [diff] [blame] | 2699 | nvenc_codec_specific_pic_params(avctx, &pic_params, ctx->sei_data, sei_count); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2700 | } else { |
| 2701 | pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2702 | } |
| 2703 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 2704 | res = nvenc_push_context(avctx); |
| 2705 | if (res < 0) |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2706 | return res; |
Timo Rothenpieler | be74ba6 | 2017-02-13 21:59:46 | [diff] [blame] | 2707 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2708 | nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params); |
Limin Wang | 4ef766e | 2020-06-15 14:08:40 | [diff] [blame] | 2709 | |
Brad Hards | 63948a6 | 2021-05-25 10:11:57 | [diff] [blame] | 2710 | for (i = 0; i < sei_count; i++) |
| 2711 | av_freep(&(ctx->sei_data[i].payload)); |
Timo Rothenpieler | be74ba6 | 2017-02-13 21:59:46 | [diff] [blame] | 2712 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 2713 | res = nvenc_pop_context(avctx); |
| 2714 | if (res < 0) |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2715 | return res; |
Timo Rothenpieler | be74ba6 | 2017-02-13 21:59:46 | [diff] [blame] | 2716 | |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2717 | if (nv_status != NV_ENC_SUCCESS && |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2718 | nv_status != NV_ENC_ERR_NEED_MORE_INPUT) |
| 2719 | return nvenc_print_error(avctx, nv_status, "EncodePicture failed!"); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2720 | |
James Almer | 827d6fe | 2020-06-09 21:31:32 | [diff] [blame] | 2721 | if (frame && frame->buf[0]) { |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 2722 | av_fifo_write(ctx->output_surface_queue, &in_surf, 1); |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2723 | |
| 2724 | if (avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) |
| 2725 | timestamp_queue_enqueue(ctx->timestamp_list, frame->pts); |
Andrey Turkin | 58c6dcb | 2016-05-29 12:51:36 | [diff] [blame] | 2726 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2727 | |
Andrey Turkin | 2f53b5b | 2016-05-29 12:50:06 | [diff] [blame] | 2728 | /* all the pending buffers are now ready for output */ |
| 2729 | if (nv_status == NV_ENC_SUCCESS) { |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 2730 | while (av_fifo_read(ctx->output_surface_queue, &tmp_out_surf, 1) >= 0) |
| 2731 | av_fifo_write(ctx->output_surface_ready_queue, &tmp_out_surf, 1); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2732 | } |
| 2733 | |
Timo Rothenpieler | a56d049 | 2017-09-02 17:30:21 | [diff] [blame] | 2734 | return 0; |
| 2735 | } |
| 2736 | |
| 2737 | int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) |
| 2738 | { |
Timo Rothenpieler | d0961d3 | 2017-09-02 17:42:13 | [diff] [blame] | 2739 | NvencSurface *tmp_out_surf; |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 2740 | int res, res2; |
Timo Rothenpieler | a56d049 | 2017-09-02 17:30:21 | [diff] [blame] | 2741 | |
| 2742 | NvencContext *ctx = avctx->priv_data; |
Timo Rothenpieler | a56d049 | 2017-09-02 17:30:21 | [diff] [blame] | 2743 | |
James Almer | 827d6fe | 2020-06-09 21:31:32 | [diff] [blame] | 2744 | AVFrame *frame = ctx->frame; |
| 2745 | |
Hendrik Leppkes | bff6d98 | 2017-11-11 15:51:58 | [diff] [blame] | 2746 | if ((!ctx->cu_context && !ctx->d3d11_device) || !ctx->nvencoder) |
Timo Rothenpieler | a56d049 | 2017-09-02 17:30:21 | [diff] [blame] | 2747 | return AVERROR(EINVAL); |
| 2748 | |
James Almer | 827d6fe | 2020-06-09 21:31:32 | [diff] [blame] | 2749 | if (!frame->buf[0]) { |
| 2750 | res = ff_encode_get_frame(avctx, frame); |
| 2751 | if (res < 0 && res != AVERROR_EOF) |
| 2752 | return res; |
| 2753 | } |
| 2754 | |
| 2755 | res = nvenc_send_frame(avctx, frame); |
| 2756 | if (res < 0) { |
| 2757 | if (res != AVERROR(EAGAIN)) |
| 2758 | return res; |
| 2759 | } else |
| 2760 | av_frame_unref(frame); |
| 2761 | |
| 2762 | if (output_ready(avctx, avctx->internal->draining)) { |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 2763 | av_fifo_read(ctx->output_surface_ready_queue, &tmp_out_surf, 1); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2764 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 2765 | res = nvenc_push_context(avctx); |
| 2766 | if (res < 0) |
| 2767 | return res; |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 2768 | |
Timo Rothenpieler | d0961d3 | 2017-09-02 17:42:13 | [diff] [blame] | 2769 | res = process_output_surface(avctx, pkt, tmp_out_surf); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2770 | |
Hendrik Leppkes | 6fcbf39 | 2017-11-11 15:13:24 | [diff] [blame] | 2771 | res2 = nvenc_pop_context(avctx); |
| 2772 | if (res2 < 0) |
| 2773 | return res2; |
Ganapathy Kasi | 43c417a | 2017-05-31 02:03:14 | [diff] [blame] | 2774 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2775 | if (res) |
| 2776 | return res; |
| 2777 | |
Anton Khirnov | 587545c | 2022-01-06 15:59:49 | [diff] [blame] | 2778 | av_fifo_write(ctx->unused_surface_queue, &tmp_out_surf, 1); |
James Almer | 827d6fe | 2020-06-09 21:31:32 | [diff] [blame] | 2779 | } else if (avctx->internal->draining) { |
Timo Rothenpieler | a56d049 | 2017-09-02 17:30:21 | [diff] [blame] | 2780 | return AVERROR_EOF; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 2781 | } else { |
Timo Rothenpieler | a56d049 | 2017-09-02 17:30:21 | [diff] [blame] | 2782 | return AVERROR(EAGAIN); |
| 2783 | } |
| 2784 | |
| 2785 | return 0; |
| 2786 | } |
| 2787 | |
Philip Langdale | 3ea7057 | 2019-12-20 23:34:33 | [diff] [blame] | 2788 | av_cold void ff_nvenc_encode_flush(AVCodecContext *avctx) |
| 2789 | { |
James Almer | 827d6fe | 2020-06-09 21:31:32 | [diff] [blame] | 2790 | NvencContext *ctx = avctx->priv_data; |
| 2791 | |
| 2792 | nvenc_send_frame(avctx, NULL); |
Timo Rothenpieler | 6c418ae | 2023-06-16 18:23:26 | [diff] [blame] | 2793 | av_fifo_reset2(ctx->timestamp_list); |
Philip Langdale | 3ea7057 | 2019-12-20 23:34:33 | [diff] [blame] | 2794 | } |