xref: /aosp_15_r20/external/libopenapv/src/oapv_def.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_DEF_H_4738294732894739280473892473829_
33 #define _OAPV_DEF_H_4738294732894739280473892473829_
34 
35 #ifndef ENABLE_ENCODER
36 #define ENABLE_ENCODER 1 // for enabling encoder functionality
37 #endif
38 
39 #ifndef ENABLE_DECODER
40 #define ENABLE_DECODER 1 // for enabling decoder functionality
41 #endif
42 
43 #include "oapv.h"
44 #include "oapv_port.h"
45 #include "oapv_tpool.h"
46 
47 /* oapv encoder magic code */
48 #define OAPVE_MAGIC_CODE          0x41503145 /* AP1E */
49 
50 /* oapv decoder magic code */
51 #define OAPVD_MAGIC_CODE          0x41503144 /* AP1D */
52 
53 /* Max. and min. Quantization parameter */
54 #define MAX_QUANT                 63
55 #define MIN_QUANT                 0
56 
57 #define MAX_COST                  (1.7e+308) /* maximum cost value */
58 
59 #define Y_C                       0 /* Y luma */
60 #define U_C                       1 /* Cb Chroma */
61 #define V_C                       2 /* Cr Chroma */
62 #define X_C                       3 /* X channel */
63 #define N_C                       4 /* maximum number of color component */
64 
65 #define OAPV_VLC_TREE_LEVEL       2
66 #define OAPV_MIN_DC_LEVEL_CTX     0
67 #define OAPV_MAX_DC_LEVEL_CTX     5
68 #define OAPV_MIN_AC_LEVEL_CTX     0
69 #define OAPV_MAX_AC_LEVEL_CTX     4
70 
71 /* need to check */
72 #define OAPV_MAX_TILE_ROWS        20
73 #define OAPV_MAX_TILE_COLS        20
74 #define OAPV_MAX_TILES            (OAPV_MAX_TILE_ROWS * OAPV_MAX_TILE_COLS)
75 
76 /* Maximum transform dynamic range (excluding sign bit) */
77 #define MAX_TX_DYNAMIC_RANGE      15
78 #define MAX_TX_VAL                ((1 << MAX_TX_DYNAMIC_RANGE) - 1)
79 #define MIN_TX_VAL                (-(1 << MAX_TX_DYNAMIC_RANGE))
80 
81 #define QUANT_SHIFT               14
82 #define QUANT_DQUANT_SHIFT        20
83 
84 /* encoder status */
85 #define ENC_TILE_STAT_NOT_ENCODED 0
86 #define ENC_TILE_STAT_ON_ENCODING 1
87 #define ENC_TILE_STAT_ENCODED     2
88 
89 /*****************************************************************************
90  * PBU data structure
91  *****************************************************************************/
92 typedef struct oapv_pbuh oapv_pbuh_t;
93 struct oapv_pbuh { // 4-byte
94     int pbu_type;  /* u( 8) */
95     int group_id;  /* u(16) */
96     // int reserved_zero_8bits                    /* u( 8) */
97 };
98 
99 /*****************************************************************************
100  * Frame info
101  *****************************************************************************/
102 typedef struct oapv_fi oapv_fi_t;
103 struct oapv_fi {     // 112byte
104     int profile_idc; /* u( 8) */
105     int level_idc;   /* u( 8) */
106     int band_idc;    /* u( 3) */
107     // int            reserved_zero_5bits;                     /* u( 5) */
108     u32 frame_width;           /* u(32) */
109     u32 frame_height;          /* u(32) */
110     int chroma_format_idc;     /* u( 4) */
111     int bit_depth;             /* u( 4) */
112     int capture_time_distance; /* u( 8) */
113     // int            reserved_zero_8bits;                     /* u( 8) */
114 };
115 
116 /*****************************************************************************
117  * Frame header
118  *****************************************************************************/
119 typedef struct oapv_fh oapv_fh_t;
120 struct oapv_fh {
121     oapv_fi_t fi;
122     // int reserved_zero_8bits_3;                 /* u( 8) */
123     int       color_description_present_flag; /* u( 1) */
124     int       color_primaries;                /* u( 8) */
125     int       transfer_characteristics;       /* u( 8) */
126     int       matrix_coefficients;            /* u( 8) */
127     int       use_q_matrix;                   /* u( 1) */
128     /* (start) quantization_matix  */
129     int       q_matrix[N_C][OAPV_BLK_H][OAPV_BLK_W]; /* u( 8) minus 1*/
130     /* ( end ) quantization_matix  */
131     /* (start) tile_info */
132     int       tile_width_in_mbs;            /* u(28) minus 1*/
133     int       tile_height_in_mbs;           /* u(28) minus 1*/
134     int       tile_size_present_in_fh_flag; /* u( 1) */
135     u32       tile_size[OAPV_MAX_TILES];    /* u(32) */
136     /* ( end ) tile_info  */
137     // int reserved_zero_8bits_4;                   /* u( 8) */
138 };
139 
140 /*****************************************************************************
141  * Tile header
142  *****************************************************************************/
143 #define OAPV_TILE_SIZE_LEN 4 /* u(32), 4byte */
144 typedef struct oapv_th oapv_th_t;
145 struct oapv_th {
146     int tile_header_size;    /* u(16) */
147     int tile_index;          /* u(16) */
148     u32 tile_data_size[N_C]; /* u(32) */
149     int tile_qp[N_C];        /* u( 8) */
150     int reserved_zero_8bits; /* u( 8) */
151 };
152 
153 /*****************************************************************************
154  * Access unit info
155  *****************************************************************************/
156 typedef struct oapv_aui oapv_aui_t;
157 struct oapv_aui {
158     int       num_frames;                    // u(16)
159     int       pbu_type[OAPV_MAX_NUM_FRAMES]; // u(8)
160     int       group_id[OAPV_MAX_NUM_FRAMES]; // u(16)
161     // int reserved_zero_8bits[OAPV_MAX_NUM_FRAMES]; // u(8)
162     oapv_fi_t frame_info[OAPV_MAX_NUM_FRAMES];
163     // int reserved_zero_8bits // u(8)
164 };
165 
166 ///////////////////////////////////////////////////////////////////////////////
167 // start of encoder code
168 #if ENABLE_ENCODER
169 ///////////////////////////////////////////////////////////////////////////////
170 /*****************************************************************************
171  * pre-declaration
172  *****************************************************************************/
173 typedef struct oapve_ctx  oapve_ctx_t;
174 typedef struct oapve_core oapve_core_t;
175 
176 /*****************************************************************************
177  * pre-defined function structure
178  *****************************************************************************/
179 typedef void (*oapv_fn_itx_part_t)(s16 *coef, s16 *t, int shift, int line);
180 typedef void (*oapv_fn_itx_t)(s16 *coef, int shift1, int shift2, int line);
181 typedef void (*oapv_fn_tx_t)(s16 *coef, s16 *t, int shift, int line);
182 typedef void (*oapv_fn_itx_adj_t)(int *src, int *dst, int itrans_diff_idx, int diff_step, int shift);
183 typedef int (*oapv_fn_quant_t)(s16 *coef, u8 qp, int q_matrix[OAPV_BLK_D], int log2_w, int log2_h, int bit_depth, int deadzone_offset);
184 typedef void (*oapv_fn_dquant_t)(s16 *coef, s16 q_matrix[OAPV_BLK_D], int log2_w, int log2_h, s8 shift);
185 typedef int (*oapv_fn_sad_t)(int w, int h, void *src1, void *src2, int s_src1, int s_src2, int bit_depth);
186 typedef s64 (*oapv_fn_ssd_t)(int w, int h, void *src1, void *src2, int s_src1, int s_src2, int bit_depth);
187 typedef void (*oapv_fn_diff_t)(int w, int h, void *src1, void *src2, int s_src1, int s_src2, int s_diff, s16 *diff, int bit_depth);
188 
189 typedef double (*oapv_fn_block_cost_t)(oapve_ctx_t *ctx, oapve_core_t *core, int log2_w, int log2_h, int c);
190 typedef void (*oapv_fn_imgb_to_block_rc)(oapv_imgb_t *imgb, int c, int x_l, int y_l, int w_l, int h_l, s16 *block);
191 typedef void (*oapv_fn_imgb_to_block)(void *src, int blk_w, int blk_h, int s_src, int offset_src, int s_dst, void *dst);
192 typedef void (*oapv_fn_block_to_imgb)(void *src, int blk_w, int blk_h, int s_src, int offset_dst, int s_dst, void *dst);
193 typedef void (*oapv_fn_img_pad)(oapve_ctx_t *ctx, oapv_imgb_t *imgb);
194 typedef int (*oapv_fn_had8x8)(pel *org, int s_org);
195 
196 /*****************************************************************************
197  * rate-control related
198  *****************************************************************************/
199 typedef struct oapve_rc_param {
200     double alpha;
201     double beta;
202 
203     int    qp;
204     double lambda;
205     double cost;
206 } oapve_rc_param_t;
207 
208 typedef struct oapve_rc_tile {
209     int              qp;
210     int              target_bits;
211     double           lambda;
212     int              number_pixel;
213     double           cost;
214     int              target_bits_left;
215 
216     u64              total_dist;
217     oapve_rc_param_t rc_param;
218 } oapve_rc_tile_t;
219 
220 /*****************************************************************************
221  * CORE information used for encoding process.
222  *
223  * The variables in this structure are very often used in encoding process.
224  *****************************************************************************/
225 struct oapve_core {
226     ALIGNED_16(s16 coef[OAPV_BLK_D]);
227     ALIGNED_16(s16 coef_rec[OAPV_BLK_D]);
228     oapve_ctx_t *ctx;
229     int          prev_dc_ctx[N_C];
230     int          prev_1st_ac_ctx[N_C];
231     int          tile_idx;
232     int          prev_dc[N_C];
233 
234     int          qp[N_C]; // QPs for Y, Cb(U), Cr(V)
235     int          dq_shift[N_C];
236 
237     int          q_mat_enc[N_C][OAPV_BLK_D];
238     s16          q_mat_dec[N_C][OAPV_BLK_D];
239     int          thread_idx;
240     /* platform specific data, if needed */
241     void        *pf;
242 };
243 
244 #include "oapv_bs.h"
245 
246 typedef struct oapve_tile oapve_tile_t;
247 struct oapve_tile {
248     oapv_th_t       th;
249 
250     int             x; /* x (column) position in a frame in unit of pixel */
251     int             y; /* y (row) position in a frame in unit of pixel */
252     int             w; /* tile width in unit of pixel */
253     int             h; /* tile height in unit of pixel */
254     u32             tile_size;
255     oapve_rc_tile_t rc;
256     u8             *bs_buf;
257     s32             bs_size;
258     u32             bs_buf_max;
259     volatile s32    stat;
260 };
261 
262 /******************************************************************************
263  * CONTEXT used for encoding process.
264  *
265  * All have to be stored are in this structure.
266  *****************************************************************************/
267 struct oapve_ctx {
268     u32                       magic; // magic code
269     oapve_t                   id;    // identifier
270     oapve_cdesc_t             cdesc;
271     oapv_imgb_t              *imgb;
272     oapv_imgb_t              *rec;
273 
274     oapve_param_t            *param;
275     oapv_fh_t                 fh;
276     oapve_tile_t              tile[OAPV_MAX_TILES];
277     int                       num_tiles;
278     int                       num_tile_cols;
279     int                       num_tile_rows;
280     int                       qp[N_C];
281     int                       w;
282     int                       h;
283     int                       cfi;
284     int                       num_comp;
285     int                       bit_depth;
286     int                       comp_sft[N_C][2];
287     int                       log2_block;
288     oapv_tpool_t             *tpool;
289     oapv_thread_t             thread_id[OAPV_MAX_THREADS];
290     oapv_sync_obj_t           sync_obj;
291     oapve_core_t             *core[OAPV_MAX_THREADS];
292 
293     oapv_bs_t                 bs;
294     const oapv_fn_itx_part_t *fn_itx_part;
295     const oapv_fn_itx_t      *fn_itx;
296     const oapv_fn_itx_adj_t  *fn_itx_adj;
297     const oapv_fn_tx_t       *fn_txb;
298     const oapv_fn_quant_t    *fn_quant;
299     const oapv_fn_dquant_t   *fn_dquant;
300     const oapv_fn_sad_t      *fn_sad;
301     const oapv_fn_ssd_t      *fn_ssd;
302     const oapv_fn_diff_t     *fn_diff;
303     oapv_fn_imgb_to_block_rc  fn_imgb_to_block_rc;
304     oapv_fn_imgb_to_block     fn_imgb_to_block[N_C];
305     oapv_fn_block_to_imgb     fn_block_to_imgb[N_C];
306     oapv_fn_img_pad           fn_img_pad;
307     oapv_fn_block_cost_t      fn_block;
308     oapv_fn_had8x8            fn_had8x8;
309     int                       use_frm_hash;
310     void                     *tx_tbl;
311 
312     oapve_rc_param_t          rc_param;
313 
314     /* platform specific data, if needed */
315     void                     *pf;
316 };
317 ///////////////////////////////////////////////////////////////////////////////
318 // end of encoder code
319 #endif // ENABLE_ENCODER
320 ///////////////////////////////////////////////////////////////////////////////
321 
322 ///////////////////////////////////////////////////////////////////////////////
323 // start of decoder code
324 #if ENABLE_DECODER
325 ///////////////////////////////////////////////////////////////////////////////
326 #define DEC_TILE_STAT_NOT_DECODED 0
327 #define DEC_TILE_STAT_ON_DECODING 1
328 #define DEC_TILE_STAT_DECODED     2
329 #define DEC_TILE_STAT_SIZE_ERROR  -1
330 
331 typedef struct oapvd_tile oapvd_tile_t;
332 struct oapvd_tile {
333     oapv_th_t    th;
334 
335     int          x;         /* x (column) position in a frame in unit of pixel */
336     int          y;         /* y (row) position in a frame in unit of pixel */
337     int          w;         /* tile width in unit of pixel */
338     int          h;         /* tile height in unit of pixel */
339     u32          data_size; /* tile size including tile_size_minus1 syntax */
340 
341     u8          *bs_beg; /* start position of tile in input bistream */
342     u8          *bs_end; /* end position of tile() in input bistream */
343     volatile s32 stat;   // decoding status
344 };
345 
346 typedef struct oapvd_core oapvd_core_t;
347 typedef struct oapvd_ctx  oapvd_ctx_t;
348 
349 struct oapvd_core {
350     ALIGNED_16(s16 coef[OAPV_MB_D]);
351     oapvd_ctx_t *ctx;
352 
353     int          prev_dc_ctx[N_C];
354     int          prev_1st_ac_ctx[N_C];
355     int          prev_dc[N_C];
356     int          qp[N_C];
357     int          dq_shift[N_C];
358     s16          q_mat[N_C][OAPV_BLK_D];
359 
360     int          tile_idx;
361 
362     /* platform specific data, if needed */
363     void        *pf;
364 };
365 
366 struct oapvd_ctx {
367     u32                     magic; // magic code
368     oapvd_t                 id;    // identifier
369 
370     oapvd_cdesc_t           cdesc;
371     oapvd_core_t           *core[OAPV_MAX_THREADS];
372     oapv_imgb_t            *imgb;
373     const oapv_fn_itx_t    *fn_itx;
374     const oapv_fn_dquant_t *fn_dquant;
375     oapv_fn_block_to_imgb   fn_block_to_imgb[N_C];
376     oapv_bs_t               bs;
377 
378     oapv_fh_t               fh;
379     oapvd_tile_t            tile[OAPV_MAX_TILES];
380 
381     u8                     *tile_end;
382     int                     num_tiles;
383     int                     num_tile_cols;
384     int                     num_tile_rows;
385     int                     w;
386     int                     h;
387     oapv_tpool_t           *tpool;
388     oapv_thread_t           thread_id[OAPV_MAX_THREADS];
389     oapv_sync_obj_t         sync_obj;
390     int                     cfi;              // chroma format indicator
391     int                     bit_depth;        // bit depth of decoding picture
392     int                     num_comp;         // number of components
393     int                     comp_sft[N_C][2]; // width or height shift value of each compoents, 0: width, 1: height
394     int                     use_frm_hash;
395 
396     /* platform specific data, if needed */
397     void                   *pf;
398 };
399 ///////////////////////////////////////////////////////////////////////////////
400 // end of decoder code
401 #endif // ENABLE_DECODER
402 ///////////////////////////////////////////////////////////////////////////////
403 
404 #define OAPV_FRAME_INFO_BYTE (112)
405 #define OAPV_PBU_HEADER_BYTE (32)
406 
407 #include "oapv_metadata.h"
408 #include "oapv_vlc.h"
409 #include "oapv_tq.h"
410 #include "oapv_util.h"
411 #include "oapv_tbl.h"
412 #include "oapv_rc.h"
413 #include "oapv_sad.h"
414 
415 #if X86_SSE
416 #include "sse/oapv_sad_sse.h"
417 #include "sse/oapv_tq_sse.h"
418 #include "avx/oapv_sad_avx.h"
419 #include "avx/oapv_tq_avx.h"
420 #elif ARM_NEON
421 #include "neon/oapv_sad_neon.h"
422 #include "neon/oapv_tq_neon.h"
423 #endif
424 
425 #endif /* _OAPV_DEF_H_4738294732894739280473892473829_ */
426