jdphuff.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. Unit JdpHuff;
  2. { This file contains Huffman entropy decoding routines for progressive JPEG.
  3. Much of the complexity here has to do with supporting input suspension.
  4. If the data source module demands suspension, we want to be able to back
  5. up to the start of the current MCU. To do this, we copy state variables
  6. into local working storage, and update them back to the permanent
  7. storage only upon successful completion of an MCU. }
  8. { Original: jdphuff.c ; Copyright (C) 1995-1997, Thomas G. Lane. }
  9. interface
  10. {$I jconfig.inc}
  11. uses
  12. jmorecfg,
  13. jinclude,
  14. jpeglib,
  15. jdeferr,
  16. jerror,
  17. jutils,
  18. jdhuff; { Declarations shared with jdhuff.c }
  19. {GLOBAL}
  20. procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
  21. implementation
  22. { Expanded entropy decoder object for progressive Huffman decoding.
  23. The savable_state subrecord contains fields that change within an MCU,
  24. but must not be updated permanently until we complete the MCU. }
  25. type
  26. savable_state = record
  27. EOBRUN : uInt; { remaining EOBs in EOBRUN }
  28. last_dc_val : array[00..MAX_COMPS_IN_SCAN-1] of int;
  29. { last DC coef for each component }
  30. end;
  31. type
  32. phuff_entropy_ptr = ^phuff_entropy_decoder;
  33. phuff_entropy_decoder = record
  34. pub : jpeg_entropy_decoder; { public fields }
  35. { These fields are loaded into local variables at start of each MCU.
  36. In case of suspension, we exit WITHOUT updating them. }
  37. bitstate : bitread_perm_state; { Bit buffer at start of MCU }
  38. saved : savable_state; { Other state at start of MCU }
  39. { These fields are NOT loaded into local working state. }
  40. restarts_to_go : uInt; { MCUs left in this restart interval }
  41. { Pointers to derived tables (these workspaces have image lifespan) }
  42. derived_tbls : array[0..NUM_HUFF_TBLS-1] of d_derived_tbl_ptr;
  43. ac_derived_tbl : d_derived_tbl_ptr; { active table during an AC scan }
  44. end;
  45. { Forward declarations }
  46. {METHODDEF}
  47. function decode_mcu_DC_first (cinfo : j_decompress_ptr;
  48. var MCU_data : array of JBLOCKROW) : boolean;
  49. far; forward;
  50. {METHODDEF}
  51. function decode_mcu_AC_first (cinfo : j_decompress_ptr;
  52. var MCU_data : array of JBLOCKROW) : boolean;
  53. far; forward;
  54. {METHODDEF}
  55. function decode_mcu_DC_refine (cinfo : j_decompress_ptr;
  56. var MCU_data : array of JBLOCKROW) : boolean;
  57. far; forward;
  58. {METHODDEF}
  59. function decode_mcu_AC_refine (cinfo : j_decompress_ptr;
  60. var MCU_data : array of JBLOCKROW) : boolean;
  61. far; forward;
  62. { Initialize for a Huffman-compressed scan. }
  63. {METHODDEF}
  64. procedure start_pass_phuff_decoder (cinfo : j_decompress_ptr); far;
  65. var
  66. entropy : phuff_entropy_ptr;
  67. is_DC_band, bad : boolean;
  68. ci, coefi, tbl : int;
  69. coef_bit_ptr : coef_bits_ptr;
  70. compptr : jpeg_component_info_ptr;
  71. var
  72. cindex : int;
  73. expected : int;
  74. begin
  75. entropy := phuff_entropy_ptr (cinfo^.entropy);
  76. is_DC_band := (cinfo^.Ss = 0);
  77. { Validate scan parameters }
  78. bad := FALSE;
  79. if (is_DC_band) then
  80. begin
  81. if (cinfo^.Se <> 0) then
  82. bad := TRUE;
  83. end
  84. else
  85. begin
  86. { need not check Ss/Se < 0 since they came from unsigned bytes }
  87. if (cinfo^.Ss > cinfo^.Se) or (cinfo^.Se >= DCTSIZE2) then
  88. bad := TRUE;
  89. { AC scans may have only one component }
  90. if (cinfo^.comps_in_scan <> 1) then
  91. bad := TRUE;
  92. end;
  93. if (cinfo^.Ah <> 0) then
  94. begin
  95. { Successive approximation refinement scan: must have Al = Ah-1. }
  96. if (cinfo^.Al <> cinfo^.Ah-1) then
  97. bad := TRUE;
  98. end;
  99. if (cinfo^.Al > 13) then { need not check for < 0 }
  100. bad := TRUE;
  101. { Arguably the maximum Al value should be less than 13 for 8-bit precision,
  102. but the spec doesn't say so, and we try to be liberal about what we
  103. accept. Note: large Al values could result in out-of-range DC
  104. coefficients during early scans, leading to bizarre displays due to
  105. overflows in the IDCT math. But we won't crash. }
  106. if (bad) then
  107. ERREXIT4(j_common_ptr(cinfo), JERR_BAD_PROGRESSION,
  108. cinfo^.Ss, cinfo^.Se, cinfo^.Ah, cinfo^.Al);
  109. { Update progression status, and verify that scan order is legal.
  110. Note that inter-scan inconsistencies are treated as warnings
  111. not fatal errors ... not clear if this is right way to behave. }
  112. for ci := 0 to pred(cinfo^.comps_in_scan) do
  113. begin
  114. cindex := cinfo^.cur_comp_info[ci]^.component_index;
  115. coef_bit_ptr := coef_bits_ptr(@(cinfo^.coef_bits^[cindex])); {^[0] ???
  116. Nomssi }
  117. if (not is_DC_band) and (coef_bit_ptr^[0] < 0) then
  118. { AC without prior DC scan }
  119. WARNMS2(j_common_ptr(cinfo), JWRN_BOGUS_PROGRESSION, cindex, 0);
  120. for coefi := cinfo^.Ss to cinfo^.Se do
  121. begin
  122. if (coef_bit_ptr^[coefi] < 0) then
  123. expected := 0
  124. else
  125. expected := coef_bit_ptr^[coefi];
  126. if (cinfo^.Ah <> expected) then
  127. WARNMS2(j_common_ptr(cinfo), JWRN_BOGUS_PROGRESSION, cindex, coefi);
  128. coef_bit_ptr^[coefi] := cinfo^.Al;
  129. end;
  130. end;
  131. { Select MCU decoding routine }
  132. if (cinfo^.Ah = 0) then
  133. begin
  134. if (is_DC_band) then
  135. entropy^.pub.decode_mcu := decode_mcu_DC_first
  136. else
  137. entropy^.pub.decode_mcu := decode_mcu_AC_first;
  138. end
  139. else
  140. begin
  141. if (is_DC_band) then
  142. entropy^.pub.decode_mcu := decode_mcu_DC_refine
  143. else
  144. entropy^.pub.decode_mcu := decode_mcu_AC_refine;
  145. end;
  146. for ci := 0 to pred(cinfo^.comps_in_scan) do
  147. begin
  148. compptr := cinfo^.cur_comp_info[ci];
  149. { Make sure requested tables are present, and compute derived tables.
  150. We may build same derived table more than once, but it's not expensive. }
  151. if (is_DC_band) then
  152. begin
  153. if (cinfo^.Ah = 0) then
  154. begin { DC refinement needs no table }
  155. tbl := compptr^.dc_tbl_no;
  156. jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
  157. entropy^.derived_tbls[tbl]);
  158. end;
  159. end
  160. else
  161. begin
  162. tbl := compptr^.ac_tbl_no;
  163. jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
  164. entropy^.derived_tbls[tbl]);
  165. { remember the single active table }
  166. entropy^.ac_derived_tbl := entropy^.derived_tbls[tbl];
  167. end;
  168. { Initialize DC predictions to 0 }
  169. entropy^.saved.last_dc_val[ci] := 0;
  170. end;
  171. { Initialize bitread state variables }
  172. entropy^.bitstate.bits_left := 0;
  173. entropy^.bitstate.get_buffer := 0; { unnecessary, but keeps Purify quiet }
  174. entropy^.pub.insufficient_data := FALSE;
  175. { Initialize private state variables }
  176. entropy^.saved.EOBRUN := 0;
  177. { Initialize restart counter }
  178. entropy^.restarts_to_go := cinfo^.restart_interval;
  179. end;
  180. { Figure F.12: extend sign bit.
  181. On some machines, a shift and add will be faster than a table lookup. }
  182. {$ifdef AVOID_TABLES}
  183. #define HUFF_EXTEND(x,s)
  184. ((x) < (1shl((s)-1)) ? (x) + (((-1)shl(s)) + 1) : (x))
  185. {$else}
  186. { #define HUFF_EXTEND(x,s)
  187. if (x) < extend_test[s] then
  188. (x) + extend_offset[s]
  189. else
  190. (x)}
  191. const
  192. extend_test : Array[0..16-1] of int = { entry n is 2**(n-1) }
  193. ($0000, $0001, $0002, $0004, $0008, $0010, $0020, $0040,
  194. $0080, $0100, $0200, $0400, $0800, $1000, $2000, $4000);
  195. const
  196. extend_offset : array[0..16-1] of int = { entry n is (-1 shl n) + 1 }
  197. ( 0, ((-1) shl 1) + 1, ((-1) shl 2) + 1, ((-1) shl 3) + 1, ((-1) shl 4) + 1,
  198. ((-1) shl 5) + 1, ((-1) shl 6) + 1, ((-1) shl 7) + 1, ((-1) shl 8) + 1,
  199. ((-1) shl 9) + 1, ((-1) shl 10) + 1, ((-1) shl 11) + 1, ((-1) shl 12) + 1,
  200. ((-1) shl 13) + 1, ((-1) shl 14) + 1, ((-1) shl 15) + 1 );
  201. {$endif} { AVOID_TABLES }
  202. { Check for a restart marker & resynchronize decoder.
  203. return:=s FALSE if must suspend. }
  204. {LOCAL}
  205. function process_restart (cinfo : j_decompress_ptr) : boolean;
  206. var
  207. entropy : phuff_entropy_ptr;
  208. ci : int;
  209. begin
  210. entropy := phuff_entropy_ptr (cinfo^.entropy);
  211. { Throw away any unused bits remaining in bit buffer; }
  212. { include any full bytes in next_marker's count of discarded bytes }
  213. Inc(cinfo^.marker^.discarded_bytes, entropy^.bitstate.bits_left div 8);
  214. entropy^.bitstate.bits_left := 0;
  215. { Advance past the RSTn marker }
  216. if (not cinfo^.marker^.read_restart_marker (cinfo)) then
  217. begin
  218. process_restart := FALSE;
  219. exit;
  220. end;
  221. { Re-initialize DC predictions to 0 }
  222. for ci := 0 to pred(cinfo^.comps_in_scan) do
  223. entropy^.saved.last_dc_val[ci] := 0;
  224. { Re-init EOB run count, too }
  225. entropy^.saved.EOBRUN := 0;
  226. { Reset restart counter }
  227. entropy^.restarts_to_go := cinfo^.restart_interval;
  228. { Reset out-of-data flag, unless read_restart_marker left us smack up
  229. against a marker. In that case we will end up treating the next data
  230. segment as empty, and we can avoid producing bogus output pixels by
  231. leaving the flag set. }
  232. if (cinfo^.unread_marker = 0) then
  233. entropy^.pub.insufficient_data := FALSE;
  234. process_restart := TRUE;
  235. end;
  236. { Huffman MCU decoding.
  237. Each of these routines decodes and returns one MCU's worth of
  238. Huffman-compressed coefficients.
  239. The coefficients are reordered from zigzag order into natural array order,
  240. but are not dequantized.
  241. The i'th block of the MCU is stored into the block pointed to by
  242. MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
  243. We return FALSE if data source requested suspension. In that case no
  244. changes have been made to permanent state. (Exception: some output
  245. coefficients may already have been assigned. This is harmless for
  246. spectral selection, since we'll just re-assign them on the next call.
  247. Successive approximation AC refinement has to be more careful, however.) }
  248. { MCU decoding for DC initial scan (either spectral selection,
  249. or first pass of successive approximation). }
  250. {METHODDEF}
  251. function decode_mcu_DC_first (cinfo : j_decompress_ptr;
  252. var MCU_data : array of JBLOCKROW) : boolean;
  253. label
  254. label1;
  255. var
  256. entropy : phuff_entropy_ptr;
  257. Al : int;
  258. {register} s, r : int;
  259. blkn, ci : int;
  260. block : JBLOCK_PTR;
  261. {BITREAD_STATE_VARS;}
  262. get_buffer : bit_buf_type ; {register}
  263. bits_left : int; {register}
  264. br_state : bitread_working_state;
  265. state : savable_state;
  266. tbl : d_derived_tbl_ptr;
  267. compptr : jpeg_component_info_ptr;
  268. var
  269. nb, look : int; {register}
  270. begin
  271. entropy := phuff_entropy_ptr (cinfo^.entropy);
  272. Al := cinfo^.Al;
  273. { Process restart marker if needed; may have to suspend }
  274. if (cinfo^.restart_interval <> 0) then
  275. begin
  276. if (entropy^.restarts_to_go = 0) then
  277. if (not process_restart(cinfo)) then
  278. begin
  279. decode_mcu_DC_first := FALSE;
  280. exit;
  281. end;
  282. end;
  283. { If we've run out of data, just leave the MCU set to zeroes.
  284. This way, we return uniform gray for the remainder of the segment. }
  285. if not entropy^.pub.insufficient_data then
  286. begin
  287. { Load up working state }
  288. {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
  289. br_state.cinfo := cinfo;
  290. br_state.next_input_byte := cinfo^.src^.next_input_byte;
  291. br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  292. get_buffer := entropy^.bitstate.get_buffer;
  293. bits_left := entropy^.bitstate.bits_left;
  294. {ASSIGN_STATE(state, entropy^.saved);}
  295. state := entropy^.saved;
  296. { Outer loop handles each block in the MCU }
  297. for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  298. begin
  299. block := JBLOCK_PTR(MCU_data[blkn]);
  300. ci := cinfo^.MCU_membership[blkn];
  301. compptr := cinfo^.cur_comp_info[ci];
  302. tbl := entropy^.derived_tbls[compptr^.dc_tbl_no];
  303. { Decode a single block's worth of coefficients }
  304. { Section F.2.2.1: decode the DC coefficient difference }
  305. {HUFF_DECODE(s, br_state, tbl, return FALSE, label1);}
  306. if (bits_left < HUFF_LOOKAHEAD) then
  307. begin
  308. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
  309. begin
  310. decode_mcu_DC_first := FALSE;
  311. exit;
  312. end;
  313. get_buffer := br_state.get_buffer;
  314. bits_left := br_state.bits_left;
  315. if (bits_left < HUFF_LOOKAHEAD) then
  316. begin
  317. nb := 1;
  318. goto label1;
  319. end;
  320. end;
  321. {look := PEEK_BITS(HUFF_LOOKAHEAD);}
  322. look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
  323. pred(1 shl HUFF_LOOKAHEAD);
  324. nb := tbl^.look_nbits[look];
  325. if (nb <> 0) then
  326. begin
  327. {DROP_BITS(nb);}
  328. Dec(bits_left, nb);
  329. s := tbl^.look_sym[look];
  330. end
  331. else
  332. begin
  333. nb := HUFF_LOOKAHEAD+1;
  334. label1:
  335. s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
  336. if (s < 0) then
  337. begin
  338. decode_mcu_DC_first := FALSE;
  339. exit;
  340. end;
  341. get_buffer := br_state.get_buffer;
  342. bits_left := br_state.bits_left;
  343. end;
  344. if (s <> 0) then
  345. begin
  346. {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
  347. if (bits_left < s) then
  348. begin
  349. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
  350. begin
  351. decode_mcu_DC_first := FALSE;
  352. exit;
  353. end;
  354. get_buffer := br_state.get_buffer;
  355. bits_left := br_state.bits_left;
  356. end;
  357. {r := GET_BITS(s);}
  358. Dec(bits_left, s);
  359. r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
  360. {s := HUFF_EXTEND(r, s);}
  361. if (r < extend_test[s]) then
  362. s := r + extend_offset[s]
  363. else
  364. s := r;
  365. end;
  366. { Convert DC difference to actual value, update last_dc_val }
  367. Inc(s, state.last_dc_val[ci]);
  368. state.last_dc_val[ci] := s;
  369. { Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) }
  370. block^[0] := JCOEF (s shl Al);
  371. end;
  372. { Completed MCU, so update state }
  373. {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  374. cinfo^.src^.next_input_byte := br_state.next_input_byte;
  375. cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  376. entropy^.bitstate.get_buffer := get_buffer;
  377. entropy^.bitstate.bits_left := bits_left;
  378. {ASSIGN_STATE(entropy^.saved, state);}
  379. entropy^.saved := state;
  380. end;
  381. { Account for restart interval (no-op if not using restarts) }
  382. Dec(entropy^.restarts_to_go);
  383. decode_mcu_DC_first := TRUE;
  384. end;
  385. { MCU decoding for AC initial scan (either spectral selection,
  386. or first pass of successive approximation). }
  387. {METHODDEF}
  388. function decode_mcu_AC_first (cinfo : j_decompress_ptr;
  389. var MCU_data : array of JBLOCKROW) : boolean;
  390. label
  391. label2;
  392. var
  393. entropy : phuff_entropy_ptr;
  394. Se : int;
  395. Al : int;
  396. {register} s, k, r : int;
  397. EOBRUN : uInt;
  398. block : JBLOCK_PTR;
  399. {BITREAD_STATE_VARS;}
  400. get_buffer : bit_buf_type ; {register}
  401. bits_left : int; {register}
  402. br_state : bitread_working_state;
  403. tbl : d_derived_tbl_ptr;
  404. var
  405. nb, look : int; {register}
  406. begin
  407. entropy := phuff_entropy_ptr (cinfo^.entropy);
  408. Se := cinfo^.Se;
  409. Al := cinfo^.Al;
  410. { Process restart marker if needed; may have to suspend }
  411. if (cinfo^.restart_interval <> 0) then
  412. begin
  413. if (entropy^.restarts_to_go = 0) then
  414. if (not process_restart(cinfo)) then
  415. begin
  416. decode_mcu_AC_first := FALSE;
  417. exit;
  418. end;
  419. end;
  420. { If we've run out of data, just leave the MCU set to zeroes.
  421. This way, we return uniform gray for the remainder of the segment. }
  422. if not entropy^.pub.insufficient_data then
  423. begin
  424. { Load up working state.
  425. We can avoid loading/saving bitread state if in an EOB run. }
  426. EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }
  427. { There is always only one block per MCU }
  428. if (EOBRUN > 0) then { if it's a band of zeroes... }
  429. Dec(EOBRUN) { ...process it now (we do nothing) }
  430. else
  431. begin
  432. {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
  433. br_state.cinfo := cinfo;
  434. br_state.next_input_byte := cinfo^.src^.next_input_byte;
  435. br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  436. get_buffer := entropy^.bitstate.get_buffer;
  437. bits_left := entropy^.bitstate.bits_left;
  438. block := JBLOCK_PTR(MCU_data[0]);
  439. tbl := entropy^.ac_derived_tbl;
  440. k := cinfo^.Ss;
  441. while (k <= Se) do
  442. begin
  443. {HUFF_DECODE(s, br_state, tbl, return FALSE, label2);}
  444. if (bits_left < HUFF_LOOKAHEAD) then
  445. begin
  446. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
  447. begin
  448. decode_mcu_AC_first := FALSE;
  449. exit;
  450. end;
  451. get_buffer := br_state.get_buffer;
  452. bits_left := br_state.bits_left;
  453. if (bits_left < HUFF_LOOKAHEAD) then
  454. begin
  455. nb := 1;
  456. goto label2;
  457. end;
  458. end;
  459. {look := PEEK_BITS(HUFF_LOOKAHEAD);}
  460. look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
  461. pred(1 shl HUFF_LOOKAHEAD);
  462. nb := tbl^.look_nbits[look];
  463. if (nb <> 0) then
  464. begin
  465. {DROP_BITS(nb);}
  466. Dec(bits_left, nb);
  467. s := tbl^.look_sym[look];
  468. end
  469. else
  470. begin
  471. nb := HUFF_LOOKAHEAD+1;
  472. label2:
  473. s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
  474. if (s < 0) then
  475. begin
  476. decode_mcu_AC_first := FALSE;
  477. exit;
  478. end;
  479. get_buffer := br_state.get_buffer;
  480. bits_left := br_state.bits_left;
  481. end;
  482. r := s shr 4;
  483. s := s and 15;
  484. if (s <> 0) then
  485. begin
  486. Inc(k, r);
  487. {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
  488. if (bits_left < s) then
  489. begin
  490. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
  491. begin
  492. decode_mcu_AC_first := FALSE;
  493. exit;
  494. end;
  495. get_buffer := br_state.get_buffer;
  496. bits_left := br_state.bits_left;
  497. end;
  498. {r := GET_BITS(s);}
  499. Dec(bits_left, s);
  500. r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
  501. {s := HUFF_EXTEND(r, s);}
  502. if (r < extend_test[s]) then
  503. s := r + extend_offset[s]
  504. else
  505. s := r;
  506. { Scale and output coefficient in natural (dezigzagged) order }
  507. block^[jpeg_natural_order[k]] := JCOEF (s shl Al);
  508. end
  509. else
  510. begin
  511. if (r = 15) then
  512. begin { ZRL }
  513. Inc(k, 15); { skip 15 zeroes in band }
  514. end
  515. else
  516. begin { EOBr, run length is 2^r + appended bits }
  517. EOBRUN := 1 shl r;
  518. if (r <> 0) then
  519. begin { EOBr, r > 0 }
  520. {CHECK_BIT_BUFFER(br_state, r, return FALSE);}
  521. if (bits_left < r) then
  522. begin
  523. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
  524. begin
  525. decode_mcu_AC_first := FALSE;
  526. exit;
  527. end;
  528. get_buffer := br_state.get_buffer;
  529. bits_left := br_state.bits_left;
  530. end;
  531. {r := GET_BITS(r);}
  532. Dec(bits_left, r);
  533. r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );
  534. Inc(EOBRUN, r);
  535. end;
  536. Dec(EOBRUN); { this band is processed at this moment }
  537. break; { force end-of-band }
  538. end;
  539. end;
  540. Inc(k);
  541. end;
  542. {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  543. cinfo^.src^.next_input_byte := br_state.next_input_byte;
  544. cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  545. entropy^.bitstate.get_buffer := get_buffer;
  546. entropy^.bitstate.bits_left := bits_left;
  547. end;
  548. { Completed MCU, so update state }
  549. entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }
  550. end;
  551. { Account for restart interval (no-op if not using restarts) }
  552. Dec(entropy^.restarts_to_go);
  553. decode_mcu_AC_first := TRUE;
  554. end;
  555. { MCU decoding for DC successive approximation refinement scan.
  556. Note: we assume such scans can be multi-component, although the spec
  557. is not very clear on the point. }
  558. {METHODDEF}
  559. function decode_mcu_DC_refine (cinfo : j_decompress_ptr;
  560. var MCU_data : array of JBLOCKROW) : boolean;
  561. var
  562. entropy : phuff_entropy_ptr;
  563. p1 : int; { 1 in the bit position being coded }
  564. blkn : int;
  565. block : JBLOCK_PTR;
  566. {BITREAD_STATE_VARS;}
  567. get_buffer : bit_buf_type ; {register}
  568. bits_left : int; {register}
  569. br_state : bitread_working_state;
  570. begin
  571. entropy := phuff_entropy_ptr (cinfo^.entropy);
  572. p1 := 1 shl cinfo^.Al;
  573. { Process restart marker if needed; may have to suspend }
  574. if (cinfo^.restart_interval <> 0) then
  575. begin
  576. if (entropy^.restarts_to_go = 0) then
  577. if (not process_restart(cinfo)) then
  578. begin
  579. decode_mcu_DC_refine := FALSE;
  580. exit;
  581. end;
  582. end;
  583. { Not worth the cycles to check insufficient_data here,
  584. since we will not change the data anyway if we read zeroes. }
  585. { Load up working state }
  586. {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
  587. br_state.cinfo := cinfo;
  588. br_state.next_input_byte := cinfo^.src^.next_input_byte;
  589. br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  590. get_buffer := entropy^.bitstate.get_buffer;
  591. bits_left := entropy^.bitstate.bits_left;
  592. { Outer loop handles each block in the MCU }
  593. for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  594. begin
  595. block := JBLOCK_PTR(MCU_data[blkn]);
  596. { Encoded data is simply the next bit of the two's-complement DC value }
  597. {CHECK_BIT_BUFFER(br_state, 1, return FALSE);}
  598. if (bits_left < 1) then
  599. begin
  600. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
  601. begin
  602. decode_mcu_DC_refine := FALSE;
  603. exit;
  604. end;
  605. get_buffer := br_state.get_buffer;
  606. bits_left := br_state.bits_left;
  607. end;
  608. {if (GET_BITS(1)) then}
  609. Dec(bits_left);
  610. if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) ) <> 0 then
  611. block^[0] := block^[0] or p1;
  612. { Note: since we use OR, repeating the assignment later is safe }
  613. end;
  614. { Completed MCU, so update state }
  615. {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  616. cinfo^.src^.next_input_byte := br_state.next_input_byte;
  617. cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  618. entropy^.bitstate.get_buffer := get_buffer;
  619. entropy^.bitstate.bits_left := bits_left;
  620. { Account for restart interval (no-op if not using restarts) }
  621. Dec(entropy^.restarts_to_go);
  622. decode_mcu_DC_refine := TRUE;
  623. end;
  624. { MCU decoding for AC successive approximation refinement scan. }
  625. {METHODDEF}
  626. function decode_mcu_AC_refine (cinfo : j_decompress_ptr;
  627. var MCU_data : array of JBLOCKROW) : boolean;
  628. label
  629. undoit, label3;
  630. var
  631. entropy : phuff_entropy_ptr;
  632. Se : int;
  633. p1 : int; { 1 in the bit position being coded }
  634. m1 : int; { -1 in the bit position being coded }
  635. {register} s, k, r : int;
  636. EOBRUN : uInt;
  637. block : JBLOCK_PTR;
  638. thiscoef : JCOEF_PTR;
  639. {BITREAD_STATE_VARS;}
  640. get_buffer : bit_buf_type ; {register}
  641. bits_left : int; {register}
  642. br_state : bitread_working_state;
  643. tbl : d_derived_tbl_ptr;
  644. num_newnz : int;
  645. newnz_pos : array[0..DCTSIZE2-1] of int;
  646. var
  647. pos : int;
  648. var
  649. nb, look : int; {register}
  650. begin
  651. entropy := phuff_entropy_ptr (cinfo^.entropy);
  652. Se := cinfo^.Se;
  653. p1 := 1 shl cinfo^.Al; { 1 in the bit position being coded }
  654. m1 := (-1) shl cinfo^.Al; { -1 in the bit position being coded }
  655. { Process restart marker if needed; may have to suspend }
  656. if (cinfo^.restart_interval <> 0) then
  657. begin
  658. if (entropy^.restarts_to_go = 0) then
  659. if (not process_restart(cinfo)) then
  660. begin
  661. decode_mcu_AC_refine := FALSE;
  662. exit;
  663. end;
  664. end;
  665. { If we've run out of data, don't modify the MCU. }
  666. if not entropy^.pub.insufficient_data then
  667. begin
  668. { Load up working state }
  669. {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
  670. br_state.cinfo := cinfo;
  671. br_state.next_input_byte := cinfo^.src^.next_input_byte;
  672. br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  673. get_buffer := entropy^.bitstate.get_buffer;
  674. bits_left := entropy^.bitstate.bits_left;
  675. EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }
  676. { There is always only one block per MCU }
  677. block := JBLOCK_PTR(MCU_data[0]);
  678. tbl := entropy^.ac_derived_tbl;
  679. { If we are forced to suspend, we must undo the assignments to any newly
  680. nonzero coefficients in the block, because otherwise we'd get confused
  681. next time about which coefficients were already nonzero.
  682. But we need not undo addition of bits to already-nonzero coefficients;
  683. instead, we can test the current bit position to see if we already did it.}
  684. num_newnz := 0;
  685. { initialize coefficient loop counter to start of band }
  686. k := cinfo^.Ss;
  687. if (EOBRUN = 0) then
  688. begin
  689. while (k <= Se) do
  690. begin
  691. {HUFF_DECODE(s, br_state, tbl, goto undoit, label3);}
  692. if (bits_left < HUFF_LOOKAHEAD) then
  693. begin
  694. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
  695. goto undoit;
  696. get_buffer := br_state.get_buffer;
  697. bits_left := br_state.bits_left;
  698. if (bits_left < HUFF_LOOKAHEAD) then
  699. begin
  700. nb := 1;
  701. goto label3;
  702. end;
  703. end;
  704. {look := PEEK_BITS(HUFF_LOOKAHEAD);}
  705. look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
  706. pred(1 shl HUFF_LOOKAHEAD);
  707. nb := tbl^.look_nbits[look];
  708. if (nb <> 0) then
  709. begin
  710. {DROP_BITS(nb);}
  711. Dec(bits_left, nb);
  712. s := tbl^.look_sym[look];
  713. end
  714. else
  715. begin
  716. nb := HUFF_LOOKAHEAD+1;
  717. label3:
  718. s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
  719. if (s < 0) then
  720. goto undoit;
  721. get_buffer := br_state.get_buffer;
  722. bits_left := br_state.bits_left;
  723. end;
  724. r := s shr 4;
  725. s := s and 15;
  726. if (s <> 0) then
  727. begin
  728. if (s <> 1) then { size of new coef should always be 1 }
  729. WARNMS(j_common_ptr(cinfo), JWRN_HUFF_BAD_CODE);
  730. {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
  731. if (bits_left < 1) then
  732. begin
  733. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
  734. goto undoit;
  735. get_buffer := br_state.get_buffer;
  736. bits_left := br_state.bits_left;
  737. end;
  738. {if (GET_BITS(1)) then}
  739. Dec(bits_left);
  740. if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
  741. s := p1 { newly nonzero coef is positive }
  742. else
  743. s := m1; { newly nonzero coef is negative }
  744. end
  745. else
  746. begin
  747. if (r <> 15) then
  748. begin
  749. EOBRUN := 1 shl r; { EOBr, run length is 2^r + appended bits }
  750. if (r <> 0) then
  751. begin
  752. {CHECK_BIT_BUFFER(br_state, r, goto undoit);}
  753. if (bits_left < r) then
  754. begin
  755. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
  756. goto undoit;
  757. get_buffer := br_state.get_buffer;
  758. bits_left := br_state.bits_left;
  759. end;
  760. {r := GET_BITS(r);}
  761. Dec(bits_left, r);
  762. r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );
  763. Inc(EOBRUN, r);
  764. end;
  765. break; { rest of block is handled by EOB logic }
  766. end;
  767. { note s := 0 for processing ZRL }
  768. end;
  769. { Advance over already-nonzero coefs and r still-zero coefs,
  770. appending correction bits to the nonzeroes. A correction bit is 1
  771. if the absolute value of the coefficient must be increased. }
  772. repeat
  773. thiscoef :=@(block^[jpeg_natural_order[k]]);
  774. if (thiscoef^ <> 0) then
  775. begin
  776. {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
  777. if (bits_left < 1) then
  778. begin
  779. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
  780. goto undoit;
  781. get_buffer := br_state.get_buffer;
  782. bits_left := br_state.bits_left;
  783. end;
  784. {if (GET_BITS(1)) then}
  785. Dec(bits_left);
  786. if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
  787. begin
  788. if ((thiscoef^ and p1) = 0) then
  789. begin { do nothing if already set it }
  790. if (thiscoef^ >= 0) then
  791. Inc(thiscoef^, p1)
  792. else
  793. Inc(thiscoef^, m1);
  794. end;
  795. end;
  796. end
  797. else
  798. begin
  799. Dec(r);
  800. if (r < 0) then
  801. break; { reached target zero coefficient }
  802. end;
  803. Inc(k);
  804. until (k > Se);
  805. if (s <> 0) then
  806. begin
  807. pos := jpeg_natural_order[k];
  808. { Output newly nonzero coefficient }
  809. block^[pos] := JCOEF (s);
  810. { Remember its position in case we have to suspend }
  811. newnz_pos[num_newnz] := pos;
  812. Inc(num_newnz);
  813. end;
  814. Inc(k);
  815. end;
  816. end;
  817. if (EOBRUN > 0) then
  818. begin
  819. { Scan any remaining coefficient positions after the end-of-band
  820. (the last newly nonzero coefficient, if any). Append a correction
  821. bit to each already-nonzero coefficient. A correction bit is 1
  822. if the absolute value of the coefficient must be increased. }
  823. while (k <= Se) do
  824. begin
  825. thiscoef := @(block^[jpeg_natural_order[k]]);
  826. if (thiscoef^ <> 0) then
  827. begin
  828. {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
  829. if (bits_left < 1) then
  830. begin
  831. if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
  832. goto undoit;
  833. get_buffer := br_state.get_buffer;
  834. bits_left := br_state.bits_left;
  835. end;
  836. {if (GET_BITS(1)) then}
  837. Dec(bits_left);
  838. if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
  839. begin
  840. if ((thiscoef^ and p1) = 0) then
  841. begin { do nothing if already changed it }
  842. if (thiscoef^ >= 0) then
  843. Inc(thiscoef^, p1)
  844. else
  845. Inc(thiscoef^, m1);
  846. end;
  847. end;
  848. end;
  849. Inc(k);
  850. end;
  851. { Count one block completed in EOB run }
  852. Dec(EOBRUN);
  853. end;
  854. { Completed MCU, so update state }
  855. {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  856. cinfo^.src^.next_input_byte := br_state.next_input_byte;
  857. cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  858. entropy^.bitstate.get_buffer := get_buffer;
  859. entropy^.bitstate.bits_left := bits_left;
  860. entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }
  861. end;
  862. { Account for restart interval (no-op if not using restarts) }
  863. Dec(entropy^.restarts_to_go);
  864. decode_mcu_AC_refine := TRUE;
  865. exit;
  866. undoit:
  867. { Re-zero any output coefficients that we made newly nonzero }
  868. while (num_newnz > 0) do
  869. begin
  870. Dec(num_newnz);
  871. block^[newnz_pos[num_newnz]] := 0;
  872. end;
  873. decode_mcu_AC_refine := FALSE;
  874. end;
  875. { Module initialization routine for progressive Huffman entropy decoding. }
  876. {GLOBAL}
  877. procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
  878. var
  879. entropy : phuff_entropy_ptr;
  880. coef_bit_ptr : int_ptr;
  881. ci, i : int;
  882. begin
  883. entropy := phuff_entropy_ptr(
  884. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  885. SIZEOF(phuff_entropy_decoder)) );
  886. cinfo^.entropy := jpeg_entropy_decoder_ptr (entropy);
  887. entropy^.pub.start_pass := start_pass_phuff_decoder;
  888. { Mark derived tables unallocated }
  889. for i := 0 to pred(NUM_HUFF_TBLS) do
  890. begin
  891. entropy^.derived_tbls[i] := NIL;
  892. end;
  893. { Create progression status table }
  894. cinfo^.coef_bits := coef_bits_ptrrow (
  895. cinfo^.mem^.alloc_small ( j_common_ptr (cinfo), JPOOL_IMAGE,
  896. cinfo^.num_components*DCTSIZE2*SIZEOF(int)) );
  897. coef_bit_ptr := @cinfo^.coef_bits^[0][0];
  898. for ci := 0 to pred(cinfo^.num_components) do
  899. for i := 0 to pred(DCTSIZE2) do
  900. begin
  901. coef_bit_ptr^ := -1;
  902. Inc(coef_bit_ptr);
  903. end;
  904. end;
  905. end.