Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(460)

Unified Diff: content/common/gpu/media/vaapi_jpeg_parser.cc

Issue 748023002: Add JPEG parser for JPEG decode acceleration (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/common/gpu/media/vaapi_jpeg_parser.cc
diff --git a/content/common/gpu/media/vaapi_jpeg_parser.cc b/content/common/gpu/media/vaapi_jpeg_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e2be6f9612cf64c3fd39bd61390f242e4a9706d7
--- /dev/null
+++ b/content/common/gpu/media/vaapi_jpeg_parser.cc
@@ -0,0 +1,323 @@
+// 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 "content/common/gpu/media/vaapi_jpeg_parser.h"
+
+using base::BigEndianReader;
+
+namespace content {
+
+VaapiJpegParser::VaapiJpegParser(const uint8_t* buffer, size_t length)
+ : buffer_(buffer), length_(length) {
+}
+
+VaapiJpegParser::~VaapiJpegParser() {
+}
+
+bool VaapiJpegParser::ParseSOF(BigEndianReader reader) {
+ // Spec B.2.2 Frame header syntax
+ uint8_t precision;
+
+ if (!reader.ReadU8(&precision))
+ return false;
+ if (!reader.ReadU16(&result_.visible_height))
+ return false;
+ if (!reader.ReadU16(&result_.visible_width))
+ return false;
+ if (!reader.ReadU8(&result_.num_component))
+ return false;
+
+ if (precision != 8) {
+ DLOG(ERROR) << "Only support 8 bit precision, not "
+ << static_cast<int>(precision) << " bit";
+ return false;
+ }
+ if (result_.num_component != 3 ||
wuchengli 2014/12/17 09:03:02 Follow the (read) order of precesion, width/height
kcwu 2014/12/24 11:32:37 Done.
+ result_.num_component >= arraysize(result_.components)) {
+ DLOG(ERROR) << "num_component=" << static_cast<int>(result_.num_component)
+ << " is not supported";
+ return false;
+ }
+
+ // Size 64k*64k is the maximum in the JPEG standard. VAAPI doesn't support
+ // larger than
+ // 16k*16k.
wuchengli 2014/12/17 09:03:02 combine with the previous line
kcwu 2014/12/24 11:32:37 Done.
+ if (result_.visible_height > 16384 || result_.visible_width > 16384) {
+ DLOG(ERROR) << "VAAPI don't support size(" << result_.visible_width << "*"
+ << result_.visible_height << ") larger than 16k*16k";
+ return false;
+ }
+
+ for (int i = 0; i < result_.num_component; i++) {
+ JpegComponent& component = result_.components[i];
+ if (!reader.ReadU8(&component.id))
+ return false;
+ if (component.id > result_.num_component) {
+ DLOG(ERROR) << "component id (" << static_cast<int>(component.id)
+ << ") should be <= num_component ("
+ << static_cast<int>(result_.num_component) << ")";
+ return false;
+ }
+ uint8_t hv;
+ if (!reader.ReadU8(&hv))
+ return false;
+ component.horizontal_sampling_factor = hv / 16;
+ component.vertical_sampling_factor = hv % 16;
+ if (!reader.ReadU8(&component.quantization_table_selector))
+ return false;
+ }
+
+ if (result_.components[0].horizontal_sampling_factor <
+ result_.components[1].horizontal_sampling_factor ||
+ result_.components[0].horizontal_sampling_factor <
+ result_.components[2].horizontal_sampling_factor) {
+ DLOG(ERROR) << "VAAPI don't supports horizontal sampling factor of Y"
+ << " smaller than Cb and Cr";
+ return false;
+ }
+ if (result_.components[0].vertical_sampling_factor <
+ result_.components[1].vertical_sampling_factor ||
+ result_.components[0].vertical_sampling_factor <
+ result_.components[2].vertical_sampling_factor) {
+ DLOG(ERROR) << "VAAPI don't supports vertical sampling factor of Y"
+ << " smaller than Cb and Cr";
+ return false;
+ }
+
+ return true;
+}
+
+bool VaapiJpegParser::ParseDQT(BigEndianReader reader) {
+ // Spec B.2.4.1 Quantization table-specification syntax
+ while (reader.remaining() > 0) {
+ uint8_t tmp;
+ if (!reader.ReadU8(&tmp))
+ return false;
+ uint8_t precision = tmp / 16;
+ uint8_t table_id = tmp % 16;
+ if (precision != 0) {
+ DLOG(ERROR) << "16 bits quantization table is not supported";
+ return false;
+ }
+ if (table_id >= kJpegQuantizationTableNum)
wuchengli 2014/12/17 09:03:02 add a DLOG
kcwu 2014/12/24 11:32:37 Done.
+ 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 VaapiJpegParser::ParseDHT(BigEndianReader reader) {
+ // Spec B.2.4.2 Huffman table-specification syntax
+ while (reader.remaining() > 0) {
+ uint8_t t;
wuchengli 2014/12/17 09:03:02 nit: s/t/tmp/ to be consistent with ParseDQT
kcwu 2014/12/24 11:32:37 Done.
+ if (!reader.ReadU8(&t))
+ return false;
+ int table_class = t / 16;
+ int table_id = t % 16;
+ if (table_class >= 2) {
+ DLOG(ERROR) << "Invalid table class: " << table_class;
+ return false;
+ }
+ 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];
+
+ if (count > sizeof(table->code_value)) {
+ DLOG(ERROR) << "Number of code values is invalid: " << count;
+ return false;
+ }
+ if (!reader.ReadBytes(&table->code_value, count))
+ return false;
+ table->valid = true;
+ }
+ return true;
+}
+
+bool VaapiJpegParser::ParseDRI(BigEndianReader reader) {
+ // Spec B.2.4.4 Restart interval definition syntax
+ return reader.ReadU16(&result_.restart_interval) && reader.remaining() == 0;
+}
+
+bool VaapiJpegParser::ParseSOS(BigEndianReader reader) {
+ // Spec B.2.3 Scan header syntax
+ if (!reader.ReadU8(&result_.scan.num_component))
+ return false;
+ if (result_.scan.num_component != result_.num_component) {
+ DLOG(ERROR) << "The number of scan components ("
+ << static_cast<int>(result_.scan.num_component)
+ << ") mismatches the number of image components ("
+ << static_cast<int>(result_.num_component) << ")";
+ return false;
+ }
+
+ for (int i = 0; i < result_.scan.num_component; i++) {
+ JpegScan::Component* component = &result_.scan.components[i];
+ if (!reader.ReadU8(&component->component_selector))
+ return false;
+ uint8_t tmp;
+ if (!reader.ReadU8(&tmp))
+ return false;
+ 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 >= kJpegHuffmanTableNum_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 >= kJpegHuffmanTableNum_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;
+ if (!reader.ReadU8(&spectral_selection_start))
+ return false;
+ if (!reader.ReadU8(&spectral_selection_end))
+ return false;
+ if (!reader.ReadU8(&point_transform))
+ return false;
+ 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 VaapiJpegParser::ParseSOI(BigEndianReader reader) {
+ // Spec B.2.1 High-level syntax
+ 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) {
+ if (!reader.ReadU8(&marker1) || marker1 != MARKER1)
+ return false;
+
+ do {
+ if (!reader.ReadU8(&marker2))
+ return false;
+ } while (marker2 == MARKER1); // skip padding
wuchengli 2014/12/17 09:03:02 s/padding/fill bytes/ according to the spec.
kcwu 2014/12/24 11:32:37 Done.
+
+ uint16_t size;
+ if (!reader.ReadU16(&size))
+ return false;
+ if (reader.remaining() < size) {
+ DLOG(ERROR) << "Ill-formed JPEG. Remain size (" << reader.remaining()
wuchengli 2014/12/17 09:03:02 s/Remain/Remaining/
kcwu 2014/12/24 11:32:37 Done.
+ << ") 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(BigEndianReader(reader.ptr(), size))) {
+ DLOG(ERROR) << "ParseSOF failed";
+ return false;
+ }
+ break;
+ case DQT:
+ if (!ParseDQT(BigEndianReader(reader.ptr(), size))) {
+ DLOG(ERROR) << "ParseDQT failed";
+ return false;
+ }
+ has_marker_dqt = true;
+ break;
+ case DHT:
+ if (!ParseDHT(BigEndianReader(reader.ptr(), size))) {
+ DLOG(ERROR) << "ParseDHT failed";
+ return false;
+ }
+ break;
+ case DRI:
+ if (!ParseDRI(BigEndianReader(reader.ptr(), size))) {
+ DLOG(ERROR) << "ParseDRI failed";
+ return false;
+ }
+ break;
+ case SOS:
+ if (!ParseSOS(BigEndianReader(reader.ptr(), size))) {
+ 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 = reader.ptr();
+ result_.scan.data_size = reader.remaining();
+
+ return true;
+}
+
+const JpegParseResult* VaapiJpegParser::Parse() {
+ BigEndianReader reader(reinterpret_cast<const char*>(buffer_), length_);
+ memset(&result_, 0, sizeof(result_));
+
+ uint8_t marker1, marker2;
+ if (!reader.ReadU8(&marker1) || marker1 != MARKER1 ||
+ !reader.ReadU8(&marker2) || marker2 != SOI) {
+ LOG(ERROR) << "Not a JPEG";
+ return NULL;
+ }
+
+ if (ParseSOI(BigEndianReader(reader.ptr(), reader.remaining())))
+ return &result_;
+
+ return NULL;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698