blob: d7b6de54b4cff9086eb2f91377af2121fc4f2d25
1 | /* |
2 | * atsc-cc -- ATSC Closed Caption decoder |
3 | * |
4 | * Copyright (C) 2008 Michael H. Schimek <mschimek@users.sf.net> |
5 | * |
6 | * Contains code from zvbi-ntsc-cc closed caption decoder written by |
7 | * <timecop@japan.co.jp>, Mike Baker <mbm@linux.com>, |
8 | * Mark K. Kim <dev@cbreak.org>. |
9 | * |
10 | * Thanks to Karol Zapolski for his support. |
11 | * |
12 | * This program is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU General Public License as published by |
14 | * the Free Software Foundation; either version 2 of the License, or |
15 | * (at your option) any later version. |
16 | * |
17 | * This program is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU General Public License for more details. |
21 | * |
22 | * You should have received a copy of the GNU General Public License |
23 | * along with this program; if not, write to the Free Software |
24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
25 | * MA 02110-1301, USA. |
26 | */ |
27 | |
28 | #ifdef HAVE_CONFIG_H |
29 | # include "config.h" |
30 | #endif |
31 | |
32 | #define _GNU_SOURCE 1 |
33 | |
34 | #include "libzvbi.h" |
35 | #include "dtvcc.h" |
36 | #include "am_debug.h" |
37 | |
38 | #define elements(array) (sizeof(array) / sizeof(array[0])) |
39 | |
40 | #define LOG_TAG "ZVBI" |
41 | #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) |
42 | #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) |
43 | |
44 | #if __GNUC__ < 3 |
45 | # define likely(expr) (expr) |
46 | # define unlikely(expr) (expr) |
47 | #else |
48 | # define likely(expr) __builtin_expect(expr, 1) |
49 | # define unlikely(expr) __builtin_expect(expr, 0) |
50 | #endif |
51 | |
52 | #define N_ELEMENTS(array) (sizeof (array) / sizeof ((array)[0])) |
53 | #define CLEAR(var) memset (&(var), 0, sizeof (var)) |
54 | /* FIXME __typeof__ is a GCC extension. */ |
55 | #undef SWAP |
56 | #define SWAP(x, y) \ |
57 | do { \ |
58 | __typeof__ (x) _x = x; \ |
59 | x = y; \ |
60 | y = _x; \ |
61 | } while (0) |
62 | |
63 | #undef MIN |
64 | #define MIN(x, y) ({ \ |
65 | __typeof__ (x) _x = (x); \ |
66 | __typeof__ (y) _y = (y); \ |
67 | (void)(&_x == &_y); /* warn if types do not match */ \ |
68 | /* return */ (_x < _y) ? _x : _y; \ |
69 | }) |
70 | |
71 | #undef MAX |
72 | #define MAX(x, y) ({ \ |
73 | __typeof__ (x) _x = (x); \ |
74 | __typeof__ (y) _y = (y); \ |
75 | (void)(&_x == &_y); /* warn if types do not match */ \ |
76 | /* return */ (_x > _y) ? _x : _y; \ |
77 | }) |
78 | |
79 | #undef PARENT |
80 | #define PARENT(_ptr, _type, _member) ({ \ |
81 | __typeof__ (&((_type *) 0)->_member) _p = (_ptr); \ |
82 | (_p != 0) ? (_type *)(((char *) _p) - offsetof (_type, \ |
83 | _member)) : (_type *) 0; \ |
84 | }) |
85 | |
86 | #define VBI_RGBA(r, g, b) \ |
87 | ((((r) & 0xFF) << 0) | (((g) & 0xFF) << 8) \ |
88 | | (((b) & 0xFF) << 16) | (0xFF << 24)) |
89 | |
90 | /* These should be defined in inttypes.h. */ |
91 | #ifndef PRId64 |
92 | # define PRId64 "lld" |
93 | #endif |
94 | #ifndef PRIu64 |
95 | # define PRIu64 "llu" |
96 | #endif |
97 | #ifndef PRIx64 |
98 | # define PRIx64 "llx" |
99 | #endif |
100 | |
101 | extern void |
102 | vbi_transp_colormap(vbi_decoder *vbi, vbi_rgba *d, vbi_rgba *s, int entries); |
103 | extern void |
104 | vbi_send_event(vbi_decoder *vbi, vbi_event *ev); |
105 | static void |
106 | dtvcc_get_visible_windows(struct dtvcc_service *ds, int *cnt, struct dtvcc_window **windows); |
107 | |
108 | /* EIA 608-B decoder. */ |
109 | |
110 | static int |
111 | printable (int c) |
112 | { |
113 | if ((c & 0x7F) < 0x20) |
114 | return '.'; |
115 | else |
116 | return c & 0x7F; |
117 | } |
118 | |
119 | static void |
120 | dump (FILE * fp, |
121 | const uint8_t * buf, |
122 | unsigned int n_bytes) |
123 | _vbi_unused; |
124 | |
125 | static void |
126 | dump (FILE * fp, |
127 | const uint8_t * buf, |
128 | unsigned int n_bytes) |
129 | { |
130 | const unsigned int width = 16; |
131 | unsigned int i; |
132 | |
133 | for (i = 0; i < n_bytes; i += width) { |
134 | unsigned int end; |
135 | unsigned int j; |
136 | |
137 | end = MIN (i + width, n_bytes); |
138 | for (j = i; j < end; ++j) |
139 | fprintf (fp, "%02x ", buf[j]); |
140 | for (; j < i + width; ++j) |
141 | fputs (" ", fp); |
142 | fputc (' ', fp); |
143 | for (j = i; j < end; ++j) { |
144 | int c = buf[j]; |
145 | fputc (printable (c), fp); |
146 | } |
147 | fputc ('\n', fp); |
148 | } |
149 | } |
150 | |
151 | #define log(verb, templ, args...) \ |
152 | log_message (2, /* print_errno */ FALSE, templ , ##args) |
153 | |
154 | #define log_errno(verb, templ, args...) \ |
155 | log_message (2, /* print_errno */ TRUE, templ , ##args) |
156 | |
157 | #define bug(templ, args...) \ |
158 | log_message (1, /* print_errno */ FALSE, "BUG: " templ , ##args) |
159 | |
160 | static void |
161 | log_message (unsigned int verbosity, |
162 | vbi_bool print_errno, |
163 | const char * templ, |
164 | ...) |
165 | { |
166 | if (1) { |
167 | va_list ap; |
168 | |
169 | va_start (ap, templ); |
170 | |
171 | fprintf (stderr, "TVCC: "); |
172 | vfprintf (stderr, templ, ap); |
173 | |
174 | if (print_errno) { |
175 | fprintf (stderr, ": %s.\n", |
176 | strerror (errno)); |
177 | } |
178 | |
179 | va_end (ap); |
180 | } |
181 | } |
182 | |
183 | static void * |
184 | xmalloc (size_t size) |
185 | { |
186 | void *p; |
187 | |
188 | p = malloc (size); |
189 | if (NULL == p) { |
190 | log(2, "no memory"); |
191 | } |
192 | |
193 | return p; |
194 | } |
195 | |
196 | static char * |
197 | xasprintf (const char * templ, |
198 | ...) |
199 | { |
200 | va_list ap; |
201 | char *s; |
202 | int r; |
203 | |
204 | va_start (ap, templ); |
205 | |
206 | r = vasprintf (&s, templ, ap); |
207 | if (r < 0 || NULL == s) { |
208 | return NULL; |
209 | } |
210 | |
211 | va_end (ap); |
212 | |
213 | return s; |
214 | } |
215 | |
216 | _vbi_inline void |
217 | vbi_char_copy_attr (struct vbi_char * cp1, |
218 | struct vbi_char * cp2, |
219 | unsigned int attr) |
220 | { |
221 | if (attr & VBI_UNDERLINE) |
222 | cp1->underline = cp2->underline; |
223 | if (attr & VBI_ITALIC) |
224 | cp1->italic = cp2->italic; |
225 | if (attr & VBI_FLASH) |
226 | cp1->flash = cp2->flash; |
227 | } |
228 | |
229 | _vbi_inline void |
230 | vbi_char_clear_attr (struct vbi_char * cp, |
231 | unsigned int attr) |
232 | { |
233 | if (attr & VBI_UNDERLINE) |
234 | cp->underline = 0; |
235 | if (attr & VBI_ITALIC) |
236 | cp->italic = 0; |
237 | if (attr & VBI_FLASH) |
238 | cp->flash = 0; |
239 | } |
240 | |
241 | _vbi_inline void |
242 | vbi_char_set_attr (struct vbi_char * cp, |
243 | unsigned int attr) |
244 | { |
245 | if (attr & VBI_UNDERLINE) |
246 | cp->underline = 1; |
247 | if (attr & VBI_ITALIC) |
248 | cp->italic = 1; |
249 | if (attr & VBI_FLASH) |
250 | cp->flash = 1; |
251 | } |
252 | |
253 | _vbi_inline unsigned int |
254 | vbi_char_has_attr (struct vbi_char * cp, |
255 | unsigned int attr) |
256 | { |
257 | attr &= (VBI_UNDERLINE | VBI_ITALIC | VBI_FLASH); |
258 | |
259 | if (0 == cp->underline) |
260 | attr &= ~VBI_UNDERLINE; |
261 | if (0 == cp->italic) |
262 | attr &= ~VBI_ITALIC; |
263 | if (0 == cp->flash) |
264 | attr &= ~VBI_FLASH; |
265 | |
266 | return attr; |
267 | } |
268 | |
269 | _vbi_inline unsigned int |
270 | vbi_char_xor_attr (struct vbi_char * cp1, |
271 | struct vbi_char * cp2, |
272 | unsigned int attr) |
273 | { |
274 | attr &= (VBI_UNDERLINE | VBI_ITALIC | VBI_FLASH); |
275 | |
276 | if (0 == (cp1->underline ^ cp2->underline)) |
277 | attr &= ~VBI_UNDERLINE; |
278 | if (0 == (cp1->italic ^ cp2->italic)) |
279 | attr &= ~VBI_ITALIC; |
280 | if (0 == (cp1->flash ^ cp2->flash)) |
281 | attr &= ~VBI_FLASH; |
282 | |
283 | return attr; |
284 | } |
285 | |
286 | /* EIA 608-B Closed Caption decoder. */ |
287 | |
288 | static void |
289 | cc_timestamp_reset (struct cc_timestamp * ts) |
290 | { |
291 | CLEAR (ts->sys); |
292 | ts->pts = -1; |
293 | } |
294 | |
295 | static vbi_bool |
296 | cc_timestamp_isset (struct cc_timestamp * ts) |
297 | { |
298 | return (ts->pts >= 0 || 0 != (ts->sys.tv_sec | ts->sys.tv_usec)); |
299 | } |
300 | |
301 | static const vbi_color |
302 | cc_color_map [8] = { |
303 | VBI_WHITE, VBI_GREEN, VBI_BLUE, VBI_CYAN, |
304 | VBI_RED, VBI_YELLOW, VBI_MAGENTA, VBI_BLACK |
305 | }; |
306 | |
307 | static const int8_t |
308 | cc_pac_row_map [16] = { |
309 | /* 0 */ 10, /* 0x1040 */ |
310 | /* 1 */ -1, /* no function */ |
311 | /* 2 */ 0, 1, 2, 3, /* 0x1140 ... 0x1260 */ |
312 | /* 6 */ 11, 12, 13, 14, /* 0x1340 ... 0x1460 */ |
313 | /* 10 */ 4, 5, 6, 7, 8, 9 /* 0x1540 ... 0x1760 */ |
314 | }; |
315 | |
316 | static vbi_pgno |
317 | cc_channel_num (struct cc_decoder * cd, |
318 | struct cc_channel * ch) |
319 | { |
320 | return (ch - cd->channel) + 1; |
321 | } |
322 | |
323 | /* Note 47 CFR 15.119 (h) Character Attributes: "(1) Transmission of |
324 | Attributes. A character may be transmitted with any or all of four |
325 | attributes: Color, italics, underline, and flash. All of these |
326 | attributes are set by control codes included in the received |
327 | data. An attribute will remain in effect until changed by another |
328 | control code or until the end of the row is reached. Each row |
329 | begins with a control code which sets the color and underline |
330 | attributes. (White non-underlined is the default display attribute |
331 | if no Preamble Address Code is received before the first character |
332 | on an empty row.) Attributes are not affected by transparent spaces |
333 | within a row. (i) All Mid-Row Codes and the Flash On command are |
334 | spacing attributes which appear in the display just as if a |
335 | standard space (20h) had been received. Preamble Address Codes are |
336 | non-spacing and will not alter any attributes when used to position |
337 | the cursor in the midst of a row of characters. (ii) The color |
338 | attribute has the highest priority and can only be changed by the |
339 | Mid-Row Code of another color. Italics has the next highest |
340 | priority. If characters with both color and italics are desired, |
341 | the italics Mid-Row Code must follow the color assignment. Any |
342 | color Mid-Row Code will turn off italics. If the least significant |
343 | bit of a Preamble Address Code or of a color or italics Mid-Row |
344 | Code is a 1 (high), underlining is turned on. If that bit is a 0 |
345 | (low), underlining is off. (iii) The flash attribute is transmitted |
346 | as a Miscellaneous Control Code. The Flash On command will not |
347 | alter the status of the color, italics, or underline |
348 | attributes. However, any coloror italics Mid-Row Code will turn off |
349 | flash. (iv) Thus, for example, if a red, italicized, underlined, |
350 | flashing character is desired, the attributes must be received in |
351 | the following order: a red Mid-Row or Preamble Address Code, an |
352 | italics Mid-Row Code with underline bit, and the Flash On |
353 | command. The character will then be preceded by three spaces (two |
354 | if red was assigned via a Preamble Address Code)." |
355 | |
356 | EIA 608-B Annex C.7 Preamble Address Codes and Tab Offsets |
357 | (Regulatory/Preferred): "In general, Preamble Address Codes (PACs) |
358 | have no immediate effect on the display. A major exception is the |
359 | receipt of a PAC during roll-up captioning. In that case, if the |
360 | base row designated in the PAC is not the same as the current base |
361 | row, the display shall be moved immediately to the new base |
362 | row. [...] |
363 | |
364 | |
365 | cc_channel_num (struct cc_decoder * cd, |
366 | struct cc_channel * ch) |
367 | indenting PAC carries the attributes of white, |
368 | non-italicized, and it sets underlining on or off. Tab Offset |
369 | commands do not change these attributes. If an indenting PAC with |
370 | underline ON is received followed by a Tab Offset and by text, the |
371 | text shall be underlined (except as noted below). When a |
372 | displayable character is received, it is deposited at the current |
373 | cursor position. If there is already a displayable character in the |
374 | column immediately to the left, the new character assumes the |
375 | attributes of that character. The new character may be arriving as |
376 | the result of an indenting PAC (with or without a Tab Offset), and |
377 | that PAC may designate other attributes, but the new character is |
378 | forced to assume the attributes of the character immediately to its |
379 | left, and the PAC's attributes are ignored. If, when a displayable |
380 | character is received, it overwrites an existing PAC or mid-row |
381 | code, and there are already characters to the right of the new |
382 | character, these existing characters shall assume the same |
383 | attributes as the new character. This adoption can result in a |
384 | whole caption row suddenly changing color, underline, italics, |
385 | and/or flash attributes." |
386 | |
387 | EIA 608-B Annex C.14 Special Cases Regarding Attributes |
388 | (Normative): "In most cases, Preamble Address Codes shall set |
389 | attributes for the caption elements they address. It is |
390 | theoretically possible for a service provider to use an indenting |
391 | PAC to start a row at Column 5 or greater, and then to use |
392 | Backspace to move the cursor to the left of the PAC into an area to |
393 | which no attributes have been assigned. It is also possible for a |
394 | roll-up row, having been created by a Carriage Return, to receive |
395 | characters with no PAC used to set attributes. In these cases, and |
396 | in any other case where no explicit attributes have been assigned, |
397 | the display shall be white, non-underlined, non-italicized, and |
398 | non-flashing. In case new displayable characters are received |
399 | immediately after a Delete to End of Row (DER), the display |
400 | attributes of the first deleted character shall remain in effect if |
401 | there is a displayable character to the left of the cursor; |
402 | otherwise, the most recently received PAC shall set the display |
403 | attributes." |
404 | |
405 | 47 CFR 15.119 (n) Glossary of terms: "(6) Displayable character: |
406 | Any letter, number or symbol which is defined for on-screen |
407 | display, plus the 20h space. [...] (13) Special characters: |
408 | Displayable characters (except for "transparent space") [...]" */ |
409 | |
410 | static void |
411 | cc_format_row (struct cc_decoder * cd, |
412 | struct vbi_char * cp, |
413 | struct cc_channel * ch, |
414 | unsigned int buffer, |
415 | unsigned int row, |
416 | vbi_bool to_upper, |
417 | vbi_bool padding) |
418 | { |
419 | struct vbi_char ac; |
420 | unsigned int i; |
421 | |
422 | cd = cd; /* unused */ |
423 | |
424 | /* 47 CFR 15.119 (h)(1). EIA 608-B Section 6.4. */ |
425 | CLEAR (ac); |
426 | ac.foreground = VBI_WHITE; |
427 | ac.background = VBI_BLACK; |
428 | |
429 | /* Shortcut. */ |
430 | if (0 == (ch->dirty[buffer] & (1 << row))) { |
431 | vbi_char *end; |
432 | |
433 | ac.unicode = 0x20; |
434 | ac.opacity = VBI_TRANSPARENT_SPACE; |
435 | |
436 | end = cp + CC_MAX_COLUMNS; |
437 | if (padding) |
438 | end += 2; |
439 | |
440 | while (cp < end) |
441 | *cp++ = ac; |
442 | |
443 | return; |
444 | } |
445 | |
446 | if (padding) { |
447 | ac.unicode = 0x20; |
448 | ac.opacity = VBI_TRANSPARENT_SPACE; |
449 | *cp++ = ac; |
450 | } |
451 | |
452 | /* EIA 608-B Section 6.4. */ |
453 | ac.opacity = VBI_OPAQUE; |
454 | |
455 | for (i = CC_FIRST_COLUMN - 1; i <= CC_LAST_COLUMN; ++i) { |
456 | unsigned int color; |
457 | unsigned int c; |
458 | |
459 | ac.unicode = 0x20; |
460 | |
461 | c = ch->buffer[buffer][row][i]; |
462 | if (0 == c) { |
463 | if (padding |
464 | && VBI_TRANSPARENT_SPACE != cp[-1].opacity |
465 | && 0x20 != cp[-1].unicode) { |
466 | /* Append a space with the same colors |
467 | and opacity (opaque or |
468 | transp. backgr.) as the text to the |
469 | left of it. */ |
470 | *cp++ = ac; |
471 | /* We don't underline spaces, see |
472 | below. */ |
473 | vbi_char_clear_attr (cp - 1, -1); |
474 | } else if (i > 0) { |
475 | *cp++ = ac; |
476 | cp[-1].opacity = VBI_TRANSPARENT_SPACE; |
477 | } |
478 | |
479 | continue; |
480 | } else if (c < 0x1040) { |
481 | if (padding |
482 | && VBI_TRANSPARENT_SPACE == cp[-1].opacity) { |
483 | /* Prepend a space with the same |
484 | colors and opacity (opaque or |
485 | transp. backgr.) as the text to the |
486 | right of it. */ |
487 | cp[-1] = ac; |
488 | /* We don't underline spaces, see |
489 | below. */ |
490 | vbi_char_clear_attr (cp - 1, -1); |
491 | } |
492 | |
493 | if ((c >= 'a' && c <= 'z') |
494 | || 0x7E == c /* n with tilde */) { |
495 | /* We do not force these characters to |
496 | upper case because the standard |
497 | character set includes upper case |
498 | versions of these characters and |
499 | lower case was probably |
500 | deliberately transmitted. */ |
501 | ac.unicode = vbi_caption_unicode |
502 | (c, /* to_upper */ FALSE); |
503 | } else { |
504 | ac.unicode = vbi_caption_unicode |
505 | (c, to_upper); |
506 | } |
507 | } else if (c < 0x1120) { |
508 | /* Preamble Address Codes -- 001 crrr 1ri xxxu */ |
509 | |
510 | /* PAC is a non-spacing attribute and only |
511 | stored in the buffer at the addressed |
512 | column minus one if it replaces a |
513 | transparent space (EIA 608-B Annex C.7, |
514 | C.14). There's always a transparent space |
515 | to the left of the first column but we show |
516 | this zeroth column only if padding is |
517 | enabled. */ |
518 | if (padding |
519 | && VBI_TRANSPARENT_SPACE != cp[-1].opacity |
520 | && 0x20 != cp[-1].unicode) { |
521 | /* See 0 == c. */ |
522 | *cp++ = ac; |
523 | vbi_char_clear_attr (cp - 1, -1); |
524 | } else if (i > 0) { |
525 | *cp++ = ac; |
526 | cp[-1].opacity = VBI_TRANSPARENT_SPACE; |
527 | } |
528 | |
529 | vbi_char_clear_attr (&ac, VBI_UNDERLINE | VBI_ITALIC); |
530 | if (c & 0x0001) |
531 | vbi_char_set_attr (&ac, VBI_UNDERLINE); |
532 | if (c & 0x0010) { |
533 | ac.foreground = VBI_WHITE; |
534 | } else { |
535 | color = (c >> 1) & 7; |
536 | if (7 == color) { |
537 | ac.foreground = VBI_WHITE; |
538 | vbi_char_set_attr (&ac, VBI_ITALIC); |
539 | } else { |
540 | ac.foreground = cc_color_map[color]; |
541 | } |
542 | } |
543 | |
544 | continue; |
545 | } else if (c < 0x1130) { |
546 | /* Mid-Row Codes -- 001 c001 010 xxxu */ |
547 | /* 47 CFR 15.119 Mid-Row Codes table, |
548 | (h)(1)(ii), (h)(1)(iii). */ |
549 | |
550 | /* 47 CFR 15.119 (h)(1)(i), EIA 608-B Section |
551 | 6.2: Mid-Row codes, FON, BT, FA and FAU are |
552 | set-at spacing attributes. */ |
553 | |
554 | vbi_char_clear_attr (&ac, -1); |
555 | if (c & 0x0001) |
556 | vbi_char_set_attr (&ac, VBI_UNDERLINE); |
557 | color = (c >> 1) & 7; |
558 | if (7 == color) { |
559 | vbi_char_set_attr (&ac, VBI_ITALIC); |
560 | } else { |
561 | ac.foreground = cc_color_map[color]; |
562 | } |
563 | } else if (c < 0x1220) { |
564 | /* Special Characters -- 001 c001 011 xxxx */ |
565 | /* 47 CFR 15.119 Character Set Table. */ |
566 | |
567 | if (padding |
568 | && VBI_TRANSPARENT_SPACE == cp[-1].opacity) { |
569 | cp[-1] = ac; |
570 | vbi_char_clear_attr (cp - 1, -1); |
571 | } |
572 | |
573 | /* Note we already stored 0 instead of 0x1139 |
574 | (transparent space) in the ch->buffer. */ |
575 | ac.unicode = vbi_caption_unicode (c, to_upper); |
576 | } else if (c < 0x1428) { |
577 | /* Extended Character Set -- 001 c01x 01x xxxx */ |
578 | /* EIA 608-B Section 6.4.2 */ |
579 | |
580 | if (padding |
581 | && VBI_TRANSPARENT_SPACE == cp[-1].opacity) { |
582 | cp[-1] = ac; |
583 | vbi_char_clear_attr (cp - 1, -1); |
584 | } |
585 | |
586 | /* We do not force these characters to upper |
587 | case because the extended character set |
588 | includes upper case versions of all letters |
589 | and lower case was probably deliberately |
590 | transmitted. */ |
591 | ac.unicode = vbi_caption_unicode |
592 | (c, /* to_upper */ FALSE); |
593 | } else if (c < 0x172D) { |
594 | /* FON Flash On -- 001 c10f 010 1000 */ |
595 | /* 47 CFR 15.119 (h)(1)(iii). */ |
596 | |
597 | vbi_char_set_attr (&ac, VBI_FLASH); |
598 | } else if (c < 0x172E) { |
599 | /* BT Background Transparent -- 001 c111 010 1101 */ |
600 | /* EIA 608-B Section 6.4. */ |
601 | |
602 | ac.opacity = VBI_TRANSPARENT_FULL; |
603 | } else if (c <= 0x172F) { |
604 | /* FA Foreground Black -- 001 c111 010 111u */ |
605 | /* EIA 608-B Section 6.4. */ |
606 | |
607 | if (c & 0x0001) |
608 | vbi_char_set_attr (&ac, VBI_UNDERLINE); |
609 | ac.foreground = VBI_BLACK; |
610 | } |
611 | |
612 | *cp++ = ac; |
613 | |
614 | /* 47 CFR 15.119 and EIA 608-B are silent about |
615 | underlined spaces, but considering the example in |
616 | 47 CFR (h)(1)(iv) which would produce something |
617 | ugly like "__text" I suppose we should not |
618 | underline them. For good measure we also clear the |
619 | invisible italic and flash attribute. */ |
620 | if (0x20 == ac.unicode) |
621 | vbi_char_clear_attr (cp - 1, -1); |
622 | } |
623 | |
624 | if (padding) { |
625 | ac.unicode = 0x20; |
626 | vbi_char_clear_attr (&ac, -1); |
627 | |
628 | if (VBI_TRANSPARENT_SPACE != cp[-1].opacity |
629 | && 0x20 != cp[-1].unicode) { |
630 | *cp = ac; |
631 | } else { |
632 | ac.opacity = VBI_TRANSPARENT_SPACE; |
633 | *cp = ac; |
634 | } |
635 | } |
636 | } |
637 | |
638 | typedef enum { |
639 | /** |
640 | * 47 CFR Section 15.119 requires caption decoders to roll |
641 | * caption smoothly: Nominally each character cell has a |
642 | * height of 13 field lines. When this flag is set the current |
643 | * caption should be displayed with a vertical offset of 12 |
644 | * field lines, and after every 1001 / 30000 seconds the |
645 | * caption overlay should move up by one field line until the |
646 | * offset is zero. The roll rate should be no more than 0.433 |
647 | * seconds/row for other character cell heights. |
648 | * |
649 | * The flag may be set again before the offset returned to |
650 | * zero. The caption overlay should jump to offset 12 in this |
651 | * case regardless. |
652 | */ |
653 | VBI_START_ROLLING = (1 << 0) |
654 | } vbi_cc_page_flags; |
655 | |
656 | static void |
657 | cc_display_event (struct cc_decoder * cd, |
658 | struct cc_channel * ch, |
659 | vbi_cc_page_flags flags) |
660 | { |
661 | cd = cd; /* unused */ |
662 | ch = ch; |
663 | flags = flags; |
664 | } |
665 | |
666 | /* This decoder is mainly designed to overlay caption onto live video, |
667 | but to create transcripts we also offer an event every time a line |
668 | of caption is complete. The event occurs when certain control codes |
669 | are received. |
670 | |
671 | In POP_ON mode we send the event upon reception of EOC, which swaps |
672 | the displayed and non-displayed memory. |
673 | |
674 | In ROLL_UP and TEXT mode captioners are not expected to display new |
675 | text by erasing and overwriting a row with PAC, TOx, BS and DER so |
676 | we ignore these codes. In ROLL_UP mode CR, EDM, EOC, RCL and RDC |
677 | complete a line. CR moves the cursor to a new row, EDM erases the |
678 | displayed memory. The remaining codes switch to POP_ON or PAINT_ON |
679 | mode. In TEXT mode CR and TR are our line completion indicators. CR |
680 | works as above and TR erases the displayed memory. EDM, EOC, RDC, |
681 | RCL and RUx have no effect on TEXT buffers. |
682 | |
683 | In PAINT_ON mode RDC never erases the displayed memory and CR has |
684 | no function. Instead captioners can freely position the cursor and |
685 | erase or overwrite (parts of) rows with PAC, TOx, BS and DER, or |
686 | erase all rows with EDM. We send an event on PAC, EDM, EOC, RCL and |
687 | RUx, provided the characters (including spacing attributes) in the |
688 | current row changed since the last event. PAC is the only control |
689 | code which can move the cursor to the left and/or to a new row, and |
690 | likely to introduce a new line. EOC, RCL and RUx switch to POP_ON |
691 | or ROLL_UP mode. */ |
692 | |
693 | static void |
694 | cc_stream_event (struct cc_decoder * cd, |
695 | struct cc_channel * ch, |
696 | unsigned int first_row, |
697 | unsigned int last_row) |
698 | { |
699 | vbi_pgno channel; |
700 | unsigned int row; |
701 | |
702 | channel = cc_channel_num (cd, ch); |
703 | |
704 | for (row = first_row; row <= last_row; ++row) { |
705 | struct vbi_char text[36]; |
706 | unsigned int end; |
707 | |
708 | cc_format_row (cd, text, ch, |
709 | ch->displayed_buffer, |
710 | row, /* to_upper */ FALSE, |
711 | /* padding */ FALSE); |
712 | |
713 | for (end = 32; end > 0; --end) { |
714 | if (VBI_TRANSPARENT_SPACE != text[end - 1].opacity) |
715 | break; |
716 | } |
717 | |
718 | if (0 == end) |
719 | continue; |
720 | } |
721 | |
722 | cc_timestamp_reset (&ch->timestamp_c0); |
723 | } |
724 | |
725 | static void |
726 | cc_put_char (struct cc_decoder * cd, |
727 | struct cc_channel * ch, |
728 | int c, |
729 | vbi_bool displayable, |
730 | vbi_bool backspace) |
731 | { |
732 | uint16_t *text; |
733 | unsigned int curr_buffer; |
734 | unsigned int row; |
735 | unsigned int column; |
736 | |
737 | /* 47 CFR Section 15.119 (f)(1), (f)(2), (f)(3). */ |
738 | curr_buffer = ch->displayed_buffer |
739 | ^ (CC_MODE_POP_ON == ch->mode); |
740 | |
741 | row = ch->curr_row; |
742 | column = ch->curr_column; |
743 | |
744 | if (unlikely (backspace)) { |
745 | /* 47 CFR 15.119 (f)(1)(vi), (f)(2)(ii), |
746 | (f)(3)(i). EIA 608-B Section 6.4.2, 7.4. */ |
747 | if (column > CC_FIRST_COLUMN) |
748 | --column; |
749 | } else { |
750 | /* 47 CFR 15.119 (f)(1)(v), (f)(1)(vi), (f)(2)(ii), |
751 | (f)(3)(i). EIA 608-B Section 7.4. */ |
752 | if (column < CC_LAST_COLUMN) |
753 | ch->curr_column = column + 1; |
754 | } |
755 | |
756 | text = &ch->buffer[curr_buffer][row][0]; |
757 | text[column] = c; |
758 | |
759 | /* Send a display update event when the displayed buffer of |
760 | the current channel changed, but no more than once for each |
761 | pair of Closed Caption bytes. */ |
762 | /* XXX This may not be a visible change, but such cases are |
763 | rare and we'd need something close to format_row() to be |
764 | sure. */ |
765 | if (CC_MODE_POP_ON != ch->mode) { |
766 | cd->event_pending = ch; |
767 | } |
768 | |
769 | if (likely (displayable)) { |
770 | /* Note EIA 608-B Annex C.7, C.14. */ |
771 | if (CC_FIRST_COLUMN == column |
772 | || 0 == text[column - 1]) { |
773 | /* Note last_pac may be 0 as well. */ |
774 | text[column - 1] = ch->last_pac; |
775 | } |
776 | |
777 | if (c >= 'a' && c <= 'z') { |
778 | ch->uppercase_predictor = 0; |
779 | } else if (c >= 'A' && c <= 'Z') { |
780 | unsigned int up; |
781 | |
782 | up = ch->uppercase_predictor + 1; |
783 | if (up > 0) |
784 | ch->uppercase_predictor = up; |
785 | } |
786 | } else if (unlikely (0 == c)) { |
787 | unsigned int i; |
788 | |
789 | /* This is Special Character "Transparent space". */ |
790 | |
791 | for (i = CC_FIRST_COLUMN; i <= CC_LAST_COLUMN; ++i) |
792 | c |= ch->buffer[curr_buffer][row][i]; |
793 | |
794 | ch->dirty[curr_buffer] &= ~((0 == c) << row); |
795 | |
796 | return; |
797 | } |
798 | |
799 | assert (sizeof (ch->dirty[0]) * 8 - 1 >= CC_MAX_ROWS); |
800 | ch->dirty[curr_buffer] |= 1 << row; |
801 | |
802 | if (ch->timestamp_c0.pts < 0 |
803 | && 0 == (ch->timestamp_c0.sys.tv_sec |
804 | | ch->timestamp_c0.sys.tv_usec)) { |
805 | ch->timestamp_c0 = cd->timestamp; |
806 | } |
807 | } |
808 | |
809 | static void |
810 | cc_ext_control_code (struct cc_decoder * cd, |
811 | struct cc_channel * ch, |
812 | unsigned int c2) |
813 | { |
814 | unsigned int column; |
815 | |
816 | switch (c2) { |
817 | case 0x21: /* TO1 */ |
818 | case 0x22: /* TO2 */ |
819 | case 0x23: /* TO3 Tab Offset -- 001 c111 010 00xx */ |
820 | /* 47 CFR 15.119 (e)(1)(ii). EIA 608-B Section 7.4, |
821 | Annex C.7. */ |
822 | column = ch->curr_column + (c2 & 3); |
823 | ch->curr_column = MIN (column, |
824 | (unsigned int) CC_LAST_COLUMN); |
825 | break; |
826 | |
827 | case 0x24: /* Select standard character set in normal size */ |
828 | case 0x25: /* Select standard character set in double size */ |
829 | case 0x26: /* Select first private character set */ |
830 | case 0x27: /* Select second private character set */ |
831 | case 0x28: /* Select character set GB 2312-80 (Chinese) */ |
832 | case 0x29: /* Select character set KSC 5601-1987 (Korean) */ |
833 | case 0x2A: /* Select first registered character set. */ |
834 | /* EIA 608-B Section 6.3 Closed Group Extensions. */ |
835 | break; |
836 | |
837 | case 0x2D: /* BT Background Transparent -- 001 c111 010 1101 */ |
838 | case 0x2E: /* FA Foreground Black -- 001 c111 010 1110 */ |
839 | case 0x2F: /* FAU Foregr. Black Underl. -- 001 c111 010 1111 */ |
840 | /* EIA 608-B Section 6.2. */ |
841 | cc_put_char (cd, ch, 0x1700 | c2, |
842 | /* displayable */ FALSE, |
843 | /* backspace */ TRUE); |
844 | break; |
845 | |
846 | default: |
847 | /* 47 CFR Section 15.119 (j): Ignore. */ |
848 | break; |
849 | } |
850 | } |
851 | |
852 | /* Send a stream event if the current row has changed since the last |
853 | stream event. This is necessary in paint-on mode where CR has no |
854 | function and captioners can freely position the cursor to erase or |
855 | overwrite (parts of) rows. */ |
856 | static void |
857 | cc_stream_event_if_changed (struct cc_decoder * cd, |
858 | struct cc_channel * ch) |
859 | { |
860 | unsigned int curr_buffer; |
861 | unsigned int row; |
862 | unsigned int i; |
863 | |
864 | curr_buffer = ch->displayed_buffer; |
865 | row = ch->curr_row; |
866 | |
867 | if (0 == (ch->dirty[curr_buffer] & (1 << row))) |
868 | return; |
869 | |
870 | for (i = CC_FIRST_COLUMN; i <= CC_LAST_COLUMN; ++i) { |
871 | unsigned int c1; |
872 | unsigned int c2; |
873 | |
874 | c1 = ch->buffer[curr_buffer][row][i]; |
875 | if (c1 >= 0x1040) { |
876 | if (c1 < 0x1120) { |
877 | c1 = 0; /* PAC -- non-spacing */ |
878 | } else if (c1 < 0x1130 || c1 >= 0x1428) { |
879 | /* MR, FON, BT, FA, FAU -- spacing */ |
880 | c1 = 0x20; |
881 | } |
882 | } |
883 | |
884 | c2 = ch->buffer[2][row][i]; |
885 | if (c2 >= 0x1040) { |
886 | if (c2 < 0x1120) { |
887 | c2 = 0; |
888 | } else if (c2 < 0x1130 || c2 >= 0x1428) { |
889 | c1 = 0x20; |
890 | } |
891 | } |
892 | |
893 | if (c1 != c2) { |
894 | cc_stream_event (cd, ch, row, row); |
895 | |
896 | memcpy (ch->buffer[2][row], |
897 | ch->buffer[curr_buffer][row], |
898 | sizeof (ch->buffer[0][0])); |
899 | |
900 | ch->dirty[2] = ch->dirty[curr_buffer]; |
901 | |
902 | return; |
903 | } |
904 | } |
905 | } |
906 | |
907 | static void |
908 | cc_end_of_caption (struct cc_decoder * cd, |
909 | struct cc_channel * ch) |
910 | { |
911 | unsigned int curr_buffer; |
912 | unsigned int row; |
913 | |
914 | /* EOC End Of Caption -- 001 c10f 010 1111 */ |
915 | |
916 | curr_buffer = ch->displayed_buffer; |
917 | |
918 | switch (ch->mode) { |
919 | case CC_MODE_UNKNOWN: |
920 | case CC_MODE_POP_ON: |
921 | break; |
922 | |
923 | case CC_MODE_ROLL_UP: |
924 | row = ch->curr_row; |
925 | if (0 != (ch->dirty[curr_buffer] & (1 << row))) |
926 | cc_stream_event (cd, ch, row, row); |
927 | break; |
928 | |
929 | case CC_MODE_PAINT_ON: |
930 | cc_stream_event_if_changed (cd, ch); |
931 | break; |
932 | |
933 | case CC_MODE_TEXT: |
934 | /* Not reached. (ch is a caption channel.) */ |
935 | return; |
936 | } |
937 | |
938 | ch->displayed_buffer = curr_buffer ^= 1; |
939 | |
940 | /* 47 CFR Section 15.119 (f)(2). */ |
941 | ch->mode = CC_MODE_POP_ON; |
942 | |
943 | if (0 != ch->dirty[curr_buffer]) { |
944 | ch->timestamp_c0 = cd->timestamp; |
945 | |
946 | cc_stream_event (cd, ch, |
947 | CC_FIRST_ROW, |
948 | CC_LAST_ROW); |
949 | |
950 | cc_display_event (cd, ch, 0); |
951 | } |
952 | } |
953 | |
954 | static void |
955 | cc_carriage_return (struct cc_decoder * cd, |
956 | struct cc_channel * ch) |
957 | { |
958 | unsigned int curr_buffer; |
959 | unsigned int row; |
960 | unsigned int window_rows; |
961 | unsigned int first_row; |
962 | |
963 | /* CR Carriage Return -- 001 c10f 010 1101 */ |
964 | |
965 | curr_buffer = ch->displayed_buffer; |
966 | row = ch->curr_row; |
967 | |
968 | switch (ch->mode) { |
969 | case CC_MODE_UNKNOWN: |
970 | return; |
971 | |
972 | case CC_MODE_ROLL_UP: |
973 | /* 47 CFR Section 15.119 (f)(1)(iii). */ |
974 | ch->curr_column = CC_FIRST_COLUMN; |
975 | |
976 | /* 47 CFR 15.119 (f)(1): "The cursor always remains on |
977 | the base row." */ |
978 | |
979 | /* XXX Spec? */ |
980 | ch->last_pac = 0; |
981 | |
982 | /* No event if the buffer contains only |
983 | TRANSPARENT_SPACEs. */ |
984 | if (0 == ch->dirty[curr_buffer]) |
985 | return; |
986 | |
987 | window_rows = MIN (row + 1 - CC_FIRST_ROW, |
988 | ch->window_rows); |
989 | break; |
990 | |
991 | case CC_MODE_POP_ON: |
992 | case CC_MODE_PAINT_ON: |
993 | /* 47 CFR 15.119 (f)(2)(i), (f)(3)(i): No effect. */ |
994 | return; |
995 | |
996 | case CC_MODE_TEXT: |
997 | /* 47 CFR Section 15.119 (f)(1)(iii). */ |
998 | ch->curr_column = CC_FIRST_COLUMN; |
999 | |
1000 | /* XXX Spec? */ |
1001 | ch->last_pac = 0; |
1002 | |
1003 | /* EIA 608-B Section 7.4: "When Text Mode has |
1004 | initially been selected and the specified Text |
1005 | memory is empty, the cursor starts at the topmost |
1006 | row, Column 1, and moves down to Column 1 on the |
1007 | next row each time a Carriage Return is received |
1008 | until the last available row is reached. A variety |
1009 | of methods may be used to accomplish the scrolling, |
1010 | provided that the text is legible while moving. For |
1011 | example, as soon as all of the available rows of |
1012 | text are on the screen, Text Mode switches to the |
1013 | standard roll-up type of presentation." */ |
1014 | |
1015 | if (CC_LAST_ROW != row) { |
1016 | if (0 != (ch->dirty[curr_buffer] & (1 << row))) { |
1017 | cc_stream_event (cd, ch, row, row); |
1018 | } |
1019 | |
1020 | ch->curr_row = row + 1; |
1021 | |
1022 | return; |
1023 | } |
1024 | |
1025 | /* No event if the buffer contains all |
1026 | TRANSPARENT_SPACEs. */ |
1027 | if (0 == ch->dirty[curr_buffer]) |
1028 | return; |
1029 | |
1030 | window_rows = CC_MAX_ROWS; |
1031 | |
1032 | break; |
1033 | } |
1034 | |
1035 | /* 47 CFR Section 15.119 (f)(1)(iii). In roll-up mode: "Each |
1036 | time a Carriage Return is received, the text in the top row |
1037 | of the window is erased from memory and from the display or |
1038 | scrolled off the top of the window. The remaining rows of |
1039 | text are each rolled up into the next highest row in the |
1040 | window, leaving the base row blank and ready to accept new |
1041 | text." */ |
1042 | |
1043 | if (0 != (ch->dirty[curr_buffer] & (1 << row))) { |
1044 | cc_stream_event (cd, ch, row, row); |
1045 | } |
1046 | |
1047 | first_row = row + 1 - window_rows; |
1048 | memmove (ch->buffer[curr_buffer][first_row], |
1049 | ch->buffer[curr_buffer][first_row + 1], |
1050 | (window_rows - 1) * sizeof (ch->buffer[0][0])); |
1051 | |
1052 | ch->dirty[curr_buffer] >>= 1; |
1053 | |
1054 | memset (ch->buffer[curr_buffer][row], 0, |
1055 | sizeof (ch->buffer[0][0])); |
1056 | |
1057 | cc_display_event (cd, ch, VBI_START_ROLLING); |
1058 | } |
1059 | |
1060 | static void |
1061 | cc_erase_memory (struct cc_decoder * cd, |
1062 | struct cc_channel * ch, |
1063 | unsigned int buffer) |
1064 | { |
1065 | if (0 != ch->dirty[buffer]) { |
1066 | CLEAR (ch->buffer[buffer]); |
1067 | |
1068 | ch->dirty[buffer] = 0; |
1069 | |
1070 | if (buffer == ch->displayed_buffer) |
1071 | cc_display_event (cd, ch, 0); |
1072 | } |
1073 | } |
1074 | |
1075 | static void |
1076 | cc_erase_displayed_memory (struct cc_decoder * cd, |
1077 | struct cc_channel * ch) |
1078 | { |
1079 | unsigned int row; |
1080 | |
1081 | /* EDM Erase Displayed Memory -- 001 c10f 010 1100 */ |
1082 | |
1083 | switch (ch->mode) { |
1084 | case CC_MODE_UNKNOWN: |
1085 | /* We have not received EOC, RCL, RDC or RUx yet, but |
1086 | ch is valid. */ |
1087 | break; |
1088 | |
1089 | case CC_MODE_ROLL_UP: |
1090 | row = ch->curr_row; |
1091 | if (0 != (ch->dirty[ch->displayed_buffer] & (1 << row))) |
1092 | cc_stream_event (cd, ch, row, row); |
1093 | break; |
1094 | |
1095 | case CC_MODE_PAINT_ON: |
1096 | cc_stream_event_if_changed (cd, ch); |
1097 | break; |
1098 | |
1099 | case CC_MODE_POP_ON: |
1100 | /* Nothing to do. */ |
1101 | break; |
1102 | |
1103 | case CC_MODE_TEXT: |
1104 | /* Not reached. (ch is a caption channel.) */ |
1105 | return; |
1106 | } |
1107 | |
1108 | /* May send a display event. */ |
1109 | cc_erase_memory (cd, ch, ch->displayed_buffer); |
1110 | } |
1111 | |
1112 | static void |
1113 | cc_text_restart (struct cc_decoder * cd, |
1114 | struct cc_channel * ch) |
1115 | { |
1116 | unsigned int curr_buffer; |
1117 | unsigned int row; |
1118 | |
1119 | /* TR Text Restart -- 001 c10f 010 1010 */ |
1120 | |
1121 | curr_buffer = ch->displayed_buffer; |
1122 | row = ch->curr_row; |
1123 | |
1124 | /* ch->mode is invariably CC_MODE_TEXT. */ |
1125 | |
1126 | if (0 != (ch->dirty[curr_buffer] & (1 << row))) { |
1127 | cc_stream_event (cd, ch, row, row); |
1128 | } |
1129 | |
1130 | /* EIA 608-B Section 7.4. */ |
1131 | /* May send a display event. */ |
1132 | cc_erase_memory (cd, ch, ch->displayed_buffer); |
1133 | |
1134 | /* EIA 608-B Section 7.4. */ |
1135 | ch->curr_row = CC_FIRST_ROW; |
1136 | ch->curr_column = CC_FIRST_COLUMN; |
1137 | } |
1138 | |
1139 | static void |
1140 | cc_resume_direct_captioning (struct cc_decoder * cd, |
1141 | struct cc_channel * ch) |
1142 | { |
1143 | unsigned int curr_buffer; |
1144 | unsigned int row; |
1145 | |
1146 | /* RDC Resume Direct Captioning -- 001 c10f 010 1001 */ |
1147 | |
1148 | /* 47 CFR 15.119 (f)(1)(x), (f)(2)(vi) and EIA 608-B Annex |
1149 | B.7: Does not erase memory, does not move the cursor when |
1150 | resuming after a Text transmission. |
1151 | |
1152 | XXX If ch->mode is unknown, roll-up or pop-on, what shall |
1153 | we do if no PAC is received between RDC and the text? */ |
1154 | |
1155 | curr_buffer = ch->displayed_buffer; |
1156 | row = ch->curr_row; |
1157 | |
1158 | switch (ch->mode) { |
1159 | case CC_MODE_ROLL_UP: |
1160 | if (0 != (ch->dirty[curr_buffer] & (1 << row))) |
1161 | cc_stream_event (cd, ch, row, row); |
1162 | |
1163 | /* fall through */ |
1164 | |
1165 | case CC_MODE_UNKNOWN: |
1166 | case CC_MODE_POP_ON: |
1167 | /* No change since last stream_event(). */ |
1168 | memcpy (ch->buffer[2], ch->buffer[curr_buffer], |
1169 | sizeof (ch->buffer[2])); |
1170 | break; |
1171 | |
1172 | case CC_MODE_PAINT_ON: |
1173 | /* Mode continues. */ |
1174 | break; |
1175 | |
1176 | case CC_MODE_TEXT: |
1177 | /* Not reached. (ch is a caption channel.) */ |
1178 | return; |
1179 | } |
1180 | |
1181 | ch->mode = CC_MODE_PAINT_ON; |
1182 | } |
1183 | |
1184 | static void |
1185 | cc_resize_window (struct cc_decoder * cd, |
1186 | struct cc_channel * ch, |
1187 | unsigned int new_rows) |
1188 | { |
1189 | unsigned int curr_buffer; |
1190 | unsigned int max_rows; |
1191 | unsigned int old_rows; |
1192 | unsigned int row1; |
1193 | |
1194 | curr_buffer = ch->displayed_buffer; |
1195 | |
1196 | /* No event if the buffer contains all TRANSPARENT_SPACEs. */ |
1197 | if (0 == ch->dirty[curr_buffer]) |
1198 | return; |
1199 | |
1200 | row1 = ch->curr_row + 1; |
1201 | max_rows = row1 - CC_FIRST_ROW; |
1202 | old_rows = MIN (ch->window_rows, max_rows); |
1203 | new_rows = MIN (new_rows, max_rows); |
1204 | |
1205 | /* Nothing to do unless the window shrinks. */ |
1206 | if (0 == new_rows || new_rows >= old_rows) |
1207 | return; |
1208 | |
1209 | memset (&ch->buffer[curr_buffer][row1 - old_rows][0], 0, |
1210 | (old_rows - new_rows) |
1211 | * sizeof (ch->buffer[0][0])); |
1212 | |
1213 | ch->dirty[curr_buffer] &= -1 << (row1 - new_rows); |
1214 | |
1215 | cc_display_event (cd, ch, 0); |
1216 | } |
1217 | |
1218 | static void |
1219 | cc_roll_up_caption (struct cc_decoder * cd, |
1220 | struct cc_channel * ch, |
1221 | unsigned int c2) |
1222 | { |
1223 | unsigned int window_rows; |
1224 | |
1225 | /* Roll-Up Captions -- 001 c10f 010 01xx */ |
1226 | |
1227 | window_rows = (c2 & 7) - 3; /* 2, 3, 4 */ |
1228 | |
1229 | switch (ch->mode) { |
1230 | case CC_MODE_ROLL_UP: |
1231 | /* 47 CFR 15.119 (f)(1)(iv). */ |
1232 | /* May send a display event. */ |
1233 | cc_resize_window (cd, ch, window_rows); |
1234 | |
1235 | /* fall through */ |
1236 | |
1237 | case CC_MODE_UNKNOWN: |
1238 | ch->mode = CC_MODE_ROLL_UP; |
1239 | ch->window_rows = window_rows; |
1240 | |
1241 | /* 47 CFR 15.119 (f)(1)(ix): No cursor movements, |
1242 | no memory erasing. */ |
1243 | |
1244 | break; |
1245 | |
1246 | case CC_MODE_PAINT_ON: |
1247 | cc_stream_event_if_changed (cd, ch); |
1248 | |
1249 | /* fall through */ |
1250 | |
1251 | case CC_MODE_POP_ON: |
1252 | ch->mode = CC_MODE_ROLL_UP; |
1253 | ch->window_rows = window_rows; |
1254 | |
1255 | /* 47 CFR 15.119 (f)(1)(ii). */ |
1256 | ch->curr_row = CC_LAST_ROW; |
1257 | ch->curr_column = CC_FIRST_COLUMN; |
1258 | |
1259 | /* 47 CFR 15.119 (f)(1)(x). */ |
1260 | /* May send a display event. */ |
1261 | cc_erase_memory (cd, ch, ch->displayed_buffer); |
1262 | cc_erase_memory (cd, ch, ch->displayed_buffer ^ 1); |
1263 | |
1264 | break; |
1265 | |
1266 | case CC_MODE_TEXT: |
1267 | /* Not reached. (ch is a caption channel.) */ |
1268 | return; |
1269 | } |
1270 | } |
1271 | |
1272 | static void |
1273 | cc_delete_to_end_of_row (struct cc_decoder * cd, |
1274 | struct cc_channel * ch) |
1275 | { |
1276 | unsigned int curr_buffer; |
1277 | unsigned int row; |
1278 | |
1279 | /* DER Delete To End Of Row -- 001 c10f 010 0100 */ |
1280 | |
1281 | /* 47 CFR 15.119 (f)(1)(vii), (f)(2)(iii), (f)(3)(ii) and EIA |
1282 | 608-B Section 7.4: In all caption modes and Text mode |
1283 | "[the] Delete to End of Row command will erase from memory |
1284 | any characters or control codes starting at the current |
1285 | cursor location and in all columns to its right on the same |
1286 | row." */ |
1287 | |
1288 | curr_buffer = ch->displayed_buffer |
1289 | ^ (CC_MODE_POP_ON == ch->mode); |
1290 | |
1291 | row = ch->curr_row; |
1292 | |
1293 | /* No event if the row contains only TRANSPARENT_SPACEs. */ |
1294 | if (0 != (ch->dirty[curr_buffer] & (1 << row))) { |
1295 | unsigned int column; |
1296 | unsigned int i; |
1297 | uint16_t c; |
1298 | |
1299 | column = ch->curr_column; |
1300 | |
1301 | memset (&ch->buffer[curr_buffer][row][column], 0, |
1302 | (CC_LAST_COLUMN - column + 1) |
1303 | * sizeof (ch->buffer[0][0][0])); |
1304 | |
1305 | c = 0; |
1306 | for (i = CC_FIRST_COLUMN; i < column; ++i) |
1307 | c |= ch->buffer[curr_buffer][row][i]; |
1308 | |
1309 | ch->dirty[curr_buffer] &= ~((0 == c) << row); |
1310 | |
1311 | cc_display_event (cd, ch, 0); |
1312 | } |
1313 | } |
1314 | |
1315 | static void |
1316 | cc_backspace (struct cc_decoder * cd, |
1317 | struct cc_channel * ch) |
1318 | { |
1319 | unsigned int curr_buffer; |
1320 | unsigned int row; |
1321 | unsigned int column; |
1322 | |
1323 | /* BS Backspace -- 001 c10f 010 0001 */ |
1324 | |
1325 | /* 47 CFR Section 15.119 (f)(1)(vi), (f)(2)(ii), (f)(3)(i) and |
1326 | EIA 608-B Section 7.4. */ |
1327 | column = ch->curr_column; |
1328 | if (column <= CC_FIRST_COLUMN) |
1329 | return; |
1330 | |
1331 | ch->curr_column = --column; |
1332 | |
1333 | curr_buffer = ch->displayed_buffer |
1334 | ^ (CC_MODE_POP_ON == ch->mode); |
1335 | |
1336 | row = ch->curr_row; |
1337 | |
1338 | /* No event if there's no visible effect. */ |
1339 | if (0 != ch->buffer[curr_buffer][row][column]) { |
1340 | unsigned int i; |
1341 | uint16_t c; |
1342 | |
1343 | /* 47 CFR 15.119 (f), (f)(1)(vi), (f)(2)(ii) and EIA |
1344 | 608-B Section 7.4. */ |
1345 | ch->buffer[curr_buffer][row][column] = 0; |
1346 | |
1347 | c = 0; |
1348 | for (i = CC_FIRST_COLUMN; i <= CC_LAST_COLUMN; ++i) |
1349 | c |= ch->buffer[curr_buffer][row][i]; |
1350 | |
1351 | ch->dirty[curr_buffer] &= ~((0 == c) << row); |
1352 | |
1353 | cc_display_event (cd, ch, 0); |
1354 | } |
1355 | } |
1356 | |
1357 | static void |
1358 | cc_resume_caption_loading (struct cc_decoder * cd, |
1359 | struct cc_channel * ch) |
1360 | { |
1361 | unsigned int row; |
1362 | |
1363 | /* RCL Resume Caption Loading -- 001 c10f 010 0000 */ |
1364 | |
1365 | switch (ch->mode) { |
1366 | case CC_MODE_UNKNOWN: |
1367 | case CC_MODE_POP_ON: |
1368 | break; |
1369 | |
1370 | case CC_MODE_ROLL_UP: |
1371 | row = ch->curr_row; |
1372 | if (0 != (ch->dirty[ch->displayed_buffer] & (1 << row))) |
1373 | cc_stream_event (cd, ch, row, row); |
1374 | break; |
1375 | |
1376 | case CC_MODE_PAINT_ON: |
1377 | cc_stream_event_if_changed (cd, ch); |
1378 | break; |
1379 | |
1380 | case CC_MODE_TEXT: |
1381 | /* Not reached. (ch is a caption channel.) */ |
1382 | return; |
1383 | } |
1384 | |
1385 | /* 47 CFR 15.119 (f)(1)(x): Does not erase memory. |
1386 | (f)(2)(iv): Cursor position remains unchanged. */ |
1387 | |
1388 | ch->mode = CC_MODE_POP_ON; |
1389 | } |
1390 | |
1391 | |
1392 | |
1393 | /* Note curr_ch is invalid if UNKNOWN_CC_CHANNEL == cd->cc.curr_ch_num. */ |
1394 | static struct cc_channel * |
1395 | cc_switch_channel (struct cc_decoder * cd, |
1396 | struct cc_channel * curr_ch, |
1397 | vbi_pgno new_ch_num, |
1398 | enum field_num f) |
1399 | { |
1400 | struct cc_channel *new_ch; |
1401 | |
1402 | if (UNKNOWN_CC_CHANNEL != cd->curr_ch_num[f] |
1403 | && CC_MODE_UNKNOWN != curr_ch->mode) { |
1404 | /* XXX Force a display update if we do not send events |
1405 | on every display change. */ |
1406 | } |
1407 | |
1408 | cd->curr_ch_num[f] = new_ch_num; |
1409 | new_ch = &cd->channel[new_ch_num - VBI_CAPTION_CC1]; |
1410 | |
1411 | return new_ch; |
1412 | } |
1413 | |
1414 | /* Note ch is invalid if UNKNOWN_CC_CHANNEL == cd->cc.curr_ch_num[f]. */ |
1415 | static void |
1416 | cc_misc_control_code (struct cc_decoder * cd, |
1417 | struct cc_channel * ch, |
1418 | unsigned int c2, |
1419 | unsigned int ch_num0, |
1420 | enum field_num f) |
1421 | { |
1422 | unsigned int new_ch_num; |
1423 | |
1424 | /* Misc Control Codes -- 001 c10f 010 xxxx */ |
1425 | |
1426 | /* c = channel (0 -> CC1/CC3/T1/T3, 1 -> CC2/CC4/T2/T4) |
1427 | -- 47 CFR Section 15.119, EIA 608-B Section 7.7. |
1428 | f = field (0 -> F1, 1 -> F2) |
1429 | -- EIA 608-B Section 8.4, 8.5. */ |
1430 | |
1431 | /* XXX The f flag is intended to detect accidential field |
1432 | swapping and we should use it for that purpose. */ |
1433 | |
1434 | switch (c2 & 15) { |
1435 | case 0: /* RCL Resume Caption Loading -- 001 c10f 010 0000 */ |
1436 | /* 47 CFR 15.119 (f)(2) and EIA 608-B Section 7.7. */ |
1437 | new_ch_num = VBI_CAPTION_CC1 + (ch_num0 & 3); |
1438 | ch = cc_switch_channel (cd, ch, new_ch_num, f); |
1439 | cc_resume_caption_loading (cd, ch); |
1440 | break; |
1441 | |
1442 | case 1: /* BS Backspace -- 001 c10f 010 0001 */ |
1443 | if (UNKNOWN_CC_CHANNEL == cd->curr_ch_num[f] |
1444 | || CC_MODE_UNKNOWN == ch->mode) |
1445 | break; |
1446 | cc_backspace (cd, ch); |
1447 | break; |
1448 | |
1449 | case 2: /* reserved (formerly AOF Alarm Off) */ |
1450 | case 3: /* reserved (formerly AON Alarm On) */ |
1451 | break; |
1452 | |
1453 | case 4: /* DER Delete To End Of Row -- 001 c10f 010 0100 */ |
1454 | if (UNKNOWN_CC_CHANNEL == cd->curr_ch_num[f] |
1455 | || CC_MODE_UNKNOWN == ch->mode) |
1456 | break; |
1457 | cc_delete_to_end_of_row (cd, ch); |
1458 | break; |
1459 | |
1460 | case 5: /* RU2 */ |
1461 | case 6: /* RU3 */ |
1462 | case 7: /* RU4 Roll-Up Captions -- 001 c10f 010 01xx */ |
1463 | /* 47 CFR 15.119 (f)(1) and EIA 608-B Section 7.7. */ |
1464 | new_ch_num = VBI_CAPTION_CC1 + (ch_num0 & 3); |
1465 | ch = cc_switch_channel (cd, ch, new_ch_num, f); |
1466 | cc_roll_up_caption (cd, ch, c2); |
1467 | break; |
1468 | |
1469 | case 8: /* FON Flash On -- 001 c10f 010 1000 */ |
1470 | if (UNKNOWN_CC_CHANNEL == cd->curr_ch_num[f] |
1471 | || CC_MODE_UNKNOWN == ch->mode) |
1472 | break; |
1473 | |
1474 | /* 47 CFR 15.119 (h)(1)(i): Spacing attribute. */ |
1475 | cc_put_char (cd, ch, 0x1428, |
1476 | /* displayable */ FALSE, |
1477 | /* backspace */ FALSE); |
1478 | break; |
1479 | |
1480 | case 9: /* RDC Resume Direct Captioning -- 001 c10f 010 1001 */ |
1481 | /* 47 CFR 15.119 (f)(3) and EIA 608-B Section 7.7. */ |
1482 | new_ch_num = VBI_CAPTION_CC1 + (ch_num0 & 3); |
1483 | ch = cc_switch_channel (cd, ch, new_ch_num, f); |
1484 | cc_resume_direct_captioning (cd, ch); |
1485 | break; |
1486 | |
1487 | case 10: /* TR Text Restart -- 001 c10f 010 1010 */ |
1488 | /* EIA 608-B Section 7.4. */ |
1489 | new_ch_num = VBI_CAPTION_T1 + (ch_num0 & 3); |
1490 | ch = cc_switch_channel (cd, ch, new_ch_num, f); |
1491 | cc_text_restart (cd, ch); |
1492 | break; |
1493 | |
1494 | case 11: /* RTD Resume Text Display -- 001 c10f 010 1011 */ |
1495 | /* EIA 608-B Section 7.4. */ |
1496 | new_ch_num = VBI_CAPTION_T1 + (ch_num0 & 3); |
1497 | ch = cc_switch_channel (cd, ch, new_ch_num, f); |
1498 | /* ch->mode is invariably CC_MODE_TEXT. */ |
1499 | break; |
1500 | |
1501 | case 12: /* EDM Erase Displayed Memory -- 001 c10f 010 1100 */ |
1502 | /* 47 CFR 15.119 (f). EIA 608-B Section 7.7 and Annex |
1503 | B.7: "[The] command shall be acted upon as |
1504 | appropriate for caption processing without |
1505 | terminating the Text Mode data stream." */ |
1506 | |
1507 | /* We need not check cd->curr_ch_num because bit 2 is |
1508 | implied, bit 1 is the known field number and bit 0 |
1509 | is coded in the control code. */ |
1510 | ch = &cd->channel[ch_num0 & 3]; |
1511 | |
1512 | cc_erase_displayed_memory (cd, ch); |
1513 | |
1514 | break; |
1515 | |
1516 | case 13: /* CR Carriage Return -- 001 c10f 010 1101 */ |
1517 | if (UNKNOWN_CC_CHANNEL == cd->curr_ch_num[f]) |
1518 | break; |
1519 | cc_carriage_return (cd, ch); |
1520 | break; |
1521 | |
1522 | case 14: /* ENM Erase Non-Displayed Memory -- 001 c10f 010 1110 */ |
1523 | /* 47 CFR 15.119 (f)(2)(v). EIA 608-B Section 7.7 and |
1524 | Annex B.7: "[The] command shall be acted upon as |
1525 | appropriate for caption processing without |
1526 | terminating the Text Mode data stream." */ |
1527 | |
1528 | /* See EDM. */ |
1529 | ch = &cd->channel[ch_num0 & 3]; |
1530 | |
1531 | cc_erase_memory (cd, ch, ch->displayed_buffer ^ 1); |
1532 | |
1533 | break; |
1534 | |
1535 | case 15: /* EOC End Of Caption -- 001 c10f 010 1111 */ |
1536 | /* 47 CFR 15.119 (f), (f)(2), (f)(3)(iv) and EIA 608-B |
1537 | Section 7.7, Annex C.11. */ |
1538 | new_ch_num = VBI_CAPTION_CC1 + (ch_num0 & 3); |
1539 | ch = cc_switch_channel (cd, ch, new_ch_num, f); |
1540 | cc_end_of_caption (cd, ch); |
1541 | break; |
1542 | } |
1543 | } |
1544 | |
1545 | static void |
1546 | cc_move_window (struct cc_decoder * cd, |
1547 | struct cc_channel * ch, |
1548 | unsigned int new_base_row) |
1549 | { |
1550 | uint8_t *base; |
1551 | unsigned int curr_buffer; |
1552 | unsigned int bytes_per_row; |
1553 | unsigned int old_max_rows; |
1554 | unsigned int new_max_rows; |
1555 | unsigned int copy_bytes; |
1556 | unsigned int erase_begin; |
1557 | unsigned int erase_end; |
1558 | |
1559 | curr_buffer = ch->displayed_buffer; |
1560 | |
1561 | /* No event if we do not move the window or the buffer |
1562 | contains only TRANSPARENT_SPACEs. */ |
1563 | if (new_base_row == ch->curr_row |
1564 | || 0 == ch->dirty[curr_buffer]) |
1565 | return; |
1566 | |
1567 | base = (void *) &ch->buffer[curr_buffer][CC_FIRST_ROW][0]; |
1568 | bytes_per_row = sizeof (ch->buffer[0][0]); |
1569 | |
1570 | old_max_rows = ch->curr_row + 1 - CC_FIRST_ROW; |
1571 | new_max_rows = new_base_row + 1 - CC_FIRST_ROW; |
1572 | copy_bytes = MIN (MIN (old_max_rows, new_max_rows), |
1573 | ch->window_rows) * bytes_per_row; |
1574 | |
1575 | if (new_base_row < ch->curr_row) { |
1576 | erase_begin = (new_base_row + 1) * bytes_per_row; |
1577 | erase_end = (ch->curr_row + 1) * bytes_per_row; |
1578 | |
1579 | memmove (base + erase_begin - copy_bytes, |
1580 | base + erase_end - copy_bytes, copy_bytes); |
1581 | |
1582 | ch->dirty[curr_buffer] >>= ch->curr_row - new_base_row; |
1583 | } else { |
1584 | erase_begin = (ch->curr_row + 1) * bytes_per_row |
1585 | - copy_bytes; |
1586 | erase_end = (new_base_row + 1) * bytes_per_row |
1587 | - copy_bytes; |
1588 | |
1589 | memmove (base + erase_end, |
1590 | base + erase_begin, copy_bytes); |
1591 | |
1592 | ch->dirty[curr_buffer] <<= new_base_row - ch->curr_row; |
1593 | ch->dirty[curr_buffer] &= CC_ALL_ROWS_MASK; |
1594 | } |
1595 | |
1596 | memset (base + erase_begin, 0, erase_end - erase_begin); |
1597 | |
1598 | cc_display_event (cd, ch, 0); |
1599 | } |
1600 | |
1601 | static void |
1602 | cc_preamble_address_code (struct cc_decoder * cd, |
1603 | struct cc_channel * ch, |
1604 | unsigned int c1, |
1605 | unsigned int c2) |
1606 | { |
1607 | unsigned int row; |
1608 | |
1609 | /* PAC Preamble Address Codes -- 001 crrr 1ri xxxu */ |
1610 | |
1611 | row = cc_pac_row_map[(c1 & 7) * 2 + ((c2 >> 5) & 1)]; |
1612 | if ((int) row < 0) |
1613 | return; |
1614 | |
1615 | switch (ch->mode) { |
1616 | case CC_MODE_UNKNOWN: |
1617 | return; |
1618 | |
1619 | case CC_MODE_ROLL_UP: |
1620 | /* EIA 608-B Annex C.4. */ |
1621 | if (ch->window_rows > row + 1) |
1622 | row = ch->window_rows - 1; |
1623 | |
1624 | /* 47 CFR Section 15.119 (f)(1)(ii). */ |
1625 | /* May send a display event. */ |
1626 | cc_move_window (cd, ch, row); |
1627 | |
1628 | ch->curr_row = row; |
1629 | |
1630 | break; |
1631 | |
1632 | case CC_MODE_PAINT_ON: |
1633 | cc_stream_event_if_changed (cd, ch); |
1634 | |
1635 | /* fall through */ |
1636 | |
1637 | case CC_MODE_POP_ON: |
1638 | /* XXX 47 CFR 15.119 (f)(2)(i), (f)(3)(i): In Pop-on |
1639 | and paint-on mode "Preamble Address Codes can be |
1640 | used to move the cursor around the screen in random |
1641 | order to place captions on Rows 1 to 15." We do not |
1642 | have a limit on the number of displayable rows, but |
1643 | as EIA 608-B Annex C.6 points out, if more than |
1644 | four rows must be displayed they were probably |
1645 | received in error and we should respond |
1646 | accordingly. */ |
1647 | |
1648 | /* 47 CFR Section 15.119 (d)(1)(i) and EIA 608-B Annex |
1649 | C.7. */ |
1650 | ch->curr_row = row; |
1651 | |
1652 | break; |
1653 | |
1654 | case CC_MODE_TEXT: |
1655 | /* 47 CFR 15.119 (e)(1) and EIA 608-B Section 7.4: |
1656 | Does not change the cursor row. */ |
1657 | break; |
1658 | } |
1659 | |
1660 | if (c2 & 0x10) { |
1661 | /* 47 CFR 15.119 (e)(1)(i) and EIA 608-B Table 71. */ |
1662 | ch->curr_column = CC_FIRST_COLUMN + (c2 & 0x0E) * 2; |
1663 | } |
1664 | |
1665 | /* PAC is a non-spacing attribute for the next character, see |
1666 | cc_put_char(). */ |
1667 | ch->last_pac = 0x1000 | c2; |
1668 | } |
1669 | |
1670 | static void |
1671 | cc_control_code (struct cc_decoder * cd, |
1672 | unsigned int c1, |
1673 | unsigned int c2, |
1674 | enum field_num f) |
1675 | { |
1676 | struct cc_channel *ch; |
1677 | unsigned int ch_num0; |
1678 | |
1679 | /* Caption / text, field 1 / 2, primary / secondary channel. */ |
1680 | ch_num0 = (((cd->curr_ch_num[f] - VBI_CAPTION_CC1) & 4) |
1681 | + f * 2 |
1682 | + ((c1 >> 3) & 1)); |
1683 | |
1684 | /* Note ch is invalid if UNKNOWN_CC_CHANNEL == |
1685 | cd->curr_ch_num[f]. */ |
1686 | ch = &cd->channel[ch_num0]; |
1687 | |
1688 | if (c2 >= 0x40) { |
1689 | /* Preamble Address Codes -- 001 crrr 1ri xxxu */ |
1690 | if (UNKNOWN_CC_CHANNEL != cd->curr_ch_num[f]) |
1691 | cc_preamble_address_code (cd, ch, c1, c2); |
1692 | return; |
1693 | } |
1694 | |
1695 | switch (c1 & 7) { |
1696 | case 0: |
1697 | if (UNKNOWN_CC_CHANNEL == cd->curr_ch_num[f] |
1698 | || CC_MODE_UNKNOWN == ch->mode) |
1699 | break; |
1700 | |
1701 | if (c2 < 0x30) { |
1702 | /* Backgr. Attr. Codes -- 001 c000 010 xxxt */ |
1703 | /* EIA 608-B Section 6.2. */ |
1704 | cc_put_char (cd, ch, 0x1000 | c2, |
1705 | /* displayable */ FALSE, |
1706 | /* backspace */ TRUE); |
1707 | } else { |
1708 | /* Undefined. */ |
1709 | } |
1710 | |
1711 | break; |
1712 | |
1713 | case 1: |
1714 | if (UNKNOWN_CC_CHANNEL == cd->curr_ch_num[f] |
1715 | || CC_MODE_UNKNOWN == ch->mode) |
1716 | break; |
1717 | |
1718 | if (c2 < 0x30) { |
1719 | /* Mid-Row Codes -- 001 c001 010 xxxu */ |
1720 | /* 47 CFR 15.119 (h)(1)(i): Spacing attribute. */ |
1721 | cc_put_char (cd, ch, 0x1100 | c2, |
1722 | /* displayable */ FALSE, |
1723 | /* backspace */ FALSE); |
1724 | } else { |
1725 | /* Special Characters -- 001 c001 011 xxxx */ |
1726 | if (0x39 == c2) { |
1727 | /* Transparent space. */ |
1728 | cc_put_char (cd, ch, 0, |
1729 | /* displayable */ FALSE, |
1730 | /* backspace */ FALSE); |
1731 | } else { |
1732 | cc_put_char (cd, ch, 0x1100 | c2, |
1733 | /* displayable */ TRUE, |
1734 | /* backspace */ FALSE); |
1735 | } |
1736 | } |
1737 | |
1738 | break; |
1739 | |
1740 | case 2: |
1741 | case 3: /* Extended Character Set -- 001 c01x 01x xxxx */ |
1742 | if (UNKNOWN_CC_CHANNEL == cd->curr_ch_num[f] |
1743 | || CC_MODE_UNKNOWN == ch->mode) |
1744 | break; |
1745 | |
1746 | /* EIA 608-B Section 6.4.2. */ |
1747 | cc_put_char (cd, ch, (c1 * 256 + c2) & 0x777F, |
1748 | /* displayable */ TRUE, |
1749 | /* backspace */ TRUE); |
1750 | break; |
1751 | |
1752 | case 4: |
1753 | case 5: |
1754 | if (c2 < 0x30) { |
1755 | /* Misc. Control Codes -- 001 c10f 010 xxxx */ |
1756 | cc_misc_control_code (cd, ch, c2, ch_num0, f); |
1757 | } else { |
1758 | /* Undefined. */ |
1759 | } |
1760 | |
1761 | break; |
1762 | |
1763 | case 6: /* reserved */ |
1764 | break; |
1765 | |
1766 | case 7: /* Extended control codes -- 001 c111 01x xxxx */ |
1767 | if (UNKNOWN_CC_CHANNEL == cd->curr_ch_num[f] |
1768 | || CC_MODE_UNKNOWN == ch->mode) |
1769 | break; |
1770 | |
1771 | cc_ext_control_code (cd, ch, c2); |
1772 | |
1773 | break; |
1774 | } |
1775 | } |
1776 | |
1777 | static vbi_bool |
1778 | cc_characters (struct cc_decoder * cd, |
1779 | struct cc_channel * ch, |
1780 | int c) |
1781 | { |
1782 | if (0 == c) { |
1783 | if (CC_MODE_UNKNOWN == ch->mode) |
1784 | return TRUE; |
1785 | |
1786 | /* XXX After x NUL characters (presumably a caption |
1787 | pause), force a display update if we do not send |
1788 | events on every display change. */ |
1789 | |
1790 | return TRUE; |
1791 | } |
1792 | |
1793 | if (c < 0x20) { |
1794 | /* Parity error or invalid data. */ |
1795 | |
1796 | if (c < 0 && CC_MODE_UNKNOWN != ch->mode) { |
1797 | /* 47 CFR Section 15.119 (j)(1). */ |
1798 | cc_put_char (cd, ch, 0x7F, |
1799 | /* displayable */ TRUE, |
1800 | /* backspace */ FALSE); |
1801 | } |
1802 | |
1803 | return FALSE; |
1804 | } |
1805 | |
1806 | if (CC_MODE_UNKNOWN != ch->mode) { |
1807 | cc_put_char (cd, ch, c, |
1808 | /* displayable */ TRUE, |
1809 | /* backspace */ FALSE); |
1810 | } |
1811 | |
1812 | return TRUE; |
1813 | } |
1814 | |
1815 | vbi_bool |
1816 | cc_feed (struct cc_decoder * cd, |
1817 | const uint8_t buffer[2], |
1818 | unsigned int line, |
1819 | const struct timeval * tv, |
1820 | int64_t pts) |
1821 | { |
1822 | int c1, c2; |
1823 | enum field_num f; |
1824 | vbi_bool all_successful; |
1825 | |
1826 | assert (NULL != cd); |
1827 | |
1828 | f = FIELD_1; |
1829 | |
1830 | switch (line) { |
1831 | case 21: /* NTSC */ |
1832 | case 22: /* PAL/SECAM */ |
1833 | break; |
1834 | |
1835 | case 284: /* NTSC */ |
1836 | f = FIELD_2; |
1837 | break; |
1838 | |
1839 | default: |
1840 | return FALSE; |
1841 | } |
1842 | |
1843 | cd->timestamp.sys = *tv; |
1844 | cd->timestamp.pts = pts; |
1845 | |
1846 | /* FIXME deferred reset here */ |
1847 | |
1848 | c1 = vbi_unpar8 (buffer[0]); |
1849 | c2 = vbi_unpar8 (buffer[1]); |
1850 | |
1851 | all_successful = TRUE; |
1852 | |
1853 | /* 47 CFR 15.119 (2)(i)(4): "If the first transmission of a |
1854 | control code pair passes parity, it is acted upon within |
1855 | one video frame. If the next frame contains a perfect |
1856 | repeat of the same pair, the redundant code is ignored. If, |
1857 | however, the next frame contains a different but also valid |
1858 | control code pair, this pair, too, will be acted upon (and |
1859 | the receiver will expect a repeat of this second pair in |
1860 | the next frame). If the first byte of the expected |
1861 | redundant control code pair fails the parity check and the |
1862 | second byte is identical to the second byte in the |
1863 | immediately preceding pair, then the expected redundant |
1864 | code is ignored. If there are printing characters in place |
1865 | of the redundant code, they will be processed normally." |
1866 | |
1867 | EIA 608-B Section 8.3: Caption control codes on field 2 may |
1868 | repeat as on field 1. Section 8.6.2: XDS control codes |
1869 | shall not repeat. */ |
1870 | |
1871 | if (unlikely (c1 < 0)) { |
1872 | goto parity_error; |
1873 | } else if (c1 == cd->expect_ctrl[f][0] |
1874 | && c2 == cd->expect_ctrl[f][1]) { |
1875 | /* Already acted upon. */ |
1876 | cd->expect_ctrl[f][0] = -1; |
1877 | goto finish; |
1878 | } |
1879 | |
1880 | if (c1 >= 0x10 && c1 < 0x20) { |
1881 | /* Caption control code. */ |
1882 | |
1883 | /* There's no XDS on field 1, we just |
1884 | use an array to save a branch. */ |
1885 | cd->in_xds[f] = FALSE; |
1886 | |
1887 | /* 47 CFR Section 15.119 (i)(1), (i)(2). */ |
1888 | if (c2 < 0x20) { |
1889 | /* Parity error or invalid control code. |
1890 | Let's hope it repeats. */ |
1891 | goto parity_error; |
1892 | } |
1893 | |
1894 | cc_control_code (cd, c1, c2, f); |
1895 | |
1896 | if (cd->event_pending) { |
1897 | cc_display_event (cd, cd->event_pending, 0); |
1898 | cd->event_pending = NULL; |
1899 | } |
1900 | |
1901 | cd->expect_ctrl[f][0] = c1; |
1902 | cd->expect_ctrl[f][1] = c2; |
1903 | } else { |
1904 | cd->expect_ctrl[f][0] = -1; |
1905 | |
1906 | if (c1 < 0x10) { |
1907 | if (FIELD_1 == f) { |
1908 | /* 47 CFR Section 15.119 (i)(1): "If the |
1909 | non-printing character in the pair is |
1910 | in the range 00h to 0Fh, that character |
1911 | alone will be ignored and the second |
1912 | character will be treated normally." */ |
1913 | c1 = 0; |
1914 | } else if (0x0F == c1) { |
1915 | /* XDS packet terminator. */ |
1916 | cd->in_xds[FIELD_2] = FALSE; |
1917 | goto finish; |
1918 | } else if (c1 >= 0x01) { |
1919 | /* XDS packet start or continuation. |
1920 | EIA 608-B Section 7.7, 8.5: Also |
1921 | interrupts a Text mode |
1922 | transmission. */ |
1923 | cd->in_xds[FIELD_2] = TRUE; |
1924 | goto finish; |
1925 | } |
1926 | } |
1927 | |
1928 | { |
1929 | struct cc_channel *ch; |
1930 | vbi_pgno ch_num; |
1931 | |
1932 | ch_num = cd->curr_ch_num[f]; |
1933 | if (UNKNOWN_CC_CHANNEL == ch_num) |
1934 | goto finish; |
1935 | |
1936 | ch_num = ((ch_num - VBI_CAPTION_CC1) & 5) + f * 2; |
1937 | ch = &cd->channel[ch_num]; |
1938 | |
1939 | all_successful &= cc_characters (cd, ch, c1); |
1940 | all_successful &= cc_characters (cd, ch, c2); |
1941 | |
1942 | if (cd->event_pending) { |
1943 | cc_display_event (cd, cd->event_pending, 0); |
1944 | cd->event_pending = NULL; |
1945 | } |
1946 | } |
1947 | } |
1948 | |
1949 | finish: |
1950 | cd->error_history = cd->error_history * 2 + all_successful; |
1951 | |
1952 | return all_successful; |
1953 | |
1954 | parity_error: |
1955 | cd->expect_ctrl[f][0] = -1; |
1956 | |
1957 | /* XXX Some networks stupidly transmit 0x0000 instead of |
1958 | 0x8080 as filler. Perhaps we shouldn't take that as a |
1959 | serious parity error. */ |
1960 | cd->error_history *= 2; |
1961 | |
1962 | return FALSE; |
1963 | } |
1964 | |
1965 | void |
1966 | cc_reset (struct cc_decoder * cd) |
1967 | { |
1968 | unsigned int ch_num; |
1969 | |
1970 | assert (NULL != cd); |
1971 | |
1972 | for (ch_num = 0; ch_num < MAX_CC_CHANNELS; ++ch_num) { |
1973 | struct cc_channel *ch; |
1974 | |
1975 | ch = &cd->channel[ch_num]; |
1976 | |
1977 | if (ch_num <= 3) { |
1978 | ch->mode = CC_MODE_UNKNOWN; |
1979 | |
1980 | /* Something suitable for roll-up mode. */ |
1981 | ch->curr_row = CC_LAST_ROW; |
1982 | ch->curr_column = CC_FIRST_COLUMN; |
1983 | ch->window_rows = 4; |
1984 | } else { |
1985 | ch->mode = CC_MODE_TEXT; /* invariable */ |
1986 | |
1987 | /* EIA 608-B Section 7.4: "When Text Mode has |
1988 | initially been selected and the specified |
1989 | Text memory is empty, the cursor starts at |
1990 | the topmost row, Column 1." */ |
1991 | ch->curr_row = CC_FIRST_ROW; |
1992 | ch->curr_column = CC_FIRST_COLUMN; |
1993 | ch->window_rows = 0; /* n/a */ |
1994 | } |
1995 | |
1996 | ch->displayed_buffer = 0; |
1997 | |
1998 | ch->last_pac = 0; |
1999 | |
2000 | CLEAR (ch->buffer); |
2001 | CLEAR (ch->dirty); |
2002 | |
2003 | cc_timestamp_reset (&ch->timestamp); |
2004 | cc_timestamp_reset (&ch->timestamp_c0); |
2005 | } |
2006 | |
2007 | cd->curr_ch_num[0] = UNKNOWN_CC_CHANNEL; |
2008 | cd->curr_ch_num[1] = UNKNOWN_CC_CHANNEL; |
2009 | |
2010 | memset (cd->expect_ctrl, -1, sizeof (cd->expect_ctrl)); |
2011 | |
2012 | CLEAR (cd->in_xds); |
2013 | |
2014 | cd->event_pending = NULL; |
2015 | } |
2016 | |
2017 | void |
2018 | cc_init (struct cc_decoder * cd) |
2019 | { |
2020 | cc_reset (cd); |
2021 | |
2022 | cd->error_history = 0; |
2023 | |
2024 | cc_timestamp_reset (&cd->timestamp); |
2025 | } |
2026 | |
2027 | #if 0 /* to be replaced */ |
2028 | |
2029 | static int webtv_check(struct caption_recorder *cr, char * buf,int len) |
2030 | { |
2031 | unsigned long sum; |
2032 | unsigned long nwords; |
2033 | unsigned short csum=0; |
2034 | char temp[9]; |
2035 | int nbytes=0; |
2036 | |
2037 | while (buf[0]!='<' && len > 6) //search for the start |
2038 | { |
2039 | buf++; len--; |
2040 | } |
2041 | |
2042 | if (len == 6) //failure to find start |
2043 | return 0; |
2044 | |
2045 | |
2046 | while (nbytes+6 <= len) |
2047 | { |
2048 | //look for end of object checksum, it's enclosed in []'s and there shouldn't be any [' after |
2049 | if (buf[nbytes] == '[' && buf[nbytes+5] == ']' && buf[nbytes+6] != '[') |
2050 | break; |
2051 | else |
2052 | nbytes++; |
2053 | } |
2054 | if (nbytes+6>len) //failure to find end |
2055 | return 0; |
2056 | |
2057 | nwords = nbytes >> 1; sum = 0; |
2058 | |
2059 | //add up all two byte words |
2060 | while (nwords-- > 0) { |
2061 | sum += *buf++ << 8; |
2062 | sum += *buf++; |
2063 | } |
2064 | if (nbytes & 1) { |
2065 | sum += *buf << 8; |
2066 | } |
2067 | csum = (unsigned short)(sum >> 16); |
2068 | while(csum !=0) { |
2069 | sum = csum + (sum & 0xffff); |
2070 | csum = (unsigned short)(sum >> 16); |
2071 | } |
2072 | sprintf(temp,"%04X\n",(int)~sum&0xffff); |
2073 | buf++; |
2074 | if (!strncmp(buf,temp,4)) |
2075 | { |
2076 | buf[5]=0; |
2077 | if (cr->cur_ch[cr->field] >= 0 && cr->cc_fp[cr->cur_ch[cr->field]]) { |
2078 | if (!cr->plain) |
2079 | fprintf(cr->cc_fp[cr->cur_ch[cr->field]], "\33[35mWEBTV: %s\33[0m\n",buf-nbytes-1); |
2080 | else |
2081 | fprintf(cr->cc_fp[cr->cur_ch[cr->field]], "WEBTV: %s\n",buf-nbytes-1); |
2082 | fflush (cr->cc_fp[cr->cur_ch[cr->field]]); |
2083 | } |
2084 | } |
2085 | return 0; |
2086 | } |
2087 | |
2088 | #endif /* 0 */ |
2089 | |
2090 | /* CEA 708-C Digital TV Closed Caption decoder. */ |
2091 | |
2092 | static const uint8_t |
2093 | dtvcc_c0_length [4] = { |
2094 | 1, 1, 2, 3 |
2095 | }; |
2096 | |
2097 | static const uint8_t |
2098 | dtvcc_c1_length [32] = { |
2099 | /* 0x80 CW0 ... CW7 */ 1, 1, 1, 1, 1, 1, 1, 1, |
2100 | /* 0x88 CLW */ 2, |
2101 | /* 0x89 DSW */ 2, |
2102 | /* 0x8A HDW */ 2, |
2103 | /* 0x8B TGW */ 2, |
2104 | |
2105 | /* 0x8C DLW */ 2, |
2106 | /* 0x8D DLY */ 2, |
2107 | /* 0x8E DLC */ 1, |
2108 | /* 0x8F RST */ 1, |
2109 | |
2110 | /* 0x90 SPA */ 3, |
2111 | /* 0x91 SPC */ 4, |
2112 | /* 0x92 SPL */ 3, |
2113 | /* CEA 708-C Section 7.1.5.1: 0x93 ... 0x96 are |
2114 | reserved one byte codes. */ 1, 1, 1, 1, |
2115 | /* 0x97 SWA */ 5, |
2116 | /* 0x98 DF0 ... DF7 */ 7, 7, 7, 7, 7, 7, 7, 7 |
2117 | }; |
2118 | |
2119 | static const uint16_t |
2120 | dtvcc_g2 [96] = { |
2121 | /* Note Unicode defines no transparent spaces. */ |
2122 | 0x0020, /* 0x1020 Transparent space */ |
2123 | 0x00A0, /* 0x1021 Non-breaking transparent space */ |
2124 | |
2125 | 0, /* 0x1022 reserved */ |
2126 | 0, |
2127 | 0, |
2128 | 0x2026, /* 0x1025 Horizontal ellipsis */ |
2129 | 0, |
2130 | 0, |
2131 | 0, |
2132 | 0, |
2133 | 0x0160, /* 0x102A S with caron */ |
2134 | 0, |
2135 | 0x0152, /* 0x102C Ligature OE */ |
2136 | 0, |
2137 | 0, |
2138 | 0, |
2139 | |
2140 | /* CEA 708-C Section 7.1.8: "The character (0x30) is a solid |
2141 | block which fills the entire character position with the |
2142 | text foreground color." */ |
2143 | 0x2588, /* 0x1030 Full block */ |
2144 | |
2145 | 0x2018, /* 0x1031 Left single quotation mark */ |
2146 | 0x2019, /* 0x1032 Right single quotation mark */ |
2147 | 0x201C, /* 0x1033 Left double quotation mark */ |
2148 | 0x201D, /* 0x1034 Right double quotation mark */ |
2149 | 0x2022, |
2150 | 0, |
2151 | 0, |
2152 | 0, |
2153 | 0x2122, /* 0x1039 Trademark sign */ |
2154 | 0x0161, /* 0x103A s with caron */ |
2155 | 0, |
2156 | 0x0153, /* 0x103C Ligature oe */ |
2157 | 0x2120, /* 0x103D Service mark */ |
2158 | 0, |
2159 | 0x0178, /* 0x103F Y with diaeresis */ |
2160 | |
2161 | /* Code points 0x1040 ... 0x106F reserved. */ |
2162 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
2163 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
2164 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
2165 | |
2166 | 0, /* 0x1070 reserved */ |
2167 | 0, |
2168 | 0, |
2169 | 0, |
2170 | 0, |
2171 | 0, |
2172 | 0x215B, /* 0x1076 1/8 */ |
2173 | 0x215C, /* 0x1077 3/8 */ |
2174 | 0x215D, /* 0x1078 5/8 */ |
2175 | 0x215E, /* 0x1079 7/8 */ |
2176 | 0x2502, /* 0x107A Box drawings vertical */ |
2177 | 0x2510, /* 0x107B Box drawings down and left */ |
2178 | 0x2514, /* 0x107C Box drawings up and right */ |
2179 | 0x2500, /* 0x107D Box drawings horizontal */ |
2180 | 0x2518, /* 0x107E Box drawings up and left */ |
2181 | 0x250C /* 0x107F Box drawings down and right */ |
2182 | }; |
2183 | |
2184 | static vbi_rgba |
2185 | dtvcc_color_map[8] = { |
2186 | VBI_RGBA(0x00, 0x00, 0x00), VBI_RGBA(0xFF, 0x00, 0x00), |
2187 | VBI_RGBA(0x00, 0xFF, 0x00), VBI_RGBA(0xFF, 0xFF, 0x00), |
2188 | VBI_RGBA(0x00, 0x00, 0xFF), VBI_RGBA(0xFF, 0x00, 0xFF), |
2189 | VBI_RGBA(0x00, 0xFF, 0xFF), VBI_RGBA(0xFF, 0xFF, 0xFF) |
2190 | }; |
2191 | |
2192 | |
2193 | static void |
2194 | dtvcc_set_page_color_map(vbi_decoder *vbi, vbi_page *pg) |
2195 | { |
2196 | vbi_transp_colormap(vbi, pg->color_map, dtvcc_color_map, 8); |
2197 | } |
2198 | |
2199 | static vbi_color |
2200 | dtvcc_map_color(dtvcc_color c) |
2201 | { |
2202 | vbi_color ret = VBI_BLACK; |
2203 | |
2204 | c &= 0x2A; |
2205 | switch(c){ |
2206 | case 0: |
2207 | ret = VBI_BLACK; |
2208 | break; |
2209 | case 0x20: |
2210 | ret = VBI_RED; |
2211 | break; |
2212 | case 0x08: |
2213 | ret = VBI_GREEN; |
2214 | break; |
2215 | case 0x28: |
2216 | ret = VBI_YELLOW; |
2217 | break; |
2218 | case 0x02: |
2219 | ret = VBI_BLUE; |
2220 | break; |
2221 | case 0x22: |
2222 | ret = VBI_MAGENTA; |
2223 | break; |
2224 | case 0x0A: |
2225 | ret = VBI_CYAN; |
2226 | break; |
2227 | case 0x2A: |
2228 | ret = VBI_WHITE; |
2229 | break; |
2230 | } |
2231 | |
2232 | return ret; |
2233 | } |
2234 | |
2235 | unsigned int |
2236 | dtvcc_unicode (unsigned int c) |
2237 | { |
2238 | if (unlikely (0 == (c & 0x60))) { |
2239 | /* C0, C1, C2, C3 */ |
2240 | return 0; |
2241 | } else if (likely (c < 0x100)) { |
2242 | /* G0, G1 */ |
2243 | if (unlikely (0x7F == c)) |
2244 | return 0x266A; /* music note */ |
2245 | else |
2246 | return c; |
2247 | } else if (c < 0x1080) { |
2248 | if (unlikely (c < 0x1020)) |
2249 | return 0; |
2250 | else |
2251 | return dtvcc_g2[c - 0x1020]; |
2252 | } else if (0x10A0 == c) { |
2253 | /* We map all G2/G3 characters which are not |
2254 | representable in Unicode to private code U+E900 |
2255 | ... U+E9FF. */ |
2256 | return 0xf101; /* caption icon */ |
2257 | } |
2258 | |
2259 | return 0; |
2260 | } |
2261 | |
2262 | static void |
2263 | dtvcc_render(struct dtvcc_decoder * dc, struct dtvcc_service * ds) |
2264 | { |
2265 | #if 0 |
2266 | vbi_event event; |
2267 | struct tvcc_decoder *td = PARENT(dc, struct tvcc_decoder, dtvcc); |
2268 | struct dtvcc_window *win[8]; |
2269 | int i, cnt; |
2270 | |
2271 | //printf("render check\n"); |
2272 | |
2273 | cnt = 8; |
2274 | dtvcc_get_visible_windows(ds, &cnt, win); |
2275 | //if (!cnt) |
2276 | // return; |
2277 | |
2278 | if (cnt != ds->old_win_cnt) { |
2279 | //printf("cnt changed\n"); |
2280 | goto changed; |
2281 | } |
2282 | |
2283 | for (i = 0; i < cnt; i ++) { |
2284 | struct dtvcc_window *w1 = win[i]; |
2285 | struct dtvcc_window *w2 = &ds->old_window[i]; |
2286 | |
2287 | if (memcmp(w1->buffer, w2->buffer, sizeof(w1->buffer))) { |
2288 | //printf("text changed\n"); |
2289 | goto changed; |
2290 | } |
2291 | |
2292 | if (memcmp(&w1->style, &w2->style, sizeof(w1->style))) { |
2293 | //printf("style changed\n"); |
2294 | goto changed; |
2295 | } |
2296 | |
2297 | if (memcmp(&w1->curr_pen, &w2->curr_pen, sizeof(w1->curr_pen))) { |
2298 | //printf("pen changed\n"); |
2299 | goto changed; |
2300 | } |
2301 | |
2302 | if (w1->row_count != w2->row_count) { |
2303 | //printf("row changed\n"); |
2304 | goto changed; |
2305 | } |
2306 | |
2307 | if (w1->column_count != w2->column_count) { |
2308 | //printf("col changed\n"); |
2309 | goto changed; |
2310 | } |
2311 | |
2312 | if (w1->visible != w2->visible) { |
2313 | //printf("vis changed\n"); |
2314 | goto changed; |
2315 | } |
2316 | } |
2317 | |
2318 | return; |
2319 | changed: |
2320 | for (i = 0; i < cnt; i ++) { |
2321 | ds->old_window[i] = *win[i]; |
2322 | } |
2323 | ds->old_win_cnt = cnt; |
2324 | #endif |
2325 | ds->update = 1; |
2326 | #if 0 |
2327 | |
2328 | event.type = VBI_EVENT_CAPTION; |
2329 | event.ev.caption.pgno = ds - dc->service + 1 + 8/*after 8 cc channels*/; |
2330 | |
2331 | /* Permits calling tvcc_fetch_page from handler */ |
2332 | pthread_mutex_unlock(&td->mutex); |
2333 | |
2334 | vbi_send_event(td->vbi, &event); |
2335 | |
2336 | pthread_mutex_lock(&td->mutex); |
2337 | #endif |
2338 | } |
2339 | |
2340 | static void |
2341 | dtvcc_reset_service (struct dtvcc_service * ds); |
2342 | |
2343 | static unsigned int |
2344 | dtvcc_window_id (struct dtvcc_service * ds, |
2345 | struct dtvcc_window * dw) |
2346 | { |
2347 | return dw - ds->window; |
2348 | } |
2349 | |
2350 | static unsigned int |
2351 | dtvcc_service_num (struct dtvcc_decoder * dc, |
2352 | struct dtvcc_service * ds) |
2353 | { |
2354 | return ds - dc->service + 1; |
2355 | } |
2356 | |
2357 | /* Up to eight windows can be visible at once, so which one displays |
2358 | the caption? Let's take a guess. */ |
2359 | static struct dtvcc_window * |
2360 | dtvcc_caption_window (struct dtvcc_service * ds) |
2361 | { |
2362 | struct dtvcc_window *dw; |
2363 | unsigned int max_priority; |
2364 | unsigned int window_id; |
2365 | |
2366 | dw = NULL; |
2367 | max_priority = 8; |
2368 | |
2369 | for (window_id = 0; window_id < 8; ++window_id) { |
2370 | if (0 == (ds->created & (1 << window_id))) |
2371 | continue; |
2372 | if (!ds->window[window_id].visible) |
2373 | continue; |
2374 | if (DIR_BOTTOM_TOP |
2375 | != ds->window[window_id].style.scroll_direction) |
2376 | continue; |
2377 | if (ds->window[window_id].priority < max_priority) { |
2378 | dw = &ds->window[window_id]; |
2379 | max_priority = ds->window[window_id].priority; |
2380 | } |
2381 | } |
2382 | |
2383 | return dw; |
2384 | } |
2385 | |
2386 | static void |
2387 | dtvcc_stream_event (struct dtvcc_decoder * dc, |
2388 | struct dtvcc_service * ds, |
2389 | struct dtvcc_window * dw, |
2390 | unsigned int row) |
2391 | { |
2392 | vbi_char text[48]; |
2393 | vbi_char ac; |
2394 | unsigned int column; |
2395 | |
2396 | if (NULL == dw || dw != dtvcc_caption_window (ds)) |
2397 | return; |
2398 | |
2399 | /* Note we only stream windows with scroll direction |
2400 | upwards. */ |
2401 | if (0 != (dw->streamed & (1 << row)) |
2402 | /* || !cc_timestamp_isset (&dw->timestamp_c0)*/) |
2403 | return; |
2404 | |
2405 | dw->streamed |= 1 << row; |
2406 | |
2407 | for (column = 0; column < dw->column_count; ++column) { |
2408 | if (0 != dw->buffer[row][column]) |
2409 | break; |
2410 | } |
2411 | |
2412 | /* Row contains only transparent spaces. */ |
2413 | if (column >= dw->column_count) |
2414 | return; |
2415 | |
2416 | |
2417 | dtvcc_render(dc, ds); |
2418 | |
2419 | /* TO DO. */ |
2420 | CLEAR (ac); |
2421 | ac.foreground = VBI_WHITE; |
2422 | ac.background = VBI_BLACK; |
2423 | ac.opacity = VBI_OPAQUE; |
2424 | |
2425 | for (column = 0; column < dw->column_count; ++column) { |
2426 | unsigned int c; |
2427 | |
2428 | c = dw->buffer[row][column]; |
2429 | if (0 == c) { |
2430 | ac.unicode = 0x20; |
2431 | } else { |
2432 | ac.unicode = dtvcc_unicode (c); |
2433 | if (0 == ac.unicode) { |
2434 | ac.unicode = 0x20; |
2435 | } |
2436 | } |
2437 | text[column] = ac; |
2438 | } |
2439 | |
2440 | cc_timestamp_reset (&dw->timestamp_c0); |
2441 | } |
2442 | |
2443 | static vbi_bool |
2444 | dtvcc_put_char (struct dtvcc_decoder * dc, |
2445 | struct dtvcc_service * ds, |
2446 | unsigned int c) |
2447 | { |
2448 | struct dtvcc_window *dw; |
2449 | unsigned int row; |
2450 | unsigned int column,i; |
2451 | |
2452 | dc = dc; /* unused */ |
2453 | |
2454 | dw = ds->curr_window; |
2455 | |
2456 | //printf("putchar %c\n", c); |
2457 | |
2458 | if (NULL == dw) { |
2459 | ds->error_line = __LINE__; |
2460 | //AM_DEBUG(1, "================ window null !!!!!"); |
2461 | return FALSE; |
2462 | } |
2463 | |
2464 | row = dw->curr_row; |
2465 | column = dw->curr_column; |
2466 | |
2467 | /* FIXME how should we handle TEXT_TAG_NOT_DISPLAYABLE? */ |
2468 | |
2469 | dw->buffer[row][column] = c; |
2470 | dw->pen[row][column] = dw->curr_pen.style; |
2471 | //AM_DEBUG(1, "========= putchar %x %c", c, c); |
2472 | if (dw->visible) |
2473 | dtvcc_render(dc, ds); |
2474 | |
2475 | switch (dw->style.print_direction) { |
2476 | case DIR_LEFT_RIGHT: |
2477 | dw->streamed &= ~(1 << row); |
2478 | if (!cc_timestamp_isset (&dw->timestamp_c0)) |
2479 | dw->timestamp_c0 = ds->timestamp; |
2480 | if (++column >= dw->column_count) |
2481 | return TRUE; |
2482 | break; |
2483 | |
2484 | case DIR_RIGHT_LEFT: |
2485 | dw->streamed &= ~(1 << row); |
2486 | if (!cc_timestamp_isset (&dw->timestamp_c0)) |
2487 | dw->timestamp_c0 = ds->timestamp; |
2488 | if (column-- <= 0) |
2489 | return TRUE; |
2490 | break; |
2491 | |
2492 | case DIR_TOP_BOTTOM: |
2493 | dw->streamed &= ~(1 << column); |
2494 | if (!cc_timestamp_isset (&dw->timestamp_c0)) |
2495 | dw->timestamp_c0 = ds->timestamp; |
2496 | if (++row >= dw->row_count) |
2497 | return TRUE; |
2498 | break; |
2499 | |
2500 | case DIR_BOTTOM_TOP: |
2501 | dw->streamed &= ~(1 << column); |
2502 | if (!cc_timestamp_isset (&dw->timestamp_c0)) |
2503 | dw->timestamp_c0 = ds->timestamp; |
2504 | if (row-- <= 0) |
2505 | return TRUE; |
2506 | break; |
2507 | } |
2508 | |
2509 | dw->curr_row = row; |
2510 | dw->curr_column = column; |
2511 | |
2512 | |
2513 | return TRUE; |
2514 | } |
2515 | |
2516 | static vbi_bool |
2517 | dtvcc_set_pen_location (struct dtvcc_decoder * dc, |
2518 | struct dtvcc_service * ds, |
2519 | const uint8_t * buf) |
2520 | { |
2521 | struct dtvcc_window *dw; |
2522 | unsigned int row; |
2523 | unsigned int column; |
2524 | |
2525 | dw = ds->curr_window; |
2526 | if (NULL == dw) { |
2527 | ds->error_line = __LINE__; |
2528 | return FALSE; |
2529 | } |
2530 | |
2531 | row = buf[1]; |
2532 | /* We check the top four zero bits. */ |
2533 | if (row >= 16) { |
2534 | ds->error_line = __LINE__; |
2535 | return FALSE; |
2536 | } |
2537 | |
2538 | column = buf[2]; |
2539 | /* We also check the top two zero bits. */ |
2540 | if (column >= 42) { |
2541 | ds->error_line = __LINE__; |
2542 | return FALSE; |
2543 | } |
2544 | |
2545 | if (row > dw->row_count) |
2546 | row = dw->row_count - 1; |
2547 | if (column > dw->column_count) |
2548 | column = dw->column_count - 1; |
2549 | |
2550 | if (row != dw->curr_row) { |
2551 | dtvcc_stream_event (dc, ds, dw, dw->curr_row); |
2552 | } |
2553 | |
2554 | /* FIXME there's more. */ |
2555 | dw->curr_row = row; |
2556 | dw->curr_column = column; |
2557 | |
2558 | return TRUE; |
2559 | } |
2560 | |
2561 | static vbi_bool |
2562 | dtvcc_set_pen_color (struct dtvcc_service * ds, |
2563 | const uint8_t * buf) |
2564 | { |
2565 | struct dtvcc_window *dw; |
2566 | unsigned int c; |
2567 | |
2568 | dw = ds->curr_window; |
2569 | if (NULL == dw) { |
2570 | ds->error_line = __LINE__; |
2571 | return FALSE; |
2572 | } |
2573 | |
2574 | c = buf[3]; |
2575 | if (0 != (c & 0xC0)) { |
2576 | ds->error_line = __LINE__; |
2577 | return FALSE; |
2578 | } |
2579 | |
2580 | dw->curr_pen.style.edge_color = c; |
2581 | c = buf[1]; |
2582 | dw->curr_pen.style.fg_opacity = c >> 6; |
2583 | dw->curr_pen.style.fg_color = c & 0x3F; |
2584 | dw->curr_pen.style.fg_flash = ((c>>6)==1)?1:0; |
2585 | c = buf[2]; |
2586 | dw->curr_pen.style.bg_opacity = c >> 6; |
2587 | dw->curr_pen.style.bg_color = c & 0x3F; |
2588 | dw->curr_pen.style.bg_flash = ((c>>6)==1)?1:0; |
2589 | |
2590 | return TRUE; |
2591 | } |
2592 | |
2593 | static vbi_bool |
2594 | dtvcc_set_pen_attributes (struct dtvcc_service * ds, |
2595 | const uint8_t * buf) |
2596 | { |
2597 | struct dtvcc_window *dw; |
2598 | unsigned int c; |
2599 | enum pen_size pen_size; |
2600 | enum offset offset; |
2601 | enum edge edge_type; |
2602 | |
2603 | dw = ds->curr_window; |
2604 | if (NULL == dw) { |
2605 | ds->error_line = __LINE__; |
2606 | return FALSE; |
2607 | } |
2608 | |
2609 | c = buf[1]; |
2610 | offset = (c >> 2) & 3; |
2611 | pen_size = c & 3; |
2612 | //TODO: why not larger than 3 |
2613 | /* |
2614 | if ((offset | pen_size) >= 3) { |
2615 | ds->error_line = __LINE__; |
2616 | return FALSE; |
2617 | } */ |
2618 | |
2619 | c = buf[2]; |
2620 | edge_type = (c >> 3) & 7; |
2621 | if (edge_type >= 6) { |
2622 | ds->error_line = __LINE__; |
2623 | return FALSE; |
2624 | } |
2625 | |
2626 | c = buf[1]; |
2627 | dw->curr_pen.text_tag = c >> 4; |
2628 | dw->curr_pen.style.offset = offset; |
2629 | dw->curr_pen.style.pen_size = pen_size; |
2630 | c = buf[2]; |
2631 | dw->curr_pen.style.italics = c >> 7; |
2632 | dw->curr_pen.style.underline = (c >> 6) & 1; |
2633 | dw->curr_pen.style.edge_type = edge_type; |
2634 | dw->curr_pen.style.font_style = c & 7; |
2635 | |
2636 | return TRUE; |
2637 | } |
2638 | |
2639 | static vbi_bool |
2640 | dtvcc_set_window_attributes (struct dtvcc_service * ds, |
2641 | const uint8_t * buf) |
2642 | { |
2643 | struct dtvcc_window *dw; |
2644 | unsigned int c; |
2645 | enum edge border_type; |
2646 | enum display_effect display_effect; |
2647 | |
2648 | dw = ds->curr_window; |
2649 | if (NULL == dw) |
2650 | return FALSE; |
2651 | |
2652 | c = buf[2]; |
2653 | border_type = ((buf[3] >> 5) & 0x04) | (c >> 6); |
2654 | if (border_type >= 6) |
2655 | return FALSE; |
2656 | |
2657 | c = buf[4]; |
2658 | display_effect = c & 3; |
2659 | if (display_effect >= 3) |
2660 | return FALSE; |
2661 | |
2662 | c = buf[1]; |
2663 | dw->style.fill_opacity = c >> 6; |
2664 | dw->style.fill_color = c & 0x3F; |
2665 | dw->style.window_flash = ((c>>6)==1)?1:0; |
2666 | c = buf[2]; |
2667 | dw->style.border_type = border_type; |
2668 | dw->style.border_color = c & 0x3F; |
2669 | c = buf[3]; |
2670 | dw->style.wordwrap = (c >> 6) & 1; |
2671 | dw->style.print_direction = (c >> 4) & 3; |
2672 | dw->style.scroll_direction = (c >> 2) & 3; |
2673 | dw->style.justify = c & 3; |
2674 | c = buf[4]; |
2675 | dw->style.effect_speed = c >> 4; |
2676 | dw->style.effect_direction = (c >> 2) & 3; |
2677 | dw->style.display_effect = display_effect; |
2678 | |
2679 | return TRUE; |
2680 | } |
2681 | |
2682 | static vbi_bool |
2683 | dtvcc_clear_windows (struct dtvcc_decoder * dc, |
2684 | struct dtvcc_service * ds, |
2685 | dtvcc_window_map window_map) |
2686 | { |
2687 | unsigned int i; |
2688 | |
2689 | window_map &= ds->created; |
2690 | |
2691 | for (i = 0; i < 8; ++i) { |
2692 | struct dtvcc_window *dw; |
2693 | |
2694 | if (0 == (window_map & (1 << i))) |
2695 | continue; |
2696 | |
2697 | dw = &ds->window[i]; |
2698 | |
2699 | dtvcc_stream_event (dc, ds, dw, dw->curr_row); |
2700 | |
2701 | memset (dw->buffer, 0, sizeof (dw->buffer)); |
2702 | memset (dw->pen, 0, sizeof(dw->pen)); |
2703 | |
2704 | dw->streamed = 0; |
2705 | |
2706 | if (dw->visible) |
2707 | dtvcc_render(dc, ds); |
2708 | |
2709 | /* FIXME CEA 708-C Section 7.1.4 (Form Feed) |
2710 | and 8.10.5.3 confuse me. */ |
2711 | if (0) { |
2712 | dw->curr_column = 0; |
2713 | dw->curr_row = 0; |
2714 | } |
2715 | } |
2716 | |
2717 | return TRUE; |
2718 | } |
2719 | |
2720 | static vbi_bool |
2721 | dtvcc_define_window (struct dtvcc_decoder * dc, |
2722 | struct dtvcc_service * ds, |
2723 | uint8_t * buf) |
2724 | { |
2725 | static const struct dtvcc_window_style window_styles [7] = { |
2726 | { |
2727 | JUSTIFY_LEFT, DIR_LEFT_RIGHT, DIR_BOTTOM_TOP, |
2728 | FALSE, DISPLAY_EFFECT_SNAP, 0, 0, 0, |
2729 | OPACITY_SOLID, EDGE_NONE, 0 |
2730 | }, { |
2731 | JUSTIFY_LEFT, DIR_LEFT_RIGHT, DIR_BOTTOM_TOP, |
2732 | FALSE, DISPLAY_EFFECT_SNAP, 0, 0, 0, |
2733 | OPACITY_TRANSPARENT, EDGE_NONE, 0 |
2734 | }, { |
2735 | JUSTIFY_CENTER, DIR_LEFT_RIGHT, DIR_BOTTOM_TOP, |
2736 | FALSE, DISPLAY_EFFECT_SNAP, 0, 0, 0, |
2737 | OPACITY_SOLID, EDGE_NONE, 0 |
2738 | }, { |
2739 | JUSTIFY_LEFT, DIR_LEFT_RIGHT, DIR_BOTTOM_TOP, |
2740 | TRUE, DISPLAY_EFFECT_SNAP, 0, 0, 0, |
2741 | OPACITY_SOLID, EDGE_NONE, 0 |
2742 | }, { |
2743 | JUSTIFY_LEFT, DIR_LEFT_RIGHT, DIR_BOTTOM_TOP, |
2744 | TRUE, DISPLAY_EFFECT_SNAP, 0, 0, 0, |
2745 | OPACITY_TRANSPARENT, EDGE_NONE, 0 |
2746 | }, { |
2747 | JUSTIFY_CENTER, DIR_LEFT_RIGHT, DIR_BOTTOM_TOP, |
2748 | TRUE, DISPLAY_EFFECT_SNAP, 0, 0, 0, |
2749 | OPACITY_SOLID, EDGE_NONE, 0 |
2750 | }, { |
2751 | JUSTIFY_LEFT, DIR_TOP_BOTTOM, DIR_RIGHT_LEFT, |
2752 | FALSE, DISPLAY_EFFECT_SNAP, 0, 0, 0, |
2753 | OPACITY_SOLID, EDGE_NONE, 0 |
2754 | } |
2755 | }; |
2756 | static const struct dtvcc_pen_style pen_styles [7] = { |
2757 | { |
2758 | PEN_SIZE_STANDARD, 0, OFFSET_NORMAL, FALSE, |
2759 | FALSE, EDGE_NONE, 0x3F, OPACITY_SOLID, |
2760 | 0x00, OPACITY_SOLID, 0 |
2761 | }, { |
2762 | PEN_SIZE_STANDARD, 1, OFFSET_NORMAL, FALSE, |
2763 | FALSE, EDGE_NONE, 0x3F, OPACITY_SOLID, |
2764 | 0x00, OPACITY_SOLID, 0 |
2765 | }, { |
2766 | PEN_SIZE_STANDARD, 2, OFFSET_NORMAL, FALSE, |
2767 | FALSE, EDGE_NONE, 0x3F, OPACITY_SOLID, |
2768 | 0x00, OPACITY_SOLID, 0 |
2769 | }, { |
2770 | PEN_SIZE_STANDARD, 3, OFFSET_NORMAL, FALSE, |
2771 | FALSE, EDGE_NONE, 0x3F, OPACITY_SOLID, |
2772 | 0x00, OPACITY_SOLID, 0 |
2773 | }, { |
2774 | PEN_SIZE_STANDARD, 4, OFFSET_NORMAL, FALSE, |
2775 | FALSE, EDGE_NONE, 0x3F, OPACITY_SOLID, |
2776 | 0x00, OPACITY_SOLID, 0 |
2777 | }, { |
2778 | PEN_SIZE_STANDARD, 3, OFFSET_NORMAL, FALSE, |
2779 | FALSE, EDGE_UNIFORM, 0x3F, OPACITY_SOLID, |
2780 | 0, OPACITY_TRANSPARENT, 0x00 |
2781 | }, { |
2782 | PEN_SIZE_STANDARD, 4, OFFSET_NORMAL, FALSE, |
2783 | FALSE, EDGE_UNIFORM, 0x3F, OPACITY_SOLID, |
2784 | 0, OPACITY_TRANSPARENT, 0x00 |
2785 | } |
2786 | }; |
2787 | struct dtvcc_window *dw; |
2788 | dtvcc_window_map window_map; |
2789 | vbi_bool anchor_relative; |
2790 | unsigned int anchor_vertical; |
2791 | unsigned int anchor_horizontal; |
2792 | unsigned int anchor_point; |
2793 | unsigned int column_count_m1; |
2794 | unsigned int window_id; |
2795 | unsigned int window_style_id; |
2796 | unsigned int pen_style_id; |
2797 | unsigned int c; |
2798 | |
2799 | //printf("define window\n"); |
2800 | |
2801 | if (0 != ((buf[1] | buf[6]) & 0xC0)) { |
2802 | ds->error_line = __LINE__; |
2803 | return FALSE; |
2804 | } |
2805 | |
2806 | c = buf[2]; |
2807 | anchor_relative = (c >> 7) & 1; |
2808 | anchor_vertical = c & 0x7F; |
2809 | anchor_horizontal = buf[3]; |
2810 | if (0 == anchor_relative) { |
2811 | if (unlikely (anchor_vertical >= 75 |
2812 | || anchor_horizontal >= 210)) { |
2813 | ds->error_line = __LINE__; |
2814 | return FALSE; |
2815 | } |
2816 | } else { |
2817 | if (unlikely (anchor_vertical >= 100 |
2818 | || anchor_horizontal >= 100)) { |
2819 | ds->error_line = __LINE__; |
2820 | return FALSE; |
2821 | } |
2822 | } |
2823 | |
2824 | c = buf[4]; |
2825 | anchor_point = c >> 4; |
2826 | if (unlikely (anchor_point >= 9)) { |
2827 | ds->error_line = __LINE__; |
2828 | return FALSE; |
2829 | } |
2830 | |
2831 | //printf("define window %d\n", column_count_m1); |
2832 | |
2833 | column_count_m1 = buf[5]; |
2834 | /* We also check the top two zero bits. */ |
2835 | if (unlikely (column_count_m1 >= 42)) { |
2836 | ds->error_line = __LINE__; |
2837 | return FALSE; |
2838 | } |
2839 | //printf("define windowa\n"); |
2840 | |
2841 | window_id = buf[0] & 7; |
2842 | dw = &ds->window[window_id]; |
2843 | window_map = 1 << window_id; |
2844 | |
2845 | ds->curr_window = dw; |
2846 | |
2847 | c = buf[1]; |
2848 | dw->visible = (c >> 5) & 1; |
2849 | dw->row_lock = (c >> 4) & 1; |
2850 | dw->column_lock = (c >> 4) & 1; |
2851 | dw->priority = c & 7; |
2852 | |
2853 | dw->anchor_relative = anchor_relative; |
2854 | dw->anchor_vertical = anchor_vertical; |
2855 | dw->anchor_horizontal = anchor_horizontal; |
2856 | dw->anchor_point = anchor_point; |
2857 | |
2858 | c = buf[4]; |
2859 | dw->row_count = (c & 15) + 1; |
2860 | dw->column_count = column_count_m1 + 1; |
2861 | |
2862 | c = buf[6]; |
2863 | window_style_id = (c >> 3) & 7; |
2864 | pen_style_id = c & 7; |
2865 | |
2866 | if (window_style_id > 0) { |
2867 | dw->style = window_styles[window_style_id-1]; |
2868 | } else if (0 == (ds->created & window_map)) { |
2869 | dw->style = window_styles[1]; |
2870 | } |
2871 | |
2872 | if (pen_style_id > 0) { |
2873 | dw->curr_pen.style = pen_styles[pen_style_id]; |
2874 | } else if (0 == (ds->created & window_map)) { |
2875 | dw->curr_pen.style = pen_styles[1]; |
2876 | } |
2877 | |
2878 | if (0 != (ds->created & window_map)) |
2879 | return TRUE; |
2880 | |
2881 | /* Has to be something, no? */ |
2882 | dw->curr_pen.text_tag = TEXT_TAG_NOT_DISPLAYABLE; |
2883 | |
2884 | dw->curr_column = 0; |
2885 | dw->curr_row = 0; |
2886 | |
2887 | dw->streamed = 0; |
2888 | |
2889 | cc_timestamp_reset (&dw->timestamp_c0); |
2890 | |
2891 | ds->created |= window_map; |
2892 | |
2893 | //printf("define %x %x\n", ds->curr_window, ds->created); |
2894 | |
2895 | return dtvcc_clear_windows (dc, ds, window_map); |
2896 | } |
2897 | |
2898 | static vbi_bool |
2899 | dtvcc_display_windows (struct dtvcc_decoder * dc, |
2900 | struct dtvcc_service * ds, |
2901 | unsigned int c, |
2902 | dtvcc_window_map window_map) |
2903 | { |
2904 | unsigned int i; |
2905 | |
2906 | window_map &= ds->created; |
2907 | |
2908 | //printf("display %02x %p %02x\n", c, ds->curr_window, ds->created); |
2909 | if (ds->curr_window == NULL && ds->created == 0) { |
2910 | return FALSE; |
2911 | //return TRUE; |
2912 | } |
2913 | |
2914 | for (i = 0; i < 8; ++i) { |
2915 | struct dtvcc_window *dw; |
2916 | vbi_bool was_visible; |
2917 | |
2918 | if (0 == (window_map & (1 << i))) |
2919 | continue; |
2920 | |
2921 | dw = &ds->window[i]; |
2922 | was_visible = dw->visible; |
2923 | |
2924 | switch (c) { |
2925 | case 0x89: /* DSW DisplayWindows */ |
2926 | dw->visible = TRUE; |
2927 | break; |
2928 | |
2929 | case 0x8A: /* HDW HideWindows */ |
2930 | dw->visible = FALSE; |
2931 | break; |
2932 | |
2933 | case 0x8B: /* TGW ToggleWindows */ |
2934 | dw->visible = was_visible ^ TRUE; |
2935 | break; |
2936 | } |
2937 | |
2938 | if (!was_visible) { |
2939 | unsigned int row; |
2940 | |
2941 | dw->timestamp_c0 = ds->timestamp; |
2942 | for (row = 0; row < dw->row_count; ++row) { |
2943 | dtvcc_stream_event (dc, ds, dw, row); |
2944 | } |
2945 | } |
2946 | } |
2947 | |
2948 | dtvcc_render(dc, ds); |
2949 | |
2950 | return TRUE; |
2951 | } |
2952 | |
2953 | static vbi_bool |
2954 | dtvcc_carriage_return (struct dtvcc_decoder * dc, |
2955 | struct dtvcc_service * ds) |
2956 | { |
2957 | struct dtvcc_window *dw; |
2958 | unsigned int row; |
2959 | unsigned int column; |
2960 | |
2961 | dw = ds->curr_window; |
2962 | if (NULL == dw) { |
2963 | ds->error_line = __LINE__; |
2964 | return FALSE; |
2965 | } |
2966 | |
2967 | dtvcc_stream_event (dc, ds, dw, dw->curr_row); |
2968 | |
2969 | row = dw->curr_row; |
2970 | column = dw->curr_column; |
2971 | |
2972 | switch (dw->style.scroll_direction) { |
2973 | case DIR_LEFT_RIGHT: |
2974 | dw->curr_row = 0; |
2975 | if (column > 0) { |
2976 | dw->curr_column = column - 1; |
2977 | break; |
2978 | } |
2979 | dw->streamed = (dw->streamed << 1) |
2980 | & ~(1 << dw->column_count); |
2981 | for (row = 0; row < dw->row_count; ++row) { |
2982 | for (column = dw->column_count - 1; |
2983 | column > 0; --column) { |
2984 | dw->buffer[row][column] = |
2985 | dw->buffer[row][column - 1]; |
2986 | dw->pen[row][column] = |
2987 | dw->pen[row][column - 1]; |
2988 | } |
2989 | dw->buffer[row][column] = 0; |
2990 | memset(&dw->pen[row][column], 0, sizeof(dw->pen[0][0])); |
2991 | } |
2992 | break; |
2993 | |
2994 | case DIR_RIGHT_LEFT: |
2995 | dw->curr_row = 0; |
2996 | if (column + 1 < dw->row_count) { |
2997 | dw->curr_column = column + 1; |
2998 | break; |
2999 | } |
3000 | dw->streamed >>= 1; |
3001 | for (row = 0; row < dw->row_count; ++row) { |
3002 | for (column = 0; |
3003 | column < dw->column_count - 1; ++column) { |
3004 | dw->buffer[row][column] = |
3005 | dw->buffer[row][column + 1]; |
3006 | dw->pen[row][column] = |
3007 | dw->pen[row][column + 1]; |
3008 | } |
3009 | dw->buffer[row][column] = 0; |
3010 | memset(&dw->pen[row][column], 0, sizeof(dw->pen[0][0])); |
3011 | } |
3012 | break; |
3013 | |
3014 | case DIR_TOP_BOTTOM: |
3015 | dw->curr_column = 0; |
3016 | if (row > 0) { |
3017 | dw->curr_row = row - 1; |
3018 | break; |
3019 | } |
3020 | dw->streamed = (dw->streamed << 1) |
3021 | & ~(1 << dw->row_count); |
3022 | memmove (&dw->buffer[1], &dw->buffer[0], |
3023 | sizeof (dw->buffer[0]) * (dw->row_count - 1)); |
3024 | memmove (&dw->pen[1], &dw->pen[0], |
3025 | sizeof (dw->pen[0]) * (dw->row_count - 1)); |
3026 | memset (&dw->buffer[0], 0, sizeof (dw->buffer[0])); |
3027 | memset (&dw->pen[0], 0, sizeof(dw->pen[0])); |
3028 | break; |
3029 | |
3030 | case DIR_BOTTOM_TOP: |
3031 | dw->curr_column = 0; |
3032 | if (row + 1 < dw->row_count) { |
3033 | dw->curr_row = row + 1; |
3034 | break; |
3035 | } |
3036 | dw->streamed >>= 1; |
3037 | memmove (&dw->buffer[0], &dw->buffer[1], |
3038 | sizeof (dw->buffer[0]) * (dw->row_count - 1)); |
3039 | memmove (&dw->pen[0], &dw->pen[1], |
3040 | sizeof (dw->pen[0]) * (dw->row_count - 1)); |
3041 | memset (&dw->buffer[row], 0, sizeof (dw->buffer[0])); |
3042 | memset (&dw->pen[row], 0, sizeof (dw->pen[0])); |
3043 | break; |
3044 | } |
3045 | |
3046 | return TRUE; |
3047 | } |
3048 | |
3049 | static vbi_bool |
3050 | dtvcc_form_feed (struct dtvcc_decoder * dc, |
3051 | struct dtvcc_service * ds) |
3052 | { |
3053 | struct dtvcc_window *dw; |
3054 | dtvcc_window_map window_map; |
3055 | |
3056 | dw = ds->curr_window; |
3057 | if (NULL == dw) { |
3058 | ds->error_line = __LINE__; |
3059 | return FALSE; |
3060 | } |
3061 | |
3062 | window_map = 1 << dtvcc_window_id (ds, dw); |
3063 | |
3064 | if (!dtvcc_clear_windows (dc, ds, window_map)) |
3065 | return FALSE; |
3066 | |
3067 | dw->curr_row = 0; |
3068 | dw->curr_column = 0; |
3069 | |
3070 | return TRUE; |
3071 | } |
3072 | |
3073 | static vbi_bool |
3074 | dtvcc_backspace (struct dtvcc_decoder * dc, |
3075 | struct dtvcc_service * ds) |
3076 | { |
3077 | struct dtvcc_window *dw; |
3078 | unsigned int row; |
3079 | unsigned int column; |
3080 | unsigned int mask; |
3081 | |
3082 | dc = dc; /* unused */ |
3083 | |
3084 | dw = ds->curr_window; |
3085 | if (NULL == dw) { |
3086 | ds->error_line = __LINE__; |
3087 | return FALSE; |
3088 | } |
3089 | |
3090 | row = dw->curr_row; |
3091 | column = dw->curr_column; |
3092 | |
3093 | switch (dw->style.print_direction) { |
3094 | case DIR_LEFT_RIGHT: |
3095 | mask = 1 << row; |
3096 | if (column-- <= 0) |
3097 | return TRUE; |
3098 | break; |
3099 | |
3100 | case DIR_RIGHT_LEFT: |
3101 | mask = 1 << row; |
3102 | if (++column >= dw->column_count) |
3103 | return TRUE; |
3104 | break; |
3105 | |
3106 | case DIR_TOP_BOTTOM: |
3107 | mask = 1 << column; |
3108 | if (row-- <= 0) |
3109 | return TRUE; |
3110 | break; |
3111 | |
3112 | case DIR_BOTTOM_TOP: |
3113 | mask = 1 << column; |
3114 | if (++row >= dw->row_count) |
3115 | return TRUE; |
3116 | break; |
3117 | } |
3118 | |
3119 | if (0 != dw->buffer[row][column]) { |
3120 | dw->streamed &= ~mask; |
3121 | dw->buffer[row][column] = 0; |
3122 | memset(&dw->pen[row][column], 0, sizeof(dw->pen[0][0])); |
3123 | } |
3124 | |
3125 | dw->curr_row = row; |
3126 | dw->curr_column = column; |
3127 | |
3128 | return TRUE; |
3129 | } |
3130 | |
3131 | static vbi_bool |
3132 | dtvcc_hor_carriage_return (struct dtvcc_decoder * dc, |
3133 | struct dtvcc_service * ds) |
3134 | { |
3135 | struct dtvcc_window *dw; |
3136 | unsigned int row; |
3137 | unsigned int column; |
3138 | unsigned int mask; |
3139 | |
3140 | dc = dc; /* unused */ |
3141 | |
3142 | dw = ds->curr_window; |
3143 | if (NULL == dw) { |
3144 | ds->error_line = __LINE__; |
3145 | return FALSE; |
3146 | } |
3147 | |
3148 | row = dw->curr_row; |
3149 | column = dw->curr_column; |
3150 | |
3151 | switch (dw->style.print_direction) { |
3152 | case DIR_LEFT_RIGHT: |
3153 | case DIR_RIGHT_LEFT: |
3154 | mask = 1 << row; |
3155 | memset (&dw->buffer[row][0], 0, |
3156 | sizeof (dw->buffer[0])); |
3157 | memset (&dw->pen[row][0], 0, |
3158 | sizeof (dw->pen[0])); |
3159 | if (DIR_LEFT_RIGHT == dw->style.print_direction) |
3160 | dw->curr_column = 0; |
3161 | else |
3162 | dw->curr_column = dw->column_count - 1; |
3163 | break; |
3164 | |
3165 | case DIR_TOP_BOTTOM: |
3166 | case DIR_BOTTOM_TOP: |
3167 | mask = 1 << column; |
3168 | for (row = 0; row < dw->column_count; ++row) { |
3169 | dw->buffer[row][column] = 0; |
3170 | memset(&dw->pen[row][column], 0, sizeof(dw->pen[0][0])); |
3171 | } |
3172 | if (DIR_TOP_BOTTOM == dw->style.print_direction) |
3173 | dw->curr_row = 0; |
3174 | else |
3175 | dw->curr_row = dw->row_count - 1; |
3176 | break; |
3177 | } |
3178 | |
3179 | dw->streamed &= ~mask; |
3180 | |
3181 | return TRUE; |
3182 | } |
3183 | |
3184 | static vbi_bool |
3185 | dtvcc_delete_windows (struct dtvcc_decoder * dc, |
3186 | struct dtvcc_service * ds, |
3187 | dtvcc_window_map window_map) |
3188 | { |
3189 | struct dtvcc_window *dw; |
3190 | int i; |
3191 | int changed = 0; |
3192 | |
3193 | for (i = 0; i < N_ELEMENTS(ds->window); i ++) { |
3194 | dw = &ds->window[i]; |
3195 | |
3196 | if (NULL != dw) { |
3197 | unsigned int window_id; |
3198 | window_id = dtvcc_window_id (ds, dw); |
3199 | if (ds->created & (1 << window_id)) { |
3200 | if (window_map & (1 << window_id)) { |
3201 | //printf("delete window %d\n", window_id); |
3202 | dtvcc_stream_event (dc, ds, dw, dw->curr_row); |
3203 | |
3204 | if (dw == ds->curr_window) |
3205 | ds->curr_window = NULL; |
3206 | |
3207 | if (dw->visible) |
3208 | dtvcc_render(dc, ds); |
3209 | |
3210 | memset (dw->buffer, 0, sizeof (dw->buffer)); |
3211 | memset (dw->pen, 0, sizeof(dw->pen)); |
3212 | dw->visible = 0; |
3213 | |
3214 | changed = 1; |
3215 | } |
3216 | } |
3217 | } |
3218 | } |
3219 | |
3220 | ds->created &= ~window_map; |
3221 | |
3222 | return TRUE; |
3223 | } |
3224 | |
3225 | static vbi_bool |
3226 | dtvcc_delay_cmd (struct dtvcc_decoder * dc, |
3227 | struct dtvcc_service * ds, |
3228 | uint8_t delay_cmd, |
3229 | uint8_t delay_time) |
3230 | { |
3231 | struct timespec now_ts; |
3232 | int plus_one_second = 0; |
3233 | clock_gettime(CLOCK_REALTIME, &now_ts); |
3234 | if (delay_cmd == 0x8D) |
3235 | { |
3236 | /* Set trigger time |
3237 | Until that time, sevice stop decoding. |
3238 | */ |
3239 | /* Delay time is tenths of second */ |
3240 | /* We set the timer a little faster to avoid double delay conflict */ |
3241 | if ((1000000000 - (delay_time%10) * 100000000) > now_ts.tv_nsec) |
3242 | { |
3243 | ds->delay_timer.tv_nsec = ((delay_time % 10) * 100000000 + now_ts.tv_nsec) % 1000000000; |
3244 | ds->delay_timer.tv_sec = (1 + now_ts.tv_sec + delay_time/10) -1; |
3245 | } |
3246 | else |
3247 | { |
3248 | ds->delay_timer.tv_nsec = (delay_time % 10) * 100000000 + now_ts.tv_nsec; |
3249 | ds->delay_timer.tv_sec = (now_ts.tv_sec + delay_time / 10) -1; |
3250 | } |
3251 | ds->delay_timer.tv_sec = now_ts.tv_sec + 1; |
3252 | ds->delay = 1; |
3253 | ds->delay_cancel = 0; |
3254 | //AM_DEBUG(1, "Enter delay cmd, now %d until %d", now_ts.tv_sec, ds->delay_timer.tv_sec); |
3255 | } |
3256 | else if (delay_cmd == 0x8E) |
3257 | { |
3258 | ds->delay = 0; |
3259 | ds->delay_cancel = 1; |
3260 | } |
3261 | return TRUE; |
3262 | } |
3263 | |
3264 | static vbi_bool |
3265 | dtvcc_command (struct dtvcc_decoder * dc, |
3266 | struct dtvcc_service * ds, |
3267 | unsigned int * se_length, |
3268 | uint8_t * buf, |
3269 | unsigned int n_bytes) |
3270 | { |
3271 | unsigned int c; |
3272 | unsigned int window_id; |
3273 | |
3274 | c = buf[0]; |
3275 | if ((int8_t) c < 0) { |
3276 | *se_length = dtvcc_c1_length[c - 0x80]; |
3277 | } else { |
3278 | *se_length = dtvcc_c0_length[c >> 3]; |
3279 | } |
3280 | |
3281 | if (*se_length > n_bytes) { |
3282 | ds->error_line = __LINE__; |
3283 | return TRUE; |
3284 | } |
3285 | switch (c) { |
3286 | case 0x08: /* BS Backspace */ |
3287 | return dtvcc_backspace (dc, ds); |
3288 | |
3289 | case 0x0C: /* FF Form Feed */ |
3290 | return dtvcc_form_feed (dc, ds); |
3291 | |
3292 | case 0x0D: /* CR Carriage Return */ |
3293 | return dtvcc_carriage_return (dc, ds); |
3294 | |
3295 | case 0x0E: /* HCR Horizontal Carriage Return */ |
3296 | return dtvcc_hor_carriage_return (dc, ds); |
3297 | |
3298 | case 0x80 ... 0x87: /* CWx SetCurrentWindow */ |
3299 | window_id = c & 7; |
3300 | if (0 == (ds->created & (1 << window_id))) { |
3301 | ds->error_line = __LINE__; |
3302 | return FALSE; |
3303 | } |
3304 | ds->curr_window = &ds->window[window_id]; |
3305 | return TRUE; |
3306 | |
3307 | case 0x88: /* CLW ClearWindows */ |
3308 | return dtvcc_clear_windows (dc, ds, buf[1]); |
3309 | |
3310 | case 0x89: /* DSW DisplayWindows */ |
3311 | return dtvcc_display_windows (dc, ds, c, buf[1]); |
3312 | |
3313 | case 0x8D: |
3314 | dtvcc_delay_cmd(dc, ds, 0x8d, buf[1]); |
3315 | return 0; |
3316 | |
3317 | case 0x8E: |
3318 | dtvcc_delay_cmd(dc, ds, 0x8e, buf[1]); |
3319 | return 0; |
3320 | |
3321 | case 0x8A: /* HDW HideWindows */ |
3322 | return dtvcc_display_windows (dc, ds, c, buf[1]); |
3323 | |
3324 | case 0x8B: /* TGW ToggleWindows */ |
3325 | return dtvcc_display_windows (dc, ds, c, buf[1]); |
3326 | |
3327 | case 0x8C: /* DLW DeleteWindows */ |
3328 | return dtvcc_delete_windows (dc, ds, buf[1]); |
3329 | |
3330 | case 0x8F: /* RST Reset */ |
3331 | dtvcc_reset_service (ds); |
3332 | return TRUE; |
3333 | |
3334 | case 0x90: /* SPA SetPenAttributes */ |
3335 | return dtvcc_set_pen_attributes (ds, buf); |
3336 | |
3337 | case 0x91: /* SPC SetPenColor */ |
3338 | return dtvcc_set_pen_color (ds, buf); |
3339 | |
3340 | case 0x92: /* SPL SetPenLocation */ |
3341 | return dtvcc_set_pen_location (dc, ds, buf); |
3342 | |
3343 | case 0x97: /* SWA SetWindowAttributes */ |
3344 | return dtvcc_set_window_attributes (ds, buf); |
3345 | |
3346 | case 0x98 ... 0x9F: /* DFx DefineWindow */ |
3347 | return dtvcc_define_window (dc, ds, buf); |
3348 | |
3349 | default: |
3350 | return TRUE; |
3351 | } |
3352 | } |
3353 | |
3354 | static vbi_bool |
3355 | dtvcc_decode_se (struct dtvcc_decoder * dc, |
3356 | struct dtvcc_service * ds, |
3357 | unsigned int * se_length, |
3358 | uint8_t * buf, |
3359 | unsigned int n_bytes) |
3360 | { |
3361 | unsigned int c; |
3362 | |
3363 | c = buf[0]; |
3364 | if (likely (0 != (c & 0x60))) { |
3365 | /* G0/G1 character. */ |
3366 | *se_length = 1; |
3367 | return dtvcc_put_char (dc, ds, c); |
3368 | } |
3369 | |
3370 | if (0x10 != c) { |
3371 | /* C0/C1 control code. */ |
3372 | return dtvcc_command (dc, ds, se_length, |
3373 | buf, n_bytes); |
3374 | } |
3375 | |
3376 | if (unlikely (n_bytes < 2)) { |
3377 | ds->error_line = __LINE__; |
3378 | return FALSE; |
3379 | } |
3380 | |
3381 | c = buf[1]; |
3382 | if (likely (0 != (c & 0x60))) { |
3383 | /* G2/G3 character. */ |
3384 | *se_length = 2; |
3385 | return dtvcc_put_char (dc, ds, 0x1000 | c); |
3386 | } |
3387 | |
3388 | /* CEA 708-C defines no C2 or C3 commands. */ |
3389 | |
3390 | if ((int8_t) c >= 0) { |
3391 | /* C2 code. */ |
3392 | *se_length = (c >> 3) + 2; |
3393 | } else if (c < 0x90) { |
3394 | /* C3 Fixed Length Commands. */ |
3395 | *se_length = (c >> 3) - 10; |
3396 | } else { |
3397 | /* C3 Variable Length Commands. */ |
3398 | |
3399 | if (unlikely (n_bytes < 3)) { |
3400 | ds->error_line = __LINE__; |
3401 | return FALSE; |
3402 | } |
3403 | |
3404 | /* type [2], zero_bit [1], |
3405 | length [5] */ |
3406 | *se_length = (buf[2] & 0x1F) + 3; |
3407 | } |
3408 | |
3409 | if (unlikely (n_bytes < *se_length)) { |
3410 | ds->error_line = __LINE__; |
3411 | return FALSE; |
3412 | } |
3413 | |
3414 | return TRUE; |
3415 | } |
3416 | |
3417 | static vbi_bool |
3418 | dtvcc_decode_syntactic_elements (struct dtvcc_decoder * dc, |
3419 | struct dtvcc_service * ds, |
3420 | uint8_t * buf, |
3421 | unsigned int n_bytes) |
3422 | { |
3423 | ds->timestamp = dc->timestamp; |
3424 | struct timespec ts_now; |
3425 | |
3426 | #if 0 |
3427 | AM_DEBUG(1, "+++++++++++++++++++++ servie %d\n", n_bytes); |
3428 | { |
3429 | int i; |
3430 | |
3431 | for (i = 0; i < n_bytes; i ++) |
3432 | AM_DEBUG(1, "++++++++++++++ %02x ", buf[i]); |
3433 | } |
3434 | #endif |
3435 | while (n_bytes > 0) { |
3436 | unsigned int se_length; |
3437 | |
3438 | //printf("dec se %02x\n", buf[0]); |
3439 | |
3440 | if (!dtvcc_decode_se (dc, ds, |
3441 | &se_length, |
3442 | buf, n_bytes)) { |
3443 | return FALSE; |
3444 | } |
3445 | |
3446 | buf += se_length; |
3447 | n_bytes -= se_length; |
3448 | } |
3449 | |
3450 | return TRUE; |
3451 | } |
3452 | |
3453 | void |
3454 | dtvcc_decode_packet (struct dtvcc_decoder * dc, |
3455 | const struct timeval * tv, |
3456 | int64_t pts) |
3457 | { |
3458 | unsigned int packet_size_code; |
3459 | unsigned int packet_size; |
3460 | unsigned int i; |
3461 | |
3462 | #if 0 |
3463 | printf("%d dtvcc decode packet %d: ", get_input_offset(), dc->packet_size); |
3464 | |
3465 | for (i = 0; i < dc->packet_size; i ++) { |
3466 | printf("%02x ", dc->packet[i]); |
3467 | } |
3468 | printf("\n"); |
3469 | #endif |
3470 | dc->timestamp.sys = *tv; |
3471 | dc->timestamp.pts = pts; |
3472 | |
3473 | /* Packet Layer. */ |
3474 | |
3475 | /* sequence_number [2], packet_size_code [6], |
3476 | packet_data [n * 8] */ |
3477 | #if 1 |
3478 | if (dc->next_sequence_number >= 0 |
3479 | && 0 != ((dc->packet[0] ^ dc->next_sequence_number) & 0xC0)) { |
3480 | dtvcc_reset (dc); |
3481 | return; |
3482 | } |
3483 | #endif |
3484 | dc->next_sequence_number = dc->packet[0] + 0x40; |
3485 | |
3486 | packet_size_code = dc->packet[0] & 0x3F; |
3487 | packet_size = 128; |
3488 | if (packet_size_code > 0) |
3489 | packet_size = packet_size_code * 2; |
3490 | |
3491 | /* CEA 708-C Section 5: Apparently packet_size need not be |
3492 | equal to the actually transmitted amount of data. */ |
3493 | if (packet_size > dc->packet_size) { |
3494 | /* |
3495 | dtvcc_reset (dc); |
3496 | return;*/ |
3497 | packet_size = dc->packet_size; |
3498 | } |
3499 | |
3500 | /* Service Layer. */ |
3501 | |
3502 | /* CEA 708-C Section 6.2.5, 6.3: Service Blocks and syntactic |
3503 | elements must not cross Caption Channel Packet |
3504 | boundaries. */ |
3505 | |
3506 | for (i = 1; i < packet_size;) { |
3507 | unsigned int service_number; |
3508 | unsigned int block_size; |
3509 | unsigned int header_size; |
3510 | unsigned int c; |
3511 | |
3512 | header_size = 1; |
3513 | |
3514 | /* service_number [3], block_size [5], |
3515 | (null_fill [2], extended_service_number [6]), |
3516 | (Block_data [n * 8]) */ |
3517 | |
3518 | c = dc->packet[i]; |
3519 | service_number = (c & 0xE0) >> 5; |
3520 | |
3521 | //printf("srv %d\n", service_number); |
3522 | |
3523 | /* CEA 708-C Section 6.3: Ignore block_size if |
3524 | service_number is zero. */ |
3525 | if (0 == service_number) { |
3526 | /* NULL Service Block Header, no more data in |
3527 | this Caption Channel Packet. */ |
3528 | break; |
3529 | } |
3530 | |
3531 | /* CEA 708-C Section 6.2.1: Apparently block_size zero |
3532 | is valid, although properly it should only occur in |
3533 | NULL Service Block Headers. */ |
3534 | block_size = c & 0x1F; |
3535 | |
3536 | if (7 == service_number) { |
3537 | if (i + 1 > packet_size) |
3538 | break; |
3539 | //goto service_block_incomplete; |
3540 | |
3541 | header_size = 2; |
3542 | c = dc->packet[i + 1]; |
3543 | |
3544 | /* We also check the null_fill bits. */ |
3545 | if (c < 7 || c > 63) |
3546 | break; |
3547 | //goto invalid_service_block; |
3548 | |
3549 | service_number = c; |
3550 | } |
3551 | |
3552 | //printf("srv %d %d %d %d\n", service_number, header_size, block_size, packet_size); |
3553 | |
3554 | if (i + header_size + block_size > packet_size) |
3555 | { |
3556 | break; |
3557 | } |
3558 | //goto service_block_incomplete; |
3559 | |
3560 | if (service_number <= 6) { |
3561 | struct dtvcc_service *ds; |
3562 | unsigned int in; |
3563 | |
3564 | ds = &dc->service[service_number - 1]; |
3565 | in = ds->service_data_in; |
3566 | memcpy (ds->service_data + in, |
3567 | dc->packet + i + header_size, |
3568 | block_size); |
3569 | ds->service_data_in = in + block_size; |
3570 | } |
3571 | |
3572 | i += header_size + block_size; |
3573 | } |
3574 | |
3575 | for (i = 0; i < 6; ++i) { |
3576 | struct dtvcc_service *ds; |
3577 | struct program *pr; |
3578 | vbi_bool success; |
3579 | |
3580 | ds = &dc->service[i]; |
3581 | if (0 == ds->service_data_in) |
3582 | continue; |
3583 | if (!ds->delay || |
3584 | (ds->delay && ds->service_data_in>=128)) |
3585 | { |
3586 | //AM_DEBUG(1, "service datain %d", ds->service_data_in); |
3587 | success = dtvcc_decode_syntactic_elements |
3588 | (dc, ds, ds->service_data, ds->service_data_in); |
3589 | if (ds->service_data_in >= 128) |
3590 | { |
3591 | ds->delay = 0; |
3592 | ds->delay_cancel = 0; |
3593 | } |
3594 | ds->service_data_in = 0; |
3595 | |
3596 | if (success) |
3597 | continue; |
3598 | } |
3599 | //dtvcc_reset_service (ds); |
3600 | dc->next_sequence_number = -1; |
3601 | } |
3602 | |
3603 | return; |
3604 | |
3605 | invalid_service_block: |
3606 | { |
3607 | dtvcc_reset (dc); |
3608 | return; |
3609 | } |
3610 | |
3611 | service_block_incomplete: |
3612 | { |
3613 | dtvcc_reset (dc); |
3614 | return; |
3615 | } |
3616 | |
3617 | } |
3618 | |
3619 | void |
3620 | dtvcc_try_decode_packet (struct dtvcc_decoder * dc, |
3621 | const struct timeval * tv, |
3622 | int64_t pts) |
3623 | { |
3624 | unsigned int packet_size_code; |
3625 | unsigned int packet_size; |
3626 | |
3627 | if (dc->packet_size < 1) |
3628 | return; |
3629 | |
3630 | packet_size_code = dc->packet[0] & 0x3F; |
3631 | |
3632 | packet_size = 128; |
3633 | if (packet_size_code > 0) |
3634 | packet_size = packet_size_code * 2; |
3635 | |
3636 | if (packet_size <= dc->packet_size) { |
3637 | dtvcc_decode_packet(dc, tv, pts); |
3638 | dc->packet_size = 0; |
3639 | } |
3640 | } |
3641 | |
3642 | static void |
3643 | dtvcc_reset_service (struct dtvcc_service * ds) |
3644 | { |
3645 | ds->curr_window = NULL; |
3646 | ds->created = 0; |
3647 | ds->delay = 0; |
3648 | ds->delay_cancel = 0; |
3649 | |
3650 | cc_timestamp_reset (&ds->timestamp); |
3651 | } |
3652 | |
3653 | void |
3654 | dtvcc_reset (struct dtvcc_decoder * dc) |
3655 | { |
3656 | dtvcc_reset_service (&dc->service[0]); |
3657 | dtvcc_reset_service (&dc->service[1]); |
3658 | dc->packet_size = 0; |
3659 | dc->next_sequence_number = -1; |
3660 | } |
3661 | |
3662 | void |
3663 | dtvcc_init (struct dtvcc_decoder * dc) |
3664 | { |
3665 | memset(dc, 0, sizeof(struct dtvcc_decoder)); |
3666 | dtvcc_reset (dc); |
3667 | cc_timestamp_reset (&dc->timestamp); |
3668 | } |
3669 | |
3670 | static void dtvcc_window_to_page(vbi_decoder *vbi, struct dtvcc_window *dw, struct vbi_page *pg) |
3671 | { |
3672 | int i, j, c; |
3673 | vbi_char ac; |
3674 | vbi_opacity fg_opacity, bg_opacity; |
3675 | |
3676 | static const vbi_opacity vbi_opacity_map[]={ |
3677 | VBI_OPAQUE, |
3678 | VBI_OPAQUE, |
3679 | VBI_SEMI_TRANSPARENT, |
3680 | VBI_TRANSPARENT_SPACE |
3681 | }; |
3682 | |
3683 | memset(pg, 0, sizeof(struct vbi_page)); |
3684 | #if 1 |
3685 | pg->rows = dw->row_count; |
3686 | pg->columns = dw->column_count; |
3687 | |
3688 | dtvcc_set_page_color_map(vbi, pg); |
3689 | memset(dw->row_start, 0, sizeof(dw->row_start)); |
3690 | |
3691 | for (i=0; i<pg->rows; i++) |
3692 | { |
3693 | for (j=0; j<pg->columns; j++) |
3694 | { |
3695 | memset(&ac, 0, sizeof(ac)); |
3696 | /*use the curr_pen to draw all the text, actually this isn't reasonable*/ |
3697 | if (dw->curr_pen.style.fg_opacity >= N_ELEMENTS(vbi_opacity_map)){ |
3698 | fg_opacity = VBI_OPAQUE; |
3699 | }else{ |
3700 | fg_opacity = vbi_opacity_map[dw->curr_pen.style.fg_opacity]; |
3701 | } |
3702 | |
3703 | if (dw->curr_pen.style.bg_opacity >= N_ELEMENTS(vbi_opacity_map)){ |
3704 | bg_opacity = VBI_OPAQUE; |
3705 | }else{ |
3706 | bg_opacity = vbi_opacity_map[dw->curr_pen.style.bg_opacity]; |
3707 | } |
3708 | |
3709 | ac.opacity = (fg_opacity<<4) | bg_opacity; |
3710 | ac.foreground = dtvcc_map_color(dw->curr_pen.style.fg_color); |
3711 | ac.background = dtvcc_map_color(dw->curr_pen.style.bg_color); |
3712 | |
3713 | c = dw->buffer[i][j]; |
3714 | if (0 == c) { |
3715 | ac.unicode = 0x20; |
3716 | ac.opacity = VBI_TRANSPARENT_SPACE; |
3717 | dw->row_start[i] ++; |
3718 | } else { |
3719 | ac.unicode = dtvcc_unicode (c); |
3720 | if (0 == ac.unicode) { |
3721 | ac.unicode = 0x20; |
3722 | dw->row_start[i] ++; |
3723 | } |
3724 | } |
3725 | pg->text[i*pg->columns + j] = ac; |
3726 | } |
3727 | } |
3728 | #else |
3729 | pg->rows = 15; |
3730 | pg->columns = 42; |
3731 | dtvcc_set_page_color_map(vbi, pg); |
3732 | for (i=0; i<pg->rows; i++) |
3733 | { |
3734 | for (j=0; j<pg->columns; j++) |
3735 | { |
3736 | memset(&ac, 0, sizeof(ac)); |
3737 | ac.opacity = VBI_OPAQUE; |
3738 | ac.foreground = VBI_WHITE; |
3739 | ac.background = VBI_BLACK; |
3740 | if (j == 0) |
3741 | c = '0' + (i%10); |
3742 | else |
3743 | c = '0' + (j%10); |
3744 | ac.unicode = dtvcc_unicode (c); |
3745 | LOGI("TEXT(%x): %c", ac.unicode, ac.unicode); |
3746 | pg->text[i*pg->columns + j] = ac; |
3747 | } |
3748 | } |
3749 | #endif |
3750 | pg->screen_color = 0; |
3751 | pg->screen_opacity = VBI_OPAQUE; |
3752 | //pg->font[0] = vbi_font_descriptors; /* English */ |
3753 | //pg->font[1] = vbi_font_descriptors; |
3754 | } |
3755 | |
3756 | static int dtvcc_compare_window_priority(const void *window1, const void *window2) |
3757 | { |
3758 | struct dtvcc_window *w1 = (struct dtvcc_window*)window1; |
3759 | struct dtvcc_window *w2 = (struct dtvcc_window*)window2; |
3760 | |
3761 | return (w1->priority - w2->priority); |
3762 | } |
3763 | |
3764 | static void dtvcc_get_visible_windows(struct dtvcc_service *ds, int *cnt, struct dtvcc_window **windows) |
3765 | { |
3766 | int i, j = 0; |
3767 | |
3768 | for (i=0; i<N_ELEMENTS(ds->window); i++){ |
3769 | if (ds->window[i].visible && j < *cnt){ |
3770 | windows[j++] = &ds->window[i]; |
3771 | } |
3772 | } |
3773 | |
3774 | qsort(windows, j, sizeof(windows[0]), dtvcc_compare_window_priority); |
3775 | |
3776 | *cnt = j; |
3777 | } |
3778 | |
3779 | void tvcc_fetch_page(struct tvcc_decoder *td, int pgno, int *sub_cnt, struct vbi_page *sub_pages) |
3780 | { |
3781 | int sub_pg = 0; |
3782 | |
3783 | if (pgno < 1 || pgno > 14 || *sub_cnt <= 0) |
3784 | goto fetch_done; |
3785 | |
3786 | if (pgno <= 8){ |
3787 | if (vbi_fetch_cc_page(td->vbi, &sub_pages[0], pgno, 1)){ |
3788 | sub_pg = 1; |
3789 | sub_pages[0].pgno = pgno; |
3790 | } |
3791 | }else{ |
3792 | int i; |
3793 | struct dtvcc_service *ds = &td->dtvcc.service[pgno - 1 - 8]; |
3794 | struct dtvcc_window *dw; |
3795 | struct dtvcc_window *visible_windows[8]; |
3796 | |
3797 | sub_pg = *sub_cnt; |
3798 | if (sub_pg > 8) |
3799 | sub_pg = 8; |
3800 | |
3801 | dtvcc_get_visible_windows(ds, &sub_pg, visible_windows); |
3802 | |
3803 | for (i=0; i<sub_pg; i++){ |
3804 | dw = visible_windows[i]; |
3805 | |
3806 | dtvcc_window_to_page(td->vbi, dw, &sub_pages[i]); |
3807 | |
3808 | sub_pages[i].vbi = td->vbi; |
3809 | sub_pages[i].pgno = pgno; |
3810 | sub_pages[i].subno = dw - ds->window; |
3811 | } |
3812 | } |
3813 | |
3814 | fetch_done: |
3815 | *sub_cnt = sub_pg; |
3816 | } |
3817 | |
3818 | /* ATSC A/53 Part 4:2007 Closed Caption Data decoder */ |
3819 | |
3820 | /* Only handle effect */ |
3821 | static void update_service_status_internal (struct tvcc_decoder *td) |
3822 | { |
3823 | int i, j, k, l; |
3824 | struct timespec ts_now; |
3825 | struct dtvcc_decoder *decoder; |
3826 | struct dtvcc_pen_style *target_pen; |
3827 | int flash; |
3828 | |
3829 | decoder = &td->dtvcc; |
3830 | clock_gettime(CLOCK_REALTIME, &ts_now); |
3831 | |
3832 | flash = (ts_now.tv_nsec / 250000000) & 1; |
3833 | |
3834 | /* CS1 - CS6 */ |
3835 | for (i = 0; i < 6; ++i) |
3836 | { |
3837 | struct dtvcc_service *ds; |
3838 | struct program *pr; |
3839 | vbi_bool success; |
3840 | ds = &decoder->service[i]; |
3841 | /* Check every effect */ |
3842 | if (ds->delay) |
3843 | { |
3844 | struct vbi_event event; |
3845 | /* time is up */ |
3846 | if ((ts_now.tv_sec > ds->delay_timer.tv_sec) || |
3847 | ((ts_now.tv_sec == ds->delay_timer.tv_sec) &&(ts_now.tv_nsec > ds->delay_timer.tv_nsec)) || |
3848 | ds->delay_cancel) |
3849 | { |
3850 | //AM_DEBUG(1, "delay timeup"); |
3851 | ds->delay = 0; |
3852 | ds->delay_cancel = 0; |
3853 | dtvcc_decode_syntactic_elements |
3854 | (decoder, ds, ds->service_data, ds->service_data_in); |
3855 | |
3856 | ds->service_data_in = 0; |
3857 | } |
3858 | } |
3859 | |
3860 | if (flash == decoder->flash_state) |
3861 | continue; |
3862 | |
3863 | for (j = 0; j < 8; j++) |
3864 | { |
3865 | /*window flash treatment */ |
3866 | if (ds->window[j].style.window_flash) |
3867 | { |
3868 | ds->window[j].style.fill_opacity = flash?0:3; |
3869 | ds->update = 1; |
3870 | } |
3871 | |
3872 | /* Pen flash treatment */ |
3873 | for (k = 0; k < 16; k++) |
3874 | { |
3875 | for (l =0; l<42; l++) |
3876 | { |
3877 | target_pen = &ds->window[j].pen[k][l]; |
3878 | if (target_pen->bg_flash) |
3879 | { |
3880 | target_pen->bg_opacity = flash?0:3; |
3881 | ds->update = 1; |
3882 | } |
3883 | if (target_pen->fg_flash) |
3884 | { |
3885 | target_pen->fg_opacity = flash?0:3; |
3886 | ds->update = 1; |
3887 | } |
3888 | } |
3889 | } |
3890 | } |
3891 | } |
3892 | |
3893 | decoder->flash_state = flash; |
3894 | } |
3895 | |
3896 | static void |
3897 | update_display (struct tvcc_decoder *td) |
3898 | { |
3899 | int i; |
3900 | |
3901 | for (i = 0; i < N_ELEMENTS(td->dtvcc.service); i ++) { |
3902 | struct dtvcc_service *ds = &td->dtvcc.service[i]; |
3903 | |
3904 | if (ds->update) { |
3905 | struct vbi_event event; |
3906 | |
3907 | event.type = VBI_EVENT_CAPTION; |
3908 | event.ev.caption.pgno = i + 1 + 8/*after 8 cc channels*/; |
3909 | |
3910 | /* Permits calling tvcc_fetch_page from handler */ |
3911 | pthread_mutex_unlock(&td->mutex); |
3912 | |
3913 | vbi_send_event(td->vbi, &event); |
3914 | pthread_mutex_lock(&td->mutex); |
3915 | |
3916 | ds->update = 0; |
3917 | } |
3918 | } |
3919 | } |
3920 | |
3921 | |
3922 | /* Note pts may be < 0 if no PTS was received. */ |
3923 | void |
3924 | tvcc_decode_data (struct tvcc_decoder *td, |
3925 | int64_t pts, |
3926 | const uint8_t * buf, |
3927 | unsigned int n_bytes) |
3928 | { |
3929 | unsigned int process_cc_data_flag; |
3930 | unsigned int cc_count; |
3931 | unsigned int i; |
3932 | vbi_bool dtvcc; |
3933 | struct timeval now; |
3934 | |
3935 | if (buf[0] != 0x03) |
3936 | return; |
3937 | process_cc_data_flag = buf[1] & 0x40; |
3938 | if (!process_cc_data_flag) |
3939 | { |
3940 | return; |
3941 | } |
3942 | |
3943 | cc_count = buf[1] & 0x1F; |
3944 | dtvcc = FALSE; |
3945 | |
3946 | #if 0 |
3947 | printf("tvcc decode %d:\n", n_bytes); |
3948 | { |
3949 | int i; |
3950 | |
3951 | for (i = 0; i < n_bytes; i ++) { |
3952 | printf("%02x ", buf[i]); |
3953 | if ((i + 1) % 16 == 0) |
3954 | printf("\n"); |
3955 | } |
3956 | printf("\n"); |
3957 | } |
3958 | #endif |
3959 | pthread_mutex_lock(&td->mutex); |
3960 | |
3961 | //printf("cc count %d\n", cc_count); |
3962 | |
3963 | for (i = 0; i < cc_count; ++i) { |
3964 | unsigned int b0; |
3965 | unsigned int cc_valid; |
3966 | enum cc_type cc_type; |
3967 | unsigned int cc_data_1; |
3968 | unsigned int cc_data_2; |
3969 | unsigned int j; |
3970 | |
3971 | b0 = buf[3 + i * 3]; |
3972 | cc_valid = b0 & 4; |
3973 | cc_type = (enum cc_type)(b0 & 3); |
3974 | cc_data_1 = buf[4 + i * 3]; |
3975 | cc_data_2 = buf[5 + i * 3]; |
3976 | |
3977 | //printf("cc type %02x %02x %02x %02x\n", cc_type, cc_valid, cc_data_1, cc_data_2); |
3978 | |
3979 | switch (cc_type) { |
3980 | case NTSC_F1: |
3981 | case NTSC_F2: |
3982 | //printf("ntsc cc\n"); |
3983 | /* Note CEA 708-C Table 4: Only one NTSC pair |
3984 | will be present in field picture user_data |
3985 | or in progressive video pictures, and up to |
3986 | three can occur if the frame rate < 30 Hz |
3987 | or repeat_first_field = 1. */ |
3988 | if (!cc_valid || i >= 3 || dtvcc) { |
3989 | /* Illegal, invalid or filler. */ |
3990 | break; |
3991 | } |
3992 | #if 0 |
3993 | cc_feed (&td->cc, &buf[12 + i * 3], |
3994 | /* line */ (NTSC_F1 == cc_type) ? 21 : 284, |
3995 | &now, pts); |
3996 | #endif |
3997 | vbi_decode_caption(td->vbi, (NTSC_F1 == cc_type) ? 21 : 284, &buf[4 + i * 3]); |
3998 | break; |
3999 | |
4000 | case DTVCC_DATA: |
4001 | j = td->dtvcc.packet_size; |
4002 | if (j <= 0) { |
4003 | /* Missed packet start. */ |
4004 | break; |
4005 | } else if (!cc_valid) { |
4006 | /* End of DTVCC packet. */ |
4007 | } else if (j + 2 > 128) { |
4008 | /* Packet buffer overflow. */ |
4009 | dtvcc_reset (&td->dtvcc); |
4010 | td->dtvcc.packet_size = 0; |
4011 | } else { |
4012 | td->dtvcc.packet[j] = cc_data_1; |
4013 | td->dtvcc.packet[j + 1] = cc_data_2; |
4014 | td->dtvcc.packet_size = j + 2; |
4015 | |
4016 | dtvcc_try_decode_packet (&td->dtvcc, &now, pts); |
4017 | } |
4018 | break; |
4019 | |
4020 | case DTVCC_START: |
4021 | dtvcc = TRUE; |
4022 | j = td->dtvcc.packet_size; |
4023 | if (j > 0) { |
4024 | /* End of DTVCC packet. */ |
4025 | dtvcc_decode_packet (&td->dtvcc, |
4026 | &now, pts); |
4027 | } |
4028 | if (!cc_valid) { |
4029 | /* No new data. */ |
4030 | td->dtvcc.packet_size = 0; |
4031 | } else { |
4032 | td->dtvcc.packet[0] = cc_data_1; |
4033 | td->dtvcc.packet[1] = cc_data_2; |
4034 | td->dtvcc.packet_size = 2; |
4035 | dtvcc_try_decode_packet(&td->dtvcc, &now, pts); |
4036 | } |
4037 | break; |
4038 | } |
4039 | } |
4040 | |
4041 | update_service_status_internal(td); |
4042 | update_display(td); |
4043 | |
4044 | pthread_mutex_unlock(&td->mutex); |
4045 | } |
4046 | |
4047 | /* Only handle effect */ |
4048 | void update_service_status(struct tvcc_decoder *td) |
4049 | { |
4050 | pthread_mutex_lock(&td->mutex); |
4051 | |
4052 | update_service_status_internal(td); |
4053 | update_display(td); |
4054 | |
4055 | pthread_mutex_unlock(&td->mutex); |
4056 | } |
4057 | |
4058 | void tvcc_init(struct tvcc_decoder *td) |
4059 | { |
4060 | pthread_mutex_init(&td->mutex, NULL); |
4061 | dtvcc_init(&td->dtvcc); |
4062 | cc_init(&td->cc); |
4063 | td->vbi = vbi_decoder_new(); |
4064 | } |
4065 | |
4066 | void tvcc_destroy(struct tvcc_decoder *td) |
4067 | { |
4068 | if (td){ |
4069 | vbi_decoder_delete(td->vbi); |
4070 | td->vbi = NULL; |
4071 | pthread_mutex_destroy(&td->mutex); |
4072 | } |
4073 | } |
4074 | |
4075 | void tvcc_reset(struct tvcc_decoder *td) |
4076 | { |
4077 | dtvcc_reset(&td->dtvcc); |
4078 | cc_reset(&td->cc); |
4079 | } |
4080 | |
4081 |