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