FFmpeg
opusdsp.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 modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include "libavcodec/opusdsp.h"
20 
21 #include "checkasm.h"
22 
23 #define randomize_float(buf, len) \
24  do { \
25  for (int i = 0; i < len; i++) { \
26  float f = (float)rnd() / (UINT_MAX >> 5) - 16.0f; \
27  buf[i] = f; \
28  } \
29  } while (0)
30 
31 #define EPS 0.005
32 #define MAX_SIZE (960)
33 
34 /* period is between 15 and 1022, inclusive */
35 static void test_postfilter(int period)
36 {
37  LOCAL_ALIGNED(16, float, data0, [MAX_SIZE + 1024]);
38  LOCAL_ALIGNED(16, float, data1, [MAX_SIZE + 1024]);
39 
40  /* This filter can explode very easily, so use a tapset from the codec.
41  * In the codec these are usually multiplied by at least 0.09375f,
42  * so its outside the largest filter value, but the filter is still stable
43  * so use it. */
44  float gains[3] = { 0.3066406250f, 0.2170410156f, 0.1296386719f };
45 
46  /* The codec will always call with an offset which is aligned once
47  * (period + 2) is subtracted, but here we have to align it outselves. */
48  int offset = FFALIGN(period + 2, 4);
49 
50  declare_func(void, float *data, int period, float *gains, int len);
51 
52  randomize_float(data0, MAX_SIZE + 1024);
53  memcpy(data1, data0, (MAX_SIZE + 1024)*sizeof(float));
54 
55  call_ref(data0 + offset, period, gains, MAX_SIZE);
56  call_new(data1 + offset, period, gains, MAX_SIZE);
57 
58  if (!float_near_abs_eps_array(data0 + offset, data1 + offset, EPS, MAX_SIZE))
59  fail();
60  bench_new(data1 + offset, period, gains, MAX_SIZE);
61 }
62 
63 static void test_deemphasis(void)
64 {
65  LOCAL_ALIGNED(16, float, src, [FFALIGN(MAX_SIZE, 4)]);
66  LOCAL_ALIGNED(16, float, dst0, [FFALIGN(MAX_SIZE, 4)]);
67  LOCAL_ALIGNED(16, float, dst1, [FFALIGN(MAX_SIZE, 4)]);
68  float coeff0 = (float)rnd() / (UINT_MAX >> 5) - 16.0f, coeff1 = coeff0;
69 
70  declare_func_float(float, float *out, float *in, float coeff, int len);
71 
73 
74  coeff0 = call_ref(dst0, src, coeff0, MAX_SIZE);
75  coeff1 = call_new(dst1, src, coeff1, MAX_SIZE);
76 
77  if (!float_near_abs_eps(coeff0, coeff1, EPS) ||
78  !float_near_abs_eps_array(dst0, dst1, EPS, MAX_SIZE))
79  fail();
80  bench_new(dst1, src, coeff1, MAX_SIZE);
81 }
82 
84 {
85  OpusDSP ctx;
86  ff_opus_dsp_init(&ctx);
87 
88  if (check_func(ctx.postfilter, "postfilter_15"))
89  test_postfilter(15);
90  report("postfilter_15");
91 
92  if (check_func(ctx.postfilter, "postfilter_512"))
93  test_postfilter(512);
94  report("postfilter_512");
95 
96  if (check_func(ctx.postfilter, "postfilter_1022"))
97  test_postfilter(1022);
98  report("postfilter_1022");
99 
100  if (check_func(ctx.deemphasis, "deemphasis"))
101  test_deemphasis();
102  report("deemphasis");
103 }
void checkasm_check_opusdsp(void)
Definition: opusdsp.c:83
int float_near_abs_eps_array(const float *a, const float *b, float eps, unsigned len)
Definition: checkasm.c:323
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void test_postfilter(int period)
Definition: opusdsp.c:35
#define MAX_SIZE
Definition: opusdsp.c:32
#define src
Definition: vp8dsp.c:254
#define report
Definition: checkasm.h:125
static void test_deemphasis(void)
Definition: opusdsp.c:63
int float_near_abs_eps(float a, float b, float eps)
Definition: checkasm.c:312
float(* deemphasis)(float *out, float *in, float coeff, int len)
Definition: opusdsp.h:28
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
#define f(width, name)
Definition: cbs_vp9.c:255
#define FFALIGN(x, a)
Definition: macros.h:48
#define EPS
Definition: opusdsp.c:31
#define randomize_float(buf, len)
Definition: opusdsp.c:23
#define declare_func(ret,...)
Definition: checkasm.h:117
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without period
#define fail()
Definition: checkasm.h:122
AVFormatContext * ctx
Definition: movenc.c:48
#define call_ref(...)
Definition: checkasm.h:128
#define declare_func_float(ret,...)
Definition: checkasm.h:118
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
#define check_func(func,...)
Definition: checkasm.h:113
#define LOCAL_ALIGNED(a, t, v,...)
Definition: internal.h:114
#define rnd()
Definition: checkasm.h:106
av_cold void ff_opus_dsp_init(OpusDSP *ctx)
Definition: opusdsp.c:52
int len
#define bench_new(...)
Definition: checkasm.h:255
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
FILE * out
Definition: movenc.c:54
#define call_new(...)
Definition: checkasm.h:195
void(* postfilter)(float *data, int period, float *gains, int len)
Definition: opusdsp.h:27