FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
21 #include "libavutil/attributes.h"
22 #include "libavutil/mem.h"
23 
24 #include "rl.h"
25 
27 {
28  int i;
29 
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]);
34  }
35 }
36 
38  uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
39 {
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;
43 
44  /* If table is static, we can quit if rl->max_level[0] is not NULL */
45  if (static_store && rl->max_level[0])
46  return 0;
47 
48  /* compute max_level[], max_run[] and index_run[] */
49  for (last = 0; last < 2; last++) {
50  if (last == 0) {
51  start = 0;
52  end = rl->last;
53  } else {
54  start = rl->last;
55  end = rl->n;
56  }
57 
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)
65  index_run[run] = i;
66  if (level > max_level[run])
67  max_level[run] = level;
68  if (run > max_run[level])
69  max_run[level] = run;
70  }
71  if (static_store)
72  rl->max_level[last] = static_store[last];
73  else {
74  rl->max_level[last] = av_malloc(MAX_RUN + 1);
75  if (!rl->max_level[last])
76  goto fail;
77  }
78  memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
79  if (static_store)
80  rl->max_run[last] = static_store[last] + MAX_RUN + 1;
81  else {
82  rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
83  if (!rl->max_run[last])
84  goto fail;
85  }
86  memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
87  if (static_store)
88  rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
89  else {
90  rl->index_run[last] = av_malloc(MAX_RUN + 1);
91  if (!rl->index_run[last])
92  goto fail;
93  }
94  memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
95  }
96  return 0;
97 
98 fail:
99  ff_rl_free(rl);
100  return AVERROR(ENOMEM);
101 }
102 
103 av_cold void ff_rl_init_vlc(RLTable *rl, unsigned static_size)
104 {
105  int i, q;
106  VLC_TYPE table[1500][2] = {{0}};
107  VLC vlc = { .table = table, .table_allocated = static_size };
108  av_assert0(static_size <= FF_ARRAY_ELEMS(table));
109  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);
110 
111  for (q = 0; q < 32; q++) {
112  int qmul = q * 2;
113  int qadd = (q - 1) | 1;
114 
115  if (q == 0) {
116  qmul = 1;
117  qadd = 0;
118  }
119  for (i = 0; i < vlc.table_size; i++) {
120  int code = vlc.table[i][0];
121  int len = vlc.table[i][1];
122  int level, run;
123 
124  if (len == 0) { // illegal code
125  run = 66;
126  level = MAX_LEVEL;
127  } else if (len < 0) { // more bits needed
128  run = 0;
129  level = code;
130  } else {
131  if (code == rl->n) { // esc
132  run = 66;
133  level = 0;
134  } else {
135  run = rl->table_run[code] + 1;
136  level = rl->table_level[code] * qmul + qadd;
137  if (code >= rl->last) run += 192;
138  }
139  }
140  rl->rl_vlc[q][i].len = len;
141  rl->rl_vlc[q][i].level = level;
142  rl->rl_vlc[q][i].run = run;
143  }
144  }
145 }
int last
number of values for last = 0
Definition: rl.h:40
int table_size
Definition: get_bits.h:66
memory handling functions
#define VLC_TYPE
Definition: get_bits.h:61
const int8_t * table_level
Definition: rl.h:43
uint8_t run
Definition: svq3.c:149
RLTable.
Definition: rl.h:38
Macro definitions for various function/variable attributes.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:46
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:45
#define MAX_LEVEL
Definition: rl.h:35
#define AVERROR(e)
Definition: error.h:43
static const struct endianess table[]
rl header.
int8_t len
Definition: get_bits.h:71
#define fail()
Definition: checkasm.h:80
Definition: get_bits.h:63
int n
number of entries of table_vlc minus 1
Definition: rl.h:39
const uint16_t(* table_vlc)[2]
Definition: rl.h:41
const int8_t * table_run
Definition: rl.h:42
#define INIT_VLC_USE_NEW_STATIC
Definition: get_bits.h:482
void ff_rl_free(RLTable *rl)
Free the contents of a dynamically allocated table.
Definition: rl.c:26
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:47
#define FF_ARRAY_ELEMS(a)
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:465
av_cold int ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: rl.c:37
uint8_t * index_run[2]
encoding only
Definition: rl.h:44
uint8_t level
Definition: svq3.c:150
uint8_t run
Definition: get_bits.h:72
#define MAX_RUN
Definition: rl.h:34
int len
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
int16_t level
Definition: get_bits.h:70
av_cold void ff_rl_init_vlc(RLTable *rl, unsigned static_size)
Definition: rl.c:103
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553