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
|
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "ttxtsubsfilter.h"
#include "teletext.h"
cTtxtSubsFilter::cTtxtSubsFilter(void)
:
mCount(0),
mPages(0)
{
int i;
for(i = 0; i < 8; i++)
mMagPage[i] = -1;
}
cTtxtSubsFilter::~cTtxtSubsFilter(void)
{
FreePages();
}
void cTtxtSubsFilter::SetPages(size_t count, uint16_t *pages)
{
FreePages();
mPages = (uint16_t *) malloc(count * sizeof(uint16_t));
memcpy((char *) mPages, (char *) pages, count * sizeof(uint16_t));
mCount = count;
if(0) {
fprintf(stderr, "SetPages: %d, ", count);
for(size_t i = 0; i < count; i++)
fprintf(stderr, "%03x ", pages[i]);
fprintf(stderr, "\n");
}
}
// XXX IF MERGING PAGES FROM SEVERAL PIDS (which we currently don't)
// Need to insert a line Y0 if
// - there has been data from other streams inserted and the next line is not a Y0
// there has been data from other magazines and the next line for this mag is not a Y0
// indata and outdata are pointers to a ttxt data row buffers,
// if return value <> 0 outdata countains data to pass on
int cTtxtSubsFilter::Filter(char *indata, char *outdata)
{
int mp;
int mag; // X in ETSI EN 300 706
int packet; // Y
int result = 0;
struct ttxt_data_field *d = (struct ttxt_data_field *) indata;
mp = UNHAM_INV(d->mag_addr_ham[0], d->mag_addr_ham[1]);
mag = mp & 0x7;
packet = (mp >> 3) & 0x1f;
if (0) { // XXX
uint8_t buf[2];
ham8_4byte(mp, buf);
fprintf(stderr, "unham %02x %02x -> %02x, ham: %02x %02x\n",
invtab[d->mag_addr_ham[0]], invtab[d->mag_addr_ham[1]],
mp,
buf[0], buf[1]);
}
// packet 0, change page no in this magazine
if(packet == 0) {
uint8_t no;
int newindex;
no = UNHAM_INV(d->data[0], d->data[1]);
mMagPage[mag] = newindex = Find(mCount, mPages, (mag << 8) | no);
if(newindex != -1) {
//fprintf(stderr, "Filter: %x%02x (%d)\n", mag, no, newindex); // XXX
MakeY0(outdata, indata, mPages[newindex]);
result = 1;
}
} else if(packet >= 1 && packet <= 25) {
if(mMagPage[mag] != -1) {
memcpy(outdata, indata, sizeof(ttxt_data_field));
result = 1;
}
}
return result;
}
void cTtxtSubsFilter::FreePages(void)
{
mCount = 0;
if(mPages) {
free(mPages);
mPages = NULL;
}
}
int cTtxtSubsFilter::Find(int count, uint16_t *pages, uint16_t pageno){
int i;
for(i = 0; i < count; i++) {
if(pages[i] == pageno)
return i;
}
return -1;
}
// make a new Y0 line with the flags in
void cTtxtSubsFilter::MakeY0(char *outdata, char *indata, uint16_t newpageno)
{
struct ttxt_data_field *i = (struct ttxt_data_field *) indata;
struct ttxt_data_field *o = (struct ttxt_data_field *) outdata;
uint8_t hambuf[2];
o->data_unit_id = 3; // EBU Teletxt subtitle data
o->data_unit_length = 44;
o->par_loff = 0xc0; // reserved bits + unknown line
o->framing_code = 0xe4;
// new magazine number (Y = 0)
ham8_4byte((newpageno >> 8) & 0x7, hambuf);
o->mag_addr_ham[0] = invtab[hambuf[0]];
o->mag_addr_ham[1] = invtab[hambuf[1]];
// new page number
ham8_4byte(newpageno & 0xff, hambuf);
o->data[0] = invtab[hambuf[0]];
o->data[1] = invtab[hambuf[1]];
// copy flags
memcpy(o->data + 2, i->data + 2, 6);
// new text
unsigned char txtbuf[32];
size_t txtlen;
size_t n;
txtlen = snprintf((char *) txtbuf, 32, "%03x", newpageno < 0x100 ? newpageno + 0x800 : newpageno);
for(n = 0; n < txtlen; n++) {
if(parity(txtbuf[n]))
o->data[n + 8] = invtab[txtbuf[n] | 0x80]; // XXX wrong parity?
else
o->data[n + 8] = invtab[txtbuf[n]]; // XXX wrong parity?
}
for(; n < 32; n++)
o->data[n + 8] = invtab[(unsigned char) ' ']; // space already has right parity
}
|