2010年1月28日

code :Transport Stream continuity check - 3

print all pid packet number and the not-continue packe number

from struct import *

tsfile = open('src.trp','rb')
if tsfile == None:
print 'open file fail'
tsstring = tsfile.read()
print 'read file ok'
tsdata = unpack(repr(len(tsstring))+'B',tsstring)
print 'convert OK, size = %d' % len(tsdata)

i=0
while tsdata[i] != 0x47:
i += 1
print 'found 1st sync: %d' % i


pidcount={}
discount={}
contp = {}
while i < len(tsdata)-188:
pid = ( tsdata[i+1]<< 8 | tsdata[i+2] ) & 0x1FFF
cont = tsdata[i+3] & 0x0F
if pid in contp.keys():
# print '%04X : %02X' % (pid,cont)
if cont != ((contp[pid]+1)&0xF):
if cont != 0 or contp[pid] != 0:
print 'not cont: %04X : %02x %02x' % (pid,contp[pid],cont)
discount[pid] += 1
contp[pid] = cont
pidcount[pid] += 1
else:
contp.update({pid:cont})
pidcount.update({pid:1})
discount.update({pid:0})
i += 188

# print out not cont number
allpid = contp.keys()
allpid.sort()

for ele in allpid:
print 'pid: %04X packet count : %7d, not continue: %d' % (ele,pidcount[ele],discount[ele])

2010年1月27日

code :Transport Stream continuity check - 2

承上篇:計算每一個 pid 的數量

from struct import *

# read in all data and convert to unsigned char
tsfile = open('logOsv17.trp','rb')
if tsfile == None:
print "Error opening logOsv17.trp to read"
tsstring = tsfile.read()
print "the size is : %d" % len(tsstring)

tsdata = unpack(repr(len(tsstring))+'B',tsstring)
print "%02x %02x %02x" % (tsdata[0],tsdata[1],tsdata[2])


# locate the 1st sync
i=0
while tsdata[0] != 0x47:
i += 1
print "found 1st 0x47: %d %02x" % (i,tsdata[i+188])

# start accumlate
pidtable = {} # pid : packet numbers
while i < len(tsdata)-188:
pid = tsdata[i+1]<<8 | tsdata[i+2]
pid = pid & 0x1FFF
# print "%02x %02x (%04x) - %02x" % (tsdata[i+1],tsdata[i+2],pid,tsdata[i+3]&0x0F)

if pid not in pidtable:
pidtable.update({pid:0})
else:
pidtable[pid] += 1

# find next packet sync
if tsdata[i+188] != 0x47:
print "%d : %02x" % (i,tsdata[i+188])
i+=1
while tsdata[i] != 0x47:
i += 1
else:
i += 188

# print results
allpid = pidtable.keys()
allpid.sort()
for i in allpid:
print "%04x : %d" % (i,pidtable[i])

2010年1月26日

code :Transport Stream continuity check

今天的進度: 找出所有的 pid

from struct import *

tsfile = open('logOsv17.trp','rb')
if tsfile == None:
print "Error opening logOsv17.trp to read"
tsstring = tsfile.read()
print "the size is : %d" % len(tsstring)

tsdata = unpack(repr(len(tsstring))+'B',tsstring)
print "%02x %02x %02x" % (tsdata[0],tsdata[1],tsdata[2])


prev0x47=0
i=0
while tsdata[0] != 0x47:
i += 1
print "found 1st 0x47: %d %02x" % (i,tsdata[i+188])

pidtable = []
while i < len(tsdata)-188:
pid = tsdata[i+1]<<8 | tsdata[i+2]
pid = pid & 0x1FFF
# print "%02x %02x (%04x) - %02x" % (tsdata[i+1],tsdata[i+2],pid,tsdata[i+3]&0x0F)
if pid not in pidtable:
pidtable.append(pid)

if tsdata[i+188] != 0x47:
print "%d : %02x" % (i,tsdata[i+188])
i+=1
while tsdata[i] != 0x47:
i += 1
else:
i += 188
pidtable.sort()
for i in pidtable:
print "%04x" % i

2010年1月25日

read binary file and convert to binary tuple

因為 file.read( ) 的傳回值是 string。所以沒辦法用 %X 印出。
也沒辦法用數值比較(?)。

所以讀進來後,要用 struct.unpack( )把 string 解譯為 binary。

unpack 的用法是:

unpack(fmt,string)

fmt 格式字串
string 要被轉換的 string

要一個一個 byte 的轉為 unsigned char ,要用 'B'。
'B' 前面還要指定總共要轉幾個 byte。
又,unpack 傳回的是 tuple,所以一次把所有的 string 轉成一個 unsigned char tunple,
要先寫好 fmt:

fmtstring = repr(len(readstring)) + 'B'

然後再轉:

data = unpack(fmtstring,readstring)

也可以寫在一起:

data = unpack(repr(len(readstring))+'B',readstring)