forked from fsphil/ssdv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
222 lines (188 loc) · 5.47 KB
/
main.c
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
/* SSDV - Slow Scan Digital Video */
/*=======================================================================*/
/* Copyright 2011-2016 Philip Heron <[email protected]> */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "ssdv.h"
void exit_usage()
{
fprintf(stderr,
"Usage: ssdv [-e|-d] [-n] [-t <percentage>] [-c <callsign>] [-i <id>] [-q <level>] [<in file>] [<out file>]\n"
"\n"
" -e Encode JPEG to SSDV packets.\n"
" -d Decode SSDV packets to JPEG.\n"
"\n"
" -n Encode packets with no FEC.\n"
" -t For testing, drops the specified percentage of packets while decoding.\n"
" -c Set the callign. Accepts A-Z 0-9 and space, up to 6 characters.\n"
" -i Set the image ID (0-255).\n"
" -q Set the JPEG quality level (0 to 7, defaults to 4).\n"
" -v Print data for each packet decoded.\n"
"\n");
exit(-1);
}
int main(int argc, char *argv[])
{
int c, i;
FILE *fin = stdin;
FILE *fout = stdout;
char encode = -1;
char type = SSDV_TYPE_NORMAL;
int droptest = 0;
int verbose = 0;
int errors;
char callsign[7];
uint8_t image_id = 0;
int8_t quality = 4;
ssdv_t ssdv;
uint8_t pkt[SSDV_PKT_SIZE], b[128], *jpeg;
size_t jpeg_length;
callsign[0] = '\0';
opterr = 0;
while((c = getopt(argc, argv, "ednc:i:q:t:v")) != -1)
{
switch(c)
{
case 'e': encode = 1; break;
case 'd': encode = 0; break;
case 'n': type = SSDV_TYPE_NOFEC; break;
case 'c':
if(strlen(optarg) > 6)
fprintf(stderr, "Warning: callsign is longer than 6 characters.\n");
strncpy(callsign, optarg, 6);
callsign[6] = '\0';
break;
case 'i': image_id = atoi(optarg); break;
case 'q': quality = atoi(optarg); break;
case 't': droptest = atoi(optarg); break;
case 'v': verbose = 1; break;
case '?': exit_usage();
}
}
c = argc - optind;
if(c > 2) exit_usage();
for(i = 0; i < c; i++)
{
if(!strcmp(argv[optind + i], "-")) continue;
switch(i)
{
case 0:
fin = fopen(argv[optind + i], "rb");
if(!fin)
{
fprintf(stderr, "Error opening '%s' for input:\n", argv[optind + i]);
perror("fopen");
return(-1);
}
break;
case 1:
fout = fopen(argv[optind + i], "wb");
if(!fout)
{
fprintf(stderr, "Error opening '%s' for output:\n", argv[optind + i]);
perror("fopen");
return(-1);
}
break;
}
}
switch(encode)
{
case 0: /* Decode */
if(droptest > 0) fprintf(stderr, "*** NOTE: Drop test enabled: %i ***\n", droptest);
ssdv_dec_init(&ssdv);
jpeg_length = 1024 * 1024 * 4;
jpeg = malloc(jpeg_length);
ssdv_dec_set_buffer(&ssdv, jpeg, jpeg_length);
i = 0;
while(fread(pkt, 1, SSDV_PKT_SIZE, fin) > 0)
{
/* Drop % of packets */
if(droptest && (rand() / (RAND_MAX / 100) < droptest)) continue;
/* Test the packet is valid */
if(ssdv_dec_is_packet(pkt, &errors) != 0) continue;
if(verbose)
{
ssdv_packet_info_t p;
ssdv_dec_header(&p, pkt);
fprintf(stderr, "Decoded image packet. Callsign: %s, Image ID: %d, Resolution: %dx%d, Packet ID: %d (%d errors corrected)\n"
">> Type: %d, Quality: %d, EOI: %d, MCU Mode: %d, MCU Offset: %d, MCU ID: %d/%d\n",
p.callsign_s,
p.image_id,
p.width,
p.height,
p.packet_id,
errors,
p.type,
p.quality,
p.eoi,
p.mcu_mode,
p.mcu_offset,
p.mcu_id,
p.mcu_count
);
}
/* Feed it to the decoder */
ssdv_dec_feed(&ssdv, pkt);
i++;
}
ssdv_dec_get_jpeg(&ssdv, &jpeg, &jpeg_length);
fwrite(jpeg, 1, jpeg_length, fout);
free(jpeg);
fprintf(stderr, "Read %i packets\n", i);
break;
case 1: /* Encode */
ssdv_enc_init(&ssdv, type, callsign, image_id, quality);
ssdv_enc_set_buffer(&ssdv, pkt);
i = 0;
while(1)
{
while((c = ssdv_enc_get_packet(&ssdv)) == SSDV_FEED_ME)
{
size_t r = fread(b, 1, 128, fin);
if(r <= 0)
{
fprintf(stderr, "Premature end of file\n");
break;
}
ssdv_enc_feed(&ssdv, b, r);
}
if(c == SSDV_EOI)
{
fprintf(stderr, "ssdv_enc_get_packet said EOI\n");
break;
}
else if(c != SSDV_OK)
{
fprintf(stderr, "ssdv_enc_get_packet failed: %i\n", c);
return(-1);
}
fwrite(pkt, 1, SSDV_PKT_SIZE, fout);
i++;
}
fprintf(stderr, "Wrote %i packets\n", i);
break;
default:
fprintf(stderr, "No mode specified.\n");
break;
}
if(fin != stdin) fclose(fin);
if(fout != stdout) fclose(fout);
return(0);
}