FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/rangecoder.c
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 53 55 96.4%
Branches: 23 24 95.8%

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 * based upon
26 * "Range encoding: an algorithm for removing redundancy from a digitised
27 * message.
28 * G. N. N. Martin Presented in March 1979 to the Video &
29 * Data Recording Conference,
30 * IBM UK Scientific Center held in Southampton July 24-27 1979."
31 */
32
33 #include <string.h>
34
35 #include "libavutil/attributes.h"
36 #include "libavutil/avassert.h"
37 #include "libavutil/bswap.h"
38 #include "libavutil/intreadwrite.h"
39
40 #include "rangecoder.h"
41
42 17648 av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
43 {
44 17648 c->bytestream_start =
45 17648 c->bytestream = buf;
46 17648 c->bytestream_end = buf + buf_size;
47 17648 c->low = 0;
48 17648 c->range = 0xFF00;
49 17648 c->outstanding_count = 0;
50 17648 c->outstanding_byte = -1;
51 17648 }
52
53 8497 av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf,
54 int buf_size)
55 {
56 /* cast to avoid compiler warning */
57 8497 ff_init_range_encoder(c, (uint8_t *)buf, buf_size);
58
59 8497 c->low = AV_RB16(c->bytestream);
60 8497 c->bytestream += 2;
61 8497 c->overread = 0;
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8497 times.
8497 if (c->low >= 0xFF00) {
63 c->low = 0xFF00;
64 c->bytestream_end = c->bytestream;
65 }
66 8497 }
67
68 6508 void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
69 {
70 6508 const int64_t one = 1LL << 32;
71 int64_t p;
72 int last_p8, p8, i;
73
74 6508 memset(c->zero_state, 0, sizeof(c->zero_state));
75 6508 memset(c->one_state, 0, sizeof(c->one_state));
76
77 6508 last_p8 = 0;
78 6508 p = one / 2;
79
2/2
✓ Branch 0 taken 833024 times.
✓ Branch 1 taken 6508 times.
839532 for (i = 0; i < 128; i++) {
80 833024 p8 = (256 * p + one / 2) >> 32; // FIXME: try without the one
81
2/2
✓ Branch 0 taken 566196 times.
✓ Branch 1 taken 266828 times.
833024 if (p8 <= last_p8)
82 566196 p8 = last_p8 + 1;
83
6/6
✓ Branch 0 taken 826516 times.
✓ Branch 1 taken 6508 times.
✓ Branch 2 taken 364448 times.
✓ Branch 3 taken 462068 times.
✓ Branch 4 taken 296000 times.
✓ Branch 5 taken 68448 times.
833024 if (last_p8 && last_p8 < 256 && p8 <= max_p)
84 296000 c->one_state[last_p8] = p8;
85
86 833024 p += ((one - p) * factor + one / 2) >> 32;
87 833024 last_p8 = p8;
88 }
89
90
2/2
✓ Branch 0 taken 1535660 times.
✓ Branch 1 taken 6508 times.
1542168 for (i = 256 - max_p; i <= max_p; i++) {
91
2/2
✓ Branch 0 taken 296000 times.
✓ Branch 1 taken 1239660 times.
1535660 if (c->one_state[i])
92 296000 continue;
93
94 1239660 p = (i * one + 128) >> 8;
95 1239660 p += ((one - p) * factor + one / 2) >> 32;
96 1239660 p8 = (256 * p + one / 2) >> 32; // FIXME: try without the one
97
2/2
✓ Branch 0 taken 4460 times.
✓ Branch 1 taken 1235200 times.
1239660 if (p8 <= i)
98 4460 p8 = i + 1;
99
2/2
✓ Branch 0 taken 6508 times.
✓ Branch 1 taken 1233152 times.
1239660 if (p8 > max_p)
100 6508 p8 = max_p;
101 1239660 c->one_state[i] = p8;
102 }
103
104
2/2
✓ Branch 0 taken 1653032 times.
✓ Branch 1 taken 6508 times.
1659540 for (i = 1; i < 255; i++)
105 1653032 c->zero_state[i] = 256 - c->one_state[256 - i];
106 6508 }
107
108 /* Return the number of bytes written. */
109 9151 int ff_rac_terminate(RangeCoder *c, int version)
110 {
111
2/2
✓ Branch 0 taken 7444 times.
✓ Branch 1 taken 1707 times.
9151 if (version == 1)
112 7444 put_rac(c, (uint8_t[]) { 129 }, 0);
113 9151 c->range = 0xFF;
114 9151 c->low += 0xFF;
115 9151 renorm_encoder(c);
116 9151 c->range = 0xFF;
117 9151 renorm_encoder(c);
118
119 av_assert1(c->low == 0);
120 av_assert1(c->range >= 0x100);
121
122 9151 return c->bytestream - c->bytestream_start;
123 }
124