| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 35 | typedef struct RangeCoder { | ||
| 36 | int low; | ||
| 37 | int range; | ||
| 38 | int outstanding_count; | ||
| 39 | int outstanding_byte; | ||
| 40 | uint8_t zero_state[256]; | ||
| 41 | uint8_t one_state[256]; | ||
| 42 | uint8_t *bytestream_start; | ||
| 43 | uint8_t *bytestream; | ||
| 44 | uint8_t *bytestream_end; | ||
| 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 | */ | ||
| 58 | int ff_rac_terminate(RangeCoder *c, int version); | ||
| 59 | |||
| 60 | void ff_build_rac_states(RangeCoder *c, int factor, int max_p); | ||
| 61 | |||
| 62 | 126117916 | static inline void renorm_encoder(RangeCoder *c) | |
| 63 | { | ||
| 64 |
2/2✓ Branch 0 taken 124940874 times.
✓ Branch 1 taken 1177042 times.
|
126117916 | if (c->low - 0xFF01 >= 0x10000 - 0xFF01U) { |
| 65 | 124940874 | int mask = c->low - 0xFF01 >> 31; | |
| 66 | 124940874 | *c->bytestream = c->outstanding_byte + 1 + mask; | |
| 67 | 124940874 | c->bytestream += c->outstanding_byte >= 0; | |
| 68 |
2/2✓ Branch 0 taken 1176971 times.
✓ Branch 1 taken 124940874 times.
|
126117845 | for (; c->outstanding_count; c->outstanding_count--) |
| 69 | 1176971 | *c->bytestream++ = mask; | |
| 70 | 124940874 | c->outstanding_byte = c->low >> 8; | |
| 71 | } else { | ||
| 72 | 1177042 | c->outstanding_count++; | |
| 73 | } | ||
| 74 | |||
| 75 | 126117916 | c->low = (c->low & 0xFF) << 8; | |
| 76 | 126117916 | c->range <<= 8; | |
| 77 | 126117916 | } | |
| 78 | |||
| 79 | 1702620 | static inline int get_rac_count(RangeCoder *c) | |
| 80 | { | ||
| 81 | 1702620 | int x = c->bytestream - c->bytestream_start + c->outstanding_count; | |
| 82 |
2/2✓ Branch 0 taken 1702155 times.
✓ Branch 1 taken 465 times.
|
1702620 | if (c->outstanding_byte >= 0) |
| 83 | 1702155 | x++; | |
| 84 | 1702620 | return 8 * x - av_log2(c->range); | |
| 85 | } | ||
| 86 | |||
| 87 | 2056422021 | static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit) | |
| 88 | { | ||
| 89 | 2056422021 | int range1 = (c->range * (*state)) >> 8; | |
| 90 | |||
| 91 | av_assert2(*state); | ||
| 92 | av_assert2(range1 < c->range); | ||
| 93 | av_assert2(range1 > 0); | ||
| 94 |
2/2✓ Branch 0 taken 921739679 times.
✓ Branch 1 taken 1134682342 times.
|
2056422021 | if (!bit) { |
| 95 | 921739679 | c->range -= range1; | |
| 96 | 921739679 | *state = c->zero_state[*state]; | |
| 97 | } else { | ||
| 98 | 1134682342 | c->low += c->range - range1; | |
| 99 | 1134682342 | c->range = range1; | |
| 100 | 1134682342 | *state = c->one_state[*state]; | |
| 101 | } | ||
| 102 | |||
| 103 |
2/2✓ Branch 0 taken 126095212 times.
✓ Branch 1 taken 1930326809 times.
|
2056422021 | if (c->range < 0x100) |
| 104 | 126095212 | renorm_encoder(c); | |
| 105 | 2056422021 | } | |
| 106 | |||
| 107 | 102455417 | static inline void refill(RangeCoder *c) | |
| 108 | { | ||
| 109 | 102455417 | c->range <<= 8; | |
| 110 | 102455417 | c->low <<= 8; | |
| 111 |
2/2✓ Branch 0 taken 102453674 times.
✓ Branch 1 taken 1743 times.
|
102455417 | if (c->bytestream < c->bytestream_end) { |
| 112 | 102453674 | c->low += c->bytestream[0]; | |
| 113 | 102453674 | c->bytestream++; | |
| 114 | } else | ||
| 115 | 1743 | c->overread ++; | |
| 116 | 102455417 | } | |
| 117 | |||
| 118 | 1694934594 | static inline int get_rac(RangeCoder *c, uint8_t *const state) | |
| 119 | { | ||
| 120 | 1694934594 | int range1 = (c->range * (*state)) >> 8; | |
| 121 | |||
| 122 | 1694934594 | c->range -= range1; | |
| 123 |
2/2✓ Branch 0 taken 744223913 times.
✓ Branch 1 taken 950710681 times.
|
1694934594 | if (c->low < c->range) { |
| 124 | 744223913 | *state = c->zero_state[*state]; | |
| 125 |
2/2✓ Branch 0 taken 51489153 times.
✓ Branch 1 taken 692734760 times.
|
744223913 | if (c->range < 0x100) |
| 126 | 51489153 | refill(c); | |
| 127 | 744223913 | return 0; | |
| 128 | } else { | ||
| 129 | 950710681 | c->low -= c->range; | |
| 130 | 950710681 | *state = c->one_state[*state]; | |
| 131 | 950710681 | c->range = range1; | |
| 132 |
2/2✓ Branch 0 taken 50966264 times.
✓ Branch 1 taken 899744417 times.
|
950710681 | if (c->range < 0x100) |
| 133 | 50966264 | refill(c); | |
| 134 | 950710681 | return 1; | |
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | #endif /* AVCODEC_RANGECODER_H */ | ||
| 139 |