/alps/ufodecode

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/ufodecode

« back to all changes in this revision

Viewing changes to test/ipedec.c

  • Committer: Suren A. Chilingaryan
  • Date: 2012-10-23 00:05:26 UTC
  • mfrom: (39.1.12 upstream)
  • Revision ID: csa@dside.dyndns.org-20121023000526-ifuq3qa3ppzbygqr
Merge from mv

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#include <getopt.h>
9
9
#include <ufodecode.h>
10
10
 
 
11
typedef struct {
 
12
    int clear_frame;
 
13
    int dry_run;
 
14
    int verbose;
 
15
    int rows;
 
16
    int print_frame_rate;
 
17
    int print_num_rows;
 
18
    int cont;
 
19
    int superimpose;
 
20
} Options;
 
21
 
11
22
static int
12
23
read_raw_file(const char *filename, char **buffer, size_t *length)
13
24
{
14
 
    FILE *fp = fopen(filename, "rb"); 
 
25
    FILE *fp = fopen(filename, "rb");
15
26
    if (fp == NULL)
16
27
        return ENOENT;
17
28
 
38
49
static void
39
50
usage(void)
40
51
{
41
 
    printf("usage: ipedec [--num-rows=ROWS] [--clear-frame] FILE [FILE ...]\n\
 
52
    printf("usage: ipedec [OPTION]... FILE [FILE ...]\n\
42
53
Options:\n\
43
 
  -h, --help         Show this help message and exit\n\
44
 
  -v, --verbose      Print additional information on STDOUT\n\
45
 
  -r, --num-rows=N   N rows that are contained in the file\n\
46
 
  -c, --clear-frame  Clear the frame for each iteration\n");
 
54
  -h, --help                Show this help message and exit\n\
 
55
  -v, --verbose             Print additional information on STDOUT\n\
 
56
  -r, --num-rows=N          N rows that are contained in the file\n\
 
57
  -c, --clear-frame         Clear the frame for each iteration\n\
 
58
  -d, --dry-run             Do not save the frames\n\
 
59
  -s, --superimpose=C       Superimpose frames on top of the first with color C as borders\n\
 
60
  -f, --print-frame-rate    Print frame rate on STDOUT\n\
 
61
      --print-num-rows      Print number of rows on STDOUT\n\
 
62
      --continue            Continue decoding frames even when errors occur\n");
47
63
}
48
64
 
49
65
static void
122
138
}
123
139
 
124
140
static void
125
 
process_file(const char *filename, int rows, int clear_frame, int verbose)
 
141
superimpose_on_top (uint16_t *top, uint16_t *bottom, uint16_t color)
 
142
{
 
143
    int x, y;
 
144
    int in_void = top[0] == 0;
 
145
 
 
146
    for (y = 0; y < 1088; y++) {
 
147
        int offset = y * 2048;
 
148
 
 
149
        if ((in_void && top[offset] != 0) ||
 
150
            (!in_void && top[offset] == 0)) {
 
151
            in_void = 1 - in_void;
 
152
 
 
153
            for (x = 0; x < 2048; x++)
 
154
                top[offset++] = color;
 
155
        } 
 
156
        else {
 
157
            for (x = 0; x < 2048; x++, offset++) {
 
158
                if (top[offset] == 0) 
 
159
                    top[offset] = bottom[offset];
 
160
            }
 
161
        }
 
162
    }
 
163
}
 
164
 
 
165
static int
 
166
process_file(const char *filename, Options *opts)
126
167
{
127
168
    UfoDecoder      *decoder;
128
169
    UfoDecoderMeta   meta = {0};
129
170
    Timer           *timer;
130
171
    char            *buffer;
131
172
    size_t           num_bytes;
132
 
    int              error;
 
173
    size_t           frame_size;
 
174
    uint16_t        *orig;
133
175
    uint16_t        *pixels;
134
176
    uint32_t         time_stamp, old_time_stamp;
135
 
    int              n_frames = 0;
 
177
    int              n_frames;
 
178
    int              error = 0;
136
179
    FILE            *fp;
137
180
    char             output_name[256];
138
 
    
 
181
    float            mtime;
 
182
 
 
183
    frame_size = 2048 * 1088 * sizeof(uint16_t);
139
184
    error = read_raw_file(filename, &buffer, &num_bytes);
140
185
 
141
186
    if (error) {
142
187
        fprintf(stderr, "Error reading %s: %s\n", filename, strerror(error));
143
 
        return;
 
188
        return error;
144
189
    }
145
190
 
146
 
    decoder = ufo_decoder_new(rows, 2048, (uint32_t *) buffer, num_bytes);
 
191
    decoder = ufo_decoder_new(opts->rows, 2048, (uint32_t *) buffer, num_bytes);
147
192
 
148
193
    if (decoder == NULL) {
149
194
        fprintf(stderr, "Failed to initialize decoder\n");
150
 
        return;
151
 
    }
152
 
 
153
 
    snprintf(output_name, 256, "%s.raw", filename);
154
 
    fp = fopen(output_name, "wb");
155
 
 
156
 
    if (!fp) {
157
 
        fprintf(stderr, "Failed to open file for writing\n");
158
 
        return;
159
 
    }
 
195
        return 1;
 
196
    }
 
197
 
 
198
    if (!opts->dry_run) {
 
199
        snprintf(output_name, 256, "%s.raw", filename);
 
200
        fp = fopen(output_name, "wb");
 
201
 
 
202
        if (!fp) {
 
203
            fprintf(stderr, "Failed to open file for writing\n");
 
204
            return 1;
 
205
        }
 
206
    }
 
207
 
 
208
    if (opts->superimpose)
 
209
        orig = (uint16_t *) malloc(frame_size);
 
210
 
 
211
    pixels = (uint16_t *) malloc(frame_size);
160
212
 
161
213
    timer = timer_new ();
162
 
    pixels = (uint16_t *) malloc(2048 * 1088 * sizeof(uint16_t));
163
214
    n_frames = 0;
 
215
    old_time_stamp = 0;
164
216
 
165
217
    while (error != EIO) {
166
 
        if (clear_frame)
167
 
            memset(pixels, 0, 2048 * 1088 * sizeof(uint16_t));
 
218
        if (opts->clear_frame || opts->superimpose)
 
219
            memset(pixels, 0, frame_size);
168
220
 
169
221
        timer_start (timer);
170
222
        error = ufo_decoder_get_next_frame(decoder, &pixels, &meta);
171
223
        timer_stop (timer);
 
224
        n_frames++;
 
225
 
 
226
        if (opts->superimpose && n_frames == 1)
 
227
            memcpy (orig, pixels, frame_size);
172
228
 
173
229
        if (!error) {
174
 
            if (verbose) {
 
230
            if (opts->verbose) {
175
231
                printf("Status for frame %i\n", n_frames);
176
232
                print_meta_data (&meta);
177
233
            }
178
234
 
179
 
            n_frames++;
180
 
            fwrite(pixels, sizeof(uint16_t), 2048 * 1088, fp);
181
 
        }
182
 
        else if (error != EIO)
183
 
            fprintf(stderr, "Failed to decode frame %i\n", n_frames); 
184
 
    }
185
 
 
186
 
    fclose(fp);
187
 
 
188
 
    float mtime = timer->seconds * 1000.0 + timer->useconds / 1000.0;
189
 
    printf("Decoded %i frames in %.5fms\n", n_frames, mtime);
 
235
            if (opts->print_frame_rate) {
 
236
                uint32_t diff = 80 * (meta.time_stamp - old_time_stamp);
 
237
 
 
238
                printf("%-6d", 1000000000 / diff);
 
239
                old_time_stamp = meta.time_stamp;
 
240
            }
 
241
 
 
242
            if (opts->print_num_rows)
 
243
                printf("%d", meta.n_rows); 
 
244
 
 
245
            if (opts->print_frame_rate || opts->print_num_rows)
 
246
                printf("\n");
 
247
 
 
248
            if (opts->superimpose)
 
249
                superimpose_on_top (pixels, orig, opts->superimpose);
 
250
 
 
251
            if (!opts->dry_run)
 
252
                fwrite(pixels, sizeof(uint16_t), 2048 * 1088, fp);
 
253
        }
 
254
        else if (error != EIO) {
 
255
            fprintf(stderr, "Failed to decode frame %i\n", n_frames);
 
256
 
 
257
            if (opts->cont) {
 
258
                /* Save the frame even though we know it is corrupted */
 
259
                if (!opts->dry_run)
 
260
                    fwrite(pixels, sizeof(uint16_t), 2048 * 1088, fp);
 
261
            }
 
262
            else
 
263
                break;
 
264
        }
 
265
    }
 
266
 
 
267
    if (!opts->dry_run)
 
268
        fclose(fp);
 
269
 
 
270
    if (opts->verbose) {
 
271
        mtime = timer->seconds * 1000.0 + timer->useconds / 1000.0;
 
272
        printf("Decoded %i frames in %.5fms\n", n_frames, mtime);
 
273
    }
190
274
 
191
275
    free(pixels);
192
276
    free(buffer);
193
277
    timer_destroy (timer);
194
278
    ufo_decoder_free(decoder);
 
279
 
 
280
    return error == EIO ? 0 : error;
195
281
}
196
282
 
