jdmarker.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /*
  2. * jdmarker.c
  3. *
  4. * Copyright (C) 1991-1995, 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. * because some bozo at Hijaak couldn't read the spec.
  286. * Minor version should be 0..2, but process anyway if newer.
  287. */
  288. if (b[5] != 1)
  289. WARNMS2(cinfo, JWRN_JFIF_MAJOR, b[5], b[6]);
  290. else if (b[6] > 2)
  291. TRACEMS2(cinfo, 1, JTRC_JFIF_MINOR, b[5], b[6]);
  292. /* Save info */
  293. cinfo->saw_JFIF_marker = TRUE;
  294. cinfo->density_unit = b[7];
  295. cinfo->X_density = (b[8] << 8) + b[9];
  296. cinfo->Y_density = (b[10] << 8) + b[11];
  297. TRACEMS3(cinfo, 1, JTRC_JFIF,
  298. cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
  299. if (b[12] | b[13])
  300. TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL, b[12], b[13]);
  301. if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3))
  302. TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) length);
  303. } else {
  304. /* Start of APP0 does not match "JFIF" */
  305. TRACEMS1(cinfo, 1, JTRC_APP0, (int) length + JFIF_LEN);
  306. }
  307. } else {
  308. /* Too short to be JFIF marker */
  309. TRACEMS1(cinfo, 1, JTRC_APP0, (int) length);
  310. }
  311. INPUT_SYNC(cinfo);
  312. if (length > 0) /* skip any remaining data -- could be lots */
  313. (*cinfo->src->skip_input_data) (cinfo, (long) length);
  314. return TRUE;
  315. }
  316. METHODDEF boolean
  317. get_app14 (j_decompress_ptr cinfo)
  318. /* Process an APP14 marker */
  319. {
  320. #define ADOBE_LEN 12
  321. INT32 length;
  322. UINT8 b[ADOBE_LEN];
  323. int buffp;
  324. unsigned int version, flags0, flags1, transform;
  325. INPUT_VARS(cinfo);
  326. INPUT_2BYTES(cinfo, length, return FALSE);
  327. length -= 2;
  328. /* See if an Adobe APP14 marker is present */
  329. if (length >= ADOBE_LEN) {
  330. for (buffp = 0; buffp < ADOBE_LEN; buffp++)
  331. INPUT_BYTE(cinfo, b[buffp], return FALSE);
  332. length -= ADOBE_LEN;
  333. if (b[0]==0x41 && b[1]==0x64 && b[2]==0x6F && b[3]==0x62 && b[4]==0x65) {
  334. /* Found Adobe APP14 marker */
  335. version = (b[5] << 8) + b[6];
  336. flags0 = (b[7] << 8) + b[8];
  337. flags1 = (b[9] << 8) + b[10];
  338. transform = b[11];
  339. TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
  340. cinfo->saw_Adobe_marker = TRUE;
  341. cinfo->Adobe_transform = (UINT8) transform;
  342. } else {
  343. /* Start of APP14 does not match "Adobe" */
  344. TRACEMS1(cinfo, 1, JTRC_APP14, (int) length + ADOBE_LEN);
  345. }
  346. } else {
  347. /* Too short to be Adobe marker */
  348. TRACEMS1(cinfo, 1, JTRC_APP14, (int) length);
  349. }
  350. INPUT_SYNC(cinfo);
  351. if (length > 0) /* skip any remaining data -- could be lots */
  352. (*cinfo->src->skip_input_data) (cinfo, (long) length);
  353. return TRUE;
  354. }
  355. LOCAL boolean
  356. get_dac (j_decompress_ptr cinfo)
  357. /* Process a DAC marker */
  358. {
  359. INT32 length;
  360. int index, val;
  361. INPUT_VARS(cinfo);
  362. INPUT_2BYTES(cinfo, length, return FALSE);
  363. length -= 2;
  364. while (length > 0) {
  365. INPUT_BYTE(cinfo, index, return FALSE);
  366. INPUT_BYTE(cinfo, val, return FALSE);
  367. length -= 2;
  368. TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
  369. if (index < 0 || index >= (2*NUM_ARITH_TBLS))
  370. ERREXIT1(cinfo, JERR_DAC_INDEX, index);
  371. if (index >= NUM_ARITH_TBLS) { /* define AC table */
  372. cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
  373. } else { /* define DC table */
  374. cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
  375. cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
  376. if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
  377. ERREXIT1(cinfo, JERR_DAC_VALUE, val);
  378. }
  379. }
  380. INPUT_SYNC(cinfo);
  381. return TRUE;
  382. }
  383. LOCAL boolean
  384. get_dht (j_decompress_ptr cinfo)
  385. /* Process a DHT marker */
  386. {
  387. INT32 length;
  388. UINT8 bits[17];
  389. UINT8 huffval[256];
  390. int i, index, count;
  391. JHUFF_TBL **htblptr;
  392. INPUT_VARS(cinfo);
  393. INPUT_2BYTES(cinfo, length, return FALSE);
  394. length -= 2;
  395. while (length > 0) {
  396. INPUT_BYTE(cinfo, index, return FALSE);
  397. TRACEMS1(cinfo, 1, JTRC_DHT, index);
  398. bits[0] = 0;
  399. count = 0;
  400. for (i = 1; i <= 16; i++) {
  401. INPUT_BYTE(cinfo, bits[i], return FALSE);
  402. count += bits[i];
  403. }
  404. length -= 1 + 16;
  405. TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
  406. bits[1], bits[2], bits[3], bits[4],
  407. bits[5], bits[6], bits[7], bits[8]);
  408. TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
  409. bits[9], bits[10], bits[11], bits[12],
  410. bits[13], bits[14], bits[15], bits[16]);
  411. if (count > 256 || ((INT32) count) > length)
  412. ERREXIT(cinfo, JERR_DHT_COUNTS);
  413. for (i = 0; i < count; i++)
  414. INPUT_BYTE(cinfo, huffval[i], return FALSE);
  415. length -= count;
  416. if (index & 0x10) { /* AC table definition */
  417. index -= 0x10;
  418. htblptr = &cinfo->ac_huff_tbl_ptrs[index];
  419. } else { /* DC table definition */
  420. htblptr = &cinfo->dc_huff_tbl_ptrs[index];
  421. }
  422. if (index < 0 || index >= NUM_HUFF_TBLS)
  423. ERREXIT1(cinfo, JERR_DHT_INDEX, index);
  424. if (*htblptr == NULL)
  425. *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  426. MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  427. MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
  428. }
  429. INPUT_SYNC(cinfo);
  430. return TRUE;
  431. }
  432. LOCAL boolean
  433. get_dqt (j_decompress_ptr cinfo)
  434. /* Process a DQT marker */
  435. {
  436. INT32 length;
  437. int n, i, prec;
  438. unsigned int tmp;
  439. JQUANT_TBL *quant_ptr;
  440. INPUT_VARS(cinfo);
  441. INPUT_2BYTES(cinfo, length, return FALSE);
  442. length -= 2;
  443. while (length > 0) {
  444. INPUT_BYTE(cinfo, n, return FALSE);
  445. prec = n >> 4;
  446. n &= 0x0F;
  447. TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
  448. if (n >= NUM_QUANT_TBLS)
  449. ERREXIT1(cinfo, JERR_DQT_INDEX, n);
  450. if (cinfo->quant_tbl_ptrs[n] == NULL)
  451. cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
  452. quant_ptr = cinfo->quant_tbl_ptrs[n];
  453. for (i = 0; i < DCTSIZE2; i++) {
  454. if (prec)
  455. INPUT_2BYTES(cinfo, tmp, return FALSE);
  456. else
  457. INPUT_BYTE(cinfo, tmp, return FALSE);
  458. quant_ptr->quantval[i] = (UINT16) tmp;
  459. }
  460. for (i = 0; i < DCTSIZE2; i += 8) {
  461. TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
  462. quant_ptr->quantval[i ], quant_ptr->quantval[i+1],
  463. quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
  464. quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
  465. quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
  466. }
  467. length -= DCTSIZE2+1;
  468. if (prec) length -= DCTSIZE2;
  469. }
  470. INPUT_SYNC(cinfo);
  471. return TRUE;
  472. }
  473. LOCAL boolean
  474. get_dri (j_decompress_ptr cinfo)
  475. /* Process a DRI marker */
  476. {
  477. INT32 length;
  478. unsigned int tmp;
  479. INPUT_VARS(cinfo);
  480. INPUT_2BYTES(cinfo, length, return FALSE);
  481. if (length != 4)
  482. ERREXIT(cinfo, JERR_BAD_LENGTH);
  483. INPUT_2BYTES(cinfo, tmp, return FALSE);
  484. TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
  485. cinfo->restart_interval = tmp;
  486. INPUT_SYNC(cinfo);
  487. return TRUE;
  488. }
  489. METHODDEF boolean
  490. skip_variable (j_decompress_ptr cinfo)
  491. /* Skip over an unknown or uninteresting variable-length marker */
  492. {
  493. INT32 length;
  494. INPUT_VARS(cinfo);
  495. INPUT_2BYTES(cinfo, length, return FALSE);
  496. TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
  497. INPUT_SYNC(cinfo); /* do before skip_input_data */
  498. (*cinfo->src->skip_input_data) (cinfo, (long) length - 2L);
  499. return TRUE;
  500. }
  501. /*
  502. * Find the next JPEG marker, save it in cinfo->unread_marker.
  503. * Returns FALSE if had to suspend before reaching a marker;
  504. * in that case cinfo->unread_marker is unchanged.
  505. *
  506. * Note that the result might not be a valid marker code,
  507. * but it will never be 0 or FF.
  508. */
  509. LOCAL boolean
  510. next_marker (j_decompress_ptr cinfo)
  511. {
  512. int c;
  513. INPUT_VARS(cinfo);
  514. for (;;) {
  515. INPUT_BYTE(cinfo, c, return FALSE);
  516. /* Skip any non-FF bytes.
  517. * This may look a bit inefficient, but it will not occur in a valid file.
  518. * We sync after each discarded byte so that a suspending data source
  519. * can discard the byte from its buffer.
  520. */
  521. while (c != 0xFF) {
  522. cinfo->marker->discarded_bytes++;
  523. INPUT_SYNC(cinfo);
  524. INPUT_BYTE(cinfo, c, return FALSE);
  525. }
  526. /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
  527. * pad bytes, so don't count them in discarded_bytes. We assume there
  528. * will not be so many consecutive FF bytes as to overflow a suspending
  529. * data source's input buffer.
  530. */
  531. do {
  532. INPUT_BYTE(cinfo, c, return FALSE);
  533. } while (c == 0xFF);
  534. if (c != 0)
  535. break; /* found a valid marker, exit loop */
  536. /* Reach here if we found a stuffed-zero data sequence (FF/00).
  537. * Discard it and loop back to try again.
  538. */
  539. cinfo->marker->discarded_bytes += 2;
  540. INPUT_SYNC(cinfo);
  541. }
  542. if (cinfo->marker->discarded_bytes != 0) {
  543. WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
  544. cinfo->marker->discarded_bytes = 0;
  545. }
  546. cinfo->unread_marker = c;
  547. INPUT_SYNC(cinfo);
  548. return TRUE;
  549. }
  550. LOCAL boolean
  551. first_marker (j_decompress_ptr cinfo)
  552. /* Like next_marker, but used to obtain the initial SOI marker. */
  553. /* For this marker, we do not allow preceding garbage or fill; otherwise,
  554. * we might well scan an entire input file before realizing it ain't JPEG.
  555. * If an application wants to process non-JFIF files, it must seek to the
  556. * SOI before calling the JPEG library.
  557. */
  558. {
  559. int c, c2;
  560. INPUT_VARS(cinfo);
  561. INPUT_BYTE(cinfo, c, return FALSE);
  562. INPUT_BYTE(cinfo, c2, return FALSE);
  563. if (c != 0xFF || c2 != (int) M_SOI)
  564. ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
  565. cinfo->unread_marker = c2;
  566. INPUT_SYNC(cinfo);
  567. return TRUE;
  568. }
  569. /*
  570. * Read markers until SOS or EOI.
  571. *
  572. * Returns same codes as are defined for jpeg_consume_input:
  573. * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  574. */
  575. METHODDEF int
  576. read_markers (j_decompress_ptr cinfo)
  577. {
  578. /* Outer loop repeats once for each marker. */
  579. for (;;) {
  580. /* Collect the marker proper, unless we already did. */
  581. /* NB: first_marker() enforces the requirement that SOI appear first. */
  582. if (cinfo->unread_marker == 0) {
  583. if (! cinfo->marker->saw_SOI) {
  584. if (! first_marker(cinfo))
  585. return JPEG_SUSPENDED;
  586. } else {
  587. if (! next_marker(cinfo))
  588. return JPEG_SUSPENDED;
  589. }
  590. }
  591. /* At this point cinfo->unread_marker contains the marker code and the
  592. * input point is just past the marker proper, but before any parameters.
  593. * A suspension will cause us to return with this state still true.
  594. */
  595. switch (cinfo->unread_marker) {
  596. case M_SOI:
  597. if (! get_soi(cinfo))
  598. return JPEG_SUSPENDED;
  599. break;
  600. case M_SOF0: /* Baseline */
  601. case M_SOF1: /* Extended sequential, Huffman */
  602. if (! get_sof(cinfo, FALSE, FALSE))
  603. return JPEG_SUSPENDED;
  604. break;
  605. case M_SOF2: /* Progressive, Huffman */
  606. if (! get_sof(cinfo, TRUE, FALSE))
  607. return JPEG_SUSPENDED;
  608. break;
  609. case M_SOF9: /* Extended sequential, arithmetic */
  610. if (! get_sof(cinfo, FALSE, TRUE))
  611. return JPEG_SUSPENDED;
  612. break;
  613. case M_SOF10: /* Progressive, arithmetic */
  614. if (! get_sof(cinfo, TRUE, TRUE))
  615. return JPEG_SUSPENDED;
  616. break;
  617. /* Currently unsupported SOFn types */
  618. case M_SOF3: /* Lossless, Huffman */
  619. case M_SOF5: /* Differential sequential, Huffman */
  620. case M_SOF6: /* Differential progressive, Huffman */
  621. case M_SOF7: /* Differential lossless, Huffman */
  622. case M_JPG: /* Reserved for JPEG extensions */
  623. case M_SOF11: /* Lossless, arithmetic */
  624. case M_SOF13: /* Differential sequential, arithmetic */
  625. case M_SOF14: /* Differential progressive, arithmetic */
  626. case M_SOF15: /* Differential lossless, arithmetic */
  627. ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
  628. break;
  629. case M_SOS:
  630. if (! get_sos(cinfo))
  631. return JPEG_SUSPENDED;
  632. cinfo->unread_marker = 0; /* processed the marker */
  633. return JPEG_REACHED_SOS;
  634. case M_EOI:
  635. TRACEMS(cinfo, 1, JTRC_EOI);
  636. cinfo->unread_marker = 0; /* processed the marker */
  637. return JPEG_REACHED_EOI;
  638. case M_DAC:
  639. if (! get_dac(cinfo))
  640. return JPEG_SUSPENDED;
  641. break;
  642. case M_DHT:
  643. if (! get_dht(cinfo))
  644. return JPEG_SUSPENDED;
  645. break;
  646. case M_DQT:
  647. if (! get_dqt(cinfo))
  648. return JPEG_SUSPENDED;
  649. break;
  650. case M_DRI:
  651. if (! get_dri(cinfo))
  652. return JPEG_SUSPENDED;
  653. break;
  654. case M_APP0:
  655. case M_APP1:
  656. case M_APP2:
  657. case M_APP3:
  658. case M_APP4:
  659. case M_APP5:
  660. case M_APP6:
  661. case M_APP7:
  662. case M_APP8:
  663. case M_APP9:
  664. case M_APP10:
  665. case M_APP11:
  666. case M_APP12:
  667. case M_APP13:
  668. case M_APP14:
  669. case M_APP15:
  670. if (! (*cinfo->marker->process_APPn[cinfo->unread_marker - (int) M_APP0]) (cinfo))
  671. return JPEG_SUSPENDED;
  672. break;
  673. case M_COM:
  674. if (! (*cinfo->marker->process_COM) (cinfo))
  675. return JPEG_SUSPENDED;
  676. break;
  677. case M_RST0: /* these are all parameterless */
  678. case M_RST1:
  679. case M_RST2:
  680. case M_RST3:
  681. case M_RST4:
  682. case M_RST5:
  683. case M_RST6:
  684. case M_RST7:
  685. case M_TEM:
  686. TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
  687. break;
  688. case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
  689. if (! skip_variable(cinfo))
  690. return JPEG_SUSPENDED;
  691. break;
  692. default: /* must be DHP, EXP, JPGn, or RESn */
  693. /* For now, we treat the reserved markers as fatal errors since they are
  694. * likely to be used to signal incompatible JPEG Part 3 extensions.
  695. * Once the JPEG 3 version-number marker is well defined, this code
  696. * ought to change!
  697. */
  698. ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
  699. break;
  700. }
  701. /* Successfully processed marker, so reset state variable */
  702. cinfo->unread_marker = 0;
  703. } /* end loop */
  704. }
  705. /*
  706. * Read a restart marker, which is expected to appear next in the datastream;
  707. * if the marker is not there, take appropriate recovery action.
  708. * Returns FALSE if suspension is required.
  709. *
  710. * This is called by the entropy decoder after it has read an appropriate
  711. * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder
  712. * has already read a marker from the data source. Under normal conditions
  713. * cinfo->unread_marker will be reset to 0 before returning; if not reset,
  714. * it holds a marker which the decoder will be unable to read past.
  715. */
  716. METHODDEF boolean
  717. read_restart_marker (j_decompress_ptr cinfo)
  718. {
  719. /* Obtain a marker unless we already did. */
  720. /* Note that next_marker will complain if it skips any data. */
  721. if (cinfo->unread_marker == 0) {
  722. if (! next_marker(cinfo))
  723. return FALSE;
  724. }
  725. if (cinfo->unread_marker ==
  726. ((int) M_RST0 + cinfo->marker->next_restart_num)) {
  727. /* Normal case --- swallow the marker and let entropy decoder continue */
  728. TRACEMS1(cinfo, 2, JTRC_RST, cinfo->marker->next_restart_num);
  729. cinfo->unread_marker = 0;
  730. } else {
  731. /* Uh-oh, the restart markers have been messed up. */
  732. /* Let the data source manager determine how to resync. */
  733. if (! (*cinfo->src->resync_to_restart) (cinfo,
  734. cinfo->marker->next_restart_num))
  735. return FALSE;
  736. }
  737. /* Update next-restart state */
  738. cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
  739. return TRUE;
  740. }
  741. /*
  742. * This is the default resync_to_restart method for data source managers
  743. * to use if they don't have any better approach. Some data source managers
  744. * may be able to back up, or may have additional knowledge about the data
  745. * which permits a more intelligent recovery strategy; such managers would
  746. * presumably supply their own resync method.
  747. *
  748. * read_restart_marker calls resync_to_restart if it finds a marker other than
  749. * the restart marker it was expecting. (This code is *not* used unless
  750. * a nonzero restart interval has been declared.) cinfo->unread_marker is
  751. * the marker code actually found (might be anything, except 0 or FF).
  752. * The desired restart marker number (0..7) is passed as a parameter.
  753. * This routine is supposed to apply whatever error recovery strategy seems
  754. * appropriate in order to position the input stream to the next data segment.
  755. * Note that cinfo->unread_marker is treated as a marker appearing before
  756. * the current data-source input point; usually it should be reset to zero
  757. * before returning.
  758. * Returns FALSE if suspension is required.
  759. *
  760. * This implementation is substantially constrained by wanting to treat the
  761. * input as a data stream; this means we can't back up. Therefore, we have
  762. * only the following actions to work with:
  763. * 1. Simply discard the marker and let the entropy decoder resume at next
  764. * byte of file.
  765. * 2. Read forward until we find another marker, discarding intervening
  766. * data. (In theory we could look ahead within the current bufferload,
  767. * without having to discard data if we don't find the desired marker.
  768. * This idea is not implemented here, in part because it makes behavior
  769. * dependent on buffer size and chance buffer-boundary positions.)
  770. * 3. Leave the marker unread (by failing to zero cinfo->unread_marker).
  771. * This will cause the entropy decoder to process an empty data segment,
  772. * inserting dummy zeroes, and then we will reprocess the marker.
  773. *
  774. * #2 is appropriate if we think the desired marker lies ahead, while #3 is
  775. * appropriate if the found marker is a future restart marker (indicating
  776. * that we have missed the desired restart marker, probably because it got
  777. * corrupted).
  778. * We apply #2 or #3 if the found marker is a restart marker no more than
  779. * two counts behind or ahead of the expected one. We also apply #2 if the
  780. * found marker is not a legal JPEG marker code (it's certainly bogus data).
  781. * If the found marker is a restart marker more than 2 counts away, we do #1
  782. * (too much risk that the marker is erroneous; with luck we will be able to
  783. * resync at some future point).
  784. * For any valid non-restart JPEG marker, we apply #3. This keeps us from
  785. * overrunning the end of a scan. An implementation limited to single-scan
  786. * files might find it better to apply #2 for markers other than EOI, since
  787. * any other marker would have to be bogus data in that case.
  788. */
  789. GLOBAL boolean
  790. jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
  791. {
  792. int marker = cinfo->unread_marker;
  793. int action = 1;
  794. /* Always put up a warning. */
  795. WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
  796. /* Outer loop handles repeated decision after scanning forward. */
  797. for (;;) {
  798. if (marker < (int) M_SOF0)
  799. action = 2; /* invalid marker */
  800. else if (marker < (int) M_RST0 || marker > (int) M_RST7)
  801. action = 3; /* valid non-restart marker */
  802. else {
  803. if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
  804. marker == ((int) M_RST0 + ((desired+2) & 7)))
  805. action = 3; /* one of the next two expected restarts */
  806. else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
  807. marker == ((int) M_RST0 + ((desired-2) & 7)))
  808. action = 2; /* a prior restart, so advance */
  809. else
  810. action = 1; /* desired restart or too far away */
  811. }
  812. TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
  813. switch (action) {
  814. case 1:
  815. /* Discard marker and let entropy decoder resume processing. */
  816. cinfo->unread_marker = 0;
  817. return TRUE;
  818. case 2:
  819. /* Scan to the next marker, and repeat the decision loop. */
  820. if (! next_marker(cinfo))
  821. return FALSE;
  822. marker = cinfo->unread_marker;
  823. break;
  824. case 3:
  825. /* Return without advancing past this marker. */
  826. /* Entropy decoder will be forced to process an empty segment. */
  827. return TRUE;
  828. }
  829. } /* end loop */
  830. }
  831. /*
  832. * Reset marker processing state to begin a fresh datastream.
  833. */
  834. METHODDEF void
  835. reset_marker_reader (j_decompress_ptr cinfo)
  836. {
  837. cinfo->comp_info = NULL; /* until allocated by get_sof */
  838. cinfo->input_scan_number = 0; /* no SOS seen yet */
  839. cinfo->unread_marker = 0; /* no pending marker */
  840. cinfo->marker->saw_SOI = FALSE; /* set internal state too */
  841. cinfo->marker->saw_SOF = FALSE;
  842. cinfo->marker->discarded_bytes = 0;
  843. }
  844. /*
  845. * Initialize the marker reader module.
  846. * This is called only once, when the decompression object is created.
  847. */
  848. GLOBAL void
  849. jinit_marker_reader (j_decompress_ptr cinfo)
  850. {
  851. int i;
  852. /* Create subobject in permanent pool */
  853. cinfo->marker = (struct jpeg_marker_reader *)
  854. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  855. SIZEOF(struct jpeg_marker_reader));
  856. /* Initialize method pointers */
  857. cinfo->marker->reset_marker_reader = reset_marker_reader;
  858. cinfo->marker->read_markers = read_markers;
  859. cinfo->marker->read_restart_marker = read_restart_marker;
  860. cinfo->marker->process_COM = skip_variable;
  861. for (i = 0; i < 16; i++)
  862. cinfo->marker->process_APPn[i] = skip_variable;
  863. cinfo->marker->process_APPn[0] = get_app0;
  864. cinfo->marker->process_APPn[14] = get_app14;
  865. /* Reset marker processing state */
  866. reset_marker_reader(cinfo);
  867. }