FFmpeg
flacdsp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 James Almer
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include <string.h>
22 #include "checkasm.h"
23 #include "libavcodec/flacdsp.h"
24 #include "libavcodec/mathops.h"
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mem_internal.h"
29 
30 #define BUF_SIZE 256
31 #define MAX_CHANNELS 8
32 
33 #define randomize_buffers() \
34  do { \
35  int i, j; \
36  for (i = 0; i < BUF_SIZE; i += 4) { \
37  for (j = 0; j < channels; j++) { \
38  uint32_t r = rnd() & (1 << (bits - 2)) - 1; \
39  AV_WN32A(ref_src[j] + i, r); \
40  AV_WN32A(new_src[j] + i, r); \
41  } \
42  } \
43  } while (0)
44 
45 static void check_decorrelate(uint8_t **ref_dst, uint8_t **ref_src, uint8_t **new_dst, uint8_t **new_src,
46  int channels, int bits) {
47  declare_func(void, uint8_t **out, int32_t **in, int channels, int len, int shift);
48 
50  call_ref(ref_dst, (int32_t **)ref_src, channels, BUF_SIZE / sizeof(int32_t), 8);
51  call_new(new_dst, (int32_t **)new_src, channels, BUF_SIZE / sizeof(int32_t), 8);
52  if (memcmp(*ref_dst, *new_dst, bits == 16 ? BUF_SIZE * (channels/2) : BUF_SIZE * channels) ||
53  memcmp(*ref_src, *new_src, BUF_SIZE * channels))
54  fail();
55  bench_new(new_dst, (int32_t **)new_src, channels, BUF_SIZE / sizeof(int32_t), 8);
56 }
57 
58 static void check_lpc(int pred_order, int bps)
59 {
60  int qlevel = rnd() % 16;
61  int coeff_prec = (rnd() % 15) + 1;
62  LOCAL_ALIGNED_16(int32_t, coeffs, [32]);
66 
67  declare_func(void, int32_t *, const int[32], int, int, int);
68 
69  if (bps <= 16)
70  coeff_prec = av_clip(coeff_prec, 0, 32 - bps - av_log2(pred_order));
71 
72  for (int i = 0; i < 32; i++)
73  coeffs[i] = sign_extend(rnd(), coeff_prec);
74  for (int i = 0; i < BUF_SIZE; i++)
75  dst[i] = sign_extend(rnd(), bps);
76 
77  memcpy(dst0, dst, BUF_SIZE * sizeof (int32_t));
78  memcpy(dst1, dst, BUF_SIZE * sizeof (int32_t));
79  call_ref(dst0, coeffs, pred_order, qlevel, BUF_SIZE);
80  call_new(dst1, coeffs, pred_order, qlevel, BUF_SIZE);
81  if (memcmp(dst0, dst1, BUF_SIZE * sizeof (int32_t)) != 0)
82  fail();
83  bench_new(dst, coeffs, pred_order, qlevel, BUF_SIZE);
84 }
85 
86 static void check_lpc33(int pred_order)
87 {
88  int qlevel = rnd() % 16;
89  int coeff_prec = (rnd() % 15) + 1;
90  LOCAL_ALIGNED_16(int64_t, dst, [BUF_SIZE]);
91  LOCAL_ALIGNED_16(int64_t, dst0, [BUF_SIZE]);
92  LOCAL_ALIGNED_16(int64_t, dst1, [BUF_SIZE]);
93  LOCAL_ALIGNED_16(int32_t, residuals, [BUF_SIZE]);
94  LOCAL_ALIGNED_16(int32_t, coeffs, [32]);
95 
96  declare_func(void, int64_t *, const int32_t *, const int[32], int, int, int);
97 
98  for (int i = 0; i < 32; i++)
99  coeffs[i] = sign_extend(rnd(), coeff_prec);
100 
101  for (int i = 0; i < BUF_SIZE; i++) {
102  residuals[i] = sign_extend(rnd(), pred_order);
103  dst[i] = sign_extend64(((int64_t)rnd() << 1) | (rnd() & 1), 33);
104  }
105 
106  memcpy(dst0, dst, BUF_SIZE * sizeof (int64_t));
107  memcpy(dst1, dst, BUF_SIZE * sizeof (int64_t));
108  call_ref(dst0, residuals, coeffs, pred_order, qlevel, BUF_SIZE);
109  call_new(dst1, residuals, coeffs, pred_order, qlevel, BUF_SIZE);
110  if (memcmp(dst0, dst1, BUF_SIZE * sizeof (int64_t)) != 0)
111  fail();
112  bench_new(dst, residuals, coeffs, pred_order, qlevel, BUF_SIZE);
113 }
114 
115 static void check_wasted32(void)
116 {
117  int wasted = rnd() % 32;
121 
122  declare_func(void, int32_t *, int, int);
123 
124  for (int i = 0; i < BUF_SIZE; i++)
125  dst[i] = rnd();
126 
127  memcpy(dst0, dst, BUF_SIZE * sizeof (int32_t));
128  memcpy(dst1, dst, BUF_SIZE * sizeof (int32_t));
129  call_ref(dst0, wasted, BUF_SIZE);
130  call_new(dst1, wasted, BUF_SIZE);
131  if (memcmp(dst0, dst1, BUF_SIZE * sizeof (int32_t)) != 0)
132  fail();
133  bench_new(dst, wasted, BUF_SIZE);
134 }
135 
136 static void check_wasted33(void)
137 {
138  int wasted = rnd() % 33;
139  LOCAL_ALIGNED_16(int64_t, dst0, [BUF_SIZE]);
140  LOCAL_ALIGNED_16(int64_t, dst1, [BUF_SIZE]);
141  LOCAL_ALIGNED_16(int32_t, residuals, [BUF_SIZE]);
142 
143  declare_func(void, int64_t *, const int32_t *, int, int);
144 
145  for (int i = 0; i < BUF_SIZE; i++)
146  residuals[i] = rnd();
147 
148  call_ref(dst0, residuals, wasted, BUF_SIZE);
149  call_new(dst1, residuals, wasted, BUF_SIZE);
150  if (memcmp(dst0, dst1, BUF_SIZE * sizeof (int64_t)) != 0)
151  fail();
152  bench_new(dst0, residuals, wasted, BUF_SIZE);
153 }
154 
156 {
157  LOCAL_ALIGNED_16(uint8_t, ref_dst, [BUF_SIZE*MAX_CHANNELS]);
158  LOCAL_ALIGNED_16(uint8_t, ref_buf, [BUF_SIZE*MAX_CHANNELS]);
159  LOCAL_ALIGNED_16(uint8_t, new_dst, [BUF_SIZE*MAX_CHANNELS]);
160  LOCAL_ALIGNED_16(uint8_t, new_buf, [BUF_SIZE*MAX_CHANNELS]);
161  uint8_t *ref_src[] = { &ref_buf[BUF_SIZE*0], &ref_buf[BUF_SIZE*1], &ref_buf[BUF_SIZE*2], &ref_buf[BUF_SIZE*3],
162  &ref_buf[BUF_SIZE*4], &ref_buf[BUF_SIZE*5], &ref_buf[BUF_SIZE*6], &ref_buf[BUF_SIZE*7] };
163  uint8_t *new_src[] = { &new_buf[BUF_SIZE*0], &new_buf[BUF_SIZE*1], &new_buf[BUF_SIZE*2], &new_buf[BUF_SIZE*3],
164  &new_buf[BUF_SIZE*4], &new_buf[BUF_SIZE*5], &new_buf[BUF_SIZE*6], &new_buf[BUF_SIZE*7] };
165  static const char * const names[3] = { "ls", "rs", "ms" };
166  static const struct {
167  enum AVSampleFormat fmt;
168  int bits;
169  } fmts[] = {
170  { AV_SAMPLE_FMT_S16, 16 },
171  { AV_SAMPLE_FMT_S32, 32 },
172  };
173  static const signed char pred_orders[] = { 13, 16, 29, 32 };
175  int i, j;
176 
177  for (i = 0; i < 2; i++) {
178  ff_flacdsp_init(&h, fmts[i].fmt, 2);
179  for (j = 0; j < 3; j++)
180  if (check_func(h.decorrelate[j + 1], "flac_decorrelate_%s_%d", names[j], fmts[i].bits))
181  check_decorrelate(&ref_dst, ref_src, &new_dst, new_src, 2, fmts[i].bits);
182  for (j = 2; j <= MAX_CHANNELS; j += 2) {
183  ff_flacdsp_init(&h, fmts[i].fmt, j);
184  if (check_func(h.decorrelate[0], "flac_decorrelate_indep%d_%d", j, fmts[i].bits))
185  check_decorrelate(&ref_dst, ref_src, &new_dst, new_src, j, fmts[i].bits);
186  }
187  }
188 
189  report("decorrelate");
190 
191  for (i = 0; i < FF_ARRAY_ELEMS(pred_orders); i++)
192  if (check_func(h.lpc16, "flac_lpc_16_%d", pred_orders[i]))
193  check_lpc(pred_orders[i], 16);
194  for (i = 0; i < FF_ARRAY_ELEMS(pred_orders); i++)
195  if (check_func(h.lpc32, "flac_lpc_32_%d", pred_orders[i]))
196  check_lpc(pred_orders[i], 32);
197  for (i = 0; i < FF_ARRAY_ELEMS(pred_orders); i++)
198  if (check_func(h.lpc33, "flac_lpc_33_%d", pred_orders[i]))
199  check_lpc33(pred_orders[i]);
200 
201  report("lpc");
202 
203  if (check_func(h.wasted32, "flac_wasted_32"))
204  check_wasted32();
205  if (check_func(h.wasted33, "flac_wasted_33"))
206  check_wasted33();
207 
208  report("wasted");
209 }
av_clip
#define av_clip
Definition: common.h:99
mem_internal.h
out
FILE * out
Definition: movenc.c:55
check_func
#define check_func(func,...)
Definition: checkasm.h:173
flacdsp.h
call_ref
#define call_ref(...)
Definition: checkasm.h:188
fail
#define fail()
Definition: checkasm.h:182
checkasm.h
rnd
#define rnd()
Definition: checkasm.h:166
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
check_wasted32
static void check_wasted32(void)
Definition: flacdsp.c:115
intreadwrite.h
bits
uint8_t bits
Definition: vp3data.h:128
LOCAL_ALIGNED_16
#define LOCAL_ALIGNED_16(t, v,...)
Definition: mem_internal.h:150
sign_extend64
static av_const int64_t sign_extend64(int64_t val, unsigned bits)
Definition: mathops.h:142
channels
channels
Definition: aptx.h:31
checkasm_check_flacdsp
void checkasm_check_flacdsp(void)
Definition: flacdsp.c:155
call_new
#define call_new(...)
Definition: checkasm.h:291
mathops.h
MAX_CHANNELS
#define MAX_CHANNELS
Definition: flacdsp.c:31
shift
static int shift(int a, int b)
Definition: bonk.c:261
ff_flacdsp_init
av_cold void ff_flacdsp_init(FLACDSPContext *c, enum AVSampleFormat fmt, int channels)
Definition: flacdsp.c:114
bps
unsigned bps
Definition: movenc.c:1852
check_lpc
static void check_lpc(int pred_order, int bps)
Definition: flacdsp.c:58
report
#define report
Definition: checkasm.h:185
bench_new
#define bench_new(...)
Definition: checkasm.h:362
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
check_decorrelate
static void check_decorrelate(uint8_t **ref_dst, uint8_t **ref_src, uint8_t **new_dst, uint8_t **new_src, int channels, int bits)
Definition: flacdsp.c:45
randomize_buffers
#define randomize_buffers()
Definition: flacdsp.c:33
internal.h
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
len
int len
Definition: vorbis_enc_data.h:426
check_wasted33
static void check_wasted33(void)
Definition: flacdsp.c:136
check_lpc33
static void check_lpc33(int pred_order)
Definition: flacdsp.c:86
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:133
declare_func
#define declare_func(ret,...)
Definition: checkasm.h:177
int32_t
int32_t
Definition: audioconvert.c:56
h
h
Definition: vp9dsp_template.c:2038
BUF_SIZE
#define BUF_SIZE
Definition: flacdsp.c:30
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
FLACDSPContext
Definition: flacdsp.h:26
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26