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