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