dctref.c
Go to the documentation of this file.
1 /*
2  * reference discrete cosine transform (double precision)
3  * Copyright (C) 2009 Dylan Yudaken
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * reference discrete cosine transform (double precision)
25  *
26  * @author Dylan Yudaken (dyudaken at gmail)
27  *
28  * @note This file could be optimized a lot, but is for
29  * reference and so readability is better.
30  */
31 
32 #include "libavutil/mathematics.h"
33 #include "dctref.h"
34 
35 static double coefficients[8 * 8];
36 
37 /**
38  * Initialize the double precision discrete cosine transform
39  * functions fdct & idct.
40  */
42 {
43  unsigned int i, j;
44 
45  for (j = 0; j < 8; ++j) {
46  coefficients[j] = sqrt(0.125);
47  for (i = 8; i < 64; i += 8) {
48  coefficients[i + j] = 0.5 * cos(i * (j + 0.5) * M_PI / 64.0);
49  }
50  }
51 }
52 
53 /**
54  * Transform 8x8 block of data with a double precision forward DCT <br>
55  * This is a reference implementation.
56  *
57  * @param block pointer to 8x8 block of data to transform
58  */
59 void ff_ref_fdct(short *block)
60 {
61  /* implement the equation: block = coefficients * block * coefficients' */
62 
63  unsigned int i, j, k;
64  double out[8 * 8];
65 
66  /* out = coefficients * block */
67  for (i = 0; i < 64; i += 8) {
68  for (j = 0; j < 8; ++j) {
69  double tmp = 0;
70  for (k = 0; k < 8; ++k) {
71  tmp += coefficients[i + k] * block[k * 8 + j];
72  }
73  out[i + j] = tmp * 8;
74  }
75  }
76 
77  /* block = out * (coefficients') */
78  for (j = 0; j < 8; ++j) {
79  for (i = 0; i < 64; i += 8) {
80  double tmp = 0;
81  for (k = 0; k < 8; ++k) {
82  tmp += out[i + k] * coefficients[j * 8 + k];
83  }
84  block[i + j] = floor(tmp + 0.499999999999);
85  }
86  }
87 }
88 
89 /**
90  * Transform 8x8 block of data with a double precision inverse DCT <br>
91  * This is a reference implementation.
92  *
93  * @param block pointer to 8x8 block of data to transform
94  */
95 void ff_ref_idct(short *block)
96 {
97  /* implement the equation: block = (coefficients') * block * coefficients */
98 
99  unsigned int i, j, k;
100  double out[8 * 8];
101 
102  /* out = block * coefficients */
103  for (i = 0; i < 64; i += 8) {
104  for (j = 0; j < 8; ++j) {
105  double tmp = 0;
106  for (k = 0; k < 8; ++k) {
107  tmp += block[i + k] * coefficients[k * 8 + j];
108  }
109  out[i + j] = tmp;
110  }
111  }
112 
113  /* block = (coefficients') * out */
114  for (i = 0; i < 8; ++i) {
115  for (j = 0; j < 8; ++j) {
116  double tmp = 0;
117  for (k = 0; k < 64; k += 8) {
118  tmp += coefficients[k + i] * out[k + j];
119  }
120  block[i * 8 + j] = floor(tmp + 0.5);
121  }
122  }
123 }
av_cold void ff_ref_dct_init(void)
Initialize the double precision discrete cosine transform functions fdct & idct.
Definition: dctref.c:41
#define av_cold
Definition: attributes.h:78
integer sqrt
Definition: avutil.txt:2
static double coefficients[8 *8]
Definition: dctref.c:35
for k
synthesis window for stochastic i
void ff_ref_fdct(short *block)
Transform 8x8 block of data with a double precision forward DCT This is a reference implementation...
Definition: dctref.c:59
void ff_ref_idct(short *block)
Transform 8x8 block of data with a double precision inverse DCT This is a reference implementation...
Definition: dctref.c:95
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
#define M_PI
Definition: mathematics.h:46