lagarith: Convert to the new bitstream reader
[libav.git] / libavcodec / lagarithrac.c
CommitLineData
d267b339
CEH
1/*
2 * Lagarith range decoder
3 * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
4 * Copyright (c) 2009 David Conrad
5 *
2912e87a 6 * This file is part of Libav.
d267b339 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
d267b339
CEH
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
d267b339
CEH
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
d267b339
CEH
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
f25a2ece 24 * @file
d267b339
CEH
25 * Lagarith range decoder
26 * @author Nathan Caldwell
27 * @author David Conrad
28 */
29
6fad5abc 30#include "bitstream.h"
d267b339
CEH
31#include "lagarithrac.h"
32
6fad5abc 33void ff_lag_rac_init(lag_rac *l, BitstreamContext *bc, int length)
d267b339 34{
0a82f527 35 int i, j, left;
d267b339
CEH
36
37 /* According to reference decoder "1st byte is garbage",
6fad5abc 38 * however, it gets skipped by the call to bitstream_align()
d267b339 39 */
6fad5abc
AH
40 bitstream_align(bc);
41 left = bitstream_bits_left(bc) >> 3;
d267b339 42 l->bytestream_start =
6fad5abc 43 l->bytestream = bc->buffer + bitstream_tell(bc) / 8;
0a82f527 44 l->bytestream_end = l->bytestream_start + FFMIN(length, left);
d267b339
CEH
45
46 l->range = 0x80;
47 l->low = *l->bytestream >> 1;
0b699920 48 l->hash_shift = FFMAX(l->scale, 8) - 8;
d267b339
CEH
49
50 for (i = j = 0; i < 256; i++) {
51 unsigned r = i << l->hash_shift;
52 while (l->prob[j + 1] <= r)
53 j++;
54 l->range_hash[i] = j;
55 }
56
57 /* Add conversion factor to hash_shift so we don't have to in lag_get_rac. */
58 l->hash_shift += 23;
59}