1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * common internal and external API header
24  */
25 module ffmpeg.libavutil.common;
26 extern (C) @nogc nothrow:
27 
28 extern (D) auto AV_NE(T0, T1)(auto ref T0 be, auto ref T1 le)
29 {
30     return le;
31 }
32 
33 //rounded division & shift
34 extern (D) auto RSHIFT(T0, T1)(auto ref T0 a, auto ref T1 b)
35 {
36     return a > 0 ? (a + ((1 << b) >> 1)) >> b : (a + ((1 << b) >> 1) - 1) >> b;
37 }
38 
39 /* assume b>0 */
40 extern (D) auto ROUNDED_DIV(T0, T1)(auto ref T0 a, auto ref T1 b)
41 {
42     return (a >= 0 ? a + (b >> 1) : a - (b >> 1)) / b;
43 }
44 
45 /* Fast a/(1<<b) rounded toward +inf. Assume a>=0 and b>=0 */
46 extern (D) auto AV_CEIL_RSHIFT(T0, T1)(auto ref T0 a, auto ref T1 b)
47 {
48     return /*!av_builtin_constant_p() ?*/ -((-a) >> b) /*: (a + (1 << b) - 1) >> b*/;
49 }
50 
51 /* Backwards compat. */
52 alias FF_CEIL_RSHIFT = AV_CEIL_RSHIFT;
53 
54 extern (D) auto FFUDIV(T0, T1)(auto ref T0 a, auto ref T1 b)
55 {
56     return (a > 0 ? a : a - b + 1) / b;
57 }
58 
59 extern (D) auto FFUMOD(T0, T1)(auto ref T0 a, auto ref T1 b)
60 {
61     return a - b * FFUDIV(a, b);
62 }
63 
64 /**
65  * Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they
66  * are not representable as absolute values of their type. This is the same
67  * as with *abs()
68  * @see FFNABS()
69  */
70 extern (D) auto FFABS(T)(auto ref T a)
71 {
72     return a >= 0 ? a : (-a);
73 }
74 
75 extern (D) int FFSIGN(T)(auto ref T a)
76 {
77     return a > 0 ? 1 : -1;
78 }
79 
80 /**
81  * Negative Absolute value.
82  * this works for all integers of all types.
83  * As with many macros, this evaluates its argument twice, it thus must not have
84  * a sideeffect, that is FFNABS(x++) has undefined behavior.
85  */
86 extern (D) auto FFNABS(T)(auto ref T a)
87 {
88     return a <= 0 ? a : (-a);
89 }
90 
91 /**
92  * Unsigned Absolute value.
93  * This takes the absolute value of a signed int and returns it as a unsigned.
94  * This also works with INT_MIN which would otherwise not be representable
95  * As with many macros, this evaluates its argument twice.
96  */
97 extern (D) auto FFABSU(T)(auto ref T a)
98 {
99     return a <= 0 ? -cast(uint) a : cast(uint) a;
100 }
101 
102 extern (D) auto FFABS64U(T)(auto ref T a)
103 {
104     return a <= 0 ? -cast(ulong) a : cast(ulong) a;
105 }
106 
107 /**
108  * Comparator.
109  * For two numerical expressions x and y, gives 1 if x > y, -1 if x < y, and 0
110  * if x == y. This is useful for instance in a qsort comparator callback.
111  * Furthermore, compilers are able to optimize this to branchless code, and
112  * there is no risk of overflow with signed types.
113  * As with many macros, this evaluates its argument multiple times, it thus
114  * must not have a side-effect.
115  */
116 extern (D) auto FFDIFFSIGN(T0, T1)(auto ref T0 x, auto ref T1 y)
117 {
118     return (x > y) - (x < y);
119 }
120 
121 extern (D) auto FFMAX(T0, T1)(auto ref T0 a, auto ref T1 b)
122 {
123     return a > b ? a : b;
124 }
125 
126 extern (D) auto FFMAX3(T0, T1, T2)(auto ref T0 a, auto ref T1 b, auto ref T2 c)
127 {
128     return FFMAX(FFMAX(a, b), c);
129 }
130 
131 extern (D) auto FFMIN(T0, T1)(auto ref T0 a, auto ref T1 b)
132 {
133     return a > b ? b : a;
134 }
135 
136 extern (D) auto FFMIN3(T0, T1, T2)(auto ref T0 a, auto ref T1 b, auto ref T2 c)
137 {
138     return FFMIN(FFMIN(a, b), c);
139 }
140 
141 extern (D) size_t FF_ARRAY_ELEMS(T)(auto ref T a)
142 {
143     return a.sizeof / (a[0]).sizeof;
144 }
145 
146 /* misc math functions */
147 
148 alias av_ceil_log2 = av_ceil_log2_c;
149 
150 alias av_clip = av_clip_c;
151 
152 alias av_clip64 = av_clip64_c;
153 
154 alias av_clip_uint8 = av_clip_uint8_c;
155 
156 alias av_clip_int8 = av_clip_int8_c;
157 
158 alias av_clip_uint16 = av_clip_uint16_c;
159 
160 alias av_clip_int16 = av_clip_int16_c;
161 
162 alias av_clipl_int32 = av_clipl_int32_c;
163 
164 alias av_clip_intp2 = av_clip_intp2_c;
165 
166 alias av_clip_uintp2 = av_clip_uintp2_c;
167 
168 alias av_mod_uintp2 = av_mod_uintp2_c;
169 
170 alias av_sat_add32 = av_sat_add32_c;
171 
172 alias av_sat_dadd32 = av_sat_dadd32_c;
173 
174 alias av_sat_sub32 = av_sat_sub32_c;
175 
176 alias av_sat_dsub32 = av_sat_dsub32_c;
177 
178 alias av_sat_add64 = av_sat_add64_c;
179 
180 alias av_sat_sub64 = av_sat_sub64_c;
181 
182 alias av_clipf = av_clipf_c;
183 
184 alias av_clipd = av_clipd_c;
185 
186 alias av_popcount = av_popcount_c;
187 
188 alias av_popcount64 = av_popcount64_c;
189 
190 alias av_parity = av_parity_c;
191 
192 int av_log2 (uint v);
193 
194 int av_log2_16bit (uint v);
195 
196 /**
197  * Clip a signed integer value into the amin-amax range.
198  * @param a value to clip
199  * @param amin minimum value of the clip range
200  * @param amax maximum value of the clip range
201  * @return clipped value
202  */
203 int av_clip_c (int a, int amin, int amax);
204 
205 /**
206  * Clip a signed 64bit integer value into the amin-amax range.
207  * @param a value to clip
208  * @param amin minimum value of the clip range
209  * @param amax maximum value of the clip range
210  * @return clipped value
211  */
212 long av_clip64_c (long a, long amin, long amax);
213 
214 /**
215  * Clip a signed integer value into the 0-255 range.
216  * @param a value to clip
217  * @return clipped value
218  */
219 ubyte av_clip_uint8_c (int a);
220 
221 /**
222  * Clip a signed integer value into the -128,127 range.
223  * @param a value to clip
224  * @return clipped value
225  */
226 byte av_clip_int8_c (int a);
227 
228 /**
229  * Clip a signed integer value into the 0-65535 range.
230  * @param a value to clip
231  * @return clipped value
232  */
233 ushort av_clip_uint16_c (int a);
234 
235 /**
236  * Clip a signed integer value into the -32768,32767 range.
237  * @param a value to clip
238  * @return clipped value
239  */
240 short av_clip_int16_c (int a);
241 
242 /**
243  * Clip a signed 64-bit integer value into the -2147483648,2147483647 range.
244  * @param a value to clip
245  * @return clipped value
246  */
247 int av_clipl_int32_c (long a);
248 
249 /**
250  * Clip a signed integer into the -(2^p),(2^p-1) range.
251  * @param  a value to clip
252  * @param  p bit position to clip at
253  * @return clipped value
254  */
255 int av_clip_intp2_c (int a, int p);
256 
257 /**
258  * Clip a signed integer to an unsigned power of two range.
259  * @param  a value to clip
260  * @param  p bit position to clip at
261  * @return clipped value
262  */
263 uint av_clip_uintp2_c (int a, int p);
264 
265 /**
266  * Clear high bits from an unsigned integer starting with specific bit position
267  * @param  a value to clip
268  * @param  p bit position to clip at
269  * @return clipped value
270  */
271 uint av_mod_uintp2_c (uint a, uint p);
272 
273 /**
274  * Add two signed 32-bit values with saturation.
275  *
276  * @param  a one value
277  * @param  b another value
278  * @return sum with signed saturation
279  */
280 int av_sat_add32_c (int a, int b);
281 
282 /**
283  * Add a doubled value to another value with saturation at both stages.
284  *
285  * @param  a first value
286  * @param  b value doubled and added to a
287  * @return sum sat(a + sat(2*b)) with signed saturation
288  */
289 int av_sat_dadd32_c (int a, int b);
290 
291 /**
292  * Subtract two signed 32-bit values with saturation.
293  *
294  * @param  a one value
295  * @param  b another value
296  * @return difference with signed saturation
297  */
298 int av_sat_sub32_c (int a, int b);
299 
300 /**
301  * Subtract a doubled value from another value with saturation at both stages.
302  *
303  * @param  a first value
304  * @param  b value doubled and subtracted from a
305  * @return difference sat(a - sat(2*b)) with signed saturation
306  */
307 int av_sat_dsub32_c (int a, int b);
308 
309 /**
310  * Add two signed 64-bit values with saturation.
311  *
312  * @param  a one value
313  * @param  b another value
314  * @return sum with signed saturation
315  */
316 long av_sat_add64_c (long a, long b);
317 
318 /**
319  * Subtract two signed 64-bit values with saturation.
320  *
321  * @param  a one value
322  * @param  b another value
323  * @return difference with signed saturation
324  */
325 long av_sat_sub64_c (long a, long b);
326 
327 /**
328  * Clip a float value into the amin-amax range.
329  * @param a value to clip
330  * @param amin minimum value of the clip range
331  * @param amax maximum value of the clip range
332  * @return clipped value
333  */
334 float av_clipf_c (float a, float amin, float amax);
335 
336 /**
337  * Clip a double value into the amin-amax range.
338  * @param a value to clip
339  * @param amin minimum value of the clip range
340  * @param amax maximum value of the clip range
341  * @return clipped value
342  */
343 double av_clipd_c (double a, double amin, double amax);
344 
345 /** Compute ceil(log2(x)).
346  * @param x value used to compute ceil(log2(x))
347  * @return computed ceiling of log2(x)
348  */
349 int av_ceil_log2_c (int x);
350 
351 /**
352  * Count number of bits set to one in x
353  * @param x value to count bits of
354  * @return the number of bits set to one in x
355  */
356 int av_popcount_c (uint x);
357 
358 /**
359  * Count number of bits set to one in x
360  * @param x value to count bits of
361  * @return the number of bits set to one in x
362  */
363 int av_popcount64_c (ulong x);
364 
365 int av_parity_c (uint v);
366 
367 extern (D) auto MKTAG(T0, T1, T2, T3)(auto ref T0 a, auto ref T1 b, auto ref T2 c, auto ref T3 d)
368 {
369     return a | (b << 8) | (c << 16) | (cast(uint) d << 24);
370 }
371 
372 extern (D) auto MKBETAG(T0, T1, T2, T3)(auto ref T0 a, auto ref T1 b, auto ref T2 c, auto ref T3 d)
373 {
374     return d | (c << 8) | (b << 16) | (cast(uint) a << 24);
375 }
376 
377 /**
378  * Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
379  *
380  * @param val      Output value, must be an lvalue of type uint32_t.
381  * @param GET_BYTE Expression reading one byte from the input.
382  *                 Evaluated up to 7 times (4 for the currently
383  *                 assigned Unicode range).  With a memory buffer
384  *                 input, this could be *ptr++, or if you want to make sure
385  *                 that *ptr stops at the end of a NULL terminated string then
386  *                 *ptr ? *ptr++ : 0
387  * @param ERROR    Expression to be evaluated on invalid input,
388  *                 typically a goto statement.
389  *
390  * @warning ERROR should not contain a loop control statement which
391  * could interact with the internal while loop, and should force an
392  * exit from the macro code (e.g. through a goto or a return) in order
393  * to prevent undefined results.
394  */
395 
396 /**
397  * Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.
398  *
399  * @param val       Output value, must be an lvalue of type uint32_t.
400  * @param GET_16BIT Expression returning two bytes of UTF-16 data converted
401  *                  to native byte order.  Evaluated one or two times.
402  * @param ERROR     Expression to be evaluated on invalid input,
403  *                  typically a goto statement.
404  */
405 
406 /**
407  * @def PUT_UTF8(val, tmp, PUT_BYTE)
408  * Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
409  * @param val is an input-only argument and should be of type uint32_t. It holds
410  * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
411  * val is given as a function it is executed only once.
412  * @param tmp is a temporary variable and should be of type uint8_t. It
413  * represents an intermediate value during conversion that is to be
414  * output by PUT_BYTE.
415  * @param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
416  * It could be a function or a statement, and uses tmp as the input byte.
417  * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
418  * executed up to 4 times for values in the valid UTF-8 range and up to
419  * 7 times in the general case, depending on the length of the converted
420  * Unicode character.
421  */
422 
423 /**
424  * @def PUT_UTF16(val, tmp, PUT_16BIT)
425  * Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).
426  * @param val is an input-only argument and should be of type uint32_t. It holds
427  * a UCS-4 encoded Unicode character that is to be converted to UTF-16. If
428  * val is given as a function it is executed only once.
429  * @param tmp is a temporary variable and should be of type uint16_t. It
430  * represents an intermediate value during conversion that is to be
431  * output by PUT_16BIT.
432  * @param PUT_16BIT writes the converted UTF-16 data to any proper destination
433  * in desired endianness. It could be a function or a statement, and uses tmp
434  * as the input byte.  For example, PUT_BYTE could be "*output++ = tmp;"
435  * PUT_BYTE will be executed 1 or 2 times depending on input character.
436  */
437 
438 /* HAVE_AV_CONFIG_H */
439 
440 /* AVUTIL_COMMON_H */