2016-05-25 15:04:28 +02:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg 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.
|
|
|
|
*
|
|
|
|
* FFmpeg 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 FFmpeg; 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
|
|
|
|
|
2016-08-27 14:39:35 +02:00
|
|
|
#include "compat/nvenc/nvEncodeAPI.h"
|
2016-05-25 15:04:28 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "libavutil/fifo.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
|
|
|
|
#if CONFIG_CUDA
|
2016-05-29 14:23:26 +02:00
|
|
|
#include "libavutil/hwcontext_cuda.h"
|
2016-05-25 15:04:28 +02:00
|
|
|
#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
|
|
|
|
|
|
|
|
#define MAX_REGISTERED_FRAMES 64
|
|
|
|
|
|
|
|
typedef struct NvencSurface
|
|
|
|
{
|
|
|
|
NV_ENC_INPUT_PTR input_surface;
|
|
|
|
AVFrame *in_ref;
|
|
|
|
NV_ENC_MAP_INPUT_RESOURCE in_map;
|
|
|
|
int reg_idx;
|
|
|
|
int width;
|
|
|
|
int height;
|
2016-09-07 18:15:22 +02:00
|
|
|
int pitch;
|
2016-05-25 15:04:28 +02:00
|
|
|
|
|
|
|
NV_ENC_OUTPUT_PTR output_surface;
|
|
|
|
NV_ENC_BUFFER_FORMAT format;
|
|
|
|
int size;
|
|
|
|
int lockCount;
|
|
|
|
} NvencSurface;
|
|
|
|
|
|
|
|
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);
|
|
|
|
typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
|
|
|
|
|
2016-08-28 19:46:44 +02:00
|
|
|
typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPIGETMAXSUPPORTEDVERSION)(uint32_t* version);
|
2016-05-25 15:04:28 +02:00
|
|
|
typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
|
|
|
|
|
|
|
|
typedef struct NvencDynLoadFunctions
|
|
|
|
{
|
|
|
|
#if !CONFIG_CUDA
|
2016-05-29 14:23:26 +02:00
|
|
|
void *cuda;
|
2016-05-25 15:04:28 +02:00
|
|
|
#endif
|
2016-05-29 14:23:26 +02:00
|
|
|
void *nvenc;
|
2016-05-25 15:04:28 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
PCUCTXDESTROY cu_ctx_destroy;
|
|
|
|
|
|
|
|
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
|
|
|
|
int nvenc_device_count;
|
|
|
|
} NvencDynLoadFunctions;
|
|
|
|
|
2016-05-25 16:05:50 +02:00
|
|
|
enum {
|
|
|
|
PRESET_DEFAULT = 0,
|
|
|
|
PRESET_SLOW,
|
|
|
|
PRESET_MEDIUM,
|
|
|
|
PRESET_FAST,
|
|
|
|
PRESET_HP,
|
|
|
|
PRESET_HQ,
|
|
|
|
PRESET_BD ,
|
|
|
|
PRESET_LOW_LATENCY_DEFAULT ,
|
|
|
|
PRESET_LOW_LATENCY_HQ ,
|
|
|
|
PRESET_LOW_LATENCY_HP,
|
|
|
|
PRESET_LOSSLESS_DEFAULT, // lossless presets must be the last ones
|
|
|
|
PRESET_LOSSLESS_HP,
|
|
|
|
};
|
|
|
|
|
2016-05-25 16:26:25 +02:00
|
|
|
enum {
|
|
|
|
NV_ENC_H264_PROFILE_BASELINE,
|
|
|
|
NV_ENC_H264_PROFILE_MAIN,
|
|
|
|
NV_ENC_H264_PROFILE_HIGH,
|
|
|
|
NV_ENC_H264_PROFILE_HIGH_444P,
|
|
|
|
};
|
|
|
|
|
2016-08-25 17:18:03 +02:00
|
|
|
enum {
|
|
|
|
NV_ENC_HEVC_PROFILE_MAIN,
|
|
|
|
NV_ENC_HEVC_PROFILE_MAIN_10,
|
|
|
|
};
|
|
|
|
|
2016-05-25 16:05:50 +02:00
|
|
|
enum {
|
|
|
|
NVENC_LOWLATENCY = 1,
|
|
|
|
NVENC_LOSSLESS = 2,
|
|
|
|
NVENC_ONE_PASS = 4,
|
|
|
|
NVENC_TWO_PASSES = 8,
|
|
|
|
};
|
|
|
|
|
2016-05-29 14:23:26 +02:00
|
|
|
enum {
|
|
|
|
LIST_DEVICES = -2,
|
|
|
|
ANY_DEVICE,
|
|
|
|
};
|
|
|
|
|
2016-05-25 15:04:28 +02:00
|
|
|
typedef struct NvencContext
|
|
|
|
{
|
|
|
|
AVClass *avclass;
|
|
|
|
|
|
|
|
NvencDynLoadFunctions nvenc_dload_funcs;
|
|
|
|
|
|
|
|
NV_ENC_INITIALIZE_PARAMS init_encode_params;
|
|
|
|
NV_ENC_CONFIG encode_config;
|
|
|
|
CUcontext cu_context;
|
|
|
|
CUcontext cu_context_internal;
|
|
|
|
|
2016-05-29 12:07:34 +02:00
|
|
|
int nb_surfaces;
|
2016-05-25 15:04:28 +02:00
|
|
|
NvencSurface *surfaces;
|
|
|
|
|
|
|
|
AVFifoBuffer *output_surface_queue;
|
|
|
|
AVFifoBuffer *output_surface_ready_queue;
|
|
|
|
AVFifoBuffer *timestamp_list;
|
|
|
|
|
|
|
|
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-05-31 18:59:35 +02:00
|
|
|
/* timestamps of the first two frames, for computing the first dts
|
|
|
|
* when B-frames are present */
|
|
|
|
int64_t initial_pts[2];
|
|
|
|
int first_packet_output;
|
2016-05-25 15:04:28 +02:00
|
|
|
|
|
|
|
void *nvencoder;
|
|
|
|
|
2016-05-25 16:05:50 +02:00
|
|
|
int preset;
|
2016-05-25 16:26:25 +02:00
|
|
|
int profile;
|
2016-05-25 17:00:52 +02:00
|
|
|
int level;
|
2016-05-25 17:06:02 +02:00
|
|
|
int tier;
|
2016-05-25 18:39:54 +02:00
|
|
|
int rc;
|
2016-05-25 15:04:28 +02:00
|
|
|
int cbr;
|
|
|
|
int twopass;
|
2016-05-29 14:23:26 +02:00
|
|
|
int device;
|
2016-05-25 16:05:50 +02:00
|
|
|
int flags;
|
2016-05-29 12:07:34 +02:00
|
|
|
int async_depth;
|
2016-08-25 17:20:03 +02:00
|
|
|
int rc_lookahead;
|
2016-05-25 15:04:28 +02: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);
|
|
|
|
|
|
|
|
extern const enum AVPixelFormat ff_nvenc_pix_fmts[];
|
|
|
|
|
|
|
|
#endif /* AVCODEC_NVENC_H */
|