jdcoefct.c 25 KB

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