Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.1KB

  1. /*
  2. * jdtrans.c
  3. *
  4. * Copyright (C) 1995-1997, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains library routines for transcoding decompression,
  9. * that is, reading raw DCT coefficient arrays from an input JPEG file.
  10. * The routines in jdapimin.c will also be needed by a transcoder.
  11. */
  12. #define JPEG_INTERNALS
  13. #include "jinclude.h"
  14. #include "jpeglib.h"
  15. /* Forward declarations */
  16. LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
  17. /*
  18. * Read the coefficient arrays from a JPEG file.
  19. * jpeg_read_header must be completed before calling this.
  20. *
  21. * The entire image is read into a set of virtual coefficient-block arrays,
  22. * one per component. The return value is a pointer to the array of
  23. * virtual-array descriptors. These can be manipulated directly via the
  24. * JPEG memory manager, or handed off to jpeg_write_coefficients().
  25. * To release the memory occupied by the virtual arrays, call
  26. * jpeg_finish_decompress() when done with the data.
  27. *
  28. * An alternative usage is to simply obtain access to the coefficient arrays
  29. * during a buffered-image-mode decompression operation. This is allowed
  30. * after any jpeg_finish_output() call. The arrays can be accessed until
  31. * jpeg_finish_decompress() is called. (Note that any call to the library
  32. * may reposition the arrays, so don't rely on access_virt_barray() results
  33. * to stay valid across library calls.)
  34. *
  35. * Returns NULL if suspended. This case need be checked only if
  36. * a suspending data source is used.
  37. */
  38. GLOBAL(jvirt_barray_ptr *)
  39. jpeg_read_coefficients (j_decompress_ptr cinfo)
  40. {
  41. if (cinfo->global_state == DSTATE_READY) {
  42. /* First call: initialize active modules */
  43. transdecode_master_selection(cinfo);
  44. cinfo->global_state = DSTATE_RDCOEFS;
  45. }
  46. if (cinfo->global_state == DSTATE_RDCOEFS) {
  47. /* Absorb whole file into the coef buffer */
  48. for (;;) {
  49. int retcode;
  50. /* Call progress monitor hook if present */
  51. if (cinfo->progress != NULL)
  52. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  53. /* Absorb some more input */
  54. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  55. if (retcode == JPEG_SUSPENDED)
  56. return NULL;
  57. if (retcode == JPEG_REACHED_EOI)
  58. break;
  59. /* Advance progress counter if appropriate */
  60. if (cinfo->progress != NULL &&
  61. (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
  62. if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
  63. /* startup underestimated number of scans; ratchet up one scan */
  64. cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
  65. }
  66. }
  67. }
  68. /* Set state so that jpeg_finish_decompress does the right thing */
  69. cinfo->global_state = DSTATE_STOPPING;
  70. }
  71. /* At this point we should be in state DSTATE_STOPPING if being used
  72. * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
  73. * to the coefficients during a full buffered-image-mode decompression.
  74. */
  75. if ((cinfo->global_state == DSTATE_STOPPING ||
  76. cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
  77. return cinfo->coef->coef_arrays;
  78. }
  79. /* Oops, improper usage */
  80. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  81. return NULL; /* keep compiler happy */
  82. }
  83. /*
  84. * Master selection of decompression modules for transcoding.
  85. * This substitutes for jdmaster.c's initialization of the full decompressor.
  86. */
  87. LOCAL(void)
  88. transdecode_master_selection (j_decompress_ptr cinfo)
  89. {
  90. /* This is effectively a buffered-image operation. */
  91. cinfo->buffered_image = TRUE;
  92. /* Entropy decoding: either Huffman or arithmetic coding. */
  93. if (cinfo->arith_code) {
  94. ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  95. } else {
  96. if (cinfo->progressive_mode) {
  97. #ifdef D_PROGRESSIVE_SUPPORTED
  98. jinit_phuff_decoder(cinfo);
  99. #else
  100. ERREXIT(cinfo, JERR_NOT_COMPILED);
  101. #endif
  102. } else
  103. jinit_huff_decoder(cinfo);
  104. }
  105. /* Always get a full-image coefficient buffer. */
  106. jinit_d_coef_controller(cinfo, TRUE);
  107. /* We can now tell the memory manager to allocate virtual arrays. */
  108. (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  109. /* Initialize input side of decompressor to consume first scan. */
  110. (*cinfo->inputctl->start_input_pass) (cinfo);
  111. /* Initialize progress monitoring. */
  112. if (cinfo->progress != NULL) {
  113. int nscans;
  114. /* Estimate number of scans to set pass_limit. */
  115. if (cinfo->progressive_mode) {
  116. /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  117. nscans = 2 + 3 * cinfo->num_components;
  118. } else if (cinfo->inputctl->has_multiple_scans) {
  119. /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  120. nscans = cinfo->num_components;
  121. } else {
  122. nscans = 1;
  123. }
  124. cinfo->progress->pass_counter = 0L;
  125. cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
  126. cinfo->progress->completed_passes = 0;
  127. cinfo->progress->total_passes = 1;
  128. }
  129. }