media/libjpeg/jddctmgr.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libjpeg/jddctmgr.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,338 @@
     1.4 +/*
     1.5 + * jddctmgr.c
     1.6 + *
     1.7 + * This file was part of the Independent JPEG Group's software:
     1.8 + * Copyright (C) 1994-1996, Thomas G. Lane.
     1.9 + * Modified 2002-2010 by Guido Vollbeding.
    1.10 + * libjpeg-turbo Modifications:
    1.11 + * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
    1.12 + * Copyright (C) 2010, D. R. Commander.
    1.13 + * For conditions of distribution and use, see the accompanying README file.
    1.14 + *
    1.15 + * This file contains the inverse-DCT management logic.
    1.16 + * This code selects a particular IDCT implementation to be used,
    1.17 + * and it performs related housekeeping chores.  No code in this file
    1.18 + * is executed per IDCT step, only during output pass setup.
    1.19 + *
    1.20 + * Note that the IDCT routines are responsible for performing coefficient
    1.21 + * dequantization as well as the IDCT proper.  This module sets up the
    1.22 + * dequantization multiplier table needed by the IDCT routine.
    1.23 + */
    1.24 +
    1.25 +#define JPEG_INTERNALS
    1.26 +#include "jinclude.h"
    1.27 +#include "jpeglib.h"
    1.28 +#include "jdct.h"		/* Private declarations for DCT subsystem */
    1.29 +#include "jsimddct.h"
    1.30 +#include "jpegcomp.h"
    1.31 +
    1.32 +
    1.33 +/*
    1.34 + * The decompressor input side (jdinput.c) saves away the appropriate
    1.35 + * quantization table for each component at the start of the first scan
    1.36 + * involving that component.  (This is necessary in order to correctly
    1.37 + * decode files that reuse Q-table slots.)
    1.38 + * When we are ready to make an output pass, the saved Q-table is converted
    1.39 + * to a multiplier table that will actually be used by the IDCT routine.
    1.40 + * The multiplier table contents are IDCT-method-dependent.  To support
    1.41 + * application changes in IDCT method between scans, we can remake the
    1.42 + * multiplier tables if necessary.
    1.43 + * In buffered-image mode, the first output pass may occur before any data
    1.44 + * has been seen for some components, and thus before their Q-tables have
    1.45 + * been saved away.  To handle this case, multiplier tables are preset
    1.46 + * to zeroes; the result of the IDCT will be a neutral gray level.
    1.47 + */
    1.48 +
    1.49 +
    1.50 +/* Private subobject for this module */
    1.51 +
    1.52 +typedef struct {
    1.53 +  struct jpeg_inverse_dct pub;	/* public fields */
    1.54 +
    1.55 +  /* This array contains the IDCT method code that each multiplier table
    1.56 +   * is currently set up for, or -1 if it's not yet set up.
    1.57 +   * The actual multiplier tables are pointed to by dct_table in the
    1.58 +   * per-component comp_info structures.
    1.59 +   */
    1.60 +  int cur_method[MAX_COMPONENTS];
    1.61 +} my_idct_controller;
    1.62 +
    1.63 +typedef my_idct_controller * my_idct_ptr;
    1.64 +
    1.65 +
    1.66 +/* Allocated multiplier tables: big enough for any supported variant */
    1.67 +
    1.68 +typedef union {
    1.69 +  ISLOW_MULT_TYPE islow_array[DCTSIZE2];
    1.70 +#ifdef DCT_IFAST_SUPPORTED
    1.71 +  IFAST_MULT_TYPE ifast_array[DCTSIZE2];
    1.72 +#endif
    1.73 +#ifdef DCT_FLOAT_SUPPORTED
    1.74 +  FLOAT_MULT_TYPE float_array[DCTSIZE2];
    1.75 +#endif
    1.76 +} multiplier_table;
    1.77 +
    1.78 +
    1.79 +/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
    1.80 + * so be sure to compile that code if either ISLOW or SCALING is requested.
    1.81 + */
    1.82 +#ifdef DCT_ISLOW_SUPPORTED
    1.83 +#define PROVIDE_ISLOW_TABLES
    1.84 +#else
    1.85 +#ifdef IDCT_SCALING_SUPPORTED
    1.86 +#define PROVIDE_ISLOW_TABLES
    1.87 +#endif
    1.88 +#endif
    1.89 +
    1.90 +
    1.91 +/*
    1.92 + * Prepare for an output pass.
    1.93 + * Here we select the proper IDCT routine for each component and build
    1.94 + * a matching multiplier table.
    1.95 + */
    1.96 +
    1.97 +METHODDEF(void)
    1.98 +start_pass (j_decompress_ptr cinfo)
    1.99 +{
   1.100 +  my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
   1.101 +  int ci, i;
   1.102 +  jpeg_component_info *compptr;
   1.103 +  int method = 0;
   1.104 +  inverse_DCT_method_ptr method_ptr = NULL;
   1.105 +  JQUANT_TBL * qtbl;
   1.106 +
   1.107 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.108 +       ci++, compptr++) {
   1.109 +    /* Select the proper IDCT routine for this component's scaling */
   1.110 +    switch (compptr->_DCT_scaled_size) {
   1.111 +#ifdef IDCT_SCALING_SUPPORTED
   1.112 +    case 1:
   1.113 +      method_ptr = jpeg_idct_1x1;
   1.114 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
   1.115 +      break;
   1.116 +    case 2:
   1.117 +      if (jsimd_can_idct_2x2())
   1.118 +        method_ptr = jsimd_idct_2x2;
   1.119 +      else
   1.120 +        method_ptr = jpeg_idct_2x2;
   1.121 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
   1.122 +      break;
   1.123 +    case 3:
   1.124 +      method_ptr = jpeg_idct_3x3;
   1.125 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.126 +      break;
   1.127 +    case 4:
   1.128 +      if (jsimd_can_idct_4x4())
   1.129 +        method_ptr = jsimd_idct_4x4;
   1.130 +      else
   1.131 +        method_ptr = jpeg_idct_4x4;
   1.132 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
   1.133 +      break;
   1.134 +    case 5:
   1.135 +      method_ptr = jpeg_idct_5x5;
   1.136 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.137 +      break;
   1.138 +    case 6:
   1.139 +      method_ptr = jpeg_idct_6x6;
   1.140 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.141 +      break;
   1.142 +    case 7:
   1.143 +      method_ptr = jpeg_idct_7x7;
   1.144 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.145 +      break;
   1.146 +#endif
   1.147 +    case DCTSIZE:
   1.148 +      switch (cinfo->dct_method) {
   1.149 +#ifdef DCT_ISLOW_SUPPORTED
   1.150 +      case JDCT_ISLOW:
   1.151 +	if (jsimd_can_idct_islow())
   1.152 +	  method_ptr = jsimd_idct_islow;
   1.153 +	else
   1.154 +	  method_ptr = jpeg_idct_islow;
   1.155 +	method = JDCT_ISLOW;
   1.156 +	break;
   1.157 +#endif
   1.158 +#ifdef DCT_IFAST_SUPPORTED
   1.159 +      case JDCT_IFAST:
   1.160 +	if (jsimd_can_idct_ifast())
   1.161 +	  method_ptr = jsimd_idct_ifast;
   1.162 +	else
   1.163 +	  method_ptr = jpeg_idct_ifast;
   1.164 +	method = JDCT_IFAST;
   1.165 +	break;
   1.166 +#endif
   1.167 +#ifdef DCT_FLOAT_SUPPORTED
   1.168 +      case JDCT_FLOAT:
   1.169 +	if (jsimd_can_idct_float())
   1.170 +	  method_ptr = jsimd_idct_float;
   1.171 +	else
   1.172 +	  method_ptr = jpeg_idct_float;
   1.173 +	method = JDCT_FLOAT;
   1.174 +	break;
   1.175 +#endif
   1.176 +      default:
   1.177 +	ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.178 +	break;
   1.179 +      }
   1.180 +      break;
   1.181 +    case 9:
   1.182 +      method_ptr = jpeg_idct_9x9;
   1.183 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.184 +      break;
   1.185 +    case 10:
   1.186 +      method_ptr = jpeg_idct_10x10;
   1.187 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.188 +      break;
   1.189 +    case 11:
   1.190 +      method_ptr = jpeg_idct_11x11;
   1.191 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.192 +      break;
   1.193 +    case 12:
   1.194 +      method_ptr = jpeg_idct_12x12;
   1.195 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.196 +      break;
   1.197 +    case 13:
   1.198 +      method_ptr = jpeg_idct_13x13;
   1.199 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.200 +      break;
   1.201 +    case 14:
   1.202 +      method_ptr = jpeg_idct_14x14;
   1.203 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.204 +      break;
   1.205 +    case 15:
   1.206 +      method_ptr = jpeg_idct_15x15;
   1.207 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.208 +      break;
   1.209 +    case 16:
   1.210 +      method_ptr = jpeg_idct_16x16;
   1.211 +      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
   1.212 +      break;
   1.213 +    default:
   1.214 +      ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size);
   1.215 +      break;
   1.216 +    }
   1.217 +    idct->pub.inverse_DCT[ci] = method_ptr;
   1.218 +    /* Create multiplier table from quant table.
   1.219 +     * However, we can skip this if the component is uninteresting
   1.220 +     * or if we already built the table.  Also, if no quant table
   1.221 +     * has yet been saved for the component, we leave the
   1.222 +     * multiplier table all-zero; we'll be reading zeroes from the
   1.223 +     * coefficient controller's buffer anyway.
   1.224 +     */
   1.225 +    if (! compptr->component_needed || idct->cur_method[ci] == method)
   1.226 +      continue;
   1.227 +    qtbl = compptr->quant_table;
   1.228 +    if (qtbl == NULL)		/* happens if no data yet for component */
   1.229 +      continue;
   1.230 +    idct->cur_method[ci] = method;
   1.231 +    switch (method) {
   1.232 +#ifdef PROVIDE_ISLOW_TABLES
   1.233 +    case JDCT_ISLOW:
   1.234 +      {
   1.235 +	/* For LL&M IDCT method, multipliers are equal to raw quantization
   1.236 +	 * coefficients, but are stored as ints to ensure access efficiency.
   1.237 +	 */
   1.238 +	ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
   1.239 +	for (i = 0; i < DCTSIZE2; i++) {
   1.240 +	  ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
   1.241 +	}
   1.242 +      }
   1.243 +      break;
   1.244 +#endif
   1.245 +#ifdef DCT_IFAST_SUPPORTED
   1.246 +    case JDCT_IFAST:
   1.247 +      {
   1.248 +	/* For AA&N IDCT method, multipliers are equal to quantization
   1.249 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
   1.250 +	 *   scalefactor[0] = 1
   1.251 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
   1.252 +	 * For integer operation, the multiplier table is to be scaled by
   1.253 +	 * IFAST_SCALE_BITS.
   1.254 +	 */
   1.255 +	IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
   1.256 +#define CONST_BITS 14
   1.257 +	static const INT16 aanscales[DCTSIZE2] = {
   1.258 +	  /* precomputed values scaled up by 14 bits */
   1.259 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
   1.260 +	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
   1.261 +	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
   1.262 +	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
   1.263 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
   1.264 +	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
   1.265 +	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
   1.266 +	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
   1.267 +	};
   1.268 +	SHIFT_TEMPS
   1.269 +
   1.270 +	for (i = 0; i < DCTSIZE2; i++) {
   1.271 +	  ifmtbl[i] = (IFAST_MULT_TYPE)
   1.272 +	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
   1.273 +				  (INT32) aanscales[i]),
   1.274 +		    CONST_BITS-IFAST_SCALE_BITS);
   1.275 +	}
   1.276 +      }
   1.277 +      break;
   1.278 +#endif
   1.279 +#ifdef DCT_FLOAT_SUPPORTED
   1.280 +    case JDCT_FLOAT:
   1.281 +      {
   1.282 +	/* For float AA&N IDCT method, multipliers are equal to quantization
   1.283 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
   1.284 +	 *   scalefactor[0] = 1
   1.285 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
   1.286 +	 */
   1.287 +	FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
   1.288 +	int row, col;
   1.289 +	static const double aanscalefactor[DCTSIZE] = {
   1.290 +	  1.0, 1.387039845, 1.306562965, 1.175875602,
   1.291 +	  1.0, 0.785694958, 0.541196100, 0.275899379
   1.292 +	};
   1.293 +
   1.294 +	i = 0;
   1.295 +	for (row = 0; row < DCTSIZE; row++) {
   1.296 +	  for (col = 0; col < DCTSIZE; col++) {
   1.297 +	    fmtbl[i] = (FLOAT_MULT_TYPE)
   1.298 +	      ((double) qtbl->quantval[i] *
   1.299 +	       aanscalefactor[row] * aanscalefactor[col]);
   1.300 +	    i++;
   1.301 +	  }
   1.302 +	}
   1.303 +      }
   1.304 +      break;
   1.305 +#endif
   1.306 +    default:
   1.307 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.308 +      break;
   1.309 +    }
   1.310 +  }
   1.311 +}
   1.312 +
   1.313 +
   1.314 +/*
   1.315 + * Initialize IDCT manager.
   1.316 + */
   1.317 +
   1.318 +GLOBAL(void)
   1.319 +jinit_inverse_dct (j_decompress_ptr cinfo)
   1.320 +{
   1.321 +  my_idct_ptr idct;
   1.322 +  int ci;
   1.323 +  jpeg_component_info *compptr;
   1.324 +
   1.325 +  idct = (my_idct_ptr)
   1.326 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.327 +				SIZEOF(my_idct_controller));
   1.328 +  cinfo->idct = (struct jpeg_inverse_dct *) idct;
   1.329 +  idct->pub.start_pass = start_pass;
   1.330 +
   1.331 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.332 +       ci++, compptr++) {
   1.333 +    /* Allocate and pre-zero a multiplier table for each component */
   1.334 +    compptr->dct_table =
   1.335 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.336 +				  SIZEOF(multiplier_table));
   1.337 +    MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
   1.338 +    /* Mark multiplier table not yet set up for any method */
   1.339 +    idct->cur_method[ci] = -1;
   1.340 +  }
   1.341 +}

mercurial