GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mpegaudiodsp_template.c Lines: 137 137 100.0 %
Date: 2021-01-21 13:05:02 Branches: 32 32 100.0 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2001, 2002 Fabrice Bellard
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include <stdint.h>
22
23
#include "libavutil/attributes.h"
24
#include "libavutil/mem.h"
25
#include "libavutil/mem_internal.h"
26
#include "libavutil/thread.h"
27
28
#include "dct32.h"
29
#include "mathops.h"
30
#include "mpegaudiodsp.h"
31
#include "mpegaudio.h"
32
33
#if USE_FLOATS
34
#define RENAME(n) n##_float
35
36
3021056
static inline float round_sample(float *sum)
37
{
38
3021056
    float sum1=*sum;
39
3021056
    *sum = 0;
40
3021056
    return sum1;
41
}
42
43
#define MACS(rt, ra, rb) rt+=(ra)*(rb)
44
#define MULS(ra, rb) ((ra)*(rb))
45
#define MULH3(x, y, s) ((s)*(y)*(x))
46
#define MLSS(rt, ra, rb) rt-=(ra)*(rb)
47
#define MULLx(x, y, s) ((y)*(x))
48
#define FIXHR(x)        ((float)(x))
49
#define FIXR(x)        ((float)(x))
50
#define SHR(a,b)       ((a)*(1.0f/(1<<(b))))
51
52
#else
53
54
#define RENAME(n) n##_fixed
55
#define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
56
57
13913856
static inline int round_sample(int64_t *sum)
58
{
59
    int sum1;
60
13913856
    sum1 = (int)((*sum) >> OUT_SHIFT);
61
13913856
    *sum &= (1<<OUT_SHIFT)-1;
62
13913856
    return av_clip_int16(sum1);
63
}
64
65
#   define MULS(ra, rb) MUL64(ra, rb)
66
#   define MACS(rt, ra, rb) MAC64(rt, ra, rb)
67
#   define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
68
#   define MULH3(x, y, s) MULH((s)*(x), y)
69
#   define MULLx(x, y, s) MULL((int)(x),(y),s)
70
#   define SHR(a,b)       (((int)(a))>>(b))
71
#   define FIXR(a)        ((int)((a) * FRAC_ONE + 0.5))
72
#   define FIXHR(a)       ((int)((a) * (1LL<<32) + 0.5))
73
#endif
74
75
/** Window for MDCT. Actually only the elements in [0,17] and
76
    [MDCT_BUF_SIZE/2, MDCT_BUF_SIZE/2 + 17] are actually used. The rest
77
    is just to preserve alignment for SIMD implementations.
78
*/
79
DECLARE_ALIGNED(16, INTFLOAT, RENAME(ff_mdct_win))[8][MDCT_BUF_SIZE];
80
81
DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
82
83
#define SUM8(op, sum, w, p)               \
84
{                                         \
85
    op(sum, (w)[0 * 64], (p)[0 * 64]);    \
86
    op(sum, (w)[1 * 64], (p)[1 * 64]);    \
87
    op(sum, (w)[2 * 64], (p)[2 * 64]);    \
88
    op(sum, (w)[3 * 64], (p)[3 * 64]);    \
89
    op(sum, (w)[4 * 64], (p)[4 * 64]);    \
90
    op(sum, (w)[5 * 64], (p)[5 * 64]);    \
91
    op(sum, (w)[6 * 64], (p)[6 * 64]);    \
92
    op(sum, (w)[7 * 64], (p)[7 * 64]);    \
93
}
94
95
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
96
{                                               \
97
    INTFLOAT tmp;\
98
    tmp = p[0 * 64];\
99
    op1(sum1, (w1)[0 * 64], tmp);\
100
    op2(sum2, (w2)[0 * 64], tmp);\
101
    tmp = p[1 * 64];\
102
    op1(sum1, (w1)[1 * 64], tmp);\
103
    op2(sum2, (w2)[1 * 64], tmp);\
104
    tmp = p[2 * 64];\
105
    op1(sum1, (w1)[2 * 64], tmp);\
106
    op2(sum2, (w2)[2 * 64], tmp);\
107
    tmp = p[3 * 64];\
108
    op1(sum1, (w1)[3 * 64], tmp);\
109
    op2(sum2, (w2)[3 * 64], tmp);\
110
    tmp = p[4 * 64];\
111
    op1(sum1, (w1)[4 * 64], tmp);\
112
    op2(sum2, (w2)[4 * 64], tmp);\
113
    tmp = p[5 * 64];\
114
    op1(sum1, (w1)[5 * 64], tmp);\
115
    op2(sum2, (w2)[5 * 64], tmp);\
116
    tmp = p[6 * 64];\
117
    op1(sum1, (w1)[6 * 64], tmp);\
118
    op2(sum2, (w2)[6 * 64], tmp);\
119
    tmp = p[7 * 64];\
120
    op1(sum1, (w1)[7 * 64], tmp);\
121
    op2(sum2, (w2)[7 * 64], tmp);\
122
}
123
124
529216
void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
125
                                  int *dither_state, OUT_INT *samples,
