jdhuff.pas 39 KB

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