OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2011 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 */ | |
10 | |
11 #include "webrtc/media/base/videoframe.h" | |
12 | |
13 #include <string.h> | |
14 | |
15 #include "webrtc/base/arraysize.h" | |
16 #include "webrtc/base/checks.h" | |
17 #include "webrtc/base/logging.h" | |
18 #include "webrtc/media/base/videocommon.h" | |
19 | |
20 namespace cricket { | |
21 | |
22 static const size_t kMaxSampleSize = 1000000000u; | |
23 // Returns whether a sample is valid. | |
24 bool VideoFrame::Validate(uint32_t fourcc, | |
25 int w, | |
26 int h, | |
27 const uint8_t* sample, | |
28 size_t sample_size) { | |
29 if (h < 0) { | |
30 h = -h; | |
31 } | |
32 // 16384 is maximum resolution for VP8 codec. | |
33 if (w < 1 || w > 16384 || h < 1 || h > 16384) { | |
34 LOG(LS_ERROR) << "Invalid dimensions: " << w << "x" << h; | |
35 return false; | |
36 } | |
37 uint32_t format = CanonicalFourCC(fourcc); | |
38 int expected_bpp = 8; | |
39 switch (format) { | |
40 case FOURCC_I400: | |
41 case FOURCC_RGGB: | |
42 case FOURCC_BGGR: | |
43 case FOURCC_GRBG: | |
44 case FOURCC_GBRG: | |
45 expected_bpp = 8; | |
46 break; | |
47 case FOURCC_I420: | |
48 case FOURCC_I411: | |
49 case FOURCC_YU12: | |
50 case FOURCC_YV12: | |
51 case FOURCC_M420: | |
52 case FOURCC_NV21: | |
53 case FOURCC_NV12: | |
54 expected_bpp = 12; | |
55 break; | |
56 case FOURCC_I422: | |
57 case FOURCC_YV16: | |
58 case FOURCC_YUY2: | |
59 case FOURCC_UYVY: | |
60 case FOURCC_RGBP: | |
61 case FOURCC_RGBO: | |
62 case FOURCC_R444: | |
63 expected_bpp = 16; | |
64 break; | |
65 case FOURCC_I444: | |
66 case FOURCC_YV24: | |
67 case FOURCC_24BG: | |
68 case FOURCC_RAW: | |
69 expected_bpp = 24; | |
70 break; | |
71 | |
72 case FOURCC_ABGR: | |
73 case FOURCC_BGRA: | |
74 case FOURCC_ARGB: | |
75 expected_bpp = 32; | |
76 break; | |
77 | |
78 case FOURCC_MJPG: | |
79 case FOURCC_H264: | |
80 expected_bpp = 0; | |
81 break; | |
82 default: | |
83 expected_bpp = 8; // Expect format is at least 8 bits per pixel. | |
84 break; | |
85 } | |
86 size_t expected_size = (w * expected_bpp + 7) / 8 * h; | |
87 // For compressed formats, expect 4 bits per 16 x 16 macro. I420 would be | |
88 // 6 bits, but grey can be 4 bits. | |
89 if (expected_bpp == 0) { | |
90 expected_size = ((w + 15) / 16) * ((h + 15) / 16) * 4 / 8; | |
91 } | |
92 if (sample == NULL) { | |
93 LOG(LS_ERROR) << "NULL sample pointer." | |
94 << " format: " << GetFourccName(format) | |
95 << " bpp: " << expected_bpp | |
96 << " size: " << w << "x" << h | |
97 << " expected: " << expected_size | |
98 << " " << sample_size; | |
99 return false; | |
100 } | |
101 // TODO(fbarchard): Make function to dump information about frames. | |
102 uint8_t four_samples[4] = {0, 0, 0, 0}; | |
103 for (size_t i = 0; i < arraysize(four_samples) && i < sample_size; ++i) { | |
104 four_samples[i] = sample[i]; | |
105 } | |
106 if (sample_size < expected_size) { | |
107 LOG(LS_ERROR) << "Size field is too small." | |
108 << " format: " << GetFourccName(format) | |
109 << " bpp: " << expected_bpp | |
110 << " size: " << w << "x" << h | |
111 << " " << sample_size | |
112 << " expected: " << expected_size | |
113 << " sample[0..3]: " << static_cast<int>(four_samples[0]) | |
114 << ", " << static_cast<int>(four_samples[1]) | |
115 << ", " << static_cast<int>(four_samples[2]) | |
116 << ", " << static_cast<int>(four_samples[3]); | |
117 return false; | |
118 } | |
119 if (sample_size > kMaxSampleSize) { | |
120 LOG(LS_WARNING) << "Size field is invalid." | |
121 << " format: " << GetFourccName(format) | |
122 << " bpp: " << expected_bpp | |
123 << " size: " << w << "x" << h | |
124 << " " << sample_size | |
125 << " expected: " << 2 * expected_size | |
126 << " sample[0..3]: " << static_cast<int>(four_samples[0]) | |
127 << ", " << static_cast<int>(four_samples[1]) | |
128 << ", " << static_cast<int>(four_samples[2]) | |
129 << ", " << static_cast<int>(four_samples[3]); | |
130 return false; | |
131 } | |
132 // Show large size warning once every 100 frames. | |
133 // TODO(fbarchard): Make frame counter atomic for thread safety. | |
134 static int large_warn100 = 0; | |
135 size_t large_expected_size = expected_size * 2; | |
136 if (expected_bpp >= 8 && | |
137 (sample_size > large_expected_size || sample_size > kMaxSampleSize) && | |
138 large_warn100 % 100 == 0) { | |
139 ++large_warn100; | |
140 LOG(LS_WARNING) << "Size field is too large." | |
141 << " format: " << GetFourccName(format) | |
142 << " bpp: " << expected_bpp | |
143 << " size: " << w << "x" << h | |
144 << " bytes: " << sample_size | |
145 << " expected: " << large_expected_size | |
146 << " sample[0..3]: " << static_cast<int>(four_samples[0]) | |
147 << ", " << static_cast<int>(four_samples[1]) | |
148 << ", " << static_cast<int>(four_samples[2]) | |
149 << ", " << static_cast<int>(four_samples[3]); | |
150 } | |
151 | |
152 // TODO(fbarchard): Add duplicate pixel check. | |
153 // TODO(fbarchard): Use frame counter atomic for thread safety. | |
154 static bool valid_once = true; | |
155 if (valid_once) { | |
156 valid_once = false; | |
157 LOG(LS_INFO) << "Validate frame passed." | |
158 << " format: " << GetFourccName(format) | |
159 << " bpp: " << expected_bpp | |
160 << " size: " << w << "x" << h | |
161 << " bytes: " << sample_size | |
162 << " expected: " << expected_size | |
163 << " sample[0..3]: " << static_cast<int>(four_samples[0]) | |
164 << ", " << static_cast<int>(four_samples[1]) | |
165 << ", " << static_cast<int>(four_samples[2]) | |
166 << ", " << static_cast<int>(four_samples[3]); | |
167 } | |
168 return true; | |
169 } | |
170 | |
171 } // namespace cricket | |
OLD | NEW |