From e1484ebed6d7a7e193d27e5d5fdf1ee585127081 Mon Sep 17 00:00:00 2001 From: Vitor Sessak Date: Tue, 23 Mar 2010 19:48:16 +0000 Subject: [PATCH] Implement the discrete sine/cosine transforms DCT-I and DST-I Originally committed as revision 22649 to svn://svn.ffmpeg.org/ffmpeg/trunk --- doc/APIchanges | 3 ++ libavcodec/avfft.c | 2 +- libavcodec/avfft.h | 18 ++++++++--- libavcodec/dct.c | 75 +++++++++++++++++++++++++++++++++++++++++----- libavcodec/fft.h | 11 ++++--- 5 files changed, 93 insertions(+), 16 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index a7936c5677..57b01f68a1 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -12,6 +12,9 @@ libavutil: 2009-03-08 API changes, most recent first: +2010-03-15 - r22649 - lavc 52.59.0 - av_dct_init() + Support DCT-I and DST-I + 2010-03-15 - r22540 - lavf 52.56.0 - AVFormatContext.start_time_realtime Add AVFormatContext.start_time_realtime field. diff --git a/libavcodec/avfft.c b/libavcodec/avfft.c index c49c9de117..25fc4e0955 100644 --- a/libavcodec/avfft.c +++ b/libavcodec/avfft.c @@ -116,7 +116,7 @@ void av_rdft_end(RDFTContext *s) #if CONFIG_DCT -DCTContext *av_dct_init(int nbits, int inverse) +DCTContext *av_dct_init(int nbits, enum DCTTransformType inverse) { DCTContext *s = av_malloc(sizeof(*s)); diff --git a/libavcodec/avfft.h b/libavcodec/avfft.h index d0babc0909..623f0a33b5 100644 --- a/libavcodec/avfft.h +++ b/libavcodec/avfft.h @@ -77,12 +77,22 @@ void av_rdft_end(RDFTContext *s); typedef struct DCTContext DCTContext; +enum DCTTransformType { + DCT_II = 0, + DCT_III, + DCT_I, + DST_I, +}; + /** - * Set up (Inverse)DCT. - * @param nbits log2 of the length of the input array - * @param inverse >0 forward transform, <0 inverse transform + * Sets up DCT. + * @param nbits size of the input array: + * (1 << nbits) for DCT-II, DCT-III and DST-I + * (1 << nbits) + 1 for DCT-I + * + * @note the first element of the input of DST-I is ignored */ -DCTContext *av_dct_init(int nbits, int inverse); +DCTContext *av_dct_init(int nbits, enum DCTTransformType type); void av_dct_calc(DCTContext *s, FFTSample *data); void av_dct_end (DCTContext *s); diff --git a/libavcodec/dct.c b/libavcodec/dct.c index 537601b2e9..57314c29cb 100644 --- a/libavcodec/dct.c +++ b/libavcodec/dct.c @@ -37,6 +37,66 @@ /* cos((M_PI * x / (2*n)) */ #define COS(s,n,x) (s->costab[x]) +static void ff_dst_calc_I_c(DCTContext *ctx, FFTSample *data) +{ + int n = 1 << ctx->nbits; + int i; + + data[0] = 0; + for(i = 1; i < n/2; i++) { + float tmp1 = data[i ]; + float tmp2 = data[n - i]; + float s = SIN(ctx, n, 2*i); + + s *= tmp1 + tmp2; + tmp1 = (tmp1 - tmp2) * 0.5f; + data[i ] = s + tmp1; + data[n - i] = s - tmp1; + } + + data[n/2] *= 2; + ff_rdft_calc(&ctx->rdft, data); + + data[0] *= 0.5f; + + for(i = 1; i < n-2; i += 2) { + data[i + 1] += data[i - 1]; + data[i ] = -data[i + 2]; + } + + data[n-1] = 0; +} + +static void ff_dct_calc_I_c(DCTContext *ctx, FFTSample *data) +{ + int n = 1 << ctx->nbits; + int i; + float next = -0.5f * (data[0] - data[n]); + + for(i = 0; i < n/2; i++) { + float tmp1 = data[i ]; + float tmp2 = data[n - i]; + float s = SIN(ctx, n, 2*i); + float c = COS(ctx, n, 2*i); + + c *= tmp1 - tmp2; + s *= tmp1 - tmp2; + + next += c; + + tmp1 = (tmp1 + tmp2) * 0.5f; + data[i ] = tmp1 - s; + data[n - i] = tmp1 + s; + } + + ff_rdft_calc(&ctx->rdft, data); + data[n] = data[1]; + data[1] = next; + + for(i = 3; i <= n; i += 2) + data[i] = data[i - 2] - data[i]; +} + static void ff_dct_calc_III_c(DCTContext *ctx, FFTSample *data) { int n = 1 << ctx->nbits; @@ -112,7 +172,7 @@ void ff_dct_calc(DCTContext *s, FFTSample *data) s->dct_calc(s, data); } -av_cold int ff_dct_init(DCTContext *s, int nbits, int inverse) +av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse) { int n = 1 << nbits; int i; @@ -126,7 +186,7 @@ av_cold int ff_dct_init(DCTContext *s, int nbits, int inverse) s->csc2 = av_malloc(n/2 * sizeof(FFTSample)); - if (ff_rdft_init(&s->rdft, nbits, inverse) < 0) { + if (ff_rdft_init(&s->rdft, nbits, inverse == DCT_III) < 0) { av_free(s->csc2); return -1; } @@ -134,11 +194,12 @@ av_cold int ff_dct_init(DCTContext *s, int nbits, int inverse) for (i = 0; i < n/2; i++) s->csc2[i] = 0.5 / sin((M_PI / (2*n) * (2*i + 1))); - if(inverse) { - s->dct_calc = ff_dct_calc_III_c; - } else - s->dct_calc = ff_dct_calc_II_c; - + switch(inverse) { + case DCT_I : s->dct_calc = ff_dct_calc_I_c; break; + case DCT_II : s->dct_calc = ff_dct_calc_II_c ; break; + case DCT_III: s->dct_calc = ff_dct_calc_III_c; break; + case DST_I : s->dct_calc = ff_dst_calc_I_c; break; + } return 0; } diff --git a/libavcodec/fft.h b/libavcodec/fft.h index 6a8f41e79e..1f5b2e86da 100644 --- a/libavcodec/fft.h +++ b/libavcodec/fft.h @@ -226,11 +226,14 @@ struct DCTContext { }; /** - * Sets up (Inverse)DCT. - * @param nbits log2 of the length of the input array - * @param inverse >0 forward transform, <0 inverse transform + * Sets up DCT. + * @param nbits size of the input array: + * (1 << nbits) for DCT-II, DCT-III and DST-I + * (1 << nbits) + 1 for DCT-I + * + * @note the first element of the input of DST-I is ignored */ -int ff_dct_init(DCTContext *s, int nbits, int inverse); +int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType type); void ff_dct_calc(DCTContext *s, FFTSample *data); void ff_dct_end (DCTContext *s);