OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/video_coding/codecs/i420/include/i420.h" | 11 #include "webrtc/modules/video_coding/codecs/i420/include/i420.h" |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
14 #include <string> | 14 #include <string> |
15 | 15 |
16 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 16 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 const size_t kI420HeaderSize = 4; | 19 const size_t kI420HeaderSize = 4; |
20 } | 20 } |
21 | 21 |
22 namespace webrtc { | 22 namespace webrtc { |
23 | 23 |
24 I420Encoder::I420Encoder() : _inited(false), _encodedImage(), | 24 I420Encoder::I420Encoder() |
25 _encodedCompleteCallback(NULL) { | 25 : _inited(false), _encodedImage(), _encodedCompleteCallback(NULL) {} |
26 } | |
27 | 26 |
28 I420Encoder::~I420Encoder() { | 27 I420Encoder::~I420Encoder() { |
29 _inited = false; | 28 _inited = false; |
30 delete [] _encodedImage._buffer; | 29 delete[] _encodedImage._buffer; |
31 } | 30 } |
32 | 31 |
33 int I420Encoder::Release() { | 32 int I420Encoder::Release() { |
34 // Should allocate an encoded frame and then release it here, for that we | 33 // Should allocate an encoded frame and then release it here, for that we |
35 // actually need an init flag. | 34 // actually need an init flag. |
36 if (_encodedImage._buffer != NULL) { | 35 if (_encodedImage._buffer != NULL) { |
37 delete [] _encodedImage._buffer; | 36 delete[] _encodedImage._buffer; |
38 _encodedImage._buffer = NULL; | 37 _encodedImage._buffer = NULL; |
39 } | 38 } |
40 _inited = false; | 39 _inited = false; |
41 return WEBRTC_VIDEO_CODEC_OK; | 40 return WEBRTC_VIDEO_CODEC_OK; |
42 } | 41 } |
43 | 42 |
44 int I420Encoder::InitEncode(const VideoCodec* codecSettings, | 43 int I420Encoder::InitEncode(const VideoCodec* codecSettings, |
45 int /*numberOfCores*/, | 44 int /*numberOfCores*/, |
46 size_t /*maxPayloadSize */) { | 45 size_t /*maxPayloadSize */) { |
47 if (codecSettings == NULL) { | 46 if (codecSettings == NULL) { |
48 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 47 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
49 } | 48 } |
50 if (codecSettings->width < 1 || codecSettings->height < 1) { | 49 if (codecSettings->width < 1 || codecSettings->height < 1) { |
51 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 50 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
52 } | 51 } |
53 | 52 |
54 // Allocating encoded memory. | 53 // Allocating encoded memory. |
55 if (_encodedImage._buffer != NULL) { | 54 if (_encodedImage._buffer != NULL) { |
56 delete [] _encodedImage._buffer; | 55 delete[] _encodedImage._buffer; |
57 _encodedImage._buffer = NULL; | 56 _encodedImage._buffer = NULL; |
58 _encodedImage._size = 0; | 57 _encodedImage._size = 0; |
59 } | 58 } |
60 const size_t newSize = | 59 const size_t newSize = |
61 CalcBufferSize(kI420, codecSettings->width, codecSettings->height) + | 60 CalcBufferSize(kI420, codecSettings->width, codecSettings->height) + |
62 kI420HeaderSize; | 61 kI420HeaderSize; |
63 uint8_t* newBuffer = new uint8_t[newSize]; | 62 uint8_t* newBuffer = new uint8_t[newSize]; |
64 if (newBuffer == NULL) { | 63 if (newBuffer == NULL) { |
65 return WEBRTC_VIDEO_CODEC_MEMORY; | 64 return WEBRTC_VIDEO_CODEC_MEMORY; |
66 } | 65 } |
(...skipping 27 matching lines...) Expand all Loading... |
94 int height = inputImage.height(); | 93 int height = inputImage.height(); |
95 if (height > std::numeric_limits<uint16_t>::max()) { | 94 if (height > std::numeric_limits<uint16_t>::max()) { |
96 return WEBRTC_VIDEO_CODEC_ERR_SIZE; | 95 return WEBRTC_VIDEO_CODEC_ERR_SIZE; |
97 } | 96 } |
98 | 97 |
99 size_t req_length = | 98 size_t req_length = |
100 CalcBufferSize(kI420, inputImage.width(), inputImage.height()) + | 99 CalcBufferSize(kI420, inputImage.width(), inputImage.height()) + |
101 kI420HeaderSize; | 100 kI420HeaderSize; |
102 if (_encodedImage._size > req_length) { | 101 if (_encodedImage._size > req_length) { |
103 // Reallocate buffer. | 102 // Reallocate buffer. |
104 delete [] _encodedImage._buffer; | 103 delete[] _encodedImage._buffer; |
105 | 104 |
106 _encodedImage._buffer = new uint8_t[req_length]; | 105 _encodedImage._buffer = new uint8_t[req_length]; |
107 _encodedImage._size = req_length; | 106 _encodedImage._size = req_length; |
108 } | 107 } |
109 | 108 |
110 uint8_t *buffer = _encodedImage._buffer; | 109 uint8_t* buffer = _encodedImage._buffer; |
111 | 110 |
112 buffer = InsertHeader(buffer, width, height); | 111 buffer = InsertHeader(buffer, width, height); |
113 | 112 |
114 int ret_length = ExtractBuffer(inputImage, req_length - kI420HeaderSize, | 113 int ret_length = |
115 buffer); | 114 ExtractBuffer(inputImage, req_length - kI420HeaderSize, buffer); |
116 if (ret_length < 0) | 115 if (ret_length < 0) |
117 return WEBRTC_VIDEO_CODEC_MEMORY; | 116 return WEBRTC_VIDEO_CODEC_MEMORY; |
118 _encodedImage._length = ret_length + kI420HeaderSize; | 117 _encodedImage._length = ret_length + kI420HeaderSize; |
119 | 118 |
120 _encodedCompleteCallback->Encoded(_encodedImage, NULL, NULL); | 119 _encodedCompleteCallback->Encoded(_encodedImage, NULL, NULL); |
121 return WEBRTC_VIDEO_CODEC_OK; | 120 return WEBRTC_VIDEO_CODEC_OK; |
122 } | 121 } |
123 | 122 |
124 uint8_t* I420Encoder::InsertHeader(uint8_t *buffer, uint16_t width, | 123 uint8_t* I420Encoder::InsertHeader(uint8_t* buffer, |
| 124 uint16_t width, |
125 uint16_t height) { | 125 uint16_t height) { |
126 *buffer++ = static_cast<uint8_t>(width >> 8); | 126 *buffer++ = static_cast<uint8_t>(width >> 8); |
127 *buffer++ = static_cast<uint8_t>(width & 0xFF); | 127 *buffer++ = static_cast<uint8_t>(width & 0xFF); |
128 *buffer++ = static_cast<uint8_t>(height >> 8); | 128 *buffer++ = static_cast<uint8_t>(height >> 8); |
129 *buffer++ = static_cast<uint8_t>(height & 0xFF); | 129 *buffer++ = static_cast<uint8_t>(height & 0xFF); |
130 return buffer; | 130 return buffer; |
131 } | 131 } |
132 | 132 |
133 int | 133 int I420Encoder::RegisterEncodeCompleteCallback( |
134 I420Encoder::RegisterEncodeCompleteCallback(EncodedImageCallback* callback) { | 134 EncodedImageCallback* callback) { |
135 _encodedCompleteCallback = callback; | 135 _encodedCompleteCallback = callback; |
136 return WEBRTC_VIDEO_CODEC_OK; | 136 return WEBRTC_VIDEO_CODEC_OK; |
137 } | 137 } |
138 | 138 |
139 | 139 I420Decoder::I420Decoder() |
140 I420Decoder::I420Decoder() : _decodedImage(), _width(0), _height(0), | 140 : _decodedImage(), |
141 _inited(false), _decodeCompleteCallback(NULL) { | 141 _width(0), |
142 } | 142 _height(0), |
| 143 _inited(false), |
| 144 _decodeCompleteCallback(NULL) {} |
143 | 145 |
144 I420Decoder::~I420Decoder() { | 146 I420Decoder::~I420Decoder() { |
145 Release(); | 147 Release(); |
146 } | 148 } |
147 | 149 |
148 int | 150 int I420Decoder::Reset() { |
149 I420Decoder::Reset() { | |
150 return WEBRTC_VIDEO_CODEC_OK; | 151 return WEBRTC_VIDEO_CODEC_OK; |
151 } | 152 } |
152 | 153 |
153 | 154 int I420Decoder::InitDecode(const VideoCodec* codecSettings, |
154 int | 155 int /*numberOfCores */) { |
155 I420Decoder::InitDecode(const VideoCodec* codecSettings, | |
156 int /*numberOfCores */) { | |
157 if (codecSettings == NULL) { | 156 if (codecSettings == NULL) { |
158 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 157 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
159 } else if (codecSettings->width < 1 || codecSettings->height < 1) { | 158 } else if (codecSettings->width < 1 || codecSettings->height < 1) { |
160 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 159 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
161 } | 160 } |
162 _width = codecSettings->width; | 161 _width = codecSettings->width; |
163 _height = codecSettings->height; | 162 _height = codecSettings->height; |
164 _inited = true; | 163 _inited = true; |
165 return WEBRTC_VIDEO_CODEC_OK; | 164 return WEBRTC_VIDEO_CODEC_OK; |
166 } | 165 } |
167 | 166 |
168 int I420Decoder::Decode(const EncodedImage& inputImage, bool /*missingFrames*/, | 167 int I420Decoder::Decode(const EncodedImage& inputImage, |
| 168 bool /*missingFrames*/, |
169 const RTPFragmentationHeader* /*fragmentation*/, | 169 const RTPFragmentationHeader* /*fragmentation*/, |
170 const CodecSpecificInfo* /*codecSpecificInfo*/, | 170 const CodecSpecificInfo* /*codecSpecificInfo*/, |
171 int64_t /*renderTimeMs*/) { | 171 int64_t /*renderTimeMs*/) { |
172 if (inputImage._buffer == NULL) { | 172 if (inputImage._buffer == NULL) { |
173 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 173 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
174 } | 174 } |
175 if (_decodeCompleteCallback == NULL) { | 175 if (_decodeCompleteCallback == NULL) { |
176 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 176 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
177 } | 177 } |
178 if (inputImage._length <= 0) { | 178 if (inputImage._length <= 0) { |
(...skipping 17 matching lines...) Expand all Loading... |
196 _height = height; | 196 _height = height; |
197 | 197 |
198 // Verify that the available length is sufficient: | 198 // Verify that the available length is sufficient: |
199 size_t req_length = CalcBufferSize(kI420, _width, _height) + kI420HeaderSize; | 199 size_t req_length = CalcBufferSize(kI420, _width, _height) + kI420HeaderSize; |
200 | 200 |
201 if (req_length > inputImage._length) { | 201 if (req_length > inputImage._length) { |
202 return WEBRTC_VIDEO_CODEC_ERROR; | 202 return WEBRTC_VIDEO_CODEC_ERROR; |
203 } | 203 } |
204 // Set decoded image parameters. | 204 // Set decoded image parameters. |
205 int half_width = (_width + 1) / 2; | 205 int half_width = (_width + 1) / 2; |
206 _decodedImage.CreateEmptyFrame(_width, _height, | 206 _decodedImage.CreateEmptyFrame(_width, _height, _width, half_width, |
207 _width, half_width, half_width); | 207 half_width); |
208 // Converting from buffer to plane representation. | 208 // Converting from buffer to plane representation. |
209 int ret = ConvertToI420(kI420, buffer, 0, 0, _width, _height, 0, | 209 int ret = ConvertToI420(kI420, buffer, 0, 0, _width, _height, 0, |
210 kVideoRotation_0, &_decodedImage); | 210 kVideoRotation_0, &_decodedImage); |
211 if (ret < 0) { | 211 if (ret < 0) { |
212 return WEBRTC_VIDEO_CODEC_MEMORY; | 212 return WEBRTC_VIDEO_CODEC_MEMORY; |
213 } | 213 } |
214 _decodedImage.set_timestamp(inputImage._timeStamp); | 214 _decodedImage.set_timestamp(inputImage._timeStamp); |
215 | 215 |
216 _decodeCompleteCallback->Decoded(_decodedImage); | 216 _decodeCompleteCallback->Decoded(_decodedImage); |
217 return WEBRTC_VIDEO_CODEC_OK; | 217 return WEBRTC_VIDEO_CODEC_OK; |
218 } | 218 } |
219 | 219 |
220 const uint8_t* I420Decoder::ExtractHeader(const uint8_t* buffer, | 220 const uint8_t* I420Decoder::ExtractHeader(const uint8_t* buffer, |
221 uint16_t* width, uint16_t* height) { | 221 uint16_t* width, |
| 222 uint16_t* height) { |
222 *width = static_cast<uint16_t>(*buffer++) << 8; | 223 *width = static_cast<uint16_t>(*buffer++) << 8; |
223 *width |= *buffer++; | 224 *width |= *buffer++; |
224 *height = static_cast<uint16_t>(*buffer++) << 8; | 225 *height = static_cast<uint16_t>(*buffer++) << 8; |
225 *height |= *buffer++; | 226 *height |= *buffer++; |
226 | 227 |
227 return buffer; | 228 return buffer; |
228 } | 229 } |
229 | 230 |
230 int I420Decoder::RegisterDecodeCompleteCallback( | 231 int I420Decoder::RegisterDecodeCompleteCallback( |
231 DecodedImageCallback* callback) { | 232 DecodedImageCallback* callback) { |
232 _decodeCompleteCallback = callback; | 233 _decodeCompleteCallback = callback; |
233 return WEBRTC_VIDEO_CODEC_OK; | 234 return WEBRTC_VIDEO_CODEC_OK; |
234 } | 235 } |
235 | 236 |
236 int I420Decoder::Release() { | 237 int I420Decoder::Release() { |
237 _inited = false; | 238 _inited = false; |
238 return WEBRTC_VIDEO_CODEC_OK; | 239 return WEBRTC_VIDEO_CODEC_OK; |
239 } | 240 } |
240 } // namespace webrtc | 241 } // namespace webrtc |
OLD | NEW |