FFmpeg
Data Structures | Macros | Enumerations | Functions | Variables
utils.c File Reference
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "checkasm_config.h"
#include "checkasm/test.h"
#include "checkasm/utils.h"
#include "internal.h"

Go to the source code of this file.

Data Structures

union  intfloat
 

Macros

#define RANDOMIZE_DIST(buf, ftype, width, mean, stddev)
 
#define DEF_CHECKASM_INIT_MASK(BITS, PIXEL)
 
#define PRINT_LINE(buf1, buf2, xstart, xend, xpad, fmt, fmtw)
 
#define PRINT_RECT(type, buf1, buf2, ystart, yend, xstart, xend, fmt, fmtw)
 
#define CHECK_RECT(buf1, buf2, ystart, yend, xstart, xend, msg, compare, type, fmt, fmtw)
 
#define DEF_CHECKASM_CHECK_BODY(compare, type, fmt, fmtw)
 
#define cmp_int(a, b, len)   (!memcmp(a, b, (len) * sizeof(*(a))))
 
#define DEF_CHECKASM_CHECK_FUNC(type, fmt, fmtw)
 
#define cmp_float(a, b, len)   float_near_ulp_array(a, b, max_ulp, len)
 

Enumerations

enum  {
  PAT_ZERO, PAT_ONE, PAT_RAND, PAT_LOW,
  PAT_HIGH, PAT_ALTLO, PAT_ALTHI, PAT_MIX
}
 

Functions

NOINLINE void checkasm_noop (void *ptr)
 
static ALWAYS_INLINE uint64_t gettime_nsec (int is_seed)
 
uint64_t checkasm_gettime_nsec (void)
 
uint64_t checkasm_gettime_nsec_diff (uint64_t t)
 
unsigned checkasm_seed (void)
 
void checkasm_srand (unsigned seed)
 
uint32_t checkasm_rand_uint32 (void)
 Generate a random 32-bit unsigned integer. More...
 
int32_t checkasm_rand_int32 (void)
 Generate a random 32-bit signed integer. More...
 
int checkasm_rand (void)
 Generate a random non-negative integer. More...
 
double checkasm_randf (void)
 Generate a random double-precision floating-point number. More...
 
static double marsaglia (double *z2)
 
double checkasm_rand_norm (void)
 Generate a random number from the standard normal distribution. More...
 
double checkasm_rand_dist (CheckasmDist dist)
 Generate a normally distributed random number. More...
 
void checkasm_randomize (void *bufp, size_t bytes)
 Fill a buffer with uniformly chosen random bytes. More...
 
void checkasm_randomize_mask8 (uint8_t *buf, int width, uint8_t mask)
 Fill a uint8_t buffer with random values chosen uniformly within a mask. More...
 
void checkasm_randomize_mask16 (uint16_t *buf, int width, uint16_t mask)
 Fill a uint16_t buffer with random values chosen uniformly within a mask. More...
 
void checkasm_randomize_range (double *buf, int width, double range)
 Fill a double buffer with random values chosen uniformly below a limit. More...
 
void checkasm_randomize_rangef (float *buf, int width, float range)
 Fill a float buffer with random values chosen uniformly below a limit. More...
 
void checkasm_randomize_dist (double *buf, int width, CheckasmDist dist)
 Fill a double buffer with normally distributed random values. More...
 
void checkasm_randomize_distf (float *buf, int width, CheckasmDist dist)
 Fill a float buffer with normally distributed random values. More...
 
void checkasm_randomize_norm (double *buf, int width)
 Fill a double buffer with values from a standard normal distribution. More...
 
void checkasm_randomize_normf (float *buf, int width)
 Fill a float buffer with values from a standard normal distribution. More...
 
void checkasm_clear (void *buf, size_t bytes)
 Clear a buffer to a pre-determined pattern (currently 0xAA) More...
 
void checkasm_clear8 (uint8_t *buf, int width, uint8_t val)
 Fill a uint8_t buffer with a constant value. More...
 
void checkasm_clear16 (uint16_t *buf, int width, uint16_t val)
 Fill a uint16_t buffer with a constant value. More...
 
static int clz (const unsigned int mask)
 
static int shift_rand (int x)
 
void checkasm_init (void *buf, size_t bytes)
 Initialize a buffer with pathological test patterns. More...
 
