127b27ec6Sopenharmony_ci# LZ4 Streaming API Example : Double Buffer 227b27ec6Sopenharmony_ciby *Takayuki Matsuoka* 327b27ec6Sopenharmony_ci 427b27ec6Sopenharmony_ci`blockStreaming_doubleBuffer.c` is LZ4 Streaming API example which implements double buffer (de)compression. 527b27ec6Sopenharmony_ci 627b27ec6Sopenharmony_ciPlease note : 727b27ec6Sopenharmony_ci 827b27ec6Sopenharmony_ci - Firstly, read "LZ4 Streaming API Basics". 927b27ec6Sopenharmony_ci - This is relatively advanced application example. 1027b27ec6Sopenharmony_ci - Output file is not compatible with lz4frame and platform dependent. 1127b27ec6Sopenharmony_ci 1227b27ec6Sopenharmony_ci 1327b27ec6Sopenharmony_ci## What's the point of this example ? 1427b27ec6Sopenharmony_ci 1527b27ec6Sopenharmony_ci - Handle huge file in small amount of memory 1627b27ec6Sopenharmony_ci - Always better compression ratio than Block API 1727b27ec6Sopenharmony_ci - Uniform block size 1827b27ec6Sopenharmony_ci 1927b27ec6Sopenharmony_ci 2027b27ec6Sopenharmony_ci## How the compression works 2127b27ec6Sopenharmony_ci 2227b27ec6Sopenharmony_ciFirst of all, allocate "Double Buffer" for input and LZ4 compressed data buffer for output. 2327b27ec6Sopenharmony_ciDouble buffer has two pages, "first" page (Page#1) and "second" page (Page#2). 2427b27ec6Sopenharmony_ci 2527b27ec6Sopenharmony_ci``` 2627b27ec6Sopenharmony_ci Double Buffer 2727b27ec6Sopenharmony_ci 2827b27ec6Sopenharmony_ci Page#1 Page#2 2927b27ec6Sopenharmony_ci +---------+---------+ 3027b27ec6Sopenharmony_ci | Block#1 | | 3127b27ec6Sopenharmony_ci +----+----+---------+ 3227b27ec6Sopenharmony_ci | 3327b27ec6Sopenharmony_ci v 3427b27ec6Sopenharmony_ci {Out#1} 3527b27ec6Sopenharmony_ci 3627b27ec6Sopenharmony_ci 3727b27ec6Sopenharmony_ci Prefix Dependency 3827b27ec6Sopenharmony_ci +---------+ 3927b27ec6Sopenharmony_ci | | 4027b27ec6Sopenharmony_ci v | 4127b27ec6Sopenharmony_ci +---------+----+----+ 4227b27ec6Sopenharmony_ci | Block#1 | Block#2 | 4327b27ec6Sopenharmony_ci +---------+----+----+ 4427b27ec6Sopenharmony_ci | 4527b27ec6Sopenharmony_ci v 4627b27ec6Sopenharmony_ci {Out#2} 4727b27ec6Sopenharmony_ci 4827b27ec6Sopenharmony_ci 4927b27ec6Sopenharmony_ci External Dictionary Mode 5027b27ec6Sopenharmony_ci +---------+ 5127b27ec6Sopenharmony_ci | | 5227b27ec6Sopenharmony_ci | v 5327b27ec6Sopenharmony_ci +----+----+---------+ 5427b27ec6Sopenharmony_ci | Block#3 | Block#2 | 5527b27ec6Sopenharmony_ci +----+----+---------+ 5627b27ec6Sopenharmony_ci | 5727b27ec6Sopenharmony_ci v 5827b27ec6Sopenharmony_ci {Out#3} 5927b27ec6Sopenharmony_ci 6027b27ec6Sopenharmony_ci 6127b27ec6Sopenharmony_ci Prefix Dependency 6227b27ec6Sopenharmony_ci +---------+ 6327b27ec6Sopenharmony_ci | | 6427b27ec6Sopenharmony_ci v | 6527b27ec6Sopenharmony_ci +---------+----+----+ 6627b27ec6Sopenharmony_ci | Block#3 | Block#4 | 6727b27ec6Sopenharmony_ci +---------+----+----+ 6827b27ec6Sopenharmony_ci | 6927b27ec6Sopenharmony_ci v 7027b27ec6Sopenharmony_ci {Out#4} 7127b27ec6Sopenharmony_ci``` 7227b27ec6Sopenharmony_ci 7327b27ec6Sopenharmony_ciNext, read first block to double buffer's first page. And compress it by `LZ4_compress_continue()`. 7427b27ec6Sopenharmony_ciFor the first time, LZ4 doesn't know any previous dependencies, 7527b27ec6Sopenharmony_ciso it just compress the line without dependencies and generates compressed block {Out#1} to LZ4 compressed data buffer. 7627b27ec6Sopenharmony_ciAfter that, write {Out#1} to the file. 7727b27ec6Sopenharmony_ci 7827b27ec6Sopenharmony_ciNext, read second block to double buffer's second page. And compress it. 7927b27ec6Sopenharmony_ciThis time, LZ4 can use dependency to Block#1 to improve compression ratio. 8027b27ec6Sopenharmony_ciThis dependency is called "Prefix mode". 8127b27ec6Sopenharmony_ci 8227b27ec6Sopenharmony_ciNext, read third block to double buffer's *first* page, and compress it. 8327b27ec6Sopenharmony_ciAlso this time, LZ4 can use dependency to Block#2. 8427b27ec6Sopenharmony_ciThis dependency is called "External Dictonaly mode". 8527b27ec6Sopenharmony_ci 8627b27ec6Sopenharmony_ciContinue these procedure to the end of the file. 8727b27ec6Sopenharmony_ci 8827b27ec6Sopenharmony_ci 8927b27ec6Sopenharmony_ci## How the decompression works 9027b27ec6Sopenharmony_ci 9127b27ec6Sopenharmony_ciDecompression will do reverse order. 9227b27ec6Sopenharmony_ci 9327b27ec6Sopenharmony_ci - Read first compressed block. 9427b27ec6Sopenharmony_ci - Decompress it to the first page and write that page to the file. 9527b27ec6Sopenharmony_ci - Read second compressed block. 9627b27ec6Sopenharmony_ci - Decompress it to the second page and write that page to the file. 9727b27ec6Sopenharmony_ci - Read third compressed block. 9827b27ec6Sopenharmony_ci - Decompress it to the *first* page and write that page to the file. 9927b27ec6Sopenharmony_ci 10027b27ec6Sopenharmony_ciContinue these procedure to the end of the compressed file. 101