| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/gpu/media/h264_parser.h" | 5 #include "content/common/gpu/media/h264_parser.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 | 10 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 } | 43 } |
| 44 | 44 |
| 45 H264SliceHeader::H264SliceHeader() { | 45 H264SliceHeader::H264SliceHeader() { |
| 46 memset(this, 0, sizeof(*this)); | 46 memset(this, 0, sizeof(*this)); |
| 47 } | 47 } |
| 48 | 48 |
| 49 H264SEIMessage::H264SEIMessage() { | 49 H264SEIMessage::H264SEIMessage() { |
| 50 memset(this, 0, sizeof(*this)); | 50 memset(this, 0, sizeof(*this)); |
| 51 } | 51 } |
| 52 | 52 |
| 53 H264Parser::H264BitReader::H264BitReader() | |
| 54 : data_(NULL), | |
| 55 bytes_left_(0), | |
| 56 curr_byte_(0), | |
| 57 num_remaining_bits_in_curr_byte_(0), | |
| 58 prev_two_bytes_(0) { | |
| 59 } | |
| 60 | |
| 61 H264Parser::H264BitReader::~H264BitReader() {} | |
| 62 | |
| 63 bool H264Parser::H264BitReader::Initialize(const uint8* data, off_t size) { | |
| 64 DCHECK(data); | |
| 65 | |
| 66 if (size < 1) | |
| 67 return false; | |
| 68 | |
| 69 data_ = data; | |
| 70 bytes_left_ = size; | |
| 71 num_remaining_bits_in_curr_byte_ = 0; | |
| 72 // Initially set to 0xffff to accept all initial two-byte sequences. | |
| 73 prev_two_bytes_ = 0xffff; | |
| 74 | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 bool H264Parser::H264BitReader::UpdateCurrByte() { | |
| 79 if (bytes_left_ < 1) | |
| 80 return false; | |
| 81 | |
| 82 // Emulation prevention three-byte detection. | |
| 83 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03). | |
| 84 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) { | |
| 85 // Detected 0x000003, skip last byte. | |
| 86 ++data_; | |
| 87 --bytes_left_; | |
| 88 // Need another full three bytes before we can detect the sequence again. | |
| 89 prev_two_bytes_ = 0xffff; | |
| 90 | |
| 91 if (bytes_left_ < 1) | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 // Load a new byte and advance pointers. | |
| 96 curr_byte_ = *data_++ & 0xff; | |
| 97 --bytes_left_; | |
| 98 num_remaining_bits_in_curr_byte_ = 8; | |
| 99 | |
| 100 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_; | |
| 101 | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 // Read |num_bits| (1 to 31 inclusive) from the stream and return them | |
| 106 // in |out|, with first bit in the stream as MSB in |out| at position | |
| 107 // (|num_bits| - 1). | |
| 108 bool H264Parser::H264BitReader::ReadBits(int num_bits, int *out) { | |
| 109 int bits_left = num_bits; | |
| 110 *out = 0; | |
| 111 DCHECK(num_bits <= 31); | |
| 112 | |
| 113 while (num_remaining_bits_in_curr_byte_ < bits_left) { | |
| 114 // Take all that's left in current byte, shift to make space for the rest. | |
| 115 *out = (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_)); | |
| 116 bits_left -= num_remaining_bits_in_curr_byte_; | |
| 117 | |
| 118 if (!UpdateCurrByte()) | |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left)); | |
| 123 *out &= ((1 << num_bits) - 1); | |
| 124 num_remaining_bits_in_curr_byte_ -= bits_left; | |
| 125 | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 129 off_t H264Parser::H264BitReader::NumBitsLeft() { | |
| 130 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8); | |
| 131 } | |
| 132 | |
| 133 bool H264Parser::H264BitReader::HasMoreRBSPData() { | |
| 134 // Make sure we have more bits, if we are at 0 bits in current byte | |
| 135 // and updating current byte fails, we don't have more data anyway. | |
| 136 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte()) | |
| 137 return false; | |
| 138 | |
| 139 // On last byte? | |
| 140 if (bytes_left_) | |
| 141 return true; | |
| 142 | |
| 143 // Last byte, look for stop bit; | |
| 144 // We have more RBSP data if the last non-zero bit we find is not the | |
| 145 // first available bit. | |
| 146 return (curr_byte_ & | |
| 147 ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0; | |
| 148 } | |
| 149 | |
| 150 #define READ_BITS_OR_RETURN(num_bits, out) \ | 53 #define READ_BITS_OR_RETURN(num_bits, out) \ |
| 151 do { \ | 54 do { \ |
| 152 int _out; \ | 55 int _out; \ |
| 153 if (!br_.ReadBits(num_bits, &_out)) { \ | 56 if (!br_.ReadBits(num_bits, &_out)) { \ |
| 154 DVLOG(1) << "Error in stream: unexpected EOS while trying to read " #out; \ | 57 DVLOG(1) << "Error in stream: unexpected EOS while trying to read " #out; \ |
| 155 return kInvalidStream; \ | 58 return kInvalidStream; \ |
| 156 } \ | 59 } \ |
| 157 *out = _out; \ | 60 *out = _out; \ |
| 158 } while (0) | 61 } while (0) |
| 159 | 62 |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 | 254 |
| 352 if (!LocateNALU(stream_, bytes_left_, &off_to_nalu_start, &nalu->size)) { | 255 if (!LocateNALU(stream_, bytes_left_, &off_to_nalu_start, &nalu->size)) { |
| 353 DVLOG(4) << "Could not find next NALU, bytes left in stream: " | 256 DVLOG(4) << "Could not find next NALU, bytes left in stream: " |
| 354 << bytes_left_; | 257 << bytes_left_; |
| 355 return kEOStream; | 258 return kEOStream; |
| 356 } | 259 } |
| 357 | 260 |
| 358 nalu->data = stream_ + off_to_nalu_start; | 261 nalu->data = stream_ + off_to_nalu_start; |
| 359 | 262 |
| 360 // Initialize bit reader at the start of found NALU. | 263 // Initialize bit reader at the start of found NALU. |
| 361 if (!br_.Initialize(nalu->data, nalu->size)) | 264 br_.Initialize(nalu->data, nalu->size); |
| 265 |
| 266 if (!br_.HasMoreData()) |
| 362 return kEOStream; | 267 return kEOStream; |
| 363 | 268 |
| 364 DVLOG(4) << "Looking for NALU, Stream bytes left: " << bytes_left_ | 269 DVLOG(4) << "Looking for NALU, Stream bytes left: " << bytes_left_ |
| 365 << " off to next nalu: " << off_to_nalu_start; | 270 << " off to next nalu: " << off_to_nalu_start; |
| 366 | 271 |
| 367 // Move parser state to after this NALU, so next time AdvanceToNextNALU | 272 // Move parser state to after this NALU, so next time AdvanceToNextNALU |
| 368 // is called, we will effectively be skipping it; | 273 // is called, we will effectively be skipping it; |
| 369 // other parsing functions will use the position saved | 274 // other parsing functions will use the position saved |
| 370 // in bit reader for parsing, so we don't have to remember it here. | 275 // in bit reader for parsing, so we don't have to remember it here. |
| 371 stream_ += off_to_nalu_start + nalu->size; | 276 stream_ += off_to_nalu_start + nalu->size; |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 805 READ_SE_OR_RETURN(&pps->pic_init_qs_minus26); | 710 READ_SE_OR_RETURN(&pps->pic_init_qs_minus26); |
| 806 IN_RANGE_OR_RETURN(pps->pic_init_qs_minus26, -26, 25); | 711 IN_RANGE_OR_RETURN(pps->pic_init_qs_minus26, -26, 25); |
| 807 | 712 |
| 808 READ_SE_OR_RETURN(&pps->chroma_qp_index_offset); | 713 READ_SE_OR_RETURN(&pps->chroma_qp_index_offset); |
| 809 IN_RANGE_OR_RETURN(pps->chroma_qp_index_offset, -12, 12); | 714 IN_RANGE_OR_RETURN(pps->chroma_qp_index_offset, -12, 12); |
| 810 | 715 |
| 811 READ_BOOL_OR_RETURN(&pps->deblocking_filter_control_present_flag); | 716 READ_BOOL_OR_RETURN(&pps->deblocking_filter_control_present_flag); |
| 812 READ_BOOL_OR_RETURN(&pps->constrained_intra_pred_flag); | 717 READ_BOOL_OR_RETURN(&pps->constrained_intra_pred_flag); |
| 813 READ_BOOL_OR_RETURN(&pps->redundant_pic_cnt_present_flag); | 718 READ_BOOL_OR_RETURN(&pps->redundant_pic_cnt_present_flag); |
| 814 | 719 |
| 815 if (br_.HasMoreRBSPData()) { | 720 if (br_.HasMoreData()) { |
| 816 READ_BOOL_OR_RETURN(&pps->transform_8x8_mode_flag); | 721 READ_BOOL_OR_RETURN(&pps->transform_8x8_mode_flag); |
| 817 READ_BOOL_OR_RETURN(&pps->pic_scaling_matrix_present_flag); | 722 READ_BOOL_OR_RETURN(&pps->pic_scaling_matrix_present_flag); |
| 818 | 723 |
| 819 if (pps->pic_scaling_matrix_present_flag) { | 724 if (pps->pic_scaling_matrix_present_flag) { |
| 820 DVLOG(4) << "Picture scaling matrix present"; | 725 DVLOG(4) << "Picture scaling matrix present"; |
| 821 res = ParsePPSScalingLists(*sps, pps.get()); | 726 res = ParsePPSScalingLists(*sps, pps.get()); |
| 822 if (res != kOk) | 727 if (res != kOk) |
| 823 return res; | 728 return res; |
| 824 } | 729 } |
| 825 | 730 |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1202 | 1107 |
| 1203 default: | 1108 default: |
| 1204 DVLOG(4) << "Unsupported SEI message"; | 1109 DVLOG(4) << "Unsupported SEI message"; |
| 1205 break; | 1110 break; |
| 1206 } | 1111 } |
| 1207 | 1112 |
| 1208 return kOk; | 1113 return kOk; |
| 1209 } | 1114 } |
| 1210 | 1115 |
| 1211 } // namespace content | 1116 } // namespace content |
| OLD | NEW |