diff options
| author | Antonin Descampe <antonin@gmail.com> | 2016-01-25 23:22:06 +0100 |
|---|---|---|
| committer | Antonin Descampe <antonin@gmail.com> | 2016-01-25 23:22:06 +0100 |
| commit | 0febbff19e05cb1a6c2613eee76ad46a669c96cd (patch) | |
| tree | b6442d01348e2ba6404d0fd6abcc4eb6d6cc1390 /src/lib/openjpwl | |
| parent | bede1568b197021bb576b935422af6c1d0a79e21 (diff) | |
processed all c/h files through astyle with opj_astyle.cfgcodingstyle
Diffstat (limited to 'src/lib/openjpwl')
| -rw-r--r-- | src/lib/openjpwl/crc.c | 166 | ||||
| -rw-r--r-- | src/lib/openjpwl/crc.h | 6 | ||||
| -rw-r--r-- | src/lib/openjpwl/jpwl.c | 2145 | ||||
| -rw-r--r-- | src/lib/openjpwl/jpwl.h | 272 | ||||
| -rw-r--r-- | src/lib/openjpwl/jpwl_lib.c | 3325 | ||||
| -rw-r--r-- | src/lib/openjpwl/rs.c | 673 | ||||
| -rw-r--r-- | src/lib/openjpwl/rs.h | 4 |
7 files changed, 3311 insertions, 3280 deletions
diff --git a/src/lib/openjpwl/crc.c b/src/lib/openjpwl/crc.c index e2ffafac..38ca8b9d 100644 --- a/src/lib/openjpwl/crc.c +++ b/src/lib/openjpwl/crc.c @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -56,45 +56,46 @@ * CRC-checksum. * * implemented by Michael Neumann, 14.06.1998 - * + * */ const unsigned short CRC16_table[256] = { - 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, - 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, - 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, - 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, - 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, - 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, - 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, - 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, - 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, - 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, - 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, - 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, - 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, - 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, - 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, - 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, - 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, - 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, - 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, - 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, - 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, - 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, - 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, - 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, - 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, - 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, - 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, - 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, - 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, - 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, - 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, - 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 }; -void updateCRC16(unsigned short *crc, unsigned char data) { - *crc = CRC16_table[(*crc >> 8) & 0xFF] ^ (*crc << 8) ^ data; +void updateCRC16(unsigned short *crc, unsigned char data) +{ + *crc = CRC16_table[(*crc >> 8) & 0xFF] ^ (*crc << 8) ^ data; } @@ -113,53 +114,54 @@ void updateCRC16(unsigned short *crc, unsigned char data) { * */ const unsigned long CRC32_table[256] = { - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, - 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, - 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, - 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, - 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, - 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, - 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, - 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, - 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, - 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, - 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, - 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, - 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, - 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, - 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, - 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, - 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, - 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, - 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, - 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, - 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, - 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, - 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, - 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, - 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, - 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, - 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, - 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, - 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, - 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, - 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, - 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, - 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, - 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, - 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, - 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, - 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, - 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, - 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, - 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, - 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; -void updateCRC32(unsigned long *crc, unsigned char data) { - *crc = CRC32_table[(unsigned char) *crc ^ data] ^ ((*crc >> 8) & 0x00FFFFFF); +void updateCRC32(unsigned long *crc, unsigned char data) +{ + *crc = CRC32_table[(unsigned char) *crc ^ data] ^ ((*crc >> 8) & 0x00FFFFFF); } #endif /* USE_JPWL */ diff --git a/src/lib/openjpwl/crc.h b/src/lib/openjpwl/crc.h index e4695f75..ea399822 100644 --- a/src/lib/openjpwl/crc.h +++ b/src/lib/openjpwl/crc.h @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -57,7 +57,7 @@ * CRC-checksum. * * implemented by Michael Neumann, 14.06.1998 - * + * */ void updateCRC16(unsigned short *, unsigned char); diff --git a/src/lib/openjpwl/jpwl.c b/src/lib/openjpwl/jpwl.c index 99a34990..d4df6d5c 100644 --- a/src/lib/openjpwl/jpwl.c +++ b/src/lib/openjpwl/jpwl.c @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -47,7 +47,7 @@ /** number of JPWL prepared markers */ static int jwmarker_num; /** properties of JPWL markers to insert */ -static jpwl_marker_t jwmarker[JPWL_MAX_NO_MARKERS]; +static jpwl_marker_t jwmarker[JPWL_MAX_NO_MARKERS]; /*@}*/ @@ -80,11 +80,11 @@ jpwl_epc_ms_t *jpwl_epc_create(opj_j2k_t *j2k, opj_bool esd_on, opj_bool red_on, @param sensval pointer to an array of sensitivity values (if NULL, they will be automatically filled) @return returns the freshly created ESD */ -jpwl_esd_ms_t *jpwl_esd_create(opj_j2k_t *j2k, int comps, - unsigned char addrm, unsigned char ad_size, - unsigned char senst, int se_size, int tileno, - unsigned long int svalnum, void *sensval); - +jpwl_esd_ms_t *jpwl_esd_create(opj_j2k_t *j2k, int comps, + unsigned char addrm, unsigned char ad_size, + unsigned char senst, int se_size, int tileno, + unsigned long int svalnum, void *sensval); + /** this function is used to compare two JPWL markers based on their relevant wishlist position @param arg1 pointer to first marker @@ -117,1201 +117,1211 @@ void jpwl_esd_write(opj_j2k_t *j2k, jpwl_esd_ms_t *esdmark, unsigned char *buf); /*-----------------------------------------------------------------*/ -void jpwl_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) { +void jpwl_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) +{ - int mm; + int mm; - /* let's reset some settings */ + /* let's reset some settings */ - /* clear the existing markers */ - for (mm = 0; mm < jwmarker_num; mm++) { + /* clear the existing markers */ + for (mm = 0; mm < jwmarker_num; mm++) { - switch (jwmarker[mm].id) { + switch (jwmarker[mm].id) { - case J2K_MS_EPB: - opj_free(jwmarker[mm].m.epbmark); - break; + case J2K_MS_EPB: + opj_free(jwmarker[mm].m.epbmark); + break; - case J2K_MS_EPC: - opj_free(jwmarker[mm].m.epcmark); - break; + case J2K_MS_EPC: + opj_free(jwmarker[mm].m.epcmark); + break; - case J2K_MS_ESD: - opj_free(jwmarker[mm].m.esdmark); - break; + case J2K_MS_ESD: + opj_free(jwmarker[mm].m.esdmark); + break; - case J2K_MS_RED: - opj_free(jwmarker[mm].m.redmark); - break; + case J2K_MS_RED: + opj_free(jwmarker[mm].m.redmark); + break; - default: - break; - } + default: + break; } + } - /* clear the marker structure array */ - memset(jwmarker, 0, sizeof(jpwl_marker_t) * JPWL_MAX_NO_MARKERS); + /* clear the marker structure array */ + memset(jwmarker, 0, sizeof(jpwl_marker_t) * JPWL_MAX_NO_MARKERS); - /* no more markers in the list */ - jwmarker_num = 0; + /* no more markers in the list */ + jwmarker_num = 0; - /* let's begin creating a marker list, according to user wishes */ - jpwl_prepare_marks(j2k, cio, image); + /* let's begin creating a marker list, according to user wishes */ + jpwl_prepare_marks(j2k, cio, image); - /* now we dump the JPWL markers on the codestream */ - jpwl_dump_marks(j2k, cio, image); + /* now we dump the JPWL markers on the codestream */ + jpwl_dump_marks(j2k, cio, image); - /* do not know exactly what is this for, - but it gets called during index creation */ - j2k->pos_correction = 0; + /* do not know exactly what is this for, + but it gets called during index creation */ + j2k->pos_correction = 0; } -opj_bool j2k_add_marker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) { - - if (!cstr_info) - return OPJ_FALSE; - - /* expand the list? */ - if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) { - opj_marker_info_t* new_marker; - cstr_info->maxmarknum += 100; - new_marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum * sizeof(opj_marker_info_t)); - if (! new_marker) - { - opj_free(cstr_info->marker); - cstr_info->marker = 0; - cstr_info->marknum = 0; - cstr_info->maxmarknum = 0; - /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add a marker\n"); */ - /* TODO_test_add_marker_result;*/ - return OPJ_FALSE; - } - cstr_info->marker = new_marker; +opj_bool j2k_add_marker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) +{ + + if (!cstr_info) + return OPJ_FALSE; + + /* expand the list? */ + if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) { + opj_marker_info_t* new_marker; + cstr_info->maxmarknum += 100; + new_marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum * sizeof(opj_marker_info_t)); + if (! new_marker) { + opj_free(cstr_info->marker); + cstr_info->marker = 0; + cstr_info->marknum = 0; + cstr_info->maxmarknum = 0; + /* opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add a marker\n"); */ + /* TODO_test_add_marker_result;*/ + return OPJ_FALSE; } + cstr_info->marker = new_marker; + } - /* add the marker */ - cstr_info->marker[cstr_info->marknum].type = type; - cstr_info->marker[cstr_info->marknum].pos = pos; - cstr_info->marker[cstr_info->marknum].len = len; - cstr_info->marknum++; - return OPJ_TRUE; + /* add the marker */ + cstr_info->marker[cstr_info->marknum].type = type; + cstr_info->marker[cstr_info->marknum].pos = pos; + cstr_info->marker[cstr_info->marknum].len = len; + cstr_info->marknum++; + return OPJ_TRUE; } -void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) { +void jpwl_prepare_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) +{ - unsigned short int socsiz_len = 0; - int ciopos = cio_tell(cio), soc_pos = j2k->cstr_info->main_head_start; - unsigned char *socp = NULL; + unsigned short int socsiz_len = 0; + int ciopos = cio_tell(cio), soc_pos = j2k->cstr_info->main_head_start; + unsigned char *socp = NULL; - int tileno, acc_tpno, tpno, tilespec, hprot, sens, pprot, packspec, lastileno, packno; + int tileno, acc_tpno, tpno, tilespec, hprot, sens, pprot, packspec, lastileno, packno; - jpwl_epb_ms_t *epb_mark; - jpwl_epc_ms_t *epc_mark; - jpwl_esd_ms_t *esd_mark; - (void)image; + jpwl_epb_ms_t *epb_mark; + jpwl_epc_ms_t *epc_mark; + jpwl_esd_ms_t *esd_mark; + (void)image; - /* find (SOC + SIZ) length */ - /* I assume SIZ is always the first marker after SOC */ - cio_seek(cio, soc_pos + 4); - socsiz_len = (unsigned short int) cio_read(cio, 2) + 4; /* add the 2 marks length itself */ - cio_seek(cio, soc_pos + 0); - socp = cio_getbp(cio); /* pointer to SOC */ + /* find (SOC + SIZ) length */ + /* I assume SIZ is always the first marker after SOC */ + cio_seek(cio, soc_pos + 4); + socsiz_len = (unsigned short int) cio_read(cio, 2) + 4; /* add the 2 marks length itself */ + cio_seek(cio, soc_pos + 0); + socp = cio_getbp(cio); /* pointer to SOC */ - /* - EPC MS for Main Header: if we are here it's required - */ - /* create the EPC */ - if ((epc_mark = jpwl_epc_create( + /* + EPC MS for Main Header: if we are here it's required + */ + /* create the EPC */ + if ((epc_mark = jpwl_epc_create( j2k, j2k->cp->esd_on, /* is ESD present? */ j2k->cp->red_on, /* is RED present? */ j2k->cp->epb_on, /* is EPB present? */ OPJ_FALSE /* are informative techniques present? */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (epc_mark) { - jwmarker[jwmarker_num].id = J2K_MS_EPC; /* its type */ - jwmarker[jwmarker_num].m.epcmark = epc_mark; /* the EPC */ - jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* after SIZ */ - jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.1; /* not so first */ - jwmarker[jwmarker_num].len = epc_mark->Lepc; /* its length */ - jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].parms_ready = OPJ_FALSE; /* not ready */ - jwmarker[jwmarker_num].data_ready = OPJ_TRUE; /* ready */ - jwmarker_num++; - }; + ))) { + + /* Add this marker to the 'insertanda' list */ + if (epc_mark) { + jwmarker[jwmarker_num].id = J2K_MS_EPC; /* its type */ + jwmarker[jwmarker_num].m.epcmark = epc_mark; /* the EPC */ + jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* after SIZ */ + jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.1; /* not so first */ + jwmarker[jwmarker_num].len = epc_mark->Lepc; /* its length */ + jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].parms_ready = OPJ_FALSE; /* not ready */ + jwmarker[jwmarker_num].data_ready = OPJ_TRUE; /* ready */ + jwmarker_num++; + }; - opj_event_msg(j2k->cinfo, EVT_INFO, - "MH EPC : setting %s%s%s\n", - j2k->cp->esd_on ? "ESD, " : "", - j2k->cp->red_on ? "RED, " : "", - j2k->cp->epb_on ? "EPB, " : "" - ); + opj_event_msg(j2k->cinfo, EVT_INFO, + "MH EPC : setting %s%s%s\n", + j2k->cp->esd_on ? "ESD, " : "", + j2k->cp->red_on ? "RED, " : "", + j2k->cp->epb_on ? "EPB, " : "" + ); + + } else { + /* ooops, problems */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH EPC\n"); + }; + + /* + ESD MS for Main Header + */ + /* first of all, must MH have an ESD MS? */ + if (j2k->cp->esd_on && (j2k->cp->sens_MH >= 0)) { + + /* Create the ESD */ + if ((esd_mark = jpwl_esd_create( + j2k, /* this encoder handle */ + -1, /* we are averaging over all components */ + (unsigned char) j2k->cp->sens_range, /* range method */ + (unsigned char) j2k->cp->sens_addr, /* sensitivity addressing */ + (unsigned char) j2k->cp->sens_MH, /* sensitivity method */ + j2k->cp->sens_size, /* sensitivity size */ + -1, /* this ESD is in main header */ + 0 /*j2k->cstr_info->num*/, /* number of packets in codestream */ + NULL /*sensval*/ /* pointer to sensitivity data of packets */ + ))) { + + /* Add this marker to the 'insertanda' list */ + if (jwmarker_num < JPWL_MAX_NO_MARKERS) { + jwmarker[jwmarker_num].id = J2K_MS_ESD; /* its type */ + jwmarker[jwmarker_num].m.esdmark = esd_mark; /* the EPB */ + jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* we choose to place it after SIZ */ + jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.2; /* not first at all! */ + jwmarker[jwmarker_num].len = esd_mark->Lesd; /* its length */ + jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* not ready, yet */ + jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* not ready */ + jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ + jwmarker_num++; + } + + opj_event_msg(j2k->cinfo, EVT_INFO, + "MH ESDs: method %d\n", + j2k->cp->sens_MH + ); } else { - /* ooops, problems */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH EPC\n"); + /* ooops, problems */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH ESD\n"); }; - /* - ESD MS for Main Header - */ - /* first of all, must MH have an ESD MS? */ - if (j2k->cp->esd_on && (j2k->cp->sens_MH >= 0)) { + } + + /* + ESD MSs for Tile Part Headers + */ + /* cycle through tiles */ + sens = -1; /* default spec: no ESD */ + tilespec = 0; /* first tile spec */ + acc_tpno = 0; + for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) { + + opj_event_msg(j2k->cinfo, EVT_INFO, + "Tile %d has %d tile part(s)\n", + tileno, j2k->cstr_info->tile[tileno].num_tps + ); + + /* for every tile part in the tile */ + for (tpno = 0; tpno < j2k->cstr_info->tile[tileno].num_tps; tpno++, acc_tpno++) { + + int sot_len, Psot, Psotp, mm; + unsigned long sot_pos, post_sod_pos; + + unsigned long int left_THmarks_len; + + /******* sot_pos = j2k->cstr_info->tile[tileno].start_pos; */ + sot_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos; + cio_seek(cio, sot_pos + 2); + sot_len = cio_read(cio, 2); /* SOT Len */ + cio_skip(cio, 2); + Psotp = cio_tell(cio); + Psot = cio_read(cio, 4); /* tile length */ + + /******* post_sod_pos = j2k->cstr_info->tile[tileno].end_header + 1; */ + post_sod_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_end_header + 1; + left_THmarks_len = post_sod_pos - sot_pos; + + /* add all the lengths of the markers which are len-ready and stay within SOT and SOD */ + for (mm = 0; mm < jwmarker_num; mm++) { + if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) { + if (jwmarker[mm].len_ready) + left_THmarks_len += jwmarker[mm].len + 2; + else { + opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n", + jwmarker[mm].id, jwmarker[mm].dpos); + exit(1); + } + } + } + + /******* if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == tileno)) */ + if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == acc_tpno)) + /* we got a specification from this tile onwards */ + sens = j2k->cp->sens_TPH[tilespec++]; + + /* must this TPH have an ESD MS? */ + if (j2k->cp->esd_on && (sens >= 0)) { /* Create the ESD */ if ((esd_mark = jpwl_esd_create( - j2k, /* this encoder handle */ - -1, /* we are averaging over all components */ - (unsigned char) j2k->cp->sens_range, /* range method */ - (unsigned char) j2k->cp->sens_addr, /* sensitivity addressing */ - (unsigned char) j2k->cp->sens_MH, /* sensitivity method */ - j2k->cp->sens_size, /* sensitivity size */ - -1, /* this ESD is in main header */ - 0 /*j2k->cstr_info->num*/, /* number of packets in codestream */ - NULL /*sensval*/ /* pointer to sensitivity data of packets */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (jwmarker_num < JPWL_MAX_NO_MARKERS) { - jwmarker[jwmarker_num].id = J2K_MS_ESD; /* its type */ - jwmarker[jwmarker_num].m.esdmark = esd_mark; /* the EPB */ - jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* we choose to place it after SIZ */ - jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.2; /* not first at all! */ - jwmarker[jwmarker_num].len = esd_mark->Lesd; /* its length */ - jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* not ready, yet */ - jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* not ready */ - jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ - jwmarker_num++; - } + j2k, /* this encoder handle */ + -1, /* we are averaging over all components */ + (unsigned char) j2k->cp->sens_range, /* range method */ + (unsigned char) j2k->cp->sens_addr, /* sensitivity addressing size */ + (unsigned char) sens, /* sensitivity method */ + j2k->cp->sens_size, /* sensitivity value size */ + tileno, /* this ESD is in a tile */ + 0, /* number of packets in codestream */ + NULL /* pointer to sensitivity data of packets */ + ))) { + + /* Add this marker to the 'insertanda' list */ + if (jwmarker_num < JPWL_MAX_NO_MARKERS) { + jwmarker[jwmarker_num].id = J2K_MS_ESD; /* its type */ + jwmarker[jwmarker_num].m.esdmark = esd_mark; /* the EPB */ + /****** jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].start_pos + sot_len + 2; */ /* after SOT */ + jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2; /* after SOT */ + jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.2; /* not first at all! */ + jwmarker[jwmarker_num].len = esd_mark->Lesd; /* its length */ + jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready, yet */ + jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* not ready */ + jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* ready */ + jwmarker_num++; + } - opj_event_msg(j2k->cinfo, EVT_INFO, - "MH ESDs: method %d\n", - j2k->cp->sens_MH - ); + /* update Psot of the tile */ + cio_seek(cio, Psotp); + cio_write(cio, Psot + esd_mark->Lesd + 2, 4); + + opj_event_msg(j2k->cinfo, EVT_INFO, + /******* "TPH ESDs: tile %02d, method %d\n", */ + "TPH ESDs: tile %02d, part %02d, method %d\n", + /******* tileno, */ + tileno, tpno, + sens + ); } else { - /* ooops, problems */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH ESD\n"); + /* ooops, problems */ + /***** opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d\n", tileno); */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d,%d\n", tileno, tpno); }; + } + } - /* - ESD MSs for Tile Part Headers - */ - /* cycle through tiles */ - sens = -1; /* default spec: no ESD */ - tilespec = 0; /* first tile spec */ - acc_tpno = 0; - for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) { + }; - opj_event_msg(j2k->cinfo, EVT_INFO, - "Tile %d has %d tile part(s)\n", - tileno, j2k->cstr_info->tile[tileno].num_tps - ); + /* + EPB MS for Main Header + */ + /* first of all, must MH have an EPB MS? */ + if (j2k->cp->epb_on && (j2k->cp->hprot_MH > 0)) { + + int mm; - /* for every tile part in the tile */ - for (tpno = 0; tpno < j2k->cstr_info->tile[tileno].num_tps; tpno++, acc_tpno++) { - - int sot_len, Psot, Psotp, mm; - unsigned long sot_pos, post_sod_pos; - - unsigned long int left_THmarks_len; - - /******* sot_pos = j2k->cstr_info->tile[tileno].start_pos; */ - sot_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos; - cio_seek(cio, sot_pos + 2); - sot_len = cio_read(cio, 2); /* SOT Len */ - cio_skip(cio, 2); - Psotp = cio_tell(cio); - Psot = cio_read(cio, 4); /* tile length */ - - /******* post_sod_pos = j2k->cstr_info->tile[tileno].end_header + 1; */ - post_sod_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_end_header + 1; - left_THmarks_len = post_sod_pos - sot_pos; - - /* add all the lengths of the markers which are len-ready and stay within SOT and SOD */ - for (mm = 0; mm < jwmarker_num; mm++) { - if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) { - if (jwmarker[mm].len_ready) - left_THmarks_len += jwmarker[mm].len + 2; - else { - opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n", - jwmarker[mm].id, jwmarker[mm].dpos); - exit(1); - } - } - } - - /******* if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == tileno)) */ - if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->sens_TPH_tileno[tilespec] == acc_tpno)) - /* we got a specification from this tile onwards */ - sens = j2k->cp->sens_TPH[tilespec++]; - - /* must this TPH have an ESD MS? */ - if (j2k->cp->esd_on && (sens >= 0)) { - - /* Create the ESD */ - if ((esd_mark = jpwl_esd_create( - j2k, /* this encoder handle */ - -1, /* we are averaging over all components */ - (unsigned char) j2k->cp->sens_range, /* range method */ - (unsigned char) j2k->cp->sens_addr, /* sensitivity addressing size */ - (unsigned char) sens, /* sensitivity method */ - j2k->cp->sens_size, /* sensitivity value size */ - tileno, /* this ESD is in a tile */ - 0, /* number of packets in codestream */ - NULL /* pointer to sensitivity data of packets */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (jwmarker_num < JPWL_MAX_NO_MARKERS) { - jwmarker[jwmarker_num].id = J2K_MS_ESD; /* its type */ - jwmarker[jwmarker_num].m.esdmark = esd_mark; /* the EPB */ - /****** jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].start_pos + sot_len + 2; */ /* after SOT */ - jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2; /* after SOT */ - jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos + 0.2; /* not first at all! */ - jwmarker[jwmarker_num].len = esd_mark->Lesd; /* its length */ - jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready, yet */ - jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* not ready */ - jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* ready */ - jwmarker_num++; - } - - /* update Psot of the tile */ - cio_seek(cio, Psotp); - cio_write(cio, Psot + esd_mark->Lesd + 2, 4); - - opj_event_msg(j2k->cinfo, EVT_INFO, - /******* "TPH ESDs: tile %02d, method %d\n", */ - "TPH ESDs: tile %02d, part %02d, method %d\n", - /******* tileno, */ - tileno, tpno, - sens - ); - - } else { - /* ooops, problems */ - /***** opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d\n", tileno); */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH ESD #%d,%d\n", tileno, tpno); - }; - - } - + /* position of SOT */ + unsigned int sot_pos = j2k->cstr_info->main_head_end + 1; + + /* how much space is there between end of SIZ and beginning of SOT? */ + int left_MHmarks_len = sot_pos - socsiz_len; + + /* add all the lengths of the markers which are len-ready and stay within SOC and SOT */ + for (mm = 0; mm < jwmarker_num; mm++) { + if ( jwmarker[mm].pos < sot_pos) { /* jwmarker[mm].pos >=0 since ulong */ + if (jwmarker[mm].len_ready) + left_MHmarks_len += jwmarker[mm].len + 2; + else { + opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up MH EPB\n", + jwmarker[mm].id, jwmarker[mm].dpos); + exit(1); } - + } + } + + /* Create the EPB */ + if ((epb_mark = jpwl_epb_create( + j2k, /* this encoder handle */ + OPJ_TRUE, /* is it the latest? */ + OPJ_TRUE, /* is it packed? not for now */ + -1, /* we are in main header */ + 0, /* its index is 0 (first) */ + j2k->cp->hprot_MH, /* protection type parameters of data */ + socsiz_len, /* pre-data: only SOC+SIZ */ + left_MHmarks_len /* post-data: from SOC to SOT, and all JPWL markers within */ + ))) { + + /* Add this marker to the 'insertanda' list */ + if (jwmarker_num < JPWL_MAX_NO_MARKERS) { + jwmarker[jwmarker_num].id = J2K_MS_EPB; /* its type */ + jwmarker[jwmarker_num].m.epbmark = epb_mark; /* the EPB */ + jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* after SIZ */ + jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos; /* first first first! */ + jwmarker[jwmarker_num].len = epb_mark->Lepb; /* its length */ + jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ + jwmarker_num++; + } + + opj_event_msg(j2k->cinfo, EVT_INFO, + "MH EPB : prot. %d\n", + j2k->cp->hprot_MH + ); + + } else { + /* ooops, problems */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH EPB\n"); }; + } + + /* + EPB MSs for Tile Parts + */ + /* cycle through TPHs */ + hprot = j2k->cp->hprot_MH; /* default spec */ + tilespec = 0; /* first tile spec */ + lastileno = 0; + packspec = 0; + pprot = -1; + acc_tpno = 0; + for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) { - /* - EPB MS for Main Header - */ - /* first of all, must MH have an EPB MS? */ - if (j2k->cp->epb_on && (j2k->cp->hprot_MH > 0)) { - - int mm; - - /* position of SOT */ - unsigned int sot_pos = j2k->cstr_info->main_head_end + 1; - - /* how much space is there between end of SIZ and beginning of SOT? */ - int left_MHmarks_len = sot_pos - socsiz_len; - - /* add all the lengths of the markers which are len-ready and stay within SOC and SOT */ - for (mm = 0; mm < jwmarker_num; mm++) { - if ( jwmarker[mm].pos < sot_pos) { /* jwmarker[mm].pos >=0 since ulong */ - if (jwmarker[mm].len_ready) - left_MHmarks_len += jwmarker[mm].len + 2; - else { - opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up MH EPB\n", - jwmarker[mm].id, jwmarker[mm].dpos); - exit(1); - } - } + opj_event_msg(j2k->cinfo, EVT_INFO, + "Tile %d has %d tile part(s)\n", + tileno, j2k->cstr_info->tile[tileno].num_tps + ); + + /* for every tile part in the tile */ + for (tpno = 0; tpno < j2k->cstr_info->tile[tileno].num_tps; tpno++, acc_tpno++) { + + int sot_len, Psot, Psotp, mm, epb_index = 0, prot_len = 0; + unsigned long sot_pos, post_sod_pos; + unsigned long int left_THmarks_len/*, epbs_len = 0*/; + int startpack = 0, stoppack = j2k->cstr_info->packno; + int first_tp_pack, last_tp_pack; + jpwl_epb_ms_t *tph_epb = NULL; + + /****** sot_pos = j2k->cstr_info->tile[tileno].start_pos; */ + sot_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos; + cio_seek(cio, sot_pos + 2); + sot_len = cio_read(cio, 2); /* SOT Len */ + cio_skip(cio, 2); + Psotp = cio_tell(cio); + Psot = cio_read(cio, 4); /* tile length */ + + /* a-priori length of the data dwelling between SOT and SOD */ + /****** post_sod_pos = j2k->cstr_info->tile[tileno].end_header + 1; */ + post_sod_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_end_header + 1; + left_THmarks_len = post_sod_pos - (sot_pos + sot_len + 2); + + /* add all the lengths of the JPWL markers which are len-ready and stay within SOT and SOD */ + for (mm = 0; mm < jwmarker_num; mm++) { + if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) { + if (jwmarker[mm].len_ready) + left_THmarks_len += jwmarker[mm].len + 2; + else { + opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n", + jwmarker[mm].id, jwmarker[mm].dpos); + exit(1); + } } + } + + /****** if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == tileno)) */ + if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == acc_tpno)) + /* we got a specification from this tile part onwards */ + hprot = j2k->cp->hprot_TPH[tilespec++]; + + /* must this TPH have an EPB MS? */ + if (j2k->cp->epb_on && (hprot > 0)) { /* Create the EPB */ if ((epb_mark = jpwl_epb_create( - j2k, /* this encoder handle */ - OPJ_TRUE, /* is it the latest? */ - OPJ_TRUE, /* is it packed? not for now */ - -1, /* we are in main header */ - 0, /* its index is 0 (first) */ - j2k->cp->hprot_MH, /* protection type parameters of data */ - socsiz_len, /* pre-data: only SOC+SIZ */ - left_MHmarks_len /* post-data: from SOC to SOT, and all JPWL markers within */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (jwmarker_num < JPWL_MAX_NO_MARKERS) { - jwmarker[jwmarker_num].id = J2K_MS_EPB; /* its type */ - jwmarker[jwmarker_num].m.epbmark = epb_mark; /* the EPB */ - jwmarker[jwmarker_num].pos = soc_pos + socsiz_len; /* after SIZ */ - jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos; /* first first first! */ - jwmarker[jwmarker_num].len = epb_mark->Lepb; /* its length */ - jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ - jwmarker_num++; - } + j2k, /* this encoder handle */ + OPJ_FALSE, /* is it the latest? in TPH, no for now (if huge data size in TPH, we'd need more) */ + OPJ_TRUE, /* is it packed? yes for now */ + tileno, /* we are in TPH */ + epb_index++, /* its index is 0 (first) */ + hprot, /* protection type parameters of following data */ + sot_len + 2, /* pre-data length: only SOT */ + left_THmarks_len /* post-data length: from SOT end to SOD inclusive */ + ))) { + + /* Add this marker to the 'insertanda' list */ + if (jwmarker_num < JPWL_MAX_NO_MARKERS) { + jwmarker[jwmarker_num].id = J2K_MS_EPB; /* its type */ + jwmarker[jwmarker_num].m.epbmark = epb_mark; /* the EPB */ + /****** jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].start_pos + sot_len + 2; */ /* after SOT */ + jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2; /* after SOT */ + jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos; /* first first first! */ + jwmarker[jwmarker_num].len = epb_mark->Lepb; /* its length */ + jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* ready */ + jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ + jwmarker_num++; + } - opj_event_msg(j2k->cinfo, EVT_INFO, - "MH EPB : prot. %d\n", - j2k->cp->hprot_MH - ); + /* update Psot of the tile */ + Psot += epb_mark->Lepb + 2; + + opj_event_msg(j2k->cinfo, EVT_INFO, + /***** "TPH EPB : tile %02d, prot. %d\n", */ + "TPH EPB : tile %02d, part %02d, prot. %d\n", + /***** tileno, */ + tileno, tpno, + hprot + ); + + /* save this TPH EPB address */ + tph_epb = epb_mark; } else { - /* ooops, problems */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create MH EPB\n"); + /* ooops, problems */ + /****** opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB #%d\n", tileno); */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB in #%d,d\n", tileno, tpno); }; - } - /* - EPB MSs for Tile Parts - */ - /* cycle through TPHs */ - hprot = j2k->cp->hprot_MH; /* default spec */ - tilespec = 0; /* first tile spec */ - lastileno = 0; - packspec = 0; - pprot = -1; - acc_tpno = 0; - for (tileno = 0; tileno < j2k->cstr_info->tw * j2k->cstr_info->th; tileno++) { + } - opj_event_msg(j2k->cinfo, EVT_INFO, - "Tile %d has %d tile part(s)\n", - tileno, j2k->cstr_info->tile[tileno].num_tps - ); + startpack = 0; + /* EPB MSs for UEP packet data protection in Tile Parts */ + /****** for (packno = 0; packno < j2k->cstr_info->num; packno++) { */ + /*first_tp_pack = (tpno > 0) ? (first_tp_pack + j2k->cstr_info->tile[tileno].tp[tpno - 1].tp_numpacks) : 0;*/ + first_tp_pack = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pack; + last_tp_pack = first_tp_pack + j2k->cstr_info->tile[tileno].tp[tpno].tp_numpacks - 1; + for (packno = 0; packno < j2k->cstr_info->tile[tileno].tp[tpno].tp_numpacks; packno++) { - /* for every tile part in the tile */ - for (tpno = 0; tpno < j2k->cstr_info->tile[tileno].num_tps; tpno++, acc_tpno++) { - - int sot_len, Psot, Psotp, mm, epb_index = 0, prot_len = 0; - unsigned long sot_pos, post_sod_pos; - unsigned long int left_THmarks_len/*, epbs_len = 0*/; - int startpack = 0, stoppack = j2k->cstr_info->packno; - int first_tp_pack, last_tp_pack; - jpwl_epb_ms_t *tph_epb = NULL; - - /****** sot_pos = j2k->cstr_info->tile[tileno].start_pos; */ - sot_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos; - cio_seek(cio, sot_pos + 2); - sot_len = cio_read(cio, 2); /* SOT Len */ - cio_skip(cio, 2); - Psotp = cio_tell(cio); - Psot = cio_read(cio, 4); /* tile length */ - - /* a-priori length of the data dwelling between SOT and SOD */ - /****** post_sod_pos = j2k->cstr_info->tile[tileno].end_header + 1; */ - post_sod_pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_end_header + 1; - left_THmarks_len = post_sod_pos - (sot_pos + sot_len + 2); - - /* add all the lengths of the JPWL markers which are len-ready and stay within SOT and SOD */ - for (mm = 0; mm < jwmarker_num; mm++) { - if ((jwmarker[mm].pos >= sot_pos) && (jwmarker[mm].pos < post_sod_pos)) { - if (jwmarker[mm].len_ready) - left_THmarks_len += jwmarker[mm].len + 2; - else { - opj_event_msg(j2k->cinfo, EVT_ERROR, "MS %x in %f is not len-ready: could not set up TH EPB\n", - jwmarker[mm].id, jwmarker[mm].dpos); - exit(1); - } - } - } - - /****** if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == tileno)) */ - if ((tilespec < JPWL_MAX_NO_TILESPECS) && (j2k->cp->hprot_TPH_tileno[tilespec] == acc_tpno)) - /* we got a specification from this tile part onwards */ - hprot = j2k->cp->hprot_TPH[tilespec++]; - - /* must this TPH have an EPB MS? */ - if (j2k->cp->epb_on && (hprot > 0)) { - - /* Create the EPB */ - if ((epb_mark = jpwl_epb_create( - j2k, /* this encoder handle */ - OPJ_FALSE, /* is it the latest? in TPH, no for now (if huge data size in TPH, we'd need more) */ - OPJ_TRUE, /* is it packed? yes for now */ - tileno, /* we are in TPH */ - epb_index++, /* its index is 0 (first) */ - hprot, /* protection type parameters of following data */ - sot_len + 2, /* pre-data length: only SOT */ - left_THmarks_len /* post-data length: from SOT end to SOD inclusive */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (jwmarker_num < JPWL_MAX_NO_MARKERS) { - jwmarker[jwmarker_num].id = J2K_MS_EPB; /* its type */ - jwmarker[jwmarker_num].m.epbmark = epb_mark; /* the EPB */ - /****** jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].start_pos + sot_len + 2; */ /* after SOT */ - jwmarker[jwmarker_num].pos = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2; /* after SOT */ - jwmarker[jwmarker_num].dpos = (double) jwmarker[jwmarker_num].pos; /* first first first! */ - jwmarker[jwmarker_num].len = epb_mark->Lepb; /* its length */ - jwmarker[jwmarker_num].len_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].parms_ready = OPJ_TRUE; /* ready */ - jwmarker[jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ - jwmarker_num++; - } - - /* update Psot of the tile */ - Psot += epb_mark->Lepb + 2; - - opj_event_msg(j2k->cinfo, EVT_INFO, - /***** "TPH EPB : tile %02d, prot. %d\n", */ - "TPH EPB : tile %02d, part %02d, prot. %d\n", - /***** tileno, */ - tileno, tpno, - hprot - ); - - /* save this TPH EPB address */ - tph_epb = epb_mark; - - } else { - /* ooops, problems */ - /****** opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB #%d\n", tileno); */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB in #%d,d\n", tileno, tpno); - }; - - } - - startpack = 0; - /* EPB MSs for UEP packet data protection in Tile Parts */ - /****** for (packno = 0; packno < j2k->cstr_info->num; packno++) { */ - /*first_tp_pack = (tpno > 0) ? (first_tp_pack + j2k->cstr_info->tile[tileno].tp[tpno - 1].tp_numpacks) : 0;*/ - first_tp_pack = j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pack; - last_tp_pack = first_tp_pack + j2k->cstr_info->tile[tileno].tp[tpno].tp_numpacks - 1; - for (packno = 0; packno < j2k->cstr_info->tile[tileno].tp[tpno].tp_numpacks; packno++) { - - /******** if ((packspec < JPWL_MAX_NO_PACKSPECS) && - (j2k->cp->pprot_tileno[packspec] == tileno) && (j2k->cp->pprot_packno[packspec] == packno)) { */ - if ((packspec < JPWL_MAX_NO_PACKSPECS) && - (j2k->cp->pprot_tileno[packspec] == acc_tpno) && (j2k->cp->pprot_packno[packspec] == packno)) { - - /* we got a specification from this tile and packet onwards */ - /* print the previous spec */ - if (packno > 0) { - stoppack = packno - 1; - opj_event_msg(j2k->cinfo, EVT_INFO, - /***** "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", */ - "UEP EPBs: tile %02d, part %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", - /***** tileno, */ - tileno, tpno, - startpack, - stoppack, - /***** j2k->cstr_info->tile[tileno].packet[startpack].start_pos, */ - j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos, - /***** j2k->cstr_info->tile[tileno].packet[stoppack].end_pos, */ - j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos, - pprot); - - /***** prot_len = j2k->cstr_info->tile[tileno].packet[stoppack].end_pos + 1 - - j2k->cstr_info->tile[tileno].packet[startpack].start_pos; */ - prot_len = j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos + 1 - - j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos; - - /* - particular case: if this is the last header and the last packet, - then it is better to protect even the EOC marker - */ - /****** if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && - (stoppack == (j2k->cstr_info->num - 1))) */ - if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && - (tpno == (j2k->cstr_info->tile[tileno].num_tps - 1)) && - (stoppack == last_tp_pack)) - /* add the EOC len */ - prot_len += 2; - - /* let's add the EPBs */ - Psot += jpwl_epbs_add( - j2k, /* J2K handle */ - jwmarker, /* pointer to JPWL markers list */ - &jwmarker_num, /* pointer to the number of current markers */ - OPJ_FALSE, /* latest */ - OPJ_TRUE, /* packed */ - OPJ_FALSE, /* inside MH */ - &epb_index, /* pointer to EPB index */ - pprot, /* protection type */ - /****** (double) (j2k->cstr_info->tile[tileno].start_pos + sot_len + 2) + 0.0001, */ /* position */ - (double) (j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2) + 0.0001, /* position */ - tileno, /* number of tile */ - 0, /* length of pre-data */ - prot_len /*4000*/ /* length of post-data */ - ); - } - - startpack = packno; - pprot = j2k->cp->pprot[packspec++]; - } - - /*printf("Tile %02d, pack %02d ==> %d\n", tileno, packno, pprot);*/ - - } - - /* we are at the end: print the remaining spec */ + /******** if ((packspec < JPWL_MAX_NO_PACKSPECS) && + (j2k->cp->pprot_tileno[packspec] == tileno) && (j2k->cp->pprot_packno[packspec] == packno)) { */ + if ((packspec < JPWL_MAX_NO_PACKSPECS) && + (j2k->cp->pprot_tileno[packspec] == acc_tpno) && (j2k->cp->pprot_packno[packspec] == packno)) { + + /* we got a specification from this tile and packet onwards */ + /* print the previous spec */ + if (packno > 0) { stoppack = packno - 1; - if (pprot >= 0) { - - opj_event_msg(j2k->cinfo, EVT_INFO, - /**** "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", */ - "UEP EPBs: tile %02d, part %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", - /**** tileno, */ - tileno, tpno, - startpack, - stoppack, - /***** j2k->image_info->tile[tileno].packet[startpack].start_pos, - j2k->image_info->tile[tileno].packet[stoppack].end_pos, */ - j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos, - j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos, - pprot); - - /***** prot_len = j2k->cstr_info->tile[tileno].packet[stoppack].end_pos + 1 - - j2k->cstr_info->tile[tileno].packet[startpack].start_pos; */ - prot_len = j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos + 1 - - j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos; - - /* - particular case: if this is the last header and the last packet, - then it is better to protect even the EOC marker - */ - /***** if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && - (stoppack == (j2k->cstr_info->num - 1))) */ - if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && - (tpno == (j2k->cstr_info->tile[tileno].num_tps - 1)) && - (stoppack == last_tp_pack)) - /* add the EOC len */ - prot_len += 2; - - /* let's add the EPBs */ - Psot += jpwl_epbs_add( - j2k, /* J2K handle */ - jwmarker, /* pointer to JPWL markers list */ - &jwmarker_num, /* pointer to the number of current markers */ - OPJ_TRUE, /* latest */ - OPJ_TRUE, /* packed */ - OPJ_FALSE, /* inside MH */ - &epb_index, /* pointer to EPB index */ - pprot, /* protection type */ - /***** (double) (j2k->cstr_info->tile[tileno].start_pos + sot_len + 2) + 0.0001,*/ /* position */ - (double) (j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2) + 0.0001, /* position */ - tileno, /* number of tile */ - 0, /* length of pre-data */ - prot_len /*4000*/ /* length of post-data */ - ); - } - - /* we can now check if the TPH EPB was really the last one */ - if (tph_epb && (epb_index == 1)) { - /* set the TPH EPB to be the last one in current header */ - tph_epb->Depb |= (unsigned char) ((OPJ_TRUE & 0x0001) << 6); - tph_epb = NULL; - } - - /* write back Psot */ - cio_seek(cio, Psotp); - cio_write(cio, Psot, 4); - + opj_event_msg(j2k->cinfo, EVT_INFO, + /***** "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", */ + "UEP EPBs: tile %02d, part %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", + /***** tileno, */ + tileno, tpno, + startpack, + stoppack, + /***** j2k->cstr_info->tile[tileno].packet[startpack].start_pos, */ + j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos, + /***** j2k->cstr_info->tile[tileno].packet[stoppack].end_pos, */ + j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos, + pprot); + + /***** prot_len = j2k->cstr_info->tile[tileno].packet[stoppack].end_pos + 1 - + j2k->cstr_info->tile[tileno].packet[startpack].start_pos; */ + prot_len = j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos + 1 - + j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos; + + /* + particular case: if this is the last header and the last packet, + then it is better to protect even the EOC marker + */ + /****** if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && + (stoppack == (j2k->cstr_info->num - 1))) */ + if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && + (tpno == (j2k->cstr_info->tile[tileno].num_tps - 1)) && + (stoppack == last_tp_pack)) + /* add the EOC len */ + prot_len += 2; + + /* let's add the EPBs */ + Psot += jpwl_epbs_add( + j2k, /* J2K handle */ + jwmarker, /* pointer to JPWL markers list */ + &jwmarker_num, /* pointer to the number of current markers */ + OPJ_FALSE, /* latest */ + OPJ_TRUE, /* packed */ + OPJ_FALSE, /* inside MH */ + &epb_index, /* pointer to EPB index */ + pprot, /* protection type */ + /****** (double) (j2k->cstr_info->tile[tileno].start_pos + sot_len + 2) + 0.0001, */ /* position */ + (double) (j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2) + 0.0001, /* position */ + tileno, /* number of tile */ + 0, /* length of pre-data */ + prot_len /*4000*/ /* length of post-data */ + ); + } + + startpack = packno; + pprot = j2k->cp->pprot[packspec++]; } - }; + /*printf("Tile %02d, pack %02d ==> %d\n", tileno, packno, pprot);*/ - /* reset the position */ - cio_seek(cio, ciopos); + } -} + /* we are at the end: print the remaining spec */ + stoppack = packno - 1; + if (pprot >= 0) { -void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) { + opj_event_msg(j2k->cinfo, EVT_INFO, + /**** "UEP EPBs: tile %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", */ + "UEP EPBs: tile %02d, part %02d, packs. %02d-%02d (B %d-%d), prot. %d\n", + /**** tileno, */ + tileno, tpno, + startpack, + stoppack, + /***** j2k->image_info->tile[tileno].packet[startpack].start_pos, + j2k->image_info->tile[tileno].packet[stoppack].end_pos, */ + j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos, + j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos, + pprot); + + /***** prot_len = j2k->cstr_info->tile[tileno].packet[stoppack].end_pos + 1 - + j2k->cstr_info->tile[tileno].packet[startpack].start_pos; */ + prot_len = j2k->cstr_info->tile[tileno].packet[first_tp_pack + stoppack].end_pos + 1 - + j2k->cstr_info->tile[tileno].packet[first_tp_pack + startpack].start_pos; + + /* + particular case: if this is the last header and the last packet, + then it is better to protect even the EOC marker + */ + /***** if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && + (stoppack == (j2k->cstr_info->num - 1))) */ + if ((tileno == ((j2k->cstr_info->tw * j2k->cstr_info->th) - 1)) && + (tpno == (j2k->cstr_info->tile[tileno].num_tps - 1)) && + (stoppack == last_tp_pack)) + /* add the EOC len */ + prot_len += 2; + + /* let's add the EPBs */ + Psot += jpwl_epbs_add( + j2k, /* J2K handle */ + jwmarker, /* pointer to JPWL markers list */ + &jwmarker_num, /* pointer to the number of current markers */ + OPJ_TRUE, /* latest */ + OPJ_TRUE, /* packed */ + OPJ_FALSE, /* inside MH */ + &epb_index, /* pointer to EPB index */ + pprot, /* protection type */ + /***** (double) (j2k->cstr_info->tile[tileno].start_pos + sot_len + 2) + 0.0001,*/ /* position */ + (double) (j2k->cstr_info->tile[tileno].tp[tpno].tp_start_pos + sot_len + 2) + 0.0001, /* position */ + tileno, /* number of tile */ + 0, /* length of pre-data */ + prot_len /*4000*/ /* length of post-data */ + ); + } + + /* we can now check if the TPH EPB was really the last one */ + if (tph_epb && (epb_index == 1)) { + /* set the TPH EPB to be the last one in current header */ + tph_epb->Depb |= (unsigned char) ((OPJ_TRUE & 0x0001) << 6); + tph_epb = NULL; + } + + /* write back Psot */ + cio_seek(cio, Psotp); + cio_write(cio, Psot, 4); - int mm; - unsigned long int old_size = j2k->cstr_info->codestream_size; - unsigned long int new_size = old_size; - int /*ciopos = cio_tell(cio),*/ soc_pos = j2k->cstr_info->main_head_start; - unsigned char *jpwl_buf, *orig_buf; - unsigned long int orig_pos; - double epbcoding_time = 0.0, esdcoding_time = 0.0; - (void)image; - - /* Order JPWL markers according to their wishlist position */ - qsort((void *) jwmarker, (size_t) jwmarker_num, sizeof (jpwl_marker_t), jpwl_markcomp); - - /* compute markers total size */ - for (mm = 0; mm < jwmarker_num; mm++) { - /*printf("%x, %d, %.10f, %d long\n", jwmarker[mm].id, jwmarker[mm].pos, - jwmarker[mm].dpos, jwmarker[mm].len);*/ - new_size += jwmarker[mm].len + 2; } - /* allocate a new buffer of proper size */ - if (!(jpwl_buf = (unsigned char *) opj_malloc((size_t) (new_size + soc_pos) * sizeof(unsigned char)))) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for JPWL codestream buffer\n"); - exit(1); - }; + }; - /* copy the jp2 part, if any */ - orig_buf = jpwl_buf; - memcpy(jpwl_buf, cio->buffer, soc_pos); - jpwl_buf += soc_pos; + /* reset the position */ + cio_seek(cio, ciopos); - /* cycle through markers */ - orig_pos = soc_pos + 0; /* start from the beginning */ - cio_seek(cio, soc_pos + 0); /* rewind the original */ - for (mm = 0; mm < jwmarker_num; mm++) { +} - /* - need to copy a piece of the original codestream - if there is such - */ - memcpy(jpwl_buf, cio_getbp(cio), jwmarker[mm].pos - orig_pos); - jpwl_buf += jwmarker[mm].pos - orig_pos; - orig_pos = jwmarker[mm].pos; - cio_seek(cio, orig_pos); +void jpwl_dump_marks(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image) +{ + + int mm; + unsigned long int old_size = j2k->cstr_info->codestream_size; + unsigned long int new_size = old_size; + int /*ciopos = cio_tell(cio),*/ soc_pos = j2k->cstr_info->main_head_start; + unsigned char *jpwl_buf, *orig_buf; + unsigned long int orig_pos; + double epbcoding_time = 0.0, esdcoding_time = 0.0; + (void)image; + + /* Order JPWL markers according to their wishlist position */ + qsort((void *) jwmarker, (size_t) jwmarker_num, sizeof (jpwl_marker_t), jpwl_markcomp); + + /* compute markers total size */ + for (mm = 0; mm < jwmarker_num; mm++) { + /*printf("%x, %d, %.10f, %d long\n", jwmarker[mm].id, jwmarker[mm].pos, + jwmarker[mm].dpos, jwmarker[mm].len);*/ + new_size += jwmarker[mm].len + 2; + } + + /* allocate a new buffer of proper size */ + if (!(jpwl_buf = (unsigned char *) opj_malloc((size_t) (new_size + soc_pos) * sizeof(unsigned char)))) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for JPWL codestream buffer\n"); + exit(1); + }; + + /* copy the jp2 part, if any */ + orig_buf = jpwl_buf; + memcpy(jpwl_buf, cio->buffer, soc_pos); + jpwl_buf += soc_pos; + + /* cycle through markers */ + orig_pos = soc_pos + 0; /* start from the beginning */ + cio_seek(cio, soc_pos + 0); /* rewind the original */ + for (mm = 0; mm < jwmarker_num; mm++) { - /* - then write down the marker - */ - switch (jwmarker[mm].id) { + /* + need to copy a piece of the original codestream + if there is such + */ + memcpy(jpwl_buf, cio_getbp(cio), jwmarker[mm].pos - orig_pos); + jpwl_buf += jwmarker[mm].pos - orig_pos; + orig_pos = jwmarker[mm].pos; + cio_seek(cio, orig_pos); - case J2K_MS_EPB: - jpwl_epb_write(j2k, jwmarker[mm].m.epbmark, jpwl_buf); - break; + /* + then write down the marker + */ + switch (jwmarker[mm].id) { - case J2K_MS_EPC: - jpwl_epc_write(j2k, jwmarker[mm].m.epcmark, jpwl_buf); - break; + case J2K_MS_EPB: + jpwl_epb_write(j2k, jwmarker[mm].m.epbmark, jpwl_buf); + break; - case J2K_MS_ESD: - jpwl_esd_write(j2k, jwmarker[mm].m.esdmark, jpwl_buf); - break; + case J2K_MS_EPC: + jpwl_epc_write(j2k, jwmarker[mm].m.epcmark, jpwl_buf); + break; - case J2K_MS_RED: - memset(jpwl_buf, 0, jwmarker[mm].len + 2); /* placeholder */ - break; + case J2K_MS_ESD: + jpwl_esd_write(j2k, jwmarker[mm].m.esdmark, jpwl_buf); + break; - default: - break; - }; + case J2K_MS_RED: + memset(jpwl_buf, 0, jwmarker[mm].len + 2); /* placeholder */ + break; - /* we update the markers struct */ - if (j2k->cstr_info) - j2k->cstr_info->marker[j2k->cstr_info->marknum - 1].pos = (jpwl_buf - orig_buf); - - /* we set the marker dpos to the new position in the JPWL codestream */ - jwmarker[mm].dpos = (double) (jpwl_buf - orig_buf); + default: + break; + }; - /* advance JPWL buffer position */ - jpwl_buf += jwmarker[mm].len + 2; + /* we update the markers struct */ + if (j2k->cstr_info) + j2k->cstr_info->marker[j2k->cstr_info->marknum - 1].pos = (jpwl_buf - orig_buf); - } + /* we set the marker dpos to the new position in the JPWL codestream */ + jwmarker[mm].dpos = (double) (jpwl_buf - orig_buf); - /* finish remaining original codestream */ - memcpy(jpwl_buf, cio_getbp(cio), old_size - (orig_pos - soc_pos)); - jpwl_buf += old_size - (orig_pos - soc_pos); - cio_seek(cio, soc_pos + old_size); - - /* - update info file based on added markers - */ - if (!jpwl_update_info(j2k, jwmarker, jwmarker_num)) - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not update OPJ cstr_info structure\n"); + /* advance JPWL buffer position */ + jpwl_buf += jwmarker[mm].len + 2; - /* now we need to repass some markers and fill their data fields */ - - /* first of all, DL and Pcrc in EPCs */ - for (mm = 0; mm < jwmarker_num; mm++) { + } - /* find the EPCs */ - if (jwmarker[mm].id == J2K_MS_EPC) { + /* finish remaining original codestream */ + memcpy(jpwl_buf, cio_getbp(cio), old_size - (orig_pos - soc_pos)); + jpwl_buf += old_size - (orig_pos - soc_pos); + cio_seek(cio, soc_pos + old_size); - int epc_pos = (int) jwmarker[mm].dpos, pp; - unsigned short int mycrc = 0x0000; + /* + update info file based on added markers + */ + if (!jpwl_update_info(j2k, jwmarker, jwmarker_num)) + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not update OPJ cstr_info structure\n"); - /* fix and fill the DL field */ - jwmarker[mm].m.epcmark->DL = new_size; - orig_buf[epc_pos + 6] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 24); - orig_buf[epc_pos + 7] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 16); - orig_buf[epc_pos + 8] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 8); - orig_buf[epc_pos + 9] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 0); + /* now we need to repass some markers and fill their data fields */ - /* compute the CRC field (excluding itself) */ - for (pp = 0; pp < 4; pp++) - jpwl_updateCRC16(&mycrc, orig_buf[epc_pos + pp]); - for (pp = 6; pp < (jwmarker[mm].len + 2); pp++) - jpwl_updateCRC16(&mycrc, orig_buf[epc_pos + pp]); + /* first of all, DL and Pcrc in EPCs */ + for (mm = 0; mm < jwmarker_num; mm++) { - /* fix and fill the CRC */ - jwmarker[mm].m.epcmark->Pcrc = mycrc; - orig_buf[epc_pos + 4] = (unsigned char) (jwmarker[mm].m.epcmark->Pcrc >> 8); - orig_buf[epc_pos + 5] = (unsigned char) (jwmarker[mm].m.epcmark->Pcrc >> 0); + /* find the EPCs */ + if (jwmarker[mm].id == J2K_MS_EPC) { + + int epc_pos = (int) jwmarker[mm].dpos, pp; + unsigned short int mycrc = 0x0000; + + /* fix and fill the DL field */ + jwmarker[mm].m.epcmark->DL = new_size; + orig_buf[epc_pos + 6] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 24); + orig_buf[epc_pos + 7] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 16); + orig_buf[epc_pos + 8] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 8); + orig_buf[epc_pos + 9] = (unsigned char) (jwmarker[mm].m.epcmark->DL >> 0); + + /* compute the CRC field (excluding itself) */ + for (pp = 0; pp < 4; pp++) + jpwl_updateCRC16(&mycrc, orig_buf[epc_pos + pp]); + for (pp = 6; pp < (jwmarker[mm].len + 2); pp++) + jpwl_updateCRC16(&mycrc, orig_buf[epc_pos + pp]); + + /* fix and fill the CRC */ + jwmarker[mm].m.epcmark->Pcrc = mycrc; + orig_buf[epc_pos + 4] = (unsigned char) (jwmarker[mm].m.epcmark->Pcrc >> 8); + orig_buf[epc_pos + 5] = (unsigned char) (jwmarker[mm].m.epcmark->Pcrc >> 0); - } } + } - /* then, sensitivity data in ESDs */ - esdcoding_time = opj_clock(); - for (mm = 0; mm < jwmarker_num; mm++) { + /* then, sensitivity data in ESDs */ + esdcoding_time = opj_clock(); + for (mm = 0; mm < jwmarker_num; mm++) { - /* find the ESDs */ - if (jwmarker[mm].id == J2K_MS_ESD) { + /* find the ESDs */ + if (jwmarker[mm].id == J2K_MS_ESD) { - /* remember that they are now in a new position (dpos) */ - int esd_pos = (int) jwmarker[mm].dpos; + /* remember that they are now in a new position (dpos) */ + int esd_pos = (int) jwmarker[mm].dpos; - jpwl_esd_fill(j2k, jwmarker[mm].m.esdmark, &orig_buf[esd_pos]); - - } + jpwl_esd_fill(j2k, jwmarker[mm].m.esdmark, &orig_buf[esd_pos]); } - esdcoding_time = opj_clock() - esdcoding_time; - if (j2k->cp->esd_on) - opj_event_msg(j2k->cinfo, EVT_INFO, "ESDs sensitivities computed in %f s\n", esdcoding_time); - /* finally, RS or CRC parity in EPBs */ - epbcoding_time = opj_clock(); - for (mm = 0; mm < jwmarker_num; mm++) { + } + esdcoding_time = opj_clock() - esdcoding_time; + if (j2k->cp->esd_on) + opj_event_msg(j2k->cinfo, EVT_INFO, "ESDs sensitivities computed in %f s\n", esdcoding_time); - /* find the EPBs */ - if (jwmarker[mm].id == J2K_MS_EPB) { - - /* remember that they are now in a new position (dpos) */ - int nn, accum_len; - - /* let's see how many EPBs are following this one, included itself */ - /* for this to work, we suppose that the markers are correctly ordered */ - /* and, overall, that they are in packed mode inside headers */ - accum_len = 0; - for (nn = mm; (nn < jwmarker_num) && (jwmarker[nn].id == J2K_MS_EPB) && - (jwmarker[nn].pos == jwmarker[mm].pos); nn++) - accum_len += jwmarker[nn].m.epbmark->Lepb + 2; - - /* fill the current (first) EPB with post-data starting from the computed position */ - jpwl_epb_fill(j2k, jwmarker[mm].m.epbmark, &orig_buf[(int) jwmarker[mm].dpos], - &orig_buf[(int) jwmarker[mm].dpos + accum_len]); - - /* fill the remaining EPBs in the header with post-data starting from the last position */ - for (nn = mm + 1; (nn < jwmarker_num) && (jwmarker[nn].id == J2K_MS_EPB) && - (jwmarker[nn].pos == jwmarker[mm].pos); nn++) - jpwl_epb_fill(j2k, jwmarker[nn].m.epbmark, &orig_buf[(int) jwmarker[nn].dpos], NULL); - - /* skip all the processed EPBs */ - mm = nn - 1; - } + /* finally, RS or CRC parity in EPBs */ + epbcoding_time = opj_clock(); + for (mm = 0; mm < jwmarker_num; mm++) { + + /* find the EPBs */ + if (jwmarker[mm].id == J2K_MS_EPB) { + + /* remember that they are now in a new position (dpos) */ + int nn, accum_len; + + /* let's see how many EPBs are following this one, included itself */ + /* for this to work, we suppose that the markers are correctly ordered */ + /* and, overall, that they are in packed mode inside headers */ + accum_len = 0; + for (nn = mm; (nn < jwmarker_num) && (jwmarker[nn].id == J2K_MS_EPB) && + (jwmarker[nn].pos == jwmarker[mm].pos); nn++) + accum_len += jwmarker[nn].m.epbmark->Lepb + 2; + + /* fill the current (first) EPB with post-data starting from the computed position */ + jpwl_epb_fill(j2k, jwmarker[mm].m.epbmark, &orig_buf[(int) jwmarker[mm].dpos], + &orig_buf[(int) jwmarker[mm].dpos + accum_len]); + /* fill the remaining EPBs in the header with post-data starting from the last position */ + for (nn = mm + 1; (nn < jwmarker_num) && (jwmarker[nn].id == J2K_MS_EPB) && + (jwmarker[nn].pos == jwmarker[mm].pos); nn++) + jpwl_epb_fill(j2k, jwmarker[nn].m.epbmark, &orig_buf[(int) jwmarker[nn].dpos], NULL); + + /* skip all the processed EPBs */ + mm = nn - 1; } - epbcoding_time = opj_clock() - epbcoding_time; - if (j2k->cp->epb_on) - opj_event_msg(j2k->cinfo, EVT_INFO, "EPBs redundancy computed in %f s\n", epbcoding_time); - - /* free original cio buffer and set it to the JPWL one */ - opj_free(cio->buffer); - cio->cinfo = cio->cinfo; /* no change */ - cio->openmode = cio->openmode; /* no change */ - cio->buffer = orig_buf; - cio->length = new_size + soc_pos; - cio->start = cio->buffer; - cio->end = cio->buffer + cio->length; - cio->bp = cio->buffer; - cio_seek(cio, soc_pos + new_size); + + } + epbcoding_time = opj_clock() - epbcoding_time; + if (j2k->cp->epb_on) + opj_event_msg(j2k->cinfo, EVT_INFO, "EPBs redundancy computed in %f s\n", epbcoding_time); + + /* free original cio buffer and set it to the JPWL one */ + opj_free(cio->buffer); + cio->cinfo = cio->cinfo; /* no change */ + cio->openmode = cio->openmode; /* no change */ + cio->buffer = orig_buf; + cio->length = new_size + soc_pos; + cio->start = cio->buffer; + cio->end = cio->buffer + cio->length; + cio->bp = cio->buffer; + cio_seek(cio, soc_pos + new_size); } -void j2k_read_epc(opj_j2k_t *j2k) { - unsigned long int DL, Lepcp, Pcrcp, l; - unsigned short int Lepc, Pcrc = 0x0000; - unsigned char Pepc; - opj_cio_t *cio = j2k->cio; - const char *ans1; - - /* Simply read the EPC parameters */ - Lepcp = cio_tell(cio); - Lepc = cio_read(cio, 2); - Pcrcp = cio_tell(cio); - cio_skip(cio, 2); /* Pcrc */ - DL = cio_read(cio, 4); - Pepc = cio_read(cio, 1); - - /* compute Pcrc */ - cio_seek(cio, Lepcp - 2); - - /* Marker */ - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - - /* Length */ - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - - /* skip Pcrc */ - cio_skip(cio, 2); - - /* read all remaining */ - for (l = 4; l < Lepc; l++) - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - - /* check Pcrc with the result */ - cio_seek(cio, Pcrcp); - ans1 = (Pcrc == (unsigned short int) cio_read(cio, 2)) ? "crc-ok" : "crc-ko"; - - /* now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "EPC(%u,%d): %s, DL=%d%s %s %s\n", - Lepcp - 2, - Lepc, - ans1, - DL, /* data length this EPC is referring to */ - (Pepc & 0x10) ? ", esd" : "", /* ESD is present */ - (Pepc & 0x20) ? ", red" : "", /* RED is present */ - (Pepc & 0x40) ? ", epb" : ""); /* EPB is present */ - - cio_seek(cio, Lepcp + Lepc); +void j2k_read_epc(opj_j2k_t *j2k) +{ + unsigned long int DL, Lepcp, Pcrcp, l; + unsigned short int Lepc, Pcrc = 0x0000; + unsigned char Pepc; + opj_cio_t *cio = j2k->cio; + const char *ans1; + + /* Simply read the EPC parameters */ + Lepcp = cio_tell(cio); + Lepc = cio_read(cio, 2); + Pcrcp = cio_tell(cio); + cio_skip(cio, 2); /* Pcrc */ + DL = cio_read(cio, 4); + Pepc = cio_read(cio, 1); + + /* compute Pcrc */ + cio_seek(cio, Lepcp - 2); + + /* Marker */ + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + + /* Length */ + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + + /* skip Pcrc */ + cio_skip(cio, 2); + + /* read all remaining */ + for (l = 4; l < Lepc; l++) + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + + /* check Pcrc with the result */ + cio_seek(cio, Pcrcp); + ans1 = (Pcrc == (unsigned short int) cio_read(cio, 2)) ? "crc-ok" : "crc-ko"; + + /* now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "EPC(%u,%d): %s, DL=%d%s %s %s\n", + Lepcp - 2, + Lepc, + ans1, + DL, /* data length this EPC is referring to */ + (Pepc & 0x10) ? ", esd" : "", /* ESD is present */ + (Pepc & 0x20) ? ", red" : "", /* RED is present */ + (Pepc & 0x40) ? ", epb" : ""); /* EPB is present */ + + cio_seek(cio, Lepcp + Lepc); } #if 0 -void j2k_write_epc(opj_j2k_t *j2k) { +void j2k_write_epc(opj_j2k_t *j2k) +{ - unsigned long int DL, Lepcp, Pcrcp, l; - unsigned short int Lepc, Pcrc; - unsigned char Pepc; + unsigned long int DL, Lepcp, Pcrcp, l; + unsigned short int Lepc, Pcrc; + unsigned char Pepc; - opj_cio_t *cio = j2k->cio; + opj_cio_t *cio = j2k->cio; - cio_write(cio, J2K_MS_EPC, 2); /* EPC */ - Lepcp = cio_tell(cio); - cio_skip(cio, 2); + cio_write(cio, J2K_MS_EPC, 2); /* EPC */ + Lepcp = cio_tell(cio); + cio_skip(cio, 2); - /* CRC-16 word of the EPC */ - Pcrc = 0x0000; /* initialize */ - Pcrcp = cio_tell(cio); - cio_write(cio, Pcrc, 2); /* Pcrc placeholder*/ + /* CRC-16 word of the EPC */ + Pcrc = 0x0000; /* initialize */ + Pcrcp = cio_tell(cio); + cio_write(cio, Pcrc, 2); /* Pcrc placeholder*/ - /* data length of the EPC protection domain */ - DL = 0x00000000; /* we leave this set to 0, as if the information is not available */ - cio_write(cio, DL, 4); /* DL */ + /* data length of the EPC protection domain */ + DL = 0x00000000; /* we leave this set to 0, as if the information is not available */ + cio_write(cio, DL, 4); /* DL */ - /* jpwl capabilities */ - Pepc = 0x00; - cio_write(cio, Pepc, 1); /* Pepc */ + /* jpwl capabilities */ + Pepc = 0x00; + cio_write(cio, Pepc, 1); /* Pepc */ - /* ID section */ - /* no ID's, as of now */ + /* ID section */ + /* no ID's, as of now */ - Lepc = (unsigned short) (cio_tell(cio) - Lepcp); - cio_seek(cio, Lepcp); - cio_write(cio, Lepc, 2); /* Lepc */ + Lepc = (unsigned short) (cio_tell(cio) - Lepcp); + cio_seek(cio, Lepcp); + cio_write(cio, Lepc, 2); /* Lepc */ - /* compute Pcrc */ - cio_seek(cio, Lepcp - 2); + /* compute Pcrc */ + cio_seek(cio, Lepcp - 2); - /* Marker */ - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + /* Marker */ + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - /* Length */ - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + /* Length */ + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - /* skip Pcrc */ - cio_skip(cio, 2); + /* skip Pcrc */ + cio_skip(cio, 2); - /* read all remaining */ - for (l = 4; l < Lepc; l++) - jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); + /* read all remaining */ + for (l = 4; l < Lepc; l++) + jpwl_updateCRC16(&Pcrc, (unsigned char) cio_read(cio, 1)); - /* fill Pcrc with the result */ - cio_seek(cio, Pcrcp); - cio_write(cio, Pcrc, 2); + /* fill Pcrc with the result */ + cio_seek(cio, Pcrcp); + cio_write(cio, Pcrc, 2); - cio_seek(cio, Lepcp + Lepc); + cio_seek(cio, Lepcp + Lepc); - /* marker struct update */ - j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, Lepcp - 2, Lepc + 2); + /* marker struct update */ + j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, Lepcp - 2, Lepc + 2); } #endif -void j2k_read_epb(opj_j2k_t *j2k) { - unsigned long int LDPepb, Pepb; - unsigned short int Lepb; - unsigned char Depb; - char str1[25] = ""; - opj_bool status; - static opj_bool first_in_tph = OPJ_TRUE; - int type, pre_len, post_len; - static unsigned char *redund = NULL; - - opj_cio_t *cio = j2k->cio; - - /* B/W = 45, RGB = 51 */ - /* SIZ SIZ_FIELDS SIZ_COMPS FOLLOWING_MARKER */ - int skipnum = 2 + 38 + 3 * j2k->cp->exp_comps + 2; - - if (j2k->cp->correct) { - - /* go back to EPB marker value */ - cio_seek(cio, cio_tell(cio) - 2); - - /* we need to understand where we are */ - if (j2k->state == J2K_STATE_MH) { - /* we are in MH */ - type = 0; /* MH */ - pre_len = skipnum; /* SOC+SIZ */ - post_len = -1; /* auto */ - - } else if ((j2k->state == J2K_STATE_TPH) && first_in_tph) { - /* we are in TPH */ - type = 1; /* TPH */ - pre_len = 12; /* SOC+SIZ */ - first_in_tph = OPJ_FALSE; - post_len = -1; /* auto */ +void j2k_read_epb(opj_j2k_t *j2k) +{ + unsigned long int LDPepb, Pepb; + unsigned short int Lepb; + unsigned char Depb; + char str1[25] = ""; + opj_bool status; + static opj_bool first_in_tph = OPJ_TRUE; + int type, pre_len, post_len; + static unsigned char *redund = NULL; + + opj_cio_t *cio = j2k->cio; + + /* B/W = 45, RGB = 51 */ + /* SIZ SIZ_FIELDS SIZ_COMPS FOLLOWING_MARKER */ + int skipnum = 2 + 38 + 3 * j2k->cp->exp_comps + 2; + + if (j2k->cp->correct) { + + /* go back to EPB marker value */ + cio_seek(cio, cio_tell(cio) - 2); + + /* we need to understand where we are */ + if (j2k->state == J2K_STATE_MH) { + /* we are in MH */ + type = 0; /* MH */ + pre_len = skipnum; /* SOC+SIZ */ + post_len = -1; /* auto */ + + } else if ((j2k->state == J2K_STATE_TPH) && first_in_tph) { + /* we are in TPH */ + type = 1; /* TPH */ + pre_len = 12; /* SOC+SIZ */ + first_in_tph = OPJ_FALSE; + post_len = -1; /* auto */ - } else { - /* we are elsewhere */ - type = 2; /* other */ - pre_len = 0; /* nada */ - post_len = -1; /* auto */ + } else { + /* we are elsewhere */ + type = 2; /* other */ + pre_len = 0; /* nada */ + post_len = -1; /* auto */ - } + } - /* call EPB corrector */ - /*printf("before %x, ", redund);*/ - status = jpwl_epb_correct(j2k, /* J2K decompressor handle */ - cio->bp, /* pointer to EPB in codestream buffer */ - type, /* EPB type: MH */ - pre_len, /* length of pre-data */ - post_len, /* length of post-data: -1 means auto */ - NULL, /* do everything auto */ - &redund - ); - /*printf("after %x\n", redund);*/ - - /* Read the (possibly corrected) EPB parameters */ - cio_skip(cio, 2); - Lepb = cio_read(cio, 2); - Depb = cio_read(cio, 1); - LDPepb = cio_read(cio, 4); - Pepb = cio_read(cio, 4); - - if (!status) { - - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL correction could not be performed\n"); - - /* advance to EPB endpoint */ - cio_skip(cio, Lepb + 2); - - return; - } + /* call EPB corrector */ + /*printf("before %x, ", redund);*/ + status = jpwl_epb_correct(j2k, /* J2K decompressor handle */ + cio->bp, /* pointer to EPB in codestream buffer */ + type, /* EPB type: MH */ + pre_len, /* length of pre-data */ + post_len, /* length of post-data: -1 means auto */ + NULL, /* do everything auto */ + &redund + ); + /*printf("after %x\n", redund);*/ + + /* Read the (possibly corrected) EPB parameters */ + cio_skip(cio, 2); + Lepb = cio_read(cio, 2); + Depb = cio_read(cio, 1); + LDPepb = cio_read(cio, 4); + Pepb = cio_read(cio, 4); - /* last in current header? */ - if (Depb & 0x40) { - redund = NULL; /* reset the pointer to L4 buffer */ - first_in_tph = OPJ_TRUE; - } + if (!status) { - /* advance to EPB endpoint */ - cio_skip(cio, Lepb - 11); + opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL correction could not be performed\n"); - } else { + /* advance to EPB endpoint */ + cio_skip(cio, Lepb + 2); - /* Simply read the EPB parameters */ - Lepb = cio_read(cio, 2); - Depb = cio_read(cio, 1); - LDPepb = cio_read(cio, 4); - Pepb = cio_read(cio, 4); - - /* What does Pepb tells us about the protection method? */ - if (((Pepb & 0xF0000000) >> 28) == 0) - sprintf(str1, "pred"); /* predefined */ - else if (((Pepb & 0xF0000000) >> 28) == 1) - sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */ - else if (((Pepb & 0xF0000000) >> 28) == 2) - sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */ - else if (Pepb == 0xFFFFFFFF) - sprintf(str1, "nometh"); /* RS mode */ - else - sprintf(str1, "unknown"); /* unknown */ - - /* Now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "EPB(%d): (%sl, %sp, %u), %lu, %s\n", - cio_tell(cio) - 13, - (Depb & 0x40) ? "" : "n", /* latest EPB or not? */ - (Depb & 0x80) ? "" : "n", /* packed or unpacked EPB? */ - (Depb & 0x3F), /* EPB index value */ - LDPepb, /*length of the data protected by the EPB */ - str1); /* protection method */ - - cio_skip(cio, Lepb - 11); + return; + } + + /* last in current header? */ + if (Depb & 0x40) { + redund = NULL; /* reset the pointer to L4 buffer */ + first_in_tph = OPJ_TRUE; } + + /* advance to EPB endpoint */ + cio_skip(cio, Lepb - 11); + + } else { + + /* Simply read the EPB parameters */ + Lepb = cio_read(cio, 2); + Depb = cio_read(cio, 1); + LDPepb = cio_read(cio, 4); + Pepb = cio_read(cio, 4); + + /* What does Pepb tells us about the protection method? */ + if (((Pepb & 0xF0000000) >> 28) == 0) + sprintf(str1, "pred"); /* predefined */ + else if (((Pepb & 0xF0000000) >> 28) == 1) + sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */ + else if (((Pepb & 0xF0000000) >> 28) == 2) + sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */ + else if (Pepb == 0xFFFFFFFF) + sprintf(str1, "nometh"); /* RS mode */ + else + sprintf(str1, "unknown"); /* unknown */ + + /* Now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "EPB(%d): (%sl, %sp, %u), %lu, %s\n", + cio_tell(cio) - 13, + (Depb & 0x40) ? "" : "n", /* latest EPB or not? */ + (Depb & 0x80) ? "" : "n", /* packed or unpacked EPB? */ + (Depb & 0x3F), /* EPB index value */ + LDPepb, /*length of the data protected by the EPB */ + str1); /* protection method */ + + cio_skip(cio, Lepb - 11); + } } -void j2k_write_epb(opj_j2k_t *j2k) { - unsigned long int LDPepb, Pepb, Lepbp; - unsigned short int Lepb; - unsigned char Depb; +void j2k_write_epb(opj_j2k_t *j2k) +{ + unsigned long int LDPepb, Pepb, Lepbp; + unsigned short int Lepb; + unsigned char Depb; - opj_cio_t *cio = j2k->cio; + opj_cio_t *cio = j2k->cio; - cio_write(cio, J2K_MS_EPB, 2); /* EPB */ - Lepbp = cio_tell(cio); - cio_skip(cio, 2); + cio_write(cio, J2K_MS_EPB, 2); /* EPB */ + Lepbp = cio_tell(cio); + cio_skip(cio, 2); - /* EPB style */ - Depb = 0x00; /* test */ - cio_write(cio, Depb, 1); /* Depb */ + /* EPB style */ + Depb = 0x00; /* test */ + cio_write(cio, Depb, 1); /* Depb */ - /* length of the data to be protected by this EPB */ - LDPepb = 0x00000000; /* test */ - cio_write(cio, LDPepb, 4); /* LDPepb */ + /* length of the data to be protected by this EPB */ + LDPepb = 0x00000000; /* test */ + cio_write(cio, LDPepb, 4); /* LDPepb */ - /* next error correction tool */ - Pepb = 0x00000000; /* test */ - cio_write(cio, Pepb, 4); /* Pepb */ + /* next error correction tool */ + Pepb = 0x00000000; /* test */ + cio_write(cio, Pepb, 4); /* Pepb */ - /* EPB data */ - /* no data, as of now */ + /* EPB data */ + /* no data, as of now */ - Lepb = (unsigned short) (cio_tell(cio) - Lepbp); - cio_seek(cio, Lepbp); - cio_write(cio, Lepb, 2); /* Lepb */ + Lepb = (unsigned short) (cio_tell(cio) - Lepbp); + cio_seek(cio, Lepbp); + cio_write(cio, Lepb, 2); /* Lepb */ - cio_seek(cio, Lepbp + Lepb); + cio_seek(cio, Lepbp + Lepb); - /* marker struct update */ - j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, Lepbp - 2, Lepb + 2); + /* marker struct update */ + j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, Lepbp - 2, Lepb + 2); } -void j2k_read_esd(opj_j2k_t *j2k) { - unsigned short int Lesd, Cesd; - unsigned char Pesd; +void j2k_read_esd(opj_j2k_t *j2k) +{ + unsigned short int Lesd, Cesd; + unsigned char Pesd; - int cesdsize = (j2k->image->numcomps >= 257) ? 2 : 1; + int cesdsize = (j2k->image->numcomps >= 257) ? 2 : 1; - char str1[4][4] = {"p", "br", "pr", "res"}; - char str2[8][8] = {"res", "mse", "mse-r", "psnr", "psnr-i", "maxerr", "tse", "res"}; - - opj_cio_t *cio = j2k->cio; + char str1[4][4] = {"p", "br", "pr", "res"}; + char str2[8][8] = {"res", "mse", "mse-r", "psnr", "psnr-i", "maxerr", "tse", "res"}; - /* Simply read the ESD parameters */ - Lesd = cio_read(cio, 2); - Cesd = cio_read(cio, cesdsize); - Pesd = cio_read(cio, 1); + opj_cio_t *cio = j2k->cio; - /* Now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "ESD(%d): c%d, %s, %s, %s, %s, %s\n", - cio_tell(cio) - (5 + cesdsize), - Cesd, /* component number for this ESD */ - str1[(Pesd & (unsigned char) 0xC0) >> 6], /* addressing mode */ - str2[(Pesd & (unsigned char) 0x38) >> 3], /* sensitivity type */ - ((Pesd & (unsigned char) 0x04) >> 2) ? "2Bs" : "1Bs", - ((Pesd & (unsigned char) 0x02) >> 1) ? "4Ba" : "2Ba", - (Pesd & (unsigned char) 0x01) ? "avgc" : ""); - - cio_skip(cio, Lesd - (3 + cesdsize)); + /* Simply read the ESD parameters */ + Lesd = cio_read(cio, 2); + Cesd = cio_read(cio, cesdsize); + Pesd = cio_read(cio, 1); + + /* Now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "ESD(%d): c%d, %s, %s, %s, %s, %s\n", + cio_tell(cio) - (5 + cesdsize), + Cesd, /* component number for this ESD */ + str1[(Pesd & (unsigned char) 0xC0) >> 6], /* addressing mode */ + str2[(Pesd & (unsigned char) 0x38) >> 3], /* sensitivity type */ + ((Pesd & (unsigned char) 0x04) >> 2) ? "2Bs" : "1Bs", + ((Pesd & (unsigned char) 0x02) >> 1) ? "4Ba" : "2Ba", + (Pesd & (unsigned char) 0x01) ? "avgc" : ""); + + cio_skip(cio, Lesd - (3 + cesdsize)); } -void j2k_read_red(opj_j2k_t *j2k) { - unsigned short int Lred; - unsigned char Pred; - char str1[4][4] = {"p", "br", "pr", "res"}; - - opj_cio_t *cio = j2k->cio; +void j2k_read_red(opj_j2k_t *j2k) +{ + unsigned short int Lred; + unsigned char Pred; + char str1[4][4] = {"p", "br", "pr", "res"}; - /* Simply read the RED parameters */ - Lred = cio_read(cio, 2); - Pred = cio_read(cio, 1); + opj_cio_t *cio = j2k->cio; - /* Now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "RED(%d): %s, %dc, %s, %s\n", - cio_tell(cio) - 5, - str1[(Pred & (unsigned char) 0xC0) >> 6], /* addressing mode */ - (Pred & (unsigned char) 0x38) >> 3, /* corruption level */ - ((Pred & (unsigned char) 0x02) >> 1) ? "4Ba" : "2Ba", /* address range */ - (Pred & (unsigned char) 0x01) ? "errs" : "free"); /* error free? */ - - cio_skip(cio, Lred - 3); + /* Simply read the RED parameters */ + Lred = cio_read(cio, 2); + Pred = cio_read(cio, 1); + + /* Now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "RED(%d): %s, %dc, %s, %s\n", + cio_tell(cio) - 5, + str1[(Pred & (unsigned char) 0xC0) >> 6], /* addressing mode */ + (Pred & (unsigned char) 0x38) >> 3, /* corruption level */ + ((Pred & (unsigned char) 0x02) >> 1) ? "4Ba" : "2Ba", /* address range */ + (Pred & (unsigned char) 0x01) ? "errs" : "free"); /* error free? */ + + cio_skip(cio, Lred - 3); } -opj_bool jpwl_check_tile(opj_j2k_t *j2k, opj_tcd_t *tcd, int tileno) { +opj_bool jpwl_check_tile(opj_j2k_t *j2k, opj_tcd_t *tcd, int tileno) +{ #ifdef oerhgierhgvhreit4u - /* - we navigate through the tile and find possible invalid parameters: - this saves a lot of crashes!!!!! - */ - int compno, resno, precno, /*layno,*/ bandno, blockno; - int numprecincts, numblocks; - - /* this is the selected tile */ - opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[tileno]); - - /* will keep the component */ - opj_tcd_tilecomp_t *comp = NULL; - - /* will keep the resolution */ - opj_tcd_resolution_t *res; - - /* will keep the subband */ - opj_tcd_band_t *band; - - /* will keep the precinct */ - opj_tcd_precinct_t *prec; - - /* will keep the codeblock */ - opj_tcd_cblk_t *block; - - /* check all tile components */ - for (compno = 0; compno < tile->numcomps; compno++) { - comp = &(tile->comps[compno]); - - /* check all component resolutions */ - for (resno = 0; resno < comp->numresolutions; resno++) { - res = &(comp->resolutions[resno]); - numprecincts = res->pw * res->ph; - - /* check all the subbands */ - for (bandno = 0; bandno < res->numbands; bandno++) { - band = &(res->bands[bandno]); - - /* check all the precincts */ - for (precno = 0; precno < numprecincts; precno++) { - prec = &(band->precincts[precno]); - numblocks = prec->ch * prec->cw; - - /* check all the codeblocks */ - for (blockno = 0; blockno < numblocks; blockno++) { - block = &(prec->cblks[blockno]); - - /* x-origin is invalid */ - if ((block->x0 < prec->x0) || (block->x0 > prec->x1)) { - opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, - "JPWL: wrong x-cord of block origin %d => x-prec is (%d, %d)\n", - block->x0, prec->x0, prec->x1); - if (!JPWL_ASSUME) - return OPJ_FALSE; - }; - } - } - } + /* + we navigate through the tile and find possible invalid parameters: + this saves a lot of crashes!!!!! + */ + int compno, resno, precno, /*layno,*/ bandno, blockno; + int numprecincts, numblocks; + + /* this is the selected tile */ + opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[tileno]); + + /* will keep the component */ + opj_tcd_tilecomp_t *comp = NULL; + + /* will keep the resolution */ + opj_tcd_resolution_t *res; + + /* will keep the subband */ + opj_tcd_band_t *band; + + /* will keep the precinct */ + opj_tcd_precinct_t *prec; + + /* will keep the codeblock */ + opj_tcd_cblk_t *block; + + /* check all tile components */ + for (compno = 0; compno < tile->numcomps; compno++) { + comp = &(tile->comps[compno]); + + /* check all component resolutions */ + for (resno = 0; resno < comp->numresolutions; resno++) { + res = &(comp->resolutions[resno]); + numprecincts = res->pw * res->ph; + + /* check all the subbands */ + for (bandno = 0; bandno < res->numbands; bandno++) { + band = &(res->bands[bandno]); + + /* check all the precincts */ + for (precno = 0; precno < numprecincts; precno++) { + prec = &(band->precincts[precno]); + numblocks = prec->ch * prec->cw; + + /* check all the codeblocks */ + for (blockno = 0; blockno < numblocks; blockno++) { + block = &(prec->cblks[blockno]); + + /* x-origin is invalid */ + if ((block->x0 < prec->x0) || (block->x0 > prec->x1)) { + opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + "JPWL: wrong x-cord of block origin %d => x-prec is (%d, %d)\n", + block->x0, prec->x0, prec->x1); + if (!JPWL_ASSUME) + return OPJ_FALSE; + }; + } } + } } + } #else - (void)j2k; - (void)tcd; - (void)tileno; + (void)j2k; + (void)tcd; + (void)tileno; #endif - return OPJ_TRUE; + return OPJ_TRUE; } /*@}*/ @@ -1328,52 +1338,55 @@ opj_bool jpwl_check_tile(opj_j2k_t *j2k, opj_tcd_t *tcd, int tileno) { /** @name Local static functions */ /*@{*/ -void j2k_read_sec(opj_j2k_t *j2k) { - unsigned short int Lsec; - - opj_cio_t *cio = j2k->cio; +void j2k_read_sec(opj_j2k_t *j2k) +{ + unsigned short int Lsec; - /* Simply read the SEC length */ - Lsec = cio_read(cio, 2); + opj_cio_t *cio = j2k->cio; - /* Now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "SEC(%d)\n", - cio_tell(cio) - 2 - ); + /* Simply read the SEC length */ + Lsec = cio_read(cio, 2); - cio_skip(cio, Lsec - 2); + /* Now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "SEC(%d)\n", + cio_tell(cio) - 2 + ); + + cio_skip(cio, Lsec - 2); } -void j2k_write_sec(opj_j2k_t *j2k) { - unsigned short int Lsec = 24; - int i; +void j2k_write_sec(opj_j2k_t *j2k) +{ + unsigned short int Lsec = 24; + int i; - opj_cio_t *cio = j2k->cio; + opj_cio_t *cio = j2k->cio; - cio_write(cio, J2K_MS_SEC, 2); /* SEC */ - cio_write(cio, Lsec, 2); + cio_write(cio, J2K_MS_SEC, 2); /* SEC */ + cio_write(cio, Lsec, 2); - /* write dummy data */ - for (i = 0; i < Lsec - 2; i++) - cio_write(cio, 0, 1); + /* write dummy data */ + for (i = 0; i < Lsec - 2; i++) + cio_write(cio, 0, 1); } -void j2k_read_insec(opj_j2k_t *j2k) { - unsigned short int Linsec; - - opj_cio_t *cio = j2k->cio; +void j2k_read_insec(opj_j2k_t *j2k) +{ + unsigned short int Linsec; - /* Simply read the INSEC length */ - Linsec = cio_read(cio, 2); + opj_cio_t *cio = j2k->cio; - /* Now we write them to screen */ - opj_event_msg(j2k->cinfo, EVT_INFO, - "INSEC(%d)\n", - cio_tell(cio) - 2 - ); + /* Simply read the INSEC length */ + Linsec = cio_read(cio, 2); + + /* Now we write them to screen */ + opj_event_msg(j2k->cinfo, EVT_INFO, + "INSEC(%d)\n", + cio_tell(cio) - 2 + ); - cio_skip(cio, Linsec - 2); + cio_skip(cio, Linsec - 2); } diff --git a/src/lib/openjpwl/jpwl.h b/src/lib/openjpwl/jpwl.h index ca0ee0a1..fed92cb2 100644 --- a/src/lib/openjpwl/jpwl.h +++ b/src/lib/openjpwl/jpwl.h @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -60,154 +60,154 @@ Assume a basic codestream structure, so you can resort better from uncorrected e EPB (Error Protection Block) Marker segment */ typedef struct jpwl_epb_ms { - /**@name Private fields set by epb_create */ - /*@{*/ - /** is the latest in header? */ - opj_bool latest; - /** is it in packed mode? */ - opj_bool packed; - /** TH where this marker has been placed (-1 means MH) */ - int tileno; - /** index in current header (0-63) */ - unsigned char index; - /** error protection method [-1=absent 0=none 1=predefined 16=CRC-16 32=CRC-32 37-128=RS] */ - int hprot; - /** message word length of pre-data */ - int k_pre; - /** code word length of pre-data */ - int n_pre; - /** length of pre-data */ - int pre_len; - /** message word length of post-data */ - int k_post; - /** code word length of post-data */ - int n_post; - /** length of post-data */ - int post_len; - /*@}*/ - /**@name Marker segment fields */ - /*@{*/ - /** two bytes for the length of EPB MS, exluding the marker itself (11 to 65535 bytes) */ - unsigned short int Lepb; - /** single byte for the style */ - unsigned char Depb; - /** four bytes, from 0 to 2^31-1 */ - unsigned long int LDPepb; - /** four bytes, next error management method */ - unsigned long int Pepb; - /** EPB data, variable size */ - unsigned char *data; - /*@}*/ + /**@name Private fields set by epb_create */ + /*@{*/ + /** is the latest in header? */ + opj_bool latest; + /** is it in packed mode? */ + opj_bool packed; + /** TH where this marker has been placed (-1 means MH) */ + int tileno; + /** index in current header (0-63) */ + unsigned char index; + /** error protection method [-1=absent 0=none 1=predefined 16=CRC-16 32=CRC-32 37-128=RS] */ + int hprot; + /** message word length of pre-data */ + int k_pre; + /** code word length of pre-data */ + int n_pre; + /** length of pre-data */ + int pre_len; + /** message word length of post-data */ + int k_post; + /** code word length of post-data */ + int n_post; + /** length of post-data */ + int post_len; + /*@}*/ + /**@name Marker segment fields */ + /*@{*/ + /** two bytes for the length of EPB MS, exluding the marker itself (11 to 65535 bytes) */ + unsigned short int Lepb; + /** single byte for the style */ + unsigned char Depb; + /** four bytes, from 0 to 2^31-1 */ + unsigned long int LDPepb; + /** four bytes, next error management method */ + unsigned long int Pepb; + /** EPB data, variable size */ + unsigned char *data; + /*@}*/ } jpwl_epb_ms_t; /** EPC (Error Protection Capability) Marker segment */ typedef struct jpwl_epc_ms { - /** is ESD active? */ - opj_bool esd_on; - /** is RED active? */ - opj_bool red_on; - /** is EPB active? */ - opj_bool epb_on; - /** are informative techniques active? */ - opj_bool info_on; - /**@name Marker segment fields */ - /*@{*/ - /** two bytes for the length of EPC MS, exluding the marker itself (9 to 65535 bytes) */ - unsigned short int Lepc; - /** two bytes, CRC for the EPC, excluding Pcrc itself */ - unsigned short int Pcrc; - /** four bytes, the codestream length from SOC to EOC */ - unsigned long int DL; - /** one byte, signals JPWL techniques adoption */ - unsigned char Pepc; - /** EPC data, variable length */ - unsigned char *data; - /*@}*/ + /** is ESD active? */ + opj_bool esd_on; + /** is RED active? */ + opj_bool red_on; + /** is EPB active? */ + opj_bool epb_on; + /** are informative techniques active? */ + opj_bool info_on; + /**@name Marker segment fields */ + /*@{*/ + /** two bytes for the length of EPC MS, exluding the marker itself (9 to 65535 bytes) */ + unsigned short int Lepc; + /** two bytes, CRC for the EPC, excluding Pcrc itself */ + unsigned short int Pcrc; + /** four bytes, the codestream length from SOC to EOC */ + unsigned long int DL; + /** one byte, signals JPWL techniques adoption */ + unsigned char Pepc; + /** EPC data, variable length */ + unsigned char *data; + /*@}*/ } jpwl_epc_ms_t; /** ESD (Error Sensitivity Descriptor) Marker segment */ typedef struct jpwl_esd_ms { - /** codestream addressing mode [0=packet, 1=byte range, 2=packet range, 3=reserved] */ - unsigned char addrm; - /** size of codestream addresses [2/4 bytes] */ - unsigned char ad_size; - /** type of sensitivity - [0=relative error, 1=MSE, 2=MSE reduction, 3=PSNR, 4=PSNR increment, - 5=MAXERR (absolute peak error), 6=TSE (total squared error), 7=reserved */ - unsigned char senst; - /** size of sensitivity data (1/2 bytes) */ - unsigned char se_size; - /**@name Marker segment fields */ - /*@{*/ - /** two bytes for the length of ESD MS, exluding the marker itself (4 to 65535 bytes) */ - unsigned short int Lesd; - /** two bytes, component of error sensitivity */ - unsigned short int Cesd; - /** one byte, signals JPWL techniques adoption */ - unsigned char Pesd; - /** ESD data, variable length */ - unsigned char *data; - /*@}*/ - /**@name Fields set by esd_create (only internal use) */ - /*@{*/ - /** number of components in the image */ - int numcomps; - /** tile where this marker has been placed (-1 means MH) */ - int tileno; - /** number of sensitivity values */ - unsigned long int svalnum; - /** size of a single sensitivity pair (address+value) */ - size_t sensval_size; - /*@}*/ + /** codestream addressing mode [0=packet, 1=byte range, 2=packet range, 3=reserved] */ + unsigned char addrm; + /** size of codestream addresses [2/4 bytes] */ + unsigned char ad_size; + /** type of sensitivity + [0=relative error, 1=MSE, 2=MSE reduction, 3=PSNR, 4=PSNR increment, + 5=MAXERR (absolute peak error), 6=TSE (total squared error), 7=reserved */ + unsigned char senst; + /** size of sensitivity data (1/2 bytes) */ + unsigned char se_size; + /**@name Marker segment fields */ + /*@{*/ + /** two bytes for the length of ESD MS, exluding the marker itself (4 to 65535 bytes) */ + unsigned short int Lesd; + /** two bytes, component of error sensitivity */ + unsigned short int Cesd; + /** one byte, signals JPWL techniques adoption */ + unsigned char Pesd; + /** ESD data, variable length */ + unsigned char *data; + /*@}*/ + /**@name Fields set by esd_create (only internal use) */ + /*@{*/ + /** number of components in the image */ + int numcomps; + /** tile where this marker has been placed (-1 means MH) */ + int tileno; + /** number of sensitivity values */ + unsigned long int svalnum; + /** size of a single sensitivity pair (address+value) */ + size_t sensval_size; + /*@}*/ } jpwl_esd_ms_t; /** RED (Residual Error Descriptor) Marker segment */ typedef struct jpwl_red_ms { - /** two bytes for the length of RED MS, exluding the marker itself (3 to 65535 bytes) */ - unsigned short int Lred; - /** one byte, signals JPWL techniques adoption */ - unsigned char Pred; - /** RED data, variable length */ - unsigned char *data; + /** two bytes for the length of RED MS, exluding the marker itself (3 to 65535 bytes) */ + unsigned short int Lred; + /** one byte, signals JPWL techniques adoption */ + unsigned char Pred; + /** RED data, variable length */ + unsigned char *data; } jpwl_red_ms_t; /** Structure used to store JPWL markers temporary position and readyness */ typedef struct jpwl_marker { - /** marker value (J2K_MS_EPC, etc.) */ - int id; - /** union keeping the pointer to the real marker struct */ - union jpwl_marks { - /** pointer to EPB marker */ - jpwl_epb_ms_t *epbmark; - /** pointer to EPC marker */ - jpwl_epc_ms_t *epcmark; - /** pointer to ESD marker */ - jpwl_esd_ms_t *esdmark; - /** pointer to RED marker */ - jpwl_red_ms_t *redmark; - } m; - /** position where the marker should go, in the pre-JPWL codestream */ - unsigned long int pos; - /** same as before, only written as a double, so we can sort it better */ - double dpos; - /** length of the marker segment (marker excluded) */ - unsigned short int len; - /** the marker length is ready or not? */ - opj_bool len_ready; - /** the marker position is ready or not? */ - opj_bool pos_ready; - /** the marker parameters are ready or not? */ - opj_bool parms_ready; - /** are the written data ready or not */ - opj_bool data_ready; + /** marker value (J2K_MS_EPC, etc.) */ + int id; + /** union keeping the pointer to the real marker struct */ + union jpwl_marks { + /** pointer to EPB marker */ + jpwl_epb_ms_t *epbmark; + /** pointer to EPC marker */ + jpwl_epc_ms_t *epcmark; + /** pointer to ESD marker */ + jpwl_esd_ms_t *esdmark; + /** pointer to RED marker */ + jpwl_red_ms_t *redmark; + } m; + /** position where the marker should go, in the pre-JPWL codestream */ + unsigned long int pos; + /** same as before, only written as a double, so we can sort it better */ + double dpos; + /** length of the marker segment (marker excluded) */ + unsigned short int len; + /** the marker length is ready or not? */ + opj_bool len_ready; + /** the marker position is ready or not? */ + opj_bool pos_ready; + /** the marker parameters are ready or not? */ + opj_bool parms_ready; + /** are the written data ready or not */ + opj_bool data_ready; } jpwl_marker_t; /** @@ -288,7 +288,7 @@ void j2k_read_red(opj_j2k_t *j2k); @return returns the freshly created EPB */ jpwl_epb_ms_t *jpwl_epb_create(opj_j2k_t *j2k, opj_bool latest, opj_bool packed, int tileno, int idx, int hprot, - unsigned long int pre_len, unsigned long int post_len); + unsigned long int pre_len, unsigned long int post_len); /** add a number of EPB marker segments @param j2k J2K compressor handle @@ -306,9 +306,9 @@ jpwl_epb_ms_t *jpwl_epb_create(opj_j2k_t *j2k, opj_bool latest, opj_bool packed, @return returns the length of all added markers */ int jpwl_epbs_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num, - opj_bool latest, opj_bool packed, opj_bool insideMH, int *idx, int hprot, - double place_pos, int tileno, - unsigned long int pre_len, unsigned long int post_len); + opj_bool latest, opj_bool packed, opj_bool insideMH, int *idx, int hprot, + double place_pos, int tileno, + unsigned long int pre_len, unsigned long int post_len); /** add a number of ESD marker segments @param j2k J2K compressor handle @@ -324,16 +324,16 @@ int jpwl_epbs_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num, @return returns the length of all added markers */ int jpwl_esds_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num, - int comps, unsigned char addrm, unsigned char ad_size, - unsigned char senst, unsigned char se_size, - double place_pos, int tileno); - + int comps, unsigned char addrm, unsigned char ad_size, + unsigned char senst, unsigned char se_size, + double place_pos, int tileno); + /** updates the information structure by modifying the positions and lengths @param j2k J2K compressor handle @param jwmarker pointer to JPWL markers list @param jwmarker_num number of JPWL markers @return returns true in case of success -*/ +*/ opj_bool jpwl_update_info(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int jwmarker_num); @@ -360,7 +360,7 @@ opj_bool jpwl_correct(opj_j2k_t *j2k); @return returns true if correction could be successfully performed */ opj_bool jpwl_epb_correct(opj_j2k_t *j2k, unsigned char *buffer, int type, int pre_len, int post_len, int *conn, - unsigned char **L4_bufp); + unsigned char **L4_bufp); /** check that a tile and its children have valid data @param j2k J2K decompressor handle diff --git a/src/lib/openjpwl/jpwl_lib.c b/src/lib/openjpwl/jpwl_lib.c index 772c8db3..a6409668 100644 --- a/src/lib/openjpwl/jpwl_lib.c +++ b/src/lib/openjpwl/jpwl_lib.c @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * @@ -46,7 +46,7 @@ #define MAX_V2 131040.0 /** conversion between a double precision floating point -number and the corresponding pseudo-floating point used +number and the corresponding pseudo-floating point used to represent sensitivity values @param V the double precision value @param bytes the number of bytes of the representation @@ -54,1750 +54,1765 @@ to represent sensitivity values */ unsigned short int jpwl_double_to_pfp(double V, int bytes); -/** conversion between a pseudo-floating point used +/** conversion between a pseudo-floating point used to represent sensitivity values and the corresponding -double precision floating point number +double precision floating point number @param em the pseudo-floating point value (cast accordingly) @param bytes the number of bytes of the representation @return the double precision value */ double jpwl_pfp_to_double(unsigned short int em, int bytes); - /*-------------------------------------------------------------*/ +/*-------------------------------------------------------------*/ int jpwl_markcomp(const void *arg1, const void *arg2) { - /* Compare the two markers' positions */ - double diff = (((jpwl_marker_t *) arg1)->dpos - ((jpwl_marker_t *) arg2)->dpos); - - if (diff == 0.0) - return (0); - else if (diff < 0) - return (-1); - else - return (+1); + /* Compare the two markers' positions */ + double diff = (((jpwl_marker_t *) arg1)->dpos - ((jpwl_marker_t *) arg2)->dpos); + + if (diff == 0.0) + return (0); + else if (diff < 0) + return (-1); + else + return (+1); } int jpwl_epbs_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num, - opj_bool latest, opj_bool packed, opj_bool insideMH, int *idx, int hprot, - double place_pos, int tileno, - unsigned long int pre_len, unsigned long int post_len) { - - jpwl_epb_ms_t *epb_mark = NULL; - - int k_pre, k_post, n_pre, n_post; - - unsigned long int L1, L2, dL4, max_postlen, epbs_len = 0; - - /* We find RS(n,k) for EPB parms and pre-data, if any */ - if (insideMH && (*idx == 0)) { - /* First EPB in MH */ - k_pre = 64; - n_pre = 160; - } else if (!insideMH && (*idx == 0)) { - /* First EPB in TH */ - k_pre = 25; - n_pre = 80; - } else { - /* Following EPBs in MH or TH */ - k_pre = 13; - n_pre = 40; - }; - - /* Find lengths, Figs. B3 and B4 */ - /* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */ - L1 = pre_len + 13; - - /* size of pre-data redundancy */ - /* (redundancy per codeword) * (number of codewords, rounded up) */ - L2 = (n_pre - k_pre) * (unsigned long int) ceil((double) L1 / (double) k_pre); - - /* Find protection type for post data and its associated redundancy field length*/ - if ((hprot == 16) || (hprot == 32)) { - /* there is a CRC for post-data */ - k_post = post_len; - n_post = post_len + (hprot >> 3); - /*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */ - - } else if ((hprot >= 37) && (hprot <= 128)) { - /* there is a RS for post-data */ - k_post = 32; - n_post = hprot; - - } else { - /* Use predefined codes */ - n_post = n_pre; - k_post = k_pre; - }; - - /* Create the EPB(s) */ - while (post_len > 0) { - - /* maximum postlen in order to respect EPB size - (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms)*/ - /* (message word size) * (number of containable parity words) */ - max_postlen = k_post * (unsigned long int) floor((double) JPWL_MAXIMUM_EPB_ROOM / (double) (n_post - k_post)); - - /* maximum postlen in order to respect EPB size */ - if (*idx == 0) - /* (we use (JPWL_MAXIMUM_EPB_ROOM - L2) instead of 65535 for keeping room for EPB parms + pre-data) */ - /* (message word size) * (number of containable parity words) */ - max_postlen = k_post * (unsigned long int) floor((double) (JPWL_MAXIMUM_EPB_ROOM - L2) / (double) (n_post - k_post)); - - else - /* (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms) */ - /* (message word size) * (number of containable parity words) */ - max_postlen = k_post * (unsigned long int) floor((double) JPWL_MAXIMUM_EPB_ROOM / (double) (n_post - k_post)); - - /* null protection case */ - /* the max post length can be as large as the LDPepb field can host */ - if (hprot == 0) - max_postlen = INT_MAX; - - /* length to use */ - dL4 = min(max_postlen, post_len); - - if ((epb_mark = jpwl_epb_create( - j2k, /* this encoder handle */ - latest ? (dL4 < max_postlen) : OPJ_FALSE, /* is it the latest? */ - packed, /* is it packed? */ - tileno, /* we are in TPH */ - *idx, /* its index */ - hprot, /* protection type parameters of following data */ - 0, /* pre-data: nothing for now */ - dL4 /* post-data: the stub computed previously */ - ))) { - - /* Add this marker to the 'insertanda' list */ - if (*jwmarker_num < JPWL_MAX_NO_MARKERS) { - jwmarker[*jwmarker_num].id = J2K_MS_EPB; /* its type */ - jwmarker[*jwmarker_num].m.epbmark = epb_mark; /* the EPB */ - jwmarker[*jwmarker_num].pos = (int) place_pos; /* after SOT */ - jwmarker[*jwmarker_num].dpos = place_pos + 0.0000001 * (double)(*idx); /* not very first! */ - jwmarker[*jwmarker_num].len = epb_mark->Lepb; /* its length */ - jwmarker[*jwmarker_num].len_ready = OPJ_TRUE; /* ready */ - jwmarker[*jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ - jwmarker[*jwmarker_num].parms_ready = OPJ_TRUE; /* ready */ - jwmarker[*jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ - (*jwmarker_num)++; - } - - /* increment epb index */ - (*idx)++; - - /* decrease postlen */ - post_len -= dL4; - - /* increase the total length of EPBs */ - epbs_len += epb_mark->Lepb + 2; - - } else { - /* ooops, problems */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB for UEP in tile %d\n", tileno); - }; - } - - return epbs_len; + opj_bool latest, opj_bool packed, opj_bool insideMH, int *idx, int hprot, + double place_pos, int tileno, + unsigned long int pre_len, unsigned long int post_len) +{ + + jpwl_epb_ms_t *epb_mark = NULL; + + int k_pre, k_post, n_pre, n_post; + + unsigned long int L1, L2, dL4, max_postlen, epbs_len = 0; + + /* We find RS(n,k) for EPB parms and pre-data, if any */ + if (insideMH && (*idx == 0)) { + /* First EPB in MH */ + k_pre = 64; + n_pre = 160; + } else if (!insideMH && (*idx == 0)) { + /* First EPB in TH */ + k_pre = 25; + n_pre = 80; + } else { + /* Following EPBs in MH or TH */ + k_pre = 13; + n_pre = 40; + }; + + /* Find lengths, Figs. B3 and B4 */ + /* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */ + L1 = pre_len + 13; + + /* size of pre-data redundancy */ + /* (redundancy per codeword) * (number of codewords, rounded up) */ + L2 = (n_pre - k_pre) * (unsigned long int) ceil((double) L1 / (double) k_pre); + + /* Find protection type for post data and its associated redundancy field length*/ + if ((hprot == 16) || (hprot == 32)) { + /* there is a CRC for post-data */ + k_post = post_len; + n_post = post_len + (hprot >> 3); + /*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */ + + } else if ((hprot >= 37) && (hprot <= 128)) { + /* there is a RS for post-data */ + k_post = 32; + n_post = hprot; + + } else { + /* Use predefined codes */ + n_post = n_pre; + k_post = k_pre; + }; + + /* Create the EPB(s) */ + while (post_len > 0) { + + /* maximum postlen in order to respect EPB size + (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms)*/ + /* (message word size) * (number of containable parity words) */ + max_postlen = k_post * (unsigned long int) floor((double) JPWL_MAXIMUM_EPB_ROOM / (double) (n_post - k_post)); + + /* maximum postlen in order to respect EPB size */ + if (*idx == 0) + /* (we use (JPWL_MAXIMUM_EPB_ROOM - L2) instead of 65535 for keeping room for EPB parms + pre-data) */ + /* (message word size) * (number of containable parity words) */ + max_postlen = k_post * (unsigned long int) floor((double) (JPWL_MAXIMUM_EPB_ROOM - L2) / (double) (n_post - k_post)); + + else + /* (we use JPWL_MAXIMUM_EPB_ROOM instead of 65535 for keeping room for EPB parms) */ + /* (message word size) * (number of containable parity words) */ + max_postlen = k_post * (unsigned long int) floor((double) JPWL_MAXIMUM_EPB_ROOM / (double) (n_post - k_post)); + + /* null protection case */ + /* the max post length can be as large as the LDPepb field can host */ + if (hprot == 0) + max_postlen = INT_MAX; + + /* length to use */ + dL4 = min(max_postlen, post_len); + + if ((epb_mark = jpwl_epb_create( + j2k, /* this encoder handle */ + latest ? (dL4 < max_postlen) : OPJ_FALSE, /* is it the latest? */ + packed, /* is it packed? */ + tileno, /* we are in TPH */ + *idx, /* its index */ + hprot, /* protection type parameters of following data */ + 0, /* pre-data: nothing for now */ + dL4 /* post-data: the stub computed previously */ + ))) { + + /* Add this marker to the 'insertanda' list */ + if (*jwmarker_num < JPWL_MAX_NO_MARKERS) { + jwmarker[*jwmarker_num].id = J2K_MS_EPB; /* its type */ + jwmarker[*jwmarker_num].m.epbmark = epb_mark; /* the EPB */ + jwmarker[*jwmarker_num].pos = (int) place_pos; /* after SOT */ + jwmarker[*jwmarker_num].dpos = place_pos + 0.0000001 * (double)(*idx); /* not very first! */ + jwmarker[*jwmarker_num].len = epb_mark->Lepb; /* its length */ + jwmarker[*jwmarker_num].len_ready = OPJ_TRUE; /* ready */ + jwmarker[*jwmarker_num].pos_ready = OPJ_TRUE; /* ready */ + jwmarker[*jwmarker_num].parms_ready = OPJ_TRUE; /* ready */ + jwmarker[*jwmarker_num].data_ready = OPJ_FALSE; /* not ready */ + (*jwmarker_num)++; + } + + /* increment epb index */ + (*idx)++; + + /* decrease postlen */ + post_len -= dL4; + + /* increase the total length of EPBs */ + epbs_len += epb_mark->Lepb + 2; + + } else { + /* ooops, problems */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not create TPH EPB for UEP in tile %d\n", tileno); + }; + } + + return epbs_len; } jpwl_epb_ms_t *jpwl_epb_create(opj_j2k_t *j2k, opj_bool latest, opj_bool packed, int tileno, int idx, int hprot, - unsigned long int pre_len, unsigned long int post_len) { - - jpwl_epb_ms_t *epb = NULL; - /*unsigned short int data_len = 0;*/ - unsigned short int L2, L3; - unsigned long int L1, L4; - /*unsigned char *predata_in = NULL;*/ - - opj_bool insideMH = (tileno == -1); - - /* Alloc space */ - if (!(epb = (jpwl_epb_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_epb_ms_t)))) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for one EPB MS\n"); - return NULL; - }; - - /* We set RS(n,k) for EPB parms and pre-data, if any */ - if (insideMH && (idx == 0)) { - /* First EPB in MH */ - epb->k_pre = 64; - epb->n_pre = 160; - } else if (!insideMH && (idx == 0)) { - /* First EPB in TH */ - epb->k_pre = 25; - epb->n_pre = 80; - } else { - /* Following EPBs in MH or TH */ - epb->k_pre = 13; - epb->n_pre = 40; - }; - - /* Find lengths, Figs. B3 and B4 */ - /* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */ - L1 = pre_len + 13; - epb->pre_len = pre_len; - - /* size of pre-data redundancy */ - /* (redundancy per codeword) * (number of codewords, rounded up) */ - L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 / (double) epb->k_pre); - - /* length of post-data */ - L4 = post_len; - epb->post_len = post_len; - - /* Find protection type for post data and its associated redundancy field length*/ - if ((hprot == 16) || (hprot == 32)) { - /* there is a CRC for post-data */ - epb->Pepb = 0x10000000 | ((unsigned long int) hprot >> 5); /* 0=CRC-16, 1=CRC-32 */ - epb->k_post = post_len; - epb->n_post = post_len + (hprot >> 3); - /*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */ - - } else if ((hprot >= 37) && (hprot <= 128)) { - /* there is a RS for post-data */ - epb->Pepb = 0x20000020 | (((unsigned long int) hprot & 0x000000FF) << 8); - epb->k_post = 32; - epb->n_post = hprot; - - } else if (hprot == 1) { - /* Use predefined codes */ - epb->Pepb = (unsigned long int) 0x00000000; - epb->n_post = epb->n_pre; - epb->k_post = epb->k_pre; - - } else if (hprot == 0) { - /* Placeholder EPB: only protects its parameters, no protection method */ - epb->Pepb = (unsigned long int) 0xFFFFFFFF; - epb->n_post = 1; - epb->k_post = 1; - - } else { - opj_event_msg(j2k->cinfo, EVT_ERROR, "Invalid protection value for EPB h = %d\n", hprot); - return NULL; - } - - epb->hprot = hprot; - - /* (redundancy per codeword) * (number of codewords, rounded up) */ - L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 / (double) epb->k_post); - - /* private fields */ - epb->tileno = tileno; - - /* Fill some fields of the EPB */ - - /* total length of the EPB MS (less the EPB marker itself): */ - /* Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) + pre_redundancy + post-redundancy */ - epb->Lepb = 11 + L2 + L3; - - /* EPB style */ - epb->Depb = ((packed & 0x0001) << 7) | ((latest & 0x0001) << 6) | (idx & 0x003F); - - /* length of data protected by EPB: */ - epb->LDPepb = L1 + L4; - - return epb; + unsigned long int pre_len, unsigned long int post_len) +{ + + jpwl_epb_ms_t *epb = NULL; + /*unsigned short int data_len = 0;*/ + unsigned short int L2, L3; + unsigned long int L1, L4; + /*unsigned char *predata_in = NULL;*/ + + opj_bool insideMH = (tileno == -1); + + /* Alloc space */ + if (!(epb = (jpwl_epb_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_epb_ms_t)))) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for one EPB MS\n"); + return NULL; + }; + + /* We set RS(n,k) for EPB parms and pre-data, if any */ + if (insideMH && (idx == 0)) { + /* First EPB in MH */ + epb->k_pre = 64; + epb->n_pre = 160; + } else if (!insideMH && (idx == 0)) { + /* First EPB in TH */ + epb->k_pre = 25; + epb->n_pre = 80; + } else { + /* Following EPBs in MH or TH */ + epb->k_pre = 13; + epb->n_pre = 40; + }; + + /* Find lengths, Figs. B3 and B4 */ + /* size of pre data: pre_buf(pre_len) + EPB(2) + Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) */ + L1 = pre_len + 13; + epb->pre_len = pre_len; + + /* size of pre-data redundancy */ + /* (redundancy per codeword) * (number of codewords, rounded up) */ + L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 / (double) epb->k_pre); + + /* length of post-data */ + L4 = post_len; + epb->post_len = post_len; + + /* Find protection type for post data and its associated redundancy field length*/ + if ((hprot == 16) || (hprot == 32)) { + /* there is a CRC for post-data */ + epb->Pepb = 0x10000000 | ((unsigned long int) hprot >> 5); /* 0=CRC-16, 1=CRC-32 */ + epb->k_post = post_len; + epb->n_post = post_len + (hprot >> 3); + /*L3 = hprot >> 3;*/ /* 2 (CRC-16) or 4 (CRC-32) bytes */ + + } else if ((hprot >= 37) && (hprot <= 128)) { + /* there is a RS for post-data */ + epb->Pepb = 0x20000020 | (((unsigned long int) hprot & 0x000000FF) << 8); + epb->k_post = 32; + epb->n_post = hprot; + + } else if (hprot == 1) { + /* Use predefined codes */ + epb->Pepb = (unsigned long int) 0x00000000; + epb->n_post = epb->n_pre; + epb->k_post = epb->k_pre; + + } else if (hprot == 0) { + /* Placeholder EPB: only protects its parameters, no protection method */ + epb->Pepb = (unsigned long int) 0xFFFFFFFF; + epb->n_post = 1; + epb->k_post = 1; + + } else { + opj_event_msg(j2k->cinfo, EVT_ERROR, "Invalid protection value for EPB h = %d\n", hprot); + return NULL; + } + + epb->hprot = hprot; + + /* (redundancy per codeword) * (number of codewords, rounded up) */ + L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 / (double) epb->k_post); + + /* private fields */ + epb->tileno = tileno; + + /* Fill some fields of the EPB */ + + /* total length of the EPB MS (less the EPB marker itself): */ + /* Lepb(2) + Depb(1) + LDPepb(4) + Pepb(4) + pre_redundancy + post-redundancy */ + epb->Lepb = 11 + L2 + L3; + + /* EPB style */ + epb->Depb = ((packed & 0x0001) << 7) | ((latest & 0x0001) << 6) | (idx & 0x003F); + + /* length of data protected by EPB: */ + epb->LDPepb = L1 + L4; + + return epb; } -void jpwl_epb_write(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf) { +void jpwl_epb_write(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf) +{ - /* Marker */ - *(buf++) = (unsigned char) (J2K_MS_EPB >> 8); - *(buf++) = (unsigned char) (J2K_MS_EPB >> 0); + /* Marker */ + *(buf++) = (unsigned char) (J2K_MS_EPB >> 8); + *(buf++) = (unsigned char) (J2K_MS_EPB >> 0); - /* Lepb */ - *(buf++) = (unsigned char) (epb->Lepb >> 8); - *(buf++) = (unsigned char) (epb->Lepb >> 0); + /* Lepb */ + *(buf++) = (unsigned char) (epb->Lepb >> 8); + *(buf++) = (unsigned char) (epb->Lepb >> 0); - /* Depb */ - *(buf++) = (unsigned char) (epb->Depb >> 0); + /* Depb */ + *(buf++) = (unsigned char) (epb->Depb >> 0); - /* LDPepb */ - *(buf++) = (unsigned char) (epb->LDPepb >> 24); - *(buf++) = (unsigned char) (epb->LDPepb >> 16); - *(buf++) = (unsigned char) (epb->LDPepb >> 8); - *(buf++) = (unsigned char) (epb->LDPepb >> 0); + /* LDPepb */ + *(buf++) = (unsigned char) (epb->LDPepb >> 24); + *(buf++) = (unsigned char) (epb->LDPepb >> 16); + *(buf++) = (unsigned char) (epb->LDPepb >> 8); + *(buf++) = (unsigned char) (epb->LDPepb >> 0); - /* Pepb */ - *(buf++) = (unsigned char) (epb->Pepb >> 24); - *(buf++) = (unsigned char) (epb->Pepb >> 16); - *(buf++) = (unsigned char) (epb->Pepb >> 8); - *(buf++) = (unsigned char) (epb->Pepb >> 0); + /* Pepb */ + *(buf++) = (unsigned char) (epb->Pepb >> 24); + *(buf++) = (unsigned char) (epb->Pepb >> 16); + *(buf++) = (unsigned char) (epb->Pepb >> 8); + *(buf++) = (unsigned char) (epb->Pepb >> 0); - /* Data */ - /*memcpy(buf, epb->data, (size_t) epb->Lepb - 11);*/ - memset(buf, 0, (size_t) epb->Lepb - 11); + /* Data */ + /*memcpy(buf, epb->data, (size_t) epb->Lepb - 11);*/ + memset(buf, 0, (size_t) epb->Lepb - 11); - /* update markers struct */ - j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, -1, epb->Lepb + 2); + /* update markers struct */ + j2k_add_marker(j2k->cstr_info, J2K_MS_EPB, -1, epb->Lepb + 2); } -jpwl_epc_ms_t *jpwl_epc_create(opj_j2k_t *j2k, opj_bool esd_on, opj_bool red_on, opj_bool epb_on, opj_bool info_on) { +jpwl_epc_ms_t *jpwl_epc_create(opj_j2k_t *j2k, opj_bool esd_on, opj_bool red_on, opj_bool epb_on, opj_bool info_on) +{ - jpwl_epc_ms_t *epc = NULL; + jpwl_epc_ms_t *epc = NULL; - /* Alloc space */ - if (!(epc = (jpwl_epc_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_epc_ms_t)))) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for EPC MS\n"); - return NULL; - }; + /* Alloc space */ + if (!(epc = (jpwl_epc_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_epc_ms_t)))) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for EPC MS\n"); + return NULL; + }; - /* Set the EPC parameters */ - epc->esd_on = esd_on; - epc->epb_on = epb_on; - epc->red_on = red_on; - epc->info_on = info_on; + /* Set the EPC parameters */ + epc->esd_on = esd_on; + epc->epb_on = epb_on; + epc->red_on = red_on; + epc->info_on = info_on; - /* Fill the EPC fields with default values */ - epc->Lepc = 9; - epc->Pcrc = 0x0000; - epc->DL = 0x00000000; - epc->Pepc = ((j2k->cp->esd_on & 0x0001) << 4) | ((j2k->cp->red_on & 0x0001) << 5) | - ((j2k->cp->epb_on & 0x0001) << 6) | ((j2k->cp->info_on & 0x0001) << 7); + /* Fill the EPC fields with default values */ + epc->Lepc = 9; + epc->Pcrc = 0x0000; + epc->DL = 0x00000000; + epc->Pepc = ((j2k->cp->esd_on & 0x0001) << 4) | ((j2k->cp->red_on & 0x0001) << 5) | + ((j2k->cp->epb_on & 0x0001) << 6) | ((j2k->cp->info_on & 0x0001) << 7); - return (epc); + return (epc); } -opj_bool jpwl_epb_fill(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf, unsigned char *post_buf) { - - unsigned long int L1, L2, L3, L4; - int remaining; - unsigned long int P, NN_P; - - /* Operating buffer */ - static unsigned char codeword[NN], *parityword; - - unsigned char *L1_buf, *L2_buf; - /* these ones are static, since we need to keep memory of - the exact place from one call to the other */ - static unsigned char *L3_buf, *L4_buf; - - /* some consistency check */ - if (!buf) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs\n"); - return OPJ_FALSE; - } - - if (!post_buf && !L4_buf) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs data\n"); - return OPJ_FALSE; - } - - /* - * Compute parity bytes on pre-data, ALWAYS present (at least only for EPB parms) - */ - - /* Initialize RS structures */ - P = epb->n_pre - epb->k_pre; - NN_P = NN - P; - memset(codeword, 0, NN); - parityword = codeword + NN_P; - init_rs(NN_P); - - /* pre-data begins pre_len bytes before of EPB buf */ - L1_buf = buf - epb->pre_len; - L1 = epb->pre_len + 13; - - /* redundancy for pre-data begins immediately after EPB parms */ - L2_buf = buf + 13; - L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 / (double) epb->k_pre); - - /* post-data - the position of L4 buffer can be: - 1) passed as a parameter: in that case use it - 2) null: in that case use the previous (static) one - */ - if (post_buf) - L4_buf = post_buf; - L4 = epb->post_len; - - /* post-data redundancy begins immediately after pre-data redundancy */ - L3_buf = L2_buf + L2; - L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 / (double) epb->k_post); - - /* let's check whether EPB length is sufficient to contain all these data */ - if (epb->Lepb < (11 + L2 + L3)) - opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no room in EPB data field for writing redundancy data\n"); - /*printf("Env. %d, nec. %d (%d + %d)\n", epb->Lepb - 11, L2 + L3, L2, L3);*/ - - /* Compute redundancy of pre-data message words */ - remaining = L1; - while (remaining) { - - /* copy message data into codeword buffer */ - if (remaining < epb->k_pre) { - /* the last message word is zero-padded */ - memset(codeword, 0, NN); - memcpy(codeword, L1_buf, remaining); - L1_buf += remaining; - remaining = 0; - - } else { - memcpy(codeword, L1_buf, epb->k_pre); - L1_buf += epb->k_pre; - remaining -= epb->k_pre; - - } - - /* Encode the buffer and obtain parity bytes */ - if (encode_rs(codeword, parityword)) - opj_event_msg(j2k->cinfo, EVT_WARNING, - "Possible encoding error in codeword @ position #%d\n", (L1_buf - buf) / epb->k_pre); - - /* copy parity bytes only in redundancy buffer */ - memcpy(L2_buf, parityword, P); - - /* advance parity buffer */ - L2_buf += P; - } - - /* - * Compute parity bytes on post-data, may be absent if there are no data - */ - /*printf("Hprot is %d (tileno=%d, k_pre=%d, n_pre=%d, k_post=%d, n_post=%d, pre_len=%d, post_len=%d)\n", - epb->hprot, epb->tileno, epb->k_pre, epb->n_pre, epb->k_post, epb->n_post, epb->pre_len, - epb->post_len);*/ - if (epb->hprot < 0) { - - /* there should be no EPB */ - - } else if (epb->hprot == 0) { - - /* no protection for the data */ - /* advance anyway */ - L4_buf += epb->post_len; - - } else if (epb->hprot == 16) { - - /* CRC-16 */ - unsigned short int mycrc = 0x0000; - - /* compute the CRC field (excluding itself) */ - remaining = L4; - while (remaining--) - jpwl_updateCRC16(&mycrc, *(L4_buf++)); - - /* write the CRC field */ - *(L3_buf++) = (unsigned char) (mycrc >> 8); - *(L3_buf++) = (unsigned char) (mycrc >> 0); - - } else if (epb->hprot == 32) { - - /* CRC-32 */ - unsigned long int mycrc = 0x00000000; - - /* compute the CRC field (excluding itself) */ - remaining = L4; - while (remaining--) - jpwl_updateCRC32(&mycrc, *(L4_buf++)); - - /* write the CRC field */ - *(L3_buf++) = (unsigned char) (mycrc >> 24); - *(L3_buf++) = (unsigned char) (mycrc >> 16); - *(L3_buf++) = (unsigned char) (mycrc >> 8); - *(L3_buf++) = (unsigned char) (mycrc >> 0); - - } else { - - /* RS */ - - /* Initialize RS structures */ - P = epb->n_post - epb->k_post; - NN_P = NN - P; - memset(codeword, 0, NN); - parityword = codeword + NN_P; - init_rs(NN_P); - - /* Compute redundancy of post-data message words */ - remaining = L4; - while (remaining) { - - /* copy message data into codeword buffer */ - if (remaining < epb->k_post) { - /* the last message word is zero-padded */ - memset(codeword, 0, NN); - memcpy(codeword, L4_buf, remaining); - L4_buf += remaining; - remaining = 0; +opj_bool jpwl_epb_fill(opj_j2k_t *j2k, jpwl_epb_ms_t *epb, unsigned char *buf, unsigned char *post_buf) +{ + + unsigned long int L1, L2, L3, L4; + int remaining; + unsigned long int P, NN_P; + + /* Operating buffer */ + static unsigned char codeword[NN], *parityword; + + unsigned char *L1_buf, *L2_buf; + /* these ones are static, since we need to keep memory of + the exact place from one call to the other */ + static unsigned char *L3_buf, *L4_buf; + + /* some consistency check */ + if (!buf) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs\n"); + return OPJ_FALSE; + } + + if (!post_buf && !L4_buf) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no operating buffer for EPBs data\n"); + return OPJ_FALSE; + } + + /* + * Compute parity bytes on pre-data, ALWAYS present (at least only for EPB parms) + */ + + /* Initialize RS structures */ + P = epb->n_pre - epb->k_pre; + NN_P = NN - P; + memset(codeword, 0, NN); + parityword = codeword + NN_P; + init_rs(NN_P); + + /* pre-data begins pre_len bytes before of EPB buf */ + L1_buf = buf - epb->pre_len; + L1 = epb->pre_len + 13; + + /* redundancy for pre-data begins immediately after EPB parms */ + L2_buf = buf + 13; + L2 = (epb->n_pre - epb->k_pre) * (unsigned short int) ceil((double) L1 / (double) epb->k_pre); + + /* post-data + the position of L4 buffer can be: + 1) passed as a parameter: in that case use it + 2) null: in that case use the previous (static) one + */ + if (post_buf) + L4_buf = post_buf; + L4 = epb->post_len; + + /* post-data redundancy begins immediately after pre-data redundancy */ + L3_buf = L2_buf + L2; + L3 = (epb->n_post - epb->k_post) * (unsigned short int) ceil((double) L4 / (double) epb->k_post); + + /* let's check whether EPB length is sufficient to contain all these data */ + if (epb->Lepb < (11 + L2 + L3)) + opj_event_msg(j2k->cinfo, EVT_ERROR, "There is no room in EPB data field for writing redundancy data\n"); + /*printf("Env. %d, nec. %d (%d + %d)\n", epb->Lepb - 11, L2 + L3, L2, L3);*/ + + /* Compute redundancy of pre-data message words */ + remaining = L1; + while (remaining) { + + /* copy message data into codeword buffer */ + if (remaining < epb->k_pre) { + /* the last message word is zero-padded */ + memset(codeword, 0, NN); + memcpy(codeword, L1_buf, remaining); + L1_buf += remaining; + remaining = 0; + + } else { + memcpy(codeword, L1_buf, epb->k_pre); + L1_buf += epb->k_pre; + remaining -= epb->k_pre; + + } + + /* Encode the buffer and obtain parity bytes */ + if (encode_rs(codeword, parityword)) + opj_event_msg(j2k->cinfo, EVT_WARNING, + "Possible encoding error in codeword @ position #%d\n", (L1_buf - buf) / epb->k_pre); + + /* copy parity bytes only in redundancy buffer */ + memcpy(L2_buf, parityword, P); + + /* advance parity buffer */ + L2_buf += P; + } + + /* + * Compute parity bytes on post-data, may be absent if there are no data + */ + /*printf("Hprot is %d (tileno=%d, k_pre=%d, n_pre=%d, k_post=%d, n_post=%d, pre_len=%d, post_len=%d)\n", + epb->hprot, epb->tileno, epb->k_pre, epb->n_pre, epb->k_post, epb->n_post, epb->pre_len, + epb->post_len);*/ + if (epb->hprot < 0) { + + /* there should be no EPB */ + + } else if (epb->hprot == 0) { + + /* no protection for the data */ + /* advance anyway */ + L4_buf += epb->post_len; + + } else if (epb->hprot == 16) { + + /* CRC-16 */ + unsigned short int mycrc = 0x0000; + + /* compute the CRC field (excluding itself) */ + remaining = L4; + while (remaining--) + jpwl_updateCRC16(&mycrc, *(L4_buf++)); + + /* write the CRC field */ + *(L3_buf++) = (unsigned char) (mycrc >> 8); + *(L3_buf++) = (unsigned char) (mycrc >> 0); + + } else if (epb->hprot == 32) { + + /* CRC-32 */ + unsigned long int mycrc = 0x00000000; + + /* compute the CRC field (excluding itself) */ + remaining = L4; + while (remaining--) + jpwl_updateCRC32(&mycrc, *(L4_buf++)); + + /* write the CRC field */ + *(L3_buf++) = (unsigned char) (mycrc >> 24); + *(L3_buf++) = (unsigned char) (mycrc >> 16); + *(L3_buf++) = (unsigned char) (mycrc >> 8); + *(L3_buf++) = (unsigned char) (mycrc >> 0); + + } else { + + /* RS */ + + /* Initialize RS structures */ + P = epb->n_post - epb->k_post; + NN_P = NN - P; + memset(codeword, 0, NN); + parityword = codeword + NN_P; + init_rs(NN_P); + + /* Compute redundancy of post-data message words */ + remaining = L4; + while (remaining) { + + /* copy message data into codeword buffer */ + if (remaining < epb->k_post) { + /* the last message word is zero-padded */ + memset(codeword, 0, NN); + memcpy(codeword, L4_buf, remaining); + L4_buf += remaining; + remaining = 0; + + } else { + memcpy(codeword, L4_buf, epb->k_post); + L4_buf += epb->k_post; + remaining -= epb->k_post; - } else { - memcpy(codeword, L4_buf, epb->k_post); - L4_buf += epb->k_post; - remaining -= epb->k_post; - - } - - /* Encode the buffer and obtain parity bytes */ - if (encode_rs(codeword, parityword)) - opj_event_msg(j2k->cinfo, EVT_WARNING, - "Possible encoding error in codeword @ position #%d\n", (L4_buf - buf) / epb->k_post); + } - /* copy parity bytes only in redundancy buffer */ - memcpy(L3_buf, parityword, P); + /* Encode the buffer and obtain parity bytes */ + if (encode_rs(codeword, parityword)) + opj_event_msg(j2k->cinfo, EVT_WARNING, + "Possible encoding error in codeword @ position #%d\n", (L4_buf - buf) / epb->k_post); - /* advance parity buffer */ - L3_buf += P; - } + /* copy parity bytes only in redundancy buffer */ + memcpy(L3_buf, parityword, P); - } + /* advance parity buffer */ + L3_buf += P; + } - return OPJ_TRUE; + } + + return OPJ_TRUE; } -opj_bool jpwl_correct(opj_j2k_t *j2k) { - - opj_cio_t *cio = j2k->cio; - opj_bool status; - static opj_bool mh_done = OPJ_FALSE; - int mark_pos, id, len, skips, sot_pos; - unsigned long int Psot = 0; - - /* go back to marker position */ - mark_pos = cio_tell(cio) - 2; - cio_seek(cio, mark_pos); - - if ((j2k->state == J2K_STATE_MHSOC) && !mh_done) { - - int mark_val = 0, skipnum = 0; - - /* - COLOR IMAGE - first thing to do, if we are here, is to look whether - 51 (skipnum) positions ahead there is an EPB, in case of MH - */ - /* - B/W IMAGE - first thing to do, if we are here, is to look whether - 45 (skipnum) positions ahead there is an EPB, in case of MH - */ - /* SIZ SIZ_FIELDS SIZ_COMPS FOLLOWING_MARKER */ - skipnum = 2 + 38 + 3 * j2k->cp->exp_comps + 2; - if ((cio->bp + skipnum) < cio->end) { - - cio_skip(cio, skipnum); - - /* check that you are not going beyond the end of codestream */ - - /* call EPB corrector */ - status = jpwl_epb_correct(j2k, /* J2K decompressor handle */ - cio->bp, /* pointer to EPB in codestream buffer */ - 0, /* EPB type: MH */ - skipnum, /* length of pre-data */ - -1, /* length of post-data: -1 means auto */ - NULL, - NULL - ); - - /* read the marker value */ - mark_val = (*(cio->bp) << 8) | *(cio->bp + 1); - - if (status && (mark_val == J2K_MS_EPB)) { - /* we found it! */ - mh_done = OPJ_TRUE; - return OPJ_TRUE; - } - - /* Disable correction in case of missing or bad head EPB */ - /* We can't do better! */ - /* PATCHED: 2008-01-25 */ - /* MOVED UP: 2008-02-01 */ - if (!status) { - j2k->cp->correct = OPJ_FALSE; - opj_event_msg(j2k->cinfo, EVT_WARNING, "Couldn't find the MH EPB: disabling JPWL\n"); - } - - } - - } - - if (OPJ_TRUE /*(j2k->state == J2K_STATE_TPHSOT) || (j2k->state == J2K_STATE_TPH)*/) { - /* else, look if 12 positions ahead there is an EPB, in case of TPH */ - cio_seek(cio, mark_pos); - if ((cio->bp + 12) < cio->end) { - - cio_skip(cio, 12); - - /* call EPB corrector */ - status = jpwl_epb_correct(j2k, /* J2K decompressor handle */ - cio->bp, /* pointer to EPB in codestream buffer */ - 1, /* EPB type: TPH */ - 12, /* length of pre-data */ - -1, /* length of post-data: -1 means auto */ - NULL, - NULL - ); - if (status) - /* we found it! */ - return OPJ_TRUE; - } - } - - return OPJ_FALSE; - - /* for now, don't use this code */ - - /* else, look if here is an EPB, in case of other */ - if (mark_pos > 64) { - /* it cannot stay before the first MH EPB */ - cio_seek(cio, mark_pos); - cio_skip(cio, 0); - - /* call EPB corrector */ - status = jpwl_epb_correct(j2k, /* J2K decompressor handle */ - cio->bp, /* pointer to EPB in codestream buffer */ - 2, /* EPB type: TPH */ - 0, /* length of pre-data */ - -1, /* length of post-data: -1 means auto */ - NULL, - NULL - ); - if (status) - /* we found it! */ - return OPJ_TRUE; - } - - /* nope, no EPBs probably, or they are so damaged that we can give up */ - return OPJ_FALSE; - - return OPJ_TRUE; - - /* AN ATTEMPT OF PARSER */ - /* NOT USED ACTUALLY */ - - /* go to the beginning of the file */ - cio_seek(cio, 0); - - /* let's begin */ - j2k->state = J2K_STATE_MHSOC; - - /* cycle all over the markers */ - while (cio_tell(cio) < cio->length) { - - /* read the marker */ - mark_pos = cio_tell(cio); - id = cio_read(cio, 2); - - /* details */ - printf("Marker@%d: %X\n", cio_tell(cio) - 2, id); - - /* do an action in response to the read marker */ - switch (id) { - - /* short markers */ - - /* SOC */ - case J2K_MS_SOC: - j2k->state = J2K_STATE_MHSIZ; - len = 0; - skips = 0; - break; - - /* EOC */ - case J2K_MS_EOC: - j2k->state = J2K_STATE_MT; - len = 0; - skips = 0; - break; - - /* particular case of SOD */ - case J2K_MS_SOD: - len = Psot - (mark_pos - sot_pos) - 2; - skips = len; - break; - - /* long markers */ - - /* SOT */ - case J2K_MS_SOT: - j2k->state = J2K_STATE_TPH; - sot_pos = mark_pos; /* position of SOT */ - len = cio_read(cio, 2); /* read the length field */ - cio_skip(cio, 2); /* this field is unnecessary */ - Psot = cio_read(cio, 4); /* tile length */ - skips = len - 8; - break; - - /* remaining */ - case J2K_MS_SIZ: - j2k->state = J2K_STATE_MH; - /* read the length field */ - len = cio_read(cio, 2); - skips = len - 2; - break; - - /* remaining */ - default: - /* read the length field */ - len = cio_read(cio, 2); - skips = len - 2; - break; - - } - - /* skip to marker's end */ - cio_skip(cio, skips); - - } +opj_bool jpwl_correct(opj_j2k_t *j2k) +{ + + opj_cio_t *cio = j2k->cio; + opj_bool status; + static opj_bool mh_done = OPJ_FALSE; + int mark_pos, id, len, skips, sot_pos; + unsigned long int Psot = 0; + + /* go back to marker position */ + mark_pos = cio_tell(cio) - 2; + cio_seek(cio, mark_pos); + + if ((j2k->state == J2K_STATE_MHSOC) && !mh_done) { + + int mark_val = 0, skipnum = 0; + + /* + COLOR IMAGE + first thing to do, if we are here, is to look whether + 51 (skipnum) positions ahead there is an EPB, in case of MH + */ + /* + B/W IMAGE + first thing to do, if we are here, is to look whether + 45 (skipnum) positions ahead there is an EPB, in case of MH + */ + /* SIZ SIZ_FIELDS SIZ_COMPS FOLLOWING_MARKER */ + skipnum = 2 + 38 + 3 * j2k->cp->exp_comps + 2; + if ((cio->bp + skipnum) < cio->end) { + + cio_skip(cio, skipnum); + + /* check that you are not going beyond the end of codestream */ + + /* call EPB corrector */ + status = jpwl_epb_correct(j2k, /* J2K decompressor handle */ + cio->bp, /* pointer to EPB in codestream buffer */ + 0, /* EPB type: MH */ + skipnum, /* length of pre-data */ + -1, /* length of post-data: -1 means auto */ + NULL, + NULL + ); + + /* read the marker value */ + mark_val = (*(cio->bp) << 8) | *(cio->bp + 1); + + if (status && (mark_val == J2K_MS_EPB)) { + /* we found it! */ + mh_done = OPJ_TRUE; + return OPJ_TRUE; + } + + /* Disable correction in case of missing or bad head EPB */ + /* We can't do better! */ + /* PATCHED: 2008-01-25 */ + /* MOVED UP: 2008-02-01 */ + if (!status) { + j2k->cp->correct = OPJ_FALSE; + opj_event_msg(j2k->cinfo, EVT_WARNING, "Couldn't find the MH EPB: disabling JPWL\n"); + } + + } + + } + + if (OPJ_TRUE /*(j2k->state == J2K_STATE_TPHSOT) || (j2k->state == J2K_STATE_TPH)*/) { + /* else, look if 12 positions ahead there is an EPB, in case of TPH */ + cio_seek(cio, mark_pos); + if ((cio->bp + 12) < cio->end) { + + cio_skip(cio, 12); + + /* call EPB corrector */ + status = jpwl_epb_correct(j2k, /* J2K decompressor handle */ + cio->bp, /* pointer to EPB in codestream buffer */ + 1, /* EPB type: TPH */ + 12, /* length of pre-data */ + -1, /* length of post-data: -1 means auto */ + NULL, + NULL + ); + if (status) + /* we found it! */ + return OPJ_TRUE; + } + } + + return OPJ_FALSE; + + /* for now, don't use this code */ + + /* else, look if here is an EPB, in case of other */ + if (mark_pos > 64) { + /* it cannot stay before the first MH EPB */ + cio_seek(cio, mark_pos); + cio_skip(cio, 0); + + /* call EPB corrector */ + status = jpwl_epb_correct(j2k, /* J2K decompressor handle */ + cio->bp, /* pointer to EPB in codestream buffer */ + 2, /* EPB type: TPH */ + 0, /* length of pre-data */ + -1, /* length of post-data: -1 means auto */ + NULL, + NULL + ); + if (status) + /* we found it! */ + return OPJ_TRUE; + } + + /* nope, no EPBs probably, or they are so damaged that we can give up */ + return OPJ_FALSE; + + return OPJ_TRUE; + + /* AN ATTEMPT OF PARSER */ + /* NOT USED ACTUALLY */ + + /* go to the beginning of the file */ + cio_seek(cio, 0); + + /* let's begin */ + j2k->state = J2K_STATE_MHSOC; + + /* cycle all over the markers */ + while (cio_tell(cio) < cio->length) { + + /* read the marker */ + mark_pos = cio_tell(cio); + id = cio_read(cio, 2); + + /* details */ + printf("Marker@%d: %X\n", cio_tell(cio) - 2, id); + + /* do an action in response to the read marker */ + switch (id) { + + /* short markers */ + + /* SOC */ + case J2K_MS_SOC: + j2k->state = J2K_STATE_MHSIZ; + len = 0; + skips = 0; + break; + + /* EOC */ + case J2K_MS_EOC: + j2k->state = J2K_STATE_MT; + len = 0; + skips = 0; + break; + + /* particular case of SOD */ + case J2K_MS_SOD: + len = Psot - (mark_pos - sot_pos) - 2; + skips = len; + break; + + /* long markers */ + + /* SOT */ + case J2K_MS_SOT: + j2k->state = J2K_STATE_TPH; + sot_pos = mark_pos; /* position of SOT */ + len = cio_read(cio, 2); /* read the length field */ + cio_skip(cio, 2); /* this field is unnecessary */ + Psot = cio_read(cio, 4); /* tile length */ + skips = len - 8; + break; + + /* remaining */ + case J2K_MS_SIZ: + j2k->state = J2K_STATE_MH; + /* read the length field */ + len = cio_read(cio, 2); + skips = len - 2; + break; + + /* remaining */ + default: + /* read the length field */ + len = cio_read(cio, 2); + skips = len - 2; + break; + + } + + /* skip to marker's end */ + cio_skip(cio, skips); + + } } opj_bool jpwl_epb_correct(opj_j2k_t *j2k, unsigned char *buffer, int type, int pre_len, int post_len, int *conn, - unsigned char **L4_bufp) { - - /* Operating buffer */ - unsigned char codeword[NN], *parityword; - - unsigned long int P, NN_P; - unsigned long int L1, L4; - int remaining, n_pre, k_pre, n_post, k_post; - - int status, tt; - - int orig_pos = cio_tell(j2k->cio); - - unsigned char *L1_buf, *L2_buf; - unsigned char *L3_buf, *L4_buf; - - unsigned long int LDPepb, Pepb; - unsigned short int Lepb; - unsigned char Depb; - char str1[25] = ""; - int myconn, errnum = 0; - opj_bool errflag = OPJ_FALSE; - - opj_cio_t *cio = j2k->cio; - - /* check for common errors */ - if (!buffer) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "The EPB pointer is a NULL buffer\n"); - return OPJ_FALSE; - } - - /* set bignesses */ - L1 = pre_len + 13; - - /* pre-data correction */ - switch (type) { - - case 0: - /* MH EPB */ - k_pre = 64; - n_pre = 160; - break; - - case 1: - /* TPH EPB */ - k_pre = 25; - n_pre = 80; - break; - - case 2: - /* other EPBs */ - k_pre = 13; - n_pre = 40; - break; - - case 3: - /* automatic setup */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Auto. setup not yet implemented\n"); - return OPJ_FALSE; - break; - - default: - /* unknown type */ - opj_event_msg(j2k->cinfo, EVT_ERROR, "Unknown expected EPB type\n"); - return OPJ_FALSE; - break; - - } - - /* Initialize RS structures */ - P = n_pre - k_pre; - NN_P = NN - P; - tt = (int) floor((float) P / 2.0F); /* correction capability of the code */ - memset(codeword, 0, NN); - parityword = codeword + NN_P; - init_rs(NN_P); - - /* Correct pre-data message words */ - L1_buf = buffer - pre_len; - L2_buf = buffer + 13; - remaining = L1; - while (remaining) { - - /* always zero-pad codewords */ - /* (this is required, since after decoding the zeros in the long codeword - could change, and keep unchanged in subsequent calls) */ - memset(codeword, 0, NN); - - /* copy codeword buffer into message bytes */ - if (remaining < k_pre) - memcpy(codeword, L1_buf, remaining); - else - memcpy(codeword, L1_buf, k_pre); - - /* copy redundancy buffer in parity bytes */ - memcpy(parityword, L2_buf, P); - - /* Decode the buffer and possibly obtain corrected bytes */ - status = eras_dec_rs(codeword, NULL, 0); - if (status == -1) { - /*if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_WARNING, - "Possible decoding error in codeword @ position #%d\n", (L1_buf - buffer) / k_pre);*/ - errflag = OPJ_TRUE; - /* we can try to safely get out from the function: - if we are here, either this is not an EPB or the first codeword - is too damaged to be helpful */ - /*return OPJ_FALSE;*/ - - } else if (status == 0) { - /*if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/ - - } else if (status <= tt) { - /* it has corrected 0 <= errs <= tt */ - /*if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/ - errnum += status; - - } else { - /*if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n"); - return OPJ_FALSE;*/ - errflag = OPJ_TRUE; - } - - - /* advance parity buffer */ - if ((status >= 0) && (status <= tt)) - /* copy back corrected parity only if all is OK */ - memcpy(L2_buf, parityword, P); - L2_buf += P; - - /* advance message buffer */ - if (remaining < k_pre) { - if ((status >= 0) && (status <= tt)) - /* copy back corrected data only if all is OK */ - memcpy(L1_buf, codeword, remaining); - L1_buf += remaining; - remaining = 0; - - } else { - if ((status >= 0) && (status <= tt)) - /* copy back corrected data only if all is OK */ - memcpy(L1_buf, codeword, k_pre); - L1_buf += k_pre; - remaining -= k_pre; - - } - } - - /* print summary */ - if (!conn) { - - /*if (errnum) - opj_event_msg(j2k->cinfo, EVT_INFO, "+ %d symbol errors corrected (Ps=%.1e)\n", errnum, - (float) errnum / ((float) n_pre * (float) L1 / (float) k_pre));*/ - if (errflag) { - /*opj_event_msg(j2k->cinfo, EVT_INFO, "+ there were unrecoverable errors\n");*/ - return OPJ_FALSE; - } - - } - - /* presumably, now, EPB parameters are correct */ - /* let's get them */ - - /* Simply read the EPB parameters */ - if (conn) - cio->bp = buffer; - cio_skip(cio, 2); /* the marker */ - Lepb = cio_read(cio, 2); - Depb = cio_read(cio, 1); - LDPepb = cio_read(cio, 4); - Pepb = cio_read(cio, 4); - - /* What does Pepb tells us about the protection method? */ - if (((Pepb & 0xF0000000) >> 28) == 0) - sprintf(str1, "pred"); /* predefined */ - else if (((Pepb & 0xF0000000) >> 28) == 1) - sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */ - else if (((Pepb & 0xF0000000) >> 28) == 2) - sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */ - else if (Pepb == 0xFFFFFFFF) - sprintf(str1, "nometh"); /* RS mode */ - else - sprintf(str1, "unknown"); /* unknown */ - - /* Now we write them to screen */ - if (!conn && post_len) - opj_event_msg(j2k->cinfo, EVT_INFO, - "EPB(%d): (%sl, %sp, %u), %lu, %s\n", - cio_tell(cio) - 13, - (Depb & 0x40) ? "" : "n", /* latest EPB or not? */ - (Depb & 0x80) ? "" : "n", /* packed or unpacked EPB? */ - (Depb & 0x3F), /* EPB index value */ - LDPepb, /*length of the data protected by the EPB */ - str1); /* protection method */ - - - /* well, we need to investigate how long is the connected length of packed EPBs */ - myconn = Lepb + 2; - if ((Depb & 0x40) == 0) /* not latest in header */ - jpwl_epb_correct(j2k, /* J2K decompressor handle */ - buffer + Lepb + 2, /* pointer to next EPB in codestream buffer */ - 2, /* EPB type: should be of other type */ - 0, /* only EPB fields */ - 0, /* do not look after */ - &myconn, - NULL - ); - if (conn) - *conn += myconn; - - /*if (!conn) - printf("connected = %d\n", myconn);*/ - - /*cio_seek(j2k->cio, orig_pos); - return OPJ_TRUE;*/ - - /* post-data - the position of L4 buffer is at the end of currently connected EPBs - */ - if (!(L4_bufp)) - L4_buf = buffer + myconn; - else if (!(*L4_bufp)) - L4_buf = buffer + myconn; - else - L4_buf = *L4_bufp; - if (post_len == -1) - L4 = LDPepb - pre_len - 13; - else if (post_len == 0) - L4 = 0; - else - L4 = post_len; - - L3_buf = L2_buf; - - /* Do a further check here on the read parameters */ - if (L4 > (unsigned long) cio_numbytesleft(j2k->cio)) - /* overflow */ - return OPJ_FALSE; - - /* we are ready for decoding the remaining data */ - if (((Pepb & 0xF0000000) >> 28) == 1) { - /* CRC here */ - if ((16 * ((Pepb & 0x00000001) + 1)) == 16) { - - /* CRC-16 */ - unsigned short int mycrc = 0x0000, filecrc = 0x0000; - - /* compute the CRC field */ - remaining = L4; - while (remaining--) - jpwl_updateCRC16(&mycrc, *(L4_buf++)); - - /* read the CRC field */ - filecrc = *(L3_buf++) << 8; - filecrc |= *(L3_buf++); - - /* check the CRC field */ - if (mycrc == filecrc) { - if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n"); - } else { - if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc, mycrc); - errflag = OPJ_TRUE; - } - } - - if ((16 * ((Pepb & 0x00000001) + 1)) == 32) { - - /* CRC-32 */ - unsigned long int mycrc = 0x00000000, filecrc = 0x00000000; - - /* compute the CRC field */ - remaining = L4; - while (remaining--) - jpwl_updateCRC32(&mycrc, *(L4_buf++)); - - /* read the CRC field */ - filecrc = *(L3_buf++) << 24; - filecrc |= *(L3_buf++) << 16; - filecrc |= *(L3_buf++) << 8; - filecrc |= *(L3_buf++); - - /* check the CRC field */ - if (mycrc == filecrc) { - if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n"); - } else { - if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc, mycrc); - errflag = OPJ_TRUE; - } - } - - } else if (Pepb == 0xFFFFFFFF) { - /* no method */ - - /* advance without doing anything */ - remaining = L4; - while (remaining--) - L4_buf++; - - } else if ((((Pepb & 0xF0000000) >> 28) == 2) || (((Pepb & 0xF0000000) >> 28) == 0)) { - /* RS coding here */ - - if (((Pepb & 0xF0000000) >> 28) == 0) { - - k_post = k_pre; - n_post = n_pre; - - } else { - - k_post = 32; - n_post = (Pepb & 0x0000FF00) >> 8; - } - - /* Initialize RS structures */ - P = n_post - k_post; - NN_P = NN - P; - tt = (int) floor((float) P / 2.0F); /* again, correction capability */ - memset(codeword, 0, NN); - parityword = codeword + NN_P; - init_rs(NN_P); - - /* Correct post-data message words */ - /*L4_buf = buffer + Lepb + 2;*/ - L3_buf = L2_buf; - remaining = L4; - while (remaining) { - - /* always zero-pad codewords */ - /* (this is required, since after decoding the zeros in the long codeword - could change, and keep unchanged in subsequent calls) */ - memset(codeword, 0, NN); - - /* copy codeword buffer into message bytes */ - if (remaining < k_post) - memcpy(codeword, L4_buf, remaining); - else - memcpy(codeword, L4_buf, k_post); - - /* copy redundancy buffer in parity bytes */ - memcpy(parityword, L3_buf, P); - - /* Decode the buffer and possibly obtain corrected bytes */ - status = eras_dec_rs(codeword, NULL, 0); - if (status == -1) { - /*if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_WARNING, - "Possible decoding error in codeword @ position #%d\n", (L4_buf - (buffer + Lepb + 2)) / k_post);*/ - errflag = OPJ_TRUE; - - } else if (status == 0) { - /*if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/ - - } else if (status <= tt) { - /*if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/ - errnum += status; - - } else { - /*if (conn == NULL) - opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n"); - return OPJ_FALSE;*/ - errflag = OPJ_TRUE; - } - - - /* advance parity buffer */ - if ((status >= 0) && (status <= tt)) - /* copy back corrected data only if all is OK */ - memcpy(L3_buf, parityword, P); - L3_buf += P; - - /* advance message buffer */ - if (remaining < k_post) { - if ((status >= 0) && (status <= tt)) - /* copy back corrected data only if all is OK */ - memcpy(L4_buf, codeword, remaining); - L4_buf += remaining; - remaining = 0; - - } else { - if ((status >= 0) && (status <= tt)) - /* copy back corrected data only if all is OK */ - memcpy(L4_buf, codeword, k_post); - L4_buf += k_post; - remaining -= k_post; - - } - } - } - - /* give back the L4_buf address */ - if (L4_bufp) - *L4_bufp = L4_buf; - - /* print summary */ - if (!conn) { - - if (errnum) - opj_event_msg(j2k->cinfo, EVT_INFO, "- %d symbol errors corrected (Ps=%.1e)\n", errnum, - (float) errnum / (float) LDPepb); - if (errflag) - opj_event_msg(j2k->cinfo, EVT_INFO, "- there were unrecoverable errors\n"); - - } - - cio_seek(j2k->cio, orig_pos); - - return OPJ_TRUE; + unsigned char **L4_bufp) +{ + + /* Operating buffer */ + unsigned char codeword[NN], *parityword; + + unsigned long int P, NN_P; + unsigned long int L1, L4; + int remaining, n_pre, k_pre, n_post, k_post; + + int status, tt; + + int orig_pos = cio_tell(j2k->cio); + + unsigned char *L1_buf, *L2_buf; + unsigned char *L3_buf, *L4_buf; + + unsigned long int LDPepb, Pepb; + unsigned short int Lepb; + unsigned char Depb; + char str1[25] = ""; + int myconn, errnum = 0; + opj_bool errflag = OPJ_FALSE; + + opj_cio_t *cio = j2k->cio; + + /* check for common errors */ + if (!buffer) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "The EPB pointer is a NULL buffer\n"); + return OPJ_FALSE; + } + + /* set bignesses */ + L1 = pre_len + 13; + + /* pre-data correction */ + switch (type) { + + case 0: + /* MH EPB */ + k_pre = 64; + n_pre = 160; + break; + + case 1: + /* TPH EPB */ + k_pre = 25; + n_pre = 80; + break; + + case 2: + /* other EPBs */ + k_pre = 13; + n_pre = 40; + break; + + case 3: + /* automatic setup */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Auto. setup not yet implemented\n"); + return OPJ_FALSE; + break; + + default: + /* unknown type */ + opj_event_msg(j2k->cinfo, EVT_ERROR, "Unknown expected EPB type\n"); + return OPJ_FALSE; + break; + + } + + /* Initialize RS structures */ + P = n_pre - k_pre; + NN_P = NN - P; + tt = (int) floor((float) P / 2.0F); /* correction capability of the code */ + memset(codeword, 0, NN); + parityword = codeword + NN_P; + init_rs(NN_P); + + /* Correct pre-data message words */ + L1_buf = buffer - pre_len; + L2_buf = buffer + 13; + remaining = L1; + while (remaining) { + + /* always zero-pad codewords */ + /* (this is required, since after decoding the zeros in the long codeword + could change, and keep unchanged in subsequent calls) */ + memset(codeword, 0, NN); + + /* copy codeword buffer into message bytes */ + if (remaining < k_pre) + memcpy(codeword, L1_buf, remaining); + else + memcpy(codeword, L1_buf, k_pre); + + /* copy redundancy buffer in parity bytes */ + memcpy(parityword, L2_buf, P); + + /* Decode the buffer and possibly obtain corrected bytes */ + status = eras_dec_rs(codeword, NULL, 0); + if (status == -1) { + /*if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_WARNING, + "Possible decoding error in codeword @ position #%d\n", (L1_buf - buffer) / k_pre);*/ + errflag = OPJ_TRUE; + /* we can try to safely get out from the function: + if we are here, either this is not an EPB or the first codeword + is too damaged to be helpful */ + /*return OPJ_FALSE;*/ + + } else if (status == 0) { + /*if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/ + + } else if (status <= tt) { + /* it has corrected 0 <= errs <= tt */ + /*if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/ + errnum += status; + + } else { + /*if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n"); + return OPJ_FALSE;*/ + errflag = OPJ_TRUE; + } + + + /* advance parity buffer */ + if ((status >= 0) && (status <= tt)) + /* copy back corrected parity only if all is OK */ + memcpy(L2_buf, parityword, P); + L2_buf += P; + + /* advance message buffer */ + if (remaining < k_pre) { + if ((status >= 0) && (status <= tt)) + /* copy back corrected data only if all is OK */ + memcpy(L1_buf, codeword, remaining); + L1_buf += remaining; + remaining = 0; + + } else { + if ((status >= 0) && (status <= tt)) + /* copy back corrected data only if all is OK */ + memcpy(L1_buf, codeword, k_pre); + L1_buf += k_pre; + remaining -= k_pre; + + } + } + + /* print summary */ + if (!conn) { + + /*if (errnum) + opj_event_msg(j2k->cinfo, EVT_INFO, "+ %d symbol errors corrected (Ps=%.1e)\n", errnum, + (float) errnum / ((float) n_pre * (float) L1 / (float) k_pre));*/ + if (errflag) { + /*opj_event_msg(j2k->cinfo, EVT_INFO, "+ there were unrecoverable errors\n");*/ + return OPJ_FALSE; + } + + } + + /* presumably, now, EPB parameters are correct */ + /* let's get them */ + + /* Simply read the EPB parameters */ + if (conn) + cio->bp = buffer; + cio_skip(cio, 2); /* the marker */ + Lepb = cio_read(cio, 2); + Depb = cio_read(cio, 1); + LDPepb = cio_read(cio, 4); + Pepb = cio_read(cio, 4); + + /* What does Pepb tells us about the protection method? */ + if (((Pepb & 0xF0000000) >> 28) == 0) + sprintf(str1, "pred"); /* predefined */ + else if (((Pepb & 0xF0000000) >> 28) == 1) + sprintf(str1, "crc-%lu", 16 * ((Pepb & 0x00000001) + 1)); /* CRC mode */ + else if (((Pepb & 0xF0000000) >> 28) == 2) + sprintf(str1, "rs(%lu,32)", (Pepb & 0x0000FF00) >> 8); /* RS mode */ + else if (Pepb == 0xFFFFFFFF) + sprintf(str1, "nometh"); /* RS mode */ + else + sprintf(str1, "unknown"); /* unknown */ + + /* Now we write them to screen */ + if (!conn && post_len) + opj_event_msg(j2k->cinfo, EVT_INFO, + "EPB(%d): (%sl, %sp, %u), %lu, %s\n", + cio_tell(cio) - 13, + (Depb & 0x40) ? "" : "n", /* latest EPB or not? */ + (Depb & 0x80) ? "" : "n", /* packed or unpacked EPB? */ + (Depb & 0x3F), /* EPB index value */ + LDPepb, /*length of the data protected by the EPB */ + str1); /* protection method */ + + + /* well, we need to investigate how long is the connected length of packed EPBs */ + myconn = Lepb + 2; + if ((Depb & 0x40) == 0) /* not latest in header */ + jpwl_epb_correct(j2k, /* J2K decompressor handle */ + buffer + Lepb + 2, /* pointer to next EPB in codestream buffer */ + 2, /* EPB type: should be of other type */ + 0, /* only EPB fields */ + 0, /* do not look after */ + &myconn, + NULL + ); + if (conn) + *conn += myconn; + + /*if (!conn) + printf("connected = %d\n", myconn);*/ + + /*cio_seek(j2k->cio, orig_pos); + return OPJ_TRUE;*/ + + /* post-data + the position of L4 buffer is at the end of currently connected EPBs + */ + if (!(L4_bufp)) + L4_buf = buffer + myconn; + else if (!(*L4_bufp)) + L4_buf = buffer + myconn; + else + L4_buf = *L4_bufp; + if (post_len == -1) + L4 = LDPepb - pre_len - 13; + else if (post_len == 0) + L4 = 0; + else + L4 = post_len; + + L3_buf = L2_buf; + + /* Do a further check here on the read parameters */ + if (L4 > (unsigned long) cio_numbytesleft(j2k->cio)) + /* overflow */ + return OPJ_FALSE; + + /* we are ready for decoding the remaining data */ + if (((Pepb & 0xF0000000) >> 28) == 1) { + /* CRC here */ + if ((16 * ((Pepb & 0x00000001) + 1)) == 16) { + + /* CRC-16 */ + unsigned short int mycrc = 0x0000, filecrc = 0x0000; + + /* compute the CRC field */ + remaining = L4; + while (remaining--) + jpwl_updateCRC16(&mycrc, *(L4_buf++)); + + /* read the CRC field */ + filecrc = *(L3_buf++) << 8; + filecrc |= *(L3_buf++); + + /* check the CRC field */ + if (mycrc == filecrc) { + if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n"); + } else { + if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc, mycrc); + errflag = OPJ_TRUE; + } + } + + if ((16 * ((Pepb & 0x00000001) + 1)) == 32) { + + /* CRC-32 */ + unsigned long int mycrc = 0x00000000, filecrc = 0x00000000; + + /* compute the CRC field */ + remaining = L4; + while (remaining--) + jpwl_updateCRC32(&mycrc, *(L4_buf++)); + + /* read the CRC field */ + filecrc = *(L3_buf++) << 24; + filecrc |= *(L3_buf++) << 16; + filecrc |= *(L3_buf++) << 8; + filecrc |= *(L3_buf++); + + /* check the CRC field */ + if (mycrc == filecrc) { + if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_INFO, "- CRC is OK\n"); + } else { + if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_WARNING, "- CRC is KO (r=%d, c=%d)\n", filecrc, mycrc); + errflag = OPJ_TRUE; + } + } + + } else if (Pepb == 0xFFFFFFFF) { + /* no method */ + + /* advance without doing anything */ + remaining = L4; + while (remaining--) + L4_buf++; + + } else if ((((Pepb & 0xF0000000) >> 28) == 2) || (((Pepb & 0xF0000000) >> 28) == 0)) { + /* RS coding here */ + + if (((Pepb & 0xF0000000) >> 28) == 0) { + + k_post = k_pre; + n_post = n_pre; + + } else { + + k_post = 32; + n_post = (Pepb & 0x0000FF00) >> 8; + } + + /* Initialize RS structures */ + P = n_post - k_post; + NN_P = NN - P; + tt = (int) floor((float) P / 2.0F); /* again, correction capability */ + memset(codeword, 0, NN); + parityword = codeword + NN_P; + init_rs(NN_P); + + /* Correct post-data message words */ + /*L4_buf = buffer + Lepb + 2;*/ + L3_buf = L2_buf; + remaining = L4; + while (remaining) { + + /* always zero-pad codewords */ + /* (this is required, since after decoding the zeros in the long codeword + could change, and keep unchanged in subsequent calls) */ + memset(codeword, 0, NN); + + /* copy codeword buffer into message bytes */ + if (remaining < k_post) + memcpy(codeword, L4_buf, remaining); + else + memcpy(codeword, L4_buf, k_post); + + /* copy redundancy buffer in parity bytes */ + memcpy(parityword, L3_buf, P); + + /* Decode the buffer and possibly obtain corrected bytes */ + status = eras_dec_rs(codeword, NULL, 0); + if (status == -1) { + /*if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_WARNING, + "Possible decoding error in codeword @ position #%d\n", (L4_buf - (buffer + Lepb + 2)) / k_post);*/ + errflag = OPJ_TRUE; + + } else if (status == 0) { + /*if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_INFO, "codeword is correctly decoded\n");*/ + + } else if (status <= tt) { + /*if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_WARNING, "%d errors corrected in codeword\n", status);*/ + errnum += status; + + } else { + /*if (conn == NULL) + opj_event_msg(j2k->cinfo, EVT_WARNING, "EPB correction capability exceeded\n"); + return OPJ_FALSE;*/ + errflag = OPJ_TRUE; + } + + + /* advance parity buffer */ + if ((status >= 0) && (status <= tt)) + /* copy back corrected data only if all is OK */ + memcpy(L3_buf, parityword, P); + L3_buf += P; + + /* advance message buffer */ + if (remaining < k_post) { + if ((status >= 0) && (status <= tt)) + /* copy back corrected data only if all is OK */ + memcpy(L4_buf, codeword, remaining); + L4_buf += remaining; + remaining = 0; + + } else { + if ((status >= 0) && (status <= tt)) + /* copy back corrected data only if all is OK */ + memcpy(L4_buf, codeword, k_post); + L4_buf += k_post; + remaining -= k_post; + + } + } + } + + /* give back the L4_buf address */ + if (L4_bufp) + *L4_bufp = L4_buf; + + /* print summary */ + if (!conn) { + + if (errnum) + opj_event_msg(j2k->cinfo, EVT_INFO, "- %d symbol errors corrected (Ps=%.1e)\n", errnum, + (float) errnum / (float) LDPepb); + if (errflag) + opj_event_msg(j2k->cinfo, EVT_INFO, "- there were unrecoverable errors\n"); + + } + + cio_seek(j2k->cio, orig_pos); + + return OPJ_TRUE; } -void jpwl_epc_write(opj_j2k_t *j2k, jpwl_epc_ms_t *epc, unsigned char *buf) { +void jpwl_epc_write(opj_j2k_t *j2k, jpwl_epc_ms_t *epc, unsigned char *buf) +{ - /* Marker */ - *(buf++) = (unsigned char) (J2K_MS_EPC >> 8); - *(buf++) = (unsigned char) (J2K_MS_EPC >> 0); + /* Marker */ + *(buf++) = (unsigned char) (J2K_MS_EPC >> 8); + *(buf++) = (unsigned char) (J2K_MS_EPC >> 0); - /* Lepc */ - *(buf++) = (unsigned char) (epc->Lepc >> 8); - *(buf++) = (unsigned char) (epc->Lepc >> 0); + /* Lepc */ + *(buf++) = (unsigned char) (epc->Lepc >> 8); + *(buf++) = (unsigned char) (epc->Lepc >> 0); - /* Pcrc */ - *(buf++) = (unsigned char) (epc->Pcrc >> 8); - *(buf++) = (unsigned char) (epc->Pcrc >> 0); + /* Pcrc */ + *(buf++) = (unsigned char) (epc->Pcrc >> 8); + *(buf++) = (unsigned char) (epc->Pcrc >> 0); - /* DL */ - *(buf++) = (unsigned char) (epc->DL >> 24); - *(buf++) = (unsigned char) (epc->DL >> 16); - *(buf++) = (unsigned char) (epc->DL >> 8); - *(buf++) = (unsigned char) (epc->DL >> 0); + /* DL */ + *(buf++) = (unsigned char) (epc->DL >> 24); + *(buf++) = (unsigned char) (epc->DL >> 16); + *(buf++) = (unsigned char) (epc->DL >> 8); + *(buf++) = (unsigned char) (epc->DL >> 0); - /* Pepc */ - *(buf++) = (unsigned char) (epc->Pepc >> 0); + /* Pepc */ + *(buf++) = (unsigned char) (epc->Pepc >> 0); - /* Data */ - /*memcpy(buf, epc->data, (size_t) epc->Lepc - 9);*/ - memset(buf, 0, (size_t) epc->Lepc - 9); + /* Data */ + /*memcpy(buf, epc->data, (size_t) epc->Lepc - 9);*/ + memset(buf, 0, (size_t) epc->Lepc - 9); - /* update markers struct */ - j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, -1, epc->Lepc + 2); + /* update markers struct */ + j2k_add_marker(j2k->cstr_info, J2K_MS_EPC, -1, epc->Lepc + 2); } int jpwl_esds_add(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int *jwmarker_num, - int comps, unsigned char addrm, unsigned char ad_size, - unsigned char senst, unsigned char se_size, - double place_pos, int tileno) { + int comps, unsigned char addrm, unsigned char ad_size, + unsigned char senst, unsigned char se_size, + double place_pos, int tileno) +{ - return 0; + return 0; } -jpwl_esd_ms_t *jpwl_esd_create(opj_j2k_t *j2k, int comp, - unsigned char addrm, unsigned char ad_size, - unsigned char senst, int se_size, int tileno, - unsigned long int svalnum, void *sensval) { - - jpwl_esd_ms_t *esd = NULL; - - /* Alloc space */ - if (!(esd = (jpwl_esd_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_esd_ms_t)))) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for ESD MS\n"); - return NULL; - }; - - /* if relative sensitivity, activate byte range mode */ - if (senst == 0) - addrm = 1; - - /* size of sensval's ... */ - if ((ad_size != 0) && (ad_size != 2) && (ad_size != 4)) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "Address size %d for ESD MS is forbidden\n", ad_size); - return NULL; - } - if ((se_size != 1) && (se_size != 2)) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "Sensitivity size %d for ESD MS is forbidden\n", se_size); - return NULL; - } - - /* ... depends on the addressing mode */ - switch (addrm) { - - /* packet mode */ - case (0): - ad_size = 0; /* as per the standard */ - esd->sensval_size = (unsigned int)se_size; - break; - - /* byte range */ - case (1): - /* auto sense address size */ - if (ad_size == 0) - /* if there are more than 66% of (2^16 - 1) bytes, switch to 4 bytes - (we keep space for possible EPBs being inserted) */ - ad_size = (j2k->cstr_info->codestream_size > (1 * 65535 / 3)) ? 4 : 2; - esd->sensval_size = ad_size + ad_size + se_size; - break; - - /* packet range */ - case (2): - /* auto sense address size */ - if (ad_size == 0) - /* if there are more than 2^16 - 1 packets, switch to 4 bytes */ - ad_size = (j2k->cstr_info->packno > 65535) ? 4 : 2; - esd->sensval_size = ad_size + ad_size + se_size; - break; - - case (3): - opj_event_msg(j2k->cinfo, EVT_ERROR, "Address mode %d for ESD MS is unimplemented\n", addrm); - return NULL; - - default: - opj_event_msg(j2k->cinfo, EVT_ERROR, "Address mode %d for ESD MS is forbidden\n", addrm); - return NULL; - } - - /* set or unset sensitivity values */ - if (svalnum <= 0) { - - switch (senst) { - - /* just based on the portions of a codestream */ - case (0): - /* MH + no. of THs + no. of packets */ - svalnum = 1 + (j2k->cstr_info->tw * j2k->cstr_info->th) * (1 + j2k->cstr_info->packno); - break; - - /* all the ones that are based on the packets */ - default: - if (tileno < 0) - /* MH: all the packets and all the tiles info is written */ - svalnum = j2k->cstr_info->tw * j2k->cstr_info->th * j2k->cstr_info->packno; - else - /* TPH: only that tile info is written */ - svalnum = j2k->cstr_info->packno; - break; - - } - } - - /* fill private fields */ - esd->senst = senst; - esd->ad_size = ad_size; - esd->se_size = se_size; - esd->addrm = addrm; - esd->svalnum = svalnum; - esd->numcomps = j2k->image->numcomps; - esd->tileno = tileno; - - /* Set the ESD parameters */ - /* length, excluding data field */ - if (esd->numcomps < 257) - esd->Lesd = 4 + (unsigned short int) (esd->svalnum * esd->sensval_size); - else - esd->Lesd = 5 + (unsigned short int) (esd->svalnum * esd->sensval_size); - - /* component data field */ - if (comp >= 0) - esd->Cesd = comp; - else - /* we are averaging */ - esd->Cesd = 0; - - /* Pesd field */ - esd->Pesd = 0x00; - esd->Pesd |= (esd->addrm & 0x03) << 6; /* addressing mode */ - esd->Pesd |= (esd->senst & 0x07) << 3; /* sensitivity type */ - esd->Pesd |= ((esd->se_size >> 1) & 0x01) << 2; /* sensitivity size */ - esd->Pesd |= ((esd->ad_size >> 2) & 0x01) << 1; /* addressing size */ - esd->Pesd |= (comp < 0) ? 0x01 : 0x00; /* averaging components */ - - /* if pointer to sensval is NULL, we can fill data field by ourselves */ - if (!sensval) { - - /* old code moved to jpwl_esd_fill() */ - esd->data = NULL; - - } else { - /* we set the data field as the sensitivity values poinnter passed to the function */ - esd->data = (unsigned char *) sensval; - } - - return (esd); +jpwl_esd_ms_t *jpwl_esd_create(opj_j2k_t *j2k, int comp, + unsigned char addrm, unsigned char ad_size, + unsigned char senst, int se_size, int tileno, + unsigned long int svalnum, void *sensval) +{ + + jpwl_esd_ms_t *esd = NULL; + + /* Alloc space */ + if (!(esd = (jpwl_esd_ms_t *) opj_malloc((size_t) 1 * sizeof (jpwl_esd_ms_t)))) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "Could not allocate room for ESD MS\n"); + return NULL; + }; + + /* if relative sensitivity, activate byte range mode */ + if (senst == 0) + addrm = 1; + + /* size of sensval's ... */ + if ((ad_size != 0) && (ad_size != 2) && (ad_size != 4)) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "Address size %d for ESD MS is forbidden\n", ad_size); + return NULL; + } + if ((se_size != 1) && (se_size != 2)) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "Sensitivity size %d for ESD MS is forbidden\n", se_size); + return NULL; + } + + /* ... depends on the addressing mode */ + switch (addrm) { + + /* packet mode */ + case (0): + ad_size = 0; /* as per the standard */ + esd->sensval_size = (unsigned int)se_size; + break; + + /* byte range */ + case (1): + /* auto sense address size */ + if (ad_size == 0) + /* if there are more than 66% of (2^16 - 1) bytes, switch to 4 bytes + (we keep space for possible EPBs being inserted) */ + ad_size = (j2k->cstr_info->codestream_size > (1 * 65535 / 3)) ? 4 : 2; + esd->sensval_size = ad_size + ad_size + se_size; + break; + + /* packet range */ + case (2): + /* auto sense address size */ + if (ad_size == 0) + /* if there are more than 2^16 - 1 packets, switch to 4 bytes */ + ad_size = (j2k->cstr_info->packno > 65535) ? 4 : 2; + esd->sensval_size = ad_size + ad_size + se_size; + break; + + case (3): + opj_event_msg(j2k->cinfo, EVT_ERROR, "Address mode %d for ESD MS is unimplemented\n", addrm); + return NULL; + + default: + opj_event_msg(j2k->cinfo, EVT_ERROR, "Address mode %d for ESD MS is forbidden\n", addrm); + return NULL; + } + + /* set or unset sensitivity values */ + if (svalnum <= 0) { + + switch (senst) { + + /* just based on the portions of a codestream */ + case (0): + /* MH + no. of THs + no. of packets */ + svalnum = 1 + (j2k->cstr_info->tw * j2k->cstr_info->th) * (1 + j2k->cstr_info->packno); + break; + + /* all the ones that are based on the packets */ + default: + if (tileno < 0) + /* MH: all the packets and all the tiles info is written */ + svalnum = j2k->cstr_info->tw * j2k->cstr_info->th * j2k->cstr_info->packno; + else + /* TPH: only that tile info is written */ + svalnum = j2k->cstr_info->packno; + break; + + } + } + + /* fill private fields */ + esd->senst = senst; + esd->ad_size = ad_size; + esd->se_size = se_size; + esd->addrm = addrm; + esd->svalnum = svalnum; + esd->numcomps = j2k->image->numcomps; + esd->tileno = tileno; + + /* Set the ESD parameters */ + /* length, excluding data field */ + if (esd->numcomps < 257) + esd->Lesd = 4 + (unsigned short int) (esd->svalnum * esd->sensval_size); + else + esd->Lesd = 5 + (unsigned short int) (esd->svalnum * esd->sensval_size); + + /* component data field */ + if (comp >= 0) + esd->Cesd = comp; + else + /* we are averaging */ + esd->Cesd = 0; + + /* Pesd field */ + esd->Pesd = 0x00; + esd->Pesd |= (esd->addrm & 0x03) << 6; /* addressing mode */ + esd->Pesd |= (esd->senst & 0x07) << 3; /* sensitivity type */ + esd->Pesd |= ((esd->se_size >> 1) & 0x01) << 2; /* sensitivity size */ + esd->Pesd |= ((esd->ad_size >> 2) & 0x01) << 1; /* addressing size */ + esd->Pesd |= (comp < 0) ? 0x01 : 0x00; /* averaging components */ + + /* if pointer to sensval is NULL, we can fill data field by ourselves */ + if (!sensval) { + + /* old code moved to jpwl_esd_fill() */ + esd->data = NULL; + + } else { + /* we set the data field as the sensitivity values poinnter passed to the function */ + esd->data = (unsigned char *) sensval; + } + + return (esd); } -opj_bool jpwl_esd_fill(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf) { - - int i; - unsigned long int vv; - unsigned long int addr1 = 0L, addr2 = 0L; - double dvalue = 0.0, Omax2, tmp, TSE = 0.0, MSE, oldMSE = 0.0, PSNR, oldPSNR = 0.0; - unsigned short int pfpvalue; - unsigned long int addrmask = 0x00000000; - opj_bool doneMH = OPJ_FALSE, doneTPH = OPJ_FALSE; - - /* sensitivity values in image info are as follows: - - for each tile, distotile is the starting distortion for that tile, sum of all components - - for each packet in a tile, disto is the distortion reduction caused by that packet to that tile - - the TSE for a single tile should be given by distotile - sum(disto) , for all components - - the MSE for a single tile is given by TSE / nbpix , for all components - - the PSNR for a single tile is given by 10*log10( Omax^2 / MSE) , for all components - (Omax is given by 2^bpp - 1 for unsigned images and by 2^(bpp - 1) - 1 for signed images - */ - - /* browse all components and find Omax */ - Omax2 = 0.0; - for (i = 0; i < j2k->image->numcomps; i++) { - tmp = pow(2.0, (double) (j2k->image->comps[i].sgnd ? - (j2k->image->comps[i].bpp - 1) : (j2k->image->comps[i].bpp))) - 1; - if (tmp > Omax2) - Omax2 = tmp; - } - Omax2 = Omax2 * Omax2; - - /* if pointer of esd->data is not null, simply write down all the values byte by byte */ - if (esd->data) { - for (i = 0; i < (int) esd->svalnum; i++) - *(buf++) = esd->data[i]; - return OPJ_TRUE; - } - - /* addressing mask */ - if (esd->ad_size == 2) - addrmask = 0x0000FFFF; /* two bytes */ - else - addrmask = 0xFFFFFFFF; /* four bytes */ - - /* set on precise point where sensitivity starts */ - if (esd->numcomps < 257) - buf += 6; - else - buf += 7; - - /* let's fill the data fields */ - for (vv = (esd->tileno < 0) ? 0 : (j2k->cstr_info->packno * esd->tileno); vv < esd->svalnum; vv++) { - - int thistile = vv / j2k->cstr_info->packno, thispacket = vv % j2k->cstr_info->packno; - - /* skip for the hack some lines below */ - if (thistile == j2k->cstr_info->tw * j2k->cstr_info->th) - break; - - /* starting tile distortion */ - if (thispacket == 0) { - TSE = j2k->cstr_info->tile[thistile].distotile; - oldMSE = TSE / j2k->cstr_info->tile[thistile].numpix; - oldPSNR = 10.0 * log10(Omax2 / oldMSE); - } - - /* TSE */ - TSE -= j2k->cstr_info->tile[thistile].packet[thispacket].disto; - - /* MSE */ - MSE = TSE / j2k->cstr_info->tile[thistile].numpix; - - /* PSNR */ - PSNR = 10.0 * log10(Omax2 / MSE); - - /* fill the address range */ - switch (esd->addrm) { - - /* packet mode */ - case (0): - /* nothing, there is none */ - break; - - /* byte range */ - case (1): - /* start address of packet */ - addr1 = (j2k->cstr_info->tile[thistile].packet[thispacket].start_pos) & addrmask; - /* end address of packet */ - addr2 = (j2k->cstr_info->tile[thistile].packet[thispacket].end_pos) & addrmask; - break; - - /* packet range */ - case (2): - /* not implemented here */ - opj_event_msg(j2k->cinfo, EVT_WARNING, "Addressing mode packet_range is not implemented\n"); - break; - - /* unknown addressing method */ - default: - /* not implemented here */ - opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown addressing mode\n"); - break; - - } - - /* hack for writing relative sensitivity of MH and TPHs */ - if ((esd->senst == 0) && (thispacket == 0)) { - - /* possible MH */ - if ((thistile == 0) && !doneMH) { - /* we have to manage MH addresses */ - addr1 = 0; /* start of MH */ - addr2 = j2k->cstr_info->main_head_end; /* end of MH */ - /* set special dvalue for this MH */ - dvalue = -10.0; - doneMH = OPJ_TRUE; /* don't come here anymore */ - vv--; /* wrap back loop counter */ - - } else if (!doneTPH) { - /* we have to manage TPH addresses */ - addr1 = j2k->cstr_info->tile[thistile].start_pos; - addr2 = j2k->cstr_info->tile[thistile].end_header; - /* set special dvalue for this TPH */ - dvalue = -1.0; - doneTPH = OPJ_TRUE; /* don't come here till the next tile */ - vv--; /* wrap back loop counter */ - } - - } else - doneTPH = OPJ_FALSE; /* reset TPH counter */ - - /* write the addresses to the buffer */ - switch (esd->ad_size) { - - case (0): - /* do nothing */ - break; - - case (2): - /* two bytes */ - *(buf++) = (unsigned char) (addr1 >> 8); - *(buf++) = (unsigned char) (addr1 >> 0); - *(buf++) = (unsigned char) (addr2 >> 8); - *(buf++) = (unsigned char) (addr2 >> 0); - break; - - case (4): - /* four bytes */ - *(buf++) = (unsigned char) (addr1 >> 24); - *(buf++) = (unsigned char) (addr1 >> 16); - *(buf++) = (unsigned char) (addr1 >> 8); - *(buf++) = (unsigned char) (addr1 >> 0); - *(buf++) = (unsigned char) (addr2 >> 24); - *(buf++) = (unsigned char) (addr2 >> 16); - *(buf++) = (unsigned char) (addr2 >> 8); - *(buf++) = (unsigned char) (addr2 >> 0); - break; - - default: - /* do nothing */ - break; - } - - - /* let's fill the value field */ - switch (esd->senst) { - - /* relative sensitivity */ - case (0): - /* we just write down the packet ordering */ - if (dvalue == -10) - /* MH */ - dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */ - else if (dvalue == -1) - /* TPH */ - dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */ - else - /* packet: first is most important, and then in decreasing order - down to the last, which counts for 1 */ - dvalue = jpwl_pfp_to_double((unsigned short) (j2k->cstr_info->packno - thispacket), esd->se_size); - break; - - /* MSE */ - case (1): - /* !!! WRONG: let's put here disto field of packets !!! */ - dvalue = MSE; - break; - - /* MSE reduction */ - case (2): - dvalue = oldMSE - MSE; - oldMSE = MSE; - break; - - /* PSNR */ - case (3): - dvalue = PSNR; - break; - - /* PSNR increase */ - case (4): - dvalue = PSNR - oldPSNR; - oldPSNR = PSNR; - break; - - /* MAXERR */ - case (5): - dvalue = 0.0; - opj_event_msg(j2k->cinfo, EVT_WARNING, "MAXERR sensitivity mode is not implemented\n"); - break; - - /* TSE */ - case (6): - dvalue = TSE; - break; - - /* reserved */ - case (7): - dvalue = 0.0; - opj_event_msg(j2k->cinfo, EVT_WARNING, "Reserved sensitivity mode is not implemented\n"); - break; - - default: - dvalue = 0.0; - break; - } - - /* compute the pseudo-floating point value */ - pfpvalue = jpwl_double_to_pfp(dvalue, esd->se_size); - - /* write the pfp value to the buffer */ - switch (esd->se_size) { - - case (1): - /* one byte */ - *(buf++) = (unsigned char) (pfpvalue >> 0); - break; - - case (2): - /* two bytes */ - *(buf++) = (unsigned char) (pfpvalue >> 8); - *(buf++) = (unsigned char) (pfpvalue >> 0); - break; - } - - } - - return OPJ_TRUE; +opj_bool jpwl_esd_fill(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf) +{ + + int i; + unsigned long int vv; + unsigned long int addr1 = 0L, addr2 = 0L; + double dvalue = 0.0, Omax2, tmp, TSE = 0.0, MSE, oldMSE = 0.0, PSNR, oldPSNR = 0.0; + unsigned short int pfpvalue; + unsigned long int addrmask = 0x00000000; + opj_bool doneMH = OPJ_FALSE, doneTPH = OPJ_FALSE; + + /* sensitivity values in image info are as follows: + - for each tile, distotile is the starting distortion for that tile, sum of all components + - for each packet in a tile, disto is the distortion reduction caused by that packet to that tile + - the TSE for a single tile should be given by distotile - sum(disto) , for all components + - the MSE for a single tile is given by TSE / nbpix , for all components + - the PSNR for a single tile is given by 10*log10( Omax^2 / MSE) , for all components + (Omax is given by 2^bpp - 1 for unsigned images and by 2^(bpp - 1) - 1 for signed images + */ + + /* browse all components and find Omax */ + Omax2 = 0.0; + for (i = 0; i < j2k->image->numcomps; i++) { + tmp = pow(2.0, (double) (j2k->image->comps[i].sgnd ? + (j2k->image->comps[i].bpp - 1) : (j2k->image->comps[i].bpp))) - 1; + if (tmp > Omax2) + Omax2 = tmp; + } + Omax2 = Omax2 * Omax2; + + /* if pointer of esd->data is not null, simply write down all the values byte by byte */ + if (esd->data) { + for (i = 0; i < (int) esd->svalnum; i++) + *(buf++) = esd->data[i]; + return OPJ_TRUE; + } + + /* addressing mask */ + if (esd->ad_size == 2) + addrmask = 0x0000FFFF; /* two bytes */ + else + addrmask = 0xFFFFFFFF; /* four bytes */ + + /* set on precise point where sensitivity starts */ + if (esd->numcomps < 257) + buf += 6; + else + buf += 7; + + /* let's fill the data fields */ + for (vv = (esd->tileno < 0) ? 0 : (j2k->cstr_info->packno * esd->tileno); vv < esd->svalnum; vv++) { + + int thistile = vv / j2k->cstr_info->packno, thispacket = vv % j2k->cstr_info->packno; + + /* skip for the hack some lines below */ + if (thistile == j2k->cstr_info->tw * j2k->cstr_info->th) + break; + + /* starting tile distortion */ + if (thispacket == 0) { + TSE = j2k->cstr_info->tile[thistile].distotile; + oldMSE = TSE / j2k->cstr_info->tile[thistile].numpix; + oldPSNR = 10.0 * log10(Omax2 / oldMSE); + } + + /* TSE */ + TSE -= j2k->cstr_info->tile[thistile].packet[thispacket].disto; + + /* MSE */ + MSE = TSE / j2k->cstr_info->tile[thistile].numpix; + + /* PSNR */ + PSNR = 10.0 * log10(Omax2 / MSE); + + /* fill the address range */ + switch (esd->addrm) { + + /* packet mode */ + case (0): + /* nothing, there is none */ + break; + + /* byte range */ + case (1): + /* start address of packet */ + addr1 = (j2k->cstr_info->tile[thistile].packet[thispacket].start_pos) & addrmask; + /* end address of packet */ + addr2 = (j2k->cstr_info->tile[thistile].packet[thispacket].end_pos) & addrmask; + break; + + /* packet range */ + case (2): + /* not implemented here */ + opj_event_msg(j2k->cinfo, EVT_WARNING, "Addressing mode packet_range is not implemented\n"); + break; + + /* unknown addressing method */ + default: + /* not implemented here */ + opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown addressing mode\n"); + break; + + } + + /* hack for writing relative sensitivity of MH and TPHs */ + if ((esd->senst == 0) && (thispacket == 0)) { + + /* possible MH */ + if ((thistile == 0) && !doneMH) { + /* we have to manage MH addresses */ + addr1 = 0; /* start of MH */ + addr2 = j2k->cstr_info->main_head_end; /* end of MH */ + /* set special dvalue for this MH */ + dvalue = -10.0; + doneMH = OPJ_TRUE; /* don't come here anymore */ + vv--; /* wrap back loop counter */ + + } else if (!doneTPH) { + /* we have to manage TPH addresses */ + addr1 = j2k->cstr_info->tile[thistile].start_pos; + addr2 = j2k->cstr_info->tile[thistile].end_header; + /* set special dvalue for this TPH */ + dvalue = -1.0; + doneTPH = OPJ_TRUE; /* don't come here till the next tile */ + vv--; /* wrap back loop counter */ + } + + } else + doneTPH = OPJ_FALSE; /* reset TPH counter */ + + /* write the addresses to the buffer */ + switch (esd->ad_size) { + + case (0): + /* do nothing */ + break; + + case (2): + /* two bytes */ + *(buf++) = (unsigned char) (addr1 >> 8); + *(buf++) = (unsigned char) (addr1 >> 0); + *(buf++) = (unsigned char) (addr2 >> 8); + *(buf++) = (unsigned char) (addr2 >> 0); + break; + + case (4): + /* four bytes */ + *(buf++) = (unsigned char) (addr1 >> 24); + *(buf++) = (unsigned char) (addr1 >> 16); + *(buf++) = (unsigned char) (addr1 >> 8); + *(buf++) = (unsigned char) (addr1 >> 0); + *(buf++) = (unsigned char) (addr2 >> 24); + *(buf++) = (unsigned char) (addr2 >> 16); + *(buf++) = (unsigned char) (addr2 >> 8); + *(buf++) = (unsigned char) (addr2 >> 0); + break; + + default: + /* do nothing */ + break; + } + + + /* let's fill the value field */ + switch (esd->senst) { + + /* relative sensitivity */ + case (0): + /* we just write down the packet ordering */ + if (dvalue == -10) + /* MH */ + dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */ + else if (dvalue == -1) + /* TPH */ + dvalue = MAX_V1 + 1000.0; /* this will cause pfpvalue set to 0xFFFF */ + else + /* packet: first is most important, and then in decreasing order + down to the last, which counts for 1 */ + dvalue = jpwl_pfp_to_double((unsigned short) (j2k->cstr_info->packno - thispacket), esd->se_size); + break; + + /* MSE */ + case (1): + /* !!! WRONG: let's put here disto field of packets !!! */ + dvalue = MSE; + break; + + /* MSE reduction */ + case (2): + dvalue = oldMSE - MSE; + oldMSE = MSE; + break; + + /* PSNR */ + case (3): + dvalue = PSNR; + break; + + /* PSNR increase */ + case (4): + dvalue = PSNR - oldPSNR; + oldPSNR = PSNR; + break; + + /* MAXERR */ + case (5): + dvalue = 0.0; + opj_event_msg(j2k->cinfo, EVT_WARNING, "MAXERR sensitivity mode is not implemented\n"); + break; + + /* TSE */ + case (6): + dvalue = TSE; + break; + + /* reserved */ + case (7): + dvalue = 0.0; + opj_event_msg(j2k->cinfo, EVT_WARNING, "Reserved sensitivity mode is not implemented\n"); + break; + + default: + dvalue = 0.0; + break; + } + + /* compute the pseudo-floating point value */ + pfpvalue = jpwl_double_to_pfp(dvalue, esd->se_size); + + /* write the pfp value to the buffer */ + switch (esd->se_size) { + + case (1): + /* one byte */ + *(buf++) = (unsigned char) (pfpvalue >> 0); + break; + + case (2): + /* two bytes */ + *(buf++) = (unsigned char) (pfpvalue >> 8); + *(buf++) = (unsigned char) (pfpvalue >> 0); + break; + } + + } + + return OPJ_TRUE; } -opj_bool jpwl_esd_write(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf) { +opj_bool jpwl_esd_write(opj_j2k_t *j2k, jpwl_esd_ms_t *esd, unsigned char *buf) +{ - /* Marker */ - *(buf++) = (unsigned char) (J2K_MS_ESD >> 8); - *(buf++) = (unsigned char) (J2K_MS_ESD >> 0); + /* Marker */ + *(buf++) = (unsigned char) (J2K_MS_ESD >> 8); + *(buf++) = (unsigned char) (J2K_MS_ESD >> 0); - /* Lesd */ - *(buf++) = (unsigned char) (esd->Lesd >> 8); - *(buf++) = (unsigned char) (esd->Lesd >> 0); + /* Lesd */ + *(buf++) = (unsigned char) (esd->Lesd >> 8); + *(buf++) = (unsigned char) (esd->Lesd >> 0); - /* Cesd */ - if (esd->numcomps >= 257) - *(buf++) = (unsigned char) (esd->Cesd >> 8); - *(buf++) = (unsigned char) (esd->Cesd >> 0); + /* Cesd */ + if (esd->numcomps >= 257) + *(buf++) = (unsigned char) (esd->Cesd >> 8); + *(buf++) = (unsigned char) (esd->Cesd >> 0); - /* Pesd */ - *(buf++) = (unsigned char) (esd->Pesd >> 0); + /* Pesd */ + *(buf++) = (unsigned char) (esd->Pesd >> 0); - /* Data */ - if (esd->numcomps < 257) - memset(buf, 0xAA, (size_t) esd->Lesd - 4); - /*memcpy(buf, esd->data, (size_t) esd->Lesd - 4);*/ - else - memset(buf, 0xAA, (size_t) esd->Lesd - 5); - /*memcpy(buf, esd->data, (size_t) esd->Lesd - 5);*/ + /* Data */ + if (esd->numcomps < 257) + memset(buf, 0xAA, (size_t) esd->Lesd - 4); + /*memcpy(buf, esd->data, (size_t) esd->Lesd - 4);*/ + else + memset(buf, 0xAA, (size_t) esd->Lesd - 5); + /*memcpy(buf, esd->data, (size_t) esd->Lesd - 5);*/ - /* update markers struct */ - j2k_add_marker(j2k->cstr_info, J2K_MS_ESD, -1, esd->Lesd + 2); + /* update markers struct */ + j2k_add_marker(j2k->cstr_info, J2K_MS_ESD, -1, esd->Lesd + 2); - return OPJ_TRUE; + return OPJ_TRUE; } -unsigned short int jpwl_double_to_pfp(double V, int bytes) { - - unsigned short int em, e, m; - - switch (bytes) { - - case (1): - - if (V < MIN_V1) { - e = 0x0000; - m = 0x0000; - } else if (V > MAX_V1) { - e = 0x000F; - m = 0x000F; - } else { - e = (unsigned short int) (floor(log(V) * 1.44269504088896) / 4.0); - m = (unsigned short int) (0.5 + (V / (pow(2.0, (double) (4 * e))))); - } - em = ((e & 0x000F) << 4) + (m & 0x000F); - break; - - case (2): - - if (V < MIN_V2) { - e = 0x0000; - m = 0x0000; - } else if (V > MAX_V2) { - e = 0x001F; - m = 0x07FF; - } else { - e = (unsigned short int) floor(log(V) * 1.44269504088896) + 15; - m = (unsigned short int) (0.5 + 2048.0 * ((V / (pow(2.0, (double) e - 15.0))) - 1.0)); - } - em = ((e & 0x001F) << 11) + (m & 0x07FF); - break; - - default: - - em = 0x0000; - break; - }; - - return em; +unsigned short int jpwl_double_to_pfp(double V, int bytes) +{ + + unsigned short int em, e, m; + + switch (bytes) { + + case (1): + + if (V < MIN_V1) { + e = 0x0000; + m = 0x0000; + } else if (V > MAX_V1) { + e = 0x000F; + m = 0x000F; + } else { + e = (unsigned short int) (floor(log(V) * 1.44269504088896) / 4.0); + m = (unsigned short int) (0.5 + (V / (pow(2.0, (double) (4 * e))))); + } + em = ((e & 0x000F) << 4) + (m & 0x000F); + break; + + case (2): + + if (V < MIN_V2) { + e = 0x0000; + m = 0x0000; + } else if (V > MAX_V2) { + e = 0x001F; + m = 0x07FF; + } else { + e = (unsigned short int) floor(log(V) * 1.44269504088896) + 15; + m = (unsigned short int) (0.5 + 2048.0 * ((V / (pow(2.0, (double) e - 15.0))) - 1.0)); + } + em = ((e & 0x001F) << 11) + (m & 0x07FF); + break; + + default: + + em = 0x0000; + break; + }; + + return em; } -double jpwl_pfp_to_double(unsigned short int em, int bytes) { +double jpwl_pfp_to_double(unsigned short int em, int bytes) +{ - double V; + double V; - switch (bytes) { + switch (bytes) { - case 1: - V = (double) (em & 0x0F) * pow(2.0, (double) (em & 0xF0)); - break; + case 1: + V = (double) (em & 0x0F) * pow(2.0, (double) (em & 0xF0)); + break; - case 2: + case 2: - V = pow(2.0, (double) ((em & 0xF800) >> 11) - 15.0) * (1.0 + (double) (em & 0x07FF) / 2048.0); - break; + V = pow(2.0, (double) ((em & 0xF800) >> 11) - 15.0) * (1.0 + (double) (em & 0x07FF) / 2048.0); + break; - default: - V = 0.0; - break; + default: + V = 0.0; + break; - } + } - return V; + return V; } -opj_bool jpwl_update_info(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int jwmarker_num) { - - int mm; - unsigned long int addlen; - - opj_codestream_info_t *info = j2k->cstr_info; - int tileno, tpno, packno, numtiles = info->th * info->tw, numpacks = info->packno; - - if (!j2k || !jwmarker ) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "J2K handle or JPWL markers list badly allocated\n"); - return OPJ_FALSE; - } - - /* main_head_end: how many markers are there before? */ - addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - if (jwmarker[mm].pos < (unsigned long int) info->main_head_end) - addlen += jwmarker[mm].len + 2; - info->main_head_end += addlen; - - /* codestream_size: always increment with all markers */ - addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - addlen += jwmarker[mm].len + 2; - info->codestream_size += addlen; - - /* navigate through all the tiles */ - for (tileno = 0; tileno < numtiles; tileno++) { - - /* start_pos: increment with markers before SOT */ - addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].start_pos) - addlen += jwmarker[mm].len + 2; - info->tile[tileno].start_pos += addlen; - - /* end_header: increment with markers before of it */ - addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_header) - addlen += jwmarker[mm].len + 2; - info->tile[tileno].end_header += addlen; - - /* end_pos: increment with markers before the end of this tile */ - /* code is disabled, since according to JPWL no markers can be beyond TPH */ - addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_pos) - addlen += jwmarker[mm].len + 2; - info->tile[tileno].end_pos += addlen; - - /* navigate through all the tile parts */ - for (tpno = 0; tpno < info->tile[tileno].num_tps; tpno++) { - - /* start_pos: increment with markers before SOT */ - addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_start_pos) - addlen += jwmarker[mm].len + 2; - info->tile[tileno].tp[tpno].tp_start_pos += addlen; - - /* end_header: increment with markers before of it */ - addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_end_header) - addlen += jwmarker[mm].len + 2; - info->tile[tileno].tp[tpno].tp_end_header += addlen; - - /* end_pos: increment with markers before the end of this tile part */ - addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_end_pos) - addlen += jwmarker[mm].len + 2; - info->tile[tileno].tp[tpno].tp_end_pos += addlen; - - } - - /* navigate through all the packets in this tile */ - for (packno = 0; packno < numpacks; packno++) { - - /* start_pos: increment with markers before the packet */ - /* disabled for the same reason as before */ - addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - if (jwmarker[mm].pos <= (unsigned long int) info->tile[tileno].packet[packno].start_pos) - addlen += jwmarker[mm].len + 2; - info->tile[tileno].packet[packno].start_pos += addlen; - - /* end_ph_pos: increment with markers before the packet */ - /* disabled for the same reason as before */ - /*addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_ph_pos) - addlen += jwmarker[mm].len + 2;*/ - info->tile[tileno].packet[packno].end_ph_pos += addlen; - - /* end_pos: increment if marker is before the end of packet */ - /* disabled for the same reason as before */ - /*addlen = 0; - for (mm = 0; mm < jwmarker_num; mm++) - if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_pos) - addlen += jwmarker[mm].len + 2;*/ - info->tile[tileno].packet[packno].end_pos += addlen; - - } - } - - /* reorder the markers list */ - - return OPJ_TRUE; +opj_bool jpwl_update_info(opj_j2k_t *j2k, jpwl_marker_t *jwmarker, int jwmarker_num) +{ + + int mm; + unsigned long int addlen; + + opj_codestream_info_t *info = j2k->cstr_info; + int tileno, tpno, packno, numtiles = info->th * info->tw, numpacks = info->packno; + + if (!j2k || !jwmarker ) { + opj_event_msg(j2k->cinfo, EVT_ERROR, "J2K handle or JPWL markers list badly allocated\n"); + return OPJ_FALSE; + } + + /* main_head_end: how many markers are there before? */ + addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + if (jwmarker[mm].pos < (unsigned long int) info->main_head_end) + addlen += jwmarker[mm].len + 2; + info->main_head_end += addlen; + + /* codestream_size: always increment with all markers */ + addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + addlen += jwmarker[mm].len + 2; + info->codestream_size += addlen; + + /* navigate through all the tiles */ + for (tileno = 0; tileno < numtiles; tileno++) { + + /* start_pos: increment with markers before SOT */ + addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].start_pos) + addlen += jwmarker[mm].len + 2; + info->tile[tileno].start_pos += addlen; + + /* end_header: increment with markers before of it */ + addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_header) + addlen += jwmarker[mm].len + 2; + info->tile[tileno].end_header += addlen; + + /* end_pos: increment with markers before the end of this tile */ + /* code is disabled, since according to JPWL no markers can be beyond TPH */ + addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].end_pos) + addlen += jwmarker[mm].len + 2; + info->tile[tileno].end_pos += addlen; + + /* navigate through all the tile parts */ + for (tpno = 0; tpno < info->tile[tileno].num_tps; tpno++) { + + /* start_pos: increment with markers before SOT */ + addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_start_pos) + addlen += jwmarker[mm].len + 2; + info->tile[tileno].tp[tpno].tp_start_pos += addlen; + + /* end_header: increment with markers before of it */ + addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_end_header) + addlen += jwmarker[mm].len + 2; + info->tile[tileno].tp[tpno].tp_end_header += addlen; + + /* end_pos: increment with markers before the end of this tile part */ + addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].tp[tpno].tp_end_pos) + addlen += jwmarker[mm].len + 2; + info->tile[tileno].tp[tpno].tp_end_pos += addlen; + + } + + /* navigate through all the packets in this tile */ + for (packno = 0; packno < numpacks; packno++) { + + /* start_pos: increment with markers before the packet */ + /* disabled for the same reason as before */ + addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + if (jwmarker[mm].pos <= (unsigned long int) info->tile[tileno].packet[packno].start_pos) + addlen += jwmarker[mm].len + 2; + info->tile[tileno].packet[packno].start_pos += addlen; + + /* end_ph_pos: increment with markers before the packet */ + /* disabled for the same reason as before */ + /*addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_ph_pos) + addlen += jwmarker[mm].len + 2;*/ + info->tile[tileno].packet[packno].end_ph_pos += addlen; + + /* end_pos: increment if marker is before the end of packet */ + /* disabled for the same reason as before */ + /*addlen = 0; + for (mm = 0; mm < jwmarker_num; mm++) + if (jwmarker[mm].pos < (unsigned long int) info->tile[tileno].packet[packno].end_pos) + addlen += jwmarker[mm].len + 2;*/ + info->tile[tileno].packet[packno].end_pos += addlen; + + } + } + + /* reorder the markers list */ + + return OPJ_TRUE; } #endif /* USE_JPWL */ diff --git a/src/lib/openjpwl/rs.c b/src/lib/openjpwl/rs.c index a0bd7c71..abc834ea 100644 --- a/src/lib/openjpwl/rs.c +++ b/src/lib/openjpwl/rs.c @@ -1,38 +1,38 @@ - /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third - * party and contributor rights, including patent rights, and no such rights - * are granted under this license. - * - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Herve Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2005-2006, Dept. of Electronic and Information Engineering, Universita' degli Studi di Perugia, Italy - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ +/* +* The copyright in this software is being made available under the 2-clauses +* BSD License, included below. This software may be subject to other third +* party and contributor rights, including patent rights, and no such rights +* are granted under this license. +* +* Copyright (c) 2001-2003, David Janssens +* Copyright (c) 2002-2003, Yannick Verschueren +* Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe +* Copyright (c) 2005, Herve Drolon, FreeImage Team +* Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium +* Copyright (c) 2005-2006, Dept. of Electronic and Information Engineering, Universita' degli Studi di Perugia, Italy +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +*/ #ifdef USE_JPWL @@ -45,7 +45,7 @@ /** * Reed-Solomon coding and decoding * Phil Karn (karn@ka9q.ampr.org) September 1996 - * + * * This file is derived from the program "new_rs_erasures.c" by Robert * Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari Thirumoorthy * (harit@spectra.eng.hawaii.edu), Aug 1995 @@ -174,11 +174,11 @@ gf Gg[NN - 1]; static /*inline*/ gf modnn(int x) { - while (x >= NN) { - x -= NN; - x = (x >> MM) + (x & NN); - } - return x; + while (x >= NN) { + x -= NN; + x = (x >> MM) + (x & NN); + } + return x; } /*#define min(a,b) ((a) < (b) ? (a) : (b))*/ @@ -202,14 +202,14 @@ modnn(int x) void init_rs(int k) { - KK = k; - if (KK >= NN) { - printf("KK must be less than 2**MM - 1\n"); - exit(1); - } - - generate_gf(); - gen_poly(); + KK = k; + if (KK >= NN) { + printf("KK must be less than 2**MM - 1\n"); + exit(1); + } + + generate_gf(); + gen_poly(); } /* generate GF(2**m) from the irreducible polynomial p(X) in p[0]..p[m] @@ -232,7 +232,7 @@ void init_rs(int k) a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1) we consider the integer "i" whose binary representation with a(0) being LSB and a(m-1) MSB is (a(0),a(1),...,a(m-1)) and locate the entry - "index_of[i]". Now, @^index_of[i] is that element whose polynomial + "index_of[i]". Now, @^index_of[i] is that element whose polynomial representation is (a(0),a(1),a(2),...,a(m-1)). NOTE: The element alpha_to[2^m-1] = 0 always signifying that the @@ -240,40 +240,40 @@ void init_rs(int k) Similarly, the element index_of[0] = A0 always signifying that the power of alpha which has the polynomial representation (0,0,...,0) is "infinity". - + */ void generate_gf(void) { - register int i, mask; - - mask = 1; - Alpha_to[MM] = 0; - for (i = 0; i < MM; i++) { - Alpha_to[i] = mask; - Index_of[Alpha_to[i]] = i; - /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */ - if (Pp[i] != 0) - Alpha_to[MM] ^= mask; /* Bit-wise EXOR operation */ - mask <<= 1; /* single left-shift */ - } - Index_of[Alpha_to[MM]] = MM; - /* - * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by - * poly-repr of @^i shifted left one-bit and accounting for any @^MM - * term that may occur when poly-repr of @^i is shifted. - */ - mask >>= 1; - for (i = MM + 1; i < NN; i++) { - if (Alpha_to[i - 1] >= mask) - Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1); - else - Alpha_to[i] = Alpha_to[i - 1] << 1; - Index_of[Alpha_to[i]] = i; - } - Index_of[0] = A0; - Alpha_to[NN] = 0; + register int i, mask; + + mask = 1; + Alpha_to[MM] = 0; + for (i = 0; i < MM; i++) { + Alpha_to[i] = mask; + Index_of[Alpha_to[i]] = i; + /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */ + if (Pp[i] != 0) + Alpha_to[MM] ^= mask; /* Bit-wise EXOR operation */ + mask <<= 1; /* single left-shift */ + } + Index_of[Alpha_to[MM]] = MM; + /* + * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by + * poly-repr of @^i shifted left one-bit and accounting for any @^MM + * term that may occur when poly-repr of @^i is shifted. + */ + mask >>= 1; + for (i = MM + 1; i < NN; i++) { + if (Alpha_to[i - 1] >= mask) + Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1); + else + Alpha_to[i] = Alpha_to[i - 1] << 1; + Index_of[Alpha_to[i]] = i; + } + Index_of[0] = A0; + Alpha_to[NN] = 0; } @@ -293,27 +293,27 @@ generate_gf(void) void gen_poly(void) { - register int i, j; - - Gg[0] = Alpha_to[B0]; - Gg[1] = 1; /* g(x) = (X+@**B0) initially */ - for (i = 2; i <= NN - KK; i++) { - Gg[i] = 1; - /* - * Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by - * (@**(B0+i-1) + x) - */ - for (j = i - 1; j > 0; j--) - if (Gg[j] != 0) - Gg[j] = Gg[j - 1] ^ Alpha_to[modnn((Index_of[Gg[j]]) + B0 + i - 1)]; - else - Gg[j] = Gg[j - 1]; - /* Gg[0] can never be zero */ - Gg[0] = Alpha_to[modnn((Index_of[Gg[0]]) + B0 + i - 1)]; - } - /* convert Gg[] to index form for quicker encoding */ - for (i = 0; i <= NN - KK; i++) - Gg[i] = Index_of[Gg[i]]; + register int i, j; + + Gg[0] = Alpha_to[B0]; + Gg[1] = 1; /* g(x) = (X+@**B0) initially */ + for (i = 2; i <= NN - KK; i++) { + Gg[i] = 1; + /* + * Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by + * (@**(B0+i-1) + x) + */ + for (j = i - 1; j > 0; j--) + if (Gg[j] != 0) + Gg[j] = Gg[j - 1] ^ Alpha_to[modnn((Index_of[Gg[j]]) + B0 + i - 1)]; + else + Gg[j] = Gg[j - 1]; + /* Gg[0] can never be zero */ + Gg[0] = Alpha_to[modnn((Index_of[Gg[0]]) + B0 + i - 1)]; + } + /* convert Gg[] to index form for quicker encoding */ + for (i = 0; i <= NN - KK; i++) + Gg[i] = Index_of[Gg[i]]; } @@ -328,31 +328,32 @@ gen_poly(void) int encode_rs(dtype *data, dtype *bb) { - register int i, j; - gf feedback; + register int i, j; + gf feedback; - CLEAR(bb,NN-KK); - for (i = KK - 1; i >= 0; i--) { + CLEAR(bb,NN-KK); + for (i = KK - 1; i >= 0; i--) { #if (MM != 8) - if(data[i] > NN) - return -1; /* Illegal symbol */ + if(data[i] > NN) + return -1; /* Illegal symbol */ #endif - feedback = Index_of[data[i] ^ bb[NN - KK - 1]]; - if (feedback != A0) { /* feedback term is non-zero */ - for (j = NN - KK - 1; j > 0; j--) - if (Gg[j] != A0) - bb[j] = bb[j - 1] ^ Alpha_to[modnn(Gg[j] + feedback)]; - else - bb[j] = bb[j - 1]; - bb[0] = Alpha_to[modnn(Gg[0] + feedback)]; - } else { /* feedback term is zero. encoder becomes a - * single-byte shifter */ - for (j = NN - KK - 1; j > 0; j--) - bb[j] = bb[j - 1]; - bb[0] = 0; - } - } - return 0; + feedback = Index_of[data[i] ^ bb[NN - KK - 1]]; + if (feedback != A0) { /* feedback term is non-zero */ + for (j = NN - KK - 1; j > 0; j--) + if (Gg[j] != A0) + bb[j] = bb[j - 1] ^ Alpha_to[modnn(Gg[j] + feedback)]; + else + bb[j] = bb[j - 1]; + bb[0] = Alpha_to[modnn(Gg[0] + feedback)]; + } else { + /* feedback term is zero. encoder becomes a + * single-byte shifter */ + for (j = NN - KK - 1; j > 0; j--) + bb[j] = bb[j - 1]; + bb[0] = 0; + } + } + return 0; } /* @@ -361,7 +362,7 @@ encode_rs(dtype *data, dtype *bb) * * Return number of symbols corrected, or -1 if codeword is illegal * or uncorrectable. - * + * * First "no_eras" erasures are declared by the calling program. Then, the * maximum # of errors correctable is t_after_eras = floor((NN-KK-no_eras)/2). * If the number of channel errors is not greater than "t_after_eras" the @@ -371,231 +372,231 @@ encode_rs(dtype *data, dtype *bb) int eras_dec_rs(dtype *data, int *eras_pos, int no_eras) { - int deg_lambda, el, deg_omega; - int i, j, r; - gf u,q,tmp,num1,num2,den,discr_r; - gf recd[NN]; - /* Err+Eras Locator poly and syndrome poly */ - /*gf lambda[NN-KK + 1], s[NN-KK + 1]; - gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1]; - gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];*/ - gf lambda[NN + 1], s[NN + 1]; - gf b[NN + 1], t[NN + 1], omega[NN + 1]; - gf root[NN], reg[NN + 1], loc[NN]; - int syn_error, count; - - /* data[] is in polynomial form, copy and convert to index form */ - for (i = NN-1; i >= 0; i--){ + int deg_lambda, el, deg_omega; + int i, j, r; + gf u,q,tmp,num1,num2,den,discr_r; + gf recd[NN]; + /* Err+Eras Locator poly and syndrome poly */ + /*gf lambda[NN-KK + 1], s[NN-KK + 1]; + gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1]; + gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];*/ + gf lambda[NN + 1], s[NN + 1]; + gf b[NN + 1], t[NN + 1], omega[NN + 1]; + gf root[NN], reg[NN + 1], loc[NN]; + int syn_error, count; + + /* data[] is in polynomial form, copy and convert to index form */ + for (i = NN-1; i >= 0; i--) { #if (MM != 8) - if(data[i] > NN) - return -1; /* Illegal symbol */ + if(data[i] > NN) + return -1; /* Illegal symbol */ #endif - recd[i] = Index_of[data[i]]; - } - /* first form the syndromes; i.e., evaluate recd(x) at roots of g(x) - * namely @**(B0+i), i = 0, ... ,(NN-KK-1) - */ - syn_error = 0; - for (i = 1; i <= NN-KK; i++) { - tmp = 0; - for (j = 0; j < NN; j++) - if (recd[j] != A0) /* recd[j] in index form */ - tmp ^= Alpha_to[modnn(recd[j] + (B0+i-1)*j)]; - syn_error |= tmp; /* set flag if non-zero syndrome => + recd[i] = Index_of[data[i]]; + } + /* first form the syndromes; i.e., evaluate recd(x) at roots of g(x) + * namely @**(B0+i), i = 0, ... ,(NN-KK-1) + */ + syn_error = 0; + for (i = 1; i <= NN-KK; i++) { + tmp = 0; + for (j = 0; j < NN; j++) + if (recd[j] != A0) /* recd[j] in index form */ + tmp ^= Alpha_to[modnn(recd[j] + (B0+i-1)*j)]; + syn_error |= tmp; /* set flag if non-zero syndrome => * error */ - /* store syndrome in index form */ - s[i] = Index_of[tmp]; - } - if (!syn_error) { - /* - * if syndrome is zero, data[] is a codeword and there are no - * errors to correct. So return data[] unmodified - */ - return 0; - } - CLEAR(&lambda[1],NN-KK); - lambda[0] = 1; - if (no_eras > 0) { - /* Init lambda to be the erasure locator polynomial */ - lambda[1] = Alpha_to[eras_pos[0]]; - for (i = 1; i < no_eras; i++) { - u = eras_pos[i]; - for (j = i+1; j > 0; j--) { - tmp = Index_of[lambda[j - 1]]; - if(tmp != A0) - lambda[j] ^= Alpha_to[modnn(u + tmp)]; - } - } + /* store syndrome in index form */ + s[i] = Index_of[tmp]; + } + if (!syn_error) { + /* + * if syndrome is zero, data[] is a codeword and there are no + * errors to correct. So return data[] unmodified + */ + return 0; + } + CLEAR(&lambda[1],NN-KK); + lambda[0] = 1; + if (no_eras > 0) { + /* Init lambda to be the erasure locator polynomial */ + lambda[1] = Alpha_to[eras_pos[0]]; + for (i = 1; i < no_eras; i++) { + u = eras_pos[i]; + for (j = i+1; j > 0; j--) { + tmp = Index_of[lambda[j - 1]]; + if(tmp != A0) + lambda[j] ^= Alpha_to[modnn(u + tmp)]; + } + } #ifdef ERASURE_DEBUG - /* find roots of the erasure location polynomial */ - for(i=1;i<=no_eras;i++) - reg[i] = Index_of[lambda[i]]; - count = 0; - for (i = 1; i <= NN; i++) { - q = 1; - for (j = 1; j <= no_eras; j++) - if (reg[j] != A0) { - reg[j] = modnn(reg[j] + j); - q ^= Alpha_to[reg[j]]; - } - if (!q) { - /* store root and error location - * number indices - */ - root[count] = i; - loc[count] = NN - i; - count++; - } - } - if (count != no_eras) { - printf("\n lambda(x) is WRONG\n"); - return -1; - } + /* find roots of the erasure location polynomial */ + for(i=1; i<=no_eras; i++) + reg[i] = Index_of[lambda[i]]; + count = 0; + for (i = 1; i <= NN; i++) { + q = 1; + for (j = 1; j <= no_eras; j++) + if (reg[j] != A0) { + reg[j] = modnn(reg[j] + j); + q ^= Alpha_to[reg[j]]; + } + if (!q) { + /* store root and error location + * number indices + */ + root[count] = i; + loc[count] = NN - i; + count++; + } + } + if (count != no_eras) { + printf("\n lambda(x) is WRONG\n"); + return -1; + } #ifndef NO_PRINT - printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n"); - for (i = 0; i < count; i++) - printf("%d ", loc[i]); - printf("\n"); + printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n"); + for (i = 0; i < count; i++) + printf("%d ", loc[i]); + printf("\n"); #endif #endif - } - for(i=0;i<NN-KK+1;i++) - b[i] = Index_of[lambda[i]]; - - /* - * Begin Berlekamp-Massey algorithm to determine error+erasure - * locator polynomial - */ - r = no_eras; - el = no_eras; - while (++r <= NN-KK) { /* r is the step number */ - /* Compute discrepancy at the r-th step in poly-form */ - discr_r = 0; - for (i = 0; i < r; i++){ - if ((lambda[i] != 0) && (s[r - i] != A0)) { - discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])]; - } - } - discr_r = Index_of[discr_r]; /* Index form */ - if (discr_r == A0) { - /* 2 lines below: B(x) <-- x*B(x) */ - COPYDOWN(&b[1],b,NN-KK); - b[0] = A0; - } else { - /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */ - t[0] = lambda[0]; - for (i = 0 ; i < NN-KK; i++) { - if(b[i] != A0) - t[i+1] = lambda[i+1] ^ Alpha_to[modnn(discr_r + b[i])]; - else - t[i+1] = lambda[i+1]; - } - if (2 * el <= r + no_eras - 1) { - el = r + no_eras - el; - /* - * 2 lines below: B(x) <-- inv(discr_r) * - * lambda(x) - */ - for (i = 0; i <= NN-KK; i++) - b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN); - } else { - /* 2 lines below: B(x) <-- x*B(x) */ - COPYDOWN(&b[1],b,NN-KK); - b[0] = A0; - } - COPY(lambda,t,NN-KK+1); - } - } - - /* Convert lambda to index form and compute deg(lambda(x)) */ - deg_lambda = 0; - for(i=0;i<NN-KK+1;i++){ - lambda[i] = Index_of[lambda[i]]; - if(lambda[i] != A0) - deg_lambda = i; - } - /* - * Find roots of the error+erasure locator polynomial. By Chien - * Search - */ - COPY(®[1],&lambda[1],NN-KK); - count = 0; /* Number of roots of lambda(x) */ - for (i = 1; i <= NN; i++) { - q = 1; - for (j = deg_lambda; j > 0; j--) - if (reg[j] != A0) { - reg[j] = modnn(reg[j] + j); - q ^= Alpha_to[reg[j]]; - } - if (!q) { - /* store root (index-form) and error location number */ - root[count] = i; - loc[count] = NN - i; - count++; - } - } + } + for(i=0; i<NN-KK+1; i++) + b[i] = Index_of[lambda[i]]; + + /* + * Begin Berlekamp-Massey algorithm to determine error+erasure + * locator polynomial + */ + r = no_eras; + el = no_eras; + while (++r <= NN-KK) { /* r is the step number */ + /* Compute discrepancy at the r-th step in poly-form */ + discr_r = 0; + for (i = 0; i < r; i++) { + if ((lambda[i] != 0) && (s[r - i] != A0)) { + discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])]; + } + } + discr_r = Index_of[discr_r]; /* Index form */ + if (discr_r == A0) { + /* 2 lines below: B(x) <-- x*B(x) */ + COPYDOWN(&b[1],b,NN-KK); + b[0] = A0; + } else { + /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */ + t[0] = lambda[0]; + for (i = 0 ; i < NN-KK; i++) { + if(b[i] != A0) + t[i+1] = lambda[i+1] ^ Alpha_to[modnn(discr_r + b[i])]; + else + t[i+1] = lambda[i+1]; + } + if (2 * el <= r + no_eras - 1) { + el = r + no_eras - el; + /* + * 2 lines below: B(x) <-- inv(discr_r) * + * lambda(x) + */ + for (i = 0; i <= NN-KK; i++) + b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN); + } else { + /* 2 lines below: B(x) <-- x*B(x) */ + COPYDOWN(&b[1],b,NN-KK); + b[0] = A0; + } + COPY(lambda,t,NN-KK+1); + } + } + + /* Convert lambda to index form and compute deg(lambda(x)) */ + deg_lambda = 0; + for(i=0; i<NN-KK+1; i++) { + lambda[i] = Index_of[lambda[i]]; + if(lambda[i] != A0) + deg_lambda = i; + } + /* + * Find roots of the error+erasure locator polynomial. By Chien + * Search + */ + COPY(®[1],&lambda[1],NN-KK); + count = 0; /* Number of roots of lambda(x) */ + for (i = 1; i <= NN; i++) { + q = 1; + for (j = deg_lambda; j > 0; j--) + if (reg[j] != A0) { + reg[j] = modnn(reg[j] + j); + q ^= Alpha_to[reg[j]]; + } + if (!q) { + /* store root (index-form) and error location number */ + root[count] = i; + loc[count] = NN - i; + count++; + } + } #ifdef DEBUG - printf("\n Final error positions:\t"); - for (i = 0; i < count; i++) - printf("%d ", loc[i]); - printf("\n"); + printf("\n Final error positions:\t"); + for (i = 0; i < count; i++) + printf("%d ", loc[i]); + printf("\n"); #endif - if (deg_lambda != count) { - /* - * deg(lambda) unequal to number of roots => uncorrectable - * error detected - */ - return -1; - } - /* - * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo - * x**(NN-KK)). in index form. Also find deg(omega). - */ - deg_omega = 0; - for (i = 0; i < NN-KK;i++){ - tmp = 0; - j = (deg_lambda < i) ? deg_lambda : i; - for(;j >= 0; j--){ - if ((s[i + 1 - j] != A0) && (lambda[j] != A0)) - tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])]; - } - if(tmp != 0) - deg_omega = i; - omega[i] = Index_of[tmp]; - } - omega[NN-KK] = A0; - - /* - * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 = - * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form - */ - for (j = count-1; j >=0; j--) { - num1 = 0; - for (i = deg_omega; i >= 0; i--) { - if (omega[i] != A0) - num1 ^= Alpha_to[modnn(omega[i] + i * root[j])]; - } - num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)]; - den = 0; - - /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */ - for (i = min(deg_lambda,NN-KK-1) & ~1; i >= 0; i -=2) { - if(lambda[i+1] != A0) - den ^= Alpha_to[modnn(lambda[i+1] + i * root[j])]; - } - if (den == 0) { + if (deg_lambda != count) { + /* + * deg(lambda) unequal to number of roots => uncorrectable + * error detected + */ + return -1; + } + /* + * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo + * x**(NN-KK)). in index form. Also find deg(omega). + */ + deg_omega = 0; + for (i = 0; i < NN-KK; i++) { + tmp = 0; + j = (deg_lambda < i) ? deg_lambda : i; + for(; j >= 0; j--) { + if ((s[i + 1 - j] != A0) && (lambda[j] != A0)) + tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])]; + } + if(tmp != 0) + deg_omega = i; + omega[i] = Index_of[tmp]; + } + omega[NN-KK] = A0; + + /* + * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 = + * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form + */ + for (j = count-1; j >=0; j--) { + num1 = 0; + for (i = deg_omega; i >= 0; i--) { + if (omega[i] != A0) + num1 ^= Alpha_to[modnn(omega[i] + i * root[j])]; + } + num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)]; + den = 0; + + /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */ + for (i = min(deg_lambda,NN-KK-1) & ~1; i >= 0; i -=2) { + if(lambda[i+1] != A0) + den ^= Alpha_to[modnn(lambda[i+1] + i * root[j])]; + } + if (den == 0) { #ifdef DEBUG - printf("\n ERROR: denominator = 0\n"); + printf("\n ERROR: denominator = 0\n"); #endif - return -1; - } - /* Apply error to data */ - if (num1 != 0) { - data[loc[j]] ^= Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN - Index_of[den])]; - } - } - return count; + return -1; + } + /* Apply error to data */ + if (num1 != 0) { + data[loc[j]] ^= Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN - Index_of[den])]; + } + } + return count; } diff --git a/src/lib/openjpwl/rs.h b/src/lib/openjpwl/rs.h index 57e5c22d..d2a7c16f 100644 --- a/src/lib/openjpwl/rs.h +++ b/src/lib/openjpwl/rs.h @@ -1,6 +1,6 @@ /* - * The copyright in this software is being made available under the 2-clauses - * BSD License, included below. This software may be subject to other third + * The copyright in this software is being made available under the 2-clauses + * BSD License, included below. This software may be subject to other third * party and contributor rights, including patent rights, and no such rights * are granted under this license. * |
