libavcodec/vorbis.c
0bde73d9
 /**
ba87f080
  * @file
07c55d8e
  * Common code for Vorbis I encoder and decoder
0bde73d9
  * @author Denes Balatoni  ( dbalatoni programozo hu )
a7adcf29
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
84a96d6a
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
84a96d6a
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
84a96d6a
  * 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
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0bde73d9
  */
 
6168781f
 /**
  * @file
  * Common code for Vorbis I encoder and decoder
  * @author Denes Balatoni  ( dbalatoni programozo hu )
  */
 
aaf47bcd
 #define BITSTREAM_READER_LE
0bde73d9
 #include "avcodec.h"
9106a698
 #include "get_bits.h"
0bde73d9
 
 #include "vorbis.h"
cc0ba5a5
 
 
0bde73d9
 /* Helper functions */
 
5e56b30e
 // x^(1/n)
 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
 {
a7adcf29
     unsigned int ret = 0, i, j;
0bde73d9
 
07c55d8e
     do {
         ++ret;
a7adcf29
         for (i = 0, j = ret; i < n - 1; i++)
             j *= ret;
     } while (j <= x);
0bde73d9
 
ccd425e7
     return ret - 1;
0bde73d9
 }
 
07c55d8e
 // Generate vlc codes from vorbis huffman code lengths
0bde73d9
 
5e039e1b
 // 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.
cf3ac543
 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
5e56b30e
 {
0a6b1a9f
     uint32_t exit_at_level[33] = { 404 };
0bde73d9
 
cf3ac543
     unsigned i, j, p, code;
0bde73d9
 
f190f676
 #ifdef DEBUG
07c55d8e
     GetBitContext gb;
43eb5908
 #endif
0bde73d9
 
a7adcf29
     for (p = 0; (bits[p] == 0) && (p < num); ++p)
0b8e7ab0
         ;
6f6b0311
     if (p == num)
07c55d8e
         return 0;
0bde73d9
 
a7adcf29
     codes[p] = 0;
0b8e7ab0
     if (bits[p] > 32)
         return 1;
a7adcf29
     for (i = 0; i < bits[p]; ++i)
         exit_at_level[i+1] = 1 << i;
0bde73d9
 
f190f676
 #ifdef DEBUG
cf3ac543
     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]);
a7adcf29
     for (i = 0; i < bits[p]; ++i)
07c55d8e
         av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
     av_log(NULL, AV_LOG_INFO, "\n");
 #endif
0bde73d9
 
07c55d8e
     ++p;
0bde73d9
 
a7adcf29
     for (; p < num; ++p) {
0b8e7ab0
         if (bits[p] > 32)
a7adcf29
              return 1;
         if (bits[p] == 0)
              continue;
07c55d8e
         // find corresponding exit(node which the tree can grow further from)
a7adcf29
         for (i = bits[p]; i > 0; --i)
0b8e7ab0
             if (exit_at_level[i])
                 break;
         if (!i) // overspecified tree
a7adcf29
              return 1;
         code = exit_at_level[i];
         exit_at_level[i] = 0;
07c55d8e
         // construct code (append 0s to end) and introduce new exits
a7adcf29
         for (j = i + 1 ;j <= bits[p]; ++j)
             exit_at_level[j] = code + (1 << (j - 1));
         codes[p] = code;
43eb5908
 
f190f676
 #ifdef DEBUG
07c55d8e
         av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
0a6b1a9f
         init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
a7adcf29
         for (i = 0; i < bits[p]; ++i)
07c55d8e
             av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
         av_log(NULL, AV_LOG_INFO, "\n");
43eb5908
 #endif
 
     }
 
07c55d8e
     //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
a7adcf29
     for (p = 1; p < 33; p++)
0b8e7ab0
         if (exit_at_level[p])
             return 1;
0bde73d9
 
43eb5908
     return 0;
 }
7f09a560
 
e951b6d9
 int ff_vorbis_ready_floor1_list(AVCodecContext *avctx,
ecf79c4d
                                 vorbis_floor1_entry *list, int values)
5e56b30e
 {
2dac4acf
     int i;
07c55d8e
     list[0].sort = 0;
     list[1].sort = 1;
     for (i = 2; i < values; i++) {
         int j;
a7adcf29
         list[i].low  = 0;
07c55d8e
         list[i].high = 1;
         list[i].sort = i;
         for (j = 2; j < i; j++) {
             int tmp = list[j].x;
             if (tmp < list[i].x) {
0b8e7ab0
                 if (tmp > list[list[i].low].x)
a7adcf29
                     list[i].low  =  j;
2dac4acf
             } else {
0b8e7ab0
                 if (tmp < list[list[i].high].x)
                     list[i].high = j;
07c55d8e
             }
         }
     }
     for (i = 0; i < values - 1; i++) {
         int j;
         for (j = i + 1; j < values; j++) {
ecf79c4d
             if (list[i].x == list[j].x) {
e951b6d9
                 av_log(avctx, AV_LOG_ERROR,
ecf79c4d
                        "Duplicate value found in floor 1 X coordinates\n");
                 return AVERROR_INVALIDDATA;
             }
07c55d8e
             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;
             }
         }
     }
ecf79c4d
     return 0;
07c55d8e
 }
 
24947d49
 static inline void render_line_unrolled(intptr_t x, int y, int x1,
5e56b30e
                                         intptr_t sy, int ady, int adx,
a7adcf29
                                         float *buf)
5e56b30e
 {
2f17f9c1
     int err = -adx;
a7adcf29
     x -= x1 - 1;
     buf += x1 - 1;
2f17f9c1
     while (++x < 0) {
         err += ady;
         if (err >= 0) {
             err += ady - adx;
a7adcf29
             y   += sy;
24947d49
             buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
2f17f9c1
         }
24947d49
         buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
2f17f9c1
     }
     if (x <= 0) {
         if (err + ady >= 0)
             y += sy;
24947d49
         buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
2f17f9c1
     }
 }
 
24947d49
 static void render_line(int x0, int y0, int x1, int y1, float *buf)
5e56b30e
 {
a7adcf29
     int dy  = y1 - y0;
07c55d8e
     int adx = x1 - x0;
2f17f9c1
     int ady = FFABS(dy);
a7adcf29
     int sy  = dy < 0 ? -1 : 1;
24947d49
     buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
a7adcf29
     if (ady*2 <= adx) { // optimized common case
2f17f9c1
         render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
     } else {
57cd6d70
         int base  = dy / adx;
         int x     = x0;
24947d49
         int y     = y0;
57cd6d70
         int err   = -adx;
8fc9376c
         ady -= FFABS(base) * adx;
         while (++x < x1) {
             y += base;
             err += ady;
             if (err >= 0) {
                 err -= adx;
a7adcf29
                 y   += sy;
8fc9376c
             }
24947d49
             buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
2dac4acf
         }
2f17f9c1
     }
2dac4acf
 }
 
5e56b30e
 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
0a6b1a9f
                                   uint16_t *y_list, int *flag,
a7adcf29
                                   int multiplier, float *out, int samples)
5e56b30e
 {
24947d49
     int lx, ly, i;
07c55d8e
     lx = 0;
     ly = y_list[0] * multiplier;
     for (i = 1; i < values; i++) {
         int pos = list[i].sort;
         if (flag[pos]) {
e1813a2f
             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;
0bde73d9
         }
0b8e7ab0
         if (lx >= samples)
             break;
0bde73d9
     }
0b8e7ab0
     if (lx < samples)
         render_line(lx, ly, samples, ly, out);
0bde73d9
 }