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 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 14 matching lines...) Expand all Loading... | |
66 } | 80 } |
67 NOTREACHED() << "Invalid videoframe format provided: " << format; | 81 NOTREACHED() << "Invalid videoframe format provided: " << format; |
68 return ""; | 82 return ""; |
69 } | 83 } |
70 | 84 |
71 // static | 85 // static |
72 bool VideoFrame::IsValidConfig(VideoFrame::Format format, | 86 bool VideoFrame::IsValidConfig(VideoFrame::Format format, |
73 const gfx::Size& coded_size, | 87 const gfx::Size& coded_size, |
74 const gfx::Rect& visible_rect, | 88 const gfx::Rect& visible_rect, |
75 const gfx::Size& natural_size) { | 89 const gfx::Size& natural_size) { |
76 return (format != VideoFrame::UNKNOWN && | 90 // Check maximum limits for all formats. |
77 !coded_size.IsEmpty() && | 91 if (coded_size.GetArea() > limits::kMaxCanvas || |
78 coded_size.GetArea() <= limits::kMaxCanvas && | 92 coded_size.width() > limits::kMaxDimension || |
79 coded_size.width() <= limits::kMaxDimension && | 93 coded_size.height() > limits::kMaxDimension || |
80 coded_size.height() <= limits::kMaxDimension && | 94 visible_rect.x() < 0 || visible_rect.y() < 0 || |
81 !visible_rect.IsEmpty() && | 95 visible_rect.right() > coded_size.width() || |
82 visible_rect.x() >= 0 && visible_rect.y() >= 0 && | 96 visible_rect.bottom() > coded_size.height() || |
83 visible_rect.right() <= coded_size.width() && | 97 natural_size.GetArea() > limits::kMaxCanvas || |
84 visible_rect.bottom() <= coded_size.height() && | 98 natural_size.width() > limits::kMaxDimension || |
85 !natural_size.IsEmpty() && | 99 natural_size.height() > limits::kMaxDimension) |
86 natural_size.GetArea() <= limits::kMaxCanvas && | 100 return false; |
87 natural_size.width() <= limits::kMaxDimension && | 101 |
88 natural_size.height() <= limits::kMaxDimension); | 102 // Check format-specific width/height requirements. |
103 switch (format) { | |
104 case VideoFrame::UNKNOWN: | |
105 return (coded_size.IsEmpty() && visible_rect.IsEmpty() && | |
106 natural_size.IsEmpty()); | |
107 case VideoFrame::YV12: | |
108 case VideoFrame::YV12J: | |
109 case VideoFrame::I420: | |
110 case VideoFrame::YV12A: | |
111 // YUV formats have width/height requirements due to chroma subsampling. | |
112 if (static_cast<size_t>(coded_size.height()) < | |
113 RoundUp(visible_rect.bottom(), 2)) | |
Sergey Ulanov
2014/06/07 02:42:06
if visible_rect=coded_size this essentially requir
| |
114 return false; | |
115 // Fallthrough. | |
116 case VideoFrame::YV16: | |
117 if (static_cast<size_t>(coded_size.width()) < | |
118 RoundUp(visible_rect.right(), 2)) | |
119 return false; | |
120 break; | |
121 case VideoFrame::NATIVE_TEXTURE: | |
122 #if defined(VIDEO_HOLE) | |
123 case VideoFrame::HOLE: | |
124 #endif // defined(VIDEO_HOLE) | |
125 // NATIVE_TEXTURE and HOLE have no software-allocated buffers and are | |
126 // allowed to skip the below check and be empty. | |
127 return true; | |
128 } | |
129 | |
130 // Check that software-allocated buffer formats are not empty. | |
131 return (!coded_size.IsEmpty() && !visible_rect.IsEmpty() && | |
132 !natural_size.IsEmpty()); | |
89 } | 133 } |
90 | 134 |
91 // static | 135 // static |
92 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture( | 136 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture( |
93 scoped_ptr<gpu::MailboxHolder> mailbox_holder, | 137 scoped_ptr<gpu::MailboxHolder> mailbox_holder, |
94 const ReleaseMailboxCB& mailbox_holder_release_cb, | 138 const ReleaseMailboxCB& mailbox_holder_release_cb, |
95 const gfx::Size& coded_size, | 139 const gfx::Size& coded_size, |
96 const gfx::Rect& visible_rect, | 140 const gfx::Rect& visible_rect, |
97 const gfx::Size& natural_size, | 141 const gfx::Size& natural_size, |
98 base::TimeDelta timestamp, | 142 base::TimeDelta timestamp, |
(...skipping 21 matching lines...) Expand all Loading... | |
120 scoped_refptr<VideoFrame> VideoFrame::WrapExternalPackedMemory( | 164 scoped_refptr<VideoFrame> VideoFrame::WrapExternalPackedMemory( |
121 Format format, | 165 Format format, |
122 const gfx::Size& coded_size, | 166 const gfx::Size& coded_size, |
123 const gfx::Rect& visible_rect, | 167 const gfx::Rect& visible_rect, |
124 const gfx::Size& natural_size, | 168 const gfx::Size& natural_size, |
125 uint8* data, | 169 uint8* data, |
126 size_t data_size, | 170 size_t data_size, |
127 base::SharedMemoryHandle handle, | 171 base::SharedMemoryHandle handle, |
128 base::TimeDelta timestamp, | 172 base::TimeDelta timestamp, |
129 const base::Closure& no_longer_needed_cb) { | 173 const base::Closure& no_longer_needed_cb) { |
174 if (!IsValidConfig(format, coded_size, visible_rect, natural_size)) | |
175 return NULL; | |
130 if (data_size < AllocationSize(format, coded_size)) | 176 if (data_size < AllocationSize(format, coded_size)) |
131 return NULL; | 177 return NULL; |
132 | 178 |
133 switch (format) { | 179 switch (format) { |
134 case I420: { | 180 case I420: { |
135 scoped_refptr<VideoFrame> frame(new VideoFrame( | 181 scoped_refptr<VideoFrame> frame(new VideoFrame( |
136 format, coded_size, visible_rect, natural_size, timestamp, false)); | 182 format, coded_size, visible_rect, natural_size, timestamp, false)); |
137 frame->shared_memory_handle_ = handle; | 183 frame->shared_memory_handle_ = handle; |
138 frame->strides_[kYPlane] = coded_size.width(); | 184 frame->strides_[kYPlane] = coded_size.width(); |
139 frame->strides_[kUPlane] = coded_size.width() / 2; | 185 frame->strides_[kUPlane] = coded_size.width() / 2; |
(...skipping 17 matching lines...) Expand all Loading... | |
157 const gfx::Rect& visible_rect, | 203 const gfx::Rect& visible_rect, |
158 const gfx::Size& natural_size, | 204 const gfx::Size& natural_size, |
159 int32 y_stride, | 205 int32 y_stride, |
160 int32 u_stride, | 206 int32 u_stride, |
161 int32 v_stride, | 207 int32 v_stride, |
162 uint8* y_data, | 208 uint8* y_data, |
163 uint8* u_data, | 209 uint8* u_data, |
164 uint8* v_data, | 210 uint8* v_data, |
165 base::TimeDelta timestamp, | 211 base::TimeDelta timestamp, |
166 const base::Closure& no_longer_needed_cb) { | 212 const base::Closure& no_longer_needed_cb) { |
167 DCHECK(format == YV12 || format == YV16 || format == I420) << format; | 213 if (!IsValidConfig(format, coded_size, visible_rect, natural_size)) |
214 return NULL; | |
215 | |
168 scoped_refptr<VideoFrame> frame(new VideoFrame( | 216 scoped_refptr<VideoFrame> frame(new VideoFrame( |
169 format, coded_size, visible_rect, natural_size, timestamp, false)); | 217 format, coded_size, visible_rect, natural_size, timestamp, false)); |
170 frame->strides_[kYPlane] = y_stride; | 218 frame->strides_[kYPlane] = y_stride; |
171 frame->strides_[kUPlane] = u_stride; | 219 frame->strides_[kUPlane] = u_stride; |
172 frame->strides_[kVPlane] = v_stride; | 220 frame->strides_[kVPlane] = v_stride; |
173 frame->data_[kYPlane] = y_data; | 221 frame->data_[kYPlane] = y_data; |
174 frame->data_[kUPlane] = u_data; | 222 frame->data_[kUPlane] = u_data; |
175 frame->data_[kVPlane] = v_data; | 223 frame->data_[kVPlane] = v_data; |
176 frame->no_longer_needed_cb_ = no_longer_needed_cb; | 224 frame->no_longer_needed_cb_ = no_longer_needed_cb; |
177 return frame; | 225 return frame; |
(...skipping 27 matching lines...) Expand all Loading... | |
205 gfx::Size(), | 253 gfx::Size(), |
206 kNoTimestamp(), | 254 kNoTimestamp(), |
207 true); | 255 true); |
208 } | 256 } |
209 | 257 |
210 // static | 258 // static |
211 scoped_refptr<VideoFrame> VideoFrame::CreateColorFrame( | 259 scoped_refptr<VideoFrame> VideoFrame::CreateColorFrame( |
212 const gfx::Size& size, | 260 const gfx::Size& size, |
213 uint8 y, uint8 u, uint8 v, | 261 uint8 y, uint8 u, uint8 v, |
214 base::TimeDelta timestamp) { | 262 base::TimeDelta timestamp) { |
215 DCHECK(IsValidConfig(VideoFrame::YV12, size, gfx::Rect(size), size)); | |
216 scoped_refptr<VideoFrame> frame = VideoFrame::CreateFrame( | 263 scoped_refptr<VideoFrame> frame = VideoFrame::CreateFrame( |
217 VideoFrame::YV12, size, gfx::Rect(size), size, timestamp); | 264 VideoFrame::YV12, size, gfx::Rect(size), size, timestamp); |
218 FillYUV(frame.get(), y, u, v); | 265 FillYUV(frame.get(), y, u, v); |
219 return frame; | 266 return frame; |
220 } | 267 } |
221 | 268 |
222 // static | 269 // static |
223 scoped_refptr<VideoFrame> VideoFrame::CreateBlackFrame(const gfx::Size& size) { | 270 scoped_refptr<VideoFrame> VideoFrame::CreateBlackFrame(const gfx::Size& size) { |
224 const uint8 kBlackY = 0x00; | 271 const uint8 kBlackY = 0x00; |
225 const uint8 kBlackUV = 0x80; | 272 const uint8 kBlackUV = 0x80; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
260 return 3; | 307 return 3; |
261 case VideoFrame::YV12A: | 308 case VideoFrame::YV12A: |
262 return 4; | 309 return 4; |
263 case VideoFrame::UNKNOWN: | 310 case VideoFrame::UNKNOWN: |
264 break; | 311 break; |
265 } | 312 } |
266 NOTREACHED() << "Unsupported video frame format: " << format; | 313 NOTREACHED() << "Unsupported video frame format: " << format; |
267 return 0; | 314 return 0; |
268 } | 315 } |
269 | 316 |
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 | 317 |
276 // static | 318 // static |
277 size_t VideoFrame::AllocationSize(Format format, const gfx::Size& coded_size) { | 319 size_t VideoFrame::AllocationSize(Format format, const gfx::Size& coded_size) { |
278 size_t total = 0; | 320 size_t total = 0; |
279 for (size_t i = 0; i < NumPlanes(format); ++i) | 321 for (size_t i = 0; i < NumPlanes(format); ++i) |
280 total += PlaneAllocationSize(format, i, coded_size); | 322 total += PlaneAllocationSize(format, i, coded_size); |
281 return total; | 323 return total; |
282 } | 324 } |
283 | 325 |
284 // static | 326 // static |
285 size_t VideoFrame::PlaneAllocationSize(Format format, | 327 gfx::Size VideoFrame::PlaneSize(Format format, |
286 size_t plane, | 328 size_t plane, |
287 const gfx::Size& coded_size) { | 329 const gfx::Size& coded_size) { |
288 const size_t area = | 330 const int width = RoundUp(coded_size.width(), 2); |
289 RoundUp(coded_size.width(), 2) * RoundUp(coded_size.height(), 2); | 331 const int height = RoundUp(coded_size.height(), 2); |
290 switch (format) { | 332 switch (format) { |
291 case VideoFrame::YV12: | 333 case VideoFrame::YV12: |
292 case VideoFrame::YV12J: | 334 case VideoFrame::YV12J: |
293 case VideoFrame::I420: { | 335 case VideoFrame::I420: { |
294 switch (plane) { | 336 switch (plane) { |
295 case VideoFrame::kYPlane: | 337 case VideoFrame::kYPlane: |
296 return area; | 338 return gfx::Size(width, height); |
297 case VideoFrame::kUPlane: | 339 case VideoFrame::kUPlane: |
298 case VideoFrame::kVPlane: | 340 case VideoFrame::kVPlane: |
299 return area / 4; | 341 return gfx::Size(width / 2, height / 2); |
300 default: | 342 default: |
301 break; | 343 break; |
302 } | 344 } |
303 } | 345 } |
304 case VideoFrame::YV12A: { | 346 case VideoFrame::YV12A: { |
305 switch (plane) { | 347 switch (plane) { |
306 case VideoFrame::kYPlane: | 348 case VideoFrame::kYPlane: |
307 case VideoFrame::kAPlane: | 349 case VideoFrame::kAPlane: |
308 return area; | 350 return gfx::Size(width, height); |
309 case VideoFrame::kUPlane: | 351 case VideoFrame::kUPlane: |
310 case VideoFrame::kVPlane: | 352 case VideoFrame::kVPlane: |
311 return area / 4; | 353 return gfx::Size(width / 2, height / 2); |
312 default: | 354 default: |
313 break; | 355 break; |
314 } | 356 } |
315 } | 357 } |
316 case VideoFrame::YV16: { | 358 case VideoFrame::YV16: { |
317 switch (plane) { | 359 switch (plane) { |
318 case VideoFrame::kYPlane: | 360 case VideoFrame::kYPlane: |
319 return area; | 361 return gfx::Size(width, height); |
320 case VideoFrame::kUPlane: | 362 case VideoFrame::kUPlane: |
321 case VideoFrame::kVPlane: | 363 case VideoFrame::kVPlane: |
322 return area / 2; | 364 return gfx::Size(width / 2, height); |
323 default: | 365 default: |
324 break; | 366 break; |
325 } | 367 } |
326 } | 368 } |
327 case VideoFrame::UNKNOWN: | 369 case VideoFrame::UNKNOWN: |
328 case VideoFrame::NATIVE_TEXTURE: | 370 case VideoFrame::NATIVE_TEXTURE: |
329 #if defined(VIDEO_HOLE) | 371 #if defined(VIDEO_HOLE) |
330 case VideoFrame::HOLE: | 372 case VideoFrame::HOLE: |
331 #endif // defined(VIDEO_HOLE) | 373 #endif // defined(VIDEO_HOLE) |
332 break; | 374 break; |
333 } | 375 } |
334 NOTREACHED() << "Unsupported video frame format/plane: " | 376 NOTREACHED() << "Unsupported video frame format/plane: " |
335 << format << "/" << plane; | 377 << format << "/" << plane; |
336 return 0; | 378 return gfx::Size(); |
379 } | |
380 | |
381 size_t VideoFrame::PlaneAllocationSize(Format format, | |
382 size_t plane, | |
383 const gfx::Size& coded_size) { | |
384 // VideoFrame formats are (so far) all YUV and 1 byte per sample. | |
385 return PlaneSize(format, plane, coded_size).GetArea(); | |
337 } | 386 } |
338 | 387 |
339 // Release data allocated by AllocateYUV(). | 388 // Release data allocated by AllocateYUV(). |
340 static void ReleaseData(uint8* data) { | 389 static void ReleaseData(uint8* data) { |
341 DCHECK(data); | 390 DCHECK(data); |
342 base::AlignedFree(data); | 391 base::AlignedFree(data); |
343 } | 392 } |
344 | 393 |
345 void VideoFrame::AllocateYUV() { | 394 void VideoFrame::AllocateYUV() { |
346 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16 || | 395 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16 || |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
402 const gfx::Size& natural_size, | 451 const gfx::Size& natural_size, |
403 base::TimeDelta timestamp, | 452 base::TimeDelta timestamp, |
404 bool end_of_stream) | 453 bool end_of_stream) |
405 : format_(format), | 454 : format_(format), |
406 coded_size_(coded_size), | 455 coded_size_(coded_size), |
407 visible_rect_(visible_rect), | 456 visible_rect_(visible_rect), |
408 natural_size_(natural_size), | 457 natural_size_(natural_size), |
409 shared_memory_handle_(base::SharedMemory::NULLHandle()), | 458 shared_memory_handle_(base::SharedMemory::NULLHandle()), |
410 timestamp_(timestamp), | 459 timestamp_(timestamp), |
411 end_of_stream_(end_of_stream) { | 460 end_of_stream_(end_of_stream) { |
461 DCHECK(IsValidConfig(format_, coded_size_, visible_rect_, natural_size_)); | |
462 | |
412 memset(&strides_, 0, sizeof(strides_)); | 463 memset(&strides_, 0, sizeof(strides_)); |
413 memset(&data_, 0, sizeof(data_)); | 464 memset(&data_, 0, sizeof(data_)); |
414 } | 465 } |
415 | 466 |
416 VideoFrame::~VideoFrame() { | 467 VideoFrame::~VideoFrame() { |
417 if (!mailbox_holder_release_cb_.is_null()) { | 468 if (!mailbox_holder_release_cb_.is_null()) { |
418 base::ResetAndReturn(&mailbox_holder_release_cb_) | 469 base::ResetAndReturn(&mailbox_holder_release_cb_) |
419 .Run(mailbox_holder_.Pass()); | 470 .Run(mailbox_holder_.Pass()); |
420 } | 471 } |
421 if (!no_longer_needed_cb_.is_null()) | 472 if (!no_longer_needed_cb_.is_null()) |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
503 break; | 554 break; |
504 for (int row = 0; row < rows(plane); ++row) { | 555 for (int row = 0; row < rows(plane); ++row) { |
505 base::MD5Update(context, base::StringPiece( | 556 base::MD5Update(context, base::StringPiece( |
506 reinterpret_cast<char*>(data(plane) + stride(plane) * row), | 557 reinterpret_cast<char*>(data(plane) + stride(plane) * row), |
507 row_bytes(plane))); | 558 row_bytes(plane))); |
508 } | 559 } |
509 } | 560 } |
510 } | 561 } |
511 | 562 |
512 } // namespace media | 563 } // namespace media |
OLD | NEW |