126
                                  ptrdiff_t incr)
127
{
128
    register const MPA_INT *w, *w2, *p;
129
    int j;
130
    OUT_INT *samples2;
131
#if USE_FLOATS
132
    float sum, sum2;
133
#else
134
    int64_t sum, sum2;
135
#endif
136
137
    /* copy to avoid wrap */
138
529216
    memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
139
140
529216
    samples2 = samples + 31 * incr;
141
529216
    w = window;
142
529216
    w2 = window + 31;
143
144
529216
    sum = *dither_state;
145
529216
    p = synth_buf + 16;
146
529216
    SUM8(MACS, sum, w, p);
147
529216
    p = synth_buf + 48;
148
529216
    SUM8(MLSS, sum, w + 32, p);
149
529216
    *samples = round_sample(&sum);
150
529216
    samples += incr;
151
529216
    w++;
152
153
    /* we calculate two samples at the same time to avoid one memory
154
       access per two sample */
155
8467456
    for(j=1;j<16;j++) {
156
7938240
        sum2 = 0;
157
7938240
        p = synth_buf + 16 + j;
158
7938240
        SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
159
7938240
        p = synth_buf + 48 - j;
160
7938240
        SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
161
162
7938240
        *samples = round_sample(&sum);
163
7938240
        samples += incr;
164
7938240
        sum += sum2;
165
7938240
        *samples2 = round_sample(&sum);
166
7938240
        samples2 -= incr;
167
7938240
        w++;
168
7938240
        w2--;
169
    }
170
171
529216
    p = synth_buf + 32;
172
529216
    SUM8(MLSS, sum, w + 32, p);
173
529216
    *samples = round_sample(&sum);
174
529216
    *dither_state= sum;
175
529216
}
176
177
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
178
   32 samples. */
179
531304
void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
180
                                 int *synth_buf_offset,
181
                                 MPA_INT *window, int *dither_state,
182
                                 OUT_INT *samples, ptrdiff_t incr,
183
                                 MPA_INT *sb_samples)
