jidctflt.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. Unit JIDctFlt;
  2. {$N+}
  3. { This file contains a floating-point implementation of the
  4. inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
  5. must also perform dequantization of the input coefficients.
  6. This implementation should be more accurate than either of the integer
  7. IDCT implementations. However, it may not give the same results on all
  8. machines because of differences in roundoff behavior. Speed will depend
  9. on the hardware's floating point capacity.
  10. A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  11. on each row (or vice versa, but it's more convenient to emit a row at
  12. a time). Direct algorithms are also available, but they are much more
  13. complex and seem not to be any faster when reduced to code.
  14. This implementation is based on Arai, Agui, and Nakajima's algorithm for
  15. scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
  16. Japanese, but the algorithm is described in the Pennebaker & Mitchell
  17. JPEG textbook (see REFERENCES section in file README). The following code
  18. is based directly on figure 4-8 in P&M.
  19. While an 8-point DCT cannot be done in less than 11 multiplies, it is
  20. possible to arrange the computation so that many of the multiplies are
  21. simple scalings of the final outputs. These multiplies can then be
  22. folded into the multiplications or divisions by the JPEG quantization
  23. table entries. The AA&N method leaves only 5 multiplies and 29 adds
  24. to be done in the DCT itself.
  25. The primary disadvantage of this method is that with a fixed-point
  26. implementation, accuracy is lost due to imprecise representation of the
  27. scaled quantization values. However, that problem does not arise if
  28. we use floating point arithmetic. }
  29. { Original: jidctflt.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  30. interface
  31. {$I jconfig.inc}
  32. uses
  33. jmorecfg,
  34. jinclude,
  35. jpeglib,
  36. jdct; { Private declarations for DCT subsystem }
  37. { Perform dequantization and inverse DCT on one block of coefficients. }
  38. {GLOBAL}
  39. procedure jpeg_idct_float (cinfo : j_decompress_ptr;
  40. compptr : jpeg_component_info_ptr;
  41. coef_block : JCOEFPTR;
  42. output_buf : JSAMPARRAY;
  43. output_col : JDIMENSION);
  44. implementation
  45. { This module is specialized to the case DCTSIZE = 8. }
  46. {$ifndef DCTSIZE_IS_8}
  47. Sorry, this code only copes with 8x8 DCTs. { deliberate syntax err }
  48. {$endif}
  49. { Dequantize a coefficient by multiplying it by the multiplier-table
  50. entry; produce a float result. }
  51. function DEQUANTIZE(coef : int; quantval : FAST_FLOAT) : FAST_FLOAT;
  52. begin
  53. Dequantize := ( (coef) * quantval);
  54. end;
  55. { Descale and correctly round an INT32 value that's scaled by N bits.
  56. We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  57. the fudge factor is correct for either sign of X. }
  58. function DESCALE(x : INT32; n : int) : INT32;
  59. var
  60. shift_temp : INT32;
  61. begin
  62. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  63. shift_temp := x + (INT32(1) shl (n-1));
  64. if shift_temp < 0 then
  65. Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
  66. else
  67. Descale := (shift_temp shr n);
  68. {$else}
  69. Descale := (x + (INT32(1) shl (n-1)) shr n;
  70. {$endif}
  71. end;
  72. { Perform dequantization and inverse DCT on one block of coefficients. }
  73. {GLOBAL}
  74. procedure jpeg_idct_float (cinfo : j_decompress_ptr;
  75. compptr : jpeg_component_info_ptr;
  76. coef_block : JCOEFPTR;
  77. output_buf : JSAMPARRAY;
  78. output_col : JDIMENSION);
  79. type
  80. PWorkspace = ^TWorkspace;
  81. TWorkspace = array[0..DCTSIZE2-1] of FAST_FLOAT;
  82. var
  83. tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7 : FAST_FLOAT;
  84. tmp10, tmp11, tmp12, tmp13 : FAST_FLOAT;
  85. z5, z10, z11, z12, z13 : FAST_FLOAT;
  86. inptr : JCOEFPTR;
  87. quantptr : FLOAT_MULT_TYPE_FIELD_PTR;
  88. wsptr : PWorkSpace;
  89. outptr : JSAMPROW;
  90. range_limit : JSAMPROW;
  91. ctr : int;
  92. workspace : TWorkspace; { buffers data between passes }
  93. {SHIFT_TEMPS}
  94. var
  95. dcval : FAST_FLOAT;
  96. begin
  97. { Each IDCT routine is responsible for range-limiting its results and
  98. converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
  99. be quite far out of range if the input data is corrupt, so a bulletproof
  100. range-limiting step is required. We use a mask-and-table-lookup method
  101. to do the combined operations quickly. See the comments with
  102. prepare_range_limit_table (in jdmaster.c) for more info. }
  103. range_limit := JSAMPROW(@(cinfo^.sample_range_limit^[CENTERJSAMPLE]));
  104. { Pass 1: process columns from input, store into work array. }
  105. inptr := coef_block;
  106. quantptr := FLOAT_MULT_TYPE_FIELD_PTR (compptr^.dct_table);
  107. wsptr := @workspace;
  108. for ctr := pred(DCTSIZE) downto 0 do
  109. begin
  110. { Due to quantization, we will usually find that many of the input
  111. coefficients are zero, especially the AC terms. We can exploit this
  112. by short-circuiting the IDCT calculation for any column in which all
  113. the AC terms are zero. In that case each output is equal to the
  114. DC coefficient (with scale factor as needed).
  115. With typical images and quantization tables, half or more of the
  116. column DCT calculations can be simplified this way. }
  117. if (inptr^[DCTSIZE*1]=0) and (inptr^[DCTSIZE*2]=0) and
  118. (inptr^[DCTSIZE*3]=0) and (inptr^[DCTSIZE*4]=0) and
  119. (inptr^[DCTSIZE*5]=0) and (inptr^[DCTSIZE*6]=0) and
  120. (inptr^[DCTSIZE*7]=0) then
  121. begin
  122. { AC terms all zero }
  123. FAST_FLOAT(dcval) := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]);
  124. wsptr^[DCTSIZE*0] := dcval;
  125. wsptr^[DCTSIZE*1] := dcval;
  126. wsptr^[DCTSIZE*2] := dcval;
  127. wsptr^[DCTSIZE*3] := dcval;
  128. wsptr^[DCTSIZE*4] := dcval;
  129. wsptr^[DCTSIZE*5] := dcval;
  130. wsptr^[DCTSIZE*6] := dcval;
  131. wsptr^[DCTSIZE*7] := dcval;
  132. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  133. Inc(FLOAT_MULT_TYPE_PTR(quantptr));
  134. Inc(FAST_FLOAT_PTR(wsptr));
  135. continue;
  136. end;
  137. { Even part }
  138. tmp0 := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]);
  139. tmp1 := DEQUANTIZE(inptr^[DCTSIZE*2], quantptr^[DCTSIZE*2]);
  140. tmp2 := DEQUANTIZE(inptr^[DCTSIZE*4], quantptr^[DCTSIZE*4]);
  141. tmp3 := DEQUANTIZE(inptr^[DCTSIZE*6], quantptr^[DCTSIZE*6]);
  142. tmp10 := tmp0 + tmp2; { phase 3 }
  143. tmp11 := tmp0 - tmp2;
  144. tmp13 := tmp1 + tmp3; { phases 5-3 }
  145. tmp12 := (tmp1 - tmp3) * ({FAST_FLOAT}(1.414213562)) - tmp13; { 2*c4 }
  146. tmp0 := tmp10 + tmp13; { phase 2 }
  147. tmp3 := tmp10 - tmp13;
  148. tmp1 := tmp11 + tmp12;
  149. tmp2 := tmp11 - tmp12;
  150. { Odd part }
  151. tmp4 := DEQUANTIZE(inptr^[DCTSIZE*1], quantptr^[DCTSIZE*1]);
  152. tmp5 := DEQUANTIZE(inptr^[DCTSIZE*3], quantptr^[DCTSIZE*3]);
  153. tmp6 := DEQUANTIZE(inptr^[DCTSIZE*5], quantptr^[DCTSIZE*5]);
  154. tmp7 := DEQUANTIZE(inptr^[DCTSIZE*7], quantptr^[DCTSIZE*7]);
  155. z13 := tmp6 + tmp5; { phase 6 }
  156. z10 := tmp6 - tmp5;
  157. z11 := tmp4 + tmp7;
  158. z12 := tmp4 - tmp7;
  159. tmp7 := z11 + z13; { phase 5 }
  160. tmp11 := (z11 - z13) * ({FAST_FLOAT}(1.414213562)); { 2*c4 }
  161. z5 := (z10 + z12) * ({FAST_FLOAT}(1.847759065)); { 2*c2 }
  162. tmp10 := ({FAST_FLOAT}(1.082392200)) * z12 - z5; { 2*(c2-c6) }
  163. tmp12 := ({FAST_FLOAT}(-2.613125930)) * z10 + z5; { -2*(c2+c6) }
  164. tmp6 := tmp12 - tmp7; { phase 2 }
  165. tmp5 := tmp11 - tmp6;
  166. tmp4 := tmp10 + tmp5;
  167. wsptr^[DCTSIZE*0] := tmp0 + tmp7;
  168. wsptr^[DCTSIZE*7] := tmp0 - tmp7;
  169. wsptr^[DCTSIZE*1] := tmp1 + tmp6;
  170. wsptr^[DCTSIZE*6] := tmp1 - tmp6;
  171. wsptr^[DCTSIZE*2] := tmp2 + tmp5;
  172. wsptr^[DCTSIZE*5] := tmp2 - tmp5;
  173. wsptr^[DCTSIZE*4] := tmp3 + tmp4;
  174. wsptr^[DCTSIZE*3] := tmp3 - tmp4;
  175. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  176. Inc(FLOAT_MULT_TYPE_PTR(quantptr));
  177. Inc(FAST_FLOAT_PTR(wsptr));
  178. end;
  179. { Pass 2: process rows from work array, store into output array. }
  180. { Note that we must descale the results by a factor of 8 = 2**3. }
  181. wsptr := @workspace;
  182. for ctr := 0 to pred(DCTSIZE) do
  183. begin
  184. outptr := JSAMPROW(@(output_buf^[ctr]^[output_col]));
  185. { Rows of zeroes can be exploited in the same way as we did with columns.
  186. However, the column calculation has created many nonzero AC terms, so
  187. the simplification applies less often (typically 5% to 10% of the time).
  188. And testing floats for zero is relatively expensive, so we don't bother. }
  189. { Even part }
  190. tmp10 := wsptr^[0] + wsptr^[4];
  191. tmp11 := wsptr^[0] - wsptr^[4];
  192. tmp13 := wsptr^[2] + wsptr^[6];
  193. tmp12 := (wsptr^[2] - wsptr^[6]) * ({FAST_FLOAT}(1.414213562)) - tmp13;
  194. tmp0 := tmp10 + tmp13;
  195. tmp3 := tmp10 - tmp13;
  196. tmp1 := tmp11 + tmp12;
  197. tmp2 := tmp11 - tmp12;
  198. { Odd part }
  199. z13 := wsptr^[5] + wsptr^[3];
  200. z10 := wsptr^[5] - wsptr^[3];
  201. z11 := wsptr^[1] + wsptr^[7];
  202. z12 := wsptr^[1] - wsptr^[7];
  203. tmp7 := z11 + z13;
  204. tmp11 := (z11 - z13) * ({FAST_FLOAT}(1.414213562));
  205. z5 := (z10 + z12) * ({FAST_FLOAT}(1.847759065)); { 2*c2 }
  206. tmp10 := ({FAST_FLOAT}(1.082392200)) * z12 - z5; { 2*(c2-c6) }
  207. tmp12 := ({FAST_FLOAT}(-2.613125930)) * z10 + z5; { -2*(c2+c6) }
  208. tmp6 := tmp12 - tmp7;
  209. tmp5 := tmp11 - tmp6;
  210. tmp4 := tmp10 + tmp5;
  211. { Final output stage: scale down by a factor of 8 and range-limit }
  212. outptr^[0] := range_limit^[ int(DESCALE( INT32(Round((tmp0 + tmp7))), 3))
  213. and RANGE_MASK];
  214. outptr^[7] := range_limit^[ int(DESCALE( INT32(Round((tmp0 - tmp7))), 3))
  215. and RANGE_MASK];
  216. outptr^[1] := range_limit^[ int(DESCALE( INT32(Round((tmp1 + tmp6))), 3))
  217. and RANGE_MASK];
  218. outptr^[6] := range_limit^[ int(DESCALE( INT32(Round((tmp1 - tmp6))), 3))
  219. and RANGE_MASK];
  220. outptr^[2] := range_limit^[ int(DESCALE( INT32(Round((tmp2 + tmp5))), 3))
  221. and RANGE_MASK];
  222. outptr^[5] := range_limit^[ int(DESCALE( INT32(Round((tmp2 - tmp5))), 3))
  223. and RANGE_MASK];
  224. outptr^[4] := range_limit^[ int(DESCALE( INT32(Round((tmp3 + tmp4))), 3))
  225. and RANGE_MASK];
  226. outptr^[3] := range_limit^[ int(DESCALE( INT32(Round((tmp3 - tmp4))), 3))
  227. and RANGE_MASK];
  228. Inc(FAST_FLOAT_PTR(wsptr), DCTSIZE); { advance pointer to next row }
  229. end;
  230. end;
  231. end.