Bug 1970973 - part1 : add a method to advance more than 32 bits for BitReader. r=media-playback-reviewers,karlt a=pascalc

Differential Revision: https://phabricator.services.mozilla.com/D253259
This commit is contained in:
alwu
2025-07-01 02:15:51 +00:00
committed by pchevrel@mozilla.com
parent 1e06eff941
commit 554c812f8c
3 changed files with 22 additions and 7 deletions

View File

@@ -6,6 +6,8 @@
#include "BitReader.h"
#include "mozilla/Unused.h"
namespace mozilla {
BitReader::BitReader(const mozilla::MediaByteBuffer* aBuffer)
@@ -194,4 +196,16 @@ uint32_t BitReader::GetBitLength(const mozilla::MediaByteBuffer* aNAL) {
return size;
}
size_t BitReader::AdvanceBits(size_t aNum) {
const size_t advanceBits = std::min(aNum, BitsLeft());
size_t temp = advanceBits;
while (temp > 0) {
uint32_t readBits = temp > 32 ? 32 : temp;
// TODO : return error if reading less bits than expectation in bug 1972401.
Unused << ReadBits(readBits);
temp -= readBits;
}
return advanceBits;
}
} // namespace mozilla

View File

@@ -31,6 +31,12 @@ class BitReader {
// Limited to unsigned 64 bits.
CheckedUint64 ReadULEB128();
// Advance bits and return the actual number of bits forwarded. Unlike
// ReadBits, which can only read up to 32 bits, this function does not limit
// how many bits it can advance. If fewer bits are available than requested,
// it will only advance the available bits.
size_t AdvanceBits(size_t aNum);
// Return the number of bits parsed so far;
size_t BitCount() const;
// Return the number of bits left.

View File

@@ -158,13 +158,8 @@ Result<HVCCConfig, nsresult> HVCCConfig::Parse(
const uint8_t* currentPtr =
aExtraData->Elements() + reader.BitCount() / 8;
H265NALU nalu(currentPtr, nalUnitLength);
// ReadBits can only read at most 32 bits at a time.
uint32_t nalSize = nalUnitLength * 8;
while (nalSize > 0) {
uint32_t readBits = nalSize > 32 ? 32 : nalSize;
reader.ReadBits(readBits);
nalSize -= readBits;
}
uint32_t nalBitsLength = nalUnitLength * 8;
Unused << reader.AdvanceBits(nalBitsLength);
// Per ISO_IEC-14496-15-2022, 8.3.2.1.3 Semantics, NALU should only be
// SPS/PPS/VPS or SEI, ignore all the other types of NALU.
if (nalu.IsSPS() || nalu.IsPPS() || nalu.IsVPS() || nalu.IsSEI()) {