summaryrefslogtreecommitdiffstats
path: root/test/ipedec.c
blob: 52aebb03b17527a95a8ba4b577f3a18077f2fe63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <ufodecode.h>
#include "timer.h"

static const int MAX_ROWS = 2048;

typedef struct {
    int clear_frame;
    int dry_run;
    int verbose;
    int rows;
    int print_frame_rate;
    int print_num_rows;
    int cont;
    int convert_bayer;
} Options;

static int
read_raw_file(const char *filename, char **buffer, size_t *length)
{
    FILE *fp = fopen(filename, "rb");
    if (fp == NULL)
        return ENOENT;

    fseek(fp, 0, SEEK_END);
    *length = ftell(fp);
    rewind(fp);

    *buffer = (char *) malloc(*length);

    if (*buffer == NULL) {
        fclose(fp);
        return ENOMEM;
    }

    size_t buffer_length = fread(*buffer, 1, *length, fp);
    fclose(fp);
    if (buffer_length != *length) {
        free(*buffer);
        return ERANGE;
    }
    return 0;
}

static void
usage(void)
{
    printf("usage: ipedec [OPTION]... FILE [FILE ...]\n\
Options:\n\
  -h, --help                Show this help message and exit\n\
  -v, --verbose             Print additional information on STDOUT\n\
  -r, --num-rows=N          N rows that are contained in the file\n\
  -c, --clear-frame         Clear the frame for each iteration\n\
  -d, --dry-run             Do not save the frames\n\
  -f, --print-frame-rate    Print frame rate on STDOUT\n\
      --print-num-rows      Print number of rows on STDOUT\n\
      --continue            Continue decoding frames even when errors occur\n\
      --convert-bayer       Convert Bayer pattern to 24 Bit RGB\n");
}

static void
print_meta_data (UfoDecoderMeta *meta)
{
    printf("  frame_number    = %i\n", meta->frame_number);
    printf("  time_stamp      = %i\n", meta->time_stamp);
    printf("  output_mode     = %i\n", meta->output_mode);
    printf("  adc_resolution  = %i\n", meta->adc_resolution);
    printf("  n_rows          = %i\n", meta->n_rows);
    printf("  n_skipped_rows  = %i\n", meta->n_skipped_rows);

    printf("  status1\n");
    printf("    fsm_master_readout = %i\n", meta->status1.desc.fsm_master_readout);
    printf("    fsm_daq         = %i\n", meta->status1.desc.fsm_daq);
    printf("    pixel_full      = %i\n", meta->status1.desc.pixel_full);
    printf("    control_lock    = %i\n", meta->status1.desc.control_lock);
    printf("    data_lock       = %i\n", meta->status1.desc.data_lock);

    printf("  status2\n");
    printf("    end_of_frames   = %i\n", meta->status2.desc.end_of_frames);
    printf("    busy_or         = %i\n", meta->status2.desc.busy_or);
    printf("    busy_ddr        = %i\n", meta->status2.desc.busy_ddr);
    printf("    busy_interl     = %i\n", meta->status2.desc.busy_interl);
    printf("    error_status    = %i\n", meta->status2.desc.error_status);
    printf("    data_fifo_read_count = %i\n", meta->status2.desc.data_fifo_read_count);
    printf("    data_fifo_full       = %i\n", meta->status2.desc.data_fifo_full);
    printf("    data_fifo_empty      = %i\n", meta->status2.desc.data_fifo_empty);
    printf("    ddr_fifo_write_count = %i\n", meta->status2.desc.ddr_fifo_write_count);
    printf("    ddr_fifo_full        = %i\n", meta->status2.desc.ddr_fifo_full);
    printf("    ddr_fifo_empty       = %i\n", meta->status2.desc.ddr_fifo_empty);

    printf("  status3\n");
    printf("    row_counter     = %i\n", meta->status3.desc.row_counter);
    printf("    pixel_counter   = %i\n", meta->status3.desc.pixel_counter);
    printf("    ddr_read        = %i\n", meta->status3.desc.ddr_read);
    printf("    ddr_write       = %i\n", meta->status3.desc.ddr_write);
    printf("    ddr_arbiter     = %i\n", meta->status3.desc.ddr_arbiter);
    printf("\n");
}