void checkasm_fprintf (FILE *const f, const int color, const char *const fmt,...)
 
static COLD int should_use_color (FILE *const f)
 
COLD void checkasm_setup_fprintf (void)
 
static int get_terminal_width (void)
 
void checkasm_json (CheckasmJson *json, const char *key, const char *const fmt,...)
 
void checkasm_json_str (CheckasmJson *json, const char *key, const char *str)
 
void checkasm_json_push (CheckasmJson *json, const char *const key, const char type)
 
void checkasm_json_pop (CheckasmJson *json, char type)
 
static int is_negative (const intfloat u)
 
int checkasm_float_near_ulp (const float a, const float b, const unsigned max_ulp)
 Compare floats using ULP (Units in Last Place) tolerance. More...
 
int checkasm_float_near_ulp_array (const float *const a, const float *const b, const unsigned max_ulp, const int len)
 Compare float arrays using ULP tolerance. More...
 
int checkasm_float_near_abs_eps (const float a, const float b, const float eps)
 Compare floats using absolute epsilon tolerance. More...
 
int checkasm_float_near_abs_eps_array (const float *const a, const float *const b, const float eps, const int len)
 Compare float arrays using absolute epsilon tolerance. More...
 
int checkasm_float_near_abs_eps_ulp (const float a, const float b, const float eps, const unsigned max_ulp)
 Compare floats using both epsilon and ULP tolerances. More...
 
int checkasm_float_near_abs_eps_array_ulp (const float *const a, const float *const b, const float eps, const unsigned max_ulp, const int len)
 Compare float arrays using both epsilon and ULP tolerances. More...
 
int checkasm_double_near_abs_eps (const double a, const double b, const double eps)
 Compare doubles using absolute epsilon tolerance. More...
 
int checkasm_double_near_abs_eps_array (const double *const a, const double *const b, const double eps, const unsigned len)
 Compare double arrays using absolute epsilon tolerance. More...
 
static int check_err (const char *const file, const int line, const char *const name, const int w, const int h, int *const err)
 
int checkasm_check_impl_float_ulp (const char *file, int line, const float *buf1, ptrdiff_t stride1, const float *buf2, ptrdiff_t stride2, int w, int h, const char *name, unsigned max_ulp, int align_w, int align_h, int padding)
 Compare float buffers with ULP tolerance. More...
 
char * checkasm_vasprintf (const char *fmt, va_list arg)
 

Variables

static uint32_t xs_state [4]
 
static int use_printf_color [2]
 

Macro Definition Documentation

◆ RANDOMIZE_DIST

#define RANDOMIZE_DIST (   buf,
  ftype,
  width,
  mean,
  stddev 
)
Value:
do { \
if ((width) & 1) { \
*(buf)++ = (ftype) ((mean) + (stddev) * checkasm_rand_norm()); \
(width) ^= 1; \
} \
for (; width; width -= 2) { \
double z1, z2; \
z1 = marsaglia(&z2); \
*(buf)++ = (ftype) ((mean) + (stddev) * z1); \
*(buf)++ = (ftype) ((mean) + (stddev) * z2); \
} \
} while (0)

Definition at line 232 of file utils.c.

◆ DEF_CHECKASM_INIT_MASK

#define DEF_CHECKASM_INIT_MASK (   BITS,
  PIXEL 
)
Value:
void checkasm_init_mask##BITS(PIXEL *buf, const int width, const PIXEL mask_pixel) \
{ \
if (!width) \
return; \
\
int step = 0, mode = 0, mask = mask_pixel; \
for (int i = 0; i < width; i++, step--) { \
if (!step) { \
step = imax(shift_rand(width), 1); \
mode = checkasm_rand() & 7; \
mask = shift_rand(mask_pixel); \
} \
\
const PIXEL low = checkasm_rand_uint32() & mask; \
const PIXEL high = mask_pixel - low; \
switch (mode) { \
case PAT_ZERO: buf[i] = 0; break; \
case PAT_ONE: buf[i] = mask_pixel; break; \
case PAT_RAND: buf[i] = checkasm_rand_uint32() & mask_pixel; break; \
case PAT_LOW: buf[i] = low; break; \
case PAT_HIGH: buf[i] = high; break; \
case PAT_ALTLO: buf[i] = (i & 1) ? high : low; break; \
case PAT_ALTHI: buf[i] = (i & 1) ? low : high; break; \
case PAT_MIX: buf[i] = (checkasm_rand() & 1) ? low : high; break; \
} \
} \
}

