Coverage Report

Created: 2026-04-01 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/jpegxr/algo.c
Line
Count
Source
1
/*
2
**
3
** $Id: algo.c,v 1.3 2008-05-13 13:47:11 thor Exp $
4
**
5
**
6
*/
7
8
/*************************************************************************
9
*
10
* This software module was originally contributed by Microsoft
11
* Corporation in the course of development of the
12
* ITU-T T.832 | ISO/IEC 29199-2 ("JPEG XR") format standard for
13
* reference purposes and its performance may not have been optimized.
14
*
15
* This software module is an implementation of one or more
16
* tools as specified by the JPEG XR standard.
17
*
18
* ITU/ISO/IEC give You a royalty-free, worldwide, non-exclusive
19
* copyright license to copy, distribute, and make derivative works
20
* of this software module or modifications thereof for use in
21
* products claiming conformance to the JPEG XR standard as
22
* specified by ITU-T T.832 | ISO/IEC 29199-2.
23
*
24
* ITU/ISO/IEC give users the same free license to this software
25
* module or modifications thereof for research purposes and further
26
* ITU/ISO/IEC standardization.
27
*
28
* Those intending to use this software module in products are advised
29
* that its use may infringe existing patents. ITU/ISO/IEC have no
30
* liability for use of this software module or modifications thereof.
31
*
32
* Copyright is not released for products that do not conform to
33
* to the JPEG XR standard as specified by ITU-T T.832 |
34
* ISO/IEC 29199-2.
35
*
36
* Microsoft Corporation retains full right to modify and use the code
37
* for its own purpose, to assign or donate the code to a third party,
38
* and to inhibit third parties from using the code for products that
39
* do not conform to the JPEG XR standard as specified by ITU-T T.832 |
40
* ISO/IEC 29199-2.
41
*
42
* This copyright notice must be included in all copies or derivative
43
* works.
44
*
45
* Copyright (c) ITU-T/ISO/IEC 2008, 2009.
46
***********************************************************************/
47
48
#ifdef _MSC_VER
49
#pragma comment (user,"$Id: algo.c,v 1.3 2008-05-13 13:47:11 thor Exp $")
50
#else
51
#ident "$Id: algo.c,v 1.3 2008-05-13 13:47:11 thor Exp $"
52
#endif
53
54
/*
55
* This file contains many common algorithms of JPEGXR processing.
56
*/
57
58
# include "jxr_priv.h"
59
# include <stdio.h>
60
# include <stdlib.h>
61
# include <limits.h>
62
# include <assert.h>
63
64
static void InitVLCTable2(jxr_image_t image, int vlc_select);
65
66
const int _jxr_hp_scan_map[16] = { 0, 1, 4, 5,
67
2, 3, 6, 7,
68
8, 9,12,13,
69
10,11,14,15 };
70
71
static const unsigned ScanTotals[15] ={32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4};
72
static int long_word_flag = 0;
73
74
/*
75
* These two functions implemented floor(x/2) and ceil(x/2). Note that
76
* in C/C++ x/2 is NOT the same as floor(x/2) if x<0. It may be on
77
* some systems, but not in general. So we do all arithmetic with
78
* positive values and get the floor/ceil right by manipulating signs
79
* and rounding afterwards. The YUV444 --> RGB transform must get this
80
* rounding exactly right or there may be losses in the lossless transform.
81
*/
82
int _jxr_floor_div2(int x)
83
0
{
84
0
    if (x >= 0)
85
0
        return x/2;
86
0
    else
87
0
        return -((-x+1)/2);
88
0
}
89
90
int _jxr_ceil_div2(int x)
91
0
{
92
0
    if (x >= 0)
93
0
        return (x+1)/2;
94
0
    else
95
0
        return -((-x)/2);
96
0
}
97
98
int _jxr_quant_map(jxr_image_t image, int x, int shift)
99
0
{
100
0
    int man, exp;
101
102
0
    if (x == 0)
103
0
        return 1;
104
105
0
    if (image->scaled_flag) {
106
0
        if (x < 16) {
107
0
            man = x;
108
0
            exp = shift;
109
0
        } else {
110
0
            man = 16 + (x%16);
111
0
            exp = ((x>>4) - 1) + shift;
112
0
        }
113
0
    } else {
114
0
        if (x < 32) {
115
0
            man = (x + 3) >> 2;
116
0
            exp = 0;
117
0
        } else if (x < 48) {
118
0
            man = (16 + (x%16) + 1) >> 1;
119
0
            exp = (x>>4) - 2;
120
0
        } else {
121
0
            man = 16 + (x%16);
122
0
            exp = (x>>4) - 3;
123
0
        }
124
0
    }
125
126
0
    return man << exp;
127
0
}
128
129
int _jxr_vlc_select(int band, int chroma_flag)
130
0
{
131
0
    int vlc_select = 0;
132
133
    /* Based on the band and the chroma flag, select the vlc table
134
    that we want to use for the ABSLEVEL_INDEX decoder. */
135
0
    switch (band) {
136
0
        case 0: /* DC */
137
0
            if (chroma_flag)
138
0
                vlc_select = AbsLevelIndDCChr;
139
0
            else
140
0
                vlc_select = AbsLevelIndDCLum;
141
0
            break;
142
0
        case 1: /* LP */
143
0
            if (chroma_flag)
144
0
                vlc_select = AbsLevelIndLP1;
145
0
            else
146
0
                vlc_select = AbsLevelIndLP0;
147
0
            break;
148
0
        case 2: /* HP */
149
0
            if (chroma_flag)
150
0
                vlc_select = AbsLevelIndHP1;
151
0
            else
152
0
                vlc_select = AbsLevelIndHP0;
153
0
            break;
154
0
        default:
155
0
            assert(0);
156
0
            break;
157
0
    }
158
159
0
    return vlc_select;
160
0
}
161
162
void _jxr_InitVLCTable(jxr_image_t image, int vlc_select)
163
0
{
164
0
    struct adaptive_vlc_s*table = image->vlc_table + vlc_select;
165
0
    table->table = 0;
166
0
    table->deltatable = 0;
167
0
    table->discriminant = 0;
168
0
}
169
170
static void InitVLCTable2(jxr_image_t image, int vlc_select)
171
0
{
172
0
    struct adaptive_vlc_s*table = image->vlc_table + vlc_select;
173
0
    table->table = 1;
174
0
    table->deltatable = 0;
175
0
    table->delta2table = 1;
176
0
    table->discriminant = 0;
177
0
    table->discriminant2 = 0;
178
0
}
179
180
/*
181
* This function is for running the adapt for VLC machines that have
182
* only 2 tables. In this case, only the table and discriminant
183
* members are used, and the table is 0 or 1.
184
*/
185
void _jxr_AdaptVLCTable(jxr_image_t image, int vlc_select)
186
0
{
187
0
    const int cLowerBound = -8;
188
0
    const int cUpperBound = 8;
189
190
0
    struct adaptive_vlc_s*table = image->vlc_table + vlc_select;
191
0
    const int max_index = 1; /* Only 2 code tables. */
192
193
0
    table->deltatable = 0;
194
0
    if ((table->discriminant < cLowerBound) && (table->table != 0)) {
195
0
        table->table -= 1;
196
0
        table->discriminant = 0;
197
0
    } else if ((table->discriminant > cUpperBound) && (table->table != max_index)) {
198
0
        table->table += 1;
199
0
        table->discriminant = 0;
200
0
    } else {
201
0
        if (table->discriminant < -64) table->discriminant = -64;
202
0
        if (table->discriminant > 64) table->discriminant = 64;
203
0
    }
204
0
}
205
206
static void AdaptVLCTable2(jxr_image_t image, int vlc_select, int max_index)
207
0
{
208
0
    const int LOWER_BOUND = -8;
209
0
    const int UPPER_BOUND = 8;
210
211
0
    struct adaptive_vlc_s*table = image->vlc_table + vlc_select;
212
213
0
    int disc_lo = table->discriminant;
214
0
    int disc_hi = table->discriminant2;
215
216
0
    int change_flag = 0;
217
218
0
    if (disc_lo < LOWER_BOUND && table->table > 0) {
219
0
        table->table -= 1;
220
0
        change_flag = 1;
221
222
0
    } else if (disc_hi > UPPER_BOUND && table->table < max_index) {
223
0
        table->table += 1;
224
0
        change_flag = 1;
225
0
    }
226
227
228
0
    if (change_flag) {
229
0
        table->discriminant = 0;
230
0
        table->discriminant2 = 0;
231
232
0
        if (table->table == max_index) {
233
0
            table->deltatable = table->table - 1;
234
0
            table->delta2table = table->table - 1;
235
0
        } else if (table->table == 0) {
236
0
            table->deltatable = table->table;
237
0
            table->delta2table = table->table;
238
0
        } else {
239
0
            table->deltatable = table->table - 1;
240
0
            table->delta2table = table->table;
241
0
        }
242
243
0
    } else {
244
0
        if (table->discriminant < -64) table->discriminant = -64;
245
0
        if (table->discriminant > 64) table->discriminant = 64;
246
0
        if (table->discriminant2 < -64) table->discriminant2 = -64;
247
0
        if (table->discriminant2 > 64) table->discriminant2 = 64;
248
0
    }
249
0
}
250
251
void _jxr_AdaptLP(jxr_image_t image)
252
0
{
253
0
    AdaptVLCTable2(image, DecFirstIndLPLum, 4);
254
0
    AdaptVLCTable2(image, DecIndLPLum0, 3);
255
0
    AdaptVLCTable2(image, DecIndLPLum1, 3);
256
0
    AdaptVLCTable2(image, DecFirstIndLPChr, 4);
257
0
    AdaptVLCTable2(image, DecIndLPChr0, 3);
258
0
    AdaptVLCTable2(image, DecIndLPChr1, 3);
259
260
0
    _jxr_AdaptVLCTable(image, AbsLevelIndLP0);
261
0
    _jxr_AdaptVLCTable(image, AbsLevelIndLP1);
262
263
0
    DEBUG(" AdaptLP: DecFirstIndLPLum=%d\n", image->vlc_table[DecFirstIndLPLum].table);
264
0
    DEBUG(" : DecIndLPLum0=%d\n", image->vlc_table[DecIndLPLum0].table);
265
0
    DEBUG(" : DecIndLPLum1=%d\n", image->vlc_table[DecIndLPLum1].table);
266
0
    DEBUG(" : DecFirstIndLPChr=%d\n", image->vlc_table[DecFirstIndLPChr].table);
267
0
    DEBUG(" : DecIndLPChr0=%d\n", image->vlc_table[DecIndLPChr0].table);
268
0
    DEBUG(" : DecIndLPChr1=%d\n", image->vlc_table[DecIndLPChr1].table);
269
0
}
270
271
void _jxr_AdaptHP(jxr_image_t image)
272
0
{
273
0
    AdaptVLCTable2(image, DecFirstIndHPLum, 4);
274
0
    AdaptVLCTable2(image, DecIndHPLum0, 3);
275
0
    AdaptVLCTable2(image, DecIndHPLum1, 3);
276
0
    AdaptVLCTable2(image, DecFirstIndHPChr, 4);
277
0
    AdaptVLCTable2(image, DecIndHPChr0, 3);
278
0
    AdaptVLCTable2(image, DecIndHPChr1, 3);
279
280
0
    _jxr_AdaptVLCTable(image, AbsLevelIndHP0);
281
0
    _jxr_AdaptVLCTable(image, AbsLevelIndHP1);
282
283
0
    _jxr_AdaptVLCTable(image, DecNumCBP);
284
0
    _jxr_AdaptVLCTable(image, DecNumBlkCBP);
285
0
}
286
287
/*
288
* Despite the name, this function doesn't actually reset a
289
* context. It uses the tx (X position of the time) and mx (X position
290
* of the macroblock in the tile) to calculate true or false that the
291
* caller should invoke a context reset.
292
*/
293
int _jxr_InitContext(jxr_image_t image, unsigned tx, unsigned ty,
294
                     unsigned mx, unsigned my)
