2005-05-17 19:02:43 +00:00
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2007-05-10 15:24:01 +00:00
|
|
|
* Common code for Vorbis I encoder and decoder
|
2005-05-17 19:02:43 +00:00
|
|
|
* @author Denes Balatoni ( dbalatoni programozo hu )
|
2009-09-29 11:43:58 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2005-05-21 01:17:20 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 15:30:46 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2005-05-21 01:17:20 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2005-05-21 01:17:20 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 15:30:46 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-12 22:43:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-05-17 19:02:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-14 01:11:19 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Common code for Vorbis I encoder and decoder
|
|
|
|
* @author Denes Balatoni ( dbalatoni programozo hu )
|
|
|
|
*/
|
|
|
|
|
2011-12-22 15:33:31 +00:00
|
|
|
#define BITSTREAM_READER_LE
|
2005-05-17 19:02:43 +00:00
|
|
|
#include "avcodec.h"
|
2009-04-13 16:20:26 +00:00
|
|
|
#include "get_bits.h"
|
2005-05-17 19:02:43 +00:00
|
|
|
|
|
|
|
#include "vorbis.h"
|
2006-09-30 18:47:15 +00:00
|
|
|
|
|
|
|
|
2005-05-17 19:02:43 +00:00
|
|
|
/* Helper functions */
|
|
|
|
|
2009-09-28 21:13:47 +00:00
|
|
|
// x^(1/n)
|
|
|
|
unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
|
|
|
|
{
|
2009-09-29 11:43:58 +00:00
|
|
|
unsigned int ret = 0, i, j;
|
2005-05-17 19:02:43 +00:00
|
|
|
|
2007-05-10 15:24:01 +00:00
|
|
|
do {
|
|
|
|
++ret;
|
2009-09-29 11:43:58 +00:00
|
|
|
for (i = 0, j = ret; i < n - 1; i++)
|
|
|
|
j *= ret;
|
|
|
|
} while (j <= x);
|
2005-05-17 19:02:43 +00:00
|
|
|
|
2008-05-06 09:16:36 +00:00
|
|
|
return ret - 1;
|
2005-05-17 19:02:43 +00:00
|
|
|
}
|
|
|
|
|
2007-05-10 15:24:01 +00:00
|
|
|
// Generate vlc codes from vorbis huffman code lengths
|
2005-05-17 19:02:43 +00:00
|
|
|
|
2009-07-08 19:39:23 +00:00
|
|
|
// the two bits[p] > 32 checks should be redundant, all calling code should
|
|
|
|
// already ensure that, but since it allows overwriting the stack it seems
|
|
|
|
// reasonable to check redundantly.
|
2011-04-29 18:12:48 +00:00
|
|
|
int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
|
2009-09-28 21:13:47 +00:00
|
|
|
{
|
2011-04-29 14:08:20 +00:00
|
|
|
uint32_t exit_at_level[33] = { 404 };
|
2005-05-17 19:02:43 +00:00
|
|
|
|
2011-04-29 18:12:48 +00:00
|
|
|
unsigned i, j, p, code;
|
2005-05-17 19:02:43 +00:00
|
|
|
|
2011-06-02 20:45:15 +00:00
|
|
|
#ifdef DEBUG
|
2007-05-10 15:24:01 +00:00
|
|
|
GetBitContext gb;
|
2006-02-04 08:55:34 +00:00
|
|
|
#endif
|
2005-05-17 19:02:43 +00:00
|
|
|
|
2009-09-29 11:43:58 +00:00
|
|
|
for (p = 0; (bits[p] == 0) && (p < num); ++p)
|
2009-09-28 21:46:22 +00:00
|
|
|
;
|
2009-09-29 11:43:58 +00:00
|
|
|
if (p == num) {
|
2007-05-10 15:24:01 +00:00
|
|
|
// av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n");
|
|
|
|
return 0;
|
2005-05-17 19:02:43 +00:00
|
|
|
}
|
|
|
|
|
2009-09-29 11:43:58 +00:00
|
|
|
codes[p] = 0;
|
2009-09-28 21:46:22 +00:00
|
|
|
if (bits[p] > 32)
|
|
|
|
return 1;
|
2009-09-29 11:43:58 +00:00
|
|
|
for (i = 0; i < bits[p]; ++i)
|
|
|
|
exit_at_level[i+1] = 1 << i;
|
2005-05-17 19:02:43 +00:00
|
|
|
|
2011-06-02 20:45:15 +00:00
|
|
|
#ifdef DEBUG
|
2011-04-29 18:12:48 +00:00
|
|
|
av_log(NULL, AV_LOG_INFO, " %u. of %u code len %d code %d - ", p, num, bits[p], codes[p]);
|
|
|
|
init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
|
2009-09-29 11:43:58 +00:00
|
|
|
for (i = 0; i < bits[p]; ++i)
|
2007-05-10 15:24:01 +00:00
|
|
|
av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
|
|
|
|
av_log(NULL, AV_LOG_INFO, "\n");
|
|
|
|
#endif
|
2005-05-17 19:02:43 +00:00
|
|
|
|
2007-05-10 15:24:01 +00:00
|
|
|
++p;
|
2005-05-17 19:02:43 +00:00
|
|
|
|
2009-09-29 11:43:58 +00:00
|
|
|
for (; p < num; ++p) {
|
2009-09-28 21:46:22 +00:00
|
|
|
if (bits[p] > 32)
|
2009-09-29 11:43:58 +00:00
|
|
|
return 1;
|
|
|
|
if (bits[p] == 0)
|
|
|
|
continue;
|
2007-05-10 15:24:01 +00:00
|
|
|
// find corresponding exit(node which the tree can grow further from)
|
2009-09-29 11:43:58 +00:00
|
|
|
for (i = bits[p]; i > 0; --i)
|
2009-09-28 21:46:22 +00:00
|
|
|
if (exit_at_level[i])
|
|
|
|
break;
|
|
|
|
if (!i) // overspecified tree
|
2009-09-29 11:43:58 +00:00
|
|
|
return 1;
|
|
|
|
code = exit_at_level[i];
|
|
|
|
exit_at_level[i] = 0;
|
2007-05-10 15:24:01 +00:00
|
|
|
// construct code (append 0s to end) and introduce new exits
|
2009-09-29 11:43:58 +00:00
|
|
|
for (j = i + 1 ;j <= bits[p]; ++j)
|
|
|
|
exit_at_level[j] = code + (1 << (j - 1));
|
|
|
|
codes[p] = code;
|
2006-02-04 08:55:34 +00:00
|
|
|
|
2011-06-02 20:45:15 +00:00
|
|
|
#ifdef DEBUG
|
2007-05-10 15:24:01 +00:00
|
|
|
av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
|
2011-04-29 14:08:20 +00:00
|
|
|
init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
|
2009-09-29 11:43:58 +00:00
|
|
|
for (i = 0; i < bits[p]; ++i)
|
2007-05-10 15:24:01 +00:00
|
|
|
av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
|
|
|
|
av_log(NULL, AV_LOG_INFO, "\n");
|
2006-02-04 08:55:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-10 15:24:01 +00:00
|
|
|
//no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
|
2009-09-29 11:43:58 +00:00
|
|
|
for (p = 1; p < 33; p++)
|
2009-09-28 21:46:22 +00:00
|
|
|
if (exit_at_level[p])
|
|
|
|
return 1;
|
2005-05-17 19:02:43 +00:00
|
|
|
|
2006-02-04 08:55:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-10-01 17:38:07 +00:00
|
|
|
|
2009-09-28 21:13:47 +00:00
|
|
|
void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values)
|
|
|
|
{
|
2006-08-03 03:18:47 +00:00
|
|
|
int i;
|
2007-05-10 15:24:01 +00:00
|
|
|
list[0].sort = 0;
|
|
|
|
list[1].sort = 1;
|
|
|
|
for (i = 2; i < values; i++) {
|
|
|
|
int j;
|
2009-09-29 11:43:58 +00:00
|
|
|
list[i].low = 0;
|
2007-05-10 15:24:01 +00:00
|
|
|
list[i].high = 1;
|
|
|
|
list[i].sort = i;
|
|
|
|
for (j = 2; j < i; j++) {
|
|
|
|
int tmp = list[j].x;
|
|
|
|
if (tmp < list[i].x) {
|
2009-09-28 21:46:22 +00:00
|
|
|
if (tmp > list[list[i].low].x)
|
2009-09-29 11:43:58 +00:00
|
|
|
list[i].low = j;
|
2006-08-03 03:18:47 +00:00
|
|
|
} else {
|
2009-09-28 21:46:22 +00:00
|
|
|
if (tmp < list[list[i].high].x)
|
|
|
|
list[i].high = j;
|
2007-05-10 15:24:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < values - 1; i++) {
|
|
|
|
int j;
|
|
|
|
for (j = i + 1; j < values; j++) {
|
|
|
|
if (list[list[i].sort].x > list[list[j].sort].x) {
|
|
|
|
int tmp = list[i].sort;
|
|
|
|
list[i].sort = list[j].sort;
|
|
|
|
list[j].sort = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-11 01:01:26 +00:00
|
|
|
static inline void render_line_unrolled(intptr_t x, int y, int x1,
|
2009-09-28 21:13:47 +00:00
|
|
|
intptr_t sy, int ady, int adx,
|
2009-09-29 11:43:58 +00:00
|
|
|
float *buf)
|
2009-09-28 21:13:47 +00:00
|
|
|
{
|
2009-08-27 13:41:11 +00:00
|
|
|
int err = -adx;
|
2009-09-29 11:43:58 +00:00
|
|
|
x -= x1 - 1;
|
|
|
|
buf += x1 - 1;
|
2009-08-27 13:41:11 +00:00
|
|
|
while (++x < 0) {
|
|
|
|
err += ady;
|
|
|
|
if (err >= 0) {
|
|
|
|
err += ady - adx;
|
2009-09-29 11:43:58 +00:00
|
|
|
y += sy;
|
2012-01-11 01:01:26 +00:00
|
|
|
buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
|
2009-08-27 13:41:11 +00:00
|
|
|
}
|
2012-01-11 01:01:26 +00:00
|
|
|
buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
|
2009-08-27 13:41:11 +00:00
|
|
|
}
|
|
|
|
if (x <= 0) {
|
|
|
|
if (err + ady >= 0)
|
|
|
|
y += sy;
|
2012-01-11 01:01:26 +00:00
|
|
|
buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
|
2009-08-27 13:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-11 01:01:26 +00:00
|
|
|
static void render_line(int x0, int y0, int x1, int y1, float *buf)
|
2009-09-28 21:13:47 +00:00
|
|
|
{
|
2009-09-29 11:43:58 +00:00
|
|
|
int dy = y1 - y0;
|
2007-05-10 15:24:01 +00:00
|
|
|
int adx = x1 - x0;
|
2009-08-27 13:41:11 +00:00
|
|
|
int ady = FFABS(dy);
|
2009-09-29 11:43:58 +00:00
|
|
|
int sy = dy < 0 ? -1 : 1;
|
2012-01-11 01:01:26 +00:00
|
|
|
buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
|
2009-09-29 11:43:58 +00:00
|
|
|
if (ady*2 <= adx) { // optimized common case
|
2009-08-27 13:41:11 +00:00
|
|
|
render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
|
|
|
|
} else {
|
2012-01-05 20:25:41 +00:00
|
|
|
int base = dy / adx;
|
|
|
|
int x = x0;
|
2012-01-11 01:01:26 +00:00
|
|
|
int y = y0;
|
2012-01-05 20:25:41 +00:00
|
|
|
int err = -adx;
|
2009-08-27 13:41:29 +00:00
|
|
|
ady -= FFABS(base) * adx;
|
|
|
|
while (++x < x1) {
|
|
|
|
y += base;
|
|
|
|
err += ady;
|
|
|
|
if (err >= 0) {
|
|
|
|
err -= adx;
|
2009-09-29 11:43:58 +00:00
|
|
|
y += sy;
|
2009-08-27 13:41:29 +00:00
|
|
|
}
|
2012-01-11 01:01:26 +00:00
|
|
|
buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
|
2006-08-03 03:18:47 +00:00
|
|
|
}
|
2009-08-27 13:41:11 +00:00
|
|
|
}
|
2006-08-03 03:18:47 +00:00
|
|
|
}
|
|
|
|
|
2009-09-28 21:13:47 +00:00
|
|
|
void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
|
2011-04-29 14:08:20 +00:00
|
|
|
uint16_t *y_list, int *flag,
|
2009-09-29 11:43:58 +00:00
|
|
|
int multiplier, float *out, int samples)
|
2009-09-28 21:13:47 +00:00
|
|
|
{
|
2012-01-11 01:01:26 +00:00
|
|
|
int lx, ly, i;
|
2007-05-10 15:24:01 +00:00
|
|
|
lx = 0;
|
|
|
|
ly = y_list[0] * multiplier;
|
|
|
|
for (i = 1; i < values; i++) {
|
|
|
|
int pos = list[i].sort;
|
|
|
|
if (flag[pos]) {
|
2008-02-09 13:53:15 +00:00
|
|
|
int x1 = list[pos].x;
|
|
|
|
int y1 = y_list[pos] * multiplier;
|
|
|
|
if (lx < samples)
|
|
|
|
render_line(lx, ly, FFMIN(x1,samples), y1, out);
|
|
|
|
lx = x1;
|
|
|
|
ly = y1;
|
2005-05-17 19:02:43 +00:00
|
|
|
}
|
2009-09-28 21:46:22 +00:00
|
|
|
if (lx >= samples)
|
|
|
|
break;
|
2005-05-17 19:02:43 +00:00
|
|
|
}
|
2009-09-28 21:46:22 +00:00
|
|
|
if (lx < samples)
|
|
|
|
render_line(lx, ly, samples, ly, out);
|
2005-05-17 19:02:43 +00:00
|
|
|
}
|