Definition at line 331 of file utils.c.

◆ PRINT_LINE

#define PRINT_LINE (   buf1,
  buf2,
  xstart,
  xend,
  xpad,
  fmt,
  fmtw 
)
Value:
do { \
for (int x = xstart; x < xend; x++) { \
if (buf1[x] != buf2[x]) \
checkasm_fprintf(stderr, COLOR_RED, " " fmt, buf1[x]); \
else \
fprintf(stderr, " " fmt, buf1[x]); \
} \
for (int pad = xend; pad < xstart + xpad; pad++) \
fprintf(stderr, &" "[9 - fmtw]); \
} while (0)

Definition at line 596 of file utils.c.

◆ PRINT_RECT

#define PRINT_RECT (   type,
  buf1,
  buf2,
  ystart,
  yend,
  xstart,
  xend,
  fmt,
  fmtw 
)
Value:
do { \
const type *ptr1 = (buf1) + ystart * stride1; \
const type *ptr2 = (buf2) + ystart * stride1; \
const int elem_size = 2 * (fmtw + 1) + 1; \
const int display_elems = imin(term_width / elem_size, xend - xstart); \
for (int y = ystart; y < yend; y++) { \
for (int xpos = xstart; xpos < xend; xpos += display_elems) { \
const int xstep = imin(xpos + display_elems, xend); \
if (xpos == xstart) /* line change */ \
checkasm_fprintf(stderr, COLOR_BLUE, "%3d: ", y); \
else \
fprintf(stderr, " "); \
PRINT_LINE(ptr1, ptr2, xpos, xstep, display_elems, fmt, fmtw); \
fprintf(stderr, " "); \
PRINT_LINE(ptr2, ptr1, xpos, xstep, display_elems, fmt, fmtw); \
fprintf(stderr, " "); \
for (int x = xpos; x < xstep; x++) { \
if (ptr1[x] != ptr2[x]) \
checkasm_fprintf(stderr, COLOR_RED, "x"); \
else \
fprintf(stderr, "."); \
} \
fprintf(stderr, "\n"); \
} \
ptr1 += stride1; \
ptr2 += stride2; \
} \
} while (0)

Definition at line 608 of file utils.c.

◆ CHECK_RECT

#define CHECK_RECT (   buf1,
  buf2,
  ystart,
  yend,
  xstart,
  xend,
  msg,
  compare,
  type,
  fmt,
  fmtw 
)
Value:
do { \
const int xw = xend - xstart; \
for (int y = ystart; y < yend; y++) { \
if (compare(&buf1[y * stride1 + xstart], &buf2[y * stride2 + xstart], xw)) \
continue; \
if (check_err(file, line, name, w, h, &err)) \
return 1; \
/* Exclude unneeded lines on overwrite above */ \
int yprint = y < 0 ? y : ystart; \
if (msg[0]) \
fprintf(stderr, " %s (%dx%d, from idx [%d]):\n", msg, xend - xstart, \
yend - yprint, xstart); \
PRINT_RECT(type, buf1, buf2, yprint, yend, xstart, xend, fmt, fmtw); \
break; \
} \
} while (0)

Definition at line 638 of file utils.c.

◆ DEF_CHECKASM_CHECK_BODY

#define DEF_CHECKASM_CHECK_BODY (   compare,
  type,
  fmt,
  fmtw 
)
Value:
do { \
const int overhead = 5 + 3 + 3; \
const int term_width = get_terminal_width() - overhead; \
const int aligned_w = (w + align_w - 1) & ~(align_w - 1); \
stride1 /= sizeof(type); \
stride2 /= sizeof(type); \
\
int err = 0; \
CHECK_RECT(buf1, buf2, 0, h, 0, w, "", compare, type, fmt, fmtw); \
if (align_h >= 1) { \
const int aligned_h = (h + align_h - 1) & ~(align_h - 1); \
CHECK_RECT(buf1, buf2, -padding, 0, -padding, w + padding, "overwrite top", \
compare, type, fmt, fmtw); \
CHECK_RECT(buf1, buf2, aligned_h, aligned_h + padding, -padding, \
w + padding, "overwrite bottom", compare, type, fmt, fmtw); \
} \
CHECK_RECT(buf1, buf2, 0, h, -padding, 0, "overwrite left", compare, type, fmt, \
fmtw); \
CHECK_RECT(buf1, buf2, 0, h, aligned_w, aligned_w + padding, "overwrite right", \
compare, type, fmt, fmtw); \
return err; \
} while (0)

