Code clean-up for crc.c, lfg.c, log.c, random_see.d, rational.c and tree.c.

Signed-off-by: Ronald S. Bultje <rsbultje@gmail.com>
This commit is contained in:
Yordan Makariev 2011-12-03 20:25:57 +02:00 committed by Ronald S. Bultje
parent 4a59eca97a
commit 425b45d4b0
6 changed files with 284 additions and 233 deletions

View File

@ -56,7 +56,8 @@ static AVCRC av_crc_table[AV_CRC_MAX][257];
* @param ctx_size size of ctx in bytes * @param ctx_size size of ctx in bytes
* @return <0 on failure * @return <0 on failure
*/ */
int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){ int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
{
unsigned i, j; unsigned i, j;
uint32_t c; uint32_t c;
@ -81,7 +82,8 @@ int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
if (ctx_size >= sizeof(AVCRC) * 1024) if (ctx_size >= sizeof(AVCRC) * 1024)
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
for (j = 0; j < 3; j++) for (j = 0; j < 3; j++)
ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ]; ctx[256 *(j + 1) + i] =
(ctx[256 * j + i] >> 8) ^ ctx[ctx[256 * j + i] & 0xFF];
#endif #endif
return 0; return 0;
@ -92,7 +94,8 @@ int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
* @param crc_id ID of a standard CRC * @param crc_id ID of a standard CRC
* @return a pointer to the CRC table or NULL on failure * @return a pointer to the CRC table or NULL on failure
*/ */
const AVCRC *av_crc_get_table(AVCRCId crc_id){ const AVCRC *av_crc_get_table(AVCRCId crc_id)
{
#if !CONFIG_HARDCODED_TABLES #if !CONFIG_HARDCODED_TABLES
if (!av_crc_table[crc_id][FF_ARRAY_ELEMS(av_crc_table[crc_id]) - 1]) if (!av_crc_table[crc_id][FF_ARRAY_ELEMS(av_crc_table[crc_id]) - 1])
if (av_crc_init(av_crc_table[crc_id], if (av_crc_init(av_crc_table[crc_id],
@ -112,7 +115,9 @@ const AVCRC *av_crc_get_table(AVCRCId crc_id){
* *
* @see av_crc_init() "le" parameter * @see av_crc_init() "le" parameter
*/ */
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length){ uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
const uint8_t *buffer, size_t length)
{
const uint8_t *end = buffer + length; const uint8_t *end = buffer + length;
#if !CONFIG_SMALL #if !CONFIG_SMALL
@ -122,10 +127,10 @@ uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t le
while (buffer < end - 3) { while (buffer < end - 3) {
crc ^= av_le2ne32(*(const uint32_t *) buffer); buffer += 4; crc ^= av_le2ne32(*(const uint32_t *) buffer); buffer += 4;
crc = ctx[3*256 + ( crc &0xFF)] crc = ctx[3 * 256 + ( crc & 0xFF)] ^
^ctx[2*256 + ((crc>>8 )&0xFF)] ctx[2 * 256 + ((crc >> 8 ) & 0xFF)] ^
^ctx[1*256 + ((crc>>16)&0xFF)] ctx[1 * 256 + ((crc >> 16) & 0xFF)] ^
^ctx[0*256 + ((crc>>24) )]; ctx[0 * 256 + ((crc >> 24) )];
} }
} }
#endif #endif
@ -137,13 +142,15 @@ uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t le
#ifdef TEST #ifdef TEST
#undef printf #undef printf
int main(void){ int main(void)
{
uint8_t buf[1999]; uint8_t buf[1999];
int i; int i;
int p[4][3] = { { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 }, int p[4][3] = { { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 },
{ AV_CRC_32_IEEE , 0x04C11DB7, 0xC0F5BAE0 }, { AV_CRC_32_IEEE , 0x04C11DB7, 0xC0F5BAE0 },
{ AV_CRC_16_ANSI , 0x8005 , 0x1FBB }, { AV_CRC_16_ANSI , 0x8005 , 0x1FBB },
{AV_CRC_8_ATM , 0x07, 0xE3 },}; { AV_CRC_8_ATM , 0x07 , 0xE3 }
};
const AVCRC *ctx; const AVCRC *ctx;
for (i = 0; i < sizeof(buf); i++) for (i = 0; i < sizeof(buf); i++)

View File

@ -27,12 +27,14 @@
#include "intreadwrite.h" #include "intreadwrite.h"
#include "attributes.h" #include "attributes.h"
void av_cold av_lfg_init(AVLFG *c, unsigned int seed){ void av_cold av_lfg_init(AVLFG *c, unsigned int seed)
{
uint8_t tmp[16] = { 0 }; uint8_t tmp[16] = { 0 };
int i; int i;
for (i = 8; i < 64; i += 4) { for (i = 8; i < 64; i += 4) {
AV_WL32(tmp, seed); tmp[4]=i; AV_WL32(tmp, seed);
tmp[4] = i;
av_md5_sum(tmp, tmp, 16); av_md5_sum(tmp, tmp, 16);
c->state[i ] = AV_RL32(tmp); c->state[i ] = AV_RL32(tmp);
c->state[i + 1] = AV_RL32(tmp + 4); c->state[i + 1] = AV_RL32(tmp + 4);

View File

@ -48,12 +48,14 @@ static const uint8_t color[]={0x41,0x41,0x11,0x03,9,9,9};
static int use_color = -1; static int use_color = -1;
#undef fprintf #undef fprintf
static void colored_fputs(int level, const char *str){ static void colored_fputs(int level, const char *str)
{
if (use_color < 0) { if (use_color < 0) {
#if defined(_WIN32) && !defined(__MINGW32CE__) #if defined(_WIN32) && !defined(__MINGW32CE__)
CONSOLE_SCREEN_BUFFER_INFO con_info; CONSOLE_SCREEN_BUFFER_INFO con_info;
con = GetStdHandle(STD_ERROR_HANDLE); con = GetStdHandle(STD_ERROR_HANDLE);
use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR"); use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
!getenv("AV_LOG_FORCE_NOCOLOR");
if (use_color) { if (use_color) {
GetConsoleScreenBufferInfo(con, &con_info); GetConsoleScreenBufferInfo(con, &con_info);
attr_orig = con_info.wAttributes; attr_orig = con_info.wAttributes;
@ -61,9 +63,11 @@ static void colored_fputs(int level, const char *str){
} }
#elif HAVE_ISATTY #elif HAVE_ISATTY
use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") && use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
(getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR")); (getenv("TERM") && isatty(2) ||
getenv("AV_LOG_FORCE_COLOR"));
#else #else
use_color= getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR"); use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
!getenv("AV_LOG_FORCE_NOCOLOR");
#endif #endif
} }
@ -76,7 +80,8 @@ static void colored_fputs(int level, const char *str){
} }
} }
const char* av_default_item_name(void* ptr){ const char *av_default_item_name(void *ptr)
{
return (*(AVClass **) ptr)->class_name; return (*(AVClass **) ptr)->class_name;
} }
@ -94,12 +99,15 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
#undef fprintf #undef fprintf
if (print_prefix && avc) { if (print_prefix && avc) {
if (avc->parent_log_context_offset) { if (avc->parent_log_context_offset) {
AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset); AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
avc->parent_log_context_offset);
if (parent && *parent) { if (parent && *parent) {
snprintf(line, sizeof(line), "[%s @ %p] ", (*parent)->item_name(parent), parent); snprintf(line, sizeof(line), "[%s @ %p] ",
(*parent)->item_name(parent), parent);
} }
} }
snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr); snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
avc->item_name(ptr), ptr);
} }
vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl); vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
@ -107,10 +115,12 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
print_prefix = strlen(line) && line[strlen(line) - 1] == '\n'; print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
#if HAVE_ISATTY #if HAVE_ISATTY
if(!is_atty) is_atty= isatty(2) ? 1 : -1; if (!is_atty)
is_atty = isatty(2) ? 1 : -1;
#endif #endif
if(print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strncmp(line, prev, sizeof line)){ if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
!strncmp(line, prev, sizeof line)) {
count++; count++;
if (is_atty == 1) if (is_atty == 1)
fprintf(stderr, " Last message repeated %d times\r", count); fprintf(stderr, " Last message repeated %d times\r", count);
@ -124,14 +134,16 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
av_strlcpy(prev, line, sizeof line); av_strlcpy(prev, line, sizeof line);
} }
static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback; static void (*av_log_callback)(void*, int, const char*, va_list) =
av_log_default_callback;
void av_log(void* avcl, int level, const char *fmt, ...) void av_log(void* avcl, int level, const char *fmt, ...)
{ {
AVClass* avc = avcl ? *(AVClass **) avcl : NULL; AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
va_list vl; va_list vl;
va_start(vl, fmt); va_start(vl, fmt);
if(avc && avc->version >= (50<<16 | 15<<8 | 2) && avc->log_level_offset_offset && level>=AV_LOG_FATAL) if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
avc->log_level_offset_offset && level >= AV_LOG_FATAL)
level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset); level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
av_vlog(avcl, level, fmt, vl); av_vlog(avcl, level, fmt, vl);
va_end(vl); va_end(vl);

View File

@ -33,7 +33,9 @@
#include "mathematics.h" #include "mathematics.h"
#include "rational.h" #include "rational.h"
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max){ int av_reduce(int *dst_num, int *dst_den,
int64_t num, int64_t den, int64_t max)
{
AVRational a0 = { 0, 1 }, a1 = { 1, 0 }; AVRational a0 = { 0, 1 }, a1 = { 1, 0 };
int sign = (num < 0) ^ (den < 0); int sign = (num < 0) ^ (den < 0);
int64_t gcd = av_gcd(FFABS(num), FFABS(den)); int64_t gcd = av_gcd(FFABS(num), FFABS(den));
@ -75,25 +77,34 @@ int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
return den == 0; return den == 0;
} }
AVRational av_mul_q(AVRational b, AVRational c){ AVRational av_mul_q(AVRational b, AVRational c)
av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX); {
av_reduce(&b.num, &b.den,
b.num * (int64_t) c.num,
b.den * (int64_t) c.den, INT_MAX);
return b; return b;
} }
AVRational av_div_q(AVRational b, AVRational c){ AVRational av_div_q(AVRational b, AVRational c)
{
return av_mul_q(b, (AVRational) { c.den, c.num }); return av_mul_q(b, (AVRational) { c.den, c.num });
} }
AVRational av_add_q(AVRational b, AVRational c) { AVRational av_add_q(AVRational b, AVRational c) {
av_reduce(&b.num, &b.den, b.num * (int64_t)c.den + c.num * (int64_t)b.den, b.den * (int64_t)c.den, INT_MAX); av_reduce(&b.num, &b.den,
b.num * (int64_t) c.den +
c.num * (int64_t) b.den,
b.den * (int64_t) c.den, INT_MAX);
return b; return b;
} }
AVRational av_sub_q(AVRational b, AVRational c){ AVRational av_sub_q(AVRational b, AVRational c)
{
return av_add_q(b, (AVRational) { -c.num, c.den }); return av_add_q(b, (AVRational) { -c.num, c.den });
} }
AVRational av_d2q(double d, int max){ AVRational av_d2q(double d, int max)
{
AVRational a; AVRational a;
#define LOG2 0.69314718055994530941723212145817656807550013436025 #define LOG2 0.69314718055994530941723212145817656807550013436025
int exponent; int exponent;
@ -143,11 +154,14 @@ int main(void)
for (b.num = -2; b.num <= 2; b.num++) { for (b.num = -2; b.num <= 2; b.num++) {
for (b.den = -2; b.den <= 2; b.den++) { for (b.den = -2; b.den <= 2; b.den++) {
int c = av_cmp_q(a,b); int c = av_cmp_q(a,b);
double d= av_q2d(a) == av_q2d(b) ? 0 : (av_q2d(a) - av_q2d(b)); double d = av_q2d(a) == av_q2d(b) ?
0 : (av_q2d(a) - av_q2d(b));
if (d > 0) d = 1; if (d > 0) d = 1;
else if (d < 0) d = -1; else if (d < 0) d = -1;
else if (d != d) d = INT_MIN; else if (d != d) d = INT_MIN;
if(c!=d) av_log(0, AV_LOG_ERROR, "%d/%d %d/%d, %d %f\n", a.num, a.den, b.num, b.den, c,d); if (c != d)
av_log(0, AV_LOG_ERROR, "%d/%d %d/%d, %d %f\n", a.num,
a.den, b.num, b.den, c,d);
} }
} }
} }

View File

@ -29,7 +29,9 @@ typedef struct AVTreeNode{
const int av_tree_node_size = sizeof(AVTreeNode); const int av_tree_node_size = sizeof(AVTreeNode);
void *av_tree_find(const AVTreeNode *t, void *key, int (*cmp)(void *key, const void *b), void *next[2]){ void *av_tree_find(const AVTreeNode *t, void *key,
int (*cmp)(void *key, const void *b), void *next[2])
{
if (t) { if (t) {
unsigned int v = cmp(key, t->elem); unsigned int v = cmp(key, t->elem);
if (v) { if (v) {
@ -46,7 +48,9 @@ void *av_tree_find(const AVTreeNode *t, void *key, int (*cmp)(void *key, const v
return NULL; return NULL;
} }
void *av_tree_insert(AVTreeNode **tp, void *key, int (*cmp)(void *key, const void *b), AVTreeNode **next){ void *av_tree_insert(AVTreeNode **tp, void *key,
int (*cmp)(void *key, const void *b), AVTreeNode **next)
{
AVTreeNode *t = *tp; AVTreeNode *t = *tp;
if (t) { if (t) {
unsigned int v = cmp(t->elem, key); unsigned int v = cmp(t->elem, key);
@ -117,7 +121,8 @@ void *av_tree_insert(AVTreeNode **tp, void *key, int (*cmp)(void *key, const voi
} }
return ret; return ret;
} else { } else {
*tp= *next; *next= NULL; *tp = *next;
*next = NULL;
if (*tp) { if (*tp) {
(*tp)->elem = key; (*tp)->elem = key;
return NULL; return NULL;
@ -126,7 +131,8 @@ void *av_tree_insert(AVTreeNode **tp, void *key, int (*cmp)(void *key, const voi
} }
} }
void av_tree_destroy(AVTreeNode *t){ void av_tree_destroy(AVTreeNode *t)
{
if (t) { if (t) {
av_tree_destroy(t->child[0]); av_tree_destroy(t->child[0]);
av_tree_destroy(t->child[1]); av_tree_destroy(t->child[1]);
@ -134,12 +140,18 @@ void av_tree_destroy(AVTreeNode *t){
} }
} }
void av_tree_enumerate(AVTreeNode *t, void *opaque, int (*cmp)(void *opaque, void *elem), int (*enu)(void *opaque, void *elem)){ void av_tree_enumerate(AVTreeNode *t, void *opaque,
int (*cmp)(void *opaque, void *elem),
int (*enu)(void *opaque, void *elem))
{
if (t) { if (t) {
int v = cmp ? cmp(opaque, t->elem) : 0; int v = cmp ? cmp(opaque, t->elem) : 0;
if(v>=0) av_tree_enumerate(t->child[0], opaque, cmp, enu); if (v >= 0)
if(v==0) enu(opaque, t->elem); av_tree_enumerate(t->child[0], opaque, cmp, enu);
if(v<=0) av_tree_enumerate(t->child[1], opaque, cmp, enu); if (v == 0)
enu(opaque, t->elem);
if (v <= 0)
av_tree_enumerate(t->child[1], opaque, cmp, enu);
} }
} }
@ -147,7 +159,8 @@ void av_tree_enumerate(AVTreeNode *t, void *opaque, int (*cmp)(void *opaque, voi
#include "lfg.h" #include "lfg.h"
static int check(AVTreeNode *t){ static int check(AVTreeNode *t)
{
if (t) { if (t) {
int left = check(t->child[0]); int left = check(t->child[0]);
int right = check(t->child[1]); int right = check(t->child[1]);
@ -163,7 +176,8 @@ static int check(AVTreeNode *t){
return 0; return 0;
} }
static void print(AVTreeNode *t, int depth){ static void print(AVTreeNode *t, int depth)
{
int i; int i;
for (i = 0; i < depth * 4; i++) av_log(NULL, AV_LOG_ERROR, " "); for (i = 0; i < depth * 4; i++) av_log(NULL, AV_LOG_ERROR, " ");
if (t) { if (t) {
@ -174,11 +188,13 @@ static void print(AVTreeNode *t, int depth){
av_log(NULL, AV_LOG_ERROR, "NULL\n"); av_log(NULL, AV_LOG_ERROR, "NULL\n");
} }
static int cmp(void *a, const void *b){ static int cmp(void *a, const void *b)
{
return (uint8_t *) a - (const uint8_t *) b; return (uint8_t *) a - (const uint8_t *) b;
} }
int main(void){ int main (void)
{
int i; int i;
void *k; void *k;
AVTreeNode *root = NULL, *node = NULL; AVTreeNode *root = NULL, *node = NULL;