OLD | NEW |
---|---|
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 | 21 // static |
22 scoped_refptr<VideoFrame> VideoFrame::CreateFrame( | 22 scoped_refptr<VideoFrame> VideoFrame::CreateFrame( |
23 VideoFrame::Format format, | 23 VideoFrame::Format format, |
24 const gfx::Size& coded_size, | 24 const gfx::Size& coded_size, |
25 const gfx::Rect& visible_rect, | 25 const gfx::Rect& visible_rect, |
26 const gfx::Size& natural_size, | 26 const gfx::Size& natural_size, |
27 base::TimeDelta timestamp) { | 27 base::TimeDelta timestamp) { |
28 DCHECK(IsValidConfig(format, coded_size, visible_rect, natural_size)); | 28 // Since we're creating a new YUV frame (and allocating memory for it |
29 scoped_refptr<VideoFrame> frame(new VideoFrame( | 29 // ourselves), we can pad the requested |coded_size| if necessary. |
Ami GONE FROM CHROMIUM
2014/02/27 01:20:12
"necessary" for what?
sheu
2014/02/28 00:40:19
Added commentary.
| |
30 format, coded_size, visible_rect, natural_size, timestamp, false)); | 30 gfx::Size new_coded_size(coded_size); |
Ami GONE FROM CHROMIUM
2014/02/27 01:20:12
nit: when making a change like this I prefer to re
sheu
2014/02/28 00:40:19
I like to make the header declaration and the func
Ami GONE FROM CHROMIUM
2014/02/28 00:48:13
Meh.
(your way is better for cleaner signatures, m
| |
31 switch (format) { | 31 switch (format) { |
32 case VideoFrame::YV12: | 32 case VideoFrame::YV12: |
33 case VideoFrame::YV12A: | 33 case VideoFrame::YV12A: |
34 case VideoFrame::YV16: | |
35 case VideoFrame::I420: | 34 case VideoFrame::I420: |
36 case VideoFrame::YV12J: | 35 case VideoFrame::YV12J: |
37 frame->AllocateYUV(); | 36 new_coded_size.set_height((new_coded_size.height() + 1) / 2 * 2); |
37 // Fallthrough. | |
38 case VideoFrame::YV16: | |
39 new_coded_size.set_width((new_coded_size.width() + 1) / 2 * 2); | |
38 break; | 40 break; |
39 default: | 41 default: |
40 LOG(FATAL) << "Unsupported frame format: " << format; | 42 LOG(FATAL) << "Only YUV formats supported: " << format; |
43 return NULL; | |
Ami GONE FROM CHROMIUM
2014/02/27 01:20:12
Is there not enough __noreturn__ whateverness on L
sheu
2014/02/28 00:40:19
Nope. Still has to return something.
| |
41 } | 44 } |
45 DCHECK(IsValidConfig(format, new_coded_size, visible_rect, natural_size)); | |
46 scoped_refptr<VideoFrame> frame(new VideoFrame( | |
47 format, new_coded_size, visible_rect, natural_size, timestamp, false)); | |
48 frame->AllocateYUV(); | |
42 return frame; | 49 return frame; |
43 } | 50 } |
44 | 51 |
45 // static | 52 // static |
46 std::string VideoFrame::FormatToString(VideoFrame::Format format) { | 53 std::string VideoFrame::FormatToString(VideoFrame::Format format) { |
47 switch (format) { | 54 switch (format) { |
48 case VideoFrame::UNKNOWN: | 55 case VideoFrame::UNKNOWN: |
49 return "UNKNOWN"; | 56 return "UNKNOWN"; |
50 case VideoFrame::YV12: | 57 case VideoFrame::YV12: |
51 return "YV12"; | 58 return "YV12"; |
(...skipping 16 matching lines...) Expand all Loading... | |
68 } | 75 } |
69 NOTREACHED() << "Invalid videoframe format provided: " << format; | 76 NOTREACHED() << "Invalid videoframe format provided: " << format; |
70 return ""; | 77 return ""; |
71 } | 78 } |
72 | 79 |
73 // static | 80 // static |
74 bool VideoFrame::IsValidConfig(VideoFrame::Format format, | 81 bool VideoFrame::IsValidConfig(VideoFrame::Format format, |
75 const gfx::Size& coded_size, | 82 const gfx::Size& coded_size, |
76 const gfx::Rect& visible_rect, | 83 const gfx::Rect& visible_rect, |
77 const gfx::Size& natural_size) { | 84 const gfx::Size& natural_size) { |
78 return (format != VideoFrame::UNKNOWN && | 85 if (format == VideoFrame::UNKNOWN || coded_size.IsEmpty() || |
79 !coded_size.IsEmpty() && | 86 coded_size.GetArea() > limits::kMaxCanvas || |
80 coded_size.GetArea() <= limits::kMaxCanvas && | 87 coded_size.width() > limits::kMaxDimension || |
81 coded_size.width() <= limits::kMaxDimension && | 88 coded_size.height() > limits::kMaxDimension || visible_rect.IsEmpty() || |
82 coded_size.height() <= limits::kMaxDimension && | 89 visible_rect.x() < 0 || visible_rect.y() < 0 || |
83 !visible_rect.IsEmpty() && | 90 visible_rect.right() > coded_size.width() || |
84 visible_rect.x() >= 0 && visible_rect.y() >= 0 && | 91 visible_rect.bottom() > coded_size.height() || natural_size.IsEmpty() || |
85 visible_rect.right() <= coded_size.width() && | 92 natural_size.GetArea() > limits::kMaxCanvas || |
86 visible_rect.bottom() <= coded_size.height() && | 93 natural_size.width() > limits::kMaxDimension || |
87 !natural_size.IsEmpty() && | 94 natural_size.height() > limits::kMaxDimension) |
88 natural_size.GetArea() <= limits::kMaxCanvas && | 95 return false; |
89 natural_size.width() <= limits::kMaxDimension && | 96 |
90 natural_size.height() <= limits::kMaxDimension); | 97 // YUV formats have width/height requirements due to chroma subsampling. |
98 switch (format) { | |
99 case VideoFrame::YV12: | |
100 case VideoFrame::YV12J: | |
101 case VideoFrame::I420: | |
102 case VideoFrame::YV12A: | |
103 if (coded_size.height() % 2 != 0) | |
Ami GONE FROM CHROMIUM
2014/02/27 01:20:12
I almost want to CHECK this...
sheu
2014/02/28 00:40:19
Well, for the cases where we only DCHECK(IsValidCo
| |
104 return false; | |
105 // Fallthrough. | |
106 case VideoFrame::YV16: | |
107 if (coded_size.width() % 2 != 0) | |
Ami GONE FROM CHROMIUM
2014/02/27 01:20:12
I'd really like for this to be run through the "od
| |
108 return false; | |
109 case VideoFrame::UNKNOWN: | |
Ami GONE FROM CHROMIUM
2014/02/27 01:20:12
should be unreachable.
sheu
2014/02/28 00:40:19
With the DCHECK in the VideoFrame constructor, the
| |
110 case VideoFrame::NATIVE_TEXTURE: | |
111 case VideoFrame::HISTOGRAM_MAX: | |
112 #if defined(VIDEO_HOLE) | |
113 case VideoFrame::HOLE: | |
114 #endif // defined(VIDEO_HOLE) | |
115 break; | |
116 } | |
117 | |
118 return true; | |
91 } | 119 } |
92 | 120 |
93 // static | 121 // static |
94 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture( | 122 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture( |
95 scoped_ptr<gpu::MailboxHolder> mailbox_holder, | 123 scoped_ptr<gpu::MailboxHolder> mailbox_holder, |
96 const ReleaseMailboxCB& mailbox_holder_release_cb, | 124 const ReleaseMailboxCB& mailbox_holder_release_cb, |
97 const gfx::Size& coded_size, | 125 const gfx::Size& coded_size, |
98 const gfx::Rect& visible_rect, | 126 const gfx::Rect& visible_rect, |
99 const gfx::Size& natural_size, | 127 const gfx::Size& natural_size, |
100 base::TimeDelta timestamp, | 128 base::TimeDelta timestamp, |
(...skipping 21 matching lines...) Expand all Loading... | |
122 scoped_refptr<VideoFrame> VideoFrame::WrapExternalPackedMemory( | 150 scoped_refptr<VideoFrame> VideoFrame::WrapExternalPackedMemory( |
123 Format format, | 151 Format format, |
124 const gfx::Size& coded_size, | 152 const gfx::Size& coded_size, |
125 const gfx::Rect& visible_rect, | 153 const gfx::Rect& visible_rect, |
126 const gfx::Size& natural_size, | 154 const gfx::Size& natural_size, |
127 uint8* data, | 155 uint8* data, |
128 size_t data_size, | 156 size_t data_size, |
129 base::SharedMemoryHandle handle, | 157 base::SharedMemoryHandle handle, |
130 base::TimeDelta timestamp, | 158 base::TimeDelta timestamp, |
131 const base::Closure& no_longer_needed_cb) { | 159 const base::Closure& no_longer_needed_cb) { |
160 if (!IsValidConfig(format, coded_size, visible_rect, natural_size)) | |
161 return NULL; | |
132 if (data_size < AllocationSize(format, coded_size)) | 162 if (data_size < AllocationSize(format, coded_size)) |
133 return NULL; | 163 return NULL; |
134 | 164 |
135 switch (format) { | 165 switch (format) { |
136 case I420: { | 166 case I420: { |
137 scoped_refptr<VideoFrame> frame(new VideoFrame( | 167 scoped_refptr<VideoFrame> frame(new VideoFrame( |
138 format, coded_size, visible_rect, natural_size, timestamp, false)); | 168 format, coded_size, visible_rect, natural_size, timestamp, false)); |
139 frame->shared_memory_handle_ = handle; | 169 frame->shared_memory_handle_ = handle; |
140 frame->strides_[kYPlane] = coded_size.width(); | 170 frame->strides_[kYPlane] = coded_size.width(); |
141 frame->strides_[kUPlane] = coded_size.width() / 2; | 171 frame->strides_[kUPlane] = coded_size.width() / 2; |
(...skipping 17 matching lines...) Expand all Loading... | |
159 const gfx::Rect& visible_rect, | 189 const gfx::Rect& visible_rect, |
160 const gfx::Size& natural_size, | 190 const gfx::Size& natural_size, |
161 int32 y_stride, | 191 int32 y_stride, |
162 int32 u_stride, | 192 int32 u_stride, |
163 int32 v_stride, | 193 int32 v_stride, |
164 uint8* y_data, | 194 uint8* y_data, |
165 uint8* u_data, | 195 uint8* u_data, |
166 uint8* v_data, | 196 uint8* v_data, |
167 base::TimeDelta timestamp, | 197 base::TimeDelta timestamp, |
168 const base::Closure& no_longer_needed_cb) { | 198 const base::Closure& no_longer_needed_cb) { |
169 DCHECK(format == YV12 || format == YV16 || format == I420) << format; | 199 if (!IsValidConfig(format, coded_size, visible_rect, natural_size)) |
200 return NULL; | |
201 | |
170 scoped_refptr<VideoFrame> frame(new VideoFrame( | 202 scoped_refptr<VideoFrame> frame(new VideoFrame( |
171 format, coded_size, visible_rect, natural_size, timestamp, false)); | 203 format, coded_size, visible_rect, natural_size, timestamp, false)); |
172 frame->strides_[kYPlane] = y_stride; | 204 frame->strides_[kYPlane] = y_stride; |
173 frame->strides_[kUPlane] = u_stride; | 205 frame->strides_[kUPlane] = u_stride; |
174 frame->strides_[kVPlane] = v_stride; | 206 frame->strides_[kVPlane] = v_stride; |
175 frame->data_[kYPlane] = y_data; | 207 frame->data_[kYPlane] = y_data; |
176 frame->data_[kUPlane] = u_data; | 208 frame->data_[kUPlane] = u_data; |
177 frame->data_[kVPlane] = v_data; | 209 frame->data_[kVPlane] = v_data; |
178 frame->no_longer_needed_cb_ = no_longer_needed_cb; | 210 frame->no_longer_needed_cb_ = no_longer_needed_cb; |
179 return frame; | 211 return frame; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
275 | 307 |
276 // static | 308 // static |
277 size_t VideoFrame::AllocationSize(Format format, const gfx::Size& coded_size) { | 309 size_t VideoFrame::AllocationSize(Format format, const gfx::Size& coded_size) { |
278 size_t total = 0; | 310 size_t total = 0; |
279 for (size_t i = 0; i < NumPlanes(format); ++i) | 311 for (size_t i = 0; i < NumPlanes(format); ++i) |
280 total += PlaneAllocationSize(format, i, coded_size); | 312 total += PlaneAllocationSize(format, i, coded_size); |
281 return total; | 313 return total; |
282 } | 314 } |
283 | 315 |
284 // static | 316 // static |
285 size_t VideoFrame::PlaneAllocationSize(Format format, | 317 gfx::Size VideoFrame::PlaneSize(Format format, |
286 size_t plane, | 318 size_t plane, |
287 const gfx::Size& coded_size) { | 319 const gfx::Size& coded_size) { |
288 const size_t area = | 320 const int width = RoundUp(coded_size.width(), 2); |
289 RoundUp(coded_size.width(), 2) * RoundUp(coded_size.height(), 2); | 321 const int height = RoundUp(coded_size.height(), 2); |
290 switch (format) { | 322 switch (format) { |
291 case VideoFrame::YV12: | 323 case VideoFrame::YV12: |
292 case VideoFrame::YV12J: | 324 case VideoFrame::YV12J: |
293 case VideoFrame::I420: { | 325 case VideoFrame::I420: { |
294 switch (plane) { | 326 switch (plane) { |
295 case VideoFrame::kYPlane: | 327 case VideoFrame::kYPlane: |
296 return area; | 328 return gfx::Size(width, height); |
297 case VideoFrame::kUPlane: | 329 case VideoFrame::kUPlane: |
298 case VideoFrame::kVPlane: | 330 case VideoFrame::kVPlane: |
299 return area / 4; | 331 return gfx::Size(width / 2, height / 2); |
300 default: | 332 default: |
301 break; | 333 break; |
302 } | 334 } |
303 } | 335 } |
304 case VideoFrame::YV12A: { | 336 case VideoFrame::YV12A: { |
305 switch (plane) { | 337 switch (plane) { |
306 case VideoFrame::kYPlane: | 338 case VideoFrame::kYPlane: |
307 case VideoFrame::kAPlane: | 339 case VideoFrame::kAPlane: |
308 return area; | 340 return gfx::Size(width, height); |
309 case VideoFrame::kUPlane: | 341 case VideoFrame::kUPlane: |
310 case VideoFrame::kVPlane: | 342 case VideoFrame::kVPlane: |
311 return area / 4; | 343 return gfx::Size(width / 2, height / 2); |
312 default: | 344 default: |
313 break; | 345 break; |
314 } | 346 } |
315 } | 347 } |
316 case VideoFrame::YV16: { | 348 case VideoFrame::YV16: { |
317 switch (plane) { | 349 switch (plane) { |
318 case VideoFrame::kYPlane: | 350 case VideoFrame::kYPlane: |
319 return area; | 351 return gfx::Size(width, height); |
320 case VideoFrame::kUPlane: | 352 case VideoFrame::kUPlane: |
321 case VideoFrame::kVPlane: | 353 case VideoFrame::kVPlane: |
322 return area / 2; | 354 return gfx::Size(width / 2, height); |
323 default: | 355 default: |
324 break; | 356 break; |
325 } | 357 } |
326 } | 358 } |
327 case VideoFrame::UNKNOWN: | 359 case VideoFrame::UNKNOWN: |
328 case VideoFrame::NATIVE_TEXTURE: | 360 case VideoFrame::NATIVE_TEXTURE: |
329 case VideoFrame::HISTOGRAM_MAX: | 361 case VideoFrame::HISTOGRAM_MAX: |
330 #if defined(VIDEO_HOLE) | 362 #if defined(VIDEO_HOLE) |
331 case VideoFrame::HOLE: | 363 case VideoFrame::HOLE: |
332 #endif // defined(VIDEO_HOLE) | 364 #endif // defined(VIDEO_HOLE) |
333 break; | 365 break; |
334 } | 366 } |
335 NOTREACHED() << "Unsupported video frame format/plane: " | 367 NOTREACHED() << "Unsupported video frame format/plane: " |
336 << format << "/" << plane; | 368 << format << "/" << plane; |
337 return 0; | 369 return gfx::Size(); |
370 } | |
371 | |
372 size_t VideoFrame::PlaneAllocationSize(Format format, | |
373 size_t plane, | |
374 const gfx::Size& coded_size) { | |
375 // VideoFrame formats are (so far) all YUV and 1 byte per sample. | |
376 return PlaneSize(format, plane, coded_size).GetArea(); | |
338 } | 377 } |
339 | 378 |
340 // Release data allocated by AllocateYUV(). | 379 // Release data allocated by AllocateYUV(). |
341 static void ReleaseData(uint8* data) { | 380 static void ReleaseData(uint8* data) { |
342 DCHECK(data); | 381 DCHECK(data); |
343 base::AlignedFree(data); | 382 base::AlignedFree(data); |
344 } | 383 } |
345 | 384 |
346 void VideoFrame::AllocateYUV() { | 385 void VideoFrame::AllocateYUV() { |
347 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16 || | 386 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16 || |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
403 const gfx::Size& natural_size, | 442 const gfx::Size& natural_size, |
404 base::TimeDelta timestamp, | 443 base::TimeDelta timestamp, |
405 bool end_of_stream) | 444 bool end_of_stream) |
406 : format_(format), | 445 : format_(format), |
407 coded_size_(coded_size), | 446 coded_size_(coded_size), |
408 visible_rect_(visible_rect), | 447 visible_rect_(visible_rect), |
409 natural_size_(natural_size), | 448 natural_size_(natural_size), |
410 shared_memory_handle_(base::SharedMemory::NULLHandle()), | 449 shared_memory_handle_(base::SharedMemory::NULLHandle()), |
411 timestamp_(timestamp), | 450 timestamp_(timestamp), |
412 end_of_stream_(end_of_stream) { | 451 end_of_stream_(end_of_stream) { |
452 DCHECK(format_ == VideoFrame::UNKNOWN || | |
453 IsValidConfig(format_, coded_size_, visible_rect_, natural_size_)); | |
454 DCHECK_GE(visible_rect_.x(), 0); | |
455 DCHECK_GE(visible_rect_.y(), 0); | |
456 DCHECK_LE(visible_rect_.right(), coded_size_.width()); | |
457 DCHECK_LE(visible_rect_.height(), coded_size_.height()); | |
Ami GONE FROM CHROMIUM
2014/02/27 01:20:12
These last 4 are part of IsValidConfig(), so why D
sheu
2014/02/28 00:40:19
Gonna fix up the check paths a bit.
| |
458 | |
413 memset(&strides_, 0, sizeof(strides_)); | 459 memset(&strides_, 0, sizeof(strides_)); |
414 memset(&data_, 0, sizeof(data_)); | 460 memset(&data_, 0, sizeof(data_)); |
415 } | 461 } |
416 | 462 |
417 VideoFrame::~VideoFrame() { | 463 VideoFrame::~VideoFrame() { |
418 if (!mailbox_holder_release_cb_.is_null()) { | 464 if (!mailbox_holder_release_cb_.is_null()) { |
419 base::ResetAndReturn(&mailbox_holder_release_cb_) | 465 base::ResetAndReturn(&mailbox_holder_release_cb_) |
420 .Run(mailbox_holder_.Pass()); | 466 .Run(mailbox_holder_.Pass()); |
421 } | 467 } |
422 if (!no_longer_needed_cb_.is_null()) | 468 if (!no_longer_needed_cb_.is_null()) |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
504 break; | 550 break; |
505 for (int row = 0; row < rows(plane); ++row) { | 551 for (int row = 0; row < rows(plane); ++row) { |
506 base::MD5Update(context, base::StringPiece( | 552 base::MD5Update(context, base::StringPiece( |
507 reinterpret_cast<char*>(data(plane) + stride(plane) * row), | 553 reinterpret_cast<char*>(data(plane) + stride(plane) * row), |
508 row_bytes(plane))); | 554 row_bytes(plane))); |
509 } | 555 } |
510 } | 556 } |
511 } | 557 } |
512 | 558 |
513 } // namespace media | 559 } // namespace media |
OLD | NEW |