00001
00023 #undef V_DEBUG
00024
00025
00026 #define ALT_BITSTREAM_READER_LE
00027 #include "avcodec.h"
00028 #include "bitstream.h"
00029
00030 #include "vorbis.h"
00031
00032
00033
00034
00035 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int 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
00047
00048 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, uint_fast32_t num) {
00049 uint_fast32_t exit_at_level[33]={404,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00050 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
00051
00052 uint_fast8_t i,j;
00053 uint_fast32_t code,p;
00054
00055 #ifdef V_DEBUG
00056 GetBitContext gb;
00057 #endif
00058
00059 for(p=0;(bits[p]==0) && (p<num);++p);
00060 if (p==num) {
00061
00062 return 0;
00063 }
00064
00065 codes[p]=0;
00066 for(i=0;i<bits[p];++i) {
00067 exit_at_level[i+1]=1<<i;
00068 }
00069
00070 #ifdef V_DEBUG
00071 av_log(NULL, AV_LOG_INFO, " %d. of %d code len %d code %d - ", p, num, bits[p], codes[p]);
00072 init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
00073 for(i=0;i<bits[p];++i) {
00074 av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
00075 }
00076 av_log(NULL, AV_LOG_INFO, "\n");
00077 #endif
00078
00079 ++p;
00080
00081 for(;p<num;++p) {
00082 if (bits[p]==0) continue;
00083
00084 for(i=bits[p];i>0;--i) {
00085 if (exit_at_level[i]) break;
00086 }
00087 if (!i) return 1;
00088 code=exit_at_level[i];
00089 exit_at_level[i]=0;
00090
00091 for(j=i+1;j<=bits[p];++j) {
00092 exit_at_level[j]=code+(1<<(j-1));
00093 }
00094 codes[p]=code;
00095
00096 #ifdef V_DEBUG
00097 av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
00098 init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
00099 for(i=0;i<bits[p];++i) {
00100 av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
00101 }
00102 av_log(NULL, AV_LOG_INFO, "\n");
00103 #endif
00104
00105 }
00106
00107
00108 for (p=1; p<33; p++)
00109 if (exit_at_level[p]) return 1;
00110
00111 return 0;
00112 }
00113
00114 void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values) {
00115 int i;
00116 list[0].sort = 0;
00117 list[1].sort = 1;
00118 for (i = 2; i < values; i++) {
00119 int j;
00120 list[i].low = 0;
00121 list[i].high = 1;
00122 list[i].sort = i;
00123 for (j = 2; j < i; j++) {
00124 int tmp = list[j].x;
00125 if (tmp < list[i].x) {
00126 if (tmp > list[list[i].low].x) list[i].low = j;
00127 } else {
00128 if (tmp < list[list[i].high].x) list[i].high = j;
00129 }
00130 }
00131 }
00132 for (i = 0; i < values - 1; i++) {
00133 int j;
00134 for (j = i + 1; j < values; j++) {
00135 if (list[list[i].sort].x > list[list[j].sort].x) {
00136 int tmp = list[i].sort;
00137 list[i].sort = list[j].sort;
00138 list[j].sort = tmp;
00139 }
00140 }
00141 }
00142 }
00143
00144 static void render_line(int x0, int y0, int x1, int y1, float * buf) {
00145 int dy = y1 - y0;
00146 int adx = x1 - x0;
00147 int base = dy / adx;
00148 int ady = FFABS(dy) - FFABS(base) * adx;
00149 int x = x0;
00150 int y = y0;
00151 int err = 0;
00152 int sy = dy<0 ? -1 : 1;
00153 buf[x] = ff_vorbis_floor1_inverse_db_table[y];
00154 while (++x < x1) {
00155 err += ady;
00156 if (err >= adx) {
00157 err -= adx;
00158 y += sy;
00159 }
00160 y += base;
00161 buf[x] = ff_vorbis_floor1_inverse_db_table[y];
00162 }
00163 }
00164
00165 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) {
00166 int lx, ly, i;
00167 lx = 0;
00168 ly = y_list[0] * multiplier;
00169 for (i = 1; i < values; i++) {
00170 int pos = list[i].sort;
00171 if (flag[pos]) {
00172 int x1 = list[pos].x;
00173 int y1 = y_list[pos] * multiplier;
00174 if (lx < samples)
00175 render_line(lx, ly, FFMIN(x1,samples), y1, out);
00176 lx = x1;
00177 ly = y1;
00178 }
00179 if (lx >= samples) break;
00180 }
00181 if (lx < samples) render_line(lx, ly, samples, ly, out);
00182 }