Fix a bug to read last 3 bytes in AnnexBUtils

In the current implementation due to missing "="
operator, last three bytes were not checked for
000 or 001 sequence.

In sample_no_bframes file, few samples has extra 0 at the end.
It was working fine with the bug because muxer was writing some
harmless 0 at the end.

PiperOrigin-RevId: 672994054
This commit is contained in:
sheenachhabra 2024-09-10 09:28:23 -07:00 committed by Copybara-Service
parent 8bfa7e2de1
commit 35dc10aac8
4 changed files with 42 additions and 10 deletions

View File

@ -138,14 +138,14 @@ import java.nio.ByteBuffer;
}
private static boolean isThreeByteNalStartCode(ByteBuffer input, int currentIndex) {
return (currentIndex < input.limit() - THREE_BYTE_NAL_START_CODE_SIZE
return (currentIndex <= input.limit() - THREE_BYTE_NAL_START_CODE_SIZE
&& input.get(currentIndex) == 0x00
&& input.get(currentIndex + 1) == 0x00
&& input.get(currentIndex + 2) == 0x01);
}
private static boolean isThreeBytesZeroSequence(ByteBuffer input, int currentIndex) {
return (currentIndex < input.limit() - THREE_BYTE_NAL_START_CODE_SIZE
return (currentIndex <= input.limit() - THREE_BYTE_NAL_START_CODE_SIZE
&& input.get(currentIndex) == 0x00
&& input.get(currentIndex + 1) == 0x00
&& input.get(currentIndex + 2) == 0x00);

View File

@ -73,6 +73,38 @@ public final class DefaultAnnexBToAvccConverterTest {
assertThrows(IllegalStateException.class, () -> annexBToAvccConverter.process(input));
}
@Test
public void convertAnnexBToAvcc_withExtraZeroesAtTheEnd_removesExtraZeroes() {
ByteBuffer input = ByteBuffer.allocate(10);
// Add 3 byte start code for the NAL unit.
input.put(0, (byte) 0);
input.put(1, (byte) 0);
input.put(2, (byte) 1);
// Add fake NAL unit data;
input.put(3, (byte) 300);
input.put(4, (byte) 300);
input.put(5, (byte) 300);
input.put(6, (byte) 300);
// Add extra zeroes at the end.
input.put(7, (byte) 0);
input.put(8, (byte) 0);
input.put(9, (byte) 0);
AnnexBToAvccConverter annexBToAvccConverter = AnnexBToAvccConverter.DEFAULT;
ByteBuffer output = annexBToAvccConverter.process(input);
ByteBuffer expectedOutput = ByteBuffer.allocate(8);
// First 4 bytes for NAL unit length.
expectedOutput.putInt(4);
// Fake NAL unit data;
expectedOutput.put(4, (byte) 300);
expectedOutput.put(5, (byte) 300);
expectedOutput.put(6, (byte) 300);
expectedOutput.put(7, (byte) 300);
expectedOutput.rewind();
assertThat(output).isEqualTo(expectedOutput);
}
/** Returns {@link ByteBuffer} filled with random NAL unit data without start code. */
private static ByteBuffer generateFakeNalUnitData(int length) {
ByteBuffer buffer = ByteBuffer.allocateDirect(length);

View File

@ -201,7 +201,7 @@ track 0:
flags = 536870913
data = length 6, hash 31B22286
track 1:
total output bytes = 301222
total output bytes = 301213
sample count = 30
format 0:
id = 2
@ -237,7 +237,7 @@ track 1:
sample 3:
time = 100100
flags = 0
data = length 976, hash CD49C23C
data = length 973, hash 916BC744
sample 4:
time = 133455
flags = 0
@ -265,7 +265,7 @@ track 1:
sample 10:
time = 333655
flags = 0
data = length 9792, hash B2B9AB4B
data = length 9789, hash A2D01095
sample 11:
time = 367022
flags = 0
@ -309,7 +309,7 @@ track 1:
sample 21:
time = 700700
flags = 0
data = length 12192, hash E0F8A71A
data = length 12189, hash 423C3F26
sample 22:
time = 734055
flags = 0

View File

@ -7,7 +7,7 @@ seekMap:
getPosition(1065600) = [[timeUs=0, position=400052]]
numberOfTracks = 2
track 0:
total output bytes = 301222
total output bytes = 301213
sample count = 30
format 0:
id = 1
@ -43,7 +43,7 @@ track 0:
sample 3:
time = 100100
flags = 0
data = length 976, hash CD49C23C
data = length 973, hash 916BC744
sample 4:
time = 133455
flags = 0
@ -71,7 +71,7 @@ track 0:
sample 10:
time = 333655
flags = 0
data = length 9792, hash B2B9AB4B
data = length 9789, hash A2D01095
sample 11:
time = 367022
flags = 0
@ -115,7 +115,7 @@ track 0:
sample 21:
time = 700700
flags = 0
data = length 12192, hash E0F8A71A
data = length 12189, hash 423C3F26
sample 22:
time = 734055
flags = 0