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

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

Issue 178133005: Audit/fix use of media::VideoFrame::coded_size() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: e6f9affb danakj@ comments. Created 6 years, 9 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 | Annotate | Revision Log
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 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/aligned_memory.h" 12 #include "base/memory/aligned_memory.h"
13 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
14 #include "gpu/command_buffer/common/mailbox_holder.h" 14 #include "gpu/command_buffer/common/mailbox_holder.h"
15 #include "media/base/limits.h" 15 #include "media/base/limits.h"
16 #include "media/base/video_util.h" 16 #include "media/base/video_util.h"
17 #include "third_party/skia/include/core/SkBitmap.h" 17 #include "third_party/skia/include/core/SkBitmap.h"
18 18
19 namespace media { 19 namespace media {
20 20
21 static inline size_t RoundUp(size_t value, size_t alignment) {
22 // Check that |alignment| is a power of 2.
23 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1)));
24 return ((value + (alignment - 1)) & ~(alignment - 1));
25 }
26
21 // static 27 // static
22 scoped_refptr<VideoFrame> VideoFrame::CreateFrame( 28 scoped_refptr<VideoFrame> VideoFrame::CreateFrame(
23 VideoFrame::Format format, 29 VideoFrame::Format format,
24 const gfx::Size& coded_size, 30 const gfx::Size& coded_size,
25 const gfx::Rect& visible_rect, 31 const gfx::Rect& visible_rect,
26 const gfx::Size& natural_size, 32 const gfx::Size& natural_size,
27 base::TimeDelta timestamp) { 33 base::TimeDelta timestamp) {
28 DCHECK(IsValidConfig(format, coded_size, visible_rect, natural_size)); 34 // Since we're creating a new YUV frame (and allocating memory for it
29 scoped_refptr<VideoFrame> frame(new VideoFrame( 35 // ourselves), we can pad the requested |coded_size| if necessary if the
30 format, coded_size, visible_rect, natural_size, timestamp, false)); 36 // request does not line up on sample boundaries.
37 gfx::Size new_coded_size(coded_size);
31 switch (format) { 38 switch (format) {
32 case VideoFrame::YV12: 39 case VideoFrame::YV12:
33 case VideoFrame::YV12A: 40 case VideoFrame::YV12A:
34 case VideoFrame::YV16:
35 case VideoFrame::I420: 41 case VideoFrame::I420:
36 case VideoFrame::YV12J: 42 case VideoFrame::YV12J:
37 frame->AllocateYUV(); 43 new_coded_size.set_height((new_coded_size.height() + 1) / 2 * 2);
44 // Fallthrough.
45 case VideoFrame::YV16:
46 new_coded_size.set_width((new_coded_size.width() + 1) / 2 * 2);
38 break; 47 break;
39 default: 48 default:
40 LOG(FATAL) << "Unsupported frame format: " << format; 49 LOG(FATAL) << "Only YUV formats supported: " << format;
50 return NULL;
41 } 51 }
52 DCHECK(IsValidConfig(format, new_coded_size, visible_rect, natural_size));
53 scoped_refptr<VideoFrame> frame(new VideoFrame(
54 format, new_coded_size, visible_rect, natural_size, timestamp, false));
55 frame->AllocateYUV();
42 return frame; 56 return frame;
43 } 57 }
44 58
45 // static 59 // static
46 std::string VideoFrame::FormatToString(VideoFrame::Format format) { 60 std::string VideoFrame::FormatToString(VideoFrame::Format format) {
47 switch (format) { 61 switch (format) {
48 case VideoFrame::UNKNOWN: 62 case VideoFrame::UNKNOWN:
49 return "UNKNOWN"; 63 return "UNKNOWN";
50 case VideoFrame::YV12: 64 case VideoFrame::YV12:
51 return "YV12"; 65 return "YV12";
(...skipping 16 matching lines...) Expand all
68 } 82 }
69 NOTREACHED() << "Invalid videoframe format provided: " << format; 83 NOTREACHED() << "Invalid videoframe format provided: " << format;
70 return ""; 84 return "";
71 } 85 }
72 86
73 // static 87 // static
74 bool VideoFrame::IsValidConfig(VideoFrame::Format format, 88 bool VideoFrame::IsValidConfig(VideoFrame::Format format,
75 const gfx::Size& coded_size, 89 const gfx::Size& coded_size,
76 const gfx::Rect& visible_rect, 90 const gfx::Rect& visible_rect,
77 const gfx::Size& natural_size) { 91 const gfx::Size& natural_size) {
78 return (format != VideoFrame::UNKNOWN && 92 switch (format) {
79 !coded_size.IsEmpty() && 93 case VideoFrame::UNKNOWN:
80 coded_size.GetArea() <= limits::kMaxCanvas && 94 return (coded_size.IsEmpty() && visible_rect.IsEmpty() &&
81 coded_size.width() <= limits::kMaxDimension && 95 natural_size.IsEmpty());
82 coded_size.height() <= limits::kMaxDimension && 96 case VideoFrame::YV12:
83 !visible_rect.IsEmpty() && 97 case VideoFrame::YV12J:
84 visible_rect.x() >= 0 && visible_rect.y() >= 0 && 98 case VideoFrame::I420:
85 visible_rect.right() <= coded_size.width() && 99 case VideoFrame::YV12A:
86 visible_rect.bottom() <= coded_size.height() && 100 // YUV formats have width/height requirements due to chroma subsampling.
87 !natural_size.IsEmpty() && 101 if (static_cast<size_t>(coded_size.height()) <
88 natural_size.GetArea() <= limits::kMaxCanvas && 102 RoundUp(visible_rect.bottom(), 2))
Ami GONE FROM CHROMIUM 2014/02/28 23:17:32 Subtle! So it's ok for coded size to be odd as lon
sheu 2014/03/01 01:56:57 The relevant codecs already allow for odd-sized co
89 natural_size.width() <= limits::kMaxDimension && 103 return false;
90 natural_size.height() <= limits::kMaxDimension); 104 // Fallthrough.
105 case VideoFrame::YV16:
106 if (static_cast<size_t>(coded_size.width()) <
107 RoundUp(visible_rect.right(), 2))
108 return false;
109 break;
110 case VideoFrame::NATIVE_TEXTURE:
111 #if defined(VIDEO_HOLE)
112 case VideoFrame::HOLE:
113 #endif // defined(VIDEO_HOLE)
114 break;
115 case VideoFrame::HISTOGRAM_MAX:
116 NOTREACHED();
117 return false;
118 }
119
120 if (coded_size.IsEmpty() || coded_size.GetArea() > limits::kMaxCanvas ||
121 coded_size.width() > limits::kMaxDimension ||
122 coded_size.height() > limits::kMaxDimension || visible_rect.IsEmpty() ||
123 visible_rect.x() < 0 || visible_rect.y() < 0 ||
124 visible_rect.right() > coded_size.width() ||
125 visible_rect.bottom() > coded_size.height() || natural_size.IsEmpty() ||
126 natural_size.GetArea() > limits::kMaxCanvas ||
127 natural_size.width() > limits::kMaxDimension ||
128 natural_size.height() > limits::kMaxDimension)
129 return false;
130
131 return true;
91 } 132 }
92 133
93 // static 134 // static
94 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture( 135 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture(
95 scoped_ptr<gpu::MailboxHolder> mailbox_holder, 136 scoped_ptr<gpu::MailboxHolder> mailbox_holder,
96 const ReleaseMailboxCB& mailbox_holder_release_cb, 137 const ReleaseMailboxCB& mailbox_holder_release_cb,
97 const gfx::Size& coded_size, 138 const gfx::Size& coded_size,
98 const gfx::Rect& visible_rect, 139 const gfx::Rect& visible_rect,
99 const gfx::Size& natural_size, 140 const gfx::Size& natural_size,
100 base::TimeDelta timestamp, 141 base::TimeDelta timestamp,
(...skipping 21 matching lines...) Expand all
122 scoped_refptr<VideoFrame> VideoFrame::WrapExternalPackedMemory( 163 scoped_refptr<VideoFrame> VideoFrame::WrapExternalPackedMemory(
123 Format format, 164 Format format,
124 const gfx::Size& coded_size, 165 const gfx::Size& coded_size,
125 const gfx::Rect& visible_rect, 166 const gfx::Rect& visible_rect,
126 const gfx::Size& natural_size, 167 const gfx::Size& natural_size,
127 uint8* data, 168 uint8* data,
128 size_t data_size, 169 size_t data_size,
129 base::SharedMemoryHandle handle, 170 base::SharedMemoryHandle handle,
130 base::TimeDelta timestamp, 171 base::TimeDelta timestamp,
131 const base::Closure& no_longer_needed_cb) { 172 const base::Closure& no_longer_needed_cb) {
173 if (!IsValidConfig(format, coded_size, visible_rect, natural_size))
174 return NULL;
132 if (data_size < AllocationSize(format, coded_size)) 175 if (data_size < AllocationSize(format, coded_size))
133 return NULL; 176 return NULL;
134 177
135 switch (format) { 178 switch (format) {
136 case I420: { 179 case I420: {
137 scoped_refptr<VideoFrame> frame(new VideoFrame( 180 scoped_refptr<VideoFrame> frame(new VideoFrame(
138 format, coded_size, visible_rect, natural_size, timestamp, false)); 181 format, coded_size, visible_rect, natural_size, timestamp, false));
139 frame->shared_memory_handle_ = handle; 182 frame->shared_memory_handle_ = handle;
140 frame->strides_[kYPlane] = coded_size.width(); 183 frame->strides_[kYPlane] = coded_size.width();
141 frame->strides_[kUPlane] = coded_size.width() / 2; 184 frame->strides_[kUPlane] = coded_size.width() / 2;
(...skipping 17 matching lines...) Expand all
159 const gfx::Rect& visible_rect, 202 const gfx::Rect& visible_rect,
160 const gfx::Size& natural_size, 203 const gfx::Size& natural_size,
161 int32 y_stride, 204 int32 y_stride,
162 int32 u_stride, 205 int32 u_stride,
163 int32 v_stride, 206 int32 v_stride,
164 uint8* y_data, 207 uint8* y_data,
165 uint8* u_data, 208 uint8* u_data,
166 uint8* v_data, 209 uint8* v_data,
167 base::TimeDelta timestamp, 210 base::TimeDelta timestamp,
168 const base::Closure& no_longer_needed_cb) { 211 const base::Closure& no_longer_needed_cb) {
169 DCHECK(format == YV12 || format == YV16 || format == I420) << format; 212 if (!IsValidConfig(format, coded_size, visible_rect, natural_size))
213 return NULL;
214
170 scoped_refptr<VideoFrame> frame(new VideoFrame( 215 scoped_refptr<VideoFrame> frame(new VideoFrame(
171 format, coded_size, visible_rect, natural_size, timestamp, false)); 216 format, coded_size, visible_rect, natural_size, timestamp, false));
172 frame->strides_[kYPlane] = y_stride; 217 frame->strides_[kYPlane] = y_stride;
173 frame->strides_[kUPlane] = u_stride; 218 frame->strides_[kUPlane] = u_stride;
174 frame->strides_[kVPlane] = v_stride; 219 frame->strides_[kVPlane] = v_stride;
175 frame->data_[kYPlane] = y_data; 220 frame->data_[kYPlane] = y_data;
176 frame->data_[kUPlane] = u_data; 221 frame->data_[kUPlane] = u_data;
177 frame->data_[kVPlane] = v_data; 222 frame->data_[kVPlane] = v_data;
178 frame->no_longer_needed_cb_ = no_longer_needed_cb; 223 frame->no_longer_needed_cb_ = no_longer_needed_cb;
179 return frame; 224 return frame;
(...skipping 24 matching lines...) Expand all
204 gfx::Size(), 249 gfx::Size(),
205 kNoTimestamp(), 250 kNoTimestamp(),
206 true); 251 true);
207 } 252 }
208 253
209 // static 254 // static
210 scoped_refptr<VideoFrame> VideoFrame::CreateColorFrame( 255 scoped_refptr<VideoFrame> VideoFrame::CreateColorFrame(
211 const gfx::Size& size, 256 const gfx::Size& size,
212 uint8 y, uint8 u, uint8 v, 257 uint8 y, uint8 u, uint8 v,
213 base::TimeDelta timestamp) { 258 base::TimeDelta timestamp) {
214 DCHECK(IsValidConfig(VideoFrame::YV12, size, gfx::Rect(size), size));
215 scoped_refptr<VideoFrame> frame = VideoFrame::CreateFrame( 259 scoped_refptr<VideoFrame> frame = VideoFrame::CreateFrame(
216 VideoFrame::YV12, size, gfx::Rect(size), size, timestamp); 260 VideoFrame::YV12, size, gfx::Rect(size), size, timestamp);
217 FillYUV(frame.get(), y, u, v); 261 FillYUV(frame.get(), y, u, v);
218 return frame; 262 return frame;
219 } 263 }
220 264
221 // static 265 // static
222 scoped_refptr<VideoFrame> VideoFrame::CreateBlackFrame(const gfx::Size& size) { 266 scoped_refptr<VideoFrame> VideoFrame::CreateBlackFrame(const gfx::Size& size) {
223 const uint8 kBlackY = 0x00; 267 const uint8 kBlackY = 0x00;
224 const uint8 kBlackUV = 0x80; 268 const uint8 kBlackUV = 0x80;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 case VideoFrame::YV12A: 304 case VideoFrame::YV12A:
261 return 4; 305 return 4;
262 case VideoFrame::UNKNOWN: 306 case VideoFrame::UNKNOWN:
263 case VideoFrame::HISTOGRAM_MAX: 307 case VideoFrame::HISTOGRAM_MAX:
264 break; 308 break;
265 } 309 }
266 NOTREACHED() << "Unsupported video frame format: " << format; 310 NOTREACHED() << "Unsupported video frame format: " << format;
267 return 0; 311 return 0;
268 } 312 }
269 313
270 static inline size_t RoundUp(size_t value, size_t alignment) {
271 // Check that |alignment| is a power of 2.
272 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1)));
273 return ((value + (alignment - 1)) & ~(alignment-1));
274 }
275 314
276 // static 315 // static
277 size_t VideoFrame::AllocationSize(Format format, const gfx::Size& coded_size) { 316 size_t VideoFrame::AllocationSize(Format format, const gfx::Size& coded_size) {
278 size_t total = 0; 317 size_t total = 0;
279 for (size_t i = 0; i < NumPlanes(format); ++i) 318 for (size_t i = 0; i < NumPlanes(format); ++i)
280 total += PlaneAllocationSize(format, i, coded_size); 319 total += PlaneAllocationSize(format, i, coded_size);
281 return total; 320 return total;
282 } 321 }
283 322
284 // static 323 // static
285 size_t VideoFrame::PlaneAllocationSize(Format format, 324 gfx::Size VideoFrame::PlaneSize(Format format,
286 size_t plane, 325 size_t plane,
287 const gfx::Size& coded_size) { 326 const gfx::Size& coded_size) {
288 const size_t area = 327 const int width = RoundUp(coded_size.width(), 2);
289 RoundUp(coded_size.width(), 2) * RoundUp(coded_size.height(), 2); 328 const int height = RoundUp(coded_size.height(), 2);
290 switch (format) { 329 switch (format) {
291 case VideoFrame::YV12: 330 case VideoFrame::YV12:
292 case VideoFrame::YV12J: 331 case VideoFrame::YV12J:
293 case VideoFrame::I420: { 332 case VideoFrame::I420: {
294 switch (plane) { 333 switch (plane) {
295 case VideoFrame::kYPlane: 334 case VideoFrame::kYPlane:
296 return area; 335 return gfx::Size(width, height);
297 case VideoFrame::kUPlane: 336 case VideoFrame::kUPlane:
298 case VideoFrame::kVPlane: 337 case VideoFrame::kVPlane:
299 return area / 4; 338 return gfx::Size(width / 2, height / 2);
300 default: 339 default:
301 break; 340 break;
302 } 341 }
303 } 342 }
304 case VideoFrame::YV12A: { 343 case VideoFrame::YV12A: {
305 switch (plane) { 344 switch (plane) {
306 case VideoFrame::kYPlane: 345 case VideoFrame::kYPlane:
307 case VideoFrame::kAPlane: 346 case VideoFrame::kAPlane:
308 return area; 347 return gfx::Size(width, height);
309 case VideoFrame::kUPlane: 348 case VideoFrame::kUPlane:
310 case VideoFrame::kVPlane: 349 case VideoFrame::kVPlane:
311 return area / 4; 350 return gfx::Size(width / 2, height / 2);
312 default: 351 default:
313 break; 352 break;
314 } 353 }
315 } 354 }
316 case VideoFrame::YV16: { 355 case VideoFrame::YV16: {
317 switch (plane) { 356 switch (plane) {
318 case VideoFrame::kYPlane: 357 case VideoFrame::kYPlane:
319 return area; 358 return gfx::Size(width, height);
320 case VideoFrame::kUPlane: 359 case VideoFrame::kUPlane:
321 case VideoFrame::kVPlane: 360 case VideoFrame::kVPlane:
322 return area / 2; 361 return gfx::Size(width / 2, height);
323 default: 362 default:
324 break; 363 break;
325 } 364 }
326 } 365 }
327 case VideoFrame::UNKNOWN: 366 case VideoFrame::UNKNOWN:
328 case VideoFrame::NATIVE_TEXTURE: 367 case VideoFrame::NATIVE_TEXTURE:
329 case VideoFrame::HISTOGRAM_MAX: 368 case VideoFrame::HISTOGRAM_MAX:
330 #if defined(VIDEO_HOLE) 369 #if defined(VIDEO_HOLE)
331 case VideoFrame::HOLE: 370 case VideoFrame::HOLE:
332 #endif // defined(VIDEO_HOLE) 371 #endif // defined(VIDEO_HOLE)
333 break; 372 break;
334 } 373 }
335 NOTREACHED() << "Unsupported video frame format/plane: " 374 NOTREACHED() << "Unsupported video frame format/plane: "
336 << format << "/" << plane; 375 << format << "/" << plane;
337 return 0; 376 return gfx::Size();
377 }
378
379 size_t VideoFrame::PlaneAllocationSize(Format format,
380 size_t plane,
381 const gfx::Size& coded_size) {
382 // VideoFrame formats are (so far) all YUV and 1 byte per sample.
383 return PlaneSize(format, plane, coded_size).GetArea();
338 } 384 }
339 385
340 // Release data allocated by AllocateYUV(). 386 // Release data allocated by AllocateYUV().
341 static void ReleaseData(uint8* data) { 387 static void ReleaseData(uint8* data) {
342 DCHECK(data); 388 DCHECK(data);
343 base::AlignedFree(data); 389 base::AlignedFree(data);
344 } 390 }
345 391
346 void VideoFrame::AllocateYUV() { 392 void VideoFrame::AllocateYUV() {
347 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16 || 393 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16 ||
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 const gfx::Size& natural_size, 449 const gfx::Size& natural_size,
404 base::TimeDelta timestamp, 450 base::TimeDelta timestamp,
405 bool end_of_stream) 451 bool end_of_stream)
406 : format_(format), 452 : format_(format),
407 coded_size_(coded_size), 453 coded_size_(coded_size),
408 visible_rect_(visible_rect), 454 visible_rect_(visible_rect),
409 natural_size_(natural_size), 455 natural_size_(natural_size),
410 shared_memory_handle_(base::SharedMemory::NULLHandle()), 456 shared_memory_handle_(base::SharedMemory::NULLHandle()),
411 timestamp_(timestamp), 457 timestamp_(timestamp),
412 end_of_stream_(end_of_stream) { 458 end_of_stream_(end_of_stream) {
459 DCHECK(IsValidConfig(format_, coded_size_, visible_rect_, natural_size_));
460
413 memset(&strides_, 0, sizeof(strides_)); 461 memset(&strides_, 0, sizeof(strides_));
414 memset(&data_, 0, sizeof(data_)); 462 memset(&data_, 0, sizeof(data_));
415 } 463 }
416 464
417 VideoFrame::~VideoFrame() { 465 VideoFrame::~VideoFrame() {
418 if (!mailbox_holder_release_cb_.is_null()) { 466 if (!mailbox_holder_release_cb_.is_null()) {
419 base::ResetAndReturn(&mailbox_holder_release_cb_) 467 base::ResetAndReturn(&mailbox_holder_release_cb_)
420 .Run(mailbox_holder_.Pass()); 468 .Run(mailbox_holder_.Pass());
421 } 469 }
422 if (!no_longer_needed_cb_.is_null()) 470 if (!no_longer_needed_cb_.is_null())
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 break; 552 break;
505 for (int row = 0; row < rows(plane); ++row) { 553 for (int row = 0; row < rows(plane); ++row) {
506 base::MD5Update(context, base::StringPiece( 554 base::MD5Update(context, base::StringPiece(
507 reinterpret_cast<char*>(data(plane) + stride(plane) * row), 555 reinterpret_cast<char*>(data(plane) + stride(plane) * row),
508 row_bytes(plane))); 556 row_bytes(plane)));
509 } 557 }
510 } 558 }
511 } 559 }
512 560
513 } // namespace media 561 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698