📄 jrevdct.c
字号:
/* * jrevdct.c * * This file is part of the Independent JPEG Group's software. * The IJG code is distributed under the terms reproduced here: * * LEGAL ISSUES * ============ * * In plain English: * * 1. We don't promise that this software works. (But if you find any bugs, * please let us know!) * 2. You can use this software for whatever you want. You don't have to * pay us. * 3. You may not pretend that you wrote this software. If you use it in a * program, you must acknowledge somewhere in your documentation that * you've used the IJG code. * * In legalese: * * The authors make NO WARRANTY or representation, either express or implied, * with respect to this software, its quality, accuracy, merchantability, or * fitness for a particular purpose. This software is provided "AS IS", and * you, its user, assume the entire risk as to its quality and accuracy. * * This software is copyright (C) 1991, 1992, Thomas G. Lane. * All Rights Reserved except as specified below. * * Permission is hereby granted to use, copy, modify, and distribute this * software (or portions thereof) for any purpose, without fee, subject to * these conditions: * (1) If any part of the source code for this software is distributed, then * this copyright and no-warranty notice must be included unaltered; and any * additions, deletions, or changes to the original files must be clearly * indicated in accompanying documentation. * (2) If only executable code is distributed, then the accompanying * documentation must state that "this software is based in part on the * work of the Independent JPEG Group". * (3) Permission for use of this software is granted only if the user * accepts full responsibility for any undesirable consequences; the authors * accept NO LIABILITY for damages of any kind. * * These conditions apply to any software derived from or based on the IJG * code, not just to the unmodified library. If you use our work, you ought * to acknowledge us. * * Permission is NOT granted for the use of any IJG author's name or company * name in advertising or publicity relating to this software or products * derived from it. This software may be referred to only as * "the Independent JPEG Group's software". * * We specifically permit and encourage the use of this software as the * basis of commercial products, provided that all warranty or liability * claims are assumed by the product vendor. * * * ARCHIVE LOCATIONS * ================= * * The "official" archive site for this software is ftp.uu.net (Internet * address 192.48.96.9). The most recent released version can always be * found there in directory graphics/jpeg. This particular version will * be archived as graphics/jpeg/jpegsrc.v6a.tar.gz. If you are on the * Internet, you can retrieve files from ftp.uu.net by standard anonymous * FTP. If you don't have FTP access, UUNET's archives are also available * via UUCP; contact help@uunet.uu.net for information on retrieving files * that way. * * Numerous Internet sites maintain copies of the UUNET files. However, * only ftp.uu.net is guaranteed to have the latest official version. * * You can also obtain this software in DOS-compatible "zip" archive * format from the SimTel archives (ftp.coast.net:/SimTel/msdos/graphics/), * or on CompuServe in the Graphics Support forum (GO CIS:GRAPHSUP), * library 12 "JPEG Tools". Again, these versions may sometimes lag behind * the ftp.uu.net release. * * The JPEG FAQ (Frequently Asked Questions) article is a useful source of * general information about JPEG. It is updated constantly and therefore * is not included in this distribution. The FAQ is posted every two weeks * to Usenet newsgroups comp.graphics.misc, news.answers, and other groups. * You can always obtain the latest version from the news.answers archive * at rtfm.mit.edu. By FTP, fetch /pub/usenet/news.answers/jpeg-faq/part1 * and .../part2. If you don't have FTP, send e-mail to * mail-server@rtfm.mit.edu with body * send usenet/news.answers/jpeg-faq/part1 * send usenet/news.answers/jpeg-faq/part2 * * ============== * * * This file contains the basic inverse-DCT transformation subroutine. * * This implementation is based on an algorithm described in * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics, * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991. * The primary algorithm described there uses 11 multiplies and 29 adds. * We use their alternate method with 12 multiplies and 32 adds. * The advantage of this method is that no data path contains more than one * multiplication; this allows a very simple and accurate implementation in * scaled fixed-point arithmetic, with a minimal number of shifts. * * * CHANGES FOR BERKELEY MPEG * ========================= * * This file has been altered to use the Berkeley MPEG header files, * to add the capability to handle sparse DCT matrices efficiently, * and to relabel the inverse DCT function as well as the file * (formerly jidctint.c). * * I've made lots of modifications to attempt to take advantage of the * sparse nature of the DCT matrices we're getting. Although the logic * is cumbersome, it's straightforward and the resulting code is much * faster. * * A better way to do this would be to pass in the DCT block as a sparse * matrix, perhaps with the difference cases encoded. */#include <string.h>#include "video.h"#include "proto.h"#define GLOBAL /* a function referenced thru EXTERNs */ /* We assume that right shift corresponds to signed division by 2 with * rounding towards minus infinity. This is correct for typical "arithmetic * shift" instructions that shift in copies of the sign bit. But some * C compilers implement >> with an unsigned shift. For these machines you * must define RIGHT_SHIFT_IS_UNSIGNED. * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity. * It is only applied with constant shift counts. SHIFT_TEMPS must be * included in the variables of any routine using RIGHT_SHIFT. */ #ifdef RIGHT_SHIFT_IS_UNSIGNED#define SHIFT_TEMPS INT32 shift_temp;#define RIGHT_SHIFT(x,shft) \ ((shift_temp = (x)) < 0 ? \ (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \ (shift_temp >> (shft)))#else#define SHIFT_TEMPS#define RIGHT_SHIFT(x,shft) ((x) >> (shft))#endif/* * This routine is specialized to the case DCTSIZE = 8. */#if DCTSIZE != 8 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */#endif/* * A 2-D IDCT can be done by 1-D IDCT on each row followed by 1-D IDCT * on each column. Direct algorithms are also available, but they are * much more complex and seem not to be any faster when reduced to code. * * The poop on this scaling stuff is as follows: * * Each 1-D IDCT step produces outputs which are a factor of sqrt(N) * larger than the true IDCT outputs. The final outputs are therefore * a factor of N larger than desired; since N=8 this can be cured by * a simple right shift at the end of the algorithm. The advantage of * this arrangement is that we save two multiplications per 1-D IDCT, * because the y0 and y4 inputs need not be divided by sqrt(N). * * We have to do addition and subtraction of the integer inputs, which * is no problem, and multiplication by fractional constants, which is * a problem to do in integer arithmetic. We multiply all the constants * by CONST_SCALE and convert them to integer constants (thus retaining * CONST_BITS bits of precision in the constants). After doing a * multiplication we have to divide the product by CONST_SCALE, with proper * rounding, to produce the correct output. This division can be done * cheaply as a right shift of CONST_BITS bits. We postpone shifting * as long as possible so that partial sums can be added together with * full fractional precision. * * The outputs of the first pass are scaled up by PASS1_BITS bits so that * they are represented to better-than-integral precision. These outputs * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word * with the recommended scaling. (To scale up 12-bit sample data further, an * intermediate INT32 array would be needed.) * * To avoid overflow of the 32-bit intermediate results in pass 2, we must * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis * shows that the values given below are the most effective. */#ifdef EIGHT_BIT_SAMPLES#define PASS1_BITS 2#else#define PASS1_BITS 1 /* lose a little precision to avoid overflow */#endif#define ONE ((INT32) 1)#define CONST_SCALE (ONE << CONST_BITS)/* Convert a positive real constant to an integer scaled by CONST_SCALE. * IMPORTANT: if your compiler doesn't do this arithmetic at compile time, * you will pay a significant penalty in run time. In that case, figure * the correct integer constant values and insert them by hand. */#define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5))/* When adding two opposite-signed fixes, the 0.5 cancels */#define FIX2(x) ((INT32) ((x) * CONST_SCALE))/* Descale and correctly round an INT32 value that's scaled by N bits. * We assume RIGHT_SHIFT rounds towards minus infinity, so adding * the fudge factor is correct for either sign of X. */#define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. * For 8-bit samples with the recommended scaling, all the variable * and constant values involved are no more than 16 bits wide, so a * 16x16->32 bit multiply can be used instead of a full 32x32 multiply; * this provides a useful speedup on many machines. * There is no way to specify a 16x16->32 multiply in portable C, but * some C compilers will do the right thing if you provide the correct * combination of casts. * NB: for 12-bit samples, a full 32-bit multiplication will be needed. */#ifdef EIGHT_BIT_SAMPLES#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */#define MULTIPLY(var,const) (((INT16) (var)) * ((INT16) (const)))#endif#ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */#define MULTIPLY(var,const) (((INT16) (var)) * ((INT32) (const)))#endif#endif#ifndef MULTIPLY /* default definition */#define MULTIPLY(var,const) ((var) * (const))#endif#ifndef NO_SPARSE_DCT#define SPARSE_SCALE_FACTOR 8 #endif/* Precomputed idct value arrays. */static DCTELEM PreIDCT[64][64];/* *-------------------------------------------------------------- * * init_pre_idct -- * * Pre-computes singleton coefficient IDCT values. * * Results: * None. * * Side effects: * None. * *-------------------------------------------------------------- */voidinit_pre_idct() { int i; for (i=0; i<64; i++) { memset((char *) PreIDCT[i], 0, 64*sizeof(DCTELEM)); PreIDCT[i][i] = 1 << SPARSE_SCALE_FACTOR; j_rev_dct(PreIDCT[i]); }}#ifndef NO_SPARSE_DCT /* *-------------------------------------------------------------- * * j_rev_dct_sparse -- * * Performs the inverse DCT on one block of coefficients. * * Results: * None. * * Side effects: * None. * *-------------------------------------------------------------- */voidj_rev_dct_sparse (data, pos) DCTBLOCK data; int pos;{ short int val; register int *dp; register int v; int quant;#ifdef SPARSE_AC register DCTELEM *dataptr; DCTELEM *ndataptr; int coeff, rr; DCTBLOCK tmpdata, tmp2data; DCTELEM *tmpdataptr, *tmp2dataptr; int printFlag = 1;#endif /* If DC Coefficient. */ if (pos == 0) { dp = (int *)data; v = *data; quant = 8; /* Compute 32 bit value to assign. This speeds things up a bit */ if (v < 0) { val = -v; val += (quant >> 1); val /= quant; val = -val; } else { val = (v + (quant >> 1)) / quant; } v = ((val & 0xffff) | (val << 16)); dp[0] = v; dp[1] = v; dp[2] = v; dp[3] = v; dp[4] = v; dp[5] = v; dp[6] = v; dp[7] = v; dp[8] = v; dp[9] = v; dp[10] = v; dp[11] = v; dp[12] = v; dp[13] = v; dp[14] = v; dp[15] = v; dp[16] = v; dp[17] = v; dp[18] = v; dp[19] = v; dp[20] = v; dp[21] = v; dp[22] = v; dp[23] = v; dp[24] = v; dp[25] = v; dp[26] = v; dp[27] = v; dp[28] = v; dp[29] = v; dp[30] = v; dp[31] = v; return; } /* Some other coefficient. */#ifdef SPARSE_AC dataptr = (DCTELEM *)data; coeff = dataptr[pos]; ndataptr = PreIDCT[pos]; printf ("\n"); printf ("COEFFICIENT = %3d, POSITION = %2d\n", coeff, pos); for (v=0; v<64; v++) { memcpy((char *) tmpdata, data, 64*sizeof(DCTELEM)); } tmpdataptr = (DCTELEM *)tmpdata; for (v=0; v<64; v++) { memcpy((char *) tmp2data, data, 64*sizeof(DCTELEM)); } tmp2dataptr = (DCTELEM *)tmp2data;#ifdef DEBUG printf ("original DCTBLOCK:\n"); for (rr=0; rr<8; rr++) { for (v=0; v<8; v++) { if (dataptr[8*rr+v] != tmpdataptr[8*rr+v]) fprintf(stderr, "Error in copy\n"); printf ("%3d ", dataptr[8*rr+v]); } printf("\n"); }#endif printf("\n"); for (rr=0; rr<4; rr++) { dataptr[0] = (ndataptr[0] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[1] = (ndataptr[1] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[2] = (ndataptr[2] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[3] = (ndataptr[3] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[4] = (ndataptr[4] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[5] = (ndataptr[5] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[6] = (ndataptr[6] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[7] = (ndataptr[7] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[8] = (ndataptr[8] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[9] = (ndataptr[9] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[10] = (ndataptr[10] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[11] = (ndataptr[11] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[12] = (ndataptr[12] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[13] = (ndataptr[13] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[14] = (ndataptr[14] * coeff) >> SPARSE_SCALE_FACTOR; dataptr[15] = (ndataptr[15] * coeff) >> SPARSE_SCALE_FACTOR; dataptr += 16; ndataptr += 16; } dataptr = (DCTELEM *) data;#ifdef DEBUG printf ("sparse IDCT:\n"); for (rr=0; rr<8; rr++) { for (v=0; v<8; v++) { printf ("%3d ", dataptr[8*rr+v]); } printf("\n"); } printf("\n");#endif /* DEBUG */#else /* NO_SPARSE_AC */#ifdef FLOATDCT if (qualityFlag) float_idct(data); else#endif /* FLOATDCT */ j_rev_dct(data);#endif /* SPARSE_AC */ return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -