TUT HEVC Encoder
sao_shared_generics.h
Go to the documentation of this file.
1/*****************************************************************************
2 * This file is part of Kvazaar HEVC encoder.
3 *
4 * Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright notice, this
14 * list of conditions and the following disclaimer in the documentation and/or
15 * other materials provided with the distribution.
16 *
17 * * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ****************************************************************************/
32
33#ifndef SAO_BAND_DDISTORTION_H_
34#define SAO_BAND_DDISTORTION_H_
35
36// #include "encoder.h"
37#include "encoderstate.h"
38#include "kvazaar.h"
39#include "sao.h"
40
41// Mapping of edge_idx values to eo-classes.
43{
44 // Mapping relationships between a, b and c to eo_idx.
45 static const int sao_eo_idx_to_eo_category[] = { 1, 2, 0, 3, 4 };
46
47 int eo_idx = 2 + SIGN3((int)c - (int)a) + SIGN3((int)c - (int)b);
48
49 return sao_eo_idx_to_eo_category[eo_idx];
50}
51
52static int sao_edge_ddistortion_generic(const kvz_pixel *orig_data,
53 const kvz_pixel *rec_data,
54 int32_t block_width,
55 int32_t block_height,
56 int32_t eo_class,
57 const int32_t offsets[NUM_SAO_EDGE_CATEGORIES])
58{
59 int y, x;
60 int32_t sum = 0;
61 vector2d_t a_ofs = g_sao_edge_offsets[eo_class][0];
62 vector2d_t b_ofs = g_sao_edge_offsets[eo_class][1];
63
64 for (y = 1; y < block_height - 1; y++) {
65 for (x = 1; x < block_width - 1; x++) {
66 uint32_t c_pos = y * block_width + x;
67 uint32_t a_pos = (y + a_ofs.y) * block_width + x + a_ofs.x;
68 uint32_t b_pos = (y + b_ofs.y) * block_width + x + b_ofs.x;
69
70 uint8_t a = rec_data[a_pos];
71 uint8_t b = rec_data[b_pos];
72 uint8_t c = rec_data[c_pos];
73 uint8_t orig = orig_data[c_pos];
74
75 int32_t eo_cat = sao_calc_eo_cat(a, b, c);
76 int32_t offset = offsets[eo_cat];
77
78 if (offset != 0) {
79 int32_t diff = orig - c;
80 int32_t delta = diff - offset;
81 int32_t curr = delta * delta - diff * diff;
82
83 sum += curr;
84 }
85 }
86 }
87 return sum;
88}
89
90static int sao_band_ddistortion_generic(const encoder_state_t * const state,
91 const kvz_pixel *orig_data,
92 const kvz_pixel *rec_data,
93 int block_width,
94 int block_height,
95 int band_pos,
96 const int sao_bands[4])
97{
98 int y, x;
99 int shift = state->encoder_control->bitdepth-5;
100 int sum = 0;
101 for (y = 0; y < block_height; ++y) {
102 for (x = 0; x < block_width; ++x) {
103 const int32_t curr_pos = y * block_width + x;
104
105 kvz_pixel rec = rec_data[curr_pos];
106 kvz_pixel orig = orig_data[curr_pos];
107
108 int32_t band = (rec >> shift) - band_pos;
109 int32_t offset = 0;
110 if (band >= 0 && band <= 3) {
111 offset = sao_bands[band];
112 }
113 // Offset is applied to reconstruction, so it is subtracted from diff.
114
115 int32_t diff = orig - rec;
116 int32_t delta = diff - offset;
117
118 int32_t dmask = (offset == 0) ? -1 : 0;
119 diff &= ~dmask;
120 delta &= ~dmask;
121
122 sum += delta * delta - diff * diff;
123 }
124 }
125
126 return sum;
127}
128
129#endif
Top level of the encoder implementation.
#define SIGN3(x)
Definition: global.h:216
This file defines the public API of Kvazaar when used as a library.
uint8_t kvz_pixel
Definition: kvazaar.h:95
Sample Adaptive Offset filter.
static const vector2d_t g_sao_edge_offsets[SAO_NUM_EO][2]
Definition: sao.h:71
@ NUM_SAO_EDGE_CATEGORIES
Definition: sao.h:52
static int sao_band_ddistortion_generic(const encoder_state_t *const state, const kvz_pixel *orig_data, const kvz_pixel *rec_data, int block_width, int block_height, int band_pos, const int sao_bands[4])
Definition: sao_shared_generics.h:90
static int sao_calc_eo_cat(kvz_pixel a, kvz_pixel b, kvz_pixel c)
Definition: sao_shared_generics.h:42
static int sao_edge_ddistortion_generic(const kvz_pixel *orig_data, const kvz_pixel *rec_data, int32_t block_width, int32_t block_height, int32_t eo_class, const int32_t offsets[NUM_SAO_EDGE_CATEGORIES])
Definition: sao_shared_generics.h:52
int8_t bitdepth
Definition: encoder.h:82
Definition: encoderstate.h:274
const encoder_control_t * encoder_control
Definition: encoderstate.h:275
Definition: cu.h:121
int y
Definition: cu.h:123
int x
Definition: cu.h:122