Skip to content

Commit 72632ef

Browse files
committed
add pcm16 decibel
1 parent 9fc26c2 commit 72632ef

19 files changed

+630
-52
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
out
12
*.out
23
/Halide-Runtime*
34
.git

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ifeq ($(shell [ -d detector/Halide-Runtime ] && echo "1"),1)
2929
else
3030
curl -O -sSL https://github.com/halide/Halide/releases/download/v14.0.0/Halide-14.0.0-x86-64-linux-6b9ed2afd1d6d0badf04986602c943e287d44e46.tar.gz
3131
tar xzf Halide-14.0.0-x86-64-linux-6b9ed2afd1d6d0badf04986602c943e287d44e46.tar.gz
32-
mv Halide-14.0.0-x86-64-linux detector/Halide-Runtime
32+
mv Halide-14.0.0-x86-64-linux ./Halide-Runtime
3333
rm Halide-14.0.0-x86-64-linux-6b9ed2afd1d6d0badf04986602c943e287d44e46.tar.gz
3434
endif
3535

README.md

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,57 +19,58 @@ darwin/amd64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
1919

2020
```
2121
src 320x240
22-
BenchmarkJIT/cloneimg : 0.00767ms
23-
BenchmarkJIT/convert_from_argb : 0.02328ms
24-
BenchmarkJIT/convert_from_abgr : 0.03573ms
25-
BenchmarkJIT/convert_from_bgra : 0.02430ms
26-
BenchmarkJIT/convert_from_rabg : 0.03159ms
27-
BenchmarkJIT/convert_from_yuv_420 : 0.02964ms
28-
BenchmarkJIT/convert_from_yuv_444 : 0.02652ms
29-
BenchmarkJIT/convert_to_yuv_420 : 0.05654ms
30-
BenchmarkJIT/convert_to_yuv_444 : 0.07356ms
31-
BenchmarkJIT/rotate0 : 0.00828ms
32-
BenchmarkJIT/rotate90 : 0.02600ms
33-
BenchmarkJIT/rotate180 : 0.00792ms
34-
BenchmarkJIT/rotate270 : 0.02560ms
35-
BenchmarkJIT/crop : 0.06128ms
36-
BenchmarkJIT/scale : 0.13941ms
37-
BenchmarkJIT/scale_box : 0.20701ms
38-
BenchmarkJIT/scale_linear : 0.20637ms
39-
BenchmarkJIT/scale_gaussian : 0.31937ms
40-
BenchmarkJIT/blend_normal : 0.09480ms
41-
BenchmarkJIT/blend_sub : 0.08381ms
42-
BenchmarkJIT/blend_add : 0.08439ms
43-
BenchmarkJIT/blend_diff : 0.08445ms
44-
BenchmarkJIT/grayscale : 0.03752ms
45-
BenchmarkJIT/invert : 0.03684ms
46-
BenchmarkJIT/brightness : 0.04142ms
47-
BenchmarkJIT/gammacorrection : 0.07600ms
48-
BenchmarkJIT/contrast : 0.01532ms
49-
BenchmarkJIT/boxblur : 0.10674ms
50-
BenchmarkJIT/gaussianblur : 0.31972ms
51-
BenchmarkJIT/blockmozaic : 0.27346ms
52-
BenchmarkJIT/erosion : 0.11407ms
53-
BenchmarkJIT/dilation : 0.11997ms
54-
BenchmarkJIT/morphology_open : 0.14157ms
55-
BenchmarkJIT/morphology_close : 0.10427ms
56-
BenchmarkJIT/morphology_gradient : 0.07612ms
57-
BenchmarkJIT/emboss : 0.06083ms
58-
BenchmarkJIT/laplacian : 0.04251ms
59-
BenchmarkJIT/highpass : 0.03843ms
60-
BenchmarkJIT/gradient : 0.03320ms
61-
BenchmarkJIT/edgedetect : 0.02701ms
62-
BenchmarkJIT/sobel : 0.06392ms
63-
BenchmarkJIT/canny : 0.28839ms
64-
BenchmarkJIT/canny_dilate : 0.34880ms
65-
BenchmarkJIT/canny_morphology_open : 0.38943ms
66-
BenchmarkJIT/canny_morphology_close : 0.39011ms
67-
BenchmarkJIT/match_template_sad : 5.69188ms
68-
BenchmarkJIT/match_template_ssd : 4.75666ms
69-
BenchmarkJIT/match_template_ncc : 8.98426ms
70-
BenchmarkJIT/prepared_match_template_ncc : 6.23328ms
71-
BenchmarkJIT/match_template_zncc : 12.64066ms
72-
BenchmarkJIT/prepared_match_template_zncc : 11.67131ms
22+
BenchmarkJIT/cloneimg : 0.00788ms
23+
BenchmarkJIT/convert_from_argb : 0.02375ms
24+
BenchmarkJIT/convert_from_abgr : 0.03884ms
25+
BenchmarkJIT/convert_from_bgra : 0.02470ms
26+
BenchmarkJIT/convert_from_rabg : 0.03655ms
27+
BenchmarkJIT/convert_from_yuv_420 : 0.03168ms
28+
BenchmarkJIT/convert_from_yuv_444 : 0.02686ms
29+
BenchmarkJIT/convert_to_yuv_420 : 0.06742ms
30+
BenchmarkJIT/convert_to_yuv_444 : 0.07209ms
31+
BenchmarkJIT/rotate0 : 0.00774ms
32+
BenchmarkJIT/rotate90 : 0.02599ms
33+
BenchmarkJIT/rotate180 : 0.00802ms
34+
BenchmarkJIT/rotate270 : 0.02582ms
35+
BenchmarkJIT/crop : 0.06126ms
36+
BenchmarkJIT/scale : 0.14009ms
37+
BenchmarkJIT/scale_box : 0.20598ms
38+
BenchmarkJIT/scale_linear : 0.20440ms
39+
BenchmarkJIT/scale_gaussian : 0.31444ms
40+
BenchmarkJIT/blend_normal : 0.08443ms
41+
BenchmarkJIT/blend_sub : 0.08398ms
42+
BenchmarkJIT/blend_add : 0.08364ms
43+
BenchmarkJIT/blend_diff : 0.08453ms
44+
BenchmarkJIT/grayscale : 0.03687ms
45+
BenchmarkJIT/invert : 0.03730ms
46+
BenchmarkJIT/brightness : 0.04703ms
47+
BenchmarkJIT/gammacorrection : 0.08013ms
48+
BenchmarkJIT/contrast : 0.01549ms
49+
BenchmarkJIT/boxblur : 0.11211ms
50+
BenchmarkJIT/gaussianblur : 0.33058ms
51+
BenchmarkJIT/blockmozaic : 0.27455ms
52+
BenchmarkJIT/erosion : 0.11934ms
53+
BenchmarkJIT/dilation : 0.12013ms
54+
BenchmarkJIT/morphology_open : 0.10370ms
55+
BenchmarkJIT/morphology_close : 0.10435ms
56+
BenchmarkJIT/morphology_gradient : 0.07684ms
57+
BenchmarkJIT/emboss : 0.04402ms
58+
BenchmarkJIT/laplacian : 0.03192ms
59+
BenchmarkJIT/highpass : 0.03847ms
60+
BenchmarkJIT/gradient : 0.03322ms
61+
BenchmarkJIT/edgedetect : 0.02705ms
62+
BenchmarkJIT/sobel : 0.06276ms
63+
BenchmarkJIT/canny : 0.29922ms
64+
BenchmarkJIT/canny_dilate : 0.35114ms
65+
BenchmarkJIT/canny_morphology_open : 0.44488ms
66+
BenchmarkJIT/canny_morphology_close : 0.40522ms
67+
BenchmarkJIT/match_template_sad : 5.75482ms
68+
BenchmarkJIT/match_template_ssd : 4.48363ms
69+
BenchmarkJIT/match_template_ncc : 8.32420ms
70+
BenchmarkJIT/prepared_match_template_ncc : 6.22423ms
71+
BenchmarkJIT/match_template_zncc : 12.73780ms
72+
BenchmarkJIT/prepared_match_template_zncc : 11.38906ms
73+
BenchmarkJIT/pcm16_decibel : 0.00257ms
7374
```
7475

7576
## AOT benchmarks
@@ -620,6 +621,20 @@ ycbcr, err := blurry.ConvertToYUV444(rgba)
620621
| 420 | `blurry.ConvertToYUV420(*image.RGBA)` |
621622
| 444 | `blurry.ConvertToYUV444(*image.RGBA)` |
622623

624+
#### PCM16 Decibel
625+
626+
Gets the decibel of given PCM16.
627+
628+
```go
629+
var data []byte
630+
decibel, err := blurry.PCM16Decibel(data, length)
631+
632+
or
633+
634+
var input []int16
635+
decibel, err := blurry.PCM16DecibelFromInt16(input)
636+
```
637+
623638
## CLI usage
624639

625640
Run it via docker.

blurry.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const Expr degree0 = cast<uint8_t>(0);
2222
const Expr degree45 = cast<uint8_t>(45);
2323
const Expr degree90 = cast<uint8_t>(90);
2424
const Expr degree135 = cast<uint8_t>(135);
25+
const Expr pcm16_max = cast<float>(32768.0f); // 2^15
26+
const Expr ln10 = cast<float>(2.30258509299404568401799145468436420760110148862877297603332790f); // https://oeis.org/A002392
2527

2628
const Func kernel_sobel_x = kernel_sobel3x3_x();
2729
const Func kernel_sobel_y = kernel_sobel3x3_y();
@@ -145,6 +147,34 @@ Func wrapFunc_xy(Buffer<double> buf, const char* name) {
145147
return f;
146148
}
147149

