blob: f5b0b6de52cd24489f298669ecc0b5831c9cce69 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2011 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/media/base/videoframe.h"
29
30#include <cstring>
31
32#if !defined(DISABLE_YUV)
33#include "libyuv/compare.h"
34#include "libyuv/planar_functions.h"
35#include "libyuv/scale.h"
36#endif
37
38#include "talk/base/logging.h"
39#include "talk/media/base/videocommon.h"
40
41namespace cricket {
42
43// Round to 2 pixels because Chroma channels are half size.
44#define ROUNDTO2(v) (v & ~1)
45
46talk_base::StreamResult VideoFrame::Write(talk_base::StreamInterface* stream,
47 int* error) {
48 talk_base::StreamResult result = talk_base::SR_SUCCESS;
49 const uint8* src_y = GetYPlane();
50 const uint8* src_u = GetUPlane();
51 const uint8* src_v = GetVPlane();
52 if (!src_y || !src_u || !src_v) {
53 return result; // Nothing to write.
54 }
55 const int32 y_pitch = GetYPitch();
56 const int32 u_pitch = GetUPitch();
57 const int32 v_pitch = GetVPitch();
58 const size_t width = GetWidth();
59 const size_t height = GetHeight();
60 const size_t half_width = (width + 1) >> 1;
61 const size_t half_height = (height + 1) >> 1;
62 // Write Y.
63 for (size_t row = 0; row < height; ++row) {
64 result = stream->Write(src_y + row * y_pitch, width, NULL, error);
65 if (result != talk_base::SR_SUCCESS) {
66 return result;
67 }
68 }
69 // Write U.
70 for (size_t row = 0; row < half_height; ++row) {
71 result = stream->Write(src_u + row * u_pitch, half_width, NULL, error);
72 if (result != talk_base::SR_SUCCESS) {
73 return result;
74 }
75 }
76 // Write V.
77 for (size_t row = 0; row < half_height; ++row) {
78 result = stream->Write(src_v + row * v_pitch, half_width, NULL, error);
79 if (result != talk_base::SR_SUCCESS) {
80 return result;
81 }
82 }
83 return result;
84}
85
86bool VideoFrame::CopyToPlanes(
87 uint8* dst_y, uint8* dst_u, uint8* dst_v,
88 int32 dst_pitch_y, int32 dst_pitch_u, int32 dst_pitch_v) const {
89#if !defined(DISABLE_YUV)
90 int32 src_width = GetWidth();
91 int32 src_height = GetHeight();
92 return libyuv::I420Copy(GetYPlane(), GetYPitch(),
93 GetUPlane(), GetUPitch(),
94 GetVPlane(), GetVPitch(),
95 dst_y, dst_pitch_y,
96 dst_u, dst_pitch_u,
97 dst_v, dst_pitch_v,
98 src_width, src_height) == 0;
99#else
100 int uv_size = GetUPitch() * GetChromaHeight();
101 memcpy(dst_y, GetYPlane(), GetWidth() * GetHeight());
102 memcpy(dst_u, GetUPlane(), uv_size);
103 memcpy(dst_v, GetVPlane(), uv_size);
104 return true;
105#endif
106}
107
108void VideoFrame::CopyToFrame(VideoFrame* dst) const {
109 if (!dst) {
110 LOG(LS_ERROR) << "NULL dst pointer.";
111 return;
112 }
113
114 CopyToPlanes(dst->GetYPlane(), dst->GetUPlane(), dst->GetVPlane(),
115 dst->GetYPitch(), dst->GetUPitch(), dst->GetVPitch());
116}
117
118// TODO(fbarchard): Handle odd width/height with rounding.
119void VideoFrame::StretchToPlanes(
120 uint8* dst_y, uint8* dst_u, uint8* dst_v,
121 int32 dst_pitch_y, int32 dst_pitch_u, int32 dst_pitch_v,
122 size_t width, size_t height, bool interpolate, bool vert_crop) const {
123 if (!GetYPlane() || !GetUPlane() || !GetVPlane()) {
124 LOG(LS_ERROR) << "NULL plane pointer.";
125 return;
126 }
127
128 size_t src_width = GetWidth();
129 size_t src_height = GetHeight();
130 if (width == src_width && height == src_height) {
131 CopyToPlanes(dst_y, dst_u, dst_v, dst_pitch_y, dst_pitch_u, dst_pitch_v);
132 return;
133 }
134 const uint8* src_y = GetYPlane();
135 const uint8* src_u = GetUPlane();
136 const uint8* src_v = GetVPlane();
137
138 if (vert_crop) {
139 // Adjust the input width:height ratio to be the same as the output ratio.
140 if (src_width * height > src_height * width) {
141 // Reduce the input width, but keep size/position aligned for YuvScaler
142 src_width = ROUNDTO2(src_height * width / height);
143 int32 iwidth_offset = ROUNDTO2((GetWidth() - src_width) / 2);
144 src_y += iwidth_offset;
145 src_u += iwidth_offset / 2;
146 src_v += iwidth_offset / 2;
147 } else if (src_width * height < src_height * width) {
148 // Reduce the input height.
149 src_height = src_width * height / width;
150 int32 iheight_offset = (GetHeight() - src_height) >> 2;
151 iheight_offset <<= 1; // Ensure that iheight_offset is even.
152 src_y += iheight_offset * GetYPitch();
153 src_u += iheight_offset / 2 * GetUPitch();
154 src_v += iheight_offset / 2 * GetVPitch();
155 }
156 }
157
158 // TODO(fbarchard): Implement a simple scale for non-libyuv.
159#if !defined(DISABLE_YUV)
160 // Scale to the output I420 frame.
161 libyuv::Scale(src_y, src_u, src_v,
162 GetYPitch(), GetUPitch(), GetVPitch(),
163 src_width, src_height,
164 dst_y, dst_u, dst_v, dst_pitch_y, dst_pitch_u, dst_pitch_v,
165 width, height, interpolate);
166#endif
167}
168
169size_t VideoFrame::StretchToBuffer(size_t dst_width, size_t dst_height,
170 uint8* dst_buffer, size_t size,
171 bool interpolate, bool vert_crop) const {
172 if (!dst_buffer) {
173 LOG(LS_ERROR) << "NULL dst_buffer pointer.";
174 return 0;
175 }
176
177 size_t needed = SizeOf(dst_width, dst_height);
178 if (needed <= size) {
179 uint8* dst_y = dst_buffer;
180 uint8* dst_u = dst_y + dst_width * dst_height;
181 uint8* dst_v = dst_u + ((dst_width + 1) >> 1) * ((dst_height + 1) >> 1);
182 StretchToPlanes(dst_y, dst_u, dst_v,
183 dst_width, (dst_width + 1) >> 1, (dst_width + 1) >> 1,
184 dst_width, dst_height, interpolate, vert_crop);
185 }
186 return needed;
187}
188
189void VideoFrame::StretchToFrame(VideoFrame* dst,
190 bool interpolate, bool vert_crop) const {
191 if (!dst) {
192 LOG(LS_ERROR) << "NULL dst pointer.";
193 return;
194 }
195
196 StretchToPlanes(dst->GetYPlane(), dst->GetUPlane(), dst->GetVPlane(),
197 dst->GetYPitch(), dst->GetUPitch(), dst->GetVPitch(),
198 dst->GetWidth(), dst->GetHeight(),
199 interpolate, vert_crop);
200 dst->SetElapsedTime(GetElapsedTime());
201 dst->SetTimeStamp(GetTimeStamp());
202}
203
204VideoFrame* VideoFrame::Stretch(size_t dst_width, size_t dst_height,
205 bool interpolate, bool vert_crop) const {
206 VideoFrame* dest = CreateEmptyFrame(dst_width, dst_height,
207 GetPixelWidth(), GetPixelHeight(),
208 GetElapsedTime(), GetTimeStamp());
209 if (dest) {
210 StretchToFrame(dest, interpolate, vert_crop);
211 }
212 return dest;
213}
214
215bool VideoFrame::SetToBlack() {
216#if !defined(DISABLE_YUV)
217 return libyuv::I420Rect(GetYPlane(), GetYPitch(),
218 GetUPlane(), GetUPitch(),
219 GetVPlane(), GetVPitch(),
220 0, 0, GetWidth(), GetHeight(),
221 16, 128, 128) == 0;
222#else
223 int uv_size = GetUPitch() * GetChromaHeight();
224 memset(GetYPlane(), 16, GetWidth() * GetHeight());
225 memset(GetUPlane(), 128, uv_size);
226 memset(GetVPlane(), 128, uv_size);
227 return true;
228#endif
229}
230
231static const size_t kMaxSampleSize = 1000000000u;
232// Returns whether a sample is valid
233bool VideoFrame::Validate(uint32 fourcc, int w, int h,
234 const uint8 *sample, size_t sample_size) {
235 if (h < 0) {
236 h = -h;
237 }
238 // 16384 is maximum resolution for VP8 codec.
239 if (w < 1 || w > 16384 || h < 1 || h > 16384) {
240 LOG(LS_ERROR) << "Invalid dimensions: " << w << "x" << h;
241 return false;
242 }
243 uint32 format = CanonicalFourCC(fourcc);
244 int expected_bpp = 8;
245 switch (format) {
246 case FOURCC_I400:
247 case FOURCC_RGGB:
248 case FOURCC_BGGR:
249 case FOURCC_GRBG:
250 case FOURCC_GBRG:
251 expected_bpp = 8;
252 break;
253 case FOURCC_I420:
254 case FOURCC_I411:
255 case FOURCC_YU12:
256 case FOURCC_YV12:
257 case FOURCC_M420:
258 case FOURCC_Q420:
259 case FOURCC_NV21:
260 case FOURCC_NV12:
261 expected_bpp = 12;
262 break;
263 case FOURCC_I422:
264 case FOURCC_YV16:
265 case FOURCC_YUY2:
266 case FOURCC_UYVY:
267 case FOURCC_RGBP:
268 case FOURCC_RGBO:
269 case FOURCC_R444:
270 expected_bpp = 16;
271 break;
272 case FOURCC_I444:
273 case FOURCC_YV24:
274 case FOURCC_24BG:
275 case FOURCC_RAW:
276 expected_bpp = 24;
277 break;
278
279 case FOURCC_ABGR:
280 case FOURCC_BGRA:
281 case FOURCC_ARGB:
282 expected_bpp = 32;
283 break;
284
285 case FOURCC_MJPG:
286 case FOURCC_H264:
287 expected_bpp = 0;
288 break;
289 default:
290 expected_bpp = 8; // Expect format is at least 8 bits per pixel.
291 break;
292 }
293 size_t expected_size = (w * expected_bpp + 7) / 8 * h;
294 // For compressed formats, expect 4 bits per 16 x 16 macro. I420 would be
295 // 6 bits, but grey can be 4 bits.
296 if (expected_bpp == 0) {
297 expected_size = ((w + 15) / 16) * ((h + 15) / 16) * 4 / 8;
298 }
299 if (sample == NULL) {
300 LOG(LS_ERROR) << "NULL sample pointer."
301 << " format: " << GetFourccName(format)
302 << " bpp: " << expected_bpp
303 << " size: " << w << "x" << h
304 << " expected: " << expected_size
305 << " " << sample_size;
306 return false;
307 }
308 if (sample_size < expected_size) {
309 LOG(LS_ERROR) << "Size field is too small."
310 << " format: " << GetFourccName(format)
311 << " bpp: " << expected_bpp
312 << " size: " << w << "x" << h
313 << " " << sample_size
314 << " expected: " << expected_size
315 << " sample[0..3]: " << static_cast<int>(sample[0])
316 << ", " << static_cast<int>(sample[1])
317 << ", " << static_cast<int>(sample[2])
318 << ", " << static_cast<int>(sample[3]);
319 return false;
320 }
321 if (sample_size > kMaxSampleSize) {
322 LOG(LS_WARNING) << "Size field is invalid."
323 << " format: " << GetFourccName(format)
324 << " bpp: " << expected_bpp
325 << " size: " << w << "x" << h
326 << " " << sample_size
327 << " expected: " << 2 * expected_size
328 << " sample[0..3]: " << static_cast<int>(sample[0])
329 << ", " << static_cast<int>(sample[1])
330 << ", " << static_cast<int>(sample[2])
331 << ", " << static_cast<int>(sample[3]);
332 return false;
333 }
334 // Show large size warning once every 100 frames.
335 static int large_warn100 = 0;
336 size_t large_expected_size = expected_size * 2;
337 if (expected_bpp >= 8 &&
338 (sample_size > large_expected_size || sample_size > kMaxSampleSize) &&
339 large_warn100 % 100 == 0) {
340 ++large_warn100;
341 LOG(LS_WARNING) << "Size field is too large."
342 << " format: " << GetFourccName(format)
343 << " bpp: " << expected_bpp
344 << " size: " << w << "x" << h
345 << " bytes: " << sample_size
346 << " expected: " << large_expected_size
347 << " sample[0..3]: " << static_cast<int>(sample[0])
348 << ", " << static_cast<int>(sample[1])
349 << ", " << static_cast<int>(sample[2])
350 << ", " << static_cast<int>(sample[3]);
351 }
352 // Scan pages to ensure they are there and don't contain a single value and
353 // to generate an error.
354 if (!memcmp(sample + sample_size - 8, sample + sample_size - 4, 4) &&
355 !memcmp(sample, sample + 4, sample_size - 4)) {
356 LOG(LS_WARNING) << "Duplicate value for all pixels."
357 << " format: " << GetFourccName(format)
358 << " bpp: " << expected_bpp
359 << " size: " << w << "x" << h
360 << " bytes: " << sample_size
361 << " expected: " << expected_size
362 << " sample[0..3]: " << static_cast<int>(sample[0])
363 << ", " << static_cast<int>(sample[1])
364 << ", " << static_cast<int>(sample[2])
365 << ", " << static_cast<int>(sample[3]);
366 }
367
368 static bool valid_once = true;
369 if (valid_once) {
370 valid_once = false;
371 LOG(LS_INFO) << "Validate frame passed."
372 << " format: " << GetFourccName(format)
373 << " bpp: " << expected_bpp
374 << " size: " << w << "x" << h
375 << " bytes: " << sample_size
376 << " expected: " << expected_size
377 << " sample[0..3]: " << static_cast<int>(sample[0])
378 << ", " << static_cast<int>(sample[1])
379 << ", " << static_cast<int>(sample[2])
380 << ", " << static_cast<int>(sample[3]);
381 }
382 return true;
383}
384
385} // namespace cricket