672a19aa |
/*
* Range coder
* Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
672a19aa |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
672a19aa |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
672a19aa |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
672a19aa |
*/ |
115329f1 |
|
672a19aa |
/** |
ba87f080 |
* @file |
672a19aa |
* Range coder.
*/
|
98790382 |
#ifndef AVCODEC_RANGECODER_H
#define AVCODEC_RANGECODER_H |
699b3f99 |
|
99545457 |
#include <stdint.h> |
90558e84 |
|
245976da |
#include "libavutil/common.h" |
f0a3259f |
#include "libavutil/avassert.h" |
99545457 |
|
90558e84 |
typedef struct RangeCoder { |
672a19aa |
int low;
int range;
int outstanding_count;
int outstanding_byte;
uint8_t zero_state[256]; |
90558e84 |
uint8_t one_state[256]; |
672a19aa |
uint8_t *bytestream_start;
uint8_t *bytestream;
uint8_t *bytestream_end; |
90558e84 |
} RangeCoder; |
672a19aa |
void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
int ff_rac_terminate(RangeCoder *c);
void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
|
90558e84 |
static inline void renorm_encoder(RangeCoder *c)
{
// FIXME: optimize
while (c->range < 0x100) {
if (c->outstanding_byte < 0) {
c->outstanding_byte = c->low >> 8;
} else if (c->low <= 0xFF00) { |
672a19aa |
*c->bytestream++ = c->outstanding_byte; |
90558e84 |
for (; c->outstanding_count; c->outstanding_count--) |
672a19aa |
*c->bytestream++ = 0xFF; |
90558e84 |
c->outstanding_byte = c->low >> 8;
} else if (c->low >= 0x10000) { |
672a19aa |
*c->bytestream++ = c->outstanding_byte + 1; |
90558e84 |
for (; c->outstanding_count; c->outstanding_count--) |
672a19aa |
*c->bytestream++ = 0x00; |
90558e84 |
c->outstanding_byte = (c->low >> 8) & 0xFF;
} else { |
672a19aa |
c->outstanding_count++;
} |
115329f1 |
|
90558e84 |
c->low = (c->low & 0xFF) << 8; |
672a19aa |
c->range <<= 8;
}
}
|
90558e84 |
static inline int get_rac_count(RangeCoder *c)
{
int x = c->bytestream - c->bytestream_start + c->outstanding_count;
if (c->outstanding_byte >= 0) |
1e6b5700 |
x++; |
90558e84 |
return 8 * x - av_log2(c->range); |
1e6b5700 |
}
|
90558e84 |
static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit)
{
int range1 = (c->range * (*state)) >> 8; |
672a19aa |
|
f0a3259f |
av_assert2(*state);
av_assert2(range1 < c->range);
av_assert2(range1 > 0); |
90558e84 |
if (!bit) { |
672a19aa |
c->range -= range1; |
90558e84 |
*state = c->zero_state[*state];
} else {
c->low += c->range - range1; |
672a19aa |
c->range = range1; |
90558e84 |
*state = c->one_state[*state]; |
672a19aa |
} |
115329f1 |
|
672a19aa |
renorm_encoder(c);
}
|
90558e84 |
static inline void refill(RangeCoder *c)
{
if (c->range < 0x100) { |
672a19aa |
c->range <<= 8; |
90558e84 |
c->low <<= 8;
if (c->bytestream < c->bytestream_end)
c->low += c->bytestream[0]; |
672a19aa |
c->bytestream++;
}
}
|
90558e84 |
static inline int get_rac(RangeCoder *c, uint8_t *const state)
{
int range1 = (c->range * (*state)) >> 8; |
154e30f6 |
int av_unused one_mask; |
115329f1 |
|
672a19aa |
c->range -= range1;
#if 1 |
90558e84 |
if (c->low < c->range) {
*state = c->zero_state[*state]; |
672a19aa |
refill(c);
return 0; |
90558e84 |
} else {
c->low -= c->range;
*state = c->one_state[*state]; |
672a19aa |
c->range = range1;
refill(c);
return 1;
}
#else |
90558e84 |
one_mask = (c->range - c->low - 1) >> 31; |
115329f1 |
|
90558e84 |
c->low -= c->range & one_mask; |
672a19aa |
c->range += (range1 - c->range) & one_mask; |
115329f1 |
|
90558e84 |
*state = c->zero_state[(*state) + (256 & one_mask)]; |
115329f1 |
|
672a19aa |
refill(c);
|
90558e84 |
return one_mask & 1; |
672a19aa |
#endif
}
|
98790382 |
#endif /* AVCODEC_RANGECODER_H */ |