Files
reims-vgpu/guest/bundle/tests/video_h264_tests.cpp
T
Anees Iqbal 163bbf7f29 🐛 Read macOS 11's parsed H.264 SPS in its own layout
On macOS 11 every H.264 picture above AppleGVA's size floor decoded to an
all-zero frame. AppleGVA 11 (108.7.2) parses the SPS header in H.264's
older syntax: four constraint flags, then one field for the four bits after
them. Later releases hold six flags and two reserved bits there. So in 11's
parsed SPS every field from level_idc on sits 8 bytes earlier than the
driver's Sps. The driver read the record as Sps and sent the host an SPS
with the wrong level, reference count and size. The host refused it
("max_num_ref_frames is out of range", then "PPS 0 has not been received"
for each picture after), and AppleGVA's H.264 decoder reports no failure,
so the guest got untouched frames with errors=0.

The record does not say its layout, so the driver now takes, once a
session, the one layout in which the record describes the picture
(chooseSpsLayout). The checks are ones a misplaced record cannot pass by
chance:
- profile_idc and level_idc are values H.264 defines;
- the SPS's seq_parameter_set_id is its PPS's;
- the coded size is the one the output surface's size rounds up to, in
  whole macroblocks (of a field pair where frame_mbs_only_flag is 0).
A record that fits no layout, or both, is refused and logged. So is a later
picture of the session that does not fit the session's layout. Nothing is
guessed. The four header bits of 11's field come back as
constraint_set4_flag, constraint_set5_flag and the reserved bits, and the
SPS written holds them as the stream did.

The layouts were measured on the guest (780M host, 2026-11-08) from a
640x480 x264 stream's records. 11.7.11 holds level_idc at 0x1c, the size at
0x820 and 0x824 and max_num_ref_frames at 0x818; 12.7.6, 13.7.8 and 26.7.2
hold them where Sps does. This matches AppleGVA 108.7.2's own reads (read
from its code). The parsed PPS has one layout on all four releases.

Tests: each of the seven x264 streams is read back whole from records of
both layouts, and only in its own, at its shown size and its coded size but
not a macroblock smaller, and not against a PPS of another SPS. The four
header bits are written back. A record that fits no layout and one that
fits both are refused. A driver session keeps the layout its first picture
showed, and refuses the other layout, a record of neither and a surface too
narrow, sending nothing of them to the host.
2026-11-08 10:11:39 -08:00

322 lines
12 KiB
C++