Definition at line 657 of file utils.c.

◆ cmp_int

#define cmp_int (   a,
  b,
  len 
)    (!memcmp(a, b, (len) * sizeof(*(a))))

Definition at line 681 of file utils.c.

◆ DEF_CHECKASM_CHECK_FUNC

#define DEF_CHECKASM_CHECK_FUNC (   type,
  fmt,
  fmtw 
)
Value:
int checkasm_check_impl_##type(const char *file, int line, const type *buf1, \
ptrdiff_t stride1, const type *buf2, \
ptrdiff_t stride2, int w, int h, const char *name, \
int align_w, int align_h, int padding) \
{ \
DEF_CHECKASM_CHECK_BODY(cmp_int, type, fmt, fmtw); \
}

Definition at line 682 of file utils.c.

◆ cmp_float

#define cmp_float (   a,
  b,
  len 
)    float_near_ulp_array(a, b, max_ulp, len)

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
PAT_ZERO 
PAT_ONE 
PAT_RAND 
PAT_LOW 
PAT_HIGH 
PAT_ALTLO 
PAT_ALTHI 
PAT_MIX 

Definition at line 315 of file utils.c.

Function Documentation

◆ checkasm_noop()

NOINLINE void checkasm_noop ( void *  ptr)

◆ gettime_nsec()

static ALWAYS_INLINE uint64_t gettime_nsec ( int  is_seed)
static

Definition at line 68 of file utils.c.

Referenced by checkasm_gettime_nsec(), checkasm_gettime_nsec_diff(), and checkasm_seed().

◆ checkasm_gettime_nsec()

uint64_t checkasm_gettime_nsec ( void  )

◆ checkasm_gettime_nsec_diff()

uint64_t checkasm_gettime_nsec_diff ( uint64_t  t)

◆ checkasm_seed()

unsigned checkasm_seed ( void  )

Definition at line 117 of file utils.c.

Referenced by checkasm_run().

◆ checkasm_srand()

void checkasm_srand ( unsigned  seed)

Definition at line 127 of file utils.c.

Referenced by check_cpu_flag(), and checkasm_check_key().

◆ marsaglia()

static double marsaglia ( double z2)
inlinestatic

Definition at line 171 of file utils.c.

Referenced by checkasm_rand_norm().

◆ checkasm_rand_norm()

double checkasm_rand_norm ( void  )

Generate a random number from the standard normal distribution.

Returns
Random value from N(0,1) distribution
See also
checkasm_dist_standard

Definition at line 185 of file utils.c.

Referenced by checkasm_rand_dist().

◆ checkasm_rand_dist()

double checkasm_rand_dist ( CheckasmDist  dist)

Generate a normally distributed random number.

Parameters
[in]distDistribution parameters (mean and standard deviation)
Returns
Random value from the specified normal distribution

Definition at line 196 of file utils.c.

◆ clz()

static int clz ( const unsigned int  mask)
inlinestatic

Definition at line 302 of file utils.c.

Referenced by shift_rand().

◆ shift_rand()

static int shift_rand ( int  x)
static

Definition at line 309 of file utils.c.

◆ checkasm_fprintf()

void checkasm_fprintf ( FILE *const  f,
const int  color,
const char *const  fmt,
  ... 
)

Definition at line 366 of file utils.c.

◆ should_use_color()

static COLD int should_use_color ( FILE *const  f)
static

Definition at line 382 of file utils.c.

Referenced by checkasm_setup_fprintf().

◆ checkasm_setup_fprintf()

COLD void checkasm_setup_fprintf ( void  )

Definition at line 404 of file utils.c.

Referenced by checkasm_list_cpu_flags(), and checkasm_run().

◆ get_terminal_width()

