Loading...
Searching...
No Matches
common.h File Reference

common internal and external API header More...

`#include <errno.h>`
`#include <inttypes.h>`
`#include <limits.h>`
`#include <math.h>`
`#include <stdint.h>`
`#include <stdio.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include "attributes.h"`
`#include "error.h"`
`#include "macros.h"`
`#include "version.h"`
`#include "mem.h"`

Go to the source code of this file.

## Macros

#define | RSHIFT(a, b)
#define | ROUNDED_DIV(a, b)
#define | AV_CEIL_RSHIFT(a, b)
#define | FF_CEIL_RSHIFT AV_CEIL_RSHIFT
#define | FFUDIV(a, b)
#define | FFUMOD(a, b)
#define | FFABS(a)
| Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable as absolute values of their type.
#define | FFSIGN(a)
#define | FFNABS(a)
| Negative Absolute value.
#define | FFABSU(a)
| Unsigned Absolute value.
#define | FFABS64U(a)
#define | av_ceil_log2 av_ceil_log2_c
#define | av_clip av_clip_c
#define | av_clip64 av_clip64_c
#define | av_clip_uint8 av_clip_uint8_c
#define | av_clip_int8 av_clip_int8_c
#define | av_clip_uint16 av_clip_uint16_c
#define | av_clip_int16 av_clip_int16_c
#define | av_clipl_int32 av_clipl_int32_c
#define | av_clip_intp2 av_clip_intp2_c
#define | av_clip_uintp2 av_clip_uintp2_c
#define | av_sat_add32 av_sat_add32_c
#define | av_sat_dadd32 av_sat_dadd32_c
#define | av_sat_sub32 av_sat_sub32_c
#define | av_sat_dsub32 av_sat_dsub32_c
#define | av_sat_add64 av_sat_add64_c
#define | av_sat_sub64 av_sat_sub64_c
#define | av_clipf av_clipf_c
#define | av_clipd av_clipd_c
#define | av_zero_extend av_zero_extend_c
#define | av_popcount av_popcount_c
#define | av_popcount64 av_popcount64_c
#define | av_parity av_parity_c
#define | GET_UTF8(val, GET_BYTE, ERROR)
| Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
#define | GET_UTF16(val, GET_16BIT, ERROR)
| Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.
#define | PUT_UTF8(val, tmp, PUT_BYTE)
| Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
#define | PUT_UTF16(val, tmp, PUT_16BIT)
| Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).

## Functions

av_const int | av_log2 (unsigned v)
av_const int | av_log2_16bit (unsigned v)
static av_always_inline av_const int | av_clip_c (int a, int amin, int amax)
| Clip a signed integer value into the amin-amax range.
static av_always_inline av_const int64_t | av_clip64_c (int64_t a, int64_t amin, int64_t amax)
| Clip a signed 64bit integer value into the amin-amax range.
static av_always_inline av_const uint8_t | av_clip_uint8_c (int a)
| Clip a signed integer value into the 0-255 range.
static av_always_inline av_const int8_t | av_clip_int8_c (int a)
| Clip a signed integer value into the -128,127 range.
static av_always_inline av_const uint16_t | av_clip_uint16_c (int a)
| Clip a signed integer value into the 0-65535 range.
static av_always_inline av_const int16_t | av_clip_int16_c (int a)
| Clip a signed integer value into the -32768,32767 range.
static av_always_inline av_const int32_t | av_clipl_int32_c (int64_t a)
| Clip a signed 64-bit integer value into the -2147483648,2147483647 range.
static av_always_inline av_const int | av_clip_intp2_c (int a, int p)
| Clip a signed integer into the -(2^p),(2^p-1) range.
static av_always_inline av_const unsigned | av_clip_uintp2_c (int a, int p)
| Clip a signed integer to an unsigned power of two range.
static av_always_inline av_const unsigned | av_zero_extend_c (unsigned a, unsigned p)
| Clear high bits from an unsigned integer starting with specific bit position.
static av_always_inline int | av_sat_add32_c (int a, int b)
| Add two signed 32-bit values with saturation.
static av_always_inline int | av_sat_dadd32_c (int a, int b)
| Add a doubled value to another value with saturation at both stages.
static av_always_inline int | av_sat_sub32_c (int a, int b)
| Subtract two signed 32-bit values with saturation.
static av_always_inline int | av_sat_dsub32_c (int a, int b)
| Subtract a doubled value from another value with saturation at both stages.
static av_always_inline int64_t | av_sat_add64_c (int64_t a, int64_t b)
| Add two signed 64-bit values with saturation.
static av_always_inline int64_t | av_sat_sub64_c (int64_t a, int64_t b)
| Subtract two signed 64-bit values with saturation.
static av_always_inline av_const float | av_clipf_c (float a, float amin, float amax)
| Clip a float value into the amin-amax range.
static av_always_inline av_const double | av_clipd_c (double a, double amin, double amax)
| Clip a double value into the amin-amax range.
static av_always_inline av_const int | av_ceil_log2_c (int x)
| Compute ceil(log2(x)).
static av_always_inline av_const int | av_popcount_c (uint32_t x)
| Count number of bits set to one in x.
static av_always_inline av_const int | av_popcount64_c (uint64_t x)
| Count number of bits set to one in x.
static av_always_inline av_const int | av_parity_c (uint32_t v)

