FFmpeg
lagarithrac.h
Go to the documentation of this file.
1 /*
2  * Lagarith range decoder
3  * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
4  * Copyright (c) 2009 David Conrad
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 /**
24  * @file
25  * Lagarith range decoder
26  * @author Nathan Caldwell
27  * @author David Conrad
28  */
29 
30 #ifndef AVCODEC_LAGARITHRAC_H
31 #define AVCODEC_LAGARITHRAC_H
32 
33 #include <stdint.h>
34 #include "libavutil/intreadwrite.h"
35 #include "avcodec.h"
36 #include "get_bits.h"
37 
38 typedef struct lag_rac {
40  unsigned low;
41  unsigned range;
42  unsigned scale; /**< Number of bits of precision in range. */
43  unsigned hash_shift; /**< Number of bits to shift to calculate hash for radix search. */
44 
45  const uint8_t *bytestream_start; /**< Start of input bytestream. */
46  const uint8_t *bytestream; /**< Current position in input bytestream. */
47  const uint8_t *bytestream_end; /**< End position of input bytestream. */
48 
49  int overread;
50 #define MAX_OVERREAD 4
51 
52  uint32_t prob[258]; /**< Table of cumulative probability for each symbol. */
53  uint8_t range_hash[1024]; /**< Hash table mapping upper byte to approximate symbol. */
54 } lag_rac;
55 
56 void ff_lag_rac_init(lag_rac *l, GetBitContext *gb, int length);
57 
58 /* TODO: Optimize */
59 static inline void lag_rac_refill(lag_rac *l)
60 {
61  while (l->range <= 0x800000) {
62  l->low <<= 8;
63  l->range <<= 8;
64  l->low |= 0xff & (AV_RB16(l->bytestream) >> 1);
65  if (l->bytestream < l->bytestream_end)
66  l->bytestream++;
67  else
68  l->overread++;
69  }
70 }
71 
72 /**
73  * Decode a single byte from the compressed plane described by *l.
74  * @param l pointer to lag_rac for the current plane
75  * @return next byte of decoded data
76  */
77 static inline uint8_t lag_get_rac(lag_rac *l)
78 {
79  unsigned range_scaled, low_scaled;
80  int val;
81 
82  lag_rac_refill(l);
83 
84  range_scaled = l->range >> l->scale;
85 
86  if (l->low < range_scaled * l->prob[255]) {
87  /* val = 0 is frequent enough to deserve a shortcut */
88  if (l->low < range_scaled * l->prob[1]) {
89  val = 0;
90  } else {
91  low_scaled = l->low / (range_scaled<<(l->hash_shift));
92 
93  val = l->range_hash[low_scaled];
94  while (l->low >= range_scaled * l->prob[val + 1])
95  val++;
96  }
97 
98  l->range = range_scaled * (l->prob[val + 1] - l->prob[val]);
99  } else {
100  val = 255;
101  l->range -= range_scaled * l->prob[255];
102  }
103 
104  if (!l->range)
105  l->range = 0x80;
106 
107  l->low -= range_scaled * l->prob[val];
108 
109  return val;
110 }
111 
112 
113 #endif /* AVCODEC_LAGARITHRAC_H */
lag_rac::low
unsigned low
Definition: lagarithrac.h:40
lag_rac::scale
unsigned scale
Number of bits of precision in range.
Definition: lagarithrac.h:42
lag_rac::hash_shift
unsigned hash_shift
Number of bits to shift to calculate hash for radix search.
Definition: lagarithrac.h:43
lag_rac::overread
int overread
Definition: lagarithrac.h:49
lag_get_rac
static uint8_t lag_get_rac(lag_rac *l)
Decode a single byte from the compressed plane described by *l.
Definition: lagarithrac.h:77
lag_rac::prob
uint32_t prob[258]
Table of cumulative probability for each symbol.
Definition: lagarithrac.h:52
lag_rac::range_hash
uint8_t range_hash[1024]
Hash table mapping upper byte to approximate symbol.
Definition: lagarithrac.h:53
GetBitContext
Definition: get_bits.h:61
val
static double val(void *priv, double ch)
Definition: aeval.c:77
intreadwrite.h
get_bits.h
lag_rac
Definition: lagarithrac.h:38
ff_lag_rac_init
void ff_lag_rac_init(lag_rac *l, GetBitContext *gb, int length)
Definition: lagarithrac.c:33
lag_rac::range
unsigned range
Definition: lagarithrac.h:41
lag_rac::avctx
AVCodecContext * avctx
Definition: lagarithrac.h:39
lag_rac_refill
static void lag_rac_refill(lag_rac *l)
Definition: lagarithrac.h:59
lag_rac::bytestream_start
const uint8_t * bytestream_start
Start of input bytestream.
Definition: lagarithrac.h:45
lag_rac::bytestream
const uint8_t * bytestream
Current position in input bytestream.
Definition: lagarithrac.h:46
avcodec.h
AVCodecContext
main external API structure.
Definition: avcodec.h:398
lag_rac::bytestream_end
const uint8_t * bytestream_end
End position of input bytestream.
Definition: lagarithrac.h:47
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98