summaryrefslogtreecommitdiff
path: root/src/caption.c (plain)
blob: 9c823de5217894aa5a81c56a973571dbe22cf795
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
53static inline void
54caption_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 */
79static const char *
80language[8] = {
81 "Unknown",
82 "English",
83 "Español",
84 "Français",
85 "Deutsch",
86 "Italiano",
87 "Other",
88 "None"
89};
90
91static uint32_t hcrc[128];
92
93static void init_hcrc(void) __attribute__ ((constructor));
94
95
96/*
97http://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 */
102static void
103init_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
117static int
118xds_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
138static void
139flush_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
153static inline void
154xds_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
614static void
615xds_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
718static void
719itv_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
750static void
751render(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
772static void
773clear(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
787static void
788roll_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
809static inline void
810update(cc_channel *ch)
811{
812#if 1
813int 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[ch->hidden^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
828static void
829word_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
870static inline void
871set_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
879static void
880put_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 if (ch->mode == MODE_POP_ON)
893 return;
894 ch->update = 1;
895 //if ((c.unicode & 0x7F) == 0x20)
896 // word_break(cc, ch, 1);
897 //AM_DEBUG(1, "add render in putchar");
898}
899
900static inline cc_channel *
901switch_channel(struct caption *cc, cc_channel *ch, int new_chan)
902{
903 word_break(cc, ch, 1); // we leave for a number of frames
904
905 return &cc->channel[cc->curr_chan = new_chan];
906}
907
908static void
909erase_memory(struct caption *cc, cc_channel *ch, int page)
910{
911 vbi_page *pg = ch->pg + page;
912 vbi_char *acp = pg->text;
913 memset(pg->text, 0, sizeof(pg->text));
914#if 0
915 //vbi_char c = cc->transp_space[ch >= &cc->channel[4]];
916 vbi_char c = {0};
917 int i,j;
918 for (i = 0; i < COLUMNS * ROWS; acp++, i++)
919 *acp = c;
920
921 pg->dirty.y0 = 0;
922 pg->dirty.y1 = ROWS - 1;
923 pg->dirty.roll = ROWS;
924#endif
925}
926
927static const vbi_color
928palette_mapping[8] = {
929 VBI_WHITE, VBI_GREEN, VBI_BLUE, VBI_CYAN,
930 VBI_RED, VBI_YELLOW, VBI_MAGENTA, VBI_BLACK
931};
932
933static int
934row_mapping[] = {
935 10, -1, 0, 1, 2, 3, 11, 12, 13, 14, 4, 5, 6, 7, 8, 9
936};
937
938// not verified means I didn't encounter the code in a
939// sample stream yet
940
941_vbi_inline void
942caption_command(vbi_decoder *vbi, struct caption *cc,
943 unsigned char c1, unsigned char c2, vbi_bool field2)
944{
945 XDS_SEP_DEBUG("caption_command\n");
946 cc_channel *ch;
947 int chan, col, i;
948 int last_row;
949 chan = (cc->curr_chan & 4) + field2 * 2 + ((c1 >> 3) & 1);
950 ch = &cc->channel[chan];
951
952 c1 &= 7;
953
954 if (c2 >= 0x40) { /* Preamble Address Codes 001 crrr 1ri xxxu */
955 int row = row_mapping[(c1 << 1) + ((c2 >> 5) & 1)];
956 int old_row = ch->row;
957
958 if (row < 0 || !ch->mode)
959 return;
960
961 ch->attr.underline = c2 & 1;
962 ch->attr.background = VBI_BLACK;
963 ch->attr.opacity = VBI_OPAQUE;
964 ch->attr.flash = FALSE;
965
966 word_break(cc, ch, 1);
967
968 if (ch->mode == MODE_ROLL_UP) {
969 int row1 = row - ch->roll + 1;
970
971 if (row1 < 0)
972 row1 = 0;
973
974 if (row1 != ch->row1) {
975 ch->row1 = row1;
976 erase_memory(cc, ch, ch->hidden);
977 erase_memory(cc, ch, ch->hidden ^ 1);
978 }
979
980 set_cursor(ch, 1, ch->row1 + ch->roll - 1);
981 } else
982 set_cursor(ch, 1, row);
983
984 if (c2 & 0x10) {
985 /*
986 col = ch->col;
987
988 for (i = (c2 & 14) * 2; i > 0 && col < COLUMNS - 1; i--)
989 ch->line[col++] = cc->transp_space[chan >> 2];
990
991 if (col > ch->col)
992 ch->col = ch->col1 = col;
993 */
994
995 ch->col = (c2 & 14) * 2 + 1;
996
997 if (old_row != ch->row) {
998 ch->attr.italic = FALSE;
999 ch->attr.foreground = VBI_WHITE;
1000 }
1001 } else {
1002// not verified
1003 c2 = (c2 >> 1) & 7;
1004
1005 if (c2 < 7) {
1006 ch->attr.italic = FALSE;
1007 ch->attr.foreground = palette_mapping[c2];
1008 } else {
1009 ch->attr.italic = TRUE;
1010 //ch->attr.foreground = VBI_WHITE;
1011 }
1012 }
1013
1014 return;
1015 }
1016
1017 switch (c1) {
1018 case 0: /* Optional Attributes 001 c000 010 xxxt */
1019// not verified
1020 ch->attr.opacity = (c2 & 1) ? VBI_SEMI_TRANSPARENT : VBI_OPAQUE;
1021 ch->attr.background = palette_mapping[(c2 >> 1) & 7];
1022 return;
1023
1024 case 1:
1025 if (c2 & 0x10) { /* Special Characters 001 c001 011 xxxx */
1026// not verified
1027 c2 &= 15;
1028
1029 if (c2 == 9) { // "transparent space"
1030 if (ch->col < COLUMNS - 1) {
1031 ch->line[ch->col++] = cc->transp_space[chan >> 2];
1032 ch->col1 = ch->col;
1033 } else
1034 ch->line[COLUMNS - 2] = cc->transp_space[chan >> 2];
1035 // XXX boxed logic?
1036 } else {
1037 vbi_char c = ch->attr;
1038
1039 c.unicode = vbi_caption_unicode (0x1130 | (c2 & 15),
1040 /* to_upper */ FALSE);
1041 XDS_SEP_DEBUG("vbi_caption_unicode %c\n",c.unicode);
1042 put_char(cc, ch, c);
1043 }
1044 } else { /* Midrow Codes 001 c001 010 xxxu */
1045// not verified
1046 ch->attr.flash = FALSE;
1047 ch->attr.underline = c2 & 1;
1048
1049 c2 = (c2 >> 1) & 7;
1050
1051 if (c2 < 7) {
1052 ch->attr.italic = FALSE;
1053 ch->attr.foreground = palette_mapping[c2];
1054 } else {
1055 ch->attr.italic = TRUE;
1056 //ch->attr.foreground = VBI_WHITE;
1057 }
1058 }
1059
1060 return;
1061
1062 case 2: /* Optional Extended Characters 001 c01f 01x xxxx */
1063 case 3:
1064 /* Send specs to the maintainer of this code */
1065 {
1066 vbi_char c;
1067
1068 c = ch->attr;
1069 c.unicode = vbi_caption_unicode((c1 << 8) | c2 | 0x1000, 0);
1070
1071 if (c.unicode) {
1072 if (ch->col > 1)
1073 ch->col --;
1074
1075 put_char(cc, ch, c);
1076 }
1077 }
1078 return;
1079
1080 case 4: /* Misc Control Codes 001 c10f 010 xxxx */
1081 case 5: /* Misc Control Codes 001 c10f 010 xxxx */
1082 /* f ("field"): purpose? */
1083
1084 switch (c2 & 15) {
1085 case 0: /* Resume Caption Loading 001 c10f 010 0000 */
1086 ch = switch_channel(cc, ch, chan & 3);
1087
1088 ch->mode = MODE_POP_ON;
1089
1090// no? erase_memory(cc, ch);
1091
1092 return;
1093
1094 /* case 4: reserved */
1095
1096 case 5: /* Roll-Up Captions 001 c10f 010 0xxx */
1097 case 6:
1098 case 7:
1099 {
1100 int roll = (c2 & 7) - 3;
1101
1102 ch = switch_channel(cc, ch, chan & 3);
1103
1104 if (ch->mode == MODE_ROLL_UP && ch->roll == roll)
1105 return;
1106
1107 erase_memory(cc, ch, ch->hidden);
1108 erase_memory(cc, ch, ch->hidden ^ 1);
1109
1110 ch->mode = MODE_ROLL_UP;
1111 ch->roll = roll;
1112
1113 set_cursor(ch, 1, 14);
1114
1115 ch->row1 = 14 - roll + 1;
1116
1117 return;
1118 }
1119
1120 case 9: /* Resume Direct Captioning 001 c10f 010 1001 */
1121// not verified
1122 ch = switch_channel(cc, ch, chan & 3);
1123 ch->mode = MODE_PAINT_ON;
1124 return;
1125
1126 case 10: /* Text Restart 001 c10f 010 1010 */
1127// not verified
1128 erase_memory(cc, ch, ch->hidden);
1129 erase_memory(cc, ch, ch->hidden ^ 1);
1130 ch = switch_channel(cc, ch, chan | 4);
1131 set_cursor(ch, 1, 0);
1132 erase_memory(cc, ch, ch->hidden);
1133 erase_memory(cc, ch, ch->hidden ^ 1);
1134 return;
1135
1136 case 11: /* Resume Text Display 001 c10f 010 1011 */
1137 ch = switch_channel(cc, ch, chan | 4);
1138 return;
1139
1140 case 15: /* End Of Caption 001 c10f 010 1111 */
1141 ch = switch_channel(cc, ch, chan & 3);
1142 ch->mode = MODE_POP_ON;
1143
1144 word_break(cc, ch, 1);
1145
1146 ch->hidden ^= 1;
1147
1148 render(ch->pg + (ch->hidden ^ 1), -1 /* ! */);
1149
1150 erase_memory(cc, ch, ch->hidden); // yes?
1151
1152 /*
1153 * A Preamble Address Code should follow,
1154 * reset to a known state to be safe.
1155 * Reset ch->line for new ch->hidden.
1156 * XXX row 0?
1157 */
1158 set_cursor(ch, 1, ROWS - 1);
1159
1160 return;
1161
1162 case 8: /* Flash On 001 c10f 010 1000 */
1163// not verified
1164 ch->attr.flash = TRUE;
1165 return;
1166
1167 case 1: /* Backspace 001 c10f 010 0001 */
1168// not verified
1169 if (ch->mode) {
1170 if (ch->col > 1) {
1171 if (ch->line[ch->col - 1].unicode == 0)
1172 return;
1173 ch->col --;
1174 } else if (ch->row > 0) {
1175 vbi_char *acp;
1176
1177 ch->row --;
1178 ch->line = ch->pg[ch->hidden].text + ch->row * COLUMNS;
1179 ch->col1 = 1;
1180 ch->col = COLUMNS - 1;
1181 acp = ch->line + COLUMNS - 1;
1182
1183 while (ch->col > 1) {
1184 if (acp->unicode != 0)
1185 break;
1186
1187 acp --;
1188 ch->col --;
1189 }
1190 } else {
1191 return;
1192 }
1193
1194 memset(&ch->line[ch->col], 0, sizeof(vbi_char));
1195 if (ch->col < ch->col1)
1196 ch->col1 = ch->col;
1197 ch->update = 1;
1198 }
1199
1200 return;
1201
1202 case 13: /* Carriage Return 001 c10f 010 1101 */
1203 if (ch == cc->channel + 5)
1204 itv_separator(vbi, cc, 0);
1205
1206 if (!ch->mode)
1207 return;
1208
1209 last_row = ch->row1 + ch->roll - 1;
1210
1211 if (last_row > ROWS - 1)
1212 last_row = ROWS - 1;
1213
1214 if (ch->row < last_row) {
1215 word_break(cc, ch, 1);
1216 set_cursor(ch, 1, ch->row + 1);
1217 } else {
1218 vbi_char *acp = &ch->pg[ch->hidden ^ (ch->mode != MODE_POP_ON)]
1219 .text[ch->row1 * COLUMNS];
1220
1221 word_break(cc, ch, 1);
1222 ch->update = 1;
1223
1224 /*if (ch->mode == MODE_ROLL_UP) */
1225 {
1226 memmove(acp, acp + COLUMNS, sizeof(*acp) * (ch->roll - 1) * COLUMNS);
1227 for (i = 0; i <= COLUMNS; i++) {
1228 memset(&ch->line[i], 0, sizeof(vbi_char));
1229 }
1230 }
1231
1232 if (ch->mode != MODE_POP_ON) {
1233 ch->update = 1;
1234 roll_up(ch->pg + (ch->hidden ^ 1), ch->row1, last_row);
1235 }
1236
1237 ch->col1 = ch->col = 1;
1238 }
1239 ch->attr.underline = FALSE;
1240 ch->attr.background = VBI_BLACK;
1241 ch->attr.opacity = VBI_OPAQUE;
1242 ch->attr.flash = FALSE;
1243 ch->attr.italic = FALSE;
1244 ch->attr.foreground = VBI_WHITE;
1245
1246 return;
1247
1248 case 4: /* Delete To End Of Row 001 c10f 010 0100 */
1249// not verified
1250 if (!ch->mode)
1251 return;
1252
1253 for (i = ch->col; i <= COLUMNS - 1; i++) {
1254 memset(&ch->line[i], 0, sizeof(vbi_char));
1255 }
1256
1257 word_break(cc, ch, 0);
1258
1259 if (ch->mode != MODE_POP_ON) {
1260 ch->update = 1;
1261 render(ch->pg + (ch->hidden ^ 1), ch->row);
1262 }
1263
1264 ch->update = 1;
1265 return;
1266
1267 case 12: /* Erase Displayed Memory 001 c10f 010 1100 */
1268// s1, s4: EDM always before EOC
1269 /* Received in text mode */
1270 /*
1271 if (cc->curr_chan >= 4)
1272 {
1273 for (i = 0; i < 2; i++ )
1274 {
1275 ch = &cc->channel[i + (cc->curr_chan - 4)/2];
1276 erase_memory(cc, ch, 0);
1277 erase_memory(cc, ch, 1);
1278 }
1279 }
1280 else
1281 {
1282
1283 if (ch->mode != MODE_POP_ON)
1284 erase_memory(cc, ch, ch->hidden);
1285
1286 erase_memory(cc, ch, ch->hidden ^ 1);
1287
1288
1289 clear(ch->pg + (ch->hidden ^ 1));
1290 }*/
1291
1292 if (cc->curr_chan < 4) {
1293 //erase_memory(cc, ch, ch->hidden);
1294 erase_memory(cc, ch, ch->hidden ^ 1);
1295 clear(ch->pg + (ch->hidden ^ 1));
1296
1297 ch->update = 1;
1298 }
1299 return;
1300 case 14: /* Erase Non-Displayed Memory 001 c10f 010 1110 */
1301// not verified
1302 /*if (ch->mode == MODE_POP_ON)*/
1303 erase_memory(cc, ch, ch->hidden);
1304
1305 //erase_memory(cc, ch, ch->hidden ^ 1);
1306 //ch->update = 1;
1307 return;
1308 }
1309
1310 return;
1311
1312 /* case 6: reserved */
1313
1314 case 7:
1315 if (!ch->mode)
1316 return;
1317
1318 switch (c2) {
1319 case 0x21 ... 0x23: /* Misc Control Codes, Tabs 001 c111 010 00xx */
1320// not verified
1321 /*
1322 col = ch->col;
1323
1324 for (i = c2 & 3; i > 0 && col < COLUMNS - 1; i--)
1325 ch->line[col++] = cc->transp_space[chan >> 2];
1326
1327 if (col > ch->col)
1328 ch->col = ch->col1 = col;*/
1329
1330 ch->col += (c2 & 3);
1331 if (ch->col >= COLUMNS)
1332 ch->col = COLUMNS - 1;
1333
1334 return;
1335
1336 case 0x2D: /* Optional Attributes 001 c111 010 11xx */
1337// not verified
1338 ch->attr.opacity = VBI_TRANSPARENT_FULL;
1339 break;
1340
1341 case 0x2E: /* Optional Attributes 001 c111 010 11xx */
1342 case 0x2F:
1343// not verified
1344 ch->attr.foreground = VBI_BLACK;
1345 ch->attr.underline = c2 & 1;
1346 break;
1347
1348 default:
1349 return;
1350 }
1351
1352 /* Optional Attributes, backspace magic */
1353
1354 if (ch->col > 1 && (ch->line[ch->col - 1].unicode & 0x7F) == 0x20) {
1355 vbi_char c = ch->attr;
1356
1357 c.unicode = 0x0020;
1358 ch->line[ch->col - 1] = c;
1359 }
1360 }
1361}
1362
1363/**
1364 * @internal
1365 * @param vbi Initialized vbi decoding context.
1366 * @param line ITU-R line number this data originated from.
1367 * @param buf Two bytes.
1368 *
1369 * Decode two bytes of Closed Caption data (Caption, XDS, ITV),
1370 * updating the decoder state accordingly. May send events.
1371 */
1372void
1373vbi_decode_caption(vbi_decoder *vbi, int line, uint8_t *buf)
1374{
1375 XDS_SEP_DEBUG("vbi_decode_caption\n");
1376 struct caption *cc = &vbi->cc;
1377 char c1 = buf[0] & 0x7F;
1378 int field2 = 1, i;
1379
1380 pthread_mutex_lock(&cc->mutex);
1381 //AM_DEBUG(1, "vbi_data: line: %d %x %x", line, buf[0], buf[1]);
1382
1383 if (line == 21) {
1384 cc->curr_chan = cc->curr_chan_f1;
1385 memcpy(cc->last, cc->last_f1, sizeof(cc->last));
1386 } else {
1387 cc->curr_chan = cc->curr_chan_f2;
1388 memcpy(cc->last, cc->last_f2, sizeof(cc->last));
1389 }
1390
1391 switch (line) {
1392 case 21: /* NTSC */
1393 case 22: /* PAL */
1394 field2 = 0;
1395 break;
1396
1397 case 335: /* PAL, hardly XDS */
1398 break;
1399
1400 case 284: /* NTSC */
1401 CC_DUMP(
1402 putchar(_vbi_to_ascii (buf[0]));
1403 putchar(_vbi_to_ascii (buf[1]));
1404 fflush(stdout);
1405 )
1406
1407 if (vbi_unpar8 (buf[0]) >= 0) { //vbi_unpar8 (buf[0]) >= 0
1408 if (c1 == 0) {
1409 goto finish;
1410 } else if (c1 <= 0x0F) {
1411 xds_separator(vbi, buf);
1412 cc->xds = (c1 != XDS_END);
1413 goto finish;
1414 } else if (c1 <= 0x1F) {
1415 cc->xds = FALSE;
1416 } else if (cc->xds) {
1417 xds_separator(vbi, buf);
1418 goto finish;
1419 }
1420 } else if (cc->xds) {
1421 xds_separator(vbi, buf);
1422 goto finish;
1423 }
1424
1425 break;
1426
1427 default:
1428 goto finish;
1429 }
1430
1431 //if ( (buf[0]) < 0) { //vbi_unpar8 (buf[0]) < 0
1432 if ( vbi_unpar8 (buf[0]) < 0) { //
1433#if 0
1434 c1 = 127;
1435 buf[0] = c1; /* traditional 'bad' glyph, ccfont has */
1436 buf[1] = c1; /* room, design a special glyph? */
1437#else
1438 goto finish;
1439#endif
1440 }
1441
1442 CC_DUMP(
1443 putchar(_vbi_to_ascii (buf[0]));
1444 putchar(_vbi_to_ascii (buf[1]));
1445 fflush(stdout);
1446 )
1447
1448 switch (c1) {
1449 cc_channel *ch;
1450 vbi_char c;
1451
1452 case 0x01 ... 0x0F:
1453 /*if (!field2)*/
1454 cc->last[0] = 0;
1455 break; /* XDS field 1?? */
1456
1457 case 0x10 ... 0x1F:
1458 //if ( (buf[1]) >= 0) { // vbi_unpar8 (buf[1])
1459 if ( vbi_unpar8 (buf[1]) >= 0) {
1460 if (/*!field2
1461 &&*/ buf[0] == cc->last[0]
1462 && buf[1] == cc->last[1]) {
1463 /* cmd repetition F1: already executed */
1464 cc->last[0] = 0; /* one rep */
1465 break;
1466 }
1467
1468 caption_command(vbi, cc, c1, buf[1] & 0x7F, field2);
1469
1470 /*if (!field2)*/ {
1471 cc->last[0] = buf[0];
1472 cc->last[1] = buf[1];
1473 }
1474 } else /*if (!field2)*/
1475 cc->last[0] = 0;
1476
1477 break;
1478
1479 default:
1480 CC_TEXT_DUMP(
1481 putchar(_vbi_to_ascii (buf[0]));
1482 putchar(_vbi_to_ascii (buf[1]));
1483 fflush(stdout);
1484 )
1485#if 0
1486 AM_DEBUG(1, "text value: %c %c %x %x", _vbi_to_ascii(buf[0]),
1487 _vbi_to_ascii(buf[1]), buf[0], buf[1]);
1488#endif
1489
1490 ch = &cc->channel[(cc->curr_chan & 5) + field2 * 2];
1491
1492 if (buf[0] == 0x80 && buf[1] == 0x80) {
1493 if (ch->mode) {
1494 if (ch->nul_ct == 2)
1495 word_break(cc, ch, 1);
1496 ch->nul_ct += 2;
1497 }
1498
1499 break;
1500 }
1501
1502 /*if (!field2)*/
1503 cc->last[0] = 0;
1504
1505 ch->nul_ct = 0;
1506
1507 if (!ch->mode)
1508 break;
1509
1510 ch->time = vbi->time; /* activity measure */
1511
1512 c = ch->attr;
1513
1514 for (i = 0; i < 2; i++) {
1515 //char ci = (buf[i]) & 0x7F; /* 127 if bad */ //vbi_unpar8 (buf[i])
1516 char ci = vbi_unpar8 (buf[i]) & 0x7F; /* 127 if bad */
1517 if (ci <= 0x1F) /* 0x00 no char, 0x01 ... 0x1F invalid */
1518 continue;
1519
1520 if (ch == cc->channel + 5) // 'T2'
1521 itv_separator(vbi, cc, ci);
1522
1523 c.unicode = vbi_caption_unicode(ci, /* to_upper */ FALSE);
1524
1525 put_char(cc, ch, c);
1526 }
1527 }
1528 for (i=0; i<8; i++)
1529 {
1530 if (cc->channel[i].update == 1)
1531 update(&cc->channel[i]);
1532 cc->channel[i].update = 0;
1533 }
1534
1535 if (line == 21) {
1536 cc->curr_chan_f1 = cc->curr_chan;
1537 memcpy(cc->last_f1, cc->last, sizeof(cc->last));
1538 } else {
1539 cc->curr_chan_f2 = cc->curr_chan;
1540 memcpy(cc->last_f2, cc->last, sizeof(cc->last));
1541 }
1542
1543 finish:
1544 pthread_mutex_unlock(&cc->mutex);
1545}
1546
1547/**
1548 * @internal
1549 * @param vbi Initialized vbi decoding context.
1550 *
1551 * This function must be called after desynchronisation
1552 * has been detected (i. e. vbi data has been lost)
1553 * to reset the Closed Caption decoder.
1554 */
1555void
1556vbi_caption_desync(vbi_decoder *vbi)
1557{
1558 struct caption *cc = &vbi->cc;
1559
1560 /* cc->curr_chan = 8; *//* garbage */
1561
1562 /* cc->xds = FALSE; */
1563
1564 if (cc->curr_sp) {
1565 memset(cc->curr_sp, 0, sizeof(*(cc->curr_sp)));
1566 cc->curr_sp = NULL;
1567 }
1568
1569 cc->itv_count = 0;
1570}
1571
1572/**
1573 * @internal
1574 * @param vbi Initialized vbi decoding context.
1575 *
1576 * This function must be called after a channel switch,
1577 * to reset the Closed Caption decoder.
1578 */
1579void
1580vbi_caption_channel_switched(vbi_decoder *vbi)
1581{
1582 struct caption *cc = &vbi->cc;
1583 cc_channel *ch;
1584 int i;
1585
1586 for (i = 0; i < 9; i++) {
1587 ch = &cc->channel[i];
1588
1589 if (i < 4) {
1590 ch->mode = MODE_NONE; // MODE_ROLL_UP;
1591 ch->row = ROWS - 1;
1592 ch->row1 = ROWS - 3;
1593 ch->roll = 3;
1594 } else {
1595 ch->mode = MODE_TEXT;
1596 ch->row1 = ch->row = 0;
1597 ch->roll = ROWS;
1598 }
1599
1600 ch->attr.opacity = VBI_OPAQUE;
1601 ch->attr.foreground = VBI_WHITE;
1602 ch->attr.background = VBI_BLACK;
1603
1604 set_cursor(ch, 1, ch->row);
1605
1606 ch->time = 0.0;
1607
1608 ch->hidden = 0;
1609
1610 ch->pg[0].dirty.y0 = 0;
1611 ch->pg[0].dirty.y1 = ROWS - 1;
1612 ch->pg[0].dirty.roll = 0;
1613
1614 erase_memory(cc, ch, 0);
1615
1616 memcpy(&ch->pg[1], &ch->pg[0], sizeof(ch->pg[1]));
1617 }
1618
1619 cc->xds = FALSE;
1620
1621 memset(&cc->sub_packet, 0, sizeof(cc->sub_packet));
1622
1623 cc->info_cycle[0] = 0;
1624 cc->info_cycle[1] = 0;
1625
1626 vbi_caption_desync(vbi);
1627}
1628
1629static vbi_rgba
1630default_color_map[8] = {
1631 VBI_RGBA(0x00, 0x00, 0x00), VBI_RGBA(0xFF, 0x00, 0x00),
1632 VBI_RGBA(0x00, 0xFF, 0x00), VBI_RGBA(0xFF, 0xFF, 0x00),
1633 VBI_RGBA(0x00, 0x00, 0xFF), VBI_RGBA(0xFF, 0x00, 0xFF),
1634 VBI_RGBA(0x00, 0xFF, 0xFF), VBI_RGBA(0xFF, 0xFF, 0xFF)
1635};
1636
1637/**
1638 * @internal
1639 * @param vbi Initialized vbi decoding context.
1640 *
1641 * After the client changed text brightness and saturation
1642 * this function adjusts the Closed Caption color palette.
1643 */
1644void
1645vbi_caption_color_level(vbi_decoder *vbi)
1646{
1647 int i;
1648
1649 vbi_transp_colormap(vbi, vbi->cc.channel[0].pg[0].color_map,
1650 default_color_map, 8);
1651
1652 for (i = 1; i < 16; i++)
1653 memcpy(vbi->cc.channel[i >> 1].pg[i & 1].color_map,
1654 vbi->cc.channel[0].pg[0].color_map,
1655 sizeof(default_color_map));
1656}
1657
1658/**
1659 * @internal
1660 * @param vbi VBI decoding context.
1661 *
1662 * This function is called during @a vbi destruction
1663 * to destroy Closed Caption subset of @a vbi.
1664 */
1665void
1666vbi_caption_destroy(vbi_decoder *vbi)
1667{
1668 pthread_mutex_destroy(&vbi->cc.mutex);
1669}
1670
1671/**
1672 * @internal
1673 * @param vbi VBI decoding context.
1674 *
1675 * This function is called during @a vbi initialization
1676 * to initialize the Closed Caption subset of @a vbi.
1677 */
1678void
1679vbi_caption_init(vbi_decoder *vbi)
1680{
1681 struct caption *cc = &vbi->cc;
1682 cc_channel *ch;
1683 int i;
1684
1685 memset(cc, 0, sizeof(struct caption));
1686
1687 pthread_mutex_init(&cc->mutex, NULL);
1688
1689 for (i = 0; i < 9; i++) {
1690 ch = &cc->channel[i];
1691
1692 ch->pg[0].vbi = vbi;
1693
1694 ch->pg[0].pgno = i + 1;
1695 ch->pg[0].subno = 0;
1696
1697 ch->pg[0].rows = ROWS;
1698 ch->pg[0].columns = COLUMNS;
1699
1700 ch->pg[0].screen_color = 0;
1701 ch->pg[0].screen_opacity = (i < 4) ? VBI_TRANSPARENT_SPACE : VBI_OPAQUE;
1702
1703 ch->pg[0].font[0] = vbi_font_descriptors; /* English */
1704 ch->pg[0].font[1] = vbi_font_descriptors;
1705
1706 memcpy(&ch->pg[1], &ch->pg[0], sizeof(ch->pg[1]));
1707 }
1708
1709 for (i = 0; i < 2; i++) {
1710 cc->transp_space[i].foreground = VBI_WHITE;
1711 cc->transp_space[i].background = VBI_BLACK;
1712 cc->transp_space[i].unicode = 0x0020;
1713 }
1714
1715 cc->transp_space[0].opacity = VBI_TRANSPARENT_SPACE;
1716 cc->transp_space[1].opacity = VBI_OPAQUE;
1717
1718 vbi_caption_channel_switched(vbi);
1719
1720 vbi_caption_color_level(vbi);
1721}
1722
1723/**
1724 * @param vbi Initialized vbi decoding context.
1725 * @param pg Place to store the formatted page.
1726 * @param pgno Page number 1 ... 8 of the page to fetch, see vbi_pgno.
1727 * @param reset @c TRUE resets the vbi_page dirty fields in cache after
1728 * fetching. Pass @c FALSE only if you plan to call this function again
1729 * to update other displays.
1730 *
1731 * Fetches a Closed Caption page designated by @a pgno from the cache,
1732 * formats and stores it in @a pg. CC pages are transmitted basically in
1733 * two modes: at once and character by character ("roll-up" mode).
1734 * Either way you get a snapshot of the page as it should appear on
1735 * screen at present. With vbi_event_handler_add() you can request a
1736 * @c VBI_EVENT_CAPTION event to be notified about pending changes
1737 * (in case of "roll-up" mode that is with each new word received)
1738 * and the vbi_page->dirty fields will mark the lines actually in
1739 * need of updates, to speed up rendering.
1740 *
1741 * Although safe to do, this function is not supposed to be
1742 * called from an event handler, since rendering may block decoding
1743 * for extended periods of time.
1744 *
1745 * @return
1746 * @c FALSE if some error occured.
1747 */
1748vbi_bool
1749vbi_fetch_cc_page(vbi_decoder *vbi, vbi_page *pg, vbi_pgno pgno, vbi_bool reset)
1750{
1751 cc_channel *ch = vbi->cc.channel + ((pgno - 1) & 7);
1752 vbi_page *spg;
1753
1754 reset = reset;
1755
1756 if (pgno < 1 || pgno > 8)
1757 return FALSE;
1758
1759 pthread_mutex_lock(&vbi->cc.mutex);
1760
1761 spg = ch->pg + (ch->hidden ^ 1);
1762
1763 memcpy(pg, spg, sizeof(*pg)); /* shortcut? */
1764
1765 spg->dirty.y0 = ROWS;
1766 spg->dirty.y1 = -1;
1767 spg->dirty.roll = 0;
1768
1769 pthread_mutex_unlock(&vbi->cc.mutex);
1770
1771 return 1;
1772}
1773
1774void vbi_refresh_cc(vbi_decoder *vbi)
1775{
1776 cc_channel *ch;
1777 vbi_page *spg;
1778 int i, j;
1779 int flash_flag = 0;
1780 vbi_event event;
1781 pthread_mutex_lock(&vbi->cc.mutex);
1782 for (i = 0; i < 8; i++)
1783 {
1784 flash_flag = 0;
1785 ch = &vbi->cc.channel[i];
1786 spg = ch->pg + (ch->hidden ^ 1);
1787 for (j = 0; j < sizeof(spg->text)/sizeof(vbi_char); j++)
1788 {
1789 if (spg->text[j].flash)
1790 {
1791 flash_flag = 1;
1792 break;
1793 }
1794 }
1795 if (flash_flag)
1796 {
1797 update(ch);
1798 }
1799 }
1800 pthread_mutex_unlock(&vbi->cc.mutex);
1801}
1802
1803/*
1804Local variables:
1805c-set-style: K&R
1806c-basic-offset: 8
1807End:
1808*/
1809