00001
00023 #undef V_DEBUG
00024
00025
00026 #define ALT_BITSTREAM_READER_LE
00027 #include "avcodec.h"
00028 #include "get_bits.h"
00029
00030 #include "vorbis.h"
00031
00032
00033
00034
00035
00036 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
00037 {
00038 unsigned int ret = 0, i, j;
00039
00040 do {
00041 ++ret;
00042 for (i = 0, j = ret; i < n - 1; i++)
00043 j *= ret;
00044 } while (j <= x);
00045
00046 return ret - 1;
00047 }
00048
00049
00050
00051
00052
00053
00054 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, uint_fast32_t num)
00055 {
00056 uint_fast32_t exit_at_level[33] = {
00057 404, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00058 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00059
00060 uint_fast8_t i, j;
00061 uint_fast32_t code, p;
00062
00063 #ifdef V_DEBUG
00064 GetBitContext gb;
00065 #endif
00066
00067 for (p = 0; (bits[p] == 0) && (p < num); ++p)
00068 ;
00069 if (p == num) {
00070
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 V_DEBUG
00081 av_log(NULL, AV_LOG_INFO, " %d. of %d code len %d code %d - ", p, num, bits[p], codes[p]);
00082 init_get_bits(&gb, (uint_fast8_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
00096 for (i = bits[p]; i > 0; --i)
00097 if (exit_at_level[i])
00098 break;
00099 if (!i)
00100 return 1;
00101 code = exit_at_level[i];
00102 exit_at_level[i] = 0;
00103
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 V_DEBUG
00109 av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
00110 init_get_bits(&gb, (uint_fast8_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
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, uint8_t 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[y];
00179 }
00180 buf[x] = ff_vorbis_floor1_inverse_db_table[y];
00181 }
00182 if (x <= 0) {
00183 if (err + ady >= 0)
00184 y += sy;
00185 buf[x] = ff_vorbis_floor1_inverse_db_table[y];
00186 }
00187 }
00188
00189 static void render_line(int x0, uint8_t 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[y0];
00196 if (ady*2 <= adx) {
00197 render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
00198 } else {
00199 int base = dy / adx;
00200 int x = x0;
00201 uint8_t 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[y];
00212 }
00213 }
00214 }
00215
00216 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
00217 uint_fast16_t *y_list, int *flag,
00218 int multiplier, float *out, int samples)
00219 {
00220 int lx, i;
00221 uint8_t ly;
00222 lx = 0;
00223 ly = y_list[0] * multiplier;
00224 for (i = 1; i < values; i++) {
00225 int pos = list[i].sort;
00226 if (flag[pos]) {
00227 int x1 = list[pos].x;
00228 int y1 = y_list[pos] * multiplier;
00229 if (lx < samples)
00230 render_line(lx, ly, FFMIN(x1,samples), y1, out);
00231 lx = x1;
00232 ly = y1;
00233 }
00234 if (lx >= samples)
00235 break;
00236 }
00237 if (lx < samples)
00238 render_line(lx, ly, samples, ly, out);
00239 }