Chromium Code Reviews| Index: media/filters/jpeg_parser.cc |
| diff --git a/media/filters/jpeg_parser.cc b/media/filters/jpeg_parser.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8625d98a98305c68516eb628769c4336488ccb01 |
| --- /dev/null |
| +++ b/media/filters/jpeg_parser.cc |
| @@ -0,0 +1,361 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/logging.h" |
| +#include "media/filters/jpeg_parser.h" |
|
wuchengli
2015/01/05 07:32:55
This should be moved to the first include.
http:/
kcwu
2015/01/05 08:50:50
Done.
|
| + |
| +using base::BigEndianReader; |
| + |
| +#define READ_U8_OR_RETURN_FALSE(out) \ |
| + do { \ |
| + uint8_t _out; \ |
| + if (!reader.ReadU8(&_out)) { \ |
| + DVLOG(1) \ |
| + << "Error in stream: unexpected EOS while trying to read " #out; \ |
| + return false; \ |
| + } \ |
| + *(out) = _out; \ |
| + } while (0) |
| + |
| +#define READ_U16_OR_RETURN_FALSE(out) \ |
| + do { \ |
| + uint16_t _out; \ |
| + if (!reader.ReadU16(&_out)) { \ |
| + DVLOG(1) \ |
| + << "Error in stream: unexpected EOS while trying to read " #out; \ |
| + return false; \ |
| + } \ |
| + *(out) = _out; \ |
| + } while (0) |
| + |
| +#define IN_RANGE_OR_RETURN_FALSE(val, min, max) \ |
| + do { \ |
| + if ((val) < (min) || (val) > (max)) { \ |
| + DVLOG(1) << "Error in stream: invalid value, expected " #val " to be" \ |
| + << " in range [" << (min) << ":" << (max) << "]" \ |
| + << " found " << (val) << " instead"; \ |
| + return false; \ |
| + } \ |
| + } while (0) |
| + |
| +namespace media { |
| + |
| +namespace { |
| +enum JpegMarker { |
| + SOF0 = 0xC0, // start of frame (baseline) |
| + DHT = 0xC4, // define huffman table |
| + SOI = 0xD8, // start of image |
| + SOS = 0xDA, // start of scan |
| + DQT = 0xDB, // define quantization table |
| + DRI = 0xDD, // define restart internal |
| + MARKER1 = 0xFF, // jpeg marker prefix |
| +}; |
| +} // namespace |
| + |
| +JpegParseResult::JpegParseResult() { |
| +} |
| + |
| +JpegParser::JpegParser() { |
| +} |
| +JpegParser::~JpegParser() { |
| +} |
| + |
| +bool JpegParser::ParseSOF(const char* buffer, |
| + size_t length, |
| + JpegParseResult* result) { |
| + // Spec B.2.2 Frame header syntax |
| + DCHECK(buffer); |
| + DCHECK(result); |
| + BigEndianReader reader(buffer, length); |
| + |
| + uint8_t precision; |
| + uint16_t visible_width; |
| + uint16_t visible_height; |
| + READ_U8_OR_RETURN_FALSE(&precision); |
| + READ_U16_OR_RETURN_FALSE(&visible_height); |
| + READ_U16_OR_RETURN_FALSE(&visible_width); |
| + READ_U8_OR_RETURN_FALSE(&result->num_components); |
| + result->visible_size = gfx::Size(visible_width, visible_height); |
| + |
| + if (precision != 8) { |
| + DLOG(ERROR) << "Only support 8 bit precision, not " |
|
wuchengli
2015/01/05 07:32:55
nit: s/8 bit/8-bit/
kcwu
2015/01/05 08:50:50
Done.
|
| + << static_cast<int>(precision) << " bit for baseline"; |
| + return false; |
| + } |
| + if (result->num_components >= arraysize(result->components)) { |
| + DLOG(ERROR) << "num_components=" << static_cast<int>(result->num_components) |
| + << " is not supported"; |
| + return false; |
| + } |
| + |
| + for (size_t i = 0; i < result->num_components; i++) { |
| + JpegComponent& component = result->components[i]; |
| + READ_U8_OR_RETURN_FALSE(&component.id); |
| + if (component.id > result->num_components) { |
| + DLOG(ERROR) << "component id (" << static_cast<int>(component.id) |
| + << ") should be <= num_components (" |
| + << static_cast<int>(result->num_components) << ")"; |
| + return false; |
| + } |
| + uint8_t hv; |
| + READ_U8_OR_RETURN_FALSE(&hv); |
| + component.horizontal_sampling_factor = hv / 16; |
| + component.vertical_sampling_factor = hv % 16; |
| + IN_RANGE_OR_RETURN_FALSE(component.horizontal_sampling_factor, 1, 4); |
| + IN_RANGE_OR_RETURN_FALSE(component.vertical_sampling_factor, 1, 4); |
| + READ_U8_OR_RETURN_FALSE(&component.quantization_table_selector); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool JpegParser::ParseDQT(const char* buffer, |
| + size_t length, |
| + JpegParseResult* result) { |
| + // Spec B.2.4.1 Quantization table-specification syntax |
| + DCHECK(buffer); |
| + DCHECK(result); |
| + BigEndianReader reader(buffer, length); |
| + while (reader.remaining() > 0) { |
| + uint8_t tmp; |
| + READ_U8_OR_RETURN_FALSE(&tmp); |
|
wuchengli
2015/01/05 07:32:55
Can we rename tmp to something more meaningful? RE
kcwu
2015/01/05 08:50:50
Done. Do you feel it make more sense now?
|
| + uint8_t precision = tmp / 16; |
| + uint8_t table_id = tmp % 16; |
| + IN_RANGE_OR_RETURN_FALSE(precision, 0, 1); |
| + if (precision == 1) { // 1 means 16-bit precision |
| + DLOG(ERROR) << "An 8-bit DCT-based process shall not use a 16-bit " |
| + << "precision quantization table"; |
| + return false; |
| + } |
| + if (table_id >= kJpegMaxQuantizationTableNum) { |
| + DLOG(ERROR) << "Quantization table id (" << static_cast<int>(table_id) |
| + << ") exceeded " << kJpegMaxQuantizationTableNum; |
| + return false; |
| + } |
| + |
| + if (!reader.ReadBytes(&result->q_table[table_id].value, |
| + sizeof(result->q_table[table_id].value))) |
| + return false; |
| + result->q_table[table_id].valid = true; |
| + } |
| + return true; |
| +} |
| + |
| +bool JpegParser::ParseDHT(const char* buffer, |
| + size_t length, |
| + JpegParseResult* result) { |
| + // Spec B.2.4.2 Huffman table-specification syntax |
| + DCHECK(buffer); |
| + DCHECK(result); |
| + BigEndianReader reader(buffer, length); |
| + while (reader.remaining() > 0) { |
| + uint8_t tmp; |
| + READ_U8_OR_RETURN_FALSE(&tmp); |
| + int table_class = tmp / 16; |
| + int table_id = tmp % 16; |
| + IN_RANGE_OR_RETURN_FALSE(table_class, 0, 1); |
| + if (table_id >= 2) { |
| + DLOG(ERROR) << "Table id(" << table_id |
| + << ") >= 2 is invalid for baseline profile"; |
| + return false; |
| + } |
| + |
| + JpegHuffmanTable* table; |
| + if (table_class == 1) |
| + table = &result->ac_table[table_id]; |
| + else |
| + table = &result->dc_table[table_id]; |
| + |
| + size_t count = 0; |
| + if (!reader.ReadBytes(&table->code_length, sizeof(table->code_length))) |
| + return false; |
| + for (size_t i = 0; i < arraysize(table->code_length); i++) |
| + count += table->code_length[i]; |
| + |
| + IN_RANGE_OR_RETURN_FALSE(count, 0, sizeof(table->code_value)); |
| + if (!reader.ReadBytes(&table->code_value, count)) |
| + return false; |
| + table->valid = true; |
| + } |
| + return true; |
| +} |
| + |
| +bool JpegParser::ParseDRI(const char* buffer, |
| + size_t length, |
| + JpegParseResult* result) { |
| + // Spec B.2.4.4 Restart interval definition syntax |
| + DCHECK(buffer); |
| + DCHECK(result); |
| + BigEndianReader reader(buffer, length); |
| + return reader.ReadU16(&result->restart_interval) && reader.remaining() == 0; |
| +} |
| + |
| +bool JpegParser::ParseSOS(const char* buffer, |
| + size_t length, |
| + JpegParseResult* result) { |
| + // Spec B.2.3 Scan header syntax |
| + DCHECK(buffer); |
| + DCHECK(result); |
| + BigEndianReader reader(buffer, length); |
| + READ_U8_OR_RETURN_FALSE(&result->scan.num_components); |
| + if (result->scan.num_components != result->num_components) { |
| + DLOG(ERROR) << "The number of scan components (" |
| + << static_cast<int>(result->scan.num_components) |
| + << ") mismatches the number of image components (" |
| + << static_cast<int>(result->num_components) << ")"; |
| + return false; |
| + } |
| + |
| + for (int i = 0; i < result->scan.num_components; i++) { |
| + JpegScan::Component* component = &result->scan.components[i]; |
| + READ_U8_OR_RETURN_FALSE(&component->component_selector); |
| + uint8_t tmp; |
| + READ_U8_OR_RETURN_FALSE(&tmp); |
| + component->dc_selector = tmp / 16; |
| + component->ac_selector = tmp % 16; |
| + if (component->component_selector != result->components[i].id) { |
| + DLOG(ERROR) << "component selector mismatches image component id"; |
| + return false; |
| + } |
| + if (component->dc_selector >= kJpegMaxHuffmanTableNum_baseline) { |
| + DLOG(ERROR) << "DC selector (" << static_cast<int>(component->dc_selector) |
| + << ") should be 0 or 1 for baseline mode"; |
| + return false; |
| + } |
| + if (component->ac_selector >= kJpegMaxHuffmanTableNum_baseline) { |
| + DLOG(ERROR) << "AC selector (" << static_cast<int>(component->ac_selector) |
| + << ") should be 0 or 1 for baseline mode"; |
| + return false; |
| + } |
| + } |
| + |
| + // Unused fields, only for value checking. |
| + uint8_t spectral_selection_start; |
| + uint8_t spectral_selection_end; |
| + uint8_t point_transform; |
| + READ_U8_OR_RETURN_FALSE(&spectral_selection_start); |
| + READ_U8_OR_RETURN_FALSE(&spectral_selection_end); |
| + READ_U8_OR_RETURN_FALSE(&point_transform); |
| + if (spectral_selection_start != 0 || spectral_selection_end != 63) { |
| + DLOG(ERROR) << "Spectral selection should be 0,63 for baseline mode"; |
| + return false; |
| + } |
| + if (point_transform != 0) { |
| + DLOG(ERROR) << "Point transform should be 0 for baseline mode"; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool JpegParser::ParseSOI(const char* buffer, |
| + size_t length, |
| + JpegParseResult* result) { |
| + // Spec B.2.1 High-level syntax |
| + DCHECK(buffer); |
| + DCHECK(result); |
| + BigEndianReader reader(buffer, length); |
| + uint8_t marker1; |
| + uint8_t marker2; |
| + bool has_marker_dqt = false; |
| + bool has_marker_sos = false; |
| + |
| + // Once reached SOS, all neccesary data are parsed. |
| + while (!has_marker_sos) { |
| + READ_U8_OR_RETURN_FALSE(&marker1); |
| + if (marker1 != MARKER1) |
| + return false; |
| + |
| + do { |
| + READ_U8_OR_RETURN_FALSE(&marker2); |
| + } while (marker2 == MARKER1); // skip fill bytes |
| + |
| + uint16_t size; |
| + READ_U16_OR_RETURN_FALSE(&size); |
| + if (reader.remaining() < size) { |
| + DLOG(ERROR) << "Ill-formed JPEG. Remaining size (" << reader.remaining() |
| + << ") is smaller than header specified (" << size << ")"; |
| + return false; |
| + } |
| + |
| + // The size includes the size field itself. |
| + if (size < sizeof(size)) { |
| + DLOG(ERROR) << "Ill-formed JPEG. Segment size (" << size |
| + << ") is smaller than size field (" << sizeof(size) << ")"; |
| + return false; |
| + } |
| + size -= sizeof(size); |
| + |
| + switch (marker2) { |
| + case SOF0: |
| + if (!ParseSOF(reader.ptr(), size, result)) { |
| + DLOG(ERROR) << "ParseSOF failed"; |
| + return false; |
| + } |
| + break; |
| + case DQT: |
| + if (!ParseDQT(reader.ptr(), size, result)) { |
| + DLOG(ERROR) << "ParseDQT failed"; |
| + return false; |
| + } |
| + has_marker_dqt = true; |
| + break; |
| + case DHT: |
| + if (!ParseDHT(reader.ptr(), size, result)) { |
| + DLOG(ERROR) << "ParseDHT failed"; |
| + return false; |
| + } |
| + break; |
| + case DRI: |
| + if (!ParseDRI(reader.ptr(), size, result)) { |
| + DLOG(ERROR) << "ParseDRI failed"; |
| + return false; |
| + } |
| + break; |
| + case SOS: |
| + if (!ParseSOS(reader.ptr(), size, result)) { |
| + DLOG(ERROR) << "ParseSOS failed"; |
| + return false; |
| + } |
| + has_marker_sos = true; |
| + break; |
| + default: |
| + DVLOG(4) << "unknown marker " << static_cast<int>(marker2); |
| + break; |
| + } |
| + reader.Skip(size); |
| + } |
| + |
| + if (!has_marker_dqt) { |
| + DLOG(ERROR) << "No DQT marker found"; |
| + return false; |
| + } |
| + |
| + // Scan data follows scan header immediately. |
| + result->scan.data = reinterpret_cast<const uint8_t*>(reader.ptr()); |
| + result->scan.data_size = reader.remaining(); |
| + |
| + return true; |
| +} |
| + |
| +bool JpegParser::Parse(const uint8_t* buffer, |
|
wuchengli
2015/01/05 07:32:55
Move this before ParseSOF to be consistent with th
kcwu
2015/01/05 08:50:50
Done.
|
| + size_t length, |
| + JpegParseResult* result) { |
| + DCHECK(buffer); |
| + DCHECK(result); |
| + BigEndianReader reader(reinterpret_cast<const char*>(buffer), length); |
| + memset(result, 0, sizeof(JpegParseResult)); |
| + |
| + uint8_t marker1, marker2; |
| + READ_U8_OR_RETURN_FALSE(&marker1); |
| + READ_U8_OR_RETURN_FALSE(&marker2); |
| + if (marker1 != MARKER1 || marker2 != SOI) { |
| + LOG(ERROR) << "Not a JPEG"; |
|
wuchengli
2015/01/05 07:32:55
This is not an actual error. The caller should con
kcwu
2015/01/05 08:50:50
Done.
|
| + return false; |
| + } |
| + |
| + return ParseSOI(reader.ptr(), reader.remaining(), result); |
| +} |
| + |
| +} // namespace media |