1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
|
/*! @file inverse.c
*
* @brief Implementation of the inverse wavelet transforms.
*
* (C) Copyright 2018 GoPro Inc (http://gopro.com/).
*
* Licensed under either:
* - Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
* - MIT license, http://opensource.org/licenses/MIT
* at your option.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "headers.h"
//! Rounding adjustment used by the inverse wavelet transforms
static const int32_t rounding = 4;
/*!
@brief Apply the inverse horizontal wavelet transform
This routine applies the inverse wavelet transform to a row of
lowpass and highpass coefficients, producing an output row that
is write as wide.
*/
STATIC CODEC_ERROR InvertHorizontal16s(PIXEL *lowpass, //!< Horizontal lowpass coefficients
PIXEL *highpass, //!< Horizontal highpass coefficients
PIXEL *output, //!< Row of reconstructed results
DIMENSION input_width, //!< Number of values in the input row
DIMENSION output_width //!< Number of values in the output row
)
{
const int last_column = input_width - 1;
int32_t even;
int32_t odd;
// Start processing at the beginning of the row
int column = 0;
// Process the first two output points with special filters for the left border
even = 0;
odd = 0;
// Apply the even reconstruction filter to the lowpass band
even += 11 * lowpass[column + 0];
even -= 4 * lowpass[column + 1];
even += 1 * lowpass[column + 2];
even += rounding;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highpass[column];
even >>= 1;
// The lowpass result should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Apply the odd reconstruction filter to the lowpass band
odd += 5 * lowpass[column + 0];
odd += 4 * lowpass[column + 1];
odd -= 1 * lowpass[column + 2];
odd += rounding;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highpass[column];
odd >>= 1;
// The lowpass result should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Store the last two output points produced by the loop
output[2 * column + 0] = clamp_uint14(even);
output[2 * column + 1] = clamp_uint14(odd);
// Advance to the next input column (second pair of output values)
column++;
// Process the rest of the columns up to the last column in the row
for (; column < last_column; column++)
{
int32_t even = 0; // Result of convolution with even filter
int32_t odd = 0; // Result of convolution with odd filter
// Apply the even reconstruction filter to the lowpass band
even += lowpass[column - 1];
even -= lowpass[column + 1];
even += 4;
even >>= 3;
even += lowpass[column + 0];
// Add the highpass correction
even += highpass[column];
even >>= 1;
// The lowpass result should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Place the even result in the even column
//output[2 * column + 0] = clamp_uint12(even);
output[2 * column + 0] = clamp_uint14(even);
// Apply the odd reconstruction filter to the lowpass band
odd -= lowpass[column - 1];
odd += lowpass[column + 1];
odd += 4;
odd >>= 3;
odd += lowpass[column + 0];
// Subtract the highpass correction
odd -= highpass[column];
odd >>= 1;
// The lowpass result should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Place the odd result in the odd column
//output[2 * column + 1] = clamp_uint14(odd);
output[2 * column + 1] = clamp_uint14(odd);
}
// Should have exited the loop at the column for right border processing
assert(column == last_column);
// Process the last two output points with special filters for the right border
even = 0;
odd = 0;
// Apply the even reconstruction filter to the lowpass band
even += 5 * lowpass[column + 0];
even += 4 * lowpass[column - 1];
even -= 1 * lowpass[column - 2];
even += rounding;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highpass[column];
even >>= 1;
// The lowpass result should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Place the even result in the even column
output[2 * column + 0] = clamp_uint14(even);
if (2 * column + 1 < output_width)
{
// Apply the odd reconstruction filter to the lowpass band
odd += 11 * lowpass[column + 0];
odd -= 4 * lowpass[column - 1];
odd += 1 * lowpass[column - 2];
odd += rounding;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highpass[column];
odd >>= 1;
// The lowpass result should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Place the odd result in the odd column
output[2 * column + 1] = clamp_uint14(odd);
}
return CODEC_ERROR_OKAY;
}
/*!
@brief Apply the inverse horizontal wavelet transform
This routine is similar to @ref InvertHorizontal16s, but a scale factor
that was applied during encoding is removed from the output values.
*/
STATIC CODEC_ERROR InvertHorizontalDescale16s(PIXEL *lowpass, PIXEL *highpass, PIXEL *output,
DIMENSION input_width, DIMENSION output_width,
int descale)
{
const int last_column = input_width - 1;
// Start processing at the beginning of the row
int column = 0;
int descale_shift = 0;
int32_t even;
int32_t odd;
/*
The implementation of the inverse filter includes descaling by a factor of two
because the last division by two in the computation of the even and odd results
that is performed using a right arithmetic shift has been omitted from the code.
*/
if (descale == 2) {
descale_shift = 1;
}
// Check that the descaling value is reasonable
assert(descale_shift >= 0);
// Process the first two output points with special filters for the left border
even = 0;
odd = 0;
// Apply the even reconstruction filter to the lowpass band
even += 11 * lowpass[column + 0];
even -= 4 * lowpass[column + 1];
even += 1 * lowpass[column + 2];
even += rounding;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highpass[column];
// Remove any scaling used during encoding
even <<= descale_shift;
// The lowpass result should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Apply the odd reconstruction filter to the lowpass band
odd += 5 * lowpass[column + 0];
odd += 4 * lowpass[column + 1];
odd -= 1 * lowpass[column + 2];
odd += rounding;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highpass[column];
// Remove any scaling used during encoding
odd <<= descale_shift;
// The lowpass result should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
output[2 * column + 0] = ClampPixel(even);
output[2 * column + 1] = ClampPixel(odd);
// Advance to the next input column (second pair of output values)
column++;
// Process the rest of the columns up to the last column in the row
for (; column < last_column; column++)
{
int32_t even = 0; // Result of convolution with even filter
int32_t odd = 0; // Result of convolution with odd filter
// Apply the even reconstruction filter to the lowpass band
even += lowpass[column - 1];
even -= lowpass[column + 1];
even += 4;
even >>= 3;
even += lowpass[column + 0];
// Add the highpass correction
even += highpass[column];
// Remove any scaling used during encoding
even <<= descale_shift;
// The lowpass result should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Place the even result in the even column
output[2 * column + 0] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd -= lowpass[column - 1];
odd += lowpass[column + 1];
odd += 4;
odd >>= 3;
odd += lowpass[column + 0];
// Subtract the highpass correction
odd -= highpass[column];
// Remove any scaling used during encoding
odd <<= descale_shift;
// The lowpass result should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Place the odd result in the odd column
output[2 * column + 1] = ClampPixel(odd);
}
// Should have exited the loop at the column for right border processing
assert(column == last_column);
// Process the last two output points with special filters for the right border
even = 0;
odd = 0;
// Apply the even reconstruction filter to the lowpass band
even += 5 * lowpass[column + 0];
even += 4 * lowpass[column - 1];
even -= 1 * lowpass[column - 2];
even += rounding;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highpass[column];
// Remove any scaling used during encoding
even <<= descale_shift;
// The lowpass result should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Place the even result in the even column
output[2 * column + 0] = ClampPixel(even);
if (2 * column + 1 < output_width)
{
// Apply the odd reconstruction filter to the lowpass band
odd += 11 * lowpass[column + 0];
odd -= 4 * lowpass[column - 1];
odd += 1 * lowpass[column - 2];
odd += rounding;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highpass[column];
// Remove any scaling used during encoding
odd <<= descale_shift;
// The lowpass result should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Place the odd result in the odd column
output[2 * column + 1] = ClampPixel(odd);
}
return CODEC_ERROR_OKAY;
}
/*!
@brief Apply the inverse spatial wavelet filter
Dequantize the coefficients in the highpass bands and apply the
inverse spatial wavelet filter to compute a lowpass band that
has twice the width and height of the input bands.
The inverse vertical filter is applied to the upper and lower bands
on the left and the upper and lower bands on the right. The inverse
horizontal filter is applied to the left and right (lowpass and highpass)
results from the vertical inverse. Each application of the inverse
vertical filter produces two output rows and each application of the
inverse horizontal filter produces an output row that is twice as wide.
The inverse wavelet filter is a three tap filter.
For the even output values, add and subtract the off-center values,
add the rounding correction, and divide by eight, then add the center
value, add the highpass coefficient, and divide by two.
For the odd output values, the add and subtract operations for the
off-center values are reversed the the highpass coefficient is subtracted.
Divisions are implemented by right arithmetic shifts.
Special formulas for the inverse vertical filter are applied to the top
and bottom rows.
*/
CODEC_ERROR InvertSpatialQuant16s(gpr_allocator *allocator,
PIXEL *lowlow_band, int lowlow_pitch,
PIXEL *lowhigh_band, int lowhigh_pitch,
PIXEL *highlow_band, int highlow_pitch,
PIXEL *highhigh_band, int highhigh_pitch,
PIXEL *output_image, int output_pitch,
DIMENSION input_width, DIMENSION input_height,
DIMENSION output_width, DIMENSION output_height,
QUANT quantization[])
{
PIXEL *lowlow = (PIXEL *)lowlow_band;
PIXEL *lowhigh = lowhigh_band;
PIXEL *highlow = highlow_band;
PIXEL *highhigh = highhigh_band;
PIXEL *output = output_image;
PIXEL *even_lowpass;
PIXEL *even_highpass;
PIXEL *odd_lowpass;
PIXEL *odd_highpass;
PIXEL *even_output;
PIXEL *odd_output;
size_t buffer_row_size;
int last_row = input_height - 1;
int row, column;
PIXEL *lowhigh_row[3];
PIXEL *lowhigh_line[3];
PIXEL *highlow_line;
PIXEL *highhigh_line;
QUANT highlow_quantization = quantization[HL_BAND];
QUANT lowhigh_quantization = quantization[LH_BAND];
QUANT highhigh_quantization = quantization[HH_BAND];
// Compute positions within the temporary buffer for each row of horizontal lowpass
// and highpass intermediate coefficients computed by the vertical inverse transform
buffer_row_size = input_width * sizeof(PIXEL);
// Compute the positions of the even and odd rows of coefficients
even_lowpass = (PIXEL *)allocator->Alloc(buffer_row_size);
even_highpass = (PIXEL *)allocator->Alloc(buffer_row_size);
odd_lowpass = (PIXEL *)allocator->Alloc(buffer_row_size);
odd_highpass = (PIXEL *)allocator->Alloc(buffer_row_size);
// Compute the positions of the dequantized highpass rows
lowhigh_line[0] = (PIXEL *)allocator->Alloc(buffer_row_size);
lowhigh_line[1] = (PIXEL *)allocator->Alloc(buffer_row_size);
lowhigh_line[2] = (PIXEL *)allocator->Alloc(buffer_row_size);
highlow_line = (PIXEL *)allocator->Alloc(buffer_row_size);
highhigh_line = (PIXEL *)allocator->Alloc(buffer_row_size);
// Convert pitch from bytes to pixels
lowlow_pitch /= sizeof(PIXEL);
lowhigh_pitch /= sizeof(PIXEL);
highlow_pitch /= sizeof(PIXEL);
highhigh_pitch /= sizeof(PIXEL);
output_pitch /= sizeof(PIXEL);
// Initialize the pointers to the even and odd output rows
even_output = output;
odd_output = output + output_pitch;
// Apply the vertical border filter to the first row
row = 0;
// Set pointers to the first three rows in the first highpass band
lowhigh_row[0] = lowhigh + 0 * lowhigh_pitch;
lowhigh_row[1] = lowhigh + 1 * lowhigh_pitch;
lowhigh_row[2] = lowhigh + 2 * lowhigh_pitch;
// Dequantize three rows of highpass coefficients in the first highpass band
DequantizeBandRow16s(lowhigh_row[0], input_width, lowhigh_quantization, lowhigh_line[0]);
DequantizeBandRow16s(lowhigh_row[1], input_width, lowhigh_quantization, lowhigh_line[1]);
DequantizeBandRow16s(lowhigh_row[2], input_width, lowhigh_quantization, lowhigh_line[2]);
// Dequantize one row of coefficients each in the second and third highpass bands
DequantizeBandRow16s(highlow, input_width, highlow_quantization, highlow_line);
DequantizeBandRow16s(highhigh, input_width, highhigh_quantization, highhigh_line);
for (column = 0; column < input_width; column++)
{
int32_t even = 0; // Result of convolution with even filter
int32_t odd = 0; // Result of convolution with odd filter
/***** Compute the vertical inverse for the left two bands *****/
// Apply the even reconstruction filter to the lowpass band
even += 11 * lowlow[column + 0 * lowlow_pitch];
even -= 4 * lowlow[column + 1 * lowlow_pitch];
even += 1 * lowlow[column + 2 * lowlow_pitch];
even += rounding;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highlow_line[column];
even >>= 1;
// The inverse of the left two bands should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Place the even result in the even row
even_lowpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd += 5 * lowlow[column + 0 * lowlow_pitch];
odd += 4 * lowlow[column + 1 * lowlow_pitch];
odd -= 1 * lowlow[column + 2 * lowlow_pitch];
odd += rounding;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highlow_line[column];
odd >>= 1;
// The inverse of the left two bands should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Place the odd result in the odd row
odd_lowpass[column] = ClampPixel(odd);
/***** Compute the vertical inverse for the right two bands *****/
even = 0;
odd = 0;
// Apply the even reconstruction filter to the lowpass band
even += 11 * lowhigh_line[0][column];
even -= 4 * lowhigh_line[1][column];
even += 1 * lowhigh_line[2][column];
even += rounding;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highhigh_line[column];
even >>= 1;
// Place the even result in the even row
even_highpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd += 5 * lowhigh_line[0][column];
odd += 4 * lowhigh_line[1][column];
odd -= 1 * lowhigh_line[2][column];
odd += rounding;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highhigh_line[column];
odd >>= 1;
// Place the odd result in the odd row
odd_highpass[column] = ClampPixel(odd);
}
// Apply the inverse horizontal transform to the even and odd rows
InvertHorizontal16s(even_lowpass, even_highpass, even_output, input_width, output_width);
InvertHorizontal16s(odd_lowpass, odd_highpass, odd_output, input_width, output_width);
// Advance to the next pair of even and odd output rows
even_output += 2 * output_pitch;
odd_output += 2 * output_pitch;
// Always advance the highpass row pointers
highlow += highlow_pitch;
highhigh += highhigh_pitch;
// Advance the row index
row++;
// Process the middle rows using the interior reconstruction filters
for (; row < last_row; row++)
{
// Dequantize one row from each of the two highpass bands
DequantizeBandRow16s(highlow, input_width, highlow_quantization, highlow_line);
DequantizeBandRow16s(highhigh, input_width, highhigh_quantization, highhigh_line);
// Process the entire row
for (column = 0; column < input_width; column++)
{
int32_t even = 0; // Result of convolution with even filter
int32_t odd = 0; // Result of convolution with odd filter
/***** Compute the vertical inverse for the left two bands *****/
// Apply the even reconstruction filter to the lowpass band
even += lowlow[column + 0 * lowlow_pitch];
even -= lowlow[column + 2 * lowlow_pitch];
even += 4;
even >>= 3;
even += lowlow[column + 1 * lowlow_pitch];
// Add the highpass correction
even += highlow_line[column];
even >>= 1;
// The inverse of the left two bands should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Place the even result in the even row
even_lowpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd -= lowlow[column + 0 * lowlow_pitch];
odd += lowlow[column + 2 * lowlow_pitch];
odd += 4;
odd >>= 3;
odd += lowlow[column + 1 * lowlow_pitch];
// Subtract the highpass correction
odd -= highlow_line[column];
odd >>= 1;
// The inverse of the left two bands should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Place the odd result in the odd row
odd_lowpass[column] = ClampPixel(odd);
/***** Compute the vertical inverse for the right two bands *****/
even = 0;
odd = 0;
// Apply the even reconstruction filter to the lowpass band
even += lowhigh_line[0][column];
even -= lowhigh_line[2][column];
even += 4;
even >>= 3;
even += lowhigh_line[1][column];
// Add the highpass correction
even += highhigh_line[column];
even >>= 1;
// Place the even result in the even row
even_highpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd -= lowhigh_line[0][column];
odd += lowhigh_line[2][column];
odd += 4;
odd >>= 3;
odd += lowhigh_line[1][column];
// Subtract the highpass correction
odd -= highhigh_line[column];
odd >>= 1;
// Place the odd result in the odd row
odd_highpass[column] = ClampPixel(odd);
}
// Apply the inverse horizontal transform to the even and odd rows and descale the results
InvertHorizontal16s(even_lowpass, even_highpass, even_output, input_width, output_width);
InvertHorizontal16s(odd_lowpass, odd_highpass, odd_output, input_width, output_width);
// Advance to the next input row in each band
lowlow += lowlow_pitch;
lowhigh += lowhigh_pitch;
highlow += highlow_pitch;
highhigh += highhigh_pitch;
// Advance to the next pair of even and odd output rows
even_output += 2 * output_pitch;
odd_output += 2 * output_pitch;
if (row < last_row - 1)
{
// Compute the address of the next row in the lowhigh band
PIXEL *lowhigh_row_ptr = (lowhigh + 2 * lowhigh_pitch);
//PIXEL *lowhigh_row_ptr = (lowhigh + lowhigh_pitch);
// Shift the rows in the buffer of dequantized lowhigh bands
PIXEL *temp = lowhigh_line[0];
lowhigh_line[0] = lowhigh_line[1];
lowhigh_line[1] = lowhigh_line[2];
lowhigh_line[2] = temp;
// Undo quantization for the next row in the lowhigh band
DequantizeBandRow16s(lowhigh_row_ptr, input_width, lowhigh_quantization, lowhigh_line[2]);
}
}
// Should have exited the loop at the last row
assert(row == last_row);
// Advance the lowlow pointer to the last row in the band
lowlow += lowlow_pitch;
// Check that the band pointers are on the last row in each wavelet band
assert(lowlow == (lowlow_band + last_row * lowlow_pitch));
assert(highlow == (highlow_band + last_row * highlow_pitch));
assert(highhigh == (highhigh_band + last_row * highhigh_pitch));
// Undo quantization for the highlow and highhigh bands
DequantizeBandRow16s(highlow, input_width, highlow_quantization, highlow_line);
DequantizeBandRow16s(highhigh, input_width, highhigh_quantization, highhigh_line);
// Apply the vertical border filter to the last row
for (column = 0; column < input_width; column++)
{
int32_t even = 0; // Result of convolution with even filter
int32_t odd = 0; // Result of convolution with odd filter
/***** Compute the vertical inverse for the left two bands *****/
// Apply the even reconstruction filter to the lowpass band
even += 5 * lowlow[column + 0 * lowlow_pitch];
even += 4 * lowlow[column - 1 * lowlow_pitch];
even -= 1 * lowlow[column - 2 * lowlow_pitch];
even += 4;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highlow_line[column];
even >>= 1;
// The inverse of the left two bands should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Place the even result in the even row
even_lowpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd += 11 * lowlow[column + 0 * lowlow_pitch];
odd -= 4 * lowlow[column - 1 * lowlow_pitch];
odd += 1 * lowlow[column - 2 * lowlow_pitch];
odd += 4;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highlow_line[column];
odd >>= 1;
// The inverse of the left two bands should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Place the odd result in the odd row
odd_lowpass[column] = ClampPixel(odd);
// Compute the vertical inverse for the right two bands //
even = 0;
odd = 0;
// Apply the even reconstruction filter to the lowpass band
even += 5 * lowhigh_line[2][column];
even += 4 * lowhigh_line[1][column];
even -= 1 * lowhigh_line[0][column];
even += 4;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highhigh_line[column];
even >>= 1;
// Place the even result in the even row
even_highpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd += 11 * lowhigh_line[2][column];
odd -= 4 * lowhigh_line[1][column];
odd += 1 * lowhigh_line[0][column];
odd += 4;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highhigh_line[column];
odd >>= 1;
// Place the odd result in the odd row
odd_highpass[column] = ClampPixel(odd);
}
// Apply the inverse horizontal transform to the even and odd rows and descale the results
InvertHorizontal16s(even_lowpass, even_highpass, even_output, input_width, output_width);
// Is the output wavelet shorter than twice the height of the input wavelet?
if (2 * row + 1 < output_height) {
InvertHorizontal16s(odd_lowpass, odd_highpass, odd_output, input_width, output_width);
}
// Free the scratch buffers
allocator->Free(even_lowpass);
allocator->Free(even_highpass);
allocator->Free(odd_lowpass);
allocator->Free(odd_highpass);
allocator->Free(lowhigh_line[0]);
allocator->Free(lowhigh_line[1]);
allocator->Free(lowhigh_line[2]);
allocator->Free(highlow_line);
allocator->Free(highhigh_line);
return CODEC_ERROR_OKAY;
}
/*!
@brief Apply the inverse spatial transform with descaling
This routine is similar to @ref InvertSpatialQuant16s, but a scale factor
that was applied during encoding is removed from the output values.
*/
CODEC_ERROR InvertSpatialQuantDescale16s(gpr_allocator *allocator,
PIXEL *lowlow_band, int lowlow_pitch,
PIXEL *lowhigh_band, int lowhigh_pitch,
PIXEL *highlow_band, int highlow_pitch,
PIXEL *highhigh_band, int highhigh_pitch,
PIXEL *output_image, int output_pitch,
DIMENSION input_width, DIMENSION input_height,
DIMENSION output_width, DIMENSION output_height,
int descale, QUANT quantization[])
{
PIXEL *lowlow = lowlow_band;
PIXEL *lowhigh = lowhigh_band;
PIXEL *highlow = highlow_band;
PIXEL *highhigh = highhigh_band;
PIXEL *output = output_image;
PIXEL *even_lowpass;
PIXEL *even_highpass;
PIXEL *odd_lowpass;
PIXEL *odd_highpass;
PIXEL *even_output;
PIXEL *odd_output;
size_t buffer_row_size;
int last_row = input_height - 1;
int row, column;
PIXEL *lowhigh_row[3];
PIXEL *lowhigh_line[3];
PIXEL *highlow_line;
PIXEL *highhigh_line;
QUANT highlow_quantization = quantization[HL_BAND];
QUANT lowhigh_quantization = quantization[LH_BAND];
QUANT highhigh_quantization = quantization[HH_BAND];
// Compute positions within the temporary buffer for each row of horizontal lowpass
// and highpass intermediate coefficients computed by the vertical inverse transform
buffer_row_size = input_width * sizeof(PIXEL);
// Allocate space for the even and odd rows of results from the inverse vertical filter
even_lowpass = (PIXEL *)allocator->Alloc(buffer_row_size);
even_highpass = (PIXEL *)allocator->Alloc(buffer_row_size);
odd_lowpass = (PIXEL *)allocator->Alloc(buffer_row_size);
odd_highpass = (PIXEL *)allocator->Alloc(buffer_row_size);
// Allocate scratch space for the dequantized highpass coefficients
lowhigh_line[0] = (PIXEL *)allocator->Alloc(buffer_row_size);
lowhigh_line[1] = (PIXEL *)allocator->Alloc(buffer_row_size);
lowhigh_line[2] = (PIXEL *)allocator->Alloc(buffer_row_size);
highlow_line = (PIXEL *)allocator->Alloc(buffer_row_size);
highhigh_line = (PIXEL *)allocator->Alloc(buffer_row_size);
// Convert pitch from bytes to pixels
lowlow_pitch /= sizeof(PIXEL);
lowhigh_pitch /= sizeof(PIXEL);
highlow_pitch /= sizeof(PIXEL);
highhigh_pitch /= sizeof(PIXEL);
output_pitch /= sizeof(PIXEL);
// Initialize the pointers to the even and odd output rows
even_output = output;
odd_output = output + output_pitch;
// Apply the vertical border filter to the first row
row = 0;
// Set pointers to the first three rows in the first highpass band
lowhigh_row[0] = lowhigh + 0 * lowhigh_pitch;
lowhigh_row[1] = lowhigh + 1 * lowhigh_pitch;
lowhigh_row[2] = lowhigh + 2 * lowhigh_pitch;
// Dequantize three rows of highpass coefficients in the first highpass band
DequantizeBandRow16s(lowhigh_row[0], input_width, lowhigh_quantization, lowhigh_line[0]);
DequantizeBandRow16s(lowhigh_row[1], input_width, lowhigh_quantization, lowhigh_line[1]);
DequantizeBandRow16s(lowhigh_row[2], input_width, lowhigh_quantization, lowhigh_line[2]);
// Dequantize one row of coefficients each in the second and third highpass bands
DequantizeBandRow16s(highlow, input_width, highlow_quantization, highlow_line);
DequantizeBandRow16s(highhigh, input_width, highhigh_quantization, highhigh_line);
for (column = 0; column < input_width; column++)
{
int32_t even = 0; // Result of convolution with even filter
int32_t odd = 0; // Result of convolution with odd filter
/***** Compute the vertical inverse for the left two bands *****/
// Apply the even reconstruction filter to the lowpass band
even += 11 * lowlow[column + 0 * lowlow_pitch];
even -= 4 * lowlow[column + 1 * lowlow_pitch];
even += 1 * lowlow[column + 2 * lowlow_pitch];
even += rounding;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highlow_line[column];
even = DivideByShift(even, 1);
// The inverse of the left two bands should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Place the even result in the even row
even_lowpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd += 5 * lowlow[column + 0 * lowlow_pitch];
odd += 4 * lowlow[column + 1 * lowlow_pitch];
odd -= 1 * lowlow[column + 2 * lowlow_pitch];
odd += rounding;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highlow_line[column];
odd = DivideByShift(odd, 1);
// The inverse of the left two bands should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Place the odd result in the odd row
odd_lowpass[column] = ClampPixel(odd);
/***** Compute the vertical inverse for the right two bands *****/
even = 0;
odd = 0;
// Apply the even reconstruction filter to the lowpass band
even += 11 * lowhigh_line[0][column];
even -= 4 * lowhigh_line[1][column];
even += 1 * lowhigh_line[2][column];
even += rounding;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highhigh_line[column];
even = DivideByShift(even, 1);
// Place the even result in the even row
even_highpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd += 5 * lowhigh_line[0][column];
odd += 4 * lowhigh_line[1][column];
odd -= 1 * lowhigh_line[2][column];
odd += rounding;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highhigh_line[column];
odd = DivideByShift(odd, 1);
// Place the odd result in the odd row
odd_highpass[column] = ClampPixel(odd);
}
// Apply the inverse horizontal transform to the even and odd rows and descale the results
InvertHorizontalDescale16s(even_lowpass, even_highpass, even_output,
input_width, output_width, descale);
InvertHorizontalDescale16s(odd_lowpass, odd_highpass, odd_output,
input_width, output_width, descale);
// Advance to the next pair of even and odd output rows
even_output += 2 * output_pitch;
odd_output += 2 * output_pitch;
// Always advance the highpass row pointers
highlow += highlow_pitch;
highhigh += highhigh_pitch;
// Advance the row index
row++;
// Process the middle rows using the interior reconstruction filters
for (; row < last_row; row++)
{
// Dequantize one row from each of the two highpass bands
DequantizeBandRow16s(highlow, input_width, highlow_quantization, highlow_line);
DequantizeBandRow16s(highhigh, input_width, highhigh_quantization, highhigh_line);
// Process the entire row
for (column = 0; column < input_width; column++)
{
int32_t even = 0; // Result of convolution with even filter
int32_t odd = 0; // Result of convolution with odd filter
/***** Compute the vertical inverse for the left two bands *****/
// Apply the even reconstruction filter to the lowpass band
even += lowlow[column + 0 * lowlow_pitch];
even -= lowlow[column + 2 * lowlow_pitch];
even += 4;
even >>= 3;
even += lowlow[column + 1 * lowlow_pitch];
// Add the highpass correction
even += highlow_line[column];
even = DivideByShift(even, 1);
// The inverse of the left two bands should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Place the even result in the even row
even_lowpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd -= lowlow[column + 0 * lowlow_pitch];
odd += lowlow[column + 2 * lowlow_pitch];
odd += 4;
odd >>= 3;
odd += lowlow[column + 1 * lowlow_pitch];
// Subtract the highpass correction
odd -= highlow_line[column];
odd = DivideByShift(odd, 1);
// The inverse of the left two bands should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Place the odd result in the odd row
odd_lowpass[column] = ClampPixel(odd);
/***** Compute the vertical inverse for the right two bands *****/
even = 0;
odd = 0;
// Apply the even reconstruction filter to the lowpass band
even += lowhigh_line[0][column];
even -= lowhigh_line[2][column];
even += 4;
even >>= 3;
even += lowhigh_line[1][column];
// Add the highpass correction
even += highhigh_line[column];
even = DivideByShift(even, 1);
// Place the even result in the even row
even_highpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd -= lowhigh_line[0][column];
odd += lowhigh_line[2][column];
odd += 4;
odd >>= 3;
odd += lowhigh_line[1][column];
// Subtract the highpass correction
odd -= highhigh_line[column];
odd = DivideByShift(odd, 1);
// Place the odd result in the odd row
odd_highpass[column] = ClampPixel(odd);
}
// Apply the inverse horizontal transform to the even and odd rows and descale the results
InvertHorizontalDescale16s(even_lowpass, even_highpass, even_output,
input_width, output_width, descale);
InvertHorizontalDescale16s(odd_lowpass, odd_highpass, odd_output,
input_width, output_width, descale);
// Advance to the next input row in each band
lowlow += lowlow_pitch;
lowhigh += lowhigh_pitch;
highlow += highlow_pitch;
highhigh += highhigh_pitch;
// Advance to the next pair of even and odd output rows
even_output += 2 * output_pitch;
odd_output += 2 * output_pitch;
if (row < last_row - 1)
{
// Compute the address of the next row in the lowhigh band
PIXEL *lowhigh_row_ptr = (lowhigh + 2 * lowhigh_pitch);
// Shift the rows in the buffer of dequantized lowhigh bands
PIXEL *temp = lowhigh_line[0];
lowhigh_line[0] = lowhigh_line[1];
lowhigh_line[1] = lowhigh_line[2];
lowhigh_line[2] = temp;
// Undo quantization for the next row in the lowhigh band
DequantizeBandRow16s(lowhigh_row_ptr, input_width, lowhigh_quantization, lowhigh_line[2]);
}
}
// Should have exited the loop at the last row
assert(row == last_row);
// Advance the lowlow pointer to the last row in the band
lowlow += lowlow_pitch;
// Check that the band pointers are on the last row in each wavelet band
assert(lowlow == (lowlow_band + last_row * lowlow_pitch));
assert(highlow == (highlow_band + last_row * highlow_pitch));
assert(highhigh == (highhigh_band + last_row * highhigh_pitch));
// Undo quantization for the highlow and highhigh bands
DequantizeBandRow16s(highlow, input_width, highlow_quantization, highlow_line);
DequantizeBandRow16s(highhigh, input_width, highhigh_quantization, highhigh_line);
// Apply the vertical border filter to the last row
for (column = 0; column < input_width; column++)
{
int32_t even = 0; // Result of convolution with even filter
int32_t odd = 0; // Result of convolution with odd filter
/***** Compute the vertical inverse for the left two bands *****/
// Apply the even reconstruction filter to the lowpass band
even += 5 * lowlow[column + 0 * lowlow_pitch];
even += 4 * lowlow[column - 1 * lowlow_pitch];
even -= 1 * lowlow[column - 2 * lowlow_pitch];
even += rounding;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highlow_line[column];
even = DivideByShift(even, 1);
// The inverse of the left two bands should be a positive number
//assert(0 <= even && even <= INT16_MAX);
// Place the even result in the even row
even_lowpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd += 11 * lowlow[column + 0 * lowlow_pitch];
odd -= 4 * lowlow[column - 1 * lowlow_pitch];
odd += 1 * lowlow[column - 2 * lowlow_pitch];
odd += rounding;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highlow_line[column];
odd = DivideByShift(odd, 1);
// The inverse of the left two bands should be a positive number
//assert(0 <= odd && odd <= INT16_MAX);
// Place the odd result in the odd row
odd_lowpass[column] = ClampPixel(odd);
/***** Compute the vertical inverse for the right two bands *****/
even = 0;
odd = 0;
// Apply the even reconstruction filter to the lowpass band
even += 5 * lowhigh_line[2][column];
even += 4 * lowhigh_line[1][column];
even -= 1 * lowhigh_line[0][column];
even += rounding;
even = DivideByShift(even, 3);
// Add the highpass correction
even += highhigh_line[column];
even = DivideByShift(even, 1);
// Place the even result in the even row
even_highpass[column] = ClampPixel(even);
// Apply the odd reconstruction filter to the lowpass band
odd += 11 * lowhigh_line[2][column];
odd -= 4 * lowhigh_line[1][column];
odd += 1 * lowhigh_line[0][column];
odd += rounding;
odd = DivideByShift(odd, 3);
// Subtract the highpass correction
odd -= highhigh_line[column];
odd = DivideByShift(odd, 1);
// Place the odd result in the odd row
odd_highpass[column] = ClampPixel(odd);
}
// Apply the inverse horizontal transform to the even and odd rows and descale the results
InvertHorizontalDescale16s(even_lowpass, even_highpass, even_output,
input_width, output_width, descale);
// Is the output wavelet shorter than twice the height of the input wavelet?
if (2 * row + 1 < output_height) {
InvertHorizontalDescale16s(odd_lowpass, odd_highpass, odd_output,
input_width, output_width, descale);
}
// Free the scratch buffers
allocator->Free(even_lowpass);
allocator->Free(even_highpass);
allocator->Free(odd_lowpass);
allocator->Free(odd_highpass);
allocator->Free(lowhigh_line[0]);
allocator->Free(lowhigh_line[1]);
allocator->Free(lowhigh_line[2]);
allocator->Free(highlow_line);
allocator->Free(highhigh_line);
return CODEC_ERROR_OKAY;
}
|