Libav 0.7.1
|
00001 00023 #define ALT_BITSTREAM_READER_LE 00024 #include "avcodec.h" 00025 #include "get_bits.h" 00026 00027 #include "vorbis.h" 00028 00029 00030 /* Helper functions */ 00031 00032 // x^(1/n) 00033 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n) 00034 { 00035 unsigned int ret = 0, i, j; 00036 00037 do { 00038 ++ret; 00039 for (i = 0, j = ret; i < n - 1; i++) 00040 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, unsigned num) 00052 { 00053 uint32_t exit_at_level[33] = { 404 }; 00054 00055 unsigned i, j, p, code; 00056 00057 #ifdef DEBUG 00058 GetBitContext gb; 00059 #endif 00060 00061 for (p = 0; (bits[p] == 0) && (p < num); ++p) 00062 ; 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) 00070 return 1; 00071 for (i = 0; i < bits[p]; ++i) 00072 exit_at_level[i+1] = 1 << i; 00073 00074 #ifdef DEBUG 00075 av_log(NULL, AV_LOG_INFO, " %u. of %u code len %d code %d - ", p, num, bits[p], codes[p]); 00076 init_get_bits(&gb, (uint8_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 av_log(NULL, AV_LOG_INFO, "\n"); 00080 #endif 00081 00082 ++p; 00083 00084 for (; p < num; ++p) { 00085 if (bits[p] > 32) 00086 return 1; 00087 if (bits[p] == 0) 00088 continue; 00089 // find corresponding exit(node which the tree can grow further from) 00090 for (i = bits[p]; i > 0; --i) 00091 if (exit_at_level[i]) 00092 break; 00093 if (!i) // overspecified tree 00094 return 1; 00095 code = exit_at_level[i]; 00096 exit_at_level[i] = 0; 00097 // construct code (append 0s to end) and introduce new exits 00098 for (j = i + 1 ;j <= bits[p]; ++j) 00099 exit_at_level[j] = code + (1 << (j - 1)); 00100 codes[p] = code; 00101 00102 #ifdef DEBUG 00103 av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]); 00104 init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]); 00105 for (i = 0; i < bits[p]; ++i) 00106 av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0"); 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]) 00115 return 1; 00116 00117 return 0; 00118 } 00119 00120 void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values) 00121 { 00122 int i; 00123 list[0].sort = 0; 00124 list[1].sort = 1; 00125 for (i = 2; i < values; i++) { 00126 int j; 00127 list[i].low = 0; 00128 list[i].high = 1; 00129 list[i].sort = i; 00130 for (j = 2; j < i; j++) { 00131 int tmp = list[j].x; 00132 if (tmp < list[i].x) { 00133 if (tmp > list[list[i].low].x) 00134 list[i].low = j; 00135 } else { 00136 if (tmp < list[list[i].high].x) 00137 list[i].high = j; 00138 } 00139 } 00140 } 00141 for (i = 0; i < values - 1; i++) { 00142 int j; 00143 for (j = i + 1; j < values; j++) { 00144 if (list[list[i].sort].x > list[list[j].sort].x) { 00145 int tmp = list[i].sort; 00146 list[i].sort = list[j].sort; 00147 list[j].sort = tmp; 00148 } 00149 } 00150 } 00151 } 00152 00153 static inline void render_line_unrolled(intptr_t x, intptr_t y, int x1, 00154 intptr_t sy, int ady, int adx, 00155 float *buf) 00156 { 00157 int err = -adx; 00158 x -= x1 - 1; 00159 buf += x1 - 1; 00160 while (++x < 0) { 00161 err += ady; 00162 if (err >= 0) { 00163 err += ady - adx; 00164 y += sy; 00165 buf[x++] = ff_vorbis_floor1_inverse_db_table[y]; 00166 } 00167 buf[x] = ff_vorbis_floor1_inverse_db_table[y]; 00168 } 00169 if (x <= 0) { 00170 if (err + ady >= 0) 00171 y += sy; 00172 buf[x] = ff_vorbis_floor1_inverse_db_table[y]; 00173 } 00174 } 00175 00176 static void render_line(int x0, int y0, int x1, int y1, float *buf) 00177 { 00178 int dy = y1 - y0; 00179 int adx = x1 - x0; 00180 int ady = FFABS(dy); 00181 int sy = dy < 0 ? -1 : 1; 00182 buf[x0] = ff_vorbis_floor1_inverse_db_table[y0]; 00183 if (ady*2 <= adx) { // optimized common case 00184 render_line_unrolled(x0, y0, x1, sy, ady, adx, buf); 00185 } else { 00186 int base = dy / adx; 00187 int x = x0; 00188 int y = y0; 00189 int err = -adx; 00190 ady -= FFABS(base) * adx; 00191 while (++x < x1) { 00192 y += base; 00193 err += ady; 00194 if (err >= 0) { 00195 err -= adx; 00196 y += sy; 00197 } 00198 buf[x] = ff_vorbis_floor1_inverse_db_table[y]; 00199 } 00200 } 00201 } 00202 00203 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values, 00204 uint16_t *y_list, int *flag, 00205 int multiplier, float *out, int samples) 00206 { 00207 int lx, ly, i; 00208 lx = 0; 00209 ly = y_list[0] * multiplier; 00210 for (i = 1; i < values; i++) { 00211 int pos = list[i].sort; 00212 if (flag[pos]) { 00213 int x1 = list[pos].x; 00214 int y1 = y_list[pos] * multiplier; 00215 if (lx < samples) 00216 render_line(lx, ly, FFMIN(x1,samples), y1, out); 00217 lx = x1; 00218 ly = y1; 00219 } 00220 if (lx >= samples) 00221 break; 00222 } 00223 if (lx < samples) 00224 render_line(lx, ly, samples, ly, out); 00225 }