Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

737 lines
25KB

  1. /*
  2. * jdcoefct.c
  3. *
  4. * Copyright (C) 1994-1997, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains the coefficient buffer controller for decompression.
  9. * This controller is the top level of the JPEG decompressor proper.
  10. * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
  11. *
  12. * In buffered-image mode, this controller is the interface between
  13. * input-oriented processing and output-oriented processing.
  14. * Also, the input side (only) is used when reading a file for transcoding.
  15. */
  16. #define JPEG_INTERNALS
  17. #include "jinclude.h"
  18. #include "jpeglib.h"
  19. /* Block smoothing is only applicable for progressive JPEG, so: */
  20. #ifndef D_PROGRESSIVE_SUPPORTED
  21. #undef BLOCK_SMOOTHING_SUPPORTED
  22. #endif
  23. /* Private buffer controller object */
  24. typedef struct {
  25. struct jpeg_d_coef_controller pub; /* public fields */
  26. /* These variables keep track of the current location of the input side. */
  27. /* cinfo->input_iMCU_row is also used for this. */
  28. JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
  29. int MCU_vert_offset; /* counts MCU rows within iMCU row */
  30. int MCU_rows_per_iMCU_row; /* number of such rows needed */
  31. /* The output side's location is represented by cinfo->output_iMCU_row. */
  32. /* In single-pass modes, it's sufficient to buffer just one MCU.
  33. * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
  34. * and let the entropy decoder write into that workspace each time.
  35. * (On 80x86, the workspace is FAR even though it's not really very big;
  36. * this is to keep the module interfaces unchanged when a large coefficient
  37. * buffer is necessary.)
  38. * In multi-pass modes, this array points to the current MCU's blocks
  39. * within the virtual arrays; it is used only by the input side.
  40. */
  41. JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
  42. #ifdef D_MULTISCAN_FILES_SUPPORTED
  43. /* In multi-pass modes, we need a virtual block array for each component. */
  44. jvirt_barray_ptr whole_image[MAX_COMPONENTS];
  45. #endif
  46. #ifdef BLOCK_SMOOTHING_SUPPORTED
  47. /* When doing block smoothing, we latch coefficient Al values here */
  48. int * coef_bits_latch;
  49. #define SAVED_COEFS 6 /* we save coef_bits[0..5] */
  50. #endif
  51. } my_coef_controller3;
  52. typedef my_coef_controller3 * my_coef_ptr3;
  53. /* Forward declarations */
  54. METHODDEF(int) decompress_onepass
  55. JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
  56. #ifdef D_MULTISCAN_FILES_SUPPORTED
  57. METHODDEF(int) decompress_data
  58. JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
  59. #endif
  60. #ifdef BLOCK_SMOOTHING_SUPPORTED
  61. LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
  62. METHODDEF(int) decompress_smooth_data
  63. JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
  64. #endif
  65. LOCAL(void)
  66. start_iMCU_row3 (j_decompress_ptr cinfo)
  67. /* Reset within-iMCU-row counters for a new row (input side) */
  68. {
  69. my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  70. /* In an interleaved scan, an MCU row is the same as an iMCU row.
  71. * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  72. * But at the bottom of the image, process only what's left.
  73. */
  74. if (cinfo->comps_in_scan > 1) {
  75. coef->MCU_rows_per_iMCU_row = 1;
  76. } else {
  77. if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
  78. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  79. else
  80. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  81. }
  82. coef->MCU_ctr = 0;
  83. coef->MCU_vert_offset = 0;
  84. }
  85. /*
  86. * Initialize for an input processing pass.
  87. */
  88. METHODDEF(void)
  89. start_input_pass (j_decompress_ptr cinfo)
  90. {
  91. cinfo->input_iMCU_row = 0;
  92. start_iMCU_row3(cinfo);
  93. }
  94. /*
  95. * Initialize for an output processing pass.
  96. */
  97. METHODDEF(void)
  98. start_output_pass (j_decompress_ptr cinfo)
  99. {
  100. #ifdef BLOCK_SMOOTHING_SUPPORTED
  101. my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  102. /* If multipass, check to see whether to use block smoothing on this pass */
  103. if (coef->pub.coef_arrays != NULL) {
  104. if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
  105. coef->pub.decompress_data = decompress_smooth_data;
  106. else
  107. coef->pub.decompress_data = decompress_data;
  108. }
  109. #endif
  110. cinfo->output_iMCU_row = 0;
  111. }
  112. /*
  113. * Decompress and return some data in the single-pass case.
  114. * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  115. * Input and output must run in lockstep since we have only a one-MCU buffer.
  116. * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  117. *
  118. * NB: output_buf contains a plane for each component in image,
  119. * which we index according to the component's SOF position.
  120. */
  121. METHODDEF(int)
  122. decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  123. {
  124. my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  125. JDIMENSION MCU_col_num; /* index of current MCU within row */
  126. JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  127. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  128. int blkn, ci, xindex, yindex, yoffset, useful_width;
  129. JSAMPARRAY output_ptr;
  130. JDIMENSION start_col, output_col;
  131. jpeg_component_info *compptr;
  132. inverse_DCT_method_ptr inverse_DCT;
  133. /* Loop to process as much as one whole iMCU row */
  134. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  135. yoffset++) {
  136. for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
  137. MCU_col_num++) {
  138. /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
  139. jzero_far((void FAR *) coef->MCU_buffer[0],
  140. (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
  141. if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  142. /* Suspension forced; update state counters and exit */
  143. coef->MCU_vert_offset = yoffset;
  144. coef->MCU_ctr = MCU_col_num;
  145. return JPEG_SUSPENDED;
  146. }
  147. /* Determine where data should go in output_buf and do the IDCT thing.
  148. * We skip dummy blocks at the right and bottom edges (but blkn gets
  149. * incremented past them!). Note the inner loop relies on having
  150. * allocated the MCU_buffer[] blocks sequentially.
  151. */
  152. blkn = 0; /* index of current DCT block within MCU */
  153. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  154. compptr = cinfo->cur_comp_info[ci];
  155. /* Don't bother to IDCT an uninteresting component. */
  156. if (! compptr->component_needed) {
  157. blkn += compptr->MCU_blocks;
  158. continue;
  159. }
  160. inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
  161. useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  162. : compptr->last_col_width;
  163. output_ptr = output_buf[compptr->component_index] +
  164. yoffset * compptr->DCT_scaled_size;
  165. start_col = MCU_col_num * compptr->MCU_sample_width;
  166. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  167. if (cinfo->input_iMCU_row < last_iMCU_row ||
  168. yoffset+yindex < compptr->last_row_height) {
  169. output_col = start_col;
  170. for (xindex = 0; xindex < useful_width; xindex++) {
  171. (*inverse_DCT) (cinfo, compptr,
  172. (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
  173. output_ptr, output_col);
  174. output_col += compptr->DCT_scaled_size;
  175. }
  176. }
  177. blkn += compptr->MCU_width;
  178. output_ptr += compptr->DCT_scaled_size;
  179. }
  180. }
  181. }
  182. /* Completed an MCU row, but perhaps not an iMCU row */
  183. coef->MCU_ctr = 0;
  184. }
  185. /* Completed the iMCU row, advance counters for next one */
  186. cinfo->output_iMCU_row++;
  187. if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
  188. start_iMCU_row3(cinfo);
  189. return JPEG_ROW_COMPLETED;
  190. }
  191. /* Completed the scan */
  192. (*cinfo->inputctl->finish_input_pass) (cinfo);
  193. return JPEG_SCAN_COMPLETED;
  194. }
  195. /*
  196. * Dummy consume-input routine for single-pass operation.
  197. */
  198. METHODDEF(int)
  199. dummy_consume_data (j_decompress_ptr)
  200. {
  201. return JPEG_SUSPENDED; /* Always indicate nothing was done */
  202. }
  203. #ifdef D_MULTISCAN_FILES_SUPPORTED
  204. /*
  205. * Consume input data and store it in the full-image coefficient buffer.
  206. * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
  207. * ie, v_samp_factor block rows for each component in the scan.
  208. * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  209. */
  210. METHODDEF(int)
  211. consume_data (j_decompress_ptr cinfo)
  212. {
  213. my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  214. JDIMENSION MCU_col_num; /* index of current MCU within row */
  215. int blkn, ci, xindex, yindex, yoffset;
  216. JDIMENSION start_col;
  217. JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  218. JBLOCKROW buffer_ptr;
  219. jpeg_component_info *compptr;
  220. /* Align the virtual buffers for the components used in this scan. */
  221. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  222. compptr = cinfo->cur_comp_info[ci];
  223. buffer[ci] = (*cinfo->mem->access_virt_barray)
  224. ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  225. cinfo->input_iMCU_row * compptr->v_samp_factor,
  226. (JDIMENSION) compptr->v_samp_factor, TRUE);
  227. /* Note: entropy decoder expects buffer to be zeroed,
  228. * but this is handled automatically by the memory manager
  229. * because we requested a pre-zeroed array.
  230. */
  231. }
  232. /* Loop to process one whole iMCU row */
  233. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  234. yoffset++) {
  235. for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
  236. MCU_col_num++) {
  237. /* Construct list of pointers to DCT blocks belonging to this MCU */
  238. blkn = 0; /* index of current DCT block within MCU */
  239. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  240. compptr = cinfo->cur_comp_info[ci];
  241. start_col = MCU_col_num * compptr->MCU_width;
  242. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  243. buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  244. for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  245. coef->MCU_buffer[blkn++] = buffer_ptr++;
  246. }
  247. }
  248. }
  249. /* Try to fetch the MCU. */
  250. if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  251. /* Suspension forced; update state counters and exit */
  252. coef->MCU_vert_offset = yoffset;
  253. coef->MCU_ctr = MCU_col_num;
  254. return JPEG_SUSPENDED;
  255. }
  256. }
  257. /* Completed an MCU row, but perhaps not an iMCU row */
  258. coef->MCU_ctr = 0;
  259. }
  260. /* Completed the iMCU row, advance counters for next one */
  261. if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
  262. start_iMCU_row3(cinfo);
  263. return JPEG_ROW_COMPLETED;
  264. }
  265. /* Completed the scan */
  266. (*cinfo->inputctl->finish_input_pass) (cinfo);
  267. return JPEG_SCAN_COMPLETED;
  268. }
  269. /*
  270. * Decompress and return some data in the multi-pass case.
  271. * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  272. * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  273. *
  274. * NB: output_buf contains a plane for each component in image.
  275. */
  276. METHODDEF(int)
  277. decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  278. {
  279. my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  280. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  281. JDIMENSION block_num;
  282. int ci, block_row, block_rows;
  283. JBLOCKARRAY buffer;
  284. JBLOCKROW buffer_ptr;
  285. JSAMPARRAY output_ptr;
  286. JDIMENSION output_col;
  287. jpeg_component_info *compptr;
  288. inverse_DCT_method_ptr inverse_DCT;
  289. /* Force some input to be done if we are getting ahead of the input. */
  290. while (cinfo->input_scan_number < cinfo->output_scan_number ||
  291. (cinfo->input_scan_number == cinfo->output_scan_number &&
  292. cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
  293. if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
  294. return JPEG_SUSPENDED;
  295. }
  296. /* OK, output from the virtual arrays. */
  297. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  298. ci++, compptr++) {
  299. /* Don't bother to IDCT an uninteresting component. */
  300. if (! compptr->component_needed)
  301. continue;
  302. /* Align the virtual buffer for this component. */
  303. buffer = (*cinfo->mem->access_virt_barray)
  304. ((j_common_ptr) cinfo, coef->whole_image[ci],
  305. cinfo->output_iMCU_row * compptr->v_samp_factor,
  306. (JDIMENSION) compptr->v_samp_factor, FALSE);
  307. /* Count non-dummy DCT block rows in this iMCU row. */
  308. if (cinfo->output_iMCU_row < last_iMCU_row)
  309. block_rows = compptr->v_samp_factor;
  310. else {
  311. /* NB: can't use last_row_height here; it is input-side-dependent! */
  312. block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  313. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  314. }
  315. inverse_DCT = cinfo->idct->inverse_DCT[ci];
  316. output_ptr = output_buf[ci];
  317. /* Loop over all DCT blocks to be processed. */
  318. for (block_row = 0; block_row < block_rows; block_row++) {
  319. buffer_ptr = buffer[block_row];
  320. output_col = 0;
  321. for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
  322. (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
  323. output_ptr, output_col);
  324. buffer_ptr++;
  325. output_col += compptr->DCT_scaled_size;
  326. }
  327. output_ptr += compptr->DCT_scaled_size;
  328. }
  329. }
  330. if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  331. return JPEG_ROW_COMPLETED;
  332. return JPEG_SCAN_COMPLETED;
  333. }
  334. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  335. #ifdef BLOCK_SMOOTHING_SUPPORTED
  336. /*
  337. * This code applies interblock smoothing as described by section K.8
  338. * of the JPEG standard: the first 5 AC coefficients are estimated from
  339. * the DC values of a DCT block and its 8 neighboring blocks.
  340. * We apply smoothing only for progressive JPEG decoding, and only if
  341. * the coefficients it can estimate are not yet known to full precision.
  342. */
  343. /* Natural-order array positions of the first 5 zigzag-order coefficients */
  344. #define Q01_POS 1
  345. #define Q10_POS 8
  346. #define Q20_POS 16
  347. #define Q11_POS 9
  348. #define Q02_POS 2
  349. /*
  350. * Determine whether block smoothing is applicable and safe.
  351. * We also latch the current states of the coef_bits[] entries for the
  352. * AC coefficients; otherwise, if the input side of the decompressor
  353. * advances into a new scan, we might think the coefficients are known
  354. * more accurately than they really are.
  355. */
  356. LOCAL(boolean)
  357. smoothing_ok (j_decompress_ptr cinfo)
  358. {
  359. my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  360. boolean smoothing_useful = FALSE;
  361. int ci, coefi;
  362. jpeg_component_info *compptr;
  363. JQUANT_TBL * qtable;
  364. int * coef_bits;
  365. int * coef_bits_latch;
  366. if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
  367. return FALSE;
  368. /* Allocate latch area if not already done */
  369. if (coef->coef_bits_latch == NULL)
  370. coef->coef_bits_latch = (int *)
  371. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  372. cinfo->num_components *
  373. (SAVED_COEFS * SIZEOF(int)));
  374. coef_bits_latch = coef->coef_bits_latch;
  375. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  376. ci++, compptr++) {
  377. /* All components' quantization values must already be latched. */
  378. if ((qtable = compptr->quant_table) == NULL)
  379. return FALSE;
  380. /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
  381. if (qtable->quantval[0] == 0 ||
  382. qtable->quantval[Q01_POS] == 0 ||
  383. qtable->quantval[Q10_POS] == 0 ||
  384. qtable->quantval[Q20_POS] == 0 ||
  385. qtable->quantval[Q11_POS] == 0 ||
  386. qtable->quantval[Q02_POS] == 0)
  387. return FALSE;
  388. /* DC values must be at least partly known for all components. */
  389. coef_bits = cinfo->coef_bits[ci];
  390. if (coef_bits[0] < 0)
  391. return FALSE;
  392. /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
  393. for (coefi = 1; coefi <= 5; coefi++) {
  394. coef_bits_latch[coefi] = coef_bits[coefi];
  395. if (coef_bits[coefi] != 0)
  396. smoothing_useful = TRUE;
  397. }
  398. coef_bits_latch += SAVED_COEFS;
  399. }
  400. return smoothing_useful;
  401. }
  402. /*
  403. * Variant of decompress_data for use when doing block smoothing.
  404. */
  405. METHODDEF(int)
  406. decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  407. {
  408. my_coef_ptr3 coef = (my_coef_ptr3) cinfo->coef;
  409. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  410. JDIMENSION block_num, last_block_column;
  411. int ci, block_row, block_rows, access_rows;
  412. JBLOCKARRAY buffer;
  413. JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
  414. JSAMPARRAY output_ptr;
  415. JDIMENSION output_col;
  416. jpeg_component_info *compptr;
  417. inverse_DCT_method_ptr inverse_DCT;
  418. boolean first_row, last_row;
  419. JBLOCK workspace;
  420. int *coef_bits;
  421. JQUANT_TBL *quanttbl;
  422. INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
  423. int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
  424. int Al, pred;
  425. /* Force some input to be done if we are getting ahead of the input. */
  426. while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  427. ! cinfo->inputctl->eoi_reached) {
  428. if (cinfo->input_scan_number == cinfo->output_scan_number) {
  429. /* If input is working on current scan, we ordinarily want it to
  430. * have completed the current row. But if input scan is DC,
  431. * we want it to keep one row ahead so that next block row's DC
  432. * values are up to date.
  433. */
  434. JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
  435. if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
  436. break;
  437. }
  438. if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
  439. return JPEG_SUSPENDED;
  440. }
  441. /* OK, output from the virtual arrays. */
  442. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  443. ci++, compptr++) {
  444. /* Don't bother to IDCT an uninteresting component. */
  445. if (! compptr->component_needed)
  446. continue;
  447. /* Count non-dummy DCT block rows in this iMCU row. */
  448. if (cinfo->output_iMCU_row < last_iMCU_row) {
  449. block_rows = compptr->v_samp_factor;
  450. access_rows = block_rows * 2; /* this and next iMCU row */
  451. last_row = FALSE;
  452. } else {
  453. /* NB: can't use last_row_height here; it is input-side-dependent! */
  454. block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  455. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  456. access_rows = block_rows; /* this iMCU row only */
  457. last_row = TRUE;
  458. }
  459. /* Align the virtual buffer for this component. */
  460. if (cinfo->output_iMCU_row > 0) {
  461. access_rows += compptr->v_samp_factor; /* prior iMCU row too */
  462. buffer = (*cinfo->mem->access_virt_barray)
  463. ((j_common_ptr) cinfo, coef->whole_image[ci],
  464. (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
  465. (JDIMENSION) access_rows, FALSE);
  466. buffer += compptr->v_samp_factor; /* point to current iMCU row */
  467. first_row = FALSE;
  468. } else {
  469. buffer = (*cinfo->mem->access_virt_barray)
  470. ((j_common_ptr) cinfo, coef->whole_image[ci],
  471. (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
  472. first_row = TRUE;
  473. }
  474. /* Fetch component-dependent info */
  475. coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
  476. quanttbl = compptr->quant_table;
  477. Q00 = quanttbl->quantval[0];
  478. Q01 = quanttbl->quantval[Q01_POS];
  479. Q10 = quanttbl->quantval[Q10_POS];
  480. Q20 = quanttbl->quantval[Q20_POS];
  481. Q11 = quanttbl->quantval[Q11_POS];
  482. Q02 = quanttbl->quantval[Q02_POS];
  483. inverse_DCT = cinfo->idct->inverse_DCT[ci];
  484. output_ptr = output_buf[ci];
  485. /* Loop over all DCT blocks to be processed. */
  486. for (block_row = 0; block_row < block_rows; block_row++) {
  487. buffer_ptr = buffer[block_row];
  488. if (first_row && block_row == 0)
  489. prev_block_row = buffer_ptr;
  490. else
  491. prev_block_row = buffer[block_row-1];
  492. if (last_row && block_row == block_rows-1)
  493. next_block_row = buffer_ptr;
  494. else
  495. next_block_row = buffer[block_row+1];
  496. /* We fetch the surrounding DC values using a sliding-register approach.
  497. * Initialize all nine here so as to do the right thing on narrow pics.
  498. */
  499. DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
  500. DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
  501. DC7 = DC8 = DC9 = (int) next_block_row[0][0];
  502. output_col = 0;
  503. last_block_column = compptr->width_in_blocks - 1;
  504. for (block_num = 0; block_num <= last_block_column; block_num++) {
  505. /* Fetch current DCT block into workspace so we can modify it. */
  506. jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
  507. /* Update DC values */
  508. if (block_num < last_block_column) {
  509. DC3 = (int) prev_block_row[1][0];
  510. DC6 = (int) buffer_ptr[1][0];
  511. DC9 = (int) next_block_row[1][0];
  512. }
  513. /* Compute coefficient estimates per K.8.
  514. * An estimate is applied only if coefficient is still zero,
  515. * and is not known to be fully accurate.
  516. */
  517. /* AC01 */
  518. if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
  519. num = 36 * Q00 * (DC4 - DC6);
  520. if (num >= 0) {
  521. pred = (int) (((Q01<<7) + num) / (Q01<<8));
  522. if (Al > 0 && pred >= (1<<Al))
  523. pred = (1<<Al)-1;
  524. } else {
  525. pred = (int) (((Q01<<7) - num) / (Q01<<8));
  526. if (Al > 0 && pred >= (1<<Al))
  527. pred = (1<<Al)-1;
  528. pred = -pred;
  529. }
  530. workspace[1] = (JCOEF) pred;
  531. }
  532. /* AC10 */
  533. if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
  534. num = 36 * Q00 * (DC2 - DC8);
  535. if (num >= 0) {
  536. pred = (int) (((Q10<<7) + num) / (Q10<<8));
  537. if (Al > 0 && pred >= (1<<Al))
  538. pred = (1<<Al)-1;
  539. } else {
  540. pred = (int) (((Q10<<7) - num) / (Q10<<8));
  541. if (Al > 0 && pred >= (1<<Al))
  542. pred = (1<<Al)-1;
  543. pred = -pred;
  544. }
  545. workspace[8] = (JCOEF) pred;
  546. }
  547. /* AC20 */
  548. if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
  549. num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
  550. if (num >= 0) {
  551. pred = (int) (((Q20<<7) + num) / (Q20<<8));
  552. if (Al > 0 && pred >= (1<<Al))
  553. pred = (1<<Al)-1;
  554. } else {
  555. pred = (int) (((Q20<<7) - num) / (Q20<<8));
  556. if (Al > 0 && pred >= (1<<Al))
  557. pred = (1<<Al)-1;
  558. pred = -pred;
  559. }
  560. workspace[16] = (JCOEF) pred;
  561. }
  562. /* AC11 */
  563. if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
  564. num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
  565. if (num >= 0) {
  566. pred = (int) (((Q11<<7) + num) / (Q11<<8));
  567. if (Al > 0 && pred >= (1<<Al))
  568. pred = (1<<Al)-1;
  569. } else {
  570. pred = (int) (((Q11<<7) - num) / (Q11<<8));
  571. if (Al > 0 && pred >= (1<<Al))
  572. pred = (1<<Al)-1;
  573. pred = -pred;
  574. }
  575. workspace[9] = (JCOEF) pred;
  576. }
  577. /* AC02 */
  578. if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
  579. num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
  580. if (num >= 0) {
  581. pred = (int) (((Q02<<7) + num) / (Q02<<8));
  582. if (Al > 0 && pred >= (1<<Al))
  583. pred = (1<<Al)-1;
  584. } else {
  585. pred = (int) (((Q02<<7) - num) / (Q02<<8));
  586. if (Al > 0 && pred >= (1<<Al))
  587. pred = (1<<Al)-1;
  588. pred = -pred;
  589. }
  590. workspace[2] = (JCOEF) pred;
  591. }
  592. /* OK, do the IDCT */
  593. (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
  594. output_ptr, output_col);
  595. /* Advance for next column */
  596. DC1 = DC2; DC2 = DC3;
  597. DC4 = DC5; DC5 = DC6;
  598. DC7 = DC8; DC8 = DC9;
  599. buffer_ptr++, prev_block_row++, next_block_row++;
  600. output_col += compptr->DCT_scaled_size;
  601. }
  602. output_ptr += compptr->DCT_scaled_size;
  603. }
  604. }
  605. if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  606. return JPEG_ROW_COMPLETED;
  607. return JPEG_SCAN_COMPLETED;
  608. }
  609. #endif /* BLOCK_SMOOTHING_SUPPORTED */
  610. /*
  611. * Initialize coefficient buffer controller.
  612. */
  613. GLOBAL(void)
  614. jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  615. {
  616. my_coef_ptr3 coef;
  617. coef = (my_coef_ptr3)
  618. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  619. SIZEOF(my_coef_controller3));
  620. cinfo->coef = (struct jpeg_d_coef_controller *) coef;
  621. coef->pub.start_input_pass = start_input_pass;
  622. coef->pub.start_output_pass = start_output_pass;
  623. #ifdef BLOCK_SMOOTHING_SUPPORTED
  624. coef->coef_bits_latch = NULL;
  625. #endif
  626. /* Create the coefficient buffer. */
  627. if (need_full_buffer) {
  628. #ifdef D_MULTISCAN_FILES_SUPPORTED
  629. /* Allocate a full-image virtual array for each component, */
  630. /* padded to a multiple of samp_factor DCT blocks in each direction. */
  631. /* Note we ask for a pre-zeroed array. */
  632. int ci, access_rows;
  633. jpeg_component_info *compptr;
  634. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  635. ci++, compptr++) {
  636. access_rows = compptr->v_samp_factor;
  637. #ifdef BLOCK_SMOOTHING_SUPPORTED
  638. /* If block smoothing could be used, need a bigger window */
  639. if (cinfo->progressive_mode)
  640. access_rows *= 3;
  641. #endif
  642. coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  643. ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
  644. (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  645. (long) compptr->h_samp_factor),
  646. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  647. (long) compptr->v_samp_factor),
  648. (JDIMENSION) access_rows);
  649. }
  650. coef->pub.consume_data = consume_data;
  651. coef->pub.decompress_data = decompress_data;
  652. coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
  653. #else
  654. ERREXIT(cinfo, JERR_NOT_COMPILED);
  655. #endif
  656. } else {
  657. /* We only need a single-MCU buffer. */
  658. JBLOCKROW buffer;
  659. int i;
  660. buffer = (JBLOCKROW)
  661. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  662. D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  663. for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
  664. coef->MCU_buffer[i] = buffer + i;
  665. }
  666. coef->pub.consume_data = dummy_consume_data;
  667. coef->pub.decompress_data = decompress_onepass;
  668. coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
  669. }
  670. }