jcmaster.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. /*
  2. * jcmaster.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1997, Thomas G. Lane.
  6. * Modified 2003-2010 by Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2010, 2016, 2018, D. R. Commander.
  9. * mozjpeg Modifications:
  10. * Copyright (C) 2014, Mozilla Corporation.
  11. * For conditions of distribution and use, see the accompanying README file.
  12. *
  13. * This file contains master control logic for the JPEG compressor.
  14. * These routines are concerned with parameter validation, initial setup,
  15. * and inter-pass control (determining the number of passes and the work
  16. * to be done in each pass).
  17. */
  18. #define JPEG_INTERNALS
  19. #include "jinclude.h"
  20. #include "jpeglib.h"
  21. #include "jpegcomp.h"
  22. #include "jconfigint.h"
  23. #include "jmemsys.h"
  24. #include "jcmaster.h"
  25. /*
  26. * Support routines that do various essential calculations.
  27. */
  28. #if JPEG_LIB_VERSION >= 70
  29. /*
  30. * Compute JPEG image dimensions and related values.
  31. * NOTE: this is exported for possible use by application.
  32. * Hence it mustn't do anything that can't be done twice.
  33. */
  34. GLOBAL(void)
  35. jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
  36. /* Do computations that are needed before master selection phase */
  37. {
  38. /* Hardwire it to "no scaling" */
  39. cinfo->jpeg_width = cinfo->image_width;
  40. cinfo->jpeg_height = cinfo->image_height;
  41. cinfo->min_DCT_h_scaled_size = DCTSIZE;
  42. cinfo->min_DCT_v_scaled_size = DCTSIZE;
  43. }
  44. #endif
  45. LOCAL(void)
  46. initial_setup (j_compress_ptr cinfo, boolean transcode_only)
  47. /* Do computations that are needed before master selection phase */
  48. {
  49. int ci;
  50. jpeg_component_info *compptr;
  51. long samplesperrow;
  52. JDIMENSION jd_samplesperrow;
  53. #if JPEG_LIB_VERSION >= 70
  54. #if JPEG_LIB_VERSION >= 80
  55. if (!transcode_only)
  56. #endif
  57. jpeg_calc_jpeg_dimensions(cinfo);
  58. #endif
  59. /* Sanity check on image dimensions */
  60. if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 ||
  61. cinfo->num_components <= 0 || cinfo->input_components <= 0)
  62. ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  63. /* Make sure image isn't bigger than I can handle */
  64. if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION ||
  65. (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION)
  66. ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  67. /* Width of an input scanline must be representable as JDIMENSION. */
  68. samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
  69. jd_samplesperrow = (JDIMENSION) samplesperrow;
  70. if ((long) jd_samplesperrow != samplesperrow)
  71. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  72. /* For now, precision must match compiled-in value... */
  73. if (cinfo->data_precision != BITS_IN_JSAMPLE)
  74. ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  75. /* Check that number of components won't exceed internal array sizes */
  76. if (cinfo->num_components > MAX_COMPONENTS)
  77. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  78. MAX_COMPONENTS);
  79. /* Compute maximum sampling factors; check factor validity */
  80. cinfo->max_h_samp_factor = 1;
  81. cinfo->max_v_samp_factor = 1;
  82. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  83. ci++, compptr++) {
  84. if (compptr->h_samp_factor <= 0 ||
  85. compptr->h_samp_factor > MAX_SAMP_FACTOR ||
  86. compptr->v_samp_factor <= 0 ||
  87. compptr->v_samp_factor > MAX_SAMP_FACTOR)
  88. ERREXIT(cinfo, JERR_BAD_SAMPLING);
  89. cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  90. compptr->h_samp_factor);
  91. cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  92. compptr->v_samp_factor);
  93. }
  94. /* Compute dimensions of components */
  95. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  96. ci++, compptr++) {
  97. /* Fill in the correct component_index value; don't rely on application */
  98. compptr->component_index = ci;
  99. /* For compression, we never do DCT scaling. */
  100. #if JPEG_LIB_VERSION >= 70
  101. compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
  102. #else
  103. compptr->DCT_scaled_size = DCTSIZE;
  104. #endif
  105. /* Size in DCT blocks */
  106. compptr->width_in_blocks = (JDIMENSION)
  107. jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
  108. (long) (cinfo->max_h_samp_factor * DCTSIZE));
  109. compptr->height_in_blocks = (JDIMENSION)
  110. jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
  111. (long) (cinfo->max_v_samp_factor * DCTSIZE));
  112. /* Size in samples */
  113. compptr->downsampled_width = (JDIMENSION)
  114. jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
  115. (long) cinfo->max_h_samp_factor);
  116. compptr->downsampled_height = (JDIMENSION)
  117. jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
  118. (long) cinfo->max_v_samp_factor);
  119. /* Mark component needed (this flag isn't actually used for compression) */
  120. compptr->component_needed = TRUE;
  121. }
  122. /* Compute number of fully interleaved MCU rows (number of times that
  123. * main controller will call coefficient controller).
  124. */
  125. cinfo->total_iMCU_rows = (JDIMENSION)
  126. jdiv_round_up((long) cinfo->_jpeg_height,
  127. (long) (cinfo->max_v_samp_factor*DCTSIZE));
  128. }
  129. #ifdef C_MULTISCAN_FILES_SUPPORTED
  130. LOCAL(void)
  131. validate_script (j_compress_ptr cinfo)
  132. /* Verify that the scan script in cinfo->scan_info[] is valid; also
  133. * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
  134. */
  135. {
  136. const jpeg_scan_info *scanptr;
  137. int scanno, ncomps, ci, coefi, thisi;
  138. int Ss, Se, Ah, Al;
  139. boolean component_sent[MAX_COMPONENTS];
  140. #ifdef C_PROGRESSIVE_SUPPORTED
  141. int *last_bitpos_ptr;
  142. int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
  143. /* -1 until that coefficient has been seen; then last Al for it */
  144. #endif
  145. if (cinfo->master->optimize_scans) {
  146. cinfo->progressive_mode = TRUE;
  147. /* When we optimize scans, there is redundancy in the scan list
  148. * and this function will fail. Therefore skip all this checking
  149. */
  150. return;
  151. }
  152. if (cinfo->num_scans <= 0)
  153. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
  154. /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
  155. * for progressive JPEG, no scan can have this.
  156. */
  157. scanptr = cinfo->scan_info;
  158. if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
  159. #ifdef C_PROGRESSIVE_SUPPORTED
  160. cinfo->progressive_mode = TRUE;
  161. last_bitpos_ptr = & last_bitpos[0][0];
  162. for (ci = 0; ci < cinfo->num_components; ci++)
  163. for (coefi = 0; coefi < DCTSIZE2; coefi++)
  164. *last_bitpos_ptr++ = -1;
  165. #else
  166. ERREXIT(cinfo, JERR_NOT_COMPILED);
  167. #endif
  168. } else {
  169. cinfo->progressive_mode = FALSE;
  170. for (ci = 0; ci < cinfo->num_components; ci++)
  171. component_sent[ci] = FALSE;
  172. }
  173. for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
  174. /* Validate component indexes */
  175. ncomps = scanptr->comps_in_scan;
  176. if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
  177. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
  178. for (ci = 0; ci < ncomps; ci++) {
  179. thisi = scanptr->component_index[ci];
  180. if (thisi < 0 || thisi >= cinfo->num_components)
  181. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  182. /* Components must appear in SOF order within each scan */
  183. if (ci > 0 && thisi <= scanptr->component_index[ci-1])
  184. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  185. }
  186. /* Validate progression parameters */
  187. Ss = scanptr->Ss;
  188. Se = scanptr->Se;
  189. Ah = scanptr->Ah;
  190. Al = scanptr->Al;
  191. if (cinfo->progressive_mode) {
  192. #ifdef C_PROGRESSIVE_SUPPORTED
  193. /* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah
  194. * and Al, but that seems wrong: the upper bound ought to depend on data
  195. * precision. Perhaps they really meant 0..N+1 for N-bit precision.
  196. * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
  197. * out-of-range reconstructed DC values during the first DC scan,
  198. * which might cause problems for some decoders.
  199. */
  200. #if BITS_IN_JSAMPLE == 8
  201. #define MAX_AH_AL 10
  202. #else
  203. #define MAX_AH_AL 13
  204. #endif
  205. if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
  206. Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
  207. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  208. if (Ss == 0) {
  209. if (Se != 0) /* DC and AC together not OK */
  210. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  211. } else {
  212. if (ncomps != 1) /* AC scans must be for only one component */
  213. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  214. }
  215. for (ci = 0; ci < ncomps; ci++) {
  216. last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
  217. if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
  218. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  219. for (coefi = Ss; coefi <= Se; coefi++) {
  220. if (last_bitpos_ptr[coefi] < 0) {
  221. /* first scan of this coefficient */
  222. if (Ah != 0)
  223. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  224. } else {
  225. /* not first scan */
  226. if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
  227. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  228. }
  229. last_bitpos_ptr[coefi] = Al;
  230. }
  231. }
  232. #endif
  233. } else {
  234. /* For sequential JPEG, all progression parameters must be these: */
  235. if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
  236. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  237. /* Make sure components are not sent twice */
  238. for (ci = 0; ci < ncomps; ci++) {
  239. thisi = scanptr->component_index[ci];
  240. if (component_sent[thisi])
  241. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  242. component_sent[thisi] = TRUE;
  243. }
  244. }
  245. }
  246. /* Now verify that everything got sent. */
  247. if (cinfo->progressive_mode) {
  248. #ifdef C_PROGRESSIVE_SUPPORTED
  249. /* For progressive mode, we only check that at least some DC data
  250. * got sent for each component; the spec does not require that all bits
  251. * of all coefficients be transmitted. Would it be wiser to enforce
  252. * transmission of all coefficient bits??
  253. */
  254. for (ci = 0; ci < cinfo->num_components; ci++) {
  255. if (last_bitpos[ci][0] < 0)
  256. ERREXIT(cinfo, JERR_MISSING_DATA);
  257. }
  258. #endif
  259. } else {
  260. for (ci = 0; ci < cinfo->num_components; ci++) {
  261. if (! component_sent[ci])
  262. ERREXIT(cinfo, JERR_MISSING_DATA);
  263. }
  264. }
  265. }
  266. #endif /* C_MULTISCAN_FILES_SUPPORTED */
  267. LOCAL(void)
  268. select_scan_parameters (j_compress_ptr cinfo)
  269. /* Set up the scan parameters for the current scan */
  270. {
  271. int ci;
  272. #ifdef C_MULTISCAN_FILES_SUPPORTED
  273. my_master_ptr master = (my_master_ptr) cinfo->master;
  274. if (master->pass_number < master->pass_number_scan_opt_base) {
  275. cinfo->comps_in_scan = 1;
  276. if (cinfo->master->use_scans_in_trellis) {
  277. cinfo->cur_comp_info[0] =
  278. &cinfo->comp_info[master->pass_number /
  279. (4 * cinfo->master->trellis_num_loops)];
  280. cinfo->Ss = (master->pass_number % 4 < 2) ?
  281. 1 : cinfo->master->trellis_freq_split + 1;
  282. cinfo->Se = (master->pass_number % 4 < 2) ?
  283. cinfo->master->trellis_freq_split : DCTSIZE2 - 1;
  284. } else {
  285. cinfo->cur_comp_info[0] =
  286. &cinfo->comp_info[master->pass_number /
  287. (2 * cinfo->master->trellis_num_loops)];
  288. cinfo->Ss = 1;
  289. cinfo->Se = DCTSIZE2-1;
  290. }
  291. }
  292. else if (cinfo->scan_info != NULL) {
  293. /* Prepare for current scan --- the script is already validated */
  294. const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number;
  295. cinfo->comps_in_scan = scanptr->comps_in_scan;
  296. for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
  297. cinfo->cur_comp_info[ci] =
  298. &cinfo->comp_info[scanptr->component_index[ci]];
  299. }
  300. cinfo->Ss = scanptr->Ss;
  301. cinfo->Se = scanptr->Se;
  302. cinfo->Ah = scanptr->Ah;
  303. cinfo->Al = scanptr->Al;
  304. if (cinfo->master->optimize_scans) {
  305. /* luma frequency split passes */
  306. if (master->scan_number >= cinfo->master->num_scans_luma_dc +
  307. 3 * cinfo->master->Al_max_luma + 2 &&
  308. master->scan_number < cinfo->master->num_scans_luma)
  309. cinfo->Al = master->best_Al_luma;
  310. /* chroma frequency split passes */
  311. if (master->scan_number >= cinfo->master->num_scans_luma +
  312. cinfo->master->num_scans_chroma_dc +
  313. (6 * cinfo->master->Al_max_chroma + 4) &&
  314. master->scan_number < cinfo->num_scans)
  315. cinfo->Al = master->best_Al_chroma;
  316. }
  317. /* save value for later retrieval during printout of scans */
  318. master->actual_Al[master->scan_number] = cinfo->Al;
  319. }
  320. else
  321. #endif
  322. {
  323. /* Prepare for single sequential-JPEG scan containing all components */
  324. if (cinfo->num_components > MAX_COMPS_IN_SCAN)
  325. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  326. MAX_COMPS_IN_SCAN);
  327. cinfo->comps_in_scan = cinfo->num_components;
  328. for (ci = 0; ci < cinfo->num_components; ci++) {
  329. cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  330. }
  331. cinfo->Ss = 0;
  332. cinfo->Se = DCTSIZE2-1;
  333. cinfo->Ah = 0;
  334. cinfo->Al = 0;
  335. }
  336. }
  337. LOCAL(void)
  338. per_scan_setup (j_compress_ptr cinfo)
  339. /* Do computations that are needed before processing a JPEG scan */
  340. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  341. {
  342. int ci, mcublks, tmp;
  343. jpeg_component_info *compptr;
  344. if (cinfo->comps_in_scan == 1) {
  345. /* Noninterleaved (single-component) scan */
  346. compptr = cinfo->cur_comp_info[0];
  347. /* Overall image size in MCUs */
  348. cinfo->MCUs_per_row = compptr->width_in_blocks;
  349. cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  350. /* For noninterleaved scan, always one block per MCU */
  351. compptr->MCU_width = 1;
  352. compptr->MCU_height = 1;
  353. compptr->MCU_blocks = 1;
  354. compptr->MCU_sample_width = DCTSIZE;
  355. compptr->last_col_width = 1;
  356. /* For noninterleaved scans, it is convenient to define last_row_height
  357. * as the number of block rows present in the last iMCU row.
  358. */
  359. tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  360. if (tmp == 0) tmp = compptr->v_samp_factor;
  361. compptr->last_row_height = tmp;
  362. /* Prepare array describing MCU composition */
  363. cinfo->blocks_in_MCU = 1;
  364. cinfo->MCU_membership[0] = 0;
  365. } else {
  366. /* Interleaved (multi-component) scan */
  367. if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  368. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  369. MAX_COMPS_IN_SCAN);
  370. /* Overall image size in MCUs */
  371. cinfo->MCUs_per_row = (JDIMENSION)
  372. jdiv_round_up((long) cinfo->_jpeg_width,
  373. (long) (cinfo->max_h_samp_factor*DCTSIZE));
  374. cinfo->MCU_rows_in_scan = (JDIMENSION)
  375. jdiv_round_up((long) cinfo->_jpeg_height,
  376. (long) (cinfo->max_v_samp_factor*DCTSIZE));
  377. cinfo->blocks_in_MCU = 0;
  378. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  379. compptr = cinfo->cur_comp_info[ci];
  380. /* Sampling factors give # of blocks of component in each MCU */
  381. compptr->MCU_width = compptr->h_samp_factor;
  382. compptr->MCU_height = compptr->v_samp_factor;
  383. compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  384. compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
  385. /* Figure number of non-dummy blocks in last MCU column & row */
  386. tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  387. if (tmp == 0) tmp = compptr->MCU_width;
  388. compptr->last_col_width = tmp;
  389. tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  390. if (tmp == 0) tmp = compptr->MCU_height;
  391. compptr->last_row_height = tmp;
  392. /* Prepare array describing MCU composition */
  393. mcublks = compptr->MCU_blocks;
  394. if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
  395. ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  396. while (mcublks-- > 0) {
  397. cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  398. }
  399. }
  400. }
  401. /* Convert restart specified in rows to actual MCU count. */
  402. /* Note that count must fit in 16 bits, so we provide limiting. */
  403. if (cinfo->restart_in_rows > 0) {
  404. long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  405. cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  406. }
  407. }
  408. /*
  409. * Per-pass setup.
  410. * This is called at the beginning of each pass. We determine which modules
  411. * will be active during this pass and give them appropriate start_pass calls.
  412. * We also set is_last_pass to indicate whether any more passes will be
  413. * required.
  414. */
  415. METHODDEF(void)
  416. prepare_for_pass (j_compress_ptr cinfo)
  417. {
  418. my_master_ptr master = (my_master_ptr) cinfo->master;
  419. cinfo->master->trellis_passes =
  420. master->pass_number < master->pass_number_scan_opt_base;
  421. switch (master->pass_type) {
  422. case main_pass:
  423. /* Initial pass: will collect input data, and do either Huffman
  424. * optimization or data output for the first scan.
  425. */
  426. select_scan_parameters(cinfo);
  427. per_scan_setup(cinfo);
  428. if (! cinfo->raw_data_in) {
  429. (*cinfo->cconvert->start_pass) (cinfo);
  430. (*cinfo->downsample->start_pass) (cinfo);
  431. (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  432. }
  433. (*cinfo->fdct->start_pass) (cinfo);
  434. (*cinfo->entropy->start_pass) (cinfo, (cinfo->optimize_coding || cinfo->master->trellis_quant) && !cinfo->arith_code);
  435. (*cinfo->coef->start_pass) (cinfo,
  436. (master->total_passes > 1 ?
  437. JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  438. (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  439. if (cinfo->optimize_coding || cinfo->master->trellis_quant) {
  440. /* No immediate data output; postpone writing frame/scan headers */
  441. master->pub.call_pass_startup = FALSE;
  442. } else {
  443. /* Will write frame/scan headers at first jpeg_write_scanlines call */
  444. master->pub.call_pass_startup = TRUE;
  445. }
  446. break;
  447. #ifdef ENTROPY_OPT_SUPPORTED
  448. case huff_opt_pass:
  449. /* Do Huffman optimization for a scan after the first one. */
  450. select_scan_parameters(cinfo);
  451. per_scan_setup(cinfo);
  452. if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
  453. (*cinfo->entropy->start_pass) (cinfo, TRUE);
  454. (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  455. master->pub.call_pass_startup = FALSE;
  456. break;
  457. }
  458. /* Special case: Huffman DC refinement scans need no Huffman table
  459. * and therefore we can skip the optimization pass for them.
  460. */
  461. master->pass_type = output_pass;
  462. master->pass_number++;
  463. #endif
  464. /*FALLTHROUGH*/
  465. case output_pass:
  466. /* Do a data-output pass. */
  467. /* We need not repeat per-scan setup if prior optimization pass did it. */
  468. if (! cinfo->optimize_coding) {
  469. select_scan_parameters(cinfo);
  470. per_scan_setup(cinfo);
  471. }
  472. if (cinfo->master->optimize_scans) {
  473. master->saved_dest = cinfo->dest;
  474. cinfo->dest = NULL;
  475. master->scan_size[master->scan_number] = 0;
  476. jpeg_mem_dest_internal(cinfo, &master->scan_buffer[master->scan_number], &master->scan_size[master->scan_number], JPOOL_IMAGE);
  477. (*cinfo->dest->init_destination)(cinfo);
  478. }
  479. (*cinfo->entropy->start_pass) (cinfo, FALSE);
  480. (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  481. /* We emit frame/scan headers now */
  482. if (master->scan_number == 0)
  483. (*cinfo->marker->write_frame_header) (cinfo);
  484. (*cinfo->marker->write_scan_header) (cinfo);
  485. master->pub.call_pass_startup = FALSE;
  486. break;
  487. case trellis_pass:
  488. if (master->pass_number %
  489. (cinfo->num_components * (cinfo->master->use_scans_in_trellis ? 4 : 2)) == 1 &&
  490. cinfo->master->trellis_q_opt) {
  491. int i, j;
  492. for (i = 0; i < NUM_QUANT_TBLS; i++) {
  493. for (j = 1; j < DCTSIZE2; j++) {
  494. cinfo->master->norm_src[i][j] = 0.0;
  495. cinfo->master->norm_coef[i][j] = 0.0;
  496. }
  497. }
  498. }
  499. (*cinfo->entropy->start_pass) (cinfo, !cinfo->arith_code);
  500. (*cinfo->coef->start_pass) (cinfo, JBUF_REQUANT);
  501. master->pub.call_pass_startup = FALSE;
  502. break;
  503. default:
  504. ERREXIT(cinfo, JERR_NOT_COMPILED);
  505. }
  506. master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
  507. /* Set up progress monitor's pass info if present */
  508. if (cinfo->progress != NULL) {
  509. cinfo->progress->completed_passes = master->pass_number;
  510. cinfo->progress->total_passes = master->total_passes;
  511. }
  512. }
  513. /*
  514. * Special start-of-pass hook.
  515. * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  516. * In single-pass processing, we need this hook because we don't want to
  517. * write frame/scan headers during jpeg_start_compress; we want to let the
  518. * application write COM markers etc. between jpeg_start_compress and the
  519. * jpeg_write_scanlines loop.
  520. * In multi-pass processing, this routine is not used.
  521. */
  522. METHODDEF(void)
  523. pass_startup (j_compress_ptr cinfo)
  524. {
  525. cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
  526. (*cinfo->marker->write_frame_header) (cinfo);
  527. (*cinfo->marker->write_scan_header) (cinfo);
  528. }
  529. LOCAL(void)
  530. copy_buffer (j_compress_ptr cinfo, int scan_idx)
  531. {
  532. my_master_ptr master = (my_master_ptr) cinfo->master;
  533. unsigned long size = master->scan_size[scan_idx];
  534. unsigned char * src = master->scan_buffer[scan_idx];
  535. int i;
  536. if (cinfo->err->trace_level > 0) {
  537. fprintf(stderr, "SCAN ");
  538. for (i = 0; i < cinfo->scan_info[scan_idx].comps_in_scan; i++)
  539. fprintf(stderr, "%s%d", (i==0)?"":",", cinfo->scan_info[scan_idx].component_index[i]);
  540. fprintf(stderr, ": %d %d", cinfo->scan_info[scan_idx].Ss, cinfo->scan_info[scan_idx].Se);
  541. fprintf(stderr, " %d %d", cinfo->scan_info[scan_idx].Ah, master->actual_Al[scan_idx]);
  542. fprintf(stderr, "\n");
  543. }
  544. while (size >= cinfo->dest->free_in_buffer)
  545. {
  546. MEMCOPY(cinfo->dest->next_output_byte, src, cinfo->dest->free_in_buffer);
  547. src += cinfo->dest->free_in_buffer;
  548. size -= cinfo->dest->free_in_buffer;
  549. cinfo->dest->next_output_byte += cinfo->dest->free_in_buffer;
  550. cinfo->dest->free_in_buffer = 0;
  551. if (!(*cinfo->dest->empty_output_buffer)(cinfo))
  552. ERREXIT(cinfo, JERR_UNSUPPORTED_SUSPEND);
  553. }
  554. MEMCOPY(cinfo->dest->next_output_byte, src, size);
  555. cinfo->dest->next_output_byte += size;
  556. cinfo->dest->free_in_buffer -= size;
  557. }
  558. LOCAL(void)
  559. select_scans (j_compress_ptr cinfo, int next_scan_number)
  560. {
  561. my_master_ptr master = (my_master_ptr) cinfo->master;
  562. int base_scan_idx = 0;
  563. int luma_freq_split_scan_start = cinfo->master->num_scans_luma_dc +
  564. 3 * cinfo->master->Al_max_luma + 2;
  565. int chroma_freq_split_scan_start = cinfo->master->num_scans_luma +
  566. cinfo->master->num_scans_chroma_dc +
  567. (6 * cinfo->master->Al_max_chroma + 4);
  568. int passes_per_scan = cinfo->optimize_coding ? 2 : 1;
  569. if (next_scan_number > 1 && next_scan_number <= luma_freq_split_scan_start) {
  570. if ((next_scan_number - 1) % 3 == 2) {
  571. int Al = (next_scan_number - 1) / 3;
  572. int i;
  573. unsigned long cost = 0;
  574. cost += master->scan_size[next_scan_number-2];
  575. cost += master->scan_size[next_scan_number-1];
  576. for (i = 0; i < Al; i++)
  577. cost += master->scan_size[3 + 3*i];
  578. if (Al == 0 || cost < master->best_cost) {
  579. master->best_cost = cost;
  580. master->best_Al_luma = Al;
  581. } else {
  582. master->scan_number = luma_freq_split_scan_start - 1;
  583. master->pass_number = passes_per_scan * (master->scan_number + 1) - 1 + master->pass_number_scan_opt_base;
  584. }
  585. }
  586. } else if (next_scan_number > luma_freq_split_scan_start &&
  587. next_scan_number <= cinfo->master->num_scans_luma) {
  588. if (next_scan_number == luma_freq_split_scan_start + 1) {
  589. master->best_freq_split_idx_luma = 0;
  590. master->best_cost = master->scan_size[next_scan_number-1];
  591. } else if ((next_scan_number - luma_freq_split_scan_start) % 2 == 1) {
  592. int idx = (next_scan_number - luma_freq_split_scan_start) >> 1;
  593. unsigned long cost = 0;
  594. cost += master->scan_size[next_scan_number-2];
  595. cost += master->scan_size[next_scan_number-1];
  596. if (cost < master->best_cost) {
  597. master->best_cost = cost;
  598. master->best_freq_split_idx_luma = idx;
  599. }
  600. /* if after testing first 3, no split is the best, don't search further */
  601. if ((idx == 2 && master->best_freq_split_idx_luma == 0) ||
  602. (idx == 3 && master->best_freq_split_idx_luma != 2) ||
  603. (idx == 4 && master->best_freq_split_idx_luma != 4)) {
  604. master->scan_number = cinfo->master->num_scans_luma - 1;
  605. master->pass_number = passes_per_scan * (master->scan_number + 1) - 1 + master->pass_number_scan_opt_base;
  606. master->pub.is_last_pass = (master->pass_number == master->total_passes - 1);
  607. }
  608. }
  609. } else if (cinfo->num_scans > cinfo->master->num_scans_luma) {
  610. if (next_scan_number == cinfo->master->num_scans_luma +
  611. cinfo->master->num_scans_chroma_dc) {
  612. base_scan_idx = cinfo->master->num_scans_luma;
  613. master->interleave_chroma_dc = master->scan_size[base_scan_idx] <= master->scan_size[base_scan_idx+1] + master->scan_size[base_scan_idx+2];
  614. } else if (next_scan_number > cinfo->master->num_scans_luma +
  615. cinfo->master->num_scans_chroma_dc &&
  616. next_scan_number <= chroma_freq_split_scan_start) {
  617. base_scan_idx = cinfo->master->num_scans_luma +
  618. cinfo->master->num_scans_chroma_dc;
  619. if ((next_scan_number - base_scan_idx) % 6 == 4) {
  620. int Al = (next_scan_number - base_scan_idx) / 6;
  621. int i;
  622. unsigned long cost = 0;
  623. cost += master->scan_size[next_scan_number-4];
  624. cost += master->scan_size[next_scan_number-3];
  625. cost += master->scan_size[next_scan_number-2];
  626. cost += master->scan_size[next_scan_number-1];
  627. for (i = 0; i < Al; i++) {
  628. cost += master->scan_size[base_scan_idx + 4 + 6*i];
  629. cost += master->scan_size[base_scan_idx + 5 + 6*i];
  630. }
  631. if (Al == 0 || cost < master->best_cost) {
  632. master->best_cost = cost;
  633. master->best_Al_chroma = Al;
  634. } else {
  635. master->scan_number = chroma_freq_split_scan_start - 1;
  636. master->pass_number = passes_per_scan * (master->scan_number + 1) - 1 + master->pass_number_scan_opt_base;
  637. }
  638. }
  639. } else if (next_scan_number > chroma_freq_split_scan_start && next_scan_number <= cinfo->num_scans) {
  640. if (next_scan_number == chroma_freq_split_scan_start + 2) {
  641. master->best_freq_split_idx_chroma = 0;
  642. master->best_cost = master->scan_size[next_scan_number-2];
  643. master->best_cost += master->scan_size[next_scan_number-1];
  644. } else if ((next_scan_number - chroma_freq_split_scan_start) % 4 == 2) {
  645. int idx = (next_scan_number - chroma_freq_split_scan_start) >> 2;
  646. unsigned long cost = 0;
  647. cost += master->scan_size[next_scan_number-4];
  648. cost += master->scan_size[next_scan_number-3];
  649. cost += master->scan_size[next_scan_number-2];
  650. cost += master->scan_size[next_scan_number-1];
  651. if (cost < master->best_cost) {
  652. master->best_cost = cost;
  653. master->best_freq_split_idx_chroma = idx;
  654. }
  655. /* if after testing first 3, no split is the best, don't search further */
  656. if ((idx == 2 && master->best_freq_split_idx_chroma == 0) ||
  657. (idx == 3 && master->best_freq_split_idx_chroma != 2) ||
  658. (idx == 4 && master->best_freq_split_idx_chroma != 4)) {
  659. master->scan_number = cinfo->num_scans - 1;
  660. master->pass_number = passes_per_scan * (master->scan_number + 1) - 1 + master->pass_number_scan_opt_base;
  661. master->pub.is_last_pass = (master->pass_number == master->total_passes - 1);
  662. }
  663. }
  664. }
  665. }
  666. if (master->scan_number == cinfo->num_scans - 1) {
  667. int i, Al;
  668. int min_Al = MIN(master->best_Al_luma, master->best_Al_chroma);
  669. copy_buffer(cinfo, 0);
  670. if (cinfo->num_scans > cinfo->master->num_scans_luma &&
  671. cinfo->master->dc_scan_opt_mode != 0) {
  672. base_scan_idx = cinfo->master->num_scans_luma;
  673. if (master->interleave_chroma_dc && cinfo->master->dc_scan_opt_mode != 1)
  674. copy_buffer(cinfo, base_scan_idx);
  675. else {
  676. copy_buffer(cinfo, base_scan_idx+1);
  677. copy_buffer(cinfo, base_scan_idx+2);
  678. }
  679. }
  680. if (master->best_freq_split_idx_luma == 0)
  681. copy_buffer(cinfo, luma_freq_split_scan_start);
  682. else {
  683. copy_buffer(cinfo, luma_freq_split_scan_start+2*(master->best_freq_split_idx_luma-1)+1);
  684. copy_buffer(cinfo, luma_freq_split_scan_start+2*(master->best_freq_split_idx_luma-1)+2);
  685. }
  686. /* copy the LSB refinements as well */
  687. for (Al = master->best_Al_luma-1; Al >= min_Al; Al--)
  688. copy_buffer(cinfo, 3 + 3*Al);
  689. if (cinfo->num_scans > cinfo->master->num_scans_luma) {
  690. if (master->best_freq_split_idx_chroma == 0) {
  691. copy_buffer(cinfo, chroma_freq_split_scan_start);
  692. copy_buffer(cinfo, chroma_freq_split_scan_start+1);
  693. }
  694. else {
  695. copy_buffer(cinfo, chroma_freq_split_scan_start+4*(master->best_freq_split_idx_chroma-1)+2);
  696. copy_buffer(cinfo, chroma_freq_split_scan_start+4*(master->best_freq_split_idx_chroma-1)+3);
  697. copy_buffer(cinfo, chroma_freq_split_scan_start+4*(master->best_freq_split_idx_chroma-1)+4);
  698. copy_buffer(cinfo, chroma_freq_split_scan_start+4*(master->best_freq_split_idx_chroma-1)+5);
  699. }
  700. base_scan_idx = cinfo->master->num_scans_luma +
  701. cinfo->master->num_scans_chroma_dc;
  702. for (Al = master->best_Al_chroma-1; Al >= min_Al; Al--) {
  703. copy_buffer(cinfo, base_scan_idx + 6*Al + 4);
  704. copy_buffer(cinfo, base_scan_idx + 6*Al + 5);
  705. }
  706. }
  707. for (Al = min_Al-1; Al >= 0; Al--) {
  708. copy_buffer(cinfo, 3 + 3*Al);
  709. if (cinfo->num_scans > cinfo->master->num_scans_luma) {
  710. copy_buffer(cinfo, base_scan_idx + 6*Al + 4);
  711. copy_buffer(cinfo, base_scan_idx + 6*Al + 5);
  712. }
  713. }
  714. /* free the memory allocated for buffers */
  715. for (i = 0; i < cinfo->num_scans; i++)
  716. if (master->scan_buffer[i])
  717. free(master->scan_buffer[i]);
  718. }
  719. }
  720. /*
  721. * Finish up at end of pass.
  722. */
  723. METHODDEF(void)
  724. finish_pass_master (j_compress_ptr cinfo)
  725. {
  726. my_master_ptr master = (my_master_ptr) cinfo->master;
  727. /* The entropy coder always needs an end-of-pass call,
  728. * either to analyze statistics or to flush its output buffer.
  729. */
  730. (*cinfo->entropy->finish_pass) (cinfo);
  731. /* Update state for next pass */
  732. switch (master->pass_type) {
  733. case main_pass:
  734. /* next pass is either output of scan 0 (after optimization)
  735. * or output of scan 1 (if no optimization).
  736. */
  737. if (cinfo->master->trellis_quant)
  738. master->pass_type = trellis_pass;
  739. else {
  740. master->pass_type = output_pass;
  741. if (! cinfo->optimize_coding)
  742. master->scan_number++;
  743. }
  744. break;
  745. case huff_opt_pass:
  746. /* next pass is always output of current scan */
  747. master->pass_type = (master->pass_number < master->pass_number_scan_opt_base-1) ? trellis_pass : output_pass;
  748. break;
  749. case output_pass:
  750. /* next pass is either optimization or output of next scan */
  751. if (cinfo->optimize_coding)
  752. master->pass_type = huff_opt_pass;
  753. if (cinfo->master->optimize_scans) {
  754. (*cinfo->dest->term_destination)(cinfo);
  755. cinfo->dest = master->saved_dest;
  756. select_scans(cinfo, master->scan_number + 1);
  757. }
  758. master->scan_number++;
  759. break;
  760. case trellis_pass:
  761. if (cinfo->optimize_coding)
  762. master->pass_type = huff_opt_pass;
  763. else
  764. master->pass_type = (master->pass_number < master->pass_number_scan_opt_base-1) ? trellis_pass : output_pass;
  765. if ((master->pass_number + 1) %
  766. (cinfo->num_components * (cinfo->master->use_scans_in_trellis ? 4 : 2)) == 0 &&
  767. cinfo->master->trellis_q_opt) {
  768. int i, j;
  769. for (i = 0; i < NUM_QUANT_TBLS; i++) {
  770. for (j = 1; j < DCTSIZE2; j++) {
  771. if (cinfo->master->norm_coef[i][j] != 0.0) {
  772. int q = (int)(cinfo->master->norm_src[i][j] /
  773. cinfo->master->norm_coef[i][j] + 0.5);
  774. if (q > 254) q = 254;
  775. if (q < 1) q = 1;
  776. cinfo->quant_tbl_ptrs[i]->quantval[j] = q;
  777. }
  778. }
  779. }
  780. }
  781. break;
  782. }
  783. master->pass_number++;
  784. }
  785. /*
  786. * Initialize master compression control.
  787. */
  788. GLOBAL(void)
  789. jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
  790. {
  791. my_master_ptr master = (my_master_ptr) cinfo->master;
  792. master->pub.prepare_for_pass = prepare_for_pass;
  793. master->pub.pass_startup = pass_startup;
  794. master->pub.finish_pass = finish_pass_master;
  795. master->pub.is_last_pass = FALSE;
  796. master->pub.call_pass_startup = FALSE;
  797. /* Validate parameters, determine derived values */
  798. initial_setup(cinfo, transcode_only);
  799. if (cinfo->scan_info != NULL) {
  800. #ifdef C_MULTISCAN_FILES_SUPPORTED
  801. validate_script(cinfo);
  802. #else
  803. ERREXIT(cinfo, JERR_NOT_COMPILED);
  804. #endif
  805. } else {
  806. cinfo->progressive_mode = FALSE;
  807. cinfo->num_scans = 1;
  808. }
  809. if (cinfo->progressive_mode && !cinfo->arith_code) /* TEMPORARY HACK ??? */
  810. cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
  811. /* Initialize my private state */
  812. if (transcode_only) {
  813. /* no main pass in transcoding */
  814. if (cinfo->optimize_coding)
  815. master->pass_type = huff_opt_pass;
  816. else
  817. master->pass_type = output_pass;
  818. } else {
  819. /* for normal compression, first pass is always this type: */
  820. master->pass_type = main_pass;
  821. }
  822. master->scan_number = 0;
  823. master->pass_number = 0;
  824. if (cinfo->optimize_coding)
  825. master->total_passes = cinfo->num_scans * 2;
  826. else
  827. master->total_passes = cinfo->num_scans;
  828. master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")";
  829. master->pass_number_scan_opt_base = 0;
  830. if (cinfo->master->trellis_quant) {
  831. if (cinfo->optimize_coding)
  832. master->pass_number_scan_opt_base =
  833. ((cinfo->master->use_scans_in_trellis) ? 4 : 2) * cinfo->num_components *
  834. cinfo->master->trellis_num_loops;
  835. else
  836. master->pass_number_scan_opt_base =
  837. ((cinfo->master->use_scans_in_trellis) ? 2 : 1) * cinfo->num_components *
  838. cinfo->master->trellis_num_loops + 1;
  839. master->total_passes += master->pass_number_scan_opt_base;
  840. }
  841. if (cinfo->master->optimize_scans) {
  842. int i;
  843. master->best_Al_chroma = 0;
  844. for (i = 0; i < cinfo->num_scans; i++)
  845. master->scan_buffer[i] = NULL;
  846. }
  847. }