diff --git a/packager/media/filters/h264_bit_reader.cc b/packager/media/filters/h264_bit_reader.cc index 2e71f5b715..22b221cbb5 100644 --- a/packager/media/filters/h264_bit_reader.cc +++ b/packager/media/filters/h264_bit_reader.cc @@ -86,6 +86,48 @@ bool H264BitReader::ReadBits(int num_bits, int* out) { return true; } +bool H264BitReader::ReadUE(int* val) { + int num_bits = -1; + int bit; + int rest; + + // Count the number of contiguous zero bits. + do { + if (!ReadBits(1, &bit)) + return false; + num_bits++; + } while (bit == 0); + + if (num_bits > 31) + return false; + + // Calculate exp-Golomb code value of size num_bits. + *val = (1 << num_bits) - 1; + + if (num_bits > 0) { + if (!ReadBits(num_bits, &rest)) + return false; + *val += rest; + } + + return true; +} + +bool H264BitReader::ReadSE(int* val) { + int ue; + + // See Chapter 9 in the spec. + if (!ReadUE(&ue)) + return false; + + if (ue % 2 == 0) + *val = -(ue / 2); + else + *val = ue / 2 + 1; + + return true; +} + off_t H264BitReader::NumBitsLeft() { return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8); } diff --git a/packager/media/filters/h264_bit_reader.h b/packager/media/filters/h264_bit_reader.h index 4c3d55a49e..0107e269a4 100644 --- a/packager/media/filters/h264_bit_reader.h +++ b/packager/media/filters/h264_bit_reader.h @@ -36,6 +36,13 @@ class H264BitReader { // bits in the stream), true otherwise. bool ReadBits(int num_bits, int* out); + // Exp-Golomb code parsing as specified in chapter 9.1 of the spec. + // Read one unsigned exp-Golomb code from the stream and return in |*val|. + bool ReadUE(int* val); + + // Read one signed exp-Golomb code from the stream and return in |*val|. + bool ReadSE(int* val); + // Return the number of bits left in the stream. off_t NumBitsLeft(); diff --git a/packager/media/filters/h264_parser.cc b/packager/media/filters/h264_parser.cc index ea87168fcd..860103b5f0 100644 --- a/packager/media/filters/h264_parser.cc +++ b/packager/media/filters/h264_parser.cc @@ -115,13 +115,11 @@ H264SEIMessage::H264SEIMessage() { #define READ_BITS_OR_RETURN(num_bits, out) \ do { \ - int _out; \ - if (!br->ReadBits(num_bits, &_out)) { \ + if (!br->ReadBits(num_bits, (out))) { \ DVLOG(1) \ << "Error in stream: unexpected EOS while trying to read " #out; \ return kInvalidStream; \ } \ - *out = _out; \ } while (0) #define READ_BOOL_OR_RETURN(out) \ @@ -132,12 +130,12 @@ H264SEIMessage::H264SEIMessage() { << "Error in stream: unexpected EOS while trying to read " #out; \ return kInvalidStream; \ } \ - *out = _out != 0; \ + *(out) = _out != 0; \ } while (0) #define READ_UE_OR_RETURN(out) \ do { \ - if (ReadUE(br, out) != kOk) { \ + if (!br->ReadUE(out)) { \ DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \ return kInvalidStream; \ } \ @@ -145,7 +143,7 @@ H264SEIMessage::H264SEIMessage() { #define READ_SE_OR_RETURN(out) \ do { \ - if (ReadSE(br, out) != kOk) { \ + if (!br->ReadSE(out)) { \ DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \ return kInvalidStream; \ } \ @@ -199,48 +197,6 @@ const H264SPS* H264Parser::GetSPS(int sps_id) { return active_SPSes_[sps_id]; } -H264Parser::Result H264Parser::ReadUE(H264BitReader* br, int* val) { - int num_bits = -1; - int bit; - int rest; - - // Count the number of contiguous zero bits. - do { - READ_BITS_OR_RETURN(1, &bit); - num_bits++; - } while (bit == 0); - - if (num_bits > 31) - return kInvalidStream; - - // Calculate exp-Golomb code value of size num_bits. - *val = (1 << num_bits) - 1; - - if (num_bits > 0) { - READ_BITS_OR_RETURN(num_bits, &rest); - *val += rest; - } - - return kOk; -} - -H264Parser::Result H264Parser::ReadSE(H264BitReader* br, int* val) { - int ue; - Result res; - - // See Chapter 9 in the spec. - res = ReadUE(br, &ue); - if (res != kOk) - return res; - - if (ue % 2 == 0) - *val = -(ue / 2); - else - *val = ue / 2 + 1; - - return kOk; -} - // Default scaling lists (per spec). static const int kDefault4x4Intra[kH264ScalingList4x4Length] = { 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42, }; diff --git a/packager/media/filters/h264_parser.h b/packager/media/filters/h264_parser.h index 3f954181d9..aaa46f64f4 100644 --- a/packager/media/filters/h264_parser.h +++ b/packager/media/filters/h264_parser.h @@ -285,13 +285,6 @@ class H264Parser { Result ParseSEI(const Nalu& nalu, H264SEIMessage* sei_msg); private: - // Exp-Golomb code parsing as specified in chapter 9.1 of the spec. - // Read one unsigned exp-Golomb code from the stream and return in |*val|. - Result ReadUE(H264BitReader* br, int* val); - - // Read one signed exp-Golomb code from the stream and return in |*val|. - Result ReadSE(H264BitReader* br, int* val); - // Parse scaling lists (see spec). Result ParseScalingList(H264BitReader* br, int size, diff --git a/packager/media/filters/nalu_reader.cc b/packager/media/filters/nalu_reader.cc index 806ede9a54..e03237e832 100644 --- a/packager/media/filters/nalu_reader.cc +++ b/packager/media/filters/nalu_reader.cc @@ -137,8 +137,7 @@ NaluReader::Result NaluReader::Advance(Nalu* nalu) { DVLOG(4) << "NALU type: " << static_cast(nalu->type()) << " at: " << reinterpret_cast(nalu->data()) - << " data size: " << nalu->payload_size() - << " ref: " << static_cast(nalu->ref_idc()); + << " data size: " << nalu->payload_size(); return NaluReader::kOk; }