initial
This commit is contained in:
16
Makefile
Normal file
16
Makefile
Normal file
@@ -0,0 +1,16 @@
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -O2
|
||||
LDFLAGS = -lm
|
||||
|
||||
TARGET = sstv
|
||||
SRCS = main.c sstv.c robot36.c image.c wav.c
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) $(OBJS) -o $(TARGET) $(LDFLAGS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET) $(OBJS)
|
||||
41
image.c
Normal file
41
image.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
#include "image.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
int image_load(image_t *img, const char *filename) {
|
||||
int ch;
|
||||
img->data = stbi_load(filename, &img->width, &img->height, &ch, 3);
|
||||
return img->data != NULL;
|
||||
}
|
||||
|
||||
int image_resize(image_t *src, image_t *dst, int new_w, int new_h) {
|
||||
dst->width = new_w;
|
||||
dst->height = new_h;
|
||||
dst->data = malloc(new_w * new_h * 3);
|
||||
if (!dst->data) return 0;
|
||||
|
||||
for (int y = 0; y < new_h; y++) {
|
||||
for (int x = 0; x < new_w; x++) {
|
||||
int sx = x * src->width / new_w;
|
||||
int sy = y * src->height / new_h;
|
||||
for (int c = 0; c < 3; c++) {
|
||||
dst->data[(y*new_w + x)*3 + c] =
|
||||
src->data[(sy*src->width + sx)*3 + c];
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void image_free(image_t *img) {
|
||||
if (img->data)
|
||||
stbi_image_free(img->data);
|
||||
img->data = NULL;
|
||||
}
|
||||
|
||||
void image_free_raw(image_t *img) {
|
||||
if (img->data)
|
||||
free(img->data);
|
||||
img->data = NULL;
|
||||
}
|
||||
17
image.h
Normal file
17
image.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef IMAGE_H
|
||||
#define IMAGE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
int width;
|
||||
int height;
|
||||
uint8_t *data;
|
||||
} image_t;
|
||||
|
||||
int image_load(image_t *img, const char *filename);
|
||||
int image_resize(image_t *src, image_t *dst, int new_w, int new_h);
|
||||
void image_free(image_t *img);
|
||||
void image_free_raw(image_t *img);
|
||||
|
||||
#endif // !IMAGE_H
|
||||
17
main.c
Normal file
17
main.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "sstv.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 3) {
|
||||
printf("Usage: %s input_image output.wav\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!sstv_encode_robot36(argv[1], argv[2])) {
|
||||
printf("Encoding failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Done.\n");
|
||||
return 0;
|
||||
}
|
||||
38
readme.md
Normal file
38
readme.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# sstv e36 encoder
|
||||
|
||||
This is a simple C SSTV Robot36 encoder!
|
||||
Written in pure C
|
||||
|
||||
## License:
|
||||
|
||||
This project is distributed under the MiT License!
|
||||
|
||||
### Our License:
|
||||
#### MIT :
|
||||
```
|
||||
Copyright 2026 , rattatwinko
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
```
|
||||
|
||||
#### For stb_image.h:
|
||||
```
|
||||
ALTERNATIVE A - MIT License
|
||||
Copyright (c) 2017 Sean Barrett
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
```
|
||||
43
robot36.c
Normal file
43
robot36.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "robot36.h"
|
||||
#include "sstv.h"
|
||||
#include <stdint.h>
|
||||
|
||||
// From Dayton Paper Appendix B - ITU BT.601
|
||||
// http://www.barberdsp.com/downloads/Dayton%20Paper.pdf
|
||||
static void rgb_to_ycbcr(uint8_t r, uint8_t g, uint8_t b,
|
||||
double *Y, double *RY, double *BY) {
|
||||
*Y = 16.0 + .003906 * ((65.738 * r) + (129.057 * g) + (25.064 * b));
|
||||
*RY = 128.0 + .003906 * ((112.439 * r) + (-94.154 * g) + (-18.285 * b));
|
||||
*BY = 128.0 + .003906 * ((-37.945 * r) + (-74.494 * g) + (112.439 * b));
|
||||
}
|
||||
|
||||
static void scan_line_y(wav_t *wav, uint8_t *line) {
|
||||
for (int x = 0; x < ROBOT36_WIDTH; x++) {
|
||||
double Y, RY, BY;
|
||||
rgb_to_ycbcr(line[x*3+0], line[x*3+1], line[x*3+2], &Y, &RY, &BY);
|
||||
sstv_tone(wav, 1500.0 + Y * 3.1372549, 88.0 / ROBOT36_WIDTH);
|
||||
}
|
||||
}
|
||||
|
||||
static void scan_line_chroma(wav_t *wav, uint8_t *line, int is_ry) {
|
||||
for (int x = 0; x < ROBOT36_WIDTH; x++) {
|
||||
double Y, RY, BY;
|
||||
rgb_to_ycbcr(line[x*3+0], line[x*3+1], line[x*3+2], &Y, &RY, &BY);
|
||||
double c = is_ry ? RY : BY;
|
||||
sstv_tone(wav, 1500.0 + c * 3.1372549, 44.0 / ROBOT36_WIDTH);
|
||||
}
|
||||
}
|
||||
|
||||
void robot36_encode_image(wav_t *wav, uint8_t *rgb) {
|
||||
for (int y = 0; y < ROBOT36_HEIGHT; y++) {
|
||||
uint8_t *line = &rgb[y * ROBOT36_WIDTH * 3];
|
||||
int even = (y % 2 == 0);
|
||||
|
||||
sstv_tone(wav, 1200, 9.0); // sync
|
||||
sstv_tone(wav, 1500, 3.0); // sync porch
|
||||
scan_line_y(wav, line);
|
||||
sstv_tone(wav, even ? 1500 : 2300, 4.5); // separator - tells decoder Cr vs Cb!
|
||||
sstv_tone(wav, 1900, 1.5); // porch before chroma
|
||||
scan_line_chroma(wav, line, even); // Cr on even, Cb on odd
|
||||
}
|
||||
}
|
||||
12
robot36.h
Normal file
12
robot36.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef ROBOT36_H
|
||||
#define ROBOT36_H
|
||||
|
||||
#include "wav.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#define ROBOT36_WIDTH 320
|
||||
#define ROBOT36_HEIGHT 240
|
||||
|
||||
void robot36_encode_image(wav_t *wav, uint8_t *rgb);
|
||||
|
||||
#endif // !ROBOT36_H
|
||||
78
sstv.c
Normal file
78
sstv.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "sstv.h"
|
||||
#include "image.h"
|
||||
#include "robot36.h"
|
||||
#include "wav.h"
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define PI 3.14159265358979
|
||||
#define AMPLITUDE 12000
|
||||
|
||||
static double phase = 0.0;
|
||||
|
||||
void sstv_tone(wav_t *wav, double freq, double duration_ms) {
|
||||
uint32_t total_samples = (uint32_t)((duration_ms / 1000.0) * wav->sample_rate);
|
||||
|
||||
for (uint32_t i = 0; i < total_samples; i++) {
|
||||
double step = 2.0 * PI * freq / wav->sample_rate;
|
||||
phase += step;
|
||||
if (phase > 2.0 * PI)
|
||||
phase -= 2.0 * PI;
|
||||
|
||||
int16_t sample = (int16_t)(AMPLITUDE * sin(phase));
|
||||
wave_write_sample(wav, sample);
|
||||
}
|
||||
}
|
||||
|
||||
void sstv_vis_header(wav_t *wav) {
|
||||
uint8_t vis = 0x08;
|
||||
uint8_t parity = 0;
|
||||
|
||||
sstv_tone(wav, 1900, 300);
|
||||
sstv_tone(wav, 1200, 10);
|
||||
sstv_tone(wav, 1900, 300);
|
||||
|
||||
sstv_tone(wav, 1200, 30);
|
||||
|
||||
for (int i = 0; i < 7; i++) {
|
||||
uint8_t bit = (vis >> i) & 1;
|
||||
if (bit) {
|
||||
sstv_tone(wav, 1100, 30);
|
||||
parity ^= 1;
|
||||
} else {
|
||||
sstv_tone(wav, 1300, 30);
|
||||
}
|
||||
}
|
||||
|
||||
sstv_tone(wav, parity ? 1100 : 1300, 30);
|
||||
|
||||
sstv_tone(wav, 1200, 30);
|
||||
}
|
||||
|
||||
int sstv_encode_robot36(const char *input_image, const char *output_wav) {
|
||||
image_t img, resized;
|
||||
wav_t wav;
|
||||
|
||||
if (!image_load(&img, input_image))
|
||||
return 0;
|
||||
|
||||
if (!image_resize(&img, &resized, ROBOT36_WIDTH, ROBOT36_HEIGHT)) {
|
||||
image_free(&img);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!wave_open(&wav, output_wav, 44100)) {
|
||||
image_free(&img);
|
||||
image_free_raw(&resized);
|
||||
return 0;
|
||||
}
|
||||
|
||||
sstv_vis_header(&wav);
|
||||
robot36_encode_image(&wav, resized.data);
|
||||
wave_close(&wav);
|
||||
|
||||
image_free(&img);
|
||||
image_free_raw(&resized);
|
||||
|
||||
return 1;
|
||||
}
|
||||
11
sstv.h
Normal file
11
sstv.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef SSTV_H
|
||||
#define SSTV_H
|
||||
|
||||
#include "wav.h"
|
||||
#include <stdint.h>
|
||||
|
||||
void sstv_tone(wav_t *wav, double freq, double duration_ms);
|
||||
void sstv_vis_header(wav_t *wav);
|
||||
int sstv_encode_robot36(const char *input_image, const char *output_wav);
|
||||
|
||||
#endif // !SSTV_H
|
||||
7988
stb_image.h
Executable file
7988
stb_image.h
Executable file
File diff suppressed because it is too large
Load Diff
71
wav.c
Normal file
71
wav.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "wav.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
static void write_u16(FILE *f, uint16_t v) {
|
||||
uint8_t b[2];
|
||||
b[0] = (uint8_t)(v & 0xff);
|
||||
b[1] = (uint8_t)((v >> 8) & 0xff);
|
||||
fwrite(b, 1, 2, f);
|
||||
}
|
||||
|
||||
static void write_u32(FILE *f, uint32_t v) {
|
||||
uint8_t b[4];
|
||||
b[0] = (uint8_t)(v & 0xff);
|
||||
b[1] = (uint8_t)((v >> 8) & 0xff);
|
||||
b[2] = (uint8_t)((v >> 16) & 0xff);
|
||||
b[3] = (uint8_t)((v >> 24) & 0xff);
|
||||
fwrite(b, 1, 4, f);
|
||||
}
|
||||
|
||||
int wave_open(wav_t *wav, const char *filename, uint32_t sample_rate) {
|
||||
|
||||
wav->file = fopen(filename, "wb");
|
||||
if (!wav->file)
|
||||
return 0;
|
||||
|
||||
wav->sample_rate = sample_rate;
|
||||
wav->samples_written = 0;
|
||||
|
||||
/* RIFF header */
|
||||
fwrite((uint8_t[]){'R','I','F','F'}, 1, 4, wav->file);
|
||||
write_u32(wav->file, 0); // Placeholder for file size
|
||||
fwrite((uint8_t[]){'W','A','V','E'}, 1, 4, wav->file);
|
||||
|
||||
/* fmt chunk */
|
||||
fwrite((uint8_t[]){'f','m','t',' '}, 1, 4, wav->file);
|
||||
write_u32(wav->file, 16); // PCM chunk size
|
||||
write_u16(wav->file, 1); // Audio format (1 = PCM)
|
||||
write_u16(wav->file, 1); // Channels (1 = mono)
|
||||
write_u32(wav->file, sample_rate); // Sample rate
|
||||
write_u32(wav->file, sample_rate * 2); // Byte rate (sr * block_align)
|
||||
write_u16(wav->file, 2); // Block align (mono 16-bit = 2)
|
||||
write_u16(wav->file, 16); // Bits per sample
|
||||
|
||||
/* data chunk */
|
||||
fwrite((uint8_t[]){'d','a','t','a'}, 1, 4, wav->file);
|
||||
write_u32(wav->file, 0); // Placeholder for data size
|
||||
|
||||
return 1;
|
||||
}
|
||||
void wave_write_sample(wav_t *wav, int16_t sample) {
|
||||
write_u16(wav->file, (uint16_t)sample);
|
||||
wav->samples_written++;
|
||||
}
|
||||
|
||||
void wave_close(wav_t *wav) {
|
||||
|
||||
uint32_t data_size = wav->samples_written * 2;
|
||||
uint32_t file_size = 36 + data_size;
|
||||
|
||||
/* Patch RIFF size */
|
||||
fseek(wav->file, 4, SEEK_SET);
|
||||
write_u32(wav->file, file_size);
|
||||
|
||||
/* Patch data chunk size */
|
||||
fseek(wav->file, 40, SEEK_SET);
|
||||
write_u32(wav->file, data_size);
|
||||
|
||||
fclose(wav->file);
|
||||
}
|
||||
18
wav.h
Normal file
18
wav.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef WAV_H
|
||||
#define WAV_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
FILE *file;
|
||||
uint32_t sample_rate;
|
||||
uint32_t samples_written;
|
||||
} wav_t;
|
||||
|
||||
int wave_open(wav_t *wav, const char *filename, uint32_t sample_rate);
|
||||
void wave_write_sample(wav_t *wav, int16_t sample);
|
||||
|
||||
void wave_close(wav_t *wav);
|
||||
|
||||
#endif // !WAV_H
|
||||
Reference in New Issue
Block a user