1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 /* if true, decode file in place instead of creating a new output file */
40 bool inplace;
41 /* if true, use memory mapping instead of copying all input into memory */
42 bool mmap;
43 /* if true, assume input is a sparse file */
44 bool sparse;
45 /* if true, print more verbose information to stderr */
46 bool verbose;
47 const char *fec_filename;
48 int fec_fd;
49 int inp_fd;
50 /* the number of Reed-Solomon generator polynomial roots, also the number
51 of parity bytes generated for each N bytes in RS(M, N) */
52 int roots;
53 /* for RS(M, N), N = M - roots */
54 int rs_n;
55 int threads;
56 uint32_t fec_size;
57 uint64_t blocks;
58 uint64_t inp_size;
59 uint64_t pos;
60 uint64_t rounds;
61 uint64_t rv;
62 uint8_t *fec;
63 uint8_t *fec_mmap_addr;
64 uint8_t *input;
65 uint8_t *output;
66 };
67
68 struct image_proc_ctx;
69 typedef void (*image_proc_func)(image_proc_ctx *);
70
71 struct image_proc_ctx {
72 image_proc_func func;
73 int id;
74 image *ctx;
75 uint64_t rv;
76 uint64_t fec_pos;
77 uint64_t start;
78 uint64_t end;
79 void *rs;
80 };
81
82 extern bool image_load(const std::vector<std::string>& filename, image *ctx,
83 bool output_needed);
84 extern bool image_save(const std::string& filename, image *ctx);
85
86 extern bool image_ecc_new(const std::string& filename, image *ctx);
87 extern bool image_ecc_load(const std::string& filename, image *ctx);
88 extern bool image_ecc_save(image *ctx);
89
90 extern bool image_process(image_proc_func f, image *ctx);
91
92 extern void image_init(image *ctx);
93 extern void image_free(image *ctx);
94
image_get_interleaved_byte(uint64_t i,image * ctx)95 inline uint8_t image_get_interleaved_byte(uint64_t i, image *ctx)
96 {
97 uint64_t offset = fec_ecc_interleave(i, ctx->rs_n, ctx->rounds);
98
99 if (unlikely(offset >= ctx->inp_size)) {
100 return 0;
101 }
102
103 return ctx->input[offset];
104 }
105
image_set_interleaved_byte(uint64_t i,image * ctx,uint8_t value)106 inline void image_set_interleaved_byte(uint64_t i, image *ctx,
107 uint8_t value)
108 {
109 uint64_t offset = fec_ecc_interleave(i, ctx->rs_n, ctx->rounds);
110
111 if (unlikely(offset >= ctx->inp_size)) {
112 assert(value == 0);
113 } else if (ctx->output && ctx->output[offset] != value) {
114 ctx->output[offset] = value;
115 }
116 }
117
118 #endif // __FEC_H__
119