Chromium Code Reviews| Index: media/video/capture/mac/video_capture_device_avfoundation_mac.mm |
| diff --git a/media/video/capture/mac/video_capture_device_avfoundation_mac.mm b/media/video/capture/mac/video_capture_device_avfoundation_mac.mm |
| index f3c7ca76bd283694c1ef9cda38430123222b59b6..fb1f0c4208b7b6439e8b2b7984c1877e3bfac5b0 100644 |
| --- a/media/video/capture/mac/video_capture_device_avfoundation_mac.mm |
| +++ b/media/video/capture/mac/video_capture_device_avfoundation_mac.mm |
| @@ -6,6 +6,8 @@ |
| #import <CoreVideo/CoreVideo.h> |
| +#include <cstring> // For memchr. |
| + |
| #include "base/logging.h" |
| #include "base/mac/foundation_util.h" |
| #include "media/video/capture/mac/video_capture_device_mac.h" |
| @@ -30,6 +32,20 @@ media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { |
| } |
| } |
| +// Returns frame size by finding the End Of Image marker, or 0 if not found. |
| +size_t JpegFrameSize(const char* sample, size_t sampleSize) { |
| + // Jump to next marker (0xff), check for End Of Image (0xd9), repeat. |
| + const char* end = sample + sampleSize - 1; |
| + for (const char* it = sample; |
| + (it = static_cast<const char*>(memchr(it, 0xff, end - it))); |
|
torbjorng
2015/02/12 14:32:57
I'm not too fond of side-effects in loop terminati
|
| + ++it) { |
| + if (it[1] == static_cast<char>(0xd9)) |
| + return 2 + (it - sample); |
| + } |
| + DLOG(WARNING) << "JPEG End Of Image (EOI) marker not found."; |
| + return 0; |
| +} |
| + |
| @implementation VideoCaptureDeviceAVFoundation |
| #pragma mark Class methods |
| @@ -306,6 +322,15 @@ media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { |
| // Expect the MJPEG data to be available as a contiguous reference, i.e. |
| // not covered by multiple memory blocks. |
| CHECK_EQ(lengthAtOffset, frameSize); |
| + |
| + // If |frameSize| is suspiciously high (>= 8 bpp), calculate the actual |
| + // size by finding the end of image marker. The purpose is to speed up the |
| + // jpeg decoding in the browser. |
|
tommi (sloooow) - chröme
2015/02/11 15:57:44
what about adding a note about the validation step
|
| + if (static_cast<int>(frameSize) >= dimensions.width * dimensions.height) |
| + frameSize = JpegFrameSize(baseAddress, frameSize); |
|
tommi (sloooow) - chröme
2015/02/11 15:57:44
If JpegFrameSize fails and returns 0, should we ke
|
| + |
| + if (frameSize == 0) |
| + return; |
| } |
| } else { |
| videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer); |