## Detailed Description

common internal and external API header

Definition in file common.h.

## Macro Definition Documentation

## RSHIFT

#define RSHIFT | ( |  | a,
|  |  | b )

Value:
((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))

Definition at line 56 of file common.h.

## ROUNDED_DIV

#define ROUNDED_DIV | ( |  | a,
|  |  | b )

Value:
(((a)>=0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))

Definition at line 58 of file common.h.

## AV_CEIL_RSHIFT

#define AV_CEIL_RSHIFT | ( |  | a,
|  |  | b )

Value:
(!av_builtin_constant_p(b) ? -((-(a)) >> (b)) \
: ((a) + (1<<(b)) - 1) >> (b))
av_builtin_constant_p
#define av_builtin_constant_p
Definition attributes.h:185

Definition at line 60 of file common.h.

## FF_CEIL_RSHIFT

#define FF_CEIL_RSHIFT AV_CEIL_RSHIFT

Definition at line 63 of file common.h.

## FFUDIV

#define FFUDIV | ( |  | a,
|  |  | b )

Value:
(((a)>0 ?(a):(a)-(b)+1) / (b))

Definition at line 65 of file common.h.

## FFUMOD

#define FFUMOD | ( |  | a,
|  |  | b )

Value:
((a)-(b)*FFUDIV(a,b))
FFUDIV
#define FFUDIV(a, b)
Definition common.h:65

Definition at line 66 of file common.h.

## FFABS

#define FFABS | ( |  | a | ) |

Value:
((a) >= 0 ? (a) : (-(a)))

Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable as absolute values of their type.

This is the same as with *abs()

See also
  FFNABS()

Definition at line 74 of file common.h.

## FFSIGN

#define FFSIGN | ( |  | a | ) |

Value:
((a) > 0 ? 1 : -1)

Definition at line 75 of file common.h.

## FFNABS

#define FFNABS | ( |  | a | ) |

Value:
((a) <= 0 ? (a) : (-(a)))

Negative Absolute value.

this works for all integers of all types. As with many macros, this evaluates its argument twice, it thus must not have a sideeffect, that is FFNABS(x++) has undefined behavior.

Definition at line 83 of file common.h.

## FFABSU

#define FFABSU | ( |  | a | ) |

Value:
((a) <= 0 ? -(unsigned)(a) : (unsigned)(a))

Unsigned Absolute value.

This takes the absolute value of a signed int and returns it as a unsigned. This also works with INT_MIN which would otherwise not be representable As with many macros, this evaluates its argument twice.

Definition at line 91 of file common.h.

## FFABS64U

#define FFABS64U | ( |  | a | ) |

Value:
((a) <= 0 ? -(uint64_t)(a) : (uint64_t)(a))

Definition at line 92 of file common.h.

## av_ceil_log2

#define av_ceil_log2 av_ceil_log2_c

Definition at line 97 of file common.h.

## av_clip

#define av_clip av_clip_c

Definition at line 100 of file common.h.

## av_clip64

#define av_clip64 av_clip64_c

Definition at line 103 of file common.h.

## av_clip_uint8

#define av_clip_uint8 av_clip_uint8_c

Definition at line 106 of file common.h.

## av_clip_int8

#define av_clip_int8 av_clip_int8_c

Definition at line 109 of file common.h.

## av_clip_uint16

#define av_clip_uint16 av_clip_uint16_c

Definition at line 112 of file common.h.

## av_clip_int16

#define av_clip_int16 av_clip_int16_c

Definition at line 115 of file common.h.

## av_clipl_int32

