Fix range check on j in hash calculation.
[FFMpeg-mirror/lagarith.git] / libavcodec / lagrange.c
blob8c6fa2d131f9acd22176d975a58cd4f00e6c4773
1 /*
2 * Lagarith range decoder
3 * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
4 * Copyright (c) 2009 David Conrad
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
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.
13 * FFmpeg is distributed in the hope that it will be useful,
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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 /**
24 * @file libavcodec/lagrange.c
25 * Lagarith range decoder
26 * @author Nathan Caldwell
27 * @author David Conrad
30 #include "get_bits.h"
31 #include "lagrange.h"
33 void lag_rac_init(lag_rac *l, GetBitContext *gb, int length)
35 int i, j;
37 /* According to reference decoder "1st byte is garbage",
38 * however, it gets skipped by the call to align_get_bits()
40 align_get_bits(gb);
41 l->bytestream_start =
42 l->bytestream = gb->buffer + get_bits_count(gb) / 8;
43 l->bytestream_end = l->bytestream_start + length;
45 l->range = 0x80;
46 l->low = *l->bytestream >> 1;
47 l->hash_shift = FFMAX(l->scale - 8, 0);
49 for (i = j = 0; i < 256; i++) {
50 unsigned r = i << l->hash_shift;
51 while (l->prob[j + 1] <= r && j < 255)
52 j++;
53 l->range_hash[i] = j;