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

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
« no previous file with comments | « media/base/video_frame.h ('k') | media/ffmpeg/ffmpeg_common.h » ('j') | 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 "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,
19 base::TimeDelta timestamp, 24 base::TimeDelta timestamp,
20 base::TimeDelta duration) { 25 base::TimeDelta duration) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 return frame; 92 return frame;
88 } 93 }
89 94
90 static inline size_t RoundUp(size_t value, size_t alignment) { 95 static inline size_t RoundUp(size_t value, size_t alignment) {
91 // Check that |alignment| is a power of 2. 96 // Check that |alignment| is a power of 2.
92 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); 97 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1)));
93 return ((value + (alignment - 1)) & ~(alignment-1)); 98 return ((value + (alignment - 1)) & ~(alignment-1));
94 } 99 }
95 100
96 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) { 101 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) {
97 // Round up to align at a 64-bit (8 byte) boundary for each row. This 102 // Round up to align at least at a 64-bit (8 byte) boundary for each row.
98 // is sufficient for MMX reads (movq). 103 // This is sufficient for MMX reads (movq).
99 size_t bytes_per_row = RoundUp(width_ * bytes_per_pixel, 8); 104 size_t bytes_per_row = RoundUp(width_, 16) * bytes_per_pixel;
DaleCurtis 2012/06/16 04:23:10 Don't have time to do a full review this second, b
105 size_t aligned_height = RoundUp(height_, 16);
100 strides_[VideoFrame::kRGBPlane] = bytes_per_row; 106 strides_[VideoFrame::kRGBPlane] = bytes_per_row;
101 data_[VideoFrame::kRGBPlane] = new uint8[bytes_per_row * height_]; 107 #if !defined(OS_ANDROID)
108 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery
109 // doesn't need to be repeated in every single user of aligned data.
110 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>(
111 av_malloc(bytes_per_row * aligned_height));
112 #else
113 data_[VideoFrame::kRGBPlane] = new uint8_t[bytes_per_row * aligned_height];
114 #endif
102 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7)); 115 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7));
103 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0); 116 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0);
104 } 117 }
105 118
106 static const int kFramePadBytes = 15; // Allows faster SIMD YUV convert. 119 static const int kFramePadBytes = 15; // Allows faster SIMD YUV convert.
107 120
108 void VideoFrame::AllocateYUV() { 121 void VideoFrame::AllocateYUV() {
109 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16); 122 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16);
110 // Align Y rows at 32-bit (4 byte) boundaries. The stride for both YV12 and 123 // Align Y rows at least at 16 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 124 // 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 125 // 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 126 // 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. 127 // 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 128 // 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 129 // 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 130 // the Y values of the final row, but assumes that the last row of U & V
118 // a full two rows of Y. 131 // applies to a full two rows of Y.
119 size_t y_height = rows(VideoFrame::kYPlane); 132 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane), 16);
120 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane), 4); 133 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane), 16);
121 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane), 4); 134 size_t y_height = RoundUp(height_, 16);
122 size_t uv_height = rows(VideoFrame::kUPlane); 135 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height;
123 size_t y_bytes = y_height * y_stride; 136 size_t y_bytes = y_height * y_stride;
124 size_t uv_bytes = uv_height * uv_stride; 137 size_t uv_bytes = uv_height * uv_stride;
125 138
126 uint8* data = new uint8[y_bytes + (uv_bytes * 2) + kFramePadBytes]; 139 #if !defined(OS_ANDROID)
140 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery
141 // doesn't need to be repeated in every single user of aligned data.
142 uint8* data = reinterpret_cast<uint8*>(
143 av_malloc(y_bytes + (uv_bytes * 2) + kFramePadBytes));
144 #else
145 uint8* data = new uint8_t[y_bytes + (uv_bytes * 2) + kFramePadBytes];
146 #endif
127 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0); 147 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0);
128 data_[VideoFrame::kYPlane] = data; 148 data_[VideoFrame::kYPlane] = data;
129 data_[VideoFrame::kUPlane] = data + y_bytes; 149 data_[VideoFrame::kUPlane] = data + y_bytes;
130 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes; 150 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes;
131 strides_[VideoFrame::kYPlane] = y_stride; 151 strides_[VideoFrame::kYPlane] = y_stride;
132 strides_[VideoFrame::kUPlane] = uv_stride; 152 strides_[VideoFrame::kUPlane] = uv_stride;
133 strides_[VideoFrame::kVPlane] = uv_stride; 153 strides_[VideoFrame::kVPlane] = uv_stride;
134 } 154 }
135 155
136 VideoFrame::VideoFrame(VideoFrame::Format format, 156 VideoFrame::VideoFrame(VideoFrame::Format format,
(...skipping 14 matching lines...) Expand all
151 171
152 VideoFrame::~VideoFrame() { 172 VideoFrame::~VideoFrame() {
153 if (format_ == NATIVE_TEXTURE && !texture_no_longer_needed_.is_null()) { 173 if (format_ == NATIVE_TEXTURE && !texture_no_longer_needed_.is_null()) {
154 texture_no_longer_needed_.Run(); 174 texture_no_longer_needed_.Run();
155 texture_no_longer_needed_.Reset(); 175 texture_no_longer_needed_.Reset();
156 } 176 }
157 177
158 // In multi-plane allocations, only a single block of memory is allocated 178 // 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 179 // on the heap, and other |data| pointers point inside the same, single block
160 // so just delete index 0. 180 // so just delete index 0.
161 delete[] data_[0]; 181 if (data_[0]) {
182 #if !defined(OS_ANDROID)
183 av_free(data_[0]);
184 #else
185 delete[] data_[0];
186 #endif
187 }
162 } 188 }
163 189
164 bool VideoFrame::IsValidPlane(size_t plane) const { 190 bool VideoFrame::IsValidPlane(size_t plane) const {
165 switch (format_) { 191 switch (format_) {
166 case RGB32: 192 case RGB32:
167 return plane == kRGBPlane; 193 return plane == kRGBPlane;
168 194
169 case YV12: 195 case YV12:
170 case YV16: 196 case YV16:
171 return plane == kYPlane || plane == kUPlane || plane == kVPlane; 197 return plane == kYPlane || plane == kUPlane || plane == kVPlane;
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 break; 283 break;
258 for(int row = 0; row < rows(plane); row++) { 284 for(int row = 0; row < rows(plane); row++) {
259 base::MD5Update(context, base::StringPiece( 285 base::MD5Update(context, base::StringPiece(
260 reinterpret_cast<char*>(data(plane) + stride(plane) * row), 286 reinterpret_cast<char*>(data(plane) + stride(plane) * row),
261 row_bytes(plane))); 287 row_bytes(plane)));
262 } 288 }
263 } 289 }
264 } 290 }
265 291
266 } // namespace media 292 } // namespace media
OLDNEW
« no previous file with comments | « media/base/video_frame.h ('k') | media/ffmpeg/ffmpeg_common.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698