Libav
opus_imdct.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 Mozilla Corporation
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include <float.h>
27 #include <math.h>
28 #include <stddef.h>
29 
30 #include "config.h"
31 
32 #include "libavutil/attributes.h"
33 #include "libavutil/common.h"
34 
35 #include "avfft.h"
36 #include "opus.h"
37 #include "opus_imdct.h"
38 
39 // minimal iMDCT size to make SIMD opts easier
40 #define CELT_MIN_IMDCT_SIZE 120
41 
42 // complex c = a * b
43 #define CMUL3(cre, cim, are, aim, bre, bim) \
44 do { \
45  cre = are * bre - aim * bim; \
46  cim = are * bim + aim * bre; \
47 } while (0)
48 
49 #define CMUL(c, a, b) CMUL3((c).re, (c).im, (a).re, (a).im, (b).re, (b).im)
50 
51 // complex c = a * b
52 // d = a * conjugate(b)
53 #define CMUL2(c, d, a, b) \
54 do { \
55  float are = (a).re; \
56  float aim = (a).im; \
57  float bre = (b).re; \
58  float bim = (b).im; \
59  float rr = are * bre; \
60  float ri = are * bim; \
61  float ir = aim * bre; \
62  float ii = aim * bim; \
63  (c).re = rr - ii; \
64  (c).im = ri + ir; \
65  (d).re = rr + ii; \
66  (d).im = -ri + ir; \
67 } while (0)
68 
70 {
71  CeltIMDCTContext *s = *ps;
72  int i;
73 
74  if (!s)
75  return;
76 
77  for (i = 0; i < FF_ARRAY_ELEMS(s->exptab); i++)
78  av_freep(&s->exptab[i]);
79 
81 
82  av_freep(&s->tmp);
83 
84  av_freep(ps);
85 }
86 
87 static void celt_imdct_half(CeltIMDCTContext *s, float *dst, const float *src,
88  ptrdiff_t stride, float scale);
89 
91 {
93  int len2 = 15 * (1 << N);
94  int len = 2 * len2;
95  int i, j;
96 
97  if (len2 > CELT_MAX_FRAME_SIZE || len2 < CELT_MIN_IMDCT_SIZE)
98  return AVERROR(EINVAL);
99 
100  s = av_mallocz(sizeof(*s));
101  if (!s)
102  return AVERROR(ENOMEM);
103 
104  s->fft_n = N - 1;
105  s->len4 = len2 / 2;
106  s->len2 = len2;
107 
108  s->tmp = av_malloc(len * 2 * sizeof(*s->tmp));
109  if (!s->tmp)
110  goto fail;
111 
112  s->twiddle_exptab = av_malloc(s->len4 * sizeof(*s->twiddle_exptab));
113  if (!s->twiddle_exptab)
114  goto fail;
115 
116  for (i = 0; i < s->len4; i++) {
117  s->twiddle_exptab[i].re = cos(2 * M_PI * (i + 0.125 + s->len4) / len);
118  s->twiddle_exptab[i].im = sin(2 * M_PI * (i + 0.125 + s->len4) / len);
119  }
120 
121  for (i = 0; i < FF_ARRAY_ELEMS(s->exptab); i++) {
122  int N = 15 * (1 << i);
123  s->exptab[i] = av_malloc(sizeof(*s->exptab[i]) * FFMAX(N, 19));
124  if (!s->exptab[i])
125  goto fail;
126 
127  for (j = 0; j < N; j++) {
128  s->exptab[i][j].re = cos(2 * M_PI * j / N);
129  s->exptab[i][j].im = sin(2 * M_PI * j / N);
130  }
131  }
132 
133  // wrap around to simplify fft15
134  for (j = 15; j < 19; j++)
135  s->exptab[0][j] = s->exptab[0][j - 15];
136 
138 
139  if (ARCH_AARCH64)
141 
142  *ps = s;
143 
144  return 0;
145 fail:
147  return AVERROR(ENOMEM);
148 }
149 
150 static void fft5(FFTComplex *out, const FFTComplex *in, ptrdiff_t stride)
151 {
152  // [0] = exp(2 * i * pi / 5), [1] = exp(2 * i * pi * 2 / 5)
153  static const FFTComplex fact[] = { { 0.30901699437494745, 0.95105651629515353 },
154  { -0.80901699437494734, 0.58778525229247325 } };
155 
156  FFTComplex z[4][4];
157 
158  CMUL2(z[0][0], z[0][3], in[1 * stride], fact[0]);
159  CMUL2(z[0][1], z[0][2], in[1 * stride], fact[1]);
160  CMUL2(z[1][0], z[1][3], in[2 * stride], fact[0]);
161  CMUL2(z[1][1], z[1][2], in[2 * stride], fact[1]);
162  CMUL2(z[2][0], z[2][3], in[3 * stride], fact[0]);
163  CMUL2(z[2][1], z[2][2], in[3 * stride], fact[1]);
164  CMUL2(z[3][0], z[3][3], in[4 * stride], fact[0]);
165  CMUL2(z[3][1], z[3][2], in[4 * stride], fact[1]);
166 
167  out[0].re = in[0].re + in[stride].re + in[2 * stride].re + in[3 * stride].re + in[4 * stride].re;
168  out[0].im = in[0].im + in[stride].im + in[2 * stride].im + in[3 * stride].im + in[4 * stride].im;
169 
170  out[1].re = in[0].re + z[0][0].re + z[1][1].re + z[2][2].re + z[3][3].re;
171  out[1].im = in[0].im + z[0][0].im + z[1][1].im + z[2][2].im + z[3][3].im;
172 
173  out[2].re = in[0].re + z[0][1].re + z[1][3].re + z[2][0].re + z[3][2].re;
174  out[2].im = in[0].im + z[0][1].im + z[1][3].im + z[2][0].im + z[3][2].im;
175 
176  out[3].re = in[0].re + z[0][2].re + z[1][0].re + z[2][3].re + z[3][1].re;
177  out[3].im = in[0].im + z[0][2].im + z[1][0].im + z[2][3].im + z[3][1].im;
178 
179  out[4].re = in[0].re + z[0][3].re + z[1][2].re + z[2][1].re + z[3][0].re;
180  out[4].im = in[0].im + z[0][3].im + z[1][2].im + z[2][1].im + z[3][0].im;
181 }
182 
183 static void fft15(CeltIMDCTContext *s, FFTComplex *out, const FFTComplex *in, ptrdiff_t stride)
184 {
185  const FFTComplex *exptab = s->exptab[0];
186  FFTComplex tmp[5];
187  FFTComplex tmp1[5];
188  FFTComplex tmp2[5];
189  int k;
190 
191  fft5(tmp, in, stride * 3);
192  fft5(tmp1, in + stride, stride * 3);
193  fft5(tmp2, in + 2 * stride, stride * 3);
194 
195  for (k = 0; k < 5; k++) {
196  FFTComplex t1, t2;
197 
198  CMUL(t1, tmp1[k], exptab[k]);
199  CMUL(t2, tmp2[k], exptab[2 * k]);
200  out[k].re = tmp[k].re + t1.re + t2.re;
201  out[k].im = tmp[k].im + t1.im + t2.im;
202 
203  CMUL(t1, tmp1[k], exptab[k + 5]);
204  CMUL(t2, tmp2[k], exptab[2 * (k + 5)]);
205  out[k + 5].re = tmp[k].re + t1.re + t2.re;
206  out[k + 5].im = tmp[k].im + t1.im + t2.im;
207 
208  CMUL(t1, tmp1[k], exptab[k + 10]);
209  CMUL(t2, tmp2[k], exptab[2 * k + 5]);
210  out[k + 10].re = tmp[k].re + t1.re + t2.re;
211  out[k + 10].im = tmp[k].im + t1.im + t2.im;
212  }
213 }
214 
215 /*
216  * FFT of the length 15 * (2^N)
217  */
219  int N, ptrdiff_t stride)
220 {
221  if (N) {
222  const FFTComplex *exptab = s->exptab[N];
223  const int len2 = 15 * (1 << (N - 1));
224  int k;
225 
226  fft_calc(s, out, in, N - 1, stride * 2);
227  fft_calc(s, out + len2, in + stride, N - 1, stride * 2);
228 
229  for (k = 0; k < len2; k++) {
230  FFTComplex t;
231 
232  CMUL(t, out[len2 + k], exptab[k]);
233 
234  out[len2 + k].re = out[k].re - t.re;
235  out[len2 + k].im = out[k].im - t.im;
236 
237  out[k].re += t.re;
238  out[k].im += t.im;
239  }
240  } else
241  fft15(s, out, in, stride);
242 }
243 
244 static void celt_imdct_half(CeltIMDCTContext *s, float *dst, const float *src,
245  ptrdiff_t stride, float scale)
246 {
247  FFTComplex *z = (FFTComplex *)dst;
248  const int len8 = s->len4 / 2;
249  const float *in1 = src;
250  const float *in2 = src + (s->len2 - 1) * stride;
251  int i;
252 
253  for (i = 0; i < s->len4; i++) {
254  FFTComplex tmp = { *in2, *in1 };
255  CMUL(s->tmp[i], tmp, s->twiddle_exptab[i]);
256  in1 += 2 * stride;
257  in2 -= 2 * stride;
258  }
259 
260  fft_calc(s, z, s->tmp, s->fft_n, 1);
261 
262  for (i = 0; i < len8; i++) {
263  float r0, i0, r1, i1;
264 
265  CMUL3(r0, i1, z[len8 - i - 1].im, z[len8 - i - 1].re, s->twiddle_exptab[len8 - i - 1].im, s->twiddle_exptab[len8 - i - 1].re);
266  CMUL3(r1, i0, z[len8 + i].im, z[len8 + i].re, s->twiddle_exptab[len8 + i].im, s->twiddle_exptab[len8 + i].re);
267  z[len8 - i - 1].re = scale * r0;
268  z[len8 - i - 1].im = scale * i0;
269  z[len8 + i].re = scale * r1;
270  z[len8 + i].im = scale * i1;
271  }
272 }
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
void(* imdct_half)(struct CeltIMDCTContext *s, float *dst, const float *src, ptrdiff_t src_stride, float scale)
Calculate the middle half of the iMDCT.
Definition: opus_imdct.h:40
static struct @16 * exptab
FFTComplex * exptab[6]
Definition: opus_imdct.h:35
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static void fft_calc(CeltIMDCTContext *s, FFTComplex *out, const FFTComplex *in, int N, ptrdiff_t stride)
Definition: opus_imdct.c:218
FFTSample re
Definition: avfft.h:38
#define FF_ARRAY_ELEMS(a)
int stride
Definition: mace.c:144
Macro definitions for various function/variable attributes.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
#define ARCH_AARCH64
Definition: config.h:12
#define av_cold
Definition: attributes.h:66
FFTComplex * tmp
Definition: opus_imdct.h:31
av_cold void ff_celt_imdct_uninit(CeltIMDCTContext **ps)
Free an iMDCT.
Definition: opus_imdct.c:69
#define CMUL(c, a, b)
Definition: opus_imdct.c:49
#define AVERROR(e)
Definition: error.h:43
FFTComplex * twiddle_exptab
Definition: opus_imdct.h:33
#define FFMAX(a, b)
Definition: common.h:55
#define CELT_MIN_IMDCT_SIZE
Definition: opus_imdct.c:40
if(ac->has_optimized_func)
float im
Definition: fft-test.c:69
FFT functions.
av_cold int ff_celt_imdct_init(CeltIMDCTContext **ps, int N)
Init an iMDCT of the length 2 * 15 * (2^N)
Definition: opus_imdct.c:90
#define CMUL2(c, d, a, b)
Definition: opus_imdct.c:53
void ff_celt_imdct_init_aarch64(CeltIMDCTContext *s)
static void celt_imdct_half(CeltIMDCTContext *s, float *dst, const float *src, ptrdiff_t stride, float scale)
Definition: opus_imdct.c:244
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
FFTSample im
Definition: avfft.h:38
common internal and external API header
float re
Definition: fft-test.c:69
int len
#define CELT_MAX_FRAME_SIZE
Definition: opus.h:44
#define CMUL3(cre, cim, are, aim, bre, bim)
Definition: opus_imdct.c:43
static void fft15(CeltIMDCTContext *s, FFTComplex *out, const FFTComplex *in, ptrdiff_t stride)
Definition: opus_imdct.c:183
static void fft5(FFTComplex *out, const FFTComplex *in, ptrdiff_t stride)
Definition: opus_imdct.c:150
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205