00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <cstdlib>
00024 #include <cstdarg>
00025 #include <unistd.h>
00026 #include <cstring>
00027 #include <cctype>
00028 #include <cerrno>
00029 #include <fcntl.h>
00030 #include <sys/types.h>
00031 #ifdef HAVE_GETOPT_H
00032 # include <getopt.h>
00033 #endif
00034
00035 #include "cc.h"
00036
00037 static int parityok(int n)
00038 {
00039 int mask = 0;
00040 int j, k;
00041 for (k = 1, j = 0; j < 7; j++)
00042 {
00043 if (n & (1 << j))
00044 k++;
00045 }
00046 if ((k & 1) == ((n >> 7) & 1))
00047 mask |= 0x00FF;
00048 for (k = 1, j = 8; j < 15; j++)
00049 {
00050 if (n & (1 << j))
00051 k++;
00052 }
00053 if ((k & 1) == ((n >> 15) & 1))
00054 mask |= 0xFF00;
00055 return mask;
00056 }
00057
00058
00059 static int decodebit(unsigned char *data, int threshold, int scale1)
00060 {
00061 int i, sum = 0;
00062 for (i = 0; i < scale1; i++)
00063 sum += data[i];
00064 return (sum > threshold * scale1);
00065 }
00066
00067
00068 static int decode(unsigned char *vbiline, int scale0, int scale1)
00069 {
00070 int max[7], min[7], val[7], i, clk, tmp, sample, packedbits = 0;
00071
00072 for (clk = 0; clk < 7; clk++)
00073 max[clk] = min[clk] = val[clk] = -1;
00074 clk = tmp = 0;
00075 i = 30;
00076
00077 while (i < 600 && clk < 7)
00078 {
00079 sample = vbiline[i];
00080 if (max[clk] < 0)
00081 {
00082 if (sample > 85 && sample > val[clk])
00083 (val[clk] = sample, tmp = i);
00084 else if (val[clk] - sample > 30)
00085 (max[clk] = tmp, i = tmp + 10);
00086 }
00087 else
00088 {
00089 if (sample < 85 && sample < val[clk])
00090 (val[clk] = sample, tmp = i);
00091 else if (sample - val[clk] > 30)
00092 (min[clk++] = tmp, i = tmp + 10);
00093 }
00094 i++;
00095 }
00096
00097 i = min[6] = min[5] - max[5] + max[6];
00098
00099 if (clk != 7 || vbiline[max[3]] - vbiline[min[5]] < 45)
00100 return -1;
00101
00102
00103 for (i = 0, sample = 0; i < 7; i++)
00104 sample = (sample + vbiline[min[i]] + vbiline[max[i]]) / 3;
00105
00106 for (i = min[6]; vbiline[i] < sample; i++);
00107
00108 tmp = i + scale0;
00109 for (i = 0; i < 16; i++)
00110 if (decodebit(&vbiline[tmp + i * scale0], sample, scale1))
00111 packedbits |= 1 << i;
00112 return packedbits & parityok(packedbits);
00113 }
00114
00115 #if 0
00116 static int webtv_check(char *buf, int len)
00117 {
00118 unsigned long sum;
00119 unsigned long nwords;
00120 unsigned short csum = 0;
00121 char temp[9];
00122 int nbytes = 0;
00123
00124 while (buf[0] != '<' && len > 6)
00125 {
00126 buf++;
00127 len--;
00128 }
00129
00130 if (len == 6)
00131 return 0;
00132
00133 while (nbytes + 6 <= len)
00134 {
00135
00136 if (buf[nbytes] == '[' && buf[nbytes + 5] == ']' && buf[nbytes + 6] != '[')
00137 break;
00138 else
00139 nbytes++;
00140 }
00141 if (nbytes + 6 > len)
00142 return 0;
00143
00144 nwords = nbytes >> 1;
00145 sum = 0;
00146
00147
00148 while (nwords-- > 0)
00149 {
00150 sum += *buf++ << 8;
00151 sum += *buf++;
00152 }
00153 if (nbytes & 1)
00154 {
00155 sum += *buf << 8;
00156 }
00157 csum = (unsigned short) (sum >> 16);
00158 while (csum != 0)
00159 {
00160 sum = csum + (sum & 0xffff);
00161 csum = (unsigned short) (sum >> 16);
00162 }
00163 sprintf(temp, "%04X\n", (int) ~sum & 0xffff);
00164 buf++;
00165 if (!strncmp(buf, temp, 4))
00166 {
00167 buf[5] = 0;
00168 printf("\33[35mWEBTV: %s\33[0m\n", buf - nbytes - 1);
00169 fflush(stdout);
00170 }
00171 return 0;
00172 }
00173 #endif
00174
00175 #if 0
00176
00177 struct cc *cc_open(const char *vbi_name)
00178 {
00179 struct cc *cc;
00180
00181 if (!(cc = new struct cc))
00182 {
00183 printf("out of memory\n");
00184 return 0;
00185 }
00186
00187 if ((cc->fd = open(vbi_name, O_RDONLY)) == -1)
00188 {
00189 printf("cannot open vbi device\n");
00190 free(cc);
00191 return 0;
00192 }
00193
00194 cc->code1 = -1;
00195 cc->code2 = -1;
00196
00197 return cc;
00198 }
00199 #endif
00200
00201 void cc_close(struct cc *cc)
00202 {
00203 close(cc->fd);
00204 delete cc;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 #if 0
00214
00215 void cc_handler(struct cc *cc)
00216 {
00217 if (read(cc->fd, cc->buffer, CC_VBIBUFSIZE) != CC_VBIBUFSIZE)
00218 {
00219 printf("Can't read vbi data\n");
00220 cc->code1 = -2;
00221 cc->code2 = -2;
00222 }
00223 else
00224 {
00225 cc->code1 = decode((unsigned char *)(cc->buffer + (2048 * 11)));
00226 cc->code2 = decode((unsigned char *)(cc->buffer + (2048 * 27)));
00227 }
00228 }
00229 #endif
00230
00231 void cc_decode(struct cc *cc)
00232 {
00233 int spl = cc->samples_per_line;
00234 int sl = cc->start_line;
00235 int l21_f1 = spl * (21 - sl);
00236 int l21_f2 = spl * (cc->line_count + 21 - sl);
00237 cc->code1 = decode((unsigned char *)(cc->buffer + l21_f1), cc->scale0, cc->scale1);
00238 cc->code2 = decode((unsigned char *)(cc->buffer + l21_f2), cc->scale0, cc->scale1);
00239 }