1 
2 /*************************************************************************
3 *
4 * This software module was originally contributed by Microsoft
5 * Corporation in the course of development of the
6 * ITU-T T.832 | ISO/IEC 29199-2 ("JPEG XR") format standard for
7 * reference purposes and its performance may not have been optimized.
8 *
9 * This software module is an implementation of one or more
10 * tools as specified by the JPEG XR standard.
11 *
12 * ITU/ISO/IEC give You a royalty-free, worldwide, non-exclusive
13 * copyright license to copy, distribute, and make derivative works
14 * of this software module or modifications thereof for use in
15 * products claiming conformance to the JPEG XR standard as
16 * specified by ITU-T T.832 | ISO/IEC 29199-2.
17 *
18 * ITU/ISO/IEC give users the same free license to this software
19 * module or modifications thereof for research purposes and further
20 * ITU/ISO/IEC standardization.
21 *
22 * Those intending to use this software module in products are advised
23 * that its use may infringe existing patents. ITU/ISO/IEC have no
24 * liability for use of this software module or modifications thereof.
25 *
26 * Copyright is not released for products that do not conform to
27 * to the JPEG XR standard as specified by ITU-T T.832 |
28 * ISO/IEC 29199-2.
29 *
30 * Microsoft Corporation retains full right to modify and use the code
31 * for its own purpose, to assign or donate the code to a third party,
32 * and to inhibit third parties from using the code for products that
33 * do not conform to the JPEG XR standard as specified by ITU-T T.832 |
34 * ISO/IEC 29199-2.
35 *
36 * This copyright notice must be included in all copies or derivative
37 * works.
38 *
39 * Copyright (c) ITU-T/ISO/IEC 2008, 2009.
40 ***********************************************************************/
41 
42 #ifdef _MSC_VER
43 #pragma comment (user,"$Id: r_parse.c,v 1.39 2008/03/24 18:06:56 steve Exp $")
44 #else
45 #ident "$Id: r_parse.c,v 1.39 2008/03/24 18:06:56 steve Exp $"
46 #endif
47 
48 # include "jxr_priv.h"
49 # include <stdlib.h>
50 # include <memory.h>
51 # include <assert.h>
52 
53 static int r_image_header(jxr_image_t image, struct rbitstream*str);
54 static int r_image_plane_header(jxr_image_t image, struct rbitstream*str, int alpha);
55 static int r_INDEX_TABLE(jxr_image_t image, struct rbitstream*str);
56 static int64_t r_PROFILE_LEVEL_INFO(jxr_image_t image, struct rbitstream*str);
57 static int r_TILE(jxr_image_t image, struct rbitstream*str);
58 
59 static int r_HP_QP(jxr_image_t image, struct rbitstream*str);
60 
61 static int32_t r_DEC_DC(jxr_image_t image, struct rbitstream*str,
62                         unsigned tx, unsigned ty,
63                         unsigned mx, unsigned my,
64                         int model_bits, int chroma_flag, int is_dc_ch);
65 static uint32_t r_DECODE_ABS_LEVEL(jxr_image_t image, struct rbitstream*str,
66                                    int band, int chroma_flag);
67 static int r_DECODE_FIRST_INDEX(jxr_image_t image, struct rbitstream*str,
68                                 int chroma_flag, int band);
69 static int r_DECODE_INDEX(jxr_image_t image, struct rbitstream*str,
70                           int location, int chroma_flag, int band, int context);
71 static int r_DECODE_RUN(jxr_image_t image, struct rbitstream*str, int max_run);
72 static int r_REFINE_LP(struct rbitstream*str, int coeff, int model_bits);
73 static int r_REFINE_CBP(struct rbitstream*str, int cbp);
74 static void r_PredCBP(jxr_image_t image, int*diff_cbp,
75                       unsigned tx, unsigned ty,
76                       unsigned mx, unsigned my);
77 static int r_DECODE_BLOCK_ADAPTIVE(jxr_image_t image, struct rbitstream*str,
78                                    unsigned tx, unsigned mx,
79                                    int cbp_flag, int chroma_flag,
80                                    int channel, int block, int mbhp_pred_mode,
81                                    unsigned model_bits);
82 static void r_BLOCK_FLEXBITS(jxr_image_t image, struct rbitstream*str,
83                              unsigned tx, unsigned ty,
84                              unsigned mx, unsigned my,
85                              unsigned chan, unsigned bl, unsigned model_bits);
86 static int r_calculate_mbhp_mode(jxr_image_t image, int tx, int mx);
87 static int get_is_dc_yuv(struct rbitstream*str);
88 static int dec_cbp_yuv_lp1(jxr_image_t image, struct rbitstream*str);
89 static int dec_abslevel_index(jxr_image_t image, struct rbitstream*str, int vlc_select);
90 static int get_num_cbp(struct rbitstream*str, struct adaptive_vlc_s*vlc);
91 static int get_num_blkcbp(jxr_image_t image, struct rbitstream*str, struct adaptive_vlc_s*vlc);
92 static int get_value_012(struct rbitstream*str);
93 static int get_num_ch_blk(struct rbitstream*str);
94 
95 
96 
jxr_read_image_bitstream(jxr_image_t image,FILE * fd)97 int jxr_read_image_bitstream(jxr_image_t image, FILE*fd)
98 {
99     int rc;
100     struct rbitstream bits;
101     _jxr_rbitstream_initialize(&bits, fd);
102 
103     /* Image header for the image overall */
104     rc = r_image_header(image, &bits);
105     if (rc < 0) return rc;
106 
107     /* Image plane. */
108     rc = r_image_plane_header(image, &bits, 0);
109     if (rc < 0) return rc;
110 
111     /* Make image structures that need header details. */
112     _jxr_make_mbstore(image, 0);
113 
114     /* If there is an alpa channel, process the image place header
115     for it. */
116     if (ALPHACHANNEL_FLAG(image)) {
117         int ch;
118 
119         image->alpha = jxr_create_input();
120         *image->alpha = *image;
121 
122         rc = r_image_plane_header(image->alpha, &bits, 1);
123         if (rc < 0) return rc;
124 
125         for(ch = 0; ch < image->num_channels; ch ++)
126             memset(&image->alpha->strip[ch], 0, sizeof(image->alpha->strip[ch]));
127 
128         _jxr_make_mbstore(image->alpha, 0);
129         image->alpha->primary = 0;
130     }
131 
132     rc = r_INDEX_TABLE(image, &bits);
133 
134     /* Store command line input values for later comparison */
135     uint8_t input_profile = image->profile_idc;
136     uint8_t input_level = image->level_idc;
137 
138     /* inferred value as per Appendix B */
139     image->profile_idc = 111;
140     image->level_idc = 255;
141 
142     int64_t subsequent_bytes = _jxr_rbitstream_intVLW(&bits);
143     DEBUG(" Subsequent bytes with %ld bytes\n", subsequent_bytes);
144     if (subsequent_bytes > 0) {
145         int64_t read_bytes = r_PROFILE_LEVEL_INFO(image,&bits);
146         int64_t additional_bytes = subsequent_bytes - read_bytes;
147         int64_t idx;
148         for (idx = 0 ; idx < additional_bytes ; idx += 1) {
149             _jxr_rbitstream_uint8(&bits); /* RESERVED_A_BYTE */
150         }
151     }
152 
153     assert(image->profile_idc <= input_profile);
154     assert(image->level_idc <= input_level);
155 
156     rc = jxr_test_PROFILE_IDC(image, 1);
157     rc = jxr_test_LEVEL_IDC(image, 1);
158 
159     DEBUG("MARK HERE as the tile base. bitpos=%zu\n", _jxr_rbitstream_bitpos(&bits));
160     _jxr_rbitstream_mark(&bits);
161 
162     /* The image data is in a TILE element even if there is no
163     tiling. No tiling just means 1 big tile. */
164     rc = r_TILE(image, &bits);
165 
166     DEBUG("Consumed %zu bytes of the bitstream\n", bits.read_count);
167 
168 #ifdef VERIFY_16BIT
169     if(image->lwf_test == 0)
170         DEBUG("Meet conditions for LONG_WORD_FLAG == 0!");
171     else {
172         DEBUG("Don't meet conditions for LONG_WORD_FLAG == 0!");
173         if (LONG_WORD_FLAG(image) == 0)
174             return JXR_EC_BADFORMAT;
175     }
176 #endif
177 
178     return rc;
179 }
180 
jxr_test_LONG_WORD_FLAG(jxr_image_t image,int flag)181 int jxr_test_LONG_WORD_FLAG(jxr_image_t image, int flag)
182 {
183 #ifdef VERIFY_16BIT
184     if (flag == 0 && image->lwf_test != 0) {
185         DEBUG("Using LONG_WORD_FLAG decoder but did not meet LONG_WORD_FLAG == 0 conditions!");
186         return JXR_EC_BADFORMAT;
187     }
188     else
189 #endif
190         return 0;
191 
192 }
193 
194 #if defined(DETAILED_DEBUG)
195 static const char*bitdepth_names[16] = {
196     "BD1WHITE1", "BD8", "BD16", "BD16S",
197     "BD16F", "RESERVED5", "BD32S", "BD32F",
198     "BD5", "BD10", "BD565", "RESERVED11"
199     "RESERVED12", "RESERVED12", "RESERVED12","BD1BLACK1"
200 };
201 
202 #endif
203 
r_image_header(jxr_image_t image,struct rbitstream * str)204 static int r_image_header(jxr_image_t image, struct rbitstream*str)
205 {
206     const char GDI_SIG[] = "WMPHOTO\0";
207     unsigned idx;
208 
209     unsigned version_info, version_sub_info;
210 
211     /* Read and test the GDI_SIGNATURE magic number */
212     for (idx = 0 ; idx < 8 ; idx += 1) {
213         uint8_t byte = _jxr_rbitstream_uint8(str);
214         if (byte != GDI_SIG[idx]) {
215             return JXR_EC_BADMAGIC;
216         }
217     }
218 
219     DEBUG("Got magic number.\n");
220     DEBUG("START IMAGE_HEADER (bitpos=%zu)\n", _jxr_rbitstream_bitpos(str));
221 
222     /* Get the version info */
223     version_info = _jxr_rbitstream_uint4(str);
224 
225     image->disableTileOverlapFlag = _jxr_rbitstream_uint1(str);
226     DEBUG("  disableTileOverlapFlag: %d\n", image->disableTileOverlapFlag);
227 
228     version_sub_info = _jxr_rbitstream_uint3(str);
229     DEBUG("  Version: %u.%u\n", version_info, version_sub_info);
230 
231     /* Read some of the flags as a group. There are a bunch of
232     small flag values together here, so it is economical to
233     just collect them all at once. */
234     image->header_flags1 = _jxr_rbitstream_uint8(str);
235     image->header_flags2 = _jxr_rbitstream_uint8(str);
236     image->header_flags_fmt = _jxr_rbitstream_uint8(str);
237 
238     /* check container conformance */
239     if (image->container_current_separate_alpha == 0)
240         assert(SOURCE_CLR_FMT(image) == image->container_color);
241     assert(((image->header_flags_fmt & 0x0f) == 15 ? 0 : (image->header_flags_fmt & 0x0f)) == image->container_bpc);
242     if (image->container_separate_alpha == 0)
243         assert(image->container_alpha == ALPHACHANNEL_FLAG(image));
244     else
245         assert(ALPHACHANNEL_FLAG(image) == 0);
246 
247     DEBUG(" Flags group1=0x%02x\n", image->header_flags1);
248     DEBUG(" Flags group2=0x%02x\n", image->header_flags2);
249     DEBUG(" OUTPUT_CLR_FMT=%d\n", SOURCE_CLR_FMT(image));
250     DEBUG(" OUTPUT_BITDEPTH=%d (%s)\n", SOURCE_BITDEPTH(image), bitdepth_names[SOURCE_BITDEPTH(image)]);
251 
252     /* Get the configured image dimensions. */
253     if (SHORT_HEADER_FLAG(image)) {
254         DEBUG(" SHORT_HEADER_FLAG=true\n");
255         image->width1 = _jxr_rbitstream_uint16(str);
256         image->height1 = _jxr_rbitstream_uint16(str);
257     } else {
258         DEBUG(" SHORT_HEADER_FLAG=false\n");
259         image->width1 = _jxr_rbitstream_uint32(str);
260         image->height1 = _jxr_rbitstream_uint32(str);
261     }
262 
263     /* check container conformance */
264     assert(image->width1 + 1 == image->container_width);
265     assert(image->height1 + 1 == image->container_height);
266 
267     DEBUG(" Image dimensions: %u x %u\n", image->width1+1, image->height1+1);
268 
269     assert(image->tile_row_height == 0);
270     assert(image->tile_column_width == 0);
271     assert(image->tile_column_position == 0);
272     if (jxr_get_TILING_FLAG(image)) {
273         image->tile_columns = _jxr_rbitstream_uint12(str) + 1;
274         image->tile_rows = _jxr_rbitstream_uint12(str) + 1;
275         DEBUG(" TILING %u columns, %u rows (bitpos=%zu)\n",
276             image->tile_columns, image->tile_rows,
277             _jxr_rbitstream_bitpos(str));
278 
279 
280     } else {
281         /* NO TILING means that the entire image is exactly 1
282         tile. Configure the single tile to be the size of the
283         entire image. */
284         image->tile_columns = 1;
285         image->tile_rows = 1;
286         DEBUG(" NO TILING\n");
287     }
288 
289     /* Collect the widths of the tile columns. All but the last
290     column width are encoded in the input stream. The last is
291     inferred from the accumulated width of the columns and the
292     total width of the image. If there is no tiling, then there
293     is exactly 1 tile, and this degenerates to the width of the
294     image.
295 
296     The heights of tile rows is processed exactly the same way. */
297 
298     image->tile_column_width = (unsigned*)calloc(2*image->tile_columns, sizeof(unsigned));
299     image->tile_column_position = image->tile_column_width + image->tile_columns;
300     image->tile_row_height = (unsigned*)calloc(2*image->tile_rows, sizeof(unsigned));
301     image->tile_row_position = image->tile_row_height + image->tile_rows;
302 
303     unsigned wid_sum = 0;
304     if (SHORT_HEADER_FLAG(image)) {
305         for (idx = 0 ; idx < image->tile_columns-1 ; idx += 1) {
306             image->tile_column_width[idx] = _jxr_rbitstream_uint8(str);
307             image->tile_column_position[idx] = wid_sum;
308             wid_sum += image->tile_column_width[idx];
309         }
310 
311     } else {
312         for (idx = 0 ; idx < image->tile_columns-1 ; idx += 1) {
313             image->tile_column_width[idx] = _jxr_rbitstream_uint16(str);
314             image->tile_column_position[idx] = wid_sum;
315             wid_sum += image->tile_column_width[idx];
316         }
317     }
318     /* calculate final tile width after windowing parameters are found */
319 
320     unsigned hei_sum = 0;
321     if (SHORT_HEADER_FLAG(image)) {
322         for (idx = 0 ; idx < image->tile_rows-1 ; idx += 1) {
323             image->tile_row_height[idx] = _jxr_rbitstream_uint8(str);
324             image->tile_row_position[idx] = hei_sum;
325             hei_sum += image->tile_row_height[idx];
326         }
327 
328     } else {
329         for (idx = 0 ; idx < image->tile_rows-1 ; idx += 1) {
330             image->tile_row_height[idx] = _jxr_rbitstream_uint16(str);
331             image->tile_row_position[idx] = hei_sum;
332             hei_sum += image->tile_row_height[idx];
333         }
334     }
335     /* calculate final tile height after windowing parameters are found */
336 
337     if (WINDOWING_FLAG(image)) {
338         image->window_extra_top = _jxr_rbitstream_uint6(str);
339         image->window_extra_left = _jxr_rbitstream_uint6(str);
340         image->window_extra_bottom = _jxr_rbitstream_uint6(str);
341         image->window_extra_right = _jxr_rbitstream_uint6(str);
342     } else {
343         image->window_extra_top = 0;
344         image->window_extra_left = 0;
345         if ((image->height1 + 1) % 16 == 0)
346             image->window_extra_bottom = 0;
347         else
348             image->window_extra_bottom = 16 - ((image->height1 + 1) % 16);
349         if ((image->width1 + 1) % 16 == 0)
350             image->window_extra_right = 0;
351         else
352             image->window_extra_right = 16 - ((image->width1 + 1) % 16);
353         DEBUG(" NO WINDOWING\n");
354     }
355     image->extended_width = image->width1 + 1 + image->window_extra_left + image->window_extra_right;
356     image->extended_height = image->height1 + 1 + image->window_extra_top + image->window_extra_bottom;
357 
358     image->lwf_test = 0;
359 
360     image->tile_column_width[image->tile_columns-1] = (image->extended_width >> 4)-wid_sum;
361     image->tile_column_position[image->tile_columns-1] = wid_sum;
362 
363     image->tile_row_height[image->tile_rows-1] = (image->extended_height >> 4)-hei_sum;
364     image->tile_row_position[image->tile_rows-1] = hei_sum;
365 
366 #if defined(DETAILED_DEBUG)
367     DEBUG(" Tile widths:");
368     for (idx = 0 ; idx < image->tile_columns ; idx += 1)
369         DEBUG(" %u", image->tile_column_width[idx]);
370     DEBUG("\n");
371     DEBUG(" Tile heights:");
372     for (idx = 0 ; idx < image->tile_rows ; idx += 1)
373         DEBUG(" %u", image->tile_row_height[idx]);
374     DEBUG("\n");
375 #endif
376 
377     /* Perform some checks */
378     assert(image->extended_width % 16 == 0);
379     if ((OVERLAP_INFO(image) >= 2) && (image->use_clr_fmt == 1 || image->use_clr_fmt == 2))
380     {
381         assert(image->extended_width >= 32);
382         if (image->disableTileOverlapFlag) {
383             unsigned int idx = 0;
384             for (idx = 0; idx < image->tile_columns ; idx += 1)
385                 assert(image->tile_column_width[idx] > 1);
386         }
387     }
388     assert(image->extended_height % 16 == 0);
389 
390     DEBUG("END IMAGE_HEADER (%zu bytes)\n", str->read_count);
391     return 0;
392 }
393 
r_image_plane_header(jxr_image_t image,struct rbitstream * str,int alpha)394 static int r_image_plane_header(jxr_image_t image, struct rbitstream*str, int alpha)
395 {
396     size_t save_count = str->read_count;
397     DEBUG("START IMAGE_PLANE_HEADER (bitpos=%zu)\n", _jxr_rbitstream_bitpos(str));
398 
399     /* NOTE: The "use_clr_fmt" is the encoded color format, and is
400     not necessarily the same as the image color format
401     signaled in the image header. All of our processing of an
402     image plane is handled using the "use_clr_fmt", and only
403     transformed to the image color format on the way out. */
404 
405     image->use_clr_fmt = _jxr_rbitstream_uint3(str); /* INTERNAL_CLR_FMT */
406     image->scaled_flag = _jxr_rbitstream_uint1(str); /* NO_SCALED_FLAG */
407     image->bands_present = _jxr_rbitstream_uint4(str); /* BANDS_PRESENT */
408 
409     /* for alpha image plane, INTERNAL_CLR_FMT == YONLY */
410     if (alpha)
411         assert(image->use_clr_fmt == 0);
412 
413     DEBUG(" INTERNAL_CLR_FMT = %d\n", image->use_clr_fmt);
414     DEBUG(" SCALED_FLAG = %s\n", image->scaled_flag? "true" : "false");
415     DEBUG(" BANDS_PRESENT = %d\n", image->bands_present);
416 
417     uint16_t num_components;
418     switch (image->use_clr_fmt) {
419         case 0: /* YONLY */
420             image->num_channels = 1;
421             break;
422         case 1: /* YUV420 */
423             _jxr_rbitstream_uint1(str); /* RESERVED_E_BIT */
424             image->chroma_centering_x = _jxr_rbitstream_uint3(str); /* CHROMA_CENTERING_X */
425             _jxr_rbitstream_uint1(str); /* RESERVED_G_BIT */
426             image->chroma_centering_y = _jxr_rbitstream_uint3(str); /* CHROMA_CENTERING_Y */
427             image->num_channels = 3;
428             break;
429         case 2: /* YUV422 */
430             _jxr_rbitstream_uint1(str); /* RESERVED_E_BIT */
431             image->chroma_centering_x = _jxr_rbitstream_uint3(str); /* CHROMA_CENTERING_X */
432             _jxr_rbitstream_uint4(str); /* RESERVED_H */
433             image->chroma_centering_y = 0;
434             image->num_channels = 3;
435             break;
436         case 3: /* YUV444 */
437             _jxr_rbitstream_uint4(str); /* RESERVED_F */
438             _jxr_rbitstream_uint4(str); /* RESERVED_H */
439             image->num_channels = 3;
440             break;
441         case 4: /* YUVK */
442             image->num_channels = 4;
443             break;
444         case 6: /* NCOMPONENT */
445             num_components = _jxr_rbitstream_uint4(str);
446             if (num_components == 0xf) {
447                 image->num_channels = 16 + _jxr_rbitstream_uint12(str);
448             }
449             else {
450                 image->num_channels = 1 + num_components;
451                 _jxr_rbitstream_uint4(str); /* RESERVED_H */
452             }
453             break;
454         case 5: /* RESERVED */
455         case 7: /* RESERVED */
456             break;
457     }
458 
459     /*
460     check container conformance - specific for tag based container
461     this section should be modified when the container is
462     */
463     if (image->container_alpha) {
464         if (image->container_separate_alpha) {
465             if (image->container_current_separate_alpha) {
466                 assert(image->num_channels == 1);
467                 assert(image->bands_present == image->container_alpha_band_presence  || image->container_alpha_band_presence > 3 || image->container_alpha_band_presence < 0);
468             }
469             else {
470                 assert(image->num_channels == image->container_nc - 1);
471                 assert((image->bands_present == image->container_image_band_presence) || (image->container_image_band_presence > 3) || (image->container_image_band_presence < 0));
472             }
473         }
474         else {
475             if (alpha) {
476                 assert(image->num_channels == 1);
477                 assert(image->bands_present == image->container_alpha_band_presence || image->container_alpha_band_presence > 3 || image->container_alpha_band_presence < 0);
478             }
479             else {
480                 assert(image->num_channels == image->container_nc - 1);
481                 assert(image->bands_present == image->container_image_band_presence || image->container_image_band_presence > 3 || (image->container_image_band_presence < 0));
482             }
483         }
484     }
485     else {
486         assert(image->num_channels == image->container_nc);
487         assert(image->bands_present == image->container_image_band_presence || image->container_image_band_presence > 3 || (image->container_image_band_presence < 0));
488     }
489 
490 
491     switch (SOURCE_BITDEPTH(image)) {
492         case 0: /* BD1WHITE1 */
493         case 1: /* BD8 */
494         case 4: /* BD16F */
495         case 8: /* BD5 */
496         case 9: /* BD10 */
497         case 15: /* BD1BLACK1 */
498             image->shift_bits = 0;
499             break;
500         case 2: /* BD16 */
501         case 3: /* BD16S */
502         case 6: /* BD32S */
503             image->shift_bits = _jxr_rbitstream_uint8(str); /* SHIFT_BITS */
504             DEBUG(" SHIFT_BITS = %u\n", image->shift_bits);
505             break;
506         case 7: /* BD32F */
507             image->len_mantissa = _jxr_rbitstream_uint8(str); /* LEN_MANTISSA */
508             image->exp_bias = _jxr_rbitstream_uint8(str); /* EXP_BIAS */
509             DEBUG(" LEN_MANTISSA = %u\n", image->len_mantissa);
510             DEBUG(" EXP_BIAS = %u\n", image->exp_bias);
511             break;
512         default: /* RESERVED */
513             DEBUG(" XXXX Inexplicable SOURCE_BITDEPTH=%u\n", SOURCE_BITDEPTH(image));
514             break;
515     }
516 
517     /* If the stream signals that the DC frames use a uniform
518     quantization parameter, then collect that parameter
519     here. In this case, DC quantization parameters elsewhere in
520     the image are suppressed. Note that per macroblock, there
521     is only 1 DC value, so only 1 DC QP is needed. */
522     image->dc_frame_uniform = _jxr_rbitstream_uint1(str);
523     DEBUG(" DC_FRAME_UNIFORM = %s\n", image->dc_frame_uniform?"true":"false");
524     if (image->dc_frame_uniform) {
525         _jxr_r_DC_QP(image, str);
526     }
527 
528     if (image->bands_present != 3 /*DCONLY*/) {
529         _jxr_rbitstream_uint1(str); /* RESERVED_I_BIT */
530 
531         image->lp_frame_uniform = _jxr_rbitstream_uint1(str);
532         DEBUG(" LP_FRAME_UNIFORM = %s\n", image->lp_frame_uniform?"true":"false");
533         if (image->lp_frame_uniform) {
534             image->num_lp_qps = 1;
535             _jxr_r_LP_QP(image, str);
536         }
537 
538         if (image->bands_present != 2 /*NOHIGHPASS*/) {
539             _jxr_rbitstream_uint1(str); /* RESERVED_J_BIT */
540 
541             image->hp_frame_uniform = _jxr_rbitstream_uint1(str);
542             DEBUG(" HP_FRAME_UNIFORM = %s\n", image->hp_frame_uniform?"true":"false");
543             if (image->hp_frame_uniform) {
544                 image->num_hp_qps = 1;
545                 r_HP_QP(image, str);
546             }
547         }
548 
549     }
550 
551     _jxr_rbitstream_syncbyte(str);
552     DEBUG("END IMAGE_PLANE_HEADER (%zd bytes, bitpos=%zu)\n",
553         str->read_count - save_count, _jxr_rbitstream_bitpos(str));
554 
555     return 0;
556 }
557 
get_ch_mode(jxr_image_t image,struct rbitstream * str)558 static int get_ch_mode(jxr_image_t image, struct rbitstream*str)
559 {
560     int ch_mode;
561     if (image->num_channels == 1) {
562         ch_mode = 0; /* UNIFORM */
563     } else {
564         ch_mode = _jxr_rbitstream_uint2(str);
565     }
566     return ch_mode;
567 }
568 
_jxr_r_DC_QP(jxr_image_t image,struct rbitstream * str)569 int _jxr_r_DC_QP(jxr_image_t image, struct rbitstream*str)
570 {
571     unsigned idx;
572 
573     int ch_mode = get_ch_mode(image, str);
574     DEBUG(" DC_QP CH_MODE=%d ", ch_mode);
575 
576     switch (ch_mode) {
577         case 0: /* UNIFORM */
578             image->dc_quant_ch[0] = _jxr_rbitstream_uint8(str);
579             DEBUG(" DC_QUANT UNIFORM =%u", image->dc_quant_ch[0]);
580             for (idx = 1 ; idx < image->num_channels ; idx += 1)
581                 image->dc_quant_ch[idx] = image->dc_quant_ch[0];
582             break;
583         case 1: /* SEPARATE */
584             image->dc_quant_ch[0] = _jxr_rbitstream_uint8(str);
585             image->dc_quant_ch[1] = _jxr_rbitstream_uint8(str);
586             image->dc_quant_ch[2] = image->dc_quant_ch[1];
587             DEBUG(" DC_QUANT SEPARATE Y=%u, Chr=%u", image->dc_quant_ch[0],image->dc_quant_ch[1]);
588             break;
589         case 2: /* INDEPENDENT */
590             assert(image->num_channels <= MAX_CHANNELS);
591             for (idx = 0 ; idx < image->num_channels ; idx += 1) {
592                 image->dc_quant_ch[idx] = _jxr_rbitstream_uint8(str);
593                 DEBUG(" DC_QUANT INDEPENDENT[%d] = %u", idx, image->dc_quant_ch[idx]);
594             }
595             break;
596         case 3: /* Reserved */
597             break;
598         default:
599             assert(0);
600             break;
601     }
602     DEBUG("\n");
603 
604     return 0;
605 }
606 
_jxr_r_LP_QP(jxr_image_t image,struct rbitstream * str)607 int _jxr_r_LP_QP(jxr_image_t image, struct rbitstream*str)
608 {
609     unsigned q;
610 
611     for (q = 0 ; q < image->num_lp_qps ; q += 1) {
612         unsigned idx;
613         int ch_mode = get_ch_mode(image, str);
614         DEBUG(" LP_QP[%u] CH_MODE=%d LP_QUANT=", q, ch_mode);
615 
616         switch (ch_mode) {
617             case 0: /* UNIFORM */
618                 image->lp_quant_ch[0][q] = _jxr_rbitstream_uint8(str);
619                 DEBUG("%d", image->lp_quant_ch[0][q]);
620                 for (idx = 1 ; idx < image->num_channels ; idx += 1)
621                     image->lp_quant_ch[idx][q] = image->lp_quant_ch[0][q];
622                 break;
623             case 1: /* SEPARATE */
624                 image->lp_quant_ch[0][q] = _jxr_rbitstream_uint8(str);
625                 image->lp_quant_ch[1][q] = _jxr_rbitstream_uint8(str);
626                 DEBUG("SEPARATE Y=%d Chr=%d", image->lp_quant_ch[0][q], image->lp_quant_ch[1][q]);
627                 for (idx = 2 ; idx < image->num_channels ; idx += 1)
628                     image->lp_quant_ch[idx][q] = image->lp_quant_ch[1][q];
629                 break;
630             case 2: /* INDEPENDENT */
631                 DEBUG("INDEPENDENT =");
632                 for (idx = 0 ; idx < image->num_channels ; idx += 1) {
633                     image->lp_quant_ch[idx][q] = _jxr_rbitstream_uint8(str);
634                     DEBUG(" %d", image->lp_quant_ch[idx][q]);
635                 }
636                 break;
637             case 3: /* Reserved */
638                 break;
639             default:
640                 assert(0);
641                 break;
642         }
643         DEBUG("\n");
644     }
645 
646     return 0;
647 }
648 
r_HP_QP(jxr_image_t image,struct rbitstream * str)649 static int r_HP_QP(jxr_image_t image, struct rbitstream*str)
650 {
651     unsigned q;
652 
653     for (q = 0 ; q < image->num_hp_qps ; q += 1) {
654         unsigned idx;
655         int ch_mode = get_ch_mode(image, str);
656         DEBUG("HP_QP[%u] CH_MODE: %d ", q, ch_mode);
657 
658         switch (ch_mode) {
659             case 0: /* UNIFORM */
660                 image->HP_QUANT_Y[q] = _jxr_rbitstream_uint8(str);
661                 DEBUG("UNIFORM %d", image->hp_quant_ch[0][q]);
662                 for (idx = 1 ; idx < image->num_channels ; idx += 1)
663                     image->hp_quant_ch[idx][q] = image->hp_quant_ch[0][q];
664                 break;
665             case 1: /* SEPARATE */
666                 image->HP_QUANT_Y[q] = _jxr_rbitstream_uint8(str);
667                 image->hp_quant_ch[1][q] = _jxr_rbitstream_uint8(str);
668                 DEBUG("SEPARATE Y=%d Chr=%d", image->hp_quant_ch[0][q], image->hp_quant_ch[1][q]);
669                 for (idx = 2 ; idx < image->num_channels ; idx += 1)
670                     image->hp_quant_ch[idx][q] = image->hp_quant_ch[1][q];
671                 break;
672             case 2: /* INDEPENDENT */
673                 DEBUG("INDEPENDENT =");
674                 for (idx = 0 ; idx < image->num_channels ; idx += 1) {
675                     image->hp_quant_ch[idx][q] = _jxr_rbitstream_uint8(str);
676                     DEBUG(" %d", image->hp_quant_ch[idx][q]);
677                 }
678                 break;
679             case 3: /* Reserved */
680                 break;
681             default:
682                 assert(0);
683                 break;
684         }
685         DEBUG(" bitpos=%zu\n", _jxr_rbitstream_bitpos(str));
686     }
687 
688     return 0;
689 }
690 
r_INDEX_TABLE(jxr_image_t image,struct rbitstream * str)691 static int r_INDEX_TABLE(jxr_image_t image, struct rbitstream*str)
692 {
693     DEBUG("INDEX_TABLE START bitpos=%zu\n", _jxr_rbitstream_bitpos(str));
694     if (INDEXTABLE_PRESENT_FLAG(image)) {
695         uint8_t s0 = _jxr_rbitstream_uint8(str);
696         uint8_t s1 = _jxr_rbitstream_uint8(str);
697         DEBUG(" STARTCODE = 0x%02x 0x%02x\n", s0, s1);
698         if (s0 != 0x00 || s1 != 0x01)
699             return JXR_EC_ERROR;
700 
701         int num_index_table_entries;
702         int idx;
703 
704         if (FREQUENCY_MODE_CODESTREAM_FLAG(image) == 0 /* SPATIALMODE */) {
705             num_index_table_entries = image->tile_rows * image->tile_columns;
706 
707         } else {
708             num_index_table_entries = image->tile_rows * image->tile_columns;
709             switch (image->bands_present) {
710                 case 4: /* ISOLATED */
711                     num_index_table_entries *= 4;
712                     break;
713                 default:
714                     num_index_table_entries *= 4 - image->bands_present;
715                     break;
716             }
717         }
718         image->tile_index_table_length = num_index_table_entries;
719 
720         assert(image->tile_index_table == 0);
721         image->tile_index_table = (int64_t*)calloc(num_index_table_entries, sizeof(int64_t));
722         DEBUG(" INDEX_TABLE has %d table entries\n", num_index_table_entries);
723 
724         for (idx = 0 ; idx < num_index_table_entries ; idx += 1) {
725             int64_t off = _jxr_rbitstream_intVLW(str);
726             DEBUG(" ... %ld\n", off);
727             image->tile_index_table[idx] = off;
728         }
729     }
730 
731     DEBUG("INTEX_TABLE DONE bitpos=%zu\n", _jxr_rbitstream_bitpos(str));
732     return 0;
733 }
734 
r_PROFILE_LEVEL_INFO(jxr_image_t image,struct rbitstream * str)735 static int64_t r_PROFILE_LEVEL_INFO(jxr_image_t image, struct rbitstream*str)
736 {
737     int64_t num_bytes = 0;
738     uint16_t reserved_l;
739     unsigned last_flag;
740 
741     int64_t last;
742     for (last = 0 ; last == 0 ; last = last_flag) {
743         image->profile_idc = _jxr_rbitstream_uint8(str); /* PROFILE_IDC */
744         DEBUG(" Profile signaled in file %ld bytes\n", image->profile_idc);
745         image->level_idc = _jxr_rbitstream_uint8(str); /* LEVEL_IDC */
746         DEBUG(" Level signaled in file %ld bytes\n", image->level_idc);
747         reserved_l = _jxr_rbitstream_uint15(str); /* RESERVED_L */
748         last_flag = _jxr_rbitstream_uint1(str); /* LAST_FLAG */
749         num_bytes += 4;
750     }
751 
752     return num_bytes;
753 }
754 
r_TILE(jxr_image_t image,struct rbitstream * str)755 static int r_TILE(jxr_image_t image, struct rbitstream*str)
756 {
757 int rc = 0;
758     image->tile_quant = (struct jxr_tile_qp *) calloc(image->tile_columns*image->tile_rows, sizeof(*(image->tile_quant)));
759     assert(image->tile_quant);
760 
761     if (FREQUENCY_MODE_CODESTREAM_FLAG(image) == 0 /* SPATIALMODE */) {
762 
763         unsigned tx, ty, tt=0;
764         for (ty = 0 ; ty < image->tile_rows ; ty += 1) {
765             for (tx = 0 ; tx < image->tile_columns ; tx += 1) {
766                 if(INDEXTABLE_PRESENT_FLAG(image))
767                 {
768                     _jxr_rbitstream_seek(str, image->tile_index_table[tt]);
769                     tt++;
770                 }
771                 rc = _jxr_r_TILE_SPATIAL(image, str, tx, ty);
772                 if (rc < 0) goto RET;
773             }
774         }
775     } else { /* FREQUENCYMODE */
776 
777         int num_bands = 0;
778         switch (image->bands_present) {
779             case 0: /* ALL */
780                 num_bands = 4;
781                 break;
782             case 1: /* NOFLEXBITS */
783                 num_bands = 3;
784                 break;
785             case 2: /* NOHIGHPASS */
786                 num_bands = 2;
787                 break;
788             case 3: /* DCONLY */
789                 num_bands = 1;
790                 break;
791             case 4: /* ISOLATED */
792                 break;
793         }
794 
795         unsigned tx, ty, tt;
796         for (ty = 0, tt=0 ; ty < image->tile_rows ; ty += 1) {
797             for (tx = 0 ; tx < image->tile_columns ; tx += 1) {
798                 _jxr_rbitstream_seek(str, image->tile_index_table[tt*num_bands+0]);
799                 rc = _jxr_r_TILE_DC(image, str, tx, ty);
800                 if (rc < 0) goto RET;
801                 tt += 1;
802             }
803         }
804 
805         if (num_bands > 1) {
806             for (ty = 0, tt=0 ; ty < image->tile_rows ; ty += 1) {
807                 for (tx = 0 ; tx < image->tile_columns ; tx += 1) {
808                     _jxr_rbitstream_seek(str, image->tile_index_table[tt*num_bands+1]);
809                     rc = _jxr_r_TILE_LP(image, str, tx, ty);
810                     if (rc < 0) goto RET;
811                     tt += 1;
812                 }
813             }
814         }
815 
816         if (num_bands > 2) {
817             for (ty = 0, tt=0 ; ty < image->tile_rows ; ty += 1) {
818                 for (tx = 0 ; tx < image->tile_columns ; tx += 1) {
819                     _jxr_rbitstream_seek(str, image->tile_index_table[tt*num_bands+2]);
820                     rc = _jxr_r_TILE_HP(image, str, tx, ty);
821                     if (rc < 0) goto RET;
822                     tt += 1;
823                 }
824             }
825         }
826 
827         if (num_bands > 3) {
828             for (ty = 0, tt=0 ; ty < image->tile_rows ; ty += 1) {
829                 for (tx = 0 ; tx < image->tile_columns ; tx += 1) {
830                     int64_t off = image->tile_index_table[tt*num_bands+3];
831                     if (off >= 0) {
832                         _jxr_rbitstream_seek(str, off);
833                         rc = _jxr_r_TILE_FLEXBITS(image, str, tx, ty);
834                         if (rc < 0) goto RET;
835                     } else {
836                         _jxr_r_TILE_FLEXBITS_ESCAPE(image, tx, ty);
837                     }
838                     tt += 1;
839                 }
840             }
841         }
842 
843         _jxr_frequency_mode_render(image);
844     }
845 
846 RET:
847     free(image->tile_quant);
848     return rc;
849 }
850 
_jxr_r_TILE_HEADER_DC(jxr_image_t image,struct rbitstream * str,int alpha_flag,unsigned tx,unsigned ty)851 void _jxr_r_TILE_HEADER_DC(jxr_image_t image, struct rbitstream*str,
852                            int alpha_flag, unsigned tx, unsigned ty)
853 {
854     DEBUG(" TILE_HEADER_DC START bitpos=%zu\n", _jxr_rbitstream_bitpos(str));
855     if (image->dc_frame_uniform == 0) {
856         DEBUG(" TILE_HEADER_DC: parse non-uniform DC_QP\n");
857         _jxr_r_DC_QP(image, str);
858         memcpy(image->tile_quant[ty*(image->tile_columns) + tx ].dc_quant_ch, image->dc_quant_ch, MAX_CHANNELS);
859     }
860 }
861 
_jxr_r_TILE_HEADER_LOWPASS(jxr_image_t image,struct rbitstream * str,int alpha_flag,unsigned tx,unsigned ty)862 void _jxr_r_TILE_HEADER_LOWPASS(jxr_image_t image, struct rbitstream*str,
863                                 int alpha_flag,
864                                 unsigned tx, unsigned ty)
865 {
866     DEBUG(" TILE_HEADER_LOWPASS START bitpos=%zu\n", _jxr_rbitstream_bitpos(str));
867     if (image->lp_frame_uniform == 0) {
868         image->lp_use_dc_qp = _jxr_rbitstream_uint1(str);
869         DEBUG(" TILE_HEADER_LP: parse non-uniform LP_QP: USE_DC_QP=%u\n",
870             image->lp_use_dc_qp);
871         if (image->lp_use_dc_qp == 0) {
872             image->num_lp_qps = _jxr_rbitstream_uint4(str) + 1;
873             DEBUG(" TILE_HEADER_LP: NUM_LP_QPS = %d\n", image->num_lp_qps);
874             _jxr_r_LP_QP(image, str);
875             memcpy(image->tile_quant[ty*(image->tile_columns) + tx].lp_quant_ch, image->lp_quant_ch, MAX_CHANNELS*MAX_LP_QPS);
876         }
877         else
878         {
879             /* Use the same quantization index as the dc band (the dc quantization step size could be different for each tile, so store it */
880             int ch;
881             for(ch = 0; ch < image->num_channels; ch++)
882                 image->tile_quant[ty*(image->tile_columns) + tx].lp_quant_ch[ch][0] = image->dc_quant_ch[ch];
883         }
884     }
885 }
886 
887 
_jxr_r_TILE_HEADER_HIGHPASS(jxr_image_t image,struct rbitstream * str,int alpha_flag,unsigned tx,unsigned ty)888 void _jxr_r_TILE_HEADER_HIGHPASS(jxr_image_t image, struct rbitstream*str,
889                                  int alpha_flag,
890                                  unsigned tx, unsigned ty)
891 {
892     if (image->hp_frame_uniform == 0) {
893         image->hp_use_lp_qp = _jxr_rbitstream_uint1(str);
894         DEBUG(" TILE_HEADER_HP: parse non-uniform HP_QP: USE_LP_QP=%u\n",
895             image->hp_use_lp_qp);
896 
897         if (image->hp_use_lp_qp == 0) {
898             image->num_hp_qps = _jxr_rbitstream_uint4(str) + 1;
899             DEBUG(" TILE_HEADER_HIGHPASS: NUM_HP_QPS = %d\n", image->num_hp_qps);
900             r_HP_QP(image, str);
901             memcpy(image->tile_quant[ty*(image->tile_columns) + tx].hp_quant_ch, image->lp_quant_ch, MAX_CHANNELS*MAX_HP_QPS);
902         }
903         else
904         {
905             /* Use the same quantization index as the lp band (the lp quantization step size could be different for each tile, so store it */
906             int ch;
907             image->num_hp_qps = image->num_lp_qps;
908             for(ch = 0; ch < image->num_channels; ch++) {
909                 memcpy(image->hp_quant_ch[ch], image->lp_quant_ch[ch], MAX_LP_QPS);
910                 memcpy(image->tile_quant[ty*(image->tile_columns) + tx].hp_quant_ch[ch], image->lp_quant_ch[ch], MAX_LP_QPS);
911             }
912         }
913     }
914 }
915 
_jxr_DECODE_QP_INDEX(struct rbitstream * str,unsigned index_count)916 unsigned _jxr_DECODE_QP_INDEX(struct rbitstream*str, unsigned index_count)
917 {
918     static const int bits_per_qp_index[] = {0,0,1,1,2,2,3,3, 3,3,4,4,4,4,4,4,4};
919     assert(index_count <= 16);
920 
921     int nonzero_flag = _jxr_rbitstream_uint1(str);
922 
923     if (nonzero_flag == 0)
924         return 0;
925 
926     int bits_count = bits_per_qp_index[index_count];
927     /* DECODE_QP_INDEX is onny called if the index count is
928     greater then 1. Therefore, the bits_count here must be more
929     then zero. */
930     assert(bits_count > 0);
931 
932     return _jxr_rbitstream_uintN(str, bits_count)+1;
933 }
934 
935 /*
936 * Decode the single DC component for the macroblock.
937 */
_jxr_r_MB_DC(jxr_image_t image,struct rbitstream * str,int alpha_flag,unsigned tx,unsigned ty,unsigned mx,unsigned my)938 void _jxr_r_MB_DC(jxr_image_t image, struct rbitstream*str,
939                   int alpha_flag,
940                   unsigned tx, unsigned ty,
941                   unsigned mx, unsigned my)
942 {
943     int lap_mean[2];
944     lap_mean[0] = 0;
945     lap_mean[1] = 0;
946 
947     DEBUG(" MB_DC tile=[%u %u] mb=[%u %u] bitpos=%zu\n",
948         tx, ty, mx, my, _jxr_rbitstream_bitpos(str));
949 
950     if (_jxr_InitContext(image, tx, ty, mx, my)) {
951         DEBUG(" MB_DC: Initialize Context\n");
952         _jxr_InitVLCTable(image, AbsLevelIndDCLum);
953         _jxr_InitVLCTable(image, AbsLevelIndDCChr);
954         _jxr_InitializeModelMB(&image->model_dc, 0/*DC*/);
955     }
956 
957     if (image->use_clr_fmt==0 || image->use_clr_fmt==4 || image->use_clr_fmt==6) {
958         /* clr_fmt == YONLY, YUVK or NCOMPONENT */
959         unsigned idx;
960         for (idx = 0 ; idx < image->num_channels ; idx += 1) {
961             int m = (idx == 0)? 0 : 1;
962             int model_bits = image->model_dc.bits[m];
963             unsigned is_dc_ch = _jxr_rbitstream_uint1(str);
964             DEBUG(" MB_DC: IS_DC_CH=%u, model_bits=%d\n",
965                 is_dc_ch, model_bits);
966             if (is_dc_ch) {
967                 lap_mean[m] += 1;
968             }
969             uint32_t dc_val = r_DEC_DC(image, str, tx, ty, mx, my,
970                 model_bits, 0/*chroma_flag==FALSE*/,
971                 is_dc_ch);
972 
973             MACROBLK_CUR_DC(image,idx,tx, mx) = dc_val;
974             DEBUG(" dc_val at t=[%u %u], m=[%u %u] == %d (0x%08x)\n",
975                 tx, ty, mx, my, (int32_t)dc_val, dc_val);
976         }
977     } else {
978         assert(image->num_channels == 3);
979         int is_dc_yuv = get_is_dc_yuv(str);
980         int model_bits_y = image->model_dc.bits[0];
981         int model_bits_uv = image->model_dc.bits[1];
982         DEBUG(" MB_DC: IS_DC_YUV=0x%x, model_bits[0]=%d, model_bits[1]=%d\n",
983             is_dc_yuv, model_bits_y, model_bits_uv);
984 
985         if (is_dc_yuv&4)
986             lap_mean[0] += 1;
987         uint32_t dc_val_y = r_DEC_DC(image, str, tx, ty, mx, my,
988             model_bits_y, 0/*chroma_flag==FALSE*/,
989             is_dc_yuv&4);
990 
991         if (is_dc_yuv&2)
992             lap_mean[1] += 1;
993         uint32_t dc_val_u = r_DEC_DC(image, str, tx, ty, mx, my,
994             model_bits_uv, 1/*chroma_flag==TRUE*/,
995             is_dc_yuv&2);
996 
997         if (is_dc_yuv&1)
998             lap_mean[1] += 1;
999         uint32_t dc_val_v = r_DEC_DC(image, str, tx, ty, mx, my,
1000             model_bits_uv, 1/*chroma_flag==TRUE*/,
1001             is_dc_yuv&1);
1002 
1003         MACROBLK_CUR_DC(image,0,tx, mx) = dc_val_y;
1004         MACROBLK_CUR_DC(image,1,tx, mx) = dc_val_u;
1005         MACROBLK_CUR_DC(image,2,tx, mx) = dc_val_v;
1006         DEBUG(" dc_val at t=[%u %u], m=[%u %u] == %d (0x%08x), %d (0x%08x), %d (0x%08x)\n",
1007             tx, ty, mx, my, (int)dc_val_y, dc_val_y, (int)dc_val_u, dc_val_u, (int)dc_val_v, dc_val_v);
1008     }
1009 
1010     /* */
1011     DEBUG(" MB_DC: UpdateModelMB: lap_mean={%u %u}\n", lap_mean[0], lap_mean[1]);
1012     _jxr_UpdateModelMB(image, lap_mean, &image->model_dc, 0/*DC*/);
1013     if (_jxr_ResetContext(image, tx, mx)) {
1014         DEBUG(" MB_DC: Reset Context\n");
1015         /* AdaptDC */
1016         _jxr_AdaptVLCTable(image, AbsLevelIndDCLum);
1017         _jxr_AdaptVLCTable(image, AbsLevelIndDCChr);
1018     }
1019     DEBUG(" MB_DC DONE tile=[%u %u] mb=[%u %u]\n", tx, ty, mx, my);
1020 }
1021 
1022 /*
1023 * When the LP value is input from the stream, it is delivered into
1024 * the target array based on a scan order. The "lopass_scanorder"
1025 * array maps the list of LP values (actually the position in the
1026 * list) to the location in the scan. Thus the scan order places the
1027 * value into the lpinput array.
1028 *
1029 * A property of the lpinput is that it is sparse. The adpative scan
1030 * order tries to adapt the scan order so that the most frequent value
1031 * is pressed to the beginning of the input stream. It does this by
1032 * counting the arrival of each value, and bubbling frequent values
1033 * forward.
1034 *
1035 * Note in the code below that the "i" value ranges from 1-16 but the
1036 * tables are numbered from 0-15. Thus "i-1" is used to index tables.
1037 *
1038 * Note that the scanorder is adapted while we go, but the only
1039 * adjustment is to swap the current position with the previous. Thus,
1040 * it is not possible to effect the current pass with the adaptation.
1041 */
AdaptiveLPScan(jxr_image_t image,int lpinput_n[],int i,int value)1042 static void AdaptiveLPScan(jxr_image_t image, int lpinput_n[], int i, int value)
1043 {
1044     assert(i > 0);
1045     int k = image->lopass_scanorder[i-1];
1046     lpinput_n[k] = value;
1047     image->lopass_scantotals[i-1] += 1;
1048     if (i>1 && image->lopass_scantotals[i-1] > image->lopass_scantotals[i-2]) {
1049         SWAP(image->lopass_scantotals[i-1], image->lopass_scantotals[i-2]);
1050         SWAP(image->lopass_scanorder[i-1], image->lopass_scanorder[i-2]);
1051     }
1052 }
1053 
_jxr_r_MB_LP(jxr_image_t image,struct rbitstream * str,int alpha_flag,unsigned tx,unsigned ty,unsigned mx,unsigned my)1054 void _jxr_r_MB_LP(jxr_image_t image, struct rbitstream*str,
1055                   int alpha_flag,
1056                   unsigned tx, unsigned ty,
1057                   unsigned mx, unsigned my)
1058 {
1059     static const int transpose420[4] = {0, 2,
1060         1, 3 };
1061     static const int transpose422[8] = {0, 2, 1, 3, 4, 6, 5, 7};
1062     int LPInput[8][16];
1063     int idx;
1064 
1065     for (idx = 0 ; idx < 8 ; idx += 1) {
1066         int k;
1067         for (k = 0 ; k < 16 ; k += 1)
1068             LPInput[idx][k] = 0;
1069     }
1070 
1071     int lap_mean[2];
1072     lap_mean[0] = 0;
1073     lap_mean[1] = 0;
1074 
1075     DEBUG(" MB_LP tile=[%u %u] mb=[%u %u] bitpos=%zu\n",
1076         tx, ty, mx, my, _jxr_rbitstream_bitpos(str));
1077 
1078     if (_jxr_InitContext(image, tx, ty, mx, my)) {
1079         DEBUG(" Init contexts\n");
1080         _jxr_InitializeCountCBPLP(image);
1081         _jxr_InitLPVLC(image);
1082         _jxr_InitializeAdaptiveScanLP(image);
1083         _jxr_InitializeModelMB(&image->model_lp, 1/*LP*/);
1084     }
1085 
1086     if (_jxr_ResetTotals(image, mx)) {
1087         _jxr_ResetTotalsAdaptiveScanLP(image);
1088     }
1089 
1090     int full_planes = image->num_channels;
1091     if (image->use_clr_fmt==2 || image->use_clr_fmt==1)
1092         full_planes = 2;
1093 
1094     /* The CBPLP signals whether any non-zero coefficients are
1095     present in the LP band for this macroblock. It is a bitmask
1096     with a bit for each channel. So for example, YONLY, which
1097     has 1 channel, has a 1-bit cbplp. */
1098 
1099     int cbplp = 0;
1100     /* if CLR_FMT is YUV420, YUV422 or YUV444... */
1101     if (image->use_clr_fmt==1 || image->use_clr_fmt==2 || image->use_clr_fmt==3) {
1102         DEBUG(" MB_LP: Calculate YUV CBP using CountZeroCBPLP=%d, CountMaxCBPLP=%d bitpos=%zu\n",
1103             image->count_zero_CBPLP, image->count_max_CBPLP, _jxr_rbitstream_bitpos(str));
1104         int max = full_planes * 4 - 5;
1105         if (image->count_zero_CBPLP <= 0 || image->count_max_CBPLP < 0) {
1106             int cbp_yuv_lp1 = dec_cbp_yuv_lp1(image, str);
1107             if (image->count_max_CBPLP < image->count_zero_CBPLP)
1108                 cbplp = max - cbp_yuv_lp1;
1109             else
1110                 cbplp = cbp_yuv_lp1;
1111         } else {
1112             uint32_t cbp_yuv_lp2 = _jxr_rbitstream_uintN(str, full_planes);
1113             cbplp = cbp_yuv_lp2;
1114         }
1115         _jxr_UpdateCountCBPLP(image, cbplp, max);
1116 
1117     } else {
1118         int idx;
1119         cbplp = 0;
1120         for (idx = 0 ; idx < image->num_channels ; idx += 1) {
1121             int cbp_ch_lp = _jxr_rbitstream_uint1(str);
1122             cbplp |= cbp_ch_lp << idx;
1123         }
1124     }
1125 
1126     DEBUG(" MB_LP: cbplp = 0x%x (full_planes=%u)\n", cbplp, full_planes);
1127 
1128     int ndx;
1129     for (ndx = 0 ; ndx < full_planes ; ndx += 1) {
1130         int idx;
1131         const int chroma_flag = ndx>0? 1 : 0;
1132         int num_nonzero = 0;
1133 
1134         DEBUG(" MB_LP: process full_plane %u, CBPLP for plane=%d, bitpos=%zu\n",
1135             ndx, (cbplp>>ndx)&1, _jxr_rbitstream_bitpos(str));
1136         if ((cbplp>>ndx) & 1) {
1137             /* If the CBPLP bit is set for this plane, then we
1138             have parsing to do. Decode the (15) coeffs and
1139             arrange them for use in the MB. */
1140             int RLCoeffs[32] = {0};
1141             for (idx = 0 ; idx < 32 ; idx += 1)
1142                 RLCoeffs[idx] = 0;
1143 
1144             int location = 1;
1145             /* if CLR_FMT is YUV420 or YUV422 */
1146             if (image->use_clr_fmt==1/*YUV420*/ && chroma_flag)
1147                 location = 10;
1148             if (image->use_clr_fmt==2/*YUV422*/ && chroma_flag)
1149                 location = 2;
1150 
1151             num_nonzero = r_DECODE_BLOCK(image, str,
1152                 chroma_flag, RLCoeffs, 1/*LP*/, location);
1153             DEBUG(" : num_nonzero = %d\n", num_nonzero);
1154             assert(num_nonzero <= 16);
1155 
1156             if ((image->use_clr_fmt==1 || image->use_clr_fmt==2) && chroma_flag) {
1157                 static const int remap_arr[] = {4, 1, 2, 3, 5, 6, 7};
1158                 int temp[14];
1159                 int idx;
1160                 for (idx = 0 ; idx < 14 ; idx += 1)
1161                     temp[idx] = 0;
1162 
1163                 int remap_off = 0;
1164                 if (image->use_clr_fmt==1/*YUV420*/)
1165                     remap_off = 1;
1166 
1167                 int count_chr = 14;
1168                 if (image->use_clr_fmt==1/*YUV420*/)
1169                     count_chr = 6;
1170 
1171                 int k, i = 0;
1172                 for (k = 0; k < num_nonzero; k+=1) {
1173                     i += RLCoeffs[k*2+0];
1174                     temp[i] = RLCoeffs[k*2+1];
1175                     i += 1;
1176                 }
1177                 for (k = 0; k < count_chr; k+=1) {
1178                     int remap = remap_arr[(k>>1) + remap_off];
1179                     int plane = (k&1) + 1;
1180                     if (image->use_clr_fmt==1)
1181                         remap = transpose420[remap];
1182                     if (image->use_clr_fmt==2)
1183                         remap = transpose422[remap];
1184                     LPInput[plane][remap] = temp[k];
1185                 }
1186 #if defined(DEBUG)
1187                 {
1188                     int k;
1189                     DEBUG(" RLCoeffs[ndx=%d] ==", ndx);
1190                     for (k = 0 ; k<(num_nonzero*2); k+=2) {
1191                         DEBUG(" %d/0x%x", RLCoeffs[k+0], RLCoeffs[k+1]);
1192                     }
1193                     DEBUG("\n");
1194                     DEBUG(" temp ==");
1195                     for (k = 0 ; k<14; k+=1) {
1196                         DEBUG(" 0x%x", temp[k]);
1197                     }
1198                     DEBUG("\n");
1199                 }
1200 #endif
1201             } else {
1202                 /* "i" is the current position in the LP
1203                 array. It is adjusted based in the run
1204                 each time around. This implines that the
1205                 run value is the number of 0 elements in
1206                 the LP array between non-zero values. */
1207                 int k, i = 1;
1208                 for (k = 0; k < num_nonzero; k+=1) {
1209                     i += RLCoeffs[k*2];
1210                     AdaptiveLPScan(image, LPInput[ndx], i, RLCoeffs[k*2+1]);
1211                     i += 1;
1212                 }
1213             }
1214         }
1215 
1216 #if defined(DEBUG)
1217         if (image->use_clr_fmt == 2/*YUV422*/) {
1218             int k;
1219             DEBUG(" lp val[ndx=%d] before refine ==", ndx);
1220             for (k = 1 ; k<8; k+=1) {
1221                 DEBUG(" 0x%x/0x%x", LPInput[1][k], LPInput[2][k]);
1222             }
1223             DEBUG("\n");
1224 
1225         } else if (image->use_clr_fmt == 1/*YUV420*/) {
1226             int k;
1227             DEBUG(" lp val[ndx=%d] before refine ==", ndx);
1228             for (k = 1 ; k<4; k+=1) {
1229                 DEBUG(" 0x%x/0x%x", LPInput[1][k], LPInput[2][k]);
1230             }
1231             DEBUG("\n");
1232 
1233         } else {
1234             int k;
1235             DEBUG(" lp val[ndx=%d] before refine ==", ndx);
1236             for (k = 1 ; k<16; k+=1) {
1237                 DEBUG(" 0x%x", LPInput[ndx][k]);
1238             }
1239             DEBUG("\n");
1240             DEBUG(" adapted scan order ==");
1241             for (k = 0 ; k<15; k+=1) {
1242                 DEBUG(" %2d", image->lopass_scanorder[k]);
1243             }
1244             DEBUG("\n");
1245             DEBUG(" adapted scan totals ==");
1246             for (k = 0 ; k<15; k+=1) {
1247                 DEBUG(" %2d", image->lopass_scantotals[k]);
1248             }
1249             DEBUG("\n");
1250         }
1251 #endif
1252 
1253         int model_bits = image->model_lp.bits[chroma_flag];
1254         lap_mean[chroma_flag] += num_nonzero;
1255         DEBUG(" MB_LP: start refine, model_bits=%d, bitpos=%zu\n",
1256             model_bits, _jxr_rbitstream_bitpos(str));
1257         if (model_bits) {
1258             static const int transpose444[16] = { 0, 4, 8,12,
1259                 1, 5, 9,13,
1260                 2, 6,10,14,
1261                 3, 7,11,15 };
1262             if (chroma_flag == 0) {
1263                 int k;
1264                 for (k=1 ;k<16; k+=1) {
1265                     int k_ptr = transpose444[k];
1266                     LPInput[ndx][k_ptr] = r_REFINE_LP(str, LPInput[ndx][k_ptr], model_bits);
1267                 }
1268             } else {
1269                 int k;
1270                 switch (image->use_clr_fmt) {
1271                     case 1: /* YUV420 */
1272                         for (k=1 ; k<4; k+=1) {
1273                             int k_ptr = transpose420[k];
1274                             LPInput[1][k_ptr] = r_REFINE_LP(str, LPInput[1][k_ptr], model_bits);
1275                             LPInput[2][k_ptr] = r_REFINE_LP(str, LPInput[2][k_ptr], model_bits);
1276                         }
1277                         break;
1278                     case 2: /* YUV422 */
1279                         for (k=1 ; k<8; k+=1) {
1280                             int k_ptr = transpose422[k];
1281                             DEBUG(" MP_LP: Refine LP_Input[1/2][%d] = 0x%x/0x%x bitpos=%zu\n",
1282                                 k_ptr, LPInput[1][k_ptr], LPInput[2][k_ptr],
1283                                 _jxr_rbitstream_bitpos(str));
1284                             LPInput[1][k_ptr] = r_REFINE_LP(str, LPInput[1][k_ptr], model_bits);
1285                             LPInput[2][k_ptr] = r_REFINE_LP(str, LPInput[2][k_ptr], model_bits);
1286                         }
1287                         break;
1288                     default: /* All others */
1289                         for (k=1 ;k<16; k+=1) {
1290                             int k_ptr = transpose444[k];
1291                             LPInput[ndx][k_ptr] = r_REFINE_LP(str, LPInput[ndx][k_ptr], model_bits);
1292                         }
1293                         break;
1294                 }
1295             }
1296         }
1297 
1298         /* Stash the calculated LP values into the current
1299         MACROBLK strip */
1300         if (chroma_flag == 0) {
1301             /* All luma planes are simply copied into the macroblk. */
1302             int k;
1303             DEBUG(" lp val ==");
1304             for (k = 1 ; k<16; k+=1) {
1305                 DEBUG(" 0x%x", LPInput[ndx][k]);
1306                 MACROBLK_CUR_LP(image, ndx, tx, mx, k-1) = LPInput[ndx][k];
1307             }
1308             DEBUG("\n");
1309         } else {
1310             int k;
1311             DEBUG(" lp val (ch=%d) ==", ndx);
1312             switch (image->use_clr_fmt) {
1313                 case 1:/* YUV420 */
1314                     /* The chroma for YUV420 is interleaved. */
1315                     for (k = 1 ; k < 4 ; k+=1) {
1316                         DEBUG(" 0x%x/0x%x", LPInput[1][k], LPInput[2][k]);
1317                         MACROBLK_CUR_LP(image, 1, tx, mx, k-1) = LPInput[1][k];
1318                         MACROBLK_CUR_LP(image, 2, tx, mx, k-1) = LPInput[2][k];
1319                     }
1320                     break;
1321                 case 2:/* YUV422 */
1322                     /* The chroma for YUV422 is interleaved. */
1323                     for (k = 1 ; k < 8 ; k+=1) {
1324                         DEBUG(" 0x%x/0x%x", LPInput[1][k], LPInput[2][k]);
1325                         MACROBLK_CUR_LP(image, 1, tx, mx, k-1) = LPInput[1][k];
1326                         MACROBLK_CUR_LP(image, 2, tx, mx, k-1) = LPInput[2][k];
1327                     }
1328                     break;
1329                 default:
1330                     for (k = 1 ; k < 16 ; k += 1) {
1331                         DEBUG(" 0x%x", LPInput[ndx][k]);
1332                         MACROBLK_CUR_LP(image, ndx, tx, mx, k-1) = LPInput[ndx][k];
1333                     }
1334                     break;
1335             }
1336             DEBUG("\n");
1337         }
1338     }
1339 
1340     DEBUG(" MB_LP: UpdateModelMB lap_mean={%d, %d}\n", lap_mean[0], lap_mean[1]);
1341     _jxr_UpdateModelMB(image, lap_mean, &image->model_lp, 1/*band=LP*/);
1342     if (_jxr_ResetContext(image, tx, mx)) {
1343         DEBUG(" AdaptLP at the end of mx=%d (my=%d, ndx=%u)\n", mx, my, ndx);
1344         _jxr_AdaptLP(image);
1345     }
1346 
1347     DEBUG(" MB_LP DONE tile=[%u %u] mb=[%u %u]\n", tx, ty, mx, my);
1348 }
1349 
1350 /*
1351 * This decides the MBCBP for the macroblock. This value is then used
1352 * by the MB_HP to know how to decide the HP values for the macroblock.
1353 */
_jxr_r_MB_CBP(jxr_image_t image,struct rbitstream * str,int alpha_flag,unsigned tx,unsigned ty,unsigned mx,unsigned my)1354 int _jxr_r_MB_CBP(jxr_image_t image, struct rbitstream*str, int alpha_flag,
1355                   unsigned tx, unsigned ty, unsigned mx, unsigned my)
1356 {
1357     static const int flc_table[] = {0, 2, 1, 2, 2, 0};
1358     static const int off_table[] = {0, 4, 2, 8, 12, 1};
1359     static const int out_table[] = {
1360         0, 15, 3, 12,
1361         1, 2, 4, 8,
1362         5, 6, 9, 10,
1363         7, 11, 13, 14 };
1364 
1365         int diff_cbp[MAX_CHANNELS];
1366         int idx;
1367         for (idx = 0 ; idx < MAX_CHANNELS ; idx += 1)
1368             diff_cbp[idx] = 0;
1369 
1370         DEBUG(" MB_CBP tile=[%u %u] mb=[%u %u] bitpos=%zu\n",
1371             tx, ty, mx, my, _jxr_rbitstream_bitpos(str));
1372 
1373         if (_jxr_InitContext(image, tx, ty, mx, my)) {
1374             DEBUG(" MB_CBP: InitContext\n");
1375             /* This happens only at the top left edge of the tile. */
1376             _jxr_InitCBPVLC(image);
1377         }
1378 
1379         /* "Channels" is not quite the same as "planes". For the
1380         purposes of CBP parsing, a color image has 1 channel. */
1381         int channels = 1;
1382         if (image->use_clr_fmt==4/*YUVK*/ || image->use_clr_fmt==6/*NCOMPONENT*/)
1383             channels = image->num_channels;
1384 
1385         /* This "for" loop decides not the code block pattern itself,
1386         but the encoded difference values. These are then added to
1387         the predicted values that are calculated later to make the
1388         actual MBCBP values. */
1389         int chan;
1390         for (chan = 0 ; chan < channels ; chan += 1) {
1391             DEBUG(" MB_CBP: Decode CBP for channel %d bitpos=%zu\n", chan, _jxr_rbitstream_bitpos(str));
1392             struct adaptive_vlc_s*vlc = image->vlc_table + DecNumCBP;
1393             int num_cbp = get_num_cbp(str, vlc);
1394 
1395             assert(vlc->deltatable == 0 && num_cbp < 5);
1396             static const int Num_CBP_Delta[5] = {0, -1, 0, 1, 1};
1397             vlc->discriminant += Num_CBP_Delta[num_cbp];
1398 
1399             DEBUG(" MB_CBP: Got num_cbp=%d, start REFINE_CBP at bitpos=%zu\n",
1400                 num_cbp, _jxr_rbitstream_bitpos(str));
1401 
1402             int cbp = r_REFINE_CBP(str, num_cbp);
1403 
1404             DEBUG(" MB_CBP: Refined CBP=0x%x (num=%d)\n", cbp, num_cbp);
1405 
1406             /* The cbp is a "block present" bit hask for a group of
1407             4 blocks. This is used to inform the loop below that
1408             then tries to fill discern the 4 bits for the range
1409             enabled by this first level cbp. For example, if
1410             cbp=0x5, then the 16 diff_cbp values are 0x0?0? where
1411             the ? nibbles are yet to be resolved by the loop
1412             below. */
1413 
1414             int blk;
1415             for (blk = 0 ; blk < 4 ; blk += 1) {
1416                 if ( (cbp & (1<<blk)) == 0 )
1417                     continue;
1418 
1419                 vlc = image->vlc_table + DecNumBlkCBP;
1420                 DEBUG(" MB_CBP: block=%d Use DecNumBlkCBP table=%d, discriminant=%d, bitpos=%zu\n",
1421                     blk, vlc->table, vlc->discriminant, _jxr_rbitstream_bitpos(str));
1422 
1423                 int num_blkcbp = get_num_blkcbp(image, str, vlc);
1424 
1425                 assert(vlc->deltatable == 0);
1426 
1427                 if (image->use_clr_fmt==0 || image->use_clr_fmt==4 || image->use_clr_fmt==6) {
1428                     assert(num_blkcbp < 5);
1429                     static const int Num_BLKCBP_Delta5[5] = {0, -1, 0, 1, 1};
1430                     vlc->discriminant += Num_BLKCBP_Delta5[num_blkcbp];
1431                 } else {
1432                     assert(num_blkcbp < 9);
1433                     static const int Num_BLKCBP_Delta9[9] = {2, 2, 1, 1, -1, -2, -2, -2, -3};
1434                     vlc->discriminant += Num_BLKCBP_Delta9[num_blkcbp];
1435                 }
1436 
1437                 DEBUG(" MB_CBP: NUM_BLKCBP=%d, discriminant becomes=%d, \n",
1438                     num_blkcbp, vlc->discriminant);
1439 
1440                 int val = num_blkcbp + 1;
1441 
1442                 int blkcbp = 0;
1443 
1444                 /* Should only be true if this is chroma data. */
1445                 if (val >= 6) {
1446                     int chr_cbp = get_value_012(str);
1447                     blkcbp = 0x10 * (chr_cbp+1);
1448                     if (val >= 9) {
1449                         int val_inc = get_value_012(str);
1450                         val += val_inc;
1451                     }
1452                     DEBUG(" MB_CBP: iVal=%d, CHR_CBP=%x\n", val, chr_cbp);
1453                     val -= 6;
1454                 }
1455                 assert(val < 6);
1456 
1457                 int code = off_table[val];
1458                 if (flc_table[val]) {
1459                     code += _jxr_rbitstream_uintN(str, flc_table[val]);
1460                 }
1461 
1462                 assert(code < 16);
1463                 blkcbp += out_table[code];
1464 
1465                 DEBUG(" MB_CBP: NUM_BLKCBP=%d, iCode=%d\n", num_blkcbp, code);
1466                 DEBUG(" MB_CBP: blkcbp=0x%x for chunk blk=%d\n", blkcbp, blk);
1467 
1468                 /* blkcbp is done. Now calculate the
1469                 diff_cbp. How this is done (and how many
1470                 there are) depend on the color format. */
1471 
1472                 switch (image->use_clr_fmt) {
1473                     case 3: /*YUV444*/
1474                         diff_cbp[0] |= (blkcbp&0xf) << (blk * 4);
1475                         if (blkcbp & 0x10) {
1476                             int num_ch_blk = get_num_ch_blk(str);
1477                             int cbp_chr = r_REFINE_CBP(str, num_ch_blk+1);
1478                             DEBUG(" MB_CBP: Refined CBP_U=0x%x (num=%d)\n", cbp_chr, num_ch_blk);
1479                             diff_cbp[1] |= cbp_chr << (blk*4);
1480                         }
1481                         if (blkcbp & 0x20) {
1482                             int num_ch_blk = get_num_ch_blk(str);
1483                             int cbp_chr = r_REFINE_CBP(str, num_ch_blk+1);
1484                             DEBUG(" MB_CBP: Refined CBP_V=0x%x (num=%d)\n", cbp_chr, num_ch_blk);
1485                             diff_cbp[2] |= cbp_chr << (blk*4);
1486                         }
1487                         break;
1488 
1489                     case 2: /*YUV422*/
1490                         diff_cbp[0] |= (blkcbp&0xf) << (blk*4);
1491                         if (blkcbp & 0x10) {
1492                             const int shift[4] = {0, 1, 4, 5};
1493                             int cbp_ch_blk = get_value_012(str);
1494                             int cbp_chr = shift[cbp_ch_blk+1];
1495                             diff_cbp[1] |= cbp_chr << shift[blk];
1496                             DEBUG(" MB_CBP: Refined CBP_U=0x%x (cbp_ch_blk=%d, blk=%d)\n",
1497                                 diff_cbp[1], cbp_ch_blk, blk);
1498                         }
1499                         if (blkcbp & 0x20) {
1500                             const int shift[4] = {0, 1, 4, 5};
1501                             int cbp_ch_blk = get_value_012(str);
1502                             int cbp_chr = shift[cbp_ch_blk+1];
1503                             diff_cbp[2] |= cbp_chr << shift[blk];
1504                             DEBUG(" MB_CBP: Refined CBP_V=0x%x (cbp_ch_blk=%d, blk=%d)\n",
1505                                 diff_cbp[2], cbp_ch_blk, blk);
1506                         }
1507                         break;
1508 
1509                     case 1: /*YUV420*/
1510                         diff_cbp[0] |= (blkcbp & 0xf) << (blk*4);
1511                         diff_cbp[1] |= ((blkcbp >> 4) & 1) << blk;
1512                         diff_cbp[2] += ((blkcbp >> 5) & 1) << blk;
1513                         break;
1514 
1515                     default:
1516                         diff_cbp[chan] |= blkcbp << (blk*4);
1517                         break;
1518                 }
1519             }
1520             DEBUG(" MB_CBP: chan=%d, num_cbp=%d, cbp=0x%1x\n", chan, num_cbp, cbp);
1521         }
1522 
1523 #if defined(DETAILED_DEBUG)
1524         for (chan = 0 ; chan < image->num_channels ; chan += 1) {
1525             DEBUG(" MB_CBP: diff_cbp[%d]=0x%04x\n", chan, diff_cbp[chan]);
1526         }
1527 #endif
1528 
1529         r_PredCBP(image, diff_cbp, tx, ty, mx, my);
1530 
1531         DEBUG(" MB_CBP done tile=[%u %u] mb=[%u %u]\n", tx, ty, mx, my);
1532         return 0;
1533 }
1534 
r_REFINE_CBP(struct rbitstream * str,int num)1535 static int r_REFINE_CBP(struct rbitstream*str, int num)
1536 {
1537     switch (num) {
1538         case 1:
1539             return 1 << _jxr_rbitstream_uint2(str);
1540 
1541         case 2:
1542             /*
1543             * table value
1544             * 00 3
1545             * 01 5
1546             * 100 6
1547             * 101 9
1548             * 110 10
1549             * 111 12
1550             */
1551             if (_jxr_rbitstream_uint1(str) == 0) {
1552                 if (_jxr_rbitstream_uint1(str) == 0)
1553                     return 3;
1554                 else
1555                     return 5;
1556             } else { /* 1xx */
1557                 if (_jxr_rbitstream_uint1(str) == 0) { /* 10x */
1558                     if (_jxr_rbitstream_uint1(str) == 0)
1559                         return 6;
1560                     else
1561                         return 9;
1562                 } else { /* 11x */
1563                     if (_jxr_rbitstream_uint1(str) == 0)
1564                         return 10;
1565                     else
1566                         return 12;
1567                 }
1568             }
1569 
1570         case 3:
1571             return 0x0f ^ (1 << _jxr_rbitstream_uint2(str));
1572 
1573         case 4:
1574             return 0x0f;
1575 
1576         default:
1577             return 0x00;
1578     }
1579 }
1580 
1581 
_jxr_r_MB_HP(jxr_image_t image,struct rbitstream * str,int alpha_flag,unsigned tx,unsigned ty,unsigned mx,unsigned my)1582 int _jxr_r_MB_HP(jxr_image_t image, struct rbitstream*str,
1583                  int alpha_flag,
1584                  unsigned tx, unsigned ty,
1585                  unsigned mx, unsigned my)
1586 {
1587     DEBUG(" MB_HP tile=[%u %u] mb=[%u %u] bitpos=%zu\n",
1588         tx, ty, mx, my, _jxr_rbitstream_bitpos(str));
1589 
1590     if (_jxr_InitContext(image, tx, ty, mx, my)) {
1591         DEBUG(" MB_HP: InitContext\n");
1592         /* This happens only at the top left edge of the tile. */
1593         _jxr_InitHPVLC(image);
1594         _jxr_InitializeAdaptiveScanHP(image);
1595         _jxr_InitializeModelMB(&image->model_hp, 2/*band=HP*/);
1596     }
1597 
1598     if (_jxr_ResetTotals(image, mx)) {
1599         _jxr_ResetTotalsAdaptiveScanHP(image);
1600     }
1601 
1602     /* FLEXBITS are embedded in the HP data if there are FLEXBITS
1603     present in the bitstream AND we are in SPATIAL (not
1604     FREQUENCY) mode. */
1605     int flex_flag = 1;
1606     if (image->bands_present == 1) /* NOFLEXBITS */
1607         flex_flag = 0;
1608     if (FREQUENCY_MODE_CODESTREAM_FLAG(image) != 0) /* FREQUENCY_MODE */
1609         flex_flag = 0;
1610 
1611     int lap_mean[2];
1612     lap_mean[0] = 0;
1613     lap_mean[1] = 0;
1614 
1615     /* Calculate the MB HP prediction mode. This uses only local
1616     information, namely the LP values. */
1617     int mbhp_pred_mode = r_calculate_mbhp_mode(image, tx, mx);
1618     assert(mbhp_pred_mode < 4);
1619 
1620     int idx;
1621     for (idx = 0 ; idx < image->num_channels; idx += 1) {
1622         int chroma_flag = idx>0? 1 : 0;
1623         int nblocks = 4;
1624         if (chroma_flag && image->use_clr_fmt==1/*YUV420*/)
1625             nblocks = 1;
1626         else if (chroma_flag && image->use_clr_fmt==2/*YUV422*/)
1627             nblocks = 2;
1628 
1629         unsigned model_bits = image->model_hp.bits[chroma_flag];
1630         int cbp = MACROBLK_CUR_HPCBP(image, idx, tx, mx);
1631         int block;
1632         DEBUG(" MB_HP channel=%d, cbp=0x%x, model_bits=%u MBHPMode=%d\n",
1633             idx, cbp, model_bits, mbhp_pred_mode);
1634         for (block=0 ; block<(nblocks*4) ; block += 1, cbp >>= 1) {
1635             int bpos = block;
1636             /* Only remap the Y plane of YUV42X data. */
1637             if (nblocks == 4)
1638                 bpos = _jxr_hp_scan_map[block];
1639             int num_nonzero = r_DECODE_BLOCK_ADAPTIVE(image, str, tx, mx,
1640                 cbp&1, chroma_flag,
1641                 idx, bpos, mbhp_pred_mode,
1642                 model_bits);
1643             if (num_nonzero < 0) {
1644                 DEBUG("ERROR: r_DECODE_BLOCK_ADAPTIVE returned rc=%d\n", num_nonzero);
1645                 return JXR_EC_ERROR;
1646             }
1647             if (flex_flag)
1648                 r_BLOCK_FLEXBITS(image, str, tx, ty, mx, my,
1649                 idx, bpos, model_bits);
1650             lap_mean[chroma_flag] += num_nonzero;
1651         }
1652 
1653     }
1654 
1655     int use_num_channels = image->num_channels;
1656     if (image->use_clr_fmt == 1/*YUV420*/ || image->use_clr_fmt == 2/*YUV422*/)
1657         use_num_channels = 1;
1658 
1659     /* Propagate HP predictions only in SPATIAL MODE. If this is
1660     FREQUENCY mode, and there is a FLEXBITS pass later, then do
1661     *not* do the predictions, leaving them to the FLEXBITS tile. */
1662     if (FREQUENCY_MODE_CODESTREAM_FLAG(image) == 0 || image->bands_present == 1) {
1663         DEBUG(" MB_HP: propagate hp predictions within MB_HP function\n");
1664         for (idx = 0 ; idx < use_num_channels ; idx += 1)
1665             _jxr_propagate_hp_predictions(image, idx, tx, mx, mbhp_pred_mode);
1666     }
1667 
1668     DEBUG(" MP_HP: lap_mean={%u, %u}, model_hp.bits={%u %u}, model_hp.state={%d %d}\n",
1669         lap_mean[0], lap_mean[1],
1670         image->model_hp.bits[0], image->model_hp.bits[1],
1671         image->model_hp.state[0], image->model_hp.state[1]);
1672 
1673     MACROBLK_CUR(image,0,tx,mx).mbhp_pred_mode = mbhp_pred_mode;
1674     MACROBLK_CUR(image,0,tx,mx).hp_model_bits[0] = image->model_hp.bits[0];
1675     MACROBLK_CUR(image,0,tx,mx).hp_model_bits[1] = image->model_hp.bits[1];
1676 
1677     _jxr_UpdateModelMB(image, lap_mean, &image->model_hp, 2/*band=HP*/);
1678     if (_jxr_ResetContext(image, tx, mx)) {
1679         DEBUG(" MB_HP: Run AdaptHP\n");
1680         _jxr_AdaptHP(image);
1681     }
1682 
1683     DEBUG(" MP_HP: Updated: lap_mean={%u, %u}, model_hp.bits={%u %u}, model_hp.state={%d %d}\n",
1684         lap_mean[0], lap_mean[1],
1685         image->model_hp.bits[0], image->model_hp.bits[1],
1686         image->model_hp.state[0], image->model_hp.state[1]);
1687 
1688     DEBUG(" MB_HP DONE tile=[%u %u] mb=[%u %u]\n", tx, ty, mx, my);
1689     return 0;
1690 }
1691 
_jxr_r_MB_FLEXBITS(jxr_image_t image,struct rbitstream * str,int alpha_flag,unsigned tx,unsigned ty,unsigned mx,unsigned my)1692 int _jxr_r_MB_FLEXBITS(jxr_image_t image, struct rbitstream*str,
1693                        int alpha_flag,
1694                        unsigned tx, unsigned ty,
1695                        unsigned mx, unsigned my)
1696 {
1697     int idx;
1698     for (idx = 0 ; idx < image->num_channels ; idx += 1) {
1699         int chroma_flag = idx>0? 1 : 0;
1700         int nblocks = 4;
1701         if (chroma_flag && image->use_clr_fmt==1/*YUV420*/)
1702             nblocks = 1;
1703         else if (chroma_flag && image->use_clr_fmt==2/*YUV422*/)
1704             nblocks = 2;
1705 
1706         unsigned model_bits = MACROBLK_CUR(image,0,tx,mx).hp_model_bits[chroma_flag];
1707         int block;
1708         for (block=0 ; block<(nblocks*4) ; block += 1) {
1709             int bpos = block;
1710             /* Only remap the Y plane of YUV42X data. */
1711             if (nblocks == 4)
1712                 bpos = _jxr_hp_scan_map[block];
1713 
1714             r_BLOCK_FLEXBITS(image, str, tx, ty, mx, my,
1715                 idx, bpos, model_bits);
1716         }
1717     }
1718 
1719     return 0;
1720 }
1721 
1722 /*
1723 * Decode a single DC component value from the input stream.
1724 */
r_DEC_DC(jxr_image_t image,struct rbitstream * str,unsigned tx,unsigned ty,unsigned mx,unsigned my,int model_bits,int chroma_flag,int is_dc_ch)1725 static int32_t r_DEC_DC(jxr_image_t image, struct rbitstream*str,
1726                         unsigned tx, unsigned ty,
1727                         unsigned mx, unsigned my,
1728                         int model_bits, int chroma_flag, int is_dc_ch)
1729 {
1730     int32_t dc_val = 0;
1731 
1732     if (is_dc_ch) {
1733         dc_val = r_DECODE_ABS_LEVEL(image, str, 0/*DC*/, chroma_flag) -1;
1734         DEBUG(" DEC_DC: DECODE_ABS_LEVEL = %u (0x%08x)\n", dc_val, dc_val);
1735     }
1736 
1737     /* If there are model_bits, then read them literally from the
1738     bitstream and use them as the LSB bits for the DC value. */
1739     if (model_bits > 0) {
1740         DEBUG(" DEC_DC: Collect %u model_bits\n", model_bits);
1741         int idx;
1742         for (idx = 0 ; idx < model_bits ; idx += 1) {
1743             dc_val <<= 1;
1744             dc_val |= _jxr_rbitstream_uint1(str);
1745         }
1746     }
1747 
1748     /* If the dc_val is non-zero, it may have a sign so decode the
1749     sign bit and apply it. */
1750     if (dc_val != 0) {
1751         int sign_flag = _jxr_rbitstream_uint1(str);
1752         DEBUG(" DEC_DC: sign_flag=%s\n", sign_flag? "true":"false");
1753         if (sign_flag)
1754             dc_val = - dc_val;
1755     }
1756 
1757     DEBUG(" DEC_DC: DC value is %d (0x%08x)\n", dc_val, dc_val);
1758     return dc_val;
1759 }
1760 
1761 /*
1762 * This function decodes one sample from one of the bands. The code is
1763 * the same for any of the bands. The band (and chroma_flag) is only
1764 * used to select the vlc_table.
1765 *
1766 * Note that the chroma_flag is only interpreted as the "chroma_flag"
1767 * when the band is DC. Otherwise, the chroma_flag argument is taken
1768 * as the "context" argument described in the specification.
1769 */
r_DECODE_ABS_LEVEL(jxr_image_t image,struct rbitstream * str,int band,int chroma_flag)1770 static uint32_t r_DECODE_ABS_LEVEL(jxr_image_t image, struct rbitstream*str,
1771                                    int band, int chroma_flag)
1772 {
1773     int vlc_select = _jxr_vlc_select(band, chroma_flag);
1774     DEBUG(" Use vlc_select = %s (table=%d) to decode level index, bitpos=%zu\n",
1775         _jxr_vlc_index_name(vlc_select), image->vlc_table[vlc_select].table,
1776         _jxr_rbitstream_bitpos(str));
1777 
1778     const int remap[] = {2, 3, 4, 6, 10, 14};
1779     const int fixed_len[] = {0, 0, 1, 2, 2, 2};
1780 
1781     int abslevel_index = dec_abslevel_index(image, str, vlc_select);
1782     DEBUG(" ABSLEVEL_INDEX = %d\n", abslevel_index);
1783 
1784     image->vlc_table[vlc_select].discriminant += _jxr_abslevel_index_delta[abslevel_index];
1785 
1786     uint32_t level;
1787     if (abslevel_index < 6) {
1788         int fixed = fixed_len[abslevel_index];
1789         level = remap[abslevel_index];
1790         uint32_t level_ref = 0;
1791         if (fixed > 0) {
1792             assert(fixed <= 32);
1793             int idx;
1794             for (idx = 0 ; idx < fixed ; idx += 1) {
1795                 level_ref <<= 1;
1796                 level_ref |= _jxr_rbitstream_uint1(str);
1797             }
1798             level += level_ref;
1799         }
1800         DEBUG(" ABS_LEVEL = 0x%x (fixed = %d, level_ref = %d)\n",
1801             level, fixed, level_ref);
1802     } else {
1803         int fixed = 4 + _jxr_rbitstream_uint4(str);
1804         if (fixed == 19) {
1805             fixed += _jxr_rbitstream_uint2(str);
1806             if (fixed == 22) {
1807                 fixed += _jxr_rbitstream_uint3(str);
1808             }
1809         }
1810 
1811         assert(fixed <= 32);
1812 
1813         uint32_t level_ref = 0;
1814         int idx;
1815         for (idx = 0 ; idx < fixed ; idx += 1) {
1816             level_ref <<= 1;
1817             level_ref |= _jxr_rbitstream_uint1(str);
1818         }
1819         level = 2 + (1 << fixed) + level_ref;
1820         DEBUG(" ABS_LEVEL = 0x%x (fixed = %d, level_ref = %d)\n",
1821             level, fixed, level_ref);
1822     }
1823 
1824     return level;
1825 }
1826 
1827 /*
1828 * The DECODE_BLOCK decodes the block as run/coefficient pairs. The
1829 * run is the distance to the next coefficient, and is followed by the
1830 * coefficient itself. The skipped values are implicitly zeros. A
1831 * later process takes these pairs including adaptation of their position.
1832 */
r_DECODE_BLOCK(jxr_image_t image,struct rbitstream * str,int chroma_flag,int coeff[32],int band,int location)1833 int r_DECODE_BLOCK(jxr_image_t image, struct rbitstream*str,
1834                    int chroma_flag, int coeff[32], int band, int location)
1835 {
1836     DEBUG(" DECODE_BLOCK chroma_flag=%d, band=%d, location=%d bitpos=%zu\n",
1837         chroma_flag, band, location, _jxr_rbitstream_bitpos(str));
1838     int num_nz = 1;
1839 
1840     /* The index is a bit field that encodes three values:
1841     * index[0] : 1 means the run to the next coeff is == 0
1842     * index[1] : 1 means the next coefficient is >1
1843     * index[3:2]: 0 This is the last coefficient
1844     * 1 the next non-zero coefficient immediately follows
1845     * 2 there are zero coefficients before the next.
1846     */
1847     int index = r_DECODE_FIRST_INDEX(image, str, chroma_flag, band);
1848 
1849     DEBUG(" first index=0x%x\n", index);
1850 
1851     int sr = index & 1;
1852     int srn = index >> 2;
1853     int context = sr & srn;
1854 
1855     /* Decode the first coefficient. Note that the chroma_flag
1856     argument to DECODE_ABS_LEVEL really is supposed to be the
1857     context. It is "chroma" for DC values (band==0) and context
1858     for LP and HP values. This DECODE_BLOCK is only called for
1859     LP and HP blocks. */
1860     int sign_flag = _jxr_rbitstream_uint1(str);
1861     if (index&2)
1862         coeff[1] = r_DECODE_ABS_LEVEL(image, str, band, context);
1863     else
1864         coeff[1] = 1;
1865 
1866     if (sign_flag)
1867         coeff[1] = -coeff[1];
1868 
1869     /* Decode the run to the first coefficient. */
1870     if (index&1) {
1871         coeff[0] = 0;
1872     } else {
1873         assert( location < 15 );
1874         coeff[0] = r_DECODE_RUN(image, str, 15-location);
1875     }
1876 
1877     DEBUG(" coeff[0] = %d (run)\n", coeff[0]);
1878     DEBUG(" coeff[1] = 0x%x (coeff)\n", coeff[1]);
1879 
1880     location += coeff[0] + 1;
1881 
1882     while (srn != 0) { /* While more coefficients are expected... */
1883         sr = srn & 1;
1884 
1885         /* Decode run to the next coefficient. */
1886         if (srn & 1) {
1887             coeff[num_nz*2] = 0;
1888         } else {
1889             coeff[num_nz*2] = r_DECODE_RUN(image, str, 15-location);
1890         }
1891 
1892         DEBUG(" coeff[%d*2+0] = %d (run)\n", num_nz, coeff[num_nz*2]);
1893 
1894         location += coeff[num_nz*2] + 1;
1895 
1896         /* The index is a bit field that encodes two values:
1897         * index[0] : 1 means the run to the next coeff is == 0
1898         * index[2:1]: 0 This is the last coefficient
1899         * 1 the next non-zero coefficient immediately follows
1900         * 2 there are zero coefficients before the next.
1901         * The location can clue the DECODE_INDEX that certain
1902         * constraints on the possible index values may exist,
1903         * and certain restricted tables are used.
1904         */
1905         index = r_DECODE_INDEX(image, str, location, chroma_flag, band, context);
1906         DEBUG(" next index=0x%x\n", index);
1907 
1908         srn = index >> 1;
1909         context &= srn;
1910 
1911         /* Decode the next coefficient. */
1912         sign_flag = _jxr_rbitstream_uint1(str);
1913         if (index & 1)
1914             coeff[num_nz*2+1] = r_DECODE_ABS_LEVEL(image, str,
1915             band, context);
1916         else
1917             coeff[num_nz*2+1] = 1;
1918 
1919         if (sign_flag)
1920             coeff[num_nz*2+1] = -coeff[num_nz*2+1];
1921 
1922         DEBUG(" coeff[%d*2+1] = 0x%x (coeff)\n", num_nz, coeff[num_nz*2+1]);
1923         num_nz += 1;
1924     }
1925 
1926     DEBUG(" DECODE_BLOCK done, num_nz=%d\n", num_nz);
1927     return num_nz;
1928 }
1929 
r_DECODE_FIRST_INDEX(jxr_image_t image,struct rbitstream * str,int chroma_flag,int band)1930 static int r_DECODE_FIRST_INDEX(jxr_image_t image, struct rbitstream*str,
1931                                 int chroma_flag, int band)
1932 {
1933     /* VALUE TABLE0
1934     * 0 0000 1..
1935     * 1 0000 01.
1936     * 2 0000 000
1937     * 3 0000 001
1938     * 4 0010 0..
1939     * 5 010. ...
1940     * 6 0010 1..
1941     * 7 1... ...
1942     * 8 0011 0..
1943     * 9 0001 ...
1944     * 10 0011 1..
1945     * 11 011. ...
1946     * (Table 59: Note that the first bit is handled as a special
1947     * case, so the array only needs to account for the last 6 bits.)
1948     */
1949     static const unsigned char c0b[64] = {
1950         6, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
1951         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
1952         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1953         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
1954     };
1955     static const signed char c0v[64] = {
1956         2, 3, 1, 1, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9, 9, 9,
1957         4, 4, 4, 4, 6, 6, 6, 6, 8, 8, 8, 8, 10,10,10,10,
1958         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1959         11,11,11,11, 11,11,11,11, 11,11,11,11, 11,11,11,11
1960     };
1961     /* VALUE TABLE1
1962     * 0 0010 ..
1963     * 1 0001 0.
1964     * 2 0000 00
1965     * 3 0000 01
1966     * 4 0011 ..
1967     * 5 010. ..
1968     * 6 0001 1.
1969     * 7 11.. ..
1970     * 8 011. ..
1971     * 9 100. ..
1972     * 10 0000 1.
1973     * 11 101. ..
1974     */
1975     static const unsigned char c1b[64] = {
1976         6, 6, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
1977         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1978         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1979         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
1980     };
1981     static const signed char c1v[64] = {
1982         2, 3,10,10, 1, 1, 6, 6, 0, 0, 0, 0, 4, 4, 4, 4,
1983         5, 5, 5, 5, 5, 5, 5, 5, 8, 8, 8, 8, 8, 8, 8, 8,
1984         9, 9, 9, 9, 9, 9, 9, 9, 11,11,11,11, 11,11,11,11,
1985         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
1986     };
1987 
1988     /* VALUE TABLE2
1989     * 0 11.. ...
1990     * 1 001. ...
1991     * 2 0000 000
1992     * 3 0000 001
1993     * 4 0000 1..
1994     * 5 010. ...
1995     * 6 0000 010
1996     * 7 011. ...
1997     * 8 100. ...
1998     * 9 101. ...
1999     * 10 0000 011
2000     * 11 0001 ...
2001     */
2002     static const unsigned char c2b[128] = {
2003         7, 7, 7, 7, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
2004         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2005         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2006         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2007 
2008         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2009         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2010         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2011         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
2012     };
2013     static const signed char c2v[128] = {
2014         2, 3, 6,10, 4, 4, 4, 4, 11,11,11,11, 11,11,11,11,
2015         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2016         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
2017         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
2018 
2019         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
2020         9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
2021         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2022         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2023     };
2024 
2025     /* VALUE TABLE3
2026     * 0 001. ...
2027     * 1 11.. ...
2028     * 2 0000 000
2029     * 3 0000 1..
2030     * 4 0001 0..
2031     * 5 010. ...
2032     * 6 0000 001
2033     * 7 011. ...
2034     * 8 0001 1..
2035     * 9 100. ...
2036     * 10 0000 01.
2037     * 11 101. ...
2038     */
2039     static const unsigned char c3b[128] = {
2040         7, 7, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
2041         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2042         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2043         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2044 
2045         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2046         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2047         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2048         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
2049     };
2050     static const signed char c3v[128] = {
2051         2, 6,10,10, 3, 3, 3, 3, 4, 4, 4, 4, 8, 8, 8, 8,
2052         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2053         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
2054         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
2055 
2056         9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
2057         11,11,11,11, 11,11,11,11, 11,11,11,11, 11,11,11,11,
2058         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2059         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
2060     };
2061 
2062     /* VALUE TABLE4
2063     * 0 010. ....
2064     * 1 1... ....
2065     * 2 0000 001.
2066     * 3 0001 ....
2067     * 4 0000 010.
2068     * 5 011. ....
2069     * 6 0000 0000
2070     * 7 0010 ....
2071     * 8 0000 011.
2072     * 9 0011 ....
2073     * 10 0000 0001
2074     * 11 0000 1...
2075     */
2076     static const unsigned char c4b[128] = {
2077         7, 7, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4,
2078         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2079         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2080         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2081 
2082         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2083         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2084         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2085         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
2086     };
2087     static const signed char c4v[128] = {
2088         6,10, 2, 2, 4, 4, 8, 8, 11,11,11,11, 11,11,11,11,
2089         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2090         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
2091         9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
2092 
2093         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2094         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2095         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
2096         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
2097     };
2098 
2099     abs_level_vlc_index_t vlc_select = (abs_level_vlc_index_t)0;
2100 
2101     switch (band) {
2102         case 1: /* LP */
2103             if (chroma_flag)
2104                 vlc_select = DecFirstIndLPChr;
2105             else
2106                 vlc_select = DecFirstIndLPLum;
2107             break;
2108         case 2: /* HP */
2109             if (chroma_flag)
2110                 vlc_select = DecFirstIndHPChr;
2111             else
2112                 vlc_select = DecFirstIndHPLum;
2113             break;
2114         default:
2115             assert(0);
2116             break;
2117     }
2118 
2119     int vlc_table = image->vlc_table[vlc_select].table;
2120     int first_index = 0;
2121 
2122     switch (vlc_table) {
2123         case 0:
2124             if (_jxr_rbitstream_uint1(str)) {
2125                 first_index = 7;
2126             } else {
2127                 first_index = _jxr_rbitstream_intE(str, 6, c0b, c0v);
2128             }
2129             break;
2130 
2131         case 1:
2132             first_index = _jxr_rbitstream_intE(str, 6, c1b, c1v);
2133             break;
2134 
2135         case 2:
2136             first_index = _jxr_rbitstream_intE(str, 7, c2b, c2v);
2137             break;
2138 
2139         case 3:
2140             first_index = _jxr_rbitstream_intE(str, 7, c3b, c3v);
2141             break;
2142 
2143         case 4:
2144             if (_jxr_rbitstream_uint1(str)) {
2145                 first_index = 1;
2146             } else {
2147                 first_index = _jxr_rbitstream_intE(str, 7, c4b, c4v);
2148             }
2149             break;
2150 
2151         default:
2152             assert(0);
2153             break;
2154     }
2155 
2156     int delta_table = image->vlc_table[vlc_select].deltatable;
2157     int delta2table = image->vlc_table[vlc_select].delta2table;
2158 
2159     typedef int deltatable_t[12];
2160     const deltatable_t FirstIndexDelta[4] = {
2161         { 1, 1, 1, 1, 1, 0, 0,-1, 2, 1, 0, 0 },
2162         { 2, 2,-1,-1,-1, 0,-2,-1, 0, 0,-2,-1 },
2163         {-1, 1, 0, 2, 0, 0, 0, 0,-2, 0, 1, 1 },
2164         { 0, 1, 0, 1,-2, 0,-1,-1,-2,-1,-2,-2 }
2165     };
2166     assert(delta_table < 4);
2167     assert(delta2table < 4);
2168     assert(first_index < 12);
2169     image->vlc_table[vlc_select].discriminant += FirstIndexDelta[delta_table][first_index];
2170     image->vlc_table[vlc_select].discriminant2 += FirstIndexDelta[delta2table][first_index];
2171     DEBUG(" DECODE_FIRST_INDEX: vlc_select = %s, vlc_table = %d, deltatable/2 = %d/%d, discriminant/2 = %d/%d, first_index=%d\n",
2172         _jxr_vlc_index_name(vlc_select), vlc_table,
2173         delta_table, delta2table,
2174         image->vlc_table[vlc_select].discriminant,
2175         image->vlc_table[vlc_select].discriminant2, first_index);
2176 
2177     return first_index;
2178 }
2179 
r_DECODE_INDEX(jxr_image_t image,struct rbitstream * str,int location,int chroma_flag,int band,int context)2180 static int r_DECODE_INDEX(jxr_image_t image, struct rbitstream*str,
2181                           int location, int chroma_flag, int band, int context)
2182 {
2183     int vlc_select = 0;
2184 
2185     switch (band) {
2186         case 1: /* LP */
2187             if (chroma_flag)
2188                 vlc_select = context? DecIndLPChr1 : DecIndLPChr0;
2189             else
2190                 vlc_select = context? DecIndLPLum1 : DecIndLPLum0;
2191             break;
2192         case 2: /* HP */
2193             if (chroma_flag)
2194                 vlc_select = context? DecIndHPChr1 : DecIndHPChr0;
2195             else
2196                 vlc_select = context? DecIndHPLum1 : DecIndHPLum0;
2197             break;
2198         default:
2199             assert(0);
2200             break;
2201     }
2202 
2203     int index = 0;
2204 
2205     /* If location > 15, then there can't possibly be coefficients
2206     after the next, so the encoding will only encode the low
2207     bit, that hints the run is zero or not. */
2208     if (location > 15) {
2209         index = _jxr_rbitstream_uint1(str);
2210         DEBUG(" DECODE_INDEX: location=%d, index=%d\n", location, index);
2211         return index;
2212     }
2213 
2214     /* If location == 15, then this is probably the last
2215     coefficient, but there may be more. We do know that there
2216     are no zero coefficients before the next (if there is one).
2217     Use a fixed table to decode the index with reduced alphabet. */
2218     if (location == 15) {
2219         /* Table 61
2220         * INDEX2 table
2221         * 0 0
2222         * 2 10
2223         * 1 110
2224         * 3 111
2225         */
2226         if (_jxr_rbitstream_uint1(str) == 0)
2227             index = 0; /* 0 */
2228         else if (_jxr_rbitstream_uint1(str) == 0)
2229             index = 2; /* 10 */
2230         else if (_jxr_rbitstream_uint1(str) == 0)
2231             index = 1; /* 110 */
2232         else
2233             index = 3; /* 111 */
2234         DEBUG(" DECODE_INDEX: location=%d, index=%d\n", location, index);
2235         return index;
2236     }
2237 
2238     /* For more general cases, use adaptive table selections to
2239     decode the full set of index possibilities. */
2240     int vlc_table = image->vlc_table[vlc_select].table;
2241     DEBUG(" DECODE_INDEX: vlc_select = %s, vlc_table = %d chroma_flag=%d\n",
2242         _jxr_vlc_index_name(vlc_select), vlc_table, chroma_flag);
2243 
2244     /* Table 60 is implemented in this switch. */
2245     switch (vlc_table) {
2246         case 0:
2247             /* INDEX1 table0
2248             * 0 1
2249             * 1 00000
2250             * 2 001
2251             * 3 00001
2252             * 4 01
2253             * 5 0001
2254             */
2255             if (_jxr_rbitstream_uint1(str) == 1)
2256                 index = 0; /* 1 */
2257             else if (_jxr_rbitstream_uint1(str) == 1)
2258                 index = 4; /* 01 */
2259             else if (_jxr_rbitstream_uint1(str) == 1)
2260                 index = 2; /* 001 */
2261             else if (_jxr_rbitstream_uint1(str) == 1)
2262                 index = 5; /* 0001 */
2263             else if (_jxr_rbitstream_uint1(str) == 1)
2264                 index = 3; /* 00001 */
2265             else
2266                 index = 1; /* 00000 */
2267             break;
2268 
2269         case 1:
2270             /* INDEX1 table1
2271             * 0 01
2272             * 1 0000
2273             * 2 10
2274             * 3 0001
2275             * 4 11
2276             * 5 001
2277             */
2278             switch (_jxr_rbitstream_uint2(str)) {
2279                 case 1: /* 01 */
2280                     index = 0;
2281                     break;
2282                 case 2: /* 10 */
2283                     index = 2;
2284                     break;
2285                 case 3: /* 11 */
2286                     index = 4;
2287                     break;
2288                 case 0: /* 00... */
2289                     if (_jxr_rbitstream_uint1(str) == 1)
2290                         index = 5; /* 001 */
2291                     else if (_jxr_rbitstream_uint1(str) == 1)
2292                         index = 3; /* 0001 */
2293                     else
2294                         index = 1; /* 0000 */
2295                     break;
2296             }
2297             break;
2298 
2299         case 2:
2300             /* INDEX1 table2
2301             * 0 0000
2302             * 1 0001
2303             * 2 01
2304             * 3 10
2305             * 4 11
2306             * 5 001
2307             */
2308             switch (_jxr_rbitstream_uint2(str)) {
2309                 case 1: /* 01 */
2310                     index = 2;
2311                     break;
2312                 case 2: /* 10 */
2313                     index = 3;
2314                     break;
2315                 case 3: /* 11 */
2316                     index = 4;
2317                     break;
2318                 case 0: /* 00... */
2319                     if (_jxr_rbitstream_uint1(str))
2320                         index = 5; /* 001 */
2321                     else if (_jxr_rbitstream_uint1(str))
2322                         index = 1; /* 0001 */
2323                     else
2324                         index = 0; /* 0000 */
2325                     break;
2326             }
2327             break;
2328 
2329         case 3:
2330             /* INDEX1 table3
2331             * 0 00000
2332             * 1 00001
2333             * 2 01
2334             * 3 1
2335             * 4 0001
2336             * 5 001
2337             */
2338             if (_jxr_rbitstream_uint1(str))
2339                 index = 3; /* 1 */
2340             else if (_jxr_rbitstream_uint1(str))
2341                 index = 2; /* 01 */
2342             else if (_jxr_rbitstream_uint1(str))
2343                 index = 5; /* 001 */
2344             else if (_jxr_rbitstream_uint1(str))
2345                 index = 4; /* 0001 */
2346             else if (_jxr_rbitstream_uint1(str))
2347                 index = 1; /* 00001 */
2348             else
2349                 index = 0; /* 00000 */
2350             break;
2351 
2352         default:
2353             assert(0);
2354     }
2355 
2356     int vlc_delta = image->vlc_table[vlc_select].deltatable;
2357     int vlc_delta2 = image->vlc_table[vlc_select].delta2table;
2358 
2359     typedef int deltatable_t[6];
2360     const deltatable_t Index1Delta[3] = {
2361         {-1, 1, 1, 1, 0, 1 },
2362         {-2, 0, 0, 2, 0, 0 },
2363         {-1,-1, 0, 1,-2, 0 }
2364     };
2365 
2366     image->vlc_table[vlc_select].discriminant += Index1Delta[vlc_delta][index];
2367     image->vlc_table[vlc_select].discriminant2+= Index1Delta[vlc_delta2][index];
2368 
2369     DEBUG(" DECODE_INDEX: vlc_select = %s, deltatable/2 = %d/%d, discriminant/2 becomes %d/%d\n",
2370         _jxr_vlc_index_name(vlc_select), vlc_delta, vlc_delta2,
2371         image->vlc_table[vlc_select].discriminant,
2372         image->vlc_table[vlc_select].discriminant2);
2373 
2374     return index;
2375 }
2376 
r_DECODE_RUN(jxr_image_t image,struct rbitstream * str,int max_run)2377 static int r_DECODE_RUN(jxr_image_t image, struct rbitstream*str, int max_run)
2378 {
2379     int run;
2380 
2381     if (max_run < 5) {
2382         DEBUG(" DECODE_RUN max_run=%d (<5) bitpos=%zu\n",
2383             max_run, _jxr_rbitstream_bitpos(str));
2384         switch (max_run) {
2385             case 1:
2386                 run = 1;
2387                 break;
2388             case 2:
2389                 if (_jxr_rbitstream_uint1(str))
2390                     run = 1;
2391                 else
2392                     run = 2;
2393                 break;
2394             case 3:
2395                 if (_jxr_rbitstream_uint1(str))
2396                     run = 1;
2397                 else if (_jxr_rbitstream_uint1(str))
2398                     run = 2;
2399                 else
2400                     run = 3;
2401                 break;
2402             case 4:
2403                 if (_jxr_rbitstream_uint1(str))
2404                     run = 1;
2405                 else if (_jxr_rbitstream_uint1(str))
2406                     run = 2;
2407                 else if (_jxr_rbitstream_uint1(str))
2408                     run = 3;
2409                 else
2410                     run = 4;
2411                 break;
2412         }
2413 
2414     } else {
2415         static const int RunBin[15] = {-1,-1,-1,-1, 2,2,2, 1,1,1,1, 0,0,0,0 };
2416         static const int RunFixedLen[15] = {0,0,1,1,3, 0,0,1,1,2, 0,0,0,0,1 };
2417         static const int Remap[15] = {1,2,3,5,7, 1,2,3,5,7, 1,2,3,4,5 };
2418         int run_index = 0;
2419         if (_jxr_rbitstream_uint1(str))
2420             run_index = 0; /* 1 */
2421         else if (_jxr_rbitstream_uint1(str))
2422             run_index = 1; /* 01 */
2423         else if (_jxr_rbitstream_uint1(str))
2424             run_index = 2; /* 001 */
2425         else if (_jxr_rbitstream_uint1(str))
2426             run_index = 4; /* 0001 */
2427         else
2428             run_index = 3; /* 0000 */
2429 
2430         DEBUG(" DECODE_RUN max_run=%d, RUN_INDEX=%d\n", max_run, run_index);
2431 
2432         assert(max_run < 15);
2433         int index = run_index + 5*RunBin[max_run];
2434         DEBUG(" DECODE_RUN index=%d\n", index);
2435 
2436         assert(run_index < 15);
2437         int fixed = RunFixedLen[index];
2438         DEBUG(" DECODE_RUN fixed=%d (bitpos=%zu)\n",
2439             fixed, _jxr_rbitstream_bitpos(str));
2440 
2441         assert(run_index < 15);
2442         run = Remap[index];
2443         if (fixed) {
2444             run += _jxr_rbitstream_uintN(str, fixed);
2445         }
2446     }
2447 
2448     DEBUG(" DECODE_RUN max_run=%d, run=%d\n", max_run, run);
2449 
2450     return run;
2451 }
2452 
2453 
r_REFINE_LP(struct rbitstream * str,int coeff,int model_bits)2454 static int r_REFINE_LP(struct rbitstream*str, int coeff, int model_bits)
2455 {
2456     int coeff_refinement = _jxr_rbitstream_uintN(str, model_bits);
2457 
2458     if (coeff > 0) {
2459         coeff <<= model_bits;
2460         coeff += coeff_refinement;
2461     } else if (coeff < 0) {
2462         coeff <<= model_bits;
2463         coeff -= coeff_refinement;
2464     } else {
2465         coeff = coeff_refinement;
2466         if (coeff) {
2467             int sign_flag = _jxr_rbitstream_uint1(str);
2468             if (sign_flag)
2469                 coeff = -coeff;
2470         }
2471     }
2472 
2473     return coeff;
2474 }
2475 
r_PredCBP(jxr_image_t image,int * diff_cbp,unsigned tx,unsigned ty,unsigned mx,unsigned my)2476 static void r_PredCBP(jxr_image_t image, int*diff_cbp,
2477                       unsigned tx, unsigned ty,
2478                       unsigned mx, unsigned my)
2479 {
2480     int idx;
2481 
2482     if (_jxr_InitContext(image, tx, ty, mx, my)) {
2483         _jxr_InitializeCBPModel(image);
2484     }
2485 
2486     assert(my == image->cur_my);
2487     switch (image->use_clr_fmt) {
2488         case 1: /*YUV420*/
2489             MACROBLK_CUR_HPCBP(image, 0, tx, mx)
2490                 = _jxr_PredCBP444(image, diff_cbp, 0, tx, mx, my);
2491             MACROBLK_CUR_HPCBP(image, 1, tx, mx)
2492                 = _jxr_PredCBP420(image, diff_cbp, 1, tx, mx, my);
2493             MACROBLK_CUR_HPCBP(image, 2, tx, mx)
2494                 = _jxr_PredCBP420(image, diff_cbp, 2, tx, mx, my);
2495             DEBUG(" PredCBP: Predicted HPCBP[ch=0]: 0x%04x (YUV420)\n",
2496                 MACROBLK_CUR_HPCBP(image, 0, tx, mx));
2497             DEBUG(" PredCBP: Predicted HPCBP[ch=1]: 0x%04x (YUV420)\n",
2498                 MACROBLK_CUR_HPCBP(image, 1, tx, mx));
2499             DEBUG(" PredCBP: Predicted HPCBP[ch=2]: 0x%04x (YUV420)\n",
2500                 MACROBLK_CUR_HPCBP(image, 2, tx, mx));
2501             break;
2502         case 2: /*YUV422*/
2503             MACROBLK_CUR_HPCBP(image, 0, tx, mx)
2504                 = _jxr_PredCBP444(image, diff_cbp, 0, tx, mx, my);
2505             MACROBLK_CUR_HPCBP(image, 1, tx, mx)
2506                 = _jxr_PredCBP422(image, diff_cbp, 1, tx, mx, my);
2507             MACROBLK_CUR_HPCBP(image, 2, tx, mx)
2508                 = _jxr_PredCBP422(image, diff_cbp, 2, tx, mx, my);
2509             DEBUG(" PredCBP: Predicted HPCBP[ch=0]: 0x%04x (YUV422)\n",
2510                 MACROBLK_CUR_HPCBP(image, 0, tx, mx));
2511             DEBUG(" PredCBP: Predicted HPCBP[ch=1]: 0x%04x (YUV422)\n",
2512                 MACROBLK_CUR_HPCBP(image, 1, tx, mx));
2513             DEBUG(" PredCBP: Predicted HPCBP[ch=2]: 0x%04x (YUV422)\n",
2514                 MACROBLK_CUR_HPCBP(image, 2, tx, mx));
2515             break;
2516         default:
2517             for (idx = 0; idx<image->num_channels; idx += 1) {
2518                 MACROBLK_CUR_HPCBP(image, idx, tx, mx)
2519                     = _jxr_PredCBP444(image, diff_cbp, idx, tx, mx, my);
2520                 DEBUG(" PredCBP: Predicted HPCBP[ch=%d]: 0x%04x\n",
2521                     idx, MACROBLK_CUR_HPCBP(image, idx, tx, mx));
2522             }
2523             break;
2524     }
2525 }
2526 
AdaptiveHPScan(jxr_image_t image,int hpinput_n[],int i,int value,int MBHPMode)2527 static void AdaptiveHPScan(jxr_image_t image, int hpinput_n[],
2528                            int i, int value, int MBHPMode)
2529 {
2530     assert(i > 0);
2531 
2532     if (MBHPMode == 1) {
2533         int k = image->hipass_ver_scanorder[i-1];
2534         image->hipass_ver_scantotals[i-1] += 1;
2535         assert(k < 16);
2536         hpinput_n[k] = value;
2537 
2538         if ((i>1) && (image->hipass_ver_scantotals[i-1] > image->hipass_ver_scantotals[i-2])) {
2539             SWAP(image->hipass_ver_scantotals[i-1], image->hipass_ver_scantotals[i-2]);
2540             SWAP(image->hipass_ver_scanorder[i-1], image->hipass_ver_scanorder[i-2]);
2541         }
2542     } else {
2543         int k = image->hipass_hor_scanorder[i-1];
2544         image->hipass_hor_scantotals[i-1] += 1;
2545         assert(k < 16);
2546         hpinput_n[k] = value;
2547 
2548         if ((i>1) && (image->hipass_hor_scantotals[i-1] > image->hipass_hor_scantotals[i-2])) {
2549             SWAP(image->hipass_hor_scantotals[i-1], image->hipass_hor_scantotals[i-2]);
2550             SWAP(image->hipass_hor_scanorder[i-1], image->hipass_hor_scanorder[i-2]);
2551         }
2552     }
2553 }
2554 
2555 /*
2556 * For each block within the macroblk, there are 15 HP values and the
2557 * DECODE_BLOCK_ADAPTIVE function is called to collect those values.
2558 */
r_DECODE_BLOCK_ADAPTIVE(jxr_image_t image,struct rbitstream * str,unsigned tx,unsigned mx,int cbp_flag,int chroma_flag,int channel,int block,int mbhp_pred_mode,unsigned model_bits)2559 static int r_DECODE_BLOCK_ADAPTIVE(jxr_image_t image, struct rbitstream*str,
2560                                    unsigned tx, unsigned mx,
2561                                    int cbp_flag, int chroma_flag,
2562                                    int channel, int block, int mbhp_pred_mode,
2563                                    unsigned model_bits)
2564 {
2565     int RLCoeffs[32] = {0};
2566 
2567     int num_nonzero = 0;
2568     if (cbp_flag) {
2569         int idx, k;
2570         int hpinput[16];
2571         for (k = 0 ; k < 16 ; k += 1)
2572             hpinput[k] = 0;
2573 
2574         num_nonzero = r_DECODE_BLOCK(image, str, chroma_flag, RLCoeffs, 2/*HP*/, 1);
2575 
2576         for (idx = 0, k = 1 ; idx < num_nonzero ; idx += 1) {
2577             assert(idx < 16);
2578             k += RLCoeffs[idx*2];
2579             if (k >= 16) {
2580                 DEBUG("ERROR: r_DECODE_BLOCK returned bogus RLCoeffs table. ch=%d, tx=%u, mx=%u, k=%d\n",
2581                     channel, tx, mx, k);
2582                 for (idx = 0 ; idx < num_nonzero ; idx += 1) {
2583                     DEBUG(" : RLCoeffs[%d] = %d\n", idx*2, RLCoeffs[idx*2]);
2584                     DEBUG(" : RLCoeffs[%d] = 0x%x\n", idx*2+1, RLCoeffs[idx*2+1]);
2585                 }
2586                 return JXR_EC_ERROR;
2587             }
2588             assert(k < 16);
2589             AdaptiveHPScan(image, hpinput, k, RLCoeffs[idx*2+1], mbhp_pred_mode);
2590             k += 1;
2591         }
2592 #if defined(DETAILED_DEBUG)
2593         {
2594             DEBUG(" HP val[tx=%u, mx=%d, block=%d] ==", tx, mx, block);
2595             for (k = 1 ; k<16; k+=1) {
2596                 DEBUG(" 0x%x", hpinput[k]);
2597             }
2598             DEBUG("\n");
2599             DEBUG(" adapted hor scan order (MBHPMode=%d) ==", mbhp_pred_mode);
2600             for (k = 0 ; k<15; k+=1) {
2601                 DEBUG(" %2d", image->hipass_hor_scanorder[k]);
2602             }
2603             DEBUG("\n");
2604             DEBUG(" adapted hor scan totals ==");
2605             for (k = 0 ; k<15; k+=1) {
2606                 DEBUG(" %2d", image->hipass_hor_scantotals[k]);
2607             }
2608             DEBUG("\n");
2609             DEBUG(" adapted ver scan order (MBHPMode=%d) ==", mbhp_pred_mode);
2610             for (k = 0 ; k<15; k+=1) {
2611                 DEBUG(" %2d", image->hipass_ver_scanorder[k]);
2612             }
2613             DEBUG("\n");
2614             DEBUG(" adapted ver scan totals ==");
2615             for (k = 0 ; k<15; k+=1) {
2616                 DEBUG(" %2d", image->hipass_ver_scantotals[k]);
2617             }
2618             DEBUG("\n");
2619         }
2620 #endif
2621         if (SKIP_HP_DATA(image)) {
2622             for (idx = 1; idx < 16; idx += 1)
2623                 MACROBLK_CUR_HP(image, channel, tx, mx, block, idx-1) = 0;
2624         } else {
2625             for (idx = 1; idx < 16; idx += 1)
2626                 MACROBLK_CUR_HP(image, channel, tx, mx, block, idx-1) = hpinput[idx] << model_bits;
2627         }
2628     }
2629 
2630     return num_nonzero;
2631 }
2632 
2633 
2634 
r_DECODE_FLEX(jxr_image_t image,struct rbitstream * str,unsigned tx,unsigned mx,int ch,unsigned block,unsigned k,unsigned flexbits)2635 static void r_DECODE_FLEX(jxr_image_t image, struct rbitstream*str,
2636                           unsigned tx, unsigned mx,
2637                           int ch, unsigned block, unsigned k,
2638                           unsigned flexbits)
2639 {
2640     /* NOTE: The model_bits shift was already applied, when the HP
2641     value was first parsed. */
2642     int coeff = MACROBLK_CUR_HP(image, ch, tx, mx, block, k);
2643 
2644     int flex_ref = _jxr_rbitstream_uintN(str, flexbits);
2645     DEBUG(" DECODE_FLEX: coeff=0x%08x, flex_ref=0x%08x\n", coeff, flex_ref);
2646     if (coeff > 0) {
2647         coeff += flex_ref << image->trim_flexbits;
2648     } else if (coeff < 0) {
2649         coeff -= flex_ref << image->trim_flexbits;
2650     } else {
2651         if (flex_ref != 0 && _jxr_rbitstream_uint1(str))
2652             coeff = (-flex_ref) << image->trim_flexbits;
2653         else
2654             coeff = (+flex_ref) << image->trim_flexbits;
2655     }
2656 
2657     if (! SKIP_FLEX_DATA(image))
2658         MACROBLK_CUR_HP(image, ch, tx, mx, block, k) = coeff;
2659 }
2660 
r_BLOCK_FLEXBITS(jxr_image_t image,struct rbitstream * str,unsigned tx,unsigned ty,unsigned mx,unsigned my,unsigned ch,unsigned bl,unsigned model_bits)2661 static void r_BLOCK_FLEXBITS(jxr_image_t image, struct rbitstream*str,
2662                              unsigned tx, unsigned ty,
2663                              unsigned mx, unsigned my,
2664                              unsigned ch, unsigned bl, unsigned model_bits)
2665 {
2666     const int transpose444 [16] = {0, 4, 8,12,
2667         1, 5, 9,13,
2668         2, 6,10,14,
2669         3, 7,11,15 };
2670     unsigned flexbits_left = model_bits;
2671     if (image->trim_flexbits > flexbits_left)
2672         flexbits_left = 0;
2673     else
2674         flexbits_left -= image->trim_flexbits;
2675 
2676     DEBUG(" BLOCK_FLEXBITS: flexbits_left=%u (model=%u, trim=%u) block=%u bitpos=%zu\n",
2677         flexbits_left, model_bits, image->trim_flexbits, bl, _jxr_rbitstream_bitpos(str));
2678     if (flexbits_left > 0) {
2679         int idx;
2680         for (idx = 1; idx < 16; idx += 1) {
2681             int idx_trans = transpose444[idx];
2682             r_DECODE_FLEX(image, str, tx, mx, ch, bl, idx_trans-1, flexbits_left);
2683         }
2684     }
2685     DEBUG(" BLOCK_FLEXBITS done\n");
2686 }
2687 
r_calculate_mbhp_mode(jxr_image_t image,int tx,int mx)2688 static int r_calculate_mbhp_mode(jxr_image_t image, int tx, int mx)
2689 {
2690     long strength_hor = 0;
2691     long strength_ver = 0;
2692     const long orientation_weight = 4;
2693 
2694     /* Add up the LP magnitudes along the top edge */
2695     strength_hor += abs(MACROBLK_CUR_LP(image, 0, tx, mx, 0));
2696     strength_hor += abs(MACROBLK_CUR_LP(image, 0, tx, mx, 1));
2697     strength_hor += abs(MACROBLK_CUR_LP(image, 0, tx, mx, 2));
2698 
2699     /* Add up the LP magnitudes along the left edge */
2700     strength_ver += abs(MACROBLK_CUR_LP(image, 0, tx, mx, 3));
2701     strength_ver += abs(MACROBLK_CUR_LP(image, 0, tx, mx, 7));
2702     strength_ver += abs(MACROBLK_CUR_LP(image, 0, tx, mx, 11));
2703 
2704     switch (image->use_clr_fmt) {
2705         case 0: /*YONLY*/
2706         case 6: /*NCOMPONENT*/
2707             break;
2708         case 3: /*YUV444*/
2709         case 4: /*YUVK */
2710             strength_hor += abs(MACROBLK_CUR_LP(image, 1, tx, mx, 0));
2711             strength_hor += abs(MACROBLK_CUR_LP(image, 2, tx, mx, 0));
2712             strength_ver += abs(MACROBLK_CUR_LP(image, 1, tx, mx, 3));
2713             strength_ver += abs(MACROBLK_CUR_LP(image, 2, tx, mx, 3));
2714             break;
2715         case 2: /*YUV422*/
2716             strength_hor += abs(MACROBLK_CUR_LP(image, 1, tx, mx, 0));
2717             strength_hor += abs(MACROBLK_CUR_LP(image, 2, tx, mx, 0));
2718             strength_ver += abs(MACROBLK_CUR_LP(image, 1, tx, mx, 1));
2719             strength_ver += abs(MACROBLK_CUR_LP(image, 2, tx, mx, 1));
2720             strength_hor += abs(MACROBLK_CUR_LP(image, 1, tx, mx, 4));
2721             strength_hor += abs(MACROBLK_CUR_LP(image, 2, tx, mx, 4));
2722             strength_ver += abs(MACROBLK_CUR_LP(image, 1, tx, mx, 5));
2723             strength_ver += abs(MACROBLK_CUR_LP(image, 2, tx, mx, 5));
2724             break;
2725         case 1: /*YUV420*/
2726             strength_hor += abs(MACROBLK_CUR_LP(image, 1, tx, mx, 0));
2727             strength_hor += abs(MACROBLK_CUR_LP(image, 2, tx, mx, 0));
2728             strength_ver += abs(MACROBLK_CUR_LP(image, 1, tx, mx, 1));
2729             strength_ver += abs(MACROBLK_CUR_LP(image, 2, tx, mx, 1));
2730             break;
2731         default:
2732             assert(0);
2733     }
2734 
2735     if (strength_hor * orientation_weight < strength_ver)
2736         return 0; /* predict from left */
2737     if (strength_ver * orientation_weight < strength_hor)
2738         return 1; /* predict from top */
2739 
2740     /* There is no strong weight from top or left, so do not
2741     bother with prediction. */
2742     return 2;
2743 }
2744 
2745 
2746 /*
2747 * This function does HP coefficient propagation within a completed
2748 * macroblock.
2749 */
_jxr_propagate_hp_predictions(jxr_image_t image,int ch,unsigned tx,unsigned mx,int mbhp_pred_mode)2750 void _jxr_propagate_hp_predictions(jxr_image_t image, int ch, unsigned tx, unsigned mx,
2751                                    int mbhp_pred_mode)
2752 {
2753     if (mbhp_pred_mode == 0) { /* Prediction left to right */
2754         int idx;
2755         for (idx = 1 ; idx < 16 ; idx += 1) {
2756             if (idx%4 == 0)
2757                 continue;
2758             CHECK3(image->lwf_test, MACROBLK_CUR_HP(image,ch,tx,mx,idx, 3), MACROBLK_CUR_HP(image,ch,tx,mx,idx, 7), MACROBLK_CUR_HP(image,ch,tx,mx,idx, 11));
2759             MACROBLK_CUR_HP(image,ch,tx,mx,idx, 3) += MACROBLK_CUR_HP(image,ch,tx,mx,idx-1, 3);
2760             MACROBLK_CUR_HP(image,ch,tx,mx,idx, 7) += MACROBLK_CUR_HP(image,ch,tx,mx,idx-1, 7);
2761             MACROBLK_CUR_HP(image,ch,tx,mx,idx,11) += MACROBLK_CUR_HP(image,ch,tx,mx,idx-1,11);
2762 
2763 #if defined(DETAILED_DEBUG)
2764             {
2765                 int k;
2766                 DEBUG(" HP val predicted(l)[ch=%d, tx=%u, mx=%d, block=%d] ==", ch, tx, mx, idx);
2767                 for (k = 1 ; k<16; k+=1) {
2768                     DEBUG(" 0x%x", MACROBLK_CUR_HP(image,ch,tx,mx,idx,k-1));
2769                 }
2770                 DEBUG("\n");
2771             }
2772 #endif
2773         }
2774     } else if (mbhp_pred_mode == 1) { /* Prediction top to bottom. */
2775         int idx;
2776         for (idx = 4 ; idx < 16 ; idx += 1) {
2777             CHECK3(image->lwf_test, MACROBLK_CUR_HP(image,ch,tx,mx,idx, 0), MACROBLK_CUR_HP(image,ch,tx,mx,idx, 1), MACROBLK_CUR_HP(image,ch,tx,mx,idx, 2));
2778             MACROBLK_CUR_HP(image,ch,tx,mx,idx,0) += MACROBLK_CUR_HP(image,ch,tx,mx, idx-4,0);
2779             MACROBLK_CUR_HP(image,ch,tx,mx,idx,1) += MACROBLK_CUR_HP(image,ch,tx,mx, idx-4,1);
2780             MACROBLK_CUR_HP(image,ch,tx,mx,idx,2) += MACROBLK_CUR_HP(image,ch,tx,mx, idx-4,2);
2781 #if defined(DETAILED_DEBUG)
2782             {
2783                 int k;
2784                 DEBUG(" HP val predicted(t)[ch=%d, tx=%u, mx=%d, block=%d] ==", ch, tx, mx, idx);
2785                 for (k = 1 ; k<16; k+=1) {
2786                     DEBUG(" 0x%x", MACROBLK_CUR_HP(image,ch,tx,mx,idx,k-1));
2787                 }
2788                 DEBUG("\n");
2789             }
2790 #endif
2791         }
2792     }
2793 
2794     switch (image->use_clr_fmt) {
2795         case 1:/*YUV420*/
2796             assert(ch == 0);
2797             if (mbhp_pred_mode == 0) {
2798                 int idx;
2799                 for (idx = 1 ; idx <= 3 ; idx += 2) {
2800                     CHECK3(image->lwf_test, MACROBLK_CUR_HP(image,1,tx,mx,idx, 3), MACROBLK_CUR_HP(image,1,tx,mx,idx, 7), MACROBLK_CUR_HP(image,1,tx,mx,idx, 11));
2801                     CHECK3(image->lwf_test, MACROBLK_CUR_HP(image,2,tx,mx,idx, 3), MACROBLK_CUR_HP(image,2,tx,mx,idx, 7), MACROBLK_CUR_HP(image,2,tx,mx,idx, 11));
2802                     MACROBLK_CUR_HP(image,1,tx,mx,idx,3) += MACROBLK_CUR_HP(image,1,tx,mx,idx-1,3);
2803                     MACROBLK_CUR_HP(image,2,tx,mx,idx,3) += MACROBLK_CUR_HP(image,2,tx,mx,idx-1,3);
2804                     MACROBLK_CUR_HP(image,1,tx,mx,idx,7) += MACROBLK_CUR_HP(image,1,tx,mx,idx-1,7);
2805                     MACROBLK_CUR_HP(image,2,tx,mx,idx,7) += MACROBLK_CUR_HP(image,2,tx,mx,idx-1,7);
2806                     MACROBLK_CUR_HP(image,1,tx,mx,idx,11)+= MACROBLK_CUR_HP(image,1,tx,mx,idx-1,11);
2807                     MACROBLK_CUR_HP(image,2,tx,mx,idx,11)+= MACROBLK_CUR_HP(image,2,tx,mx,idx-1,11);
2808 #if defined(DETAILED_DEBUG)
2809                     int k;
2810                     DEBUG(" HP val predicted(l)[ch=1, tx=%u, mx=%d, block=%d] ==", tx, mx, idx);
2811                     for (k = 1 ; k<16; k+=1) {
2812                         DEBUG(" 0x%x", MACROBLK_CUR_HP(image,1,tx,mx,idx,k-1));
2813                     }
2814                     DEBUG("\n");
2815                     DEBUG(" HP val predicted(l)[ch=2, tx=%u, mx=%d, block=%d] ==", tx, mx, idx);
2816                     for (k = 1 ; k<16; k+=1) {
2817                         DEBUG(" 0x%x", MACROBLK_CUR_HP(image,2,tx,mx,idx,k-1));
2818                     }
2819                     DEBUG("\n");
2820 #endif
2821                 }
2822             } else if (mbhp_pred_mode == 1) {
2823                 int idx;
2824                 for (idx = 2; idx <= 3 ; idx += 1) {
2825                     CHECK3(image->lwf_test, MACROBLK_CUR_HP(image,1,tx,mx,idx, 0), MACROBLK_CUR_HP(image,1,tx,mx,idx, 1), MACROBLK_CUR_HP(image,1,tx,mx,idx, 2));
2826                     CHECK3(image->lwf_test, MACROBLK_CUR_HP(image,2,tx,mx,idx, 0), MACROBLK_CUR_HP(image,2,tx,mx,idx, 1), MACROBLK_CUR_HP(image,2,tx,mx,idx, 2));
2827                     MACROBLK_CUR_HP(image,1,tx,mx,idx,0) += MACROBLK_CUR_HP(image,1,tx,mx,idx-2,0);
2828                     MACROBLK_CUR_HP(image,2,tx,mx,idx,0) += MACROBLK_CUR_HP(image,2,tx,mx,idx-2,0);
2829                     MACROBLK_CUR_HP(image,1,tx,mx,idx,1) += MACROBLK_CUR_HP(image,1,tx,mx,idx-2,1);
2830                     MACROBLK_CUR_HP(image,2,tx,mx,idx,1) += MACROBLK_CUR_HP(image,2,tx,mx,idx-2,1);
2831                     MACROBLK_CUR_HP(image,1,tx,mx,idx,2) += MACROBLK_CUR_HP(image,1,tx,mx,idx-2,2);
2832                     MACROBLK_CUR_HP(image,2,tx,mx,idx,2) += MACROBLK_CUR_HP(image,2,tx,mx,idx-2,2);
2833 #if defined(DETAILED_DEBUG)
2834                     int k;
2835                     DEBUG(" HP val predicted(t)[ch=1, tx=%u, mx=%d, block=%d] ==", tx, mx, idx);
2836                     for (k = 1 ; k<16; k+=1) {
2837                         DEBUG(" 0x%x", MACROBLK_CUR_HP(image,1,tx,mx,idx,k-1));
2838                     }
2839                     DEBUG("\n");
2840                     DEBUG(" HP val predicted(t)[ch=2, tx=%u, mx=%d, block=%d] ==", tx, mx, idx);
2841                     for (k = 1 ; k<16; k+=1) {
2842                         DEBUG(" 0x%x", MACROBLK_CUR_HP(image,2,tx,mx,idx,k-1));
2843                     }
2844                     DEBUG("\n");
2845 #endif
2846                 }
2847             }
2848             break;
2849 
2850         case 2:/*YUV422*/
2851             assert(ch == 0);
2852             if (mbhp_pred_mode == 0) {
2853                 int idx;
2854                 for (idx = 1 ; idx <= 7 ; idx += 2) {
2855                     CHECK3(image->lwf_test, MACROBLK_CUR_HP(image,1,tx,mx,idx, 3), MACROBLK_CUR_HP(image,1,tx,mx,idx, 7), MACROBLK_CUR_HP(image,1,tx,mx,idx, 11));
2856                     CHECK3(image->lwf_test, MACROBLK_CUR_HP(image,2,tx,mx,idx, 3), MACROBLK_CUR_HP(image,2,tx,mx,idx, 7), MACROBLK_CUR_HP(image,2,tx,mx,idx, 11));
2857                     MACROBLK_CUR_HP(image,1,tx,mx,idx,3) += MACROBLK_CUR_HP(image,1,tx,mx,idx-1,3);
2858                     MACROBLK_CUR_HP(image,2,tx,mx,idx,3) += MACROBLK_CUR_HP(image,2,tx,mx,idx-1,3);
2859                     MACROBLK_CUR_HP(image,1,tx,mx,idx,7) += MACROBLK_CUR_HP(image,1,tx,mx,idx-1,7);
2860                     MACROBLK_CUR_HP(image,2,tx,mx,idx,7) += MACROBLK_CUR_HP(image,2,tx,mx,idx-1,7);
2861                     MACROBLK_CUR_HP(image,1,tx,mx,idx,11)+= MACROBLK_CUR_HP(image,1,tx,mx,idx-1,11);
2862                     MACROBLK_CUR_HP(image,2,tx,mx,idx,11)+= MACROBLK_CUR_HP(image,2,tx,mx,idx-1,11);
2863 #if defined(DETAILED_DEBUG)
2864                     int k;
2865                     DEBUG(" HP val predicted(l)[ch=1, tx=%u, mx=%d, block=%d] ==", tx, mx, idx);
2866                     for (k = 1 ; k<16; k+=1) {
2867                         DEBUG(" 0x%x", MACROBLK_CUR_HP(image,1,tx,mx,idx,k-1));
2868                     }
2869                     DEBUG("\n");
2870                     DEBUG(" HP val predicted(l)[ch=2, tx=%u, mx=%d, block=%d] ==", tx, mx, idx);
2871                     for (k = 1 ; k<16; k+=1) {
2872                         DEBUG(" 0x%x", MACROBLK_CUR_HP(image,2,tx,mx,idx,k-1));
2873                     }
2874                     DEBUG("\n");
2875 #endif
2876                 }
2877             } else if (mbhp_pred_mode == 1) {
2878                 int idx;
2879                 for (idx = 2; idx <= 7 ; idx += 1) {
2880                     CHECK3(image->lwf_test, MACROBLK_CUR_HP(image,1,tx,mx,idx, 0), MACROBLK_CUR_HP(image,1,tx,mx,idx, 1), MACROBLK_CUR_HP(image,1,tx,mx,idx, 2));
2881                     CHECK3(image->lwf_test, MACROBLK_CUR_HP(image,2,tx,mx,idx, 0), MACROBLK_CUR_HP(image,2,tx,mx,idx, 1), MACROBLK_CUR_HP(image,2,tx,mx,idx, 2));
2882                     MACROBLK_CUR_HP(image,1,tx,mx,idx,0) += MACROBLK_CUR_HP(image,1,tx,mx,idx-2,0);
2883                     MACROBLK_CUR_HP(image,2,tx,mx,idx,0) += MACROBLK_CUR_HP(image,2,tx,mx,idx-2,0);
2884                     MACROBLK_CUR_HP(image,1,tx,mx,idx,1) += MACROBLK_CUR_HP(image,1,tx,mx,idx-2,1);
2885                     MACROBLK_CUR_HP(image,2,tx,mx,idx,1) += MACROBLK_CUR_HP(image,2,tx,mx,idx-2,1);
2886                     MACROBLK_CUR_HP(image,1,tx,mx,idx,2) += MACROBLK_CUR_HP(image,1,tx,mx,idx-2,2);
2887                     MACROBLK_CUR_HP(image,2,tx,mx,idx,2) += MACROBLK_CUR_HP(image,2,tx,mx,idx-2,2);
2888 #if defined(DETAILED_DEBUG)
2889                     int k;
2890                     DEBUG(" HP val predicted(t)[ch=1, tx=%u, mx=%d, block=%d] ==", tx, mx, idx);
2891                     for (k = 1 ; k<16; k+=1) {
2892                         DEBUG(" 0x%x", MACROBLK_CUR_HP(image,1,tx,mx,idx,k-1));
2893                     }
2894                     DEBUG("\n");
2895                     DEBUG(" HP val predicted(t)[ch=2, tx=%u, mx=%d, block=%d] ==", tx, mx, idx);
2896                     for (k = 1 ; k<16; k+=1) {
2897                         DEBUG(" 0x%x", MACROBLK_CUR_HP(image,2,tx,mx,idx,k-1));
2898                     }
2899                     DEBUG("\n");
2900 #endif
2901                 }
2902             }
2903             break;
2904 
2905         default:
2906             break;
2907     }
2908 }
2909 
2910 
2911 /*
2912 * Code IS_DC_YUV
2913 * 10 0
2914 * 001 1
2915 * 00001 2
2916 * 0001 3
2917 * 11 4
2918 * 010 5
2919 * 00000 6
2920 * 011 7
2921 */
get_is_dc_yuv(struct rbitstream * str)2922 static int get_is_dc_yuv(struct rbitstream*str)
2923 {
2924     if (_jxr_rbitstream_uint1(str) == 1) { /* 1... */
2925         if (_jxr_rbitstream_uint1(str) == 1) /* 11 */
2926             return 4;
2927         else /* 10 */
2928             return 0;
2929     }
2930     else {
2931         switch (_jxr_rbitstream_uint2(str)) { /* 1... */
2932             case 0: /* 000... */
2933                 if (_jxr_rbitstream_uint1(str) == 1) /* 0001 */
2934                     return 3;
2935                 else if (_jxr_rbitstream_uint1(str) == 1) /* 00001 */
2936                     return 2;
2937                 else /* 00000 */
2938                     return 6;
2939             case 1: /* 001 */
2940                 return 1;
2941 
2942             case 2: /* 010 */
2943                 return 5;
2944 
2945             case 3: /* 011 */
2946                 return 7;
2947         }
2948     }
2949 
2950     assert(0); /* Should not get here. */
2951     return -1;
2952 }
2953 
2954 /*
2955 * table0 table1 value
2956 * 1 1 0
2957 * 01 000 1
2958 * 001 001 2
2959 * 0000 010 3
2960 * 0001 011 4
2961 */
get_num_cbp(struct rbitstream * str,struct adaptive_vlc_s * vlc)2962 static int get_num_cbp(struct rbitstream*str, struct adaptive_vlc_s*vlc)
2963 {
2964     assert(vlc->table < 2);
2965 
2966     if (_jxr_rbitstream_uint1(str) == 1)
2967         return 0;
2968 
2969     if (vlc->table == 0) {
2970         if (_jxr_rbitstream_uint1(str) == 1)
2971             return 1;
2972         if (_jxr_rbitstream_uint1(str) == 1)
2973             return 2;
2974         if (_jxr_rbitstream_uint1(str) == 1)
2975             return 4;
2976         else
2977             return 3;
2978     } else {
2979         uint8_t tmp = _jxr_rbitstream_uint2(str);
2980         return tmp + 1;
2981     }
2982 }
2983 
get_num_blkcbp(jxr_image_t image,struct rbitstream * str,struct adaptive_vlc_s * vlc)2984 static int get_num_blkcbp(jxr_image_t image, struct rbitstream*str,
2985 struct adaptive_vlc_s*vlc)
2986 {
2987     switch (image->use_clr_fmt) {
2988         case 0: /*YONLY*/
2989         case 4: /*YUVK*/
2990         case 6: /*NCOMPONENT*/
2991             /*
2992             * table0 table1 value
2993             * 1 1 0
2994             * 01 000 1
2995             * 001 001 2
2996             * 0000 010 3
2997             * 0001 011 4
2998             *
2999             * NOTE that this is exactly the same table as for
3000             * NUM_CBP above.
3001             */
3002             return get_num_cbp(str, vlc);
3003 
3004         default:
3005             /*
3006             * table0 table1 value
3007             * 010 1 0
3008             * 00000 001 1
3009             * 0010 010 2
3010             * 00001 0001 3
3011             * 00010 000001 4
3012             * 1 011 5
3013             * 011 00001 6
3014             * 00011 0000000 7
3015             * 0011 0000001 8
3016             */
3017             if (vlc->table == 0) {
3018                 static const unsigned char codeb[32] = {
3019                     5, 5, 5, 5, 4, 4, 4, 4,
3020                     3, 3, 3, 3, 3, 3, 3, 3,
3021                     1, 1, 1, 1, 1, 1, 1, 1, /* 1xxxx */
3022                     1, 1, 1, 1, 1, 1, 1, 1
3023                 };
3024                 static const signed char codev[32] = {
3025                     1, 3, 4, 7, 2, 2, 8, 8,
3026                     0, 0, 0, 0, 6, 6, 6, 6,
3027                     5, 5, 5, 5, 5, 5, 5, 5,
3028                     5, 5, 5, 5, 5, 5, 5, 5
3029                 };
3030                 return _jxr_rbitstream_intE(str, 5, codeb, codev);
3031             } else {
3032                 static const unsigned char codeb[64] = {
3033                     6, 6, 5, 5, 4, 4, 4, 4,
3034                     3, 3, 3, 3, 3, 3, 3, 3,
3035                     3, 3, 3, 3, 3, 3, 3, 3,
3036                     3, 3, 3, 3, 3, 3, 3, 3,
3037                     1, 1, 1, 1, 1, 1, 1, 1,
3038                     1, 1, 1, 1, 1, 1, 1, 1,
3039                     1, 1, 1, 1, 1, 1, 1, 1,
3040                     1, 1, 1, 1, 1, 1, 1, 1
3041                 };
3042                 static const signed char codev[64] = {
3043                     7, 4, 6, 6, 3, 3, 3, 3,
3044                     1, 1, 1, 1, 1, 1, 1, 1,
3045                     2, 2, 2, 2, 2, 2, 2, 2,
3046                     5, 5, 5, 5, 5, 5, 5, 5,
3047                     0, 0, 0, 0, 0, 0, 0, 0,
3048                     0, 0, 0, 0, 0, 0, 0, 0,
3049                     0, 0, 0, 0, 0, 0, 0, 0,
3050                     0, 0, 0, 0, 0, 0, 0, 0
3051                 };
3052                 int tmp = _jxr_rbitstream_intE(str, 6, codeb, codev);
3053                 if (tmp == 7) tmp += _jxr_rbitstream_uint1(str);
3054                 return tmp;
3055             }
3056     }
3057 }
3058 
3059 
3060 
3061 /*
3062 * value table0 table1
3063 * 0 01 1
3064 * 1 10 01
3065 * 2 11 001
3066 * 3 001 0001
3067 * 4 0001 00001
3068 * 5 00000 000000
3069 * 6 00001 000001
3070 */
3071 static const unsigned char abslevel_code0b[64] = {
3072     5, 5, 5, 5, 4, 4, 4, 4, /* 00000x, 00001x, 0001xx */
3073     3, 3, 3, 3, 3, 3, 3, 3, /* 001xxx */
3074     2, 2, 2, 2, 2, 2, 2, 2, /* 01xxxx */
3075     2, 2, 2, 2, 2, 2, 2, 2,
3076     2, 2, 2, 2, 2, 2, 2, 2, /* 10xxxx */
3077     2, 2, 2, 2, 2, 2, 2, 2,
3078     2, 2, 2, 2, 2, 2, 2, 2, /* 11xxxx */
3079     2, 2, 2, 2, 2, 2, 2, 2
3080 };
3081 static const signed char abslevel_code0v[64] = {
3082     5, 5, 6, 6, 4, 4, 4, 4,
3083     3, 3, 3, 3, 3, 3, 3, 3,
3084     0, 0, 0, 0, 0, 0, 0, 0,
3085     0, 0, 0, 0, 0, 0, 0, 0,
3086     1, 1, 1, 1, 1, 1, 1, 1,
3087     1, 1, 1, 1, 1, 1, 1, 1,
3088     2, 2, 2, 2, 2, 2, 2, 2,
3089     2, 2, 2, 2, 2, 2, 2, 2
3090 };
3091 
3092 static const unsigned char abslevel_code1b[64] = {
3093     6, 6, 5, 5, 4, 4, 4, 4, /* 000000, 000001, 00001x, 0001xx */
3094     3, 3, 3, 3, 3, 3, 3, 3, /* 001xxx */
3095     2, 2, 2, 2, 2, 2, 2, 2, /* 01xxxx */
3096     2, 2, 2, 2, 2, 2, 2, 2,
3097     1, 1, 1, 1, 1, 1, 1, 1, /* 1xxxxx */
3098     1, 1, 1, 1, 1, 1, 1, 1,
3099     1, 1, 1, 1, 1, 1, 1, 1,
3100     1, 1, 1, 1, 1, 1, 1, 1
3101 };
3102 static const signed char abslevel_code1v[64] = {
3103     5, 6, 4, 4, 3, 3, 3, 3,
3104     2, 2, 2, 2, 2, 2, 2, 2,
3105     1, 1, 1, 1, 1, 1, 1, 1,
3106     1, 1, 1, 1, 1, 1, 1, 1,
3107     0, 0, 0, 0, 0, 0, 0, 0,
3108     0, 0, 0, 0, 0, 0, 0, 0,
3109     0, 0, 0, 0, 0, 0, 0, 0,
3110     0, 0, 0, 0, 0, 0, 0, 0
3111 };
3112 
dec_abslevel_index(jxr_image_t image,struct rbitstream * str,int vlc_select)3113 static int dec_abslevel_index(jxr_image_t image, struct rbitstream*str, int vlc_select)
3114 {
3115     const unsigned char*codeb = image->vlc_table[vlc_select].table? abslevel_code1b :abslevel_code0b;
3116     const signed char*codev = image->vlc_table[vlc_select].table? abslevel_code1v : abslevel_code0v;
3117 
3118     return _jxr_rbitstream_intE(str, 6, codeb, codev);
3119 }
3120 
dec_cbp_yuv_lp1(jxr_image_t image,struct rbitstream * str)3121 static int dec_cbp_yuv_lp1(jxr_image_t image, struct rbitstream*str)
3122 {
3123     static const unsigned char codeb[16] = {
3124         1, 1, 1, 1,
3125         1, 1, 1, 1,
3126         3, 3, 4, 4,
3127         4, 4, 4, 4 };
3128         static const signed char codev[16] = {
3129             0, 0, 0, 0,
3130             0, 0, 0, 0,
3131             1, 1, 2, 3,
3132             4, 5, 6, 7 };
3133 
3134             switch (image->use_clr_fmt) {
3135                 case 3: /* YUV444 */
3136                     return _jxr_rbitstream_intE(str, 4, codeb, codev);
3137                 case 1: /* YUV420 */
3138                 case 2: /* YUV422 */
3139                     if (_jxr_rbitstream_uint1(str) == 0) {
3140                         return 0;
3141 
3142                     } else if (_jxr_rbitstream_uint1(str) == 0) {
3143                         return 1;
3144 
3145                     } else if (_jxr_rbitstream_uint1(str) == 0) {
3146                         return 2;
3147 
3148                     } else {
3149                         return 3;
3150                     }
3151                 default:
3152                     assert(0);
3153                     return 0;
3154             }
3155 }
3156 
3157 /*
3158 * Bits Value
3159 * 1 0
3160 * 01 1
3161 * 00 2
3162 */
get_value_012(struct rbitstream * str)3163 static int get_value_012(struct rbitstream*str)
3164 {
3165     if (_jxr_rbitstream_uint1(str) == 1)
3166         return 0;
3167     else if ( _jxr_rbitstream_uint1(str) == 1)
3168         return 1;
3169     else
3170         return 2;
3171 }
3172 
3173 /*
3174 * Bits Value
3175 * 1 0
3176 * 01 1
3177 * 000 2
3178 * 001 3
3179 */
get_num_ch_blk(struct rbitstream * str)3180 static int get_num_ch_blk(struct rbitstream*str)
3181 {
3182     if (_jxr_rbitstream_uint1(str) == 1)
3183         return 0;
3184     if (_jxr_rbitstream_uint1(str) == 1)
3185         return 1;
3186     if (_jxr_rbitstream_uint1(str) == 1)
3187         return 3;
3188     else
3189         return 2;
3190 }
3191 
3192 /*
3193 * $Log: r_parse.c,v $
3194 * Revision 1.41 2009/05/29 12:00:00 microsoft
3195 * Reference Software v1.6 updates.
3196 *
3197 * Revision 1.40 2009/04/13 12:00:00 microsoft
3198 * Reference Software v1.5 updates.
3199 *
3200 * Revision 1.39 2008/03/24 18:06:56 steve
3201 * Imrpove DEBUG messages around quantization.
3202 *
3203 * Revision 1.38 2008/03/21 18:30:21 steve
3204 * Get HP Prediction right for YUVK (CMYK)
3205 *
3206 * Revision 1.37 2008/03/20 22:39:41 steve
3207 * Fix various debug prints of QP data.
3208 *
3209 * Revision 1.36 2008/03/17 21:48:55 steve
3210 * CMYK decode support
3211 *
3212 * Revision 1.35 2008/03/13 21:23:27 steve
3213 * Add pipeline step for YUV420.
3214 *
3215 * Revision 1.34 2008/03/13 00:07:22 steve
3216 * Encode HP of YUV422
3217 *
3218 * Revision 1.33 2008/03/06 22:47:39 steve
3219 * Clean up parsing/encoding of QP counts
3220 *
3221 * Revision 1.32 2008/03/06 02:05:48 steve
3222 * Distributed quantization
3223 *
3224 * Revision 1.31 2008/03/05 04:04:30 steve
3225 * Clarify constraints on USE_DC_QP in image plane header.
3226 *
3227 * Revision 1.30 2008/03/05 00:31:17 steve
3228 * Handle UNIFORM/IMAGEPLANE_UNIFORM compression.
3229 *
3230 * Revision 1.29 2008/02/28 18:50:31 steve
3231 * Portability fixes.
3232 *
3233 * Revision 1.28 2008/02/26 23:52:44 steve
3234 * Remove ident for MS compilers.
3235 *
3236 * Revision 1.27 2008/02/23 01:55:51 steve
3237 * CBP REFINE is more complex when CHR is involved.
3238 *
3239 * Revision 1.26 2008/02/22 23:01:33 steve
3240 * Compress macroblock HP CBP packets.
3241 *
3242 * Revision 1.25 2008/01/01 01:07:26 steve
3243 * Add missing HP prediction.
3244 *
3245 * Revision 1.24 2007/12/30 00:16:00 steve
3246 * Add encoding of HP values.
3247 *
3248 * Revision 1.23 2007/12/13 18:01:09 steve
3249 * Stubs for HP encoding.
3250 *
3251 * Revision 1.22 2007/12/12 00:37:33 steve
3252 * Cleanup some debug messages.
3253 *
3254 * Revision 1.21 2007/12/06 23:12:41 steve
3255 * Stubs for LP encode operations.
3256 *
3257 * Revision 1.20 2007/12/06 17:54:09 steve
3258 * UpdateModelMB dump details.
3259 *
3260 * Revision 1.19 2007/11/26 01:47:15 steve
3261 * Add copyright notices per MS request.
3262 *
3263 * Revision 1.18 2007/11/21 00:34:30 steve
3264 * Rework spatial mode tile macroblock shuffling.
3265 *
3266 * Revision 1.17 2007/11/20 00:05:47 steve
3267 * Complex handling of mbhp_pred_mode in frequency dmoe.
3268 *
3269 * Revision 1.16 2007/11/19 18:22:34 steve
3270 * Skip ESCaped FLEXBITS tiles.
3271 *
3272 * Revision 1.15 2007/11/16 20:03:57 steve
3273 * Store MB Quant, not qp_index.
3274 *
3275 * Revision 1.14 2007/11/16 17:33:24 steve
3276 * Do HP prediction after FLEXBITS frequency tiles.
3277 *
3278 * Revision 1.13 2007/11/16 00:29:05 steve
3279 * Support FREQUENCY mode HP and FLEXBITS
3280 *
3281 * Revision 1.12 2007/11/14 23:56:17 steve
3282 * Fix TILE ordering, using seeks, for FREQUENCY mode.
3283 *
3284 * Revision 1.11 2007/11/14 00:17:26 steve
3285 * Fix parsing of QP indices.
3286 *
3287 * Revision 1.10 2007/11/13 03:27:23 steve
3288 * Add Frequency mode LP support.
3289 *
3290 * Revision 1.9 2007/11/12 23:21:55 steve
3291 * Infrastructure for frequency mode ordering.
3292 *
3293 * Revision 1.8 2007/11/08 19:38:38 steve
3294 * Get stub DCONLY compression to work.
3295 *
3296 * Revision 1.7 2007/11/01 21:09:40 steve
3297 * Multiple rows of tiles.
3298 *
3299 * Revision 1.6 2007/10/30 21:32:46 steve
3300 * Support for multiple tile columns.
3301 *
3302 * Revision 1.5 2007/09/08 01:01:43 steve
3303 * YUV444 color parses properly.
3304 *
3305 * Revision 1.4 2007/07/30 23:09:57 steve
3306 * Interleave FLEXBITS within HP block.
3307 *
3308 * Revision 1.3 2007/07/24 20:56:28 steve
3309 * Fix HP prediction and model bits calculations.
3310 *
3311 * Revision 1.2 2007/06/07 18:53:06 steve
3312 * Parse HP coeffs that are all 0.
3313 *
3314 * Revision 1.1 2007/06/06 17:19:12 steve
3315 * Introduce to CVS.
3316 *
3317 */
3318 
3319