jdcoefct.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * jdcoefct.c
  3. *
  4. * Copyright (C) 1994-1996, 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. /* Natural-order array positions of the first 5 zigzag-order coefficients */
  343. #define Q01_POS 1
  344. #define Q10_POS 8
  345. #define Q20_POS 16
  346. #define Q11_POS 9
  347. #define Q02_POS 2
  348. /*
  349. * Determine whether block smoothing is applicable and safe.
  350. * We also latch the current states of the coef_bits[] entries for the
  351. * AC coefficients; otherwise, if the input side of the decompressor
  352. * advances into a new scan, we might think the coefficients are known
  353. * more accurately than they really are.
  354. */
  355. LOCAL(boolean)
  356. smoothing_ok (j_decompress_ptr cinfo)
  357. {
  358. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  359. boolean smoothing_useful = FALSE;
  360. int ci, coefi;
  361. jpeg_component_info *compptr;
  362. JQUANT_TBL * qtable;
  363. int * coef_bits;
  364. int * coef_bits_latch;
  365. if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
  366. return FALSE;
  367. /* Allocate latch area if not already done */
  368. if (coef->coef_bits_latch == NULL)
  369. coef->coef_bits_latch = (int *)
  370. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  371. cinfo->num_components *
  372. (SAVED_COEFS * SIZEOF(int)));
  373. coef_bits_latch = coef->coef_bits_latch;
  374. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  375. ci++, compptr++) {
  376. /* All components' quantization values must already be latched. */
  377. if ((qtable = compptr->quant_table) == NULL)
  378. return FALSE;
  379. /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
  380. if (qtable->quantval[0] == 0 ||
  381. qtable->quantval[Q01_POS] == 0 ||
  382. qtable->quantval[Q10_POS] == 0 ||
  383. qtable->quantval[Q20_POS] == 0 ||
  384. qtable->quantval[Q11_POS] == 0 ||
  385. qtable->quantval[Q02_POS] == 0)
  386. return FALSE;
  387. /* DC values must be at least partly known for all components. */
  388. coef_bits = cinfo->coef_bits[ci];
  389. if (coef_bits[0] < 0)
  390. return FALSE;
  391. /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
  392. for (coefi = 1; coefi <= 5; coefi++) {
  393. coef_bits_latch[coefi] = coef_bits[coefi];
  394. if (coef_bits[coefi] != 0)
  395. smoothing_useful = TRUE;
  396. }
  397. coef_bits_latch += SAVED_COEFS;
  398. }
  399. return smoothing_useful;
  400. }
  401. /*
  402. * Variant of decompress_data for use when doing block smoothing.
  403. */
  404. METHODDEF(int)
  405. decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  406. {
  407. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  408. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  409. JDIMENSION block_num, last_block_column;
  410. int ci, block_row, block_rows, access_rows;
  411. JBLOCKARRAY buffer;
  412. JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
  413. JSAMPARRAY output_ptr;
  414. JDIMENSION output_col;
  415. jpeg_component_info *compptr;
  416. inverse_DCT_method_ptr inverse_DCT;
  417. boolean first_row, last_row;
  418. JBLOCK workspace;
  419. int *coef_bits;
  420. JQUANT_TBL *quanttbl;
  421. INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
  422. int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
  423. int Al, pred;
  424. /* Force some input to be done if we are getting ahead of the input. */
  425. while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  426. ! cinfo->inputctl->eoi_reached) {
  427. if (cinfo->input_scan_number == cinfo->output_scan_number) {
  428. /* If input is working on current scan, we ordinarily want it to
  429. * have completed the current row. But if input scan is DC,
  430. * we want it to keep one row ahead so that next block row's DC
  431. * values are up to date.
  432. */
  433. JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
  434. if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
  435. break;
  436. }
  437. if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
  438. return JPEG_SUSPENDED;
  439. }
  440. /* OK, output from the virtual arrays. */
  441. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  442. ci++, compptr++) {
  443. /* Don't bother to IDCT an uninteresting component. */
  444. if (! compptr->component_needed)
  445. continue;
  446. /* Count non-dummy DCT block rows in this iMCU row. */
  447. if (cinfo->output_iMCU_row < last_iMCU_row) {
  448. block_rows = compptr->v_samp_factor;
  449. access_rows = block_rows * 2; /* this and next iMCU row */
  450. last_row = FALSE;
  451. } else {
  452. /* NB: can't use last_row_height here; it is input-side-dependent! */
  453. block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  454. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  455. access_rows = block_rows; /* this iMCU row only */
  456. last_row = TRUE;
  457. }
  458. /* Align the virtual buffer for this component. */
  459. if (cinfo->output_iMCU_row > 0) {
  460. access_rows += compptr->v_samp_factor; /* prior iMCU row too */
  461. buffer = (*cinfo->mem->access_virt_barray)
  462. ((j_common_ptr) cinfo, coef->whole_image[ci],
  463. (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
  464. (JDIMENSION) access_rows, FALSE);
  465. buffer += compptr->v_samp_factor; /* point to current iMCU row */
  466. first_row = FALSE;
  467. } else {
  468. buffer = (*cinfo->mem->access_virt_barray)
  469. ((j_common_ptr) cinfo, coef->whole_image[ci],
  470. (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
  471. first_row = TRUE;
  472. }
  473. /* Fetch component-dependent info */
  474. coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
  475. quanttbl = compptr->quant_table;
  476. Q00 = quanttbl->quantval[0];
  477. Q01 = quanttbl->quantval[Q01_POS];
  478. Q10 = quanttbl->quantval[Q10_POS];
  479. Q20 = quanttbl->quantval[Q20_POS];
  480. Q11 = quanttbl->quantval[Q11_POS];
  481. Q02 = quanttbl->quantval[Q02_POS];
  482. inverse_DCT = cinfo->idct->inverse_DCT[ci];
  483. output_ptr = output_buf[ci];
  484. /* Loop over all DCT blocks to be processed. */
  485. for (block_row = 0; block_row < block_rows; block_row++) {
  486. buffer_ptr = buffer[block_row];
  487. if (first_row && block_row == 0)
  488. prev_block_row = buffer_ptr;
  489. else
  490. prev_block_row = buffer[block_row-1];
  491. if (last_row && block_row == block_rows-1)
  492. next_block_row = buffer_ptr;
  493. else
  494. next_block_row = buffer[block_row+1];
  495. /* We fetch the surrounding DC values using a sliding-register approach.
  496. * Initialize all nine here so as to do the right thing on narrow pics.
  497. */
  498. DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
  499. DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
  500. DC7 = DC8 = DC9 = (int) next_block_row[0][0];
  501. output_col = 0;
  502. last_block_column = compptr->width_in_blocks - 1;
  503. for (block_num = 0; block_num <= last_block_column; block_num++) {
  504. /* Fetch current DCT block into workspace so we can modify it. */
  505. jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
  506. /* Update DC values */
  507. if (block_num < last_block_column) {
  508. DC3 = (int) prev_block_row[1][0];
  509. DC6 = (int) buffer_ptr[1][0];
  510. DC9 = (int) next_block_row[1][0];
  511. }
  512. /* Compute coefficient estimates per K.8.
  513. * An estimate is applied only if coefficient is still zero,
  514. * and is not known to be fully accurate.
  515. */
  516. /* AC01 */
  517. if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
  518. num = 36 * Q00 * (DC4 - DC6);
  519. if (num >= 0) {
  520. pred = (int) (((Q01<<7) + num) / (Q01<<8));
  521. if (Al > 0 && pred >= (1<<Al))
  522. pred = (1<<Al)-1;
  523. } else {
  524. pred = (int) (((Q01<<7) - num) / (Q01<<8));
  525. if (Al > 0 && pred >= (1<<Al))
  526. pred = (1<<Al)-1;
  527. pred = -pred;
  528. }
  529. workspace[1] = (JCOEF) pred;
  530. }
  531. /* AC10 */
  532. if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
  533. num = 36 * Q00 * (DC2 - DC8);
  534. if (num >= 0) {
  535. pred = (int) (((Q10<<7) + num) / (Q10<<8));
  536. if (Al > 0 && pred >= (1<<Al))
  537. pred = (1<<Al)-1;
  538. } else {
  539. pred = (int) (((Q10<<7) - num) / (Q10<<8));
  540. if (Al > 0 && pred >= (1<<Al))
  541. pred = (1<<Al)-1;
  542. pred = -pred;
  543. }
  544. workspace[8] = (JCOEF) pred;
  545. }
  546. /* AC20 */
  547. if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
  548. num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
  549. if (num >= 0) {
  550. pred = (int) (((Q20<<7) + num) / (Q20<<8));
  551. if (Al > 0 && pred >= (1<<Al))
  552. pred = (1<<Al)-1;
  553. } else {
  554. pred = (int) (((Q20<<7) - num) / (Q20<<8));
  555. if (Al > 0 && pred >= (1<<Al))
  556. pred = (1<<Al)-1;
  557. pred = -pred;
  558. }
  559. workspace[16] = (JCOEF) pred;
  560. }
  561. /* AC11 */
  562. if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
  563. num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
  564. if (num >= 0) {
  565. pred = (int) (((Q11<<7) + num) / (Q11<<8));
  566. if (Al > 0 && pred >= (1<<Al))
  567. pred = (1<<Al)-1;
  568. } else {
  569. pred = (int) (((Q11<<7) - num) / (Q11<<8));
  570. if (Al > 0 && pred >= (1<<Al))
  571. pred = (1<<Al)-1;
  572. pred = -pred;
  573. }
  574. workspace[9] = (JCOEF) pred;
  575. }
  576. /* AC02 */
  577. if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
  578. num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
  579. if (num >= 0) {
  580. pred = (int) (((Q02<<7) + num) / (Q02<<8));
  581. if (Al > 0 && pred >= (1<<Al))
  582. pred = (1<<Al)-1;
  583. } else {
  584. pred = (int) (((Q02<<7) - num) / (Q02<<8));
  585. if (Al > 0 && pred >= (1<<Al))
  586. pred = (1<<Al)-1;
  587. pred = -pred;
  588. }
  589. workspace[2] = (JCOEF) pred;
  590. }
  591. /* OK, do the IDCT */
  592. (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
  593. output_ptr, output_col);
  594. /* Advance for next column */
  595. DC1 = DC2; DC2 = DC3;
  596. DC4 = DC5; DC5 = DC6;
  597. DC7 = DC8; DC8 = DC9;
  598. buffer_ptr++, prev_block_row++, next_block_row++;
  599. output_col += compptr->DCT_scaled_size;
  600. }
  601. output_ptr += compptr->DCT_scaled_size;
  602. }
  603. }
  604. if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  605. return JPEG_ROW_COMPLETED;
  606. return JPEG_SCAN_COMPLETED;
  607. }
  608. #endif /* BLOCK_SMOOTHING_SUPPORTED */
  609. /*
  610. * Initialize coefficient buffer controller.
  611. */
  612. GLOBAL(void)
  613. jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  614. {
  615. my_coef_ptr coef;
  616. coef = (my_coef_ptr)
  617. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  618. SIZEOF(my_coef_controller));
  619. cinfo->coef = (struct jpeg_d_coef_controller *) coef;
  620. coef->pub.start_input_pass = start_input_pass;
  621. coef->pub.start_output_pass = start_output_pass;
  622. #ifdef BLOCK_SMOOTHING_SUPPORTED
  623. coef->coef_bits_latch = NULL;
  624. #endif
  625. /* Create the coefficient buffer. */
  626. if (need_full_buffer) {
  627. #ifdef D_MULTISCAN_FILES_SUPPORTED
  628. /* Allocate a full-image virtual array for each component, */
  629. /* padded to a multiple of samp_factor DCT blocks in each direction. */
  630. /* Note we ask for a pre-zeroed array. */
  631. int ci, access_rows;
  632. jpeg_component_info *compptr;
  633. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  634. ci++, compptr++) {
  635. access_rows = compptr->v_samp_factor;
  636. #ifdef BLOCK_SMOOTHING_SUPPORTED
  637. /* If block smoothing could be used, need a bigger window */
  638. if (cinfo->progressive_mode)
  639. access_rows *= 3;
  640. #endif
  641. coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  642. ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
  643. (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  644. (long) compptr->h_samp_factor),
  645. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  646. (long) compptr->v_samp_factor),
  647. (JDIMENSION) access_rows);
  648. }
  649. coef->pub.consume_data = consume_data;
  650. coef->pub.decompress_data = decompress_data;
  651. coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
  652. #else
  653. ERREXIT(cinfo, JERR_NOT_COMPILED);
  654. #endif
  655. } else {
  656. /* We only need a single-MCU buffer. */
  657. JBLOCKROW buffer;
  658. int i;
  659. buffer = (JBLOCKROW)
  660. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  661. D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  662. for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
  663. coef->MCU_buffer[i] = buffer + i;
  664. }
  665. coef->pub.consume_data = dummy_consume_data;
  666. coef->pub.decompress_data = decompress_onepass;
  667. coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
  668. }
  669. }