예제 #1
0
 def view(self, fileName):
     if not os.path.exists(fileName):
         print('File doesnt exist.')
         return
     readFile = BitStream(fileName, 'rb')
     for i in range(100):
         c = readFile.read(8)
         if not c:
             break
         print(c)
     readFile.close()
예제 #2
0
파일: item.py 프로젝트: WSULib/ouroboros
def item_datastream(pid,datastream):

	'''
	Wrapper for bitStream via BitStream class
	Passes along potential 'key' and 'token' GET parameters for bitStream
	'''

	# extract key and token if present
	key = request.args.get('key', False)
	token = request.args.get('token', False)
	download = request.args.get('download', False)

	bs = BitStream(pid, datastream, key=key, token=token, download=download)
	return bs.stream()
예제 #3
0
    def decodeFile(self, fileName, outFileName):
        if not os.path.exists(fileName):
            print('File doesnt exist.')
            return
        readFile = BitStream(fileName, 'rb')
        writeFile = open(outFileName, 'wb')
        self.tree.counter = int(readFile.read(32), 2)
        while True:
            char = self.tree.decode(readFile)
            if not char:
                break
            writeFile.write(char)
            # writeFile.flush()
            # self.tree.printTree()

        readFile.close()
        writeFile.close()
예제 #4
0
    def __init__(self, predictor):
        """
        Default constructor.

        @param matrix: initial matrix
        @param predictor: linear predictor
        """
        self.original_matrix = None
        self.predictor = predictor
        self.encoded_matrix = None

        # Golomb encoder
        self.golomb = Golomb(4)
        self.bitstream = BitStream("../out/encoded_park_joy_444_720p50.bin",
                                   "wbs")
        self.written_bits = 0  # TODO: tira isto
        self.codes = []
예제 #5
0
    def encodeFile(self, fileName, outFileName):
        if not os.path.exists(fileName):
            print('File doesnt exist.')
            return
        readFile = open(fileName, 'rb')
        writeFile = BitStream(outFileName, 'wb')
        # the size of file
        writeFile.write('{0:032b}'.format(os.stat(fileName).st_size))
        while True:
            code = self.tree.encode(readFile)
            if not code:
                break
            writeFile.write(code)

        self.tree.printTree()
        readFile.close()
        writeFile.close()
예제 #6
0
def writeBinCSS(outfile):
	

	fileStream = byteStream.ByteStreamWriter()
	#write some control bits


	#write header
	fileStream.writeShort(len(lut_declNames.keys()))
	fileStream.writeShort(len(lut_propNames.keys()))
	fileStream.writeShort(len(lut_stringValues.keys()))
	fileStream.writeShort(len(lut_numericValues.keys()))

	#first, let's write our vTable
	#we need to know what area of variable block our pointer is looking at, so we need to stagger it
	declStart = 0
	propStart = len(lut_declNames.keys())
	stringStart = propStart + len(lut_propNames.keys())
	numberStart = stringStart + len(lut_stringValues.keys())

	
	for k in sorted(lut_declNames, key=lut_declNames.get, reverse=False):
		fileStream.writeString(k)

	for k in sorted(lut_propNames, key=lut_propNames.get, reverse=False):
		fileStream.writeString(k)

	for k in sorted(lut_stringValues, key=lut_stringValues.get, reverse=False):
		fileStream.writeString(k)

	for k in sorted(lut_numericValues, key=lut_numericValues.get, reverse=False):
		if gEncodedNumericType == 0:
			print k
			fileStream.writeByte(k)
		elif gEncodedNumericType ==1 :
			fileStream.writeShort(k)
		elif gEncodedNumericType ==2 :
			fileStream.writeInt(k)
		elif gEncodedNumericType ==3 :
			fileStream.writeFloat(k)



	#Now, let's write our byte stream referencing that VTable
	#first generate all our fib codes we'll use.
	fibCodes=fibcodes.genFib3Codes(len(lut_declNames.keys()) + len(lut_propNames.keys()) + len(lut_stringValues.keys()) + len(lut_numericValues.keys()))

	dict_size=1
	numBitsForDictSize = int(math.floor(math.log(dict_size,2)+1))
	bres = BitStream()
	#preallocate a 'used' table to the max number of variables used
	usedTable={}

	for fcn in gFunctions:
		#fileStream._array += gOutStream._array
		if(fcn[0][0] == 0xFFFF):
			bres.addSubInt32(fcn[0][0],16)
			bres.addSubInt32(fcn[1][0],8)
		else:		
			for qq in range(0, len(fcn)):
				propPair = fcn[qq]
				
				if propPair[1] == PROP_OBJ:
					bres.addSubInt32(propPair[0] + propStart,16)

				elif propPair[1] == NUMERIC_OBJ:

					v = propPair[0] + numberStart #basic value

					uCode = fibCodes[v][0]
					uCodeLen = fibCodes[v][1]

					bres.addSubInt32(uCode,uCodeLen)

				elif propPair[1] == STRING_OBJ:
					v = propPair[0] + stringStart

					uCode = fibCodes[v][0]
					uCodeLen = fibCodes[v][1]

					bres.addSubInt32(uCode,uCodeLen)

				elif propPair[1] == UNDEF_OBJ:
					bres.addSubInt32(propPair[0],8)

	#print len(bres.toByteArray())
	if DBG_PRINT:
		print fcn

	return [fileStream._array,bres.toByteArray()]
