| 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 #include <climits> | 8 #include <climits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 static inline size_t RoundUp(size_t value, size_t alignment) { | 29 static inline size_t RoundUp(size_t value, size_t alignment) { |
| 30 DCHECK(IsPowerOfTwo(alignment)); | 30 DCHECK(IsPowerOfTwo(alignment)); |
| 31 return ((value + (alignment - 1)) & ~(alignment - 1)); | 31 return ((value + (alignment - 1)) & ~(alignment - 1)); |
| 32 } | 32 } |
| 33 | 33 |
| 34 static inline size_t RoundDown(size_t value, size_t alignment) { | 34 static inline size_t RoundDown(size_t value, size_t alignment) { |
| 35 DCHECK(IsPowerOfTwo(alignment)); | 35 DCHECK(IsPowerOfTwo(alignment)); |
| 36 return value & ~(alignment - 1); | 36 return value & ~(alignment - 1); |
| 37 } | 37 } |
| 38 | 38 |
| 39 static std::string ConfigToString(const VideoPixelFormat format, | |
| 40 const VideoFrame::StorageType storage_type, | |
| 41 const gfx::Size& coded_size, | |
| 42 const gfx::Rect& visible_rect, | |
| 43 const gfx::Size& natural_size) { | |
| 44 return base::StringPrintf( | |
| 45 "format:%s coded_size:%s visible_rect:%s natural_size:%s", | |
| 46 VideoPixelFormatToString(format).c_str(), coded_size.ToString().c_str(), | |
| 47 visible_rect.ToString().c_str(), natural_size.ToString().c_str()); | |
| 48 } | |
| 49 | |
| 50 static std::string StorageTypeToString( | 39 static std::string StorageTypeToString( |
| 51 const VideoFrame::StorageType storage_type) { | 40 const VideoFrame::StorageType storage_type) { |
| 52 switch (storage_type) { | 41 switch (storage_type) { |
| 53 case VideoFrame::STORAGE_UNKNOWN: | 42 case VideoFrame::STORAGE_UNKNOWN: |
| 54 return "UNKNOWN"; | 43 return "UNKNOWN"; |
| 55 case VideoFrame::STORAGE_OPAQUE: | 44 case VideoFrame::STORAGE_OPAQUE: |
| 56 return "OPAQUE"; | 45 return "OPAQUE"; |
| 57 case VideoFrame::STORAGE_UNOWNED_MEMORY: | 46 case VideoFrame::STORAGE_UNOWNED_MEMORY: |
| 58 return "UNOWNED_MEMORY"; | 47 return "UNOWNED_MEMORY"; |
| 59 case VideoFrame::STORAGE_OWNED_MEMORY: | 48 case VideoFrame::STORAGE_OWNED_MEMORY: |
| 60 return "OWNED_MEMORY"; | 49 return "OWNED_MEMORY"; |
| 61 case VideoFrame::STORAGE_SHMEM: | 50 case VideoFrame::STORAGE_SHMEM: |
| 62 return "SHMEM"; | 51 return "SHMEM"; |
| 63 #if defined(OS_LINUX) | 52 #if defined(OS_LINUX) |
| 64 case VideoFrame::STORAGE_DMABUFS: | 53 case VideoFrame::STORAGE_DMABUFS: |
| 65 return "DMABUFS"; | 54 return "DMABUFS"; |
| 66 #endif | 55 #endif |
| 67 #if defined(VIDEO_HOLE) | 56 #if defined(VIDEO_HOLE) |
| 68 case VideoFrame::STORAGE_HOLE: | 57 case VideoFrame::STORAGE_HOLE: |
| 69 return "HOLE"; | 58 return "HOLE"; |
| 70 #endif | 59 #endif |
| 71 case VideoFrame::STORAGE_GPU_MEMORY_BUFFERS: | 60 case VideoFrame::STORAGE_GPU_MEMORY_BUFFERS: |
| 72 return "GPU_MEMORY_BUFFERS"; | 61 return "GPU_MEMORY_BUFFERS"; |
| 62 case VideoFrame::STORAGE_MOJO_SHARED_BUFFER: |
| 63 return "MOJO_SHARED_BUFFER"; |
| 73 } | 64 } |
| 74 | 65 |
| 75 NOTREACHED() << "Invalid StorageType provided: " << storage_type; | 66 NOTREACHED() << "Invalid StorageType provided: " << storage_type; |
| 76 return "INVALID"; | 67 return "INVALID"; |
| 77 } | 68 } |
| 78 | 69 |
| 79 // Returns true if |plane| is a valid plane index for the given |format|. | |
| 80 static bool IsValidPlane(size_t plane, VideoPixelFormat format) { | |
| 81 DCHECK_LE(VideoFrame::NumPlanes(format), | |
| 82 static_cast<size_t>(VideoFrame::kMaxPlanes)); | |
| 83 return (plane < VideoFrame::NumPlanes(format)); | |
| 84 } | |
| 85 | |
| 86 // Returns true if |frame| is accesible mapped in the VideoFrame memory space. | 70 // Returns true if |frame| is accesible mapped in the VideoFrame memory space. |
| 87 // static | 71 // static |
| 88 static bool IsStorageTypeMappable(VideoFrame::StorageType storage_type) { | 72 static bool IsStorageTypeMappable(VideoFrame::StorageType storage_type) { |
| 89 return | 73 return |
| 90 #if defined(OS_LINUX) | 74 #if defined(OS_LINUX) |
| 91 // This is not strictly needed but makes explicit that, at VideoFrame | 75 // This is not strictly needed but makes explicit that, at VideoFrame |
| 92 // level, DmaBufs are not mappable from userspace. | 76 // level, DmaBufs are not mappable from userspace. |
| 93 storage_type != VideoFrame::STORAGE_DMABUFS && | 77 storage_type != VideoFrame::STORAGE_DMABUFS && |
| 94 #endif | 78 #endif |
| 95 (storage_type == VideoFrame::STORAGE_UNOWNED_MEMORY || | 79 (storage_type == VideoFrame::STORAGE_UNOWNED_MEMORY || |
| 96 storage_type == VideoFrame::STORAGE_OWNED_MEMORY || | 80 storage_type == VideoFrame::STORAGE_OWNED_MEMORY || |
| 97 storage_type == VideoFrame::STORAGE_SHMEM || | 81 storage_type == VideoFrame::STORAGE_SHMEM || |
| 98 storage_type == VideoFrame::STORAGE_GPU_MEMORY_BUFFERS); | 82 storage_type == VideoFrame::STORAGE_GPU_MEMORY_BUFFERS || |
| 99 } | 83 storage_type == VideoFrame::STORAGE_MOJO_SHARED_BUFFER); |
| 100 | |
| 101 // Returns the pixel size per element for given |plane| and |format|. E.g. 2x2 | |
| 102 // for the U-plane in PIXEL_FORMAT_I420. | |
| 103 static gfx::Size SampleSize(VideoPixelFormat format, size_t plane) { | |
| 104 DCHECK(IsValidPlane(plane, format)); | |
| 105 | |
| 106 switch (plane) { | |
| 107 case VideoFrame::kYPlane: | |
| 108 case VideoFrame::kAPlane: | |
| 109 return gfx::Size(1, 1); | |
| 110 | |
| 111 case VideoFrame::kUPlane: // and VideoFrame::kUVPlane: | |
| 112 case VideoFrame::kVPlane: | |
| 113 switch (format) { | |
| 114 case PIXEL_FORMAT_YV24: | |
| 115 case PIXEL_FORMAT_YUV444P9: | |
| 116 case PIXEL_FORMAT_YUV444P10: | |
| 117 return gfx::Size(1, 1); | |
| 118 | |
| 119 case PIXEL_FORMAT_YV16: | |
| 120 case PIXEL_FORMAT_YUV422P9: | |
| 121 case PIXEL_FORMAT_YUV422P10: | |
| 122 return gfx::Size(2, 1); | |
| 123 | |
| 124 case PIXEL_FORMAT_YV12: | |
| 125 case PIXEL_FORMAT_I420: | |
| 126 case PIXEL_FORMAT_YV12A: | |
| 127 case PIXEL_FORMAT_NV12: | |
| 128 case PIXEL_FORMAT_NV21: | |
| 129 case PIXEL_FORMAT_MT21: | |
| 130 case PIXEL_FORMAT_YUV420P9: | |
| 131 case PIXEL_FORMAT_YUV420P10: | |
| 132 return gfx::Size(2, 2); | |
| 133 | |
| 134 case PIXEL_FORMAT_UNKNOWN: | |
| 135 case PIXEL_FORMAT_UYVY: | |
| 136 case PIXEL_FORMAT_YUY2: | |
| 137 case PIXEL_FORMAT_ARGB: | |
| 138 case PIXEL_FORMAT_XRGB: | |
| 139 case PIXEL_FORMAT_RGB24: | |
| 140 case PIXEL_FORMAT_RGB32: | |
| 141 case PIXEL_FORMAT_MJPEG: | |
| 142 break; | |
| 143 } | |
| 144 } | |
| 145 NOTREACHED(); | |
| 146 return gfx::Size(); | |
| 147 } | |
| 148 | |
| 149 // Return the alignment for the whole frame, calculated as the max of the | |
| 150 // alignment for each individual plane. | |
| 151 static gfx::Size CommonAlignment(VideoPixelFormat format) { | |
| 152 int max_sample_width = 0; | |
| 153 int max_sample_height = 0; | |
| 154 for (size_t plane = 0; plane < VideoFrame::NumPlanes(format); ++plane) { | |
| 155 const gfx::Size sample_size = SampleSize(format, plane); | |
| 156 max_sample_width = std::max(max_sample_width, sample_size.width()); | |
| 157 max_sample_height = std::max(max_sample_height, sample_size.height()); | |
| 158 } | |
| 159 return gfx::Size(max_sample_width, max_sample_height); | |
| 160 } | |
| 161 | |
| 162 // Returns the number of bytes per element for given |plane| and |format|. | |
| 163 static int BytesPerElement(VideoPixelFormat format, size_t plane) { | |
| 164 DCHECK(IsValidPlane(plane, format)); | |
| 165 switch (format) { | |
| 166 case PIXEL_FORMAT_ARGB: | |
| 167 case PIXEL_FORMAT_XRGB: | |
| 168 case PIXEL_FORMAT_RGB32: | |
| 169 return 4; | |
| 170 case PIXEL_FORMAT_RGB24: | |
| 171 return 3; | |
| 172 case PIXEL_FORMAT_UYVY: | |
| 173 case PIXEL_FORMAT_YUY2: | |
| 174 case PIXEL_FORMAT_YUV420P9: | |
| 175 case PIXEL_FORMAT_YUV422P9: | |
| 176 case PIXEL_FORMAT_YUV444P9: | |
| 177 case PIXEL_FORMAT_YUV420P10: | |
| 178 case PIXEL_FORMAT_YUV422P10: | |
| 179 case PIXEL_FORMAT_YUV444P10: | |
| 180 return 2; | |
| 181 case PIXEL_FORMAT_NV12: | |
| 182 case PIXEL_FORMAT_NV21: | |
| 183 case PIXEL_FORMAT_MT21: { | |
| 184 static const int bytes_per_element[] = {1, 2}; | |
| 185 DCHECK_LT(plane, arraysize(bytes_per_element)); | |
| 186 return bytes_per_element[plane]; | |
| 187 } | |
| 188 case PIXEL_FORMAT_YV12: | |
| 189 case PIXEL_FORMAT_I420: | |
| 190 case PIXEL_FORMAT_YV16: | |
| 191 case PIXEL_FORMAT_YV12A: | |
| 192 case PIXEL_FORMAT_YV24: | |
| 193 return 1; | |
| 194 case PIXEL_FORMAT_MJPEG: | |
| 195 return 0; | |
| 196 case PIXEL_FORMAT_UNKNOWN: | |
| 197 break; | |
| 198 } | |
| 199 NOTREACHED(); | |
| 200 return 0; | |
| 201 } | 84 } |
| 202 | 85 |
| 203 // static | 86 // static |
| 204 bool VideoFrame::IsValidConfig(VideoPixelFormat format, | 87 bool VideoFrame::IsValidConfig(VideoPixelFormat format, |
| 205 StorageType storage_type, | 88 StorageType storage_type, |
| 206 const gfx::Size& coded_size, | 89 const gfx::Size& coded_size, |
| 207 const gfx::Rect& visible_rect, | 90 const gfx::Rect& visible_rect, |
| 208 const gfx::Size& natural_size) { | 91 const gfx::Size& natural_size) { |
| 209 // Check maximum limits for all formats. | 92 // Check maximum limits for all formats. |
| 210 int coded_size_area = coded_size.GetCheckedArea().ValueOrDefault(INT_MAX); | 93 int coded_size_area = coded_size.GetCheckedArea().ValueOrDefault(INT_MAX); |
| (...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 870 if (release_sync_token_.HasData()) | 753 if (release_sync_token_.HasData()) |
| 871 client->WaitSyncToken(release_sync_token_); | 754 client->WaitSyncToken(release_sync_token_); |
| 872 client->GenerateSyncToken(&release_sync_token_); | 755 client->GenerateSyncToken(&release_sync_token_); |
| 873 } | 756 } |
| 874 | 757 |
| 875 std::string VideoFrame::AsHumanReadableString() { | 758 std::string VideoFrame::AsHumanReadableString() { |
| 876 if (metadata()->IsTrue(media::VideoFrameMetadata::END_OF_STREAM)) | 759 if (metadata()->IsTrue(media::VideoFrameMetadata::END_OF_STREAM)) |
| 877 return "end of stream"; | 760 return "end of stream"; |
| 878 | 761 |
| 879 std::ostringstream s; | 762 std::ostringstream s; |
| 880 s << "format: " << VideoPixelFormatToString(format_) | 763 s << ConfigToString(format_, storage_type_, coded_size_, visible_rect_, |
| 881 << " storage_type: " << StorageTypeToString(storage_type_) | 764 natural_size_) |
| 882 << " coded_size: " << coded_size_.ToString() | 765 << " timestamp:" << timestamp_.InMicroseconds(); |
| 883 << " visible_rect: " << visible_rect_.ToString() | |
| 884 << " natural_size: " << natural_size_.ToString() | |
| 885 << " timestamp: " << timestamp_.InMicroseconds(); | |
| 886 return s.str(); | 766 return s.str(); |
| 887 } | 767 } |
| 888 | 768 |
| 889 // static | 769 // static |
| 890 scoped_refptr<VideoFrame> VideoFrame::WrapExternalStorage( | 770 scoped_refptr<VideoFrame> VideoFrame::WrapExternalStorage( |
| 891 VideoPixelFormat format, | 771 VideoPixelFormat format, |
| 892 StorageType storage_type, | 772 StorageType storage_type, |
| 893 const gfx::Size& coded_size, | 773 const gfx::Size& coded_size, |
| 894 const gfx::Rect& visible_rect, | 774 const gfx::Rect& visible_rect, |
| 895 const gfx::Size& natural_size, | 775 const gfx::Size& natural_size, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 951 shared_memory_handle_(base::SharedMemory::NULLHandle()), | 831 shared_memory_handle_(base::SharedMemory::NULLHandle()), |
| 952 shared_memory_offset_(0), | 832 shared_memory_offset_(0), |
| 953 timestamp_(timestamp) { | 833 timestamp_(timestamp) { |
| 954 DCHECK(IsValidConfig(format_, storage_type, coded_size_, visible_rect_, | 834 DCHECK(IsValidConfig(format_, storage_type, coded_size_, visible_rect_, |
| 955 natural_size_)); | 835 natural_size_)); |
| 956 memset(&mailbox_holders_, 0, sizeof(mailbox_holders_)); | 836 memset(&mailbox_holders_, 0, sizeof(mailbox_holders_)); |
| 957 memset(&strides_, 0, sizeof(strides_)); | 837 memset(&strides_, 0, sizeof(strides_)); |
| 958 memset(&data_, 0, sizeof(data_)); | 838 memset(&data_, 0, sizeof(data_)); |
| 959 } | 839 } |
| 960 | 840 |
| 841 VideoFrame::~VideoFrame() { |
| 842 if (!mailbox_holders_release_cb_.is_null()) { |
| 843 gpu::SyncToken release_sync_token; |
| 844 { |
| 845 // To ensure that changes to |release_sync_token_| are visible on this |
| 846 // thread (imply a memory barrier). |
| 847 base::AutoLock locker(release_sync_token_lock_); |
| 848 release_sync_token = release_sync_token_; |
| 849 } |
| 850 base::ResetAndReturn(&mailbox_holders_release_cb_).Run(release_sync_token); |
| 851 } |
| 852 |
| 853 for (auto& callback : done_callbacks_) |
| 854 base::ResetAndReturn(&callback).Run(); |
| 855 } |
| 856 |
| 857 // static |
| 858 std::string VideoFrame::ConfigToString(const VideoPixelFormat format, |
| 859 const StorageType storage_type, |
| 860 const gfx::Size& coded_size, |
| 861 const gfx::Rect& visible_rect, |
| 862 const gfx::Size& natural_size) { |
| 863 return base::StringPrintf( |
| 864 "format:%s storage_type:%s coded_size:%s visible_rect:%s natural_size:%s", |
| 865 VideoPixelFormatToString(format).c_str(), |
| 866 StorageTypeToString(storage_type).c_str(), coded_size.ToString().c_str(), |
| 867 visible_rect.ToString().c_str(), natural_size.ToString().c_str()); |
| 868 } |
| 869 |
| 870 // static |
| 871 bool VideoFrame::IsValidPlane(size_t plane, VideoPixelFormat format) { |
| 872 DCHECK_LE(NumPlanes(format), static_cast<size_t>(kMaxPlanes)); |
| 873 return (plane < NumPlanes(format)); |
| 874 } |
| 875 |
| 876 // static |
| 877 gfx::Size VideoFrame::DetermineAlignedSize(VideoPixelFormat format, |
| 878 const gfx::Size& dimensions) { |
| 879 const gfx::Size alignment = CommonAlignment(format); |
| 880 const gfx::Size adjusted = |
| 881 gfx::Size(RoundUp(dimensions.width(), alignment.width()), |
| 882 RoundUp(dimensions.height(), alignment.height())); |
| 883 DCHECK((adjusted.width() % alignment.width() == 0) && |
| 884 (adjusted.height() % alignment.height() == 0)); |
| 885 return adjusted; |
| 886 } |
| 887 |
| 888 void VideoFrame::set_data(size_t plane, uint8_t* ptr) { |
| 889 DCHECK(IsValidPlane(plane, format_)); |
| 890 DCHECK(ptr); |
| 891 data_[plane] = ptr; |
| 892 } |
| 893 |
| 894 void VideoFrame::set_stride(size_t plane, int stride) { |
| 895 DCHECK(IsValidPlane(plane, format_)); |
| 896 DCHECK_GT(stride, 0); |
| 897 strides_[plane] = stride; |
| 898 } |
| 899 |
| 961 VideoFrame::VideoFrame(VideoPixelFormat format, | 900 VideoFrame::VideoFrame(VideoPixelFormat format, |
| 962 StorageType storage_type, | 901 StorageType storage_type, |
| 963 const gfx::Size& coded_size, | 902 const gfx::Size& coded_size, |
| 964 const gfx::Rect& visible_rect, | 903 const gfx::Rect& visible_rect, |
| 965 const gfx::Size& natural_size, | 904 const gfx::Size& natural_size, |
| 966 base::TimeDelta timestamp, | 905 base::TimeDelta timestamp, |
| 967 base::SharedMemoryHandle handle, | 906 base::SharedMemoryHandle handle, |
| 968 size_t shared_memory_offset) | 907 size_t shared_memory_offset) |
| 969 : VideoFrame(format, | 908 : VideoFrame(format, |
| 970 storage_type, | 909 storage_type, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 988 : VideoFrame(format, | 927 : VideoFrame(format, |
| 989 storage_type, | 928 storage_type, |
| 990 coded_size, | 929 coded_size, |
| 991 visible_rect, | 930 visible_rect, |
| 992 natural_size, | 931 natural_size, |
| 993 timestamp) { | 932 timestamp) { |
| 994 memcpy(&mailbox_holders_, mailbox_holders, sizeof(mailbox_holders_)); | 933 memcpy(&mailbox_holders_, mailbox_holders, sizeof(mailbox_holders_)); |
| 995 mailbox_holders_release_cb_ = mailbox_holder_release_cb; | 934 mailbox_holders_release_cb_ = mailbox_holder_release_cb; |
| 996 } | 935 } |
| 997 | 936 |
| 998 VideoFrame::~VideoFrame() { | |
| 999 if (!mailbox_holders_release_cb_.is_null()) { | |
| 1000 gpu::SyncToken release_sync_token; | |
| 1001 { | |
| 1002 // To ensure that changes to |release_sync_token_| are visible on this | |
| 1003 // thread (imply a memory barrier). | |
| 1004 base::AutoLock locker(release_sync_token_lock_); | |
| 1005 release_sync_token = release_sync_token_; | |
| 1006 } | |
| 1007 base::ResetAndReturn(&mailbox_holders_release_cb_).Run(release_sync_token); | |
| 1008 } | |
| 1009 | |
| 1010 for (auto& callback : done_callbacks_) | |
| 1011 base::ResetAndReturn(&callback).Run(); | |
| 1012 } | |
| 1013 | |
| 1014 // static | 937 // static |
| 1015 scoped_refptr<VideoFrame> VideoFrame::CreateFrameInternal( | 938 scoped_refptr<VideoFrame> VideoFrame::CreateFrameInternal( |
| 1016 VideoPixelFormat format, | 939 VideoPixelFormat format, |
| 1017 const gfx::Size& coded_size, | 940 const gfx::Size& coded_size, |
| 1018 const gfx::Rect& visible_rect, | 941 const gfx::Rect& visible_rect, |
| 1019 const gfx::Size& natural_size, | 942 const gfx::Size& natural_size, |
| 1020 base::TimeDelta timestamp, | 943 base::TimeDelta timestamp, |
| 1021 bool zero_initialize_memory) { | 944 bool zero_initialize_memory) { |
| 1022 if (!IsYuvPlanar(format)) { | 945 if (!IsYuvPlanar(format)) { |
| 1023 NOTIMPLEMENTED(); | 946 NOTIMPLEMENTED(); |
| 1024 return nullptr; | 947 return nullptr; |
| 1025 } | 948 } |
| 1026 | 949 |
| 1027 // Since we're creating a new YUV frame (and allocating memory for it | 950 // Since we're creating a new YUV frame (and allocating memory for it |
| 1028 // ourselves), we can pad the requested |coded_size| if necessary if the | 951 // ourselves), we can pad the requested |coded_size| if necessary if the |
| 1029 // request does not line up on sample boundaries. See discussion at | 952 // request does not line up on sample boundaries. See discussion at |
| 1030 // http://crrev.com/1240833003 | 953 // http://crrev.com/1240833003 |
| 1031 const gfx::Size alignment = CommonAlignment(format); | 954 const gfx::Size new_coded_size = DetermineAlignedSize(format, coded_size); |
| 1032 const gfx::Size new_coded_size = | |
| 1033 gfx::Size(RoundUp(coded_size.width(), alignment.width()), | |
| 1034 RoundUp(coded_size.height(), alignment.height())); | |
| 1035 DCHECK((new_coded_size.width() % alignment.width() == 0) && | |
| 1036 (new_coded_size.height() % alignment.height() == 0)); | |
| 1037 | |
| 1038 const StorageType storage = STORAGE_OWNED_MEMORY; | 955 const StorageType storage = STORAGE_OWNED_MEMORY; |
| 1039 if (!IsValidConfig(format, storage, new_coded_size, visible_rect, | 956 if (!IsValidConfig(format, storage, new_coded_size, visible_rect, |
| 1040 natural_size)) { | 957 natural_size)) { |
| 1041 LOG(DFATAL) << __FUNCTION__ << " Invalid config." | 958 LOG(DFATAL) << __FUNCTION__ << " Invalid config." |
| 1042 << ConfigToString(format, storage, coded_size, visible_rect, | 959 << ConfigToString(format, storage, coded_size, visible_rect, |
| 1043 natural_size); | 960 natural_size); |
| 1044 return nullptr; | 961 return nullptr; |
| 1045 } | 962 } |
| 1046 | 963 |
| 1047 scoped_refptr<VideoFrame> frame(new VideoFrame( | 964 scoped_refptr<VideoFrame> frame(new VideoFrame( |
| 1048 format, storage, new_coded_size, visible_rect, natural_size, timestamp)); | 965 format, storage, new_coded_size, visible_rect, natural_size, timestamp)); |
| 1049 frame->AllocateYUV(zero_initialize_memory); | 966 frame->AllocateYUV(zero_initialize_memory); |
| 1050 return frame; | 967 return frame; |
| 1051 } | 968 } |
| 1052 | 969 |
| 970 // static |
| 971 gfx::Size VideoFrame::SampleSize(VideoPixelFormat format, size_t plane) { |
| 972 DCHECK(IsValidPlane(plane, format)); |
| 973 |
| 974 switch (plane) { |
| 975 case kYPlane: |
| 976 case kAPlane: |
| 977 return gfx::Size(1, 1); |
| 978 |
| 979 case kUPlane: // and kUVPlane: |
| 980 case kVPlane: |
| 981 switch (format) { |
| 982 case PIXEL_FORMAT_YV24: |
| 983 case PIXEL_FORMAT_YUV444P9: |
| 984 case PIXEL_FORMAT_YUV444P10: |
| 985 return gfx::Size(1, 1); |
| 986 |
| 987 case PIXEL_FORMAT_YV16: |
| 988 case PIXEL_FORMAT_YUV422P9: |
| 989 case PIXEL_FORMAT_YUV422P10: |
| 990 return gfx::Size(2, 1); |
| 991 |
| 992 case PIXEL_FORMAT_YV12: |
| 993 case PIXEL_FORMAT_I420: |
| 994 case PIXEL_FORMAT_YV12A: |
| 995 case PIXEL_FORMAT_NV12: |
| 996 case PIXEL_FORMAT_NV21: |
| 997 case PIXEL_FORMAT_MT21: |
| 998 case PIXEL_FORMAT_YUV420P9: |
| 999 case PIXEL_FORMAT_YUV420P10: |
| 1000 return gfx::Size(2, 2); |
| 1001 |
| 1002 case PIXEL_FORMAT_UNKNOWN: |
| 1003 case PIXEL_FORMAT_UYVY: |
| 1004 case PIXEL_FORMAT_YUY2: |
| 1005 case PIXEL_FORMAT_ARGB: |
| 1006 case PIXEL_FORMAT_XRGB: |
| 1007 case PIXEL_FORMAT_RGB24: |
| 1008 case PIXEL_FORMAT_RGB32: |
| 1009 case PIXEL_FORMAT_MJPEG: |
| 1010 break; |
| 1011 } |
| 1012 } |
| 1013 NOTREACHED(); |
| 1014 return gfx::Size(); |
| 1015 } |
| 1016 |
| 1017 // static |
| 1018 int VideoFrame::BytesPerElement(VideoPixelFormat format, size_t plane) { |
| 1019 DCHECK(IsValidPlane(plane, format)); |
| 1020 switch (format) { |
| 1021 case PIXEL_FORMAT_ARGB: |
| 1022 case PIXEL_FORMAT_XRGB: |
| 1023 case PIXEL_FORMAT_RGB32: |
| 1024 return 4; |
| 1025 case PIXEL_FORMAT_RGB24: |
| 1026 return 3; |
| 1027 case PIXEL_FORMAT_UYVY: |
| 1028 case PIXEL_FORMAT_YUY2: |
| 1029 case PIXEL_FORMAT_YUV420P9: |
| 1030 case PIXEL_FORMAT_YUV422P9: |
| 1031 case PIXEL_FORMAT_YUV444P9: |
| 1032 case PIXEL_FORMAT_YUV420P10: |
| 1033 case PIXEL_FORMAT_YUV422P10: |
| 1034 case PIXEL_FORMAT_YUV444P10: |
| 1035 return 2; |
| 1036 case PIXEL_FORMAT_NV12: |
| 1037 case PIXEL_FORMAT_NV21: |
| 1038 case PIXEL_FORMAT_MT21: { |
| 1039 static const int bytes_per_element[] = {1, 2}; |
| 1040 DCHECK_LT(plane, arraysize(bytes_per_element)); |
| 1041 return bytes_per_element[plane]; |
| 1042 } |
| 1043 case PIXEL_FORMAT_YV12: |
| 1044 case PIXEL_FORMAT_I420: |
| 1045 case PIXEL_FORMAT_YV16: |
| 1046 case PIXEL_FORMAT_YV12A: |
| 1047 case PIXEL_FORMAT_YV24: |
| 1048 return 1; |
| 1049 case PIXEL_FORMAT_MJPEG: |
| 1050 return 0; |
| 1051 case PIXEL_FORMAT_UNKNOWN: |
| 1052 break; |
| 1053 } |
| 1054 NOTREACHED(); |
| 1055 return 0; |
| 1056 } |
| 1057 |
| 1058 // static |
| 1059 gfx::Size VideoFrame::CommonAlignment(VideoPixelFormat format) { |
| 1060 int max_sample_width = 0; |
| 1061 int max_sample_height = 0; |
| 1062 for (size_t plane = 0; plane < NumPlanes(format); ++plane) { |
| 1063 const gfx::Size sample_size = SampleSize(format, plane); |
| 1064 max_sample_width = std::max(max_sample_width, sample_size.width()); |
| 1065 max_sample_height = std::max(max_sample_height, sample_size.height()); |
| 1066 } |
| 1067 return gfx::Size(max_sample_width, max_sample_height); |
| 1068 } |
| 1069 |
| 1053 void VideoFrame::AllocateYUV(bool zero_initialize_memory) { | 1070 void VideoFrame::AllocateYUV(bool zero_initialize_memory) { |
| 1054 DCHECK_EQ(storage_type_, STORAGE_OWNED_MEMORY); | 1071 DCHECK_EQ(storage_type_, STORAGE_OWNED_MEMORY); |
| 1055 static_assert(0 == kYPlane, "y plane data must be index 0"); | 1072 static_assert(0 == kYPlane, "y plane data must be index 0"); |
| 1056 | 1073 |
| 1057 size_t data_size = 0; | 1074 size_t data_size = 0; |
| 1058 size_t offset[kMaxPlanes]; | 1075 size_t offset[kMaxPlanes]; |
| 1059 for (size_t plane = 0; plane < NumPlanes(format_); ++plane) { | 1076 for (size_t plane = 0; plane < NumPlanes(format_); ++plane) { |
| 1060 // The *2 in alignment for height is because some formats (e.g. h264) allow | 1077 // The *2 in alignment for height is because some formats (e.g. h264) allow |
| 1061 // interlaced coding, and then the size needs to be a multiple of two | 1078 // interlaced coding, and then the size needs to be a multiple of two |
| 1062 // macroblocks (vertically). See | 1079 // macroblocks (vertically). See |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1079 if (zero_initialize_memory) | 1096 if (zero_initialize_memory) |
| 1080 memset(data, 0, data_size); | 1097 memset(data, 0, data_size); |
| 1081 | 1098 |
| 1082 for (size_t plane = 0; plane < NumPlanes(format_); ++plane) | 1099 for (size_t plane = 0; plane < NumPlanes(format_); ++plane) |
| 1083 data_[plane] = data + offset[plane]; | 1100 data_[plane] = data + offset[plane]; |
| 1084 | 1101 |
| 1085 AddDestructionObserver(base::Bind(&base::AlignedFree, data)); | 1102 AddDestructionObserver(base::Bind(&base::AlignedFree, data)); |
| 1086 } | 1103 } |
| 1087 | 1104 |
| 1088 } // namespace media | 1105 } // namespace media |
| OLD | NEW |