# Path: pave.py
# class PaVE:
#   def __init__( self ):
#     self.buf = ""
# 
#   def append( self, packet ):
#     self.buf += packet
# 
# 
#   def extract( self ):
#     "return single packet (header, payload)"
#     if not self.buf.startswith("PaVE"):
#       if "PaVE" in self.buf:
#         self.buf = self.buf[ self.buf.index("PaVE") : ]
# 
#     if len(self.buf) < 4+1+1+2+4:
#       # at least struct of version and header and payload sizes must be ready
#       return "",""
#     
#     if not self.buf.startswith( "PaVE" ):
#       return "",""
# 
#     version, codec, headerSize, payloadSize = struct.unpack_from("BBHI", self.buf, 4 )
#     if len(self.buf) < headerSize + payloadSize:
#       return "",""
# 
#     ret = self.buf[:headerSize], self.buf[headerSize:headerSize+payloadSize]
#     self.buf = self.buf[headerSize + payloadSize : ]
#     return ret
# 
# def isIFrame( header ):
#   "return True if I-Frame"
#   return struct.unpack_from("B", header, 30)[0] == 1
# 
# def frameEncodedWidth( header ):
#   return struct.unpack_from("H", header, 12)[0]
# 
# def frameEncodedHeight( header ):
#   return struct.unpack_from("H", header, 14)[0]


import cvideo
import cv2
import numpy as np
import sys
from pave import PaVE, isIFrame, frameEncodedWidth, frameEncodedHeight


sys.path.append( ".." ) 

print cvideo.init()

img = np.zeros([720,1280,3], dtype=np.uint8)
missingIFrame = True
filename = sys.argv[1]
pave = PaVE()
pave.append( open( filename, "rb" ).read() )
header,payload = pave.extract()
while len(header) > 0:
  w,h = frameEncodedWidth(header), frameEncodedHeight(header)
  if img.shape[0] != h or img.shape[1] != w:
    print img.shape, (w,h)
    img = np.zeros([h,w,3], dtype=np.uint8)

# Path: pave.py
# class PaVE:
#   def __init__( self ):
#     self.buf = ""
# 
#   def append( self, packet ):
#     self.buf += packet
# 
# 
#   def extract( self ):
#     "return single packet (header, payload)"
#     if not self.buf.startswith("PaVE"):
#       if "PaVE" in self.buf:
#         self.buf = self.buf[ self.buf.index("PaVE") : ]
# 
#     if len(self.buf) < 4+1+1+2+4:
#       # at least struct of version and header and payload sizes must be ready
#       return "",""
#     
#     if not self.buf.startswith( "PaVE" ):
#       return "",""
# 
#     version, codec, headerSize, payloadSize = struct.unpack_from("BBHI", self.buf, 4 )
#     if len(self.buf) < headerSize + payloadSize:
#       return "",""
# 
#     ret = self.buf[:headerSize], self.buf[headerSize:headerSize+payloadSize]
#     self.buf = self.buf[headerSize + payloadSize : ]
#     return ret
# 
# def isIFrame( header ):
#   "return True if I-Frame"
#   return struct.unpack_from("B", header, 30)[0] == 1
# 
# def frameEncodedWidth( header ):
#   return struct.unpack_from("H", header, 12)[0]
# 
# def frameEncodedHeight( header ):
#   return struct.unpack_from("H", header, 14)[0]


# Path: cvideo/test.py
import cvideo
import cv2
import numpy as np
import sys
from pave import PaVE, isIFrame, frameEncodedWidth, frameEncodedHeight



sys.path.append( ".." ) 

print cvideo.init()

img = np.zeros([720,1280,3], dtype=np.uint8)
missingIFrame = True
filename = sys.argv[1]
pave = PaVE()
pave.append( open( filename, "rb" ).read() )
header,payload = pave.extract()
while len(header) > 0:
  w,h = frameEncodedWidth(header), frameEncodedHeight(header)
  if img.shape[0] != h or img.shape[1] != w:
    print img.shape, (w,h)
    img = np.zeros([h,w,3], dtype=np.uint8)

  missingIFrame = missingIFrame and not isIFrame(header)