1 
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
5 #include <schroedinger/schro.h>
6 #include <schroedinger/schrounpack.h>
7 #include <schroedinger/schroorc.h>
8 #include <string.h>
9 
10 
11 /* When defined, this trims -1's and 1's from the end of slices by
12  * converting them to 0's.  (Zeros get trimmed by default.)  It
13  * doesn't seem to affect psnr any. (limited testing) */
14 //#define USE_TRAILING_DEAD_ZONE 1
15 
16 
17 typedef struct _SchroLowDelay SchroLowDelay;
18 typedef struct _SchroLDSubband SchroLDSubband;
19 
20 struct _SchroLDSubband
21 {
22   int16_t *data;
23   int x_stride;
24   int y_stride;
25   int slice_width;
26   int slice_height;
27 
28 };
29 
30 struct _SchroLowDelay
31 {
32   SchroFrame *frame;
33 
34   SchroParams *params;
35 
36   int n_subbands;
37   int n_vert_slices;
38   int n_horiz_slices;
39 
40   SchroFrameData luma_subbands[SCHRO_LIMIT_SUBBANDS];
41   SchroFrameData chroma1_subbands[SCHRO_LIMIT_SUBBANDS];
42   SchroFrameData chroma2_subbands[SCHRO_LIMIT_SUBBANDS];
43 
44   SchroFrame *reconstructed_frame;
45 
46   int16_t *quant_y_data;
47   int16_t *quant_uv_data;
48 
49   int slice_y_size;
50   int slice_uv_size;
51 
52   int slice_y_width;
53   int slice_y_height;
54   int slice_uv_width;
55   int slice_uv_height;
56 
57   int16_t *saved_dc_values;
58   //int16_t *quant_data;
59 
60   int subband_shift[SCHRO_LIMIT_SUBBANDS];
61   SchroLDSubband subbands[SCHRO_LIMIT_SUBBANDS][3];
62   int16_t *y_quants;
63   int16_t *y_offsets;
64   int16_t *uv_quants;
65   int16_t *uv_offsets;
66   int *y_memoffsets;
67   int *uv_memoffsets;
68 
69   int length_bits;
70 };
71 
72 
73 #if 0
74 void
75 schro_encoder_init_subbands (SchroEncoderFrame * frame)
76 {
77   int i;
78   int pos;
79   SchroParams *params = &frame->params;
80 
81   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
82     pos = schro_subband_get_position (i);
83 
84     schro_subband_get_frame_data (frame->luma_subbands + i,
85         frame->iwt_frame, 0, pos, params);
86     schro_subband_get_frame_data (frame->chroma1_subbands + i,
87         frame->iwt_frame, 0, pos, params);
88     schro_subband_get_frame_data (frame->chroma2_subbands + i,
89         frame->iwt_frame, 0, pos, params);
90   }
91 }
92 #endif
93 
94 
95 static int
ilog2up(unsigned int x)96 ilog2up (unsigned int x)
97 {
98   int i;
99 
100   for (i = 0; i < 32; i++) {
101     if (x == 0)
102       return i;
103     x >>= 1;
104   }
105   return 0;
106 }
107 
108 
109 static void
schro_decoder_decode_slice_slow(SchroPicture * picture,SchroLowDelay * lowdelay,int slice_x,int slice_y,int offset,int slice_bytes)110 schro_decoder_decode_slice_slow (SchroPicture * picture,
111     SchroLowDelay * lowdelay,
112     int slice_x, int slice_y, int offset, int slice_bytes)
113 {
114   SchroParams *params = &picture->params;
115   SchroUnpack y_unpack;
116   SchroUnpack uv_unpack;
117   int quant_index;
118   int base_index;
119   int length_bits;
120   int slice_y_length;
121   int i;
122   int x, y;
123   int value;
124 
125   schro_unpack_init_with_data (&y_unpack,
126       OFFSET (picture->lowdelay_buffer->data, offset), slice_bytes, 1);
127 
128   base_index = schro_unpack_decode_bits (&y_unpack, 7);
129   length_bits = ilog2up (8 * slice_bytes);
130 
131   slice_y_length = schro_unpack_decode_bits (&y_unpack, length_bits);
132 
133   schro_unpack_copy (&uv_unpack, &y_unpack);
134   schro_unpack_limit_bits_remaining (&y_unpack, slice_y_length);
135   schro_unpack_skip_bits (&uv_unpack, slice_y_length);
136 
137   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
138     int quant_factor;
139     int quant_offset;
140     int16_t *line;
141     SchroFrameData block;
142 
143     schro_frame_data_get_codeblock (&block, lowdelay->luma_subbands + i,
144         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
145 
146     quant_index = CLAMP (base_index - params->quant_matrix[i], 0, 60);
147 
148     quant_factor = schro_table_quant[quant_index];
149     quant_offset = schro_table_offset_1_2[quant_index];
150 
151     for (y = 0; y < block.height; y++) {
152       line = SCHRO_FRAME_DATA_GET_LINE (&block, y);
153       for (x = 0; x < block.width; x++) {
154         value = schro_unpack_decode_sint (&y_unpack);
155         line[x] = schro_dequantise (value, quant_factor, quant_offset);
156       }
157     }
158   }
159 
160   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
161     int quant_factor;
162     int quant_offset;
163     int16_t *line1;
164     int16_t *line2;
165     SchroFrameData block1;
166     SchroFrameData block2;
167 
168     schro_frame_data_get_codeblock (&block1, lowdelay->chroma1_subbands + i,
169         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
170     schro_frame_data_get_codeblock (&block2, lowdelay->chroma2_subbands + i,
171         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
172 
173     quant_index = CLAMP (base_index - params->quant_matrix[i], 0, 60);
174     quant_factor = schro_table_quant[quant_index];
175     quant_offset = schro_table_offset_1_2[quant_index];
176 
177     for (y = 0; y < block1.height; y++) {
178       line1 = SCHRO_FRAME_DATA_GET_LINE (&block1, y);
179       line2 = SCHRO_FRAME_DATA_GET_LINE (&block2, y);
180       for (x = 0; x < block1.width; x++) {
181         value = schro_unpack_decode_sint (&uv_unpack);
182         line1[x] = schro_dequantise (value, quant_factor, quant_offset);
183         value = schro_unpack_decode_sint (&uv_unpack);
184         line2[x] = schro_dequantise (value, quant_factor, quant_offset);
185       }
186     }
187   }
188 }
189 
190 static void
schro_decoder_decode_slice_slow_s32(SchroPicture * picture,SchroLowDelay * lowdelay,int slice_x,int slice_y,int offset,int slice_bytes)191 schro_decoder_decode_slice_slow_s32 (SchroPicture * picture,
192     SchroLowDelay * lowdelay,
193     int slice_x, int slice_y, int offset, int slice_bytes)
194 {
195   SchroParams *params = &picture->params;
196   SchroUnpack y_unpack;
197   SchroUnpack uv_unpack;
198   int quant_index;
199   int base_index;
200   int length_bits;
201   int slice_y_length;
202   int i;
203   int x, y;
204   int value;
205 
206   schro_unpack_init_with_data (&y_unpack,
207       OFFSET (picture->lowdelay_buffer->data, offset), slice_bytes, 1);
208 
209   base_index = schro_unpack_decode_bits (&y_unpack, 7);
210   length_bits = ilog2up (8 * slice_bytes);
211 
212   slice_y_length = schro_unpack_decode_bits (&y_unpack, length_bits);
213 
214   schro_unpack_copy (&uv_unpack, &y_unpack);
215   schro_unpack_limit_bits_remaining (&y_unpack, slice_y_length);
216   schro_unpack_skip_bits (&uv_unpack, slice_y_length);
217 
218   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
219     int quant_factor;
220     int quant_offset;
221     int32_t *line;
222     SchroFrameData block;
223 
224     schro_frame_data_get_codeblock (&block, lowdelay->luma_subbands + i,
225         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
226 
227     quant_index = CLAMP (base_index - params->quant_matrix[i], 0, 60);
228 
229     quant_factor = schro_table_quant[quant_index];
230     quant_offset = schro_table_offset_1_2[quant_index];
231 
232     for (y = 0; y < block.height; y++) {
233       line = SCHRO_FRAME_DATA_GET_LINE (&block, y);
234       for (x = 0; x < block.width; x++) {
235         value = schro_unpack_decode_sint (&y_unpack);
236         line[x] = schro_dequantise (value, quant_factor, quant_offset);
237       }
238     }
239   }
240 
241   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
242     int quant_factor;
243     int quant_offset;
244     int32_t *line1;
245     int32_t *line2;
246     SchroFrameData block1;
247     SchroFrameData block2;
248 
249     schro_frame_data_get_codeblock (&block1, lowdelay->chroma1_subbands + i,
250         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
251     schro_frame_data_get_codeblock (&block2, lowdelay->chroma2_subbands + i,
252         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
253 
254     quant_index = CLAMP (base_index - params->quant_matrix[i], 0, 60);
255     quant_factor = schro_table_quant[quant_index];
256     quant_offset = schro_table_offset_1_2[quant_index];
257 
258     for (y = 0; y < block1.height; y++) {
259       line1 = SCHRO_FRAME_DATA_GET_LINE (&block1, y);
260       line2 = SCHRO_FRAME_DATA_GET_LINE (&block2, y);
261       for (x = 0; x < block1.width; x++) {
262         value = schro_unpack_decode_sint (&uv_unpack);
263         line1[x] = schro_dequantise (value, quant_factor, quant_offset);
264         value = schro_unpack_decode_sint (&uv_unpack);
265         line2[x] = schro_dequantise (value, quant_factor, quant_offset);
266       }
267     }
268   }
269 }
270 
271 
272 static void
schro_decoder_decode_slice_fast(SchroPicture * picture,SchroLowDelay * lowdelay,int slice_x,int slice_y,int offset,int slice_bytes)273 schro_decoder_decode_slice_fast (SchroPicture * picture,
274     SchroLowDelay * lowdelay,
275     int slice_x, int slice_y, int offset, int slice_bytes)
276 {
277   SchroUnpack y_unpack;
278   SchroUnpack uv_unpack;
279   int base_index;
280   int slice_y_length;
281   int16_t *quant_data;
282 
283   schro_unpack_init_with_data (&y_unpack,
284       OFFSET (picture->lowdelay_buffer->data, offset), slice_bytes, 1);
285 
286   base_index = schro_unpack_decode_bits (&y_unpack, 7);
287 
288   slice_y_length = schro_unpack_decode_bits (&y_unpack, lowdelay->length_bits);
289 
290   schro_unpack_copy (&uv_unpack, &y_unpack);
291   schro_unpack_limit_bits_remaining (&y_unpack, slice_y_length);
292   schro_unpack_skip_bits (&uv_unpack, slice_y_length);
293 
294   quant_data = lowdelay->quant_y_data + slice_x * lowdelay->slice_y_size;
295   schro_unpack_decode_sint_s16 (quant_data, &y_unpack, lowdelay->slice_y_size);
296 
297   orc_dequantise_var_s16_ip (quant_data,
298       lowdelay->y_quants + base_index * lowdelay->slice_y_size,
299       lowdelay->y_offsets + base_index * lowdelay->slice_y_size,
300       lowdelay->slice_y_size);
301 
302 
303   quant_data = lowdelay->quant_uv_data + slice_x * lowdelay->slice_uv_size;
304   schro_unpack_decode_sint_s16 (quant_data, &uv_unpack,
305       lowdelay->slice_uv_size);
306   orc_dequantise_var_s16_ip (quant_data,
307       lowdelay->uv_quants + base_index * lowdelay->slice_uv_size,
308       lowdelay->uv_offsets + base_index * lowdelay->slice_uv_size,
309       lowdelay->slice_uv_size);
310 }
311 
312 static void
schro_lowdelay_restride_slices(SchroPicture * picture,SchroLowDelay * lowdelay,int slice_y)313 schro_lowdelay_restride_slices (SchroPicture * picture,
314     SchroLowDelay * lowdelay, int slice_y)
315 {
316   int k;
317   int j;
318   int x, y;
319   int i;
320   int16_t *quant_data;
321 
322   quant_data = lowdelay->quant_y_data;
323   j = 0;
324   for (i = 0; i < lowdelay->n_subbands; i++) {
325     int16_t *line;
326     SchroFrameData block;
327 
328     block.data = SCHRO_FRAME_DATA_GET_PIXEL_S16 (lowdelay->luma_subbands + i,
329         0, (lowdelay->slice_y_height >> lowdelay->subband_shift[i]) * slice_y);
330     block.stride = lowdelay->luma_subbands[i].stride;
331 
332     for (y = 0; y < lowdelay->subbands[i][0].slice_height; y++) {
333       line = SCHRO_FRAME_DATA_GET_LINE (&block, y);
334 
335       switch (lowdelay->subbands[i][0].slice_width) {
336         case 1:
337           for (k = 0; k < lowdelay->n_horiz_slices; k++) {
338             line[k] =
339                 quant_data[k * lowdelay->slice_y_size + j];
340           }
341           j++;
342           break;
343         case 2:
344           for (k = 0; k < lowdelay->n_horiz_slices; k++) {
345             line[k * 2 + 0] =
346                 quant_data[k * lowdelay->slice_y_size + j + 0];
347             line[k * 2 + 1] =
348                 quant_data[k * lowdelay->slice_y_size + j + 1];
349           }
350           j += 2;
351           break;
352         case 4:
353           for (k = 0; k < lowdelay->n_horiz_slices; k++) {
354             memcpy (line + k * 4,
355                 quant_data + k * lowdelay->slice_y_size + j,
356                 sizeof (int16_t) * 4);
357           }
358           j += 4;
359           break;
360         case 8:
361           for (k = 0; k < lowdelay->n_horiz_slices; k++) {
362             memcpy (line + k * 8,
363                 quant_data + k * lowdelay->slice_y_size + j,
364                 sizeof (int16_t) * 8);
365           }
366           j += 8;
367           break;
368         default:
369           for (k = 0; k < lowdelay->n_horiz_slices; k++) {
370             memcpy (line + k * lowdelay->subbands[i][0].slice_width,
371                 quant_data + k * lowdelay->slice_y_size + j,
372                 sizeof (int16_t) * lowdelay->subbands[i][0].slice_width);
373           }
374           j += lowdelay->subbands[i][0].slice_width;
375           break;
376       }
377     }
378   }
379 
380   quant_data = lowdelay->quant_uv_data;
381   j = 0;
382   for (i = 0; i < lowdelay->n_subbands; i++) {
383     int16_t *line1;
384     int16_t *line2;
385     SchroFrameData block1;
386     SchroFrameData block2;
387 
388     block1.data =
389         SCHRO_FRAME_DATA_GET_PIXEL_S16 (lowdelay->chroma1_subbands + i, 0,
390         (lowdelay->slice_uv_height >> lowdelay->subband_shift[i]) * slice_y);
391     block1.stride = lowdelay->chroma1_subbands[i].stride;
392 
393     block2.data =
394         SCHRO_FRAME_DATA_GET_PIXEL_S16 (lowdelay->chroma2_subbands + i, 0,
395         (lowdelay->slice_uv_height >> lowdelay->subband_shift[i]) * slice_y);
396     block2.stride = lowdelay->chroma2_subbands[i].stride;
397 
398     for (y = 0; y < lowdelay->subbands[i][1].slice_height; y++) {
399       line1 = SCHRO_FRAME_DATA_GET_LINE (&block1, y);
400       line2 = SCHRO_FRAME_DATA_GET_LINE (&block2, y);
401 
402       for (k = 0; k < lowdelay->n_horiz_slices; k++) {
403         for (x = 0; x < lowdelay->subbands[i][1].slice_width; x++) {
404           line1[k * lowdelay->subbands[i][1].slice_width + x] =
405               quant_data[k * lowdelay->slice_uv_size + j + x * 2 + 0];
406           line2[k * lowdelay->subbands[i][1].slice_width + x] =
407               quant_data[k * lowdelay->slice_uv_size + j + x * 2 + 1];
408         }
409       }
410       j += lowdelay->subbands[i][1].slice_width * 2;
411     }
412   }
413 
414 }
415 
416 static void
schro_lowdelay_init(SchroLowDelay * lowdelay,SchroFrame * frame,SchroParams * params)417 schro_lowdelay_init (SchroLowDelay * lowdelay, SchroFrame * frame,
418     SchroParams * params)
419 {
420   int i;
421   int size;
422 
423   lowdelay->params = params;
424   lowdelay->frame = frame;
425   lowdelay->n_subbands = 1 + 3 * params->transform_depth;
426 
427   lowdelay->n_horiz_slices = params->n_horiz_slices;
428   lowdelay->n_vert_slices = params->n_vert_slices;
429 
430   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
431     int position = schro_subband_get_position (i);
432     SchroFrameData fd;
433 
434     schro_subband_get_frame_data (lowdelay->luma_subbands + i,
435         frame, 0, position, params);
436     schro_subband_get_frame_data (lowdelay->chroma1_subbands + i,
437         frame, 1, position, params);
438     schro_subband_get_frame_data (lowdelay->chroma2_subbands + i,
439         frame, 2, position, params);
440 
441     schro_frame_data_get_codeblock (&fd, lowdelay->luma_subbands + i,
442         0, 0, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
443     lowdelay->subbands[i][0].data = fd.data;
444     lowdelay->subbands[i][0].slice_width = fd.width;
445     lowdelay->subbands[i][0].slice_height = fd.height;
446     lowdelay->subbands[i][0].x_stride = fd.width * sizeof (int16_t);
447     lowdelay->subbands[i][0].y_stride = fd.height * fd.stride;
448 
449     schro_frame_data_get_codeblock (&fd, lowdelay->chroma1_subbands + i,
450         0, 0, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
451     lowdelay->subbands[i][1].data = fd.data;
452     lowdelay->subbands[i][1].slice_width = fd.width;
453     lowdelay->subbands[i][1].slice_height = fd.height;
454     lowdelay->subbands[i][1].x_stride = fd.width * sizeof (int16_t);
455     lowdelay->subbands[i][1].y_stride = fd.height * fd.stride;
456 
457     lowdelay->subband_shift[i] =
458         params->transform_depth - SCHRO_SUBBAND_SHIFT (position);
459   }
460 
461   size = 1000;
462   lowdelay->saved_dc_values = schro_malloc (sizeof (int16_t) * size);
463 }
464 
465 static void
schro_lowdelay_cleanup(SchroLowDelay * lowdelay)466 schro_lowdelay_cleanup (SchroLowDelay * lowdelay)
467 {
468 
469   schro_free (lowdelay->saved_dc_values);
470 }
471 
472 static void
schro_lowdelay_init_quant_arrays(SchroLowDelay * lowdelay)473 schro_lowdelay_init_quant_arrays (SchroLowDelay * lowdelay)
474 {
475   int base_index;
476   int i;
477   int j;
478   int x, y;
479 
480   j = 0;
481   for (base_index = 0; base_index < 60; base_index++) {
482     for (i = 0; i < lowdelay->n_subbands; i++) {
483       int quant_factor;
484       int quant_offset;
485       int quant_index;
486 
487       quant_index =
488           CLAMP (base_index - lowdelay->params->quant_matrix[i], 0, 60);
489 
490       quant_factor = schro_table_quant[quant_index];
491       quant_offset = schro_table_offset_1_2[quant_index];
492       for (y = 0; y < lowdelay->subbands[i][0].slice_height; y++) {
493         for (x = 0; x < lowdelay->subbands[i][0].slice_width; x++) {
494           lowdelay->y_quants[j] = quant_factor;
495           lowdelay->y_offsets[j] = quant_offset + 2;
496           j++;
497         }
498       }
499     }
500   }
501 
502   j = 0;
503   for (base_index = 0; base_index < 60; base_index++) {
504     for (i = 0; i < lowdelay->n_subbands; i++) {
505       int quant_index;
506       int quant_factor;
507       int quant_offset;
508       SchroFrameData block1;
509 
510       schro_frame_data_get_codeblock (&block1, lowdelay->chroma1_subbands + i,
511           0, 0, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
512 
513       quant_index =
514           CLAMP (base_index - lowdelay->params->quant_matrix[i], 0, 60);
515       quant_factor = schro_table_quant[quant_index];
516       quant_offset = schro_table_offset_1_2[quant_index];
517 
518       for (y = 0; y < block1.height; y++) {
519         for (x = 0; x < block1.width; x++) {
520           lowdelay->uv_quants[j] = quant_factor;
521           lowdelay->uv_offsets[j] = quant_offset + 2;
522           j++;
523           lowdelay->uv_quants[j] = quant_factor;
524           lowdelay->uv_offsets[j] = quant_offset + 2;
525           j++;
526         }
527       }
528     }
529   }
530 }
531 
532 static void
schro_lowdelay_init_memoffsets(SchroLowDelay * lowdelay)533 schro_lowdelay_init_memoffsets (SchroLowDelay * lowdelay)
534 {
535   int i, j;
536   int x, y;
537   int16_t *baseptr;
538 
539   baseptr = lowdelay->frame->components[0].data;
540 
541   j = 0;
542   for (i = 0; i < 1 + 3 * lowdelay->params->transform_depth; i++) {
543     int16_t *line;
544     SchroFrameData block;
545 
546     schro_frame_data_get_codeblock (&block, lowdelay->luma_subbands + i,
547         0, 0, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
548 
549     for (y = 0; y < lowdelay->subbands[i][0].slice_height; y++) {
550       line = SCHRO_FRAME_DATA_GET_LINE (&block, y);
551       for (x = 0; x < lowdelay->subbands[i][0].slice_width; x++) {
552         lowdelay->y_memoffsets[j] = ((char *) (line + x)) - ((char *) baseptr);
553         j++;
554       }
555     }
556   }
557 }
558 
559 void
schro_decoder_decode_lowdelay_transform_data_fast(SchroPicture * picture)560 schro_decoder_decode_lowdelay_transform_data_fast (SchroPicture * picture)
561 {
562   SchroParams *params = &picture->params;
563   SchroLowDelay lowdelay;
564   int x, y;
565   int n_bytes;
566   int remainder;
567   int accumulator;
568   int extra;
569   int offset;
570 
571   memset (&lowdelay, 0, sizeof (SchroLowDelay));
572   lowdelay.n_horiz_slices = params->n_horiz_slices;
573   lowdelay.n_vert_slices = params->n_vert_slices;
574   schro_lowdelay_init (&lowdelay, picture->transform_frame, params);
575 
576   n_bytes = params->slice_bytes_num / params->slice_bytes_denom;
577   remainder = params->slice_bytes_num % params->slice_bytes_denom;
578 
579   lowdelay.length_bits = ilog2up (8 * n_bytes);
580 
581   SCHRO_ASSERT ((params->iwt_luma_width % params->n_horiz_slices) == 0);
582   SCHRO_ASSERT ((params->iwt_luma_height % params->n_vert_slices) == 0);
583   SCHRO_ASSERT ((params->iwt_chroma_width % params->n_horiz_slices) == 0);
584   SCHRO_ASSERT ((params->iwt_chroma_height % params->n_vert_slices) == 0);
585 
586   lowdelay.slice_y_size = (params->iwt_luma_width / params->n_horiz_slices) *
587       (params->iwt_luma_height / params->n_vert_slices);
588   lowdelay.slice_uv_size = (params->iwt_chroma_width / params->n_horiz_slices) *
589       (params->iwt_chroma_height / params->n_vert_slices) * 2;
590 
591   lowdelay.slice_y_width = (params->iwt_luma_width / params->n_horiz_slices);
592   lowdelay.slice_y_height = (params->iwt_luma_height / params->n_vert_slices);
593   lowdelay.slice_uv_width = (params->iwt_chroma_width / params->n_horiz_slices);
594   lowdelay.slice_uv_height =
595       (params->iwt_chroma_height / params->n_vert_slices);
596 
597   lowdelay.quant_y_data = schro_malloc (sizeof (int16_t) *
598       lowdelay.slice_y_size * params->n_horiz_slices);
599   lowdelay.quant_uv_data = schro_malloc (sizeof (int16_t) *
600       lowdelay.slice_uv_size * params->n_horiz_slices);
601   lowdelay.y_quants =
602       schro_malloc (60 * sizeof (int16_t) * lowdelay.slice_y_size);
603   lowdelay.y_offsets =
604       schro_malloc (60 * sizeof (int16_t) * lowdelay.slice_y_size);
605   lowdelay.y_memoffsets = schro_malloc (sizeof (int) * lowdelay.slice_y_size);
606   lowdelay.uv_quants =
607       schro_malloc (60 * sizeof (int16_t) * lowdelay.slice_uv_size);
608   lowdelay.uv_offsets =
609       schro_malloc (60 * sizeof (int16_t) * lowdelay.slice_uv_size);
610   lowdelay.uv_memoffsets = schro_malloc (sizeof (int) * lowdelay.slice_uv_size);
611 
612   schro_lowdelay_init_quant_arrays (&lowdelay);
613   schro_lowdelay_init_memoffsets (&lowdelay);
614 
615   offset = 0;
616   accumulator = 0;
617   for (y = 0; y < lowdelay.n_vert_slices; y++) {
618 
619     for (x = 0; x < lowdelay.n_horiz_slices; x++) {
620       accumulator += remainder;
621       if (accumulator >= params->slice_bytes_denom) {
622         extra = 1;
623         accumulator -= params->slice_bytes_denom;
624       } else {
625         extra = 0;
626       }
627 
628       schro_decoder_decode_slice_fast (picture, &lowdelay,
629           x, y, offset, n_bytes + extra);
630       offset += n_bytes + extra;
631     }
632 
633     schro_lowdelay_restride_slices (picture, &lowdelay, y);
634   }
635 
636   schro_decoder_subband_dc_predict (lowdelay.luma_subbands + 0);
637   schro_decoder_subband_dc_predict (lowdelay.chroma1_subbands + 0);
638   schro_decoder_subband_dc_predict (lowdelay.chroma2_subbands + 0);
639 
640   schro_free (lowdelay.quant_y_data);
641   schro_free (lowdelay.quant_uv_data);
642   schro_free (lowdelay.y_quants);
643   schro_free (lowdelay.y_offsets);
644   schro_free (lowdelay.y_memoffsets);
645   schro_free (lowdelay.uv_quants);
646   schro_free (lowdelay.uv_offsets);
647   schro_free (lowdelay.uv_memoffsets);
648   schro_lowdelay_cleanup (&lowdelay);
649 }
650 
651 void
schro_decoder_decode_lowdelay_transform_data_slow(SchroPicture * picture)652 schro_decoder_decode_lowdelay_transform_data_slow (SchroPicture * picture)
653 {
654   SchroParams *params = &picture->params;
655   SchroLowDelay lowdelay;
656   int x, y;
657   int n_bytes;
658   int remainder;
659   int accumulator;
660   int extra;
661   int offset;
662 
663   memset (&lowdelay, 0, sizeof (SchroLowDelay));
664   schro_lowdelay_init (&lowdelay, picture->transform_frame, params);
665 
666   lowdelay.n_horiz_slices = params->n_horiz_slices;
667   lowdelay.n_vert_slices = params->n_vert_slices;
668 
669   n_bytes = params->slice_bytes_num / params->slice_bytes_denom;
670   remainder = params->slice_bytes_num % params->slice_bytes_denom;
671 
672   offset = 0;
673   accumulator = 0;
674   for (y = 0; y < lowdelay.n_vert_slices; y++) {
675 
676     for (x = 0; x < lowdelay.n_horiz_slices; x++) {
677       accumulator += remainder;
678       if (accumulator >= params->slice_bytes_denom) {
679         extra = 1;
680         accumulator -= params->slice_bytes_denom;
681       } else {
682         extra = 0;
683       }
684 
685       schro_decoder_decode_slice_slow (picture, &lowdelay,
686           x, y, offset, n_bytes + extra);
687       offset += n_bytes + extra;
688     }
689   }
690 
691   schro_decoder_subband_dc_predict (lowdelay.luma_subbands + 0);
692   schro_decoder_subband_dc_predict (lowdelay.chroma1_subbands + 0);
693   schro_decoder_subband_dc_predict (lowdelay.chroma2_subbands + 0);
694 
695   schro_lowdelay_cleanup (&lowdelay);
696 }
697 
698 void
schro_decoder_decode_lowdelay_transform_data_slow_s32(SchroPicture * picture)699 schro_decoder_decode_lowdelay_transform_data_slow_s32 (SchroPicture * picture)
700 {
701   SchroParams *params = &picture->params;
702   SchroLowDelay lowdelay;
703   int x, y;
704   int n_bytes;
705   int remainder;
706   int accumulator;
707   int extra;
708   int offset;
709 
710   memset (&lowdelay, 0, sizeof (SchroLowDelay));
711   schro_lowdelay_init (&lowdelay, picture->transform_frame, params);
712 
713   lowdelay.n_horiz_slices = params->n_horiz_slices;
714   lowdelay.n_vert_slices = params->n_vert_slices;
715 
716   n_bytes = params->slice_bytes_num / params->slice_bytes_denom;
717   remainder = params->slice_bytes_num % params->slice_bytes_denom;
718 
719   offset = 0;
720   accumulator = 0;
721   for (y = 0; y < lowdelay.n_vert_slices; y++) {
722 
723     for (x = 0; x < lowdelay.n_horiz_slices; x++) {
724       accumulator += remainder;
725       if (accumulator >= params->slice_bytes_denom) {
726         extra = 1;
727         accumulator -= params->slice_bytes_denom;
728       } else {
729         extra = 0;
730       }
731 
732       schro_decoder_decode_slice_slow_s32 (picture, &lowdelay,
733           x, y, offset, n_bytes + extra);
734       offset += n_bytes + extra;
735     }
736   }
737 
738   schro_decoder_subband_dc_predict_s32 (lowdelay.luma_subbands + 0);
739   schro_decoder_subband_dc_predict_s32 (lowdelay.chroma1_subbands + 0);
740   schro_decoder_subband_dc_predict_s32 (lowdelay.chroma2_subbands + 0);
741 
742   schro_lowdelay_cleanup (&lowdelay);
743 }
744 
745 
746 void
schro_decoder_decode_lowdelay_transform_data(SchroPicture * picture)747 schro_decoder_decode_lowdelay_transform_data (SchroPicture * picture)
748 {
749   SchroParams *params = &picture->params;
750 
751   if (SCHRO_FRAME_FORMAT_DEPTH (picture->transform_frame->format) ==
752       SCHRO_FRAME_FORMAT_DEPTH_S32) {
753     return schro_decoder_decode_lowdelay_transform_data_slow_s32 (picture);
754   } else if ((params->iwt_chroma_width >> params->transform_depth) %
755       params->n_horiz_slices == 0 &&
756       (params->iwt_chroma_height >> params->transform_depth) %
757       params->n_vert_slices == 0) {
758     return schro_decoder_decode_lowdelay_transform_data_fast (picture);
759   } else {
760     return schro_decoder_decode_lowdelay_transform_data_slow (picture);
761   }
762 }
763 
764 #ifdef ENABLE_ENCODER
765 static int
schro_dc_predict(int16_t * line,int stride,int x,int y)766 schro_dc_predict (int16_t * line, int stride, int x, int y)
767 {
768   int16_t *prev_line = OFFSET (line, -stride);
769 
770   if (y > 0) {
771     if (x > 0) {
772       return schro_divide3 (line[-1] + prev_line[0] + prev_line[-1] + 1);
773     } else {
774       return prev_line[0];
775     }
776   } else {
777     if (x > 0) {
778       return line[-1];
779     } else {
780       return 0;
781     }
782   }
783 }
784 
785 static int
schro_encoder_encode_slice(SchroEncoderFrame * frame,SchroLowDelay * lowdelay,int slice_x,int slice_y,int slice_bytes,int base_index)786 schro_encoder_encode_slice (SchroEncoderFrame * frame,
787     SchroLowDelay * lowdelay,
788     int slice_x, int slice_y, int slice_bytes, int base_index)
789 {
790   int length_bits;
791   int slice_y_length;
792   int i;
793   int start_bits;
794   int end_bits;
795   int16_t *quant_data = frame->quant_data;
796 
797   start_bits = schro_pack_get_bit_offset (frame->pack);
798 
799   schro_pack_encode_bits (frame->pack, 7, base_index);
800   length_bits = ilog2up (8 * slice_bytes);
801 
802   slice_y_length = frame->slice_y_bits - frame->slice_y_trailing_zeros;
803   schro_pack_encode_bits (frame->pack, length_bits, slice_y_length);
804 
805   for (i = 0; i < lowdelay->slice_y_size - frame->slice_y_trailing_zeros; i++) {
806     schro_pack_encode_sint (frame->pack, quant_data[i]);
807   }
808 
809   quant_data += lowdelay->slice_y_size;
810   for (i = 0; i < lowdelay->slice_uv_size - frame->slice_uv_trailing_zeros / 2;
811       i++) {
812     schro_pack_encode_sint (frame->pack, quant_data[i]);
813     schro_pack_encode_sint (frame->pack,
814         quant_data[i + lowdelay->slice_uv_size]);
815   }
816 
817   end_bits = schro_pack_get_bit_offset (frame->pack);
818   SCHRO_DEBUG ("total bits %d used bits %d expected %d", slice_bytes * 8,
819       end_bits - start_bits,
820       7 + length_bits + frame->slice_y_bits + frame->slice_uv_bits -
821       frame->slice_y_trailing_zeros - frame->slice_uv_trailing_zeros);
822   SCHRO_ASSERT (end_bits - start_bits ==
823       7 + length_bits + frame->slice_y_bits + frame->slice_uv_bits -
824       frame->slice_y_trailing_zeros - frame->slice_uv_trailing_zeros);
825 
826   if (end_bits - start_bits > slice_bytes * 8) {
827     SCHRO_ERROR
828         ("slice overran buffer by %d bits (slice_bytes %d base_index %d)",
829         end_bits - start_bits - slice_bytes * 8, slice_bytes, base_index);
830     SCHRO_ASSERT (0);
831   } else {
832     int left = slice_bytes * 8 - (end_bits - start_bits);
833     for (i = 0; i < left; i++) {
834       schro_pack_encode_bit (frame->pack, 1);
835     }
836   }
837 
838   return end_bits - start_bits;
839 }
840 
841 static int
estimate_array(int16_t * data,int n)842 estimate_array (int16_t * data, int n)
843 {
844   int i;
845   int n_bits = 0;
846 
847   for (i = 0; i < n; i++) {
848     n_bits += schro_pack_estimate_sint (data[i]);
849   }
850   return n_bits;
851 }
852 
853 static void
quantise_block(SchroFrameData * block,int16_t * quant_data,int quant_index)854 quantise_block (SchroFrameData * block, int16_t * quant_data, int quant_index)
855 {
856   int quant_factor;
857   int quant_offset;
858   int x, y;
859   int n = 0;
860   int16_t *line;
861 
862   quant_factor = schro_table_quant[quant_index];
863   quant_offset = schro_table_offset_1_2[quant_index];
864 
865   for (y = 0; y < block->height; y++) {
866     line = SCHRO_FRAME_DATA_GET_LINE (block, y);
867     for (x = 0; x < block->width; x++) {
868       quant_data[n] = schro_quantise (line[x], quant_factor, quant_offset);
869       n++;
870     }
871   }
872 }
873 
874 static void
quantise_dc_block(SchroFrameData * block,int16_t * quant_data,int quant_index,int slice_x,int slice_y,SchroFrameData * reconstructed_block)875 quantise_dc_block (SchroFrameData * block, int16_t * quant_data,
876     int quant_index, int slice_x, int slice_y,
877     SchroFrameData *reconstructed_block)
878 {
879   int quant_factor;
880   int quant_offset;
881   int x, y;
882   int n = 0;
883   int pred_value;
884   int16_t *line;
885   int16_t *rline;
886 
887   quant_factor = schro_table_quant[quant_index];
888   quant_offset = schro_table_offset_1_2[quant_index];
889 
890   for (y = 0; y < block->height; y++) {
891     line = SCHRO_FRAME_DATA_GET_LINE (block, y);
892     rline = SCHRO_FRAME_DATA_GET_LINE (reconstructed_block, y);
893     for (x = 0; x < block->width; x++) {
894       rline[x] = line[x];
895       pred_value = schro_dc_predict (rline + x, reconstructed_block->stride,
896           slice_x + x, slice_y + y);
897       quant_data[n] = schro_quantise (line[x] - pred_value,
898           quant_factor, quant_offset);
899       rline[x] = pred_value + schro_dequantise (quant_data[n],
900           quant_factor, quant_offset);
901       n++;
902     }
903   }
904 }
905 
906 static void
dequantise_block(SchroFrameData * block,int16_t * quant_data,int quant_index)907 dequantise_block (SchroFrameData * block, int16_t * quant_data, int quant_index)
908 {
909   int quant_factor;
910   int quant_offset;
911   int x, y;
912   int n = 0;
913   int16_t *line;
914 
915   quant_factor = schro_table_quant[quant_index];
916   quant_offset = schro_table_offset_1_2[quant_index];
917 
918   for (y = 0; y < block->height; y++) {
919     line = SCHRO_FRAME_DATA_GET_LINE (block, y);
920     for (x = 0; x < block->width; x++) {
921       line[x] = schro_dequantise (quant_data[n], quant_factor, quant_offset);
922       n++;
923     }
924   }
925 }
926 
927 static int
schro_encoder_estimate_slice(SchroEncoderFrame * frame,SchroLowDelay * lowdelay,int slice_x,int slice_y,int slice_bytes,int base_index)928 schro_encoder_estimate_slice (SchroEncoderFrame * frame,
929     SchroLowDelay * lowdelay,
930     int slice_x, int slice_y, int slice_bytes, int base_index)
931 {
932   SchroParams *params = &frame->params;
933   int quant_index;
934   int i;
935   int n_bits;
936   int n;
937   int16_t *quant_data = frame->quant_data;
938 
939   n_bits = 7 + ilog2up (8 * slice_bytes);
940 
941   /* Figure out how many values are in each component. */
942   /* FIXME this should go somewhere else or be elimitated */
943   lowdelay->slice_y_size = 0;
944   lowdelay->slice_uv_size = 0;
945   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
946     SchroFrameData block;
947 
948     schro_frame_data_get_codeblock (&block, lowdelay->luma_subbands + i,
949         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
950     lowdelay->slice_y_size += block.height * block.width;
951 
952     schro_frame_data_get_codeblock (&block, lowdelay->chroma1_subbands + i,
953         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
954     lowdelay->slice_uv_size += block.height * block.width;
955   }
956 
957   /* Estimate Y */
958   n = 0;
959   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
960     SchroFrameData block;
961 
962     schro_frame_data_get_codeblock (&block, lowdelay->luma_subbands + i,
963         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
964 
965     quant_index = CLAMP (base_index - params->quant_matrix[i], 0, 60);
966 
967     if (i == 0) {
968       SchroFrameData reconstructed_block;
969 
970       schro_frame_data_get_codeblock (&reconstructed_block,
971           lowdelay->reconstructed_frame->components + 0,
972           slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
973       quantise_dc_block (&block, quant_data + n, quant_index,
974           (lowdelay->luma_subbands[i].width * slice_x) /
975           lowdelay->n_horiz_slices,
976           (lowdelay->luma_subbands[i].height * slice_y) /
977           lowdelay->n_vert_slices, &reconstructed_block);
978     } else {
979       quantise_block (&block, quant_data + n, quant_index);
980     }
981     n += block.height * block.width;
982   }
983 #ifdef USE_TRAILING_DEAD_ZONE
984   for (i = 0; i < n; i++) {
985     if (quant_data[n - 1 - i] < -1 || quant_data[n - 1 - i] > 1)
986       break;
987     quant_data[n - 1 - i] = 0;
988   }
989 #endif
990   frame->slice_y_bits = estimate_array (quant_data, n);
991 
992   for (i = 0; i < n; i++) {
993     if (quant_data[n - 1 - i] != 0)
994       break;
995   }
996   frame->slice_y_trailing_zeros = i;
997 
998   /* Estimate UV */
999   n = 0;
1000   quant_data += lowdelay->slice_y_size;
1001   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
1002     SchroFrameData block1;
1003     SchroFrameData block2;
1004 
1005     schro_frame_data_get_codeblock (&block1, lowdelay->chroma1_subbands + i,
1006         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
1007     schro_frame_data_get_codeblock (&block2, lowdelay->chroma2_subbands + i,
1008         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
1009 
1010     quant_index = CLAMP (base_index - params->quant_matrix[i], 0, 60);
1011 
1012     if (i == 0) {
1013       SchroFrameData reconstructed_block;
1014 
1015       schro_frame_data_get_codeblock (&reconstructed_block,
1016           lowdelay->reconstructed_frame->components + 1,
1017           slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
1018       quantise_dc_block (&block1, quant_data + n, quant_index,
1019           (lowdelay->chroma1_subbands[i].width * slice_x) /
1020           lowdelay->n_horiz_slices,
1021           (lowdelay->chroma1_subbands[i].height * slice_y) /
1022           lowdelay->n_vert_slices, &reconstructed_block);
1023 
1024       schro_frame_data_get_codeblock (&reconstructed_block,
1025           lowdelay->reconstructed_frame->components + 2,
1026           slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
1027       quantise_dc_block (&block2, quant_data + n + lowdelay->slice_uv_size,
1028           quant_index,
1029           (lowdelay->chroma1_subbands[i].width * slice_x) /
1030           lowdelay->n_horiz_slices,
1031           (lowdelay->chroma1_subbands[i].height * slice_y) /
1032           lowdelay->n_vert_slices, &reconstructed_block);
1033     } else {
1034       quantise_block (&block1, quant_data + n, quant_index);
1035       quantise_block (&block2, quant_data + n + lowdelay->slice_uv_size,
1036           quant_index);
1037     }
1038     n += block1.height * block1.width;
1039   }
1040 #ifdef USE_TRAILING_DEAD_ZONE
1041   for (i = 0; i < n; i++) {
1042     if (quant_data[n - 1 - i] < -1 || quant_data[n - 1 - i] > 1)
1043       break;
1044     if (quant_data[2 * n - 1 - i] < -1 || quant_data[2 * n - 1 - i] > 1)
1045       break;
1046     quant_data[n - 1 - i] = 0;
1047     quant_data[2 * n - 1 - i] = 0;
1048   }
1049 #endif
1050   frame->slice_uv_bits = estimate_array (quant_data, n * 2);
1051 
1052   for (i = 0; i < n; i++) {
1053     if (quant_data[n - 1 - i] != 0)
1054       break;
1055     if (quant_data[2 * n - 1 - i] != 0)
1056       break;
1057   }
1058   frame->slice_uv_trailing_zeros = 2 * i;
1059 
1060   return n_bits + frame->slice_y_bits + frame->slice_uv_bits -
1061       frame->slice_y_trailing_zeros - frame->slice_uv_trailing_zeros;
1062 }
1063 
1064 static void
schro_encoder_dequantise_slice(SchroEncoderFrame * frame,SchroLowDelay * lowdelay,int slice_x,int slice_y,int slice_bytes,int base_index)1065 schro_encoder_dequantise_slice (SchroEncoderFrame * frame,
1066     SchroLowDelay * lowdelay,
1067     int slice_x, int slice_y, int slice_bytes, int base_index)
1068 {
1069   SchroParams *params = &frame->params;
1070   int quant_index;
1071   int i;
1072   int n;
1073   int16_t *quant_data = frame->quant_data;
1074 
1075   n = 0;
1076   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
1077     SchroFrameData block;
1078 
1079     schro_frame_data_get_codeblock (&block, lowdelay->luma_subbands + i,
1080         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
1081 
1082     quant_index = CLAMP (base_index - params->quant_matrix[i], 0, 60);
1083 
1084     if (i == 0) {
1085       /* dc dequant is handled by estimation */
1086     } else {
1087       dequantise_block (&block, quant_data + n, quant_index);
1088     }
1089     n += block.height * block.width;
1090   }
1091 
1092   n = 0;
1093   quant_data += lowdelay->slice_y_size;
1094   for (i = 0; i < 1 + 3 * params->transform_depth; i++) {
1095     SchroFrameData block1;
1096     SchroFrameData block2;
1097 
1098     schro_frame_data_get_codeblock (&block1, lowdelay->chroma1_subbands + i,
1099         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
1100     schro_frame_data_get_codeblock (&block2, lowdelay->chroma2_subbands + i,
1101         slice_x, slice_y, lowdelay->n_horiz_slices, lowdelay->n_vert_slices);
1102 
1103     quant_index = CLAMP (base_index - params->quant_matrix[i], 0, 60);
1104 
1105     if (i == 0) {
1106       /* dc dequant is handled by estimation */
1107     } else {
1108       dequantise_block (&block1, quant_data + n, quant_index);
1109       dequantise_block (&block2, quant_data + n + lowdelay->slice_uv_size,
1110           quant_index);
1111     }
1112     n += block1.height * block1.width;
1113   }
1114 }
1115 
1116 static int
schro_encoder_pick_slice_index(SchroEncoderFrame * frame,SchroLowDelay * lowdelay,int slice_x,int slice_y,int slice_bytes)1117 schro_encoder_pick_slice_index (SchroEncoderFrame * frame,
1118     SchroLowDelay * lowdelay, int slice_x, int slice_y, int slice_bytes)
1119 {
1120   int i;
1121   int n;
1122   int size;
1123 
1124   i = 0;
1125   n = schro_encoder_estimate_slice (frame, lowdelay,
1126       slice_x, slice_y, slice_bytes, i);
1127   if (n <= slice_bytes * 8) {
1128     schro_encoder_dequantise_slice (frame, lowdelay,
1129         slice_x, slice_y, slice_bytes, i);
1130     return i;
1131   }
1132 
1133   size = 32;
1134   while (size >= 1) {
1135     n = schro_encoder_estimate_slice (frame, lowdelay,
1136         slice_x, slice_y, slice_bytes, i + size);
1137     if (n >= slice_bytes * 8) {
1138       i += size;
1139     }
1140     size >>= 1;
1141   }
1142 
1143   schro_encoder_estimate_slice (frame, lowdelay,
1144       slice_x, slice_y, slice_bytes, i + 1);
1145   schro_encoder_dequantise_slice (frame, lowdelay,
1146       slice_x, slice_y, slice_bytes, i + 1);
1147   return i + 1;
1148 }
1149 
1150 void
schro_encoder_encode_lowdelay_transform_data(SchroEncoderFrame * frame)1151 schro_encoder_encode_lowdelay_transform_data (SchroEncoderFrame * frame)
1152 {
1153   SchroParams *params = &frame->params;
1154   SchroLowDelay lowdelay;
1155   int x, y;
1156   int n_bytes;
1157   int remainder;
1158   int accumulator;
1159   int extra;
1160   int base_index;
1161   int total_bits;
1162 
1163   schro_lowdelay_init (&lowdelay, frame->iwt_frame, params);
1164   lowdelay.reconstructed_frame = schro_frame_new_and_alloc (NULL,
1165       frame->iwt_frame->format,
1166       lowdelay.luma_subbands[0].width, lowdelay.luma_subbands[0].height);
1167 
1168   lowdelay.n_horiz_slices = params->n_horiz_slices;
1169   lowdelay.n_vert_slices = params->n_vert_slices;
1170 
1171   n_bytes = params->slice_bytes_num / params->slice_bytes_denom;
1172   remainder = params->slice_bytes_num % params->slice_bytes_denom;
1173 
1174   accumulator = 0;
1175   total_bits = 0;
1176   for (y = 0; y < lowdelay.n_vert_slices; y++) {
1177 
1178     for (x = 0; x < lowdelay.n_horiz_slices; x++) {
1179       accumulator += remainder;
1180       if (accumulator >= params->slice_bytes_denom) {
1181         extra = 1;
1182         accumulator -= params->slice_bytes_denom;
1183       } else {
1184         extra = 0;
1185       }
1186 
1187       base_index = schro_encoder_pick_slice_index (frame, &lowdelay,
1188           x, y, n_bytes + extra);
1189       total_bits += schro_encoder_encode_slice (frame, &lowdelay,
1190           x, y, n_bytes + extra, base_index);
1191     }
1192   }
1193 
1194   SCHRO_INFO ("used bits %d of %d", total_bits,
1195       lowdelay.n_horiz_slices * lowdelay.n_vert_slices *
1196       params->slice_bytes_num * 8 / params->slice_bytes_denom);
1197 
1198   schro_frame_unref (lowdelay.reconstructed_frame);
1199   schro_lowdelay_cleanup (&lowdelay);
1200 }
1201 #endif
1202