ReviewOS

also looking at this

home-lang/home

feat(video): add native AVC/HEVC and synchronized AAC MP4 streaming

#913
Merged chris wants to merge feat/avc-hevc-mp4 into main
14 files +1119 -0
packages/video/src/containers/mp4_stream.zigadded+571-0
Changes to packages/video/src/containers/mp4_stream.zig
@@ -0,0 +1,571 @@
1//! Home streaming fragmented MP4 with optional AAC-LC audio, adapted from mp4_muxer.zig.
2//! Complete sample description + mvex; exact sizes/offsets; no file-sized buffer.
3//! AVC/HEVC are length-prefixed, with out-of-band parameter sets (avc1/hvc1).
4const std = @import("std");
5pub const Codec = enum { h264, hevc };
6pub const Sink = struct {
7 context: *anyopaque,
8 write: *const fn (*anyopaque, []const u8) anyerror!void,
9 patch: ?*const fn (*anyopaque, u64, []const u8) anyerror!void = null,
10};
11pub const AudioConfig = struct { sample_rate: u32, channels: u32, bitrate: u32, delay: u32, config: [2]u8 };
12const AudioOffsets = struct { track: usize = 0, media: usize = 0, empty: usize = 0, edit: usize = 0 };
13
14const Buffer = struct {
15 data: [16384]u8 = undefined,
16 len: usize = 0,
17 fn bytes(s: *Buffer, b: []const u8) !void {
18 if (b.len > s.data.len - s.len) return error.HeaderTooLarge;
19 @memcpy(s.data[s.len..][0..b.len], b);
20 s.len += b.len;
21 }
22 fn zero(s: *Buffer, n: usize) !void {
23 if (n > s.data.len - s.len) return error.HeaderTooLarge;
24 @memset(s.data[s.len..][0..n], 0);
25 s.len += n;
26 }
27 fn int16(s: *Buffer, n: u16) !void {
28 var b: [2]u8 = undefined;
29 std.mem.writeInt(u16, &b, n, .big);
30 try s.bytes(&b);
31 }
32 fn int32(s: *Buffer, n: u32) !void {
33 var b: [4]u8 = undefined;
34 std.mem.writeInt(u32, &b, n, .big);
35 try s.bytes(&b);
36 }
37 fn int64(s: *Buffer, n: u64) !void {
38 var b: [8]u8 = undefined;
39 std.mem.writeInt(u64, &b, n, .big);
40 try s.bytes(&b);
41 }
42 fn box(s: *Buffer, name: *const [4]u8) !usize {
43 const at = s.len;
44 try s.int32(0);
45 try s.bytes(name);
46 return at;
47 }
48 fn end(s: *Buffer, at: usize) void {
49 std.mem.writeInt(u32, s.data[at..][0..4], @intCast(s.len - at), .big);
50 }
51 fn full(s: *Buffer, name: *const [4]u8, flags: u32) !usize {
52 const at = try s.box(name);
53 try s.int32(flags);
54 return at;
55 }
56 fn slice(s: *const Buffer) []const u8 {
57 return s.data[0..s.len];
58 }
59 fn matrix(s: *Buffer) !void {
60 for ([_]u32{ 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 }) |v| try s.int32(v);
61 }
62};
63pub const Nals = struct {
64 data: []const u8,
65 at: usize = 0,
66 pub fn next(s: *Nals) !?[]const u8 {
67 if (s.at == s.data.len) return null;
68 if (s.data.len - s.at < 4) return error.InvalidNal;
69 const n = std.mem.readInt(u32, s.data[s.at..][0..4], .big);
70 s.at += 4;
71 if (n == 0 or n > s.data.len - s.at) return error.InvalidNal;
72 const nal = s.data[s.at..][0..n];
73 s.at += n;
74 return nal;
75 }
76};
77fn config(b: *Buffer, codec: Codec, headers: []const u8) !void {
78 var nals: Nals = .{ .data = headers };
79 var sps: []const u8 = &.{};
80 var pps: []const u8 = &.{};
81 var vps: []const u8 = &.{};
82 while (try nals.next()) |nal| {
83 if (nal.len > 2048) return error.HeaderTooLarge;
84 const kind = if (codec == .h264) nal[0] & 31 else (nal[0] >> 1) & 63;
85 if (kind == (if (codec == .h264) @as(u8, 7) else 33)) sps = nal;
86 if (kind == (if (codec == .h264) @as(u8, 8) else 34)) pps = nal;
87 if (codec == .hevc and kind == 32) vps = nal;
88 }
89 if (sps.len < 4 or pps.len < 2) return error.MissingParameterSets;
90 const at = try b.box(if (codec == .h264) "avcC" else "hvcC");
91 if (codec == .h264) {
92 // The adapter fixes AVC to 8-bit 4:2:0 High profile.
93 if (sps[1] != 100) return error.UnsupportedProfile;
94 try b.bytes(&.{ 1, sps[1], sps[2], sps[3], 255, 225 });
95 try b.int16(@intCast(sps.len));
96 try b.bytes(sps);
97 try b.bytes(&.{1});
98 try b.int16(@intCast(pps.len));
99 try b.bytes(pps);
100 try b.bytes(&.{ 253, 248, 248, 0 }); // high profile: 4:2:0, 8-bit, no SPS extensions
101 } else {
102 if (vps.len < 2 or sps.len < 16) return error.MissingParameterSets;
103 // Remove emulation prevention before reading SPS profile_tier_level.
104 var rbsp: [2048]u8 = undefined;
105 var len: usize = 0;
106 var zeros: usize = 0;
107 for (sps[2..]) |byte| {
108 if (zeros >= 2 and byte == 3) {
109 zeros = 0;
110 continue;
111 }
112 rbsp[len] = byte;
113 len += 1;
114 zeros = if (byte == 0) zeros + 1 else 0;
115 }
116 if (len < 13 or (rbsp[0] & 14) != 0 or (rbsp[1] & 31) != 1) return error.UnsupportedProfile;
117 try b.bytes(&.{1});
118 try b.bytes(rbsp[1..13]);
119 try b.bytes(&.{ 240, 0, 252, 253, 248, 248, 0, 0, 15, 3 });
120 for ([_]u8{ 32, 33, 34 }, [_][]const u8{ vps, sps, pps }) |kind, nal| {
121 try b.bytes(&.{128 | kind});
122 try b.int16(1);
123 try b.int16(@intCast(nal.len));
124 try b.bytes(nal);
125 }
126 }
127 b.end(at);
128}
129fn audioTrack(b: *Buffer, cfg: AudioConfig) !AudioOffsets {
130 const rates = [_]u32{ 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000 };
131 var index: ?u16 = null;
132 for (rates, 0..) |rate, i| if (rate == cfg.sample_rate) {
133 index = @intCast(i);
134 };
135 if (index == null or cfg.sample_rate > 48000 or cfg.channels < 1 or cfg.channels > 2 or cfg.bitrate < 16000 or cfg.bitrate > 320000 or cfg.delay > 16384) return error.InvalidConfiguration;
136 const expected = (@as(u16, 2) << 11) | (index.? << 7) | (@as(u16, @intCast(cfg.channels)) << 3);
137 if (std.mem.readInt(u16, &cfg.config, .big) != expected) return error.InvalidConfiguration;
138 var offsets: AudioOffsets = .{};
139 const trak = try b.box("trak");
140 const tkhd = try b.full("tkhd", 0x1000007);
141 try b.zero(16);
142 try b.int32(2);
143 try b.zero(4);
144 offsets.track = b.len;
145 try b.int64(0);
146 try b.zero(8);
147 try b.zero(4);
148 try b.int16(0x100);
149 try b.int16(0);
150 try b.matrix();
151 try b.zero(8);
152 b.end(tkhd);
153 const edts = try b.box("edts");
154 const elst = try b.full("elst", 0x1000000);
155 try b.int32(2);
156 offsets.empty = b.len;
157 try b.int64(0);
158 try b.int64(std.math.maxInt(u64));
159 try b.int16(1);
160 try b.int16(0);
161 offsets.edit = b.len;
162 try b.int64(0);
163 try b.int64(cfg.delay);
164 try b.int16(1);
165 try b.int16(0);
166 b.end(elst);
167 b.end(edts);
168 const mdia = try b.box("mdia");
169 const mdhd = try b.full("mdhd", 0x1000000);
170 try b.zero(16);
171 try b.int32(cfg.sample_rate);
172 offsets.media = b.len;
173 try b.int64(0);
174 try b.int16(0x55c4);
175 try b.int16(0);
176 b.end(mdhd);
177 const hdlr = try b.full("hdlr", 0);
178 try b.int32(0);
179 try b.bytes("soun");
180 try b.zero(12);
181 try b.bytes("Home AAC\x00");
182 b.end(hdlr);
183 const minf = try b.box("minf");
184 const smhd = try b.full("smhd", 0);
185 try b.zero(4);
186 b.end(smhd);
187 const dinf = try b.box("dinf");
188 const dref = try b.full("dref", 0);
189 try b.int32(1);
190 const url = try b.full("url ", 1);
191 b.end(url);
192 b.end(dref);
193 b.end(dinf);
194 const stbl = try b.box("stbl");
195 const stsd = try b.full("stsd", 0);
196 try b.int32(1);
197 const entry = try b.box("mp4a");
198 try b.zero(6);
199 try b.int16(1);
200 try b.zero(8);
201 try b.int16(@intCast(cfg.channels));
202 try b.int16(16);
203 try b.zero(4);
204 try b.int32(cfg.sample_rate << 16);
205 const esds = try b.full("esds", 0);
206 try b.bytes(&.{ 3, 25 });
207 try b.int16(2);
208 try b.bytes(&.{ 0, 4, 17, 0x40, 0x15, 0, 0x20, 0 });
209 try b.int32(cfg.bitrate);
210 try b.int32(cfg.bitrate);
211 try b.bytes(&.{ 5, 2 });
212 try b.bytes(&cfg.config);
213 try b.bytes(&.{ 6, 1, 2 });
214 b.end(esds);
215 b.end(entry);
216 b.end(stsd);
217 for ([_]*const [4]u8{ "stts", "stsc", "stco" }) |name| {
218 const at = try b.full(name, 0);
219 try b.int32(0);
220 b.end(at);
221 }
222 const stsz = try b.full("stsz", 0);
223 try b.zero(8);
224 b.end(stsz);
225 // Explicit decoder pre-roll makes the edit list authoritative. Without
226 // roll sample groups Apple assumes an additional legacy 2112-sample delay.
227 const sgpd = try b.full("sgpd", 0x1000000);
228 try b.bytes("roll");
229 try b.int32(2);
230 try b.int32(1);
231 try b.int16(65535);
232 b.end(sgpd);
233 const sbgp = try b.full("sbgp", 0);
234 try b.bytes("roll");
235 try b.int32(0);
236 b.end(sbgp);
237 b.end(stbl);
238 b.end(minf);
239 b.end(mdia);
240 b.end(trak);
241 return offsets;
242}
243pub const Writer = struct {
244 sink: Sink,
245 sequence: u32 = 1,
246 last_dts: u64 = 0,
247 frames: u64 = 0,
248 failed: bool = false,
249 finished: bool = false,
250 audio_config: ?AudioConfig = null,
251 audio_packets: u64 = 0,
252 audio_pending: [8192]u8 = undefined,
253 audio_pending_len: usize = 0,
254 video_end_us: u64 = 0,
255 movie_duration: usize = 0,
256 video_duration: usize = 0,
257 video_media_duration: usize = 0,
258 audio_offsets: AudioOffsets = .{},
259 pub fn initAv(sink: Sink, width: u32, height: u32, codec: Codec, headers: []const u8, audio_cfg: AudioConfig) !Writer {
260 if (sink.patch == null) return error.InvalidConfiguration;
261 return initInternal(sink, width, height, codec, headers, audio_cfg);
262 }
263 pub fn init(sink: Sink, width: u32, height: u32, codec: Codec, headers: []const u8) !Writer {
264 return initInternal(sink, width, height, codec, headers, null);
265 }
266 fn initInternal(sink: Sink, width: u32, height: u32, codec: Codec, headers: []const u8, audio_cfg: ?AudioConfig) !Writer {
267 if (width == 0 or height == 0 or width > 8192 or height > 8192) return error.InvalidConfiguration;
268 var b: Buffer = .{};
269 const ftyp = try b.box("ftyp");
270 try b.bytes("iso6");
271 try b.int32(512);
272 try b.bytes("iso6isommp41");
273 b.end(ftyp);
274 const moov = try b.box("moov");
275 const mvhd = try b.full("mvhd", 0x1000000);
276 try b.zero(16);
277 try b.int32(1000000);
278 const movie_duration = b.len;
279 try b.int64(0);
280 try b.int32(0x10000);
281 try b.int16(0x100);
282 try b.zero(10);
283 try b.matrix();
284 try b.zero(24);
285 try b.int32(if (audio_cfg != null) 3 else 2);
286 b.end(mvhd);
287 const trak = try b.box("trak");
288 const tkhd = try b.full("tkhd", 0x1000007);
289 try b.zero(16);
290 try b.int32(1);
291 try b.zero(4);
292 const video_duration = b.len;
293 try b.int64(0);
294 try b.zero(8);
295 try b.zero(8);
296 try b.matrix();
297 try b.int32(width << 16);
298 try b.int32(height << 16);
299 b.end(tkhd);
300 const mdia = try b.box("mdia");
301 const mdhd = try b.full("mdhd", 0x1000000);
302 try b.zero(16);
303 try b.int32(1000000);
304 const video_media_duration = b.len;
305 try b.int64(0);
306 try b.int16(0x55c4);
307 try b.int16(0);
308 b.end(mdhd);
309 const hdlr = try b.full("hdlr", 0);
310 try b.int32(0);
311 try b.bytes("vide");
312 try b.zero(12);
313 try b.bytes("Home Video\x00");
314 b.end(hdlr);
315 const minf = try b.box("minf");
316 const vmhd = try b.full("vmhd", 1);
317 try b.zero(8);
318 b.end(vmhd);
319 const dinf = try b.box("dinf");
320 const dref = try b.full("dref", 0);
321 try b.int32(1);
322 const url = try b.full("url ", 1);
323 b.end(url);
324 b.end(dref);
325 b.end(dinf);
326 const stbl = try b.box("stbl");
327 const stsd = try b.full("stsd", 0);
328 try b.int32(1);
329 const entry = try b.box(if (codec == .h264) "avc1" else "hvc1");
330 try b.zero(6);
331 try b.int16(1);
332 try b.zero(16);
333 try b.int16(@intCast(width));
334 try b.int16(@intCast(height));
335 try b.int32(0x480000);
336 try b.int32(0x480000);
337 try b.int32(0);
338 try b.int16(1);
339 try b.zero(32);
340 try b.int16(24);
341 try b.int16(65535);
342 try config(&b, codec, headers);
343 const colr = try b.box("colr");
344 try b.bytes("nclx");
345 try b.int16(1);
346 try b.int16(13);
347 try b.int16(6);
348 try b.bytes(&.{0});
349 b.end(colr);
350 b.end(entry);
351 b.end(stsd);
352 for ([_]*const [4]u8{ "stts", "stsc", "stco" }) |name| {
353 const at = try b.full(name, 0);
354 try b.int32(0);
355 b.end(at);
356 }
357 const stsz = try b.full("stsz", 0);
358 try b.zero(8);
359 b.end(stsz);
360 b.end(stbl);
361 b.end(minf);
362 b.end(mdia);
363 b.end(trak);
364 const offsets = if (audio_cfg) |cfg| try audioTrack(&b, cfg) else AudioOffsets{};
365 const mvex = try b.box("mvex");
366 const trex = try b.full("trex", 0);
367 try b.int32(1);
368 try b.int32(1);
369 try b.zero(12);
370 b.end(trex);
371 if (audio_cfg != null) {
372 const audio_trex = try b.full("trex", 0);
373 try b.int32(2);
374 try b.int32(1);
375 try b.int32(1024);
376 try b.int32(0);
377 try b.int32(0x02000000);
378 b.end(audio_trex);
379 }
380 b.end(mvex);
381 b.end(moov);
382 try sink.write(sink.context, b.slice());
383 return .{ .sink = sink, .audio_config = audio_cfg, .audio_offsets = offsets, .movie_duration = movie_duration, .video_duration = video_duration, .video_media_duration = video_media_duration };
384 }
385 /// One complete sample. Caller supplies its actual duration, using the next
386 /// capture time; the last sample uses nominal frame duration. PTS == DTS.
387 pub fn frame(s: *Writer, sample: []const u8, dts_us: u64, duration_us: u32, key: bool) !void {
388 if (s.failed or s.finished) return error.WriterFailed;
389 if (sample.len == 0 or sample.len > std.math.maxInt(u32) - 8 or duration_us == 0 or (s.frames != 0 and dts_us <= s.last_dts) or s.sequence == std.math.maxInt(u32)) return error.InvalidSample;
390 var nals: Nals = .{ .data = sample };
391 while (try nals.next()) |_| {}
392 const end = try std.math.add(u64, dts_us, duration_us);
393 try s.fragment(1, sample, dts_us, duration_us, key);
394 s.frames += 1;
395 s.last_dts = dts_us;
396 s.video_end_us = end;
397 }
398 pub fn audio(s: *Writer, sample: []const u8) !void {
399 if (s.audio_config == null or s.failed or s.finished) return error.WriterFailed;
400 if (sample.len == 0 or sample.len > 8192 or s.sequence == std.math.maxInt(u32)) return error.InvalidSample;
401 if (s.audio_pending_len != 0) try s.flushAudio(1024);
402 @memcpy(s.audio_pending[0..sample.len], sample);
403 s.audio_pending_len = sample.len;
404 }
405 fn flushAudio(s: *Writer, duration: u32) !void {
406 if (s.audio_pending_len == 0) return;
407 const time = try std.math.mul(u64, s.audio_packets, 1024);
408 try s.fragment(2, s.audio_pending[0..s.audio_pending_len], time, duration, true);
409 s.audio_pending_len = 0;
410 s.audio_packets += 1;
411 }
412 /// Patch fixed metadata only after draining AAC. Decoder delay and tail
413 /// padding stay in media samples; edit lists expose exactly the input PCM.
414 pub fn finish(s: *Writer, audio_start_us: u64, presented_frames: u64) !void {
415 if (s.failed or s.finished) return error.WriterFailed;
416 const cfg = s.audio_config orelse return error.InvalidConfiguration;
417 const encoded = try std.math.mul(u64, s.audio_packets + @intFromBool(s.audio_pending_len != 0), 1024);
418 const valid_media = try std.math.add(u64, presented_frames, cfg.delay);
419 if (encoded < valid_media or encoded - valid_media >= 1024) return error.InvalidSample;
420 const padding = encoded - valid_media;
421 const scaled = @as(u128, presented_frames) * 1000000 / cfg.sample_rate;
422 if (scaled > std.math.maxInt(u64)) return error.InvalidSample;
423 const duration: u64 = @intCast(scaled);
424 const audio_end = try std.math.add(u64, audio_start_us, duration);
425 errdefer s.failed = true;
426 // A short final duration also signals tail trim to fragmented readers
427 // that do not apply the finite edit duration to the last fragment.
428 try s.flushAudio(@intCast(1024 - padding));
429 try s.patch64(s.movie_duration, @max(s.video_end_us, audio_end));
430 try s.patch64(s.video_duration, s.video_end_us);
431 try s.patch64(s.video_media_duration, s.video_end_us);
432 try s.patch64(s.audio_offsets.track, audio_end);
433 try s.patch64(s.audio_offsets.media, valid_media);
434 try s.patch64(s.audio_offsets.empty, audio_start_us);
435 try s.patch64(s.audio_offsets.edit, duration);
436 s.finished = true;
437 }
438 fn patch64(s: *Writer, offset: usize, value: u64) !void {
439 var bytes: [8]u8 = undefined;
440 std.mem.writeInt(u64, &bytes, value, .big);
441 try s.sink.patch.?(s.sink.context, offset, &bytes);
442 }
443 fn fragment(s: *Writer, track: u32, sample: []const u8, time: u64, duration: u32, key: bool) !void {
444 if (s.failed or s.finished) return error.WriterFailed;
445 if (s.sequence == std.math.maxInt(u32)) return error.InvalidSample;
446 var b: Buffer = .{};
447 const moof = try b.box("moof");
448 const mfhd = try b.full("mfhd", 0);
449 try b.int32(s.sequence);
450 b.end(mfhd);
451 const traf = try b.box("traf");
452 const tfhd = try b.full("tfhd", 0x20000);
453 try b.int32(track);
454 b.end(tfhd);
455 const tfdt = try b.full("tfdt", 0x1000000);
456 try b.int64(time);
457 b.end(tfdt);
458 const trun = try b.full("trun", 0x701);
459 try b.int32(1);
460 const offset = b.len;
461 try b.int32(0);
462 try b.int32(duration);
463 try b.int32(@intCast(sample.len));
464 try b.int32(if (key) 0x02000000 else 0x01010000);
465 b.end(trun);
466 if (track == 2) {
467 const sbgp = try b.full("sbgp", 0);
468 try b.bytes("roll");
469 try b.int32(1);
470 try b.int32(1);
471 try b.int32(1);
472 b.end(sbgp);
473 }
474 b.end(traf);
475 b.end(moof);
476 std.mem.writeInt(u32, b.data[offset..][0..4], @intCast(b.len + 8), .big);
477 try b.int32(@intCast(sample.len + 8));
478 try b.bytes("mdat");
479 errdefer s.failed = true;
480 try s.sink.write(s.sink.context, b.slice());
481 try s.sink.write(s.sink.context, sample);
482 s.sequence += 1;
483 }
484};
485test "NAL lengths reject truncation and empty units" {
486 var n: Nals = .{ .data = &.{ 0, 0, 0, 2, 0x65 } };
487 try std.testing.expectError(error.InvalidNal, n.next());
488 n = .{ .data = &.{ 0, 0, 0, 0 } };
489 try std.testing.expectError(error.InvalidNal, n.next());
490}
491test "fragment sizes, offsets and 64-bit time are exact; sink failure poisons writer" {
492 const Out = struct {
493 data: std.ArrayList(u8) = .empty,
494 fail: bool = false,
495 fn write(ctx: *anyopaque, bytes: []const u8) anyerror!void {
496 const s: *@This() = @ptrCast(@alignCast(ctx));
497 if (s.fail) return error.DiskFull;
498 try s.data.appendSlice(std.testing.allocator, bytes);
499 }
500 };
501 var out: Out = .{};
502 defer out.data.deinit(std.testing.allocator);
503 var writer: Writer = .{ .sink = .{ .context = &out, .write = Out.write } };
504 try writer.frame(&.{ 0, 0, 0, 2, 0x65, 0x80 }, 0x100000000, 41667, true);
505 const bytes = out.data.items;
506 const size = std.mem.readInt(u32, bytes[0..4], .big);
507 try std.testing.expectEqualStrings("moof", bytes[4..8]);
508 try std.testing.expectEqual(@as(u32, 114), std.mem.readInt(u32, bytes[size..][0..4], .big) + size);
509 try std.testing.expectEqual(@as(u64, 0x100000000), std.mem.readInt(u64, bytes[60..68], .big));
510 try std.testing.expectEqual(size + 8, std.mem.readInt(u32, bytes[84..88], .big));
511 try std.testing.expectError(error.InvalidSample, writer.frame(&.{ 0, 0, 0, 1, 1 }, 0x100000000, 1, false));
512 out.fail = true;
513 try std.testing.expectError(error.DiskFull, writer.frame(&.{ 0, 0, 0, 1, 1 }, 0x100000001, 1, false));
514 try std.testing.expectError(error.WriterFailed, writer.frame(&.{ 0, 0, 0, 1, 1 }, 0x100000002, 1, false));
515}
516
517/// CodecPrivate for Matroska, without the enclosing avcC/hvcC box header.
518pub fn codecPrivate(codec: Codec, headers: []const u8, destination: []u8) ![]const u8 {
519 var b: Buffer = .{};
520 try config(&b, codec, headers);
521 const data = b.slice()[8..];
522 if (destination.len < data.len) return error.HeaderTooLarge;
523 @memcpy(destination[0..data.len], data);
524 return destination[0..data.len];
525}
526test "Matroska CodecPrivate reuses checked config and respects destination bounds" {
527 const headers = [_]u8{ 0, 0, 0, 4, 0x67, 100, 0, 40, 0, 0, 0, 2, 0x68, 0x80 };
528 var destination: [128]u8 = undefined;
529 const data = try codecPrivate(.h264, &headers, &destination);
530 try std.testing.expectEqualSlices(u8, &.{ 1, 100, 0, 40, 255, 225 }, data[0..6]);
531 try std.testing.expectError(error.HeaderTooLarge, codecPrivate(.h264, &headers, destination[0..1]));
532 try std.testing.expectError(error.MissingParameterSets, codecPrivate(.h264, &.{}, &destination));
533}
534test "AAC track sample clock, edit delay/tail, fixed patches and sink failure" {
535 const Out = struct {
536 bytes: std.ArrayList(u8) = .empty,
537 fail: bool = false,
538 fn write(ctx: *anyopaque, data: []const u8) anyerror!void {
539 const s: *@This() = @ptrCast(@alignCast(ctx));
540 try s.bytes.appendSlice(std.testing.allocator, data);
541 }
542 fn patch(ctx: *anyopaque, offset: u64, data: []const u8) anyerror!void {
543 const s: *@This() = @ptrCast(@alignCast(ctx));
544 if (s.fail) return error.DiskFull;
545 if (offset + data.len > s.bytes.items.len) return error.InvalidOffset;
546 @memcpy(s.bytes.items[@intCast(offset)..][0..data.len], data);
547 }
548 };
549 var out: Out = .{};
550 defer out.bytes.deinit(std.testing.allocator);
551 const headers = [_]u8{ 0, 0, 0, 4, 0x67, 100, 0, 40, 0, 0, 0, 2, 0x68, 0x80 };
552 const cfg: AudioConfig = .{ .sample_rate = 48000, .channels = 2, .bitrate = 128000, .delay = 2048, .config = .{ 0x11, 0x90 } };
553 const sink: Sink = .{ .context = &out, .write = Out.write, .patch = Out.patch };
554 var writer = try Writer.initAv(sink, 128, 96, .h264, &headers, cfg);
555 try std.testing.expect(std.mem.indexOf(u8, out.bytes.items, "mp4a") != null);
556 try writer.frame(&.{ 0, 0, 0, 2, 0x65, 0x80 }, 0, 41667, true);
557 try std.testing.expectError(error.InvalidSample, writer.finish(12345, 1000));
558 for (0..3) |_| try writer.audio(&.{ 0x21, 0x10 });
559 try writer.finish(12345, 1000);
560 try std.testing.expectEqual(@as(u64, 12345), std.mem.readInt(u64, out.bytes.items[writer.audio_offsets.empty..][0..8], .big));
561 try std.testing.expectEqual(@as(u64, 20833), std.mem.readInt(u64, out.bytes.items[writer.audio_offsets.edit..][0..8], .big));
562 try std.testing.expectEqual(@as(u64, 2048), std.mem.readInt(u64, out.bytes.items[writer.audio_offsets.edit + 8 ..][0..8], .big));
563 try std.testing.expectEqual(@as(u64, 3048), std.mem.readInt(u64, out.bytes.items[writer.audio_offsets.media..][0..8], .big));
564 try std.testing.expectError(error.WriterFailed, writer.audio(&.{1}));
565 var failure = try Writer.initAv(sink, 128, 96, .h264, &headers, cfg);
566 try failure.audio(&.{ 0x21, 0x10 });
567 try failure.audio(&.{ 0x21, 0x10 });
568 out.fail = true;
569 try std.testing.expectError(error.DiskFull, failure.finish(0, 0));
570 try std.testing.expectError(error.WriterFailed, failure.audio(&.{1}));
571}