FFmpeg
Loading...
Searching...
No Matches
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/avassert.h"
33#include "libavutil/intmath.h"
34
35typedef struct RangeCoder {
36 int low;
37 int range;
40 uint8_t zero_state[256];
41 uint8_t one_state[256];
43 uint8_t *bytestream;
46#define MAX_OVERREAD 2
48
49void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
50void 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
60void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
61
62static inline void renorm_encoder(RangeCoder *c)
63{
64 if (c->low - 0xFF01 >= 0x10000 - 0xFF01U) {
65 int mask = c->low - 0xFF01 >> 31;
66 *c->bytestream = c->outstanding_byte + 1 + mask;
67 c->bytestream += c->outstanding_byte >= 0;
68 for (; c->outstanding_count; c->outstanding_count--)
69 *c->bytestream++ = mask;
70 c->outstanding_byte = c->low >> 8;
71 } else {
72 c->outstanding_count++;
73 }
74
75 c->low = (c->low & 0xFF) << 8;
76 c->range <<= 8;
77}
78
79static inline int get_rac_count(RangeCoder *c)
80{
81 int x = c->bytestream - c->bytestream_start + c->outstanding_count;
82 if (c->outstanding_byte >= 0)
83 x++;
84 return 8 * x - av_log2(c->range);
85}
86
87static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit)
88{
89 int range1 = (c->range * (*state)) >> 8;
90
92 av_assert2(range1 < c->range);
93 av_assert2(range1 > 0);
94 if (!bit) {
95 c->range -= range1;
96 *state = c->zero_state[*state];
97 } else {
98 c->low += c->range - range1;
99 c->range = range1;
100 *state = c->one_state[*state];
101 }
102
103 if (c->range < 0x100)
105}
106
107static inline void refill(RangeCoder *c)
108{
109 c->range <<= 8;
110 c->low <<= 8;
111 if (c->bytestream < c->bytestream_end) {
112 c->low += c->bytestream[0];
113 c->bytestream++;
114 } else
115 c->overread ++;
116}
117
118static inline int get_rac(RangeCoder *c, uint8_t *const state)
119{
120 int range1 = (c->range * (*state)) >> 8;
121
122 c->range -= range1;
123 if (c->low < c->range) {
124 *state = c->zero_state[*state];
125 if (c->range < 0x100)
126 refill(c);
127 return 0;
128 } else {
129 c->low -= c->range;
130 *state = c->one_state[*state];
131 c->range = range1;
132 if (c->range < 0x100)
133 refill(c);
134 return 1;
135 }
136}
137
138#endif /* AVCODEC_RANGECODER_H */
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define bit(string, value)
Definition cbs_mpeg2.c:56
static struct @346255127015250356166251341105367306144006377143 state
#define put_rac(C, S, B)
#define av_log2
Definition intmath.h:84
static const int factor[16]
Definition vf_pp7.c:98
version
Definition libkvazaar.c:313
static const uint16_t mask[17]
Definition lzw.c:38
enum AVColorRange range
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
Definition rangecoder.c:109
static void renorm_encoder(RangeCoder *c)
Definition rangecoder.h:62
void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
Definition rangecoder.c:53
static void refill(RangeCoder *c)
Definition rangecoder.h:107
static int get_rac_count(RangeCoder *c)
Definition rangecoder.h:79
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition rangecoder.h:118
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
uint8_t * bytestream_start
Definition rangecoder.h:42
uint8_t * bytestream
Definition rangecoder.h:43
int outstanding_count
Definition rangecoder.h:38
uint32_t low
Definition mss3.c:66
uint8_t * bytestream_end
Definition rangecoder.h:44
uint32_t range
Definition mss3.c:66
uint8_t one_state[256]
Definition rangecoder.h:41
int outstanding_byte
Definition rangecoder.h:39
uint8_t zero_state[256]
Definition rangecoder.h:40
static double c[64]