Lines Matching +full:- +full:qe

5  * Developed 1997-2015 by Guido Vollbeding.
6 * libjpeg-turbo Modifications:
7 * Copyright (C) 2015-2020, 2022, D. R. Commander.
12 * (implementing Recommendation ITU-T T.81 | ISO/IEC 10918-1).
19 * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
27 #define NEG_1 ((unsigned int)-1)
38 /* init: ct = -16 */
40 /* error: ct = -1 */
77 struct jpeg_source_mgr *src = cinfo->src; in get_byte()
79 if (src->bytes_in_buffer == 0) in get_byte()
80 if (!(*src->fill_input_buffer) (cinfo)) in get_byte()
82 src->bytes_in_buffer--; in get_byte()
83 return *src->next_input_byte++; in get_byte()
90 * Machine-dependent optimization facilities
101 * coding interval (16 bits) and the next-bits buffer,
102 * the cut-point between these two parts is floating
117 register arith_entropy_ptr e = (arith_entropy_ptr)cinfo->entropy; in arith_decode()
119 register JLONG qe, temp; in arith_decode() local
123 while (e->a < 0x8000L) { in arith_decode()
124 if (--e->ct < 0) { in arith_decode()
126 if (cinfo->unread_marker) in arith_decode()
142 cinfo->unread_marker = data; in arith_decode()
147 e->c = (e->c << 8) | data; /* insert data into C register */ in arith_decode()
148 if ((e->ct += 8) < 0) /* update bit shift counter */ in arith_decode()
150 if (++e->ct == 0) in arith_decode()
151 /* Got 2 initial bytes -> re-init A and exit loop */ in arith_decode()
152 e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ in arith_decode()
154 e->a <<= 1; in arith_decode()
158 * Qe values and probability estimation state machine in arith_decode()
161 qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ in arith_decode()
162 nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ in arith_decode()
163 nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ in arith_decode()
166 temp = e->a - qe; in arith_decode()
167 e->a = temp; in arith_decode()
168 temp <<= e->ct; in arith_decode()
169 if (e->c >= temp) { in arith_decode()
170 e->c -= temp; in arith_decode()
172 if (e->a < qe) { in arith_decode()
173 e->a = qe; in arith_decode()
176 e->a = qe; in arith_decode()
180 } else if (e->a < 0x8000L) { in arith_decode()
182 if (e->a < qe) { in arith_decode()
201 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in process_restart()
206 if (!(*cinfo->marker->read_restart_marker) (cinfo)) in process_restart()
209 /* Re-initialize statistics areas */ in process_restart()
210 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { in process_restart()
211 compptr = cinfo->cur_comp_info[ci]; in process_restart()
212 if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { in process_restart()
213 memset(entropy->dc_stats[compptr->dc_tbl_no], 0, DC_STAT_BINS); in process_restart()
215 entropy->last_dc_val[ci] = 0; in process_restart()
216 entropy->dc_context[ci] = 0; in process_restart()
218 if (!cinfo->progressive_mode || cinfo->Ss) { in process_restart()
219 memset(entropy->ac_stats[compptr->ac_tbl_no], 0, AC_STAT_BINS); in process_restart()
224 entropy->c = 0; in process_restart()
225 entropy->a = 0; in process_restart()
226 entropy->ct = -16; /* force reading 2 initial bytes to fill C */ in process_restart()
229 entropy->restarts_to_go = cinfo->restart_interval; in process_restart()
236 * arithmetic-compressed coefficients.
252 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in decode_mcu_DC_first()
259 if (cinfo->restart_interval) { in decode_mcu_DC_first()
260 if (entropy->restarts_to_go == 0) in decode_mcu_DC_first()
262 entropy->restarts_to_go--; in decode_mcu_DC_first()
265 if (entropy->ct == -1) return TRUE; /* if error do nothing */ in decode_mcu_DC_first()
269 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { in decode_mcu_DC_first()
271 ci = cinfo->MCU_membership[blkn]; in decode_mcu_DC_first()
272 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; in decode_mcu_DC_first()
277 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; in decode_mcu_DC_first()
281 entropy->dc_context[ci] = 0; in decode_mcu_DC_first()
289 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ in decode_mcu_DC_first()
293 entropy->ct = -1; /* magnitude overflow */ in decode_mcu_DC_first()
300 if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1)) in decode_mcu_DC_first()
301 entropy->dc_context[ci] = 0; /* zero diff category */ in decode_mcu_DC_first()
302 else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1)) in decode_mcu_DC_first()
303 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ in decode_mcu_DC_first()
305 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ in decode_mcu_DC_first()
311 v += 1; if (sign) v = -v; in decode_mcu_DC_first()
312 entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff; in decode_mcu_DC_first()
316 (*block)[0] = (JCOEF)LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al); in decode_mcu_DC_first()
331 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in decode_mcu_AC_first()
338 if (cinfo->restart_interval) { in decode_mcu_AC_first()
339 if (entropy->restarts_to_go == 0) in decode_mcu_AC_first()
341 entropy->restarts_to_go--; in decode_mcu_AC_first()
344 if (entropy->ct == -1) return TRUE; /* if error do nothing */ in decode_mcu_AC_first()
348 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; in decode_mcu_AC_first()
353 for (k = cinfo->Ss; k <= cinfo->Se; k++) { in decode_mcu_AC_first()
354 st = entropy->ac_stats[tbl] + 3 * (k - 1); in decode_mcu_AC_first()
358 if (k > cinfo->Se) { in decode_mcu_AC_first()
360 entropy->ct = -1; /* spectral overflow */ in decode_mcu_AC_first()
366 sign = arith_decode(cinfo, entropy->fixed_bin); in decode_mcu_AC_first()
372 st = entropy->ac_stats[tbl] + in decode_mcu_AC_first()
373 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); in decode_mcu_AC_first()
377 entropy->ct = -1; /* magnitude overflow */ in decode_mcu_AC_first()
389 v += 1; if (sign) v = -v; in decode_mcu_AC_first()
391 (*block)[jpeg_natural_order[k]] = (JCOEF)((unsigned)v << cinfo->Al); in decode_mcu_AC_first()
405 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in decode_mcu_DC_refine()
410 if (cinfo->restart_interval) { in decode_mcu_DC_refine()
411 if (entropy->restarts_to_go == 0) in decode_mcu_DC_refine()
413 entropy->restarts_to_go--; in decode_mcu_DC_refine()
416 st = entropy->fixed_bin; /* use fixed probability estimation */ in decode_mcu_DC_refine()
417 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ in decode_mcu_DC_refine()
421 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { in decode_mcu_DC_refine()
422 /* Encoded data is simply the next bit of the two's-complement DC value */ in decode_mcu_DC_refine()
438 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in decode_mcu_AC_refine()
446 if (cinfo->restart_interval) { in decode_mcu_AC_refine()
447 if (entropy->restarts_to_go == 0) in decode_mcu_AC_refine()
449 entropy->restarts_to_go--; in decode_mcu_AC_refine()
452 if (entropy->ct == -1) return TRUE; /* if error do nothing */ in decode_mcu_AC_refine()
456 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; in decode_mcu_AC_refine()
458 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ in decode_mcu_AC_refine()
459 m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */ in decode_mcu_AC_refine()
461 /* Establish EOBx (previous stage end-of-block) index */ in decode_mcu_AC_refine()
462 for (kex = cinfo->Se; kex > 0; kex--) in decode_mcu_AC_refine()
465 for (k = cinfo->Ss; k <= cinfo->Se; k++) { in decode_mcu_AC_refine()
466 st = entropy->ac_stats[tbl] + 3 * (k - 1); in decode_mcu_AC_refine()
481 if (arith_decode(cinfo, entropy->fixed_bin)) in decode_mcu_AC_refine()
488 if (k > cinfo->Se) { in decode_mcu_AC_refine()
490 entropy->ct = -1; /* spectral overflow */ in decode_mcu_AC_refine()
501 * Decode one MCU's worth of arithmetic-compressed coefficients.
507 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in decode_mcu()
515 if (cinfo->restart_interval) { in decode_mcu()
516 if (entropy->restarts_to_go == 0) in decode_mcu()
518 entropy->restarts_to_go--; in decode_mcu()
521 if (entropy->ct == -1) return TRUE; /* if error do nothing */ in decode_mcu()
525 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { in decode_mcu()
527 ci = cinfo->MCU_membership[blkn]; in decode_mcu()
528 compptr = cinfo->cur_comp_info[ci]; in decode_mcu()
532 tbl = compptr->dc_tbl_no; in decode_mcu()
535 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; in decode_mcu()
539 entropy->dc_context[ci] = 0; in decode_mcu()
547 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ in decode_mcu()
551 entropy->ct = -1; /* magnitude overflow */ in decode_mcu()
558 if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1)) in decode_mcu()
559 entropy->dc_context[ci] = 0; /* zero diff category */ in decode_mcu()
560 else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1)) in decode_mcu()
561 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ in decode_mcu()
563 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ in decode_mcu()
569 v += 1; if (sign) v = -v; in decode_mcu()
570 entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff; in decode_mcu()
574 (*block)[0] = (JCOEF)entropy->last_dc_val[ci]; in decode_mcu()
578 tbl = compptr->ac_tbl_no; in decode_mcu()
581 for (k = 1; k <= DCTSIZE2 - 1; k++) { in decode_mcu()
582 st = entropy->ac_stats[tbl] + 3 * (k - 1); in decode_mcu()
586 if (k > DCTSIZE2 - 1) { in decode_mcu()
588 entropy->ct = -1; /* spectral overflow */ in decode_mcu()
594 sign = arith_decode(cinfo, entropy->fixed_bin); in decode_mcu()
600 st = entropy->ac_stats[tbl] + in decode_mcu()
601 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); in decode_mcu()
605 entropy->ct = -1; /* magnitude overflow */ in decode_mcu()
617 v += 1; if (sign) v = -v; in decode_mcu()
628 * Initialize for an arithmetic-compressed scan.
634 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in start_pass()
638 if (cinfo->progressive_mode) { in start_pass()
640 if (cinfo->Ss == 0) { in start_pass()
641 if (cinfo->Se != 0) in start_pass()
645 if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1) in start_pass()
648 if (cinfo->comps_in_scan != 1) in start_pass()
651 if (cinfo->Ah != 0) { in start_pass()
652 /* Successive approximation refinement scan: must have Al = Ah-1. */ in start_pass()
653 if (cinfo->Ah - 1 != cinfo->Al) in start_pass()
656 if (cinfo->Al > 13) { /* need not check for < 0 */ in start_pass()
659 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); in start_pass()
662 * Note that inter-scan inconsistencies are treated as warnings in start_pass()
665 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { in start_pass()
666 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; in start_pass()
667 int *coef_bit_ptr = &cinfo->coef_bits[cindex][0]; in start_pass()
669 &cinfo->coef_bits[cindex + cinfo->num_components][0]; in start_pass()
670 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ in start_pass()
672 for (coefi = MIN(cinfo->Ss, 1); coefi <= MAX(cinfo->Se, 9); coefi++) { in start_pass()
673 if (cinfo->input_scan_number > 1) in start_pass()
678 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { in start_pass()
680 if (cinfo->Ah != expected) in start_pass()
682 coef_bit_ptr[coefi] = cinfo->Al; in start_pass()
686 if (cinfo->Ah == 0) { in start_pass()
687 if (cinfo->Ss == 0) in start_pass()
688 entropy->pub.decode_mcu = decode_mcu_DC_first; in start_pass()
690 entropy->pub.decode_mcu = decode_mcu_AC_first; in start_pass()
692 if (cinfo->Ss == 0) in start_pass()
693 entropy->pub.decode_mcu = decode_mcu_DC_refine; in start_pass()
695 entropy->pub.decode_mcu = decode_mcu_AC_refine; in start_pass()
701 if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2 - 1 || in start_pass()
702 cinfo->Ah != 0 || cinfo->Al != 0) in start_pass()
705 entropy->pub.decode_mcu = decode_mcu; in start_pass()
709 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { in start_pass()
710 compptr = cinfo->cur_comp_info[ci]; in start_pass()
711 if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { in start_pass()
712 tbl = compptr->dc_tbl_no; in start_pass()
715 if (entropy->dc_stats[tbl] == NULL) in start_pass()
716 entropy->dc_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small) in start_pass()
718 memset(entropy->dc_stats[tbl], 0, DC_STAT_BINS); in start_pass()
720 entropy->last_dc_val[ci] = 0; in start_pass()
721 entropy->dc_context[ci] = 0; in start_pass()
723 if (!cinfo->progressive_mode || cinfo->Ss) { in start_pass()
724 tbl = compptr->ac_tbl_no; in start_pass()
727 if (entropy->ac_stats[tbl] == NULL) in start_pass()
728 entropy->ac_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small) in start_pass()
730 memset(entropy->ac_stats[tbl], 0, AC_STAT_BINS); in start_pass()
735 entropy->c = 0; in start_pass()
736 entropy->a = 0; in start_pass()
737 entropy->ct = -16; /* force reading 2 initial bytes to fill C */ in start_pass()
738 entropy->pub.insufficient_data = FALSE; in start_pass()
741 entropy->restarts_to_go = cinfo->restart_interval; in start_pass()
756 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, in jinit_arith_decoder()
758 cinfo->entropy = (struct jpeg_entropy_decoder *)entropy; in jinit_arith_decoder()
759 entropy->pub.start_pass = start_pass; in jinit_arith_decoder()
763 entropy->dc_stats[i] = NULL; in jinit_arith_decoder()
764 entropy->ac_stats[i] = NULL; in jinit_arith_decoder()
768 entropy->fixed_bin[0] = 113; in jinit_arith_decoder()
770 if (cinfo->progressive_mode) { in jinit_arith_decoder()
773 cinfo->coef_bits = (int (*)[DCTSIZE2]) in jinit_arith_decoder()
774 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, in jinit_arith_decoder()
775 cinfo->num_components * 2 * DCTSIZE2 * in jinit_arith_decoder()
777 coef_bit_ptr = &cinfo->coef_bits[0][0]; in jinit_arith_decoder()
778 for (ci = 0; ci < cinfo->num_components; ci++) in jinit_arith_decoder()
780 *coef_bit_ptr++ = -1; in jinit_arith_decoder()