jdcoefct.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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, 2019-2020, 2022, D. R. Commander.
  9. * Copyright (C) 2015, 2020, 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->insufficient_data)
  92. cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
  93. if (!(*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  94. /* Suspension forced; update state counters and exit */
  95. coef->MCU_vert_offset = yoffset;
  96. coef->MCU_ctr = MCU_col_num;
  97. return JPEG_SUSPENDED;
  98. }
  99. /* Only perform the IDCT on blocks that are contained within the desired
  100. * cropping region.
  101. */
  102. if (MCU_col_num >= cinfo->master->first_iMCU_col &&
  103. MCU_col_num <= cinfo->master->last_iMCU_col) {
  104. /* Determine where data should go in output_buf and do the IDCT thing.
  105. * We skip dummy blocks at the right and bottom edges (but blkn gets
  106. * incremented past them!). Note the inner loop relies on having
  107. * allocated the MCU_buffer[] blocks sequentially.
  108. */
  109. blkn = 0; /* index of current DCT block within MCU */
  110. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  111. compptr = cinfo->cur_comp_info[ci];
  112. /* Don't bother to IDCT an uninteresting component. */
  113. if (!compptr->component_needed) {
  114. blkn += compptr->MCU_blocks;
  115. continue;
  116. }
  117. inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
  118. useful_width = (MCU_col_num < last_MCU_col) ?
  119. compptr->MCU_width : compptr->last_col_width;
  120. output_ptr = output_buf[compptr->component_index] +
  121. yoffset * compptr->_DCT_scaled_size;
  122. start_col = (MCU_col_num - cinfo->master->first_iMCU_col) *
  123. compptr->MCU_sample_width;
  124. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  125. if (cinfo->input_iMCU_row < last_iMCU_row ||
  126. yoffset + yindex < compptr->last_row_height) {
  127. output_col = start_col;
  128. for (xindex = 0; xindex < useful_width; xindex++) {
  129. (*inverse_DCT) (cinfo, compptr,
  130. (JCOEFPTR)coef->MCU_buffer[blkn + xindex],
  131. output_ptr, output_col);
  132. output_col += compptr->_DCT_scaled_size;
  133. }
  134. }
  135. blkn += compptr->MCU_width;
  136. output_ptr += compptr->_DCT_scaled_size;
  137. }
  138. }
  139. }
  140. }
  141. /* Completed an MCU row, but perhaps not an iMCU row */
  142. coef->MCU_ctr = 0;
  143. }
  144. /* Completed the iMCU row, advance counters for next one */
  145. cinfo->output_iMCU_row++;
  146. if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
  147. start_iMCU_row(cinfo);
  148. return JPEG_ROW_COMPLETED;
  149. }
  150. /* Completed the scan */
  151. (*cinfo->inputctl->finish_input_pass) (cinfo);
  152. return JPEG_SCAN_COMPLETED;
  153. }
  154. /*
  155. * Dummy consume-input routine for single-pass operation.
  156. */
  157. METHODDEF(int)
  158. dummy_consume_data(j_decompress_ptr cinfo)
  159. {
  160. return JPEG_SUSPENDED; /* Always indicate nothing was done */
  161. }
  162. #ifdef D_MULTISCAN_FILES_SUPPORTED
  163. /*
  164. * Consume input data and store it in the full-image coefficient buffer.
  165. * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
  166. * ie, v_samp_factor block rows for each component in the scan.
  167. * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  168. */
  169. METHODDEF(int)
  170. consume_data(j_decompress_ptr cinfo)
  171. {
  172. my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
  173. JDIMENSION MCU_col_num; /* index of current MCU within row */
  174. int blkn, ci, xindex, yindex, yoffset;
  175. JDIMENSION start_col;
  176. JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  177. JBLOCKROW buffer_ptr;
  178. jpeg_component_info *compptr;
  179. /* Align the virtual buffers for the components used in this scan. */
  180. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  181. compptr = cinfo->cur_comp_info[ci];
  182. buffer[ci] = (*cinfo->mem->access_virt_barray)
  183. ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
  184. cinfo->input_iMCU_row * compptr->v_samp_factor,
  185. (JDIMENSION)compptr->v_samp_factor, TRUE);
  186. /* Note: entropy decoder expects buffer to be zeroed,
  187. * but this is handled automatically by the memory manager
  188. * because we requested a pre-zeroed array.
  189. */
  190. }
  191. /* Loop to process one whole iMCU row */
  192. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  193. yoffset++) {
  194. for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
  195. MCU_col_num++) {
  196. /* Construct list of pointers to DCT blocks belonging to this MCU */
  197. blkn = 0; /* index of current DCT block within MCU */
  198. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  199. compptr = cinfo->cur_comp_info[ci];
  200. start_col = MCU_col_num * compptr->MCU_width;
  201. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  202. buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
  203. for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  204. coef->MCU_buffer[blkn++] = buffer_ptr++;
  205. }
  206. }
  207. }
  208. if (!cinfo->entropy->insufficient_data)
  209. cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
  210. /* Try to fetch the MCU. */
  211. if (!(*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  212. /* Suspension forced; update state counters and exit */
  213. coef->MCU_vert_offset = yoffset;
  214. coef->MCU_ctr = MCU_col_num;
  215. return JPEG_SUSPENDED;
  216. }
  217. }
  218. /* Completed an MCU row, but perhaps not an iMCU row */
  219. coef->MCU_ctr = 0;
  220. }
  221. /* Completed the iMCU row, advance counters for next one */
  222. if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
  223. start_iMCU_row(cinfo);
  224. return JPEG_ROW_COMPLETED;
  225. }
  226. /* Completed the scan */
  227. (*cinfo->inputctl->finish_input_pass) (cinfo);
  228. return JPEG_SCAN_COMPLETED;
  229. }
  230. /*
  231. * Decompress and return some data in the multi-pass case.
  232. * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  233. * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  234. *
  235. * NB: output_buf contains a plane for each component in image.
  236. */
  237. METHODDEF(int)
  238. decompress_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  239. {
  240. my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
  241. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  242. JDIMENSION block_num;
  243. int ci, block_row, block_rows;
  244. JBLOCKARRAY buffer;
  245. JBLOCKROW buffer_ptr;
  246. JSAMPARRAY output_ptr;
  247. JDIMENSION output_col;
  248. jpeg_component_info *compptr;
  249. inverse_DCT_method_ptr inverse_DCT;
  250. /* Force some input to be done if we are getting ahead of the input. */
  251. while (cinfo->input_scan_number < cinfo->output_scan_number ||
  252. (cinfo->input_scan_number == cinfo->output_scan_number &&
  253. cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
  254. if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  255. return JPEG_SUSPENDED;
  256. }
  257. /* OK, output from the virtual arrays. */
  258. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  259. ci++, compptr++) {
  260. /* Don't bother to IDCT an uninteresting component. */
  261. if (!compptr->component_needed)
  262. continue;
  263. /* Align the virtual buffer for this component. */
  264. buffer = (*cinfo->mem->access_virt_barray)
  265. ((j_common_ptr)cinfo, coef->whole_image[ci],
  266. cinfo->output_iMCU_row * compptr->v_samp_factor,
  267. (JDIMENSION)compptr->v_samp_factor, FALSE);
  268. /* Count non-dummy DCT block rows in this iMCU row. */
  269. if (cinfo->output_iMCU_row < last_iMCU_row)
  270. block_rows = compptr->v_samp_factor;
  271. else {
  272. /* NB: can't use last_row_height here; it is input-side-dependent! */
  273. block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
  274. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  275. }
  276. inverse_DCT = cinfo->idct->inverse_DCT[ci];
  277. output_ptr = output_buf[ci];
  278. /* Loop over all DCT blocks to be processed. */
  279. for (block_row = 0; block_row < block_rows; block_row++) {
  280. buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];
  281. output_col = 0;
  282. for (block_num = cinfo->master->first_MCU_col[ci];
  283. block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {
  284. (*inverse_DCT) (cinfo, compptr, (JCOEFPTR)buffer_ptr, output_ptr,
  285. output_col);
  286. buffer_ptr++;
  287. output_col += compptr->_DCT_scaled_size;
  288. }
  289. output_ptr += compptr->_DCT_scaled_size;
  290. }
  291. }
  292. if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  293. return JPEG_ROW_COMPLETED;
  294. return JPEG_SCAN_COMPLETED;
  295. }
  296. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  297. #ifdef BLOCK_SMOOTHING_SUPPORTED
  298. /*
  299. * This code applies interblock smoothing; the first 9 AC coefficients are
  300. * estimated from the DC values of a DCT block and its 24 neighboring blocks.
  301. * We apply smoothing only for progressive JPEG decoding, and only if
  302. * the coefficients it can estimate are not yet known to full precision.
  303. */
  304. /* Natural-order array positions of the first 9 zigzag-order coefficients */
  305. #define Q01_POS 1
  306. #define Q10_POS 8
  307. #define Q20_POS 16
  308. #define Q11_POS 9
  309. #define Q02_POS 2
  310. #define Q03_POS 3
  311. #define Q12_POS 10
  312. #define Q21_POS 17
  313. #define Q30_POS 24
  314. /*
  315. * Determine whether block smoothing is applicable and safe.
  316. * We also latch the current states of the coef_bits[] entries for the
  317. * AC coefficients; otherwise, if the input side of the decompressor
  318. * advances into a new scan, we might think the coefficients are known
  319. * more accurately than they really are.
  320. */
  321. LOCAL(boolean)
  322. smoothing_ok(j_decompress_ptr cinfo)
  323. {
  324. my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
  325. boolean smoothing_useful = FALSE;
  326. int ci, coefi;
  327. jpeg_component_info *compptr;
  328. JQUANT_TBL *qtable;
  329. int *coef_bits, *prev_coef_bits;
  330. int *coef_bits_latch, *prev_coef_bits_latch;
  331. if (!cinfo->progressive_mode || cinfo->coef_bits == NULL)
  332. return FALSE;
  333. /* Allocate latch area if not already done */
  334. if (coef->coef_bits_latch == NULL)
  335. coef->coef_bits_latch = (int *)
  336. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  337. cinfo->num_components * 2 *
  338. (SAVED_COEFS * sizeof(int)));
  339. coef_bits_latch = coef->coef_bits_latch;
  340. prev_coef_bits_latch =
  341. &coef->coef_bits_latch[cinfo->num_components * SAVED_COEFS];
  342. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  343. ci++, compptr++) {
  344. /* All components' quantization values must already be latched. */
  345. if ((qtable = compptr->quant_table) == NULL)
  346. return FALSE;
  347. /* Verify DC & first 9 AC quantizers are nonzero to avoid zero-divide. */
  348. if (qtable->quantval[0] == 0 ||
  349. qtable->quantval[Q01_POS] == 0 ||
  350. qtable->quantval[Q10_POS] == 0 ||
  351. qtable->quantval[Q20_POS] == 0 ||
  352. qtable->quantval[Q11_POS] == 0 ||
  353. qtable->quantval[Q02_POS] == 0 ||
  354. qtable->quantval[Q03_POS] == 0 ||
  355. qtable->quantval[Q12_POS] == 0 ||
  356. qtable->quantval[Q21_POS] == 0 ||
  357. qtable->quantval[Q30_POS] == 0)
  358. return FALSE;
  359. /* DC values must be at least partly known for all components. */
  360. coef_bits = cinfo->coef_bits[ci];
  361. prev_coef_bits = cinfo->coef_bits[ci + cinfo->num_components];
  362. if (coef_bits[0] < 0)
  363. return FALSE;
  364. coef_bits_latch[0] = coef_bits[0];
  365. /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
  366. for (coefi = 1; coefi < SAVED_COEFS; coefi++) {
  367. if (cinfo->input_scan_number > 1)
  368. prev_coef_bits_latch[coefi] = prev_coef_bits[coefi];
  369. else
  370. prev_coef_bits_latch[coefi] = -1;
  371. coef_bits_latch[coefi] = coef_bits[coefi];
  372. if (coef_bits[coefi] != 0)
  373. smoothing_useful = TRUE;
  374. }
  375. coef_bits_latch += SAVED_COEFS;
  376. prev_coef_bits_latch += SAVED_COEFS;
  377. }
  378. return smoothing_useful;
  379. }
  380. /*
  381. * Variant of decompress_data for use when doing block smoothing.
  382. */
  383. METHODDEF(int)
  384. decompress_smooth_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  385. {
  386. my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
  387. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  388. JDIMENSION block_num, last_block_column;
  389. int ci, block_row, block_rows, access_rows;
  390. JBLOCKARRAY buffer;
  391. JBLOCKROW buffer_ptr, prev_prev_block_row, prev_block_row;
  392. JBLOCKROW next_block_row, next_next_block_row;
  393. JSAMPARRAY output_ptr;
  394. JDIMENSION output_col;
  395. jpeg_component_info *compptr;
  396. inverse_DCT_method_ptr inverse_DCT;
  397. boolean change_dc;
  398. JCOEF *workspace;
  399. int *coef_bits;
  400. JQUANT_TBL *quanttbl;
  401. JLONG Q00, Q01, Q02, Q03 = 0, Q10, Q11, Q12 = 0, Q20, Q21 = 0, Q30 = 0, num;
  402. int DC01, DC02, DC03, DC04, DC05, DC06, DC07, DC08, DC09, DC10, DC11, DC12,
  403. DC13, DC14, DC15, DC16, DC17, DC18, DC19, DC20, DC21, DC22, DC23, DC24,
  404. DC25;
  405. int Al, pred;
  406. /* Keep a local variable to avoid looking it up more than once */
  407. workspace = coef->workspace;
  408. /* Force some input to be done if we are getting ahead of the input. */
  409. while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  410. !cinfo->inputctl->eoi_reached) {
  411. if (cinfo->input_scan_number == cinfo->output_scan_number) {
  412. /* If input is working on current scan, we ordinarily want it to
  413. * have completed the current row. But if input scan is DC,
  414. * we want it to keep two rows ahead so that next two block rows' DC
  415. * values are up to date.
  416. */
  417. JDIMENSION delta = (cinfo->Ss == 0) ? 2 : 0;
  418. if (cinfo->input_iMCU_row > cinfo->output_iMCU_row + delta)
  419. break;
  420. }
  421. if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  422. return JPEG_SUSPENDED;
  423. }
  424. /* OK, output from the virtual arrays. */
  425. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  426. ci++, compptr++) {
  427. /* Don't bother to IDCT an uninteresting component. */
  428. if (!compptr->component_needed)
  429. continue;
  430. /* Count non-dummy DCT block rows in this iMCU row. */
  431. if (cinfo->output_iMCU_row + 1 < last_iMCU_row) {
  432. block_rows = compptr->v_samp_factor;
  433. access_rows = block_rows * 3; /* this and next two iMCU rows */
  434. } else if (cinfo->output_iMCU_row < last_iMCU_row) {
  435. block_rows = compptr->v_samp_factor;
  436. access_rows = block_rows * 2; /* this and next iMCU row */
  437. } else {
  438. /* NB: can't use last_row_height here; it is input-side-dependent! */
  439. block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
  440. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  441. access_rows = block_rows; /* this iMCU row only */
  442. }
  443. /* Align the virtual buffer for this component. */
  444. if (cinfo->output_iMCU_row > 1) {
  445. access_rows += 2 * compptr->v_samp_factor; /* prior two iMCU rows too */
  446. buffer = (*cinfo->mem->access_virt_barray)
  447. ((j_common_ptr)cinfo, coef->whole_image[ci],
  448. (cinfo->output_iMCU_row - 2) * compptr->v_samp_factor,
  449. (JDIMENSION)access_rows, FALSE);
  450. buffer += 2 * compptr->v_samp_factor; /* point to current iMCU row */
  451. } else if (cinfo->output_iMCU_row > 0) {
  452. buffer = (*cinfo->mem->access_virt_barray)
  453. ((j_common_ptr)cinfo, coef->whole_image[ci],
  454. (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
  455. (JDIMENSION)access_rows, FALSE);
  456. buffer += compptr->v_samp_factor; /* point to current iMCU row */
  457. } else {
  458. buffer = (*cinfo->mem->access_virt_barray)
  459. ((j_common_ptr)cinfo, coef->whole_image[ci],
  460. (JDIMENSION)0, (JDIMENSION)access_rows, FALSE);
  461. }
  462. /* Fetch component-dependent info.
  463. * If the current scan is incomplete, then we use the component-dependent
  464. * info from the previous scan.
  465. */
  466. if (cinfo->output_iMCU_row > cinfo->master->last_good_iMCU_row)
  467. coef_bits =
  468. coef->coef_bits_latch + ((ci + cinfo->num_components) * SAVED_COEFS);
  469. else
  470. coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
  471. /* We only do DC interpolation if no AC coefficient data is available. */
  472. change_dc =
  473. coef_bits[1] == -1 && coef_bits[2] == -1 && coef_bits[3] == -1 &&
  474. coef_bits[4] == -1 && coef_bits[5] == -1 && coef_bits[6] == -1 &&
  475. coef_bits[7] == -1 && coef_bits[8] == -1 && coef_bits[9] == -1;
  476. quanttbl = compptr->quant_table;
  477. Q00 = quanttbl->quantval[0];
  478. Q01 = quanttbl->quantval[Q01_POS];
  479. Q10 = quanttbl->quantval[Q10_POS];
  480. Q20 = quanttbl->quantval[Q20_POS];
  481. Q11 = quanttbl->quantval[Q11_POS];
  482. Q02 = quanttbl->quantval[Q02_POS];
  483. if (change_dc) {
  484. Q03 = quanttbl->quantval[Q03_POS];
  485. Q12 = quanttbl->quantval[Q12_POS];
  486. Q21 = quanttbl->quantval[Q21_POS];
  487. Q30 = quanttbl->quantval[Q30_POS];
  488. }
  489. inverse_DCT = cinfo->idct->inverse_DCT[ci];
  490. output_ptr = output_buf[ci];
  491. /* Loop over all DCT blocks to be processed. */
  492. for (block_row = 0; block_row < block_rows; block_row++) {
  493. buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];
  494. if (block_row > 0 || cinfo->output_iMCU_row > 0)
  495. prev_block_row =
  496. buffer[block_row - 1] + cinfo->master->first_MCU_col[ci];
  497. else
  498. prev_block_row = buffer_ptr;
  499. if (block_row > 1 || cinfo->output_iMCU_row > 1)
  500. prev_prev_block_row =
  501. buffer[block_row - 2] + cinfo->master->first_MCU_col[ci];
  502. else
  503. prev_prev_block_row = prev_block_row;
  504. if (block_row < block_rows - 1 || cinfo->output_iMCU_row < last_iMCU_row)
  505. next_block_row =
  506. buffer[block_row + 1] + cinfo->master->first_MCU_col[ci];
  507. else
  508. next_block_row = buffer_ptr;
  509. if (block_row < block_rows - 2 ||
  510. cinfo->output_iMCU_row + 1 < last_iMCU_row)
  511. next_next_block_row =
  512. buffer[block_row + 2] + cinfo->master->first_MCU_col[ci];
  513. else
  514. next_next_block_row = next_block_row;
  515. /* We fetch the surrounding DC values using a sliding-register approach.
  516. * Initialize all 25 here so as to do the right thing on narrow pics.
  517. */
  518. DC01 = DC02 = DC03 = DC04 = DC05 = (int)prev_prev_block_row[0][0];
  519. DC06 = DC07 = DC08 = DC09 = DC10 = (int)prev_block_row[0][0];
  520. DC11 = DC12 = DC13 = DC14 = DC15 = (int)buffer_ptr[0][0];
  521. DC16 = DC17 = DC18 = DC19 = DC20 = (int)next_block_row[0][0];
  522. DC21 = DC22 = DC23 = DC24 = DC25 = (int)next_next_block_row[0][0];
  523. output_col = 0;
  524. last_block_column = compptr->width_in_blocks - 1;
  525. for (block_num = cinfo->master->first_MCU_col[ci];
  526. block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {
  527. /* Fetch current DCT block into workspace so we can modify it. */
  528. jcopy_block_row(buffer_ptr, (JBLOCKROW)workspace, (JDIMENSION)1);
  529. /* Update DC values */
  530. if (block_num == cinfo->master->first_MCU_col[ci] &&
  531. block_num < last_block_column) {
  532. DC04 = (int)prev_prev_block_row[1][0];
  533. DC09 = (int)prev_block_row[1][0];
  534. DC14 = (int)buffer_ptr[1][0];
  535. DC19 = (int)next_block_row[1][0];
  536. DC24 = (int)next_next_block_row[1][0];
  537. }
  538. if (block_num + 1 < last_block_column) {
  539. DC05 = (int)prev_prev_block_row[2][0];
  540. DC10 = (int)prev_block_row[2][0];
  541. DC15 = (int)buffer_ptr[2][0];
  542. DC20 = (int)next_block_row[2][0];
  543. DC25 = (int)next_next_block_row[2][0];
  544. }
  545. /* If DC interpolation is enabled, compute coefficient estimates using
  546. * a Gaussian-like kernel, keeping the averages of the DC values.
  547. *
  548. * If DC interpolation is disabled, compute coefficient estimates using
  549. * an algorithm similar to the one described in Section K.8 of the JPEG
  550. * standard, except applied to a 5x5 window rather than a 3x3 window.
  551. *
  552. * An estimate is applied only if the coefficient is still zero and is
  553. * not known to be fully accurate.
  554. */
  555. /* AC01 */
  556. if ((Al = coef_bits[1]) != 0 && workspace[1] == 0) {
  557. num = Q00 * (change_dc ?
  558. (-DC01 - DC02 + DC04 + DC05 - 3 * DC06 + 13 * DC07 -
  559. 13 * DC09 + 3 * DC10 - 3 * DC11 + 38 * DC12 - 38 * DC14 +
  560. 3 * DC15 - 3 * DC16 + 13 * DC17 - 13 * DC19 + 3 * DC20 -
  561. DC21 - DC22 + DC24 + DC25) :
  562. (-7 * DC11 + 50 * DC12 - 50 * DC14 + 7 * DC15));
  563. if (num >= 0) {
  564. pred = (int)(((Q01 << 7) + num) / (Q01 << 8));
  565. if (Al > 0 && pred >= (1 << Al))
  566. pred = (1 << Al) - 1;
  567. } else {
  568. pred = (int)(((Q01 << 7) - num) / (Q01 << 8));
  569. if (Al > 0 && pred >= (1 << Al))
  570. pred = (1 << Al) - 1;
  571. pred = -pred;
  572. }
  573. workspace[1] = (JCOEF)pred;
  574. }
  575. /* AC10 */
  576. if ((Al = coef_bits[2]) != 0 && workspace[8] == 0) {
  577. num = Q00 * (change_dc ?
  578. (-DC01 - 3 * DC02 - 3 * DC03 - 3 * DC04 - DC05 - DC06 +
  579. 13 * DC07 + 38 * DC08 + 13 * DC09 - DC10 + DC16 -
  580. 13 * DC17 - 38 * DC18 - 13 * DC19 + DC20 + DC21 +
  581. 3 * DC22 + 3 * DC23 + 3 * DC24 + DC25) :
  582. (-7 * DC03 + 50 * DC08 - 50 * DC18 + 7 * DC23));
  583. if (num >= 0) {
  584. pred = (int)(((Q10 << 7) + num) / (Q10 << 8));
  585. if (Al > 0 && pred >= (1 << Al))
  586. pred = (1 << Al) - 1;
  587. } else {
  588. pred = (int)(((Q10 << 7) - num) / (Q10 << 8));
  589. if (Al > 0 && pred >= (1 << Al))
  590. pred = (1 << Al) - 1;
  591. pred = -pred;
  592. }
  593. workspace[8] = (JCOEF)pred;
  594. }
  595. /* AC20 */
  596. if ((Al = coef_bits[3]) != 0 && workspace[16] == 0) {
  597. num = Q00 * (change_dc ?
  598. (DC03 + 2 * DC07 + 7 * DC08 + 2 * DC09 - 5 * DC12 - 14 * DC13 -
  599. 5 * DC14 + 2 * DC17 + 7 * DC18 + 2 * DC19 + DC23) :
  600. (-DC03 + 13 * DC08 - 24 * DC13 + 13 * DC18 - DC23));
  601. if (num >= 0) {
  602. pred = (int)(((Q20 << 7) + num) / (Q20 << 8));
  603. if (Al > 0 && pred >= (1 << Al))
  604. pred = (1 << Al) - 1;
  605. } else {
  606. pred = (int)(((Q20 << 7) - num) / (Q20 << 8));
  607. if (Al > 0 && pred >= (1 << Al))
  608. pred = (1 << Al) - 1;
  609. pred = -pred;
  610. }
  611. workspace[16] = (JCOEF)pred;
  612. }
  613. /* AC11 */
  614. if ((Al = coef_bits[4]) != 0 && workspace[9] == 0) {
  615. num = Q00 * (change_dc ?
  616. (-DC01 + DC05 + 9 * DC07 - 9 * DC09 - 9 * DC17 +
  617. 9 * DC19 + DC21 - DC25) :
  618. (DC10 + DC16 - 10 * DC17 + 10 * DC19 - DC02 - DC20 + DC22 -
  619. DC24 + DC04 - DC06 + 10 * DC07 - 10 * DC09));
  620. if (num >= 0) {
  621. pred = (int)(((Q11 << 7) + num) / (Q11 << 8));
  622. if (Al > 0 && pred >= (1 << Al))
  623. pred = (1 << Al) - 1;
  624. } else {
  625. pred = (int)(((Q11 << 7) - num) / (Q11 << 8));
  626. if (Al > 0 && pred >= (1 << Al))
  627. pred = (1 << Al) - 1;
  628. pred = -pred;
  629. }
  630. workspace[9] = (JCOEF)pred;
  631. }
  632. /* AC02 */
  633. if ((Al = coef_bits[5]) != 0 && workspace[2] == 0) {
  634. num = Q00 * (change_dc ?
  635. (2 * DC07 - 5 * DC08 + 2 * DC09 + DC11 + 7 * DC12 - 14 * DC13 +
  636. 7 * DC14 + DC15 + 2 * DC17 - 5 * DC18 + 2 * DC19) :
  637. (-DC11 + 13 * DC12 - 24 * DC13 + 13 * DC14 - DC15));
  638. if (num >= 0) {
  639. pred = (int)(((Q02 << 7) + num) / (Q02 << 8));
  640. if (Al > 0 && pred >= (1 << Al))
  641. pred = (1 << Al) - 1;
  642. } else {
  643. pred = (int)(((Q02 << 7) - num) / (Q02 << 8));
  644. if (Al > 0 && pred >= (1 << Al))
  645. pred = (1 << Al) - 1;
  646. pred = -pred;
  647. }
  648. workspace[2] = (JCOEF)pred;
  649. }
  650. if (change_dc) {
  651. /* AC03 */
  652. if ((Al = coef_bits[6]) != 0 && workspace[3] == 0) {
  653. num = Q00 * (DC07 - DC09 + 2 * DC12 - 2 * DC14 + DC17 - DC19);
  654. if (num >= 0) {
  655. pred = (int)(((Q03 << 7) + num) / (Q03 << 8));
  656. if (Al > 0 && pred >= (1 << Al))
  657. pred = (1 << Al) - 1;
  658. } else {
  659. pred = (int)(((Q03 << 7) - num) / (Q03 << 8));
  660. if (Al > 0 && pred >= (1 << Al))
  661. pred = (1 << Al) - 1;
  662. pred = -pred;
  663. }
  664. workspace[3] = (JCOEF)pred;
  665. }
  666. /* AC12 */
  667. if ((Al = coef_bits[7]) != 0 && workspace[10] == 0) {
  668. num = Q00 * (DC07 - 3 * DC08 + DC09 - DC17 + 3 * DC18 - DC19);
  669. if (num >= 0) {
  670. pred = (int)(((Q12 << 7) + num) / (Q12 << 8));
  671. if (Al > 0 && pred >= (1 << Al))
  672. pred = (1 << Al) - 1;
  673. } else {
  674. pred = (int)(((Q12 << 7) - num) / (Q12 << 8));
  675. if (Al > 0 && pred >= (1 << Al))
  676. pred = (1 << Al) - 1;
  677. pred = -pred;
  678. }
  679. workspace[10] = (JCOEF)pred;
  680. }
  681. /* AC21 */
  682. if ((Al = coef_bits[8]) != 0 && workspace[17] == 0) {
  683. num = Q00 * (DC07 - DC09 - 3 * DC12 + 3 * DC14 + DC17 - DC19);
  684. if (num >= 0) {
  685. pred = (int)(((Q21 << 7) + num) / (Q21 << 8));
  686. if (Al > 0 && pred >= (1 << Al))
  687. pred = (1 << Al) - 1;
  688. } else {
  689. pred = (int)(((Q21 << 7) - num) / (Q21 << 8));
  690. if (Al > 0 && pred >= (1 << Al))
  691. pred = (1 << Al) - 1;
  692. pred = -pred;
  693. }
  694. workspace[17] = (JCOEF)pred;
  695. }
  696. /* AC30 */
  697. if ((Al = coef_bits[9]) != 0 && workspace[24] == 0) {
  698. num = Q00 * (DC07 + 2 * DC08 + DC09 - DC17 - 2 * DC18 - DC19);
  699. if (num >= 0) {
  700. pred = (int)(((Q30 << 7) + num) / (Q30 << 8));
  701. if (Al > 0 && pred >= (1 << Al))
  702. pred = (1 << Al) - 1;
  703. } else {
  704. pred = (int)(((Q30 << 7) - num) / (Q30 << 8));
  705. if (Al > 0 && pred >= (1 << Al))
  706. pred = (1 << Al) - 1;
  707. pred = -pred;
  708. }
  709. workspace[24] = (JCOEF)pred;
  710. }
  711. /* coef_bits[0] is non-negative. Otherwise this function would not
  712. * be called.
  713. */
  714. num = Q00 *
  715. (-2 * DC01 - 6 * DC02 - 8 * DC03 - 6 * DC04 - 2 * DC05 -
  716. 6 * DC06 + 6 * DC07 + 42 * DC08 + 6 * DC09 - 6 * DC10 -
  717. 8 * DC11 + 42 * DC12 + 152 * DC13 + 42 * DC14 - 8 * DC15 -
  718. 6 * DC16 + 6 * DC17 + 42 * DC18 + 6 * DC19 - 6 * DC20 -
  719. 2 * DC21 - 6 * DC22 - 8 * DC23 - 6 * DC24 - 2 * DC25);
  720. if (num >= 0) {
  721. pred = (int)(((Q00 << 7) + num) / (Q00 << 8));
  722. } else {
  723. pred = (int)(((Q00 << 7) - num) / (Q00 << 8));
  724. pred = -pred;
  725. }
  726. workspace[0] = (JCOEF)pred;
  727. } /* change_dc */
  728. /* OK, do the IDCT */
  729. (*inverse_DCT) (cinfo, compptr, (JCOEFPTR)workspace, output_ptr,
  730. output_col);
  731. /* Advance for next column */
  732. DC01 = DC02; DC02 = DC03; DC03 = DC04; DC04 = DC05;
  733. DC06 = DC07; DC07 = DC08; DC08 = DC09; DC09 = DC10;
  734. DC11 = DC12; DC12 = DC13; DC13 = DC14; DC14 = DC15;
  735. DC16 = DC17; DC17 = DC18; DC18 = DC19; DC19 = DC20;
  736. DC21 = DC22; DC22 = DC23; DC23 = DC24; DC24 = DC25;
  737. buffer_ptr++, prev_block_row++, next_block_row++,
  738. prev_prev_block_row++, next_next_block_row++;
  739. output_col += compptr->_DCT_scaled_size;
  740. }
  741. output_ptr += compptr->_DCT_scaled_size;
  742. }
  743. }
  744. if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  745. return JPEG_ROW_COMPLETED;
  746. return JPEG_SCAN_COMPLETED;
  747. }
  748. #endif /* BLOCK_SMOOTHING_SUPPORTED */
  749. /*
  750. * Initialize coefficient buffer controller.
  751. */
  752. GLOBAL(void)
  753. jinit_d_coef_controller(j_decompress_ptr cinfo, boolean need_full_buffer)
  754. {
  755. my_coef_ptr coef;
  756. coef = (my_coef_ptr)
  757. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  758. sizeof(my_coef_controller));
  759. cinfo->coef = (struct jpeg_d_coef_controller *)coef;
  760. coef->pub.start_input_pass = start_input_pass;
  761. coef->pub.start_output_pass = start_output_pass;
  762. #ifdef BLOCK_SMOOTHING_SUPPORTED
  763. coef->coef_bits_latch = NULL;
  764. #endif
  765. /* Create the coefficient buffer. */
  766. if (need_full_buffer) {
  767. #ifdef D_MULTISCAN_FILES_SUPPORTED
  768. /* Allocate a full-image virtual array for each component, */
  769. /* padded to a multiple of samp_factor DCT blocks in each direction. */
  770. /* Note we ask for a pre-zeroed array. */
  771. int ci, access_rows;
  772. jpeg_component_info *compptr;
  773. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  774. ci++, compptr++) {
  775. access_rows = compptr->v_samp_factor;
  776. #ifdef BLOCK_SMOOTHING_SUPPORTED
  777. /* If block smoothing could be used, need a bigger window */
  778. if (cinfo->progressive_mode)
  779. access_rows *= 5;
  780. #endif
  781. coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  782. ((j_common_ptr)cinfo, JPOOL_IMAGE, TRUE,
  783. (JDIMENSION)jround_up((long)compptr->width_in_blocks,
  784. (long)compptr->h_samp_factor),
  785. (JDIMENSION)jround_up((long)compptr->height_in_blocks,
  786. (long)compptr->v_samp_factor),
  787. (JDIMENSION)access_rows);
  788. }
  789. coef->pub.consume_data = consume_data;
  790. coef->pub.decompress_data = decompress_data;
  791. coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
  792. #else
  793. ERREXIT(cinfo, JERR_NOT_COMPILED);
  794. #endif
  795. } else {
  796. /* We only need a single-MCU buffer. */
  797. JBLOCKROW buffer;
  798. int i;
  799. buffer = (JBLOCKROW)
  800. (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  801. D_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
  802. for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
  803. coef->MCU_buffer[i] = buffer + i;
  804. }
  805. coef->pub.consume_data = dummy_consume_data;
  806. coef->pub.decompress_data = decompress_onepass;
  807. coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
  808. }
  809. /* Allocate the workspace buffer */
  810. coef->workspace = (JCOEF *)
  811. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  812. sizeof(JCOEF) * DCTSIZE2);
  813. }