1 /*
2  * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef __VPU_API_H__
17 #define __VPU_API_H__
18 
19 #include "rk_type.h"
20 
21 /**
22  * @brief rockchip media process interface
23  */
24 
25 #define VPU_API_NOPTS_VALUE          (0x8000000000000000LL)
26 
27 /*
28  * bit definition of ColorType in structure VPU_FRAME
29  */
30 #define VPU_OUTPUT_FORMAT_TYPE_MASK                 (0x0000ffff)
31 #define VPU_OUTPUT_FORMAT_ARGB8888                  (0x00000000)
32 #define VPU_OUTPUT_FORMAT_ABGR8888                  (0x00000001)
33 #define VPU_OUTPUT_FORMAT_RGB888                    (0x00000002)
34 #define VPU_OUTPUT_FORMAT_RGB565                    (0x00000003)
35 #define VPU_OUTPUT_FORMAT_RGB555                    (0x00000004)
36 #define VPU_OUTPUT_FORMAT_YUV420_SEMIPLANAR         (0x00000005)
37 #define VPU_OUTPUT_FORMAT_YUV420_PLANAR             (0x00000006)
38 #define VPU_OUTPUT_FORMAT_YUV422                    (0x00000007)
39 #define VPU_OUTPUT_FORMAT_YUV444                    (0x00000008)
40 #define VPU_OUTPUT_FORMAT_YCH420                    (0x00000009)
41 #define VPU_OUTPUT_FORMAT_BIT_MASK                  (0x000f0000)
42 #define VPU_OUTPUT_FORMAT_BIT_8                     (0x00000000)
43 #define VPU_OUTPUT_FORMAT_BIT_10                    (0x00010000)
44 #define VPU_OUTPUT_FORMAT_BIT_12                    (0x00020000)
45 #define VPU_OUTPUT_FORMAT_BIT_14                    (0x00030000)
46 #define VPU_OUTPUT_FORMAT_BIT_16                    (0x00040000)
47 #define VPU_OUTPUT_FORMAT_COLORSPACE_MASK           (0x00f00000)
48 #define VPU_OUTPUT_FORMAT_COLORSPACE_BT709          (0x00100000)
49 #define VPU_OUTPUT_FORMAT_COLORSPACE_BT2020         (0x00200000)
50 #define VPU_OUTPUT_FORMAT_DYNCRANGE_MASK            (0x0f000000)
51 #define VPU_OUTPUT_FORMAT_DYNCRANGE_SDR             (0x00000000)
52 #define VPU_OUTPUT_FORMAT_DYNCRANGE_HDR10           (0x01000000)
53 #define VPU_OUTPUT_FORMAT_DYNCRANGE_HDR_HLG         (0x02000000)
54 #define VPU_OUTPUT_FORMAT_DYNCRANGE_HDR_DOLBY       (0x03000000)
55 
56 /**
57  * @brief input picture type
58  */
59 typedef enum {
60     ENC_INPUT_YUV420_PLANAR = 0,              /**< YYYY... UUUU... VVVV */
61     ENC_INPUT_YUV420_SEMIPLANAR = 1,          /**< YYYY... UVUVUV...    */
62     ENC_INPUT_YUV422_INTERLEAVED_YUYV = 2,    /**< YUYVYUYV...          */
63     ENC_INPUT_YUV422_INTERLEAVED_UYVY = 3,    /**< UYVYUYVY...          */
64     ENC_INPUT_RGB565 = 4,                     /**< 16-bit RGB           */
65     ENC_INPUT_BGR565 = 5,                     /**< 16-bit RGB           */
66     ENC_INPUT_RGB555 = 6,                     /**< 15-bit RGB           */
67     ENC_INPUT_BGR555 = 7,                     /**< 15-bit RGB           */
68     ENC_INPUT_RGB444 = 8,                     /**< 12-bit RGB           */
69     ENC_INPUT_BGR444 = 9,                     /**< 12-bit RGB           */
70     ENC_INPUT_RGB888 = 10,                    /**< 24-bit RGB           */
71     ENC_INPUT_BGR888 = 11,                    /**< 24-bit RGB           */
72     ENC_INPUT_RGB101010 = 12,                 /**< 30-bit RGB           */
73     ENC_INPUT_BGR101010 = 13                  /**< 30-bit RGB           */
74 } EncInputPictureType;
75 
76 typedef enum VPU_API_CMD {
77     VPU_API_ENC_SETCFG,
78     VPU_API_ENC_GETCFG,
79     VPU_API_ENC_SETFORMAT,
80     VPU_API_ENC_SETIDRFRAME,
81 
82     VPU_API_ENABLE_DEINTERLACE,
83     VPU_API_SET_VPUMEM_CONTEXT,
84     VPU_API_USE_PRESENT_TIME_ORDER,
85     VPU_API_SET_DEFAULT_WIDTH_HEIGH,
86     VPU_API_SET_INFO_CHANGE,
87     VPU_API_USE_FAST_MODE,
88     VPU_API_DEC_GET_STREAM_COUNT,
89     VPU_API_GET_VPUMEM_USED_COUNT,
90     VPU_API_GET_FRAME_INFO,
91     VPU_API_SET_OUTPUT_BLOCK,
92     VPU_API_GET_EOS_STATUS,
93     VPU_API_SET_OUTPUT_MODE,
94 
95     /* get sps/pps header */
96     VPU_API_GET_EXTRA_INFO = 0x200,
97 
98     VPU_API_SET_IMMEDIATE_OUT = 0x1000,
99     VPU_API_SET_PARSER_SPLIT_MODE,          /* NOTE: should control before init */
100 
101     VPU_API_ENC_VEPU22_START = 0x2000,
102     VPU_API_ENC_SET_VEPU22_CFG,
103     VPU_API_ENC_GET_VEPU22_CFG,
104     VPU_API_ENC_SET_VEPU22_CTU_QP,
105     VPU_API_ENC_SET_VEPU22_ROI,
106 
107     /* mlvec dynamic configure */
108     VPU_API_ENC_MLVEC_CFG = 0x4000,
109     VPU_API_ENC_SET_MAX_TID,
110     VPU_API_ENC_SET_MARK_LTR,
111     VPU_API_ENC_SET_USE_LTR,
112     VPU_API_ENC_SET_FRAME_QP,
113     VPU_API_ENC_SET_BASE_LAYER_PID,
114 } VPU_API_CMD;
115 
116 typedef struct {
117     RK_U32   TimeLow;
118     RK_U32   TimeHigh;
119 } TIME_STAMP;
120 
121 typedef struct {
122     RK_U32   CodecType;
123     RK_U32   ImgWidth;
124     RK_U32   ImgHeight;
125     RK_U32   ImgHorStride;
126     RK_U32   ImgVerStride;
127     RK_U32   BufSize;
128 } VPU_GENERIC;
129 
130 typedef struct VPUMem {
131     RK_U32  phy_addr;
132     RK_U32 *vir_addr;
133     RK_U32  size;
134     RK_U32 *offset;
135 } VPUMemLinear_t;
136 
137 typedef struct tVPU_FRAME {
138     RK_U32              FrameBusAddr[2];    // 0: Y address; 1: UV address;
139     RK_U32              FrameWidth;         // buffer horizontal stride
140     RK_U32              FrameHeight;        // buffer vertical   stride
141     RK_U32              OutputWidth;        // deprecated
142     RK_U32              OutputHeight;       // deprecated
143     RK_U32              DisplayWidth;       // valid width  for display
144     RK_U32              DisplayHeight;      // valid height for display
145     RK_U32              CodingType;
146     RK_U32              FrameType;          // frame; top_field_first; bot_field_first
147     RK_U32              ColorType;
148     RK_U32              DecodeFrmNum;
149     TIME_STAMP          ShowTime;
150     RK_U32              ErrorInfo;          // error information
151     RK_U32              employ_cnt;
152     VPUMemLinear_t      vpumem;
153     struct tVPU_FRAME  *next_frame;
154     union {
155         struct {
156             RK_U32      Res0[2];
157             struct {
158                 RK_U32      ColorPrimaries : 8;
159                 RK_U32      ColorTransfer  : 8;
160                 RK_U32      ColorCoeffs    : 8;
161                 RK_U32      ColorRange     : 1;
162                 RK_U32      Res1           : 7;
163             };
164 
165             RK_U32      Res2;
166         };
167 
168         RK_U32          Res[4];
169     };
170 } VPU_FRAME;
171 
172 typedef struct VideoPacket {
173     RK_S64 pts;                /* with unit of us */
174     RK_S64 dts;                /* with unit of us */
175     RK_U8 *data;
176     RK_S32 size;
177     RK_U32 capability;
178     RK_U32 nFlags;
179 } VideoPacket_t;
180 
181 typedef struct DecoderOut {
182     RK_U8 *data;
183     RK_U32 size;
184     RK_S64 timeUs;
185     RK_S32 nFlags;
186 } DecoderOut_t;
187 
188 typedef struct ParserOut {
189     RK_U8 *data;
190     RK_U32 size;
191     RK_S64 timeUs;
192     RK_U32 nFlags;
193     RK_U32 width;
194     RK_U32 height;
195 } ParserOut_t;
196 
197 typedef struct EncInputStream {
198     RK_U8 *buf;
199     RK_S32 size;
200     RK_U32 bufPhyAddr;
201     RK_S64 timeUs;
202     RK_U32 nFlags;
203 } EncInputStream_t;
204 
205 typedef struct EncoderOut {
206     RK_U8 *data;
207     RK_S32 size;
208     RK_S64 timeUs;
209     RK_S32 keyFrame;
210 } EncoderOut_t;
211 
212 /*
213  * @brief Enumeration used to define the possible video compression codings.
214  * @note  This essentially refers to file extensions. If the coding is
215  *        being used to specify the ENCODE type, then additional work
216  *        must be done to configure the exact flavor of the compression
217  *        to be used.  For decode cases where the user application can
218  *        not differentiate between MPEG-4 and H.264 bit streams, it is
219  *        up to the codec to handle this.
220  *
221  *        sync with the omx_video.h
222  */
223 typedef enum OMX_RK_VIDEO_CODINGTYPE {
224     OMX_RK_VIDEO_CodingUnused,                          /**< Value when coding is N/A */
225     OMX_RK_VIDEO_CodingAutoDetect,                      /**< Autodetection of coding type */
226     OMX_RK_VIDEO_CodingMPEG2,                           /**< AKA: H.262 */
227     OMX_RK_VIDEO_CodingH263,                            /**< H.263 */
228     OMX_RK_VIDEO_CodingMPEG4,                           /**< MPEG-4 */
229     OMX_RK_VIDEO_CodingWMV,                             /**< Windows Media Video (WMV1,WMV2,WMV3)*/
230     OMX_RK_VIDEO_CodingRV,                              /**< all versions of Real Video */
231     OMX_RK_VIDEO_CodingAVC,                             /**< H.264/AVC */
232     OMX_RK_VIDEO_CodingMJPEG,                           /**< Motion JPEG */
233     OMX_RK_VIDEO_CodingVP8,                             /**< VP8 */
234     OMX_RK_VIDEO_CodingVP9,                             /**< VP9 */
235     OMX_RK_VIDEO_CodingVC1 = 0x01000000,                /**< Windows Media Video (WMV1,WMV2,WMV3)*/
236     OMX_RK_VIDEO_CodingFLV1,                            /**< Sorenson H.263 */
237     OMX_RK_VIDEO_CodingDIVX3,                           /**< DIVX3 */
238     OMX_RK_VIDEO_CodingVP6,
239     OMX_RK_VIDEO_CodingHEVC,                            /**< H.265/HEVC */
240     OMX_RK_VIDEO_CodingAVS,                             /**< AVS+ */
241     OMX_RK_VIDEO_CodingKhronosExtensions = 0x6F000000,  /**< Reserved region for introducing Khronos
242 							     Standard Extensions */
243     OMX_RK_VIDEO_CodingVendorStartUnused = 0x7F000000,  /**< Reserved region for introducing Vendor Extensions */
244     OMX_RK_VIDEO_CodingMax = 0x7FFFFFFF
245 } OMX_RK_VIDEO_CODINGTYPE;
246 
247 typedef enum CODEC_TYPE {
248     CODEC_NONE,
249     CODEC_DECODER,
250     CODEC_ENCODER,
251     CODEC_BUTT,
252 } CODEC_TYPE;
253 
254 typedef enum VPU_API_ERR {
255     VPU_API_OK                      = 0,
256     VPU_API_ERR_UNKNOW              = -1,
257     VPU_API_ERR_BASE                = -1000,
258     VPU_API_ERR_LIST_STREAM         = VPU_API_ERR_BASE - 1,
259     VPU_API_ERR_INIT                = VPU_API_ERR_BASE - 2,
260     VPU_API_ERR_VPU_CODEC_INIT      = VPU_API_ERR_BASE - 3,
261     VPU_API_ERR_STREAM              = VPU_API_ERR_BASE - 4,
262     VPU_API_ERR_FATAL_THREAD        = VPU_API_ERR_BASE - 5,
263     VPU_API_EOS_STREAM_REACHED      = VPU_API_ERR_BASE - 11,
264 
265     VPU_API_ERR_BUTT,
266 } VPU_API_ERR;
267 
268 typedef enum VPU_FRAME_ERR {
269     VPU_FRAME_ERR_UNKNOW           = 0x0001,
270     VPU_FRAME_ERR_UNSUPPORT        = 0x0002,
271 } VPU_FRAME_ERR;
272 
273 typedef struct EncParameter {
274     RK_S32 width;
275     RK_S32 height;
276     RK_S32 rc_mode;                 /* 0 - CQP mode; 1 - CBR mode; 2 - FIXQP mode */
277     RK_S32 bitRate;                 /* target bitrate */
278     RK_S32 framerate;
279     RK_S32 qp;
280     RK_S32 enableCabac;
281     RK_S32 cabacInitIdc;
282     RK_S32 format;
283     RK_S32 intraPicRate;
284     RK_S32 framerateout;
285     RK_S32 profileIdc;
286     RK_S32 levelIdc;
287     RK_S32 reserved[3];
288 } EncParameter_t;
289 
290 typedef struct EXtraCfg {
291     RK_S32 vc1extra_size;
292     RK_S32 vp6codeid;
293     RK_S32 tsformat;
294     RK_U32 ori_vpu; /* use origin vpu framework */
295     /* below used in decode */
296     RK_U32 mpp_mode;     /* use mpp framework */
297     RK_U32 bit_depth;    /* 8 or 10 bit */
298     RK_U32 yuv_format;   /* 0:420 1:422 2:444 */
299     RK_U32 reserved[16];
300 } EXtraCfg_t;
301 
302 /**
303  * @brief vpu function interface
304  */
305 typedef struct VpuCodecContext {
306     void* vpuApiObj;
307 
308     CODEC_TYPE codecType;
309     OMX_RK_VIDEO_CODINGTYPE videoCoding;
310 
311     RK_U32 width;
312     RK_U32 height;
313     void  *extradata;
314     RK_S32 extradata_size;
315 
316     RK_U8  enableparsing;
317 
318     RK_S32 no_thread;
319     EXtraCfg_t extra_cfg;
320 
321     void* private_data;
322 
323     /*
324      ** 1: error state(not working)  0: working
325     */
326     RK_S32 decoder_err;
327 
328     /**
329      * Allocate and initialize an VpuCodecContext.
330      *
331      * @param ctx The context of vpu api, allocated in this function.
332      * @param extraData The extra data of codec, some codecs need / can
333      *        use extradata like Huffman tables, also live VC1 codec can
334      *        use extradata to initialize itself.
335      * @param extra_size The size of extra data.
336      *
337      * @return 0 for init success, others for failure.
338      * @note check whether ctx has been allocated success after you do init.
339      */
340     RK_S32 (*init)(struct VpuCodecContext *ctx, RK_U8 *extraData, RK_U32 extra_size);
341     /**
342      * @brief both send video stream packet to decoder and get video frame from
343      *        decoder at the same time
344      * @param ctx The context of vpu codec
345      * @param pkt[in] Stream to be decoded
346      * @param aDecOut[out] Decoding frame
347      * @return 0 for decode success, others for failure.
348      */
349     RK_S32 (*decode)(struct VpuCodecContext *ctx, VideoPacket_t *pkt, DecoderOut_t *aDecOut);
350     /**
351      * @brief both send video frame to encoder and get encoded video stream from
352      *        encoder at the same time.
353      * @param ctx The context of vpu codec
354      * @param aEncInStrm[in] Frame to be encoded
355      * @param aEncOut[out] Encoding stream
356      * @return 0 for encode success, others for failure.
357      */
358     RK_S32 (*encode)(struct VpuCodecContext *ctx, EncInputStream_t *aEncInStrm, EncoderOut_t *aEncOut);
359     /**
360      * @brief flush codec while do fast forward playing.
361      * @param ctx The context of vpu codec
362      * @return 0 for flush success, others for failure.
363      */
364     RK_S32 (*flush)(struct VpuCodecContext *ctx);
365     RK_S32 (*control)(struct VpuCodecContext *ctx, VPU_API_CMD cmdType, void* param);
366     /**
367      * @brief send video stream packet to decoder only, async interface
368      * @param ctx The context of vpu codec
369      * @param pkt Stream to be decoded
370      * @return 0 for success, others for failure.
371      */
372     RK_S32 (*decode_sendstream)(struct VpuCodecContext *ctx, VideoPacket_t *pkt);
373     /**
374      * @brief get video frame from decoder only, async interface
375      * @param ctx The context of vpu codec
376      * @param aDecOut Decoding frame
377      * @return 0 for success, others for failure.
378      */
379     RK_S32 (*decode_getframe)(struct VpuCodecContext *ctx, DecoderOut_t *aDecOut);
380     /**
381      * @brief send video frame to encoder only, async interface
382      * @param ctx The context of vpu codec
383      * @param aEncInStrm Frame to be encoded
384      * @return 0 for success, others for failure.
385      */
386     RK_S32 (*encoder_sendframe)(struct VpuCodecContext *ctx, EncInputStream_t *aEncInStrm);
387     /**
388      * @brief get encoded video packet from encoder only, async interface
389      * @param ctx The context of vpu codec
390      * @param aEncOut Encoding stream
391      * @return 0 for success, others for failure.
392      */
393     RK_S32 (*encoder_getstream)(struct VpuCodecContext *ctx, EncoderOut_t *aEncOut);
394 } VpuCodecContext_t;
395 
396 /* allocated vpu codec context */
397 #ifdef __cplusplus
398 extern "C"
399 {
400 #endif
401 
402 /**
403  * @brief open context of vpu
404  * @param ctx pointer of vpu codec context
405  */
406 RK_S32 vpu_open_context(struct VpuCodecContext **ctx);
407 /**
408  * @brief close context of vpu
409  * @param ctx pointer of vpu codec context
410  */
411 RK_S32 vpu_close_context(struct VpuCodecContext **ctx);
412 
413 #ifdef __cplusplus
414 }
415 #endif
416 
417 /*
418  * vpu_mem api
419  */
420 #define vpu_display_mem_pool_FIELDS \
421     RK_S32 (*commit_hdl)(vpu_display_mem_pool *p, RK_S32 hdl, RK_S32 size); \
422     void* (*get_free)(vpu_display_mem_pool *p); \
423     RK_S32 (*inc_used)(vpu_display_mem_pool *p, void *hdl); \
424     RK_S32 (*put_used)(vpu_display_mem_pool *p, void *hdl); \
425     RK_S32 (*reset)(vpu_display_mem_pool *p); \
426     RK_S32 (*get_unused_num)(vpu_display_mem_pool *p); \
427     RK_S32 buff_size; \
428     float version; \
429     RK_S32 res[18]
430 
431 typedef struct vpu_display_mem_pool vpu_display_mem_pool;
432 
433 struct vpu_display_mem_pool {
434     vpu_display_mem_pool_FIELDS;
435 };
436 
437 #ifdef __cplusplus
438 extern "C"
439 {
440 #endif
441 
442 /*
443  * vpu memory handle interface
444  */
445 RK_S32 VPUMemJudgeIommu(void);
446 RK_S32 VPUMallocLinear(VPUMemLinear_t *p, RK_U32 size);
447 RK_S32 VPUFreeLinear(VPUMemLinear_t *p);
448 RK_S32 VPUMemDuplicate(VPUMemLinear_t *dst, VPUMemLinear_t *src);
449 RK_S32 VPUMemLink(VPUMemLinear_t *p);
450 RK_S32 VPUMemFlush(VPUMemLinear_t *p);
451 RK_S32 VPUMemClean(VPUMemLinear_t *p);
452 RK_S32 VPUMemInvalidate(VPUMemLinear_t *p);
453 RK_S32 VPUMemGetFD(VPUMemLinear_t *p);
454 RK_S32 VPUMallocLinearFromRender(VPUMemLinear_t *p, RK_U32 size, void *ctx);
455 
456 /*
457  * vpu memory allocator and manager interface
458  */
459 vpu_display_mem_pool* open_vpu_memory_pool(void);
460 void close_vpu_memory_pool(vpu_display_mem_pool *p);
461 int create_vpu_memory_pool_allocator(vpu_display_mem_pool **ipool, int num, int size);
462 void release_vpu_memory_pool_allocator(vpu_display_mem_pool *ipool);
463 
464 #ifdef __cplusplus
465 }
466 #endif
467 
468 #endif /* __VPU_API_H__ */
469