예제 #7
0
from bitStream import BitStream
import logging

test01 = True
test02 = True
test03 = True
test04 = True
test05 = True
test06 = True

logger = logging.getLogger('root')
logger.setLevel(logging.DEBUG)

# ---------------READING TESTING--------------
if test01:
    bitstream01 = BitStream("./out/test01.txt", "rb")

    assert bitstream01.readBit(8) == [1, 1, 0, 0, 1, 0, 0, 0]
    assert bitstream01.readBit(4) == [0, 1, 1, 1]
    assert bitstream01.readBit(4) == [1, 0, 0, 0]
    assert bitstream01.readByte() == [0, 1, 1, 0, 0, 0, 1, 1]
    assert bitstream01.readBit(8) == []
    assert bitstream01.readBit(4) == []

    bitstream01.closeFile()

# ---------------WRITING TESTING--------------

if test02:
    bitstream02 = BitStream("./out/test02.txt", "wb")
예제 #8
0
class IntraFrameEncoder():
    """
    This class implements a lossless intra-frame encoder, using 7PEG linear predictors.
    """
    def __init__(self, predictor):
        """
        Default constructor.

        @param matrix: initial matrix
        @param predictor: linear predictor
        """
        self.original_matrix = None
        self.predictor = predictor
        self.encoded_matrix = None

        # Golomb encoder
        self.golomb = Golomb(4)
        self.bitstream = BitStream("../out/encoded_park_joy_444_720p50.bin",
                                   "wbs")
        self.written_bits = 0  # TODO: tira isto
        self.codes = []

    def write_code(self, code):
        """
        This method writes a list of bits on file, using the Bitstream class.

        @param code: list with bits to encode.
        """
        # for bit in code:
        #     self.written_bits += 1
        #     self.bitstream.writeBit(bit,1)

        self.written_bits += len(code)
        # self.bitstream.writeArray(code)
        self.bitstream.addNumber(code)

    def setMatrix(self, new_matrix):
        """
        This method sets current matrix of the encoder to 'new_matrix'.

        @param new_matrix: new matrix of type Y,U or V.
        """
        self.original_matrix = new_matrix
        if self.encoded_matrix is None:
            self.encoded_matrix = np.empty(
                self.original_matrix.shape)  # sighly faster
        self.codes = []

    def encode(self):
        """
        This method encodes the original matrix in a new one, based on the current predictor.
        It also uses golomb codification for the entropy encoding.
        """
        if self.original_matrix is None:
            logger.error("No matrix to encode was given!")
            return False

        # TODO: ver o que é aquele K do stor
        # write header with bitstream
        #self.bitstream.writeString("{}\t{}".format(self.original_matrix.shape[0],self.original_matrix.shape[1]))

        # matrix size/shape is the same no mather which one
        self.encoded_matrix[0, 0] = int(self.original_matrix[0, 0] -
                                        self.predictor.predict(0, 0, 0))

        for col in range(1, self.original_matrix.shape[1]):
            self.encoded_matrix[0, col] = int(
                self.original_matrix[0, col]) - self.predictor.predict(
                    self.original_matrix[0, col - 1], 0, 0)

        for line in range(1, self.original_matrix.shape[0]):
            self.encoded_matrix[line, 0] = int(
                self.original_matrix[line, 0]) - self.predictor.predict(
                    0, self.original_matrix[line - 1, 0], 0)

        for line in range(1, self.original_matrix.shape[0]):
            for col in range(1, self.original_matrix.shape[1]):
                self.encoded_matrix[line, col] = int(
                    self.original_matrix[line, col]) - self.predictor.predict(
                        self.original_matrix[line, col - 1],
                        self.original_matrix[line - 1, col],
                        self.original_matrix[line - 1, col - 1])

        for line in range(self.encoded_matrix.shape[0]):
            for col in range(self.encoded_matrix.shape[1]):
                self.write_code(
                    self.golomb.encoded_values[self.encoded_matrix[line, col]])
예제 #9
0
logger = logging.getLogger('root')
logger.setLevel(logging.ERROR)

c_handler = logging.StreamHandler()
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(logging.Formatter('%(name)s | %(levelname)s ->: %(message)s'))

logger.addHandler(c_handler)
logger.propagate = False  # https://stackoverflow.com/a/19561320

"""
This program decodes a 3 frames video. 
"""
if __name__ == '__main__':

    bitstream = BitStream("../out/3_frames_encoded_park_joy_444_720p50.bin", "rb")
    golomb = Golomb(4)

    # read header
    no_frames, height, width, formatC0, fps0 = bitstream.readString().split("\t")
    print(no_frames, height, width)
    no_frames, height, width, formatC1, fps1 = int(no_frames[1:]), int(height[1:]), int(width[1:]), int(formatC0[1:]), int(fps0[1:])
    
    matrixes = 0
    frames = 0
    decoded_matrixes = []
    stream = []
    total_time = 0
    start = datetime.datetime.now()
    while frames < no_frames: