2
0

jdcoefct.c 25 KB

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