Coverage Report

Created: 2026-09-14 07:34

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