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_UTIL_H__
33 #define __OAPV_UTIL_H__
34
35 #include "oapv_def.h"
36
37 #define oapv_max(a, b) (((a) > (b)) ? (a) : (b))
38 #define oapv_min(a, b) (((a) < (b)) ? (a) : (b))
39 #define oapv_median(x, y, z) ((((y) < (z)) ^ ((z) < (x))) ? (((x) < (y)) ^ ((z) < (x))) ? (y) : (x) : (z))
40
41 #define oapv_abs(a) (((a) > (0)) ? (a) : (-(a)))
42 #define oapv_abs64(a) (((a) ^ ((a) >> 63)) - ((a) >> 63)) // only for 64bit variable
43 #define oapv_abs32(a) (((a) ^ ((a) >> 31)) - ((a) >> 31)) // only for 32bit variable
44 #define oapv_abs16(a) (((a) ^ ((a) >> 15)) - ((a) >> 15)) // only for 16bit variable
45
46 #define oapv_clip3(min, max, val) oapv_max((min), oapv_min((max), (val)))
47 #define oapv_clip16_add(a, b) (oapv_min((a) + (b), 0xffff)) // clipping addition
48
49 // macro to get a sign from a value.
50 // operation: if(val < 0) return 1, else return 0
51 #define oapv_get_sign(val) ((val < 0) ? 1 : 0)
52
53 // macro to set sign into a value.
54 // operation: if(sign == 0) return val, else if(sign == 1) return -val
55 #define oapv_set_sign(val, sign) ((sign) ? -val : val)
56
57 // macro to get a sign from a 16-bit value.
58 // operation: if(val < 0) return 1, else return 0
59 #define oapv_get_sign16(val) (((val) >> 15) & 1)
60
61 // macro to set sign to a 16-bit value.
62 // operation: if(sign == 0) return val, else if(sign == 1) return -val
63 #define oapv_set_sign16(val, sign) (((val) ^ ((s16)((sign) << 15) >> 15)) + (sign))
64
65 #define oapv_modulo_idx(num, mod) (((num) + (mod)) % (mod))
66
67 #define oapv_align_value(val, align) ((((val) + (align) - 1) / (align)) * (align))
68
chroma_format_idc_to_color_format(int chroma_format_idc)69 static inline int chroma_format_idc_to_color_format(int chroma_format_idc)
70 {
71 return ((chroma_format_idc == 0) ? OAPV_CF_YCBCR400
72 : (chroma_format_idc == 1) ? OAPV_CF_YCBCR420
73 : (chroma_format_idc == 2) ? OAPV_CF_YCBCR422
74 : (chroma_format_idc == 3) ? OAPV_CF_YCBCR444
75 : OAPV_CF_YCBCR4444);
76 }
77
color_format_to_chroma_format_idc(int color_format)78 static inline int color_format_to_chroma_format_idc(int color_format)
79 {
80 if(color_format == OAPV_CF_PLANAR2) {
81 return 2;
82 }
83 else {
84 return ((color_format == OAPV_CF_YCBCR400) ? 0
85 : (color_format == OAPV_CF_YCBCR420) ? 1
86 : (color_format == OAPV_CF_YCBCR422) ? 2
87 : (color_format == OAPV_CF_YCBCR444) ? 3
88 : 4);
89 }
90 }
91
get_chroma_sft_w(int chroma_format_idc)92 static inline int get_chroma_sft_w(int chroma_format_idc)
93 {
94 return ((chroma_format_idc == 0) ? 1
95 : (chroma_format_idc == 1) ? 1
96 : (chroma_format_idc == 2) ? 1
97 : 0);
98 }
99
get_chroma_sft_h(int chroma_format_idc)100 static inline int get_chroma_sft_h(int chroma_format_idc)
101 {
102 return ((chroma_format_idc == 0) ? 1
103 : (chroma_format_idc == 1) ? 1
104 : 0);
105 }
get_num_comp(int chroma_format_idc)106 static inline int get_num_comp(int chroma_format_idc)
107 {
108
109 return (chroma_format_idc == 0) ? 1
110 : (chroma_format_idc == 4) ? 4
111 : 3;
112 }
113
imgb_addref(oapv_imgb_t * imgb)114 static inline void imgb_addref(oapv_imgb_t *imgb)
115 {
116 if(imgb->addref) {
117 imgb->addref(imgb);
118 }
119 }
120
imgb_release(oapv_imgb_t * imgb)121 static inline void imgb_release(oapv_imgb_t *imgb)
122 {
123 if(imgb->release) {
124 imgb->release(imgb);
125 }
126 }
127
128 /* MD5 structure */
129 typedef struct
130 {
131 u32 h[4]; /* hash state ABCD */
132 u8 msg[64]; /*input buffer */
133 u32 bits[2]; /* number of bits, modulo 2^64 (lsb first)*/
134 } oapv_md5_t;
135
136 /* MD5 Functions */
137 void oapv_imgb_set_md5(oapv_imgb_t *imgb);
138 void oapv_block_copy(s16 *src, int src_stride, s16 *dst, int dst_stride, int log2_copy_w, int log2_copy_h);
139 int oapv_set_md5_pld(oapvm_t mid, int group_id, oapv_imgb_t *rec);
140
141 #if X86_SSE
142 int oapv_check_cpu_info_x86();
143 #endif
144
145 /* For debugging (START) */
146 #define ENC_DEC_DUMP 0
147 #if ENC_DEC_DUMP
148 #if defined(__GNUC__)
149 #pragma message "warning! syntax dump is on"
150 #else
151 #pragma message("warning! syntax dump is on")
152 #endif
153
154 #define DUMP_ENABLE_HLS 1
155 #define DUMP_ENABLE_COEF 1
156
157 typedef enum _OAPV_DUMP_OPTION {
158 OAPV_DUMP_HLS,
159 OAPV_DUMP_COEF,
160 } OAPV_DUMP_OPTION;
161
162 extern FILE *oapv_fp_dump;
163 extern int oapv_is_dump;
164
165 void oapv_dump_string0(int cond, const char *fmt, ...);
166 void oapv_dump_coef0(short *coef, int size, int x, int y, int c);
167 void oapv_dump_create0(int is_enc);
168 void oapv_dump_delete0();
169
170 // Syntax dump macro functions
171 #define DUMP_CREATE(is_enc) oapv_dump_create0(is_enc)
172 #define DUMP_DELETE() oapv_dump_delete0()
173 #define DUMP_SET(val) oapv_is_dump = val
174 #define DUMP_HLS(name, val) \
175 oapv_dump_string0(OAPV_DUMP_HLS, "%-34s: %12d\n", #name, val)
176 #define DUMP_COEF(coef, size, x, y, c) \
177 oapv_dump_coef0(coef, size, x, y, c)
178 #define DUMP_SAVE(id) long dump32u8i90432890_##id = ftell(oapv_fp_dump)
179 #define DUMP_LOAD(id) fseek(oapv_fp_dump, dump32u8i90432890_##id, SEEK_SET)
180 #else
181 #define DUMP_CREATE(is_enc)
182 #define DUMP_DELETE()
183 #define DUMP_SET(val)
184 #define DUMP_HLS(name, val)
185 #define DUMP_COEF(coef, size, args, ...)
186 #define DUMP_SAVE(id)
187 #define DUMP_LOAD(id)
188 #endif
189 /* For debugging (END) */
190
191 #endif /* __OAPV_UTIL_H__ */
192