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 <string.h>
00032
00033 #include "libavutil/mathematics.h"
00034 #include "dct.h"
00035 #include "dct32.h"
00036
00037
00038 #define SIN(s, n, x) (s->costab[(n) - (x)])
00039
00040
00041 #define COS(s, n, x) (s->costab[x])
00042
00043 static void ff_dst_calc_I_c(DCTContext *ctx, FFTSample *data)
00044 {
00045 int n = 1 << ctx->nbits;
00046 int i;
00047
00048 data[0] = 0;
00049 for (i = 1; i < n / 2; i++) {
00050 float tmp1 = data[i ];
00051 float tmp2 = data[n - i];
00052 float s = SIN(ctx, n, 2 * i);
00053
00054 s *= tmp1 + tmp2;
00055 tmp1 = (tmp1 - tmp2) * 0.5f;
00056 data[i] = s + tmp1;
00057 data[n - i] = s - tmp1;
00058 }
00059
00060 data[n / 2] *= 2;
00061 ctx->rdft.rdft_calc(&ctx->rdft, data);
00062
00063 data[0] *= 0.5f;
00064
00065 for (i = 1; i < n - 2; i += 2) {
00066 data[i + 1] += data[i - 1];
00067 data[i] = -data[i + 2];
00068 }
00069
00070 data[n - 1] = 0;
00071 }
00072
00073 static void ff_dct_calc_I_c(DCTContext *ctx, FFTSample *data)
00074 {
00075 int n = 1 << ctx->nbits;
00076 int i;
00077 float next = -0.5f * (data[0] - data[n]);
00078
00079 for (i = 0; i < n / 2; i++) {
00080 float tmp1 = data[i];
00081 float tmp2 = data[n - i];
00082 float s = SIN(ctx, n, 2 * i);
00083 float c = COS(ctx, n, 2 * i);
00084
00085 c *= tmp1 - tmp2;
00086 s *= tmp1 - tmp2;
00087
00088 next += c;
00089
00090 tmp1 = (tmp1 + tmp2) * 0.5f;
00091 data[i] = tmp1 - s;
00092 data[n - i] = tmp1 + s;
00093 }
00094
00095 ctx->rdft.rdft_calc(&ctx->rdft, data);
00096 data[n] = data[1];
00097 data[1] = next;
00098
00099 for (i = 3; i <= n; i += 2)
00100 data[i] = data[i - 2] - data[i];
00101 }
00102
00103 static void ff_dct_calc_III_c(DCTContext *ctx, FFTSample *data)
00104 {
00105 int n = 1 << ctx->nbits;
00106 int i;
00107
00108 float next = data[n - 1];
00109 float inv_n = 1.0f / n;
00110
00111 for (i = n - 2; i >= 2; i -= 2) {
00112 float val1 = data[i];
00113 float val2 = data[i - 1] - data[i + 1];
00114 float c = COS(ctx, n, i);
00115 float s = SIN(ctx, n, i);
00116
00117 data[i] = c * val1 + s * val2;
00118 data[i + 1] = s * val1 - c * val2;
00119 }
00120
00121 data[1] = 2 * next;
00122
00123 ctx->rdft.rdft_calc(&ctx->rdft, data);
00124
00125 for (i = 0; i < n / 2; i++) {
00126 float tmp1 = data[i] * inv_n;
00127 float tmp2 = data[n - i - 1] * inv_n;
00128 float csc = ctx->csc2[i] * (tmp1 - tmp2);
00129
00130 tmp1 += tmp2;
00131 data[i] = tmp1 + csc;
00132 data[n - i - 1] = tmp1 - csc;
00133 }
00134 }
00135
00136 static void ff_dct_calc_II_c(DCTContext *ctx, FFTSample *data)
00137 {
00138 int n = 1 << ctx->nbits;
00139 int i;
00140 float next;
00141
00142 for (i = 0; i < n / 2; i++) {
00143 float tmp1 = data[i];
00144 float tmp2 = data[n - i - 1];
00145 float s = SIN(ctx, n, 2 * i + 1);
00146
00147 s *= tmp1 - tmp2;
00148 tmp1 = (tmp1 + tmp2) * 0.5f;
00149
00150 data[i] = tmp1 + s;
00151 data[n-i-1] = tmp1 - s;
00152 }
00153
00154 ctx->rdft.rdft_calc(&ctx->rdft, data);
00155
00156 next = data[1] * 0.5;
00157 data[1] *= -1;
00158
00159 for (i = n - 2; i >= 0; i -= 2) {
00160 float inr = data[i ];
00161 float ini = data[i + 1];
00162 float c = COS(ctx, n, i);
00163 float s = SIN(ctx, n, i);
00164
00165 data[i] = c * inr + s * ini;
00166 data[i + 1] = next;
00167
00168 next += s * inr - c * ini;
00169 }
00170 }
00171
00172 static void dct32_func(DCTContext *ctx, FFTSample *data)
00173 {
00174 ctx->dct32(data, data);
00175 }
00176
00177 av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
00178 {
00179 int n = 1 << nbits;
00180 int i;
00181
00182 memset(s, 0, sizeof(*s));
00183
00184 s->nbits = nbits;
00185 s->inverse = inverse;
00186
00187 if (inverse == DCT_II && nbits == 5) {
00188 s->dct_calc = dct32_func;
00189 } else {
00190 ff_init_ff_cos_tabs(nbits + 2);
00191
00192 s->costab = ff_cos_tabs[nbits + 2];
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)
00213 ff_dct_init_mmx(s);
00214
00215 return 0;
00216 }
00217
00218 av_cold void ff_dct_end(DCTContext *s)
00219 {
00220 ff_rdft_end(&s->rdft);
00221 av_free(s->csc2);
00222 }