Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/base/bit_reader_h264.h" | |
| 6 | |
| 7 namespace media { | |
| 8 | |
| 9 BitReaderH264::BitReaderH264(const uint8* data, int size) | |
| 10 : data_(data), | |
| 11 bytes_left_(size), | |
| 12 prev_two_bytes_(0xffff), | |
| 13 bit_reader_core_(this) { | |
| 14 DCHECK(data_ != NULL); | |
| 15 DCHECK_GE(bytes_left_, 0); | |
| 16 } | |
| 17 | |
| 18 BitReaderH264::~BitReaderH264() {} | |
| 19 | |
| 20 int BitReaderH264::ReadUE(uint32* out) { | |
| 21 // Get the number of leading zeros. | |
| 22 int zero_count = -1; | |
| 23 bool is_one; | |
| 24 do { | |
| 25 if (!bit_reader_core_.ReadFlag(&is_one)) | |
| 26 return -1; | |
| 27 zero_count++; | |
| 28 } while(!is_one); | |
| 29 | |
| 30 int code_size = 2 * zero_count + 1; | |
| 31 | |
| 32 // If zero_count is greater than 31, the calculated value will overflow, | |
| 33 // just skip the bits corresponding to the exp-golomb value. | |
| 34 if (zero_count > 31) { | |
| 35 if (!bit_reader_core_.SkipBits(zero_count)) | |
| 36 return -1; | |
| 37 return code_size; | |
| 38 } | |
| 39 | |
| 40 // Read the actual value. | |
| 41 uint32 base = (1 << zero_count) - 1; | |
| 42 uint32 offset; | |
| 43 if (!ReadBits(zero_count, &offset)) | |
| 44 return false; | |
|
damienv1
2013/12/28 06:44:12
return -1;
| |
| 45 *out = base + offset; | |
| 46 | |
| 47 return code_size; | |
| 48 } | |
| 49 | |
| 50 bool BitReaderH264::HasMoreRBSPData() { | |
| 51 uint64 bit_reg; | |
| 52 int nbits = bit_reader_core_.PeekBitsMsbAligned(9, &bit_reg); | |
| 53 | |
| 54 // Not on last byte. | |
| 55 if (nbits > 8) | |
| 56 return true; | |
| 57 | |
| 58 // Last byte, look for stop bit; | |
| 59 // We have more RBSP data if the last non-zero bit we find is not the | |
| 60 // first available bit. | |
| 61 return (bit_reg << 1) != 0; | |
| 62 } | |
| 63 | |
| 64 int BitReaderH264::GetBytes(int max_nbytes, const uint8** out) { | |
| 65 DCHECK_LE(max_nbytes, static_cast<int>(sizeof(data_window_))); | |
| 66 DCHECK_GE(max_nbytes, 0); | |
| 67 DCHECK(out); | |
| 68 | |
| 69 const uint8* start = data_; | |
| 70 bool emulation_prevention_byte_detected = false; | |
| 71 | |
| 72 int nbytes = 0; | |
| 73 *out = data_; | |
| 74 | |
| 75 while (nbytes < max_nbytes && bytes_left_ > 0) { | |
| 76 // Emulation prevention three-byte detection. | |
| 77 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03) | |
| 78 // and starts copying data to |data_window_| which is used as the output. | |
| 79 if (*data_ == 0x3 && (prev_two_bytes_ & 0xffff) == 0) { | |
| 80 if (!emulation_prevention_byte_detected && nbytes > 0) | |
| 81 memcpy(data_window_, start, nbytes); | |
| 82 emulation_prevention_byte_detected = true; | |
| 83 *out = data_window_; | |
| 84 // Need another full three bytes before we can detect the sequence again. | |
| 85 prev_two_bytes_ = 0xffff; | |
| 86 } else { | |
| 87 if (emulation_prevention_byte_detected) | |
| 88 data_window_[nbytes] = *data_; | |
| 89 prev_two_bytes_ = (prev_two_bytes_ << 8) | *data_; | |
| 90 ++nbytes; | |
| 91 } | |
| 92 | |
| 93 ++data_; | |
| 94 --bytes_left_; | |
| 95 } | |
| 96 | |
| 97 return nbytes; | |
| 98 } | |
| 99 | |
| 100 int BitReaderH264::GetBytesLeft() const { | |
| 101 // Getting the actual number of bytes left would be really inefficient | |
| 102 // as this would mean parsing the rest of data | |
| 103 // for possible start code emulation prevention bytes. | |
| 104 // So this function should rather not be used in the framework of H264. | |
| 105 NOTREACHED(); | |
| 106 return 0; | |
| 107 } | |
| 108 | |
| 109 } // namespace media | |
| OLD | NEW |