GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/lagarithrac.h Lines: 24 26 92.3 %
Date: 2019-11-20 04:07:19 Branches: 10 12 83.3 %

Line Branch Exec Source
1
/*
2
 * Lagarith range decoder
3
 * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
4
 * Copyright (c) 2009 David Conrad
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * Lagarith range decoder
26
 * @author Nathan Caldwell
27
 * @author David Conrad
28
 */
29
30
#ifndef AVCODEC_LAGARITHRAC_H
31
#define AVCODEC_LAGARITHRAC_H
32
33
#include <stdint.h>
34
#include "libavutil/common.h"
35
#include "libavutil/intreadwrite.h"
36
#include "avcodec.h"
37
#include "get_bits.h"
38
39
typedef struct lag_rac {
40
    AVCodecContext *avctx;
41
    unsigned low;
42
    unsigned range;
43
    unsigned scale;             /**< Number of bits of precision in range. */
44
    unsigned hash_shift;        /**< Number of bits to shift to calculate hash for radix search. */
45
46
    const uint8_t *bytestream_start;  /**< Start of input bytestream. */
47
    const uint8_t *bytestream;        /**< Current position in input bytestream. */
48
    const uint8_t *bytestream_end;    /**< End position of input bytestream. */
49
50
    int overread;
51
#define MAX_OVERREAD 4
52
53
    uint32_t prob[258];         /**< Table of cumulative probability for each symbol. */
54
    uint8_t  range_hash[1024];   /**< Hash table mapping upper byte to approximate symbol. */
55
} lag_rac;
56
57
void ff_lag_rac_init(lag_rac *l, GetBitContext *gb, int length);
58
59
/* TODO: Optimize */
60
4618749
static inline void lag_rac_refill(lag_rac *l)
61
{
62
6432389
    while (l->range <= 0x800000) {
63
1813640
        l->low   <<= 8;
64
1813640
        l->range <<= 8;
65
1813640
        l->low |= 0xff & (AV_RB16(l->bytestream) >> 1);
66
1813640
        if (l->bytestream < l->bytestream_end)
67
1813640
            l->bytestream++;
68
        else
69
            l->overread++;
70
    }
71
4618749
}
72
73
/**
74
 * Decode a single byte from the compressed plane described by *l.
75
 * @param l pointer to lag_rac for the current plane
76
 * @return next byte of decoded data
77
 */
78
4618749
static inline uint8_t lag_get_rac(lag_rac *l)
79
{
80
    unsigned range_scaled, low_scaled;
81
    int val;
82
83
4618749
    lag_rac_refill(l);
84
85
4618749
    range_scaled = l->range >> l->scale;
86
87
4618749
    if (l->low < range_scaled * l->prob[255]) {
88
        /* val = 0 is frequent enough to deserve a shortcut */
89
3840282
        if (l->low < range_scaled * l->prob[1]) {
90
1480947
            val = 0;
91
        } else {
92
2359335
            low_scaled = l->low / (range_scaled<<(l->hash_shift));
93
94
2359335
            val = l->range_hash[low_scaled];
95
2880891
            while (l->low >= range_scaled * l->prob[val + 1])
96
521556
                val++;
97
        }
98
99
3840282
        l->range = range_scaled * (l->prob[val + 1] - l->prob[val]);
100
    } else {
101
778467
        val = 255;
102
778467
        l->range -= range_scaled * l->prob[255];
103
    }
104
105
4618749
    if (!l->range)
106
        l->range = 0x80;
107
108
4618749
    l->low -= range_scaled * l->prob[val];
109
110
4618749
    return val;
111
}
112
113
114
#endif /* AVCODEC_LAGARITHRAC_H */