Skip to content

Commit 454ec85

Browse files
authored
Merge pull request AcademySoftwareFoundation#117 from AcademySoftwareFoundation/safe_and_fast_vlc
Safe and fast VLC code
2 parents 7f07eaa + 10c0fc5 commit 454ec85

File tree

10 files changed

+290
-609
lines changed

10 files changed

+290
-609
lines changed

src/oapv.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,8 @@ static int enc_tile_comp(oapv_bs_t *bs, oapve_tile_t *tile, oapve_ctx_t *ctx, oa
795795
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);
796796

797797
ctx->fn_enc_blk(ctx, core, OAPV_LOG2_BLK_W, OAPV_LOG2_BLK_H, c);
798-
oapve_vlc_dc_coef(ctx, core, bs, core->dc_diff, c);
799-
oapve_vlc_ac_coef(ctx, core, bs, core->coef, 0, c);
798+
oapve_vlc_dc_coef(bs, core->dc_diff, &core->kparam_dc[c]);
799+
oapve_vlc_ac_coef(bs, core->coef, &core->kparam_ac[c]);
800800
DUMP_COEF(core->coef, OAPV_BLK_D, blk_x, blk_y, c);
801801

802802
if(rec != NULL) {
@@ -867,8 +867,8 @@ static int enc_tile(oapve_ctx_t *ctx, oapve_core_t *core, oapve_tile_t *tile)
867867
}
868868