static int get_terminal_width ( void  )
static

Definition at line 410 of file utils.c.

◆ checkasm_json()

void checkasm_json ( CheckasmJson json,
const char *  key,
const char *const  fmt,
  ... 
)

Definition at line 428 of file utils.c.

◆ checkasm_json_str()

void checkasm_json_str ( CheckasmJson json,
const char *  key,
const char *  str 
)

Definition at line 444 of file utils.c.

Referenced by cpu_info_json(), json_var(), print_bench_header(), and print_bench_iter().

◆ checkasm_json_push()

void checkasm_json_push ( CheckasmJson json,
const char *const  key,
const char  type 
)

Definition at line 469 of file utils.c.

Referenced by json_measurement(), json_var(), print_bench_header(), and print_bench_iter().

◆ checkasm_json_pop()

void checkasm_json_pop ( CheckasmJson json,
char  type 
)

◆ is_negative()

static int is_negative ( const intfloat  u)
static

Definition at line 504 of file utils.c.

Referenced by checkasm_float_near_ulp().

◆ check_err()

static int check_err ( const char *const  file,
const int  line,
const char *const  name,
const int  w,
const int  h,
int *const  err 
)
static

Definition at line 584 of file utils.c.

◆ checkasm_vasprintf()

char* checkasm_vasprintf ( const char *  fmt,
va_list  arg 
)

Definition at line 711 of file utils.c.

Referenced by checkasm_set_func_variant().

Variable Documentation

◆ xs_state

uint32_t xs_state[4]
static

Definition at line 125 of file utils.c.

Referenced by checkasm_rand_uint32(), and checkasm_srand().

◆ use_printf_color

int use_printf_color[2]
static

Definition at line 363 of file utils.c.

Referenced by checkasm_fprintf(), and checkasm_setup_fprintf().

name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
marsaglia
static double marsaglia(double *z2)
Definition: utils.c:171
PAT_ZERO
@ PAT_ZERO
Definition: utils.c:316
PAT_RAND
@ PAT_RAND
Definition: utils.c:318
PAT_ONE
@ PAT_ONE
Definition: utils.c:317
mask
int mask
Definition: mediacodecdec_common.c:154
mode
Definition: swscale.c:71
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
get_terminal_width
static int get_terminal_width(void)
Definition: utils.c:410
high
int high
Definition: dovi_rpuenc.c:39
check_err
static int check_err(const char *const file, const int line, const char *const name, const int w, const int h, int *const err)
Definition: utils.c:584
imax
static int imax(const int a, const int b)
Definition: internal.h:160
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ftype
#define ftype
Definition: aap_template.c:30
PAT_HIGH
@ PAT_HIGH
Definition: utils.c:320
checkasm_rand_norm
double checkasm_rand_norm(void)
Generate a random number from the standard normal distribution.
Definition: utils.c:185
imin
static int imin(const int a, const int b)
Definition: internal.h:165
BITS
#define BITS
Definition: h264_mb.c:793
PAT_MIX
@ PAT_MIX
Definition: utils.c:323
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
line
Definition: graph2dot.c:48
width
static int width
Definition: utils.c:158
cmp_int
#define cmp_int(a, b, len)
Definition: utils.c:681
shift_rand
static int shift_rand(int x)
Definition: utils.c:309
PAT_ALTLO
@ PAT_ALTLO
Definition: utils.c:321
checkasm_rand
int checkasm_rand(void)
Generate a random non-negative integer.
Definition: utils.c:159
compare
static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
Definition: vf_find_rect.c:114
PIXEL
#define PIXEL
Definition: blend_modes.c:45
mean
static float mean(const float *input, int size)
Definition: vf_nnedi.c:861
checkasm_rand_uint32
uint32_t checkasm_rand_uint32(void)
Generate a random 32-bit unsigned integer.
Definition: utils.c:135
COLOR_BLUE
#define COLOR_BLUE
Definition: internal.h:105
w
uint8_t w
Definition: llvidencdsp.c:39
PAT_ALTHI
@ PAT_ALTHI
Definition: utils.c:322
h
h
Definition: vp9dsp_template.c:2070
PAT_LOW
@ PAT_LOW
Definition: utils.c:319
COLOR_RED
#define COLOR_RED
Definition: internal.h:102