-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtracestats.py
executable file
·207 lines (190 loc) · 5.48 KB
/
tracestats.py
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
#!/usr/bin/env python
#
# Copyright 2009 Claudio Pisa (claudio dot pisa at uniroma2 dot it)
#
# This file is part of SVEF (SVC Streaming Evaluation Framework).
#
# SVEF is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SVEF is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SVEF. If not, see <http://www.gnu.org/licenses/>.
#
import sys
import os
from nalulib import *
if(len(sys.argv) < 2):
print """
Compute statistics for the given trace file, excluding control NALUs.
Usage: %s <trace file> [<fps>]
""" % (os.path.basename(sys.argv[0]))
sys.exit(1)
class GraphMatrix():
def __init__(self, gmdict = None):
if gmdict == None:
self.data = {}
else:
self.data = gmdict
def set(self,i,j,value):
self.data.update({(i,j): value})
def get(self,i,j):
return self.data[(i,j)]
def getmax(self):
"return (maxi, maxj), i.e. the biggest indexes"
ilist = []
jlist = []
for i,j in self.data.keys():
ilist.append(i)
jlist.append(j)
return (max(ilist), max(jlist))
class SuperGraph():
def __init__(self, graphmatrix=None, filterfunc=lambda x: x):
if graphmatrix == None:
self.graphmatrix = GraphMatrix()
else:
self.graphmatrix = graphmatrix
self.filterfunc=filterfunc
def __str__(self):
maxtid,maxqid = self.graphmatrix.getmax()
ret = ""
ret += "\n"
ret += "QID ^ \n"
ret += " | \n"
for qid in range(maxqid,-1,-1):
ret += " %2d | " % qid
for tid in range(maxtid,-1,-1):
try:
ret += " %7s " % self.filterfunc(self.graphmatrix.get(maxtid-tid,qid))
except KeyError:
ret += " "*9
ret += "\n"
ret += " +-"
ret += "-" * (maxtid+1)*9
ret += "----->\n"
ret += " "
for tid in range(maxtid+1):
ret += " %7d " % tid
ret += " TID\n\n\n"
return ret
def __repr__(self):
return self.__str__()
class TraceStats():
def __init__(self):
self.naludict = dict()
self.bytesdict = dict()
self.framecounter = 0
self.gopsizes=[]
self.ctrlcounter=1
self.maxnalulen=0
def __str__(self):
ret = ""
gm = GraphMatrix(self.naludict)
sg = SuperGraph(gm)
ret += str(sg)
gm = GraphMatrix(self.bytesdict)
sg = SuperGraph(gm, filterfunc=lambda x:"%.2f" % (x*8.0/10**6))
ret += str(sg)
keyz = self.naludict.keys()[:]
def laycmp(x, y):
xi, xj = x
yi, yj = y
revx = (xj, xi)
revy = (yj, yi)
if revx < revy:
return -1
elif revx == revy:
return 0
else:
return 1
keyz.sort(cmp=laycmp)
nalutot=0
bytestot=0
for key in keyz:
ret += "layer %s: %d NALUs\t(%d bytes = %d Kbytes = %.2f Mbits)\tavg len: %.2f bytes\n" \
% (key, self.naludict[key], self.bytesdict[key], \
self.bytesdict[key]/1024, self.bytesdict[key]*8.0/10**6, \
1.0*self.bytesdict[key]/self.naludict[key])
nalutot += self.naludict[key]
bytestot += self.bytesdict[key]
ret += "total: %d NALUs \t(%d bytes = %d Kbytes = %.2f Mbits)\tavg len: %.2f bytes\tmax NALU len: %d bytes\n" \
% (nalutot, bytestot, bytestot/1024, bytestot*8.0/10**6, \
1.0*bytestot/nalutot, self.maxnalulen)
ret += "\n"
ret += "Total number of control NALUs (i.e. guessed total number of frames): %d\n" % self.framecounter
ret += "GOP size (guessed from control NALUs): %.2f\n" % self.getGopSize()
if fps != None:
ret += "Total bitrate (guessed): %.2f bits per second\n" % (bytestot*8.0*fps/self.framecounter)
return ret
def __repr__(self):
return self.__str__()
def addNalu(self, nalu):
"Add a NALU to the stats"
if nalu.isControlNALU():
self.framecounter += 1
if nalu.tid == 0 and nalu.qid == 0:
self.gopsizes.append(self.ctrlcounter)
self.ctrlcounter = 1
else:
self.ctrlcounter += 1
return
layer = (nalu.tid, nalu.qid)
if self.naludict.has_key(layer):
self.naludict[layer] += 1
else:
self.naludict[layer] = 1
if self.bytesdict.has_key(layer):
self.bytesdict[layer] += nalu.length
else:
self.bytesdict[layer] = nalu.length
self.maxnalulen = max(self.maxnalulen, nalu.length)
def getGopSize(self):
"return the most frequent value"
gs = {}
for s in self.gopsizes:
try:
v=gs[s] + 1
except KeyError:
v=1
gs.update({s:v})
maxk = None
maxv = 0
for k, v in gs.iteritems():
if v > maxv:
maxv = v
maxk = k
return maxk
tracefilename = sys.argv[1]
try:
fps = int(sys.argv[2])
except IndexError:
fps = None
# load lines from the trace file
tracefile = open(tracefilename)
nalulist = []
naluheader = []
naludict = {}
for line in tracefile:
try:
nalu = NALU(line)
if nalu.packettype == "SliceData":
nalulist.append(nalu)
naludict.update({nalu.id: nalu})
else:
naluheader.append(nalu)
except IndexError:
pass
except NALUException:
pass
tracefile.close()
# compute statistics
ts = TraceStats()
for nalu in nalulist:
ts.addNalu(nalu)
print ts