rangecoder.c
Go to the documentation of this file.
1 /*
2  * Range coder
3  * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
34 #include <string.h>
35 
36 #include "avcodec.h"
37 #include "rangecoder.h"
38 #include "bytestream.h"
39 
40 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
41 {
42  c->bytestream_start =
43  c->bytestream = buf;
44  c->bytestream_end = buf + buf_size;
45  c->low = 0;
46  c->range = 0xFF00;
47  c->outstanding_count = 0;
48  c->outstanding_byte = -1;
49 }
50 
51 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
52 {
53  /* cast to avoid compiler warning */
54  ff_init_range_encoder(c, (uint8_t *)buf, buf_size);
55 
56  c->low = bytestream_get_be16(&c->bytestream);
57 }
58 
59 void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
60 {
61  const int64_t one = 1LL << 32;
62  int64_t p;
63  int last_p8, p8, i;
64 
65  memset(c->zero_state, 0, sizeof(c->zero_state));
66  memset(c->one_state, 0, sizeof(c->one_state));
67 
68  last_p8 = 0;
69  p = one / 2;
70  for (i = 0; i < 128; i++) {
71  p8 = (256 * p + one / 2) >> 32; // FIXME: try without the one
72  if (p8 <= last_p8)
73  p8 = last_p8 + 1;
74  if (last_p8 && last_p8 < 256 && p8 <= max_p)
75  c->one_state[last_p8] = p8;
76 
77  p += ((one - p) * factor + one / 2) >> 32;
78  last_p8 = p8;
79  }
80 
81  for (i = 256 - max_p; i <= max_p; i++) {
82  if (c->one_state[i])
83  continue;
84 
85  p = (i * one + 128) >> 8;
86  p += ((one - p) * factor + one / 2) >> 32;
87  p8 = (256 * p + one / 2) >> 32; // FIXME: try without the one
88  if (p8 <= i)
89  p8 = i + 1;
90  if (p8 > max_p)
91  p8 = max_p;
92  c->one_state[i] = p8;
93  }
94 
95  for (i = 1; i < 255; i++)
96  c->zero_state[i] = 256 - c->one_state[256 - i];
97 }
98 
99 /* Return the number of bytes written. */
101 {
102  c->range = 0xFF;
103  c->low += 0xFF;
104  renorm_encoder(c);
105  c->range = 0xFF;
106  renorm_encoder(c);
107 
108  assert(c->low == 0);
109  assert(c->range >= 0x100);
110 
111  return c->bytestream - c->bytestream_start;
112 }
113 
114 #ifdef TEST
115 #define SIZE 10240
116 
117 #include "libavutil/lfg.h"
118 #include "libavutil/log.h"
119 
120 int main(void)
121 {
122  RangeCoder c;
123  uint8_t b[9 * SIZE];
124  uint8_t r[9 * SIZE];
125  int i;
126  uint8_t state[10];
127  AVLFG prng;
128 
129  av_lfg_init(&prng, 1);
130 
131  ff_init_range_encoder(&c, b, SIZE);
132  ff_build_rac_states(&c, 0.05 * (1LL << 32), 128 + 64 + 32 + 16);
133 
134  memset(state, 128, sizeof(state));
135 
136  for (i = 0; i < SIZE; i++)
137  r[i] = av_lfg_get(&prng) % 7;
138 
139  for (i = 0; i < SIZE; i++)
140  put_rac(&c, state, r[i] & 1);
141 
142  ff_rac_terminate(&c);
143 
144  ff_init_range_decoder(&c, b, SIZE);
145 
146  memset(state, 128, sizeof(state));
147 
148  for (i = 0; i < SIZE; i++)
149  if ((r[i] & 1) != get_rac(&c, state)) {
150  av_log(NULL, AV_LOG_ERROR, "rac failure at %d\n", i);
151  return 1;
152  }
153 
154  return 0;
155 }
156 #endif /* TEST */
Definition: lfg.h:25
uint32_t low
Definition: mss3.c:63
void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
Definition: rangecoder.c:40
uint8_t zero_state[256]
Definition: rangecoder.h:40
Range coder.
uint8_t * bytestream_end
Definition: rangecoder.h:44
uint32_t range
Definition: mss3.c:63
uint8_t one_state[256]
Definition: rangecoder.h:41
int ff_rac_terminate(RangeCoder *c)
Definition: rangecoder.c:100
uint8_t
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition: rangecoder.h:115
#define SIZE
Definition: golomb-test.c:29
#define b
Definition: input.c:52
#define r
Definition: input.c:51
static void renorm_encoder(RangeCoder *c)
Definition: rangecoder.h:52
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
int main(int argc, char **argv)
Definition: avconv.c:2348
uint8_t * bytestream
Definition: rangecoder.h:43
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:59
NULL
Definition: eval.c:52
int outstanding_byte
Definition: rangecoder.h:39
external API header
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: rangecoder.c:51
static uint32_t state
Definition: trasher.c:27
int outstanding_count
Definition: rangecoder.h:38
#define put_rac(C, S, B)
uint8_t * bytestream_start
Definition: rangecoder.h:42