-
Notifications
You must be signed in to change notification settings - Fork 0
/
h264_dissector.py
executable file
·97 lines (77 loc) · 1.86 KB
/
h264_dissector.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
from struct import *
import hashlib
if len(sys.argv) != 2:
print("Usage " + sys.argv[0] + " input_file")
sys.exit(1)
in_name = sys.argv[1]
def printNalu(f, start, end):
# seek to start
f.seek(start, 0)
# get header info
b = unpack('B', f.read(1))[0]
if (b & 0x80):
print("WARN: forbidden_zero_bit is set !")
nal_ref_idc = (b >> 5) & 0x3
nal_unit_type = b & 0x1F
# seek to start
f.seek(start, 0)
# compute sha1 of payload (without head)
m = hashlib.sha1()
payloadLen = end - start
payload = f.read(payloadLen)
m.update(payload)
sha1 = m.hexdigest()
print ("[NALU] type: " + str(nal_unit_type)
+ " ref_idc: " + str(nal_ref_idc)
+ " len: " + str(payloadLen)
+ " sha1: " + str(sha1))
# print(" START: " + payload[0:32].encode('hex'))
# if payloadLen > 32:
# print(" END: " + payload[payloadLen-31:].encode('hex'))
def extractNalu(f):
head = f.read(3)
if not head:
return False
# look for nalu start code
a,b,c = unpack('BBB', head)
while a != 0 or b != 0 or c != 1:
# move forward
a = b
b = c
h = f.read(1)
if not h:
return False
c = unpack('B', h)[0]
# found nalu start
start = f.tell()
# look for nalu end
head = f.read(3)
if not head:
return False
a,b,c = unpack('BBB', head)
while a != 0 or b != 0 or (c != 0 and c != 1):
# move forward
a = b
b = c
h = f.read(1)
if not h:
return False
c = unpack('B', h)[0]
# found nalu end
end = f.tell() - 3
printNalu(f, start, end)
# got back to nalu end
f.seek(end, 0)
return True
with open(in_name, 'rb') as f:
try:
i = 0
while extractNalu(f):
i += 1
except EOFError:
print("unexpected end of file")
print("Done, processed " + str(i) + " NALU")