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

Side by Side Diff: src/images/SkImageDecoder_libjpeg.cpp

Issue 399683007: JPEG YUV Decoding (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Update from blink's version of YUV decoding Created 6 years, 2 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2007 The Android Open Source Project 2 * Copyright 2007 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 8
9 #include "SkImageDecoder.h" 9 #include "SkImageDecoder.h"
10 #include "SkImageEncoder.h" 10 #include "SkImageEncoder.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 SK_CONF_DECLARE(bool, c_suppressJPEGImageDecoderWarnings, 47 SK_CONF_DECLARE(bool, c_suppressJPEGImageDecoderWarnings,
48 "images.jpeg.suppressDecoderWarnings", 48 "images.jpeg.suppressDecoderWarnings",
49 DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_WARNINGS, 49 DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_WARNINGS,
50 "Suppress most JPG warnings when calling decode functions."); 50 "Suppress most JPG warnings when calling decode functions.");
51 SK_CONF_DECLARE(bool, c_suppressJPEGImageDecoderErrors, 51 SK_CONF_DECLARE(bool, c_suppressJPEGImageDecoderErrors,
52 "images.jpeg.suppressDecoderErrors", 52 "images.jpeg.suppressDecoderErrors",
53 DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_ERRORS, 53 DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_ERRORS,
54 "Suppress most JPG error messages when decode " 54 "Suppress most JPG error messages when decode "
55 "function fails."); 55 "function fails.");
56 56
57 // Enum for YUV decoding
58 enum YUVSubsampling {
59 kUNKNOWN_YUVSubsampling,
60 k410_YUVSubsampling,
61 k411_YUVSubsampling,
62 k420_YUVSubsampling,
63 k422_YUVSubsampling,
64 k440_YUVSubsampling,
65 k444_YUVSubsampling
66 };
67
68 enum SizeType {
69 kSizeForMemoryAllocation_SizeType,
70 kActualSize_SizeType
71 };
72
57 ////////////////////////////////////////////////////////////////////////// 73 //////////////////////////////////////////////////////////////////////////
58 ////////////////////////////////////////////////////////////////////////// 74 //////////////////////////////////////////////////////////////////////////
59 75
60 static void overwrite_mem_buffer_size(jpeg_decompress_struct* cinfo) { 76 static void overwrite_mem_buffer_size(jpeg_decompress_struct* cinfo) {
61 #ifdef SK_BUILD_FOR_ANDROID 77 #ifdef SK_BUILD_FOR_ANDROID
62 /* Check if the device indicates that it has a large amount of system memory 78 /* Check if the device indicates that it has a large amount of system memory
63 * if so, increase the memory allocation to 30MB instead of the default 5MB. 79 * if so, increase the memory allocation to 30MB instead of the default 5MB.
64 */ 80 */
65 #ifdef ANDROID_LARGE_MEMORY_DEVICE 81 #ifdef ANDROID_LARGE_MEMORY_DEVICE
66 cinfo->mem->max_memory_to_use = 30 * 1024 * 1024; 82 cinfo->mem->max_memory_to_use = 30 * 1024 * 1024;
(...skipping 28 matching lines...) Expand all
95 } 111 }
96 /* To suppress error messages with a SK_DEBUG binary, set the 112 /* To suppress error messages with a SK_DEBUG binary, set the
97 * environment variable "skia_images_jpeg_suppressDecoderErrors" 113 * environment variable "skia_images_jpeg_suppressDecoderErrors"
98 * to "true". Inside a program that links to skia: 114 * to "true". Inside a program that links to skia:
99 * SK_CONF_SET("images.jpeg.suppressDecoderErrors", true); */ 115 * SK_CONF_SET("images.jpeg.suppressDecoderErrors", true); */
100 if (c_suppressJPEGImageDecoderErrors) { 116 if (c_suppressJPEGImageDecoderErrors) {
101 cinfo->err->output_message = &do_nothing_output_message; 117 cinfo->err->output_message = &do_nothing_output_message;
102 } 118 }
103 } 119 }
104 120
121 static SkISize compute_yuv_size(const jpeg_decompress_struct& info, int componen t,
122 SizeType sizeType)
123 {
124 if (sizeType == kSizeForMemoryAllocation_SizeType) {
125 return SkISize::Make(info.cur_comp_info[component]->width_in_blocks * DC TSIZE,
126 info.cur_comp_info[component]->height_in_blocks * D CTSIZE);
127 }
128 return SkISize::Make(info.cur_comp_info[component]->downsampled_width,
129 info.cur_comp_info[component]->downsampled_height);
130 }
131
132 static YUVSubsampling yuv_subsampling(const jpeg_decompress_struct& info)
133 {
134 if ((DCTSIZE == 8)
135 && (info.num_components == 3)
136 && (info.comps_in_scan >= info.num_components)
137 && (info.scale_denom <= 8)
138 && (info.cur_comp_info[0])
139 && (info.cur_comp_info[1])
140 && (info.cur_comp_info[2])
141 && (info.cur_comp_info[1]->h_samp_factor == 1)
142 && (info.cur_comp_info[1]->v_samp_factor == 1)
143 && (info.cur_comp_info[2]->h_samp_factor == 1)
144 && (info.cur_comp_info[2]->v_samp_factor == 1)) {
145 int h = info.cur_comp_info[0]->h_samp_factor;
146 int v = info.cur_comp_info[0]->v_samp_factor;
147 // 4:4:4 : (h == 1) && (v == 1)
148 // 4:4:0 : (h == 1) && (v == 2)
149 // 4:2:2 : (h == 2) && (v == 1)
150 // 4:2:0 : (h == 2) && (v == 2)
151 // 4:1:1 : (h == 4) && (v == 1)
152 // 4:1:0 : (h == 4) && (v == 2)
153 if (v == 1) {
154 switch (h) {
155 case 1:
156 return k444_YUVSubsampling;
157 case 2:
158 return k422_YUVSubsampling;
159 case 4:
160 return k411_YUVSubsampling;
161 default:
162 break;
163 }
164 } else if (v == 2) {
165 switch (h) {
166 case 1:
167 return k440_YUVSubsampling;
168 case 2:
169 return k420_YUVSubsampling;
170 case 4:
171 return k410_YUVSubsampling;
172 default:
173 break;
174 }
175 }
176 }
177
178 return kUNKNOWN_YUVSubsampling;
179 }
180
105 #ifdef SK_BUILD_FOR_ANDROID 181 #ifdef SK_BUILD_FOR_ANDROID
106 class SkJPEGImageIndex { 182 class SkJPEGImageIndex {
107 public: 183 public:
108 SkJPEGImageIndex(SkStreamRewindable* stream, SkImageDecoder* decoder) 184 SkJPEGImageIndex(SkStreamRewindable* stream, SkImageDecoder* decoder)
109 : fSrcMgr(stream, decoder) 185 : fSrcMgr(stream, decoder)
110 , fInfoInitialized(false) 186 , fInfoInitialized(false)
111 , fHuffmanCreated(false) 187 , fHuffmanCreated(false)
112 , fDecompressStarted(false) 188 , fDecompressStarted(false)
113 { 189 {
114 SkDEBUGCODE(fReadHeaderSucceeded = false;) 190 SkDEBUGCODE(fReadHeaderSucceeded = false;)
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 virtual Format getFormat() const { 308 virtual Format getFormat() const {
233 return kJPEG_Format; 309 return kJPEG_Format;
234 } 310 }
235 311
236 protected: 312 protected:
237 #ifdef SK_BUILD_FOR_ANDROID 313 #ifdef SK_BUILD_FOR_ANDROID
238 virtual bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *h eight) SK_OVERRIDE; 314 virtual bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *h eight) SK_OVERRIDE;
239 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRI DE; 315 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRI DE;
240 #endif 316 #endif
241 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; 317 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
318 virtual bool onDecodeToYUV(SkStream* stream, SkISize componentSizes[3],
319 SkImagePlanes* imagePlanes) SK_OVERRIDE;
242 320
243 private: 321 private:
244 #ifdef SK_BUILD_FOR_ANDROID 322 #ifdef SK_BUILD_FOR_ANDROID
245 SkJPEGImageIndex* fImageIndex; 323 SkJPEGImageIndex* fImageIndex;
246 int fImageWidth; 324 int fImageWidth;
247 int fImageHeight; 325 int fImageHeight;
248 #endif 326 #endif
249 327
250 /** 328 /**
251 * Determine the appropriate bitmap colortype and out_color_space based on 329 * Determine the appropriate bitmap colortype and out_color_space based on
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 return false; 396 return false;
319 } 397 }
320 } 398 }
321 return true; 399 return true;
322 } 400 }
323 #endif 401 #endif
324 402
325 // This guy exists just to aid in debugging, as it allows debuggers to just 403 // This guy exists just to aid in debugging, as it allows debuggers to just
326 // set a break-point in one place to see all error exists. 404 // set a break-point in one place to see all error exists.
327 static bool return_false(const jpeg_decompress_struct& cinfo, 405 static bool return_false(const jpeg_decompress_struct& cinfo,
328 const SkBitmap& bm, const char caller[]) { 406 int width, int height, const char caller[]) {
329 if (!(c_suppressJPEGImageDecoderErrors)) { 407 if (!(c_suppressJPEGImageDecoderErrors)) {
330 char buffer[JMSG_LENGTH_MAX]; 408 char buffer[JMSG_LENGTH_MAX];
331 cinfo.err->format_message((const j_common_ptr)&cinfo, buffer); 409 cinfo.err->format_message((const j_common_ptr)&cinfo, buffer);
332 SkDebugf("libjpeg error %d <%s> from %s [%d %d]\n", 410 SkDebugf("libjpeg error %d <%s> from %s [%d %d]\n",
333 cinfo.err->msg_code, buffer, caller, bm.width(), bm.height()); 411 cinfo.err->msg_code, buffer, caller, width, height);
334 } 412 }
335 return false; // must always return false 413 return false; // must always return false
336 } 414 }
337 415
416 static bool return_false(const jpeg_decompress_struct& cinfo,
417 const SkBitmap& bm, const char caller[]) {
418 return return_false(cinfo, bm.width(), bm.height(), caller);
419 }
420
338 // Convert a scanline of CMYK samples to RGBX in place. Note that this 421 // Convert a scanline of CMYK samples to RGBX in place. Note that this
339 // method moves the "scanline" pointer in its processing 422 // method moves the "scanline" pointer in its processing
340 static void convert_CMYK_to_RGB(uint8_t* scanline, unsigned int width) { 423 static void convert_CMYK_to_RGB(uint8_t* scanline, unsigned int width) {
341 // At this point we've received CMYK pixels from libjpeg. We 424 // At this point we've received CMYK pixels from libjpeg. We
342 // perform a crude conversion to RGB (based on the formulae 425 // perform a crude conversion to RGB (based on the formulae
343 // from easyrgb.com): 426 // from easyrgb.com):
344 // CMYK -> CMY 427 // CMYK -> CMY
345 // C = ( C * (1 - K) + K ) // for each CMY component 428 // C = ( C * (1 - K) + K ) // for each CMY component
346 // CMY -> RGB 429 // CMY -> RGB
347 // R = ( 1 - C ) * 255 // for each RGB component 430 // R = ( 1 - C ) * 255 // for each RGB component
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 // we formally skip the rest, so we don't get a complaint from libjpeg 802 // we formally skip the rest, so we don't get a complaint from libjpeg
720 if (!skip_src_rows(&cinfo, srcRow, 803 if (!skip_src_rows(&cinfo, srcRow,
721 cinfo.output_height - cinfo.output_scanline)) { 804 cinfo.output_height - cinfo.output_scanline)) {
722 return return_false(cinfo, *bm, "skip rows"); 805 return return_false(cinfo, *bm, "skip rows");
723 } 806 }
724 jpeg_finish_decompress(&cinfo); 807 jpeg_finish_decompress(&cinfo);
725 808
726 return true; 809 return true;
727 } 810 }
728 811
812 static void update_components_sizes(const jpeg_decompress_struct& cinfo, SkISize componentSizes[3],
813 SizeType sizeType) {
814 for (int i = 0; i < 3; ++i) {
815 componentSizes[i] = compute_yuv_size(cinfo, i, sizeType);
816 }
817 }
818
819 static bool output_raw_data(jpeg_decompress_struct& cinfo, SkImagePlanes* imageP lanes) {
820 // U size and V size have to be the same if we're calling output_raw_dat a()
821 SkISize uvSize = compute_yuv_size(cinfo, 1, kSizeForMemoryAllocation_Siz eType);
822 SkASSERT(uvSize == compute_yuv_size(cinfo, 2, kSizeForMemoryAllocation_SizeT ype));
823
824 JSAMPARRAY bufferraw[3];
825 JSAMPROW bufferraw2[32];
826 bufferraw[0] = &bufferraw2[0]; // Y channel rows (8 or 16)
827 bufferraw[1] = &bufferraw2[16]; // U channel rows (8)
828 bufferraw[2] = &bufferraw2[24]; // V channel rows (8)
829 int yWidth = cinfo.output_width;
830 int yHeight = cinfo.output_height;
831 int yMaxH = yHeight - 1;
832 int v = cinfo.cur_comp_info[0]->v_samp_factor;
833 int uvMaxH = uvSize.height() - 1;
834 JSAMPROW outputY = static_cast<JSAMPROW>(imagePlanes->plane(0));
835 JSAMPROW outputU = static_cast<JSAMPROW>(imagePlanes->plane(1));
836 JSAMPROW outputV = static_cast<JSAMPROW>(imagePlanes->plane(2));
837 size_t rowBytesY = imagePlanes->rowBytes(0);
838 size_t rowBytesU = imagePlanes->rowBytes(1);
839 size_t rowBytesV = imagePlanes->rowBytes(2);
840
841 int yScanlinesToRead = DCTSIZE * v;
842 SkAutoMalloc lastRowStorage(yWidth * 8);
843 JSAMPROW yLastRow = (JSAMPROW)lastRowStorage.get();
844 JSAMPROW uLastRow = yLastRow + 2 * yWidth;
845 JSAMPROW vLastRow = uLastRow + 2 * yWidth;
846 JSAMPROW dummyRow = vLastRow + 2 * yWidth;
847
848 while (cinfo.output_scanline < cinfo.output_height) {
849 // Request 8 or 16 scanlines: returns 0 or more scanlines.
850 bool hasYLastRow(false), hasUVLastRow(false);
851 // Assign 8 or 16 rows of memory to read the Y channel.
852 for (int i = 0; i < yScanlinesToRead; ++i) {
853 int scanline = (cinfo.output_scanline + i);
854 if (scanline < yMaxH) {
855 bufferraw2[i] = &outputY[scanline * rowBytesY];
856 } else if (scanline == yMaxH) {
857 bufferraw2[i] = yLastRow;
858 hasYLastRow = true;
859 } else {
860 bufferraw2[i] = dummyRow;
861 }
862 }
863 int scaledScanline = cinfo.output_scanline / v;
864 // Assign 8 rows of memory to read the U and V channels.
865 for (int i = 0; i < 8; ++i) {
866 int scanline = (scaledScanline + i);
867 if (scanline < uvMaxH) {
868 bufferraw2[16 + i] = &outputU[scanline * rowBytesU];
869 bufferraw2[24 + i] = &outputV[scanline * rowBytesV];
870 } else if (scanline == uvMaxH) {
871 bufferraw2[16 + i] = uLastRow;
872 bufferraw2[24 + i] = vLastRow;
873 hasUVLastRow = true;
874 } else {
875 bufferraw2[16 + i] = dummyRow;
876 bufferraw2[24 + i] = dummyRow;
877 }
878 }
879 JDIMENSION scanlinesRead = jpeg_read_raw_data(&cinfo, bufferraw, yScanli nesToRead);
880
881 if (scanlinesRead == 0)
882 return false;
883
884 if (hasYLastRow) {
885 memcpy(&outputY[yMaxH * rowBytesY], yLastRow, yWidth);
886 }
887 if (hasUVLastRow) {
888 memcpy(&outputU[uvMaxH * rowBytesU], uLastRow, uvSize.width());
889 memcpy(&outputV[uvMaxH * rowBytesV], vLastRow, uvSize.width());
890 }
891 }
892
893 cinfo.output_scanline = SkMin32(cinfo.output_scanline, cinfo.output_height);
894
895 return true;
896 }
897
898 bool SkJPEGImageDecoder::onDecodeToYUV(SkStream* stream, SkISize componentSizes[ 3],
899 SkImagePlanes* imagePlanes) {
900 #ifdef TIME_DECODE
901 SkAutoTime atm("JPEG Decode");
902 #endif
903
904 if (this->getSampleSize() != 1) {
905 return false; // Resizing not supported
906 }
907
908 JPEGAutoClean autoClean;
909
910 jpeg_decompress_struct cinfo;
911 skjpeg_source_mgr srcManager(stream, this);
912
913 skjpeg_error_mgr errorManager;
914 set_error_mgr(&cinfo, &errorManager);
915
916 // All objects need to be instantiated before this setjmp call so that
917 // they will be cleaned up properly if an error occurs.
918 if (setjmp(errorManager.fJmpBuf)) {
919 return return_false(cinfo, 0, 0, "setjmp");
920 }
921
922 initialize_info(&cinfo, &srcManager);
923 autoClean.set(&cinfo);
924
925 int status = jpeg_read_header(&cinfo, true);
926 if (status != JPEG_HEADER_OK) {
927 return return_false(cinfo, 0, 0, "read_header");
928 }
929
930 if (cinfo.jpeg_color_space != JCS_YCbCr) {
931 // It's not an error to not be encoded in YUV, so no need to use return_ false()
932 return false;
933 }
934
935 cinfo.out_color_space = JCS_YCbCr;
936 cinfo.raw_data_out = TRUE;
937
938 if (NULL == imagePlanes) { // Compute size only
939 update_components_sizes(cinfo, componentSizes, kSizeForMemoryAllocation_ SizeType);
940 return true;
941 }
942
943 set_dct_method(*this, &cinfo);
944
945 SkASSERT(1 == cinfo.scale_num);
946 cinfo.scale_denom = 1;
947
948 turn_off_visual_optimizations(&cinfo);
949
950 #ifdef ANDROID_RGB
951 cinfo.dither_mode = JDITHER_NONE;
952 #endif
953
954 /* image_width and image_height are the original dimensions, available
955 after jpeg_read_header(). To see the scaled dimensions, we have to call
956 jpeg_start_decompress(), and then read output_width and output_height.
957 */
958 if (!jpeg_start_decompress(&cinfo)) {
959 return return_false(cinfo, 0, 0, "start_decompress");
960 }
961
962 if (!output_raw_data(cinfo, imagePlanes)) {
963 return return_false(cinfo, 0, 0, "output_raw_data");
964 }
965
966 update_components_sizes(cinfo, componentSizes, kActualSize_SizeType);
967 jpeg_finish_decompress(&cinfo);
968 return true;
969 }
970
729 #ifdef SK_BUILD_FOR_ANDROID 971 #ifdef SK_BUILD_FOR_ANDROID
730 bool SkJPEGImageDecoder::onBuildTileIndex(SkStreamRewindable* stream, int *width , int *height) { 972 bool SkJPEGImageDecoder::onBuildTileIndex(SkStreamRewindable* stream, int *width , int *height) {
731 973
732 SkAutoTDelete<SkJPEGImageIndex> imageIndex(SkNEW_ARGS(SkJPEGImageIndex, (str eam, this))); 974 SkAutoTDelete<SkJPEGImageIndex> imageIndex(SkNEW_ARGS(SkJPEGImageIndex, (str eam, this)));
733 jpeg_decompress_struct* cinfo = imageIndex->cinfo(); 975 jpeg_decompress_struct* cinfo = imageIndex->cinfo();
734 976
735 skjpeg_error_mgr sk_err; 977 skjpeg_error_mgr sk_err;
736 set_error_mgr(cinfo, &sk_err); 978 set_error_mgr(cinfo, &sk_err);
737 979
738 // All objects need to be instantiated before this setjmp call so that 980 // All objects need to be instantiated before this setjmp call so that
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after
1233 return SkImageDecoder::kUnknown_Format; 1475 return SkImageDecoder::kUnknown_Format;
1234 } 1476 }
1235 1477
1236 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { 1478 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) {
1237 return (SkImageEncoder::kJPEG_Type == t) ? SkNEW(SkJPEGImageEncoder) : NULL; 1479 return (SkImageEncoder::kJPEG_Type == t) ? SkNEW(SkJPEGImageEncoder) : NULL;
1238 } 1480 }
1239 1481
1240 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); 1482 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory);
1241 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); 1483 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg);
1242 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); 1484 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698