197
283
int main(int argc, char const* argv[])
198
284
{
199
285
    int getopt_ret, index;
200
286
 
 
287
    enum {
 
288
        SUPERIMPOSE  = 's',
 
289
        CLEAR_FRAME  = 'c',
 
290
        DRY_RUN      = 'd',
 
291
        FRAME_RATE   = 'f',
 
292
        HELP         = 'h',
 
293
        SET_NUM_ROWS = 'r', 
 
294
        VERBOSE      = 'v',
 
295
        CONTINUE,
 
296
        NUM_ROWS
 
297
    };
 
298
 
201
299
    static struct option long_options[] = {
202
 
        { "num-rows", required_argument, 0, 'r' },
203
 
        { "clear-frame", no_argument, 0, 'c' },
204
 
        { "verbose", no_argument, 0, 'v' },
205
 
        { "help", no_argument, 0, 'h' },
 
300
        { "num-rows",           required_argument, 0, SET_NUM_ROWS },
 
301
        { "clear-frame",        no_argument, 0, CLEAR_FRAME },
 
302
        { "verbose",            no_argument, 0, VERBOSE },
 
303
        { "help",               no_argument, 0, HELP },
 
304
        { "dry-run",            no_argument, 0, DRY_RUN },
 
305
        { "print-frame-rate",   no_argument, 0, FRAME_RATE },
 
306
        { "continue",           no_argument, 0, CONTINUE },
 
307
        { "print-num-rows",     no_argument, 0, NUM_ROWS },
 
308
        { "superimpose",        required_argument, 0, SUPERIMPOSE },
206
309
        { 0, 0, 0, 0 }
207
310
    };
208
311
 
209
 
    int clear_frame = 0;
210
 
    int verbose = 0;
211
 
    int rows = 1088;
 
312
    static Options opts = {
 
313
        .rows = 1088,
 
314
        .verbose = 0,
 
315
        .dry_run = 0,
 
316
        .clear_frame = 0,
 
317
        .print_frame_rate = 0,
 
318
        .print_num_rows = 0,
 
319
        .cont = 0,
 
320
        .superimpose = 0
 
321
    };
212
322
 
213
 
    while ((getopt_ret = getopt_long(argc, (char *const *) argv, "r:cvh", long_options, &index)) != -1) {
 
323
    while ((getopt_ret = getopt_long(argc, (char *const *) argv, "r:s:cvhdf", long_options, &index)) != -1) {
214
324
        switch (getopt_ret) {
215
 
            case 'r': 
216
 
                rows = atoi(optarg);
217
 
                break;
218
 
            case 'c':
219
 
                clear_frame = 1;
220
 
                break;
221
 
            case 'v':
222
 
                verbose = 1;
223
 
                break;
224
 
            case 'h':
 
325
            case SET_NUM_ROWS:
 
326
                opts.rows = atoi(optarg);
 
327
                break;
 
328
            case CLEAR_FRAME:
 
329
                opts.clear_frame = 1;
 
330
                break;
 
331
            case VERBOSE:
 
332
                opts.verbose = 1;
 
333
                break;
 
334
            case HELP:
225
335
                usage();
226
336
                return 0;
 
337
            case DRY_RUN:
 
338
                opts.dry_run = 1;
 
339
                break;
 
340
            case FRAME_RATE:
 
341
                opts.print_frame_rate = 1;
 
342
                break;
 
343
            case CONTINUE:
 
344
                opts.cont = 1;
 
345
                break;
 
346
            case NUM_ROWS:
 
347
                opts.print_num_rows = 1;
 
348
            case SUPERIMPOSE:
 
349
                opts.superimpose = atoi(optarg);
 
350
                break;
227
351
            default:
228
352
                break;
229
 
        } 
 
353
        }
 
354
    }
 
355
 
 
356
    if (opts.clear_frame && opts.superimpose) {
 
357
        fprintf(stderr, "Error: --clear-frame and --superimpose are mutual exclusive\n");
 
358
        return 1;
230
359
    }
231
360
 
232
361
    if (optind == argc) {
234
363
        return 1;
235
364
    }
236
365
 
237
 
    while (optind < argc)
238
 
        process_file(argv[optind++], rows, clear_frame, verbose);
 
366
    while (optind < argc) {
 
367
        int errcode = process_file(argv[optind++], &opts);
 
368
 
 
369
        if (errcode != 0)
 
370
            return errcode;
 
371
    }
239
372
 
240
373
    return 0;
241
374
}