295
0
{
296
0
    if (mx == 0 && my == 0)
297
0
        return 1;
298
0
    else
299
0
        return 0;
300
0
}
301
302
/*
303
* This function guards Adapt?? functions that are called during the
304
* parse. At the end of a MB where this function returns true, the
305
* processing function will call the Adapt?? function.
306
*
307
* NOTE: It is *correct* that this function is true for the first and
308
* the last macroblock. This allows some far right context of a tile
309
* to inform the first column of the next line, but also allows crazy
310
* differences to be adapted away.
311
*/
312
int _jxr_ResetContext(jxr_image_t image, unsigned tx, unsigned mx)
313
0
{
314
0
    assert(tx < image->tile_columns);
315
0
    assert(image->tile_column_width);
316
    /* Return true for every 16 macroblocks in the tile. */
317
0
    if (mx%16 == 0)
318
0
        return 1;
319
    /* Return true for the final macroblock in the tile. */
320
0
    if (image->tile_column_width[tx] == mx+1)
321
0
        return 1;
322
323
0
    return 0;
324
0
}
325
326
int _jxr_ResetTotals(jxr_image_t image, unsigned mx)
327
0
{
328
0
    if (mx%16 == 0)
329
0
        return 1;
330
0
    else return 0;
331
0
}
332
333
void _jxr_InitializeModelMB(struct model_s*model, int band)
334
0
{
335
0
    assert(band <= 2);
336
337
0
    model->state[0] = 0;
338
0
    model->state[1] = 0;
339
0
    model->bits[0] = (2-band) * 4;
340
0
    model->bits[1] = (2-band) * 4;
341
0
}
342
343
void _jxr_UpdateModelMB(jxr_image_t image, int lap_mean[2], struct model_s*model, int band)
344
0
{
345
0
    const int modelweight = 70;
346
0
    static const int weight0[3] = { 240, 12, 1 };
347
0
    static const int weight1[3][MAX_CHANNELS] = {
348
0
        {0,240,120,80, 60,48,40,34, 30,27,24,22, 20,18,17,16 },
349
0
        {0,12,6,4, 3,2,2,2, 2,1,1,1, 1,1,1,1 },
350
0
        {0,16,8,5, 4,3,3,2, 2,2,2,1, 1,1,1,1 }
351
0
    };
352
0
    static const int weight2[6] = { 120,37,2, 120,18,1 };
353
0
    int j;
354
355
0
    assert(band < 3);
356
357
0
    lap_mean[0] *= weight0[band];
358
0
    switch (image->use_clr_fmt) {
359
0
        case 1: /* YUV420 */
360
0
            lap_mean[1] *= weight2[band];
361
0
            break;
362
0
        case 2: /* YUV422 */
363
0
            lap_mean[1] *= weight2[3+band];
364
0
            break;
365
0
        default:
366
0
            lap_mean[1] *= weight1[band][image->num_channels-1];
367
0
            if (band == 2 /*HP*/)
368
0
                lap_mean[1] >>= 4;
369
0
            break;
370
0
    }
371
372
0
    for (j = 0; j < 2; j += 1) {
373
0
        int ms = model->state[j];
374
0
        int delta = (lap_mean[j] - modelweight) >> 2;
375
376
0
        if (delta <= -8) {
377
0
            delta += 4;
378
0
            if (delta < -16)
379
0
                delta = -16;
380
0
            ms += delta;
381
0
            if (ms < -8) {
382
0
                if (model->bits[j] == 0) {
383
0
                    ms = -8;
384
0
                } else {
385
0
                    ms = 0;
386
0
                    model->bits[j] -= 1;
387
0
                }
388
0
            }
389
0
        } else if (delta >= 8) {
390
0
            delta -= 4;
391
0
            if (delta > 15)
392
0
                delta = 15;
393
0
            ms += delta;
394
0
            if (ms > 8) {
395
0
                if (model->bits[j] >= 15) {
396
0
                    model->bits[j] = 15;
397
0
                    ms = 8;
398
0
                } else {
399
0
                    ms = 0;
400
0
                    model->bits[j] += 1;
401
0
                }
402
0
            }
403
0
        }
404
0
        model->state[j] = ms;
405
        /* If clr_fmt == YONLY, then we really only want to do
406
        this loop once, for j==0. */
407
0
        if (image->use_clr_fmt == 0/*YONLY*/)
408
0
            break;
409
0
    }
410
0
}
411
412
void _jxr_InitializeCountCBPLP(jxr_image_t image)
413
0
{
414
0
    image->count_max_CBPLP = 1;
415
0
    image->count_zero_CBPLP = 1;
416
0
}
417
418
void _jxr_UpdateCountCBPLP(jxr_image_t image, int cbplp, int max)
419
0
{
420
0
    image->count_zero_CBPLP += 1;
421
0
    if (cbplp == 0)
422
0
        image->count_zero_CBPLP -= 4;
423
0
    if (image->count_zero_CBPLP > 7)
424
0
        image->count_zero_CBPLP = 7;
425
0
    if (image->count_zero_CBPLP < -8)
426
0
        image->count_zero_CBPLP = -8;
427
428
0
    image->count_max_CBPLP += 1;
429
0
    if (cbplp == max)
430
0
        image->count_max_CBPLP -= 4;
431
0
    if (image->count_max_CBPLP > 7)
432
0
        image->count_max_CBPLP = 7;
433
0
    if (image->count_max_CBPLP < -8)
434
0
        image->count_max_CBPLP = -8;
435
0
}
436
437
void _jxr_InitLPVLC(jxr_image_t image)
438
0
{
439
0
    DEBUG(" ... InitLPVLC\n");
440
0
    InitVLCTable2(image, DecFirstIndLPLum);
441
0
    InitVLCTable2(image, DecIndLPLum0);
442
0
    InitVLCTable2(image, DecIndLPLum1);
443
0
    InitVLCTable2(image, DecFirstIndLPChr);
444
0
    InitVLCTable2(image, DecIndLPChr0);
445
0
    InitVLCTable2(image, DecIndLPChr1);
446
447
0
    _jxr_InitVLCTable(image, AbsLevelIndLP0);
448
0
    _jxr_InitVLCTable(image, AbsLevelIndLP1);
449
0
}
450
451
void _jxr_InitializeAdaptiveScanLP(jxr_image_t image)
452
0
{
453
0
    static const int ScanOrderLP[15] = { 4, 1, 5,
454
0
        8, 2, 9, 6,
455
0
        12, 3, 10, 13,
456
0
        7, 14, 11, 15};
457
0
    int idx;
458
0
    for (idx = 0 ; idx < 15 ; idx += 1) {
459
0
        image->lopass_scanorder[idx] = ScanOrderLP[idx];
460
0
        image->lopass_scantotals[idx] = ScanTotals[idx];
461
0
    }
462
0
}
463
464
/*
465
*/
466
void _jxr_InitializeAdaptiveScanHP(jxr_image_t image)
467
0
{
468
0
    static const unsigned ScanOrderHor[15] ={ 4, 1, 5,
469
0
        8, 2, 9, 6,
470
0
        12, 3, 10, 13,
471
0
        7, 14, 11, 15};
472
0
    static const unsigned ScanOrderVer[15] ={ 1, 2, 5,
473
0
        4, 3, 6, 9,
474
0
        8, 7, 12, 15,
475
0
        13, 10, 11, 14};
476
0
    int idx;
477
0
    for (idx = 0 ; idx < 15 ; idx += 1) {
478
0
        image->hipass_hor_scanorder[idx] = ScanOrderHor[idx];
479
0
        image->hipass_hor_scantotals[idx] = ScanTotals[idx];
480
0
        image->hipass_ver_scanorder[idx] = ScanOrderVer[idx];
481
0
        image->hipass_ver_scantotals[idx] = ScanTotals[idx];
482
0
    }
483
0
}
484
485
void _jxr_InitializeCBPModel(jxr_image_t image)
486
0
{
487
0
    image->hp_cbp_model.state[0] = 0;
488
0
    image->hp_cbp_model.state[1] = 0;
489
0
    image->hp_cbp_model.count0[0] = -4;
490
0
    image->hp_cbp_model.count0[1] = -4;
491
0
    image->hp_cbp_model.count1[0] = 4;
492
0
    image->hp_cbp_model.count1[1] = 4;
493
0
}
494
495
void _jxr_InitHPVLC(jxr_image_t image)
496
0
{
497
0
    InitVLCTable2(image, DecFirstIndHPLum);
498
0
    InitVLCTable2(image, DecIndHPLum0);
499
0
    InitVLCTable2(image, DecIndHPLum1);
500
0
    InitVLCTable2(image, DecFirstIndHPChr);
501
0
    InitVLCTable2(image, DecIndHPChr0);
502
0
    InitVLCTable2(image, DecIndHPChr1);
503
504
0
    _jxr_InitVLCTable(image, AbsLevelIndHP0);
505
0
    _jxr_InitVLCTable(image, AbsLevelIndHP1);
506
507
    /* _jxr_InitVLCTable(image, DecNumCBP); */
508
    /* _jxr_InitVLCTable(image, DecNumBlkCBP); */
509
0
}
510
511
void _jxr_InitCBPVLC(jxr_image_t image)
512
0
{
513
0
    _jxr_InitVLCTable(image, DecNumCBP);
514
0
    _jxr_InitVLCTable(image, DecNumBlkCBP);
515
0
}
516
517
static int num_ones(int val)
518
0
{
519
0
    int cnt = 0;
520
521
0
    assert(val >= 0);
522
523
0
    while (val > 0) {
524
0
        if (val&1) cnt += 1;
525
0
        val >>= 1;
526
0
    }
527
0
    return cnt;
528
0
}
529
530
0
# define SAT(x) do { if ((x) > 15) (x)=15 ; else if ((x) < -16) (x)=-16; } while(0);
531
532
static void update_cbp_model(jxr_image_t image, int c1, int norig)
533
0
{
534
0
    const int ndiff = 3;
535
536
0
    struct cbp_model_s*hp_cbp_model = & (image->hp_cbp_model);
537
538
0
    hp_cbp_model->count0[c1] += norig - ndiff;
539
0
    SAT(hp_cbp_model->count0[c1]);
540
541
0
    hp_cbp_model->count1[c1] += 16 - norig - ndiff;
542
0
    SAT(hp_cbp_model->count1[c1]);
543
544
0
    if (hp_cbp_model->count0[c1] < 0) {
545
0
        if (hp_cbp_model->count0[c1] < hp_cbp_model->count1[c1])
546
0
            hp_cbp_model->state[c1] = 1;
547
0
        else
548
0
            hp_cbp_model->state[c1] = 2;
549
550
0
    } else if (hp_cbp_model->count1[c1] < 0) {
551
0
        hp_cbp_model->state[c1] = 2;
552
553
0
    } else {
554
0
        hp_cbp_model->state[c1] = 0;
555
0
    }
556
0
}
557
558
int _jxr_PredCBP444(jxr_image_t image, int*diff_cbp,
559
                    int channel, unsigned tx,
560
                    unsigned mx, unsigned my)
