ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
/******************************************************************************
 *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * 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.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
 
/*!
******************************************************************************
* \file ihevce_enc_sbh_funcs.c
*
* \brief
*    This file contains utility functions for sbh
*
* \date
*    31/08/2012
*
* \author
*    Ittiam
*
* List of Functions
*    ihevce_sign_data_hiding()
*
******************************************************************************
*/
 
/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/
/* System include files */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <math.h>
 
/* User include files */
#include "ihevc_typedefs.h"
#include "itt_video_api.h"
#include "ihevce_api.h"
 
#include "rc_cntrl_param.h"
#include "rc_frame_info_collector.h"
#include "rc_look_ahead_params.h"
 
#include "ihevc_defs.h"
#include "ihevc_structs.h"
#include "ihevc_platform_macros.h"
#include "ihevc_deblk.h"
#include "ihevc_itrans_recon.h"
#include "ihevc_chroma_itrans_recon.h"
#include "ihevc_chroma_intra_pred.h"
#include "ihevc_intra_pred.h"
#include "ihevc_inter_pred.h"
#include "ihevc_mem_fns.h"
#include "ihevc_padding.h"
#include "ihevc_weighted_pred.h"
#include "ihevc_sao.h"
#include "ihevc_resi_trans.h"
#include "ihevc_quant_iquant_ssd.h"
#include "ihevc_cabac_tables.h"
#include "ihevc_trans_tables.h"
#include "ihevc_trans_macros.h"
 
#include "ihevce_defs.h"
#include "ihevce_lap_enc_structs.h"
#include "ihevce_multi_thrd_structs.h"
#include "ihevce_multi_thrd_funcs.h"
#include "ihevce_me_common_defs.h"
#include "ihevce_had_satd.h"
#include "ihevce_error_codes.h"
#include "ihevce_bitstream.h"
#include "ihevce_cabac.h"
#include "ihevce_rdoq_macros.h"
#include "ihevce_function_selector.h"
#include "ihevce_enc_structs.h"
#include "ihevce_global_tables.h"
#include "ihevce_enc_sbh_utils.h"
 
/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/
 
/**
*******************************************************************************
*
* @brief
*  This function find the coefficient that needs to be modified for SBH
*  for each sub block, if required
*
* @par Description:
*  Checks the validity for applying SBH
*
* @param[inout] ps_rdoq_sbh_params
*  All the necessary parameters for SBH
*
* @returns  None
*
* @remarks  None
*
********************************************************************************
*/
void ihevce_sign_data_hiding(rdoq_sbh_ctxt_t *ps_rdoq_sbh_params)
{
    WORD32 i, trans_unit_idx;
    UWORD8 *pu1_trans_table = NULL;
    UWORD8 *pu1_csb_table;
    WORD32 shift_value, mask_value;
    WORD32 blk_row, blk_col;
 
    WORD32 x_pos, y_pos;
    WORD16 i2_quant_coeff;
    WORD32 best_pos = -1;
 
    WORD16 *pi2_quant_coeffs = ps_rdoq_sbh_params->pi2_quant_coeffs;
    WORD16 *pi2_iquant_data = ps_rdoq_sbh_params->pi2_iquant_coeffs;
    WORD16 *pi2_tr_coeffs = ps_rdoq_sbh_params->pi2_trans_values;
    WORD32 *pi4_subBlock2csbfId_map = ps_rdoq_sbh_params->pi4_subBlock2csbfId_map;
    WORD16 *pi2_dequant_coeff = ps_rdoq_sbh_params->pi2_dequant_coeff;
    UWORD8 *pu1_csbf_buf = ps_rdoq_sbh_params->pu1_csbf_buf;
    WORD32 dst_iq_strd = ps_rdoq_sbh_params->i4_iq_data_strd;
    WORD32 dst_q_strd = ps_rdoq_sbh_params->i4_q_data_strd;
 
    WORD32 scan_idx = ps_rdoq_sbh_params->i4_scan_idx;
    WORD32 qp_div = ps_rdoq_sbh_params->i4_qp_div;
    WORD32 trans_size = ps_rdoq_sbh_params->i4_trans_size;
    WORD32 qp_rem = ps_rdoq_sbh_params->i2_qp_rem;
    LWORD64 ssd_cost = ps_rdoq_sbh_params->i8_ssd_cost;
 
    WORD32 last_cg = -1;
 
    WORD32 log2_size, bit_depth, shift_iq;
 
    GETRANGE(log2_size, trans_size);
    log2_size -= 1;
    bit_depth = ps_rdoq_sbh_params->i4_bit_depth;
    shift_iq = bit_depth + log2_size - 5;
 
    /* Select proper order for your transform unit and csb based on scan_idx*/
    /* and the trans_size */
 
    /* scan order inside a csb */
    pu1_csb_table = (UWORD8 *)&(g_u1_scan_table_4x4[scan_idx][0]);
 
    /* GETRANGE will give the log_2 of trans_size to shift_value */
    GETRANGE(shift_value, trans_size);
    shift_value = shift_value - 3; /* for finding. row no. from scan index */
    mask_value = (trans_size / 4) - 1; /*for finding the col. no. from scan index*/
    switch(trans_size)
    {
    case 32:
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_8x8[scan_idx][0]);
        break;
    case 16:
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_4x4[scan_idx][0]);
        break;
    case 8:
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_2x2[scan_idx][0]);
        break;
    case 4:
        pu1_trans_table = (UWORD8 *)&(g_u1_scan_table_1x1[0]);
        break;
    default:
        ASSERT(0);
        break;
    }
    for(trans_unit_idx = (trans_size * trans_size / 16) - 1; trans_unit_idx >= 0; trans_unit_idx--)
    {
        WORD32 last_scan_pos = -1, first_scan_pos = 16, sign_first_coeff, sum_abs_level = 0,
               quant_coeff_first;
 
        if(pu1_csbf_buf[pi4_subBlock2csbfId_map[pu1_trans_table[trans_unit_idx]]])
        {
            /* row of csb */
            blk_row = (pu1_trans_table[trans_unit_idx] >> shift_value) * 4;
            /* col of csb */
            blk_col = (pu1_trans_table[trans_unit_idx] & mask_value) * 4;
 
            if(last_cg == -1)
            {
                last_cg = 1;
            }
 
            for(i = 15; i >= 0; i--)
            {
                x_pos = (pu1_csb_table[i] & 0x3) + blk_col;
                y_pos = (pu1_csb_table[i] >> 2) + blk_row;
 
                i2_quant_coeff = pi2_quant_coeffs[x_pos + (y_pos * trans_size)];
 
                if(i2_quant_coeff)
                {
                    first_scan_pos = i;
                    if(-1 == last_scan_pos)
                    {
                        last_scan_pos = i;
                    }
 
                    sum_abs_level += abs(i2_quant_coeff);
                }
            }
 
            if((last_scan_pos - first_scan_pos) >= 4)
            {
                x_pos = (pu1_csb_table[first_scan_pos] & 0x3) + blk_col;
                y_pos = (pu1_csb_table[first_scan_pos] >> 2) + blk_row;
 
                quant_coeff_first = pi2_quant_coeffs[x_pos + (y_pos * trans_size)];
 
                sign_first_coeff = (quant_coeff_first > 0) ? 0 : 1;
 
                if(sign_first_coeff != (sum_abs_level & 0x1))
                {
                    WORD32 q_err;
                    WORD32 min_cost = MAX_INT;
                    WORD32 final_change = 0, cur_cost = 0, cur_change = 0;
                    WORD16 i2_tr_coeff;
                    WORD16 i2_iquant_coeff;
 
                    for(i = (last_cg == 1) ? last_scan_pos : 15; i >= 0; i--)
                    {
                        x_pos = (pu1_csb_table[i] & 0x3) + blk_col;
                        y_pos = (pu1_csb_table[i] >> 2) + blk_row;
 
                        i2_quant_coeff = pi2_quant_coeffs[x_pos + (y_pos * trans_size)];
                        i2_tr_coeff = pi2_tr_coeffs[x_pos + (y_pos * trans_size)];
                        i2_iquant_coeff = pi2_iquant_data[x_pos + (y_pos * dst_iq_strd)];
 
                        q_err = abs(i2_tr_coeff) - abs(i2_iquant_coeff);
 
                        if(i2_quant_coeff != 0)
                        {
                            cur_cost = -1 * SIGN(q_err) * q_err;
 
                            if(q_err <= 0)
                            {
                                if(i == first_scan_pos && abs(i2_quant_coeff) == 1)
                                {
                                    cur_cost = MAX_INT;
                                }
                            }
                        }
                        else
                        {
                            cur_cost = -q_err;
                            if(i < first_scan_pos)
                            {
                                WORD32 sign_bit = (i2_tr_coeff >= 0 ? 0 : 1);
 
                                if(sign_first_coeff != sign_bit)
                                {
                                    cur_cost = MAX_INT;
                                }
                            }
                        }
 
                        cur_change = (i2_quant_coeff == 0) ? 1 : (q_err > 0 ? 1 : -1);
 
                        if(cur_cost < min_cost)
                        {
                            min_cost = cur_cost;
                            final_change = cur_change;
                            best_pos = i;
                        }
                    }
                    if((i2_quant_coeff == 32767) || (i2_quant_coeff == -32768))
                    {
                        final_change = -1;
                    }
 
                    x_pos = (pu1_csb_table[best_pos] & 0x3) + blk_col;
                    y_pos = (pu1_csb_table[best_pos] >> 2) + blk_row;
                    i2_iquant_coeff = pi2_iquant_data[x_pos + (y_pos * dst_iq_strd)];
                    i2_tr_coeff = pi2_tr_coeffs[x_pos + (y_pos * trans_size)];
 
                    if(i2_tr_coeff >= 0)
                    {
                        pi2_quant_coeffs[x_pos + (y_pos * trans_size)] += final_change;
                    }
                    else
                    {
                        pi2_quant_coeffs[x_pos + (y_pos * trans_size)] -= final_change;
                    }
 
                    {
                        WORD32 i4_err1, i4_err2;
 
                        /*  Inverse Quantization    */
                        IQUANT(
                            pi2_iquant_data[y_pos * dst_iq_strd + x_pos],
                            pi2_quant_coeffs[y_pos * dst_q_strd + x_pos],
                            pi2_dequant_coeff[y_pos * trans_size + x_pos] *
                                g_ihevc_iquant_scales[qp_rem],
                            shift_iq,
                            qp_div);
 
                        i4_err1 = (i2_tr_coeff - i2_iquant_coeff);
                        i4_err1 = i4_err1 * i4_err1;
                        ssd_cost = ssd_cost - i4_err1;
                        i4_err2 = (i2_tr_coeff - pi2_iquant_data[y_pos * dst_iq_strd + x_pos]);
                        i4_err2 = i4_err2 * i4_err2;
                        ssd_cost = ssd_cost + i4_err2;
                    }
                }
            }
            if(last_cg == 1)
            {
                last_cg = 0;
            }
        }
    }
 
    ps_rdoq_sbh_params->i8_ssd_cost = ssd_cost;
}