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