2015-05-30 14:40:13 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Libav.
|
|
|
|
*
|
|
|
|
* Libav is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Libav is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with Libav; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AVCODEC_NVENC_H
|
|
|
|
#define AVCODEC_NVENC_H
|
|
|
|
|
|
|
|
#include <nvEncodeAPI.h>
|
|
|
|
|
2016-01-13 13:07:37 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2015-05-30 14:40:13 +00:00
|
|
|
#include "libavutil/fifo.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
|
2016-05-13 07:13:47 +00:00
|
|
|
#if CONFIG_CUDA
|
|
|
|
#include <cuda.h>
|
|
|
|
#else
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#define CUDAAPI __stdcall
|
|
|
|
#else
|
|
|
|
#define CUDAAPI
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef enum cudaError_enum {
|
|
|
|
CUDA_SUCCESS = 0
|
|
|
|
} CUresult;
|
|
|
|
typedef int CUdevice;
|
|
|
|
typedef void* CUcontext;
|
|
|
|
typedef void* CUdeviceptr;
|
|
|
|
#endif
|
|
|
|
|
2016-02-01 14:56:31 +00:00
|
|
|
#define MAX_REGISTERED_FRAMES 64
|
|
|
|
|
2016-01-30 21:06:47 +00:00
|
|
|
typedef struct NVENCFrame {
|
|
|
|
NV_ENC_INPUT_PTR in;
|
2016-02-01 14:56:31 +00:00
|
|
|
AVFrame *in_ref;
|
|
|
|
NV_ENC_MAP_INPUT_RESOURCE in_map;
|
|
|
|
int reg_idx;
|
|
|
|
|
2016-01-30 21:06:47 +00:00
|
|
|
NV_ENC_OUTPUT_PTR out;
|
2015-05-30 14:40:13 +00:00
|
|
|
NV_ENC_BUFFER_FORMAT format;
|
2016-01-30 21:06:47 +00:00
|
|
|
} NVENCFrame;
|
2015-05-30 14:40:13 +00:00
|
|
|
|
|
|
|
typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
|
|
|
|
typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
|
|
|
|
typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
|
|
|
|
typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
|
|
|
|
typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
|
|
|
|
typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
|
|
|
|
typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
|
2017-01-13 09:53:35 +00:00
|
|
|
typedef CUresult(CUDAAPI *PCUCTXPUSHCURRENT)(CUcontext ctx);
|
2015-05-30 14:40:13 +00:00
|
|
|
typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
|
|
|
|
|
|
|
|
typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
|
|
|
|
|
|
|
|
typedef struct NVENCLibraryContext
|
|
|
|
{
|
2016-01-13 13:07:37 +00:00
|
|
|
#if !CONFIG_CUDA
|
2015-05-30 14:40:13 +00:00
|
|
|
void *cuda;
|
2016-01-13 13:07:37 +00:00
|
|
|
#endif
|
2015-05-30 14:40:13 +00:00
|
|
|
void *nvenc;
|
|
|
|
|
|
|
|
PCUINIT cu_init;
|
|
|
|
PCUDEVICEGETCOUNT cu_device_get_count;
|
|
|
|
PCUDEVICEGET cu_device_get;
|
|
|
|
PCUDEVICEGETNAME cu_device_get_name;
|
|
|
|
PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
|
|
|
|
PCUCTXCREATE cu_ctx_create;
|
|
|
|
PCUCTXPOPCURRENT cu_ctx_pop_current;
|
2017-01-13 09:53:35 +00:00
|
|
|
PCUCTXPUSHCURRENT cu_ctx_push_current;
|
2015-05-30 14:40:13 +00:00
|
|
|
PCUCTXDESTROY cu_ctx_destroy;
|
|
|
|
|
|
|
|
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
|
|
|
|
} NVENCLibraryContext;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PRESET_DEFAULT,
|
2016-09-24 15:54:58 +00:00
|
|
|
PRESET_SLOW,
|
|
|
|
PRESET_MEDIUM,
|
|
|
|
PRESET_FAST,
|
2015-05-30 14:40:13 +00:00
|
|
|
PRESET_HP,
|
|
|
|
PRESET_HQ,
|
|
|
|
PRESET_BD ,
|
|
|
|
PRESET_LOW_LATENCY_DEFAULT ,
|
|
|
|
PRESET_LOW_LATENCY_HQ ,
|
|
|
|
PRESET_LOW_LATENCY_HP,
|
|
|
|
PRESET_LOSSLESS_DEFAULT,
|
|
|
|
PRESET_LOSSLESS_HP,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
NV_ENC_H264_PROFILE_BASELINE,
|
|
|
|
NV_ENC_H264_PROFILE_MAIN,
|
|
|
|
NV_ENC_H264_PROFILE_HIGH,
|
|
|
|
NV_ENC_H264_PROFILE_HIGH_444,
|
|
|
|
NV_ENC_H264_PROFILE_CONSTRAINED_HIGH,
|
|
|
|
};
|
|
|
|
|
2016-09-24 15:54:59 +00:00
|
|
|
enum {
|
|
|
|
NV_ENC_HEVC_PROFILE_MAIN,
|
|
|
|
NV_ENC_HEVC_PROFILE_MAIN_10,
|
|
|
|
NV_ENC_HEVC_PROFILE_REXT,
|
|
|
|
};
|
|
|
|
|
2015-05-30 14:40:13 +00:00
|
|
|
enum {
|
|
|
|
NVENC_LOWLATENCY = 1,
|
2016-09-24 15:54:58 +00:00
|
|
|
NVENC_LOSSLESS = 2,
|
|
|
|
NVENC_ONE_PASS = 4,
|
|
|
|
NVENC_TWO_PASSES = 8,
|
2015-05-30 14:40:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
LIST_DEVICES = -2,
|
|
|
|
ANY_DEVICE,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct NVENCContext {
|
|
|
|
AVClass *class;
|
|
|
|
NVENCLibraryContext nvel;
|
|
|
|
|
|
|
|
NV_ENC_INITIALIZE_PARAMS params;
|
|
|
|
NV_ENC_CONFIG config;
|
|
|
|
|
|
|
|
CUcontext cu_context;
|
2016-02-01 14:56:31 +00:00
|
|
|
CUcontext cu_context_internal;
|
2015-05-30 14:40:13 +00:00
|
|
|
|
|
|
|
int nb_surfaces;
|
2016-01-30 21:06:47 +00:00
|
|
|
NVENCFrame *frames;
|
2015-05-30 14:40:13 +00:00
|
|
|
AVFifoBuffer *timestamps;
|
2017-06-24 12:14:22 +00:00
|
|
|
AVFifoBuffer *pending, *ready, *unused_surface_queue;
|
2015-05-30 14:40:13 +00:00
|
|
|
|
2016-02-01 14:56:31 +00:00
|
|
|
struct {
|
|
|
|
CUdeviceptr ptr;
|
|
|
|
NV_ENC_REGISTERED_PTR regptr;
|
|
|
|
int mapped;
|
|
|
|
} registered_frames[MAX_REGISTERED_FRAMES];
|
|
|
|
int nb_registered_frames;
|
|
|
|
|
|
|
|
/* the actual data pixel format, different from
|
|
|
|
* AVCodecContext.pix_fmt when using hwaccel frames on input */
|
|
|
|
enum AVPixelFormat data_pix_fmt;
|
|
|
|
|
2016-01-08 16:08:39 +00:00
|
|
|
/* timestamps of the first two frames, for computing the first dts
|
2016-04-27 17:45:23 +00:00
|
|
|
* when B-frames are present */
|
2016-01-08 16:08:39 +00:00
|
|
|
int64_t initial_pts[2];
|
|
|
|
int first_packet_output;
|
2015-05-30 14:40:13 +00:00
|
|
|
|
|
|
|
void *nvenc_ctx;
|
|
|
|
|
|
|
|
int preset;
|
|
|
|
int profile;
|
|
|
|
int level;
|
|
|
|
int tier;
|
|
|
|
int rc;
|
|
|
|
int device;
|
|
|
|
int flags;
|
2015-07-25 21:20:28 +00:00
|
|
|
int async_depth;
|
2016-09-24 15:55:00 +00:00
|
|
|
int rc_lookahead;
|
|
|
|
int aq;
|
|
|
|
int no_scenecut;
|
|
|
|
int b_adapt;
|
|
|
|
int temporal_aq;
|
|
|
|
int zerolatency;
|
|
|
|
int nonref_p;
|
|
|
|
int strict_gop;
|
|
|
|
int aq_strength;
|
|
|
|
int quality;
|
2017-03-07 06:32:14 +00:00
|
|
|
int init_qp_p;
|
|
|
|
int init_qp_b;
|
|
|
|
int init_qp_i;
|
2015-05-30 14:40:13 +00:00
|
|
|
} NVENCContext;
|
|
|
|
|
|
|
|
int ff_nvenc_encode_init(AVCodecContext *avctx);
|
|
|
|
|
|
|
|
int ff_nvenc_encode_close(AVCodecContext *avctx);
|
|
|
|
|
|
|
|
int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
|
|
|
|
const AVFrame *frame, int *got_packet);
|
|
|
|
|
2016-01-30 20:33:48 +00:00
|
|
|
extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
|
|
|
|
|
2015-05-30 14:40:13 +00:00
|
|
|
#endif /* AVCODEC_NVENC_H */
|