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

Side by Side Diff: media/video/capture/mac/video_capture_device_avfoundation_mac.mm

Issue 912293005: Mac Video Capture AVFoundation: Calculate compressed frame size for MJPEG. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "media/video/capture/mac/video_capture_device_avfoundation_mac.h" 5 #import "media/video/capture/mac/video_capture_device_avfoundation_mac.h"
6 6
7 #import <CoreVideo/CoreVideo.h> 7 #import <CoreVideo/CoreVideo.h>
8 8
9 #include <cstring> // For memchr.
10
9 #include "base/logging.h" 11 #include "base/logging.h"
10 #include "base/mac/foundation_util.h" 12 #include "base/mac/foundation_util.h"
11 #include "media/video/capture/mac/video_capture_device_mac.h" 13 #include "media/video/capture/mac/video_capture_device_mac.h"
12 #include "ui/gfx/geometry/size.h" 14 #include "ui/gfx/geometry/size.h"
13 15
14 // Prefer MJPEG if frame width or height is larger than this. 16 // Prefer MJPEG if frame width or height is larger than this.
15 static const int kMjpegWidthThreshold = 640; 17 static const int kMjpegWidthThreshold = 640;
16 static const int kMjpegHeightThreshold = 480; 18 static const int kMjpegHeightThreshold = 480;
17 19
18 // This function translates Mac Core Video pixel formats to Chromium pixel 20 // This function translates Mac Core Video pixel formats to Chromium pixel
19 // formats. Chromium pixel formats are sorted in order of preference. 21 // formats. Chromium pixel formats are sorted in order of preference.
20 media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { 22 media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) {
21 switch (code) { 23 switch (code) {
22 case kCVPixelFormatType_422YpCbCr8: 24 case kCVPixelFormatType_422YpCbCr8:
23 return media::PIXEL_FORMAT_UYVY; 25 return media::PIXEL_FORMAT_UYVY;
24 case CoreMediaGlue::kCMPixelFormat_422YpCbCr8_yuvs: 26 case CoreMediaGlue::kCMPixelFormat_422YpCbCr8_yuvs:
25 return media::PIXEL_FORMAT_YUY2; 27 return media::PIXEL_FORMAT_YUY2;
26 case CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML: 28 case CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML:
27 return media::PIXEL_FORMAT_MJPEG; 29 return media::PIXEL_FORMAT_MJPEG;
28 default: 30 default:
29 return media::PIXEL_FORMAT_UNKNOWN; 31 return media::PIXEL_FORMAT_UNKNOWN;
30 } 32 }
31 } 33 }
32 34
35 // Returns frame size by finding the End Of Image marker, or 0 if not found.
36 size_t JpegFrameSize(const char* sample, size_t sampleSize) {
37 // Jump to next marker (0xff), check for End Of Image (0xd9), repeat.
38 const char* end = sample + sampleSize - 1;
39 for (const char* it = sample;
40 (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
41 ++it) {
42 if (it[1] == static_cast<char>(0xd9))
43 return 2 + (it - sample);
44 }
45 DLOG(WARNING) << "JPEG End Of Image (EOI) marker not found.";
46 return 0;
47 }
48
33 @implementation VideoCaptureDeviceAVFoundation 49 @implementation VideoCaptureDeviceAVFoundation
34 50
35 #pragma mark Class methods 51 #pragma mark Class methods
36 52
37 + (void)getDeviceNames:(NSMutableDictionary*)deviceNames { 53 + (void)getDeviceNames:(NSMutableDictionary*)deviceNames {
38 // At this stage we already know that AVFoundation is supported and the whole 54 // At this stage we already know that AVFoundation is supported and the whole
39 // library is loaded and initialised, by the device monitoring. 55 // library is loaded and initialised, by the device monitoring.
40 NSArray* devices = [AVCaptureDeviceGlue devices]; 56 NSArray* devices = [AVCaptureDeviceGlue devices];
41 for (CrAVCaptureDevice* device in devices) { 57 for (CrAVCaptureDevice* device in devices) {
42 if (([device hasMediaType:AVFoundationGlue::AVMediaTypeVideo()] || 58 if (([device hasMediaType:AVFoundationGlue::AVMediaTypeVideo()] ||
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 // If MJPEG, use block buffer instead of pixel buffer. 315 // If MJPEG, use block buffer instead of pixel buffer.
300 CoreMediaGlue::CMBlockBufferRef blockBuffer = 316 CoreMediaGlue::CMBlockBufferRef blockBuffer =
301 CoreMediaGlue::CMSampleBufferGetDataBuffer(sampleBuffer); 317 CoreMediaGlue::CMSampleBufferGetDataBuffer(sampleBuffer);
302 if (blockBuffer) { 318 if (blockBuffer) {
303 size_t lengthAtOffset; 319 size_t lengthAtOffset;
304 CoreMediaGlue::CMBlockBufferGetDataPointer( 320 CoreMediaGlue::CMBlockBufferGetDataPointer(
305 blockBuffer, 0, &lengthAtOffset, &frameSize, &baseAddress); 321 blockBuffer, 0, &lengthAtOffset, &frameSize, &baseAddress);
306 // Expect the MJPEG data to be available as a contiguous reference, i.e. 322 // Expect the MJPEG data to be available as a contiguous reference, i.e.
307 // not covered by multiple memory blocks. 323 // not covered by multiple memory blocks.
308 CHECK_EQ(lengthAtOffset, frameSize); 324 CHECK_EQ(lengthAtOffset, frameSize);
325
326 // If |frameSize| is suspiciously high (>= 8 bpp), calculate the actual
327 // size by finding the end of image marker. The purpose is to speed up the
328 // jpeg decoding in the browser.
tommi (sloooow) - chröme 2015/02/11 15:57:44 what about adding a note about the validation step
329 if (static_cast<int>(frameSize) >= dimensions.width * dimensions.height)
330 frameSize = JpegFrameSize(baseAddress, frameSize);
tommi (sloooow) - chröme 2015/02/11 15:57:44 If JpegFrameSize fails and returns 0, should we ke
331
332 if (frameSize == 0)
333 return;
309 } 334 }
310 } else { 335 } else {
311 videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer); 336 videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer);
312 // Lock the frame and calculate frame size. 337 // Lock the frame and calculate frame size.
313 if (CVPixelBufferLockBaseAddress(videoFrame, kCVPixelBufferLock_ReadOnly) == 338 if (CVPixelBufferLockBaseAddress(videoFrame, kCVPixelBufferLock_ReadOnly) ==
314 kCVReturnSuccess) { 339 kCVReturnSuccess) {
315 baseAddress = static_cast<char*>(CVPixelBufferGetBaseAddress(videoFrame)); 340 baseAddress = static_cast<char*>(CVPixelBufferGetBaseAddress(videoFrame));
316 frameSize = CVPixelBufferGetHeight(videoFrame) * 341 frameSize = CVPixelBufferGetHeight(videoFrame) *
317 CVPixelBufferGetBytesPerRow(videoFrame); 342 CVPixelBufferGetBytesPerRow(videoFrame);
318 } else { 343 } else {
(...skipping 23 matching lines...) Expand all
342 } 367 }
343 368
344 - (void)sendErrorString:(NSString*)error { 369 - (void)sendErrorString:(NSString*)error {
345 DLOG(ERROR) << [error UTF8String]; 370 DLOG(ERROR) << [error UTF8String];
346 base::AutoLock lock(lock_); 371 base::AutoLock lock(lock_);
347 if (frameReceiver_) 372 if (frameReceiver_)
348 frameReceiver_->ReceiveError([error UTF8String]); 373 frameReceiver_->ReceiveError([error UTF8String]);
349 } 374 }
350 375
351 @end 376 @end
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698