-
Notifications
You must be signed in to change notification settings - Fork 16
/
PktDecoder.java
1400 lines (1236 loc) · 54 KB
/
PktDecoder.java
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
/**
* CVS identifier:
*
* $Id: PktDecoder.java,v 1.46 2002/07/19 12:35:14 grosbois Exp $
*
* Class: PktDecoder
*
* Description: Reads packets heads and keeps location of
* code-blocks' codewords
*
*
*
* COPYRIGHT:
*
* This software module was originally developed by Raphaël Grosbois and
* Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
* Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
* Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
* Centre France S.A) in the course of development of the JPEG2000
* standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
* software module is an implementation of a part of the JPEG 2000
* Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
* Systems AB and Canon Research Centre France S.A (collectively JJ2000
* Partners) agree not to assert against ISO/IEC and users of the JPEG
* 2000 Standard (Users) any of their rights under the copyright, not
* including other intellectual property rights, for this software module
* with respect to the usage by ISO/IEC and Users of this software module
* or modifications thereof for use in hardware or software products
* claiming conformance to the JPEG 2000 Standard. Those intending to use
* this software module in hardware or software products are advised that
* their use may infringe existing patents. The original developers of
* this software module, JJ2000 Partners and ISO/IEC assume no liability
* for use of this software module or modifications thereof. No license
* or right to this software module is granted for non JPEG 2000 Standard
* conforming products. JJ2000 Partners have full right to use this
* software module for his/her own purpose, assign or donate this
* software module to any third party and to inhibit third parties from
* using this software module for non JPEG 2000 Standard conforming
* products. This copyright notice must be included in all copies or
* derivative works of this software module.
*
* Copyright (c) 1999/2000 JJ2000 Partners.
* */
package jj2000.j2k.codestream.reader;
import jj2000.j2k.wavelet.synthesis.*;
import jj2000.j2k.codestream.*;
import jj2000.j2k.entropy.*;
import jj2000.j2k.wavelet.*;
import jj2000.j2k.decoder.*;
import jj2000.j2k.image.*;
import jj2000.j2k.util.*;
import jj2000.j2k.io.*;
import java.util.*;
import java.io.*;
/**
* This class is used to read packet's head and body. All the members must be
* re-initialized at the beginning of each tile thanks to the restart()
* method.
* */
public class PktDecoder implements StdEntropyCoderOptions {
/** Reference to the codestream reader agent */
private BitstreamReaderAgent src;
/** Flag indicating whether packed packet header was used for this tile */
private boolean pph = false;
/** The packed packet header if it was used */
private ByteArrayInputStream pphbais;
/** Reference to decoder specifications */
private DecoderSpecs decSpec;
/** Reference to the HeaderDecoder */
private HeaderDecoder hd;
/** Initial value of the state variable associated with code-block
* length. */
private final int INIT_LBLOCK = 3;
/** The wrapper to read bits for the packet heads */
private PktHeaderBitReader bin;
/** Reference to the stream where to read from */
private RandomAccessIO ehs;
/**
* Maximum number of precincts :
*
* <ul>
* <li> 1st dim: component index.</li>
* <li> 2nd dim: resolution level index.</li>
* </ul>
* */
private Coord[][] numPrec;
/** Index of the current tile */
private int tIdx;
/**
* Array containing the coordinates, width, height, indexes, ... of the
* precincts in the current tile:
*
* <ul>
* <li> 1st dim: component index.</li>
* <li> 2nd dim: resolution level index.</li>
* <li> 3rd dim: precinct index.</li>
* </ul>
* */
private PrecInfo[][][] ppinfo;
/**
* Lblock value used to read code size information in each packet head:
*
* <ul>
* <li> 1st dim: component index.</li>
* <li> 2nd dim: resolution level index.</li>
* <li> 3rd dim: subband index.</li>
* <li> 4th/5th dim: code-block index (vert. and horiz.).</li>
* </ul>
* */
private int[][][][][] lblock;
/**
* Tag tree used to read inclusion informations in packet's head:
*
* <ul>
* <li> 1st dim: component index.</li>
* <li> 2nd dim: resolution level index.</li>
* <li> 3rd dim: precinct index.</li>
* <li> 4th dim: subband index.</li>
* */
private TagTreeDecoder[][][][] ttIncl;
/**
* Tag tree used to read bit-depth information in packet's head:
*
* <ul>
* <li> 1st dim: component index.</li>
* <li> 2nd dim: resolution level index.</li>
* <li> 3rd dim: precinct index.</li>
* <li> 4th dim: subband index.</li>
* </ul>
* */
private TagTreeDecoder[][][][] ttMaxBP;
/** Number of layers in t he current tile */
private int nl = 0;
/** The number of components */
private int nc;
/** Whether or not SOP marker segment are used */
private boolean sopUsed = false;
/** Whether or not EPH marker are used */
private boolean ephUsed = false;
/** Index of the current packet in the tile. Used with SOP marker segment
* */
private int pktIdx;
/** List of code-blocks found in last read packet head (one list
* per subband) */
private Vector[] cblks;
/** Number of codeblocks encountered. used for ncb quit condition*/
private int ncb;
/** Maximum number of codeblocks to read before ncb quit condition is
* reached */
private int maxCB;
/** Flag indicating whether ncb quit condition has been reached */
private boolean ncbQuit;
/** The tile in which the ncb quit condition was reached */
private int tQuit;
/** The component in which the ncb quit condition was reached */
private int cQuit;
/** The subband in which the ncb quit condition was reached */
private int sQuit;
/** The resolution in which the ncb quit condition was reached */
private int rQuit;
/** The x position of the last code block before ncb quit reached */
private int xQuit;
/** The y position of the last code block before ncb quit reached */
private int yQuit;
/** True if truncation mode is used. False if it is parsing mode */
private boolean isTruncMode;
/**
* Creates an empty PktDecoder object associated with given decoder
* specifications and HeaderDecoder. This object must be initialized
* thanks to the restart method before being used.
*
* @param decSpec The decoder specifications.
*
* @param hd The HeaderDecoder instance.
*
* @param ehs The stream where to read data from.
*
* @param src The bit stream reader agent.
*
* @param isTruncMode Whether or not truncation mode is required.
*
* @param maxCB The maximum number of code-blocks to read before ncbquit
*
* */
public PktDecoder(DecoderSpecs decSpec,HeaderDecoder hd,
RandomAccessIO ehs,BitstreamReaderAgent src,
boolean isTruncMode, int maxCB) {
this.decSpec = decSpec;
this.hd = hd;
this.ehs = ehs;
this.isTruncMode = isTruncMode;
bin = new PktHeaderBitReader(ehs);
this.src = src;
ncb = 0;
ncbQuit = false;
this.maxCB = maxCB;
}
/**
* Re-initialize the PktDecoder instance at the beginning of a new tile.
*
* @param nc The number of components in this tile
*
* @param mdl The maximum number of decomposition level in each component
* of this tile
*
* @param nl The number of layers in this tile
*
* @param cbI The code-blocks array
*
* @param pph Flag indicating whether packed packet headers was used
*
* @param pphbais Stream containing the packed packet headers
* */
public CBlkInfo[][][][][] restart(int nc,int[] mdl,int nl,
CBlkInfo[][][][][] cbI, boolean pph,
ByteArrayInputStream pphbais) {
this.nc = nc;
this.nl = nl;
this.tIdx = src.getTileIdx();
this.pph = pph;
this.pphbais = pphbais;
sopUsed = ((Boolean)decSpec.sops.getTileDef(tIdx)).booleanValue();
pktIdx = 0;
ephUsed = ((Boolean)decSpec.ephs.getTileDef(tIdx)).booleanValue();
cbI = new CBlkInfo[nc][][][][];
lblock = new int[nc][][][][];
ttIncl = new TagTreeDecoder[nc][][][];
ttMaxBP = new TagTreeDecoder[nc][][][];
numPrec = new Coord[nc][];
ppinfo = new PrecInfo[nc][][];
// Used to compute the maximum number of precincts for each resolution
// level
int tcx0, tcy0, tcx1, tcy1; // Current tile position in the domain of
// the image component
int trx0, try0, trx1, try1; // Current tile position in the reduced
// resolution image domain
int xrsiz, yrsiz; // Component sub-sampling factors
SubbandSyn root,sb;
int mins,maxs;
Coord nBlk = null;
int cb0x = src.getCbULX();
int cb0y = src.getCbULY();
for(int c=0; c<nc; c++) {
cbI[c] = new CBlkInfo[mdl[c]+1][][][];
lblock[c] = new int[mdl[c]+1][][][];
ttIncl[c] = new TagTreeDecoder[mdl[c]+1][][];
ttMaxBP[c] = new TagTreeDecoder[mdl[c]+1][][];
numPrec[c] = new Coord[mdl[c]+1];
ppinfo[c] = new PrecInfo[mdl[c]+1][];
// Get the tile-component coordinates on the reference grid
tcx0 = src.getResULX(c,mdl[c]);
tcy0 = src.getResULY(c,mdl[c]);
tcx1 = tcx0 + src.getTileCompWidth(tIdx,c,mdl[c]);
tcy1 = tcy0 + src.getTileCompHeight(tIdx,c,mdl[c]);
for(int r=0; r<=mdl[c]; r++) {
// Tile's coordinates in the reduced resolution image domain
trx0 = (int)Math.ceil(tcx0/(double)(1<<(mdl[c]-r)));
try0 = (int)Math.ceil(tcy0/(double)(1<<(mdl[c]-r)));
trx1 = (int)Math.ceil(tcx1/(double)(1<<(mdl[c]-r)));
try1 = (int)Math.ceil(tcy1/(double)(1<<(mdl[c]-r)));
// Calculate the maximum number of precincts for each
// resolution level taking into account tile specific options.
double twoppx = (double)getPPX(tIdx,c,r);
double twoppy = (double)getPPY(tIdx,c,r);
numPrec[c][r] = new Coord();
if (trx1>trx0) {
numPrec[c][r].x = (int)Math.ceil((trx1-cb0x)/twoppx)
- (int)Math.floor((trx0-cb0x)/twoppx);
} else {
numPrec[c][r].x = 0;
}
if (try1>try0) {
numPrec[c][r].y = (int)Math.ceil((try1-cb0y)/twoppy)
- (int)Math.floor((try0-cb0y)/twoppy);
} else {
numPrec[c][r].y = 0;
}
// First and last subbands indexes
mins = (r==0) ? 0 : 1;
maxs = (r==0) ? 1 : 4;
int maxPrec = numPrec[c][r].x * numPrec[c][r].y;
ttIncl[c][r] = new TagTreeDecoder[maxPrec][maxs+1];
ttMaxBP[c][r] = new TagTreeDecoder[maxPrec][maxs+1];
cbI[c][r] = new CBlkInfo[maxs+1][][];
lblock[c][r] = new int[maxs+1][][];
ppinfo[c][r] = new PrecInfo[maxPrec];
fillPrecInfo(c,r,mdl[c]);
root = (SubbandSyn)src.getSynSubbandTree(tIdx,c);
for(int s=mins; s<maxs; s++){
sb = (SubbandSyn)root.getSubbandByIdx(r,s);
nBlk = sb.numCb;
cbI[c][r][s] = new CBlkInfo[nBlk.y][nBlk.x];
lblock[c][r][s] = new int[nBlk.y][nBlk.x];
for(int i=nBlk.y-1;i>=0;i--) {
ArrayUtil.intArraySet(lblock[c][r][s][i],INIT_LBLOCK);
}
} // loop on subbands
} // End loop on resolution levels
} // End loop on components
return cbI;
}
/**
* Retrives precincts and code-blocks coordinates in the given resolution,
* level and component. Finishes TagTreeEncoder initialization as well.
*
* @param c Component index.
*
* @param r Resolution level index.
*
* @param mdl Number of decomposition level in component <tt>c</tt>.
* */
private void fillPrecInfo(int c,int r,int mdl) {
if(ppinfo[c][r].length==0) return; // No precinct in this
// resolution level
Coord tileI = src.getTile(null);
Coord nTiles = src.getNumTiles(null);
int xsiz,ysiz,x0siz,y0siz;
int xt0siz,yt0siz;
int xtsiz,ytsiz;
xt0siz = src.getTilePartULX();
yt0siz = src.getTilePartULY();
xtsiz = src.getNomTileWidth();
ytsiz = src.getNomTileHeight();
x0siz = hd.getImgULX();
y0siz = hd.getImgULY();
xsiz = hd.getImgWidth();
ysiz = hd.getImgHeight();
int tx0 = (tileI.x==0) ? x0siz : xt0siz+tileI.x*xtsiz;
int ty0 = (tileI.y==0) ? y0siz : yt0siz+tileI.y*ytsiz;
int tx1 = (tileI.x!=nTiles.x-1) ? xt0siz+(tileI.x+1)*xtsiz : xsiz;
int ty1 = (tileI.y!=nTiles.y-1) ? yt0siz+(tileI.y+1)*ytsiz : ysiz;
int xrsiz = hd.getCompSubsX(c);
int yrsiz = hd.getCompSubsY(c);
int tcx0 = src.getResULX(c,mdl);
int tcy0 = src.getResULY(c,mdl);
int tcx1 = tcx0 + src.getTileCompWidth(tIdx,c,mdl);
int tcy1 = tcy0 + src.getTileCompHeight(tIdx,c,mdl);
int ndl = mdl-r;
int trx0 = (int)Math.ceil(tcx0/(double)(1<<ndl));
int try0 = (int)Math.ceil(tcy0/(double)(1<<ndl));
int trx1 = (int)Math.ceil(tcx1/(double)(1<<ndl));
int try1 = (int)Math.ceil(tcy1/(double)(1<<ndl));
int cb0x = src.getCbULX();
int cb0y = src.getCbULY();
double twoppx = (double)getPPX(tIdx,c,r);
double twoppy = (double)getPPY(tIdx,c,r);
int twoppx2 = (int)(twoppx/2);
int twoppy2 = (int)(twoppy/2);
// Precincts are located at (cb0x+i*twoppx,cb0y+j*twoppy)
// Valid precincts are those which intersect with the current
// resolution level
int maxPrec = ppinfo[c][r].length;
int nPrec = 0;
int istart = (int)Math.floor((try0-cb0y)/twoppy);
int iend = (int)Math.floor((try1-1-cb0y)/twoppy);
int jstart = (int)Math.floor((trx0-cb0x)/twoppx);
int jend = (int)Math.floor((trx1-1-cb0x)/twoppx);
int acb0x,acb0y;
SubbandSyn root = src.getSynSubbandTree(tIdx,c);
SubbandSyn sb = null;
int p0x,p0y,p1x,p1y; // Precinct projection in subband
int s0x,s0y,s1x,s1y; // Active subband portion
int cw,ch;
int kstart,kend,lstart,lend,k0,l0;
int prg_ulx,prg_uly;
int prg_w = (int)twoppx<<ndl;
int prg_h = (int)twoppy<<ndl;
int tmp1,tmp2;
CBlkCoordInfo cb;
for(int i=istart; i<=iend; i++) { // Vertical precincts
for(int j=jstart; j<=jend; j++,nPrec++) { // Horizontal precincts
if(j==jstart && (trx0-cb0x)%(xrsiz*((int)twoppx))!=0) {
prg_ulx = tx0;
} else {
prg_ulx = cb0x+j*xrsiz*((int)twoppx<<ndl);
}
if(i==istart && (try0-cb0y)%(yrsiz*((int)twoppy))!=0) {
prg_uly = ty0;
} else {
prg_uly = cb0y+i*yrsiz*((int)twoppy<<ndl);
}
ppinfo[c][r][nPrec] =
new PrecInfo(r,(int)(cb0x+j*twoppx),(int)(cb0y+i*twoppy),
(int)twoppx,(int)twoppy,
prg_ulx,prg_uly,prg_w,prg_h);
if(r==0) { // LL subband
acb0x = cb0x;
acb0y = cb0y;
p0x = acb0x+j*(int)twoppx;
p1x = p0x + (int)twoppx;
p0y = acb0y+i*(int)twoppy;
p1y = p0y + (int)twoppy;
sb = (SubbandSyn)root.getSubbandByIdx(0,0);
s0x = (p0x<sb.ulcx) ? sb.ulcx : p0x;
s1x = (p1x>sb.ulcx+sb.w) ? sb.ulcx+sb.w : p1x;
s0y = (p0y<sb.ulcy) ? sb.ulcy : p0y;
s1y = (p1y>sb.ulcy+sb.h) ? sb.ulcy+sb.h : p1y;
// Code-blocks are located at (acb0x+k*cw,acb0y+l*ch)
cw = sb.nomCBlkW;
ch = sb.nomCBlkH;
k0 = (int)Math.floor((sb.ulcy-acb0y)/(double)ch);
kstart = (int)Math.floor((s0y-acb0y)/(double)ch);
kend = (int)Math.floor((s1y-1-acb0y)/(double)ch);
l0 = (int)Math.floor((sb.ulcx-acb0x)/(double)cw);
lstart = (int)Math.floor((s0x-acb0x)/(double)cw);
lend = (int)Math.floor((s1x-1-acb0x)/(double)cw);
if(s1x-s0x<=0 || s1y-s0y<=0) {
ppinfo[c][r][nPrec].nblk[0] = 0;
ttIncl[c][r][nPrec][0] = new TagTreeDecoder(0,0);
ttMaxBP[c][r][nPrec][0] = new TagTreeDecoder(0,0);
} else {
ttIncl[c][r][nPrec][0] =
new TagTreeDecoder(kend-kstart+1,lend-lstart+1);
ttMaxBP[c][r][nPrec][0] =
new TagTreeDecoder(kend-kstart+1,lend-lstart+1);
ppinfo[c][r][nPrec].cblk[0] =
new CBlkCoordInfo[kend-kstart+1][lend-lstart+1];
ppinfo[c][r][nPrec].
nblk[0] = (kend-kstart+1)*(lend-lstart+1);
for(int k=kstart; k<=kend; k++) { // Vertical cblks
for(int l=lstart; l<=lend; l++) { // Horiz. cblks
cb = new CBlkCoordInfo(k-k0,l-l0);
if(l==l0) {
cb.ulx = sb.ulx;
} else {
cb.ulx = sb.ulx+l*cw-(sb.ulcx-acb0x);
}
if(k==k0) {
cb.uly = sb.uly;
} else {
cb.uly = sb.uly+k*ch-(sb.ulcy-acb0y);
}
tmp1 = acb0x+l*cw;
tmp1 = (tmp1>sb.ulcx) ? tmp1 : sb.ulcx;
tmp2 = acb0x+(l+1)*cw;
tmp2 = (tmp2>sb.ulcx+sb.w) ?
sb.ulcx+sb.w : tmp2;
cb.w = tmp2-tmp1;
tmp1 = acb0y+k*ch;
tmp1 = (tmp1>sb.ulcy) ? tmp1 : sb.ulcy;
tmp2 = acb0y+(k+1)*ch;
tmp2 = (tmp2>sb.ulcy+sb.h) ?
sb.ulcy+sb.h : tmp2;
cb.h = tmp2-tmp1;
ppinfo[c][r][nPrec].
cblk[0][k-kstart][l-lstart] = cb;
} // Horizontal code-blocks
} // Vertical code-blocks
}
} else { // HL, LH and HH subbands
// HL subband
acb0x = 0;
acb0y = cb0y;
p0x = acb0x+j*twoppx2;
p1x = p0x + twoppx2;
p0y = acb0y+i*twoppy2;
p1y = p0y + twoppy2;
sb = (SubbandSyn)root.getSubbandByIdx(r,1);
s0x = (p0x<sb.ulcx) ? sb.ulcx : p0x;
s1x = (p1x>sb.ulcx+sb.w) ? sb.ulcx+sb.w : p1x;
s0y = (p0y<sb.ulcy) ? sb.ulcy : p0y;
s1y = (p1y>sb.ulcy+sb.h) ? sb.ulcy+sb.h : p1y;
// Code-blocks are located at (acb0x+k*cw,acb0y+l*ch)
cw = sb.nomCBlkW;
ch = sb.nomCBlkH;
k0 = (int)Math.floor((sb.ulcy-acb0y)/(double)ch);
kstart = (int)Math.floor((s0y-acb0y)/(double)ch);
kend = (int)Math.floor((s1y-1-acb0y)/(double)ch);
l0 = (int)Math.floor((sb.ulcx-acb0x)/(double)cw);
lstart = (int)Math.floor((s0x-acb0x)/(double)cw);
lend = (int)Math.floor((s1x-1-acb0x)/(double)cw);
if(s1x-s0x<=0 || s1y-s0y<=0) {
ppinfo[c][r][nPrec].nblk[1] = 0;
ttIncl[c][r][nPrec][1] = new TagTreeDecoder(0,0);
ttMaxBP[c][r][nPrec][1] = new TagTreeDecoder(0,0);
} else {
ttIncl[c][r][nPrec][1] =
new TagTreeDecoder(kend-kstart+1,lend-lstart+1);
ttMaxBP[c][r][nPrec][1] =
new TagTreeDecoder(kend-kstart+1,lend-lstart+1);
ppinfo[c][r][nPrec].cblk[1] =
new CBlkCoordInfo[kend-kstart+1][lend-lstart+1];
ppinfo[c][r][nPrec].
nblk[1] = (kend-kstart+1)*(lend-lstart+1);
for(int k=kstart; k<=kend; k++) { // Vertical cblks
for(int l=lstart; l<=lend; l++) { // Horiz. cblks
cb = new CBlkCoordInfo(k-k0,l-l0);
if(l==l0) {
cb.ulx = sb.ulx;
} else {
cb.ulx = sb.ulx+l*cw-(sb.ulcx-acb0x);
}
if(k==k0) {
cb.uly = sb.uly;
} else {
cb.uly = sb.uly+k*ch-(sb.ulcy-acb0y);
}
tmp1 = acb0x+l*cw;
tmp1 = (tmp1>sb.ulcx) ? tmp1 : sb.ulcx;
tmp2 = acb0x+(l+1)*cw;
tmp2 = (tmp2>sb.ulcx+sb.w) ?
sb.ulcx+sb.w : tmp2;
cb.w = tmp2-tmp1;
tmp1 = acb0y+k*ch;
tmp1 = (tmp1>sb.ulcy) ? tmp1 : sb.ulcy;
tmp2 = acb0y+(k+1)*ch;
tmp2 = (tmp2>sb.ulcy+sb.h) ?
sb.ulcy+sb.h : tmp2;
cb.h = tmp2-tmp1;
ppinfo[c][r][nPrec].
cblk[1][k-kstart][l-lstart] = cb;
} // Horizontal code-blocks
} // Vertical code-blocks
}
// LH subband
acb0x = cb0x;
acb0y = 0;
p0x = acb0x+j*twoppx2;
p1x = p0x + twoppx2;
p0y = acb0y+i*twoppy2;
p1y = p0y + twoppy2;
sb = (SubbandSyn)root.getSubbandByIdx(r,2);
s0x = (p0x<sb.ulcx) ? sb.ulcx : p0x;
s1x = (p1x>sb.ulcx+sb.w) ? sb.ulcx+sb.w : p1x;
s0y = (p0y<sb.ulcy) ? sb.ulcy : p0y;
s1y = (p1y>sb.ulcy+sb.h) ? sb.ulcy+sb.h : p1y;
// Code-blocks are located at (acb0x+k*cw,acb0y+l*ch)
cw = sb.nomCBlkW;
ch = sb.nomCBlkH;
k0 = (int)Math.floor((sb.ulcy-acb0y)/(double)ch);
kstart = (int)Math.floor((s0y-acb0y)/(double)ch);
kend = (int)Math.floor((s1y-1-acb0y)/(double)ch);
l0 = (int)Math.floor((sb.ulcx-acb0x)/(double)cw);
lstart = (int)Math.floor((s0x-acb0x)/(double)cw);
lend = (int)Math.floor((s1x-1-acb0x)/(double)cw);
if(s1x-s0x<=0 || s1y-s0y<=0) {
ppinfo[c][r][nPrec].nblk[2] = 0;
ttIncl[c][r][nPrec][2] = new TagTreeDecoder(0,0);
ttMaxBP[c][r][nPrec][2] = new TagTreeDecoder(0,0);
} else {
ttIncl[c][r][nPrec][2] =
new TagTreeDecoder(kend-kstart+1,lend-lstart+1);
ttMaxBP[c][r][nPrec][2] =
new TagTreeDecoder(kend-kstart+1,lend-lstart+1);
ppinfo[c][r][nPrec].cblk[2] =
new CBlkCoordInfo[kend-kstart+1][lend-lstart+1];
ppinfo[c][r][nPrec].
nblk[2] = (kend-kstart+1)*(lend-lstart+1);
for(int k=kstart; k<=kend; k++) { // Vertical cblks
for(int l=lstart; l<=lend; l++) { // Horiz cblks
cb = new CBlkCoordInfo(k-k0,l-l0);
if(l==l0) {
cb.ulx = sb.ulx;
} else {
cb.ulx = sb.ulx+l*cw-(sb.ulcx-acb0x);
}
if(k==k0) {
cb.uly = sb.uly;
} else {
cb.uly = sb.uly+k*ch-(sb.ulcy-acb0y);
}
tmp1 = acb0x+l*cw;
tmp1 = (tmp1>sb.ulcx) ? tmp1 : sb.ulcx;
tmp2 = acb0x+(l+1)*cw;
tmp2 = (tmp2>sb.ulcx+sb.w) ?
sb.ulcx+sb.w : tmp2;
cb.w = tmp2-tmp1;
tmp1 = acb0y+k*ch;
tmp1 = (tmp1>sb.ulcy) ? tmp1 : sb.ulcy;
tmp2 = acb0y+(k+1)*ch;
tmp2 = (tmp2>sb.ulcy+sb.h) ?
sb.ulcy+sb.h : tmp2;
cb.h = tmp2-tmp1;
ppinfo[c][r][nPrec].
cblk[2][k-kstart][l-lstart] = cb;
} // Horizontal code-blocks
} // Vertical code-blocks
}
// HH subband
acb0x = 0;
acb0y = 0;
p0x = acb0x+j*twoppx2;
p1x = p0x + twoppx2;
p0y = acb0y+i*twoppy2;
p1y = p0y + twoppy2;
sb = (SubbandSyn)root.getSubbandByIdx(r,3);
s0x = (p0x<sb.ulcx) ? sb.ulcx : p0x;
s1x = (p1x>sb.ulcx+sb.w) ? sb.ulcx+sb.w : p1x;
s0y = (p0y<sb.ulcy) ? sb.ulcy : p0y;
s1y = (p1y>sb.ulcy+sb.h) ? sb.ulcy+sb.h : p1y;
// Code-blocks are located at (acb0x+k*cw,acb0y+l*ch)
cw = sb.nomCBlkW;
ch = sb.nomCBlkH;
k0 = (int)Math.floor((sb.ulcy-acb0y)/(double)ch);
kstart = (int)Math.floor((s0y-acb0y)/(double)ch);
kend = (int)Math.floor((s1y-1-acb0y)/(double)ch);
l0 = (int)Math.floor((sb.ulcx-acb0x)/(double)cw);
lstart = (int)Math.floor((s0x-acb0x)/(double)cw);
lend = (int)Math.floor((s1x-1-acb0x)/(double)cw);
if(s1x-s0x<=0 || s1y-s0y<=0) {
ppinfo[c][r][nPrec].nblk[3] = 0;
ttIncl[c][r][nPrec][3] = new TagTreeDecoder(0,0);
ttMaxBP[c][r][nPrec][3] = new TagTreeDecoder(0,0);
} else {
ttIncl[c][r][nPrec][3] =
new TagTreeDecoder(kend-kstart+1,lend-lstart+1);
ttMaxBP[c][r][nPrec][3] =
new TagTreeDecoder(kend-kstart+1,lend-lstart+1);
ppinfo[c][r][nPrec].cblk[3] =
new CBlkCoordInfo[kend-kstart+1][lend-lstart+1];
ppinfo[c][r][nPrec].
nblk[3] = (kend-kstart+1)*(lend-lstart+1);
for(int k=kstart; k<=kend; k++) { // Vertical cblks
for(int l=lstart; l<=lend; l++) { // Horiz cblks
cb = new CBlkCoordInfo(k-k0,l-l0);
if(l==l0) {
cb.ulx = sb.ulx;
} else {
cb.ulx = sb.ulx+l*cw-(sb.ulcx-acb0x);
}
if(k==k0) {
cb.uly = sb.uly;
} else {
cb.uly = sb.uly+k*ch-(sb.ulcy-acb0y);
}
tmp1 = acb0x+l*cw;
tmp1 = (tmp1>sb.ulcx) ? tmp1 : sb.ulcx;
tmp2 = acb0x+(l+1)*cw;
tmp2 = (tmp2>sb.ulcx+sb.w) ?
sb.ulcx+sb.w : tmp2;
cb.w = tmp2-tmp1;
tmp1 = acb0y+k*ch;
tmp1 = (tmp1>sb.ulcy) ? tmp1 : sb.ulcy;
tmp2 = acb0y+(k+1)*ch;
tmp2 = (tmp2>sb.ulcy+sb.h) ?
sb.ulcy+sb.h : tmp2;
cb.h = tmp2-tmp1;
ppinfo[c][r][nPrec].
cblk[3][k-kstart][l-lstart] = cb;
} // Horizontal code-blocks
} // Vertical code-blocks
}
}
} // Horizontal precincts
} // Vertical precincts
}
/**
* Gets the number of precincts in a given component and resolution level.
*
* @param c Component index
*
* @param r Resolution index
* */
public int getNumPrecinct(int c,int r) {
return numPrec[c][r].x*numPrec[c][r].y;
}
/**
* Read specified packet head and found length of each code-block's piece
* of codewords as well as number of skipped most significant bit-planes.
*
* @param l layer index
*
* @param r Resolution level index
*
* @param c Component index
*
* @param p Precinct index
*
* @param cbI CBlkInfo array of relevant component and resolution
* level.
*
* @param nb The number of bytes to read in each tile before reaching
* output rate (used by truncation mode)
*
* @return True if specified output rate or EOF is reached.
* */
public boolean readPktHead(int l,int r,int c,int p,CBlkInfo[][][] cbI,
int[] nb) throws IOException {
CBlkInfo ccb;
int nSeg; // number of segment to read
int cbLen; // Length of cblk's code-words
int ltp; // last truncation point index
int passtype; // coding pass type
TagTreeDecoder tdIncl,tdBD;
int tmp,tmp2,totnewtp,lblockCur,tpidx;
int sumtotnewtp = 0;
Coord cbc;
int startPktHead = ehs.getPos();
if(startPktHead>=ehs.length()) {
// EOF reached at the beginning of this packet head
return true;
}
int tIdx = src.getTileIdx();
PktHeaderBitReader bin;
int mend,nend;
int b;
SubbandSyn sb;
SubbandSyn root = src.getSynSubbandTree(tIdx,c);
// If packed packet headers was used, use separate stream for reading
// of packet headers
if(pph) {
bin = new PktHeaderBitReader(pphbais);
} else {
bin = this.bin;
}
int mins = (r==0) ? 0 : 1;
int maxs = (r==0) ? 1 : 4;
boolean precFound = false;
for(int s=mins; s<maxs; s++) {
if(p<ppinfo[c][r].length) {
precFound = true;
}
}
if(!precFound) {
return false;
}
PrecInfo prec = ppinfo[c][r][p];
// Synchronize for bit reading
bin.sync();
// If packet is empty there is no info in it (i.e. no code-blocks)
if(bin.readBit()==0) {
// No code-block is included
cblks = new Vector[maxs+1];
for(int s=mins; s<maxs; s++){
cblks[s] = new Vector();
}
pktIdx++;
// If truncation mode, checks if output rate is reached
// unless ncb quit condition is used in which case headers
// are not counted
if(isTruncMode && maxCB == -1) {
tmp = ehs.getPos()-startPktHead;
if(tmp>nb[tIdx]) {
nb[tIdx] = 0;
return true;
} else {
nb[tIdx] -= tmp;
}
}
// Read EPH marker if needed
if(ephUsed) {
readEPHMarker(bin);
}
return false;
}
// Packet is not empty => decode info
// Loop on each subband in this resolution level
if(cblks==null || cblks.length<maxs+1) {
cblks = new Vector[maxs+1];
}
for(int s=mins; s<maxs; s++) {
if(cblks[s]==null) {
cblks[s] = new Vector();
} else {
cblks[s].removeAllElements();
}
sb = (SubbandSyn)root.getSubbandByIdx(r,s);
// No code-block in this precinct
if(prec.nblk[s]==0) {
// Go to next subband
continue;
}
tdIncl = ttIncl[c][r][p][s];
tdBD = ttMaxBP[c][r][p][s];
mend = (prec.cblk[s]==null) ? 0 : prec.cblk[s].length;
for(int m=0; m<mend; m++) { // Vertical code-blocks
nend = (prec.cblk[s][m]==null) ? 0 : prec.cblk[s][m].length;
for (int n=0; n<nend; n++) { // Horizontal code-blocks
cbc = prec.cblk[s][m][n].idx;
b = cbc.x+cbc.y*sb.numCb.x;
ccb = cbI[s][cbc.y][cbc.x];
try {
// If code-block not included in previous layer(s)
if(ccb==null || ccb.ctp==0) {
if(ccb==null) {
ccb = cbI[s][cbc.y][cbc.x] =
new CBlkInfo(prec.cblk[s][m][n].ulx,
prec.cblk[s][m][n].uly,
prec.cblk[s][m][n].w,
prec.cblk[s][m][n].h,nl);
}
ccb.pktIdx[l] = pktIdx;
// Read inclusion using tag-tree
tmp = tdIncl.update(m,n,l+1,bin);
if(tmp>l) { // Not included
continue;
}
// Read bitdepth using tag-tree
tmp = 1;// initialization
for(tmp2=1; tmp>=tmp2; tmp2++) {
tmp = tdBD.update(m,n,tmp2,bin);
}
ccb.msbSkipped = tmp2-2;
// New code-block => at least one truncation point
totnewtp = 1;
ccb.addNTP(l,0);
// Check whether ncb quit condition is reached
ncb++;
if(maxCB != -1 && !ncbQuit && ncb == maxCB){
// ncb quit contidion reached
ncbQuit = true;
tQuit = tIdx;
cQuit = c;
sQuit = s;
rQuit = r;
xQuit = cbc.x;
yQuit = cbc.y;
}
} else { // If code-block already included in one of
// the previous layers.
ccb.pktIdx[l] = pktIdx;
// If not inclused
if(bin.readBit()!=1) {
continue;
}
// At least 1 more truncation point than
// prev. packet
totnewtp = 1;
}
// Read new truncation points
if(bin.readBit()==1) {// if bit is 1
totnewtp++;
// if next bit is 0 do nothing
if(bin.readBit()==1) {//if is 1
totnewtp++;
tmp = bin.readBits(2);
totnewtp += tmp;
// If next 2 bits are not 11 do nothing
if(tmp==0x3) { //if 11
tmp = bin.readBits(5);
totnewtp += tmp;
// If next 5 bits are not 11111 do nothing
if(tmp==0x1F) { //if 11111
totnewtp += bin.readBits(7);
}
}
}
}
ccb.addNTP(l,totnewtp);
sumtotnewtp += totnewtp;
cblks[s].addElement(prec.cblk[s][m][n]);
// Code-block length
// -- Compute the number of bit to read to obtain
// code-block length.
// numBits = betaLamda + log2(totnewtp);
// The length is signalled for each segment in
// addition to the final one. The total length is the
// sum of all segment lengths.
// If regular termination in use, then there is one
// segment per truncation point present. Otherwise, if
// selective arithmetic bypass coding mode is present,
// then there is one termination per bypass/MQ and
// MQ/bypass transition. Otherwise the only
// termination is at the end of the code-block.
int options =
((Integer)decSpec.ecopts.getTileCompVal(tIdx,c)).
intValue();
if( (options&OPT_TERM_PASS) != 0) {
// Regular termination in use, one segment per new
// pass (i.e. truncation point)
nSeg = totnewtp;
} else if( (options&OPT_BYPASS) != 0) {
// Selective arithmetic coding bypass coding mode
// in use, but no regular termination 1 segment up
// to the end of the last pass of the 4th most
// significant bit-plane, and, in each following
// bit-plane, one segment upto the end of the 2nd
// pass and one upto the end of the 3rd pass.