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

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: Add a test to DM 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
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(YUVPlanesSizes* sizes, YUVPlanesWidthBytes* widthB ytes,
511 SkYUVColorSpace* colorSpace) const {
512 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
513 if (!is_yuv_supported(dinfo)) {
514 return false;
515 }
516
517 sizes->YSize.set(dinfo->comp_info[0].downsampled_width, dinfo->comp_info[0]. downsampled_height);
518 sizes->USize.set(dinfo->comp_info[1].downsampled_width, dinfo->comp_info[1]. downsampled_height);
519 sizes->VSize.set(dinfo->comp_info[2].downsampled_width, dinfo->comp_info[2]. downsampled_height);
520 widthBytes->YWidthBytes = dinfo->comp_info[0].width_in_blocks * DCTSIZE;
521 widthBytes->UWidthBytes = dinfo->comp_info[1].width_in_blocks * DCTSIZE;
522 widthBytes->VWidthBytes = dinfo->comp_info[2].width_in_blocks * DCTSIZE;
523
524 if (colorSpace) {
525 *colorSpace = kJPEG_SkYUVColorSpace;
526 }
527
528 return true;
529 }
530
531 SkCodec::Result SkJpegCodec::onGetYUV8Planes(const YUVPlanesSizes* sizes, void* pixels[3],
532 const YUVPlanesWidthBytes* widthBytes) {
533 YUVPlanesSizes expectedSize;
534 YUVPlanesWidthBytes minWidthBytes;
535
536 // This will check is_yuv_supported(), so we don't need to here.
537 bool supportsYUV = this->onQueryYUV8(&expectedSize, &minWidthBytes, nullptr) ;
538 if (!supportsYUV || memcmp((const void*) &expectedSize, (const void*) sizes, sizeof(sizes)) ||
539 minWidthBytes.YWidthBytes < widthBytes->YWidthBytes ||
540 minWidthBytes.UWidthBytes < widthBytes->UWidthBytes ||
541 minWidthBytes.VWidthBytes < widthBytes->VWidthBytes) {
542 return fDecoderMgr->returnFailure("onGetYUV8Sizes", kInvalidInput);
543 }
544
545 // Set the jump location for libjpeg errors
546 if (setjmp(fDecoderMgr->getJmpBuf())) {
547 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
548 }
549
550 // Get a pointer to the decompress info since we will use it quite frequentl y
551 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
552
553 dinfo->raw_data_out = TRUE;
554 if (!jpeg_start_decompress(dinfo)) {
555 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
556 }
557
558 // A previous implementation claims that the return value of is_yuv_supporte d()
559 // may change after calling jpeg_start_decompress(). It looks to me like th is
560 // was caused by a bug in the old code, but we'll be safe and check here.
561 SkASSERT(is_yuv_supported(dinfo));
562
563 // Currently, we require that the Y plane dimensions match the image dimensi ons
564 // and that the U and V planes are the same dimensions.
565 SkASSERT(sizes->USize == sizes->VSize);
566 SkASSERT((uint32_t) sizes->YSize.width() == dinfo->output_width &&
567 (uint32_t) sizes->YSize.height() == dinfo->output_height);
568
569 // Build a JSAMPIMAGE to handle output from libjpeg-turbo. A JSAMPIMAGE has
570 // a 2-D array of pixels for each of the components (Y, U, V) in the image.
571 // Cheat Sheet:
572 // JSAMPIMAGE == JSAMPLEARRAY* == JSAMPROW** == JSAMPLE***
573 JSAMPARRAY yuv[3];
574
575 // Set aside enough space for pointers to rows of Y, U, and V.
576 JSAMPROW rowptrs[2 * DCTSIZE + DCTSIZE + DCTSIZE];
577 yuv[0] = &rowptrs[0]; // Y rows (DCTSIZE or 2 * DCTSIZE)
578 yuv[1] = &rowptrs[2 * DCTSIZE]; // U rows (DCTSIZE)
579 yuv[2] = &rowptrs[3 * DCTSIZE]; // V rows (DCTSIZE)
580
581 // Initialize rowptrs.
582 int numYRowsPerBlock = DCTSIZE * dinfo->comp_info[0].v_samp_factor;
583 for (int i = 0; i < numYRowsPerBlock; i++) {
584 rowptrs[i] = SkTAddOffset<JSAMPLE>(pixels[0], i * widthBytes->YWidthByte s);
585 }
586 for (int i = 0; i < DCTSIZE; i++) {
587 rowptrs[i + 2 * DCTSIZE] = SkTAddOffset<JSAMPLE>(pixels[1], i * widthByt es->UWidthBytes);
588 rowptrs[i + 3 * DCTSIZE] = SkTAddOffset<JSAMPLE>(pixels[2], i * widthByt es->VWidthBytes);
589 }
590
591 // After each loop iteration, we will increment pointers to Y, U, and V.
592 int blockIncrementY = numYRowsPerBlock * widthBytes->YWidthBytes;
593 int blockIncrementU = DCTSIZE * widthBytes->UWidthBytes;
594 int blockIncrementV = DCTSIZE * widthBytes->VWidthBytes;
595
596 uint32_t numRowsPerBlock = numYRowsPerBlock;
597
598 // We intentionally round down here, as this first loop will only handle
599 // full block rows. As a special case at the end, we will handle any
600 // remaining rows that do not make up a full block.
601 const int numIters = dinfo->output_height / numRowsPerBlock;
602 for (int i = 0; i < numIters; i++) {
603 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
604 if (linesRead < numRowsPerBlock) {
605 // FIXME: Handle incomplete YUV decodes without signalling an error.
606 return kInvalidInput;
607 }
608
609 // Update rowptrs.
610 for (int i = 0; i < numYRowsPerBlock; i++) {
611 rowptrs[i] += blockIncrementY;
612 }
613 for (int i = 0; i < DCTSIZE; i++) {
614 rowptrs[i + 2 * DCTSIZE] += blockIncrementU;
615 rowptrs[i + 3 * DCTSIZE] += blockIncrementV;
616 }
617 }
618
619 uint32_t remainingRows = dinfo->output_height - dinfo->output_scanline;
620 SkASSERT(remainingRows == dinfo->output_height % numRowsPerBlock);
621 SkASSERT(dinfo->output_scanline == numIters * numRowsPerBlock);
622 if (remainingRows > 0) {
623 // libjpeg-turbo needs memory to be padded by the block sizes. We will fulfill
624 // this requirement using a dummy row buffer.
625 // FIXME: Should SkCodec have an extra memory buffer that can be shared among
626 // all of the implementations that use temporary/garbage memory?
627 SkAutoTMalloc<JSAMPLE> dummyRow(widthBytes->YWidthBytes);
628 for (int i = remainingRows; i < numYRowsPerBlock; i++) {
629 rowptrs[i] = dummyRow.get();
630 }
631 int remainingUVRows = dinfo->comp_info[1].downsampled_height - DCTSIZE * numIters;
632 for (int i = remainingUVRows; i < DCTSIZE; i++) {
633 rowptrs[i + 2 * DCTSIZE] = dummyRow.get();
634 rowptrs[i + 3 * DCTSIZE] = dummyRow.get();
635 }
636
637 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
638 if (linesRead < remainingRows) {
639 // FIXME: Handle incomplete YUV decodes without signalling an error.
640 return kInvalidInput;
641 }
642 }
643
644 return kSuccess;
645 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698