1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3""" 4* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. 5* Licensed under the Apache License, Version 2.0 (the "License"); 6* you may not use this file except in compliance with the License. 7* You may obtain a copy of the License at 8* 9* http://www.apache.org/licenses/LICENSE-2.0 10* 11* Unless required by applicable law or agreed to in writing, software 12* distributed under the License is distributed on an "AS IS" BASIS, 13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14* See the License for the specific language governing permissions and 15* limitations under the License. 16""" 17 18import zlib 19import struct 20import time 21import numpy as np 22import cv2 23import sys 24import re 25import argparse 26 27 28class RawPlayer: 29 """ 30 Play a boot video file 31 """ 32 33 def __init__(self, args): 34 self._raw = args.raw 35 cv2.namedWindow("play", cv2.WINDOW_AUTOSIZE) 36 pass 37 38 def play(self): 39 screen_size = re.findall("bootanimation-([0-9]+)x([0-9]+).raw", self._raw) 40 if len(screen_size) != 1: 41 exit() 42 width, height = int(screen_size[0][0]), int(screen_size[0][1]) 43 with open(sys.argv[-1], "rb") as fp: 44 data = fp.read() 45 off = 8 46 img = None 47 while off < len(data): 48 data_type, offset, length, data_length = struct.unpack("IIII", data[off:off + 16]) 49 off += 16 50 if data_type == 0: 51 time.sleep(0.03) 52 continue 53 54 out = zlib.decompress(data[off:off + data_length]) 55 56 if img is None: 57 img = np.copy(np.frombuffer(out, dtype=np.uint8)) 58 else: 59 temp_img = np.frombuffer(out, dtype=np.uint8) 60 img[offset:offset + length] = temp_img 61 reshape_img = img.reshape((height, width, 4)) 62 cv2.imshow("play", reshape_img) 63 if cv2.waitKey(30) & 0xff == 27 or cv2.getWindowProperty("play", cv2.WND_PROP_VISIBLE) < 1: 64 break 65 while data_length % 4 != 0: 66 data_length += 1 67 off += data_length 68 69 70def parse_option(): 71 parser = argparse.ArgumentParser(description="Play a boot video file", 72 usage="python raw_player.py [-h] <*.raw>\n" 73 " eg.: python raw_player.py ./bootanimation-640x480.raw") 74 parser.add_argument("raw", metavar="<*.raw>", help="file <*.raw> to play") 75 return parser.parse_args() 76 77 78if __name__ == "__main__": 79 raw_player = RawPlayer(parse_option()) 80 raw_player.play() 81