Skip to content

Commit 867ed70

Browse files
authored
Safe and fast VLC decoding (#111)
* intermediate implementation Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> * speed-up of AC parsing Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> * improved speed Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> * refactoring more Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> * refactoring more Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> * refactored more Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> * refactoring and speed-up Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> * merging Signed-off-by: Kwang Pyo Choi <kp5.choi@samsung.com> --------- Signed-off-by: kp5.choi@samsung.com <kp5.choi@samsung.com> Signed-off-by: Kwang Pyo Choi <kp5.choi@samsung.com> Signed-off-by: kpchoi <kp5.choi@samsung.com>
1 parent 43f0d16 commit 867ed70

File tree

10 files changed

+1039
-1056
lines changed

10 files changed

+1039
-1056
lines changed

src/oapv.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static double enc_block_rdo_slow(oapve_ctx_t *ctx, oapve_core_t *core, int log2_
375375

376376
int best_cost = INT_MAX;
377377
int zero_dist = 0;
378-
const u16 *scanp = oapv_tbl_scan;
378+
const u8 *scanp = oapv_tbl_scan;
379379
const int map_idx_diff[15] = { 0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7 };
380380
double lambda = 0.57 * pow(2.0, (qp - 12.0) / 3.0);
381381

@@ -525,7 +525,7 @@ static double enc_block_rdo_placebo(oapve_ctx_t* ctx, oapve_core_t* core, int lo
525525
s16* best_recon = core->coef_rec;
526526

527527
double best_cost = INT_MAX;
528-
const u16* scanp = oapv_tbl_scan;
528+
const u8* scanp = oapv_tbl_scan;
529529

530530
oapv_mcpy(org, core->coef, sizeof(s16) * OAPV_BLK_D);
531531
oapv_trans(ctx, core->coef, log2_w, log2_h, bit_depth);
@@ -752,8 +752,8 @@ static int enc_tile_comp(oapv_bs_t *bs, oapve_tile_t *tile, oapve_ctx_t *ctx, oa
752752
ctx->fn_imgb_to_blk[c](o16, OAPV_BLK_W, OAPV_BLK_H, s_org, blk_x, (OAPV_BLK_W << 1), core->coef, ctx->bit_depth);
753753

754754
ctx->fn_enc_blk(ctx, core, OAPV_LOG2_BLK_W, OAPV_LOG2_BLK_H, c);
755-
oapve_vlc_dc_coeff(ctx, core, bs, core->dc_diff, c);
756-
oapve_vlc_ac_coeff(ctx, core, bs, core->coef, 0, c);
755+
oapve_vlc_dc_coef(ctx, core, bs, core->dc_diff, c);
756+
oapve_vlc_ac_coef(ctx, core, bs, core->coef, 0, c);
757757
DUMP_COEF(core->coef, OAPV_BLK_D, blk_x, blk_y, c);
758758

759759
if(rec != NULL) {
@@ -1613,12 +1613,15 @@ static int dec_tile_comp(oapvd_tile_t *tile, oapvd_ctx_t *ctx, oapvd_core_t *cor
16131613
for(mb_x = le; mb_x < ri; mb_x += mb_w) {
16141614
for(blk_y = mb_y; blk_y < (mb_y + mb_h); blk_y += OAPV_BLK_H) {
16151615
for(blk_x = mb_x; blk_x < (mb_x + mb_w); blk_x += OAPV_BLK_W) {
1616+
// clear coefficient buffers in a macroblock
1617+
oapv_mset_x128(core->coef, 0, sizeof(s16)*OAPV_MB_D);
1618+
16161619
// parse DC coefficient
1617-
ret = oapvd_vlc_dc_coeff(ctx, core, bs, &core->dc_diff, c);
1620+
ret = oapvd_vlc_dc_coef(bs, &core->dc_diff, &core->kparam_dc[c]);
16181621
oapv_assert_rv(OAPV_SUCCEEDED(ret), ret);
16191622

16201623
// parse AC coefficient
1621-
ret = oapvd_vlc_ac_coeff(ctx, core, bs, core->coef, c);
1624+
ret = oapvd_vlc_ac_coef(bs, core->coef, &core->kparam_ac[c]);
16221625
oapv_assert_rv(OAPV_SUCCEEDED(ret), ret);
16231626
DUMP_COEF(core->coef, OAPV_BLK_D, blk_x, blk_y, c);
16241627

@@ -1657,8 +1660,8 @@ static int dec_tile(oapvd_core_t *core, oapvd_tile_t *tile)
16571660
int dq_scale = oapv_tbl_dq_scale[core->qp[c] % 6];
16581661
core->dq_shift[c] = ctx->bit_depth - 2 - (core->qp[c] / 6);
16591662

1660-
core->prev_dc_ctx[c] = 20;
1661-
core->prev_1st_ac_ctx[c] = 0;
1663+
core->kparam_dc[c] = OAPV_KPARAM_DC_MAX;
1664+
core->kparam_ac[c] = OAPV_KPARAM_AC_MIN;
16621665
core->prev_dc[c] = 0;
16631666

16641667
midx = 0;

src/oapv_def.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@
6363
#define N_C 4 /* maximum number of color component */
6464

6565
#define OAPV_VLC_TREE_LEVEL 2
66-
#define OAPV_MIN_DC_LEVEL_CTX 0
67-
#define OAPV_MAX_DC_LEVEL_CTX 5
68-
#define OAPV_MIN_AC_LEVEL_CTX 0
69-
#define OAPV_MAX_AC_LEVEL_CTX 4
66+
#define OAPV_KPARAM_DC_MIN 0
67+
#define OAPV_KPARAM_DC_MAX 5
68+
#define OAPV_KPARAM_AC_MIN 0
69+
#define OAPV_KPARAM_AC_MAX 4
70+
#define OAPV_KPARAM_RUN_MIN 0
71+
#define OAPV_KPARAM_RUN_MAX 2
7072

7173
/* Maximum transform dynamic range (excluding sign bit) */
7274
#define MAX_TX_DYNAMIC_RANGE 15
@@ -352,8 +354,8 @@ struct oapvd_core {
352354
ALIGNED_16(s16 coef[OAPV_MB_D]);
353355
s16 q_mat[N_C][OAPV_BLK_D];
354356

355-
int prev_dc_ctx[N_C];
356-
int prev_1st_ac_ctx[N_C];
357+
int kparam_dc[N_C];
358+
int kparam_ac[N_C];
357359
int prev_dc[N_C];
358360
int dc_diff; /* DC difference, which is represented in 17 bits */
359361
/* and coded as abs_dc_coeff_diff and sign_dc_coeff_diff */

src/oapv_metadata.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ int oapvm_get(oapvm_t mid, int group_id, int type, void **data, int *size, unsig
248248

249249
*data = mdp->pld_data;
250250
*size = mdp->pld_size;
251-
252251
return OAPV_OK;
253252

254253
ERR:

src/oapv_metadata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ struct oapv_md_ud {
105105
u8 *undefined_data_payload;
106106
};
107107

108-
#endif /* _OAPV_METADATA_H_ */
108+
#endif /* _OAPV_METADATA_H_ */

src/oapv_tbl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const s8 oapv_tbl_tm8[8][8] = {
8181

8282
const int oapv_tbl_dq_scale[6] = {40, 45, 51, 57, 64, 71};
8383

84-
const u16 oapv_tbl_scan[OAPV_BLK_D] = {
84+
const u8 oapv_tbl_scan[OAPV_BLK_D] = {
8585
0, 1, 8, 16, 9, 2, 3, 10,
8686
17, 24, 32, 25, 18, 11, 4, 5,
8787
12, 19, 26, 33, 40, 48, 41, 34,
@@ -837,4 +837,4 @@ int oapv_itrans_diff[64][64] = {
837837
-1228, 3413, -5120, 6075, -6075, 5120, -3413, 1228
838838
}
839839
};
840-
// clang-format on
840+
// clang-format on

src/oapv_tbl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
extern const u8 oapv_tbl_log2[257];
3838
extern const s8 oapv_tbl_tm8[8][8];
3939
extern const int oapv_tbl_dq_scale[6];
40-
extern const u16 oapv_tbl_scan[OAPV_BLK_D];
40+
extern const u8 oapv_tbl_scan[OAPV_BLK_D];
4141
extern const u32 CODE_LUT_100[100][5][2];
4242
extern int oapv_itrans_diff[64][64];
4343

src/oapv_tq.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ int oapve_rdoq(oapve_core_t* core, s16 *src_coef, s16 *dst_coef, int log2_cuw, i
118118
u8 qp = core->qp[ch_type];
119119
const s32 tr_shift = MAX_TX_DYNAMIC_RANGE - bit_depth - 3;
120120
const u32 max_num_coef = 1 << (log2_cuw + log2_cuh);
121-
const u16 *scan = oapv_tbl_scan;
121+
const u8 *scan = oapv_tbl_scan;
122122
const int q_bits = QUANT_SHIFT + tr_shift + (qp / 6);
123123
int nnz = 0;
124124
u32 sum_all = 0;
@@ -163,7 +163,7 @@ int oapve_rdoq(oapve_core_t* core, s16 *src_coef, s16 *dst_coef, int log2_cuw, i
163163
return nnz;
164164
}
165165

166-
rice_level = oapv_clip3(OAPV_MIN_DC_LEVEL_CTX, OAPV_MAX_DC_LEVEL_CTX, core->prev_dc_ctx[ch_type] >> 1);
166+
rice_level = oapv_clip3(OAPV_KPARAM_DC_MIN, OAPV_KPARAM_DC_MAX, core->prev_dc_ctx[ch_type] >> 1);
167167
rice_run = prev_run / 4;
168168
if(rice_run > 2) {
169169
rice_run = 2;
@@ -185,7 +185,7 @@ int oapve_rdoq(oapve_core_t* core, s16 *src_coef, s16 *dst_coef, int log2_cuw, i
185185
err1 = (double)tmp_level_double[blk_pos] * core->err_scale_tbl[ch_type][blk_pos];
186186
uncoded_cost = err1 * err1;
187187

188-
rice_level = oapv_clip3(OAPV_MIN_DC_LEVEL_CTX, OAPV_MAX_DC_LEVEL_CTX, core->prev_dc_ctx[ch_type] >> 1);
188+
rice_level = oapv_clip3(OAPV_KPARAM_DC_MIN, OAPV_KPARAM_DC_MAX, core->prev_dc_ctx[ch_type] >> 1);
189189

190190
for(tmp_level = max_level; tmp_level >= min_level; tmp_level--) {
191191
if(tmp_level == 0) {
@@ -197,7 +197,7 @@ int oapve_rdoq(oapve_core_t* core, s16 *src_coef, s16 *dst_coef, int log2_cuw, i
197197
double curr_run_bit_cost = oapve_vlc_get_run_cost(63, 0, lambda);
198198
double curr_bit_cost = oapve_vlc_get_level_cost(oapv_abs(tmp_level - core->prev_dc[ch_type]), rice_level, lambda) + curr_run_bit_cost;
199199
double curr_cost = curr_dist + curr_bit_cost;
200-
200+
201201
if(curr_cost < best_cost) {
202202
best_level = tmp_level;
203203
base_dist = curr_dist;
@@ -231,7 +231,7 @@ int oapve_rdoq(oapve_core_t* core, s16 *src_coef, s16 *dst_coef, int log2_cuw, i
231231
}
232232
rice_level = prev_level >> 2;
233233
if(rice_level > 4) {
234-
rice_level = OAPV_MAX_AC_LEVEL_CTX;
234+
rice_level = OAPV_KPARAM_AC_MAX;
235235
}
236236

237237
for(tmp_level = max_level; tmp_level >= min_level; tmp_level--) {

src/oapv_util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,4 @@ void oapv_dump_delete0()
436436
fclose(oapv_fp_dump);
437437
}
438438
}
439-
#endif
439+
#endif

0 commit comments

Comments
 (0)