def gen_src_images(src_format, w, h, nframes): seed = 0 images = [] for i in range(nframes): #create a dummy ImageWrapper to compress: if src_format.startswith("YUV"): strides, pixels = make_planar_input(src_format, w, h, use_strings=False, populate=True, seed=seed + i) planes = ImageWrapper._3_PLANES else: pixels = make_rgb_input(src_format, w, h, use_strings=False, populate=True, seed=seed + i) strides = w * 3 planes = ImageWrapper.PACKED image = ImageWrapper(0, 0, w, h, pixels, src_format, 24, strides, planes=planes) images.append(image) seed += 10 return images
def perf_measure_planar(csc_module, w=1920, h=1080): count = min(MAX_ITER, int(PERF_LOOP * 1024 * 1024 / (w * h))) for src_format in sorted(csc_module.get_input_colorspaces()): if src_format not in ("YUV420P", "YUV422P", "YUV444P"): continue strides, pixels = make_planar_input(src_format, w, h, populate=True) rgb_dst_formats = sorted([ x for x in csc_module.get_output_colorspaces(src_format) if (x.find("YUV") < 0 and not x.endswith("P")) ]) for dst_format in rgb_dst_formats: #print("make_planar_input(%s, %s, %s) strides=%s, len(pixels=%s", src_format, w, h, strides, len(pixels)) start = time.time() do_test_csc_planar(csc_module, src_format, dst_format, w, h, strides, pixels, count=count) end = time.time() if DEBUG: print("%s did %sx%s csc %s times in %.1fms" % (csc_module, w, h, count, end - start)) mpps = float(w * h * count) / (end - start) dim = ("%sx%s" % (w, h)).rjust(10) info = ("%s to %s at %s" % (src_format.ljust(7), dst_format.ljust(7), dim)).ljust(40) print("%s: %s MPixels/s" % (info, int(mpps / 1024 / 1024)))
def perf_measure_planar(csc_module, w=1920, h=1080, test_scaling=[(1,1), (1,2), (1,3)]): count = min(MAX_ITER, int(PERF_LOOP*1024*1024/(w*h))) for csc_speed in (0, 50, 100): print("**** %4ix%-4i - speed=%i%%" % (w, h, csc_speed)) for src_format in sorted(csc_module.get_input_colorspaces()): if src_format not in ("YUV420P", "YUV422P", "YUV444P"): continue strides, pixels = make_planar_input(src_format, w, h, populate=True) rgb_dst_formats = sorted([x for x in csc_module.get_output_colorspaces(src_format) if (x.find("YUV")<0 and not x.endswith("P"))]) for dst_format in rgb_dst_formats: if not csc_module.get_spec(src_format, dst_format).can_scale: test_scaling=[(1,1)] #print("make_planar_input(%s, %s, %s) strides=%s, len(pixels=%s", src_format, w, h, strides, len(pixels)) for m,d in test_scaling: dst_w, dst_h = w*m//d, h*m//d start = time.time() do_test_csc_planar(csc_module, src_format, dst_format, w, h, strides, pixels, dst_w, dst_h, count=count, csc_speed=csc_speed) end = time.time() if DEBUG: print("%s did %sx%s csc %s times in %.1fms" % (csc_module, w, h, count, end-start)) mpps = float(w*h*count)/(end-start) info = ("%s to %s" % (src_format.ljust(7), dst_format.ljust(7))).ljust(24) if m!=1 or d!=1: scaling_info = ", dowscaled by %i/%i" % (m, d) else: scaling_info = "" print("%s: %4i MPixels/s%s" % (info, int(mpps/1024/1024), scaling_info))
def test_csc_planar_all(csc_module, w, h): #get_subsampling_divs() e420 = bytearray([0xff, 0x0, 0x87, 0x0, 0xff, 0x0, 0x88, 0x0, 0xff, 0x0, 0x81, 0x0, 0xff, 0x0, 0x82]) e422 = bytearray([0xff, 0x0, 0x87, 0x0, 0xff, 0x0, 0x86, 0x0, 0xff, 0x0, 0x84, 0x0, 0xff, 0x0, 0x84]) e444 = bytearray([0xff, 0x0, 0x87, 0x0, 0xff, 0x0, 0x87, 0x0, 0xff, 0x0, 0x87, 0x0, 0xff, 0x0, 0x87]) CHECKS = { ("YUV420P", "XRGB", 32, 32) : ((e420,)), ("YUV422P", "XRGB", 32, 32) : ((e422,)), ("YUV444P", "XRGB", 32, 32) : ((e444,)), } for use_strings in (True, False): planar_in = [x for x in csc_module.get_input_colorspaces() if x.startswith("YUV")] for src_format in sorted(planar_in): dst_formats = sorted([x for x in csc_module.get_output_colorspaces(src_format) if (not x.startswith("YUV") and not x.endswith("P"))]) populate = len([cs for (cs,cd,cw,ch) in CHECKS.keys() if (cs==src_format and cd in dst_formats and cw==w and ch==h)])>0 #populate = len([x for x in FMT_TO_EXPECTED_OUTPUT.keys() if x[0]==src_format and x[1] in dst_formats])>0 strides, pixels = make_planar_input(src_format, w, h, use_strings, populate=populate) for dst_format in dst_formats: spec = csc_module.get_spec(src_format, dst_format) if w<spec.min_w or h<spec.min_h: print("skipping test %s to %s at %sx%s because dimensions are too small" % (src_format, dst_format, w, h)) continue out_pixels = do_test_csc_planar(csc_module, src_format, dst_format, w, h, strides, pixels, w, h) if DEBUG: print("test_csc_planar_all() %s to %s head of output pixels=%s" % (src_format, dst_format, dump_pixels(out_pixels[:128]))) expected = CHECKS.get((src_format, dst_format)) ok = check_plane(dst_format, out_pixels, expected) if ok: pass else: print("test_csc_planar_all(%s, %s, %s) %s to %s, use_strings=%s" % (csc_module.get_type(), w, h, src_format, dst_format, use_strings))
def perf_measure_planar(csc_module, w=1920, h=1080, test_scaling=[(1, 1), (1, 2), (1, 3)]): count = min(MAX_ITER, int(PERF_LOOP * 1024 * 1024 / (w * h))) for csc_speed in (0, 50, 100): print("**** %4ix%-4i - speed=%i%%" % (w, h, csc_speed)) for src_format in sorted(csc_module.get_input_colorspaces()): if src_format not in ("YUV420P", "YUV422P", "YUV444P"): continue strides, pixels = make_planar_input(src_format, w, h, populate=True) rgb_dst_formats = sorted([ x for x in csc_module.get_output_colorspaces(src_format) if (x.find("YUV") < 0 and not x.endswith("P")) ]) for dst_format in rgb_dst_formats: if not csc_module.get_spec(src_format, dst_format).can_scale: test_scaling = [(1, 1)] #print("make_planar_input(%s, %s, %s) strides=%s, len(pixels=%s", src_format, w, h, strides, len(pixels)) for m, d in test_scaling: dst_w, dst_h = w * m // d, h * m // d start = time.time() do_test_csc_planar(csc_module, src_format, dst_format, w, h, strides, pixels, dst_w, dst_h, count=count, csc_speed=csc_speed) end = time.time() if DEBUG: print("%s did %sx%s csc %s times in %.1fms" % (csc_module, w, h, count, end - start)) mpps = float(w * h * count) / (end - start) info = ( "%s to %s" % (src_format.ljust(7), dst_format.ljust(7))).ljust(24) if m != 1 or d != 1: scaling_info = ", dowscaled by %i/%i" % (m, d) else: scaling_info = "" print("%s: %4i MPixels/s%s" % (info, int(mpps / 1024 / 1024), scaling_info))
def do_test_codec_roundtrip(encoder_class, decoder_class, encoding, src_format, dst_formats, w, h, populate): quality = 100 speed = 100 scaling = (1,1) options = {} start = time.time() encoder = encoder_class() #print("%s%s" % (encoder.init_context, (w, h, src_format, dst_formats, encoding, quality, speed, scaling, options))) encoder.init_context(w, h, src_format, dst_formats, encoding, quality, speed, scaling, options) end = time.time() print("encoder %s initialized in %.1fms" % (encoder, 1000.0*(end-start))) start = time.time() decoder = decoder_class() decoder.init_context(encoding, w, h, src_format) end = time.time() print("decoder %s initialized in %.1fms" % (decoder, 1000.0*(end-start))) for i in range(4): if src_format.find("RGB")>=0 or src_format.find("BGR")>=0: pixels = make_rgb_input(src_format, w, h, populate=populate, seed=i) isize = len(pixels) stride = len(src_format)*w #print(" input pixels: %s (%sx%s, stride=%s, stride*h=%s)" % (len(pixels), w, h, stride, stride*h)) assert len(pixels)>=stride*h, "not enough pixels! (expected at least %s but got %s)" % (stride*h, len(pixels)) image = ImageWrapper(0, 0, w, h, pixels, src_format, 24, stride, planes=ImageWrapper.PACKED) else: strides, pixels = make_planar_input(src_format, w, h, populate=populate) isize = sum([len(x) for x in pixels]) image = ImageWrapper(0, 0, w, h, pixels, src_format, 24, strides, planes=ImageWrapper.PLANAR_3) print("FRAME %s" % i) print("using %s to compress %s" % (encoder, image)) start = time.time() data, options = encoder.compress_image(image) end = time.time() assert data is not None, "compression failed" print("compressed %s bytes down to %s (%.1f%%) in %.1fms" % (isize, len(data), 100.0*len(data)/isize, 1000.0*(end-start))) print("uncompressing %s bytes using %s" % (len(data), decoder)) options['csc'] = src_format start = time.time() out_image = decoder.decompress_image(data, options) end = time.time() assert out_image is not None, "decompression failed" print("uncompressed to %s in %.1fms" % (str(out_image), 1000.0*(end-start)))
def do_test_codec_roundtrip(encoder_class, decoder_class, encoding, src_format, dst_formats, w, h, populate): quality = 100 speed = 100 scaling = (1,1) options = {} start = time.time() encoder = encoder_class() #print("%s%s" % (encoder.init_context, (w, h, src_format, dst_formats, encoding, quality, speed, scaling, options))) encoder.init_context(w, h, src_format, dst_formats, encoding, quality, speed, scaling, options) end = time.time() print("encoder %s initialized in %.1fms" % (encoder, 1000.0*(end-start))) start = time.time() decoder = decoder_class() decoder.init_context(encoding, w, h, src_format) end = time.time() print("decoder %s initialized in %.1fms" % (decoder, 1000.0*(end-start))) for i in range(4): if src_format.find("RGB")>=0 or src_format.find("BGR")>=0: pixels = make_rgb_input(src_format, w, h, populate=populate, seed=i) isize = len(pixels) stride = len(src_format)*w #print(" input pixels: %s (%sx%s, stride=%s, stride*h=%s)" % (len(pixels), w, h, stride, stride*h)) assert len(pixels)>=stride*h, "not enough pixels! (expected at least %s but got %s)" % (stride*h, len(pixels)) image = ImageWrapper(0, 0, w, h, pixels, src_format, 24, stride, planes=ImageWrapper.PACKED) else: strides, pixels = make_planar_input(src_format, w, h, populate=populate) isize = sum([len(x) for x in pixels]) image = ImageWrapper(0, 0, w, h, pixels, src_format, 24, strides, planes=ImageWrapper._3_PLANES) print("FRAME %s" % i) print("using %s to compress %s" % (encoder, image)) start = time.time() data, options = encoder.compress_image(image) end = time.time() assert data is not None, "compression failed" print("compressed %s bytes down to %s (%.1f%%) in %.1fms" % (isize, len(data), 100.0*len(data)/isize, 1000.0*(end-start))) print("uncompressing %s bytes using %s" % (len(data), decoder)) options['csc'] = src_format start = time.time() out_image = decoder.decompress_image(data, options) end = time.time() assert out_image is not None, "decompression failed" print("uncompressed to %s in %.1fms" % (str(out_image), 1000.0*(end-start)))
def gen_src_images(src_format, w, h, nframes): seed = 0 images = [] for i in range(nframes): #create a dummy ImageWrapper to compress: if src_format.startswith("YUV"): strides, pixels = make_planar_input(src_format, w, h, use_strings=False, populate=True, seed=seed+i) planes = ImageWrapper._3_PLANES else: pixels = make_rgb_input(src_format, w, h, use_strings=False, populate=True, seed=seed+i) strides = w*3 planes = ImageWrapper.PACKED image = ImageWrapper(0, 0, w, h, pixels, src_format, 24, strides, planes=planes) images.append(image) seed += 10 return images
def perf_measure_planar(csc_module, w=1920, h=1080): count = min(MAX_ITER, int(PERF_LOOP*1024*1024/(w*h))) for src_format in sorted(csc_module.get_input_colorspaces()): if src_format not in ("YUV420P", "YUV422P", "YUV444P"): continue strides, pixels = make_planar_input(src_format, w, h, populate=True) rgb_dst_formats = sorted([x for x in csc_module.get_output_colorspaces(src_format) if (x.find("YUV")<0 and not x.endswith("P"))]) for dst_format in rgb_dst_formats: #print("make_planar_input(%s, %s, %s) strides=%s, len(pixels=%s", src_format, w, h, strides, len(pixels)) start = time.time() do_test_csc_planar(csc_module, src_format, dst_format, w, h, strides, pixels, count=count) end = time.time() if DEBUG: print("%s did %sx%s csc %s times in %.1fms" % (csc_module, w, h, count, end-start)) mpps = float(w*h*count)/(end-start) dim = ("%sx%s" % (w,h)).rjust(10) info = ("%s to %s at %s" % (src_format.ljust(7), dst_format.ljust(7), dim)).ljust(40) print("%s: %s MPixels/s" % (info, int(mpps/1024/1024)))
def test_csc_planar_all(csc_module, w, h): #get_subsampling_divs() e420 = bytearray([ 0xff, 0x0, 0x87, 0x0, 0xff, 0x0, 0x88, 0x0, 0xff, 0x0, 0x81, 0x0, 0xff, 0x0, 0x82 ]) e422 = bytearray([ 0xff, 0x0, 0x87, 0x0, 0xff, 0x0, 0x86, 0x0, 0xff, 0x0, 0x84, 0x0, 0xff, 0x0, 0x84 ]) e444 = bytearray([ 0xff, 0x0, 0x87, 0x0, 0xff, 0x0, 0x87, 0x0, 0xff, 0x0, 0x87, 0x0, 0xff, 0x0, 0x87 ]) CHECKS = { ("YUV420P", "XRGB", 32, 32): ((e420, )), ("YUV422P", "XRGB", 32, 32): ((e422, )), ("YUV444P", "XRGB", 32, 32): ((e444, )), } for use_strings in (True, False): planar_in = [ x for x in csc_module.get_input_colorspaces() if x.startswith("YUV") ] for src_format in sorted(planar_in): dst_formats = sorted([ x for x in csc_module.get_output_colorspaces(src_format) if (not x.startswith("YUV") and not x.endswith("P")) ]) populate = len([ cs for (cs, cd, cw, ch) in CHECKS.keys() if (cs == src_format and cd in dst_formats and cw == w and ch == h) ]) > 0 #populate = len([x for x in FMT_TO_EXPECTED_OUTPUT.keys() if x[0]==src_format and x[1] in dst_formats])>0 strides, pixels = make_planar_input(src_format, w, h, use_strings, populate=populate) for dst_format in dst_formats: spec = csc_module.get_spec(src_format, dst_format) if w < spec.min_w or h < spec.min_h: print( "skipping test %s to %s at %sx%s because dimensions are too small" % (src_format, dst_format, w, h)) continue out_pixels = do_test_csc_planar(csc_module, src_format, dst_format, w, h, strides, pixels, w, h) if DEBUG: print( "test_csc_planar_all() %s to %s head of output pixels=%s" % (src_format, dst_format, dump_pixels( out_pixels[:128]))) expected = CHECKS.get((src_format, dst_format)) ok = check_plane(dst_format, out_pixels, expected) if ok: pass else: print( "test_csc_planar_all(%s, %s, %s) %s to %s, use_strings=%s" % (csc_module.get_type(), w, h, src_format, dst_format, use_strings))