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
|
import gl, GL
import VFile
import os
class InputViewer:
def __init__(self, filename, title, *args):
try:
self.vin = VFile.VinFile(filename)
except (EOFError, VFile.Error):
raise IOError, 'bad video input file'
self.vin.warmcache()
if not title:
title = os.path.split(filename)[1]
self.filename = filename
self.title = title
self.qsize = len(self.vin.index)
gl.foreground()
gl.prefsize(self.vin.width, self.vin.height)
self.wid = -1
self.reset()
def close(self):
self.vin.close()
if self.wid > 0:
gl.winclose(self.wid)
def rewind(self):
self.vin.rewind()
self.reset()
def getinfo(self):
return self.vin.getinfo()
# Internal
def reset(self):
if self.wid > 0:
gl.winset(self.wid)
gl.clear()
self.vin.initcolormap()
self.qindex = 0
def show(self):
if self.wid < 0:
gl.foreground()
gl.prefsize(self.vin.width, self.vin.height)
self.wid = gl.winopen(self.title)
gl.clear()
self.vin.initcolormap()
gl.winset(self.wid)
if self.qindex >= self.qsize:
self.vin.clear()
return
dt, d, cd = self.vin.getrandomframe(self.qindex)
self.vin.showframe(d, cd)
def redraw(self, wid):
if wid == self.wid >= 0:
gl.winset(self.wid)
gl.reshapeviewport()
self.vin.clear()
self.show()
def get(self):
if self.qindex >= self.qsize:
return None
if self.qindex > 0:
prevt, ds, cs = \
self.vin.getrandomframeheader(self.qindex-1)
else:
prevt = 0
t, data, cdata = self.vin.getrandomframe(self.qindex)
self.qindex = self.qindex + 1
return t-prevt, data, cdata
def backup(self):
if self.qindex == 0:
return 0
self.qindex = self.qindex - 1
return 1
def seek(self, i):
if not 0 <= i <= self.qsize:
return 0
self.qindex = i
return 1
def tell(self):
return self.qindex
def qsizes(self):
return self.qindex, self.qsize - self.qindex
def qinfo(self):
return 0, self.qindex, self.qsize
class OutputViewer:
def __init__(self, filename, title, qsize):
try:
self.vout = VFile.VoutFile(filename)
except (EOFError, VFile.Error):
raise IOError, 'bad video output file'
if not title:
title = os.path.split(filename)[1]
self.filename = filename
self.title = title
self.qsize = qsize
gl.foreground()
self.wid = -1
self.reset()
def close(self):
while self.queue:
self.flushq()
self.vout.close()
if self.wid > 0:
gl.winclose(self.wid)
def rewind(self):
info = self.vout.getinfo()
self.vout.close()
self.vout = VFile.VoutFile(self.filename)
self.vout.setinfo(info)
self.reset()
def getinfo(self):
return self.vout.getinfo()
def setinfo(self, info):
if info == self.getinfo(): return # No change
self.vout.setinfo(info)
if self.wid > 0:
gl.winclose(self.wid)
self.wid = -1
# Internal
def reset(self):
if self.wid > 0:
gl.winset(self.wid)
gl.clear()
self.vout.initcolormap()
self.queue = []
self.spares = []
self.written = 0
self.lastt = 0
# Internal
def flushq(self):
if self.written == 0:
self.vout.writeheader()
dt, d, cd = self.queue[0]
self.lastt = self.lastt + dt
self.vout.writeframe(self.lastt, d, cd)
del self.queue[0]
self.written = self.written + 1
def show(self):
if self.wid < 0:
gl.foreground()
gl.prefsize(self.vout.width, self.vout.height)
self.wid = gl.winopen(self.title)
gl.clear()
self.vout.initcolormap()
gl.winset(self.wid)
if not self.queue:
self.vout.clear()
return
dt, d, cd = self.queue[-1]
self.vout.showframe(d, cd)
def redraw(self, wid):
if wid == self.wid >= 0:
gl.winset(self.wid)
gl.reshapeviewport()
self.vout.clear()
self.show()
def backup(self):
if len(self.queue) < 1: return 0
self.spares.insert(0, self.queue[-1])
del self.queue[-1]
return 1
def forward(self):
if not self.spares: return 0
self.queue.append(self.spares[0])
del self.spares[0]
return 1
def put(self, item):
self.queue.append(item)
self.spares = []
while len(self.queue) > self.qsize:
self.flushq()
def seek(self, i):
i = i - self.written
if not 0 <= i <= len(self.queue) + len(self.spares):
return 0
while i < len(self.queue):
if not self.backup():
return 0
while i > len(self.queue):
if not self.forward():
return 0
return 1
def trunc(self):
del self.spares[:]
def tell(self):
return self.written + len(self.queue)
def qsizes(self):
return len(self.queue), len(self.spares)
def qinfo(self):
first = self.written
pos = first + len(self.queue)
last = pos + len(self.spares)
return first, pos, last
def test():
import sys
a = InputViewer(sys.argv[1], '')
b = OutputViewer(sys.argv[2], '')
b.setinfo(a.getinfo())
while 1:
a.show()
data = a.get()
if data is None:
break
b.put(data)
b.show()
while a.backup():
data = a.get()
b.put(data)
b.show()
if a.backup(): a.show()
while 1:
data = a.get()
if data is None:
break
b.put(data)
b.show()
a.show()
b.close()
|