FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/rangecoder.h
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 54 54 100.0%
Functions: 5 5 100.0%
Branches: 22 22 100.0%

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 126087278 static inline void renorm_encoder(RangeCoder *c)
63 {
64 // FIXME: optimize
65
2/2
✓ Branch 0 taken 11987 times.
✓ Branch 1 taken 126075291 times.
126087278 if (c->outstanding_byte < 0) {
66 11987 c->outstanding_byte = c->low >> 8;
67
2/2
✓ Branch 0 taken 83008144 times.
✓ Branch 1 taken 43067147 times.
126075291 } else if (c->low <= 0xFF00) {
68 83008144 *c->bytestream++ = c->outstanding_byte;
69
2/2
✓ Branch 0 taken 1017676 times.
✓ Branch 1 taken 83008144 times.
84025820 for (; c->outstanding_count; c->outstanding_count--)
70 1017676 *c->bytestream++ = 0xFF;
71 83008144 c->outstanding_byte = c->low >> 8;
72
2/2
✓ Branch 0 taken 41886774 times.
✓ Branch 1 taken 1180373 times.
43067147 } else if (c->low >= 0x10000) {
73 41886774 *c->bytestream++ = c->outstanding_byte + 1;
74
2/2
✓ Branch 0 taken 162603 times.
✓ Branch 1 taken 41886774 times.
42049377 for (; c->outstanding_count; c->outstanding_count--)
75 162603 *c->bytestream++ = 0x00;
76 41886774 c->outstanding_byte = (c->low >> 8) & 0xFF;
77 } else {
78 1180373 c->outstanding_count++;
79 }
80
81 126087278 c->low = (c->low & 0xFF) << 8;
82 126087278 c->range <<= 8;
83 126087278 }
84
85 1673460 static inline int get_rac_count(RangeCoder *c)
86 {
87 1673460 int x = c->bytestream - c->bytestream_start + c->outstanding_count;
88
2/2
✓ Branch 0 taken 1672998 times.
✓ Branch 1 taken 462 times.
1673460 if (c->outstanding_byte >= 0)
89 1672998 x++;
90 1673460 return 8 * x - av_log2(c->range);
91 }
92
93 2056143421 static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit)
94 {
95 2056143421 int range1 = (c->range * (*state)) >> 8;
96
97 av_assert2(*state);
98 av_assert2(range1 < c->range);
99 av_assert2(range1 > 0);
100
2/2
✓ Branch 0 taken 921595469 times.
✓ Branch 1 taken 1134547952 times.
2056143421 if (!bit) {
101 921595469 c->range -= range1;
102 921595469 *state = c->zero_state[*state];
103 } else {
104 1134547952 c->low += c->range - range1;
105 1134547952 c->range = range1;
106 1134547952 *state = c->one_state[*state];
107 }
108
109
2/2
✓ Branch 0 taken 126064930 times.
✓ Branch 1 taken 2056143421 times.
2182208351 while (c->range < 0x100)
110 126064930 renorm_encoder(c);
111 2056143421 }
112
113 1729017157 static inline void refill(RangeCoder *c)
114 {
115
2/2
✓ Branch 0 taken 104572014 times.
✓ Branch 1 taken 1624445143 times.
1729017157 if (c->range < 0x100) {
116 104572014 c->range <<= 8;
117 104572014 c->low <<= 8;
118
2/2
✓ Branch 0 taken 104570331 times.
✓ Branch 1 taken 1683 times.
104572014 if (c->bytestream < c->bytestream_end) {
119 104570331 c->low += c->bytestream[0];
120 104570331 c->bytestream++;
121 } else
122 1683 c->overread ++;
123 }
124 1729017157 }
125
126 1729017157 static inline int get_rac(RangeCoder *c, uint8_t *const state)
127 {
128 1729017157 int range1 = (c->range * (*state)) >> 8;
129
130 1729017157 c->range -= range1;
131
2/2
✓ Branch 0 taken 759253520 times.
✓ Branch 1 taken 969763637 times.
1729017157 if (c->low < c->range) {
132 759253520 *state = c->zero_state[*state];
133 759253520 refill(c);
134 759253520 return 0;
135 } else {
136 969763637 c->low -= c->range;
137 969763637 *state = c->one_state[*state];
138 969763637 c->range = range1;
139 969763637 refill(c);
140 969763637 return 1;
141 }
142 }
143
144 #endif /* AVCODEC_RANGECODER_H */
145