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