jidct2d.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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.