You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1222 lines
38KB

  1. /*
  2. * NVIDIA NVENC Support
  3. * Copyright (C) 2015 Luca Barbato
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include <cuda.h>
  23. #include <nvEncodeAPI.h>
  24. #include <string.h>
  25. #define CUDA_LIBNAME "libcuda.so"
  26. #if HAVE_DLFCN_H
  27. #include <dlfcn.h>
  28. #define NVENC_LIBNAME "libnvidia-encode.so"
  29. #elif HAVE_WINDOWS_H
  30. #include <windows.h>
  31. #if ARCH_X86_64
  32. #define NVENC_LIBNAME "nvEncodeAPI64.dll"
  33. #else
  34. #define NVENC_LIBNAME "nvEncodeAPI.dll"
  35. #endif
  36. #define dlopen(filename, flags) LoadLibrary((filename))
  37. #define dlsym(handle, symbol) GetProcAddress(handle, symbol)
  38. #define dlclose(handle) FreeLibrary(handle)
  39. #endif
  40. #include "libavutil/common.h"
  41. #include "libavutil/imgutils.h"
  42. #include "libavutil/mem.h"
  43. #include "avcodec.h"
  44. #include "internal.h"
  45. #include "nvenc.h"
  46. #define NVENC_CAP 0x30
  47. #define BITSTREAM_BUFFER_SIZE 1024 * 1024
  48. #define LOAD_LIBRARY(l, path) \
  49. do { \
  50. if (!((l) = dlopen(path, RTLD_LAZY))) { \
  51. av_log(avctx, AV_LOG_ERROR, \
  52. "Cannot load %s\n", \
  53. path); \
  54. return AVERROR_UNKNOWN; \
  55. } \
  56. } while (0)
  57. #define LOAD_SYMBOL(fun, lib, symbol) \
  58. do { \
  59. if (!((fun) = dlsym(lib, symbol))) { \
  60. av_log(avctx, AV_LOG_ERROR, \
  61. "Cannot load %s\n", \
  62. symbol); \
  63. return AVERROR_UNKNOWN; \
  64. } \
  65. } while (0)
  66. const enum AVPixelFormat ff_nvenc_pix_fmts[] = {
  67. AV_PIX_FMT_NV12,
  68. AV_PIX_FMT_YUV420P,
  69. AV_PIX_FMT_YUV444P,
  70. AV_PIX_FMT_NONE
  71. };
  72. static const struct {
  73. NVENCSTATUS nverr;
  74. int averr;
  75. const char *desc;
  76. } nvenc_errors[] = {
  77. { NV_ENC_SUCCESS, 0, "success" },
  78. { NV_ENC_ERR_NO_ENCODE_DEVICE, AVERROR(ENOENT), "no encode device" },
  79. { NV_ENC_ERR_UNSUPPORTED_DEVICE, AVERROR(ENOSYS), "unsupported device" },
  80. { NV_ENC_ERR_INVALID_ENCODERDEVICE, AVERROR(EINVAL), "invalid encoder device" },
  81. { NV_ENC_ERR_INVALID_DEVICE, AVERROR(EINVAL), "invalid device" },
  82. { NV_ENC_ERR_DEVICE_NOT_EXIST, AVERROR(EIO), "device does not exist" },
  83. { NV_ENC_ERR_INVALID_PTR, AVERROR(EFAULT), "invalid ptr" },
  84. { NV_ENC_ERR_INVALID_EVENT, AVERROR(EINVAL), "invalid event" },
  85. { NV_ENC_ERR_INVALID_PARAM, AVERROR(EINVAL), "invalid param" },
  86. { NV_ENC_ERR_INVALID_CALL, AVERROR(EINVAL), "invalid call" },
  87. { NV_ENC_ERR_OUT_OF_MEMORY, AVERROR(ENOMEM), "out of memory" },
  88. { NV_ENC_ERR_ENCODER_NOT_INITIALIZED, AVERROR(EINVAL), "encoder not initialized" },
  89. { NV_ENC_ERR_UNSUPPORTED_PARAM, AVERROR(ENOSYS), "unsupported param" },
  90. { NV_ENC_ERR_LOCK_BUSY, AVERROR(EAGAIN), "lock busy" },
  91. { NV_ENC_ERR_NOT_ENOUGH_BUFFER, AVERROR(ENOBUFS), "not enough buffer" },
  92. { NV_ENC_ERR_INVALID_VERSION, AVERROR(EINVAL), "invalid version" },
  93. { NV_ENC_ERR_MAP_FAILED, AVERROR(EIO), "map failed" },
  94. { NV_ENC_ERR_NEED_MORE_INPUT, AVERROR(EAGAIN), "need more input" },
  95. { NV_ENC_ERR_ENCODER_BUSY, AVERROR(EAGAIN), "encoder busy" },
  96. { NV_ENC_ERR_EVENT_NOT_REGISTERD, AVERROR(EBADF), "event not registered" },
  97. { NV_ENC_ERR_GENERIC, AVERROR_UNKNOWN, "generic error" },
  98. { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, AVERROR(EINVAL), "incompatible client key" },
  99. { NV_ENC_ERR_UNIMPLEMENTED, AVERROR(ENOSYS), "unimplemented" },
  100. { NV_ENC_ERR_RESOURCE_REGISTER_FAILED, AVERROR(EIO), "resource register failed" },
  101. { NV_ENC_ERR_RESOURCE_NOT_REGISTERED, AVERROR(EBADF), "resource not registered" },
  102. { NV_ENC_ERR_RESOURCE_NOT_MAPPED, AVERROR(EBADF), "resource not mapped" },
  103. };
  104. static int nvenc_map_error(NVENCSTATUS err, const char **desc)
  105. {
  106. int i;
  107. for (i = 0; i < FF_ARRAY_ELEMS(nvenc_errors); i++) {
  108. if (nvenc_errors[i].nverr == err) {
  109. if (desc)
  110. *desc = nvenc_errors[i].desc;
  111. return nvenc_errors[i].averr;
  112. }
  113. }
  114. if (desc)
  115. *desc = "unknown error";
  116. return AVERROR_UNKNOWN;
  117. }
  118. static int nvenc_print_error(void *log_ctx, NVENCSTATUS err,
  119. const char *error_string)
  120. {
  121. const char *desc;
  122. int ret;
  123. ret = nvenc_map_error(err, &desc);
  124. av_log(log_ctx, AV_LOG_ERROR, "%s: %s (%d)\n", error_string, desc, err);
  125. return ret;
  126. }
  127. static av_cold int nvenc_load_libraries(AVCodecContext *avctx)
  128. {
  129. NVENCContext *ctx = avctx->priv_data;
  130. NVENCLibraryContext *nvel = &ctx->nvel;
  131. PNVENCODEAPICREATEINSTANCE nvenc_create_instance;
  132. NVENCSTATUS err;
  133. #if CONFIG_CUDA
  134. nvel->cu_init = cuInit;
  135. nvel->cu_device_get_count = cuDeviceGetCount;
  136. nvel->cu_device_get = cuDeviceGet;
  137. nvel->cu_device_get_name = cuDeviceGetName;
  138. nvel->cu_device_compute_capability = cuDeviceComputeCapability;
  139. nvel->cu_ctx_create = cuCtxCreate_v2;
  140. nvel->cu_ctx_pop_current = cuCtxPopCurrent_v2;
  141. nvel->cu_ctx_destroy = cuCtxDestroy_v2;
  142. #else
  143. LOAD_LIBRARY(nvel->cuda, CUDA_LIBNAME);
  144. LOAD_SYMBOL(nvel->cu_init, nvel->cuda, "cuInit");
  145. LOAD_SYMBOL(nvel->cu_device_get_count, nvel->cuda, "cuDeviceGetCount");
  146. LOAD_SYMBOL(nvel->cu_device_get, nvel->cuda, "cuDeviceGet");
  147. LOAD_SYMBOL(nvel->cu_device_get_name, nvel->cuda, "cuDeviceGetName");
  148. LOAD_SYMBOL(nvel->cu_device_compute_capability, nvel->cuda,
  149. "cuDeviceComputeCapability");
  150. LOAD_SYMBOL(nvel->cu_ctx_create, nvel->cuda, "cuCtxCreate_v2");
  151. LOAD_SYMBOL(nvel->cu_ctx_pop_current, nvel->cuda, "cuCtxPopCurrent_v2");
  152. LOAD_SYMBOL(nvel->cu_ctx_destroy, nvel->cuda, "cuCtxDestroy_v2");
  153. #endif
  154. LOAD_LIBRARY(nvel->nvenc, NVENC_LIBNAME);
  155. LOAD_SYMBOL(nvenc_create_instance, nvel->nvenc,
  156. "NvEncodeAPICreateInstance");
  157. nvel->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
  158. err = nvenc_create_instance(&nvel->nvenc_funcs);
  159. if (err != NV_ENC_SUCCESS)
  160. return nvenc_print_error(avctx, err, "Cannot create the NVENC instance");
  161. return 0;
  162. }
  163. static int nvenc_open_session(AVCodecContext *avctx)
  164. {
  165. NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
  166. NVENCContext *ctx = avctx->priv_data;
  167. NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
  168. int ret;
  169. params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
  170. params.apiVersion = NVENCAPI_VERSION;
  171. params.device = ctx->cu_context;
  172. params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
  173. ret = nv->nvEncOpenEncodeSessionEx(&params, &ctx->nvenc_ctx);
  174. if (ret != NV_ENC_SUCCESS) {
  175. ctx->nvenc_ctx = NULL;
  176. return nvenc_print_error(avctx, ret, "Cannot open the NVENC Session");
  177. }
  178. return 0;
  179. }
  180. static int nvenc_check_codec_support(AVCodecContext *avctx)
  181. {
  182. NVENCContext *ctx = avctx->priv_data;
  183. NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
  184. int i, ret, count = 0;
  185. GUID *guids = NULL;
  186. ret = nv->nvEncGetEncodeGUIDCount(ctx->nvenc_ctx, &count);
  187. if (ret != NV_ENC_SUCCESS || !count)
  188. return AVERROR(ENOSYS);
  189. guids = av_malloc(count * sizeof(GUID));
  190. if (!guids)
  191. return AVERROR(ENOMEM);
  192. ret = nv->nvEncGetEncodeGUIDs(ctx->nvenc_ctx, guids, count, &count);
  193. if (ret != NV_ENC_SUCCESS) {
  194. ret = AVERROR(ENOSYS);
  195. goto fail;
  196. }
  197. ret = AVERROR(ENOSYS);
  198. for (i = 0; i < count; i++) {
  199. if (!memcmp(&guids[i], &ctx->params.encodeGUID, sizeof(*guids))) {
  200. ret = 0;
  201. break;
  202. }
  203. }
  204. fail:
  205. av_free(guids);
  206. return ret;
  207. }
  208. static int nvenc_check_cap(AVCodecContext *avctx, NV_ENC_CAPS cap)
  209. {
  210. NVENCContext *ctx = avctx->priv_data;
  211. NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
  212. NV_ENC_CAPS_PARAM params = { 0 };
  213. int ret, val = 0;
  214. params.version = NV_ENC_CAPS_PARAM_VER;
  215. params.capsToQuery = cap;
  216. ret = nv->nvEncGetEncodeCaps(ctx->nvenc_ctx, ctx->params.encodeGUID, &params, &val);
  217. if (ret == NV_ENC_SUCCESS)
  218. return val;
  219. return 0;
  220. }
  221. static int nvenc_check_capabilities(AVCodecContext *avctx)
  222. {
  223. int ret;
  224. ret = nvenc_check_codec_support(avctx);
  225. if (ret < 0) {
  226. av_log(avctx, AV_LOG_VERBOSE, "Codec not supported\n");
  227. return ret;
  228. }
  229. ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_YUV444_ENCODE);
  230. if (avctx->pix_fmt == AV_PIX_FMT_YUV444P && ret <= 0) {
  231. av_log(avctx, AV_LOG_VERBOSE, "YUV444P not supported\n");
  232. return AVERROR(ENOSYS);
  233. }
  234. ret = nvenc_check_cap(avctx, NV_ENC_CAPS_WIDTH_MAX);
  235. if (ret < avctx->width) {
  236. av_log(avctx, AV_LOG_VERBOSE, "Width %d exceeds %d\n",
  237. avctx->width, ret);
  238. return AVERROR(ENOSYS);
  239. }
  240. ret = nvenc_check_cap(avctx, NV_ENC_CAPS_HEIGHT_MAX);
  241. if (ret < avctx->height) {
  242. av_log(avctx, AV_LOG_VERBOSE, "Height %d exceeds %d\n",
  243. avctx->height, ret);
  244. return AVERROR(ENOSYS);
  245. }
  246. ret = nvenc_check_cap(avctx, NV_ENC_CAPS_NUM_MAX_BFRAMES);
  247. if (ret < avctx->max_b_frames) {
  248. av_log(avctx, AV_LOG_VERBOSE, "Max b-frames %d exceed %d\n",
  249. avctx->max_b_frames, ret);
  250. return AVERROR(ENOSYS);
  251. }
  252. return 0;
  253. }
  254. static int nvenc_check_device(AVCodecContext *avctx, int idx)
  255. {
  256. NVENCContext *ctx = avctx->priv_data;
  257. NVENCLibraryContext *nvel = &ctx->nvel;
  258. char name[128] = { 0 };
  259. int major, minor, ret;
  260. CUdevice cu_device;
  261. CUcontext dummy;
  262. int loglevel = AV_LOG_VERBOSE;
  263. if (ctx->device == LIST_DEVICES)
  264. loglevel = AV_LOG_INFO;
  265. ret = nvel->cu_device_get(&cu_device, idx);
  266. if (ret != CUDA_SUCCESS) {
  267. av_log(avctx, AV_LOG_ERROR,
  268. "Cannot access the CUDA device %d\n",
  269. idx);
  270. return -1;
  271. }
  272. ret = nvel->cu_device_get_name(name, sizeof(name), cu_device);
  273. if (ret != CUDA_SUCCESS)
  274. return -1;
  275. ret = nvel->cu_device_compute_capability(&major, &minor, cu_device);
  276. if (ret != CUDA_SUCCESS)
  277. return -1;
  278. av_log(avctx, loglevel, "Device %d [%s] ", cu_device, name);
  279. if (((major << 4) | minor) < NVENC_CAP)
  280. goto fail;
  281. ret = nvel->cu_ctx_create(&ctx->cu_context, 0, cu_device);
  282. if (ret != CUDA_SUCCESS)
  283. goto fail;
  284. ret = nvel->cu_ctx_pop_current(&dummy);
  285. if (ret != CUDA_SUCCESS)
  286. goto fail2;
  287. if ((ret = nvenc_open_session(avctx)) < 0)
  288. goto fail2;
  289. if ((ret = nvenc_check_capabilities(avctx)) < 0)
  290. goto fail3;
  291. av_log(avctx, loglevel, "supports NVENC\n");
  292. if (ctx->device == cu_device || ctx->device == ANY_DEVICE)
  293. return 0;
  294. fail3:
  295. nvel->nvenc_funcs.nvEncDestroyEncoder(ctx->nvenc_ctx);
  296. ctx->nvenc_ctx = NULL;
  297. fail2:
  298. nvel->cu_ctx_destroy(ctx->cu_context);
  299. ctx->cu_context = NULL;
  300. fail:
  301. if (ret != 0)
  302. av_log(avctx, loglevel, "does not support NVENC (major %d minor %d)\n",
  303. major, minor);
  304. return AVERROR(ENOSYS);
  305. }
  306. static int nvenc_setup_device(AVCodecContext *avctx)
  307. {
  308. NVENCContext *ctx = avctx->priv_data;
  309. NVENCLibraryContext *nvel = &ctx->nvel;
  310. int i, nb_devices = 0;
  311. if ((nvel->cu_init(0)) != CUDA_SUCCESS) {
  312. av_log(avctx, AV_LOG_ERROR,
  313. "Cannot init CUDA\n");
  314. return AVERROR_UNKNOWN;
  315. }
  316. if ((nvel->cu_device_get_count(&nb_devices)) != CUDA_SUCCESS) {
  317. av_log(avctx, AV_LOG_ERROR,
  318. "Cannot enumerate the CUDA devices\n");
  319. return AVERROR_UNKNOWN;
  320. }
  321. switch (avctx->codec->id) {
  322. case AV_CODEC_ID_H264:
  323. ctx->params.encodeGUID = NV_ENC_CODEC_H264_GUID;
  324. break;
  325. case AV_CODEC_ID_HEVC:
  326. ctx->params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
  327. break;
  328. default:
  329. return AVERROR_BUG;
  330. }
  331. for (i = 0; i < nb_devices; ++i) {
  332. if ((nvenc_check_device(avctx, i)) >= 0 && ctx->device != LIST_DEVICES)
  333. return 0;
  334. }
  335. if (ctx->device == LIST_DEVICES)
  336. return AVERROR_EXIT;
  337. return AVERROR(ENOSYS);
  338. }
  339. typedef struct GUIDTuple {
  340. const GUID guid;
  341. int flags;
  342. } GUIDTuple;
  343. static int nvec_map_preset(NVENCContext *ctx)
  344. {
  345. GUIDTuple presets[] = {
  346. { NV_ENC_PRESET_DEFAULT_GUID },
  347. { NV_ENC_PRESET_HP_GUID },
  348. { NV_ENC_PRESET_HQ_GUID },
  349. { NV_ENC_PRESET_BD_GUID },
  350. { NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID, NVENC_LOWLATENCY },
  351. { NV_ENC_PRESET_LOW_LATENCY_HP_GUID, NVENC_LOWLATENCY },
  352. { NV_ENC_PRESET_LOW_LATENCY_HQ_GUID, NVENC_LOWLATENCY },
  353. { NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID, NVENC_LOSSLESS },
  354. { NV_ENC_PRESET_LOSSLESS_HP_GUID, NVENC_LOSSLESS },
  355. { { 0 } }
  356. };
  357. GUIDTuple *t = &presets[ctx->preset];
  358. ctx->params.presetGUID = t->guid;
  359. ctx->flags = t->flags;
  360. return AVERROR(EINVAL);
  361. }
  362. static void set_constqp(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
  363. {
  364. rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
  365. rc->constQP.qpInterB = avctx->global_quality;
  366. rc->constQP.qpInterP = avctx->global_quality;
  367. rc->constQP.qpIntra = avctx->global_quality;
  368. }
  369. static void set_vbr(AVCodecContext *avctx, NV_ENC_RC_PARAMS *rc)
  370. {
  371. if (avctx->qmin >= 0) {
  372. rc->enableMinQP = 1;
  373. rc->minQP.qpInterB = avctx->qmin;
  374. rc->minQP.qpInterP = avctx->qmin;
  375. rc->minQP.qpIntra = avctx->qmin;
  376. }
  377. if (avctx->qmax >= 0) {
  378. rc->enableMaxQP = 1;
  379. rc->maxQP.qpInterB = avctx->qmax;
  380. rc->maxQP.qpInterP = avctx->qmax;
  381. rc->maxQP.qpIntra = avctx->qmax;
  382. }
  383. }
  384. static void nvenc_override_rate_control(AVCodecContext *avctx,
  385. NV_ENC_RC_PARAMS *rc)
  386. {
  387. NVENCContext *ctx = avctx->priv_data;
  388. switch (ctx->rc) {
  389. case NV_ENC_PARAMS_RC_CONSTQP:
  390. if (avctx->global_quality < 0) {
  391. av_log(avctx, AV_LOG_WARNING,
  392. "The constant quality rate-control requires "
  393. "the 'global_quality' option set.\n");
  394. return;
  395. }
  396. set_constqp(avctx, rc);
  397. return;
  398. case NV_ENC_PARAMS_RC_2_PASS_VBR:
  399. case NV_ENC_PARAMS_RC_VBR:
  400. if (avctx->qmin < 0 && avctx->qmax < 0) {
  401. av_log(avctx, AV_LOG_WARNING,
  402. "The variable bitrate rate-control requires "
  403. "the 'qmin' and/or 'qmax' option set.\n");
  404. return;
  405. }
  406. case NV_ENC_PARAMS_RC_VBR_MINQP:
  407. if (avctx->qmin < 0) {
  408. av_log(avctx, AV_LOG_WARNING,
  409. "The variable bitrate rate-control requires "
  410. "the 'qmin' option set.\n");
  411. return;
  412. }
  413. set_vbr(avctx, rc);
  414. break;
  415. case NV_ENC_PARAMS_RC_CBR:
  416. break;
  417. case NV_ENC_PARAMS_RC_2_PASS_QUALITY:
  418. case NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP:
  419. if (!(ctx->flags & NVENC_LOWLATENCY)) {
  420. av_log(avctx, AV_LOG_WARNING,
  421. "The multipass rate-control requires "
  422. "a low-latency preset.\n");
  423. return;
  424. }
  425. }
  426. rc->rateControlMode = ctx->rc;
  427. }
  428. static void nvenc_setup_rate_control(AVCodecContext *avctx)
  429. {
  430. NVENCContext *ctx = avctx->priv_data;
  431. NV_ENC_RC_PARAMS *rc = &ctx->config.rcParams;
  432. if (avctx->bit_rate > 0)
  433. rc->averageBitRate = avctx->bit_rate;
  434. if (avctx->rc_max_rate > 0)
  435. rc->maxBitRate = avctx->rc_max_rate;
  436. if (ctx->rc > 0) {
  437. nvenc_override_rate_control(avctx, rc);
  438. } else if (avctx->global_quality > 0) {
  439. set_constqp(avctx, rc);
  440. } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
  441. rc->rateControlMode = NV_ENC_PARAMS_RC_VBR;
  442. set_vbr(avctx, rc);
  443. }
  444. if (avctx->rc_buffer_size > 0)
  445. rc->vbvBufferSize = avctx->rc_buffer_size;
  446. if (rc->averageBitRate > 0)
  447. avctx->bit_rate = rc->averageBitRate;
  448. }
  449. static int nvenc_setup_h264_config(AVCodecContext *avctx)
  450. {
  451. NVENCContext *ctx = avctx->priv_data;
  452. NV_ENC_CONFIG *cc = &ctx->config;
  453. NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
  454. NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
  455. vui->colourDescriptionPresentFlag = 1;
  456. vui->videoSignalTypePresentFlag = 1;
  457. vui->colourMatrix = avctx->colorspace;
  458. vui->colourPrimaries = avctx->color_primaries;
  459. vui->transferCharacteristics = avctx->color_trc;
  460. vui->videoFullRangeFlag = avctx->color_range == AVCOL_RANGE_JPEG;
  461. h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
  462. h264->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
  463. h264->maxNumRefFrames = avctx->refs;
  464. h264->idrPeriod = cc->gopLength;
  465. if (ctx->profile)
  466. avctx->profile = ctx->profile;
  467. if (avctx->pix_fmt == AV_PIX_FMT_YUV444P)
  468. h264->chromaFormatIDC = 3;
  469. else
  470. h264->chromaFormatIDC = 1;
  471. switch (ctx->profile) {
  472. case NV_ENC_H264_PROFILE_BASELINE:
  473. cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
  474. break;
  475. case NV_ENC_H264_PROFILE_MAIN:
  476. cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
  477. break;
  478. case NV_ENC_H264_PROFILE_HIGH:
  479. cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
  480. break;
  481. case NV_ENC_H264_PROFILE_HIGH_444:
  482. cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
  483. break;
  484. case NV_ENC_H264_PROFILE_CONSTRAINED_HIGH:
  485. cc->profileGUID = NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID;
  486. break;
  487. }
  488. h264->level = ctx->level;
  489. return 0;
  490. }
  491. static int nvenc_setup_hevc_config(AVCodecContext *avctx)
  492. {
  493. NVENCContext *ctx = avctx->priv_data;
  494. NV_ENC_CONFIG *cc = &ctx->config;
  495. NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
  496. hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
  497. hevc->repeatSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
  498. hevc->maxNumRefFramesInDPB = avctx->refs;
  499. hevc->idrPeriod = cc->gopLength;
  500. /* No other profile is supported in the current SDK version 5 */
  501. cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
  502. avctx->profile = FF_PROFILE_HEVC_MAIN;
  503. if (ctx->level) {
  504. hevc->level = ctx->level;
  505. } else {
  506. hevc->level = NV_ENC_LEVEL_AUTOSELECT;
  507. }
  508. if (ctx->tier) {
  509. hevc->tier = ctx->tier;
  510. }
  511. return 0;
  512. }
  513. static int nvenc_setup_codec_config(AVCodecContext *avctx)
  514. {
  515. switch (avctx->codec->id) {
  516. case AV_CODEC_ID_H264:
  517. return nvenc_setup_h264_config(avctx);
  518. case AV_CODEC_ID_HEVC:
  519. return nvenc_setup_hevc_config(avctx);
  520. }
  521. return 0;
  522. }
  523. static int nvenc_setup_encoder(AVCodecContext *avctx)
  524. {
  525. NVENCContext *ctx = avctx->priv_data;
  526. NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
  527. NV_ENC_PRESET_CONFIG preset_cfg = { 0 };
  528. AVCPBProperties *cpb_props;
  529. int ret;
  530. ctx->params.version = NV_ENC_INITIALIZE_PARAMS_VER;
  531. ctx->params.encodeHeight = avctx->height;
  532. ctx->params.encodeWidth = avctx->width;
  533. if (avctx->sample_aspect_ratio.num &&
  534. avctx->sample_aspect_ratio.den &&
  535. (avctx->sample_aspect_ratio.num != 1 ||
  536. avctx->sample_aspect_ratio.den != 1)) {
  537. av_reduce(&ctx->params.darWidth,
  538. &ctx->params.darHeight,
  539. avctx->width * avctx->sample_aspect_ratio.num,
  540. avctx->height * avctx->sample_aspect_ratio.den,
  541. INT_MAX / 8);
  542. } else {
  543. ctx->params.darHeight = avctx->height;
  544. ctx->params.darWidth = avctx->width;
  545. }
  546. ctx->params.frameRateNum = avctx->time_base.den;
  547. ctx->params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
  548. ctx->params.enableEncodeAsync = 0;
  549. ctx->params.enablePTD = 1;
  550. ctx->params.encodeConfig = &ctx->config;
  551. nvec_map_preset(ctx);
  552. preset_cfg.version = NV_ENC_PRESET_CONFIG_VER;
  553. preset_cfg.presetCfg.version = NV_ENC_CONFIG_VER;
  554. ret = nv->nvEncGetEncodePresetConfig(ctx->nvenc_ctx,
  555. ctx->params.encodeGUID,
  556. ctx->params.presetGUID,
  557. &preset_cfg);
  558. if (ret != NV_ENC_SUCCESS)
  559. return nvenc_print_error(avctx, ret, "Cannot get the preset configuration");
  560. memcpy(&ctx->config, &preset_cfg.presetCfg, sizeof(ctx->config));
  561. ctx->config.version = NV_ENC_CONFIG_VER;
  562. if (avctx->gop_size > 0) {
  563. if (avctx->max_b_frames > 0) {
  564. /* 0 is intra-only,
  565. * 1 is I/P only,
  566. * 2 is one B Frame,
  567. * 3 two B frames, and so on. */
  568. ctx->config.frameIntervalP = avctx->max_b_frames + 1;
  569. } else if (avctx->max_b_frames == 0) {
  570. ctx->config.frameIntervalP = 1;
  571. }
  572. ctx->config.gopLength = avctx->gop_size;
  573. } else if (avctx->gop_size == 0) {
  574. ctx->config.frameIntervalP = 0;
  575. ctx->config.gopLength = 1;
  576. }
  577. if (ctx->config.frameIntervalP > 1)
  578. avctx->max_b_frames = ctx->config.frameIntervalP - 1;
  579. ctx->initial_pts[0] = AV_NOPTS_VALUE;
  580. ctx->initial_pts[1] = AV_NOPTS_VALUE;
  581. nvenc_setup_rate_control(avctx);
  582. if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  583. ctx->config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
  584. } else {
  585. ctx->config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
  586. }
  587. if ((ret = nvenc_setup_codec_config(avctx)) < 0)
  588. return ret;
  589. ret = nv->nvEncInitializeEncoder(ctx->nvenc_ctx, &ctx->params);
  590. if (ret != NV_ENC_SUCCESS)
  591. return nvenc_print_error(avctx, ret, "Cannot initialize the decoder");
  592. cpb_props = ff_add_cpb_side_data(avctx);
  593. if (!cpb_props)
  594. return AVERROR(ENOMEM);
  595. cpb_props->max_bitrate = avctx->rc_max_rate;
  596. cpb_props->min_bitrate = avctx->rc_min_rate;
  597. cpb_props->avg_bitrate = avctx->bit_rate;
  598. cpb_props->buffer_size = avctx->rc_buffer_size;
  599. return 0;
  600. }
  601. static int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
  602. {
  603. NVENCContext *ctx = avctx->priv_data;
  604. NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
  605. int ret;
  606. NV_ENC_CREATE_INPUT_BUFFER in_buffer = { 0 };
  607. NV_ENC_CREATE_BITSTREAM_BUFFER out_buffer = { 0 };
  608. in_buffer.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
  609. out_buffer.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
  610. in_buffer.width = avctx->width;
  611. in_buffer.height = avctx->height;
  612. in_buffer.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED;
  613. switch (avctx->pix_fmt) {
  614. case AV_PIX_FMT_YUV420P:
  615. in_buffer.bufferFmt = NV_ENC_BUFFER_FORMAT_YV12_PL;
  616. break;
  617. case AV_PIX_FMT_NV12:
  618. in_buffer.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL;
  619. break;
  620. case AV_PIX_FMT_YUV444P:
  621. in_buffer.bufferFmt = NV_ENC_BUFFER_FORMAT_YUV444_PL;
  622. break;
  623. default:
  624. return AVERROR_BUG;
  625. }
  626. ret = nv->nvEncCreateInputBuffer(ctx->nvenc_ctx, &in_buffer);
  627. if (ret != NV_ENC_SUCCESS)
  628. return nvenc_print_error(avctx, ret, "CreateInputBuffer failed");
  629. ctx->frames[idx].in = in_buffer.inputBuffer;
  630. ctx->frames[idx].format = in_buffer.bufferFmt;
  631. /* 1MB is large enough to hold most output frames.
  632. * NVENC increases this automaticaly if it's not enough. */
  633. out_buffer.size = BITSTREAM_BUFFER_SIZE;
  634. out_buffer.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED;
  635. ret = nv->nvEncCreateBitstreamBuffer(ctx->nvenc_ctx, &out_buffer);
  636. if (ret != NV_ENC_SUCCESS)
  637. return nvenc_print_error(avctx, ret, "CreateBitstreamBuffer failed");
  638. ctx->frames[idx].out = out_buffer.bitstreamBuffer;
  639. return 0;
  640. }
  641. static int nvenc_setup_surfaces(AVCodecContext *avctx)
  642. {
  643. NVENCContext *ctx = avctx->priv_data;
  644. int i, ret;
  645. ctx->nb_surfaces = FFMAX(4 + avctx->max_b_frames,
  646. ctx->nb_surfaces);
  647. ctx->frames = av_mallocz_array(ctx->nb_surfaces, sizeof(*ctx->frames));
  648. if (!ctx->frames)
  649. return AVERROR(ENOMEM);
  650. ctx->timestamps = av_fifo_alloc(ctx->nb_surfaces * sizeof(int64_t));
  651. if (!ctx->timestamps)
  652. return AVERROR(ENOMEM);
  653. ctx->pending = av_fifo_alloc(ctx->nb_surfaces * sizeof(*ctx->frames));
  654. if (!ctx->pending)
  655. return AVERROR(ENOMEM);
  656. ctx->ready = av_fifo_alloc(ctx->nb_surfaces * sizeof(*ctx->frames));
  657. if (!ctx->ready)
  658. return AVERROR(ENOMEM);
  659. for (i = 0; i < ctx->nb_surfaces; i++) {
  660. if ((ret = nvenc_alloc_surface(avctx, i)) < 0)
  661. return ret;
  662. }
  663. return 0;
  664. }
  665. #define EXTRADATA_SIZE 512
  666. static int nvenc_setup_extradata(AVCodecContext *avctx)
  667. {
  668. NVENCContext *ctx = avctx->priv_data;
  669. NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
  670. NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
  671. int ret;
  672. avctx->extradata = av_mallocz(EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
  673. if (!avctx->extradata)
  674. return AVERROR(ENOMEM);
  675. payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
  676. payload.spsppsBuffer = avctx->extradata;
  677. payload.inBufferSize = EXTRADATA_SIZE;
  678. payload.outSPSPPSPayloadSize = &avctx->extradata_size;
  679. ret = nv->nvEncGetSequenceParams(ctx->nvenc_ctx, &payload);
  680. if (ret != NV_ENC_SUCCESS)
  681. return nvenc_print_error(avctx, ret, "Cannot get the extradata");
  682. return 0;
  683. }
  684. av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
  685. {
  686. NVENCContext *ctx = avctx->priv_data;
  687. NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
  688. int i;
  689. /* the encoder has to be flushed before it can be closed */
  690. if (ctx->nvenc_ctx) {
  691. NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
  692. .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
  693. nv->nvEncEncodePicture(ctx->nvenc_ctx, &params);
  694. }
  695. av_fifo_free(ctx->timestamps);
  696. av_fifo_free(ctx->pending);
  697. av_fifo_free(ctx->ready);
  698. if (ctx->frames) {
  699. for (i = 0; i < ctx->nb_surfaces; ++i) {
  700. nv->nvEncDestroyInputBuffer(ctx->nvenc_ctx, ctx->frames[i].in);
  701. nv->nvEncDestroyBitstreamBuffer(ctx->nvenc_ctx, ctx->frames[i].out);
  702. }
  703. }
  704. av_freep(&ctx->frames);
  705. if (ctx->nvenc_ctx)
  706. nv->nvEncDestroyEncoder(ctx->nvenc_ctx);
  707. if (ctx->cu_context)
  708. ctx->nvel.cu_ctx_destroy(ctx->cu_context);
  709. if (ctx->nvel.nvenc)
  710. dlclose(ctx->nvel.nvenc);
  711. #if !CONFIG_CUDA
  712. if (ctx->nvel.cuda)
  713. dlclose(ctx->nvel.cuda);
  714. #endif
  715. return 0;
  716. }
  717. av_cold int ff_nvenc_encode_init(AVCodecContext *avctx)
  718. {
  719. int ret;
  720. if ((ret = nvenc_load_libraries(avctx)) < 0)
  721. return ret;
  722. if ((ret = nvenc_setup_device(avctx)) < 0)
  723. return ret;
  724. if ((ret = nvenc_setup_encoder(avctx)) < 0)
  725. return ret;
  726. if ((ret = nvenc_setup_surfaces(avctx)) < 0)
  727. return ret;
  728. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  729. if ((ret = nvenc_setup_extradata(avctx)) < 0)
  730. return ret;
  731. }
  732. return 0;
  733. }
  734. static NVENCFrame *get_free_frame(NVENCContext *ctx)
  735. {
  736. int i;
  737. for (i = 0; i < ctx->nb_surfaces; i++) {
  738. if (!ctx->frames[i].locked) {
  739. ctx->frames[i].locked = 1;
  740. return &ctx->frames[i];
  741. }
  742. }
  743. return NULL;
  744. }
  745. static int nvenc_copy_frame(NV_ENC_LOCK_INPUT_BUFFER *in, const AVFrame *frame)
  746. {
  747. uint8_t *buf = in->bufferDataPtr;
  748. int off = frame->height * in->pitch;
  749. switch (frame->format) {
  750. case AV_PIX_FMT_YUV420P:
  751. av_image_copy_plane(buf, in->pitch,
  752. frame->data[0], frame->linesize[0],
  753. frame->width, frame->height);
  754. buf += off;
  755. av_image_copy_plane(buf, in->pitch >> 1,
  756. frame->data[2], frame->linesize[2],
  757. frame->width >> 1, frame->height >> 1);
  758. buf += off >> 2;
  759. av_image_copy_plane(buf, in->pitch >> 1,
  760. frame->data[1], frame->linesize[1],
  761. frame->width >> 1, frame->height >> 1);
  762. break;
  763. case AV_PIX_FMT_NV12:
  764. av_image_copy_plane(buf, in->pitch,
  765. frame->data[0], frame->linesize[0],
  766. frame->width, frame->height);
  767. buf += off;
  768. av_image_copy_plane(buf, in->pitch,
  769. frame->data[1], frame->linesize[1],
  770. frame->width, frame->height >> 1);
  771. break;
  772. case AV_PIX_FMT_YUV444P:
  773. av_image_copy_plane(buf, in->pitch,
  774. frame->data[0], frame->linesize[0],
  775. frame->width, frame->height);
  776. buf += off;
  777. av_image_copy_plane(buf, in->pitch,
  778. frame->data[1], frame->linesize[1],
  779. frame->width, frame->height);
  780. buf += off;
  781. av_image_copy_plane(buf, in->pitch,
  782. frame->data[2], frame->linesize[2],
  783. frame->width, frame->height);
  784. break;
  785. default:
  786. return AVERROR_BUG;
  787. }
  788. return 0;
  789. }
  790. static int nvenc_upload_frame(AVCodecContext *avctx, const AVFrame *frame,
  791. NVENCFrame *nvenc_frame)
  792. {
  793. NVENCContext *ctx = avctx->priv_data;
  794. NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
  795. NV_ENC_LOCK_INPUT_BUFFER params = { 0 };
  796. int ret;
  797. params.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
  798. params.inputBuffer = nvenc_frame->in;
  799. ret = nv->nvEncLockInputBuffer(ctx->nvenc_ctx, &params);
  800. if (ret != NV_ENC_SUCCESS)
  801. return nvenc_print_error(avctx, ret, "Cannot lock the buffer");
  802. ret = nvenc_copy_frame(&params, frame);
  803. if (ret < 0)
  804. goto fail;
  805. ret = nv->nvEncUnlockInputBuffer(ctx->nvenc_ctx, nvenc_frame->in);
  806. if (ret != NV_ENC_SUCCESS)
  807. return nvenc_print_error(avctx, ret, "Cannot unlock the buffer");
  808. return 0;
  809. fail:
  810. nv->nvEncUnlockInputBuffer(ctx->nvenc_ctx, nvenc_frame->in);
  811. return ret;
  812. }
  813. static void nvenc_codec_specific_pic_params(AVCodecContext *avctx,
  814. NV_ENC_PIC_PARAMS *params)
  815. {
  816. NVENCContext *ctx = avctx->priv_data;
  817. switch (avctx->codec->id) {
  818. case AV_CODEC_ID_H264:
  819. params->codecPicParams.h264PicParams.sliceMode =
  820. ctx->config.encodeCodecConfig.h264Config.sliceMode;
  821. params->codecPicParams.h264PicParams.sliceModeData =
  822. ctx->config.encodeCodecConfig.h264Config.sliceModeData;
  823. break;
  824. case AV_CODEC_ID_HEVC:
  825. params->codecPicParams.hevcPicParams.sliceMode =
  826. ctx->config.encodeCodecConfig.hevcConfig.sliceMode;
  827. params->codecPicParams.hevcPicParams.sliceModeData =
  828. ctx->config.encodeCodecConfig.hevcConfig.sliceModeData;
  829. break;
  830. }
  831. }
  832. static inline int nvenc_enqueue_timestamp(AVFifoBuffer *f, int64_t pts)
  833. {
  834. return av_fifo_generic_write(f, &pts, sizeof(pts), NULL);
  835. }
  836. static inline int nvenc_dequeue_timestamp(AVFifoBuffer *f, int64_t *pts)
  837. {
  838. return av_fifo_generic_read(f, pts, sizeof(*pts), NULL);
  839. }
  840. static int nvenc_set_timestamp(AVCodecContext *avctx,
  841. NV_ENC_LOCK_BITSTREAM *params,
  842. AVPacket *pkt)
  843. {
  844. NVENCContext *ctx = avctx->priv_data;
  845. pkt->pts = params->outputTimeStamp;
  846. pkt->duration = params->outputDuration;
  847. /* generate the first dts by linearly extrapolating the
  848. * first two pts values to the past */
  849. if (avctx->max_b_frames > 0 && !ctx->first_packet_output &&
  850. ctx->initial_pts[1] != AV_NOPTS_VALUE) {
  851. int64_t ts0 = ctx->initial_pts[0], ts1 = ctx->initial_pts[1];
  852. int64_t delta;
  853. if ((ts0 < 0 && ts1 > INT64_MAX + ts0) ||
  854. (ts0 > 0 && ts1 < INT64_MIN + ts0))
  855. return AVERROR(ERANGE);
  856. delta = ts1 - ts0;
  857. if ((delta < 0 && ts0 > INT64_MAX + delta) ||
  858. (delta > 0 && ts0 < INT64_MIN + delta))
  859. return AVERROR(ERANGE);
  860. pkt->dts = ts0 - delta;
  861. ctx->first_packet_output = 1;
  862. return 0;
  863. }
  864. return nvenc_dequeue_timestamp(ctx->timestamps, &pkt->dts);
  865. }
  866. static int nvenc_get_output(AVCodecContext *avctx, AVPacket *pkt)
  867. {
  868. NVENCContext *ctx = avctx->priv_data;
  869. NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
  870. NV_ENC_LOCK_BITSTREAM params = { 0 };
  871. NVENCFrame *frame;
  872. int ret;
  873. ret = av_fifo_generic_read(ctx->ready, &frame, sizeof(frame), NULL);
  874. if (ret)
  875. return ret;
  876. params.version = NV_ENC_LOCK_BITSTREAM_VER;
  877. params.outputBitstream = frame->out;
  878. ret = nv->nvEncLockBitstream(ctx->nvenc_ctx, &params);
  879. if (ret < 0)
  880. return nvenc_print_error(avctx, ret, "Cannot lock the bitstream");
  881. ret = ff_alloc_packet(pkt, params.bitstreamSizeInBytes);
  882. if (ret < 0)
  883. return ret;
  884. memcpy(pkt->data, params.bitstreamBufferPtr, pkt->size);
  885. ret = nv->nvEncUnlockBitstream(ctx->nvenc_ctx, frame->out);
  886. if (ret < 0)
  887. return nvenc_print_error(avctx, ret, "Cannot unlock the bitstream");
  888. frame->locked = 0;
  889. ret = nvenc_set_timestamp(avctx, &params, pkt);
  890. if (ret < 0)
  891. return ret;
  892. switch (params.pictureType) {
  893. case NV_ENC_PIC_TYPE_IDR:
  894. pkt->flags |= AV_PKT_FLAG_KEY;
  895. #if FF_API_CODED_FRAME
  896. FF_DISABLE_DEPRECATION_WARNINGS
  897. case NV_ENC_PIC_TYPE_INTRA_REFRESH:
  898. case NV_ENC_PIC_TYPE_I:
  899. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  900. break;
  901. case NV_ENC_PIC_TYPE_P:
  902. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
  903. break;
  904. case NV_ENC_PIC_TYPE_B:
  905. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
  906. break;
  907. case NV_ENC_PIC_TYPE_BI:
  908. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_BI;
  909. break;
  910. FF_ENABLE_DEPRECATION_WARNINGS
  911. #endif
  912. }
  913. return 0;
  914. }
  915. static int output_ready(AVCodecContext *avctx, int flush)
  916. {
  917. NVENCContext *ctx = avctx->priv_data;
  918. /* when B-frames are enabled, we wait for two initial timestamps to
  919. * calculate the first dts */
  920. if (!flush && avctx->max_b_frames > 0 &&
  921. (ctx->initial_pts[0] == AV_NOPTS_VALUE || ctx->initial_pts[1] == AV_NOPTS_VALUE))
  922. return 0;
  923. return av_fifo_size(ctx->ready) > 0;
  924. }
  925. int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  926. const AVFrame *frame, int *got_packet)
  927. {
  928. NVENCContext *ctx = avctx->priv_data;
  929. NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
  930. NV_ENC_PIC_PARAMS params = { 0 };
  931. NVENCFrame *nvenc_frame = NULL;
  932. int enc_ret, ret;
  933. params.version = NV_ENC_PIC_PARAMS_VER;
  934. if (frame) {
  935. nvenc_frame = get_free_frame(ctx);
  936. if (!nvenc_frame) {
  937. av_log(avctx, AV_LOG_ERROR, "No free surfaces\n");
  938. return AVERROR_BUG;
  939. }
  940. ret = nvenc_upload_frame(avctx, frame, nvenc_frame);
  941. if (ret < 0)
  942. return ret;
  943. params.inputBuffer = nvenc_frame->in;
  944. params.bufferFmt = nvenc_frame->format;
  945. params.inputWidth = frame->width;
  946. params.inputHeight = frame->height;
  947. params.outputBitstream = nvenc_frame->out;
  948. params.inputTimeStamp = frame->pts;
  949. if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  950. if (frame->top_field_first)
  951. params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
  952. else
  953. params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
  954. } else {
  955. params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
  956. }
  957. nvenc_codec_specific_pic_params(avctx, &params);
  958. ret = nvenc_enqueue_timestamp(ctx->timestamps, frame->pts);
  959. if (ret < 0)
  960. return ret;
  961. if (ctx->initial_pts[0] == AV_NOPTS_VALUE)
  962. ctx->initial_pts[0] = frame->pts;
  963. else if (ctx->initial_pts[1] == AV_NOPTS_VALUE)
  964. ctx->initial_pts[1] = frame->pts;
  965. } else {
  966. params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
  967. }
  968. enc_ret = nv->nvEncEncodePicture(ctx->nvenc_ctx, &params);
  969. if (enc_ret != NV_ENC_SUCCESS &&
  970. enc_ret != NV_ENC_ERR_NEED_MORE_INPUT)
  971. return nvenc_print_error(avctx, enc_ret, "Error encoding the frame");
  972. if (nvenc_frame) {
  973. ret = av_fifo_generic_write(ctx->pending, &nvenc_frame, sizeof(nvenc_frame), NULL);
  974. if (ret < 0)
  975. return ret;
  976. }
  977. /* all the pending buffers are now ready for output */
  978. if (enc_ret == NV_ENC_SUCCESS) {
  979. while (av_fifo_size(ctx->pending) > 0) {
  980. av_fifo_generic_read(ctx->pending, &nvenc_frame, sizeof(nvenc_frame), NULL);
  981. av_fifo_generic_write(ctx->ready, &nvenc_frame, sizeof(nvenc_frame), NULL);
  982. }
  983. }
  984. if (output_ready(avctx, !frame)) {
  985. ret = nvenc_get_output(avctx, pkt);
  986. if (ret < 0)
  987. return ret;
  988. *got_packet = 1;
  989. } else {
  990. *got_packet = 0;
  991. }
  992. return 0;
  993. }