#define av_clipl_int32 av_clipl_int32_c

Definition at line 118 of file common.h.

Referenced by av_sat_add32_c(), and av_sat_sub32_c().

## av_clip_intp2

#define av_clip_intp2 av_clip_intp2_c

Definition at line 121 of file common.h.

## av_clip_uintp2

#define av_clip_uintp2 av_clip_uintp2_c

Definition at line 124 of file common.h.

## av_sat_add32

#define av_sat_add32 av_sat_add32_c

Definition at line 127 of file common.h.

Referenced by av_sat_dadd32_c(), and av_sat_dsub32_c().

## av_sat_dadd32

#define av_sat_dadd32 av_sat_dadd32_c

Definition at line 130 of file common.h.

## av_sat_sub32

#define av_sat_sub32 av_sat_sub32_c

Definition at line 133 of file common.h.

Referenced by av_sat_dsub32_c().

## av_sat_dsub32

#define av_sat_dsub32 av_sat_dsub32_c

Definition at line 136 of file common.h.

## av_sat_add64

#define av_sat_add64 av_sat_add64_c

Definition at line 139 of file common.h.

## av_sat_sub64

#define av_sat_sub64 av_sat_sub64_c

Definition at line 142 of file common.h.

## av_clipf

#define av_clipf av_clipf_c

Definition at line 145 of file common.h.

## av_clipd

#define av_clipd av_clipd_c

Definition at line 148 of file common.h.

## av_zero_extend

#define av_zero_extend av_zero_extend_c

Definition at line 151 of file common.h.

## av_popcount

#define av_popcount av_popcount_c

Definition at line 154 of file common.h.

Referenced by av_parity_c(), and av_popcount64_c().

## av_popcount64

#define av_popcount64 av_popcount64_c

Definition at line 157 of file common.h.

## av_parity

#define av_parity av_parity_c

Definition at line 160 of file common.h.

## GET_UTF8

#define GET_UTF8 | ( |  | val,
|  |  | GET_BYTE,
|  |  | ERROR )

Value:
val= (uint8_t)(GET_BYTE);\
{\
uint32_t top = (val & 128) >> 1;\
if ((val & 0xc0) == 0x80 || val >= 0xFE)\
{ERROR}\
while (val & top) {\
unsigned int tmp = (uint8_t)(GET_BYTE) - 128;\
if(tmp>>6)\
{ERROR}\
val= (val<<6) + tmp;\
top <<= 5;\
}\
val &= (top << 1) - 1;\
}

Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.

Parameters

  val | Output value, must be an lvalue of type uint32_t.
  GET_BYTE | Expression reading one byte from the input. Evaluated up to 7 times (4 for the currently assigned Unicode range). With a memory buffer input, this could be *ptr++, or if you want to make sure that *ptr stops at the end of a NULL terminated string then *ptr ? *ptr++ : 0
  ERROR | Expression to be evaluated on invalid input, typically a goto statement.

Warning
  ERROR should not contain a loop control statement which could interact with the internal while loop, and should force an exit from the macro code (e.g. through a goto or a return) in order to prevent undefined results.

Definition at line 488 of file common.h.

## GET_UTF16

#define GET_UTF16 | ( |  | val,
|  |  | GET_16BIT,
|  |  | ERROR )

Value:
val = (uint16_t)(GET_16BIT);\
{\
unsigned int hi = val - 0xD800;\
if (hi < 0x800) {\
val = (uint16_t)(GET_16BIT) - 0xDC00;\
if (val > 0x3FFU || hi > 0x3FFU)\
{ERROR}\
val += (hi<<10) + 0x10000;\
}\
}\

Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.

Parameters

  val | Output value, must be an lvalue of type uint32_t.
  GET_16BIT | Expression returning two bytes of UTF-16 data converted to native byte order. Evaluated one or two times.
  ERROR | Expression to be evaluated on invalid input, typically a goto statement.

Definition at line 513 of file common.h.

## PUT_UTF8

#define PUT_UTF8 | ( |  | val,
|  |  | tmp,
|  |  | PUT_BYTE )

Value:
{\
int bytes, shift;\
uint32_t in = val;\
if (in < 0x80) {\
tmp = in;\
PUT_BYTE\
} else {\
bytes = (av_log2(in) + 4) / 5;\
shift = (bytes - 1) * 6;\
tmp = (256 - (256 >> bytes)) | (in >> shift);\
PUT_BYTE\
while (shift >= 6) {\
shift -= 6;\
tmp = 0x80 | ((in >> shift) & 0x3f);\
PUT_BYTE\
}\
}\
}
av_log2
av_const int av_log2(unsigned v)

Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).

