Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1 | /* |
| 2 | * H.264 hardware encoding using nvidia nvenc |
| 3 | * Copyright (c) 2014 Timo Rothenpieler <[email protected]> |
| 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 | |
Timo Rothenpieler | 1efdb0a | 2014-12-25 13:55:31 | [diff] [blame] | 22 | #if defined(_WIN32) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 23 | #include <windows.h> |
| 24 | #else |
| 25 | #include <dlfcn.h> |
| 26 | #endif |
| 27 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 28 | #include <nvEncodeAPI.h> |
| 29 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 30 | #include "libavutil/internal.h" |
| 31 | #include "libavutil/imgutils.h" |
| 32 | #include "libavutil/avassert.h" |
| 33 | #include "libavutil/opt.h" |
| 34 | #include "libavutil/mem.h" |
| 35 | #include "avcodec.h" |
| 36 | #include "internal.h" |
| 37 | #include "thread.h" |
| 38 | |
Timo Rothenpieler | 1efdb0a | 2014-12-25 13:55:31 | [diff] [blame] | 39 | #if defined(_WIN32) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 40 | #define CUDAAPI __stdcall |
| 41 | #else |
| 42 | #define CUDAAPI |
| 43 | #endif |
| 44 | |
Timo Rothenpieler | 1efdb0a | 2014-12-25 13:55:31 | [diff] [blame] | 45 | #if defined(_WIN32) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 46 | #define LOAD_FUNC(l, s) GetProcAddress(l, s) |
| 47 | #define DL_CLOSE_FUNC(l) FreeLibrary(l) |
| 48 | #else |
| 49 | #define LOAD_FUNC(l, s) dlsym(l, s) |
| 50 | #define DL_CLOSE_FUNC(l) dlclose(l) |
| 51 | #endif |
| 52 | |
| 53 | typedef enum cudaError_enum { |
| 54 | CUDA_SUCCESS = 0 |
| 55 | } CUresult; |
| 56 | typedef int CUdevice; |
| 57 | typedef void* CUcontext; |
| 58 | |
| 59 | typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags); |
| 60 | typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count); |
| 61 | typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal); |
| 62 | typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev); |
| 63 | typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev); |
| 64 | typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev); |
| 65 | typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx); |
| 66 | typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx); |
| 67 | |
| 68 | typedef NVENCSTATUS (NVENCAPI* PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList); |
| 69 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 70 | typedef struct NvencInputSurface |
| 71 | { |
| 72 | NV_ENC_INPUT_PTR input_surface; |
| 73 | int width; |
| 74 | int height; |
| 75 | |
| 76 | int lockCount; |
| 77 | |
| 78 | NV_ENC_BUFFER_FORMAT format; |
| 79 | } NvencInputSurface; |
| 80 | |
| 81 | typedef struct NvencOutputSurface |
| 82 | { |
| 83 | NV_ENC_OUTPUT_PTR output_surface; |
| 84 | int size; |
| 85 | |
| 86 | NvencInputSurface* input_surface; |
| 87 | |
| 88 | int busy; |
| 89 | } NvencOutputSurface; |
| 90 | |
| 91 | typedef struct NvencData |
| 92 | { |
| 93 | union { |
| 94 | int64_t timestamp; |
| 95 | NvencOutputSurface *surface; |
| 96 | }; |
| 97 | } NvencData; |
| 98 | |
| 99 | typedef struct NvencDataList |
| 100 | { |
| 101 | NvencData* data; |
| 102 | |
| 103 | uint32_t pos; |
| 104 | uint32_t count; |
| 105 | uint32_t size; |
| 106 | } NvencDataList; |
| 107 | |
| 108 | typedef struct NvencDynLoadFunctions |
| 109 | { |
| 110 | PCUINIT cu_init; |
| 111 | PCUDEVICEGETCOUNT cu_device_get_count; |
| 112 | PCUDEVICEGET cu_device_get; |
| 113 | PCUDEVICEGETNAME cu_device_get_name; |
| 114 | PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability; |
| 115 | PCUCTXCREATE cu_ctx_create; |
| 116 | PCUCTXPOPCURRENT cu_ctx_pop_current; |
| 117 | PCUCTXDESTROY cu_ctx_destroy; |
| 118 | |
| 119 | NV_ENCODE_API_FUNCTION_LIST nvenc_funcs; |
| 120 | int nvenc_device_count; |
| 121 | CUdevice nvenc_devices[16]; |
| 122 | |
Timo Rothenpieler | 1efdb0a | 2014-12-25 13:55:31 | [diff] [blame] | 123 | #if defined(_WIN32) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 124 | HMODULE cuda_lib; |
| 125 | HMODULE nvenc_lib; |
| 126 | #else |
| 127 | void* cuda_lib; |
| 128 | void* nvenc_lib; |
| 129 | #endif |
| 130 | } NvencDynLoadFunctions; |
| 131 | |
| 132 | typedef struct NvencContext |
| 133 | { |
| 134 | AVClass *avclass; |
| 135 | |
| 136 | NvencDynLoadFunctions nvenc_dload_funcs; |
| 137 | |
| 138 | NV_ENC_INITIALIZE_PARAMS init_encode_params; |
| 139 | NV_ENC_CONFIG encode_config; |
| 140 | CUcontext cu_context; |
| 141 | |
| 142 | int max_surface_count; |
| 143 | NvencInputSurface *input_surfaces; |
| 144 | NvencOutputSurface *output_surfaces; |
| 145 | |
| 146 | NvencDataList output_surface_queue; |
| 147 | NvencDataList output_surface_ready_queue; |
| 148 | NvencDataList timestamp_list; |
| 149 | int64_t last_dts; |
| 150 | |
| 151 | void *nvencoder; |
| 152 | |
| 153 | char *preset; |
| 154 | int cbr; |
| 155 | int twopass; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 156 | int gpu; |
| 157 | } NvencContext; |
| 158 | |
| 159 | static NvencData* data_queue_dequeue(NvencDataList* queue) |
| 160 | { |
| 161 | uint32_t mask; |
| 162 | uint32_t read_pos; |
| 163 | |
| 164 | av_assert0(queue); |
| 165 | av_assert0(queue->size); |
| 166 | av_assert0(queue->data); |
| 167 | |
| 168 | if (!queue->count) |
| 169 | return NULL; |
| 170 | |
| 171 | /* Size always is a multiple of two */ |
| 172 | mask = queue->size - 1; |
| 173 | read_pos = (queue->pos - queue->count) & mask; |
| 174 | queue->count--; |
| 175 | |
| 176 | return &queue->data[read_pos]; |
| 177 | } |
| 178 | |
| 179 | static int data_queue_enqueue(NvencDataList* queue, NvencData *data) |
| 180 | { |
| 181 | NvencDataList new_queue; |
| 182 | NvencData* tmp_data; |
| 183 | uint32_t mask; |
| 184 | |
| 185 | if (!queue->size) { |
| 186 | /* size always has to be a multiple of two */ |
| 187 | queue->size = 4; |
| 188 | queue->pos = 0; |
| 189 | queue->count = 0; |
| 190 | |
| 191 | queue->data = av_malloc(queue->size * sizeof(*(queue->data))); |
| 192 | |
| 193 | if (!queue->data) { |
| 194 | queue->size = 0; |
| 195 | return AVERROR(ENOMEM); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (queue->count == queue->size) { |
| 200 | new_queue.size = queue->size << 1; |
| 201 | new_queue.pos = 0; |
| 202 | new_queue.count = 0; |
| 203 | new_queue.data = av_malloc(new_queue.size * sizeof(*(queue->data))); |
| 204 | |
| 205 | if (!new_queue.data) |
| 206 | return AVERROR(ENOMEM); |
| 207 | |
| 208 | while (tmp_data = data_queue_dequeue(queue)) |
| 209 | data_queue_enqueue(&new_queue, tmp_data); |
| 210 | |
| 211 | av_free(queue->data); |
| 212 | *queue = new_queue; |
| 213 | } |
| 214 | |
| 215 | mask = queue->size - 1; |
| 216 | |
| 217 | queue->data[queue->pos] = *data; |
| 218 | queue->pos = (queue->pos + 1) & mask; |
| 219 | queue->count++; |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int out_surf_queue_enqueue(NvencDataList* queue, NvencOutputSurface* surface) |
| 225 | { |
| 226 | NvencData data; |
| 227 | data.surface = surface; |
| 228 | |
| 229 | return data_queue_enqueue(queue, &data); |
| 230 | } |
| 231 | |
| 232 | static NvencOutputSurface* out_surf_queue_dequeue(NvencDataList* queue) |
| 233 | { |
| 234 | NvencData* res = data_queue_dequeue(queue); |
| 235 | |
| 236 | if (!res) |
| 237 | return NULL; |
| 238 | |
| 239 | return res->surface; |
| 240 | } |
| 241 | |
| 242 | static int timestamp_queue_enqueue(NvencDataList* queue, int64_t timestamp) |
| 243 | { |
| 244 | NvencData data; |
| 245 | data.timestamp = timestamp; |
| 246 | |
| 247 | return data_queue_enqueue(queue, &data); |
| 248 | } |
| 249 | |
| 250 | static int64_t timestamp_queue_dequeue(NvencDataList* queue) |
| 251 | { |
| 252 | NvencData* res = data_queue_dequeue(queue); |
| 253 | |
| 254 | if (!res) |
| 255 | return AV_NOPTS_VALUE; |
| 256 | |
| 257 | return res->timestamp; |
| 258 | } |
| 259 | |
| 260 | #define CHECK_LOAD_FUNC(t, f, s) \ |
| 261 | do { \ |
| 262 | (f) = (t)LOAD_FUNC(dl_fn->cuda_lib, s); \ |
| 263 | if (!(f)) { \ |
| 264 | av_log(avctx, AV_LOG_FATAL, "Failed loading %s from CUDA library\n", s); \ |
| 265 | goto error; \ |
| 266 | } \ |
| 267 | } while (0) |
| 268 | |
| 269 | static av_cold int nvenc_dyload_cuda(AVCodecContext *avctx) |
| 270 | { |
| 271 | NvencContext *ctx = avctx->priv_data; |
| 272 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 273 | |
| 274 | if (dl_fn->cuda_lib) |
| 275 | return 1; |
| 276 | |
| 277 | #if defined(_WIN32) |
| 278 | dl_fn->cuda_lib = LoadLibrary(TEXT("nvcuda.dll")); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 279 | #else |
| 280 | dl_fn->cuda_lib = dlopen("libcuda.so", RTLD_LAZY); |
| 281 | #endif |
| 282 | |
| 283 | if (!dl_fn->cuda_lib) { |
| 284 | av_log(avctx, AV_LOG_FATAL, "Failed loading CUDA library\n"); |
| 285 | goto error; |
| 286 | } |
| 287 | |
| 288 | CHECK_LOAD_FUNC(PCUINIT, dl_fn->cu_init, "cuInit"); |
| 289 | CHECK_LOAD_FUNC(PCUDEVICEGETCOUNT, dl_fn->cu_device_get_count, "cuDeviceGetCount"); |
| 290 | CHECK_LOAD_FUNC(PCUDEVICEGET, dl_fn->cu_device_get, "cuDeviceGet"); |
| 291 | CHECK_LOAD_FUNC(PCUDEVICEGETNAME, dl_fn->cu_device_get_name, "cuDeviceGetName"); |
| 292 | CHECK_LOAD_FUNC(PCUDEVICECOMPUTECAPABILITY, dl_fn->cu_device_compute_capability, "cuDeviceComputeCapability"); |
| 293 | CHECK_LOAD_FUNC(PCUCTXCREATE, dl_fn->cu_ctx_create, "cuCtxCreate_v2"); |
| 294 | CHECK_LOAD_FUNC(PCUCTXPOPCURRENT, dl_fn->cu_ctx_pop_current, "cuCtxPopCurrent_v2"); |
| 295 | CHECK_LOAD_FUNC(PCUCTXDESTROY, dl_fn->cu_ctx_destroy, "cuCtxDestroy_v2"); |
| 296 | |
| 297 | return 1; |
| 298 | |
| 299 | error: |
| 300 | |
| 301 | if (dl_fn->cuda_lib) |
| 302 | DL_CLOSE_FUNC(dl_fn->cuda_lib); |
| 303 | |
| 304 | dl_fn->cuda_lib = NULL; |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static av_cold int check_cuda_errors(AVCodecContext *avctx, CUresult err, const char *func) |
| 310 | { |
| 311 | if (err != CUDA_SUCCESS) { |
| 312 | av_log(avctx, AV_LOG_FATAL, ">> %s - failed with error code 0x%x\n", func, err); |
| 313 | return 0; |
| 314 | } |
| 315 | return 1; |
| 316 | } |
| 317 | #define check_cuda_errors(f) if (!check_cuda_errors(avctx, f, #f)) goto error |
| 318 | |
| 319 | static av_cold int nvenc_check_cuda(AVCodecContext *avctx) |
| 320 | { |
| 321 | int device_count = 0; |
| 322 | CUdevice cu_device = 0; |
| 323 | char gpu_name[128]; |
| 324 | int smminor = 0, smmajor = 0; |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 325 | int i, smver, target_smver; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 326 | |
| 327 | NvencContext *ctx = avctx->priv_data; |
| 328 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 329 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 330 | switch (avctx->codec->id) { |
| 331 | case AV_CODEC_ID_H264: |
| 332 | target_smver = 0x30; |
| 333 | break; |
| 334 | case AV_CODEC_ID_H265: |
| 335 | target_smver = 0x52; |
| 336 | break; |
| 337 | default: |
| 338 | av_log(avctx, AV_LOG_FATAL, "nvenc: Unknown codec name\n"); |
| 339 | goto error; |
| 340 | } |
| 341 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 342 | if (!nvenc_dyload_cuda(avctx)) |
| 343 | return 0; |
| 344 | |
| 345 | if (dl_fn->nvenc_device_count > 0) |
| 346 | return 1; |
| 347 | |
| 348 | check_cuda_errors(dl_fn->cu_init(0)); |
| 349 | |
| 350 | check_cuda_errors(dl_fn->cu_device_get_count(&device_count)); |
| 351 | |
| 352 | if (!device_count) { |
| 353 | av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n"); |
| 354 | goto error; |
| 355 | } |
| 356 | |
| 357 | av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", device_count); |
| 358 | |
| 359 | dl_fn->nvenc_device_count = 0; |
| 360 | |
| 361 | for (i = 0; i < device_count; ++i) { |
| 362 | check_cuda_errors(dl_fn->cu_device_get(&cu_device, i)); |
| 363 | check_cuda_errors(dl_fn->cu_device_get_name(gpu_name, sizeof(gpu_name), cu_device)); |
| 364 | check_cuda_errors(dl_fn->cu_device_compute_capability(&smmajor, &smminor, cu_device)); |
| 365 | |
| 366 | smver = (smmajor << 4) | smminor; |
| 367 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 368 | av_log(avctx, AV_LOG_VERBOSE, "[ GPU #%d - < %s > has Compute SM %d.%d, NVENC %s ]\n", i, gpu_name, smmajor, smminor, (smver >= target_smver) ? "Available" : "Not Available"); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 369 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 370 | if (smver >= target_smver) |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 371 | dl_fn->nvenc_devices[dl_fn->nvenc_device_count++] = cu_device; |
| 372 | } |
| 373 | |
| 374 | if (!dl_fn->nvenc_device_count) { |
| 375 | av_log(avctx, AV_LOG_FATAL, "No NVENC capable devices found\n"); |
| 376 | goto error; |
| 377 | } |
| 378 | |
| 379 | return 1; |
| 380 | |
| 381 | error: |
| 382 | |
| 383 | dl_fn->nvenc_device_count = 0; |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static av_cold int nvenc_dyload_nvenc(AVCodecContext *avctx) |
| 389 | { |
| 390 | PNVENCODEAPICREATEINSTANCE nvEncodeAPICreateInstance = 0; |
| 391 | NVENCSTATUS nvstatus; |
| 392 | |
| 393 | NvencContext *ctx = avctx->priv_data; |
| 394 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 395 | |
| 396 | if (!nvenc_check_cuda(avctx)) |
| 397 | return 0; |
| 398 | |
| 399 | if (dl_fn->nvenc_lib) |
| 400 | return 1; |
| 401 | |
| 402 | #if defined(_WIN32) |
| 403 | if (sizeof(void*) == 8) { |
| 404 | dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI64.dll")); |
| 405 | } else { |
| 406 | dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI.dll")); |
| 407 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 408 | #else |
| 409 | dl_fn->nvenc_lib = dlopen("libnvidia-encode.so.1", RTLD_LAZY); |
| 410 | #endif |
| 411 | |
| 412 | if (!dl_fn->nvenc_lib) { |
| 413 | av_log(avctx, AV_LOG_FATAL, "Failed loading the nvenc library\n"); |
| 414 | goto error; |
| 415 | } |
| 416 | |
| 417 | nvEncodeAPICreateInstance = (PNVENCODEAPICREATEINSTANCE)LOAD_FUNC(dl_fn->nvenc_lib, "NvEncodeAPICreateInstance"); |
| 418 | |
| 419 | if (!nvEncodeAPICreateInstance) { |
| 420 | av_log(avctx, AV_LOG_FATAL, "Failed to load nvenc entrypoint\n"); |
| 421 | goto error; |
| 422 | } |
| 423 | |
| 424 | dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER; |
| 425 | |
| 426 | nvstatus = nvEncodeAPICreateInstance(&dl_fn->nvenc_funcs); |
| 427 | |
| 428 | if (nvstatus != NV_ENC_SUCCESS) { |
| 429 | av_log(avctx, AV_LOG_FATAL, "Failed to create nvenc instance\n"); |
| 430 | goto error; |
| 431 | } |
| 432 | |
| 433 | av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n"); |
| 434 | |
| 435 | return 1; |
| 436 | |
| 437 | error: |
| 438 | if (dl_fn->nvenc_lib) |
| 439 | DL_CLOSE_FUNC(dl_fn->nvenc_lib); |
| 440 | |
| 441 | dl_fn->nvenc_lib = NULL; |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static av_cold void nvenc_unload_nvenc(AVCodecContext *avctx) |
| 447 | { |
| 448 | NvencContext *ctx = avctx->priv_data; |
| 449 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 450 | |
| 451 | DL_CLOSE_FUNC(dl_fn->nvenc_lib); |
| 452 | dl_fn->nvenc_lib = NULL; |
| 453 | |
| 454 | dl_fn->nvenc_device_count = 0; |
| 455 | |
| 456 | DL_CLOSE_FUNC(dl_fn->cuda_lib); |
| 457 | dl_fn->cuda_lib = NULL; |
| 458 | |
| 459 | dl_fn->cu_init = NULL; |
| 460 | dl_fn->cu_device_get_count = NULL; |
| 461 | dl_fn->cu_device_get = NULL; |
| 462 | dl_fn->cu_device_get_name = NULL; |
| 463 | dl_fn->cu_device_compute_capability = NULL; |
| 464 | dl_fn->cu_ctx_create = NULL; |
| 465 | dl_fn->cu_ctx_pop_current = NULL; |
| 466 | dl_fn->cu_ctx_destroy = NULL; |
| 467 | |
| 468 | av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n"); |
| 469 | } |
| 470 | |
| 471 | static av_cold int nvenc_encode_init(AVCodecContext *avctx) |
| 472 | { |
| 473 | NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encode_session_params = { 0 }; |
| 474 | NV_ENC_PRESET_CONFIG preset_config = { 0 }; |
| 475 | CUcontext cu_context_curr; |
| 476 | CUresult cu_res; |
| 477 | GUID encoder_preset = NV_ENC_PRESET_HQ_GUID; |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 478 | GUID codec; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 479 | NVENCSTATUS nv_status = NV_ENC_SUCCESS; |
| 480 | int surfaceCount = 0; |
| 481 | int i, num_mbs; |
| 482 | int isLL = 0; |
| 483 | int res = 0; |
Timo Rothenpieler | fb34c58 | 2015-01-26 12:28:22 | [diff] [blame] | 484 | int dw, dh; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 485 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 486 | NvencContext *ctx = avctx->priv_data; |
| 487 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 488 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 489 | |
| 490 | if (!nvenc_dyload_nvenc(avctx)) |
| 491 | return AVERROR_EXTERNAL; |
| 492 | |
| 493 | avctx->coded_frame = av_frame_alloc(); |
| 494 | if (!avctx->coded_frame) { |
| 495 | res = AVERROR(ENOMEM); |
| 496 | goto error; |
| 497 | } |
| 498 | |
| 499 | ctx->last_dts = AV_NOPTS_VALUE; |
| 500 | |
| 501 | ctx->encode_config.version = NV_ENC_CONFIG_VER; |
| 502 | ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER; |
| 503 | preset_config.version = NV_ENC_PRESET_CONFIG_VER; |
| 504 | preset_config.presetCfg.version = NV_ENC_CONFIG_VER; |
| 505 | encode_session_params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER; |
| 506 | encode_session_params.apiVersion = NVENCAPI_VERSION; |
Timo Rothenpieler | bc3f767 | 2015-01-16 00:02:40 | [diff] [blame] | 507 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 508 | if (ctx->gpu >= dl_fn->nvenc_device_count) { |
| 509 | av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->gpu, dl_fn->nvenc_device_count); |
| 510 | res = AVERROR(EINVAL); |
| 511 | goto error; |
| 512 | } |
| 513 | |
| 514 | ctx->cu_context = NULL; |
| 515 | cu_res = dl_fn->cu_ctx_create(&ctx->cu_context, 0, dl_fn->nvenc_devices[ctx->gpu]); |
| 516 | |
| 517 | if (cu_res != CUDA_SUCCESS) { |
| 518 | av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res); |
| 519 | res = AVERROR_EXTERNAL; |
| 520 | goto error; |
| 521 | } |
| 522 | |
| 523 | cu_res = dl_fn->cu_ctx_pop_current(&cu_context_curr); |
| 524 | |
| 525 | if (cu_res != CUDA_SUCCESS) { |
| 526 | av_log(avctx, AV_LOG_FATAL, "Failed popping CUDA context: 0x%x\n", (int)cu_res); |
| 527 | res = AVERROR_EXTERNAL; |
| 528 | goto error; |
| 529 | } |
| 530 | |
| 531 | encode_session_params.device = ctx->cu_context; |
| 532 | encode_session_params.deviceType = NV_ENC_DEVICE_TYPE_CUDA; |
| 533 | |
| 534 | nv_status = p_nvenc->nvEncOpenEncodeSessionEx(&encode_session_params, &ctx->nvencoder); |
| 535 | if (nv_status != NV_ENC_SUCCESS) { |
| 536 | ctx->nvencoder = NULL; |
| 537 | av_log(avctx, AV_LOG_FATAL, "OpenEncodeSessionEx failed: 0x%x - invalid license key?\n", (int)nv_status); |
| 538 | res = AVERROR_EXTERNAL; |
| 539 | goto error; |
| 540 | } |
| 541 | |
| 542 | if (ctx->preset) { |
| 543 | if (!strcmp(ctx->preset, "hp")) { |
| 544 | encoder_preset = NV_ENC_PRESET_HP_GUID; |
| 545 | } else if (!strcmp(ctx->preset, "hq")) { |
| 546 | encoder_preset = NV_ENC_PRESET_HQ_GUID; |
| 547 | } else if (!strcmp(ctx->preset, "bd")) { |
| 548 | encoder_preset = NV_ENC_PRESET_BD_GUID; |
| 549 | } else if (!strcmp(ctx->preset, "ll")) { |
| 550 | encoder_preset = NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID; |
| 551 | isLL = 1; |
| 552 | } else if (!strcmp(ctx->preset, "llhp")) { |
| 553 | encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HP_GUID; |
| 554 | isLL = 1; |
| 555 | } else if (!strcmp(ctx->preset, "llhq")) { |
| 556 | encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID; |
| 557 | isLL = 1; |
| 558 | } else if (!strcmp(ctx->preset, "default")) { |
| 559 | encoder_preset = NV_ENC_PRESET_DEFAULT_GUID; |
| 560 | } else { |
| 561 | av_log(avctx, AV_LOG_FATAL, "Preset \"%s\" is unknown! Supported presets: hp, hq, bd, ll, llhp, llhq, default\n", ctx->preset); |
| 562 | res = AVERROR(EINVAL); |
| 563 | goto error; |
| 564 | } |
| 565 | } |
| 566 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 567 | switch (avctx->codec->id) { |
| 568 | case AV_CODEC_ID_H264: |
| 569 | codec = NV_ENC_CODEC_H264_GUID; |
| 570 | break; |
| 571 | case AV_CODEC_ID_H265: |
| 572 | codec = NV_ENC_CODEC_HEVC_GUID; |
| 573 | break; |
| 574 | default: |
| 575 | av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n"); |
| 576 | res = AVERROR(EINVAL); |
| 577 | goto error; |
| 578 | } |
| 579 | |
| 580 | nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, codec, encoder_preset, &preset_config); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 581 | if (nv_status != NV_ENC_SUCCESS) { |
| 582 | av_log(avctx, AV_LOG_FATAL, "GetEncodePresetConfig failed: 0x%x\n", (int)nv_status); |
| 583 | res = AVERROR_EXTERNAL; |
| 584 | goto error; |
| 585 | } |
| 586 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 587 | ctx->init_encode_params.encodeGUID = codec; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 588 | ctx->init_encode_params.encodeHeight = avctx->height; |
| 589 | ctx->init_encode_params.encodeWidth = avctx->width; |
Timo Rothenpieler | fb34c58 | 2015-01-26 12:28:22 | [diff] [blame] | 590 | |
| 591 | if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den && |
| 592 | (avctx->sample_aspect_ratio.num != 1 || avctx->sample_aspect_ratio.num != 1)) { |
| 593 | av_reduce(&dw, &dh, |
| 594 | avctx->width * avctx->sample_aspect_ratio.num, |
| 595 | avctx->height * avctx->sample_aspect_ratio.den, |
| 596 | 1024 * 1024); |
| 597 | ctx->init_encode_params.darHeight = dh; |
| 598 | ctx->init_encode_params.darWidth = dw; |
| 599 | } else { |
| 600 | ctx->init_encode_params.darHeight = avctx->height; |
| 601 | ctx->init_encode_params.darWidth = avctx->width; |
| 602 | } |
| 603 | |
Philip Langdale | d20df26 | 2015-01-28 17:05:53 | [diff] [blame] | 604 | // De-compensate for hardware, dubiously, trying to compensate for |
| 605 | // playback at 704 pixel width. |
| 606 | if (avctx->width == 720 && |
| 607 | (avctx->height == 480 || avctx->height == 576)) { |
| 608 | av_reduce(&dw, &dh, |
| 609 | ctx->init_encode_params.darWidth * 44, |
| 610 | ctx->init_encode_params.darHeight * 45, |
| 611 | 1024 * 1204); |
| 612 | ctx->init_encode_params.darHeight = dh; |
| 613 | ctx->init_encode_params.darWidth = dw; |
| 614 | } |
| 615 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 616 | ctx->init_encode_params.frameRateNum = avctx->time_base.den; |
| 617 | ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame; |
| 618 | |
| 619 | num_mbs = ((avctx->width + 15) >> 4) * ((avctx->height + 15) >> 4); |
| 620 | ctx->max_surface_count = (num_mbs >= 8160) ? 32 : 48; |
| 621 | |
| 622 | ctx->init_encode_params.enableEncodeAsync = 0; |
| 623 | ctx->init_encode_params.enablePTD = 1; |
| 624 | |
| 625 | ctx->init_encode_params.presetGUID = encoder_preset; |
| 626 | |
| 627 | ctx->init_encode_params.encodeConfig = &ctx->encode_config; |
| 628 | memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config)); |
| 629 | ctx->encode_config.version = NV_ENC_CONFIG_VER; |
| 630 | |
Philip Langdale | ff0c559 | 2015-01-24 20:52:58 | [diff] [blame] | 631 | if (avctx->refs >= 0) { |
| 632 | /* 0 means "let the hardware decide" */ |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 633 | switch (avctx->codec->id) { |
| 634 | case AV_CODEC_ID_H264: |
| 635 | ctx->encode_config.encodeCodecConfig.h264Config.maxNumRefFrames = avctx->refs; |
| 636 | break; |
| 637 | case AV_CODEC_ID_H265: |
| 638 | ctx->encode_config.encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB = avctx->refs; |
| 639 | break; |
| 640 | /* Earlier switch/case will return if unknown codec is passed. */ |
| 641 | } |
Philip Langdale | ff0c559 | 2015-01-24 20:52:58 | [diff] [blame] | 642 | } |
| 643 | |
Timo Rothenpieler | 914fd42 | 2015-01-26 12:28:21 | [diff] [blame] | 644 | if (avctx->gop_size > 0) { |
| 645 | if (avctx->max_b_frames >= 0) { |
| 646 | /* 0 is intra-only, 1 is I/P only, 2 is one B Frame, 3 two B frames, and so on. */ |
| 647 | ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1; |
| 648 | } |
| 649 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 650 | ctx->encode_config.gopLength = avctx->gop_size; |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 651 | switch (avctx->codec->id) { |
| 652 | case AV_CODEC_ID_H264: |
| 653 | ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = avctx->gop_size; |
| 654 | break; |
| 655 | case AV_CODEC_ID_H265: |
| 656 | ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = avctx->gop_size; |
| 657 | break; |
| 658 | /* Earlier switch/case will return if unknown codec is passed. */ |
| 659 | } |
Timo Rothenpieler | 914fd42 | 2015-01-26 12:28:21 | [diff] [blame] | 660 | } else if (avctx->gop_size == 0) { |
| 661 | ctx->encode_config.frameIntervalP = 0; |
| 662 | ctx->encode_config.gopLength = 1; |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 663 | switch (avctx->codec->id) { |
| 664 | case AV_CODEC_ID_H264: |
| 665 | ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = 1; |
| 666 | break; |
| 667 | case AV_CODEC_ID_H265: |
| 668 | ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = 1; |
| 669 | break; |
| 670 | /* Earlier switch/case will return if unknown codec is passed. */ |
| 671 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 672 | } |
| 673 | |
Timo Rothenpieler | 914fd42 | 2015-01-26 12:28:21 | [diff] [blame] | 674 | /* when there're b frames, set dts offset */ |
| 675 | if (ctx->encode_config.frameIntervalP >= 2) |
| 676 | ctx->last_dts = -2; |
| 677 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 678 | if (avctx->bit_rate > 0) |
| 679 | ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate; |
| 680 | |
| 681 | if (avctx->rc_max_rate > 0) |
| 682 | ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate; |
| 683 | |
| 684 | if (ctx->cbr) { |
| 685 | if (!ctx->twopass) { |
| 686 | ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR; |
| 687 | } else if (ctx->twopass == 1 || isLL) { |
| 688 | ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_QUALITY; |
| 689 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 690 | if (avctx->codec->id == AV_CODEC_ID_H264) { |
| 691 | ctx->encode_config.encodeCodecConfig.h264Config.adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE; |
| 692 | ctx->encode_config.encodeCodecConfig.h264Config.fmoMode = NV_ENC_H264_FMO_DISABLE; |
| 693 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 694 | |
| 695 | if (!isLL) |
| 696 | av_log(avctx, AV_LOG_WARNING, "Twopass mode is only known to work with low latency (ll, llhq, llhp) presets.\n"); |
| 697 | } else { |
| 698 | ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR; |
| 699 | } |
| 700 | } else if (avctx->global_quality > 0) { |
| 701 | ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; |
| 702 | ctx->encode_config.rcParams.constQP.qpInterB = avctx->global_quality; |
| 703 | ctx->encode_config.rcParams.constQP.qpInterP = avctx->global_quality; |
| 704 | ctx->encode_config.rcParams.constQP.qpIntra = avctx->global_quality; |
| 705 | |
| 706 | avctx->qmin = -1; |
| 707 | avctx->qmax = -1; |
| 708 | } else if (avctx->qmin >= 0 && avctx->qmax >= 0) { |
| 709 | ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR; |
| 710 | |
| 711 | ctx->encode_config.rcParams.enableMinQP = 1; |
| 712 | ctx->encode_config.rcParams.enableMaxQP = 1; |
| 713 | |
| 714 | ctx->encode_config.rcParams.minQP.qpInterB = avctx->qmin; |
| 715 | ctx->encode_config.rcParams.minQP.qpInterP = avctx->qmin; |
| 716 | ctx->encode_config.rcParams.minQP.qpIntra = avctx->qmin; |
| 717 | |
| 718 | ctx->encode_config.rcParams.maxQP.qpInterB = avctx->qmax; |
| 719 | ctx->encode_config.rcParams.maxQP.qpInterP = avctx->qmax; |
| 720 | ctx->encode_config.rcParams.maxQP.qpIntra = avctx->qmax; |
| 721 | } |
| 722 | |
| 723 | if (avctx->rc_buffer_size > 0) |
| 724 | ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size; |
| 725 | |
| 726 | if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) { |
| 727 | ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD; |
| 728 | } else { |
| 729 | ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME; |
| 730 | } |
| 731 | |
| 732 | switch (avctx->profile) { |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 733 | case FF_PROFILE_HEVC_MAIN: |
| 734 | ctx->encode_config.profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID; |
| 735 | break; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 736 | case FF_PROFILE_H264_BASELINE: |
| 737 | ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID; |
| 738 | break; |
| 739 | case FF_PROFILE_H264_MAIN: |
| 740 | ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID; |
| 741 | break; |
| 742 | case FF_PROFILE_H264_HIGH: |
| 743 | case FF_PROFILE_UNKNOWN: |
| 744 | ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID; |
| 745 | break; |
| 746 | default: |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 747 | av_log(avctx, AV_LOG_WARNING, "Unsupported profile requested, falling back to high\n"); |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 748 | ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID; |
| 749 | break; |
| 750 | } |
| 751 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 752 | switch (avctx->codec->id) { |
| 753 | case AV_CODEC_ID_H264: |
| 754 | ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourDescriptionPresentFlag = 1; |
| 755 | ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoSignalTypePresentFlag = 1; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 756 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 757 | ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourMatrix = avctx->colorspace; |
| 758 | ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourPrimaries = avctx->color_primaries; |
| 759 | ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.transferCharacteristics = avctx->color_trc; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 760 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 761 | ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoFullRangeFlag = avctx->color_range == AVCOL_RANGE_JPEG; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 762 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 763 | ctx->encode_config.encodeCodecConfig.h264Config.disableSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0; |
| 764 | ctx->encode_config.encodeCodecConfig.h264Config.repeatSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1; |
| 765 | break; |
| 766 | case AV_CODEC_ID_H265: |
| 767 | ctx->encode_config.encodeCodecConfig.hevcConfig.disableSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0; |
| 768 | ctx->encode_config.encodeCodecConfig.hevcConfig.repeatSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1; |
| 769 | break; |
| 770 | /* Earlier switch/case will return if unknown codec is passed. */ |
| 771 | } |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 772 | |
| 773 | nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params); |
| 774 | if (nv_status != NV_ENC_SUCCESS) { |
| 775 | av_log(avctx, AV_LOG_FATAL, "InitializeEncoder failed: 0x%x\n", (int)nv_status); |
| 776 | res = AVERROR_EXTERNAL; |
| 777 | goto error; |
| 778 | } |
| 779 | |
| 780 | ctx->input_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->input_surfaces)); |
| 781 | |
| 782 | if (!ctx->input_surfaces) { |
| 783 | res = AVERROR(ENOMEM); |
| 784 | goto error; |
| 785 | } |
| 786 | |
| 787 | ctx->output_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->output_surfaces)); |
| 788 | |
| 789 | if (!ctx->output_surfaces) { |
| 790 | res = AVERROR(ENOMEM); |
| 791 | goto error; |
| 792 | } |
| 793 | |
| 794 | for (surfaceCount = 0; surfaceCount < ctx->max_surface_count; ++surfaceCount) { |
| 795 | NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 }; |
| 796 | NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 }; |
| 797 | allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER; |
| 798 | allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER; |
| 799 | |
| 800 | allocSurf.width = (avctx->width + 31) & ~31; |
| 801 | allocSurf.height = (avctx->height + 31) & ~31; |
| 802 | |
| 803 | allocSurf.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED; |
| 804 | |
| 805 | switch (avctx->pix_fmt) { |
| 806 | case AV_PIX_FMT_YUV420P: |
| 807 | allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YV12_PL; |
| 808 | break; |
| 809 | |
| 810 | case AV_PIX_FMT_NV12: |
| 811 | allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL; |
| 812 | break; |
| 813 | |
| 814 | case AV_PIX_FMT_YUV444P: |
| 815 | allocSurf.bufferFmt = NV_ENC_BUFFER_FORMAT_YUV444_PL; |
| 816 | break; |
| 817 | |
| 818 | default: |
| 819 | av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format\n"); |
| 820 | res = AVERROR(EINVAL); |
| 821 | goto error; |
| 822 | } |
| 823 | |
| 824 | nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf); |
| 825 | if (nv_status = NV_ENC_SUCCESS){ |
| 826 | av_log(avctx, AV_LOG_FATAL, "CreateInputBuffer failed\n"); |
| 827 | res = AVERROR_EXTERNAL; |
| 828 | goto error; |
| 829 | } |
| 830 | |
| 831 | ctx->input_surfaces[surfaceCount].lockCount = 0; |
| 832 | ctx->input_surfaces[surfaceCount].input_surface = allocSurf.inputBuffer; |
| 833 | ctx->input_surfaces[surfaceCount].format = allocSurf.bufferFmt; |
| 834 | ctx->input_surfaces[surfaceCount].width = allocSurf.width; |
| 835 | ctx->input_surfaces[surfaceCount].height = allocSurf.height; |
| 836 | |
| 837 | /* 1MB is large enough to hold most output frames. NVENC increases this automaticaly if it's not enough. */ |
| 838 | allocOut.size = 1024 * 1024; |
| 839 | |
| 840 | allocOut.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED; |
| 841 | |
| 842 | nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut); |
| 843 | if (nv_status = NV_ENC_SUCCESS) { |
| 844 | av_log(avctx, AV_LOG_FATAL, "CreateBitstreamBuffer failed\n"); |
| 845 | ctx->output_surfaces[surfaceCount++].output_surface = NULL; |
| 846 | res = AVERROR_EXTERNAL; |
| 847 | goto error; |
| 848 | } |
| 849 | |
| 850 | ctx->output_surfaces[surfaceCount].output_surface = allocOut.bitstreamBuffer; |
| 851 | ctx->output_surfaces[surfaceCount].size = allocOut.size; |
| 852 | ctx->output_surfaces[surfaceCount].busy = 0; |
| 853 | } |
| 854 | |
| 855 | if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { |
| 856 | uint32_t outSize = 0; |
| 857 | char tmpHeader[256]; |
| 858 | NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 }; |
| 859 | payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER; |
| 860 | |
| 861 | payload.spsppsBuffer = tmpHeader; |
| 862 | payload.inBufferSize = sizeof(tmpHeader); |
| 863 | payload.outSPSPPSPayloadSize = &outSize; |
| 864 | |
| 865 | nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload); |
| 866 | if (nv_status != NV_ENC_SUCCESS) { |
| 867 | av_log(avctx, AV_LOG_FATAL, "GetSequenceParams failed\n"); |
| 868 | goto error; |
| 869 | } |
| 870 | |
| 871 | avctx->extradata_size = outSize; |
| 872 | avctx->extradata = av_mallocz(outSize + FF_INPUT_BUFFER_PADDING_SIZE); |
| 873 | |
| 874 | if (!avctx->extradata) { |
| 875 | res = AVERROR(ENOMEM); |
| 876 | goto error; |
| 877 | } |
| 878 | |
| 879 | memcpy(avctx->extradata, tmpHeader, outSize); |
| 880 | } |
| 881 | |
| 882 | if (ctx->encode_config.frameIntervalP > 1) |
| 883 | avctx->has_b_frames = 2; |
| 884 | |
| 885 | if (ctx->encode_config.rcParams.averageBitRate > 0) |
| 886 | avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate; |
| 887 | |
| 888 | return 0; |
| 889 | |
| 890 | error: |
| 891 | |
| 892 | for (i = 0; i < surfaceCount; ++i) { |
| 893 | p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[i].input_surface); |
| 894 | if (ctx->output_surfaces[i].output_surface) |
| 895 | p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->output_surfaces[i].output_surface); |
| 896 | } |
| 897 | |
| 898 | if (ctx->nvencoder) |
| 899 | p_nvenc->nvEncDestroyEncoder(ctx->nvencoder); |
| 900 | |
| 901 | if (ctx->cu_context) |
| 902 | dl_fn->cu_ctx_destroy(ctx->cu_context); |
| 903 | |
| 904 | av_frame_free(&avctx->coded_frame); |
| 905 | |
| 906 | nvenc_unload_nvenc(avctx); |
| 907 | |
| 908 | ctx->nvencoder = NULL; |
| 909 | ctx->cu_context = NULL; |
| 910 | |
| 911 | return res; |
| 912 | } |
| 913 | |
| 914 | static av_cold int nvenc_encode_close(AVCodecContext *avctx) |
| 915 | { |
| 916 | NvencContext *ctx = avctx->priv_data; |
| 917 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 918 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 919 | int i; |
| 920 | |
| 921 | av_freep(&ctx->timestamp_list.data); |
| 922 | av_freep(&ctx->output_surface_ready_queue.data); |
| 923 | av_freep(&ctx->output_surface_queue.data); |
| 924 | |
| 925 | for (i = 0; i < ctx->max_surface_count; ++i) { |
| 926 | p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->input_surfaces[i].input_surface); |
| 927 | p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->output_surfaces[i].output_surface); |
| 928 | } |
| 929 | ctx->max_surface_count = 0; |
| 930 | |
| 931 | p_nvenc->nvEncDestroyEncoder(ctx->nvencoder); |
| 932 | ctx->nvencoder = NULL; |
| 933 | |
| 934 | dl_fn->cu_ctx_destroy(ctx->cu_context); |
| 935 | ctx->cu_context = NULL; |
| 936 | |
| 937 | nvenc_unload_nvenc(avctx); |
| 938 | |
| 939 | av_frame_free(&avctx->coded_frame); |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, AVFrame *coded_frame, NvencOutputSurface *tmpoutsurf) |
| 945 | { |
| 946 | NvencContext *ctx = avctx->priv_data; |
| 947 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 948 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 949 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 950 | uint32_t slice_mode_data; |
| 951 | uint32_t *slice_offsets; |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 952 | NV_ENC_LOCK_BITSTREAM lock_params = { 0 }; |
| 953 | NVENCSTATUS nv_status; |
| 954 | int res = 0; |
| 955 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 956 | switch (avctx->codec->id) { |
| 957 | case AV_CODEC_ID_H264: |
| 958 | slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData; |
| 959 | break; |
| 960 | case AV_CODEC_ID_H265: |
| 961 | slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData; |
| 962 | break; |
| 963 | default: |
| 964 | av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n"); |
| 965 | res = AVERROR(EINVAL); |
| 966 | goto error; |
| 967 | } |
| 968 | slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets)); |
| 969 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 970 | if (!slice_offsets) |
| 971 | return AVERROR(ENOMEM); |
| 972 | |
| 973 | lock_params.version = NV_ENC_LOCK_BITSTREAM_VER; |
| 974 | |
| 975 | lock_params.doNotWait = 0; |
| 976 | lock_params.outputBitstream = tmpoutsurf->output_surface; |
| 977 | lock_params.sliceOffsets = slice_offsets; |
| 978 | |
| 979 | nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params); |
| 980 | if (nv_status != NV_ENC_SUCCESS) { |
| 981 | av_log(avctx, AV_LOG_ERROR, "Failed locking bitstream buffer\n"); |
| 982 | res = AVERROR_EXTERNAL; |
| 983 | goto error; |
| 984 | } |
| 985 | |
| 986 | if (res = ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes)) { |
| 987 | p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface); |
| 988 | goto error; |
| 989 | } |
| 990 | |
| 991 | memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes); |
| 992 | |
| 993 | nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface); |
| 994 | if (nv_status != NV_ENC_SUCCESS) |
| 995 | av_log(avctx, AV_LOG_ERROR, "Failed unlocking bitstream buffer, expect the gates of mordor to open\n"); |
| 996 | |
| 997 | switch (lock_params.pictureType) { |
| 998 | case NV_ENC_PIC_TYPE_IDR: |
| 999 | pkt->flags |= AV_PKT_FLAG_KEY; |
| 1000 | case NV_ENC_PIC_TYPE_I: |
| 1001 | avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I; |
| 1002 | break; |
| 1003 | case NV_ENC_PIC_TYPE_P: |
| 1004 | avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P; |
| 1005 | break; |
| 1006 | case NV_ENC_PIC_TYPE_B: |
| 1007 | avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B; |
| 1008 | break; |
| 1009 | case NV_ENC_PIC_TYPE_BI: |
| 1010 | avctx->coded_frame->pict_type = AV_PICTURE_TYPE_BI; |
| 1011 | break; |
| 1012 | default: |
| 1013 | av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n"); |
| 1014 | av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n"); |
| 1015 | res = AVERROR_EXTERNAL; |
| 1016 | goto error; |
| 1017 | } |
| 1018 | |
| 1019 | pkt->pts = lock_params.outputTimeStamp; |
| 1020 | pkt->dts = timestamp_queue_dequeue(&ctx->timestamp_list); |
| 1021 | |
Timo Rothenpieler | 914fd42 | 2015-01-26 12:28:21 | [diff] [blame] | 1022 | /* when there're b frame(s), set dts offset */ |
agathah | 72c61c2 | 2015-01-07 09:19:32 | [diff] [blame] | 1023 | if (ctx->encode_config.frameIntervalP >= 2) |
| 1024 | pkt->dts -= 1; |
| 1025 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1026 | if (pkt->dts > pkt->pts) |
| 1027 | pkt->dts = pkt->pts; |
| 1028 | |
| 1029 | if (ctx->last_dts != AV_NOPTS_VALUE && pkt->dts <= ctx->last_dts) |
| 1030 | pkt->dts = ctx->last_dts + 1; |
| 1031 | |
| 1032 | ctx->last_dts = pkt->dts; |
| 1033 | |
| 1034 | av_free(slice_offsets); |
| 1035 | |
| 1036 | return 0; |
| 1037 | |
| 1038 | error: |
| 1039 | |
| 1040 | av_free(slice_offsets); |
| 1041 | timestamp_queue_dequeue(&ctx->timestamp_list); |
| 1042 | |
| 1043 | return res; |
| 1044 | } |
| 1045 | |
| 1046 | static int nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
| 1047 | const AVFrame *frame, int *got_packet) |
| 1048 | { |
| 1049 | NVENCSTATUS nv_status; |
| 1050 | NvencOutputSurface *tmpoutsurf; |
| 1051 | int res, i = 0; |
| 1052 | |
| 1053 | NvencContext *ctx = avctx->priv_data; |
| 1054 | NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; |
| 1055 | NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; |
| 1056 | |
| 1057 | NV_ENC_PIC_PARAMS pic_params = { 0 }; |
| 1058 | pic_params.version = NV_ENC_PIC_PARAMS_VER; |
| 1059 | |
| 1060 | if (frame) { |
| 1061 | NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 }; |
| 1062 | NvencInputSurface *inSurf = NULL; |
| 1063 | |
| 1064 | for (i = 0; i < ctx->max_surface_count; ++i) { |
| 1065 | if (!ctx->input_surfaces[i].lockCount) { |
| 1066 | inSurf = &ctx->input_surfaces[i]; |
| 1067 | break; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | av_assert0(inSurf); |
| 1072 | |
| 1073 | inSurf->lockCount = 1; |
| 1074 | |
| 1075 | lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER; |
| 1076 | lockBufferParams.inputBuffer = inSurf->input_surface; |
| 1077 | |
| 1078 | nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams); |
| 1079 | if (nv_status != NV_ENC_SUCCESS) { |
| 1080 | av_log(avctx, AV_LOG_ERROR, "Failed locking nvenc input buffer\n"); |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) { |
| 1085 | uint8_t *buf = lockBufferParams.bufferDataPtr; |
| 1086 | |
| 1087 | av_image_copy_plane(buf, lockBufferParams.pitch, |
| 1088 | frame->data[0], frame->linesize[0], |
| 1089 | avctx->width, avctx->height); |
| 1090 | |
| 1091 | buf += inSurf->height * lockBufferParams.pitch; |
| 1092 | |
| 1093 | av_image_copy_plane(buf, lockBufferParams.pitch >> 1, |
| 1094 | frame->data[2], frame->linesize[2], |
| 1095 | avctx->width >> 1, avctx->height >> 1); |
| 1096 | |
| 1097 | buf += (inSurf->height * lockBufferParams.pitch) >> 2; |
| 1098 | |
| 1099 | av_image_copy_plane(buf, lockBufferParams.pitch >> 1, |
| 1100 | frame->data[1], frame->linesize[1], |
| 1101 | avctx->width >> 1, avctx->height >> 1); |
| 1102 | } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) { |
| 1103 | uint8_t *buf = lockBufferParams.bufferDataPtr; |
| 1104 | |
| 1105 | av_image_copy_plane(buf, lockBufferParams.pitch, |
| 1106 | frame->data[0], frame->linesize[0], |
| 1107 | avctx->width, avctx->height); |
| 1108 | |
| 1109 | buf += inSurf->height * lockBufferParams.pitch; |
| 1110 | |
| 1111 | av_image_copy_plane(buf, lockBufferParams.pitch, |
| 1112 | frame->data[1], frame->linesize[1], |
| 1113 | avctx->width, avctx->height >> 1); |
| 1114 | } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P) { |
| 1115 | uint8_t *buf = lockBufferParams.bufferDataPtr; |
| 1116 | |
| 1117 | av_image_copy_plane(buf, lockBufferParams.pitch, |
| 1118 | frame->data[0], frame->linesize[0], |
| 1119 | avctx->width, avctx->height); |
| 1120 | |
| 1121 | buf += inSurf->height * lockBufferParams.pitch; |
| 1122 | |
| 1123 | av_image_copy_plane(buf, lockBufferParams.pitch, |
| 1124 | frame->data[1], frame->linesize[1], |
| 1125 | avctx->width, avctx->height); |
| 1126 | |
| 1127 | buf += inSurf->height * lockBufferParams.pitch; |
| 1128 | |
| 1129 | av_image_copy_plane(buf, lockBufferParams.pitch, |
| 1130 | frame->data[2], frame->linesize[2], |
| 1131 | avctx->width, avctx->height); |
| 1132 | } else { |
| 1133 | av_log(avctx, AV_LOG_FATAL, "Invalid pixel format!\n"); |
| 1134 | return AVERROR(EINVAL); |
| 1135 | } |
| 1136 | |
| 1137 | nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, inSurf->input_surface); |
| 1138 | if (nv_status != NV_ENC_SUCCESS) { |
| 1139 | av_log(avctx, AV_LOG_FATAL, "Failed unlocking input buffer!\n"); |
| 1140 | return AVERROR_EXTERNAL; |
| 1141 | } |
| 1142 | |
| 1143 | for (i = 0; i < ctx->max_surface_count; ++i) |
| 1144 | if (!ctx->output_surfaces[i].busy) |
| 1145 | break; |
| 1146 | |
| 1147 | if (i == ctx->max_surface_count) { |
| 1148 | inSurf->lockCount = 0; |
| 1149 | av_log(avctx, AV_LOG_FATAL, "No free output surface found!\n"); |
| 1150 | return AVERROR_EXTERNAL; |
| 1151 | } |
| 1152 | |
| 1153 | ctx->output_surfaces[i].input_surface = inSurf; |
| 1154 | |
| 1155 | pic_params.inputBuffer = inSurf->input_surface; |
| 1156 | pic_params.bufferFmt = inSurf->format; |
| 1157 | pic_params.inputWidth = avctx->width; |
| 1158 | pic_params.inputHeight = avctx->height; |
| 1159 | pic_params.outputBitstream = ctx->output_surfaces[i].output_surface; |
| 1160 | pic_params.completionEvent = 0; |
| 1161 | |
| 1162 | if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) { |
| 1163 | if (frame->top_field_first) { |
| 1164 | pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM; |
| 1165 | } else { |
| 1166 | pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP; |
| 1167 | } |
| 1168 | } else { |
| 1169 | pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME; |
| 1170 | } |
| 1171 | |
| 1172 | pic_params.encodePicFlags = 0; |
| 1173 | pic_params.inputTimeStamp = frame->pts; |
| 1174 | pic_params.inputDuration = 0; |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 1175 | switch (avctx->codec->id) { |
| 1176 | case AV_CODEC_ID_H264: |
| 1177 | pic_params.codecPicParams.h264PicParams.sliceMode = ctx->encode_config.encodeCodecConfig.h264Config.sliceMode; |
| 1178 | pic_params.codecPicParams.h264PicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData; |
| 1179 | break; |
| 1180 | case AV_CODEC_ID_H265: |
| 1181 | pic_params.codecPicParams.hevcPicParams.sliceMode = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode; |
| 1182 | pic_params.codecPicParams.hevcPicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData; |
| 1183 | break; |
| 1184 | default: |
| 1185 | av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n"); |
| 1186 | return AVERROR(EINVAL); |
| 1187 | } |
Timo Rothenpieler | bc3f767 | 2015-01-16 00:02:40 | [diff] [blame] | 1188 | |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1189 | res = timestamp_queue_enqueue(&ctx->timestamp_list, frame->pts); |
| 1190 | |
| 1191 | if (res) |
| 1192 | return res; |
| 1193 | } else { |
| 1194 | pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS; |
| 1195 | } |
| 1196 | |
| 1197 | nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params); |
| 1198 | |
| 1199 | if (frame && nv_status == NV_ENC_ERR_NEED_MORE_INPUT) { |
| 1200 | res = out_surf_queue_enqueue(&ctx->output_surface_queue, &ctx->output_surfaces[i]); |
| 1201 | |
| 1202 | if (res) |
| 1203 | return res; |
| 1204 | |
| 1205 | ctx->output_surfaces[i].busy = 1; |
| 1206 | } |
| 1207 | |
| 1208 | if (nv_status != NV_ENC_SUCCESS && nv_status != NV_ENC_ERR_NEED_MORE_INPUT) { |
| 1209 | av_log(avctx, AV_LOG_ERROR, "EncodePicture failed!\n"); |
| 1210 | return AVERROR_EXTERNAL; |
| 1211 | } |
| 1212 | |
| 1213 | if (nv_status != NV_ENC_ERR_NEED_MORE_INPUT) { |
| 1214 | while (ctx->output_surface_queue.count) { |
| 1215 | tmpoutsurf = out_surf_queue_dequeue(&ctx->output_surface_queue); |
| 1216 | res = out_surf_queue_enqueue(&ctx->output_surface_ready_queue, tmpoutsurf); |
| 1217 | |
| 1218 | if (res) |
| 1219 | return res; |
| 1220 | } |
| 1221 | |
| 1222 | if (frame) { |
| 1223 | res = out_surf_queue_enqueue(&ctx->output_surface_ready_queue, &ctx->output_surfaces[i]); |
| 1224 | |
| 1225 | if (res) |
| 1226 | return res; |
| 1227 | |
| 1228 | ctx->output_surfaces[i].busy = 1; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | if (ctx->output_surface_ready_queue.count) { |
| 1233 | tmpoutsurf = out_surf_queue_dequeue(&ctx->output_surface_ready_queue); |
| 1234 | |
| 1235 | res = process_output_surface(avctx, pkt, avctx->coded_frame, tmpoutsurf); |
| 1236 | |
| 1237 | if (res) |
| 1238 | return res; |
| 1239 | |
| 1240 | tmpoutsurf->busy = 0; |
| 1241 | av_assert0(tmpoutsurf->input_surface->lockCount); |
| 1242 | tmpoutsurf->input_surface->lockCount--; |
| 1243 | |
| 1244 | *got_packet = 1; |
| 1245 | } else { |
| 1246 | *got_packet = 0; |
| 1247 | } |
| 1248 | |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | static enum AVPixelFormat pix_fmts_nvenc[] = { |
| 1253 | AV_PIX_FMT_NV12, |
| 1254 | AV_PIX_FMT_NONE |
| 1255 | }; |
| 1256 | |
| 1257 | #define OFFSET(x) offsetof(NvencContext, x) |
| 1258 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM |
| 1259 | static const AVOption options[] = { |
| 1260 | { "preset", "Set the encoding preset (one of hq, hp, bd, ll, llhq, llhp, default)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "hq" }, 0, 0, VE }, |
| 1261 | { "cbr", "Use cbr encoding mode", OFFSET(cbr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, |
| 1262 | { "2pass", "Use 2pass cbr encoding mode (low latency mode only)", OFFSET(twopass), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1263 | { "gpu", "Selects which NVENC capable GPU to use. First GPU is 0, second is 1, and so on.", OFFSET(gpu), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, |
| 1264 | { NULL } |
| 1265 | }; |
| 1266 | |
| 1267 | static const AVClass nvenc_class = { |
| 1268 | .class_name = "nvenc", |
| 1269 | .item_name = av_default_item_name, |
| 1270 | .option = options, |
| 1271 | .version = LIBAVUTIL_VERSION_INT, |
| 1272 | }; |
| 1273 | |
| 1274 | static const AVCodecDefault nvenc_defaults[] = { |
| 1275 | { "b", "0" }, |
| 1276 | { "qmin", "-1" }, |
| 1277 | { "qmax", "-1" }, |
| 1278 | { "qdiff", "-1" }, |
| 1279 | { "qblur", "-1" }, |
| 1280 | { "qcomp", "-1" }, |
| 1281 | { NULL }, |
| 1282 | }; |
| 1283 | |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 1284 | #if CONFIG_NVENC_ENCODER |
Timo Rothenpieler | 2a428db | 2014-11-29 23:04:37 | [diff] [blame] | 1285 | AVCodec ff_nvenc_encoder = { |
| 1286 | .name = "nvenc", |
| 1287 | .long_name = NULL_IF_CONFIG_SMALL("Nvidia NVENC h264 encoder"), |
| 1288 | .type = AVMEDIA_TYPE_VIDEO, |
| 1289 | .id = AV_CODEC_ID_H264, |
| 1290 | .priv_data_size = sizeof(NvencContext), |
| 1291 | .init = nvenc_encode_init, |
| 1292 | .encode2 = nvenc_encode_frame, |
| 1293 | .close = nvenc_encode_close, |
| 1294 | .capabilities = CODEC_CAP_DELAY, |
| 1295 | .priv_class = &nvenc_class, |
| 1296 | .defaults = nvenc_defaults, |
| 1297 | .pix_fmts = pix_fmts_nvenc, |
| 1298 | }; |
Philip Langdale | 21175d8 | 2015-03-24 04:34:59 | [diff] [blame^] | 1299 | #endif |
| 1300 | |
| 1301 | #if CONFIG_NVENC_H265_ENCODER |
| 1302 | AVCodec ff_nvenc_h265_encoder = { |
| 1303 | .name = "nvenc_h265", |
| 1304 | .long_name = NULL_IF_CONFIG_SMALL("Nvidia NVENC h265 encoder"), |
| 1305 | .type = AVMEDIA_TYPE_VIDEO, |
| 1306 | .id = AV_CODEC_ID_H265, |
| 1307 | .priv_data_size = sizeof(NvencContext), |
| 1308 | .init = nvenc_encode_init, |
| 1309 | .encode2 = nvenc_encode_frame, |
| 1310 | .close = nvenc_encode_close, |
| 1311 | .capabilities = CODEC_CAP_DELAY, |
| 1312 | .priv_class = &nvenc_class, |
| 1313 | .defaults = nvenc_defaults, |
| 1314 | .pix_fmts = pix_fmts_nvenc, |
| 1315 | }; |
| 1316 | #endif |