| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef __FEC_H__ |
| 18 |
#define __FEC_H__ |
| 19 |
|
| 20 |
#include <utils/Compat.h> |
| 21 |
#include <string> |
| 22 |
#include <vector> |
| 23 |
#include <fec/io.h> |
| 24 |
#include <fec/ecc.h> |
| 25 |
|
| 26 |
#define IMAGE_MIN_THREADS 1 |
| 27 |
#define IMAGE_MAX_THREADS 128 |
| 28 |
|
| 29 |
#define INFO(x...) \ |
| 30 |
fprintf(stderr, x); |
| 31 |
#define FATAL(x...) { \ |
| 32 |
fprintf(stderr, x); \ |
| 33 |
exit(1); \ |
| 34 |
} |
| 35 |
|
| 36 |
#define unlikely(x) __builtin_expect(!!(x), 0) |
| 37 |
|
| 38 |
struct image { |
| 39 |
|
| 40 |
bool inplace; |
| 41 |
|
| 42 |
bool sparse; |
| 43 |
|
| 44 |
bool verbose; |
| 45 |
const char *fec_filename; |
| 46 |
int fec_fd; |
| 47 |
int inp_fd; |
| 48 |
|
| 49 |
|
| 50 |
int roots; |
| 51 |
|
| 52 |
int rs_n; |
| 53 |
int threads; |
| 54 |
uint32_t fec_size; |
| 55 |
uint32_t padding; |
| 56 |
uint64_t blocks; |
| 57 |
uint64_t inp_size; |
| 58 |
uint64_t pos; |
| 59 |
uint64_t rounds; |
| 60 |
uint64_t rv; |
| 61 |
uint8_t *fec; |
| 62 |
uint8_t *input; |
| 63 |
uint8_t *output; |
| 64 |
}; |
| 65 |
|
| 66 |
struct image_proc_ctx; |
| 67 |
typedef void (*image_proc_func)(image_proc_ctx *); |
| 68 |
|
| 69 |
struct image_proc_ctx { |
| 70 |
image_proc_func func; |
| 71 |
int id; |
| 72 |
image *ctx; |
| 73 |
uint64_t rv; |
| 74 |
uint64_t fec_pos; |
| 75 |
uint64_t start; |
| 76 |
uint64_t end; |
| 77 |
void *rs; |
| 78 |
}; |
| 79 |
|
| 80 |
extern bool image_load(const std::vector<std::string>& filename, image *ctx); |
| 81 |
extern bool image_save(const std::string& filename, image *ctx); |
| 82 |
|
| 83 |
extern bool image_ecc_new(const std::string& filename, image *ctx); |
| 84 |
extern bool image_ecc_load(const std::string& filename, image *ctx); |
| 85 |
extern bool image_ecc_save(image *ctx); |
| 86 |
|
| 87 |
extern bool image_process(image_proc_func f, image *ctx); |
| 88 |
|
| 89 |
extern void image_init(image *ctx); |
| 90 |
extern void image_free(image *ctx); |
| 91 |
|
| 92 |
inline uint8_t image_get_interleaved_byte(uint64_t i, image *ctx) |
| 93 |
{ |
| 94 |
uint64_t offset = fec_ecc_interleave(i, ctx->rs_n, ctx->rounds); |
| 95 |
|
| 96 |
if (unlikely(offset >= ctx->inp_size)) { |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
return ctx->input[offset]; |
| 101 |
} |
| 102 |
|
| 103 |
inline void image_set_interleaved_byte(uint64_t i, image *ctx, |
| 104 |
uint8_t value) |
| 105 |
{ |
| 106 |
uint64_t offset = fec_ecc_interleave(i, ctx->rs_n, ctx->rounds); |
| 107 |
|
| 108 |
if (unlikely(offset >= ctx->inp_size)) { |
| 109 |
assert(value == 0); |
| 110 |
} else if (ctx->output && ctx->output[offset] != value) { |
| 111 |
ctx->output[offset] = value; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
#endif |