nvcuvid.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * This copyright notice applies to this header file only:
  3. *
  4. * Copyright (c) 2010-2020 NVIDIA Corporation
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use,
  10. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the software, and to permit persons to whom the
  12. * software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. /********************************************************************************************************************/
  28. //! \file nvcuvid.h
  29. //! NVDECODE API provides video decoding interface to NVIDIA GPU devices.
  30. //! \date 2015-2019
  31. //! This file contains the interface constants, structure definitions and function prototypes.
  32. /********************************************************************************************************************/
  33. #if !defined(__NVCUVID_H__)
  34. #define __NVCUVID_H__
  35. #include "cuviddec.h"
  36. #if defined(__cplusplus)
  37. extern "C" {
  38. #endif /* __cplusplus */
  39. /***********************************************/
  40. //!
  41. //! High-level helper APIs for video sources
  42. //!
  43. /***********************************************/
  44. typedef void *CUvideosource;
  45. typedef void *CUvideoparser;
  46. typedef long long CUvideotimestamp;
  47. /************************************************************************/
  48. //! \enum cudaVideoState
  49. //! Video source state enums
  50. //! Used in cuvidSetVideoSourceState and cuvidGetVideoSourceState APIs
  51. /************************************************************************/
  52. typedef enum {
  53. cudaVideoState_Error = -1, /**< Error state (invalid source) */
  54. cudaVideoState_Stopped = 0, /**< Source is stopped (or reached end-of-stream) */
  55. cudaVideoState_Started = 1 /**< Source is running and delivering data */
  56. } cudaVideoState;
  57. /************************************************************************/
  58. //! \enum cudaAudioCodec
  59. //! Audio compression enums
  60. //! Used in CUAUDIOFORMAT structure
  61. /************************************************************************/
  62. typedef enum {
  63. cudaAudioCodec_MPEG1=0, /**< MPEG-1 Audio */
  64. cudaAudioCodec_MPEG2, /**< MPEG-2 Audio */
  65. cudaAudioCodec_MP3, /**< MPEG-1 Layer III Audio */
  66. cudaAudioCodec_AC3, /**< Dolby Digital (AC3) Audio */
  67. cudaAudioCodec_LPCM, /**< PCM Audio */
  68. cudaAudioCodec_AAC, /**< AAC Audio */
  69. } cudaAudioCodec;
  70. /************************************************************************************************/
  71. //! \ingroup STRUCTS
  72. //! \struct CUVIDEOFORMAT
  73. //! Video format
  74. //! Used in cuvidGetSourceVideoFormat API
  75. /************************************************************************************************/
  76. typedef struct
  77. {
  78. cudaVideoCodec codec; /**< OUT: Compression format */
  79. /**
  80. * OUT: frame rate = numerator / denominator (for example: 30000/1001)
  81. */
  82. struct {
  83. /**< OUT: frame rate numerator (0 = unspecified or variable frame rate) */
  84. unsigned int numerator;
  85. /**< OUT: frame rate denominator (0 = unspecified or variable frame rate) */
  86. unsigned int denominator;
  87. } frame_rate;
  88. unsigned char progressive_sequence; /**< OUT: 0=interlaced, 1=progressive */
  89. unsigned char bit_depth_luma_minus8; /**< OUT: high bit depth luma. E.g, 2 for 10-bitdepth, 4 for 12-bitdepth */
  90. unsigned char bit_depth_chroma_minus8; /**< OUT: high bit depth chroma. E.g, 2 for 10-bitdepth, 4 for 12-bitdepth */
  91. unsigned char min_num_decode_surfaces; /**< OUT: Minimum number of decode surfaces to be allocated for correct
  92. decoding. The client can send this value in ulNumDecodeSurfaces
  93. (in CUVIDDECODECREATEINFO structure).
  94. This guarantees correct functionality and optimal video memory
  95. usage but not necessarily the best performance, which depends on
  96. the design of the overall application. The optimal number of
  97. decode surfaces (in terms of performance and memory utilization)
  98. should be decided by experimentation for each application, but it
  99. cannot go below min_num_decode_surfaces.
  100. If this value is used for ulNumDecodeSurfaces then it must be
  101. returned to parser during sequence callback. */
  102. unsigned int coded_width; /**< OUT: coded frame width in pixels */
  103. unsigned int coded_height; /**< OUT: coded frame height in pixels */
  104. /**
  105. * area of the frame that should be displayed
  106. * typical example:
  107. * coded_width = 1920, coded_height = 1088
  108. * display_area = { 0,0,1920,1080 }
  109. */
  110. struct {
  111. int left; /**< OUT: left position of display rect */
  112. int top; /**< OUT: top position of display rect */
  113. int right; /**< OUT: right position of display rect */
  114. int bottom; /**< OUT: bottom position of display rect */
  115. } display_area;
  116. cudaVideoChromaFormat chroma_format; /**< OUT: Chroma format */
  117. unsigned int bitrate; /**< OUT: video bitrate (bps, 0=unknown) */
  118. /**
  119. * OUT: Display Aspect Ratio = x:y (4:3, 16:9, etc)
  120. */
  121. struct {
  122. int x;
  123. int y;
  124. } display_aspect_ratio;
  125. /**
  126. * Video Signal Description
  127. * Refer section E.2.1 (VUI parameters semantics) of H264 spec file
  128. */
  129. struct {
  130. unsigned char video_format : 3; /**< OUT: 0-Component, 1-PAL, 2-NTSC, 3-SECAM, 4-MAC, 5-Unspecified */
  131. unsigned char video_full_range_flag : 1; /**< OUT: indicates the black level and luma and chroma range */
  132. unsigned char reserved_zero_bits : 4; /**< Reserved bits */
  133. unsigned char color_primaries; /**< OUT: chromaticity coordinates of source primaries */
  134. unsigned char transfer_characteristics; /**< OUT: opto-electronic transfer characteristic of the source picture */
  135. unsigned char matrix_coefficients; /**< OUT: used in deriving luma and chroma signals from RGB primaries */
  136. } video_signal_description;
  137. unsigned int seqhdr_data_length; /**< OUT: Additional bytes following (CUVIDEOFORMATEX) */
  138. } CUVIDEOFORMAT;
  139. /****************************************************************/
  140. //! \ingroup STRUCTS
  141. //! \struct CUVIDEOFORMATEX
  142. //! Video format including raw sequence header information
  143. //! Used in cuvidGetSourceVideoFormat API
  144. /****************************************************************/
  145. typedef struct
  146. {
  147. CUVIDEOFORMAT format; /**< OUT: CUVIDEOFORMAT structure */
  148. unsigned char raw_seqhdr_data[1024]; /**< OUT: Sequence header data */
  149. } CUVIDEOFORMATEX;
  150. /****************************************************************/
  151. //! \ingroup STRUCTS
  152. //! \struct CUAUDIOFORMAT
  153. //! Audio formats
  154. //! Used in cuvidGetSourceAudioFormat API
  155. /****************************************************************/
  156. typedef struct
  157. {
  158. cudaAudioCodec codec; /**< OUT: Compression format */
  159. unsigned int channels; /**< OUT: number of audio channels */
  160. unsigned int samplespersec; /**< OUT: sampling frequency */
  161. unsigned int bitrate; /**< OUT: For uncompressed, can also be used to determine bits per sample */
  162. unsigned int reserved1; /**< Reserved for future use */
  163. unsigned int reserved2; /**< Reserved for future use */
  164. } CUAUDIOFORMAT;
  165. /***************************************************************/
  166. //! \enum CUvideopacketflags
  167. //! Data packet flags
  168. //! Used in CUVIDSOURCEDATAPACKET structure
  169. /***************************************************************/
  170. typedef enum {
  171. CUVID_PKT_ENDOFSTREAM = 0x01, /**< Set when this is the last packet for this stream */
  172. CUVID_PKT_TIMESTAMP = 0x02, /**< Timestamp is valid */
  173. CUVID_PKT_DISCONTINUITY = 0x04, /**< Set when a discontinuity has to be signalled */
  174. CUVID_PKT_ENDOFPICTURE = 0x08, /**< Set when the packet contains exactly one frame or one field */
  175. CUVID_PKT_NOTIFY_EOS = 0x10, /**< If this flag is set along with CUVID_PKT_ENDOFSTREAM, an additional (dummy)
  176. display callback will be invoked with null value of CUVIDPARSERDISPINFO which
  177. should be interpreted as end of the stream. */
  178. } CUvideopacketflags;
  179. /*****************************************************************************/
  180. //! \ingroup STRUCTS
  181. //! \struct CUVIDSOURCEDATAPACKET
  182. //! Data Packet
  183. //! Used in cuvidParseVideoData API
  184. //! IN for cuvidParseVideoData
  185. /*****************************************************************************/
  186. typedef struct _CUVIDSOURCEDATAPACKET
  187. {
  188. unsigned long flags; /**< IN: Combination of CUVID_PKT_XXX flags */
  189. unsigned long payload_size; /**< IN: number of bytes in the payload (may be zero if EOS flag is set) */
  190. const unsigned char *payload; /**< IN: Pointer to packet payload data (may be NULL if EOS flag is set) */
  191. CUvideotimestamp timestamp; /**< IN: Presentation time stamp (10MHz clock), only valid if
  192. CUVID_PKT_TIMESTAMP flag is set */
  193. } CUVIDSOURCEDATAPACKET;
  194. // Callback for packet delivery
  195. typedef int (CUDAAPI *PFNVIDSOURCECALLBACK)(void *, CUVIDSOURCEDATAPACKET *);
  196. /**************************************************************************************************************************/
  197. //! \ingroup STRUCTS
  198. //! \struct CUVIDSOURCEPARAMS
  199. //! Describes parameters needed in cuvidCreateVideoSource API
  200. //! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported
  201. //! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed.
  202. /**************************************************************************************************************************/
  203. typedef struct _CUVIDSOURCEPARAMS
  204. {
  205. unsigned int ulClockRate; /**< IN: Time stamp units in Hz (0=default=10000000Hz) */
  206. unsigned int uReserved1[7]; /**< Reserved for future use - set to zero */
  207. void *pUserData; /**< IN: User private data passed in to the data handlers */
  208. PFNVIDSOURCECALLBACK pfnVideoDataHandler; /**< IN: Called to deliver video packets */
  209. PFNVIDSOURCECALLBACK pfnAudioDataHandler; /**< IN: Called to deliver audio packets. */
  210. void *pvReserved2[8]; /**< Reserved for future use - set to NULL */
  211. } CUVIDSOURCEPARAMS;
  212. /**********************************************/
  213. //! \ingroup ENUMS
  214. //! \enum CUvideosourceformat_flags
  215. //! CUvideosourceformat_flags
  216. //! Used in cuvidGetSourceVideoFormat API
  217. /**********************************************/
  218. typedef enum {
  219. CUVID_FMT_EXTFORMATINFO = 0x100 /**< Return extended format structure (CUVIDEOFORMATEX) */
  220. } CUvideosourceformat_flags;
  221. #if !defined(__APPLE__)
  222. /***************************************************************************************************************************/
  223. //! \ingroup FUNCTS
  224. //! \fn CUresult CUDAAPI cuvidCreateVideoSource(CUvideosource *pObj, const char *pszFileName, CUVIDSOURCEPARAMS *pParams)
  225. //! Create CUvideosource object. CUvideosource spawns demultiplexer thread that provides two callbacks:
  226. //! pfnVideoDataHandler() and pfnAudioDataHandler()
  227. //! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported
  228. //! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed.
  229. /***************************************************************************************************************************/
  230. CUresult CUDAAPI cuvidCreateVideoSource(CUvideosource *pObj, const char *pszFileName, CUVIDSOURCEPARAMS *pParams);
  231. /***************************************************************************************************************************/
  232. //! \ingroup FUNCTS
  233. //! \fn CUresult CUDAAPI cuvidCreateVideoSourceW(CUvideosource *pObj, const wchar_t *pwszFileName, CUVIDSOURCEPARAMS *pParams)
  234. //! Create video source
  235. /***************************************************************************************************************************/
  236. CUresult CUDAAPI cuvidCreateVideoSourceW(CUvideosource *pObj, const wchar_t *pwszFileName, CUVIDSOURCEPARAMS *pParams);
  237. /********************************************************************/
  238. //! \ingroup FUNCTS
  239. //! \fn CUresult CUDAAPI cuvidDestroyVideoSource(CUvideosource obj)
  240. //! Destroy video source
  241. /********************************************************************/
  242. CUresult CUDAAPI cuvidDestroyVideoSource(CUvideosource obj);
  243. /******************************************************************************************/
  244. //! \ingroup FUNCTS
  245. //! \fn CUresult CUDAAPI cuvidSetVideoSourceState(CUvideosource obj, cudaVideoState state)
  246. //! Set video source state to:
  247. //! cudaVideoState_Started - to signal the source to run and deliver data
  248. //! cudaVideoState_Stopped - to stop the source from delivering the data
  249. //! cudaVideoState_Error - invalid source
  250. /******************************************************************************************/
  251. CUresult CUDAAPI cuvidSetVideoSourceState(CUvideosource obj, cudaVideoState state);
  252. /******************************************************************************************/
  253. //! \ingroup FUNCTS
  254. //! \fn cudaVideoState CUDAAPI cuvidGetVideoSourceState(CUvideosource obj)
  255. //! Get video source state
  256. //! Returns:
  257. //! cudaVideoState_Started - if Source is running and delivering data
  258. //! cudaVideoState_Stopped - if Source is stopped or reached end-of-stream
  259. //! cudaVideoState_Error - if Source is in error state
  260. /******************************************************************************************/
  261. cudaVideoState CUDAAPI cuvidGetVideoSourceState(CUvideosource obj);
  262. /******************************************************************************************************************/
  263. //! \ingroup FUNCTS
  264. //! \fn CUresult CUDAAPI cuvidGetSourceVideoFormat(CUvideosource obj, CUVIDEOFORMAT *pvidfmt, unsigned int flags)
  265. //! Gets video source format in pvidfmt, flags is set to combination of CUvideosourceformat_flags as per requirement
  266. /******************************************************************************************************************/
  267. CUresult CUDAAPI cuvidGetSourceVideoFormat(CUvideosource obj, CUVIDEOFORMAT *pvidfmt, unsigned int flags);
  268. /**************************************************************************************************************************/
  269. //! \ingroup FUNCTS
  270. //! \fn CUresult CUDAAPI cuvidGetSourceAudioFormat(CUvideosource obj, CUAUDIOFORMAT *paudfmt, unsigned int flags)
  271. //! Get audio source format
  272. //! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported
  273. //! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed.
  274. /**************************************************************************************************************************/
  275. CUresult CUDAAPI cuvidGetSourceAudioFormat(CUvideosource obj, CUAUDIOFORMAT *paudfmt, unsigned int flags);
  276. #endif
  277. /**********************************************************************************/
  278. //! \ingroup STRUCTS
  279. //! \struct CUVIDPARSERDISPINFO
  280. //! Used in cuvidParseVideoData API with PFNVIDDISPLAYCALLBACK pfnDisplayPicture
  281. /**********************************************************************************/
  282. typedef struct _CUVIDPARSERDISPINFO
  283. {
  284. int picture_index; /**< OUT: Index of the current picture */
  285. int progressive_frame; /**< OUT: 1 if progressive frame; 0 otherwise */
  286. int top_field_first; /**< OUT: 1 if top field is displayed first; 0 otherwise */
  287. int repeat_first_field; /**< OUT: Number of additional fields (1=ivtc, 2=frame doubling, 4=frame tripling,
  288. -1=unpaired field) */
  289. CUvideotimestamp timestamp; /**< OUT: Presentation time stamp */
  290. } CUVIDPARSERDISPINFO;
  291. /***********************************************************************************************************************/
  292. //! Parser callbacks
  293. //! The parser will call these synchronously from within cuvidParseVideoData(), whenever there is sequence change or a picture
  294. //! is ready to be decoded and/or displayed. First argument in functions is "void *pUserData" member of structure CUVIDSOURCEPARAMS
  295. //! Return values from these callbacks are interpreted as below. If the callbacks return failure, it will be propagated by
  296. //! cuvidParseVideoData() to the application.
  297. //! PFNVIDSEQUENCECALLBACK : 0: fail, 1: succeeded, > 1: override dpb size of parser (set by CUVIDPARSERPARAMS::ulMaxNumDecodeSurfaces
  298. //! while creating parser)
  299. //! PFNVIDDECODECALLBACK : 0: fail, >=1: succeeded
  300. //! PFNVIDDISPLAYCALLBACK : 0: fail, >=1: succeeded
  301. /***********************************************************************************************************************/
  302. typedef int (CUDAAPI *PFNVIDSEQUENCECALLBACK)(void *, CUVIDEOFORMAT *);
  303. typedef int (CUDAAPI *PFNVIDDECODECALLBACK)(void *, CUVIDPICPARAMS *);
  304. typedef int (CUDAAPI *PFNVIDDISPLAYCALLBACK)(void *, CUVIDPARSERDISPINFO *);
  305. /**************************************/
  306. //! \ingroup STRUCTS
  307. //! \struct CUVIDPARSERPARAMS
  308. //! Used in cuvidCreateVideoParser API
  309. /**************************************/
  310. typedef struct _CUVIDPARSERPARAMS
  311. {
  312. cudaVideoCodec CodecType; /**< IN: cudaVideoCodec_XXX */
  313. unsigned int ulMaxNumDecodeSurfaces; /**< IN: Max # of decode surfaces (parser will cycle through these) */
  314. unsigned int ulClockRate; /**< IN: Timestamp units in Hz (0=default=10000000Hz) */
  315. unsigned int ulErrorThreshold; /**< IN: % Error threshold (0-100) for calling pfnDecodePicture (100=always
  316. IN: call pfnDecodePicture even if picture bitstream is fully corrupted) */
  317. unsigned int ulMaxDisplayDelay; /**< IN: Max display queue delay (improves pipelining of decode with display)
  318. 0=no delay (recommended values: 2..4) */
  319. unsigned int uReserved1[5]; /**< IN: Reserved for future use - set to 0 */
  320. void *pUserData; /**< IN: User data for callbacks */
  321. PFNVIDSEQUENCECALLBACK pfnSequenceCallback; /**< IN: Called before decoding frames and/or whenever there is a fmt change */
  322. PFNVIDDECODECALLBACK pfnDecodePicture; /**< IN: Called when a picture is ready to be decoded (decode order) */
  323. PFNVIDDISPLAYCALLBACK pfnDisplayPicture; /**< IN: Called whenever a picture is ready to be displayed (display order) */
  324. void *pvReserved2[7]; /**< Reserved for future use - set to NULL */
  325. CUVIDEOFORMATEX *pExtVideoInfo; /**< IN: [Optional] sequence header data from system layer */
  326. } CUVIDPARSERPARAMS;
  327. /************************************************************************************************/
  328. //! \ingroup FUNCTS
  329. //! \fn CUresult CUDAAPI cuvidCreateVideoParser(CUvideoparser *pObj, CUVIDPARSERPARAMS *pParams)
  330. //! Create video parser object and initialize
  331. /************************************************************************************************/
  332. CUresult CUDAAPI cuvidCreateVideoParser(CUvideoparser *pObj, CUVIDPARSERPARAMS *pParams);
  333. /************************************************************************************************/
  334. //! \ingroup FUNCTS
  335. //! \fn CUresult CUDAAPI cuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET *pPacket)
  336. //! Parse the video data from source data packet in pPacket
  337. //! Extracts parameter sets like SPS, PPS, bitstream etc. from pPacket and
  338. //! calls back pfnDecodePicture with CUVIDPICPARAMS data for kicking of HW decoding
  339. //! calls back pfnSequenceCallback with CUVIDEOFORMAT data for initial sequence header or when
  340. //! the decoder encounters a video format change
  341. //! calls back pfnDisplayPicture with CUVIDPARSERDISPINFO data to display a video frame
  342. /************************************************************************************************/
  343. CUresult CUDAAPI cuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET *pPacket);
  344. /************************************************************************************************/
  345. //! \ingroup FUNCTS
  346. //! \fn CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj)
  347. //! Destroy the video parser
  348. /************************************************************************************************/
  349. CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj);
  350. /**********************************************************************************************/
  351. #if defined(__cplusplus)
  352. }
  353. #endif /* __cplusplus */
  354. #endif // __NVCUVID_H__