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

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
« media/base/video_frame.h ('K') | « media/base/video_frame.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/string_piece.h" 12 #include "base/string_piece.h"
13 #include "media/base/limits.h" 13 #include "media/base/limits.h"
14 #include "media/base/video_util.h" 14 #include "media/base/video_util.h"
15
15 #if !defined(OS_ANDROID) 16 #if !defined(OS_ANDROID)
16 #include "media/ffmpeg/ffmpeg_common.h" 17 #include "base/memory/aligned_memory.h"
17 #endif 18 #endif
18 19
19 namespace media { 20 namespace media {
20 21
21 // static 22 // static
22 scoped_refptr<VideoFrame> VideoFrame::CreateFrame( 23 scoped_refptr<VideoFrame> VideoFrame::CreateFrame(
23 VideoFrame::Format format, 24 VideoFrame::Format format,
24 const gfx::Size& coded_size, 25 const gfx::Size& coded_size,
25 const gfx::Rect& visible_rect, 26 const gfx::Rect& visible_rect,
26 const gfx::Size& natural_size, 27 const gfx::Size& natural_size,
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 147
147 static const int kFrameSizeAlignment = 16; 148 static const int kFrameSizeAlignment = 16;
148 // Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally. 149 // Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally.
149 static const int kFramePadBytes = 15; 150 static const int kFramePadBytes = 15;
150 151
151 // Release data allocated by AllocateRGB() or AllocateYUV(). 152 // Release data allocated by AllocateRGB() or AllocateYUV().
152 static void ReleaseData(uint8* data) { 153 static void ReleaseData(uint8* data) {
153 DCHECK(data); 154 DCHECK(data);
154 if (data) { 155 if (data) {
155 #if !defined(OS_ANDROID) 156 #if !defined(OS_ANDROID)
156 av_free(data); 157 base::AlignedFree(data);
157 #else 158 #else
158 delete[] data; 159 delete[] data;
159 #endif 160 #endif
160 } 161 }
161 } 162 }
162 163
163 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) { 164 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) {
164 // Round up to align at least at a 16-byte boundary for each row. 165 // 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). 166 // This is sufficient for MMX and SSE2 reads (movq/movdqa).
166 size_t bytes_per_row = RoundUp(coded_size_.width(), 167 size_t bytes_per_row = RoundUp(coded_size_.width(),
167 kFrameSizeAlignment) * bytes_per_pixel; 168 kFrameSizeAlignment) * bytes_per_pixel;
168 size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment); 169 size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment);
xhwang 2012/12/02 02:13:13 Why we always need to align height?
rbultje1 2012/12/03 19:39:35 Codecs work in complete 16x16 macroblocks, and dec
xhwang 2012/12/04 01:51:30 I see, thanks.
169 strides_[VideoFrame::kRGBPlane] = bytes_per_row; 170 strides_[VideoFrame::kRGBPlane] = bytes_per_row;
170 #if !defined(OS_ANDROID) 171 #if !defined(OS_ANDROID)
171 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery 172 // 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 // doesn't need to be repeated in every single user of aligned data.
173 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>( 174 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>(
174 av_malloc(bytes_per_row * aligned_height + kFramePadBytes)); 175 base::AlignedAlloc(bytes_per_row * aligned_height + kFramePadBytes,
176 kAlignmentSize));
175 #else 177 #else
176 data_[VideoFrame::kRGBPlane] = new uint8_t[bytes_per_row * aligned_height]; 178 data_[VideoFrame::kRGBPlane] = new uint8_t[bytes_per_row * aligned_height];
177 #endif 179 #endif
178 no_longer_needed_cb_ = base::Bind(&ReleaseData, data_[VideoFrame::kRGBPlane]); 180 no_longer_needed_cb_ = base::Bind(&ReleaseData, data_[VideoFrame::kRGBPlane]);
179 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7)); 181 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7));
180 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0); 182 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0);
181 } 183 }
182 184
183 void VideoFrame::AllocateYUV() { 185 void VideoFrame::AllocateYUV() {
184 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16); 186 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16);
(...skipping 12 matching lines...) Expand all
197 kFrameSizeAlignment); 199 kFrameSizeAlignment);
198 // The *2 here is because some formats (e.g. h264) allow interlaced coding, 200 // 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). 201 // and then the size needs to be a multiple of two macroblocks (vertically).
200 // See libavcodec/utils.c:avcodec_align_dimensions2(). 202 // See libavcodec/utils.c:avcodec_align_dimensions2().
201 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2); 203 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2);
202 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height; 204 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height;
203 size_t y_bytes = y_height * y_stride; 205 size_t y_bytes = y_height * y_stride;
204 size_t uv_bytes = uv_height * uv_stride; 206 size_t uv_bytes = uv_height * uv_stride;
205 207
206 #if !defined(OS_ANDROID) 208 #if !defined(OS_ANDROID)
207 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery 209 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery
xhwang 2012/12/02 02:13:13 I wonder how we can remove #ifdef if for Android w
rbultje1 2012/12/03 19:39:35 Android doesn't use FFmpeg, so we can't use av_mal
xhwang 2012/12/04 01:51:30 base::AlignedAlloc should be available on Android
208 // doesn't need to be repeated in every single user of aligned data. 210 // 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 211 // The extra line of UV being allocated is because h264 chroma MC
210 // overreads by one line in some cases, see libavcodec/utils.c: 212 // overreads by one line in some cases, see libavcodec/utils.c:
211 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm: 213 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm:
212 // put_h264_chroma_mc4_ssse3(). 214 // put_h264_chroma_mc4_ssse3().
213 uint8* data = reinterpret_cast<uint8*>( 215 uint8* data = reinterpret_cast<uint8*>(
214 av_malloc(y_bytes + (uv_bytes * 2 + uv_stride) + kFramePadBytes)); 216 base::AlignedAlloc(y_bytes + (uv_bytes * 2 + uv_stride) + kFramePadBytes,
217 kAlignmentSize));
215 #else 218 #else
216 uint8* data = new uint8_t[y_bytes + (uv_bytes * 2)]; 219 uint8* data = new uint8_t[y_bytes + (uv_bytes * 2)];
217 #endif 220 #endif
218 no_longer_needed_cb_ = base::Bind(&ReleaseData, data); 221 no_longer_needed_cb_ = base::Bind(&ReleaseData, data);
219 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0); 222 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0);
220 data_[VideoFrame::kYPlane] = data; 223 data_[VideoFrame::kYPlane] = data;
221 data_[VideoFrame::kUPlane] = data + y_bytes; 224 data_[VideoFrame::kUPlane] = data + y_bytes;
222 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes; 225 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes;
223 strides_[VideoFrame::kYPlane] = y_stride; 226 strides_[VideoFrame::kYPlane] = y_stride;
224 strides_[VideoFrame::kUPlane] = uv_stride; 227 strides_[VideoFrame::kUPlane] = uv_stride;
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 break; 347 break;
345 for (int row = 0; row < rows(plane); ++row) { 348 for (int row = 0; row < rows(plane); ++row) {
346 base::MD5Update(context, base::StringPiece( 349 base::MD5Update(context, base::StringPiece(
347 reinterpret_cast<char*>(data(plane) + stride(plane) * row), 350 reinterpret_cast<char*>(data(plane) + stride(plane) * row),
348 row_bytes(plane))); 351 row_bytes(plane)));
349 } 352 }
350 } 353 }
351 } 354 }
352 355
353 } // namespace media 356 } // namespace media
OLDNEW
« media/base/video_frame.h ('K') | « media/base/video_frame.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698