Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 1 | /* |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 2 | * The copyright in this software is being made available under the 2-clauses |
| 3 | * BSD License, included below. This software may be subject to other third |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 4 | * party and contributor rights, including patent rights, and no such rights |
| 5 | * are granted under this license. |
| 6 | * |
| 7 | * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium |
| 8 | * Copyright (c) 2002-2014, Professor Benoit Macq |
| 9 | * Copyright (c) 2001-2003, David Janssens |
| 10 | * Copyright (c) 2002-2003, Yannick Verschueren |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 11 | * Copyright (c) 2003-2007, Francois-Olivier Devaux |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 12 | * Copyright (c) 2003-2014, Antonin Descampe |
| 13 | * Copyright (c) 2005, Herve Drolon, FreeImage Team |
| 14 | * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr> |
| 15 | * All rights reserved. |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * 1. Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer in the |
| 24 | * documentation and/or other materials provided with the distribution. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' |
| 27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 30 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 32 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 35 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 | * POSSIBILITY OF SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | #include "opj_includes.h" |
| 40 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 41 | #include <assert.h> |
| 42 | |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 43 | /** @defgroup MQC MQC - Implementation of an MQ-Coder */ |
| 44 | /*@{*/ |
| 45 | |
| 46 | /** @name Local static functions */ |
| 47 | /*@{*/ |
| 48 | |
| 49 | /** |
| 50 | Output a byte, doing bit-stuffing if necessary. |
| 51 | After a 0xff byte, the next byte must be smaller than 0x90. |
| 52 | @param mqc MQC handle |
| 53 | */ |
| 54 | static void opj_mqc_byteout(opj_mqc_t *mqc); |
| 55 | /** |
| 56 | Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000 |
| 57 | @param mqc MQC handle |
| 58 | */ |
| 59 | static void opj_mqc_renorme(opj_mqc_t *mqc); |
| 60 | /** |
| 61 | Encode the most probable symbol |
| 62 | @param mqc MQC handle |
| 63 | */ |
| 64 | static void opj_mqc_codemps(opj_mqc_t *mqc); |
| 65 | /** |
| 66 | Encode the most least symbol |
| 67 | @param mqc MQC handle |
| 68 | */ |
| 69 | static void opj_mqc_codelps(opj_mqc_t *mqc); |
| 70 | /** |
| 71 | Fill mqc->c with 1's for flushing |
| 72 | @param mqc MQC handle |
| 73 | */ |
| 74 | static void opj_mqc_setbits(opj_mqc_t *mqc); |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 75 | /*@}*/ |
| 76 | |
| 77 | /*@}*/ |
| 78 | |
| 79 | /* <summary> */ |
| 80 | /* This array defines all the possible states for a context. */ |
| 81 | /* </summary> */ |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 82 | static const opj_mqc_state_t mqc_states[47 * 2] = { |
| 83 | {0x5601, 0, &mqc_states[2], &mqc_states[3]}, |
| 84 | {0x5601, 1, &mqc_states[3], &mqc_states[2]}, |
| 85 | {0x3401, 0, &mqc_states[4], &mqc_states[12]}, |
| 86 | {0x3401, 1, &mqc_states[5], &mqc_states[13]}, |
| 87 | {0x1801, 0, &mqc_states[6], &mqc_states[18]}, |
| 88 | {0x1801, 1, &mqc_states[7], &mqc_states[19]}, |
| 89 | {0x0ac1, 0, &mqc_states[8], &mqc_states[24]}, |
| 90 | {0x0ac1, 1, &mqc_states[9], &mqc_states[25]}, |
| 91 | {0x0521, 0, &mqc_states[10], &mqc_states[58]}, |
| 92 | {0x0521, 1, &mqc_states[11], &mqc_states[59]}, |
| 93 | {0x0221, 0, &mqc_states[76], &mqc_states[66]}, |
| 94 | {0x0221, 1, &mqc_states[77], &mqc_states[67]}, |
| 95 | {0x5601, 0, &mqc_states[14], &mqc_states[13]}, |
| 96 | {0x5601, 1, &mqc_states[15], &mqc_states[12]}, |
| 97 | {0x5401, 0, &mqc_states[16], &mqc_states[28]}, |
| 98 | {0x5401, 1, &mqc_states[17], &mqc_states[29]}, |
| 99 | {0x4801, 0, &mqc_states[18], &mqc_states[28]}, |
| 100 | {0x4801, 1, &mqc_states[19], &mqc_states[29]}, |
| 101 | {0x3801, 0, &mqc_states[20], &mqc_states[28]}, |
| 102 | {0x3801, 1, &mqc_states[21], &mqc_states[29]}, |
| 103 | {0x3001, 0, &mqc_states[22], &mqc_states[34]}, |
| 104 | {0x3001, 1, &mqc_states[23], &mqc_states[35]}, |
| 105 | {0x2401, 0, &mqc_states[24], &mqc_states[36]}, |
| 106 | {0x2401, 1, &mqc_states[25], &mqc_states[37]}, |
| 107 | {0x1c01, 0, &mqc_states[26], &mqc_states[40]}, |
| 108 | {0x1c01, 1, &mqc_states[27], &mqc_states[41]}, |
| 109 | {0x1601, 0, &mqc_states[58], &mqc_states[42]}, |
| 110 | {0x1601, 1, &mqc_states[59], &mqc_states[43]}, |
| 111 | {0x5601, 0, &mqc_states[30], &mqc_states[29]}, |
| 112 | {0x5601, 1, &mqc_states[31], &mqc_states[28]}, |
| 113 | {0x5401, 0, &mqc_states[32], &mqc_states[28]}, |
| 114 | {0x5401, 1, &mqc_states[33], &mqc_states[29]}, |
| 115 | {0x5101, 0, &mqc_states[34], &mqc_states[30]}, |
| 116 | {0x5101, 1, &mqc_states[35], &mqc_states[31]}, |
| 117 | {0x4801, 0, &mqc_states[36], &mqc_states[32]}, |
| 118 | {0x4801, 1, &mqc_states[37], &mqc_states[33]}, |
| 119 | {0x3801, 0, &mqc_states[38], &mqc_states[34]}, |
| 120 | {0x3801, 1, &mqc_states[39], &mqc_states[35]}, |
| 121 | {0x3401, 0, &mqc_states[40], &mqc_states[36]}, |
| 122 | {0x3401, 1, &mqc_states[41], &mqc_states[37]}, |
| 123 | {0x3001, 0, &mqc_states[42], &mqc_states[38]}, |
| 124 | {0x3001, 1, &mqc_states[43], &mqc_states[39]}, |
| 125 | {0x2801, 0, &mqc_states[44], &mqc_states[38]}, |
| 126 | {0x2801, 1, &mqc_states[45], &mqc_states[39]}, |
| 127 | {0x2401, 0, &mqc_states[46], &mqc_states[40]}, |
| 128 | {0x2401, 1, &mqc_states[47], &mqc_states[41]}, |
| 129 | {0x2201, 0, &mqc_states[48], &mqc_states[42]}, |
| 130 | {0x2201, 1, &mqc_states[49], &mqc_states[43]}, |
| 131 | {0x1c01, 0, &mqc_states[50], &mqc_states[44]}, |
| 132 | {0x1c01, 1, &mqc_states[51], &mqc_states[45]}, |
| 133 | {0x1801, 0, &mqc_states[52], &mqc_states[46]}, |
| 134 | {0x1801, 1, &mqc_states[53], &mqc_states[47]}, |
| 135 | {0x1601, 0, &mqc_states[54], &mqc_states[48]}, |
| 136 | {0x1601, 1, &mqc_states[55], &mqc_states[49]}, |
| 137 | {0x1401, 0, &mqc_states[56], &mqc_states[50]}, |
| 138 | {0x1401, 1, &mqc_states[57], &mqc_states[51]}, |
| 139 | {0x1201, 0, &mqc_states[58], &mqc_states[52]}, |
| 140 | {0x1201, 1, &mqc_states[59], &mqc_states[53]}, |
| 141 | {0x1101, 0, &mqc_states[60], &mqc_states[54]}, |
| 142 | {0x1101, 1, &mqc_states[61], &mqc_states[55]}, |
| 143 | {0x0ac1, 0, &mqc_states[62], &mqc_states[56]}, |
| 144 | {0x0ac1, 1, &mqc_states[63], &mqc_states[57]}, |
| 145 | {0x09c1, 0, &mqc_states[64], &mqc_states[58]}, |
| 146 | {0x09c1, 1, &mqc_states[65], &mqc_states[59]}, |
| 147 | {0x08a1, 0, &mqc_states[66], &mqc_states[60]}, |
| 148 | {0x08a1, 1, &mqc_states[67], &mqc_states[61]}, |
| 149 | {0x0521, 0, &mqc_states[68], &mqc_states[62]}, |
| 150 | {0x0521, 1, &mqc_states[69], &mqc_states[63]}, |
| 151 | {0x0441, 0, &mqc_states[70], &mqc_states[64]}, |
| 152 | {0x0441, 1, &mqc_states[71], &mqc_states[65]}, |
| 153 | {0x02a1, 0, &mqc_states[72], &mqc_states[66]}, |
| 154 | {0x02a1, 1, &mqc_states[73], &mqc_states[67]}, |
| 155 | {0x0221, 0, &mqc_states[74], &mqc_states[68]}, |
| 156 | {0x0221, 1, &mqc_states[75], &mqc_states[69]}, |
| 157 | {0x0141, 0, &mqc_states[76], &mqc_states[70]}, |
| 158 | {0x0141, 1, &mqc_states[77], &mqc_states[71]}, |
| 159 | {0x0111, 0, &mqc_states[78], &mqc_states[72]}, |
| 160 | {0x0111, 1, &mqc_states[79], &mqc_states[73]}, |
| 161 | {0x0085, 0, &mqc_states[80], &mqc_states[74]}, |
| 162 | {0x0085, 1, &mqc_states[81], &mqc_states[75]}, |
| 163 | {0x0049, 0, &mqc_states[82], &mqc_states[76]}, |
| 164 | {0x0049, 1, &mqc_states[83], &mqc_states[77]}, |
| 165 | {0x0025, 0, &mqc_states[84], &mqc_states[78]}, |
| 166 | {0x0025, 1, &mqc_states[85], &mqc_states[79]}, |
| 167 | {0x0015, 0, &mqc_states[86], &mqc_states[80]}, |
| 168 | {0x0015, 1, &mqc_states[87], &mqc_states[81]}, |
| 169 | {0x0009, 0, &mqc_states[88], &mqc_states[82]}, |
| 170 | {0x0009, 1, &mqc_states[89], &mqc_states[83]}, |
| 171 | {0x0005, 0, &mqc_states[90], &mqc_states[84]}, |
| 172 | {0x0005, 1, &mqc_states[91], &mqc_states[85]}, |
| 173 | {0x0001, 0, &mqc_states[90], &mqc_states[86]}, |
| 174 | {0x0001, 1, &mqc_states[91], &mqc_states[87]}, |
| 175 | {0x5601, 0, &mqc_states[92], &mqc_states[92]}, |
| 176 | {0x5601, 1, &mqc_states[93], &mqc_states[93]}, |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 179 | /* |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 180 | ========================================================== |
| 181 | local functions |
| 182 | ========================================================== |
| 183 | */ |
| 184 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 185 | static void opj_mqc_byteout(opj_mqc_t *mqc) |
| 186 | { |
| 187 | /* bp is initialized to start - 1 in opj_mqc_init_enc() */ |
| 188 | /* but this is safe, see opj_tcd_code_block_enc_allocate_data() */ |
| 189 | assert(mqc->bp >= mqc->start - 1); |
| 190 | if (*mqc->bp == 0xff) { |
| 191 | mqc->bp++; |
| 192 | *mqc->bp = (OPJ_BYTE)(mqc->c >> 20); |
| 193 | mqc->c &= 0xfffff; |
| 194 | mqc->ct = 7; |
| 195 | } else { |
| 196 | if ((mqc->c & 0x8000000) == 0) { |
| 197 | mqc->bp++; |
| 198 | *mqc->bp = (OPJ_BYTE)(mqc->c >> 19); |
| 199 | mqc->c &= 0x7ffff; |
| 200 | mqc->ct = 8; |
| 201 | } else { |
| 202 | (*mqc->bp)++; |
| 203 | if (*mqc->bp == 0xff) { |
| 204 | mqc->c &= 0x7ffffff; |
| 205 | mqc->bp++; |
| 206 | *mqc->bp = (OPJ_BYTE)(mqc->c >> 20); |
| 207 | mqc->c &= 0xfffff; |
| 208 | mqc->ct = 7; |
| 209 | } else { |
| 210 | mqc->bp++; |
| 211 | *mqc->bp = (OPJ_BYTE)(mqc->c >> 19); |
| 212 | mqc->c &= 0x7ffff; |
| 213 | mqc->ct = 8; |
| 214 | } |
| 215 | } |
| 216 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 219 | static void opj_mqc_renorme(opj_mqc_t *mqc) |
| 220 | { |
| 221 | do { |
| 222 | mqc->a <<= 1; |
| 223 | mqc->c <<= 1; |
| 224 | mqc->ct--; |
| 225 | if (mqc->ct == 0) { |
| 226 | opj_mqc_byteout(mqc); |
| 227 | } |
| 228 | } while ((mqc->a & 0x8000) == 0); |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 231 | static void opj_mqc_codemps(opj_mqc_t *mqc) |
| 232 | { |
| 233 | mqc->a -= (*mqc->curctx)->qeval; |
| 234 | if ((mqc->a & 0x8000) == 0) { |
| 235 | if (mqc->a < (*mqc->curctx)->qeval) { |
| 236 | mqc->a = (*mqc->curctx)->qeval; |
| 237 | } else { |
| 238 | mqc->c += (*mqc->curctx)->qeval; |
| 239 | } |
| 240 | *mqc->curctx = (*mqc->curctx)->nmps; |
| 241 | opj_mqc_renorme(mqc); |
| 242 | } else { |
| 243 | mqc->c += (*mqc->curctx)->qeval; |
| 244 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 247 | static void opj_mqc_codelps(opj_mqc_t *mqc) |
| 248 | { |
| 249 | mqc->a -= (*mqc->curctx)->qeval; |
| 250 | if (mqc->a < (*mqc->curctx)->qeval) { |
| 251 | mqc->c += (*mqc->curctx)->qeval; |
| 252 | } else { |
| 253 | mqc->a = (*mqc->curctx)->qeval; |
| 254 | } |
| 255 | *mqc->curctx = (*mqc->curctx)->nlps; |
| 256 | opj_mqc_renorme(mqc); |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 259 | static void opj_mqc_setbits(opj_mqc_t *mqc) |
| 260 | { |
| 261 | OPJ_UINT32 tempc = mqc->c + mqc->a; |
| 262 | mqc->c |= 0xffff; |
| 263 | if (mqc->c >= tempc) { |
| 264 | mqc->c -= 0x8000; |
| 265 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 268 | /* |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 269 | ========================================================== |
| 270 | MQ-Coder interface |
| 271 | ========================================================== |
| 272 | */ |
| 273 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 274 | OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc) |
| 275 | { |
| 276 | const ptrdiff_t diff = mqc->bp - mqc->start; |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 277 | #if 0 |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 278 | assert(diff <= 0xffffffff && diff >= 0); /* UINT32_MAX */ |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 279 | #endif |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 280 | return (OPJ_UINT32)diff; |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 283 | void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp) |
| 284 | { |
| 285 | /* To avoid the curctx pointer to be dangling, but not strictly */ |
| 286 | /* required as the current context is always set before encoding */ |
| 287 | opj_mqc_setcurctx(mqc, 0); |
| 288 | |
| 289 | /* As specified in Figure C.10 - Initialization of the encoder */ |
| 290 | /* (C.2.8 Initialization of the encoder (INITENC)) */ |
| 291 | mqc->a = 0x8000; |
| 292 | mqc->c = 0; |
| 293 | /* Yes, we point before the start of the buffer, but this is safe */ |
| 294 | /* given opj_tcd_code_block_enc_allocate_data() */ |
| 295 | mqc->bp = bp - 1; |
| 296 | mqc->ct = 12; |
| 297 | /* At this point we should test *(mqc->bp) against 0xFF, but this is not */ |
| 298 | /* necessary, as this is only used at the beginning of the code block */ |
| 299 | /* and our initial fake byte is set at 0 */ |
| 300 | assert(*(mqc->bp) != 0xff); |
| 301 | |
| 302 | mqc->start = bp; |
| 303 | mqc->end_of_byte_stream_counter = 0; |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 306 | void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d) |
| 307 | { |
| 308 | if ((*mqc->curctx)->mps == d) { |
| 309 | opj_mqc_codemps(mqc); |
| 310 | } else { |
| 311 | opj_mqc_codelps(mqc); |
| 312 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 315 | void opj_mqc_flush(opj_mqc_t *mqc) |
| 316 | { |
| 317 | /* C.2.9 Termination of coding (FLUSH) */ |
| 318 | /* Figure C.11 – FLUSH procedure */ |
| 319 | opj_mqc_setbits(mqc); |
| 320 | mqc->c <<= mqc->ct; |
| 321 | opj_mqc_byteout(mqc); |
| 322 | mqc->c <<= mqc->ct; |
| 323 | opj_mqc_byteout(mqc); |
| 324 | |
| 325 | /* It is forbidden that a coding pass ends with 0xff */ |
| 326 | if (*mqc->bp != 0xff) { |
| 327 | /* Advance pointer so that opj_mqc_numbytes() returns a valid value */ |
| 328 | mqc->bp++; |
| 329 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 332 | #define BYPASS_CT_INIT 0xDEADBEEF |
| 333 | |
| 334 | void opj_mqc_bypass_init_enc(opj_mqc_t *mqc) |
| 335 | { |
| 336 | /* This function is normally called after at least one opj_mqc_flush() */ |
| 337 | /* which will have advance mqc->bp by at least 2 bytes beyond its */ |
| 338 | /* initial position */ |
| 339 | assert(mqc->bp >= mqc->start); |
| 340 | mqc->c = 0; |
| 341 | /* in theory we should initialize to 8, but use this special value */ |
| 342 | /* as a hint that opj_mqc_bypass_enc() has never been called, so */ |
| 343 | /* as to avoid the 0xff 0x7f elimination trick in opj_mqc_bypass_flush_enc() */ |
| 344 | /* to trigger when we don't have output any bit during this bypass sequence */ |
| 345 | /* Any value > 8 will do */ |
| 346 | mqc->ct = BYPASS_CT_INIT; |
| 347 | /* Given that we are called after opj_mqc_flush(), the previous byte */ |
| 348 | /* cannot be 0xff. */ |
| 349 | assert(mqc->bp[-1] != 0xff); |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 352 | void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d) |
| 353 | { |
| 354 | if (mqc->ct == BYPASS_CT_INIT) { |
| 355 | mqc->ct = 8; |
| 356 | } |
| 357 | mqc->ct--; |
| 358 | mqc->c = mqc->c + (d << mqc->ct); |
| 359 | if (mqc->ct == 0) { |
| 360 | *mqc->bp = (OPJ_BYTE)mqc->c; |
| 361 | mqc->ct = 8; |
| 362 | /* If the previous byte was 0xff, make sure that the next msb is 0 */ |
| 363 | if (*mqc->bp == 0xff) { |
| 364 | mqc->ct = 7; |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 365 | } |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 366 | mqc->bp++; |
| 367 | mqc->c = 0; |
| 368 | } |
| 369 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 370 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 371 | OPJ_UINT32 opj_mqc_bypass_get_extra_bytes(opj_mqc_t *mqc, OPJ_BOOL erterm) |
| 372 | { |
| 373 | return (mqc->ct < 7 || |
| 374 | (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) ? 1 : 0; |
| 375 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 376 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 377 | void opj_mqc_bypass_flush_enc(opj_mqc_t *mqc, OPJ_BOOL erterm) |
| 378 | { |
| 379 | /* Is there any bit remaining to be flushed ? */ |
| 380 | /* If the last output byte is 0xff, we can discard it, unless */ |
| 381 | /* erterm is required (I'm not completely sure why in erterm */ |
| 382 | /* we must output 0xff 0x2a if the last byte was 0xff instead of */ |
| 383 | /* discarding it, but Kakadu requires it when decoding */ |
| 384 | /* in -fussy mode) */ |
| 385 | if (mqc->ct < 7 || (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) { |
| 386 | OPJ_BYTE bit_value = 0; |
| 387 | /* If so, fill the remaining lsbs with an alternating sequence of */ |
| 388 | /* 0,1,... */ |
| 389 | /* Note: it seems the standard only requires that for a ERTERM flush */ |
| 390 | /* and doesn't specify what to do for a regular BYPASS flush */ |
| 391 | while (mqc->ct > 0) { |
| 392 | mqc->ct--; |
| 393 | mqc->c += (OPJ_UINT32)(bit_value << mqc->ct); |
| 394 | bit_value = (OPJ_BYTE)(1U - bit_value); |
| 395 | } |
| 396 | *mqc->bp = (OPJ_BYTE)mqc->c; |
| 397 | /* Advance pointer so that opj_mqc_numbytes() returns a valid value */ |
| 398 | mqc->bp++; |
| 399 | } else if (mqc->ct == 7 && mqc->bp[-1] == 0xff) { |
| 400 | /* Discard last 0xff */ |
| 401 | assert(!erterm); |
| 402 | mqc->bp --; |
| 403 | } else if (mqc->ct == 8 && !erterm && |
| 404 | mqc->bp[-1] == 0x7f && mqc->bp[-2] == 0xff) { |
| 405 | /* Tiny optimization: discard terminating 0xff 0x7f since it is */ |
| 406 | /* interpreted as 0xff 0x7f [0xff 0xff] by the decoder, and given */ |
| 407 | /* the bit stuffing, in fact as 0xff 0xff [0xff ..] */ |
| 408 | /* Happens once on opj_compress -i ../MAPA.tif -o MAPA.j2k -M 1 */ |
| 409 | mqc->bp -= 2; |
| 410 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 411 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 412 | assert(mqc->bp[-1] != 0xff); |
| 413 | } |
| 414 | |
| 415 | void opj_mqc_reset_enc(opj_mqc_t *mqc) |
| 416 | { |
| 417 | opj_mqc_resetstates(mqc); |
| 418 | opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46); |
| 419 | opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3); |
| 420 | opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); |
| 421 | } |
| 422 | |
| 423 | #ifdef notdef |
| 424 | OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc) |
| 425 | { |
| 426 | OPJ_UINT32 correction = 1; |
| 427 | |
| 428 | /* <flush part> */ |
| 429 | OPJ_INT32 n = (OPJ_INT32)(27 - 15 - mqc->ct); |
| 430 | mqc->c <<= mqc->ct; |
| 431 | while (n > 0) { |
| 432 | opj_mqc_byteout(mqc); |
| 433 | n -= (OPJ_INT32)mqc->ct; |
| 434 | mqc->c <<= mqc->ct; |
| 435 | } |
| 436 | opj_mqc_byteout(mqc); |
| 437 | |
| 438 | return correction; |
| 439 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 440 | #endif |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 441 | |
| 442 | void opj_mqc_restart_init_enc(opj_mqc_t *mqc) |
| 443 | { |
| 444 | /* <Re-init part> */ |
| 445 | |
| 446 | /* As specified in Figure C.10 - Initialization of the encoder */ |
| 447 | /* (C.2.8 Initialization of the encoder (INITENC)) */ |
| 448 | mqc->a = 0x8000; |
| 449 | mqc->c = 0; |
| 450 | mqc->ct = 12; |
| 451 | /* This function is normally called after at least one opj_mqc_flush() */ |
| 452 | /* which will have advance mqc->bp by at least 2 bytes beyond its */ |
| 453 | /* initial position */ |
| 454 | mqc->bp --; |
| 455 | assert(mqc->bp >= mqc->start - 1); |
| 456 | assert(*mqc->bp != 0xff); |
| 457 | if (*mqc->bp == 0xff) { |
| 458 | mqc->ct = 13; |
| 459 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 462 | void opj_mqc_erterm_enc(opj_mqc_t *mqc) |
| 463 | { |
| 464 | OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1); |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 465 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 466 | while (k > 0) { |
| 467 | mqc->c <<= mqc->ct; |
| 468 | mqc->ct = 0; |
| 469 | opj_mqc_byteout(mqc); |
| 470 | k -= (OPJ_INT32)mqc->ct; |
| 471 | } |
| 472 | |
| 473 | if (*mqc->bp != 0xff) { |
| 474 | opj_mqc_byteout(mqc); |
| 475 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 478 | void opj_mqc_segmark_enc(opj_mqc_t *mqc) |
| 479 | { |
| 480 | OPJ_UINT32 i; |
| 481 | opj_mqc_setcurctx(mqc, 18); |
| 482 | |
| 483 | for (i = 1; i < 5; i++) { |
| 484 | opj_mqc_encode(mqc, i % 2); |
| 485 | } |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 488 | static void opj_mqc_init_dec_common(opj_mqc_t *mqc, |
| 489 | OPJ_BYTE *bp, |
| 490 | OPJ_UINT32 len, |
| 491 | OPJ_UINT32 extra_writable_bytes) |
| 492 | { |
| 493 | (void)extra_writable_bytes; |
| 494 | |
| 495 | assert(extra_writable_bytes >= OPJ_COMMON_CBLK_DATA_EXTRA); |
| 496 | mqc->start = bp; |
| 497 | mqc->end = bp + len; |
| 498 | /* Insert an artificial 0xFF 0xFF marker at end of the code block */ |
| 499 | /* data so that the bytein routines stop on it. This saves us comparing */ |
| 500 | /* the bp and end pointers */ |
| 501 | /* But before inserting it, backup th bytes we will overwrite */ |
| 502 | memcpy(mqc->backup, mqc->end, OPJ_COMMON_CBLK_DATA_EXTRA); |
| 503 | mqc->end[0] = 0xFF; |
| 504 | mqc->end[1] = 0xFF; |
| 505 | mqc->bp = bp; |
| 506 | } |
| 507 | void opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len, |
| 508 | OPJ_UINT32 extra_writable_bytes) |
| 509 | { |
| 510 | /* Implements ISO 15444-1 C.3.5 Initialization of the decoder (INITDEC) */ |
| 511 | /* Note: alternate "J.1 - Initialization of the software-conventions */ |
| 512 | /* decoder" has been tried, but does */ |
| 513 | /* not bring any improvement. */ |
| 514 | /* See https://github.com/uclouvain/openjpeg/issues/921 */ |
| 515 | opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes); |
| 516 | opj_mqc_setcurctx(mqc, 0); |
| 517 | mqc->end_of_byte_stream_counter = 0; |
| 518 | if (len == 0) { |
| 519 | mqc->c = 0xff << 16; |
| 520 | } else { |
| 521 | mqc->c = (OPJ_UINT32)(*mqc->bp << 16); |
| 522 | } |
| 523 | |
| 524 | opj_mqc_bytein(mqc); |
| 525 | mqc->c <<= 7; |
| 526 | mqc->ct -= 7; |
| 527 | mqc->a = 0x8000; |
| 528 | } |
| 529 | |
| 530 | |
| 531 | void opj_mqc_raw_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len, |
| 532 | OPJ_UINT32 extra_writable_bytes) |
| 533 | { |
| 534 | opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes); |
| 535 | mqc->c = 0; |
| 536 | mqc->ct = 0; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | void opq_mqc_finish_dec(opj_mqc_t *mqc) |
| 541 | { |
| 542 | /* Restore the bytes overwritten by opj_mqc_init_dec_common() */ |
| 543 | memcpy(mqc->end, mqc->backup, OPJ_COMMON_CBLK_DATA_EXTRA); |
| 544 | } |
| 545 | |
| 546 | void opj_mqc_resetstates(opj_mqc_t *mqc) |
| 547 | { |
| 548 | OPJ_UINT32 i; |
| 549 | for (i = 0; i < MQC_NUMCTXS; i++) { |
| 550 | mqc->ctxs[i] = mqc_states; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, |
| 555 | OPJ_INT32 prob) |
| 556 | { |
| 557 | mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)]; |
Philip P. Moltmann | ac3d58c | 2016-03-04 15:19:21 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |