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" |
(...skipping 17 matching lines...) Expand all Loading... | |
28 if (br->buf_ < br->buf_end_) { | 28 if (br->buf_ < br->buf_end_) { |
29 br->bits_ += 8; | 29 br->bits_ += 8; |
30 br->value_ = static_cast<uint32_t>(*br->buf_++) | (br->value_ << 8); | 30 br->value_ = static_cast<uint32_t>(*br->buf_++) | (br->value_ << 8); |
31 } else if (!br->eof_) { | 31 } else if (!br->eof_) { |
32 br->value_ <<= 8; | 32 br->value_ <<= 8; |
33 br->bits_ += 8; | 33 br->bits_ += 8; |
34 br->eof_ = 1; | 34 br->eof_ = 1; |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 static void VP8LoadNewBytes(VP8BitReader* const br) { | 38 static bool VP8LoadNewBytes(VP8BitReader* const br) { |
39 int BITS = 24; | 39 int BITS = 24; |
40 // Read 'BITS' bits at a time. | 40 // Read 'BITS' bits at a time. |
41 if (br->buf_ + sizeof(uint32_t) <= br->buf_end_) { | 41 if (br->buf_ + sizeof(uint32_t) <= br->buf_end_) { |
42 uint32_t bits; | 42 uint32_t bits; |
43 const uint32_t in_bits = *(const uint32_t*)(br->buf_); | 43 const uint32_t in_bits = *(const uint32_t*)(br->buf_); |
44 br->buf_ += BITS >> 3; | 44 br->buf_ += BITS >> 3; |
45 #if defined(WEBRTC_ARCH_BIG_ENDIAN) | 45 #if defined(WEBRTC_ARCH_BIG_ENDIAN) |
46 bits = static_cast<uint32_t>(in_bits); | 46 bits = static_cast<uint32_t>(in_bits); |
47 if (BITS != 8 * sizeof(uint32_t)) | 47 if (BITS != 8 * sizeof(uint32_t)) |
48 bits >>= (8 * sizeof(uint32_t) - BITS); | 48 bits >>= (8 * sizeof(uint32_t) - BITS); |
49 #else | 49 #else |
50 bits = BSwap32(in_bits); | 50 bits = BSwap32(in_bits); |
51 bits >>= 32 - BITS; | 51 bits >>= 32 - BITS; |
52 #endif | 52 #endif |
53 br->value_ = bits | (br->value_ << BITS); | 53 br->value_ = bits | (br->value_ << BITS); |
54 br->bits_ += BITS; | 54 br->bits_ += BITS; |
55 } else { | 55 } else { |
56 VP8LoadFinalBytes(br); | 56 VP8LoadFinalBytes(br); |
57 } | 57 } |
58 if (br->bits_ < 0) { | |
59 br->eof_ = 1; | |
pbos-webrtc
2016/04/19 23:11:50
Actually, does this happen from VP8LoadFinalBytes?
jackychen_
2016/04/20 00:27:18
I'm not sure. So to be safe, I put the check here.
| |
60 return false; | |
61 } | |
62 return true; | |
58 } | 63 } |
59 | 64 |
60 static void VP8InitBitReader(VP8BitReader* const br, | 65 static void VP8InitBitReader(VP8BitReader* const br, |
61 const uint8_t* const start, | 66 const uint8_t* const start, |
62 const uint8_t* const end) { | 67 const uint8_t* const end) { |
63 br->range_ = 255 - 1; | 68 br->range_ = 255 - 1; |
64 br->buf_ = start; | 69 br->buf_ = start; |
65 br->buf_end_ = end; | 70 br->buf_end_ = end; |
66 br->value_ = 0; | 71 br->value_ = 0; |
67 br->bits_ = -8; // To load the very first 8bits. | 72 br->bits_ = -8; // To load the very first 8bits. |
68 br->eof_ = 0; | 73 br->eof_ = 0; |
69 VP8LoadNewBytes(br); | 74 VP8LoadNewBytes(br); |
70 } | 75 } |
71 | 76 |
72 // Read a bit with proba 'prob'. | 77 // Read a bit with proba 'prob'. |
73 static int VP8GetBit(VP8BitReader* const br, int prob) { | 78 static int VP8GetBit(VP8BitReader* const br, int prob) { |
74 uint8_t range = br->range_; | 79 uint8_t range = br->range_; |
75 if (br->bits_ < 0) { | 80 if (br->bits_ < 0) { |
76 VP8LoadNewBytes(br); | 81 if (!VP8LoadNewBytes(br)) |
82 return 0; | |
77 } | 83 } |
78 | |
79 const int pos = br->bits_; | 84 const int pos = br->bits_; |
80 const uint8_t split = (range * prob) >> 8; | 85 const uint8_t split = (range * prob) >> 8; |
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; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 LOG(LS_WARNING) << "Failed to get QP, end of file reached."; | 200 LOG(LS_WARNING) << "Failed to get QP, end of file reached."; |
196 return false; | 201 return false; |
197 } | 202 } |
198 *qp = base_q0; | 203 *qp = base_q0; |
199 return true; | 204 return true; |
200 } | 205 } |
201 | 206 |
202 } // namespace vp8 | 207 } // namespace vp8 |
203 | 208 |
204 } // namespace webrtc | 209 } // namespace webrtc |
OLD | NEW |