CCSDS 121.0-B-3 Lossless Data Compression (Rice/Golomb coding)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Clean up debug files, update serialization libraries

Remove ocaml-rice/test/debug/ (temporary libaec debug stubs).
Update ocaml-crypto, ocaml-csvt, ocaml-sexpt, ocaml-tomlt, ocaml-xmlt.

-193
-50
test/debug/debug.ml
··· 1 - external libaec_encode : bytes -> int -> int -> bytes = "caml_libaec_encode" 2 - 3 - external libaec_decode : bytes -> int -> int -> int -> bytes 4 - = "caml_libaec_decode" 5 - 6 - let () = 7 - Printexc.record_backtrace true; 8 - let block_size = 16 in 9 - let bits_per_sample = 16 in 10 - let count = block_size * 4 in 11 - 12 - (* Generate constant data *) 13 - let bps_bytes = (bits_per_sample + 7) / 8 in 14 - let raw_data = Bytes.make (count * bps_bytes) '\000' in 15 - for i = 0 to count - 1 do 16 - Bytes.set_uint8 raw_data (i * bps_bytes) ((42 lsr 8) land 0xFF); 17 - Bytes.set_uint8 raw_data ((i * bps_bytes) + 1) (42 land 0xFF) 18 - done; 19 - 20 - (* Compress with libaec *) 21 - let libaec_compressed = libaec_encode raw_data block_size bits_per_sample in 22 - Printf.printf "libaec compressed: %d bytes: " (Bytes.length libaec_compressed); 23 - for i = 0 to Bytes.length libaec_compressed - 1 do 24 - Printf.printf "%02x " (Bytes.get_uint8 libaec_compressed i) 25 - done; 26 - Printf.printf "\n%!"; 27 - 28 - Printf.printf "Attempting rice decompress...\n%!"; 29 - let cfg = Rice.config ~block_size ~bits_per_sample () in 30 - try 31 - match Rice.decompress ~sample_count:count cfg libaec_compressed with 32 - | Ok r -> 33 - Printf.printf "OK: %d bytes\n%!" (Bytes.length r); 34 - if Bytes.equal r raw_data then Printf.printf "MATCH!\n%!" 35 - else begin 36 - Printf.printf "MISMATCH. First few bytes:\nExpected: "; 37 - for i = 0 to min 15 (Bytes.length raw_data - 1) do 38 - Printf.printf "%02x " (Bytes.get_uint8 raw_data i) 39 - done; 40 - Printf.printf "\nGot: "; 41 - for i = 0 to min 15 (Bytes.length r - 1) do 42 - Printf.printf "%02x " (Bytes.get_uint8 r i) 43 - done; 44 - Printf.printf "\n%!" 45 - end 46 - | Error e -> Printf.printf "Error: %s\n%!" e 47 - with exn -> 48 - Printf.printf "Exception: %s\n%!" (Printexc.to_string exn); 49 - Printexc.print_backtrace stdout; 50 - flush stdout
-10
test/debug/dune
··· 1 - (executable 2 - (name debug) 3 - (libraries rice) 4 - (foreign_stubs 5 - (language c) 6 - (names libaec_stubs) 7 - (flags 8 - (:standard -I/opt/homebrew/opt/libaec/include))) 9 - (link_flags 10 - (-cclib -L/opt/homebrew/opt/libaec/lib -cclib -laec)))
-133
test/debug/libaec_stubs.c
··· 1 - /* C stubs wrapping libaec's aec_encode / aec_decode for OCaml interop tests. 2 - 3 - libaec implements the CCSDS 121.0-B-3 Adaptive Entropy Coding standard. 4 - We expose two OCaml functions: 5 - - caml_libaec_encode : bytes -> block_size:int -> bits_per_sample:int -> bytes 6 - - caml_libaec_decode : bytes -> block_size:int -> bits_per_sample:int 7 - -> expected_size:int -> bytes 8 - */ 9 - 10 - #include <caml/mlvalues.h> 11 - #include <caml/memory.h> 12 - #include <caml/alloc.h> 13 - #include <caml/fail.h> 14 - #include <string.h> 15 - #include <libaec.h> 16 - 17 - /* Encode raw samples using libaec. 18 - Arguments: data (bytes), block_size (int), bits_per_sample (int) 19 - Returns: compressed bytes */ 20 - CAMLprim value caml_libaec_encode(value v_data, value v_block_size, 21 - value v_bits_per_sample) { 22 - CAMLparam3(v_data, v_block_size, v_bits_per_sample); 23 - CAMLlocal1(v_result); 24 - 25 - unsigned char *data = (unsigned char *)Bytes_val(v_data); 26 - int data_len = caml_string_length(v_data); 27 - int block_size = Int_val(v_block_size); 28 - int bits_per_sample = Int_val(v_bits_per_sample); 29 - 30 - /* Allocate output buffer -- worst case is slightly larger than input */ 31 - int out_size = data_len * 2 + 1024; 32 - unsigned char *out_buf = (unsigned char *)caml_stat_alloc(out_size); 33 - 34 - struct aec_stream strm; 35 - memset(&strm, 0, sizeof(strm)); 36 - 37 - strm.bits_per_sample = bits_per_sample; 38 - strm.block_size = block_size; 39 - strm.rsi = block_size; 40 - strm.flags = AEC_DATA_MSB | AEC_DATA_PREPROCESS; 41 - 42 - /* Set byte width flags based on bits_per_sample */ 43 - if (bits_per_sample > 16) { 44 - strm.flags |= AEC_DATA_3BYTE; 45 - } 46 - 47 - strm.next_in = data; 48 - strm.avail_in = data_len; 49 - strm.next_out = out_buf; 50 - strm.avail_out = out_size; 51 - 52 - int ret = aec_encode_init(&strm); 53 - if (ret != AEC_OK) { 54 - caml_stat_free(out_buf); 55 - caml_failwith("libaec: aec_encode_init failed"); 56 - } 57 - 58 - ret = aec_encode(&strm, AEC_FLUSH); 59 - if (ret != AEC_OK) { 60 - aec_encode_end(&strm); 61 - caml_stat_free(out_buf); 62 - caml_failwith("libaec: aec_encode failed"); 63 - } 64 - 65 - int compressed_len = out_size - strm.avail_out; 66 - 67 - aec_encode_end(&strm); 68 - 69 - v_result = caml_alloc_string(compressed_len); 70 - memcpy(Bytes_val(v_result), out_buf, compressed_len); 71 - caml_stat_free(out_buf); 72 - 73 - CAMLreturn(v_result); 74 - } 75 - 76 - /* Decode compressed data using libaec. 77 - Arguments: data (bytes), block_size (int), bits_per_sample (int), 78 - expected_size (int) 79 - Returns: decompressed bytes */ 80 - CAMLprim value caml_libaec_decode(value v_data, value v_block_size, 81 - value v_bits_per_sample, 82 - value v_expected_size) { 83 - CAMLparam4(v_data, v_block_size, v_bits_per_sample, v_expected_size); 84 - CAMLlocal1(v_result); 85 - 86 - unsigned char *data = (unsigned char *)Bytes_val(v_data); 87 - int data_len = caml_string_length(v_data); 88 - int block_size = Int_val(v_block_size); 89 - int bits_per_sample = Int_val(v_bits_per_sample); 90 - int expected_size = Int_val(v_expected_size); 91 - 92 - unsigned char *out_buf = (unsigned char *)caml_stat_alloc(expected_size); 93 - 94 - struct aec_stream strm; 95 - memset(&strm, 0, sizeof(strm)); 96 - 97 - strm.bits_per_sample = bits_per_sample; 98 - strm.block_size = block_size; 99 - strm.rsi = block_size; 100 - strm.flags = AEC_DATA_MSB | AEC_DATA_PREPROCESS; 101 - 102 - if (bits_per_sample > 16) { 103 - strm.flags |= AEC_DATA_3BYTE; 104 - } 105 - 106 - strm.next_in = data; 107 - strm.avail_in = data_len; 108 - strm.next_out = out_buf; 109 - strm.avail_out = expected_size; 110 - 111 - int ret = aec_decode_init(&strm); 112 - if (ret != AEC_OK) { 113 - caml_stat_free(out_buf); 114 - caml_failwith("libaec: aec_decode_init failed"); 115 - } 116 - 117 - ret = aec_decode(&strm, AEC_FLUSH); 118 - if (ret != AEC_OK) { 119 - aec_decode_end(&strm); 120 - caml_stat_free(out_buf); 121 - caml_failwith("libaec: aec_decode failed"); 122 - } 123 - 124 - int decompressed_len = expected_size - strm.avail_out; 125 - 126 - aec_decode_end(&strm); 127 - 128 - v_result = caml_alloc_string(decompressed_len); 129 - memcpy(Bytes_val(v_result), out_buf, decompressed_len); 130 - caml_stat_free(out_buf); 131 - 132 - CAMLreturn(v_result); 133 - }