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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
implement WmAVI;
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
Rect, Display, Image: import draw;
include "tk.m";
tk: Tk;
Toplevel: import tk;
include "tkclient.m";
tkclient: Tkclient;
ctxt: ref Draw->Context;
include "selectfile.m";
selectfile: Selectfile;
include "dialog.m";
dialog: Dialog;
include "riff.m";
avi: Riff;
AVIhdr, AVIstream, RD: import avi;
video: ref AVIstream;
WmAVI: module
{
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
Stopped, Playing, Paused: con iota;
state := Stopped;
cmap: array of byte;
codedbuf: array of byte;
pixelbuf: array of byte;
pixelrec: Draw->Rect;
task_cfg := array[] of {
"canvas .c",
"frame .b",
"button .b.File -text File -command {send cmd file}",
"button .b.Stop -text Stop -command {send cmd stop}",
"button .b.Pause -text Pause -command {send cmd pause}",
"button .b.Play -text Play -command {send cmd play}",
"frame .f",
"label .f.file -text {File:}",
"label .f.name",
"pack .f.file .f.name -side left",
"pack .b.File .b.Stop .b.Pause .b.Play -side left",
"pack .f -fill x",
"pack .b -anchor w",
"pack .c -side bottom -fill both -expand 1",
"pack propagate . 0",
};
init(xctxt: ref Draw->Context, nil: list of string)
{
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
tk = load Tk Tk->PATH;
tkclient= load Tkclient Tkclient->PATH;
dialog = load Dialog Dialog->PATH;
selectfile = load Selectfile Selectfile->PATH;
ctxt = xctxt;
sys->pctl(Sys->NEWPGRP, nil);
tkclient->init();
dialog->init();
selectfile->init();
(t, wmctl) := tkclient->toplevel(ctxt, "", "AVI Player", 0);
cmd := chan of string;
tk->namechan(t, cmd, "cmd");
for (c:=0; c<len task_cfg; c++)
tk->cmd(t, task_cfg[c]);
tk->cmd(t, "bind . <Configure> {send cmd resize}");
tk->cmd(t, "update");
tkclient->onscreen(t, nil);
tkclient->startinput(t, "kbd"::"ptr"::nil);
avi = load Riff Riff->PATH;
if(avi == nil) {
dialog->prompt(ctxt, t.image, "error -fg red", "Loading Interfaces",
"Failed to load the RIFF/AVI\ninterface:"+sys->sprint("%r"),
0, "Exit"::nil);
return;
}
avi->init();
fname := "";
state = Stopped;
for(;;) alt {
s := <-t.ctxt.kbd =>
tk->keyboard(t, s);
s := <-t.ctxt.ptr =>
tk->pointer(t, *s);
s := <-t.ctxt.ctl or
s = <-t.wreq or
s = <-wmctl =>
if(s == "exit") {
state = Stopped;
return;
}
tkclient->wmctl(t, s);
press := <-cmd =>
case press {
"file" =>
state = Stopped;
patterns := list of {
"*.avi (Microsoft movie files)",
"* (All Files)"
};
fname = selectfile->filename(ctxt, t.image, "Locate AVI files",
patterns, nil);
if(fname != nil) {
tk->cmd(t, ".f.name configure -text {"+fname+"}");
tk->cmd(t, "update");
}
"play" =>
if (state != Stopped) {
state = Playing;
continue;
}
if(fname != nil) {
state = Playing;
spawn play(t, fname);
}
"pause" =>
if(state == Playing)
state = Paused;
"stop" =>
state = Stopped;
}
}
}
play(t: ref Toplevel, file: string)
{
sp := list of { "Stop Play" };
(r, err) := avi->open(file);
if(err != nil) {
dialog->prompt(ctxt, t.image, "error -fg red", "Open AVI file", err, 0, sp);
return;
}
err = avi->r.check4("AVI ");
if(err != nil) {
dialog->prompt(ctxt, t.image, "error -fg red", "Read AVI format", err, 0, sp);
return;
}
(code, l) := avi->r.gethdr();
if(code != "LIST") {
dialog->prompt(ctxt, t.image, "error -fg red", "Parse AVI headers",
"no list under AVI section header", 0, sp);
return;
}
err = avi->r.check4("hdrl");
if(err != nil) {
dialog->prompt(ctxt, t.image, "error -fg red", "Read AVI header", err, 0, sp);
return;
}
avihdr: ref AVIhdr;
(avihdr, err) = avi->r.avihdr();
if(err != nil) {
dialog->prompt(ctxt, t.image, "error -fg red", "Read AVI header", err, 0, sp);
return;
}
#
# read the stream info & format structures
#
stream := array[avihdr.streams] of ref AVIstream;
for(i := 0; i < avihdr.streams; i++) {
(stream[i], err) = avi->r.streaminfo();
if(err != nil) {
dialog->prompt(ctxt, t.image, "error -fg red", "Parse AVI headers",
"Failed to parse stream headers\n"+err, 0, sp);
return;
}
if(stream[i].stype == "vids") {
video = stream[i];
err = video.fmt2binfo();
if(err != nil) {
dialog->prompt(ctxt, t.image, "error -fg red",
"Parse AVI Video format",
"Invalid stream headers\n"+err, 0, sp);
return;
}
}
}
img: ref Draw->Image;
if(video != nil) {
case video.binfo.compression {
* =>
dialog->prompt(ctxt, t.image, "error -fg red",
"Parse AVI Compression method",
"unknown compression/encoding method", 0, sp);
return;
avi->BI_RLE8 =>
cmap = array[len video.binfo.cmap] of byte;
for(i = 0; i < len video.binfo.cmap; i++) {
e := video.binfo.cmap[i];
cmap[i] = byte ctxt.display.rgb2cmap(e.r, e.g, e.b);
}
break;
}
chans: draw->Chans;
case video.binfo.bitcount {
* =>
dialog->prompt(ctxt, t.image, "error -fg red",
"Check AVI Video format",
string video.binfo.bitcount+
" bits per pixel not supported", 0, sp);
return;
8 =>
chans = Draw->CMAP8;
mem := video.binfo.width*video.binfo.height;
pixelbuf = array[mem] of byte;
};
pixelrec.min = (0, 0);
pixelrec.max = (video.binfo.width, video.binfo.height);
img = ctxt.display.newimage(pixelrec, chans, 0, Draw->White);
if (img == nil) {
sys->fprint(sys->fildes(2), "coffee: failed to allocate image\n");
exit;
}
}
#
# Parse out the junk headers we don't understand
#
parse: for(;;) {
(code, l) = avi->r.gethdr();
if(l < 0)
break;
case code {
* =>
# sys->print("%s %d\n", code, l);
avi->r.skip(l);
"LIST" =>
err = avi->r.check4("movi");
if(err != nil) {
dialog->prompt(ctxt, t.image, "error -fg red",
"Strip AVI headers",
"no movi chunk", 0, sp);
return;
}
break parse;
}
}
canvr := canvsize(t);
p := (Draw->Point)(0, 0);
dx := canvr.dx();
if(dx > video.binfo.width)
p.x = (dx - video.binfo.width)/2;
dy := canvr.dy();
if(dy > video.binfo.height)
p.y = (dy - video.binfo.height)/2;
canvr = canvr.addpt(p);
chunk: for(;;) {
while(state == Paused)
sys->sleep(0);
if(state == Stopped)
break chunk;
(code, l) = avi->r.gethdr();
if(l <= 0)
break;
if(l & 1)
l++;
case code {
* =>
avi->r.skip(l);
"00db" => # Stream 0 Video DIB
dib(r, img, l);
"00dc" => # Stream 0 Video DIB compressed
dibc(r, img, l);
t.image.draw(canvr, img, nil, img.r.min);
"idx1" =>
break chunk;
}
}
state = Stopped;
}
dib(r: ref RD, i: ref Draw->Image, l: int): int
{
if(len codedbuf < l)
codedbuf = array[l] of byte;
if(r.readn(codedbuf, l) != l)
return -1;
case video.binfo.bitcount {
8 =>
for(k := 0; k < l; k++)
codedbuf[k] = cmap[int codedbuf[k]];
i.writepixels(pixelrec, codedbuf);
}
return 0;
}
dibc(r: ref RD, i: ref Draw->Image, l: int): int
{
if(len codedbuf < l)
codedbuf = array[l] of byte;
if(r.readn(codedbuf, l) != l)
return -1;
case video.binfo.compression {
avi->BI_RLE8 =>
p := 0;
posn := 0;
x := 0;
y := video.binfo.height-1;
w := video.binfo.width;
decomp: while(p < l) {
n := int codedbuf[p++];
if(n == 0) {
esc := int codedbuf[p++];
case esc {
0 => # end of line
x = 0;
y--;
1 => # end of image
break decomp;
2 => # Delta dx,dy
x += int codedbuf[p++];
y -= int codedbuf[p++];
* =>
posn = x+y*w;
for(k := 0; k < esc; k++)
pixelbuf[posn++] = cmap[int codedbuf[p++]];
x += esc;
if(p & 1)
p++;
};
}
else {
posn = x+y*w;
v := cmap[int codedbuf[p++]];
for(k := 0; k < n; k++)
pixelbuf[posn++] = v;
x += n;
}
}
i.writepixels(pixelrec, pixelbuf);
}
return 0;
}
canvsize(t: ref Toplevel): Rect
{
r: Rect;
r.min.x = int tk->cmd(t, ".c cget -actx");
r.min.y = int tk->cmd(t, ".c cget -acty");
r.max.x = r.min.x + int tk->cmd(t, ".c cget -width");
r.max.y = r.min.y + int tk->cmd(t, ".c cget -height");
return r;
}
|