OLD | NEW |
---|---|
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" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 // Check that |alignment| is a power of 2. | 117 // Check that |alignment| is a power of 2. |
118 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); | 118 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); |
119 return ((value + (alignment - 1)) & ~(alignment-1)); | 119 return ((value + (alignment - 1)) & ~(alignment-1)); |
120 } | 120 } |
121 | 121 |
122 static const int kFrameSizeAlignment = 16; | 122 static const int kFrameSizeAlignment = 16; |
123 // Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally. | 123 // Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally. |
124 static const int kFramePadBytes = 15; | 124 static const int kFramePadBytes = 15; |
125 | 125 |
126 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) { | 126 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) { |
127 coded_size_.set_width(RoundUp(coded_size_.width(), 4)); | |
sheu
2012/11/16 03:03:45
This is kinda hacky. I'm doing this basically bec
| |
127 // Round up to align at least at a 16-byte boundary for each row. | 128 // Round up to align at least at a 16-byte boundary for each row. |
128 // This is sufficient for MMX and SSE2 reads (movq/movdqa). | 129 // This is sufficient for MMX and SSE2 reads (movq/movdqa). |
129 size_t bytes_per_row = RoundUp(coded_size_.width(), | 130 size_t bytes_per_row = RoundUp(coded_size_.width(), |
130 kFrameSizeAlignment) * bytes_per_pixel; | 131 kFrameSizeAlignment) * bytes_per_pixel; |
131 size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment); | 132 size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment); |
132 strides_[VideoFrame::kRGBPlane] = bytes_per_row; | 133 strides_[VideoFrame::kRGBPlane] = bytes_per_row; |
133 #if !defined(OS_ANDROID) | 134 #if !defined(OS_ANDROID) |
134 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery | 135 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery |
135 // doesn't need to be repeated in every single user of aligned data. | 136 // doesn't need to be repeated in every single user of aligned data. |
136 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>( | 137 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>( |
137 av_malloc(bytes_per_row * aligned_height + kFramePadBytes)); | 138 av_malloc(bytes_per_row * aligned_height + kFramePadBytes)); |
138 #else | 139 #else |
139 data_[VideoFrame::kRGBPlane] = new uint8_t[bytes_per_row * aligned_height]; | 140 data_[VideoFrame::kRGBPlane] = new uint8_t[bytes_per_row * aligned_height]; |
140 #endif | 141 #endif |
141 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7)); | 142 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7)); |
142 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0); | 143 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0); |
143 } | 144 } |
144 | 145 |
145 void VideoFrame::AllocateYUV() { | 146 void VideoFrame::AllocateYUV() { |
146 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16); | 147 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16); |
147 // Align Y rows at least at 16 byte boundaries. The stride for both | 148 coded_size_.set_width(RoundUp(coded_size_.width(), 4)); |
148 // YV12 and YV16 is 1/2 of the stride of Y. For YV12, every row of bytes for | 149 // Align Y rows at least at 32 byte boundaries, so the stride for both YV12 |
149 // U and V applies to two rows of Y (one byte of UV for 4 bytes of Y), so in | 150 // and YV16 at 1/2 of the stride of Y is aligned to 16. For YV12, every row of |
150 // the case of YV12 the strides are identical for the same width surface, but | 151 // bytes for U and V applies to two rows of Y (one byte of UV for 4 bytes of |
151 // the number of bytes allocated for YV12 is 1/2 the amount for U & V as | 152 // Y), so in the case of YV12 the strides are identical for the same width |
152 // YV16. We also round the height of the surface allocated to be an even | 153 // surface, but the number of bytes allocated for YV12 is 1/2 the amount for |
153 // number to avoid any potential of faulting by code that attempts to access | 154 // U & V as YV16. We also round the height of the surface allocated to be an |
154 // the Y values of the final row, but assumes that the last row of U & V | 155 // even number to avoid any potential of faulting by code that attempts to |
155 // applies to a full two rows of Y. | 156 // access the Y values of the final row, but assumes that the last row of U & |
157 // V applies to a full two rows of Y. | |
156 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane), | 158 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane), |
157 kFrameSizeAlignment); | 159 kFrameSizeAlignment * 2); |
158 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane), | 160 // We keep the UV stride (and height below) proportional to the Y stride |
159 kFrameSizeAlignment); | 161 // (and height), since we will be assuming an identical scale factor for |
162 // stride (and height) for the Y and UV planes. | |
163 size_t uv_stride = y_stride / 2; | |
160 // The *2 here is because some formats (e.g. h264) allow interlaced coding, | 164 // The *2 here is because some formats (e.g. h264) allow interlaced coding, |
161 // and then the size needs to be a multiple of two macroblocks (vertically). | 165 // and then the size needs to be a multiple of two macroblocks (vertically). |
162 // See libavcodec/utils.c:avcodec_align_dimensions2(). | 166 // See libavcodec/utils.c:avcodec_align_dimensions2(). |
163 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2); | 167 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2); |
164 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height; | 168 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height; |
165 size_t y_bytes = y_height * y_stride; | 169 size_t y_bytes = y_height * y_stride; |
166 size_t uv_bytes = uv_height * uv_stride; | 170 size_t uv_bytes = uv_height * uv_stride; |
167 | 171 |
168 #if !defined(OS_ANDROID) | 172 #if !defined(OS_ANDROID) |
169 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery | 173 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 break; | 322 break; |
319 for(int row = 0; row < rows(plane); row++) { | 323 for(int row = 0; row < rows(plane); row++) { |
320 base::MD5Update(context, base::StringPiece( | 324 base::MD5Update(context, base::StringPiece( |
321 reinterpret_cast<char*>(data(plane) + stride(plane) * row), | 325 reinterpret_cast<char*>(data(plane) + stride(plane) * row), |
322 row_bytes(plane))); | 326 row_bytes(plane))); |
323 } | 327 } |
324 } | 328 } |
325 } | 329 } |
326 | 330 |
327 } // namespace media | 331 } // namespace media |
OLD | NEW |