/*
* video_h264_tests: the video driver's H.264 parameter-set writer (src/RgfxVideoH264.*), run on
* the Mac (guest/bundle/Makefile `test`). For each stream of video_h264_cases.hpp it writes the
* SPS and PPS from the fields AppleGVA's parsed form holds and checks them against the NAL units
* the encoder wrote: the SPS bit for bit up to its VUI flag, which ours clears, and the PPS byte
* for byte. It also checks the emulation prevention of a large field, and the refusals.
*
* Exits 0 and prints a one-line summary when every check passes; aborts on the first failure.
*/
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <initializer_list>
#include <vector>
#include "RgfxVideoH264.hpp"
using namespace rgfx::video::h264;
namespace {
int g_checks = 0;
#define CHECK(cond, ...) \
do { \
g_checks++; \
if (!(cond)) { \
std::fprintf(stderr, "FAIL %s:%d: %s: ", __FILE__, __LINE__, #cond); \
std::fprintf(stderr, __VA_ARGS__); \
std::fprintf(stderr, "\n"); \
std::abort(); \
} \
} while (0)
struct Case {
const char *name;
Sps sps;
Pps pps;
std::vector<uint8_t> sps_nal; // header byte first, no start code, as the encoder escaped it
std::vector<uint8_t> pps_nal;
uint32_t sps_vui_bit;
};
template <size_t N> void fill(uint32_t (&list)[N], std::initializer_list<uint32_t> values)
{
size_t i = 0;
for (uint32_t v : values)
list[i++] = v;
}
#include "video_h264_cases.hpp"
// A NAL unit's RBSP: its emulation prevention bytes taken out.
std::vector<uint8_t> unescape(const uint8_t *nal, size_t size)
{
std::vector<uint8_t> out;
uint32_t zeros = 0;
for (size_t i = 0; i < size; i++) {
if (zeros >= 2 && nal[i] == 3) {
zeros = 0;
continue;
}
out.push_back(nal[i]);
zeros = nal[i] == 0 ? zeros + 1 : 0;
}
return out;
}
uint32_t bitAt(const std::vector<uint8_t> &bytes, uint32_t bit)
{
return (bytes[bit / 8] >> (7 - bit % 8)) & 1;
}
// The written NAL unit after its four-byte start code.
std::vector<uint8_t> payload(const std::vector<uint8_t> &written)
{
CHECK(written.size() > 5 && written[0] == 0 && written[1] == 0 && written[2] == 0 && written[3] == 1,
"no start code");
return std::vector<uint8_t>(written.begin() + 4, written.end());
}
// No 0x000000, 0x000001 or 0x000002 left (0x000003 is the escape itself).
void checkEscaped(const std::vector<uint8_t> &nal, const char *name)
{
for (size_t i = 2; i < nal.size(); i++)
CHECK(!(nal[i - 2] == 0 && nal[i - 1] == 0 && nal[i] <= 2), "%s: unescaped run at %zu", name, i);
}
void checkCase(const Case &c)
{
std::vector<uint8_t> written;
CHECK(writeSps(c.sps, written), "%s: SPS refused", c.name);
std::vector<uint8_t> nal = payload(written);
checkEscaped(nal, c.name);
std::vector<uint8_t> ours = unescape(nal.data(), nal.size());
std::vector<uint8_t> theirs = unescape(c.sps_nal.data(), c.sps_nal.size());
for (uint32_t bit = 0; bit < c.sps_vui_bit; bit++)
CHECK(bitAt(ours, bit) == bitAt(theirs, bit), "%s: SPS bit %u differs", c.name, bit);
CHECK(bitAt(theirs, c.sps_vui_bit) == 1, "%s: the stream's SPS has no VUI", c.name);
// Ours: the VUI flag clear, the stop bit, and zeros to the byte's end.
CHECK(bitAt(ours, c.sps_vui_bit) == 0, "%s: VUI flag set", c.name);
CHECK(bitAt(ours, c.sps_vui_bit + 1) == 1, "%s: no stop bit", c.name);
CHECK(ours.size() == (c.sps_vui_bit + 2 + 7) / 8, "%s: SPS of %zu bytes", c.name, ours.size());
for (uint32_t bit = c.sps_vui_bit + 2; bit < ours.size() * 8; bit++)
CHECK(bitAt(ours, bit) == 0, "%s: SPS trailing bit %u set", c.name, bit);
written.clear();
CHECK(writePps(c.pps, c.sps, written), "%s: PPS refused", c.name);
nal = payload(written);
CHECK(nal == c.pps_nal, "%s: PPS of %zu bytes differs from the stream's %zu", c.name, nal.size(),
c.pps_nal.size());
// Both appended after what out held.
std::vector<uint8_t> spsWritten, ppsWritten, both = {0xaa};
CHECK(writeSps(c.sps, spsWritten) && writePps(c.pps, c.sps, ppsWritten), "%s: refused", c.name);
CHECK(writeSps(c.sps, both) && writePps(c.pps, c.sps, both), "%s: refused", c.name);
CHECK(both.size() == 1 + spsWritten.size() + ppsWritten.size() && both[0] == 0xaa &&
std::memcmp(both.data() + 1, spsWritten.data(), spsWritten.size()) == 0,
"%s: not appended", c.name);
}
void checkEscapes()
{
// Profile 66 with no flags and level 0 makes the second and third bytes zero; then come five
// ue(0) (the SPS id, frame-num length, POC type 0, its lsb length, the references) and the
// gaps flag, and the width's ue with 24 leading zeros, which runs from bit 30 to bit 53: two
// zero bytes, then 0x02 or 0x03.
Sps s = {};
s.profile_idc = 66;
s.pic_width_in_mbs_minus1 = 0xffffff;
s.frame_mbs_only_flag = 1;
std::vector<uint8_t> written;
CHECK(writeSps(s, written), "large width refused");
std::vector<uint8_t> nal = payload(written);
checkEscaped(nal, "large width");
std::vector<uint8_t> rbsp = unescape(nal.data(), nal.size());
CHECK(rbsp.size() < nal.size(), "nothing escaped");
CHECK(rbsp[0] == 0x67 && rbsp[1] == 66 && rbsp[2] == 0 && rbsp[3] == 0, "SPS head");
uint32_t bit = 8 + 24 + 5 + 1;
uint32_t zeros = 0;
while (bitAt(rbsp, bit) == 0) {
zeros++;
bit++;
}
uint32_t value = 0;
for (uint32_t i = 0; i <= zeros; i++)
value = (value << 1) | bitAt(rbsp, bit++);
CHECK(zeros == 24 && value - 1 == 0xffffff, "width read back as %u zeros, %#x", zeros, value - 1);
}
void checkRefusals()
{
Case c = case_high();
std::vector<uint8_t> out = {1, 2};
Sps s = c.sps;
s.chroma_format_idc = 3;
CHECK(!writeSps(s, out) && out.size() == 2, "4:4:4 taken");
s = c.sps;
s.pic_order_cnt_type = 3;
CHECK(!writeSps(s, out) && out.size() == 2, "POC type 3 taken");
Pps p = c.pps;
p.num_slice_groups_minus1 = 1;
CHECK(!writePps(p, c.sps, out) && out.size() == 2, "slice groups taken");
p = c.pps;
p.weighted_bipred_idc = 3;
CHECK(!writePps(p, c.sps, out) && out.size() == 2, "bipred 3 taken");
}
// The record of `s` as macOS 11's AppleGVA lays it out (SpsLayout::kFourConstraintFlags): its
// four constraint flags, then the four header bits after them in one field, constraint_set4_flag
// first, then Sps's fields from level_idc on, 8 bytes earlier. Both records are Sps's size, as
// AppleGVA's run on past it (the VUI).
std::vector<uint8_t> recordOn11(const Sps &s)
{
std::vector<uint8_t> r(sizeof(Sps), 0);
std::memcpy(r.data(), &s, 0x18);
const uint32_t bits =
s.constraint_set_flag[4] << 3 | s.constraint_set_flag[5] << 2 | s.reserved_zero_2bits;
std::memcpy(r.data() + 0x18, &bits, 4);
std::memcpy(r.data() + 0x1c, reinterpret_cast<const uint8_t *>(&s) + offsetof(Sps, level_idc),
sizeof(Sps) - offsetof(Sps, level_idc));
return r;
}
// The record of `s` as every later release measured lays it out (SpsLayout::kSixConstraintFlags).
std::vector<uint8_t> recordOn12(const Sps &s)
{
std::vector<uint8_t> r(sizeof(Sps));
std::memcpy(r.data(), &s, sizeof s);
return r;
}
// The size of the pictures `s` describes, as the surface AppleGVA decodes them into holds them:
// its coded size less its cropping, in 4:2:0's units.
void shown(const Sps &s, uint32_t *width, uint32_t *height)
{
const uint32_t fields = 2 - s.frame_mbs_only_flag;
*width = 16 * (s.pic_width_in_mbs_minus1 + 1) -
2 * (s.frame_crop_left_offset + s.frame_crop_right_offset);
*height = 16 * fields * (s.pic_height_in_map_units_minus1 + 1) -
2 * fields * (s.frame_crop_top_offset + s.frame_crop_bottom_offset);
}
bool same(const Sps &a, const Sps &b) { return std::memcmp(&a, &b, sizeof a) == 0; }
// Each stream's SPS is read back whole from the record of either layout, and only in its own:
// at its shown size and at its coded size, and at neither a macroblock smaller.
void checkLayouts(const Case &c)
{
uint32_t width, height;
shown(c.sps, &width, &height);
const uint32_t id = c.pps.seq_parameter_set_id;
const uint32_t coded[2] = {16 * (c.sps.pic_width_in_mbs_minus1 + 1),
16 * (2 - c.sps.frame_mbs_only_flag) *
(c.sps.pic_height_in_map_units_minus1 + 1)};
struct Record {
std::vector<uint8_t> bytes;
SpsLayout layout;
};
for (const Record &r : {Record{recordOn12(c.sps), SpsLayout::kSixConstraintFlags},
Record{recordOn11(c.sps), SpsLayout::kFourConstraintFlags}}) {
const char *on = r.layout == SpsLayout::kSixConstraintFlags ? "12's" : "11's";
CHECK(same(spsIn(r.bytes.data(), r.layout), c.sps), "%s: %s record read back", c.name, on);
const uint32_t whole[2] = {width, height};
for (const uint32_t *size : {whole, coded}) {
SpsLayout layout = {};
Sps sps = {};
NoLayout why = {};
CHECK(chooseSpsLayout(r.bytes.data(), id, size[0], size[1], &layout, &sps, &why) &&
layout == r.layout && same(sps, c.sps),
"%s: %s record at %ux%u", c.name, on, size[0], size[1]);
}
SpsLayout layout = {};
Sps sps = {};
NoLayout why = {};
CHECK(!chooseSpsLayout(r.bytes.data(), id, coded[0] - 16, height, &layout, &sps, &why) &&
why == NoLayout::kNone,
"%s: %s record fits a surface a macroblock narrower", c.name, on);
CHECK(!chooseSpsLayout(r.bytes.data(), id, width,
coded[1] - 16 * (2 - c.sps.frame_mbs_only_flag), &layout, &sps, &why) &&
why == NoLayout::kNone,
"%s: %s record fits a surface a macroblock shorter", c.name, on);
CHECK(!chooseSpsLayout(r.bytes.data(), id + 1, width, height, &layout, &sps, &why) &&
why == NoLayout::kNone,
"%s: %s record fits a PPS of another SPS", c.name, on);
}
}
// 11's field of four header bits comes back as constraint_set4_flag, constraint_set5_flag and
// the two reserved bits, which the SPS written holds as the stream had them.
void checkHeaderBits()
{
Case c = case_high();
c.sps.constraint_set_flag[4] = 1;
c.sps.reserved_zero_2bits = 2;
const std::vector<uint8_t> record = recordOn11(c.sps);
const Sps s = spsIn(record.data(), SpsLayout::kFourConstraintFlags);
CHECK(s.constraint_set_flag[4] == 1 && s.constraint_set_flag[5] == 0 && s.reserved_zero_2bits == 2,
"11's header bits read as %u %u %u", s.constraint_set_flag[4], s.constraint_set_flag[5],
s.reserved_zero_2bits);
std::vector<uint8_t> ours, theirs;
CHECK(writeSps(s, ours) && writeSps(c.sps, theirs) && ours == theirs, "11's SPS written");
}
// A record that describes its picture in no layout, or in both, is refused, the reason said.
void checkLayoutRefusals()
{
Case c = case_high();
uint32_t width, height;
shown(c.sps, &width, &height);
SpsLayout layout = {};
Sps sps = {};
NoLayout why = {};
Sps s = c.sps;
s.level_idc = 0;
for (const std::vector<uint8_t> &r : {recordOn12(s), recordOn11(s)})
CHECK(!chooseSpsLayout(r.data(), 0, width, height, &layout, &sps, &why) &&
why == NoLayout::kNone,
"a record of level 0 taken");
s = c.sps;
s.profile_idc = 101;
CHECK(!chooseSpsLayout(recordOn12(s).data(), 0, width, height, &layout, &sps, &why) &&
why == NoLayout::kNone,
"a record of profile 101 taken");
// A 32x32 picture whose record reads as one in both layouts: 12's from its own fields, 11's
// from the words 8 bytes earlier (level_idc from constraint_set_flag[5], the size from
// max_num_ref_frames and the gaps flag, frame_mbs_only_flag from the width).
s = {};
s.profile_idc = 66;
s.level_idc = 30;
s.constraint_set_flag[5] = 30;
s.max_num_ref_frames = 1;
s.gaps_in_frame_num_value_allowed_flag = 1;
s.pic_width_in_mbs_minus1 = 1;
s.pic_height_in_map_units_minus1 = 1;
s.frame_mbs_only_flag = 1;
CHECK(spsFits(spsIn(recordOn12(s).data(), SpsLayout::kFourConstraintFlags), 0, 32, 32),
"the twofold record fits not in 11's layout");
CHECK(!chooseSpsLayout(recordOn12(s).data(), 0, 32, 32, &layout, &sps, &why) &&
why == NoLayout::kMany,
"a record fitting both layouts taken");
}
} // namespace
int main()
{
for (auto make : kCases) {
checkCase(make());
checkLayouts(make());
}
checkEscapes();
checkRefusals();
checkHeaderBits();
checkLayoutRefusals();
std::printf("video_h264_tests: %d checks passed over %zu streams\n", g_checks, kCases.size());
return 0;
}