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
3 // found in the LICENSE file.
5 #include "media/base/video_capture_types.h"
7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h"
9 #include "media/base/limits.h"
13 VideoCaptureFormat::VideoCaptureFormat()
15 pixel_format(VIDEO_CAPTURE_PIXEL_FORMAT_UNKNOWN
),
16 pixel_storage(PIXEL_STORAGE_CPU
) {
19 VideoCaptureFormat::VideoCaptureFormat(const gfx::Size
& frame_size
,
21 VideoCapturePixelFormat pixel_format
)
22 : frame_size(frame_size
),
23 frame_rate(frame_rate
),
24 pixel_format(pixel_format
),
25 pixel_storage(PIXEL_STORAGE_CPU
) {
28 VideoCaptureFormat::VideoCaptureFormat(const gfx::Size
& frame_size
,
30 VideoCapturePixelFormat pixel_format
,
31 VideoPixelStorage pixel_storage
)
32 : frame_size(frame_size
),
33 frame_rate(frame_rate
),
34 pixel_format(pixel_format
),
35 pixel_storage(pixel_storage
) {
38 bool VideoCaptureFormat::IsValid() const {
39 return (frame_size
.width() < media::limits::kMaxDimension
) &&
40 (frame_size
.height() < media::limits::kMaxDimension
) &&
41 (frame_size
.GetArea() >= 0) &&
42 (frame_size
.GetArea() < media::limits::kMaxCanvas
) &&
43 (frame_rate
>= 0.0f
) &&
44 (frame_rate
< media::limits::kMaxFramesPerSecond
) &&
45 (pixel_storage
!= PIXEL_STORAGE_TEXTURE
||
46 pixel_format
== VIDEO_CAPTURE_PIXEL_FORMAT_ARGB
);
49 size_t VideoCaptureFormat::ImageAllocationSize() const {
50 size_t result_frame_size
= frame_size
.GetArea();
51 switch (pixel_format
) {
52 case VIDEO_CAPTURE_PIXEL_FORMAT_I420
:
53 case VIDEO_CAPTURE_PIXEL_FORMAT_YV12
:
54 case VIDEO_CAPTURE_PIXEL_FORMAT_NV12
:
55 case VIDEO_CAPTURE_PIXEL_FORMAT_NV21
:
56 result_frame_size
= result_frame_size
* 3 / 2;
58 case VIDEO_CAPTURE_PIXEL_FORMAT_UYVY
:
59 case VIDEO_CAPTURE_PIXEL_FORMAT_YUY2
:
60 result_frame_size
*= 2;
62 case VIDEO_CAPTURE_PIXEL_FORMAT_RGB24
:
63 result_frame_size
*= 3;
65 case VIDEO_CAPTURE_PIXEL_FORMAT_RGB32
:
66 case VIDEO_CAPTURE_PIXEL_FORMAT_ARGB
:
67 result_frame_size
*= 4;
69 case VIDEO_CAPTURE_PIXEL_FORMAT_MJPEG
:
70 result_frame_size
= 0;
72 default: // Sizes for the rest of the formats are unknown.
73 NOTREACHED() << "Unknown pixel format provided.";
76 return result_frame_size
;
80 std::string
VideoCaptureFormat::ToString(const VideoCaptureFormat
& format
) {
81 return base::StringPrintf("(%s)@%.3ffps, pixel format: %s storage: %s.",
82 format
.frame_size
.ToString().c_str(),
84 PixelFormatToString(format
.pixel_format
).c_str(),
85 PixelStorageToString(format
.pixel_storage
).c_str());
89 std::string
VideoCaptureFormat::PixelFormatToString(
90 VideoCapturePixelFormat format
) {
92 case VIDEO_CAPTURE_PIXEL_FORMAT_UNKNOWN
:
94 case VIDEO_CAPTURE_PIXEL_FORMAT_I420
:
96 case VIDEO_CAPTURE_PIXEL_FORMAT_YUY2
:
98 case VIDEO_CAPTURE_PIXEL_FORMAT_UYVY
:
100 case VIDEO_CAPTURE_PIXEL_FORMAT_RGB24
:
102 case VIDEO_CAPTURE_PIXEL_FORMAT_RGB32
:
104 case VIDEO_CAPTURE_PIXEL_FORMAT_ARGB
:
106 case VIDEO_CAPTURE_PIXEL_FORMAT_MJPEG
:
108 case VIDEO_CAPTURE_PIXEL_FORMAT_NV12
:
110 case VIDEO_CAPTURE_PIXEL_FORMAT_NV21
:
112 case VIDEO_CAPTURE_PIXEL_FORMAT_YV12
:
115 NOTREACHED() << "Invalid VideoCapturePixelFormat provided: " << format
;
116 return std::string();
120 std::string
VideoCaptureFormat::PixelStorageToString(
121 VideoPixelStorage storage
) {
123 case PIXEL_STORAGE_CPU
:
125 case PIXEL_STORAGE_TEXTURE
:
127 case PIXEL_STORAGE_GPUMEMORYBUFFER
:
128 return "GPUMEMORYBUFFER";
130 NOTREACHED() << "Invalid VideoPixelStorage provided: "
131 << static_cast<int>(storage
);
132 return std::string();
135 VideoCaptureParams::VideoCaptureParams()
136 : resolution_change_policy(RESOLUTION_POLICY_FIXED_RESOLUTION
),
137 power_line_frequency(PowerLineFrequency::FREQUENCY_DEFAULT
) {}