forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBf94e32dTahinli2 months ago3458 commits

raw_to_y4m.py63 lines, 2.8 KiBHistoryRawDownload
1#!/usr/bin/env python3
2"""Wrap raw planar yuv420p frames into a minimal y4m (for vpxenc).
3
4 ffmpeg ... -f rawvideo - | raw_to_y4m.py W H FPS out.y4m DEPTH
5
6DEPTH (8, 10 or 12) is REQUIRED and is the number of BYTES per sample in the
7raw stream on stdin, not the stream's bit depth: ffmpeg's rawvideo muxer
8never packs to the bit depth, it uses 1 byte for `yuv420p` and 2 bytes for
9`yuv420p10le` / `yuv420p12le`. This script has no parser and no way to learn
10it, so the depth is an ASSERTED argument: it is checked against the stdin
11length, and a mismatch is a hard error naming the depth, the byte count it
12implied, and the misframed y4m it would have produced. The old version
13computed `w*h*3//2` unconditionally, so a 10-bit stream (2 bytes per sample)
14was cut into frames at half length, the chroma planes of frame N+1 were read
15as the tail of frame N, and the last partial frame was dropped silently --
16a y4m that vpxenc accepts and encodes, with no error anywhere.
17
18The y4m header's `C` tag follows the same argument, so the wrapped stream
19carries the depth it actually has.
20
21LIMIT, stated because it cannot be fixed here: raw planar video carries NO
22depth of its own, so the length check below can only catch a stream whose
23byte count is not a whole number of frames at the claimed depth. A 10-bit
24stream of N frames is exactly 2N 8-bit frames, and no length test can tell
25those apart -- that case is caught only by the caller asserting the right
26DEPTH, which is why DEPTH is required and not defaulted.
27"""
28
29import sys
30
31
32if len(sys.argv) != 6:
33 sys.exit(
34 "usage: raw_to_y4m.py W H FPS out.y4m DEPTH "
35 "(DEPTH = 8|10|12, the BYTES per sample on stdin)"
36 )
37w, h, fps, out, depth = sys.argv[1:6]
38try:
39 depth = int(depth)
40except ValueError:
41 sys.exit(f"raw_to_y4m.py: DEPTH {depth!r} is not a number (8, 10 or 12)")
42if depth not in (8, 10, 12):
43 sys.exit(
44 f"raw_to_y4m.py: DEPTH {depth} is not 8, 10 or 12 -- the frame size below would be "
45 "wrong and every frame after the first would be misframed"
46 )
47bpp = 1 if depth == 8 else 2
48frame = int(w) * int(h) * 3 // 2 * bpp
49raw = sys.stdin.buffer.read()
50if len(raw) % frame:
51 sys.exit(
52 f"raw_to_y4m.py: stdin is {len(raw)} bytes, not a whole number of "
53 f"{int(w)}x{int(h)} 4:2:0 frames at {depth}-bit ({bpp} bytes per sample, "
54 f"{frame} bytes each): {len(raw) % frame} trailing bytes. At the other depth a "
55 f"frame would be {int(w) * int(h) * 3 // 2 * (3 - bpp)} bytes and each frame would "
56 "carry half a frame of the next one's planes. Refusing to write a misframed y4m."
57 )
58ctag = {8: "C420jpeg", 10: "C420p10", 12: "C420p12"}[depth]
59with open(out, "wb") as f:
60 f.write(f"YUV4MPEG2 W{w} H{h} F{fps}:1 Ip A1:1 {ctag}\n".encode())
61 for i in range(len(raw) // frame):
62 f.write(b"FRAME\n")
63 f.write(raw[i * frame : (i + 1) * frame])