1 /*
2 * Closed Caption Decoding
3 * Copyright (c) 2015 Anshul Maheshwari
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
26
27 #define SCREEN_ROWS 15
28 #define SCREEN_COLUMNS 32
29
30 #define SET_FLAG(var, val) ( (var) |= ( 1 << (val)) )
31 #define UNSET_FLAG(var, val) ( (var) &= ~( 1 << (val)) )
32 #define CHECK_FLAG(var, val) ( (var) & ( 1 << (val)) )
33
35
41 };
42
54 };
55
61 };
62
68 };
69
71 {
73 [0x27] = "\u2019",
74 [0x2a] = "\u00e1",
75 [0x5c] = "\u00e9",
76 [0x5e] = "\u00ed",
77 [0x5f] = "\u00f3",
78 [0x60] = "\u00fa",
79 [0x7b] = "\u00e7",
80 [0x7c] = "\u00f7",
81 [0x7d] = "\u00d1",
82 [0x7e] = "\u00f1",
83 [0x7f] = "\u2588"
84 },
86 [0x30] = "\u00ae",
87 [0x31] = "\u00b0",
88 [0x32] = "\u00bd",
89 [0x33] = "\u00bf",
90 [0x34] = "\u2122",
91 [0x35] = "\u00a2",
92 [0x36] = "\u00a3",
93 [0x37] = "\u266a",
94 [0x38] = "\u00e0",
95 [0x39] = "\u00A0",
96 [0x3a] = "\u00e8",
97 [0x3b] = "\u00e2",
98 [0x3c] = "\u00ea",
99 [0x3d] = "\u00ee",
100 [0x3e] = "\u00f4",
101 [0x3f] = "\u00fb",
102 },
104 [0x20] = "\u00c1",
105 [0x21] = "\u00c9",
106 [0x22] = "\u00d3",
107 [0x23] = "\u00da",
108 [0x24] = "\u00dc",
109 [0x25] = "\u00fc",
110 [0x26] = "\u00b4",
111 [0x27] = "\u00a1",
112 [0x28] = "*",
113 [0x29] = "\u2018",
114 [0x2a] = "-",
115 [0x2b] = "\u00a9",
116 [0x2c] = "\u2120",
117 [0x2d] = "\u00b7",
118 [0x2e] = "\u201c",
119 [0x2f] = "\u201d",
120 [0x30] = "\u00c0",
121 [0x31] = "\u00c2",
122 [0x32] = "\u00c7",
123 [0x33] = "\u00c8",
124 [0x34] = "\u00ca",
125 [0x35] = "\u00cb",
126 [0x36] = "\u00eb",
127 [0x37] = "\u00ce",
128 [0x38] = "\u00cf",
129 [0x39] = "\u00ef",
130 [0x3a] = "\u00d4",
131 [0x3b] = "\u00d9",
132 [0x3c] = "\u00f9",
133 [0x3d] = "\u00db",
134 [0x3e] = "\u00ab",
135 [0x3f] = "\u00bb",
136 },
138 [0x20] = "\u00c3",
139 [0x21] = "\u00e3",
140 [0x22] = "\u00cd",
141 [0x23] = "\u00cc",
142 [0x24] = "\u00ec",
143 [0x25] = "\u00d2",
144 [0x26] = "\u00f2",
145 [0x27] = "\u00d5",
146 [0x28] = "\u00f5",
147 [0x29] = "{",
148 [0x2a] = "}",
149 [0x2b] = "\\",
150 [0x2c] = "^",
151 [0x2d] = "_",
152 [0x2e] = "|",
153 [0x2f] = "~",
154 [0x30] = "\u00c4",
155 [0x31] = "\u00e4",
156 [0x32] = "\u00d6",
157 [0x33] = "\u00f6",
158 [0x34] = "\u00df",
159 [0x35] = "\u00a5",
160 [0x36] = "\u00a4",
161 [0x37] = "\u00a6",
162 [0x38] = "\u00c5",
163 [0x39] = "\u00e5",
164 [0x3a] = "\u00d8",
165 [0x3b] = "\u00f8",
166 [0x3c] = "\u250c",
167 [0x3d] = "\u2510",
168 [0x3e] = "\u2514",
169 [0x3f] = "\u2518",
170 },
171 };
172
174 {
183 };
184
186 {
219 /* total 32 entries */
220 };
221
223 /* +1 is used to compensate null character of string */
229 /*
230 * Bitmask of used rows; if a bit is not set, the
231 * corresponding row is not used.
232 * for setting row 1 use row | (1 << 0)
233 * for setting row 15 use row | (1 << 14)
234 */
236 };
237
262
264 {
267
270 /* taking by default roll up to 2 */
274 ctx->cursor_row = 10;
282 3,
286 }
287
289 }
290
292 {
296 return 0;
297 }
298
300 {
302 ctx->screen[0].row_used = 0;
303 ctx->screen[1].row_used = 0;
304 ctx->prev_cmd[0] = 0;
305 ctx->prev_cmd[1] = 0;
308 ctx->cursor_row = 10;
309 ctx->cursor_column = 0;
310 ctx->cursor_font = 0;
311 ctx->cursor_color = 0;
313 ctx->cursor_charset = 0;
314 ctx->active_screen = 0;
315 ctx->last_real_time = 0;
316 ctx->screen_touched = 0;
317 ctx->buffer_changed = 0;
322 }
323
324 /**
325 * @param ctx closed caption context just to print log
326 */
328 {
329 uint8_t col =
ctx->cursor_column;
331 char *font = screen->
fonts[
ctx->cursor_row];
333 char *bg = screen->
bgs[
ctx->cursor_row];
335
337 row[col] = ch;
338 font[col] =
ctx->cursor_font;
340 bg[col] =
ctx->bg_color;
341 charset[col] =
ctx->cursor_charset;
343 if (ch)
ctx->cursor_column++;
344 return;
345 }
346 /* We have extra space at end only for null character */
348 row[col] = ch;
349 return;
350 }
351 else {
353 return;
354 }
355 }
356
357 /**
358 * This function after validating parity bit, also remove it from data pair.
359 * The first byte doesn't pass parity, we replace it with a solid blank
360 * and process the pair.
361 * If the second byte doesn't pass parity, it returns INVALIDDATA
362 * user can ignore the whole pair and pass the other pair.
363 */
365 {
366 uint8_t cc_valid = (*cc_data_pair & 4) >>2;
367 uint8_t cc_type = *cc_data_pair & 3;
368
369 *hi = cc_data_pair[1];
370
371 if (!cc_valid)
373
374 // if EIA-608 data then verify parity.
375 if (cc_type==0 || cc_type==1) {
378 }
380 *hi = 0x7F;
381 }
382 }
383
384 //Skip non-data
385 if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD)
386 && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0)
388
389 //skip 708 data
390 if (cc_type == 3 || cc_type == 2)
392
393 return 0;
394 }
395
397 {
400 // use Inactive screen
401 return ctx->screen + !
ctx->active_screen;
405 // use active screen
406 return ctx->screen +
ctx->active_screen;
407 }
408 /* It was never an option */
410 }
411
413 {
416
418 return;
419
421
422 /* +1 signify cursor_row starts from 0
423 * Can't keep lines less then row cursor pos
424 */
425 keep_lines =
FFMIN(
ctx->cursor_row + 1,
ctx->rollup);
426
428 if (
i >
ctx->cursor_row - keep_lines && i <= ctx->cursor_row)
429 continue;
431 }
432
433 for (
i = 0;
i < keep_lines && screen->
row_used;
i++) {
434 const int i_row =
ctx->cursor_row - keep_lines +
i + 1;
435
443 }
444
446 }
447
449 {
451 struct Screen *screen =
ctx->screen +
ctx->active_screen;
455 const int bidx =
ctx->buffer_index;
456
458
460 {
463 const char *charset = screen->
charsets[
i];
464 j = 0;
466 j++;
469 }
470 }
471
473 {
476 const char *font = screen->
fonts[
i];
477 const char *bg = screen->
bgs[
i];
479 const char *charset = screen->
charsets[
i];
480 const char *override;
481 int x, y, seen_char = 0;
482 j = 0;
483
484 /* skip leading space */
486 j++;
487
490 av_bprintf(&
ctx->buffer[bidx],
"{\\an7}{\\pos(%d,%d)}", x, y);
491
493 const char *e_tag = "", *s_tag = "", *c_tag = "", *b_tag = "";
494
495 if (row[j] == 0)
496 break;
497
498 if (prev_font != font[j]) {
499 switch (prev_font) {
501 e_tag = "{\\i0}";
502 break;
504 e_tag = "{\\u0}";
505 break;
507 e_tag = "{\\u0}{\\i0}";
508 break;
509 }
510 switch (font[j]) {
512 s_tag = "{\\i1}";
513 break;
515 s_tag = "{\\u1}";
516 break;
518 s_tag = "{\\u1}{\\i1}";
519 break;
520 }
521 }
522 if (prev_color !=
color[j]) {
525 c_tag = "{\\c&HFFFFFF&}";
526 break;
528 c_tag = "{\\c&H00FF00&}";
529 break;
531 c_tag = "{\\c&HFF0000&}";
532 break;
534 c_tag = "{\\c&HFFFF00&}";
535 break;
537 c_tag = "{\\c&H0000FF&}";
538 break;
540 c_tag = "{\\c&H00FFFF&}";
541 break;
543 c_tag = "{\\c&HFF00FF&}";
544 break;
545 }
546 }
547 if (prev_bg_color != bg[j]) {
548 switch (bg[j]) {
550 b_tag = "{\3円c&HFFFFFF&}";
551 break;
553 b_tag = "{\3円c&H00FF00&}";
554 break;
556 b_tag = "{\3円c&HFF0000&}";
557 break;
559 b_tag = "{\3円c&HFFFF00&}";
560 break;
562 b_tag = "{\3円c&H0000FF&}";
563 break;
565 b_tag = "{\3円c&H00FFFF&}";
566 break;
568 b_tag = "{\3円c&HFF00FF&}";
569 break;
571 b_tag = "{\3円c&H000000&}";
572 break;
573 }
574 }
575
576 prev_font = font[j];
577 prev_color =
color[j];
578 prev_bg_color = bg[j];
580 if (override) {
581 av_bprintf(&
ctx->buffer[bidx],
"%s%s%s%s%s", e_tag, s_tag, c_tag, b_tag,
override);
582 seen_char = 1;
583 } else if (row[j] == ' ' && !seen_char) {
584 av_bprintf(&
ctx->buffer[bidx],
"%s%s%s%s\\h", e_tag, s_tag, c_tag, b_tag);
585 } else {
586 av_bprintf(&
ctx->buffer[bidx],
"%s%s%s%s%c", e_tag, s_tag, c_tag, b_tag, row[j]);
587 seen_char = 1;
588 }
589
590 }
592 }
593 }
596 if (screen->
row_used &&
ctx->buffer[bidx].len >= 2) {
597 ctx->buffer[bidx].len -= 2;
598 ctx->buffer[bidx].str[
ctx->buffer[bidx].len] = 0;
599 }
600 ctx->buffer_changed = 1;
601 return 0;
602 }
603
605 {
606 ctx->buffer_time[0] =
ctx->buffer_time[1];
607 ctx->buffer_time[1] =
pts;
608 }
609
611 {
612 const int i = (lo & 0xf) >> 1;
613
615 }
616
618 {
621
623 return;
624
627
630 }
631
633 {
634 static const int8_t row_map[] = {
635 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
636 };
637 const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
640
641 if (row_map[
index] <= 0) {
643 return;
644 }
645
646 lo &= 0x1f;
647
648 ctx->cursor_row = row_map[
index] - 1;
652 ctx->cursor_column = 0;
654 for (
i = 0;
i < indent;
i++) {
656 }
657 }
658
660 {
661 struct Screen *screen =
ctx->screen +
ctx->active_screen;
663
664 // In buffered mode, keep writing to screen until it is wiped.
665 // Before wiping the display, capture contents to emit subtitle.
668
671
672 // In realtime mode, emit an empty caption so the last one doesn't
673 // stay on the screen.
676
678 }
679
681 {
683
684 ctx->active_screen = !
ctx->active_screen;
685
686 // In buffered mode, we wait til the *next* EOC and
687 // capture what was already on the screen since the last EOC.
690
691 ctx->cursor_column = 0;
692
693 // In realtime mode, we display the buffered contents (after
694 // flipping the buffer to active above) as soon as EOC arrives.
697
699 }
700
702 {
705 }
706
708 {
710
712
713 switch (hi) {
714 case 0x11:
716 break;
717 case 0x12:
718 if (
ctx->cursor_column > 0)
719 ctx->cursor_column -= 1;
721 break;
722 case 0x13:
723 if (
ctx->cursor_column > 0)
724 ctx->cursor_column -= 1;
726 break;
727 default:
730 break;
731 }
732
733 if (lo) {
735 }
737
739 ctx->screen_touched = 1;
740
741 if (lo)
743 else
745 }
746
748 {
750
751 if (hi ==
ctx->prev_cmd[0] && lo ==
ctx->prev_cmd[1]) {
752 return 0;
753 }
754
755 /* set prev command */
756 ctx->prev_cmd[0] = hi;
757 ctx->prev_cmd[1] = lo;
758
759 if ( (hi == 0x10 && (lo >= 0x40 && lo <= 0x5f)) ||
760 ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
762 } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) ||
763 ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) {
765 } else if ((hi == 0x10 && lo >= 0x20 && lo <= 0x2f)) {
767 } else if (hi == 0x14 || hi == 0x15 || hi == 0x1c) {
768 switch (lo) {
769 case 0x20:
770 /* resume caption loading */
772 break;
773 case 0x24:
775 break;
776 case 0x25:
777 case 0x26:
778 case 0x27:
779 ctx->rollup = lo - 0x23;
781 break;
782 case 0x29:
783 /* resume direct captioning */
785 break;
786 case 0x2b:
787 /* resume text display */
789 break;
790 case 0x2c:
791 /* erase display memory */
793 break;
794 case 0x2d:
795 /* carriage return */
800 ctx->cursor_column = 0;
801 break;
802 case 0x2e:
803 /* erase buffered (non displayed) memory */
804 // Only in realtime mode. In buffered mode, we re-use the inactive screen
805 // for our own buffering.
806 if (
ctx->real_time) {
807 struct Screen *screen =
ctx->screen + !
ctx->active_screen;
809 }
810 break;
811 case 0x2f:
812 /* end of caption */
815 break;
816 default:
817 ff_dlog(
ctx,
"Unknown command 0x%hhx 0x%hhx\n", hi, lo);
818 break;
819 }
820 } else if (hi >= 0x11 && hi <= 0x13) {
821 /* Special characters */
823 } else if (hi >= 0x20) {
824 /* Standard characters (always in pairs) */
826 ctx->prev_cmd[0] =
ctx->prev_cmd[1] = 0;
827 } else if (hi == 0x17 && lo >= 0x21 && lo <= 0x23) {
829 /* Tab offsets (spacing) */
830 for (
i = 0;
i < lo - 0x20;
i++) {
832 }
833 } else {
834 /* Ignoring all other non data code */
835 ff_dlog(
ctx,
"Unknown command 0x%hhx 0x%hhx\n", hi, lo);
836 }
837
839 }
840
842 int *got_sub,
const AVPacket *avpkt)
843 {
845 int64_t in_time =
sub->pts;
847 int64_t end_time;
848 int bidx =
ctx->buffer_index;
849 const uint8_t *bptr = avpkt->
data;
853 unsigned nb_rect_allocated = 0;
854
855 for (
i = 0;
i <
len;
i += 3) {
856 uint8_t hi, cc_type = bptr[
i] & 1;
857
858 if (
ctx->data_field < 0)
859 ctx->data_field = cc_type;
860
862 continue;
863
864 if (cc_type !=
ctx->data_field)
865 continue;
866
870
871 if (!
ctx->buffer_changed)
872 continue;
873 ctx->buffer_changed = 0;
874
876 ctx->buffer_index = bidx = !
ctx->buffer_index;
877
879
880 if (
ctx->buffer[bidx].str[0] ||
ctx->real_time) {
881 ff_dlog(
ctx,
"cdp writing data (%s)\n",
ctx->buffer[bidx].str);
884 end_time =
ctx->buffer_time[1];
888 else
889 sub->end_display_time = -1;
893 ctx->last_real_time =
sub->pts;
894 ctx->screen_touched = 0;
895 }
896 }
897
898 if (!bptr && !
ctx->real_time &&
ctx->buffer[!
ctx->buffer_index].str[0]) {
899 bidx = !
ctx->buffer_index;
903 sub->pts =
ctx->buffer_time[1];
906 if (
sub->end_display_time == 0)
907 sub->end_display_time =
ctx->buffer[bidx].len * 20;
908 }
909
910 if (
ctx->real_time &&
ctx->screen_touched &&
912 ctx->last_real_time =
sub->pts;
913 ctx->screen_touched = 0;
914
916 ctx->buffer_changed = 0;
917
921 sub->end_display_time = -1;
922 }
923
924 *got_sub =
sub->num_rects > 0;
926 }
927
928 #define OFFSET(x) offsetof(CCaptionSubContext, x)
929 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
931 {
"real_time",
"emit subtitle events as they are decoded for real-time display",
OFFSET(real_time),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
SD },
932 {
"real_time_latency_msec",
"minimum elapsed time between emitting real-time subtitle events",
OFFSET(real_time_latency_msec),
AV_OPT_TYPE_INT, { .i64 = 200 }, 0, 500,
SD },
933 {
"data_field",
"select data field",
OFFSET(data_field),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
SD,
"data_field" },
934 {
"auto",
"pick first one that appears", 0,
AV_OPT_TYPE_CONST, { .i64 =-1 }, 0, 0,
SD,
"data_field" },
938 };
939
945 };
946
960 };