[MD settings] moving attached() code
[chromium-blink-merge.git] / media / filters / jpeg_parser.h
blob0d9e17fa544e9bc2fe3a90de13560afe46f44a03
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_FILTERS_JPEG_PARSER_H_
6 #define MEDIA_FILTERS_JPEG_PARSER_H_
8 #include <stddef.h>
9 #include <stdint.h>
10 #include "media/base/media_export.h"
12 namespace media {
14 // It's not a full featured JPEG parser implememtation. It only parses JPEG
15 // baseline sequential process. For explanations of each struct and its
16 // members, see JPEG specification at
17 // http://www.w3.org/Graphics/JPEG/itu-t81.pdf.
19 enum JpegMarker {
20 JPEG_SOF0 = 0xC0, // start of frame (baseline)
21 JPEG_SOF1 = 0xC1, // start of frame (extended sequential)
22 JPEG_SOF2 = 0xC2, // start of frame (progressive)
23 JPEG_SOF3 = 0xC3, // start of frame (lossless))
24 JPEG_DHT = 0xC4, // define huffman table
25 JPEG_SOF5 = 0xC5, // start of frame (differential, sequential)
26 JPEG_SOF6 = 0xC6, // start of frame (differential, progressive)
27 JPEG_SOF7 = 0xC7, // start of frame (differential, lossless)
28 JPEG_SOF9 = 0xC9, // start of frame (arithmetic coding, extended)
29 JPEG_SOF10 = 0xCA, // start of frame (arithmetic coding, progressive)
30 JPEG_SOF11 = 0xCB, // start of frame (arithmetic coding, lossless)
31 JPEG_SOF13 = 0xCD, // start of frame (differential, arithmetic, sequential)
32 JPEG_SOF14 = 0xCE, // start of frame (differential, arithmetic, progressive)
33 JPEG_SOF15 = 0xCF, // start of frame (differential, arithmetic, lossless)
34 JPEG_RST0 = 0xD0, // restart
35 JPEG_RST1 = 0xD1, // restart
36 JPEG_RST2 = 0xD2, // restart
37 JPEG_RST3 = 0xD3, // restart
38 JPEG_RST4 = 0xD4, // restart
39 JPEG_RST5 = 0xD5, // restart
40 JPEG_RST6 = 0xD6, // restart
41 JPEG_RST7 = 0xD7, // restart
42 JPEG_SOI = 0xD8, // start of image
43 JPEG_EOI = 0xD9, // end of image
44 JPEG_SOS = 0xDA, // start of scan
45 JPEG_DQT = 0xDB, // define quantization table
46 JPEG_DRI = 0xDD, // define restart internal
47 JPEG_MARKER_PREFIX = 0xFF, // jpeg marker prefix
50 const size_t kJpegMaxHuffmanTableNumBaseline = 2;
51 const size_t kJpegMaxComponents = 4;
52 const size_t kJpegMaxQuantizationTableNum = 4;
54 // Parsing result of JPEG DHT marker.
55 struct JpegHuffmanTable {
56 bool valid;
57 uint8_t code_length[16];
58 uint8_t code_value[256];
61 // Parsing result of JPEG DQT marker.
62 struct JpegQuantizationTable {
63 bool valid;
64 uint8_t value[64]; // baseline only supports 8 bits quantization table
67 // Parsing result of a JPEG component.
68 struct JpegComponent {
69 uint8_t id;
70 uint8_t horizontal_sampling_factor;
71 uint8_t vertical_sampling_factor;
72 uint8_t quantization_table_selector;
75 // Parsing result of a JPEG SOF marker.
76 struct JpegFrameHeader {
77 uint16_t visible_width;
78 uint16_t visible_height;
79 uint16_t coded_width;
80 uint16_t coded_height;
81 uint8_t num_components;
82 JpegComponent components[kJpegMaxComponents];
85 // Parsing result of JPEG SOS marker.
86 struct JpegScanHeader {
87 uint8_t num_components;
88 struct Component {
89 uint8_t component_selector;
90 uint8_t dc_selector;
91 uint8_t ac_selector;
92 } components[kJpegMaxComponents];
95 struct JpegParseResult {
96 JpegFrameHeader frame_header;
97 JpegHuffmanTable dc_table[kJpegMaxHuffmanTableNumBaseline];
98 JpegHuffmanTable ac_table[kJpegMaxHuffmanTableNumBaseline];
99 JpegQuantizationTable q_table[kJpegMaxQuantizationTableNum];
100 uint16_t restart_interval;
101 JpegScanHeader scan;
102 const char* data;
103 // The size of compressed data of the first image.
104 size_t data_size;
105 // The size of the first entire image including header.
106 size_t image_size;
109 // Parses JPEG picture in |buffer| with |length|. Returns true iff header is
110 // valid and JPEG baseline sequential process is present. If parsed
111 // successfully, |result| is the parsed result.
112 MEDIA_EXPORT bool ParseJpegPicture(const uint8_t* buffer,
113 size_t length,
114 JpegParseResult* result);
116 // Parses the first image of JPEG stream in |buffer| with |length|. Returns
117 // true iff header is valid and JPEG baseline sequential process is present.
118 // If parsed successfully, |result| is the parsed result.
119 MEDIA_EXPORT bool ParseJpegStream(const uint8_t* buffer,
120 size_t length,
121 JpegParseResult* result);
123 } // namespace media
125 #endif // MEDIA_FILTERS_JPEG_PARSER_H_