1 #ifndef __jxr_priv_H
2 #define __jxr_priv_H
3 /*************************************************************************
4 *
5 * This software module was originally contributed by Microsoft
6 * Corporation in the course of development of the
7 * ITU-T T.832 | ISO/IEC 29199-2 ("JPEG XR") format standard for
8 * reference purposes and its performance may not have been optimized.
9 *
10 * This software module is an implementation of one or more
11 * tools as specified by the JPEG XR standard.
12 *
13 * ITU/ISO/IEC give You a royalty-free, worldwide, non-exclusive
14 * copyright license to copy, distribute, and make derivative works
15 * of this software module or modifications thereof for use in
16 * products claiming conformance to the JPEG XR standard as
17 * specified by ITU-T T.832 | ISO/IEC 29199-2.
18 *
19 * ITU/ISO/IEC give users the same free license to this software
20 * module or modifications thereof for research purposes and further
21 * ITU/ISO/IEC standardization.
22 *
23 * Those intending to use this software module in products are advised
24 * that its use may infringe existing patents. ITU/ISO/IEC have no
25 * liability for use of this software module or modifications thereof.
26 *
27 * Copyright is not released for products that do not conform to
28 * to the JPEG XR standard as specified by ITU-T T.832 |
29 * ISO/IEC 29199-2.
30 *
31 * Microsoft Corporation retains full right to modify and use the code
32 * for its own purpose, to assign or donate the code to a third party,
33 * and to inhibit third parties from using the code for products that
34 * do not conform to the JPEG XR standard as specified by ITU-T T.832 |
35 * ISO/IEC 29199-2.
36 *
37 * This copyright notice must be included in all copies or derivative
38 * works.
39 *
40 * Copyright (c) ITU-T/ISO/IEC 2008, 2009.
41 ***********************************************************************/
42 
43 #ifdef _MSC_VER
44 #pragma comment (user,"$Id: jxr_priv.h,v 1.3 2008-05-13 13:47:11 thor Exp $")
45 #else
46 #ident "$Id: jxr_priv.h,v 1.3 2008-05-13 13:47:11 thor Exp $"
47 #endif
48 
49 # include "jpegxr.h"
50 
51 #ifndef _MSC_VER
52 # include <stdint.h>
53 #else
54 /* MSVC (as of 2008) does not support C99 or the stdint.h header
55 file. So include a private little header file here that does the
56 minimal typedefs that we need. */
57 # include "stdint_minimal.h"
58 #endif
59 
60 /* define this to check if range of values exceeds signed 16-bit */
61 #define VERIFY_16BIT
62 
63 #ifdef VERIFY_16BIT
64 #define CHECK1(flag, a) if(((a) < -0x8000) || ((a) >= 0x8000)) flag = 1
65 #else
66 #define CHECK1(flag, a) do { } while(0)
67 #endif
68 
69 #define CHECK2(flag, a, b) CHECK1(flag, a); CHECK1(flag, b)
70 #define CHECK3(flag, a, b, c) CHECK2(flag, a, b); CHECK1(flag, c)
71 #define CHECK4(flag, a, b, c, d) CHECK3(flag, a, b, c); CHECK1(flag, d)
72 #define CHECK5(flag, a, b, c, d, e) CHECK4(flag, a, b, c, d); CHECK1(flag, e)
73 #define CHECK6(flag, a, b, c, d, e, f) CHECK5(flag, a, b, c, d, e); CHECK1(flag, f)
74 
75 struct macroblock_s{
76     int*data;
77     /* This is used to temporarily hold Predicted values. */
78     int*pred_dclp;
79     /* */
80     unsigned lp_quant : 8;
81     unsigned hp_quant : 8;
82     int mbhp_pred_mode : 3;
83     int have_qnt : 1;/* THOR: If set, the quant values are valid */
84     /* Stash HP CBP values for the macroblock. */
85     int hp_cbp, hp_diff_cbp;
86     /* model_bits for current HP. HP uses this to pass model_bits
87     to FLEXBITS parsing. */
88     unsigned hp_model_bits[2];
89 };
90 
91 struct adaptive_vlc_s{
92     int discriminant;
93     int discriminant2;
94     int table;
95     int deltatable;
96     int delta2table;
97 };
98 
99 struct cbp_model_s{
100     int state[2];
101     int count0[2];
102     int count1[2];
103 };
104 
105 typedef enum abs_level_vlc_index_e{
106     AbsLevelIndDCLum,
107     AbsLevelIndDCChr,
108     DecFirstIndLPLum,
109     AbsLevelIndLP0,
110     AbsLevelIndLP1,
111     AbsLevelIndHP0,
112     AbsLevelIndHP1,
113     DecIndLPLum0,
114     DecIndLPLum1,
115     DecFirstIndLPChr,
116     DecIndLPChr0,
117     DecIndLPChr1,
118     DecNumCBP,
119     DecNumBlkCBP,
120     DecIndHPLum0,
121     DecIndHPLum1,
122     DecFirstIndHPLum,
123     DecFirstIndHPChr,
124     DecIndHPChr0,
125     DecIndHPChr1,
126     AbsLevelInd_COUNT
127 }abs_level_vlc_index_t;
128 
129 struct model_s{
130     int bits[2];
131     int state[2];
132 };
133 
134 struct jxr_image{
135     int user_flags;
136 
137     /* These store are the width/height minus 1 (so that 4Gwidth
138     is OK but 0 width is not.) This, by the way, is how the
139     width/height is stored in the HPPhoto stream too. */
140     uint32_t width1;
141     uint32_t height1;
142     uint32_t extended_width;
143     uint32_t extended_height;
144 
145     uint8_t header_flags1;
146     uint8_t header_flags2;
147     /* Color format of the source image data */
148     uint8_t header_flags_fmt;
149     jxr_output_clr_fmt_t output_clr_fmt;
150     /* Color format to use internally. */
151     /* 0=YONLY, 1=YUV420, 2=YUV422, 3=YUV444, 4=CMYK, 5=CMYKDIRECT, 6=NCOMPONENT */
152     uint8_t use_clr_fmt;
153 
154     /* pixel format */
155     jxrc_t_pixelFormat ePixelFormat;
156 
157     /* If TRIM_FLEXBITS_FLAG, then this is the bits to trim. */
158     unsigned trim_flexbits : 4;
159 
160     /* 0==ALL, 1==NOFLEXBITS, 2==NOHIGHPASS, 3==DCONLY, 4==ISOLATED */
161     uint8_t bands_present;
162     uint8_t bands_present_of_primary; /* stores value of primary image plane to apply restriction on value of alpha image plane */
163 
164     uint8_t chroma_centering_x;
165     uint8_t chroma_centering_y;
166 
167     uint8_t num_channels;
168 
169     /* Disable overlap flag, true for hard tiles, false for soft tiles*/
170     unsigned disableTileOverlapFlag;
171 
172     unsigned tile_rows;
173     unsigned tile_columns;
174     unsigned*tile_row_height;
175     unsigned*tile_column_width;
176     /* This is the position in image macroblocks. */
177     unsigned*tile_column_position;
178     unsigned*tile_row_position;
179     /* The numbers collected by INDEX_TABLE */
180     int64_t*tile_index_table;
181     int64_t tile_index_table_length;
182 
183     uint16_t window_extra_top;
184     uint16_t window_extra_left;
185     uint16_t window_extra_bottom;
186     uint16_t window_extra_right;
187 
188     /* Information from the plane header. */
189 
190     unsigned scaled_flag : 1;
191     unsigned dc_frame_uniform : 1;
192     unsigned lp_use_dc_qp : 1;
193     unsigned lp_frame_uniform : 1;
194     unsigned hp_use_lp_qp : 1;
195     unsigned hp_frame_uniform : 1;
196 
197     unsigned char shift_bits;
198     unsigned char len_mantissa;
199     char exp_bias;
200 
201     /* Per-tile information (changes as tiles are processed) */
202     unsigned num_lp_qps;
203     unsigned num_hp_qps;
204 
205     /* State variables used by encoder/decoder. */
206     int cur_my; /* Address of strip_cur */
207     struct{
208         struct macroblock_s*up4;
209         struct macroblock_s*up3;
210         struct macroblock_s*up2;
211         struct macroblock_s*up1;
212         struct macroblock_s*cur;
213         int *upsample_memory_y; /* Number of elements is dependent on number of MBs in each MB row */
214         int *upsample_memory_x; /* Always contains 16 elements */
215     }strip[MAX_CHANNELS];
216 
217     /* SPATIAL: Hold previous strips of current tile */
218     /* FREQUENCY: mbs for the entire image. */
219     struct macroblock_s*mb_row_buffer[MAX_CHANNELS];
220     /* Hold final 4 strips of previous tile */
221     struct macroblock_s*mb_row_context[MAX_CHANNELS];
222 
223     struct adaptive_vlc_s vlc_table[AbsLevelInd_COUNT];
224     int count_max_CBPLP;
225     int count_zero_CBPLP;
226 
227     struct cbp_model_s hp_cbp_model;
228 
229     unsigned lopass_scanorder[15];
230     unsigned lopass_scantotals[15];
231 
232     unsigned hipass_hor_scanorder[15];
233     unsigned hipass_hor_scantotals[15];
234     unsigned hipass_ver_scanorder[15];
235     unsigned hipass_ver_scantotals[15];
236 
237     jxr_component_mode_t dc_component_mode, lp_component_mode, hp_component_mode;
238 
239     /* Quantization parameters for up to 16 channels. */
240     unsigned char dc_quant_ch[MAX_CHANNELS];
241     unsigned char lp_quant_ch[MAX_CHANNELS][MAX_LP_QPS];
242     unsigned char hp_quant_ch[MAX_CHANNELS][MAX_HP_QPS];
243 # define HP_QUANT_Y hp_quant_ch[0]
244 
245     /* Per-tile quantization data (used during encode) */
246     struct jxr_tile_qp*tile_quant;
247 # define GET_TILE_QUANT(image,tx,ty) ((image)->tile_quant + (ty)*((image)->tile_rows+1) + (tx))
248 
249     struct model_s model_dc, model_lp, model_hp;
250 
251     struct model_s*model_hp_buffer;
252     struct cbp_model_s*hp_cbp_model_buffer;
253 
254     block_fun_t out_fun;
255     block_fun_t inp_fun;
256     void*user_data;
257 
258     struct jxr_image * alpha;  /* interleaved alpha image plane */
259     int primary;               /* primary channel or alpha channel */
260 
261     uint8_t profile_idc;
262     uint8_t level_idc;
263 
264     uint8_t lwf_test; /* flag to track whether long_word_flag needs to be TRUE */
265 
266     /* store container values */
267     uint32_t container_width;
268     uint32_t container_height;
269     uint8_t container_nc;
270     uint8_t container_alpha;
271     uint8_t container_separate_alpha;
272     jxr_bitdepth_t container_bpc;
273     jxr_output_clr_fmt_t container_color;
274     uint8_t container_image_band_presence;
275     uint8_t container_alpha_band_presence;
276     uint8_t container_current_separate_alpha;
277 };
278 
279 extern unsigned char _jxr_select_lp_index(jxr_image_t image, unsigned tx, unsigned ty,
280                                           unsigned mx, unsigned my);
281 extern unsigned char _jxr_select_hp_index(jxr_image_t image, unsigned tx, unsigned ty,
282                                           unsigned mx, unsigned my);
283 
284 /* User flags for controlling encode/decode */
285 # define SKIP_HP_DATA(image) ((image)->user_flags & 0x0001)
286 # define SKIP_FLEX_DATA(image) ((image)->user_flags & 0x0002)
287 
288 /* Get the width of the image in macroblocks. Round up. */
289 # define WIDTH_BLOCKS(image) (((image)->width1 + 16) >> 4)
290 # define HEIGHT_BLOCKS(image) (((image)->height1 + 16) >> 4)
291 
292 # define EXTENDED_WIDTH_BLOCKS(image) (((image)->extended_width) >> 4)
293 # define EXTENDED_HEIGHT_BLOCKS(image) (((image)->extended_height) >> 4)
294 
295 /* TRUE if tiling is supported in this image. */
296 # define TILING_FLAG(image) ((image)->header_flags1 & 0x80)
297 /* TRUE if the bitstream format is frequency mode (FREQUENCY_MODE_CODESTREAM_FLAG is true), FALSE for spatial mode */
298 # define FREQUENCY_MODE_CODESTREAM_FLAG(image) ((image)->header_flags1 & 0x40)
299 /* TRUE if the INDEXTABLE_PRESENT_FLAG is set for the image. */
300 # define INDEXTABLE_PRESENT_FLAG(image) ((image)->header_flags1 & 0x04)
301 /* OVERLAP value: 0, 1, 2 or 3 */
302 # define OVERLAP_INFO(image) ((image)->header_flags1 & 0x03)
303 /* LONG_WORD_FLAG */
304 # define LONG_WORD_FLAG(image) ((image)->header_flags2 & 0x40)
305 
306 /* TRUE if the SHORT_HEADER flag is set. */
307 # define SHORT_HEADER_FLAG(image) ((image)->header_flags2 & 0x80)
308 /* TRUE if windowing is allowed, 0 otherwise. */
309 # define WINDOWING_FLAG(image) ((image)->header_flags2 & 0x20)
310 # define TRIM_FLEXBITS_FLAG(image) ((image)->header_flags2 & 0x10)
311 # define ALPHACHANNEL_FLAG(image) ((image)->header_flags2 & 0x01)
312 
313 # define SOURCE_CLR_FMT(image) (((image)->header_flags_fmt >> 4) & 0x0f)
314 
315 /* SOURCE_BITDEPTH (aka OUTPUT_BITDEPTH) can be:
316 * 0 BD1WHITE1
317 * 1 BD8
318 * 2 BD16
319 * 3 BD16S
320 * 4 BD16F
321 * 5 RESERVED
322 * 6 BD32S
323 * 7 BD32F
324 * 8 BD5
325 * 9 BD10
326 * 10 BD565
327 * 11 RESERVED
328 * 12 RESERVED
329 * 13 RESERVED
330 * 14 RESERVED
331 * 15 BD1BLACK1
332 */
333 # define SOURCE_BITDEPTH(image) (((image)->header_flags_fmt >> 0) & 0x0f)
334 
335 /*
336 * Given the tile index and macroblock index within the tile, return
337 * the l-value macroblock structure.
338 */
339 # define MACROBLK_CUR(image,c,tx,mx) ((image)->strip[c].cur[(image)->tile_column_position[tx]+mx])
340 # define MACROBLK_UP1(image,c,tx,mx) ((image)->strip[c].up1[(image)->tile_column_position[tx]+mx])
341 # define MACROBLK_UP2(image,c,tx,mx) ((image)->strip[c].up2[(image)->tile_column_position[tx]+mx])
342 # define MACROBLK_UP3(image,c,tx,mx) ((image)->strip[c].up3[(image)->tile_column_position[tx]+mx])
343 # define MACROBLK_UP4(image,c,tx,mx) ((image)->strip[c].up4[(image)->tile_column_position[tx]+mx])
344 
345 /* Return the l-value of the DC coefficient in the macroblock. */
346 # define MACROBLK_UP2_DC(image, c, tx, mx) (MACROBLK_UP2(image,c,tx,mx).data[0])
347 # define MACROBLK_UP_DC(image, c, tx, mx) (MACROBLK_UP1(image,c,tx,mx).data[0])
348 # define MACROBLK_CUR_DC(image, c, tx, mx) (MACROBLK_CUR(image,c,tx,mx).data[0])
349 /* Return the l-value of the LP coefficient in the macroblock.
350 The k value can range from 0-14. (There are 15 LP coefficients.) */
351 # define MACROBLK_CUR_LP(image,c,tx,mx,k) (MACROBLK_CUR(image,c,tx,mx).data[1+k])
352 # define MACROBLK_UP_LP(image,c,tx,mx,k) (MACROBLK_UP1(image,c,tx,mx).data[1+k])
353 # define MACROBLK_UP2_LP(image,c,tx,mx,k) (MACROBLK_UP2(image,c,tx,mx).data[1+k])
354 
355 # define MACROBLK_CUR_LP_QUANT(image,c,tx,mx) (MACROBLK_CUR(image,c,tx,mx).lp_quant)
356 # define MACROBLK_UP1_LP_QUANT(image,c,tx,mx) (MACROBLK_UP1(image,c,tx,mx).lp_quant)
357 # define MACROBLK_UP2_LP_QUANT(image,c,tx,mx) (MACROBLK_UP2(image,c,tx,mx).lp_quant)
358 
359 /* Return the l-value of the HP coefficient in the macroblk.
360 The blk value is 0-15 and identifies the block within the macroblk.
361 The k value is 0-14 and addresses the coefficient within the blk. */
362 # define MACROBLK_CUR_HP(image,c,tx,mx,blk,k) (MACROBLK_CUR(image,c,tx,mx).data[16+15*(blk)+(k)])
363 # define MACROBLK_UP1_HP(image,c,tx,mx,blk,k) (MACROBLK_UP1(image,c,tx,mx).data[16+15*(blk)+(k)])
364 # define MACROBLK_UP2_HP(image,c,tx,mx,blk,k) (MACROBLK_UP2(image,c,tx,mx).data[16+15*(blk)+(k)])
365 
366 # define MACROBLK_CUR_HP_QUANT(image,c,tx,mx) (MACROBLK_CUR(image,c,tx,mx).hp_quant)
367 # define MACROBLK_UP1_HP_QUANT(image,c,tx,mx) (MACROBLK_UP1(image,c,tx,mx).hp_quant)
368 # define MACROBLK_UP2_HP_QUANT(image,c,tx,mx) (MACROBLK_UP2(image,c,tx,mx).hp_quant)
369 
370 /* Return the l-value of the HP CBP value. */
371 # define MACROBLK_CUR_HPCBP(image,c,tx,mx) (MACROBLK_CUR(image,c,tx,mx).hp_cbp)
372 # define MACROBLK_UP1_HPCBP(image,c,tx,mx) (MACROBLK_UP1(image,c,tx,mx).hp_cbp)
373 
374 extern void _jxr_make_mbstore(jxr_image_t image, int include_up4);
375 extern void _jxr_fill_strip(jxr_image_t image);
376 extern void _jxr_rflush_mb_strip(jxr_image_t image, int tx, int ty, int my);
377 extern void _jxr_wflush_mb_strip(jxr_image_t image, int tx, int ty, int my, int read_new);
378 
379 /* Common strip handling functions. */
380 extern void _jxr_clear_strip_cur(jxr_image_t image);
381 extern void _jxr_r_rotate_mb_strip(jxr_image_t image);
382 
383 /* Wrap up an image collected in frequency mode. */
384 extern void _jxr_frequency_mode_render(jxr_image_t image);
385 
386 /* Application interface functions */
387 extern void _jxr_send_mb_to_output(jxr_image_t image, int mx, int my, int*data);
388 
389 /* I/O functions. */
390 
391 struct rbitstream{
392     unsigned char byte;
393     int bits_avail;
394     FILE*fd;
395     size_t read_count;
396 
397     long mark_stream_position;
398 };
399 
400 /* Get the current *bit* position, for diagnostic use. */
401 extern void _jxr_rbitstream_initialize(struct rbitstream*str, FILE*fd);
402 extern size_t _jxr_rbitstream_bitpos(struct rbitstream*str);
403 
404 extern void _jxr_rbitstream_syncbyte(struct rbitstream*str);
405 extern int _jxr_rbitstream_uint1(struct rbitstream*str);
406 extern uint8_t _jxr_rbitstream_uint2(struct rbitstream*str);
407 extern uint8_t _jxr_rbitstream_uint3(struct rbitstream*str);
408 extern uint8_t _jxr_rbitstream_uint4(struct rbitstream*str);
409 extern uint8_t _jxr_rbitstream_uint6(struct rbitstream*str);
410 extern uint8_t _jxr_rbitstream_uint8(struct rbitstream*str);
411 extern uint16_t _jxr_rbitstream_uint12(struct rbitstream*str);
412 extern uint16_t _jxr_rbitstream_uint15(struct rbitstream*str);
413 extern uint16_t _jxr_rbitstream_uint16(struct rbitstream*str);
414 extern uint32_t _jxr_rbitstream_uint32(struct rbitstream*str);
415 extern uint32_t _jxr_rbitstream_uintN(struct rbitstream*str, int N);
416 /* Return <0 if there is an escape code. */
417 extern int64_t _jxr_rbitstream_intVLW(struct rbitstream*str);
418 
419 extern int _jxr_rbitstream_intE(struct rbitstream*str, int code_size,
420                                 const unsigned char*codeb,
421                                 const signed char*codev);
422 extern const char* _jxr_vlc_index_name(int vlc);
423 
424 /*
425 * These functions provided limited random access into the file
426 * stream. The mark() function causes a given point to be marked as
427 * "zero", then the seek() function can go to a *byte* position
428 * relative the zeo mark. These functions can only be called when the
429 * stream is on a byte boundary.
430 */
431 extern void _jxr_rbitstream_mark(struct rbitstream*str);
432 extern void _jxr_rbitstream_seek(struct rbitstream*str, uint64_t off);
433 
434 struct wbitstream{
435     unsigned char byte;
436     int bits_ready;
437     FILE*fd;
438     size_t write_count;
439 };
440 
441 extern void _jxr_wbitstream_initialize(struct wbitstream*str, FILE*fd);
442 extern size_t _jxr_wbitstream_bitpos(struct wbitstream*str);
443 extern void _jxr_wbitstream_syncbyte(struct wbitstream*str);
444 extern void _jxr_wbitstream_uint1(struct wbitstream*str, int val);
445 extern void _jxr_wbitstream_uint2(struct wbitstream*str, uint8_t val);
446 extern void _jxr_wbitstream_uint3(struct wbitstream*str, uint8_t val);
447 extern void _jxr_wbitstream_uint4(struct wbitstream*str, uint8_t val);
448 extern void _jxr_wbitstream_uint6(struct wbitstream*str, uint8_t val);
449 extern void _jxr_wbitstream_uint8(struct wbitstream*str, uint8_t val);
450 extern void _jxr_wbitstream_uint12(struct wbitstream*str, uint16_t val);
451 extern void _jxr_wbitstream_uint15(struct wbitstream*str, uint16_t val);
452 extern void _jxr_wbitstream_uint16(struct wbitstream*str, uint16_t val);
453 extern void _jxr_wbitstream_uint32(struct wbitstream*str, uint32_t val);
454 extern void _jxr_wbitstream_uintN(struct wbitstream*str, uint32_t val, int N);
455 extern void _jxr_wbitstream_intVLW(struct wbitstream*str, uint64_t val);
456 extern void _jxr_wbitstream_flush(struct wbitstream*str);
457 
458 extern void _jxr_wbitstream_mark(struct wbitstream*str);
459 extern void _jxr_wbitstream_seek(struct wbitstream*str, uint64_t off);
460 
461 extern void _jxr_w_TILE_SPATIAL(jxr_image_t image, struct wbitstream*str,
462                                 unsigned tx, unsigned ty);
463 extern void _jxr_w_TILE_DC(jxr_image_t image, struct wbitstream*str,
464                           unsigned tx, unsigned ty);
465 extern void _jxr_w_TILE_LP(jxr_image_t image, struct wbitstream*str,
466                          unsigned tx, unsigned ty);
467 extern void _jxr_w_TILE_HP_FLEX(jxr_image_t image, struct wbitstream*str,
468                          unsigned tx, unsigned ty);
469 extern void _jxr_w_TILE_HEADER_DC(jxr_image_t image, struct wbitstream*str,
470                              int alpha_flag, unsigned tx, unsigned ty);
471 extern void _jxr_w_TILE_HEADER_LOWPASS(jxr_image_t image, struct wbitstream*str,
472                                   int alpha_flag, unsigned tx, unsigned ty);
473 extern void _jxr_w_TILE_HEADER_HIGHPASS(jxr_image_t image, struct wbitstream*str,
474                                    int alpha_flag, unsigned tx, unsigned ty);
475 extern void _jxr_w_MB_DC(jxr_image_t image, struct wbitstream*str,
476                     int alpha_flag,
477                     unsigned tx, unsigned ty,
478                     unsigned mx, unsigned my);
479 extern void _jxr_w_MB_LP(jxr_image_t image, struct wbitstream*str,
480                     int alpha_flag,
481                     unsigned tx, unsigned ty,
482                     unsigned mx, unsigned my);
483 extern int _jxr_w_MB_HP(jxr_image_t image, struct wbitstream*str,
484                    int alpha_flag,
485                    unsigned tx, unsigned ty,
486                    unsigned mx, unsigned my, struct wbitstream*strFB);
487 extern void _jxr_w_MB_CBP(jxr_image_t image, struct wbitstream*str,
488                      int alpha_flag,
489                      unsigned tx, unsigned ty,
490                      unsigned mx, unsigned my);
491 extern void _jxr_w_DC_QP(jxr_image_t image, struct wbitstream*str);
492 extern void _jxr_w_LP_QP(jxr_image_t image, struct wbitstream*str);
493 extern void _jxr_w_HP_QP(jxr_image_t image, struct wbitstream*str);
494 extern void _jxr_w_ENCODE_QP_INDEX(jxr_image_t image, struct wbitstream*str,
495                               unsigned tx, unsigned ty, unsigned mx, unsigned my,
496                               unsigned num_qps, unsigned qp_index);
497 
498 
499 extern int _jxr_r_TILE_SPATIAL(jxr_image_t image, struct rbitstream*str,
500                                unsigned tx, unsigned ty);
501 extern int _jxr_r_TILE_DC(jxr_image_t image, struct rbitstream*str,
502                           unsigned tx, unsigned ty);
503 extern int _jxr_r_TILE_LP(jxr_image_t image, struct rbitstream*str,
504                           unsigned tx, unsigned ty);
505 extern int _jxr_r_TILE_HP(jxr_image_t image, struct rbitstream*str,
506                           unsigned tx, unsigned ty);
507 extern int _jxr_r_TILE_FLEXBITS(jxr_image_t image, struct rbitstream*str,
508                                 unsigned tx, unsigned ty);
509 extern int _jxr_r_TILE_FLEXBITS_ESCAPE(jxr_image_t image,
510                                        unsigned tx, unsigned ty);
511 extern void _jxr_r_TILE_HEADER_DC(jxr_image_t image, struct rbitstream*str,
512                                   int alpha_flag, unsigned tx, unsigned ty);
513 extern void _jxr_r_TILE_HEADER_LOWPASS(jxr_image_t image, struct rbitstream*str,
514                                        int alpha_flag, unsigned tx, unsigned ty);
515 extern void _jxr_r_TILE_HEADER_HIGHPASS(jxr_image_t image, struct rbitstream*str,
516                                         int alpha_flag, unsigned tx, unsigned ty);
517 
518 extern void _jxr_r_MB_DC(jxr_image_t image, struct rbitstream*str,
519                          int alpha_flag,
520                          unsigned tx, unsigned ty,
521                          unsigned mx, unsigned my);
522 extern void _jxr_r_MB_LP(jxr_image_t image, struct rbitstream*str,
523                          int alpha_flag,
524                          unsigned tx, unsigned ty,
525                          unsigned mx, unsigned my);
526 extern int _jxr_r_MB_HP(jxr_image_t image, struct rbitstream*str,
527                         int alpha_flag,
528                         unsigned tx, unsigned ty,
529                         unsigned mx, unsigned my);
530 extern int _jxr_r_MB_CBP(jxr_image_t image, struct rbitstream*str,
531                          int alpha_flag,
532                          unsigned tx, unsigned ty,
533                          unsigned mx, unsigned my);
534 int _jxr_r_MB_FLEXBITS(jxr_image_t image, struct rbitstream*str,
535                        int alpha_flag,
536                        unsigned tx, unsigned ty,
537                        unsigned mx, unsigned my);
538 
539 extern int _jxr_r_DC_QP(jxr_image_t image, struct rbitstream*str);
540 extern int _jxr_r_LP_QP(jxr_image_t image, struct rbitstream*str);
541 
542 extern unsigned _jxr_DECODE_QP_INDEX(struct rbitstream*str,
543                                      unsigned index_count);
544 extern int r_DECODE_BLOCK(jxr_image_t image, struct rbitstream*str,
545                           int chroma_flag, int coeff[16], int band, int location);
546 
547 /* algorithm functions */
548 /*
549 * In these functions, the mx and my are the position of the MB
550 * within a tile (i.e. mx=my=0 is the top left MB in the TILE not
551 * necessarily the image). The mx and my are given it units of MB. The
552 * pixel position is mx*16, my*16.
553 */
554 
555 extern int _jxr_quant_map(jxr_image_t image, int x, int shift);
556 extern int _jxr_InitContext(jxr_image_t image, unsigned tx, unsigned ty,
557                             unsigned mx, unsigned my);
558 extern int _jxr_ResetContext(jxr_image_t image, unsigned tx, unsigned mx);
559 extern int _jxr_ResetTotals(jxr_image_t image, unsigned mx);
560 extern int _jxr_vlc_select(int band, int chroma_flag);
561 extern void _jxr_InitVLCTable(jxr_image_t image, int vlc_select);
562 extern void _jxr_AdaptVLCTable(jxr_image_t image, int vlc_select);
563 extern void _jxr_AdaptLP(jxr_image_t image);
564 extern void _jxr_AdaptHP(jxr_image_t image);
565 extern void _jxr_InitializeModelMB(struct model_s*model, int band);
566 extern void _jxr_UpdateModelMB(jxr_image_t image, int lap_mean[2],
567 struct model_s*model, int band);
568 
569 extern void _jxr_InitializeCountCBPLP(jxr_image_t image);
570 extern void _jxr_UpdateCountCBPLP(jxr_image_t image, int cbplp, int max);
571 
572 extern void _jxr_InitLPVLC(jxr_image_t image);
573 extern void _jxr_InitCBPVLC(jxr_image_t image);
574 extern void _jxr_InitHPVLC(jxr_image_t image);
575 
576 extern void _jxr_InitializeCBPModel(jxr_image_t image);
577 extern void _jxr_w_PredCBP444(jxr_image_t image,
578                               int ch, unsigned tx,
579                               unsigned mx, int my);
580 extern void _jxr_w_PredCBP422(jxr_image_t image,
581                               int ch, unsigned tx,
582                               unsigned mx, int my);
583 extern void _jxr_w_PredCBP420(jxr_image_t image,
584                               int ch, unsigned tx,
585                               unsigned mx, int my);
586 extern int _jxr_PredCBP444(jxr_image_t image, int*diff_cbp,
587                            int channel, unsigned tx, unsigned mx, unsigned my);
588 extern int _jxr_PredCBP422(jxr_image_t image, int*diff_cbp,
589                            int channel, unsigned tx, unsigned mx, unsigned my);
590 extern int _jxr_PredCBP420(jxr_image_t image, int*diff_cbp,
591                            int channel, unsigned tx, unsigned mx, unsigned my);
592 extern void _jxr_UpdateCBPModel(jxr_image_t image);
593 
594 extern void _jxr_InitializeAdaptiveScanLP(jxr_image_t image);
595 extern void _jxr_ResetTotalsAdaptiveScanLP(jxr_image_t image);
596 
597 extern void _jxr_InitializeAdaptiveScanHP(jxr_image_t image);
598 extern void _jxr_ResetTotalsAdaptiveScanHP(jxr_image_t image);
599 
600 extern void _jxr_complete_cur_dclp(jxr_image_t image, int tx, int mx, int my);
601 extern void _jxr_propagate_hp_predictions(jxr_image_t image,
602                                           int ch, unsigned tx, unsigned mx,
603                                           int mbhp_pred_mode);
604 
605 extern void _jxr_4OverlapFilter(int*a, int*b, int*c, int*d);
606 extern void _jxr_4x4OverlapFilter(int*a, int*b, int*c, int*d,
607                                   int*e, int*f, int*g, int*h,
608                                   int*i, int*j, int*k, int*l,
609                                   int*m, int*n, int*o, int*p);
610 extern void _jxr_2OverlapFilter(int*a, int*b);
611 
612 extern void _jxr_2x2OverlapFilter(int*a, int*b, int*c, int*d);
613 
614 extern void _jxr_4PreFilter(int*a, int*b, int*c, int*d);
615 extern void _jxr_4x4PreFilter(int*a, int*b, int*c, int*d,
616                               int*e, int*f, int*g, int*h,
617                               int*i, int*j, int*k, int*l,
618                               int*m, int*n, int*o, int*p);
619 
620 extern void _jxr_2PreFilter(int*a, int*b);
621 
622 extern void _jxr_2x2PreFilter(int*a, int*b, int*c, int*d);
623 
624 extern const int _jxr_abslevel_index_delta[7];
625 
626 /*
627 * All the blocks of HP data (16 blocks, 15 coefficients per block) in
628 * a macroblock are stored together. The data is written into the
629 * stream in this order:
630 *
631 * 0 1 4 5
632 * 2 3 6 7
633 * 8 9 12 13
634 * 10 11 14 15
635 */
636 extern const int _jxr_hp_scan_map[16];
637 
638 extern uint8_t _jxr_read_lwf_test_flag();
639 extern void _jxr_4x4IPCT(int*coeff);
640 extern void _jxr_2x2IPCT(int*coeff);
641 extern void _jxr_2ptT(int*a, int*b);
642 extern void _jxr_2ptFwdT(int*a, int*b);
643 extern void _jxr_InvPermute2pt(int*a, int*b);
644 extern void _jxr_4x4PCT(int*coeff);
645 extern void _jxr_2x2PCT(int*coeff);
646 
647 
648 extern int _jxr_floor_div2(int x);
649 extern int _jxr_ceil_div2(int x);
650 
651 # define SWAP(x,y) do { int tmp = (x); (x) = (y); (y) = tmp; } while(0)
652 
653 
654 /* ***
655 * Container types
656 * ***/
657 
658 struct ifd_table{
659     uint16_t tag;
660     uint16_t type;
661     uint32_t cnt;
662 
663     union{
664         uint8_t   v_byte[4];
665         uint16_t  v_short[2];
666         uint32_t  v_long;
667         int8_t    v_sbyte[4];
668         int16_t   v_sshort[2];
669         int32_t   v_slong;
670         float     v_float;
671 
672         uint8_t  *p_byte;
673         uint16_t *p_short;
674         uint32_t *p_long;
675         uint64_t *p_rational;
676         int8_t   *p_sbyte;
677         int16_t  *p_sshort;
678         int32_t  *p_slong;
679         int64_t  *p_srational;
680         float    *p_float;
681         double   *p_double;
682     }value_;
683 };
684 
685 /*
686 * This is the container itself. It contains pointers to the tables.
687 */
688 struct jxr_container{
689     /* Number of images in the container. */
690     int image_count;
691 
692     /* IFDs for all the images. */
693     unsigned*table_cnt;
694     struct ifd_table**table;
695 
696     /* Members for managing output. */
697 
698     FILE*fd; /* File to write to. */
699     uint32_t file_mark;
700     uint32_t next_ifd_mark;
701     uint32_t image_count_mark;
702     uint32_t image_offset_mark;
703     uint32_t alpha_count_mark;
704     uint32_t alpha_offset_mark;
705     uint32_t alpha_begin_mark; /* Required to calculate bytes used up by the alpha plane */
706 
707     uint8_t image_band, alpha_band; /* for bands present */
708     uint32_t wid, hei;
709     unsigned char pixel_format[16];
710 
711     uint8_t separate_alpha_image_plane;
712 };
713 
714 extern int jxr_read_image_pixelformat(jxr_container_t c);
715 
716 extern unsigned int isEqualGUID(unsigned char guid1[16], unsigned char guid2[16]);
717 
718 #if defined(DETAILED_DEBUG)
719 # define DEBUG(...) fprintf(stdout, __VA_ARGS__)
720 #else
721 # define DEBUG(...) do { } while(0)
722 #endif
723 
724 extern const char*_jxr_vld_index_name(int vlc);
725 
726 /*
727 * $Log: jxr_priv.h,v $
728 * Revision 1.67 2009/05/29 12:00:00 microsoft
729 * Reference Software v1.6 updates.
730 *
731 * Revision 1.66 2009/04/13 12:00:00 microsoft
732 * Reference Software v1.5 updates.
733 *
734 * Revision 1.65 2008-05-13 13:47:11 thor
735 * Some experiments with a smarter selection for the quantization size,
736 * does not yet compile.
737 *
738 * Revision 1.64 2008-05-09 19:57:48 thor
739 * Reformatted for unix LF.
740 *
741 * Revision 1.63 2008-04-15 14:28:12 thor
742 * Start of the repository for the jpegxr reference software.
743 *
744 * Revision 1.62 2008/03/20 22:38:53 steve
745 * Use MB HPQP instead of first HPQP in decode.
746 *
747 * Revision 1.61 2008/03/14 00:54:08 steve
748 * Add second prefilter for YUV422 and YUV420 encode.
749 *
750 * Revision 1.60 2008/03/13 21:23:27 steve
751 * Add pipeline step for YUV420.
752 *
753 * Revision 1.59 2008/03/13 17:49:31 steve
754 * Fix problem with YUV422 CBP prediction for UV planes
755 *
756 * Add support for YUV420 encoding.
757 *
758 * Revision 1.58 2008/03/11 22:12:48 steve
759 * Encode YUV422 through DC.
760 *
761 * Revision 1.57 2008/03/06 02:05:48 steve
762 * Distributed quantization
763 *
764 * Revision 1.56 2008/03/05 00:31:17 steve
765 * Handle UNIFORM/IMAGEPLANE_UNIFORM compression.
766 *
767 * Revision 1.55 2008/03/02 18:35:27 steve
768 * Add support for BD16
769 *
770 * Revision 1.54 2008/03/01 02:46:08 steve
771 * Add support for JXR container.
772 *
773 * Revision 1.53 2008/02/28 18:50:31 steve
774 * Portability fixes.
775 *
776 * Revision 1.52 2008/02/26 23:44:25 steve
777 * Handle the special case of compiling via MS C.
778 *
779 * Revision 1.51 2008/02/01 22:49:52 steve
780 * Handle compress of YUV444 color DCONLY
781 *
782 * Revision 1.50 2008/01/08 01:06:20 steve
783 * Add first pass overlap filtering.
784 *
785 * Revision 1.49 2008/01/06 01:29:28 steve
786 * Add support for TRIM_FLEXBITS in compression.
787 *
788 * Revision 1.48 2008/01/04 17:07:35 steve
789 * API interface for setting QP values.
790 *
791 * Revision 1.47 2008/01/01 01:07:26 steve
792 * Add missing HP prediction.
793 *
794 * Revision 1.46 2007/12/30 00:16:00 steve
795 * Add encoding of HP values.
796 *
797 * Revision 1.45 2007/12/14 17:10:39 steve
798 * HP CBP Prediction
799 *
800 * Revision 1.44 2007/12/13 18:01:09 steve
801 * Stubs for HP encoding.
802 *
803 * Revision 1.43 2007/12/07 01:20:34 steve
804 * Fix adapt not adapting on line ends.
805 *
806 * Revision 1.42 2007/12/06 23:12:41 steve
807 * Stubs for LP encode operations.
808 *
809 * Revision 1.41 2007/12/04 22:06:10 steve
810 * Infrastructure for encoding LP.
811 *
812 * Revision 1.40 2007/11/30 01:50:58 steve
813 * Compression of DCONLY GRAY.
814 *
815 * Revision 1.39 2007/11/26 01:47:15 steve
816 * Add copyright notices per MS request.
817 *
818 * Revision 1.38 2007/11/21 23:26:14 steve
819 * make all strip buffers store MB data.
820 */
821 #endif
822