forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

gen_intraonly.py73 lines, 2.7 KiBHistoryRawDownload
1#!/usr/bin/env python3
2"""Turn an ffmpeg VP9 keyframe GOP into the intra-only witness.
3
4Re-heads frame 0 (a visible keyframe) as an INTRA-ONLY inter frame
5(frame_type=1, show_frame=0, intra_only=1, reset_frame_context=2,
6refresh_frame_flags=0xff) and keeps every later frame as-is: the intra-only
7frame must reconstruct exactly like the keyframe it replaces, so the whole GOP
8decodes to the same shown frames.
9
10reset_frame_context=2 matters: `vp9_setup_past_independence` resets the
11selected stored context to the defaults, and the keyframe's compressed header
12was coded against the defaults. With reset 0/1 the frame would read whatever
13context the previous frame stored and the bool stream desyncs.
14
15usage: gen_intraonly.py <keyframe-gop.ivf> <out.ivf>
16The keyframe uncompressed header (profile 0, 8-bit, 4:2:0, error_resilient=0)
17is 8 bits + 24 sync + 3 color_space + 1 color_range => the render flag sits at
18bit 68 and the fields from bit 69 on (loop filter, quant, segmentation, tile
19info, header_size) are copied verbatim. The new prefix is 76 bits, so the
20field bits shift by 7 and the compressed header + tiles (orig[U:], with
21U = ceil(E/8)) follow the re-padded header. E was found by trying every byte
22offset until libvpx accepted the frame; the committed fixture's E is 140.
23"""
24import struct, sys
25
26src, dst = sys.argv[1], sys.argv[2]
27d = open(src, 'rb').read()
28off, frames = 32, []
29while off + 12 <= len(d):
30 sz = struct.unpack('<I', d[off:off+4])[0]
31 frames.append((d[off+4:off+12], d[off+12:off+12+sz]))
32 off += 12 + sz
33
34def bits_of(b):
35 return [(x >> i) & 1 for x in b for i in range(7, -1, -1)]
36
37K = frames[0][1]
38kb = bits_of(K)
39assert kb[5] == 0 and kb[6] == 1 and kb[7] == 0, "expected a visible, non-ER keyframe"
40rf = kb[68]
41p1 = 69 if rf == 0 else 101
42prefix = kb[0:5] + [1, 0, kb[7], 1] + [1, 0] + kb[8:32] + [1] * 8 + kb[36:52] + kb[52:68] + [rf]
43if rf:
44 prefix += kb[69:101]
45assert len(prefix) == 76
46
47E = 140 # committed fixture; any E whose (E+7)>>3 == 18 also decodes
48post = kb[p1:E]
49nb = prefix + post
50while len(nb) % 8:
51 nb.append(0)
52hdr = bytearray()
53for i in range(0, len(nb), 8):
54 v = 0
55 for j in range(8):
56 v = (v << 1) | nb[i+j]
57 hdr.append(v)
58Kp = bytes(hdr) + K[(E + 7) >> 3:]
59
60out = bytearray(b'DKIF' + b'\0' * 28)
61out[6:8] = struct.pack('<H', 32)
62out[8:12] = b'VP90'
63out[12:14] = struct.pack('<H', 320)
64out[14:16] = struct.pack('<H', 240)
65out[16:20] = struct.pack('<I', 24)
66out[20:24] = struct.pack('<I', 1)
67out[24:28] = struct.pack('<I', len(frames) + 1)
68out += struct.pack('<I', len(K)) + frames[0][0] + K
69out += struct.pack('<I', len(Kp)) + frames[0][0] + Kp
70for ts, f in frames[1:]:
71 out += struct.pack('<I', len(f)) + ts + f
72open(dst, 'wb').write(out)
73print("wrote", dst)