Move some MQC functions into a header for speed
[openjpeg.git] / src / lib / openjp2 / mqc.c
1 /*
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 
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
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux 
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
41 /** @defgroup MQC MQC - Implementation of an MQ-Coder */
42 /*@{*/
43
44 /** @name Local static functions */
45 /*@{*/
46
47 /**
48 Output a byte, doing bit-stuffing if necessary.
49 After a 0xff byte, the next byte must be smaller than 0x90.
50 @param mqc MQC handle
51 */
52 static void opj_mqc_byteout(opj_mqc_t *mqc);
53 /**
54 Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
55 @param mqc MQC handle
56 */
57 static void opj_mqc_renorme(opj_mqc_t *mqc);
58 /**
59 Encode the most probable symbol
60 @param mqc MQC handle
61 */
62 static void opj_mqc_codemps(opj_mqc_t *mqc);
63 /**
64 Encode the most least symbol
65 @param mqc MQC handle
66 */
67 static void opj_mqc_codelps(opj_mqc_t *mqc);
68 /**
69 Fill mqc->c with 1's for flushing
70 @param mqc MQC handle
71 */
72 static void opj_mqc_setbits(opj_mqc_t *mqc);
73 /*@}*/
74
75 /*@}*/
76
77 /* <summary> */
78 /* This array defines all the possible states for a context. */
79 /* </summary> */
80 static opj_mqc_state_t mqc_states[47 * 2] = {
81         {0x5601, 0, &mqc_states[2], &mqc_states[3]},
82         {0x5601, 1, &mqc_states[3], &mqc_states[2]},
83         {0x3401, 0, &mqc_states[4], &mqc_states[12]},
84         {0x3401, 1, &mqc_states[5], &mqc_states[13]},
85         {0x1801, 0, &mqc_states[6], &mqc_states[18]},
86         {0x1801, 1, &mqc_states[7], &mqc_states[19]},
87         {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
88         {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
89         {0x0521, 0, &mqc_states[10], &mqc_states[58]},
90         {0x0521, 1, &mqc_states[11], &mqc_states[59]},
91         {0x0221, 0, &mqc_states[76], &mqc_states[66]},
92         {0x0221, 1, &mqc_states[77], &mqc_states[67]},
93         {0x5601, 0, &mqc_states[14], &mqc_states[13]},
94         {0x5601, 1, &mqc_states[15], &mqc_states[12]},
95         {0x5401, 0, &mqc_states[16], &mqc_states[28]},
96         {0x5401, 1, &mqc_states[17], &mqc_states[29]},
97         {0x4801, 0, &mqc_states[18], &mqc_states[28]},
98         {0x4801, 1, &mqc_states[19], &mqc_states[29]},
99         {0x3801, 0, &mqc_states[20], &mqc_states[28]},
100         {0x3801, 1, &mqc_states[21], &mqc_states[29]},
101         {0x3001, 0, &mqc_states[22], &mqc_states[34]},
102         {0x3001, 1, &mqc_states[23], &mqc_states[35]},
103         {0x2401, 0, &mqc_states[24], &mqc_states[36]},
104         {0x2401, 1, &mqc_states[25], &mqc_states[37]},
105         {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
106         {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
107         {0x1601, 0, &mqc_states[58], &mqc_states[42]},
108         {0x1601, 1, &mqc_states[59], &mqc_states[43]},
109         {0x5601, 0, &mqc_states[30], &mqc_states[29]},
110         {0x5601, 1, &mqc_states[31], &mqc_states[28]},
111         {0x5401, 0, &mqc_states[32], &mqc_states[28]},
112         {0x5401, 1, &mqc_states[33], &mqc_states[29]},
113         {0x5101, 0, &mqc_states[34], &mqc_states[30]},
114         {0x5101, 1, &mqc_states[35], &mqc_states[31]},
115         {0x4801, 0, &mqc_states[36], &mqc_states[32]},
116         {0x4801, 1, &mqc_states[37], &mqc_states[33]},
117         {0x3801, 0, &mqc_states[38], &mqc_states[34]},
118         {0x3801, 1, &mqc_states[39], &mqc_states[35]},
119         {0x3401, 0, &mqc_states[40], &mqc_states[36]},
120         {0x3401, 1, &mqc_states[41], &mqc_states[37]},
121         {0x3001, 0, &mqc_states[42], &mqc_states[38]},
122         {0x3001, 1, &mqc_states[43], &mqc_states[39]},
123         {0x2801, 0, &mqc_states[44], &mqc_states[38]},
124         {0x2801, 1, &mqc_states[45], &mqc_states[39]},
125         {0x2401, 0, &mqc_states[46], &mqc_states[40]},
126         {0x2401, 1, &mqc_states[47], &mqc_states[41]},
127         {0x2201, 0, &mqc_states[48], &mqc_states[42]},
128         {0x2201, 1, &mqc_states[49], &mqc_states[43]},
129         {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
130         {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
131         {0x1801, 0, &mqc_states[52], &mqc_states[46]},
132         {0x1801, 1, &mqc_states[53], &mqc_states[47]},
133         {0x1601, 0, &mqc_states[54], &mqc_states[48]},
134         {0x1601, 1, &mqc_states[55], &mqc_states[49]},
135         {0x1401, 0, &mqc_states[56], &mqc_states[50]},
136         {0x1401, 1, &mqc_states[57], &mqc_states[51]},
137         {0x1201, 0, &mqc_states[58], &mqc_states[52]},
138         {0x1201, 1, &mqc_states[59], &mqc_states[53]},
139         {0x1101, 0, &mqc_states[60], &mqc_states[54]},
140         {0x1101, 1, &mqc_states[61], &mqc_states[55]},
141         {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
142         {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
143         {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
144         {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
145         {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
146         {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
147         {0x0521, 0, &mqc_states[68], &mqc_states[62]},
148         {0x0521, 1, &mqc_states[69], &mqc_states[63]},
149         {0x0441, 0, &mqc_states[70], &mqc_states[64]},
150         {0x0441, 1, &mqc_states[71], &mqc_states[65]},
151         {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
152         {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
153         {0x0221, 0, &mqc_states[74], &mqc_states[68]},
154         {0x0221, 1, &mqc_states[75], &mqc_states[69]},
155         {0x0141, 0, &mqc_states[76], &mqc_states[70]},
156         {0x0141, 1, &mqc_states[77], &mqc_states[71]},
157         {0x0111, 0, &mqc_states[78], &mqc_states[72]},
158         {0x0111, 1, &mqc_states[79], &mqc_states[73]},
159         {0x0085, 0, &mqc_states[80], &mqc_states[74]},
160         {0x0085, 1, &mqc_states[81], &mqc_states[75]},
161         {0x0049, 0, &mqc_states[82], &mqc_states[76]},
162         {0x0049, 1, &mqc_states[83], &mqc_states[77]},
163         {0x0025, 0, &mqc_states[84], &mqc_states[78]},
164         {0x0025, 1, &mqc_states[85], &mqc_states[79]},
165         {0x0015, 0, &mqc_states[86], &mqc_states[80]},
166         {0x0015, 1, &mqc_states[87], &mqc_states[81]},
167         {0x0009, 0, &mqc_states[88], &mqc_states[82]},
168         {0x0009, 1, &mqc_states[89], &mqc_states[83]},
169         {0x0005, 0, &mqc_states[90], &mqc_states[84]},
170         {0x0005, 1, &mqc_states[91], &mqc_states[85]},
171         {0x0001, 0, &mqc_states[90], &mqc_states[86]},
172         {0x0001, 1, &mqc_states[91], &mqc_states[87]},
173         {0x5601, 0, &mqc_states[92], &mqc_states[92]},
174         {0x5601, 1, &mqc_states[93], &mqc_states[93]},
175 };
176
177 /* 
178 ==========================================================
179    local functions
180 ==========================================================
181 */
182
183 static void opj_mqc_byteout(opj_mqc_t *mqc) {
184         /* avoid accessing uninitialized memory*/
185         if (mqc->bp == mqc->start-1) {
186                 mqc->bp++;
187                 *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
188                 mqc->c &= 0x7ffff;
189                 mqc->ct = 8;
190         }
191         else if (*mqc->bp == 0xff) {
192                 mqc->bp++;
193                 *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
194                 mqc->c &= 0xfffff;
195                 mqc->ct = 7;
196         } else {
197                 if ((mqc->c & 0x8000000) == 0) {        
198                         mqc->bp++;
199                         *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
200                         mqc->c &= 0x7ffff;
201                         mqc->ct = 8;
202                 } else {
203                         (*mqc->bp)++;
204                         if (*mqc->bp == 0xff) {
205                                 mqc->c &= 0x7ffffff;
206                                 mqc->bp++;
207                                 *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
208                                 mqc->c &= 0xfffff;
209                                 mqc->ct = 7;
210                         } else {
211                                 mqc->bp++;
212                                 *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
213                                 mqc->c &= 0x7ffff;
214                                 mqc->ct = 8;
215                         }
216                 }
217         }
218 }
219
220 static void opj_mqc_renorme(opj_mqc_t *mqc) {
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);
229 }
230
231 static void opj_mqc_codemps(opj_mqc_t *mqc) {
232         mqc->a -= (*mqc->curctx)->qeval;
233         if ((mqc->a & 0x8000) == 0) {
234                 if (mqc->a < (*mqc->curctx)->qeval) {
235                         mqc->a = (*mqc->curctx)->qeval;
236                 } else {
237                         mqc->c += (*mqc->curctx)->qeval;
238                 }
239                 *mqc->curctx = (*mqc->curctx)->nmps;
240                 opj_mqc_renorme(mqc);
241         } else {
242                 mqc->c += (*mqc->curctx)->qeval;
243         }
244 }
245
246 static void opj_mqc_codelps(opj_mqc_t *mqc) {
247         mqc->a -= (*mqc->curctx)->qeval;
248         if (mqc->a < (*mqc->curctx)->qeval) {
249                 mqc->c += (*mqc->curctx)->qeval;
250         } else {
251                 mqc->a = (*mqc->curctx)->qeval;
252         }
253         *mqc->curctx = (*mqc->curctx)->nlps;
254         opj_mqc_renorme(mqc);
255 }
256
257 static void opj_mqc_setbits(opj_mqc_t *mqc) {
258         OPJ_UINT32 tempc = mqc->c + mqc->a;
259         mqc->c |= 0xffff;
260         if (mqc->c >= tempc) {
261                 mqc->c -= 0x8000;
262         }
263 }
264
265 /* 
266 ==========================================================
267    MQ-Coder interface
268 ==========================================================
269 */
270
271 opj_mqc_t* opj_mqc_create(void) {
272         opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
273 #ifdef MQC_PERF_OPT
274         if (mqc) {
275                 mqc->buffer = NULL;
276         }
277 #endif
278         return mqc;
279 }
280
281 void opj_mqc_destroy(opj_mqc_t *mqc) {
282         if(mqc) {
283 #ifdef MQC_PERF_OPT
284                 if (mqc->buffer) {
285                         opj_free(mqc->buffer);
286                 }
287 #endif
288                 opj_free(mqc);
289         }
290 }
291
292 OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc) {
293         const ptrdiff_t diff = mqc->bp - mqc->start;
294 #if 0
295   assert( diff <= 0xffffffff && diff >= 0 ); /* UINT32_MAX */
296 #endif
297         return (OPJ_UINT32)diff;
298 }
299
300 void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp) {
301     /* TODO MSD: need to take a look to the v2 version */
302         opj_mqc_setcurctx(mqc, 0);
303         mqc->a = 0x8000;
304         mqc->c = 0;
305         mqc->bp = bp - 1;
306         mqc->ct = 12;
307         mqc->start = bp;
308 }
309
310 void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d) {
311         if ((*mqc->curctx)->mps == d) {
312                 opj_mqc_codemps(mqc);
313         } else {
314                 opj_mqc_codelps(mqc);
315         }
316 }
317
318 void opj_mqc_flush(opj_mqc_t *mqc) {
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         if (*mqc->bp != 0xff) {
326                 mqc->bp++;
327         }
328 }
329
330 void opj_mqc_bypass_init_enc(opj_mqc_t *mqc) {
331         mqc->c = 0;
332         mqc->ct = 8;
333         /*if (*mqc->bp == 0xff) {
334         mqc->ct = 7;
335      } */
336 }
337
338 void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d) {
339         mqc->ct--;
340         mqc->c = mqc->c + (d << mqc->ct);
341         if (mqc->ct == 0) {
342                 mqc->bp++;
343                 *mqc->bp = (OPJ_BYTE)mqc->c;
344                 mqc->ct = 8;
345                 if (*mqc->bp == 0xff) {
346                         mqc->ct = 7;
347                 }
348                 mqc->c = 0;
349         }
350 }
351
352 OPJ_UINT32 opj_mqc_bypass_flush_enc(opj_mqc_t *mqc) {
353         OPJ_BYTE bit_padding;
354         
355         bit_padding = 0;
356         
357         if (mqc->ct != 0) {
358                 while (mqc->ct > 0) {
359                         mqc->ct--;
360                         mqc->c += (OPJ_UINT32)(bit_padding << mqc->ct);
361                         bit_padding = (bit_padding + 1) & 0x01;
362                 }
363                 mqc->bp++;
364                 *mqc->bp = (OPJ_BYTE)mqc->c;
365                 mqc->ct = 8;
366                 mqc->c = 0;
367         }
368         
369         return 1;
370 }
371
372 void opj_mqc_reset_enc(opj_mqc_t *mqc) {
373         opj_mqc_resetstates(mqc);
374         opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
375         opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
376         opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
377 }
378
379 OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc) {
380         OPJ_UINT32 correction = 1;
381         
382         /* <flush part> */
383         OPJ_INT32 n = (OPJ_INT32)(27 - 15 - mqc->ct);
384         mqc->c <<= mqc->ct;
385         while (n > 0) {
386                 opj_mqc_byteout(mqc);
387                 n -= (OPJ_INT32)mqc->ct;
388                 mqc->c <<= mqc->ct;
389         }
390         opj_mqc_byteout(mqc);
391         
392         return correction;
393 }
394
395 void opj_mqc_restart_init_enc(opj_mqc_t *mqc) {
396         /* <Re-init part> */
397         opj_mqc_setcurctx(mqc, 0);
398         mqc->a = 0x8000;
399         mqc->c = 0;
400         mqc->ct = 12;
401         mqc->bp--;
402         if (*mqc->bp == 0xff) {
403                 mqc->ct = 13;
404         }
405 }
406
407 void opj_mqc_erterm_enc(opj_mqc_t *mqc) {
408         OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1);
409         
410         while (k > 0) {
411                 mqc->c <<= mqc->ct;
412                 mqc->ct = 0;
413                 opj_mqc_byteout(mqc);
414                 k -= (OPJ_INT32)mqc->ct;
415         }
416         
417         if (*mqc->bp != 0xff) {
418                 opj_mqc_byteout(mqc);
419         }
420 }
421
422 void opj_mqc_segmark_enc(opj_mqc_t *mqc) {
423         OPJ_UINT32 i;
424         opj_mqc_setcurctx(mqc, 18);
425         
426         for (i = 1; i < 5; i++) {
427                 opj_mqc_encode(mqc, i % 2);
428         }
429 }
430
431 OPJ_BOOL opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len) {
432         opj_mqc_setcurctx(mqc, 0);
433         mqc->start = bp;
434         mqc->end = bp + len;
435         mqc->bp = bp;
436         if (len==0) mqc->c = 0xff << 16;
437         else mqc->c = (OPJ_UINT32)(*mqc->bp << 16);
438
439 #ifdef MQC_PERF_OPT /* TODO_MSD: check this option and put in experimental */
440         {
441         OPJ_UINT32 c;
442                 OPJ_UINT32 *ip;
443                 OPJ_BYTE *end = mqc->end - 1;
444         void* new_buffer = opj_realloc(mqc->buffer, (len + 1) * sizeof(OPJ_UINT32));
445         if (! new_buffer) {
446             opj_free(mqc->buffer);
447             mqc->buffer = NULL;
448             return OPJ_FALSE;
449         }
450         mqc->buffer = new_buffer;
451                 
452         ip = (OPJ_UINT32 *) mqc->buffer;
453
454                 while (bp < end) {
455                         c = *(bp + 1);
456                         if (*bp == 0xff) {
457                                 if (c > 0x8f) {
458                                         break;
459                                 } else {
460                                         *ip = 0x00000017 | (c << 9);
461                                 }
462                         } else {
463                                 *ip = 0x00000018 | (c << 8);
464                         }
465                         bp++;
466                         ip++;
467                 }
468
469                 /* Handle last byte of data */
470                 c = 0xff;
471                 if (*bp == 0xff) {
472                         *ip = 0x0000ff18;
473                 } else {
474                         bp++;
475                         *ip = 0x00000018 | (c << 8);
476                 }
477                 ip++;
478
479                 *ip = 0x0000ff08;
480                 mqc->bp = mqc->buffer;
481         }
482 #endif
483         opj_mqc_bytein(mqc);
484         mqc->c <<= 7;
485         mqc->ct -= 7;
486         mqc->a = 0x8000;
487         return OPJ_TRUE;
488 }
489
490 void opj_mqc_resetstates(opj_mqc_t *mqc) {
491         OPJ_UINT32 i;
492         for (i = 0; i < MQC_NUMCTXS; i++) {
493                 mqc->ctxs[i] = mqc_states;
494         }
495 }
496
497 void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, OPJ_INT32 prob) {
498         mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)];
499 }
500
501