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/common.h"
33 #include "libavutil/avassert.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
;
47 void ff_init_range_encoder(RangeCoder
*c
, uint8_t *buf
, int buf_size
);
48 void ff_init_range_decoder(RangeCoder
*c
, const uint8_t *buf
, int buf_size
);
49 int ff_rac_terminate(RangeCoder
*c
);
50 void ff_build_rac_states(RangeCoder
*c
, int factor
, int max_p
);
52 static inline void renorm_encoder(RangeCoder
*c
)
55 while (c
->range
< 0x100) {
56 if (c
->outstanding_byte
< 0) {
57 c
->outstanding_byte
= c
->low
>> 8;
58 } else if (c
->low
<= 0xFF00) {
59 *c
->bytestream
++ = c
->outstanding_byte
;
60 for (; c
->outstanding_count
; c
->outstanding_count
--)
61 *c
->bytestream
++ = 0xFF;
62 c
->outstanding_byte
= c
->low
>> 8;
63 } else if (c
->low
>= 0x10000) {
64 *c
->bytestream
++ = c
->outstanding_byte
+ 1;
65 for (; c
->outstanding_count
; c
->outstanding_count
--)
66 *c
->bytestream
++ = 0x00;
67 c
->outstanding_byte
= (c
->low
>> 8) & 0xFF;
69 c
->outstanding_count
++;
72 c
->low
= (c
->low
& 0xFF) << 8;
77 static inline int get_rac_count(RangeCoder
*c
)
79 int x
= c
->bytestream
- c
->bytestream_start
+ c
->outstanding_count
;
80 if (c
->outstanding_byte
>= 0)
82 return 8 * x
- av_log2(c
->range
);
85 static inline void put_rac(RangeCoder
*c
, uint8_t *const state
, int bit
)
87 int range1
= (c
->range
* (*state
)) >> 8;
90 av_assert2(range1
< c
->range
);
91 av_assert2(range1
> 0);
94 *state
= c
->zero_state
[*state
];
96 c
->low
+= c
->range
- range1
;
98 *state
= c
->one_state
[*state
];
104 static inline void refill(RangeCoder
*c
)
106 if (c
->range
< 0x100) {
109 if (c
->bytestream
< c
->bytestream_end
)
110 c
->low
+= c
->bytestream
[0];
115 static inline int get_rac(RangeCoder
*c
, uint8_t *const state
)
117 int range1
= (c
->range
* (*state
)) >> 8;
121 if (c
->low
< c
->range
) {
122 *state
= c
->zero_state
[*state
];
127 *state
= c
->one_state
[*state
];
134 int one_mask one_mask
= (c
->range
- c
->low
- 1) >> 31;
136 c
->low
-= c
->range
& one_mask
;
137 c
->range
+= (range1
- c
->range
) & one_mask
;
139 *state
= c
->zero_state
[(*state
) + (256 & one_mask
)];
147 #endif /* AVCODEC_RANGECODER_H */