184
{
185
    MPA_INT *synth_buf;
186
    int offset;
187
188
531304
    offset = *synth_buf_offset;
189
531304
    synth_buf = synth_buf_ptr + offset;
190
191
531304
    s->RENAME(dct32)(synth_buf, sb_samples);
192
531304
    s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
193
194
531304
    offset = (offset - 32) & 511;
195
531304
    *synth_buf_offset = offset;
196
531304
}
197
198
95
static av_cold void mpa_synth_init(MPA_INT *window)
199
{
200
    int i, j;
201
202
    /* max = 18760, max sum over all 16 coefs : 44736 */
203
24510
    for(i=0;i<257;i++) {
204
        INTFLOAT v;
205
24415
        v = ff_mpa_enwindow[i];
206
#if USE_FLOATS
207
12079
        v *= 1.0 / (1LL<<(16 + FRAC_BITS));
208
#endif
209
24415
        window[i] = v;
210
24415
        if ((i & 63) != 0)
211
23940
            v = -v;
212
24415
        if (i != 0)
213
24320
            window[512 - i] = v;
214
    }
215
216
217
    // Needed for avoiding shuffles in ASM implementations
218
855
    for(i=0; i < 8; i++)
219
12920
        for(j=0; j < 16; j++)
220
12160
            window[512+16*i+j] = window[64*i+32-j];
221
222
855
    for(i=0; i < 8; i++)
223
12920
        for(j=0; j < 16; j++)
224
12160
            window[512+128+16*i+j] = window[64*i+48-j];
225
95
}
226
227
95
static av_cold void mpa_synth_window_init(void)
228
{
229
95
    mpa_synth_init(RENAME(ff_mpa_synth_window));
230
95
}
231
232
95
av_cold void RENAME(ff_mpa_synth_init)(void)
233
{
234
    static AVOnce init_static_once = AV_ONCE_INIT;
235
95
    ff_thread_once(&init_static_once, mpa_synth_window_init);
236
95
}
237
238
/* cos(pi*i/18) */
239
#define C1 FIXHR(0.98480775301220805936/2)
240
#define C2 FIXHR(0.93969262078590838405/2)
241
#define C3 FIXHR(0.86602540378443864676/2)
242
#define C4 FIXHR(0.76604444311897803520/2)
243
#define C5 FIXHR(0.64278760968653932632/2)
244
#define C6 FIXHR(0.5/2)
245
#define C7 FIXHR(0.34202014332566873304/2)
246
#define C8 FIXHR(0.17364817766693034885/2)
247
248
/* 0.5 / cos(pi*(2*i+1)/36) */
249
static const INTFLOAT icos36[9] = {
250
    FIXR(0.50190991877167369479),
251
    FIXR(0.51763809020504152469), //0
252
    FIXR(0.55168895948124587824),
253
    FIXR(0.61038729438072803416),
254
    FIXR(0.70710678118654752439), //1
255
    FIXR(0.87172339781054900991),
256
    FIXR(1.18310079157624925896),
257
    FIXR(1.93185165257813657349), //2
258
    FIXR(5.73685662283492756461),
259
};
260
261
/* 0.5 / cos(pi*(2*i+1)/36) */
262
static const INTFLOAT icos36h[9] = {
263
    FIXHR(0.50190991877167369479/2),
264
    FIXHR(0.51763809020504152469/2), //0
265
    FIXHR(0.55168895948124587824/2),
266
    FIXHR(0.61038729438072803416/2),
267
    FIXHR(0.70710678118654752439/2), //1
268
    FIXHR(0.87172339781054900991/2),
269
    FIXHR(1.18310079157624925896/4),
270
    FIXHR(1.93185165257813657349/4), //2
271
//    FIXHR(5.73685662283492756461),
272
};
273
274
/* using Lee like decomposition followed by hand coded 9 points DCT */
275
155407
static void imdct36(INTFLOAT *out, INTFLOAT *buf, SUINTFLOAT *in, INTFLOAT *win)
276
{
277
    int i, j;
278
    SUINTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
279
    SUINTFLOAT tmp[18], *tmp1, *in1;
280
281
2797326
    for (i = 17; i >= 1; i--)
282
2641919
        in[i] += in[i-1];
283
1398663
    for (i = 17; i >= 3; i -= 2)
284
1243256
        in[i] += in[i-2];
285
286
466221
    for (j = 0; j < 2; j++) {
287
310814
        tmp1 = tmp + j;
288
310814
        in1 = in + j;
289
290
310814
        t2 = in1[2*4] + in1[2*8] - in1[2*2];
291
292
310814
        t3 = in1[2*0] + SHR(in1[2*6],1);
293
310814
        t1 = in1[2*0] - in1[2*6];
294
310814
        tmp1[ 6] = t1 - SHR(t2,1);
295
310814
        tmp1[16] = t1 + t2;
296
297
310814
        t0 = MULH3(in1[2*2] + in1[2*4] ,    C2, 2);
298
310814
        t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
299
310814
        t2 = MULH3(in1[2*2] + in1[2*8] ,   -C4, 2);
300
301
310814
        tmp1[10] = t3 - t0 - t2;
302
310814
        tmp1[ 2] = t3 + t0 + t1;
303
310814
        tmp1[14] = t3 + t2 - t1;
304
305
310814
        tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
306
310814
        t2 = MULH3(in1[2*1] + in1[2*5],    C1, 2);
307
310814
        t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
308
310814
        t0 = MULH3(in1[2*3], C3, 2);
309
310
310814
        t1 = MULH3(in1[2*1] + in1[2*7],   -C5, 2);
311
312
310814
        tmp1[ 0] = t2 + t3 + t0;
313
310814
        tmp1[12] = t2 + t1 - t0;
314
310814
        tmp1[ 8] = t3 - t1 - t0;
315
    }
316
317
155407
    i = 0;
318
777035
    for (j = 0; j < 4; j++) {
319
621628
        t0 = tmp[i];
320
621628
        t1 = tmp[i + 2];
321
621628
        s0 = t1 + t0;
322
621628
        s2 = t1 - t0;
323
324
621628
        t2 = tmp[i + 1];
325
621628
        t3 = tmp[i + 3];
326
621628
        s1 = MULH3(t3 + t2, icos36h[    j], 2);
327
621628
        s3 = MULLx(t3 - t2, icos36 [8 - j], FRAC_BITS);
328
329
621628
        t0 = s0 + s1;
330
621628
        t1 = s0 - s1;
331
621628
        out[(9 + j) * SBLIMIT] = MULH3(t1, win[     9 + j], 1) + buf[4*(9 + j)];
332
621628
        out[(8 - j) * SBLIMIT] = MULH3(t1, win[     8 - j], 1) + buf[4*(8 - j)];
333
621628
        buf[4 * ( 9 + j     )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + j], 1);
334
621628
        buf[4 * ( 8 - j     )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - j], 1);
