1*dfc6aa5cSAndroid Build Coastguard WorkerIJG JPEG LIBRARY: SYSTEM ARCHITECTURE 2*dfc6aa5cSAndroid Build Coastguard Worker 3*dfc6aa5cSAndroid Build Coastguard WorkerThis file was part of the Independent JPEG Group's software: 4*dfc6aa5cSAndroid Build Coastguard WorkerCopyright (C) 1991-2012, Thomas G. Lane, Guido Vollbeding. 5*dfc6aa5cSAndroid Build Coastguard WorkerIt was modified by The libjpeg-turbo Project to include only information 6*dfc6aa5cSAndroid Build Coastguard Workerrelevant to libjpeg-turbo. 7*dfc6aa5cSAndroid Build Coastguard WorkerFor conditions of distribution and use, see the accompanying README.ijg file. 8*dfc6aa5cSAndroid Build Coastguard Worker 9*dfc6aa5cSAndroid Build Coastguard Worker 10*dfc6aa5cSAndroid Build Coastguard WorkerThis file provides an overview of the architecture of the IJG JPEG software; 11*dfc6aa5cSAndroid Build Coastguard Workerthat is, the functions of the various modules in the system and the interfaces 12*dfc6aa5cSAndroid Build Coastguard Workerbetween modules. For more precise details about any data structure or calling 13*dfc6aa5cSAndroid Build Coastguard Workerconvention, see the include files and comments in the source code. 14*dfc6aa5cSAndroid Build Coastguard Worker 15*dfc6aa5cSAndroid Build Coastguard WorkerWe assume that the reader is already somewhat familiar with the JPEG standard. 16*dfc6aa5cSAndroid Build Coastguard WorkerThe README.ijg file includes references for learning about JPEG. The file 17*dfc6aa5cSAndroid Build Coastguard Workerlibjpeg.txt describes the library from the viewpoint of an application 18*dfc6aa5cSAndroid Build Coastguard Workerprogrammer using the library; it's best to read that file before this one. 19*dfc6aa5cSAndroid Build Coastguard WorkerAlso, the file coderules.txt describes the coding style conventions we use. 20*dfc6aa5cSAndroid Build Coastguard Worker 21*dfc6aa5cSAndroid Build Coastguard WorkerIn this document, JPEG-specific terminology follows the JPEG standard: 22*dfc6aa5cSAndroid Build Coastguard Worker A "component" means a color channel, e.g., Red or Luminance. 23*dfc6aa5cSAndroid Build Coastguard Worker A "sample" is a single component value (i.e., one number in the image data). 24*dfc6aa5cSAndroid Build Coastguard Worker A "coefficient" is a frequency coefficient (a DCT transform output number). 25*dfc6aa5cSAndroid Build Coastguard Worker A "block" is an 8x8 group of samples or coefficients. 26*dfc6aa5cSAndroid Build Coastguard Worker An "MCU" (minimum coded unit) is an interleaved set of blocks of size 27*dfc6aa5cSAndroid Build Coastguard Worker determined by the sampling factors, or a single block in a 28*dfc6aa5cSAndroid Build Coastguard Worker noninterleaved scan. 29*dfc6aa5cSAndroid Build Coastguard WorkerWe do not use the terms "pixel" and "sample" interchangeably. When we say 30*dfc6aa5cSAndroid Build Coastguard Workerpixel, we mean an element of the full-size image, while a sample is an element 31*dfc6aa5cSAndroid Build Coastguard Workerof the downsampled image. Thus the number of samples may vary across 32*dfc6aa5cSAndroid Build Coastguard Workercomponents while the number of pixels does not. (This terminology is not used 33*dfc6aa5cSAndroid Build Coastguard Workerrigorously throughout the code, but it is used in places where confusion would 34*dfc6aa5cSAndroid Build Coastguard Workerotherwise result.) 35*dfc6aa5cSAndroid Build Coastguard Worker 36*dfc6aa5cSAndroid Build Coastguard Worker 37*dfc6aa5cSAndroid Build Coastguard Worker*** System features *** 38*dfc6aa5cSAndroid Build Coastguard Worker 39*dfc6aa5cSAndroid Build Coastguard WorkerThe IJG distribution contains two parts: 40*dfc6aa5cSAndroid Build Coastguard Worker * A subroutine library for JPEG compression and decompression. 41*dfc6aa5cSAndroid Build Coastguard Worker * cjpeg/djpeg, two sample applications that use the library to transform 42*dfc6aa5cSAndroid Build Coastguard Worker JFIF JPEG files to and from several other image formats. 43*dfc6aa5cSAndroid Build Coastguard Workercjpeg/djpeg are of no great intellectual complexity: they merely add a simple 44*dfc6aa5cSAndroid Build Coastguard Workercommand-line user interface and I/O routines for several uncompressed image 45*dfc6aa5cSAndroid Build Coastguard Workerformats. This document concentrates on the library itself. 46*dfc6aa5cSAndroid Build Coastguard Worker 47*dfc6aa5cSAndroid Build Coastguard WorkerWe desire the library to be capable of supporting all JPEG baseline, extended 48*dfc6aa5cSAndroid Build Coastguard Workersequential, and progressive DCT processes. Hierarchical processes are not 49*dfc6aa5cSAndroid Build Coastguard Workersupported. 50*dfc6aa5cSAndroid Build Coastguard Worker 51*dfc6aa5cSAndroid Build Coastguard WorkerThe library does not support the lossless (spatial) JPEG process. Lossless 52*dfc6aa5cSAndroid Build Coastguard WorkerJPEG shares little or no code with lossy JPEG, and would normally be used 53*dfc6aa5cSAndroid Build Coastguard Workerwithout the extensive pre- and post-processing provided by this library. 54*dfc6aa5cSAndroid Build Coastguard WorkerWe feel that lossless JPEG is better handled by a separate library. 55*dfc6aa5cSAndroid Build Coastguard Worker 56*dfc6aa5cSAndroid Build Coastguard WorkerWithin these limits, any set of compression parameters allowed by the JPEG 57*dfc6aa5cSAndroid Build Coastguard Workerspec should be readable for decompression. (We can be more restrictive about 58*dfc6aa5cSAndroid Build Coastguard Workerwhat formats we can generate.) Although the system design allows for all 59*dfc6aa5cSAndroid Build Coastguard Workerparameter values, some uncommon settings are not yet implemented and may 60*dfc6aa5cSAndroid Build Coastguard Workernever be; nonintegral sampling ratios are the prime example. Furthermore, 61*dfc6aa5cSAndroid Build Coastguard Workerwe treat 8-bit vs. 12-bit data precision as a compile-time switch, not a 62*dfc6aa5cSAndroid Build Coastguard Workerrun-time option, because most machines can store 8-bit pixels much more 63*dfc6aa5cSAndroid Build Coastguard Workercompactly than 12-bit. 64*dfc6aa5cSAndroid Build Coastguard Worker 65*dfc6aa5cSAndroid Build Coastguard WorkerBy itself, the library handles only interchange JPEG datastreams --- in 66*dfc6aa5cSAndroid Build Coastguard Workerparticular the widely used JFIF file format. The library can be used by 67*dfc6aa5cSAndroid Build Coastguard Workersurrounding code to process interchange or abbreviated JPEG datastreams that 68*dfc6aa5cSAndroid Build Coastguard Workerare embedded in more complex file formats. (For example, libtiff uses this 69*dfc6aa5cSAndroid Build Coastguard Workerlibrary to implement JPEG compression within the TIFF file format.) 70*dfc6aa5cSAndroid Build Coastguard Worker 71*dfc6aa5cSAndroid Build Coastguard WorkerThe library includes a substantial amount of code that is not covered by the 72*dfc6aa5cSAndroid Build Coastguard WorkerJPEG standard but is necessary for typical applications of JPEG. These 73*dfc6aa5cSAndroid Build Coastguard Workerfunctions preprocess the image before JPEG compression or postprocess it after 74*dfc6aa5cSAndroid Build Coastguard Workerdecompression. They include colorspace conversion, downsampling/upsampling, 75*dfc6aa5cSAndroid Build Coastguard Workerand color quantization. This code can be omitted if not needed. 76*dfc6aa5cSAndroid Build Coastguard Worker 77*dfc6aa5cSAndroid Build Coastguard WorkerA wide range of quality vs. speed tradeoffs are possible in JPEG processing, 78*dfc6aa5cSAndroid Build Coastguard Workerand even more so in decompression postprocessing. The decompression library 79*dfc6aa5cSAndroid Build Coastguard Workerprovides multiple implementations that cover most of the useful tradeoffs, 80*dfc6aa5cSAndroid Build Coastguard Workerranging from very-high-quality down to fast-preview operation. On the 81*dfc6aa5cSAndroid Build Coastguard Workercompression side we have generally not provided low-quality choices, since 82*dfc6aa5cSAndroid Build Coastguard Workercompression is normally less time-critical. It should be understood that the 83*dfc6aa5cSAndroid Build Coastguard Workerlow-quality modes may not meet the JPEG standard's accuracy requirements; 84*dfc6aa5cSAndroid Build Coastguard Workernonetheless, they are useful for viewers. 85*dfc6aa5cSAndroid Build Coastguard Worker 86*dfc6aa5cSAndroid Build Coastguard Worker 87*dfc6aa5cSAndroid Build Coastguard Worker*** System overview *** 88*dfc6aa5cSAndroid Build Coastguard Worker 89*dfc6aa5cSAndroid Build Coastguard WorkerThe compressor and decompressor are each divided into two main sections: 90*dfc6aa5cSAndroid Build Coastguard Workerthe JPEG compressor or decompressor proper, and the preprocessing or 91*dfc6aa5cSAndroid Build Coastguard Workerpostprocessing functions. The interface between these two sections is the 92*dfc6aa5cSAndroid Build Coastguard Workerimage data that Rec. ITU-T T.81 | ISO/IEC 10918-1 regards as its input or 93*dfc6aa5cSAndroid Build Coastguard Workeroutput: this data is in the colorspace to be used for compression, and it is 94*dfc6aa5cSAndroid Build Coastguard Workerdownsampled to the sampling factors to be used. The preprocessing and 95*dfc6aa5cSAndroid Build Coastguard Workerpostprocessing steps are responsible for converting a normal image 96*dfc6aa5cSAndroid Build Coastguard Workerrepresentation to or from this form. (Those few applications that want to deal 97*dfc6aa5cSAndroid Build Coastguard Workerwith YCbCr downsampled data can skip the preprocessing or postprocessing step.) 98*dfc6aa5cSAndroid Build Coastguard Worker 99*dfc6aa5cSAndroid Build Coastguard WorkerLooking more closely, the compressor library contains the following main 100*dfc6aa5cSAndroid Build Coastguard Workerelements: 101*dfc6aa5cSAndroid Build Coastguard Worker 102*dfc6aa5cSAndroid Build Coastguard Worker Preprocessing: 103*dfc6aa5cSAndroid Build Coastguard Worker * Color space conversion (e.g., RGB to YCbCr). 104*dfc6aa5cSAndroid Build Coastguard Worker * Edge expansion and downsampling. Optionally, this step can do simple 105*dfc6aa5cSAndroid Build Coastguard Worker smoothing --- this is often helpful for low-quality source data. 106*dfc6aa5cSAndroid Build Coastguard Worker JPEG proper: 107*dfc6aa5cSAndroid Build Coastguard Worker * MCU assembly, DCT, quantization. 108*dfc6aa5cSAndroid Build Coastguard Worker * Entropy coding (sequential or progressive, Huffman or arithmetic). 109*dfc6aa5cSAndroid Build Coastguard Worker 110*dfc6aa5cSAndroid Build Coastguard WorkerIn addition to these modules we need overall control, marker generation, 111*dfc6aa5cSAndroid Build Coastguard Workerand support code (memory management & error handling). There is also a 112*dfc6aa5cSAndroid Build Coastguard Workermodule responsible for physically writing the output data --- typically 113*dfc6aa5cSAndroid Build Coastguard Workerthis is just an interface to fwrite(), but some applications may need to 114*dfc6aa5cSAndroid Build Coastguard Workerdo something else with the data. 115*dfc6aa5cSAndroid Build Coastguard Worker 116*dfc6aa5cSAndroid Build Coastguard WorkerThe decompressor library contains the following main elements: 117*dfc6aa5cSAndroid Build Coastguard Worker 118*dfc6aa5cSAndroid Build Coastguard Worker JPEG proper: 119*dfc6aa5cSAndroid Build Coastguard Worker * Entropy decoding (sequential or progressive, Huffman or arithmetic). 120*dfc6aa5cSAndroid Build Coastguard Worker * Dequantization, inverse DCT, MCU disassembly. 121*dfc6aa5cSAndroid Build Coastguard Worker Postprocessing: 122*dfc6aa5cSAndroid Build Coastguard Worker * Upsampling. Optionally, this step may be able to do more general 123*dfc6aa5cSAndroid Build Coastguard Worker rescaling of the image. 124*dfc6aa5cSAndroid Build Coastguard Worker * Color space conversion (e.g., YCbCr to RGB). This step may also 125*dfc6aa5cSAndroid Build Coastguard Worker provide gamma adjustment [ currently it does not ]. 126*dfc6aa5cSAndroid Build Coastguard Worker * Optional color quantization (e.g., reduction to 256 colors). 127*dfc6aa5cSAndroid Build Coastguard Worker * Optional color precision reduction (e.g., 24-bit to 15-bit color). 128*dfc6aa5cSAndroid Build Coastguard Worker [This feature is not currently implemented.] 129*dfc6aa5cSAndroid Build Coastguard Worker 130*dfc6aa5cSAndroid Build Coastguard WorkerWe also need overall control, marker parsing, and a data source module. 131*dfc6aa5cSAndroid Build Coastguard WorkerThe support code (memory management & error handling) can be shared with 132*dfc6aa5cSAndroid Build Coastguard Workerthe compression half of the library. 133*dfc6aa5cSAndroid Build Coastguard Worker 134*dfc6aa5cSAndroid Build Coastguard WorkerThere may be several implementations of each of these elements, particularly 135*dfc6aa5cSAndroid Build Coastguard Workerin the decompressor, where a wide range of speed/quality tradeoffs is very 136*dfc6aa5cSAndroid Build Coastguard Workeruseful. It must be understood that some of the best speedups involve 137*dfc6aa5cSAndroid Build Coastguard Workermerging adjacent steps in the pipeline. For example, upsampling, color space 138*dfc6aa5cSAndroid Build Coastguard Workerconversion, and color quantization might all be done at once when using a 139*dfc6aa5cSAndroid Build Coastguard Workerlow-quality ordered-dither technique. The system architecture is designed to 140*dfc6aa5cSAndroid Build Coastguard Workerallow such merging where appropriate. 141*dfc6aa5cSAndroid Build Coastguard Worker 142*dfc6aa5cSAndroid Build Coastguard Worker 143*dfc6aa5cSAndroid Build Coastguard WorkerNote: it is convenient to regard edge expansion (padding to block boundaries) 144*dfc6aa5cSAndroid Build Coastguard Workeras a preprocessing/postprocessing function, even though 145*dfc6aa5cSAndroid Build Coastguard WorkerRec. ITU-T T.81 | ISO/IEC 10918-1 includes it in compression/decompression. We 146*dfc6aa5cSAndroid Build Coastguard Workerdo this because downsampling/upsampling can be simplified a little if they work 147*dfc6aa5cSAndroid Build Coastguard Workeron padded data: it's not necessary to have special cases at the right and 148*dfc6aa5cSAndroid Build Coastguard Workerbottom edges. Therefore the interface buffer is always an integral number of 149*dfc6aa5cSAndroid Build Coastguard Workerblocks wide and high, and we expect compression preprocessing to pad the source 150*dfc6aa5cSAndroid Build Coastguard Workerdata properly. Padding will occur only to the next block (8-sample) boundary. 151*dfc6aa5cSAndroid Build Coastguard WorkerIn an interleaved-scan situation, additional dummy blocks may be used to fill 152*dfc6aa5cSAndroid Build Coastguard Workerout MCUs, but the MCU assembly and disassembly logic will create or discard 153*dfc6aa5cSAndroid Build Coastguard Workerthese blocks internally. (This is advantageous for speed reasons, since we 154*dfc6aa5cSAndroid Build Coastguard Workeravoid DCTing the dummy blocks. It also permits a small reduction in file size, 155*dfc6aa5cSAndroid Build Coastguard Workerbecause the compressor can choose dummy block contents so as to minimize their 156*dfc6aa5cSAndroid Build Coastguard Workersize in compressed form. Finally, it makes the interface buffer specification 157*dfc6aa5cSAndroid Build Coastguard Workerindependent of whether the file is actually interleaved or not.) Applications 158*dfc6aa5cSAndroid Build Coastguard Workerthat wish to deal directly with the downsampled data must provide similar 159*dfc6aa5cSAndroid Build Coastguard Workerbuffering and padding for odd-sized images. 160*dfc6aa5cSAndroid Build Coastguard Worker 161*dfc6aa5cSAndroid Build Coastguard Worker 162*dfc6aa5cSAndroid Build Coastguard Worker*** Poor man's object-oriented programming *** 163*dfc6aa5cSAndroid Build Coastguard Worker 164*dfc6aa5cSAndroid Build Coastguard WorkerIt should be clear by now that we have a lot of quasi-independent processing 165*dfc6aa5cSAndroid Build Coastguard Workersteps, many of which have several possible behaviors. To avoid cluttering the 166*dfc6aa5cSAndroid Build Coastguard Workercode with lots of switch statements, we use a simple form of object-style 167*dfc6aa5cSAndroid Build Coastguard Workerprogramming to separate out the different possibilities. 168*dfc6aa5cSAndroid Build Coastguard Worker 169*dfc6aa5cSAndroid Build Coastguard WorkerFor example, two different color quantization algorithms could be implemented 170*dfc6aa5cSAndroid Build Coastguard Workeras two separate modules that present the same external interface; at runtime, 171*dfc6aa5cSAndroid Build Coastguard Workerthe calling code will access the proper module indirectly through an "object". 172*dfc6aa5cSAndroid Build Coastguard Worker 173*dfc6aa5cSAndroid Build Coastguard WorkerWe can get the limited features we need while staying within portable C. 174*dfc6aa5cSAndroid Build Coastguard WorkerThe basic tool is a function pointer. An "object" is just a struct 175*dfc6aa5cSAndroid Build Coastguard Workercontaining one or more function pointer fields, each of which corresponds to 176*dfc6aa5cSAndroid Build Coastguard Workera method name in real object-oriented languages. During initialization we 177*dfc6aa5cSAndroid Build Coastguard Workerfill in the function pointers with references to whichever module we have 178*dfc6aa5cSAndroid Build Coastguard Workerdetermined we need to use in this run. Then invocation of the module is done 179*dfc6aa5cSAndroid Build Coastguard Workerby indirecting through a function pointer; on most machines this is no more 180*dfc6aa5cSAndroid Build Coastguard Workerexpensive than a switch statement, which would be the only other way of 181*dfc6aa5cSAndroid Build Coastguard Workermaking the required run-time choice. The really significant benefit, of 182*dfc6aa5cSAndroid Build Coastguard Workercourse, is keeping the source code clean and well structured. 183*dfc6aa5cSAndroid Build Coastguard Worker 184*dfc6aa5cSAndroid Build Coastguard WorkerWe can also arrange to have private storage that varies between different 185*dfc6aa5cSAndroid Build Coastguard Workerimplementations of the same kind of object. We do this by making all the 186*dfc6aa5cSAndroid Build Coastguard Workermodule-specific object structs be separately allocated entities, which will 187*dfc6aa5cSAndroid Build Coastguard Workerbe accessed via pointers in the master compression or decompression struct. 188*dfc6aa5cSAndroid Build Coastguard WorkerThe "public" fields or methods for a given kind of object are specified by 189*dfc6aa5cSAndroid Build Coastguard Workera commonly known struct. But a module's initialization code can allocate 190*dfc6aa5cSAndroid Build Coastguard Workera larger struct that contains the common struct as its first member, plus 191*dfc6aa5cSAndroid Build Coastguard Workeradditional private fields. With appropriate pointer casting, the module's 192*dfc6aa5cSAndroid Build Coastguard Workerinternal functions can access these private fields. (For a simple example, 193*dfc6aa5cSAndroid Build Coastguard Workersee jdatadst.c, which implements the external interface specified by struct 194*dfc6aa5cSAndroid Build Coastguard Workerjpeg_destination_mgr, but adds extra fields.) 195*dfc6aa5cSAndroid Build Coastguard Worker 196*dfc6aa5cSAndroid Build Coastguard Worker(Of course this would all be a lot easier if we were using C++, but we are 197*dfc6aa5cSAndroid Build Coastguard Workernot yet prepared to assume that everyone has a C++ compiler.) 198*dfc6aa5cSAndroid Build Coastguard Worker 199*dfc6aa5cSAndroid Build Coastguard WorkerAn important benefit of this scheme is that it is easy to provide multiple 200*dfc6aa5cSAndroid Build Coastguard Workerversions of any method, each tuned to a particular case. While a lot of 201*dfc6aa5cSAndroid Build Coastguard Workerprecalculation might be done to select an optimal implementation of a method, 202*dfc6aa5cSAndroid Build Coastguard Workerthe cost per invocation is constant. For example, the upsampling step might 203*dfc6aa5cSAndroid Build Coastguard Workerhave a "generic" method, plus one or more "hardwired" methods for the most 204*dfc6aa5cSAndroid Build Coastguard Workerpopular sampling factors; the hardwired methods would be faster because they'd 205*dfc6aa5cSAndroid Build Coastguard Workeruse straight-line code instead of for-loops. The cost to determine which 206*dfc6aa5cSAndroid Build Coastguard Workermethod to use is paid only once, at startup, and the selection criteria are 207*dfc6aa5cSAndroid Build Coastguard Workerhidden from the callers of the method. 208*dfc6aa5cSAndroid Build Coastguard Worker 209*dfc6aa5cSAndroid Build Coastguard WorkerThis plan differs a little bit from usual object-oriented structures, in that 210*dfc6aa5cSAndroid Build Coastguard Workeronly one instance of each object class will exist during execution. The 211*dfc6aa5cSAndroid Build Coastguard Workerreason for having the class structure is that on different runs we may create 212*dfc6aa5cSAndroid Build Coastguard Workerdifferent instances (choose to execute different modules). You can think of 213*dfc6aa5cSAndroid Build Coastguard Workerthe term "method" as denoting the common interface presented by a particular 214*dfc6aa5cSAndroid Build Coastguard Workerset of interchangeable functions, and "object" as denoting a group of related 215*dfc6aa5cSAndroid Build Coastguard Workermethods, or the total shared interface behavior of a group of modules. 216*dfc6aa5cSAndroid Build Coastguard Worker 217*dfc6aa5cSAndroid Build Coastguard Worker 218*dfc6aa5cSAndroid Build Coastguard Worker*** Overall control structure *** 219*dfc6aa5cSAndroid Build Coastguard Worker 220*dfc6aa5cSAndroid Build Coastguard WorkerWe previously mentioned the need for overall control logic in the compression 221*dfc6aa5cSAndroid Build Coastguard Workerand decompression libraries. In IJG implementations prior to v5, overall 222*dfc6aa5cSAndroid Build Coastguard Workercontrol was mostly provided by "pipeline control" modules, which proved to be 223*dfc6aa5cSAndroid Build Coastguard Workerlarge, unwieldy, and hard to understand. To improve the situation, the 224*dfc6aa5cSAndroid Build Coastguard Workercontrol logic has been subdivided into multiple modules. The control modules 225*dfc6aa5cSAndroid Build Coastguard Workerconsist of: 226*dfc6aa5cSAndroid Build Coastguard Worker 227*dfc6aa5cSAndroid Build Coastguard Worker1. Master control for module selection and initialization. This has two 228*dfc6aa5cSAndroid Build Coastguard Workerresponsibilities: 229*dfc6aa5cSAndroid Build Coastguard Worker 230*dfc6aa5cSAndroid Build Coastguard Worker 1A. Startup initialization at the beginning of image processing. 231*dfc6aa5cSAndroid Build Coastguard Worker The individual processing modules to be used in this run are selected 232*dfc6aa5cSAndroid Build Coastguard Worker and given initialization calls. 233*dfc6aa5cSAndroid Build Coastguard Worker 234*dfc6aa5cSAndroid Build Coastguard Worker 1B. Per-pass control. This determines how many passes will be performed 235*dfc6aa5cSAndroid Build Coastguard Worker and calls each active processing module to configure itself 236*dfc6aa5cSAndroid Build Coastguard Worker appropriately at the beginning of each pass. End-of-pass processing, 237*dfc6aa5cSAndroid Build Coastguard Worker where necessary, is also invoked from the master control module. 238*dfc6aa5cSAndroid Build Coastguard Worker 239*dfc6aa5cSAndroid Build Coastguard Worker Method selection is partially distributed, in that a particular processing 240*dfc6aa5cSAndroid Build Coastguard Worker module may contain several possible implementations of a particular method, 241*dfc6aa5cSAndroid Build Coastguard Worker which it will select among when given its initialization call. The master 242*dfc6aa5cSAndroid Build Coastguard Worker control code need only be concerned with decisions that affect more than 243*dfc6aa5cSAndroid Build Coastguard Worker one module. 244*dfc6aa5cSAndroid Build Coastguard Worker 245*dfc6aa5cSAndroid Build Coastguard Worker2. Data buffering control. A separate control module exists for each 246*dfc6aa5cSAndroid Build Coastguard Worker inter-processing-step data buffer. This module is responsible for 247*dfc6aa5cSAndroid Build Coastguard Worker invoking the processing steps that write or read that data buffer. 248*dfc6aa5cSAndroid Build Coastguard Worker 249*dfc6aa5cSAndroid Build Coastguard WorkerEach buffer controller sees the world as follows: 250*dfc6aa5cSAndroid Build Coastguard Worker 251*dfc6aa5cSAndroid Build Coastguard Workerinput data => processing step A => buffer => processing step B => output data 252*dfc6aa5cSAndroid Build Coastguard Worker | | | 253*dfc6aa5cSAndroid Build Coastguard Worker ------------------ controller ------------------ 254*dfc6aa5cSAndroid Build Coastguard Worker 255*dfc6aa5cSAndroid Build Coastguard WorkerThe controller knows the dataflow requirements of steps A and B: how much data 256*dfc6aa5cSAndroid Build Coastguard Workerthey want to accept in one chunk and how much they output in one chunk. Its 257*dfc6aa5cSAndroid Build Coastguard Workerfunction is to manage its buffer and call A and B at the proper times. 258*dfc6aa5cSAndroid Build Coastguard Worker 259*dfc6aa5cSAndroid Build Coastguard WorkerA data buffer control module may itself be viewed as a processing step by a 260*dfc6aa5cSAndroid Build Coastguard Workerhigher-level control module; thus the control modules form a binary tree with 261*dfc6aa5cSAndroid Build Coastguard Workerelementary processing steps at the leaves of the tree. 262*dfc6aa5cSAndroid Build Coastguard Worker 263*dfc6aa5cSAndroid Build Coastguard WorkerThe control modules are objects. A considerable amount of flexibility can 264*dfc6aa5cSAndroid Build Coastguard Workerbe had by replacing implementations of a control module. For example: 265*dfc6aa5cSAndroid Build Coastguard Worker* Merging of adjacent steps in the pipeline is done by replacing a control 266*dfc6aa5cSAndroid Build Coastguard Worker module and its pair of processing-step modules with a single processing- 267*dfc6aa5cSAndroid Build Coastguard Worker step module. (Hence the possible merges are determined by the tree of 268*dfc6aa5cSAndroid Build Coastguard Worker control modules.) 269*dfc6aa5cSAndroid Build Coastguard Worker* In some processing modes, a given interstep buffer need only be a "strip" 270*dfc6aa5cSAndroid Build Coastguard Worker buffer large enough to accommodate the desired data chunk sizes. In other 271*dfc6aa5cSAndroid Build Coastguard Worker modes, a full-image buffer is needed and several passes are required. 272*dfc6aa5cSAndroid Build Coastguard Worker The control module determines which kind of buffer is used and manipulates 273*dfc6aa5cSAndroid Build Coastguard Worker virtual array buffers as needed. One or both processing steps may be 274*dfc6aa5cSAndroid Build Coastguard Worker unaware of the multi-pass behavior. 275*dfc6aa5cSAndroid Build Coastguard Worker 276*dfc6aa5cSAndroid Build Coastguard WorkerIn theory, we might be able to make all of the data buffer controllers 277*dfc6aa5cSAndroid Build Coastguard Workerinterchangeable and provide just one set of implementations for all. In 278*dfc6aa5cSAndroid Build Coastguard Workerpractice, each one contains considerable special-case processing for its 279*dfc6aa5cSAndroid Build Coastguard Workerparticular job. The buffer controller concept should be regarded as an 280*dfc6aa5cSAndroid Build Coastguard Workeroverall system structuring principle, not as a complete description of the 281*dfc6aa5cSAndroid Build Coastguard Workertask performed by any one controller. 282*dfc6aa5cSAndroid Build Coastguard Worker 283*dfc6aa5cSAndroid Build Coastguard Worker 284*dfc6aa5cSAndroid Build Coastguard Worker*** Compression object structure *** 285*dfc6aa5cSAndroid Build Coastguard Worker 286*dfc6aa5cSAndroid Build Coastguard WorkerHere is a sketch of the logical structure of the JPEG compression library: 287*dfc6aa5cSAndroid Build Coastguard Worker 288*dfc6aa5cSAndroid Build Coastguard Worker |-- Colorspace conversion 289*dfc6aa5cSAndroid Build Coastguard Worker |-- Preprocessing controller --| 290*dfc6aa5cSAndroid Build Coastguard Worker | |-- Downsampling 291*dfc6aa5cSAndroid Build Coastguard WorkerMain controller --| 292*dfc6aa5cSAndroid Build Coastguard Worker | |-- Forward DCT, quantize 293*dfc6aa5cSAndroid Build Coastguard Worker |-- Coefficient controller --| 294*dfc6aa5cSAndroid Build Coastguard Worker |-- Entropy encoding 295*dfc6aa5cSAndroid Build Coastguard Worker 296*dfc6aa5cSAndroid Build Coastguard WorkerThis sketch also describes the flow of control (subroutine calls) during 297*dfc6aa5cSAndroid Build Coastguard Workertypical image data processing. Each of the components shown in the diagram is 298*dfc6aa5cSAndroid Build Coastguard Workeran "object" which may have several different implementations available. One 299*dfc6aa5cSAndroid Build Coastguard Workeror more source code files contain the actual implementation(s) of each object. 300*dfc6aa5cSAndroid Build Coastguard Worker 301*dfc6aa5cSAndroid Build Coastguard WorkerThe objects shown above are: 302*dfc6aa5cSAndroid Build Coastguard Worker 303*dfc6aa5cSAndroid Build Coastguard Worker* Main controller: buffer controller for the subsampled-data buffer, which 304*dfc6aa5cSAndroid Build Coastguard Worker holds the preprocessed input data. This controller invokes preprocessing to 305*dfc6aa5cSAndroid Build Coastguard Worker fill the subsampled-data buffer, and JPEG compression to empty it. There is 306*dfc6aa5cSAndroid Build Coastguard Worker usually no need for a full-image buffer here; a strip buffer is adequate. 307*dfc6aa5cSAndroid Build Coastguard Worker 308*dfc6aa5cSAndroid Build Coastguard Worker* Preprocessing controller: buffer controller for the downsampling input data 309*dfc6aa5cSAndroid Build Coastguard Worker buffer, which lies between colorspace conversion and downsampling. Note 310*dfc6aa5cSAndroid Build Coastguard Worker that a unified conversion/downsampling module would probably replace this 311*dfc6aa5cSAndroid Build Coastguard Worker controller entirely. 312*dfc6aa5cSAndroid Build Coastguard Worker 313*dfc6aa5cSAndroid Build Coastguard Worker* Colorspace conversion: converts application image data into the desired 314*dfc6aa5cSAndroid Build Coastguard Worker JPEG color space; also changes the data from pixel-interleaved layout to 315*dfc6aa5cSAndroid Build Coastguard Worker separate component planes. Processes one pixel row at a time. 316*dfc6aa5cSAndroid Build Coastguard Worker 317*dfc6aa5cSAndroid Build Coastguard Worker* Downsampling: performs reduction of chroma components as required. 318*dfc6aa5cSAndroid Build Coastguard Worker Optionally may perform pixel-level smoothing as well. Processes a "row 319*dfc6aa5cSAndroid Build Coastguard Worker group" at a time, where a row group is defined as Vmax pixel rows of each 320*dfc6aa5cSAndroid Build Coastguard Worker component before downsampling, and Vk sample rows afterwards (remember Vk 321*dfc6aa5cSAndroid Build Coastguard Worker differs across components). Some downsampling or smoothing algorithms may 322*dfc6aa5cSAndroid Build Coastguard Worker require context rows above and below the current row group; the 323*dfc6aa5cSAndroid Build Coastguard Worker preprocessing controller is responsible for supplying these rows via proper 324*dfc6aa5cSAndroid Build Coastguard Worker buffering. The downsampler is responsible for edge expansion at the right 325*dfc6aa5cSAndroid Build Coastguard Worker edge (i.e., extending each sample row to a multiple of 8 samples); but the 326*dfc6aa5cSAndroid Build Coastguard Worker preprocessing controller is responsible for vertical edge expansion (i.e., 327*dfc6aa5cSAndroid Build Coastguard Worker duplicating the bottom sample row as needed to make a multiple of 8 rows). 328*dfc6aa5cSAndroid Build Coastguard Worker 329*dfc6aa5cSAndroid Build Coastguard Worker* Coefficient controller: buffer controller for the DCT-coefficient data. 330*dfc6aa5cSAndroid Build Coastguard Worker This controller handles MCU assembly, including insertion of dummy DCT 331*dfc6aa5cSAndroid Build Coastguard Worker blocks when needed at the right or bottom edge. When performing 332*dfc6aa5cSAndroid Build Coastguard Worker Huffman-code optimization or emitting a multiscan JPEG file, this 333*dfc6aa5cSAndroid Build Coastguard Worker controller is responsible for buffering the full image. The equivalent of 334*dfc6aa5cSAndroid Build Coastguard Worker one fully interleaved MCU row of subsampled data is processed per call, 335*dfc6aa5cSAndroid Build Coastguard Worker even when the JPEG file is noninterleaved. 336*dfc6aa5cSAndroid Build Coastguard Worker 337*dfc6aa5cSAndroid Build Coastguard Worker* Forward DCT and quantization: Perform DCT, quantize, and emit coefficients. 338*dfc6aa5cSAndroid Build Coastguard Worker Works on one or more DCT blocks at a time. (Note: the coefficients are now 339*dfc6aa5cSAndroid Build Coastguard Worker emitted in normal array order, which the entropy encoder is expected to 340*dfc6aa5cSAndroid Build Coastguard Worker convert to zigzag order as necessary. Prior versions of the IJG code did 341*dfc6aa5cSAndroid Build Coastguard Worker the conversion to zigzag order within the quantization step.) 342*dfc6aa5cSAndroid Build Coastguard Worker 343*dfc6aa5cSAndroid Build Coastguard Worker* Entropy encoding: Perform Huffman or arithmetic entropy coding and emit the 344*dfc6aa5cSAndroid Build Coastguard Worker coded data to the data destination module. Works on one MCU per call. 345*dfc6aa5cSAndroid Build Coastguard Worker For progressive JPEG, the same DCT blocks are fed to the entropy coder 346*dfc6aa5cSAndroid Build Coastguard Worker during each pass, and the coder must emit the appropriate subset of 347*dfc6aa5cSAndroid Build Coastguard Worker coefficients. 348*dfc6aa5cSAndroid Build Coastguard Worker 349*dfc6aa5cSAndroid Build Coastguard WorkerIn addition to the above objects, the compression library includes these 350*dfc6aa5cSAndroid Build Coastguard Workerobjects: 351*dfc6aa5cSAndroid Build Coastguard Worker 352*dfc6aa5cSAndroid Build Coastguard Worker* Master control: determines the number of passes required, controls overall 353*dfc6aa5cSAndroid Build Coastguard Worker and per-pass initialization of the other modules. 354*dfc6aa5cSAndroid Build Coastguard Worker 355*dfc6aa5cSAndroid Build Coastguard Worker* Marker writing: generates JPEG markers (except for RSTn, which is emitted 356*dfc6aa5cSAndroid Build Coastguard Worker by the entropy encoder when needed). 357*dfc6aa5cSAndroid Build Coastguard Worker 358*dfc6aa5cSAndroid Build Coastguard Worker* Data destination manager: writes the output JPEG datastream to its final 359*dfc6aa5cSAndroid Build Coastguard Worker destination (e.g., a file). The destination manager supplied with the 360*dfc6aa5cSAndroid Build Coastguard Worker library knows how to write to a stdio stream or to a memory buffer; 361*dfc6aa5cSAndroid Build Coastguard Worker for other behaviors, the surrounding application may provide its own 362*dfc6aa5cSAndroid Build Coastguard Worker destination manager. 363*dfc6aa5cSAndroid Build Coastguard Worker 364*dfc6aa5cSAndroid Build Coastguard Worker* Memory manager: allocates and releases memory, controls virtual arrays 365*dfc6aa5cSAndroid Build Coastguard Worker (with backing store management, where required). 366*dfc6aa5cSAndroid Build Coastguard Worker 367*dfc6aa5cSAndroid Build Coastguard Worker* Error handler: performs formatting and output of error and trace messages; 368*dfc6aa5cSAndroid Build Coastguard Worker determines handling of nonfatal errors. The surrounding application may 369*dfc6aa5cSAndroid Build Coastguard Worker override some or all of this object's methods to change error handling. 370*dfc6aa5cSAndroid Build Coastguard Worker 371*dfc6aa5cSAndroid Build Coastguard Worker* Progress monitor: supports output of "percent-done" progress reports. 372*dfc6aa5cSAndroid Build Coastguard Worker This object represents an optional callback to the surrounding application: 373*dfc6aa5cSAndroid Build Coastguard Worker if wanted, it must be supplied by the application. 374*dfc6aa5cSAndroid Build Coastguard Worker 375*dfc6aa5cSAndroid Build Coastguard WorkerThe error handler, destination manager, and progress monitor objects are 376*dfc6aa5cSAndroid Build Coastguard Workerdefined as separate objects in order to simplify application-specific 377*dfc6aa5cSAndroid Build Coastguard Workercustomization of the JPEG library. A surrounding application may override 378*dfc6aa5cSAndroid Build Coastguard Workerindividual methods or supply its own all-new implementation of one of these 379*dfc6aa5cSAndroid Build Coastguard Workerobjects. The object interfaces for these objects are therefore treated as 380*dfc6aa5cSAndroid Build Coastguard Workerpart of the application interface of the library, whereas the other objects 381*dfc6aa5cSAndroid Build Coastguard Workerare internal to the library. 382*dfc6aa5cSAndroid Build Coastguard Worker 383*dfc6aa5cSAndroid Build Coastguard WorkerThe error handler and memory manager are shared by JPEG compression and 384*dfc6aa5cSAndroid Build Coastguard Workerdecompression; the progress monitor, if used, may be shared as well. 385*dfc6aa5cSAndroid Build Coastguard Worker 386*dfc6aa5cSAndroid Build Coastguard Worker 387*dfc6aa5cSAndroid Build Coastguard Worker*** Decompression object structure *** 388*dfc6aa5cSAndroid Build Coastguard Worker 389*dfc6aa5cSAndroid Build Coastguard WorkerHere is a sketch of the logical structure of the JPEG decompression library: 390*dfc6aa5cSAndroid Build Coastguard Worker 391*dfc6aa5cSAndroid Build Coastguard Worker |-- Entropy decoding 392*dfc6aa5cSAndroid Build Coastguard Worker |-- Coefficient controller --| 393*dfc6aa5cSAndroid Build Coastguard Worker | |-- Dequantize, Inverse DCT 394*dfc6aa5cSAndroid Build Coastguard WorkerMain controller --| 395*dfc6aa5cSAndroid Build Coastguard Worker | |-- Upsampling 396*dfc6aa5cSAndroid Build Coastguard Worker |-- Postprocessing controller --| |-- Colorspace conversion 397*dfc6aa5cSAndroid Build Coastguard Worker |-- Color quantization 398*dfc6aa5cSAndroid Build Coastguard Worker |-- Color precision reduction 399*dfc6aa5cSAndroid Build Coastguard Worker 400*dfc6aa5cSAndroid Build Coastguard WorkerAs before, this diagram also represents typical control flow. The objects 401*dfc6aa5cSAndroid Build Coastguard Workershown are: 402*dfc6aa5cSAndroid Build Coastguard Worker 403*dfc6aa5cSAndroid Build Coastguard Worker* Main controller: buffer controller for the subsampled-data buffer, which 404*dfc6aa5cSAndroid Build Coastguard Worker holds the output of JPEG decompression proper. This controller's primary 405*dfc6aa5cSAndroid Build Coastguard Worker task is to feed the postprocessing procedure. Some upsampling algorithms 406*dfc6aa5cSAndroid Build Coastguard Worker may require context rows above and below the current row group; when this 407*dfc6aa5cSAndroid Build Coastguard Worker is true, the main controller is responsible for managing its buffer so as 408*dfc6aa5cSAndroid Build Coastguard Worker to make context rows available. In the current design, the main buffer is 409*dfc6aa5cSAndroid Build Coastguard Worker always a strip buffer; a full-image buffer is never required. 410*dfc6aa5cSAndroid Build Coastguard Worker 411*dfc6aa5cSAndroid Build Coastguard Worker* Coefficient controller: buffer controller for the DCT-coefficient data. 412*dfc6aa5cSAndroid Build Coastguard Worker This controller handles MCU disassembly, including deletion of any dummy 413*dfc6aa5cSAndroid Build Coastguard Worker DCT blocks at the right or bottom edge. When reading a multiscan JPEG 414*dfc6aa5cSAndroid Build Coastguard Worker file, this controller is responsible for buffering the full image. 415*dfc6aa5cSAndroid Build Coastguard Worker (Buffering DCT coefficients, rather than samples, is necessary to support 416*dfc6aa5cSAndroid Build Coastguard Worker progressive JPEG.) The equivalent of one fully interleaved MCU row of 417*dfc6aa5cSAndroid Build Coastguard Worker subsampled data is processed per call, even when the source JPEG file is 418*dfc6aa5cSAndroid Build Coastguard Worker noninterleaved. 419*dfc6aa5cSAndroid Build Coastguard Worker 420*dfc6aa5cSAndroid Build Coastguard Worker* Entropy decoding: Read coded data from the data source module and perform 421*dfc6aa5cSAndroid Build Coastguard Worker Huffman or arithmetic entropy decoding. Works on one MCU per call. 422*dfc6aa5cSAndroid Build Coastguard Worker For progressive JPEG decoding, the coefficient controller supplies the prior 423*dfc6aa5cSAndroid Build Coastguard Worker coefficients of each MCU (initially all zeroes), which the entropy decoder 424*dfc6aa5cSAndroid Build Coastguard Worker modifies in each scan. 425*dfc6aa5cSAndroid Build Coastguard Worker 426*dfc6aa5cSAndroid Build Coastguard Worker* Dequantization and inverse DCT: like it says. Note that the coefficients 427*dfc6aa5cSAndroid Build Coastguard Worker buffered by the coefficient controller have NOT been dequantized; we 428*dfc6aa5cSAndroid Build Coastguard Worker merge dequantization and inverse DCT into a single step for speed reasons. 429*dfc6aa5cSAndroid Build Coastguard Worker When scaled-down output is asked for, simplified DCT algorithms may be used 430*dfc6aa5cSAndroid Build Coastguard Worker that emit fewer samples per DCT block, not the full 8x8. Works on one DCT 431*dfc6aa5cSAndroid Build Coastguard Worker block at a time. 432*dfc6aa5cSAndroid Build Coastguard Worker 433*dfc6aa5cSAndroid Build Coastguard Worker* Postprocessing controller: buffer controller for the color quantization 434*dfc6aa5cSAndroid Build Coastguard Worker input buffer, when quantization is in use. (Without quantization, this 435*dfc6aa5cSAndroid Build Coastguard Worker controller just calls the upsampler.) For two-pass quantization, this 436*dfc6aa5cSAndroid Build Coastguard Worker controller is responsible for buffering the full-image data. 437*dfc6aa5cSAndroid Build Coastguard Worker 438*dfc6aa5cSAndroid Build Coastguard Worker* Upsampling: restores chroma components to full size. (May support more 439*dfc6aa5cSAndroid Build Coastguard Worker general output rescaling, too. Note that if undersized DCT outputs have 440*dfc6aa5cSAndroid Build Coastguard Worker been emitted by the DCT module, this module must adjust so that properly 441*dfc6aa5cSAndroid Build Coastguard Worker sized outputs are created.) Works on one row group at a time. This module 442*dfc6aa5cSAndroid Build Coastguard Worker also calls the color conversion module, so its top level is effectively a 443*dfc6aa5cSAndroid Build Coastguard Worker buffer controller for the upsampling->color conversion buffer. However, in 444*dfc6aa5cSAndroid Build Coastguard Worker all but the highest-quality operating modes, upsampling and color 445*dfc6aa5cSAndroid Build Coastguard Worker conversion are likely to be merged into a single step. 446*dfc6aa5cSAndroid Build Coastguard Worker 447*dfc6aa5cSAndroid Build Coastguard Worker* Colorspace conversion: convert from JPEG color space to output color space, 448*dfc6aa5cSAndroid Build Coastguard Worker and change data layout from separate component planes to pixel-interleaved. 449*dfc6aa5cSAndroid Build Coastguard Worker Works on one pixel row at a time. 450*dfc6aa5cSAndroid Build Coastguard Worker 451*dfc6aa5cSAndroid Build Coastguard Worker* Color quantization: reduce the data to colormapped form, using either an 452*dfc6aa5cSAndroid Build Coastguard Worker externally specified colormap or an internally generated one. This module 453*dfc6aa5cSAndroid Build Coastguard Worker is not used for full-color output. Works on one pixel row at a time; may 454*dfc6aa5cSAndroid Build Coastguard Worker require two passes to generate a color map. Note that the output will 455*dfc6aa5cSAndroid Build Coastguard Worker always be a single component representing colormap indexes. In the current 456*dfc6aa5cSAndroid Build Coastguard Worker design, the output values are JSAMPLEs, so an 8-bit compilation cannot 457*dfc6aa5cSAndroid Build Coastguard Worker quantize to more than 256 colors. This is unlikely to be a problem in 458*dfc6aa5cSAndroid Build Coastguard Worker practice. 459*dfc6aa5cSAndroid Build Coastguard Worker 460*dfc6aa5cSAndroid Build Coastguard Worker* Color reduction: this module handles color precision reduction, e.g., 461*dfc6aa5cSAndroid Build Coastguard Worker generating 15-bit color (5 bits/primary) from JPEG's 24-bit output. 462*dfc6aa5cSAndroid Build Coastguard Worker Not quite clear yet how this should be handled... should we merge it with 463*dfc6aa5cSAndroid Build Coastguard Worker colorspace conversion??? 464*dfc6aa5cSAndroid Build Coastguard Worker 465*dfc6aa5cSAndroid Build Coastguard WorkerNote that some high-speed operating modes might condense the entire 466*dfc6aa5cSAndroid Build Coastguard Workerpostprocessing sequence to a single module (upsample, color convert, and 467*dfc6aa5cSAndroid Build Coastguard Workerquantize in one step). 468*dfc6aa5cSAndroid Build Coastguard Worker 469*dfc6aa5cSAndroid Build Coastguard WorkerIn addition to the above objects, the decompression library includes these 470*dfc6aa5cSAndroid Build Coastguard Workerobjects: 471*dfc6aa5cSAndroid Build Coastguard Worker 472*dfc6aa5cSAndroid Build Coastguard Worker* Master control: determines the number of passes required, controls overall 473*dfc6aa5cSAndroid Build Coastguard Worker and per-pass initialization of the other modules. This is subdivided into 474*dfc6aa5cSAndroid Build Coastguard Worker input and output control: jdinput.c controls only input-side processing, 475*dfc6aa5cSAndroid Build Coastguard Worker while jdmaster.c handles overall initialization and output-side control. 476*dfc6aa5cSAndroid Build Coastguard Worker 477*dfc6aa5cSAndroid Build Coastguard Worker* Marker reading: decodes JPEG markers (except for RSTn). 478*dfc6aa5cSAndroid Build Coastguard Worker 479*dfc6aa5cSAndroid Build Coastguard Worker* Data source manager: supplies the input JPEG datastream. The source 480*dfc6aa5cSAndroid Build Coastguard Worker manager supplied with the library knows how to read from a stdio stream 481*dfc6aa5cSAndroid Build Coastguard Worker or from a memory buffer; for other behaviors, the surrounding application 482*dfc6aa5cSAndroid Build Coastguard Worker may provide its own source manager. 483*dfc6aa5cSAndroid Build Coastguard Worker 484*dfc6aa5cSAndroid Build Coastguard Worker* Memory manager: same as for compression library. 485*dfc6aa5cSAndroid Build Coastguard Worker 486*dfc6aa5cSAndroid Build Coastguard Worker* Error handler: same as for compression library. 487*dfc6aa5cSAndroid Build Coastguard Worker 488*dfc6aa5cSAndroid Build Coastguard Worker* Progress monitor: same as for compression library. 489*dfc6aa5cSAndroid Build Coastguard Worker 490*dfc6aa5cSAndroid Build Coastguard WorkerAs with compression, the data source manager, error handler, and progress 491*dfc6aa5cSAndroid Build Coastguard Workermonitor are candidates for replacement by a surrounding application. 492*dfc6aa5cSAndroid Build Coastguard Worker 493*dfc6aa5cSAndroid Build Coastguard Worker 494*dfc6aa5cSAndroid Build Coastguard Worker*** Decompression input and output separation *** 495*dfc6aa5cSAndroid Build Coastguard Worker 496*dfc6aa5cSAndroid Build Coastguard WorkerTo support efficient incremental display of progressive JPEG files, the 497*dfc6aa5cSAndroid Build Coastguard Workerdecompressor is divided into two sections that can run independently: 498*dfc6aa5cSAndroid Build Coastguard Worker 499*dfc6aa5cSAndroid Build Coastguard Worker1. Data input includes marker parsing, entropy decoding, and input into the 500*dfc6aa5cSAndroid Build Coastguard Worker coefficient controller's DCT coefficient buffer. Note that this 501*dfc6aa5cSAndroid Build Coastguard Worker processing is relatively cheap and fast. 502*dfc6aa5cSAndroid Build Coastguard Worker 503*dfc6aa5cSAndroid Build Coastguard Worker2. Data output reads from the DCT coefficient buffer and performs the IDCT 504*dfc6aa5cSAndroid Build Coastguard Worker and all postprocessing steps. 505*dfc6aa5cSAndroid Build Coastguard Worker 506*dfc6aa5cSAndroid Build Coastguard WorkerFor a progressive JPEG file, the data input processing is allowed to get 507*dfc6aa5cSAndroid Build Coastguard Workerarbitrarily far ahead of the data output processing. (This occurs only 508*dfc6aa5cSAndroid Build Coastguard Workerif the application calls jpeg_consume_input(); otherwise input and output 509*dfc6aa5cSAndroid Build Coastguard Workerrun in lockstep, since the input section is called only when the output 510*dfc6aa5cSAndroid Build Coastguard Workersection needs more data.) In this way the application can avoid making 511*dfc6aa5cSAndroid Build Coastguard Workerextra display passes when data is arriving faster than the display pass 512*dfc6aa5cSAndroid Build Coastguard Workercan run. Furthermore, it is possible to abort an output pass without 513*dfc6aa5cSAndroid Build Coastguard Workerlosing anything, since the coefficient buffer is read-only as far as the 514*dfc6aa5cSAndroid Build Coastguard Workeroutput section is concerned. See libjpeg.txt for more detail. 515*dfc6aa5cSAndroid Build Coastguard Worker 516*dfc6aa5cSAndroid Build Coastguard WorkerA full-image coefficient array is only created if the JPEG file has multiple 517*dfc6aa5cSAndroid Build Coastguard Workerscans (or if the application specifies buffered-image mode anyway). When 518*dfc6aa5cSAndroid Build Coastguard Workerreading a single-scan file, the coefficient controller normally creates only 519*dfc6aa5cSAndroid Build Coastguard Workera one-MCU buffer, so input and output processing must run in lockstep in this 520*dfc6aa5cSAndroid Build Coastguard Workercase. jpeg_consume_input() is effectively a no-op in this situation. 521*dfc6aa5cSAndroid Build Coastguard Worker 522*dfc6aa5cSAndroid Build Coastguard WorkerThe main impact of dividing the decompressor in this fashion is that we must 523*dfc6aa5cSAndroid Build Coastguard Workerbe very careful with shared variables in the cinfo data structure. Each 524*dfc6aa5cSAndroid Build Coastguard Workervariable that can change during the course of decompression must be 525*dfc6aa5cSAndroid Build Coastguard Workerclassified as belonging to data input or data output, and each section must 526*dfc6aa5cSAndroid Build Coastguard Workerlook only at its own variables. For example, the data output section may not 527*dfc6aa5cSAndroid Build Coastguard Workerdepend on any of the variables that describe the current scan in the JPEG 528*dfc6aa5cSAndroid Build Coastguard Workerfile, because these may change as the data input section advances into a new 529*dfc6aa5cSAndroid Build Coastguard Workerscan. 530*dfc6aa5cSAndroid Build Coastguard Worker 531*dfc6aa5cSAndroid Build Coastguard WorkerThe progress monitor is (somewhat arbitrarily) defined to treat input of the 532*dfc6aa5cSAndroid Build Coastguard Workerfile as one pass when buffered-image mode is not used, and to ignore data 533*dfc6aa5cSAndroid Build Coastguard Workerinput work completely when buffered-image mode is used. Note that the 534*dfc6aa5cSAndroid Build Coastguard Workerlibrary has no reliable way to predict the number of passes when dealing 535*dfc6aa5cSAndroid Build Coastguard Workerwith a progressive JPEG file, nor can it predict the number of output passes 536*dfc6aa5cSAndroid Build Coastguard Workerin buffered-image mode. So the work estimate is inherently bogus anyway. 537*dfc6aa5cSAndroid Build Coastguard Worker 538*dfc6aa5cSAndroid Build Coastguard WorkerNo comparable division is currently made in the compression library, because 539*dfc6aa5cSAndroid Build Coastguard Workerthere isn't any real need for it. 540*dfc6aa5cSAndroid Build Coastguard Worker 541*dfc6aa5cSAndroid Build Coastguard Worker 542*dfc6aa5cSAndroid Build Coastguard Worker*** Data formats *** 543*dfc6aa5cSAndroid Build Coastguard Worker 544*dfc6aa5cSAndroid Build Coastguard WorkerArrays of pixel sample values use the following data structure: 545*dfc6aa5cSAndroid Build Coastguard Worker 546*dfc6aa5cSAndroid Build Coastguard Worker typedef something JSAMPLE; a pixel component value, 0..MAXJSAMPLE 547*dfc6aa5cSAndroid Build Coastguard Worker typedef JSAMPLE *JSAMPROW; ptr to a row of samples 548*dfc6aa5cSAndroid Build Coastguard Worker typedef JSAMPROW *JSAMPARRAY; ptr to a list of rows 549*dfc6aa5cSAndroid Build Coastguard Worker typedef JSAMPARRAY *JSAMPIMAGE; ptr to a list of color-component arrays 550*dfc6aa5cSAndroid Build Coastguard Worker 551*dfc6aa5cSAndroid Build Coastguard WorkerThe basic element type JSAMPLE will be one of unsigned char or short. Short 552*dfc6aa5cSAndroid Build Coastguard Workerwill be used if samples wider than 8 bits are to be supported (this is a 553*dfc6aa5cSAndroid Build Coastguard Workercompile-time option). Otherwise, unsigned char is used. 554*dfc6aa5cSAndroid Build Coastguard Worker 555*dfc6aa5cSAndroid Build Coastguard WorkerWith these conventions, JSAMPLE values can be assumed to be >= 0. This helps 556*dfc6aa5cSAndroid Build Coastguard Workersimplify correct rounding during downsampling, etc. The JPEG standard's 557*dfc6aa5cSAndroid Build Coastguard Workerspecification that sample values run from -128..127 is accommodated by 558*dfc6aa5cSAndroid Build Coastguard Workersubtracting 128 from the sample value in the DCT step. Similarly, during 559*dfc6aa5cSAndroid Build Coastguard Workerdecompression the output of the IDCT step will be immediately shifted back to 560*dfc6aa5cSAndroid Build Coastguard Worker0..255. (NB: different values are required when 12-bit samples are in use. 561*dfc6aa5cSAndroid Build Coastguard WorkerThe code is written in terms of MAXJSAMPLE and CENTERJSAMPLE, which will be 562*dfc6aa5cSAndroid Build Coastguard Workerdefined as 255 and 128 respectively in an 8-bit implementation, and as 4095 563*dfc6aa5cSAndroid Build Coastguard Workerand 2048 in a 12-bit implementation.) 564*dfc6aa5cSAndroid Build Coastguard Worker 565*dfc6aa5cSAndroid Build Coastguard WorkerWe use a pointer per row, rather than a two-dimensional JSAMPLE array. This 566*dfc6aa5cSAndroid Build Coastguard Workerchoice costs only a small amount of memory and has several benefits: 567*dfc6aa5cSAndroid Build Coastguard Worker* Code using the data structure doesn't need to know the allocated width of 568*dfc6aa5cSAndroid Build Coastguard Worker the rows. This simplifies edge expansion/compression, since we can work 569*dfc6aa5cSAndroid Build Coastguard Worker in an array that's wider than the logical picture width. 570*dfc6aa5cSAndroid Build Coastguard Worker* Indexing doesn't require multiplication; this is a performance win on many 571*dfc6aa5cSAndroid Build Coastguard Worker machines. 572*dfc6aa5cSAndroid Build Coastguard Worker* Arrays with more than 64K total elements can be supported even on machines 573*dfc6aa5cSAndroid Build Coastguard Worker where malloc() cannot allocate chunks larger than 64K. 574*dfc6aa5cSAndroid Build Coastguard Worker* The rows forming a component array may be allocated at different times 575*dfc6aa5cSAndroid Build Coastguard Worker without extra copying. This trick allows some speedups in smoothing steps 576*dfc6aa5cSAndroid Build Coastguard Worker that need access to the previous and next rows. 577*dfc6aa5cSAndroid Build Coastguard Worker 578*dfc6aa5cSAndroid Build Coastguard WorkerNote that each color component is stored in a separate array; we don't use the 579*dfc6aa5cSAndroid Build Coastguard Workertraditional layout in which the components of a pixel are stored together. 580*dfc6aa5cSAndroid Build Coastguard WorkerThis simplifies coding of modules that work on each component independently, 581*dfc6aa5cSAndroid Build Coastguard Workerbecause they don't need to know how many components there are. Furthermore, 582*dfc6aa5cSAndroid Build Coastguard Workerwe can read or write each component to a temporary file independently, which 583*dfc6aa5cSAndroid Build Coastguard Workeris helpful when dealing with noninterleaved JPEG files. 584*dfc6aa5cSAndroid Build Coastguard Worker 585*dfc6aa5cSAndroid Build Coastguard WorkerIn general, a specific sample value is accessed by code such as 586*dfc6aa5cSAndroid Build Coastguard Worker image[colorcomponent][row][col] 587*dfc6aa5cSAndroid Build Coastguard Workerwhere col is measured from the image left edge, but row is measured from the 588*dfc6aa5cSAndroid Build Coastguard Workerfirst sample row currently in memory. Either of the first two indexings can 589*dfc6aa5cSAndroid Build Coastguard Workerbe precomputed by copying the relevant pointer. 590*dfc6aa5cSAndroid Build Coastguard Worker 591*dfc6aa5cSAndroid Build Coastguard Worker 592*dfc6aa5cSAndroid Build Coastguard WorkerSince most image-processing applications prefer to work on images in which 593*dfc6aa5cSAndroid Build Coastguard Workerthe components of a pixel are stored together, the data passed to or from the 594*dfc6aa5cSAndroid Build Coastguard Workersurrounding application uses the traditional convention: a single pixel is 595*dfc6aa5cSAndroid Build Coastguard Workerrepresented by N consecutive JSAMPLE values, and an image row is an array of 596*dfc6aa5cSAndroid Build Coastguard Worker(# of color components)*(image width) JSAMPLEs. One or more rows of data can 597*dfc6aa5cSAndroid Build Coastguard Workerbe represented by a pointer of type JSAMPARRAY in this scheme. This scheme is 598*dfc6aa5cSAndroid Build Coastguard Workerconverted to component-wise storage inside the JPEG library. (Applications 599*dfc6aa5cSAndroid Build Coastguard Workerthat want to skip JPEG preprocessing or postprocessing will have to contend 600*dfc6aa5cSAndroid Build Coastguard Workerwith component-wise storage.) 601*dfc6aa5cSAndroid Build Coastguard Worker 602*dfc6aa5cSAndroid Build Coastguard Worker 603*dfc6aa5cSAndroid Build Coastguard WorkerArrays of DCT-coefficient values use the following data structure: 604*dfc6aa5cSAndroid Build Coastguard Worker 605*dfc6aa5cSAndroid Build Coastguard Worker typedef short JCOEF; a 16-bit signed integer 606*dfc6aa5cSAndroid Build Coastguard Worker typedef JCOEF JBLOCK[DCTSIZE2]; an 8x8 block of coefficients 607*dfc6aa5cSAndroid Build Coastguard Worker typedef JBLOCK *JBLOCKROW; ptr to one horizontal row of 8x8 blocks 608*dfc6aa5cSAndroid Build Coastguard Worker typedef JBLOCKROW *JBLOCKARRAY; ptr to a list of such rows 609*dfc6aa5cSAndroid Build Coastguard Worker typedef JBLOCKARRAY *JBLOCKIMAGE; ptr to a list of color component arrays 610*dfc6aa5cSAndroid Build Coastguard Worker 611*dfc6aa5cSAndroid Build Coastguard WorkerThe underlying type is at least a 16-bit signed integer; while "short" is big 612*dfc6aa5cSAndroid Build Coastguard Workerenough on all machines of interest, on some machines it is preferable to use 613*dfc6aa5cSAndroid Build Coastguard Worker"int" for speed reasons, despite the storage cost. Coefficients are grouped 614*dfc6aa5cSAndroid Build Coastguard Workerinto 8x8 blocks (but we always use #defines DCTSIZE and DCTSIZE2 rather than 615*dfc6aa5cSAndroid Build Coastguard Worker"8" and "64"). 616*dfc6aa5cSAndroid Build Coastguard Worker 617*dfc6aa5cSAndroid Build Coastguard WorkerThe contents of a coefficient block may be in either "natural" or zigzagged 618*dfc6aa5cSAndroid Build Coastguard Workerorder, and may be true values or divided by the quantization coefficients, 619*dfc6aa5cSAndroid Build Coastguard Workerdepending on where the block is in the processing pipeline. In the current 620*dfc6aa5cSAndroid Build Coastguard Workerlibrary, coefficient blocks are kept in natural order everywhere; the entropy 621*dfc6aa5cSAndroid Build Coastguard Workercodecs zigzag or dezigzag the data as it is written or read. The blocks 622*dfc6aa5cSAndroid Build Coastguard Workercontain quantized coefficients everywhere outside the DCT/IDCT subsystems. 623*dfc6aa5cSAndroid Build Coastguard Worker(This latter decision may need to be revisited to support variable 624*dfc6aa5cSAndroid Build Coastguard Workerquantization a la JPEG Part 3.) 625*dfc6aa5cSAndroid Build Coastguard Worker 626*dfc6aa5cSAndroid Build Coastguard WorkerNotice that the allocation unit is now a row of 8x8 blocks, corresponding to 627*dfc6aa5cSAndroid Build Coastguard Workereight rows of samples. Otherwise the structure is much the same as for 628*dfc6aa5cSAndroid Build Coastguard Workersamples, and for the same reasons. 629*dfc6aa5cSAndroid Build Coastguard Worker 630*dfc6aa5cSAndroid Build Coastguard Worker 631*dfc6aa5cSAndroid Build Coastguard Worker*** Suspendable processing *** 632*dfc6aa5cSAndroid Build Coastguard Worker 633*dfc6aa5cSAndroid Build Coastguard WorkerIn some applications it is desirable to use the JPEG library as an 634*dfc6aa5cSAndroid Build Coastguard Workerincremental, memory-to-memory filter. In this situation the data source or 635*dfc6aa5cSAndroid Build Coastguard Workerdestination may be a limited-size buffer, and we can't rely on being able to 636*dfc6aa5cSAndroid Build Coastguard Workerempty or refill the buffer at arbitrary times. Instead the application would 637*dfc6aa5cSAndroid Build Coastguard Workerlike to have control return from the library at buffer overflow/underrun, and 638*dfc6aa5cSAndroid Build Coastguard Workerthen resume compression or decompression at a later time. 639*dfc6aa5cSAndroid Build Coastguard Worker 640*dfc6aa5cSAndroid Build Coastguard WorkerThis scenario is supported for simple cases. (For anything more complex, we 641*dfc6aa5cSAndroid Build Coastguard Workerrecommend that the application "bite the bullet" and develop real multitasking 642*dfc6aa5cSAndroid Build Coastguard Workercapability.) The libjpeg.txt file goes into more detail about the usage and 643*dfc6aa5cSAndroid Build Coastguard Workerlimitations of this capability; here we address the implications for library 644*dfc6aa5cSAndroid Build Coastguard Workerstructure. 645*dfc6aa5cSAndroid Build Coastguard Worker 646*dfc6aa5cSAndroid Build Coastguard WorkerThe essence of the problem is that the entropy codec (coder or decoder) must 647*dfc6aa5cSAndroid Build Coastguard Workerbe prepared to stop at arbitrary times. In turn, the controllers that call 648*dfc6aa5cSAndroid Build Coastguard Workerthe entropy codec must be able to stop before having produced or consumed all 649*dfc6aa5cSAndroid Build Coastguard Workerthe data that they normally would handle in one call. That part is reasonably 650*dfc6aa5cSAndroid Build Coastguard Workerstraightforward: we make the controller call interfaces include "progress 651*dfc6aa5cSAndroid Build Coastguard Workercounters" which indicate the number of data chunks successfully processed, and 652*dfc6aa5cSAndroid Build Coastguard Workerwe require callers to test the counter rather than just assume all of the data 653*dfc6aa5cSAndroid Build Coastguard Workerwas processed. 654*dfc6aa5cSAndroid Build Coastguard Worker 655*dfc6aa5cSAndroid Build Coastguard WorkerRather than trying to restart at an arbitrary point, the current Huffman 656*dfc6aa5cSAndroid Build Coastguard Workercodecs are designed to restart at the beginning of the current MCU after a 657*dfc6aa5cSAndroid Build Coastguard Workersuspension due to buffer overflow/underrun. At the start of each call, the 658*dfc6aa5cSAndroid Build Coastguard Workercodec's internal state is loaded from permanent storage (in the JPEG object 659*dfc6aa5cSAndroid Build Coastguard Workerstructures) into local variables. On successful completion of the MCU, the 660*dfc6aa5cSAndroid Build Coastguard Workerpermanent state is updated. (This copying is not very expensive, and may even 661*dfc6aa5cSAndroid Build Coastguard Workerlead to *improved* performance if the local variables can be registerized.) 662*dfc6aa5cSAndroid Build Coastguard WorkerIf a suspension occurs, the codec simply returns without updating the state, 663*dfc6aa5cSAndroid Build Coastguard Workerthus effectively reverting to the start of the MCU. Note that this implies 664*dfc6aa5cSAndroid Build Coastguard Workerleaving some data unprocessed in the source/destination buffer (ie, the 665*dfc6aa5cSAndroid Build Coastguard Workercompressed partial MCU). The data source/destination module interfaces are 666*dfc6aa5cSAndroid Build Coastguard Workerspecified so as to make this possible. This also implies that the data buffer 667*dfc6aa5cSAndroid Build Coastguard Workermust be large enough to hold a worst-case compressed MCU; a couple thousand 668*dfc6aa5cSAndroid Build Coastguard Workerbytes should be enough. 669*dfc6aa5cSAndroid Build Coastguard Worker 670*dfc6aa5cSAndroid Build Coastguard WorkerIn a successive-approximation AC refinement scan, the progressive Huffman 671*dfc6aa5cSAndroid Build Coastguard Workerdecoder has to be able to undo assignments of newly nonzero coefficients if it 672*dfc6aa5cSAndroid Build Coastguard Workersuspends before the MCU is complete, since decoding requires distinguishing 673*dfc6aa5cSAndroid Build Coastguard Workerpreviously-zero and previously-nonzero coefficients. This is a bit tedious 674*dfc6aa5cSAndroid Build Coastguard Workerbut probably won't have much effect on performance. Other variants of Huffman 675*dfc6aa5cSAndroid Build Coastguard Workerdecoding need not worry about this, since they will just store the same values 676*dfc6aa5cSAndroid Build Coastguard Workeragain if forced to repeat the MCU. 677*dfc6aa5cSAndroid Build Coastguard Worker 678*dfc6aa5cSAndroid Build Coastguard WorkerThis approach would probably not work for an arithmetic codec, since its 679*dfc6aa5cSAndroid Build Coastguard Workermodifiable state is quite large and couldn't be copied cheaply. Instead it 680*dfc6aa5cSAndroid Build Coastguard Workerwould have to suspend and resume exactly at the point of the buffer end. 681*dfc6aa5cSAndroid Build Coastguard Worker 682*dfc6aa5cSAndroid Build Coastguard WorkerThe JPEG marker reader is designed to cope with suspension at an arbitrary 683*dfc6aa5cSAndroid Build Coastguard Workerpoint. It does so by backing up to the start of the marker parameter segment, 684*dfc6aa5cSAndroid Build Coastguard Workerso the data buffer must be big enough to hold the largest marker of interest. 685*dfc6aa5cSAndroid Build Coastguard WorkerAgain, a couple KB should be adequate. (A special "skip" convention is used 686*dfc6aa5cSAndroid Build Coastguard Workerto bypass COM and APPn markers, so these can be larger than the buffer size 687*dfc6aa5cSAndroid Build Coastguard Workerwithout causing problems; otherwise a 64K buffer would be needed in the worst 688*dfc6aa5cSAndroid Build Coastguard Workercase.) 689*dfc6aa5cSAndroid Build Coastguard Worker 690*dfc6aa5cSAndroid Build Coastguard WorkerThe JPEG marker writer currently does *not* cope with suspension. 691*dfc6aa5cSAndroid Build Coastguard WorkerWe feel that this is not necessary; it is much easier simply to require 692*dfc6aa5cSAndroid Build Coastguard Workerthe application to ensure there is enough buffer space before starting. (An 693*dfc6aa5cSAndroid Build Coastguard Workerempty 2K buffer is more than sufficient for the header markers; and ensuring 694*dfc6aa5cSAndroid Build Coastguard Workerthere are a dozen or two bytes available before calling jpeg_finish_compress() 695*dfc6aa5cSAndroid Build Coastguard Workerwill suffice for the trailer.) This would not work for writing multi-scan 696*dfc6aa5cSAndroid Build Coastguard WorkerJPEG files, but we simply do not intend to support that capability with 697*dfc6aa5cSAndroid Build Coastguard Workersuspension. 698*dfc6aa5cSAndroid Build Coastguard Worker 699*dfc6aa5cSAndroid Build Coastguard Worker 700*dfc6aa5cSAndroid Build Coastguard Worker*** Memory manager services *** 701*dfc6aa5cSAndroid Build Coastguard Worker 702*dfc6aa5cSAndroid Build Coastguard WorkerThe JPEG library's memory manager controls allocation and deallocation of 703*dfc6aa5cSAndroid Build Coastguard Workermemory, and it manages large "virtual" data arrays on machines where the 704*dfc6aa5cSAndroid Build Coastguard Workeroperating system does not provide virtual memory. Note that the same 705*dfc6aa5cSAndroid Build Coastguard Workermemory manager serves both compression and decompression operations. 706*dfc6aa5cSAndroid Build Coastguard Worker 707*dfc6aa5cSAndroid Build Coastguard WorkerIn all cases, allocated objects are tied to a particular compression or 708*dfc6aa5cSAndroid Build Coastguard Workerdecompression master record, and they will be released when that master 709*dfc6aa5cSAndroid Build Coastguard Workerrecord is destroyed. 710*dfc6aa5cSAndroid Build Coastguard Worker 711*dfc6aa5cSAndroid Build Coastguard WorkerThe memory manager does not provide explicit deallocation of objects. 712*dfc6aa5cSAndroid Build Coastguard WorkerInstead, objects are created in "pools" of free storage, and a whole pool 713*dfc6aa5cSAndroid Build Coastguard Workercan be freed at once. This approach helps prevent storage-leak bugs, and 714*dfc6aa5cSAndroid Build Coastguard Workerit speeds up operations whenever malloc/free are slow (as they often are). 715*dfc6aa5cSAndroid Build Coastguard WorkerThe pools can be regarded as lifetime identifiers for objects. Two 716*dfc6aa5cSAndroid Build Coastguard Workerpools/lifetimes are defined: 717*dfc6aa5cSAndroid Build Coastguard Worker * JPOOL_PERMANENT lasts until master record is destroyed 718*dfc6aa5cSAndroid Build Coastguard Worker * JPOOL_IMAGE lasts until done with image (JPEG datastream) 719*dfc6aa5cSAndroid Build Coastguard WorkerPermanent lifetime is used for parameters and tables that should be carried 720*dfc6aa5cSAndroid Build Coastguard Workeracross from one datastream to another; this includes all application-visible 721*dfc6aa5cSAndroid Build Coastguard Workerparameters. Image lifetime is used for everything else. (A third lifetime, 722*dfc6aa5cSAndroid Build Coastguard WorkerJPOOL_PASS = one processing pass, was originally planned. However it was 723*dfc6aa5cSAndroid Build Coastguard Workerdropped as not being worthwhile. The actual usage patterns are such that the 724*dfc6aa5cSAndroid Build Coastguard Workerpeak memory usage would be about the same anyway; and having per-pass storage 725*dfc6aa5cSAndroid Build Coastguard Workersubstantially complicates the virtual memory allocation rules --- see below.) 726*dfc6aa5cSAndroid Build Coastguard Worker 727*dfc6aa5cSAndroid Build Coastguard WorkerThe memory manager deals with three kinds of object: 728*dfc6aa5cSAndroid Build Coastguard Worker1. "Small" objects. Typically these require no more than 10K-20K total. 729*dfc6aa5cSAndroid Build Coastguard Worker2. "Large" objects. These may require tens to hundreds of K depending on 730*dfc6aa5cSAndroid Build Coastguard Worker image size. Semantically they behave the same as small objects, but we 731*dfc6aa5cSAndroid Build Coastguard Worker distinguish them because pool allocation heuristics may differ for large and 732*dfc6aa5cSAndroid Build Coastguard Worker small objects (historically, large objects were also referenced by far 733*dfc6aa5cSAndroid Build Coastguard Worker pointers on MS-DOS machines.) Note that individual "large" objects cannot 734*dfc6aa5cSAndroid Build Coastguard Worker exceed the size allowed by type size_t, which may be 64K or less on some 735*dfc6aa5cSAndroid Build Coastguard Worker machines. 736*dfc6aa5cSAndroid Build Coastguard Worker3. "Virtual" objects. These are large 2-D arrays of JSAMPLEs or JBLOCKs 737*dfc6aa5cSAndroid Build Coastguard Worker (typically large enough for the entire image being processed). The 738*dfc6aa5cSAndroid Build Coastguard Worker memory manager provides stripwise access to these arrays. On machines 739*dfc6aa5cSAndroid Build Coastguard Worker without virtual memory, the rest of the array may be swapped out to a 740*dfc6aa5cSAndroid Build Coastguard Worker temporary file. 741*dfc6aa5cSAndroid Build Coastguard Worker 742*dfc6aa5cSAndroid Build Coastguard Worker(Note: JSAMPARRAY and JBLOCKARRAY data structures are a combination of large 743*dfc6aa5cSAndroid Build Coastguard Workerobjects for the data proper and small objects for the row pointers. For 744*dfc6aa5cSAndroid Build Coastguard Workerconvenience and speed, the memory manager provides single routines to create 745*dfc6aa5cSAndroid Build Coastguard Workerthese structures. Similarly, virtual arrays include a small control block 746*dfc6aa5cSAndroid Build Coastguard Workerand a JSAMPARRAY or JBLOCKARRAY working buffer, all created with one call.) 747*dfc6aa5cSAndroid Build Coastguard Worker 748*dfc6aa5cSAndroid Build Coastguard WorkerIn the present implementation, virtual arrays are only permitted to have image 749*dfc6aa5cSAndroid Build Coastguard Workerlifespan. (Permanent lifespan would not be reasonable, and pass lifespan is 750*dfc6aa5cSAndroid Build Coastguard Workernot very useful since a virtual array's raison d'etre is to store data for 751*dfc6aa5cSAndroid Build Coastguard Workermultiple passes through the image.) We also expect that only "small" objects 752*dfc6aa5cSAndroid Build Coastguard Workerwill be given permanent lifespan, though this restriction is not required by 753*dfc6aa5cSAndroid Build Coastguard Workerthe memory manager. 754*dfc6aa5cSAndroid Build Coastguard Worker 755*dfc6aa5cSAndroid Build Coastguard WorkerIn a non-virtual-memory machine, some performance benefit can be gained by 756*dfc6aa5cSAndroid Build Coastguard Workermaking the in-memory buffers for virtual arrays be as large as possible. 757*dfc6aa5cSAndroid Build Coastguard Worker(For small images, the buffers might fit entirely in memory, so blind 758*dfc6aa5cSAndroid Build Coastguard Workerswapping would be very wasteful.) The memory manager will adjust the height 759*dfc6aa5cSAndroid Build Coastguard Workerof the buffers to fit within a prespecified maximum memory usage. In order 760*dfc6aa5cSAndroid Build Coastguard Workerto do this in a reasonably optimal fashion, the manager needs to allocate all 761*dfc6aa5cSAndroid Build Coastguard Workerof the virtual arrays at once. Therefore, there isn't a one-step allocation 762*dfc6aa5cSAndroid Build Coastguard Workerroutine for virtual arrays; instead, there is a "request" routine that simply 763*dfc6aa5cSAndroid Build Coastguard Workerallocates the control block, and a "realize" routine (called just once) that 764*dfc6aa5cSAndroid Build Coastguard Workerdetermines space allocation and creates all of the actual buffers. The 765*dfc6aa5cSAndroid Build Coastguard Workerrealize routine must allow for space occupied by non-virtual large objects. 766*dfc6aa5cSAndroid Build Coastguard Worker(We don't bother to factor in the space needed for small objects, on the 767*dfc6aa5cSAndroid Build Coastguard Workergrounds that it isn't worth the trouble.) 768*dfc6aa5cSAndroid Build Coastguard Worker 769*dfc6aa5cSAndroid Build Coastguard WorkerTo support all this, we establish the following protocol for doing business 770*dfc6aa5cSAndroid Build Coastguard Workerwith the memory manager: 771*dfc6aa5cSAndroid Build Coastguard Worker 1. Modules must request virtual arrays (which may have only image lifespan) 772*dfc6aa5cSAndroid Build Coastguard Worker during the initial setup phase, i.e., in their jinit_xxx routines. 773*dfc6aa5cSAndroid Build Coastguard Worker 2. All "large" objects (including JSAMPARRAYs and JBLOCKARRAYs) must also be 774*dfc6aa5cSAndroid Build Coastguard Worker allocated during initial setup. 775*dfc6aa5cSAndroid Build Coastguard Worker 3. realize_virt_arrays will be called at the completion of initial setup. 776*dfc6aa5cSAndroid Build Coastguard Worker The above conventions ensure that sufficient information is available 777*dfc6aa5cSAndroid Build Coastguard Worker for it to choose a good size for virtual array buffers. 778*dfc6aa5cSAndroid Build Coastguard WorkerSmall objects of any lifespan may be allocated at any time. We expect that 779*dfc6aa5cSAndroid Build Coastguard Workerthe total space used for small objects will be small enough to be negligible 780*dfc6aa5cSAndroid Build Coastguard Workerin the realize_virt_arrays computation. 781*dfc6aa5cSAndroid Build Coastguard Worker 782*dfc6aa5cSAndroid Build Coastguard WorkerIn a virtual-memory machine, we simply pretend that the available space is 783*dfc6aa5cSAndroid Build Coastguard Workerinfinite, thus causing realize_virt_arrays to decide that it can allocate all 784*dfc6aa5cSAndroid Build Coastguard Workerthe virtual arrays as full-size in-memory buffers. The overhead of the 785*dfc6aa5cSAndroid Build Coastguard Workervirtual-array access protocol is very small when no swapping occurs. 786*dfc6aa5cSAndroid Build Coastguard Worker 787*dfc6aa5cSAndroid Build Coastguard WorkerA virtual array can be specified to be "pre-zeroed"; when this flag is set, 788*dfc6aa5cSAndroid Build Coastguard Workernever-yet-written sections of the array are set to zero before being made 789*dfc6aa5cSAndroid Build Coastguard Workeravailable to the caller. If this flag is not set, never-written sections 790*dfc6aa5cSAndroid Build Coastguard Workerof the array contain garbage. (This feature exists primarily because the 791*dfc6aa5cSAndroid Build Coastguard Workerequivalent logic would otherwise be needed in jdcoefct.c for progressive 792*dfc6aa5cSAndroid Build Coastguard WorkerJPEG mode; we may as well make it available for possible other uses.) 793*dfc6aa5cSAndroid Build Coastguard Worker 794*dfc6aa5cSAndroid Build Coastguard WorkerThe first write pass on a virtual array is required to occur in top-to-bottom 795*dfc6aa5cSAndroid Build Coastguard Workerorder; read passes, as well as any write passes after the first one, may 796*dfc6aa5cSAndroid Build Coastguard Workeraccess the array in any order. This restriction exists partly to simplify 797*dfc6aa5cSAndroid Build Coastguard Workerthe virtual array control logic, and partly because some file systems may not 798*dfc6aa5cSAndroid Build Coastguard Workersupport seeking beyond the current end-of-file in a temporary file. The main 799*dfc6aa5cSAndroid Build Coastguard Workerimplication of this restriction is that rearrangement of rows (such as 800*dfc6aa5cSAndroid Build Coastguard Workerconverting top-to-bottom data order to bottom-to-top) must be handled while 801*dfc6aa5cSAndroid Build Coastguard Workerreading data out of the virtual array, not while putting it in. 802*dfc6aa5cSAndroid Build Coastguard Worker 803*dfc6aa5cSAndroid Build Coastguard Worker 804*dfc6aa5cSAndroid Build Coastguard Worker*** Memory manager internal structure *** 805*dfc6aa5cSAndroid Build Coastguard Worker 806*dfc6aa5cSAndroid Build Coastguard WorkerTo isolate system dependencies as much as possible, we have broken the 807*dfc6aa5cSAndroid Build Coastguard Workermemory manager into two parts. There is a reasonably system-independent 808*dfc6aa5cSAndroid Build Coastguard Worker"front end" (jmemmgr.c) and a "back end" that contains only the code 809*dfc6aa5cSAndroid Build Coastguard Workerlikely to change across systems. All of the memory management methods 810*dfc6aa5cSAndroid Build Coastguard Workeroutlined above are implemented by the front end. The back end provides 811*dfc6aa5cSAndroid Build Coastguard Workerthe following routines for use by the front end (none of these routines 812*dfc6aa5cSAndroid Build Coastguard Workerare known to the rest of the JPEG code): 813*dfc6aa5cSAndroid Build Coastguard Worker 814*dfc6aa5cSAndroid Build Coastguard Workerjpeg_mem_init, jpeg_mem_term system-dependent initialization/shutdown 815*dfc6aa5cSAndroid Build Coastguard Worker 816*dfc6aa5cSAndroid Build Coastguard Workerjpeg_get_small, jpeg_free_small interface to malloc and free library routines 817*dfc6aa5cSAndroid Build Coastguard Worker (or their equivalents) 818*dfc6aa5cSAndroid Build Coastguard Worker 819*dfc6aa5cSAndroid Build Coastguard Workerjpeg_get_large, jpeg_free_large historically was used to interface with 820*dfc6aa5cSAndroid Build Coastguard Worker FAR malloc/free on MS-DOS machines; now the 821*dfc6aa5cSAndroid Build Coastguard Worker same as jpeg_get_small/jpeg_free_small 822*dfc6aa5cSAndroid Build Coastguard Worker 823*dfc6aa5cSAndroid Build Coastguard Workerjpeg_mem_available estimate available memory 824*dfc6aa5cSAndroid Build Coastguard Worker 825*dfc6aa5cSAndroid Build Coastguard Workerjpeg_open_backing_store create a backing-store object 826*dfc6aa5cSAndroid Build Coastguard Worker 827*dfc6aa5cSAndroid Build Coastguard Workerread_backing_store, manipulate a backing-store object 828*dfc6aa5cSAndroid Build Coastguard Workerwrite_backing_store, 829*dfc6aa5cSAndroid Build Coastguard Workerclose_backing_store 830*dfc6aa5cSAndroid Build Coastguard Worker 831*dfc6aa5cSAndroid Build Coastguard WorkerOn some systems there will be more than one type of backing-store object. 832*dfc6aa5cSAndroid Build Coastguard Workerjpeg_open_backing_store is responsible for choosing how to implement a given 833*dfc6aa5cSAndroid Build Coastguard Workerobject. The read/write/close routines are method pointers in the structure 834*dfc6aa5cSAndroid Build Coastguard Workerthat describes a given object; this lets them be different for different object 835*dfc6aa5cSAndroid Build Coastguard Workertypes. 836*dfc6aa5cSAndroid Build Coastguard Worker 837*dfc6aa5cSAndroid Build Coastguard WorkerIt may be necessary to ensure that backing store objects are explicitly 838*dfc6aa5cSAndroid Build Coastguard Workerreleased upon abnormal program termination. To support this, we will expect 839*dfc6aa5cSAndroid Build Coastguard Workerthe main program or surrounding application to arrange to call self_destruct 840*dfc6aa5cSAndroid Build Coastguard Worker(typically via jpeg_destroy) upon abnormal termination. This may require a 841*dfc6aa5cSAndroid Build Coastguard WorkerSIGINT signal handler or equivalent. We don't want to have the back end module 842*dfc6aa5cSAndroid Build Coastguard Workerinstall its own signal handler, because that would pre-empt the surrounding 843*dfc6aa5cSAndroid Build Coastguard Workerapplication's ability to control signal handling. 844*dfc6aa5cSAndroid Build Coastguard Worker 845*dfc6aa5cSAndroid Build Coastguard WorkerThe IJG distribution includes several memory manager back end implementations. 846*dfc6aa5cSAndroid Build Coastguard WorkerUsually the same back end should be suitable for all applications on a given 847*dfc6aa5cSAndroid Build Coastguard Workersystem, but it is possible for an application to supply its own back end at 848*dfc6aa5cSAndroid Build Coastguard Workerneed. 849*dfc6aa5cSAndroid Build Coastguard Worker 850*dfc6aa5cSAndroid Build Coastguard Worker 851*dfc6aa5cSAndroid Build Coastguard Worker*** Implications of DNL marker *** 852*dfc6aa5cSAndroid Build Coastguard Worker 853*dfc6aa5cSAndroid Build Coastguard WorkerSome JPEG files may use a DNL marker to postpone definition of the image 854*dfc6aa5cSAndroid Build Coastguard Workerheight (this would be useful for a fax-like scanner's output, for instance). 855*dfc6aa5cSAndroid Build Coastguard WorkerIn these files the SOF marker claims the image height is 0, and you only 856*dfc6aa5cSAndroid Build Coastguard Workerfind out the true image height at the end of the first scan. 857*dfc6aa5cSAndroid Build Coastguard Worker 858*dfc6aa5cSAndroid Build Coastguard WorkerWe could read these files as follows: 859*dfc6aa5cSAndroid Build Coastguard Worker1. Upon seeing zero image height, replace it by 65535 (the maximum allowed). 860*dfc6aa5cSAndroid Build Coastguard Worker2. When the DNL is found, update the image height in the global image 861*dfc6aa5cSAndroid Build Coastguard Worker descriptor. 862*dfc6aa5cSAndroid Build Coastguard WorkerThis implies that control modules must avoid making copies of the image 863*dfc6aa5cSAndroid Build Coastguard Workerheight, and must re-test for termination after each MCU row. This would 864*dfc6aa5cSAndroid Build Coastguard Workerbe easy enough to do. 865*dfc6aa5cSAndroid Build Coastguard Worker 866*dfc6aa5cSAndroid Build Coastguard WorkerIn cases where image-size data structures are allocated, this approach will 867*dfc6aa5cSAndroid Build Coastguard Workerresult in very inefficient use of virtual memory or much-larger-than-necessary 868*dfc6aa5cSAndroid Build Coastguard Workertemporary files. This seems acceptable for something that probably won't be a 869*dfc6aa5cSAndroid Build Coastguard Workermainstream usage. People might have to forgo use of memory-hogging options 870*dfc6aa5cSAndroid Build Coastguard Worker(such as two-pass color quantization or noninterleaved JPEG files) if they 871*dfc6aa5cSAndroid Build Coastguard Workerwant efficient conversion of such files. (One could improve efficiency by 872*dfc6aa5cSAndroid Build Coastguard Workerdemanding a user-supplied upper bound for the height, less than 65536; in most 873*dfc6aa5cSAndroid Build Coastguard Workercases it could be much less.) 874*dfc6aa5cSAndroid Build Coastguard Worker 875*dfc6aa5cSAndroid Build Coastguard WorkerThe standard also permits the SOF marker to overestimate the image height, 876*dfc6aa5cSAndroid Build Coastguard Workerwith a DNL to give the true, smaller height at the end of the first scan. 877*dfc6aa5cSAndroid Build Coastguard WorkerThis would solve the space problems if the overestimate wasn't too great. 878*dfc6aa5cSAndroid Build Coastguard WorkerHowever, it implies that you don't even know whether DNL will be used. 879*dfc6aa5cSAndroid Build Coastguard Worker 880*dfc6aa5cSAndroid Build Coastguard WorkerThis leads to a couple of very serious objections: 881*dfc6aa5cSAndroid Build Coastguard Worker1. Testing for a DNL marker must occur in the inner loop of the decompressor's 882*dfc6aa5cSAndroid Build Coastguard Worker Huffman decoder; this implies a speed penalty whether the feature is used 883*dfc6aa5cSAndroid Build Coastguard Worker or not. 884*dfc6aa5cSAndroid Build Coastguard Worker2. There is no way to hide the last-minute change in image height from an 885*dfc6aa5cSAndroid Build Coastguard Worker application using the decoder. Thus *every* application using the IJG 886*dfc6aa5cSAndroid Build Coastguard Worker library would suffer a complexity penalty whether it cared about DNL or 887*dfc6aa5cSAndroid Build Coastguard Worker not. 888*dfc6aa5cSAndroid Build Coastguard WorkerWe currently do not support DNL because of these problems. 889*dfc6aa5cSAndroid Build Coastguard Worker 890*dfc6aa5cSAndroid Build Coastguard WorkerA different approach is to insist that DNL-using files be preprocessed by a 891*dfc6aa5cSAndroid Build Coastguard Workerseparate program that reads ahead to the DNL, then goes back and fixes the SOF 892*dfc6aa5cSAndroid Build Coastguard Workermarker. This is a much simpler solution and is probably far more efficient. 893*dfc6aa5cSAndroid Build Coastguard WorkerEven if one wants piped input, buffering the first scan of the JPEG file needs 894*dfc6aa5cSAndroid Build Coastguard Workera lot smaller temp file than is implied by the maximum-height method. For 895*dfc6aa5cSAndroid Build Coastguard Workerthis approach we'd simply treat DNL as a no-op in the decompressor (at most, 896*dfc6aa5cSAndroid Build Coastguard Workercheck that it matches the SOF image height). 897*dfc6aa5cSAndroid Build Coastguard Worker 898*dfc6aa5cSAndroid Build Coastguard WorkerWe will not worry about making the compressor capable of outputting DNL. 899*dfc6aa5cSAndroid Build Coastguard WorkerSomething similar to the first scheme above could be applied if anyone ever 900*dfc6aa5cSAndroid Build Coastguard Workerwants to make that work. 901