Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/jxr/image/decode/strPredQuantDec.c
4393 views
1
//*@@@+++@@@@******************************************************************
2
//
3
// Copyright © Microsoft Corp.
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are met:
8
//
9
// • Redistributions of source code must retain the above copyright notice,
10
// this list of conditions and the following disclaimer.
11
// • Redistributions in binary form must reproduce the above copyright notice,
12
// this list of conditions and the following disclaimer in the documentation
13
// and/or other materials provided with the distribution.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
// POSSIBILITY OF SUCH DAMAGE.
26
//
27
//*@@@---@@@@******************************************************************
28
29
#include "strcodec.h"
30
31
#define DEQUANT(iRaw, iQP) ((iRaw) * (iQP))
32
33
Void dequantizeBlock4x4(PixelI * pRec, Int * pOrg, const Int * pIndex, Int iQPLP)
34
{
35
Int i;
36
37
for(i = 1; i < 16; i ++)
38
pRec[pIndex[i]] = DEQUANT(pOrg[i], iQPLP);
39
}
40
41
Void dequantizeBlock2x2(PixelI * pRec, Int * pOrg, Int iQPLP)
42
{
43
pRec[32] = DEQUANT(pOrg[1], iQPLP);
44
pRec[16] = DEQUANT(pOrg[2], iQPLP);
45
pRec[48] = DEQUANT(pOrg[3], iQPLP);
46
}
47
48
Void dequantizeBlock4x2(PixelI * pRec, Int * pOrg, Int iQPLP)
49
{
50
pRec[ 64] = DEQUANT(pOrg[1], iQPLP);
51
pRec[ 16] = DEQUANT(pOrg[2], iQPLP);
52
pRec[ 80] = DEQUANT(pOrg[3], iQPLP);
53
pRec[ 32] = DEQUANT(pOrg[4], iQPLP);
54
pRec[ 96] = DEQUANT(pOrg[5], iQPLP);
55
pRec[ 48] = DEQUANT(pOrg[6], iQPLP);
56
pRec[112] = DEQUANT(pOrg[7], iQPLP);
57
}
58
59
60
Int dequantizeMacroblock(CWMImageStrCodec * pSC)
61
{
62
const COLORFORMAT cf = pSC->m_param.cfColorFormat;
63
CWMIMBInfo *pMBInfo = &pSC->MBInfo;
64
CWMITile * pTile = pSC->pTile + pSC->cTileColumn;
65
const size_t iChannels = pSC->m_param.cNumChannels;
66
size_t i;
67
68
for(i = 0; i < iChannels; i ++){
69
//dequantize DC
70
pSC->p1MBbuffer[i][0] = DEQUANT(pMBInfo->iBlockDC[i][0], pTile->pQuantizerDC[i]->iQP);
71
72
// dequantize LP
73
if(pSC->WMISCP.sbSubband != SB_DC_ONLY) {
74
if(i == 0 || (cf != YUV_422 && cf != YUV_420))
75
dequantizeBlock4x4(pSC->p1MBbuffer[i] , pMBInfo->iBlockDC[i], dctIndex[2], pTile->pQuantizerLP[i][pMBInfo->iQIndexLP].iQP);
76
else if(cf == YUV_422)
77
dequantizeBlock4x2(pSC->p1MBbuffer[i], pMBInfo->iBlockDC[i], pTile->pQuantizerLP[i][pMBInfo->iQIndexLP].iQP);
78
else // 420
79
dequantizeBlock2x2(pSC->p1MBbuffer[i], pMBInfo->iBlockDC[i], pTile->pQuantizerLP[i][pMBInfo->iQIndexLP].iQP);
80
}
81
}
82
83
return ICERR_OK;
84
}
85
86
/* frequency domain inverse DCAC prediction */
87
Void predDCACDec(CWMImageStrCodec * pSC)
88
{
89
const COLORFORMAT cf = pSC->m_param.cfColorFormat;
90
const Int iChannels = (cf == YUV_420 || cf == YUV_422) ? 1 : (Int) pSC->m_param.cNumChannels;
91
CWMIMBInfo *pMBInfo = &(pSC->MBInfo);
92
size_t mbX = pSC->cColumn;// mbY = pSC->cRow;
93
Int iDCACPredMode = getDCACPredMode(pSC, mbX);
94
Int iDCPredMode = (iDCACPredMode & 0x3);
95
Int iADPredMode = (iDCACPredMode & 0xC);
96
PixelI * pOrg, * pRef;
97
Int ii;
98
99
for(ii = 0; ii < iChannels; ii ++){
100
pOrg = pMBInfo->iBlockDC[ii];//[dcBlkIdx + (i >> 4)]; // current DC block
101
102
/* DC prediction */
103
if(iDCPredMode == 1){ // predict DC from top
104
pOrg[0] += pSC->PredInfoPrevRow[ii][mbX].iDC;
105
}
106
else if(iDCPredMode == 0){ // predict DC from left
107
pOrg[0] += (pSC->PredInfo[ii] + mbX - 1)->iDC;
108
}
109
else if(iDCPredMode == 2){// predict DC from top&left
110
pOrg[0] += ((pSC->PredInfo[ii] + mbX - 1)->iDC + (pSC->PredInfoPrevRow[ii] + mbX)->iDC) >> 1;
111
}
112
113
/* AD prediction */
114
if(iADPredMode == 4){// predict AD from top
115
pRef = (pSC->PredInfoPrevRow[ii] + mbX)->piAD;
116
pOrg[4] += pRef[3], pOrg[8] += pRef[4], pOrg[12] += pRef[5];
117
}
118
else if(iADPredMode == 0){// predict AD from left
119
pRef = (pSC->PredInfo[ii] + mbX - 1)->piAD;
120
pOrg[1] += pRef[0], pOrg[2] += pRef[1], pOrg[3] += pRef[2];
121
}
122
}
123
124
if(cf == YUV_420){
125
for(ii = 1; ii < 3; ii ++){
126
pOrg = pMBInfo->iBlockDC[ii];//dcBlkIdx + ii]; // current DC block
127
128
/* DC prediction */
129
if(iDCPredMode == 1){ // predict DC from top
130
pOrg[0] += (pSC->PredInfoPrevRow[ii] + mbX)->iDC;
131
}
132
else if(iDCPredMode == 0){ // predict DC from left
133
pOrg[0] += (pSC->PredInfo[ii] + mbX - 1)->iDC;
134
}
135
else if(iDCPredMode == 2){ // predict DC from top&left
136
pOrg[0] += (((pSC->PredInfo[ii] + mbX - 1)->iDC + (pSC->PredInfoPrevRow[ii] + mbX)->iDC + 1) >> 1);
137
}
138
139
/* AD prediciton */
140
if(iADPredMode == 4){// predict AD from top
141
pOrg[2] += (pSC->PredInfoPrevRow[ii] + mbX)->piAD[1];
142
}
143
else if(iADPredMode == 0){// predict AD from left
144
pOrg[1] += (pSC->PredInfo[ii] + mbX - 1)->piAD[0];
145
}
146
}
147
}
148
else if(cf == YUV_422){
149
for(ii = 1; ii < 3; ii ++){
150
pOrg = pMBInfo->iBlockDC[ii];//[dcBlkIdx + ii]; // current DC block
151
152
/* DC prediciton */
153
if(iDCPredMode == 1){ // predict DC from top
154
pOrg[0] += (pSC->PredInfoPrevRow[ii] + mbX)->iDC;
155
}
156
else if(iDCPredMode == 0){ // predict DC from left
157
pOrg[0] += (pSC->PredInfo[ii] + mbX - 1)->iDC;
158
}
159
else if(iDCPredMode == 2){ // predict DC from top&left
160
pOrg[0] += (((pSC->PredInfo[ii] + mbX - 1)->iDC + (pSC->PredInfoPrevRow[ii] + mbX)->iDC + 1) >> 1);
161
}
162
163
/* AD prediction */
164
if(iADPredMode == 4){// predict AD from top
165
pOrg[4] += (pSC->PredInfoPrevRow[ii] + mbX)->piAD[4]; // AC of HT !!!
166
pOrg[2] += (pSC->PredInfoPrevRow[ii] + mbX)->piAD[3];
167
pOrg[6] += pOrg[2];
168
}
169
else if(iADPredMode == 0){// predict AD from left
170
pOrg[4] += (pSC->PredInfo[ii] + mbX - 1)->piAD[4]; // AC of HT !!!
171
pOrg[1] += (pSC->PredInfo[ii] + mbX - 1)->piAD[0];
172
pOrg[5] += (pSC->PredInfo[ii] + mbX - 1)->piAD[2];
173
}
174
else if(iDCPredMode == 1){
175
pOrg[6] += pOrg[2];
176
}
177
}
178
}
179
180
pMBInfo->iOrientation = 2 - getACPredMode(pMBInfo, cf);
181
}
182
183
/*************************************************************************
184
Frequency domain inverse AC prediction
185
*************************************************************************/
186
Void predACDec(CWMImageStrCodec * pSC)
187
{
188
const COLORFORMAT cf = pSC->m_param.cfColorFormat;
189
const Int iChannels = (cf == YUV_420 || cf == YUV_422) ? 1 : (Int) pSC->m_param.cNumChannels;
190
// size_t mbX = pSC->cColumn, mbY = pSC->cRow;
191
CWMIMBInfo *pMBInfo = &pSC->MBInfo;
192
Int iACPredMode = 2 - pMBInfo->iOrientation;
193
PixelI * pOrg, * pRef;
194
Int i, j;
195
196
/* AC prediction */
197
for(i = 0; i < iChannels; i++){
198
// prediction only happens inside MB
199
PixelI* pSrc = pSC->p1MBbuffer[i];//0 == i ? pSC->pY1 : (1 == i ? pSC->pU1 : pSC->pV1);
200
201
switch (iACPredMode)
202
{
203
case 1:
204
{
205
// predict from top
206
static U8 blkIdx[] = {1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15};
207
208
for (j = 0; j < sizeof(blkIdx) / sizeof(*blkIdx); ++j)
209
{
210
pOrg = pSrc + 16 * blkIdx[j];
211
pRef = pOrg - 16;
212
213
pOrg[ 2] += pRef[ 2];
214
pOrg[10] += pRef[10];
215
pOrg[ 9] += pRef[ 9];
216
}
217
break;
218
}
219
220
case 0:
221
// predict from left
222
for (j = 64; j < 256; j += 16)
223
{
224
pOrg = pSrc + j;
225
pRef = pOrg - 64;
226
227
pOrg[1] += pRef[1];
228
pOrg[5] += pRef[5];
229
pOrg[6] += pRef[6];
230
}
231
break;
232
233
default:
234
// no prediction
235
break;
236
}
237
}
238
239
if(cf == YUV_420){
240
for(i = 16; i <= 20; i += 4){
241
PixelI* pSrc = pSC->p1MBbuffer[(i >> 2) - 3];//16 == i ? pSC->pU1 : pSC->pV1;
242
243
switch (iACPredMode)
244
{
245
case 1:
246
{
247
// predict from top
248
for (j = 1; j <= 3; j += 2)
249
{
250
pOrg = pSrc + 16 * j;
251
pRef = pOrg - 16;
252
253
pOrg[ 2] += pRef[ 2];
254
pOrg[10] += pRef[10];
255
pOrg[ 9] += pRef[ 9];
256
}
257
break;
258
}
259
260
case 0:
261
// predict from left
262
for (j = 2; j <= 3; ++j)
263
{
264
pOrg = pSrc + 16 * j;
265
pRef = pOrg - 32;
266
267
pOrg[1] += pRef[1];
268
pOrg[5] += pRef[5];
269
pOrg[6] += pRef[6];
270
}
271
break;
272
273
default:
274
// no prediction
275
break;
276
}
277
}
278
}
279
else if(cf == YUV_422){
280
for(i = 16; i < 32; i += 8){
281
PixelI* pSrc = pSC->p1MBbuffer[(i >> 3) - 1];//16 == i ? pSC->pU1 : pSC->pV1;
282
283
switch (iACPredMode)
284
{
285
case 1:
286
{
287
// predict from top
288
for (j = 2; j < 8; j ++)
289
{
290
pOrg = pSrc + blkOffsetUV_422[j];
291
pRef = pOrg - 16;
292
293
pOrg[10] += pRef[10];
294
pOrg[ 2] += pRef[ 2];
295
pOrg[ 9] += pRef[ 9];
296
}
297
break;
298
}
299
300
case 0:
301
// predict from left
302
for (j = 1; j < 8; j += 2)
303
{
304
pOrg = pSrc + blkOffsetUV_422[j];
305
pRef = pOrg - 64;
306
307
pOrg[1] += pRef[1];
308
pOrg[5] += pRef[5];
309
pOrg[6] += pRef[6];
310
}
311
break;
312
313
default:
314
// no prediction
315
break;
316
}
317
}
318
}
319
}
320
321
/*************************************************************************
322
CBP
323
*************************************************************************/
324
static int NumOnes(int i)
325
{
326
int retval = 0;
327
static const int g_Count[] = { 0,1,1,2, 1,2,2,3, 1,2,2,3, 2,3,3,4 };
328
i = i & 0xffff;
329
while (i) {
330
retval += g_Count[i & 0xf];
331
i >>= 4;
332
}
333
return retval;
334
}
335
336
#define SATURATE32(x) if((unsigned int)(x + 16) >= 32) { if (x < 0) x = -16; else x = 15; }
337
338
/* CBP prediction for 16 x 16 MB */
339
/* block index */
340
/* 0 1 4 5 */
341
/* 2 3 6 7 */
342
/* 8 9 12 13 */
343
/* 10 11 14 15 */
344
static Int predCBPCDec(CWMImageStrCodec * pSC, Int iCBP, size_t mbX, size_t mbY, size_t c, CCBPModel *pModel)
345
{
346
Int iNOrig;
347
const int iNDiff = AVG_NDIFF;
348
size_t c1 = c ? 1 : 0;
349
350
UNREFERENCED_PARAMETER( mbY );
351
352
if (pModel->m_iState[c1] == 0) {
353
if(pSC->m_bCtxLeft) {
354
if (pSC->m_bCtxTop) {
355
iCBP ^= 1;
356
}
357
else {
358
Int iTopCBP = (pSC->PredInfoPrevRow[c] + mbX)->iCBP;
359
iCBP ^= (iTopCBP >> 10) & 1; // left: top(10) => 0
360
}
361
}
362
else {
363
Int iLeftCBP = (pSC->PredInfo[c] + mbX - 1)->iCBP;
364
iCBP ^= ((iLeftCBP >> 5) & 1); // left(5) => 0
365
}
366
367
iCBP ^= (0x02 & (iCBP << 1)); // 0 => 1
368
iCBP ^= (0x10 & (iCBP << 3)); // 1 => 4
369
iCBP ^= (0x20 & (iCBP << 1)); // 4 => 5
370
371
iCBP ^= ((iCBP & 0x33) << 2);
372
iCBP ^= ((iCBP & 0xcc) << 6);
373
iCBP ^= ((iCBP & 0x3300) << 2);
374
375
}
376
else if (pModel->m_iState[c1] == 2) {
377
iCBP ^= 0xffff;
378
}
379
380
iNOrig = NumOnes(iCBP);
381
382
pModel->m_iCount0[c1] += iNOrig - iNDiff;
383
SATURATE32(pModel->m_iCount0[c1]);
384
385
pModel->m_iCount1[c1] += 16 - iNOrig - iNDiff;
386
SATURATE32(pModel->m_iCount1[c1]);
387
388
if (pModel->m_iCount0[c1] < 0) {
389
if (pModel->m_iCount0[c1] < pModel->m_iCount1[c1]) {
390
pModel->m_iState[c1] = 1;
391
}
392
else {
393
pModel->m_iState[c1] = 2;
394
}
395
}
396
else if (pModel->m_iCount1[c1] < 0) {
397
pModel->m_iState[c1] = 2;
398
}
399
else {
400
pModel->m_iState[c1] = 0;
401
}
402
return iCBP;
403
}
404
405
static Int predCBPC420Dec(CWMImageStrCodec * pSC, Int iCBP, size_t mbX, size_t mbY, size_t c, CCBPModel *pModel)
406
{
407
Int iNOrig;
408
const int iNDiff = AVG_NDIFF;
409
410
UNREFERENCED_PARAMETER( mbY );
411
412
if (pModel->m_iState[1] == 0) {
413
if(pSC->m_bCtxLeft) {
414
if (pSC->m_bCtxTop) {
415
iCBP ^= 1;
416
}
417
else {
418
Int iTopCBP = (pSC->PredInfoPrevRow[c] + mbX)->iCBP;
419
iCBP ^= (iTopCBP >> 2) & 1; // left: top(2) => 0
420
}
421
}
422
else {
423
Int iLeftCBP = (pSC->PredInfo[c] + mbX - 1)->iCBP;
424
iCBP ^= ((iLeftCBP >> 1) & 1); // left(1) => 0
425
}
426
427
iCBP ^= (0x02 & (iCBP << 1)); // 0 => 1
428
iCBP ^= ((iCBP & 0x3) << 2); // [0 1] -> [2 3]
429
}
430
else if (pModel->m_iState[1] == 2) {
431
iCBP ^= 0xf;
432
}
433
434
iNOrig = NumOnes(iCBP) * 4;
435
436
pModel->m_iCount0[1] += iNOrig - iNDiff;
437
SATURATE32(pModel->m_iCount0[1]);
438
439
pModel->m_iCount1[1] += 16 - iNOrig - iNDiff;
440
SATURATE32(pModel->m_iCount1[1]);
441
442
if (pModel->m_iCount0[1] < 0) {
443
if (pModel->m_iCount0[1] < pModel->m_iCount1[1]) {
444
pModel->m_iState[1] = 1;
445
}
446
else {
447
pModel->m_iState[1] = 2;
448
}
449
}
450
else if (pModel->m_iCount1[1] < 0) {
451
pModel->m_iState[1] = 2;
452
}
453
else {
454
pModel->m_iState[1] = 0;
455
}
456
457
return iCBP;
458
}
459
460
static Int predCBPC422Dec(CWMImageStrCodec * pSC, Int iCBP, size_t mbX, size_t mbY, size_t c, CCBPModel *pModel)
461
{
462
Int iNOrig;
463
const int iNDiff = AVG_NDIFF;
464
465
UNREFERENCED_PARAMETER( mbY );
466
467
if (pModel->m_iState[1] == 0) {
468
if(pSC->m_bCtxLeft) {
469
if (pSC->m_bCtxTop) {
470
iCBP ^= 1;
471
}
472
else {
473
Int iTopCBP = (pSC->PredInfoPrevRow[c] + mbX)->iCBP;
474
iCBP ^= (iTopCBP >> 6) & 1; // left: top(6) => 0
475
}
476
}
477
else {
478
Int iLeftCBP = (pSC->PredInfo[c] + mbX - 1)->iCBP;
479
iCBP ^= ((iLeftCBP >> 1) & 1); // left(1) => 0
480
}
481
482
iCBP ^= (iCBP & 0x1) << 1; // [0]->[1]
483
iCBP ^= (iCBP & 0x3) << 2; // [0 1]->[2 3]
484
iCBP ^= (iCBP & 0xc) << 2; // [2 3]->[4 5]
485
iCBP ^= (iCBP & 0x30) << 2; // [4 5]->[6 7]
486
}
487
else if (pModel->m_iState[1] == 2) {
488
iCBP ^= 0xff;
489
}
490
491
iNOrig = NumOnes(iCBP) * 2;
492
493
pModel->m_iCount0[1] += iNOrig - iNDiff;
494
SATURATE32(pModel->m_iCount0[1]);
495
496
pModel->m_iCount1[1] += 16 - iNOrig - iNDiff;
497
SATURATE32(pModel->m_iCount1[1]);
498
499
if (pModel->m_iCount0[1] < 0) {
500
if (pModel->m_iCount0[1] < pModel->m_iCount1[1]) {
501
pModel->m_iState[1] = 1;
502
}
503
else {
504
pModel->m_iState[1] = 2;
505
}
506
}
507
else if (pModel->m_iCount1[1] < 0) {
508
pModel->m_iState[1] = 2;
509
}
510
else {
511
pModel->m_iState[1] = 0;
512
}
513
514
return iCBP;
515
}
516
517
518
/* Coded Block Pattern (CBP) prediction */
519
Void predCBPDec(CWMImageStrCodec *pSC, CCodingContext *pContext)
520
{
521
const COLORFORMAT cf = pSC->m_param.cfColorFormat;
522
const size_t iChannels = (cf == YUV_420 || cf == YUV_422) ? 1 : pSC->m_param.cNumChannels;
523
size_t i, mbX = pSC->cColumn, mbY = pSC->cRow;
524
CWMIMBInfo *pMBInfo = &(pSC->MBInfo);
525
526
for (i = 0; i < iChannels; i++) {
527
(pSC->PredInfo[i] + mbX)->iCBP = pMBInfo->iCBP[i] = predCBPCDec(pSC, pMBInfo->iDiffCBP[i], mbX, mbY, i, &pContext->m_aCBPModel); // Y Channel
528
}
529
530
if (cf == YUV_422){
531
(pSC->PredInfo[1] + mbX)->iCBP = pMBInfo->iCBP[1] = predCBPC422Dec(pSC, pMBInfo->iDiffCBP[1], mbX, mbY, 1, &pContext->m_aCBPModel);
532
(pSC->PredInfo[2] + mbX)->iCBP = pMBInfo->iCBP[2] = predCBPC422Dec(pSC, pMBInfo->iDiffCBP[2], mbX, mbY, 2, &pContext->m_aCBPModel);
533
}
534
else if (cf == YUV_420) {
535
(pSC->PredInfo[1] + mbX)->iCBP = pMBInfo->iCBP[1] = predCBPC420Dec(pSC, pMBInfo->iDiffCBP[1], mbX, mbY, 1, &pContext->m_aCBPModel);
536
(pSC->PredInfo[2] + mbX)->iCBP = pMBInfo->iCBP[2] = predCBPC420Dec(pSC, pMBInfo->iDiffCBP[2], mbX, mbY, 2, &pContext->m_aCBPModel);
537
}
538
//}
539
}
540
541