| 1 |
/* |
| 2 |
* Copyright (C) 2008-2020 Team Kodi |
| 3 |
* |
| 4 |
* Copyright (C) 2000-2008 the xine project |
| 5 |
* |
| 6 |
* Copyright (C) Christian Vogler |
| 7 |
* [email protected] - December 2001 |
| 8 |
* |
| 9 |
* This file is part of xine, a free video player. |
| 10 |
* |
| 11 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 12 |
* See LICENSES/README.md for more information. |
| 13 |
* |
| 14 |
* stuff needed to provide closed captioning decoding and display |
| 15 |
* |
| 16 |
* Some small bits and pieces of the EIA-608 captioning decoder were |
| 17 |
* adapted from CCDecoder 0.9.1 by Mike Baker. The latest version is |
| 18 |
* available at http://sourceforge.net/projects/ccdecoder/. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <ctype.h> |
| 25 |
|
| 26 |
|
| 27 |
#ifdef __cplusplus |
| 28 |
extern "C" { |
| 29 |
#endif |
| 30 |
|
| 31 |
#include "cc_decoder.h" |
| 32 |
|
| 33 |
/* --------------------- misc. EIA 608 definitions -------------------*/ |
| 34 |
|
| 35 |
/* mapping from PAC row code to actual CC row */ |
| 36 |
static int rowdata[] = {10, -1, 0, 1, 2, 3, 11, 12, 13, 14, 4, 5, 6, 7, 8, 9}; |
| 37 |
|
| 38 |
/* CC codes use odd parity for error detection, since they originally were */ |
| 39 |
/* transmitted via noisy video signals */ |
| 40 |
static int parity_table[256]; |
| 41 |
|
| 42 |
static cc_buffer_t* active_ccbuffer(cc_decoder_t* dec); |
| 43 |
|
| 44 |
/* --------------------- EIA 608 charsets -----------------------------*/ |
| 45 |
// for all charsets, CC codes are essentially identical to ASCII apart for a few exceptions |
| 46 |
// see https://en.wikipedia.org/wiki/EIA-608#Characters for reference |
| 47 |
|
| 48 |
enum cc_charset |
| 49 |
{ |
| 50 |
CCSET_BASIC_AMERICAN = 0, |
| 51 |
CCSET_SPECIAL_AMERICAN, |
| 52 |
CCSET_EXTENDED_SPANISH_FRENCH_MISC, |
| 53 |
CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH |
| 54 |
}; |
| 55 |
|
| 56 |
const char* get_char_override(uint8_t charset, uint8_t c) |
| 57 |
{ |
| 58 |
if (charset == CCSET_BASIC_AMERICAN) |
| 59 |
{ |
| 60 |
switch (c) |
| 61 |
{ |
| 62 |
case 0x27: |
| 63 |
return u8"\u2019"; |
| 64 |
case 0x2a: |
| 65 |
return u8"\u00e1"; |
| 66 |
case 0x5c: |
| 67 |
return u8"\u00e9"; |
| 68 |
case 0x5e: |
| 69 |
return u8"\u00ed"; |
| 70 |
case 0x5f: |
| 71 |
return u8"\u00f3"; |
| 72 |
case 0x60: |
| 73 |
return u8"\u00fa"; |
| 74 |
case 0x7b: |
| 75 |
return u8"\u00e7"; |
| 76 |
case 0x7c: |
| 77 |
return u8"\u00f7"; |
| 78 |
case 0x7d: |
| 79 |
return u8"\u00d1"; |
| 80 |
case 0x7e: |
| 81 |
return u8"\u00f1"; |
| 82 |
case 0x7f: |
| 83 |
return u8"\u2588"; |
| 84 |
default: |
| 85 |
break; |
| 86 |
} |
| 87 |
} |
| 88 |
else if (charset == CCSET_SPECIAL_AMERICAN) |
| 89 |
{ |
| 90 |
switch (c) |
| 91 |
{ |
| 92 |
case 0x30: |
| 93 |
return u8"\u00ae"; |
| 94 |
case 0x31: |
| 95 |
return u8"\u00b0"; |
| 96 |
case 0x32: |
| 97 |
return u8"\u00bd"; |
| 98 |
case 0x33: |
| 99 |
return u8"\u00bf"; |
| 100 |
case 0x34: |
| 101 |
return u8"\u2122"; |
| 102 |
case 0x35: |
| 103 |
return u8"\u00a2"; |
| 104 |
case 0x36: |
| 105 |
return u8"\u00a3"; |
| 106 |
case 0x37: |
| 107 |
return u8"\u266a"; |
| 108 |
case 0x38: |
| 109 |
return u8"\u00e0"; |
| 110 |
case 0x39: |
| 111 |
return u8"\u00A0"; |
| 112 |
case 0x3a: |
| 113 |
return u8"\u00e8"; |
| 114 |
case 0x3b: |
| 115 |
return u8"\u00e2"; |
| 116 |
case 0x3c: |
| 117 |
return u8"\u00ea"; |
| 118 |
case 0x3d: |
| 119 |
return u8"\u00ee"; |
| 120 |
case 0x3e: |
| 121 |
return u8"\u00f4"; |
| 122 |
case 0x3f: |
| 123 |
return u8"\u00fb"; |
| 124 |
default: |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
else if (charset == CCSET_EXTENDED_SPANISH_FRENCH_MISC) |
| 129 |
{ |
| 130 |
switch (c) |
| 131 |
{ |
| 132 |
case 0x20: |
| 133 |
return u8"\u00c1"; |
| 134 |
case 0x21: |
| 135 |
return u8"\u00c9"; |
| 136 |
case 0x22: |
| 137 |
return u8"\u00d3"; |
| 138 |
case 0x23: |
| 139 |
return u8"\u00da"; |
| 140 |
case 0x24: |
| 141 |
return u8"\u00dc"; |
| 142 |
case 0x25: |
| 143 |
return u8"\u00fc"; |
| 144 |
case 0x26: |
| 145 |
return u8"\u00b4"; |
| 146 |
case 0x27: |
| 147 |
return u8"\u00a1"; |
| 148 |
case 0x28: |
| 149 |
return u8"*"; |
| 150 |
case 0x29: |
| 151 |
return u8"\u2018"; |
| 152 |
case 0x2a: |
| 153 |
return u8"-"; |
| 154 |
case 0x2b: |
| 155 |
return u8"\u00a9"; |
| 156 |
case 0x2c: |
| 157 |
return u8"\u2120"; |
| 158 |
case 0x2d: |
| 159 |
return u8"\u00b7"; |
| 160 |
case 0x2e: |
| 161 |
return u8"\u201c"; |
| 162 |
case 0x2f: |
| 163 |
return u8"\u201d"; |
| 164 |
case 0x30: |
| 165 |
return u8"\u00c0"; |
| 166 |
case 0x31: |
| 167 |
return u8"\u00c2"; |
| 168 |
case 0x32: |
| 169 |
return u8"\u00c7"; |
| 170 |
case 0x33: |
| 171 |
return u8"\u00c8"; |
| 172 |
case 0x34: |
| 173 |
return u8"\u00ca"; |
| 174 |
case 0x35: |
| 175 |
return u8"\u00cb"; |
| 176 |
case 0x36: |
| 177 |
return u8"\u00eb"; |
| 178 |
case 0x37: |
| 179 |
return u8"\u00ce"; |
| 180 |
case 0x38: |
| 181 |
return u8"\u00cf"; |
| 182 |
case 0x39: |
| 183 |
return u8"\u00ef"; |
| 184 |
case 0x3a: |
| 185 |
return u8"\u00d4"; |
| 186 |
case 0x3b: |
| 187 |
return u8"\u00d9"; |
| 188 |
case 0x3c: |
| 189 |
return u8"\u00f9"; |
| 190 |
case 0x3d: |
| 191 |
return u8"\u00db"; |
| 192 |
case 0x3e: |
| 193 |
return u8"\u00ab"; |
| 194 |
case 0x3f: |
| 195 |
return u8"\u00bb"; |
| 196 |
default: |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
else if (charset == CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH) |
| 201 |
{ |
| 202 |
switch (c) |
| 203 |
{ |
| 204 |
case 0x20: |
| 205 |
return u8"\u00c3"; |
| 206 |
case 0x21: |
| 207 |
return u8"\u00e3"; |
| 208 |
case 0x22: |
| 209 |
return u8"\u00cd"; |
| 210 |
case 0x23: |
| 211 |
return u8"\u00cc"; |
| 212 |
case 0x24: |
| 213 |
return u8"\u00ec"; |
| 214 |
case 0x25: |
| 215 |
return u8"\u00d2"; |
| 216 |
case 0x26: |
| 217 |
return u8"\u00f2"; |
| 218 |
case 0x27: |
| 219 |
return u8"\u00d5"; |
| 220 |
case 0x28: |
| 221 |
return u8"\u00f5"; |
| 222 |
case 0x29: |
| 223 |
return u8"{"; |
| 224 |
case 0x2a: |
| 225 |
return u8"}"; |
| 226 |
case 0x2b: |
| 227 |
return u8"\\"; |
| 228 |
case 0x2c: |
| 229 |
return u8"^"; |
| 230 |
case 0x2d: |
| 231 |
return u8"_"; |
| 232 |
case 0x2e: |
| 233 |
return u8"|"; |
| 234 |
case 0x2f: |
| 235 |
return u8"~"; |
| 236 |
case 0x30: |
| 237 |
return u8"\u00c4"; |
| 238 |
case 0x31: |
| 239 |
return u8"\u00e4"; |
| 240 |
case 0x32: |
| 241 |
return u8"\u00d6"; |
| 242 |
case 0x33: |
| 243 |
return u8"\u00f6"; |
| 244 |
case 0x34: |
| 245 |
return u8"\u00df"; |
| 246 |
case 0x35: |
| 247 |
return u8"\u00a5"; |
| 248 |
case 0x36: |
| 249 |
return u8"\u00a4"; |
| 250 |
case 0x37: |
| 251 |
return u8"\u00a6"; |
| 252 |
case 0x38: |
| 253 |
return u8"\u00c5"; |
| 254 |
case 0x39: |
| 255 |
return u8"\u00e5"; |
| 256 |
case 0x3a: |
| 257 |
return u8"\u00d8"; |
| 258 |
case 0x3b: |
| 259 |
return u8"\u00f8"; |
| 260 |
case 0x3c: |
| 261 |
return u8"\u250c"; |
| 262 |
case 0x3d: |
| 263 |
return u8"\u2510"; |
| 264 |
case 0x3e: |
| 265 |
return u8"\u2514"; |
| 266 |
case 0x3f: |
| 267 |
return u8"\u2518"; |
| 268 |
default: |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
// regular ascii char |
| 273 |
return NULL; |
| 274 |
} |
| 275 |
|
| 276 |
/*---------------- general utility functions ---------------------*/ |
| 277 |
|
| 278 |
static int parity(uint8_t byte) |
| 279 |
{ |
| 280 |
int i; |
| 281 |
int ones = 0; |
| 282 |
|
| 283 |
for (i = 0; i < 7; i++) { |
| 284 |
if (byte & (1 << i)) |
| 285 |
ones++; |
| 286 |
} |
| 287 |
|
| 288 |
return ones & 1; |
| 289 |
} |
| 290 |
|
| 291 |
static void build_parity_table(void) |
| 292 |
{ |
| 293 |
uint8_t byte; |
| 294 |
for (byte = 0; byte <= 127; byte++) { |
| 295 |
int parity_v = parity(byte); |
| 296 |
/* CC uses odd parity (i.e., # of 1's in byte is odd.) */ |
| 297 |
parity_table[byte] = parity_v; |
| 298 |
parity_table[byte | 0x80] = !parity_v; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
static int good_parity(uint16_t data) |
| 303 |
{ |
| 304 |
int ret = parity_table[data & 0xff] && parity_table[(data & 0xff00) >> 8]; |
| 305 |
if (! ret) |
| 306 |
printf("Bad parity in EIA-608 data (%x)\n", data); |
| 307 |
return ret; |
| 308 |
} |
| 309 |
|
| 310 |
static void ccbuf_add_char(cc_buffer_t* buf, uint8_t c, uint8_t charset) |
| 311 |
{ |
| 312 |
cc_row_t *rowbuf = &buf->rows[buf->rowpos]; |
| 313 |
int pos = rowbuf->pos; |
| 314 |
int left_displayable = (pos > 0) && (pos <= rowbuf->num_chars); |
| 315 |
|
| 316 |
if (pos >= CC_COLUMNS) |
| 317 |
{ |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
/* midrow PAC attributes are applied only if there is no displayable */ |
| 322 |
/* character to the immediate left. This makes the implementation rather */ |
| 323 |
/* complicated, but this is what the EIA-608 standard specifies. :-( */ |
| 324 |
if (rowbuf->pac_attr_chg && !rowbuf->attr_chg && !left_displayable) |
| 325 |
{ |
| 326 |
rowbuf->attr_chg = 1; |
| 327 |
rowbuf->cells[pos].attributes = rowbuf->pac_attr; |
| 328 |
} |
| 329 |
|
| 330 |
rowbuf->cells[pos].c = c; |
| 331 |
rowbuf->cells[pos].charset = charset; |
| 332 |
rowbuf->cells[pos].midrow_attr = rowbuf->attr_chg; |
| 333 |
rowbuf->pos++; |
| 334 |
|
| 335 |
if (rowbuf->num_chars < rowbuf->pos) |
| 336 |
rowbuf->num_chars = rowbuf->pos; |
| 337 |
|
| 338 |
rowbuf->attr_chg = 0; |
| 339 |
rowbuf->pac_attr_chg = 0; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
static void ccbuf_set_cursor(cc_buffer_t *buf, int row, int column, |
| 344 |
int underline, int italics, int color) |
| 345 |
{ |
| 346 |
cc_row_t *rowbuf = &buf->rows[row]; |
| 347 |
cc_attribute_t attr; |
| 348 |
|
| 349 |
attr.italic = italics; |
| 350 |
attr.underline = underline; |
| 351 |
attr.foreground = color; |
| 352 |
attr.background = BLACK; |
| 353 |
|
| 354 |
rowbuf->pac_attr = attr; |
| 355 |
rowbuf->pac_attr_chg = 1; |
| 356 |
|
| 357 |
buf->rowpos = row; |
| 358 |
rowbuf->pos = column; |
| 359 |
rowbuf->attr_chg = 0; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
static void ccbuf_apply_attribute(cc_buffer_t *buf, cc_attribute_t *attr) |
| 364 |
{ |
| 365 |
cc_row_t *rowbuf = &buf->rows[buf->rowpos]; |
| 366 |
int pos = rowbuf->pos; |
| 367 |
|
| 368 |
rowbuf->attr_chg = 1; |
| 369 |
rowbuf->cells[pos].attributes = *attr; |
| 370 |
/* A midrow attribute always counts as a space */ |
| 371 |
ccbuf_add_char(buf, (unsigned int)' ', CCSET_BASIC_AMERICAN); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
static void ccbuf_tab(cc_buffer_t *buf, int tabsize) |
| 376 |
{ |
| 377 |
cc_row_t *rowbuf = &buf->rows[buf->rowpos]; |
| 378 |
rowbuf->pos += tabsize; |
| 379 |
if (rowbuf->pos > CC_COLUMNS) |
| 380 |
{ |
| 381 |
rowbuf->pos = CC_COLUMNS; |
| 382 |
return; |
| 383 |
} |
| 384 |
/* tabs have no effect on pending PAC attribute changes */ |
| 385 |
} |
| 386 |
|
| 387 |
/*----------------- cc_memory_t methods --------------------------------*/ |
| 388 |
|
| 389 |
static void ccrow_der(cc_row_t *row, int pos) |
| 390 |
{ |
| 391 |
int i; |
| 392 |
for (i = pos; i < CC_COLUMNS; i++) |
| 393 |
{ |
| 394 |
row->cells[i].c = ' '; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
static void ccmem_clear(cc_memory_t *buf) |
| 399 |
{ |
| 400 |
int i; |
| 401 |
memset(buf, 0, sizeof (cc_memory_t)); |
| 402 |
for (i = 0; i < CC_ROWS; i++) |
| 403 |
{ |
| 404 |
ccrow_der(&buf->channel[0].rows[i], 0); |
| 405 |
ccrow_der(&buf->channel[1].rows[i], 0); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
static void ccmem_init(cc_memory_t *buf) |
| 410 |
{ |
| 411 |
ccmem_clear(buf); |
| 412 |
} |
| 413 |
|
| 414 |
static void ccmem_exit(cc_memory_t *buf) |
| 415 |
{ |
| 416 |
/*FIXME: anything to deallocate?*/ |
| 417 |
} |
| 418 |
|
| 419 |
static void cea608_process_style_modifiers(cc_decoder_t* dec) |
| 420 |
{ |
| 421 |
const int channel_no = dec->on_buf->channel_no; |
| 422 |
const int row_pos = dec->on_buf->channel[channel_no].rowpos; |
| 423 |
const cc_row_t current_row = dec->on_buf->channel[channel_no].rows[row_pos]; |
| 424 |
// ANSI CTA-608E: |
| 425 |
// If, when a displayable character is received, it overwrites an existing PAC |
| 426 |
// or mid-row code, and there are already characters to the right of the new character, |
| 427 |
// these existing characters shall assume the same attributes as the new character. |
| 428 |
// This adoption can result in a whole caption row suddenly changing color, |
| 429 |
// underline, italics, and/or flash attributes. |
| 430 |
const int cursor_position = current_row.pos; |
| 431 |
// default style: white, black background, no italic, no underline |
| 432 |
cc_attribute_t textattr = {0, 0, WHITE, BLACK}; |
| 433 |
for (int pos = cursor_position; pos >= 0 && pos <= CC_COLUMNS; pos--) |
| 434 |
{ |
| 435 |
const cc_attribute_t ccAttribute = current_row.cells[pos].attributes; |
| 436 |
if (ccAttribute.italic > 0) |
| 437 |
{ |
| 438 |
// italics resets color to white |
| 439 |
textattr.foreground = WHITE; |
| 440 |
textattr.italic = 1; |
| 441 |
} |
| 442 |
if (ccAttribute.underline > 0) |
| 443 |
{ |
| 444 |
textattr.underline = 1; |
| 445 |
} |
| 446 |
if (ccAttribute.foreground > WHITE && ccAttribute.foreground <= BLACK && textattr.italic == 0) |
| 447 |
{ |
| 448 |
textattr.foreground = ccAttribute.foreground; |
| 449 |
} |
| 450 |
} |
| 451 |
dec->textattr = textattr; |
| 452 |
} |
| 453 |
|
| 454 |
void ccmem_tobuf(cc_decoder_t *dec) |
| 455 |
{ |
| 456 |
cc_buffer_t *buf = &dec->on_buf->channel[dec->on_buf->channel_no]; |
| 457 |
int empty = 1; |
| 458 |
dec->textlen = 0; |
| 459 |
int i,j; |
| 460 |
for (i = 0; i < CC_ROWS; i++) |
| 461 |
{ |
| 462 |
for (j = 0; j<CC_COLUMNS; j++) |
| 463 |
if (buf->rows[i].cells[j].c != ' ') |
| 464 |
{ |
| 465 |
empty = 0; |
| 466 |
break; |
| 467 |
} |
| 468 |
if (!empty) |
| 469 |
break; |
| 470 |
} |
| 471 |
if (empty) |
| 472 |
return; // Nothing to write |
| 473 |
|
| 474 |
for (i = 0; i<CC_ROWS; i++) |
| 475 |
{ |
| 476 |
int empty = 1; |
| 477 |
for (j = 0; j<CC_COLUMNS; j++) |
| 478 |
if (buf->rows[i].cells[j].c != ' ') |
| 479 |
empty = 0; |
| 480 |
if (!empty) |
| 481 |
{ |
| 482 |
int f, l; // First,last used char |
| 483 |
for (f = 0; f<CC_COLUMNS; f++) |
| 484 |
if (buf->rows[i].cells[f].c != ' ') |
| 485 |
break; |
| 486 |
for (l = CC_COLUMNS-1; l>0; l--) |
| 487 |
if (buf->rows[i].cells[l].c != ' ') |
| 488 |
break; |
| 489 |
for (j = f; j <= l; j++) |
| 490 |
{ |
| 491 |
const char* chbytes = |
| 492 |
get_char_override(buf->rows[i].cells[j].charset, buf->rows[i].cells[j].c); |
| 493 |
if (chbytes != NULL) |
| 494 |
{ |
| 495 |
for (; *chbytes != '\0'; chbytes++) |
| 496 |
{ |
| 497 |
dec->text[dec->textlen++] = *chbytes; |
| 498 |
} |
| 499 |
} |
| 500 |
else |
| 501 |
{ |
| 502 |
// ascii char |
| 503 |
dec->text[dec->textlen++] = (unsigned char)buf->rows[i].cells[j].c; |
| 504 |
} |
| 505 |
} |
| 506 |
dec->text[dec->textlen++] = '\n'; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
// FIXME: the end-of-string char is often wrong cause unexpected behaviours |
| 511 |
if (dec->textlen > 0) |
| 512 |
{ |
| 513 |
if (dec->text[dec->textlen - 1] == '\n' && dec->text[dec->textlen] != '\0') |
| 514 |
{ |
| 515 |
dec->text[dec->textlen] = '\0'; |
| 516 |
} |
| 517 |
else if (dec->text[dec->textlen] != '\0') |
| 518 |
{ |
| 519 |
dec->text[dec->textlen++] = '\n'; |
| 520 |
dec->text[dec->textlen++] = '\0'; |
| 521 |
} |
| 522 |
} |
| 523 |
cea608_process_style_modifiers(dec); |
| 524 |
dec->callback(0, dec->userdata); |
| 525 |
} |
| 526 |
|
| 527 |
/*----------------- cc_decoder_t methods --------------------------------*/ |
| 528 |
|
| 529 |
static void cc_set_channel(cc_decoder_t *dec, int channel) |
| 530 |
{ |
| 531 |
(*dec->active)->channel_no = channel; |
| 532 |
} |
| 533 |
|
| 534 |
static cc_buffer_t *active_ccbuffer(cc_decoder_t *dec) |
| 535 |
{ |
| 536 |
cc_memory_t *mem = *dec->active; |
| 537 |
return &mem->channel[mem->channel_no]; |
| 538 |
} |
| 539 |
|
| 540 |
static void cc_hide_displayed(cc_decoder_t* dec) |
| 541 |
{ |
| 542 |
for (int i = 0; i < CC_ROWS * CC_COLUMNS + 1; i++) |
| 543 |
{ |
| 544 |
dec->text[i] = '\0'; |
| 545 |
} |
| 546 |
dec->callback(0, dec->userdata); |
| 547 |
} |
| 548 |
|
| 549 |
static void cc_swap_buffers(cc_decoder_t *dec) |
| 550 |
{ |
| 551 |
cc_memory_t *temp; |
| 552 |
|
| 553 |
/* hide caption in displayed memory */ |
| 554 |
cc_hide_displayed(dec); |
| 555 |
|
| 556 |
temp = dec->on_buf; |
| 557 |
dec->on_buf = dec->off_buf; |
| 558 |
dec->off_buf = temp; |
| 559 |
|
| 560 |
/* show new displayed memory */ |
| 561 |
/* cc_show_displayed(dec); */ |
| 562 |
} |
| 563 |
|
| 564 |
static void cc_roll_up(cc_decoder_t *dec) |
| 565 |
{ |
| 566 |
cc_buffer_t *buf = active_ccbuffer(dec); |
| 567 |
int i, j; |
| 568 |
for (i = buf->rowpos - dec->rollup_rows + 1; i < buf->rowpos; i++) |
| 569 |
{ |
| 570 |
if (i < 0) |
| 571 |
continue; |
| 572 |
|
| 573 |
for (j = 0; j < CC_COLUMNS; j++) |
| 574 |
{ |
| 575 |
buf->rows[i].cells[j] = buf->rows[i + 1].cells[j]; |
| 576 |
} |
| 577 |
} |
| 578 |
for (j = 0; j < CC_COLUMNS; j++) |
| 579 |
{ |
| 580 |
buf->rows[buf->rowpos].cells[j].c = ' '; |
| 581 |
} |
| 582 |
buf->rows[buf->rowpos].pos = 0; |
| 583 |
} |
| 584 |
|
| 585 |
static void cc_decode_standard_char(cc_decoder_t *dec, uint8_t c1, uint8_t c2) |
| 586 |
{ |
| 587 |
cc_buffer_t *buf = active_ccbuffer(dec); |
| 588 |
/* c1 always is a valid character */ |
| 589 |
ccbuf_add_char(buf, c1, CCSET_BASIC_AMERICAN); |
| 590 |
/* c2 might not be a printable character, even if c1 was */ |
| 591 |
if (c2 & 0x60) |
| 592 |
ccbuf_add_char(buf, c2, CCSET_BASIC_AMERICAN); |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
static void cc_decode_PAC(cc_decoder_t *dec, int channel, |
| 597 |
uint8_t c1, uint8_t c2) |
| 598 |
{ |
| 599 |
cc_buffer_t *buf; |
| 600 |
int row, column = 0; |
| 601 |
int underline, italics = 0, color; |
| 602 |
|
| 603 |
/* There is one invalid PAC code combination. Ignore it. */ |
| 604 |
if (c1 == 0x10 && c2 > 0x5f) |
| 605 |
return; |
| 606 |
|
| 607 |
cc_set_channel(dec, channel); |
| 608 |
buf = active_ccbuffer(dec); |
| 609 |
|
| 610 |
row = rowdata[((c1 & 0x07) << 1) | ((c2 & 0x20) >> 5)]; |
| 611 |
if (c2 & 0x10) |
| 612 |
{ |
| 613 |
column = ((c2 & 0x0e) >> 1) * 4; /* preamble indentation */ |
| 614 |
color = WHITE; /* indented lines have white color */ |
| 615 |
} |
| 616 |
else if ((c2 & 0x0e) == 0x0e) |
| 617 |
{ |
| 618 |
italics = 1; /* italics, they are always white */ |
| 619 |
color = WHITE; |
| 620 |
} |
| 621 |
else |
| 622 |
color = (c2 & 0x0e) >> 1; |
| 623 |
underline = c2 & 0x01; |
| 624 |
|
| 625 |
ccbuf_set_cursor(buf, row, column, underline, italics, color); |
| 626 |
} |
| 627 |
|
| 628 |
static void cc_decode_ext_attribute(cc_decoder_t* dec, int channel) |
| 629 |
{ |
| 630 |
cc_set_channel(dec, channel); |
| 631 |
} |
| 632 |
|
| 633 |
static void cc_decode_special_char(cc_decoder_t* dec, int channel, uint8_t charset, uint8_t c2) |
| 634 |
{ |
| 635 |
cc_buffer_t *buf; |
| 636 |
|
| 637 |
cc_set_channel(dec, channel); |
| 638 |
buf = active_ccbuffer(dec); |
| 639 |
ccbuf_add_char(buf, c2, charset); |
| 640 |
} |
| 641 |
|
| 642 |
static void cc_decode_midrow_attr(cc_decoder_t* dec, int channel, uint8_t c2) |
| 643 |
{ |
| 644 |
cc_buffer_t *buf; |
| 645 |
cc_attribute_t attr; |
| 646 |
|
| 647 |
cc_set_channel(dec, channel); |
| 648 |
buf = active_ccbuffer(dec); |
| 649 |
if (c2 < 0x2e) |
| 650 |
{ |
| 651 |
attr.italic = 0; |
| 652 |
attr.foreground = (c2 & 0xe) >> 1; |
| 653 |
} |
| 654 |
else |
| 655 |
{ |
| 656 |
attr.italic = 1; |
| 657 |
attr.foreground = WHITE; |
| 658 |
} |
| 659 |
attr.underline = c2 & 0x1; |
| 660 |
attr.background = BLACK; |
| 661 |
|
| 662 |
ccbuf_apply_attribute(buf, &attr); |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
static void cc_decode_misc_control_code(cc_decoder_t* dec, int channel, uint8_t c2) |
| 667 |
{ |
| 668 |
cc_set_channel(dec, channel); |
| 669 |
cc_buffer_t *buf; |
| 670 |
|
| 671 |
switch (c2) |
| 672 |
{ /* 0x20 <= c2 <= 0x2f */ |
| 673 |
case 0x20: /* RCL */ |
| 674 |
dec->style = CC_POPON; |
| 675 |
dec->active = &dec->off_buf; |
| 676 |
break; |
| 677 |
|
| 678 |
case 0x21: /* backspace */ |
| 679 |
break; |
| 680 |
|
| 681 |
case 0x24: /* DER */ |
| 682 |
buf = active_ccbuffer(dec); |
| 683 |
ccrow_der(&buf->rows[buf->rowpos], buf->rows[buf->rowpos].pos); |
| 684 |
break; |
| 685 |
|
| 686 |
case 0x25: /* RU2 */ |
| 687 |
dec->rollup_rows = 2; |
| 688 |
dec->style = CC_ROLLUP; |
| 689 |
dec->active = &dec->on_buf; |
| 690 |
break; |
| 691 |
|
| 692 |
case 0x26: /* RU3 */ |
| 693 |
dec->rollup_rows = 3; |
| 694 |
dec->style = CC_ROLLUP; |
| 695 |
dec->active = &dec->on_buf; |
| 696 |
break; |
| 697 |
|
| 698 |
case 0x27: /* RU4 */ |
| 699 |
dec->rollup_rows = 4; |
| 700 |
dec->style = CC_ROLLUP; |
| 701 |
dec->active = &dec->on_buf; |
| 702 |
break; |
| 703 |
|
| 704 |
case 0x28: /* FON */ |
| 705 |
break; |
| 706 |
|
| 707 |
case 0x29: /* RDC */ |
| 708 |
dec->style = CC_PAINTON; |
| 709 |
dec->active = &dec->on_buf; |
| 710 |
break; |
| 711 |
|
| 712 |
case 0x2a: /* TR */ |
| 713 |
break; |
| 714 |
|
| 715 |
case 0x2b: /* RTD */ |
| 716 |
break; |
| 717 |
|
| 718 |
case 0x2c: /* EDM - erase displayed memory */ |
| 719 |
cc_hide_displayed(dec); |
| 720 |
ccmem_clear(dec->on_buf); |
| 721 |
break; |
| 722 |
|
| 723 |
case 0x2d: /* carriage return */ |
| 724 |
if (dec->style == CC_ROLLUP) |
| 725 |
{ |
| 726 |
cc_roll_up(dec); |
| 727 |
} |
| 728 |
break; |
| 729 |
|
| 730 |
case 0x2e: /* ENM - erase non-displayed memory */ |
| 731 |
ccmem_clear(dec->off_buf); |
| 732 |
break; |
| 733 |
|
| 734 |
case 0x2f: /* EOC - swap displayed and non displayed memory */ |
| 735 |
cc_swap_buffers(dec); |
| 736 |
dec->style = CC_POPON; |
| 737 |
dec->active = &dec->off_buf; |
| 738 |
ccmem_tobuf(dec); |
| 739 |
break; |
| 740 |
|
| 741 |
default: |
| 742 |
break; |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
static void cc_decode_tab(cc_decoder_t* dec, int channel, uint8_t c2) |
| 747 |
{ |
| 748 |
cc_buffer_t *buf; |
| 749 |
|
| 750 |
cc_set_channel(dec, channel); |
| 751 |
buf = active_ccbuffer(dec); |
| 752 |
ccbuf_tab(buf, c2 & 0x3); |
| 753 |
} |
| 754 |
|
| 755 |
static void cc_decode_EIA608(cc_decoder_t *dec, uint16_t data) |
| 756 |
{ |
| 757 |
uint8_t c1 = data & 0x7f; |
| 758 |
uint8_t c2 = (data >> 8) & 0x7f; |
| 759 |
|
| 760 |
if (c1 & 0x60) |
| 761 |
{ /* normal character, 0x20 <= c1 <= 0x7f */ |
| 762 |
if (dec->style == CC_NOTSET) |
| 763 |
return; |
| 764 |
|
| 765 |
cc_decode_standard_char(dec, c1, c2); |
| 766 |
if (dec->style == CC_ROLLUP) |
| 767 |
{ |
| 768 |
ccmem_tobuf(dec); |
| 769 |
} |
| 770 |
} |
| 771 |
else if (c1 & 0x10) |
| 772 |
{ |
| 773 |
/* control code or special character */ |
| 774 |
/* 0x10 <= c1 <= 0x1f */ |
| 775 |
int channel = (c1 & 0x08) >> 3; |
| 776 |
c1 &= ~0x08; |
| 777 |
|
| 778 |
/* control sequences are often repeated. In this case, we should */ |
| 779 |
/* evaluate it only once. */ |
| 780 |
if (data != dec->lastcode) |
| 781 |
{ |
| 782 |
if (c2 & 0x40) |
| 783 |
{ /* preamble address code: 0x40 <= c2 <= 0x7f */ |
| 784 |
cc_decode_PAC(dec, channel, c1, c2); |
| 785 |
} |
| 786 |
else |
| 787 |
{ |
| 788 |
switch (c1) |
| 789 |
{ |
| 790 |
case 0x10: /* extended background attribute code */ |
| 791 |
cc_decode_ext_attribute(dec, channel); |
| 792 |
break; |
| 793 |
|
| 794 |
case 0x11: /* attribute or special character */ |
| 795 |
if (dec->style == CC_NOTSET) |
| 796 |
return; |
| 797 |
|
| 798 |
if ((c2 & 0x30) == 0x30) |
| 799 |
{ |
| 800 |
/* special char: 0x30 <= c2 <= 0x3f */ |
| 801 |
/* CCSET_SPECIAL_AMERICAN */ |
| 802 |
cc_decode_special_char(dec, channel, CCSET_SPECIAL_AMERICAN, c2); |
| 803 |
if (dec->style == CC_ROLLUP) |
| 804 |
{ |
| 805 |
ccmem_tobuf(dec); |
| 806 |
} |
| 807 |
} |
| 808 |
else if (c2 & 0x20) |
| 809 |
{ |
| 810 |
/* midrow attribute: 0x20 <= c2 <= 0x2f */ |
| 811 |
cc_decode_midrow_attr(dec, channel, c2); |
| 812 |
} |
| 813 |
break; |
| 814 |
|
| 815 |
case 0x12: /* CCSET_EXTENDED_SPANISH_FRENCH_MISC */ |
| 816 |
cc_decode_special_char(dec, channel, CCSET_EXTENDED_SPANISH_FRENCH_MISC, c2); |
| 817 |
if (dec->style == CC_ROLLUP) |
| 818 |
{ |
| 819 |
ccmem_tobuf(dec); |
| 820 |
} |
| 821 |
break; |
| 822 |
|
| 823 |
case 0x13: /* CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH */ |
| 824 |
cc_decode_special_char(dec, channel, CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH, c2); |
| 825 |
if (dec->style == CC_ROLLUP) |
| 826 |
{ |
| 827 |
ccmem_tobuf(dec); |
| 828 |
} |
| 829 |
break; |
| 830 |
|
| 831 |
case 0x14: /* possibly miscellaneous control code */ |
| 832 |
cc_decode_misc_control_code(dec, channel, c2); |
| 833 |
break; |
| 834 |
|
| 835 |
case 0x17: /* possibly misc. control code TAB offset */ |
| 836 |
/* 0x21 <= c2 <= 0x23 */ |
| 837 |
if (c2 >= 0x21 && c2 <= 0x23) |
| 838 |
{ |
| 839 |
cc_decode_tab(dec, channel, c2); |
| 840 |
} |
| 841 |
break; |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
dec->lastcode = data; |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
void decode_cc(cc_decoder_t *dec, const uint8_t *buffer, uint32_t buf_len) |
| 852 |
{ |
| 853 |
uint32_t i; |
| 854 |
for (i = 0; i<buf_len; i += 3) |
| 855 |
{ |
| 856 |
unsigned char cc_type = buffer[i] & 0x03; |
| 857 |
|
| 858 |
uint8_t data1 = buffer[i + 1]; |
| 859 |
uint8_t data2 = buffer[i + 2]; |
| 860 |
|
| 861 |
switch (cc_type) |
| 862 |
{ |
| 863 |
case 0: |
| 864 |
if (good_parity(data1 | (data2 << 8))) |
| 865 |
{ |
| 866 |
cc_decode_EIA608(dec, data1 | (data2 << 8)); |
| 867 |
} |
| 868 |
break; |
| 869 |
|
| 870 |
case 1: |
| 871 |
break; |
| 872 |
|
| 873 |
default: |
| 874 |
break; |
| 875 |
} |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
cc_decoder_t *cc_decoder_open() |
| 880 |
{ |
| 881 |
cc_decoder_t *dec = (cc_decoder_t *) calloc(1, sizeof (cc_decoder_t)); |
| 882 |
if (!dec) |
| 883 |
return NULL; |
| 884 |
|
| 885 |
ccmem_init(&dec->buffer[0]); |
| 886 |
ccmem_init(&dec->buffer[1]); |
| 887 |
dec->on_buf = &dec->buffer[0]; |
| 888 |
dec->off_buf = &dec->buffer[1]; |
| 889 |
dec->active = &dec->off_buf; |
| 890 |
|
| 891 |
dec->lastcode = 0; |
| 892 |
|
| 893 |
return dec; |
| 894 |
} |
| 895 |
|
| 896 |
void cc_decoder_close(cc_decoder_t *dec) |
| 897 |
{ |
| 898 |
ccmem_exit(&dec->buffer[0]); |
| 899 |
ccmem_exit(&dec->buffer[1]); |
| 900 |
|
| 901 |
free(dec); |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
/*--------------- initialization methods --------------------------*/ |
| 906 |
|
| 907 |
void cc_decoder_init(void) |
| 908 |
{ |
| 909 |
build_parity_table(); |
| 910 |
} |
| 911 |
|
| 912 |
#ifdef __cplusplus |
| 913 |
} |
| 914 |
#endif |
| 915 |
|