GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/rangecoder.c Lines: 63 67 94.0 %
Date: 2020-09-25 14:59:26 Branches: 30 34 88.2 %

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/intreadwrite.h"
38
39
#include "avcodec.h"
40
#include "rangecoder.h"
41
42
15244
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
43
{
44
15244
    c->bytestream_start  =
45
15244
    c->bytestream        = buf;
46
15244
    c->bytestream_end    = buf + buf_size;
47
15244
    c->low               = 0;
48
15244
    c->range             = 0xFF00;
49
15244
    c->outstanding_count = 0;
50
15244
    c->outstanding_byte  = -1;
51
15244
}
52
53
7662
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
7662
    ff_init_range_encoder(c, (uint8_t *)buf, buf_size);
58
59
7662
    c->low         = AV_RB16(c->bytestream);
60
7662
    c->bytestream += 2;
61
7662
    c->overread    = 0;
62
7662
    if (c->low >= 0xFF00) {
63
        c->low = 0xFF00;
64
        c->bytestream_end = c->bytestream;
65
    }
66
7662
}
67
68
5937
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
69
{
70
5937
    const int64_t one = 1LL << 32;
71
    int64_t p;
72
    int last_p8, p8, i;
73
74
5937
    memset(c->zero_state, 0, sizeof(c->zero_state));
75
5937
    memset(c->one_state, 0, sizeof(c->one_state));
76
77
5937
    last_p8 = 0;
78
5937
    p       = one / 2;
79
765873
    for (i = 0; i < 128; i++) {
80
759936
        p8 = (256 * p + one / 2) >> 32; // FIXME: try without the one
81
759936
        if (p8 <= last_p8)
82
516519
            p8 = last_p8 + 1;
83

759936
        if (last_p8 && last_p8 < 256 && p8 <= max_p)
84
268592
            c->one_state[last_p8] = p8;
85
86
759936
        p      += ((one - p) * factor + one / 2) >> 32;
87
759936
        last_p8 = p8;
88
    }
89
90
1403986
    for (i = 256 - max_p; i <= max_p; i++) {
91
1398049
        if (c->one_state[i])
92
268592
            continue;
93
94
1129457
        p  = (i * one + 128) >> 8;
95
1129457
        p += ((one - p) * factor + one / 2) >> 32;
96
1129457
        p8 = (256 * p + one / 2) >> 32; // FIXME: try without the one
97
1129457
        if (p8 <= i)
98
3889
            p8 = i + 1;
99
1129457
        if (p8 > max_p)
100
5937
            p8 = max_p;
101
1129457
        c->one_state[i] = p8;
102
    }
103
104
1513935
    for (i = 1; i < 255; i++)
105
1507998
        c->zero_state[i] = 256 - c->one_state[256 - i];
106
5937
}
107
108
/* Return the number of bytes written. */
109
7582
int ff_rac_terminate(RangeCoder *c, int version)
110
{
111
7582
    if (version == 1)
112
5824
        put_rac(c, (uint8_t[]) { 129 }, 0);
113
7582
    c->range = 0xFF;
114
7582
    c->low  += 0xFF;
115
7582
    renorm_encoder(c);
116
7582
    c->range = 0xFF;
117
7582
    renorm_encoder(c);
118
119
    av_assert1(c->low   == 0);
120
    av_assert1(c->range >= 0x100);
121
122
7582
    return c->bytestream - c->bytestream_start;
123
}
124
125
2048
int ff_rac_check_termination(RangeCoder *c, int version)
126
{
127
2048
    if (version == 1) {
128
1024
        RangeCoder tmp = *c;
129
1024
        get_rac(c, (uint8_t[]) { 129 });
130
131

1024
        if (c->bytestream == tmp.bytestream && c->bytestream > c->bytestream_start)
132
896
            tmp.low -= *--tmp.bytestream;
133
1024
        tmp.bytestream_end = tmp.bytestream;
134
135
1024
        if (get_rac(&tmp, (uint8_t[]) { 129 }))
136
            return AVERROR_INVALIDDATA;
137
    } else {
138
1024
        if (c->bytestream_end != c->bytestream)
139
            return AVERROR_INVALIDDATA;
140
    }
141
2048
    return 0;
142
}