Libav
rl.c
Go to the documentation of this file.
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 #include <string.h>
21 
22 #include "libavutil/attributes.h"
23 #include "libavutil/mem.h"
24 
25 #include "rl.h"
26 
28 {
29  int i;
30 
31  for (i = 0; i < 2; i++) {
32  av_freep(&rl->max_run[i]);
33  av_freep(&rl->max_level[i]);
34  av_freep(&rl->index_run[i]);
35  }
36 }
37 
39  uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
40 {
41  int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
42  uint8_t index_run[MAX_RUN + 1];
43  int last, run, level, start, end, i;
44 
45  /* If table is static, we can quit if rl->max_level[0] is not NULL */
46  if (static_store && rl->max_level[0])
47  return 0;
48 
49  /* compute max_level[], max_run[] and index_run[] */
50  for (last = 0; last < 2; last++) {
51  if (last == 0) {
52  start = 0;
53  end = rl->last;
54  } else {
55  start = rl->last;
56  end = rl->n;
57  }
58 
59  memset(max_level, 0, MAX_RUN + 1);
60  memset(max_run, 0, MAX_LEVEL + 1);
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  if (static_store)
73  rl->max_level[last] = static_store[last];
74  else {
75  rl->max_level[last] = av_malloc(MAX_RUN + 1);
76  if (!rl->max_level[last])
77  goto fail;
78  }
79  memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
80  if (static_store)
81  rl->max_run[last] = static_store[last] + MAX_RUN + 1;
82  else {
83  rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
84  if (!rl->max_run[last])
85  goto fail;
86  }
87  memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
88  if (static_store)
89  rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
90  else {
91  rl->index_run[last] = av_malloc(MAX_RUN + 1);
92  if (!rl->index_run[last])
93  goto fail;
94  }
95  memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
96  }
97  return 0;
98 
99 fail:
100  ff_rl_free(rl);
101  return AVERROR(ENOMEM);
102 }
103 
105 {
106  int i, q;
107 
108  for (q = 0; q < 32; q++) {
109  int qmul = q * 2;
110  int qadd = (q - 1) | 1;
111 
112  if (q == 0) {
113  qmul = 1;
114  qadd = 0;
115  }
116  for (i = 0; i < rl->vlc.table_size; i++) {
117  int code = rl->vlc.table[i][0];
118  int len = rl->vlc.table[i][1];
119  int level, run;
120 
121  if (len == 0) { // illegal code
122  run = 66;
123  level = MAX_LEVEL;
124  } else if (len < 0) { // more bits needed
125  run = 0;
126  level = code;
127  } else {
128  if (code == rl->n) { // esc
129  run = 66;
130  level = 0;
131  } else {
132  run = rl->table_run[code] + 1;
133  level = rl->table_level[code] * qmul + qadd;
134  if (code >= rl->last) run += 192;
135  }
136  }
137  rl->rl_vlc[q][i].len = len;
138  rl->rl_vlc[q][i].level = level;
139  rl->rl_vlc[q][i].run = run;
140  }
141  }
142 }
int last
number of values for last = 0
Definition: rl.h:41
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int table_size
Definition: vlc.h:29
memory handling functions
const int8_t * table_level
Definition: rl.h:44
uint8_t run
Definition: svq3.c:203
RLTable.
Definition: rl.h:39
Macro definitions for various function/variable attributes.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:47
uint8_t
#define av_cold
Definition: attributes.h:66
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:46
#define MAX_LEVEL
Definition: rl.h:36
#define AVERROR(e)
Definition: error.h:43
av_cold void ff_rl_init_vlc(RLTable *rl)
Definition: rl.c:104
rl header.
VLC vlc
decoding only deprecated FIXME remove
Definition: rl.h:48
int8_t len
Definition: vlc.h:34
#define fail()
Definition: checkasm.h:80
int n
number of entries of table_vlc minus 1
Definition: rl.h:40
const int8_t * table_run
Definition: rl.h:43
void ff_rl_free(RLTable *rl)
Free the contents of a dynamically allocated table.
Definition: rl.c:27
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:49
av_cold int ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: rl.c:38
uint8_t * index_run[2]
encoding only
Definition: rl.h:45
uint8_t level
Definition: svq3.c:204
uint8_t run
Definition: vlc.h:35
#define MAX_RUN
Definition: rl.h:35
int len
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int16_t level
Definition: vlc.h:33