Mercurial > libavcodec.hg
annotate dct.c @ 11520:a791382fd782 libavcodec
Reindent after r22618.
| author | vitor |
|---|---|
| date | Sun, 21 Mar 2010 11:36:17 +0000 |
| parents | c091ab3b4135 |
| children | f468aac92300 |
| rev | line source |
|---|---|
| 10944 | 1 /* |
| 2 * (I)DCT Transforms | |
| 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 | |
| 6 * | |
| 7 * This file is part of FFmpeg. | |
| 8 * | |
| 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. | |
| 13 * | |
| 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. | |
| 18 * | |
| 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 | |
| 22 */ | |
| 23 | |
| 24 /** | |
| 25 * @file libavcodec/dct.c | |
| 26 * (Inverse) Discrete Cosine Transforms. These are also known as the | |
| 27 * type II and type III DCTs respectively. | |
| 28 */ | |
| 29 | |
| 30 #include <math.h> | |
| 11370 | 31 #include "libavutil/mathematics.h" |
| 32 #include "fft.h" | |
| 10944 | 33 |
| 34 /* sin((M_PI * x / (2*n)) */ | |
| 35 #define SIN(s,n,x) (s->costab[(n) - (x)]) | |
| 36 | |
| 37 /* cos((M_PI * x / (2*n)) */ | |
| 38 #define COS(s,n,x) (s->costab[x]) | |
| 39 | |
|
11519
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
40 static void ff_dct_calc_III_c(DCTContext *ctx, FFTSample *data) |
| 10944 | 41 { |
| 42 int n = 1 << ctx->nbits; | |
| 43 int i; | |
| 44 | |
| 11520 | 45 float next = data[n - 1]; |
| 46 float inv_n = 1.0f / n; | |
| 10944 | 47 |
| 11520 | 48 for (i = n - 2; i >= 2; i -= 2) { |
| 49 float val1 = data[i ]; | |
| 50 float val2 = data[i - 1] - data[i + 1]; | |
| 51 float c = COS(ctx, n, i); | |
| 52 float s = SIN(ctx, n, i); | |
| 10944 | 53 |
| 11520 | 54 data[i ] = c * val1 + s * val2; |
| 55 data[i + 1] = s * val1 - c * val2; | |
| 56 } | |
| 10944 | 57 |
| 11520 | 58 data[1] = 2 * next; |
| 10944 | 59 |
| 11520 | 60 ff_rdft_calc(&ctx->rdft, data); |
| 10944 | 61 |
| 11520 | 62 for (i = 0; i < n / 2; i++) { |
| 63 float tmp1 = data[i ] * inv_n; | |
| 64 float tmp2 = data[n - i - 1] * inv_n; | |
| 65 float csc = ctx->csc2[i] * (tmp1 - tmp2); | |
| 10944 | 66 |
| 11520 | 67 tmp1 += tmp2; |
| 68 data[i ] = tmp1 + csc; | |
| 69 data[n - i - 1] = tmp1 - csc; | |
| 70 } | |
|
11519
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
71 } |
|
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
72 |
|
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
73 static void ff_dct_calc_II_c(DCTContext *ctx, FFTSample *data) |
|
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
74 { |
|
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
75 int n = 1 << ctx->nbits; |
|
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
76 int i; |
| 11520 | 77 float next; |
| 78 | |
| 79 for (i=0; i < n/2; i++) { | |
| 80 float tmp1 = data[i ]; | |
| 81 float tmp2 = data[n - i - 1]; | |
| 82 float s = SIN(ctx, n, 2*i + 1); | |
| 10944 | 83 |
| 11520 | 84 s *= tmp1 - tmp2; |
| 85 tmp1 = (tmp1 + tmp2) * 0.5f; | |
| 10944 | 86 |
| 11520 | 87 data[i ] = tmp1 + s; |
| 88 data[n-i-1] = tmp1 - s; | |
| 89 } | |
| 10944 | 90 |
| 11520 | 91 ff_rdft_calc(&ctx->rdft, data); |
| 10944 | 92 |
| 11520 | 93 next = data[1] * 0.5; |
| 94 data[1] *= -1; | |
| 10944 | 95 |
| 11520 | 96 for (i = n - 2; i >= 0; i -= 2) { |
| 97 float inr = data[i ]; | |
| 98 float ini = data[i + 1]; | |
| 99 float c = COS(ctx, n, i); | |
| 100 float s = SIN(ctx, n, i); | |
| 10944 | 101 |
| 11520 | 102 data[i ] = c * inr + s * ini; |
| 10944 | 103 |
| 11520 | 104 data[i+1] = next; |
| 10944 | 105 |
| 11520 | 106 next += s * inr - c * ini; |
| 107 } | |
| 10944 | 108 } |
| 109 | |
| 110 void ff_dct_calc(DCTContext *s, FFTSample *data) | |
| 111 { | |
|
11518
c4d18d452f82
Call DCT by function pointer. Needed for any future ASM implementation and
vitor
parents:
11517
diff
changeset
|
112 s->dct_calc(s, data); |
| 10944 | 113 } |
| 114 | |
|
11517
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
115 av_cold int ff_dct_init(DCTContext *s, int nbits, int inverse) |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
116 { |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
117 int n = 1 << nbits; |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
118 int i; |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
119 |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
120 s->nbits = nbits; |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
121 s->inverse = inverse; |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
122 |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
123 ff_init_ff_cos_tabs(nbits+2); |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
124 |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
125 s->costab = ff_cos_tabs[nbits+2]; |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
126 |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
127 s->csc2 = av_malloc(n/2 * sizeof(FFTSample)); |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
128 |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
129 if (ff_rdft_init(&s->rdft, nbits, inverse) < 0) { |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
130 av_free(s->csc2); |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
131 return -1; |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
132 } |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
133 |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
134 for (i = 0; i < n/2; i++) |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
135 s->csc2[i] = 0.5 / sin((M_PI / (2*n) * (2*i + 1))); |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
136 |
|
11519
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
137 if(inverse) { |
|
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
138 s->dct_calc = ff_dct_calc_III_c; |
|
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
139 } else |
|
c091ab3b4135
Split DCT-II and DCT-III in different functions, they do not share any code.
vitor
parents:
11518
diff
changeset
|
140 s->dct_calc = ff_dct_calc_II_c; |
|
11518
c4d18d452f82
Call DCT by function pointer. Needed for any future ASM implementation and
vitor
parents:
11517
diff
changeset
|
141 |
|
11517
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
142 return 0; |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
143 } |
|
e3b680f6c106
Cosmetics: move ff_dct_init() to the bottom of the file
vitor
parents:
11370
diff
changeset
|
144 |
| 10944 | 145 av_cold void ff_dct_end(DCTContext *s) |
| 146 { | |
| 147 ff_rdft_end(&s->rdft); | |
| 148 av_free(s->csc2); | |
| 149 } |
