Source code of Windows XP (NT5)
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.

725 lines
24 KiB

  1. /*
  2. * jdcoefct.c
  3. *
  4. * Copyright (C) 1994-1995, 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_controller;
  52. typedef my_coef_controller * my_coef_ptr;
  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_row (j_decompress_ptr cinfo)
  67. /* Reset within-iMCU-row counters for a new row (input side) */
  68. {
  69. my_coef_ptr coef = (my_coef_ptr) 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_row(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_ptr coef = (my_coef_ptr) 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. * For single pass, this is the same as the components in the scan.
  120. */
  121. METHODDEF int
  122. decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  123. {
  124. my_coef_ptr coef = (my_coef_ptr) 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[ci] + yoffset * compptr->DCT_scaled_size;
  164. start_col = MCU_col_num * compptr->MCU_sample_width;
  165. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  166. if (cinfo->input_iMCU_row < last_iMCU_row ||
  167. yoffset+yindex < compptr->last_row_height) {
  168. output_col = start_col;
  169. for (xindex = 0; xindex < useful_width; xindex++) {
  170. (*inverse_DCT) (cinfo, compptr,
  171. (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
  172. output_ptr, output_col);
  173. output_col += compptr->DCT_scaled_size;
  174. }
  175. }
  176. blkn += compptr->MCU_width;
  177. output_ptr += compptr->DCT_scaled_size;
  178. }
  179. }
  180. }
  181. /* Completed an MCU row, but perhaps not an iMCU row */
  182. coef->MCU_ctr = 0;
  183. }
  184. /* Completed the iMCU row, advance counters for next one */
  185. cinfo->output_iMCU_row++;
  186. if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
  187. start_iMCU_row(cinfo);
  188. return JPEG_ROW_COMPLETED;
  189. }
  190. /* Completed the scan */
  191. (*cinfo->inputctl->finish_input_pass) (cinfo);
  192. return JPEG_SCAN_COMPLETED;
  193. }
  194. /*
  195. * Dummy consume-input routine for single-pass operation.
  196. */
  197. METHODDEF int
  198. dummy_consume_data (j_decompress_ptr cinfo)
  199. {
  200. return JPEG_SUSPENDED; /* Always indicate nothing was done */
  201. }
  202. #ifdef D_MULTISCAN_FILES_SUPPORTED
  203. /*
  204. * Consume input data and store it in the full-image coefficient buffer.
  205. * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
  206. * ie, v_samp_factor block rows for each component in the scan.
  207. * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  208. */
  209. METHODDEF int
  210. consume_data (j_decompress_ptr cinfo)
  211. {
  212. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  213. JDIMENSION MCU_col_num; /* index of current MCU within row */
  214. int blkn, ci, xindex, yindex, yoffset;
  215. JDIMENSION start_col;
  216. JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  217. JBLOCKROW buffer_ptr;
  218. jpeg_component_info *compptr;
  219. /* Align the virtual buffers for the components used in this scan. */
  220. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  221. compptr = cinfo->cur_comp_info[ci];
  222. buffer[ci] = (*cinfo->mem->access_virt_barray)
  223. ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  224. cinfo->input_iMCU_row * compptr->v_samp_factor,
  225. (JDIMENSION) compptr->v_samp_factor, TRUE);
  226. /* Note: entropy decoder expects buffer to be zeroed,
  227. * but this is handled automatically by the memory manager
  228. * because we requested a pre-zeroed array.
  229. */
  230. }
  231. /* Loop to process one whole iMCU row */
  232. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  233. yoffset++) {
  234. for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
  235. MCU_col_num++) {
  236. /* Construct list of pointers to DCT blocks belonging to this MCU */
  237. blkn = 0; /* index of current DCT block within MCU */
  238. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  239. compptr = cinfo->cur_comp_info[ci];
  240. start_col = MCU_col_num * compptr->MCU_width;
  241. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  242. buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  243. for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  244. coef->MCU_buffer[blkn++] = buffer_ptr++;
  245. }
  246. }
  247. }
  248. /* Try to fetch the MCU. */
  249. if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  250. /* Suspension forced; update state counters and exit */
  251. coef->MCU_vert_offset = yoffset;
  252. coef->MCU_ctr = MCU_col_num;
  253. return JPEG_SUSPENDED;
  254. }
  255. }
  256. /* Completed an MCU row, but perhaps not an iMCU row */
  257. coef->MCU_ctr = 0;
  258. }
  259. /* Completed the iMCU row, advance counters for next one */
  260. if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
  261. start_iMCU_row(cinfo);
  262. return JPEG_ROW_COMPLETED;
  263. }
  264. /* Completed the scan */
  265. (*cinfo->inputctl->finish_input_pass) (cinfo);
  266. return JPEG_SCAN_COMPLETED;
  267. }
  268. /*
  269. * Decompress and return some data in the multi-pass case.
  270. * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  271. * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  272. *
  273. * NB: output_buf contains a plane for each component in image.
  274. */
  275. METHODDEF int
  276. decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  277. {
  278. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  279. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  280. JDIMENSION block_num;
  281. int ci, block_row, block_rows;
  282. JBLOCKARRAY buffer;
  283. JBLOCKROW buffer_ptr;
  284. JSAMPARRAY output_ptr;
  285. JDIMENSION output_col;
  286. jpeg_component_info *compptr;
  287. inverse_DCT_method_ptr inverse_DCT;
  288. /* Force some input to be done if we are getting ahead of the input. */
  289. while (cinfo->input_scan_number < cinfo->output_scan_number ||
  290. (cinfo->input_scan_number == cinfo->output_scan_number &&
  291. cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
  292. if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
  293. return JPEG_SUSPENDED;
  294. }
  295. /* OK, output from the virtual arrays. */
  296. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  297. ci++, compptr++) {
  298. /* Don't bother to IDCT an uninteresting component. */
  299. if (! compptr->component_needed)
  300. continue;
  301. /* Align the virtual buffer for this component. */
  302. buffer = (*cinfo->mem->access_virt_barray)
  303. ((j_common_ptr) cinfo, coef->whole_image[ci],
  304. cinfo->output_iMCU_row * compptr->v_samp_factor,
  305. (JDIMENSION) compptr->v_samp_factor, FALSE);
  306. /* Count non-dummy DCT block rows in this iMCU row. */
  307. if (cinfo->output_iMCU_row < last_iMCU_row)
  308. block_rows = compptr->v_samp_factor;
  309. else {
  310. /* NB: can't use last_row_height here; it is input-side-dependent! */
  311. block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  312. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  313. }
  314. inverse_DCT = cinfo->idct->inverse_DCT[ci];
  315. output_ptr = output_buf[ci];
  316. /* Loop over all DCT blocks to be processed. */
  317. for (block_row = 0; block_row < block_rows; block_row++) {
  318. buffer_ptr = buffer[block_row];
  319. output_col = 0;
  320. for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
  321. (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
  322. output_ptr, output_col);
  323. buffer_ptr++;
  324. output_col += compptr->DCT_scaled_size;
  325. }
  326. output_ptr += compptr->DCT_scaled_size;
  327. }
  328. }
  329. if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  330. return JPEG_ROW_COMPLETED;
  331. return JPEG_SCAN_COMPLETED;
  332. }
  333. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  334. #ifdef BLOCK_SMOOTHING_SUPPORTED
  335. /*
  336. * This code applies interblock smoothing as described by section K.8
  337. * of the JPEG standard: the first 5 AC coefficients are estimated from
  338. * the DC values of a DCT block and its 8 neighboring blocks.
  339. * We apply smoothing only for progressive JPEG decoding, and only if
  340. * the coefficients it can estimate are not yet known to full precision.
  341. */
  342. /*
  343. * Determine whether block smoothing is applicable and safe.
  344. * We also latch the current states of the coef_bits[] entries for the
  345. * AC coefficients; otherwise, if the input side of the decompressor
  346. * advances into a new scan, we might think the coefficients are known
  347. * more accurately than they really are.
  348. */
  349. LOCAL boolean
  350. smoothing_ok (j_decompress_ptr cinfo)
  351. {
  352. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  353. boolean smoothing_useful = FALSE;
  354. int ci, coefi;
  355. jpeg_component_info *compptr;
  356. JQUANT_TBL * qtable;
  357. int * coef_bits;
  358. int * coef_bits_latch;
  359. if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
  360. return FALSE;
  361. /* Allocate latch area if not already done */
  362. if (coef->coef_bits_latch == NULL)
  363. coef->coef_bits_latch = (int *)
  364. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  365. cinfo->num_components *
  366. (SAVED_COEFS * SIZEOF(int)));
  367. coef_bits_latch = coef->coef_bits_latch;
  368. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  369. ci++, compptr++) {
  370. /* All components' quantization values must already be latched. */
  371. if ((qtable = compptr->quant_table) == NULL)
  372. return FALSE;
  373. /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
  374. for (coefi = 0; coefi <= 5; coefi++) {
  375. if (qtable->quantval[coefi] == 0)
  376. return FALSE;
  377. }
  378. /* DC values must be at least partly known for all components. */
  379. coef_bits = cinfo->coef_bits[ci];
  380. if (coef_bits[0] < 0)
  381. return FALSE;
  382. /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
  383. for (coefi = 1; coefi <= 5; coefi++) {
  384. coef_bits_latch[coefi] = coef_bits[coefi];
  385. if (coef_bits[coefi] != 0)
  386. smoothing_useful = TRUE;
  387. }
  388. coef_bits_latch += SAVED_COEFS;
  389. }
  390. return smoothing_useful;
  391. }
  392. /*
  393. * Variant of decompress_data for use when doing block smoothing.
  394. */
  395. METHODDEF int
  396. decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  397. {
  398. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  399. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  400. JDIMENSION block_num, last_block_column;
  401. int ci, block_row, block_rows, access_rows;
  402. JBLOCKARRAY buffer;
  403. JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
  404. JSAMPARRAY output_ptr;
  405. JDIMENSION output_col;
  406. jpeg_component_info *compptr;
  407. inverse_DCT_method_ptr inverse_DCT;
  408. boolean first_row, last_row;
  409. JBLOCK workspace;
  410. int *coef_bits;
  411. JQUANT_TBL *quanttbl;
  412. INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
  413. int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
  414. int Al, pred;
  415. /* Force some input to be done if we are getting ahead of the input. */
  416. while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  417. ! cinfo->inputctl->eoi_reached) {
  418. if (cinfo->input_scan_number == cinfo->output_scan_number) {
  419. /* If input is working on current scan, we ordinarily want it to
  420. * have completed the current row. But if input scan is DC,
  421. * we want it to keep one row ahead so that next block row's DC
  422. * values are up to date.
  423. */
  424. JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
  425. if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
  426. break;
  427. }
  428. if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
  429. return JPEG_SUSPENDED;
  430. }
  431. /* OK, output from the virtual arrays. */
  432. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  433. ci++, compptr++) {
  434. /* Don't bother to IDCT an uninteresting component. */
  435. if (! compptr->component_needed)
  436. continue;
  437. /* Count non-dummy DCT block rows in this iMCU row. */
  438. if (cinfo->output_iMCU_row < last_iMCU_row) {
  439. block_rows = compptr->v_samp_factor;
  440. access_rows = block_rows * 2; /* this and next iMCU row */
  441. last_row = FALSE;
  442. } else {
  443. /* NB: can't use last_row_height here; it is input-side-dependent! */
  444. block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  445. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  446. access_rows = block_rows; /* this iMCU row only */
  447. last_row = TRUE;
  448. }
  449. /* Align the virtual buffer for this component. */
  450. if (cinfo->output_iMCU_row > 0) {
  451. access_rows += compptr->v_samp_factor; /* prior iMCU row too */
  452. buffer = (*cinfo->mem->access_virt_barray)
  453. ((j_common_ptr) cinfo, coef->whole_image[ci],
  454. (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
  455. (JDIMENSION) access_rows, FALSE);
  456. buffer += compptr->v_samp_factor; /* point to current iMCU row */
  457. first_row = FALSE;
  458. } else {
  459. buffer = (*cinfo->mem->access_virt_barray)
  460. ((j_common_ptr) cinfo, coef->whole_image[ci],
  461. (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
  462. first_row = TRUE;
  463. }
  464. /* Fetch component-dependent info */
  465. coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
  466. quanttbl = compptr->quant_table;
  467. Q00 = quanttbl->quantval[0];
  468. Q01 = quanttbl->quantval[1];
  469. Q10 = quanttbl->quantval[2];
  470. Q20 = quanttbl->quantval[3];
  471. Q11 = quanttbl->quantval[4];
  472. Q02 = quanttbl->quantval[5];
  473. inverse_DCT = cinfo->idct->inverse_DCT[ci];
  474. output_ptr = output_buf[ci];
  475. /* Loop over all DCT blocks to be processed. */
  476. for (block_row = 0; block_row < block_rows; block_row++) {
  477. buffer_ptr = buffer[block_row];
  478. if (first_row && block_row == 0)
  479. prev_block_row = buffer_ptr;
  480. else
  481. prev_block_row = buffer[block_row-1];
  482. if (last_row && block_row == block_rows-1)
  483. next_block_row = buffer_ptr;
  484. else
  485. next_block_row = buffer[block_row+1];
  486. /* We fetch the surrounding DC values using a sliding-register approach.
  487. * Initialize all nine here so as to do the right thing on narrow pics.
  488. */
  489. DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
  490. DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
  491. DC7 = DC8 = DC9 = (int) next_block_row[0][0];
  492. output_col = 0;
  493. last_block_column = compptr->width_in_blocks - 1;
  494. for (block_num = 0; block_num <= last_block_column; block_num++) {
  495. /* Fetch current DCT block into workspace so we can modify it. */
  496. jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
  497. /* Update DC values */
  498. if (block_num < last_block_column) {
  499. DC3 = (int) prev_block_row[1][0];
  500. DC6 = (int) buffer_ptr[1][0];
  501. DC9 = (int) next_block_row[1][0];
  502. }
  503. /* Compute coefficient estimates per K.8.
  504. * An estimate is applied only if coefficient is still zero,
  505. * and is not known to be fully accurate.
  506. */
  507. /* AC01 */
  508. if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
  509. num = 36 * Q00 * (DC4 - DC6);
  510. if (num >= 0) {
  511. pred = (int) (((Q01<<7) + num) / (Q01<<8));
  512. if (Al > 0 && pred >= (1<<Al))
  513. pred = (1<<Al)-1;
  514. } else {
  515. pred = (int) (((Q01<<7) - num) / (Q01<<8));
  516. if (Al > 0 && pred >= (1<<Al))
  517. pred = (1<<Al)-1;
  518. pred = -pred;
  519. }
  520. workspace[1] = (JCOEF) pred;
  521. }
  522. /* AC10 */
  523. if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
  524. num = 36 * Q00 * (DC2 - DC8);
  525. if (num >= 0) {
  526. pred = (int) (((Q10<<7) + num) / (Q10<<8));
  527. if (Al > 0 && pred >= (1<<Al))
  528. pred = (1<<Al)-1;
  529. } else {
  530. pred = (int) (((Q10<<7) - num) / (Q10<<8));
  531. if (Al > 0 && pred >= (1<<Al))
  532. pred = (1<<Al)-1;
  533. pred = -pred;
  534. }
  535. workspace[8] = (JCOEF) pred;
  536. }
  537. /* AC20 */
  538. if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
  539. num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
  540. if (num >= 0) {
  541. pred = (int) (((Q20<<7) + num) / (Q20<<8));
  542. if (Al > 0 && pred >= (1<<Al))
  543. pred = (1<<Al)-1;
  544. } else {
  545. pred = (int) (((Q20<<7) - num) / (Q20<<8));
  546. if (Al > 0 && pred >= (1<<Al))
  547. pred = (1<<Al)-1;
  548. pred = -pred;
  549. }
  550. workspace[16] = (JCOEF) pred;
  551. }
  552. /* AC11 */
  553. if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
  554. num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
  555. if (num >= 0) {
  556. pred = (int) (((Q11<<7) + num) / (Q11<<8));
  557. if (Al > 0 && pred >= (1<<Al))
  558. pred = (1<<Al)-1;
  559. } else {
  560. pred = (int) (((Q11<<7) - num) / (Q11<<8));
  561. if (Al > 0 && pred >= (1<<Al))
  562. pred = (1<<Al)-1;
  563. pred = -pred;
  564. }
  565. workspace[9] = (JCOEF) pred;
  566. }
  567. /* AC02 */
  568. if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
  569. num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
  570. if (num >= 0) {
  571. pred = (int) (((Q02<<7) + num) / (Q02<<8));
  572. if (Al > 0 && pred >= (1<<Al))
  573. pred = (1<<Al)-1;
  574. } else {
  575. pred = (int) (((Q02<<7) - num) / (Q02<<8));
  576. if (Al > 0 && pred >= (1<<Al))
  577. pred = (1<<Al)-1;
  578. pred = -pred;
  579. }
  580. workspace[2] = (JCOEF) pred;
  581. }
  582. /* OK, do the IDCT */
  583. (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
  584. output_ptr, output_col);
  585. /* Advance for next column */
  586. DC1 = DC2; DC2 = DC3;
  587. DC4 = DC5; DC5 = DC6;
  588. DC7 = DC8; DC8 = DC9;
  589. buffer_ptr++, prev_block_row++, next_block_row++;
  590. output_col += compptr->DCT_scaled_size;
  591. }
  592. output_ptr += compptr->DCT_scaled_size;
  593. }
  594. }
  595. if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  596. return JPEG_ROW_COMPLETED;
  597. return JPEG_SCAN_COMPLETED;
  598. }
  599. #endif /* BLOCK_SMOOTHING_SUPPORTED */
  600. /*
  601. * Initialize coefficient buffer controller.
  602. */
  603. GLOBAL void
  604. jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  605. {
  606. my_coef_ptr coef;
  607. coef = (my_coef_ptr)
  608. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  609. SIZEOF(my_coef_controller));
  610. cinfo->coef = (struct jpeg_d_coef_controller *) coef;
  611. coef->pub.start_input_pass = start_input_pass;
  612. coef->pub.start_output_pass = start_output_pass;
  613. #ifdef BLOCK_SMOOTHING_SUPPORTED
  614. coef->coef_bits_latch = NULL;
  615. #endif
  616. /* Create the coefficient buffer. */
  617. if (need_full_buffer) {
  618. #ifdef D_MULTISCAN_FILES_SUPPORTED
  619. /* Allocate a full-image virtual array for each component, */
  620. /* padded to a multiple of samp_factor DCT blocks in each direction. */
  621. /* Note we ask for a pre-zeroed array. */
  622. int ci, access_rows;
  623. jpeg_component_info *compptr;
  624. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  625. ci++, compptr++) {
  626. access_rows = compptr->v_samp_factor;
  627. #ifdef BLOCK_SMOOTHING_SUPPORTED
  628. /* If block smoothing could be used, need a bigger window */
  629. if (cinfo->progressive_mode)
  630. access_rows *= 3;
  631. #endif
  632. coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  633. ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
  634. (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  635. (long) compptr->h_samp_factor),
  636. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  637. (long) compptr->v_samp_factor),
  638. (JDIMENSION) access_rows);
  639. }
  640. coef->pub.consume_data = consume_data;
  641. coef->pub.decompress_data = decompress_data;
  642. coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
  643. #else
  644. ERREXIT(cinfo, JERR_NOT_COMPILED);
  645. #endif
  646. } else {
  647. /* We only need a single-MCU buffer. */
  648. JBLOCKROW buffer;
  649. int i;
  650. buffer = (JBLOCKROW)
  651. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  652. D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  653. for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
  654. coef->MCU_buffer[i] = buffer + i;
  655. }
  656. coef->pub.consume_data = dummy_consume_data;
  657. coef->pub.decompress_data = decompress_onepass;
  658. coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
  659. }
  660. }