jpeg_parser.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef MEDIA_PARSERS_JPEG_PARSER_H_
  5. #define MEDIA_PARSERS_JPEG_PARSER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "media/parsers/media_parsers_export.h"
  9. namespace media {
  10. // It's not a full featured JPEG parser implementation. It only parses JPEG
  11. // baseline sequential process (invalid or progressive JPEGs should fail but not
  12. // crash). For explanations of each struct and its members, see JPEG
  13. // specification at http://www.w3.org/Graphics/JPEG/itu-t81.pdf.
  14. enum JpegMarker {
  15. JPEG_SOF0 = 0xC0, // start of frame (baseline)
  16. JPEG_SOF1 = 0xC1, // start of frame (extended sequential)
  17. JPEG_SOF2 = 0xC2, // start of frame (progressive)
  18. JPEG_SOF3 = 0xC3, // start of frame (lossless))
  19. JPEG_DHT = 0xC4, // define huffman table
  20. JPEG_SOF5 = 0xC5, // start of frame (differential, sequential)
  21. JPEG_SOF6 = 0xC6, // start of frame (differential, progressive)
  22. JPEG_SOF7 = 0xC7, // start of frame (differential, lossless)
  23. JPEG_SOF9 = 0xC9, // start of frame (arithmetic coding, extended)
  24. JPEG_SOF10 = 0xCA, // start of frame (arithmetic coding, progressive)
  25. JPEG_SOF11 = 0xCB, // start of frame (arithmetic coding, lossless)
  26. JPEG_SOF13 = 0xCD, // start of frame (differential, arithmetic, sequential)
  27. JPEG_SOF14 = 0xCE, // start of frame (differential, arithmetic, progressive)
  28. JPEG_SOF15 = 0xCF, // start of frame (differential, arithmetic, lossless)
  29. JPEG_RST0 = 0xD0, // restart
  30. JPEG_RST1 = 0xD1, // restart
  31. JPEG_RST2 = 0xD2, // restart
  32. JPEG_RST3 = 0xD3, // restart
  33. JPEG_RST4 = 0xD4, // restart
  34. JPEG_RST5 = 0xD5, // restart
  35. JPEG_RST6 = 0xD6, // restart
  36. JPEG_RST7 = 0xD7, // restart
  37. JPEG_SOI = 0xD8, // start of image
  38. JPEG_EOI = 0xD9, // end of image
  39. JPEG_SOS = 0xDA, // start of scan
  40. JPEG_DQT = 0xDB, // define quantization table
  41. JPEG_DRI = 0xDD, // define restart internal
  42. JPEG_APP0 = 0xE0, // start of application segment (APP0)
  43. JPEG_APP1 = 0xE1, // start of application segment (APP1)
  44. JPEG_MARKER_PREFIX = 0xFF, // jpeg marker prefix
  45. };
  46. // JPEG format uses 2 bytes to denote the size of a segment, and the size
  47. // includes the 2 bytes used for specifying it. Therefore, maximum data size
  48. // allowed is: 65535 - 2 = 65533.
  49. constexpr size_t kMaxMarkerSizeAllowed = 65533;
  50. // JPEG header only uses 2 bytes to represent width and height.
  51. constexpr int kMaxDimension = 65535;
  52. constexpr size_t kDctSize = 64;
  53. constexpr size_t kNumDcRunSizeBits = 16;
  54. constexpr size_t kNumAcRunSizeBits = 16;
  55. constexpr size_t kNumDcCodeWordsHuffVal = 12;
  56. constexpr size_t kNumAcCodeWordsHuffVal = 162;
  57. constexpr size_t kJpegDefaultHeaderSize =
  58. 67 + (kDctSize * 2) + (kNumDcRunSizeBits * 2) +
  59. (kNumDcCodeWordsHuffVal * 2) + (kNumAcRunSizeBits * 2) +
  60. (kNumAcCodeWordsHuffVal * 2);
  61. constexpr size_t kJFIFApp0Size = 16;
  62. constexpr size_t kJFIFApp1HeaderSize = 4;
  63. const size_t kJpegMaxHuffmanTableNumBaseline = 2;
  64. const size_t kJpegMaxComponents = 4;
  65. const size_t kJpegMaxQuantizationTableNum = 4;
  66. // Parsing result of JPEG DHT marker.
  67. struct JpegHuffmanTable {
  68. bool valid;
  69. uint8_t code_length[16];
  70. uint8_t code_value[162];
  71. };
  72. // K.3.3.1 "Specification of typical tables for DC difference coding"
  73. MEDIA_PARSERS_EXPORT
  74. extern const JpegHuffmanTable kDefaultDcTable[kJpegMaxHuffmanTableNumBaseline];
  75. // K.3.3.2 "Specification of typical tables for AC coefficient coding"
  76. MEDIA_PARSERS_EXPORT
  77. extern const JpegHuffmanTable kDefaultAcTable[kJpegMaxHuffmanTableNumBaseline];
  78. // Parsing result of JPEG DQT marker.
  79. struct JpegQuantizationTable {
  80. bool valid;
  81. uint8_t value[kDctSize]; // baseline only supports 8 bits quantization table
  82. };
  83. MEDIA_PARSERS_EXPORT extern const uint8_t kZigZag8x8[64];
  84. // Table K.1 Luminance quantization table
  85. // Table K.2 Chrominance quantization table
  86. MEDIA_PARSERS_EXPORT
  87. extern const JpegQuantizationTable kDefaultQuantTable[2];
  88. // Parsing result of a JPEG component.
  89. struct JpegComponent {
  90. uint8_t id;
  91. uint8_t horizontal_sampling_factor;
  92. uint8_t vertical_sampling_factor;
  93. uint8_t quantization_table_selector;
  94. };
  95. // Parsing result of a JPEG SOF marker.
  96. struct JpegFrameHeader {
  97. uint16_t visible_width;
  98. uint16_t visible_height;
  99. uint16_t coded_width;
  100. uint16_t coded_height;
  101. uint8_t num_components;
  102. JpegComponent components[kJpegMaxComponents];
  103. };
  104. // Parsing result of JPEG SOS marker.
  105. struct JpegScanHeader {
  106. uint8_t num_components;
  107. struct Component {
  108. uint8_t component_selector;
  109. uint8_t dc_selector;
  110. uint8_t ac_selector;
  111. } components[kJpegMaxComponents];
  112. };
  113. struct JpegParseResult {
  114. JpegFrameHeader frame_header;
  115. JpegHuffmanTable dc_table[kJpegMaxHuffmanTableNumBaseline];
  116. JpegHuffmanTable ac_table[kJpegMaxHuffmanTableNumBaseline];
  117. JpegQuantizationTable q_table[kJpegMaxQuantizationTableNum];
  118. uint16_t restart_interval;
  119. JpegScanHeader scan;
  120. const char* data;
  121. // The size of compressed data of the first image.
  122. size_t data_size;
  123. // The size of the first entire image including header.
  124. size_t image_size;
  125. };
  126. // Parses JPEG picture in |buffer| with |length|. Returns true iff header is
  127. // valid and JPEG baseline sequential process is present. If parsed
  128. // successfully, |result| is the parsed result.
  129. MEDIA_PARSERS_EXPORT
  130. bool ParseJpegPicture(const uint8_t* buffer,
  131. size_t length,
  132. JpegParseResult* result);
  133. // Parses the first image of JPEG stream in |buffer| with |length|. Returns
  134. // true iff header is valid and JPEG baseline sequential process is present.
  135. // If parsed successfully, |result| is the parsed result.
  136. MEDIA_PARSERS_EXPORT
  137. bool ParseJpegStream(const uint8_t* buffer,
  138. size_t length,
  139. JpegParseResult* result);
  140. } // namespace media
  141. #endif // MEDIA_PARSERS_JPEG_PARSER_H_