static int
process_file(const char *filename, Options *opts)
{
    UfoDecoder      *decoder;
    UfoDecoderMeta   meta = {0};
    Timer           *timer;
    char            *buffer;
    size_t           num_bytes;
    uint16_t        *pixels;
    uint8_t         *rgb_pixels;
    uint32_t         time_stamp, old_time_stamp;
    int              n_frames;
    int              error = 0;
    FILE            *fp;
    char             output_name[256];
    float            mtime;

    error = read_raw_file(filename, &buffer, &num_bytes);

    if (error) {
        fprintf(stderr, "Error reading %s: %s\n", filename, strerror(error));
        return error;
    }

    decoder = ufo_decoder_new(opts->rows, 2048, (uint32_t *) buffer, num_bytes);

    if (decoder == NULL) {
        fprintf(stderr, "Failed to initialize decoder\n");
        return 1;
    }

    if (!opts->dry_run) {
        snprintf(output_name, 256, "%s.raw", filename);
        fp = fopen(output_name, "wb");

        if (!fp) {
            fprintf(stderr, "Failed to open file for writing\n");
            return 1;
        }
    }

    if (opts->convert_bayer)
        rgb_pixels = malloc (2048 * MAX_ROWS * 3);

    timer = timer_new ();
    pixels = (uint16_t *) malloc(2048 * MAX_ROWS * sizeof(uint16_t));
    n_frames = 0;
    old_time_stamp = 0;

    while (error != EIO) {
        timer_start (timer);
        error = ufo_decoder_get_next_frame(decoder, &pixels, &meta);

        if (meta.n_rows == 0)
            meta.n_rows = opts->rows;

        timer_stop (timer);
        n_frames++;

        if (!error) {
            if (opts->verbose) {
                printf("Status for frame %i\n", n_frames);
                print_meta_data (&meta);
            }

            if (opts->print_frame_rate) {
                uint32_t diff = 80 * (meta.time_stamp - old_time_stamp);

                printf("%-6d", 1000000000 / diff);
                old_time_stamp = meta.time_stamp;
            }

            if (opts->print_num_rows)
                printf("%d", meta.n_rows); 

            if (opts->print_frame_rate || opts->print_num_rows)
                printf("\n");

            if (opts->clear_frame)
                memset(pixels, 0, 2048 * meta.n_rows * sizeof(uint16_t));

            if (!opts->dry_run) {
                if (opts->convert_bayer) {
                    ufo_convert_bayer_to_rgb (pixels, rgb_pixels, 2048, meta.n_rows);
                    fwrite (rgb_pixels, sizeof(uint8_t), 2048 * meta.n_rows * 3, fp);
                }
                else
                    fwrite(pixels, sizeof(uint16_t), 2048 * meta.n_rows, fp);
            }
        }
        else if (error != EIO) {
            fprintf(stderr, "Failed to decode frame %i\n", n_frames);

            if (opts->cont) {
                /* Save the frame even though we know it is corrupted */
                if (!opts->dry_run) {
                    if (opts->convert_bayer) {
                        ufo_convert_bayer_to_rgb (pixels, rgb_pixels, 2048, meta.n_rows);
                        fwrite (rgb_pixels, sizeof(uint8_t), 2048 * meta.n_rows * 3, fp);
                    }
                    else
                        fwrite(pixels, sizeof(uint16_t), 2048 * meta.n_rows, fp);
                }
            }
            else
                break;
        }
    }

    if (!opts->dry_run)
        fclose(fp);

    if (opts->verbose) {
        mtime = timer->seconds * 1000.0 + timer->useconds / 1000.0;
        printf("Decoded %i frames in %.5fms\n", n_frames, mtime);
    }

    if (opts->convert_bayer)
        free (rgb_pixels);

    free(pixels);
    free(buffer);
    timer_destroy (timer);
    ufo_decoder_free(decoder);

    return error == EIO ? 0 : error;
}

int main(int argc, char const* argv[])
{
    int getopt_ret, index;

    enum {
        CLEAR_FRAME  = 'c',
        DRY_RUN      = 'd',
        FRAME_RATE   = 'f',
        HELP         = 'h',
        SET_NUM_ROWS = 'r', 
        VERBOSE      = 'v',
        CONTINUE,
        NUM_ROWS,
        CONVERT_BAYER,
    };

    static struct option long_options[] = {
        { "num-rows",           required_argument, 0, SET_NUM_ROWS },
        { "clear-frame",        no_argument, 0, CLEAR_FRAME },
        { "verbose",            no_argument, 0, VERBOSE },
        { "help",               no_argument, 0, HELP },
        { "dry-run",            no_argument, 0, DRY_RUN },
        { "print-frame-rate",   no_argument, 0, FRAME_RATE },
        { "continue",           no_argument, 0, CONTINUE },
        { "print-num-rows",     no_argument, 0, NUM_ROWS },
        { "convert-bayer",      no_argument, 0, CONVERT_BAYER },
        { 0, 0, 0, 0 }
    };

    static Options opts = {
        .rows = 1088,
        .verbose = 0,
        .dry_run = 0,
        .clear_frame = 0,
        .print_frame_rate = 0,
        .print_num_rows = 0,
        .cont = 0,
        .convert_bayer = 0
    };

    while ((getopt_ret = getopt_long(argc, (char *const *) argv, "r:cvhdf", long_options, &index)) != -1) {
        switch (getopt_ret) {
            case SET_NUM_ROWS:
                opts.rows = atoi(optarg);
                break;
            case CLEAR_FRAME:
                opts.clear_frame = 1;
                break;
            case VERBOSE:
                opts.verbose = 1;
                break;
            case HELP:
                usage();
                return 0;
            case DRY_RUN:
                opts.dry_run = 1;
                break;
            case FRAME_RATE:
                opts.print_frame_rate = 1;
                break;
            case CONTINUE:
                opts.cont = 1;
                break;
            case NUM_ROWS:
                opts.print_num_rows = 1;
                break;
            case CONVERT_BAYER:
                opts.convert_bayer = 1;
                break;
            default:
                break;
        }
    }

    if (optind == argc) {
        printf("ipedec: no input files\n");
        return 1;
    }

    while (optind < argc) {
        int errcode = process_file(argv[optind++], &opts);

        if (errcode != 0)
            return errcode;
    }

    return 0;
}