Skip to content

Commit

Permalink
Replace av_malloc with AlignedAlloc for memory allocation in VideoFrame.
Browse files Browse the repository at this point in the history
See also r157836.

BUG=150920
TEST=media_unittests


Review URL: https://chromiumcodereview.appspot.com/11308310

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171976 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
xhwang@chromium.org committed Dec 8, 2012
1 parent 5e1119d commit 486238b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
40 changes: 9 additions & 31 deletions media/base/video_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/aligned_memory.h"
#include "base/string_piece.h"
#include "media/base/limits.h"
#include "media/base/video_util.h"
#if !defined(OS_ANDROID)
#include "media/ffmpeg/ffmpeg_common.h"
#endif

namespace media {

Expand Down Expand Up @@ -72,9 +70,8 @@ scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture(
base::TimeDelta timestamp,
const ReadPixelsCB& read_pixels_cb,
const base::Closure& no_longer_needed_cb) {
scoped_refptr<VideoFrame> frame(
new VideoFrame(NATIVE_TEXTURE, coded_size, visible_rect, natural_size,
timestamp));
scoped_refptr<VideoFrame> frame(new VideoFrame(
NATIVE_TEXTURE, coded_size, visible_rect, natural_size, timestamp));
frame->texture_id_ = texture_id;
frame->texture_target_ = texture_target;
frame->read_pixels_cb_ = read_pixels_cb;
Expand Down Expand Up @@ -144,20 +141,10 @@ static inline size_t RoundUp(size_t value, size_t alignment) {
return ((value + (alignment - 1)) & ~(alignment-1));
}

static const int kFrameSizeAlignment = 16;
// Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally.
static const int kFramePadBytes = 15;

// Release data allocated by AllocateRGB() or AllocateYUV().
static void ReleaseData(uint8* data) {
DCHECK(data);
if (data) {
#if !defined(OS_ANDROID)
av_free(data);
#else
delete[] data;
#endif
}
base::AlignedFree(data);
}

void VideoFrame::AllocateRGB(size_t bytes_per_pixel) {
Expand All @@ -167,14 +154,9 @@ void VideoFrame::AllocateRGB(size_t bytes_per_pixel) {
kFrameSizeAlignment) * bytes_per_pixel;
size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment);
strides_[VideoFrame::kRGBPlane] = bytes_per_row;
#if !defined(OS_ANDROID)
// TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery
// doesn't need to be repeated in every single user of aligned data.
data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>(
av_malloc(bytes_per_row * aligned_height + kFramePadBytes));
#else
data_[VideoFrame::kRGBPlane] = new uint8_t[bytes_per_row * aligned_height];
#endif
base::AlignedAlloc(bytes_per_row * aligned_height + kFrameSizePadding,
kFrameAddressAlignment));
no_longer_needed_cb_ = base::Bind(&ReleaseData, data_[VideoFrame::kRGBPlane]);
DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7));
COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0);
Expand Down Expand Up @@ -203,18 +185,14 @@ void VideoFrame::AllocateYUV() {
size_t y_bytes = y_height * y_stride;
size_t uv_bytes = uv_height * uv_stride;

#if !defined(OS_ANDROID)
// TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery
// doesn't need to be repeated in every single user of aligned data.
// The extra line of UV being allocated is because h264 chroma MC
// overreads by one line in some cases, see libavcodec/utils.c:
// avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm:
// put_h264_chroma_mc4_ssse3().
uint8* data = reinterpret_cast<uint8*>(
av_malloc(y_bytes + (uv_bytes * 2 + uv_stride) + kFramePadBytes));
#else
uint8* data = new uint8_t[y_bytes + (uv_bytes * 2)];
#endif
base::AlignedAlloc(
y_bytes + (uv_bytes * 2 + uv_stride) + kFrameSizePadding,
kFrameAddressAlignment));
no_longer_needed_cb_ = base::Bind(&ReleaseData, data);
COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0);
data_[VideoFrame::kYPlane] = data;
Expand Down
6 changes: 6 additions & 0 deletions media/base/video_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ namespace media {

class MEDIA_EXPORT VideoFrame : public base::RefCountedThreadSafe<VideoFrame> {
public:
enum {
kFrameSizeAlignment = 16,
kFrameSizePadding = 16,
kFrameAddressAlignment = 32
};

enum {
kMaxPlanes = 3,

Expand Down
26 changes: 20 additions & 6 deletions media/ffmpeg/ffmpeg_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/basictypes.h"
#include "base/logging.h"
#include "media/base/decoder_buffer.h"
#include "media/base/video_frame.h"
#include "media/base/video_util.h"

namespace media {
Expand All @@ -17,19 +18,32 @@ namespace media {
COMPILE_ASSERT(DecoderBuffer::kPaddingSize >= FF_INPUT_BUFFER_PADDING_SIZE,
decoder_buffer_padding_size_does_not_fit_ffmpeg_requirement);

// Alignment requirement by FFmpeg for input buffers. This need to be updated
// to match FFmpeg when it changes.
// Alignment requirement by FFmpeg for input and output buffers. This need to
// be updated to match FFmpeg when it changes.
#if defined(ARCH_CPU_ARM_FAMILY)
static const int kFFmpegInputBufferAlignmentSize = 16;
static const int kFFmpegBufferAddressAlignment = 16;
#else
static const int kFFmpegInputBufferAlignmentSize = 32;
static const int kFFmpegBufferAddressAlignment = 32;
#endif

// Check here to ensure FFmpeg only receives data aligned to its specifications.
COMPILE_ASSERT(
DecoderBuffer::kAlignmentSize >= kFFmpegInputBufferAlignmentSize &&
DecoderBuffer::kAlignmentSize % kFFmpegInputBufferAlignmentSize == 0,
DecoderBuffer::kAlignmentSize >= kFFmpegBufferAddressAlignment &&
DecoderBuffer::kAlignmentSize % kFFmpegBufferAddressAlignment == 0,
decoder_buffer_alignment_size_does_not_fit_ffmpeg_requirement);

// Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally.
// See video_get_buffer() in libavcodec/utils.c.
static const int kFFmpegOutputBufferPaddingSize = 16;

COMPILE_ASSERT(VideoFrame::kFrameSizePadding >= kFFmpegOutputBufferPaddingSize,
video_frame_padding_size_does_not_fit_ffmpeg_requirement);

COMPILE_ASSERT(
VideoFrame::kFrameAddressAlignment >= kFFmpegBufferAddressAlignment &&
VideoFrame::kFrameAddressAlignment % kFFmpegBufferAddressAlignment == 0,
video_frame_address_alignment_does_not_fit_ffmpeg_requirement);

static const AVRational kMicrosBase = { 1, base::Time::kMicrosecondsPerSecond };

base::TimeDelta ConvertFromTimeBase(const AVRational& time_base,
Expand Down

0 comments on commit 486238b

Please sign in to comment.