/alps/ufodecode

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/ufodecode

« back to all changes in this revision

Viewing changes to test/ipedec.c

  • Committer: Matthias Vogelgesang
  • Date: 2012-07-17 14:28:58 UTC
  • Revision ID: matthias.vogelgesang@kit.edu-20120717142858-9gsb7mux5vttdt31
Commit version 0.2 of libufodecode

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
 
 
12
 
static int read_raw_file(const char *filename, char **buffer, size_t *length)
 
11
static int
 
12
read_raw_file(const char *filename, char **buffer, size_t *length)
13
13
{
14
14
    FILE *fp = fopen(filename, "rb"); 
15
15
    if (fp == NULL)
35
35
    return 0;
36
36
}
37
37
 
38
 
static void usage(void)
 
38
static void
 
39
usage(void)
39
40
{
40
41
    printf("usage: ipedec [--num-rows=ROWS] [--clear-frame] FILE [FILE ...]\n\
41
42
Options:\n\
45
46
  -c, --clear-frame  Clear the frame for each iteration\n");
46
47
}
47
48
 
48
 
static void process_file(const char *filename, int rows, int clear_frame, int verbose)
49
 
{
50
 
    char *buffer = NULL;
51
 
    size_t num_bytes = 0;
52
 
    int err = 0;
53
 
    uint16_t *pixels = (uint16_t *) malloc(2048 * 1088 * sizeof(uint16_t));
54
 
    uint32_t num_rows, frame_number, time_stamp, old_time_stamp = 0;
55
 
    int num_frames = 0;
56
 
    struct timeval start, end;
57
 
    long seconds = 0L, useconds = 0L;
58
 
    int error = read_raw_file(filename, &buffer, &num_bytes);
 
49
static void
 
50
print_meta_data (UfoDecoderMeta *meta)
 
51
{
 
52
    printf("  frame_number    = %i\n", meta->frame_number);
 
53
    printf("  time_stamp      = %i\n", meta->time_stamp);
 
54
    printf("  n_rows          = %i\n", meta->n_rows);
 
55
    printf("  n_skipped_rows  = %i\n", meta->n_skipped_rows);
 
56
 
 
57
    printf("  status1\n");
 
58
    printf("    fsm_master_readout = %i\n", meta->status1.desc.fsm_master_readout);
 
59
    printf("    fsm_daq         = %i\n", meta->status1.desc.fsm_daq);
 
60
    printf("    pixel_full      = %i\n", meta->status1.desc.pixel_full);
 
61
    printf("    control_lock    = %i\n", meta->status1.desc.control_lock);
 
62
    printf("    data_lock       = %i\n", meta->status1.desc.data_lock);
 
63
 
 
64
    printf("  status2\n");
 
65
    printf("    end_of_frames   = %i\n", meta->status2.desc.end_of_frames);
 
66
    printf("    busy_or         = %i\n", meta->status2.desc.busy_or);
 
67
    printf("    busy_ddr        = %i\n", meta->status2.desc.busy_ddr);
 
68
    printf("    busy_interl     = %i\n", meta->status2.desc.busy_interl);
 
69
    printf("    error_status    = %i\n", meta->status2.desc.error_status);
 
70
    printf("    data_fifo_read_count = %i\n", meta->status2.desc.data_fifo_read_count);
 
71
    printf("    data_fifo_full       = %i\n", meta->status2.desc.data_fifo_full);
 
72
    printf("    data_fifo_empty      = %i\n", meta->status2.desc.data_fifo_empty);
 
73
    printf("    ddr_fifo_write_count = %i\n", meta->status2.desc.ddr_fifo_write_count);
 
74
    printf("    ddr_fifo_full        = %i\n", meta->status2.desc.ddr_fifo_full);
 
75
    printf("    ddr_fifo_empty       = %i\n", meta->status2.desc.ddr_fifo_empty);
 
76
 
 
77
    printf("  status3\n");
 
78
    printf("    row_counter     = %i\n", meta->status3.desc.row_counter);
 
79
    printf("    pixel_counter   = %i\n", meta->status3.desc.pixel_counter);
 
80
    printf("    ddr_read        = %i\n", meta->status3.desc.ddr_read);
 
81
    printf("    ddr_write       = %i\n", meta->status3.desc.ddr_write);
 
82
    printf("    ddr_arbiter     = %i\n", meta->status3.desc.ddr_arbiter);
 
83
    printf("\n");
 
84
}
 
85
 
 
86
typedef struct {
 
87
    struct timeval  start;
 
88
    long            seconds;
 
89
    long            useconds;
 
90
} Timer;
 
91
 
 
92
static Timer *
 
93
timer_new (void)
 
94
{
 
95
    Timer *t = (Timer *) malloc (sizeof (Timer));
 
96
    t->seconds = t->useconds = 0L;
 
97
    return t;
 
98
}
 
99
 
 
100
static void
 
101
timer_destroy (Timer *t)
 
102
{
 
103
    free (t);
 
104
}
 
105
 
 
106
static void
 
107
timer_start (Timer *t)
 
108
{
 
109
    gettimeofday(&t->start, NULL);
 
110
}
 
111
 
 
112
static void
 
113
timer_stop (Timer *t)
 
114
{
 
115
    struct timeval end;
 
116
 
 
117
    gettimeofday(&end, NULL);
 
118
    t->seconds += end.tv_sec - t->start.tv_sec;
 
119
    t->useconds += end.tv_usec - t->start.tv_usec;
 
120
}
 
121
 
 
122
static void
 
123
process_file(const char *filename, int rows, int clear_frame, int verbose)
 
124
{
 
125
    UfoDecoder      *decoder;
 
126
    UfoDecoderMeta   meta = {0};
 
127
    Timer           *timer;
 
128
    char            *buffer;
 
129
    size_t           num_bytes;
 
130
    int              error;
 
131
    uint16_t        *pixels;
 
132
    uint32_t         time_stamp, old_time_stamp;
 
133
    int              n_frames = 0;
 
134
    FILE            *fp;
 
135
    char             output_name[256];
 
136
    
 
137
    error = read_raw_file(filename, &buffer, &num_bytes);
59
138
 
60
139
    if (error) {
61
140
        fprintf(stderr, "Error reading %s: %s\n", filename, strerror(error));
62
141
        return;
63
142
    }
64
143
 
65
 
    ufo_decoder decoder = ufo_decoder_new(rows, 2048, (uint32_t *) buffer, num_bytes);
 
144
    decoder = ufo_decoder_new(rows, 2048, (uint32_t *) buffer, num_bytes);
66
145
 
67
 
    if (!decoder) {
 
146
    if (decoder == NULL) {
68
147
        fprintf(stderr, "Failed to initialize decoder\n");
69
148
        return;
70
149
    }
71
150
 
72
 
    char output_name[256];
73
151
    snprintf(output_name, 256, "%s.raw", filename);
74
 
    FILE *fp = fopen(output_name, "wb");
 
152
    fp = fopen(output_name, "wb");
75
153
 
76
154
    if (!fp) {
77
155
        fprintf(stderr, "Failed to open file for writing\n");
78
156
        return;
79
157
    }
80
158
 
81
 
    while (err != EIO) {
 
159
    timer = timer_new ();
 
160
    pixels = (uint16_t *) malloc(2048 * 1088 * sizeof(uint16_t));
 
161
    n_frames = 0;
 
162
 
 
163
    while (error != EIO) {
82
164
        if (clear_frame)
83
165
            memset(pixels, 0, 2048 * 1088 * sizeof(uint16_t));
84
166
 
85
 
        gettimeofday(&start, NULL);
86
 
        err = ufo_decoder_get_next_frame(decoder, &pixels, &num_rows, &frame_number, &time_stamp, NULL);
87
 
        gettimeofday(&end, NULL);
88
 
 
89
 
        if (verbose) {
90
 
            int time_stamp_diff = 80 * (time_stamp - old_time_stamp);
91
 
 
92
 
            if (time_stamp_diff != 0)
93
 
                printf(" %d\t %d\n", 1000000000 / time_stamp_diff, num_rows);
94
 
 
95
 
            old_time_stamp = time_stamp;
96
 
        }
97
 
 
98
 
        if (!err) {
99
 
            num_frames++;
100
 
            seconds += end.tv_sec - start.tv_sec;
101
 
            useconds += end.tv_usec - start.tv_usec;
 
167
        timer_start (timer);
 
168
        error = ufo_decoder_get_next_frame(decoder, &pixels, &meta);
 
169
        timer_stop (timer);
 
170
 
 
171
        if (!error) {
 
172
            if (verbose) {
 
173
                printf("Status for frame %i\n", n_frames);
 
174
                print_meta_data (&meta);
 
175
            }
 
176
 
 
177
            n_frames++;
102
178
            fwrite(pixels, sizeof(uint16_t), 2048 * 1088, fp);
103
179
        }
104
 
        else if (err != EIO)
105
 
            fprintf(stderr, "Failed to decode frame %i\n", num_frames); 
 
180
        else if (error != EIO)
 
181
            fprintf(stderr, "Failed to decode frame %i\n", n_frames); 
106
182
    }
107
183
 
108
184
    fclose(fp);
109
185
 
110
 
    float mtime = seconds * 1000.0 + useconds / 1000.0;
111
 
    printf("Decoded %i frames in %.5fms\n", num_frames, mtime);
 
186
    float mtime = timer->seconds * 1000.0 + timer->useconds / 1000.0;
 
187
    printf("Decoded %i frames in %.5fms\n", n_frames, mtime);
112
188
 
113
189
    free(pixels);
 
190
    free(buffer);
 
191
    timer_destroy (timer);
114
192
    ufo_decoder_free(decoder);
115
 
    free(buffer);
116
193
}
117
194
 
118
195
int main(int argc, char const* argv[])