magjed@webrtc.org | 398a784 | 2015-03-05 14:03:08 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
perkj | d027178 | 2016-10-20 07:24:01 | [diff] [blame] | 10 | #include "webrtc/common_video/include/video_frame_buffer.h" |
| 11 | |
| 12 | #include <string.h> |
magjed@webrtc.org | 398a784 | 2015-03-05 14:03:08 | [diff] [blame] | 13 | |
Niels Möller | 34d0c32 | 2016-06-13 11:06:01 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | |
Henrik Kjellander | bc32410 | 2017-07-01 14:48:15 | [diff] [blame] | 16 | #include "webrtc/base/checks.h" |
| 17 | #include "webrtc/base/keep_ref_until_done.h" |
nisse | 5ed7972 | 2016-03-30 06:44:19 | [diff] [blame] | 18 | #include "libyuv/convert.h" |
fbarchard | 8bd3924 | 2016-07-09 00:33:20 | [diff] [blame] | 19 | #include "libyuv/planar_functions.h" |
Niels Möller | 34d0c32 | 2016-06-13 11:06:01 | [diff] [blame] | 20 | #include "libyuv/scale.h" |
magjed@webrtc.org | 398a784 | 2015-03-05 14:03:08 | [diff] [blame] | 21 | |
magjed@webrtc.org | 398a784 | 2015-03-05 14:03:08 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
Magnus Jedvert | a5090c1 | 2015-08-25 21:22:08 | [diff] [blame] | 24 | WrappedI420Buffer::WrappedI420Buffer(int width, |
Per | f92c6f8 | 2015-04-02 10:30:51 | [diff] [blame] | 25 | int height, |
| 26 | const uint8_t* y_plane, |
| 27 | int y_stride, |
| 28 | const uint8_t* u_plane, |
| 29 | int u_stride, |
| 30 | const uint8_t* v_plane, |
| 31 | int v_stride, |
| 32 | const rtc::Callback0<void>& no_longer_used) |
Magnus Jedvert | a5090c1 | 2015-08-25 21:22:08 | [diff] [blame] | 33 | : width_(width), |
| 34 | height_(height), |
| 35 | y_plane_(y_plane), |
| 36 | u_plane_(u_plane), |
| 37 | v_plane_(v_plane), |
| 38 | y_stride_(y_stride), |
| 39 | u_stride_(u_stride), |
| 40 | v_stride_(v_stride), |
| 41 | no_longer_used_cb_(no_longer_used) { |
Per | f92c6f8 | 2015-04-02 10:30:51 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | WrappedI420Buffer::~WrappedI420Buffer() { |
| 45 | no_longer_used_cb_(); |
| 46 | } |
| 47 | |
Per | f92c6f8 | 2015-04-02 10:30:51 | [diff] [blame] | 48 | int WrappedI420Buffer::width() const { |
| 49 | return width_; |
| 50 | } |
| 51 | |
| 52 | int WrappedI420Buffer::height() const { |
| 53 | return height_; |
| 54 | } |
| 55 | |
nisse | 1e4f606 | 2016-04-18 12:34:40 | [diff] [blame] | 56 | const uint8_t* WrappedI420Buffer::DataY() const { |
| 57 | return y_plane_; |
| 58 | } |
| 59 | const uint8_t* WrappedI420Buffer::DataU() const { |
| 60 | return u_plane_; |
| 61 | } |
| 62 | const uint8_t* WrappedI420Buffer::DataV() const { |
| 63 | return v_plane_; |
Per | f92c6f8 | 2015-04-02 10:30:51 | [diff] [blame] | 64 | } |
| 65 | |
nisse | 1e4f606 | 2016-04-18 12:34:40 | [diff] [blame] | 66 | int WrappedI420Buffer::StrideY() const { |
| 67 | return y_stride_; |
| 68 | } |
| 69 | int WrappedI420Buffer::StrideU() const { |
| 70 | return u_stride_; |
| 71 | } |
| 72 | int WrappedI420Buffer::StrideV() const { |
| 73 | return v_stride_; |
Per | f92c6f8 | 2015-04-02 10:30:51 | [diff] [blame] | 74 | } |
| 75 | |
yuweih | 7c9d6a2 | 2017-06-23 03:28:06 | [diff] [blame] | 76 | // Template to implement a wrapped buffer for a I4??BufferInterface. |
| 77 | template <typename Base> |
| 78 | class WrappedYuvBuffer : public Base { |
| 79 | public: |
| 80 | WrappedYuvBuffer(int width, |
| 81 | int height, |
| 82 | const uint8_t* y_plane, |
| 83 | int y_stride, |
| 84 | const uint8_t* u_plane, |
| 85 | int u_stride, |
| 86 | const uint8_t* v_plane, |
| 87 | int v_stride, |
| 88 | const rtc::Callback0<void>& no_longer_used) |
| 89 | : width_(width), |
| 90 | height_(height), |
| 91 | y_plane_(y_plane), |
| 92 | u_plane_(u_plane), |
| 93 | v_plane_(v_plane), |
| 94 | y_stride_(y_stride), |
| 95 | u_stride_(u_stride), |
| 96 | v_stride_(v_stride), |
| 97 | no_longer_used_cb_(no_longer_used) {} |
| 98 | |
| 99 | int width() const override { return width_; } |
| 100 | |
| 101 | int height() const override { return height_; } |
| 102 | |
| 103 | const uint8_t* DataY() const override { return y_plane_; } |
| 104 | |
| 105 | const uint8_t* DataU() const override { return u_plane_; } |
| 106 | |
| 107 | const uint8_t* DataV() const override { return v_plane_; } |
| 108 | |
| 109 | int StrideY() const override { return y_stride_; } |
| 110 | |
| 111 | int StrideU() const override { return u_stride_; } |
| 112 | |
| 113 | int StrideV() const override { return v_stride_; } |
| 114 | |
| 115 | private: |
| 116 | friend class rtc::RefCountedObject<WrappedYuvBuffer>; |
| 117 | |
| 118 | ~WrappedYuvBuffer() override { no_longer_used_cb_(); } |
| 119 | |
| 120 | const int width_; |
| 121 | const int height_; |
| 122 | const uint8_t* const y_plane_; |
| 123 | const uint8_t* const u_plane_; |
| 124 | const uint8_t* const v_plane_; |
| 125 | const int y_stride_; |
| 126 | const int u_stride_; |
| 127 | const int v_stride_; |
| 128 | rtc::Callback0<void> no_longer_used_cb_; |
| 129 | }; |
| 130 | |
| 131 | rtc::scoped_refptr<I420BufferInterface> WrapI420Buffer( |
| 132 | int width, |
| 133 | int height, |
| 134 | const uint8_t* y_plane, |
| 135 | int y_stride, |
| 136 | const uint8_t* u_plane, |
| 137 | int u_stride, |
| 138 | const uint8_t* v_plane, |
| 139 | int v_stride, |
| 140 | const rtc::Callback0<void>& no_longer_used) { |
| 141 | return rtc::scoped_refptr<I420BufferInterface>( |
| 142 | new rtc::RefCountedObject<WrappedYuvBuffer<I420BufferInterface>>( |
| 143 | width, height, y_plane, y_stride, u_plane, u_stride, v_plane, |
| 144 | v_stride, no_longer_used)); |
| 145 | } |
| 146 | |
| 147 | rtc::scoped_refptr<I444BufferInterface> WrapI444Buffer( |
| 148 | int width, |
| 149 | int height, |
| 150 | const uint8_t* y_plane, |
| 151 | int y_stride, |
| 152 | const uint8_t* u_plane, |
| 153 | int u_stride, |
| 154 | const uint8_t* v_plane, |
| 155 | int v_stride, |
| 156 | const rtc::Callback0<void>& no_longer_used) { |
| 157 | return rtc::scoped_refptr<I444BufferInterface>( |
| 158 | new rtc::RefCountedObject<WrappedYuvBuffer<I444BufferInterface>>( |
| 159 | width, height, y_plane, y_stride, u_plane, u_stride, v_plane, |
| 160 | v_stride, no_longer_used)); |
| 161 | } |
| 162 | |
| 163 | rtc::scoped_refptr<PlanarYuvBuffer> WrapYuvBuffer( |
| 164 | VideoFrameBuffer::Type type, |
| 165 | int width, |
| 166 | int height, |
| 167 | const uint8_t* y_plane, |
| 168 | int y_stride, |
| 169 | const uint8_t* u_plane, |
| 170 | int u_stride, |
| 171 | const uint8_t* v_plane, |
| 172 | int v_stride, |
| 173 | const rtc::Callback0<void>& no_longer_used) { |
| 174 | switch (type) { |
| 175 | case VideoFrameBuffer::Type::kI420: |
| 176 | return WrapI420Buffer(width, height, y_plane, y_stride, u_plane, u_stride, |
| 177 | v_plane, v_stride, no_longer_used); |
| 178 | case VideoFrameBuffer::Type::kI444: |
| 179 | return WrapI444Buffer(width, height, y_plane, y_stride, u_plane, u_stride, |
| 180 | v_plane, v_stride, no_longer_used); |
| 181 | default: |
| 182 | FATAL() << "Unexpected frame buffer type."; |
| 183 | return nullptr; |
| 184 | } |
| 185 | } |
| 186 | |
magjed@webrtc.org | 398a784 | 2015-03-05 14:03:08 | [diff] [blame] | 187 | } // namespace webrtc |