FFmpeg
rl.c
Go to the documentation of this file.
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 
24 #include "rl.h"
25 
26 av_cold void ff_rl_init_level_run(uint8_t max_level[MAX_LEVEL + 1],
27  uint8_t index_run[MAX_RUN + 1],
28  const uint8_t table_run[/* n */],
29  const uint8_t table_level[/* n*/],
30  int n)
31 {
32  memset(index_run, n, MAX_RUN + 1);
33  for (int i = 0; i < n; i++) {
34  int run = table_run[i];
35  int level = table_level[i];
36  if (index_run[run] == n)
37  index_run[run] = i;
38  if (level > max_level[run])
39  max_level[run] = level;
40  }
41 }
42 
44  uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
45 {
46  int last, run, level, start, end, i;
47 
48  /* compute max_level[], max_run[] and index_run[] */
49  for (last = 0; last < 2; last++) {
50  int8_t *max_level = static_store[last];
51  int8_t *max_run = static_store[last] + MAX_RUN + 1;
52  uint8_t *index_run = static_store[last] + MAX_RUN + 1 + MAX_LEVEL + 1;
53  if (last == 0) {
54  start = 0;
55  end = rl->last;
56  } else {
57  start = rl->last;
58  end = rl->n;
59  }
60 
61  memset(index_run, rl->n, MAX_RUN + 1);
62  for (i = start; i < end; i++) {
63  run = rl->table_run[i];
64  level = rl->table_level[i];
65  if (index_run[run] == rl->n)
66  index_run[run] = i;
67  if (level > max_level[run])
68  max_level[run] = level;
69  if (run > max_run[level])
70  max_run[level] = run;
71  }
72  rl->max_level[last] = max_level;
73  rl->max_run[last] = max_run;
74  rl->index_run[last] = index_run;
75  }
76 }
77 
78 av_cold void ff_rl_init_vlc(RLTable *rl, unsigned static_size)
79 {
80  VLCElem *vlc;
81 
82  ff_vlc_init_table_sparse(rl->rl_vlc[0], static_size, 9, rl->n + 1,
83  &rl->table_vlc[0][1], 4, 2,
84  &rl->table_vlc[0][0], 4, 2,
85  NULL, 0, 0, 0);
86 
87  vlc = rl->rl_vlc[0];
88 
89  // We count down to avoid trashing the first RL-VLC
90  for (int q = 32; --q >= 0;) {
91  int qmul = q * 2;
92  int qadd = (q - 1) | 1;
93 
94  if (!rl->rl_vlc[q])
95  continue;
96 
97  if (q == 0) {
98  qmul = 1;
99  qadd = 0;
100  }
101  for (unsigned i = 0; i < static_size; i++) {
102  int idx = vlc[i].sym;
103  int len = vlc[i].len;
104  int level, run;
105 
106  if (len == 0) { // illegal code
107  run = 66;
108  level = MAX_LEVEL;
109  } else if (len < 0) { // more bits needed
110  run = 0;
111  level = idx;
112  } else {
113  if (idx == rl->n) { // esc
114  run = 66;
115  level = 0;
116  } else {
117  run = rl->table_run[idx] + 1;
118  level = rl->table_level[idx] * qmul + qadd;
119  if (idx >= rl->last) run += 192;
120  }
121  }
122  rl->rl_vlc[q][i].len8 = len;
123  rl->rl_vlc[q][i].level = level;
124  rl->rl_vlc[q][i].run = run;
125  }
126  }
127 }
VLCElem::level
int16_t level
Definition: vlc.h:41
level
uint8_t level
Definition: svq3.c:205
RLTable::index_run
uint8_t * index_run[2]
encoding only
Definition: rl.h:45
ff_rl_init_level_run
av_cold void ff_rl_init_level_run(uint8_t max_level[MAX_LEVEL+1], uint8_t index_run[MAX_RUN+1], const uint8_t table_run[], const uint8_t table_level[], int n)
Initialize max_level and index_run from table_run and table_level; this is equivalent to initializing...
Definition: rl.c:26
MAX_RUN
#define MAX_RUN
Definition: rl.h:35
VLCElem::len
VLCBaseType len
Definition: vlc.h:37
ff_rl_init_vlc
av_cold void ff_rl_init_vlc(RLTable *rl, unsigned static_size)
Initialize rl_vlc from n, last, table_vlc, table_run and table_level.
Definition: rl.c:78
RLTable
RLTable.
Definition: rl.h:39
VLCElem::sym
VLCBaseType sym
Definition: vlc.h:36
RLTable::n
int n
number of entries of table_vlc minus 1
Definition: rl.h:40
av_cold
#define av_cold
Definition: attributes.h:90
RLTable::max_level
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:46
ff_rl_init
av_cold void ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Initialize index_run, max_level and max_run from n, last, table_vlc, table_run and table_level.
Definition: rl.c:43
VLCElem::run
uint8_t run
Definition: vlc.h:43
NULL
#define NULL
Definition: coverity.c:32
run
uint8_t run
Definition: svq3.c:204
RLTable::table_vlc
const uint16_t(* table_vlc)[2]
Definition: rl.h:42
MAX_LEVEL
#define MAX_LEVEL
Definition: rl.h:36
RLTable::table_level
const int8_t * table_level
Definition: rl.h:44
VLCElem
Definition: vlc.h:32
VLCElem::len8
int8_t len8
Definition: vlc.h:42
attributes.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
RLTable::max_run
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:47
len
int len
Definition: vorbis_enc_data.h:426
RLTable::last
int last
number of values for last = 0
Definition: rl.h:41
rl.h
ff_vlc_init_table_sparse
av_cold void ff_vlc_init_table_sparse(VLCElem table[], int table_size, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: vlc.c:384
RLTable::table_run
const int8_t * table_run
Definition: rl.h:43
RLTable::rl_vlc
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48