summaryrefslogtreecommitdiffstats
path: root/Demo/sgi/video/statit.py
blob: 360e49e8738e638f450b123a1e2bbacddd3a966c (plain)
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
import sys
from time import millitimer

def main():
	filename = 'film2.video'
	if sys.argv[1:]: filename = sys.argv[1]
	f = open(filename, 'r')

	line = f.readline()
	w, h = eval(line[:-1])
	w2, h2 = w/2, h/2
	size = w2 * h2

	data = data2 = t = t0 = t1 = None
	nframes = 0
	t0 = millitimer()
	while 1:
		line = f.readline()
		if not line: break
		t = eval(line[:-1])
		data = None
		data = f.read(size)
		if len(data) <> size:
			raise EOFError
		dostat(w2, h2, data)
		nframes = nframes+1
	t1 = millitimer()
	
	t = 0.001 * (t1-t0)
	fps = 0.1 * int(10*nframes/t)
	print nframes, 'frames in', t, 'sec. =', fps, 'frames/sec.'

def dostat(w, h, data):
	print
	stat3(w, h, data)

# Statistic op 1: frequencies of byte values
def stat1(w, h, data):
	bins = [0]*256
	for c in data:
		i = ord(c)
		bins[i] = bins[i]+1
	prbins(bins)

def prbins(bins):
	import string
	s = ''
	tot = 0
	for i in range(256):
		tot = tot + bins[i]
		s = s + string.rjust(`bins[i]`, 4)
		if len(s) >= 4*16:
			print s, string.rjust(`tot`, 7)
			s = ''
			tot = 0

# Statistic op 2: run lengths
def stat2(w, h, data):
	runs = []
	for y in range(h):
		count, value = 0, ord(data[y*w])
		for c in data[y*w : y*w+w]:
			i = ord(c)
			if i <> value:
				runs.append(count, value)
				count, value = 0, i
			count = count+1
		runs.append(count, value)
	print len(runs), 'runs =', 0.1 * (10*w*h/len(runs)), 'bytes/run'

# Statistic op 3: frequencies of byte differences
def stat3(w, h, data):
	bins = [0]*256
	prev = 0
	for c in data:
		i = ord(c)
		delta = divmod(i-prev, 256)[1]
		prev = i
		bins[delta] = bins[delta]+1
	prbins(bins)

# Try packing
def packblock(w, h, data):
	res = ''
	for y in range(h):
		res = res + packline(data[y*w : y*w+w])
	return res

def packline(line):
	bytes = []
	for c in line:
		bytes.append(ord(c))
	prev = bytes[0]
	i, n = 1, len(bytes)
	while i < n:
		for pack in (0, 2, 4, 8):
			if pack = 0:
				lo, hi = 0, 0
			else:
				hi = pow(2, pack-1)-1
				lo = -hi-1
			p = prev
			j = i
			count = 0
			while j < n:
				x = bytes[j]
				delta = byte(x-p)
				if not lo <= delta <= hi:
					break
				p = x
				j = j+1

def byte(x): return divmod(x, 256)[1]

main()