~ft/treason

ref: c1156352ba4eceae2ec875fd06fc94c38c92271f treason/stream_ivf.c -rw-r--r-- 3.0 KiB
c1156352 — Sigrid Solveig Haflínudóttir info: mixed up dispdelay and yuv→rgb, fix it 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "stream.h"

extern Streamops ivfops;

static int
Bu16le(Biobuf *b, u16int *o)
{
	int x;

	x = Bgetc(b);
	x |= Bgetc(b)<<8;
	*o = x;
	if(x < 0)
		werrstr("failed to read 2 bytes");

	return x < 0 ? -1 : 0;
}

static int
Bu32le(Biobuf *b, u32int *o)
{
	int x, i;

	*o = 0;
	for(i = 0; i < 4; *o |= x<<(i*8), i++){
		if((x = Bgetc(b)) < 0){
			werrstr("failed to read 4 bytes");
			return -1;
		}
	}

	return 0;
}

static int
Bu64le(Biobuf *b, u64int *o)
{
	int x, i;

	*o = 0;
	for(i = 0; i < 8; *o |= x<<(i*8), i++){
		if((x = Bgetc(b)) < 0){
			werrstr("failed to read 8 bytes");
			return -1;
		}
	}

	return 0;
}

int
ivfopenb(void *bio, Stream *s, int *failed)
{
	u16int hlen, w, h, fmt;
	u32int tbdenum, tbnum;
	char tmp[6];

	if(bio == nil){
		werrstr("nil bio");
		return -1;
	}
	if(Bread(bio, tmp, 6) != 6 || Bu16le(bio, &hlen) < 0){
		werrstr("header read failed");
		Bterm(bio);
		return -1;
	}
	if(memcmp(tmp, "DKIF", 4) != 0){
		werrstr("expected DKIF, got %02x%02x%02x%02x", tmp[0], tmp[1], tmp[2], tmp[3]);
		Bterm(bio);
		return -1;
	}
	if(hlen < 0x20 || Bread(bio, tmp, 4) != 4){
		werrstr("invalid header: hlen=%d", hlen);
		goto err;
	}

	if(memcmp(tmp, "AV01", 4) == 0)
		fmt = FmtAV1;
	else if(memcmp(tmp, "VP90", 4) == 0)
		fmt = FmtVP9;
	else if(memcmp(tmp, "VP80", 4) == 0)
		fmt = FmtVP8;
	else if(memcmp(tmp, "AVC1", 4) == 0) /* FIXME that's non-standard */
		fmt = FmtH264;
	else{
		werrstr("unsupported format %.*s", 4, tmp);
		goto err;
	}

	if(Bu16le(bio, &w) < 0 || Bu16le(bio, &h) < 0 || Bu32le(bio, &tbdenum) < 0 || Bu32le(bio, &tbnum) < 0){
		werrstr("invalid header: %r");
		goto err;
	}
	if(Bseek(bio, hlen, 0) != hlen){
		werrstr("invalid IVF stream");
		goto err;
	}

	s->type = Svideo;
	s->fmt = fmt;
	s->video.w = w;
	s->video.h = h;
	s->timebase.denum = tbdenum;
	s->timebase.num = tbnum;
	s->b = bio;
	memmove(&s->ops, &ivfops, sizeof(ivfops));

	return 0;
err:
	*failed = 1;
	Bterm(bio);
	free(s);
	return -1;
}

static Stream *
ivfopen(char *path, int *num, int *failed)
{
	Stream *s;

	if((s = calloc(1, sizeof(*s))) == nil){
		*failed = 1;
		return nil;
	}

	*num = 1;
	if(ivfopenb(Bopen(path, OREAD|OCEXEC), s, failed) != 0){
		free(s);
		s = nil;
	}

	return s;
}

static int
ivfread(Stream *s, Streamframe *f)
{
	u64int timestamp;
	u32int sz;
	u8int *buf;
	int n;

	if(Bu32le(s->b, &sz) < 0 || Bu64le(s->b, &timestamp) < 0 || (int)sz < 0){
		/* eof */
		f->sz = 0;
		return 0;
	}
	buf = s->buf;
	if(s->ops.alloc != nil)
		buf = s->ops.alloc(s->ops.aux, sz);
	else if(sz > s->bufsz){
		if((buf = realloc(s->buf, sz)) == nil){
			werrstr("frame is too big: %d bytes", sz);
			goto err;
		}
		s->buf = buf;
	}
	if((n = Bread(s->b, buf, sz)) != sz){
		werrstr("short read (%d < %d)", n, sz);
		goto err;
	}
	f->buf = buf;
	f->sz = sz;
	f->timestamp = timestamp;

	return 0;
err:
	werrstr("ivf: %r");
	return -1;
}

static void
ivfclose(Stream *s)
{
	if(s->b != nil){
		Bterm(s->b);
		s->b = nil;
	}
	free(s->buf);
	s->buf = nil;
}

Streamops ivfops = {
	.open = ivfopen,
	.read = ivfread,
	.close = ivfclose,
};