36 #define FREEZE_INTERVAL 128
40 #define MAX_FRAME_SIZE 32768
45 #define MAX_TRELLIS 16
51 for (
i = 0;
i < 2;
i++) {
63 c->band[0].scale_factor = 8;
64 c->band[1].scale_factor = 2;
65 c->prev_samples_pos = 22;
80 "allowed. Using %d instead of %d\n", new_frame_size,
96 "allowed. Using %d instead of %d\n", new_trellis,
101 int frontier = 1 << avctx->
trellis;
104 for (
int i = 0;
i < 2;
i++) {
106 c->node_buf[
i] =
av_calloc(frontier, 2 *
sizeof(**
c->node_buf));
107 c->nodep_buf[
i] =
av_calloc(frontier, 2 *
sizeof(**
c->nodep_buf));
108 if (!
c->paths[
i] || !
c->node_buf[
i] || !
c->nodep_buf[
i])
120 35, 72, 110, 150, 190, 233, 276, 323,
121 370, 422, 473, 530, 587, 650, 714, 786,
122 858, 940, 1023, 1121, 1219, 1339, 1458, 1612,
123 1765, 1980, 2195, 2557, 2919
127 int *xlow,
int *xhigh)
130 c->prev_samples[
c->prev_samples_pos++] =
samples[0];
131 c->prev_samples[
c->prev_samples_pos++] =
samples[1];
132 c->dsp.apply_qmf(
c->prev_samples +
c->prev_samples_pos - 24, xout);
133 *xlow = xout[0] + xout[1] >> 14;
134 *xhigh = xout[0] - xout[1] >> 14;
136 memmove(
c->prev_samples,
137 c->prev_samples +
c->prev_samples_pos - 22,
138 22 *
sizeof(
c->prev_samples[0]));
139 c->prev_samples_pos = 22;
146 int pred = 141 *
state->scale_factor >> 8;
157 limit = limit + 1 << 10;
162 return (
diff < 0 ? (
i < 2 ? 63 : 33) : 61) -
i;
170 int frontier = 1 << trellis;
173 int pathn[2] = {0, 0}, froze = -1;
176 for (
i = 0;
i < 2;
i++) {
177 nodes[
i] =
c->nodep_buf[
i];
178 nodes_next[
i] =
c->nodep_buf[
i] + frontier;
179 memset(
c->nodep_buf[
i], 0, 2 * frontier *
sizeof(*
c->nodep_buf[
i]));
180 nodes[
i][0] =
c->node_buf[
i] + frontier;
181 nodes[
i][0]->
ssd = 0;
182 nodes[
i][0]->
path = 0;
183 nodes[
i][0]->state =
c->band[
i];
186 for (
i = 0; i < nb_samples >> 1;
i++) {
189 int heap_pos[2] = {0, 0};
191 for (j = 0; j < 2; j++) {
192 next[j] =
c->node_buf[j] + frontier*(
i & 1);
193 memset(nodes_next[j], 0, frontier *
sizeof(**nodes_next));
198 for (j = 0; j < frontier && nodes[0][j]; j++) {
204 int range = j < frontier/2 ? 4 : 0;
207 int ilow =
encode_low(&cur_node->state, xlow);
209 for (k = ilow - range; k <= ilow + range && k <= 63; k += 4) {
210 int decoded, dec_diff,
pos;
219 + cur_node->state.s_predictor, 14);
220 dec_diff = xlow - decoded;
222 #define STORE_NODE(index, UPDATE, VALUE)\
223 ssd = cur_node->ssd + dec_diff*dec_diff;\
226 if (ssd < cur_node->ssd)\
228 if (heap_pos[index] < frontier) {\
229 pos = heap_pos[index]++;\
230 av_assert2(pathn[index] < FREEZE_INTERVAL * frontier);\
231 node = nodes_next[index][pos] = next[index]++;\
232 node->path = pathn[index]++;\
236 pos = (frontier>>1) + (heap_pos[index] & ((frontier>>1) - 1));\
237 if (ssd >= nodes_next[index][pos]->ssd)\
240 node = nodes_next[index][pos];\
243 node->state = cur_node->state;\
245 c->paths[index][node->path].value = VALUE;\
246 c->paths[index][node->path].prev = cur_node->path;\
250 int parent = (pos - 1) >> 1;\
251 if (nodes_next[index][parent]->ssd <= ssd)\
253 FFSWAP(struct TrellisNode*, nodes_next[index][parent],\
254 nodes_next[index][pos]);\
261 for (j = 0; j < frontier && nodes[1][j]; j++) {
269 for (ihigh = 0; ihigh < 4; ihigh++) {
270 int dhigh, decoded, dec_diff,
pos;
274 dhigh = cur_node->state.scale_factor *
276 decoded =
av_clip_intp2(dhigh + cur_node->state.s_predictor, 14);
277 dec_diff = xhigh - decoded;
283 for (j = 0; j < 2; j++) {
286 if (nodes[j][0]->
ssd > (1 << 16)) {
287 for (k = 1; k < frontier && nodes[j][k]; k++)
288 nodes[j][k]->
ssd -= nodes[j][0]->
ssd;
289 nodes[j][0]->ssd = 0;
294 p[0] = &
c->paths[0][nodes[0][0]->path];
295 p[1] = &
c->paths[1][nodes[1][0]->path];
296 for (j =
i; j > froze; j--) {
297 dst[j] = p[1]->value << 6 | p[0]->value;
298 p[0] = &
c->paths[0][p[0]->
prev];
299 p[1] = &
c->paths[1][p[1]->
prev];
302 pathn[0] = pathn[1] = 0;
303 memset(nodes[0] + 1, 0, (frontier - 1)*
sizeof(**nodes));
304 memset(nodes[1] + 1, 0, (frontier - 1)*
sizeof(**nodes));
308 p[0] = &
c->paths[0][nodes[0][0]->
path];
309 p[1] = &
c->paths[1][nodes[1][0]->
path];
310 for (j =
i; j > froze; j--) {
311 dst[j] = p[1]->value << 6 | p[0]->value;
312 p[0] = &
c->paths[0][p[0]->
prev];
313 p[1] = &
c->paths[1][p[1]->
prev];
315 c->band[0] = nodes[0][0]->state;
316 c->band[1] = nodes[1][0]->state;
322 int xlow, xhigh, ilow, ihigh;
329 *dst = ihigh << 6 | ilow;
337 for (
i = 0;
i < nb_samples;
i += 2)
345 const int16_t *
samples = (
const int16_t *)
frame->data[0];
352 nb_samples =
frame->nb_samples - (
frame->nb_samples & 1);
360 if (nb_samples < frame->nb_samples) {
361 int16_t last_samples[2] = {
samples[nb_samples],
samples[nb_samples] };