| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/base/video_frame_impl.h" | |
| 6 | |
| 7 namespace media { | |
| 8 | |
| 9 // static | |
| 10 void VideoFrameImpl::CreateFrame(VideoSurface::Format format, | |
| 11 size_t width, | |
| 12 size_t height, | |
| 13 base::TimeDelta timestamp, | |
| 14 base::TimeDelta duration, | |
| 15 scoped_refptr<VideoFrame>* frame_out) { | |
| 16 DCHECK(width > 0 && height > 0); | |
| 17 DCHECK(width * height < 100000000); | |
| 18 DCHECK(frame_out); | |
| 19 bool alloc_worked = false; | |
| 20 scoped_refptr<VideoFrameImpl> frame = | |
| 21 new VideoFrameImpl(format, width, height); | |
| 22 if (frame) { | |
| 23 frame->SetTimestamp(timestamp); | |
| 24 frame->SetDuration(duration); | |
| 25 switch (format) { | |
| 26 case VideoSurface::RGB555: | |
| 27 case VideoSurface::RGB565: | |
| 28 alloc_worked = frame->AllocateRGB(2u); | |
| 29 break; | |
| 30 case VideoSurface::RGB24: | |
| 31 alloc_worked = frame->AllocateRGB(3u); | |
| 32 break; | |
| 33 case VideoSurface::RGB32: | |
| 34 case VideoSurface::RGBA: | |
| 35 alloc_worked = frame->AllocateRGB(4u); | |
| 36 break; | |
| 37 case VideoSurface::YV12: | |
| 38 case VideoSurface::YV16: | |
| 39 alloc_worked = frame->AllocateYUV(); | |
| 40 break; | |
| 41 default: | |
| 42 NOTREACHED(); | |
| 43 alloc_worked = false; | |
| 44 break; | |
| 45 } | |
| 46 } | |
| 47 *frame_out = alloc_worked ? frame : NULL; | |
| 48 } | |
| 49 | |
| 50 // static | |
| 51 void VideoFrameImpl::CreateEmptyFrame(scoped_refptr<VideoFrame>* frame_out) { | |
| 52 *frame_out = new VideoFrameImpl(VideoSurface::EMPTY, 0, 0); | |
| 53 } | |
| 54 | |
| 55 // static | |
| 56 void VideoFrameImpl::CreateBlackFrame(int width, int height, | |
| 57 scoped_refptr<VideoFrame>* frame_out) { | |
| 58 DCHECK_GT(width, 0); | |
| 59 DCHECK_GT(height, 0); | |
| 60 | |
| 61 // Create our frame. | |
| 62 scoped_refptr<VideoFrame> frame; | |
| 63 const base::TimeDelta kZero; | |
| 64 VideoFrameImpl::CreateFrame(VideoSurface::YV12, width, height, kZero, kZero, | |
| 65 &frame); | |
| 66 DCHECK(frame); | |
| 67 | |
| 68 // Now set the data to YUV(0,128,128). | |
| 69 const uint8 kBlackY = 0x00; | |
| 70 const uint8 kBlackUV = 0x80; | |
| 71 VideoSurface surface; | |
| 72 frame->Lock(&surface); | |
| 73 DCHECK_EQ(VideoSurface::YV12, surface.format) << "Expected YV12 surface"; | |
| 74 | |
| 75 // Fill the Y plane. | |
| 76 for (size_t i = 0; i < surface.height; ++i) { | |
| 77 memset(surface.data[VideoSurface::kYPlane], kBlackY, surface.width); | |
| 78 surface.data[VideoSurface::kYPlane] | |
| 79 += surface.strides[VideoSurface::kYPlane]; | |
| 80 } | |
| 81 | |
| 82 // Fill the U and V planes. | |
| 83 for (size_t i = 0; i < (surface.height / 2); ++i) { | |
| 84 memset(surface.data[VideoSurface::kUPlane], kBlackUV, surface.width / 2); | |
| 85 memset(surface.data[VideoSurface::kVPlane], kBlackUV, surface.width / 2); | |
| 86 surface.data[VideoSurface::kUPlane] += | |
| 87 surface.strides[VideoSurface::kUPlane]; | |
| 88 surface.data[VideoSurface::kVPlane] += | |
| 89 surface.strides[VideoSurface::kVPlane]; | |
| 90 } | |
| 91 frame->Unlock(); | |
| 92 | |
| 93 // Success! | |
| 94 *frame_out = frame; | |
| 95 } | |
| 96 | |
| 97 static inline size_t RoundUp(size_t value, size_t alignment) { | |
| 98 // Check that |alignment| is a power of 2. | |
| 99 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); | |
| 100 return ((value + (alignment - 1)) & ~(alignment-1)); | |
| 101 } | |
| 102 | |
| 103 bool VideoFrameImpl::AllocateRGB(size_t bytes_per_pixel) { | |
| 104 // Round up to align at a 64-bit (8 byte) boundary for each row. This | |
| 105 // is sufficient for MMX reads (movq). | |
| 106 size_t bytes_per_row = RoundUp(surface_.width * bytes_per_pixel, 8); | |
| 107 surface_.planes = VideoSurface::kNumRGBPlanes; | |
| 108 surface_.strides[VideoSurface::kRGBPlane] = bytes_per_row; | |
| 109 surface_.data[VideoSurface::kRGBPlane] = new uint8[bytes_per_row * | |
| 110 surface_.height]; | |
| 111 DCHECK(surface_.data[VideoSurface::kRGBPlane]); | |
| 112 DCHECK(!(reinterpret_cast<intptr_t>( | |
| 113 surface_.data[VideoSurface::kRGBPlane]) & 7)); | |
| 114 COMPILE_ASSERT(0 == VideoSurface::kRGBPlane, RGB_data_must_be_index_0); | |
| 115 return (NULL != surface_.data[VideoSurface::kRGBPlane]); | |
| 116 } | |
| 117 | |
| 118 bool VideoFrameImpl::AllocateYUV() { | |
| 119 DCHECK(surface_.format == VideoSurface::YV12 || | |
| 120 surface_.format == VideoSurface::YV16); | |
| 121 // Align Y rows at 32-bit (4 byte) boundaries. The stride for both YV12 and | |
| 122 // YV16 is 1/2 of the stride of Y. For YV12, every row of bytes for U and V | |
| 123 // applies to two rows of Y (one byte of UV for 4 bytes of Y), so in the | |
| 124 // case of YV12 the strides are identical for the same width surface, but the | |
| 125 // number of bytes allocated for YV12 is 1/2 the amount for U & V as YV16. | |
| 126 // We also round the height of the surface allocated to be an even number | |
| 127 // to avoid any potential of faulting by code that attempts to access the Y | |
| 128 // values of the final row, but assumes that the last row of U & V applies to | |
| 129 // a full two rows of Y. | |
| 130 size_t alloc_height = RoundUp(surface_.height, 2); | |
| 131 size_t y_bytes_per_row = RoundUp(surface_.width, 4); | |
| 132 size_t uv_stride = RoundUp(y_bytes_per_row / 2, 4); | |
| 133 size_t y_bytes = alloc_height * y_bytes_per_row; | |
| 134 size_t uv_bytes = alloc_height * uv_stride; | |
| 135 if (surface_.format == VideoSurface::YV12) { | |
| 136 uv_bytes /= 2; | |
| 137 } | |
| 138 uint8* data = new uint8[y_bytes + (uv_bytes * 2)]; | |
| 139 if (data) { | |
| 140 surface_.planes = VideoSurface::kNumYUVPlanes; | |
| 141 COMPILE_ASSERT(0 == VideoSurface::kYPlane, y_plane_data_must_be_index_0); | |
| 142 surface_.data[VideoSurface::kYPlane] = data; | |
| 143 surface_.data[VideoSurface::kUPlane] = data + y_bytes; | |
| 144 surface_.data[VideoSurface::kVPlane] = data + y_bytes + uv_bytes; | |
| 145 surface_.strides[VideoSurface::kYPlane] = y_bytes_per_row; | |
| 146 surface_.strides[VideoSurface::kUPlane] = uv_stride; | |
| 147 surface_.strides[VideoSurface::kVPlane] = uv_stride; | |
| 148 return true; | |
| 149 } | |
| 150 NOTREACHED(); | |
| 151 return false; | |
| 152 } | |
| 153 | |
| 154 VideoFrameImpl::VideoFrameImpl(VideoSurface::Format format, | |
| 155 size_t width, | |
| 156 size_t height) { | |
| 157 locked_ = false; | |
| 158 memset(&surface_, 0, sizeof(surface_)); | |
| 159 surface_.format = format; | |
| 160 surface_.width = width; | |
| 161 surface_.height = height; | |
| 162 } | |
| 163 | |
| 164 VideoFrameImpl::~VideoFrameImpl() { | |
| 165 // In multi-plane allocations, only a single block of memory is allocated | |
| 166 // on the heap, and other |data| pointers point inside the same, single block | |
| 167 // so just delete index 0. | |
| 168 delete[] surface_.data[0]; | |
| 169 } | |
| 170 | |
| 171 bool VideoFrameImpl::Lock(VideoSurface* surface) { | |
| 172 DCHECK(!locked_); | |
| 173 DCHECK_NE(surface_.format, VideoSurface::EMPTY); | |
| 174 if (locked_) { | |
| 175 memset(surface, 0, sizeof(*surface)); | |
| 176 return false; | |
| 177 } | |
| 178 locked_ = true; | |
| 179 COMPILE_ASSERT(sizeof(*surface) == sizeof(surface_), surface_size_mismatch); | |
| 180 memcpy(surface, &surface_, sizeof(*surface)); | |
| 181 return true; | |
| 182 } | |
| 183 | |
| 184 void VideoFrameImpl::Unlock() { | |
| 185 DCHECK(locked_); | |
| 186 DCHECK_NE(surface_.format, VideoSurface::EMPTY); | |
| 187 locked_ = false; | |
| 188 } | |
| 189 | |
| 190 bool VideoFrameImpl::IsEndOfStream() const { | |
| 191 return surface_.format == VideoSurface::EMPTY; | |
| 192 } | |
| 193 | |
| 194 } // namespace media | |
| OLD | NEW |