Chromium Code Reviews| 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 <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/memory/aligned_memory.h" | |
| 12 #include "base/string_piece.h" | 13 #include "base/string_piece.h" |
| 13 #include "media/base/limits.h" | 14 #include "media/base/limits.h" |
| 14 #include "media/base/video_util.h" | 15 #include "media/base/video_util.h" |
| 15 #if !defined(OS_ANDROID) | |
| 16 #include "media/ffmpeg/ffmpeg_common.h" | |
| 17 #endif | |
| 18 | 16 |
| 19 namespace media { | 17 namespace media { |
| 20 | 18 |
| 21 // static | 19 // static |
| 22 scoped_refptr<VideoFrame> VideoFrame::CreateFrame( | 20 scoped_refptr<VideoFrame> VideoFrame::CreateFrame( |
| 23 VideoFrame::Format format, | 21 VideoFrame::Format format, |
| 24 const gfx::Size& coded_size, | 22 const gfx::Size& coded_size, |
| 25 const gfx::Rect& visible_rect, | 23 const gfx::Rect& visible_rect, |
| 26 const gfx::Size& natural_size, | 24 const gfx::Size& natural_size, |
| 27 base::TimeDelta timestamp) { | 25 base::TimeDelta timestamp) { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 const base::TimeDelta kZero; | 135 const base::TimeDelta kZero; |
| 138 return CreateColorFrame(size, kBlackY, kBlackUV, kBlackUV, kZero); | 136 return CreateColorFrame(size, kBlackY, kBlackUV, kBlackUV, kZero); |
| 139 } | 137 } |
| 140 | 138 |
| 141 static inline size_t RoundUp(size_t value, size_t alignment) { | 139 static inline size_t RoundUp(size_t value, size_t alignment) { |
| 142 // Check that |alignment| is a power of 2. | 140 // Check that |alignment| is a power of 2. |
| 143 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); | 141 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); |
| 144 return ((value + (alignment - 1)) & ~(alignment-1)); | 142 return ((value + (alignment - 1)) & ~(alignment-1)); |
| 145 } | 143 } |
| 146 | 144 |
| 145 static const int kFrameAddressAlignment = 32; | |
|
DaleCurtis
2012/12/04 18:54:24
Hmm, I'm worried about the proliferation of these
xhwang
2012/12/04 19:40:50
I am not sure if VideoFrame should depend on Decod
DaleCurtis
2012/12/04 19:46:53
Can we add a header file with the constants and us
rbultje1
2012/12/04 19:53:28
FFmpeg has no constants for output padding, it is
xhwang
2012/12/04 20:54:56
Hmm, I feel we should keep input and output buffer
| |
| 147 static const int kFrameSizeAlignment = 16; | 146 static const int kFrameSizeAlignment = 16; |
| 148 // Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally. | 147 // Allows faster SIMD YUV convert. Also, FFmpeg overreads/-writes occasionally. |
| 149 static const int kFramePadBytes = 15; | 148 static const int kFramePadBytes = 15; |
|
DaleCurtis
2012/12/04 18:54:24
This should be 16 and just set to DecoderBuffer::k
xhwang
2012/12/04 19:40:50
Will do.
| |
| 150 | 149 |
| 151 // Release data allocated by AllocateRGB() or AllocateYUV(). | 150 // Release data allocated by AllocateRGB() or AllocateYUV(). |
| 152 static void ReleaseData(uint8* data) { | 151 static void ReleaseData(uint8* data) { |
| 153 DCHECK(data); | 152 DCHECK(data); |
| 154 if (data) { | 153 if (data) |
| 155 #if !defined(OS_ANDROID) | 154 base::AlignedFree(data); |
| 156 av_free(data); | |
| 157 #else | |
| 158 delete[] data; | |
| 159 #endif | |
| 160 } | |
| 161 } | 155 } |
| 162 | 156 |
| 163 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) { | 157 void VideoFrame::AllocateRGB(size_t bytes_per_pixel) { |
| 164 // Round up to align at least at a 16-byte boundary for each row. | 158 // 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). | 159 // This is sufficient for MMX and SSE2 reads (movq/movdqa). |
| 166 size_t bytes_per_row = RoundUp(coded_size_.width(), | 160 size_t bytes_per_row = RoundUp(coded_size_.width(), |
| 167 kFrameSizeAlignment) * bytes_per_pixel; | 161 kFrameSizeAlignment) * bytes_per_pixel; |
| 168 size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment); | 162 size_t aligned_height = RoundUp(coded_size_.height(), kFrameSizeAlignment); |
| 169 strides_[VideoFrame::kRGBPlane] = bytes_per_row; | 163 strides_[VideoFrame::kRGBPlane] = bytes_per_row; |
| 170 #if !defined(OS_ANDROID) | |
| 171 // 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 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>( | 164 data_[VideoFrame::kRGBPlane] = reinterpret_cast<uint8*>( |
| 174 av_malloc(bytes_per_row * aligned_height + kFramePadBytes)); | 165 base::AlignedAlloc(bytes_per_row * aligned_height + kFramePadBytes, |
| 175 #else | 166 kFrameAddressAlignment)); |
| 176 data_[VideoFrame::kRGBPlane] = new uint8_t[bytes_per_row * aligned_height]; | |
| 177 #endif | |
| 178 no_longer_needed_cb_ = base::Bind(&ReleaseData, data_[VideoFrame::kRGBPlane]); | 167 no_longer_needed_cb_ = base::Bind(&ReleaseData, data_[VideoFrame::kRGBPlane]); |
| 179 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7)); | 168 DCHECK(!(reinterpret_cast<intptr_t>(data_[VideoFrame::kRGBPlane]) & 7)); |
| 180 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0); | 169 COMPILE_ASSERT(0 == VideoFrame::kRGBPlane, RGB_data_must_be_index_0); |
| 181 } | 170 } |
| 182 | 171 |
| 183 void VideoFrame::AllocateYUV() { | 172 void VideoFrame::AllocateYUV() { |
| 184 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16); | 173 DCHECK(format_ == VideoFrame::YV12 || format_ == VideoFrame::YV16); |
| 185 // Align Y rows at least at 16 byte boundaries. The stride for both | 174 // Align Y rows at least at 16 byte boundaries. The stride for both |
| 186 // YV12 and YV16 is 1/2 of the stride of Y. For YV12, every row of bytes for | 175 // YV12 and YV16 is 1/2 of the stride of Y. For YV12, every row of bytes for |
| 187 // U and V applies to two rows of Y (one byte of UV for 4 bytes of Y), so in | 176 // U and V applies to two rows of Y (one byte of UV for 4 bytes of Y), so in |
| 188 // the case of YV12 the strides are identical for the same width surface, but | 177 // the case of YV12 the strides are identical for the same width surface, but |
| 189 // the number of bytes allocated for YV12 is 1/2 the amount for U & V as | 178 // the number of bytes allocated for YV12 is 1/2 the amount for U & V as |
| 190 // YV16. We also round the height of the surface allocated to be an even | 179 // YV16. We also round the height of the surface allocated to be an even |
| 191 // number to avoid any potential of faulting by code that attempts to access | 180 // number to avoid any potential of faulting by code that attempts to access |
| 192 // the Y values of the final row, but assumes that the last row of U & V | 181 // the Y values of the final row, but assumes that the last row of U & V |
| 193 // applies to a full two rows of Y. | 182 // applies to a full two rows of Y. |
| 194 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane), | 183 size_t y_stride = RoundUp(row_bytes(VideoFrame::kYPlane), |
| 195 kFrameSizeAlignment); | 184 kFrameSizeAlignment); |
| 196 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane), | 185 size_t uv_stride = RoundUp(row_bytes(VideoFrame::kUPlane), |
| 197 kFrameSizeAlignment); | 186 kFrameSizeAlignment); |
| 198 // The *2 here is because some formats (e.g. h264) allow interlaced coding, | 187 // 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). | 188 // and then the size needs to be a multiple of two macroblocks (vertically). |
| 200 // See libavcodec/utils.c:avcodec_align_dimensions2(). | 189 // See libavcodec/utils.c:avcodec_align_dimensions2(). |
| 201 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2); | 190 size_t y_height = RoundUp(coded_size_.height(), kFrameSizeAlignment * 2); |
| 202 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height; | 191 size_t uv_height = format_ == VideoFrame::YV12 ? y_height / 2 : y_height; |
| 203 size_t y_bytes = y_height * y_stride; | 192 size_t y_bytes = y_height * y_stride; |
| 204 size_t uv_bytes = uv_height * uv_stride; | 193 size_t uv_bytes = uv_height * uv_stride; |
| 205 | 194 |
| 206 #if !defined(OS_ANDROID) | |
| 207 // TODO(dalecurtis): use DataAligned or so, so this #ifdef hackery | |
| 208 // 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 | 195 // The extra line of UV being allocated is because h264 chroma MC |
| 210 // overreads by one line in some cases, see libavcodec/utils.c: | 196 // overreads by one line in some cases, see libavcodec/utils.c: |
| 211 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm: | 197 // avcodec_align_dimensions2() and libavcodec/x86/h264_chromamc.asm: |
| 212 // put_h264_chroma_mc4_ssse3(). | 198 // put_h264_chroma_mc4_ssse3(). |
| 213 uint8* data = reinterpret_cast<uint8*>( | 199 uint8* data = reinterpret_cast<uint8*>( |
| 214 av_malloc(y_bytes + (uv_bytes * 2 + uv_stride) + kFramePadBytes)); | 200 base::AlignedAlloc(y_bytes + (uv_bytes * 2 + uv_stride) + kFramePadBytes, |
| 215 #else | 201 kFrameAddressAlignment)); |
| 216 uint8* data = new uint8_t[y_bytes + (uv_bytes * 2)]; | |
| 217 #endif | |
| 218 no_longer_needed_cb_ = base::Bind(&ReleaseData, data); | 202 no_longer_needed_cb_ = base::Bind(&ReleaseData, data); |
| 219 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0); | 203 COMPILE_ASSERT(0 == VideoFrame::kYPlane, y_plane_data_must_be_index_0); |
| 220 data_[VideoFrame::kYPlane] = data; | 204 data_[VideoFrame::kYPlane] = data; |
| 221 data_[VideoFrame::kUPlane] = data + y_bytes; | 205 data_[VideoFrame::kUPlane] = data + y_bytes; |
| 222 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes; | 206 data_[VideoFrame::kVPlane] = data + y_bytes + uv_bytes; |
| 223 strides_[VideoFrame::kYPlane] = y_stride; | 207 strides_[VideoFrame::kYPlane] = y_stride; |
| 224 strides_[VideoFrame::kUPlane] = uv_stride; | 208 strides_[VideoFrame::kUPlane] = uv_stride; |
| 225 strides_[VideoFrame::kVPlane] = uv_stride; | 209 strides_[VideoFrame::kVPlane] = uv_stride; |
| 226 } | 210 } |
| 227 | 211 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 344 break; | 328 break; |
| 345 for (int row = 0; row < rows(plane); ++row) { | 329 for (int row = 0; row < rows(plane); ++row) { |
| 346 base::MD5Update(context, base::StringPiece( | 330 base::MD5Update(context, base::StringPiece( |
| 347 reinterpret_cast<char*>(data(plane) + stride(plane) * row), | 331 reinterpret_cast<char*>(data(plane) + stride(plane) * row), |
| 348 row_bytes(plane))); | 332 row_bytes(plane))); |
| 349 } | 333 } |
| 350 } | 334 } |
| 351 } | 335 } |
| 352 | 336 |
| 353 } // namespace media | 337 } // namespace media |
| OLD | NEW |