forked from xdsopl/robot36
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.c
226 lines (201 loc) · 7.22 KB
/
encode.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
222
223
224
225
/*
robot36 - encode and decode images using SSTV in Robot 36 mode
Written in 2011 by <Ahmet Inan> <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <complex.h>
#include <limits.h>
#include "yuv.h"
#include "utils.h"
#include "pcm.h"
#include "img.h"
struct img *img;
struct pcm *pcm;
complex float nco;
float hz2rad;
int channels;
short *buff;
int rate = 48000;
const double sync_porch_sec = 0.003l;
const double porch_sec = 0.0015l;
const double y_sec = 0.088l;
const double uv_sec = 0.044l;
const double hor_sync_sec = 0.009l;
const double seperator_sec = 0.0045l;
int sync_porch_len = 0;
int porch_len = 0;
int y_len = 0;
int uv_len = 0;
int hor_sync_len = 0;
int seperator_len = 0;
int add_sample(float val)
{
for (int i = 0; i < channels; i++)
buff[i] = (float)SHRT_MAX * val;
return write_pcm(pcm, buff, 1);
}
void add_freq(float freq)
{
add_sample(creal(nco));
nco *= cexpf(freq * hz2rad * I);
}
void hor_sync()
{
for (int ticks = 0; ticks < hor_sync_len; ticks++)
add_freq(1200.0);
}
void sync_porch()
{
for (int ticks = 0; ticks < sync_porch_len; ticks++)
add_freq(1500.0);
}
void porch()
{
for (int ticks = 0; ticks < porch_len; ticks++)
add_freq(1900.0);
}
void even_seperator()
{
for (int ticks = 0; ticks < seperator_len; ticks++)
add_freq(1500.0);
}
void odd_seperator()
{
for (int ticks = 0; ticks < seperator_len; ticks++)
add_freq(2300.0);
}
void y_scan(int y)
{
for (int ticks = 0; ticks < y_len; ticks++) {
float xf = fclampf((320.0 * ticks) / (float)y_len, 0.0, 319.0);
int x0 = xf;
int x1 = fclampf(x0 + 1, 0.0, 319.0);
int off0 = 3 * y * img->width + 3 * x0;
int off1 = 3 * y * img->width + 3 * x1;
float R0 = linear(img->pixel[off0 + 0]);
float G0 = linear(img->pixel[off0 + 1]);
float B0 = linear(img->pixel[off0 + 2]);
float R1 = linear(img->pixel[off1 + 0]);
float G1 = linear(img->pixel[off1 + 1]);
float B1 = linear(img->pixel[off1 + 2]);
uint8_t R = srgb(flerpf(R0, R1, xf - (float)x0));
uint8_t G = srgb(flerpf(G0, G1, xf - (float)x0));
uint8_t B = srgb(flerpf(B0, B1, xf - (float)x0));
add_freq(1500.0 + 800.0 * Y_RGB(R, G, B) / 255.0);
}
}
void v_scan(int y)
{
for (int ticks = 0; ticks < uv_len; ticks++) {
float xf = fclampf((160.0 * ticks) / (float)uv_len, 0.0, 159.0);
int x0 = xf;
int x1 = fclampf(x0 + 1, 0.0, 159.0);
int evn0 = 3 * y * img->width + 6 * x0;
int evn1 = 3 * y * img->width + 6 * x1;
int odd0 = 3 * (y + 1) * img->width + 6 * x0;
int odd1 = 3 * (y + 1) * img->width + 6 * x1;
float R0 = (linear(img->pixel[evn0 + 0]) + linear(img->pixel[odd0 + 0]) + linear(img->pixel[evn0 + 3]) + linear(img->pixel[odd0 + 3])) / 4;
float G0 = (linear(img->pixel[evn0 + 1]) + linear(img->pixel[odd0 + 1]) + linear(img->pixel[evn0 + 4]) + linear(img->pixel[odd0 + 4])) / 4;
float B0 = (linear(img->pixel[evn0 + 2]) + linear(img->pixel[odd0 + 2]) + linear(img->pixel[evn0 + 5]) + linear(img->pixel[odd0 + 5])) / 4;
float R1 = (linear(img->pixel[evn1 + 0]) + linear(img->pixel[odd1 + 0]) + linear(img->pixel[evn1 + 3]) + linear(img->pixel[odd1 + 3])) / 4;
float G1 = (linear(img->pixel[evn1 + 1]) + linear(img->pixel[odd1 + 1]) + linear(img->pixel[evn1 + 4]) + linear(img->pixel[odd1 + 4])) / 4;
float B1 = (linear(img->pixel[evn1 + 2]) + linear(img->pixel[odd1 + 2]) + linear(img->pixel[evn1 + 5]) + linear(img->pixel[odd1 + 5])) / 4;
uint8_t R = srgb(flerpf(R0, R1, xf - (float)x0));
uint8_t G = srgb(flerpf(G0, G1, xf - (float)x0));
uint8_t B = srgb(flerpf(B0, B1, xf - (float)x0));
add_freq(1500.0 + 800.0 * V_RGB(R, G, B) / 255.0);
}
}
void u_scan(int y)
{
for (int ticks = 0; ticks < uv_len; ticks++) {
float xf = fclampf((160.0 * ticks) / (float)uv_len, 0.0, 159.0);
int x0 = xf;
int x1 = fclampf(x0 + 1, 0.0, 159.0);
int evn0 = 3 * (y - 1) * img->width + 6 * x0;
int evn1 = 3 * (y - 1) * img->width + 6 * x1;
int odd0 = 3 * y * img->width + 6 * x0;
int odd1 = 3 * y * img->width + 6 * x1;
float R0 = (linear(img->pixel[evn0 + 0]) + linear(img->pixel[odd0 + 0]) + linear(img->pixel[evn0 + 3]) + linear(img->pixel[odd0 + 3])) / 4;
float G0 = (linear(img->pixel[evn0 + 1]) + linear(img->pixel[odd0 + 1]) + linear(img->pixel[evn0 + 4]) + linear(img->pixel[odd0 + 4])) / 4;
float B0 = (linear(img->pixel[evn0 + 2]) + linear(img->pixel[odd0 + 2]) + linear(img->pixel[evn0 + 5]) + linear(img->pixel[odd0 + 5])) / 4;
float R1 = (linear(img->pixel[evn1 + 0]) + linear(img->pixel[odd1 + 0]) + linear(img->pixel[evn1 + 3]) + linear(img->pixel[odd1 + 3])) / 4;
float G1 = (linear(img->pixel[evn1 + 1]) + linear(img->pixel[odd1 + 1]) + linear(img->pixel[evn1 + 4]) + linear(img->pixel[odd1 + 4])) / 4;
float B1 = (linear(img->pixel[evn1 + 2]) + linear(img->pixel[odd1 + 2]) + linear(img->pixel[evn1 + 5]) + linear(img->pixel[odd1 + 5])) / 4;
uint8_t R = srgb(flerpf(R0, R1, xf - (float)x0));
uint8_t G = srgb(flerpf(G0, G1, xf - (float)x0));
uint8_t B = srgb(flerpf(B0, B1, xf - (float)x0));
add_freq(1500.0 + 800.0 * U_RGB(R, G, B) / 255.0);
}
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: %s <input.ppm> <output.wav|default|hw:?,?> <rate>\n", argv[0]);
return 1;
}
if (!open_img_read(&img, argv[1]))
return 1;
if (320 != img->width || 240 != img->height) {
fprintf(stderr, "image not 320x240\n");
close_img(img);
return 1;
}
char *pcm_name = "default";
if (argc > 2)
pcm_name = argv[2];
if (argc > 3)
rate = atoi(argv[3]);
if (!open_pcm_write(&pcm, pcm_name, rate, 1, 37.5))
return 1;
rate = rate_pcm(pcm);
channels = channels_pcm(pcm);
sync_porch_len = rate * sync_porch_sec;
porch_len = rate * porch_sec;
y_len = rate * y_sec;
uv_len = rate * uv_sec;
hor_sync_len = rate * hor_sync_sec;
seperator_len = rate * seperator_sec;
// fprintf(stderr, "%d %d %d %d %d %d\n", sync_porch_len, porch_len, y_len, uv_len, hor_sync_len, seperator_len);
buff = (short *)malloc(sizeof(short)*channels);
info_pcm(pcm);
if (fabsf(porch_sec * rate - porch_len) > 0.0001)
fprintf(stderr, "this rate will not give accurate (smooth) results.\ntry 40000Hz and resample to %dHz\n", rate);
hz2rad = (2.0 * M_PI) / rate;
nco = -I * 0.7;
enum { N = 13 };
float seq_freq[N] = { 1900.0, 1200.0, 1900.0, 1200.0, 1300.0, 1300.0, 1300.0, 1100.0, 1300.0, 1300.0, 1300.0, 1100.0, 1200.0 };
float seq_time[N] = { 0.3, 0.01, 0.3, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03 };
for (int ticks = 0; ticks < (int)(0.3 * rate); ticks++)
add_sample(0.0);
for (int i = 0; i < N; i++)
for (int ticks = 0; ticks < (int)(seq_time[i] * rate); ticks++)
add_freq(seq_freq[i]);
for (int y = 0; y < img->height; y++) {
// EVEN LINES
hor_sync();
sync_porch();
y_scan(y);
even_seperator();
porch();
v_scan(y);
// ODD LINES
y++;
hor_sync();
sync_porch();
y_scan(y);
odd_seperator();
porch();
u_scan(y);
}
while (add_sample(0.0));
close_pcm(pcm);
close_img(img);
return 0;
}