|
2 | 2 | // This software is released under the 2-Clause BSD license, included |
3 | 3 | // below. |
4 | 4 | // |
5 | | -// Copyright (c) 2019, Aous Naman |
| 5 | +// Copyright (c) 2019, Aous Naman |
6 | 6 | // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia |
7 | 7 | // Copyright (c) 2019, The University of New South Wales, Australia |
8 | | -// |
| 8 | +// |
9 | 9 | // Redistribution and use in source and binary forms, with or without |
10 | 10 | // modification, are permitted provided that the following conditions are |
11 | 11 | // met: |
12 | | -// |
| 12 | +// |
13 | 13 | // 1. Redistributions of source code must retain the above copyright |
14 | 14 | // notice, this list of conditions and the following disclaimer. |
15 | | -// |
| 15 | +// |
16 | 16 | // 2. Redistributions in binary form must reproduce the above copyright |
17 | 17 | // notice, this list of conditions and the following disclaimer in the |
18 | 18 | // documentation and/or other materials provided with the distribution. |
19 | | -// |
| 19 | +// |
20 | 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
21 | 21 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
22 | 22 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
@@ -254,7 +254,7 @@ namespace ojph { |
254 | 254 | bits2 = 32 - (int)count_leading_zeros(cp->pass_length[1]); |
255 | 255 | int bits = ojph_max(bits1, bits2 - extra_bit) - 3; |
256 | 256 | bits = ojph_max(bits, 0); |
257 | | - bb_put_bits(&bb, 0xFFFFFFFEu, bits+1, |
| 257 | + bb_put_bits(&bb, 0xFFFFFFFEu, bits+1, |
258 | 258 | elastic, cur_coded_list, ph_bytes); |
259 | 259 |
|
260 | 260 | bb_put_bits(&bb, cp->pass_length[0], bits+3, |
@@ -463,37 +463,53 @@ namespace ojph { |
463 | 463 | } |
464 | 464 | cp->num_passes = num_passes; |
465 | 465 |
|
466 | | - //parse pass lengths |
467 | | - //for one pass, one length, but for 2 or 3 passes, two lengths |
468 | | - int extra_bit = cp->num_passes > 2 ? 1 : 0; |
469 | | - int bits1 = 3; |
| 466 | + // Parse pass lengths |
| 467 | + // When number of passes is one, one length. |
| 468 | + // When number of passes is two or three, two lengths. |
| 469 | + // When number of passes > 3, we have place holder passes; |
| 470 | + // In this case, subtract multiples of 3 from the number of |
| 471 | + // passes; for example, if we have 10 passes, we subtract 9, |
| 472 | + // producing 1 pass. |
| 473 | + |
| 474 | + // 1 => 1, 2 => 2, 3 => 3, 4 => 1, 5 => 2, 6 => 3 |
| 475 | + ui32 num_phld_passes = (num_passes - 1) / 3; |
| 476 | + cp->missing_msbs += num_phld_passes; |
| 477 | + |
| 478 | + num_phld_passes *= 3; |
| 479 | + cp->num_passes = num_passes - num_phld_passes; |
| 480 | + cp->pass_length[0] = cp->pass_length[1] = 0; |
| 481 | + |
| 482 | + int Lblock = 3; |
470 | 483 | bit = 1; |
471 | 484 | while (bit) |
472 | 485 | { |
| 486 | + // add any extra bits here |
473 | 487 | if (bb_read_bit(&bb, bit) == false) |
474 | 488 | { data_left = 0; throw "error reading from file p8"; } |
475 | | - bits1 += bit; |
| 489 | + Lblock += bit; |
476 | 490 | } |
477 | 491 |
|
478 | | - if (bb_read_bits(&bb, bits1, bit) == false) |
| 492 | + int bits = Lblock + 31 - count_leading_zeros(num_phld_passes + 1); |
| 493 | + if (bb_read_bits(&bb, bits, bit) == false) |
479 | 494 | { data_left = 0; throw "error reading from file p9"; } |
480 | | - if (bit < 2) { |
| 495 | + if (bit < 2) |
481 | 496 | throw "The cleanup segment of an HT codeblock cannot contain " |
482 | 497 | "less than 2 bytes"; |
483 | | - } |
484 | | - if (bit >= 65535) { |
| 498 | + if (bit >= 65535) |
485 | 499 | throw "The cleanup segment of an HT codeblock must contain " |
486 | 500 | "less than 65535 bytes"; |
487 | | - } |
488 | 501 | cp->pass_length[0] = bit; |
489 | | - if (num_passes > 1) |
| 502 | + |
| 503 | + if (cp->num_passes > 1) |
490 | 504 | { |
491 | | - if (bb_read_bits(&bb, bits1 + extra_bit, bit) == false) |
| 505 | + //bits = Lblock + 31 - count_leading_zeros(cp->num_passes - 1); |
| 506 | + // The following is simpler than the above, I think? |
| 507 | + bits = Lblock + (cp->num_passes > 2 ? 1 : 0); |
| 508 | + if (bb_read_bits(&bb, bits, bit) == false) |
492 | 509 | { data_left = 0; throw "error reading from file p10"; } |
493 | | - if (bit >= 2047) { |
| 510 | + if (bit >= 2047) |
494 | 511 | throw "The refinement segment (SigProp and MagRep passes) of " |
495 | 512 | "an HT codeblock must contain less than 2047 bytes"; |
496 | | - } |
497 | 513 | cp->pass_length[1] = bit; |
498 | 514 | } |
499 | 515 | } |
@@ -532,7 +548,7 @@ namespace ojph { |
532 | 548 | ui32 t = ojph_min(num_bytes, bb.bytes_left); |
533 | 549 | file->seek(t, infile_base::OJPH_SEEK_CUR); |
534 | 550 | ui32 bytes_read = (ui32)(file->tell() - cur_loc); |
535 | | - cp->pass_length[0] = cp->pass_length[1] = 0; |
| 551 | + cp->pass_length[0] = cp->pass_length[1] = 0; |
536 | 552 | bb.bytes_left -= bytes_read; |
537 | 553 | assert(bytes_read == t || bb.bytes_left == 0); |
538 | 554 | } |
|
0 commit comments