aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/atrac3plus_pqf/atrac3plus_pqf.c
blob: 07424690484c1b0852b738c65e3116d5b394d47c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * This file is part of AtracDEnc.
 *
 * AtracDEnc is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * AtracDEnc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with AtracDEnc; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * Musepack pqf implementation was used as reference.
 * see svn.musepack.net
 */

#include <stdlib.h>

#include <string.h>
#include <stdio.h>
#include <math.h>

#include "atrac3plus_pqf.h"
#include "atrac3plus_pqf_data.h"

#include "lib/mdct/dct.h"

/*
 * Number of subbands to split input signal
 */
#define SUBBANDS_NUM 16
/*
 * Number of samples in each subband
 */
#define SUBBAND_SIZE 128
/*
 * Size of filter prototype
 */
#define PROTO_SZ 384

#define FRAME_SZ ((SUBBANDS_NUM * SUBBAND_SIZE))
#define OVERLAP_SZ ((PROTO_SZ - SUBBANDS_NUM))


static float fir[PROTO_SZ];

struct at3plus_pqf_a_ctx {
    float buf[FRAME_SZ + OVERLAP_SZ];
    atde_dct_ctx_t dct_ctx;
};

static void init(void)
{
    static int inited = 0;

    if (inited)
        return;

    inited = 1;

    for (int i = 0; i < 16; i++) {
        for (int j = 0; j < ATRAC3P_PQF_FIR_LEN; j++) {
	    if (i >= 8) {
	        fir[j + 96  + (i - 8) * 12] = ff_ipqf_coeffs1[j][i];
	        fir[j + 288 + (i - 8) * 12] = ff_ipqf_coeffs2[j][i];
	    } else {
	        fir[j + 192 + i * 12] = ff_ipqf_coeffs2[j][i];
	        fir[j + 0   + i * 12] = ff_ipqf_coeffs1[j][i];
	    }
        }
    }
}

static void vectoring(const float* const x, float* y)
{
    for (int i = 0; i < 32; i++) {
        y[i] = 0;
        for (int j = 0; j < ATRAC3P_PQF_FIR_LEN; j++) {
            y[i] += fir[i * 12 + j] * x[j * 32 + i];
        }
    }
}

static void matrixing(atde_dct_ctx_t ctx, const float* y, float* samples )
{
    float yy[SUBBANDS_NUM];
    float res[SUBBANDS_NUM];

    for (int i = 0; i < 8; i++) {
        yy[i] = y[i + 8] + y[7 - i];
        yy[i + 8] = y[i + 16] + y[31 - i];
    }

    atde_do_dct4_16(ctx, yy, res);

    for (int i = 0; i < SUBBANDS_NUM; i++) {
        samples[i * SUBBAND_SIZE] = res[SUBBANDS_NUM - 1 - i];
    }
}

at3plus_pqf_a_ctx_t at3plus_pqf_create_a_ctx()
{
    at3plus_pqf_a_ctx_t ctx = (at3plus_pqf_a_ctx_t)malloc(sizeof(struct at3plus_pqf_a_ctx));

    for (int i = 0; i < FRAME_SZ + OVERLAP_SZ; i++) {
        ctx->buf[i] = 0.0;
    }

    ctx->dct_ctx = atde_create_dct4_16(128 * 512.0);

    init();

    return ctx;
}

void at3plus_pqf_free_a_ctx(at3plus_pqf_a_ctx_t ctx)
{
    atde_free_dct_ctx(ctx->dct_ctx);

    free(ctx);
}

void at3plus_pqf_do_analyse(at3plus_pqf_a_ctx_t ctx, const float* in, float* out)
{
    float y[SUBBANDS_NUM * 2];

    float* const buf = ctx->buf;

    const float* x = buf;

    memcpy(buf + OVERLAP_SZ, in, sizeof(in[0]) * FRAME_SZ);

    for (int i = 0; i < SUBBAND_SIZE; i++) {
        vectoring(x, y);
        matrixing (ctx->dct_ctx, y, &out[i]);
        x += SUBBANDS_NUM;
    }

    memcpy(buf, buf + FRAME_SZ, sizeof(buf[0]) * OVERLAP_SZ);
}