| @@ -0,0 +1,89 @@ |
| 1 | +//! Allocation-free elementary-packet and PTS output helpers. Callers own sinks. |
| 2 | +const std = ; |
| 3 | +/// Validate every four-byte big-endian NAL length before any destination write. |
| 4 | +/// Annex B uses four-byte start codes, so conversion preserves byte length. |
| 5 | +pub fn annexB(input: []const u8, output: []u8) ![]const u8 { |
| 6 | + if (input.len == 0) return error.InvalidPacket; |
| 7 | + var at: usize = 0; |
| 8 | + while (at < input.len) { |
| 9 | + if (input.len - at < 4) return error.InvalidPacket; |
| 10 | + const n = std.mem.readInt(u32, input[at..][0..4], .big); |
| 11 | + at += 4; |
| 12 | + if (n == 0 or n > input.len - at) return error.InvalidPacket; |
| 13 | + at += n; |
| 14 | + } |
| 15 | + if (output.len < input.len) return error.BufferTooSmall; |
| 16 | + at = 0; |
| 17 | + while (at < input.len) { |
| 18 | + const n = std.mem.readInt(u32, input[at..][0..4], .big); |
| 19 | + @memcpy(output[at..][0..4], &[_]u8{ 0, 0, 0, 1 }); |
| 20 | + @memcpy(output[at + 4 ..][0..n], input[at + 4 ..][0..n]); |
| 21 | + at += 4 + n; |
| 22 | + } |
| 23 | + return output[0..input.len]; |
| 24 | +} |
| 25 | +/// Microsecond PTS as milliseconds with exactly three decimals and LF. Integer |
| 26 | +/// arithmetic preserves zero and all u64 values without floating-point loss. |
| 27 | +pub fn formatPts(timestamp_us: u64, output: []u8) ![]const u8 { |
| 28 | + var scratch: [32]u8 = undefined; |
| 29 | + const line = try std.fmt.bufPrint(&scratch, "{d}.{d:0>3}\n", .{ timestamp_us / 1000, timestamp_us % 1000 }); |
| 30 | + if (output.len < line.len) return error.BufferTooSmall; |
| 31 | + @memcpy(output[0..line.len], line); |
| 32 | + return output[0..line.len]; |
| 33 | +} |
| 34 | +pub const Sink = struct { context: *anyopaque, write: *const fn (*anyopaque, []const u8) anyerror!void }; |
| 35 | +pub const PtsWriter = struct { |
| 36 | + sink: Sink, |
| 37 | + last: ?u64 = null, |
| 38 | + failed: bool = false, |
| 39 | + pub fn frame(self: *PtsWriter, timestamp_us: u64) !void { |
| 40 | + if (self.failed) return error.SinkFailed; |
| 41 | + if (self.last) |last| if (timestamp_us <= last) return error.InvalidTimestamp; |
| 42 | + var buffer: [32]u8 = undefined; |
| 43 | + const line = try formatPts(timestamp_us, &buffer); |
| 44 | + self.sink.write(self.sink.context, line) catch |err| { |
| 45 | + self.failed = true; |
| 46 | + return err; |
| 47 | + }; |
| 48 | + self.last = timestamp_us; |
| 49 | + } |
| 50 | +}; |
| 51 | +test "Annex B converts multiple NAL units and rejects malformed input atomically" { |
| 52 | + var out: [16]u8 = undefined; |
| 53 | + @memset(&out, 0x55); |
| 54 | + for ([_][]const u8{ &.{}, &.{0}, &.{ 0, 0, 0, 0 }, &.{ 0, 0, 0, 2, 1 }, &.{ 0, 0, 0, 1, 5, 0 } }) |bad| { |
| 55 | + try std.testing.expectError(error.InvalidPacket, annexB(bad, &out)); |
| 56 | + for (out) |byte| try std.testing.expectEqual(@as(u8, 0x55), byte); |
| 57 | + } |
| 58 | + const packet = [_]u8{ 0, 0, 0, 2, 0x67, 0x11, 0, 0, 0, 1, 0x68 }; |
| 59 | + try std.testing.expectError(error.BufferTooSmall, annexB(&packet, out[0..10])); |
| 60 | + try std.testing.expectEqualSlices(u8, &.{ 0, 0, 0, 1, 0x67, 0x11, 0, 0, 0, 1, 0x68 }, try annexB(&packet, &out)); |
| 61 | +} |
| 62 | +test "PTS formatting is exact and too-small destinations are unchanged" { |
| 63 | + var out: [32]u8 = undefined; |
| 64 | + try std.testing.expectEqualStrings("0.000\n", try formatPts(0, &out)); |
| 65 | + try std.testing.expectEqualStrings("41.666\n", try formatPts(41666, &out)); |
| 66 | + try std.testing.expectEqualStrings("18446744073709551.615\n", try formatPts(std.math.maxInt(u64), &out)); |
| 67 | + @memset(&out, 0x55); |
| 68 | + try std.testing.expectError(error.BufferTooSmall, formatPts(1234, out[0..2])); |
| 69 | + for (out) |byte| try std.testing.expectEqual(@as(u8, 0x55), byte); |
| 70 | +} |
| 71 | +test "PTS sink failure poisons the writer; rejected timestamps do not call the sink" { |
| 72 | + const Test = struct { |
| 73 | + calls: usize = 0, |
| 74 | + fn write(ctx: *anyopaque, data: []const u8) !void { |
| 75 | + const s: *@This() = @ptrCast(@alignCast(ctx)); |
| 76 | + s.calls += 1; |
| 77 | + if (s.calls > 1) return error.Injected; |
| 78 | + try std.testing.expectEqualStrings("0.000\n", data); |
| 79 | + } |
| 80 | + }; |
| 81 | + var state: Test = .{}; |
| 82 | + var writer = PtsWriter{ .sink = .{ .context = &state, .write = Test.write } }; |
| 83 | + try writer.frame(0); |
| 84 | + try std.testing.expectError(error.InvalidTimestamp, writer.frame(0)); |
| 85 | + try std.testing.expectEqual(@as(usize, 1), state.calls); |
| 86 | + try std.testing.expectError(error.Injected, writer.frame(1)); |
| 87 | + try std.testing.expectError(error.SinkFailed, writer.frame(2)); |
| 88 | + try std.testing.expectEqual(@as(usize, 2), state.calls); |
| 89 | +} |