libavcodec/vorbis.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of Libav.
00003  *
00004  * Libav is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * Libav is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with Libav; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00017  */
00018 
00025 #define BITSTREAM_READER_LE
00026 #include "avcodec.h"
00027 #include "get_bits.h"
00028 
00029 #include "vorbis.h"
00030 
00031 
00032 /* Helper functions */
00033 
00034 // x^(1/n)
00035 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
00036 {
00037     unsigned int ret = 0, i, j;
00038 
00039     do {
00040         ++ret;
00041         for (i = 0, j = ret; i < n - 1; i++)
00042             j *= ret;
00043     } while (j <= x);
00044 
00045     return ret - 1;
00046 }
00047 
00048 // Generate vlc codes from vorbis huffman code lengths
00049 
00050 // the two bits[p] > 32 checks should be redundant, all calling code should
00051 // already ensure that, but since it allows overwriting the stack it seems
00052 // reasonable to check redundantly.
00053 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
00054 {
00055     uint32_t exit_at_level[33] = { 404 };
00056 
00057     unsigned i, j, p, code;
00058 
00059 #ifdef DEBUG
00060     GetBitContext gb;
00061 #endif
00062 
00063     for (p = 0; (bits[p] == 0) && (p < num); ++p)
00064         ;
00065     if (p == num) {
00066 //        av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n");
00067         return 0;
00068     }
00069 
00070     codes[p] = 0;
00071     if (bits[p] > 32)
00072         return 1;
00073     for (i = 0; i < bits[p]; ++i)
00074         exit_at_level[i+1] = 1 << i;
00075 
00076 #ifdef DEBUG
00077     av_log(NULL, AV_LOG_INFO, " %u. of %u code len %d code %d - ", p, num, bits[p], codes[p]);
00078     init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
00079     for (i = 0; i < bits[p]; ++i)
00080         av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
00081     av_log(NULL, AV_LOG_INFO, "\n");
00082 #endif
00083 
00084     ++p;
00085 
00086     for (; p < num; ++p) {
00087         if (bits[p] > 32)
00088              return 1;
00089         if (bits[p] == 0)
00090              continue;
00091         // find corresponding exit(node which the tree can grow further from)
00092         for (i = bits[p]; i > 0; --i)
00093             if (exit_at_level[i])
00094                 break;
00095         if (!i) // overspecified tree
00096              return 1;
00097         code = exit_at_level[i];
00098         exit_at_level[i] = 0;
00099         // construct code (append 0s to end) and introduce new exits
00100         for (j = i + 1 ;j <= bits[p]; ++j)
00101             exit_at_level[j] = code + (1 << (j - 1));
00102         codes[p] = code;
00103 
00104 #ifdef DEBUG
00105         av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
00106         init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
00107         for (i = 0; i < bits[p]; ++i)
00108             av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
00109         av_log(NULL, AV_LOG_INFO, "\n");
00110 #endif
00111 
00112     }
00113 
00114     //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
00115     for (p = 1; p < 33; p++)
00116         if (exit_at_level[p])
00117             return 1;
00118 
00119     return 0;
00120 }
00121 
00122 void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values)
00123 {
00124     int i;
00125     list[0].sort = 0;
00126     list[1].sort = 1;
00127     for (i = 2; i < values; i++) {
00128         int j;
00129         list[i].low  = 0;
00130         list[i].high = 1;
00131         list[i].sort = i;
00132         for (j = 2; j < i; j++) {
00133             int tmp = list[j].x;
00134             if (tmp < list[i].x) {
00135                 if (tmp > list[list[i].low].x)
00136                     list[i].low  =  j;
00137             } else {
00138                 if (tmp < list[list[i].high].x)
00139                     list[i].high = j;
00140             }
00141         }
00142     }
00143     for (i = 0; i < values - 1; i++) {
00144         int j;
00145         for (j = i + 1; j < values; j++) {
00146             if (list[list[i].sort].x > list[list[j].sort].x) {
00147                 int tmp = list[i].sort;
00148                 list[i].sort = list[j].sort;
00149                 list[j].sort = tmp;
00150             }
00151         }
00152     }
00153 }
00154 
00155 static inline void render_line_unrolled(intptr_t x, int y, int x1,
00156                                         intptr_t sy, int ady, int adx,
00157                                         float *buf)
00158 {
00159     int err = -adx;
00160     x -= x1 - 1;
00161     buf += x1 - 1;
00162     while (++x < 0) {
00163         err += ady;
00164         if (err >= 0) {
00165             err += ady - adx;
00166             y   += sy;
00167             buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00168         }
00169         buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00170     }
00171     if (x <= 0) {
00172         if (err + ady >= 0)
00173             y += sy;
00174         buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00175     }
00176 }
00177 
00178 static void render_line(int x0, int y0, int x1, int y1, float *buf)
00179 {
00180     int dy  = y1 - y0;
00181     int adx = x1 - x0;
00182     int ady = FFABS(dy);
00183     int sy  = dy < 0 ? -1 : 1;
00184     buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
00185     if (ady*2 <= adx) { // optimized common case
00186         render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
00187     } else {
00188         int base  = dy / adx;
00189         int x     = x0;
00190         int y     = y0;
00191         int err   = -adx;
00192         ady -= FFABS(base) * adx;
00193         while (++x < x1) {
00194             y += base;
00195             err += ady;
00196             if (err >= 0) {
00197                 err -= adx;
00198                 y   += sy;
00199             }
00200             buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00201         }
00202     }
00203 }
00204 
00205 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
00206                                   uint16_t *y_list, int *flag,
00207                                   int multiplier, float *out, int samples)
00208 {
00209     int lx, ly, i;
00210     lx = 0;
00211     ly = y_list[0] * multiplier;
00212     for (i = 1; i < values; i++) {
00213         int pos = list[i].sort;
00214         if (flag[pos]) {
00215             int x1 = list[pos].x;
00216             int y1 = y_list[pos] * multiplier;
00217             if (lx < samples)
00218                 render_line(lx, ly, FFMIN(x1,samples), y1, out);
00219             lx = x1;
00220             ly = y1;
00221         }
00222         if (lx >= samples)
00223             break;
00224     }
00225     if (lx < samples)
00226         render_line(lx, ly, samples, ly, out);
00227 }