Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(755)

Side by Side Diff: media/base/video_frame.cc

Issue 11308310: Replace av_malloc with AlignedAlloc for memory allocation in VideoFrame. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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);
DaleCurtis 2012/12/07 19:11:15 This doesn't really make sense. You DCHECK() and t
xhwang 2012/12/07 20:25:33 Done.
154 if (data) { 147 if (data)
155 #if !defined(OS_ANDROID) 148 base::AlignedFree(data);
156 av_free(data);
157 #else
158 delete[] data;
159 #endif
160 }
161 } 149 }
162 150
163 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) { 151 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) {
164 // Round up to align at least at a 16-byte boundary for each row. 152 // 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). 153 // This is sufficient for MMX and SSE2 reads (movq/movdqa).
166 size_t bytes_per_row = RoundUp(coded_size_.width(), 154 size_t bytes_per_row = RoundUp(coded_size_.width(),
167 kFrameSizeAlignment) * bytes_per_pixel; 155 kFrameSizeAlignment) * bytes_per_pixel;
168 size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment); 156 size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment);
169 strides_[VideoFrame::kRGBPlane] = bytes_per_row; 157 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*>( 158 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>(
174 av_malloc(bytes_per_row * aligned_height + kFramePadBytes)); 159 base::AlignedAlloc(bytes_per_row * aligned_height + kFrameSizePadding,
175 #else 160 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]); 161 no_longer_needed_cb_ = base::Bind(&ReleaseData, data_[VideoFrame::kRGBPlane]);
179 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7)); 162 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7));
180 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0); 163 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0);
181 } 164 }
182 165
183 void VideoFrame::AllocateYUV() { 166 void VideoFrame::AllocateYUV() {
184 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16); 167 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16);
185 // Align Y rows at least at 16 byte boundaries. The stride for both 168 // 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 169 // 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 170 // 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 171 // 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 172 // 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 173 // 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 174 // 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 175 // 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. 176 // applies to a full two rows of Y.
194 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane), 177 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane),
195 kFrameSizeAlignment); 178 kFrameSizeAlignment);
196 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane), 179 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane),
197 kFrameSizeAlignment); 180 kFrameSizeAlignment);
198 // The *2 here is because some formats (e.g. h264) allow interlaced coding, 181 // 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). 182 // and then the size needs to be a multiple of two macroblocks (vertically).
200 // See libavcodec/utils.c:avcodec_align_dimensions2(). 183 // See libavcodec/utils.c:avcodec_align_dimensions2().
201 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2); 184 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2);
202 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height; 185 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height;
203 size_t y_bytes = y_height * y_stride; 186 size_t y_bytes = y_height * y_stride;
204 size_t uv_bytes = uv_height * uv_stride; 187 size_t uv_bytes = uv_height * uv_stride;
205 188
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 189 // The extra line of UV being allocated is because h264 chroma MC
210 // overreads by one line in some cases, see libavcodec/utils.c: 190 // overreads by one line in some cases, see libavcodec/utils.c:
211 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm: 191 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm:
212 // put_h264_chroma_mc4_ssse3(). 192 // put_h264_chroma_mc4_ssse3().
213 uint8* data = reinterpret_cast<uint8*>( 193 uint8* data = reinterpret_cast<uint8*>(
214 av_malloc(y_bytes + (uv_bytes * 2 + uv_stride) + kFramePadBytes)); 194 base::AlignedAlloc(
215 #else 195 y_bytes + (uv_bytes * 2 + uv_stride) + kFrameSizePadding,
216 uint8* data = new uint8_t[y_bytes + (uv_bytes * 2)]; 196 kFrameAddressAlignment));
217 #endif
218 no_longer_needed_cb_ = base::Bind(&ReleaseData, data); 197 no_longer_needed_cb_ = base::Bind(&ReleaseData, data);
219 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0); 198 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0);
220 data_[VideoFrame::kYPlane] = data; 199 data_[VideoFrame::kYPlane] = data;
221 data_[VideoFrame::kUPlane] = data + y_bytes; 200 data_[VideoFrame::kUPlane] = data + y_bytes;
222 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes; 201 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes;
223 strides_[VideoFrame::kYPlane] = y_stride; 202 strides_[VideoFrame::kYPlane] = y_stride;
224 strides_[VideoFrame::kUPlane] = uv_stride; 203 strides_[VideoFrame::kUPlane] = uv_stride;
225 strides_[VideoFrame::kVPlane] = uv_stride; 204 strides_[VideoFrame::kVPlane] = uv_stride;
226 } 205 }
227 206
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 break; 323 break;
345 for (int row = 0; row < rows(plane); ++row) { 324 for (int row = 0; row < rows(plane); ++row) {
346 base::MD5Update(context, base::StringPiece( 325 base::MD5Update(context, base::StringPiece(
347 reinterpret_cast<char*>(data(plane) + stride(plane) * row), 326 reinterpret_cast<char*>(data(plane) + stride(plane) * row),
348 row_bytes(plane))); 327 row_bytes(plane)));
349 } 328 }
350 } 329 }
351 } 330 }
352 331
353 } // namespace media 332 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698