FFmpeg  4.3.9
rangecoder.h
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Range coder.
25  */
26 
27 #ifndef AVCODEC_RANGECODER_H
28 #define AVCODEC_RANGECODER_H
29 
30 #include <stdint.h>
31 
32 #include "libavutil/common.h"
33 #include "libavutil/avassert.h"
34 
35 typedef struct RangeCoder {
36  int low;
37  int range;
45  int overread;
46 #define MAX_OVERREAD 2
47 } RangeCoder;
48 
49 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
50 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
51 
52 /**
53  * Terminates the range coder
54  * @param version version 0 requires the decoder to know the data size in bytes
55  * version 1 needs about 1 bit more space but does not need to
56  * carry the size from encoder to decoder
57  */
59 
60 /**
61  * Check if at the current position there is a valid looking termination
62  * @param version version 0 requires the decoder to know the data size in bytes
63  * version 1 needs about 1 bit more space but does not need to
64  * carry the size from encoder to decoder
65  * @returns negative AVERROR code on error or non negative.
66  */
68 
69 void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
70 
71 static inline void renorm_encoder(RangeCoder *c)
72 {
73  // FIXME: optimize
74  if (c->outstanding_byte < 0) {
75  c->outstanding_byte = c->low >> 8;
76  } else if (c->low <= 0xFF00) {
77  *c->bytestream++ = c->outstanding_byte;
78  for (; c->outstanding_count; c->outstanding_count--)
79  *c->bytestream++ = 0xFF;
80  c->outstanding_byte = c->low >> 8;
81  } else if (c->low >= 0x10000) {
82  *c->bytestream++ = c->outstanding_byte + 1;
83  for (; c->outstanding_count; c->outstanding_count--)
84  *c->bytestream++ = 0x00;
85  c->outstanding_byte = (c->low >> 8) & 0xFF;
86  } else {
87  c->outstanding_count++;
88  }
89 
90  c->low = (c->low & 0xFF) << 8;
91  c->range <<= 8;
92 }
93 
94 static inline int get_rac_count(RangeCoder *c)
95 {
96  int x = c->bytestream - c->bytestream_start + c->outstanding_count;
97  if (c->outstanding_byte >= 0)
98  x++;
99  return 8 * x - av_log2(c->range);
100 }
101 
102 static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit)
103 {
104  int range1 = (c->range * (*state)) >> 8;
105 
106  av_assert2(*state);
107  av_assert2(range1 < c->range);
108  av_assert2(range1 > 0);
109  if (!bit) {
110  c->range -= range1;
111  *state = c->zero_state[*state];
112  } else {
113  c->low += c->range - range1;
114  c->range = range1;
115  *state = c->one_state[*state];
116  }
117 
118  while (c->range < 0x100)
119  renorm_encoder(c);
120 }
121 
122 static inline void refill(RangeCoder *c)
123 {
124  if (c->range < 0x100) {
125  c->range <<= 8;
126  c->low <<= 8;
127  if (c->bytestream < c->bytestream_end) {
128  c->low += c->bytestream[0];
129  c->bytestream++;
130  } else
131  c->overread ++;
132  }
133 }
134 
135 static inline int get_rac(RangeCoder *c, uint8_t *const state)
136 {
137  int range1 = (c->range * (*state)) >> 8;
138 
139  c->range -= range1;
140  if (c->low < c->range) {
141  *state = c->zero_state[*state];
142  refill(c);
143  return 0;
144  } else {
145  c->low -= c->range;
146  *state = c->one_state[*state];
147  c->range = range1;
148  refill(c);
149  return 1;
150  }
151 }
152 
153 #endif /* AVCODEC_RANGECODER_H */
version
Definition: libkvazaar.c:292
static struct @314 state
uint32_t low
Definition: mss3.c:64
uint8_t zero_state[256]
Definition: rangecoder.h:40
uint8_t * bytestream_end
Definition: rangecoder.h:44
uint32_t range
Definition: mss3.c:64
uint8_t one_state[256]
Definition: rangecoder.h:41
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
Definition: rangecoder.c:109
uint8_t
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition: rangecoder.h:135
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
Definition: rangecoder.c:42
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:68
static void put_rac(RangeCoder *c, uint8_t *const state, int bit)
Definition: rangecoder.h:102
int ff_rac_check_termination(RangeCoder *c, int version)
Check if at the current position there is a valid looking termination.
Definition: rangecoder.c:125
static void renorm_encoder(RangeCoder *c)
Definition: rangecoder.h:71
simple assert() macros that are a bit more flexible than ISO C assert().
uint8_t * bytestream
Definition: rangecoder.h:43
static void refill(RangeCoder *c)
Definition: rangecoder.h:122
void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: rangecoder.c:53
#define av_log2
Definition: intmath.h:83
int outstanding_byte
Definition: rangecoder.h:39
static int get_rac_count(RangeCoder *c)
Definition: rangecoder.h:94
static const int factor[16]
Definition: vf_pp7.c:75
common internal and external API header
#define bit(string, value)
Definition: cbs_mpeg2.c:58
int overread
Definition: rangecoder.h:45
static double c[64]
int outstanding_count
Definition: rangecoder.h:38
uint8_t * bytestream_start
Definition: rangecoder.h:42