blob: dba14a167ad02a36f43f91600af2743e226f1bef [file] [log] [blame]
Timo Rothenpieler2a428db2014-11-29 23:04:371/*
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
Andrey Turkin7aa16d52016-05-25 13:04:2822#include "config.h"
23
Timo Rothenpieler1efdb0a2014-12-25 13:55:3124#if defined(_WIN32)
Timo Rothenpieler2a428db2014-11-29 23:04:3725#include <windows.h>
Andrey Turkin0d021cc2016-05-29 12:23:2626
27#define CUDA_LIBNAME TEXT("nvcuda.dll")
28#if ARCH_X86_64
29#define NVENC_LIBNAME TEXT("nvEncodeAPI64.dll")
Timo Rothenpieler2a428db2014-11-29 23:04:3730#else
Andrey Turkin0d021cc2016-05-29 12:23:2631#define NVENC_LIBNAME TEXT("nvEncodeAPI.dll")
Timo Rothenpieler2a428db2014-11-29 23:04:3732#endif
33
Andrey Turkin0d021cc2016-05-29 12:23:2634#define dlopen(filename, flags) LoadLibrary((filename))
35#define dlsym(handle, symbol) GetProcAddress(handle, symbol)
36#define dlclose(handle) FreeLibrary(handle)
37#else
38#include <dlfcn.h>
39
40#define CUDA_LIBNAME "libcuda.so"
41#define NVENC_LIBNAME "libnvidia-encode.so"
42#endif
43
44#include "libavutil/hwcontext.h"
Timo Rothenpieler2a428db2014-11-29 23:04:3745#include "libavutil/imgutils.h"
46#include "libavutil/avassert.h"
Timo Rothenpieler2a428db2014-11-29 23:04:3747#include "libavutil/mem.h"
Timo Rothenpieler2a428db2014-11-29 23:04:3748#include "internal.h"
Andrey Turkin7aa16d52016-05-25 13:04:2849#include "nvenc.h"
Andrey Turkina8cf25d2016-05-20 22:08:0650
Andrey Turkin0d021cc2016-05-29 12:23:2651#define NVENC_CAP 0x30
Andrey Turkinf84dfbc2016-05-25 16:39:5452#define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
53 rc == NV_ENC_PARAMS_RC_2_PASS_QUALITY || \
54 rc == NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP)
55
Andrey Turkin0d021cc2016-05-29 12:23:2656#define LOAD_LIBRARY(l, path) \
57 do { \
58 if (!((l) = dlopen(path, RTLD_LAZY))) { \
59 av_log(avctx, AV_LOG_ERROR, \
60 "Cannot load %s\n", \
61 path); \
62 return AVERROR_UNKNOWN; \
63 } \
64 } while (0)
65
66#define LOAD_SYMBOL(fun, lib, symbol) \
67 do { \
68 if (!((fun) = dlsym(lib, symbol))) { \
69 av_log(avctx, AV_LOG_ERROR, \
70 "Cannot load %s\n", \
71 symbol); \
72 return AVERROR_UNKNOWN; \
73 } \
74 } while (0)
Timo Rothenpieler2a428db2014-11-29 23:04:3775
Andrey Turkin7aa16d52016-05-25 13:04:2876const enum AVPixelFormat ff_nvenc_pix_fmts[] = {
77 AV_PIX_FMT_YUV420P,
78 AV_PIX_FMT_NV12,
79 AV_PIX_FMT_YUV444P,
80#if CONFIG_CUDA
81 AV_PIX_FMT_CUDA,
82#endif
83 AV_PIX_FMT_NONE
84};
Timo Rothenpieler2a428db2014-11-29 23:04:3785
Andrey Turkine1691c42016-05-20 15:37:0086static const struct {
87 NVENCSTATUS nverr;
88 int averr;
89 const char *desc;
90} nvenc_errors[] = {
91 { NV_ENC_SUCCESS, 0, "success" },
92 { NV_ENC_ERR_NO_ENCODE_DEVICE, AVERROR(ENOENT), "no encode device" },
93 { NV_ENC_ERR_UNSUPPORTED_DEVICE, AVERROR(ENOSYS), "unsupported device" },
94 { NV_ENC_ERR_INVALID_ENCODERDEVICE, AVERROR(EINVAL), "invalid encoder device" },
95 { NV_ENC_ERR_INVALID_DEVICE, AVERROR(EINVAL), "invalid device" },
96 { NV_ENC_ERR_DEVICE_NOT_EXIST, AVERROR(EIO), "device does not exist" },
97 { NV_ENC_ERR_INVALID_PTR, AVERROR(EFAULT), "invalid ptr" },
98 { NV_ENC_ERR_INVALID_EVENT, AVERROR(EINVAL), "invalid event" },
99 { NV_ENC_ERR_INVALID_PARAM, AVERROR(EINVAL), "invalid param" },
100 { NV_ENC_ERR_INVALID_CALL, AVERROR(EINVAL), "invalid call" },
101 { NV_ENC_ERR_OUT_OF_MEMORY, AVERROR(ENOMEM), "out of memory" },
102 { NV_ENC_ERR_ENCODER_NOT_INITIALIZED, AVERROR(EINVAL), "encoder not initialized" },
103 { NV_ENC_ERR_UNSUPPORTED_PARAM, AVERROR(ENOSYS), "unsupported param" },
104 { NV_ENC_ERR_LOCK_BUSY, AVERROR(EAGAIN), "lock busy" },
105 { NV_ENC_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOBUFS), "not enough buffer" },
106 { NV_ENC_ERR_INVALID_VERSION, AVERROR(EINVAL), "invalid version" },
107 { NV_ENC_ERR_MAP_FAILED, AVERROR(EIO), "map failed" },
108 { NV_ENC_ERR_NEED_MORE_INPUT, AVERROR(EAGAIN), "need more input" },
109 { NV_ENC_ERR_ENCODER_BUSY, AVERROR(EAGAIN), "encoder busy" },
110 { NV_ENC_ERR_EVENT_NOT_REGISTERD, AVERROR(EBADF), "event not registered" },
111 { NV_ENC_ERR_GENERIC, AVERROR_UNKNOWN, "generic error" },
112 { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, AVERROR(EINVAL), "incompatible client key" },
113 { NV_ENC_ERR_UNIMPLEMENTED, AVERROR(ENOSYS), "unimplemented" },
114 { NV_ENC_ERR_RESOURCE_REGISTER_FAILED, AVERROR(EIO), "resource register failed" },
115 { NV_ENC_ERR_RESOURCE_NOT_REGISTERED, AVERROR(EBADF), "resource not registered" },
116 { NV_ENC_ERR_RESOURCE_NOT_MAPPED, AVERROR(EBADF), "resource not mapped" },
117};
118
119static int nvenc_map_error(NVENCSTATUS err, const char **desc)
120{
121 int i;
122 for (i = 0; i < FF_ARRAY_ELEMS(nvenc_errors); i++) {
123 if (nvenc_errors[i].nverr == err) {
124 if (desc)
125 *desc = nvenc_errors[i].desc;
126 return nvenc_errors[i].averr;
127 }
128 }
129 if (desc)
130 *desc = "unknown error";
131 return AVERROR_UNKNOWN;
132}
133
134static int nvenc_print_error(void *log_ctx, NVENCSTATUS err,
135 const char *error_string)
136{
137 const char *desc;
138 int ret;
139 ret = nvenc_map_error(err, &desc);
140 av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
141 return ret;
142}
143
Andrey Turkincfb49fc2016-05-20 16:13:20144static void timestamp_queue_enqueue(AVFifoBuffer* queue, int64_t timestamp)
Timo Rothenpieler2a428db2014-11-29 23:04:37145{
Andrey Turkincfb49fc2016-05-20 16:13:20146 av_fifo_generic_write(queue, &timestamp, sizeof(timestamp), NULL);
Timo Rothenpieler2a428db2014-11-29 23:04:37147}
148
Andrey Turkincfb49fc2016-05-20 16:13:20149static int64_t timestamp_queue_dequeue(AVFifoBuffer* queue)
Timo Rothenpieler2a428db2014-11-29 23:04:37150{
Andrey Turkincfb49fc2016-05-20 16:13:20151 int64_t timestamp = AV_NOPTS_VALUE;
152 if (av_fifo_size(queue) > 0)
153 av_fifo_generic_read(queue, &timestamp, sizeof(timestamp), NULL);
Timo Rothenpieler2a428db2014-11-29 23:04:37154
Andrey Turkincfb49fc2016-05-20 16:13:20155 return timestamp;
Timo Rothenpieler2a428db2014-11-29 23:04:37156}
157
Andrey Turkin0d021cc2016-05-29 12:23:26158static av_cold int nvenc_load_libraries(AVCodecContext *avctx)
Timo Rothenpieler2a428db2014-11-29 23:04:37159{
160 NvencContext *ctx = avctx->priv_data;
161 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
Andrey Turkin0d021cc2016-05-29 12:23:26162 PNVENCODEAPICREATEINSTANCE nvenc_create_instance;
163 NVENCSTATUS err;
Timo Rothenpieler2a428db2014-11-29 23:04:37164
Andrey Turkina8cf25d2016-05-20 22:08:06165#if CONFIG_CUDA
166 dl_fn->cu_init = cuInit;
167 dl_fn->cu_device_get_count = cuDeviceGetCount;
168 dl_fn->cu_device_get = cuDeviceGet;
169 dl_fn->cu_device_get_name = cuDeviceGetName;
170 dl_fn->cu_device_compute_capability = cuDeviceComputeCapability;
171 dl_fn->cu_ctx_create = cuCtxCreate_v2;
172 dl_fn->cu_ctx_pop_current = cuCtxPopCurrent_v2;
173 dl_fn->cu_ctx_destroy = cuCtxDestroy_v2;
Andrey Turkina8cf25d2016-05-20 22:08:06174#else
Andrey Turkin0d021cc2016-05-29 12:23:26175 LOAD_LIBRARY(dl_fn->cuda, CUDA_LIBNAME);
Timo Rothenpieler2a428db2014-11-29 23:04:37176
Andrey Turkin0d021cc2016-05-29 12:23:26177 LOAD_SYMBOL(dl_fn->cu_init, dl_fn->cuda, "cuInit");
178 LOAD_SYMBOL(dl_fn->cu_device_get_count, dl_fn->cuda, "cuDeviceGetCount");
179 LOAD_SYMBOL(dl_fn->cu_device_get, dl_fn->cuda, "cuDeviceGet");
180 LOAD_SYMBOL(dl_fn->cu_device_get_name, dl_fn->cuda, "cuDeviceGetName");
181 LOAD_SYMBOL(dl_fn->cu_device_compute_capability, dl_fn->cuda,
182 "cuDeviceComputeCapability");
183 LOAD_SYMBOL(dl_fn->cu_ctx_create, dl_fn->cuda, "cuCtxCreate_v2");
184 LOAD_SYMBOL(dl_fn->cu_ctx_pop_current, dl_fn->cuda, "cuCtxPopCurrent_v2");
185 LOAD_SYMBOL(dl_fn->cu_ctx_destroy, dl_fn->cuda, "cuCtxDestroy_v2");
Timo Rothenpieler2a428db2014-11-29 23:04:37186#endif
187
Andrey Turkin0d021cc2016-05-29 12:23:26188 LOAD_LIBRARY(dl_fn->nvenc, NVENC_LIBNAME);
Timo Rothenpieler2a428db2014-11-29 23:04:37189
Andrey Turkin0d021cc2016-05-29 12:23:26190 LOAD_SYMBOL(nvenc_create_instance, dl_fn->nvenc,
191 "NvEncodeAPICreateInstance");
Timo Rothenpieler2a428db2014-11-29 23:04:37192
193 dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
194
Andrey Turkin0d021cc2016-05-29 12:23:26195 err = nvenc_create_instance(&dl_fn->nvenc_funcs);
196 if (err != NV_ENC_SUCCESS)
197 return nvenc_print_error(avctx, err, "Failed to create nvenc instance");
Timo Rothenpieler2a428db2014-11-29 23:04:37198
199 av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n");
200
Andrey Turkin0d021cc2016-05-29 12:23:26201 return 0;
202}
Timo Rothenpieler2a428db2014-11-29 23:04:37203
Andrey Turkin0d021cc2016-05-29 12:23:26204static av_cold int nvenc_open_session(AVCodecContext *avctx)
205{
206 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
207 NvencContext *ctx = avctx->priv_data;
208 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
209 NVENCSTATUS ret;
Timo Rothenpieler2a428db2014-11-29 23:04:37210
Andrey Turkin0d021cc2016-05-29 12:23:26211 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
212 params.apiVersion = NVENCAPI_VERSION;
213 params.device = ctx->cu_context;
214 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
215
216 ret = p_nvenc->nvEncOpenEncodeSessionEx(&params, &ctx->nvencoder);
217 if (ret != NV_ENC_SUCCESS) {
218 ctx->nvencoder = NULL;
219 return nvenc_print_error(avctx, ret, "OpenEncodeSessionEx failed");
220 }
Timo Rothenpieler2a428db2014-11-29 23:04:37221
222 return 0;
223}
224
Andrey Turkin0d021cc2016-05-29 12:23:26225static int nvenc_check_codec_support(AVCodecContext *avctx)
226{
227 NvencContext *ctx = avctx->priv_data;
228 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
229 int i, ret, count = 0;
230 GUID *guids = NULL;
231
232 ret = p_nvenc->nvEncGetEncodeGUIDCount(ctx->nvencoder, &count);
233
234 if (ret != NV_ENC_SUCCESS || !count)
235 return AVERROR(ENOSYS);
236
237 guids = av_malloc(count * sizeof(GUID));
238 if (!guids)
239 return AVERROR(ENOMEM);
240
241 ret = p_nvenc->nvEncGetEncodeGUIDs(ctx->nvencoder, guids, count, &count);
242 if (ret != NV_ENC_SUCCESS) {
243 ret = AVERROR(ENOSYS);
244 goto fail;
245 }
246
247 ret = AVERROR(ENOSYS);
248 for (i = 0; i < count; i++) {
249 if (!memcmp(&guids[i], &ctx->init_encode_params.encodeGUID, sizeof(*guids))) {
250 ret = 0;
251 break;
252 }
253 }
254
255fail:
256 av_free(guids);
257
258 return ret;
259}
260
261static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap)
262{
263 NvencContext *ctx = avctx->priv_data;
264 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &ctx->nvenc_dload_funcs.nvenc_funcs;
265 NV_ENC_CAPS_PARAM params = { 0 };
266 int ret, val = 0;
267
268 params.version = NV_ENC_CAPS_PARAM_VER;
269 params.capsToQuery = cap;
270
271 ret = p_nvenc->nvEncGetEncodeCaps(ctx->nvencoder, ctx->init_encode_params.encodeGUID, &params, &val);
272
273 if (ret == NV_ENC_SUCCESS)
274 return val;
275 return 0;
276}
277
278static int nvenc_check_capabilities(AVCodecContext *avctx)
279{
280 NvencContext *ctx = avctx->priv_data;
281 int ret;
282
283 ret = nvenc_check_codec_support(avctx);
284 if (ret < 0) {
285 av_log(avctx, AV_LOG_VERBOSE, "Codec not supported\n");
286 return ret;
287 }
288
289 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_YUV444_ENCODE);
290 if (ctx->data_pix_fmt == AV_PIX_FMT_YUV444P && ret <= 0) {
291 av_log(avctx, AV_LOG_VERBOSE, "YUV444P not supported\n");
292 return AVERROR(ENOSYS);
293 }
294
295 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE);
296 if (ctx->preset >= PRESET_LOSSLESS_DEFAULT && ret <= 0) {
297 av_log(avctx, AV_LOG_VERBOSE, "Lossless encoding not supported\n");
298 return AVERROR(ENOSYS);
299 }
300
301 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_WIDTH_MAX);
302 if (ret < avctx->width) {
303 av_log(avctx, AV_LOG_VERBOSE, "Width %d exceeds %d\n",
304 avctx->width, ret);
305 return AVERROR(ENOSYS);
306 }
307
308 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_HEIGHT_MAX);
309 if (ret < avctx->height) {
310 av_log(avctx, AV_LOG_VERBOSE, "Height %d exceeds %d\n",
311 avctx->height, ret);
312 return AVERROR(ENOSYS);
313 }
314
315 ret = nvenc_check_cap(avctx, NV_ENC_CAPS_NUM_MAX_BFRAMES);
316 if (ret < avctx->max_b_frames) {
317 av_log(avctx, AV_LOG_VERBOSE, "Max b-frames %d exceed %d\n",
318 avctx->max_b_frames, ret);
319
320 return AVERROR(ENOSYS);
321 }
322
323 return 0;
324}
325
326static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
327{
328 NvencContext *ctx = avctx->priv_data;
329 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
330 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
331 char name[128] = { 0};
332 int major, minor, ret;
333 CUresult cu_res;
334 CUdevice cu_device;
335 CUcontext dummy;
336 int loglevel = AV_LOG_VERBOSE;
337
338 if (ctx->device == LIST_DEVICES)
339 loglevel = AV_LOG_INFO;
340
341 cu_res = dl_fn->cu_device_get(&cu_device, idx);
342 if (cu_res != CUDA_SUCCESS) {
343 av_log(avctx, AV_LOG_ERROR,
344 "Cannot access the CUDA device %d\n",
345 idx);
346 return -1;
347 }
348
349 cu_res = dl_fn->cu_device_get_name(name, sizeof(name), cu_device);
350 if (cu_res != CUDA_SUCCESS)
351 return -1;
352
353 cu_res = dl_fn->cu_device_compute_capability(&major, &minor, cu_device);
354 if (cu_res != CUDA_SUCCESS)
355 return -1;
356
357 av_log(avctx, loglevel, "[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx, name, major, minor);
358 if (((major << 4) | minor) < NVENC_CAP) {
359 av_log(avctx, loglevel, "does not support NVENC\n");
360 goto fail;
361 }
362
363 cu_res = dl_fn->cu_ctx_create(&ctx->cu_context_internal, 0, cu_device);
364 if (cu_res != CUDA_SUCCESS) {
365 av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res);
366 goto fail;
367 }
368
369 ctx->cu_context = ctx->cu_context_internal;
370
371 cu_res = dl_fn->cu_ctx_pop_current(&dummy);
372 if (cu_res != CUDA_SUCCESS) {
373 av_log(avctx, AV_LOG_FATAL, "Failed popping CUDA context: 0x%x\n", (int)cu_res);
374 goto fail2;
375 }
376
377 if ((ret = nvenc_open_session(avctx)) < 0)
378 goto fail2;
379
380 if ((ret = nvenc_check_capabilities(avctx)) < 0)
381 goto fail3;
382
383 av_log(avctx, loglevel, "supports NVENC\n");
384
385 dl_fn->nvenc_device_count++;
386
387 if (ctx->device == dl_fn->nvenc_device_count - 1 || ctx->device == ANY_DEVICE)
388 return 0;
389
390fail3:
391 p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
392 ctx->nvencoder = NULL;
393
394fail2:
395 dl_fn->cu_ctx_destroy(ctx->cu_context_internal);
396 ctx->cu_context_internal = NULL;
397
398fail:
399 return AVERROR(ENOSYS);
400}
401
Andrey Turkin82d705e2016-05-20 14:49:24402static av_cold int nvenc_setup_device(AVCodecContext *avctx)
Timo Rothenpieler2a428db2014-11-29 23:04:37403{
Timo Rothenpieler2a428db2014-11-29 23:04:37404 NvencContext *ctx = avctx->priv_data;
405 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
Timo Rothenpieler2a428db2014-11-29 23:04:37406
Andrey Turkinfaffff82016-05-25 14:05:50407 switch (avctx->codec->id) {
408 case AV_CODEC_ID_H264:
409 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
410 break;
411 case AV_CODEC_ID_HEVC:
412 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
413 break;
414 default:
415 return AVERROR_BUG;
416 }
417
Andrey Turkina8cf25d2016-05-20 22:08:06418 if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
Andrey Turkin0d021cc2016-05-29 12:23:26419#if CONFIG_CUDA
420 AVHWFramesContext *frames_ctx;
Andrey Turkina8cf25d2016-05-20 22:08:06421 AVCUDADeviceContext *device_hwctx;
Andrey Turkin0d021cc2016-05-29 12:23:26422 int ret;
Andrey Turkina8cf25d2016-05-20 22:08:06423
Andrey Turkin0d021cc2016-05-29 12:23:26424 if (!avctx->hw_frames_ctx)
Andrey Turkina8cf25d2016-05-20 22:08:06425 return AVERROR(EINVAL);
Andrey Turkin0d021cc2016-05-29 12:23:26426
427 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
428 device_hwctx = frames_ctx->device_ctx->hwctx;
429
430 ctx->cu_context = device_hwctx->cuda_ctx;
431
432 ret = nvenc_open_session(avctx);
433 if (ret < 0)
434 return ret;
435
436 ret = nvenc_check_capabilities(avctx);
437 if (ret < 0) {
438 av_log(avctx, AV_LOG_FATAL, "Provided device doesn't support required NVENC features\n");
439 return ret;
440 }
441#else
442 return AVERROR_BUG;
443#endif
444 } else {
445 int i, nb_devices = 0;
446
447 if ((dl_fn->cu_init(0)) != CUDA_SUCCESS) {
448 av_log(avctx, AV_LOG_ERROR,
449 "Cannot init CUDA\n");
450 return AVERROR_UNKNOWN;
Andrey Turkina8cf25d2016-05-20 22:08:06451 }
452
Andrey Turkin0d021cc2016-05-29 12:23:26453 if ((dl_fn->cu_device_get_count(&nb_devices)) != CUDA_SUCCESS) {
454 av_log(avctx, AV_LOG_ERROR,
455 "Cannot enumerate the CUDA devices\n");
456 return AVERROR_UNKNOWN;
457 }
Andrey Turkina8cf25d2016-05-20 22:08:06458
Andrey Turkin0d021cc2016-05-29 12:23:26459 if (!nb_devices) {
460 av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
461 return AVERROR_EXTERNAL;
462 }
463
464 av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", nb_devices);
465
466 dl_fn->nvenc_device_count = 0;
467 for (i = 0; i < nb_devices; ++i) {
468 if ((nvenc_check_device(avctx, i)) >= 0 && ctx->device != LIST_DEVICES)
469 return 0;
470 }
471
472 if (ctx->device == LIST_DEVICES)
473 return AVERROR_EXIT;
474
475 if (!dl_fn->nvenc_device_count) {
476 av_log(avctx, AV_LOG_FATAL, "No NVENC capable devices found\n");
477 return AVERROR_EXTERNAL;
478 }
479
480 av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->device, dl_fn->nvenc_device_count);
Andrey Turkin82d705e2016-05-20 14:49:24481 return AVERROR(EINVAL);
Timo Rothenpieler2a428db2014-11-29 23:04:37482 }
483
Andrey Turkin82d705e2016-05-20 14:49:24484 return 0;
485}
486
Andrey Turkinfaffff82016-05-25 14:05:50487typedef struct GUIDTuple {
488 const GUID guid;
489 int flags;
490} GUIDTuple;
491
492static void nvenc_map_preset(NvencContext *ctx)
493{
494 GUIDTuple presets[] = {
495 { NV_ENC_PRESET_DEFAULT_GUID },
496 { NV_ENC_PRESET_HQ_GUID, NVENC_TWO_PASSES }, /* slow */
497 { NV_ENC_PRESET_HQ_GUID, NVENC_ONE_PASS }, /* medium */
498 { NV_ENC_PRESET_HP_GUID, NVENC_ONE_PASS }, /* fast */
499 { NV_ENC_PRESET_HP_GUID },
500 { NV_ENC_PRESET_HQ_GUID },
501 { NV_ENC_PRESET_BD_GUID },
502 { NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID, NVENC_LOWLATENCY },
503 { NV_ENC_PRESET_LOW_LATENCY_HQ_GUID, NVENC_LOWLATENCY },
504 { NV_ENC_PRESET_LOW_LATENCY_HP_GUID, NVENC_LOWLATENCY },
505 { NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID, NVENC_LOSSLESS },
506 { NV_ENC_PRESET_LOSSLESS_HP_GUID, NVENC_LOSSLESS },
507 };
508
509 GUIDTuple *t = &presets[ctx->preset];
510
511 ctx->init_encode_params.presetGUID = t->guid;
512 ctx->flags = t->flags;
513}
514
Andrey Turkin82d705e2016-05-20 14:49:24515static av_cold void set_constqp(AVCodecContext *avctx)
516{
517 NvencContext *ctx = avctx->priv_data;
Andrey Turkinf84dfbc2016-05-25 16:39:54518 NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
Andrey Turkin82d705e2016-05-20 14:49:24519
Andrey Turkinf84dfbc2016-05-25 16:39:54520 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
521 rc->constQP.qpInterB = avctx->global_quality;
522 rc->constQP.qpInterP = avctx->global_quality;
523 rc->constQP.qpIntra = avctx->global_quality;
524
525 avctx->qmin = -1;
526 avctx->qmax = -1;
Andrey Turkin82d705e2016-05-20 14:49:24527}
528
529static av_cold void set_vbr(AVCodecContext *avctx)
530{
531 NvencContext *ctx = avctx->priv_data;
Andrey Turkinf84dfbc2016-05-25 16:39:54532 NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
533 int qp_inter_p;
Andrey Turkin82d705e2016-05-20 14:49:24534
Andrey Turkinf84dfbc2016-05-25 16:39:54535 if (avctx->qmin >= 0 && avctx->qmax >= 0) {
536 rc->enableMinQP = 1;
537 rc->enableMaxQP = 1;
Andrey Turkin82d705e2016-05-20 14:49:24538
Andrey Turkinf84dfbc2016-05-25 16:39:54539 rc->minQP.qpInterB = avctx->qmin;
540 rc->minQP.qpInterP = avctx->qmin;
541 rc->minQP.qpIntra = avctx->qmin;
Andrey Turkin82d705e2016-05-20 14:49:24542
Andrey Turkinf84dfbc2016-05-25 16:39:54543 rc->maxQP.qpInterB = avctx->qmax;
544 rc->maxQP.qpInterP = avctx->qmax;
545 rc->maxQP.qpIntra = avctx->qmax;
546
547 qp_inter_p = (avctx->qmax + 3 * avctx->qmin) / 4; // biased towards Qmin
548 } else {
549 qp_inter_p = 26; // default to 26
550 }
551
552 rc->enableInitialRCQP = 1;
553 rc->initialRCQP.qpInterP = qp_inter_p;
554
555 if (avctx->i_quant_factor != 0.0 && avctx->b_quant_factor != 0.0) {
556 rc->initialRCQP.qpIntra = av_clip(
557 qp_inter_p * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51);
558 rc->initialRCQP.qpInterB = av_clip(
559 qp_inter_p * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 0, 51);
560 } else {
561 rc->initialRCQP.qpIntra = qp_inter_p;
562 rc->initialRCQP.qpInterB = qp_inter_p;
563 }
Andrey Turkin82d705e2016-05-20 14:49:24564}
565
566static av_cold void set_lossless(AVCodecContext *avctx)
567{
568 NvencContext *ctx = avctx->priv_data;
Andrey Turkinf84dfbc2016-05-25 16:39:54569 NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
Andrey Turkin82d705e2016-05-20 14:49:24570
Andrey Turkinf84dfbc2016-05-25 16:39:54571 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
572 rc->constQP.qpInterB = 0;
573 rc->constQP.qpInterP = 0;
574 rc->constQP.qpIntra = 0;
575
576 avctx->qmin = -1;
577 avctx->qmax = -1;
578}
579
580static void nvenc_override_rate_control(AVCodecContext *avctx)
581{
582 NvencContext *ctx = avctx->priv_data;
583 NV_ENC_RC_PARAMS *rc = &ctx->encode_config.rcParams;
584
585 switch (ctx->rc) {
586 case NV_ENC_PARAMS_RC_CONSTQP:
587 if (avctx->global_quality <= 0) {
588 av_log(avctx, AV_LOG_WARNING,
589 "The constant quality rate-control requires "
590 "the 'global_quality' option set.\n");
591 return;
592 }
593 set_constqp(avctx);
594 return;
595 case NV_ENC_PARAMS_RC_2_PASS_VBR:
596 case NV_ENC_PARAMS_RC_VBR:
597 if (avctx->qmin < 0 && avctx->qmax < 0) {
598 av_log(avctx, AV_LOG_WARNING,
599 "The variable bitrate rate-control requires "
600 "the 'qmin' and/or 'qmax' option set.\n");
601 set_vbr(avctx);
602 return;
603 }
604 case NV_ENC_PARAMS_RC_VBR_MINQP:
605 if (avctx->qmin < 0) {
606 av_log(avctx, AV_LOG_WARNING,
607 "The variable bitrate rate-control requires "
608 "the 'qmin' option set.\n");
609 set_vbr(avctx);
610 return;
611 }
612 set_vbr(avctx);
613 break;
614 case NV_ENC_PARAMS_RC_CBR:
615 break;
616 case NV_ENC_PARAMS_RC_2_PASS_QUALITY:
617 case NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP:
618 if (!(ctx->flags & NVENC_LOWLATENCY)) {
619 av_log(avctx, AV_LOG_WARNING,
620 "The multipass rate-control requires "
621 "a low-latency preset.\n");
622 return;
623 }
624 }
625
626 rc->rateControlMode = ctx->rc;
Andrey Turkin82d705e2016-05-20 14:49:24627}
628
Andrey Turkinfaffff82016-05-25 14:05:50629static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx)
Andrey Turkin82d705e2016-05-20 14:49:24630{
631 NvencContext *ctx = avctx->priv_data;
632
Andrey Turkin82d705e2016-05-20 14:49:24633 if (avctx->bit_rate > 0) {
634 ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate;
635 } else if (ctx->encode_config.rcParams.averageBitRate > 0) {
636 ctx->encode_config.rcParams.maxBitRate = ctx->encode_config.rcParams.averageBitRate;
637 }
638
639 if (avctx->rc_max_rate > 0)
640 ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
641
Andrey Turkinf84dfbc2016-05-25 16:39:54642 if (ctx->rc < 0) {
643 if (ctx->flags & NVENC_ONE_PASS)
644 ctx->twopass = 0;
645 if (ctx->flags & NVENC_TWO_PASSES)
646 ctx->twopass = 1;
647
648 if (ctx->twopass < 0)
649 ctx->twopass = (ctx->flags & NVENC_LOWLATENCY) != 0;
650
651 if (ctx->cbr) {
652 if (ctx->twopass) {
653 ctx->rc = NV_ENC_PARAMS_RC_2_PASS_QUALITY;
654 } else {
655 ctx->rc = NV_ENC_PARAMS_RC_CBR;
656 }
657 } else if (avctx->global_quality > 0) {
658 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
659 } else if (ctx->twopass) {
660 ctx->rc = NV_ENC_PARAMS_RC_2_PASS_VBR;
661 } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
662 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
663 }
664 }
665
Andrey Turkinfaffff82016-05-25 14:05:50666 if (ctx->flags & NVENC_LOSSLESS) {
Andrey Turkin82d705e2016-05-20 14:49:24667 set_lossless(avctx);
Andrey Turkinf84dfbc2016-05-25 16:39:54668 } else if (ctx->rc > 0) {
669 nvenc_override_rate_control(avctx);
Andrey Turkin82d705e2016-05-20 14:49:24670 } else {
Andrey Turkinf84dfbc2016-05-25 16:39:54671 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
672 set_vbr(avctx);
Andrey Turkin82d705e2016-05-20 14:49:24673 }
674
675 if (avctx->rc_buffer_size > 0) {
676 ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size;
677 } else if (ctx->encode_config.rcParams.averageBitRate > 0) {
678 ctx->encode_config.rcParams.vbvBufferSize = 2 * ctx->encode_config.rcParams.averageBitRate;
679 }
680}
681
Andrey Turkinfaffff82016-05-25 14:05:50682static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx)
Andrey Turkin82d705e2016-05-20 14:49:24683{
Andrey Turkinfaffff82016-05-25 14:05:50684 NvencContext *ctx = avctx->priv_data;
685 NV_ENC_CONFIG *cc = &ctx->encode_config;
686 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
687 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
Andrey Turkin82d705e2016-05-20 14:49:24688
Andrey Turkinfaffff82016-05-25 14:05:50689 vui->colourMatrix = avctx->colorspace;
690 vui->colourPrimaries = avctx->color_primaries;
691 vui->transferCharacteristics = avctx->color_trc;
692 vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG
Andrey Turkina8cf25d2016-05-20 22:08:06693 || 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 Turkin82d705e2016-05-20 14:49:24694
Andrey Turkinfaffff82016-05-25 14:05:50695 vui->colourDescriptionPresentFlag =
Andrey Turkin82d705e2016-05-20 14:49:24696 (avctx->colorspace != 2 || avctx->color_primaries != 2 || avctx->color_trc != 2);
697
Andrey Turkinfaffff82016-05-25 14:05:50698 vui->videoSignalTypePresentFlag =
699 (vui->colourDescriptionPresentFlag
700 || vui->videoFormat != 5
701 || vui->videoFullRangeFlag != 0);
Andrey Turkin82d705e2016-05-20 14:49:24702
Andrey Turkinfaffff82016-05-25 14:05:50703 h264->sliceMode = 3;
704 h264->sliceModeData = 1;
Andrey Turkin82d705e2016-05-20 14:49:24705
Andrey Turkinfaffff82016-05-25 14:05:50706 h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
Andrey Turkinf84dfbc2016-05-25 16:39:54707 h264->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
708 h264->outputAUD = 1;
Andrey Turkin82d705e2016-05-20 14:49:24709
Andrey Turkinf84dfbc2016-05-25 16:39:54710 if (avctx->refs >= 0) {
711 /* 0 means "let the hardware decide" */
712 h264->maxNumRefFrames = avctx->refs;
713 }
714 if (avctx->gop_size >= 0) {
715 h264->idrPeriod = cc->gopLength;
716 }
717
718 if (IS_CBR(cc->rcParams.rateControlMode)) {
719 h264->outputBufferingPeriodSEI = 1;
720 h264->outputPictureTimingSEI = 1;
721 }
722
723 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_2_PASS_QUALITY ||
724 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP ||
725 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_2_PASS_VBR) {
726 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
727 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
728 }
Andrey Turkin82d705e2016-05-20 14:49:24729
Andrey Turkin98243212016-05-25 14:26:25730 if (ctx->flags & NVENC_LOSSLESS) {
731 h264->qpPrimeYZeroTransformBypassFlag = 1;
732 } else {
733 switch(ctx->profile) {
734 case NV_ENC_H264_PROFILE_BASELINE:
Andrey Turkinfaffff82016-05-25 14:05:50735 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
Andrey Turkin82d705e2016-05-20 14:49:24736 avctx->profile = FF_PROFILE_H264_BASELINE;
Andrey Turkin98243212016-05-25 14:26:25737 break;
738 case NV_ENC_H264_PROFILE_MAIN:
739 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
740 avctx->profile = FF_PROFILE_H264_MAIN;
741 break;
742 case NV_ENC_H264_PROFILE_HIGH:
743 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
744 avctx->profile = FF_PROFILE_H264_HIGH;
745 break;
746 case NV_ENC_H264_PROFILE_HIGH_444P:
Andrey Turkinfaffff82016-05-25 14:05:50747 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
Andrey Turkin82d705e2016-05-20 14:49:24748 avctx->profile = FF_PROFILE_H264_HIGH_444_PREDICTIVE;
Andrey Turkin98243212016-05-25 14:26:25749 break;
Andrey Turkin82d705e2016-05-20 14:49:24750 }
751 }
752
753 // force setting profile as high444p if input is AV_PIX_FMT_YUV444P
Andrey Turkina8cf25d2016-05-20 22:08:06754 if (ctx->data_pix_fmt == AV_PIX_FMT_YUV444P) {
Andrey Turkinfaffff82016-05-25 14:05:50755 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
Andrey Turkin82d705e2016-05-20 14:49:24756 avctx->profile = FF_PROFILE_H264_HIGH_444_PREDICTIVE;
757 }
758
Andrey Turkinfaffff82016-05-25 14:05:50759 h264->chromaFormatIDC = avctx->profile == FF_PROFILE_H264_HIGH_444_PREDICTIVE ? 3 : 1;
Andrey Turkin82d705e2016-05-20 14:49:24760
Andrey Turkinb0172872016-05-25 15:00:52761 h264->level = ctx->level;
Andrey Turkin82d705e2016-05-20 14:49:24762
763 return 0;
764}
765
766static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
767{
Andrey Turkinfaffff82016-05-25 14:05:50768 NvencContext *ctx = avctx->priv_data;
769 NV_ENC_CONFIG *cc = &ctx->encode_config;
770 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
771 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
Andrey Turkin82d705e2016-05-20 14:49:24772
Andrey Turkinfaffff82016-05-25 14:05:50773 vui->colourMatrix = avctx->colorspace;
774 vui->colourPrimaries = avctx->color_primaries;
775 vui->transferCharacteristics = avctx->color_trc;
776 vui->videoFullRangeFlag = (avctx->color_range == AVCOL_RANGE_JPEG
Andrey Turkina8cf25d2016-05-20 22:08:06777 || 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 Turkin82d705e2016-05-20 14:49:24778
Andrey Turkinfaffff82016-05-25 14:05:50779 vui->colourDescriptionPresentFlag =
Andrey Turkin82d705e2016-05-20 14:49:24780 (avctx->colorspace != 2 || avctx->color_primaries != 2 || avctx->color_trc != 2);
781
Andrey Turkinfaffff82016-05-25 14:05:50782 vui->videoSignalTypePresentFlag =
783 (vui->colourDescriptionPresentFlag
784 || vui->videoFormat != 5
785 || vui->videoFullRangeFlag != 0);
Andrey Turkin82d705e2016-05-20 14:49:24786
Andrey Turkinfaffff82016-05-25 14:05:50787 hevc->sliceMode = 3;
788 hevc->sliceModeData = 1;
Andrey Turkin82d705e2016-05-20 14:49:24789
Andrey Turkinfaffff82016-05-25 14:05:50790 hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
Andrey Turkinf84dfbc2016-05-25 16:39:54791 hevc->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
792 hevc->outputAUD = 1;
Andrey Turkin82d705e2016-05-20 14:49:24793
Andrey Turkinf84dfbc2016-05-25 16:39:54794 if (avctx->refs >= 0) {
795 /* 0 means "let the hardware decide" */
796 hevc->maxNumRefFramesInDPB = avctx->refs;
797 }
798 if (avctx->gop_size >= 0) {
799 hevc->idrPeriod = cc->gopLength;
800 }
801
802 if (IS_CBR(cc->rcParams.rateControlMode)) {
803 hevc->outputBufferingPeriodSEI = 1;
804 hevc->outputPictureTimingSEI = 1;
805 }
Andrey Turkin82d705e2016-05-20 14:49:24806
807 /* No other profile is supported in the current SDK version 5 */
Andrey Turkinfaffff82016-05-25 14:05:50808 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
Andrey Turkin82d705e2016-05-20 14:49:24809 avctx->profile = FF_PROFILE_HEVC_MAIN;
810
Andrey Turkinb0172872016-05-25 15:00:52811 hevc->level = ctx->level;
Andrey Turkin82d705e2016-05-20 14:49:24812
Andrey Turkin40df4682016-05-25 15:06:02813 hevc->tier = ctx->tier;
Andrey Turkin82d705e2016-05-20 14:49:24814
815 return 0;
816}
817
Andrey Turkinfaffff82016-05-25 14:05:50818static av_cold int nvenc_setup_codec_config(AVCodecContext *avctx)
Andrey Turkin82d705e2016-05-20 14:49:24819{
820 switch (avctx->codec->id) {
821 case AV_CODEC_ID_H264:
Andrey Turkinfaffff82016-05-25 14:05:50822 return nvenc_setup_h264_config(avctx);
823 case AV_CODEC_ID_HEVC:
Andrey Turkin82d705e2016-05-20 14:49:24824 return nvenc_setup_hevc_config(avctx);
825 /* Earlier switch/case will return if unknown codec is passed. */
826 }
827
828 return 0;
829}
830
831static av_cold int nvenc_setup_encoder(AVCodecContext *avctx)
832{
833 NvencContext *ctx = avctx->priv_data;
834 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
835 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
836
837 NV_ENC_PRESET_CONFIG preset_config = { 0 };
Andrey Turkin82d705e2016-05-20 14:49:24838 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
839 AVCPBProperties *cpb_props;
Andrey Turkin82d705e2016-05-20 14:49:24840 int res = 0;
841 int dw, dh;
842
843 ctx->last_dts = AV_NOPTS_VALUE;
844
845 ctx->encode_config.version = NV_ENC_CONFIG_VER;
846 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
Andrey Turkinfaffff82016-05-25 14:05:50847
848 ctx->init_encode_params.encodeHeight = avctx->height;
849 ctx->init_encode_params.encodeWidth = avctx->width;
850
851 ctx->init_encode_params.encodeConfig = &ctx->encode_config;
852
853 nvenc_map_preset(ctx);
854
Andrey Turkin82d705e2016-05-20 14:49:24855 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
856 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
857
Andrey Turkinfaffff82016-05-25 14:05:50858 nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder,
859 ctx->init_encode_params.encodeGUID,
860 ctx->init_encode_params.presetGUID,
861 &preset_config);
862 if (nv_status != NV_ENC_SUCCESS)
863 return nvenc_print_error(avctx, nv_status, "Cannot get the preset configuration");
Timo Rothenpieler2a428db2014-11-29 23:04:37864
Andrey Turkinfaffff82016-05-25 14:05:50865 memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
Agatha Hu49046582015-09-11 09:07:10866
Andrey Turkinfaffff82016-05-25 14:05:50867 ctx->encode_config.version = NV_ENC_CONFIG_VER;
Timo Rothenpielerfb34c582015-01-26 12:28:22868
869 if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den &&
870 (avctx->sample_aspect_ratio.num != 1 || avctx->sample_aspect_ratio.num != 1)) {
871 av_reduce(&dw, &dh,
872 avctx->width * avctx->sample_aspect_ratio.num,
873 avctx->height * avctx->sample_aspect_ratio.den,
874 1024 * 1024);
875 ctx->init_encode_params.darHeight = dh;
876 ctx->init_encode_params.darWidth = dw;
877 } else {
878 ctx->init_encode_params.darHeight = avctx->height;
879 ctx->init_encode_params.darWidth = avctx->width;
880 }
881
Philip Langdaled20df262015-01-28 17:05:53882 // De-compensate for hardware, dubiously, trying to compensate for
883 // playback at 704 pixel width.
884 if (avctx->width == 720 &&
885 (avctx->height == 480 || avctx->height == 576)) {
886 av_reduce(&dw, &dh,
887 ctx->init_encode_params.darWidth * 44,
888 ctx->init_encode_params.darHeight * 45,
Philip Langdale7ae805d2015-05-27 01:35:15889 1024 * 1024);
Philip Langdaled20df262015-01-28 17:05:53890 ctx->init_encode_params.darHeight = dh;
891 ctx->init_encode_params.darWidth = dw;
892 }
893
Timo Rothenpieler2a428db2014-11-29 23:04:37894 ctx->init_encode_params.frameRateNum = avctx->time_base.den;
895 ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
896
Timo Rothenpieler2a428db2014-11-29 23:04:37897 ctx->init_encode_params.enableEncodeAsync = 0;
898 ctx->init_encode_params.enablePTD = 1;
899
Timo Rothenpieler914fd422015-01-26 12:28:21900 if (avctx->gop_size > 0) {
901 if (avctx->max_b_frames >= 0) {
902 /* 0 is intra-only, 1 is I/P only, 2 is one B Frame, 3 two B frames, and so on. */
903 ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
904 }
905
Timo Rothenpieler2a428db2014-11-29 23:04:37906 ctx->encode_config.gopLength = avctx->gop_size;
Timo Rothenpieler914fd422015-01-26 12:28:21907 } else if (avctx->gop_size == 0) {
908 ctx->encode_config.frameIntervalP = 0;
909 ctx->encode_config.gopLength = 1;
Timo Rothenpieler2a428db2014-11-29 23:04:37910 }
911
Timo Rothenpieler914fd422015-01-26 12:28:21912 /* when there're b frames, set dts offset */
913 if (ctx->encode_config.frameIntervalP >= 2)
914 ctx->last_dts = -2;
915
Andrey Turkinfaffff82016-05-25 14:05:50916 nvenc_setup_rate_control(avctx);
Timo Rothenpieler2a428db2014-11-29 23:04:37917
Vittorio Giovara7c6eb0a2015-06-29 19:59:37918 if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
Timo Rothenpieler2a428db2014-11-29 23:04:37919 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
920 } else {
921 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
922 }
923
Andrey Turkinfaffff82016-05-25 14:05:50924 res = nvenc_setup_codec_config(avctx);
Andrey Turkin82d705e2016-05-20 14:49:24925 if (res)
926 return res;
Timo Rothenpieler2a428db2014-11-29 23:04:37927
928 nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
929 if (nv_status != NV_ENC_SUCCESS) {
Andrey Turkine1691c42016-05-20 15:37:00930 return nvenc_print_error(avctx, nv_status, "InitializeEncoder failed");
Timo Rothenpieler2a428db2014-11-29 23:04:37931 }
932
933 if (ctx->encode_config.frameIntervalP > 1)
934 avctx->has_b_frames = 2;
935
936 if (ctx->encode_config.rcParams.averageBitRate > 0)
937 avctx->bit_rate = ctx->encode_config.rcParams.averageBitRate;
938
Anton Khirnov1520c6f2015-10-03 13:19:10939 cpb_props = ff_add_cpb_side_data(avctx);
940 if (!cpb_props)
941 return AVERROR(ENOMEM);
Hendrik Leppkes5fc17ed2015-12-17 12:41:29942 cpb_props->max_bitrate = ctx->encode_config.rcParams.maxBitRate;
Anton Khirnov1520c6f2015-10-03 13:19:10943 cpb_props->avg_bitrate = avctx->bit_rate;
Hendrik Leppkes5fc17ed2015-12-17 12:41:29944 cpb_props->buffer_size = ctx->encode_config.rcParams.vbvBufferSize;
Anton Khirnov1520c6f2015-10-03 13:19:10945
Timo Rothenpieler2a428db2014-11-29 23:04:37946 return 0;
Andrey Turkin82d705e2016-05-20 14:49:24947}
948
949static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
950{
951 NvencContext *ctx = avctx->priv_data;
952 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
953 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
954
955 NVENCSTATUS nv_status;
Andrey Turkin82d705e2016-05-20 14:49:24956 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
Andrey Turkin82d705e2016-05-20 14:49:24957 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
958
Andrey Turkina8cf25d2016-05-20 22:08:06959 switch (ctx->data_pix_fmt) {
Andrey Turkin82d705e2016-05-20 14:49:24960 case AV_PIX_FMT_YUV420P:
Andrey Turkina8cf25d2016-05-20 22:08:06961 ctx->surfaces[idx].format = NV_ENC_BUFFER_FORMAT_YV12_PL;
Andrey Turkin82d705e2016-05-20 14:49:24962 break;
963
964 case AV_PIX_FMT_NV12:
Andrey Turkina8cf25d2016-05-20 22:08:06965 ctx->surfaces[idx].format = NV_ENC_BUFFER_FORMAT_NV12_PL;
Andrey Turkin82d705e2016-05-20 14:49:24966 break;
967
968 case AV_PIX_FMT_YUV444P:
Andrey Turkina8cf25d2016-05-20 22:08:06969 ctx->surfaces[idx].format = NV_ENC_BUFFER_FORMAT_YUV444_PL;
Andrey Turkin82d705e2016-05-20 14:49:24970 break;
971
972 default:
973 av_log(avctx, AV_LOG_FATAL, "Invalid input pixel format\n");
974 return AVERROR(EINVAL);
975 }
976
Andrey Turkina8cf25d2016-05-20 22:08:06977 if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
978 ctx->surfaces[idx].in_ref = av_frame_alloc();
979 if (!ctx->surfaces[idx].in_ref)
980 return AVERROR(ENOMEM);
981 } else {
982 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
983 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
984 allocSurf.width = (avctx->width + 31) & ~31;
985 allocSurf.height = (avctx->height + 31) & ~31;
986 allocSurf.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
987 allocSurf.bufferFmt = ctx->surfaces[idx].format;
988
989 nv_status = p_nvenc->nvEncCreateInputBuffer(ctx->nvencoder, &allocSurf);
990 if (nv_status != NV_ENC_SUCCESS) {
991 return nvenc_print_error(avctx, nv_status, "CreateInputBuffer failed");
992 }
993
994 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
995 ctx->surfaces[idx].width = allocSurf.width;
996 ctx->surfaces[idx].height = allocSurf.height;
Andrey Turkin82d705e2016-05-20 14:49:24997 }
998
Andrey Turkine1691c42016-05-20 15:37:00999 ctx->surfaces[idx].lockCount = 0;
Andrey Turkin82d705e2016-05-20 14:49:241000
1001 /* 1MB is large enough to hold most output frames. NVENC increases this automaticaly if it's not enough. */
1002 allocOut.size = 1024 * 1024;
1003
1004 allocOut.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED;
1005
1006 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(ctx->nvencoder, &allocOut);
1007 if (nv_status != NV_ENC_SUCCESS) {
Andrey Turkine1691c42016-05-20 15:37:001008 int err = nvenc_print_error(avctx, nv_status, "CreateBitstreamBuffer failed");
Andrey Turkina8cf25d2016-05-20 22:08:061009 if (avctx->pix_fmt != AV_PIX_FMT_CUDA)
1010 p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[idx].input_surface);
1011 av_frame_free(&ctx->surfaces[idx].in_ref);
Andrey Turkine1691c42016-05-20 15:37:001012 return err;
Andrey Turkin82d705e2016-05-20 14:49:241013 }
1014
Andrey Turkine1691c42016-05-20 15:37:001015 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1016 ctx->surfaces[idx].size = allocOut.size;
Andrey Turkin82d705e2016-05-20 14:49:241017
1018 return 0;
1019}
1020
Andrey Turkinb6933532016-05-25 16:57:111021static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx)
Andrey Turkin82d705e2016-05-20 14:49:241022{
Andrey Turkin82d705e2016-05-20 14:49:241023 NvencContext *ctx = avctx->priv_data;
Andrey Turkinb6933532016-05-25 16:57:111024 int i, res;
Andrey Turkinf052ef32016-05-29 10:07:341025 int num_mbs = ((avctx->width + 15) >> 4) * ((avctx->height + 15) >> 4);
1026 ctx->nb_surfaces = FFMAX((num_mbs >= 8160) ? 32 : 48,
1027 ctx->nb_surfaces);
1028 ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1);
Andrey Turkin82d705e2016-05-20 14:49:241029
Andrey Turkin82d705e2016-05-20 14:49:241030
Andrey Turkinf052ef32016-05-29 10:07:341031 ctx->surfaces = av_mallocz_array(ctx->nb_surfaces, sizeof(*ctx->surfaces));
1032 if (!ctx->surfaces)
Andrey Turkin82d705e2016-05-20 14:49:241033 return AVERROR(ENOMEM);
Andrey Turkin82d705e2016-05-20 14:49:241034
Andrey Turkinf052ef32016-05-29 10:07:341035 ctx->timestamp_list = av_fifo_alloc(ctx->nb_surfaces * sizeof(int64_t));
Andrey Turkincfb49fc2016-05-20 16:13:201036 if (!ctx->timestamp_list)
1037 return AVERROR(ENOMEM);
Andrey Turkinf052ef32016-05-29 10:07:341038 ctx->output_surface_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*));
Andrey Turkincfb49fc2016-05-20 16:13:201039 if (!ctx->output_surface_queue)
1040 return AVERROR(ENOMEM);
Andrey Turkinf052ef32016-05-29 10:07:341041 ctx->output_surface_ready_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*));
Andrey Turkincfb49fc2016-05-20 16:13:201042 if (!ctx->output_surface_ready_queue)
1043 return AVERROR(ENOMEM);
1044
Andrey Turkinf052ef32016-05-29 10:07:341045 for (i = 0; i < ctx->nb_surfaces; i++) {
Andrey Turkinb6933532016-05-25 16:57:111046 if ((res = nvenc_alloc_surface(avctx, i)) < 0)
Andrey Turkin82d705e2016-05-20 14:49:241047 return res;
1048 }
1049
1050 return 0;
1051}
1052
1053static av_cold int nvenc_setup_extradata(AVCodecContext *avctx)
1054{
1055 NvencContext *ctx = avctx->priv_data;
1056 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1057 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1058
1059 NVENCSTATUS nv_status;
1060 uint32_t outSize = 0;
1061 char tmpHeader[256];
1062 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1063 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1064
1065 payload.spsppsBuffer = tmpHeader;
1066 payload.inBufferSize = sizeof(tmpHeader);
1067 payload.outSPSPPSPayloadSize = &outSize;
1068
1069 nv_status = p_nvenc->nvEncGetSequenceParams(ctx->nvencoder, &payload);
1070 if (nv_status != NV_ENC_SUCCESS) {
Andrey Turkine1691c42016-05-20 15:37:001071 return nvenc_print_error(avctx, nv_status, "GetSequenceParams failed");
Andrey Turkin82d705e2016-05-20 14:49:241072 }
1073
1074 avctx->extradata_size = outSize;
1075 avctx->extradata = av_mallocz(outSize + AV_INPUT_BUFFER_PADDING_SIZE);
1076
1077 if (!avctx->extradata) {
1078 return AVERROR(ENOMEM);
1079 }
1080
1081 memcpy(avctx->extradata, tmpHeader, outSize);
1082
1083 return 0;
1084}
1085
Andrey Turkin7aa16d52016-05-25 13:04:281086av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
Timo Rothenpieler2a428db2014-11-29 23:04:371087{
Andrey Turkinb6933532016-05-25 16:57:111088 NvencContext *ctx = avctx->priv_data;
Timo Rothenpieler2a428db2014-11-29 23:04:371089 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1090 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1091 int i;
1092
Andrey Turkinb6933532016-05-25 16:57:111093 /* the encoder has to be flushed before it can be closed */
1094 if (ctx->nvencoder) {
1095 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1096 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1097
1098 p_nvenc->nvEncEncodePicture(ctx->nvencoder, &params);
1099 }
1100
Andrey Turkincfb49fc2016-05-20 16:13:201101 av_fifo_freep(&ctx->timestamp_list);
1102 av_fifo_freep(&ctx->output_surface_ready_queue);
1103 av_fifo_freep(&ctx->output_surface_queue);
Timo Rothenpieler2a428db2014-11-29 23:04:371104
Andrey Turkinb6933532016-05-25 16:57:111105 if (ctx->surfaces && avctx->pix_fmt == AV_PIX_FMT_CUDA) {
Andrey Turkinf052ef32016-05-29 10:07:341106 for (i = 0; i < ctx->nb_surfaces; ++i) {
Andrey Turkina8cf25d2016-05-20 22:08:061107 if (ctx->surfaces[i].input_surface) {
1108 p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, ctx->surfaces[i].in_map.mappedResource);
1109 }
1110 }
1111 for (i = 0; i < ctx->nb_registered_frames; i++) {
1112 if (ctx->registered_frames[i].regptr)
1113 p_nvenc->nvEncUnregisterResource(ctx->nvencoder, ctx->registered_frames[i].regptr);
1114 }
1115 ctx->nb_registered_frames = 0;
1116 }
1117
Andrey Turkinb6933532016-05-25 16:57:111118 if (ctx->surfaces) {
Andrey Turkinf052ef32016-05-29 10:07:341119 for (i = 0; i < ctx->nb_surfaces; ++i) {
Andrey Turkinb6933532016-05-25 16:57:111120 if (avctx->pix_fmt != AV_PIX_FMT_CUDA)
1121 p_nvenc->nvEncDestroyInputBuffer(ctx->nvencoder, ctx->surfaces[i].input_surface);
1122 av_frame_free(&ctx->surfaces[i].in_ref);
1123 p_nvenc->nvEncDestroyBitstreamBuffer(ctx->nvencoder, ctx->surfaces[i].output_surface);
1124 }
Timo Rothenpieler2a428db2014-11-29 23:04:371125 }
Andrey Turkincfb49fc2016-05-20 16:13:201126 av_freep(&ctx->surfaces);
Andrey Turkinf052ef32016-05-29 10:07:341127 ctx->nb_surfaces = 0;
Timo Rothenpieler2a428db2014-11-29 23:04:371128
Andrey Turkinb6933532016-05-25 16:57:111129 if (ctx->nvencoder)
1130 p_nvenc->nvEncDestroyEncoder(ctx->nvencoder);
Timo Rothenpieler2a428db2014-11-29 23:04:371131 ctx->nvencoder = NULL;
1132
Andrey Turkina8cf25d2016-05-20 22:08:061133 if (ctx->cu_context_internal)
1134 dl_fn->cu_ctx_destroy(ctx->cu_context_internal);
1135 ctx->cu_context = ctx->cu_context_internal = NULL;
Timo Rothenpieler2a428db2014-11-29 23:04:371136
Andrey Turkin0d021cc2016-05-29 12:23:261137 if (dl_fn->nvenc)
1138 dlclose(dl_fn->nvenc);
1139 dl_fn->nvenc = NULL;
Andrey Turkinb6933532016-05-25 16:57:111140
1141 dl_fn->nvenc_device_count = 0;
1142
1143#if !CONFIG_CUDA
Andrey Turkin0d021cc2016-05-29 12:23:261144 if (dl_fn->cuda)
1145 dlclose(dl_fn->cuda);
1146 dl_fn->cuda = NULL;
Andrey Turkinb6933532016-05-25 16:57:111147#endif
1148
1149 dl_fn->cu_init = NULL;
1150 dl_fn->cu_device_get_count = NULL;
1151 dl_fn->cu_device_get = NULL;
1152 dl_fn->cu_device_get_name = NULL;
1153 dl_fn->cu_device_compute_capability = NULL;
1154 dl_fn->cu_ctx_create = NULL;
1155 dl_fn->cu_ctx_pop_current = NULL;
1156 dl_fn->cu_ctx_destroy = NULL;
1157
1158 av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
1159
1160 return 0;
1161}
1162
1163av_cold int ff_nvenc_encode_init(AVCodecContext *avctx)
1164{
Andrey Turkin0d021cc2016-05-29 12:23:261165 NvencContext *ctx = avctx->priv_data;
1166 int ret;
Andrey Turkinb6933532016-05-25 16:57:111167
Andrey Turkin0d021cc2016-05-29 12:23:261168 if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
1169 AVHWFramesContext *frames_ctx;
1170 if (!avctx->hw_frames_ctx) {
1171 av_log(avctx, AV_LOG_ERROR,
1172 "hw_frames_ctx must be set when using GPU frames as input\n");
1173 return AVERROR(EINVAL);
1174 }
1175 frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1176 ctx->data_pix_fmt = frames_ctx->sw_format;
1177 } else {
1178 ctx->data_pix_fmt = avctx->pix_fmt;
1179 }
Andrey Turkinb6933532016-05-25 16:57:111180
Andrey Turkin0d021cc2016-05-29 12:23:261181 if ((ret = nvenc_load_libraries(avctx)) < 0)
1182 return ret;
Andrey Turkinb6933532016-05-25 16:57:111183
Andrey Turkin0d021cc2016-05-29 12:23:261184 if ((ret = nvenc_setup_device(avctx)) < 0)
1185 return ret;
Andrey Turkinb6933532016-05-25 16:57:111186
Andrey Turkin0d021cc2016-05-29 12:23:261187 if ((ret = nvenc_setup_encoder(avctx)) < 0)
1188 return ret;
Andrey Turkinb6933532016-05-25 16:57:111189
Andrey Turkin0d021cc2016-05-29 12:23:261190 if ((ret = nvenc_setup_surfaces(avctx)) < 0)
1191 return ret;
Andrey Turkinb6933532016-05-25 16:57:111192
1193 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
Andrey Turkin0d021cc2016-05-29 12:23:261194 if ((ret = nvenc_setup_extradata(avctx)) < 0)
1195 return ret;
Andrey Turkinb6933532016-05-25 16:57:111196 }
Timo Rothenpieler2a428db2014-11-29 23:04:371197
Timo Rothenpieler2a428db2014-11-29 23:04:371198 return 0;
1199}
1200
Andrey Turkine1691c42016-05-20 15:37:001201static NvencSurface *get_free_frame(NvencContext *ctx)
Andrey Turkin82d705e2016-05-20 14:49:241202{
1203 int i;
1204
Andrey Turkinf052ef32016-05-29 10:07:341205 for (i = 0; i < ctx->nb_surfaces; ++i) {
Andrey Turkine1691c42016-05-20 15:37:001206 if (!ctx->surfaces[i].lockCount) {
1207 ctx->surfaces[i].lockCount = 1;
1208 return &ctx->surfaces[i];
Andrey Turkin82d705e2016-05-20 14:49:241209 }
1210 }
1211
1212 return NULL;
1213}
1214
Andrey Turkine1691c42016-05-20 15:37:001215static int nvenc_copy_frame(AVCodecContext *avctx, NvencSurface *inSurf,
Andrey Turkin82d705e2016-05-20 14:49:241216 NV_ENC_LOCK_INPUT_BUFFER *lockBufferParams, const AVFrame *frame)
1217{
1218 uint8_t *buf = lockBufferParams->bufferDataPtr;
1219 int off = inSurf->height * lockBufferParams->pitch;
1220
Andrey Turkina8cf25d2016-05-20 22:08:061221 if (frame->format == AV_PIX_FMT_YUV420P) {
Andrey Turkin82d705e2016-05-20 14:49:241222 av_image_copy_plane(buf, lockBufferParams->pitch,
1223 frame->data[0], frame->linesize[0],
1224 avctx->width, avctx->height);
1225
1226 buf += off;
1227
1228 av_image_copy_plane(buf, lockBufferParams->pitch >> 1,
1229 frame->data[2], frame->linesize[2],
1230 avctx->width >> 1, avctx->height >> 1);
1231
1232 buf += off >> 2;
1233
1234 av_image_copy_plane(buf, lockBufferParams->pitch >> 1,
1235 frame->data[1], frame->linesize[1],
1236 avctx->width >> 1, avctx->height >> 1);
Andrey Turkina8cf25d2016-05-20 22:08:061237 } else if (frame->format == AV_PIX_FMT_NV12) {
Andrey Turkin82d705e2016-05-20 14:49:241238 av_image_copy_plane(buf, lockBufferParams->pitch,
1239 frame->data[0], frame->linesize[0],
1240 avctx->width, avctx->height);
1241
1242 buf += off;
1243
1244 av_image_copy_plane(buf, lockBufferParams->pitch,
1245 frame->data[1], frame->linesize[1],
1246 avctx->width, avctx->height >> 1);
Andrey Turkina8cf25d2016-05-20 22:08:061247 } else if (frame->format == AV_PIX_FMT_YUV444P) {
Andrey Turkin82d705e2016-05-20 14:49:241248 av_image_copy_plane(buf, lockBufferParams->pitch,
1249 frame->data[0], frame->linesize[0],
1250 avctx->width, avctx->height);
1251
1252 buf += off;
1253
1254 av_image_copy_plane(buf, lockBufferParams->pitch,
1255 frame->data[1], frame->linesize[1],
1256 avctx->width, avctx->height);
1257
1258 buf += off;
1259
1260 av_image_copy_plane(buf, lockBufferParams->pitch,
1261 frame->data[2], frame->linesize[2],
1262 avctx->width, avctx->height);
1263 } else {
1264 av_log(avctx, AV_LOG_FATAL, "Invalid pixel format!\n");
1265 return AVERROR(EINVAL);
1266 }
1267
1268 return 0;
1269}
1270
Andrey Turkina8cf25d2016-05-20 22:08:061271static int nvenc_find_free_reg_resource(AVCodecContext *avctx)
1272{
1273 NvencContext *ctx = avctx->priv_data;
1274 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1275 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1276
1277 int i;
1278
1279 if (ctx->nb_registered_frames == FF_ARRAY_ELEMS(ctx->registered_frames)) {
1280 for (i = 0; i < ctx->nb_registered_frames; i++) {
1281 if (!ctx->registered_frames[i].mapped) {
1282 if (ctx->registered_frames[i].regptr) {
1283 p_nvenc->nvEncUnregisterResource(ctx->nvencoder,
1284 ctx->registered_frames[i].regptr);
1285 ctx->registered_frames[i].regptr = NULL;
1286 }
1287 return i;
1288 }
1289 }
1290 } else {
1291 return ctx->nb_registered_frames++;
1292 }
1293
1294 av_log(avctx, AV_LOG_ERROR, "Too many registered CUDA frames\n");
1295 return AVERROR(ENOMEM);
1296}
1297
1298static int nvenc_register_frame(AVCodecContext *avctx, const AVFrame *frame)
1299{
1300 NvencContext *ctx = avctx->priv_data;
1301 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1302 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1303
1304 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
1305 NV_ENC_REGISTER_RESOURCE reg;
1306 int i, idx, ret;
1307
1308 for (i = 0; i < ctx->nb_registered_frames; i++) {
1309 if (ctx->registered_frames[i].ptr == (CUdeviceptr)frame->data[0])
1310 return i;
1311 }
1312
1313 idx = nvenc_find_free_reg_resource(avctx);
1314 if (idx < 0)
1315 return idx;
1316
1317 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
1318 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
1319 reg.width = frames_ctx->width;
1320 reg.height = frames_ctx->height;
1321 reg.bufferFormat = ctx->surfaces[0].format;
1322 reg.pitch = frame->linesize[0];
1323 reg.resourceToRegister = frame->data[0];
1324
1325 ret = p_nvenc->nvEncRegisterResource(ctx->nvencoder, &reg);
1326 if (ret != NV_ENC_SUCCESS) {
1327 nvenc_print_error(avctx, ret, "Error registering an input resource");
1328 return AVERROR_UNKNOWN;
1329 }
1330
1331 ctx->registered_frames[idx].ptr = (CUdeviceptr)frame->data[0];
1332 ctx->registered_frames[idx].regptr = reg.registeredResource;
1333 return idx;
1334}
1335
Andrey Turkin82d705e2016-05-20 14:49:241336static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame,
Andrey Turkine1691c42016-05-20 15:37:001337 NvencSurface *nvenc_frame)
Andrey Turkin82d705e2016-05-20 14:49:241338{
1339 NvencContext *ctx = avctx->priv_data;
1340 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1341 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1342
1343 int res;
1344 NVENCSTATUS nv_status;
Andrey Turkin82d705e2016-05-20 14:49:241345
Andrey Turkina8cf25d2016-05-20 22:08:061346 if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
1347 int reg_idx = nvenc_register_frame(avctx, frame);
1348 if (reg_idx < 0) {
1349 av_log(avctx, AV_LOG_ERROR, "Could not register an input CUDA frame\n");
1350 return reg_idx;
1351 }
Andrey Turkin82d705e2016-05-20 14:49:241352
Andrey Turkina8cf25d2016-05-20 22:08:061353 res = av_frame_ref(nvenc_frame->in_ref, frame);
1354 if (res < 0)
1355 return res;
1356
1357 nvenc_frame->in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
1358 nvenc_frame->in_map.registeredResource = ctx->registered_frames[reg_idx].regptr;
1359 nv_status = p_nvenc->nvEncMapInputResource(ctx->nvencoder, &nvenc_frame->in_map);
1360 if (nv_status != NV_ENC_SUCCESS) {
1361 av_frame_unref(nvenc_frame->in_ref);
1362 return nvenc_print_error(avctx, nv_status, "Error mapping an input resource");
1363 }
1364
1365 ctx->registered_frames[reg_idx].mapped = 1;
1366 nvenc_frame->reg_idx = reg_idx;
1367 nvenc_frame->input_surface = nvenc_frame->in_map.mappedResource;
1368 return 0;
1369 } else {
1370 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
1371
1372 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1373 lockBufferParams.inputBuffer = nvenc_frame->input_surface;
1374
1375 nv_status = p_nvenc->nvEncLockInputBuffer(ctx->nvencoder, &lockBufferParams);
1376 if (nv_status != NV_ENC_SUCCESS) {
1377 return nvenc_print_error(avctx, nv_status, "Failed locking nvenc input buffer");
1378 }
1379
1380 res = nvenc_copy_frame(avctx, nvenc_frame, &lockBufferParams, frame);
1381
1382 nv_status = p_nvenc->nvEncUnlockInputBuffer(ctx->nvencoder, nvenc_frame->input_surface);
1383 if (nv_status != NV_ENC_SUCCESS) {
1384 return nvenc_print_error(avctx, nv_status, "Failed unlocking input buffer!");
1385 }
1386
1387 return res;
Andrey Turkin82d705e2016-05-20 14:49:241388 }
Andrey Turkin82d705e2016-05-20 14:49:241389}
1390
1391static void nvenc_codec_specific_pic_params(AVCodecContext *avctx,
1392 NV_ENC_PIC_PARAMS *params)
1393{
1394 NvencContext *ctx = avctx->priv_data;
1395
1396 switch (avctx->codec->id) {
1397 case AV_CODEC_ID_H264:
1398 params->codecPicParams.h264PicParams.sliceMode = ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
1399 params->codecPicParams.h264PicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1400 break;
1401 case AV_CODEC_ID_H265:
1402 params->codecPicParams.hevcPicParams.sliceMode = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
1403 params->codecPicParams.hevcPicParams.sliceModeData = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1404 break;
1405 }
1406}
1407
Andrey Turkine1691c42016-05-20 15:37:001408static int process_output_surface(AVCodecContext *avctx, AVPacket *pkt, NvencSurface *tmpoutsurf)
Timo Rothenpieler2a428db2014-11-29 23:04:371409{
1410 NvencContext *ctx = avctx->priv_data;
1411 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1412 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1413
Philip Langdale21175d82015-03-24 04:34:591414 uint32_t slice_mode_data;
1415 uint32_t *slice_offsets;
Timo Rothenpieler2a428db2014-11-29 23:04:371416 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
1417 NVENCSTATUS nv_status;
1418 int res = 0;
1419
Lucas Cooperfd554702016-03-07 23:47:561420 enum AVPictureType pict_type;
1421
Philip Langdale21175d82015-03-24 04:34:591422 switch (avctx->codec->id) {
1423 case AV_CODEC_ID_H264:
1424 slice_mode_data = ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1425 break;
1426 case AV_CODEC_ID_H265:
1427 slice_mode_data = ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1428 break;
1429 default:
Agatha Hu49046582015-09-11 09:07:101430 av_log(avctx, AV_LOG_ERROR, "Unknown codec name\n");
Philip Langdale21175d82015-03-24 04:34:591431 res = AVERROR(EINVAL);
1432 goto error;
1433 }
1434 slice_offsets = av_mallocz(slice_mode_data * sizeof(*slice_offsets));
1435
Timo Rothenpieler2a428db2014-11-29 23:04:371436 if (!slice_offsets)
1437 return AVERROR(ENOMEM);
1438
1439 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
1440
1441 lock_params.doNotWait = 0;
1442 lock_params.outputBitstream = tmpoutsurf->output_surface;
1443 lock_params.sliceOffsets = slice_offsets;
1444
1445 nv_status = p_nvenc->nvEncLockBitstream(ctx->nvencoder, &lock_params);
1446 if (nv_status != NV_ENC_SUCCESS) {
Andrey Turkine1691c42016-05-20 15:37:001447 res = nvenc_print_error(avctx, nv_status, "Failed locking bitstream buffer");
Timo Rothenpieler2a428db2014-11-29 23:04:371448 goto error;
1449 }
1450
Agatha Hu49046582015-09-11 09:07:101451 if (res = ff_alloc_packet2(avctx, pkt, lock_params.bitstreamSizeInBytes,0)) {
Timo Rothenpieler2a428db2014-11-29 23:04:371452 p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1453 goto error;
1454 }
1455
1456 memcpy(pkt->data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
1457
1458 nv_status = p_nvenc->nvEncUnlockBitstream(ctx->nvencoder, tmpoutsurf->output_surface);
1459 if (nv_status != NV_ENC_SUCCESS)
Andrey Turkine1691c42016-05-20 15:37:001460 nvenc_print_error(avctx, nv_status, "Failed unlocking bitstream buffer, expect the gates of mordor to open");
Timo Rothenpieler2a428db2014-11-29 23:04:371461
Andrey Turkina8cf25d2016-05-20 22:08:061462
1463 if (avctx->pix_fmt == AV_PIX_FMT_CUDA) {
1464 p_nvenc->nvEncUnmapInputResource(ctx->nvencoder, tmpoutsurf->in_map.mappedResource);
1465 av_frame_unref(tmpoutsurf->in_ref);
1466 ctx->registered_frames[tmpoutsurf->reg_idx].mapped = 0;
1467
1468 tmpoutsurf->input_surface = NULL;
1469 }
1470
Timo Rothenpieler2a428db2014-11-29 23:04:371471 switch (lock_params.pictureType) {
1472 case NV_ENC_PIC_TYPE_IDR:
1473 pkt->flags |= AV_PKT_FLAG_KEY;
1474 case NV_ENC_PIC_TYPE_I:
Lucas Cooperfd554702016-03-07 23:47:561475 pict_type = AV_PICTURE_TYPE_I;
Timo Rothenpieler2a428db2014-11-29 23:04:371476 break;
1477 case NV_ENC_PIC_TYPE_P:
Lucas Cooperfd554702016-03-07 23:47:561478 pict_type = AV_PICTURE_TYPE_P;
Timo Rothenpieler2a428db2014-11-29 23:04:371479 break;
1480 case NV_ENC_PIC_TYPE_B:
Lucas Cooperfd554702016-03-07 23:47:561481 pict_type = AV_PICTURE_TYPE_B;
Timo Rothenpieler2a428db2014-11-29 23:04:371482 break;
1483 case NV_ENC_PIC_TYPE_BI:
Lucas Cooperfd554702016-03-07 23:47:561484 pict_type = AV_PICTURE_TYPE_BI;
Timo Rothenpieler2a428db2014-11-29 23:04:371485 break;
1486 default:
1487 av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered, expect the output to be broken.\n");
1488 av_log(avctx, AV_LOG_ERROR, "Please report this error and include as much information on how to reproduce it as possible.\n");
1489 res = AVERROR_EXTERNAL;
1490 goto error;
Lucas Cooperfd554702016-03-07 23:47:561491 }
1492
1493#if FF_API_CODED_FRAME
1494FF_DISABLE_DEPRECATION_WARNINGS
1495 avctx->coded_frame->pict_type = pict_type;
Vittorio Giovara40cf1bb2015-07-15 17:41:221496FF_ENABLE_DEPRECATION_WARNINGS
1497#endif
Lucas Cooperfd554702016-03-07 23:47:561498
1499 ff_side_data_set_encoder_stats(pkt,
1500 (lock_params.frameAvgQP - 1) * FF_QP2LAMBDA, NULL, 0, pict_type);
Timo Rothenpieler2a428db2014-11-29 23:04:371501
1502 pkt->pts = lock_params.outputTimeStamp;
Andrey Turkincfb49fc2016-05-20 16:13:201503 pkt->dts = timestamp_queue_dequeue(ctx->timestamp_list);
Timo Rothenpieler2a428db2014-11-29 23:04:371504
Timo Rothenpieler914fd422015-01-26 12:28:211505 /* when there're b frame(s), set dts offset */
agathah72c61c22015-01-07 09:19:321506 if (ctx->encode_config.frameIntervalP >= 2)
1507 pkt->dts -= 1;
1508
Timo Rothenpieler2a428db2014-11-29 23:04:371509 if (pkt->dts > pkt->pts)
1510 pkt->dts = pkt->pts;
1511
1512 if (ctx->last_dts != AV_NOPTS_VALUE && pkt->dts <= ctx->last_dts)
1513 pkt->dts = ctx->last_dts + 1;
1514
1515 ctx->last_dts = pkt->dts;
1516
1517 av_free(slice_offsets);
1518
1519 return 0;
1520
1521error:
1522
1523 av_free(slice_offsets);
Andrey Turkincfb49fc2016-05-20 16:13:201524 timestamp_queue_dequeue(ctx->timestamp_list);
Timo Rothenpieler2a428db2014-11-29 23:04:371525
1526 return res;
1527}
1528
Andrey Turkincfb49fc2016-05-20 16:13:201529static int output_ready(NvencContext *ctx, int flush)
1530{
1531 int nb_ready, nb_pending;
1532
1533 nb_ready = av_fifo_size(ctx->output_surface_ready_queue) / sizeof(NvencSurface*);
1534 nb_pending = av_fifo_size(ctx->output_surface_queue) / sizeof(NvencSurface*);
Andrey Turkinf052ef32016-05-29 10:07:341535 return nb_ready > 0 && (flush || nb_ready + nb_pending >= ctx->async_depth);
Andrey Turkincfb49fc2016-05-20 16:13:201536}
1537
Andrey Turkin7aa16d52016-05-25 13:04:281538int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
Timo Rothenpieler2a428db2014-11-29 23:04:371539 const AVFrame *frame, int *got_packet)
1540{
1541 NVENCSTATUS nv_status;
Andrey Turkine1691c42016-05-20 15:37:001542 NvencSurface *tmpoutsurf, *inSurf;
1543 int res;
Timo Rothenpieler2a428db2014-11-29 23:04:371544
1545 NvencContext *ctx = avctx->priv_data;
1546 NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
1547 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
1548
1549 NV_ENC_PIC_PARAMS pic_params = { 0 };
1550 pic_params.version = NV_ENC_PIC_PARAMS_VER;
1551
1552 if (frame) {
Andrey Turkin82d705e2016-05-20 14:49:241553 inSurf = get_free_frame(ctx);
Timo Rothenpieler2a428db2014-11-29 23:04:371554 av_assert0(inSurf);
1555
Andrey Turkin82d705e2016-05-20 14:49:241556 res = nvenc_upload_frame(avctx, frame, inSurf);
1557 if (res) {
1558 inSurf->lockCount = 0;
1559 return res;
Timo Rothenpieler2a428db2014-11-29 23:04:371560 }
1561
Timo Rothenpieler2a428db2014-11-29 23:04:371562 pic_params.inputBuffer = inSurf->input_surface;
1563 pic_params.bufferFmt = inSurf->format;
1564 pic_params.inputWidth = avctx->width;
1565 pic_params.inputHeight = avctx->height;
Andrey Turkine1691c42016-05-20 15:37:001566 pic_params.outputBitstream = inSurf->output_surface;
Timo Rothenpieler2a428db2014-11-29 23:04:371567 pic_params.completionEvent = 0;
1568
Michael Niedermayer94d68a42015-07-27 19:14:311569 if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
Timo Rothenpieler2a428db2014-11-29 23:04:371570 if (frame->top_field_first) {
1571 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
1572 } else {
1573 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
1574 }
1575 } else {
1576 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
1577 }
1578
1579 pic_params.encodePicFlags = 0;
1580 pic_params.inputTimeStamp = frame->pts;
1581 pic_params.inputDuration = 0;
Andrey Turkin82d705e2016-05-20 14:49:241582
1583 nvenc_codec_specific_pic_params(avctx, &pic_params);
Timo Rothenpielerbc3f7672015-01-16 00:02:401584
Andrey Turkincfb49fc2016-05-20 16:13:201585 timestamp_queue_enqueue(ctx->timestamp_list, frame->pts);
Timo Rothenpieler2a428db2014-11-29 23:04:371586 } else {
1587 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
1588 }
1589
1590 nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
1591
Andrey Turkincfb49fc2016-05-20 16:13:201592 if (frame && nv_status == NV_ENC_ERR_NEED_MORE_INPUT)
1593 av_fifo_generic_write(ctx->output_surface_queue, &inSurf, sizeof(inSurf), NULL);
Timo Rothenpieler2a428db2014-11-29 23:04:371594
1595 if (nv_status != NV_ENC_SUCCESS && nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
Andrey Turkine1691c42016-05-20 15:37:001596 return nvenc_print_error(avctx, nv_status, "EncodePicture failed!");
Timo Rothenpieler2a428db2014-11-29 23:04:371597 }
1598
1599 if (nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
Andrey Turkincfb49fc2016-05-20 16:13:201600 while (av_fifo_size(ctx->output_surface_queue) > 0) {
1601 av_fifo_generic_read(ctx->output_surface_queue, &tmpoutsurf, sizeof(tmpoutsurf), NULL);
1602 av_fifo_generic_write(ctx->output_surface_ready_queue, &tmpoutsurf, sizeof(tmpoutsurf), NULL);
Timo Rothenpieler2a428db2014-11-29 23:04:371603 }
1604
Andrey Turkincfb49fc2016-05-20 16:13:201605 if (frame)
1606 av_fifo_generic_write(ctx->output_surface_ready_queue, &inSurf, sizeof(inSurf), NULL);
Timo Rothenpieler2a428db2014-11-29 23:04:371607 }
1608
Andrey Turkincfb49fc2016-05-20 16:13:201609 if (output_ready(ctx, !frame)) {
1610 av_fifo_generic_read(ctx->output_surface_ready_queue, &tmpoutsurf, sizeof(tmpoutsurf), NULL);
Timo Rothenpieler2a428db2014-11-29 23:04:371611
Timo Rothenpieler15cd2f82015-07-25 21:26:421612 res = process_output_surface(avctx, pkt, tmpoutsurf);
Timo Rothenpieler2a428db2014-11-29 23:04:371613
1614 if (res)
1615 return res;
1616
Andrey Turkine1691c42016-05-20 15:37:001617 av_assert0(tmpoutsurf->lockCount);
1618 tmpoutsurf->lockCount--;
Timo Rothenpieler2a428db2014-11-29 23:04:371619
1620 *got_packet = 1;
1621 } else {
1622 *got_packet = 0;
1623 }
1624
1625 return 0;
1626}