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 if the |
30 format, coded_size, visible_rect, natural_size, timestamp, false)); | 30 // request does not line up on sample boundaries. |
| 31 gfx::Size new_coded_size(coded_size); |
31 switch (format) { | 32 switch (format) { |
32 case VideoFrame::YV12: | 33 case VideoFrame::YV12: |
33 case VideoFrame::YV12A: | 34 case VideoFrame::YV12A: |
34 case VideoFrame::YV16: | |
35 case VideoFrame::I420: | 35 case VideoFrame::I420: |
36 case VideoFrame::YV12J: | 36 case VideoFrame::YV12J: |
37 frame->AllocateYUV(); | 37 new_coded_size.set_height((new_coded_size.height() + 1) / 2 * 2); |
| 38 // Fallthrough. |
| 39 case VideoFrame::YV16: |
| 40 new_coded_size.set_width((new_coded_size.width() + 1) / 2 * 2); |
38 break; | 41 break; |
39 default: | 42 default: |
40 LOG(FATAL) << "Unsupported frame format: " << format; | 43 LOG(FATAL) << "Only YUV formats supported: " << format; |
| 44 return NULL; |
41 } | 45 } |
| 46 DCHECK(IsValidConfig(format, new_coded_size, visible_rect, natural_size)); |
| 47 scoped_refptr<VideoFrame> frame(new VideoFrame( |
| 48 format, new_coded_size, visible_rect, natural_size, timestamp, false)); |
| 49 frame->AllocateYUV(); |
42 return frame; | 50 return frame; |
43 } | 51 } |
44 | 52 |
45 // static | 53 // static |
46 std::string VideoFrame::FormatToString(VideoFrame::Format format) { | 54 std::string VideoFrame::FormatToString(VideoFrame::Format format) { |
47 switch (format) { | 55 switch (format) { |
48 case VideoFrame::UNKNOWN: | 56 case VideoFrame::UNKNOWN: |
49 return "UNKNOWN"; | 57 return "UNKNOWN"; |
50 case VideoFrame::YV12: | 58 case VideoFrame::YV12: |
51 return "YV12"; | 59 return "YV12"; |
(...skipping 16 matching lines...) Expand all Loading... |
68 } | 76 } |
69 NOTREACHED() << "Invalid videoframe format provided: " << format; | 77 NOTREACHED() << "Invalid videoframe format provided: " << format; |
70 return ""; | 78 return ""; |
71 } | 79 } |
72 | 80 |
73 // static | 81 // static |
74 bool VideoFrame::IsValidConfig(VideoFrame::Format format, | 82 bool VideoFrame::IsValidConfig(VideoFrame::Format format, |
75 const gfx::Size& coded_size, | 83 const gfx::Size& coded_size, |
76 const gfx::Rect& visible_rect, | 84 const gfx::Rect& visible_rect, |
77 const gfx::Size& natural_size) { | 85 const gfx::Size& natural_size) { |
78 return (format != VideoFrame::UNKNOWN && | 86 switch (format) { |
79 !coded_size.IsEmpty() && | 87 case VideoFrame::UNKNOWN: |
80 coded_size.GetArea() <= limits::kMaxCanvas && | 88 return (coded_size.IsEmpty() && visible_rect.IsEmpty() && |
81 coded_size.width() <= limits::kMaxDimension && | 89 natural_size.IsEmpty()); |
82 coded_size.height() <= limits::kMaxDimension && | 90 case VideoFrame::YV12: |
83 !visible_rect.IsEmpty() && | 91 case VideoFrame::YV12J: |
84 visible_rect.x() >= 0 && visible_rect.y() >= 0 && | 92 case VideoFrame::I420: |
85 visible_rect.right() <= coded_size.width() && | 93 case VideoFrame::YV12A: |
86 visible_rect.bottom() <= coded_size.height() && | 94 // YUV formats have width/height requirements due to chroma subsampling. |
87 !natural_size.IsEmpty() && | 95 if (coded_size.height() % 2 != 0) |
88 natural_size.GetArea() <= limits::kMaxCanvas && | 96 return false; |
89 natural_size.width() <= limits::kMaxDimension && | 97 // Fallthrough. |
90 natural_size.height() <= limits::kMaxDimension); | 98 case VideoFrame::YV16: |
| 99 if (coded_size.width() % 2 != 0) |
| 100 return false; |
| 101 break; |
| 102 case VideoFrame::NATIVE_TEXTURE: |
| 103 #if defined(VIDEO_HOLE) |
| 104 case VideoFrame::HOLE: |
| 105 #endif // defined(VIDEO_HOLE) |
| 106 break; |
| 107 case VideoFrame::HISTOGRAM_MAX: |
| 108 NOTREACHED(); |
| 109 return false; |
| 110 } |
| 111 |
| 112 if (coded_size.IsEmpty() || coded_size.GetArea() > limits::kMaxCanvas || |
| 113 coded_size.width() > limits::kMaxDimension || |
| 114 coded_size.height() > limits::kMaxDimension || visible_rect.IsEmpty() || |
| 115 visible_rect.x() < 0 || visible_rect.y() < 0 || |
| 116 visible_rect.right() > coded_size.width() || |
| 117 visible_rect.bottom() > coded_size.height() || natural_size.IsEmpty() || |
| 118 natural_size.GetArea() > limits::kMaxCanvas || |
| 119 natural_size.width() > limits::kMaxDimension || |
| 120 natural_size.height() > limits::kMaxDimension) |
| 121 return false; |
| 122 |
| 123 return true; |
91 } | 124 } |
92 | 125 |
93 // static | 126 // static |
94 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture( | 127 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture( |
95 scoped_ptr<gpu::MailboxHolder> mailbox_holder, | 128 scoped_ptr<gpu::MailboxHolder> mailbox_holder, |
96 const ReleaseMailboxCB& mailbox_holder_release_cb, | 129 const ReleaseMailboxCB& mailbox_holder_release_cb, |
97 const gfx::Size& coded_size, | 130 const gfx::Size& coded_size, |
98 const gfx::Rect& visible_rect, | 131 const gfx::Rect& visible_rect, |
99 const gfx::Size& natural_size, | 132 const gfx::Size& natural_size, |
100 base::TimeDelta timestamp, | 133 base::TimeDelta timestamp, |
(...skipping 21 matching lines...) Expand all Loading... |
122 scoped_refptr<VideoFrame> VideoFrame::WrapExternalPackedMemory( | 155 scoped_refptr<VideoFrame> VideoFrame::WrapExternalPackedMemory( |
123 Format format, | 156 Format format, |
124 const gfx::Size& coded_size, | 157 const gfx::Size& coded_size, |
125 const gfx::Rect& visible_rect, | 158 const gfx::Rect& visible_rect, |
126 const gfx::Size& natural_size, | 159 const gfx::Size& natural_size, |
127 uint8* data, | 160 uint8* data, |
128 size_t data_size, | 161 size_t data_size, |
129 base::SharedMemoryHandle handle, | 162 base::SharedMemoryHandle handle, |
130 base::TimeDelta timestamp, | 163 base::TimeDelta timestamp, |
131 const base::Closure& no_longer_needed_cb) { | 164 const base::Closure& no_longer_needed_cb) { |
| 165 if (!IsValidConfig(format, coded_size, visible_rect, natural_size)) |
| 166 return NULL; |
132 if (data_size < AllocationSize(format, coded_size)) | 167 if (data_size < AllocationSize(format, coded_size)) |
133 return NULL; | 168 return NULL; |
134 | 169 |
135 switch (format) { | 170 switch (format) { |
136 case I420: { | 171 case I420: { |
137 scoped_refptr<VideoFrame> frame(new VideoFrame( | 172 scoped_refptr<VideoFrame> frame(new VideoFrame( |
138 format, coded_size, visible_rect, natural_size, timestamp, false)); | 173 format, coded_size, visible_rect, natural_size, timestamp, false)); |
139 frame->shared_memory_handle_ = handle; | 174 frame->shared_memory_handle_ = handle; |
140 frame->strides_[kYPlane] = coded_size.width(); | 175 frame->strides_[kYPlane] = coded_size.width(); |
141 frame->strides_[kUPlane] = coded_size.width() / 2; | 176 frame->strides_[kUPlane] = coded_size.width() / 2; |
(...skipping 17 matching lines...) Expand all Loading... |
159 const gfx::Rect& visible_rect, | 194 const gfx::Rect& visible_rect, |
160 const gfx::Size& natural_size, | 195 const gfx::Size& natural_size, |
161 int32 y_stride, | 196 int32 y_stride, |
162 int32 u_stride, | 197 int32 u_stride, |
163 int32 v_stride, | 198 int32 v_stride, |
164 uint8* y_data, | 199 uint8* y_data, |
165 uint8* u_data, | 200 uint8* u_data, |
166 uint8* v_data, | 201 uint8* v_data, |
167 base::TimeDelta timestamp, | 202 base::TimeDelta timestamp, |
168 const base::Closure& no_longer_needed_cb) { | 203 const base::Closure& no_longer_needed_cb) { |
169 DCHECK(format == YV12 || format == YV16 || format == I420) << format; | 204 if (!IsValidConfig(format, coded_size, visible_rect, natural_size)) |
| 205 return NULL; |
| 206 |
170 scoped_refptr<VideoFrame> frame(new VideoFrame( | 207 scoped_refptr<VideoFrame> frame(new VideoFrame( |
171 format, coded_size, visible_rect, natural_size, timestamp, false)); | 208 format, coded_size, visible_rect, natural_size, timestamp, false)); |
172 frame->strides_[kYPlane] = y_stride; | 209 frame->strides_[kYPlane] = y_stride; |
173 frame->strides_[kUPlane] = u_stride; | 210 frame->strides_[kUPlane] = u_stride; |
174 frame->strides_[kVPlane] = v_stride; | 211 frame->strides_[kVPlane] = v_stride; |
175 frame->data_[kYPlane] = y_data; | 212 frame->data_[kYPlane] = y_data; |
176 frame->data_[kUPlane] = u_data; | 213 frame->data_[kUPlane] = u_data; |
177 frame->data_[kVPlane] = v_data; | 214 frame->data_[kVPlane] = v_data; |
178 frame->no_longer_needed_cb_ = no_longer_needed_cb; | 215 frame->no_longer_needed_cb_ = no_longer_needed_cb; |
179 return frame; | 216 return frame; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 312 |
276 // static | 313 // static |
277 size_t VideoFrame::AllocationSize(Format format, const gfx::Size& coded_size) { | 314 size_t VideoFrame::AllocationSize(Format format, const gfx::Size& coded_size) { |
278 size_t total = 0; | 315 size_t total = 0; |
279 for (size_t i = 0; i < NumPlanes(format); ++i) | 316 for (size_t i = 0; i < NumPlanes(format); ++i) |
280 total += PlaneAllocationSize(format, i, coded_size); | 317 total += PlaneAllocationSize(format, i, coded_size); |
281 return total; | 318 return total; |
282 } | 319 } |
283 | 320 |
284 // static | 321 // static |
285 size_t VideoFrame::PlaneAllocationSize(Format format, | 322 gfx::Size VideoFrame::PlaneSize(Format format, |
286 size_t plane, | 323 size_t plane, |
287 const gfx::Size& coded_size) { | 324 const gfx::Size& coded_size) { |
288 const size_t area = | 325 const int width = RoundUp(coded_size.width(), 2); |
289 RoundUp(coded_size.width(), 2) * RoundUp(coded_size.height(), 2); | 326 const int height = RoundUp(coded_size.height(), 2); |
290 switch (format) { | 327 switch (format) { |
291 case VideoFrame::YV12: | 328 case VideoFrame::YV12: |
292 case VideoFrame::YV12J: | 329 case VideoFrame::YV12J: |
293 case VideoFrame::I420: { | 330 case VideoFrame::I420: { |
294 switch (plane) { | 331 switch (plane) { |
295 case VideoFrame::kYPlane: | 332 case VideoFrame::kYPlane: |
296 return area; | 333 return gfx::Size(width, height); |
297 case VideoFrame::kUPlane: | 334 case VideoFrame::kUPlane: |
298 case VideoFrame::kVPlane: | 335 case VideoFrame::kVPlane: |
299 return area / 4; | 336 return gfx::Size(width / 2, height / 2); |
300 default: | 337 default: |
301 break; | 338 break; |
302 } | 339 } |
303 } | 340 } |
304 case VideoFrame::YV12A: { | 341 case VideoFrame::YV12A: { |
305 switch (plane) { | 342 switch (plane) { |
306 case VideoFrame::kYPlane: | 343 case VideoFrame::kYPlane: |
307 case VideoFrame::kAPlane: | 344 case VideoFrame::kAPlane: |
308 return area; | 345 return gfx::Size(width, height); |
309 case VideoFrame::kUPlane: | 346 case VideoFrame::kUPlane: |
310 case VideoFrame::kVPlane: | 347 case VideoFrame::kVPlane: |
311 return area / 4; | 348 return gfx::Size(width / 2, height / 2); |
312 default: | 349 default: |
313 break; | 350 break; |
314 } | 351 } |
315 } | 352 } |
316 case VideoFrame::YV16: { | 353 case VideoFrame::YV16: { |
317 switch (plane) { | 354 switch (plane) { |
318 case VideoFrame::kYPlane: | 355 case VideoFrame::kYPlane: |
319 return area; | 356 return gfx::Size(width, height); |
320 case VideoFrame::kUPlane: | 357 case VideoFrame::kUPlane: |
321 case VideoFrame::kVPlane: | 358 case VideoFrame::kVPlane: |
322 return area / 2; | 359 return gfx::Size(width / 2, height); |
323 default: | 360 default: |
324 break; | 361 break; |
325 } | 362 } |
326 } | 363 } |
327 case VideoFrame::UNKNOWN: | 364 case VideoFrame::UNKNOWN: |
328 case VideoFrame::NATIVE_TEXTURE: | 365 case VideoFrame::NATIVE_TEXTURE: |
329 case VideoFrame::HISTOGRAM_MAX: | 366 case VideoFrame::HISTOGRAM_MAX: |
330 #if defined(VIDEO_HOLE) | 367 #if defined(VIDEO_HOLE) |
331 case VideoFrame::HOLE: | 368 case VideoFrame::HOLE: |
332 #endif // defined(VIDEO_HOLE) | 369 #endif // defined(VIDEO_HOLE) |
333 break; | 370 break; |
334 } | 371 } |
335 NOTREACHED() << "Unsupported video frame format/plane: " | 372 NOTREACHED() << "Unsupported video frame format/plane: " |
336 << format << "/" << plane; | 373 << format << "/" << plane; |
337 return 0; | 374 return gfx::Size(); |
| 375 } |
| 376 |
| 377 size_t VideoFrame::PlaneAllocationSize(Format format, |
| 378 size_t plane, |
| 379 const gfx::Size& coded_size) { |
| 380 // VideoFrame formats are (so far) all YUV and 1 byte per sample. |
| 381 return PlaneSize(format, plane, coded_size).GetArea(); |
338 } | 382 } |
339 | 383 |
340 // Release data allocated by AllocateYUV(). | 384 // Release data allocated by AllocateYUV(). |
341 static void ReleaseData(uint8* data) { | 385 static void ReleaseData(uint8* data) { |
342 DCHECK(data); | 386 DCHECK(data); |
343 base::AlignedFree(data); | 387 base::AlignedFree(data); |
344 } | 388 } |
345 | 389 |
346 void VideoFrame::AllocateYUV() { | 390 void VideoFrame::AllocateYUV() { |
347 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16 || | 391 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, | 447 const gfx::Size& natural_size, |
404 base::TimeDelta timestamp, | 448 base::TimeDelta timestamp, |
405 bool end_of_stream) | 449 bool end_of_stream) |
406 : format_(format), | 450 : format_(format), |
407 coded_size_(coded_size), | 451 coded_size_(coded_size), |
408 visible_rect_(visible_rect), | 452 visible_rect_(visible_rect), |
409 natural_size_(natural_size), | 453 natural_size_(natural_size), |
410 shared_memory_handle_(base::SharedMemory::NULLHandle()), | 454 shared_memory_handle_(base::SharedMemory::NULLHandle()), |
411 timestamp_(timestamp), | 455 timestamp_(timestamp), |
412 end_of_stream_(end_of_stream) { | 456 end_of_stream_(end_of_stream) { |
| 457 DCHECK(IsValidConfig(format_, coded_size_, visible_rect_, natural_size_)); |
| 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 |