Parameters

  val | is an input-only argument and should be of type uint32_t. It holds a UCS-4 encoded Unicode character that is to be converted to UTF-8. If val is given as a function it is executed only once.
  tmp | is a temporary variable and should be of type uint8_t. It represents an intermediate value during conversion that is to be output by PUT_BYTE.
  PUT_BYTE | writes the converted UTF-8 bytes to any proper destination. It could be a function or a statement, and uses tmp as the input byte. For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be executed up to 4 times for values in the valid UTF-8 range and up to 7 times in the general case, depending on the length of the converted Unicode character.

Definition at line 541 of file common.h.

## PUT_UTF16

#define PUT_UTF16 | ( |  | val,
|  |  | tmp,
|  |  | PUT_16BIT )

Value:
{\
uint32_t in = val;\
if (in < 0x10000) {\
tmp = in;\
PUT_16BIT\
} else {\
tmp = 0xD800 | ((in - 0x10000) >> 10);\
PUT_16BIT\
tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
PUT_16BIT\
}\
}\

Convert a 32-bit Unicode character to its UTF-16 encoded form (2 or 4 bytes).

Parameters

  val | is an input-only argument and should be of type uint32_t. It holds a UCS-4 encoded Unicode character that is to be converted to UTF-16. If val is given as a function it is executed only once.
  tmp | is a temporary variable and should be of type uint16_t. It represents an intermediate value during conversion that is to be output by PUT_16BIT.
  PUT_16BIT | writes the converted UTF-16 data to any proper destination in desired endianness. It could be a function or a statement, and uses tmp as the input byte. For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be executed 1 or 2 times depending on input character.

Definition at line 575 of file common.h.

## Function Documentation

## av_log2()

av_const int av_log2 | ( | unsigned | v | ) |

Referenced by av_ceil_log2_c().

## av_log2_16bit()

av_const int av_log2_16bit | ( | unsigned | v | ) |

## av_clip_c()

av_always_inline av_const int av_clip_c | ( | int | a,
|  | int | amin,
|  | int | amax )

static

Clip a signed integer value into the amin-amax range.

Parameters

  a | value to clip
  amin | minimum value of the clip range
  amax | maximum value of the clip range

Returns
  clipped value

Definition at line 178 of file common.h.

## av_clip64_c()

av_always_inline av_const int64_t av_clip64_c | ( | int64_t | a,
|  | int64_t | amin,
|  | int64_t | amax )

static

Clip a signed 64bit integer value into the amin-amax range.

Parameters

  a | value to clip
  amin | minimum value of the clip range
  amax | maximum value of the clip range

Returns
  clipped value

Definition at line 195 of file common.h.

## av_clip_uint8_c()

av_always_inline av_const uint8_t av_clip_uint8_c | ( | int | a | ) |

static

Clip a signed integer value into the 0-255 range.

Parameters

  a | value to clip

Returns
  clipped value

Definition at line 210 of file common.h.

## av_clip_int8_c()

av_always_inline av_const int8_t av_clip_int8_c | ( | int | a | ) |

static

Clip a signed integer value into the -128,127 range.

Parameters

  a | value to clip

Returns
  clipped value

Definition at line 221 of file common.h.

## av_clip_uint16_c()

av_always_inline av_const uint16_t av_clip_uint16_c | ( | int | a | ) |

static

Clip a signed integer value into the 0-65535 range.

Parameters

  a | value to clip

Returns
  clipped value

Definition at line 232 of file common.h.

## av_clip_int16_c()

av_always_inline av_const int16_t av_clip_int16_c | ( | int | a | ) |

static

Clip a signed integer value into the -32768,32767 range.

Parameters

  a | value to clip

Returns
  clipped value

Definition at line 243 of file common.h.

## av_clipl_int32_c()

av_always_inline av_const int32_t av_clipl_int32_c | ( | int64_t | a | ) |

static

Clip a signed 64-bit integer value into the -2147483648,2147483647 range.

Parameters

  a | value to clip

Returns
  clipped value

Definition at line 254 of file common.h.

## av_clip_intp2_c()

av_always_inline av_const int av_clip_intp2_c | ( | int | a,
|  | int | p )

static

Clip a signed integer into the -(2^p),(2^p-1) range.

Parameters

  a | value to clip
  p | bit position to clip at

