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