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
38struct image {
39    /* if true, decode file in place instead of creating a new output file */
40    bool inplace;
41    /* if true, assume input is a sparse file */
42    bool sparse;
43    /* if true, print more verbose information to stderr */
44    bool verbose;
45    const char *fec_filename;
46    int fec_fd;
47    int inp_fd;
48    /* the number of Reed-Solomon generator polynomial roots, also the number
49       of parity bytes generated for each N bytes in RS(M, N) */
50    int roots;
51    /* for RS(M, N), N = M - roots */
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
66struct image_proc_ctx;
67typedef void (*image_proc_func)(image_proc_ctx *);
68
69struct 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
80extern bool image_load(const std::vector<std::string>& filename, image *ctx);
81extern bool image_save(const std::string& filename, image *ctx);
82
83extern bool image_ecc_new(const std::string& filename, image *ctx);
84extern bool image_ecc_load(const std::string& filename, image *ctx);
85extern bool image_ecc_save(image *ctx);
86
87extern bool image_process(image_proc_func f, image *ctx);
88
89extern void image_init(image *ctx);
90extern void image_free(image *ctx);
91
92inline 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
103inline 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 // __FEC_H__
116