/*
 * Range coder
 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

typedef struct xlsc_rac_t
{
    int low;
    int range;
    int outstanding_count;
    int outstanding_byte;
    uint8_t transition[2][256];
    uint8_t *bytestream_start;
    uint8_t *bytestream;
    uint8_t *bytestream_end;
} xlsc_rac_t;

void xlsc_init_range_encoder(xlsc_rac_t *c, uint8_t *buf, int buf_size);
void xlsc_init_range_decoder(xlsc_rac_t *c, const uint8_t *buf, int buf_size);
int xlsc_rac_terminate(xlsc_rac_t *c);
void xlsc_build_rac_states(xlsc_rac_t *c, int factor, int max_p);

static inline void renorm_encoder( xlsc_rac_t *c )
{
    if( c->range < 0x100 )
    {
        if(c->outstanding_byte < 0)
        {
            c->outstanding_byte = c->low>>8;
        }
        else if( c->low <= 0xFF00 )
        {
            *c->bytestream++ = c->outstanding_byte;
            for(;c->outstanding_count; c->outstanding_count--)
                *c->bytestream++ = 0xFF;
            c->outstanding_byte = c->low>>8;
        }
        else if( c->low >= 0x10000 )
        {
            *c->bytestream++ = c->outstanding_byte + 1;
            for(;c->outstanding_count; c->outstanding_count--)
                *c->bytestream++ = 0x00;
            c->outstanding_byte = (c->low>>8) & 0xFF;
        }
        else
        {
            c->outstanding_count++;
        }

        c->low = (c->low & 0xFF)<<8;
        c->range <<= 8;
    }
}

static inline void put_rac( xlsc_rac_t *c, uint8_t * const state, int bit )
{
    int range1 = (c->range * (*state)) >> 8;

    if( !bit )
    {
        c->range -= range1;
        *state = c->transition[0][*state];
    }
    else
    {
        c->low += c->range - range1;
        c->range = range1;
        *state = c->transition[1][*state];
    }

    renorm_encoder(c);
}

static inline void put_rac_bypass( xlsc_rac_t *c, int bit )
{
    int range1 = c->range >> 1;

    if( !bit )
    {
        c->range -= range1;
    }
    else
    {
        c->low += c->range - range1;
        c->range = range1;
    }

    renorm_encoder(c);
}

static inline void refill( xlsc_rac_t *c )
{
    if( c->range < 0x100 )
    {
        c->range <<= 8;
        c->low <<= 8;
        if(c->bytestream < c->bytestream_end)
            c->low+= c->bytestream[0];
        c->bytestream++;
    }
}

static inline int get_rac( xlsc_rac_t *c, uint8_t * const state )
{
    int range1 = (c->range * (*state)) >> 8;
    c->range -= range1;

    if( c->low < c->range )
    {
        *state = c->transition[0][*state];
        refill(c);
        return 0;
    }
    else
    {
        c->low -= c->range;
        *state = c->transition[1][*state];
        c->range = range1;
        refill(c);
        return 1;
    }
}

static inline int get_rac_bypass( xlsc_rac_t *c )
{
    int range1 = c->range >> 1;
    c->range -= range1;

    if( c->low < c->range )
    {
        refill(c);
        return 0;
    }
    else
    {
        c->low -= c->range;
        c->range = range1;
        refill(c);
        return 1;
    }
}

static inline int size_rac( uint8_t * const state, int bit )
{
    return xlsc_rac_entropy[bit ? *state : 256-*state];
}

static inline void transition_rac( xlsc_rac_t *c, uint8_t * const state, int bit )
{
    *state = c->transition[bit][*state];
}
