jdcoefct.c 25 KB

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