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

Side by Side Diff: media/base/video_frame.cc

Issue 1704273002: Add MojoVideoFrame (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: few protected methods Created 4 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "media/base/video_frame.h" 5 #include "media/base/video_frame.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <climits> 8 #include <climits>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 18 matching lines...) Expand all
29 static inline size_t RoundUp(size_t value, size_t alignment) { 29 static inline size_t RoundUp(size_t value, size_t alignment) {
30 DCHECK(IsPowerOfTwo(alignment)); 30 DCHECK(IsPowerOfTwo(alignment));
31 return ((value + (alignment - 1)) & ~(alignment - 1)); 31 return ((value + (alignment - 1)) & ~(alignment - 1));
32 } 32 }
33 33
34 static inline size_t RoundDown(size_t value, size_t alignment) { 34 static inline size_t RoundDown(size_t value, size_t alignment) {
35 DCHECK(IsPowerOfTwo(alignment)); 35 DCHECK(IsPowerOfTwo(alignment));
36 return value & ~(alignment - 1); 36 return value & ~(alignment - 1);
37 } 37 }
38 38
39 static std::string ConfigToString(const VideoPixelFormat format,
40 const VideoFrame::StorageType storage_type,
41 const gfx::Size& coded_size,
42 const gfx::Rect& visible_rect,
43 const gfx::Size& natural_size) {
44 return base::StringPrintf(
45 "format:%s coded_size:%s visible_rect:%s natural_size:%s",
46 VideoPixelFormatToString(format).c_str(), coded_size.ToString().c_str(),
47 visible_rect.ToString().c_str(), natural_size.ToString().c_str());
48 }
49
50 static std::string StorageTypeToString( 39 static std::string StorageTypeToString(
51 const VideoFrame::StorageType storage_type) { 40 const VideoFrame::StorageType storage_type) {
52 switch (storage_type) { 41 switch (storage_type) {
53 case VideoFrame::STORAGE_UNKNOWN: 42 case VideoFrame::STORAGE_UNKNOWN:
54 return "UNKNOWN"; 43 return "UNKNOWN";
55 case VideoFrame::STORAGE_OPAQUE: 44 case VideoFrame::STORAGE_OPAQUE:
56 return "OPAQUE"; 45 return "OPAQUE";
57 case VideoFrame::STORAGE_UNOWNED_MEMORY: 46 case VideoFrame::STORAGE_UNOWNED_MEMORY:
58 return "UNOWNED_MEMORY"; 47 return "UNOWNED_MEMORY";
59 case VideoFrame::STORAGE_OWNED_MEMORY: 48 case VideoFrame::STORAGE_OWNED_MEMORY:
60 return "OWNED_MEMORY"; 49 return "OWNED_MEMORY";
61 case VideoFrame::STORAGE_SHMEM: 50 case VideoFrame::STORAGE_SHMEM:
62 return "SHMEM"; 51 return "SHMEM";
63 #if defined(OS_LINUX) 52 #if defined(OS_LINUX)
64 case VideoFrame::STORAGE_DMABUFS: 53 case VideoFrame::STORAGE_DMABUFS:
65 return "DMABUFS"; 54 return "DMABUFS";
66 #endif 55 #endif
67 #if defined(VIDEO_HOLE) 56 #if defined(VIDEO_HOLE)
68 case VideoFrame::STORAGE_HOLE: 57 case VideoFrame::STORAGE_HOLE:
69 return "HOLE"; 58 return "HOLE";
70 #endif 59 #endif
71 case VideoFrame::STORAGE_GPU_MEMORY_BUFFERS: 60 case VideoFrame::STORAGE_GPU_MEMORY_BUFFERS:
72 return "GPU_MEMORY_BUFFERS"; 61 return "GPU_MEMORY_BUFFERS";
62 case VideoFrame::STORAGE_MOJO_SHARED_MEMORY:
63 return "MOJO_SHARED_MEMORY";
73 } 64 }
74 65
75 NOTREACHED() << "Invalid StorageType provided: " << storage_type; 66 NOTREACHED() << "Invalid StorageType provided: " << storage_type;
76 return "INVALID"; 67 return "INVALID";
77 } 68 }
78 69
79 // Returns true if |plane| is a valid plane index for the given |format|.
80 static bool IsValidPlane(size_t plane, VideoPixelFormat format) {
81 DCHECK_LE(VideoFrame::NumPlanes(format),
82 static_cast<size_t>(VideoFrame::kMaxPlanes));
83 return (plane < VideoFrame::NumPlanes(format));
84 }
85
86 // Returns true if |frame| is accesible mapped in the VideoFrame memory space. 70 // Returns true if |frame| is accesible mapped in the VideoFrame memory space.
87 // static 71 // static
88 static bool IsStorageTypeMappable(VideoFrame::StorageType storage_type) { 72 static bool IsStorageTypeMappable(VideoFrame::StorageType storage_type) {
89 return 73 return
90 #if defined(OS_LINUX) 74 #if defined(OS_LINUX)
91 // This is not strictly needed but makes explicit that, at VideoFrame 75 // This is not strictly needed but makes explicit that, at VideoFrame
92 // level, DmaBufs are not mappable from userspace. 76 // level, DmaBufs are not mappable from userspace.
93 storage_type != VideoFrame::STORAGE_DMABUFS && 77 storage_type != VideoFrame::STORAGE_DMABUFS &&
94 #endif 78 #endif
95 (storage_type == VideoFrame::STORAGE_UNOWNED_MEMORY || 79 (storage_type == VideoFrame::STORAGE_UNOWNED_MEMORY ||
96 storage_type == VideoFrame::STORAGE_OWNED_MEMORY || 80 storage_type == VideoFrame::STORAGE_OWNED_MEMORY ||
97 storage_type == VideoFrame::STORAGE_SHMEM || 81 storage_type == VideoFrame::STORAGE_SHMEM ||
98 storage_type == VideoFrame::STORAGE_GPU_MEMORY_BUFFERS); 82 storage_type == VideoFrame::STORAGE_GPU_MEMORY_BUFFERS ||
99 } 83 storage_type == VideoFrame::STORAGE_MOJO_SHARED_MEMORY);
100
101 // Returns the pixel size per element for given |plane| and |format|. E.g. 2x2
102 // for the U-plane in PIXEL_FORMAT_I420.
103 static gfx::Size SampleSize(VideoPixelFormat format, size_t plane) {
104 DCHECK(IsValidPlane(plane, format));
105
106 switch (plane) {
107 case VideoFrame::kYPlane:
108 case VideoFrame::kAPlane:
109 return gfx::Size(1, 1);
110
111 case VideoFrame::kUPlane: // and VideoFrame::kUVPlane:
112 case VideoFrame::kVPlane:
113 switch (format) {
114 case PIXEL_FORMAT_YV24:
115 case PIXEL_FORMAT_YUV444P9:
116 case PIXEL_FORMAT_YUV444P10:
117 return gfx::Size(1, 1);
118
119 case PIXEL_FORMAT_YV16:
120 case PIXEL_FORMAT_YUV422P9:
121 case PIXEL_FORMAT_YUV422P10:
122 return gfx::Size(2, 1);
123
124 case PIXEL_FORMAT_YV12:
125 case PIXEL_FORMAT_I420:
126 case PIXEL_FORMAT_YV12A:
127 case PIXEL_FORMAT_NV12:
128 case PIXEL_FORMAT_NV21:
129 case PIXEL_FORMAT_MT21:
130 case PIXEL_FORMAT_YUV420P9:
131 case PIXEL_FORMAT_YUV420P10:
132 return gfx::Size(2, 2);
133
134 case PIXEL_FORMAT_UNKNOWN:
135 case PIXEL_FORMAT_UYVY:
136 case PIXEL_FORMAT_YUY2:
137 case PIXEL_FORMAT_ARGB:
138 case PIXEL_FORMAT_XRGB:
139 case PIXEL_FORMAT_RGB24:
140 case PIXEL_FORMAT_RGB32:
141 case PIXEL_FORMAT_MJPEG:
142 break;
143 }
144 }
145 NOTREACHED();
146 return gfx::Size();
147 }
148
149 // Return the alignment for the whole frame, calculated as the max of the
150 // alignment for each individual plane.
151 static gfx::Size CommonAlignment(VideoPixelFormat format) {
152 int max_sample_width = 0;
153 int max_sample_height = 0;
154 for (size_t plane = 0; plane < VideoFrame::NumPlanes(format); ++plane) {
155 const gfx::Size sample_size = SampleSize(format, plane);
156 max_sample_width = std::max(max_sample_width, sample_size.width());
157 max_sample_height = std::max(max_sample_height, sample_size.height());
158 }
159 return gfx::Size(max_sample_width, max_sample_height);
160 }
161
162 // Returns the number of bytes per element for given |plane| and |format|.
163 static int BytesPerElement(VideoPixelFormat format, size_t plane) {
164 DCHECK(IsValidPlane(plane, format));
165 switch (format) {
166 case PIXEL_FORMAT_ARGB:
167 case PIXEL_FORMAT_XRGB:
168 case PIXEL_FORMAT_RGB32:
169 return 4;
170 case PIXEL_FORMAT_RGB24:
171 return 3;
172 case PIXEL_FORMAT_UYVY:
173 case PIXEL_FORMAT_YUY2:
174 case PIXEL_FORMAT_YUV420P9:
175 case PIXEL_FORMAT_YUV422P9:
176 case PIXEL_FORMAT_YUV444P9:
177 case PIXEL_FORMAT_YUV420P10:
178 case PIXEL_FORMAT_YUV422P10:
179 case PIXEL_FORMAT_YUV444P10:
180 return 2;
181 case PIXEL_FORMAT_NV12:
182 case PIXEL_FORMAT_NV21:
183 case PIXEL_FORMAT_MT21: {
184 static const int bytes_per_element[] = {1, 2};
185 DCHECK_LT(plane, arraysize(bytes_per_element));
186 return bytes_per_element[plane];
187 }
188 case PIXEL_FORMAT_YV12:
189 case PIXEL_FORMAT_I420:
190 case PIXEL_FORMAT_YV16:
191 case PIXEL_FORMAT_YV12A:
192 case PIXEL_FORMAT_YV24:
193 return 1;
194 case PIXEL_FORMAT_MJPEG:
195 return 0;
196 case PIXEL_FORMAT_UNKNOWN:
197 break;
198 }
199 NOTREACHED();
200 return 0;
201 } 84 }
202 85
203 // static 86 // static
204 bool VideoFrame::IsValidConfig(VideoPixelFormat format, 87 bool VideoFrame::IsValidConfig(VideoPixelFormat format,
205 StorageType storage_type, 88 StorageType storage_type,
206 const gfx::Size& coded_size, 89 const gfx::Size& coded_size,
207 const gfx::Rect& visible_rect, 90 const gfx::Rect& visible_rect,
208 const gfx::Size& natural_size) { 91 const gfx::Size& natural_size) {
209 // Check maximum limits for all formats. 92 // Check maximum limits for all formats.
210 int coded_size_area = coded_size.GetCheckedArea().ValueOrDefault(INT_MAX); 93 int coded_size_area = coded_size.GetCheckedArea().ValueOrDefault(INT_MAX);
(...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after
931 // CommonAlignment() check should be made in IsValidConfig()? 814 // CommonAlignment() check should be made in IsValidConfig()?
932 // http://crbug.com/555909 815 // http://crbug.com/555909
933 frame->strides_[kUPlane] = coded_size.width() / 2; 816 frame->strides_[kUPlane] = coded_size.width() / 2;
934 frame->strides_[kVPlane] = coded_size.width() / 2; 817 frame->strides_[kVPlane] = coded_size.width() / 2;
935 frame->data_[kYPlane] = data; 818 frame->data_[kYPlane] = data;
936 frame->data_[kUPlane] = data + coded_size.GetArea(); 819 frame->data_[kUPlane] = data + coded_size.GetArea();
937 frame->data_[kVPlane] = data + (coded_size.GetArea() * 5 / 4); 820 frame->data_[kVPlane] = data + (coded_size.GetArea() * 5 / 4);
938 return frame; 821 return frame;
939 } 822 }
940 823
824 void VideoFrame::set_data(size_t plane, uint8_t* ptr) {
825 DCHECK(IsValidPlane(plane, format_));
826 DCHECK(ptr);
827 data_[plane] = ptr;
828 }
829
830 void VideoFrame::set_stride(size_t plane, int stride) {
831 DCHECK(IsValidPlane(plane, format_));
832 DCHECK_GT(stride, 0);
833 strides_[plane] = stride;
834 }
835
941 VideoFrame::VideoFrame(VideoPixelFormat format, 836 VideoFrame::VideoFrame(VideoPixelFormat format,
942 StorageType storage_type, 837 StorageType storage_type,
943 const gfx::Size& coded_size, 838 const gfx::Size& coded_size,
944 const gfx::Rect& visible_rect, 839 const gfx::Rect& visible_rect,
945 const gfx::Size& natural_size, 840 const gfx::Size& natural_size,
946 base::TimeDelta timestamp) 841 base::TimeDelta timestamp)
947 : format_(format), 842 : format_(format),
948 storage_type_(storage_type), 843 storage_type_(storage_type),
949 coded_size_(coded_size), 844 coded_size_(coded_size),
950 visible_rect_(visible_rect), 845 visible_rect_(visible_rect),
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
1079 base::AlignedAlloc(data_size, kFrameAddressAlignment)); 974 base::AlignedAlloc(data_size, kFrameAddressAlignment));
1080 if (zero_initialize_memory) 975 if (zero_initialize_memory)
1081 memset(data, 0, data_size); 976 memset(data, 0, data_size);
1082 977
1083 for (size_t plane = 0; plane < NumPlanes(format_); ++plane) 978 for (size_t plane = 0; plane < NumPlanes(format_); ++plane)
1084 data_[plane] = data + offset[plane]; 979 data_[plane] = data + offset[plane];
1085 980
1086 AddDestructionObserver(base::Bind(&base::AlignedFree, data)); 981 AddDestructionObserver(base::Bind(&base::AlignedFree, data));
1087 } 982 }
1088 983
984 // static
985 std::string VideoFrame::ConfigToString(const VideoPixelFormat format,
986 const StorageType storage_type,
987 const gfx::Size& coded_size,
988 const gfx::Rect& visible_rect,
989 const gfx::Size& natural_size) {
990 return base::StringPrintf(
991 "format:%s coded_size:%s visible_rect:%s natural_size:%s",
992 VideoPixelFormatToString(format).c_str(), coded_size.ToString().c_str(),
993 visible_rect.ToString().c_str(), natural_size.ToString().c_str());
994 }
995
996 // static
997 bool VideoFrame::IsValidPlane(size_t plane, VideoPixelFormat format) {
998 DCHECK_LE(NumPlanes(format), static_cast<size_t>(kMaxPlanes));
999 return (plane < NumPlanes(format));
1000 }
1001
1002 // static
1003 gfx::Size VideoFrame::SampleSize(VideoPixelFormat format, size_t plane) {
1004 DCHECK(IsValidPlane(plane, format));
1005
1006 switch (plane) {
1007 case kYPlane:
1008 case kAPlane:
1009 return gfx::Size(1, 1);
1010
1011 case kUPlane: // and kUVPlane:
1012 case kVPlane:
1013 switch (format) {
1014 case PIXEL_FORMAT_YV24:
1015 case PIXEL_FORMAT_YUV444P9:
1016 case PIXEL_FORMAT_YUV444P10:
1017 return gfx::Size(1, 1);
1018
1019 case PIXEL_FORMAT_YV16:
1020 case PIXEL_FORMAT_YUV422P9:
1021 case PIXEL_FORMAT_YUV422P10:
1022 return gfx::Size(2, 1);
1023
1024 case PIXEL_FORMAT_YV12:
1025 case PIXEL_FORMAT_I420:
1026 case PIXEL_FORMAT_YV12A:
1027 case PIXEL_FORMAT_NV12:
1028 case PIXEL_FORMAT_NV21:
1029 case PIXEL_FORMAT_MT21:
1030 case PIXEL_FORMAT_YUV420P9:
1031 case PIXEL_FORMAT_YUV420P10:
1032 return gfx::Size(2, 2);
1033
1034 case PIXEL_FORMAT_UNKNOWN:
1035 case PIXEL_FORMAT_UYVY:
1036 case PIXEL_FORMAT_YUY2:
1037 case PIXEL_FORMAT_ARGB:
1038 case PIXEL_FORMAT_XRGB:
1039 case PIXEL_FORMAT_RGB24:
1040 case PIXEL_FORMAT_RGB32:
1041 case PIXEL_FORMAT_MJPEG:
1042 break;
1043 }
1044 }
1045 NOTREACHED();
1046 return gfx::Size();
1047 }
1048
1049 // static
1050 int VideoFrame::BytesPerElement(VideoPixelFormat format, size_t plane) {
1051 DCHECK(IsValidPlane(plane, format));
1052 switch (format) {
1053 case PIXEL_FORMAT_ARGB:
1054 case PIXEL_FORMAT_XRGB:
1055 case PIXEL_FORMAT_RGB32:
1056 return 4;
1057 case PIXEL_FORMAT_RGB24:
1058 return 3;
1059 case PIXEL_FORMAT_UYVY:
1060 case PIXEL_FORMAT_YUY2:
1061 case PIXEL_FORMAT_YUV420P9:
1062 case PIXEL_FORMAT_YUV422P9:
1063 case PIXEL_FORMAT_YUV444P9:
1064 case PIXEL_FORMAT_YUV420P10:
1065 case PIXEL_FORMAT_YUV422P10:
1066 case PIXEL_FORMAT_YUV444P10:
1067 return 2;
1068 case PIXEL_FORMAT_NV12:
1069 case PIXEL_FORMAT_NV21:
1070 case PIXEL_FORMAT_MT21: {
1071 static const int bytes_per_element[] = {1, 2};
1072 DCHECK_LT(plane, arraysize(bytes_per_element));
1073 return bytes_per_element[plane];
1074 }
1075 case PIXEL_FORMAT_YV12:
1076 case PIXEL_FORMAT_I420:
1077 case PIXEL_FORMAT_YV16:
1078 case PIXEL_FORMAT_YV12A:
1079 case PIXEL_FORMAT_YV24:
1080 return 1;
1081 case PIXEL_FORMAT_MJPEG:
1082 return 0;
1083 case PIXEL_FORMAT_UNKNOWN:
1084 break;
1085 }
1086 NOTREACHED();
1087 return 0;
1088 }
1089
1090 // static
1091 gfx::Size VideoFrame::CommonAlignment(VideoPixelFormat format) {
1092 int max_sample_width = 0;
1093 int max_sample_height = 0;
1094 for (size_t plane = 0; plane < NumPlanes(format); ++plane) {
1095 const gfx::Size sample_size = SampleSize(format, plane);
1096 max_sample_width = std::max(max_sample_width, sample_size.width());
1097 max_sample_height = std::max(max_sample_height, sample_size.height());
1098 }
1099 return gfx::Size(max_sample_width, max_sample_height);
1100 }
1101
1089 } // namespace media 1102 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698