18
18
#define JPEG_INTERNALS
19
19
#include "jinclude.h"
20
20
#include "jpeglib.h"
21
-
21
+ #include <math.h>
22
22
23
23
/* Expanded entropy encoder object for arithmetic encoding. */
24
24
@@ -120,6 +120,10 @@ emit_byte (int val, j_compress_ptr cinfo)
120
120
{
121
121
struct jpeg_destination_mgr * dest = cinfo -> dest ;
122
122
123
+ /* Do not emit bytes during trellis passes */
124
+ if (cinfo -> master -> trellis_passes )
125
+ return ;
126
+
123
127
* dest -> next_output_byte ++ = (JOCTET ) val ;
124
128
if (-- dest -> free_in_buffer == 0 )
125
129
if (! (* dest -> empty_output_buffer ) (cinfo ))
@@ -826,6 +830,7 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
826
830
arith_entropy_ptr entropy = (arith_entropy_ptr ) cinfo -> entropy ;
827
831
int ci , tbl ;
828
832
jpeg_component_info * compptr ;
833
+ boolean progressive_mode ;
829
834
830
835
if (gather_statistics )
831
836
/* Make sure to avoid that in the master control logic!
@@ -836,8 +841,12 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
836
841
837
842
/* We assume jcmaster.c already validated the progressive scan parameters. */
838
843
844
+ /* Trellis optimization does DC and AC in same pass and without refinement
845
+ * so consider progressive mode to be off in such case */
846
+ progressive_mode = (cinfo -> master -> trellis_passes ) ? FALSE : cinfo -> progressive_mode ;
847
+
839
848
/* Select execution routines */
840
- if (cinfo -> progressive_mode ) {
849
+ if (progressive_mode ) {
841
850
if (cinfo -> Ah == 0 ) {
842
851
if (cinfo -> Ss == 0 )
843
852
entropy -> pub .encode_mcu = encode_mcu_DC_first ;
@@ -856,7 +865,7 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
856
865
for (ci = 0 ; ci < cinfo -> comps_in_scan ; ci ++ ) {
857
866
compptr = cinfo -> cur_comp_info [ci ];
858
867
/* DC needs no table for refinement scan */
859
- if (cinfo -> progressive_mode == 0 || (cinfo -> Ss == 0 && cinfo -> Ah == 0 )) {
868
+ if (progressive_mode == 0 || (cinfo -> Ss == 0 && cinfo -> Ah == 0 )) {
860
869
tbl = compptr -> dc_tbl_no ;
861
870
if (tbl < 0 || tbl >= NUM_ARITH_TBLS )
862
871
ERREXIT1 (cinfo , JERR_NO_ARITH_TABLE , tbl );
@@ -869,7 +878,7 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
869
878
entropy -> dc_context [ci ] = 0 ;
870
879
}
871
880
/* AC needs no table when not present */
872
- if (cinfo -> progressive_mode == 0 || cinfo -> Se ) {
881
+ if (progressive_mode == 0 || cinfo -> Se ) {
873
882
tbl = compptr -> ac_tbl_no ;
874
883
if (tbl < 0 || tbl >= NUM_ARITH_TBLS )
875
884
ERREXIT1 (cinfo , JERR_NO_ARITH_TABLE , tbl );
@@ -878,7 +887,7 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
878
887
((j_common_ptr ) cinfo , JPOOL_IMAGE , AC_STAT_BINS );
879
888
MEMZERO (entropy -> ac_stats [tbl ], AC_STAT_BINS );
880
889
#ifdef CALCULATE_SPECTRAL_CONDITIONING
881
- if (cinfo -> progressive_mode )
890
+ if (progressive_mode )
882
891
/* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */
883
892
cinfo -> arith_ac_K [tbl ] = cinfo -> Ss + ((8 + cinfo -> Se - cinfo -> Ss ) >> 4 );
884
893
#endif
@@ -925,3 +934,29 @@ jinit_arith_encoder (j_compress_ptr cinfo)
925
934
/* Initialize index for fixed probability estimation */
926
935
entropy -> fixed_bin [0 ] = 113 ;
927
936
}
937
+
938
+ GLOBAL (void )
939
+ jget_arith_rates (j_compress_ptr cinfo , int dc_tbl_no , int ac_tbl_no , arith_rates * r )
940
+ {
941
+ int i ;
942
+ arith_entropy_ptr entropy = (arith_entropy_ptr ) cinfo -> entropy ;
943
+ for (i = 0 ; i < DC_STAT_BINS ; i ++ ) {
944
+ int state = entropy -> dc_stats [dc_tbl_no ][i ];
945
+ int mps_val = state >> 7 ;
946
+ float prob_lps = (jpeg_aritab [state & 0x7f ] >> 16 ) / 46340.95 ; /* 32768*sqrt(2) */
947
+ float prob_0 = (mps_val ) ? prob_lps : 1.0 - prob_lps ;
948
+ float prob_1 = 1.0 - prob_0 ;
949
+ r -> rate_dc [i ][0 ] = - log (prob_0 ) / log (2.0 );
950
+ r -> rate_dc [i ][1 ] = - log (prob_1 ) / log (2.0 );
951
+ }
952
+
953
+ for (i = 0 ; i < AC_STAT_BINS ; i ++ ) {
954
+ int state = entropy -> ac_stats [ac_tbl_no ][i ];
955
+ int mps_val = state >> 7 ;
956
+ float prob_lps = (jpeg_aritab [state & 0x7f ] >> 16 ) / 46340.95 ;
957
+ float prob_0 = (mps_val ) ? prob_lps : 1.0 - prob_lps ;
958
+ float prob_1 = 1.0 - prob_0 ;
959
+ r -> rate_ac [i ][0 ] = - log (prob_0 ) / log (2.0 );
960
+ r -> rate_ac [i ][1 ] = - log (prob_1 ) / log (2.0 );
961
+ }
962
+ }
0 commit comments