FFmpeg
Loading...
Searching...
No Matches
apv_dsp.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <stdint.h>
20#include <string.h>
21
22#include "checkasm.h"
23
24#include "libavutil/common.h"
26#include "libavcodec/apv_dsp.h"
27
28static const int8_t apv_trans_matrix[8][8] = {
29 { 64, 64, 64, 64, 64, 64, 64, 64 },
30 { 89, 75, 50, 18, -18, -50, -75, -89 },
31 { 84, 35, -35, -84, -84, -35, 35, 84 },
32 { 75, -18, -89, -50, 50, 89, 18, -75 },
33 { 64, -64, -64, 64, 64, -64, -64, 64 },
34 { 50, -89, 18, 75, -75, -18, 89, -50 },
35 { 35, -84, 84, -35, -35, 84, -84, 35 },
36 { 18, -50, 75, -89, 89, -75, 50, -18 },
37};
38
39static const uint8_t apv_level_scale[6] = { 40, 45, 51, 57, 64, 71 };
40
41// Make a set of quantised coefficients by sending a random residual
42// through a forward transform and quantisation: this keeps every
43// intermediate in the ranges a real encoder would produce (completely
44// random coefficients overflow the use of 16x16->32 multiplies in the
45// SIMD paths).
46static void generate_block(int16_t *input, const int16_t *qmatrix,
47 int bit_depth, int qp_shift, int dense)
48{
49 // 64-bit intermediates here avoid rounding issues.
50 int64_t residual[8][8], tmp[8][8];
51 int amplitude;
52
53 if (dense) {
54 // Dense blocks for benchmarking are fully random and uniform
55 // over the range.
56 amplitude = (1 << bit_depth) - 1;
57 } else {
58 // Random blocks for testing bias the amplitude, both to get
59 // smaller typical coefficients (including many zeroes) and to
60 // make clipping on the ends of the range more likely.
61 amplitude = (3 << (bit_depth - 2)) >> (rnd() % 8);
62 }
63 for (int y = 0; y < 8; y++) {
64 for (int x = 0; x < 8; x++)
65 residual[y][x] =
66 (int64_t)(rnd() % (2 * amplitude + 1)) - amplitude;
67 }
68
69 // Forward transform rows.
70 for (int y = 0; y < 8; y++) {
71 for (int i = 0; i < 8; i++) {
72 int64_t sum = 0;
73 for (int j = 0; j < 8; j++)
74 sum += apv_trans_matrix[i][j] * residual[y][j];
75 tmp[y][i] = sum;
76 }
77 }
78 // Forward transform columns, renormalise, and apply quantisation
79 // to make the coefficient level.
80 for (int x = 0; x < 8; x++) {
81 for (int i = 0; i < 8; i++) {
82 int64_t coeff, step, level;
83
84 coeff = 0;
85 for (int j = 0; j < 8; j++)
86 coeff += apv_trans_matrix[i][j] * tmp[j][x];
87 coeff = ROUNDED_DIV(coeff,
88 INT64_C(1) << (bit_depth + 3));
89
90 step = qmatrix[i * 8 + x] << qp_shift;
91 level = ROUNDED_DIV(coeff * (INT64_C(1) << (bit_depth - 2)),
92 step);
93
94 input[i * 8 + x] = av_clip_int16(level);
95 }
96 }
97
98 // Levels made as above never reach the clip after dequantisation,
99 // so sometimes replace one coefficient with a huge value. A single
100 // coefficient like this does not overflow the intermediates.
101 if (!dense && rnd() % 8 == 0) {
102 int pos = rnd() % 64;
103 int level = 16384 + (rnd() % 16384);
104 input[pos] = rnd() % 2 ? level : -level;
105 }
106}
107
109{
110 LOCAL_ALIGNED_32( int16_t, input, [64]);
111 LOCAL_ALIGNED_32( int16_t, qmatrix, [64]);
112 LOCAL_ALIGNED_32(uint16_t, new_output, [64]);
113 LOCAL_ALIGNED_32(uint16_t, ref_output, [64]);
114 LOCAL_ALIGNED_32( uint8_t, new_constants, [APV_DSP_CONSTANTS_SIZE]);
115 LOCAL_ALIGNED_32( uint8_t, ref_constants, [APV_DSP_CONSTANTS_SIZE]);
116
117 declare_func(void,
118 void *output,
119 ptrdiff_t pitch,
120 const int16_t *input,
121 const void *constants);
122
123 for (int n = 0; n < 256; n++) {
124 // Fully random QP for each block.
125 int qp = rnd() % (3 + 6 * bit_depth + 1);
126 int level_scale = apv_level_scale[qp % 6];
127 int qp_shift = qp / 6;
128
129 // The reference encoder uses a flat 16-everywhere matrix by
130 // default, while any other matrix must be supplied manually by
131 // the user. Use the default matrix half the time, and a
132 // completely random matrix otherwise.
133 int flat = rnd() % 2;
134 if (flat) {
135 for (int i = 0; i < 64; i++)
136 qmatrix[i] = level_scale * 16;
137 } else {
138 for (int i = 0; i < 64; i++)
139 qmatrix[i] = level_scale * (1 + rnd() % 255);
140 }
141
142 generate_block(input, qmatrix, bit_depth, qp_shift, 0);
143
144 ff_apv_prepare_constants_c(ref_constants, qmatrix, bit_depth, qp_shift);
145 dsp->prepare_constants (new_constants, qmatrix, bit_depth, qp_shift);
146
147 call_ref(ref_output, 16, input, ref_constants);
148 call_new(new_output, 16, input, new_constants);
149
150 if (memcmp(new_output, ref_output, 64 * sizeof(*ref_output))) {
151 fail();
152 break;
153 }
154 }
155
156 {
157 // Benchmark on a dense block with QP = 12 and a flat matrix.
158 // Almost all coefficients will be nonzero.
159 int qp_shift = 2;
160 for (int i = 0; i < 64; i++)
161 qmatrix[i] = apv_level_scale[0] * 16;
162 generate_block(input, qmatrix, bit_depth, qp_shift, 1);
163
164 ff_apv_prepare_constants_c(ref_constants, qmatrix, bit_depth, qp_shift);
165 dsp->prepare_constants (new_constants, qmatrix, bit_depth, qp_shift);
166
167 call_ref(ref_output, 16, input, ref_constants);
168 call_new(new_output, 16, input, new_constants);
169 if (memcmp(new_output, ref_output, 64 * sizeof(*ref_output)))
170 fail();
171
172 bench_new(new_output, 16, input, new_constants);
173 }
174}
175
177{
178 APVDSPContext dsp;
179
180 ff_apv_dsp_init(&dsp);
181
182 for (int bit_depth = 10; bit_depth <= 16; bit_depth += 2) {
184 "decode_transquant_%d", bit_depth))
186 }
187
188 report("decode_transquant");
189}
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition af_astats.c:246
#define APV_DSP_CONSTANTS_SIZE
Size of the buffer holding constants for decode_transquant(), which must be large enough for the layo...
Definition apv_dsp.h:31
static const uint8_t apv_level_scale[6]
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define rnd
Definition checkasm.h:140
common internal and external API header
#define ROUNDED_DIV(a, b)
Definition common.h:58
#define av_clip_int16
Definition common.h:115
long long int64_t
Definition coverity.c:34
#define declare_func
Definition test.h:489
#define fail
Definition test.h:479
#define bench_new
Definition test.h:487
#define check_func
Definition test.h:481
#define call_new
Definition test.h:486
#define call_ref
Definition test.h:485
#define report
Definition test.h:480
static const vec_s16 constants[5]
static const int level_scale[2][6]
Definition intra.c:336
void ff_apv_prepare_constants_c(void *constants, const int16_t *qmatrix, int bit_depth, int qp_shift)
Definition apv_dsp.c:52
static const int8_t apv_trans_matrix[8][8]
Definition apv_dsp.c:32
av_cold void ff_apv_dsp_init(APVDSPContext *dsp)
Definition apv_dsp.c:144
#define LOCAL_ALIGNED_32(t, v,...)
unsigned int pos
Definition spdifenc.c:431
void(* prepare_constants)(void *constants, const int16_t *qmatrix, int bit_depth, int qp_shift)
Fill the constants buffer used by decode_transquant() for all blocks with the same quantisation param...
Definition apv_dsp.h:39
void(* decode_transquant)(void *output, ptrdiff_t pitch, const int16_t *input, const void *constants)
Run transquant for a single block.
Definition apv_dsp.h:49
uint8_t level
Definition svq3.c:208
static uint8_t tmp[40]
Definition aes_ctr.c:52
static void check_decode_transquant(const APVDSPContext *dsp, int bit_depth)
Definition apv_dsp.c:108
static void generate_block(int16_t *input, const int16_t *qmatrix, int bit_depth, int qp_shift, int dense)
Definition apv_dsp.c:46
void checkasm_check_apv_dsp(void)
Definition apv_dsp.c:176
static av_always_inline void flat(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)