jcdctmgr.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. Unit JcDCTmgr;
  2. { Original : jcdctmgr.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  3. { This file is part of the Independent JPEG Group's software.
  4. For conditions of distribution and use, see the accompanying README file.
  5. This file contains the forward-DCT management logic.
  6. This code selects a particular DCT implementation to be used,
  7. and it performs related housekeeping chores including coefficient
  8. quantization. }
  9. interface
  10. {$N+}
  11. {$I jconfig.inc}
  12. uses
  13. jmorecfg,
  14. jinclude,
  15. jdeferr,
  16. jerror,
  17. jpeglib,
  18. jdct, { Private declarations for DCT subsystem }
  19. jfdctint, jfdctfst, jfdctflt;
  20. { Initialize FDCT manager. }
  21. {GLOBAL}
  22. procedure jinit_forward_dct (cinfo : j_compress_ptr);
  23. implementation
  24. { Private subobject for this module }
  25. type
  26. my_fdct_ptr = ^my_fdct_controller;
  27. my_fdct_controller = record
  28. pub : jpeg_forward_dct; { public fields }
  29. { Pointer to the DCT routine actually in use }
  30. do_dct : forward_DCT_method_ptr;
  31. { The actual post-DCT divisors --- not identical to the quant table
  32. entries, because of scaling (especially for an unnormalized DCT).
  33. Each table is given in normal array order. }
  34. divisors : array[0..NUM_QUANT_TBLS-1] of DCTELEM_FIELD_PTR;
  35. {$ifdef DCT_FLOAT_SUPPORTED}
  36. { Same as above for the floating-point case. }
  37. do_float_dct : float_DCT_method_ptr;
  38. float_divisors : array[0..NUM_QUANT_TBLS-1] of FAST_FLOAT_FIELD_PTR;
  39. {$endif}
  40. end;
  41. { Initialize for a processing pass.
  42. Verify that all referenced Q-tables are present, and set up
  43. the divisor table for each one.
  44. In the current implementation, DCT of all components is done during
  45. the first pass, even if only some components will be output in the
  46. first scan. Hence all components should be examined here. }
  47. {METHODDEF}
  48. procedure start_pass_fdctmgr (cinfo : j_compress_ptr); far;
  49. var
  50. fdct : my_fdct_ptr;
  51. ci, qtblno, i : int;
  52. compptr : jpeg_component_info_ptr;
  53. qtbl : JQUANT_TBL_PTR;
  54. dtbl : DCTELEM_FIELD_PTR;
  55. {$ifdef DCT_IFAST_SUPPORTED}
  56. const
  57. CONST_BITS = 14;
  58. aanscales : array[0..DCTSIZE2-1] of INT16 =
  59. ({ precomputed values scaled up by 14 bits }
  60. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  61. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  62. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  63. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  64. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  65. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  66. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  67. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247);
  68. {SHIFT_TEMPS}
  69. { Descale and correctly round an INT32 value that's scaled by N bits.
  70. We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  71. the fudge factor is correct for either sign of X. }
  72. function DESCALE(x : INT32; n : int) : INT32;
  73. var
  74. shift_temp : INT32;
  75. begin
  76. shift_temp := x + (INT32(1) shl (n-1));
  77. {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
  78. if shift_temp < 0 then
  79. Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
  80. else
  81. {$endif}
  82. Descale := (shift_temp shr n);
  83. end;
  84. {$endif}
  85. {$ifdef DCT_FLOAT_SUPPORTED}
  86. var
  87. fdtbl : FAST_FLOAT_FIELD_PTR;
  88. row, col : int;
  89. const
  90. aanscalefactor : array[0..DCTSIZE-1] of double =
  91. (1.0, 1.387039845, 1.306562965, 1.175875602,
  92. 1.0, 0.785694958, 0.541196100, 0.275899379);
  93. {$endif}
  94. begin
  95. fdct := my_fdct_ptr (cinfo^.fdct);
  96. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  97. for ci := 0 to pred(cinfo^.num_components) do
  98. begin
  99. qtblno := compptr^.quant_tbl_no;
  100. { Make sure specified quantization table is present }
  101. if (qtblno < 0) or (qtblno >= NUM_QUANT_TBLS) or
  102. (cinfo^.quant_tbl_ptrs[qtblno] = NIL) then
  103. ERREXIT1(j_common_ptr(cinfo), JERR_NO_QUANT_TABLE, qtblno);
  104. qtbl := cinfo^.quant_tbl_ptrs[qtblno];
  105. { Compute divisors for this quant table }
  106. { We may do this more than once for same table, but it's not a big deal }
  107. case (cinfo^.dct_method) of
  108. {$ifdef DCT_ISLOW_SUPPORTED}
  109. JDCT_ISLOW:
  110. begin
  111. { For LL&M IDCT method, divisors are equal to raw quantization
  112. coefficients multiplied by 8 (to counteract scaling). }
  113. if (fdct^.divisors[qtblno] = NIL) then
  114. begin
  115. fdct^.divisors[qtblno] := DCTELEM_FIELD_PTR(
  116. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  117. DCTSIZE2 * SIZEOF(DCTELEM)) );
  118. end;
  119. dtbl := fdct^.divisors[qtblno];
  120. for i := 0 to pred(DCTSIZE2) do
  121. begin
  122. dtbl^[i] := (DCTELEM(qtbl^.quantval[i])) shl 3;
  123. end;
  124. end;
  125. {$endif}
  126. {$ifdef DCT_IFAST_SUPPORTED}
  127. JDCT_IFAST:
  128. begin
  129. { For AA&N IDCT method, divisors are equal to quantization
  130. coefficients scaled by scalefactor[row]*scalefactor[col], where
  131. scalefactor[0] := 1
  132. scalefactor[k] := cos(k*PI/16) * sqrt(2) for k=1..7
  133. We apply a further scale factor of 8. }
  134. if (fdct^.divisors[qtblno] = NIL) then
  135. begin
  136. fdct^.divisors[qtblno] := DCTELEM_FIELD_PTR(
  137. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  138. DCTSIZE2 * SIZEOF(DCTELEM)) );
  139. end;
  140. dtbl := fdct^.divisors[qtblno];
  141. for i := 0 to pred(DCTSIZE2) do
  142. begin
  143. dtbl^[i] := DCTELEM(
  144. {MULTIPLY16V16}
  145. DESCALE( INT32(qtbl^.quantval[i]) * INT32 (aanscales[i]),
  146. CONST_BITS-3) );
  147. end;
  148. end;
  149. {$endif}
  150. {$ifdef DCT_FLOAT_SUPPORTED}
  151. JDCT_FLOAT:
  152. begin
  153. { For float AA&N IDCT method, divisors are equal to quantization
  154. coefficients scaled by scalefactor[row]*scalefactor[col], where
  155. scalefactor[0] := 1
  156. scalefactor[k] := cos(k*PI/16) * sqrt(2) for k=1..7
  157. We apply a further scale factor of 8.
  158. What's actually stored is 1/divisor so that the inner loop can
  159. use a multiplication rather than a division. }
  160. if (fdct^.float_divisors[qtblno] = NIL) then
  161. begin
  162. fdct^.float_divisors[qtblno] := FAST_FLOAT_FIELD_PTR(
  163. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  164. DCTSIZE2 * SIZEOF(FAST_FLOAT)) );
  165. end;
  166. fdtbl := fdct^.float_divisors[qtblno];
  167. i := 0;
  168. for row := 0 to pred(DCTSIZE) do
  169. begin
  170. for col := 0 to pred(DCTSIZE) do
  171. begin
  172. fdtbl^[i] := {FAST_FLOAT}
  173. (1.0 / (( {double}(qtbl^.quantval[i]) *
  174. aanscalefactor[row] * aanscalefactor[col] * 8.0)));
  175. Inc(i);
  176. end;
  177. end;
  178. end;
  179. {$endif}
  180. else
  181. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  182. end;
  183. Inc(compptr);
  184. end;
  185. end;
  186. { Perform forward DCT on one or more blocks of a component.
  187. The input samples are taken from the sample_data[] array starting at
  188. position start_row/start_col, and moving to the right for any additional
  189. blocks. The quantized coefficients are returned in coef_blocks[]. }
  190. {METHODDEF}
  191. procedure forward_DCT (cinfo : j_compress_ptr;
  192. compptr : jpeg_component_info_ptr;
  193. sample_data : JSAMPARRAY;
  194. coef_blocks : JBLOCKROW;
  195. start_row : JDIMENSION;
  196. start_col : JDIMENSION;
  197. num_blocks : JDIMENSION); far;
  198. { This version is used for integer DCT implementations. }
  199. var
  200. { This routine is heavily used, so it's worth coding it tightly. }
  201. fdct : my_fdct_ptr;
  202. do_dct : forward_DCT_method_ptr;
  203. divisors : DCTELEM_FIELD_PTR;
  204. workspace : array[0..DCTSIZE2-1] of DCTELEM; { work area for FDCT subroutine }
  205. bi : JDIMENSION;
  206. var
  207. {register} workspaceptr : DCTELEMPTR;
  208. {register} elemptr : JSAMPLE_PTR;
  209. {register} elemr : int;
  210. {$ifndef DCTSIZE_IS_8}
  211. var
  212. {register} elemc : int;
  213. {$endif}
  214. var
  215. {register} temp, qval : DCTELEM;
  216. {register} i : int;
  217. {register} output_ptr : JCOEFPTR;
  218. begin
  219. fdct := my_fdct_ptr (cinfo^.fdct);
  220. do_dct := fdct^.do_dct;
  221. divisors := fdct^.divisors[compptr^.quant_tbl_no];
  222. Inc(JSAMPROW_PTR(sample_data), start_row); { fold in the vertical offset once }
  223. for bi := 0 to pred(num_blocks) do
  224. begin
  225. { Load data into workspace, applying unsigned->signed conversion }
  226. workspaceptr := @workspace[0];
  227. for elemr := 0 to pred(DCTSIZE) do
  228. begin
  229. elemptr := @sample_data^[elemr]^[start_col];
  230. {$ifdef DCTSIZE_IS_8} { unroll the inner loop }
  231. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  232. Inc(workspaceptr);
  233. Inc(elemptr);
  234. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  235. Inc(workspaceptr);
  236. Inc(elemptr);
  237. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  238. Inc(workspaceptr);
  239. Inc(elemptr);
  240. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  241. Inc(workspaceptr);
  242. Inc(elemptr);
  243. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  244. Inc(workspaceptr);
  245. Inc(elemptr);
  246. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  247. Inc(workspaceptr);
  248. Inc(elemptr);
  249. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  250. Inc(workspaceptr);
  251. Inc(elemptr);
  252. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  253. Inc(workspaceptr);
  254. {Inc(elemptr); - Value never used }
  255. {$else}
  256. for elemc := pred(DCTSIZE) downto 0 do
  257. begin
  258. workspaceptr^ := GETJSAMPLE(elemptr^) - CENTERJSAMPLE;
  259. Inc(workspaceptr);
  260. Inc(elemptr);
  261. end;
  262. {$endif}
  263. end;
  264. { Perform the DCT }
  265. do_dct (workspace);
  266. { Quantize/descale the coefficients, and store into coef_blocks[] }
  267. output_ptr := JCOEFPTR(@coef_blocks^[bi]);
  268. for i := 0 to pred(DCTSIZE2) do
  269. begin
  270. qval := divisors^[i];
  271. temp := workspace[i];
  272. { Divide the coefficient value by qval, ensuring proper rounding.
  273. Since C does not specify the direction of rounding for negative
  274. quotients, we have to force the dividend positive for portability.
  275. In most files, at least half of the output values will be zero
  276. (at default quantization settings, more like three-quarters...)
  277. so we should ensure that this case is fast. On many machines,
  278. a comparison is enough cheaper than a divide to make a special test
  279. a win. Since both inputs will be nonnegative, we need only test
  280. for a < b to discover whether a/b is 0.
  281. If your machine's division is fast enough, define FAST_DIVIDE. }
  282. if (temp < 0) then
  283. begin
  284. temp := -temp;
  285. Inc(temp, qval shr 1); { for rounding }
  286. {DIVIDE_BY(temp, qval);}
  287. {$ifdef FAST_DIVIDE}
  288. temp := temp div qval;
  289. {$else}
  290. if (temp >= qval) then
  291. temp := temp div qval
  292. else
  293. temp := 0;
  294. {$endif}
  295. temp := -temp;
  296. end
  297. else
  298. begin
  299. Inc(temp, qval shr 1); { for rounding }
  300. {DIVIDE_BY(temp, qval);}
  301. {$ifdef FAST_DIVIDE}
  302. temp := temp div qval;
  303. {$else}
  304. if (temp >= qval) then
  305. temp := temp div qval
  306. else
  307. temp := 0;
  308. {$endif}
  309. end;
  310. output_ptr^[i] := JCOEF (temp);
  311. end;
  312. Inc(start_col, DCTSIZE);
  313. end;
  314. end;
  315. {$ifdef DCT_FLOAT_SUPPORTED}
  316. {METHODDEF}
  317. procedure forward_DCT_float (cinfo : j_compress_ptr;
  318. compptr : jpeg_component_info_ptr;
  319. sample_data : JSAMPARRAY;
  320. coef_blocks : JBLOCKROW;
  321. start_row : JDIMENSION;
  322. start_col : JDIMENSION;
  323. num_blocks : JDIMENSION); far;
  324. { This version is used for floating-point DCT implementations. }
  325. var
  326. { This routine is heavily used, so it's worth coding it tightly. }
  327. fdct : my_fdct_ptr;
  328. do_dct : float_DCT_method_ptr;
  329. divisors : FAST_FLOAT_FIELD_PTR;
  330. workspace : array[0..DCTSIZE2-1] of FAST_FLOAT; { work area for FDCT subroutine }
  331. bi : JDIMENSION;
  332. var
  333. {register} workspaceptr : FAST_FLOAT_PTR;
  334. {register} elemptr : JSAMPLE_PTR;
  335. {register} elemr : int;
  336. {$ifndef DCTSIZE_IS_8}
  337. var
  338. {register} elemc : int;
  339. {$endif}
  340. var
  341. {register} temp : FAST_FLOAT;
  342. {register} i : int;
  343. {register} output_ptr : JCOEFPTR;
  344. begin
  345. fdct := my_fdct_ptr (cinfo^.fdct);
  346. do_dct := fdct^.do_float_dct;
  347. divisors := fdct^.float_divisors[compptr^.quant_tbl_no];
  348. Inc(JSAMPROW_PTR(sample_data), start_row); { fold in the vertical offset once }
  349. for bi := 0 to pred(num_blocks) do
  350. begin
  351. { Load data into workspace, applying unsigned->signed conversion }
  352. workspaceptr := @workspace[0];
  353. for elemr := 0 to pred(DCTSIZE) do
  354. begin
  355. elemptr := @(sample_data^[elemr]^[start_col]);
  356. {$ifdef DCTSIZE_IS_8} { unroll the inner loop }
  357. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  358. Inc(workspaceptr);
  359. Inc(elemptr);
  360. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  361. Inc(workspaceptr);
  362. Inc(elemptr);
  363. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  364. Inc(workspaceptr);
  365. Inc(elemptr);
  366. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  367. Inc(workspaceptr);
  368. Inc(elemptr);
  369. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  370. Inc(workspaceptr);
  371. Inc(elemptr);
  372. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  373. Inc(workspaceptr);
  374. Inc(elemptr);
  375. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  376. Inc(workspaceptr);
  377. Inc(elemptr);
  378. workspaceptr^ := {FAST_FLOAT}(GETJSAMPLE(elemptr^) - CENTERJSAMPLE);
  379. Inc(workspaceptr);
  380. {Inc(elemptr); - value never used }
  381. {$else}
  382. for elemc := pred(DCTSIZE) downto 0 do
  383. begin
  384. workspaceptr^ := {FAST_FLOAT}(
  385. (GETJSAMPLE(elemptr^) - CENTERJSAMPLE) );
  386. Inc(workspaceptr);
  387. Inc(elemptr);
  388. end;
  389. {$endif}
  390. end;
  391. { Perform the DCT }
  392. do_dct (workspace);
  393. { Quantize/descale the coefficients, and store into coef_blocks[] }
  394. output_ptr := JCOEFPTR(@(coef_blocks^[bi]));
  395. for i := 0 to pred(DCTSIZE2) do
  396. begin
  397. { Apply the quantization and scaling factor }
  398. temp := workspace[i] * divisors^[i];
  399. { Round to nearest integer.
  400. Since C does not specify the direction of rounding for negative
  401. quotients, we have to force the dividend positive for portability.
  402. The maximum coefficient size is +-16K (for 12-bit data), so this
  403. code should work for either 16-bit or 32-bit ints. }
  404. output_ptr^[i] := JCOEF ( int(Trunc (temp + {FAST_FLOAT}(16384.5))) - 16384);
  405. end;
  406. Inc(start_col, DCTSIZE);
  407. end;
  408. end;
  409. {$endif} { DCT_FLOAT_SUPPORTED }
  410. { Initialize FDCT manager. }
  411. {GLOBAL}
  412. procedure jinit_forward_dct (cinfo : j_compress_ptr);
  413. var
  414. fdct : my_fdct_ptr;
  415. i : int;
  416. begin
  417. fdct := my_fdct_ptr(
  418. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  419. SIZEOF(my_fdct_controller)) );
  420. cinfo^.fdct := jpeg_forward_dct_ptr (fdct);
  421. fdct^.pub.start_pass := start_pass_fdctmgr;
  422. case (cinfo^.dct_method) of
  423. {$ifdef DCT_ISLOW_SUPPORTED}
  424. JDCT_ISLOW:
  425. begin
  426. fdct^.pub.forward_DCT := forward_DCT;
  427. fdct^.do_dct := jpeg_fdct_islow;
  428. end;
  429. {$endif}
  430. {$ifdef DCT_IFAST_SUPPORTED}
  431. JDCT_IFAST:
  432. begin
  433. fdct^.pub.forward_DCT := forward_DCT;
  434. fdct^.do_dct := jpeg_fdct_ifast;
  435. end;
  436. {$endif}
  437. {$ifdef DCT_FLOAT_SUPPORTED}
  438. JDCT_FLOAT:
  439. begin
  440. fdct^.pub.forward_DCT := forward_DCT_float;
  441. fdct^.do_float_dct := jpeg_fdct_float;
  442. end;
  443. {$endif}
  444. else
  445. ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
  446. end;
  447. { Mark divisor tables unallocated }
  448. for i := 0 to pred(NUM_QUANT_TBLS) do
  449. begin
  450. fdct^.divisors[i] := NIL;
  451. {$ifdef DCT_FLOAT_SUPPORTED}
  452. fdct^.float_divisors[i] := NIL;
  453. {$endif}
  454. end;
  455. end;
  456. end.