GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/rl.c Lines: 58 58 100.0 %
Date: 2021-01-27 11:56:10 Branches: 31 32 96.9 %

Line Branch Exec Source
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19
#include <stdint.h>
20
#include <string.h>
21
22
#include "libavutil/attributes.h"
23
#include "libavutil/avassert.h"
24
#include "libavutil/mem.h"
25
26
#include "rl.h"
27
28
1207
av_cold void ff_rl_init(RLTable *rl,
29
                        uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
30
{
31
    int8_t  max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
32
    uint8_t index_run[MAX_RUN + 1];
33
    int last, run, level, start, end, i;
34
35
    /* If rl->max_level[0] is set, this RLTable has already been initialized */
36
1207
    if (rl->max_level[0])
37
3
        return;
38
39
    /* compute max_level[], max_run[] and index_run[] */
40
3612
    for (last = 0; last < 2; last++) {
41
2408
        if (last == 0) {
42
1204
            start = 0;
43
1204
            end = rl->last;
44
        } else {
45
1204
            start = rl->last;
46
1204
            end = rl->n;
47
        }
48
49
2408
        memset(max_level, 0, MAX_RUN + 1);
50
2408
        memset(max_run, 0, MAX_LEVEL + 1);
51
2408
        memset(index_run, rl->n, MAX_RUN + 1);
52
152254
        for (i = start; i < end; i++) {
53
149846
            run   = rl->table_run[i];
54
149846
            level = rl->table_level[i];
55
149846
            if (index_run[run] == rl->n)
56
68184
                index_run[run] = i;
57
149846
            if (level > max_level[run])
58
149840
                max_level[run] = level;
59
149846
            if (run > max_run[level])
60
118194
                max_run[level] = run;
61
        }
62
2408
        rl->max_level[last] = static_store[last];
63
2408
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
64
2408
        rl->max_run[last]   = static_store[last] + MAX_RUN + 1;
65
2408
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
66
2408
        rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
67
2408
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
68
    }
69
}
70
71
852
av_cold void ff_rl_init_vlc(RLTable *rl, unsigned static_size)
72
{
73
    int i, q;
74
852
    VLC_TYPE table[1500][2] = {{0}};
75
852
    VLC vlc = { .table = table, .table_allocated = static_size };
76
852
    av_assert0(static_size <= FF_ARRAY_ELEMS(table));
77
852
    init_vlc(&vlc, 9, rl->n + 1, &rl->table_vlc[0][1], 4, 2, &rl->table_vlc[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
78
79
12647
    for (q = 0; q < 32; q++) {
80
12294
        int qmul = q * 2;
81
12294
        int qadd = (q - 1) | 1;
82
83
12294
        if (!rl->rl_vlc[q])
84
499
            return;
85
86
11795
        if (q == 0) {
87
852
            qmul = 1;
88
852
            qadd = 0;
89
        }
90
9307213
        for (i = 0; i < vlc.table_size; i++) {
91
9295418
            int code = vlc.table[i][0];
92
9295418
            int len  = vlc.table[i][1];
93
            int level, run;
94
95
9295418
            if (len == 0) { // illegal code
96
94493
                run   = 66;
97
94493
                level = MAX_LEVEL;
98
9200925
            } else if (len < 0) { // more bits needed
99
174832
                run   = 0;
100
174832
                level = code;
101
            } else {
102
9026093
                if (code == rl->n) { // esc
103
127527
                    run   = 66;
104
127527
                    level =  0;
105
                } else {
106
8898566
                    run   = rl->table_run[code] + 1;
107
8898566
                    level = rl->table_level[code] * qmul + qadd;
108
8898566
                    if (code >= rl->last) run += 192;
109
                }
110
            }
111
9295418
            rl->rl_vlc[q][i].len   = len;
112
9295418
            rl->rl_vlc[q][i].level = level;
113
9295418
            rl->rl_vlc[q][i].run   = run;
114
        }
115
    }
116
}