Returns
  clipped value

Definition at line 266 of file common.h.

## av_clip_uintp2_c()

av_always_inline av_const unsigned av_clip_uintp2_c | ( | int | a,
|  | int | p )

static

Clip a signed integer to an unsigned power of two range.

Parameters

  a | value to clip
  p | bit position to clip at

Returns
  clipped value

Definition at line 280 of file common.h.

## av_zero_extend_c()

av_always_inline av_const unsigned av_zero_extend_c | ( | unsigned | a,
|  | unsigned | p )

static

Clear high bits from an unsigned integer starting with specific bit position.

Parameters

  a | value to clip
  p | bit position to clip at. Must be between 0 and 31.

Returns
  clipped value

Definition at line 292 of file common.h.

## av_sat_add32_c()

av_always_inline int av_sat_add32_c | ( | int | a,
|  | int | b )

static

Add two signed 32-bit values with saturation.

Parameters

  a | one value
  b | another value

Returns
  sum with signed saturation

Definition at line 318 of file common.h.

## av_sat_dadd32_c()

av_always_inline int av_sat_dadd32_c | ( | int | a,
|  | int | b )

static

Add a doubled value to another value with saturation at both stages.

Parameters

  a | first value
  b | value doubled and added to a

Returns
  sum sat(a + sat(2*b)) with signed saturation

Definition at line 330 of file common.h.

## av_sat_sub32_c()

av_always_inline int av_sat_sub32_c | ( | int | a,
|  | int | b )

static

Subtract two signed 32-bit values with saturation.

Parameters

  a | one value
  b | another value

Returns
  difference with signed saturation

Definition at line 342 of file common.h.

## av_sat_dsub32_c()

av_always_inline int av_sat_dsub32_c | ( | int | a,
|  | int | b )

static

Subtract a doubled value from another value with saturation at both stages.

Parameters

  a | first value
  b | value doubled and subtracted from a

Returns
  difference sat(a - sat(2*b)) with signed saturation

Definition at line 354 of file common.h.

## av_sat_add64_c()

av_always_inline int64_t av_sat_add64_c | ( | int64_t | a,
|  | int64_t | b )

static

Add two signed 64-bit values with saturation.

Parameters

  a | one value
  b | another value

Returns
  sum with signed saturation

Definition at line 366 of file common.h.

## av_sat_sub64_c()

av_always_inline int64_t av_sat_sub64_c | ( | int64_t | a,
|  | int64_t | b )

static

Subtract two signed 64-bit values with saturation.

Parameters

  a | one value
  b | another value

Returns
  difference with signed saturation

Definition at line 385 of file common.h.

## av_clipf_c()

av_always_inline av_const float av_clipf_c | ( | float | a,
|  | float | amin,
|  | float | amax )

static

Clip a float value into the amin-amax range.

If a is nan or -inf amin will be returned. If a is +inf amax will be returned.

Parameters

  a | value to clip
  amin | minimum value of the clip range
  amax | maximum value of the clip range

Returns
  clipped value

Definition at line 407 of file common.h.

## av_clipd_c()

av_always_inline av_const double av_clipd_c | ( | double | a,
|  | double | amin,
|  | double | amax )

static

Clip a double value into the amin-amax range.

If a is nan or -inf amin will be returned. If a is +inf amax will be returned.

Parameters

  a | value to clip
  amin | minimum value of the clip range
  amax | maximum value of the clip range

Returns
  clipped value

Definition at line 424 of file common.h.

## av_ceil_log2_c()

av_always_inline av_const int av_ceil_log2_c | ( | int | x | ) |

static

Compute ceil(log2(x)).

Parameters

  x | value used to compute ceil(log2(x))

Returns
  computed ceiling of log2(x)

Definition at line 436 of file common.h.

## av_popcount_c()

av_always_inline av_const int av_popcount_c | ( | uint32_t | x | ) |

static

Count number of bits set to one in x.

Parameters

  x | value to count bits of

Returns
  the number of bits set to one in x

Definition at line 446 of file common.h.

## av_popcount64_c()

av_always_inline av_const int av_popcount64_c | ( | uint64_t | x | ) |

static

Count number of bits set to one in x.

Parameters

  x | value to count bits of

Returns
  the number of bits set to one in x

Definition at line 460 of file common.h.

## av_parity_c()

av_always_inline av_const int av_parity_c | ( | uint32_t | v | ) |

static

Definition at line 465 of file common.h.