int main(void) { RangeCoder c; int i; uint8_t state[10]; AVLFG prng; av_lfg_init(&prng, 1); ff_init_range_encoder(&c, b, SIZE); ff_build_rac_states(&c, (1LL << 32) / 20, 128 + 64 + 32 + 16); memset(state, 128, sizeof(state)); for (i = 0; i < SIZE; i++) r[i] = av_lfg_get(&prng) % 7; for (i = 0; i < SIZE; i++) put_rac(&c, state, r[i] & 1); ff_rac_terminate(&c); ff_init_range_decoder(&c, b, SIZE); memset(state, 128, sizeof(state)); for (i = 0; i < SIZE; i++) if ((r[i] & 1) != get_rac(&c, state)) { av_log(NULL, AV_LOG_ERROR, "rac failure at %d\n", i); return 1; } return 0; }
static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed) { if (get_rac(c, state + 0)) return 0; else { int i, e, a; e = 0; while (get_rac(c, state + 1 + FFMIN(e, 9))) // 1..10 e++; a = 1; for (i = e - 1; i >= 0; i--) a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21 return (a ^ e) - e; } }
int main(void){ RangeCoder c; uint8_t b[9*SIZE]; uint8_t r[9*SIZE]; int i; uint8_t state[10]= {0}; AVLFG prng; av_lfg_init(&prng, 1); ff_init_range_encoder(&c, b, SIZE); ff_build_rac_states(&c, 0.05*(1LL<<32), 128+64+32+16); memset(state, 128, sizeof(state)); for(i=0; i<SIZE; i++){ r[i] = av_lfg_get(&prng) % 7; } for(i=0; i<SIZE; i++){ START_TIMER put_rac(&c, state, r[i]&1); STOP_TIMER("put_rac") } ff_rac_terminate(&c); ff_init_range_decoder(&c, b, SIZE); memset(state, 128, sizeof(state)); for(i=0; i<SIZE; i++){ START_TIMER if( (r[i]&1) != get_rac(&c, state) ) av_log(NULL, AV_LOG_DEBUG, "rac failure at %d\n", i); STOP_TIMER("get_rac") } return 0; }
static av_always_inline void decode_line(FFV1Context *s, int w, int16_t *sample[2], int plane_index, int bits) { PlaneContext *const p = &s->plane[plane_index]; RangeCoder *const c = &s->c; int x; int run_count = 0; int run_mode = 0; int run_index = s->run_index; if (s->slice_coding_mode == 1) { int i; for (x = 0; x < w; x++) { int v = 0; for (i=0; i<bits; i++) { uint8_t state = 128; v += v + get_rac(c, &state); } sample[1][x] = v; } return; } for (x = 0; x < w; x++) { int diff, context, sign; context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x); if (context < 0) { context = -context; sign = 1; } else sign = 0; av_assert2(context < p->context_count); if (s->ac) { diff = get_symbol_inline(c, p->state[context], 1); } else { if (context == 0 && run_mode == 0) run_mode = 1; if (run_mode) { if (run_count == 0 && run_mode == 1) { if (get_bits1(&s->gb)) { run_count = 1 << ff_log2_run[run_index]; if (x + run_count <= w) run_index++; } else { if (ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]); else run_count = 0; if (run_index) run_index--; run_mode = 2; } } run_count--; if (run_count < 0) { run_mode = 0; run_count = 0; diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits); if (diff >= 0) diff++; } else diff = 0; } else diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits); ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n", run_count, run_index, run_mode, x, get_bits_count(&s->gb)); } if (sign) diff = -diff; sample[1][x] = av_mod_uintp2(predict(sample[1] + x, sample[0] + x) + diff, bits); } s->run_index = run_index; }
static av_always_inline int RENAME(decode_line)(FFV1Context *s, int w, TYPE *sample[2], int plane_index, int bits) { PlaneContext *const p = &s->plane[plane_index]; RangeCoder *const c = &s->c; int x; int run_count = 0; int run_mode = 0; int run_index = s->run_index; if (is_input_end(s)) return AVERROR_INVALIDDATA; if (s->slice_coding_mode == 1) { int i; for (x = 0; x < w; x++) { int v = 0; for (i=0; i<bits; i++) { uint8_t state = 128; v += v + get_rac(c, &state); } sample[1][x] = v; } return 0; } for (x = 0; x < w; x++) { int diff, context, sign; if (!(x & 1023)) { if (is_input_end(s)) return AVERROR_INVALIDDATA; } context = RENAME(get_context)(p, sample[1] + x, sample[0] + x, sample[1] + x); if (context < 0) { context = -context; sign = 1; } else sign = 0; av_assert2(context < p->context_count); if (s->ac != AC_GOLOMB_RICE) { diff = get_symbol_inline(c, p->state[context], 1); } else { if (context == 0 && run_mode == 0) run_mode = 1; if (run_mode) { if (run_count == 0 && run_mode == 1) { if (get_bits1(&s->gb)) { run_count = 1 << ff_log2_run[run_index]; if (x + run_count <= w) run_index++; } else { if (ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]); else run_count = 0; if (run_index) run_index--; run_mode = 2; } } while (run_count > 1 && w-x > 1) { sample[1][x] = RENAME(predict)(sample[1] + x, sample[0] + x); x++; run_count--; } run_count--; if (run_count < 0) { run_mode = 0; run_count = 0; diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits); if (diff >= 0) diff++; } else diff = 0; } else diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits); ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n", run_count, run_index, run_mode, x, get_bits_count(&s->gb)); } if (sign) diff = -(unsigned)diff; sample[1][x] = av_mod_uintp2(RENAME(predict)(sample[1] + x, sample[0] + x) + (SUINT)diff, bits); } s->run_index = run_index; return 0; }