Commit 3b08d9d9 authored by Diego Biurrun's avatar Diego Biurrun

testprogs: K&R formatting cosmetics

parent 43992985
......@@ -24,27 +24,29 @@
* different IIR filters implementation
*/
#include "iirfilter.h"
#include <math.h>
#include "libavutil/attributes.h"
#include "libavutil/common.h"
#include "iirfilter.h"
/**
* IIR filter global parameters
*/
typedef struct FFIIRFilterCoeffs{
typedef struct FFIIRFilterCoeffs {
int order;
float gain;
int *cx;
float *cy;
}FFIIRFilterCoeffs;
} FFIIRFilterCoeffs;
/**
* IIR filter state
*/
typedef struct FFIIRFilterState{
typedef struct FFIIRFilterState {
float x[1];
}FFIIRFilterState;
} FFIIRFilterState;
/// maximum supported filter order
#define MAXORDER 30
......@@ -73,14 +75,14 @@ static av_cold int butterworth_init_coeffs(void *avc,
wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
c->cx[0] = 1;
for(i = 1; i < (order >> 1) + 1; i++)
for (i = 1; i < (order >> 1) + 1; i++)
c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
p[0][0] = 1.0;
p[0][1] = 0.0;
for(i = 1; i <= order; i++)
for (i = 1; i <= order; i++)
p[i][0] = p[i][1] = 0.0;
for(i = 0; i < order; i++){
for (i = 0; i < order; i++) {
double zp[2];
double th = (i + (order >> 1) + 0.5) * M_PI / order;
double a_re, a_im, c_re, c_im;
......@@ -93,19 +95,18 @@ static av_cold int butterworth_init_coeffs(void *avc,
zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
for(j = order; j >= 1; j--)
{
for (j = order; j >= 1; j--) {
a_re = p[j][0];
a_im = p[j][1];
p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
p[j][0] = a_re * zp[0] - a_im * zp[1] + p[j - 1][0];
p[j][1] = a_re * zp[1] + a_im * zp[0] + p[j - 1][1];
}
a_re = p[0][0]*zp[0] - p[0][1]*zp[1];
p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
a_re = p[0][0] * zp[0] - p[0][1] * zp[1];
p[0][1] = p[0][0] * zp[1] + p[0][1] * zp[0];
p[0][0] = a_re;
}
c->gain = p[order][0];
for(i = 0; i < order; i++){
for (i = 0; i < order; i++) {
c->gain += p[i][0];
c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
(p[order][0] * p[order][0] + p[order][1] * p[order][1]);
......@@ -158,7 +159,7 @@ static av_cold int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
return 0;
}
av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(void *avc,
av_cold struct FFIIRFilterCoeffs *ff_iir_filter_init_coeffs(void *avc,
enum IIRFilterType filt_type,
enum IIRFilterMode filt_mode,
int order, float cutoff_ratio,
......@@ -172,9 +173,9 @@ av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(void *avc,
FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
init_fail);
FF_ALLOC_OR_GOTO (avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
FF_ALLOC_OR_GOTO(avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
init_fail);
FF_ALLOC_OR_GOTO (avc, c->cy, sizeof(c->cy[0]) * order,
FF_ALLOC_OR_GOTO(avc, c->cy, sizeof(c->cy[0]) * order,
init_fail);
c->order = order;
......@@ -200,9 +201,9 @@ init_fail:
return NULL;
}
av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
av_cold struct FFIIRFilterState *ff_iir_filter_init_state(int order)
{
FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
FFIIRFilterState *s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
return s;
}
......@@ -211,13 +212,15 @@ av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
#define CONV_FLT(dest, source) dest = source;
#define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
in = *src0 * c->gain \
+ c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
+ c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
res = (s->x[i0] + in )*1 \
+ (s->x[i1] + s->x[i3])*4 \
+ s->x[i2] *6; \
CONV_##fmt(*dst0, res) \
in = *src0 * c->gain + \
c->cy[0] * s->x[i0] + \
c->cy[1] * s->x[i1] + \
c->cy[2] * s->x[i2] + \
c->cy[3] * s->x[i3]; \
res = (s->x[i0] + in) * 1 + \
(s->x[i1] + s->x[i3]) * 4 + \
s->x[i2] * 6; \
CONV_ ## fmt(*dst0, res) \
s->x[i0] = in; \
src0 += sstep; \
dst0 += dstep;
......@@ -243,14 +246,14 @@ av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
int j; \
float in, res; \
in = *src0 * c->gain; \
for(j = 0; j < c->order; j++) \
for (j = 0; j < c->order; j++) \
in += c->cy[j] * s->x[j]; \
res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
for(j = 1; j < c->order >> 1; j++) \
for (j = 1; j < c->order >> 1; j++) \
res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
for(j = 0; j < c->order - 1; j++) \
for (j = 0; j < c->order - 1; j++) \
s->x[j] = s->x[j + 1]; \
CONV_##fmt(*dst0, res) \
CONV_ ## fmt(*dst0, res) \
s->x[c->order - 1] = in; \
src0 += sstep; \
dst0 += dstep; \
......@@ -265,7 +268,7 @@ av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
float in = *src0 * c->gain + \
s->x[0] * c->cy[0] + \
s->x[1] * c->cy[1]; \
CONV_##fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
CONV_ ## fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
s->x[0] = s->x[1]; \
s->x[1] = in; \
src0 += sstep; \
......@@ -306,7 +309,7 @@ av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
{
if(coeffs){
if (coeffs) {
av_free(coeffs->cx);
av_free(coeffs->cy);
}
......@@ -331,9 +334,8 @@ int main(void)
cutoff_coeff, 0.0, 0.0);
fstate = ff_iir_filter_init_state(FILT_ORDER);
for (i = 0; i < SIZE; i++) {
x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
}
for (i = 0; i < SIZE; i++)
x[i] = lrint(0.75 * INT16_MAX * sin(0.5 * M_PI * i * i / SIZE));
ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
......
......@@ -21,9 +21,9 @@
*/
#include "common.h"
#include "aes.h"
#include "intreadwrite.h"
#include "timer.h"
#include "aes.h"
typedef union {
uint64_t u64[2];
......@@ -109,7 +109,8 @@ static void subshift(av_aes_block s0[2], int s, const uint8_t *box)
s3[0].u8[ 5] = box[s3[1].u8[ 1]];
}
static inline int mix_core(uint32_t multbl[][256], int a, int b, int c, int d){
static inline int mix_core(uint32_t multbl[][256], int a, int b, int c, int d)
{
#if CONFIG_SMALL
return multbl[0][a] ^ ROT(multbl[0][b], 8) ^ ROT(multbl[0][c], 16) ^ ROT(multbl[0][d], 24);
#else
......@@ -117,12 +118,13 @@ static inline int mix_core(uint32_t multbl[][256], int a, int b, int c, int d){
#endif
}
static inline void mix(av_aes_block state[2], uint32_t multbl[][256], int s1, int s3){
static inline void mix(av_aes_block state[2], uint32_t multbl[][256], int s1, int s3)
{
uint8_t (*src)[4] = state[1].u8x4;
state[0].u32[0] = mix_core(multbl, src[0][0], src[s1 ][1], src[2][2], src[s3 ][3]);
state[0].u32[1] = mix_core(multbl, src[1][0], src[s3-1][1], src[3][2], src[s1-1][3]);
state[0].u32[1] = mix_core(multbl, src[1][0], src[s3 - 1][1], src[3][2], src[s1 - 1][3]);
state[0].u32[2] = mix_core(multbl, src[2][0], src[s3 ][1], src[0][2], src[s1 ][3]);
state[0].u32[3] = mix_core(multbl, src[3][0], src[s1-1][1], src[1][2], src[s3-1][3]);
state[0].u32[3] = mix_core(multbl, src[3][0], src[s1 - 1][1], src[1][2], src[s3 - 1][3]);
}
static inline void crypt(AVAES *a, int s, const uint8_t *sbox,
......@@ -178,7 +180,7 @@ static void init_multbl2(uint32_t tbl[][256], const int c[4],
l = alog8[x + log8[c[1]]];
m = alog8[x + log8[c[2]]];
n = alog8[x + log8[c[3]]];
tbl[0][i] = AV_NE(MKBETAG(k,l,m,n), MKTAG(k,l,m,n));
tbl[0][i] = AV_NE(MKBETAG(k, l, m, n), MKTAG(k, l, m, n));
#if !CONFIG_SMALL
tbl[1][i] = ROT(tbl[0][i], 8);
tbl[2][i] = ROT(tbl[0][i], 16);
......@@ -198,7 +200,7 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
uint8_t log8[256];
uint8_t alog8[512];
if (!enc_multbl[FF_ARRAY_ELEMS(enc_multbl)-1][FF_ARRAY_ELEMS(enc_multbl[0])-1]) {
if (!enc_multbl[FF_ARRAY_ELEMS(enc_multbl) - 1][FF_ARRAY_ELEMS(enc_multbl[0]) - 1]) {
j = 1;
for (i = 0; i < 255; i++) {
alog8[i] = alog8[i + 255] = j;
......@@ -254,9 +256,8 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
a->round_key[i] = tmp[0];
}
} else {
for (i = 0; i < (rounds + 1) >> 1; i++) {
FFSWAP(av_aes_block, a->round_key[i], a->round_key[rounds-i]);
}
for (i = 0; i < (rounds + 1) >> 1; i++)
FFSWAP(av_aes_block, a->round_key[i], a->round_key[rounds - i]);
}
return 0;
......@@ -264,6 +265,7 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
#ifdef TEST
#include <string.h>
#include "lfg.h"
#include "log.h"
......@@ -276,12 +278,12 @@ int main(int argc, char **argv)
{ 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3,
0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59 }
};
uint8_t pt[16], rpt[2][16]= {
uint8_t pt[16], rpt[2][16] = {
{ 0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad,
0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3 },
{ 0 }
};
uint8_t rct[2][16]= {
uint8_t rct[2][16] = {
{ 0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7,
0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf },
{ 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0,
......@@ -313,9 +315,8 @@ int main(int argc, char **argv)
av_lfg_init(&prng, 1);
for (i = 0; i < 10000; i++) {
for (j = 0; j < 16; j++) {
for (j = 0; j < 16; j++)
pt[j] = av_lfg_get(&prng);
}
{
START_TIMER;
av_aes_crypt(&ae, temp, pt, 1, NULL, 0);
......
......@@ -19,8 +19,9 @@
*/
#include "config.h"
#include "common.h"
#include "bswap.h"
#include "common.h"
#include "crc.h"
#if CONFIG_HARDCODED_TABLES
......@@ -287,7 +288,7 @@ int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
if (ctx_size >= sizeof(AVCRC) * 1024)
for (i = 0; i < 256; i++)
for (j = 0; j < 3; j++)
ctx[256 *(j + 1) + i] =
ctx[256 * (j + 1) + i] =
(ctx[256 * j + i] >> 8) ^ ctx[ctx[256 * j + i] & 0xFF];
#endif
......@@ -338,11 +339,12 @@ int main(void)
{
uint8_t buf[1999];
int i;
int p[5][3] = { { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 },
{ AV_CRC_32_IEEE , 0x04C11DB7, 0xC0F5BAE0 },
{ AV_CRC_16_ANSI_LE, 0xA001 , 0xBFD8 },
{ AV_CRC_16_ANSI , 0x8005 , 0x1FBB },
{ AV_CRC_8_ATM , 0x07 , 0xE3 }
int p[5][3] = {
{ AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 },
{ AV_CRC_32_IEEE, 0x04C11DB7, 0xC0F5BAE0 },
{ AV_CRC_16_ANSI_LE, 0xA001, 0xBFD8 },
{ AV_CRC_16_ANSI, 0x8005, 0x1FBB },
{ AV_CRC_8_ATM, 0x07, 0xE3 }
};
const AVCRC *ctx;
......
......@@ -34,7 +34,7 @@ struct AVDES {
};
#endif
#define T(a, b, c, d, e, f, g, h) 64-a,64-b,64-c,64-d,64-e,64-f,64-g,64-h
#define T(a, b, c, d, e, f, g, h) 64 - a, 64 - b, 64 - c, 64 - d, 64 - e, 64 - f, 64 - g, 64 - h
static const uint8_t IP_shuffle[] = {
T(58, 50, 42, 34, 26, 18, 10, 2),
T(60, 52, 44, 36, 28, 20, 12, 4),
......@@ -48,7 +48,7 @@ static const uint8_t IP_shuffle[] = {
#undef T
#if CONFIG_SMALL || defined(GENTABLES)
#define T(a, b, c, d) 32-a,32-b,32-c,32-d
#define T(a, b, c, d) 32 - a, 32 - b, 32 - c, 32 - d
static const uint8_t P_shuffle[] = {
T(16, 7, 20, 21),
T(29, 12, 28, 17),
......@@ -62,7 +62,7 @@ static const uint8_t P_shuffle[] = {
#undef T
#endif
#define T(a, b, c, d, e, f, g) 64-a,64-b,64-c,64-d,64-e,64-f,64-g
#define T(a, b, c, d, e, f, g) 64 - a, 64 - b, 64 - c, 64 - d, 64 - e, 64 - f, 64 - g
static const uint8_t PC1_shuffle[] = {
T(57, 49, 41, 33, 25, 17, 9),
T( 1, 58, 50, 42, 34, 26, 18),
......@@ -75,7 +75,7 @@ static const uint8_t PC1_shuffle[] = {
};
#undef T
#define T(a, b, c, d, e, f) 56-a,56-b,56-c,56-d,56-e,56-f
#define T(a, b, c, d, e, f) 56 - a, 56 - b, 56 - c, 56 - d, 56 - e, 56 - f
static const uint8_t PC2_shuffle[] = {
T(14, 17, 11, 24, 1, 5),
T( 3, 28, 15, 6, 21, 10),
......@@ -90,29 +90,21 @@ static const uint8_t PC2_shuffle[] = {
#if CONFIG_SMALL
static const uint8_t S_boxes[8][32] = {
{
0x0e, 0xf4, 0x7d, 0x41, 0xe2, 0x2f, 0xdb, 0x18, 0xa3, 0x6a, 0xc6, 0xbc, 0x95, 0x59, 0x30, 0x87,
0xf4, 0xc1, 0x8e, 0x28, 0x4d, 0x96, 0x12, 0x7b, 0x5f, 0xbc, 0x39, 0xe7, 0xa3, 0x0a, 0x65, 0xd0,
}, {
0x3f, 0xd1, 0x48, 0x7e, 0xf6, 0x2b, 0x83, 0xe4, 0xc9, 0x07, 0x12, 0xad, 0x6c, 0x90, 0xb5, 0x5a,
0xd0, 0x8e, 0xa7, 0x1b, 0x3a, 0xf4, 0x4d, 0x21, 0xb5, 0x68, 0x7c, 0xc6, 0x09, 0x53, 0xe2, 0x9f,
}, {
0xda, 0x70, 0x09, 0x9e, 0x36, 0x43, 0x6f, 0xa5, 0x21, 0x8d, 0x5c, 0xe7, 0xcb, 0xb4, 0xf2, 0x18,
0x1d, 0xa6, 0xd4, 0x09, 0x68, 0x9f, 0x83, 0x70, 0x4b, 0xf1, 0xe2, 0x3c, 0xb5, 0x5a, 0x2e, 0xc7,
}, {
0xd7, 0x8d, 0xbe, 0x53, 0x60, 0xf6, 0x09, 0x3a, 0x41, 0x72, 0x28, 0xc5, 0x1b, 0xac, 0xe4, 0x9f,
0x3a, 0xf6, 0x09, 0x60, 0xac, 0x1b, 0xd7, 0x8d, 0x9f, 0x41, 0x53, 0xbe, 0xc5, 0x72, 0x28, 0xe4,
}, {
0xe2, 0xbc, 0x24, 0xc1, 0x47, 0x7a, 0xdb, 0x16, 0x58, 0x05, 0xf3, 0xaf, 0x3d, 0x90, 0x8e, 0x69,
0xb4, 0x82, 0xc1, 0x7b, 0x1a, 0xed, 0x27, 0xd8, 0x6f, 0xf9, 0x0c, 0x95, 0xa6, 0x43, 0x50, 0x3e,
}, {
0xac, 0xf1, 0x4a, 0x2f, 0x79, 0xc2, 0x96, 0x58, 0x60, 0x1d, 0xd3, 0xe4, 0x0e, 0xb7, 0x35, 0x8b,
0x49, 0x3e, 0x2f, 0xc5, 0x92, 0x58, 0xfc, 0xa3, 0xb7, 0xe0, 0x14, 0x7a, 0x61, 0x0d, 0x8b, 0xd6,
}, {
0xd4, 0x0b, 0xb2, 0x7e, 0x4f, 0x90, 0x18, 0xad, 0xe3, 0x3c, 0x59, 0xc7, 0x25, 0xfa, 0x86, 0x61,
0x61, 0xb4, 0xdb, 0x8d, 0x1c, 0x43, 0xa7, 0x7e, 0x9a, 0x5f, 0x06, 0xf8, 0xe0, 0x25, 0x39, 0xc2,
}, {
0x1d, 0xf2, 0xd8, 0x84, 0xa6, 0x3f, 0x7b, 0x41, 0xca, 0x59, 0x63, 0xbe, 0x05, 0xe0, 0x9c, 0x27,
{ 0x0e, 0xf4, 0x7d, 0x41, 0xe2, 0x2f, 0xdb, 0x18, 0xa3, 0x6a, 0xc6, 0xbc, 0x95, 0x59, 0x30, 0x87,
0xf4, 0xc1, 0x8e, 0x28, 0x4d, 0x96, 0x12, 0x7b, 0x5f, 0xbc, 0x39, 0xe7, 0xa3, 0x0a, 0x65, 0xd0, },
{ 0x3f, 0xd1, 0x48, 0x7e, 0xf6, 0x2b, 0x83, 0xe4, 0xc9, 0x07, 0x12, 0xad, 0x6c, 0x90, 0xb5, 0x5a,
0xd0, 0x8e, 0xa7, 0x1b, 0x3a, 0xf4, 0x4d, 0x21, 0xb5, 0x68, 0x7c, 0xc6, 0x09, 0x53, 0xe2, 0x9f, },
{ 0xda, 0x70, 0x09, 0x9e, 0x36, 0x43, 0x6f, 0xa5, 0x21, 0x8d, 0x5c, 0xe7, 0xcb, 0xb4, 0xf2, 0x18,
0x1d, 0xa6, 0xd4, 0x09, 0x68, 0x9f, 0x83, 0x70, 0x4b, 0xf1, 0xe2, 0x3c, 0xb5, 0x5a, 0x2e, 0xc7, },
{ 0xd7, 0x8d, 0xbe, 0x53, 0x60, 0xf6, 0x09, 0x3a, 0x41, 0x72, 0x28, 0xc5, 0x1b, 0xac, 0xe4, 0x9f,
0x3a, 0xf6, 0x09, 0x60, 0xac, 0x1b, 0xd7, 0x8d, 0x9f, 0x41, 0x53, 0xbe, 0xc5, 0x72, 0x28, 0xe4, },
{ 0xe2, 0xbc, 0x24, 0xc1, 0x47, 0x7a, 0xdb, 0x16, 0x58, 0x05, 0xf3, 0xaf, 0x3d, 0x90, 0x8e, 0x69,
0xb4, 0x82, 0xc1, 0x7b, 0x1a, 0xed, 0x27, 0xd8, 0x6f, 0xf9, 0x0c, 0x95, 0xa6, 0x43, 0x50, 0x3e, },
{ 0xac, 0xf1, 0x4a, 0x2f, 0x79, 0xc2, 0x96, 0x58, 0x60, 0x1d, 0xd3, 0xe4, 0x0e, 0xb7, 0x35, 0x8b,
0x49, 0x3e, 0x2f, 0xc5, 0x92, 0x58, 0xfc, 0xa3, 0xb7, 0xe0, 0x14, 0x7a, 0x61, 0x0d, 0x8b, 0xd6, },
{ 0xd4, 0x0b, 0xb2, 0x7e, 0x4f, 0x90, 0x18, 0xad, 0xe3, 0x3c, 0x59, 0xc7, 0x25, 0xfa, 0x86, 0x61,
0x61, 0xb4, 0xdb, 0x8d, 0x1c, 0x43, 0xa7, 0x7e, 0x9a, 0x5f, 0x06, 0xf8, 0xe0, 0x25, 0x39, 0xc2, },
{ 0x1d, 0xf2, 0xd8, 0x84, 0xa6, 0x3f, 0x7b, 0x41, 0xca, 0x59, 0x63, 0xbe, 0x05, 0xe0, 0x9c, 0x27,
0x27, 0x1b, 0xe4, 0x71, 0x49, 0xac, 0x8e, 0xd2, 0xf0, 0xc6, 0x9a, 0x0d, 0x3f, 0x53, 0x65, 0xb8,
}
};
......@@ -122,90 +114,75 @@ static const uint8_t S_boxes[8][32] = {
* It can be regenerated by compiling this file with -DCONFIG_SMALL -DTEST -DGENTABLES
*/
static const uint32_t S_boxes_P_shuffle[8][64] = {
{
0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202, 0x00000002, 0x00008000,
{ 0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202, 0x00000002, 0x00008000,
0x00000200, 0x00808200, 0x00808202, 0x00000200, 0x00800202, 0x00808002, 0x00800000, 0x00000002,
0x00000202, 0x00800200, 0x00800200, 0x00008200, 0x00008200, 0x00808000, 0x00808000, 0x00800202,
0x00008002, 0x00800002, 0x00800002, 0x00008002, 0x00000000, 0x00000202, 0x00008202, 0x00800000,
0x00008000, 0x00808202, 0x00000002, 0x00808000, 0x00808200, 0x00800000, 0x00800000, 0x00000200,
0x00808002, 0x00008000, 0x00008200, 0x00800002, 0x00000200, 0x00000002, 0x00800202, 0x00008202,
0x00808202, 0x00008002, 0x00808000, 0x00800202, 0x00800002, 0x00000202, 0x00008202, 0x00808200,
0x00000202, 0x00800200, 0x00800200, 0x00000000, 0x00008002, 0x00008200, 0x00000000, 0x00808002,
},
{
0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x00080000, 0x00000010, 0x40080010, 0x40004010,
0x00000202, 0x00800200, 0x00800200, 0x00000000, 0x00008002, 0x00008200, 0x00000000, 0x00808002, },
{ 0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x00080000, 0x00000010, 0x40080010, 0x40004010,
0x40000010, 0x40084010, 0x40084000, 0x40000000, 0x40004000, 0x00080000, 0x00000010, 0x40080010,
0x00084000, 0x00080010, 0x40004010, 0x00000000, 0x40000000, 0x00004000, 0x00084010, 0x40080000,
0x00080010, 0x40000010, 0x00000000, 0x00084000, 0x00004010, 0x40084000, 0x40080000, 0x00004010,
0x00000000, 0x00084010, 0x40080010, 0x00080000, 0x40004010, 0x40080000, 0x40084000, 0x00004000,
0x40080000, 0x40004000, 0x00000010, 0x40084010, 0x00084010, 0x00000010, 0x00004000, 0x40000000,
0x00004010, 0x40084000, 0x00080000, 0x40000010, 0x00080010, 0x40004010, 0x40000010, 0x00080010,
0x00084000, 0x00000000, 0x40004000, 0x00004010, 0x40000000, 0x40080010, 0x40084010, 0x00084000,
},
{
0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x04000100, 0x00000000, 0x00010104, 0x04000100,
0x00084000, 0x00000000, 0x40004000, 0x00004010, 0x40000000, 0x40080010, 0x40084010, 0x00084000, },
{ 0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x04000100, 0x00000000, 0x00010104, 0x04000100,
0x00010004, 0x04000004, 0x04000004, 0x00010000, 0x04010104, 0x00010004, 0x04010000, 0x00000104,
0x04000000, 0x00000004, 0x04010100, 0x00000100, 0x00010100, 0x04010000, 0x04010004, 0x00010104,
0x04000104, 0x00010100, 0x00010000, 0x04000104, 0x00000004, 0x04010104, 0x00000100, 0x04000000,
0x04010100, 0x04000000, 0x00010004, 0x00000104, 0x00010000, 0x04010100, 0x04000100, 0x00000000,
0x00000100, 0x00010004, 0x04010104, 0x04000100, 0x04000004, 0x00000100, 0x00000000, 0x04010004,
0x04000104, 0x00010000, 0x04000000, 0x04010104, 0x00000004, 0x00010104, 0x00010100, 0x04000004,
0x04010000, 0x04000104, 0x00000104, 0x04010000, 0x00010104, 0x00000004, 0x04010004, 0x00010100,
},
{
0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x00401040, 0x80400040, 0x80400000, 0x80001000,
0x04010000, 0x04000104, 0x00000104, 0x04010000, 0x00010104, 0x00000004, 0x04010004, 0x00010100, },
{ 0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x00401040, 0x80400040, 0x80400000, 0x80001000,
0x00000000, 0x00401000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00400040, 0x80400000,
0x80000000, 0x00001000, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x80001000, 0x00001040,
0x80400040, 0x80000000, 0x00001040, 0x00400040, 0x00001000, 0x00401040, 0x80401040, 0x80000040,
0x00400040, 0x80400000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00000000, 0x00401000,
0x00001040, 0x00400040, 0x80400040, 0x80000000, 0x80401000, 0x80001040, 0x80001040, 0x00000040,
0x80401040, 0x80000040, 0x80000000, 0x00001000, 0x80400000, 0x80001000, 0x00401040, 0x80400040,
0x80001000, 0x00001040, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x00001000, 0x00401040,
},
{
0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00040000, 0x00000080, 0x20000000, 0x01040000,
0x80001000, 0x00001040, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x00001000, 0x00401040, },
{ 0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00040000, 0x00000080, 0x20000000, 0x01040000,
0x20040080, 0x00040000, 0x01000080, 0x20040080, 0x21000080, 0x21040000, 0x00040080, 0x20000000,
0x01000000, 0x20040000, 0x20040000, 0x00000000, 0x20000080, 0x21040080, 0x21040080, 0x01000080,
0x21040000, 0x20000080, 0x00000000, 0x21000000, 0x01040080, 0x01000000, 0x21000000, 0x00040080,
0x00040000, 0x21000080, 0x00000080, 0x01000000, 0x20000000, 0x01040000, 0x21000080, 0x20040080,
0x01000080, 0x20000000, 0x21040000, 0x01040080, 0x20040080, 0x00000080, 0x01000000, 0x21040000,
0x21040080, 0x00040080, 0x21000000, 0x21040080, 0x01040000, 0x00000000, 0x20040000, 0x21000000,
0x00040080, 0x01000080, 0x20000080, 0x00040000, 0x00000000, 0x20040000, 0x01040080, 0x20000080,
},
{
0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10200000, 0x00000008, 0x10202008, 0x00200000,
0x00040080, 0x01000080, 0x20000080, 0x00040000, 0x00000000, 0x20040000, 0x01040080, 0x20000080, },
{ 0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10200000, 0x00000008, 0x10202008, 0x00200000,
0x10002000, 0x00202008, 0x00200000, 0x10000008, 0x00200008, 0x10002000, 0x10000000, 0x00002008,
0x00000000, 0x00200008, 0x10002008, 0x00002000, 0x00202000, 0x10002008, 0x00000008, 0x10200008,
0x10200008, 0x00000000, 0x00202008, 0x10202000, 0x00002008, 0x00202000, 0x10202000, 0x10000000,
0x10002000, 0x00000008, 0x10200008, 0x00202000, 0x10202008, 0x00200000, 0x00002008, 0x10000008,
0x00200000, 0x10002000, 0x10000000, 0x00002008, 0x10000008, 0x10202008, 0x00202000, 0x10200000,
0x00202008, 0x10202000, 0x00000000, 0x10200008, 0x00000008, 0x00002000, 0x10200000, 0x00202008,
0x00002000, 0x00200008, 0x10002008, 0x00000000, 0x10202000, 0x10000000, 0x00200008, 0x10002008,
},
{
0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00000400, 0x02000401, 0x00100401, 0x02100400,
0x00002000, 0x00200008, 0x10002008, 0x00000000, 0x10202000, 0x10000000, 0x00200008, 0x10002008, },
{ 0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00000400, 0x02000401, 0x00100401, 0x02100400,
0x02100401, 0x00100000, 0x00000000, 0x02000001, 0x00000001, 0x02000000, 0x02100001, 0x00000401,
0x02000400, 0x00100401, 0x00100001, 0x02000400, 0x02000001, 0x02100000, 0x02100400, 0x00100001,
0x02100000, 0x00000400, 0x00000401, 0x02100401, 0x00100400, 0x00000001, 0x02000000, 0x00100400,
0x02000000, 0x00100400, 0x00100000, 0x02000401, 0x02000401, 0x02100001, 0x02100001, 0x00000001,
0x00100001, 0x02000000, 0x02000400, 0x00100000, 0x02100400, 0x00000401, 0x00100401, 0x02100400,
0x00000401, 0x02000001, 0x02100401, 0x02100000, 0x00100400, 0x00000000, 0x00000001, 0x02100401,
0x00000000, 0x00100401, 0x02100000, 0x00000400, 0x02000001, 0x02000400, 0x00000400, 0x00100001,
},
{
0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000000, 0x08000820, 0x00000020, 0x08000000,
0x00000000, 0x00100401, 0x02100000, 0x00000400, 0x02000001, 0x02000400, 0x00000400, 0x00100001, },
{ 0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000000, 0x08000820, 0x00000020, 0x08000000,
0x00020020, 0x08020000, 0x08020820, 0x00020800, 0x08020800, 0x00020820, 0x00000800, 0x00000020,
0x08020000, 0x08000020, 0x08000800, 0x00000820, 0x00020800, 0x00020020, 0x08020020, 0x08020800,
0x00000820, 0x00000000, 0x00000000, 0x08020020, 0x08000020, 0x08000800, 0x00020820, 0x00020000,
0x00020820, 0x00020000, 0x08020800, 0x00000800, 0x00000020, 0x08020020, 0x00000800, 0x00020820,
0x08000800, 0x00000020, 0x08000020, 0x08020000, 0x08020020, 0x08000000, 0x00020000, 0x08000820,
0x00000000, 0x08020820, 0x00020020, 0x08000020, 0x08020000, 0x08000800, 0x08000820, 0x00000000,
0x08020820, 0x00020800, 0x00020800, 0x00000820, 0x00000820, 0x00020020, 0x08000000, 0x08020800,
},
0x08020820, 0x00020800, 0x00020800, 0x00000820, 0x00000820, 0x00020020, 0x08000000, 0x08020800, },
};
#endif
static uint64_t shuffle(uint64_t in, const uint8_t *shuffle, int shuffle_len) {
static uint64_t shuffle(uint64_t in, const uint8_t *shuffle, int shuffle_len)
{
int i;
uint64_t res = 0;
for (i = 0; i < shuffle_len; i++)
......@@ -213,7 +190,8 @@ static uint64_t shuffle(uint64_t in, const uint8_t *shuffle, int shuffle_len) {
return res;
}
static uint64_t shuffle_inv(uint64_t in, const uint8_t *shuffle, int shuffle_len) {
static uint64_t shuffle_inv(uint64_t in, const uint8_t *shuffle, int shuffle_len)
{
int i;
uint64_t res = 0;
shuffle += shuffle_len - 1;
......@@ -224,7 +202,8 @@ static uint64_t shuffle_inv(uint64_t in, const uint8_t *shuffle, int shuffle_len
return res;
}
static uint32_t f_func(uint32_t r, uint64_t k) {
static uint32_t f_func(uint32_t r, uint64_t k)
{
int i;
uint32_t out = 0;
// rotate to get first part of E-shuffle in the lowest 6 bits
......@@ -234,7 +213,8 @@ static uint32_t f_func(uint32_t r, uint64_t k) {
uint8_t tmp = (r ^ k) & 0x3f;
#if CONFIG_SMALL
uint8_t v = S_boxes[i][tmp >> 1];
if (tmp & 1) v >>= 4;
if (tmp & 1)
v >>= 4;
out = (out >> 4) | (v << 28);
#else
out |= S_boxes_P_shuffle[i][tmp];
......@@ -255,7 +235,8 @@ static uint32_t f_func(uint32_t r, uint64_t k) {
* Note: the specification calls this "shift", so I kept it although
* it is confusing.
*/
static uint64_t key_shift_left(uint64_t CDn) {
static uint64_t key_shift_left(uint64_t CDn)
{
uint64_t carries = (CDn >> 27) & 0x10000001;
CDn <<= 1;
CDn &= ~0x10000001;
......@@ -263,7 +244,8 @@ static uint64_t key_shift_left(uint64_t CDn) {
return CDn;
}
static void gen_roundkeys(uint64_t K[16], uint64_t key) {
static void gen_roundkeys(uint64_t K[16], uint64_t key)
{
int i;
// discard parity bits from key and shuffle it into C and D parts
uint64_t CDn = shuffle(key, PC1_shuffle, sizeof(PC1_shuffle));
......@@ -276,7 +258,8 @@ static void gen_roundkeys(uint64_t K[16], uint64_t key) {
}
}
static uint64_t des_encdec(uint64_t in, uint64_t K[16], int decrypt) {
static uint64_t des_encdec(uint64_t in, uint64_t K[16], int decrypt)
{
int i;
// used to apply round keys in reverse order for decryption
decrypt = decrypt ? 15 : 0;
......@@ -299,7 +282,8 @@ AVDES *av_des_alloc(void)
return av_mallocz(sizeof(struct AVDES));
}
int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt) {
int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt)
{
if (key_bits != 64 && key_bits != 192)
return -1;
d->triple_des = key_bits > 64;
......@@ -311,7 +295,9 @@ int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt) {
return 0;
}
static void av_des_crypt_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt, int mac) {
static void av_des_crypt_mac(AVDES *d, uint8_t *dst, const uint8_t *src,
int count, uint8_t *iv, int decrypt, int mac)
{
uint64_t iv_val = iv ? AV_RB64(iv) : 0;
while (count-- > 0) {
uint64_t dst_val;
......@@ -341,12 +327,15 @@ static void av_des_crypt_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int cou
AV_WB64(iv, iv_val);
}
void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src,
int count, uint8_t *iv, int decrypt)
{
av_des_crypt_mac(d, dst, src, count, iv, decrypt, 0);
}
void av_des_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int count) {
av_des_crypt_mac(d, dst, src, count, (uint8_t[8]){0}, 0, 1);
void av_des_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int count)
{
av_des_crypt_mac(d, dst, src, count, (uint8_t[8]) { 0 }, 0, 1);
}
#ifdef TEST
......@@ -355,15 +344,16 @@ void av_des_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int count) {
#include "time.h"
static uint64_t rand64(void) {
static uint64_t rand64(void)
{
uint64_t r = rand();
r = (r << 32) | rand();
return r;
}
static const uint8_t test_key[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
static const DECLARE_ALIGNED(8, uint8_t, plain)[] = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
static const DECLARE_ALIGNED(8, uint8_t, crypt)[] = {0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18};
static const uint8_t test_key[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
static const DECLARE_ALIGNED(8, uint8_t, crypt)[] = { 0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18 };
static DECLARE_ALIGNED(8, uint8_t, tmp)[8];
static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8];
static const uint8_t cbc_key[] = {
......@@ -372,7 +362,8 @@ static const uint8_t cbc_key[] = {
0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
};
static int run_test(int cbc, int decrypt) {
static int run_test(int cbc, int decrypt)
{
AVDES d;
int delay = cbc && !decrypt ? 2 : 1;
uint64_t res;
......@@ -395,7 +386,8 @@ static int run_test(int cbc, int decrypt) {
}
}
int main(void) {
int main(void)
{
AVDES d;
int i;
uint64_t key[3];
......@@ -421,7 +413,9 @@ int main(void) {
return 1;
}
for (i = 0; i < 1000; i++) {
key[0] = rand64(); key[1] = rand64(); key[2] = rand64();
key[0] = rand64();
key[1] = rand64();
key[2] = rand64();
data = rand64();
av_des_init(&d, key, 192, 0);
av_des_crypt(&d, &ct, &data, 1, NULL, 0);
......
......@@ -31,12 +31,13 @@
*/
#include <stdint.h>
#include "bswap.h"
#include "intreadwrite.h"
#include "md5.h"
#include "mem.h"
#include "md5.h"
typedef struct AVMD5{
typedef struct AVMD5 {
uint64_t len;
uint8_t block[64];
uint32_t ABCD[4];
......@@ -76,16 +77,21 @@ static const uint32_t T[64] = { // T[i]= fabs(sin(i+1)<<32)
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
};
#define CORE(i, a, b, c, d) do { \
#define CORE(i, a, b, c, d) \
do { \
t = S[i >> 4][i & 3]; \
a += T[i]; \
\
if (i < 32) { \
if (i < 16) a += (d ^ (b & (c ^ d))) + X[ i & 15]; \
else a += (c ^ (d & (c ^ b))) + X[(1 + 5*i) & 15]; \
if (i < 16) \
a += (d ^ (b & (c ^ d))) + X[i & 15]; \
else \
a += (c ^ (d & (c ^ b))) + X[(1 + 5 * i) & 15]; \
} else { \
if (i < 48) a += (b ^ c ^ d) + X[(5 + 3*i) & 15]; \
else a += (c ^ (b | ~d)) + X[( 7*i) & 15]; \
if (i < 48) \
a += (b ^ c ^ d) + X[(5 + 3 * i) & 15]; \
else \
a += (c ^ (b | ~d)) + X[(7 * i) & 15]; \
} \
a = b + (a << t | a >> (32 - t)); \
} while (0)
......@@ -115,10 +121,13 @@ static void body(uint32_t ABCD[4], uint32_t X[16])
}
#else
#define CORE2(i) \
CORE( i, a,b,c,d); CORE((i+1),d,a,b,c); \
CORE((i+2),c,d,a,b); CORE((i+3),b,c,d,a)
#define CORE4(i) CORE2(i); CORE2((i+4)); CORE2((i+8)); CORE2((i+12))
CORE4(0); CORE4(16); CORE4(32); CORE4(48);
CORE(i, a, b, c, d); CORE((i + 1), d, a, b, c); \
CORE((i + 2), c, d, a, b); CORE((i + 3), b, c, d, a)
#define CORE4(i) CORE2(i); CORE2((i + 4)); CORE2((i + 8)); CORE2((i + 12))
CORE4(0);
CORE4(16);
CORE4(32);
CORE4(48);
#endif
ABCD[0] += d;
......@@ -162,10 +171,10 @@ void av_md5_final(AVMD5 *ctx, uint8_t *dst)
while ((ctx->len & 63) != 56)
av_md5_update(ctx, "", 1);
av_md5_update(ctx, (uint8_t *)&finalcount, 8);
av_md5_update(ctx, (uint8_t *) &finalcount, 8);
for (i = 0; i < 4; i++)
AV_WL32(dst + 4*i, ctx->ABCD[3 - i]);
AV_WL32(dst + 4 * i, ctx->ABCD[3 - i]);
}
void av_md5_sum(uint8_t *dst, const uint8_t *src, const int len)
......@@ -188,20 +197,26 @@ static void print_md5(uint8_t *md5)
printf("\n");
}
int main(void){
int main(void)
{
uint8_t md5val[16];
int i;
uint8_t in[1000];
for (i = 0; i < 1000; i++)
in[i] = i * i;
av_md5_sum(md5val, in, 1000); print_md5(md5val);
av_md5_sum(md5val, in, 63); print_md5(md5val);
av_md5_sum(md5val, in, 64); print_md5(md5val);
av_md5_sum(md5val, in, 65); print_md5(md5val);
av_md5_sum(md5val, in, 1000);
print_md5(md5val);
av_md5_sum(md5val, in, 63);
print_md5(md5val);
av_md5_sum(md5val, in, 64);
print_md5(md5val);
av_md5_sum(md5val, in, 65);
print_md5(md5val);
for (i = 0; i < 1000; i++)
in[i] = i % 127;
av_md5_sum(md5val, in, 999); print_md5(md5val);
av_md5_sum(md5val, in, 999);
print_md5(md5val);
return 0;
}
......
......@@ -25,18 +25,18 @@
* @author Michael Niedermayer <michaelni@gmx.at>
*/
#include "avutil.h"
#include "avstring.h"
#include "avutil.h"
#include "common.h"
#include "opt.h"
#include "eval.h"
#include "dict.h"
#include "eval.h"
#include "log.h"
#include "mathematics.h"
#include "opt.h"
const AVOption *av_opt_next(const void *obj, const AVOption *last)
{
AVClass *class = *(AVClass**)obj;
AVClass *class = *(AVClass **)obj;
if (!last && class->option && class->option[0].name)
return class->option;
if (last && last[1].name)
......@@ -47,13 +47,24 @@ const AVOption *av_opt_next(const void *obj, const AVOption *last)
static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
{
switch (o->type) {
case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
*den = ((AVRational*)dst)->den;
case AV_OPT_TYPE_FLAGS:
*intnum = *(unsigned int *)dst;
return 0;
case AV_OPT_TYPE_INT:
*intnum = *(int *)dst;
return 0;
case AV_OPT_TYPE_INT64:
*intnum = *(int64_t *)dst;
return 0;
case AV_OPT_TYPE_FLOAT:
*num = *(float *)dst;
return 0;
case AV_OPT_TYPE_DOUBLE:
*num = *(double *)dst;
return 0;
case AV_OPT_TYPE_RATIONAL:
*intnum = ((AVRational *)dst)->num;
*den = ((AVRational *)dst)->den;
return 0;
}
return AVERROR(EINVAL);
......@@ -64,19 +75,29 @@ static int write_number(void *obj, const AVOption *o, void *dst, double num, int
if (o->type != AV_OPT_TYPE_FLAGS &&
(o->max * den < num * intnum || o->min * den > num * intnum)) {
av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range\n",
num*intnum/den, o->name);
num * intnum / den, o->name);
return AVERROR(ERANGE);
}
switch (o->type) {
case AV_OPT_TYPE_FLAGS:
case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
case AV_OPT_TYPE_INT:
*(int *)dst = llrint(num / den) * intnum;
break;
case AV_OPT_TYPE_INT64:
*(int64_t *)dst = llrint(num / den) * intnum;
break;
case AV_OPT_TYPE_FLOAT:
*(float *)dst = num * intnum / den;
break;
case AV_OPT_TYPE_DOUBLE:
*(double *)dst = num * intnum / den;
break;
case AV_OPT_TYPE_RATIONAL:
if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
if ((int) num == num)
*(AVRational *)dst = (AVRational) { num *intnum, den };
else
*(AVRational *)dst = av_d2q(num * intnum / den, 1 << 24);
break;
default:
return AVERROR(EINVAL);
......@@ -91,17 +112,21 @@ static const double const_values[] = {
0
};
static const char * const const_names[] = {
static const char *const const_names[] = {
"PI",
"E",
"QP2LAMBDA",
0
};
static int hexchar2int(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
static int hexchar2int(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
......@@ -146,8 +171,9 @@ static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **d
#define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
opt->type == AV_OPT_TYPE_CONST || \
opt->type == AV_OPT_TYPE_FLAGS || \
opt->type == AV_OPT_TYPE_INT) ? \
opt->default_val.i64 : opt->default_val.dbl)
opt->type == AV_OPT_TYPE_INT) \
? opt->default_val.i64 \
: opt->default_val.dbl)
static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
{
......@@ -175,11 +201,16 @@ static int set_string_number(void *obj, void *target_obj, const AVOption *o, con
const AVOption *o_named = av_opt_find(target_obj, buf, o->unit, 0, 0);
if (o_named && o_named->type == AV_OPT_TYPE_CONST)
d = DEFAULT_NUMVAL(o_named);
else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
else if (!strcmp(buf, "max" )) d = o->max;
else if (!strcmp(buf, "min" )) d = o->min;
else if (!strcmp(buf, "none" )) d = 0;
else if (!strcmp(buf, "all" )) d = ~0;
else if (!strcmp(buf, "default"))
d = DEFAULT_NUMVAL(o);
else if (!strcmp(buf, "max"))
d = o->max;
else if (!strcmp(buf, "min"))
d = o->min;
else if (!strcmp(buf, "none"))
d = 0;
else if (!strcmp(buf, "all"))
d = ~0;
else {
int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
if (res < 0) {
......@@ -190,12 +221,16 @@ static int set_string_number(void *obj, void *target_obj, const AVOption *o, con
}
if (o->type == AV_OPT_TYPE_FLAGS) {
read_number(o, dst, NULL, NULL, &intnum);
if (cmd == '+') d = intnum | (int64_t)d;
else if (cmd == '-') d = intnum &~(int64_t)d;
if (cmd == '+')
d = intnum | (int64_t)d;
else if (cmd == '-')
d = intnum & ~(int64_t)d;
} else {
read_number(o, dst, &num, &den, &intnum);
if (cmd == '+') d = notfirst*num*intnum/den + d;
else if (cmd == '-') d = notfirst*num*intnum/den - d;
if (cmd == '+')
d = notfirst * num * intnum / den + d;
else if (cmd == '-')
d = notfirst * num * intnum / den - d;
}
if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
......@@ -218,29 +253,33 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
if (!val || o->flags & AV_OPT_FLAG_READONLY)
return AVERROR(EINVAL);
dst = ((uint8_t*)target_obj) + o->offset;
dst = ((uint8_t *)target_obj) + o->offset;
switch (o->type) {
case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
case AV_OPT_TYPE_STRING:
return set_string(obj, o, val, dst);
case AV_OPT_TYPE_BINARY:
return set_string_binary(obj, o, val, dst);
case AV_OPT_TYPE_FLAGS:
case AV_OPT_TYPE_INT:
case AV_OPT_TYPE_INT64:
case AV_OPT_TYPE_FLOAT:
case AV_OPT_TYPE_DOUBLE:
case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
case AV_OPT_TYPE_RATIONAL:
return set_string_number(obj, target_obj, o, val, dst);
}
av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
return AVERROR(EINVAL);
}
#define OPT_EVAL_NUMBER(name, opttype, vartype)\
int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
{\
if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY)\
return AVERROR(EINVAL);\
return set_string_number(obj, obj, o, val, name ## _out);\
}
#define OPT_EVAL_NUMBER(name, opttype, vartype) \
int av_opt_eval_ ## name(void *obj, const AVOption *o, \
const char *val, vartype *name ## _out) \
{ \
if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
return AVERROR(EINVAL); \
return set_string_number(obj, obj, o, val, name ## _out); \
}
OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
......@@ -261,7 +300,7 @@ static int set_number(void *obj, const char *name, double num, int den, int64_t
if (o->flags & AV_OPT_FLAG_READONLY)
return AVERROR(EINVAL);
dst = ((uint8_t*)target_obj) + o->offset;
dst = ((uint8_t *)target_obj) + o->offset;
return write_number(obj, o, dst, num, den, intnum);
}
......@@ -309,7 +348,8 @@ int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int
return 0;
}
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
int search_flags)
{
void *target_obj;
AVDictionary **dst;
......@@ -337,31 +377,44 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
if (!o || !target_obj)
return AVERROR_OPTION_NOT_FOUND;
dst = (uint8_t*)target_obj + o->offset;
dst = (uint8_t *)target_obj + o->offset;
buf[0] = 0;
switch (o->type) {
case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
case AV_OPT_TYPE_FLAGS:
ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
break;
case AV_OPT_TYPE_INT:
ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
break;
case AV_OPT_TYPE_INT64:
ret = snprintf(buf, sizeof(buf), "%" PRId64, *(int64_t *)dst);
break;
case AV_OPT_TYPE_FLOAT:
ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
break;
case AV_OPT_TYPE_DOUBLE:
ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
break;
case AV_OPT_TYPE_RATIONAL:
ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num,
((AVRational *)dst)->den);
break;
case AV_OPT_TYPE_STRING:
if (*(uint8_t**)dst)
*out_val = av_strdup(*(uint8_t**)dst);
if (*(uint8_t **)dst)
*out_val = av_strdup(*(uint8_t **)dst);
else
*out_val = av_strdup("");
return *out_val ? 0 : AVERROR(ENOMEM);
case AV_OPT_TYPE_BINARY:
len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
if ((uint64_t)len*2 + 1 > INT_MAX)
len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
if ((uint64_t)len * 2 + 1 > INT_MAX)
return AVERROR(EINVAL);
if (!(*out_val = av_malloc(len*2 + 1)))
if (!(*out_val = av_malloc(len * 2 + 1)))
return AVERROR(ENOMEM);
bin = *(uint8_t**)dst;
bin = *(uint8_t **)dst;
for (i = 0; i < len; i++)
snprintf(*out_val + i*2, 3, "%02X", bin[i]);
snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
return 0;
default:
return AVERROR(EINVAL);
......@@ -381,12 +434,13 @@ static int get_number(void *obj, const char *name, double *num, int *den, int64_
if (!o || !target_obj)
goto error;
dst = ((uint8_t*)target_obj) + o->offset;
dst = ((uint8_t *)target_obj) + o->offset;
return read_number(o, dst, num, den, intnum);
error:
*den=*intnum=0;
*den =
*intnum = 0;
return -1;
}
......@@ -398,7 +452,7 @@ int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_v
if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
return ret;
*out_val = num*intnum/den;
*out_val = num * intnum / den;
return 0;
}
......@@ -410,7 +464,7 @@ int av_opt_get_double(void *obj, const char *name, int search_flags, double *out
if ((ret = get_number(obj, name, &num, &den, &intnum, search_flags)) < 0)
return ret;
*out_val = num*intnum/den;
*out_val = num * intnum / den;
return 0;
}
......@@ -424,9 +478,9 @@ int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_
return ret;
if (num == 1.0 && (int)intnum == intnum)
*out_val = (AVRational){intnum, den};
*out_val = (AVRational) { intnum, den };
else
*out_val = av_d2q(num*intnum/den, 1<<24);
*out_val = av_d2q(num * intnum / den, 1 << 24);
return 0;
}
......@@ -463,7 +517,7 @@ int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
static void opt_list(void *obj, void *av_log_obj, const char *unit,
int req_flags, int rej_flags)
{
const AVOption *opt=NULL;
const AVOption *opt = NULL;
while ((opt = av_opt_next(obj, opt))) {
if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
......@@ -473,11 +527,11 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit,
* Don't print anything but CONST's on level two.
* Only print items from the requested unit.
*/
if (!unit && opt->type==AV_OPT_TYPE_CONST)
if (!unit && opt->type == AV_OPT_TYPE_CONST)
continue;
else if (unit && opt->type!=AV_OPT_TYPE_CONST)
else if (unit && opt->type != AV_OPT_TYPE_CONST)
continue;
else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
continue;
else if (unit && opt->type == AV_OPT_TYPE_CONST)
av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
......@@ -516,8 +570,8 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit,
}
av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM) ? 'V' : '.');
av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM) ? 'A' : '.');
av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
......@@ -525,10 +579,9 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit,
if (opt->help)
av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
av_log(av_log_obj, AV_LOG_INFO, "\n");
if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
}
}
}
int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
......@@ -536,7 +589,7 @@ int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
if (!obj)
return -1;
av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
......@@ -560,13 +613,15 @@ void av_opt_set_defaults(void *s)
av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
break;
case AV_OPT_TYPE_DOUBLE:
case AV_OPT_TYPE_FLOAT: {
case AV_OPT_TYPE_FLOAT:
{
double val;
val = opt->default_val.dbl;
av_opt_set_double(s, opt->name, val, 0);
}
break;
case AV_OPT_TYPE_RATIONAL: {
case AV_OPT_TYPE_RATIONAL:
{
AVRational val;
val = av_d2q(opt->default_val.dbl, INT_MAX);
av_opt_set_q(s, opt->name, val, 0);
......@@ -580,7 +635,8 @@ void av_opt_set_defaults(void *s)
/* Cannot set defaults for these types */
break;
default:
av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
opt->type, opt->name);
}
}
}
......@@ -706,7 +762,7 @@ const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
int opt_flags, int search_flags, void **target_obj)
{
const AVClass *c = *(AVClass**)obj;
const AVClass *c = *(AVClass **)obj;
const AVOption *o = NULL;
if (!c)
......@@ -744,7 +800,7 @@ const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
void *av_opt_child_next(void *obj, void *prev)
{
const AVClass *c = *(AVClass**)obj;
const AVClass *c = *(AVClass **)obj;
if (c->child_next)
return c->child_next(obj, prev);
return NULL;
......@@ -759,15 +815,22 @@ const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *pre
static int opt_size(enum AVOptionType type)
{
switch(type) {
switch (type) {
case AV_OPT_TYPE_INT:
case AV_OPT_TYPE_FLAGS: return sizeof(int);
case AV_OPT_TYPE_INT64: return sizeof(int64_t);
case AV_OPT_TYPE_DOUBLE: return sizeof(double);
case AV_OPT_TYPE_FLOAT: return sizeof(float);
case AV_OPT_TYPE_STRING: return sizeof(uint8_t*);
case AV_OPT_TYPE_RATIONAL: return sizeof(AVRational);
case AV_OPT_TYPE_BINARY: return sizeof(uint8_t*) + sizeof(int);
case AV_OPT_TYPE_FLAGS:
return sizeof(int);
case AV_OPT_TYPE_INT64:
return sizeof(int64_t);
case AV_OPT_TYPE_DOUBLE:
return sizeof(double);
case AV_OPT_TYPE_FLOAT:
return sizeof(float);
case AV_OPT_TYPE_STRING:
return sizeof(uint8_t *);
case AV_OPT_TYPE_RATIONAL:
return sizeof(AVRational);
case AV_OPT_TYPE_BINARY:
return sizeof(uint8_t *) + sizeof(int);
}
return AVERROR(EINVAL);
}
......@@ -781,22 +844,22 @@ int av_opt_copy(void *dst, const void *src)
if (!src)
return AVERROR(EINVAL);
c = *(AVClass**)src;
if (!c || c != *(AVClass**)dst)
c = *(AVClass **)src;
if (!c || c != *(AVClass **)dst)
return AVERROR(EINVAL);
while ((o = av_opt_next(src, o))) {
void *field_dst = ((uint8_t*)dst) + o->offset;
void *field_src = ((uint8_t*)src) + o->offset;
uint8_t **field_dst8 = (uint8_t**)field_dst;
uint8_t **field_src8 = (uint8_t**)field_src;
void *field_dst = (uint8_t *)dst + o->offset;
void *field_src = (uint8_t *)src + o->offset;
uint8_t **field_dst8 = (uint8_t **)field_dst;
uint8_t **field_src8 = (uint8_t **)field_src;
if (o->type == AV_OPT_TYPE_STRING) {
set_string(dst, o, *field_src8, field_dst8);
if (*field_src8 && !*field_dst8)
ret = AVERROR(ENOMEM);
} else if (o->type == AV_OPT_TYPE_BINARY) {
int len = *(int*)(field_src8 + 1);
int len = *(int *)(field_src8 + 1);
if (*field_dst8 != *field_src8)
av_freep(field_dst8);
if (len) {
......@@ -809,7 +872,7 @@ int av_opt_copy(void *dst, const void *src)
} else {
*field_dst8 = NULL;
}
*(int*)(field_dst8 + 1) = len;
*(int *)(field_dst8 + 1) = len;
} else if (o->type == AV_OPT_TYPE_CONST) {
// do nothing
} else {
......@@ -825,8 +888,7 @@ int av_opt_copy(void *dst, const void *src)
#ifdef TEST
typedef struct TestContext
{
typedef struct TestContext {
const AVClass *class;
int num;
int toggle;
......@@ -841,16 +903,16 @@ typedef struct TestContext
#define TEST_FLAG_LAME 02
#define TEST_FLAG_MU 04
static const AVOption test_options[]= {
{"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
{"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
{"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
{"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
{"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
{"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
{"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
{"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
{NULL},
static const AVOption test_options[] = {
{ "num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100 },
{ "toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1 },
{ "rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, 10 },
{ "string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { 0 }, CHAR_MIN, CHAR_MAX },
{ "flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, 0, "flags"},
{ "cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 0, "flags"},
{ "lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 0, "flags"},
{ "mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 0, "flags"},
{ NULL },
};
static const char *test_get_name(void *ctx)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment