Debian Codegraph @

xbmc/cores/VideoPlayer/DVDCodecs/Overlay/contrib/cc_decoder708.cpp

raw
1 /*
2 * this is mostly borrowed from ccextractor http://ccextractor.sourceforge.net/
3 */
4
5 #include "cc_decoder708.h"
6
7 #include "utils/log.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 /********************************************************
14 256 BYTES IS ENOUGH FOR ALL THE SUPPORTED CHARACTERS IN
15 EIA-708, SO INTERNALLY WE USE THIS TABLE (FOR CONVENIENCE)
16
17 00-1F -> Characters that are in the G2 group in 20-3F,
18 except for 06, which is used for the closed captions
19 sign "CC" which is defined in group G3 as 00. (this
20 is by the article 33).
21 20-7F -> Group G0 as is - corresponds to the ASCII code
22 80-9F -> Characters that are in the G2 group in 60-7F
23 (there are several blank characters here, that's OK)
24 A0-FF -> Group G1 as is - non-English characters and symbols
25 */
26
27 unsigned char get_internal_from_G0 (unsigned char g0_char)
28 {
29 return g0_char;
30 }
31
32 unsigned char get_internal_from_G1 (unsigned char g1_char)
33 {
34 return g1_char;
35 }
36
37 //! @todo Probably not right
38 // G2: Extended Control Code Set 1
39 unsigned char get_internal_from_G2 (unsigned char g2_char)
40 {
41 // according to the comment a few lines above those lines are indeed wrong
42 /*
43 if (g2_char>=0x20 && g2_char<=0x3F)
44 return g2_char-0x20;
45 if (g2_char>=0x60 && g2_char<=0x7F)
46 return g2_char+0x20;
47 */
48 // Rest unmapped, so we return a blank space
49 return 0x20;
50 }
51
52 //! @todo Probably not right
53 // G3: Future Characters and Icon Expansion
54 unsigned char get_internal_from_G3 (unsigned char g3_char)
55 {
56 if (g3_char==0xa0) // The "CC" (closed captions) sign
57 return 0x06;
58 // Rest unmapped, so we return a blank space
59 return 0x20;
60 }
61
62 void clearTV (cc708_service_decoder *decoder);
63
64 const char *COMMANDS_C0[32]=
65 {
66 "NUL", // 0 = NUL
67 NULL, // 1 = Reserved
68 NULL, // 2 = Reserved
69 "ETX", // 3 = ETX
70 NULL, // 4 = Reserved
71 NULL, // 5 = Reserved
72 NULL, // 6 = Reserved
73 NULL, // 7 = Reserved
74 "BS", // 8 = Backspace
75 NULL, // 9 = Reserved
76 NULL, // A = Reserved
77 NULL, // B = Reserved
78 "FF", // C = FF
79 "CR", // D = CR
80 "HCR", // E = HCR
81 NULL, // F = Reserved
82 "EXT1",// 0x10 = EXT1,
83 NULL, // 0x11 = Reserved
84 NULL, // 0x12 = Reserved
85 NULL, // 0x13 = Reserved
86 NULL, // 0x14 = Reserved
87 NULL, // 0x15 = Reserved
88 NULL, // 0x16 = Reserved
89 NULL, // 0x17 = Reserved
90 "P16", // 0x18 = P16
91 NULL, // 0x19 = Reserved
92 NULL, // 0x1A = Reserved
93 NULL, // 0x1B = Reserved
94 NULL, // 0x1C = Reserved
95 NULL, // 0x1D = Reserved
96 NULL, // 0x1E = Reserved
97 NULL, // 0x1F = Reserved
98 };
99
100 struct S_COMMANDS_C1 COMMANDS_C1[32]=
101 {
102 {CW0,"CW0","SetCurrentWindow0", 1},
103 {CW1,"CW1","SetCurrentWindow1", 1},
104 {CW2,"CW2","SetCurrentWindow2", 1},
105 {CW3,"CW3","SetCurrentWindow3", 1},
106 {CW4,"CW4","SetCurrentWindow4", 1},
107 {CW5,"CW5","SetCurrentWindow5", 1},
108 {CW6,"CW6","SetCurrentWindow6", 1},
109 {CW7,"CW7","SetCurrentWindow7", 1},
110 {CLW,"CLW","ClearWindows", 2},
111 {DSW,"DSW","DisplayWindows", 2},
112 {HDW,"HDW","HideWindows", 2},
113 {TGW,"TGW","ToggleWindows", 2},
114 {DLW,"DLW","DeleteWindows", 2},
115 {DLY,"DLY","Delay", 2},
116 {DLC,"DLC","DelayCancel", 1},
117 {RST,"RST","Reset", 1},
118 {SPA,"SPA","SetPenAttributes", 3},
119 {SPC,"SPC","SetPenColor", 4},
120 {SPL,"SPL","SetPenLocation", 3},
121 {RSV93,"RSV93","Reserved", 1},
122 {RSV94,"RSV94","Reserved", 1},
123 {RSV95,"RSV95","Reserved", 1},
124 {RSV96,"RSV96","Reserved", 1},
125 {SWA,"SWA","SetWindowAttributes", 5},
126 {DF0,"DF0","DefineWindow0", 7},
127 {DF1,"DF0","DefineWindow1", 7},
128 {DF2,"DF0","DefineWindow2", 7},
129 {DF3,"DF0","DefineWindow3", 7},
130 {DF4,"DF0","DefineWindow4", 7},
131 {DF5,"DF0","DefineWindow5", 7},
132 {DF6,"DF0","DefineWindow6", 7},
133 {DF7,"DF0","DefineWindow7", 7}
134 };
135
136 void clear_packet(cc708_service_decoder *decoder)
137 {
138 decoder->parent->m_current_packet_length = 0;
139 }
140
141 void cc708_service_reset(cc708_service_decoder *decoder)
142 {
143 // There's lots of other stuff that we need to do, such as canceling delays
144 for (e708Window& window : decoder->windows)
145 {
146 window.is_defined=0;
147 window.visible=0;
148 window.memory_reserved=0;
149 window.is_empty=1;
150 memset (window.commands, 0,
151 sizeof (window.commands));
152 }
153 decoder->current_window=-1;
154 clearTV(decoder);
155 decoder->inited=1;
156 }
157
158 void cc708_reset(cc708_service_decoder *decoders)
159 {
160 for (int i = 0; i<CCX_DECODERS_708_MAX_SERVICES; i++)
161 {
162 cc708_service_reset (&decoders[i]);
163 }
164 // Empty packet buffer
165 clear_packet(&decoders[0]);
166 decoders[0].parent->m_last_seq = -1;
167 }
168
169 int compWindowsPriorities (const void *a, const void *b)
170 {
171 const e708Window *w1=*(e708Window * const*)a;
172 const e708Window *w2=*(e708Window * const*)b;
173 return w1->priority-w2->priority;
174 }
175
176 void clearTV (cc708_service_decoder *decoder)
177 {
178 for (unsigned char (&row)[I708_SCREENGRID_COLUMNS + 1] : decoder->tv.chars)
179 {
180 memset (row, ' ', I708_SCREENGRID_COLUMNS);
181 row[I708_SCREENGRID_COLUMNS]=0;
182 }
183 };
184
185 void printTVtoBuf (cc708_service_decoder *decoder)
186 {
187 int empty=1;
188 decoder->textlen = 0;
189 for (unsigned char(&row)[I708_SCREENGRID_COLUMNS + 1] : decoder->tv.chars)
190 {
191 for (int j=0;j<210;j++)
192 if (row[j] != ' ')
193 {
194 empty=0;
195 break;
196 }
197 if (!empty)
198 break;
199 }
200 if (empty)
201 return; // Nothing to write
202
203 for (unsigned char(&row)[I708_SCREENGRID_COLUMNS + 1] : decoder->tv.chars)
204 {
205 int empty=1;
206 for (int j=0;j<210;j++)
207 if (row[j] != ' ')
208 empty=0;
209 if (!empty)
210 {
211 int f,l; // First,last used char
212 for (f=0;f<210;f++)
213 if (row[f] != ' ')
214 break;
215 for (l=209;l>0;l--)
216 if (row[l]!=' ')
217 break;
218 for (int j=f;j<=l;j++)
219 decoder->text[decoder->textlen++] = row[j];
220 decoder->text[decoder->textlen++] = '\r';
221 decoder->text[decoder->textlen++] = '\n';
222 }
223 }
224
225 // FIXME: the end-of-string char is often wrong cause unexpected behaviours
226 if (decoder->textlen >= 2)
227 {
228 if (decoder->text[decoder->textlen - 2] == '\r' &&
229 decoder->text[decoder->textlen - 1] == '\n' && decoder->text[decoder->textlen] != '\0')
230 {
231 decoder->text[decoder->textlen] = '\0';
232 }
233 else if (decoder->text[decoder->textlen] != '\0')
234 {
235 decoder->text[decoder->textlen++] = '\r';
236 decoder->text[decoder->textlen++] = '\n';
237 decoder->text[decoder->textlen++] = '\0';
238 }
239 }
240 }
241
242 void updateScreen (cc708_service_decoder *decoder)
243 {
244 clearTV (decoder);
245
246 // THIS FUNCTION WILL DO THE MAGIC OF ACTUALLY EXPORTING THE DECODER STATUS
247 // TO SEVERAL FILES
248 e708Window *wnd[I708_MAX_WINDOWS]; // We'll store here the visible windows that contain anything
249 int visible=0;
250 for (e708Window& window : decoder->windows)
251 {
252 if (window.is_defined && window.visible && !window.is_empty)
253 wnd[visible++]=&window;
254 }
255 qsort (wnd,visible,sizeof (e708Window *),compWindowsPriorities);
256
257 for (int i=0;i<visible;i++)
258 {
259 int top,left;
260 // For each window we calculate the top,left position depending on the
261 // anchor
262 switch (wnd[i]->anchor_point)
263 {
264 case anchorpoint_top_left:
265 top=wnd[i]->anchor_vertical;
266 left=wnd[i]->anchor_horizontal;
267 break;
268 case anchorpoint_top_center:
269 top=wnd[i]->anchor_vertical;
270 left=wnd[i]->anchor_horizontal - wnd[i]->col_count/2;
271 break;
272 case anchorpoint_top_right:
273 top=wnd[i]->anchor_vertical;
274 left=wnd[i]->anchor_horizontal - wnd[i]->col_count;
275 break;
276 case anchorpoint_middle_left:
277 top=wnd[i]->anchor_vertical - wnd[i]->row_count/2;
278 left=wnd[i]->anchor_horizontal;
279 break;
280 case anchorpoint_middle_center:
281 top=wnd[i]->anchor_vertical - wnd[i]->row_count/2;
282 left=wnd[i]->anchor_horizontal - wnd[i]->col_count/2;
283 break;
284 case anchorpoint_middle_right:
285 top=wnd[i]->anchor_vertical - wnd[i]->row_count/2;
286 left=wnd[i]->anchor_horizontal - wnd[i]->col_count;
287 break;
288 case anchorpoint_bottom_left:
289 top=wnd[i]->anchor_vertical - wnd[i]->row_count;
290 left=wnd[i]->anchor_horizontal;
291 break;
292 case anchorpoint_bottom_center:
293 top=wnd[i]->anchor_vertical - wnd[i]->row_count;
294 left=wnd[i]->anchor_horizontal - wnd[i]->col_count/2;
295 break;
296 case anchorpoint_bottom_right:
297 top=wnd[i]->anchor_vertical - wnd[i]->row_count;
298 left=wnd[i]->anchor_horizontal - wnd[i]->col_count;
299 break;
300 default: // Shouldn't happen, but skip the window just in case
301 continue;
302 }
303 if (top<0)
304 top=0;
305 if (left<0)
306 left=0;
307 int copyrows=top + wnd[i]->row_count >= I708_SCREENGRID_ROWS ?
308 I708_SCREENGRID_ROWS - top : wnd[i]->row_count;
309 int copycols=left + wnd[i]->col_count >= I708_SCREENGRID_COLUMNS ?
310 I708_SCREENGRID_COLUMNS - left : wnd[i]->col_count;
311 for (int j=0;j<copyrows;j++)
312 {
313 memcpy (decoder->tv.chars[top+j],wnd[i]->rows[j],copycols);
314 }
315 }
316 printTVtoBuf(decoder);
317 decoder->callback(decoder->service, decoder->userdata);
318 }
319
320 void rollupWindow(cc708_service_decoder *decoder, int window)
321 {
322 for (int row=0; row<decoder->windows[window].row_count - 1; row++)
323 {
324 memcpy(decoder->windows[window].rows[row], decoder->windows[window].rows[row+1], decoder->windows[window].col_count);
325 }
326 memset(decoder->windows[window].rows[decoder->windows[window].row_count-1], ' ', decoder->windows[window].col_count);
327 }
328
329 /* This function handles future codes. While by definition we can't do any work on them, we must return
330 how many bytes would be consumed if these codes were supported, as defined in the specs.
331 Note: EXT1 not included */
332 // C2: Extended Miscellaneous Control Codes
333 //! @todo This code is completely untested due to lack of samples. Just following specs!
334 int handle_708_C2 (cc708_service_decoder *decoder, unsigned char *data, int data_length)
335 {
336 if (data[0]<=0x07) // 00-07...
337 return 1; // ... Single-byte control bytes (0 additional bytes)
338 else if (data[0]<=0x0f) // 08-0F ...
339 return 2; // ..two-byte control codes (1 additional byte)
340 else if (data[0]<=0x17) // 10-17 ...
341 return 3; // ..three-byte control codes (2 additional bytes)
342 return 4; // 18-1F => four-byte control codes (3 additional bytes)
343 }
344
345 int handle_708_C3 (cc708_service_decoder *decoder, unsigned char *data, int data_length)
346 {
347 if (data[0] < 0x80 || data[0] > 0x9F)
348 CLog::Log(LOGERROR, "{} - Entry in handle_708_C3 with an out of range value", __FUNCTION__);
349 if (data[0]<=0x87) // 80-87...
350 return 5; // ... Five-byte control bytes (4 additional bytes)
351 else if (data[0]<=0x8F) // 88-8F ...
352 return 6; // ..Six-byte control codes (5 additional byte)
353 // If here, then 90-9F ...
354
355 // These are variable length commands, that can even span several segments
356 // (they allow even downloading fonts or graphics).
357 //! @todo Implement if a sample ever appears
358 return 0; // Unreachable, but otherwise there's compilers warnings
359 }
360
361 // This function handles extended codes (EXT1 + code), from the extended sets
362 // G2 (20-7F) => Mostly unmapped, except for a few characters.
363 // G3 (A0-FF) => A0 is the CC symbol, everything else reserved for future expansion in EIA708-B
364 // C2 (00-1F) => Reserved for future extended misc. control and captions command codes
365 //! @todo This code is completely untested due to lack of samples. Just following specs!
366 // Returns number of used bytes, usually 1 (since EXT1 is not counted).
367 int handle_708_extended_char (cc708_service_decoder *decoder, unsigned char *data, int data_length)
368 {
369 int used;
370 unsigned char c=0x20; // Default to space
371 unsigned char code=data[0];
372 if (/* data[i]>=0x00 && */ code<=0x1F) // Comment to silence warning
373 {
374 used=handle_708_C2 (decoder, data, data_length);
375 }
376 // Group G2 - Extended Miscellaneous Characters
377 else if (code>=0x20 && code<=0x7F)
378 {
379 c=get_internal_from_G2 (code);
380 used=1;
381 process_character (decoder, c);
382 }
383 // Group C3
384 else if (code>=0x80 && code<=0x9F)
385 {
386 used=handle_708_C3 (decoder, data, data_length);
387 //! @todo Something
388 }
389 // Group G3
390 else
391 {
392 c=get_internal_from_G3 (code);
393 used=1;
394 process_character (decoder, c);
395 }
396 return used;
397 }
398
399 void process_cr (cc708_service_decoder *decoder)
400 {
401 switch (decoder->windows[decoder->current_window].attribs.print_dir)
402 {
403 case pd_left_to_right:
404 decoder->windows[decoder->current_window].pen_column=0;
405 if (decoder->windows[decoder->current_window].pen_row+1 < decoder->windows[decoder->current_window].row_count)
406 decoder->windows[decoder->current_window].pen_row++;
407 break;
408 case pd_right_to_left:
409 decoder->windows[decoder->current_window].pen_column=decoder->windows[decoder->current_window].col_count;
410 if (decoder->windows[decoder->current_window].pen_row+1 < decoder->windows[decoder->current_window].row_count)
411 decoder->windows[decoder->current_window].pen_row++;
412 break;
413 case pd_top_to_bottom:
414 decoder->windows[decoder->current_window].pen_row=0;
415 if (decoder->windows[decoder->current_window].pen_column+1 < decoder->windows[decoder->current_window].col_count)
416 decoder->windows[decoder->current_window].pen_column++;
417 break;
418 case pd_bottom_to_top:
419 decoder->windows[decoder->current_window].pen_row=decoder->windows[decoder->current_window].row_count;
420 if (decoder->windows[decoder->current_window].pen_column+1 < decoder->windows[decoder->current_window].col_count)
421 decoder->windows[decoder->current_window].pen_column++;
422 break;
423 }
424
425 if (decoder->windows[decoder->current_window].anchor_point == anchorpoint_bottom_left ||
426 decoder->windows[decoder->current_window].anchor_point == anchorpoint_bottom_center)
427 {
428 rollupWindow(decoder, decoder->current_window);
429 updateScreen(decoder);
430 }
431 }
432
433 int handle_708_C0 (cc708_service_decoder *decoder, unsigned char *data, int data_length)
434 {
435 const char *name=COMMANDS_C0[data[0]];
436 if (name==NULL)
437 name="Reserved";
438 int len=-1;
439 // These commands have a known length even if they are reserved.
440 if (/* data[0]>=0x00 && */ data[0]<=0xF) // Comment to silence warning
441 {
442 switch (data[0])
443 {
444 case 0x0d: //CR
445 process_cr (decoder);
446 break;
447 case 0x0e: // HCR (Horizontal Carriage Return)
448 //! @todo Process HDR
449 break;
450 case 0x0c: // FF (Form Feed)
451 //! @todo Process FF
452 break;
453 }
454 len=1;
455 }
456 else if (data[0]>=0x10 && data[0]<=0x17)
457 {
458 // Note that 0x10 is actually EXT1 and is dealt with somewhere else. Rest is undefined as per
459 // CEA-708-D
460 len=2;
461 }
462 else if (data[0]>=0x18 && data[0]<=0x1F)
463 {
464 // Only PE16 is defined.
465 if (data[0]==0x18) // PE16
466 {
467 ; //! @todo Handle PE16
468 }
469 len=3;
470 }
471 if (len==-1)
472 {
473 return -1;
474 }
475 if (len>data_length)
476 {
477 return -1;
478 }
479 //! @todo Do something useful eventually
480 return len;
481 }
482
483
484 void process_character(cc708_service_decoder* decoder, unsigned char internal_char)
485 {
486 if (decoder->current_window == -1 ||
487 !decoder->windows[decoder->current_window]
488 .is_defined) // Writing to a non existing window, skipping
489 return;
490 decoder->windows[decoder->current_window].is_empty = 0;
491 decoder->windows[decoder->current_window]
492 .rows[decoder->windows[decoder->current_window].pen_row]
493 [decoder->windows[decoder->current_window].pen_column] = internal_char;
494 /* Not positive this interpretation is correct. Word wrapping is optional, so
495 let's assume we don't need to autoscroll */
496 switch (decoder->windows[decoder->current_window].attribs.print_dir)
497 {
498 case pd_left_to_right:
499 if (decoder->windows[decoder->current_window].pen_column + 1 <
500 decoder->windows[decoder->current_window].col_count)
501 decoder->windows[decoder->current_window].pen_column++;
502 break;
503 case pd_right_to_left:
504 if (decoder->windows->pen_column > 0)
505 decoder->windows[decoder->current_window].pen_column--;
506 break;
507 case pd_top_to_bottom:
508 if (decoder->windows[decoder->current_window].pen_row + 1 <
509 decoder->windows[decoder->current_window].row_count)
510 decoder->windows[decoder->current_window].pen_row++;
511 break;
512 case pd_bottom_to_top:
513 if (decoder->windows[decoder->current_window].pen_row > 0)
514 decoder->windows[decoder->current_window].pen_row--;
515 break;
516 }
517 }
518
519 // G0 - Code Set - ASCII printable characters
520 int handle_708_G0 (cc708_service_decoder *decoder, unsigned char *data, int data_length)
521 {
522 //! @todo Substitution of the music note character for the ASCII DEL character
523 unsigned char c=get_internal_from_G0 (data[0]);
524 process_character (decoder, c);
525 return 1;
526 }
527
528 // G1 Code Set - ISO 8859-1 LATIN-1 Character Set
529 int handle_708_G1 (cc708_service_decoder *decoder, unsigned char *data, int data_length)
530 {
531 unsigned char c=get_internal_from_G1 (data[0]);
532 process_character (decoder, c);
533 return 1;
534 }
535
536 /*-------------------------------------------------------
537 WINDOW COMMANDS
538 ------------------------------------------------------- */
539 void handle_708_CWx_SetCurrentWindow (cc708_service_decoder *decoder, int new_window)
540 {
541 if (decoder->windows[new_window].is_defined)
542 decoder->current_window=new_window;
543 }
544
545 void clearWindowText(e708Window *window)
546 {
547 for (int i = 0; i<I708_MAX_ROWS; i++)
548 {
549 memset(window->rows[i], ' ', I708_MAX_COLUMNS);
550 window->rows[i][I708_MAX_COLUMNS] = 0;
551 }
552 memset(window->rows[I708_MAX_ROWS], 0, I708_MAX_COLUMNS + 1);
553 window->is_empty = 1;
554
555 }
556
557 void clearWindow (cc708_service_decoder *decoder, int window)
558 {
559 if (decoder->windows[window].is_defined)
560 clearWindowText(&decoder->windows[window]);
561 }
562
563 void handle_708_CLW_ClearWindows (cc708_service_decoder *decoder, int windows_bitmap)
564 {
565 if (windows_bitmap==0)
566 ;//ccx_common_logging.debug_ftn(CCX_DMT_708, "None\n");
567 else
568 {
569 for (int i=0; i<8; i++)
570 {
571 if (windows_bitmap & 1)
572 {
573 clearWindow (decoder, i);
574 }
575 windows_bitmap>>=1;
576 }
577 }
578 }
579
580 void handle_708_DSW_DisplayWindows (cc708_service_decoder *decoder, int windows_bitmap)
581 {
582 if (windows_bitmap==0)
583 ;//ccx_common_logging.debug_ftn(CCX_DMT_708, "None\n");
584 else
585 {
586 int changes=0;
587 for (e708Window& window : decoder->windows)
588 {
589 if (windows_bitmap & 1)
590 {
591 if (!window.visible)
592 {
593 changes=1;
594 window.visible=1;
595 }
596 }
597 windows_bitmap>>=1;
598 }
599 if (changes)
600 updateScreen (decoder);
601 }
602 }
603
604 void handle_708_HDW_HideWindows (cc708_service_decoder *decoder, int windows_bitmap)
605 {
606 if (windows_bitmap==0)
607 ;//ccx_common_logging.debug_ftn(CCX_DMT_708, "None\n");
608 else
609 {
610 int changes=0;
611 for (e708Window& window : decoder->windows)
612 {
613 if (windows_bitmap & 1)
614 {
615 if (window.is_defined && window.visible && !window.is_empty)
616 {
617 changes=1;
618 window.visible=0;
619 }
620 //! @todo Actually Hide Window
621 }
622 windows_bitmap>>=1;
623 }
624 if (changes)
625 updateScreen (decoder);
626 }
627 }
628
629 void handle_708_TGW_ToggleWindows (cc708_service_decoder *decoder, int windows_bitmap)
630 {
631 if (windows_bitmap==0)
632 ;//ccx_common_logging.debug_ftn(CCX_DMT_708, "None\n");
633 else
634 {
635 for (e708Window& window : decoder->windows)
636 {
637 if (windows_bitmap & 1)
638 {
639 window.visible=!window.visible;
640 }
641 windows_bitmap>>=1;
642 }
643 updateScreen(decoder);
644 }
645 }
646
647 void handle_708_DFx_DefineWindow (cc708_service_decoder *decoder, int window, unsigned char *data)
648 {
649 if (decoder->windows[window].is_defined &&
650 memcmp (decoder->windows[window].commands, data+1, 6)==0)
651 {
652 return;
653 }
654 decoder->windows[window].number=window;
655 int priority = (data[1] ) & 0x7;
656 int col_lock = (data[1]>>3) & 0x1;
657 int row_lock = (data[1]>>4) & 0x1;
658 int visible = (data[1]>>5) & 0x1;
659 int anchor_vertical = data[2] & 0x7f;
660 int relative_pos = (data[2]>>7);
661 int anchor_horizontal = data[3];
662 int row_count = data[4] & 0xf;
663 int anchor_point = data[4]>>4;
664 int col_count = data[5] & 0x3f;
665 int pen_style = data[6] & 0x7;
666 int win_style = (data[6]>>3) & 0x7;
667 col_count++; // These increments seems to be needed but no documentation
668 row_count++; // backs it up
669
670 if (anchor_vertical > I708_SCREENGRID_ROWS)
671 anchor_vertical = I708_SCREENGRID_ROWS;
672
673 decoder->windows[window].priority=priority;
674 decoder->windows[window].col_lock=col_lock;
675 decoder->windows[window].row_lock=row_lock;
676 decoder->windows[window].visible=visible;
677 decoder->windows[window].anchor_vertical=anchor_vertical;
678 decoder->windows[window].relative_pos=relative_pos;
679 decoder->windows[window].anchor_horizontal=anchor_horizontal;
680 decoder->windows[window].row_count=row_count;
681 decoder->windows[window].anchor_point=anchor_point;
682 decoder->windows[window].col_count=col_count;
683 decoder->windows[window].pen_style=pen_style;
684 decoder->windows[window].win_style=win_style;
685 if (!decoder->windows[window].is_defined)
686 {
687 // If the window is being created, all character positions in the window
688 // are set to the fill color...
689 //! @todo COLORS
690 // ...and the pen location is set to (0,0)
691 decoder->windows[window].pen_column=0;
692 decoder->windows[window].pen_row=0;
693 if (!decoder->windows[window].memory_reserved)
694 {
695 for (int i=0;i<=I708_MAX_ROWS;i++)
696 {
697 decoder->windows[window].rows[i]=(unsigned char *) malloc (I708_MAX_COLUMNS+1);
698 if (decoder->windows[window].rows[i]==NULL) // Great
699 {
700 decoder->windows[window].is_defined=0;
701 decoder->current_window=-1;
702 for (int j=0;j<i;j++)
703 free (decoder->windows[window].rows[j]);
704 return; //! @todo Warn somehow
705 }
706 }
707 decoder->windows[window].memory_reserved=1;
708 }
709 decoder->windows[window].is_defined=1;
710 memset(&decoder->windows[window].attribs, 0, sizeof(e708Window_attribs));
711 clearWindowText (&decoder->windows[window]);
712 }
713
714 // ...also makes the defined windows the current window (setCurrentWindow)
715 handle_708_CWx_SetCurrentWindow (decoder, window);
716 memcpy (decoder->windows[window].commands, data+1, 6);
717 }
718
719 void handle_708_SWA_SetWindowAttributes (cc708_service_decoder *decoder, unsigned char *data)
720 {
721 int fill_color = (data[1] ) & 0x3f;
722 int fill_opacity = (data[1]>>6) & 0x03;
723 int border_color = (data[2] ) & 0x3f;
724 int border_type01 = (data[2]>>6) & 0x03;
725 int justify = (data[3] ) & 0x03;
726 int scroll_dir = (data[3]>>2) & 0x03;
727 int print_dir = (data[3]>>4) & 0x03;
728 int word_wrap = (data[3]>>6) & 0x01;
729 int border_type = (data[3]>>5) | border_type01;
730 int display_eff = (data[4] ) & 0x03;
731 int effect_dir = (data[4]>>2) & 0x03;
732 int effect_speed = (data[4]>>4) & 0x0f;
733 if (decoder->current_window==-1)
734 {
735 // Can't do anything yet - we need a window to be defined first.
736 return;
737 }
738 decoder->windows[decoder->current_window].attribs.fill_color=fill_color;
739 decoder->windows[decoder->current_window].attribs.fill_opacity=fill_opacity;
740 decoder->windows[decoder->current_window].attribs.border_color=border_color;
741 decoder->windows[decoder->current_window].attribs.border_type01=border_type01;
742 decoder->windows[decoder->current_window].attribs.justify=justify;
743 decoder->windows[decoder->current_window].attribs.scroll_dir=scroll_dir;
744 decoder->windows[decoder->current_window].attribs.print_dir=print_dir;
745 decoder->windows[decoder->current_window].attribs.word_wrap=word_wrap;
746 decoder->windows[decoder->current_window].attribs.border_type=border_type;
747 decoder->windows[decoder->current_window].attribs.display_eff=display_eff;
748 decoder->windows[decoder->current_window].attribs.effect_dir=effect_dir;
749 decoder->windows[decoder->current_window].attribs.effect_speed=effect_speed;
750
751 }
752
753 void deleteWindow (cc708_service_decoder *decoder, int window)
754 {
755 if (window==decoder->current_window)
756 {
757 // If the current window is deleted, then the decoder's current window ID
758 // is unknown and must be reinitialized with either the SetCurrentWindow
759 // or DefineWindow command.
760 decoder->current_window=-1;
761 }
762 //! @todo Do the actual deletion (remove from display if needed, etc), mark as
763 // not defined, etc
764 if (decoder->windows[window].is_defined)
765 {
766 clearWindowText(&decoder->windows[window]);
767 }
768 decoder->windows[window].is_defined=0;
769 }
770
771 void handle_708_DLW_DeleteWindows (cc708_service_decoder *decoder, int windows_bitmap)
772 {
773 int changes=0;
774 if (windows_bitmap==0)
775 ; //ccx_common_logging.debug_ftn(CCX_DMT_708, "None\n");
776 else
777 {
778 for (int i=0; i<8; i++)
779 {
780 if (windows_bitmap & 1)
781 {
782 if (decoder->windows[i].is_defined && decoder->windows[i].visible && !decoder->windows[i].is_empty)
783 changes=1;
784 deleteWindow (decoder, i);
785 }
786 windows_bitmap>>=1;
787 }
788 }
789 if (changes)
790 updateScreen (decoder);
791
792 }
793
794 /*-------------------------------------------------------
795 WINDOW COMMANDS
796 ------------------------------------------------------- */
797 void handle_708_SPA_SetPenAttributes (cc708_service_decoder *decoder, unsigned char *data)
798 {
799 int pen_size = (data[1] ) & 0x3;
800 int offset = (data[1]>>2) & 0x3;
801 int text_tag = (data[1]>>4) & 0xf;
802 int font_tag = (data[2] ) & 0x7;
803 int edge_type = (data[2]>>3) & 0x7;
804 int underline = (data[2]>>4) & 0x1;
805 int italic = (data[2]>>5) & 0x1;
806 if (decoder->current_window==-1)
807 {
808 // Can't do anything yet - we need a window to be defined first.
809 return;
810 }
811 decoder->windows[decoder->current_window].pen.pen_size=pen_size;
812 decoder->windows[decoder->current_window].pen.offset=offset;
813 decoder->windows[decoder->current_window].pen.text_tag=text_tag;
814 decoder->windows[decoder->current_window].pen.font_tag=font_tag;
815 decoder->windows[decoder->current_window].pen.edge_type=edge_type;
816 decoder->windows[decoder->current_window].pen.underline=underline;
817 decoder->windows[decoder->current_window].pen.italic=italic;
818 }
819
820 void handle_708_SPC_SetPenColor (cc708_service_decoder *decoder, unsigned char *data)
821 {
822 int fg_color = (data[1] ) & 0x3f;
823 int fg_opacity = (data[1]>>6) & 0x03;
824 int bg_color = (data[2] ) & 0x3f;
825 int bg_opacity = (data[2]>>6) & 0x03;
826 int edge_color = (data[3]>>6) & 0x3f;
827 if (decoder->current_window==-1)
828 {
829 // Can't do anything yet - we need a window to be defined first.
830 return;
831 }
832
833 decoder->windows[decoder->current_window].pen_color.fg_color=fg_color;
834 decoder->windows[decoder->current_window].pen_color.fg_opacity=fg_opacity;
835 decoder->windows[decoder->current_window].pen_color.bg_color=bg_color;
836 decoder->windows[decoder->current_window].pen_color.bg_opacity=bg_opacity;
837 decoder->windows[decoder->current_window].pen_color.edge_color=edge_color;
838 }
839
840
841 void handle_708_SPL_SetPenLocation (cc708_service_decoder *decoder, unsigned char *data)
842 {
843 int row = data[1] & 0x0f;
844 int col = data[2] & 0x3f;
845 if (decoder->current_window==-1)
846 {
847 // Can't do anything yet - we need a window to be defined first.
848 return;
849 }
850 decoder->windows[decoder->current_window].pen_row=row;
851 decoder->windows[decoder->current_window].pen_column=col;
852 }
853
854
855 /*-------------------------------------------------------
856 SYNCHRONIZATION COMMANDS
857 ------------------------------------------------------- */
858 void handle_708_DLY_Delay (cc708_service_decoder *decoder, int tenths_of_sec)
859 {
860 //! @todo Probably ask for the current FTS and wait for this time before resuming -
861 // not sure it's worth it though
862 }
863
864 void handle_708_DLC_DelayCancel (cc708_service_decoder *decoder)
865 {
866 //! @todo See above
867 }
868
869 // C1 Code Set - Captioning Commands Control Codes
870 int handle_708_C1 (cc708_service_decoder *decoder, unsigned char *data, int data_length)
871 {
872 struct S_COMMANDS_C1 com=COMMANDS_C1[data[0]-0x80];
873 if (com.length>data_length)
874 {
875 return -1;
876 }
877 switch (com.code)
878 {
879 case CW0: /* SetCurrentWindow */
880 case CW1:
881 case CW2:
882 case CW3:
883 case CW4:
884 case CW5:
885 case CW6:
886 case CW7:
887 handle_708_CWx_SetCurrentWindow (decoder, com.code-CW0); /* Window 0 to 7 */
888 break;
889 case CLW:
890 handle_708_CLW_ClearWindows (decoder, data[1]);
891 break;
892 case DSW:
893 handle_708_DSW_DisplayWindows (decoder, data[1]);
894 break;
895 case HDW:
896 handle_708_HDW_HideWindows (decoder, data[1]);
897 break;
898 case TGW:
899 handle_708_TGW_ToggleWindows (decoder, data[1]);
900 break;
901 case DLW:
902 handle_708_DLW_DeleteWindows (decoder, data[1]);
903 break;
904 case DLY:
905 handle_708_DLY_Delay (decoder, data[1]);
906 break;
907 case DLC:
908 handle_708_DLC_DelayCancel (decoder);
909 break;
910 case RST:
911 cc708_service_reset(decoder);
912 break;
913 case SPA:
914 handle_708_SPA_SetPenAttributes (decoder, data);
915 break;
916 case SPC:
917 handle_708_SPC_SetPenColor (decoder, data);
918 break;
919 case SPL:
920 handle_708_SPL_SetPenLocation (decoder, data);
921 break;
922 case RSV93:
923 case RSV94:
924 case RSV95:
925 case RSV96:
926 break;
927 case SWA:
928 handle_708_SWA_SetWindowAttributes (decoder, data);
929 break;
930 case DF0:
931 case DF1:
932 case DF2:
933 case DF3:
934 case DF4:
935 case DF5:
936 case DF6:
937 case DF7:
938 handle_708_DFx_DefineWindow (decoder, com.code-DF0, data); /* Window 0 to 7 */
939 break;
940 default:
941 break;
942 }
943
944 return com.length;
945 }
946
947
948 void process_service_block (cc708_service_decoder *decoder, unsigned char *data, int data_length)
949 {
950 int i=0;
951 while (i<data_length)
952 {
953 int used=-1;
954 if (data[i]!=EXT1)
955 {
956 // Group C0
957 if (/* data[i]>=0x00 && */ data[i]<=0x1F) // Comment to silence warning
958 {
959 used=handle_708_C0 (decoder,data+i,data_length-i);
960 }
961 // Group G0
962 else if (data[i]>=0x20 && data[i]<=0x7F)
963 {
964 used=handle_708_G0 (decoder,data+i,data_length-i);
965 }
966 // Group C1
967 else if (data[i]>=0x80 && data[i]<=0x9F)
968 {
969 used=handle_708_C1 (decoder,data+i,data_length-i);
970 }
971 // Group C2
972 else
973 used=handle_708_G1 (decoder,data+i,data_length-i);
974 if (used==-1)
975 {
976 //! @todo Not sure if a local reset is going to be helpful here.
977 cc708_service_reset (decoder);
978 return;
979 }
980 }
981 else // Use extended set
982 {
983 used=handle_708_extended_char (decoder, data+i+1,data_length-1);
984 used++; // Since we had EXT1
985 }
986 i+=used;
987 }
988
989 // update rollup windows
990 int update = 0;
991 for (e708Window& window : decoder->windows)
992 {
993 if (window.is_defined && window.visible &&
994 (window.anchor_point == anchorpoint_bottom_left ||
995 window.anchor_point == anchorpoint_bottom_center))
996 {
997 update++;
998 break;
999 }
1000 }
1001 if (update)
1002 {
1003 updateScreen(decoder);
1004 }
1005 }
1006
1007 bool check_current_packet_complete (cc708_service_decoder *decoders)
1008 {
1009 int len = decoders[0].parent->m_current_packet[0] & 0x3F; // 6 least significants bits
1010 if (decoders[0].parent->m_current_packet_length == 0)
1011 return false;
1012
1013 if (len==0) // This is well defined in EIA-708; no magic.
1014 len=128;
1015 else
1016 len=len*2;
1017
1018 // Note that len here is the length including the header
1019 if (decoders[0].parent->m_current_packet_length == len) // Is this possible?
1020 {
1021 return true;
1022 }
1023 return false;
1024 }
1025
1026 void process_current_packet (cc708_service_decoder *decoders)
1027 {
1028 int seq = (decoders[0].parent->m_current_packet[0] & 0xC0) >> 6; // Two most significants bits
1029 int len = decoders[0].parent->m_current_packet[0] & 0x3F; // 6 least significants bits
1030 if (decoders[0].parent->m_current_packet_length == 0)
1031 return;
1032
1033 if (len==0) // This is well defined in EIA-708; no magic.
1034 len=128;
1035 else
1036 len=len*2;
1037 // Note that len here is the length including the header
1038 if (decoders[0].parent->m_current_packet_length != len) // Is this possible?
1039 {
1040 cc708_reset(decoders);
1041 return;
1042 }
1043 int last_seq = decoders[0].parent->m_last_seq;
1044 if ((last_seq != -1) && ((last_seq+1)%4 != seq))
1045 {
1046 cc708_reset(decoders);
1047 return;
1048 }
1049 decoders[0].parent->m_last_seq = seq;
1050
1051 unsigned char *pos = decoders[0].parent->m_current_packet + 1;
1052
1053 while (pos < decoders[0].parent->m_current_packet + len)
1054 {
1055 int service_number=(pos[0] & 0xE0)>>5; // 3 more significant bits
1056 int block_length = (pos[0] & 0x1F); // 5 less significant bits
1057
1058 if (service_number==7) // There is an extended header
1059 {
1060 pos++;
1061 service_number=(pos[0] & 0x3F); // 6 more significant bits
1062 if (service_number<7)
1063 {
1064 }
1065 pos = decoders[0].parent->m_current_packet + len;
1066 break;
1067 }
1068
1069 pos++; // Move to service data
1070 if (service_number==0 && block_length!=0) // Illegal, but specs say what to do...
1071 {
1072 pos = decoders[0].parent->m_current_packet + len; // Move to end
1073 break;
1074 }
1075
1076 if (service_number>0 && decoders[service_number].inited)
1077 process_service_block (&decoders[service_number], pos, block_length);
1078
1079 pos+=block_length; // Skip data
1080 }
1081
1082 clear_packet(&decoders[0]);
1083
1084 if (pos != decoders[0].parent->m_current_packet + len) // For some reason we didn't parse the whole packet
1085 {
1086 cc708_reset(decoders);
1087 }
1088
1089 if (len<128 && *pos) // Null header is mandatory if there is room
1090 {
1091 ;//ccx_common_logging.debug_ftn(CCX_DMT_708, "Warning: Null header expected but not found.\n");
1092 }
1093 }
1094
1095 void decode_708 (const unsigned char *data, int datalength, cc708_service_decoder* decoders)
1096 {
1097 /* Note: The data has this format:
1098 1 byte for cc_valid and cc_type
1099 2 bytes for the actual data */
1100 for (int i=0; i<datalength; i+=3)
1101 {
1102 unsigned char cc_valid=data[i] & 0x04;
1103 unsigned char cc_type=data[i] & 0x03;
1104
1105 switch (cc_type)
1106 {
1107 case 0:
1108 // only use 608 as fallback
1109 if (!decoders[0].parent->m_seen708)
1110 decode_cc(decoders[0].parent->m_cc608decoder, (const uint8_t*)data+i, 3);
1111 break;
1112 case 2:
1113 if (cc_valid==0) // This ends the previous packet if complete
1114 {
1115 if (check_current_packet_complete(decoders))
1116 {
1117 process_current_packet(decoders);
1118 }
1119 }
1120 else
1121 {
1122 if (decoders[0].parent->m_current_packet_length < 254)
1123 {
1124 decoders[0].parent->m_current_packet[decoders[0].parent->m_current_packet_length++]=data[i+1];
1125 decoders[0].parent->m_current_packet[decoders[0].parent->m_current_packet_length++]=data[i+2];
1126 }
1127 }
1128 break;
1129 case 3:
1130 process_current_packet(decoders);
1131 if (cc_valid)
1132 {
1133 if (decoders[0].parent->m_current_packet_length < 128)
1134 {
1135 decoders[0].parent->m_current_packet[decoders[0].parent->m_current_packet_length++]=data[i+1];
1136 decoders[0].parent->m_current_packet[decoders[0].parent->m_current_packet_length++]=data[i+2];
1137 }
1138 }
1139 break;
1140 default:
1141 break;
1142 }
1143 }
1144 }
1145
1146 void ccx_decoders_708_init(cc708_service_decoder *decoders, void (*handler)(int service, void *userdata), void *userdata, CDecoderCC708 *parent)
1147 {
1148 for (int i = 0; i<CCX_DECODERS_708_MAX_SERVICES; i++)
1149 {
1150 cc708_service_reset (&decoders[i]);
1151 decoders[i].srt_counter=0;
1152 decoders[i].service = i;
1153 decoders[i].callback = handler;
1154 decoders[i].userdata = userdata;
1155 decoders[i].parent = parent;
1156 }
1157 decoders[0].parent->m_cc608decoder->callback = handler;
1158 decoders[0].parent->m_cc608decoder->userdata = userdata;
1159
1160 decoders[0].parent->m_current_packet_length = 0;
1161 decoders[0].parent->m_last_seq = -1;
1162 decoders[0].parent->m_seen708 = false;
1163 decoders[0].parent->m_seen608 = false;
1164 }
1165
1166 //-----------------------------------------------------------------------------
1167 //
1168 //-----------------------------------------------------------------------------
1169
1170 CDecoderCC708::CDecoderCC708()
1171 {
1172 m_inited = false;
1173 cc_decoder_init();
1174 }
1175
1176 CDecoderCC708::~CDecoderCC708()
1177 {
1178 delete [] m_cc708decoders;
1179 cc_decoder_close(m_cc608decoder);
1180 }
1181
1182 void CDecoderCC708::Init(void (*handler)(int service, void *userdata), void *userdata)
1183 {
1184 m_cc608decoder = cc_decoder_open();
1185 m_cc708decoders = new cc708_service_decoder[8];
1186 ccx_decoders_708_init(m_cc708decoders, handler, userdata, this);
1187 }
1188
1189 void CDecoderCC708::Decode(const unsigned char *data, int datalength)
1190 {
1191 decode_708(data, datalength, m_cc708decoders);
1192 }