• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/vorbis.c

Go to the documentation of this file.
00001 
00023 #undef V_DEBUG
00024 //#define V_DEBUG
00025 
00026 #define ALT_BITSTREAM_READER_LE
00027 #include "avcodec.h"
00028 #include "bitstream.h"
00029 
00030 #include "vorbis.h"
00031 
00032 
00033 /* Helper functions */
00034 
00035 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n) {   // x^(1/n)
00036     unsigned int ret=0, i, j;
00037 
00038     do {
00039         ++ret;
00040         for(i=0,j=ret;i<n-1;i++) j*=ret;
00041     } while (j<=x);
00042 
00043     return ret - 1;
00044 }
00045 
00046 // Generate vlc codes from vorbis huffman code lengths
00047 
00048 // the two bits[p] > 32 checks should be redundant, all calling code should
00049 // already ensure that, but since it allows overwriting the stack it seems
00050 // reasonable to check redundantly.
00051 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, uint_fast32_t num) {
00052     uint_fast32_t exit_at_level[33]={404,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00053         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
00054 
00055     uint_fast8_t i,j;
00056     uint_fast32_t code,p;
00057 
00058 #ifdef V_DEBUG
00059     GetBitContext gb;
00060 #endif
00061 
00062     for(p=0;(bits[p]==0) && (p<num);++p);
00063     if (p==num) {
00064 //        av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n");
00065         return 0;
00066     }
00067 
00068     codes[p]=0;
00069     if (bits[p] > 32) return 1;
00070     for(i=0;i<bits[p];++i) {
00071         exit_at_level[i+1]=1<<i;
00072     }
00073 
00074 #ifdef V_DEBUG
00075     av_log(NULL, AV_LOG_INFO, " %d. of %d code len %d code %d - ", p, num, bits[p], codes[p]);
00076     init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
00077     for(i=0;i<bits[p];++i) {
00078         av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
00079     }
00080     av_log(NULL, AV_LOG_INFO, "\n");
00081 #endif
00082 
00083     ++p;
00084 
00085     for(;p<num;++p) {
00086         if (bits[p] > 32) return 1;
00087         if (bits[p]==0) continue;
00088         // find corresponding exit(node which the tree can grow further from)
00089         for(i=bits[p];i>0;--i) {
00090             if (exit_at_level[i]) break;
00091         }
00092         if (!i) return 1; // overspecified tree
00093         code=exit_at_level[i];
00094         exit_at_level[i]=0;
00095         // construct code (append 0s to end) and introduce new exits
00096         for(j=i+1;j<=bits[p];++j) {
00097             exit_at_level[j]=code+(1<<(j-1));
00098         }
00099         codes[p]=code;
00100 
00101 #ifdef V_DEBUG
00102         av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
00103         init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
00104         for(i=0;i<bits[p];++i) {
00105             av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
00106         }
00107         av_log(NULL, AV_LOG_INFO, "\n");
00108 #endif
00109 
00110     }
00111 
00112     //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
00113     for (p=1; p<33; p++)
00114         if (exit_at_level[p]) return 1;
00115 
00116     return 0;
00117 }
00118 
00119 void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values) {
00120     int i;
00121     list[0].sort = 0;
00122     list[1].sort = 1;
00123     for (i = 2; i < values; i++) {
00124         int j;
00125         list[i].low = 0;
00126         list[i].high = 1;
00127         list[i].sort = i;
00128         for (j = 2; j < i; j++) {
00129             int tmp = list[j].x;
00130             if (tmp < list[i].x) {
00131                 if (tmp > list[list[i].low].x) list[i].low = j;
00132             } else {
00133                 if (tmp < list[list[i].high].x) list[i].high = j;
00134             }
00135         }
00136     }
00137     for (i = 0; i < values - 1; i++) {
00138         int j;
00139         for (j = i + 1; j < values; j++) {
00140             if (list[list[i].sort].x > list[list[j].sort].x) {
00141                 int tmp = list[i].sort;
00142                 list[i].sort = list[j].sort;
00143                 list[j].sort = tmp;
00144             }
00145         }
00146     }
00147 }
00148 
00149 static void render_line(int x0, uint8_t y0, int x1, int y1, float * buf) {
00150     int dy = y1 - y0;
00151     int adx = x1 - x0;
00152     int base = dy / adx;
00153     int ady = FFABS(dy) - FFABS(base) * adx;
00154     int x = x0;
00155     uint8_t y = y0;
00156     int err = 0;
00157     int sy = dy<0 ? -1 : 1;
00158     buf[x] = ff_vorbis_floor1_inverse_db_table[y];
00159     while (++x < x1) {
00160         err += ady;
00161         if (err >= adx) {
00162             err -= adx;
00163             y += sy;
00164         }
00165         y += base;
00166         buf[x] = ff_vorbis_floor1_inverse_db_table[y];
00167     }
00168 }
00169 
00170 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values, uint_fast16_t * y_list, int * flag, int multiplier, float * out, int samples) {
00171     int lx, i;
00172     uint8_t ly;
00173     lx = 0;
00174     ly = y_list[0] * multiplier;
00175     for (i = 1; i < values; i++) {
00176         int pos = list[i].sort;
00177         if (flag[pos]) {
00178             int x1 = list[pos].x;
00179             int y1 = y_list[pos] * multiplier;
00180             if (lx < samples)
00181                 render_line(lx, ly, FFMIN(x1,samples), y1, out);
00182             lx = x1;
00183             ly = y1;
00184         }
00185         if (lx >= samples) break;
00186     }
00187     if (lx < samples) render_line(lx, ly, samples, ly, out);
00188 }

Generated on Sat Feb 16 2013 09:23:14 for ffmpeg by  doxygen 1.7.1