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