00001
00023 #define ALT_BITSTREAM_READER_LE
00024 #include "avcodec.h"
00025 #include "get_bits.h"
00026
00027 #include "vorbis.h"
00028
00029
00030
00031
00032
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
00047
00048
00049
00050
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
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
00090 for (i = bits[p]; i > 0; --i)
00091 if (exit_at_level[i])
00092 break;
00093 if (!i)
00094 return 1;
00095 code = exit_at_level[i];
00096 exit_at_level[i] = 0;
00097
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
00113 for (p = 1; p < 33; p++)
00114 if (exit_at_level[p])
00115 return 1;
00116
00117 return 0;
00118 }
00119
00120 int ff_vorbis_ready_floor1_list(AVCodecContext *avccontext,
00121 vorbis_floor1_entry *list, int values)
00122 {
00123 int i;
00124 list[0].sort = 0;
00125 list[1].sort = 1;
00126 for (i = 2; i < values; i++) {
00127 int j;
00128 list[i].low = 0;
00129 list[i].high = 1;
00130 list[i].sort = i;
00131 for (j = 2; j < i; j++) {
00132 int tmp = list[j].x;
00133 if (tmp < list[i].x) {
00134 if (tmp > list[list[i].low].x)
00135 list[i].low = j;
00136 } else {
00137 if (tmp < list[list[i].high].x)
00138 list[i].high = j;
00139 }
00140 }
00141 }
00142 for (i = 0; i < values - 1; i++) {
00143 int j;
00144 for (j = i + 1; j < values; j++) {
00145 if (list[i].x == list[j].x) {
00146 av_log(avccontext, AV_LOG_ERROR,
00147 "Duplicate value found in floor 1 X coordinates\n");
00148 return AVERROR_INVALIDDATA;
00149 }
00150 if (list[list[i].sort].x > list[list[j].sort].x) {
00151 int tmp = list[i].sort;
00152 list[i].sort = list[j].sort;
00153 list[j].sort = tmp;
00154 }
00155 }
00156 }
00157 return 0;
00158 }
00159
00160 static inline void render_line_unrolled(intptr_t x, int y, int x1,
00161 intptr_t sy, int ady, int adx,
00162 float *buf)
00163 {
00164 int err = -adx;
00165 x -= x1 - 1;
00166 buf += x1 - 1;
00167 while (++x < 0) {
00168 err += ady;
00169 if (err >= 0) {
00170 err += ady - adx;
00171 y += sy;
00172 buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00173 }
00174 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00175 }
00176 if (x <= 0) {
00177 if (err + ady >= 0)
00178 y += sy;
00179 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00180 }
00181 }
00182
00183 static void render_line(int x0, int y0, int x1, int y1, float *buf)
00184 {
00185 int dy = y1 - y0;
00186 int adx = x1 - x0;
00187 int ady = FFABS(dy);
00188 int sy = dy < 0 ? -1 : 1;
00189 buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
00190 if (ady*2 <= adx) {
00191 render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
00192 } else {
00193 int base = dy / adx;
00194 int x = x0;
00195 int y = y0;
00196 int err = -adx;
00197 ady -= FFABS(base) * adx;
00198 while (++x < x1) {
00199 y += base;
00200 err += ady;
00201 if (err >= 0) {
00202 err -= adx;
00203 y += sy;
00204 }
00205 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
00206 }
00207 }
00208 }
00209
00210 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
00211 uint16_t *y_list, int *flag,
00212 int multiplier, float *out, int samples)
00213 {
00214 int lx, ly, i;
00215 lx = 0;
00216 ly = y_list[0] * multiplier;
00217 for (i = 1; i < values; i++) {
00218 int pos = list[i].sort;
00219 if (flag[pos]) {
00220 int x1 = list[pos].x;
00221 int y1 = y_list[pos] * multiplier;
00222 if (lx < samples)
00223 render_line(lx, ly, FFMIN(x1,samples), y1, out);
00224 lx = x1;
00225 ly = y1;
00226 }
00227 if (lx >= samples)
00228 break;
00229 }
00230 if (lx < samples)
00231 render_line(lx, ly, samples, ly, out);
00232 }