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: w_emit.c,v 1.25 2008/03/24 18:06:56 steve Exp $")
44 #else
45 #ident "$Id: w_emit.c,v 1.25 2008/03/24 18:06:56 steve Exp $"
46 #endif
47 
48 # include "jxr_priv.h"
49 # include <stdlib.h>
50 # include <assert.h>
51 
52 void initialize_index_table(jxr_image_t image);
53 
54 static int w_image_header(jxr_image_t image, struct wbitstream*str);
55 static int w_image_plane_header(jxr_image_t image, struct wbitstream*str, int alpha);
56 static void w_INDEX_TABLE(jxr_image_t image, struct wbitstream*str);
57 static uint64_t w_PROFILE_LEVEL_INFO(jxr_image_t image, struct wbitstream*str, uint64_t bytes);
58 static void w_TILE(jxr_image_t image, struct wbitstream*str);
59 
60 static int short_header_ok(jxr_image_t image);
61 static int need_windowing_flag(jxr_image_t image);
62 static int need_trim_flexbits_flag(jxr_image_t image);
63 
64 
65 static void w_MB_FLEXBITS(jxr_image_t image, struct wbitstream*str,
66                           int alpha_flag,
67                           unsigned tx, unsigned ty,
68                           unsigned mx, unsigned my);
69 static void w_BLOCK_FLEXBITS(jxr_image_t image, struct wbitstream*str,
70                              unsigned tx, unsigned ty,
71                              unsigned mx, unsigned my,
72                              unsigned ch, unsigned bl, unsigned model_bits);
73 static void w_DEC_DC(jxr_image_t image, struct wbitstream*str,
74                      int model_bits, int chroma_flag, int is_dc_ch,
75                      int32_t dc_val);
76 static void w_DECODE_ABS_LEVEL(jxr_image_t image, struct wbitstream*str,
77                                int band, int chroma_flag, uint32_t level);
78 static void w_DECODE_BLOCK(jxr_image_t image, struct wbitstream*str, int band, int chroma_flag,
79                            const int RLCoeffs[32], int num_non_zero);
80 static void w_DECODE_FIRST_INDEX(jxr_image_t image, struct wbitstream*str,
81                                  int chroma_flag, int band, int index_code);
82 static void w_DECODE_INDEX(jxr_image_t image, struct wbitstream*str,
83                            int location, int chroma_flag, int band, int context,
84                            int index_code);
85 static void w_DECODE_RUN(jxr_image_t image, struct wbitstream*str, int max_run, int run);
86 static int w_DECODE_BLOCK_ADAPTIVE(jxr_image_t image, struct wbitstream*str,
87                                    unsigned tx, unsigned mx,
88                                    int cbp_flag, int chroma_flag,
89                                    int channel, int block, int mbhp_pred_mode,
90                                    unsigned model_bits);
91 static void w_REFINE_CBP(jxr_image_t image, struct wbitstream*str, int cbp_block_mask);
92 static void w_REFINE_CBP_CHR(jxr_image_t image, struct wbitstream*str, int cbp_block_mask);
93 static void refine_cbp_chr422(jxr_image_t image, struct wbitstream*str, int diff_cbp, int block);
94 
95 
96 static const int transpose420[4] = {0, 2,
97 1, 3 };
98 static const int transpose422[8] = {0, 2, 1, 3, 4, 6, 5, 7};
99 
100 
initialize_index_table(jxr_image_t image)101 void initialize_index_table(jxr_image_t image)
102 {
103     int num_index_table_entries;
104 
105     if (FREQUENCY_MODE_CODESTREAM_FLAG(image) == 0 /* SPATIAL MODE */) {
106         num_index_table_entries = image->tile_columns * image->tile_rows;
107     }
108     else
109     {
110         num_index_table_entries = image->tile_columns * image->tile_rows;
111         switch (image->bands_present_of_primary) {
112             case 4: /* ISOLATED */
113                 num_index_table_entries *= 4;
114                 break;
115             default:
116                 num_index_table_entries *= (4 - image->bands_present_of_primary);
117                 break;
118         }
119     }
120     image->tile_index_table_length = num_index_table_entries;
121 
122     assert(image->tile_index_table == 0);
123     image->tile_index_table = (int64_t*)calloc(num_index_table_entries, sizeof(int64_t));
124     DEBUG(" INDEX_TABLE has %d table entries\n", num_index_table_entries);
125 }
126 
fill_in_image_defaults(jxr_image_t image)127 static int fill_in_image_defaults(jxr_image_t image)
128 {
129     if (image->tile_columns == 0)
130         image->tile_columns = 1;
131     if (image->tile_rows == 0)
132         image->tile_rows = 1;
133 
134     if (image->tile_columns > 1 || image->tile_rows > 1)
135         image->header_flags1 |= 0x80; /* TILING FLAG */
136 
137     if (short_header_ok(image))
138         image->header_flags2 |= 0x80; /* SHORT_HEADER FLAG */
139     else
140         image->header_flags2 &= ~0x80;
141 
142     if (need_windowing_flag(image))
143         image->header_flags2 |= 0x20; /* WINDOWING_FLAG */
144 
145     image->window_extra_bottom = 15 - ((image->height1 + image->window_extra_top) % 16);
146     image->extended_height = image->height1 + 1 + image->window_extra_top + image->window_extra_bottom;
147     image->window_extra_right = 15 - ((image->width1 + image->window_extra_left) % 16);
148     image->extended_width = image->width1 + 1 + image->window_extra_left + image->window_extra_right;
149 
150     if (need_trim_flexbits_flag(image))
151         image->header_flags2 |= 0x10; /* TRIM_FLEXBITS_FLAG */
152     else
153         image->header_flags2 &= ~0x10;
154 
155     /* Test OUTPUT_CLR_FMT against size requirements */
156     switch(image->output_clr_fmt) {
157         case JXR_OCF_YUV420: /* YUV420 */
158             assert(image->height1 & 0x1);
159             assert((image->window_extra_top & 0x1) == 0);
160             assert((image->window_extra_bottom & 0x1) == 0);
161         case JXR_OCF_YUV422: /* YUV422 */
162             assert(image->width1 & 0x1);
163             assert((image->window_extra_left & 0x1) == 0);
164             assert((image->window_extra_right & 0x1) == 0);
165             break;
166     }
167 
168     /* Force scaling ON if we are using a subsampled color format. */
169     switch (image->use_clr_fmt) {
170 
171         /* If external and internal formats are both YUV420 (or YUV422), don't change scaled_flag.
172            Otherwise, color format is subsampled(lossy) and scaled_flag should be set as 1. */
173         case 1: /*YUV420*/
174             if (OVERLAP_INFO(image) == 2)
175                 assert(image->extended_width >= 32);
176             if (image->output_clr_fmt != JXR_OCF_YUV420)
177                 image->scaled_flag = 1;
178             break;
179         case 2: /*YUV422*/
180             if (OVERLAP_INFO(image) == 2)
181                 assert(image->extended_width >= 32);
182             if (image->output_clr_fmt != JXR_OCF_YUV422)
183                 image->scaled_flag = 1;
184             break;
185     }
186 
187     unsigned * temp_ptr, idx;
188     temp_ptr = image->tile_column_width;
189     image->tile_column_width = (unsigned*)calloc(2*image->tile_columns, sizeof(unsigned));
190     for (idx = 0 ; idx < image->tile_columns ; idx++)
191         image->tile_column_width[idx] = temp_ptr[idx];
192     image->tile_column_position = image->tile_column_width + image->tile_columns;
193 
194     temp_ptr = image->tile_row_height;
195     image->tile_row_height = (unsigned*)calloc(2*image->tile_rows, sizeof(unsigned));
196     for (idx = 0 ; idx < image->tile_rows ; idx++)
197         image->tile_row_height[idx] = temp_ptr[idx];
198     image->tile_row_position = image->tile_row_height + image->tile_rows;
199 
200     if (TILING_FLAG(image)) {
201         unsigned width_MB = EXTENDED_WIDTH_BLOCKS(image), height_MB = EXTENDED_HEIGHT_BLOCKS(image);
202         unsigned min_width = 1, total_width = 0, min_height = 1, total_height = 0;
203 
204         if (image->tile_column_width[0] == 0) {
205             total_width = 0;
206             for ( idx = 0 ; idx < image->tile_columns - 1 ; idx++ ) {
207                 image->tile_column_width[idx] = width_MB / image->tile_columns;
208                 image->tile_column_position[idx] = total_width;
209                 total_width += image->tile_column_width[idx];
210             }
211             image->tile_column_width[image->tile_columns - 1] = width_MB - total_width;
212             image->tile_column_position[image->tile_columns - 1] = total_width;
213         }
214         total_width = 0;
215 
216         if ((OVERLAP_INFO(image) == 2) && ((image->use_clr_fmt == 1/*YUV420*/) || (image->use_clr_fmt == 2/*YUV422*/)) && image->disableTileOverlapFlag)
217             min_width = 2;
218         for ( idx = 0 ; idx < image->tile_columns - 1 ; idx++ ) {
219             if (image->tile_column_width[idx] < min_width) {
220                 DEBUG(" Tile %d width is below minimum width\n", idx);
221                 assert(0);
222                 break;
223             }
224             image->tile_column_position[idx] = total_width;
225             total_width += image->tile_column_width[idx];
226         }
227         if ((total_width + min_width) > width_MB) {
228             DEBUG(" Total specified tile width is above image width\n");
229             assert(0);
230         }
231         image->tile_column_position[image->tile_columns - 1] = total_width;
232         image->tile_column_width[image->tile_columns - 1] = (width_MB - total_width);
233 
234         if (image->tile_row_height[0] == 0) {
235             total_height = 0;
236             for ( idx = 0 ; idx < image->tile_rows - 1 ; idx++ ) {
237                 image->tile_row_height[idx] = height_MB / image->tile_rows;
238                 image->tile_row_position[idx] = total_height;
239                 total_height += image->tile_row_height[idx];
240             }
241             image->tile_row_height[image->tile_rows - 1] = height_MB - total_height;
242             image->tile_row_position[image->tile_rows - 1] = total_height;
243         }
244         total_height = 0;
245 
246         for ( idx = 0 ; idx < image->tile_rows - 1 ; idx++ ) {
247             if (image->tile_row_height[idx] < min_height) {
248                 DEBUG(" Tile %d height is below minimum height\n", idx);
249                 assert(0);
250                 break;
251             }
252             image->tile_row_position[idx] = total_height;
253             total_height += image->tile_row_height[idx];
254         }
255         if ((total_height + min_height) > height_MB) {
256             DEBUG(" Total specified tile height is above image height\n");
257             assert(0);
258         }
259         image->tile_row_position[image->tile_rows - 1] = total_height;
260         image->tile_row_height[image->tile_rows - 1] = (height_MB - total_height);
261 
262     } else {
263         image->tile_column_width[0] = EXTENDED_WIDTH_BLOCKS(image);
264         image->tile_column_position[0] = 0;
265 
266         image->tile_row_height[0] = EXTENDED_HEIGHT_BLOCKS(image);
267         image->tile_row_position[0] = 0;
268     }
269 
270     image->lwf_test = 0;
271 
272     _jxr_make_mbstore(image, 1);
273     image->cur_my = -5;
274     return 0;
275 }
276 
jxr_write_image_bitstream(jxr_image_t image,FILE * fd)277 int jxr_write_image_bitstream(jxr_image_t image, FILE*fd)
278 {
279     int rc, res = 0;
280 
281     struct wbitstream bits;
282     _jxr_wbitstream_initialize(&bits, fd);
283 
284     /* Clean up the image structure in preparation for actually
285     writing the image. This checks for and configures any
286     values left to defaults, and checks for bogus settings. */
287     rc = fill_in_image_defaults(image);
288     if (rc < 0)
289         return rc;
290 
291     /* Prepare index table storage */
292     initialize_index_table(image);
293 
294 
295     rc = w_image_header(image, &bits);
296     assert(rc >= 0);
297 
298     rc = w_image_plane_header(image, &bits, 0);
299     assert(rc >= 0);
300 
301     if (ALPHACHANNEL_FLAG(image)) {
302         unsigned char window_params[5];
303         if (image->window_extra_top || image->window_extra_right) {
304             window_params[0] = 1;
305             window_params[1] = image->window_extra_top;
306             window_params[2] = image->window_extra_left;
307             window_params[3] = image->window_extra_bottom;
308             window_params[4] = image->window_extra_right;
309         }
310         else {
311             window_params[4] = window_params[3] = window_params[2] = window_params[1] = window_params[0] = 0;
312         }
313 
314         image->alpha = jxr_create_image(image->width1 + 1, image->height1 + 1, window_params);
315 
316         *image->alpha = *image;
317         image->alpha->strip[0].up4 = image->alpha->strip[0].up3 = image->alpha->strip[0].up2 =
318             image->alpha->strip[0].up1 = image->alpha->strip[0].cur = NULL;
319 
320         jxr_set_INTERNAL_CLR_FMT(image->alpha, JXR_YONLY, 1);
321         _jxr_make_mbstore(image->alpha, 1);
322         image->alpha->dc_component_mode = image->alpha->lp_component_mode = image->alpha->hp_component_mode = JXR_CM_UNIFORM;
323         image->alpha->primary = 0;
324         image->alpha->cur_my = -5;
325 
326         rc = w_image_plane_header(image->alpha, &bits, 1);
327         assert(rc >= 0);
328     }
329 
330     if (INDEXTABLE_PRESENT_FLAG(image)) {
331         struct wbitstream strCodedTiles;
332         FILE*fdCodedTiles = fopen("codedtiles.tmp", "wb");
333         _jxr_wbitstream_initialize(&strCodedTiles, fdCodedTiles);
334 
335         /* CODED_TILES() */
336         w_TILE(image, &strCodedTiles);
337 
338         w_INDEX_TABLE(image, &bits);
339 
340         _jxr_wbitstream_flush(&strCodedTiles);
341         fclose(fdCodedTiles);
342 
343         /* Profile / Level info */
344         uint64_t subsequent_bytes = 4;
345         _jxr_wbitstream_intVLW(&bits, subsequent_bytes);
346 
347         if (subsequent_bytes > 0) {
348             uint64_t additional_bytes;
349             additional_bytes = w_PROFILE_LEVEL_INFO(image, &bits, subsequent_bytes) ;
350 
351             uint64_t ibyte;
352             for (ibyte = 0 ; ibyte < additional_bytes ; ibyte += 1)
353                 _jxr_wbitstream_uint8(&bits, 0); /* RESERVED_A_BYTE */
354         }
355 
356         DEBUG("MARK HERE as the tile base. bitpos=%zu\n", _jxr_wbitstream_bitpos(&bits));
357         _jxr_wbitstream_mark(&bits);
358 
359         struct rbitstream strCodedTilesRead;
360         FILE*fdCodedTilesRead = fopen("codedtiles.tmp", "rb");
361         _jxr_rbitstream_initialize(&strCodedTilesRead, fdCodedTilesRead);
362 
363         size_t idx;
364         for (idx = 0; idx < strCodedTiles.write_count; idx++) {
365             _jxr_wbitstream_uint8(&bits, _jxr_rbitstream_uint8(&strCodedTilesRead));
366         }
367         fclose(fdCodedTilesRead);
368         /* delete file associated with CodedTiles */
369         remove("codedtiles.tmp");
370 
371     }
372     else {
373         /* Profile / Level info */
374         uint64_t subsequent_bytes = 4;
375         _jxr_wbitstream_intVLW(&bits, subsequent_bytes);
376 
377         if (subsequent_bytes > 0) {
378             uint64_t additional_bytes;
379             additional_bytes = w_PROFILE_LEVEL_INFO(image, &bits, subsequent_bytes) ;
380 
381             uint64_t ibyte;
382             for (ibyte = 0 ; ibyte < additional_bytes ; ibyte += 1)
383                 _jxr_wbitstream_uint8(&bits, 0); /* RESERVED_A_BYTE */
384         }
385 
386         DEBUG("MARK HERE as the tile base. bitpos=%zu\n", _jxr_wbitstream_bitpos(&bits));
387         _jxr_wbitstream_mark(&bits);
388 
389         /* CODED_TILES() */
390         w_TILE(image, &bits);
391     }
392 
393     _jxr_wbitstream_flush(&bits);
394 
395 #ifdef VERIFY_16BIT
396     if(image->lwf_test == 0)
397         DEBUG("Meets conditions for LONG_WORD_FLAG == 0!");
398     else {
399         DEBUG("Does not meet conditions for LONG_WORD_FLAG == 0!");
400         if (LONG_WORD_FLAG(image) == 0)
401             return JXR_EC_BADFORMAT;
402     }
403 #endif
404 
405     return res;
406 }
407 
408 #if defined(DETAILED_DEBUG)
409 static const char*bitdepth_names[16] = {
410     "BD1WHITE1", "BD8", "BD16", "BD16S",
411     "BD16F", "RESERVED5", "BD32S", "BD32F",
412     "BD5", "BD10", "BD565", "RESERVED11"
413     "RESERVED12", "RESERVED12", "RESERVED12","BD1BLACK1"
414 };
415 
416 #endif
417 
w_image_header(jxr_image_t image,struct wbitstream * str)418 static int w_image_header(jxr_image_t image, struct wbitstream*str)
419 {
420     const char GDI_SIG[] = "WMPHOTO\0";
421     int res = 0;
422     unsigned idx;
423 
424     /* GDI SIGNATURE */
425     for (idx = 0 ; idx < 8 ; idx += 1) {
426         _jxr_wbitstream_uint8(str, GDI_SIG[idx]);
427     }
428 
429     DEBUG("START IMAGE_HEADER (bitpos=%zu)\n", _jxr_wbitstream_bitpos(str));
430 
431     _jxr_wbitstream_uint4(str, 1); /* VERSION_INFO */
432 
433     _jxr_wbitstream_uint1(str, image->disableTileOverlapFlag); /* HARD_TILING_FLAG */
434 
435     _jxr_wbitstream_uint3(str, 1); /* SUB_VERSION_INFO */
436 
437     DEBUG(" Flags group1=0x%02x\n", image->header_flags1);
438     _jxr_wbitstream_uint8(str, image->header_flags1);
439 
440     DEBUG(" Flags group2=0x%02x\n", image->header_flags2);
441     _jxr_wbitstream_uint8(str, image->header_flags2);
442 
443     DEBUG(" OUTPUT_CLR_FMT=%d\n", SOURCE_CLR_FMT(image));
444     DEBUG(" OUTPUT_BITEDPTH=%d (%s)\n", SOURCE_BITDEPTH(image), bitdepth_names[SOURCE_BITDEPTH(image)]);
445     _jxr_wbitstream_uint8(str, image->header_flags_fmt);
446 
447     if (SHORT_HEADER_FLAG(image)) {
448         DEBUG(" SHORT_HEADER_FLAG=true\n");
449         _jxr_wbitstream_uint16(str, image->width1);
450         _jxr_wbitstream_uint16(str, image->height1);
451     } else {
452         DEBUG(" SHORT_HEADER_FLAG=false\n");
453         _jxr_wbitstream_uint32(str, image->width1);
454         _jxr_wbitstream_uint32(str, image->height1);
455     }
456 
457     DEBUG(" Image dimensions: %u x %u\n", image->width1+1, image->height1+1);
458 
459     /* Write TILE geometry information, if there is TILING. */
460     if (jxr_get_TILING_FLAG(image)) {
461 
462         DEBUG(" TILING %u columns, %u rows\n",
463             image->tile_columns, image->tile_rows);
464 
465         _jxr_wbitstream_uint12(str, image->tile_columns -1);
466         _jxr_wbitstream_uint12(str, image->tile_rows -1);
467 
468         for (idx = 0 ; idx < image->tile_columns-1 ; idx += 1) {
469             if (SHORT_HEADER_FLAG(image))
470                 _jxr_wbitstream_uint8(str, image->tile_column_width[idx]);
471             else
472                 _jxr_wbitstream_uint16(str,image->tile_column_width[idx]);
473         }
474         for (idx = 0 ; idx < image->tile_rows-1 ; idx += 1) {
475             if (SHORT_HEADER_FLAG(image))
476                 _jxr_wbitstream_uint8(str, image->tile_row_height[idx]);
477             else
478                 _jxr_wbitstream_uint16(str,image->tile_row_height[idx]);
479         }
480     } else {
481         DEBUG(" NO TILING\n");
482     }
483 
484 #if defined(DETAILED_DEBUG)
485     DEBUG(" Tile widths:");
486     for (idx = 0 ; idx < image->tile_columns ; idx += 1)
487         DEBUG(" %u", image->tile_column_width[idx]);
488     DEBUG("\n");
489     DEBUG(" Tile heights:");
490     for (idx = 0 ; idx < image->tile_rows ; idx += 1)
491         DEBUG(" %u", image->tile_row_height[idx]);
492     DEBUG("\n");
493 #endif
494 
495     /* Write out windowing bits. */
496     if (WINDOWING_FLAG(image)) {
497         _jxr_wbitstream_uint6(str, (uint8_t)image->window_extra_top);
498         _jxr_wbitstream_uint6(str, (uint8_t)image->window_extra_left);
499         _jxr_wbitstream_uint6(str, (uint8_t)image->window_extra_bottom);
500         _jxr_wbitstream_uint6(str, (uint8_t)image->window_extra_right);
501     }
502 
503     DEBUG("END IMAGE_HEADER\n");
504     return res;
505 }
506 
w_image_plane_header(jxr_image_t image,struct wbitstream * str,int alpha)507 static int w_image_plane_header(jxr_image_t image, struct wbitstream*str, int alpha)
508 {
509     DEBUG("START IMAGE_PLANE_HEADER (bitpos=%zu)\n", _jxr_wbitstream_bitpos(str));
510 
511     DEBUG(" INTERNAL_CLR_FMT = %d\n", image->use_clr_fmt);
512     DEBUG(" SCALED_FLAG = %s\n", image->scaled_flag? "true" : "false");
513     DEBUG(" BANDS_PRESENT = %d\n", image->bands_present);
514 
515     _jxr_wbitstream_uint3(str, image->use_clr_fmt);
516     _jxr_wbitstream_uint1(str, image->scaled_flag); /* SCALED_FLAG = 1 */
517     _jxr_wbitstream_uint4(str, image->bands_present);
518 
519     switch (image->use_clr_fmt) {
520         case 0: /* YONLY */
521             image->num_channels = 1;
522             break;
523         case 1: /* YUV420 */
524             image->num_channels = 3;
525             _jxr_wbitstream_uint4(str, 0); /* CHROMA_CENTERING */
526             _jxr_wbitstream_uint4(str, 0); /* COLOR_INTERPRETATION==0 */
527             break;
528         case 2: /* YUV422 */
529             image->num_channels = 3;
530             _jxr_wbitstream_uint4(str, 0); /* CHROMA_CENTERING */
531             _jxr_wbitstream_uint4(str, 0); /* COLOR_INTERPRETATION==0 */
532             break;
533         case 3: /* YUV444 */
534             image->num_channels = 3;
535             _jxr_wbitstream_uint4(str, 0); /* CHROMA_CENTERING */
536             _jxr_wbitstream_uint4(str, 0); /* COLOR_INTERPRETATION==0 */
537             break;
538         case 4: /* YUVK */
539             image->num_channels = 4;
540             break;
541         case 6: /* NCOMPONENT */
542             _jxr_wbitstream_uint4(str, image->num_channels-1);
543             _jxr_wbitstream_uint4(str, 0); /* COLOR_INTERPRETATION==0 */
544             break;
545         case 5: /* RESERVED */
546         case 7: /* RESERVED */
547             break;
548     }
549 
550     switch (SOURCE_BITDEPTH(image)) {
551         case 0: /* BD1WHITE1 */
552         case 1: /* BD8 */
553         case 4: /* BD16F */
554         case 8: /* BD5 */
555         case 9: /* BD10 */
556         case 10: /* BD565 */
557         case 15: /* BD1BLACK1 */
558             break;
559         case 2: /* BD16 */
560         case 3: /* BD16S */
561         case 6: /* BD32S */
562             _jxr_wbitstream_uint8(str, image->shift_bits); /* SHIFT_BITS */
563             break;
564         case 7: /* BD32F */
565             _jxr_wbitstream_uint8(str, image->len_mantissa); /* LEN_MANTISSA */
566             _jxr_wbitstream_uint8(str, image->exp_bias); /* EXP_BIAS */
567             break;
568         default: /* RESERVED */
569             break;
570     }
571 
572     /* Emit QP information for DC pass. */
573     _jxr_wbitstream_uint1(str, image->dc_frame_uniform); /* DC_FRAME_UNIFORM */
574     if (image->dc_frame_uniform) {
575         DEBUG(" DC_FRAME_UNIFORM = %s\n", image->dc_frame_uniform?"true":"false");
576         _jxr_w_DC_QP(image, str);
577     }
578 
579     if (image->bands_present != 3 /*DCONLY*/) {
580         /* Emit QP information for LP pass. */
581         _jxr_wbitstream_uint1(str, 0); /* RESERVED_I_BIT */
582         _jxr_wbitstream_uint1(str, image->lp_frame_uniform);
583         DEBUG(" LP_FRAME_UNIFORM = %s\n", image->lp_frame_uniform?"true":"false");
584         if (image->lp_frame_uniform) {
585             assert(image->num_lp_qps > 0);
586             /* _jxr_wbitstream_uint4(str, image->num_lp_qps-1); */
587             _jxr_w_LP_QP(image, str);
588         }
589 
590         if (image->bands_present != 2 /*NOHIGHPASS*/) {
591             /* Emit QP information for HP pass. */
592             _jxr_wbitstream_uint1(str, 0); /* RESERVED_J_BIT */
593             _jxr_wbitstream_uint1(str, image->hp_frame_uniform);
594             DEBUG(" HP_FRAME_UNIFORM = %s\n", image->hp_frame_uniform?"true":"false");
595             if (image->hp_frame_uniform) {
596                 _jxr_w_HP_QP(image, str);
597             }
598         }
599     }
600 
601     _jxr_wbitstream_syncbyte(str);
602     DEBUG("END IMAGE_PLANE_HEADER (bitpos=%zu)\n", _jxr_wbitstream_bitpos(str));
603 
604     return 0;
605 }
606 
put_ch_mode(jxr_image_t image,struct wbitstream * str)607 static int put_ch_mode(jxr_image_t image, struct wbitstream*str)
608 {
609     /* If there is only 1 channel, then CH_MODE==0 is implicit. */
610     if (image->num_channels == 1) {
611         assert(image->dc_component_mode == JXR_CM_UNIFORM);
612         return 0;
613     }
614 
615     int use_mode = image->dc_component_mode;
616     _jxr_wbitstream_uint2(str, use_mode);
617     return use_mode;
618 }
619 
_jxr_w_DC_QP(jxr_image_t image,struct wbitstream * str)620 void _jxr_w_DC_QP(jxr_image_t image, struct wbitstream*str)
621 {
622     int idx;
623     int ch_mode = put_ch_mode(image, str);
624     DEBUG(" DC_QP CH_MODE=%d ", ch_mode);
625 
626     switch (ch_mode) {
627         case 0: /* UNIFORM */
628             DEBUG(" DC_QUANT UNIFORM =%u", image->dc_quant_ch[0]);
629             _jxr_wbitstream_uint8(str, image->dc_quant_ch[0]);
630             break;
631         case 1: /* SEPARATE */
632             DEBUG(" DC_QUANT SEPARATE Y=%u, Chr=%u", image->dc_quant_ch[0],image->dc_quant_ch[1]);
633             _jxr_wbitstream_uint8(str, image->dc_quant_ch[0]);
634             _jxr_wbitstream_uint8(str, image->dc_quant_ch[1]);
635             break;
636         case 2: /* INDEPENDENT */
637             DEBUG(" DC_QUANT INDEPENDENT =");
638             for (idx = 0 ; idx < image->num_channels ; idx +=1) {
639                 DEBUG(" %u", image->dc_quant_ch[idx]);
640                 _jxr_wbitstream_uint8(str, image->dc_quant_ch[idx]);
641             }
642             break;
643         case 3: /* Reserved */
644             break;
645         default:
646             assert(0);
647             break;
648     }
649     DEBUG("\n");
650 }
651 
_jxr_w_LP_QP(jxr_image_t image,struct wbitstream * str)652 void _jxr_w_LP_QP(jxr_image_t image, struct wbitstream*str)
653 {
654     unsigned idx;
655     for (idx = 0 ; idx < image->num_lp_qps ; idx += 1) {
656         int ch_mode = put_ch_mode(image, str);
657         DEBUG(" LP_QP[%d] CH_MODE=%d ", idx, ch_mode);
658 
659         switch (ch_mode) {
660             case 0: /* UNIFORM */
661                 DEBUG(" LP_QUANT UNIFORM =%u", image->lp_quant_ch[0][idx]);
662                 _jxr_wbitstream_uint8(str, image->lp_quant_ch[0][idx]);
663                 break;
664             case 1: /* SEPARATE */
665                 DEBUG(" LP_QUANT SEPARATE Y=%u, Chr=%u", image->lp_quant_ch[0][idx],image->lp_quant_ch[1][idx]);
666                 _jxr_wbitstream_uint8(str, image->lp_quant_ch[0][idx]);
667                 _jxr_wbitstream_uint8(str, image->lp_quant_ch[1][idx]);
668                 break;
669             case 2: /* INDEPENDENT */
670                 DEBUG(" LP_QUANT INDEPENDENT =");
671                 int ch;
672                 for (ch = 0 ; ch < image->num_channels ; ch +=1) {
673                     DEBUG(" %u", image->lp_quant_ch[ch][idx]);
674                     _jxr_wbitstream_uint8(str, image->lp_quant_ch[ch][idx]);
675                 }
676                 break;
677             case 3: /* Reserved */
678                 break;
679             default:
680                 assert(0);
681                 break;
682         }
683         DEBUG("\n");
684     }
685 }
686 
_jxr_w_HP_QP(jxr_image_t image,struct wbitstream * str)687 void _jxr_w_HP_QP(jxr_image_t image, struct wbitstream*str)
688 {
689     unsigned idx;
690     for (idx = 0 ; idx < image->num_hp_qps ; idx += 1) {
691         int ch_mode = put_ch_mode(image, str);
692         DEBUG(" HP_QP[%d] CH_MODE=%d ", idx, ch_mode);
693 
694         switch (ch_mode) {
695             case 0: /* UNIFORM */
696                 _jxr_wbitstream_uint8(str, image->hp_quant_ch[0][idx]);
697                 DEBUG("UNIFORM %d", image->hp_quant_ch[0][idx]);
698                 break;
699             case 1: /* SEPARATE */
700                 DEBUG("SEPARATE Y=%u, Chr=%u", image->hp_quant_ch[0][idx],image->hp_quant_ch[1][idx]);
701                 _jxr_wbitstream_uint8(str, image->hp_quant_ch[0][idx]);
702                 _jxr_wbitstream_uint8(str, image->hp_quant_ch[1][idx]);
703                 break;
704             case 2: /* INDEPENDENT */
705                 DEBUG("INDEPENDENT =");
706                 int ch;
707                 for (ch = 0 ; ch < image->num_channels ; ch +=1) {
708                     DEBUG(" %u", image->hp_quant_ch[ch][idx]);
709                     _jxr_wbitstream_uint8(str, image->hp_quant_ch[ch][idx]);
710                 }
711                 break;
712             case 3: /* Reserved */
713                 break;
714             default:
715                 assert(0);
716         }
717         DEBUG(" bitpos=%zu\n", _jxr_wbitstream_bitpos(str));
718     }
719 }
720 
w_INDEX_TABLE(jxr_image_t image,struct wbitstream * str)721 static void w_INDEX_TABLE(jxr_image_t image, struct wbitstream*str)
722 {
723     DEBUG("START INDEX_TABLE at bitpos=%zu\n", _jxr_wbitstream_bitpos(str));
724 
725     if (INDEXTABLE_PRESENT_FLAG(image)) {
726         /* INDEX_TABLE_STARTCODE == 0x0001 */
727         DEBUG(" INDEX_TAB:E_STARTCODE at bitpos=%zu\n", _jxr_wbitstream_bitpos(str));
728         _jxr_wbitstream_uint8(str, 0x00);
729         _jxr_wbitstream_uint8(str, 0x01);
730 
731         int idx;
732         for (idx = 0 ; idx < image->tile_index_table_length ; idx++) {
733             _jxr_wbitstream_intVLW(str, image->tile_index_table[idx]);
734         }
735 
736         DEBUG(" INDEX_TABLE has %d table entries\n", image->tile_index_table_length);
737 
738     }
739 
740     DEBUG("INTEX_TABLE DONE bitpos=%zu\n", _jxr_wbitstream_bitpos(str));
741 }
742 
w_PROFILE_LEVEL_INFO(jxr_image_t image,struct wbitstream * str,uint64_t bytes)743 static uint64_t w_PROFILE_LEVEL_INFO(jxr_image_t image, struct wbitstream*str, uint64_t bytes)
744 {
745     uint64_t additional_bytes;
746     for (additional_bytes = bytes; additional_bytes > 3 ; additional_bytes -= 4) {
747         /* These profile and level values are default.  More logic needed */
748         _jxr_wbitstream_uint8(str, image->profile_idc); /* PROFILE_IDC */
749         _jxr_wbitstream_uint8(str, image->level_idc); /* LEVEL_IDC */
750         _jxr_wbitstream_uint15(str, 0); /* RESERVED_L */
751         if (additional_bytes > 7)
752             _jxr_wbitstream_uint1(str, 0); /* LAST_FLAG */
753         else
754             _jxr_wbitstream_uint1(str, 1); /* LAST_FLAG */
755     }
756 
757     return additional_bytes;
758 }
759 
w_TILE(jxr_image_t image,struct wbitstream * str)760 static void w_TILE(jxr_image_t image, struct wbitstream*str)
761 {
762     unsigned tile_idx = 0;
763 
764     if (FREQUENCY_MODE_CODESTREAM_FLAG(image) == 0 /* SPATIALMODE */) {
765 
766         if (TILING_FLAG(image)) {
767             unsigned tx, ty;
768             for (ty = 0 ; ty < image->tile_rows ; ty += 1)
769                 for (tx = 0 ; tx < image->tile_columns ; tx += 1) {
770                     _jxr_w_TILE_SPATIAL(image, str, tx, ty);
771                     image->tile_index_table[tile_idx] = str->write_count;
772                     tile_idx++;
773                 }
774         } else {
775             _jxr_w_TILE_SPATIAL(image, str, 0, 0);
776         }
777 
778     } else { /* FREQUENCYMODE */
779         /* Temporarily declare these bitstreams til I figure out the tmp file vs in memory issue*/
780         unsigned tx, ty;
781         uint8_t bands_present = image->bands_present_of_primary;
782 
783         for (ty = 0 ; ty < image->tile_rows ; ty += 1) {
784             for (tx = 0 ; tx < image->tile_columns ; tx += 1) {
785                 _jxr_w_TILE_DC(image, str, tx, ty);
786                 image->tile_index_table[tile_idx * (4 - bands_present) + 0] = str->write_count;
787                 if (bands_present != 3) {
788                     _jxr_w_TILE_LP(image, str, tx, ty);
789                     image->tile_index_table[tile_idx * (4 - bands_present) + 1] = str->write_count;
790                     if (bands_present != 2) {
791                         _jxr_w_TILE_HP_FLEX(image, str, tx, ty);
792                     }
793                 }
794 
795 
796                 tile_idx++;
797             }
798         }
799     }
800 
801     if (INDEXTABLE_PRESENT_FLAG(image)) {
802         for (tile_idx = image->tile_index_table_length - 1 ; tile_idx > 0  ; tile_idx--) {
803             image->tile_index_table[tile_idx] = image->tile_index_table[tile_idx - 1];
804         }
805         image->tile_index_table[0] = 0;
806     }
807 
808 }
809 
810 
short_header_ok(jxr_image_t image)811 static int short_header_ok(jxr_image_t image)
812 {
813     /* If the image width or height is too big for short header,
814     then we need a long header. */
815     if (image->width1 >= 0x10000 || image->height1 >= 0x10000)
816         return 0;
817 
818     /* If the tile width/height is too big for a short header,
819     then we need to use a long header. */
820     if (jxr_get_TILING_FLAG(image)) {
821         unsigned idx;
822 
823         for (idx = 0 ; idx < image->tile_columns ; idx += 1)
824             if (jxr_get_TILE_WIDTH(image,idx)/16 > 0x100)
825                 return 0;
826         for (idx = 0 ; idx < image->tile_rows ; idx += 1)
827             if (jxr_get_TILE_HEIGHT(image,idx)/16 > 0x100)
828                 return 0;
829     }
830 
831     /* If nothing else forces us to use a long header, then we can
832     use a short header. */
833     return 1;
834 }
835 
need_windowing_flag(jxr_image_t image)836 static int need_windowing_flag(jxr_image_t image)
837 {
838     if (image->window_extra_top > 0)
839         return 1;
840     if (image->window_extra_left > 0)
841         return 1;
842 
843     return 0;
844 }
845 
need_trim_flexbits_flag(jxr_image_t image)846 static int need_trim_flexbits_flag(jxr_image_t image)
847 {
848     /* If no FLEXBTS data, then no TRIM_FLEXBITS flag */
849     if (image->bands_present != JXR_BP_ALL)
850         return 0;
851     if (image->trim_flexbits == 0)
852         return 0;
853 
854     return 1;
855 }
856 
_jxr_w_TILE_HEADER_DC(jxr_image_t image,struct wbitstream * str,int alpha_flag,unsigned tx,unsigned ty)857 void _jxr_w_TILE_HEADER_DC(jxr_image_t image, struct wbitstream*str,
858                              int alpha_flag, unsigned tx, unsigned ty)
859 {
860     if (image->dc_frame_uniform == 0) {
861         int ch;
862 
863         /* per-tile configuration, so get the QP settings from the
864         per-tile store and make it current. */
865         assert(image->tile_quant);
866         struct jxr_tile_qp*cur = GET_TILE_QUANT(image, tx, ty);
867         image->dc_component_mode = cur->component_mode;
868         switch (image->dc_component_mode) {
869             case JXR_CM_UNIFORM:
870                 for (ch = 0 ; ch < image->num_channels ; ch += 1)
871                     image->dc_quant_ch[ch] = cur->channel[0].dc_qp;
872                 break;
873             case JXR_CM_SEPARATE:
874                 image->dc_quant_ch[0] = cur->channel[0].dc_qp;
875                 for (ch = 1 ; ch < image->num_channels ; ch += 1)
876                     image->dc_quant_ch[ch] = cur->channel[1].dc_qp;
877                 break;
878             case JXR_CM_INDEPENDENT:
879                 for (ch = 0 ; ch < image->num_channels ; ch += 1)
880                     image->dc_quant_ch[ch] = cur->channel[ch].dc_qp;
881                 break;
882             case JXR_CM_Reserved:
883                 assert(0);
884                 break;
885         }
886 
887         _jxr_w_DC_QP(image, str);
888     }
889 }
890 
_jxr_w_TILE_HEADER_LOWPASS(jxr_image_t image,struct wbitstream * str,int alpha_flag,unsigned tx,unsigned ty)891 void _jxr_w_TILE_HEADER_LOWPASS(jxr_image_t image, struct wbitstream*str,
892                                   int alpha_flag, unsigned tx, unsigned ty)
893 {
894     if (image->lp_frame_uniform == 0) {
895         int ch;
896         unsigned idx;
897 
898         /* per-tile configuration, so get the QP settings from the
899         per-tile store and make it current. */
900         assert(image->tile_quant);
901         struct jxr_tile_qp*cur = GET_TILE_QUANT(image, tx, ty);
902         image->lp_component_mode = cur->component_mode;
903         image->num_lp_qps = cur->channel[0].num_lp;
904 
905         switch (image->lp_component_mode) {
906             case JXR_CM_UNIFORM:
907                 for (ch = 0 ; ch < image->num_channels ; ch += 1) {
908                     for (idx = 0 ; idx < image->num_lp_qps ; idx += 1)
909                         image->lp_quant_ch[ch][idx] = cur->channel[0].lp_qp[idx];
910                 }
911                 break;
912             case JXR_CM_SEPARATE:
913                 for (idx = 0 ; idx < image->num_lp_qps ; idx += 1)
914                     image->lp_quant_ch[0][idx] = cur->channel[0].lp_qp[idx];
915                 for (ch = 1 ; ch < image->num_channels ; ch += 1) {
916                     for (idx = 0 ; idx < image->num_lp_qps ; idx += 1)
917                         image->lp_quant_ch[ch][idx] = cur->channel[1].lp_qp[idx];
918                 }
919                 break;
920             case JXR_CM_INDEPENDENT:
921                 for (ch = 0 ; ch < image->num_channels ; ch += 1) {
922                     for (idx = 0 ; idx < image->num_lp_qps ; idx += 1)
923                         image->lp_quant_ch[ch][idx] = cur->channel[ch].lp_qp[idx];
924                 }
925                 break;
926             case JXR_CM_Reserved:
927                 assert(0);
928                 break;
929         }
930 
931         _jxr_wbitstream_uint1(str, 0); /* XXXX USE_DC_QP == FALSE */
932         assert(image->num_lp_qps > 0);
933         DEBUG(" TILE_HEADER_LP: NUM_LP_QPS = %d\n", image->num_lp_qps);
934         _jxr_wbitstream_uint4(str, image->num_lp_qps-1);
935         _jxr_w_LP_QP(image, str);
936     }
937 }
938 
_jxr_w_TILE_HEADER_HIGHPASS(jxr_image_t image,struct wbitstream * str,int alpha_flag,unsigned tx,unsigned ty)939 void _jxr_w_TILE_HEADER_HIGHPASS(jxr_image_t image, struct wbitstream*str,
940                                    int alpha_flag, unsigned tx, unsigned ty)
941 {
942     if (image->hp_frame_uniform == 0) {
943         int ch;
944         unsigned idx;
945 
946         /* per-tile configuration, so get the QP settings from the
947         per-tile store and make it current. */
948         assert(image->tile_quant);
949         struct jxr_tile_qp*cur = GET_TILE_QUANT(image, tx, ty);
950         image->hp_component_mode = cur->component_mode;
951         image->num_hp_qps = cur->channel[0].num_hp;
952 
953         switch (image->hp_component_mode) {
954             case JXR_CM_UNIFORM:
955                 for (ch = 0 ; ch < image->num_channels ; ch += 1) {
956                     for (idx = 0 ; idx < image->num_hp_qps ; idx += 1)
957                         image->hp_quant_ch[ch][idx] = cur->channel[0].hp_qp[idx];
958                 }
959                 break;
960             case JXR_CM_SEPARATE:
961                 for (idx = 0 ; idx < image->num_hp_qps ; idx += 1)
962                     image->hp_quant_ch[0][idx] = cur->channel[0].hp_qp[idx];
963                 for (ch = 1 ; ch < image->num_channels ; ch += 1) {
964                     for (idx = 0 ; idx < image->num_hp_qps ; idx += 1)
965                         image->hp_quant_ch[ch][idx] = cur->channel[1].hp_qp[idx];
966                 }
967                 break;
968             case JXR_CM_INDEPENDENT:
969                 for (ch = 0 ; ch < image->num_channels ; ch += 1) {
970                     for (idx = 0 ; idx < image->num_hp_qps ; idx += 1)
971                         image->hp_quant_ch[ch][idx] = cur->channel[ch].hp_qp[idx];
972                 }
973                 break;
974             case JXR_CM_Reserved:
975                 assert(0);
976                 break;
977         }
978 
979         _jxr_wbitstream_uint1(str, 0); /* XXXX USE_LP_QP == FALSE */
980         assert(image->num_hp_qps > 0);
981         _jxr_wbitstream_uint4(str, image->num_hp_qps-1);
982         _jxr_w_HP_QP(image, str);
983     }
984 }
985 
_jxr_w_ENCODE_QP_INDEX(jxr_image_t image,struct wbitstream * str,unsigned tx,unsigned ty,unsigned mx,unsigned my,unsigned num_qps,unsigned qp_index)986 void _jxr_w_ENCODE_QP_INDEX(jxr_image_t image, struct wbitstream*str,
987                               unsigned tx, unsigned ty, unsigned mx, unsigned my,
988                               unsigned num_qps, unsigned qp_index)
989 {
990     static const unsigned bits_qp_index[17] = {0, 0,1,1,2, 2,3,3,3, 3,4,4,4, 4,4,4,4};
991 
992     assert(num_qps > 1 && num_qps <= 16);
993 
994     if (qp_index == 0) {
995         /* IS_QPINDEX_NONZERO_FLAG == false */
996         _jxr_wbitstream_uint1(str, 0);
997         return;
998     }
999 
1000     /* IS_QPINDEX_NONZERO_FLAG == true */
1001     _jxr_wbitstream_uint1(str, 1);
1002 
1003     int bits_count = bits_qp_index[num_qps];
1004     assert(bits_count > 0); /* num_qps must be >1 here. */
1005 
1006     _jxr_wbitstream_uintN(str, qp_index-1, bits_count);
1007 }
1008 
encode_val_dc_yuv(jxr_image_t image,struct wbitstream * str,int val)1009 static void encode_val_dc_yuv(jxr_image_t image, struct wbitstream*str, int val)
1010 {
1011     assert(val >= 0 && val <= 7);
1012 
1013     switch (val) {
1014         case 0: /* 10 */
1015             _jxr_wbitstream_uint2(str, 2);
1016             break;
1017         case 1: /* 001 */
1018             _jxr_wbitstream_uint2(str, 0);
1019             _jxr_wbitstream_uint1(str, 1);
1020             break;
1021         case 2: /* 0000 1 */
1022             _jxr_wbitstream_uint4(str, 0);
1023             _jxr_wbitstream_uint1(str, 1);
1024             break;
1025         case 3: /* 0001 */
1026             _jxr_wbitstream_uint4(str, 1);
1027             break;
1028         case 4: /* 11 */
1029             _jxr_wbitstream_uint2(str, 3);
1030             break;
1031         case 5: /* 010 */
1032             _jxr_wbitstream_uint2(str, 1);
1033             _jxr_wbitstream_uint1(str, 0);
1034             break;
1035         case 6: /* 0000 0 */
1036             _jxr_wbitstream_uint4(str, 0);
1037             _jxr_wbitstream_uint1(str, 0);
1038             break;
1039         case 7: /* 011 */
1040             _jxr_wbitstream_uint2(str, 1);
1041             _jxr_wbitstream_uint1(str, 1);
1042             break;
1043     }
1044 }
1045 
_jxr_w_MB_DC(jxr_image_t image,struct wbitstream * str,int alpha_flag,unsigned tx,unsigned ty,unsigned mx,unsigned my)1046 void _jxr_w_MB_DC(jxr_image_t image, struct wbitstream*str,
1047                     int alpha_flag,
1048                     unsigned tx, unsigned ty,
1049                     unsigned mx, unsigned my)
1050 {
1051     int lap_mean[2];
1052     lap_mean[0] = 0;
1053     lap_mean[1] = 0;
1054 
1055     DEBUG(" MB_DC tile=[%u %u] mb=[%u %u] bitpos=%zu\n",
1056         tx, ty, mx, my, _jxr_wbitstream_bitpos(str));
1057 
1058     if (_jxr_InitContext(image, tx, ty, mx, my)) {
1059         DEBUG(" MB_DC: Initialize Context\n");
1060         _jxr_InitVLCTable(image, AbsLevelIndDCLum);
1061         _jxr_InitVLCTable(image, AbsLevelIndDCChr);
1062         _jxr_InitializeModelMB(&image->model_dc, 0/*DC*/);
1063     }
1064 
1065     if (image->use_clr_fmt==0 || image->use_clr_fmt==4 || image->use_clr_fmt==6) {
1066         int32_t dc_val = 0;
1067 
1068         /* clr_fmt == YONLY, YUVK or NCOMPONENT */
1069         unsigned idx;
1070         for (idx = 0 ; idx < image->num_channels ; idx += 1) {
1071             dc_val = MACROBLK_CUR_DC(image,idx,tx,mx);
1072             int m = (idx == 0)? 0 : 1;
1073             int model_bits = image->model_dc.bits[m];
1074 
1075             unsigned is_dc_ch = (labs(dc_val)>>model_bits) != 0 ? 1 : 0;
1076             _jxr_wbitstream_uint1(str, is_dc_ch);
1077             DEBUG(" MB_DC: IS_DC_CH=%u, model_bits=%d\n",
1078                 is_dc_ch, model_bits);
1079             if (is_dc_ch) {
1080                 lap_mean[m] += 1;
1081             }
1082             DEBUG(" dc_val at t=[%u %u], m=[%u %u] == %d (0x%08x)\n",
1083                 tx, ty, mx, my, dc_val, dc_val);
1084             w_DEC_DC(image, str, model_bits, 0/*chroma*/, is_dc_ch, dc_val);
1085         }
1086     } else {
1087         int32_t dc_val_Y = MACROBLK_CUR_DC(image,0,tx,mx);
1088         int32_t dc_val_U = MACROBLK_CUR_DC(image,1,tx,mx);
1089         int32_t dc_val_V = MACROBLK_CUR_DC(image,2,tx,mx);
1090         int val_dc_yuv = 0;
1091 
1092         if ((labs(dc_val_Y) >> image->model_dc.bits[0]) != 0) {
1093             val_dc_yuv |= 0x4;
1094             lap_mean[0] += 1;
1095         }
1096         if ((labs(dc_val_U) >> image->model_dc.bits[1]) != 0) {
1097             val_dc_yuv |= 0x2;
1098             lap_mean[1] += 1;
1099         }
1100         if ((labs(dc_val_V) >> image->model_dc.bits[1]) != 0) {
1101             val_dc_yuv |= 0x1;
1102             lap_mean[1] += 1;
1103         }
1104 
1105         DEBUG(" VAL_DC_YUV = %x\n", val_dc_yuv);
1106         encode_val_dc_yuv(image, str, val_dc_yuv);
1107 
1108         DEBUG(" dc_val_Y at t=[%u %u], m=[%u %u] == %d (0x%08x)\n",
1109             tx, ty, mx, my, dc_val_Y, dc_val_Y);
1110         int model_bits = image->model_dc.bits[0];
1111         int is_dc_ch = val_dc_yuv&0x4 ? 1 : 0;
1112         w_DEC_DC(image, str, model_bits, 0/*chroma*/, is_dc_ch, dc_val_Y);
1113 
1114         DEBUG(" dc_val_U at t=[%u %u], m=[%u %u] == %d (0x%08x)\n",
1115             tx, ty, mx, my, dc_val_U, dc_val_U);
1116         model_bits = image->model_dc.bits[1];
1117         is_dc_ch = val_dc_yuv&0x2 ? 1 : 0;
1118         w_DEC_DC(image, str, model_bits, 1/*chroma*/, is_dc_ch, dc_val_U);
1119 
1120         DEBUG(" dc_val_V at t=[%u %u], m=[%u %u] == %d (0x%08x)\n",
1121             tx, ty, mx, my, dc_val_V, dc_val_V);
1122         model_bits = image->model_dc.bits[1];
1123         is_dc_ch = val_dc_yuv&0x1 ? 1 : 0;
1124         w_DEC_DC(image, str, model_bits, 1/*chroma*/, is_dc_ch, dc_val_V);
1125     }
1126 
1127     /* */
1128     DEBUG(" MB_DC: UpdateModelMB: lap_mean={%u %u}\n", lap_mean[0], lap_mean[1]);
1129     _jxr_UpdateModelMB(image, lap_mean, &image->model_dc, 0/*DC*/);
1130     if (_jxr_ResetContext(image, tx, mx)) {
1131         DEBUG(" MB_DC: Reset Context\n");
1132         _jxr_AdaptVLCTable(image, AbsLevelIndDCLum);
1133         _jxr_AdaptVLCTable(image, AbsLevelIndDCChr);
1134     }
1135 
1136     DEBUG(" MB_DC DONE tile=[%u %u] mb=[%u %u]\n", tx, ty, mx, my);
1137 }
1138 
w_REFINE_LP(struct wbitstream * str,int coeff,int model_bits)1139 static void w_REFINE_LP(struct wbitstream*str, int coeff, int model_bits)
1140 {
1141     if (coeff > 0) {
1142         _jxr_wbitstream_uintN(str, coeff, model_bits);
1143         coeff >>= model_bits;
1144         if (coeff == 0)
1145             _jxr_wbitstream_uint1(str, 0);
1146     } else if (coeff < 0) {
1147         coeff = -coeff;
1148         _jxr_wbitstream_uintN(str, coeff, model_bits);
1149         coeff >>= model_bits;
1150         if (coeff == 0)
1151             _jxr_wbitstream_uint1(str, 1);
1152     } else {
1153         _jxr_wbitstream_uintN(str, 0, model_bits);
1154         coeff >>= model_bits;
1155         /* No sign bit is needed for zero values. */
1156     }
1157 }
1158 
1159 /*
1160 * This maps the values in src[1-15] into dst[1-15], and adapts the
1161 * map as it goes.
1162 */
AdaptiveLPPermute(jxr_image_t image,int dst[16],const int src[16])1163 static void AdaptiveLPPermute(jxr_image_t image, int dst[16], const int src[16])
1164 {
1165     int idx;
1166     for (idx = 1 ; idx < 16 ; idx += 1) {
1167 
1168         int map = image->lopass_scanorder[idx-1];
1169         dst[idx] = src[map];
1170 
1171         if (dst[idx] == 0)
1172             continue;
1173 
1174         image->lopass_scantotals[idx-1] += 1;
1175 
1176         if (idx > 1 && image->lopass_scantotals[idx-1] > image->lopass_scantotals[idx-2]) {
1177             SWAP(image->lopass_scantotals[idx-1], image->lopass_scantotals[idx-2]);
1178             SWAP(image->lopass_scanorder[idx-1], image->lopass_scanorder[idx-2]);
1179         }
1180     }
1181 }
1182 
FixedLPPermuteUV(jxr_image_t image,int dst[16],int src[8][16])1183 static void FixedLPPermuteUV(jxr_image_t image, int dst[16], int src[8][16])
1184 {
1185     assert(image->use_clr_fmt==1/*YUV420*/ || image->use_clr_fmt==2/*YUV422*/);
1186 
1187     int k;
1188     int count_chr = 14;
1189     if (image->use_clr_fmt==1/*YUV420*/)
1190         count_chr = 6;
1191 
1192     static const int remap_arr422[] = {4, 1, 2, 3, 5, 6, 7};
1193 
1194     /* Shuffle the UV planes into a single dst */
1195     for (k = 0; k < count_chr; k += 1) {
1196         int remap = k>>1;
1197         int plane = (k&1) + 1; /* 1 or 2 (U or V) */
1198 
1199         if (image->use_clr_fmt==1/*YUV420*/)
1200             remap += 1;
1201         if (image->use_clr_fmt==2/*YUV422*/)
1202             remap = remap_arr422[remap];
1203 
1204         if (image->use_clr_fmt==1/*YUV420*/)
1205             remap = transpose420[remap];
1206         if (image->use_clr_fmt==2/*YUV422*/)
1207             remap = transpose422[remap];
1208         dst[k+1] = src[plane][remap];
1209     }
1210 
1211     for (k = count_chr; k < 15; k += 1)
1212         dst[k+1] = 0;
1213 }
1214 
AdaptiveHPPermute(jxr_image_t image,int dst[16],const int src[16],int mbhp_pred_mode)1215 static void AdaptiveHPPermute(jxr_image_t image, int dst[16], const int src[16], int mbhp_pred_mode)
1216 {
1217     if (mbhp_pred_mode == 1) {
1218         int idx;
1219         for (idx = 1 ; idx < 16 ; idx += 1) {
1220             int map = image->hipass_ver_scanorder[idx-1];
1221             dst[idx] = src[map];
1222 
1223             if (dst[idx] == 0)
1224                 continue;
1225 
1226             image->hipass_ver_scantotals[idx-1] += 1;
1227 
1228 
1229             if (idx > 1 && image->hipass_ver_scantotals[idx-1] > image->hipass_ver_scantotals[idx-2]) {
1230                 SWAP(image->hipass_ver_scantotals[idx-1], image->hipass_ver_scantotals[idx-2]);
1231                 SWAP(image->hipass_ver_scanorder[idx-1], image->hipass_ver_scanorder[idx-2]);
1232             }
1233         }
1234     } else {
1235         int idx;
1236         for (idx = 1 ; idx < 16 ; idx += 1) {
1237             int map = image->hipass_hor_scanorder[idx-1];
1238             dst[idx] = src[map];
1239 
1240             if (dst[idx] == 0)
1241                 continue;
1242 
1243             image->hipass_hor_scantotals[idx-1] += 1;
1244 
1245 
1246             if (idx > 1 && image->hipass_hor_scantotals[idx-1] > image->hipass_hor_scantotals[idx-2]) {
1247                 SWAP(image->hipass_hor_scantotals[idx-1], image->hipass_hor_scantotals[idx-2]);
1248                 SWAP(image->hipass_hor_scanorder[idx-1], image->hipass_hor_scanorder[idx-2]);
1249             }
1250         }
1251     }
1252 }
1253 
1254 /*
1255 * Convert the string of LP values to a compact list of non-zero LP
1256 * values interspersed with the run of zero values. This is what will
1257 * ultimately be encoded. The idea is that an LP set is generally
1258 * sparse and it is more compact to encode the zero-runs then the
1259 * zeros themselves.
1260 */
RunLengthEncode(jxr_image_t image,int RLCoeffs[32],const int LPInput[16])1261 static int RunLengthEncode(jxr_image_t image, int RLCoeffs[32], const int LPInput[16])
1262 {
1263     int lp_index = 1;
1264     int rl_index = 0;
1265     int run_count = 0;
1266 
1267     DEBUG(" RLCoeffs:");
1268     while (lp_index < 16) {
1269         if (LPInput[lp_index] == 0) {
1270             lp_index += 1;
1271             run_count += 1;
1272             continue;
1273         }
1274 
1275         assert(rl_index <= 30);
1276         RLCoeffs[rl_index+0] = run_count;
1277         RLCoeffs[rl_index+1] = LPInput[lp_index];
1278 
1279         DEBUG(" %d--0x%x", RLCoeffs[rl_index+0], RLCoeffs[rl_index+1]);
1280 
1281         rl_index += 2;
1282         run_count = 0;
1283         lp_index += 1;
1284     }
1285 
1286     DEBUG(" num_non_zero=%d\n", rl_index/2);
1287 
1288     /* Return the number of non-zero values. */
1289     return rl_index/2;
1290 }
1291 
collect_lp_input(jxr_image_t image,int ch,int tx,int mx,int LPInput[8][16],int model_bits)1292 static int collect_lp_input(jxr_image_t image, int ch, int tx, int mx, int LPInput[8][16], int model_bits)
1293 {
1294     int num_non_zero = 0;
1295 
1296     int count = 16;
1297     if (ch > 0 && image->use_clr_fmt==2/*YUV422*/)
1298         count = 8;
1299     if (ch > 0 && image->use_clr_fmt==1/*YUV420*/)
1300         count = 4;
1301 
1302     /* Collect into LPInput the coefficient values with the
1303     model_bits stripped out. Be careful to shift only POSITIVE
1304     numbers because the encoding later on uses magnitude-sign
1305     to encode these values. The values can come out differently. */
1306     int k;
1307     for (k = 1 ; k < count ; k += 1) {
1308         int sign_flag = 0;
1309         LPInput[ch][k] = MACROBLK_CUR_LP(image,ch,tx,mx,k-1);
1310         if (LPInput[ch][k] < 0) {
1311             sign_flag = 1;
1312             LPInput[ch][k] = -LPInput[ch][k];
1313         }
1314         LPInput[ch][k] >>= model_bits;
1315         if (LPInput[ch][k] != 0) {
1316             num_non_zero += 1;
1317             if (sign_flag)
1318                 LPInput[ch][k] = -LPInput[ch][k];
1319         }
1320     }
1321 
1322     return num_non_zero;
1323 }
1324 
_jxr_w_MB_LP(jxr_image_t image,struct wbitstream * str,int alpha_flag,unsigned tx,unsigned ty,unsigned mx,unsigned my)1325 void _jxr_w_MB_LP(jxr_image_t image, struct wbitstream*str,
1326                     int alpha_flag,
1327                     unsigned tx, unsigned ty,
1328                     unsigned mx, unsigned my)
1329 {
1330     int LPInput[8][16];
1331     int idx;
1332 
1333     for (idx = 0 ; idx < 8 ; idx += 1) {
1334         int k;
1335         for (k = 0 ; k < 16 ; k += 1)
1336             LPInput[idx][k] = 0;
1337     }
1338 
1339     int lap_mean[2];
1340     lap_mean[0] = 0;
1341     lap_mean[1] = 0;
1342 
1343     DEBUG(" MB_LP tile=[%u %u] mb=[%u %u] bitpos=%zu\n",
1344         tx, ty, mx, my, _jxr_wbitstream_bitpos(str));
1345 
1346     if (_jxr_InitContext(image, tx, ty, mx, my)) {
1347         DEBUG(" Init contexts\n");
1348         _jxr_InitializeCountCBPLP(image);
1349         _jxr_InitLPVLC(image);
1350         _jxr_InitializeAdaptiveScanLP(image);
1351         _jxr_InitializeModelMB(&image->model_lp, 1/*LP*/);
1352     }
1353 
1354     if (_jxr_ResetTotals(image, mx)) {
1355         _jxr_ResetTotalsAdaptiveScanLP(image);
1356     }
1357 
1358     int full_planes = image->num_channels;
1359     if (image->use_clr_fmt==1/*YUV420*/ || image->use_clr_fmt==2/*YUV422*/)
1360         full_planes = 2;
1361 
1362     /* The CBPLP signals whether any non-zero coefficients are
1363     present in the LP band for this macroblock. It is a bitmask
1364     with a bit for each channel. So for example, YONLY, which
1365     has 1 channel, has a 1-bit cbplp. */
1366 
1367     int cbplp = 0;
1368     /* if CLR_FMT is YUV420, YUV422 or YUV444... */
1369     if (image->use_clr_fmt==1 || image->use_clr_fmt==2 || image->use_clr_fmt==3) {
1370         assert(full_planes == 2 || full_planes == 3);
1371         int max = full_planes * 4 - 5;
1372         int model_bits0 = image->model_lp.bits[0];
1373         int model_bits1 = image->model_lp.bits[1];
1374 
1375         int num_non_zero = collect_lp_input(image, 0, tx, mx, LPInput, model_bits0);
1376         if (num_non_zero > 0)
1377             cbplp |= 1;
1378 
1379         switch (image->use_clr_fmt) {
1380             case 1:/*YUV420*/
1381             case 2:/*YUV422*/
1382                 /* YUV422 has a 2-bit CBPLP, the low bit is the
1383                 CBP for Y, and the high bit for UV. */
1384                 num_non_zero = collect_lp_input(image, 1, tx, mx, LPInput, model_bits1);
1385                 if (num_non_zero > 0)
1386                     cbplp |= 2;
1387                 num_non_zero = collect_lp_input(image, 2, tx, mx, LPInput, model_bits1);
1388                 if (num_non_zero > 0)
1389                     cbplp |= 2;
1390                 break;
1391 
1392             case 3:/*YUV444*/
1393                 num_non_zero = collect_lp_input(image, 1, tx, mx, LPInput, model_bits1);
1394                 if (num_non_zero > 0)
1395                     cbplp |= 2;
1396                 num_non_zero = collect_lp_input(image, 2, tx, mx, LPInput, model_bits1);
1397                 if (num_non_zero > 0)
1398                     cbplp |= 4;
1399                 break;
1400         }
1401 
1402         DEBUG(" MB_LP: count_zero_CBPLP=%d, count_max_CBPLP=%d, bitpos=%zu\n",
1403             image->count_zero_CBPLP, image->count_max_CBPLP, _jxr_wbitstream_bitpos(str));
1404         if (image->count_zero_CBPLP <= 0 || image->count_max_CBPLP < 0) {
1405             int cbplp_yuv;
1406             if (image->count_max_CBPLP < image->count_zero_CBPLP) {
1407                 assert(max >= cbplp);
1408                 cbplp_yuv = max - cbplp;
1409             } else {
1410                 cbplp_yuv = cbplp;
1411             }
1412             switch (image->use_clr_fmt) {
1413                 case 1:/*YUV420*/
1414                 case 2:/*YUV422*/
1415                     if (cbplp_yuv == 0) {
1416                         _jxr_wbitstream_uint1(str, 0); /* 0 */
1417                     } else if (cbplp_yuv == 1) {
1418                         _jxr_wbitstream_uint2(str, 2); /* 10 */
1419                     } else {
1420                         _jxr_wbitstream_uint2(str, 3);
1421                         if (cbplp_yuv == 2)
1422                             _jxr_wbitstream_uint1(str, 0); /* 110 */
1423                         else
1424                             _jxr_wbitstream_uint1(str, 1); /* 111 */
1425                     }
1426                     break;
1427 
1428                 case 3:/*YUV444*/
1429                     switch (cbplp_yuv) { /* YUV444 encoding */
1430                 case 0: /* 0 */
1431                     _jxr_wbitstream_uint1(str, 0);
1432                     break;
1433                 case 1: /* 100 */
1434                     _jxr_wbitstream_uint2(str, 2);
1435                     _jxr_wbitstream_uint1(str, 0);
1436                     break;
1437                 default:
1438                     _jxr_wbitstream_uint4(str, 0xaa + cbplp_yuv-2);
1439                     break;
1440                     }
1441                     break;
1442                 default:
1443                     assert(0);
1444             }
1445         } else {
1446             int ch;
1447             for (ch = 0 ; ch < full_planes ; ch += 1) {
1448                 if (cbplp & (1<<(full_planes-1-ch)))
1449                     _jxr_wbitstream_uint1(str, 1);
1450                 else
1451                     _jxr_wbitstream_uint1(str, 0);
1452             }
1453         }
1454         _jxr_UpdateCountCBPLP(image, cbplp, max);
1455 
1456     } else {
1457         int ch;
1458         cbplp = 0;
1459         for (ch = 0 ; ch < image->num_channels ; ch += 1) {
1460             int chroma_flag = ch > 0? 1 : 0;
1461             int model_bits = image->model_lp.bits[chroma_flag];
1462 
1463             DEBUG(" MB_LP: ch=%d, model_bits=%d\n", ch, model_bits);
1464 
1465             int num_non_zero = collect_lp_input(image, ch, tx, mx, LPInput, model_bits);
1466 
1467             /* If there are non-zero coefficients, then CBPLP
1468             for this plane is 1. Also, the CBPLP in the
1469             stream is a single bit for each plane. */
1470             if (num_non_zero > 0) {
1471                 _jxr_wbitstream_uint1(str, 1);
1472                 cbplp |= 1 << ch;
1473             } else {
1474                 _jxr_wbitstream_uint1(str, 0);
1475             }
1476         }
1477     }
1478     DEBUG(" MB_LP: cbplp = 0x%x (full_planes=%u)\n", cbplp, full_planes);
1479 
1480     int ndx;
1481     for (ndx = 0 ; ndx < full_planes ; ndx += 1) {
1482         const int chroma_flag = ndx>0? 1 : 0;
1483 
1484         DEBUG(" MB_LP: process full_plane %u, CBPLP for plane=%d, bitpos=%zu\n",
1485             ndx, (cbplp>>ndx)&1, _jxr_wbitstream_bitpos(str));
1486 #if defined(DETAILED_DEBUG)
1487         if (chroma_flag && image->use_clr_fmt == 2/*YUV422*/) {
1488             int k;
1489             DEBUG(" lp val[ndx=%d] refined data ==", ndx);
1490             for (k = 1 ; k<8; k+=1) {
1491                 DEBUG(" 0x%x/0x%x", MACROBLK_CUR_LP(image,1,tx,mx,k-1), MACROBLK_CUR_LP(image,2,tx,mx,k-1));
1492             }
1493             DEBUG("\n");
1494             DEBUG(" lp val[ndx=%d] before LPPermute ==", ndx);
1495             for (k = 1 ; k<8; k+=1) {
1496                 DEBUG(" 0x%x/0x%x", LPInput[1][k], LPInput[2][k]);
1497             }
1498             DEBUG("\n");
1499 
1500         } else if (chroma_flag && image->use_clr_fmt == 1/*YUV420*/) {
1501             int k;
1502             DEBUG(" lp val[ndx=%d] before LPPermute ==", ndx);
1503             for (k = 1 ; k<4; k+=1) {
1504                 DEBUG(" 0x%x/0x%x", LPInput[1][k], LPInput[2][k]);
1505             }
1506             DEBUG("\n");
1507 
1508         } else {
1509             int k;
1510             DEBUG(" lp val[ndx=%d] refined data ==", ndx);
1511             for (k = 1 ; k<16; k+=1) {
1512                 DEBUG(" 0x%x", MACROBLK_CUR_LP(image,ndx,tx,mx,k-1));
1513             }
1514             DEBUG("\n");
1515             DEBUG(" lp val[ndx=%d] before LPPermute ==", ndx);
1516             for (k = 1 ; k<16; k+=1) {
1517                 DEBUG(" 0x%x", LPInput[ndx][k]);
1518             }
1519             DEBUG("\n");
1520             DEBUG(" scan order ==");
1521             for (k = 0 ; k<15; k+=1) {
1522                 DEBUG(" %2d", image->lopass_scanorder[k]);
1523             }
1524             DEBUG("\n");
1525             DEBUG(" scan totals ==");
1526             for (k = 0 ; k<15; k+=1) {
1527                 DEBUG(" %2d", image->lopass_scantotals[k]);
1528             }
1529             DEBUG("\n");
1530         }
1531 #endif
1532         /* If there are coeff bits, encode them. */
1533         if ((cbplp>>ndx) & 1) {
1534             int LPInput_n[16];
1535 
1536             if (chroma_flag && (image->use_clr_fmt==1 || image->use_clr_fmt==2)) {
1537                 /* LP for YUV42X components are shuffled by
1538                 a fixed permute. */
1539                 FixedLPPermuteUV(image, LPInput_n, LPInput);
1540             } else {
1541                 /* LP for YUV444 components (and the Y of
1542                 all YUV) are adaptively ordered. */
1543                 AdaptiveLPPermute(image, LPInput_n, LPInput[ndx]);
1544             }
1545 
1546             int RLCoeffs[32] = {0};
1547             int num_non_zero = RunLengthEncode(image, RLCoeffs, LPInput_n);
1548             w_DECODE_BLOCK(image, str, 1 /* LP */, chroma_flag, RLCoeffs, num_non_zero);
1549             lap_mean[chroma_flag] += num_non_zero;
1550         }
1551 
1552         /* Emit REFINEMENT bits after the coeff bits are done. */
1553 
1554         int model_bits = image->model_lp.bits[chroma_flag];
1555         if (model_bits) {
1556             DEBUG(" MB_LP: Start refine ndx=%d, model_bits=%d, bitpos=%zu\n",
1557                 ndx, model_bits, _jxr_wbitstream_bitpos(str));
1558             static const int transpose444[16] = { 0, 4, 8,12,
1559                 1, 5, 9,13,
1560                 2, 6,10,14,
1561                 3, 7,11,15 };
1562             if (chroma_flag == 0) {
1563                 int k;
1564                 for (k=1 ;k<16; k+=1) {
1565                     int k_ptr = transpose444[k];
1566                     w_REFINE_LP(str, MACROBLK_CUR_LP(image,ndx,tx,mx,k_ptr-1), model_bits);
1567                 }
1568             } else {
1569                 int k;
1570                 switch (image->use_clr_fmt) {
1571                     case 1: /* YUV420 */
1572                         for (k=1 ; k<4; k+=1) {
1573                             int k_ptr = transpose420[k];
1574                             DEBUG(" MP_LP: Refine LP_Input[1/2][%d] = 0x%x/0x%x bitpos=%zu\n",
1575                                 k_ptr, LPInput[1][k_ptr], LPInput[2][k_ptr],
1576                                 _jxr_wbitstream_bitpos(str));
1577                             w_REFINE_LP(str, MACROBLK_CUR_LP(image,1,tx,mx,k_ptr-1), model_bits);
1578                             w_REFINE_LP(str, MACROBLK_CUR_LP(image,2,tx,mx,k_ptr-1), model_bits);
1579                         }
1580                         break;
1581                     case 2: /* YUV422 */
1582                         for (k=1 ; k<8; k+=1) {
1583                             int k_ptr = transpose422[k];
1584                             DEBUG(" MP_LP: Refine LP_Input[1/2][%d] = 0x%x/0x%x bitpos=%zu\n",
1585                                 k_ptr, LPInput[1][k_ptr], LPInput[2][k_ptr],
1586                                 _jxr_wbitstream_bitpos(str));
1587                             w_REFINE_LP(str, MACROBLK_CUR_LP(image,1,tx,mx,k_ptr-1), model_bits);
1588                             w_REFINE_LP(str, MACROBLK_CUR_LP(image,2,tx,mx,k_ptr-1), model_bits);
1589                         }
1590                         break;
1591                     default: /* All others */
1592                         for (k=1 ;k<16; k+=1) {
1593                             int k_ptr = transpose444[k];
1594                             w_REFINE_LP(str, MACROBLK_CUR_LP(image,ndx,tx,mx,k_ptr-1), model_bits);
1595                         }
1596                         break;
1597                 }
1598             }
1599         }
1600     }
1601 
1602     DEBUG(" MB_LP: UpdateModelMB lap_mean={%d, %d}\n", lap_mean[0], lap_mean[1]);
1603     _jxr_UpdateModelMB(image, lap_mean, &image->model_lp, 1/*band=LP*/);
1604     if (_jxr_ResetContext(image, tx, mx)) {
1605         DEBUG(" AdaptLP at the end of mx=%d (my=%d, ndx=%u)\n", mx, my, ndx);
1606         _jxr_AdaptLP(image);
1607     }
1608 
1609     DEBUG(" MB_LP DONE tile=[%u %u] mb=[%u %u]\n", tx, ty, mx, my);
1610 }
1611 
encode_val_1(jxr_image_t image,struct wbitstream * str,struct adaptive_vlc_s * vlc,int val1,int chr_cbp)1612 static void encode_val_1(jxr_image_t image, struct wbitstream*str,
1613 struct adaptive_vlc_s*vlc, int val1, int chr_cbp)
1614 {
1615     int tmp;
1616 
1617     switch (image->use_clr_fmt) {
1618         case 0: /* YONLY */
1619         case 4: /* YUVK */
1620         case 6: /* NCOMPONENT */
1621             assert(val1 < 5);
1622             if (vlc->table == 0) {
1623                 switch (val1) {
1624                     case 0:
1625                         _jxr_wbitstream_uint1(str, 1);
1626                         break;
1627                     case 1:
1628                         _jxr_wbitstream_uint1(str, 0);
1629                         _jxr_wbitstream_uint1(str, 1);
1630                         break;
1631                     case 2:
1632                         _jxr_wbitstream_uint1(str, 0);
1633                         _jxr_wbitstream_uint1(str, 0);
1634                         _jxr_wbitstream_uint1(str, 1);
1635                         break;
1636                     case 3:
1637                         _jxr_wbitstream_uint4(str, 0);
1638                         break;
1639                     case 4:
1640                         _jxr_wbitstream_uint4(str, 1);
1641                         break;
1642                 }
1643             }
1644             else {
1645                 switch (val1) {
1646                     case 0:
1647                         _jxr_wbitstream_uint1(str, 1);
1648                         break;
1649                     case 1:
1650                         _jxr_wbitstream_uint1(str, 0);
1651                         _jxr_wbitstream_uint2(str, 0);
1652                         break;
1653                     case 2:
1654                         _jxr_wbitstream_uint1(str, 0);
1655                         _jxr_wbitstream_uint2(str, 1);
1656                         break;
1657                     case 3:
1658                         _jxr_wbitstream_uint1(str, 0);
1659                         _jxr_wbitstream_uint2(str, 2);
1660                         break;
1661                     case 4:
1662                         _jxr_wbitstream_uint1(str, 0);
1663                         _jxr_wbitstream_uint2(str, 3);
1664                         break;
1665                 }
1666             }
1667             break;
1668 
1669         case 1: /* YUV420 */
1670         case 2: /* YUV422 */
1671         case 3: /* YUV444 */
1672             tmp = val1;
1673             if (tmp > 8)
1674                 tmp = 8;
1675 
1676             if (vlc->table == 0) {
1677                 switch (tmp) {
1678                     case 0: /* 010 */
1679                         _jxr_wbitstream_uint1(str, 0);
1680                         _jxr_wbitstream_uint2(str, 2);
1681                         break;
1682                     case 1: /* 00000 */
1683                         _jxr_wbitstream_uint1(str, 0);
1684                         _jxr_wbitstream_uint4(str, 0);
1685                         break;
1686                     case 2: /* 0010 */
1687                         _jxr_wbitstream_uint4(str, 2);
1688                         break;
1689                     case 3: /* 00001 */
1690                         _jxr_wbitstream_uint4(str, 0);
1691                         _jxr_wbitstream_uint1(str, 1);
1692                         break;
1693                     case 4: /* 00010 */
1694                         _jxr_wbitstream_uint4(str, 1);
1695                         _jxr_wbitstream_uint1(str, 0);
1696                         break;
1697                     case 5: /* 1 */
1698                         _jxr_wbitstream_uint1(str, 1);
1699                         break;
1700                     case 6: /* 011 */
1701                         _jxr_wbitstream_uint1(str, 0);
1702                         _jxr_wbitstream_uint2(str, 3);
1703                         break;
1704                     case 7: /* 00011 */
1705                         _jxr_wbitstream_uint4(str, 1);
1706                         _jxr_wbitstream_uint1(str, 1);
1707                         break;
1708                     case 8: /* 0011 */
1709                         _jxr_wbitstream_uint4(str, 3);
1710                         break;
1711                 }
1712             }
1713             else {
1714                 switch (tmp) {
1715                     case 0: /* 1 */
1716                         _jxr_wbitstream_uint1(str, 1);
1717                         break;
1718                     case 1: /* 001 */
1719                         _jxr_wbitstream_uint2(str, 0);
1720                         _jxr_wbitstream_uint1(str, 1);
1721                         break;
1722                     case 2: /* 010 */
1723                         _jxr_wbitstream_uint2(str, 1);
1724                         _jxr_wbitstream_uint1(str, 0);
1725                         break;
1726                     case 3: /* 0001 */
1727                         _jxr_wbitstream_uint4(str, 1);
1728                         break;
1729                     case 4: /* 0000 01 */
1730                         _jxr_wbitstream_uint4(str, 0);
1731                         _jxr_wbitstream_uint2(str, 1);
1732                         break;
1733                     case 5: /* 011 */
1734                         _jxr_wbitstream_uint1(str, 0);
1735                         _jxr_wbitstream_uint2(str, 3);
1736                         break;
1737                     case 6: /* 0000 1 */
1738                         _jxr_wbitstream_uint4(str, 0);
1739                         _jxr_wbitstream_uint1(str, 1);
1740                         break;
1741                     case 7: /* 0000 000 */
1742                         _jxr_wbitstream_uint4(str, 0);
1743                         _jxr_wbitstream_uint2(str, 0);
1744                         _jxr_wbitstream_uint1(str, 0);
1745                         break;
1746                     case 8: /* 0000 001 */
1747                         _jxr_wbitstream_uint4(str, 0);
1748                         _jxr_wbitstream_uint2(str, 0);
1749                         _jxr_wbitstream_uint1(str, 1);
1750                         break;
1751                 }
1752             }
1753 
1754             if (tmp >= 5) {
1755                 DEBUG(" MB_CBP: CHR_CBP=%x\n", chr_cbp);
1756                 assert(chr_cbp < 3);
1757                 switch (chr_cbp) {
1758                     case 0: /* 1 */
1759                         _jxr_wbitstream_uint1(str, 1);
1760                         break;
1761                     case 1: /* 01 */
1762                         _jxr_wbitstream_uint2(str, 1);
1763                         break;
1764                     case 2: /* 00 */
1765                         _jxr_wbitstream_uint2(str, 0);
1766                         break;
1767                 }
1768             }
1769 
1770             if (tmp == 8) {
1771                 assert((val1 - tmp) <= 2);
1772                 int val_inc = val1 - tmp;
1773                 DEBUG(" MB_CBP: VAL_INC=%d\n", val_inc);
1774                 switch (val_inc) {
1775                     case 0: /* 1 */
1776                         _jxr_wbitstream_uint1(str, 1);
1777                         break;
1778                     case 1: /* 01 */
1779                         _jxr_wbitstream_uint2(str, 1);
1780                         break;
1781                     case 2: /* 00 */
1782                         _jxr_wbitstream_uint2(str, 0);
1783                         break;
1784                 }
1785             }
1786             break;
1787         default:
1788             assert(0);
1789     }
1790 }
1791 
_jxr_w_MB_CBP(jxr_image_t image,struct wbitstream * str,int alpha_flag,unsigned tx,unsigned ty,unsigned mx,unsigned my)1792 void _jxr_w_MB_CBP(jxr_image_t image, struct wbitstream*str,
1793                      int alpha_flag,
1794                      unsigned tx, unsigned ty,
1795                      unsigned mx, unsigned my)
1796 {
1797     DEBUG(" MB_CBP tile=[%u %u] mb=[%u %u] bitpos=%zu\n",
1798         tx, ty, mx, my, _jxr_wbitstream_bitpos(str));
1799 
1800     if (_jxr_InitContext(image, tx, ty, mx, my)) {
1801         DEBUG(" MB_CBP: InitContext\n");
1802         /* This happens only at the top left edge of the tile. */
1803         _jxr_InitCBPVLC(image);
1804     }
1805 
1806     /* "Channels" is not quite the same as "planes". For the
1807     purposes of CBP parsing, a color image has 1 channel. */
1808     int channels = 1;
1809     if (image->use_clr_fmt==4/*YUVK*/ || image->use_clr_fmt==6/*NCOMPONENT*/)
1810         channels = image->num_channels;
1811 
1812     /* Get the diff_cbp values for all the channels. */
1813     int diff_cbp[MAX_CHANNELS];
1814     int ch;
1815     for (ch = 0 ; ch < image->num_channels ; ch += 1)
1816         diff_cbp[ch] = MACROBLK_CUR(image,ch,tx,mx).hp_diff_cbp;
1817 
1818     /* Now process the diff_cbp values for the channels. Note that
1819     if this is a YUV image, then channels is 1 and the loop
1820     knows to process three planes at once. */
1821     for (ch = 0 ; ch < channels ; ch += 1) {
1822 
1823         /* The CBP Block mask is a 4-bit mask that indicates
1824         which bit-blocks of the 16bit diff_cbp has non-zero
1825         bits in them. The cbp_X_block_masks are 4bit values
1826         that indicate which nibbles of the 16bit diff_cbp
1827         values have bits set. */
1828         int cbp_Y_block_mask = 0;
1829         int cbp_U_block_mask = 0;
1830         int cbp_V_block_mask = 0;
1831         if (diff_cbp[ch] & 0x000f)
1832             cbp_Y_block_mask |= 0x001;
1833         if (diff_cbp[ch] & 0x00f0)
1834             cbp_Y_block_mask |= 0x002;
1835         if (diff_cbp[ch] & 0x0f00)
1836             cbp_Y_block_mask |= 0x004;
1837         if (diff_cbp[ch] & 0xf000)
1838             cbp_Y_block_mask |= 0x008;
1839 
1840 
1841         switch (image->use_clr_fmt) {
1842             /* If thie is a YUV420 image, then the block mask
1843             includes masks for all the 3 planes. The UV
1844             planes' have only 1 CBP bit per block, so this
1845             is simple. */
1846             case 1: /* YUV420 */
1847                 if (diff_cbp[1] & 0x01)
1848                     cbp_U_block_mask |= 0x01;
1849                 if (diff_cbp[1] & 0x02)
1850                     cbp_U_block_mask |= 0x02;
1851                 if (diff_cbp[1] & 0x04)
1852                     cbp_U_block_mask |= 0x04;
1853                 if (diff_cbp[1] & 0x08)
1854                     cbp_U_block_mask |= 0x08;
1855                 if (diff_cbp[2] & 0x01)
1856                     cbp_V_block_mask |= 0x01;
1857                 if (diff_cbp[2] & 0x02)
1858                     cbp_V_block_mask |= 0x02;
1859                 if (diff_cbp[2] & 0x04)
1860                     cbp_V_block_mask |= 0x04;
1861                 if (diff_cbp[2] & 0x08)
1862                     cbp_V_block_mask |= 0x08;
1863                 break;
1864                 /* If this is a YUV422 iamge, then the block mask
1865                 includes masks for all the 3 planes, with the
1866                 UV diff_cbp bits encoded funny. */
1867             case 2: /* YUV422*/
1868                 if (diff_cbp[1] & 0x05)
1869                     cbp_U_block_mask |= 0x01;
1870                 if (diff_cbp[1] & 0x0a)
1871                     cbp_U_block_mask |= 0x02;
1872                 if (diff_cbp[1] & 0x50)
1873                     cbp_U_block_mask |= 0x04;
1874                 if (diff_cbp[1] & 0xa0)
1875                     cbp_U_block_mask |= 0x08;
1876                 if (diff_cbp[2] & 0x05)
1877                     cbp_V_block_mask |= 0x01;
1878                 if (diff_cbp[2] & 0x0a)
1879                     cbp_V_block_mask |= 0x02;
1880                 if (diff_cbp[2] & 0x50)
1881                     cbp_V_block_mask |= 0x04;
1882                 if (diff_cbp[2] & 0xa0)
1883                     cbp_V_block_mask |= 0x08;
1884                 break;
1885                 /* If this is a YUV444 image, then the block mask
1886                 includes masks for all 3 planes. */
1887             case 3: /*YUV444*/
1888                 assert(ch == 0);
1889                 if (diff_cbp[1] & 0x000f)
1890                     cbp_U_block_mask |= 0x01;
1891                 if (diff_cbp[1] & 0x00f0)
1892                     cbp_U_block_mask |= 0x02;
1893                 if (diff_cbp[1] & 0x0f00)
1894                     cbp_U_block_mask |= 0x04;
1895                 if (diff_cbp[1] & 0xf000)
1896                     cbp_U_block_mask |= 0x08;
1897                 if (diff_cbp[2] & 0x000f)
1898                     cbp_V_block_mask |= 0x01;
1899                 if (diff_cbp[2] & 0x00f0)
1900                     cbp_V_block_mask |= 0x02;
1901                 if (diff_cbp[2] & 0x0f00)
1902                     cbp_V_block_mask |= 0x04;
1903                 if (diff_cbp[2] & 0xf000)
1904                     cbp_V_block_mask |= 0x08;
1905                 break;
1906             default:
1907                 break;
1908         }
1909 
1910         DEBUG(" MB_CBP: diff_cbp[%d]=0x%x (HP_CBP=0x%x) cbp_block_mask=0x%x:%x:%x\n",
1911             ch, diff_cbp[ch], MACROBLK_CUR_HPCBP(image,ch,tx,mx),
1912             cbp_Y_block_mask, cbp_U_block_mask, cbp_V_block_mask);
1913 
1914         /* A bit in the CBP (up to 4 bits) is true if the
1915         corresponding bit of any of hte YUV planes' cbp is
1916         set. That tells the decoder to look for CBP values
1917         in any of the planes. */
1918         int cbp = cbp_Y_block_mask | cbp_U_block_mask | cbp_V_block_mask;
1919         DEBUG(" MB_CBP: Refined CBP=0x%x\n", cbp);
1920         w_REFINE_CBP(image, str, cbp);
1921 
1922         int block;
1923         for (block = 0 ; block < 4 ; block += 1) {
1924             /* If there are no diff_cbp bits in this nibble of
1925             any of the YUV planes, then skip. */
1926             if ((cbp & (1<<block)) == 0)
1927                 continue;
1928 
1929             struct adaptive_vlc_s*vlc = image->vlc_table + DecNumBlkCBP;
1930             assert(vlc->deltatable == 0);
1931 
1932             DEBUG(" MB_CBP: block=%d Use DecNumBlkCBP table=%d, discriminant=%d, bitpos=%zu\n",
1933                 block, vlc->table, vlc->discriminant, _jxr_wbitstream_bitpos(str));
1934 
1935             /* This is the block of CBP bits to encode. The
1936             blkcbp is the nibble (indexed by "block") of
1937             the diff_cbp for the Y plane, also also bits
1938             set if the UV diff_cbp values have bits
1939             set. The cbp_chr_X masks have the nibbles of
1940             the UV diff_cbp. */
1941             int blkcbp = (diff_cbp[ch] >> 4*block) & 0x000f;
1942 
1943             if (cbp_U_block_mask & (1<<block))
1944                 blkcbp |= 0x10;
1945             if (cbp_V_block_mask & (1<<block))
1946                 blkcbp |= 0x20;
1947 
1948             /* Map the CBP bit block to a more encodable
1949             code. Note that this map doesn't look at the
1950             chroma mask bits because this code is used to
1951             generate only the low 4 bits by the receiver. */
1952             static const int code_from_blkcbp[16] = {
1953                 0, 4, 5, 2,
1954                 6, 8, 9,12,
1955                 7,10,11,13,
1956                 3,14,15, 1 };
1957                 int code = code_from_blkcbp[blkcbp&0x0f];
1958                 /* Break the code down to a further encoded val
1959                 and refinement bit count. */
1960                 static const int val_from_code[16] = {
1961                     0, 5, 2, 2,
1962                     1, 1, 1, 1,
1963                     3, 3, 3, 3,
1964                     4, 4, 4, 4 };
1965                     int val = val_from_code[code];
1966                     int chr_cbp = (blkcbp >> 4) & 0x3;
1967                     if (chr_cbp > 0) {
1968                         chr_cbp -= 1;
1969                         val += 6;
1970                     }
1971 
1972                     assert(val > 0);
1973                     int num_blkcbp = val-1;
1974 
1975                     DEBUG(" MB_CBP: NUM_BLKCBP=%d, iVal=%d, iCode=%d\n", num_blkcbp, val, code);
1976                     DEBUG(" MB_CBP: blkcbp=0x%x, code=%d for chunk blk=%d\n", blkcbp, code, block);
1977 
1978                     /* Adapt DecNumBlkCBP based on the num_blkcbp value. */
1979                     if (image->use_clr_fmt==0 || image->use_clr_fmt==4 || image->use_clr_fmt==6) {
1980                         assert(num_blkcbp < 5);
1981                         static const int Num_BLKCBP_Delta5[5] = {0, -1, 0, 1, 1};
1982                         vlc->discriminant += Num_BLKCBP_Delta5[num_blkcbp];
1983                     } else {
1984                         int tmp = val-1;
1985                         if (tmp > 8) {
1986                             assert(tmp < 11);
1987                             tmp = 8;
1988                         }
1989                         assert(tmp < 9);
1990                         static const int Num_BLKCBP_Delta9[9] = {2, 2, 1, 1, -1, -2, -2, -2, -3};
1991                         vlc->discriminant += Num_BLKCBP_Delta9[tmp];
1992                     }
1993 
1994                     DEBUG(" MB_CBP: NUM_BLKCBP=%d, discriminant becomes=%d, \n",
1995                         num_blkcbp, vlc->discriminant);
1996 
1997                     /* Encode VAL-1, and CHR_CBP if present. */
1998                     encode_val_1(image, str, vlc, val-1, chr_cbp);
1999 
2000                     /* Encode CODE_INC */
2001                     static const int code_inc_from_code[16] = {
2002                         0, 0, 0, 1,
2003                         0, 1, 2, 3,
2004                         0, 1, 2, 3,
2005                         0, 1, 2, 3 };
2006                         static const int code_inc_bits_from_code[16] = {
2007                             0, 0, 1, 1,
2008                             2, 2, 2, 2,
2009                             2, 2, 2, 2,
2010                             2, 2, 2, 2 };
2011                             assert(code < 16);
2012                             switch (code_inc_bits_from_code[code]) {
2013                                 case 0:
2014                                     break;
2015                                 case 1:
2016                                     _jxr_wbitstream_uint1(str, code_inc_from_code[code]);
2017                                     break;
2018                                 case 2:
2019                                     _jxr_wbitstream_uint2(str, code_inc_from_code[code]);
2020                                     break;
2021                                 default:
2022                                     assert(0);
2023                             }
2024 
2025                             int cbp_chr_U = 0;
2026                             int cbp_chr_V = 0;
2027                             switch (image->use_clr_fmt) {
2028                                 case 0: /* YONLY */
2029                                 case 4: /* YUVK */
2030                                 case 6: /* NCOMPONENT */
2031                                     break;
2032                                 case 1:/* YUV420 */
2033                                     /* Nothing to encode. The CHR_CBP bits are
2034                                     sufficient to carry all the UV CBP
2035                                     informtation for YUV420 UV planes. */
2036                                     break;
2037                                 case 2: /* YUV422 */
2038                                     if (blkcbp & 0x10)
2039                                         refine_cbp_chr422(image, str, diff_cbp[1], block);
2040                                     if (blkcbp & 0x20)
2041                                         refine_cbp_chr422(image, str, diff_cbp[2], block);
2042                                     break;
2043                                 case 3: /* YUV444 */
2044                                     cbp_chr_U = (diff_cbp[1] >> 4*block) & 0x000f;
2045                                     cbp_chr_V = (diff_cbp[2] >> 4*block) & 0x000f;
2046                                     if (blkcbp & 0x10) {
2047                                         DEBUG(" MB_CBP: Refined CBP_U=0x%x\n", cbp_chr_U);
2048                                         w_REFINE_CBP_CHR(image, str, cbp_chr_U);
2049                                     }
2050                                     if (blkcbp & 0x20) {
2051                                         DEBUG(" MB_CBP: Refined CBP_V=0x%x\n", cbp_chr_V);
2052                                         w_REFINE_CBP_CHR(image, str, cbp_chr_V);
2053                                     }
2054                                     break;
2055                                 default:
2056                                     assert(0);
2057                                     break;
2058                             }
2059         }
2060     }
2061 
2062     DEBUG(" MB_CBP done tile=[%u %u] mb=[%u %u]\n", tx, ty, mx, my);
2063 }
2064 
refine_cbp_chr422(jxr_image_t image,struct wbitstream * str,int diff_cbp,int block)2065 static void refine_cbp_chr422(jxr_image_t image, struct wbitstream*str, int diff_cbp, int block)
2066 {
2067     static const int shift_mask[4] = {0, 1, 4, 5};
2068     int bits = diff_cbp >> shift_mask[block];
2069     switch (bits & 5) {
2070         case 0:
2071             assert(0);
2072             break;
2073         case 1: /* 1 */
2074             _jxr_wbitstream_uint1(str, 1);
2075             break;
2076         case 4: /* 01 */
2077             _jxr_wbitstream_uint2(str, 1);
2078             break;
2079         case 5: /* 00 */
2080             _jxr_wbitstream_uint2(str, 0);
2081             break;
2082     }
2083 }
2084 
refine_cbp_core(jxr_image_t image,struct wbitstream * str,int cbp_mask)2085 static void refine_cbp_core(jxr_image_t image, struct wbitstream*str, int cbp_mask)
2086 {
2087     switch (cbp_mask) {
2088         case 0x0:
2089             /* If there are no CBP bits, then encode nothing. */
2090         case 0xf:
2091             break;
2092             /* REF_CBP (num_cbp==1) */
2093         case 0x1:
2094             _jxr_wbitstream_uint2(str, 0);
2095             break;
2096         case 0x2:
2097             _jxr_wbitstream_uint2(str, 1);
2098             break;
2099         case 0x4:
2100             _jxr_wbitstream_uint2(str, 2);
2101             break;
2102         case 0x8:
2103             _jxr_wbitstream_uint2(str, 3);
2104             break;
2105             /* REF_CBP1 (num_cbp==2) */
2106         case 0x3:
2107             _jxr_wbitstream_uint2(str, 0);
2108             break;
2109         case 0x5:
2110             _jxr_wbitstream_uint2(str, 1);
2111             break;
2112         case 0x6:
2113             _jxr_wbitstream_uint1(str, 1);
2114             _jxr_wbitstream_uint2(str, 0);
2115             break;
2116         case 0x9:
2117             _jxr_wbitstream_uint1(str, 1);
2118             _jxr_wbitstream_uint2(str, 1);
2119             break;
2120         case 0xa:
2121             _jxr_wbitstream_uint1(str, 1);
2122             _jxr_wbitstream_uint2(str, 2);
2123             break;
2124         case 0xc:
2125             _jxr_wbitstream_uint1(str, 1);
2126             _jxr_wbitstream_uint2(str, 3);
2127             break;
2128             /* REF_CBP (num_cbp==3) */
2129         case 0xe:
2130             _jxr_wbitstream_uint2(str, 0);
2131             break;
2132         case 0xd:
2133             _jxr_wbitstream_uint2(str, 1);
2134             break;
2135         case 0xb:
2136             _jxr_wbitstream_uint2(str, 2);
2137             break;
2138         case 0x7:
2139             _jxr_wbitstream_uint2(str, 3);
2140             break;
2141     }
2142 }
2143 
w_REFINE_CBP(jxr_image_t image,struct wbitstream * str,int cbp_block_mask)2144 static void w_REFINE_CBP(jxr_image_t image, struct wbitstream*str, int cbp_block_mask)
2145 {
2146     DEBUG(" REFINE_CBP: input CBP=%d (0x%x)\n", cbp_block_mask, cbp_block_mask);
2147 
2148     int num_cbp = 0;
2149     int idx;
2150     for (idx = 0 ; idx < 4 ; idx += 1) {
2151         if (cbp_block_mask & (1<<idx))
2152             num_cbp += 1;
2153     }
2154 
2155     struct adaptive_vlc_s*vlc = image->vlc_table + DecNumCBP;
2156 
2157     assert(vlc->deltatable == 0 && num_cbp < 5);
2158     static const int Num_CBP_Delta[5] = {0, -1, 0, 1, 1};
2159     vlc->discriminant += Num_CBP_Delta[num_cbp];
2160 
2161 
2162     /* First encode the NUM_CBP. (The REFINE_CBP in the spec does
2163     not include this step, but we include it here because it is
2164     part of the cbp_block_mask encoding.) */
2165     int vlc_table = vlc->table;
2166     assert(vlc_table < 2);
2167 
2168     DEBUG(" REFINE_CBP: num_cbp=%d, vlc table=%d\n", num_cbp, vlc_table);
2169 
2170     if (vlc_table == 0) {
2171         switch (num_cbp) {
2172             case 4:
2173                 _jxr_wbitstream_uint1(str, 0);
2174             case 2:
2175                 _jxr_wbitstream_uint1(str, 0);
2176             case 1:
2177                 _jxr_wbitstream_uint1(str, 0);
2178             case 0:
2179                 _jxr_wbitstream_uint1(str, 1);
2180                 break;
2181             case 3:
2182                 _jxr_wbitstream_uint1(str, 0);
2183                 _jxr_wbitstream_uint1(str, 0);
2184                 _jxr_wbitstream_uint1(str, 0);
2185                 _jxr_wbitstream_uint1(str, 0);
2186                 break;
2187         }
2188     }
2189     else {
2190         switch (num_cbp) {
2191             case 0:
2192                 _jxr_wbitstream_uint1(str, 1);
2193                 break;
2194             default:
2195                 _jxr_wbitstream_uint1(str, 0);
2196                 _jxr_wbitstream_uint2(str, num_cbp-1);
2197                 break;
2198         }
2199     }
2200 
2201     /* Now encode the CBP itself. Note that many encodings look
2202     the same. The decoder uses the NUM_CBP encoded above to
2203     distinguish between the different possible values. */
2204     refine_cbp_core(image, str, cbp_block_mask);
2205 }
2206 
w_REFINE_CBP_CHR(jxr_image_t image,struct wbitstream * str,int cbp_block_mask)2207 static void w_REFINE_CBP_CHR(jxr_image_t image, struct wbitstream*str, int cbp_block_mask)
2208 {
2209     DEBUG(" REFINE_CBP: input CBP(CHR)= 0x%x)\n", cbp_block_mask);
2210 
2211     int num_cbp = 0;
2212     int idx;
2213     for (idx = 0 ; idx < 4 ; idx += 1) {
2214         if (cbp_block_mask & (1<<idx))
2215             num_cbp += 1;
2216     }
2217 
2218     /* If refining a CBP block mask for a chroma plane, then we
2219     know that there must be at least one block bit set, so we
2220     can encode num_cbp-1 instead, and possibly save space. */
2221     assert(num_cbp > 0);
2222     num_cbp -= 1;
2223 
2224     /* First encode the NUM_CBP. (The REFINE_CBP in the spec does
2225     not include this step, but we include it here because it is
2226     part of the cbp_block_mask encoding.) */
2227     DEBUG(" REFINE_CBP(CHR): num_ch_blk=%d\n", num_cbp);
2228 
2229     assert(num_cbp < 4);
2230     switch (num_cbp) {
2231         case 0: /* 1 */
2232             _jxr_wbitstream_uint1(str,1);
2233             break;
2234         case 1: /* 01 */
2235             _jxr_wbitstream_uint2(str, 1);
2236             break;
2237         case 2: /* 000 */
2238             _jxr_wbitstream_uint2(str, 0);
2239             _jxr_wbitstream_uint1(str, 0);
2240             break;
2241         case 3: /* 001 */
2242             _jxr_wbitstream_uint2(str, 0);
2243             _jxr_wbitstream_uint1(str, 1);
2244             break;
2245     }
2246 
2247     /* Now encode the CBP itself. Note that many encodings look
2248     the same. The decoder uses the NUM_CBP encoded above to
2249     distinguish between the different possible values. */
2250     refine_cbp_core(image, str, cbp_block_mask);
2251 }
2252 
2253 
_jxr_w_MB_HP(jxr_image_t image,struct wbitstream * str,int alpha_flag,unsigned tx,unsigned ty,unsigned mx,unsigned my,struct wbitstream * strFB)2254 int _jxr_w_MB_HP(jxr_image_t image, struct wbitstream*str,
2255                    int alpha_flag,
2256                    unsigned tx, unsigned ty,
2257                    unsigned mx, unsigned my,
2258                    struct wbitstream*strFB)
2259 {
2260     /* This function can act either as MB_HP() or MB_HP_FLEX() */
2261     DEBUG(" MB_HP tile=[%u %u] mb=[%u %u] bitpos=%zu\n",
2262         tx, ty, mx, my, _jxr_wbitstream_bitpos(str));
2263 
2264     if (_jxr_InitContext(image, tx, ty, mx, my)) {
2265         DEBUG(" MB_HP: InitContext\n");
2266         /* This happens only at the top left edge of the tile. */
2267         _jxr_InitHPVLC(image);
2268         _jxr_InitializeAdaptiveScanHP(image);
2269     }
2270 
2271     if (_jxr_ResetTotals(image, mx)) {
2272         _jxr_ResetTotalsAdaptiveScanHP(image);
2273     }
2274 
2275     /* FLEXBITS are embedded in the HP data if there are FLEXBITS
2276     present in the bitstream AND we are in SPATIAL (not
2277     FREQUENCY) mode. */
2278     int flex_flag = 1;
2279     if (image->bands_present == 1) /* NOFLEXBITS */
2280         flex_flag = 0;
2281     if (FREQUENCY_MODE_CODESTREAM_FLAG(image) != 0) /* FREQUENCY_MODE */
2282         flex_flag = 0;
2283 
2284     int mbhp_pred_mode = MACROBLK_CUR(image,0,tx,mx).mbhp_pred_mode;
2285 
2286     int idx;
2287     for (idx = 0 ; idx < image->num_channels; idx += 1) {
2288         int chroma_flag = idx>0? 1 : 0;
2289         int nblocks = 4;
2290         if (chroma_flag && image->use_clr_fmt==1/*YUV420*/)
2291             nblocks = 1;
2292         else if (chroma_flag && image->use_clr_fmt==2/*YUV422*/)
2293             nblocks = 2;
2294 
2295         unsigned model_bits = MACROBLK_CUR(image,0,tx,mx).hp_model_bits[chroma_flag];
2296         int cbp = MACROBLK_CUR_HPCBP(image, idx, tx, mx);
2297         int block;
2298         DEBUG(" MB_HP channel=%d, cbp=0x%x, model_bits=%u MBHPMode=%d\n",
2299             idx, cbp, model_bits, mbhp_pred_mode);
2300         for (block=0 ; block<(nblocks*4) ; block += 1, cbp >>= 1) {
2301             int bpos = block;
2302             /* Only remap the Y plane of YUV42X data. */
2303             if (nblocks == 4)
2304                 bpos = _jxr_hp_scan_map[block];
2305             int num_nonzero = w_DECODE_BLOCK_ADAPTIVE(image, str, tx, mx,
2306                 cbp&1, chroma_flag,
2307                 idx, bpos, mbhp_pred_mode,
2308                 model_bits);
2309             if (num_nonzero < 0) {
2310                 DEBUG("ERROR: r_DECODE_BLOCK_ADAPTIVE returned rc=%d\n", num_nonzero);
2311                 return JXR_EC_ERROR;
2312             }
2313             if (strFB)
2314                 w_BLOCK_FLEXBITS(image, strFB, tx, ty, mx, my,
2315                 idx, bpos, model_bits);
2316             else if (flex_flag)
2317                 w_BLOCK_FLEXBITS(image, str, tx, ty, mx, my,
2318                 idx, bpos, model_bits);
2319         }
2320 
2321     }
2322 
2323     if (_jxr_ResetContext(image, tx, mx)) {
2324         DEBUG(" MB_HP: Run AdaptHP\n");
2325         _jxr_AdaptHP(image);
2326     }
2327 
2328     DEBUG(" MB_HP DONE tile=[%u %u] mb=[%u %u]\n", tx, ty, mx, my);
2329     return 0;
2330 }
2331 
w_DECODE_FLEX(jxr_image_t image,struct wbitstream * str,unsigned tx,unsigned mx,int ch,unsigned block,unsigned k,unsigned flexbits)2332 static void w_DECODE_FLEX(jxr_image_t image, struct wbitstream*str,
2333                           unsigned tx, unsigned mx,
2334                           int ch, unsigned block, unsigned k,
2335                           unsigned flexbits)
2336 {
2337     int coeff = MACROBLK_CUR_HP(image, ch, tx, mx, block, k);
2338     int sign = 0;
2339     if (coeff < 0) {
2340         sign = 1;
2341         coeff = -coeff;
2342     }
2343 
2344     coeff >>= image->trim_flexbits;
2345     int mask = (1 << flexbits) - 1;
2346 
2347     int flex_ref = coeff & mask;
2348     coeff &= ~mask;
2349     DEBUG(" DECODE_FLEX: coeff=0x%08x, flex_ref=0x%08x\n", coeff, flex_ref);
2350 
2351     _jxr_wbitstream_uintN(str, flex_ref, flexbits);
2352 
2353     if (coeff == 0 && flex_ref != 0)
2354         _jxr_wbitstream_uint1(str, sign);
2355 }
2356 
w_BLOCK_FLEXBITS(jxr_image_t image,struct wbitstream * str,unsigned tx,unsigned ty,unsigned mx,unsigned my,unsigned ch,unsigned bl,unsigned model_bits)2357 static void w_BLOCK_FLEXBITS(jxr_image_t image, struct wbitstream*str,
2358                              unsigned tx, unsigned ty,
2359                              unsigned mx, unsigned my,
2360                              unsigned ch, unsigned bl, unsigned model_bits)
2361 {
2362     const int transpose444 [16] = {0, 4, 8,12,
2363         1, 5, 9,13,
2364         2, 6,10,14,
2365         3, 7,11,15 };
2366     unsigned flexbits_left = model_bits;
2367     if (image->trim_flexbits > flexbits_left)
2368         flexbits_left = 0;
2369     else
2370         flexbits_left -= image->trim_flexbits;
2371 
2372     DEBUG(" BLOCK_FLEXBITS: flexbits_left=%u (model=%u, trim=%u) block=%u bitpos=%zu\n",
2373         flexbits_left, model_bits, image->trim_flexbits, bl, _jxr_wbitstream_bitpos(str));
2374 
2375     if (flexbits_left > 0) {
2376         int idx;
2377         for (idx = 1 ; idx < 16 ; idx += 1) {
2378             int idx_trans = transpose444[idx];
2379             w_DECODE_FLEX(image, str, tx, mx, ch, bl, idx_trans-1, flexbits_left);
2380         }
2381     }
2382 
2383     DEBUG(" BLOCK_FLEXBITS done\n");
2384 }
2385 
w_DEC_DC(jxr_image_t image,struct wbitstream * str,int model_bits,int chroma_flag,int is_dc_ch,int32_t dc_val)2386 static void w_DEC_DC(jxr_image_t image, struct wbitstream*str,
2387                      int model_bits, int chroma_flag, int is_dc_ch,
2388                      int32_t dc_val)
2389 {
2390     int idx;
2391     int sign_flag = 0;
2392     int zero_flag = dc_val==0;
2393 
2394     DEBUG(" DEC_DC: DC value is %d (0x%08x)\n", dc_val, dc_val);
2395 
2396     /* Values are encoded as magnitude/sign, and *not* the 2s
2397     complement value. So here we get the sign of the value and
2398     convert it to a magnitude for encoding. */
2399     if (dc_val < 0) {
2400         sign_flag = 1;
2401         dc_val = -dc_val;
2402     }
2403 
2404     /* Pull the LSB bits from the value and save them in a stack
2405     of DC_REF values. This reduces the number of bits in the dc
2406     val that will be encoded. */
2407     uint32_t dc_ref_stack = 0;
2408     for (idx = 0 ; idx < model_bits ; idx += 1) {
2409         dc_ref_stack <<= 1;
2410         dc_ref_stack |= dc_val&1;
2411         dc_val >>= 1;
2412     }
2413 
2414     /* The is_dc_ch flag only gates the encoding of the high bits
2415     of the absolute level. If the level is non-zero, then
2416     presumably the is_dc_ch flag is true and we go ahead and
2417     encode the level. The model_bits are encoded in any case. */
2418     if (dc_val != 0) {
2419         assert( is_dc_ch );
2420         DEBUG(" DEC_DC: DECODE_ABS_LEVEL = %d (0x%08x)\n", dc_val, dc_val);
2421         w_DECODE_ABS_LEVEL(image, str, 0/*DC*/, chroma_flag, dc_val + 1);
2422     }
2423 
2424     /* Play back the bits in the dc_ref stack. This causes them to
2425     appear MSB first in the DC_REF field. These are the
2426     model_bits, the LSB of the level. */
2427     for (idx = 0 ; idx < model_bits ; idx += 1) {
2428         _jxr_wbitstream_uint1(str, dc_ref_stack&1);
2429         dc_ref_stack >>= 1;
2430     }
2431 
2432     /* The sign bit is last. Include it only if the value is nonzero*/
2433     if (!zero_flag) {
2434         DEBUG(" DEC_DC: sign_flag=%s\n", sign_flag? "true":"false");
2435         _jxr_wbitstream_uint1(str, sign_flag);
2436     }
2437 }
2438 
2439 static void encode_abslevel_index(jxr_image_t image, struct wbitstream*str,
2440                                   int abslevel_index, int vlc_select);
2441 
2442 /*
2443 * This function actually *ENCODES* the ABS_LEVEL.
2444 */
w_DECODE_ABS_LEVEL(jxr_image_t image,struct wbitstream * str,int band,int chroma_flag,uint32_t level)2445 static void w_DECODE_ABS_LEVEL(jxr_image_t image, struct wbitstream*str,
2446                                int band, int chroma_flag, uint32_t level)
2447 {
2448     int vlc_select = _jxr_vlc_select(band, chroma_flag);
2449     DEBUG(" Use vlc_select = %s (table=%d) to encode level index, bitpos=%zu\n",
2450         _jxr_vlc_index_name(vlc_select), image->vlc_table[vlc_select].table,
2451         _jxr_wbitstream_bitpos(str));
2452 
2453     const uint32_t abslevel_limit[6] = { 2, 3, 5, 9, 13, 17 };
2454     const uint32_t abslevel_remap[6] = {2, 3, 4, 6, 10, 14};
2455     const int abslevel_fixedlen[6] = {0, 0, 1, 2, 2, 2};
2456 
2457     /* choose the smallest level index that can carry "level". the
2458     abslevel_limit array holds the maximim value that each
2459     index can encode. */
2460     int abslevel_index = 0;
2461     while (abslevel_index < 6 && abslevel_limit[abslevel_index] < level) {
2462         abslevel_index += 1;
2463     }
2464 
2465     DEBUG(" ABSLEVEL_INDEX = %d\n", abslevel_index);
2466     encode_abslevel_index(image, str, abslevel_index, vlc_select);
2467 
2468     image->vlc_table[vlc_select].discriminant += _jxr_abslevel_index_delta[abslevel_index];
2469 
2470     if (abslevel_index < 6) {
2471         /* The level index encodes most of the level value. The
2472         abslevel_remap is the actual value that the index
2473         encodes. The fixedlen array then gives the number of
2474         extra bits available to encode the last bit of
2475         value. This *must* be enough. */
2476 
2477         int idx;
2478         int fixedlen = abslevel_fixedlen[abslevel_index];
2479         uint32_t level_ref = level - abslevel_remap[abslevel_index];
2480 
2481         DEBUG(" ABS_LEVEL = 0x%x (fixed = %d, level_ref = %d)\n",
2482             level, fixedlen, level_ref);
2483 
2484         /* Stack the residual bits... */
2485         uint32_t level_ref_stack = 0;
2486         for (idx = 0 ; idx < fixedlen ; idx += 1) {
2487             level_ref_stack <<= 1;
2488             level_ref_stack |= level_ref & 1;
2489             level_ref >>= 1;
2490         }
2491 
2492         assert(level_ref == 0);
2493 
2494         /* Emit the residual bits in MSB order. */
2495         for (idx = 0 ; idx < fixedlen ; idx += 1) {
2496             _jxr_wbitstream_uint1(str, level_ref_stack&1);
2497             level_ref_stack >>= 1;
2498         }
2499 
2500     } else {
2501         uint32_t level_ref = level - 2;
2502         assert(level_ref > 1);
2503         unsigned fixed = 0;
2504         while (level_ref > 1) {
2505             level_ref >>= 1;
2506             fixed += 1;
2507         }
2508 
2509         assert(level >= ((1U<<fixed) + 2));
2510         level_ref = level - (1<<fixed) - 2;
2511         DEBUG(" ABS_LEVEL = 0x%x (fixed = %d, level_ref = %u\n", level, fixed, level_ref);
2512 
2513         unsigned fixed_tmp = fixed - 4;
2514         if (fixed_tmp < 15) {
2515             _jxr_wbitstream_uint4(str, fixed_tmp);
2516 
2517         } else {
2518             _jxr_wbitstream_uint4(str, 0xf);
2519             fixed_tmp -= 15;
2520             if (fixed_tmp < 3) {
2521                 _jxr_wbitstream_uint2(str, fixed_tmp);
2522             } else {
2523                 _jxr_wbitstream_uint2(str, 0x3);
2524                 fixed_tmp -= 3;
2525                 assert(fixed_tmp < 8);
2526                 _jxr_wbitstream_uint3(str, fixed_tmp);
2527             }
2528         }
2529         _jxr_wbitstream_uintN(str, level_ref, fixed);
2530     }
2531 }
2532 
encode_abslevel_index(jxr_image_t image,struct wbitstream * str,int abslevel_index,int vlc_select)2533 static void encode_abslevel_index(jxr_image_t image, struct wbitstream*str,
2534                                   int abslevel_index, int vlc_select)
2535 {
2536     int table = image->vlc_table[vlc_select].table;
2537     assert(table==0 || table==1);
2538 
2539     if (table==0) {
2540         switch (abslevel_index) {
2541             case 0:
2542                 _jxr_wbitstream_uint2(str, 0x1);
2543                 break;
2544             case 1:
2545                 _jxr_wbitstream_uint2(str, 0x2);
2546                 break;
2547             case 2:
2548                 _jxr_wbitstream_uint2(str, 0x3);
2549                 break;
2550             case 3:
2551                 _jxr_wbitstream_uint2(str, 0x0);
2552                 _jxr_wbitstream_uint1(str, 0x1);
2553                 break;
2554             case 4:
2555                 _jxr_wbitstream_uint4(str, 0x1);
2556                 break;
2557             case 5:
2558                 _jxr_wbitstream_uint4(str, 0x0);
2559                 _jxr_wbitstream_uint1(str, 0x0);
2560                 break;
2561             case 6:
2562                 _jxr_wbitstream_uint4(str, 0x0);
2563                 _jxr_wbitstream_uint1(str, 0x1);
2564                 break;
2565         }
2566     }
2567     else {
2568         switch (abslevel_index) {
2569             case 0:
2570                 _jxr_wbitstream_uint1(str, 0x1);
2571                 break;
2572             case 1:
2573                 _jxr_wbitstream_uint2(str, 0x1);
2574                 break;
2575             case 2:
2576                 _jxr_wbitstream_uint2(str, 0x0);
2577                 _jxr_wbitstream_uint1(str, 0x1);
2578                 break;
2579             case 3:
2580                 _jxr_wbitstream_uint4(str, 0x1);
2581                 break;
2582             case 4:
2583                 _jxr_wbitstream_uint4(str, 0x0);
2584                 _jxr_wbitstream_uint1(str, 0x1);
2585                 break;
2586             case 5:
2587                 _jxr_wbitstream_uint4(str, 0x0);
2588                 _jxr_wbitstream_uint2(str, 0x0);
2589                 break;
2590             case 6:
2591                 _jxr_wbitstream_uint4(str, 0x0);
2592                 _jxr_wbitstream_uint2(str, 0x1);
2593                 break;
2594         }
2595     }
2596 }
2597 
w_DECODE_BLOCK(jxr_image_t image,struct wbitstream * str,int band,int chroma_flag,const int RLCoeffs[32],int num_non_zero)2598 static void w_DECODE_BLOCK(jxr_image_t image, struct wbitstream*str, int band, int chroma_flag,
2599                            const int RLCoeffs[32], int num_non_zero)
2600 {
2601     int location = 1;
2602     /* if CLR_FMT is YUV420 or YUV422 */
2603     if (image->use_clr_fmt==1/*YUV420*/ && chroma_flag && band==1)
2604         location = 10;
2605     if (image->use_clr_fmt==2/*YUV422*/ && chroma_flag && band==1)
2606         location = 2;
2607     int index_code = 0;
2608     int sign_flag = 0;
2609     int value = RLCoeffs[1];
2610 
2611     if (value < 0) {
2612         sign_flag = 1;
2613         value = -value;
2614     }
2615 
2616     /* FIRST_INDEX */
2617     if (RLCoeffs[0] == 0)
2618         index_code |= 1;
2619     if (value != 1)
2620         index_code |= 2;
2621 
2622     if (num_non_zero == 1)
2623         index_code |= 0<<2;
2624     else if (RLCoeffs[2] == 0)
2625         index_code |= 1<<2;
2626     else
2627         index_code |= 2<<2;
2628 
2629     DEBUG(" DECODE_BLOCK: chroma_flag=%d, band=%d value=%s%d num_non_zero=%d index_code=0x%x bitpos=%zu\n",
2630         chroma_flag, band, sign_flag?"-":"+", value, num_non_zero, index_code, _jxr_wbitstream_bitpos(str));
2631 
2632     DEBUG(" coeff[0] = %d (run)\n", RLCoeffs[0]);
2633     DEBUG(" coeff[1] = 0x%x\n", RLCoeffs[1]);
2634     DEBUG(" coeff[1*2+0] = %d (run)\n", RLCoeffs[2]);
2635 
2636     int context = (index_code&1) & (index_code>>2);
2637 
2638     /* Encode FIRST_INDEX */
2639     w_DECODE_FIRST_INDEX(image, str, chroma_flag, band, index_code);
2640     /* SIGN_FLAG */
2641     _jxr_wbitstream_uint1(str, sign_flag);
2642     if (index_code&2) {
2643         DEBUG(" DECODE_BLOCK: DECODE_ABS_LEVEL = %d (0x%08x)\n", value, value);
2644         w_DECODE_ABS_LEVEL(image, str, band, context, value);
2645     }
2646     if ((index_code&1) == 0) {
2647         w_DECODE_RUN(image, str, 15-location, RLCoeffs[0]);
2648     }
2649     location += 1 + RLCoeffs[0];
2650 
2651     int nz_index = 1;
2652     while (nz_index < num_non_zero) {
2653         /* If the previous index didn't already flag this as a
2654         zero run, then encode the run now. */
2655         if (RLCoeffs[2*nz_index+0] > 0)
2656             w_DECODE_RUN(image, str, 15-location, RLCoeffs[2*nz_index+0]);
2657 
2658         location += 1 + RLCoeffs[2*nz_index+0];
2659 
2660         value = RLCoeffs[2*nz_index+1];
2661         sign_flag = 0;
2662         if (value < 0) {
2663             sign_flag = 1;
2664             value = -value;
2665         }
2666 
2667         /* index_code */
2668         index_code = 0;
2669         if (value != 1)
2670             index_code |= 1;
2671         if (nz_index+1 == num_non_zero)
2672             index_code |= 0<<1;
2673         else if (RLCoeffs[2*nz_index+2] == 0)
2674             index_code |= 1<<1;
2675         else
2676             index_code |= 2<<1;
2677 
2678         DEBUG(" DECODE_BLOCK: nz_index=%u, index_code=%x\n", nz_index, index_code);
2679         w_DECODE_INDEX(image, str, location, chroma_flag, band, context, index_code);
2680         context &= index_code >> 1;
2681 
2682         _jxr_wbitstream_uint1(str, sign_flag);
2683         if (value != 1) {
2684             w_DECODE_ABS_LEVEL(image, str, band, context, value);
2685         }
2686 
2687         nz_index += 1;
2688     }
2689 }
2690 
w_DECODE_FIRST_INDEX(jxr_image_t image,struct wbitstream * str,int chroma_flag,int band,int first_index)2691 static void w_DECODE_FIRST_INDEX(jxr_image_t image, struct wbitstream*str,
2692                                  int chroma_flag, int band, int first_index)
2693 {
2694     struct encode_table_s{
2695         unsigned char bits;
2696         unsigned char len;
2697     };
2698 
2699     typedef struct encode_table_s first_index_table_t[12];
2700     static const first_index_table_t first_index_vlc[5] = {
2701         { /* VLC0 */
2702             { 0x08, 5 }, /* 0 == 0000 1... */
2703             { 0x04, 6 }, /* 1 == 0000 01.. */
2704             { 0x00, 7 }, /* 2 == 0000 000. */
2705             { 0x02, 7 }, /* 3 == 0000 001. */
2706             { 0x20, 5 }, /* 4 == 0010 0... */
2707             { 0x40, 3 }, /* 5 == 010. .... */
2708             { 0x28, 5 }, /* 6 == 0010 1... */
2709             { 0x80, 1 }, /* 7 == 1... .... */
2710             { 0x30, 5 }, /* 8 == 0011 0... */
2711             { 0x10, 4 }, /* 9 == 0001 .... */
2712             { 0x38, 5 }, /* 10 == 0011 1... */
2713             { 0x60, 3 } /* 11 == 011. .... */
2714         },
2715         { /* VLC1 */
2716             { 0x20, 4 }, /* 0 == 0010 .... */
2717             { 0x10, 5 }, /* 1 == 0001 0... */
2718             { 0x00, 6 }, /* 2 == 0000 00.. */
2719             { 0x04, 6 }, /* 3 == 0000 01.. */
2720             { 0x30, 4 }, /* 4 == 0011 .... */
2721             { 0x40, 3 }, /* 5 == 010. .... */
2722             { 0x18, 5 }, /* 6 == 0001 1... */
2723             { 0xc0, 2 }, /* 7 == 11.. .... */
2724             { 0x60, 3 }, /* 8 == 011. .... */
2725             { 0x80, 3 }, /* 9 == 100. .... */
2726             { 0x08, 5 }, /* 10 == 0000 1... */
2727             { 0xa0, 3 } /* 11 == 101. .... */
2728         },
2729         { /* VLC2 */
2730             { 0xc0, 2 }, /* 0 == 11.. .... */
2731             { 0x20, 3 }, /* 1 == 001. .... */
2732             { 0x00, 7 }, /* 2 == 0000 000. */
2733             { 0x02, 7 }, /* 3 == 0000 001. */
2734             { 0x08, 5 }, /* 4 == 0000 1... */
2735             { 0x40, 3 }, /* 5 == 010. .... */
2736             { 0x04, 7 }, /* 6 == 0000 010. */
2737             { 0x60, 3 }, /* 7 == 011. .... */
2738             { 0x80, 3 }, /* 8 == 100. .... */
2739             { 0xa0, 3 }, /* 9 == 101. .... */
2740             { 0x06, 7 }, /* 10 == 0000 011. */
2741             { 0x10, 4 }, /* 11 == 0001 .... */
2742         },
2743         { /* VLC3 */
2744             { 0x20, 3 }, /* 0 == 001. .... */
2745             { 0xc0, 2 }, /* 1 == 11.. .... */
2746             { 0x00, 7 }, /* 2 == 0000 000. */
2747             { 0x08, 5 }, /* 3 == 0000 1... */
2748             { 0x10, 5 }, /* 4 == 0001 0... */
2749             { 0x40, 3 }, /* 5 == 010. .... */
2750             { 0x02, 7 }, /* 6 == 0000 001. */
2751             { 0x60, 3 }, /* 7 == 011. .... */
2752             { 0x18, 5 }, /* 8 == 0001 1... */
2753             { 0x80, 3 }, /* 9 == 100. .... */
2754             { 0x04, 6 }, /* 10 == 0000 01.. */
2755             { 0xa0, 3 } /* 11 == 101. .... */
2756             },
2757             { /* VLC4 */
2758                 { 0x40, 3 }, /* 0 == 010. .... */
2759                 { 0x80, 1 }, /* 1 == 1... .... */
2760                 { 0x02, 7 }, /* 2 == 0000 001. */
2761                 { 0x10, 4 }, /* 3 == 0001 .... */
2762                 { 0x04, 7 }, /* 4 == 0000 010. */
2763                 { 0x60, 3 }, /* 5 == 011. .... */
2764                 { 0x00, 8 }, /* 6 == 0000 0000 */
2765                 { 0x20, 4 }, /* 7 == 0010 .... */
2766                 { 0x06, 7 }, /* 8 == 0000 011. */
2767                 { 0x30, 4 }, /* 9 == 0011 .... */
2768                 { 0x01, 8 }, /* 10 == 0000 0001 */
2769                 { 0x08, 5 } /* 11 == 0000 1... */
2770             }
2771     };
2772 
2773     abs_level_vlc_index_t vlc_select = (abs_level_vlc_index_t)0;
2774 
2775     switch (band) {
2776         case 1: /* LP */
2777             if (chroma_flag)
2778                 vlc_select = DecFirstIndLPChr;
2779             else
2780                 vlc_select = DecFirstIndLPLum;
2781             break;
2782         case 2: /* HP */
2783             if (chroma_flag)
2784                 vlc_select = DecFirstIndHPChr;
2785             else
2786                 vlc_select = DecFirstIndHPLum;
2787             break;
2788         default:
2789             assert(0);
2790             break;
2791     }
2792 
2793     int vlc_table = image->vlc_table[vlc_select].table;
2794     DEBUG(" DECODE_FIRST_INDEX: vlc_select = %s, vlc_table = %d, encode 0x%x\n",
2795         _jxr_vlc_index_name(vlc_select), vlc_table, first_index);
2796 
2797     unsigned char bits = first_index_vlc[vlc_table][first_index].bits;
2798     unsigned char len = first_index_vlc[vlc_table][first_index].len;
2799 
2800     DEBUG(" bits/len = 0x%02x/%u\n", bits, len);
2801 
2802     while (len > 0) {
2803         _jxr_wbitstream_uint1(str, (bits&0x80)? 1 : 0);
2804         bits <<= 1;
2805         len -= 1;
2806     }
2807 
2808     int delta_table = image->vlc_table[vlc_select].deltatable;
2809     int delta2table = image->vlc_table[vlc_select].delta2table;
2810 
2811     typedef int deltatable_t[12];
2812     const deltatable_t FirstIndexDelta[4] = {
2813         { 1, 1, 1, 1, 1, 0, 0,-1, 2, 1, 0, 0 },
2814         { 2, 2,-1,-1,-1, 0,-2,-1, 0, 0,-2,-1 },
2815         {-1, 1, 0, 2, 0, 0, 0, 0,-2, 0, 1, 1 },
2816         { 0, 1, 0, 1,-2, 0,-1,-1,-2,-1,-2,-2 }
2817     };
2818     assert(delta_table < 4);
2819     assert(delta2table < 4);
2820     assert(first_index < 12);
2821     image->vlc_table[vlc_select].discriminant += FirstIndexDelta[delta_table][first_index];
2822     image->vlc_table[vlc_select].discriminant2 += FirstIndexDelta[delta2table][first_index];
2823 }
2824 
w_DECODE_INDEX(jxr_image_t image,struct wbitstream * str,int location,int chroma_flag,int band,int context,int index_code)2825 static void w_DECODE_INDEX(jxr_image_t image, struct wbitstream*str,
2826                            int location, int chroma_flag, int band, int context,
2827                            int index_code)
2828 {
2829     int vlc_select = 0;
2830 
2831     switch (band) {
2832         case 1: /* LP */
2833             if (chroma_flag)
2834                 vlc_select = context? DecIndLPChr1 : DecIndLPChr0;
2835             else
2836                 vlc_select = context? DecIndLPLum1 : DecIndLPLum0;
2837             break;
2838         case 2: /* HP */
2839             if (chroma_flag)
2840                 vlc_select = context? DecIndHPChr1 : DecIndHPChr0;
2841             else
2842                 vlc_select = context? DecIndHPLum1 : DecIndHPLum0;
2843             break;
2844         default:
2845             assert(0);
2846             break;
2847     }
2848 
2849     /* If location>15, then there can't possibly be coefficients
2850     after the next, so only the low bit need be encoded. */
2851     if (location > 15) {
2852         DEBUG(" DECODE_INDEX: location=%d, index_code=%d\n", location, index_code);
2853         assert(index_code <= 1);
2854         _jxr_wbitstream_uint1(str, index_code);
2855         return;
2856     }
2857 
2858     if (location == 15) {
2859         DEBUG(" DECODE_INDEX: location=%d, index_code=%d\n", location, index_code);
2860         /* Table 61
2861         * INDEX2 table
2862         * 0 0
2863         * 2 10
2864         * 1 110
2865         * 3 111
2866         */
2867         switch (index_code) {
2868             case 0:
2869                 _jxr_wbitstream_uint1(str, 0);
2870                 break;
2871             case 2:
2872                 _jxr_wbitstream_uint2(str, 2);
2873                 break;
2874             case 1:
2875                 _jxr_wbitstream_uint1(str, 1);
2876                 _jxr_wbitstream_uint2(str, 2);
2877                 break;
2878             case 3:
2879                 _jxr_wbitstream_uint1(str, 1);
2880                 _jxr_wbitstream_uint2(str, 3);
2881                 break;
2882             default:
2883                 assert(0);
2884         }
2885         return;
2886     }
2887 
2888     int vlc_table = image->vlc_table[vlc_select].table;
2889     DEBUG(" DECODE_INDEX: vlc_select = %s, vlc_table = %d chroma_flag=%d, index_code=%d\n",
2890         _jxr_vlc_index_name(vlc_select), vlc_table, chroma_flag, index_code);
2891 
2892     struct encode_table_s{
2893         unsigned char bits;
2894         unsigned char len;
2895     };
2896 
2897     typedef struct encode_table_s index1_table_t[6];
2898     static const index1_table_t index1_vlc[5] = {
2899         { /* VLC0 */
2900             { 0x80, 1 }, /* 0 == 1... .... */
2901             { 0x00, 5 }, /* 1 == 0000 0... */
2902             { 0x20, 3 }, /* 2 == 001. .... */
2903             { 0x08, 5 }, /* 3 == 0000 1... */
2904             { 0x40, 2 }, /* 4 == 01.. .... */
2905             { 0x10, 4 } /* 5 == 0001 .... */
2906         },
2907         { /* VLC1 */
2908             { 0x40, 2 }, /* 0 == 01.. .... */
2909             { 0x00, 4 }, /* 1 == 0000 .... */
2910             { 0x80, 2 }, /* 2 == 10.. .... */
2911             { 0x10, 4 }, /* 3 == 0001 .... */
2912             { 0xc0, 2 }, /* 4 == 11.. .... */
2913             { 0x20, 3 } /* 5 == 001. .... */
2914         },
2915         { /* VLC2 */
2916             { 0x00, 4 }, /* 0 == 0000 .... */
2917             { 0x10, 4 }, /* 1 == 0001 .... */
2918             { 0x40, 2 }, /* 2 == 01.. .... */
2919             { 0x80, 2 }, /* 3 == 10.. .... */
2920             { 0xc0, 2 }, /* 4 == 11.. .... */
2921             { 0x20, 3 } /* 5 == 001. .... */
2922         },
2923         { /* VLC3 */
2924             { 0x00, 5 }, /* 0 == 0000 0... */
2925             { 0x08, 5 }, /* 1 == 0000 1... */
2926             { 0x40, 2 }, /* 2 == 01.. .... */
2927             { 0x80, 1 }, /* 3 == 1... .... */
2928             { 0x10, 4 }, /* 4 == 0001 .... */
2929             { 0x20, 3 } /* 5 == 001. .... */
2930         }
2931     };
2932 
2933     unsigned char bits = index1_vlc[vlc_table][index_code].bits;
2934     unsigned char len = index1_vlc[vlc_table][index_code].len;
2935 
2936     DEBUG(" bits/len = 0x%02x/%u\n", bits, len);
2937 
2938     while (len > 0) {
2939         _jxr_wbitstream_uint1(str, (bits&0x80)? 1 : 0);
2940         bits <<= 1;
2941         len -= 1;
2942     }
2943 
2944     int vlc_delta = image->vlc_table[vlc_select].deltatable;
2945     int vlc_delta2 = image->vlc_table[vlc_select].delta2table;
2946 
2947     typedef int deltatable_t[6];
2948     const deltatable_t Index1Delta[3] = {
2949         {-1, 1, 1, 1, 0, 1 },
2950         {-2, 0, 0, 2, 0, 0 },
2951         {-1,-1, 0, 1,-2, 0 }
2952     };
2953     image->vlc_table[vlc_select].discriminant += Index1Delta[vlc_delta][index_code];
2954     image->vlc_table[vlc_select].discriminant2+= Index1Delta[vlc_delta2][index_code];
2955 
2956     DEBUG(" DECODE_INDEX: deltatable/2 = %d/%d, discriminant/2 becomes %d/%d\n",
2957         vlc_delta, vlc_delta2,
2958         image->vlc_table[vlc_select].discriminant,
2959         image->vlc_table[vlc_select].discriminant2);
2960 
2961 }
2962 
w_DECODE_RUN(jxr_image_t image,struct wbitstream * str,int max_run,int run)2963 static void w_DECODE_RUN(jxr_image_t image, struct wbitstream*str, int max_run, int run)
2964 {
2965     assert(run > 0);
2966     if (max_run < 5) {
2967         DEBUG(" DECODE_RUN max_run=%d (<5) run=%d, bitpos=%zu\n",
2968             max_run, run, _jxr_wbitstream_bitpos(str));
2969 
2970         switch (max_run) {
2971             case 1:
2972                 assert(run == 1);
2973                 break;
2974             case 2:
2975                 assert(run <= 2);
2976                 if (run == 2)
2977                     _jxr_wbitstream_uint1(str, 0);
2978                 else
2979                     _jxr_wbitstream_uint1(str, 1);
2980                 break;
2981             case 3:
2982                 if (run == 1)
2983                     _jxr_wbitstream_uint1(str, 1);
2984                 else if (run == 2) {
2985                     _jxr_wbitstream_uint1(str, 0);
2986                     _jxr_wbitstream_uint1(str, 1);
2987                 } else {
2988                     assert(run == 3);
2989                     _jxr_wbitstream_uint1(str, 0);
2990                     _jxr_wbitstream_uint1(str, 0);
2991                 }
2992                 break;
2993             case 4:
2994                 if (run == 1) {
2995                     _jxr_wbitstream_uint1(str, 1);
2996                 } else if (run == 2) {
2997                     _jxr_wbitstream_uint1(str, 0);
2998                     _jxr_wbitstream_uint1(str, 1);
2999                 } else if (run == 3) {
3000                     _jxr_wbitstream_uint1(str, 0);
3001                     _jxr_wbitstream_uint1(str, 0);
3002                     _jxr_wbitstream_uint1(str, 1);
3003                 } else {
3004                     assert(run == 4);
3005                     _jxr_wbitstream_uint1(str, 0);
3006                     _jxr_wbitstream_uint1(str, 0);
3007                     _jxr_wbitstream_uint1(str, 0);
3008                 }
3009         }
3010         return;
3011     }
3012 
3013     static const int remap[15] = {1,2,3,5,7,1,2,3,5,7,1,2,3,4,5};
3014     static const int run_bin[15] = {-1,-1,-1,-1,2,2,2,1,1,1,1,0,0,0,0};
3015     static const int run_fixed_len[15] = {0,0,1,1,3,0,0,1,1,2,0,0,0,0,1};
3016 
3017     assert(max_run < 15);
3018     int index_run_bin = 5*run_bin[max_run];
3019 
3020     int run_index = 0;
3021     for (run_index = 0; run_index < 5; run_index += 1) {
3022         int index = run_index + index_run_bin;
3023         if (index < 0)
3024             continue;
3025 
3026         int use_run = remap[index];
3027         if (use_run > run)
3028             continue;
3029 
3030         int fixed = run_fixed_len[index];
3031         int range = 1 << fixed;
3032         if (run >= (use_run + range))
3033             continue;
3034 
3035         /* RUN_INDEX */
3036         switch(run_index) {
3037             case 0:
3038                 _jxr_wbitstream_uint1(str, 1);
3039                 break;
3040             case 1:
3041                 _jxr_wbitstream_uint2(str, 1);
3042                 break;
3043             case 2:
3044                 _jxr_wbitstream_uint1(str, 0);
3045                 _jxr_wbitstream_uint2(str, 1);
3046                 break;
3047             case 3:
3048                 _jxr_wbitstream_uint4(str, 0);
3049                 break;
3050             case 4:
3051                 _jxr_wbitstream_uint4(str, 1);
3052                 break;
3053         }
3054 
3055         int run_fixed = 0;
3056         if (fixed > 0) {
3057             run_fixed = run - use_run;
3058             assert(run_fixed >= 0 && run_fixed < (1<<fixed));
3059             _jxr_wbitstream_uintN(str, run_fixed, fixed);
3060         }
3061 
3062         DEBUG(" DECODE_RUN max_run=%d run=%d+%d = %d, bitpos=%zu\n",
3063             max_run, use_run, run_fixed, run, _jxr_wbitstream_bitpos(str));
3064 
3065         return;
3066     }
3067     assert(0);
3068 }
3069 
w_DECODE_BLOCK_ADAPTIVE(jxr_image_t image,struct wbitstream * str,unsigned tx,unsigned mx,int cbp_flag,int chroma_flag,int channel,int block,int mbhp_pred_mode,unsigned model_bits)3070 static int w_DECODE_BLOCK_ADAPTIVE(jxr_image_t image, struct wbitstream*str,
3071                                    unsigned tx, unsigned mx,
3072                                    int cbp_flag, int chroma_flag,
3073                                    int channel, int block, int mbhp_pred_mode,
3074                                    unsigned model_bits)
3075 {
3076     /* If the CBP flag is off, then there isn't really anything to
3077     encode here. */
3078     if (cbp_flag == 0)
3079         return 0;
3080 
3081     int values[16];
3082     values[0] = 0;
3083     int idx;
3084     DEBUG(" HP val[tx=%d, mx=%d, block=%d] ==", tx, mx, block);
3085     for (idx = 0 ; idx < 15 ; idx += 1) {
3086         values[1+idx] = MACROBLK_CUR_HP(image,channel,tx,mx,block,idx);
3087         /* Shift out the model bits. Be careful to note that the
3088         value is processed as sign/magnitude, so do the
3089         shifting to the abs of the value and invert it back
3090         if nesessary. */
3091         if (values[1+idx] >= 0)
3092             values[1+idx] >>= model_bits;
3093         else
3094             values[1+idx] = - ( (-values[1+idx]) >> model_bits );
3095         DEBUG(" 0x%x", values[1+idx]);
3096     }
3097     DEBUG("\n");
3098 
3099     int values2[16];
3100     values2[0] = 0;
3101     AdaptiveHPPermute(image, values2, values, mbhp_pred_mode);
3102 #if defined(DETAILED_DEBUG)
3103     {
3104         int k;
3105         DEBUG(" adapted hor scan order (MBHPMode=%d) ==", mbhp_pred_mode);
3106         for (k = 0 ; k<15; k+=1) {
3107             DEBUG(" %2d", image->hipass_hor_scanorder[k]);
3108         }
3109         DEBUG("\n");
3110         DEBUG(" adapted hor scan totals ==");
3111         for (k = 0 ; k<15; k+=1) {
3112             DEBUG(" %2d", image->hipass_hor_scantotals[k]);
3113         }
3114         DEBUG("\n");
3115         DEBUG(" adapted ver scan order (MBHPMode=%d) ==", mbhp_pred_mode);
3116         for (k = 0 ; k<15; k+=1) {
3117             DEBUG(" %2d", image->hipass_ver_scanorder[k]);
3118         }
3119         DEBUG("\n");
3120         DEBUG(" adapted ver scan totals ==");
3121         for (k = 0 ; k<15; k+=1) {
3122             DEBUG(" %2d", image->hipass_ver_scantotals[k]);
3123         }
3124         DEBUG("\n");
3125     }
3126 #endif
3127 
3128     int coeffs[32];
3129     int num_nonzero = RunLengthEncode(image, coeffs, values2);
3130 
3131     w_DECODE_BLOCK(image, str, 2, chroma_flag, coeffs, num_nonzero);
3132 
3133     return num_nonzero;;
3134 }
3135 
3136 /*
3137 * $Log: w_emit.c,v $
3138 * Revision 1.28 2009/09/16 12:00:00 microsoft
3139 * Reference Software v1.8 updates.
3140 *
3141 * Revision 1.27 2009/05/29 12:00:00 microsoft
3142 * Reference Software v1.6 updates.
3143 *
3144 * Revision 1.26 2009/04/13 12:00:00 microsoft
3145 * Reference Software v1.5 updates.
3146 *
3147 * Revision 1.25 2008/03/24 18:06:56 steve
3148 * Imrpove DEBUG messages around quantization.
3149 *
3150 * Revision 1.24 2008/03/21 18:05:53 steve
3151 * Proper CMYK formatting on input.
3152 *
3153 * Revision 1.23 2008/03/13 21:23:27 steve
3154 * Add pipeline step for YUV420.
3155 *
3156 * Revision 1.22 2008/03/13 00:30:56 steve
3157 * Force SCALING on if using a subsampled color format.
3158 *
3159 * Revision 1.21 2008/03/11 22:12:49 steve
3160 * Encode YUV422 through DC.
3161 *
3162 * Revision 1.20 2008/03/05 06:58:10 gus
3163 * *** empty log message ***
3164 *
3165 * Revision 1.19 2008/03/05 04:04:30 steve
3166 * Clarify constraints on USE_DC_QP in image plane header.
3167 *
3168 * Revision 1.18 2008/03/05 00:31:18 steve
3169 * Handle UNIFORM/IMAGEPLANE_UNIFORM compression.
3170 *
3171 * Revision 1.17 2008/03/02 19:56:27 steve
3172 * Infrastructure to read write BD16 files.
3173 *
3174 * Revision 1.16 2008/02/28 18:50:31 steve
3175 * Portability fixes.
3176 *
3177 * Revision 1.15 2008/02/26 23:52:44 steve
3178 * Remove ident for MS compilers.
3179 *
3180 * Revision 1.14 2008/02/22 23:01:33 steve
3181 * Compress macroblock HP CBP packets.
3182 *
3183 * Revision 1.13 2008/02/01 22:49:53 steve
3184 * Handle compress of YUV444 color DCONLY
3185 *
3186 * Revision 1.12 2008/01/08 01:06:20 steve
3187 * Add first pass overlap filtering.
3188 *
3189 * Revision 1.11 2008/01/07 16:19:10 steve
3190 * Properly configure TRIM_FLEXBITS_FLAG bit.
3191 *
3192 * Revision 1.10 2008/01/04 17:07:36 steve
3193 * API interface for setting QP values.
3194 *
3195 * Revision 1.9 2007/12/13 18:01:09 steve
3196 * Stubs for HP encoding.
3197 *
3198 * Revision 1.8 2007/12/07 01:20:34 steve
3199 * Fix adapt not adapting on line ends.
3200 *
3201 * Revision 1.7 2007/12/05 18:14:19 steve
3202 * hard-code LOG_WORD_FLAG true.
3203 *
3204 * Revision 1.6 2007/12/04 22:06:10 steve
3205 * Infrastructure for encoding LP.
3206 *
3207 * Revision 1.5 2007/11/30 01:50:58 steve
3208 * Compression of DCONLY GRAY.
3209 *
3210 * Revision 1.4 2007/11/26 01:47:16 steve
3211 * Add copyright notices per MS request.
3212 *
3213 * Revision 1.3 2007/11/08 19:38:38 steve
3214 * Get stub DCONLY compression to work.
3215 *
3216 * Revision 1.2 2007/11/08 02:52:33 steve
3217 * Some progress in some encoding infrastructure.
3218 *
3219 * Revision 1.1 2007/06/06 17:19:13 steve
3220 * Introduce to CVS.
3221 *
3222 */
3223 
3224