00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00030 #include <math.h>
00031 #include "libavutil/mathematics.h"
00032 #include "dct.h"
00033 #include "dct32.h"
00034
00035
00036 #define SIN(s,n,x) (s->costab[(n) - (x)])
00037
00038
00039 #define COS(s,n,x) (s->costab[x])
00040
00041 static void ff_dst_calc_I_c(DCTContext *ctx, FFTSample *data)
00042 {
00043 int n = 1 << ctx->nbits;
00044 int i;
00045
00046 data[0] = 0;
00047 for(i = 1; i < n/2; i++) {
00048 float tmp1 = data[i ];
00049 float tmp2 = data[n - i];
00050 float s = SIN(ctx, n, 2*i);
00051
00052 s *= tmp1 + tmp2;
00053 tmp1 = (tmp1 - tmp2) * 0.5f;
00054 data[i ] = s + tmp1;
00055 data[n - i] = s - tmp1;
00056 }
00057
00058 data[n/2] *= 2;
00059 ctx->rdft.rdft_calc(&ctx->rdft, data);
00060
00061 data[0] *= 0.5f;
00062
00063 for(i = 1; i < n-2; i += 2) {
00064 data[i + 1] += data[i - 1];
00065 data[i ] = -data[i + 2];
00066 }
00067
00068 data[n-1] = 0;
00069 }
00070
00071 static void ff_dct_calc_I_c(DCTContext *ctx, FFTSample *data)
00072 {
00073 int n = 1 << ctx->nbits;
00074 int i;
00075 float next = -0.5f * (data[0] - data[n]);
00076
00077 for(i = 0; i < n/2; i++) {
00078 float tmp1 = data[i ];
00079 float tmp2 = data[n - i];
00080 float s = SIN(ctx, n, 2*i);
00081 float c = COS(ctx, n, 2*i);
00082
00083 c *= tmp1 - tmp2;
00084 s *= tmp1 - tmp2;
00085
00086 next += c;
00087
00088 tmp1 = (tmp1 + tmp2) * 0.5f;
00089 data[i ] = tmp1 - s;
00090 data[n - i] = tmp1 + s;
00091 }
00092
00093 ctx->rdft.rdft_calc(&ctx->rdft, data);
00094 data[n] = data[1];
00095 data[1] = next;
00096
00097 for(i = 3; i <= n; i += 2)
00098 data[i] = data[i - 2] - data[i];
00099 }
00100
00101 static void ff_dct_calc_III_c(DCTContext *ctx, FFTSample *data)
00102 {
00103 int n = 1 << ctx->nbits;
00104 int i;
00105
00106 float next = data[n - 1];
00107 float inv_n = 1.0f / n;
00108
00109 for (i = n - 2; i >= 2; i -= 2) {
00110 float val1 = data[i ];
00111 float val2 = data[i - 1] - data[i + 1];
00112 float c = COS(ctx, n, i);
00113 float s = SIN(ctx, n, i);
00114
00115 data[i ] = c * val1 + s * val2;
00116 data[i + 1] = s * val1 - c * val2;
00117 }
00118
00119 data[1] = 2 * next;
00120
00121 ctx->rdft.rdft_calc(&ctx->rdft, data);
00122
00123 for (i = 0; i < n / 2; i++) {
00124 float tmp1 = data[i ] * inv_n;
00125 float tmp2 = data[n - i - 1] * inv_n;
00126 float csc = ctx->csc2[i] * (tmp1 - tmp2);
00127
00128 tmp1 += tmp2;
00129 data[i ] = tmp1 + csc;
00130 data[n - i - 1] = tmp1 - csc;
00131 }
00132 }
00133
00134 static void ff_dct_calc_II_c(DCTContext *ctx, FFTSample *data)
00135 {
00136 int n = 1 << ctx->nbits;
00137 int i;
00138 float next;
00139
00140 for (i=0; i < n/2; i++) {
00141 float tmp1 = data[i ];
00142 float tmp2 = data[n - i - 1];
00143 float s = SIN(ctx, n, 2*i + 1);
00144
00145 s *= tmp1 - tmp2;
00146 tmp1 = (tmp1 + tmp2) * 0.5f;
00147
00148 data[i ] = tmp1 + s;
00149 data[n-i-1] = tmp1 - s;
00150 }
00151
00152 ctx->rdft.rdft_calc(&ctx->rdft, data);
00153
00154 next = data[1] * 0.5;
00155 data[1] *= -1;
00156
00157 for (i = n - 2; i >= 0; i -= 2) {
00158 float inr = data[i ];
00159 float ini = data[i + 1];
00160 float c = COS(ctx, n, i);
00161 float s = SIN(ctx, n, i);
00162
00163 data[i ] = c * inr + s * ini;
00164
00165 data[i+1] = next;
00166
00167 next += s * inr - c * ini;
00168 }
00169 }
00170
00171 static void dct32_func(DCTContext *ctx, FFTSample *data)
00172 {
00173 ctx->dct32(data, data);
00174 }
00175
00176 av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
00177 {
00178 int n = 1 << nbits;
00179 int i;
00180
00181 memset(s, 0, sizeof(*s));
00182
00183 s->nbits = nbits;
00184 s->inverse = inverse;
00185
00186 if (inverse == DCT_II && nbits == 5) {
00187 s->dct_calc = dct32_func;
00188 } else {
00189 ff_init_ff_cos_tabs(nbits+2);
00190
00191 s->costab = ff_cos_tabs[nbits+2];
00192
00193 s->csc2 = av_malloc(n/2 * sizeof(FFTSample));
00194
00195 if (ff_rdft_init(&s->rdft, nbits, inverse == DCT_III) < 0) {
00196 av_free(s->csc2);
00197 return -1;
00198 }
00199
00200 for (i = 0; i < n/2; i++)
00201 s->csc2[i] = 0.5 / sin((M_PI / (2*n) * (2*i + 1)));
00202
00203 switch(inverse) {
00204 case DCT_I : s->dct_calc = ff_dct_calc_I_c; break;
00205 case DCT_II : s->dct_calc = ff_dct_calc_II_c ; break;
00206 case DCT_III: s->dct_calc = ff_dct_calc_III_c; break;
00207 case DST_I : s->dct_calc = ff_dst_calc_I_c; break;
00208 }
00209 }
00210
00211 s->dct32 = ff_dct32_float;
00212 if (HAVE_MMX) ff_dct_init_mmx(s);
00213
00214 return 0;
00215 }
00216
00217 av_cold void ff_dct_end(DCTContext *s)
00218 {
00219 ff_rdft_end(&s->rdft);
00220 av_free(s->csc2);
00221 }