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/string_piece.h" | 13 #include "base/string_piece.h" |
13 #include "media/base/limits.h" | 14 #include "media/base/limits.h" |
14 #include "media/base/video_util.h" | 15 #include "media/base/video_util.h" |
15 #if !defined(OS_ANDROID) | |
16 #include "media/ffmpeg/ffmpeg_common.h" | |
17 #endif | |
18 | 16 |
19 namespace media { | 17 namespace media { |
20 | 18 |
21 // static | 19 // static |
22 scoped_refptr<VideoFrame> VideoFrame::CreateFrame( | 20 scoped_refptr<VideoFrame> VideoFrame::CreateFrame( |
23 VideoFrame::Format format, | 21 VideoFrame::Format format, |
24 const gfx::Size& coded_size, | 22 const gfx::Size& coded_size, |
25 const gfx::Rect& visible_rect, | 23 const gfx::Rect& visible_rect, |
26 const gfx::Size& natural_size, | 24 const gfx::Size& natural_size, |
27 base::TimeDelta timestamp) { | 25 base::TimeDelta timestamp) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 // static | 63 // static |
66 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture( | 64 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture( |
67 uint32 texture_id, | 65 uint32 texture_id, |
68 uint32 texture_target, | 66 uint32 texture_target, |
69 const gfx::Size& coded_size, | 67 const gfx::Size& coded_size, |
70 const gfx::Rect& visible_rect, | 68 const gfx::Rect& visible_rect, |
71 const gfx::Size& natural_size, | 69 const gfx::Size& natural_size, |
72 base::TimeDelta timestamp, | 70 base::TimeDelta timestamp, |
73 const ReadPixelsCB& read_pixels_cb, | 71 const ReadPixelsCB& read_pixels_cb, |
74 const base::Closure& no_longer_needed_cb) { | 72 const base::Closure& no_longer_needed_cb) { |
75 scoped_refptr<VideoFrame> frame( | 73 scoped_refptr<VideoFrame> frame(new VideoFrame( |
76 new VideoFrame(NATIVE_TEXTURE, coded_size, visible_rect, natural_size, | 74 NATIVE_TEXTURE, coded_size, visible_rect, natural_size, timestamp)); |
77 timestamp)); | |
78 frame->texture_id_ = texture_id; | 75 frame->texture_id_ = texture_id; |
79 frame->texture_target_ = texture_target; | 76 frame->texture_target_ = texture_target; |
80 frame->read_pixels_cb_ = read_pixels_cb; | 77 frame->read_pixels_cb_ = read_pixels_cb; |
81 frame->no_longer_needed_cb_ = no_longer_needed_cb; | 78 frame->no_longer_needed_cb_ = no_longer_needed_cb; |
82 return frame; | 79 return frame; |
83 } | 80 } |
84 | 81 |
85 void VideoFrame::ReadPixelsFromNativeTexture(void* pixels) { | 82 void VideoFrame::ReadPixelsFromNativeTexture(void* pixels) { |
86 DCHECK_EQ(format_, NATIVE_TEXTURE); | 83 DCHECK_EQ(format_, NATIVE_TEXTURE); |
87 if (!read_pixels_cb_.is_null()) | 84 if (!read_pixels_cb_.is_null()) |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 const base::TimeDelta kZero; | 134 const base::TimeDelta kZero; |
138 return CreateColorFrame(size, kBlackY, kBlackUV, kBlackUV, kZero); | 135 return CreateColorFrame(size, kBlackY, kBlackUV, kBlackUV, kZero); |
139 } | 136 } |
140 | 137 |
141 static inline size_t RoundUp(size_t value, size_t alignment) { | 138 static inline size_t RoundUp(size_t value, size_t alignment) { |
142 // Check that |alignment| is a power of 2. | 139 // Check that |alignment| is a power of 2. |
143 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); | 140 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); |
144 return ((value + (alignment - 1)) & ~(alignment-1)); | 141 return ((value + (alignment - 1)) & ~(alignment-1)); |
145 } | 142 } |
146 | 143 |
147 static const int kFrameSizeAlignment = 16; | |
148 // Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally. | |
149 static const int kFramePadBytes = 15; | |
150 | |
151 // Release data allocated by AllocateRGB() or AllocateYUV(). | 144 // Release data allocated by AllocateRGB() or AllocateYUV(). |
152 static void ReleaseData(uint8* data) { | 145 static void ReleaseData(uint8* data) { |
153 DCHECK(data); | 146 DCHECK(data); |
154 if (data) { | 147 base::AlignedFree(data); |
155 #if !defined(OS_ANDROID) | |
156 av_free(data); | |
157 #else | |
158 delete[] data; | |
159 #endif | |
160 } | |
161 } | 148 } |
162 | 149 |
163 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) { | 150 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) { |
164 // Round up to align at least at a 16-byte boundary for each row. | 151 // Round up to align at least at a 16-byte boundary for each row. |
165 // This is sufficient for MMX and SSE2 reads (movq/movdqa). | 152 // This is sufficient for MMX and SSE2 reads (movq/movdqa). |
166 size_t bytes_per_row = RoundUp(coded_size_.width(), | 153 size_t bytes_per_row = RoundUp(coded_size_.width(), |
167 kFrameSizeAlignment) * bytes_per_pixel; | 154 kFrameSizeAlignment) * bytes_per_pixel; |
168 size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment); | 155 size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment); |
169 strides_[VideoFrame::kRGBPlane] = bytes_per_row; | 156 strides_[VideoFrame::kRGBPlane] = bytes_per_row; |
170 #if !defined(OS_ANDROID) | |
171 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery | |
172 // doesn't need to be repeated in every single user of aligned data. | |
173 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>( | 157 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>( |
174 av_malloc(bytes_per_row * aligned_height + kFramePadBytes)); | 158 base::AlignedAlloc(bytes_per_row * aligned_height + kFrameSizePadding, |
175 #else | 159 kFrameAddressAlignment)); |
176 data_[VideoFrame::kRGBPlane] = new uint8_t[bytes_per_row * aligned_height]; | |
177 #endif | |
178 no_longer_needed_cb_ = base::Bind(&ReleaseData, data_[VideoFrame::kRGBPlane]); | 160 no_longer_needed_cb_ = base::Bind(&ReleaseData, data_[VideoFrame::kRGBPlane]); |
179 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7)); | 161 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7)); |
180 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0); | 162 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0); |
181 } | 163 } |
182 | 164 |
183 void VideoFrame::AllocateYUV() { | 165 void VideoFrame::AllocateYUV() { |
184 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16); | 166 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16); |
185 // Align Y rows at least at 16 byte boundaries. The stride for both | 167 // Align Y rows at least at 16 byte boundaries. The stride for both |
186 // YV12 and YV16 is 1/2 of the stride of Y. For YV12, every row of bytes for | 168 // YV12 and YV16 is 1/2 of the stride of Y. For YV12, every row of bytes for |
187 // U and V applies to two rows of Y (one byte of UV for 4 bytes of Y), so in | 169 // U and V applies to two rows of Y (one byte of UV for 4 bytes of Y), so in |
188 // the case of YV12 the strides are identical for the same width surface, but | 170 // the case of YV12 the strides are identical for the same width surface, but |
189 // the number of bytes allocated for YV12 is 1/2 the amount for U & V as | 171 // the number of bytes allocated for YV12 is 1/2 the amount for U & V as |
190 // YV16. We also round the height of the surface allocated to be an even | 172 // YV16. We also round the height of the surface allocated to be an even |
191 // number to avoid any potential of faulting by code that attempts to access | 173 // number to avoid any potential of faulting by code that attempts to access |
192 // the Y values of the final row, but assumes that the last row of U & V | 174 // the Y values of the final row, but assumes that the last row of U & V |
193 // applies to a full two rows of Y. | 175 // applies to a full two rows of Y. |
194 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane), | 176 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane), |
195 kFrameSizeAlignment); | 177 kFrameSizeAlignment); |
196 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane), | 178 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane), |
197 kFrameSizeAlignment); | 179 kFrameSizeAlignment); |
198 // The *2 here is because some formats (e.g. h264) allow interlaced coding, | 180 // The *2 here is because some formats (e.g. h264) allow interlaced coding, |
199 // and then the size needs to be a multiple of two macroblocks (vertically). | 181 // and then the size needs to be a multiple of two macroblocks (vertically). |
200 // See libavcodec/utils.c:avcodec_align_dimensions2(). | 182 // See libavcodec/utils.c:avcodec_align_dimensions2(). |
201 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2); | 183 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2); |
202 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height; | 184 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height; |
203 size_t y_bytes = y_height * y_stride; | 185 size_t y_bytes = y_height * y_stride; |
204 size_t uv_bytes = uv_height * uv_stride; | 186 size_t uv_bytes = uv_height * uv_stride; |
205 | 187 |
206 #if !defined(OS_ANDROID) | |
207 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery | |
208 // doesn't need to be repeated in every single user of aligned data. | |
209 // The extra line of UV being allocated is because h264 chroma MC | 188 // The extra line of UV being allocated is because h264 chroma MC |
210 // overreads by one line in some cases, see libavcodec/utils.c: | 189 // overreads by one line in some cases, see libavcodec/utils.c: |
211 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm: | 190 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm: |
212 // put_h264_chroma_mc4_ssse3(). | 191 // put_h264_chroma_mc4_ssse3(). |
213 uint8* data = reinterpret_cast<uint8*>( | 192 uint8* data = reinterpret_cast<uint8*>( |
214 av_malloc(y_bytes + (uv_bytes * 2 + uv_stride) + kFramePadBytes)); | 193 base::AlignedAlloc( |
215 #else | 194 y_bytes + (uv_bytes * 2 + uv_stride) + kFrameSizePadding, |
216 uint8* data = new uint8_t[y_bytes + (uv_bytes * 2)]; | 195 kFrameAddressAlignment)); |
217 #endif | |
218 no_longer_needed_cb_ = base::Bind(&ReleaseData, data); | 196 no_longer_needed_cb_ = base::Bind(&ReleaseData, data); |
219 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0); | 197 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0); |
220 data_[VideoFrame::kYPlane] = data; | 198 data_[VideoFrame::kYPlane] = data; |
221 data_[VideoFrame::kUPlane] = data + y_bytes; | 199 data_[VideoFrame::kUPlane] = data + y_bytes; |
222 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes; | 200 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes; |
223 strides_[VideoFrame::kYPlane] = y_stride; | 201 strides_[VideoFrame::kYPlane] = y_stride; |
224 strides_[VideoFrame::kUPlane] = uv_stride; | 202 strides_[VideoFrame::kUPlane] = uv_stride; |
225 strides_[VideoFrame::kVPlane] = uv_stride; | 203 strides_[VideoFrame::kVPlane] = uv_stride; |
226 } | 204 } |
227 | 205 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 break; | 322 break; |
345 for (int row = 0; row < rows(plane); ++row) { | 323 for (int row = 0; row < rows(plane); ++row) { |
346 base::MD5Update(context, base::StringPiece( | 324 base::MD5Update(context, base::StringPiece( |
347 reinterpret_cast<char*>(data(plane) + stride(plane) * row), | 325 reinterpret_cast<char*>(data(plane) + stride(plane) * row), |
348 row_bytes(plane))); | 326 row_bytes(plane))); |
349 } | 327 } |
350 } | 328 } |
351 } | 329 } |
352 | 330 |
353 } // namespace media | 331 } // namespace media |
OLD | NEW |