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