jdcoefct.c 26 KB

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