Browse code

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>

Yordan Makariev authored on 2011/12/04 03:25:57
Showing 6 changed files
... ...
@@ -56,32 +56,34 @@ static AVCRC av_crc_table[AV_CRC_MAX][257];
56 56
  * @param ctx_size size of ctx in bytes
57 57
  * @return <0 on failure
58 58
  */
59
-int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
59
+int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
60
+{
60 61
     unsigned i, j;
61 62
     uint32_t c;
62 63
 
63
-    if (bits < 8 || bits > 32 || poly >= (1LL<<bits))
64
+    if (bits < 8 || bits > 32 || poly >= (1LL << bits))
64 65
         return -1;
65
-    if (ctx_size != sizeof(AVCRC)*257 && ctx_size != sizeof(AVCRC)*1024)
66
+    if (ctx_size != sizeof(AVCRC) * 257 && ctx_size != sizeof(AVCRC) * 1024)
66 67
         return -1;
67 68
 
68 69
     for (i = 0; i < 256; i++) {
69 70
         if (le) {
70 71
             for (c = i, j = 0; j < 8; j++)
71
-                c = (c>>1)^(poly & (-(c&1)));
72
+                c = (c >> 1) ^ (poly & (-(c & 1)));
72 73
             ctx[i] = c;
73 74
         } else {
74 75
             for (c = i << 24, j = 0; j < 8; j++)
75
-                c = (c<<1) ^ ((poly<<(32-bits)) & (((int32_t)c)>>31) );
76
+                c = (c << 1) ^ ((poly << (32 - bits)) & (((int32_t) c) >> 31));
76 77
             ctx[i] = av_bswap32(c);
77 78
         }
78 79
     }
79
-    ctx[256]=1;
80
+    ctx[256] = 1;
80 81
 #if !CONFIG_SMALL
81
-    if(ctx_size >= sizeof(AVCRC)*1024)
82
+    if (ctx_size >= sizeof(AVCRC) * 1024)
82 83
         for (i = 0; i < 256; i++)
83
-            for(j=0; j<3; j++)
84
-                ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ];
84
+            for (j = 0; j < 3; j++)
85
+                ctx[256 *(j + 1) + i] =
86
+                    (ctx[256 * j + i] >> 8) ^ ctx[ctx[256 * j + i] & 0xFF];
85 87
 #endif
86 88
 
87 89
     return 0;
... ...
@@ -92,9 +94,10 @@ int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
92 92
  * @param crc_id ID of a standard CRC
93 93
  * @return a pointer to the CRC table or NULL on failure
94 94
  */
