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