869869
for(int c = 0; c < ctx->num_comp; c++) {
870-
core->prev_dc_ctx[c] = 20;
871-
core->prev_1st_ac_ctx[c] = 0;
870+
core->kparam_dc[c] = OAPV_KPARAM_DC_MAX;
871+
core->kparam_ac[c] = OAPV_KPARAM_AC_MIN;
872872
core->prev_dc[c] = 0;
873873

874874
int tc, s_org, s_rec;
@@ -913,7 +913,6 @@ static int enc_tile(oapve_ctx_t *ctx, oapve_core_t *core, oapve_tile_t *tile)
913913
tile->bs_size = bs_size;
914914

915915
oapv_bs_t bs_th;
916-
bs_th.is_bin_count = 0;
917916
oapv_bsw_init(&bs_th, tile->bs_buf, tile->bs_size, NULL);
918917
tile->tile_size = bs_size - OAPV_TILE_SIZE_LEN;
919918

src/oapv_bs.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ void oapv_bsw_init(oapv_bs_t *bs, u8 *buf, int size, oapv_bs_fn_flush_t fn_flush
6262
bs->code = 0;
6363
bs->leftbits = 32;
6464
bs->fn_flush = (fn_flush == NULL ? bsw_flush : fn_flush);
65-
bs->is_bin_count = 0;
66-
bs->bin_count = 0;
6765
}
6866

6967
void oapv_bsw_deinit(oapv_bs_t *bs)
@@ -99,11 +97,6 @@ int oapv_bsw_write1(oapv_bs_t *bs, int val)
9997
{
10098
oapv_assert(bs);
10199

102-
if(bs->is_bin_count) {
103-
bs->bin_count++;
104-
return 0;
105-
}
106-
107100
bs->leftbits--;
108101
bs->code |= ((val & 0x1) << bs->leftbits);
109102

@@ -124,11 +117,6 @@ int oapv_bsw_write(oapv_bs_t *bs, u32 val, int len) /* len(1 ~ 32) */
124117

125118
oapv_assert(bs);
126119

127-
if(bs->is_bin_count) {
128-
bs->bin_count += len;
129-
return 0;
130-
}
131-
132120
leftbits = bs->leftbits;
133121
val <<= (32 - len);
134122
bs->code |= (val >> (32 - leftbits));

src/oapv_bs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ struct oapv_bs {
4747
oapv_bs_fn_flush_t fn_flush; // function pointer for flush operation
4848
int ndata[4]; // arbitrary data, if needs
4949
void *pdata[4]; // arbitrary address, if needs
50-
char is_bin_count;
51-
u32 bin_count;
5250
};
5351

5452
///////////////////////////////////////////////////////////////////////////////

src/oapv_def.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include "oapv.h"
4444
#include "oapv_port.h"
45+
#include "oapv_bs.h"
4546
#include "oapv_tpool.h"
4647

4748
/* oapv encoder magic code */
@@ -224,11 +225,13 @@ typedef struct oapve_rc_tile {
224225
struct oapve_core {
225226
ALIGNED_16(s16 coef[OAPV_BLK_D]);
226227
ALIGNED_16(s16 coef_rec[OAPV_BLK_D]);
227-
oapve_ctx_t *ctx;
228-
int prev_dc_ctx[N_C];
229-
int prev_1st_ac_ctx[N_C];
230-
int tile_idx;
228+
229+
int kparam_dc[N_C];
230+
int kparam_ac[N_C];
231231
int prev_dc[N_C];
232+
233+
int tile_idx;
234+
232235
int dc_diff; /* DC difference, which is represented in 17 bits */
233236
/* and coded as abs_dc_coeff_diff and sign_dc_coeff_diff */
234237
int qp[N_C]; // QPs for Y, Cb(U), Cr(V)
@@ -238,12 +241,12 @@ struct oapve_core {
238241
s16 q_mat_dec[N_C][OAPV_BLK_D];
239242
double err_scale_tbl[N_C][OAPV_BLK_D];
240243
int thread_idx;
244+
245+
oapve_ctx_t *ctx;
241246
/* platform specific data, if needed */
242247
void *pf;
243248
};
244249

245-
#include "oapv_bs.h"
246-
247250
typedef struct oapve_tile oapve_tile_t;
248251
struct oapve_tile {
249252
oapv_th_t th;

src/oapv_tbl.c

Lines changed: 101 additions & 101 deletions
Large diffs are not rendered by default.

src/oapv_tbl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929
* POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
#ifndef _OAPV_TBL_H_
33-
#define _OAPV_TBL_H_
32+
#ifndef __OAPV_TBL_H_34243243243342435479875463453543543542432432__
33+
#define __OAPV_TBL_H_34243243243342435479875463453543543542432432__
3434

3535
#include "oapv_def.h"
3636

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];
4040
extern const u8 oapv_tbl_scan[OAPV_BLK_D];
41-
extern const u32 CODE_LUT_100[100][5][2];
4241
extern int oapv_itrans_diff[64][64];
42+
extern const u16 oapve_tbl_vlc_code[100][5][2];
4343

44-
#endif /* _OAPV_TBL_H_ */
44+
#endif /* __OAPV_TBL_H_34243243243342435479875463453543543542432432__ */

src/oapv_tq.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ int oapve_rdoq(oapve_core_t* core, s16 *src_coef, s16 *dst_coef, int log2_cuw, i
127127
s32 rice_level = 0;
128128
s32 rice_run = 0;
129129
s32 prev_run = 0;
130-
s32 prev_level = core->prev_1st_ac_ctx[ch_type];
130+
int k_ac = core->kparam_ac[ch_type];
131131
s16 tmp_coef[OAPV_BLK_D];
132132
s64 tmp_level_double[OAPV_BLK_D];
133133
double best_cost = 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_KPARAM_DC_MIN, OAPV_KPARAM_DC_MAX, core->prev_dc_ctx[ch_type] >> 1);
166+
rice_level = core->kparam_dc[ch_type];
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_KPARAM_DC_MIN, OAPV_KPARAM_DC_MAX, core->prev_dc_ctx[ch_type] >> 1);
188+
rice_level = core->kparam_dc[ch_type];
189189

190190
for(tmp_level = max_level; tmp_level >= min_level; tmp_level--) {
191191
if(tmp_level == 0) {
@@ -229,10 +229,7 @@ int oapve_rdoq(oapve_core_t* core, s16 *src_coef, s16 *dst_coef, int log2_cuw, i
229229
if(rice_run > 2) {
230230
rice_run = 2;
231231
}
232-
rice_level = prev_level >> 2;
233-
if(rice_level > 4) {
234-
rice_level = OAPV_KPARAM_AC_MAX;
235-
}
232+
rice_level = k_ac;
236233

237234
for(tmp_level = max_level; tmp_level >= min_level; tmp_level--) {
238235
if(tmp_level == 0) {
@@ -264,7 +261,7 @@ int oapve_rdoq(oapve_core_t* core, s16 *src_coef, s16 *dst_coef, int log2_cuw, i
264261

265262
if(dst_coef[blk_pos]) {
266263
prev_run = run;
267-
prev_level = abs(dst_coef[blk_pos]);
264+
k_ac = KPARAM_AC(oapv_abs(dst_coef[blk_pos]));
268265
run = 0;
269266
nnz++;
270267
}

src/oapv_util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
// operation: if(val < 0) return 1, else return 0
6363
#define oapv_get_sign16(val) (((val) >> 15) & 1)
6464

65+
// macro to get a sign from a 32-bit value.
66+
// operation: if(val < 0) return 1, else return 0
67+
#define oapv_get_sign32(val) (((val) >> 31) & 1)
68+
6569
// macro to set sign to a 16-bit value.
6670
// operation: if(sign == 0) return val, else if(sign == 1) return -val
6771
#define oapv_set_sign16(val, sign) (((val) ^ ((s16)((sign) << 15) >> 15)) + (sign))

0 commit comments

Comments
 (0)