Grok  9.7.5
mqc_enc_inl.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2022 Grok Image Compression Inc.
3  *
4  * This source code is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License, version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This source code is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Affero General Public License for more details.
12  *
13  * You should have received a copy of the GNU Affero General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  *
17  * This source code incorporates work covered by the BSD 2-clause license.
18  * Please see the LICENSE file in the root directory for details.
19  *
20  */
21 
22 #pragma once
23 
29 void mqc_byteout(mqcoder* mqc);
30 
38 #define mqc_renorme_macro(mqc, a_, c_, ct_) \
39  { \
40  do \
41  { \
42  a_ <<= 1; \
43  c_ <<= 1; \
44  ct_--; \
45  if(ct_ == 0) \
46  { \
47  mqc->c = c_; \
48  mqc_byteout(mqc); \
49  c_ = mqc->c; \
50  ct_ = mqc->ct; \
51  } \
52  } while((a_ & 0x8000) == 0); \
53  }
54 
55 #define mqc_codemps_macro(mqc, curctx, a, c, ct) \
56  { \
57  a -= (*curctx)->qeval; \
58  if((a & 0x8000) == 0) \
59  { \
60  if(a < (*curctx)->qeval) \
61  a = (*curctx)->qeval; \
62  else \
63  c += (*curctx)->qeval; \
64  *curctx = (*curctx)->nmps; \
65  mqc_renorme_macro(mqc, a, c, ct); \
66  } \
67  else \
68  { \
69  c += (*curctx)->qeval; \
70  } \
71  }
72 
73 #define mqc_codelps_macro(mqc, curctx, a, c, ct) \
74  { \
75  a -= (*curctx)->qeval; \
76  if(a < (*curctx)->qeval) \
77  c += (*curctx)->qeval; \
78  else \
79  a = (*curctx)->qeval; \
80  *curctx = (*curctx)->nlps; \
81  mqc_renorme_macro(mqc, a, c, ct); \
82  }
83 
84 #define mqc_encode_macro(mqc, curctx, a, c, ct, d) \
85  { \
86  if((*curctx)->mps == (d)) \
87  mqc_codemps_macro(mqc, curctx, a, c, ct) else mqc_codelps_macro(mqc, curctx, a, c, ct) \
88  }
89 
90 #define mqc_bypass_enc_macro(mqc, c, ct, d) \
91  { \
92  if(ct == BYPASS_CT_INIT) \
93  ct = 8; \
94  ct--; \
95  c = c + ((d) << ct); \
96  if(ct == 0) \
97  { \
98  *mqc->bp = (uint8_t)c; \
99  ct = 8; \
100  /* If the previous byte was 0xff, make sure that the next msb is 0 */ \
101  if(*mqc->bp == 0xff) \
102  ct = 7; \
103  mqc->bp++; \
104  c = 0; \
105  } \
106  }
void mqc_byteout(mqcoder *mqc)
Output a byte, doing bit-stuffing if necessary.