OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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 #include "webrtc/modules/video_coding/utility/vp8_header_parser.h" | 10 #include "webrtc/modules/video_coding/utility/vp8_header_parser.h" |
11 | 11 |
12 #include "webrtc/base/logging.h" | 12 #include "webrtc/base/logging.h" |
13 | 13 |
14 namespace webrtc { | 14 namespace webrtc { |
15 | 15 |
16 namespace vp8 { | 16 namespace vp8 { |
17 namespace { | 17 namespace { |
18 const size_t kCommonPayloadHeaderLength = 3; | 18 const size_t kCommonPayloadHeaderLength = 3; |
19 const size_t kKeyPayloadHeaderLength = 10; | 19 const size_t kKeyPayloadHeaderLength = 10; |
20 const size_t kParseError = 0; | |
20 } // namespace | 21 } // namespace |
21 | 22 |
22 static uint32_t BSwap32(uint32_t x) { | 23 static uint32_t BSwap32(uint32_t x) { |
23 return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24); | 24 return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24); |
24 } | 25 } |
25 | 26 |
26 static void VP8LoadFinalBytes(VP8BitReader* const br) { | 27 static void VP8LoadFinalBytes(VP8BitReader* const br) { |
27 // Only read 8bits at a time. | 28 // Only read 8bits at a time. |
28 if (br->buf_ < br->buf_end_) { | 29 if (br->buf_ < br->buf_end_) { |
29 br->bits_ += 8; | 30 br->bits_ += 8; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 | 72 |
72 // Read a bit with proba 'prob'. | 73 // Read a bit with proba 'prob'. |
73 static int VP8GetBit(VP8BitReader* const br, int prob) { | 74 static int VP8GetBit(VP8BitReader* const br, int prob) { |
74 uint8_t range = br->range_; | 75 uint8_t range = br->range_; |
75 if (br->bits_ < 0) { | 76 if (br->bits_ < 0) { |
76 VP8LoadNewBytes(br); | 77 VP8LoadNewBytes(br); |
77 } | 78 } |
78 | 79 |
79 const int pos = br->bits_; | 80 const int pos = br->bits_; |
80 const uint8_t split = (range * prob) >> 8; | 81 const uint8_t split = (range * prob) >> 8; |
82 if (pos < 0) { | |
83 pos = 0; | |
pbos-webrtc
2016/04/15 08:58:58
Also log an error in here, but I think you should
jackychen_
2016/04/15 20:24:50
Right, it must be an invalid bitstream.
| |
84 kParseError = 1; | |
pbos-webrtc
2016/04/15 08:58:58
Does this even compile? You shouldn't modify globa
jackychen_
2016/04/15 20:24:50
Done.
| |
85 } | |
81 const uint8_t value = static_cast<uint8_t>(br->value_ >> pos); | 86 const uint8_t value = static_cast<uint8_t>(br->value_ >> pos); |
82 int bit; | 87 int bit; |
83 if (value > split) { | 88 if (value > split) { |
84 range -= split + 1; | 89 range -= split + 1; |
85 br->value_ -= static_cast<uint32_t>(split + 1) << pos; | 90 br->value_ -= static_cast<uint32_t>(split + 1) << pos; |
86 bit = 1; | 91 bit = 1; |
87 } else { | 92 } else { |
88 range = split; | 93 range = split; |
89 bit = 0; | 94 bit = 0; |
90 } | 95 } |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 if (length < kCommonPayloadHeaderLength) { | 168 if (length < kCommonPayloadHeaderLength) { |
164 LOG(LS_WARNING) << "Failed to get QP, invalid length."; | 169 LOG(LS_WARNING) << "Failed to get QP, invalid length."; |
165 return false; | 170 return false; |
166 } | 171 } |
167 VP8BitReader br; | 172 VP8BitReader br; |
168 const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16); | 173 const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16); |
169 int key_frame = !(bits & 1); | 174 int key_frame = !(bits & 1); |
170 // Size of first partition in bytes. | 175 // Size of first partition in bytes. |
171 uint32_t partition_length = (bits >> 5); | 176 uint32_t partition_length = (bits >> 5); |
172 size_t header_length = kCommonPayloadHeaderLength; | 177 size_t header_length = kCommonPayloadHeaderLength; |
178 kParseError = 0; | |
pbos-webrtc
2016/04/15 08:58:58
Should this not be a property of the bitreader?
jackychen_
2016/04/15 20:24:50
Done.
| |
173 if (key_frame) { | 179 if (key_frame) { |
174 header_length = kKeyPayloadHeaderLength; | 180 header_length = kKeyPayloadHeaderLength; |
175 } | 181 } |
176 if (header_length + partition_length > length) { | 182 if (header_length + partition_length > length) { |
177 LOG(LS_WARNING) << "Failed to get QP, invalid length: " << length; | 183 LOG(LS_WARNING) << "Failed to get QP, invalid length: " << length; |
178 return false; | 184 return false; |
179 } | 185 } |
180 buf += header_length; | 186 buf += header_length; |
181 | 187 |
182 VP8InitBitReader(&br, buf, buf + partition_length); | 188 VP8InitBitReader(&br, buf, buf + partition_length); |
183 if (key_frame) { | 189 if (key_frame) { |
184 // Color space and pixel type. | 190 // Color space and pixel type. |
185 VP8Get(&br); | 191 VP8Get(&br); |
186 VP8Get(&br); | 192 VP8Get(&br); |
187 } | 193 } |
188 ParseSegmentHeader(&br); | 194 ParseSegmentHeader(&br); |
189 ParseFilterHeader(&br); | 195 ParseFilterHeader(&br); |
190 // Number of coefficient data partitions. | 196 // Number of coefficient data partitions. |
191 VP8GetValue(&br, 2); | 197 VP8GetValue(&br, 2); |
192 // Base QP. | 198 // Base QP. |
193 const int base_q0 = VP8GetValue(&br, 7); | 199 const int base_q0 = VP8GetValue(&br, 7); |
194 if (br.eof_ == 1) { | 200 if (br.eof_ == 1) { |
195 LOG(LS_WARNING) << "Failed to get QP, end of file reached."; | 201 LOG(LS_WARNING) << "Failed to get QP, end of file reached."; |
196 return false; | 202 return false; |
197 } | 203 } |
198 *qp = base_q0; | 204 *qp = base_q0; |
205 if (kParseError) | |
206 return false; | |
199 return true; | 207 return true; |
200 } | 208 } |
201 | 209 |
202 } // namespace vp8 | 210 } // namespace vp8 |
203 | 211 |
204 } // namespace webrtc | 212 } // namespace webrtc |
OLD | NEW |