| Line | Branch | Exec | Source |
|---|---|---|---|
| 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> | ||
| 23 | #include "avcodec.h" | ||
| 24 | #include "ass.h" | ||
| 25 | #include "codec_internal.h" | ||
| 26 | #include "libavutil/attributes.h" | ||
| 27 | #include "libavutil/opt.h" | ||
| 28 | |||
| 29 | #define SCREEN_ROWS 15 | ||
| 30 | #define SCREEN_COLUMNS 32 | ||
| 31 | |||
| 32 | #define SET_FLAG(var, val) ( (var) |= ( 1 << (val)) ) | ||
| 33 | #define UNSET_FLAG(var, val) ( (var) &= ~( 1 << (val)) ) | ||
| 34 | #define CHECK_FLAG(var, val) ( (var) & ( 1 << (val)) ) | ||
| 35 | |||
| 36 | static const AVRational ms_tb = {1, 1000}; | ||
| 37 | |||
| 38 | enum cc_mode { | ||
| 39 | CCMODE_POPON, | ||
| 40 | CCMODE_PAINTON, | ||
| 41 | CCMODE_ROLLUP, | ||
| 42 | CCMODE_TEXT, | ||
| 43 | }; | ||
| 44 | |||
| 45 | enum cc_color_code { | ||
| 46 | CCCOL_WHITE, | ||
| 47 | CCCOL_GREEN, | ||
| 48 | CCCOL_BLUE, | ||
| 49 | CCCOL_CYAN, | ||
| 50 | CCCOL_RED, | ||
| 51 | CCCOL_YELLOW, | ||
| 52 | CCCOL_MAGENTA, | ||
| 53 | CCCOL_USERDEFINED, | ||
| 54 | CCCOL_BLACK, | ||
| 55 | CCCOL_TRANSPARENT, | ||
| 56 | }; | ||
| 57 | |||
| 58 | enum cc_font { | ||
| 59 | CCFONT_REGULAR, | ||
| 60 | CCFONT_ITALICS, | ||
| 61 | CCFONT_UNDERLINED, | ||
| 62 | CCFONT_UNDERLINED_ITALICS, | ||
| 63 | }; | ||
| 64 | |||
| 65 | enum cc_charset { | ||
| 66 | CCSET_BASIC_AMERICAN, | ||
| 67 | CCSET_SPECIAL_AMERICAN, | ||
| 68 | CCSET_EXTENDED_SPANISH_FRENCH_MISC, | ||
| 69 | CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH, | ||
| 70 | }; | ||
| 71 | |||
| 72 | #define CHARSET_OVERRIDE_LIST(START_SET, ENTRY, END_SET) \ | ||
| 73 | START_SET(CCSET_BASIC_AMERICAN) \ | ||
| 74 | ENTRY(0x27, "\u2019") \ | ||
| 75 | ENTRY(0x2a, "\u00e1") \ | ||
| 76 | ENTRY(0x5c, "\u00e9") \ | ||
| 77 | ENTRY(0x5e, "\u00ed") \ | ||
| 78 | ENTRY(0x5f, "\u00f3") \ | ||
| 79 | ENTRY(0x60, "\u00fa") \ | ||
| 80 | ENTRY(0x7b, "\u00e7") \ | ||
| 81 | ENTRY(0x7c, "\u00f7") \ | ||
| 82 | ENTRY(0x7d, "\u00d1") \ | ||
| 83 | ENTRY(0x7e, "\u00f1") \ | ||
| 84 | ENTRY(0x7f, "\u2588") \ | ||
| 85 | END_SET \ | ||
| 86 | START_SET(CCSET_SPECIAL_AMERICAN) \ | ||
| 87 | ENTRY(0x30, "\u00ae") \ | ||
| 88 | ENTRY(0x31, "\u00b0") \ | ||
| 89 | ENTRY(0x32, "\u00bd") \ | ||
| 90 | ENTRY(0x33, "\u00bf") \ | ||
| 91 | ENTRY(0x34, "\u2122") \ | ||
| 92 | ENTRY(0x35, "\u00a2") \ | ||
| 93 | ENTRY(0x36, "\u00a3") \ | ||
| 94 | ENTRY(0x37, "\u266a") \ | ||
| 95 | ENTRY(0x38, "\u00e0") \ | ||
| 96 | ENTRY(0x39, "\u00A0") \ | ||
| 97 | ENTRY(0x3a, "\u00e8") \ | ||
| 98 | ENTRY(0x3b, "\u00e2") \ | ||
| 99 | ENTRY(0x3c, "\u00ea") \ | ||
| 100 | ENTRY(0x3d, "\u00ee") \ | ||
| 101 | ENTRY(0x3e, "\u00f4") \ | ||
| 102 | ENTRY(0x3f, "\u00fb") \ | ||
| 103 | END_SET \ | ||
| 104 | START_SET(CCSET_EXTENDED_SPANISH_FRENCH_MISC) \ | ||
| 105 | ENTRY(0x20, "\u00c1") \ | ||
| 106 | ENTRY(0x21, "\u00c9") \ | ||
| 107 | ENTRY(0x22, "\u00d3") \ | ||
| 108 | ENTRY(0x23, "\u00da") \ | ||
| 109 | ENTRY(0x24, "\u00dc") \ | ||
| 110 | ENTRY(0x25, "\u00fc") \ | ||
| 111 | ENTRY(0x26, "\u00b4") \ | ||
| 112 | ENTRY(0x27, "\u00a1") \ | ||
| 113 | ENTRY(0x28, "*") \ | ||
| 114 | ENTRY(0x29, "\u2018") \ | ||
| 115 | ENTRY(0x2a, "-") \ | ||
| 116 | ENTRY(0x2b, "\u00a9") \ | ||
| 117 | ENTRY(0x2c, "\u2120") \ | ||
| 118 | ENTRY(0x2d, "\u00b7") \ | ||
| 119 | ENTRY(0x2e, "\u201c") \ | ||
| 120 | ENTRY(0x2f, "\u201d") \ | ||
| 121 | ENTRY(0x30, "\u00c0") \ | ||
| 122 | ENTRY(0x31, "\u00c2") \ | ||
| 123 | ENTRY(0x32, "\u00c7") \ | ||
| 124 | ENTRY(0x33, "\u00c8") \ | ||
| 125 | ENTRY(0x34, "\u00ca") \ | ||
| 126 | ENTRY(0x35, "\u00cb") \ | ||
| 127 | ENTRY(0x36, "\u00eb") \ | ||
| 128 | ENTRY(0x37, "\u00ce") \ | ||
| 129 | ENTRY(0x38, "\u00cf") \ | ||
| 130 | ENTRY(0x39, "\u00ef") \ | ||
| 131 | ENTRY(0x3a, "\u00d4") \ | ||
| 132 | ENTRY(0x3b, "\u00d9") \ | ||
| 133 | ENTRY(0x3c, "\u00f9") \ | ||
| 134 | ENTRY(0x3d, "\u00db") \ | ||
| 135 | ENTRY(0x3e, "\u00ab") \ | ||
| 136 | ENTRY(0x3f, "\u00bb") \ | ||
| 137 | END_SET \ | ||
| 138 | START_SET(CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH) \ | ||
| 139 | ENTRY(0x20, "\u00c3") \ | ||
| 140 | ENTRY(0x21, "\u00e3") \ | ||
| 141 | ENTRY(0x22, "\u00cd") \ | ||
| 142 | ENTRY(0x23, "\u00cc") \ | ||
| 143 | ENTRY(0x24, "\u00ec") \ | ||
| 144 | ENTRY(0x25, "\u00d2") \ | ||
| 145 | ENTRY(0x26, "\u00f2") \ | ||
| 146 | ENTRY(0x27, "\u00d5") \ | ||
| 147 | ENTRY(0x28, "\u00f5") \ | ||
| 148 | ENTRY(0x29, "{") \ | ||
| 149 | ENTRY(0x2a, "}") \ | ||
| 150 | ENTRY(0x2b, "\\") \ | ||
| 151 | ENTRY(0x2c, "^") \ | ||
| 152 | ENTRY(0x2d, "_") \ | ||
| 153 | ENTRY(0x2e, "|") \ | ||
| 154 | ENTRY(0x2f, "~") \ | ||
| 155 | ENTRY(0x30, "\u00c4") \ | ||
| 156 | ENTRY(0x31, "\u00e4") \ | ||
| 157 | ENTRY(0x32, "\u00d6") \ | ||
| 158 | ENTRY(0x33, "\u00f6") \ | ||
| 159 | ENTRY(0x34, "\u00df") \ | ||
| 160 | ENTRY(0x35, "\u00a5") \ | ||
| 161 | ENTRY(0x36, "\u00a4") \ | ||
| 162 | ENTRY(0x37, "\u00a6") \ | ||
| 163 | ENTRY(0x38, "\u00c5") \ | ||
| 164 | ENTRY(0x39, "\u00e5") \ | ||
| 165 | ENTRY(0x3a, "\u00d8") \ | ||
| 166 | ENTRY(0x3b, "\u00f8") \ | ||
| 167 | ENTRY(0x3c, "\u250c") \ | ||
| 168 | ENTRY(0x3d, "\u2510") \ | ||
| 169 | ENTRY(0x3e, "\u2514") \ | ||
| 170 | ENTRY(0x3f, "\u2518") \ | ||
| 171 | END_SET \ | ||
| 172 | |||
| 173 | static const char charset_overrides[4][128][sizeof("\u266a")] = | ||
| 174 | { | ||
| 175 | #define START_SET(IDX) \ | ||
| 176 | [IDX] = { | ||
| 177 | #define ENTRY(idx, string) \ | ||
| 178 | [idx] = string, | ||
| 179 | #define END_SET \ | ||
| 180 | }, | ||
| 181 | CHARSET_OVERRIDE_LIST(START_SET, ENTRY, END_SET) | ||
| 182 | }; | ||
| 183 | #define EMPTY_START(IDX) | ||
| 184 | #define EMPTY_END | ||
| 185 | #define ASSERT_ENTRY(IDX, str) \ | ||
| 186 | static_assert(sizeof(str) <= sizeof(charset_overrides[0][0]), \ | ||
| 187 | "'" str "' string takes too much space"); | ||
| 188 | CHARSET_OVERRIDE_LIST(EMPTY_START, ASSERT_ENTRY, EMPTY_END) | ||
| 189 | |||
| 190 | static const unsigned char bg_attribs[8] = // Color | ||
| 191 | { | ||
| 192 | CCCOL_WHITE, | ||
| 193 | CCCOL_GREEN, | ||
| 194 | CCCOL_BLUE, | ||
| 195 | CCCOL_CYAN, | ||
| 196 | CCCOL_RED, | ||
| 197 | CCCOL_YELLOW, | ||
| 198 | CCCOL_MAGENTA, | ||
| 199 | CCCOL_BLACK, | ||
| 200 | }; | ||
| 201 | |||
| 202 | static const unsigned char pac2_attribs[32][3] = // Color, font, ident | ||
| 203 | { | ||
| 204 | { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x40 || 0x60 | ||
| 205 | { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x41 || 0x61 | ||
| 206 | { CCCOL_GREEN, CCFONT_REGULAR, 0 }, // 0x42 || 0x62 | ||
| 207 | { CCCOL_GREEN, CCFONT_UNDERLINED, 0 }, // 0x43 || 0x63 | ||
| 208 | { CCCOL_BLUE, CCFONT_REGULAR, 0 }, // 0x44 || 0x64 | ||
| 209 | { CCCOL_BLUE, CCFONT_UNDERLINED, 0 }, // 0x45 || 0x65 | ||
| 210 | { CCCOL_CYAN, CCFONT_REGULAR, 0 }, // 0x46 || 0x66 | ||
| 211 | { CCCOL_CYAN, CCFONT_UNDERLINED, 0 }, // 0x47 || 0x67 | ||
| 212 | { CCCOL_RED, CCFONT_REGULAR, 0 }, // 0x48 || 0x68 | ||
| 213 | { CCCOL_RED, CCFONT_UNDERLINED, 0 }, // 0x49 || 0x69 | ||
| 214 | { CCCOL_YELLOW, CCFONT_REGULAR, 0 }, // 0x4a || 0x6a | ||
| 215 | { CCCOL_YELLOW, CCFONT_UNDERLINED, 0 }, // 0x4b || 0x6b | ||
| 216 | { CCCOL_MAGENTA, CCFONT_REGULAR, 0 }, // 0x4c || 0x6c | ||
| 217 | { CCCOL_MAGENTA, CCFONT_UNDERLINED, 0 }, // 0x4d || 0x6d | ||
| 218 | { CCCOL_WHITE, CCFONT_ITALICS, 0 }, // 0x4e || 0x6e | ||
| 219 | { CCCOL_WHITE, CCFONT_UNDERLINED_ITALICS, 0 }, // 0x4f || 0x6f | ||
| 220 | { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x50 || 0x70 | ||
| 221 | { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x51 || 0x71 | ||
| 222 | { CCCOL_WHITE, CCFONT_REGULAR, 4 }, // 0x52 || 0x72 | ||
| 223 | { CCCOL_WHITE, CCFONT_UNDERLINED, 4 }, // 0x53 || 0x73 | ||
| 224 | { CCCOL_WHITE, CCFONT_REGULAR, 8 }, // 0x54 || 0x74 | ||
| 225 | { CCCOL_WHITE, CCFONT_UNDERLINED, 8 }, // 0x55 || 0x75 | ||
| 226 | { CCCOL_WHITE, CCFONT_REGULAR, 12 }, // 0x56 || 0x76 | ||
| 227 | { CCCOL_WHITE, CCFONT_UNDERLINED, 12 }, // 0x57 || 0x77 | ||
| 228 | { CCCOL_WHITE, CCFONT_REGULAR, 16 }, // 0x58 || 0x78 | ||
| 229 | { CCCOL_WHITE, CCFONT_UNDERLINED, 16 }, // 0x59 || 0x79 | ||
| 230 | { CCCOL_WHITE, CCFONT_REGULAR, 20 }, // 0x5a || 0x7a | ||
| 231 | { CCCOL_WHITE, CCFONT_UNDERLINED, 20 }, // 0x5b || 0x7b | ||
| 232 | { CCCOL_WHITE, CCFONT_REGULAR, 24 }, // 0x5c || 0x7c | ||
| 233 | { CCCOL_WHITE, CCFONT_UNDERLINED, 24 }, // 0x5d || 0x7d | ||
| 234 | { CCCOL_WHITE, CCFONT_REGULAR, 28 }, // 0x5e || 0x7e | ||
| 235 | { CCCOL_WHITE, CCFONT_UNDERLINED, 28 } // 0x5f || 0x7f | ||
| 236 | /* total 32 entries */ | ||
| 237 | }; | ||
| 238 | |||
| 239 | struct Screen { | ||
| 240 | /* +1 is used to compensate null character of string */ | ||
| 241 | uint8_t characters[SCREEN_ROWS+1][SCREEN_COLUMNS+1]; | ||
| 242 | uint8_t charsets[SCREEN_ROWS+1][SCREEN_COLUMNS+1]; | ||
| 243 | uint8_t colors[SCREEN_ROWS+1][SCREEN_COLUMNS+1]; | ||
| 244 | uint8_t bgs[SCREEN_ROWS+1][SCREEN_COLUMNS+1]; | ||
| 245 | uint8_t fonts[SCREEN_ROWS+1][SCREEN_COLUMNS+1]; | ||
| 246 | /* | ||
| 247 | * Bitmask of used rows; if a bit is not set, the | ||
| 248 | * corresponding row is not used. | ||
| 249 | * for setting row 1 use row | (1 << 0) | ||
| 250 | * for setting row 15 use row | (1 << 14) | ||
| 251 | */ | ||
| 252 | int16_t row_used; | ||
| 253 | }; | ||
| 254 | |||
| 255 | typedef struct CCaptionSubContext { | ||
| 256 | AVClass *class; | ||
| 257 | void *logctx; | ||
| 258 | int real_time; | ||
| 259 | int real_time_latency_msec; | ||
| 260 | int data_field; | ||
| 261 | struct Screen screen[2]; | ||
| 262 | int active_screen; | ||
| 263 | uint8_t cursor_row; | ||
| 264 | uint8_t cursor_column; | ||
| 265 | uint8_t cursor_color; | ||
| 266 | uint8_t bg_color; | ||
| 267 | uint8_t cursor_font; | ||
| 268 | uint8_t cursor_charset; | ||
| 269 | AVBPrint buffer[2]; | ||
| 270 | int buffer_index; | ||
| 271 | int buffer_changed; | ||
| 272 | int rollup; | ||
| 273 | enum cc_mode mode; | ||
| 274 | int64_t buffer_time[2]; | ||
| 275 | int screen_touched; | ||
| 276 | int64_t last_real_time; | ||
| 277 | uint8_t prev_cmd[2]; | ||
| 278 | int readorder; | ||
| 279 | } CCaptionSubContext; | ||
| 280 | |||
| 281 | 17 | static av_cold int init_decoder(AVCodecContext *avctx) | |
| 282 | { | ||
| 283 | 17 | CCaptionSubContext *ctx = avctx->priv_data; | |
| 284 | |||
| 285 | 17 | ctx->logctx = avctx; | |
| 286 | |||
| 287 | 17 | av_bprint_init(&ctx->buffer[0], 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 288 | 17 | av_bprint_init(&ctx->buffer[1], 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 289 | /* taking by default roll up to 2 */ | ||
| 290 | 17 | ctx->mode = CCMODE_ROLLUP; | |
| 291 | 17 | ctx->bg_color = CCCOL_BLACK; | |
| 292 | 17 | ctx->rollup = 2; | |
| 293 | 17 | ctx->cursor_row = 10; | |
| 294 | 17 | return ff_ass_subtitle_header(avctx, "Monospace", | |
| 295 | ASS_DEFAULT_FONT_SIZE, | ||
| 296 | ASS_DEFAULT_COLOR, | ||
| 297 | ASS_DEFAULT_BACK_COLOR, | ||
| 298 | ASS_DEFAULT_BOLD, | ||
| 299 | ASS_DEFAULT_ITALIC, | ||
| 300 | ASS_DEFAULT_UNDERLINE, | ||
| 301 | 3, | ||
| 302 | ASS_DEFAULT_ALIGNMENT); | ||
| 303 | } | ||
| 304 | |||
| 305 | 17 | static av_cold int close_decoder(AVCodecContext *avctx) | |
| 306 | { | ||
| 307 | 17 | CCaptionSubContext *ctx = avctx->priv_data; | |
| 308 | 17 | av_bprint_finalize(&ctx->buffer[0], NULL); | |
| 309 | 17 | av_bprint_finalize(&ctx->buffer[1], NULL); | |
| 310 | 17 | return 0; | |
| 311 | } | ||
| 312 | |||
| 313 | ✗ | static av_cold void flush_decoder(AVCodecContext *avctx) | |
| 314 | { | ||
| 315 | ✗ | CCaptionSubContext *ctx = avctx->priv_data; | |
| 316 | ✗ | ctx->screen[0].row_used = 0; | |
| 317 | ✗ | ctx->screen[1].row_used = 0; | |
| 318 | ✗ | ctx->prev_cmd[0] = 0; | |
| 319 | ✗ | ctx->prev_cmd[1] = 0; | |
| 320 | ✗ | ctx->mode = CCMODE_ROLLUP; | |
| 321 | ✗ | ctx->rollup = 2; | |
| 322 | ✗ | ctx->cursor_row = 10; | |
| 323 | ✗ | ctx->cursor_column = 0; | |
| 324 | ✗ | ctx->cursor_font = 0; | |
| 325 | ✗ | ctx->cursor_color = 0; | |
| 326 | ✗ | ctx->bg_color = CCCOL_BLACK; | |
| 327 | ✗ | ctx->cursor_charset = 0; | |
| 328 | ✗ | ctx->active_screen = 0; | |
| 329 | ✗ | ctx->last_real_time = 0; | |
| 330 | ✗ | ctx->screen_touched = 0; | |
| 331 | ✗ | ctx->buffer_changed = 0; | |
| 332 | ✗ | if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP)) | |
| 333 | ✗ | ctx->readorder = 0; | |
| 334 | ✗ | av_bprint_clear(&ctx->buffer[0]); | |
| 335 | ✗ | av_bprint_clear(&ctx->buffer[1]); | |
| 336 | ✗ | } | |
| 337 | |||
| 338 | /** | ||
| 339 | * @param ctx closed caption context just to print log | ||
| 340 | */ | ||
| 341 | 5999 | static void write_char(CCaptionSubContext *ctx, struct Screen *screen, char ch) | |
| 342 | { | ||
| 343 | 5999 | uint8_t col = ctx->cursor_column; | |
| 344 | 5999 | char *row = screen->characters[ctx->cursor_row]; | |
| 345 | 5999 | char *font = screen->fonts[ctx->cursor_row]; | |
| 346 | 5999 | char *color = screen->colors[ctx->cursor_row]; | |
| 347 | 5999 | char *bg = screen->bgs[ctx->cursor_row]; | |
| 348 | 5999 | char *charset = screen->charsets[ctx->cursor_row]; | |
| 349 | |||
| 350 |
2/2✓ Branch 0 taken 5990 times.
✓ Branch 1 taken 9 times.
|
5999 | if (col < SCREEN_COLUMNS) { |
| 351 | 5990 | row[col] = ch; | |
| 352 | 5990 | font[col] = ctx->cursor_font; | |
| 353 | 5990 | color[col] = ctx->cursor_color; | |
| 354 | 5990 | bg[col] = ctx->bg_color; | |
| 355 | 5990 | charset[col] = ctx->cursor_charset; | |
| 356 | 5990 | ctx->cursor_charset = CCSET_BASIC_AMERICAN; | |
| 357 |
2/2✓ Branch 0 taken 4343 times.
✓ Branch 1 taken 1647 times.
|
5990 | if (ch) ctx->cursor_column++; |
| 358 | 5990 | return; | |
| 359 | } | ||
| 360 | /* We have extra space at end only for null character */ | ||
| 361 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 | else if (col == SCREEN_COLUMNS && ch == 0) { |
| 362 | 9 | row[col] = ch; | |
| 363 | 9 | return; | |
| 364 | } | ||
| 365 | else { | ||
| 366 | ✗ | av_log(ctx->logctx, AV_LOG_WARNING, "Data ignored due to columns exceeding screen width\n"); | |
| 367 | ✗ | return; | |
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | /** | ||
| 372 | * This function after validating parity bit, also remove it from data pair. | ||
| 373 | * The first byte doesn't pass parity, we replace it with a solid blank | ||
| 374 | * and process the pair. | ||
| 375 | * If the second byte doesn't pass parity, it returns INVALIDDATA | ||
| 376 | * user can ignore the whole pair and pass the other pair. | ||
| 377 | */ | ||
| 378 | 13817 | static int validate_cc_data_pair(const uint8_t *cc_data_pair, uint8_t *hi) | |
| 379 | { | ||
| 380 | 13817 | uint8_t cc_valid = (*cc_data_pair & 4) >>2; | |
| 381 | 13817 | uint8_t cc_type = *cc_data_pair & 3; | |
| 382 | |||
| 383 | 13817 | *hi = cc_data_pair[1]; | |
| 384 | |||
| 385 |
2/2✓ Branch 0 taken 9308 times.
✓ Branch 1 taken 4509 times.
|
13817 | if (!cc_valid) |
| 386 | 9308 | return AVERROR_INVALIDDATA; | |
| 387 | |||
| 388 | // if EIA-608 data then verify parity. | ||
| 389 |
4/4✓ Branch 0 taken 1308 times.
✓ Branch 1 taken 3201 times.
✓ Branch 2 taken 752 times.
✓ Branch 3 taken 556 times.
|
4509 | if (cc_type==0 || cc_type==1) { |
| 390 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3953 times.
|
3953 | if (!av_parity(cc_data_pair[2])) { |
| 391 | ✗ | return AVERROR_INVALIDDATA; | |
| 392 | } | ||
| 393 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3953 times.
|
3953 | if (!av_parity(cc_data_pair[1])) { |
| 394 | ✗ | *hi = 0x7F; | |
| 395 | } | ||
| 396 | } | ||
| 397 | |||
| 398 | //Skip non-data | ||
| 399 |
5/6✓ Branch 0 taken 4509 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1512 times.
✓ Branch 3 taken 2997 times.
✓ Branch 4 taken 548 times.
✓ Branch 5 taken 964 times.
|
4509 | if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD) |
| 400 |
3/4✓ Branch 0 taken 884 times.
✓ Branch 1 taken 2661 times.
✓ Branch 2 taken 884 times.
✗ Branch 3 not taken.
|
3545 | && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0) |
| 401 | 884 | return AVERROR_PATCHWELCOME; | |
| 402 | |||
| 403 | //skip 708 data | ||
| 404 |
4/4✓ Branch 0 taken 3469 times.
✓ Branch 1 taken 156 times.
✓ Branch 2 taken 400 times.
✓ Branch 3 taken 3069 times.
|
3625 | if (cc_type == 3 || cc_type == 2) |
| 405 | 556 | return AVERROR_PATCHWELCOME; | |
| 406 | |||
| 407 | 3069 | return 0; | |
| 408 | } | ||
| 409 | |||
| 410 | 1889 | static struct Screen *get_writing_screen(CCaptionSubContext *ctx) | |
| 411 | { | ||
| 412 |
2/3✓ Branch 0 taken 1676 times.
✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
|
1889 | switch (ctx->mode) { |
| 413 | 1676 | case CCMODE_POPON: | |
| 414 | // use Inactive screen | ||
| 415 |
2/2✓ Branch 0 taken 864 times.
✓ Branch 1 taken 812 times.
|
1676 | return ctx->screen + !ctx->active_screen; |
| 416 | 213 | case CCMODE_PAINTON: | |
| 417 | case CCMODE_ROLLUP: | ||
| 418 | case CCMODE_TEXT: | ||
| 419 | // use active screen | ||
| 420 | 213 | return ctx->screen + ctx->active_screen; | |
| 421 | } | ||
| 422 | /* It was never an option */ | ||
| 423 | ✗ | return NULL; | |
| 424 | } | ||
| 425 | |||
| 426 | 15 | static void roll_up(CCaptionSubContext *ctx) | |
| 427 | { | ||
| 428 | struct Screen *screen; | ||
| 429 | int i, keep_lines; | ||
| 430 | |||
| 431 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (ctx->mode == CCMODE_TEXT) |
| 432 | ✗ | return; | |
| 433 | |||
| 434 | 15 | screen = get_writing_screen(ctx); | |
| 435 | |||
| 436 | /* +1 signify cursor_row starts from 0 | ||
| 437 | * Can't keep lines less then row cursor pos | ||
| 438 | */ | ||
| 439 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8 times.
|
15 | keep_lines = FFMIN(ctx->cursor_row + 1, ctx->rollup); |
| 440 | |||
| 441 |
2/2✓ Branch 0 taken 225 times.
✓ Branch 1 taken 15 times.
|
240 | for (i = 0; i < SCREEN_ROWS; i++) { |
| 442 |
4/4✓ Branch 0 taken 160 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 130 times.
|
225 | if (i > ctx->cursor_row - keep_lines && i <= ctx->cursor_row) |
| 443 | 30 | continue; | |
| 444 | 195 | UNSET_FLAG(screen->row_used, i); | |
| 445 | } | ||
| 446 | |||
| 447 |
4/4✓ Branch 0 taken 26 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 4 times.
|
37 | for (i = 0; i < keep_lines && screen->row_used; i++) { |
| 448 | 22 | const int i_row = ctx->cursor_row - keep_lines + i + 1; | |
| 449 | |||
| 450 | 22 | memcpy(screen->characters[i_row], screen->characters[i_row+1], SCREEN_COLUMNS); | |
| 451 | 22 | memcpy(screen->colors[i_row], screen->colors[i_row+1], SCREEN_COLUMNS); | |
| 452 | 22 | memcpy(screen->bgs[i_row], screen->bgs[i_row+1], SCREEN_COLUMNS); | |
| 453 | 22 | memcpy(screen->fonts[i_row], screen->fonts[i_row+1], SCREEN_COLUMNS); | |
| 454 | 22 | memcpy(screen->charsets[i_row], screen->charsets[i_row+1], SCREEN_COLUMNS); | |
| 455 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
|
22 | if (CHECK_FLAG(screen->row_used, i_row + 1)) |
| 456 | 11 | SET_FLAG(screen->row_used, i_row); | |
| 457 | } | ||
| 458 | |||
| 459 | 15 | UNSET_FLAG(screen->row_used, ctx->cursor_row); | |
| 460 | } | ||
| 461 | |||
| 462 | 227 | static int capture_screen(CCaptionSubContext *ctx) | |
| 463 | { | ||
| 464 | 227 | int i, j, tab = 0; | |
| 465 | 227 | struct Screen *screen = ctx->screen + ctx->active_screen; | |
| 466 | 227 | enum cc_font prev_font = CCFONT_REGULAR; | |
| 467 | 227 | enum cc_color_code prev_color = CCCOL_WHITE; | |
| 468 | 227 | enum cc_color_code prev_bg_color = CCCOL_BLACK; | |
| 469 | 227 | const int bidx = ctx->buffer_index; | |
| 470 | |||
| 471 | 227 | av_bprint_clear(&ctx->buffer[bidx]); | |
| 472 | |||
| 473 |
4/4✓ Branch 0 taken 1936 times.
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 1815 times.
✓ Branch 3 taken 121 times.
|
2042 | for (i = 0; screen->row_used && i < SCREEN_ROWS; i++) |
| 474 | { | ||
| 475 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 1583 times.
|
1815 | if (CHECK_FLAG(screen->row_used, i)) { |
| 476 | 232 | const char *row = screen->characters[i]; | |
| 477 | 232 | const char *charset = screen->charsets[i]; | |
| 478 | 232 | j = 0; | |
| 479 |
3/4✓ Branch 0 taken 1164 times.
✓ Branch 1 taken 232 times.
✓ Branch 2 taken 1164 times.
✗ Branch 3 not taken.
|
1396 | while (row[j] == ' ' && charset[j] == CCSET_BASIC_AMERICAN) |
| 480 | 1164 | j++; | |
| 481 |
4/4✓ Branch 0 taken 58 times.
✓ Branch 1 taken 174 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 52 times.
|
232 | if (!tab || j < tab) |
| 482 | 180 | tab = j; | |
| 483 | } | ||
| 484 | } | ||
| 485 | |||
| 486 |
4/4✓ Branch 0 taken 1936 times.
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 1815 times.
✓ Branch 3 taken 121 times.
|
2042 | for (i = 0; screen->row_used && i < SCREEN_ROWS; i++) |
| 487 | { | ||
| 488 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 1583 times.
|
1815 | if (CHECK_FLAG(screen->row_used, i)) { |
| 489 | 232 | const char *row = screen->characters[i]; | |
| 490 | 232 | const char *font = screen->fonts[i]; | |
| 491 | 232 | const char *bg = screen->bgs[i]; | |
| 492 | 232 | const char *color = screen->colors[i]; | |
| 493 | 232 | const char *charset = screen->charsets[i]; | |
| 494 | const char *override; | ||
| 495 | 232 | int x, y, seen_char = 0; | |
| 496 | 232 | j = 0; | |
| 497 | |||
| 498 | /* skip leading space */ | ||
| 499 |
5/6✓ Branch 0 taken 1003 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 1003 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 971 times.
✓ Branch 5 taken 32 times.
|
1203 | while (row[j] == ' ' && charset[j] == CCSET_BASIC_AMERICAN && j < tab) |
| 500 | 971 | j++; | |
| 501 | |||
| 502 | 232 | x = ASS_DEFAULT_PLAYRESX * (0.1 + 0.0250 * j); | |
| 503 | 232 | y = ASS_DEFAULT_PLAYRESY * (0.1 + 0.0533 * i); | |
| 504 | 232 | av_bprintf(&ctx->buffer[bidx], "{\\an7}{\\pos(%d,%d)}", x, y); | |
| 505 | |||
| 506 |
2/2✓ Branch 0 taken 4186 times.
✓ Branch 1 taken 9 times.
|
4195 | for (; j < SCREEN_COLUMNS; j++) { |
| 507 | 4186 | const char *e_tag = "", *s_tag = "", *c_tag = "", *b_tag = ""; | |
| 508 | |||
| 509 |
2/2✓ Branch 0 taken 223 times.
✓ Branch 1 taken 3963 times.
|
4186 | if (row[j] == 0) |
| 510 | 223 | break; | |
| 511 | |||
| 512 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 3899 times.
|
3963 | if (prev_font != font[j]) { |
| 513 |
2/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
|
64 | switch (prev_font) { |
| 514 | 20 | case CCFONT_ITALICS: | |
| 515 | 20 | e_tag = "{\\i0}"; | |
| 516 | 20 | break; | |
| 517 | ✗ | case CCFONT_UNDERLINED: | |
| 518 | ✗ | e_tag = "{\\u0}"; | |
| 519 | ✗ | break; | |
| 520 | ✗ | case CCFONT_UNDERLINED_ITALICS: | |
| 521 | ✗ | e_tag = "{\\u0}{\\i0}"; | |
| 522 | ✗ | break; | |
| 523 | } | ||
| 524 |
2/4✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
64 | switch (font[j]) { |
| 525 | 44 | case CCFONT_ITALICS: | |
| 526 | 44 | s_tag = "{\\i1}"; | |
| 527 | 44 | break; | |
| 528 | ✗ | case CCFONT_UNDERLINED: | |
| 529 | ✗ | s_tag = "{\\u1}"; | |
| 530 | ✗ | break; | |
| 531 | ✗ | case CCFONT_UNDERLINED_ITALICS: | |
| 532 | ✗ | s_tag = "{\\u1}{\\i1}"; | |
| 533 | ✗ | break; | |
| 534 | } | ||
| 535 | } | ||
| 536 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3963 times.
|
3963 | if (prev_color != color[j]) { |
| 537 | ✗ | switch (color[j]) { | |
| 538 | ✗ | case CCCOL_WHITE: | |
| 539 | ✗ | c_tag = "{\\c&HFFFFFF&}"; | |
| 540 | ✗ | break; | |
| 541 | ✗ | case CCCOL_GREEN: | |
| 542 | ✗ | c_tag = "{\\c&H00FF00&}"; | |
| 543 | ✗ | break; | |
| 544 | ✗ | case CCCOL_BLUE: | |
| 545 | ✗ | c_tag = "{\\c&HFF0000&}"; | |
| 546 | ✗ | break; | |
| 547 | ✗ | case CCCOL_CYAN: | |
| 548 | ✗ | c_tag = "{\\c&HFFFF00&}"; | |
| 549 | ✗ | break; | |
| 550 | ✗ | case CCCOL_RED: | |
| 551 | ✗ | c_tag = "{\\c&H0000FF&}"; | |
| 552 | ✗ | break; | |
| 553 | ✗ | case CCCOL_YELLOW: | |
| 554 | ✗ | c_tag = "{\\c&H00FFFF&}"; | |
| 555 | ✗ | break; | |
| 556 | ✗ | case CCCOL_MAGENTA: | |
| 557 | ✗ | c_tag = "{\\c&HFF00FF&}"; | |
| 558 | ✗ | break; | |
| 559 | } | ||
| 560 | } | ||
| 561 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3963 times.
|
3963 | if (prev_bg_color != bg[j]) { |
| 562 | ✗ | switch (bg[j]) { | |
| 563 | ✗ | case CCCOL_WHITE: | |
| 564 | ✗ | b_tag = "{\\3c&HFFFFFF&}"; | |
| 565 | ✗ | break; | |
| 566 | ✗ | case CCCOL_GREEN: | |
| 567 | ✗ | b_tag = "{\\3c&H00FF00&}"; | |
| 568 | ✗ | break; | |
| 569 | ✗ | case CCCOL_BLUE: | |
| 570 | ✗ | b_tag = "{\\3c&HFF0000&}"; | |
| 571 | ✗ | break; | |
| 572 | ✗ | case CCCOL_CYAN: | |
| 573 | ✗ | b_tag = "{\\3c&HFFFF00&}"; | |
| 574 | ✗ | break; | |
| 575 | ✗ | case CCCOL_RED: | |
| 576 | ✗ | b_tag = "{\\3c&H0000FF&}"; | |
| 577 | ✗ | break; | |
| 578 | ✗ | case CCCOL_YELLOW: | |
| 579 | ✗ | b_tag = "{\\3c&H00FFFF&}"; | |
| 580 | ✗ | break; | |
| 581 | ✗ | case CCCOL_MAGENTA: | |
| 582 | ✗ | b_tag = "{\\3c&HFF00FF&}"; | |
| 583 | ✗ | break; | |
| 584 | ✗ | case CCCOL_BLACK: | |
| 585 | ✗ | b_tag = "{\\3c&H000000&}"; | |
| 586 | ✗ | break; | |
| 587 | } | ||
| 588 | } | ||
| 589 | |||
| 590 | 3963 | prev_font = font[j]; | |
| 591 | 3963 | prev_color = color[j]; | |
| 592 | 3963 | prev_bg_color = bg[j]; | |
| 593 | 3963 | override = charset_overrides[(int)charset[j]][(int)row[j]]; | |
| 594 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3952 times.
|
3963 | if (override[0]) { |
| 595 | 11 | av_bprintf(&ctx->buffer[bidx], "%s%s%s%s%s", e_tag, s_tag, c_tag, b_tag, override); | |
| 596 | 11 | seen_char = 1; | |
| 597 |
4/4✓ Branch 0 taken 823 times.
✓ Branch 1 taken 3129 times.
✓ Branch 2 taken 193 times.
✓ Branch 3 taken 630 times.
|
3952 | } else if (row[j] == ' ' && !seen_char) { |
| 598 | 193 | av_bprintf(&ctx->buffer[bidx], "%s%s%s%s\\h", e_tag, s_tag, c_tag, b_tag); | |
| 599 | } else { | ||
| 600 | 3759 | av_bprintf(&ctx->buffer[bidx], "%s%s%s%s%c", e_tag, s_tag, c_tag, b_tag, row[j]); | |
| 601 | 3759 | seen_char = 1; | |
| 602 | } | ||
| 603 | |||
| 604 | } | ||
| 605 | 232 | av_bprintf(&ctx->buffer[bidx], "\\N"); | |
| 606 | } | ||
| 607 | } | ||
| 608 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
|
227 | if (!av_bprint_is_complete(&ctx->buffer[bidx])) |
| 609 | ✗ | return AVERROR(ENOMEM); | |
| 610 |
3/4✓ Branch 0 taken 121 times.
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 121 times.
✗ Branch 3 not taken.
|
227 | if (screen->row_used && ctx->buffer[bidx].len >= 2) { |
| 611 | 121 | ctx->buffer[bidx].len -= 2; | |
| 612 | 121 | ctx->buffer[bidx].str[ctx->buffer[bidx].len] = 0; | |
| 613 | } | ||
| 614 | 227 | ctx->buffer_changed = 1; | |
| 615 | 227 | return 0; | |
| 616 | } | ||
| 617 | |||
| 618 | 203 | static void update_time(CCaptionSubContext *ctx, int64_t pts) | |
| 619 | { | ||
| 620 | 203 | ctx->buffer_time[0] = ctx->buffer_time[1]; | |
| 621 | 203 | ctx->buffer_time[1] = pts; | |
| 622 | 203 | } | |
| 623 | |||
| 624 | ✗ | static void handle_bgattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo) | |
| 625 | { | ||
| 626 | ✗ | const int i = (lo & 0xf) >> 1; | |
| 627 | |||
| 628 | ✗ | ctx->bg_color = bg_attribs[i]; | |
| 629 | ✗ | } | |
| 630 | |||
| 631 | 15 | static void handle_textattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo) | |
| 632 | { | ||
| 633 | 15 | int i = lo - 0x20; | |
| 634 | 15 | struct Screen *screen = get_writing_screen(ctx); | |
| 635 | |||
| 636 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (i >= 32) |
| 637 | ✗ | return; | |
| 638 | |||
| 639 | 15 | ctx->cursor_color = pac2_attribs[i][0]; | |
| 640 | 15 | ctx->cursor_font = pac2_attribs[i][1]; | |
| 641 | |||
| 642 | 15 | SET_FLAG(screen->row_used, ctx->cursor_row); | |
| 643 | 15 | write_char(ctx, screen, ' '); | |
| 644 | } | ||
| 645 | |||
| 646 | 203 | static void handle_pac(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo) | |
| 647 | { | ||
| 648 | static const int8_t row_map[] = { | ||
| 649 | 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10 | ||
| 650 | }; | ||
| 651 | 203 | const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 ); | |
| 652 | 203 | struct Screen *screen = get_writing_screen(ctx); | |
| 653 | int indent, i; | ||
| 654 | |||
| 655 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
|
203 | if (row_map[index] <= 0) { |
| 656 | ✗ | av_log(ctx->logctx, AV_LOG_DEBUG, "Invalid pac index encountered\n"); | |
| 657 | ✗ | return; | |
| 658 | } | ||
| 659 | |||
| 660 | 203 | lo &= 0x1f; | |
| 661 | |||
| 662 | 203 | ctx->cursor_row = row_map[index] - 1; | |
| 663 | 203 | ctx->cursor_color = pac2_attribs[lo][0]; | |
| 664 | 203 | ctx->cursor_font = pac2_attribs[lo][1]; | |
| 665 | 203 | ctx->cursor_charset = CCSET_BASIC_AMERICAN; | |
| 666 | 203 | ctx->cursor_column = 0; | |
| 667 | 203 | indent = pac2_attribs[lo][2]; | |
| 668 |
2/2✓ Branch 0 taken 1136 times.
✓ Branch 1 taken 203 times.
|
1339 | for (i = 0; i < indent; i++) { |
| 669 | 1136 | write_char(ctx, screen, ' '); | |
| 670 | } | ||
| 671 | } | ||
| 672 | |||
| 673 | 197 | static int handle_edm(CCaptionSubContext *ctx) | |
| 674 | { | ||
| 675 | 197 | struct Screen *screen = ctx->screen + ctx->active_screen; | |
| 676 | int ret; | ||
| 677 | |||
| 678 | // In buffered mode, keep writing to screen until it is wiped. | ||
| 679 | // Before wiping the display, capture contents to emit subtitle. | ||
| 680 |
1/2✓ Branch 0 taken 197 times.
✗ Branch 1 not taken.
|
197 | if (!ctx->real_time) |
| 681 | 197 | ret = capture_screen(ctx); | |
| 682 | |||
| 683 | 197 | screen->row_used = 0; | |
| 684 | 197 | ctx->bg_color = CCCOL_BLACK; | |
| 685 | |||
| 686 | // In realtime mode, emit an empty caption so the last one doesn't | ||
| 687 | // stay on the screen. | ||
| 688 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
|
197 | if (ctx->real_time) |
| 689 | ✗ | ret = capture_screen(ctx); | |
| 690 | |||
| 691 | 197 | return ret; | |
| 692 | } | ||
| 693 | |||
| 694 | 93 | static int handle_eoc(CCaptionSubContext *ctx) | |
| 695 | { | ||
| 696 | int ret; | ||
| 697 | |||
| 698 | 93 | ctx->active_screen = !ctx->active_screen; | |
| 699 | |||
| 700 | // In buffered mode, we wait til the *next* EOC and | ||
| 701 | // capture what was already on the screen since the last EOC. | ||
| 702 |
1/2✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
|
93 | if (!ctx->real_time) |
| 703 | 93 | ret = handle_edm(ctx); | |
| 704 | |||
| 705 | 93 | ctx->cursor_column = 0; | |
| 706 | |||
| 707 | // In realtime mode, we display the buffered contents (after | ||
| 708 | // flipping the buffer to active above) as soon as EOC arrives. | ||
| 709 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (ctx->real_time) |
| 710 | ✗ | ret = capture_screen(ctx); | |
| 711 | |||
| 712 | 93 | return ret; | |
| 713 | } | ||
| 714 | |||
| 715 | ✗ | static void handle_delete_end_of_row(CCaptionSubContext *ctx) | |
| 716 | { | ||
| 717 | ✗ | struct Screen *screen = get_writing_screen(ctx); | |
| 718 | ✗ | write_char(ctx, screen, 0); | |
| 719 | ✗ | } | |
| 720 | |||
| 721 | 1656 | static void handle_char(CCaptionSubContext *ctx, char hi, char lo) | |
| 722 | { | ||
| 723 | 1656 | struct Screen *screen = get_writing_screen(ctx); | |
| 724 | |||
| 725 | 1656 | SET_FLAG(screen->row_used, ctx->cursor_row); | |
| 726 | |||
| 727 |
1/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1656 times.
|
1656 | switch (hi) { |
| 728 | ✗ | case 0x11: | |
| 729 | ✗ | ctx->cursor_charset = CCSET_SPECIAL_AMERICAN; | |
| 730 | ✗ | break; | |
| 731 | ✗ | case 0x12: | |
| 732 | ✗ | if (ctx->cursor_column > 0) | |
| 733 | ✗ | ctx->cursor_column -= 1; | |
| 734 | ✗ | ctx->cursor_charset = CCSET_EXTENDED_SPANISH_FRENCH_MISC; | |
| 735 | ✗ | break; | |
| 736 | ✗ | case 0x13: | |
| 737 | ✗ | if (ctx->cursor_column > 0) | |
| 738 | ✗ | ctx->cursor_column -= 1; | |
| 739 | ✗ | ctx->cursor_charset = CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH; | |
| 740 | ✗ | break; | |
| 741 | 1656 | default: | |
| 742 | 1656 | ctx->cursor_charset = CCSET_BASIC_AMERICAN; | |
| 743 | 1656 | write_char(ctx, screen, hi); | |
| 744 | 1656 | break; | |
| 745 | } | ||
| 746 | |||
| 747 |
2/2✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 120 times.
|
1656 | if (lo) { |
| 748 | 1536 | write_char(ctx, screen, lo); | |
| 749 | } | ||
| 750 | 1656 | write_char(ctx, screen, 0); | |
| 751 | |||
| 752 |
2/2✓ Branch 0 taken 175 times.
✓ Branch 1 taken 1481 times.
|
1656 | if (ctx->mode != CCMODE_POPON) |
| 753 | 175 | ctx->screen_touched = 1; | |
| 754 | |||
| 755 | if (lo) | ||
| 756 | ff_dlog(ctx->logctx, "(%c,%c)\n", hi, lo); | ||
| 757 | else | ||
| 758 | ff_dlog(ctx->logctx, "(%c)\n", hi); | ||
| 759 | 1656 | } | |
| 760 | |||
| 761 | 2865 | static int process_cc608(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo) | |
| 762 | { | ||
| 763 | 2865 | int ret = 0; | |
| 764 | |||
| 765 |
4/4✓ Branch 0 taken 944 times.
✓ Branch 1 taken 1921 times.
✓ Branch 2 taken 672 times.
✓ Branch 3 taken 272 times.
|
2865 | if (hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) { |
| 766 | 672 | return 0; | |
| 767 | } | ||
| 768 | |||
| 769 | /* set prev command */ | ||
| 770 | 2193 | ctx->prev_cmd[0] = hi; | |
| 771 | 2193 | ctx->prev_cmd[1] = lo; | |
| 772 | |||
| 773 |
2/8✗ Branch 0 not taken.
✓ Branch 1 taken 2193 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2193 times.
✗ Branch 7 not taken.
|
2193 | if ( (hi == 0x10 && (lo >= 0x40 && lo <= 0x5f)) || |
| 774 |
5/6✓ Branch 0 taken 537 times.
✓ Branch 1 taken 1656 times.
✓ Branch 2 taken 203 times.
✓ Branch 3 taken 334 times.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
2193 | ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) { |
| 775 | 203 | handle_pac(ctx, hi, lo); | |
| 776 |
5/8✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1975 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1975 times.
|
1990 | } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) || |
| 777 | ✗ | ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) { | |
| 778 | 15 | handle_textattr(ctx, hi, lo); | |
| 779 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1975 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
1975 | } else if ((hi == 0x10 && lo >= 0x20 && lo <= 0x2f)) { |
| 780 | ✗ | handle_bgattr(ctx, hi, lo); | |
| 781 |
4/6✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 319 times.
✓ Branch 2 taken 1656 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1656 times.
|
1975 | } else if (hi == 0x14 || hi == 0x15 || hi == 0x1c) { |
| 782 |
5/10✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 104 times.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 93 times.
✗ Branch 9 not taken.
|
319 | switch (lo) { |
| 783 | 92 | case 0x20: | |
| 784 | /* resume caption loading */ | ||
| 785 | 92 | ctx->mode = CCMODE_POPON; | |
| 786 | 92 | break; | |
| 787 | ✗ | case 0x24: | |
| 788 | ✗ | handle_delete_end_of_row(ctx); | |
| 789 | ✗ | break; | |
| 790 | 15 | case 0x25: | |
| 791 | case 0x26: | ||
| 792 | case 0x27: | ||
| 793 | 15 | ctx->rollup = lo - 0x23; | |
| 794 | 15 | ctx->mode = CCMODE_ROLLUP; | |
| 795 | 15 | break; | |
| 796 | ✗ | case 0x29: | |
| 797 | /* resume direct captioning */ | ||
| 798 | ✗ | ctx->mode = CCMODE_PAINTON; | |
| 799 | ✗ | break; | |
| 800 | ✗ | case 0x2b: | |
| 801 | /* resume text display */ | ||
| 802 | ✗ | ctx->mode = CCMODE_TEXT; | |
| 803 | ✗ | break; | |
| 804 | 104 | case 0x2c: | |
| 805 | /* erase display memory */ | ||
| 806 | 104 | handle_edm(ctx); | |
| 807 | 104 | break; | |
| 808 | 15 | case 0x2d: | |
| 809 | /* carriage return */ | ||
| 810 | ff_dlog(ctx->logctx, "carriage return\n"); | ||
| 811 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
|
15 | if (!ctx->real_time) |
| 812 | 6 | ret = capture_screen(ctx); | |
| 813 | 15 | roll_up(ctx); | |
| 814 | 15 | ctx->cursor_column = 0; | |
| 815 | 15 | break; | |
| 816 | ✗ | case 0x2e: | |
| 817 | /* erase buffered (non displayed) memory */ | ||
| 818 | // Only in realtime mode. In buffered mode, we reuse the inactive screen | ||
| 819 | // for our own buffering. | ||
| 820 | ✗ | if (ctx->real_time) { | |
| 821 | ✗ | struct Screen *screen = ctx->screen + !ctx->active_screen; | |
| 822 | ✗ | screen->row_used = 0; | |
| 823 | } | ||
| 824 | ✗ | break; | |
| 825 | 93 | case 0x2f: | |
| 826 | /* end of caption */ | ||
| 827 | ff_dlog(ctx->logctx, "handle_eoc\n"); | ||
| 828 | 93 | ret = handle_eoc(ctx); | |
| 829 | 93 | break; | |
| 830 | ✗ | default: | |
| 831 | ff_dlog(ctx->logctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo); | ||
| 832 | ✗ | break; | |
| 833 | } | ||
| 834 |
2/4✓ Branch 0 taken 1656 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1656 times.
|
1656 | } else if (hi >= 0x11 && hi <= 0x13) { |
| 835 | /* Special characters */ | ||
| 836 | ✗ | handle_char(ctx, hi, lo); | |
| 837 |
1/2✓ Branch 0 taken 1656 times.
✗ Branch 1 not taken.
|
1656 | } else if (hi >= 0x20) { |
| 838 | /* Standard characters (always in pairs) */ | ||
| 839 | 1656 | handle_char(ctx, hi, lo); | |
| 840 | 1656 | ctx->prev_cmd[0] = ctx->prev_cmd[1] = 0; | |
| 841 | ✗ | } else if (hi == 0x17 && lo >= 0x21 && lo <= 0x23) { | |
| 842 | int i; | ||
| 843 | /* Tab offsets (spacing) */ | ||
| 844 | ✗ | for (i = 0; i < lo - 0x20; i++) { | |
| 845 | ✗ | handle_char(ctx, ' ', 0); | |
| 846 | } | ||
| 847 | } else { | ||
| 848 | /* Ignoring all other non data code */ | ||
| 849 | ff_dlog(ctx->logctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo); | ||
| 850 | } | ||
| 851 | |||
| 852 | 2193 | return ret; | |
| 853 | } | ||
| 854 | |||
| 855 | 852 | static int decode(AVCodecContext *avctx, AVSubtitle *sub, | |
| 856 | int *got_sub, const AVPacket *avpkt) | ||
| 857 | { | ||
| 858 | 852 | CCaptionSubContext *ctx = avctx->priv_data; | |
| 859 | 852 | int64_t in_time = sub->pts; | |
| 860 | int64_t start_time; | ||
| 861 | int64_t end_time; | ||
| 862 | 852 | int bidx = ctx->buffer_index; | |
| 863 | 852 | const uint8_t *bptr = avpkt->data; | |
| 864 | 852 | int len = avpkt->size; | |
| 865 | 852 | int ret = 0; | |
| 866 | int i; | ||
| 867 | 852 | unsigned nb_rect_allocated = 0; | |
| 868 | |||
| 869 |
2/2✓ Branch 0 taken 13817 times.
✓ Branch 1 taken 852 times.
|
14669 | for (i = 0; i < len; i += 3) { |
| 870 | 13817 | uint8_t hi, cc_type = bptr[i] & 1; | |
| 871 | |||
| 872 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 13811 times.
|
13817 | if (ctx->data_field < 0) |
| 873 | 6 | ctx->data_field = cc_type; | |
| 874 | |||
| 875 |
2/2✓ Branch 1 taken 10748 times.
✓ Branch 2 taken 3069 times.
|
13817 | if (validate_cc_data_pair(bptr + i, &hi)) |
| 876 | 13614 | continue; | |
| 877 | |||
| 878 |
2/2✓ Branch 0 taken 204 times.
✓ Branch 1 taken 2865 times.
|
3069 | if (cc_type != ctx->data_field) |
| 879 | 204 | continue; | |
| 880 | |||
| 881 | 2865 | ret = process_cc608(ctx, hi & 0x7f, bptr[i + 2] & 0x7f); | |
| 882 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2865 times.
|
2865 | if (ret < 0) |
| 883 | ✗ | return ret; | |
| 884 | |||
| 885 |
2/2✓ Branch 0 taken 2662 times.
✓ Branch 1 taken 203 times.
|
2865 | if (!ctx->buffer_changed) |
| 886 | 2662 | continue; | |
| 887 | 203 | ctx->buffer_changed = 0; | |
| 888 | |||
| 889 |
3/4✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 197 times.
✓ Branch 3 taken 6 times.
|
203 | if (!ctx->real_time && ctx->mode == CCMODE_POPON) |
| 890 | 197 | ctx->buffer_index = bidx = !ctx->buffer_index; | |
| 891 | |||
| 892 | 203 | update_time(ctx, in_time); | |
| 893 | |||
| 894 |
3/4✓ Branch 0 taken 106 times.
✓ Branch 1 taken 97 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 106 times.
|
203 | if (ctx->buffer[bidx].str[0] || ctx->real_time) { |
| 895 | ff_dlog(avctx, "cdp writing data (%s)\n", ctx->buffer[bidx].str); | ||
| 896 | 97 | start_time = ctx->buffer_time[0]; | |
| 897 | 97 | sub->pts = start_time; | |
| 898 | 97 | end_time = ctx->buffer_time[1]; | |
| 899 |
1/2✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
|
97 | if (!ctx->real_time) |
| 900 | 97 | sub->end_display_time = av_rescale_q(end_time - start_time, | |
| 901 | 97 | AV_TIME_BASE_Q, ms_tb); | |
| 902 | else | ||
| 903 | ✗ | sub->end_display_time = -1; | |
| 904 | 97 | ret = ff_ass_add_rect2(sub, ctx->buffer[bidx].str, ctx->readorder++, 0, NULL, NULL, &nb_rect_allocated); | |
| 905 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 97 times.
|
97 | if (ret < 0) |
| 906 | ✗ | return ret; | |
| 907 | 97 | ctx->last_real_time = sub->pts; | |
| 908 | 97 | ctx->screen_touched = 0; | |
| 909 | } | ||
| 910 | } | ||
| 911 | |||
| 912 |
5/6✓ Branch 0 taken 6 times.
✓ Branch 1 taken 846 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
852 | if (!bptr && !ctx->real_time && ctx->buffer[!ctx->buffer_index].str[0]) { |
| 913 | ✗ | bidx = !ctx->buffer_index; | |
| 914 | ✗ | ret = ff_ass_add_rect2(sub, ctx->buffer[bidx].str, ctx->readorder++, 0, NULL, NULL, &nb_rect_allocated); | |
| 915 | ✗ | if (ret < 0) | |
| 916 | ✗ | return ret; | |
| 917 | ✗ | av_bprint_clear(&ctx->buffer[bidx]); | |
| 918 | ✗ | sub->pts = ctx->buffer_time[1]; | |
| 919 | ✗ | sub->end_display_time = av_rescale_q(ctx->buffer_time[1] - ctx->buffer_time[0], | |
| 920 | ✗ | AV_TIME_BASE_Q, ms_tb); | |
| 921 | ✗ | if (sub->end_display_time == 0) | |
| 922 | ✗ | sub->end_display_time = ctx->buffer[bidx].len * 20; | |
| 923 | } | ||
| 924 | |||
| 925 |
4/4✓ Branch 0 taken 408 times.
✓ Branch 1 taken 444 times.
✓ Branch 2 taken 114 times.
✓ Branch 3 taken 294 times.
|
852 | if (ctx->real_time && ctx->screen_touched && |
| 926 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 90 times.
|
114 | sub->pts >= ctx->last_real_time + av_rescale_q(ctx->real_time_latency_msec, ms_tb, AV_TIME_BASE_Q)) { |
| 927 | 24 | ctx->last_real_time = sub->pts; | |
| 928 | 24 | ctx->screen_touched = 0; | |
| 929 | |||
| 930 | 24 | capture_screen(ctx); | |
| 931 | 24 | ctx->buffer_changed = 0; | |
| 932 | |||
| 933 | 24 | ret = ff_ass_add_rect2(sub, ctx->buffer[bidx].str, ctx->readorder++, 0, NULL, NULL, &nb_rect_allocated); | |
| 934 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (ret < 0) |
| 935 | ✗ | return ret; | |
| 936 | 24 | sub->end_display_time = -1; | |
| 937 | } | ||
| 938 | |||
| 939 | 852 | *got_sub = sub->num_rects > 0; | |
| 940 | 852 | return avpkt->size; | |
| 941 | } | ||
| 942 | |||
| 943 | #define OFFSET(x) offsetof(CCaptionSubContext, x) | ||
| 944 | #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM | ||
| 945 | static const AVOption options[] = { | ||
| 946 | { "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 }, | ||
| 947 | { "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 }, | ||
| 948 | { "data_field", "select data field", OFFSET(data_field), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, SD, .unit = "data_field" }, | ||
| 949 | { "auto", "pick first one that appears", 0, AV_OPT_TYPE_CONST, { .i64 =-1 }, 0, 0, SD, .unit = "data_field" }, | ||
| 950 | { "first", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, SD, .unit = "data_field" }, | ||
| 951 | { "second", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, SD, .unit = "data_field" }, | ||
| 952 | {NULL} | ||
| 953 | }; | ||
| 954 | |||
| 955 | static const AVClass ccaption_dec_class = { | ||
| 956 | .class_name = "Closed Captions Decoder", | ||
| 957 | .item_name = av_default_item_name, | ||
| 958 | .option = options, | ||
| 959 | .version = LIBAVUTIL_VERSION_INT, | ||
| 960 | }; | ||
| 961 | |||
| 962 | const FFCodec ff_ccaption_decoder = { | ||
| 963 | .p.name = "cc_dec", | ||
| 964 | CODEC_LONG_NAME("Closed Captions (EIA-608 / CEA-708)"), | ||
| 965 | .p.type = AVMEDIA_TYPE_SUBTITLE, | ||
| 966 | .p.id = AV_CODEC_ID_EIA_608, | ||
| 967 | .p.priv_class = &ccaption_dec_class, | ||
| 968 | .p.capabilities = AV_CODEC_CAP_DELAY, | ||
| 969 | .priv_data_size = sizeof(CCaptionSubContext), | ||
| 970 | .init = init_decoder, | ||
| 971 | .close = close_decoder, | ||
| 972 | .flush = flush_decoder, | ||
| 973 | FF_CODEC_DECODE_SUB_CB(decode), | ||
| 974 | }; | ||
| 975 |