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

Unified Diff: src/images/SkImageDecoder_libjpeg.cpp

Issue 399683007: JPEG YUV Decoding (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 5 months 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: src/images/SkImageDecoder_libjpeg.cpp
diff --git a/src/images/SkImageDecoder_libjpeg.cpp b/src/images/SkImageDecoder_libjpeg.cpp
index 9b937162b3e153c33dac0d26423bcd7ad1ae4ce3..9d533d9061dcfb5a3ac8aef52778f8d7db482c65 100644
--- a/src/images/SkImageDecoder_libjpeg.cpp
+++ b/src/images/SkImageDecoder_libjpeg.cpp
@@ -102,6 +102,14 @@ static void initialize_info(jpeg_decompress_struct* cinfo, skjpeg_source_mgr* sr
}
}
+static void compute_uv_size(const jpeg_decompress_struct& info, int& width, int& height)
+{
+ int h = info.cur_comp_info[0]->h_samp_factor;
+ int v = info.cur_comp_info[0]->v_samp_factor;
+ width = (info.output_width + h - 1) / h;
+ height = (info.output_height + v - 1) / v;
+}
+
#ifdef SK_BUILD_FOR_ANDROID
class SkJPEGImageIndex {
public:
@@ -217,13 +225,17 @@ private:
class SkJPEGImageDecoder : public SkImageDecoder {
public:
-#ifdef SK_BUILD_FOR_ANDROID
SkJPEGImageDecoder() {
+#ifdef SK_BUILD_FOR_ANDROID
fImageIndex = NULL;
fImageWidth = 0;
fImageHeight = 0;
+#endif
+ fYUVBuffers[0] = fYUVBuffers[1] = fYUVBuffers[2] = NULL;
+ fRowBytes[0] = fRowBytes[1] = fRowBytes[2] = 0;
}
+#ifdef SK_BUILD_FOR_ANDROID
virtual ~SkJPEGImageDecoder() {
SkDELETE(fImageIndex);
}
@@ -239,6 +251,8 @@ protected:
virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRIDE;
#endif
virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
+ virtual void onSetYUVBuffers(void* yuv[3], size_t rowBytes[3]) SK_OVERRIDE;
+ virtual bool onGetComponentSizes(SkStream* stream, SkISize sizes[3]) SK_OVERRIDE;
private:
#ifdef SK_BUILD_FOR_ANDROID
@@ -246,6 +260,42 @@ private:
int fImageWidth;
int fImageHeight;
#endif
+ uint8_t* fYUVBuffers[3];
+ size_t fRowBytes[3];
+
+ bool isSupportedYUVFormat(jpeg_decompress_struct& cinfo) const {
+ if ((cinfo.jpeg_color_space == JCS_YCbCr) &&
+ (cinfo.num_components == 3) &&
+ (cinfo.scale_denom <= 8) &&
+ (cinfo.cur_comp_info[1]->h_samp_factor == 1) &&
+ (cinfo.cur_comp_info[1]->v_samp_factor == 1) &&
+ (cinfo.cur_comp_info[2]->h_samp_factor == 1) &&
+ (cinfo.cur_comp_info[2]->v_samp_factor == 1)) {
+ int h = cinfo.cur_comp_info[0]->h_samp_factor;
+ int v = cinfo.cur_comp_info[0]->v_samp_factor;
+ if (((h == 1) || (h == 2) || (h == 4)) &&
+ ((v == 1) || (v == 2))) {
+ cinfo.out_color_space = JCS_YCbCr;
+ cinfo.raw_data_out = TRUE;
+
+#ifdef SK_PRINT_YUV_FORMAT
+ SkDebugf("YUV 4:%d:%d\n", 4 / h, (v == 1) ? 4 / h : 0);
+#endif
+ return true;
+ }
+ }
+ return false;
+ }
+
+ bool canDecodeToYUV(jpeg_decompress_struct& cinfo) const {
+ return ((NULL != fYUVBuffers[0]) &&
+ (NULL != fYUVBuffers[1]) &&
+ (NULL != fYUVBuffers[2]) &&
+ (fRowBytes[0] > 0) &&
+ (fRowBytes[1] > 0) &&
+ (fRowBytes[2] > 0) &&
+ isSupportedYUVFormat(cinfo));
+ }
/**
* Determine the appropriate bitmap colortype and out_color_space based on
@@ -255,6 +305,11 @@ private:
*/
SkColorType getBitmapColorType(jpeg_decompress_struct*);
+ bool decodeToBitmap(SkBitmap* bm,
+ jpeg_decompress_struct& cinfo,
+ SkScaledBitmapSampler& sampler);
+ bool decodeToYUV(jpeg_decompress_struct& cinfo);
+
typedef SkImageDecoder INHERITED;
};
@@ -325,12 +380,14 @@ static bool skip_src_rows_tile(jpeg_decompress_struct* cinfo,
// This guy exists just to aid in debugging, as it allows debuggers to just
// set a break-point in one place to see all error exists.
static bool return_false(const jpeg_decompress_struct& cinfo,
- const SkBitmap& bm, const char caller[]) {
+ const SkBitmap* bm, const char caller[]) {
if (!(c_suppressJPEGImageDecoderErrors)) {
char buffer[JMSG_LENGTH_MAX];
cinfo.err->format_message((const j_common_ptr)&cinfo, buffer);
SkDebugf("libjpeg error %d <%s> from %s [%d %d]\n",
- cinfo.err->msg_code, buffer, caller, bm.width(), bm.height());
+ cinfo.err->msg_code, buffer, caller,
+ (NULL != bm) ? bm->width() : 0,
+ (NULL != bm) ? bm->height() : 0);
}
return false; // must always return false
}
@@ -545,7 +602,7 @@ bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
// All objects need to be instantiated before this setjmp call so that
// they will be cleaned up properly if an error occurs.
if (setjmp(errorManager.fJmpBuf)) {
- return return_false(cinfo, *bm, "setjmp");
+ return return_false(cinfo, bm, "setjmp");
}
initialize_info(&cinfo, &srcManager);
@@ -553,7 +610,7 @@ bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
int status = jpeg_read_header(&cinfo, true);
if (status != JPEG_HEADER_OK) {
- return return_false(cinfo, *bm, "read_header");
+ return return_false(cinfo, bm, "read_header");
}
/* Try to fulfill the requested sampleSize. Since jpeg can do it (when it
@@ -608,7 +665,7 @@ bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
return bm->setInfo(SkImageInfo::Make(smpl.scaledWidth(), smpl.scaledHeight(),
colorType, alphaType));
} else {
- return return_false(cinfo, *bm, "start_decompress");
+ return return_false(cinfo, bm, "start_decompress");
}
}
sampleSize = recompute_sampleSize(sampleSize, cinfo);
@@ -616,7 +673,7 @@ bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
// should we allow the Chooser (if present) to pick a colortype for us???
if (!this->chooseFromOneChoice(colorType, cinfo.output_width, cinfo.output_height)) {
- return return_false(cinfo, *bm, "chooseFromOneChoice");
+ return return_false(cinfo, bm, "chooseFromOneChoice");
}
#endif
@@ -630,8 +687,15 @@ bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
if (SkImageDecoder::kDecodeBounds_Mode == mode) {
return true;
}
+
+ return canDecodeToYUV(cinfo) ? decodeToYUV(cinfo) : decodeToBitmap(bm, cinfo, sampler);
+}
+
+bool SkJPEGImageDecoder::decodeToBitmap(SkBitmap* bm,
+ jpeg_decompress_struct& cinfo,
+ SkScaledBitmapSampler& sampler) {
if (!this->allocPixelRef(bm, NULL)) {
- return return_false(cinfo, *bm, "allocPixelRef");
+ return return_false(cinfo, bm, "allocPixelRef");
}
SkAutoLockPixels alp(*bm);
@@ -657,7 +721,7 @@ bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
break; // Skip to jpeg_finish_decompress()
}
if (this->shouldCancelDecode()) {
- return return_false(cinfo, *bm, "shouldCancelDecode");
+ return return_false(cinfo, bm, "shouldCancelDecode");
}
rowptr += bpr;
}
@@ -671,11 +735,11 @@ bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
int srcBytesPerPixel;
if (!get_src_config(cinfo, &sc, &srcBytesPerPixel)) {
- return return_false(cinfo, *bm, "jpeg colorspace");
+ return return_false(cinfo, bm, "jpeg colorspace");
}
if (!sampler.begin(bm, sc, *this)) {
- return return_false(cinfo, *bm, "sampler.begin");
+ return return_false(cinfo, bm, "sampler.begin");
}
SkAutoMalloc srcStorage(cinfo.output_width * srcBytesPerPixel);
@@ -683,7 +747,7 @@ bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
// Possibly skip initial rows [sampler.srcY0]
if (!skip_src_rows(&cinfo, srcRow, sampler.srcY0())) {
- return return_false(cinfo, *bm, "skip rows");
+ return return_false(cinfo, bm, "skip rows");
}
// now loop through scanlines until y == bm->height() - 1
@@ -698,7 +762,7 @@ bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
break; // Skip to jpeg_finish_decompress()
}
if (this->shouldCancelDecode()) {
- return return_false(cinfo, *bm, "shouldCancelDecode");
+ return return_false(cinfo, bm, "shouldCancelDecode");
}
if (JCS_CMYK == cinfo.out_color_space) {
@@ -712,20 +776,136 @@ bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
}
if (!skip_src_rows(&cinfo, srcRow, sampler.srcDY() - 1)) {
- return return_false(cinfo, *bm, "skip rows");
+ return return_false(cinfo, bm, "skip rows");
}
}
// we formally skip the rest, so we don't get a complaint from libjpeg
if (!skip_src_rows(&cinfo, srcRow,
cinfo.output_height - cinfo.output_scanline)) {
- return return_false(cinfo, *bm, "skip rows");
+ return return_false(cinfo, bm, "skip rows");
}
jpeg_finish_decompress(&cinfo);
return true;
}
+bool SkJPEGImageDecoder::decodeToYUV(jpeg_decompress_struct& cinfo) {
+ uint8_t* bufferraw[3];
+ uint8_t* bufferraw2[32];
+ bufferraw[0] = (uint8_t*)&bufferraw2[0];
+ bufferraw[1] = (uint8_t*)&bufferraw2[16];
+ bufferraw[2] = (uint8_t*)&bufferraw2[24];
+ int yWidth = cinfo.output_width;
+ int yHeight = cinfo.output_height;
+ int yMaxH = yHeight - 1;
+ int v = cinfo.cur_comp_info[0]->v_samp_factor;
+ int uvWidth(0), uvHeight(0);
+ compute_uv_size(cinfo, uvWidth, uvHeight);
+ int uvMaxH = uvHeight - 1;
+
+ SkAutoMalloc lastRowStorage(yWidth * 8);
+ uint8_t* yLastRow = (uint8_t*)lastRowStorage.get();
+ uint8_t* uLastRow = yLastRow + 2 * yWidth;
+ uint8_t* vLastRow = uLastRow + 2 * yWidth;
+ uint8_t* dummyRow = vLastRow + 2 * yWidth;
+
+ int scanlinesToRead = DCTSIZE * v;
+ while (cinfo.output_scanline < cinfo.output_height) {
+ // Request 8 or 16 scanlines: returns 0 or more scanlines.
+ bool hasYLastRow(false), hasUVLastRow(false);
+ for (int i = 0; i < scanlinesToRead; ++i) {
+ int scanline = (cinfo.output_scanline + i);
+ if (scanline < yMaxH) {
+ bufferraw2[i] = &fYUVBuffers[0][scanline * fRowBytes[0]];
+ } else if (scanline == yMaxH) {
+ bufferraw2[i] = yLastRow;
+ hasYLastRow = true;
+ } else {
+ bufferraw2[i] = dummyRow;
+ }
+ }
+ int scaledScanline = cinfo.output_scanline / v;
+ for (int i = 0; i < 8; ++i) {
+ int scanline = (scaledScanline + i);
+ if (scanline < uvMaxH) {
+ bufferraw2[16+i] = &fYUVBuffers[1][scanline * fRowBytes[1]];
+ bufferraw2[24+i] = &fYUVBuffers[2][scanline * fRowBytes[2]];
+ } else if (scanline == uvMaxH) {
+ bufferraw2[16+i] = uLastRow;
+ bufferraw2[24+i] = vLastRow;
+ hasUVLastRow = true;
+ } else {
+ bufferraw2[16+i] = dummyRow;
+ bufferraw2[24+i] = dummyRow;
+ }
+ }
+ JDIMENSION scanlinesRead =
+ jpeg_read_raw_data(&cinfo, (JSAMPIMAGE)bufferraw, scanlinesToRead);
+ if (scanlinesRead == 0) {
+ return false;
+ }
+ if (hasYLastRow) {
+ memcpy(&fYUVBuffers[0][ yMaxH * yWidth], yLastRow, yWidth);
+ }
+ if (hasUVLastRow) {
+ memcpy(&fYUVBuffers[1][uvMaxH * uvWidth], uLastRow, uvWidth);
+ memcpy(&fYUVBuffers[2][uvMaxH * uvWidth], vLastRow, uvWidth);
+ }
+ }
+
+ if (cinfo.output_scanline > cinfo.output_height) {
+ cinfo.output_scanline = cinfo.output_height;
+ }
+
+ jpeg_finish_decompress(&cinfo);
+
+ return true;
+}
+
+void SkJPEGImageDecoder::onSetYUVBuffers(void* yuv[3], size_t rowBytes[3]) {
+ for (int i = 0; i < 3; ++i) {
+ fYUVBuffers[i] = (uint8_t*)yuv[i];
+ fRowBytes[i] = rowBytes[i];
+ }
+}
+
+bool SkJPEGImageDecoder::onGetComponentSizes(SkStream* stream, SkISize sizes[3]) {
+ JPEGAutoClean autoClean;
+
+ jpeg_decompress_struct cinfo;
+ skjpeg_source_mgr srcManager(stream, this);
+
+ skjpeg_error_mgr errorManager;
+ set_error_mgr(&cinfo, &errorManager);
+
+ // All objects need to be instantiated before this setjmp call so that
+ // they will be cleaned up properly if an error occurs.
+ if (setjmp(errorManager.fJmpBuf)) {
+ return return_false(cinfo, NULL, "setjmp");
+ }
+
+ initialize_info(&cinfo, &srcManager);
+ autoClean.set(&cinfo);
+
+ int status = jpeg_read_header(&cinfo, true);
+ if (status != JPEG_HEADER_OK) {
+ return return_false(cinfo, NULL, "read_header");
+ }
+
+ if (isSupportedYUVFormat(cinfo)) {
+ jpeg_start_decompress(&cinfo); // Compute output width
+ sizes[0].set(cinfo.output_width, cinfo.output_height);
+ int w(0), h(0);
+ compute_uv_size(cinfo, w, h);
+ sizes[1].set(w, h);
+ sizes[2].set(w, h);
+ jpeg_abort_decompress(&cinfo);
+ return true;
+ }
+ return false;
+}
+
#ifdef SK_BUILD_FOR_ANDROID
bool SkJPEGImageDecoder::onBuildTileIndex(SkStreamRewindable* stream, int *width, int *height) {
@@ -849,11 +1029,11 @@ bool SkJPEGImageDecoder::onDecodeSubset(SkBitmap* bm, const SkIRect& region) {
((startY - rect.y()) / actualSampleSize == 0);
if (swapOnly) {
if (!this->allocPixelRef(&bitmap, NULL)) {
- return return_false(*cinfo, bitmap, "allocPixelRef");
+ return return_false(*cinfo, &bitmap, "allocPixelRef");
}
} else {
if (!bitmap.allocPixels()) {
- return return_false(*cinfo, bitmap, "allocPixels");
+ return return_false(*cinfo, &bitmap, "allocPixels");
}
}
@@ -879,10 +1059,10 @@ bool SkJPEGImageDecoder::onDecodeSubset(SkBitmap* bm, const SkIRect& region) {
// onDecodeSubset() relies on onBuildTileIndex(), which
// needs a complete image to succeed.
if (0 == rowCount) {
- return return_false(*cinfo, bitmap, "read_scanlines");
+ return return_false(*cinfo, &bitmap, "read_scanlines");
}
if (this->shouldCancelDecode()) {
- return return_false(*cinfo, bitmap, "shouldCancelDecode");
+ return return_false(*cinfo, &bitmap, "shouldCancelDecode");
}
rowTotalCount += rowCount;
rowptr += bpr;
@@ -903,11 +1083,11 @@ bool SkJPEGImageDecoder::onDecodeSubset(SkBitmap* bm, const SkIRect& region) {
int srcBytesPerPixel;
if (!get_src_config(*cinfo, &sc, &srcBytesPerPixel)) {
- return return_false(*cinfo, *bm, "jpeg colorspace");
+ return return_false(*cinfo, bm, "jpeg colorspace");
}
if (!sampler.begin(&bitmap, sc, *this)) {
- return return_false(*cinfo, bitmap, "sampler.begin");
+ return return_false(*cinfo, &bitmap, "sampler.begin");
}
SkAutoMalloc srcStorage(width * srcBytesPerPixel);
@@ -915,7 +1095,7 @@ bool SkJPEGImageDecoder::onDecodeSubset(SkBitmap* bm, const SkIRect& region) {
// Possibly skip initial rows [sampler.srcY0]
if (!skip_src_rows_tile(cinfo, fImageIndex->huffmanIndex(), srcRow, sampler.srcY0())) {
- return return_false(*cinfo, bitmap, "skip rows");
+ return return_false(*cinfo, &bitmap, "skip rows");
}
// now loop through scanlines until y == bitmap->height() - 1
@@ -926,10 +1106,10 @@ bool SkJPEGImageDecoder::onDecodeSubset(SkBitmap* bm, const SkIRect& region) {
// onDecodeSubset() relies on onBuildTileIndex(), which
// needs a complete image to succeed.
if (0 == row_count) {
- return return_false(*cinfo, bitmap, "read_scanlines");
+ return return_false(*cinfo, &bitmap, "read_scanlines");
}
if (this->shouldCancelDecode()) {
- return return_false(*cinfo, bitmap, "shouldCancelDecode");
+ return return_false(*cinfo, &bitmap, "shouldCancelDecode");
}
if (JCS_CMYK == cinfo->out_color_space) {
@@ -944,7 +1124,7 @@ bool SkJPEGImageDecoder::onDecodeSubset(SkBitmap* bm, const SkIRect& region) {
if (!skip_src_rows_tile(cinfo, fImageIndex->huffmanIndex(), srcRow,
sampler.srcDY() - 1)) {
- return return_false(*cinfo, bitmap, "skip rows");
+ return return_false(*cinfo, &bitmap, "skip rows");
}
}
if (swapOnly) {

Powered by Google App Engine
This is Rietveld 408576698