h264encapi.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Abstract : Hantro 8290 H.264 Encoder API
  17. --
  18. ------------------------------------------------------------------------------*/
  19. #ifndef __H264ENCAPI_H__
  20. #define __H264ENCAPI_H__
  21. #include "basetype.h"
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. /*------------------------------------------------------------------------------
  27. 1. Type definition for encoder instance
  28. ------------------------------------------------------------------------------*/
  29. typedef const void *H264EncInst;
  30. /*------------------------------------------------------------------------------
  31. 2. Enumerations for API parameters
  32. ------------------------------------------------------------------------------*/
  33. /* Function return values */
  34. typedef enum
  35. {
  36. H264ENC_OK = 0,
  37. H264ENC_FRAME_READY = 1,
  38. H264ENC_ERROR = -1,
  39. H264ENC_NULL_ARGUMENT = -2,
  40. H264ENC_INVALID_ARGUMENT = -3,
  41. H264ENC_MEMORY_ERROR = -4,
  42. H264ENC_EWL_ERROR = -5,
  43. H264ENC_EWL_MEMORY_ERROR = -6,
  44. H264ENC_INVALID_STATUS = -7,
  45. H264ENC_OUTPUT_BUFFER_OVERFLOW = -8,
  46. H264ENC_HW_BUS_ERROR = -9,
  47. H264ENC_HW_DATA_ERROR = -10,
  48. H264ENC_HW_TIMEOUT = -11,
  49. H264ENC_HW_RESERVED = -12,
  50. H264ENC_SYSTEM_ERROR = -13,
  51. H264ENC_INSTANCE_ERROR = -14,
  52. H264ENC_HRD_ERROR = -15,
  53. H264ENC_HW_RESET = -16
  54. } H264EncRet;
  55. /* Stream type for initialization */
  56. typedef enum
  57. {
  58. H264ENC_BYTE_STREAM = 0, /* H.264 annex B: NAL unit starts with
  59. * hex bytes '00 00 00 01' */
  60. H264ENC_NAL_UNIT_STREAM = 1 /* Plain NAL units without startcode */
  61. } H264EncStreamType;
  62. /* Stream view mode and buffer requirement for initialization */
  63. typedef enum
  64. {
  65. H264ENC_BASE_VIEW_DOUBLE_BUFFER = 0, /* H.264 stream using
  66. two reference frame buffers */
  67. H264ENC_BASE_VIEW_SINGLE_BUFFER = 1, /* H.264 stream using
  68. one reference frame buffers,
  69. HRD frame discard not possible */
  70. H264ENC_MVC_STEREO_INTER_VIEW_PRED = 2, /* H.264 MVC Stereo stream using
  71. one reference frame buffer,
  72. no HRD frame discard,
  73. second view is always
  74. inter-view predicted */
  75. H264ENC_MVC_STEREO_INTER_PRED = 3 /* H.264 MVC Stereo stream using
  76. two reference frame buffers,
  77. no HRD frame discard,
  78. second view can be inter
  79. predicted */
  80. } H264EncViewMode;
  81. /* Level for initialization */
  82. typedef enum
  83. {
  84. H264ENC_LEVEL_1 = 10,
  85. H264ENC_LEVEL_1_b = 99,
  86. H264ENC_LEVEL_1_1 = 11,
  87. H264ENC_LEVEL_1_2 = 12,
  88. H264ENC_LEVEL_1_3 = 13,
  89. H264ENC_LEVEL_2 = 20,
  90. H264ENC_LEVEL_2_1 = 21,
  91. H264ENC_LEVEL_2_2 = 22,
  92. H264ENC_LEVEL_3 = 30,
  93. H264ENC_LEVEL_3_1 = 31,
  94. H264ENC_LEVEL_3_2 = 32,
  95. H264ENC_LEVEL_4 = 40,
  96. H264ENC_LEVEL_4_1 = 41,
  97. H264ENC_LEVEL_4_2 = 42,
  98. H264ENC_LEVEL_5 = 50,
  99. H264ENC_LEVEL_5_1 = 51
  100. } H264EncLevel;
  101. /* Picture YUV type for initialization */
  102. typedef enum
  103. {
  104. H264ENC_YUV420_PLANAR = 0, /* YYYY... UUUU... VVVV */
  105. H264ENC_YUV420_SEMIPLANAR = 1, /* YYYY... UVUVUV... */
  106. H264ENC_YUV422_INTERLEAVED_YUYV = 2, /* YUYVYUYV... */
  107. H264ENC_YUV422_INTERLEAVED_UYVY = 3, /* UYVYUYVY... */
  108. H264ENC_RGB565 = 4, /* 16-bit RGB */
  109. H264ENC_BGR565 = 5, /* 16-bit RGB */
  110. H264ENC_RGB555 = 6, /* 15-bit RGB */
  111. H264ENC_BGR555 = 7, /* 15-bit RGB */
  112. H264ENC_RGB444 = 8, /* 12-bit RGB */
  113. H264ENC_BGR444 = 9, /* 12-bit RGB */
  114. H264ENC_RGB888 = 10, /* 24-bit RGB */
  115. H264ENC_BGR888 = 11, /* 24-bit RGB */
  116. H264ENC_RGB101010 = 12, /* 30-bit RGB */
  117. H264ENC_BGR101010 = 13 /* 30-bit RGB */
  118. } H264EncPictureType;
  119. /* Picture rotation for pre-processing */
  120. typedef enum
  121. {
  122. H264ENC_ROTATE_0 = 0,
  123. H264ENC_ROTATE_90R = 1, /* Rotate 90 degrees clockwise */
  124. H264ENC_ROTATE_90L = 2 /* Rotate 90 degrees counter-clockwise */
  125. } H264EncPictureRotation;
  126. /* Picture color space conversion (RGB input) for pre-processing */
  127. typedef enum
  128. {
  129. H264ENC_RGBTOYUV_BT601 = 0, /* Color conversion according to BT.601 */
  130. H264ENC_RGBTOYUV_BT709 = 1, /* Color conversion according to BT.709 */
  131. H264ENC_RGBTOYUV_USER_DEFINED = 2 /* User defined color conversion */
  132. } H264EncColorConversionType;
  133. /* Picture type for encoding */
  134. typedef enum
  135. {
  136. H264ENC_INTRA_FRAME = 0,
  137. H264ENC_PREDICTED_FRAME = 1,
  138. H264ENC_NOTCODED_FRAME /* Used just as a return value */
  139. } H264EncPictureCodingType;
  140. /*------------------------------------------------------------------------------
  141. 3. Structures for API function parameters
  142. ------------------------------------------------------------------------------*/
  143. /* Configuration info for initialization
  144. * Width and height are picture dimensions after rotation
  145. * Width and height are restricted by level limitations
  146. */
  147. typedef struct
  148. {
  149. H264EncStreamType streamType; /* Byte stream / Plain NAL units */
  150. H264EncViewMode viewMode; /* Mode of stream to be generated and
  151. the corresponding amount of encoder
  152. internal frame buffers required */
  153. /* Stream Profile will be automatically decided based on parameters:
  154. * CABAC -> Main/High Profile,
  155. * 8x8-transform -> High Profile,
  156. * MVC -> Stereo High Profile */
  157. H264EncLevel level;
  158. u32 width; /* Encoded picture width in pixels, multiple of 4 */
  159. u32 height; /* Encoded picture height in pixels, multiple of 2 */
  160. u32 frameRateNum; /* The stream time scale, [1..1048575] */
  161. u32 frameRateDenom; /* Maximum frame rate is frameRateNum/frameRateDenom
  162. * in frames/second. The actual frame rate will be
  163. * defined by timeIncrement of encoded pictures,
  164. * [1..frameRateNum] */
  165. } H264EncConfig;
  166. /* Defining rectangular macroblock area in encoder picture */
  167. typedef struct
  168. {
  169. u32 enable; /* [0,1] Enables this area */
  170. u32 top; /* Top macroblock row inside area [0..heightMbs-1] */
  171. u32 left; /* Left macroblock row inside area [0..widthMbs-1] */
  172. u32 bottom; /* Bottom macroblock row inside area [top..heightMbs-1] */
  173. u32 right; /* Right macroblock row inside area [left..widthMbs-1] */
  174. } H264EncPictureArea;
  175. /* Coding control parameters */
  176. typedef struct
  177. {
  178. u32 sliceSize; /* Slice size in macroblock rows,
  179. * 0 to encode each picture in one slice,
  180. * [0..height/16]
  181. */
  182. u32 seiMessages; /* Insert picture timing and buffering
  183. * period SEI messages into the stream,
  184. * [0,1]
  185. */
  186. u32 videoFullRange; /* Input video signal sample range, [0,1]
  187. * 0 = Y range in [16..235],
  188. * Cb&Cr range in [16..240]
  189. * 1 = Y, Cb and Cr range in [0..255]
  190. */
  191. u32 constrainedIntraPrediction; /* 0 = No constrains,
  192. * 1 = Only use intra neighbours */
  193. u32 disableDeblockingFilter; /* 0 = Filter enabled,
  194. * 1 = Filter disabled,
  195. * 2 = Filter disabled on slice edges */
  196. u32 sampleAspectRatioWidth; /* Horizontal size of the sample aspect
  197. * ratio (in arbitrary units), 0 for
  198. * unspecified, [0..65535]
  199. */
  200. u32 sampleAspectRatioHeight; /* Vertical size of the sample aspect ratio
  201. * (in same units as sampleAspectRatioWidth)
  202. * 0 for unspecified, [0..65535]
  203. */
  204. u32 enableCabac; /* 0 = CAVLC - Baseline profile,
  205. * 1 = CABAC - Main profile,
  206. * 2 = CABAC/CAVLC frame based -
  207. * Performance optimized Main profile with
  208. * Intra frames encoded using CAVLC and
  209. * Inter frames encoded using CABAC */
  210. u32 cabacInitIdc; /* [0,2] CABAC table initial value */
  211. u32 transform8x8Mode; /* Enable 8x8 transform mode, High profile
  212. * 0=disabled, 1=adaptive 8x8, 2=always 8x8 */
  213. u32 quarterPixelMv; /* 1/4 pixel motion estimation
  214. * 0=disabled, 1=adaptive, 2=enabled */
  215. u32 cirStart; /* [0..mbTotal] First macroblock for
  216. Cyclic Intra Refresh */
  217. u32 cirInterval; /* [0..mbTotal] Macroblock interval for
  218. Cyclic Intra Refresh, 0=disabled */
  219. u32 intraSliceMap1; /* Bitmap for forcing slices 0..31 to intra,
  220. LSB=slice 0, MSB=slice 31, 1=intra. */
  221. u32 intraSliceMap2; /* Bitmap for forcing slices 32..63 to intra,
  222. LSB=slice 32, MSB=slice 63, 1=intra. */
  223. u32 intraSliceMap3; /* Bitmap for forcing slices 64..95 to intra,
  224. LSB=slice 64, MSB=slice 95, 1=intra. */
  225. H264EncPictureArea intraArea; /* Area for forcing intra macroblocks */
  226. H264EncPictureArea roi1Area; /* Area for 1st Region-Of-Interest */
  227. H264EncPictureArea roi2Area; /* Area for 2nd Region-Of-Interest */
  228. i32 roi1DeltaQp; /* [-15..0] QP delta value for 1st ROI */
  229. i32 roi2DeltaQp; /* [-15..0] QP delta value for 2nd ROI */
  230. } H264EncCodingCtrl;
  231. /* Rate control parameters */
  232. typedef struct
  233. {
  234. u32 pictureRc; /* Adjust QP between pictures, [0,1] */
  235. u32 mbRc; /* Adjust QP inside picture, [0,1] */
  236. u32 pictureSkip; /* Allow rate control to skip pictures, [0,1] */
  237. i32 qpHdr; /* QP for next encoded picture, [-1..51]
  238. * -1 = Let rate control calculate initial QP
  239. * This QP is used for all pictures if
  240. * HRD and pictureRc and mbRc are disabled
  241. * If HRD is enabled it may override this QP
  242. */
  243. u32 qpMin; /* Minimum QP for any picture, [0..51] */
  244. u32 qpMax; /* Maximum QP for any picture, [0..51] */
  245. u32 bitPerSecond; /* Target bitrate in bits/second, this is
  246. * needed if pictureRc, mbRc, pictureSkip or
  247. * hrd is enabled [10000..60000000]
  248. */
  249. u32 hrd; /* Hypothetical Reference Decoder model, [0,1]
  250. * restricts the instantaneous bitrate and
  251. * total bit amount of every coded picture.
  252. * Enabling HRD will cause tight constrains
  253. * on the operation of the rate control
  254. */
  255. u32 hrdCpbSize; /* Size of Coded Picture Buffer in HRD (bits) */
  256. u32 gopLen; /* Length for Group of Pictures, indicates
  257. * the distance of two intra pictures,
  258. * including first intra [1..300]
  259. */
  260. i32 intraQpDelta; /* Intra QP delta. intraQP = QP + intraQpDelta
  261. * This can be used to change the relative quality
  262. * of the Intra pictures or to lower the size
  263. * of Intra pictures. [-12..12]
  264. */
  265. u32 fixedIntraQp; /* Fixed QP value for all Intra pictures, [0..51]
  266. * 0 = Rate control calculates intra QP.
  267. */
  268. i32 mbQpAdjustment; /* Encoder uses MAD thresholding to recognize
  269. * macroblocks with least details. This value is
  270. * used to adjust the QP of these macroblocks
  271. * increasing the subjective quality. [-8..7]
  272. */
  273. } H264EncRateCtrl;
  274. /* Encoder input structure */
  275. typedef struct
  276. {
  277. u32 busLuma; /* Bus address for input picture
  278. * planar format: luminance component
  279. * semiplanar format: luminance component
  280. * interleaved format: whole picture
  281. */
  282. u32 busChromaU; /* Bus address for input chrominance
  283. * planar format: cb component
  284. * semiplanar format: both chrominance
  285. * interleaved format: not used
  286. */
  287. u32 busChromaV; /* Bus address for input chrominance
  288. * planar format: cr component
  289. * semiplanar format: not used
  290. * interleaved format: not used
  291. */
  292. u32 timeIncrement; /* The previous picture duration in units
  293. * of 1/frameRateNum. 0 for the very first picture
  294. * and typically equal to frameRateDenom for the rest.
  295. */
  296. u32 *pOutBuf; /* Pointer to output stream buffer */
  297. u32 busOutBuf; /* Bus address of output stream buffer */
  298. u32 outBufSize; /* Size of output stream buffer in bytes */
  299. H264EncPictureCodingType codingType; /* Proposed picture coding type,
  300. * INTRA/PREDICTED
  301. */
  302. u32 busLumaStab; /* bus address of next picture to stabilize (luminance) */
  303. } H264EncIn;
  304. /* Encoder output structure */
  305. typedef struct
  306. {
  307. H264EncPictureCodingType codingType; /* Realized picture coding type,
  308. * INTRA/PREDICTED/NOTCODED
  309. */
  310. u32 streamSize; /* Size of output stream in bytes */
  311. i8 *motionVectors; /* One pixel motion vector x and y and corresponding
  312. SAD value for every macroblock.
  313. Format: mb0x mb0y mb0sadMsb mb0sadLsb mb1x .. */
  314. u32 *pNaluSizeBuf; /* Output buffer for NAL unit sizes
  315. * pNaluSizeBuf[0] = NALU 0 size in bytes
  316. * pNaluSizeBuf[1] = NALU 1 size in bytes
  317. * etc
  318. * Zero value is written after last NALU.
  319. */
  320. u32 numNalus; /* Amount of NAL units */
  321. } H264EncOut;
  322. /* Input pre-processing */
  323. typedef struct
  324. {
  325. H264EncColorConversionType type;
  326. u16 coeffA; /* User defined color conversion coefficient */
  327. u16 coeffB; /* User defined color conversion coefficient */
  328. u16 coeffC; /* User defined color conversion coefficient */
  329. u16 coeffE; /* User defined color conversion coefficient */
  330. u16 coeffF; /* User defined color conversion coefficient */
  331. } H264EncColorConversion;
  332. typedef struct
  333. {
  334. u32 origWidth;
  335. u32 origHeight;
  336. u32 xOffset;
  337. u32 yOffset;
  338. H264EncPictureType inputType;
  339. H264EncPictureRotation rotation;
  340. u32 videoStabilization;
  341. H264EncColorConversion colorConversion;
  342. } H264EncPreProcessingCfg;
  343. /* Callback struct and function type. The callback is made by the encoder
  344. * when a slice is completed and available in the encoder stream output buffer. */
  345. typedef struct
  346. {
  347. u32 slicesReadyPrev;/* Indicates how many slices were completed at
  348. previous callback. This is given because
  349. several slices can be completed between
  350. the callbacks. */
  351. u32 slicesReady; /* Indicates how many slices are completed. */
  352. u32 *sliceSizes; /* Holds the size (bytes) of every completed slice. */
  353. u32 *pOutBuf; /* Pointer to beginning of output stream buffer. */
  354. void *pAppData; /* Pointer to application data. */
  355. } H264EncSliceReady;
  356. typedef void (*H264EncSliceReadyCallBackFunc)(H264EncSliceReady *sliceReady);
  357. /* Version information */
  358. typedef struct
  359. {
  360. u32 major; /* Encoder API major version */
  361. u32 minor; /* Encoder API minor version */
  362. } H264EncApiVersion;
  363. typedef struct
  364. {
  365. u32 swBuild; /* Software build ID */
  366. u32 hwBuild; /* Hardware build ID */
  367. } H264EncBuild;
  368. /*------------------------------------------------------------------------------
  369. 4. Encoder API function prototypes
  370. ------------------------------------------------------------------------------*/
  371. /* Version information */
  372. H264EncApiVersion H264EncGetApiVersion(void);
  373. H264EncBuild H264EncGetBuild(void);
  374. /* Initialization & release */
  375. H264EncRet H264EncInit(const H264EncConfig * pEncConfig,
  376. H264EncInst * instAddr);
  377. H264EncRet H264EncRelease(H264EncInst inst);
  378. /* Encoder configuration before stream generation */
  379. H264EncRet H264EncSetCodingCtrl(H264EncInst inst, const H264EncCodingCtrl *
  380. pCodingParams);
  381. H264EncRet H264EncGetCodingCtrl(H264EncInst inst, H264EncCodingCtrl *
  382. pCodingParams);
  383. /* Encoder configuration before and during stream generation */
  384. H264EncRet H264EncSetRateCtrl(H264EncInst inst,
  385. const H264EncRateCtrl * pRateCtrl);
  386. H264EncRet H264EncGetRateCtrl(H264EncInst inst,
  387. H264EncRateCtrl * pRateCtrl);
  388. H264EncRet H264EncSetPreProcessing(H264EncInst inst,
  389. const H264EncPreProcessingCfg *
  390. pPreProcCfg);
  391. H264EncRet H264EncGetPreProcessing(H264EncInst inst,
  392. H264EncPreProcessingCfg * pPreProcCfg);
  393. /* Encoder user data insertion during stream generation */
  394. H264EncRet H264EncSetSeiUserData(H264EncInst inst, const u8 * pUserData,
  395. u32 userDataSize);
  396. /* Stream generation */
  397. /* H264EncStrmStart generates the SPS and PPS. SPS is the first NAL unit and PPS
  398. * is the second NAL unit. NaluSizeBuf indicates the size of NAL units.
  399. */
  400. H264EncRet H264EncStrmStart(H264EncInst inst, const H264EncIn * pEncIn,
  401. H264EncOut * pEncOut);
  402. /* H264EncStrmEncode encodes one video frame. If SEI messages are enabled the
  403. * first NAL unit is a SEI message. When MVC mode is selected first encoded
  404. * frame belongs to view=0 and second encoded frame belongs to view=1 and so on.
  405. * When MVC mode is selected a prefix NAL unit is generated before view=0 frames.
  406. */
  407. H264EncRet H264EncStrmEncode(H264EncInst inst, const H264EncIn * pEncIn,
  408. H264EncOut * pEncOut,
  409. H264EncSliceReadyCallBackFunc cbFunc,
  410. void * pAppData);
  411. /* H264EncStrmEnd ends a stream with an EOS code. */
  412. H264EncRet H264EncStrmEnd(H264EncInst inst, const H264EncIn * pEncIn,
  413. H264EncOut * pEncOut);
  414. /* Hantro internal encoder testing */
  415. H264EncRet H264EncSetTestId(H264EncInst inst, u32 testId);
  416. /*------------------------------------------------------------------------------
  417. 5. Encoder API tracing callback function
  418. ------------------------------------------------------------------------------*/
  419. void H264EncTrace(const char *msg);
  420. #ifdef __cplusplus
  421. }
  422. #endif
  423. #endif /*__H264ENCAPI_H__*/