OLD | NEW |
---|---|
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 Loading... | |
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 SkASSERT(8 == DCTSIZE); | |
scroggo
2016/01/04 18:29:16
Can this be a compile time assert (i.e. static_ass
msarett
2016/01/12 14:25:27
Yes!
| |
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 // TODO: 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 | |
473 // only the common cases. | |
474 bool unitUV = (1 == dinfo->comp_info[1].h_samp_factor) && | |
scroggo
2016/01/04 18:29:16
Instead of creating this boolean that is just read
msarett
2016/01/12 14:25:27
Done.
| |
475 (1 == dinfo->comp_info[1].v_samp_factor) && | |
476 (1 == dinfo->comp_info[2].h_samp_factor) && | |
477 (1 == dinfo->comp_info[2].v_samp_factor); | |
478 | |
479 int hSampY = dinfo->comp_info[0].h_samp_factor; | |
480 int vSampY = dinfo->comp_info[0].v_samp_factor; | |
481 // TODO: Chromium reports the YUV subsampling factors to the client. | |
scroggo
2016/01/04 18:29:16
nit: blank line before comment.
msarett
2016/01/13 20:50:25
Done.
| |
482 // Ex: 444, 422, 420, etc. | |
483 // Could it be useful to do the same here? | |
scroggo
2016/01/04 18:29:16
If we're going to replace/merge with Chromium, it
msarett
2016/01/12 14:25:27
Currently, the gpu determines the subsampling type
| |
484 bool commonY = (1 == hSampY && 1 == vSampY) || | |
485 (2 == hSampY && 1 == vSampY) || | |
486 (2 == hSampY && 2 == vSampY) || | |
487 (1 == hSampY && 2 == vSampY) || | |
488 (4 == hSampY && 1 == vSampY) || | |
489 (4 == hSampY && 2 == vSampY); | |
490 | |
491 return unitUV && commonY; | |
492 } | |
493 | |
494 bool SkJpegCodec::onGetYUV8Sizes(SkISize sizes[3]) const { | |
495 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); | |
496 if (!is_yuv_supported(dinfo)) { | |
497 return false; | |
498 } | |
499 | |
500 sizes[0].set(dinfo->comp_info[0].width_in_blocks * 8, dinfo->comp_info[0].do wnsampled_height); | |
501 sizes[1].set(dinfo->comp_info[1].width_in_blocks * 8, dinfo->comp_info[1].do wnsampled_height); | |
502 sizes[2].set(dinfo->comp_info[2].width_in_blocks * 8, dinfo->comp_info[2].do wnsampled_height); | |
503 return true; | |
504 } | |
505 | |
506 SkCodec::Result SkJpegCodec::onGetYUV8Planes(SkISize sizes[3], void* pixels[3], size_t rowBytes[3], | |
507 SkYUVColorSpace* colorSpace) { | |
508 SkISize recommendedSizes[3]; | |
509 // This will check is_yuv_supported(), so we don't need to here. | |
510 bool supportsYUV = this->onGetYUV8Sizes(recommendedSizes); | |
511 if (!supportsYUV || memcmp((const void*) recommendedSizes, (const void*) siz es, sizeof(sizes))) | |
512 { | |
513 fDecoderMgr->returnFailure("onGetYUV8Sizes", kInvalidInput); | |
scroggo
2016/01/04 18:29:16
return
msarett
2016/01/13 20:50:25
Done.
| |
514 } | |
515 | |
516 // Set the jump location for libjpeg errors | |
517 if (setjmp(fDecoderMgr->getJmpBuf())) { | |
518 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); | |
519 } | |
520 | |
521 // Get a pointer to the decompress info since we will use it quite frequentl y | |
522 jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo(); | |
523 | |
524 dinfo->raw_data_out = TRUE; | |
525 if (!jpeg_start_decompress(dinfo)) { | |
526 return fDecoderMgr->returnFailure("startDecompress", kInvalidInput); | |
527 } | |
528 | |
529 // A previous implementation claims that the return value of is_yuv_supporte d() | |
530 // may change after calling jpeg_start_decompress(). It looks to me like th is | |
531 // was caused by a bug in the old code, but we'll be safe and check here. | |
532 SkASSERT(is_yuv_supported(dinfo)); | |
533 | |
534 // TODO: As mentioned in is_yuv_supported(), we are requiring that U and V b e | |
535 // the same size and Y be equal to the image size (possibly with paddi ng | |
536 // on the width). This is by far the most common case, but technicall y we | |
537 // could support other combinations of sampling factors. | |
538 SkASSERT(sizes[1] == sizes[2]); | |
539 SkASSERT((uint32_t) sizes[0].height() == dinfo->output_height); | |
540 | |
541 // Build a JSAMPIMAGE to handle output from libjpeg-turbo. A JSAMPIMAGE has | |
542 // a 2-D array of pixels for each of the components (Y, U, V) in the image. | |
543 // Cheat Sheet: | |
544 // JSAMPIMAGE == JSAMPLEARRAY* == JSAMPROW** == JSAMPLE*** | |
545 JSAMPARRAY yuv[3]; | |
546 // Set aside enough space for pointers to rows of Y, U, and V. | |
547 JSAMPROW rowptrs[16 + 8 + 8]; | |
548 // TODO: Note that if we support more sampling factors, the amount of space set | |
549 // aside here and/or which pointers we use for Y, U, and V may change. | |
550 // This would also add complexity to the code below. | |
551 yuv[0] = &rowptrs[0]; // Y rows (8 or 16) | |
scroggo
2016/01/04 18:29:16
nit: Line up these comments?
msarett
2016/01/12 14:25:27
Done.
| |
552 yuv[1] = &rowptrs[16]; // U rows (8) | |
553 yuv[2] = &rowptrs[24]; // V rows (8) | |
554 | |
555 // Initialize rowptrs. | |
556 int numYRowsPerBlock = 8 * dinfo->comp_info[0].v_samp_factor; | |
557 for (int i = 0; i < numYRowsPerBlock; i++) { | |
558 rowptrs[i] = (JSAMPROW) SkTAddOffset<void>(pixels[0], i * rowBytes[0]); | |
scroggo
2016/01/04 18:29:16
I think part of the point of SkTAddOffset is that
msarett
2016/01/12 14:25:27
Done.
| |
559 } | |
560 for (int i = 0; i < 8; i++) { | |
561 rowptrs[i + 16] =(JSAMPROW) SkTAddOffset<void>(pixels[1], i * rowBytes[1 ]); | |
scroggo
2016/01/04 18:29:16
nit: space between "=" and the rhs.
msarett
2016/01/13 20:50:25
Done.
| |
562 rowptrs[i + 24] = (JSAMPROW) SkTAddOffset<void>(pixels[2], i * rowBytes[ 2]); | |
563 } | |
564 | |
565 // After each loop iteration, we will increment pointers to Y, U, and V. | |
566 int blockIncrementY = numYRowsPerBlock * rowBytes[0]; | |
567 int blockIncrementU = 8 * rowBytes[1]; | |
scroggo
2016/01/04 18:29:16
Do all these "8"s refer to DCTSIZE? Should we use
msarett
2016/01/12 14:25:27
Yes.
Should we use that constant instead?
Done.
scroggo
2016/01/13 19:34:48
I think it's more clear. It is more obvious to me
| |
568 int blockIncrementV = 8 * rowBytes[2]; | |
569 | |
570 uint32_t numRowsPerBlock = numYRowsPerBlock; | |
571 // We intentionally round down here. As a final step, we may need to decode one | |
scroggo
2016/01/04 18:29:16
Why do we intentionally round down?
And why might
msarett
2016/01/12 14:25:27
I'll improve this comment.
Essentially, if the im
| |
572 // more partial block. | |
573 int numIters = dinfo->output_height / numRowsPerBlock; | |
scroggo
2016/01/04 18:29:15
can be const?
msarett
2016/01/13 20:50:25
Done.
| |
574 int iters = 0; | |
575 while (iters < numIters) { | |
scroggo
2016/01/04 18:29:16
It looks like iters is only used inside the loop.
msarett
2016/01/13 20:50:25
Done.
| |
576 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); | |
577 if (linesRead < numRowsPerBlock) { | |
578 // FIXME: We will treat this as an error for now. | |
579 // Can we fill memory and still display an image? | |
580 return kIncompleteInput; | |
581 } | |
582 | |
583 // Update rowptrs. | |
584 for (int i = 0; i < numYRowsPerBlock; i++) { | |
585 rowptrs[i] += blockIncrementY; | |
586 } | |
587 for (int i = 0; i < 8; i++) { | |
588 rowptrs[i + 16] += blockIncrementU; | |
589 rowptrs[i + 24] += blockIncrementV; | |
590 } | |
591 iters++; | |
592 } | |
593 | |
594 uint32_t remainingRows = dinfo->output_height - dinfo->output_scanline; | |
595 SkASSERT(remainingRows == dinfo->output_height % numRowsPerBlock); | |
596 SkASSERT(dinfo->output_scanline == numIters * numRowsPerBlock); | |
597 if (remainingRows > 0) { | |
598 // libjpeg-turbo needs memory to be padded by the block sizes. We will fulfill | |
599 // this requirement using a dummy row buffer. | |
600 SkAutoTMalloc<JSAMPLE> dummyRow(rowBytes[0]); | |
601 for (int i = remainingRows; i < numYRowsPerBlock; i++) { | |
602 rowptrs[i] = dummyRow.get(); | |
603 } | |
604 int remainingUVRows = dinfo->comp_info[1].downsampled_height - 8 * numIt ers; | |
605 for (int i = remainingUVRows; i < 8; i++) { | |
606 rowptrs[i + 16] = dummyRow.get(); | |
607 rowptrs[i + 24] = dummyRow.get(); | |
608 } | |
609 | |
610 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); | |
scroggo
2016/01/04 18:29:15
Should this use |remainingRows| instead of |numRow
msarett
2016/01/13 20:50:25
Actually it must use |numRowsPerBlock|. libjpeg/t
| |
611 if (linesRead < remainingRows) { | |
612 // FIXME: We will treat this as an error for now. | |
613 // Can we fill memory and still display an image? | |
614 return kIncompleteInput; | |
scroggo
2016/01/04 18:29:16
Doesn't kIncompleteInput mean (in getPixels etc) t
msarett
2016/01/12 14:25:27
Done.
| |
615 } | |
616 } | |
617 | |
618 // FIXME: This is ugly. We should find a solution where the input memory is | |
scroggo
2016/01/04 18:29:16
Does making the query call return both the logical
msarett
2016/01/12 14:25:27
Yes I believe so.
| |
619 // padded appropriately, but we are able to report the true width fro m | |
620 // the start. | |
621 sizes[0].fWidth = dinfo->comp_info[0].downsampled_width; | |
622 sizes[1].fWidth = dinfo->comp_info[1].downsampled_width; | |
623 sizes[2].fWidth = dinfo->comp_info[2].downsampled_width; | |
624 | |
625 if (nullptr != colorSpace) { | |
626 *colorSpace = kJPEG_SkYUVColorSpace; | |
627 } | |
628 | |
629 return kSuccess; | |
630 } | |
OLD | NEW |