Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
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.

1204 lines
38 KiB

3 years ago
  1. unit imjdhuff;
  2. { This file contains declarations for Huffman entropy decoding routines
  3. that are shared between the sequential decoder (jdhuff.c) and the
  4. progressive decoder (jdphuff.c). No other modules need to see these. }
  5. { This file contains Huffman entropy decoding routines.
  6. Much of the complexity here has to do with supporting input suspension.
  7. If the data source module demands suspension, we want to be able to back
  8. up to the start of the current MCU. To do this, we copy state variables
  9. into local working storage, and update them back to the permanent
  10. storage only upon successful completion of an MCU. }
  11. { Original: jdhuff.h+jdhuff.c; Copyright (C) 1991-1997, Thomas G. Lane. }
  12. interface
  13. {$I imjconfig.inc}
  14. uses
  15. imjmorecfg,
  16. imjinclude,
  17. imjdeferr,
  18. imjerror,
  19. imjutils,
  20. imjpeglib;
  21. { Declarations shared with jdphuff.c }
  22. { Derived data constructed for each Huffman table }
  23. const
  24. HUFF_LOOKAHEAD = 8; { # of bits of lookahead }
  25. type
  26. d_derived_tbl_ptr = ^d_derived_tbl;
  27. d_derived_tbl = record
  28. { Basic tables: (element [0] of each array is unused) }
  29. maxcode : array[0..18-1] of INT32; { largest code of length k (-1 if none) }
  30. { (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) }
  31. valoffset : array[0..17-1] of INT32; { huffval[] offset for codes of length k }
  32. { valoffset[k] = huffval[] index of 1st symbol of code length k, less
  33. the smallest code of length k; so given a code of length k, the
  34. corresponding symbol is huffval[code + valoffset[k]] }
  35. { Link to public Huffman table (needed only in jpeg_huff_decode) }
  36. pub : JHUFF_TBL_PTR;
  37. { Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
  38. the input data stream. If the next Huffman code is no more
  39. than HUFF_LOOKAHEAD bits long, we can obtain its length and
  40. the corresponding symbol directly from these tables. }
  41. look_nbits : array[0..(1 shl HUFF_LOOKAHEAD)-1] of int;
  42. { # bits, or 0 if too long }
  43. look_sym : array[0..(1 shl HUFF_LOOKAHEAD)-1] of UINT8;
  44. { symbol, or unused }
  45. end;
  46. { Fetching the next N bits from the input stream is a time-critical operation
  47. for the Huffman decoders. We implement it with a combination of inline
  48. macros and out-of-line subroutines. Note that N (the number of bits
  49. demanded at one time) never exceeds 15 for JPEG use.
  50. We read source bytes into get_buffer and dole out bits as needed.
  51. If get_buffer already contains enough bits, they are fetched in-line
  52. by the macros CHECK_BIT_BUFFER and GET_BITS. When there aren't enough
  53. bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
  54. as full as possible (not just to the number of bits needed; this
  55. prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
  56. Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
  57. On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
  58. at least the requested number of bits --- dummy zeroes are inserted if
  59. necessary. }
  60. type
  61. bit_buf_type = INT32 ; { type of bit-extraction buffer }
  62. const
  63. BIT_BUF_SIZE = 32; { size of buffer in bits }
  64. { If long is > 32 bits on your machine, and shifting/masking longs is
  65. reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
  66. appropriately should be a win. Unfortunately we can't define the size
  67. with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
  68. because not all machines measure sizeof in 8-bit bytes. }
  69. type
  70. bitread_perm_state = record { Bitreading state saved across MCUs }
  71. get_buffer : bit_buf_type; { current bit-extraction buffer }
  72. bits_left : int; { # of unused bits in it }
  73. end;
  74. type
  75. bitread_working_state = record
  76. { Bitreading working state within an MCU }
  77. { current data source location }
  78. { We need a copy, rather than munging the original, in case of suspension }
  79. next_input_byte : JOCTETptr; { => next byte to read from source }
  80. bytes_in_buffer : size_t; { # of bytes remaining in source buffer }
  81. { Bit input buffer --- note these values are kept in register variables,
  82. not in this struct, inside the inner loops. }
  83. get_buffer : bit_buf_type; { current bit-extraction buffer }
  84. bits_left : int; { # of unused bits in it }
  85. { Pointer needed by jpeg_fill_bit_buffer }
  86. cinfo : j_decompress_ptr; { back link to decompress master record }
  87. end;
  88. { Module initialization routine for Huffman entropy decoding. }
  89. {GLOBAL}
  90. procedure jinit_huff_decoder (cinfo : j_decompress_ptr);
  91. {GLOBAL}
  92. function jpeg_huff_decode(var state : bitread_working_state;
  93. get_buffer : bit_buf_type; {register}
  94. bits_left : int; {register}
  95. htbl : d_derived_tbl_ptr;
  96. min_bits : int) : int;
  97. { Compute the derived values for a Huffman table.
  98. Note this is also used by jdphuff.c. }
  99. {GLOBAL}
  100. procedure jpeg_make_d_derived_tbl (cinfo : j_decompress_ptr;
  101. isDC : boolean;
  102. tblno : int;
  103. var pdtbl : d_derived_tbl_ptr);
  104. { Load up the bit buffer to a depth of at least nbits }
  105. function jpeg_fill_bit_buffer (var state : bitread_working_state;
  106. get_buffer : bit_buf_type; {register}
  107. bits_left : int; {register}
  108. nbits : int) : boolean;
  109. implementation
  110. {$IFDEF MACRO}
  111. { Macros to declare and load/save bitread local variables. }
  112. {$define BITREAD_STATE_VARS}
  113. get_buffer : bit_buf_type ; {register}
  114. bits_left : int; {register}
  115. br_state : bitread_working_state;
  116. {$define BITREAD_LOAD_STATE(cinfop,permstate)}
  117. br_state.cinfo := cinfop;
  118. br_state.next_input_byte := cinfop^.src^.next_input_byte;
  119. br_state.bytes_in_buffer := cinfop^.src^.bytes_in_buffer;
  120. get_buffer := permstate.get_buffer;
  121. bits_left := permstate.bits_left;
  122. {$define BITREAD_SAVE_STATE(cinfop,permstate) }
  123. cinfop^.src^.next_input_byte := br_state.next_input_byte;
  124. cinfop^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  125. permstate.get_buffer := get_buffer;
  126. permstate.bits_left := bits_left;
  127. { These macros provide the in-line portion of bit fetching.
  128. Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
  129. before using GET_BITS, PEEK_BITS, or DROP_BITS.
  130. The variables get_buffer and bits_left are assumed to be locals,
  131. but the state struct might not be (jpeg_huff_decode needs this).
  132. CHECK_BIT_BUFFER(state,n,action);
  133. Ensure there are N bits in get_buffer; if suspend, take action.
  134. val = GET_BITS(n);
  135. Fetch next N bits.
  136. val = PEEK_BITS(n);
  137. Fetch next N bits without removing them from the buffer.
  138. DROP_BITS(n);
  139. Discard next N bits.
  140. The value N should be a simple variable, not an expression, because it
  141. is evaluated multiple times. }
  142. {$define CHECK_BIT_BUFFER(state,nbits,action)}
  143. if (bits_left < (nbits)) then
  144. begin
  145. if (not jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits)) then
  146. begin
  147. action;
  148. exit;
  149. end;
  150. get_buffer := state.get_buffer;
  151. bits_left := state.bits_left;
  152. end;
  153. {$define GET_BITS(nbits)}
  154. Dec(bits_left, (nbits));
  155. ( (int(get_buffer shr bits_left)) and ( pred(1 shl (nbits)) ) )
  156. {$define PEEK_BITS(nbits)}
  157. int(get_buffer shr (bits_left - (nbits))) and pred(1 shl (nbits))
  158. {$define DROP_BITS(nbits)}
  159. Dec(bits_left, nbits);
  160. { Code for extracting next Huffman-coded symbol from input bit stream.
  161. Again, this is time-critical and we make the main paths be macros.
  162. We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
  163. without looping. Usually, more than 95% of the Huffman codes will be 8
  164. or fewer bits long. The few overlength codes are handled with a loop,
  165. which need not be inline code.
  166. Notes about the HUFF_DECODE macro:
  167. 1. Near the end of the data segment, we may fail to get enough bits
  168. for a lookahead. In that case, we do it the hard way.
  169. 2. If the lookahead table contains no entry, the next code must be
  170. more than HUFF_LOOKAHEAD bits long.
  171. 3. jpeg_huff_decode returns -1 if forced to suspend. }
  172. macro HUFF_DECODE(s,br_state,htbl,return FALSE,slowlabel);
  173. label showlabel;
  174. var
  175. nb, look : int; {register}
  176. begin
  177. if (bits_left < HUFF_LOOKAHEAD) then
  178. begin
  179. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
  180. begin
  181. decode_mcu := FALSE;
  182. exit;
  183. end;
  184. get_buffer := br_state.get_buffer;
  185. bits_left := br_state.bits_left;
  186. if (bits_left < HUFF_LOOKAHEAD) then
  187. begin
  188. nb := 1;
  189. goto slowlabel;
  190. end;
  191. end;
  192. {look := PEEK_BITS(HUFF_LOOKAHEAD);}
  193. look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
  194. pred(1 shl HUFF_LOOKAHEAD);
  195. nb := htbl^.look_nbits[look];
  196. if (nb <> 0) then
  197. begin
  198. {DROP_BITS(nb);}
  199. Dec(bits_left, nb);
  200. s := htbl^.look_sym[look];
  201. end
  202. else
  203. begin
  204. nb := HUFF_LOOKAHEAD+1;
  205. slowlabel:
  206. s := jpeg_huff_decode(br_state,get_buffer,bits_left,htbl,nb));
  207. if (s < 0) then
  208. begin
  209. result := FALSE;
  210. exit;
  211. end;
  212. get_buffer := br_state.get_buffer;
  213. bits_left := br_state.bits_left;
  214. end;
  215. end;
  216. {$ENDIF} {MACRO}
  217. { Expanded entropy decoder object for Huffman decoding.
  218. The savable_state subrecord contains fields that change within an MCU,
  219. but must not be updated permanently until we complete the MCU. }
  220. type
  221. savable_state = record
  222. last_dc_val : array[0..MAX_COMPS_IN_SCAN-1] of int; { last DC coef for each component }
  223. end;
  224. type
  225. huff_entropy_ptr = ^huff_entropy_decoder;
  226. huff_entropy_decoder = record
  227. pub : jpeg_entropy_decoder; { public fields }
  228. { These fields are loaded into local variables at start of each MCU.
  229. In case of suspension, we exit WITHOUT updating them. }
  230. bitstate : bitread_perm_state; { Bit buffer at start of MCU }
  231. saved : savable_state; { Other state at start of MCU }
  232. { These fields are NOT loaded into local working state. }
  233. restarts_to_go : uInt; { MCUs left in this restart interval }
  234. { Pointers to derived tables (these workspaces have image lifespan) }
  235. dc_derived_tbls : array[0..NUM_HUFF_TBLS] of d_derived_tbl_ptr;
  236. ac_derived_tbls : array[0..NUM_HUFF_TBLS] of d_derived_tbl_ptr;
  237. { Precalculated info set up by start_pass for use in decode_mcu: }
  238. { Pointers to derived tables to be used for each block within an MCU }
  239. dc_cur_tbls : array[0..D_MAX_BLOCKS_IN_MCU-1] of d_derived_tbl_ptr;
  240. ac_cur_tbls : array[0..D_MAX_BLOCKS_IN_MCU-1] of d_derived_tbl_ptr;
  241. { Whether we care about the DC and AC coefficient values for each block }
  242. dc_needed : array[0..D_MAX_BLOCKS_IN_MCU-1] of boolean;
  243. ac_needed : array[0..D_MAX_BLOCKS_IN_MCU-1] of boolean;
  244. end;
  245. { Initialize for a Huffman-compressed scan. }
  246. {METHODDEF}
  247. procedure start_pass_huff_decoder (cinfo : j_decompress_ptr);
  248. var
  249. entropy : huff_entropy_ptr;
  250. ci, blkn, dctbl, actbl : int;
  251. compptr : jpeg_component_info_ptr;
  252. begin
  253. entropy := huff_entropy_ptr (cinfo^.entropy);
  254. { Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
  255. This ought to be an error condition, but we make it a warning because
  256. there are some baseline files out there with all zeroes in these bytes. }
  257. if (cinfo^.Ss <> 0) or (cinfo^.Se <> DCTSIZE2-1) or
  258. (cinfo^.Ah <> 0) or (cinfo^.Al <> 0) then
  259. WARNMS(j_common_ptr(cinfo), JWRN_NOT_SEQUENTIAL);
  260. for ci := 0 to pred(cinfo^.comps_in_scan) do
  261. begin
  262. compptr := cinfo^.cur_comp_info[ci];
  263. dctbl := compptr^.dc_tbl_no;
  264. actbl := compptr^.ac_tbl_no;
  265. { Compute derived values for Huffman tables }
  266. { We may do this more than once for a table, but it's not expensive }
  267. jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
  268. entropy^.dc_derived_tbls[dctbl]);
  269. jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
  270. entropy^.ac_derived_tbls[actbl]);
  271. { Initialize DC predictions to 0 }
  272. entropy^.saved.last_dc_val[ci] := 0;
  273. end;
  274. { Precalculate decoding info for each block in an MCU of this scan }
  275. for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  276. begin
  277. ci := cinfo^.MCU_membership[blkn];
  278. compptr := cinfo^.cur_comp_info[ci];
  279. { Precalculate which table to use for each block }
  280. entropy^.dc_cur_tbls[blkn] := entropy^.dc_derived_tbls[compptr^.dc_tbl_no];
  281. entropy^.ac_cur_tbls[blkn] := entropy^.ac_derived_tbls[compptr^.ac_tbl_no];
  282. { Decide whether we really care about the coefficient values }
  283. if (compptr^.component_needed) then
  284. begin
  285. entropy^.dc_needed[blkn] := TRUE;
  286. { we don't need the ACs if producing a 1/8th-size image }
  287. entropy^.ac_needed[blkn] := (compptr^.DCT_scaled_size > 1);
  288. end
  289. else
  290. begin
  291. entropy^.ac_needed[blkn] := FALSE;
  292. entropy^.dc_needed[blkn] := FALSE;
  293. end;
  294. end;
  295. { Initialize bitread state variables }
  296. entropy^.bitstate.bits_left := 0;
  297. entropy^.bitstate.get_buffer := 0; { unnecessary, but keeps Purify quiet }
  298. entropy^.pub.insufficient_data := FALSE;
  299. { Initialize restart counter }
  300. entropy^.restarts_to_go := cinfo^.restart_interval;
  301. end;
  302. { Compute the derived values for a Huffman table.
  303. This routine also performs some validation checks on the table.
  304. Note this is also used by jdphuff.c. }
  305. {GLOBAL}
  306. procedure jpeg_make_d_derived_tbl (cinfo : j_decompress_ptr;
  307. isDC : boolean;
  308. tblno : int;
  309. var pdtbl : d_derived_tbl_ptr);
  310. var
  311. htbl : JHUFF_TBL_PTR;
  312. dtbl : d_derived_tbl_ptr;
  313. p, i, l, si, numsymbols : int;
  314. lookbits, ctr : int;
  315. huffsize : array[0..257-1] of byte;
  316. huffcode : array[0..257-1] of uInt;
  317. code : uInt;
  318. var
  319. sym : int;
  320. begin
  321. { Note that huffsize[] and huffcode[] are filled in code-length order,
  322. paralleling the order of the symbols themselves in htbl^.huffval[]. }
  323. { Find the input Huffman table }
  324. if (tblno < 0) or (tblno >= NUM_HUFF_TBLS) then
  325. ERREXIT1(j_common_ptr(cinfo), JERR_NO_HUFF_TABLE, tblno);
  326. if isDC then
  327. htbl := cinfo^.dc_huff_tbl_ptrs[tblno]
  328. else
  329. htbl := cinfo^.ac_huff_tbl_ptrs[tblno];
  330. if (htbl = NIL) then
  331. ERREXIT1(j_common_ptr(cinfo), JERR_NO_HUFF_TABLE, tblno);
  332. { Allocate a workspace if we haven't already done so. }
  333. if (pdtbl = NIL) then
  334. pdtbl := d_derived_tbl_ptr(
  335. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  336. SIZEOF(d_derived_tbl)) );
  337. dtbl := pdtbl;
  338. dtbl^.pub := htbl; { fill in back link }
  339. { Figure C.1: make table of Huffman code length for each symbol }
  340. p := 0;
  341. for l := 1 to 16 do
  342. begin
  343. i := int(htbl^.bits[l]);
  344. if (i < 0) or (p + i > 256) then { protect against table overrun }
  345. ERREXIT(j_common_ptr(cinfo), JERR_BAD_HUFF_TABLE);
  346. while (i > 0) do
  347. begin
  348. huffsize[p] := byte(l);
  349. Inc(p);
  350. Dec(i);
  351. end;
  352. end;
  353. huffsize[p] := 0;
  354. numsymbols := p;
  355. { Figure C.2: generate the codes themselves }
  356. { We also validate that the counts represent a legal Huffman code tree. }
  357. code := 0;
  358. si := huffsize[0];
  359. p := 0;
  360. while (huffsize[p] <> 0) do
  361. begin
  362. while (( int (huffsize[p]) ) = si) do
  363. begin
  364. huffcode[p] := code;
  365. Inc(p);
  366. Inc(code);
  367. end;
  368. { code is now 1 more than the last code used for codelength si; but
  369. it must still fit in si bits, since no code is allowed to be all ones. }
  370. if (INT32(code) >= (INT32(1) shl si)) then
  371. ERREXIT(j_common_ptr(cinfo), JERR_BAD_HUFF_TABLE);
  372. code := code shl 1;
  373. Inc(si);
  374. end;
  375. { Figure F.15: generate decoding tables for bit-sequential decoding }
  376. p := 0;
  377. for l := 1 to 16 do
  378. begin
  379. if (htbl^.bits[l] <> 0) then
  380. begin
  381. { valoffset[l] = huffval[] index of 1st symbol of code length l,
  382. minus the minimum code of length l }
  383. dtbl^.valoffset[l] := INT32(p) - INT32(huffcode[p]);
  384. Inc(p, htbl^.bits[l]);
  385. dtbl^.maxcode[l] := huffcode[p-1]; { maximum code of length l }
  386. end
  387. else
  388. begin
  389. dtbl^.maxcode[l] := -1; { -1 if no codes of this length }
  390. end;
  391. end;
  392. dtbl^.maxcode[17] := long($FFFFF); { ensures jpeg_huff_decode terminates }
  393. { Compute lookahead tables to speed up decoding.
  394. First we set all the table entries to 0, indicating "too long";
  395. then we iterate through the Huffman codes that are short enough and
  396. fill in all the entries that correspond to bit sequences starting
  397. with that code. }
  398. MEMZERO(@dtbl^.look_nbits, SIZEOF(dtbl^.look_nbits));
  399. p := 0;
  400. for l := 1 to HUFF_LOOKAHEAD do
  401. begin
  402. for i := 1 to int (htbl^.bits[l]) do
  403. begin
  404. { l := current code's length, p := its index in huffcode[] & huffval[]. }
  405. { Generate left-justified code followed by all possible bit sequences }
  406. lookbits := huffcode[p] shl (HUFF_LOOKAHEAD-l);
  407. for ctr := pred(1 shl (HUFF_LOOKAHEAD-l)) downto 0 do
  408. begin
  409. dtbl^.look_nbits[lookbits] := l;
  410. dtbl^.look_sym[lookbits] := htbl^.huffval[p];
  411. Inc(lookbits);
  412. end;
  413. Inc(p);
  414. end;
  415. end;
  416. { Validate symbols as being reasonable.
  417. For AC tables, we make no check, but accept all byte values 0..255.
  418. For DC tables, we require the symbols to be in range 0..15.
  419. (Tighter bounds could be applied depending on the data depth and mode,
  420. but this is sufficient to ensure safe decoding.) }
  421. if (isDC) then
  422. begin
  423. for i := 0 to pred(numsymbols) do
  424. begin
  425. sym := htbl^.huffval[i];
  426. if (sym < 0) or (sym > 15) then
  427. ERREXIT(j_common_ptr(cinfo), JERR_BAD_HUFF_TABLE);
  428. end;
  429. end;
  430. end;
  431. { Out-of-line code for bit fetching (shared with jdphuff.c).
  432. See jdhuff.h for info about usage.
  433. Note: current values of get_buffer and bits_left are passed as parameters,
  434. but are returned in the corresponding fields of the state struct.
  435. On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
  436. of get_buffer to be used. (On machines with wider words, an even larger
  437. buffer could be used.) However, on some machines 32-bit shifts are
  438. quite slow and take time proportional to the number of places shifted.
  439. (This is true with most PC compilers, for instance.) In this case it may
  440. be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
  441. average shift distance at the cost of more calls to jpeg_fill_bit_buffer. }
  442. {$ifdef SLOW_SHIFT_32}
  443. const
  444. MIN_GET_BITS = 15; { minimum allowable value }
  445. {$else}
  446. const
  447. MIN_GET_BITS = (BIT_BUF_SIZE-7);
  448. {$endif}
  449. {GLOBAL}
  450. function jpeg_fill_bit_buffer (var state : bitread_working_state;
  451. {register} get_buffer : bit_buf_type;
  452. {register} bits_left : int;
  453. nbits : int) : boolean;
  454. label
  455. no_more_bytes;
  456. { Load up the bit buffer to a depth of at least nbits }
  457. var
  458. { Copy heavily used state fields into locals (hopefully registers) }
  459. {register} next_input_byte : {const} JOCTETptr;
  460. {register} bytes_in_buffer : size_t;
  461. var
  462. {register} c : int;
  463. var
  464. cinfo : j_decompress_ptr;
  465. begin
  466. next_input_byte := state.next_input_byte;
  467. bytes_in_buffer := state.bytes_in_buffer;
  468. cinfo := state.cinfo;
  469. { Attempt to load at least MIN_GET_BITS bits into get_buffer. }
  470. { (It is assumed that no request will be for more than that many bits.) }
  471. { We fail to do so only if we hit a marker or are forced to suspend. }
  472. if (cinfo^.unread_marker = 0) then { cannot advance past a marker }
  473. begin
  474. while (bits_left < MIN_GET_BITS) do
  475. begin
  476. { Attempt to read a byte }
  477. if (bytes_in_buffer = 0) then
  478. begin
  479. if not cinfo^.src^.fill_input_buffer(cinfo) then
  480. begin
  481. jpeg_fill_bit_buffer := FALSE;
  482. exit;
  483. end;
  484. next_input_byte := cinfo^.src^.next_input_byte;
  485. bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  486. end;
  487. Dec(bytes_in_buffer);
  488. c := GETJOCTET(next_input_byte^);
  489. Inc(next_input_byte);
  490. { If it's $FF, check and discard stuffed zero byte }
  491. if (c = $FF) then
  492. begin
  493. { Loop here to discard any padding FF's on terminating marker,
  494. so that we can save a valid unread_marker value. NOTE: we will
  495. accept multiple FF's followed by a 0 as meaning a single FF data
  496. byte. This data pattern is not valid according to the standard. }
  497. repeat
  498. if (bytes_in_buffer = 0) then
  499. begin
  500. if (not state.cinfo^.src^.fill_input_buffer (state.cinfo)) then
  501. begin
  502. jpeg_fill_bit_buffer := FALSE;
  503. exit;
  504. end;
  505. next_input_byte := state.cinfo^.src^.next_input_byte;
  506. bytes_in_buffer := state.cinfo^.src^.bytes_in_buffer;
  507. end;
  508. Dec(bytes_in_buffer);
  509. c := GETJOCTET(next_input_byte^);
  510. Inc(next_input_byte);
  511. Until (c <> $FF);
  512. if (c = 0) then
  513. begin
  514. { Found FF/00, which represents an FF data byte }
  515. c := $FF;
  516. end
  517. else
  518. begin
  519. { Oops, it's actually a marker indicating end of compressed data.
  520. Save the marker code for later use.
  521. Fine point: it might appear that we should save the marker into
  522. bitread working state, not straight into permanent state. But
  523. once we have hit a marker, we cannot need to suspend within the
  524. current MCU, because we will read no more bytes from the data
  525. source. So it is OK to update permanent state right away. }
  526. cinfo^.unread_marker := c;
  527. { See if we need to insert some fake zero bits. }
  528. goto no_more_bytes;
  529. end;
  530. end;
  531. { OK, load c into get_buffer }
  532. get_buffer := (get_buffer shl 8) or c;
  533. Inc(bits_left, 8);
  534. end { end while }
  535. end
  536. else
  537. begin
  538. no_more_bytes:
  539. { We get here if we've read the marker that terminates the compressed
  540. data segment. There should be enough bits in the buffer register
  541. to satisfy the request; if so, no problem. }
  542. if (nbits > bits_left) then
  543. begin
  544. { Uh-oh. Report corrupted data to user and stuff zeroes into
  545. the data stream, so that we can produce some kind of image.
  546. We use a nonvolatile flag to ensure that only one warning message
  547. appears per data segment. }
  548. if not cinfo^.entropy^.insufficient_data then
  549. begin
  550. WARNMS(j_common_ptr(cinfo), JWRN_HIT_MARKER);
  551. cinfo^.entropy^.insufficient_data := TRUE;
  552. end;
  553. { Fill the buffer with zero bits }
  554. get_buffer := get_buffer shl (MIN_GET_BITS - bits_left);
  555. bits_left := MIN_GET_BITS;
  556. end;
  557. end;
  558. { Unload the local registers }
  559. state.next_input_byte := next_input_byte;
  560. state.bytes_in_buffer := bytes_in_buffer;
  561. state.get_buffer := get_buffer;
  562. state.bits_left := bits_left;
  563. jpeg_fill_bit_buffer := TRUE;
  564. end;
  565. { Out-of-line code for Huffman code decoding.
  566. See jdhuff.h for info about usage. }
  567. {GLOBAL}
  568. function jpeg_huff_decode (var state : bitread_working_state;
  569. {register} get_buffer : bit_buf_type;
  570. {register} bits_left : int;
  571. htbl : d_derived_tbl_ptr;
  572. min_bits : int) : int;
  573. var
  574. {register} l : int;
  575. {register} code : INT32;
  576. begin
  577. l := min_bits;
  578. { HUFF_DECODE has determined that the code is at least min_bits }
  579. { bits long, so fetch that many bits in one swoop. }
  580. {CHECK_BIT_BUFFER(state, l, return -1);}
  581. if (bits_left < l) then
  582. begin
  583. if (not jpeg_fill_bit_buffer(state, get_buffer, bits_left, l)) then
  584. begin
  585. jpeg_huff_decode := -1;
  586. exit;
  587. end;
  588. get_buffer := state.get_buffer;
  589. bits_left := state.bits_left;
  590. end;
  591. {code := GET_BITS(l);}
  592. Dec(bits_left, l);
  593. code := (int(get_buffer shr bits_left)) and ( pred(1 shl l) );
  594. { Collect the rest of the Huffman code one bit at a time. }
  595. { This is per Figure F.16 in the JPEG spec. }
  596. while (code > htbl^.maxcode[l]) do
  597. begin
  598. code := code shl 1;
  599. {CHECK_BIT_BUFFER(state, 1, return -1);}
  600. if (bits_left < 1) then
  601. begin
  602. if (not jpeg_fill_bit_buffer(state, get_buffer, bits_left, 1)) then
  603. begin
  604. jpeg_huff_decode := -1;
  605. exit;
  606. end;
  607. get_buffer := state.get_buffer;
  608. bits_left := state.bits_left;
  609. end;
  610. {code := code or GET_BITS(1);}
  611. Dec(bits_left);
  612. code := code or ( (int(get_buffer shr bits_left)) and pred(1 shl 1) );
  613. Inc(l);
  614. end;
  615. { Unload the local registers }
  616. state.get_buffer := get_buffer;
  617. state.bits_left := bits_left;
  618. { With garbage input we may reach the sentinel value l := 17. }
  619. if (l > 16) then
  620. begin
  621. WARNMS(j_common_ptr(state.cinfo), JWRN_HUFF_BAD_CODE);
  622. jpeg_huff_decode := 0; { fake a zero as the safest result }
  623. exit;
  624. end;
  625. jpeg_huff_decode := htbl^.pub^.huffval[ int (code + htbl^.valoffset[l]) ];
  626. end;
  627. { Figure F.12: extend sign bit.
  628. On some machines, a shift and add will be faster than a table lookup. }
  629. {$ifdef AVOID_TABLES}
  630. #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
  631. {$else}
  632. {$define HUFF_EXTEND(x,s)
  633. if (x < extend_test[s]) then
  634. := x + extend_offset[s]
  635. else
  636. x;}
  637. const
  638. extend_test : array[0..16-1] of int = { entry n is 2**(n-1) }
  639. ($0000, $0001, $0002, $0004, $0008, $0010, $0020, $0040,
  640. $0080, $0100, $0200, $0400, $0800, $1000, $2000, $4000);
  641. const
  642. extend_offset : array[0..16-1] of int = { entry n is (-1 << n) + 1 }
  643. (0, ((-1) shl 1) + 1, ((-1) shl 2) + 1, ((-1) shl 3) + 1, ((-1) shl 4) + 1,
  644. ((-1) shl 5) + 1, ((-1) shl 6) + 1, ((-1) shl 7) + 1, ((-1) shl 8) + 1,
  645. ((-1) shl 9) + 1, ((-1) shl 10) + 1, ((-1) shl 11) + 1,((-1) shl 12) + 1,
  646. ((-1) shl 13) + 1, ((-1) shl 14) + 1, ((-1) shl 15) + 1);
  647. {$endif} { AVOID_TABLES }
  648. { Check for a restart marker & resynchronize decoder.
  649. Returns FALSE if must suspend. }
  650. {LOCAL}
  651. function process_restart (cinfo : j_decompress_ptr) : boolean;
  652. var
  653. entropy : huff_entropy_ptr;
  654. ci : int;
  655. begin
  656. entropy := huff_entropy_ptr (cinfo^.entropy);
  657. { Throw away any unused bits remaining in bit buffer; }
  658. { include any full bytes in next_marker's count of discarded bytes }
  659. Inc(cinfo^.marker^.discarded_bytes, entropy^.bitstate.bits_left div 8);
  660. entropy^.bitstate.bits_left := 0;
  661. { Advance past the RSTn marker }
  662. if (not cinfo^.marker^.read_restart_marker (cinfo)) then
  663. begin
  664. process_restart := FALSE;
  665. exit;
  666. end;
  667. { Re-initialize DC predictions to 0 }
  668. for ci := 0 to pred(cinfo^.comps_in_scan) do
  669. entropy^.saved.last_dc_val[ci] := 0;
  670. { Reset restart counter }
  671. entropy^.restarts_to_go := cinfo^.restart_interval;
  672. { Reset out-of-data flag, unless read_restart_marker left us smack up
  673. against a marker. In that case we will end up treating the next data
  674. segment as empty, and we can avoid producing bogus output pixels by
  675. leaving the flag set. }
  676. if (cinfo^.unread_marker = 0) then
  677. entropy^.pub.insufficient_data := FALSE;
  678. process_restart := TRUE;
  679. end;
  680. { Decode and return one MCU's worth of Huffman-compressed coefficients.
  681. The coefficients are reordered from zigzag order into natural array order,
  682. but are not dequantized.
  683. The i'th block of the MCU is stored into the block pointed to by
  684. MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
  685. (Wholesale zeroing is usually a little faster than retail...)
  686. Returns FALSE if data source requested suspension. In that case no
  687. changes have been made to permanent state. (Exception: some output
  688. coefficients may already have been assigned. This is harmless for
  689. this module, since we'll just re-assign them on the next call.) }
  690. {METHODDEF}
  691. function decode_mcu (cinfo : j_decompress_ptr;
  692. var MCU_data : array of JBLOCKROW) : boolean;
  693. label
  694. label1, label2, label3;
  695. var
  696. entropy : huff_entropy_ptr;
  697. {register} s, k, r : int;
  698. blkn, ci : int;
  699. block : JBLOCK_PTR;
  700. {BITREAD_STATE_VARS}
  701. get_buffer : bit_buf_type ; {register}
  702. bits_left : int; {register}
  703. br_state : bitread_working_state;
  704. state : savable_state;
  705. dctbl : d_derived_tbl_ptr;
  706. actbl : d_derived_tbl_ptr;
  707. var
  708. nb, look : int; {register}
  709. begin
  710. entropy := huff_entropy_ptr (cinfo^.entropy);
  711. { Process restart marker if needed; may have to suspend }
  712. if (cinfo^.restart_interval <> 0) then
  713. begin
  714. if (entropy^.restarts_to_go = 0) then
  715. if (not process_restart(cinfo)) then
  716. begin
  717. decode_mcu := FALSE;
  718. exit;
  719. end;
  720. end;
  721. { If we've run out of data, just leave the MCU set to zeroes.
  722. This way, we return uniform gray for the remainder of the segment. }
  723. if not entropy^.pub.insufficient_data then
  724. begin
  725. { Load up working state }
  726. {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
  727. br_state.cinfo := cinfo;
  728. br_state.next_input_byte := cinfo^.src^.next_input_byte;
  729. br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  730. get_buffer := entropy^.bitstate.get_buffer;
  731. bits_left := entropy^.bitstate.bits_left;
  732. {ASSIGN_STATE(state, entropy^.saved);}
  733. state := entropy^.saved;
  734. { Outer loop handles each block in the MCU }
  735. for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  736. begin
  737. block := JBLOCK_PTR(MCU_data[blkn]);
  738. dctbl := entropy^.dc_cur_tbls[blkn];
  739. actbl := entropy^.ac_cur_tbls[blkn];
  740. { Decode a single block's worth of coefficients }
  741. { Section F.2.2.1: decode the DC coefficient difference }
  742. {HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);}
  743. if (bits_left < HUFF_LOOKAHEAD) then
  744. begin
  745. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
  746. begin
  747. decode_mcu := False;
  748. exit;
  749. end;
  750. get_buffer := br_state.get_buffer;
  751. bits_left := br_state.bits_left;
  752. if (bits_left < HUFF_LOOKAHEAD) then
  753. begin
  754. nb := 1;
  755. goto label1;
  756. end;
  757. end;
  758. {look := PEEK_BITS(HUFF_LOOKAHEAD);}
  759. look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
  760. pred(1 shl HUFF_LOOKAHEAD);
  761. nb := dctbl^.look_nbits[look];
  762. if (nb <> 0) then
  763. begin
  764. {DROP_BITS(nb);}
  765. Dec(bits_left, nb);
  766. s := dctbl^.look_sym[look];
  767. end
  768. else
  769. begin
  770. nb := HUFF_LOOKAHEAD+1;
  771. label1:
  772. s := jpeg_huff_decode(br_state,get_buffer,bits_left,dctbl,nb);
  773. if (s < 0) then
  774. begin
  775. decode_mcu := FALSE;
  776. exit;
  777. end;
  778. get_buffer := br_state.get_buffer;
  779. bits_left := br_state.bits_left;
  780. end;
  781. if (s <> 0) then
  782. begin
  783. {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
  784. if (bits_left < s) then
  785. begin
  786. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
  787. begin
  788. decode_mcu := FALSE;
  789. exit;
  790. end;
  791. get_buffer := br_state.get_buffer;
  792. bits_left := br_state.bits_left;
  793. end;
  794. {r := GET_BITS(s);}
  795. Dec(bits_left, s);
  796. r := ( int(get_buffer shr bits_left)) and ( pred(1 shl s) );
  797. {s := HUFF_EXTEND(r, s);}
  798. if (r < extend_test[s]) then
  799. s := r + extend_offset[s]
  800. else
  801. s := r;
  802. end;
  803. if (entropy^.dc_needed[blkn]) then
  804. begin
  805. { Convert DC difference to actual value, update last_dc_val }
  806. ci := cinfo^.MCU_membership[blkn];
  807. Inc(s, state.last_dc_val[ci]);
  808. state.last_dc_val[ci] := s;
  809. { Output the DC coefficient (assumes jpeg_natural_order[0] := 0) }
  810. block^[0] := JCOEF (s);
  811. end;
  812. if (entropy^.ac_needed[blkn]) then
  813. begin
  814. { Section F.2.2.2: decode the AC coefficients }
  815. { Since zeroes are skipped, output area must be cleared beforehand }
  816. k := 1;
  817. while (k < DCTSIZE2) do { Nomssi: k is incr. in the loop }
  818. begin
  819. {HUFF_DECODE(s, br_state, actbl, return FALSE, label2);}
  820. if (bits_left < HUFF_LOOKAHEAD) then
  821. begin
  822. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
  823. begin
  824. decode_mcu := False;
  825. exit;
  826. end;
  827. get_buffer := br_state.get_buffer;
  828. bits_left := br_state.bits_left;
  829. if (bits_left < HUFF_LOOKAHEAD) then
  830. begin
  831. nb := 1;
  832. goto label2;
  833. end;
  834. end;
  835. {look := PEEK_BITS(HUFF_LOOKAHEAD);}
  836. look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
  837. pred(1 shl HUFF_LOOKAHEAD);
  838. nb := actbl^.look_nbits[look];
  839. if (nb <> 0) then
  840. begin
  841. {DROP_BITS(nb);}
  842. Dec(bits_left, nb);
  843. s := actbl^.look_sym[look];
  844. end
  845. else
  846. begin
  847. nb := HUFF_LOOKAHEAD+1;
  848. label2:
  849. s := jpeg_huff_decode(br_state,get_buffer,bits_left,actbl,nb);
  850. if (s < 0) then
  851. begin
  852. decode_mcu := FALSE;
  853. exit;
  854. end;
  855. get_buffer := br_state.get_buffer;
  856. bits_left := br_state.bits_left;
  857. end;
  858. r := s shr 4;
  859. s := s and 15;
  860. if (s <> 0) then
  861. begin
  862. Inc(k, r);
  863. {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
  864. if (bits_left < s) then
  865. begin
  866. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
  867. begin
  868. decode_mcu := FALSE;
  869. exit;
  870. end;
  871. get_buffer := br_state.get_buffer;
  872. bits_left := br_state.bits_left;
  873. end;
  874. {r := GET_BITS(s);}
  875. Dec(bits_left, s);
  876. r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
  877. {s := HUFF_EXTEND(r, s);}
  878. if (r < extend_test[s]) then
  879. s := r + extend_offset[s]
  880. else
  881. s := r;
  882. { Output coefficient in natural (dezigzagged) order.
  883. Note: the extra entries in jpeg_natural_order[] will save us
  884. if k >= DCTSIZE2, which could happen if the data is corrupted. }
  885. block^[jpeg_natural_order[k]] := JCOEF (s);
  886. end
  887. else
  888. begin
  889. if (r <> 15) then
  890. break;
  891. Inc(k, 15);
  892. end;
  893. Inc(k);
  894. end;
  895. end
  896. else
  897. begin
  898. { Section F.2.2.2: decode the AC coefficients }
  899. { In this path we just discard the values }
  900. k := 1;
  901. while (k < DCTSIZE2) do
  902. begin
  903. {HUFF_DECODE(s, br_state, actbl, return FALSE, label3);}
  904. if (bits_left < HUFF_LOOKAHEAD) then
  905. begin
  906. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
  907. begin
  908. decode_mcu := False;
  909. exit;
  910. end;
  911. get_buffer := br_state.get_buffer;
  912. bits_left := br_state.bits_left;
  913. if (bits_left < HUFF_LOOKAHEAD) then
  914. begin
  915. nb := 1;
  916. goto label3;
  917. end;
  918. end;
  919. {look := PEEK_BITS(HUFF_LOOKAHEAD);}
  920. look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
  921. pred(1 shl HUFF_LOOKAHEAD);
  922. nb := actbl^.look_nbits[look];
  923. if (nb <> 0) then
  924. begin
  925. {DROP_BITS(nb);}
  926. Dec(bits_left, nb);
  927. s := actbl^.look_sym[look];
  928. end
  929. else
  930. begin
  931. nb := HUFF_LOOKAHEAD+1;
  932. label3:
  933. s := jpeg_huff_decode(br_state,get_buffer,bits_left,actbl,nb);
  934. if (s < 0) then
  935. begin
  936. decode_mcu := FALSE;
  937. exit;
  938. end;
  939. get_buffer := br_state.get_buffer;
  940. bits_left := br_state.bits_left;
  941. end;
  942. r := s shr 4;
  943. s := s and 15;
  944. if (s <> 0) then
  945. begin
  946. Inc(k, r);
  947. {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
  948. if (bits_left < s) then
  949. begin
  950. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
  951. begin
  952. decode_mcu := FALSE;
  953. exit;
  954. end;
  955. get_buffer := br_state.get_buffer;
  956. bits_left := br_state.bits_left;
  957. end;
  958. {DROP_BITS(s);}
  959. Dec(bits_left, s);
  960. end
  961. else
  962. begin
  963. if (r <> 15) then
  964. break;
  965. Inc(k, 15);
  966. end;
  967. Inc(k);
  968. end;
  969. end;
  970. end;
  971. { Completed MCU, so update state }
  972. {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  973. cinfo^.src^.next_input_byte := br_state.next_input_byte;
  974. cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  975. entropy^.bitstate.get_buffer := get_buffer;
  976. entropy^.bitstate.bits_left := bits_left;
  977. {ASSIGN_STATE(entropy^.saved, state);}
  978. entropy^.saved := state;
  979. end;
  980. { Account for restart interval (no-op if not using restarts) }
  981. if entropy^.restarts_to_go > 0 then
  982. Dec(entropy^.restarts_to_go);
  983. decode_mcu := TRUE;
  984. end;
  985. { Module initialization routine for Huffman entropy decoding. }
  986. {GLOBAL}
  987. procedure jinit_huff_decoder (cinfo : j_decompress_ptr);
  988. var
  989. entropy : huff_entropy_ptr;
  990. i : int;
  991. begin
  992. entropy := huff_entropy_ptr(
  993. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  994. SIZEOF(huff_entropy_decoder)) );
  995. cinfo^.entropy := jpeg_entropy_decoder_ptr (entropy);
  996. entropy^.pub.start_pass := start_pass_huff_decoder;
  997. entropy^.pub.decode_mcu := decode_mcu;
  998. { Mark tables unallocated }
  999. for i := 0 to pred(NUM_HUFF_TBLS) do
  1000. begin
  1001. entropy^.dc_derived_tbls[i] := NIL;
  1002. entropy^.ac_derived_tbls[i] := NIL;
  1003. end;
  1004. end;
  1005. end.