jidctflt.pas 11 KB

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