xref: /aosp_15_r20/external/libopenapv/inc/oapv.h (revision abb65b4b03b69e1d508d4d9a44dcf199df16e7c3)
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the copyright owner, nor the names of its contributors
16  *   may be used to endorse or promote products derived from this software
17  *   without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef __OAPV_H__3342320849320483827648324783920483920432847382948__
33 #define __OAPV_H__3342320849320483827648324783920483920432847382948__
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #ifdef OAPV_STATIC_DEFINE
40 #define OAPV_EXPORT
41 #else
42 #include <oapv/oapv_exports.h>
43 #endif
44 
45 
46 /* size of macroblock */
47 #define OAPV_LOG2_MB                    (4)
48 #define OAPV_LOG2_MB_W                  (4)
49 #define OAPV_LOG2_MB_H                  (4)
50 #define OAPV_MB_W                       (1 << OAPV_LOG2_MB_W)
51 #define OAPV_MB_H                       (1 << OAPV_LOG2_MB_H)
52 #define OAPV_MB_D                       (OAPV_MB_W * OAPV_MB_H)
53 
54 /* size of block */
55 #define OAPV_LOG2_BLK                   (3)
56 #define OAPV_LOG2_BLK_W                 (3)
57 #define OAPV_LOG2_BLK_H                 (3)
58 #define OAPV_BLK_W                      (1 << OAPV_LOG2_BLK)
59 #define OAPV_BLK_H                      (1 << OAPV_LOG2_BLK)
60 #define OAPV_BLK_D                      (OAPV_BLK_W * OAPV_BLK_H)
61 
62 /* maximum number of thread */
63 #define OAPV_MAX_THREADS                (32)
64 
65 /*****************************************************************************
66  * return values and error code
67  *****************************************************************************/
68 #define OAPV_OK                         (0)
69 #define OAPV_ERR                        (-1) /* generic error */
70 #define OAPV_ERR_INVALID_ARGUMENT       (-101)
71 #define OAPV_ERR_OUT_OF_MEMORY          (-102)
72 #define OAPV_ERR_REACHED_MAX            (-103)
73 #define OAPV_ERR_UNSUPPORTED            (-104)
74 #define OAPV_ERR_UNEXPECTED             (-105)
75 #define OAPV_ERR_UNSUPPORTED_COLORSPACE (-201)
76 #define OAPV_ERR_MALFORMED_BITSTREAM    (-202)
77 #define OAPV_ERR_OUT_OF_BS_BUF          (-203) /* too small bitstream buffer */
78 #define OAPV_ERR_NOT_FOUND              (-204)
79 #define OAPV_ERR_FAILED_SYSCALL         (-301)   /* failed system call */
80 #define OAPV_ERR_UNKNOWN                (-32767) /* unknown error */
81 
82 /* return value checking */
83 #define OAPV_SUCCEEDED(ret)             ((ret) >= OAPV_OK)
84 #define OAPV_FAILED(ret)                ((ret) < OAPV_OK)
85 
86 /*****************************************************************************
87  * color spaces
88  * - value format = (endian << 14) | (bit-depth << 8) | (color format)
89  * - endian (1bit): little endian = 0, big endian = 1
90  * - bit-depth (6bit): 0~63
91  * - color format (8bit): 0~255
92  *****************************************************************************/
93 /* color formats */
94 #define OAPV_CF_UNKNOWN                 (0)  /* unknown color format */
95 #define OAPV_CF_YCBCR400                (10) /* Y only */
96 #define OAPV_CF_YCBCR420                (11) /* YCbCr 420 */
97 #define OAPV_CF_YCBCR422                (12) /* YCBCR 422 narrow chroma*/
98 #define OAPV_CF_YCBCR444                (13) /* YCBCR 444*/
99 #define OAPV_CF_YCBCR4444               (14) /* YCBCR 4444*/
100 #define OAPV_CF_YCBCR422N               OAPV_CF_YCBCR422
101 #define OAPV_CF_YCBCR422W               (18) /* YCBCR422 wide chroma */
102 #define OAPV_CF_PLANAR2                 (20) /* Planar Y, Combined CB-CR, 422 */
103 
104 /* macro for color space */
105 #define OAPV_CS_GET_FORMAT(cs)          (((cs) >> 0) & 0xFF)
106 #define OAPV_CS_GET_BIT_DEPTH(cs)       (((cs) >> 8) & 0x3F)
107 #define OAPV_CS_GET_BYTE_DEPTH(cs)      ((OAPV_CS_GET_BIT_DEPTH(cs) + 7) >> 3)
108 #define OAPV_CS_GET_ENDIAN(cs)          (((cs) >> 14) & 0x1)
109 #define OAPV_CS_SET(f, bit, e)          (((e) << 14) | ((bit) << 8) | (f))
110 #define OAPV_CS_SET_FORMAT(cs, v)       (((cs) & ~0xFF) | ((v) << 0))
111 #define OAPV_CS_SET_BIT_DEPTH(cs, v)    (((cs) & ~(0x3F << 8)) | ((v) << 8))
112 #define OAPV_CS_SET_ENDIAN(cs, v)       (((cs) & ~(0x1 << 14)) | ((v) << 14))
113 
114 /* pre-defined color spaces */
115 #define OAPV_CS_UNKNOWN                 OAPV_CS_SET(0, 0, 0)
116 #define OAPV_CS_YCBCR400                OAPV_CS_SET(OAPV_CF_YCBCR400, 8, 0)
117 #define OAPV_CS_YCBCR420                OAPV_CS_SET(OAPV_CF_YCBCR420, 8, 0)
118 #define OAPV_CS_YCBCR422                OAPV_CS_SET(OAPV_CF_YCBCR422, 8, 0)
119 #define OAPV_CS_YCBCR444                OAPV_CS_SET(OAPV_CF_YCBCR444, 8, 0)
120 #define OAPV_CS_YCBCR4444               OAPV_CS_SET(OAPV_CF_YCBCR4444, 8, 0)
121 #define OAPV_CS_YCBCR400_10LE           OAPV_CS_SET(OAPV_CF_YCBCR400, 10, 0)
122 #define OAPV_CS_YCBCR420_10LE           OAPV_CS_SET(OAPV_CF_YCBCR420, 10, 0)
123 #define OAPV_CS_YCBCR422_10LE           OAPV_CS_SET(OAPV_CF_YCBCR422, 10, 0)
124 #define OAPV_CS_YCBCR444_10LE           OAPV_CS_SET(OAPV_CF_YCBCR444, 10, 0)
125 #define OAPV_CS_YCBCR4444_10LE          OAPV_CS_SET(OAPV_CF_YCBCR4444, 10, 0)
126 #define OAPV_CS_YCBCR400_12LE           OAPV_CS_SET(OAPV_CF_YCBCR400, 12, 0)
127 #define OAPV_CS_YCBCR420_12LE           OAPV_CS_SET(OAPV_CF_YCBCR420, 12, 0)
128 #define OAPV_CS_YCBCR400_14LE           OAPV_CS_SET(OAPV_CF_YCBCR400, 14, 0)
129 #define OAPV_CS_YCBCR420_14LE           OAPV_CS_SET(OAPV_CF_YCBCR420, 14, 0)
130 #define OAPV_CS_P210                    OAPV_CS_SET(OAPV_CF_PLANAR2, 10, 0)
131 
132 /* max number of color channel: YCbCr4444 -> 4 channels */
133 #define OAPV_MAX_CC                     (4)
134 
135 /*****************************************************************************
136  * config types
137  *****************************************************************************/
138 #define OAPV_CFG_SET_QP                 (201)
139 #define OAPV_CFG_SET_BPS                (202)
140 #define OAPV_CFG_SET_FPS_NUM            (204)
141 #define OAPV_CFG_SET_FPS_DEN            (205)
142 #define OAPV_CFG_SET_QP_MIN             (208)
143 #define OAPV_CFG_SET_QP_MAX             (209)
144 #define OAPV_CFG_SET_USE_FRM_HASH       (301)
145 #define OAPV_CFG_GET_QP_MIN             (600)
146 #define OAPV_CFG_GET_QP_MAX             (601)
147 #define OAPV_CFG_GET_QP                 (602)
148 #define OAPV_CFG_GET_RCT                (603)
149 #define OAPV_CFG_GET_BPS                (604)
150 #define OAPV_CFG_GET_FPS_NUM            (605)
151 #define OAPV_CFG_GET_FPS_DEN            (606)
152 #define OAPV_CFG_GET_WIDTH              (701)
153 #define OAPV_CFG_GET_HEIGHT             (702)
154 
155 /*****************************************************************************
156  * HLS configs
157  *****************************************************************************/
158 #define OAPV_MAX_GRP_SIZE               ((1 << 16) - 1) // 0xFFFF reserved
159 
160 /*****************************************************************************
161  * PBU types
162  *****************************************************************************/
163 #define OAPV_PBU_TYPE_RESERVED          (0)
164 #define OAPV_PBU_TYPE_PRIMARY_FRAME     (1)
165 #define OAPV_PBU_TYPE_NON_PRIMARY_FRAME (2)
166 #define OAPV_PBU_TYPE_PREVIEW_FRAME     (25)
167 #define OAPV_PBU_TYPE_DEPTH_FRAME       (26)
168 #define OAPV_PBU_TYPE_ALPHA_FRAME       (27)
169 #define OAPV_PBU_TYPE_AU_INFO           (65)
170 #define OAPV_PBU_TYPE_METADATA          (66)
171 #define OAPV_PBU_TYPE_FILLER            (67)
172 #define OAPV_PBU_TYPE_UNKNOWN           (-1)
173 #define OAPV_PBU_NUMS                   (10)
174 
175 #define OAPV_PBU_FRAME_TYPE_NUM         (5)
176 
177 /*****************************************************************************
178  * metadata types
179  *****************************************************************************/
180 #define OAPV_METADATA_ITU_T_T35         (4)
181 #define OAPV_METADATA_MDCV              (5)
182 #define OAPV_METADATA_CLL               (6)
183 #define OAPV_METADATA_FILLER            (10)
184 #define OAPV_METADATA_USER_DEFINED      (170)
185 
186 /*****************************************************************************
187  * profiles
188  *****************************************************************************/
189 #define OAPV_PROFILE_422_10             (33)
190 
191 /*****************************************************************************
192  * optimization level control
193  *****************************************************************************/
194 #define OAPV_PRESET_FASTEST             (0)
195 #define OAPV_PRESET_FAST                (1)
196 #define OAPV_PRESET_MEDIUM              (2)
197 #define OAPV_PRESET_SLOW                (3)
198 #define OAPV_PRESET_PLACEBO             (4)
199 #define OAPV_PRESET_DEFAULT             OAPV_PRESET_MEDIUM
200 
201 /*****************************************************************************
202  * rate-control types
203  *****************************************************************************/
204 #define OAPV_RC_CQP                     (0)
205 #define OAPV_RC_ABR                     (1)
206 
207 /*****************************************************************************
208  * type and macro for media time
209  *****************************************************************************/
210 typedef long long        oapv_mtime_t; /* in 100-nanosec unit */
211 
212 /*****************************************************************************
213  * image buffer format
214  *
215  *    baddr
216  *     +---------------------------------------------------+ ---
217  *     |                                                   |  ^
218  *     |                                              |    |  |
219  *     |      a                                       v    |  |
220  *     |   --- +-----------------------------------+ ---   |  |
221  *     |    ^  |  (x, y)                           |  y    |  |
222  *     |    |  |   +---------------------------+   + ---   |  |
223  *     |    |  |   |                           |   |  ^    |  |
224  *     |    |  |   |            /\             |   |  |    |  |
225  *     |    |  |   |           /  \            |   |  |    |  |
226  *     |    |  |   |          /    \           |   |  |    |  |
227  *     |       |   |  +--------------------+   |   |       |
228  *     |    ah |   |   \                  /    |   |  h    |  e
229  *     |       |   |    +----------------+     |   |       |
230  *     |    |  |   |       |          |        |   |  |    |  |
231  *     |    |  |   |      @    O   O   @       |   |  |    |  |
232  *     |    |  |   |        \    ~   /         |   |  v    |  |
233  *     |    |  |   +---------------------------+   | ---   |  |
234  *     |    v  |                                   |       |  |
235  *     |   --- +---+-------------------------------+       |  |
236  *     |     ->| x |<----------- w ----------->|           |  |
237  *     |       |<--------------- aw -------------->|       |  |
238  *     |                                                   |  v
239  *     +---------------------------------------------------+ ---
240  *
241  *     |<---------------------- s ------------------------>|
242  *
243  * - x, y, w, aw, h, ah : unit of pixel
244  * - s, e : unit of byte
245  *****************************************************************************/
246 
247 typedef struct oapv_imgb oapv_imgb_t;
248 struct oapv_imgb {
249     int           cs; /* color space */
250     int           np; /* number of plane */
251     /* width (in unit of pixel) */
252     int           w[OAPV_MAX_CC];
253     /* height (in unit of pixel) */
254     int           h[OAPV_MAX_CC];
255     /* X position of left top (in unit of pixel) */
256     int           x[OAPV_MAX_CC];
257     /* Y postion of left top (in unit of pixel) */
258     int           y[OAPV_MAX_CC];
259     /* buffer stride (in unit of byte) */
260     int           s[OAPV_MAX_CC];
261     /* buffer elevation (in unit of byte) */
262     int           e[OAPV_MAX_CC];
263     /* address of each plane */
264     void         *a[OAPV_MAX_CC];
265 
266     /* hash data for signature */
267     unsigned char hash[OAPV_MAX_CC][16];
268 
269     /* time-stamps */
270     oapv_mtime_t  ts[4];
271 
272     int           ndata[4]; /* arbitrary data, if needs */
273     void         *pdata[4]; /* arbitrary adedress if needs */
274 
275     /* aligned width (in unit of pixel) */
276     int           aw[OAPV_MAX_CC];
277     /* aligned height (in unit of pixel) */
278     int           ah[OAPV_MAX_CC];
279 
280     /* left padding size (in unit of pixel) */
281     int           padl[OAPV_MAX_CC];
282     /* right padding size (in unit of pixel) */
283     int           padr[OAPV_MAX_CC];
284     /* up padding size (in unit of pixel) */
285     int           padu[OAPV_MAX_CC];
286     /* bottom padding size (in unit of pixel) */
287     int           padb[OAPV_MAX_CC];
288 
289     /* address of actual allocated buffer */
290     void         *baddr[OAPV_MAX_CC];
291     /* actual allocated buffer size */
292     int           bsize[OAPV_MAX_CC];
293 
294     /* life cycle management */
295     int           refcnt;
296     int (*addref)(oapv_imgb_t *imgb);
297     int (*getref)(oapv_imgb_t *imgb);
298     int (*release)(oapv_imgb_t *imgb);
299 };
300 
301 typedef struct oapv_frm oapv_frm_t;
302 struct oapv_frm {
303     oapv_imgb_t *imgb;
304     int          pbu_type;
305     int          group_id;
306 };
307 
308 #define OAPV_MAX_NUM_FRAMES (16) // max number of frames in an access unit
309 #define OAPV_MAX_NUM_METAS  (16) // max number of metadata in an access unit
310 
311 typedef struct oapv_frms oapv_frms_t;
312 struct oapv_frms {
313     int        num_frms;                 // number of frames
314     oapv_frm_t frm[OAPV_MAX_NUM_FRAMES]; // container of frames
315 };
316 
317 /*****************************************************************************
318  * Bitstream buffer
319  *****************************************************************************/
320 typedef struct oapv_bitb oapv_bitb_t;
321 struct oapv_bitb {
322     /* user space address indicating buffer */
323     void        *addr;
324     /* physical address indicating buffer, if any */
325     void        *pddr;
326     /* byte size of buffer memory */
327     int          bsize;
328     /* byte size of bitstream in buffer */
329     int          ssize;
330     /* bitstream has an error? */
331     int          err;
332     /* arbitrary data, if needs */
333     int          ndata[4];
334     /* arbitrary address, if needs */
335     void        *pdata[4];
336     /* time-stamps */
337     oapv_mtime_t ts[4];
338 };
339 
340 /*****************************************************************************
341  * brief information of frame
342  *****************************************************************************/
343 typedef struct oapv_frm_info oapv_frm_info_t;
344 struct oapv_frm_info {
345     int w;
346     int h;
347     int cs;
348     int pbu_type;
349     int group_id;
350     int profile_idc;
351     int level_idc;
352     int band_idc;
353     int chroma_format_idc;
354     int bit_depth;
355     int capture_time_distance;
356 };
357 
358 typedef struct oapv_au_info oapv_au_info_t;
359 struct oapv_au_info {
360     int             num_frms; // number of frames
361     oapv_frm_info_t frm_info[OAPV_MAX_NUM_FRAMES];
362 };
363 
364 /*****************************************************************************
365  * coding parameters
366  *****************************************************************************/
367 typedef struct oapve_param oapve_param_t;
368 struct oapve_param {
369     /* profile_idc */
370     int profile_idc;
371     /* level */
372     int level_idc;
373     /* band */
374     int band_idc;
375     /* width of input frame */
376     int w;
377     /* height of input frame */
378     int h;
379     /* frame rate (Hz) numerator, denominator */
380     int fps_num;
381     int fps_den;
382     /* rate control type */
383     int rc_type;
384     /* quantization parameter (0 ~ 63)*/
385     int qp;
386     /* quantization parameter offset for CB */
387     int qp_cb_offset;
388     /* quantization parameter offset for CR */
389     int qp_cr_offset;
390     /* bitrate (unit: kbps) */
391     int bitrate;
392     /* use filler data for tight constant bitrate */
393     int use_filler;
394     /* use filler quantization matrix */
395     int use_q_matrix;
396     int q_matrix_y[OAPV_BLK_D];
397     int q_matrix_u[OAPV_BLK_D];
398     int q_matrix_v[OAPV_BLK_D];
399     int q_matrix_x[OAPV_BLK_D];
400     /* color space */
401     int csp;
402     int tile_cols;
403     int tile_rows;
404     int tile_w_mb;
405     int tile_h_mb;
406     int preset;
407 };
408 
409 /*****************************************************************************
410  * description for encoder creation
411  *****************************************************************************/
412 typedef struct oapve_cdesc oapve_cdesc_t;
413 struct oapve_cdesc {
414     int           max_bs_buf_size;            // max bitstream buffer size
415     int           max_num_frms;               // max number of frames to be encoded
416     int           threads;                    // number of threads
417     oapve_param_t param[OAPV_MAX_NUM_FRAMES]; // encoding parameters
418 };
419 
420 /*****************************************************************************
421  * encoding status
422  *****************************************************************************/
423 typedef struct oapve_stat oapve_stat_t;
424 struct oapve_stat {
425     int            write;                         // byte size of encoded bitstream
426     oapv_au_info_t aui;                           // information of encoded frames
427     int            frm_size[OAPV_MAX_NUM_FRAMES]; // bitstream byte size of each frame
428 };
429 
430 /*****************************************************************************
431  * description for decoder creation
432  *****************************************************************************/
433 typedef struct oapvd_cdesc oapvd_cdesc_t;
434 struct oapvd_cdesc {
435     int threads; // number of threads
436 };
437 
438 /*****************************************************************************
439  * decoding status
440  *****************************************************************************/
441 typedef struct oapvd_stat oapvd_stat_t;
442 struct oapvd_stat {
443     int            read;                          // byte size of decoded bitstream (read size)
444     oapv_au_info_t aui;                           // information of decoded frames
445     int            frm_size[OAPV_MAX_NUM_FRAMES]; // bitstream byte size of each frame
446 };
447 
448 /*****************************************************************************
449  * metadata payload
450  *****************************************************************************/
451 typedef struct oapvm_payload oapvm_payload_t;
452 struct oapvm_payload {
453     int           group_id;  // group ID
454     int           type;      // payload type
455     unsigned char uuid[16];  // UUID for user-defined metadata payload
456     void         *data;      // address of metadata payload
457     int           data_size; // byte size of metadata payload
458 };
459 
460 /*****************************************************************************
461  * interface for metadata container
462  *****************************************************************************/
463 typedef void       *oapvm_t; /* instance identifier for OAPV metadata container */
464 
465 oapvm_t OAPV_EXPORT oapvm_create(int *err);
466 void OAPV_EXPORT oapvm_delete(oapvm_t mid);
467 void OAPV_EXPORT oapvm_rem_all(oapvm_t mid);
468 int OAPV_EXPORT oapvm_set(oapvm_t mid, int group_id, int type, void *data, int size, unsigned char *uuid);
469 int OAPV_EXPORT oapvm_get(oapvm_t mid, int group_id, int type, void **data, int *size, unsigned char *uuid);
470 int OAPV_EXPORT oapvm_rem(oapvm_t mid, int group_id, int type, unsigned char *uuid);
471 int OAPV_EXPORT oapvm_set_all(oapvm_t mid, oapvm_payload_t *pld, int num_plds);
472 int OAPV_EXPORT oapvm_get_all(oapvm_t mid, oapvm_payload_t *pld, int *num_plds);
473 
474 /*****************************************************************************
475  * interface for encoder
476  *****************************************************************************/
477 typedef void       *oapve_t; /* instance identifier for OAPV encoder */
478 
479 oapve_t OAPV_EXPORT oapve_create(oapve_cdesc_t *cdesc, int *err);
480 void OAPV_EXPORT oapve_delete(oapve_t eid);
481 int OAPV_EXPORT oapve_config(oapve_t eid, int cfg, void *buf, int *size);
482 int OAPV_EXPORT oapve_param_default(oapve_param_t *param);
483 int OAPV_EXPORT oapve_encode(oapve_t eid, oapv_frms_t *ifrms, oapvm_t mid, oapv_bitb_t *bitb, oapve_stat_t *stat, oapv_frms_t *rfrms);
484 
485 /*****************************************************************************
486  * interface for decoder
487  *****************************************************************************/
488 typedef void       *oapvd_t; /* instance identifier for OAPV decoder */
489 
490 oapvd_t OAPV_EXPORT oapvd_create(oapvd_cdesc_t *cdesc, int *err);
491 void OAPV_EXPORT oapvd_delete(oapvd_t did);
492 int OAPV_EXPORT oapvd_config(oapvd_t did, int cfg, void *buf, int *size);
493 int OAPV_EXPORT oapvd_decode(oapvd_t did, oapv_bitb_t *bitb, oapv_frms_t *ofrms, oapvm_t mid, oapvd_stat_t *stat);
494 
495 /*****************************************************************************
496  * interface for utility
497  *****************************************************************************/
498 int OAPV_EXPORT oapvd_info(void *au, int au_size, oapv_au_info_t *aui);
499 
500 #ifdef __cplusplus
501 } /* extern "C" */
502 #endif
503 
504 #endif /* __OAPV_H__3342320849320483827648324783920483920432847382948__ */
505