jdmarker.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. * jdmarker.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains routines to decode JPEG datastream markers.
  9. * Most of the complexity arises from our desire to support input
  10. * suspension: if not all of the data for a marker is available,
  11. * we must exit back to the application. On resumption, we reprocess
  12. * the marker.
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. typedef enum { /* JPEG marker codes */
  18. M_SOF0 = 0xc0,
  19. M_SOF1 = 0xc1,
  20. M_SOF2 = 0xc2,
  21. M_SOF3 = 0xc3,
  22. M_SOF5 = 0xc5,
  23. M_SOF6 = 0xc6,
  24. M_SOF7 = 0xc7,
  25. M_JPG = 0xc8,
  26. M_SOF9 = 0xc9,
  27. M_SOF10 = 0xca,
  28. M_SOF11 = 0xcb,
  29. M_SOF13 = 0xcd,
  30. M_SOF14 = 0xce,
  31. M_SOF15 = 0xcf,
  32. M_DHT = 0xc4,
  33. M_DAC = 0xcc,
  34. M_RST0 = 0xd0,
  35. M_RST1 = 0xd1,
  36. M_RST2 = 0xd2,
  37. M_RST3 = 0xd3,
  38. M_RST4 = 0xd4,
  39. M_RST5 = 0xd5,
  40. M_RST6 = 0xd6,
  41. M_RST7 = 0xd7,
  42. M_SOI = 0xd8,
  43. M_EOI = 0xd9,
  44. M_SOS = 0xda,
  45. M_DQT = 0xdb,
  46. M_DNL = 0xdc,
  47. M_DRI = 0xdd,
  48. M_DHP = 0xde,
  49. M_EXP = 0xdf,
  50. M_APP0 = 0xe0,
  51. M_APP1 = 0xe1,
  52. M_APP2 = 0xe2,
  53. M_APP3 = 0xe3,
  54. M_APP4 = 0xe4,
  55. M_APP5 = 0xe5,
  56. M_APP6 = 0xe6,
  57. M_APP7 = 0xe7,
  58. M_APP8 = 0xe8,
  59. M_APP9 = 0xe9,
  60. M_APP10 = 0xea,
  61. M_APP11 = 0xeb,
  62. M_APP12 = 0xec,
  63. M_APP13 = 0xed,
  64. M_APP14 = 0xee,
  65. M_APP15 = 0xef,
  66. M_JPG0 = 0xf0,
  67. M_JPG13 = 0xfd,
  68. M_COM = 0xfe,
  69. M_TEM = 0x01,
  70. M_ERROR = 0x100
  71. } JPEG_MARKER;
  72. /*
  73. * Macros for fetching data from the data source module.
  74. *
  75. * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
  76. * the current restart point; we update them only when we have reached a
  77. * suitable place to restart if a suspension occurs.
  78. */
  79. /* Declare and initialize local copies of input pointer/count */
  80. #define INPUT_VARS(cinfo) \
  81. struct jpeg_source_mgr * datasrc = (cinfo)->src; \
  82. const JOCTET * next_input_byte = datasrc->next_input_byte; \
  83. size_t bytes_in_buffer = datasrc->bytes_in_buffer
  84. /* Unload the local copies --- do this only at a restart boundary */
  85. #define INPUT_SYNC(cinfo) \
  86. ( datasrc->next_input_byte = next_input_byte, \
  87. datasrc->bytes_in_buffer = bytes_in_buffer )
  88. /* Reload the local copies --- seldom used except in MAKE_BYTE_AVAIL */
  89. #define INPUT_RELOAD(cinfo) \
  90. ( next_input_byte = datasrc->next_input_byte, \
  91. bytes_in_buffer = datasrc->bytes_in_buffer )
  92. /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
  93. * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  94. * but we must reload the local copies after a successful fill.
  95. */
  96. #define MAKE_BYTE_AVAIL(cinfo,action) \
  97. if (bytes_in_buffer == 0) { \
  98. if (! (*datasrc->fill_input_buffer) (cinfo)) \
  99. { action; } \
  100. INPUT_RELOAD(cinfo); \
  101. } \
  102. bytes_in_buffer--
  103. /* Read a byte into variable V.
  104. * If must suspend, take the specified action (typically "return FALSE").
  105. */
  106. #define INPUT_BYTE(cinfo,V,action) \
  107. MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
  108. V = GETJOCTET(*next_input_byte++); )
  109. /* As above, but read two bytes interpreted as an unsigned 16-bit integer.
  110. * V should be declared unsigned int or perhaps INT32.
  111. */
  112. #define INPUT_2BYTES(cinfo,V,action) \
  113. MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
  114. V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
  115. MAKE_BYTE_AVAIL(cinfo,action); \
  116. V += GETJOCTET(*next_input_byte++); )
  117. /*
  118. * Routines to process JPEG markers.
  119. *
  120. * Entry condition: JPEG marker itself has been read and its code saved
  121. * in cinfo->unread_marker; input restart point is just after the marker.
  122. *
  123. * Exit: if return TRUE, have read and processed any parameters, and have
  124. * updated the restart point to point after the parameters.
  125. * If return FALSE, was forced to suspend before reaching end of
  126. * marker parameters; restart point has not been moved. Same routine
  127. * will be called again after application supplies more input data.
  128. *
  129. * This approach to suspension assumes that all of a marker's parameters can
  130. * fit into a single input bufferload. This should hold for "normal"
  131. * markers. Some COM/APPn markers might have large parameter segments,
  132. * but we use skip_input_data to get past those, and thereby put the problem
  133. * on the source manager's shoulders.
  134. *
  135. * Note that we don't bother to avoid duplicate trace messages if a
  136. * suspension occurs within marker parameters. Other side effects
  137. * require more care.
  138. */
  139. LOCAL(boolean)
  140. get_soi (j_decompress_ptr cinfo)
  141. /* Process an SOI marker */
  142. {
  143. int i;
  144. TRACEMS(cinfo, 1, JTRC_SOI);
  145. if (cinfo->marker->saw_SOI)
  146. ERREXIT(cinfo, JERR_SOI_DUPLICATE);
  147. /* Reset all parameters that are defined to be reset by SOI */
  148. for (i = 0; i < NUM_ARITH_TBLS; i++) {
  149. cinfo->arith_dc_L[i] = 0;
  150. cinfo->arith_dc_U[i] = 1;
  151. cinfo->arith_ac_K[i] = 5;
  152. }
  153. cinfo->restart_interval = 0;
  154. /* Set initial assumptions for colorspace etc */
  155. cinfo->jpeg_color_space = JCS_UNKNOWN;
  156. cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
  157. cinfo->saw_JFIF_marker = FALSE;
  158. cinfo->density_unit = 0; /* set default JFIF APP0 values */
  159. cinfo->X_density = 1;
  160. cinfo->Y_density = 1;
  161. cinfo->saw_Adobe_marker = FALSE;
  162. cinfo->Adobe_transform = 0;
  163. cinfo->marker->saw_SOI = TRUE;
  164. return TRUE;
  165. }
  166. LOCAL(boolean)
  167. get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
  168. /* Process a SOFn marker */
  169. {
  170. INT32 length;
  171. int c, ci;
  172. jpeg_component_info * compptr;
  173. INPUT_VARS(cinfo);
  174. cinfo->progressive_mode = is_prog;
  175. cinfo->arith_code = is_arith;
  176. INPUT_2BYTES(cinfo, length, return FALSE);
  177. INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
  178. INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
  179. INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
  180. INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
  181. length -= 8;
  182. TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
  183. (int) cinfo->image_width, (int) cinfo->image_height,
  184. cinfo->num_components);
  185. if (cinfo->marker->saw_SOF)
  186. ERREXIT(cinfo, JERR_SOF_DUPLICATE);
  187. /* We don't support files in which the image height is initially specified */
  188. /* as 0 and is later redefined by DNL. As long as we have to check that, */
  189. /* might as well have a general sanity check. */
  190. if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  191. || cinfo->num_components <= 0)
  192. ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  193. if (length != (cinfo->num_components * 3))
  194. ERREXIT(cinfo, JERR_BAD_LENGTH);
  195. if (cinfo->comp_info == NULL) /* do only once, even if suspend */
  196. cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
  197. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  198. cinfo->num_components * SIZEOF(jpeg_component_info));
  199. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  200. ci++, compptr++) {
  201. compptr->component_index = ci;
  202. INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
  203. INPUT_BYTE(cinfo, c, return FALSE);
  204. compptr->h_samp_factor = (c >> 4) & 15;
  205. compptr->v_samp_factor = (c ) & 15;
  206. INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
  207. TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
  208. compptr->component_id, compptr->h_samp_factor,
  209. compptr->v_samp_factor, compptr->quant_tbl_no);
  210. }
  211. cinfo->marker->saw_SOF = TRUE;
  212. INPUT_SYNC(cinfo);
  213. return TRUE;
  214. }
  215. LOCAL(boolean)
  216. get_sos (j_decompress_ptr cinfo)
  217. /* Process a SOS marker */
  218. {
  219. INT32 length;
  220. int i, ci, n, c, cc;
  221. jpeg_component_info * compptr;
  222. INPUT_VARS(cinfo);
  223. if (! cinfo->marker->saw_SOF)
  224. ERREXIT(cinfo, JERR_SOS_NO_SOF);
  225. INPUT_2BYTES(cinfo, length, return FALSE);
  226. INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
  227. if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
  228. ERREXIT(cinfo, JERR_BAD_LENGTH);
  229. TRACEMS1(cinfo, 1, JTRC_SOS, n);
  230. cinfo->comps_in_scan = n;
  231. /* Collect the component-spec parameters */
  232. for (i = 0; i < n; i++) {
  233. INPUT_BYTE(cinfo, cc, return FALSE);
  234. INPUT_BYTE(cinfo, c, return FALSE);
  235. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  236. ci++, compptr++) {
  237. if (cc == compptr->component_id)
  238. goto id_found;
  239. }
  240. ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
  241. id_found:
  242. cinfo->cur_comp_info[i] = compptr;
  243. compptr->dc_tbl_no = (c >> 4) & 15;
  244. compptr->ac_tbl_no = (c ) & 15;
  245. TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
  246. compptr->dc_tbl_no, compptr->ac_tbl_no);
  247. }
  248. /* Collect the additional scan parameters Ss, Se, Ah/Al. */
  249. INPUT_BYTE(cinfo, c, return FALSE);
  250. cinfo->Ss = c;
  251. INPUT_BYTE(cinfo, c, return FALSE);
  252. cinfo->Se = c;
  253. INPUT_BYTE(cinfo, c, return FALSE);
  254. cinfo->Ah = (c >> 4) & 15;
  255. cinfo->Al = (c ) & 15;
  256. TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
  257. cinfo->Ah, cinfo->Al);
  258. /* Prepare to scan data & restart markers */
  259. cinfo->marker->next_restart_num = 0;
  260. /* Count another SOS marker */
  261. cinfo->input_scan_number++;
  262. INPUT_SYNC(cinfo);
  263. return TRUE;
  264. }
  265. METHODDEF(boolean)
  266. get_app0 (j_decompress_ptr cinfo)
  267. /* Process an APP0 marker */
  268. {
  269. #define JFIF_LEN 14
  270. INT32 length;
  271. UINT8 b[JFIF_LEN];
  272. int buffp;
  273. INPUT_VARS(cinfo);
  274. INPUT_2BYTES(cinfo, length, return FALSE);
  275. length -= 2;
  276. /* See if a JFIF APP0 marker is present */
  277. if (length >= JFIF_LEN) {
  278. for (buffp = 0; buffp < JFIF_LEN; buffp++)
  279. INPUT_BYTE(cinfo, b[buffp], return FALSE);
  280. length -= JFIF_LEN;
  281. if (b[0]==0x4A && b[1]==0x46 && b[2]==0x49 && b[3]==0x46 && b[4]==0) {
  282. /* Found JFIF APP0 marker: check version */
  283. /* Major version must be 1, anything else signals an incompatible change.
  284. * We used to treat this as an error, but now it's a nonfatal warning...
  285. * Minor version should be 0..2, but process anyway if newer.
  286. */
  287. if (b[5] != 1)
  288. WARNMS2(cinfo, JWRN_JFIF_MAJOR, b[5], b[6]);
  289. else if (b[6] > 2)
  290. TRACEMS2(cinfo, 1, JTRC_JFIF_MINOR, b[5], b[6]);
  291. /* Save info */
  292. cinfo->saw_JFIF_marker = TRUE;
  293. cinfo->density_unit = b[7];
  294. cinfo->X_density = (b[8] << 8) + b[9];
  295. cinfo->Y_density = (b[10] << 8) + b[11];
  296. TRACEMS3(cinfo, 1, JTRC_JFIF,
  297. cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
  298. if (b[12] | b[13])
  299. TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL, b[12], b[13]);
  300. if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3))
  301. TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) length);
  302. } else {
  303. /* Start of APP0 does not match "JFIF" */
  304. TRACEMS1(cinfo, 1, JTRC_APP0, (int) length + JFIF_LEN);
  305. }
  306. } else {
  307. /* Too short to be JFIF marker */
  308. TRACEMS1(cinfo, 1, JTRC_APP0, (int) length);
  309. }
  310. INPUT_SYNC(cinfo);
  311. if (length > 0) /* skip any remaining data -- could be lots */
  312. (*cinfo->src->skip_input_data) (cinfo, (long) length);
  313. return TRUE;
  314. }
  315. METHODDEF(boolean)
  316. get_app14 (j_decompress_ptr cinfo)
  317. /* Process an APP14 marker */
  318. {
  319. #define ADOBE_LEN 12
  320. INT32 length;
  321. UINT8 b[ADOBE_LEN];
  322. int buffp;
  323. unsigned int version, flags0, flags1, transform;
  324. INPUT_VARS(cinfo);
  325. INPUT_2BYTES(cinfo, length, return FALSE);
  326. length -= 2;
  327. /* See if an Adobe APP14 marker is present */
  328. if (length >= ADOBE_LEN) {
  329. for (buffp = 0; buffp < ADOBE_LEN; buffp++)
  330. INPUT_BYTE(cinfo, b[buffp], return FALSE);
  331. length -= ADOBE_LEN;
  332. if (b[0]==0x41 && b[1]==0x64 && b[2]==0x6F && b[3]==0x62 && b[4]==0x65) {
  333. /* Found Adobe APP14 marker */
  334. version = (b[5] << 8) + b[6];
  335. flags0 = (b[7] << 8) + b[8];
  336. flags1 = (b[9] << 8) + b[10];
  337. transform = b[11];
  338. TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
  339. cinfo->saw_Adobe_marker = TRUE;
  340. cinfo->Adobe_transform = (UINT8) transform;
  341. } else {
  342. /* Start of APP14 does not match "Adobe" */
  343. TRACEMS1(cinfo, 1, JTRC_APP14, (int) length + ADOBE_LEN);
  344. }
  345. } else {
  346. /* Too short to be Adobe marker */
  347. TRACEMS1(cinfo, 1, JTRC_APP14, (int) length);
  348. }
  349. INPUT_SYNC(cinfo);
  350. if (length > 0) /* skip any remaining data -- could be lots */
  351. (*cinfo->src->skip_input_data) (cinfo, (long) length);
  352. return TRUE;
  353. }
  354. LOCAL(boolean)
  355. get_dac (j_decompress_ptr cinfo)
  356. /* Process a DAC marker */
  357. {
  358. INT32 length;
  359. int index, val;
  360. INPUT_VARS(cinfo);
  361. INPUT_2BYTES(cinfo, length, return FALSE);
  362. length -= 2;
  363. while (length > 0) {
  364. INPUT_BYTE(cinfo, index, return FALSE);
  365. INPUT_BYTE(cinfo, val, return FALSE);
  366. length -= 2;
  367. TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
  368. if (index < 0 || index >= (2*NUM_ARITH_TBLS))
  369. ERREXIT1(cinfo, JERR_DAC_INDEX, index);
  370. if (index >= NUM_ARITH_TBLS) { /* define AC table */
  371. cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
  372. } else { /* define DC table */
  373. cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
  374. cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
  375. if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
  376. ERREXIT1(cinfo, JERR_DAC_VALUE, val);
  377. }
  378. }
  379. INPUT_SYNC(cinfo);
  380. return TRUE;
  381. }
  382. LOCAL(boolean)
  383. get_dht (j_decompress_ptr cinfo)
  384. /* Process a DHT marker */
  385. {
  386. INT32 length;
  387. UINT8 bits[17];
  388. UINT8 huffval[256];
  389. int i, index, count;
  390. JHUFF_TBL **htblptr;
  391. INPUT_VARS(cinfo);
  392. INPUT_2BYTES(cinfo, length, return FALSE);
  393. length -= 2;
  394. while (length > 0) {
  395. INPUT_BYTE(cinfo, index, return FALSE);
  396. TRACEMS1(cinfo, 1, JTRC_DHT, index);
  397. bits[0] = 0;
  398. count = 0;
  399. for (i = 1; i <= 16; i++) {
  400. INPUT_BYTE(cinfo, bits[i], return FALSE);
  401. count += bits[i];
  402. }
  403. length -= 1 + 16;
  404. TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
  405. bits[1], bits[2], bits[3], bits[4],
  406. bits[5], bits[6], bits[7], bits[8]);
  407. TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
  408. bits[9], bits[10], bits[11], bits[12],
  409. bits[13], bits[14], bits[15], bits[16]);
  410. if (count > 256 || ((INT32) count) > length)
  411. ERREXIT(cinfo, JERR_DHT_COUNTS);
  412. for (i = 0; i < count; i++)
  413. INPUT_BYTE(cinfo, huffval[i], return FALSE);
  414. length -= count;
  415. if (index & 0x10) { /* AC table definition */
  416. index -= 0x10;
  417. htblptr = &cinfo->ac_huff_tbl_ptrs[index];
  418. } else { /* DC table definition */
  419. htblptr = &cinfo->dc_huff_tbl_ptrs[index];
  420. }
  421. if (index < 0 || index >= NUM_HUFF_TBLS)
  422. ERREXIT1(cinfo, JERR_DHT_INDEX, index);
  423. if (*htblptr == NULL)
  424. *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  425. MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  426. MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
  427. }
  428. INPUT_SYNC(cinfo);
  429. return TRUE;
  430. }
  431. LOCAL(boolean)
  432. get_dqt (j_decompress_ptr cinfo)
  433. /* Process a DQT marker */
  434. {
  435. INT32 length;
  436. int n, i, prec;
  437. unsigned int tmp;
  438. JQUANT_TBL *quant_ptr;
  439. INPUT_VARS(cinfo);
  440. INPUT_2BYTES(cinfo, length, return FALSE);
  441. length -= 2;
  442. while (length > 0) {
  443. INPUT_BYTE(cinfo, n, return FALSE);
  444. prec = n >> 4;
  445. n &= 0x0F;
  446. TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
  447. if (n >= NUM_QUANT_TBLS)
  448. ERREXIT1(cinfo, JERR_DQT_INDEX, n);
  449. if (cinfo->quant_tbl_ptrs[n] == NULL)
  450. cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
  451. quant_ptr = cinfo->quant_tbl_ptrs[n];
  452. for (i = 0; i < DCTSIZE2; i++) {
  453. if (prec)
  454. INPUT_2BYTES(cinfo, tmp, return FALSE);
  455. else
  456. INPUT_BYTE(cinfo, tmp, return FALSE);
  457. /* We convert the zigzag-order table to natural array order. */
  458. quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
  459. }
  460. if (cinfo->err->trace_level >= 2) {
  461. for (i = 0; i < DCTSIZE2; i += 8) {
  462. TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
  463. quant_ptr->quantval[i], quant_ptr->quantval[i+1],
  464. quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
  465. quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
  466. quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
  467. }
  468. }
  469. length -= DCTSIZE2+1;
  470. if (prec) length -= DCTSIZE2;
  471. }
  472. INPUT_SYNC(cinfo);
  473. return TRUE;
  474. }
  475. LOCAL(boolean)
  476. get_dri (j_decompress_ptr cinfo)
  477. /* Process a DRI marker */
  478. {
  479. INT32 length;
  480. unsigned int tmp;
  481. INPUT_VARS(cinfo);
  482. INPUT_2BYTES(cinfo, length, return FALSE);
  483. if (length != 4)
  484. ERREXIT(cinfo, JERR_BAD_LENGTH);
  485. INPUT_2BYTES(cinfo, tmp, return FALSE);
  486. TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
  487. cinfo->restart_interval = tmp;
  488. INPUT_SYNC(cinfo);
  489. return TRUE;
  490. }
  491. METHODDEF(boolean)
  492. skip_variable (j_decompress_ptr cinfo)
  493. /* Skip over an unknown or uninteresting variable-length marker */
  494. {
  495. INT32 length;
  496. INPUT_VARS(cinfo);
  497. INPUT_2BYTES(cinfo, length, return FALSE);
  498. TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
  499. INPUT_SYNC(cinfo); /* do before skip_input_data */
  500. (*cinfo->src->skip_input_data) (cinfo, (long) length - 2L);
  501. return TRUE;
  502. }
  503. /*
  504. * Find the next JPEG marker, save it in cinfo->unread_marker.
  505. * Returns FALSE if had to suspend before reaching a marker;
  506. * in that case cinfo->unread_marker is unchanged.
  507. *
  508. * Note that the result might not be a valid marker code,
  509. * but it will never be 0 or FF.
  510. */
  511. LOCAL(boolean)
  512. next_marker (j_decompress_ptr cinfo)
  513. {
  514. int c;
  515. INPUT_VARS(cinfo);
  516. for (;;) {
  517. INPUT_BYTE(cinfo, c, return FALSE);
  518. /* Skip any non-FF bytes.
  519. * This may look a bit inefficient, but it will not occur in a valid file.
  520. * We sync after each discarded byte so that a suspending data source
  521. * can discard the byte from its buffer.
  522. */
  523. while (c != 0xFF) {
  524. cinfo->marker->discarded_bytes++;
  525. INPUT_SYNC(cinfo);
  526. INPUT_BYTE(cinfo, c, return FALSE);
  527. }
  528. /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
  529. * pad bytes, so don't count them in discarded_bytes. We assume there
  530. * will not be so many consecutive FF bytes as to overflow a suspending
  531. * data source's input buffer.
  532. */
  533. do {
  534. INPUT_BYTE(cinfo, c, return FALSE);
  535. } while (c == 0xFF);
  536. if (c != 0)
  537. break; /* found a valid marker, exit loop */
  538. /* Reach here if we found a stuffed-zero data sequence (FF/00).
  539. * Discard it and loop back to try again.
  540. */
  541. cinfo->marker->discarded_bytes += 2;
  542. INPUT_SYNC(cinfo);
  543. }
  544. if (cinfo->marker->discarded_bytes != 0) {
  545. WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
  546. cinfo->marker->discarded_bytes = 0;
  547. }
  548. cinfo->unread_marker = c;
  549. INPUT_SYNC(cinfo);
  550. return TRUE;
  551. }
  552. LOCAL(boolean)
  553. first_marker (j_decompress_ptr cinfo)
  554. /* Like next_marker, but used to obtain the initial SOI marker. */
  555. /* For this marker, we do not allow preceding garbage or fill; otherwise,
  556. * we might well scan an entire input file before realizing it ain't JPEG.
  557. * If an application wants to process non-JFIF files, it must seek to the
  558. * SOI before calling the JPEG library.
  559. */
  560. {
  561. int c, c2;
  562. INPUT_VARS(cinfo);
  563. INPUT_BYTE(cinfo, c, return FALSE);
  564. INPUT_BYTE(cinfo, c2, return FALSE);
  565. if (c != 0xFF || c2 != (int) M_SOI)
  566. ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
  567. cinfo->unread_marker = c2;
  568. INPUT_SYNC(cinfo);
  569. return TRUE;
  570. }
  571. /*
  572. * Read markers until SOS or EOI.
  573. *
  574. * Returns same codes as are defined for jpeg_consume_input:
  575. * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  576. */
  577. METHODDEF(int)
  578. read_markers (j_decompress_ptr cinfo)
  579. {
  580. /* Outer loop repeats once for each marker. */
  581. for (;;) {
  582. /* Collect the marker proper, unless we already did. */
  583. /* NB: first_marker() enforces the requirement that SOI appear first. */
  584. if (cinfo->unread_marker == 0) {
  585. if (! cinfo->marker->saw_SOI) {
  586. if (! first_marker(cinfo))
  587. return JPEG_SUSPENDED;
  588. } else {
  589. if (! next_marker(cinfo))
  590. return JPEG_SUSPENDED;
  591. }
  592. }
  593. /* At this point cinfo->unread_marker contains the marker code and the
  594. * input point is just past the marker proper, but before any parameters.
  595. * A suspension will cause us to return with this state still true.
  596. */
  597. switch (cinfo->unread_marker) {
  598. case M_SOI:
  599. if (! get_soi(cinfo))
  600. return JPEG_SUSPENDED;
  601. break;
  602. case M_SOF0: /* Baseline */
  603. case M_SOF1: /* Extended sequential, Huffman */
  604. if (! get_sof(cinfo, FALSE, FALSE))
  605. return JPEG_SUSPENDED;
  606. break;
  607. case M_SOF2: /* Progressive, Huffman */
  608. if (! get_sof(cinfo, TRUE, FALSE))
  609. return JPEG_SUSPENDED;
  610. break;
  611. case M_SOF9: /* Extended sequential, arithmetic */
  612. if (! get_sof(cinfo, FALSE, TRUE))
  613. return JPEG_SUSPENDED;
  614. break;
  615. case M_SOF10: /* Progressive, arithmetic */
  616. if (! get_sof(cinfo, TRUE, TRUE))
  617. return JPEG_SUSPENDED;
  618. break;
  619. /* Currently unsupported SOFn types */
  620. case M_SOF3: /* Lossless, Huffman */
  621. case M_SOF5: /* Differential sequential, Huffman */
  622. case M_SOF6: /* Differential progressive, Huffman */
  623. case M_SOF7: /* Differential lossless, Huffman */
  624. case M_JPG: /* Reserved for JPEG extensions */
  625. case M_SOF11: /* Lossless, arithmetic */
  626. case M_SOF13: /* Differential sequential, arithmetic */
  627. case M_SOF14: /* Differential progressive, arithmetic */
  628. case M_SOF15: /* Differential lossless, arithmetic */
  629. ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
  630. break;
  631. case M_SOS:
  632. if (! get_sos(cinfo))
  633. return JPEG_SUSPENDED;
  634. cinfo->unread_marker = 0; /* processed the marker */
  635. return JPEG_REACHED_SOS;
  636. case M_EOI:
  637. TRACEMS(cinfo, 1, JTRC_EOI);
  638. cinfo->unread_marker = 0; /* processed the marker */
  639. return JPEG_REACHED_EOI;
  640. case M_DAC:
  641. if (! get_dac(cinfo))
  642. return JPEG_SUSPENDED;
  643. break;
  644. case M_DHT:
  645. if (! get_dht(cinfo))
  646. return JPEG_SUSPENDED;
  647. break;
  648. case M_DQT:
  649. if (! get_dqt(cinfo))
  650. return JPEG_SUSPENDED;
  651. break;
  652. case M_DRI:
  653. if (! get_dri(cinfo))
  654. return JPEG_SUSPENDED;
  655. break;
  656. case M_APP0:
  657. case M_APP1:
  658. case M_APP2:
  659. case M_APP3:
  660. case M_APP4:
  661. case M_APP5:
  662. case M_APP6:
  663. case M_APP7:
  664. case M_APP8:
  665. case M_APP9:
  666. case M_APP10:
  667. case M_APP11:
  668. case M_APP12:
  669. case M_APP13:
  670. case M_APP14:
  671. case M_APP15:
  672. if (! (*cinfo->marker->process_APPn[cinfo->unread_marker - (int) M_APP0]) (cinfo))
  673. return JPEG_SUSPENDED;
  674. break;
  675. case M_COM:
  676. if (! (*cinfo->marker->process_COM) (cinfo))
  677. return JPEG_SUSPENDED;
  678. break;
  679. case M_RST0: /* these are all parameterless */
  680. case M_RST1:
  681. case M_RST2:
  682. case M_RST3:
  683. case M_RST4:
  684. case M_RST5:
  685. case M_RST6:
  686. case M_RST7:
  687. case M_TEM:
  688. TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
  689. break;
  690. case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
  691. if (! skip_variable(cinfo))
  692. return JPEG_SUSPENDED;
  693. break;
  694. default: /* must be DHP, EXP, JPGn, or RESn */
  695. /* For now, we treat the reserved markers as fatal errors since they are
  696. * likely to be used to signal incompatible JPEG Part 3 extensions.
  697. * Once the JPEG 3 version-number marker is well defined, this code
  698. * ought to change!
  699. */
  700. ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
  701. break;
  702. }
  703. /* Successfully processed marker, so reset state variable */
  704. cinfo->unread_marker = 0;
  705. } /* end loop */
  706. }
  707. /*
  708. * Read a restart marker, which is expected to appear next in the datastream;
  709. * if the marker is not there, take appropriate recovery action.
  710. * Returns FALSE if suspension is required.
  711. *
  712. * This is called by the entropy decoder after it has read an appropriate
  713. * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder
  714. * has already read a marker from the data source. Under normal conditions
  715. * cinfo->unread_marker will be reset to 0 before returning; if not reset,
  716. * it holds a marker which the decoder will be unable to read past.
  717. */
  718. METHODDEF(boolean)
  719. read_restart_marker (j_decompress_ptr cinfo)
  720. {
  721. /* Obtain a marker unless we already did. */
  722. /* Note that next_marker will complain if it skips any data. */
  723. if (cinfo->unread_marker == 0) {
  724. if (! next_marker(cinfo))
  725. return FALSE;
  726. }
  727. if (cinfo->unread_marker ==
  728. ((int) M_RST0 + cinfo->marker->next_restart_num)) {
  729. /* Normal case --- swallow the marker and let entropy decoder continue */
  730. TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
  731. cinfo->unread_marker = 0;
  732. } else {
  733. /* Uh-oh, the restart markers have been messed up. */
  734. /* Let the data source manager determine how to resync. */
  735. if (! (*cinfo->src->resync_to_restart) (cinfo,
  736. cinfo->marker->next_restart_num))
  737. return FALSE;
  738. }
  739. /* Update next-restart state */
  740. cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
  741. return TRUE;
  742. }
  743. /*
  744. * This is the default resync_to_restart method for data source managers
  745. * to use if they don't have any better approach. Some data source managers
  746. * may be able to back up, or may have additional knowledge about the data
  747. * which permits a more intelligent recovery strategy; such managers would
  748. * presumably supply their own resync method.
  749. *
  750. * read_restart_marker calls resync_to_restart if it finds a marker other than
  751. * the restart marker it was expecting. (This code is *not* used unless
  752. * a nonzero restart interval has been declared.) cinfo->unread_marker is
  753. * the marker code actually found (might be anything, except 0 or FF).
  754. * The desired restart marker number (0..7) is passed as a parameter.
  755. * This routine is supposed to apply whatever error recovery strategy seems
  756. * appropriate in order to position the input stream to the next data segment.
  757. * Note that cinfo->unread_marker is treated as a marker appearing before
  758. * the current data-source input point; usually it should be reset to zero
  759. * before returning.
  760. * Returns FALSE if suspension is required.
  761. *
  762. * This implementation is substantially constrained by wanting to treat the
  763. * input as a data stream; this means we can't back up. Therefore, we have
  764. * only the following actions to work with:
  765. * 1. Simply discard the marker and let the entropy decoder resume at next
  766. * byte of file.
  767. * 2. Read forward until we find another marker, discarding intervening
  768. * data. (In theory we could look ahead within the current bufferload,
  769. * without having to discard data if we don't find the desired marker.
  770. * This idea is not implemented here, in part because it makes behavior
  771. * dependent on buffer size and chance buffer-boundary positions.)
  772. * 3. Leave the marker unread (by failing to zero cinfo->unread_marker).
  773. * This will cause the entropy decoder to process an empty data segment,
  774. * inserting dummy zeroes, and then we will reprocess the marker.
  775. *
  776. * #2 is appropriate if we think the desired marker lies ahead, while #3 is
  777. * appropriate if the found marker is a future restart marker (indicating
  778. * that we have missed the desired restart marker, probably because it got
  779. * corrupted).
  780. * We apply #2 or #3 if the found marker is a restart marker no more than
  781. * two counts behind or ahead of the expected one. We also apply #2 if the
  782. * found marker is not a legal JPEG marker code (it's certainly bogus data).
  783. * If the found marker is a restart marker more than 2 counts away, we do #1
  784. * (too much risk that the marker is erroneous; with luck we will be able to
  785. * resync at some future point).
  786. * For any valid non-restart JPEG marker, we apply #3. This keeps us from
  787. * overrunning the end of a scan. An implementation limited to single-scan
  788. * files might find it better to apply #2 for markers other than EOI, since
  789. * any other marker would have to be bogus data in that case.
  790. */
  791. GLOBAL(boolean)
  792. jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
  793. {
  794. int marker = cinfo->unread_marker;
  795. int action = 1;
  796. /* Always put up a warning. */
  797. WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
  798. /* Outer loop handles repeated decision after scanning forward. */
  799. for (;;) {
  800. if (marker < (int) M_SOF0)
  801. action = 2; /* invalid marker */
  802. else if (marker < (int) M_RST0 || marker > (int) M_RST7)
  803. action = 3; /* valid non-restart marker */
  804. else {
  805. if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
  806. marker == ((int) M_RST0 + ((desired+2) & 7)))
  807. action = 3; /* one of the next two expected restarts */
  808. else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
  809. marker == ((int) M_RST0 + ((desired-2) & 7)))
  810. action = 2; /* a prior restart, so advance */
  811. else
  812. action = 1; /* desired restart or too far away */
  813. }
  814. TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
  815. switch (action) {
  816. case 1:
  817. /* Discard marker and let entropy decoder resume processing. */
  818. cinfo->unread_marker = 0;
  819. return TRUE;
  820. case 2:
  821. /* Scan to the next marker, and repeat the decision loop. */
  822. if (! next_marker(cinfo))
  823. return FALSE;
  824. marker = cinfo->unread_marker;
  825. break;
  826. case 3:
  827. /* Return without advancing past this marker. */
  828. /* Entropy decoder will be forced to process an empty segment. */
  829. return TRUE;
  830. }
  831. } /* end loop */
  832. }
  833. /*
  834. * Reset marker processing state to begin a fresh datastream.
  835. */
  836. METHODDEF(void)
  837. reset_marker_reader (j_decompress_ptr cinfo)
  838. {
  839. cinfo->comp_info = NULL; /* until allocated by get_sof */
  840. cinfo->input_scan_number = 0; /* no SOS seen yet */
  841. cinfo->unread_marker = 0; /* no pending marker */
  842. cinfo->marker->saw_SOI = FALSE; /* set internal state too */
  843. cinfo->marker->saw_SOF = FALSE;
  844. cinfo->marker->discarded_bytes = 0;
  845. }
  846. /*
  847. * Initialize the marker reader module.
  848. * This is called only once, when the decompression object is created.
  849. */
  850. GLOBAL(void)
  851. jinit_marker_reader (j_decompress_ptr cinfo)
  852. {
  853. int i;
  854. /* Create subobject in permanent pool */
  855. cinfo->marker = (struct jpeg_marker_reader *)
  856. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  857. SIZEOF(struct jpeg_marker_reader));
  858. /* Initialize method pointers */
  859. cinfo->marker->reset_marker_reader = reset_marker_reader;
  860. cinfo->marker->read_markers = read_markers;
  861. cinfo->marker->read_restart_marker = read_restart_marker;
  862. cinfo->marker->process_COM = skip_variable;
  863. for (i = 0; i < 16; i++)
  864. cinfo->marker->process_APPn[i] = skip_variable;
  865. cinfo->marker->process_APPn[0] = get_app0;
  866. cinfo->marker->process_APPn[14] = get_app14;
  867. /* Reset marker processing state */
  868. reset_marker_reader(cinfo);
  869. }