2 * This file is part of Libav.
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.
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.
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
21 #include "libavutil/attributes.h"
22 #include "libavutil/mem.h"
26 void ff_rl_free(RLTable *rl)
30 for (i = 0; i < 2; i++) {
31 av_freep(&rl->max_run[i]);
32 av_freep(&rl->max_level[i]);
33 av_freep(&rl->index_run[i]);
37 av_cold int ff_rl_init(RLTable *rl,
38 uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
40 int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
41 uint8_t index_run[MAX_RUN + 1];
42 int last, run, level, start, end, i;
44 /* If table is static, we can quit if rl->max_level[0] is not NULL */
45 if (static_store && rl->max_level[0])
48 /* compute max_level[], max_run[] and index_run[] */
49 for (last = 0; last < 2; last++) {
58 memset(max_level, 0, MAX_RUN + 1);
59 memset(max_run, 0, MAX_LEVEL + 1);
60 memset(index_run, rl->n, MAX_RUN + 1);
61 for (i = start; i < end; i++) {
62 run = rl->table_run[i];
63 level = rl->table_level[i];
64 if (index_run[run] == rl->n)
66 if (level > max_level[run])
67 max_level[run] = level;
68 if (run > max_run[level])
72 rl->max_level[last] = static_store[last];
74 rl->max_level[last] = av_malloc(MAX_RUN + 1);
75 if (!rl->max_level[last])
78 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
80 rl->max_run[last] = static_store[last] + MAX_RUN + 1;
82 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
83 if (!rl->max_run[last])
86 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
88 rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
90 rl->index_run[last] = av_malloc(MAX_RUN + 1);
91 if (!rl->index_run[last])
94 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
100 return AVERROR(ENOMEM);
103 av_cold void ff_rl_init_vlc(RLTable *rl)
107 for (q = 0; q < 32; q++) {
109 int qadd = (q - 1) | 1;
115 for (i = 0; i < rl->vlc.table_size; i++) {
116 int code = rl->vlc.table[i][0];
117 int len = rl->vlc.table[i][1];
120 if (len == 0) { // illegal code
123 } else if (len < 0) { // more bits needed
127 if (code == rl->n) { // esc
131 run = rl->table_run[code] + 1;
132 level = rl->table_level[code] * qmul + qadd;
133 if (code >= rl->last) run += 192;
136 rl->rl_vlc[q][i].len = len;
137 rl->rl_vlc[q][i].level = level;
138 rl->rl_vlc[q][i].run = run;