media/libjpeg/jddctmgr.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * jddctmgr.c
michael@0 3 *
michael@0 4 * This file was part of the Independent JPEG Group's software:
michael@0 5 * Copyright (C) 1994-1996, Thomas G. Lane.
michael@0 6 * Modified 2002-2010 by Guido Vollbeding.
michael@0 7 * libjpeg-turbo Modifications:
michael@0 8 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
michael@0 9 * Copyright (C) 2010, D. R. Commander.
michael@0 10 * For conditions of distribution and use, see the accompanying README file.
michael@0 11 *
michael@0 12 * This file contains the inverse-DCT management logic.
michael@0 13 * This code selects a particular IDCT implementation to be used,
michael@0 14 * and it performs related housekeeping chores. No code in this file
michael@0 15 * is executed per IDCT step, only during output pass setup.
michael@0 16 *
michael@0 17 * Note that the IDCT routines are responsible for performing coefficient
michael@0 18 * dequantization as well as the IDCT proper. This module sets up the
michael@0 19 * dequantization multiplier table needed by the IDCT routine.
michael@0 20 */
michael@0 21
michael@0 22 #define JPEG_INTERNALS
michael@0 23 #include "jinclude.h"
michael@0 24 #include "jpeglib.h"
michael@0 25 #include "jdct.h" /* Private declarations for DCT subsystem */
michael@0 26 #include "jsimddct.h"
michael@0 27 #include "jpegcomp.h"
michael@0 28
michael@0 29
michael@0 30 /*
michael@0 31 * The decompressor input side (jdinput.c) saves away the appropriate
michael@0 32 * quantization table for each component at the start of the first scan
michael@0 33 * involving that component. (This is necessary in order to correctly
michael@0 34 * decode files that reuse Q-table slots.)
michael@0 35 * When we are ready to make an output pass, the saved Q-table is converted
michael@0 36 * to a multiplier table that will actually be used by the IDCT routine.
michael@0 37 * The multiplier table contents are IDCT-method-dependent. To support
michael@0 38 * application changes in IDCT method between scans, we can remake the
michael@0 39 * multiplier tables if necessary.
michael@0 40 * In buffered-image mode, the first output pass may occur before any data
michael@0 41 * has been seen for some components, and thus before their Q-tables have
michael@0 42 * been saved away. To handle this case, multiplier tables are preset
michael@0 43 * to zeroes; the result of the IDCT will be a neutral gray level.
michael@0 44 */
michael@0 45
michael@0 46
michael@0 47 /* Private subobject for this module */
michael@0 48
michael@0 49 typedef struct {
michael@0 50 struct jpeg_inverse_dct pub; /* public fields */
michael@0 51
michael@0 52 /* This array contains the IDCT method code that each multiplier table
michael@0 53 * is currently set up for, or -1 if it's not yet set up.
michael@0 54 * The actual multiplier tables are pointed to by dct_table in the
michael@0 55 * per-component comp_info structures.
michael@0 56 */
michael@0 57 int cur_method[MAX_COMPONENTS];
michael@0 58 } my_idct_controller;
michael@0 59
michael@0 60 typedef my_idct_controller * my_idct_ptr;
michael@0 61
michael@0 62
michael@0 63 /* Allocated multiplier tables: big enough for any supported variant */
michael@0 64
michael@0 65 typedef union {
michael@0 66 ISLOW_MULT_TYPE islow_array[DCTSIZE2];
michael@0 67 #ifdef DCT_IFAST_SUPPORTED
michael@0 68 IFAST_MULT_TYPE ifast_array[DCTSIZE2];
michael@0 69 #endif
michael@0 70 #ifdef DCT_FLOAT_SUPPORTED
michael@0 71 FLOAT_MULT_TYPE float_array[DCTSIZE2];
michael@0 72 #endif
michael@0 73 } multiplier_table;
michael@0 74
michael@0 75
michael@0 76 /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
michael@0 77 * so be sure to compile that code if either ISLOW or SCALING is requested.
michael@0 78 */
michael@0 79 #ifdef DCT_ISLOW_SUPPORTED
michael@0 80 #define PROVIDE_ISLOW_TABLES
michael@0 81 #else
michael@0 82 #ifdef IDCT_SCALING_SUPPORTED
michael@0 83 #define PROVIDE_ISLOW_TABLES
michael@0 84 #endif
michael@0 85 #endif
michael@0 86
michael@0 87
michael@0 88 /*
michael@0 89 * Prepare for an output pass.
michael@0 90 * Here we select the proper IDCT routine for each component and build
michael@0 91 * a matching multiplier table.
michael@0 92 */
michael@0 93
michael@0 94 METHODDEF(void)
michael@0 95 start_pass (j_decompress_ptr cinfo)
michael@0 96 {
michael@0 97 my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
michael@0 98 int ci, i;
michael@0 99 jpeg_component_info *compptr;
michael@0 100 int method = 0;
michael@0 101 inverse_DCT_method_ptr method_ptr = NULL;
michael@0 102 JQUANT_TBL * qtbl;
michael@0 103
michael@0 104 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
michael@0 105 ci++, compptr++) {
michael@0 106 /* Select the proper IDCT routine for this component's scaling */
michael@0 107 switch (compptr->_DCT_scaled_size) {
michael@0 108 #ifdef IDCT_SCALING_SUPPORTED
michael@0 109 case 1:
michael@0 110 method_ptr = jpeg_idct_1x1;
michael@0 111 method = JDCT_ISLOW; /* jidctred uses islow-style table */
michael@0 112 break;
michael@0 113 case 2:
michael@0 114 if (jsimd_can_idct_2x2())
michael@0 115 method_ptr = jsimd_idct_2x2;
michael@0 116 else
michael@0 117 method_ptr = jpeg_idct_2x2;
michael@0 118 method = JDCT_ISLOW; /* jidctred uses islow-style table */
michael@0 119 break;
michael@0 120 case 3:
michael@0 121 method_ptr = jpeg_idct_3x3;
michael@0 122 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 123 break;
michael@0 124 case 4:
michael@0 125 if (jsimd_can_idct_4x4())
michael@0 126 method_ptr = jsimd_idct_4x4;
michael@0 127 else
michael@0 128 method_ptr = jpeg_idct_4x4;
michael@0 129 method = JDCT_ISLOW; /* jidctred uses islow-style table */
michael@0 130 break;
michael@0 131 case 5:
michael@0 132 method_ptr = jpeg_idct_5x5;
michael@0 133 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 134 break;
michael@0 135 case 6:
michael@0 136 method_ptr = jpeg_idct_6x6;
michael@0 137 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 138 break;
michael@0 139 case 7:
michael@0 140 method_ptr = jpeg_idct_7x7;
michael@0 141 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 142 break;
michael@0 143 #endif
michael@0 144 case DCTSIZE:
michael@0 145 switch (cinfo->dct_method) {
michael@0 146 #ifdef DCT_ISLOW_SUPPORTED
michael@0 147 case JDCT_ISLOW:
michael@0 148 if (jsimd_can_idct_islow())
michael@0 149 method_ptr = jsimd_idct_islow;
michael@0 150 else
michael@0 151 method_ptr = jpeg_idct_islow;
michael@0 152 method = JDCT_ISLOW;
michael@0 153 break;
michael@0 154 #endif
michael@0 155 #ifdef DCT_IFAST_SUPPORTED
michael@0 156 case JDCT_IFAST:
michael@0 157 if (jsimd_can_idct_ifast())
michael@0 158 method_ptr = jsimd_idct_ifast;
michael@0 159 else
michael@0 160 method_ptr = jpeg_idct_ifast;
michael@0 161 method = JDCT_IFAST;
michael@0 162 break;
michael@0 163 #endif
michael@0 164 #ifdef DCT_FLOAT_SUPPORTED
michael@0 165 case JDCT_FLOAT:
michael@0 166 if (jsimd_can_idct_float())
michael@0 167 method_ptr = jsimd_idct_float;
michael@0 168 else
michael@0 169 method_ptr = jpeg_idct_float;
michael@0 170 method = JDCT_FLOAT;
michael@0 171 break;
michael@0 172 #endif
michael@0 173 default:
michael@0 174 ERREXIT(cinfo, JERR_NOT_COMPILED);
michael@0 175 break;
michael@0 176 }
michael@0 177 break;
michael@0 178 case 9:
michael@0 179 method_ptr = jpeg_idct_9x9;
michael@0 180 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 181 break;
michael@0 182 case 10:
michael@0 183 method_ptr = jpeg_idct_10x10;
michael@0 184 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 185 break;
michael@0 186 case 11:
michael@0 187 method_ptr = jpeg_idct_11x11;
michael@0 188 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 189 break;
michael@0 190 case 12:
michael@0 191 method_ptr = jpeg_idct_12x12;
michael@0 192 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 193 break;
michael@0 194 case 13:
michael@0 195 method_ptr = jpeg_idct_13x13;
michael@0 196 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 197 break;
michael@0 198 case 14:
michael@0 199 method_ptr = jpeg_idct_14x14;
michael@0 200 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 201 break;
michael@0 202 case 15:
michael@0 203 method_ptr = jpeg_idct_15x15;
michael@0 204 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 205 break;
michael@0 206 case 16:
michael@0 207 method_ptr = jpeg_idct_16x16;
michael@0 208 method = JDCT_ISLOW; /* jidctint uses islow-style table */
michael@0 209 break;
michael@0 210 default:
michael@0 211 ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size);
michael@0 212 break;
michael@0 213 }
michael@0 214 idct->pub.inverse_DCT[ci] = method_ptr;
michael@0 215 /* Create multiplier table from quant table.
michael@0 216 * However, we can skip this if the component is uninteresting
michael@0 217 * or if we already built the table. Also, if no quant table
michael@0 218 * has yet been saved for the component, we leave the
michael@0 219 * multiplier table all-zero; we'll be reading zeroes from the
michael@0 220 * coefficient controller's buffer anyway.
michael@0 221 */
michael@0 222 if (! compptr->component_needed || idct->cur_method[ci] == method)
michael@0 223 continue;
michael@0 224 qtbl = compptr->quant_table;
michael@0 225 if (qtbl == NULL) /* happens if no data yet for component */
michael@0 226 continue;
michael@0 227 idct->cur_method[ci] = method;
michael@0 228 switch (method) {
michael@0 229 #ifdef PROVIDE_ISLOW_TABLES
michael@0 230 case JDCT_ISLOW:
michael@0 231 {
michael@0 232 /* For LL&M IDCT method, multipliers are equal to raw quantization
michael@0 233 * coefficients, but are stored as ints to ensure access efficiency.
michael@0 234 */
michael@0 235 ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
michael@0 236 for (i = 0; i < DCTSIZE2; i++) {
michael@0 237 ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
michael@0 238 }
michael@0 239 }
michael@0 240 break;
michael@0 241 #endif
michael@0 242 #ifdef DCT_IFAST_SUPPORTED
michael@0 243 case JDCT_IFAST:
michael@0 244 {
michael@0 245 /* For AA&N IDCT method, multipliers are equal to quantization
michael@0 246 * coefficients scaled by scalefactor[row]*scalefactor[col], where
michael@0 247 * scalefactor[0] = 1
michael@0 248 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
michael@0 249 * For integer operation, the multiplier table is to be scaled by
michael@0 250 * IFAST_SCALE_BITS.
michael@0 251 */
michael@0 252 IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
michael@0 253 #define CONST_BITS 14
michael@0 254 static const INT16 aanscales[DCTSIZE2] = {
michael@0 255 /* precomputed values scaled up by 14 bits */
michael@0 256 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
michael@0 257 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
michael@0 258 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
michael@0 259 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
michael@0 260 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
michael@0 261 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
michael@0 262 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
michael@0 263 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
michael@0 264 };
michael@0 265 SHIFT_TEMPS
michael@0 266
michael@0 267 for (i = 0; i < DCTSIZE2; i++) {
michael@0 268 ifmtbl[i] = (IFAST_MULT_TYPE)
michael@0 269 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
michael@0 270 (INT32) aanscales[i]),
michael@0 271 CONST_BITS-IFAST_SCALE_BITS);
michael@0 272 }
michael@0 273 }
michael@0 274 break;
michael@0 275 #endif
michael@0 276 #ifdef DCT_FLOAT_SUPPORTED
michael@0 277 case JDCT_FLOAT:
michael@0 278 {
michael@0 279 /* For float AA&N IDCT method, multipliers are equal to quantization
michael@0 280 * coefficients scaled by scalefactor[row]*scalefactor[col], where
michael@0 281 * scalefactor[0] = 1
michael@0 282 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
michael@0 283 */
michael@0 284 FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
michael@0 285 int row, col;
michael@0 286 static const double aanscalefactor[DCTSIZE] = {
michael@0 287 1.0, 1.387039845, 1.306562965, 1.175875602,
michael@0 288 1.0, 0.785694958, 0.541196100, 0.275899379
michael@0 289 };
michael@0 290
michael@0 291 i = 0;
michael@0 292 for (row = 0; row < DCTSIZE; row++) {
michael@0 293 for (col = 0; col < DCTSIZE; col++) {
michael@0 294 fmtbl[i] = (FLOAT_MULT_TYPE)
michael@0 295 ((double) qtbl->quantval[i] *
michael@0 296 aanscalefactor[row] * aanscalefactor[col]);
michael@0 297 i++;
michael@0 298 }
michael@0 299 }
michael@0 300 }
michael@0 301 break;
michael@0 302 #endif
michael@0 303 default:
michael@0 304 ERREXIT(cinfo, JERR_NOT_COMPILED);
michael@0 305 break;
michael@0 306 }
michael@0 307 }
michael@0 308 }
michael@0 309
michael@0 310
michael@0 311 /*
michael@0 312 * Initialize IDCT manager.
michael@0 313 */
michael@0 314
michael@0 315 GLOBAL(void)
michael@0 316 jinit_inverse_dct (j_decompress_ptr cinfo)
michael@0 317 {
michael@0 318 my_idct_ptr idct;
michael@0 319 int ci;
michael@0 320 jpeg_component_info *compptr;
michael@0 321
michael@0 322 idct = (my_idct_ptr)
michael@0 323 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 324 SIZEOF(my_idct_controller));
michael@0 325 cinfo->idct = (struct jpeg_inverse_dct *) idct;
michael@0 326 idct->pub.start_pass = start_pass;
michael@0 327
michael@0 328 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
michael@0 329 ci++, compptr++) {
michael@0 330 /* Allocate and pre-zero a multiplier table for each component */
michael@0 331 compptr->dct_table =
michael@0 332 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 333 SIZEOF(multiplier_table));
michael@0 334 MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
michael@0 335 /* Mark multiplier table not yet set up for any method */
michael@0 336 idct->cur_method[ci] = -1;
michael@0 337 }
michael@0 338 }

mercurial