jidct2d.pas 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. Unit JIDct2D;
  2. { This file contains a fast, not so accurate integer implementation of the
  3. inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
  4. must also perform dequantization of the input coefficients.
  5. A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  6. on each row (or vice versa, but it's more convenient to emit a row at
  7. a time). Direct algorithms are also available, but they are much more
  8. complex and seem not to be any faster when reduced to code.
  9. The Feig direct 2D scaled Discrete Cosine Transform extends Arai, Agui
  10. and Nakajima fast scaled DCT to 2D (464 adds and 80 mult.) with further
  11. computational saving (462 adds, 54 mults and 6 shits).
  12. The forward DCT is described with flow diagrams from the Pennebaker&
  13. Mitchell JPEG book. The inverse DCT flow diagrams are obtained
  14. from the inverse matrices. Scaling must be done accordingly.
  15. Jacques NOMSSI NZALI, May 16th 1995 }
  16. interface
  17. uses
  18. jmorecfg,
  19. jinclude,
  20. jpeglib,
  21. jdct; { Private declarations for DCT subsystem }
  22. {$I jconfig.inc}
  23. { Perform dequantization and inverse DCT on one block of coefficients. }
  24. {GLOBAL}
  25. procedure jpeg_idct_i2d (cinfo : j_decompress_ptr;
  26. compptr : jpeg_component_info_ptr;
  27. coef_block : JCOEFPTR;
  28. output_buf : JSAMPARRAY;
  29. output_col : JDIMENSION);
  30. implementation
  31. { This module is specialized to the case DCTSIZE = 8. }
  32. {$ifndef DCTSIZE_IS_8}
  33. Sorry, this code only copes with 8x8 DCTs. { deliberate syntax err }
  34. {$endif}
  35. { Scaling decisions are generally the same as in the LL&M algorithm;
  36. see jidctint.c for more details. However, we choose to descale
  37. (right shift) multiplication products as soon as they are formed,
  38. rather than carrying additional fractional bits into subsequent additions.
  39. This compromises accuracy slightly, but it lets us save a few shifts.
  40. More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
  41. everywhere except in the multiplications proper; this saves a good deal
  42. of work on 16-bit-int machines.
  43. The dequantized coefficients are not integers because the AA&N scaling
  44. factors have been incorporated. We represent them scaled up by PASS1_BITS,
  45. so that the first and second IDCT rounds have the same input scaling.
  46. For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
  47. avoid a descaling shift; this compromises accuracy rather drastically
  48. for small quantization table entries, but it saves a lot of shifts.
  49. For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
  50. so we use a much larger scaling factor to preserve accuracy.
  51. A final compromise is to represent the multiplicative constants to only
  52. 8 fractional bits, rather than 13. This saves some shifting work on some
  53. machines, and may also reduce the cost of multiplication (since there
  54. are fewer one-bits in the constants). }
  55. {$ifdef BITS_IN_JSAMPLE_IS_8}
  56. const
  57. CONST_BITS = 8;
  58. PASS1_BITS = 2;
  59. {$else}
  60. CONST_BITS = 8;
  61. PASS1_BITS = 1; { lose a little precision to avoid overflow }
  62. {$endif}
  63. { Convert a positive real constant to an integer scaled by CONST_SCALE. }
  64. const
  65. CONST_SCALE = (INT32(1) shl CONST_BITS);
  66. const
  67. FIX_1_082392200 = INT32(Round(CONST_SCALE*1.082392200)); {277}
  68. FIX_1_414213562 = INT32(Round(CONST_SCALE*1.414213562)); {362}
  69. FIX_1_847759065 = INT32(Round(CONST_SCALE*1.847759065)); {473}
  70. FIX_2_613125930 = INT32(Round(CONST_SCALE*2.613125930)); {669}
  71. { Descale and correctly round an INT32 value that's scaled by N bits.
  72. We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  73. the fudge factor is correct for either sign of X. }
  74. function DESCALE(x : INT32; n : int) : INT32;
  75. var
  76. shift_temp : INT32;
  77. begin
  78. {$ifdef USE_ACCURATE_ROUNDING}
  79. shift_temp := x + (INT32(1) shl (n-1));
  80. {$else}
  81. { We can gain a little more speed, with a further compromise in accuracy,
  82. by omitting the addition in a descaling shift. This yields an incorrectly
  83. rounded result half the time... }
  84. shift_temp := x;
  85. {$endif}
  86. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  87. if shift_temp < 0 then
  88. Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
  89. else
  90. {$endif}
  91. Descale := (shift_temp shr n);
  92. end;
  93. { Multiply a DCTELEM variable by an INT32 constant, and immediately
  94. descale to yield a DCTELEM result. }
  95. {(DCTELEM( DESCALE((var) * (const), CONST_BITS))}
  96. function Multiply(Avar, Aconst: Integer): DCTELEM;
  97. begin
  98. Multiply := DCTELEM( Avar*INT32(Aconst) div CONST_SCALE);
  99. end;
  100. { Dequantize a coefficient by multiplying it by the multiplier-table
  101. entry; produce a DCTELEM result. For 8-bit data a 16x16->16
  102. multiplication will do. For 12-bit data, the multiplier table is
  103. declared INT32, so a 32-bit multiply will be used. }
  104. {$ifdef BITS_IN_JSAMPLE_IS_8}
  105. function DEQUANTIZE(coef,quantval : int) : int;
  106. begin
  107. Dequantize := ( IFAST_MULT_TYPE(coef) * quantval);
  108. end;
  109. {$else}
  110. #define DEQUANTIZE(coef,quantval) \
  111. DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
  112. {$endif}
  113. { Like DESCALE, but applies to a DCTELEM and produces an int.
  114. We assume that int right shift is unsigned if INT32 right shift is. }
  115. function IDESCALE(x : DCTELEM; n : int) : int;
  116. {$ifdef BITS_IN_JSAMPLE_IS_8}
  117. const
  118. DCTELEMBITS = 16; { DCTELEM may be 16 or 32 bits }
  119. {$else}
  120. const
  121. DCTELEMBITS = 32; { DCTELEM must be 32 bits }
  122. {$endif}
  123. var
  124. ishift_temp : DCTELEM;
  125. begin
  126. {$ifndef USE_ACCURATE_ROUNDING}
  127. ishift_temp := x + (INT32(1) shl (n-1));
  128. {$else}
  129. { We can gain a little more speed, with a further compromise in accuracy,
  130. by omitting the addition in a descaling shift. This yields an incorrectly
  131. rounded result half the time... }
  132. ishift_temp := x;
  133. {$endif}
  134. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  135. if ishift_temp < 0 then
  136. IDescale := (ishift_temp shr n)
  137. or ((not DCTELEM(0)) shl (DCTELEMBITS-n))
  138. else
  139. {$endif}
  140. IDescale := (ishift_temp shr n);
  141. end;
  142. { Perform dequantization and inverse DCT on one block of coefficients. }
  143. {GLOBAL}
  144. procedure jpeg_idct_i2d (cinfo : j_decompress_ptr;
  145. compptr : jpeg_component_info_ptr;
  146. coef_block : JCOEFPTR;
  147. output_buf : JSAMPARRAY;
  148. output_col : JDIMENSION);
  149. Const
  150. CONST_IC4 = 1.414213562; { 1/0.707106781; }
  151. FP_IC4 = FIX_1_414213562;
  152. FP_I_C4_2 = FP_IC4;
  153. type
  154. PWorkspace = ^TWorkspace;
  155. TWorkspace = coef_bits_field; { buffers data between passes }
  156. Procedure N1(var x, y : integer); { rotator 1 }
  157. Const
  158. FP_a5 = FIX_1_847759065;
  159. FP_a4 = FIX_2_613125930;
  160. FP_a2 = FIX_1_082392200;
  161. var
  162. z5, tmp : integer;
  163. begin
  164. tmp := x;
  165. z5 := Multiply(tmp + y, FP_a5); { c6 }
  166. x := Multiply(y, FP_a2) - z5; { c2-c6 }
  167. y := Multiply(tmp, -FP_a4) + z5; { c2+c6 }
  168. end;
  169. Procedure N2(var x, y : integer); { N1 scaled by c4 }
  170. Const
  171. FP_b5 = Integer(Round(CONST_SCALE*1.847759065*CONST_IC4));
  172. FP_b4 = Integer(Round(CONST_SCALE*2.613125930*CONST_IC4));
  173. FP_b2 = Integer(Round(CONST_SCALE*1.082392200*CONST_IC4));
  174. var
  175. z5, tmp : integer;
  176. begin
  177. tmp := x;
  178. z5 := Multiply(tmp + y, FP_b5);
  179. x := Multiply(y, FP_b2) - z5;
  180. y := Multiply(tmp,-FP_b4) + z5;
  181. end;
  182. var
  183. column, row : byte;
  184. var
  185. tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7 : DCTELEM;
  186. tmp10, tmp11, tmp12, tmp13 : DCTELEM;
  187. z10, z11, z12, z13 : DCTELEM;
  188. inptr : JCOEFPTR;
  189. quantptr : IFAST_MULT_TYPE_FIELD_PTR;
  190. wsptr : PWorkspace;
  191. outptr : JSAMPROW;
  192. range_limit : JSAMPROW;
  193. ctr : int;
  194. workspace : TWorkspace; { buffers data between passes }
  195. {SHIFT_TEMPS { for DESCALE }
  196. {ISHIFT_TEMPS { for IDESCALE }
  197. var
  198. dcval : int;
  199. var
  200. dcval_ : JSAMPLE;
  201. begin
  202. { Each IDCT routine is responsible for range-limiting its results and
  203. converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
  204. be quite far out of range if the input data is corrupt, so a bulletproof
  205. range-limiting step is required. We use a mask-and-table-lookup method
  206. to do the combined operations quickly. See the comments with
  207. prepare_range_limit_table (in jdmaster.c) for more info. }
  208. range_limit := JSAMPROW(@(cinfo^.sample_range_limit^[CENTERJSAMPLE]));
  209. { Pass 1: process columns from input, store into work array. }
  210. inptr := coef_block;
  211. quantptr := IFAST_MULT_TYPE_FIELD_PTR(compptr^.dct_table);
  212. wsptr := @workspace;
  213. for ctr := pred(DCTSIZE) downto 0 do
  214. begin
  215. { short-circuiting is not easily done here }
  216. // bbo := @outptr;
  217. for num := 0 to Pred(count) do
  218. begin
  219. { R1 x R1 }
  220. for column := 7 downto 0 do
  221. BEGIN
  222. tmp5 := inptr^[1*RowSize + column];
  223. inptr^[1*RowSize + column] := inptr^[4*RowSize + column];
  224. tmp7 := inptr^[3*RowSize + column];
  225. a := inptr^[2*RowSize + column];
  226. b := inptr^[6*RowSize + column];
  227. inptr^[2*RowSize + column] := a - b;
  228. inptr^[3*RowSize + column] := a + b;
  229. a := inptr^[5*RowSize + column];
  230. inptr^[4*RowSize + column] := a - tmp7;
  231. z13 := a + tmp7;
  232. b := inptr^[7*RowSize + column];
  233. inptr^[6*RowSize + column] := tmp5 - b;
  234. z11 := tmp5 + b;
  235. inptr^[5*RowSize + column] := z11 - z13;
  236. inptr^[7*RowSize + column] := z11 + z13;
  237. END;
  238. { Even part }
  239. tmp0 := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]);
  240. tmp1 := DEQUANTIZE(inptr^[DCTSIZE*2], quantptr^[DCTSIZE*2]);
  241. tmp2 := DEQUANTIZE(inptr^[DCTSIZE*4], quantptr^[DCTSIZE*4]);
  242. tmp3 := DEQUANTIZE(inptr^[DCTSIZE*6], quantptr^[DCTSIZE*6]);
  243. tmp10 := tmp0 + tmp2; { phase 3 }
  244. tmp11 := tmp0 - tmp2;
  245. tmp13 := tmp1 + tmp3; { phases 5-3 }
  246. tmp12 := MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; { 2*c4 }
  247. tmp0 := tmp10 + tmp13; { phase 2 }
  248. tmp3 := tmp10 - tmp13;
  249. tmp1 := tmp11 + tmp12;
  250. tmp2 := tmp11 - tmp12;
  251. { Odd part }
  252. tmp4 := DEQUANTIZE(inptr^[DCTSIZE*1], quantptr^[DCTSIZE*1]);
  253. tmp5 := DEQUANTIZE(inptr^[DCTSIZE*3], quantptr^[DCTSIZE*3]);
  254. tmp6 := DEQUANTIZE(inptr^[DCTSIZE*5], quantptr^[DCTSIZE*5]);
  255. tmp7 := DEQUANTIZE(inptr^[DCTSIZE*7], quantptr^[DCTSIZE*7]);
  256. z13 := tmp6 + tmp5; { phase 6 }
  257. z10 := tmp6 - tmp5;
  258. z11 := tmp4 + tmp7;
  259. z12 := tmp4 - tmp7;
  260. tmp7 := z11 + z13; { phase 5 }
  261. tmp11 := MULTIPLY(z11 - z13, FIX_1_414213562); { 2*c4 }
  262. z5 := MULTIPLY(z10 + z12, FIX_1_847759065); { 2*c2 }
  263. tmp10 := MULTIPLY(z12, FIX_1_082392200) - z5; { 2*(c2-c6) }
  264. tmp12 := MULTIPLY(z10, - FIX_2_613125930) + z5; { -2*(c2+c6) }
  265. tmp6 := tmp12 - tmp7; { phase 2 }
  266. tmp5 := tmp11 - tmp6;
  267. tmp4 := tmp10 + tmp5;
  268. wsptr^[DCTSIZE*0] := int (tmp0 + tmp7);
  269. wsptr^[DCTSIZE*7] := int (tmp0 - tmp7);
  270. wsptr^[DCTSIZE*1] := int (tmp1 + tmp6);
  271. wsptr^[DCTSIZE*6] := int (tmp1 - tmp6);
  272. wsptr^[DCTSIZE*2] := int (tmp2 + tmp5);
  273. wsptr^[DCTSIZE*5] := int (tmp2 - tmp5);
  274. wsptr^[DCTSIZE*4] := int (tmp3 + tmp4);
  275. wsptr^[DCTSIZE*3] := int (tmp3 - tmp4);
  276. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  277. Inc(IFAST_MULT_TYPE_PTR(quantptr));
  278. Inc(int_ptr(wsptr));
  279. end;
  280. { Pass 2: process rows from work array, store into output array. }
  281. { Note that we must descale the results by a factor of 8 == 2**3, }
  282. { and also undo the PASS1_BITS scaling. }
  283. wsptr := @workspace;
  284. for ctr := 0 to pred(DCTSIZE) do
  285. begin
  286. outptr := JSAMPROW(@output_buf^[ctr]^[output_col]);
  287. { Rows of zeroes can be exploited in the same way as we did with columns.
  288. However, the column calculation has created many nonzero AC terms, so
  289. the simplification applies less often (typically 5% to 10% of the time).
  290. On machines with very fast multiplication, it's possible that the
  291. test takes more time than it's worth. In that case this section
  292. may be commented out. }
  293. {$ifndef NO_ZERO_ROW_TEST}
  294. if ((wsptr^[1]) or (wsptr^[2]) or (wsptr^[3]) or (wsptr^[4]) or (wsptr^[5]) or
  295. (wsptr^[6]) or (wsptr^[7]) = 0) then
  296. begin
  297. { AC terms all zero }
  298. JSAMPLE(dcval_) := range_limit^[IDESCALE(wsptr^[0], PASS1_BITS+3)
  299. and RANGE_MASK];
  300. outptr^[0] := dcval_;
  301. outptr^[1] := dcval_;
  302. outptr^[2] := dcval_;
  303. outptr^[3] := dcval_;
  304. outptr^[4] := dcval_;
  305. outptr^[5] := dcval_;
  306. outptr^[6] := dcval_;
  307. outptr^[7] := dcval_;
  308. Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
  309. continue;
  310. end;
  311. {$endif}
  312. { Even part }
  313. tmp10 := (DCTELEM(wsptr^[0]) + DCTELEM(wsptr^[4]));
  314. tmp11 := (DCTELEM(wsptr^[0]) - DCTELEM(wsptr^[4]));
  315. tmp13 := (DCTELEM(wsptr^[2]) + DCTELEM(wsptr^[6]));
  316. tmp12 := MULTIPLY(DCTELEM(wsptr^[2]) - DCTELEM(wsptr^[6]), FIX_1_414213562)
  317. - tmp13;
  318. tmp0 := tmp10 + tmp13;
  319. tmp3 := tmp10 - tmp13;
  320. tmp1 := tmp11 + tmp12;
  321. tmp2 := tmp11 - tmp12;
  322. { Odd part }
  323. z13 := DCTELEM(wsptr^[5]) + DCTELEM(wsptr^[3]);
  324. z10 := DCTELEM(wsptr^[5]) - DCTELEM(wsptr^[3]);
  325. z11 := DCTELEM(wsptr^[1]) + DCTELEM(wsptr^[7]);
  326. z12 := DCTELEM(wsptr^[1]) - DCTELEM(wsptr^[7]);
  327. tmp7 := z11 + z13; { phase 5 }
  328. tmp11 := MULTIPLY(z11 - z13, FIX_1_414213562); { 2*c4 }
  329. z5 := MULTIPLY(z10 + z12, FIX_1_847759065); { 2*c2 }
  330. tmp10 := MULTIPLY(z12, FIX_1_082392200) - z5; { 2*(c2-c6) }
  331. tmp12 := MULTIPLY(z10, - FIX_2_613125930) + z5; { -2*(c2+c6) }
  332. tmp6 := tmp12 - tmp7; { phase 2 }
  333. tmp5 := tmp11 - tmp6;
  334. tmp4 := tmp10 + tmp5;
  335. { Final output stage: scale down by a factor of 8 and range-limit }
  336. outptr^[0] := range_limit^[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
  337. and RANGE_MASK];
  338. outptr^[7] := range_limit^[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
  339. and RANGE_MASK];
  340. outptr^[1] := range_limit^[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
  341. and RANGE_MASK];
  342. outptr^[6] := range_limit^[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
  343. and RANGE_MASK];
  344. outptr^[2] := range_limit^[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
  345. and RANGE_MASK];
  346. outptr^[5] := range_limit^[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
  347. and RANGE_MASK];
  348. outptr^[4] := range_limit^[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
  349. and RANGE_MASK];
  350. outptr^[3] := range_limit^[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
  351. and RANGE_MASK];
  352. Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
  353. end;
  354. end;
  355. end.
  356. ----------------------------------------------------------
  357. type
  358. matasm = array[0..DCTSIZE2-1] of integer;
  359. bmatrix = array[0..DCTSIZE2-1] of byte;
  360. bmatrixptr = ^bmatrix;
  361. procedure ANN_IDCT(var coef_block :matasm;
  362. var outptr :bmatrix);
  363. var coeffs :matasm; = coef_block
  364. var outptr :bmatrix); output_buf
  365. Const
  366. CONST_IC4 = 1.414213562; { 1/0.707106781; }
  367. FP_IC4 = FIX_1_414213562;
  368. FP_I_C4_2 = FP_IC4;
  369. Function Descale(x : integer):byte;
  370. var y : integer;
  371. begin
  372. y := (x + (1 shl (16-1))+ (4 shl PASS_BITS)) div (8 shl PASS_BITS);
  373. { DeScale := x sar (3 + PASS_BITS);
  374. Borland Pascal SHR is unsigned }
  375. if y < 0 then
  376. descale := 0
  377. else
  378. if y > $ff then
  379. descale := $ff
  380. else
  381. descale := y;
  382. end;
  383. function Multiply(X, Y: Integer): integer; assembler;
  384. asm
  385. mov ax, X
  386. imul Y
  387. mov al, ah
  388. mov ah, dl
  389. end;
  390. Const
  391. RowSize = 8;
  392. var
  393. a, b : integer;
  394. inptr : JCOEFPTR;
  395. outptr : bmatrixptr;
  396. num : integer;
  397. begin
  398. { Each IDCT routine is responsible for range-limiting its results and
  399. converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
  400. be quite far out of range if the input data is corrupt, so a bulletproof
  401. range-limiting step is required. We use a mask-and-table-lookup method
  402. to do the combined operations quickly. See the comments with
  403. prepare_range_limit_table (in jdmaster.c) for more info. }
  404. range_limit := JSAMPROW(@(cinfo^.sample_range_limit^[CENTERJSAMPLE]));
  405. { Pass 1: process columns from input, store into work array. }
  406. inptr := @coef_block; + ctr*RowSize
  407. quantptr := IFAST_MULT_TYPE_FIELD_PTR(compptr^.dct_table);
  408. for ctr := pred(DCTSIZE) downto 0 do
  409. BEGIN
  410. tmp5 := inptr^[1];
  411. inptr^[1] := inptr^[4];
  412. tmp7 := inptr^[3];
  413. a := inptr^[2];
  414. b := inptr^[6];
  415. inptr^[2] := a - b;
  416. inptr^[3] := a + b;
  417. a := inptr^[5];
  418. inptr^[+ 4] := a - tmp7;
  419. z13 := a + tmp7;
  420. b := inptr^[7];
  421. inptr^[6] := tmp5 - b;
  422. z11 := tmp5 + b;
  423. inptr^[5] := z11 - z13;
  424. inptr^[7] := z11 + z13;
  425. END;
  426. { M x M tensor }
  427. for row := 0 to 7 do
  428. Case row of
  429. 0,1,3,7: { M1 }
  430. begin
  431. inptr^[row*RowSize + 2] := Multiply(inptr^[row*RowSize + 2], FP_IC4); { 2/c4 }
  432. inptr^[row*RowSize + 5] := Multiply(inptr^[row*RowSize + 5], FP_IC4); { 2/c4 }
  433. N1(inptr^[row*RowSize + 4], inptr^[row*RowSize + 6]);
  434. end;
  435. 2,5: { M2 }
  436. begin
  437. inptr^[row*RowSize + 0] := Multiply(inptr^[row*RowSize + 0], FP_IC4);
  438. inptr^[row*RowSize + 1] := Multiply(inptr^[row*RowSize + 1], FP_IC4);
  439. inptr^[row*RowSize + 3] := Multiply(inptr^[row*RowSize + 3], FP_IC4);
  440. inptr^[row*RowSize + 7] := Multiply(inptr^[row*RowSize + 7], FP_IC4);
  441. inptr^[row*RowSize + 2] := inptr^[row*RowSize + 2] * 2; { shift }
  442. inptr^[row*RowSize + 5] := inptr^[row*RowSize + 5] * 2;
  443. N2(inptr^[row*RowSize + 4], inptr^[row*RowSize + 6]);
  444. end;
  445. end; { Case }
  446. { M x N tensor }
  447. { rows 4,6 }
  448. begin
  449. N1(inptr^[4*RowSize + 0], inptr^[6*RowSize + 0]);
  450. N1(inptr^[4*RowSize + 1], inptr^[6*RowSize + 1]);
  451. N1(inptr^[4*RowSize + 3], inptr^[6*RowSize + 3]);
  452. N1(inptr^[4*RowSize + 7], inptr^[6*RowSize + 7]);
  453. N2(inptr^[4*RowSize + 2], inptr^[6*RowSize + 2]);
  454. N2(inptr^[4*RowSize + 5], inptr^[6*RowSize + 5]);
  455. { N3 }
  456. { two inverse matrices => same as FDCT }
  457. tmp0 := inptr^[4*RowSize + 4];
  458. tmp3 := inptr^[6*RowSize + 6];
  459. tmp12 := (tmp0 + tmp3) * 2;
  460. z10 := tmp0 - tmp3;
  461. tmp1 := inptr^[6*RowSize + 4];
  462. tmp2 := inptr^[4*RowSize + 6];
  463. tmp13 :=-(tmp1 - tmp2)*2;
  464. z11 := tmp1 + tmp2;
  465. tmp0 := Multiply(z10 + z11, FP_I_C4_2);
  466. tmp1 := Multiply(z10 - z11, FP_I_C4_2);
  467. inptr^[4*RowSize + 4] := tmp12 + tmp0;
  468. inptr^[6*RowSize + 4] := tmp1 + tmp13;
  469. inptr^[4*RowSize + 6] := tmp1 - tmp13;
  470. inptr^[6*RowSize + 6] := tmp12 - tmp0;
  471. end;
  472. { R2 x R2 }
  473. for row := 0 to 7 do
  474. BEGIN
  475. { Odd part }
  476. tmp7 := inptr^[row*RowSize + 7];
  477. tmp6 := inptr^[row*RowSize + 6] - tmp7;
  478. tmp5 := inptr^[row*RowSize + 5] - tmp6;
  479. tmp4 :=-inptr^[row*RowSize + 4] - tmp5;
  480. { even part }
  481. tmp0 := inptr^[row*RowSize + 0];
  482. tmp1 := inptr^[row*RowSize + 1];
  483. tmp10 := tmp0 + tmp1;
  484. tmp11 := tmp0 - tmp1;
  485. tmp2 := inptr^[row*RowSize + 2];
  486. tmp13 := inptr^[row*RowSize + 3];
  487. tmp12 := tmp2 - tmp13;
  488. tmp0 := tmp10 + tmp13;
  489. tmp3 := tmp10 - tmp13;
  490. inptr^[row*RowSize + 0] := (tmp0 + tmp7);
  491. inptr^[row*RowSize + 7] := (tmp0 - tmp7);
  492. inptr^[row*RowSize + 3] := (tmp3 + tmp4);
  493. inptr^[row*RowSize + 4] := (tmp3 - tmp4);
  494. tmp1 := tmp11 + tmp12;
  495. tmp2 := tmp11 - tmp12;
  496. inptr^[row*RowSize + 1] := (tmp1 + tmp6);
  497. inptr^[row*RowSize + 6] := (tmp1 - tmp6);
  498. inptr^[row*RowSize + 2] := (tmp2 + tmp5);
  499. inptr^[row*RowSize + 5] := (tmp2 - tmp5);
  500. END;
  501. for ctr := 0 to pred(DCTSIZE) do
  502. BEGIN
  503. outptr := JSAMPROW(@output_buf^[ctr]^[output_col]);
  504. { even part }
  505. tmp0 := inptr^[0*RowSize + ctr];
  506. tmp1 := inptr^[1*RowSize + ctr];
  507. tmp2 := inptr^[2*RowSize + ctr];
  508. tmp3 := inptr^[3*RowSize + ctr];
  509. tmp10 := tmp0 + tmp1;
  510. tmp11 := tmp0 - tmp1;
  511. tmp13 := tmp3;
  512. tmp12 := tmp2 - tmp3;
  513. tmp0 := tmp10 + tmp13;
  514. tmp3 := tmp10 - tmp13;
  515. tmp1 := tmp11 + tmp12;
  516. tmp2 := tmp11 - tmp12;
  517. { Odd part }
  518. tmp4 := inptr^[4*RowSize + ctr];
  519. tmp5 := inptr^[5*RowSize + ctr];
  520. tmp6 := inptr^[6*RowSize + ctr];
  521. tmp7 := inptr^[7*RowSize + ctr];
  522. tmp6 := tmp6 - tmp7;
  523. tmp5 := tmp5 - tmp6;
  524. tmp4 :=-tmp4 - tmp5;
  525. outptr^[0*RowSize + ctr] := DeScale(tmp0 + tmp7);
  526. outptr^[7*RowSize + ctr] := DeScale(tmp0 - tmp7);
  527. outptr^[1*RowSize + ctr] := DeScale(tmp1 + tmp6);
  528. outptr^[6*RowSize + ctr] := DeScale(tmp1 - tmp6);
  529. outptr^[2*RowSize + ctr] := DeScale(tmp2 + tmp5);
  530. outptr^[5*RowSize + ctr] := DeScale(tmp2 - tmp5);
  531. outptr^[3*RowSize + ctr] := DeScale(tmp3 + tmp4);
  532. outptr^[4*RowSize + ctr] := DeScale(tmp3 - tmp4);
  533. { Final output stage: scale down by a factor of 8 and range-limit }
  534. outptr^[0] := range_limit^[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
  535. and RANGE_MASK];
  536. outptr^[7] := range_limit^[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
  537. and RANGE_MASK];
  538. outptr^[1] := range_limit^[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
  539. and RANGE_MASK];
  540. outptr^[6] := range_limit^[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
  541. and RANGE_MASK];
  542. outptr^[2] := range_limit^[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
  543. and RANGE_MASK];
  544. outptr^[5] := range_limit^[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
  545. and RANGE_MASK];
  546. outptr^[4] := range_limit^[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
  547. and RANGE_MASK];
  548. outptr^[3] := range_limit^[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
  549. and RANGE_MASK];
  550. END;
  551. Inc(bbo);
  552. Inc(inptr);
  553. End;
  554. End; {----------------------------------------}
  555. {GLOBAL}
  556. procedure jpeg_idct_i2d (cinfo : j_decompress_ptr;
  557. compptr : jpeg_component_info_ptr;
  558. coef_block : JCOEFPTR;
  559. output_buf : JSAMPARRAY;
  560. output_col : JDIMENSION);
  561. procedure Feig_2D_IDCT(coef_block :imatrix;
  562. output_buf : JSAMPARRAY);
  563. Const
  564. CONST_IC4 = 1.414213562; { 1/0.707106781; }
  565. FP_IC4 = Integer(Round(IFX_CONST*CONST_IC4));
  566. FP_I_C4_2 = FP_IC4;
  567. Function Descale(x : integer):integer;
  568. begin
  569. DeScale := (x+ (4 shl PASS_BITS)) div (8 shl PASS_BITS);
  570. { DeScale := x sar (3 + PASS_BITS);
  571. Borland Pascal SHR is unsigned }
  572. end;
  573. {
  574. function Multiply(X, Y: Integer): integer;
  575. begin
  576. Multiply := Integer( X*LongInt(Y) div IFX_CONST);
  577. end;
  578. }
  579. function Multiply(X, Y: Integer): integer; assembler;
  580. asm
  581. mov ax, X
  582. imul Y
  583. mov al, ah
  584. mov ah, dl
  585. end;
  586. var
  587. z10, z11, z12, z13,
  588. tmp0,tmp1,tmp2,tmp3,
  589. tmp4,tmp5,tmp6,tmp7,
  590. tmp10,tmp11,
  591. tmp12,tmp13 : integer;
  592. column, row : byte;
  593. Procedure N1(var x, y : integer); { rotator 1 }
  594. Const
  595. FP_a5 = Integer(Round(IFX_CONST*1.847759065));
  596. FP_a4 = Integer(Round(IFX_CONST*2.613125930));
  597. FP_a2 = Integer(Round(IFX_CONST*1.082392200));
  598. var
  599. z5, tmp : integer;
  600. begin
  601. tmp := x;
  602. z5 := Multiply(tmp + y, FP_a5); { c6 }
  603. x := Multiply(y, FP_a2) - z5; { c2-c6 }
  604. y := Multiply(tmp, -FP_a4) + z5; { c2+c6 }
  605. end;
  606. Procedure N2(var x, y : integer); { N1 scaled by c4 }
  607. Const
  608. FP_b5 = Integer(Round(IFX_CONST*1.847759065*CONST_IC4));
  609. FP_b4 = Integer(Round(IFX_CONST*2.613125930*CONST_IC4));
  610. FP_b2 = Integer(Round(IFX_CONST*1.082392200*CONST_IC4));
  611. var
  612. z5, tmp : integer;
  613. begin
  614. tmp := x;
  615. z5 := Multiply(tmp + y, FP_b5);
  616. x := Multiply(y, FP_b2) - z5;
  617. y := Multiply(tmp,-FP_b4) + z5;
  618. end;
  619. var
  620. tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7 : DCTELEM;
  621. tmp10, tmp11, tmp12, tmp13 : DCTELEM;
  622. z10, z11, z12, z13 : DCTELEM;
  623. inptr : JCOEFPTR;
  624. quantptr : IFAST_MULT_TYPE_FIELD_PTR;
  625. wsptr : PWorkspace;
  626. outptr : JSAMPROW;
  627. range_limit : JSAMPROW;
  628. ctr : int;
  629. workspace : TWorkspace; { buffers data between passes }
  630. {SHIFT_TEMPS { for DESCALE }
  631. {ISHIFT_TEMPS { for IDESCALE }
  632. var
  633. dcval : int;
  634. var
  635. dcval_ : JSAMPLE;
  636. begin
  637. { Each IDCT routine is responsible for range-limiting its results and
  638. converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
  639. be quite far out of range if the input data is corrupt, so a bulletproof
  640. range-limiting step is required. We use a mask-and-table-lookup method
  641. to do the combined operations quickly. See the comments with
  642. prepare_range_limit_table (in jdmaster.c) for more info. }
  643. range_limit := JSAMPROW(@(cinfo^.sample_range_limit^[CENTERJSAMPLE]));
  644. { Pass 1: process columns from input, store into work array. }
  645. inptr := coef_block;
  646. quantptr := IFAST_MULT_TYPE_FIELD_PTR(compptr^.dct_table);
  647. wsptr := @workspace;
  648. { R1 x R1 }
  649. for ctr := pred(DCTSIZE) downto 0 do
  650. BEGIN
  651. { even part }
  652. tmp1 := DEQUANTIZE(inptr^[DCTSIZE*2], quantptr^[DCTSIZE*2]);
  653. tmp3 := DEQUANTIZE(inptr^[DCTSIZE*6], quantptr^[DCTSIZE*6]);
  654. wsptr^[DCTSIZE*0] := int (DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]));
  655. wsptr^[DCTSIZE*1] := int (DEQUANTIZE(inptr^[DCTSIZE*4], quantptr^[DCTSIZE*4]);
  656. { Odd part }
  657. tmp6 := DEQUANTIZE(inptr^[DCTSIZE*5], quantptr^[DCTSIZE*5]);
  658. tmp4 := DEQUANTIZE(inptr^[DCTSIZE*1], quantptr^[DCTSIZE*1]);
  659. tmp7 := DEQUANTIZE(inptr^[DCTSIZE*7], quantptr^[DCTSIZE*7]);
  660. tmp5 := DEQUANTIZE(inptr^[DCTSIZE*3], quantptr^[DCTSIZE*3]);
  661. z13 := tmp6 + tmp5;
  662. wsptr^[DCTSIZE*4] := int (tmp6 - tmp5);
  663. z11 := tmp4 + tmp7;
  664. wsptr^[DCTSIZE*6] := int (tmp4 - tmp7);
  665. wsptr^[DCTSIZE*7] := int (z11 + z13);
  666. wsptr^[DCTSIZE*5] := int (z11 - z13);
  667. wsptr^[DCTSIZE*3] := int (tmp1 + tmp3);
  668. wsptr^[DCTSIZE*2] := int (tmp1 - tmp3);
  669. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  670. Inc(IFAST_MULT_TYPE_PTR(quantptr));
  671. Inc(int_ptr(wsptr));
  672. END;
  673. wsptr := @workspace[DCTSIZE*pred(DCTSIZE)];
  674. for row := pred(DCTSIZE) downto 0 do
  675. BEGIN
  676. { Odd part }
  677. tmp5 := DCTELEM(wsptr^[1]);
  678. tmp7 := DCTELEM(wsptr^[3]);
  679. { even part }
  680. {noop:
  681. tmp0 := DCTELEM(wsptr^[0]);
  682. wsptr^[0] := DCTELEM(tmp0);}
  683. {tmp2 := DCTELEM(wsptr^[4]);}
  684. wsptr^[1] := wsptr^[4];
  685. tmp1 := DCTELEM(wsptr^[2]);
  686. tmp3 := DCTELEM(wsptr^[6]);
  687. wsptr^[2] := DCTELEM(tmp1 - tmp3);
  688. wsptr^[3] := DCTELEM(tmp1 + tmp3);
  689. { Odd part }
  690. tmp4 := DCTELEM(wsptr^[5]);
  691. tmp6 := DCTELEM(wsptr^[7]);
  692. z13 := tmp4 + tmp7;
  693. wsptr^[4] := DCTELEM(tmp4 - tmp7);
  694. z11 := tmp5 + tmp6;
  695. wsptr^[6] := DCTELEM(tmp5 - tmp6);
  696. wsptr^[7] := DCTELEM(z11 + z13);
  697. wsptr^[5] := DCTELEM(z11 - z13);
  698. Dec(int_ptr(wsptr), DCTSIZE); { advance pointer to previous row }
  699. END;
  700. { M x M tensor }
  701. wsptr := @workspace[DCTSIZE*0];
  702. for row := 0 to pred(DCTSIZE) do
  703. begin
  704. Case row of
  705. 0,1,3,7: { M1 }
  706. begin
  707. wsptr^[2] := Multiply(wsptr^[2], FP_IC4); { 2/c4 }
  708. wsptr^[5] := Multiply(wsptr^[5], FP_IC4); { 2/c4 }
  709. N1(wsptr^[ 4], wsptr^[ 6]);
  710. end;
  711. 2,5: { M2 }
  712. begin
  713. wsptr^[0] := Multiply(wsptr^[0], FP_IC4);
  714. wsptr^[1] := Multiply(wsptr^[1], FP_IC4);
  715. wsptr^[3] := Multiply(wsptr^[3], FP_IC4);
  716. wsptr^[7] := Multiply(wsptr^[7], FP_IC4);
  717. wsptr^[2] := wsptr^[2] * 2; { shift }
  718. wsptr^[5] := wsptr^[5] * 2;
  719. N2(wsptr^[4], wsptr^[6]);
  720. end;
  721. end; { Case }
  722. Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
  723. end;
  724. { M x N tensor }
  725. { rows 4,6 }
  726. begin
  727. N1(workspace[DCTSIZE*4+0], workspace[DCTSIZE*6+0]);
  728. N1(workspace[DCTSIZE*4+1], workspace[DCTSIZE*6+1]);
  729. N1(workspace[DCTSIZE*4+3], workspace[DCTSIZE*6+3]);
  730. N1(workspace[DCTSIZE*4+7], workspace[DCTSIZE*6+7]);
  731. N2(workspace[DCTSIZE*4+2], workspace[DCTSIZE*6+2]);
  732. N2(workspace[DCTSIZE*4+5], workspace[DCTSIZE*6+5]);
  733. { N3 }
  734. tmp0 := workspace[DCTSIZE*4,4];
  735. tmp1 := workspace[DCTSIZE*6,4];
  736. tmp2 := workspace[DCTSIZE*4,6];
  737. tmp3 := workspace[DCTSIZE*6,6];
  738. { two inverse matrices => same as FDCT }
  739. z10 := tmp0 - tmp3;
  740. z11 := tmp1 + tmp2;
  741. z12 := tmp0 + tmp3;
  742. z13 := tmp1 - tmp2;
  743. tmp0 := Multiply(z10 + z11, FP_I_C4_2);
  744. tmp1 := Multiply(z10 - z11, FP_I_C4_2);
  745. tmp2 := z12 * 2; { shifts }
  746. tmp3 := z13 * (-2);
  747. workspace[DCTSIZE*4,4] := tmp2 + tmp0;
  748. workspace[DCTSIZE*6,4] := tmp1 + tmp3;
  749. workspace[DCTSIZE*4,6] := tmp1 - tmp3;
  750. workspace[DCTSIZE*6,6] := tmp2 - tmp0;
  751. end;
  752. { R2 x R2 }
  753. wsptr := @workspace;
  754. for row := 0 to pred(DCTSIZE) do
  755. BEGIN
  756. { even part }
  757. tmp0 := wsptr^[0];
  758. tmp2 := wsptr^[1];
  759. tmp1 := wsptr^[2];
  760. tmp3 := wsptr^[3];
  761. tmp10 := tmp0 + tmp2;
  762. tmp11 := tmp0 - tmp2;
  763. tmp12 := tmp1 - tmp3;
  764. tmp13 := tmp3;
  765. tmp0 := tmp10 + tmp13;
  766. tmp3 := tmp10 - tmp13;
  767. tmp2 := tmp11 + tmp12;
  768. tmp1 := tmp11 - tmp12;
  769. { Odd part }
  770. tmp4 := wsptr^[4];
  771. tmp5 := wsptr^[5];
  772. tmp6 := wsptr^[6];
  773. tmp7 := wsptr^[7];
  774. tmp6 := tmp6 - tmp7;
  775. tmp5 := tmp5 - tmp6;
  776. tmp4 :=-tmp4 - tmp5;
  777. wsptr^[0] := (tmp0 + tmp7);
  778. wsptr^[7] := (tmp0 - tmp7);
  779. wsptr^[1] := (tmp2 + tmp6);
  780. wsptr^[6] := (tmp2 - tmp6);
  781. wsptr^[2] := (tmp1 + tmp5);
  782. wsptr^[5] := (tmp1 - tmp5);
  783. wsptr^[3] := (tmp3 + tmp4);
  784. wsptr^[4] := (tmp3 - tmp4);
  785. Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
  786. END;
  787. wsptr := @workspace;
  788. for ctr := 0 to pred(DCTSIZE) do
  789. BEGIN
  790. outptr := JSAMPROW(@output_buf^[ctr]^[output_col]);
  791. { even part }
  792. tmp0 := wsptr[0];
  793. tmp1 := wsptr[1];
  794. tmp2 := wsptr[2];
  795. tmp3 := wsptr[3];
  796. tmp10 := tmp0 + tmp1;
  797. tmp11 := tmp0 - tmp1;
  798. tmp13 := tmp3;
  799. tmp12 := tmp2 - tmp3;
  800. tmp0 := tmp10 + tmp13;
  801. tmp3 := tmp10 - tmp13;
  802. tmp1 := tmp11 + tmp12;
  803. tmp2 := tmp11 - tmp12;
  804. { Odd part }
  805. tmp4 := wsptr[4];
  806. tmp5 := wsptr[5];
  807. tmp6 := wsptr[6];
  808. tmp7 := wsptr[7];
  809. tmp6 := tmp6 - tmp7;
  810. tmp5 := tmp5 - tmp6;
  811. tmp4 :=-tmp4 - tmp5;
  812. { Final output stage: scale down by a factor of 8 and range-limit }
  813. outptr^[0] := range_limit^[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
  814. and RANGE_MASK];
  815. outptr^[7] := range_limit^[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
  816. and RANGE_MASK];
  817. outptr^[1] := range_limit^[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
  818. and RANGE_MASK];
  819. outptr^[6] := range_limit^[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
  820. and RANGE_MASK];
  821. outptr^[2] := range_limit^[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
  822. and RANGE_MASK];
  823. outptr^[5] := range_limit^[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
  824. and RANGE_MASK];
  825. outptr^[4] := range_limit^[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
  826. and RANGE_MASK];
  827. outptr^[3] := range_limit^[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
  828. and RANGE_MASK];
  829. Inc(int_ptr(wsptr));
  830. END;
  831. End; {----------------------------------------}
  832. {----------------------------------------------------------------------}