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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
// Copyright 2019 The libgav1 Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/dsp/loop_filter.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include "src/dsp/dsp.h"
#include "src/utils/common.h"
namespace libgav1 {
namespace dsp {
namespace {
// 7.14.6.1.
template <int bitdepth, typename Pixel>
struct LoopFilterFuncs_C {
LoopFilterFuncs_C() = delete;
static constexpr int kMaxPixel = (1 << bitdepth) - 1;
static constexpr int kMinSignedPixel = -(1 << (bitdepth - 1));
static constexpr int kMaxSignedPixel = (1 << (bitdepth - 1)) - 1;
static constexpr int kFlatThresh = 1 << (bitdepth - 8);
static void Vertical4(void* dest, ptrdiff_t stride, int outer_thresh,
int inner_thresh, int hev_thresh);
static void Horizontal4(void* dest, ptrdiff_t stride, int outer_thresh,
int inner_thresh, int hev_thresh);
static void Vertical6(void* dest, ptrdiff_t stride, int outer_thresh,
int inner_thresh, int hev_thresh);
static void Horizontal6(void* dest, ptrdiff_t stride, int outer_thresh,
int inner_thresh, int hev_thresh);
static void Vertical8(void* dest, ptrdiff_t stride, int outer_thresh,
int inner_thresh, int hev_thresh);
static void Horizontal8(void* dest, ptrdiff_t stride, int outer_thresh,
int inner_thresh, int hev_thresh);
static void Vertical14(void* dest, ptrdiff_t stride, int outer_thresh,
int inner_thresh, int hev_thresh);
static void Horizontal14(void* dest, ptrdiff_t stride, int outer_thresh,
int inner_thresh, int hev_thresh);
};
inline void AdjustThresholds(const int bitdepth, int* const outer_thresh,
int* const inner_thresh, int* const hev_thresh) {
assert(*outer_thresh >= 7 && *outer_thresh <= 3 * kMaxLoopFilterValue + 4);
assert(*inner_thresh >= 1 && *inner_thresh <= kMaxLoopFilterValue);
assert(*hev_thresh >= 0 && *hev_thresh <= 3);
*outer_thresh <<= bitdepth - 8;
*inner_thresh <<= bitdepth - 8;
*hev_thresh <<= bitdepth - 8;
}
//------------------------------------------------------------------------------
// 4-tap filters
// 7.14.6.2.
template <typename Pixel>
inline bool NeedsFilter4(const Pixel* p, ptrdiff_t step, int outer_thresh,
int inner_thresh) {
const int p1 = p[-2 * step], p0 = p[-step];
const int q0 = p[0], q1 = p[step];
return std::abs(p1 - p0) <= inner_thresh &&
std::abs(q1 - q0) <= inner_thresh &&
std::abs(p0 - q0) * 2 + std::abs(p1 - q1) / 2 <= outer_thresh;
}
// 7.14.6.2.
template <typename Pixel>
inline bool Hev(const Pixel* p, ptrdiff_t step, int thresh) {
const int p1 = p[-2 * step], p0 = p[-step], q0 = p[0], q1 = p[step];
return (std::abs(p1 - p0) > thresh) || (std::abs(q1 - q0) > thresh);
}
// 7.14.6.3.
// 4 pixels in, 2 pixels out.
template <int bitdepth, typename Pixel>
inline void Filter2_C(Pixel* p, ptrdiff_t step) {
const int p1 = p[-2 * step], p0 = p[-step], q0 = p[0], q1 = p[step];
const int min_signed_val =
LoopFilterFuncs_C<bitdepth, Pixel>::kMinSignedPixel;
const int max_signed_val =
LoopFilterFuncs_C<bitdepth, Pixel>::kMaxSignedPixel;
// 8bpp: [-893,892], 10bpp: [-3581,3580], 12bpp [-14333,14332]
const int a = 3 * (q0 - p0) + Clip3(p1 - q1, min_signed_val, max_signed_val);
// 8bpp: [-16,15], 10bpp: [-64,63], 12bpp: [-256,255]
const int a1 = Clip3(a + 4, min_signed_val, max_signed_val) >> 3;
const int a2 = Clip3(a + 3, min_signed_val, max_signed_val) >> 3;
const int max_unsigned_val = LoopFilterFuncs_C<bitdepth, Pixel>::kMaxPixel;
p[-step] = Clip3(p0 + a2, 0, max_unsigned_val);
p[0] = Clip3(q0 - a1, 0, max_unsigned_val);
}
// 7.14.6.3.
// 4 pixels in, 4 pixels out.
template <int bitdepth, typename Pixel>
inline void Filter4_C(Pixel* p, ptrdiff_t step) {
const int p1 = p[-2 * step], p0 = p[-step], q0 = p[0], q1 = p[step];
const int a = 3 * (q0 - p0);
const int min_signed_val =
LoopFilterFuncs_C<bitdepth, Pixel>::kMinSignedPixel;
const int max_signed_val =
LoopFilterFuncs_C<bitdepth, Pixel>::kMaxSignedPixel;
const int a1 = Clip3(a + 4, min_signed_val, max_signed_val) >> 3;
const int a2 = Clip3(a + 3, min_signed_val, max_signed_val) >> 3;
const int a3 = (a1 + 1) >> 1;
const int max_unsigned_val = LoopFilterFuncs_C<bitdepth, Pixel>::kMaxPixel;
p[-2 * step] = Clip3(p1 + a3, 0, max_unsigned_val);
p[-1 * step] = Clip3(p0 + a2, 0, max_unsigned_val);
p[0 * step] = Clip3(q0 - a1, 0, max_unsigned_val);
p[1 * step] = Clip3(q1 - a3, 0, max_unsigned_val);
}
template <int bitdepth, typename Pixel>
void LoopFilterFuncs_C<bitdepth, Pixel>::Vertical4(void* dest, ptrdiff_t stride,
int outer_thresh,
int inner_thresh,
int hev_thresh) {
AdjustThresholds(bitdepth, &outer_thresh, &inner_thresh, &hev_thresh);
auto* dst = static_cast<Pixel*>(dest);
stride /= sizeof(Pixel);
for (int i = 0; i < 4; ++i) {
if (NeedsFilter4(dst, 1, outer_thresh, inner_thresh)) {
if (Hev(dst, 1, hev_thresh)) {
Filter2_C<bitdepth>(dst, 1);
} else {
Filter4_C<bitdepth>(dst, 1);
}
}
dst += stride;
}
}
template <int bitdepth, typename Pixel>
void LoopFilterFuncs_C<bitdepth, Pixel>::Horizontal4(void* dest,
ptrdiff_t stride,
int outer_thresh,
int inner_thresh,
int hev_thresh) {
AdjustThresholds(bitdepth, &outer_thresh, &inner_thresh, &hev_thresh);
auto* dst = static_cast<Pixel*>(dest);
stride /= sizeof(Pixel);
for (int i = 0; i < 4; ++i) {
if (NeedsFilter4(dst, stride, outer_thresh, inner_thresh)) {
if (Hev(dst, stride, hev_thresh)) {
Filter2_C<bitdepth>(dst, stride);
} else {
Filter4_C<bitdepth>(dst, stride);
}
}
++dst;
}
}
//------------------------------------------------------------------------------
// 5-tap (chroma) filters
// 7.14.6.2.
template <typename Pixel>
inline bool NeedsFilter6(const Pixel* p, ptrdiff_t step, int outer_thresh,
int inner_thresh) {
const int p2 = p[-3 * step], p1 = p[-2 * step], p0 = p[-step];
const int q0 = p[0], q1 = p[step], q2 = p[2 * step];
return std::abs(p2 - p1) <= inner_thresh &&
std::abs(p1 - p0) <= inner_thresh &&
std::abs(q1 - q0) <= inner_thresh &&
std::abs(q2 - q1) <= inner_thresh &&
std::abs(p0 - q0) * 2 + std::abs(p1 - q1) / 2 <= outer_thresh;
}
// 7.14.6.2.
template <typename Pixel>
inline bool IsFlat3(const Pixel* p, ptrdiff_t step, int flat_thresh) {
const int p2 = p[-3 * step], p1 = p[-2 * step], p0 = p[-step];
const int q0 = p[0], q1 = p[step], q2 = p[2 * step];
return std::abs(p1 - p0) <= flat_thresh && std::abs(q1 - q0) <= flat_thresh &&
std::abs(p2 - p0) <= flat_thresh && std::abs(q2 - q0) <= flat_thresh;
}
template <typename Pixel>
inline Pixel ApplyFilter6(int filter_value) {
return static_cast<Pixel>(RightShiftWithRounding(filter_value, 3));
}
// 7.14.6.4.
// 6 pixels in, 4 pixels out.
template <typename Pixel>
inline void Filter6_C(Pixel* p, ptrdiff_t step) {
const int p2 = p[-3 * step], p1 = p[-2 * step], p0 = p[-step];
const int q0 = p[0], q1 = p[step], q2 = p[2 * step];
const int a1 = 2 * p1;
const int a0 = 2 * p0;
const int b0 = 2 * q0;
const int b1 = 2 * q1;
// The max is 8 * max_pixel + 4 for the rounder.
// 8bpp: 2044 (11 bits), 10bpp: 8188 (13 bits), 12bpp: 32764 (15 bits)
p[-2 * step] = ApplyFilter6<Pixel>(3 * p2 + a1 + a0 + q0);
p[-1 * step] = ApplyFilter6<Pixel>(p2 + a1 + a0 + b0 + q1);
p[0 * step] = ApplyFilter6<Pixel>(p1 + a0 + b0 + b1 + q2);
p[1 * step] = ApplyFilter6<Pixel>(p0 + b0 + b1 + 3 * q2);
}
template <int bitdepth, typename Pixel>
void LoopFilterFuncs_C<bitdepth, Pixel>::Vertical6(void* dest, ptrdiff_t stride,
int outer_thresh,
int inner_thresh,
int hev_thresh) {
const int flat_thresh = LoopFilterFuncs_C<bitdepth, Pixel>::kFlatThresh;
AdjustThresholds(bitdepth, &outer_thresh, &inner_thresh, &hev_thresh);
auto* dst = static_cast<Pixel*>(dest);
stride /= sizeof(Pixel);
for (int i = 0; i < 4; ++i) {
if (NeedsFilter6(dst, 1, outer_thresh, inner_thresh)) {
if (IsFlat3(dst, 1, flat_thresh)) {
Filter6_C(dst, 1);
} else if (Hev(dst, 1, hev_thresh)) {
Filter2_C<bitdepth>(dst, 1);
} else {
Filter4_C<bitdepth>(dst, 1);
}
}
dst += stride;
}
}
template <int bitdepth, typename Pixel>
void LoopFilterFuncs_C<bitdepth, Pixel>::Horizontal6(void* dest,
ptrdiff_t stride,
int outer_thresh,
int inner_thresh,
int hev_thresh) {
const int flat_thresh = LoopFilterFuncs_C<bitdepth, Pixel>::kFlatThresh;
AdjustThresholds(bitdepth, &outer_thresh, &inner_thresh, &hev_thresh);
auto* dst = static_cast<Pixel*>(dest);
stride /= sizeof(Pixel);
for (int i = 0; i < 4; ++i) {
if (NeedsFilter6(dst, stride, outer_thresh, inner_thresh)) {
if (IsFlat3(dst, stride, flat_thresh)) {
Filter6_C(dst, stride);
} else if (Hev(dst, stride, hev_thresh)) {
Filter2_C<bitdepth>(dst, stride);
} else {
Filter4_C<bitdepth>(dst, stride);
}
}
++dst;
}
}
//------------------------------------------------------------------------------
// 7-tap filters
// 7.14.6.2.
template <typename Pixel>
inline bool NeedsFilter8(const Pixel* p, ptrdiff_t step, int outer_thresh,
int inner_thresh) {
const int p3 = p[-4 * step], p2 = p[-3 * step], p1 = p[-2 * step],
p0 = p[-step];
const int q0 = p[0], q1 = p[step], q2 = p[2 * step], q3 = p[3 * step];
return std::abs(p3 - p2) <= inner_thresh &&
std::abs(p2 - p1) <= inner_thresh &&
std::abs(p1 - p0) <= inner_thresh &&
std::abs(q1 - q0) <= inner_thresh &&
std::abs(q2 - q1) <= inner_thresh &&
std::abs(q3 - q2) <= inner_thresh &&
std::abs(p0 - q0) * 2 + std::abs(p1 - q1) / 2 <= outer_thresh;
}
// 7.14.6.2.
template <typename Pixel>
inline bool IsFlat4(const Pixel* p, ptrdiff_t step, int flat_thresh) {
const int p3 = p[-4 * step], p2 = p[-3 * step], p1 = p[-2 * step],
p0 = p[-step];
const int q0 = p[0], q1 = p[step], q2 = p[2 * step], q3 = p[3 * step];
return std::abs(p1 - p0) <= flat_thresh && std::abs(q1 - q0) <= flat_thresh &&
std::abs(p2 - p0) <= flat_thresh && std::abs(q2 - q0) <= flat_thresh &&
std::abs(p3 - p0) <= flat_thresh && std::abs(q3 - q0) <= flat_thresh;
}
template <typename Pixel>
inline Pixel ApplyFilter8(int filter_value) {
return static_cast<Pixel>(RightShiftWithRounding(filter_value, 3));
}
// 7.14.6.4.
// 8 pixels in, 6 pixels out.
template <typename Pixel>
inline void Filter8_C(Pixel* p, ptrdiff_t step) {
const int p3 = p[-4 * step], p2 = p[-3 * step], p1 = p[-2 * step],
p0 = p[-step];
const int q0 = p[0], q1 = p[step], q2 = p[2 * step], q3 = p[3 * step];
// The max is 8 * max_pixel + 4 for the rounder.
// 8bpp: 2044 (11 bits), 10bpp: 8188 (13 bits), 12bpp: 32764 (15 bits)
p[-3 * step] = ApplyFilter8<Pixel>(3 * p3 + 2 * p2 + p1 + p0 + q0);
p[-2 * step] = ApplyFilter8<Pixel>(2 * p3 + p2 + 2 * p1 + p0 + q0 + q1);
p[-1 * step] = ApplyFilter8<Pixel>(p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2);
p[0 * step] = ApplyFilter8<Pixel>(p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3);
p[1 * step] = ApplyFilter8<Pixel>(p1 + p0 + q0 + 2 * q1 + q2 + 2 * q3);
p[2 * step] = ApplyFilter8<Pixel>(p0 + q0 + q1 + 2 * q2 + 3 * q3);
}
template <int bitdepth, typename Pixel>
void LoopFilterFuncs_C<bitdepth, Pixel>::Vertical8(void* dest, ptrdiff_t stride,
int outer_thresh,
int inner_thresh,
int hev_thresh) {
const int flat_thresh = LoopFilterFuncs_C<bitdepth, Pixel>::kFlatThresh;
AdjustThresholds(bitdepth, &outer_thresh, &inner_thresh, &hev_thresh);
auto* dst = static_cast<Pixel*>(dest);
stride /= sizeof(Pixel);
for (int i = 0; i < 4; ++i) {
if (NeedsFilter8(dst, 1, outer_thresh, inner_thresh)) {
if (IsFlat4(dst, 1, flat_thresh)) {
Filter8_C(dst, 1);
} else if (Hev(dst, 1, hev_thresh)) {
Filter2_C<bitdepth>(dst, 1);
} else {
Filter4_C<bitdepth>(dst, 1);
}
}
dst += stride;
}
}
template <int bitdepth, typename Pixel>
void LoopFilterFuncs_C<bitdepth, Pixel>::Horizontal8(void* dest,
ptrdiff_t stride,
int outer_thresh,
int inner_thresh,
int hev_thresh) {
const int flat_thresh = LoopFilterFuncs_C<bitdepth, Pixel>::kFlatThresh;
AdjustThresholds(bitdepth, &outer_thresh, &inner_thresh, &hev_thresh);
auto* dst = static_cast<Pixel*>(dest);
stride /= sizeof(Pixel);
for (int i = 0; i < 4; ++i) {
if (NeedsFilter8(dst, stride, outer_thresh, inner_thresh)) {
if (IsFlat4(dst, stride, flat_thresh)) {
Filter8_C(dst, stride);
} else if (Hev(dst, stride, hev_thresh)) {
Filter2_C<bitdepth>(dst, stride);
} else {
Filter4_C<bitdepth>(dst, stride);
}
}
++dst;
}
}
//------------------------------------------------------------------------------
// 13-tap filters
// 7.14.6.2.
template <typename Pixel>
inline bool IsFlatOuter4(const Pixel* p, ptrdiff_t step, int flat_thresh) {
const int p6 = p[-7 * step], p5 = p[-6 * step], p4 = p[-5 * step],
p0 = p[-step];
const int q0 = p[0], q4 = p[4 * step], q5 = p[5 * step], q6 = p[6 * step];
return std::abs(p4 - p0) <= flat_thresh && std::abs(q4 - q0) <= flat_thresh &&
std::abs(p5 - p0) <= flat_thresh && std::abs(q5 - q0) <= flat_thresh &&
std::abs(p6 - p0) <= flat_thresh && std::abs(q6 - q0) <= flat_thresh;
}
template <typename Pixel>
inline Pixel ApplyFilter14(int filter_value) {
return static_cast<Pixel>(RightShiftWithRounding(filter_value, 4));
}
// 7.14.6.4.
// 14 pixels in, 12 pixels out.
template <typename Pixel>
inline void Filter14_C(Pixel* p, ptrdiff_t step) {
const int p6 = p[-7 * step], p5 = p[-6 * step], p4 = p[-5 * step],
p3 = p[-4 * step], p2 = p[-3 * step], p1 = p[-2 * step],
p0 = p[-step];
const int q0 = p[0], q1 = p[step], q2 = p[2 * step], q3 = p[3 * step],
q4 = p[4 * step], q5 = p[5 * step], q6 = p[6 * step];
// The max is 16 * max_pixel + 8 for the rounder.
// 8bpp: 4088 (12 bits), 10bpp: 16376 (14 bits), 12bpp: 65528 (16 bits)
p[-6 * step] =
ApplyFilter14<Pixel>(p6 * 7 + p5 * 2 + p4 * 2 + p3 + p2 + p1 + p0 + q0);
p[-5 * step] = ApplyFilter14<Pixel>(p6 * 5 + p5 * 2 + p4 * 2 + p3 * 2 + p2 +
p1 + p0 + q0 + q1);
p[-4 * step] = ApplyFilter14<Pixel>(p6 * 4 + p5 + p4 * 2 + p3 * 2 + p2 * 2 +
p1 + p0 + q0 + q1 + q2);
p[-3 * step] = ApplyFilter14<Pixel>(p6 * 3 + p5 + p4 + p3 * 2 + p2 * 2 +
p1 * 2 + p0 + q0 + q1 + q2 + q3);
p[-2 * step] = ApplyFilter14<Pixel>(p6 * 2 + p5 + p4 + p3 + p2 * 2 + p1 * 2 +
p0 * 2 + q0 + q1 + q2 + q3 + q4);
p[-1 * step] = ApplyFilter14<Pixel>(p6 + p5 + p4 + p3 + p2 + p1 * 2 + p0 * 2 +
q0 * 2 + q1 + q2 + q3 + q4 + q5);
p[0 * step] = ApplyFilter14<Pixel>(p5 + p4 + p3 + p2 + p1 + p0 * 2 + q0 * 2 +
q1 * 2 + q2 + q3 + q4 + q5 + q6);
p[1 * step] = ApplyFilter14<Pixel>(p4 + p3 + p2 + p1 + p0 + q0 * 2 + q1 * 2 +
q2 * 2 + q3 + q4 + q5 + q6 * 2);
p[2 * step] = ApplyFilter14<Pixel>(p3 + p2 + p1 + p0 + q0 + q1 * 2 + q2 * 2 +
q3 * 2 + q4 + q5 + q6 * 3);
p[3 * step] = ApplyFilter14<Pixel>(p2 + p1 + p0 + q0 + q1 + q2 * 2 + q3 * 2 +
q4 * 2 + q5 + q6 * 4);
p[4 * step] = ApplyFilter14<Pixel>(p1 + p0 + q0 + q1 + q2 + q3 * 2 + q4 * 2 +
q5 * 2 + q6 * 5);
p[5 * step] =
ApplyFilter14<Pixel>(p0 + q0 + q1 + q2 + q3 + q4 * 2 + q5 * 2 + q6 * 7);
}
template <int bitdepth, typename Pixel>
void LoopFilterFuncs_C<bitdepth, Pixel>::Vertical14(void* dest,
ptrdiff_t stride,
int outer_thresh,
int inner_thresh,
int hev_thresh) {
const int flat_thresh = LoopFilterFuncs_C<bitdepth, Pixel>::kFlatThresh;
AdjustThresholds(bitdepth, &outer_thresh, &inner_thresh, &hev_thresh);
auto* dst = static_cast<Pixel*>(dest);
stride /= sizeof(Pixel);
for (int i = 0; i < 4; ++i) {
if (NeedsFilter8(dst, 1, outer_thresh, inner_thresh)) {
if (IsFlat4(dst, 1, flat_thresh)) {
if (IsFlatOuter4(dst, 1, flat_thresh)) {
Filter14_C(dst, 1);
} else {
Filter8_C(dst, 1);
}
} else if (Hev(dst, 1, hev_thresh)) {
Filter2_C<bitdepth>(dst, 1);
} else {
Filter4_C<bitdepth>(dst, 1);
}
}
dst += stride;
}
}
template <int bitdepth, typename Pixel>
void LoopFilterFuncs_C<bitdepth, Pixel>::Horizontal14(void* dest,
ptrdiff_t stride,
int outer_thresh,
int inner_thresh,
int hev_thresh) {
const int flat_thresh = LoopFilterFuncs_C<bitdepth, Pixel>::kFlatThresh;
AdjustThresholds(bitdepth, &outer_thresh, &inner_thresh, &hev_thresh);
auto* dst = static_cast<Pixel*>(dest);
stride /= sizeof(Pixel);
for (int i = 0; i < 4; ++i) {
if (NeedsFilter8(dst, stride, outer_thresh, inner_thresh)) {
if (IsFlat4(dst, stride, flat_thresh)) {
if (IsFlatOuter4(dst, stride, flat_thresh)) {
Filter14_C(dst, stride);
} else {
Filter8_C(dst, stride);
}
} else if (Hev(dst, stride, hev_thresh)) {
Filter2_C<bitdepth>(dst, stride);
} else {
Filter4_C<bitdepth>(dst, stride);
}
}
++dst;
}
}
using Defs8bpp = LoopFilterFuncs_C<8, uint8_t>;
void Init8bpp() {
Dsp* const dsp = dsp_internal::GetWritableDspTable(8);
assert(dsp != nullptr);
#if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeHorizontal] =
Defs8bpp::Horizontal4;
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeVertical] =
Defs8bpp::Vertical4;
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeHorizontal] =
Defs8bpp::Horizontal6;
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeVertical] =
Defs8bpp::Vertical6;
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeHorizontal] =
Defs8bpp::Horizontal8;
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeVertical] =
Defs8bpp::Vertical8;
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeHorizontal] =
Defs8bpp::Horizontal14;
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeVertical] =
Defs8bpp::Vertical14;
#else // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
static_cast<void>(dsp);
#ifndef LIBGAV1_Dsp8bpp_LoopFilterSize4_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeHorizontal] =
Defs8bpp::Horizontal4;
#endif
#ifndef LIBGAV1_Dsp8bpp_LoopFilterSize4_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeVertical] =
Defs8bpp::Vertical4;
#endif
#ifndef LIBGAV1_Dsp8bpp_LoopFilterSize6_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeHorizontal] =
Defs8bpp::Horizontal6;
#endif
#ifndef LIBGAV1_Dsp8bpp_LoopFilterSize6_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeVertical] =
Defs8bpp::Vertical6;
#endif
#ifndef LIBGAV1_Dsp8bpp_LoopFilterSize8_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeHorizontal] =
Defs8bpp::Horizontal8;
#endif
#ifndef LIBGAV1_Dsp8bpp_LoopFilterSize8_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeVertical] =
Defs8bpp::Vertical8;
#endif
#ifndef LIBGAV1_Dsp8bpp_LoopFilterSize14_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeHorizontal] =
Defs8bpp::Horizontal14;
#endif
#ifndef LIBGAV1_Dsp8bpp_LoopFilterSize14_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeVertical] =
Defs8bpp::Vertical14;
#endif
#endif // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
}
#if LIBGAV1_MAX_BITDEPTH >= 10
using Defs10bpp = LoopFilterFuncs_C<10, uint16_t>;
void Init10bpp() {
Dsp* const dsp = dsp_internal::GetWritableDspTable(10);
assert(dsp != nullptr);
#if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeHorizontal] =
Defs10bpp::Horizontal4;
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeVertical] =
Defs10bpp::Vertical4;
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeHorizontal] =
Defs10bpp::Horizontal6;
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeVertical] =
Defs10bpp::Vertical6;
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeHorizontal] =
Defs10bpp::Horizontal8;
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeVertical] =
Defs10bpp::Vertical8;
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeHorizontal] =
Defs10bpp::Horizontal14;
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeVertical] =
Defs10bpp::Vertical14;
#else // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
static_cast<void>(dsp);
#ifndef LIBGAV1_Dsp10bpp_LoopFilterSize4_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeHorizontal] =
Defs10bpp::Horizontal4;
#endif
#ifndef LIBGAV1_Dsp10bpp_LoopFilterSize4_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeVertical] =
Defs10bpp::Vertical4;
#endif
#ifndef LIBGAV1_Dsp10bpp_LoopFilterSize6_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeHorizontal] =
Defs10bpp::Horizontal6;
#endif
#ifndef LIBGAV1_Dsp10bpp_LoopFilterSize6_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeVertical] =
Defs10bpp::Vertical6;
#endif
#ifndef LIBGAV1_Dsp10bpp_LoopFilterSize8_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeHorizontal] =
Defs10bpp::Horizontal8;
#endif
#ifndef LIBGAV1_Dsp10bpp_LoopFilterSize8_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeVertical] =
Defs10bpp::Vertical8;
#endif
#ifndef LIBGAV1_Dsp10bpp_LoopFilterSize14_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeHorizontal] =
Defs10bpp::Horizontal14;
#endif
#ifndef LIBGAV1_Dsp10bpp_LoopFilterSize14_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeVertical] =
Defs10bpp::Vertical14;
#endif
#endif // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
}
#endif // LIBGAV1_MAX_BITDEPTH >= 10
#if LIBGAV1_MAX_BITDEPTH == 12
using Defs12bpp = LoopFilterFuncs_C<12, uint16_t>;
void Init12bpp() {
Dsp* const dsp = dsp_internal::GetWritableDspTable(12);
assert(dsp != nullptr);
#if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeHorizontal] =
Defs12bpp::Horizontal4;
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeVertical] =
Defs12bpp::Vertical4;
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeHorizontal] =
Defs12bpp::Horizontal6;
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeVertical] =
Defs12bpp::Vertical6;
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeHorizontal] =
Defs12bpp::Horizontal8;
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeVertical] =
Defs12bpp::Vertical8;
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeHorizontal] =
Defs12bpp::Horizontal14;
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeVertical] =
Defs12bpp::Vertical14;
#else // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
static_cast<void>(dsp);
#ifndef LIBGAV1_Dsp12bpp_LoopFilterSize4_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeHorizontal] =
Defs12bpp::Horizontal4;
#endif
#ifndef LIBGAV1_Dsp12bpp_LoopFilterSize4_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize4][kLoopFilterTypeVertical] =
Defs12bpp::Vertical4;
#endif
#ifndef LIBGAV1_Dsp12bpp_LoopFilterSize6_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeHorizontal] =
Defs12bpp::Horizontal6;
#endif
#ifndef LIBGAV1_Dsp12bpp_LoopFilterSize6_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize6][kLoopFilterTypeVertical] =
Defs12bpp::Vertical6;
#endif
#ifndef LIBGAV1_Dsp12bpp_LoopFilterSize8_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeHorizontal] =
Defs12bpp::Horizontal8;
#endif
#ifndef LIBGAV1_Dsp12bpp_LoopFilterSize8_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize8][kLoopFilterTypeVertical] =
Defs12bpp::Vertical8;
#endif
#ifndef LIBGAV1_Dsp12bpp_LoopFilterSize14_LoopFilterTypeHorizontal
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeHorizontal] =
Defs12bpp::Horizontal14;
#endif
#ifndef LIBGAV1_Dsp12bpp_LoopFilterSize14_LoopFilterTypeVertical
dsp->loop_filters[kLoopFilterSize14][kLoopFilterTypeVertical] =
Defs12bpp::Vertical14;
#endif
#endif // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
}
#endif // LIBGAV1_MAX_BITDEPTH == 12
} // namespace
void LoopFilterInit_C() {
Init8bpp();
#if LIBGAV1_MAX_BITDEPTH >= 10
Init10bpp();
#endif
#if LIBGAV1_MAX_BITDEPTH == 12
Init12bpp();
#endif
// Local functions that may be unused depending on the optimizations
// available.
static_cast<void>(AdjustThresholds);
}
} // namespace dsp
} // namespace libgav1
|