rl: Rename ff_*_rl() to ff_rl_*()
[libav.git] / libavcodec / rl.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav 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 * Libav 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 Libav; 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
21 #include "libavutil/attributes.h"
22
23 #include "rl.h"
24
25 av_cold void ff_rl_init(RLTable *rl,
26 uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
27 {
28 int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
29 uint8_t index_run[MAX_RUN + 1];
30 int last, run, level, start, end, i;
31
32 /* If table is static, we can quit if rl->max_level[0] is not NULL */
33 if (static_store && rl->max_level[0])
34 return;
35
36 /* compute max_level[], max_run[] and index_run[] */
37 for (last = 0; last < 2; last++) {
38 if (last == 0) {
39 start = 0;
40 end = rl->last;
41 } else {
42 start = rl->last;
43 end = rl->n;
44 }
45
46 memset(max_level, 0, MAX_RUN + 1);
47 memset(max_run, 0, MAX_LEVEL + 1);
48 memset(index_run, rl->n, MAX_RUN + 1);
49 for (i = start; i < end; i++) {
50 run = rl->table_run[i];
51 level = rl->table_level[i];
52 if (index_run[run] == rl->n)
53 index_run[run] = i;
54 if (level > max_level[run])
55 max_level[run] = level;
56 if (run > max_run[level])
57 max_run[level] = run;
58 }
59 if (static_store)
60 rl->max_level[last] = static_store[last];
61 else
62 rl->max_level[last] = av_malloc(MAX_RUN + 1);
63 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
64 if (static_store)
65 rl->max_run[last] = static_store[last] + MAX_RUN + 1;
66 else
67 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
68 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
69 if (static_store)
70 rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
71 else
72 rl->index_run[last] = av_malloc(MAX_RUN + 1);
73 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
74 }
75 }
76
77 av_cold void ff_rl_init_vlc(RLTable *rl)
78 {
79 int i, q;
80
81 for (q = 0; q < 32; q++) {
82 int qmul = q * 2;
83 int qadd = (q - 1) | 1;
84
85 if (q == 0) {
86 qmul = 1;
87 qadd = 0;
88 }
89 for (i = 0; i < rl->vlc.table_size; i++) {
90 int code = rl->vlc.table[i][0];
91 int len = rl->vlc.table[i][1];
92 int level, run;
93
94 if (len == 0) { // illegal code
95 run = 66;
96 level = MAX_LEVEL;
97 } else if (len < 0) { // more bits needed
98 run = 0;
99 level = code;
100 } else {
101 if (code == rl->n) { // esc
102 run = 66;
103 level = 0;
104 } else {
105 run = rl->table_run[code] + 1;
106 level = rl->table_level[code] * qmul + qadd;
107 if (code >= rl->last) run += 192;
108 }
109 }
110 rl->rl_vlc[q][i].len = len;
111 rl->rl_vlc[q][i].level = level;
112 rl->rl_vlc[q][i].run = run;
113 }
114 }
115 }