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

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

Issue 10451051: Provide a Chrome-owned buffer to FFmpeg for video decoding, instead of (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 6 months 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
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 "base/logging.h" 7 #include "base/logging.h"
8 #include "base/string_piece.h" 8 #include "base/string_piece.h"
9 #include "media/base/limits.h" 9 #include "media/base/limits.h"
10 #include "media/base/video_util.h" 10 #include "media/base/video_util.h"
11 #if !defined(OS_ANDROID)
12 #include "media/ffmpeg/ffmpeg_common.h"
13 #endif
14
15 #include <algorithm>
11 16
12 namespace media { 17 namespace media {
13 18
14 // static 19 // static
15 scoped_refptr<VideoFrame> VideoFrame::CreateFrame( 20 scoped_refptr<VideoFrame> VideoFrame::CreateFrame(
16 VideoFrame::Format format, 21 VideoFrame::Format format,
17 size_t width, 22 size_t width,
18 size_t height, 23 size_t height,
24 size_t alignment,
19 base::TimeDelta timestamp, 25 base::TimeDelta timestamp,
20 base::TimeDelta duration) { 26 base::TimeDelta duration) {
21 DCHECK(IsValidConfig(format, width, height)); 27 DCHECK(IsValidConfig(format, width, height, alignment));
22 scoped_refptr<VideoFrame> frame(new VideoFrame( 28 scoped_refptr<VideoFrame> frame(new VideoFrame(
23 format, width, height, timestamp, duration)); 29 format, width, height, timestamp, duration));
24 switch (format) { 30 switch (format) {
25 case VideoFrame::RGB32: 31 case VideoFrame::RGB32:
26 frame->AllocateRGB(4u); 32 frame->AllocateRGB(4u, alignment);
27 break; 33 break;
28 case VideoFrame::YV12: 34 case VideoFrame::YV12:
29 case VideoFrame::YV16: 35 case VideoFrame::YV16:
30 frame->AllocateYUV(); 36 frame->AllocateYUV(alignment);
31 break; 37 break;
32 default: 38 default:
33 LOG(FATAL) << "Unsupported frame format: " << format; 39 LOG(FATAL) << "Unsupported frame format: " << format;
34 } 40 }
35 return frame; 41 return frame;
36 } 42 }
37 43
44 static inline bool IsPowerOfTwo(int alignment) {
45 // Check that |alignment| is a power of 2.
46 return (alignment + (alignment - 1)) == (alignment | (alignment - 1));
47 }
48
38 // static 49 // static
39 bool VideoFrame::IsValidConfig( 50 bool VideoFrame::IsValidConfig(
40 VideoFrame::Format format, 51 VideoFrame::Format format,
41 size_t width, 52 size_t width,
42 size_t height) { 53 size_t height,
54 size_t alignment) {
43 55
44 return (format != VideoFrame::INVALID && 56 return (format != VideoFrame::INVALID &&
45 width > 0 && height > 0 && 57 width > 0 && height > 0 &&
46 width <= limits::kMaxDimension && height <= limits::kMaxDimension && 58 width <= limits::kMaxDimension && height <= limits::kMaxDimension &&
47 width * height <= limits::kMaxCanvas); 59 width * height <= limits::kMaxCanvas &&
60 alignment > 0 && IsPowerOfTwo(alignment));
48 } 61 }
49 62
50 // static 63 // static
51 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture( 64 scoped_refptr<VideoFrame> VideoFrame::WrapNativeTexture(
52 uint32 texture_id, 65 uint32 texture_id,
53 uint32 texture_target, 66 uint32 texture_target,
54 size_t width, 67 size_t width,
55 size_t height, 68 size_t height,
56 base::TimeDelta timestamp, 69 base::TimeDelta timestamp,
57 base::TimeDelta duration, 70 base::TimeDelta duration,
(...skipping 23 matching lines...) Expand all
81 VideoFrame::CreateFrame(VideoFrame::YV12, width, height, kZero, kZero); 94 VideoFrame::CreateFrame(VideoFrame::YV12, width, height, kZero, kZero);
82 95
83 // Now set the data to YUV(0,128,128). 96 // Now set the data to YUV(0,128,128).
84 const uint8 kBlackY = 0x00; 97 const uint8 kBlackY = 0x00;
85 const uint8 kBlackUV = 0x80; 98 const uint8 kBlackUV = 0x80;
86 FillYUV(frame, kBlackY, kBlackUV, kBlackUV); 99 FillYUV(frame, kBlackY, kBlackUV, kBlackUV);
87 return frame; 100 return frame;
88 } 101 }
89 102
90 static inline size_t RoundUp(size_t value, size_t alignment) { 103 static inline size_t RoundUp(size_t value, size_t alignment) {
91 // Check that |alignment| is a power of 2. 104 DCHECK(IsPowerOfTwo(alignment));
92 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1)));
93 return ((value + (alignment - 1)) & ~(alignment-1)); 105 return ((value + (alignment - 1)) & ~(alignment-1));
94 } 106 }
95 107
96 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) { 108 void VideoFrame::AllocateRGB(size_t bytes_per_pixel, size_t alignment) {
97 // Round up to align at a 64-bit (8 byte) boundary for each row. This 109 // Round up to align at least at a 64-bit (8 byte) boundary for each row.
98 // is sufficient for MMX reads (movq). 110 // This is sufficient for MMX reads (movq).
99 size_t bytes_per_row = RoundUp(width_ * bytes_per_pixel, 8); 111 size_t bytes_per_row = RoundUp(width_ * bytes_per_pixel,
112 std::max(alignment, static_cast<size_t>(8)));
113 size_t aligned_height = RoundUp(height_, alignment);
100 strides_[VideoFrame::kRGBPlane] = bytes_per_row; 114 strides_[VideoFrame::kRGBPlane] = bytes_per_row;
101 data_[VideoFrame::kRGBPlane] = new uint8[bytes_per_row * height_]; 115 #if !defined(OS_ANDROID)
116 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery
117 // doesn't need to be repeated in every single user of aligned data.
118 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>(
119 av_malloc(bytes_per_row * aligned_height));
scherkus (not reviewing) 2012/06/14 19:21:20 sorry -- "4 space indent" meant from the existing
rbultje1 2012/06/14 19:26:25 Done.
120 #else
121 data_[VideoFrame::kRGBPlane] = new uint8_t[bytes_per_row * aligned_height];
122 #endif
102 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7)); 123 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7));
103 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0); 124 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0);
104 } 125 }
105 126
106 static const int kFramePadBytes = 15; // Allows faster SIMD YUV convert. 127 static const int kFramePadBytes = 15; // Allows faster SIMD YUV convert.
107 128
108 void VideoFrame::AllocateYUV() { 129 void VideoFrame::AllocateYUV(size_t alignment) {
109 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16); 130 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16);
110 // Align Y rows at 32-bit (4 byte) boundaries. The stride for both YV12 and 131 // Align Y rows at least at 32-bit (4 byte) boundaries. The stride for both
111 // YV16 is 1/2 of the stride of Y. For YV12, every row of bytes for U and V 132 // YV12 and YV16 is 1/2 of the stride of Y. For YV12, every row of bytes for
112 // applies to two rows of Y (one byte of UV for 4 bytes of Y), so in the 133 // U and V applies to two rows of Y (one byte of UV for 4 bytes of Y), so in
113 // case of YV12 the strides are identical for the same width surface, but the 134 // the case of YV12 the strides are identical for the same width surface, but
114 // number of bytes allocated for YV12 is 1/2 the amount for U & V as YV16. 135 // the number of bytes allocated for YV12 is 1/2 the amount for U & V as
115 // We also round the height of the surface allocated to be an even number 136 // YV16. We also round the height of the surface allocated to be an even
116 // to avoid any potential of faulting by code that attempts to access the Y 137 // number to avoid any potential of faulting by code that attempts to access
117 // values of the final row, but assumes that the last row of U & V applies to 138 // the Y values of the final row, but assumes that the last row of U & V
118 // a full two rows of Y. 139 // applies to a full two rows of Y.
119 size_t y_height = rows(VideoFrame::kYPlane); 140 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane),
120 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane), 4); 141 std::max(alignment, static_cast<size_t>(4)));
121 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane), 4); 142 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane),
122 size_t uv_height = rows(VideoFrame::kUPlane); 143 std::max(alignment, static_cast<size_t>(4)));
144 size_t y_height = RoundUp(height_, std::max(alignment,
scherkus (not reviewing) 2012/06/14 19:21:20 does the height need to be an even number or round
rbultje1 2012/06/14 19:26:25 FFmpeg requires a multiple of 16, since each is de
145 static_cast<size_t>(2)));
146 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height;
123 size_t y_bytes = y_height * y_stride; 147 size_t y_bytes = y_height * y_stride;
124 size_t uv_bytes = uv_height * uv_stride; 148 size_t uv_bytes = uv_height * uv_stride;
125 149
126 uint8* data = new uint8[y_bytes + (uv_bytes * 2) + kFramePadBytes]; 150 #if !defined(OS_ANDROID)
151 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery
152 // doesn't need to be repeated in every single user of aligned data.
153 uint8* data = reinterpret_cast<uint8*>(
154 av_malloc(y_bytes + (uv_bytes * 2) + kFramePadBytes));
scherkus (not reviewing) 2012/06/14 19:21:20 indent here
rbultje1 2012/06/14 19:26:25 Done.
155 #else
156 uint8* data = new uint8_t[y_bytes + (uv_bytes * 2) + kFramePadBytes];
157 #endif
127 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0); 158 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0);
128 data_[VideoFrame::kYPlane] = data; 159 data_[VideoFrame::kYPlane] = data;
129 data_[VideoFrame::kUPlane] = data + y_bytes; 160 data_[VideoFrame::kUPlane] = data + y_bytes;
130 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes; 161 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes;
131 strides_[VideoFrame::kYPlane] = y_stride; 162 strides_[VideoFrame::kYPlane] = y_stride;
132 strides_[VideoFrame::kUPlane] = uv_stride; 163 strides_[VideoFrame::kUPlane] = uv_stride;
133 strides_[VideoFrame::kVPlane] = uv_stride; 164 strides_[VideoFrame::kVPlane] = uv_stride;
134 } 165 }
135 166
136 VideoFrame::VideoFrame(VideoFrame::Format format, 167 VideoFrame::VideoFrame(VideoFrame::Format format,
(...skipping 14 matching lines...) Expand all
151 182
152 VideoFrame::~VideoFrame() { 183 VideoFrame::~VideoFrame() {
153 if (format_ == NATIVE_TEXTURE && !texture_no_longer_needed_.is_null()) { 184 if (format_ == NATIVE_TEXTURE && !texture_no_longer_needed_.is_null()) {
154 texture_no_longer_needed_.Run(); 185 texture_no_longer_needed_.Run();
155 texture_no_longer_needed_.Reset(); 186 texture_no_longer_needed_.Reset();
156 } 187 }
157 188
158 // In multi-plane allocations, only a single block of memory is allocated 189 // In multi-plane allocations, only a single block of memory is allocated
159 // on the heap, and other |data| pointers point inside the same, single block 190 // on the heap, and other |data| pointers point inside the same, single block
160 // so just delete index 0. 191 // so just delete index 0.
161 delete[] data_[0]; 192 if (data_[0]) {
193 #if !defined(OS_ANDROID)
194 av_free(data_[0]);
195 #else
196 delete[] data_[0];
197 #endif
198 }
162 } 199 }
163 200
164 bool VideoFrame::IsValidPlane(size_t plane) const { 201 bool VideoFrame::IsValidPlane(size_t plane) const {
165 switch (format_) { 202 switch (format_) {
166 case RGB32: 203 case RGB32:
167 return plane == kRGBPlane; 204 return plane == kRGBPlane;
168 205
169 case YV12: 206 case YV12:
170 case YV16: 207 case YV16:
171 return plane == kYPlane || plane == kUPlane || plane == kVPlane; 208 return plane == kYPlane || plane == kUPlane || plane == kVPlane;
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 break; 294 break;
258 for(int row = 0; row < rows(plane); row++) { 295 for(int row = 0; row < rows(plane); row++) {
259 base::MD5Update(context, base::StringPiece( 296 base::MD5Update(context, base::StringPiece(
260 reinterpret_cast<char*>(data(plane) + stride(plane) * row), 297 reinterpret_cast<char*>(data(plane) + stride(plane) * row),
261 row_bytes(plane))); 298 row_bytes(plane)));
262 } 299 }
263 } 300 }
264 } 301 }
265 302
266 } // namespace media 303 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698