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
22 #include <assert.h>
27
28 #define SCREEN_ROWS 15
29 #define SCREEN_COLUMNS 32
30
31 #define SET_FLAG(var, val) ( (var) |= ( 1 << (val)) )
32 #define UNSET_FLAG(var, val) ( (var) &= ~( 1 << (val)) )
33 #define CHECK_FLAG(var, val) ( (var) & ( 1 << (val)) )
34
36
42 };
43
55 };
56
62 };
63
69 };
70
71 #define CHARSET_OVERRIDE_LIST(START_SET, ENTRY, END_SET) \
72 START_SET(CCSET_BASIC_AMERICAN) \
73 ENTRY(0x27, "\u2019") \
74 ENTRY(0x2a, "\u00e1") \
75 ENTRY(0x5c, "\u00e9") \
76 ENTRY(0x5e, "\u00ed") \
77 ENTRY(0x5f, "\u00f3") \
78 ENTRY(0x60, "\u00fa") \
79 ENTRY(0x7b, "\u00e7") \
80 ENTRY(0x7c, "\u00f7") \
81 ENTRY(0x7d, "\u00d1") \
82 ENTRY(0x7e, "\u00f1") \
83 ENTRY(0x7f, "\u2588") \
84 END_SET \
85 START_SET(CCSET_SPECIAL_AMERICAN) \
86 ENTRY(0x30, "\u00ae") \
87 ENTRY(0x31, "\u00b0") \
88 ENTRY(0x32, "\u00bd") \
89 ENTRY(0x33, "\u00bf") \
90 ENTRY(0x34, "\u2122") \
91 ENTRY(0x35, "\u00a2") \
92 ENTRY(0x36, "\u00a3") \
93 ENTRY(0x37, "\u266a") \
94 ENTRY(0x38, "\u00e0") \
95 ENTRY(0x39, "\u00A0") \
96 ENTRY(0x3a, "\u00e8") \
97 ENTRY(0x3b, "\u00e2") \
98 ENTRY(0x3c, "\u00ea") \
99 ENTRY(0x3d, "\u00ee") \
100 ENTRY(0x3e, "\u00f4") \
101 ENTRY(0x3f, "\u00fb") \
102 END_SET \
103 START_SET(CCSET_EXTENDED_SPANISH_FRENCH_MISC) \
104 ENTRY(0x20, "\u00c1") \
105 ENTRY(0x21, "\u00c9") \
106 ENTRY(0x22, "\u00d3") \
107 ENTRY(0x23, "\u00da") \
108 ENTRY(0x24, "\u00dc") \
109 ENTRY(0x25, "\u00fc") \
110 ENTRY(0x26, "\u00b4") \
111 ENTRY(0x27, "\u00a1") \
112 ENTRY(0x28, "*") \
113 ENTRY(0x29, "\u2018") \
114 ENTRY(0x2a, "-") \
115 ENTRY(0x2b, "\u00a9") \
116 ENTRY(0x2c, "\u2120") \
117 ENTRY(0x2d, "\u00b7") \
118 ENTRY(0x2e, "\u201c") \
119 ENTRY(0x2f, "\u201d") \
120 ENTRY(0x30, "\u00c0") \
121 ENTRY(0x31, "\u00c2") \
122 ENTRY(0x32, "\u00c7") \
123 ENTRY(0x33, "\u00c8") \
124 ENTRY(0x34, "\u00ca") \
125 ENTRY(0x35, "\u00cb") \
126 ENTRY(0x36, "\u00eb") \
127 ENTRY(0x37, "\u00ce") \
128 ENTRY(0x38, "\u00cf") \
129 ENTRY(0x39, "\u00ef") \
130 ENTRY(0x3a, "\u00d4") \
131 ENTRY(0x3b, "\u00d9") \
132 ENTRY(0x3c, "\u00f9") \
133 ENTRY(0x3d, "\u00db") \
134 ENTRY(0x3e, "\u00ab") \
135 ENTRY(0x3f, "\u00bb") \
136 END_SET \
137 START_SET(CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH) \
138 ENTRY(0x20, "\u00c3") \
139 ENTRY(0x21, "\u00e3") \
140 ENTRY(0x22, "\u00cd") \
141 ENTRY(0x23, "\u00cc") \
142 ENTRY(0x24, "\u00ec") \
143 ENTRY(0x25, "\u00d2") \
144 ENTRY(0x26, "\u00f2") \
145 ENTRY(0x27, "\u00d5") \
146 ENTRY(0x28, "\u00f5") \
147 ENTRY(0x29, "{") \
148 ENTRY(0x2a, "}") \
149 ENTRY(0x2b, "\\") \
150 ENTRY(0x2c, "^") \
151 ENTRY(0x2d, "_") \
152 ENTRY(0x2e, "|") \
153 ENTRY(0x2f, "~") \
154 ENTRY(0x30, "\u00c4") \
155 ENTRY(0x31, "\u00e4") \
156 ENTRY(0x32, "\u00d6") \
157 ENTRY(0x33, "\u00f6") \
158 ENTRY(0x34, "\u00df") \
159 ENTRY(0x35, "\u00a5") \
160 ENTRY(0x36, "\u00a4") \
161 ENTRY(0x37, "\u00a6") \
162 ENTRY(0x38, "\u00c5") \
163 ENTRY(0x39, "\u00e5") \
164 ENTRY(0x3a, "\u00d8") \
165 ENTRY(0x3b, "\u00f8") \
166 ENTRY(0x3c, "\u250c") \
167 ENTRY(0x3d, "\u2510") \
168 ENTRY(0x3e, "\u2514") \
169 ENTRY(0x3f, "\u2518") \
170 END_SET \
171
173 {
174 #define START_SET(IDX) \
175 [IDX] = {
176 #define ENTRY(idx, string) \
177 [idx] = string,
178 #define END_SET \
179 },
181 };
182 #define EMPTY_START(IDX)
184 #define ASSERT_ENTRY(IDX, str) \
185 static_assert(sizeof(str) <= sizeof(charset_overrides[0][0]), \
186 "'" str "' string takes too much space");
188
190 {
199 };
200
202 {
235 /* total 32 entries */
236 };
237
239 /* +1 is used to compensate null character of string */
245 /*
246 * Bitmask of used rows; if a bit is not set, the
247 * corresponding row is not used.
248 * for setting row 1 use row | (1 << 0)
249 * for setting row 15 use row | (1 << 14)
250 */
252 };
253
278
280 {
282
285 /* taking by default roll up to 2 */
289 ctx->cursor_row = 10;
297 3,
299 }
300
302 {
306 return 0;
307 }
308
310 {
312 ctx->screen[0].row_used = 0;
313 ctx->screen[1].row_used = 0;
314 ctx->prev_cmd[0] = 0;
315 ctx->prev_cmd[1] = 0;
318 ctx->cursor_row = 10;
319 ctx->cursor_column = 0;
320 ctx->cursor_font = 0;
321 ctx->cursor_color = 0;
323 ctx->cursor_charset = 0;
324 ctx->active_screen = 0;
325 ctx->last_real_time = 0;
326 ctx->screen_touched = 0;
327 ctx->buffer_changed = 0;
332 }
333
334 /**
335 * @param ctx closed caption context just to print log
336 */
338 {
339 uint8_t col =
ctx->cursor_column;
341 char *font = screen->
fonts[
ctx->cursor_row];
343 char *bg = screen->
bgs[
ctx->cursor_row];
345
347 row[col] = ch;
348 font[col] =
ctx->cursor_font;
350 bg[col] =
ctx->bg_color;
351 charset[col] =
ctx->cursor_charset;
353 if (ch)
ctx->cursor_column++;
354 return;
355 }
356 /* We have extra space at end only for null character */
358 row[col] = ch;
359 return;
360 }
361 else {
363 return;
364 }
365 }
366
367 /**
368 * This function after validating parity bit, also remove it from data pair.
369 * The first byte doesn't pass parity, we replace it with a solid blank
370 * and process the pair.
371 * If the second byte doesn't pass parity, it returns INVALIDDATA
372 * user can ignore the whole pair and pass the other pair.
373 */
375 {
376 uint8_t cc_valid = (*cc_data_pair & 4) >>2;
377 uint8_t cc_type = *cc_data_pair & 3;
378
379 *hi = cc_data_pair[1];
380
381 if (!cc_valid)
383
384 // if EIA-608 data then verify parity.
385 if (cc_type==0 || cc_type==1) {
388 }
390 *hi = 0x7F;
391 }
392 }
393
394 //Skip non-data
395 if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD)
396 && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0)
398
399 //skip 708 data
400 if (cc_type == 3 || cc_type == 2)
402
403 return 0;
404 }
405
407 {
410 // use Inactive screen
411 return ctx->screen + !
ctx->active_screen;
415 // use active screen
416 return ctx->screen +
ctx->active_screen;
417 }
418 /* It was never an option */
420 }
421
423 {
426
428 return;
429
431
432 /* +1 signify cursor_row starts from 0
433 * Can't keep lines less then row cursor pos
434 */
435 keep_lines =
FFMIN(
ctx->cursor_row + 1,
ctx->rollup);
436
438 if (
i >
ctx->cursor_row - keep_lines && i <= ctx->cursor_row)
439 continue;
441 }
442
443 for (
i = 0;
i < keep_lines && screen->
row_used;
i++) {
444 const int i_row =
ctx->cursor_row - keep_lines +
i + 1;
445
453 }
454
456 }
457
459 {
461 struct Screen *screen =
ctx->screen +
ctx->active_screen;
465 const int bidx =
ctx->buffer_index;
466
468
470 {
473 const char *charset = screen->
charsets[
i];
474 j = 0;
476 j++;
479 }
480 }
481
483 {
486 const char *font = screen->
fonts[
i];
487 const char *bg = screen->
bgs[
i];
489 const char *charset = screen->
charsets[
i];
490 const char *override;
491 int x, y, seen_char = 0;
492 j = 0;
493
494 /* skip leading space */
496 j++;
497
500 av_bprintf(&
ctx->buffer[bidx],
"{\\an7}{\\pos(%d,%d)}", x, y);
501
503 const char *e_tag = "", *s_tag = "", *c_tag = "", *b_tag = "";
504
505 if (row[j] == 0)
506 break;
507
508 if (prev_font != font[j]) {
509 switch (prev_font) {
511 e_tag = "{\\i0}";
512 break;
514 e_tag = "{\\u0}";
515 break;
517 e_tag = "{\\u0}{\\i0}";
518 break;
519 }
520 switch (font[j]) {
522 s_tag = "{\\i1}";
523 break;
525 s_tag = "{\\u1}";
526 break;
528 s_tag = "{\\u1}{\\i1}";
529 break;
530 }
531 }
532 if (prev_color !=
color[j]) {
535 c_tag = "{\\c&HFFFFFF&}";
536 break;
538 c_tag = "{\\c&H00FF00&}";
539 break;
541 c_tag = "{\\c&HFF0000&}";
542 break;
544 c_tag = "{\\c&HFFFF00&}";
545 break;
547 c_tag = "{\\c&H0000FF&}";
548 break;
550 c_tag = "{\\c&H00FFFF&}";
551 break;
553 c_tag = "{\\c&HFF00FF&}";
554 break;
555 }
556 }
557 if (prev_bg_color != bg[j]) {
558 switch (bg[j]) {
560 b_tag = "{\3円c&HFFFFFF&}";
561 break;
563 b_tag = "{\3円c&H00FF00&}";
564 break;
566 b_tag = "{\3円c&HFF0000&}";
567 break;
569 b_tag = "{\3円c&HFFFF00&}";
570 break;
572 b_tag = "{\3円c&H0000FF&}";
573 break;
575 b_tag = "{\3円c&H00FFFF&}";
576 break;
578 b_tag = "{\3円c&HFF00FF&}";
579 break;
581 b_tag = "{\3円c&H000000&}";
582 break;
583 }
584 }
585
586 prev_font = font[j];
587 prev_color =
color[j];
588 prev_bg_color = bg[j];
590 if (override[0]) {
591 av_bprintf(&
ctx->buffer[bidx],
"%s%s%s%s%s", e_tag, s_tag, c_tag, b_tag,
override);
592 seen_char = 1;
593 } else if (row[j] == ' ' && !seen_char) {
594 av_bprintf(&
ctx->buffer[bidx],
"%s%s%s%s\\h", e_tag, s_tag, c_tag, b_tag);
595 } else {
596 av_bprintf(&
ctx->buffer[bidx],
"%s%s%s%s%c", e_tag, s_tag, c_tag, b_tag, row[j]);
597 seen_char = 1;
598 }
599
600 }
602 }
603 }
606 if (screen->
row_used &&
ctx->buffer[bidx].len >= 2) {
607 ctx->buffer[bidx].len -= 2;
608 ctx->buffer[bidx].str[
ctx->buffer[bidx].len] = 0;
609 }
610 ctx->buffer_changed = 1;
611 return 0;
612 }
613
615 {
616 ctx->buffer_time[0] =
ctx->buffer_time[1];
617 ctx->buffer_time[1] =
pts;
618 }
619
621 {
622 const int i = (lo & 0xf) >> 1;
623
625 }
626
628 {
631
633 return;
634
637
640 }
641
643 {
644 static const int8_t row_map[] = {
645 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
646 };
647 const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
650
651 if (row_map[
index] <= 0) {
653 return;
654 }
655
656 lo &= 0x1f;
657
658 ctx->cursor_row = row_map[
index] - 1;
662 ctx->cursor_column = 0;
664 for (
i = 0;
i < indent;
i++) {
666 }
667 }
668
670 {
671 struct Screen *screen =
ctx->screen +
ctx->active_screen;
673
674 // In buffered mode, keep writing to screen until it is wiped.
675 // Before wiping the display, capture contents to emit subtitle.
678
681
682 // In realtime mode, emit an empty caption so the last one doesn't
683 // stay on the screen.
686
688 }
689
691 {
693
694 ctx->active_screen = !
ctx->active_screen;
695
696 // In buffered mode, we wait til the *next* EOC and
697 // capture what was already on the screen since the last EOC.
700
701 ctx->cursor_column = 0;
702
703 // In realtime mode, we display the buffered contents (after
704 // flipping the buffer to active above) as soon as EOC arrives.
707
709 }
710
712 {
715 }
716
718 {
720
722
723 switch (hi) {
724 case 0x11:
726 break;
727 case 0x12:
728 if (
ctx->cursor_column > 0)
729 ctx->cursor_column -= 1;
731 break;
732 case 0x13:
733 if (
ctx->cursor_column > 0)
734 ctx->cursor_column -= 1;
736 break;
737 default:
740 break;
741 }
742
743 if (lo) {
745 }
747
749 ctx->screen_touched = 1;
750
751 if (lo)
753 else
755 }
756
758 {
760
761 if (hi ==
ctx->prev_cmd[0] && lo ==
ctx->prev_cmd[1]) {
762 return 0;
763 }
764
765 /* set prev command */
766 ctx->prev_cmd[0] = hi;
767 ctx->prev_cmd[1] = lo;
768
769 if ( (hi == 0x10 && (lo >= 0x40 && lo <= 0x5f)) ||
770 ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
772 } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) ||
773 ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) {
775 } else if ((hi == 0x10 && lo >= 0x20 && lo <= 0x2f)) {
777 } else if (hi == 0x14 || hi == 0x15 || hi == 0x1c) {
778 switch (lo) {
779 case 0x20:
780 /* resume caption loading */
782 break;
783 case 0x24:
785 break;
786 case 0x25:
787 case 0x26:
788 case 0x27:
789 ctx->rollup = lo - 0x23;
791 break;
792 case 0x29:
793 /* resume direct captioning */
795 break;
796 case 0x2b:
797 /* resume text display */
799 break;
800 case 0x2c:
801 /* erase display memory */
803 break;
804 case 0x2d:
805 /* carriage return */
810 ctx->cursor_column = 0;
811 break;
812 case 0x2e:
813 /* erase buffered (non displayed) memory */
814 // Only in realtime mode. In buffered mode, we re-use the inactive screen
815 // for our own buffering.
816 if (
ctx->real_time) {
817 struct Screen *screen =
ctx->screen + !
ctx->active_screen;
819 }
820 break;
821 case 0x2f:
822 /* end of caption */
825 break;
826 default:
827 ff_dlog(
ctx,
"Unknown command 0x%hhx 0x%hhx\n", hi, lo);
828 break;
829 }
830 } else if (hi >= 0x11 && hi <= 0x13) {
831 /* Special characters */
833 } else if (hi >= 0x20) {
834 /* Standard characters (always in pairs) */
836 ctx->prev_cmd[0] =
ctx->prev_cmd[1] = 0;
837 } else if (hi == 0x17 && lo >= 0x21 && lo <= 0x23) {
839 /* Tab offsets (spacing) */
840 for (
i = 0;
i < lo - 0x20;
i++) {
842 }
843 } else {
844 /* Ignoring all other non data code */
845 ff_dlog(
ctx,
"Unknown command 0x%hhx 0x%hhx\n", hi, lo);
846 }
847
849 }
850
852 int *got_sub,
const AVPacket *avpkt)
853 {
858 int bidx =
ctx->buffer_index;
859 const uint8_t *bptr = avpkt->
data;
863 unsigned nb_rect_allocated = 0;
864
865 for (
i = 0;
i <
len;
i += 3) {
866 uint8_t hi, cc_type = bptr[
i] & 1;
867
868 if (
ctx->data_field < 0)
869 ctx->data_field = cc_type;
870
872 continue;
873
874 if (cc_type !=
ctx->data_field)
875 continue;
876
880
881 if (!
ctx->buffer_changed)
882 continue;
883 ctx->buffer_changed = 0;
884
886 ctx->buffer_index = bidx = !
ctx->buffer_index;
887
889
890 if (
ctx->buffer[bidx].str[0] ||
ctx->real_time) {
891 ff_dlog(
ctx,
"cdp writing data (%s)\n",
ctx->buffer[bidx].str);
894 end_time =
ctx->buffer_time[1];
898 else
903 ctx->last_real_time = sub->
pts;
904 ctx->screen_touched = 0;
905 }
906 }
907
908 if (!bptr && !
ctx->real_time &&
ctx->buffer[!
ctx->buffer_index].str[0]) {
909 bidx = !
ctx->buffer_index;
914 sub->
pts =
ctx->buffer_time[1];
919 }
920
921 if (
ctx->real_time &&
ctx->screen_touched &&
923 ctx->last_real_time = sub->
pts;
924 ctx->screen_touched = 0;
925
927 ctx->buffer_changed = 0;
928
933 }
934
937 }
938
939 #define OFFSET(x) offsetof(CCaptionSubContext, x)
940 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
942 {
"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 },
943 {
"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 },
944 {
"data_field",
"select data field",
OFFSET(data_field),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
SD, .unit =
"data_field" },
945 {
"auto",
"pick first one that appears", 0,
AV_OPT_TYPE_CONST, { .i64 =-1 }, 0, 0,
SD, .unit =
"data_field" },
949 };
950
956 };
957
970 };