335
336
621628
        t0 = s2 + s3;
337
621628
        t1 = s2 - s3;
338
621628
        out[(9 + 8 - j) * SBLIMIT] = MULH3(t1, win[     9 + 8 - j], 1) + buf[4*(9 + 8 - j)];
339
621628
        out[         j  * SBLIMIT] = MULH3(t1, win[             j], 1) + buf[4*(        j)];
340
621628
        buf[4 * ( 9 + 8 - j     )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 8 - j], 1);
341
621628
        buf[4 * (         j     )] = MULH3(t0, win[MDCT_BUF_SIZE/2         + j], 1);
342
621628
        i += 4;
343
    }
344
345
155407
    s0 = tmp[16];
346
155407
    s1 = MULH3(tmp[17], icos36h[4], 2);
347
155407
    t0 = s0 + s1;
348
155407
    t1 = s0 - s1;
349
155407
    out[(9 + 4) * SBLIMIT] = MULH3(t1, win[     9 + 4], 1) + buf[4*(9 + 4)];
350
155407
    out[(8 - 4) * SBLIMIT] = MULH3(t1, win[     8 - 4], 1) + buf[4*(8 - 4)];
351
155407
    buf[4 * ( 9 + 4     )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 4], 1);
352
155407
    buf[4 * ( 8 - 4     )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - 4], 1);
353
155407
}
354
355
8014
void RENAME(ff_imdct36_blocks)(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in,
356
                               int count, int switch_point, int block_type)
357
{
358
    int j;
359
163421
    for (j=0 ; j < count; j++) {
360
        /* apply window & overlap with previous buffer */
361
362
        /* select window */
363

155407
        int win_idx = (switch_point && j < 2) ? 0 : block_type;
364
155407
        INTFLOAT *win = RENAME(ff_mdct_win)[win_idx + (4 & -(j & 1))];
365
366
155407
        imdct36(out, buf, in, win);
367
368
155407
        in  += 18;
369
155407
        buf += ((j&3) != 3 ? 1 : (72-3));
370
155407
        out++;
371
    }
372
8014
}
373