Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavcodec/rangecoder.h
Line
Count
Source (jump to first uncovered line)
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
0
#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
static inline void renorm_encoder(RangeCoder *c)
63
660M
{
64
    // FIXME: optimize
65
709M
    while (c->range < 0x100) {
66
49.1M
        if (c->outstanding_byte < 0) {
67
138k
            c->outstanding_byte = c->low >> 8;
68
48.9M
        } else if (c->low <= 0xFF00) {
69
30.9M
            *c->bytestream++ = c->outstanding_byte;
70
32.3M
            for (; c->outstanding_count; c->outstanding_count--)
71
1.43M
                *c->bytestream++ = 0xFF;
72
30.9M
            c->outstanding_byte = c->low >> 8;
73
30.9M
        } else if (c->low >= 0x10000) {
74
16.6M
            *c->bytestream++ = c->outstanding_byte + 1;
75
16.6M
            for (; c->outstanding_count; c->outstanding_count--)
76
76.4k
                *c->bytestream++ = 0x00;
77
16.6M
            c->outstanding_byte = (c->low >> 8) & 0xFF;
78
16.6M
        } else {
79
1.44M
            c->outstanding_count++;
80
1.44M
        }
81
82
49.1M
        c->low     = (c->low & 0xFF) << 8;
83
49.1M
        c->range <<= 8;
84
49.1M
    }
85
660M
}
Unexecuted instantiation: snow.c:renorm_encoder
Unexecuted instantiation: sonic.c:renorm_encoder
rangecoder.c:renorm_encoder
Line
Count
Source
63
220k
{
64
    // FIXME: optimize
65
406k
    while (c->range < 0x100) {
66
186k
        if (c->outstanding_byte < 0) {
67
1.36k
            c->outstanding_byte = c->low >> 8;
68
184k
        } else if (c->low <= 0xFF00) {
69
165k
            *c->bytestream++ = c->outstanding_byte;
70
1.13M
            for (; c->outstanding_count; c->outstanding_count--)
71
972k
                *c->bytestream++ = 0xFF;
72
165k
            c->outstanding_byte = c->low >> 8;
73
165k
        } else if (c->low >= 0x10000) {
74
18.4k
            *c->bytestream++ = c->outstanding_byte + 1;
75
20.2k
            for (; c->outstanding_count; c->outstanding_count--)
76
1.72k
                *c->bytestream++ = 0x00;
77
18.4k
            c->outstanding_byte = (c->low >> 8) & 0xFF;
78
18.4k
        } else {
79
589
            c->outstanding_count++;
80
589
        }
81
82
186k
        c->low     = (c->low & 0xFF) << 8;
83
186k
        c->range <<= 8;
84
186k
    }
85
220k
}
snowenc.c:renorm_encoder
Line
Count
Source
63
262M
{
64
    // FIXME: optimize
65
288M
    while (c->range < 0x100) {
66
26.8M
        if (c->outstanding_byte < 0) {
67
92.3k
            c->outstanding_byte = c->low >> 8;
68
26.7M
        } else if (c->low <= 0xFF00) {
69
17.7M
            *c->bytestream++ = c->outstanding_byte;
70
17.8M
            for (; c->outstanding_count; c->outstanding_count--)
71
129k
                *c->bytestream++ = 0xFF;
72
17.7M
            c->outstanding_byte = c->low >> 8;
73
17.7M
        } else if (c->low >= 0x10000) {
74
8.96M
            *c->bytestream++ = c->outstanding_byte + 1;
75
8.99M
            for (; c->outstanding_count; c->outstanding_count--)
76
35.3k
                *c->bytestream++ = 0x00;
77
8.96M
            c->outstanding_byte = (c->low >> 8) & 0xFF;
78
8.96M
        } else {
79
103k
            c->outstanding_count++;
80
103k
        }
81
82
26.8M
        c->low     = (c->low & 0xFF) << 8;
83
26.8M
        c->range <<= 8;
84
26.8M
    }
85
262M
}
ffv1enc.c:renorm_encoder
Line
Count
Source
63
398M
{
64
    // FIXME: optimize
65
420M
    while (c->range < 0x100) {
66
22.0M
        if (c->outstanding_byte < 0) {
67
45.1k
            c->outstanding_byte = c->low >> 8;
68
22.0M
        } else if (c->low <= 0xFF00) {
69
13.0M
            *c->bytestream++ = c->outstanding_byte;
70
13.3M
            for (; c->outstanding_count; c->outstanding_count--)
71
328k
                *c->bytestream++ = 0xFF;
72
13.0M
            c->outstanding_byte = c->low >> 8;
73
13.0M
        } else if (c->low >= 0x10000) {
74
7.63M
            *c->bytestream++ = c->outstanding_byte + 1;
75
7.67M
            for (; c->outstanding_count; c->outstanding_count--)
76
39.3k
                *c->bytestream++ = 0x00;
77
7.63M
            c->outstanding_byte = (c->low >> 8) & 0xFF;
78
7.63M
        } else {
79
1.34M
            c->outstanding_count++;
80
1.34M
        }
81
82
22.0M
        c->low     = (c->low & 0xFF) << 8;
83
22.0M
        c->range <<= 8;
84
22.0M
    }
85
398M
}
Unexecuted instantiation: ffv1.c:renorm_encoder
86
87
static inline int get_rac_count(RangeCoder *c)
88
1.12M
{
89
1.12M
    int x = c->bytestream - c->bytestream_start + c->outstanding_count;
90
1.12M
    if (c->outstanding_byte >= 0)
91
1.09M
        x++;
92
1.12M
    return 8 * x - av_log2(c->range);
93
1.12M
}
Unexecuted instantiation: snow.c:get_rac_count
Unexecuted instantiation: sonic.c:get_rac_count
Unexecuted instantiation: rangecoder.c:get_rac_count
snowenc.c:get_rac_count
Line
Count
Source
88
1.12M
{
89
1.12M
    int x = c->bytestream - c->bytestream_start + c->outstanding_count;
90
1.12M
    if (c->outstanding_byte >= 0)
91
1.09M
        x++;
92
1.12M
    return 8 * x - av_log2(c->range);
93
1.12M
}
Unexecuted instantiation: ffv1enc.c:get_rac_count
Unexecuted instantiation: ffv1.c:get_rac_count
94
95
static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit)
96
660M
{
97
660M
    int range1 = (c->range * (*state)) >> 8;
98
99
660M
    av_assert2(*state);
100
660M
    av_assert2(range1 < c->range);
101
660M
    av_assert2(range1 > 0);
102
660M
    if (!bit) {
103
210M
        c->range -= range1;
104
210M
        *state    = c->zero_state[*state];
105
449M
    } else {
106
449M
        c->low  += c->range - range1;
107
449M
        c->range = range1;
108
449M
        *state   = c->one_state[*state];
109
449M
    }
110
111
660M
    renorm_encoder(c);
112
660M
}
Unexecuted instantiation: snow.c:put_rac
Unexecuted instantiation: sonic.c:put_rac
rangecoder.c:put_rac
Line
Count
Source
96
42.5k
{
97
42.5k
    int range1 = (c->range * (*state)) >> 8;
98
99
42.5k
    av_assert2(*state);
100
42.5k
    av_assert2(range1 < c->range);
101
42.5k
    av_assert2(range1 > 0);
102
42.5k
    if (!bit) {
103
42.5k
        c->range -= range1;
104
42.5k
        *state    = c->zero_state[*state];
105
42.5k
    } else {
106
0
        c->low  += c->range - range1;
107
0
        c->range = range1;
108
0
        *state   = c->one_state[*state];
109
0
    }
110
111
42.5k
    renorm_encoder(c);
112
42.5k
}
snowenc.c:put_rac
Line
Count
Source
96
262M
{
97
262M
    int range1 = (c->range * (*state)) >> 8;
98
99
262M
    av_assert2(*state);
100
262M
    av_assert2(range1 < c->range);
101
262M
    av_assert2(range1 > 0);
102
262M
    if (!bit) {
103
125M
        c->range -= range1;
104
125M
        *state    = c->zero_state[*state];
105
136M
    } else {
106
136M
        c->low  += c->range - range1;
107
136M
        c->range = range1;
108
136M
        *state   = c->one_state[*state];
109
136M
    }
110
111
262M
    renorm_encoder(c);
112
262M
}
ffv1enc.c:put_rac
Line
Count
Source
96
398M
{
97
398M
    int range1 = (c->range * (*state)) >> 8;
98
99
398M
    av_assert2(*state);
100
398M
    av_assert2(range1 < c->range);
101
398M
    av_assert2(range1 > 0);
102
398M
    if (!bit) {
103
85.3M
        c->range -= range1;
104
85.3M
        *state    = c->zero_state[*state];
105
312M
    } else {
106
312M
        c->low  += c->range - range1;
107
312M
        c->range = range1;
108
312M
        *state   = c->one_state[*state];
109
312M
    }
110
111
398M
    renorm_encoder(c);
112
398M
}
Unexecuted instantiation: ffv1.c:put_rac
113
114
static inline void refill(RangeCoder *c)
115
0
{
116
0
    if (c->range < 0x100) {
117
0
        c->range <<= 8;
118
0
        c->low   <<= 8;
119
0
        if (c->bytestream < c->bytestream_end) {
120
0
            c->low += c->bytestream[0];
121
0
            c->bytestream++;
122
0
        } else
123
0
            c->overread ++;
124
0
    }
125
0
}
Unexecuted instantiation: snow.c:refill
Unexecuted instantiation: sonic.c:refill
Unexecuted instantiation: rangecoder.c:refill
Unexecuted instantiation: snowenc.c:refill
Unexecuted instantiation: ffv1enc.c:refill
Unexecuted instantiation: ffv1.c:refill
126
127
static inline int get_rac(RangeCoder *c, uint8_t *const state)
128
0
{
129
0
    int range1 = (c->range * (*state)) >> 8;
130
131
0
    c->range -= range1;
132
0
    if (c->low < c->range) {
133
0
        *state = c->zero_state[*state];
134
0
        refill(c);
135
0
        return 0;
136
0
    } else {
137
0
        c->low  -= c->range;
138
0
        *state   = c->one_state[*state];
139
0
        c->range = range1;
140
0
        refill(c);
141
0
        return 1;
142
0
    }
143
0
}
Unexecuted instantiation: snow.c:get_rac
Unexecuted instantiation: sonic.c:get_rac
Unexecuted instantiation: rangecoder.c:get_rac
Unexecuted instantiation: snowenc.c:get_rac
Unexecuted instantiation: ffv1enc.c:get_rac
Unexecuted instantiation: ffv1.c:get_rac
144
145
#endif /* AVCODEC_RANGECODER_H */