blob: 3d7508c08b6f3ab2c750ddf2ecd2ae2c10244782
1 | /* |
2 | * libzvbi -- Closed Caption decoder |
3 | * |
4 | * Copyright (C) 2000, 2001, 2002 Michael H. Schimek |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Library General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2 of the License, or (at your option) any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Library General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Library General Public |
17 | * License along with this library; if not, write to the |
18 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 | * Boston, MA 02110-1301 USA. |
20 | */ |
21 | |
22 | /* $Id: caption.c,v 1.28 2008/02/24 14:18:03 mschimek Exp $ */ |
23 | |
24 | #ifdef HAVE_CONFIG_H |
25 | # include "config.h" |
26 | #endif |
27 | |
28 | #include <unistd.h> |
29 | |
30 | #include "misc.h" |
31 | #include "trigger.h" |
32 | #include "format.h" |
33 | #include "lang.h" |
34 | #include "hamm.h" |
35 | #include "tables.h" |
36 | #include "vbi.h" |
37 | #include <android/log.h> |
38 | #include "am_debug.h" |
39 | |
40 | #define elements(array) (sizeof(array) / sizeof(array[0])) |
41 | |
42 | #define LOG_TAG "ZVBI" |
43 | #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) |
44 | #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) |
45 | |
46 | |
47 | #define ITV_DEBUG(x) /* x */ |
48 | #define XDS_SEP_DEBUG /* x */ printf |
49 | #define XDS_SEP_DUMP(x) /* x */ |
50 | #define CC_DUMP(x) /* x */ |
51 | #define CC_TEXT_DUMP(x) /* x */ |
52 | |
53 | static inline void |
54 | caption_send_event(vbi_decoder *vbi, vbi_event *ev) |
55 | { |
56 | /* Permits calling vbi_fetch_cc_page from handler */ |
57 | pthread_mutex_unlock(&vbi->cc.mutex); |
58 | |
59 | vbi_send_event(vbi, ev); |
60 | |
61 | pthread_mutex_lock(&vbi->cc.mutex); |
62 | } |
63 | |
64 | /* |
65 | * XDS (Extended Data Service) decoder |
66 | */ |
67 | |
68 | #define XDS_CURRENT 0 |
69 | #define XDS_FUTURE 1 |
70 | #define XDS_CHANNEL 2 |
71 | #define XDS_MISC 3 |
72 | #define XDS_PUBLIC_SERVICE 4 |
73 | #define XDS_RESERVED 5 |
74 | #define XDS_UNDEFINED 6 /* proprietary format */ |
75 | |
76 | #define XDS_END 15 |
77 | |
78 | /* vbi_classify_page, program_info language */ |
79 | static const char * |
80 | language[8] = { |
81 | "Unknown", |
82 | "English", |
83 | "Español", |
84 | "Français", |
85 | "Deutsch", |
86 | "Italiano", |
87 | "Other", |
88 | "None" |
89 | }; |
90 | |
91 | static uint32_t hcrc[128]; |
92 | |
93 | static void init_hcrc(void) __attribute__ ((constructor)); |
94 | |
95 | |
96 | /* |
97 | http://www.fcc.gov/cgb/statid.html |
98 | * |
99 | * XDS has no unique station id as EBU (or is the call sign?) |
100 | * so we create a checksum over the station name. |
101 | */ |
102 | static void |
103 | init_hcrc(void) |
104 | { |
105 | unsigned int sum; |
106 | int i, j; |
107 | |
108 | for (i = 0; i < 128; i++) { |
109 | sum = 0; |
110 | for (j = 7 - 1; j >= 0; j--) |
111 | if (i & (1 << j)) |
112 | sum ^= 0x48000000L >> j; |
113 | hcrc[i] = sum; |
114 | } |
115 | } |
116 | |
117 | static int |
118 | xds_strfu(signed char *d, const uint8_t *s, int len) |
119 | { |
120 | int c, neq = 0; |
121 | |
122 | for (; len > 0 && *s <= 0x20; s++, len--); |
123 | |
124 | for (; len > 0; s++, len--) { |
125 | c = MAX((uint8_t) 0x20, *s); |
126 | neq |= *d ^ c; |
127 | *d++ = c; |
128 | } |
129 | |
130 | neq |= *d; |
131 | *d = 0; |
132 | |
133 | return neq; |
134 | } |
135 | |
136 | #define xds_intfu(d, val) (neq |= d ^ (val), d = (val)) |
137 | |
138 | static void |
139 | flush_prog_info(vbi_decoder *vbi, vbi_program_info *pi, vbi_event *e) |
140 | { |
141 | e->ev.aspect = pi->aspect; |
142 | |
143 | vbi_reset_prog_info(pi); |
144 | |
145 | if (memcmp(&e->ev.aspect, &pi->aspect, sizeof(pi->aspect)) != 0) { |
146 | e->type = VBI_EVENT_ASPECT; |
147 | caption_send_event(vbi, e); |
148 | } |
149 | |
150 | vbi->cc.info_cycle[pi->future] = 0; |
151 | } |
152 | |
153 | static inline void |
154 | xds_decoder(vbi_decoder *vbi, int _class, int type, |
155 | uint8_t *buffer, int length) |
156 | { |
157 | XDS_SEP_DEBUG("xds_decoder _class = %d,type = %d, buffer[0]=%02x, buffer[1]=%02x length = %d,\n",_class ,type,buffer[0], buffer[1],length); |
158 | vbi_network *n = &vbi->network.ev.network; |
159 | vbi_program_info *pi; |
160 | int neq, i; |
161 | vbi_event e; |
162 | |
163 | assert(length > 0 && length <= 32); |
164 | |
165 | // XXX we have no indication how long the program info applies. |
166 | // It will be canceled on channel switch, but who knows |
167 | // what the station transmits when the next program starts. |
168 | // (Nothing, possibly.) A timeout seems necessary. |
169 | |
170 | switch (_class) { |
171 | case XDS_CURRENT: /* 0 */ |
172 | case XDS_FUTURE: /* 1 */ |
173 | if (!(vbi->event_mask & (VBI_EVENT_ASPECT | VBI_EVENT_PROG_INFO | VBI_EVENT_RATING))){ |
174 | XDS_SEP_DEBUG("vbi->event_mask & VBI_EVENT_ASPECT | VBI_EVENT_PROG_INFO"); |
175 | return; |
176 | } |
177 | pi = &vbi->prog_info[_class]; |
178 | neq = 0; |
179 | |
180 | switch (type) { |
181 | case 1: /* program identification number */ |
182 | { |
183 | int month, day, hour, min; |
184 | |
185 | if (length != 4) |
186 | return; |
187 | |
188 | month = buffer[3] & 15; |
189 | day = buffer[2] & 31; |
190 | hour = buffer[1] & 31; |
191 | min = buffer[0] & 63; |
192 | |
193 | if (month == 0 || month > 12 |
194 | || day == 0 || day > 31 |
195 | || hour > 23 || min > 59) |
196 | return; |
197 | |
198 | month--; |
199 | day--; |
200 | |
201 | neq = (pi->month ^ month) | (pi->day ^ day) |
202 | | (pi->hour ^ hour) | (pi->min ^ min); |
203 | |
204 | pi->tape_delayed = !!(buffer[3] & 0x10); |
205 | |
206 | if (neq) { |
207 | flush_prog_info(vbi, pi, &e); |
208 | |
209 | pi->month = month; |
210 | pi->day = day; |
211 | pi->hour = hour; |
212 | pi->min = min; |
213 | |
214 | pi->tape_delayed = !!(buffer[3] & 0x10); |
215 | } |
216 | |
217 | break; |
218 | } |
219 | |
220 | case 2: /* program length */ |
221 | { |
222 | int lhour, lmin, ehour = -1, emin = -1, esec = 0; |
223 | |
224 | if (length < 2 || length > 6) |
225 | return; |
226 | |
227 | lhour = buffer[1] & 63; |
228 | lmin = buffer[0] & 63; |
229 | |
230 | if (length >= 3) { |
231 | ehour = buffer[3] & 63; |
232 | emin = buffer[2] & 63; |
233 | |
234 | if (length >= 5) |
235 | esec = buffer[4] & 63; |
236 | } |
237 | |
238 | if (lmin > 59 || emin > 59 || esec > 59) |
239 | return; |
240 | |
241 | xds_intfu(pi->length_hour, lhour); |
242 | xds_intfu(pi->length_min, lmin); |
243 | xds_intfu(pi->elapsed_hour, ehour); |
244 | xds_intfu(pi->elapsed_min, emin); |
245 | xds_intfu(pi->elapsed_sec, esec); |
246 | |
247 | break; |
248 | } |
249 | |
250 | case 3: /* program name */ |
251 | if (length < 2) |
252 | return; |
253 | |
254 | neq = xds_strfu(pi->title, buffer, length); |
255 | |
256 | if (!neq) { /* no title change */ |
257 | if (!(vbi->cc.info_cycle[_class] & (1 << 3))) |
258 | break; /* already reported */ |
259 | |
260 | if (!(vbi->cc.info_cycle[_class] & (1 << 1))) { |
261 | /* Second occurence without PIN */ |
262 | |
263 | flush_prog_info(vbi, pi, &e); |
264 | |
265 | xds_strfu(pi->title, buffer, length); |
266 | vbi->cc.info_cycle[_class] |= 1 << 3; |
267 | } |
268 | } |
269 | |
270 | break; |
271 | |
272 | case 4: /* program type */ |
273 | { |
274 | int neq; |
275 | |
276 | neq = (pi->type_classf != VBI_PROG_CLASSF_EIA_608); |
277 | pi->type_classf = VBI_PROG_CLASSF_EIA_608; |
278 | |
279 | for (i = 0; i < length; i++) { |
280 | neq |= pi->type_id[i] ^ buffer[i]; |
281 | pi->type_id[i] = buffer[i]; |
282 | } |
283 | |
284 | neq |= pi->type_id[i]; |
285 | pi->type_id[i] = 0; |
286 | |
287 | break; |
288 | } |
289 | |
290 | case 5: /* program rating */ |
291 | { |
292 | vbi_rating_auth auth; |
293 | int r, g, dlsv = 0; |
294 | |
295 | if (length != 2) |
296 | return; |
297 | |
298 | r = buffer[0] & 7; |
299 | g = buffer[1] & 7; |
300 | XDS_SEP_DEBUG("r = %d,g = %d\n",r,g); |
301 | if (buffer[0] & 0x20) |
302 | dlsv |= VBI_RATING_D; |
303 | if (buffer[1] & 0x08) |
304 | dlsv |= VBI_RATING_L; |
305 | if (buffer[1] & 0x10) |
306 | dlsv |= VBI_RATING_S; |
307 | if (buffer[1] & 0x20) |
308 | dlsv |= VBI_RATING_V; |
309 | |
310 | if ((buffer[0] & 0x08) == 0) { |
311 | //if (r == 0) return; |
312 | auth = VBI_RATING_AUTH_MPAA; |
313 | pi->rating.dlsv = dlsv = 0; |
314 | } else if ((buffer[0] & 0x10) == 0) { |
315 | auth = VBI_RATING_AUTH_TV_US; |
316 | r = g; |
317 | } else if ((buffer[1] & 0x08) == 0) { |
318 | if ((buffer[0] & 0x20) == 0) { |
319 | if ((r = g) > 6) return; |
320 | auth = VBI_RATING_AUTH_TV_CA_EN; |
321 | } else { |
322 | if ((r = g) > 5) return; |
323 | auth = VBI_RATING_AUTH_TV_CA_FR; |
324 | } |
325 | pi->rating.dlsv = dlsv = 0; |
326 | } else |
327 | return; |
328 | |
329 | if ((neq = (pi->rating.auth != auth |
330 | || pi->rating.id != r |
331 | || pi->rating.dlsv != dlsv))) { |
332 | pi->rating.auth = auth; |
333 | pi->rating.id = r; |
334 | pi->rating.dlsv = dlsv; |
335 | } |
336 | if (vbi->event_mask & VBI_EVENT_RATING){ |
337 | e.type = VBI_EVENT_RATING; |
338 | e.ev.prog_info = pi; |
339 | caption_send_event(vbi, &e); |
340 | } |
341 | |
342 | break; |
343 | } |
344 | |
345 | case 6: /* program audio services */ |
346 | { |
347 | static const vbi_audio_mode mode[2][8] = { |
348 | { |
349 | VBI_AUDIO_MODE_UNKNOWN, |
350 | VBI_AUDIO_MODE_MONO, |
351 | VBI_AUDIO_MODE_SIMULATED_STEREO, |
352 | VBI_AUDIO_MODE_STEREO, |
353 | VBI_AUDIO_MODE_STEREO_SURROUND, |
354 | VBI_AUDIO_MODE_DATA_SERVICE, |
355 | VBI_AUDIO_MODE_UNKNOWN, /* "other" */ |
356 | VBI_AUDIO_MODE_NONE |
357 | }, { |
358 | VBI_AUDIO_MODE_UNKNOWN, |
359 | VBI_AUDIO_MODE_MONO, |
360 | VBI_AUDIO_MODE_VIDEO_DESCRIPTIONS, |
361 | VBI_AUDIO_MODE_NON_PROGRAM_AUDIO, |
362 | VBI_AUDIO_MODE_SPECIAL_EFFECTS, |
363 | VBI_AUDIO_MODE_DATA_SERVICE, |
364 | VBI_AUDIO_MODE_UNKNOWN, /* "other" */ |
365 | VBI_AUDIO_MODE_NONE |
366 | } |
367 | }; |
368 | |
369 | if (length != 2) |
370 | return; |
371 | |
372 | for (i = 0; i < 2; i++) { |
373 | int l = (buffer[i] >> 3) & 7; |
374 | vbi_audio_mode m = mode[i][buffer[i] & 7]; |
375 | /* should be const char *, but I got that |
376 | wrong and cannot change the public |
377 | pi->audio[].language type now. */ |
378 | unsigned char *s = ((1 << l) & 0xC1) ? NULL : |
379 | (unsigned char *) language[l]; |
380 | |
381 | if (pi->audio[i].mode != m) { |
382 | neq = 1; pi->audio[i].mode = m; |
383 | } |
384 | if (pi->audio[i].language != s) { |
385 | neq = 1; pi->audio[i].language = s; |
386 | } |
387 | } |
388 | |
389 | break; |
390 | } |
391 | |
392 | case 7: /* program caption services */ |
393 | { |
394 | int services = 0; |
395 | |
396 | if (length > 8) |
397 | return; |
398 | |
399 | for (i = 0; i < 8; i++) |
400 | pi->caption_language[i] = NULL; |
401 | |
402 | for (i = 0; i < length; i++) { |
403 | int ch = buffer[i] & 7; |
404 | int l = (buffer[i] >> 3) & 7; |
405 | /* should be const char *, but I got that |
406 | wrong and cannot change the public |
407 | pi->audio[].language type now. */ |
408 | unsigned char *s; |
409 | |
410 | ch = (ch & 1) * 4 + (ch >> 1); |
411 | |
412 | services |= 1 << ch; |
413 | s = ((1 << l) & 0xC1) ? NULL : |
414 | (unsigned char *) language[l]; |
415 | |
416 | if (pi->caption_language[ch] != (unsigned char *) s) { |
417 | neq = 1; pi->caption_language[ch] = (unsigned char *) s; |
418 | } |
419 | |
420 | if (_class == XDS_CURRENT) |
421 | vbi->cc.channel[ch].language = |
422 | pi->caption_language[ch]; |
423 | } |
424 | |
425 | xds_intfu(pi->caption_services, services); |
426 | |
427 | break; |
428 | } |
429 | |
430 | case 8: /* copy generation management system */ |
431 | if (length != 1) |
432 | return; |
433 | |
434 | xds_intfu(pi->cgms_a, buffer[0] & 63); |
435 | |
436 | break; |
437 | |
438 | case 9: /* program aspect ratio */ |
439 | { |
440 | vbi_aspect_ratio *r = &e.ev.aspect; |
441 | |
442 | if (length > 3) |
443 | return; |
444 | |
445 | memset(&e, 0, sizeof(e)); |
446 | |
447 | r->first_line = (buffer[0] & 63) + 22; |
448 | r->last_line = 262 - (buffer[1] & 63); |
449 | r->film_mode = 0; |
450 | r->open_subtitles = VBI_SUBT_UNKNOWN; |
451 | |
452 | if (length >= 3 && (buffer[2] & 1)) |
453 | r->ratio = 16.0 / 9.0; |
454 | else |
455 | r->ratio = 1.0; |
456 | |
457 | if (memcmp(r, &vbi->prog_info[0].aspect, sizeof(*r)) != 0) { |
458 | vbi->prog_info[0].aspect = *r; |
459 | vbi->aspect_source = 3; |
460 | |
461 | e.type = VBI_EVENT_ASPECT; |
462 | caption_send_event(vbi, &e); |
463 | |
464 | neq = 1; |
465 | } |
466 | |
467 | break; |
468 | } |
469 | |
470 | case 0x10 ... 0x17: /* program description */ |
471 | { |
472 | int line = type & 7; |
473 | |
474 | neq = xds_strfu(pi->description[line], buffer, length); |
475 | |
476 | break; |
477 | } |
478 | |
479 | default: |
480 | return; /* no event */ |
481 | } |
482 | |
483 | if (0) |
484 | printf("[type %d cycle %08x class %d neq %d]\n", |
485 | type, vbi->cc.info_cycle[_class], _class, neq); |
486 | |
487 | if (neq){ /* first occurence of this type with this data */ |
488 | vbi->cc.info_cycle[_class] |= 1 << type; |
489 | } |
490 | else if (vbi->cc.info_cycle[_class] & (1 << type)) { |
491 | /* Second occurance of this type with same data */ |
492 | |
493 | e.type = VBI_EVENT_PROG_INFO; |
494 | e.ev.prog_info = pi; |
495 | |
496 | caption_send_event(vbi, &e); |
497 | XDS_SEP_DEBUG("caption_send_event VBI_EVENT_PROG_INFO\n"); |
498 | vbi->cc.info_cycle[_class] = 0; /* all changes reported */ |
499 | } |
500 | |
501 | break; |
502 | |
503 | case XDS_CHANNEL: |
504 | n->ts_id = -1; |
505 | |
506 | switch (type) { |
507 | case 1: /* network name */ |
508 | if (xds_strfu(n->name, buffer, length)) { |
509 | n->cycle = 1; |
510 | } else if (n->cycle == 1) { |
511 | signed char *s = n->name; |
512 | uint32_t sum; |
513 | |
514 | if (n->call[0]) |
515 | s = n->call; |
516 | |
517 | for (sum = 0; *s; s++) |
518 | sum = (sum >> 7) ^ hcrc[(sum ^ *s) & 0x7F]; |
519 | |
520 | sum &= ((1UL << 31) - 1); |
521 | sum |= 1UL << 30; |
522 | |
523 | if (n->nuid != 0) |
524 | vbi_chsw_reset(vbi, sum); |
525 | |
526 | n->nuid = sum; |
527 | |
528 | vbi->network.type = VBI_EVENT_NETWORK; |
529 | caption_send_event(vbi, &vbi->network); |
530 | |
531 | vbi->network.type = VBI_EVENT_NETWORK_ID; |
532 | caption_send_event(vbi, &vbi->network); |
533 | |
534 | n->cycle = 3; |
535 | } |
536 | |
537 | break; |
538 | |
539 | case 2: /* network call letters */ |
540 | if (xds_strfu(n->call, buffer, length)) { |
541 | if (n->cycle != 1) { |
542 | n->name[0] = 0; |
543 | n->cycle = 0; |
544 | } |
545 | } |
546 | |
547 | break; |
548 | |
549 | case 3: /* channel tape delay */ |
550 | if (length != 2) |
551 | return; |
552 | |
553 | n->tape_delay = |
554 | (buffer[1] & 31) * 60 + (buffer[0] & 63); |
555 | |
556 | break; |
557 | |
558 | case 4: /* Transmission Signal ID */ |
559 | if (length < 4) |
560 | return; |
561 | |
562 | n->ts_id = |
563 | ((buffer[3] & 15) << 0) |
564 | |((buffer[2] & 15) << 4) |
565 | |((buffer[1] & 15) << 8) |
566 | |((buffer[0] & 15) << 12); |
567 | |
568 | vbi->network.type = VBI_EVENT_NETWORK; |
569 | caption_send_event(vbi, &vbi->network); |
570 | break; |
571 | |
572 | default: |
573 | break; |
574 | } |
575 | |
576 | break; |
577 | |
578 | case XDS_MISC: |
579 | switch (type) { |
580 | case 1: /* time of day */ |
581 | if (length != 6) |
582 | return; |
583 | break; |
584 | |
585 | case 2: /* impulse capture id */ |
586 | if (length != 6) |
587 | return; |
588 | break; |
589 | |
590 | case 3: /* supplemental data location */ |
591 | break; |
592 | |
593 | case 4: /* local time zone */ |
594 | if (length != 1) |
595 | return; |
596 | break; |
597 | |
598 | case 0x40: /* out-of-band channel number */ |
599 | if (length != 2) |
600 | return; |
601 | break; |
602 | |
603 | default: |
604 | break; |
605 | } |
606 | |
607 | break; |
608 | |
609 | default: |
610 | break; |
611 | } |
612 | } |
613 | |
614 | static void |
615 | xds_separator(vbi_decoder *vbi, uint8_t *buf) |
616 | { |
617 | struct caption *cc = &vbi->cc; |
618 | xds_sub_packet *sp = cc->curr_sp; |
619 | int c1 = vbi_unpar8 (buf[0]); |
620 | int c2 = vbi_unpar8 (buf[1]); |
621 | unsigned int class, type; |
622 | |
623 | XDS_SEP_DEBUG("XDS %02x %02x\n", buf[0], buf[1]); |
624 | |
625 | if ((c1 | c2) < 0) { |
626 | XDS_SEP_DEBUG("XDS tx error, discard current packet\n"); |
627 | |
628 | if (sp) { |
629 | sp->count = 0; |
630 | sp->chksum = 0; |
631 | sp = NULL; |
632 | } |
633 | |
634 | return; |
635 | } |
636 | |
637 | switch (c1) { |
638 | case 1 ... 14: |
639 | class = (c1 - 1) >> 1; |
640 | |
641 | if (class > elements(cc->sub_packet) |
642 | || c2 > (int) elements(cc->sub_packet[0])) { |
643 | XDS_SEP_DEBUG("XDS ignore packet %d/0x%02x\n", class, c2); |
644 | cc->curr_sp = NULL; |
645 | return; |
646 | } |
647 | |
648 | cc->curr_sp = sp = &cc->sub_packet[class][c2]; |
649 | |
650 | if (c1 & 1) { /* start */ |
651 | sp->chksum = c1 + c2; |
652 | sp->count = 2; |
653 | } else if (!sp->count) { |
654 | XDS_SEP_DEBUG("XDS can't continue %d/0x%02x\n", class, c2); |
655 | cc->curr_sp = NULL; |
656 | } |
657 | |
658 | return; |
659 | |
660 | case 15: |
661 | if (!sp) |
662 | return; |
663 | |
664 | sp->chksum += c1 + c2; |
665 | |
666 | class = (sp - cc->sub_packet[0]) / elements(cc->sub_packet[0]); |
667 | type = (sp - cc->sub_packet[0]) % elements(cc->sub_packet[0]); |
668 | |
669 | if (sp->chksum & 0x7F) { |
670 | XDS_SEP_DEBUG("XDS ignore packet %d/0x%02x, " |
671 | "checksum error\n", class, type); |
672 | } else if (sp->count <= 2) { |
673 | XDS_SEP_DEBUG("XDS ignore empty packet " |
674 | "%d/0x%02x\n", class, type); |
675 | } else { |
676 | xds_decoder(vbi, class, type, sp->buffer, sp->count - 2); |
677 | |
678 | XDS_SEP_DUMP( |
679 | for (i = 0; i < sp->count - 2; i++) |
680 | printf("%c", (sp->buffer[i])); //printf("%c", _vbi_to_ascii (sp->buffer[i])); |
681 | printf(" %d/0x%02x\n", class, type); |
682 | ) |
683 | } |
684 | |
685 | sp->count = 0; |
686 | sp->chksum = 0; |
687 | cc->curr_sp = NULL; |
688 | |
689 | return; |
690 | |
691 | case 0x20 ... 0x7F: |
692 | if (!sp) |
693 | return; |
694 | |
695 | if (sp->count >= 32 + 2) { |
696 | XDS_SEP_DEBUG("XDS packet length overflow, discard %d/0x%02x\n", |
697 | (sp - cc->sub_packet[0]) / elements(cc->sub_packet[0]), |
698 | (sp - cc->sub_packet[0]) % elements(cc->sub_packet[0])); |
699 | |
700 | sp->count = 0; |
701 | sp->chksum = 0; |
702 | cc->curr_sp = NULL; |
703 | return; |
704 | } |
705 | |
706 | sp->buffer[sp->count - 2] = c1; |
707 | sp->buffer[sp->count - 1] = c2; |
708 | sp->chksum += c1 + c2; |
709 | sp->count += 1 + !!c2; |
710 | |
711 | return; |
712 | |
713 | default: |
714 | assert(!"reached"); |
715 | } |
716 | } |
717 | |
718 | static void |
719 | itv_separator(vbi_decoder *vbi, struct caption *cc, char c) |
720 | { |
721 | if (ITV_DEBUG(0 &&) !(vbi->event_mask & VBI_EVENT_TRIGGER)) |
722 | return; |
723 | |
724 | if (c >= 0x20) { |
725 | if (c == '<') // s4-nbc omitted CR |
726 | itv_separator(vbi, cc, 0); |
727 | else if (cc->itv_count > (int) sizeof(cc->itv_buf) - 2) |
728 | cc->itv_count = 0; |
729 | |
730 | cc->itv_buf[cc->itv_count++] = c; |
731 | |
732 | return; |
733 | } |
734 | |
735 | cc->itv_buf[cc->itv_count] = 0; |
736 | cc->itv_count = 0; |
737 | |
738 | ITV_DEBUG(printf("ITV: <%s>\n", cc->itv_buf)); |
739 | |
740 | vbi_atvef_trigger(vbi, cc->itv_buf); |
741 | } |
742 | |
743 | /* |
744 | * Closed Caption decoder |
745 | */ |
746 | |
747 | #define ROWS 15 |
748 | #define COLUMNS 34 |
749 | |
750 | static void |
751 | render(vbi_page *pg, int row) |
752 | { |
753 | vbi_event event; |
754 | |
755 | if (row < 0 || pg->dirty.roll) { |
756 | /* no particular row or not fetched |
757 | since last roll/clear, redraw all */ |
758 | pg->dirty.y0 = 0; |
759 | pg->dirty.y1 = ROWS - 1; |
760 | pg->dirty.roll = 0; |
761 | } else { |
762 | pg->dirty.y0 = MIN(row, pg->dirty.y0); |
763 | pg->dirty.y1 = MAX(row, pg->dirty.y1); |
764 | } |
765 | |
766 | event.type = VBI_EVENT_CAPTION; |
767 | event.ev.caption.pgno = pg->pgno; |
768 | |
769 | caption_send_event(pg->vbi, &event); |
770 | } |
771 | |
772 | static void |
773 | clear(vbi_page *pg) |
774 | { |
775 | vbi_event event; |
776 | |
777 | pg->dirty.y0 = 0; |
778 | pg->dirty.y1 = ROWS - 1; |
779 | pg->dirty.roll = -ROWS; |
780 | |
781 | event.type = VBI_EVENT_CAPTION; |
782 | event.ev.caption.pgno = pg->pgno; |
783 | |
784 | caption_send_event(pg->vbi, &event); |
785 | } |
786 | |
787 | static void |
788 | roll_up(vbi_page *pg, int first_row, int last_row) |
789 | { |
790 | vbi_event event; |
791 | |
792 | if (pg->dirty.roll != 0 || pg->dirty.y0 <= pg->dirty.y1) { |
793 | /* not fetched since last update, redraw all */ |
794 | pg->dirty.roll = 0; |
795 | pg->dirty.y0 = MIN(first_row, pg->dirty.y0); |
796 | pg->dirty.y1 = MAX(last_row, pg->dirty.y1); |
797 | } else { |
798 | pg->dirty.roll = -1; |
799 | pg->dirty.y0 = first_row; |
800 | pg->dirty.y1 = last_row; |
801 | } |
802 | |
803 | event.type = VBI_EVENT_CAPTION; |
804 | event.ev.caption.pgno = pg->pgno; |
805 | |
806 | caption_send_event(pg->vbi, &event); |
807 | } |
808 | |
809 | static inline void |
810 | update(cc_channel *ch) |
811 | { |
812 | #if 1 |
813 | int i; |
814 | vbi_char *acp = ch->line - ch->pg[ch->hidden].text + ch->pg[ch->hidden^1].text; |
815 | |
816 | memcpy(acp, ch->line, sizeof(*acp) * COLUMNS); |
817 | render(ch->pg + 1, ch->row); |
818 | #if 0 |
819 | AM_DEBUG(1,"+++ Display line"); |
820 | for (i=0;i<sizeof(*acp) * COLUMNS;i++) |
821 | { |
822 | AM_DEBUG(1,"+++ %x ", ch->line[i].unicode); |
823 | } |
824 | #endif |
825 | #endif |
826 | } |
827 | |
828 | static void |
829 | word_break(struct caption *cc, cc_channel *ch, int upd) |
830 | { |
831 | cc = cc; |
832 | |
833 | /* |
834 | * Add a leading and trailing space. |
835 | */ |
836 | #if 0 |
837 | if (ch->col > ch->col1) { |
838 | vbi_char c = ch->line[ch->col1]; |
839 | |
840 | if ((c.unicode & 0x7F) != 0x20 |
841 | && ch->line[ch->col1 - 1].opacity == VBI_TRANSPARENT_SPACE) { |
842 | c.unicode = 0x20; |
843 | ch->line[ch->col1 - 1] = c; |
844 | } |
845 | |
846 | c = ch->line[ch->col - 1]; |
847 | |
848 | if ((c.unicode & 0x7F) != 0x20 |
849 | && ch->line[ch->col].opacity == VBI_TRANSPARENT_SPACE) { |
850 | c.unicode = 0x20; |
851 | ch->line[ch->col] = c; |
852 | } |
853 | } |
854 | #endif |
855 | if (!upd || ch->mode == MODE_POP_ON) |
856 | return; |
857 | |
858 | /* |
859 | * NB we render only at spaces (end of word) and |
860 | * before cursor motions and mode switching, to keep the |
861 | * drawing efforts (scaling etc) at a minimum. update() |
862 | * for double buffering at word granularity. |
863 | * |
864 | * XXX should not render if space follows space, |
865 | * but force in long words. |
866 | */ |
867 | ch->update = 1; |
868 | } |
869 | |
870 | static inline void |
871 | set_cursor(cc_channel *ch, int col, int row) |
872 | { |
873 | ch->col = ch->col1 = col; |
874 | ch->row = row; |
875 | |
876 | ch->line = ch->pg[ch->hidden].text + row * COLUMNS; |
877 | } |
878 | |
879 | static void |
880 | put_char(struct caption *cc, cc_channel *ch, vbi_char c) |
881 | { |
882 | /* c.foreground = rand() & 7; */ |
883 | /* c.background = rand() & 7; */ |
884 | |
885 | if (ch->col < COLUMNS - 1) |
886 | ch->line[ch->col++] = c; |
887 | else { |
888 | /* line break here? */ |
889 | |
890 | ch->line[COLUMNS - 2] = c; |
891 | } |
892 | ch->update = 1; |
893 | //if ((c.unicode & 0x7F) == 0x20) |
894 | // word_break(cc, ch, 1); |
895 | //AM_DEBUG(1, "add render in putchar"); |
896 | } |
897 | |
898 | static inline cc_channel * |
899 | switch_channel(struct caption *cc, cc_channel *ch, int new_chan) |
900 | { |
901 | word_break(cc, ch, 1); // we leave for a number of frames |
902 | |
903 | return &cc->channel[cc->curr_chan = new_chan]; |
904 | } |
905 | |
906 | static void |
907 | erase_memory(struct caption *cc, cc_channel *ch, int page) |
908 | { |
909 | vbi_page *pg = ch->pg + page; |
910 | vbi_char *acp = pg->text; |
911 | memset(pg->text, 0, 1056*sizeof(vbi_char)); |
912 | #if 0 |
913 | //vbi_char c = cc->transp_space[ch >= &cc->channel[4]]; |
914 | vbi_char c = {0}; |
915 | int i,j; |
916 | for (i = 0; i < COLUMNS * ROWS; acp++, i++) |
917 | *acp = c; |
918 | |
919 | pg->dirty.y0 = 0; |
920 | pg->dirty.y1 = ROWS - 1; |
921 | pg->dirty.roll = ROWS; |
922 | #endif |
923 | } |
924 | |
925 | static const vbi_color |
926 | palette_mapping[8] = { |
927 | VBI_WHITE, VBI_GREEN, VBI_BLUE, VBI_CYAN, |
928 | VBI_RED, VBI_YELLOW, VBI_MAGENTA, VBI_BLACK |
929 | }; |
930 | |
931 | static int |
932 | row_mapping[] = { |
933 | 10, -1, 0, 1, 2, 3, 11, 12, 13, 14, 4, 5, 6, 7, 8, 9 |
934 | }; |
935 | |
936 | // not verified means I didn't encounter the code in a |
937 | // sample stream yet |
938 | |
939 | _vbi_inline void |
940 | caption_command(vbi_decoder *vbi, struct caption *cc, |
941 | unsigned char c1, unsigned char c2, vbi_bool field2) |
942 | { |
943 | XDS_SEP_DEBUG("caption_command\n"); |
944 | cc_channel *ch; |
945 | int chan, col, i; |
946 | int last_row; |
947 | |
948 | chan = (cc->curr_chan & 4) + field2 * 2 + ((c1 >> 3) & 1); |
949 | ch = &cc->channel[chan]; |
950 | |
951 | c1 &= 7; |
952 | |
953 | if (c2 >= 0x40) { /* Preamble Address Codes 001 crrr 1ri xxxu */ |
954 | int row = row_mapping[(c1 << 1) + ((c2 >> 5) & 1)]; |
955 | |
956 | if (row < 0 || !ch->mode) |
957 | return; |
958 | |
959 | ch->attr.underline = c2 & 1; |
960 | ch->attr.background = VBI_BLACK; |
961 | ch->attr.opacity = VBI_OPAQUE; |
962 | ch->attr.flash = FALSE; |
963 | |
964 | word_break(cc, ch, 1); |
965 | |
966 | if (ch->mode == MODE_ROLL_UP) { |
967 | int row1 = row - ch->roll + 1; |
968 | |
969 | if (row1 < 0) |
970 | row1 = 0; |
971 | |
972 | if (row1 != ch->row1) { |
973 | ch->row1 = row1; |
974 | erase_memory(cc, ch, ch->hidden); |
975 | erase_memory(cc, ch, ch->hidden ^ 1); |
976 | } |
977 | |
978 | set_cursor(ch, 1, ch->row1 + ch->roll - 1); |
979 | } else |
980 | set_cursor(ch, 1, row); |
981 | |
982 | if (c2 & 0x10) { |
983 | col = ch->col; |
984 | |
985 | for (i = (c2 & 14) * 2; i > 0 && col < COLUMNS - 1; i--) |
986 | ch->line[col++] = cc->transp_space[chan >> 2]; |
987 | |
988 | if (col > ch->col) |
989 | ch->col = ch->col1 = col; |
990 | |
991 | ch->attr.italic = FALSE; |
992 | ch->attr.foreground = VBI_WHITE; |
993 | } else { |
994 | // not verified |
995 | c2 = (c2 >> 1) & 7; |
996 | |
997 | if (c2 < 7) { |
998 | ch->attr.italic = FALSE; |
999 | ch->attr.foreground = palette_mapping[c2]; |
1000 | } else { |
1001 | ch->attr.italic = TRUE; |
1002 | //ch->attr.foreground = VBI_WHITE; |
1003 | } |
1004 | } |
1005 | |
1006 | return; |
1007 | } |
1008 | |
1009 | switch (c1) { |
1010 | case 0: /* Optional Attributes 001 c000 010 xxxt */ |
1011 | // not verified |
1012 | ch->attr.opacity = (c2 & 1) ? VBI_SEMI_TRANSPARENT : VBI_OPAQUE; |
1013 | ch->attr.background = palette_mapping[(c2 >> 1) & 7]; |
1014 | return; |
1015 | |
1016 | case 1: |
1017 | if (c2 & 0x10) { /* Special Characters 001 c001 011 xxxx */ |
1018 | // not verified |
1019 | c2 &= 15; |
1020 | |
1021 | if (c2 == 9) { // "transparent space" |
1022 | if (ch->col < COLUMNS - 1) { |
1023 | ch->line[ch->col++] = cc->transp_space[chan >> 2]; |
1024 | ch->col1 = ch->col; |
1025 | } else |
1026 | ch->line[COLUMNS - 2] = cc->transp_space[chan >> 2]; |
1027 | // XXX boxed logic? |
1028 | } else { |
1029 | vbi_char c = ch->attr; |
1030 | |
1031 | c.unicode = vbi_caption_unicode (0x1130 | (c2 & 15), |
1032 | /* to_upper */ FALSE); |
1033 | XDS_SEP_DEBUG("vbi_caption_unicode %c\n",c.unicode); |
1034 | put_char(cc, ch, c); |
1035 | } |
1036 | } else { /* Midrow Codes 001 c001 010 xxxu */ |
1037 | // not verified |
1038 | ch->attr.flash = FALSE; |
1039 | ch->attr.underline = c2 & 1; |
1040 | |
1041 | c2 = (c2 >> 1) & 7; |
1042 | |
1043 | if (c2 < 7) { |
1044 | ch->attr.italic = FALSE; |
1045 | ch->attr.foreground = palette_mapping[c2]; |
1046 | } else { |
1047 | ch->attr.italic = TRUE; |
1048 | //ch->attr.foreground = VBI_WHITE; |
1049 | } |
1050 | } |
1051 | |
1052 | return; |
1053 | |
1054 | case 2: /* Optional Extended Characters 001 c01f 01x xxxx */ |
1055 | case 3: |
1056 | /* Send specs to the maintainer of this code */ |
1057 | return; |
1058 | |
1059 | case 4: /* Misc Control Codes 001 c10f 010 xxxx */ |
1060 | case 5: /* Misc Control Codes 001 c10f 010 xxxx */ |
1061 | /* f ("field"): purpose? */ |
1062 | |
1063 | switch (c2 & 15) { |
1064 | case 0: /* Resume Caption Loading 001 c10f 010 0000 */ |
1065 | ch = switch_channel(cc, ch, chan & 3); |
1066 | |
1067 | ch->mode = MODE_POP_ON; |
1068 | |
1069 | // no? erase_memory(cc, ch); |
1070 | |
1071 | return; |
1072 | |
1073 | /* case 4: reserved */ |
1074 | |
1075 | case 5: /* Roll-Up Captions 001 c10f 010 0xxx */ |
1076 | case 6: |
1077 | case 7: |
1078 | { |
1079 | int roll = (c2 & 7) - 3; |
1080 | |
1081 | ch = switch_channel(cc, ch, chan & 3); |
1082 | |
1083 | if (ch->mode == MODE_ROLL_UP && ch->roll == roll) |
1084 | return; |
1085 | |
1086 | erase_memory(cc, ch, ch->hidden); |
1087 | erase_memory(cc, ch, ch->hidden ^ 1); |
1088 | |
1089 | ch->mode = MODE_ROLL_UP; |
1090 | ch->roll = roll; |
1091 | |
1092 | set_cursor(ch, 1, 14); |
1093 | |
1094 | ch->row1 = 14 - roll + 1; |
1095 | |
1096 | return; |
1097 | } |
1098 | |
1099 | case 9: /* Resume Direct Captioning 001 c10f 010 1001 */ |
1100 | // not verified |
1101 | ch = switch_channel(cc, ch, chan & 3); |
1102 | ch->mode = MODE_PAINT_ON; |
1103 | return; |
1104 | |
1105 | case 10: /* Text Restart 001 c10f 010 1010 */ |
1106 | // not verified |
1107 | ch = switch_channel(cc, ch, chan | 4); |
1108 | set_cursor(ch, 1, 0); |
1109 | return; |
1110 | |
1111 | case 11: /* Resume Text Display 001 c10f 010 1011 */ |
1112 | ch = switch_channel(cc, ch, chan | 4); |
1113 | return; |
1114 | |
1115 | case 15: /* End Of Caption 001 c10f 010 1111 */ |
1116 | ch = switch_channel(cc, ch, chan & 3); |
1117 | ch->mode = MODE_POP_ON; |
1118 | |
1119 | word_break(cc, ch, 1); |
1120 | |
1121 | ch->hidden ^= 1; |
1122 | |
1123 | render(ch->pg + (ch->hidden ^ 1), -1 /* ! */); |
1124 | |
1125 | erase_memory(cc, ch, ch->hidden); // yes? |
1126 | |
1127 | /* |
1128 | * A Preamble Address Code should follow, |
1129 | * reset to a known state to be safe. |
1130 | * Reset ch->line for new ch->hidden. |
1131 | * XXX row 0? |
1132 | */ |
1133 | set_cursor(ch, 1, ROWS - 1); |
1134 | |
1135 | return; |
1136 | |
1137 | case 8: /* Flash On 001 c10f 010 1000 */ |
1138 | // not verified |
1139 | ch->attr.flash = TRUE; |
1140 | return; |
1141 | |
1142 | case 1: /* Backspace 001 c10f 010 0001 */ |
1143 | // not verified |
1144 | if (ch->mode) { |
1145 | if (ch->col > 1) { |
1146 | ch->col --; |
1147 | } else if (ch->row > 1) { |
1148 | vbi_char *acp; |
1149 | |
1150 | ch->row --; |
1151 | ch->line = ch->pg[ch->hidden].text + ch->row * COLUMNS; |
1152 | ch->col1 = 1; |
1153 | ch->col = COLUMNS - 1; |
1154 | acp = ch->line + COLUMNS - 1; |
1155 | |
1156 | while (ch->col > 0) { |
1157 | if (acp->unicode != 0) |
1158 | break; |
1159 | |
1160 | acp --; |
1161 | ch->col --; |
1162 | } |
1163 | } else { |
1164 | return; |
1165 | } |
1166 | |
1167 | memset(&ch->line[ch->col], 0, sizeof(vbi_char)); |
1168 | if (ch->col < ch->col1) |
1169 | ch->col1 = ch->col; |
1170 | ch->update = 1; |
1171 | } |
1172 | |
1173 | return; |
1174 | |
1175 | case 13: /* Carriage Return 001 c10f 010 1101 */ |
1176 | if (ch == cc->channel + 5) |
1177 | itv_separator(vbi, cc, 0); |
1178 | |
1179 | if (!ch->mode) |
1180 | return; |
1181 | |
1182 | last_row = ch->row1 + ch->roll - 1; |
1183 | |
1184 | if (last_row > ROWS - 1) |
1185 | last_row = ROWS - 1; |
1186 | |
1187 | if (ch->row < last_row) { |
1188 | word_break(cc, ch, 1); |
1189 | set_cursor(ch, 1, ch->row + 1); |
1190 | } else { |
1191 | vbi_char *acp = &ch->pg[ch->hidden ^ (ch->mode != MODE_POP_ON)] |
1192 | .text[ch->row1 * COLUMNS]; |
1193 | |
1194 | word_break(cc, ch, 1); |
1195 | ch->update = 1; |
1196 | |
1197 | if (ch->mode == MODE_ROLL_UP) |
1198 | { |
1199 | memmove(acp, acp + COLUMNS, sizeof(*acp) * (ch->roll - 1) * COLUMNS); |
1200 | |
1201 | for (i = 0; i <= COLUMNS; i++) |
1202 | ch->line[i] = cc->transp_space[chan >> 2]; |
1203 | } |
1204 | |
1205 | if (ch->mode != MODE_POP_ON) { |
1206 | ch->update = 1; |
1207 | roll_up(ch->pg + (ch->hidden ^ 1), ch->row1, last_row); |
1208 | } |
1209 | |
1210 | ch->col1 = ch->col = 1; |
1211 | } |
1212 | |
1213 | return; |
1214 | |
1215 | case 4: /* Delete To End Of Row 001 c10f 010 0100 */ |
1216 | // not verified |
1217 | if (!ch->mode) |
1218 | return; |
1219 | |
1220 | for (i = ch->col; i <= COLUMNS - 1; i++) |
1221 | ch->line[i] = cc->transp_space[chan >> 2]; |
1222 | |
1223 | word_break(cc, ch, 0); |
1224 | |
1225 | if (ch->mode != MODE_POP_ON) { |
1226 | ch->update = 1; |
1227 | render(ch->pg + (ch->hidden ^ 1), ch->row); |
1228 | } |
1229 | |
1230 | return; |
1231 | |
1232 | case 12: /* Erase Displayed Memory 001 c10f 010 1100 */ |
1233 | // s1, s4: EDM always before EOC |
1234 | /* Received in text mode */ |
1235 | if (cc->curr_chan >= 4) |
1236 | { |
1237 | for (i = 0; i < 2; i++ ) |
1238 | { |
1239 | ch = &cc->channel[i + (cc->curr_chan - 4)/2]; |
1240 | erase_memory(cc, ch, 0); |
1241 | erase_memory(cc, ch, 1); |
1242 | } |
1243 | } |
1244 | else |
1245 | { |
1246 | |
1247 | if (ch->mode != MODE_POP_ON) |
1248 | erase_memory(cc, ch, ch->hidden); |
1249 | |
1250 | erase_memory(cc, ch, ch->hidden ^ 1); |
1251 | |
1252 | |
1253 | clear(ch->pg + (ch->hidden ^ 1)); |
1254 | } |
1255 | |
1256 | return; |
1257 | case 14: /* Erase Non-Displayed Memory 001 c10f 010 1110 */ |
1258 | // not verified |
1259 | if (ch->mode == MODE_POP_ON) |
1260 | erase_memory(cc, ch, ch->hidden); |
1261 | |
1262 | return; |
1263 | } |
1264 | |
1265 | return; |
1266 | |
1267 | /* case 6: reserved */ |
1268 | |
1269 | case 7: |
1270 | if (!ch->mode) |
1271 | return; |
1272 | |
1273 | switch (c2) { |
1274 | case 0x21 ... 0x23: /* Misc Control Codes, Tabs 001 c111 010 00xx */ |
1275 | // not verified |
1276 | col = ch->col; |
1277 | |
1278 | for (i = c2 & 3; i > 0 && col < COLUMNS - 1; i--) |
1279 | ch->line[col++] = cc->transp_space[chan >> 2]; |
1280 | |
1281 | if (col > ch->col) |
1282 | ch->col = ch->col1 = col; |
1283 | |
1284 | return; |
1285 | |
1286 | case 0x2D: /* Optional Attributes 001 c111 010 11xx */ |
1287 | // not verified |
1288 | ch->attr.opacity = VBI_TRANSPARENT_FULL; |
1289 | break; |
1290 | |
1291 | case 0x2E: /* Optional Attributes 001 c111 010 11xx */ |
1292 | case 0x2F: |
1293 | // not verified |
1294 | ch->attr.foreground = VBI_BLACK; |
1295 | ch->attr.underline = c2 & 1; |
1296 | break; |
1297 | |
1298 | default: |
1299 | return; |
1300 | } |
1301 | |
1302 | /* Optional Attributes, backspace magic */ |
1303 | |
1304 | if (ch->col > 1 && (ch->line[ch->col - 1].unicode & 0x7F) == 0x20) { |
1305 | vbi_char c = ch->attr; |
1306 | |
1307 | c.unicode = 0x0020; |
1308 | ch->line[ch->col - 1] = c; |
1309 | } |
1310 | } |
1311 | } |
1312 | |
1313 | /** |
1314 | * @internal |
1315 | * @param vbi Initialized vbi decoding context. |
1316 | * @param line ITU-R line number this data originated from. |
1317 | * @param buf Two bytes. |
1318 | * |
1319 | * Decode two bytes of Closed Caption data (Caption, XDS, ITV), |
1320 | * updating the decoder state accordingly. May send events. |
1321 | */ |
1322 | void |
1323 | vbi_decode_caption(vbi_decoder *vbi, int line, uint8_t *buf) |
1324 | { |
1325 | XDS_SEP_DEBUG("vbi_decode_caption\n"); |
1326 | struct caption *cc = &vbi->cc; |
1327 | char c1 = buf[0] & 0x7F; |
1328 | int field2 = 1, i; |
1329 | |
1330 | pthread_mutex_lock(&cc->mutex); |
1331 | AM_DEBUG(1, "vbi_data: line: %d %x %x", line, buf[0], buf[1]); |
1332 | switch (line) { |
1333 | case 21: /* NTSC */ |
1334 | case 22: /* PAL */ |
1335 | field2 = 0; |
1336 | break; |
1337 | |
1338 | case 335: /* PAL, hardly XDS */ |
1339 | break; |
1340 | |
1341 | case 284: /* NTSC */ |
1342 | CC_DUMP( |
1343 | putchar(_vbi_to_ascii (buf[0])); |
1344 | putchar(_vbi_to_ascii (buf[1])); |
1345 | fflush(stdout); |
1346 | ) |
1347 | |
1348 | if (vbi_unpar8 (buf[0]) >= 0) { //vbi_unpar8 (buf[0]) >= 0 |
1349 | if (c1 == 0) { |
1350 | goto finish; |
1351 | } else if (c1 <= 0x0F) { |
1352 | xds_separator(vbi, buf); |
1353 | cc->xds = (c1 != XDS_END); |
1354 | goto finish; |
1355 | } else if (c1 <= 0x1F) { |
1356 | cc->xds = FALSE; |
1357 | } else if (cc->xds) { |
1358 | xds_separator(vbi, buf); |
1359 | goto finish; |
1360 | } |
1361 | } else if (cc->xds) { |
1362 | xds_separator(vbi, buf); |
1363 | goto finish; |
1364 | } |
1365 | |
1366 | break; |
1367 | |
1368 | default: |
1369 | goto finish; |
1370 | } |
1371 | |
1372 | //if ( (buf[0]) < 0) { //vbi_unpar8 (buf[0]) < 0 |
1373 | if ( vbi_unpar8 (buf[0]) < 0) { // |
1374 | #if 0 |
1375 | c1 = 127; |
1376 | buf[0] = c1; /* traditional 'bad' glyph, ccfont has */ |
1377 | buf[1] = c1; /* room, design a special glyph? */ |
1378 | #else |
1379 | goto finish; |
1380 | #endif |
1381 | } |
1382 | |
1383 | CC_DUMP( |
1384 | putchar(_vbi_to_ascii (buf[0])); |
1385 | putchar(_vbi_to_ascii (buf[1])); |
1386 | fflush(stdout); |
1387 | ) |
1388 | |
1389 | switch (c1) { |
1390 | cc_channel *ch; |
1391 | vbi_char c; |
1392 | |
1393 | case 0x01 ... 0x0F: |
1394 | if (!field2) |
1395 | cc->last[0] = 0; |
1396 | break; /* XDS field 1?? */ |
1397 | |
1398 | case 0x10 ... 0x1F: |
1399 | //if ( (buf[1]) >= 0) { // vbi_unpar8 (buf[1]) |
1400 | if ( vbi_unpar8 (buf[1]) >= 0) { |
1401 | if (!field2 |
1402 | && buf[0] == cc->last[0] |
1403 | && buf[1] == cc->last[1]) { |
1404 | /* cmd repetition F1: already executed */ |
1405 | cc->last[0] = 0; /* one rep */ |
1406 | break; |
1407 | } |
1408 | |
1409 | caption_command(vbi, cc, c1, buf[1] & 0x7F, field2); |
1410 | |
1411 | if (!field2) { |
1412 | cc->last[0] = buf[0]; |
1413 | cc->last[1] = buf[1]; |
1414 | } |
1415 | } else if (!field2) |
1416 | cc->last[0] = 0; |
1417 | |
1418 | break; |
1419 | |
1420 | default: |
1421 | CC_TEXT_DUMP( |
1422 | putchar(_vbi_to_ascii (buf[0])); |
1423 | putchar(_vbi_to_ascii (buf[1])); |
1424 | fflush(stdout); |
1425 | ) |
1426 | #if 0 |
1427 | AM_DEBUG(1, "text value: %c %c %x %x", _vbi_to_ascii(buf[0]), |
1428 | _vbi_to_ascii(buf[1]), buf[0], buf[1]); |
1429 | #endif |
1430 | |
1431 | ch = &cc->channel[(cc->curr_chan & 5) + field2 * 2]; |
1432 | |
1433 | if (buf[0] == 0x80 && buf[1] == 0x80) { |
1434 | if (ch->mode) { |
1435 | if (ch->nul_ct == 2) |
1436 | word_break(cc, ch, 1); |
1437 | ch->nul_ct += 2; |
1438 | } |
1439 | |
1440 | break; |
1441 | } |
1442 | |
1443 | if (!field2) |
1444 | cc->last[0] = 0; |
1445 | |
1446 | ch->nul_ct = 0; |
1447 | |
1448 | if (!ch->mode) |
1449 | break; |
1450 | |
1451 | ch->time = vbi->time; /* activity measure */ |
1452 | |
1453 | c = ch->attr; |
1454 | |
1455 | for (i = 0; i < 2; i++) { |
1456 | //char ci = (buf[i]) & 0x7F; /* 127 if bad */ //vbi_unpar8 (buf[i]) |
1457 | char ci = vbi_unpar8 (buf[i]) & 0x7F; /* 127 if bad */ |
1458 | if (ci <= 0x1F) /* 0x00 no char, 0x01 ... 0x1F invalid */ |
1459 | continue; |
1460 | |
1461 | if (ch == cc->channel + 5) // 'T2' |
1462 | itv_separator(vbi, cc, ci); |
1463 | |
1464 | c.unicode = vbi_caption_unicode(ci, /* to_upper */ FALSE); |
1465 | |
1466 | put_char(cc, ch, c); |
1467 | } |
1468 | } |
1469 | for (i=0; i<8; i++) |
1470 | { |
1471 | if (cc->channel[i].update == 1) |
1472 | update(&cc->channel[i]); |
1473 | cc->channel[i].update = 0; |
1474 | } |
1475 | |
1476 | finish: |
1477 | pthread_mutex_unlock(&cc->mutex); |
1478 | } |
1479 | |
1480 | /** |
1481 | * @internal |
1482 | * @param vbi Initialized vbi decoding context. |
1483 | * |
1484 | * This function must be called after desynchronisation |
1485 | * has been detected (i. e. vbi data has been lost) |
1486 | * to reset the Closed Caption decoder. |
1487 | */ |
1488 | void |
1489 | vbi_caption_desync(vbi_decoder *vbi) |
1490 | { |
1491 | struct caption *cc = &vbi->cc; |
1492 | |
1493 | /* cc->curr_chan = 8; *//* garbage */ |
1494 | |
1495 | /* cc->xds = FALSE; */ |
1496 | |
1497 | if (cc->curr_sp) { |
1498 | memset(cc->curr_sp, 0, sizeof(*(cc->curr_sp))); |
1499 | cc->curr_sp = NULL; |
1500 | } |
1501 | |
1502 | cc->itv_count = 0; |
1503 | } |
1504 | |
1505 | /** |
1506 | * @internal |
1507 | * @param vbi Initialized vbi decoding context. |
1508 | * |
1509 | * This function must be called after a channel switch, |
1510 | * to reset the Closed Caption decoder. |
1511 | */ |
1512 | void |
1513 | vbi_caption_channel_switched(vbi_decoder *vbi) |
1514 | { |
1515 | struct caption *cc = &vbi->cc; |
1516 | cc_channel *ch; |
1517 | int i; |
1518 | |
1519 | for (i = 0; i < 9; i++) { |
1520 | ch = &cc->channel[i]; |
1521 | |
1522 | if (i < 4) { |
1523 | ch->mode = MODE_NONE; // MODE_ROLL_UP; |
1524 | ch->row = ROWS - 1; |
1525 | ch->row1 = ROWS - 3; |
1526 | ch->roll = 3; |
1527 | } else { |
1528 | ch->mode = MODE_TEXT; |
1529 | ch->row1 = ch->row = 0; |
1530 | ch->roll = ROWS; |
1531 | } |
1532 | |
1533 | ch->attr.opacity = VBI_OPAQUE; |
1534 | ch->attr.foreground = VBI_WHITE; |
1535 | ch->attr.background = VBI_BLACK; |
1536 | |
1537 | set_cursor(ch, 1, ch->row); |
1538 | |
1539 | ch->time = 0.0; |
1540 | |
1541 | ch->hidden = 0; |
1542 | |
1543 | ch->pg[0].dirty.y0 = 0; |
1544 | ch->pg[0].dirty.y1 = ROWS - 1; |
1545 | ch->pg[0].dirty.roll = 0; |
1546 | |
1547 | erase_memory(cc, ch, 0); |
1548 | |
1549 | memcpy(&ch->pg[1], &ch->pg[0], sizeof(ch->pg[1])); |
1550 | } |
1551 | |
1552 | cc->xds = FALSE; |
1553 | |
1554 | memset(&cc->sub_packet, 0, sizeof(cc->sub_packet)); |
1555 | |
1556 | cc->info_cycle[0] = 0; |
1557 | cc->info_cycle[1] = 0; |
1558 | |
1559 | vbi_caption_desync(vbi); |
1560 | } |
1561 | |
1562 | static vbi_rgba |
1563 | default_color_map[8] = { |
1564 | VBI_RGBA(0x00, 0x00, 0x00), VBI_RGBA(0xFF, 0x00, 0x00), |
1565 | VBI_RGBA(0x00, 0xFF, 0x00), VBI_RGBA(0xFF, 0xFF, 0x00), |
1566 | VBI_RGBA(0x00, 0x00, 0xFF), VBI_RGBA(0xFF, 0x00, 0xFF), |
1567 | VBI_RGBA(0x00, 0xFF, 0xFF), VBI_RGBA(0xFF, 0xFF, 0xFF) |
1568 | }; |
1569 | |
1570 | /** |
1571 | * @internal |
1572 | * @param vbi Initialized vbi decoding context. |
1573 | * |
1574 | * After the client changed text brightness and saturation |
1575 | * this function adjusts the Closed Caption color palette. |
1576 | */ |
1577 | void |
1578 | vbi_caption_color_level(vbi_decoder *vbi) |
1579 | { |
1580 | int i; |
1581 | |
1582 | vbi_transp_colormap(vbi, vbi->cc.channel[0].pg[0].color_map, |
1583 | default_color_map, 8); |
1584 | |
1585 | for (i = 1; i < 16; i++) |
1586 | memcpy(vbi->cc.channel[i >> 1].pg[i & 1].color_map, |
1587 | vbi->cc.channel[0].pg[0].color_map, |
1588 | sizeof(default_color_map)); |
1589 | } |
1590 | |
1591 | /** |
1592 | * @internal |
1593 | * @param vbi VBI decoding context. |
1594 | * |
1595 | * This function is called during @a vbi destruction |
1596 | * to destroy Closed Caption subset of @a vbi. |
1597 | */ |
1598 | void |
1599 | vbi_caption_destroy(vbi_decoder *vbi) |
1600 | { |
1601 | pthread_mutex_destroy(&vbi->cc.mutex); |
1602 | } |
1603 | |
1604 | /** |
1605 | * @internal |
1606 | * @param vbi VBI decoding context. |
1607 | * |
1608 | * This function is called during @a vbi initialization |
1609 | * to initialize the Closed Caption subset of @a vbi. |
1610 | */ |
1611 | void |
1612 | vbi_caption_init(vbi_decoder *vbi) |
1613 | { |
1614 | struct caption *cc = &vbi->cc; |
1615 | cc_channel *ch; |
1616 | int i; |
1617 | |
1618 | memset(cc, 0, sizeof(struct caption)); |
1619 | |
1620 | pthread_mutex_init(&cc->mutex, NULL); |
1621 | |
1622 | for (i = 0; i < 9; i++) { |
1623 | ch = &cc->channel[i]; |
1624 | |
1625 | ch->pg[0].vbi = vbi; |
1626 | |
1627 | ch->pg[0].pgno = i + 1; |
1628 | ch->pg[0].subno = 0; |
1629 | |
1630 | ch->pg[0].rows = ROWS; |
1631 | ch->pg[0].columns = COLUMNS; |
1632 | |
1633 | ch->pg[0].screen_color = 0; |
1634 | ch->pg[0].screen_opacity = (i < 4) ? VBI_TRANSPARENT_SPACE : VBI_OPAQUE; |
1635 | |
1636 | ch->pg[0].font[0] = vbi_font_descriptors; /* English */ |
1637 | ch->pg[0].font[1] = vbi_font_descriptors; |
1638 | |
1639 | memcpy(&ch->pg[1], &ch->pg[0], sizeof(ch->pg[1])); |
1640 | } |
1641 | |
1642 | for (i = 0; i < 2; i++) { |
1643 | cc->transp_space[i].foreground = VBI_WHITE; |
1644 | cc->transp_space[i].background = VBI_BLACK; |
1645 | cc->transp_space[i].unicode = 0x0020; |
1646 | } |
1647 | |
1648 | cc->transp_space[0].opacity = VBI_TRANSPARENT_SPACE; |
1649 | cc->transp_space[1].opacity = VBI_OPAQUE; |
1650 | |
1651 | vbi_caption_channel_switched(vbi); |
1652 | |
1653 | vbi_caption_color_level(vbi); |
1654 | } |
1655 | |
1656 | /** |
1657 | * @param vbi Initialized vbi decoding context. |
1658 | * @param pg Place to store the formatted page. |
1659 | * @param pgno Page number 1 ... 8 of the page to fetch, see vbi_pgno. |
1660 | * @param reset @c TRUE resets the vbi_page dirty fields in cache after |
1661 | * fetching. Pass @c FALSE only if you plan to call this function again |
1662 | * to update other displays. |
1663 | * |
1664 | * Fetches a Closed Caption page designated by @a pgno from the cache, |
1665 | * formats and stores it in @a pg. CC pages are transmitted basically in |
1666 | * two modes: at once and character by character ("roll-up" mode). |
1667 | * Either way you get a snapshot of the page as it should appear on |
1668 | * screen at present. With vbi_event_handler_add() you can request a |
1669 | * @c VBI_EVENT_CAPTION event to be notified about pending changes |
1670 | * (in case of "roll-up" mode that is with each new word received) |
1671 | * and the vbi_page->dirty fields will mark the lines actually in |
1672 | * need of updates, to speed up rendering. |
1673 | * |
1674 | * Although safe to do, this function is not supposed to be |
1675 | * called from an event handler, since rendering may block decoding |
1676 | * for extended periods of time. |
1677 | * |
1678 | * @return |
1679 | * @c FALSE if some error occured. |
1680 | */ |
1681 | vbi_bool |
1682 | vbi_fetch_cc_page(vbi_decoder *vbi, vbi_page *pg, vbi_pgno pgno, vbi_bool reset) |
1683 | { |
1684 | cc_channel *ch = vbi->cc.channel + ((pgno - 1) & 7); |
1685 | vbi_page *spg; |
1686 | |
1687 | reset = reset; |
1688 | |
1689 | if (pgno < 1 || pgno > 8) |
1690 | return FALSE; |
1691 | |
1692 | pthread_mutex_lock(&vbi->cc.mutex); |
1693 | |
1694 | spg = ch->pg + (ch->hidden ^ 1); |
1695 | |
1696 | memcpy(pg, spg, sizeof(*pg)); /* shortcut? */ |
1697 | |
1698 | spg->dirty.y0 = ROWS; |
1699 | spg->dirty.y1 = -1; |
1700 | spg->dirty.roll = 0; |
1701 | |
1702 | pthread_mutex_unlock(&vbi->cc.mutex); |
1703 | |
1704 | return 1; |
1705 | } |
1706 | |
1707 | /* |
1708 | Local variables: |
1709 | c-set-style: K&R |
1710 | c-basic-offset: 8 |
1711 | End: |
1712 | */ |
1713 |