150+
Func wrapFunc_x(Buffer<uint8_t> buf, const char* name) {
151+
Var x("x");
152+
Func f = Func(name);
153+
f(x) = buf(x);
154+
return f;
155+
}
156+
157+
Func wrapFunc_x(Buffer<int16_t> buf, const char* name) {
158+
Var x("x");
159+
Func f = Func(name);
160+
f(x) = buf(x);
161+
return f;
162+
}
163+
164+
Func wrapFunc_x(Buffer<float> buf, const char* name) {
165+
Var x("x");
166+
Func f = Func(name);
167+
f(x) = buf(x);
168+
return f;
169+
}
170+
171+
Func wrapFunc_x(Buffer<double> buf, const char* name) {
172+
Var x("x");
173+
Func f = Func(name);
174+
f(x) = buf(x);
175+
return f;
176+
}
177+
148178
Func read(Func clamped, const char *name) {
149179
Var x("x"), y("y"), ch("ch");
150180
Func read = Func(name);
@@ -2982,3 +3012,40 @@ Func contour_fn(
29823012
f(x, y) = value;
29833013
return f;
29843014
}
3015+
3016+
Expr log10(Expr v) {
3017+
return fast_log(v) / ln10;
3018+
}
3019+
3020+
Func pcm16_decibel_fn(
3021+
Func input, Param<int32_t> length
3022+
) {
3023+
Var x("x");
3024+
3025+
Func normalize = Func("pcm16_normalize");
3026+
Expr in = cast<int16_t>(input(x));
3027+
normalize(x) = cast<float>(in) / pcm16_max; // -1.0 .. x .. 1.0
3028+
3029+
Func sq = Func("square");
3030+
sq(x) = fast_pow(normalize(x), 2);
3031+
3032+
RDom rd = RDom(0, length, "length");
3033+
Func rms = Func("root_mean_square");
3034+
Expr sum_values = sum(sq(rd));
3035+
Expr mean = sum_values / cast<float>(length);
3036+
rms(_) = sqrt(mean);
3037+
3038+
Func decibel = Func("pcm16_decibel");
3039+
decibel(_) = 20 * (log10(rms(_)));
3040+
3041+
// schedule
3042+
3043+
normalize.compute_at(sq, x)
3044+
.store_root()
3045+
.vectorize(x, 64);
3046+
sq.compute_root()
3047+
.store_root()
3048+
.vectorize(x, 64);
3049+
3050+
return decibel;
3051+
}

blurry.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ Func wrapFunc_xy(Buffer<float> buf, const char* name);
3333

3434
Func wrapFunc_xy(Buffer<double> buf, const char* name);
3535

36+
Func wrapFunc_x(Buffer<uint8_t> buf, const char* name);
37+
38+
Func wrapFunc_x(Buffer<int16_t> buf, const char* name);
39+
40+
Func wrapFunc_x(Buffer<float> buf, const char* name);
41+
42+
Func wrapFunc_x(Buffer<double> buf, const char* name);
43+
3644
Func cloneimg_fn(Func input, Param<int32_t> width, Param<int32_t> height);
3745

3846
Func convert_from_abgr_fn(Func input, Param<int32_t> width, Param<int32_t> height);
@@ -225,4 +233,7 @@ Func contour_fn(
225233
Param<uint8_t> threshold, Param<uint8_t> size
226234
);
227235

236+
Func pcm16_decibel_fn(
237+
Func input, Param<int32_t> length
238+
);
228239
#endif // BLURRY_H_

blurry_benchmark.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ int jit_benchmark_bounds(Func fn, int32_t width, int32_t height, std::string nam
1515
return 0;
1616
}
1717

18+
int jit_benchmark_pcm16_bounds(Func fn, int32_t length, std::string name) {
19+
fn.compile_jit(get_jit_target_from_environment());
20+
21+
double result = benchmark(100, 10, [&]() {
22+
fn.realize({length});
23+
});
24+
printf("BenchmarkJIT/%-30s: %-3.5fms\n", name.c_str(), result * 1e3);
25+
return 0;
26+
}
27+
1828
int jit_benchmark(Func fn, Buffer<uint8_t> buf_src, std::string name) {
1929
return jit_benchmark_bounds(fn, buf_src.get()->width(), buf_src.get()->height(), name);
2030
}
@@ -640,6 +650,27 @@ int benchmark_prepared_match_template_zncc(
640650
), buf_src, "prepared_match_template_zncc");
641651
}
642652

