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

libavcodec/vorbis.c

Go to the documentation of this file.
00001 
00029 #define BITSTREAM_READER_LE
00030 #include "avcodec.h"
00031 #include "get_bits.h"
00032 
00033 #include "vorbis.h"
00034 
00035 
00036 /* Helper functions */
00037 
00038 // x^(1/n)
00039 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
00040 {
00041     unsigned int ret = 0, i, j;
00042 
00043     do {
00044         ++ret;
00045         for (i = 0, j = ret; i < n - 1; i++)
00046             j *= ret;
00047     } while (j <= x);
00048 
00049     return ret - 1;
00050 }
00051 
00052 // Generate vlc codes from vorbis huffman code lengths
00053 
00054 // the two bits[p] > 32 checks should be redundant, all calling code should
00055 // already ensure that, but since it allows overwriting the stack it seems
00056 // reasonable to check redundantly.
00057 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
00058 {
00059     uint32_t exit_at_level[33] = { 404 };
00060 
00061     unsigned i, j, p, code;
00062 
00063 #ifdef DEBUG
00064     GetBitContext gb;
00065 #endif
00066 
00067     for (p = 0; (bits[p] == 0) && (p < num); ++p)
00068         ;
00069     if (p == num) {
00070 //        av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n");
00071         return 0;
00072     }
00073 
00074     codes[p] = 0;
00075     if (bits[p] > 32)
00076         return 1;
00077     for (i = 0; i < bits[p]; ++i)
00078         exit_at_level[i+1] = 1 << i;
00079 
00080 #ifdef DEBUG
00081     av_log(NULL, AV_LOG_INFO, " %u. of %u code len %d code %d - ", p, num, bits[p], codes[p]);
00082     init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
00083     for (i = 0; i < bits[p]; ++i)
00084         av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
00085     av_log(NULL, AV_LOG_INFO, "\n");
00086 #endif
00087 
00088     ++p;
00089 
00090     for (; p < num; ++p) {
00091         if (bits[p] > 32)
00092              return 1;
00093         if (bits[p] == 0)
00094              continue;
00095         // find corresponding exit(node which the tree can grow further from)
00096         for (i = bits[p]; i > 0; --i)
00097             if (exit_at_level[i])
00098                 break;
00099         if (!i) // overspecified tree
00100              return 1;
00101         code = exit_at_level[i];
00102         exit_at_level[i] = 0;
00103         // construct code (append 0s to end) and introduce new exits
00104         for (j = i + 1 ;j <= bits[p]; ++j)
00105             exit_at_level[j] = code + (1 << (j - 1));
00106         codes[p] = code;
00107 
00108 #ifdef DEBUG
00109         av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
00110         init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
00111         for (i = 0; i < bits[p]; ++i)
00112             av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
00113         av_log(NULL, AV_LOG_INFO, "\n");
00114 #endif
00115 
00116     }
00117 
00118     //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
00119     for (p = 1; p < 33; p++)
00120         if (exit_at_level[p])
00121             return 1;
00122 
00123     return 0;
00124 }
00125 
00126 int ff_vorbis_ready_floor1_list(AVCodecContext *avccontext,
00127                                 vorbis_floor1_entry *list, int values)
00128 {
00129     int i;
00130     list[0].sort = 0;
00131     list[1].sort = 1;
00132     for (i = 2; i < values; i++) {
00133         int j;
00134         list[i].low  = 0;
00135         list[i].high = 1;
00136         list[i].sort = i;
00137         for (j = 2; j < i; j++) {
00138             int tmp = list[j].x;
00139             if (tmp < list[i].x) {
00140                 if (tmp > list[list[i].low].x)
00141                     list[i].low  =  j;
00142             } else {
00143                 if (tmp < list[list[i].high].x)
00144                     list[i].high = j;
00145             }
00146         }
00147     }
00148     for (i = 0; i < values - 1; i++) {
00149         int j;
00150         for (j = i + 1; j < values; j++) {
00151             if (list[i].x == list[j].x) {
00152                 av_log(avccontext, AV_LOG_ERROR,
00153                        "Duplicate value found in floor 1 X coordinates\n");
00154                 return AVERROR_INVALIDDATA;
00155             }
00156             if (list[list[i].sort].x > list[list[j].sort].x) {
00157                 int tmp = list[i].sort;
00158                 list[i].sort = list[j].sort;
00159                 list[j].sort = tmp;
00160             }
00161         }
00162     }
00163     return 0;
00164 }
00165 
00166 static inline void render_line_unrolled(intptr_t x, int y, int x1,
00167                                         intptr_t sy, int ady, int adx,
00168                                         float *buf)
00169 {
00170     int err = -adx;
00171     x -= x1 - 1;
00172     buf += x1 - 1;
00173     while (++x < 0) {
00174         err += ady;
00175         if (err >= 0) {
00176             err += ady - adx;
00177             y   += sy;
00178             buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00179         }
00180         buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00181     }
00182     if (x <= 0) {
00183         if (err + ady >= 0)
00184             y += sy;
00185         buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00186     }
00187 }
00188 
00189 static void render_line(int x0, int y0, int x1, int y1, float *buf)
00190 {
00191     int dy  = y1 - y0;
00192     int adx = x1 - x0;
00193     int ady = FFABS(dy);
00194     int sy  = dy < 0 ? -1 : 1;
00195     buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
00196     if (ady*2 <= adx) { // optimized common case
00197         render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
00198     } else {
00199         int base  = dy / adx;
00200         int x     = x0;
00201         int y     = y0;
00202         int err   = -adx;
00203         ady -= FFABS(base) * adx;
00204         while (++x < x1) {
00205             y += base;
00206             err += ady;
00207             if (err >= 0) {
00208                 err -= adx;
00209                 y   += sy;
00210             }
00211             buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00212         }
00213     }
00214 }
00215 
00216 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
00217                                   uint16_t *y_list, int *flag,
00218                                   int multiplier, float *out, int samples)
00219 {
00220     int lx, ly, i;
00221     lx = 0;
00222     ly = y_list[0] * multiplier;
00223     for (i = 1; i < values; i++) {
00224         int pos = list[i].sort;
00225         if (flag[pos]) {
00226             int x1 = list[pos].x;
00227             int y1 = y_list[pos] * multiplier;
00228             if (lx < samples)
00229                 render_line(lx, ly, FFMIN(x1,samples), y1, out);
00230             lx = x1;
00231             ly = y1;
00232         }
00233         if (lx >= samples)
00234             break;
00235     }
00236     if (lx < samples)
00237         render_line(lx, ly, samples, ly, out);
00238 }
Generated on Fri Feb 1 2013 14:34:45 for FFmpeg by doxygen 1.7.1