FFmpeg
vorbis.c
Go to the documentation of this file.
1 /**
2  * @file
3  * Common code for Vorbis I encoder and decoder
4  * @author Denes Balatoni ( dbalatoni programozo hu )
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * Common code for Vorbis I encoder and decoder
26  * @author Denes Balatoni ( dbalatoni programozo hu )
27  */
28 
29 #include "libavutil/common.h"
30 
31 #include "avcodec.h"
32 #include "vorbis.h"
33 #include "vorbis_data.h"
34 
35 
36 /* Helper functions */
37 
38 // x^(1/n)
39 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
40 {
41  unsigned int ret = 0, i, j;
42 
43  do {
44  ++ret;
45  for (i = 0, j = ret; i < n - 1; i++)
46  j *= ret;
47  } while (j <= x);
48 
49  return ret - 1;
50 }
51 
52 // Generate vlc codes from vorbis huffman code lengths
53 
54 // the two bits[p] > 32 checks should be redundant, all calling code should
55 // already ensure that, but since it allows overwriting the stack it seems
56 // reasonable to check redundantly.
57 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
58 {
59  uint32_t exit_at_level[33] = { 404 };
60  unsigned i, j, p, code;
61 
62  for (p = 0; (p < num) && (bits[p] == 0); ++p)
63  ;
64  if (p == num)
65  return 0;
66 
67  codes[p] = 0;
68  if (bits[p] > 32)
69  return AVERROR_INVALIDDATA;
70  for (i = 0; i < bits[p]; ++i)
71  exit_at_level[i+1] = 1u << i;
72 
73  ++p;
74 
75  for (i = p; (i < num) && (bits[i] == 0); ++i)
76  ;
77  if (i == num)
78  return 0;
79 
80  for (; p < num; ++p) {
81  if (bits[p] > 32)
82  return AVERROR_INVALIDDATA;
83  if (bits[p] == 0)
84  continue;
85  // find corresponding exit(node which the tree can grow further from)
86  for (i = bits[p]; i > 0; --i)
87  if (exit_at_level[i])
88  break;
89  if (!i) // overspecified tree
90  return AVERROR_INVALIDDATA;
91  code = exit_at_level[i];
92  exit_at_level[i] = 0;
93  // construct code (append 0s to end) and introduce new exits
94  for (j = i + 1 ;j <= bits[p]; ++j)
95  exit_at_level[j] = code + (1u << (j - 1));
96  codes[p] = code;
97  }
98 
99  //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
100  for (p = 1; p < 33; p++)
101  if (exit_at_level[p])
102  return AVERROR_INVALIDDATA;
103 
104  return 0;
105 }
106 
109 {
110  int i;
111  list[0].sort = 0;
112  list[1].sort = 1;
113  for (i = 2; i < values; i++) {
114  int j;
115  list[i].low = 0;
116  list[i].high = 1;
117  list[i].sort = i;
118  for (j = 2; j < i; j++) {
119  int tmp = list[j].x;
120  if (tmp < list[i].x) {
121  if (tmp > list[list[i].low].x)
122  list[i].low = j;
123  } else {
124  if (tmp < list[list[i].high].x)
125  list[i].high = j;
126  }
127  }
128  }
129  for (i = 0; i < values - 1; i++) {
130  int j;
131  for (j = i + 1; j < values; j++) {
132  if (list[i].x == list[j].x) {
133  av_log(avctx, AV_LOG_ERROR,
134  "Duplicate value found in floor 1 X coordinates\n");
135  return AVERROR_INVALIDDATA;
136  }
137  if (list[list[i].sort].x > list[list[j].sort].x) {
138  int tmp = list[i].sort;
139  list[i].sort = list[j].sort;
140  list[j].sort = tmp;
141  }
142  }
143  }
144  return 0;
145 }
146 
147 static inline void render_line_unrolled(intptr_t x, int y, int x1,
148  intptr_t sy, int ady, int adx,
149  float *buf)
150 {
151  int err = -adx;
152  x -= x1 - 1;
153  buf += x1 - 1;
154  while (++x < 0) {
155  err += ady;
156  if (err >= 0) {
157  err += ady - adx;
158  y += sy;
160  }
162  }
163  if (x <= 0) {
164  if (err + ady >= 0)
165  y += sy;
167  }
168 }
169 
170 static void render_line(int x0, int y0, int x1, int y1, float *buf)
171 {
172  int dy = y1 - y0;
173  int adx = x1 - x0;
174  int ady = FFABS(dy);
175  int sy = dy < 0 ? -1 : 1;
177  if (ady*2 <= adx) { // optimized common case
178  render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
179  } else {
180  int base = dy / adx;
181  int x = x0;
182  int y = y0;
183  int err = -adx;
184  ady -= FFABS(base) * adx;
185  while (++x < x1) {
186  y += base;
187  err += ady;
188  if (err >= 0) {
189  err -= adx;
190  y += sy;
191  }
193  }
194  }
195 }
196 
198  uint16_t *y_list, int *flag,
199  int multiplier, float *out, int samples)
200 {
201  int lx, ly, i;
202  lx = 0;
203  ly = y_list[0] * multiplier;
204  for (i = 1; i < values; i++) {
205  int pos = list[i].sort;
206  if (flag[pos]) {
207  int x1 = list[pos].x;
208  int y1 = y_list[pos] * multiplier;
209  if (lx < samples)
210  render_line(lx, ly, FFMIN(x1,samples), y1, out);
211  lx = x1;
212  ly = y1;
213  }
214  if (lx >= samples)
215  break;
216  }
217  if (lx < samples)
218  render_line(lx, ly, samples, ly, out);
219 }
render_line_unrolled
static void render_line_unrolled(intptr_t x, int y, int x1, intptr_t sy, int ady, int adx, float *buf)
Definition: vorbis.c:147
out
FILE * out
Definition: movenc.c:54
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:262
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
base
uint8_t base
Definition: vp3data.h:128
vorbis_data.h
ff_vorbis_nth_root
unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
Definition: vorbis.c:39
ff_vorbis_floor1_render_list
void ff_vorbis_floor1_render_list(vorbis_floor1_entry *list, int values, uint16_t *y_list, int *flag, int multiplier, float *out, int samples)
Definition: vorbis.c:197
ff_vorbis_floor1_inverse_db_table
const float ff_vorbis_floor1_inverse_db_table[256]
Definition: vorbis_data.c:2131
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
vorbis_floor1_entry
Definition: vorbis.h:28
bits
uint8_t bits
Definition: vp3data.h:128
ff_vorbis_len2vlc
int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
Definition: vorbis.c:57
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:64
list
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining list
Definition: filter_design.txt:25
ff_vorbis_ready_floor1_list
int ff_vorbis_ready_floor1_list(AVCodecContext *avctx, vorbis_floor1_entry *list, int values)
Definition: vorbis.c:107
flag
#define flag(name)
Definition: cbs_av1.c:553
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
vorbis.h
render_line
static void render_line(int x0, int y0, int x1, int y1, float *buf)
Definition: vorbis.c:170
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
avcodec.h
ret
ret
Definition: filter_design.txt:187
pos
unsigned int pos
Definition: spdifenc.c:413
AVCodecContext
main external API structure.
Definition: avcodec.h:426
values
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return values
Definition: filter_design.txt:263
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
av_clip_uint8
#define av_clip_uint8
Definition: common.h:101
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61