jidctint.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. {$IFNDEF FPC_DOTTEDUNITS}
  2. Unit JIDctInt;
  3. {$ENDIF FPC_DOTTEDUNITS}
  4. {$Q+}
  5. { This file contains a slow-but-accurate integer 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. A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  9. on each row (or vice versa, but it's more convenient to emit a row at
  10. a time). Direct algorithms are also available, but they are much more
  11. complex and seem not to be any faster when reduced to code.
  12. This implementation is based on an algorithm described in
  13. C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
  14. Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
  15. Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
  16. The primary algorithm described there uses 11 multiplies and 29 adds.
  17. We use their alternate method with 12 multiplies and 32 adds.
  18. The advantage of this method is that no data path contains more than one
  19. multiplication; this allows a very simple and accurate implementation in
  20. scaled fixed-point arithmetic, with a minimal number of shifts. }
  21. { Original : jidctint.c ; Copyright (C) 1991-1998, Thomas G. Lane. }
  22. interface
  23. {$I jconfig.inc}
  24. {$IFDEF FPC_DOTTEDUNITS}
  25. uses
  26. System.Jpeg.Jmorecfg,
  27. System.Jpeg.Jinclude,
  28. System.Jpeg.Jpeglib,
  29. System.Jpeg.Jdct; { Private declarations for DCT subsystem }
  30. {$ELSE FPC_DOTTEDUNITS}
  31. uses
  32. jmorecfg,
  33. jinclude,
  34. jpeglib,
  35. jdct; { Private declarations for DCT subsystem }
  36. {$ENDIF FPC_DOTTEDUNITS}
  37. { Perform dequantization and inverse DCT on one block of coefficients. }
  38. {GLOBAL}
  39. procedure jpeg_idct_islow (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. { The poop on this scaling stuff is as follows:
  50. Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
  51. larger than the true IDCT outputs. The final outputs are therefore
  52. a factor of N larger than desired; since N=8 this can be cured by
  53. a simple right shift at the end of the algorithm. The advantage of
  54. this arrangement is that we save two multiplications per 1-D IDCT,
  55. because the y0 and y4 inputs need not be divided by sqrt(N).
  56. We have to do addition and subtraction of the integer inputs, which
  57. is no problem, and multiplication by fractional constants, which is
  58. a problem to do in integer arithmetic. We multiply all the constants
  59. by CONST_SCALE and convert them to integer constants (thus retaining
  60. CONST_BITS bits of precision in the constants). After doing a
  61. multiplication we have to divide the product by CONST_SCALE, with proper
  62. rounding, to produce the correct output. This division can be done
  63. cheaply as a right shift of CONST_BITS bits. We postpone shifting
  64. as long as possible so that partial sums can be added together with
  65. full fractional precision.
  66. The outputs of the first pass are scaled up by PASS1_BITS bits so that
  67. they are represented to better-than-integral precision. These outputs
  68. require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
  69. with the recommended scaling. (To scale up 12-bit sample data further, an
  70. intermediate INT32 array would be needed.)
  71. To avoid overflow of the 32-bit intermediate results in pass 2, we must
  72. have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
  73. shows that the values given below are the most effective. }
  74. {$ifdef BITS_IN_JSAMPLE_IS_8}
  75. const
  76. CONST_BITS = 13;
  77. PASS1_BITS = 2;
  78. {$else}
  79. const
  80. CONST_BITS = 13;
  81. PASS1_BITS = 1; { lose a little precision to avoid overflow }
  82. {$endif}
  83. const
  84. CONST_SCALE = (INT32(1) shl CONST_BITS);
  85. const
  86. FIX_0_298631336 = INT32(Round(CONST_SCALE * 0.298631336)); {2446}
  87. FIX_0_390180644 = INT32(Round(CONST_SCALE * 0.390180644)); {3196}
  88. FIX_0_541196100 = INT32(Round(CONST_SCALE * 0.541196100)); {4433}
  89. FIX_0_765366865 = INT32(Round(CONST_SCALE * 0.765366865)); {6270}
  90. FIX_0_899976223 = INT32(Round(CONST_SCALE * 0.899976223)); {7373}
  91. FIX_1_175875602 = INT32(Round(CONST_SCALE * 1.175875602)); {9633}
  92. FIX_1_501321110 = INT32(Round(CONST_SCALE * 1.501321110)); {12299}
  93. FIX_1_847759065 = INT32(Round(CONST_SCALE * 1.847759065)); {15137}
  94. FIX_1_961570560 = INT32(Round(CONST_SCALE * 1.961570560)); {16069}
  95. FIX_2_053119869 = INT32(Round(CONST_SCALE * 2.053119869)); {16819}
  96. FIX_2_562915447 = INT32(Round(CONST_SCALE * 2.562915447)); {20995}
  97. FIX_3_072711026 = INT32(Round(CONST_SCALE * 3.072711026)); {25172}
  98. { Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  99. For 8-bit samples with the recommended scaling, all the variable
  100. and constant values involved are no more than 16 bits wide, so a
  101. 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  102. For 12-bit samples, a full 32-bit multiplication will be needed. }
  103. {$ifdef BITS_IN_JSAMPLE_IS_8}
  104. {$IFDEF BASM16}
  105. {$IFNDEF WIN32}
  106. {MULTIPLY16C16(var,const)}
  107. function Multiply(X, Y: Integer): integer; assembler;
  108. asm
  109. mov ax, X
  110. imul Y
  111. mov al, ah
  112. mov ah, dl
  113. end;
  114. {$ENDIF}
  115. {$ENDIF}
  116. function Multiply(X, Y: INT32): INT32;
  117. begin
  118. Multiply := INT32(X) * INT32(Y);
  119. end;
  120. {$else}
  121. {#define MULTIPLY(var,const) ((var) * (const))}
  122. function Multiply(X, Y: INT32): INT32;
  123. begin
  124. Multiply := INT32(X) * INT32(Y);
  125. end;
  126. {$endif}
  127. { Dequantize a coefficient by multiplying it by the multiplier-table
  128. entry; produce an int result. In this module, both inputs and result
  129. are 16 bits or less, so either int or short multiply will work. }
  130. function DEQUANTIZE(coef,quantval : int) : int;
  131. begin
  132. Dequantize := ( ISLOW_MULT_TYPE(coef) * quantval);
  133. end;
  134. { Descale and correctly round an INT32 value that's scaled by N bits.
  135. We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  136. the fudge factor is correct for either sign of X. }
  137. function DESCALE(x : INT32; n : int) : INT32;
  138. var
  139. shift_temp : INT32;
  140. begin
  141. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  142. shift_temp := x + (INT32(1) shl (n-1));
  143. if shift_temp < 0 then
  144. Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
  145. else
  146. Descale := (shift_temp shr n);
  147. {$else}
  148. Descale := (x + (INT32(1) shl (n-1)) shr n;
  149. {$endif}
  150. end;
  151. { Perform dequantization and inverse DCT on one block of coefficients. }
  152. {GLOBAL}
  153. procedure jpeg_idct_islow (cinfo : j_decompress_ptr;
  154. compptr : jpeg_component_info_ptr;
  155. coef_block : JCOEFPTR;
  156. output_buf : JSAMPARRAY;
  157. output_col : JDIMENSION);
  158. type
  159. PWorkspace = ^TWorkspace;
  160. TWorkspace = coef_bits_field; { buffers data between passes }
  161. var
  162. tmp0, tmp1, tmp2, tmp3 : INT32;
  163. tmp10, tmp11, tmp12, tmp13 : INT32;
  164. z1, z2, z3, z4, z5 : INT32;
  165. inptr : JCOEFPTR;
  166. quantptr : ISLOW_MULT_TYPE_FIELD_PTR;
  167. wsptr : PWorkspace;
  168. outptr : JSAMPROW;
  169. range_limit : JSAMPROW;
  170. ctr : int;
  171. workspace : TWorkspace;
  172. {SHIFT_TEMPS}
  173. var
  174. dcval : int;
  175. var
  176. dcval_ : JSAMPLE;
  177. begin
  178. { Each IDCT routine is responsible for range-limiting its results and
  179. converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
  180. be quite far out of range if the input data is corrupt, so a bulletproof
  181. range-limiting step is required. We use a mask-and-table-lookup method
  182. to do the combined operations quickly. See the comments with
  183. prepare_range_limit_table (in jdmaster.c) for more info. }
  184. range_limit := JSAMPROW(@(cinfo^.sample_range_limit^[CENTERJSAMPLE]));
  185. { Pass 1: process columns from input, store into work array. }
  186. { Note results are scaled up by sqrt(8) compared to a true IDCT; }
  187. { furthermore, we scale the results by 2**PASS1_BITS. }
  188. inptr := coef_block;
  189. quantptr := ISLOW_MULT_TYPE_FIELD_PTR (compptr^.dct_table);
  190. wsptr := PWorkspace(@workspace);
  191. for ctr := pred(DCTSIZE) downto 0 do
  192. begin
  193. { Due to quantization, we will usually find that many of the input
  194. coefficients are zero, especially the AC terms. We can exploit this
  195. by short-circuiting the IDCT calculation for any column in which all
  196. the AC terms are zero. In that case each output is equal to the
  197. DC coefficient (with scale factor as needed).
  198. With typical images and quantization tables, half or more of the
  199. column DCT calculations can be simplified this way. }
  200. if ((inptr^[DCTSIZE*1]=0) and (inptr^[DCTSIZE*2]=0) and
  201. (inptr^[DCTSIZE*3]=0) and (inptr^[DCTSIZE*4]=0) and
  202. (inptr^[DCTSIZE*5]=0) and (inptr^[DCTSIZE*6]=0) and
  203. (inptr^[DCTSIZE*7]=0)) then
  204. begin
  205. { AC terms all zero }
  206. dcval := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]) shl PASS1_BITS;
  207. wsptr^[DCTSIZE*0] := dcval;
  208. wsptr^[DCTSIZE*1] := dcval;
  209. wsptr^[DCTSIZE*2] := dcval;
  210. wsptr^[DCTSIZE*3] := dcval;
  211. wsptr^[DCTSIZE*4] := dcval;
  212. wsptr^[DCTSIZE*5] := dcval;
  213. wsptr^[DCTSIZE*6] := dcval;
  214. wsptr^[DCTSIZE*7] := dcval;
  215. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  216. Inc(ISLOW_MULT_TYPE_PTR(quantptr));
  217. Inc(int_ptr(wsptr));
  218. continue;
  219. end;
  220. { Even part: reverse the even part of the forward DCT. }
  221. { The rotator is sqrt(2)*c(-6). }
  222. z2 := DEQUANTIZE(inptr^[DCTSIZE*2], quantptr^[DCTSIZE*2]);
  223. z3 := DEQUANTIZE(inptr^[DCTSIZE*6], quantptr^[DCTSIZE*6]);
  224. z1 := MULTIPLY(z2 + z3, FIX_0_541196100);
  225. tmp2 := z1 + MULTIPLY(z3, - FIX_1_847759065);
  226. tmp3 := z1 + MULTIPLY(z2, FIX_0_765366865);
  227. z2 := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]);
  228. z3 := DEQUANTIZE(inptr^[DCTSIZE*4], quantptr^[DCTSIZE*4]);
  229. tmp0 := (z2 + z3) shl CONST_BITS;
  230. tmp1 := (z2 - z3) shl CONST_BITS;
  231. tmp10 := tmp0 + tmp3;
  232. tmp13 := tmp0 - tmp3;
  233. tmp11 := tmp1 + tmp2;
  234. tmp12 := tmp1 - tmp2;
  235. { Odd part per figure 8; the matrix is unitary and hence its
  236. transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. }
  237. tmp0 := DEQUANTIZE(inptr^[DCTSIZE*7], quantptr^[DCTSIZE*7]);
  238. tmp1 := DEQUANTIZE(inptr^[DCTSIZE*5], quantptr^[DCTSIZE*5]);
  239. tmp2 := DEQUANTIZE(inptr^[DCTSIZE*3], quantptr^[DCTSIZE*3]);
  240. tmp3 := DEQUANTIZE(inptr^[DCTSIZE*1], quantptr^[DCTSIZE*1]);
  241. z1 := tmp0 + tmp3;
  242. z2 := tmp1 + tmp2;
  243. z3 := tmp0 + tmp2;
  244. z4 := tmp1 + tmp3;
  245. z5 := MULTIPLY(z3 + z4, FIX_1_175875602); { sqrt(2) * c3 }
  246. tmp0 := MULTIPLY(tmp0, FIX_0_298631336); { sqrt(2) * (-c1+c3+c5-c7) }
  247. tmp1 := MULTIPLY(tmp1, FIX_2_053119869); { sqrt(2) * ( c1+c3-c5+c7) }
  248. tmp2 := MULTIPLY(tmp2, FIX_3_072711026); { sqrt(2) * ( c1+c3+c5-c7) }
  249. tmp3 := MULTIPLY(tmp3, FIX_1_501321110); { sqrt(2) * ( c1+c3-c5-c7) }
  250. z1 := MULTIPLY(z1, - FIX_0_899976223); { sqrt(2) * (c7-c3) }
  251. z2 := MULTIPLY(z2, - FIX_2_562915447); { sqrt(2) * (-c1-c3) }
  252. z3 := MULTIPLY(z3, - FIX_1_961570560); { sqrt(2) * (-c3-c5) }
  253. z4 := MULTIPLY(z4, - FIX_0_390180644); { sqrt(2) * (c5-c3) }
  254. Inc(z3, z5);
  255. Inc(z4, z5);
  256. Inc(tmp0, z1 + z3);
  257. Inc(tmp1, z2 + z4);
  258. Inc(tmp2, z2 + z3);
  259. Inc(tmp3, z1 + z4);
  260. { Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 }
  261. wsptr^[DCTSIZE*0] := int (DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS));
  262. wsptr^[DCTSIZE*7] := int (DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS));
  263. wsptr^[DCTSIZE*1] := int (DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS));
  264. wsptr^[DCTSIZE*6] := int (DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS));
  265. wsptr^[DCTSIZE*2] := int (DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS));
  266. wsptr^[DCTSIZE*5] := int (DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS));
  267. wsptr^[DCTSIZE*3] := int (DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS));
  268. wsptr^[DCTSIZE*4] := int (DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS));
  269. Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
  270. Inc(ISLOW_MULT_TYPE_PTR(quantptr));
  271. Inc(int_ptr(wsptr));
  272. end;
  273. { Pass 2: process rows from work array, store into output array. }
  274. { Note that we must descale the results by a factor of 8 == 2**3, }
  275. { and also undo the PASS1_BITS scaling. }
  276. wsptr := @workspace;
  277. for ctr := 0 to pred(DCTSIZE) do
  278. begin
  279. outptr := output_buf^[ctr];
  280. Inc(JSAMPLE_PTR(outptr), output_col);
  281. { Rows of zeroes can be exploited in the same way as we did with columns.
  282. However, the column calculation has created many nonzero AC terms, so
  283. the simplification applies less often (typically 5% to 10% of the time).
  284. On machines with very fast multiplication, it's possible that the
  285. test takes more time than it's worth. In that case this section
  286. may be commented out. }
  287. {$ifndef NO_ZERO_ROW_TEST}
  288. if ((wsptr^[1]=0) and (wsptr^[2]=0) and (wsptr^[3]=0) and (wsptr^[4]=0)
  289. and (wsptr^[5]=0) and (wsptr^[6]=0) and (wsptr^[7]=0)) then
  290. begin
  291. { AC terms all zero }
  292. JSAMPLE(dcval_) := range_limit^[int(DESCALE(INT32(wsptr^[0]),
  293. PASS1_BITS+3)) and RANGE_MASK];
  294. outptr^[0] := dcval_;
  295. outptr^[1] := dcval_;
  296. outptr^[2] := dcval_;
  297. outptr^[3] := dcval_;
  298. outptr^[4] := dcval_;
  299. outptr^[5] := dcval_;
  300. outptr^[6] := dcval_;
  301. outptr^[7] := dcval_;
  302. Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
  303. continue;
  304. end;
  305. {$endif}
  306. { Even part: reverse the even part of the forward DCT. }
  307. { The rotator is sqrt(2)*c(-6). }
  308. z2 := INT32 (wsptr^[2]);
  309. z3 := INT32 (wsptr^[6]);
  310. z1 := MULTIPLY(z2 + z3, FIX_0_541196100);
  311. tmp2 := z1 + MULTIPLY(z3, - FIX_1_847759065);
  312. tmp3 := z1 + MULTIPLY(z2, FIX_0_765366865);
  313. tmp0 := (INT32(wsptr^[0]) + INT32(wsptr^[4])) shl CONST_BITS;
  314. tmp1 := (INT32(wsptr^[0]) - INT32(wsptr^[4])) shl CONST_BITS;
  315. tmp10 := tmp0 + tmp3;
  316. tmp13 := tmp0 - tmp3;
  317. tmp11 := tmp1 + tmp2;
  318. tmp12 := tmp1 - tmp2;
  319. { Odd part per figure 8; the matrix is unitary and hence its
  320. transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. }
  321. tmp0 := INT32(wsptr^[7]);
  322. tmp1 := INT32(wsptr^[5]);
  323. tmp2 := INT32(wsptr^[3]);
  324. tmp3 := INT32(wsptr^[1]);
  325. z1 := tmp0 + tmp3;
  326. z2 := tmp1 + tmp2;
  327. z3 := tmp0 + tmp2;
  328. z4 := tmp1 + tmp3;
  329. z5 := MULTIPLY(z3 + z4, FIX_1_175875602); { sqrt(2) * c3 }
  330. tmp0 := MULTIPLY(tmp0, FIX_0_298631336); { sqrt(2) * (-c1+c3+c5-c7) }
  331. tmp1 := MULTIPLY(tmp1, FIX_2_053119869); { sqrt(2) * ( c1+c3-c5+c7) }
  332. tmp2 := MULTIPLY(tmp2, FIX_3_072711026); { sqrt(2) * ( c1+c3+c5-c7) }
  333. tmp3 := MULTIPLY(tmp3, FIX_1_501321110); { sqrt(2) * ( c1+c3-c5-c7) }
  334. z1 := MULTIPLY(z1, - FIX_0_899976223); { sqrt(2) * (c7-c3) }
  335. z2 := MULTIPLY(z2, - FIX_2_562915447); { sqrt(2) * (-c1-c3) }
  336. z3 := MULTIPLY(z3, - FIX_1_961570560); { sqrt(2) * (-c3-c5) }
  337. z4 := MULTIPLY(z4, - FIX_0_390180644); { sqrt(2) * (c5-c3) }
  338. Inc(z3, z5);
  339. Inc(z4, z5);
  340. Inc(tmp0, z1 + z3);
  341. Inc(tmp1, z2 + z4);
  342. Inc(tmp2, z2 + z3);
  343. Inc(tmp3, z1 + z4);
  344. { Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 }
  345. outptr^[0] := range_limit^[ int(DESCALE(tmp10 + tmp3,
  346. CONST_BITS+PASS1_BITS+3))
  347. and RANGE_MASK];
  348. outptr^[7] := range_limit^[ int(DESCALE(tmp10 - tmp3,
  349. CONST_BITS+PASS1_BITS+3))
  350. and RANGE_MASK];
  351. outptr^[1] := range_limit^[ int(DESCALE(tmp11 + tmp2,
  352. CONST_BITS+PASS1_BITS+3))
  353. and RANGE_MASK];
  354. outptr^[6] := range_limit^[ int(DESCALE(tmp11 - tmp2,
  355. CONST_BITS+PASS1_BITS+3))
  356. and RANGE_MASK];
  357. outptr^[2] := range_limit^[ int(DESCALE(tmp12 + tmp1,
  358. CONST_BITS+PASS1_BITS+3))
  359. and RANGE_MASK];
  360. outptr^[5] := range_limit^[ int(DESCALE(tmp12 - tmp1,
  361. CONST_BITS+PASS1_BITS+3))
  362. and RANGE_MASK];
  363. outptr^[3] := range_limit^[ int(DESCALE(tmp13 + tmp0,
  364. CONST_BITS+PASS1_BITS+3))
  365. and RANGE_MASK];
  366. outptr^[4] := range_limit^[ int(DESCALE(tmp13 - tmp0,
  367. CONST_BITS+PASS1_BITS+3))
  368. and RANGE_MASK];
  369. Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
  370. end;
  371. end;
  372. end.