aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/apv_dsp.c
blob: 07bb34ec0bf568fefb22d003d27c6ae75c99a72d (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
/*
 * This file is part of FFmpeg.
 *
 * FFmpeg 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.
 *
 * FFmpeg 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 FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdint.h>

#include "config.h"
#include "libavutil/attributes.h"
#include "libavutil/common.h"

#include "apv.h"
#include "apv_dsp.h"


static const int8_t apv_trans_matrix[8][8] = {
    {  64,  64,  64,  64,  64,  64,  64,  64 },
    {  89,  75,  50,  18, -18, -50, -75, -89 },
    {  84,  35, -35, -84, -84, -35,  35,  84 },
    {  75, -18, -89, -50,  50,  89,  18, -75 },
    {  64, -64, -64,  64,  64, -64, -64,  64 },
    {  50, -89,  18,  75, -75, -18,  89, -50 },
    {  35, -84,  84, -35, -35,  84, -84,  35 },
    {  18, -50,  75, -89,  89, -75,  50, -18 },
};

static void apv_decode_transquant_c(void *output,
                                    ptrdiff_t pitch,
                                    const int16_t *input_flat,
                                    const int16_t *qmatrix_flat,
                                    int bit_depth,
                                    int qp_shift)
{
    const int16_t (*input)[8]   = (const int16_t(*)[8])input_flat;
    const int16_t (*qmatrix)[8] = (const int16_t(*)[8])qmatrix_flat;

    int16_t scaled_coeff[8][8];
    int32_t recon_sample[8][8];

    // Dequant.
    {
        // Note that level_scale was already combined into qmatrix
        // before we got here.
        int bd_shift = bit_depth + 3 - 5;

        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                int coeff = (input[y][x] * qmatrix[y][x] * (1 << qp_shift) +
                             (1 << (bd_shift - 1))) >> bd_shift;

                scaled_coeff[y][x] =
                    av_clip(coeff, APV_MIN_TRANS_COEFF,
                                   APV_MAX_TRANS_COEFF);
            }
        }
    }

    // Transform.
    {
        int32_t tmp[8][8];

        // Vertical transform of columns.
        for (int x = 0; x < 8; x++) {
            for (int i = 0; i < 8; i++) {
                int sum = 0;
                for (int j = 0; j < 8; j++)
                    sum += apv_trans_matrix[j][i] * scaled_coeff[j][x];
                tmp[i][x] = sum;
            }
        }

        // Renormalise.
        for (int x = 0; x < 8; x++) {
            for (int y = 0; y < 8; y++)
                tmp[y][x] = (tmp[y][x] + 64) >> 7;
        }

        // Horizontal transform of rows.
        for (int y = 0; y < 8; y++) {
            for (int i = 0; i < 8; i++) {
                int sum = 0;
                for (int j = 0; j < 8; j++)
                    sum += apv_trans_matrix[j][i] * tmp[y][j];
                recon_sample[y][i] = sum;
            }
        }
    }

    // Output.
    if (bit_depth == 8) {
        uint8_t *ptr = output;
        int bd_shift = 20 - bit_depth;

        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                int sample = ((recon_sample[y][x] +
                               (1 << (bd_shift - 1))) >> bd_shift) +
                    (1 << (bit_depth - 1));
                ptr[x] = av_clip_uintp2(sample, bit_depth);
            }
            ptr += pitch;
        }
    } else {
        uint16_t *ptr = output;
        int bd_shift = 20 - bit_depth;
        pitch /= 2; // Pitch was in bytes, 2 bytes per sample.

        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                int sample = ((recon_sample[y][x] +
                               (1 << (bd_shift - 1))) >> bd_shift) +
                    (1 << (bit_depth - 1));
                ptr[x] = av_clip_uintp2(sample, bit_depth);
            }
            ptr += pitch;
        }
    }
}

av_cold void ff_apv_dsp_init(APVDSPContext *dsp)
{
    dsp->decode_transquant = apv_decode_transquant_c;

#if ARCH_X86_64
    ff_apv_dsp_init_x86_64(dsp);
#endif
}