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

Side by Side Diff: src/codec/SkJpegCodec.cpp

Issue 1549473003: Add getYUV8Planes() API to SkCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 11 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
« no previous file with comments | « src/codec/SkJpegCodec.h ('k') | tests/JpegTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
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 #include "SkCodec.h" 8 #include "SkCodec.h"
9 #include "SkJpegCodec.h" 9 #include "SkJpegCodec.h"
10 #include "SkJpegDecoderMgr.h" 10 #include "SkJpegDecoderMgr.h"
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 #endif 445 #endif
446 446
447 bool SkJpegCodec::onSkipScanlines(int count) { 447 bool SkJpegCodec::onSkipScanlines(int count) {
448 // Set the jump location for libjpeg errors 448 // Set the jump location for libjpeg errors
449 if (setjmp(fDecoderMgr->getJmpBuf())) { 449 if (setjmp(fDecoderMgr->getJmpBuf())) {
450 return fDecoderMgr->returnFalse("setjmp"); 450 return fDecoderMgr->returnFalse("setjmp");
451 } 451 }
452 452
453 return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count); 453 return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count);
454 } 454 }
455
456 static bool is_yuv_supported(jpeg_decompress_struct* dinfo) {
457 // Scaling is not supported in raw data mode.
458 SkASSERT(dinfo->scale_num == dinfo->scale_denom);
459
460 // I can't imagine that this would ever change, but we do depend on it.
461 static_assert(8 == DCTSIZE, "DCTSIZE (defined in jpeg library) should always be 8.");
462
463 if (JCS_YCbCr != dinfo->jpeg_color_space) {
464 return false;
465 }
466
467 SkASSERT(3 == dinfo->num_components);
468 SkASSERT(dinfo->comp_info);
469
470 // It is possible to perform a YUV decode for any combination of
471 // horizontal and vertical sampling that is supported by
472 // libjpeg/libjpeg-turbo. However, we will start by supporting only the
473 // common cases (where U and V have samp_factors of one).
474 //
475 // The definition of samp_factor is kind of the opposite of what SkCodec
476 // thinks of as a sampling factor. samp_factor is essentially a
477 // multiplier, and the larger the samp_factor is, the more samples that
478 // there will be. Ex:
479 // U_plane_width = image_width * (U_h_samp_factor / max_h_samp_factor)
480 //
481 // Supporting cases where the samp_factors for U or V were larger than
482 // that of Y would be an extremely difficult change, given that clients
483 // allocate memory as if the size of the Y plane is always the size of the
484 // image. However, this case is very, very rare.
485 if (!(1 == dinfo->comp_info[1].h_samp_factor) &&
486 (1 == dinfo->comp_info[1].v_samp_factor) &&
487 (1 == dinfo->comp_info[2].h_samp_factor) &&
488 (1 == dinfo->comp_info[2].v_samp_factor)) {
489 return false;
490 }
491
492 // Support all common cases of Y samp_factors.
493 // TODO (msarett): As mentioned above, it would be possible to support
494 // more combinations of samp_factors. The issues are:
495 // (1) Are there actually any images that are not covered
496 // by these cases?
497 // (2) How much complexity would be added to the
498 // implementation in order to support these rare
499 // cases?
500 int hSampY = dinfo->comp_info[0].h_samp_factor;
501 int vSampY = dinfo->comp_info[0].v_samp_factor;
502 return (1 == hSampY && 1 == vSampY) ||
503 (2 == hSampY && 1 == vSampY) ||
504 (2 == hSampY && 2 == vSampY) ||
505 (1 == hSampY && 2 == vSampY) ||
506 (4 == hSampY && 1 == vSampY) ||
507 (4 == hSampY && 2 == vSampY);
508 }
509
510 bool SkJpegCodec::onQueryYUV8(YUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace ) const {
511 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
512 if (!is_yuv_supported(dinfo)) {
513 return false;
514 }
515
516 sizeInfo->fYSize.set(dinfo->comp_info[0].downsampled_width,
517 dinfo->comp_info[0].downsampled_height);
518 sizeInfo->fUSize.set(dinfo->comp_info[1].downsampled_width,
519 dinfo->comp_info[1].downsampled_height);
520 sizeInfo->fVSize.set(dinfo->comp_info[2].downsampled_width,
521 dinfo->comp_info[2].downsampled_height);
522 sizeInfo->fYWidthBytes = dinfo->comp_info[0].width_in_blocks * DCTSIZE;
523 sizeInfo->fUWidthBytes = dinfo->comp_info[1].width_in_blocks * DCTSIZE;
524 sizeInfo->fVWidthBytes = dinfo->comp_info[2].width_in_blocks * DCTSIZE;
525
526 if (colorSpace) {
527 *colorSpace = kJPEG_SkYUVColorSpace;
528 }
529
530 return true;
531 }
532
533 SkCodec::Result SkJpegCodec::onGetYUV8Planes(const YUVSizeInfo& sizeInfo, void* pixels[3]) {
534 YUVSizeInfo defaultInfo;
535
536 // This will check is_yuv_supported(), so we don't need to here.
537 bool supportsYUV = this->onQueryYUV8(&defaultInfo, nullptr);
538 if (!supportsYUV || sizeInfo.fYSize != defaultInfo.fYSize ||
539 sizeInfo.fUSize != defaultInfo.fUSize ||
540 sizeInfo.fVSize != defaultInfo.fVSize ||
541 sizeInfo.fYWidthBytes < defaultInfo.fYWidthBytes ||
542 sizeInfo.fUWidthBytes < defaultInfo.fUWidthBytes ||
543 sizeInfo.fVWidthBytes < defaultInfo.fVWidthBytes) {
544 return fDecoderMgr->returnFailure("onGetYUV8Planes", kInvalidInput);
545 }
546
547 // Set the jump location for libjpeg errors
548 if (setjmp(fDecoderMgr->getJmpBuf())) {
549 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
550 }
551
552 // Get a pointer to the decompress info since we will use it quite frequentl y
553 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
554
555 dinfo->raw_data_out = TRUE;
556 if (!jpeg_start_decompress(dinfo)) {
557 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
558 }
559
560 // A previous implementation claims that the return value of is_yuv_supporte d()
561 // may change after calling jpeg_start_decompress(). It looks to me like th is
562 // was caused by a bug in the old code, but we'll be safe and check here.
563 SkASSERT(is_yuv_supported(dinfo));
564
565 // Currently, we require that the Y plane dimensions match the image dimensi ons
566 // and that the U and V planes are the same dimensions.
567 SkASSERT(sizeInfo.fUSize == sizeInfo.fVSize);
568 SkASSERT((uint32_t) sizeInfo.fYSize.width() == dinfo->output_width &&
569 (uint32_t) sizeInfo.fYSize.height() == dinfo->output_height);
570
571 // Build a JSAMPIMAGE to handle output from libjpeg-turbo. A JSAMPIMAGE has
572 // a 2-D array of pixels for each of the components (Y, U, V) in the image.
573 // Cheat Sheet:
574 // JSAMPIMAGE == JSAMPLEARRAY* == JSAMPROW** == JSAMPLE***
575 JSAMPARRAY yuv[3];
576
577 // Set aside enough space for pointers to rows of Y, U, and V.
578 JSAMPROW rowptrs[2 * DCTSIZE + DCTSIZE + DCTSIZE];
579 yuv[0] = &rowptrs[0]; // Y rows (DCTSIZE or 2 * DCTSIZE)
580 yuv[1] = &rowptrs[2 * DCTSIZE]; // U rows (DCTSIZE)
581 yuv[2] = &rowptrs[3 * DCTSIZE]; // V rows (DCTSIZE)
582
583 // Initialize rowptrs.
584 int numYRowsPerBlock = DCTSIZE * dinfo->comp_info[0].v_samp_factor;
585 for (int i = 0; i < numYRowsPerBlock; i++) {
586 rowptrs[i] = SkTAddOffset<JSAMPLE>(pixels[0], i * sizeInfo.fYWidthBytes) ;
587 }
588 for (int i = 0; i < DCTSIZE; i++) {
589 rowptrs[i + 2 * DCTSIZE] = SkTAddOffset<JSAMPLE>(pixels[1], i * sizeInfo .fUWidthBytes);
590 rowptrs[i + 3 * DCTSIZE] = SkTAddOffset<JSAMPLE>(pixels[2], i * sizeInfo .fVWidthBytes);
591 }
592
593 // After each loop iteration, we will increment pointers to Y, U, and V.
594 size_t blockIncrementY = numYRowsPerBlock * sizeInfo.fYWidthBytes;
595 size_t blockIncrementU = DCTSIZE * sizeInfo.fUWidthBytes;
596 size_t blockIncrementV = DCTSIZE * sizeInfo.fVWidthBytes;
597
598 uint32_t numRowsPerBlock = numYRowsPerBlock;
599
600 // We intentionally round down here, as this first loop will only handle
601 // full block rows. As a special case at the end, we will handle any
602 // remaining rows that do not make up a full block.
603 const int numIters = dinfo->output_height / numRowsPerBlock;
604 for (int i = 0; i < numIters; i++) {
605 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
606 if (linesRead < numRowsPerBlock) {
607 // FIXME: Handle incomplete YUV decodes without signalling an error.
608 return kInvalidInput;
609 }
610
611 // Update rowptrs.
612 for (int i = 0; i < numYRowsPerBlock; i++) {
613 rowptrs[i] += blockIncrementY;
614 }
615 for (int i = 0; i < DCTSIZE; i++) {
616 rowptrs[i + 2 * DCTSIZE] += blockIncrementU;
617 rowptrs[i + 3 * DCTSIZE] += blockIncrementV;
618 }
619 }
620
621 uint32_t remainingRows = dinfo->output_height - dinfo->output_scanline;
622 SkASSERT(remainingRows == dinfo->output_height % numRowsPerBlock);
623 SkASSERT(dinfo->output_scanline == numIters * numRowsPerBlock);
624 if (remainingRows > 0) {
625 // libjpeg-turbo needs memory to be padded by the block sizes. We will fulfill
626 // this requirement using a dummy row buffer.
627 // FIXME: Should SkCodec have an extra memory buffer that can be shared among
628 // all of the implementations that use temporary/garbage memory?
629 SkAutoTMalloc<JSAMPLE> dummyRow(sizeInfo.fYWidthBytes);
630 for (int i = remainingRows; i < numYRowsPerBlock; i++) {
631 rowptrs[i] = dummyRow.get();
632 }
633 int remainingUVRows = dinfo->comp_info[1].downsampled_height - DCTSIZE * numIters;
634 for (int i = remainingUVRows; i < DCTSIZE; i++) {
635 rowptrs[i + 2 * DCTSIZE] = dummyRow.get();
636 rowptrs[i + 3 * DCTSIZE] = dummyRow.get();
637 }
638
639 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
640 if (linesRead < remainingRows) {
641 // FIXME: Handle incomplete YUV decodes without signalling an error.
642 return kInvalidInput;
643 }
644 }
645
646 return kSuccess;
647 }
OLDNEW
« no previous file with comments | « src/codec/SkJpegCodec.h ('k') | tests/JpegTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698