561
0
{
562
0
    int chroma_flag = 0;
563
0
    int cbp;
564
0
    int norig;
565
566
0
    if (channel > 0)
567
0
        chroma_flag = 1;
568
569
0
    DEBUG(" PredCBP444: Prediction mode = %d\n", image->hp_cbp_model.state[chroma_flag]);
570
0
    cbp = diff_cbp[channel];
571
0
    if (image->hp_cbp_model.state[chroma_flag] == 0) {
572
0
        if (mx == 0) {
573
0
            if (my == 0)
574
0
                cbp ^= 1;
575
0
            else
576
0
                cbp ^= (MACROBLK_UP1_HPCBP(image, channel, tx, mx)>>10)&1;
577
0
        } else {
578
0
            cbp ^= (MACROBLK_CUR_HPCBP(image, channel, tx, mx-1)>>5)&1;
579
0
        }
580
581
0
        cbp ^= 0x02 & (cbp<<1);
582
0
        cbp ^= 0x10 & (cbp<<3);
583
0
        cbp ^= 0x20 & (cbp<<1);
584
0
        cbp ^= (cbp&0x33) << 2;
585
0
        cbp ^= (cbp&0xcc) << 6;
586
0
        cbp ^= (cbp&0x3300) << 2;
587
588
0
    } else if (image->hp_cbp_model.state[chroma_flag] == 2) {
589
0
        cbp ^= 0xffff;
590
0
    }
591
592
0
    norig = num_ones(cbp);
593
0
    DEBUG(" PredCBP444: NOrig=%d, CBPModel.Count0/1[%d]= %d/%d\n", norig, chroma_flag,
594
0
        image->hp_cbp_model.count0[chroma_flag],
595
0
        image->hp_cbp_model.count1[chroma_flag]);
596
0
    update_cbp_model(image, chroma_flag, norig);
597
0
    DEBUG(" PredCBP444: ...becomes CBPModel.Count0/1[%d]= %d/%d, new state=%d\n",
598
0
        chroma_flag, image->hp_cbp_model.count0[chroma_flag],
599
0
        image->hp_cbp_model.count1[chroma_flag], image->hp_cbp_model.state[chroma_flag]);
600
0
    return cbp;
601
0
}
602
603
void _jxr_w_PredCBP444(jxr_image_t image, int ch, unsigned tx, unsigned mx, int my)
604
0
{
605
0
    int chroma_flag = 0;
606
0
    int cbp;
607
0
    int norig;
608
0
    if (ch > 0)
609
0
        chroma_flag = 1;
610
611
0
    DEBUG(" PredCBP444: Prediction mode = %d\n", image->hp_cbp_model.state[chroma_flag]);
612
0
    cbp = MACROBLK_UP1_HPCBP(image,ch,tx,mx);
613
0
    norig = num_ones(cbp);
614
615
0
    DEBUG(" PredCBP444: ... cbp starts as 0x%x\n", cbp);
616
617
0
    if (image->hp_cbp_model.state[chroma_flag] == 0) {
618
619
0
        cbp ^= (cbp&0x3300) << 2;
620
0
        cbp ^= (cbp&0xcc) << 6;
621
0
        cbp ^= (cbp&0x33) << 2;
622
0
        cbp ^= 0x20 & (cbp<<1);
623
0
        cbp ^= 0x10 & (cbp<<3);
624
0
        cbp ^= 0x02 & (cbp<<1);
625
626
0
        if (mx == 0) {
627
0
            if (my == 0)
628
0
                cbp ^= 1;
629
0
            else
630
0
                cbp ^= (MACROBLK_CUR_HPCBP(image,ch, tx, mx)>>10)&1;
631
0
        } else {
632
0
            cbp ^= (MACROBLK_UP1_HPCBP(image,ch, tx, mx-1)>>5)&1;
633
0
        }
634
635
636
0
    } else if (image->hp_cbp_model.state[chroma_flag] == 2){
637
0
        cbp ^= 0xffff;
638
0
    }
639
640
0
    DEBUG(" PredCBP444: ... diff_cbp 0x%04x\n", cbp);
641
0
    MACROBLK_UP1(image,ch,tx,mx).hp_diff_cbp = cbp;
642
643
0
    update_cbp_model(image, chroma_flag, norig);
644
0
}
645
646
int _jxr_PredCBP422(jxr_image_t image, int*diff_cbp,
647
                    int channel, unsigned tx,
648
                    unsigned mx, unsigned my)
649
0
{
650
0
    int cbp;
651
0
    int norig;
652
653
0
    assert(channel > 0);
654
0
    DEBUG(" PredCBP422: Prediction mode = %d, channel=%d, cbp_mode.State[1]=%d\n",
655
0
        image->hp_cbp_model.state[1], channel, image->hp_cbp_model.state[1]);
656
0
    cbp = diff_cbp[channel];
657
658
0
    if (image->hp_cbp_model.state[1] == 0) {
659
0
        if (mx == 0) {
660
0
            if (my == 0)
661
0
                cbp ^= 1;
662
0
            else
663
0
                cbp ^= (MACROBLK_UP1_HPCBP(image, channel, tx, mx)>>6)&1;
664
0
        } else {
665
0
            cbp ^= (MACROBLK_CUR_HPCBP(image, channel, tx, mx-1)>>1)&1;
666
0
        }
667
668
0
        cbp ^= 0x02 & (cbp<<1);
669
0
        cbp ^= 0x0c & (cbp<<2);
670
0
        cbp ^= 0x30 & (cbp<<2);
671
0
        cbp ^= 0xc0 & (cbp<<2);
672
0
    } else if (image->hp_cbp_model.state[1] == 2) {
673
0
        cbp ^= 0xff;
674
0
    }
675
676
0
    norig = num_ones(cbp) * 2;
677
0
    update_cbp_model(image, 1, norig);
678
679
0
    return cbp;
680
0
}
681
682
void _jxr_w_PredCBP422(jxr_image_t image, int ch, unsigned tx, unsigned mx, int my)
683
0
{
684
0
    int cbp;
685
0
    int norig;
686
687
0
    assert(ch > 0);
688
0
    DEBUG(" PredCBP422: Prediction mode = %d\n", image->hp_cbp_model.state[1]);
689
0
    cbp = MACROBLK_UP1_HPCBP(image,ch,tx,mx);
690
0
    norig = num_ones(cbp) * 2;
691
692
0
    DEBUG(" PredCBP422: ... cbp[%d] starts as 0x%x\n", ch, cbp);
693
694
0
    if (image->hp_cbp_model.state[1] == 0) {
695
696
0
        cbp ^= 0xc0 & (cbp<<2);
697
0
        cbp ^= 0x30 & (cbp<<2);
698
0
        cbp ^= 0x0c & (cbp<<2);
699
0
        cbp ^= 0x02 & (cbp<<1);
700
701
0
        if (mx == 0) {
702
0
            if (my == 0)
703
0
                cbp ^= 1;
704
0
            else
705
0
                cbp ^= (MACROBLK_CUR_HPCBP(image, ch, tx, mx)>>6)&1;
706
0
        } else {
707
0
            cbp ^= (MACROBLK_UP1_HPCBP(image, ch, tx, mx-1)>>1)&1;
708
0
        }
709
710
0
    } else if (image->hp_cbp_model.state[1] == 2) {
711
0
        cbp ^= 0xff;
712
0
    }
713
714
0
    DEBUG(" PredCBP422: ... diff_cbp 0x%04x\n", cbp);
715
0
    MACROBLK_UP1(image,ch,tx,mx).hp_diff_cbp = cbp;
716
717
0
    update_cbp_model(image, 1, norig);
718
0
}
719
720
721
int _jxr_PredCBP420(jxr_image_t image, int*diff_cbp,
722
                    int channel, unsigned tx,
723
                    unsigned mx, unsigned my)
