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 int ff_vorbis_ready_floor1_list(AVCodecContext *avccontext,
00123                                 vorbis_floor1_entry *list, int values)
00124 {
00125     int i;
00126     list[0].sort = 0;
00127     list[1].sort = 1;
00128     for (i = 2; i < values; i++) {
00129         int j;
00130         list[i].low  = 0;
00131         list[i].high = 1;
00132         list[i].sort = i;
00133         for (j = 2; j < i; j++) {
00134             int tmp = list[j].x;
00135             if (tmp < list[i].x) {
00136                 if (tmp > list[list[i].low].x)
00137                     list[i].low  =  j;
00138             } else {
00139                 if (tmp < list[list[i].high].x)
00140                     list[i].high = j;
00141             }
00142         }
00143     }
00144     for (i = 0; i < values - 1; i++) {
00145         int j;
00146         for (j = i + 1; j < values; j++) {
00147             if (list[i].x == list[j].x) {
00148                 av_log(avccontext, AV_LOG_ERROR,
00149                        "Duplicate value found in floor 1 X coordinates\n");
00150                 return AVERROR_INVALIDDATA;
00151             }
00152             if (list[list[i].sort].x > list[list[j].sort].x) {
00153                 int tmp = list[i].sort;
00154                 list[i].sort = list[j].sort;
00155                 list[j].sort = tmp;
00156             }
00157         }
00158     }
00159     return 0;
00160 }
00161 
00162 static inline void render_line_unrolled(intptr_t x, int y, int x1,
00163                                         intptr_t sy, int ady, int adx,
00164                                         float *buf)
00165 {
00166     int err = -adx;
00167     x -= x1 - 1;
00168     buf += x1 - 1;
00169     while (++x < 0) {
00170         err += ady;
00171         if (err >= 0) {
00172             err += ady - adx;
00173             y   += sy;
00174             buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00175         }
00176         buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00177     }
00178     if (x <= 0) {
00179         if (err + ady >= 0)
00180             y += sy;
00181         buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00182     }
00183 }
00184 
00185 static void render_line(int x0, int y0, int x1, int y1, float *buf)
00186 {
00187     int dy  = y1 - y0;
00188     int adx = x1 - x0;
00189     int ady = FFABS(dy);
00190     int sy  = dy < 0 ? -1 : 1;
00191     buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
00192     if (ady*2 <= adx) { // optimized common case
00193         render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
00194     } else {
00195         int base  = dy / adx;
00196         int x     = x0;
00197         int y     = y0;
00198         int err   = -adx;
00199         ady -= FFABS(base) * adx;
00200         while (++x < x1) {
00201             y += base;
00202             err += ady;
00203             if (err >= 0) {
00204                 err -= adx;
00205                 y   += sy;
00206             }
00207             buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00208         }
00209     }
00210 }
00211 
00212 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
00213                                   uint16_t *y_list, int *flag,
00214                                   int multiplier, float *out, int samples)
00215 {
00216     int lx, ly, i;
00217     lx = 0;
00218     ly = y_list[0] * multiplier;
00219     for (i = 1; i < values; i++) {
00220         int pos = list[i].sort;
00221         if (flag[pos]) {
00222             int x1 = list[pos].x;
00223             int y1 = y_list[pos] * multiplier;
00224             if (lx < samples)
00225                 render_line(lx, ly, FFMIN(x1,samples), y1, out);
00226             lx = x1;
00227             ly = y1;
00228         }
00229         if (lx >= samples)
00230             break;
00231     }
00232     if (lx < samples)
00233         render_line(lx, ly, samples, ly, out);
00234 }