/********************************************************************1* *2* THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *3* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *4* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *5* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *6* *7* THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *8* by the Xiph.Org Foundation and contributors *9* https://www.xiph.org/ *10* *11********************************************************************1213function:1415********************************************************************/1617#include <string.h>18#include "internal.h"19#include "dct.h"2021/*Performs an inverse 8 point Type-II DCT transform.22The output is scaled by a factor of 2 relative to the orthonormal version of23the transform.24_y: The buffer to store the result in.25Data will be placed in every 8th entry (e.g., in a column of an 8x826block).27_x: The input coefficients.28The first 8 entries are used (e.g., from a row of an 8x8 block).*/29static void idct8(ogg_int16_t *_y,const ogg_int16_t _x[8]){30ogg_int32_t t[8];31ogg_int32_t r;32/*Stage 1:*/33/*0-1 butterfly.*/34t[0]=OC_C4S4*(ogg_int16_t)(_x[0]+_x[4])>>16;35t[1]=OC_C4S4*(ogg_int16_t)(_x[0]-_x[4])>>16;36/*2-3 rotation by 6pi/16.*/37t[2]=(OC_C6S2*_x[2]>>16)-(OC_C2S6*_x[6]>>16);38t[3]=(OC_C2S6*_x[2]>>16)+(OC_C6S2*_x[6]>>16);39/*4-7 rotation by 7pi/16.*/40t[4]=(OC_C7S1*_x[1]>>16)-(OC_C1S7*_x[7]>>16);41/*5-6 rotation by 3pi/16.*/42t[5]=(OC_C3S5*_x[5]>>16)-(OC_C5S3*_x[3]>>16);43t[6]=(OC_C5S3*_x[5]>>16)+(OC_C3S5*_x[3]>>16);44t[7]=(OC_C1S7*_x[1]>>16)+(OC_C7S1*_x[7]>>16);45/*Stage 2:*/46/*4-5 butterfly.*/47r=t[4]+t[5];48t[5]=OC_C4S4*(ogg_int16_t)(t[4]-t[5])>>16;49t[4]=r;50/*7-6 butterfly.*/51r=t[7]+t[6];52t[6]=OC_C4S4*(ogg_int16_t)(t[7]-t[6])>>16;53t[7]=r;54/*Stage 3:*/55/*0-3 butterfly.*/56r=t[0]+t[3];57t[3]=t[0]-t[3];58t[0]=r;59/*1-2 butterfly.*/60r=t[1]+t[2];61t[2]=t[1]-t[2];62t[1]=r;63/*6-5 butterfly.*/64r=t[6]+t[5];65t[5]=t[6]-t[5];66t[6]=r;67/*Stage 4:*/68/*0-7 butterfly.*/69_y[0<<3]=(ogg_int16_t)(t[0]+t[7]);70/*1-6 butterfly.*/71_y[1<<3]=(ogg_int16_t)(t[1]+t[6]);72/*2-5 butterfly.*/73_y[2<<3]=(ogg_int16_t)(t[2]+t[5]);74/*3-4 butterfly.*/75_y[3<<3]=(ogg_int16_t)(t[3]+t[4]);76_y[4<<3]=(ogg_int16_t)(t[3]-t[4]);77_y[5<<3]=(ogg_int16_t)(t[2]-t[5]);78_y[6<<3]=(ogg_int16_t)(t[1]-t[6]);79_y[7<<3]=(ogg_int16_t)(t[0]-t[7]);80}8182/*Performs an inverse 8 point Type-II DCT transform.83The output is scaled by a factor of 2 relative to the orthonormal version of84the transform.85_y: The buffer to store the result in.86Data will be placed in every 8th entry (e.g., in a column of an 8x887block).88_x: The input coefficients.89Only the first 4 entries are used.90The other 4 are assumed to be 0.*/91static void idct8_4(ogg_int16_t *_y,const ogg_int16_t _x[8]){92ogg_int32_t t[8];93ogg_int32_t r;94/*Stage 1:*/95t[0]=OC_C4S4*_x[0]>>16;96t[2]=OC_C6S2*_x[2]>>16;97t[3]=OC_C2S6*_x[2]>>16;98t[4]=OC_C7S1*_x[1]>>16;99t[5]=-(OC_C5S3*_x[3]>>16);100t[6]=OC_C3S5*_x[3]>>16;101t[7]=OC_C1S7*_x[1]>>16;102/*Stage 2:*/103r=t[4]+t[5];104t[5]=OC_C4S4*(ogg_int16_t)(t[4]-t[5])>>16;105t[4]=r;106r=t[7]+t[6];107t[6]=OC_C4S4*(ogg_int16_t)(t[7]-t[6])>>16;108t[7]=r;109/*Stage 3:*/110t[1]=t[0]+t[2];111t[2]=t[0]-t[2];112r=t[0]+t[3];113t[3]=t[0]-t[3];114t[0]=r;115r=t[6]+t[5];116t[5]=t[6]-t[5];117t[6]=r;118/*Stage 4:*/119_y[0<<3]=(ogg_int16_t)(t[0]+t[7]);120_y[1<<3]=(ogg_int16_t)(t[1]+t[6]);121_y[2<<3]=(ogg_int16_t)(t[2]+t[5]);122_y[3<<3]=(ogg_int16_t)(t[3]+t[4]);123_y[4<<3]=(ogg_int16_t)(t[3]-t[4]);124_y[5<<3]=(ogg_int16_t)(t[2]-t[5]);125_y[6<<3]=(ogg_int16_t)(t[1]-t[6]);126_y[7<<3]=(ogg_int16_t)(t[0]-t[7]);127}128129/*Performs an inverse 8 point Type-II DCT transform.130The output is scaled by a factor of 2 relative to the orthonormal version of131the transform.132_y: The buffer to store the result in.133Data will be placed in every 8th entry (e.g., in a column of an 8x8134block).135_x: The input coefficients.136Only the first 3 entries are used.137The other 5 are assumed to be 0.*/138static void idct8_3(ogg_int16_t *_y,const ogg_int16_t _x[8]){139ogg_int32_t t[8];140ogg_int32_t r;141/*Stage 1:*/142t[0]=OC_C4S4*_x[0]>>16;143t[2]=OC_C6S2*_x[2]>>16;144t[3]=OC_C2S6*_x[2]>>16;145t[4]=OC_C7S1*_x[1]>>16;146t[7]=OC_C1S7*_x[1]>>16;147/*Stage 2:*/148t[5]=OC_C4S4*t[4]>>16;149t[6]=OC_C4S4*t[7]>>16;150/*Stage 3:*/151t[1]=t[0]+t[2];152t[2]=t[0]-t[2];153r=t[0]+t[3];154t[3]=t[0]-t[3];155t[0]=r;156r=t[6]+t[5];157t[5]=t[6]-t[5];158t[6]=r;159/*Stage 4:*/160_y[0<<3]=(ogg_int16_t)(t[0]+t[7]);161_y[1<<3]=(ogg_int16_t)(t[1]+t[6]);162_y[2<<3]=(ogg_int16_t)(t[2]+t[5]);163_y[3<<3]=(ogg_int16_t)(t[3]+t[4]);164_y[4<<3]=(ogg_int16_t)(t[3]-t[4]);165_y[5<<3]=(ogg_int16_t)(t[2]-t[5]);166_y[6<<3]=(ogg_int16_t)(t[1]-t[6]);167_y[7<<3]=(ogg_int16_t)(t[0]-t[7]);168}169170/*Performs an inverse 8 point Type-II DCT transform.171The output is scaled by a factor of 2 relative to the orthonormal version of172the transform.173_y: The buffer to store the result in.174Data will be placed in every 8th entry (e.g., in a column of an 8x8175block).176_x: The input coefficients.177Only the first 2 entries are used.178The other 6 are assumed to be 0.*/179static void idct8_2(ogg_int16_t *_y,const ogg_int16_t _x[8]){180ogg_int32_t t[8];181ogg_int32_t r;182/*Stage 1:*/183t[0]=OC_C4S4*_x[0]>>16;184t[4]=OC_C7S1*_x[1]>>16;185t[7]=OC_C1S7*_x[1]>>16;186/*Stage 2:*/187t[5]=OC_C4S4*t[4]>>16;188t[6]=OC_C4S4*t[7]>>16;189/*Stage 3:*/190r=t[6]+t[5];191t[5]=t[6]-t[5];192t[6]=r;193/*Stage 4:*/194_y[0<<3]=(ogg_int16_t)(t[0]+t[7]);195_y[1<<3]=(ogg_int16_t)(t[0]+t[6]);196_y[2<<3]=(ogg_int16_t)(t[0]+t[5]);197_y[3<<3]=(ogg_int16_t)(t[0]+t[4]);198_y[4<<3]=(ogg_int16_t)(t[0]-t[4]);199_y[5<<3]=(ogg_int16_t)(t[0]-t[5]);200_y[6<<3]=(ogg_int16_t)(t[0]-t[6]);201_y[7<<3]=(ogg_int16_t)(t[0]-t[7]);202}203204/*Performs an inverse 8 point Type-II DCT transform.205The output is scaled by a factor of 2 relative to the orthonormal version of206the transform.207_y: The buffer to store the result in.208Data will be placed in every 8th entry (e.g., in a column of an 8x8209block).210_x: The input coefficients.211Only the first entry is used.212The other 7 are assumed to be 0.*/213static void idct8_1(ogg_int16_t *_y,const ogg_int16_t _x[1]){214_y[0<<3]=_y[1<<3]=_y[2<<3]=_y[3<<3]=215_y[4<<3]=_y[5<<3]=_y[6<<3]=_y[7<<3]=(ogg_int16_t)(OC_C4S4*_x[0]>>16);216}217218/*Performs an inverse 8x8 Type-II DCT transform.219The input is assumed to be scaled by a factor of 4 relative to orthonormal220version of the transform.221All coefficients but the first 3 in zig-zag scan order are assumed to be 0:222x x 0 0 0 0 0 0223x 0 0 0 0 0 0 02240 0 0 0 0 0 0 02250 0 0 0 0 0 0 02260 0 0 0 0 0 0 02270 0 0 0 0 0 0 02280 0 0 0 0 0 0 02290 0 0 0 0 0 0 0230_y: The buffer to store the result in.231This may be the same as _x.232_x: The input coefficients.*/233static void oc_idct8x8_3(ogg_int16_t _y[64],ogg_int16_t _x[64]){234ogg_int16_t w[64];235int i;236/*Transform rows of x into columns of w.*/237idct8_2(w,_x);238idct8_1(w+1,_x+8);239/*Transform rows of w into columns of y.*/240for(i=0;i<8;i++)idct8_2(_y+i,w+i*8);241/*Adjust for the scale factor.*/242for(i=0;i<64;i++)_y[i]=(ogg_int16_t)(_y[i]+8>>4);243/*Clear input data for next block.*/244_x[0]=_x[1]=_x[8]=0;245}246247/*Performs an inverse 8x8 Type-II DCT transform.248The input is assumed to be scaled by a factor of 4 relative to orthonormal249version of the transform.250All coefficients but the first 10 in zig-zag scan order are assumed to be 0:251x x x x 0 0 0 0252x x x 0 0 0 0 0253x x 0 0 0 0 0 0254x 0 0 0 0 0 0 02550 0 0 0 0 0 0 02560 0 0 0 0 0 0 02570 0 0 0 0 0 0 02580 0 0 0 0 0 0 0259_y: The buffer to store the result in.260This may be the same as _x.261_x: The input coefficients.*/262static void oc_idct8x8_10(ogg_int16_t _y[64],ogg_int16_t _x[64]){263ogg_int16_t w[64];264int i;265/*Transform rows of x into columns of w.*/266idct8_4(w,_x);267idct8_3(w+1,_x+8);268idct8_2(w+2,_x+16);269idct8_1(w+3,_x+24);270/*Transform rows of w into columns of y.*/271for(i=0;i<8;i++)idct8_4(_y+i,w+i*8);272/*Adjust for the scale factor.*/273for(i=0;i<64;i++)_y[i]=(ogg_int16_t)(_y[i]+8>>4);274/*Clear input data for next block.*/275_x[0]=_x[1]=_x[2]=_x[3]=_x[8]=_x[9]=_x[10]=_x[16]=_x[17]=_x[24]=0;276}277278/*Performs an inverse 8x8 Type-II DCT transform.279The input is assumed to be scaled by a factor of 4 relative to orthonormal280version of the transform.281_y: The buffer to store the result in.282This may be the same as _x.283_x: The input coefficients.*/284static void oc_idct8x8_slow(ogg_int16_t _y[64],ogg_int16_t _x[64]){285ogg_int16_t w[64];286int i;287/*Transform rows of x into columns of w.*/288for(i=0;i<8;i++)idct8(w+i,_x+i*8);289/*Transform rows of w into columns of y.*/290for(i=0;i<8;i++)idct8(_y+i,w+i*8);291/*Adjust for the scale factor.*/292for(i=0;i<64;i++)_y[i]=(ogg_int16_t)(_y[i]+8>>4);293/*Clear input data for next block.*/294for(i=0;i<64;i++)_x[i]=0;295}296297/*Performs an inverse 8x8 Type-II DCT transform.298The input is assumed to be scaled by a factor of 4 relative to orthonormal299version of the transform.*/300void oc_idct8x8_c(ogg_int16_t _y[64],ogg_int16_t _x[64],int _last_zzi){301/*_last_zzi is subtly different from an actual count of the number of302coefficients we decoded for this block.303It contains the value of zzi BEFORE the final token in the block was304decoded.305In most cases this is an EOB token (the continuation of an EOB run from a306previous block counts), and so this is the same as the coefficient count.307However, in the case that the last token was NOT an EOB token, but filled308the block up with exactly 64 coefficients, _last_zzi will be less than 64.309Provided the last token was not a pure zero run, the minimum value it can310be is 46, and so that doesn't affect any of the cases in this routine.311However, if the last token WAS a pure zero run of length 63, then _last_zzi312will be 1 while the number of coefficients decoded is 64.313Thus, we will trigger the following special case, where the real314coefficient count would not.315Note also that a zero run of length 64 will give _last_zzi a value of 0,316but we still process the DC coefficient, which might have a non-zero value317due to DC prediction.318Although convoluted, this is arguably the correct behavior: it allows us to319use a smaller transform when the block ends with a long zero run instead320of a normal EOB token.321It could be smarter... multiple separate zero runs at the end of a block322will fool it, but an encoder that generates these really deserves what it323gets.324Needless to say we inherited this approach from VP3.*/325/*Then perform the iDCT.*/326if(_last_zzi<=3)oc_idct8x8_3(_y,_x);327else if(_last_zzi<=10)oc_idct8x8_10(_y,_x);328else oc_idct8x8_slow(_y,_x);329}330331332