724
0
{
725
0
    int cbp;
726
0
    int norig;
727
0
    assert(channel > 0);
728
0
    DEBUG(" PredCBP420: Prediction mode = %d, channel=%d, cbp_mode.State[1]=%d\n",
729
0
        image->hp_cbp_model.state[1], channel, image->hp_cbp_model.state[1]);
730
0
    cbp = diff_cbp[channel];
731
732
0
    if (image->hp_cbp_model.state[1] == 0) {
733
0
        if (mx == 0) {
734
0
            if (my == 0)
735
0
                cbp ^= 1;
736
0
            else
737
0
                cbp ^= (MACROBLK_UP1_HPCBP(image, channel, tx, mx)>>2)&1;
738
0
        } else {
739
0
            cbp ^= (MACROBLK_CUR_HPCBP(image, channel, tx, mx-1)>>1)&1;
740
0
        }
741
742
0
        cbp ^= 0x02 & (cbp<<1);
743
0
        cbp ^= 0x0c & (cbp<<2);
744
0
    } else if (image->hp_cbp_model.state[1] == 2) {
745
0
        cbp ^= 0xf;
746
0
    }
747
748
0
    norig = num_ones(cbp) * 4;
749
0
    update_cbp_model(image, 1, norig);
750
751
0
    return cbp;
752
0
}
753
754
void _jxr_w_PredCBP420(jxr_image_t image, int ch, unsigned tx, unsigned mx, int my)
755
0
{
756
0
    int cbp;
757
0
    int norig;
758
0
    assert(ch > 0);
759
0
    DEBUG(" PredCBP420: Prediction mode = %d\n", image->hp_cbp_model.state[1]);
760
0
    cbp = MACROBLK_UP1_HPCBP(image,ch,tx,mx);
761
0
    norig = num_ones(cbp) * 4;
762
763
0
    DEBUG(" PredCBP420: ... cbp[%d] starts as 0x%x\n", ch, cbp);
764
765
0
    if (image->hp_cbp_model.state[1] == 0) {
766
767
0
        cbp ^= 0x0c & (cbp<<2);
768
0
        cbp ^= 0x02 & (cbp<<1);
769
770
0
        if (mx == 0) {
771
0
            if (my == 0)
772
0
                cbp ^= 1;
773
0
            else
774
0
                cbp ^= (MACROBLK_CUR_HPCBP(image, ch, tx, mx)>>2)&1;
775
0
        } else {
776
0
            cbp ^= (MACROBLK_UP1_HPCBP(image, ch, tx, mx-1)>>1)&1;
777
0
        }
778
779
0
    } else if (image->hp_cbp_model.state[1] == 2) {
780
0
        cbp ^= 0xf;
781
0
    }
782
783
0
    DEBUG(" PredCBP420: ... diff_cbp 0x%04x\n", cbp);
784
0
    MACROBLK_UP1(image,ch,tx,mx).hp_diff_cbp = cbp;
785
786
0
    update_cbp_model(image, 1, norig);
787
0
}
788
789
void _jxr_ResetTotalsAdaptiveScanLP(jxr_image_t image)
790
0
{
791
0
    int idx;
792
0
    for (idx = 0 ; idx < 15 ; idx += 1) {
793
0
        image->lopass_scantotals[idx] = ScanTotals[idx];
794
0
    }
795
0
}
796
797
void _jxr_ResetTotalsAdaptiveScanHP(jxr_image_t image)
798
0
{
799
0
    int idx;
800
0
    for (idx = 0 ; idx < 15 ; idx += 1) {
801
0
        image->hipass_hor_scantotals[idx] = ScanTotals[idx];
802
0
        image->hipass_ver_scantotals[idx] = ScanTotals[idx];
803
0
    }
804
0
}
805
806
static int
807
calculate_mbdc_mode(jxr_image_t image, int tx, int mx, int my)
808
0
{
809
0
    long left;
810
0
    long top;
811
0
    long topleft;
812
0
    long strhor;
813
0
    long strvert;
814
815
0
    if (mx == 0 && my == 0)
816
0
        return 3; /* No prediction. */
817
818
0
    if (mx == 0)
819
0
        return 1; /* Predictions from top only. */
820
821
0
    if (my == 0)
822
0
        return 0; /* prediction from left only */
823
824
0
    left = MACROBLK_CUR_DC(image, 0, tx, mx-1);
825
0
    top = MACROBLK_UP_DC(image, 0, tx, mx);
826
0
    topleft = MACROBLK_UP_DC(image, 0, tx, mx-1);
827
828
0
    strhor = 0;
829
0
    strvert = 0;
830
0
    if (image->use_clr_fmt==0 || image->use_clr_fmt==6) {/* YONLY or NCOMPONENT */
831
832
0
        strhor = labs(topleft - left);
833
0
        strvert = labs(topleft - top);
834
0
    } else {
835
0
        long left_u = MACROBLK_CUR_DC(image, 1, tx, mx-1);
836
0
        long top_u = MACROBLK_UP_DC(image, 1, tx, mx);
837
0
        long topleft_u = MACROBLK_UP_DC(image, 1, tx, mx-1);
838
0
        long left_v = MACROBLK_CUR_DC(image, 2, tx, mx-1);
839
0
        long top_v = MACROBLK_UP_DC(image, 2, tx, mx);
840
0
        long topleft_v = MACROBLK_UP_DC(image, 2, tx, mx-1);
841
842
0
        long scale = 2;
843
0
        if (image->use_clr_fmt == 2 /*YUV422*/)
844
0
            scale = 4;
845
0
        if (image->use_clr_fmt == 1 /*YUV420*/)
846
0
            scale = 8;
847
848
0
        strhor = labs(topleft - left)*scale + labs(topleft_u - left_u) + labs(topleft_v - left_v);
849
0
        strvert = labs(topleft - top)*scale + labs(topleft_u - top_u) + labs(topleft_v - top_v);
850
0
    }
851
852
0
    if ((strhor*4) < strvert)
853
0
        return 1;
854
855
0
    if ((strvert*4) < strhor)
856
0
        return 0;
857
858
0
    return 2;
859
0
}
860
861
static void predict_lp444(jxr_image_t image, int tx, int mx, int my, int ch, int mblp_mode);
862
static void predict_lp422(jxr_image_t image, int tx, int mx, int my, int ch, int mblp_mode, int mbdc_mode);
863
static void predict_lp420(jxr_image_t image, int tx, int mx, int my, int ch, int mblp_mode);
864
865
void _jxr_complete_cur_dclp(jxr_image_t image, int tx, int mx, int my)
866
0
{
867
    /* Calculate the mbcd prediction mode. This mode is used for
868
    all the planes of DC data. */
869
0
    int mbdc_mode = calculate_mbdc_mode(image, tx, mx, image->cur_my);
870
0
    int mblp_mode;
871
    /* Now process all the planes of DC data. */
872
0
    int ch;
873
0
    for (ch = 0 ; ch < image->num_channels ; ch += 1) {
874
0
        long left = mx>0? MACROBLK_CUR_DC(image,ch,tx,mx-1) : 0;
875
0
        long top = MACROBLK_UP_DC(image,ch,tx,mx);
876
877
0
        DEBUG(" MBDC_MODE=%d for TX=%d, MBx=%d, MBy=%d (cur_my=%d), ch=%d, left=0x%lx, top=0x%lx, cur=0x%x\n",
878
0
            mbdc_mode, tx, mx, my, image->cur_my, ch, left, top, MACROBLK_CUR_DC(image,ch,tx,mx));
879
880
0
        MACROBLK_CUR(image,ch,tx,mx).pred_dclp[0] = MACROBLK_CUR_DC(image,ch,tx,mx);
881
0
        CHECK1(image->lwf_test, MACROBLK_CUR_DC(image,ch,tx,mx));
882
0
        switch (mbdc_mode) {
883
0
            case 0: /* left */
884
0
                MACROBLK_CUR_DC(image,ch,tx,mx) += left;
885
0
                break;
886
0
            case 1: /* top */
887
0
                MACROBLK_CUR_DC(image,ch,tx,mx) += top;
888
0
                break;
889
0
            case 2:/* top and left */
890
                /* Note that we really MEAN >>1 and NOT /2. in
891
                particular, if the sum is -1, we want the
892
                result to also be -1. That extra stuff after
893
                the "|" is there to make sure the sign bit is
894
                not lost. Also, the chroma planes for YUV42X
895
                formats round *up* the mean, where all other
896
                planes round down. */
897
0
                if (ch>0 && (image->use_clr_fmt==1/*YUV420*/||image->use_clr_fmt==2/*YUV422*/))
898
0
                    MACROBLK_CUR_DC(image,ch,tx,mx) += (left+top+1) >> 1 | ((left+top+1)&~INT_MAX);
899
0
                else
900
0
                    MACROBLK_CUR_DC(image,ch,tx,mx) += (left+top) >> 1 | ((left+top)&~INT_MAX);
901
0
                break;
902
0
            default:
903
0
                break;
904
0
        }
905
0
    }
906
907
0
    mblp_mode = 0;
908
0
    if (mbdc_mode==0 && MACROBLK_CUR_LP_QUANT(image,0,tx,mx) == MACROBLK_CUR_LP_QUANT(image,0,tx,mx-1)) {
909
0
        mblp_mode = 0;
910
0
    } else if (mbdc_mode==1 && MACROBLK_CUR_LP_QUANT(image,0,tx,mx) == MACROBLK_UP1_LP_QUANT(image,0,tx,mx)) {
911
0
        mblp_mode = 1;
912
913
0
    } else {
914
0
        mblp_mode = 2;
915
0
    }
916
917
0
    DEBUG(" MBLP_MODE=%d for MBx=%d, MBy=%d (lp_quant=%d,lp_quant_ctx=%d)\n", mblp_mode, mx, image->cur_my,
918
0
        MACROBLK_CUR_LP_QUANT(image,0,tx,mx),
919
0
        mbdc_mode==0? MACROBLK_CUR_LP_QUANT(image,0,tx,mx-1) : mbdc_mode==1 ? MACROBLK_UP1_LP_QUANT(image,0,tx,mx) : -1);
920
921
0
    predict_lp444(image, tx, mx, my, 0, mblp_mode);
922
0
    for (ch = 1 ; ch < image->num_channels ; ch += 1) {
923
0
        switch (image->use_clr_fmt) {
924
0
            case 1: /* YUV420 */
925
0
                predict_lp420(image, tx, mx, my, ch, mblp_mode);
926
0
                break;
927
0
            case 2: /* YUV422 */
928
0
                predict_lp422(image, tx, mx, my, ch, mblp_mode, mbdc_mode);
929
0
                break;
930
0
            default:
931
0
                predict_lp444(image,tx, mx, my, ch, mblp_mode);
932
0
                break;
933
0
        }
934
0
    }
935
0
}
936
937
static void predict_lp444(jxr_image_t image, int tx, int mx, int my, int ch, int mblp_mode)
938
0
{
939
#if defined(DETAILED_DEBUG)
940
    {
941
        int jdx;
942
        DEBUG(" DC/LP (strip=%3d, mbx=%4d, ch=%d) Difference:", my, mx, ch);
943
        DEBUG(" 0x%08x", MACROBLK_CUR(image,ch,tx,mx).pred_dclp[0]);
944
        for (jdx = 0; jdx < 15 ; jdx += 1) {
945
            DEBUG(" 0x%08x", MACROBLK_CUR_LP(image,ch,tx,mx,jdx));
946
            if ((jdx+1)%4 == 3 && jdx != 14)
947
                DEBUG("\n%*s:", 46, "");
948
        }
949
        DEBUG("\n");
950
    }
951
#endif
952
953
0
    switch (mblp_mode) {
954
0
        case 0: /* left */
955
0
            CHECK3(image->lwf_test, MACROBLK_CUR_LP(image,ch,tx,mx,3), MACROBLK_CUR_LP(image,ch,tx,mx,7), MACROBLK_CUR_LP(image,ch,tx,mx,11));
956
0
            MACROBLK_CUR_LP(image,ch,tx,mx,3) += MACROBLK_CUR(image,ch,tx,mx-1).pred_dclp[4];
957
0
            MACROBLK_CUR_LP(image,ch,tx,mx,7) += MACROBLK_CUR(image,ch,tx,mx-1).pred_dclp[5];
958
0
            MACROBLK_CUR_LP(image,ch,tx,mx,11)+= MACROBLK_CUR(image,ch,tx,mx-1).pred_dclp[6];
959
0
            break;
960
0
        case 1: /* up */
961
0
            CHECK3(image->lwf_test, MACROBLK_CUR_LP(image,ch,tx,mx,0), MACROBLK_CUR_LP(image,ch,tx,mx,1), MACROBLK_CUR_LP(image,ch,tx,mx,2));
962
0
            MACROBLK_CUR_LP(image,ch,tx,mx,0) += MACROBLK_UP1(image,ch,tx,mx).pred_dclp[1];
963
0
            MACROBLK_CUR_LP(image,ch,tx,mx,1) += MACROBLK_UP1(image,ch,tx,mx).pred_dclp[2];
964
0
            MACROBLK_CUR_LP(image,ch,tx,mx,2) += MACROBLK_UP1(image,ch,tx,mx).pred_dclp[3];
965
0
            break;
966
0
        case 2:
967
0
            break;
968
0
    }
969
970
#if defined(DETAILED_DEBUG)
971
    {
972
        int jdx;
973
        DEBUG(" DC/LP (strip=%3d, mbx=%4d, ch=%d) Predicted:", my, mx, ch);
974
        DEBUG(" 0x%08x", MACROBLK_CUR_DC(image,ch,tx,mx));
975
        for (jdx = 0; jdx < 15 ; jdx += 1) {
976
            DEBUG(" 0x%08x", MACROBLK_CUR_LP(image,ch,tx,mx,jdx));
977
            if ((jdx+1)%4 == 3 && jdx != 14)
978
                DEBUG("\n%*s:", 45, "");
979
        }
980
        DEBUG("\n");
981
    }
982
#endif
983
984
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[1] = MACROBLK_CUR_LP(image,ch,tx,mx,0);
985
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[2] = MACROBLK_CUR_LP(image,ch,tx,mx,1);
986
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[3] = MACROBLK_CUR_LP(image,ch,tx,mx,2);
987
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[4] = MACROBLK_CUR_LP(image,ch,tx,mx,3);
988
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[5] = MACROBLK_CUR_LP(image,ch,tx,mx,7);
989
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[6] = MACROBLK_CUR_LP(image,ch,tx,mx,11);
990
0
}
991
992
static void predict_lp422(jxr_image_t image, int tx, int mx, int my, int ch, int mblp_mode, int mbdc_mode)
993
0
{
994
#if defined(DETAILED_DEBUG)
995
    {
996
        int jdx;
997
        DEBUG(" DC/LP (strip=%3d, tx=%d, mbx=%4d, ch=%d) Difference:", my, tx, mx, ch);
998
        DEBUG(" 0x%08x", MACROBLK_CUR(image,ch,tx,mx).pred_dclp[0]);
999
        for (jdx = 0; jdx < 7 ; jdx += 1) {
1000
            DEBUG(" 0x%08x", MACROBLK_CUR_LP(image,ch,tx,mx,jdx));
1001
            if ((jdx+1)%4 == 3 && jdx != 6)
1002
                DEBUG("\n%*s:", 52, "");
1003
        }
1004
        DEBUG("\n");
1005
    }
1006
#endif
1007
1008
0
    switch (mblp_mode) {
1009
0
case 0: /* left */
1010
0
    CHECK3(image->lwf_test, MACROBLK_CUR_LP(image,ch,tx,mx,3), MACROBLK_CUR_LP(image,ch,tx,mx,1), MACROBLK_CUR_LP(image,ch,tx,mx,5));
1011
0
    MACROBLK_CUR_LP(image,ch,tx,mx,3) += MACROBLK_CUR(image,ch,tx,mx-1).pred_dclp[4];
1012
0
    MACROBLK_CUR_LP(image,ch,tx,mx,1) += MACROBLK_CUR(image,ch,tx,mx-1).pred_dclp[2];
1013
0
    MACROBLK_CUR_LP(image,ch,tx,mx,5) += MACROBLK_CUR(image,ch,tx,mx-1).pred_dclp[6];
1014
0
    break;
1015
0
case 1: /* up */
1016
0
    CHECK3(image->lwf_test, MACROBLK_CUR_LP(image,ch,tx,mx,3), MACROBLK_CUR_LP(image,ch,tx,mx,0), MACROBLK_CUR_LP(image,ch,tx,mx,4));
1017
0
    MACROBLK_CUR_LP(image,ch,tx,mx,3) += MACROBLK_UP1(image,ch,tx,mx).pred_dclp[4];
1018
0
    MACROBLK_CUR_LP(image,ch,tx,mx,0) += MACROBLK_UP1(image,ch,tx,mx).pred_dclp[5];
1019
0
    MACROBLK_CUR_LP(image,ch,tx,mx,4) += MACROBLK_CUR_LP(image,ch,tx,mx,0);
1020
0
    break;
1021
0
case 2:
1022
0
    if (mbdc_mode == 1)
1023
0
    {
1024
0
        CHECK1(image->lwf_test, MACROBLK_CUR_LP(image,ch,tx,mx,4));
1025
0
        MACROBLK_CUR_LP(image,ch,tx,mx,4) += MACROBLK_CUR_LP(image,ch,tx,mx,0);
1026
0
    }
1027
0
    break;
1028
0
    }
1029
1030
#if defined(DETAILED_DEBUG)
1031
    {
1032
        int jdx;
1033
        DEBUG(" DC/LP (strip=%3d, tx=%d, mbx=%4d, ch=%d) Predicted:", my, tx, mx, ch);
1034
        DEBUG(" 0x%08x", MACROBLK_CUR_DC(image,ch,tx,mx));
1035
        for (jdx = 0; jdx < 7 ; jdx += 1) {
1036
            DEBUG(" 0x%08x", MACROBLK_CUR_LP(image,ch,tx,mx,jdx));
1037
            if ((jdx+1)%4 == 3 && jdx != 6)
1038
                DEBUG("\n%*s:", 51, "");
1039
        }
1040
        DEBUG("\n");
1041
    }
1042
#endif
1043
1044
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[1] = MACROBLK_CUR_LP(image,ch,tx,mx,0);
1045
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[2] = MACROBLK_CUR_LP(image,ch,tx,mx,1);
1046
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[4] = MACROBLK_CUR_LP(image,ch,tx,mx,3);
1047
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[5] = MACROBLK_CUR_LP(image,ch,tx,mx,4);
1048
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[6] = MACROBLK_CUR_LP(image,ch,tx,mx,5);
1049
0
}
1050
1051
static void predict_lp420(jxr_image_t image, int tx, int mx, int my, int ch, int mblp_mode)
1052
0
{
1053
0
    switch (mblp_mode) {
1054
0
        case 0: /* left */
1055
0
            CHECK1(image->lwf_test, MACROBLK_CUR_LP(image,ch,tx,mx,1));
1056
0
            MACROBLK_CUR_LP(image,ch,tx,mx,1) += MACROBLK_CUR(image,ch,tx,mx-1).pred_dclp[2];
1057
0
            break;
1058
0
        case 1: /* up */
1059
0
            CHECK1(image->lwf_test, MACROBLK_CUR_LP(image,ch,tx,mx,0));
1060
0
            MACROBLK_CUR_LP(image,ch,tx,mx,0) += MACROBLK_UP1(image,ch,tx,mx).pred_dclp[1];
1061
0
            break;
1062
0
    }
1063
1064
#if defined(DETAILED_DEBUG)
1065
    {
1066
        int jdx;
1067
        DEBUG(" DC/LP (strip=%3d, tx=%d, mbx=%4d, ch=%d) Predicted:", tx, my, mx, ch);
1068
        DEBUG(" 0x%08x", MACROBLK_CUR_DC(image,ch,tx,mx));
1069
        for (jdx = 0; jdx < 3 ; jdx += 1) {
1070
            DEBUG(" 0x%08x", MACROBLK_CUR_LP(image,ch,tx,mx,jdx));
1071
        }
1072
        DEBUG("\n");
1073
    }
1074
#endif
1075
1076
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[1] = MACROBLK_CUR_LP(image,ch,tx,mx,0);
1077
0
    MACROBLK_CUR(image,ch,tx,mx).pred_dclp[2] = MACROBLK_CUR_LP(image,ch,tx,mx,1);
1078
0
}
1079
1080
/*
1081
* TRANSFORM functions, forward and inverse.
1082
*/
1083
static void _InvPermute(int*coeff)
1084
0
{
1085
0
    static const int inverse[16] = {0, 8, 4, 13,
1086
0
        2, 15, 3, 14,
1087
0
        1, 12, 5, 9,
1088
0
        7, 11, 6, 10};
1089
0
    int t[16];
1090
0
    int idx;
1091
1092
0
    for (idx = 0 ; idx < 16 ; idx += 1)
1093
0
        t[inverse[idx]] = coeff[idx];
1094
0
    for (idx = 0 ; idx < 16 ; idx += 1)
1095
0
        coeff[idx] = t[idx];
1096
0
}
1097
1098
static void _FwdPermute(int*coeff)
1099
0
{
1100
0
    static const int fwd[16] = {0, 8, 4, 6,
1101
0
        2, 10, 14, 12,
1102
0
        1, 11, 15, 13,
1103
0
        9, 3, 7, 5};
1104
1105
0
    int t[16];
1106
0
    int idx;
1107
0
    for (idx = 0 ; idx < 16 ; idx += 1)
1108
0
        t[fwd[idx]] = coeff[idx];
1109
0
    for (idx = 0 ; idx < 16 ; idx += 1)
1110
0
        coeff[idx] = t[idx];
1111
0
}
1112
1113
static void _2x2T_h(int*a, int*b, int*c, int*d, int R_flag)
1114
0
{
1115
0
    int t1;
1116
0
    int t2;
1117
0
    *a += *d;
1118
0
    *b -= *c;
1119
1120
0
    t1 = ((*a - *b + R_flag) >> 1);
1121
0
    t2 = *c;
1122
1123
0
    *c = t1 - *d;
1124
0
    *d = t1 - t2;
1125
0
    CHECK5(long_word_flag, *a, *b, t1, *c, *d);
1126
0
    *a -= *d;
1127
0
    *b += *c;
1128
0
    CHECK2(long_word_flag, *a, *b);
1129
0
}
1130
1131
static void _2x2T_h_POST(int*a, int*b, int*c, int*d)
1132
0
{
1133
0
    int t1;
1134
0
    *b -= *c;
1135
0
    *a += (*d * 3 + 4) >> 3;
1136
0
    *d -= *b >> 1;
1137
0
    t1 = ((*a - *b) >> 1) - *c;
1138
0
    CHECK4(long_word_flag, *b, *a, *d, t1);
1139
0
    *c = *d;
1140
0
    *d = t1;
1141
0
    *a -= *d;
1142
0
    *b += *c;
1143
0
    CHECK2(long_word_flag, *a, *b);
1144
0
}
1145
1146
static void _2x2T_h_Enc(int*a, int*b, int*c, int*d)
1147
0
{
1148
0
    int t1;
1149
0
    int t2;
1150
0
    *a += *d;
1151
0
    *b -= *c;
1152
0
    CHECK2(long_word_flag, *a, *b);
1153
0
    t1 = *d;
1154
0
    t2 = *c;
1155
0
    *c = ((*a - *b) >> 1) - t1;
1156
0
    *d = t2 + (*b >> 1);
1157
0
    *b += *c;
1158
0
    *a -= (*d * 3 + 4) >> 3;
1159
0
    CHECK4(long_word_flag, *c, *d, *b, *a);
1160
0
}
1161
1162
static void _InvT_odd(int*a, int*b, int*c, int*d)
1163
0
{
1164
0
    *b += *d;
1165
0
    *a -= *c;
1166
0
    *d -= *b >> 1;
1167
0
    *c += (*a + 1) >> 1;
1168
0
    CHECK4(long_word_flag, *a, *b, *c, *d);
1169
1170
0
    *a -= ((*b)*3 + 4) >> 3;
1171
0
    *b += ((*a)*3 + 4) >> 3;
1172
0
    *c -= ((*d)*3 + 4) >> 3;
1173
0
    *d += ((*c)*3 + 4) >> 3;
1174
0
    CHECK4(long_word_flag, *a, *b, *c, *d);
1175
1176
0
    *c -= (*b + 1) >> 1;
1177
0
    *d = ((*a + 1) >> 1) - *d;
1178
0
    *b += *c;
1179
0
    *a -= *d;
1180
0
    CHECK4(long_word_flag, *a, *b, *c, *d);
1181
0
}
1182
1183
static void _InvT_odd_odd(int*a, int*b, int*c, int*d)
1184
0
{
1185
0
    int t1, t2;
1186
0
    *d += *a;
1187
0
    *c -= *b;
1188
0
    t1 = *d >> 1;
1189
0
    t2 = *c >> 1;
1190
0
    *a -= t1;
1191
0
    *b += t2;
1192
0
    CHECK4(long_word_flag, *a, *b, *c, *d);
1193
1194
0
    *a -= ((*b)*3 + 3) >> 3;
1195
0
    *b += ((*a)*3 + 3) >> 2;
1196
0
    CHECK2(long_word_flag, *a, *b);
1197
0
    *a -= ((*b)*3 + 4) >> 3;
1198
1199
0
    *b -= t2;
1200
0
    CHECK2(long_word_flag, *a, *b);
1201
0
    *a += t1;
1202
0
    *c += *b;
1203
0
    *d -= *a;
1204
1205
0
    *b = -*b;
1206
0
    *c = -*c;
1207
0
    CHECK4(long_word_flag, *a, *b, *c, *d);
1208
0
}
1209
1210
static void _InvT_odd_odd_POST(int*a, int*b, int*c, int*d)
1211
0
{
1212
0
    int t1, t2;
1213
1214
0
    *d += *a;
1215
0
    *c -= *b;
1216
0
    t1 = *d >> 1;
1217
0
    t2 = *c >> 1;
1218
0
    *a -= t1;
1219
0
    *b += t2;
1220
0
    CHECK4(long_word_flag, *d, *c, *a, *b);
1221
1222
0
    *a -= (*b * 3 + 6) >> 3;
1223
0
    *b += (*a * 3 + 2) >> 2;
1224
0
    CHECK2(long_word_flag, *a, *b);
1225
0
    *a -= (*b * 3 + 4) >> 3;
1226
1227
0
    *b -= t2;
1228
0
    CHECK2(long_word_flag, *a, *b);
1229
0
    *a += t1;
1230
0
    *c += *b;
1231
0
    *d -= *a;
1232
0
    CHECK3(long_word_flag, *a, *c, *d);
1233
0
}
1234
1235
static void _T_odd(int*a, int*b, int*c, int*d)
1236
0
{
1237
0
    *b -= *c;
1238
0
    *a += *d;
1239
0
    *c += (*b + 1) >> 1;
1240
0
    *d = ((*a + 1) >> 1) - *d;
1241
0
    CHECK4(long_word_flag, *b, *a, *c, *d);
1242
1243
0
    *b -= (*a * 3 + 4) >> 3;
1244
0
    *a += (*b * 3 + 4) >> 3;
1245
0
    *d -= (*c * 3 + 4) >> 3;
1246
0
    *c += (*d * 3 + 4) >> 3;
1247
0
    CHECK4(long_word_flag, *b, *a, *d, *c);
1248
1249
0
    *d += *b >> 1;
1250
0
    *c -= (*a + 1) >> 1;
1251
0
    *b -= *d;
1252
0
    *a += *c;
1253
0
    CHECK4(long_word_flag, *d, *c, *b, *a);
1254
0
}
1255
1256
static void _T_odd_odd(int*a, int*b, int*c, int*d)
1257
0
{
1258
0
    int t1;
1259
0
    int t2;
1260
0
    *b = -*b;
1261
0
    *c = -*c;
1262
0
    CHECK2(long_word_flag, *b, *c);
1263
1264
0
    *d += *a;
1265
0
    *c -= *b;
1266
0
    t1 = *d >> 1;
1267
0
    t2 = *c >> 1;
1268
0
    *a -= t1;
1269
0
    *b += t2;
1270
0
    CHECK4(long_word_flag, *d, *c, *a, *b);
1271
1272
0
    *a += (*b * 3 + 4) >> 3;
1273
0
    *b -= (*a * 3 + 3) >> 2;
1274
0
    CHECK2(long_word_flag, *a, *b);
1275
0
    *a += (*b * 3 + 3) >> 3;
1276
1277
0
    *b -= t2;
1278
0
    CHECK2(long_word_flag, *a, *b);
1279
0
    *a += t1;
1280
0
    *c += *b;
1281
0
    *d -= *a;
1282
0
    CHECK3(long_word_flag, *a, *c, *d);
1283
0
}
1284
1285
void _jxr_InvPermute2pt(int*a, int*b)
1286
0
{
1287
0
    int t1 = *a;
1288
0
    *a = *b;
1289
0
    *b = t1;
1290
0
}
1291
1292
void _jxr_2ptT(int*a, int*b)
1293
0
{
1294
0
    *a -= (*b + 1) >> 1;
1295
0
    *b += *a;
1296
0
    CHECK2(long_word_flag, *a, *b);
1297
0
}
1298
1299
/* This is the inverse of the 2ptT function */
1300
void _jxr_2ptFwdT(int*a, int*b)
1301
0
{
1302
0
    *b -= *a;
1303
0
    *a += (*b + 1) >> 1;
1304
0
    CHECK2(long_word_flag, *b, *a);
1305
0
}
1306
1307
void _jxr_2x2IPCT(int*coeff)
1308
0
{
1309
0
    _2x2T_h(coeff+0, coeff+1, coeff+2, coeff+3, 0);
1310
    /* _2x2T_h(coeff+0, coeff+2, coeff+1, coeff+3, 0); */
1311
0
}
1312
1313
void _jxr_4x4IPCT(int*coeff)
1314
0
{
1315
    /* Permute */
1316
0
    _InvPermute(coeff);
1317
1318
#if defined(DETAILED_DEBUG) && 0
1319
    {
1320
        int idx;
1321
        DEBUG(" InvPermute:\n%*s", 4, "");
1322
        for (idx = 0 ; idx < 16 ; idx += 1) {
1323
            DEBUG(" 0x%08x", coeff[idx]);
1324
            if (idx%4 == 3 && idx != 15)
1325
                DEBUG("\n%*s", 4, "");
1326
        }
1327
        DEBUG("\n");
1328
    }
1329
#endif
1330
0
    _2x2T_h (coeff+0, coeff+ 1, coeff+4, coeff+ 5, 1);
1331
0
    _InvT_odd(coeff+2, coeff+ 3, coeff+6, coeff+ 7);
1332
0
    _InvT_odd(coeff+8, coeff+12, coeff+9, coeff+13);
1333
0
    _InvT_odd_odd(coeff+10, coeff+11, coeff+14, coeff+15);
1334
#if defined(DETAILED_DEBUG) && 0
1335
    {
1336
        int idx;
1337
        DEBUG(" stage 1:\n%*s", 4, "");
1338
        for (idx = 0 ; idx < 16 ; idx += 1) {
1339
            DEBUG(" 0x%08x", coeff[idx]);
1340
            if (idx%4 == 3 && idx != 15)
1341
                DEBUG("\n%*s", 4, "");
1342
        }
1343
        DEBUG("\n");
1344
    }
1345
#endif
1346
1347
0
    _2x2T_h(coeff+0, coeff+3, coeff+12, coeff+15, 0);
1348
0
    _2x2T_h(coeff+5, coeff+6, coeff+ 9, coeff+10, 0);
1349
0
    _2x2T_h(coeff+1, coeff+2, coeff+13, coeff+14, 0);
1350
0
    _2x2T_h(coeff+4, coeff+7, coeff+ 8, coeff+11, 0);
1351
0
}
1352
1353
void _jxr_4x4PCT(int*coeff)
1354
0
{
1355
0
    _2x2T_h(coeff+0, coeff+3, coeff+12, coeff+15, 0);
1356
0
    _2x2T_h(coeff+5, coeff+6, coeff+ 9, coeff+10, 0);
1357
0
    _2x2T_h(coeff+1, coeff+2, coeff+13, coeff+14, 0);
1358
0
    _2x2T_h(coeff+4, coeff+7, coeff+ 8, coeff+11, 0);
1359
1360
0
    _2x2T_h (coeff+0, coeff+ 1, coeff+4, coeff+ 5, 1);
1361
0
    _T_odd(coeff+2, coeff+ 3, coeff+6, coeff+ 7);
1362
0
    _T_odd(coeff+8, coeff+12, coeff+9, coeff+13);
1363
0
    _T_odd_odd(coeff+10, coeff+11, coeff+14, coeff+15);
1364
1365
0
    _FwdPermute(coeff);
1366
0
}
1367
1368
static void _InvRotate(int*a, int*b)
1369
0
{
1370
0
    *a -= (*b + 1) >> 1;
1371
0
    *b += (*a + 1) >> 1;
1372
0
    CHECK2(long_word_flag, *a, *b);
1373
0
}
1374
1375
static void _InvScale(int*a, int*b)
1376
0
{
1377
0
    *a += *b;
1378
0
    *b = (*a >> 1) - *b;
1379
0
    CHECK2(long_word_flag, *a, *b);
1380
0
    *a += (*b * 3 + 0) >> 3;
1381
0
    *b -= *a >> 10;
1382
0
    CHECK2(long_word_flag, *a, *b);
1383
0
    *b += *a >> 7;
1384
0
    *b += (*a * 3 + 0) >> 4;
1385
0
    CHECK1(long_word_flag, *b);
1386
0
}
1387
1388
void _jxr_4x4OverlapFilter(int*a, int*b, int*c, int*d,
1389
                           int*e, int*f, int*g, int*h,
1390
                           int*i, int*j, int*k, int*l,
1391
                           int*m, int*n, int*o, int*p)
1392
0
{
1393
0
    _2x2T_h(a, d, m, p, 0);
1394
0
    _2x2T_h(b, c, n, o, 0);
1395
0
    _2x2T_h(e, h, i, l, 0);
1396
0
    _2x2T_h(f, g, j, k, 0);
1397
1398
0
    _InvRotate(n, m);
1399
0
    _InvRotate(j, i);
1400
0
    _InvRotate(h, d);
1401
0
    _InvRotate(g, c);
1402
0
    _InvT_odd_odd_POST(k, l, o, p);
1403
1404
0
    _InvScale(a, p);
1405
0
    _InvScale(b, o);
1406
0
    _InvScale(e, l);
1407
0
    _InvScale(f, k);
1408
1409
0
    _2x2T_h_POST(a, d, m, p);
1410
0
    _2x2T_h_POST(b, c, n, o);
1411
0
    _2x2T_h_POST(e, h, i, l);
1412
0
    _2x2T_h_POST(f, g, j, k);
1413
0
}
1414
1415
void _jxr_4OverlapFilter(int*a, int*b, int*c, int*d)
1416
0
{
1417
0
    *a += *d;
1418
0
    *b += *c;
1419
0
    *d -= ((*a + 1) >> 1);
1420
0
    *c -= ((*b + 1) >> 1);
1421
0
    CHECK4(long_word_flag, *a, *b, *d, *c);
1422
0
    _InvScale(a, d);
1423
0
    _InvScale(b, c);
1424
0
    *a += ((*d * 3 + 4) >> 3);
1425
0
    *b += ((*c * 3 + 4) >> 3);
1426
0
    *d -= (*a >> 1);
1427
0
    *c -= (*b >> 1);
1428
0
    CHECK4(long_word_flag, *a, *b, *d, *c);
1429
0
    *a += *d;
1430
0
    *b += *c;
1431
0
    *d *= -1;
1432
0
    *c *= -1;
1433
0
    CHECK4(long_word_flag, *a, *b, *d, *c);
1434
0
    _InvRotate(c, d);
1435
0
    *d += ((*a + 1) >> 1);
1436
0
    *c += ((*b + 1) >> 1);
1437
0
    *a -= *d;
1438
0
    *b -= *c;
1439
0
    CHECK4(long_word_flag, *a, *b, *d, *c);
1440
0
}
1441
1442
void _jxr_2x2OverlapFilter(int*a, int*b, int*c, int*d)
1443
0
{
1444
0
    *a += *d;
1445
0
    *b += *c;
1446
0
    *d -= (*a + 1) >> 1;
1447
0
    *c -= (*b + 1) >> 1;
1448
0
    CHECK4(long_word_flag, *a, *b, *d, *c);
1449
0
    *b += (*a + 2) >> 2;
1450
0
    *a += (*b + 1) >> 1;
1451
1452
0
    *a += (*b >> 5);
1453
0
    *a += (*b >> 9);
1454
0
    *a += (*b >> 13);
1455
0
    CHECK2(long_word_flag, *a, *b);
1456
1457
0
    *b += (*a + 2) >> 2;
1458
1459
0
    *d += (*a + 1) >> 1;
1460
0
    *c += (*b + 1) >> 1;
1461
0
    *a -= *d;
1462
0
    CHECK4(long_word_flag, *a, *b, *d, *c);
1463
0
    *b -= *c;
1464
0
    CHECK1(long_word_flag, *b);
1465
0
}
1466
1467
void _jxr_2OverlapFilter(int*a, int*b)
1468
0
{
1469
0
    *b += ((*a + 2) >> 2);
1470
0
    *a += ((*b + 1) >> 1);
1471
0
    *a += (*b >> 5);
1472
0
    *a += (*b >> 9);
1473
0
    CHECK2(long_word_flag, *a, *b);
1474
0
    *a += (*b >> 13);
1475
0
    *b += ((*a + 2) >> 2);
1476
0
    CHECK2(long_word_flag, *a, *b);
1477
0
}
1478
1479
/* Prefiltering... */
1480
1481
static void fwdT_Odd_Odd_PRE(int*a, int*b, int*c, int*d)
1482
0
{
1483
0
    int t1;
1484
0
    int t2;
1485
0
    *d += *a;
1486
0
    *c -= *b;
1487
0
    t1 = *d >> 1;
1488
0
    t2 = *c >> 1;
1489
0
    *a -= t1;
1490
0
    *b += t2;
1491
0
    CHECK4(long_word_flag, *d, *c, *a, *b);
1492
0
    *a += (*b * 3 + 4) >> 3;
1493
0
    *b -= (*a * 3 + 2) >> 2;
1494
0
    CHECK2(long_word_flag, *a, *b);
1495
0
    *a += (*b * 3 + 6) >> 3;
1496
0
    *b -= t2;
1497
0
    CHECK2(long_word_flag, *a, *b);
1498
0
    *a += t1;
1499
0
    *c += *b;
1500
0
    *d -= *a;
1501
0
    CHECK3(long_word_flag, *a, *c, *d);
1502
0
}
1503
1504
static void fwdScale(int*a, int*b)
1505
0
{
1506
0
    *b -= (*a * 3 + 0) >> 4;
1507
0
    CHECK1(long_word_flag, *b);
1508
0
    *b -= *a >> 7;
1509
0
    CHECK1(long_word_flag, *b);
1510
0
    *b += *a >> 10;
1511
0
    *a -= (*b * 3 + 0) >> 3;
1512
0
    CHECK2(long_word_flag, *b, *a);
1513
0
    *b = (*a >> 1) - *b;
1514
0
    *a -= *b;
1515
0
    CHECK2(long_word_flag, *b, *a);
1516
0
}
1517
1518
static void fwdRotate(int*a, int*b)
1519
0
{
1520
0
    *b -= (*a + 1) >> 1;
1521
0
    *a += (*b + 1) >> 1;
1522
0
    CHECK2(long_word_flag, *b, *a);
1523
0
}
1524
1525
void _jxr_4x4PreFilter(int*a, int*b, int*c, int*d,
1526
                       int*e, int*f, int*g, int*h,
1527
                       int*i, int*j, int*k, int*l,
1528
                       int*m, int*n, int*o, int*p)
