/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <stdint.h>
#include "gtest/gtest.h"
#include "BitReader.h"
#include "BitWriter.h"
#include "H264.h"
using namespace mozilla;
TEST(BitWriter, BitWriter)
{
RefPtr<MediaByteBuffer> test =
new MediaByteBuffer();
BitWriter b(test);
b.WriteBit(
false );
b.WriteBits(~
1 ULL,
1 );
// ensure that extra bits don't modify byte buffer.
b.WriteBits(
3 ,
1 );
b.WriteUE(
1280 /
16 -
1 );
b.WriteUE(
720 /
16 -
1 );
b.WriteUE(
1280 );
b.WriteUE(
720 );
b.WriteBit(
true );
b.WriteBit(
false );
b.WriteBit(
true );
b.WriteU8(
7 );
b.WriteU32(
16356 );
b.WriteU64(
116356 );
b.WriteBits(~(
0 ULL) & ~
1 ULL,
16 );
b.WriteULEB128(
16 ULL);
b.WriteULEB128(
31895793 ULL);
b.WriteULEB128(
426894039235654 ULL);
const uint32_t length = b.BitCount();
b.CloseWithRbspTrailing();
BitReader c(test);
EXPECT_EQ(c.ReadBit(),
false );
EXPECT_EQ(c.ReadBit(),
false );
EXPECT_EQ(c.ReadBit(),
true );
EXPECT_EQ(c.ReadUE(),
1280 u /
16 -
1 );
EXPECT_EQ(c.ReadUE(),
720 u /
16 -
1 );
EXPECT_EQ(c.ReadUE(),
1280 u);
EXPECT_EQ(c.ReadUE(),
720 u);
EXPECT_EQ(c.ReadBit(),
true );
EXPECT_EQ(c.ReadBit(),
false );
EXPECT_EQ(c.ReadBit(),
true );
EXPECT_EQ(c.ReadBits(
8 ),
7 u);
EXPECT_EQ(c.ReadU32(),
16356 u);
EXPECT_EQ(c.ReadU64(),
116356 u);
EXPECT_EQ(c.ReadBits(
16 ),
0 xfffeu);
EXPECT_EQ(c.ReadULEB128(),
16 ull);
EXPECT_EQ(c.ReadULEB128(),
31895793 ull);
EXPECT_EQ(c.ReadULEB128(),
426894039235654 ull);
EXPECT_EQ(length, BitReader::GetBitLength(test));
}
TEST(BitWriter, AdvanceBytes)
{
RefPtr<MediaByteBuffer> test =
new MediaByteBuffer();
BitWriter b(test);
b.WriteBits(
0 xff,
8 );
EXPECT_EQ(test->Length(),
1 u);
uint8_t data[] = {
0 xfe,
0 xfd};
test->AppendElements(data,
sizeof (data));
EXPECT_EQ(test->Length(),
3 u);
b.AdvanceBytes(
2 );
b.WriteBits(
0 xfc,
8 );
EXPECT_EQ(test->Length(),
4 u);
BitReader c(test);
EXPECT_EQ(c.ReadU32(),
0 xfffefdfc);
}
TEST(BitWriter, SPS)
{
uint8_t sps_pps[] = {
0 x01,
0 x4d,
0 x40,
0 x0c,
0 xff,
0 xe1,
0 x00,
0 x1b,
0 x67,
0 x4d,
0 x40,
0 x0c,
0 xe8,
0 x80,
0 x80,
0 x9d,
0 x80,
0 xb5,
0 x01,
0 x01,
0 x01,
0 x40,
0 x00,
0 x00,
0 x03,
0 x00,
0 x40,
0 x00,
0 x00,
0 x0f,
0 x03,
0 xc5,
0 x0a,
0 x44,
0 x80,
0 x01,
0 x00,
0 x04,
0 x68,
0 xeb,
0 xef,
0 x20};
RefPtr<MediaByteBuffer> extraData =
new MediaByteBuffer();
extraData->AppendElements(sps_pps,
sizeof (sps_pps));
SPSData spsdata1;
bool success = H264::DecodeSPSFromExtraData(extraData, spsdata1);
EXPECT_EQ(success,
true );
auto testOutput = [&](uint8_t aProfile, uint8_t aConstraints, uint8_t aLevel,
gfx::IntSize aSize,
char const * aDesc) {
RefPtr<MediaByteBuffer> extraData = H264::CreateExtraData(
aProfile, aConstraints, H264_LEVEL{aLevel}, aSize);
SPSData spsData;
success = H264::DecodeSPSFromExtraData(extraData, spsData);
EXPECT_EQ(success,
true ) << aDesc;
EXPECT_EQ(spsData.profile_idc, aProfile) << aDesc;
EXPECT_EQ(spsData.constraint_set0_flag, (aConstraints >>
7 ) &
1 ) << aDesc;
EXPECT_EQ(spsData.constraint_set1_flag, (aConstraints >>
6 ) &
1 ) << aDesc;
EXPECT_EQ(spsData.constraint_set2_flag, (aConstraints >>
5 ) &
1 ) << aDesc;
EXPECT_EQ(spsData.constraint_set3_flag, (aConstraints >>
4 ) &
1 ) << aDesc;
EXPECT_EQ(spsData.constraint_set4_flag, (aConstraints >>
3 ) &
1 ) << aDesc;
EXPECT_EQ(spsData.constraint_set5_flag, (aConstraints >>
2 ) &
1 ) << aDesc;
EXPECT_EQ(spsData.level_idc, aLevel) << aDesc;
EXPECT_TRUE(!aSize.IsEmpty());
EXPECT_EQ(spsData.pic_width,
static_cast <uint32_t>(aSize.width)) << aDesc;
EXPECT_EQ(spsData.pic_height,
static_cast <uint32_t>(aSize.height)) << aDesc;
};
testOutput(
0 x42,
0 x40,
0 x1E, {
1920 ,
1080 },
"Constrained Baseline Profile" );
testOutput(
0 x4D,
0 x00,
0 x0B, {
300 ,
300 },
"Main Profile" );
testOutput(
0 x64,
0 x0C,
0 x33, {
1280 ,
720 },
"Constrained High Profile" );
}
Messung V0.5 in Prozent C=98 H=100 G=98
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-08)
¤
*© Formatika GbR, Deutschland