Skip to content

Commit

Permalink
Fixed bitstream reader
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjw24 committed Oct 7, 2024
1 parent f2d83c2 commit cd7fab2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions source/Lib/CommonLib/BitStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ uint32_t InputBitstream::read (uint32_t uiNumberOfBits)
m_numBitsRead += uiNumberOfBits;
#endif

constexpr static uint64_t ONES = ~static_cast<uint64_t>( 0 ); // need to ensure 64 bits for the mask, because shift by 32 is UB for uin32_t

/* NB, bits are extracted from the MSB of each byte. */
uint32_t retval = 0;
Expand All @@ -111,7 +112,7 @@ uint32_t InputBitstream::read (uint32_t uiNumberOfBits)
* n=3, len(H)=7: -VVV HHHH, shift_down=4, mask=0xf8
*/
retval = static_cast<uint32_t>( m_held_bits >> ( m_num_held_bits - uiNumberOfBits ) );
retval &= ~( ~0u << uiNumberOfBits );
retval &= ~( ONES << uiNumberOfBits );
m_num_held_bits -= uiNumberOfBits;

return retval;
Expand All @@ -127,7 +128,7 @@ uint32_t InputBitstream::read (uint32_t uiNumberOfBits)
/* n=5, len(H)=3: ---- -VVV, mask=0x07, shift_up=5-3=2,
* n=9, len(H)=3: ---- -VVV, mask=0x07, shift_up=9-3=6 */
uiNumberOfBits -= m_num_held_bits;
retval = static_cast<uint32_t>( m_held_bits ) & ~( ~0u << m_num_held_bits ); // we can cast to 32 bits, because the held bits are the rightmost bits
retval = static_cast<uint32_t>( m_held_bits ) & ~( ONES << m_num_held_bits ); // we can cast to 32 bits, because the held bits are the rightmost bits
retval <<= uiNumberOfBits;
}

Expand Down
4 changes: 2 additions & 2 deletions source/Lib/DecoderLib/HLSyntaxReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,11 +1194,11 @@ void HLSyntaxReader::parseVUI( VUI* pcVUI, unsigned vuiPayloadSize )
void HLSyntaxReader::parseGeneralHrdParameters( GeneralHrdParams *hrd )
{
X_READ_CODE_NO_RANGE( num_units_in_tick, 32 );
// CHECK( num_units_in_tick <= 0, "num_units_in_tick shall be greater than 0" ); // this breaks the testset (TEMPSCAL_B_Panasonic_7.bit) although specified in the spec
CHECK( num_units_in_tick <= 0, "num_units_in_tick shall be greater than 0" );
hrd->setNumUnitsInTick( num_units_in_tick );

X_READ_CODE_NO_RANGE( time_scale, 32 );
// CHECK( time_scale <= 0, "The value of time_scale shall be greater than 0." ); // this breaks the testset (HRD_A_Fujitsu_3.bit & HRD_B_Fujitsu_2.bit) although specified in the spec
CHECK( time_scale <= 0, "The value of time_scale shall be greater than 0." );
hrd->setTimeScale( time_scale );

X_READ_FLAG( general_nal_hrd_params_present_flag );
Expand Down

0 comments on commit cd7fab2

Please sign in to comment.