-
Notifications
You must be signed in to change notification settings - Fork 8
/
purgeLastGOP.py
executable file
·134 lines (107 loc) · 4.28 KB
/
purgeLastGOP.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
#!/usr/bin/env python
#
# Copyright 2009 Claudio Pisa (claudio dot pisa at clauz dot net)
#
# 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
from nalulib import *
if len(sys.argv) < 3:
print """
Delete the last GOP from a trace file. Currently the last GOP of H.264
videos is not handled well by this framework. For this reason the last
GOP of the SVC to be sent should be purged.
Usage: %s <original stream's H264AVCDecoder output> <original trace file>
Where:
<original stream's H264AVCDecoder output>: the screen output obtained
from the H264AVCDecoder ran using the sent H.264 file as argument. For
example:
$ H264AVCDecoderLibTestStatic foreman.264 foreman_null.yuv > originaldecoderoutput.txt
<original trace file>: the trace file obtained by using the "-pt" option of the
BitstreamExtractor executable using as argument the original H.264 file.
For example:
$ BitstreamExtractorStatic -pt originaltrace.txt foreman.264
$ attachframenumber.py originaldecoderoutput.txt originaltrace.txt > originaltrace-frameno.txt
Example:
$ %s originaldecoderoutput.txt originaltrace-frameno.txt > originaltrace-frameno-nolastgop.txt
""" % (sys.argv[0], sys.argv[0])
sys.exit(1)
GOPSIZE = 16
originaldecoderoutputfilename = sys.argv[1]
originaltraceptfilename = sys.argv[2]
# load lines from the original trace file
originaltracefile = open(originaltraceptfilename)
origtracelist = []
for line in originaltracefile:
try:
nalu = NALU(line)
nalu.origframe = 0
origtracelist.append(nalu)
except NALUException:
pass
originaltracefile.close()
# load lines from the decoder file
originaldecoderoutputfile = open(originaldecoderoutputfilename)
decoderlist = []
offset = -2 * GOPSIZE + 1
offsetjustchanged = False
positive = False
for line in originaldecoderoutputfile:
try:
decnalu = DecoderNALU(line)
# find the frame for each line/NALU
if decnalu.frame == 0 and not offsetjustchanged:
offset += GOPSIZE
offsetjustchanged = True
elif decnalu.frame > 0 and not offsetjustchanged and not positive: # in the last GOP the sign changes
offset += GOPSIZE
positive = True
offsetjustchanged = True # delete?
if offset < 0:
decnalu.realframe = abs(decnalu.frame)
else:
decnalu.realframe = offset + abs(decnalu.frame)
decoderlist.append(decnalu)
except NALUException:
offsetjustchanged = False
originaldecoderoutputfile.close()
tracelist = [nalu for nalu in origtracelist if nalu.packettype == "SliceData" and not nalu.isControlNALU()]
if len(tracelist) != len(decoderlist):
print >> sys.stderr, "Length problem: %d vs. %d" % (len(tracelist), len(decoderlist))
sys.exit(1)
# check that tracefile and decoder output match
zippedlists = zip(tracelist, decoderlist)
for tn, dn in zippedlists:
if tn.lid != dn.lid or tn.tid != dn.tl or tn.qid != dn.ql:
print >> sys.stderr, "Problem: %d %d %d vs. %d %d %d at %s (frame %d)" % (tn.lid, tn.tid, tn.qid, dn.lid, dn.tl, dn.ql, tn.startpos, dn.frame)
sys.exit(2)
tn.frame = dn.realframe
tn.origframe = dn.frame
tn.type = dn.type
# print on standard output the filtered tracefile purged from the last GOP
#print "Start-Pos. Length LId TId QId Packet-Type Discardable Truncatable"
#print "========== ====== === === === ============ =========== ==========="
for tn in [tn for tn in origtracelist if tn.packettype == "StreamHeader" or tn.packettype == "ParameterSet"]:
print tn
finallist = []
purging = False
for tn in [tn for tn in origtracelist if tn.packettype == "SliceData"]:
if not purging and tn.origframe <=0:
finallist.append(tn)
else:
purging = True
for tn in finallist[:-1]:
print tn