jdcoefct.c 25 KB

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