653+
int benchmark_pcm16_decibel(){
654+
FILE *const f = fopen("./testdata/pcm16_1.data", "rb");
655+
if(f == nullptr) {
656+
return 1;
657+
}
658+
int32_t length = 1024;
659+
int16_t *data = (int16_t *) calloc(length, sizeof(int16_t));
660+
fread(data, sizeof(int16_t), length, f);
661+
fclose(f);
662+
663+
Buffer<int16_t> buf_src = Buffer<int16_t>::make_interleaved(data, length, 0, 1);
664+
buf_src.raw_buffer()->dimensions = 1;
665+
buf_src.raw_buffer()->dim[0].extent = length;
666+
buf_src.raw_buffer()->dim[0].stride = 1;
667+
Param<int32_t> _length{"length", length};
668+
669+
return jit_benchmark_pcm16_bounds(pcm16_decibel_fn(
670+
wrapFunc_x(buf_src, "buf_src"), _length
671+
), length, "pcm16_decibel");
672+
}
673+
643674
int benchmark(char **argv) {
644675
printf("benchmark...\n");
645676
Buffer<uint8_t> buf_src = load_and_convert_image(argv[2]);
@@ -702,6 +733,7 @@ int benchmark(char **argv) {
702733
benchmark_prepared_match_template_ncc(buf_src, width, height, buf_tpl, tpl_width, tpl_height);
703734
benchmark_match_template_zncc(buf_src, width, height, buf_tpl, tpl_width, tpl_height);
704735
benchmark_prepared_match_template_zncc(buf_src, width, height, buf_tpl, tpl_width, tpl_height);
736+
benchmark_pcm16_decibel();
705737
return 0;
706738
}
707739

blurry_gen.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ void init_input_yuv_444(ImageParam in_y, ImageParam in_u, ImageParam in_v, Param
134134
in_v.dim(1).set_stride(width);
135135
}
136136

137+
void init_input_array(ImageParam in, Param<int32_t> size) {
138+
in.dim(0).set_stride(1);
139+
in.dim(1).set_stride(size);
140+
}
141+
137142
void init_input_array(ImageParam in, Param<int32_t> width, Param<int32_t> height) {
138143
in.dim(0).set_stride(1);
139144
in.dim(1).set_stride(width);
@@ -155,6 +160,11 @@ void init_output_yuv_444(OutputImageParam out, Param<int32_t> width, Param<int32
155160
out.dim(1).set_stride(width);
156161
}
157162

163+
void init_output_array(OutputImageParam out, Param<int32_t> size) {
164+
out.dim(0).set_stride(1);
165+
out.dim(1).set_stride(size);
166+
}
167+
158168
void init_output_array(OutputImageParam out, Param<int32_t> width, Param<int32_t> height) {
159169
out.dim(0).set_stride(1);
160170
out.dim(1).set_stride(width);
@@ -1665,6 +1675,22 @@ void generate_contour(std::vector<Target::Feature> features) {
16651675
// }}} contour
16661676
//
16671677

1678+
//
1679+
// {{{ pcm16_decibel
1680+
//
1681+
void generate_pcm16_decibel(std::vector<Target::Feature> features) {
1682+
ImageParam src(type_of<int16_t>(), 1, "src");
1683+
1684+
Param<int32_t> length{"length", 1024};
1685+
1686+
Func fn = pcm16_decibel_fn(src.in(), length);
1687+
1688+
generate_static_link(features, fn, { src, length }, "pcm16_decibel");
1689+
}
1690+
//
1691+
// }}} pcm16_decibel
1692+
//
1693+
16681694
int generate(char **argv){
16691695
printf("generate...\n");
16701696

@@ -1735,6 +1761,7 @@ int generate(char **argv){
17351761
generate_prepare_zncc_template(features);
17361762
generate_prepared_match_template_zncc(features);
17371763
generate_contour(features);
1764+
generate_pcm16_decibel(features);
17381765

17391766
return 0;
17401767
}

0 commit comments

Comments
 (0)