jpeg_parser.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/parsers/jpeg_parser.h"
  5. #include <cstring>
  6. #include "base/big_endian.h"
  7. #include "base/check_op.h"
  8. #include "base/logging.h"
  9. using base::BigEndianReader;
  10. #define READ_U8_OR_RETURN_FALSE(out) \
  11. do { \
  12. uint8_t _out; \
  13. if (!reader.ReadU8(&_out)) { \
  14. DVLOG(1) \
  15. << "Error in stream: unexpected EOS while trying to read " #out; \
  16. return false; \
  17. } \
  18. *(out) = _out; \
  19. } while (0)
  20. #define READ_U16_OR_RETURN_FALSE(out) \
  21. do { \
  22. uint16_t _out; \
  23. if (!reader.ReadU16(&_out)) { \
  24. DVLOG(1) \
  25. << "Error in stream: unexpected EOS while trying to read " #out; \
  26. return false; \
  27. } \
  28. *(out) = _out; \
  29. } while (0)
  30. namespace media {
  31. const JpegHuffmanTable kDefaultDcTable[kJpegMaxHuffmanTableNumBaseline] = {
  32. // luminance DC coefficients
  33. {
  34. true,
  35. {0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
  36. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  37. 0x0b},
  38. },
  39. // chrominance DC coefficients
  40. {
  41. true,
  42. {0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
  43. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb},
  44. },
  45. };
  46. const JpegHuffmanTable kDefaultAcTable[kJpegMaxHuffmanTableNumBaseline] = {
  47. // luminance AC coefficients
  48. {
  49. true,
  50. {0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d},
  51. {0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
  52. 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  53. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72,
  54. 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  55. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
  56. 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  57. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75,
  58. 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  59. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3,
  60. 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  61. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
  62. 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  63. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4,
  64. 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa},
  65. },
  66. // chrominance AC coefficients
  67. {
  68. true,
  69. {0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77},
  70. {0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41,
  71. 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  72. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
  73. 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  74. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44,
  75. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  76. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74,
  77. 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  78. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a,
  79. 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  80. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  81. 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  82. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
  83. 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa},
  84. },
  85. };
  86. constexpr uint8_t kZigZag8x8[64] = {
  87. 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
  88. 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
  89. 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
  90. 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63};
  91. constexpr JpegQuantizationTable kDefaultQuantTable[2] = {
  92. // Table K.1 Luminance quantization table values.
  93. {
  94. true,
  95. {16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55,
  96. 14, 13, 16, 24, 40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62,
  97. 18, 22, 37, 56, 68, 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92,
  98. 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99},
  99. },
  100. // Table K.2 Chrominance quantization table values.
  101. {
  102. true,
  103. {17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99,
  104. 24, 26, 56, 99, 99, 99, 99, 99, 47, 66, 99, 99, 99, 99, 99, 99,
  105. 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
  106. 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99},
  107. },
  108. };
  109. static bool InRange(int value, int a, int b) {
  110. return a <= value && value <= b;
  111. }
  112. // Round up |value| to multiple of |mul|. |value| must be non-negative.
  113. // |mul| must be positive.
  114. static int RoundUp(int value, int mul) {
  115. DCHECK_GE(value, 0);
  116. DCHECK_GE(mul, 1);
  117. return (value + mul - 1) / mul * mul;
  118. }
  119. // |frame_header| is already initialized to 0 in ParseJpegPicture.
  120. static bool ParseSOF(const uint8_t* buffer,
  121. size_t length,
  122. JpegFrameHeader* frame_header) {
  123. // Spec B.2.2 Frame header syntax
  124. DCHECK(buffer);
  125. DCHECK(frame_header);
  126. BigEndianReader reader(buffer, length);
  127. uint8_t precision;
  128. READ_U8_OR_RETURN_FALSE(&precision);
  129. READ_U16_OR_RETURN_FALSE(&frame_header->visible_height);
  130. READ_U16_OR_RETURN_FALSE(&frame_header->visible_width);
  131. READ_U8_OR_RETURN_FALSE(&frame_header->num_components);
  132. if (precision != 8) {
  133. DLOG(ERROR) << "Only support 8-bit precision, not "
  134. << static_cast<int>(precision) << " bit for baseline";
  135. return false;
  136. }
  137. if (!InRange(frame_header->num_components, 1,
  138. std::size(frame_header->components))) {
  139. DLOG(ERROR) << "num_components="
  140. << static_cast<int>(frame_header->num_components)
  141. << " is not supported";
  142. return false;
  143. }
  144. int max_h_factor = 0;
  145. int max_v_factor = 0;
  146. for (size_t i = 0; i < frame_header->num_components; i++) {
  147. JpegComponent& component = frame_header->components[i];
  148. READ_U8_OR_RETURN_FALSE(&component.id);
  149. if (component.id > frame_header->num_components) {
  150. DLOG(ERROR) << "component id (" << static_cast<int>(component.id)
  151. << ") should be <= num_components ("
  152. << static_cast<int>(frame_header->num_components) << ")";
  153. return false;
  154. }
  155. uint8_t hv;
  156. READ_U8_OR_RETURN_FALSE(&hv);
  157. component.horizontal_sampling_factor = hv / 16;
  158. component.vertical_sampling_factor = hv % 16;
  159. if (component.horizontal_sampling_factor > max_h_factor)
  160. max_h_factor = component.horizontal_sampling_factor;
  161. if (component.vertical_sampling_factor > max_v_factor)
  162. max_v_factor = component.vertical_sampling_factor;
  163. if (!InRange(component.horizontal_sampling_factor, 1, 4)) {
  164. DVLOG(1) << "Invalid horizontal sampling factor "
  165. << static_cast<int>(component.horizontal_sampling_factor);
  166. return false;
  167. }
  168. if (!InRange(component.vertical_sampling_factor, 1, 4)) {
  169. DVLOG(1) << "Invalid vertical sampling factor "
  170. << static_cast<int>(component.horizontal_sampling_factor);
  171. return false;
  172. }
  173. READ_U8_OR_RETURN_FALSE(&component.quantization_table_selector);
  174. }
  175. // The size of data unit is 8*8 and the coded size should be extended
  176. // to complete minimum coded unit, MCU. See Spec A.2.
  177. frame_header->coded_width =
  178. RoundUp(frame_header->visible_width, max_h_factor * 8);
  179. frame_header->coded_height =
  180. RoundUp(frame_header->visible_height, max_v_factor * 8);
  181. return true;
  182. }
  183. // |q_table| is already initialized to 0 in ParseJpegPicture.
  184. static bool ParseDQT(const uint8_t* buffer,
  185. size_t length,
  186. JpegQuantizationTable* q_table) {
  187. // Spec B.2.4.1 Quantization table-specification syntax
  188. DCHECK(buffer);
  189. DCHECK(q_table);
  190. BigEndianReader reader(buffer, length);
  191. while (reader.remaining() > 0) {
  192. uint8_t precision_and_table_id;
  193. READ_U8_OR_RETURN_FALSE(&precision_and_table_id);
  194. uint8_t precision = precision_and_table_id / 16;
  195. uint8_t table_id = precision_and_table_id % 16;
  196. if (!InRange(precision, 0, 1)) {
  197. DVLOG(1) << "Invalid precision " << static_cast<int>(precision);
  198. return false;
  199. }
  200. if (precision == 1) { // 1 means 16-bit precision
  201. DLOG(ERROR) << "An 8-bit DCT-based process shall not use a 16-bit "
  202. << "precision quantization table";
  203. return false;
  204. }
  205. if (table_id >= kJpegMaxQuantizationTableNum) {
  206. DLOG(ERROR) << "Quantization table id (" << static_cast<int>(table_id)
  207. << ") exceeded " << kJpegMaxQuantizationTableNum;
  208. return false;
  209. }
  210. if (!reader.ReadBytes(&q_table[table_id].value,
  211. sizeof(q_table[table_id].value)))
  212. return false;
  213. q_table[table_id].valid = true;
  214. }
  215. return true;
  216. }
  217. // |dc_table| and |ac_table| are already initialized to 0 in ParseJpegPicture.
  218. static bool ParseDHT(const uint8_t* buffer,
  219. size_t length,
  220. JpegHuffmanTable* dc_table,
  221. JpegHuffmanTable* ac_table) {
  222. // Spec B.2.4.2 Huffman table-specification syntax
  223. DCHECK(buffer);
  224. DCHECK(dc_table);
  225. DCHECK(ac_table);
  226. BigEndianReader reader(buffer, length);
  227. while (reader.remaining() > 0) {
  228. uint8_t table_class_and_id;
  229. READ_U8_OR_RETURN_FALSE(&table_class_and_id);
  230. int table_class = table_class_and_id / 16;
  231. int table_id = table_class_and_id % 16;
  232. if (!InRange(table_class, 0, 1)) {
  233. DVLOG(1) << "Invalid table class " << table_class;
  234. return false;
  235. }
  236. if (table_id >= 2) {
  237. DLOG(ERROR) << "Table id(" << table_id
  238. << ") >= 2 is invalid for baseline profile";
  239. return false;
  240. }
  241. JpegHuffmanTable* table;
  242. if (table_class == 1)
  243. table = &ac_table[table_id];
  244. else
  245. table = &dc_table[table_id];
  246. size_t count = 0;
  247. if (!reader.ReadBytes(&table->code_length, sizeof(table->code_length)))
  248. return false;
  249. for (size_t i = 0; i < std::size(table->code_length); i++)
  250. count += table->code_length[i];
  251. if (!InRange(count, 0, sizeof(table->code_value))) {
  252. DVLOG(1) << "Invalid code count " << count;
  253. return false;
  254. }
  255. if (!reader.ReadBytes(&table->code_value, count))
  256. return false;
  257. table->valid = true;
  258. }
  259. return true;
  260. }
  261. static bool ParseDRI(const uint8_t* buffer,
  262. size_t length,
  263. uint16_t* restart_interval) {
  264. // Spec B.2.4.4 Restart interval definition syntax
  265. DCHECK(buffer);
  266. DCHECK(restart_interval);
  267. BigEndianReader reader(buffer, length);
  268. return reader.ReadU16(restart_interval) && reader.remaining() == 0;
  269. }
  270. // |scan| is already initialized to 0 in ParseJpegPicture.
  271. static bool ParseSOS(const uint8_t* buffer,
  272. size_t length,
  273. const JpegFrameHeader& frame_header,
  274. JpegScanHeader* scan) {
  275. // Spec B.2.3 Scan header syntax
  276. DCHECK(buffer);
  277. DCHECK(scan);
  278. BigEndianReader reader(buffer, length);
  279. READ_U8_OR_RETURN_FALSE(&scan->num_components);
  280. if (scan->num_components != frame_header.num_components) {
  281. DLOG(ERROR) << "The number of scan components ("
  282. << static_cast<int>(scan->num_components)
  283. << ") mismatches the number of image components ("
  284. << static_cast<int>(frame_header.num_components) << ")";
  285. return false;
  286. }
  287. for (int i = 0; i < scan->num_components; i++) {
  288. JpegScanHeader::Component* component = &scan->components[i];
  289. READ_U8_OR_RETURN_FALSE(&component->component_selector);
  290. uint8_t dc_and_ac_selector;
  291. READ_U8_OR_RETURN_FALSE(&dc_and_ac_selector);
  292. component->dc_selector = dc_and_ac_selector / 16;
  293. component->ac_selector = dc_and_ac_selector % 16;
  294. if (component->component_selector != frame_header.components[i].id) {
  295. DLOG(ERROR) << "component selector mismatches image component id";
  296. return false;
  297. }
  298. if (component->dc_selector >= kJpegMaxHuffmanTableNumBaseline) {
  299. DLOG(ERROR) << "DC selector (" << static_cast<int>(component->dc_selector)
  300. << ") should be 0 or 1 for baseline mode";
  301. return false;
  302. }
  303. if (component->ac_selector >= kJpegMaxHuffmanTableNumBaseline) {
  304. DLOG(ERROR) << "AC selector (" << static_cast<int>(component->ac_selector)
  305. << ") should be 0 or 1 for baseline mode";
  306. return false;
  307. }
  308. }
  309. // Unused fields, only for value checking.
  310. uint8_t spectral_selection_start;
  311. uint8_t spectral_selection_end;
  312. uint8_t point_transform;
  313. READ_U8_OR_RETURN_FALSE(&spectral_selection_start);
  314. READ_U8_OR_RETURN_FALSE(&spectral_selection_end);
  315. READ_U8_OR_RETURN_FALSE(&point_transform);
  316. if (spectral_selection_start != 0 || spectral_selection_end != 63) {
  317. DLOG(ERROR) << "Spectral selection should be 0,63 for baseline mode";
  318. return false;
  319. }
  320. if (point_transform != 0) {
  321. DLOG(ERROR) << "Point transform should be 0 for baseline mode";
  322. return false;
  323. }
  324. return true;
  325. }
  326. // |eoi_begin_ptr| will point to the beginning of the EOI marker (the FF byte)
  327. // and |eoi_end_ptr| will point to the end of image (right after the end of the
  328. // EOI marker) after search succeeds. Returns true on EOI marker found, or false
  329. // otherwise.
  330. static bool SearchEOI(const uint8_t* buffer,
  331. size_t length,
  332. const char** eoi_begin_ptr,
  333. const char** eoi_end_ptr) {
  334. DCHECK(buffer);
  335. DCHECK(eoi_begin_ptr);
  336. DCHECK(eoi_end_ptr);
  337. BigEndianReader reader(buffer, length);
  338. uint8_t marker2;
  339. while (reader.remaining() > 0) {
  340. const char* marker1_ptr = static_cast<const char*>(
  341. memchr(reader.ptr(), JPEG_MARKER_PREFIX, reader.remaining()));
  342. if (!marker1_ptr)
  343. return false;
  344. reader.Skip(marker1_ptr - reinterpret_cast<const char*>(reader.ptr()) + 1);
  345. do {
  346. READ_U8_OR_RETURN_FALSE(&marker2);
  347. } while (marker2 == JPEG_MARKER_PREFIX); // skip fill bytes
  348. switch (marker2) {
  349. // Compressed data escape.
  350. case 0x00:
  351. break;
  352. // Restart
  353. case JPEG_RST0:
  354. case JPEG_RST1:
  355. case JPEG_RST2:
  356. case JPEG_RST3:
  357. case JPEG_RST4:
  358. case JPEG_RST5:
  359. case JPEG_RST6:
  360. case JPEG_RST7:
  361. break;
  362. case JPEG_EOI:
  363. *eoi_begin_ptr = marker1_ptr;
  364. *eoi_end_ptr = reinterpret_cast<const char*>(reader.ptr());
  365. return true;
  366. default:
  367. // Skip for other markers.
  368. uint16_t size;
  369. READ_U16_OR_RETURN_FALSE(&size);
  370. if (size < sizeof(size)) {
  371. DLOG(ERROR) << "Ill-formed JPEG. Segment size (" << size
  372. << ") is smaller than size field (" << sizeof(size)
  373. << ")";
  374. return false;
  375. }
  376. size -= sizeof(size);
  377. if (!reader.Skip(size)) {
  378. DLOG(ERROR) << "Ill-formed JPEG. Remaining size ("
  379. << reader.remaining()
  380. << ") is smaller than header specified (" << size << ")";
  381. return false;
  382. }
  383. break;
  384. }
  385. }
  386. return false;
  387. }
  388. // |result| is already initialized to 0 in ParseJpegPicture.
  389. static bool ParseSOI(const uint8_t* buffer,
  390. size_t length,
  391. JpegParseResult* result) {
  392. // Spec B.2.1 High-level syntax
  393. DCHECK(buffer);
  394. DCHECK(result);
  395. BigEndianReader reader(buffer, length);
  396. uint8_t marker1;
  397. uint8_t marker2;
  398. bool has_marker_dqt = false;
  399. bool has_marker_sos = false;
  400. // Once reached SOS, all neccesary data are parsed.
  401. while (!has_marker_sos) {
  402. READ_U8_OR_RETURN_FALSE(&marker1);
  403. if (marker1 != JPEG_MARKER_PREFIX)
  404. return false;
  405. do {
  406. READ_U8_OR_RETURN_FALSE(&marker2);
  407. } while (marker2 == JPEG_MARKER_PREFIX); // skip fill bytes
  408. uint16_t size;
  409. READ_U16_OR_RETURN_FALSE(&size);
  410. // The size includes the size field itself.
  411. if (size < sizeof(size)) {
  412. DLOG(ERROR) << "Ill-formed JPEG. Segment size (" << size
  413. << ") is smaller than size field (" << sizeof(size) << ")";
  414. return false;
  415. }
  416. size -= sizeof(size);
  417. if (reader.remaining() < size) {
  418. DLOG(ERROR) << "Ill-formed JPEG. Remaining size (" << reader.remaining()
  419. << ") is smaller than header specified (" << size << ")";
  420. return false;
  421. }
  422. switch (marker2) {
  423. case JPEG_SOF0:
  424. if (!ParseSOF(reader.ptr(), size, &result->frame_header)) {
  425. DLOG(ERROR) << "ParseSOF failed";
  426. return false;
  427. }
  428. break;
  429. case JPEG_SOF1:
  430. case JPEG_SOF2:
  431. case JPEG_SOF3:
  432. case JPEG_SOF5:
  433. case JPEG_SOF6:
  434. case JPEG_SOF7:
  435. case JPEG_SOF9:
  436. case JPEG_SOF10:
  437. case JPEG_SOF11:
  438. case JPEG_SOF13:
  439. case JPEG_SOF14:
  440. case JPEG_SOF15:
  441. DLOG(ERROR) << "Only SOF0 (baseline) is supported, but got SOF"
  442. << (marker2 - JPEG_SOF0);
  443. return false;
  444. case JPEG_DQT:
  445. if (!ParseDQT(reader.ptr(), size, result->q_table)) {
  446. DLOG(ERROR) << "ParseDQT failed";
  447. return false;
  448. }
  449. has_marker_dqt = true;
  450. break;
  451. case JPEG_DHT:
  452. if (!ParseDHT(reader.ptr(), size, result->dc_table, result->ac_table)) {
  453. DLOG(ERROR) << "ParseDHT failed";
  454. return false;
  455. }
  456. break;
  457. case JPEG_DRI:
  458. if (!ParseDRI(reader.ptr(), size, &result->restart_interval)) {
  459. DLOG(ERROR) << "ParseDRI failed";
  460. return false;
  461. }
  462. break;
  463. case JPEG_SOS:
  464. if (!ParseSOS(reader.ptr(), size, result->frame_header,
  465. &result->scan)) {
  466. DLOG(ERROR) << "ParseSOS failed";
  467. return false;
  468. }
  469. has_marker_sos = true;
  470. break;
  471. default:
  472. DVLOG(4) << "unknown marker " << static_cast<int>(marker2);
  473. break;
  474. }
  475. reader.Skip(size);
  476. }
  477. if (!has_marker_dqt) {
  478. DLOG(ERROR) << "No DQT marker found";
  479. return false;
  480. }
  481. // Scan data follows scan header immediately.
  482. result->data = reinterpret_cast<const char*>(reader.ptr());
  483. result->data_size = reader.remaining();
  484. return true;
  485. }
  486. bool ParseJpegPicture(const uint8_t* buffer,
  487. size_t length,
  488. JpegParseResult* result) {
  489. DCHECK(buffer);
  490. DCHECK(result);
  491. BigEndianReader reader(buffer, length);
  492. memset(result, 0, sizeof(JpegParseResult));
  493. uint8_t marker1, marker2;
  494. READ_U8_OR_RETURN_FALSE(&marker1);
  495. READ_U8_OR_RETURN_FALSE(&marker2);
  496. if (marker1 != JPEG_MARKER_PREFIX || marker2 != JPEG_SOI) {
  497. DLOG(ERROR) << "Not a JPEG";
  498. return false;
  499. }
  500. if (!ParseSOI(reader.ptr(), reader.remaining(), result))
  501. return false;
  502. // Update the sizes: |result->data_size| should not include the EOI marker or
  503. // beyond.
  504. BigEndianReader eoi_reader(reinterpret_cast<const uint8_t*>(result->data),
  505. result->data_size);
  506. const char* eoi_begin_ptr = nullptr;
  507. const char* eoi_end_ptr = nullptr;
  508. if (!SearchEOI(eoi_reader.ptr(), eoi_reader.remaining(), &eoi_begin_ptr,
  509. &eoi_end_ptr)) {
  510. DLOG(ERROR) << "SearchEOI failed";
  511. return false;
  512. }
  513. DCHECK(eoi_begin_ptr);
  514. DCHECK(eoi_end_ptr);
  515. result->data_size = eoi_begin_ptr - result->data;
  516. result->image_size = eoi_end_ptr - reinterpret_cast<const char*>(buffer);
  517. return true;
  518. }
  519. // TODO(andrescj): this function no longer seems necessary. Fix call sites to
  520. // use ParseJpegPicture() directly.
  521. bool ParseJpegStream(const uint8_t* buffer,
  522. size_t length,
  523. JpegParseResult* result) {
  524. DCHECK(buffer);
  525. DCHECK(result);
  526. return ParseJpegPicture(buffer, length, result);
  527. }
  528. } // namespace media