• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 #ifndef AVUTIL_COMMON_H
27 #define AVUTIL_COMMON_H
28 
29 #if defined(__cplusplus) && !defined(__STDC_CONSTANT_MACROS) && !defined(UINT64_C)
30 #error missing -D__STDC_CONSTANT_MACROS / #define __STDC_CONSTANT_MACROS
31 #endif
32 
33 #include <errno.h>
34 #include <inttypes.h>
35 #include <limits.h>
36 #include <math.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "attributes.h"
43 #include "macros.h"
44 
45 //rounded division & shift
46 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
47 /* assume b>0 */
48 #define ROUNDED_DIV(a,b) (((a)>=0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
49 /* Fast a/(1<<b) rounded toward +inf. Assume a>=0 and b>=0 */
50 #define AV_CEIL_RSHIFT(a,b) (!av_builtin_constant_p(b) ? -((-(a)) >> (b)) \
51                                                        : ((a) + (1<<(b)) - 1) >> (b))
52 /* Backwards compat. */
53 #define FF_CEIL_RSHIFT AV_CEIL_RSHIFT
54 
55 #define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))
56 #define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))
57 
58 /**
59  * Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they
60  * are not representable as absolute values of their type. This is the same
61  * as with *abs()
62  * @see FFNABS()
63  */
64 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
65 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
66 
67 /**
68  * Negative Absolute value.
69  * this works for all integers of all types.
70  * As with many macros, this evaluates its argument twice, it thus must not have
71  * a sideeffect, that is FFNABS(x++) has undefined behavior.
72  */
73 #define FFNABS(a) ((a) <= 0 ? (a) : (-(a)))
74 
75 /**
76  * Unsigned Absolute value.
77  * This takes the absolute value of a signed int and returns it as a unsigned.
78  * This also works with INT_MIN which would otherwise not be representable
79  * As with many macros, this evaluates its argument twice.
80  */
81 #define FFABSU(a) ((a) <= 0 ? -(unsigned)(a) : (unsigned)(a))
82 #define FFABS64U(a) ((a) <= 0 ? -(uint64_t)(a) : (uint64_t)(a))
83 
84 /* misc math functions */
85 
86 #ifdef HAVE_AV_CONFIG_H
87 #   include "config.h"
88 #   include "intmath.h"
89 #endif
90 
91 #ifndef av_ceil_log2
92 #   define av_ceil_log2     av_ceil_log2_c
93 #endif
94 #ifndef av_clip
95 #   define av_clip          av_clip_c
96 #endif
97 #ifndef av_clip64
98 #   define av_clip64        av_clip64_c
99 #endif
100 #ifndef av_clip_uint8
101 #   define av_clip_uint8    av_clip_uint8_c
102 #endif
103 #ifndef av_clip_int8
104 #   define av_clip_int8     av_clip_int8_c
105 #endif
106 #ifndef av_clip_uint16
107 #   define av_clip_uint16   av_clip_uint16_c
108 #endif
109 #ifndef av_clip_int16
110 #   define av_clip_int16    av_clip_int16_c
111 #endif
112 #ifndef av_clipl_int32
113 #   define av_clipl_int32   av_clipl_int32_c
114 #endif
115 #ifndef av_clip_intp2
116 #   define av_clip_intp2    av_clip_intp2_c
117 #endif
118 #ifndef av_clip_uintp2
119 #   define av_clip_uintp2   av_clip_uintp2_c
120 #endif
121 #ifndef av_mod_uintp2
122 #   define av_mod_uintp2    av_mod_uintp2_c
123 #endif
124 #ifndef av_sat_add32
125 #   define av_sat_add32     av_sat_add32_c
126 #endif
127 #ifndef av_sat_dadd32
128 #   define av_sat_dadd32    av_sat_dadd32_c
129 #endif
130 #ifndef av_sat_sub32
131 #   define av_sat_sub32     av_sat_sub32_c
132 #endif
133 #ifndef av_sat_dsub32
134 #   define av_sat_dsub32    av_sat_dsub32_c
135 #endif
136 #ifndef av_sat_add64
137 #   define av_sat_add64     av_sat_add64_c
138 #endif
139 #ifndef av_sat_sub64
140 #   define av_sat_sub64     av_sat_sub64_c
141 #endif
142 #ifndef av_clipf
143 #   define av_clipf         av_clipf_c
144 #endif
145 #ifndef av_clipd
146 #   define av_clipd         av_clipd_c
147 #endif
148 #ifndef av_popcount
149 #   define av_popcount      av_popcount_c
150 #endif
151 #ifndef av_popcount64
152 #   define av_popcount64    av_popcount64_c
153 #endif
154 #ifndef av_parity
155 #   define av_parity        av_parity_c
156 #endif
157 
158 #ifndef av_log2
159 av_const int av_log2(unsigned v);
160 #endif
161 
162 #ifndef av_log2_16bit
163 av_const int av_log2_16bit(unsigned v);
164 #endif
165 
166 /**
167  * Clip a signed integer value into the amin-amax range.
168  * @param a value to clip
169  * @param amin minimum value of the clip range
170  * @param amax maximum value of the clip range
171  * @return clipped value
172  */
av_clip_c(int a,int amin,int amax)173 static av_always_inline av_const int av_clip_c(int a, int amin, int amax)
174 {
175 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
176     if (amin > amax) abort();
177 #endif
178     if      (a < amin) return amin;
179     else if (a > amax) return amax;
180     else               return a;
181 }
182 
183 /**
184  * Clip a signed 64bit integer value into the amin-amax range.
185  * @param a value to clip
186  * @param amin minimum value of the clip range
187  * @param amax maximum value of the clip range
188  * @return clipped value
189  */
av_clip64_c(int64_t a,int64_t amin,int64_t amax)190 static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, int64_t amax)
191 {
192 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
193     if (amin > amax) abort();
194 #endif
195     if      (a < amin) return amin;
196     else if (a > amax) return amax;
197     else               return a;
198 }
199 
200 /**
201  * Clip a signed integer value into the 0-255 range.
202  * @param a value to clip
203  * @return clipped value
204  */
av_clip_uint8_c(int a)205 static av_always_inline av_const uint8_t av_clip_uint8_c(int a)
206 {
207     if (a&(~0xFF)) return (~a)>>31;
208     else           return a;
209 }
210 
211 /**
212  * Clip a signed integer value into the -128,127 range.
213  * @param a value to clip
214  * @return clipped value
215  */
av_clip_int8_c(int a)216 static av_always_inline av_const int8_t av_clip_int8_c(int a)
217 {
218     if ((a+0x80U) & ~0xFF) return (a>>31) ^ 0x7F;
219     else                  return a;
220 }
221 
222 /**
223  * Clip a signed integer value into the 0-65535 range.
224  * @param a value to clip
225  * @return clipped value
226  */
av_clip_uint16_c(int a)227 static av_always_inline av_const uint16_t av_clip_uint16_c(int a)
228 {
229     if (a&(~0xFFFF)) return (~a)>>31;
230     else             return a;
231 }
232 
233 /**
234  * Clip a signed integer value into the -32768,32767 range.
235  * @param a value to clip
236  * @return clipped value
237  */
av_clip_int16_c(int a)238 static av_always_inline av_const int16_t av_clip_int16_c(int a)
239 {
240     if ((a+0x8000U) & ~0xFFFF) return (a>>31) ^ 0x7FFF;
241     else                      return a;
242 }
243 
244 /**
245  * Clip a signed 64-bit integer value into the -2147483648,2147483647 range.
246  * @param a value to clip
247  * @return clipped value
248  */
av_clipl_int32_c(int64_t a)249 static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)
250 {
251     if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (int32_t)((a>>63) ^ 0x7FFFFFFF);
252     else                                         return (int32_t)a;
253 }
254 
255 /**
256  * Clip a signed integer into the -(2^p),(2^p-1) range.
257  * @param  a value to clip
258  * @param  p bit position to clip at
259  * @return clipped value
260  */
av_clip_intp2_c(int a,int p)261 static av_always_inline av_const int av_clip_intp2_c(int a, int p)
262 {
263     if (((unsigned)a + (1 << p)) & ~((2 << p) - 1))
264         return (a >> 31) ^ ((1 << p) - 1);
265     else
266         return a;
267 }
268 
269 /**
270  * Clip a signed integer to an unsigned power of two range.
271  * @param  a value to clip
272  * @param  p bit position to clip at
273  * @return clipped value
274  */
av_clip_uintp2_c(int a,int p)275 static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
276 {
277     if (a & ~((1<<p) - 1)) return (~a) >> 31 & ((1<<p) - 1);
278     else                   return  a;
279 }
280 
281 /**
282  * Clear high bits from an unsigned integer starting with specific bit position
283  * @param  a value to clip
284  * @param  p bit position to clip at
285  * @return clipped value
286  */
av_mod_uintp2_c(unsigned a,unsigned p)287 static av_always_inline av_const unsigned av_mod_uintp2_c(unsigned a, unsigned p)
288 {
289     return a & ((1U << p) - 1);
290 }
291 
292 /**
293  * Add two signed 32-bit values with saturation.
294  *
295  * @param  a one value
296  * @param  b another value
297  * @return sum with signed saturation
298  */
av_sat_add32_c(int a,int b)299 static av_always_inline int av_sat_add32_c(int a, int b)
300 {
301     return av_clipl_int32((int64_t)a + b);
302 }
303 
304 /**
305  * Add a doubled value to another value with saturation at both stages.
306  *
307  * @param  a first value
308  * @param  b value doubled and added to a
309  * @return sum sat(a + sat(2*b)) with signed saturation
310  */
av_sat_dadd32_c(int a,int b)311 static av_always_inline int av_sat_dadd32_c(int a, int b)
312 {
313     return av_sat_add32(a, av_sat_add32(b, b));
314 }
315 
316 /**
317  * Subtract two signed 32-bit values with saturation.
318  *
319  * @param  a one value
320  * @param  b another value
321  * @return difference with signed saturation
322  */
av_sat_sub32_c(int a,int b)323 static av_always_inline int av_sat_sub32_c(int a, int b)
324 {
325     return av_clipl_int32((int64_t)a - b);
326 }
327 
328 /**
329  * Subtract a doubled value from another value with saturation at both stages.
330  *
331  * @param  a first value
332  * @param  b value doubled and subtracted from a
333  * @return difference sat(a - sat(2*b)) with signed saturation
334  */
av_sat_dsub32_c(int a,int b)335 static av_always_inline int av_sat_dsub32_c(int a, int b)
336 {
337     return av_sat_sub32(a, av_sat_add32(b, b));
338 }
339 
340 /**
341  * Add two signed 64-bit values with saturation.
342  *
343  * @param  a one value
344  * @param  b another value
345  * @return sum with signed saturation
346  */
av_sat_add64_c(int64_t a,int64_t b)347 static av_always_inline int64_t av_sat_add64_c(int64_t a, int64_t b) {
348 #if (!defined(__INTEL_COMPILER) && AV_GCC_VERSION_AT_LEAST(5,1)) || AV_HAS_BUILTIN(__builtin_add_overflow)
349     int64_t tmp;
350     return !__builtin_add_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);
351 #else
352     int64_t s = a+(uint64_t)b;
353     if ((int64_t)(a^b | ~s^b) >= 0)
354         return INT64_MAX ^ (b >> 63);
355     return s;
356 #endif
357 }
358 
359 /**
360  * Subtract two signed 64-bit values with saturation.
361  *
362  * @param  a one value
363  * @param  b another value
364  * @return difference with signed saturation
365  */
av_sat_sub64_c(int64_t a,int64_t b)366 static av_always_inline int64_t av_sat_sub64_c(int64_t a, int64_t b) {
367 #if (!defined(__INTEL_COMPILER) && AV_GCC_VERSION_AT_LEAST(5,1)) || AV_HAS_BUILTIN(__builtin_sub_overflow)
368     int64_t tmp;
369     return !__builtin_sub_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);
370 #else
371     if (b <= 0 && a >= INT64_MAX + b)
372         return INT64_MAX;
373     if (b >= 0 && a <= INT64_MIN + b)
374         return INT64_MIN;
375     return a - b;
376 #endif
377 }
378 
379 /**
380  * Clip a float value into the amin-amax range.
381  * If a is nan or -inf amin will be returned.
382  * If a is +inf amax will be returned.
383  * @param a value to clip
384  * @param amin minimum value of the clip range
385  * @param amax maximum value of the clip range
386  * @return clipped value
387  */
av_clipf_c(float a,float amin,float amax)388 static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
389 {
390 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
391     if (amin > amax) abort();
392 #endif
393     return FFMIN(FFMAX(a, amin), amax);
394 }
395 
396 /**
397  * Clip a double value into the amin-amax range.
398  * If a is nan or -inf amin will be returned.
399  * If a is +inf amax will be returned.
400  * @param a value to clip
401  * @param amin minimum value of the clip range
402  * @param amax maximum value of the clip range
403  * @return clipped value
404  */
av_clipd_c(double a,double amin,double amax)405 static av_always_inline av_const double av_clipd_c(double a, double amin, double amax)
406 {
407 #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
408     if (amin > amax) abort();
409 #endif
410     return FFMIN(FFMAX(a, amin), amax);
411 }
412 
413 /** Compute ceil(log2(x)).
414  * @param x value used to compute ceil(log2(x))
415  * @return computed ceiling of log2(x)
416  */
av_ceil_log2_c(int x)417 static av_always_inline av_const int av_ceil_log2_c(int x)
418 {
419     return av_log2((x - 1U) << 1);
420 }
421 
422 /**
423  * Count number of bits set to one in x
424  * @param x value to count bits of
425  * @return the number of bits set to one in x
426  */
av_popcount_c(uint32_t x)427 static av_always_inline av_const int av_popcount_c(uint32_t x)
428 {
429     x -= (x >> 1) & 0x55555555;
430     x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
431     x = (x + (x >> 4)) & 0x0F0F0F0F;
432     x += x >> 8;
433     return (x + (x >> 16)) & 0x3F;
434 }
435 
436 /**
437  * Count number of bits set to one in x
438  * @param x value to count bits of
439  * @return the number of bits set to one in x
440  */
av_popcount64_c(uint64_t x)441 static av_always_inline av_const int av_popcount64_c(uint64_t x)
442 {
443     return av_popcount((uint32_t)x) + av_popcount((uint32_t)(x >> 32));
444 }
445 
av_parity_c(uint32_t v)446 static av_always_inline av_const int av_parity_c(uint32_t v)
447 {
448     return av_popcount(v) & 1;
449 }
450 
451 /**
452  * Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
453  *
454  * @param val      Output value, must be an lvalue of type uint32_t.
455  * @param GET_BYTE Expression reading one byte from the input.
456  *                 Evaluated up to 7 times (4 for the currently
457  *                 assigned Unicode range).  With a memory buffer
458  *                 input, this could be *ptr++, or if you want to make sure
459  *                 that *ptr stops at the end of a NULL terminated string then
460  *                 *ptr ? *ptr++ : 0
461  * @param ERROR    Expression to be evaluated on invalid input,
462  *                 typically a goto statement.
463  *
464  * @warning ERROR should not contain a loop control statement which
465  * could interact with the internal while loop, and should force an
466  * exit from the macro code (e.g. through a goto or a return) in order
467  * to prevent undefined results.
468  */
469 #define GET_UTF8(val, GET_BYTE, ERROR)\
470     val= (GET_BYTE);\
471     {\
472         uint32_t top = (val & 128) >> 1;\
473         if ((val & 0xc0) == 0x80 || val >= 0xFE)\
474             {ERROR}\
475         while (val & top) {\
476             unsigned int tmp = (GET_BYTE) - 128;\
477             if(tmp>>6)\
478                 {ERROR}\
479             val= (val<<6) + tmp;\
480             top <<= 5;\
481         }\
482         val &= (top << 1) - 1;\
483     }
484 
485 /**
486  * Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.
487  *
488  * @param val       Output value, must be an lvalue of type uint32_t.
489  * @param GET_16BIT Expression returning two bytes of UTF-16 data converted
490  *                  to native byte order.  Evaluated one or two times.
491  * @param ERROR     Expression to be evaluated on invalid input,
492  *                  typically a goto statement.
493  */
494 #define GET_UTF16(val, GET_16BIT, ERROR)\
495     val = (GET_16BIT);\
496     {\
497         unsigned int hi = val - 0xD800;\
498         if (hi < 0x800) {\
499             val = (GET_16BIT) - 0xDC00;\
500             if (val > 0x3FFU || hi > 0x3FFU)\
501                 {ERROR}\
502             val += (hi<<10) + 0x10000;\
503         }\
504     }\
505 
506 /**
507  * @def PUT_UTF8(val, tmp, PUT_BYTE)
508  * Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
509  * @param val is an input-only argument and should be of type uint32_t. It holds
510  * a UCS-4 encoded Unicode character that is to be converted to UTF-8. If
511  * val is given as a function it is executed only once.
512  * @param tmp is a temporary variable and should be of type uint8_t. It
513  * represents an intermediate value during conversion that is to be
514  * output by PUT_BYTE.
515  * @param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
516  * It could be a function or a statement, and uses tmp as the input byte.
517  * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
518  * executed up to 4 times for values in the valid UTF-8 range and up to
519  * 7 times in the general case, depending on the length of the converted
520  * Unicode character.
521  */
522 #define PUT_UTF8(val, tmp, PUT_BYTE)\
523     {\
524         int bytes, shift;\
525         uint32_t in = val;\
526         if (in < 0x80) {\
527             tmp = in;\
528             PUT_BYTE\
529         } else {\
530             bytes = (av_log2(in) + 4) / 5;\
531             shift = (bytes - 1) * 6;\
532             tmp = (256 - (256 >> bytes)) | (in >> shift);\
533             PUT_BYTE\
534             while (shift >= 6) {\
535                 shift -= 6;\
536                 tmp = 0x80 | ((in >> shift) & 0x3f);\
537                 PUT_BYTE\
538             }\
539         }\
540     }
541 
542 /**
543  * @def PUT_UTF16(val, tmp, PUT_16BIT)
544  * Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).
545  * @param val is an input-only argument and should be of type uint32_t. It holds
546  * a UCS-4 encoded Unicode character that is to be converted to UTF-16. If
547  * val is given as a function it is executed only once.
548  * @param tmp is a temporary variable and should be of type uint16_t. It
549  * represents an intermediate value during conversion that is to be
550  * output by PUT_16BIT.
551  * @param PUT_16BIT writes the converted UTF-16 data to any proper destination
552  * in desired endianness. It could be a function or a statement, and uses tmp
553  * as the input byte.  For example, PUT_BYTE could be "*output++ = tmp;"
554  * PUT_BYTE will be executed 1 or 2 times depending on input character.
555  */
556 #define PUT_UTF16(val, tmp, PUT_16BIT)\
557     {\
558         uint32_t in = val;\
559         if (in < 0x10000) {\
560             tmp = in;\
561             PUT_16BIT\
562         } else {\
563             tmp = 0xD800 | ((in - 0x10000) >> 10);\
564             PUT_16BIT\
565             tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
566             PUT_16BIT\
567         }\
568     }\
569 
570 
571 
572 #include "mem.h"
573 
574 #ifdef HAVE_AV_CONFIG_H
575 #    include "internal.h"
576 #endif /* HAVE_AV_CONFIG_H */
577 
578 #endif /* AVUTIL_COMMON_H */
579