2 * Closed Caption Decoding
3 * Copyright (c) 2015 Anshul Maheshwari
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/opt.h"
26 #define SCREEN_ROWS 15
27 #define SCREEN_COLUMNS 32
29 #define SET_FLAG(var, val) ( (var) |= ( 1 << (val)) )
30 #define UNSET_FLAG(var, val) ( (var) &= ~( 1 << (val)) )
31 #define CHECK_FLAG(var, val) ( (var) & ( 1 << (val)) )
33 static const AVRational ms_tb = {1, 1000};
59 CCFONT_UNDERLINED_ITALICS,
64 CCSET_SPECIAL_AMERICAN,
65 CCSET_EXTENDED_SPANISH_FRENCH_MISC,
66 CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH,
69 static const char *charset_overrides[4][128] =
71 [CCSET_BASIC_AMERICAN] = {
84 [CCSET_SPECIAL_AMERICAN] = {
102 [CCSET_EXTENDED_SPANISH_FRENCH_MISC] = {
136 [CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH] = {
172 static const unsigned char bg_attribs[8] = // Color
184 static const unsigned char pac2_attribs[32][3] = // Color, font, ident
186 { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x40 || 0x60
187 { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x41 || 0x61
188 { CCCOL_GREEN, CCFONT_REGULAR, 0 }, // 0x42 || 0x62
189 { CCCOL_GREEN, CCFONT_UNDERLINED, 0 }, // 0x43 || 0x63
190 { CCCOL_BLUE, CCFONT_REGULAR, 0 }, // 0x44 || 0x64
191 { CCCOL_BLUE, CCFONT_UNDERLINED, 0 }, // 0x45 || 0x65
192 { CCCOL_CYAN, CCFONT_REGULAR, 0 }, // 0x46 || 0x66
193 { CCCOL_CYAN, CCFONT_UNDERLINED, 0 }, // 0x47 || 0x67
194 { CCCOL_RED, CCFONT_REGULAR, 0 }, // 0x48 || 0x68
195 { CCCOL_RED, CCFONT_UNDERLINED, 0 }, // 0x49 || 0x69
196 { CCCOL_YELLOW, CCFONT_REGULAR, 0 }, // 0x4a || 0x6a
197 { CCCOL_YELLOW, CCFONT_UNDERLINED, 0 }, // 0x4b || 0x6b
198 { CCCOL_MAGENTA, CCFONT_REGULAR, 0 }, // 0x4c || 0x6c
199 { CCCOL_MAGENTA, CCFONT_UNDERLINED, 0 }, // 0x4d || 0x6d
200 { CCCOL_WHITE, CCFONT_ITALICS, 0 }, // 0x4e || 0x6e
201 { CCCOL_WHITE, CCFONT_UNDERLINED_ITALICS, 0 }, // 0x4f || 0x6f
202 { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x50 || 0x70
203 { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x51 || 0x71
204 { CCCOL_WHITE, CCFONT_REGULAR, 4 }, // 0x52 || 0x72
205 { CCCOL_WHITE, CCFONT_UNDERLINED, 4 }, // 0x53 || 0x73
206 { CCCOL_WHITE, CCFONT_REGULAR, 8 }, // 0x54 || 0x74
207 { CCCOL_WHITE, CCFONT_UNDERLINED, 8 }, // 0x55 || 0x75
208 { CCCOL_WHITE, CCFONT_REGULAR, 12 }, // 0x56 || 0x76
209 { CCCOL_WHITE, CCFONT_UNDERLINED, 12 }, // 0x57 || 0x77
210 { CCCOL_WHITE, CCFONT_REGULAR, 16 }, // 0x58 || 0x78
211 { CCCOL_WHITE, CCFONT_UNDERLINED, 16 }, // 0x59 || 0x79
212 { CCCOL_WHITE, CCFONT_REGULAR, 20 }, // 0x5a || 0x7a
213 { CCCOL_WHITE, CCFONT_UNDERLINED, 20 }, // 0x5b || 0x7b
214 { CCCOL_WHITE, CCFONT_REGULAR, 24 }, // 0x5c || 0x7c
215 { CCCOL_WHITE, CCFONT_UNDERLINED, 24 }, // 0x5d || 0x7d
216 { CCCOL_WHITE, CCFONT_REGULAR, 28 }, // 0x5e || 0x7e
217 { CCCOL_WHITE, CCFONT_UNDERLINED, 28 } // 0x5f || 0x7f
218 /* total 32 entries */
222 /* +1 is used to compensate null character of string */
223 uint8_t characters[SCREEN_ROWS+1][SCREEN_COLUMNS+1];
224 uint8_t charsets[SCREEN_ROWS+1][SCREEN_COLUMNS+1];
225 uint8_t colors[SCREEN_ROWS+1][SCREEN_COLUMNS+1];
226 uint8_t bgs[SCREEN_ROWS+1][SCREEN_COLUMNS+1];
227 uint8_t fonts[SCREEN_ROWS+1][SCREEN_COLUMNS+1];
229 * Bitmask of used rows; if a bit is not set, the
230 * corresponding row is not used.
231 * for setting row 1 use row | (1 << 0)
232 * for setting row 15 use row | (1 << 14)
237 typedef struct CCaptionSubContext {
241 struct Screen screen[2];
244 uint8_t cursor_column;
245 uint8_t cursor_color;
248 uint8_t cursor_charset;
254 int64_t buffer_time[2];
256 int64_t last_real_time;
259 } CCaptionSubContext;
261 static av_cold int init_decoder(AVCodecContext *avctx)
264 CCaptionSubContext *ctx = avctx->priv_data;
266 av_bprint_init(&ctx->buffer[0], 0, AV_BPRINT_SIZE_UNLIMITED);
267 av_bprint_init(&ctx->buffer[1], 0, AV_BPRINT_SIZE_UNLIMITED);
268 /* taking by default roll up to 2 */
269 ctx->mode = CCMODE_ROLLUP;
270 ctx->bg_color = CCCOL_BLACK;
272 ctx->cursor_row = 10;
273 ret = ff_ass_subtitle_header(avctx, "Monospace",
274 ASS_DEFAULT_FONT_SIZE,
276 ASS_DEFAULT_BACK_COLOR,
279 ASS_DEFAULT_UNDERLINE,
281 ASS_DEFAULT_ALIGNMENT);
289 static av_cold int close_decoder(AVCodecContext *avctx)
291 CCaptionSubContext *ctx = avctx->priv_data;
292 av_bprint_finalize(&ctx->buffer[0], NULL);
293 av_bprint_finalize(&ctx->buffer[1], NULL);
297 static void flush_decoder(AVCodecContext *avctx)
299 CCaptionSubContext *ctx = avctx->priv_data;
300 ctx->screen[0].row_used = 0;
301 ctx->screen[1].row_used = 0;
302 ctx->prev_cmd[0] = 0;
303 ctx->prev_cmd[1] = 0;
304 ctx->mode = CCMODE_ROLLUP;
306 ctx->cursor_row = 10;
307 ctx->cursor_column = 0;
308 ctx->cursor_font = 0;
309 ctx->cursor_color = 0;
310 ctx->bg_color = CCCOL_BLACK;
311 ctx->cursor_charset = 0;
312 ctx->active_screen = 0;
313 ctx->last_real_time = 0;
314 ctx->screen_touched = 0;
315 ctx->buffer_changed = 0;
316 if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
318 av_bprint_clear(&ctx->buffer[0]);
319 av_bprint_clear(&ctx->buffer[1]);
323 * @param ctx closed caption context just to print log
325 static void write_char(CCaptionSubContext *ctx, struct Screen *screen, char ch)
327 uint8_t col = ctx->cursor_column;
328 char *row = screen->characters[ctx->cursor_row];
329 char *font = screen->fonts[ctx->cursor_row];
330 char *color = screen->colors[ctx->cursor_row];
331 char *bg = screen->bgs[ctx->cursor_row];
332 char *charset = screen->charsets[ctx->cursor_row];
334 if (col < SCREEN_COLUMNS) {
336 font[col] = ctx->cursor_font;
337 color[col] = ctx->cursor_color;
338 bg[col] = ctx->bg_color;
339 charset[col] = ctx->cursor_charset;
340 ctx->cursor_charset = CCSET_BASIC_AMERICAN;
341 if (ch) ctx->cursor_column++;
344 /* We have extra space at end only for null character */
345 else if (col == SCREEN_COLUMNS && ch == 0) {
350 av_log(ctx, AV_LOG_WARNING, "Data Ignored since exceeding screen width\n");
356 * This function after validating parity bit, also remove it from data pair.
357 * The first byte doesn't pass parity, we replace it with a solid blank
358 * and process the pair.
359 * If the second byte doesn't pass parity, it returns INVALIDDATA
360 * user can ignore the whole pair and pass the other pair.
362 static int validate_cc_data_pair(const uint8_t *cc_data_pair, uint8_t *hi)
364 uint8_t cc_valid = (*cc_data_pair & 4) >>2;
365 uint8_t cc_type = *cc_data_pair & 3;
367 *hi = cc_data_pair[1];
370 return AVERROR_INVALIDDATA;
372 // if EIA-608 data then verify parity.
373 if (cc_type==0 || cc_type==1) {
374 if (!av_parity(cc_data_pair[2])) {
375 return AVERROR_INVALIDDATA;
377 if (!av_parity(cc_data_pair[1])) {
383 if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD)
384 && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0)
385 return AVERROR_PATCHWELCOME;
388 if (cc_type == 3 || cc_type == 2)
389 return AVERROR_PATCHWELCOME;
394 static struct Screen *get_writing_screen(CCaptionSubContext *ctx)
398 // use Inactive screen
399 return ctx->screen + !ctx->active_screen;
404 return ctx->screen + ctx->active_screen;
406 /* It was never an option */
410 static void roll_up(CCaptionSubContext *ctx)
412 struct Screen *screen;
415 if (ctx->mode == CCMODE_TEXT)
418 screen = get_writing_screen(ctx);
420 /* +1 signify cursor_row starts from 0
421 * Can't keep lines less then row cursor pos
423 keep_lines = FFMIN(ctx->cursor_row + 1, ctx->rollup);
425 for (i = 0; i < SCREEN_ROWS; i++) {
426 if (i > ctx->cursor_row - keep_lines && i <= ctx->cursor_row)
428 UNSET_FLAG(screen->row_used, i);
431 for (i = 0; i < keep_lines && screen->row_used; i++) {
432 const int i_row = ctx->cursor_row - keep_lines + i + 1;
434 memcpy(screen->characters[i_row], screen->characters[i_row+1], SCREEN_COLUMNS);
435 memcpy(screen->colors[i_row], screen->colors[i_row+1], SCREEN_COLUMNS);
436 memcpy(screen->bgs[i_row], screen->bgs[i_row+1], SCREEN_COLUMNS);
437 memcpy(screen->fonts[i_row], screen->fonts[i_row+1], SCREEN_COLUMNS);
438 memcpy(screen->charsets[i_row], screen->charsets[i_row+1], SCREEN_COLUMNS);
439 if (CHECK_FLAG(screen->row_used, i_row + 1))
440 SET_FLAG(screen->row_used, i_row);
443 UNSET_FLAG(screen->row_used, ctx->cursor_row);
446 static int capture_screen(CCaptionSubContext *ctx)
449 struct Screen *screen = ctx->screen + ctx->active_screen;
450 enum cc_font prev_font = CCFONT_REGULAR;
451 enum cc_color_code prev_color = CCCOL_WHITE;
452 enum cc_color_code prev_bg_color = CCCOL_BLACK;
453 const int bidx = ctx->buffer_index;
455 av_bprint_clear(&ctx->buffer[bidx]);
457 for (i = 0; screen->row_used && i < SCREEN_ROWS; i++)
459 if (CHECK_FLAG(screen->row_used, i)) {
460 const char *row = screen->characters[i];
461 const char *charset = screen->charsets[i];
463 while (row[j] == ' ' && charset[j] == CCSET_BASIC_AMERICAN)
470 for (i = 0; screen->row_used && i < SCREEN_ROWS; i++)
472 if (CHECK_FLAG(screen->row_used, i)) {
473 const char *row = screen->characters[i];
474 const char *font = screen->fonts[i];
475 const char *bg = screen->bgs[i];
476 const char *color = screen->colors[i];
477 const char *charset = screen->charsets[i];
478 const char *override;
479 int x, y, seen_char = 0;
482 /* skip leading space */
483 while (row[j] == ' ' && charset[j] == CCSET_BASIC_AMERICAN && j < tab)
486 x = ASS_DEFAULT_PLAYRESX * (0.1 + 0.0250 * j);
487 y = ASS_DEFAULT_PLAYRESY * (0.1 + 0.0533 * i);
488 av_bprintf(&ctx->buffer[bidx], "{\\an7}{\\pos(%d,%d)}", x, y);
490 for (; j < SCREEN_COLUMNS; j++) {
491 const char *e_tag = "", *s_tag = "", *c_tag = "", *b_tag = "";
496 if (prev_font != font[j]) {
501 case CCFONT_UNDERLINED:
504 case CCFONT_UNDERLINED_ITALICS:
505 e_tag = "{\\u0}{\\i0}";
512 case CCFONT_UNDERLINED:
515 case CCFONT_UNDERLINED_ITALICS:
516 s_tag = "{\\u1}{\\i1}";
520 if (prev_color != color[j]) {
523 c_tag = "{\\c&HFFFFFF&}";
526 c_tag = "{\\c&H00FF00&}";
529 c_tag = "{\\c&HFF0000&}";
532 c_tag = "{\\c&HFFFF00&}";
535 c_tag = "{\\c&H0000FF&}";
538 c_tag = "{\\c&H00FFFF&}";
541 c_tag = "{\\c&HFF00FF&}";
545 if (prev_bg_color != bg[j]) {
548 b_tag = "{\\3c&HFFFFFF&}";
551 b_tag = "{\\3c&H00FF00&}";
554 b_tag = "{\\3c&HFF0000&}";
557 b_tag = "{\\3c&HFFFF00&}";
560 b_tag = "{\\3c&H0000FF&}";
563 b_tag = "{\\3c&H00FFFF&}";
566 b_tag = "{\\3c&HFF00FF&}";
569 b_tag = "{\\3c&H000000&}";
575 prev_color = color[j];
576 prev_bg_color = bg[j];
577 override = charset_overrides[(int)charset[j]][(int)row[j]];
579 av_bprintf(&ctx->buffer[bidx], "%s%s%s%s%s", e_tag, s_tag, c_tag, b_tag, override);
581 } else if (row[j] == ' ' && !seen_char) {
582 av_bprintf(&ctx->buffer[bidx], "%s%s%s%s\\h", e_tag, s_tag, c_tag, b_tag);
584 av_bprintf(&ctx->buffer[bidx], "%s%s%s%s%c", e_tag, s_tag, c_tag, b_tag, row[j]);
589 av_bprintf(&ctx->buffer[bidx], "\\N");
592 if (!av_bprint_is_complete(&ctx->buffer[bidx]))
593 return AVERROR(ENOMEM);
594 if (screen->row_used && ctx->buffer[bidx].len >= 2) {
595 ctx->buffer[bidx].len -= 2;
596 ctx->buffer[bidx].str[ctx->buffer[bidx].len] = 0;
598 ctx->buffer_changed = 1;
602 static void update_time(CCaptionSubContext *ctx, int64_t pts)
604 ctx->buffer_time[0] = ctx->buffer_time[1];
605 ctx->buffer_time[1] = pts;
608 static void handle_bgattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
610 const int i = (lo & 0xf) >> 1;
612 ctx->bg_color = bg_attribs[i];
615 static void handle_textattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
618 struct Screen *screen = get_writing_screen(ctx);
623 ctx->cursor_color = pac2_attribs[i][0];
624 ctx->cursor_font = pac2_attribs[i][1];
626 SET_FLAG(screen->row_used, ctx->cursor_row);
627 write_char(ctx, screen, ' ');
630 static void handle_pac(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
632 static const int8_t row_map[] = {
633 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
635 const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
636 struct Screen *screen = get_writing_screen(ctx);
639 if (row_map[index] <= 0) {
640 av_log(ctx, AV_LOG_DEBUG, "Invalid pac index encountered\n");
646 ctx->cursor_row = row_map[index] - 1;
647 ctx->cursor_color = pac2_attribs[lo][0];
648 ctx->cursor_font = pac2_attribs[lo][1];
649 ctx->cursor_charset = CCSET_BASIC_AMERICAN;
650 ctx->cursor_column = 0;
651 indent = pac2_attribs[lo][2];
652 for (i = 0; i < indent; i++) {
653 write_char(ctx, screen, ' ');
657 static int handle_edm(CCaptionSubContext *ctx)
659 struct Screen *screen = ctx->screen + ctx->active_screen;
662 // In buffered mode, keep writing to screen until it is wiped.
663 // Before wiping the display, capture contents to emit subtitle.
665 ret = capture_screen(ctx);
667 screen->row_used = 0;
668 ctx->bg_color = CCCOL_BLACK;
670 // In realtime mode, emit an empty caption so the last one doesn't
671 // stay on the screen.
673 ret = capture_screen(ctx);
678 static int handle_eoc(CCaptionSubContext *ctx)
682 ctx->active_screen = !ctx->active_screen;
684 // In buffered mode, we wait til the *next* EOC and
685 // capture what was already on the screen since the last EOC.
687 ret = handle_edm(ctx);
689 ctx->cursor_column = 0;
691 // In realtime mode, we display the buffered contents (after
692 // flipping the buffer to active above) as soon as EOC arrives.
694 ret = capture_screen(ctx);
699 static void handle_delete_end_of_row(CCaptionSubContext *ctx)
701 struct Screen *screen = get_writing_screen(ctx);
702 write_char(ctx, screen, 0);
705 static void handle_char(CCaptionSubContext *ctx, char hi, char lo)
707 struct Screen *screen = get_writing_screen(ctx);
709 SET_FLAG(screen->row_used, ctx->cursor_row);
713 ctx->cursor_charset = CCSET_SPECIAL_AMERICAN;
716 if (ctx->cursor_column > 0)
717 ctx->cursor_column -= 1;
718 ctx->cursor_charset = CCSET_EXTENDED_SPANISH_FRENCH_MISC;
721 if (ctx->cursor_column > 0)
722 ctx->cursor_column -= 1;
723 ctx->cursor_charset = CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH;
726 ctx->cursor_charset = CCSET_BASIC_AMERICAN;
727 write_char(ctx, screen, hi);
732 write_char(ctx, screen, lo);
734 write_char(ctx, screen, 0);
736 if (ctx->mode != CCMODE_POPON)
737 ctx->screen_touched = 1;
740 ff_dlog(ctx, "(%c,%c)\n", hi, lo);
742 ff_dlog(ctx, "(%c)\n", hi);
745 static int process_cc608(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
749 if (hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) {
753 /* set prev command */
754 ctx->prev_cmd[0] = hi;
755 ctx->prev_cmd[1] = lo;
757 if ( (hi == 0x10 && (lo >= 0x40 && lo <= 0x5f)) ||
758 ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
759 handle_pac(ctx, hi, lo);
760 } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) ||
761 ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) {
762 handle_textattr(ctx, hi, lo);
763 } else if ((hi == 0x10 && lo >= 0x20 && lo <= 0x2f)) {
764 handle_bgattr(ctx, hi, lo);
765 } else if (hi == 0x14 || hi == 0x15 || hi == 0x1c) {
768 /* resume caption loading */
769 ctx->mode = CCMODE_POPON;
772 handle_delete_end_of_row(ctx);
777 ctx->rollup = lo - 0x23;
778 ctx->mode = CCMODE_ROLLUP;
781 /* resume direct captioning */
782 ctx->mode = CCMODE_PAINTON;
785 /* resume text display */
786 ctx->mode = CCMODE_TEXT;
789 /* erase display memory */
793 /* carriage return */
794 ff_dlog(ctx, "carriage return\n");
796 ret = capture_screen(ctx);
798 ctx->cursor_column = 0;
801 /* erase buffered (non displayed) memory */
802 // Only in realtime mode. In buffered mode, we re-use the inactive screen
803 // for our own buffering.
804 if (ctx->real_time) {
805 struct Screen *screen = ctx->screen + !ctx->active_screen;
806 screen->row_used = 0;
811 ff_dlog(ctx, "handle_eoc\n");
812 ret = handle_eoc(ctx);
815 ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
818 } else if (hi >= 0x11 && hi <= 0x13) {
819 /* Special characters */
820 handle_char(ctx, hi, lo);
821 } else if (hi >= 0x20) {
822 /* Standard characters (always in pairs) */
823 handle_char(ctx, hi, lo);
824 ctx->prev_cmd[0] = ctx->prev_cmd[1] = 0;
825 } else if (hi == 0x17 && lo >= 0x21 && lo <= 0x23) {
827 /* Tab offsets (spacing) */
828 for (i = 0; i < lo - 0x20; i++) {
829 handle_char(ctx, ' ', 0);
832 /* Ignoring all other non data code */
833 ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
839 static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
841 CCaptionSubContext *ctx = avctx->priv_data;
842 AVSubtitle *sub = data;
843 int64_t in_time = sub->pts;
846 int bidx = ctx->buffer_index;
847 const uint8_t *bptr = avpkt->data;
848 int len = avpkt->size;
852 for (i = 0; i < len; i += 3) {
853 uint8_t hi, cc_type = bptr[i] & 1;
855 if (ctx->data_field < 0)
856 ctx->data_field = cc_type;
858 if (validate_cc_data_pair(bptr + i, &hi))
861 if (cc_type != ctx->data_field)
864 ret = process_cc608(ctx, hi & 0x7f, bptr[i + 2] & 0x7f);
868 if (!ctx->buffer_changed)
870 ctx->buffer_changed = 0;
872 if (!ctx->real_time && ctx->mode == CCMODE_POPON)
873 ctx->buffer_index = bidx = !ctx->buffer_index;
875 update_time(ctx, in_time);
877 if (ctx->buffer[bidx].str[0] || ctx->real_time) {
878 ff_dlog(ctx, "cdp writing data (%s)\n", ctx->buffer[bidx].str);
879 start_time = ctx->buffer_time[0];
880 sub->pts = start_time;
881 end_time = ctx->buffer_time[1];
883 sub->end_display_time = av_rescale_q(end_time - start_time,
884 AV_TIME_BASE_Q, ms_tb);
886 sub->end_display_time = -1;
887 ret = ff_ass_add_rect(sub, ctx->buffer[bidx].str, ctx->readorder++, 0, NULL, NULL);
890 ctx->last_real_time = sub->pts;
891 ctx->screen_touched = 0;
895 if (!bptr && !ctx->real_time && ctx->buffer[!ctx->buffer_index].str[0]) {
896 bidx = !ctx->buffer_index;
897 ret = ff_ass_add_rect(sub, ctx->buffer[bidx].str, ctx->readorder++, 0, NULL, NULL);
900 sub->pts = ctx->buffer_time[1];
901 sub->end_display_time = av_rescale_q(ctx->buffer_time[1] - ctx->buffer_time[0],
902 AV_TIME_BASE_Q, ms_tb);
903 if (sub->end_display_time == 0)
904 sub->end_display_time = ctx->buffer[bidx].len * 20;
907 if (ctx->real_time && ctx->screen_touched &&
908 sub->pts > ctx->last_real_time + av_rescale_q(200, ms_tb, AV_TIME_BASE_Q)) {
909 ctx->last_real_time = sub->pts;
910 ctx->screen_touched = 0;
913 ctx->buffer_changed = 0;
915 ret = ff_ass_add_rect(sub, ctx->buffer[bidx].str, ctx->readorder++, 0, NULL, NULL);
918 sub->end_display_time = -1;
921 *got_sub = sub->num_rects > 0;
925 #define OFFSET(x) offsetof(CCaptionSubContext, x)
926 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
927 static const AVOption options[] = {
928 { "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 },
929 { "data_field", "select data field", OFFSET(data_field), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, SD, "data_field" },
930 { "auto", "pick first one that appears", 0, AV_OPT_TYPE_CONST, { .i64 =-1 }, 0, 0, SD, "data_field" },
931 { "first", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, SD, "data_field" },
932 { "second", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, SD, "data_field" },
936 static const AVClass ccaption_dec_class = {
937 .class_name = "Closed caption Decoder",
938 .item_name = av_default_item_name,
940 .version = LIBAVUTIL_VERSION_INT,
943 AVCodec ff_ccaption_decoder = {
945 .long_name = NULL_IF_CONFIG_SMALL("Closed Caption (EIA-608 / CEA-708)"),
946 .type = AVMEDIA_TYPE_SUBTITLE,
947 .id = AV_CODEC_ID_EIA_608,
948 .priv_data_size = sizeof(CCaptionSubContext),
949 .init = init_decoder,
950 .close = close_decoder,
951 .flush = flush_decoder,
953 .priv_class = &ccaption_dec_class,
954 .capabilities = AV_CODEC_CAP_DELAY,