95
-const AVCRC *av_crc_get_table(AVCRCId crc_id){
95
+const AVCRC *av_crc_get_table(AVCRCId crc_id)
96
+{
96 97
 #if !CONFIG_HARDCODED_TABLES
97
-    if (!av_crc_table[crc_id][FF_ARRAY_ELEMS(av_crc_table[crc_id])-1])
98
+    if (!av_crc_table[crc_id][FF_ARRAY_ELEMS(av_crc_table[crc_id]) - 1])
98 99
         if (av_crc_init(av_crc_table[crc_id],
99 100
                         av_crc_table_params[crc_id].le,
100 101
                         av_crc_table_params[crc_id].bits,
... ...
@@ -112,46 +115,50 @@ const AVCRC *av_crc_get_table(AVCRCId crc_id){
112 112
  *
113 113
  * @see av_crc_init() "le" parameter
114 114
  */
115
-uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length){
116
-    const uint8_t *end= buffer+length;
115
+uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
116
+                const uint8_t *buffer, size_t length)
117
+{
118
+    const uint8_t *end = buffer + length;
117 119
 
118 120
 #if !CONFIG_SMALL
119
-    if(!ctx[256]) {
120
-        while(((intptr_t) buffer & 3) && buffer < end)
121
-            crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
121
+    if (!ctx[256]) {
122
+        while (((intptr_t) buffer & 3) && buffer < end)
123
+            crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
122 124
 
123
-        while(buffer<end-3){
124
-            crc ^= av_le2ne32(*(const uint32_t*)buffer); buffer+=4;
125
-            crc =  ctx[3*256 + ( crc     &0xFF)]
126
-                  ^ctx[2*256 + ((crc>>8 )&0xFF)]
127
-                  ^ctx[1*256 + ((crc>>16)&0xFF)]
128
-                  ^ctx[0*256 + ((crc>>24)     )];
125
+        while (buffer < end - 3) {
126
+            crc ^= av_le2ne32(*(const uint32_t *) buffer); buffer += 4;
127
+            crc = ctx[3 * 256 + ( crc        & 0xFF)] ^
128
+                  ctx[2 * 256 + ((crc >> 8 ) & 0xFF)] ^
129
+                  ctx[1 * 256 + ((crc >> 16) & 0xFF)] ^
130
+                  ctx[0 * 256 + ((crc >> 24)       )];
129 131
         }
130 132
     }
131 133
 #endif
132
-    while(buffer<end)
133
-        crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
134
+    while (buffer < end)
135
+        crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
134 136
 
135 137
     return crc;
136 138
 }
137 139
 
138 140
 #ifdef TEST
139 141
 #undef printf
140
-int main(void){
142
+int main(void)
143
+{
141 144
     uint8_t buf[1999];
142 145
     int i;
143
-    int p[4][3]={{AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04},
144
-                 {AV_CRC_32_IEEE   , 0x04C11DB7, 0xC0F5BAE0},
145
-                 {AV_CRC_16_ANSI   , 0x8005,     0x1FBB    },
146
-                 {AV_CRC_8_ATM     , 0x07,       0xE3      },};
146
+    int p[4][3] = { { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 },
147
+                    { AV_CRC_32_IEEE   , 0x04C11DB7, 0xC0F5BAE0 },
148
+                    { AV_CRC_16_ANSI   , 0x8005    , 0x1FBB     },
149
+                    { AV_CRC_8_ATM     , 0x07      , 0xE3       }
150
+    };
147 151
     const AVCRC *ctx;
148 152
 
149
-    for(i=0; i<sizeof(buf); i++)
150
-        buf[i]= i+i*i;
153
+    for (i = 0; i < sizeof(buf); i++)
154
+        buf[i] = i + i * i;
151 155
 
152
-    for(i=0; i<4; i++){
156
+    for (i = 0; i < 4; i++) {
153 157
         ctx = av_crc_get_table(p[i][0]);
154
-        printf("crc %08X =%X\n", p[i][1], av_crc(ctx, 0, buf, sizeof(buf)));
158
+        printf("crc %08X = %X\n", p[i][1], av_crc(ctx, 0, buf, sizeof(buf)));
155 159
     }
156 160
     return 0;
157 161
 }
... ...
@@ -27,19 +27,21 @@
27 27
 #include "intreadwrite.h"
28 28
 #include "attributes.h"
29 29
 
30
-void av_cold av_lfg_init(AVLFG *c, unsigned int seed){
31
-    uint8_t tmp[16]={0};
30
+void av_cold av_lfg_init(AVLFG *c, unsigned int seed)
31
+{
32
+    uint8_t tmp[16] = { 0 };
32 33
     int i;
33 34
 
34
-    for(i=8; i<64; i+=4){
35
-        AV_WL32(tmp, seed); tmp[4]=i;
36
-        av_md5_sum(tmp, tmp,  16);
37
-        c->state[i  ]= AV_RL32(tmp);
38
-        c->state[i+1]= AV_RL32(tmp+4);
39
-        c->state[i+2]= AV_RL32(tmp+8);
40
-        c->state[i+3]= AV_RL32(tmp+12);
35
+    for (i = 8; i < 64; i += 4) {
36
+        AV_WL32(tmp, seed);
37
+        tmp[4] = i;
38
+        av_md5_sum(tmp, tmp, 16);
39
+        c->state[i    ] = AV_RL32(tmp);
40
+        c->state[i + 1] = AV_RL32(tmp + 4);
41
+        c->state[i + 2] = AV_RL32(tmp + 8);
42
+        c->state[i + 3] = AV_RL32(tmp + 12);
41 43
     }
42
-    c->index=0;
44
+    c->index = 0;
43 45
 }
44 46
 
45 47
 void av_bmg_get(AVLFG *lfg, double out[2])
... ...
@@ -47,9 +49,9 @@ void av_bmg_get(AVLFG *lfg, double out[2])
47 47
     double x1, x2, w;
48 48
 
49 49
     do {
50
-        x1 = 2.0/UINT_MAX*av_lfg_get(lfg) - 1.0;
51
-        x2 = 2.0/UINT_MAX*av_lfg_get(lfg) - 1.0;
52
-        w = x1*x1 + x2*x2;
50
+        x1 = 2.0 / UINT_MAX * av_lfg_get(lfg) - 1.0;
51
+        x2 = 2.0 / UINT_MAX * av_lfg_get(lfg) - 1.0;
52
+        w  = x1 * x1 + x2 * x2;
53 53
     } while (w >= 1.0);
54 54
 
55 55
     w = sqrt((-2.0 * log(w)) / w);
... ...
@@ -63,7 +65,7 @@ void av_bmg_get(AVLFG *lfg, double out[2])
63 63
 
64 64
 int main(void)
65 65
 {
66
-    int x=0;
66
+    int x = 0;
67 67
     int i, j;
68 68
     AVLFG state;
69 69
 
... ...
@@ -71,8 +73,8 @@ int main(void)
71 71
     for (j = 0; j < 10000; j++) {
72 72
         START_TIMER
73 73
         for (i = 0; i < 624; i++) {
74
-//            av_log(NULL,AV_LOG_ERROR, "%X\n", av_lfg_get(&state));
75
-            x+=av_lfg_get(&state);
74
+            //av_log(NULL, AV_LOG_ERROR, "%X\n", av_lfg_get(&state));
75
+            x += av_lfg_get(&state);
76 76
         }
77 77
         STOP_TIMER("624 calls of av_lfg_get");
78 78
     }
... ...
@@ -35,104 +35,116 @@ static int flags;
35 35
 
36 36
 #if defined(_WIN32) && !defined(__MINGW32CE__)
37 37
 #include <windows.h>
38
-static const uint8_t color[] = {12,12,12,14,7,7,7};
38
+static const uint8_t color[] = { 12, 12, 12, 14, 7, 7, 7 };
39 39
 static int16_t background, attr_orig;
40 40
 static HANDLE con;
41 41
 #define set_color(x)  SetConsoleTextAttribute(con, background | color[x])
42 42
 #define reset_color() SetConsoleTextAttribute(con, attr_orig)
43 43
 #else
44
-static const uint8_t color[]={0x41,0x41,0x11,0x03,9,9,9};
45
-#define set_color(x)  fprintf(stderr, "\033[%d;3%dm", color[x]>>4, color[x]&15)
44
+static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 9, 9 };
45
+#define set_color(x)  fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
46 46
 #define reset_color() fprintf(stderr, "\033[0m")
47 47
 #endif
48
-static int use_color=-1;
48
+static int use_color = -1;
49 49
 
50 50
 #undef fprintf
51
-static void colored_fputs(int level, const char *str){
52
-    if(use_color<0){
51
+static void colored_fputs(int level, const char *str)
52
+{
53
+    if (use_color < 0) {
53 54
 #if defined(_WIN32) && !defined(__MINGW32CE__)
54 55
         CONSOLE_SCREEN_BUFFER_INFO con_info;
55 56
         con = GetStdHandle(STD_ERROR_HANDLE);
56
-        use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR");
57
+        use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
58
+                    !getenv("AV_LOG_FORCE_NOCOLOR");
57 59
         if (use_color) {
58 60
             GetConsoleScreenBufferInfo(con, &con_info);
59 61
             attr_orig  = con_info.wAttributes;
60 62
             background = attr_orig & 0xF0;
61 63
         }
62 64
 #elif HAVE_ISATTY
63
-        use_color= !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
64
-            (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
65
+        use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
66
+                    (getenv("TERM") && isatty(2) ||
67
+                     getenv("AV_LOG_FORCE_COLOR"));
65 68
 #else
66
-        use_color= getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR");
69
+        use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
70
+                   !getenv("AV_LOG_FORCE_NOCOLOR");
67 71
 #endif
68 72
     }
69 73
 
70
-    if(use_color){
74
+    if (use_color) {
71 75
         set_color(level);
72 76
     }
73 77
     fputs(str, stderr);
74
-    if(use_color){
78
+    if (use_color) {
75 79
         reset_color();
76 80
     }
77 81
 }
78 82
 
79
-const char* av_default_item_name(void* ptr){
80
-    return (*(AVClass**)ptr)->class_name;
83
+const char *av_default_item_name(void *ptr)
84
+{
85
+    return (*(AVClass **) ptr)->class_name;
81 86
 }
82 87
 
83 88
 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
84 89
 {
85
-    static int print_prefix=1;
90
+    static int print_prefix = 1;
86 91
     static int count;
87 92
     static char prev[1024];
88 93
     char line[1024];
89 94
     static int is_atty;
90
-    AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
91
-    if(level>av_log_level)
95
+    AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
96
+    if (level > av_log_level)
92 97
         return;
93
-    line[0]=0;
98
+    line[0] = 0;
94 99
 #undef fprintf
95
-    if(print_prefix && avc) {
100
+    if (print_prefix && avc) {
96 101
         if (avc->parent_log_context_offset) {
97
-            AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
98
-            if(parent && *parent){
99
-                snprintf(line, sizeof(line), "[%s @ %p] ", (*parent)->item_name(parent), parent);
102
+            AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
103
+                                   avc->parent_log_context_offset);
104
+            if (parent && *parent) {
105
+                snprintf(line, sizeof(line), "[%s @ %p] ",
106
+                         (*parent)->item_name(parent), parent);
100 107
             }
101 108
         }
102
-        snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
109
+        snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
110
+                 avc->item_name(ptr), ptr);
103 111
     }
104 112
 
105 113
     vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
106 114
 
107
-    print_prefix = strlen(line) && line[strlen(line)-1] == '\n';
115
+    print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
108 116
 
109 117
 #if HAVE_ISATTY
110
-    if(!is_atty) is_atty= isatty(2) ? 1 : -1;
118
+    if (!is_atty)
119
+        is_atty = isatty(2) ? 1 : -1;
111 120
 #endif
112 121
 
113
-    if(print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strncmp(line, prev, sizeof line)){
122
+    if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
123
+        !strncmp(line, prev, sizeof line)) {
114 124
         count++;
115
-        if(is_atty==1)
125
+        if (is_atty == 1)
116 126
             fprintf(stderr, "    Last message repeated %d times\r", count);
117 127
         return;
118 128
     }
119
-    if(count>0){
129
+    if (count > 0) {
120 130
         fprintf(stderr, "    Last message repeated %d times\n", count);
121
-        count=0;
131
+        count = 0;
122 132
     }
123
-    colored_fputs(av_clip(level>>3, 0, 6), line);
133
+    colored_fputs(av_clip(level >> 3, 0, 6), line);
124 134
     av_strlcpy(prev, line, sizeof line);
125 135
 }
126 136
 
127
-static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
137
+static void (*av_log_callback)(void*, int, const char*, va_list) =
138
+    av_log_default_callback;
128 139
 
129 140
 void av_log(void* avcl, int level, const char *fmt, ...)
130 141
 {
131
-    AVClass* avc= avcl ? *(AVClass**)avcl : NULL;
142
+    AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
132 143
     va_list vl;
133 144
     va_start(vl, fmt);
134
-    if(avc && avc->version >= (50<<16 | 15<<8 | 2) && avc->log_level_offset_offset && level>=AV_LOG_FATAL)
135
-        level += *(int*)(((uint8_t*)avcl) + avc->log_level_offset_offset);
145
+    if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
146
+        avc->log_level_offset_offset && level >= AV_LOG_FATAL)
147
+        level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
136 148
     av_vlog(avcl, level, fmt, vl);
137 149
     va_end(vl);
138 150
 }
... ...
@@ -154,7 +166,7 @@ void av_log_set_level(int level)
154 154
 
155 155
 void av_log_set_flags(int arg)
156 156
 {
157
-    flags= arg;
157
+    flags = arg;
158 158
 }
159 159
 
160 160
 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
... ...
@@ -40,24 +40,24 @@ static int read_random(uint32_t *dst, const char *file)
40 40
 
41 41
 static uint32_t get_generic_seed(void)
42 42
 {
43
-    clock_t last_t=0;
44
-    int bits=0;
45
-    uint64_t random=0;
43
+    clock_t last_t  = 0;
44
+    int bits        = 0;
45
+    uint64_t random = 0;
46 46
     unsigned i;
47
-    float s=0.000000000001;
47
+    float s = 0.000000000001;
48 48
 
49
-    for(i=0;bits<64;i++){
50
-        clock_t t= clock();
51
-        if(last_t && fabs(t-last_t)>s || t==(clock_t)-1){
52
-            if(i<10000 && s<(1<<24)){
53
-                s+=s;
54
-                i=t=0;
55
-            }else{
56
-                random= 2*random + (i&1);
49
+    for (i = 0; bits < 64; i++) {
50
+        clock_t t = clock();
51
+        if (last_t && fabs(t - last_t) > s || t == (clock_t) -1) {
52
+            if (i < 10000 && s < (1 << 24)) {
53
+                s += s;
54
+                i = t = 0;
55
+            } else {
56
+                random = 2 * random + (i & 1);
57 57
                 bits++;
58 58
             }
59 59
         }
60
-        last_t= t;
60
+        last_t = t;
61 61
     }
62 62
 #ifdef AV_READ_TIME
63 63
     random ^= AV_READ_TIME();
... ...
@@ -65,7 +65,7 @@ static uint32_t get_generic_seed(void)
65 65
     random ^= clock();
66 66
 #endif
67 67
 
68
-    random += random>>32;
68
+    random += random >> 32;
69 69
 
70 70
     return random;
71 71
 }
... ...
@@ -33,75 +33,86 @@
33 33
 #include "mathematics.h"
34 34
 #include "rational.h"
35 35
 
36
-int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max){
37
-    AVRational a0={0,1}, a1={1,0};
38
-    int sign= (num<0) ^ (den<0);
39
-    int64_t gcd= av_gcd(FFABS(num), FFABS(den));
40
-
41
-    if(gcd){
42
-        num = FFABS(num)/gcd;
43
-        den = FFABS(den)/gcd;
36
+int av_reduce(int *dst_num, int *dst_den,
37
+              int64_t num, int64_t den, int64_t max)
38
+{
39
+    AVRational a0 = { 0, 1 }, a1 = { 1, 0 };
40
+    int sign = (num < 0) ^ (den < 0);
41
+    int64_t gcd = av_gcd(FFABS(num), FFABS(den));
42
+
43
+    if (gcd) {
44
+        num = FFABS(num) / gcd;
45
+        den = FFABS(den) / gcd;
44 46
     }
45
-    if(num<=max && den<=max){
46
-        a1= (AVRational){num, den};
47
-        den=0;
47
+    if (num <= max && den <= max) {
48
+        a1 = (AVRational) { num, den };
49
+        den = 0;
48 50
     }
49 51
 
50
-    while(den){
51
-        uint64_t x      = num / den;
52
-        int64_t next_den= num - den*x;
53
-        int64_t a2n= x*a1.num + a0.num;
54
-        int64_t a2d= x*a1.den + a0.den;
52
+    while (den) {
53
+        uint64_t x        = num / den;
54
+        int64_t next_den  = num - den * x;
55
+        int64_t a2n       = x * a1.num + a0.num;
56
+        int64_t a2d       = x * a1.den + a0.den;
55 57
 
56
-        if(a2n > max || a2d > max){
57
-            if(a1.num) x= (max - a0.num) / a1.num;
58
-            if(a1.den) x= FFMIN(x, (max - a0.den) / a1.den);
58
+        if (a2n > max || a2d > max) {
59
+            if (a1.num) x =          (max - a0.num) / a1.num;
60
+            if (a1.den) x = FFMIN(x, (max - a0.den) / a1.den);
59 61
 
60
-            if (den*(2*x*a1.den + a0.den) > num*a1.den)
61
-                a1 = (AVRational){x*a1.num + a0.num, x*a1.den + a0.den};
62
+            if (den * (2 * x * a1.den + a0.den) > num * a1.den)
63
+                a1 = (AVRational) { x * a1.num + a0.num, x * a1.den + a0.den };
62 64
             break;
63 65
         }
64 66
 
65
-        a0= a1;
66
-        a1= (AVRational){a2n, a2d};
67
-        num= den;
68
-        den= next_den;
67
+        a0  = a1;
68
+        a1  = (AVRational) { a2n, a2d };
69
+        num = den;
70
+        den = next_den;
69 71
     }
70 72
     av_assert2(av_gcd(a1.num, a1.den) <= 1U);
71 73
 
72 74
     *dst_num = sign ? -a1.num : a1.num;
73 75
     *dst_den = a1.den;
74 76
 
75
-    return den==0;
77
+    return den == 0;
76 78
 }
77 79
 
78
-AVRational av_mul_q(AVRational b, AVRational c){
79
-    av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX);
80
+AVRational av_mul_q(AVRational b, AVRational c)
81
+{
82
+    av_reduce(&b.num, &b.den,
83
+               b.num * (int64_t) c.num,
84
+               b.den * (int64_t) c.den, INT_MAX);
80 85
     return b;
81 86
 }
82 87
 
83
-AVRational av_div_q(AVRational b, AVRational c){
84
-    return av_mul_q(b, (AVRational){c.den, c.num});
88
+AVRational av_div_q(AVRational b, AVRational c)
89
+{
90
+    return av_mul_q(b, (AVRational) { c.den, c.num });
85 91
 }
86 92
 
87
-AVRational av_add_q(AVRational b, AVRational c){
88
-    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);
93
+AVRational av_add_q(AVRational b, AVRational c) {
94
+    av_reduce(&b.num, &b.den,
95
+               b.num * (int64_t) c.den +
96
+               c.num * (int64_t) b.den,
97
+               b.den * (int64_t) c.den, INT_MAX);
89 98
     return b;
90 99
 }
91 100
 
92
-AVRational av_sub_q(AVRational b, AVRational c){
93
-    return av_add_q(b, (AVRational){-c.num, c.den});
101
+AVRational av_sub_q(AVRational b, AVRational c)
102
+{
103
+    return av_add_q(b, (AVRational) { -c.num, c.den });
94 104
 }
95 105
 
96
-AVRational av_d2q(double d, int max){
106
+AVRational av_d2q(double d, int max)
107
+{
97 108
     AVRational a;
98 109
 #define LOG2  0.69314718055994530941723212145817656807550013436025
99 110
     int exponent;
100 111
     int64_t den;
101 112
     if (isnan(d))
102
-        return (AVRational){0,0};
113
+        return (AVRational) { 0,0 };
103 114
     if (isinf(d))
104
-        return (AVRational){ d<0 ? -1:1, 0 };
115
+        return (AVRational) { d < 0 ? -1 : 1, 0 };
105 116
     exponent = FFMAX( (int)(log(fabs(d) + 1e-20)/LOG2), 0);
106 117
     den = 1LL << (61 - exponent);
107 118
     av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
... ...
@@ -127,7 +138,7 @@ int av_nearer_q(AVRational q, AVRational q1, AVRational q2)
127 127
 int av_find_nearest_q_idx(AVRational q, const AVRational* q_list)
128 128
 {
129 129
     int i, nearest_q_idx = 0;
130
-    for(i=0; q_list[i].den; i++)
130
+    for (i = 0; q_list[i].den; i++)
131 131
         if (av_nearer_q(q, q_list[i], q_list[nearest_q_idx]) > 0)
132 132
             nearest_q_idx = i;
133 133
 
... ...
@@ -138,16 +149,19 @@ int av_find_nearest_q_idx(AVRational q, const AVRational* q_list)
138 138
 int main(void)
139 139
 {
140 140
     AVRational a,b;
141
-    for(a.num=-2; a.num<=2; a.num++){
142
-        for(a.den=-2; a.den<=2; a.den++){
143
-            for(b.num=-2; b.num<=2; b.num++){
144
-                for(b.den=-2; b.den<=2; b.den++){
145
-                    int c= av_cmp_q(a,b);
146
-                    double d= av_q2d(a) == av_q2d(b) ? 0 : (av_q2d(a) - av_q2d(b));
147
-                    if(d>0) d=1;
148
-                    else if(d<0) d=-1;
149
-                    else if(d != d) d= INT_MIN;
150
-                    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);
141
+    for (a.num = -2; a.num <= 2; a.num++) {
142
+        for (a.den = -2; a.den <= 2; a.den++) {
143
+            for (b.num = -2; b.num <= 2; b.num++) {
144
+                for (b.den = -2; b.den <= 2; b.den++) {
145
+                    int c = av_cmp_q(a,b);
146
+                    double d = av_q2d(a) == av_q2d(b) ?
147
+                               0 : (av_q2d(a) - av_q2d(b));
148
+                    if (d > 0)       d = 1;
149
+                    else if (d < 0)  d = -1;
150
+                    else if (d != d) d = INT_MIN;
151
+                    if (c != d)
152
+                        av_log(0, AV_LOG_ERROR, "%d/%d %d/%d, %d %f\n", a.num,
153
+                               a.den, b.num, b.den, c,d);
151 154
                 }
152 155
             }
153 156
         }
... ...
@@ -21,22 +21,24 @@
21 21
 #include "log.h"
22 22
 #include "tree.h"
23 23
 
24
-typedef struct AVTreeNode{
24
+typedef struct AVTreeNode {
25 25
     struct AVTreeNode *child[2];
26 26
     void *elem;
27 27
     int state;
28
-}AVTreeNode;
28
+} AVTreeNode;
29 29
 
30 30
 const int av_tree_node_size = sizeof(AVTreeNode);
31 31
 
32
-void *av_tree_find(const AVTreeNode *t, void *key, int (*cmp)(void *key, const void *b), void *next[2]){
33
-    if(t){
34
-        unsigned int v= cmp(key, t->elem);
35
-        if(v){
36
-            if(next) next[v>>31]= t->elem;
37
-            return av_tree_find(t->child[(v>>31)^1], key, cmp, next);
38
-        }else{
39
-            if(next){
32
+void *av_tree_find(const AVTreeNode *t, void *key,
33
+                   int (*cmp)(void *key, const void *b), void *next[2])
34
+{
35
+    if (t) {
36
+        unsigned int v = cmp(key, t->elem);
37
+        if (v) {
38
+            if (next) next[v >> 31] = t->elem;
39
+            return av_tree_find(t->child[(v >> 31) ^ 1], key, cmp, next);
40
+        } else {
41
+            if (next) {
40 42
                 av_tree_find(t->child[0], key, cmp, next);
41 43
                 av_tree_find(t->child[1], key, cmp, next);
42 44
             }
... ...
@@ -46,41 +48,43 @@ void *av_tree_find(const AVTreeNode *t, void *key, int (*cmp)(void *key, const v
46 46
     return NULL;
47 47
 }
48 48
 
49
-void *av_tree_insert(AVTreeNode **tp, void *key, int (*cmp)(void *key, const void *b), AVTreeNode **next){
50
-    AVTreeNode *t= *tp;
51
-    if(t){
52
-        unsigned int v= cmp(t->elem, key);
49
+void *av_tree_insert(AVTreeNode **tp, void *key,
50
+                     int (*cmp)(void *key, const void *b), AVTreeNode **next)
51
+{
52
+    AVTreeNode *t = *tp;
53
+    if (t) {
54
+        unsigned int v = cmp(t->elem, key);
53 55
         void *ret;
54
-        if(!v){
55
-            if(*next)
56
+        if (!v) {
57
+            if (*next)
56 58
                 return t->elem;
57
-            else if(t->child[0]||t->child[1]){
58
-                int i= !t->child[0];
59
+            else if (t->child[0] || t->child[1]) {
60
+                int i = !t->child[0];
59 61
                 void *next_elem[2];
60 62
                 av_tree_find(t->child[i], key, cmp, next_elem);
61
-                key= t->elem= next_elem[i];
62
-                v= -i;
63
-            }else{
64
-                *next= t;
65
-                *tp=NULL;
63
+                key = t->elem = next_elem[i];
64
+                v = -i;
65
+            } else {
66
+                *next = t;
67
+                *tp = NULL;
66 68
                 return NULL;
67 69
             }
68 70
         }
69
-        ret= av_tree_insert(&t->child[v>>31], key, cmp, next);
70
-        if(!ret){
71
-            int i= (v>>31) ^ !!*next;
72
-            AVTreeNode **child= &t->child[i];
73
-            t->state += 2*i - 1;
74
-
75
-            if(!(t->state&1)){
76
-                if(t->state){
71
+        ret = av_tree_insert(&t->child[v >> 31], key, cmp, next);
72
+        if (!ret) {
73
+            int i = (v >> 31) ^ !!*next;
74
+            AVTreeNode **child = &t->child[i];
75
+            t->state += 2 * i - 1;
76
+
77
+            if (!(t->state & 1)) {
78
+                if (t->state) {
77 79
                     /* The following code is equivalent to
78 80
                     if((*child)->state*2 == -t->state)
79 81
                         rotate(child, i^1);
80 82
                     rotate(tp, i);
81 83
 
82 84
                     with rotate():
83
-                    static void rotate(AVTreeNode **tp, int i){
85
+                    static void rotate(AVTreeNode **tp, int i) {
84 86
                         AVTreeNode *t= *tp;
85 87
 
86 88
                         *tp= t->child[i];
... ...
@@ -92,54 +96,62 @@ void *av_tree_insert(AVTreeNode **tp, void *key, int (*cmp)(void *key, const voi
92 92
                     }
93 93
                     but such a rotate function is both bigger and slower
94 94
                     */
95
-                    if((*child)->state*2 == -t->state){
96
-                        *tp= (*child)->child[i^1];
97
-                        (*child)->child[i^1]= (*tp)->child[i];
98
-                        (*tp)->child[i]= *child;
99
-                        *child= (*tp)->child[i^1];
100
-                        (*tp)->child[i^1]= t;
101
-
102
-                        (*tp)->child[0]->state= -((*tp)->state>0);
103
-                        (*tp)->child[1]->state=   (*tp)->state<0 ;
104
-                        (*tp)->state=0;
105
-                    }else{
106
-                        *tp= *child;
107
-                        *child= (*child)->child[i^1];
108
-                        (*tp)->child[i^1]= t;
109
-                        if((*tp)->state) t->state  = 0;
110
-                        else             t->state>>= 1;
111
-                        (*tp)->state= -t->state;
95
+                    if (( *child )->state * 2 == -t->state) {
96
+                        *tp                    = (*child)->child[i ^ 1];
97
+                        (*child)->child[i ^ 1] = (*tp)->child[i];
98
+                        (*tp)->child[i]        = *child;
99
+                        *child                 = ( *tp )->child[i ^ 1];
100
+                        (*tp)->child[i ^ 1]    = t;
101
+
102
+                        (*tp)->child[0]->state = -((*tp)->state > 0);
103
+                        (*tp)->child[1]->state =   (*tp)->state < 0;
104
+                        (*tp)->state           = 0;
105
+                    } else {
106
+                        *tp                    = *child;
107
+                        *child                 = (*child)->child[i ^ 1];
108
+                        (*tp)->child[i ^ 1]    = t;
109
+                        if ((*tp)->state) t->state = 0;
110
+                        else              t->state >>= 1;
111
+                        (*tp)->state           = -t->state;
112 112
                     }
113 113
                 }
114 114
             }
115
-            if(!(*tp)->state ^ !!*next)
115
+            if (!(*tp)->state ^ !!*next)
116 116
                 return key;
117 117
         }
118 118
         return ret;
119
-    }else{
120
-        *tp= *next; *next= NULL;
121
-        if(*tp){
122
-            (*tp)->elem= key;
119
+    } else {
120
+        *tp   = *next;
121
+        *next = NULL;
122
+        if (*tp) {
123
+            (*tp)->elem = key;
123 124
             return NULL;
124
-        }else
125
+        } else
125 126
             return key;
126 127
     }
127 128
 }
128 129
 
129
-void av_tree_destroy(AVTreeNode *t){
130
-    if(t){
130
+void av_tree_destroy(AVTreeNode *t)
131
+{
132
+    if (t) {
131 133
         av_tree_destroy(t->child[0]);
132 134
         av_tree_destroy(t->child[1]);
133 135
         av_free(t);
134 136
     }
135 137
 }
136 138
 
137
-void av_tree_enumerate(AVTreeNode *t, void *opaque, int (*cmp)(void *opaque, void *elem), int (*enu)(void *opaque, void *elem)){
138
-    if(t){
139
-        int v= cmp ? cmp(opaque, t->elem) : 0;
140
-        if(v>=0) av_tree_enumerate(t->child[0], opaque, cmp, enu);
141
-        if(v==0) enu(opaque, t->elem);
142
-        if(v<=0) av_tree_enumerate(t->child[1], opaque, cmp, enu);
139
+void av_tree_enumerate(AVTreeNode *t, void *opaque,
140
+                       int (*cmp)(void *opaque, void *elem),
141
+                       int (*enu)(void *opaque, void *elem))
142
+{
143
+    if (t) {
144
+        int v = cmp ? cmp(opaque, t->elem) : 0;
145
+        if (v >= 0)
146
+            av_tree_enumerate(t->child[0], opaque, cmp, enu);
147
+        if (v == 0)
148
+            enu(opaque, t->elem);
149
+        if (v <= 0)
150
+            av_tree_enumerate(t->child[1], opaque, cmp, enu);
143 151
     }
144 152
 }
145 153
 
... ...
@@ -147,64 +159,68 @@ void av_tree_enumerate(AVTreeNode *t, void *opaque, int (*cmp)(void *opaque, voi
147 147
 
148 148
 #include "lfg.h"
149 149
 
150
-static int check(AVTreeNode *t){
151
-    if(t){
152
-        int left= check(t->child[0]);
153
-        int right= check(t->child[1]);
150
+static int check(AVTreeNode *t)
151
+{
152
+    if (t) {
153
+        int left  = check(t->child[0]);
154
+        int right = check(t->child[1]);
154 155
 
155
-        if(left>999 || right>999)
156
+        if (left>999 || right>999)
156 157
             return 1000;
157
-        if(right - left != t->state)
158
+        if (right - left != t->state)
158 159
             return 1000;
159
-        if(t->state>1 || t->state<-1)
160
+        if (t->state>1 || t->state<-1)
160 161
             return 1000;
161
-        return FFMAX(left, right)+1;
162
+        return FFMAX(left, right) + 1;
162 163
     }
163 164
     return 0;
164 165
 }
165 166
 
166
-static void print(AVTreeNode *t, int depth){
167
+static void print(AVTreeNode *t, int depth)
168
+{
167 169
     int i;
168
-    for(i=0; i<depth*4; i++) av_log(NULL, AV_LOG_ERROR, " ");
169
-    if(t){
170
+    for (i = 0; i < depth * 4; i++) av_log(NULL, AV_LOG_ERROR, " ");
171
+    if (t) {
170 172
         av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem);
171
-        print(t->child[0], depth+1);
172
-        print(t->child[1], depth+1);
173
-    }else
173
+        print(t->child[0], depth + 1);
174
+        print(t->child[1], depth + 1);
175
+    } else
174 176
         av_log(NULL, AV_LOG_ERROR, "NULL\n");
175 177
 }
176 178
 
177
-static int cmp(void *a, const void *b){
178
-    return (uint8_t*)a-(const uint8_t*)b;
179
+static int cmp(void *a, const void *b)
180
+{
181
+    return (uint8_t *) a - (const uint8_t *) b;
179 182
 }
180 183
 
181
-int main(void){
184
+int main (void)
185
+{
182 186
     int i;
183 187
     void *k;
184
-    AVTreeNode *root= NULL, *node=NULL;
188
+    AVTreeNode *root = NULL, *node = NULL;
185 189
     AVLFG prng;
186 190
 
187 191
     av_lfg_init(&prng, 1);
188 192
 
189
-    for(i=0; i<10000; i++){
193
+    for (i = 0; i < 10000; i++) {
190 194
         int j = av_lfg_get(&prng) % 86294;
191
-        if(check(root) > 999){
195
+        if (check(root) > 999) {
192 196
             av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
193 197
         print(root, 0);
194 198
             return -1;
195 199
         }
196 200
         av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j);
197
-        if(!node)
198
-            node= av_mallocz(av_tree_node_size);
199
-        av_tree_insert(&root, (void*)(j+1), cmp, &node);
201
+        if (!node)
202
+            node = av_mallocz(av_tree_node_size);
203
+        av_tree_insert(&root, (void *) (j + 1), cmp, &node);
200 204
 
201 205
         j = av_lfg_get(&prng) % 86294;
202 206
         {
203
-            AVTreeNode *node2=NULL;
207
+            AVTreeNode *node2 = NULL;
204 208
             av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j);
205
-            av_tree_insert(&root, (void*)(j+1), cmp, &node2);
206
-            k= av_tree_find(root, (void*)(j+1), cmp, NULL);
207
-            if(k)
209
+            av_tree_insert(&root, (void *) (j + 1), cmp, &node2);
210
+            k = av_tree_find(root, (void *) (j + 1), cmp, NULL);
211
+            if (k)
208 212
                 av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
209 213
         }
210 214
     }