forked from captainjack64/hacktv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wss.c
170 lines (135 loc) · 4.02 KB
/
wss.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
/* hacktv - Analogue video transmitter for the HackRF */
/*=======================================================================*/
/* Copyright 2018 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/>. */
/* -=== WSS encoder ===- */
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "video.h"
#include "vbidata.h"
typedef struct {
const char *id;
uint8_t code;
} _wss_modes_t;
static const _wss_modes_t _wss_modes[] = {
{ "4:3", 0x08 },
{ "16:9", 0x07 },
{ "14:9-letterbox", 0x01 },
{ "16:9-letterbox", 0x04 },
{ "auto", 0xFF },
{ NULL, 0 },
};
static size_t _group_bits(uint8_t *vbi, uint8_t code, size_t offset, size_t length)
{
int i, b;
while(length--)
{
for(i = 0; i < 6; i++, offset++)
{
if(i == 3) code ^= 1;
b = 7 - (offset % 8);
vbi[offset / 8] &= ~(1 << b);
vbi[offset / 8] |= (code & 1) << b;
}
code >>= 1;
}
return(offset);
}
int wss_init(wss_t *s, vid_t *vid, char *mode)
{
int16_t level;
size_t o;
memset(s, 0, sizeof(wss_t));
/* Find the mode settings */
s->code = 0;
for(o = 0; _wss_modes[o].id != NULL; o++)
{
if(strcasecmp(mode, _wss_modes[o].id) == 0)
{
s->code = _wss_modes[o].code;
break;
}
}
if(s->code == 0)
{
fprintf(stderr, "wss: Unrecognised mode '%s'.\n", mode);
return(VID_ERROR);
}
/* Calculate the high level for the VBI data */
level = round((vid->white_level - vid->black_level) * (5.0 / 7.0));
s->vid = vid;
s->lut = vbidata_init(
320, s->vid->width,
level,
VBIDATA_FILTER_RC, 0.7
);
if(!s->lut)
{
return(VID_OUT_OF_MEMORY);
}
/* Prepare the VBI data. Start with the run-in and start code */
s->vbi[0] = 0xF8; // 11111000
s->vbi[1] = 0xE3; // 11100011
s->vbi[2] = 0x8E; // 10001110
s->vbi[3] = 0x38; // 00111000
s->vbi[4] = 0xF1; // 11110001
s->vbi[5] = 0xE0; // 11100000
s->vbi[6] = 0xF8; // 11111___
/* Group 1 (Aspect Ratio) */
o = _group_bits(s->vbi, s->code, 29 + 24, 4);
/* Group 2 (Enhanced Services) */
o = _group_bits(s->vbi, 0x00, o, 4);
/* Group 3 (Subtitles) */
o = _group_bits(s->vbi, 0x00, o, 3);
/* Group 4 (Reserved) */
o = _group_bits(s->vbi, 0x00, o, 3);
/* Calculate width of line to blank */
s->blank_width = round(s->vid->sample_rate * 42.5e-6);
return(VID_OK);
}
void wss_free(wss_t *s)
{
if(s == NULL) return;
free(s->lut);
memset(s, 0, sizeof(wss_t));
}
int wss_render(vid_t *s, void *arg, int nlines, vid_line_t **lines)
{
wss_t *w = arg;
vid_line_t *l = lines[0];
int x;
/* WSS is rendered on line 23 */
if(l->line != 23)
{
return(1);
}
if(w->code == 0xFF)
{
/* Auto mode selects between 4:3 and 16:9 based on the
* the ratio of the source frame. */
_group_bits(w->vbi, s->ratio <= (14.0 / 9.0) ? 0x08 : 0x07, 29 + 24, 4);
}
/* 42.5μs of line 23 needs to be blanked otherwise the WSS bits may
* overlap active video */
for(x = s->half_width; x < w->blank_width; x++)
{
l->output[x * 2] = s->black_level;
}
vbidata_render_nrz(w->lut, w->vbi, -55, 137, VBIDATA_MSB_FIRST, l->output, 2);
l->vbialloc = 1;
return(1);
}