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 #include "libavutil/avassert.h"
24 
25 #include "rl.h"
26 
27 av_cold void ff_rl_init_level_run(uint8_t max_level[MAX_LEVEL + 1],
28  uint8_t index_run[MAX_RUN + 1],
29  const uint8_t table_run[/* n */],
30  const uint8_t table_level[/* n*/],
31  int n)
32 {
33  memset(index_run, n, MAX_RUN + 1);
34  for (int i = 0; i < n; i++) {
35  int run = table_run[i];
36  int level = table_level[i];
37  if (index_run[run] == n)
38  index_run[run] = i;
39  if (level > max_level[run])
40  max_level[run] = level;
41  }
42 }
43 
45  uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
46 {
47  int last, run, level, start, end, i;
48 
49  /* compute max_level[], max_run[] and index_run[] */
50  for (last = 0; last < 2; last++) {
51  int8_t *max_level = static_store[last];
52  int8_t *max_run = static_store[last] + MAX_RUN + 1;
53  uint8_t *index_run = static_store[last] + MAX_RUN + 1 + MAX_LEVEL + 1;
54  if (last == 0) {
55  start = 0;
56  end = rl->last;
57  } else {
58  start = rl->last;
59  end = rl->n;
60  }
61 
62  memset(index_run, rl->n, MAX_RUN + 1);
63  for (i = start; i < end; i++) {
64  run = rl->table_run[i];
65  level = rl->table_level[i];
66  if (index_run[run] == rl->n)
67  index_run[run] = i;
68  if (level > max_level[run])
69  max_level[run] = level;
70  if (run > max_run[level])
71  max_run[level] = run;
72  }
73  rl->max_level[last] = max_level;
74  rl->max_run[last] = max_run;
75  rl->index_run[last] = index_run;
76  }
77 }
78 
79 av_cold void ff_rl_init_vlc(RLTable *rl, unsigned static_size)
80 {
81  int i, q;
82  VLCElem table[1500] = { 0 };
83  VLC vlc = { .table = table, .table_allocated = static_size };
84  av_assert0(static_size <= FF_ARRAY_ELEMS(table));
85  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);
86 
87  for (q = 0; q < 32; q++) {
88  int qmul = q * 2;
89  int qadd = (q - 1) | 1;
90 
91  if (!rl->rl_vlc[q])
92  return;
93 
94  if (q == 0) {
95  qmul = 1;
96  qadd = 0;
97  }
98  for (i = 0; i < vlc.table_size; i++) {
99  int code = vlc.table[i].sym;
100  int len = vlc.table[i].len;
101  int level, run;
102 
103  if (len == 0) { // illegal code
104  run = 66;
105  level = MAX_LEVEL;
106  } else if (len < 0) { // more bits needed
107  run = 0;
108  level = code;
109  } else {
110  if (code == rl->n) { // esc
111  run = 66;
112  level = 0;
113  } else {
114  run = rl->table_run[code] + 1;
115  level = rl->table_level[code] * qmul + qadd;
116  if (code >= rl->last) run += 192;
117  }
118  }
119  rl->rl_vlc[q][i].len = len;
120  rl->rl_vlc[q][i].level = level;
121  rl->rl_vlc[q][i].run = run;
122  }
123  }
124 }
level
uint8_t level
Definition: svq3.c:204
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:27
MAX_RUN
#define MAX_RUN
Definition: rl.h:35
init_vlc
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:43
table
static const uint16_t table[]
Definition: prosumer.c:205
RL_VLC_ELEM::run
uint8_t run
Definition: vlc.h:40
VLCElem::len
VLCBaseType len
Definition: vlc.h:28
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:79
RLTable
RLTable.
Definition: rl.h:39
VLCElem::sym
VLCBaseType sym
Definition: vlc.h:28
avassert.h
RLTable::n
int n
number of entries of table_vlc minus 1
Definition: rl.h:40
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
RLTable::max_level
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:46
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
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:44
run
uint8_t run
Definition: svq3.c:203
RLTable::table_vlc
const uint16_t(* table_vlc)[2]
Definition: rl.h:42
INIT_VLC_USE_NEW_STATIC
#define INIT_VLC_USE_NEW_STATIC
Definition: vlc.h:100
RL_VLC_ELEM::len
int8_t len
Definition: vlc.h:39
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:27
attributes.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
RLTable::max_run
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:47
len
int len
Definition: vorbis_enc_data.h:426
VLC
Definition: vlc.h:31
RLTable::last
int last
number of values for last = 0
Definition: rl.h:41
VLC::table
VLCElem * table
Definition: vlc.h:33
VLC::table_size
int table_size
Definition: vlc.h:34
rl.h
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
RL_VLC_ELEM::level
int16_t level
Definition: vlc.h:38