3 * Copyright (c) 2009 Peter Ross <pross@xvid.org>
4 * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
5 * Copyright (c) 2010 Vitor Sessak
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 * (Inverse) Discrete Cosine Transforms. These are also known as the
27 * type II and type III DCTs respectively.
31 #include "libavutil/mathematics.h"
35 /* sin((M_PI * x / (2*n)) */
36 #define SIN(s,n,x) (s->costab[(n) - (x)])
38 /* cos((M_PI * x / (2*n)) */
39 #define COS(s,n,x) (s->costab[x])
41 static void ff_dst_calc_I_c(DCTContext *ctx, FFTSample *data)
43 int n = 1 << ctx->nbits;
47 for(i = 1; i < n/2; i++) {
48 float tmp1 = data[i ];
49 float tmp2 = data[n - i];
50 float s = SIN(ctx, n, 2*i);
53 tmp1 = (tmp1 - tmp2) * 0.5f;
55 data[n - i] = s - tmp1;
59 ctx->rdft.rdft_calc(&ctx->rdft, data);
63 for(i = 1; i < n-2; i += 2) {
64 data[i + 1] += data[i - 1];
65 data[i ] = -data[i + 2];
71 static void ff_dct_calc_I_c(DCTContext *ctx, FFTSample *data)
73 int n = 1 << ctx->nbits;
75 float next = -0.5f * (data[0] - data[n]);
77 for(i = 0; i < n/2; i++) {
78 float tmp1 = data[i ];
79 float tmp2 = data[n - i];
80 float s = SIN(ctx, n, 2*i);
81 float c = COS(ctx, n, 2*i);
88 tmp1 = (tmp1 + tmp2) * 0.5f;
90 data[n - i] = tmp1 + s;
93 ctx->rdft.rdft_calc(&ctx->rdft, data);
97 for(i = 3; i <= n; i += 2)
98 data[i] = data[i - 2] - data[i];
101 static void ff_dct_calc_III_c(DCTContext *ctx, FFTSample *data)
103 int n = 1 << ctx->nbits;
106 float next = data[n - 1];
107 float inv_n = 1.0f / n;
109 for (i = n - 2; i >= 2; i -= 2) {
110 float val1 = data[i ];
111 float val2 = data[i - 1] - data[i + 1];
112 float c = COS(ctx, n, i);
113 float s = SIN(ctx, n, i);
115 data[i ] = c * val1 + s * val2;
116 data[i + 1] = s * val1 - c * val2;
121 ctx->rdft.rdft_calc(&ctx->rdft, data);
123 for (i = 0; i < n / 2; i++) {
124 float tmp1 = data[i ] * inv_n;
125 float tmp2 = data[n - i - 1] * inv_n;
126 float csc = ctx->csc2[i] * (tmp1 - tmp2);
129 data[i ] = tmp1 + csc;
130 data[n - i - 1] = tmp1 - csc;
134 static void ff_dct_calc_II_c(DCTContext *ctx, FFTSample *data)
136 int n = 1 << ctx->nbits;
140 for (i=0; i < n/2; i++) {
141 float tmp1 = data[i ];
142 float tmp2 = data[n - i - 1];
143 float s = SIN(ctx, n, 2*i + 1);
146 tmp1 = (tmp1 + tmp2) * 0.5f;
149 data[n-i-1] = tmp1 - s;
152 ctx->rdft.rdft_calc(&ctx->rdft, data);
154 next = data[1] * 0.5;
157 for (i = n - 2; i >= 0; i -= 2) {
158 float inr = data[i ];
159 float ini = data[i + 1];
160 float c = COS(ctx, n, i);
161 float s = SIN(ctx, n, i);
163 data[i ] = c * inr + s * ini;
167 next += s * inr - c * ini;
171 static void dct32_func(DCTContext *ctx, FFTSample *data)
173 ctx->dct32(data, data);
176 av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
181 memset(s, 0, sizeof(*s));
184 s->inverse = inverse;
186 if (inverse == DCT_II && nbits == 5) {
187 s->dct_calc = dct32_func;
189 ff_init_ff_cos_tabs(nbits+2);
191 s->costab = ff_cos_tabs[nbits+2];
193 s->csc2 = av_malloc(n/2 * sizeof(FFTSample));
195 if (ff_rdft_init(&s->rdft, nbits, inverse == DCT_III) < 0) {
200 for (i = 0; i < n/2; i++)
201 s->csc2[i] = 0.5 / sin((M_PI / (2*n) * (2*i + 1)));
204 case DCT_I : s->dct_calc = ff_dct_calc_I_c; break;
205 case DCT_II : s->dct_calc = ff_dct_calc_II_c ; break;
206 case DCT_III: s->dct_calc = ff_dct_calc_III_c; break;
207 case DST_I : s->dct_calc = ff_dst_calc_I_c; break;
211 s->dct32 = ff_dct32_float;
212 if (HAVE_MMX) ff_dct_init_mmx(s);
217 av_cold void ff_dct_end(DCTContext *s)
219 ff_rdft_end(&s->rdft);