Lines Matching +full:- +full:qe

5  * Developed 1997-2009 by Guido Vollbeding.
6 * libjpeg-turbo Modifications:
12 * (implementing Recommendation ITU-T T.81 | ISO/IEC 10918-1).
19 * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
44 int next_restart_num; /* next restart number to write (0-7) */
75 * of the spec (Kx = Kmin + SRL (8 + Se - Kmin) 4).
80 * (a few - around 5 or so - bytes even for very large files),
84 * Note that currently the marker writing module emits 12-byte
85 * DAC segments for a full-component scan in a color image.
113 (ishift_temp >> (shft)) | ((~0) << (16 - (shft))) : \
125 struct jpeg_destination_mgr *dest = cinfo->dest; in emit_byte()
127 *dest->next_output_byte++ = (JOCTET)val; in emit_byte()
128 if (--dest->free_in_buffer == 0) in emit_byte()
129 if (!(*dest->empty_output_buffer) (cinfo)) in emit_byte()
135 * Finish up at the end of an arithmetic-compressed scan.
141 arith_entropy_ptr e = (arith_entropy_ptr)cinfo->entropy; in finish_pass()
146 /* Find the e->c in the coding interval with the largest in finish_pass()
148 if ((temp = (e->a - 1 + e->c) & 0xFFFF0000UL) < e->c) in finish_pass()
149 e->c = temp + 0x8000L; in finish_pass()
151 e->c = temp; in finish_pass()
153 e->c <<= e->ct; in finish_pass()
154 if (e->c & 0xF8000000UL) { in finish_pass()
156 if (e->buffer >= 0) { in finish_pass()
157 if (e->zc) in finish_pass()
159 while (--e->zc); in finish_pass()
160 emit_byte(e->buffer + 1, cinfo); in finish_pass()
161 if (e->buffer + 1 == 0xFF) in finish_pass()
164 e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */ in finish_pass()
165 e->sc = 0; in finish_pass()
167 if (e->buffer == 0) in finish_pass()
168 ++e->zc; in finish_pass()
169 else if (e->buffer >= 0) { in finish_pass()
170 if (e->zc) in finish_pass()
172 while (--e->zc); in finish_pass()
173 emit_byte(e->buffer, cinfo); in finish_pass()
175 if (e->sc) { in finish_pass()
176 if (e->zc) in finish_pass()
178 while (--e->zc); in finish_pass()
182 } while (--e->sc); in finish_pass()
186 if (e->c & 0x7FFF800L) { in finish_pass()
187 if (e->zc) /* output final pending zero bytes */ in finish_pass()
189 while (--e->zc); in finish_pass()
190 emit_byte((e->c >> 19) & 0xFF, cinfo); in finish_pass()
191 if (((e->c >> 19) & 0xFF) == 0xFF) in finish_pass()
193 if (e->c & 0x7F800L) { in finish_pass()
194 emit_byte((e->c >> 11) & 0xFF, cinfo); in finish_pass()
195 if (((e->c >> 11) & 0xFF) == 0xFF) in finish_pass()
205 * Machine-dependent optimization facilities
227 register arith_entropy_ptr e = (arith_entropy_ptr)cinfo->entropy; in arith_encode()
229 register JLONG qe, temp; in arith_encode() local
233 * Qe values and probability estimation state machine in arith_encode()
236 qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ in arith_encode()
237 nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ in arith_encode()
238 nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ in arith_encode()
241 e->a -= qe; in arith_encode()
244 if (e->a >= qe) { in arith_encode()
245 /* If the interval size (qe) for the less probable symbol (LPS) in arith_encode()
249 e->c += e->a; in arith_encode()
250 e->a = qe; in arith_encode()
255 if (e->a >= 0x8000L) in arith_encode()
256 return; /* A >= 0x8000 -> ready, no renormalization required */ in arith_encode()
257 if (e->a < qe) { in arith_encode()
258 /* If the interval size (qe) for the less probable symbol (LPS) in arith_encode()
261 e->c += e->a; in arith_encode()
262 e->a = qe; in arith_encode()
269 e->a <<= 1; in arith_encode()
270 e->c <<= 1; in arith_encode()
271 if (--e->ct == 0) { in arith_encode()
273 temp = e->c >> 19; in arith_encode()
276 if (e->buffer >= 0) { in arith_encode()
277 if (e->zc) in arith_encode()
279 while (--e->zc); in arith_encode()
280 emit_byte(e->buffer + 1, cinfo); in arith_encode()
281 if (e->buffer + 1 == 0xFF) in arith_encode()
284 e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */ in arith_encode()
285 e->sc = 0; in arith_encode()
289 e->buffer = temp & 0xFF; /* new output byte, might overflow later */ in arith_encode()
291 ++e->sc; /* stack 0xFF byte (which might overflow later) */ in arith_encode()
294 if (e->buffer == 0) in arith_encode()
295 ++e->zc; in arith_encode()
296 else if (e->buffer >= 0) { in arith_encode()
297 if (e->zc) in arith_encode()
299 while (--e->zc); in arith_encode()
300 emit_byte(e->buffer, cinfo); in arith_encode()
302 if (e->sc) { in arith_encode()
303 if (e->zc) in arith_encode()
305 while (--e->zc); in arith_encode()
309 } while (--e->sc); in arith_encode()
311 e->buffer = temp & 0xFF; /* new output byte (can still overflow) */ in arith_encode()
313 e->c &= 0x7FFFFL; in arith_encode()
314 e->ct += 8; in arith_encode()
316 } while (e->a < 0x8000L); in arith_encode()
327 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in emit_restart()
336 /* Re-initialize statistics areas */ in emit_restart()
337 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { in emit_restart()
338 compptr = cinfo->cur_comp_info[ci]; in emit_restart()
340 if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { in emit_restart()
341 MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); in emit_restart()
343 entropy->last_dc_val[ci] = 0; in emit_restart()
344 entropy->dc_context[ci] = 0; in emit_restart()
347 if (cinfo->progressive_mode == 0 || cinfo->Se) { in emit_restart()
348 MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); in emit_restart()
353 entropy->c = 0; in emit_restart()
354 entropy->a = 0x10000L; in emit_restart()
355 entropy->sc = 0; in emit_restart()
356 entropy->zc = 0; in emit_restart()
357 entropy->ct = 11; in emit_restart()
358 entropy->buffer = -1; /* empty */ in emit_restart()
370 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in encode_mcu_DC_first()
378 if (cinfo->restart_interval) { in encode_mcu_DC_first()
379 if (entropy->restarts_to_go == 0) { in encode_mcu_DC_first()
380 emit_restart(cinfo, entropy->next_restart_num); in encode_mcu_DC_first()
381 entropy->restarts_to_go = cinfo->restart_interval; in encode_mcu_DC_first()
382 entropy->next_restart_num++; in encode_mcu_DC_first()
383 entropy->next_restart_num &= 7; in encode_mcu_DC_first()
385 entropy->restarts_to_go--; in encode_mcu_DC_first()
389 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { in encode_mcu_DC_first()
391 ci = cinfo->MCU_membership[blkn]; in encode_mcu_DC_first()
392 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; in encode_mcu_DC_first()
397 m = IRIGHT_SHIFT((int)((*block)[0]), cinfo->Al); in encode_mcu_DC_first()
402 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; in encode_mcu_DC_first()
405 if ((v = m - entropy->last_dc_val[ci]) == 0) { in encode_mcu_DC_first()
407 entropy->dc_context[ci] = 0; /* zero diff category */ in encode_mcu_DC_first()
409 entropy->last_dc_val[ci] = m; in encode_mcu_DC_first()
416 entropy->dc_context[ci] = 4; /* small positive diff category */ in encode_mcu_DC_first()
418 v = -v; in encode_mcu_DC_first()
421 entropy->dc_context[ci] = 8; /* small negative diff category */ in encode_mcu_DC_first()
425 if (v -= 1) { in encode_mcu_DC_first()
429 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ in encode_mcu_DC_first()
438 if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1)) in encode_mcu_DC_first()
439 entropy->dc_context[ci] = 0; /* zero diff category */ in encode_mcu_DC_first()
440 else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1)) in encode_mcu_DC_first()
441 entropy->dc_context[ci] += 8; /* large diff category */ in encode_mcu_DC_first()
461 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in encode_mcu_AC_first()
468 if (cinfo->restart_interval) { in encode_mcu_AC_first()
469 if (entropy->restarts_to_go == 0) { in encode_mcu_AC_first()
470 emit_restart(cinfo, entropy->next_restart_num); in encode_mcu_AC_first()
471 entropy->restarts_to_go = cinfo->restart_interval; in encode_mcu_AC_first()
472 entropy->next_restart_num++; in encode_mcu_AC_first()
473 entropy->next_restart_num &= 7; in encode_mcu_AC_first()
475 entropy->restarts_to_go--; in encode_mcu_AC_first()
480 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; in encode_mcu_AC_first()
484 /* Establish EOB (end-of-block) index */ in encode_mcu_AC_first()
485 for (ke = cinfo->Se; ke > 0; ke--) in encode_mcu_AC_first()
491 if (v >>= cinfo->Al) break; in encode_mcu_AC_first()
493 v = -v; in encode_mcu_AC_first()
494 if (v >>= cinfo->Al) break; in encode_mcu_AC_first()
498 for (k = cinfo->Ss; k <= ke; k++) { in encode_mcu_AC_first()
499 st = entropy->ac_stats[tbl] + 3 * (k - 1); in encode_mcu_AC_first()
503 if (v >>= cinfo->Al) { in encode_mcu_AC_first()
505 arith_encode(cinfo, entropy->fixed_bin, 0); in encode_mcu_AC_first()
509 v = -v; in encode_mcu_AC_first()
510 if (v >>= cinfo->Al) { in encode_mcu_AC_first()
512 arith_encode(cinfo, entropy->fixed_bin, 1); in encode_mcu_AC_first()
521 if (v -= 1) { in encode_mcu_AC_first()
528 st = entropy->ac_stats[tbl] + in encode_mcu_AC_first()
529 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); in encode_mcu_AC_first()
543 /* Encode EOB decision only if k <= cinfo->Se */ in encode_mcu_AC_first()
544 if (k <= cinfo->Se) { in encode_mcu_AC_first()
545 st = entropy->ac_stats[tbl] + 3 * (k - 1); in encode_mcu_AC_first()
560 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in encode_mcu_DC_refine()
565 if (cinfo->restart_interval) { in encode_mcu_DC_refine()
566 if (entropy->restarts_to_go == 0) { in encode_mcu_DC_refine()
567 emit_restart(cinfo, entropy->next_restart_num); in encode_mcu_DC_refine()
568 entropy->restarts_to_go = cinfo->restart_interval; in encode_mcu_DC_refine()
569 entropy->next_restart_num++; in encode_mcu_DC_refine()
570 entropy->next_restart_num &= 7; in encode_mcu_DC_refine()
572 entropy->restarts_to_go--; in encode_mcu_DC_refine()
575 st = entropy->fixed_bin; /* use fixed probability estimation */ in encode_mcu_DC_refine()
576 Al = cinfo->Al; in encode_mcu_DC_refine()
579 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { in encode_mcu_DC_refine()
595 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in encode_mcu_AC_refine()
602 if (cinfo->restart_interval) { in encode_mcu_AC_refine()
603 if (entropy->restarts_to_go == 0) { in encode_mcu_AC_refine()
604 emit_restart(cinfo, entropy->next_restart_num); in encode_mcu_AC_refine()
605 entropy->restarts_to_go = cinfo->restart_interval; in encode_mcu_AC_refine()
606 entropy->next_restart_num++; in encode_mcu_AC_refine()
607 entropy->next_restart_num &= 7; in encode_mcu_AC_refine()
609 entropy->restarts_to_go--; in encode_mcu_AC_refine()
614 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; in encode_mcu_AC_refine()
618 /* Establish EOB (end-of-block) index */ in encode_mcu_AC_refine()
619 for (ke = cinfo->Se; ke > 0; ke--) in encode_mcu_AC_refine()
625 if (v >>= cinfo->Al) break; in encode_mcu_AC_refine()
627 v = -v; in encode_mcu_AC_refine()
628 if (v >>= cinfo->Al) break; in encode_mcu_AC_refine()
631 /* Establish EOBx (previous stage end-of-block) index */ in encode_mcu_AC_refine()
632 for (kex = ke; kex > 0; kex--) in encode_mcu_AC_refine()
634 if (v >>= cinfo->Ah) break; in encode_mcu_AC_refine()
636 v = -v; in encode_mcu_AC_refine()
637 if (v >>= cinfo->Ah) break; in encode_mcu_AC_refine()
641 for (k = cinfo->Ss; k <= ke; k++) { in encode_mcu_AC_refine()
642 st = entropy->ac_stats[tbl] + 3 * (k - 1); in encode_mcu_AC_refine()
647 if (v >>= cinfo->Al) { in encode_mcu_AC_refine()
652 arith_encode(cinfo, entropy->fixed_bin, 0); in encode_mcu_AC_refine()
657 v = -v; in encode_mcu_AC_refine()
658 if (v >>= cinfo->Al) { in encode_mcu_AC_refine()
663 arith_encode(cinfo, entropy->fixed_bin, 1); in encode_mcu_AC_refine()
671 /* Encode EOB decision only if k <= cinfo->Se */ in encode_mcu_AC_refine()
672 if (k <= cinfo->Se) { in encode_mcu_AC_refine()
673 st = entropy->ac_stats[tbl] + 3 * (k - 1); in encode_mcu_AC_refine()
682 * Encode and output one MCU's worth of arithmetic-compressed coefficients.
688 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in encode_mcu()
696 if (cinfo->restart_interval) { in encode_mcu()
697 if (entropy->restarts_to_go == 0) { in encode_mcu()
698 emit_restart(cinfo, entropy->next_restart_num); in encode_mcu()
699 entropy->restarts_to_go = cinfo->restart_interval; in encode_mcu()
700 entropy->next_restart_num++; in encode_mcu()
701 entropy->next_restart_num &= 7; in encode_mcu()
703 entropy->restarts_to_go--; in encode_mcu()
707 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { in encode_mcu()
709 ci = cinfo->MCU_membership[blkn]; in encode_mcu()
710 compptr = cinfo->cur_comp_info[ci]; in encode_mcu()
714 tbl = compptr->dc_tbl_no; in encode_mcu()
717 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; in encode_mcu()
720 if ((v = (*block)[0] - entropy->last_dc_val[ci]) == 0) { in encode_mcu()
722 entropy->dc_context[ci] = 0; /* zero diff category */ in encode_mcu()
724 entropy->last_dc_val[ci] = (*block)[0]; in encode_mcu()
731 entropy->dc_context[ci] = 4; /* small positive diff category */ in encode_mcu()
733 v = -v; in encode_mcu()
736 entropy->dc_context[ci] = 8; /* small negative diff category */ in encode_mcu()
740 if (v -= 1) { in encode_mcu()
744 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ in encode_mcu()
753 if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1)) in encode_mcu()
754 entropy->dc_context[ci] = 0; /* zero diff category */ in encode_mcu()
755 else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1)) in encode_mcu()
756 entropy->dc_context[ci] += 8; /* large diff category */ in encode_mcu()
765 tbl = compptr->ac_tbl_no; in encode_mcu()
767 /* Establish EOB (end-of-block) index */ in encode_mcu()
768 for (ke = DCTSIZE2 - 1; ke > 0; ke--) in encode_mcu()
773 st = entropy->ac_stats[tbl] + 3 * (k - 1); in encode_mcu()
782 arith_encode(cinfo, entropy->fixed_bin, 0); in encode_mcu()
784 v = -v; in encode_mcu()
785 arith_encode(cinfo, entropy->fixed_bin, 1); in encode_mcu()
790 if (v -= 1) { in encode_mcu()
797 st = entropy->ac_stats[tbl] + in encode_mcu()
798 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); in encode_mcu()
812 /* Encode EOB decision only if k <= DCTSIZE2 - 1 */ in encode_mcu()
813 if (k <= DCTSIZE2 - 1) { in encode_mcu()
814 st = entropy->ac_stats[tbl] + 3 * (k - 1); in encode_mcu()
824 * Initialize for an arithmetic-compressed scan.
830 arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; in start_pass()
844 if (cinfo->progressive_mode) { in start_pass()
845 if (cinfo->Ah == 0) { in start_pass()
846 if (cinfo->Ss == 0) in start_pass()
847 entropy->pub.encode_mcu = encode_mcu_DC_first; in start_pass()
849 entropy->pub.encode_mcu = encode_mcu_AC_first; in start_pass()
851 if (cinfo->Ss == 0) in start_pass()
852 entropy->pub.encode_mcu = encode_mcu_DC_refine; in start_pass()
854 entropy->pub.encode_mcu = encode_mcu_AC_refine; in start_pass()
857 entropy->pub.encode_mcu = encode_mcu; in start_pass()
860 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { in start_pass()
861 compptr = cinfo->cur_comp_info[ci]; in start_pass()
863 if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { in start_pass()
864 tbl = compptr->dc_tbl_no; in start_pass()
867 if (entropy->dc_stats[tbl] == NULL) in start_pass()
868 entropy->dc_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small) in start_pass()
870 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); in start_pass()
872 entropy->last_dc_val[ci] = 0; in start_pass()
873 entropy->dc_context[ci] = 0; in start_pass()
876 if (cinfo->progressive_mode == 0 || cinfo->Se) { in start_pass()
877 tbl = compptr->ac_tbl_no; in start_pass()
880 if (entropy->ac_stats[tbl] == NULL) in start_pass()
881 entropy->ac_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small) in start_pass()
883 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); in start_pass()
885 if (cinfo->progressive_mode) in start_pass()
887 cinfo->arith_ac_K[tbl] = cinfo->Ss + in start_pass()
888 ((8 + cinfo->Se - cinfo->Ss) >> 4); in start_pass()
894 entropy->c = 0; in start_pass()
895 entropy->a = 0x10000L; in start_pass()
896 entropy->sc = 0; in start_pass()
897 entropy->zc = 0; in start_pass()
898 entropy->ct = 11; in start_pass()
899 entropy->buffer = -1; /* empty */ in start_pass()
902 entropy->restarts_to_go = cinfo->restart_interval; in start_pass()
903 entropy->next_restart_num = 0; in start_pass()
918 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, in jinit_arith_encoder()
920 cinfo->entropy = (struct jpeg_entropy_encoder *)entropy; in jinit_arith_encoder()
921 entropy->pub.start_pass = start_pass; in jinit_arith_encoder()
922 entropy->pub.finish_pass = finish_pass; in jinit_arith_encoder()
926 entropy->dc_stats[i] = NULL; in jinit_arith_encoder()
927 entropy->ac_stats[i] = NULL; in jinit_arith_encoder()
931 entropy->fixed_bin[0] = 113; in jinit_arith_encoder()