Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 #include "media/filters/jpeg_parser.h" | |
| 6 | |
| 7 #include "base/big_endian.h" | |
| 8 #include "base/logging.h" | |
| 9 | |
| 10 using base::BigEndianReader; | |
| 11 | |
| 12 #define READ_U8_OR_RETURN_FALSE(out) \ | |
| 13 do { \ | |
| 14 uint8_t _out; \ | |
| 15 if (!reader.ReadU8(&_out)) { \ | |
| 16 DVLOG(1) \ | |
| 17 << "Error in stream: unexpected EOS while trying to read " #out; \ | |
| 18 return false; \ | |
| 19 } \ | |
| 20 *(out) = _out; \ | |
| 21 } while (0) | |
| 22 | |
| 23 #define READ_U16_OR_RETURN_FALSE(out) \ | |
| 24 do { \ | |
| 25 uint16_t _out; \ | |
| 26 if (!reader.ReadU16(&_out)) { \ | |
| 27 DVLOG(1) \ | |
| 28 << "Error in stream: unexpected EOS while trying to read " #out; \ | |
| 29 return false; \ | |
| 30 } \ | |
| 31 *(out) = _out; \ | |
| 32 } while (0) | |
| 33 | |
| 34 #define IN_RANGE_OR_RETURN_FALSE(val, min, max) \ | |
| 35 do { \ | |
| 36 if ((val) < (min) || (val) > (max)) { \ | |
| 37 DVLOG(1) << "Error in stream: invalid value, expected " #val " to be" \ | |
| 38 << " in range [" << (min) << ":" << (max) << "]" \ | |
| 39 << " found " << (val) << " instead"; \ | |
| 40 return false; \ | |
| 41 } \ | |
| 42 } while (0) | |
| 43 | |
| 44 namespace media { | |
| 45 | |
| 46 namespace { | |
| 47 enum JpegMarker { | |
| 48 SOF0 = 0xC0, // start of frame (baseline) | |
| 49 DHT = 0xC4, // define huffman table | |
| 50 SOI = 0xD8, // start of image | |
| 51 SOS = 0xDA, // start of scan | |
| 52 DQT = 0xDB, // define quantization table | |
| 53 DRI = 0xDD, // define restart internal | |
| 54 MARKER1 = 0xFF, // jpeg marker prefix | |
| 55 }; | |
| 56 } // namespace | |
| 57 | |
| 58 JpegParseResult::JpegParseResult() { | |
| 59 } | |
| 60 | |
| 61 bool JpegParser::Parse(const uint8_t* buffer, | |
| 62 size_t length, | |
| 63 JpegParseResult* result) { | |
| 64 DCHECK(buffer); | |
| 65 DCHECK(result); | |
| 66 BigEndianReader reader(reinterpret_cast<const char*>(buffer), length); | |
| 67 memset(result, 0, sizeof(JpegParseResult)); | |
| 68 | |
| 69 uint8_t marker1, marker2; | |
| 70 READ_U8_OR_RETURN_FALSE(&marker1); | |
| 71 READ_U8_OR_RETURN_FALSE(&marker2); | |
| 72 if (marker1 != MARKER1 || marker2 != SOI) { | |
| 73 DLOG(ERROR) << "Not a JPEG"; | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 return ParseSOI(reader.ptr(), reader.remaining(), result); | |
| 78 } | |
| 79 | |
| 80 bool JpegParser::ParseSOF(const char* buffer, | |
| 81 size_t length, | |
| 82 JpegParseResult* result) { | |
| 83 // Spec B.2.2 Frame header syntax | |
| 84 DCHECK(buffer); | |
| 85 DCHECK(result); | |
| 86 BigEndianReader reader(buffer, length); | |
| 87 | |
| 88 uint8_t precision; | |
| 89 uint16_t visible_width; | |
| 90 uint16_t visible_height; | |
| 91 READ_U8_OR_RETURN_FALSE(&precision); | |
| 92 READ_U16_OR_RETURN_FALSE(&visible_height); | |
| 93 READ_U16_OR_RETURN_FALSE(&visible_width); | |
| 94 READ_U8_OR_RETURN_FALSE(&result->num_components); | |
| 95 result->visible_size = gfx::Size(visible_width, visible_height); | |
|
Owen Lin
2015/01/07 05:57:37
Use SetSize() here ?
kcwu
2015/01/07 11:07:31
gfx::Size change reverted
| |
| 96 | |
| 97 if (precision != 8) { | |
| 98 DLOG(ERROR) << "Only support 8-bit precision, not " | |
| 99 << static_cast<int>(precision) << " bit for baseline"; | |
| 100 return false; | |
| 101 } | |
| 102 if (result->num_components >= arraysize(result->components)) { | |
| 103 DLOG(ERROR) << "num_components=" << static_cast<int>(result->num_components) | |
| 104 << " is not supported"; | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 for (size_t i = 0; i < result->num_components; i++) { | |
| 109 JpegComponent& component = result->components[i]; | |
| 110 READ_U8_OR_RETURN_FALSE(&component.id); | |
| 111 if (component.id > result->num_components) { | |
| 112 DLOG(ERROR) << "component id (" << static_cast<int>(component.id) | |
| 113 << ") should be <= num_components (" | |
| 114 << static_cast<int>(result->num_components) << ")"; | |
| 115 return false; | |
| 116 } | |
| 117 uint8_t hv; | |
| 118 READ_U8_OR_RETURN_FALSE(&hv); | |
| 119 component.horizontal_sampling_factor = hv / 16; | |
| 120 component.vertical_sampling_factor = hv % 16; | |
| 121 IN_RANGE_OR_RETURN_FALSE(component.horizontal_sampling_factor, 1, 4); | |
| 122 IN_RANGE_OR_RETURN_FALSE(component.vertical_sampling_factor, 1, 4); | |
| 123 READ_U8_OR_RETURN_FALSE(&component.quantization_table_selector); | |
| 124 } | |
| 125 | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 129 bool JpegParser::ParseDQT(const char* buffer, | |
| 130 size_t length, | |
| 131 JpegParseResult* result) { | |
| 132 // Spec B.2.4.1 Quantization table-specification syntax | |
| 133 DCHECK(buffer); | |
| 134 DCHECK(result); | |
| 135 BigEndianReader reader(buffer, length); | |
| 136 while (reader.remaining() > 0) { | |
| 137 uint8_t precision_and_table_id; | |
| 138 READ_U8_OR_RETURN_FALSE(&precision_and_table_id); | |
| 139 uint8_t precision = precision_and_table_id / 16; | |
| 140 uint8_t table_id = precision_and_table_id % 16; | |
| 141 IN_RANGE_OR_RETURN_FALSE(precision, 0, 1); | |
| 142 if (precision == 1) { // 1 means 16-bit precision | |
| 143 DLOG(ERROR) << "An 8-bit DCT-based process shall not use a 16-bit " | |
| 144 << "precision quantization table"; | |
| 145 return false; | |
| 146 } | |
| 147 if (table_id >= kJpegMaxQuantizationTableNum) { | |
| 148 DLOG(ERROR) << "Quantization table id (" << static_cast<int>(table_id) | |
| 149 << ") exceeded " << kJpegMaxQuantizationTableNum; | |
| 150 return false; | |
| 151 } | |
| 152 | |
| 153 if (!reader.ReadBytes(&result->q_table[table_id].value, | |
| 154 sizeof(result->q_table[table_id].value))) | |
| 155 return false; | |
| 156 result->q_table[table_id].valid = true; | |
| 157 } | |
| 158 return true; | |
| 159 } | |
| 160 | |
| 161 bool JpegParser::ParseDHT(const char* buffer, | |
| 162 size_t length, | |
| 163 JpegParseResult* result) { | |
| 164 // Spec B.2.4.2 Huffman table-specification syntax | |
| 165 DCHECK(buffer); | |
| 166 DCHECK(result); | |
| 167 BigEndianReader reader(buffer, length); | |
| 168 while (reader.remaining() > 0) { | |
| 169 uint8_t table_class_and_id; | |
| 170 READ_U8_OR_RETURN_FALSE(&table_class_and_id); | |
| 171 int table_class = table_class_and_id / 16; | |
| 172 int table_id = table_class_and_id % 16; | |
| 173 IN_RANGE_OR_RETURN_FALSE(table_class, 0, 1); | |
| 174 if (table_id >= 2) { | |
| 175 DLOG(ERROR) << "Table id(" << table_id | |
| 176 << ") >= 2 is invalid for baseline profile"; | |
| 177 return false; | |
| 178 } | |
| 179 | |
| 180 JpegHuffmanTable* table; | |
| 181 if (table_class == 1) | |
| 182 table = &result->ac_table[table_id]; | |
| 183 else | |
| 184 table = &result->dc_table[table_id]; | |
| 185 | |
| 186 size_t count = 0; | |
| 187 if (!reader.ReadBytes(&table->code_length, sizeof(table->code_length))) | |
| 188 return false; | |
| 189 for (size_t i = 0; i < arraysize(table->code_length); i++) | |
| 190 count += table->code_length[i]; | |
| 191 | |
| 192 IN_RANGE_OR_RETURN_FALSE(count, 0, sizeof(table->code_value)); | |
| 193 if (!reader.ReadBytes(&table->code_value, count)) | |
| 194 return false; | |
| 195 table->valid = true; | |
| 196 } | |
| 197 return true; | |
| 198 } | |
| 199 | |
| 200 bool JpegParser::ParseDRI(const char* buffer, | |
| 201 size_t length, | |
| 202 JpegParseResult* result) { | |
| 203 // Spec B.2.4.4 Restart interval definition syntax | |
| 204 DCHECK(buffer); | |
| 205 DCHECK(result); | |
| 206 BigEndianReader reader(buffer, length); | |
| 207 return reader.ReadU16(&result->restart_interval) && reader.remaining() == 0; | |
| 208 } | |
| 209 | |
| 210 bool JpegParser::ParseSOS(const char* buffer, | |
| 211 size_t length, | |
| 212 JpegParseResult* result) { | |
| 213 // Spec B.2.3 Scan header syntax | |
| 214 DCHECK(buffer); | |
| 215 DCHECK(result); | |
| 216 BigEndianReader reader(buffer, length); | |
| 217 READ_U8_OR_RETURN_FALSE(&result->scan.num_components); | |
| 218 if (result->scan.num_components != result->num_components) { | |
| 219 DLOG(ERROR) << "The number of scan components (" | |
| 220 << static_cast<int>(result->scan.num_components) | |
| 221 << ") mismatches the number of image components (" | |
| 222 << static_cast<int>(result->num_components) << ")"; | |
| 223 return false; | |
| 224 } | |
| 225 | |
| 226 for (int i = 0; i < result->scan.num_components; i++) { | |
| 227 JpegScan::Component* component = &result->scan.components[i]; | |
| 228 READ_U8_OR_RETURN_FALSE(&component->component_selector); | |
| 229 uint8_t dc_and_ac_selector; | |
| 230 READ_U8_OR_RETURN_FALSE(&dc_and_ac_selector); | |
| 231 component->dc_selector = dc_and_ac_selector / 16; | |
| 232 component->ac_selector = dc_and_ac_selector % 16; | |
| 233 if (component->component_selector != result->components[i].id) { | |
| 234 DLOG(ERROR) << "component selector mismatches image component id"; | |
| 235 return false; | |
| 236 } | |
| 237 if (component->dc_selector >= kJpegMaxHuffmanTableNum_baseline) { | |
| 238 DLOG(ERROR) << "DC selector (" << static_cast<int>(component->dc_selector) | |
| 239 << ") should be 0 or 1 for baseline mode"; | |
| 240 return false; | |
| 241 } | |
| 242 if (component->ac_selector >= kJpegMaxHuffmanTableNum_baseline) { | |
| 243 DLOG(ERROR) << "AC selector (" << static_cast<int>(component->ac_selector) | |
| 244 << ") should be 0 or 1 for baseline mode"; | |
| 245 return false; | |
| 246 } | |
| 247 } | |
| 248 | |
| 249 // Unused fields, only for value checking. | |
| 250 uint8_t spectral_selection_start; | |
| 251 uint8_t spectral_selection_end; | |
| 252 uint8_t point_transform; | |
| 253 READ_U8_OR_RETURN_FALSE(&spectral_selection_start); | |
| 254 READ_U8_OR_RETURN_FALSE(&spectral_selection_end); | |
| 255 READ_U8_OR_RETURN_FALSE(&point_transform); | |
| 256 if (spectral_selection_start != 0 || spectral_selection_end != 63) { | |
| 257 DLOG(ERROR) << "Spectral selection should be 0,63 for baseline mode"; | |
| 258 return false; | |
| 259 } | |
| 260 if (point_transform != 0) { | |
| 261 DLOG(ERROR) << "Point transform should be 0 for baseline mode"; | |
| 262 return false; | |
| 263 } | |
| 264 | |
| 265 return true; | |
| 266 } | |
| 267 | |
| 268 bool JpegParser::ParseSOI(const char* buffer, | |
| 269 size_t length, | |
| 270 JpegParseResult* result) { | |
| 271 // Spec B.2.1 High-level syntax | |
| 272 DCHECK(buffer); | |
| 273 DCHECK(result); | |
| 274 BigEndianReader reader(buffer, length); | |
| 275 uint8_t marker1; | |
| 276 uint8_t marker2; | |
| 277 bool has_marker_dqt = false; | |
| 278 bool has_marker_sos = false; | |
| 279 | |
| 280 // Once reached SOS, all neccesary data are parsed. | |
| 281 while (!has_marker_sos) { | |
| 282 READ_U8_OR_RETURN_FALSE(&marker1); | |
| 283 if (marker1 != MARKER1) | |
| 284 return false; | |
| 285 | |
| 286 do { | |
| 287 READ_U8_OR_RETURN_FALSE(&marker2); | |
| 288 } while (marker2 == MARKER1); // skip fill bytes | |
| 289 | |
| 290 uint16_t size; | |
| 291 READ_U16_OR_RETURN_FALSE(&size); | |
| 292 if (reader.remaining() < size) { | |
| 293 DLOG(ERROR) << "Ill-formed JPEG. Remaining size (" << reader.remaining() | |
| 294 << ") is smaller than header specified (" << size << ")"; | |
| 295 return false; | |
| 296 } | |
| 297 | |
| 298 // The size includes the size field itself. | |
| 299 if (size < sizeof(size)) { | |
| 300 DLOG(ERROR) << "Ill-formed JPEG. Segment size (" << size | |
| 301 << ") is smaller than size field (" << sizeof(size) << ")"; | |
| 302 return false; | |
| 303 } | |
| 304 size -= sizeof(size); | |
| 305 | |
| 306 switch (marker2) { | |
| 307 case SOF0: | |
| 308 if (!ParseSOF(reader.ptr(), size, result)) { | |
| 309 DLOG(ERROR) << "ParseSOF failed"; | |
| 310 return false; | |
| 311 } | |
| 312 break; | |
| 313 case DQT: | |
| 314 if (!ParseDQT(reader.ptr(), size, result)) { | |
| 315 DLOG(ERROR) << "ParseDQT failed"; | |
| 316 return false; | |
| 317 } | |
| 318 has_marker_dqt = true; | |
| 319 break; | |
| 320 case DHT: | |
| 321 if (!ParseDHT(reader.ptr(), size, result)) { | |
| 322 DLOG(ERROR) << "ParseDHT failed"; | |
| 323 return false; | |
| 324 } | |
| 325 break; | |
| 326 case DRI: | |
| 327 if (!ParseDRI(reader.ptr(), size, result)) { | |
| 328 DLOG(ERROR) << "ParseDRI failed"; | |
| 329 return false; | |
| 330 } | |
| 331 break; | |
| 332 case SOS: | |
| 333 if (!ParseSOS(reader.ptr(), size, result)) { | |
| 334 DLOG(ERROR) << "ParseSOS failed"; | |
| 335 return false; | |
| 336 } | |
| 337 has_marker_sos = true; | |
| 338 break; | |
| 339 default: | |
| 340 DVLOG(4) << "unknown marker " << static_cast<int>(marker2); | |
| 341 break; | |
| 342 } | |
| 343 reader.Skip(size); | |
| 344 } | |
| 345 | |
| 346 if (!has_marker_dqt) { | |
| 347 DLOG(ERROR) << "No DQT marker found"; | |
| 348 return false; | |
| 349 } | |
| 350 | |
| 351 // Scan data follows scan header immediately. | |
| 352 result->scan.data = reinterpret_cast<const uint8_t*>(reader.ptr()); | |
| 353 result->scan.data_size = reader.remaining(); | |
| 354 | |
| 355 return true; | |
| 356 } | |
| 357 | |
| 358 } // namespace media | |
| OLD | NEW |