3 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
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.
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.
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
27 #ifndef AVCODEC_RANGECODER_H
28 #define AVCODEC_RANGECODER_H
32 #include "libavutil/avassert.h"
33 #include "libavutil/intmath.h"
35 typedef struct RangeCoder
{
38 int outstanding_count
;
40 uint8_t zero_state
[256];
41 uint8_t one_state
[256];
42 uint8_t *bytestream_start
;
44 uint8_t *bytestream_end
;
46 #define MAX_OVERREAD 2
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
);
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
58 int ff_rac_terminate(RangeCoder
*c
, int version
);
60 void ff_build_rac_states(RangeCoder
*c
, int factor
, int max_p
);
62 static inline void renorm_encoder(RangeCoder
*c
)
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;
72 c
->outstanding_count
++;
75 c
->low
= (c
->low
& 0xFF) << 8;
79 static inline int get_rac_count(RangeCoder
*c
)
81 int x
= c
->bytestream
- c
->bytestream_start
+ c
->outstanding_count
;
82 if (c
->outstanding_byte
>= 0)
84 return 8 * x
- av_log2(c
->range
);
87 static inline void put_rac(RangeCoder
*c
, uint8_t *const state
, int bit
)
89 int range1
= (c
->range
* (*state
)) >> 8;
92 av_assert2(range1
< c
->range
);
93 av_assert2(range1
> 0);
96 *state
= c
->zero_state
[*state
];
98 c
->low
+= c
->range
- range1
;
100 *state
= c
->one_state
[*state
];
103 if (c
->range
< 0x100)
107 static inline void refill(RangeCoder
*c
)
111 if (c
->bytestream
< c
->bytestream_end
) {
112 c
->low
+= c
->bytestream
[0];
118 static inline int get_rac(RangeCoder
*c
, uint8_t *const state
)
120 int range1
= (c
->range
* (*state
)) >> 8;
123 if (c
->low
< c
->range
) {
124 *state
= c
->zero_state
[*state
];
125 if (c
->range
< 0x100)
130 *state
= c
->one_state
[*state
];
132 if (c
->range
< 0x100)
138 #endif /* AVCODEC_RANGECODER_H */