1529
0
{
1530
0
    _2x2T_h_Enc(a, d, m, p);
1531
0
    _2x2T_h_Enc(b, c, n, o);
1532
0
    _2x2T_h_Enc(e, h, i, l);
1533
0
    _2x2T_h_Enc(f, g, j, k);
1534
1535
0
    fwdScale(a, p);
1536
0
    fwdScale(b, o);
1537
0
    fwdScale(e, l);
1538
0
    fwdScale(f, k);
1539
1540
0
    fwdRotate(n, m);
1541
0
    fwdRotate(j, i);
1542
0
    fwdRotate(h, d);
1543
0
    fwdRotate(g, c);
1544
0
    fwdT_Odd_Odd_PRE(k, l, o, p);
1545
1546
0
    _2x2T_h(a, m, d, p, 0);
1547
0
    _2x2T_h(b, c, n, o, 0);
1548
0
    _2x2T_h(e, h, i, l, 0);
1549
0
    _2x2T_h(f, g, j, k, 0);
1550
0
}
1551
1552
void _jxr_4PreFilter(int*a, int*b, int*c, int*d)
1553
0
{
1554
0
    *a += *d;
1555
0
    *b += *c;
1556
0
    *d -= ((*a + 1) >> 1);
1557
0
    *c -= ((*b + 1) >> 1);
1558
0
    CHECK4(long_word_flag, *a, *b, *d, *c);
1559
0
    fwdRotate(c, d);
1560
0
    *d *= -1;
1561
0
    *c *= -1;
1562
0
    *a -= *d;
1563
0
    *b -= *c;
1564
0
    CHECK4(long_word_flag, *d, *c, *a, *b);
1565
0
    *d += (*a >> 1);
1566
0
    *c += (*b >> 1);
1567
0
    *a -= ((*d * 3 + 4) >> 3);
1568
0
    *b -= ((*c * 3 + 4) >> 3);
1569
0
    CHECK4(long_word_flag, *d, *c, *a, *b);
1570
0
    fwdScale(a, d);
1571
0
    fwdScale(b, c);
1572
0
    *d += ((*a + 1) >> 1);
1573
0
    *c += ((*b + 1) >> 1);
1574
0
    *a -= *d;
1575
0
    *b -= *c;
1576
0
    CHECK4(long_word_flag, *d, *c, *a, *b);
1577
0
}
1578
1579
void _jxr_2x2PreFilter(int*a, int*b, int*c, int*d)
1580
0
{
1581
0
    *a += *d;
1582
0
    *b += *c;
1583
0
    *d -= ((*a + 1) >> 1);
1584
0
    *c -= ((*b + 1) >> 1);
1585
0
    CHECK4(long_word_flag, *a, *b, *d, *c);
1586
0
    *b -= ((*a + 2) >> 2);
1587
0
    *a -= (*b >> 5);
1588
0
    CHECK2(long_word_flag, *b, *a);
1589
0
    *a -= (*b >> 9);
1590
0
    CHECK1(long_word_flag, *a);
1591
0
    *a -= (*b >> 13);
1592
0
    CHECK1(long_word_flag, *a);
1593
0
    *a -= ((*b + 1) >> 1);
1594
0
    *b -= ((*a + 2) >> 2);
1595
0
    *d += ((*a + 1) >> 1);
1596
0
    *c += ((*b + 1) >> 1);
1597
0
    CHECK4(long_word_flag, *a, *b, *d, *c);
1598
0
    *a -= *d;
1599
0
    *b -= *c;
1600
0
    CHECK2(long_word_flag, *a, *b);
1601
0
}
1602
1603
void _jxr_2PreFilter(int*a, int*b)
1604
0
{
1605
0
    *b -= ((*a + 2) >> 2);
1606
0
    *a -= (*b >> 13);
1607
0
    CHECK2(long_word_flag, *b, *a);
1608
0
    *a -= (*b >> 9);
1609
0
    CHECK1(long_word_flag, *a);
1610
0
    *a -= (*b >> 5);
1611
0
    CHECK1(long_word_flag, *a);
1612
0
    *a -= ((*b + 1) >> 1);
1613
0
    *b -= ((*a + 2) >> 2);
1614
0
    CHECK2(long_word_flag, *a, *b);
1615
0
}
1616
1617
uint8_t _jxr_read_lwf_test_flag()
1618
0
{
1619
0
    return long_word_flag;
1620
0
}
1621
1622
/*
1623
* $Log: algo.c,v $
1624
*
1625
* Revision 1.58 2009/05/29 12:00:00 microsoft
1626
* Reference Software v1.6 updates.
1627
*
1628
* Revision 1.57 2009/05/29 12:00:00 microsoft
1629
* Reference Software v1.6 updates.
1630
*
1631
* Revision 1.56 2009/04/13 12:00:00 microsoft
1632
* Reference Software v1.5 updates.
1633
*
1634
* Revision 1.55 2008-05-13 13:47:11 thor
1635
* Some experiments with a smarter selection for the quantization size,
1636
* does not yet compile.
1637
*
1638
* Revision 1.54 2008-05-09 19:57:48 thor
1639
* Reformatted for unix LF.
1640
*
1641
* Revision 1.53 2008-04-15 14:28:12 thor
1642
* Start of the repository for the jpegxr reference software.
1643
*
1644
* Revision 1.52 2008/03/20 18:11:50 steve
1645
* Clarify MBLPMode calculations.
1646
*
1647
* Revision 1.51 2008/03/14 00:54:08 steve
1648
* Add second prefilter for YUV422 and YUV420 encode.
1649
*
1650
* Revision 1.50 2008/03/13 22:32:26 steve
1651
* Fix CBP prediction for YUV422, mode==0.
1652
*
1653
* Revision 1.49 2008/03/13 17:49:31 steve
1654
* Fix problem with YUV422 CBP prediction for UV planes
1655
*
1656
* Add support for YUV420 encoding.
1657
*
1658
* Revision 1.48 2008/03/13 00:07:22 steve
1659
* Encode HP of YUV422
1660
*
1661
* Revision 1.47 2008/03/12 21:10:27 steve
1662
* Encode LP of YUV422
1663
*
1664
* Revision 1.46 2008/03/11 22:12:48 steve
1665
* Encode YUV422 through DC.
1666
*
1667
* Revision 1.45 2008/02/26 23:52:44 steve
1668
* Remove ident for MS compilers.
1669
*
1670
* Revision 1.44 2008/02/22 23:01:33 steve
1671
* Compress macroblock HP CBP packets.
1672
*
1673
* Revision 1.43 2008/02/01 22:49:52 steve
1674
* Handle compress of YUV444 color DCONLY
1675
*
1676
* Revision 1.42 2008/01/08 23:23:32 steve
1677
* Minor math error in overlap code.
1678
*
1679
* Revision 1.41 2008/01/08 01:06:20 steve
1680
* Add first pass overlap filtering.
1681
*
1682
* Revision 1.40 2008/01/04 17:07:35 steve
1683
* API interface for setting QP values.
1684
*
1685
* Revision 1.39 2008/01/01 01:08:23 steve
1686
* Compile warning.
1687
*
1688
* Revision 1.38 2008/01/01 01:07:25 steve
1689
* Add missing HP prediction.
1690
*
1691
* Revision 1.37 2007/12/30 00:16:00 steve
1692
* Add encoding of HP values.
1693
*
1694
* Revision 1.36 2007/12/17 23:02:57 steve
1695
* Implement MB_CBP encoding.
1696
*
1697
* Revision 1.35 2007/12/14 17:10:39 steve
1698
* HP CBP Prediction
1699
*
1700
* Revision 1.34 2007/12/12 00:36:46 steve
1701
* Use T_odd instead of InvT_odd for PCT transform.
1702
*
1703
* Revision 1.33 2007/12/06 23:12:40 steve
1704
* Stubs for LP encode operations.
1705
*
1706
* Revision 1.32 2007/11/26 01:47:15 steve
1707
* Add copyright notices per MS request.
1708
*
1709
* Revision 1.31 2007/11/21 23:24:09 steve
1710
* Account for tiles selecting LP_QUANT for pred math.
1711
*
1712
* Revision 1.30 2007/11/16 20:03:57 steve
1713
* Store MB Quant, not qp_index.
1714
*
1715
* Revision 1.29 2007/11/13 03:27:23 steve
1716
* Add Frequency mode LP support.
1717
*
1718
* Revision 1.28 2007/10/30 21:32:46 steve
1719
* Support for multiple tile columns.
1720
*
1721
* Revision 1.27 2007/10/23 00:34:12 steve
1722
* Level1 filtering for YUV422 and YUV420
1723
*
1724
* Revision 1.26 2007/10/22 21:52:37 steve
1725
* Level2 filtering for YUV420
1726
*
1727
* Revision 1.25 2007/10/19 22:07:11 steve
1728
* Prediction of YUV420 chroma planes.
1729
*
1730
* Revision 1.24 2007/10/19 16:20:21 steve
1731
* Parse YUV420 HP
1732
*
1733
* Revision 1.23 2007/10/17 23:43:19 steve
1734
* Add support for YUV420
1735
*
1736
* Revision 1.22 2007/10/04 00:30:47 steve
1737
* Fix prediction of HP CBP for YUV422 data.
1738
*
1739
* Revision 1.21 2007/10/02 20:36:29 steve
1740
* Fix YUV42X DC prediction, add YUV42X HP parsing.
1741
*
1742
* Revision 1.20 2007/10/01 20:39:33 steve
1743
* Add support for YUV422 LP bands.
1744
*
1745
* Revision 1.19 2007/09/20 18:04:10 steve
1746
* support render of YUV422 images.
1747
*
1748
* Revision 1.18 2007/09/13 23:12:34 steve
1749
* Support color HP bands.
1750
*
1751
* Revision 1.17 2007/09/11 00:40:06 steve
1752
* Fix rendering of chroma to add the missing *2.
1753
* Fix handling of the chroma LP samples
1754
* Parse some of the HP CBP data in chroma.
1755
*
1756
* Revision 1.16 2007/09/08 01:01:43 steve
1757
* YUV444 color parses properly.
1758
*
1759
* Revision 1.15 2007/09/04 19:10:46 steve
1760
* Finish level1 overlap filtering.
1761
*
1762
* Revision 1.14 2007/08/31 23:31:49 steve
1763
* Initialize CBP VLC tables at the right time.
1764
*
1765
* Revision 1.13 2007/08/28 21:58:52 steve
1766
* Rewrite filtering to match rewritten 4.7
1767
*
1768
* Revision 1.12 2007/08/14 23:39:56 steve
1769
* Fix UpdateModelMB / Add filtering functions.
1770
*
1771
* Revision 1.11 2007/08/04 00:10:51 steve
1772
* Fix subtle loss of -1 values during DC prediction.
1773
*/
1774