You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

844 lines
26KB

  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. #include "avcodec.h"
  22. #include "ass.h"
  23. #include "libavutil/opt.h"
  24. #define SCREEN_ROWS 15
  25. #define SCREEN_COLUMNS 32
  26. #define SET_FLAG(var, val) ( (var) |= ( 1 << (val)) )
  27. #define UNSET_FLAG(var, val) ( (var) &= ~( 1 << (val)) )
  28. #define CHECK_FLAG(var, val) ( (var) & ( 1 << (val)) )
  29. static const AVRational ms_tb = {1, 1000};
  30. /*
  31. * TODO list
  32. * 1) handle font and color completely
  33. */
  34. enum cc_mode {
  35. CCMODE_POPON,
  36. CCMODE_PAINTON,
  37. CCMODE_ROLLUP,
  38. CCMODE_TEXT,
  39. };
  40. enum cc_color_code {
  41. CCCOL_WHITE,
  42. CCCOL_GREEN,
  43. CCCOL_BLUE,
  44. CCCOL_CYAN,
  45. CCCOL_RED,
  46. CCCOL_YELLOW,
  47. CCCOL_MAGENTA,
  48. CCCOL_USERDEFINED,
  49. CCCOL_BLACK,
  50. CCCOL_TRANSPARENT,
  51. };
  52. enum cc_font {
  53. CCFONT_REGULAR,
  54. CCFONT_ITALICS,
  55. CCFONT_UNDERLINED,
  56. CCFONT_UNDERLINED_ITALICS,
  57. };
  58. enum cc_charset {
  59. CCSET_BASIC_AMERICAN,
  60. CCSET_SPECIAL_AMERICAN,
  61. CCSET_EXTENDED_SPANISH_FRENCH_MISC,
  62. CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH,
  63. };
  64. static const char *charset_overrides[4][128] =
  65. {
  66. [CCSET_BASIC_AMERICAN] = {
  67. [0x27] = "\u2019",
  68. [0x2a] = "\u00e1",
  69. [0x5c] = "\u00e9",
  70. [0x5e] = "\u00ed",
  71. [0x5f] = "\u00f3",
  72. [0x60] = "\u00fa",
  73. [0x7b] = "\u00e7",
  74. [0x7c] = "\u00f7",
  75. [0x7d] = "\u00d1",
  76. [0x7e] = "\u00f1",
  77. [0x7f] = "\u2588"
  78. },
  79. [CCSET_SPECIAL_AMERICAN] = {
  80. [0x30] = "\u00ae",
  81. [0x31] = "\u00b0",
  82. [0x32] = "\u00bd",
  83. [0x33] = "\u00bf",
  84. [0x34] = "\u2122",
  85. [0x35] = "\u00a2",
  86. [0x36] = "\u00a3",
  87. [0x37] = "\u266a",
  88. [0x38] = "\u00e0",
  89. [0x39] = "\u00A0",
  90. [0x3a] = "\u00e8",
  91. [0x3b] = "\u00e2",
  92. [0x3c] = "\u00ea",
  93. [0x3d] = "\u00ee",
  94. [0x3e] = "\u00f4",
  95. [0x3f] = "\u00fb",
  96. },
  97. [CCSET_EXTENDED_SPANISH_FRENCH_MISC] = {
  98. [0x20] = "\u00c1",
  99. [0x21] = "\u00c9",
  100. [0x22] = "\u00d3",
  101. [0x23] = "\u00da",
  102. [0x24] = "\u00dc",
  103. [0x25] = "\u00fc",
  104. [0x26] = "\u00b4",
  105. [0x27] = "\u00a1",
  106. [0x28] = "*",
  107. [0x29] = "\u2018",
  108. [0x2a] = "-",
  109. [0x2b] = "\u00a9",
  110. [0x2c] = "\u2120",
  111. [0x2d] = "\u00b7",
  112. [0x2e] = "\u201c",
  113. [0x2f] = "\u201d",
  114. [0x30] = "\u00c0",
  115. [0x31] = "\u00c2",
  116. [0x32] = "\u00c7",
  117. [0x33] = "\u00c8",
  118. [0x34] = "\u00ca",
  119. [0x35] = "\u00cb",
  120. [0x36] = "\u00eb",
  121. [0x37] = "\u00ce",
  122. [0x38] = "\u00cf",
  123. [0x39] = "\u00ef",
  124. [0x3a] = "\u00d4",
  125. [0x3b] = "\u00d9",
  126. [0x3c] = "\u00f9",
  127. [0x3d] = "\u00db",
  128. [0x3e] = "\u00ab",
  129. [0x3f] = "\u00bb",
  130. },
  131. [CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH] = {
  132. [0x20] = "\u00c3",
  133. [0x21] = "\u00e3",
  134. [0x22] = "\u00cd",
  135. [0x23] = "\u00cc",
  136. [0x24] = "\u00ec",
  137. [0x25] = "\u00d2",
  138. [0x26] = "\u00f2",
  139. [0x27] = "\u00d5",
  140. [0x28] = "\u00f5",
  141. [0x29] = "{",
  142. [0x2a] = "}",
  143. [0x2b] = "\\",
  144. [0x2c] = "^",
  145. [0x2d] = "_",
  146. [0x2e] = "|",
  147. [0x2f] = "~",
  148. [0x30] = "\u00c4",
  149. [0x31] = "\u00e4",
  150. [0x32] = "\u00d6",
  151. [0x33] = "\u00f6",
  152. [0x34] = "\u00df",
  153. [0x35] = "\u00a5",
  154. [0x36] = "\u00a4",
  155. [0x37] = "\u00a6",
  156. [0x38] = "\u00c5",
  157. [0x39] = "\u00e5",
  158. [0x3a] = "\u00d8",
  159. [0x3b] = "\u00f8",
  160. [0x3c] = "\u250c",
  161. [0x3d] = "\u2510",
  162. [0x3e] = "\u2514",
  163. [0x3f] = "\u2518",
  164. },
  165. };
  166. static const unsigned char pac2_attribs[32][3] = // Color, font, ident
  167. {
  168. { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x40 || 0x60
  169. { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x41 || 0x61
  170. { CCCOL_GREEN, CCFONT_REGULAR, 0 }, // 0x42 || 0x62
  171. { CCCOL_GREEN, CCFONT_UNDERLINED, 0 }, // 0x43 || 0x63
  172. { CCCOL_BLUE, CCFONT_REGULAR, 0 }, // 0x44 || 0x64
  173. { CCCOL_BLUE, CCFONT_UNDERLINED, 0 }, // 0x45 || 0x65
  174. { CCCOL_CYAN, CCFONT_REGULAR, 0 }, // 0x46 || 0x66
  175. { CCCOL_CYAN, CCFONT_UNDERLINED, 0 }, // 0x47 || 0x67
  176. { CCCOL_RED, CCFONT_REGULAR, 0 }, // 0x48 || 0x68
  177. { CCCOL_RED, CCFONT_UNDERLINED, 0 }, // 0x49 || 0x69
  178. { CCCOL_YELLOW, CCFONT_REGULAR, 0 }, // 0x4a || 0x6a
  179. { CCCOL_YELLOW, CCFONT_UNDERLINED, 0 }, // 0x4b || 0x6b
  180. { CCCOL_MAGENTA, CCFONT_REGULAR, 0 }, // 0x4c || 0x6c
  181. { CCCOL_MAGENTA, CCFONT_UNDERLINED, 0 }, // 0x4d || 0x6d
  182. { CCCOL_WHITE, CCFONT_ITALICS, 0 }, // 0x4e || 0x6e
  183. { CCCOL_WHITE, CCFONT_UNDERLINED_ITALICS, 0 }, // 0x4f || 0x6f
  184. { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x50 || 0x70
  185. { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x51 || 0x71
  186. { CCCOL_WHITE, CCFONT_REGULAR, 4 }, // 0x52 || 0x72
  187. { CCCOL_WHITE, CCFONT_UNDERLINED, 4 }, // 0x53 || 0x73
  188. { CCCOL_WHITE, CCFONT_REGULAR, 8 }, // 0x54 || 0x74
  189. { CCCOL_WHITE, CCFONT_UNDERLINED, 8 }, // 0x55 || 0x75
  190. { CCCOL_WHITE, CCFONT_REGULAR, 12 }, // 0x56 || 0x76
  191. { CCCOL_WHITE, CCFONT_UNDERLINED, 12 }, // 0x57 || 0x77
  192. { CCCOL_WHITE, CCFONT_REGULAR, 16 }, // 0x58 || 0x78
  193. { CCCOL_WHITE, CCFONT_UNDERLINED, 16 }, // 0x59 || 0x79
  194. { CCCOL_WHITE, CCFONT_REGULAR, 20 }, // 0x5a || 0x7a
  195. { CCCOL_WHITE, CCFONT_UNDERLINED, 20 }, // 0x5b || 0x7b
  196. { CCCOL_WHITE, CCFONT_REGULAR, 24 }, // 0x5c || 0x7c
  197. { CCCOL_WHITE, CCFONT_UNDERLINED, 24 }, // 0x5d || 0x7d
  198. { CCCOL_WHITE, CCFONT_REGULAR, 28 }, // 0x5e || 0x7e
  199. { CCCOL_WHITE, CCFONT_UNDERLINED, 28 } // 0x5f || 0x7f
  200. /* total 32 entries */
  201. };
  202. struct Screen {
  203. /* +1 is used to compensate null character of string */
  204. uint8_t characters[SCREEN_ROWS+1][SCREEN_COLUMNS+1];
  205. uint8_t charsets[SCREEN_ROWS+1][SCREEN_COLUMNS+1];
  206. uint8_t colors[SCREEN_ROWS+1][SCREEN_COLUMNS+1];
  207. uint8_t fonts[SCREEN_ROWS+1][SCREEN_COLUMNS+1];
  208. /*
  209. * Bitmask of used rows; if a bit is not set, the
  210. * corresponding row is not used.
  211. * for setting row 1 use row | (1 << 0)
  212. * for setting row 15 use row | (1 << 14)
  213. */
  214. int16_t row_used;
  215. };
  216. typedef struct CCaptionSubContext {
  217. AVClass *class;
  218. int real_time;
  219. struct Screen screen[2];
  220. int active_screen;
  221. uint8_t cursor_row;
  222. uint8_t cursor_column;
  223. uint8_t cursor_color;
  224. uint8_t cursor_font;
  225. uint8_t cursor_charset;
  226. AVBPrint buffer;
  227. int buffer_changed;
  228. int rollup;
  229. enum cc_mode mode;
  230. int64_t start_time;
  231. /* visible screen time */
  232. int64_t startv_time;
  233. int64_t end_time;
  234. int screen_touched;
  235. int64_t last_real_time;
  236. char prev_cmd[2];
  237. /* buffer to store pkt data */
  238. uint8_t *pktbuf;
  239. int pktbuf_size;
  240. int readorder;
  241. } CCaptionSubContext;
  242. static av_cold int init_decoder(AVCodecContext *avctx)
  243. {
  244. int ret;
  245. CCaptionSubContext *ctx = avctx->priv_data;
  246. av_bprint_init(&ctx->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
  247. /* taking by default roll up to 2 */
  248. ctx->mode = CCMODE_ROLLUP;
  249. ctx->rollup = 2;
  250. ctx->cursor_row = 10;
  251. ret = ff_ass_subtitle_header(avctx, "Monospace",
  252. ASS_DEFAULT_FONT_SIZE,
  253. ASS_DEFAULT_COLOR,
  254. ASS_DEFAULT_BACK_COLOR,
  255. ASS_DEFAULT_BOLD,
  256. ASS_DEFAULT_ITALIC,
  257. ASS_DEFAULT_UNDERLINE,
  258. 3,
  259. ASS_DEFAULT_ALIGNMENT);
  260. if (ret < 0) {
  261. return ret;
  262. }
  263. return ret;
  264. }
  265. static av_cold int close_decoder(AVCodecContext *avctx)
  266. {
  267. CCaptionSubContext *ctx = avctx->priv_data;
  268. av_bprint_finalize(&ctx->buffer, NULL);
  269. av_freep(&ctx->pktbuf);
  270. ctx->pktbuf_size = 0;
  271. return 0;
  272. }
  273. static void flush_decoder(AVCodecContext *avctx)
  274. {
  275. CCaptionSubContext *ctx = avctx->priv_data;
  276. ctx->screen[0].row_used = 0;
  277. ctx->screen[1].row_used = 0;
  278. ctx->prev_cmd[0] = 0;
  279. ctx->prev_cmd[1] = 0;
  280. ctx->mode = CCMODE_ROLLUP;
  281. ctx->rollup = 2;
  282. ctx->cursor_row = 10;
  283. ctx->cursor_column = 0;
  284. ctx->cursor_font = 0;
  285. ctx->cursor_color = 0;
  286. ctx->cursor_charset = 0;
  287. ctx->active_screen = 0;
  288. ctx->last_real_time = 0;
  289. ctx->screen_touched = 0;
  290. ctx->buffer_changed = 0;
  291. if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
  292. ctx->readorder = 0;
  293. av_bprint_clear(&ctx->buffer);
  294. }
  295. /**
  296. * @param ctx closed caption context just to print log
  297. */
  298. static void write_char(CCaptionSubContext *ctx, struct Screen *screen, char ch)
  299. {
  300. uint8_t col = ctx->cursor_column;
  301. char *row = screen->characters[ctx->cursor_row];
  302. char *font = screen->fonts[ctx->cursor_row];
  303. char *charset = screen->charsets[ctx->cursor_row];
  304. if (col < SCREEN_COLUMNS) {
  305. row[col] = ch;
  306. font[col] = ctx->cursor_font;
  307. charset[col] = ctx->cursor_charset;
  308. ctx->cursor_charset = CCSET_BASIC_AMERICAN;
  309. if (ch) ctx->cursor_column++;
  310. return;
  311. }
  312. /* We have extra space at end only for null character */
  313. else if (col == SCREEN_COLUMNS && ch == 0) {
  314. row[col] = ch;
  315. return;
  316. }
  317. else {
  318. av_log(ctx, AV_LOG_WARNING, "Data Ignored since exceeding screen width\n");
  319. return;
  320. }
  321. }
  322. /**
  323. * This function after validating parity bit, also remove it from data pair.
  324. * The first byte doesn't pass parity, we replace it with a solid blank
  325. * and process the pair.
  326. * If the second byte doesn't pass parity, it returns INVALIDDATA
  327. * user can ignore the whole pair and pass the other pair.
  328. */
  329. static int validate_cc_data_pair(uint8_t *cc_data_pair)
  330. {
  331. uint8_t cc_valid = (*cc_data_pair & 4) >>2;
  332. uint8_t cc_type = *cc_data_pair & 3;
  333. if (!cc_valid)
  334. return AVERROR_INVALIDDATA;
  335. // if EIA-608 data then verify parity.
  336. if (cc_type==0 || cc_type==1) {
  337. if (!av_parity(cc_data_pair[2])) {
  338. return AVERROR_INVALIDDATA;
  339. }
  340. if (!av_parity(cc_data_pair[1])) {
  341. cc_data_pair[1]=0x7F;
  342. }
  343. }
  344. //Skip non-data
  345. if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD)
  346. && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0)
  347. return AVERROR_PATCHWELCOME;
  348. //skip 708 data
  349. if (cc_type == 3 || cc_type == 2)
  350. return AVERROR_PATCHWELCOME;
  351. /* remove parity bit */
  352. cc_data_pair[1] &= 0x7F;
  353. cc_data_pair[2] &= 0x7F;
  354. return 0;
  355. }
  356. static struct Screen *get_writing_screen(CCaptionSubContext *ctx)
  357. {
  358. switch (ctx->mode) {
  359. case CCMODE_POPON:
  360. // use Inactive screen
  361. return ctx->screen + !ctx->active_screen;
  362. case CCMODE_PAINTON:
  363. case CCMODE_ROLLUP:
  364. case CCMODE_TEXT:
  365. // use active screen
  366. return ctx->screen + ctx->active_screen;
  367. }
  368. /* It was never an option */
  369. return NULL;
  370. }
  371. static void roll_up(CCaptionSubContext *ctx)
  372. {
  373. struct Screen *screen;
  374. int i, keep_lines;
  375. if (ctx->mode == CCMODE_TEXT)
  376. return;
  377. screen = get_writing_screen(ctx);
  378. /* +1 signify cursor_row starts from 0
  379. * Can't keep lines less then row cursor pos
  380. */
  381. keep_lines = FFMIN(ctx->cursor_row + 1, ctx->rollup);
  382. for (i = 0; i < SCREEN_ROWS; i++) {
  383. if (i > ctx->cursor_row - keep_lines && i <= ctx->cursor_row)
  384. continue;
  385. UNSET_FLAG(screen->row_used, i);
  386. }
  387. for (i = 0; i < keep_lines && screen->row_used; i++) {
  388. const int i_row = ctx->cursor_row - keep_lines + i + 1;
  389. memcpy(screen->characters[i_row], screen->characters[i_row+1], SCREEN_COLUMNS);
  390. memcpy(screen->colors[i_row], screen->colors[i_row+1], SCREEN_COLUMNS);
  391. memcpy(screen->fonts[i_row], screen->fonts[i_row+1], SCREEN_COLUMNS);
  392. memcpy(screen->charsets[i_row], screen->charsets[i_row+1], SCREEN_COLUMNS);
  393. if (CHECK_FLAG(screen->row_used, i_row + 1))
  394. SET_FLAG(screen->row_used, i_row);
  395. }
  396. UNSET_FLAG(screen->row_used, ctx->cursor_row);
  397. }
  398. static int capture_screen(CCaptionSubContext *ctx)
  399. {
  400. int i, j, tab = 0;
  401. struct Screen *screen = ctx->screen + ctx->active_screen;
  402. enum cc_font prev_font = CCFONT_REGULAR;
  403. av_bprint_clear(&ctx->buffer);
  404. for (i = 0; screen->row_used && i < SCREEN_ROWS; i++)
  405. {
  406. if (CHECK_FLAG(screen->row_used, i)) {
  407. const char *row = screen->characters[i];
  408. const char *charset = screen->charsets[i];
  409. j = 0;
  410. while (row[j] == ' ' && charset[j] == CCSET_BASIC_AMERICAN)
  411. j++;
  412. if (!tab || j < tab)
  413. tab = j;
  414. }
  415. }
  416. for (i = 0; screen->row_used && i < SCREEN_ROWS; i++)
  417. {
  418. if (CHECK_FLAG(screen->row_used, i)) {
  419. const char *row = screen->characters[i];
  420. const char *font = screen->fonts[i];
  421. const char *charset = screen->charsets[i];
  422. const char *override;
  423. int x, y, seen_char = 0;
  424. j = 0;
  425. /* skip leading space */
  426. while (row[j] == ' ' && charset[j] == CCSET_BASIC_AMERICAN && j < tab)
  427. j++;
  428. x = ASS_DEFAULT_PLAYRESX * (0.1 + 0.0250 * j);
  429. y = ASS_DEFAULT_PLAYRESY * (0.1 + 0.0533 * i);
  430. av_bprintf(&ctx->buffer, "{\\an7}{\\pos(%d,%d)}", x, y);
  431. for (; j < SCREEN_COLUMNS; j++) {
  432. const char *e_tag = "", *s_tag = "";
  433. if (row[j] == 0)
  434. break;
  435. if (prev_font != font[j]) {
  436. switch (prev_font) {
  437. case CCFONT_ITALICS:
  438. e_tag = "{\\i0}";
  439. break;
  440. case CCFONT_UNDERLINED:
  441. e_tag = "{\\u0}";
  442. break;
  443. case CCFONT_UNDERLINED_ITALICS:
  444. e_tag = "{\\u0}{\\i0}";
  445. break;
  446. }
  447. switch (font[j]) {
  448. case CCFONT_ITALICS:
  449. s_tag = "{\\i1}";
  450. break;
  451. case CCFONT_UNDERLINED:
  452. s_tag = "{\\u1}";
  453. break;
  454. case CCFONT_UNDERLINED_ITALICS:
  455. s_tag = "{\\u1}{\\i1}";
  456. break;
  457. }
  458. }
  459. prev_font = font[j];
  460. override = charset_overrides[(int)charset[j]][(int)row[j]];
  461. if (override) {
  462. av_bprintf(&ctx->buffer, "%s%s%s", e_tag, s_tag, override);
  463. seen_char = 1;
  464. } else if (row[j] == ' ' && !seen_char) {
  465. av_bprintf(&ctx->buffer, "%s%s\\h", e_tag, s_tag);
  466. } else {
  467. av_bprintf(&ctx->buffer, "%s%s%c", e_tag, s_tag, row[j]);
  468. seen_char = 1;
  469. }
  470. }
  471. av_bprintf(&ctx->buffer, "\\N");
  472. }
  473. }
  474. if (!av_bprint_is_complete(&ctx->buffer))
  475. return AVERROR(ENOMEM);
  476. if (screen->row_used && ctx->buffer.len >= 2) {
  477. ctx->buffer.len -= 2;
  478. ctx->buffer.str[ctx->buffer.len] = 0;
  479. }
  480. ctx->buffer_changed = 1;
  481. return 0;
  482. }
  483. static int reap_screen(CCaptionSubContext *ctx, int64_t pts)
  484. {
  485. ctx->start_time = ctx->startv_time;
  486. ctx->startv_time = pts;
  487. ctx->end_time = pts;
  488. return capture_screen(ctx);
  489. }
  490. static void handle_textattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
  491. {
  492. int i = lo - 0x20;
  493. struct Screen *screen = get_writing_screen(ctx);
  494. if (i >= 32)
  495. return;
  496. ctx->cursor_color = pac2_attribs[i][0];
  497. ctx->cursor_font = pac2_attribs[i][1];
  498. SET_FLAG(screen->row_used, ctx->cursor_row);
  499. write_char(ctx, screen, ' ');
  500. }
  501. static void handle_pac(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
  502. {
  503. static const int8_t row_map[] = {
  504. 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
  505. };
  506. const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
  507. struct Screen *screen = get_writing_screen(ctx);
  508. int indent, i;
  509. if (row_map[index] <= 0) {
  510. av_log(ctx, AV_LOG_DEBUG, "Invalid pac index encountered\n");
  511. return;
  512. }
  513. lo &= 0x1f;
  514. ctx->cursor_row = row_map[index] - 1;
  515. ctx->cursor_color = pac2_attribs[lo][0];
  516. ctx->cursor_font = pac2_attribs[lo][1];
  517. ctx->cursor_charset = CCSET_BASIC_AMERICAN;
  518. ctx->cursor_column = 0;
  519. indent = pac2_attribs[lo][2];
  520. for (i = 0; i < indent; i++) {
  521. write_char(ctx, screen, ' ');
  522. }
  523. }
  524. /**
  525. * @param pts it is required to set end time
  526. */
  527. static void handle_edm(CCaptionSubContext *ctx, int64_t pts)
  528. {
  529. struct Screen *screen = ctx->screen + ctx->active_screen;
  530. // In buffered mode, keep writing to screen until it is wiped.
  531. // Before wiping the display, capture contents to emit subtitle.
  532. if (!ctx->real_time)
  533. reap_screen(ctx, pts);
  534. screen->row_used = 0;
  535. // In realtime mode, emit an empty caption so the last one doesn't
  536. // stay on the screen.
  537. if (ctx->real_time)
  538. reap_screen(ctx, pts);
  539. }
  540. static void handle_eoc(CCaptionSubContext *ctx, int64_t pts)
  541. {
  542. // In buffered mode, we wait til the *next* EOC and
  543. // reap what was already on the screen since the last EOC.
  544. if (!ctx->real_time)
  545. handle_edm(ctx,pts);
  546. ctx->active_screen = !ctx->active_screen;
  547. ctx->cursor_column = 0;
  548. // In realtime mode, we display the buffered contents (after
  549. // flipping the buffer to active above) as soon as EOC arrives.
  550. if (ctx->real_time)
  551. reap_screen(ctx, pts);
  552. }
  553. static void handle_delete_end_of_row(CCaptionSubContext *ctx, char hi, char lo)
  554. {
  555. struct Screen *screen = get_writing_screen(ctx);
  556. write_char(ctx, screen, 0);
  557. }
  558. static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
  559. {
  560. struct Screen *screen = get_writing_screen(ctx);
  561. SET_FLAG(screen->row_used, ctx->cursor_row);
  562. switch (hi) {
  563. case 0x11:
  564. ctx->cursor_charset = CCSET_SPECIAL_AMERICAN;
  565. break;
  566. case 0x12:
  567. if (ctx->cursor_column > 0)
  568. ctx->cursor_column -= 1;
  569. ctx->cursor_charset = CCSET_EXTENDED_SPANISH_FRENCH_MISC;
  570. break;
  571. case 0x13:
  572. if (ctx->cursor_column > 0)
  573. ctx->cursor_column -= 1;
  574. ctx->cursor_charset = CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH;
  575. break;
  576. default:
  577. ctx->cursor_charset = CCSET_BASIC_AMERICAN;
  578. write_char(ctx, screen, hi);
  579. break;
  580. }
  581. if (lo) {
  582. write_char(ctx, screen, lo);
  583. }
  584. write_char(ctx, screen, 0);
  585. if (ctx->mode != CCMODE_POPON)
  586. ctx->screen_touched = 1;
  587. if (lo)
  588. ff_dlog(ctx, "(%c,%c)\n", hi, lo);
  589. else
  590. ff_dlog(ctx, "(%c)\n", hi);
  591. }
  592. static void process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8_t lo)
  593. {
  594. if (hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) {
  595. /* ignore redundant command */
  596. return;
  597. }
  598. /* set prev command */
  599. ctx->prev_cmd[0] = hi;
  600. ctx->prev_cmd[1] = lo;
  601. if ( (hi == 0x10 && (lo >= 0x40 && lo <= 0x5f)) ||
  602. ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
  603. handle_pac(ctx, hi, lo);
  604. } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) ||
  605. ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) {
  606. handle_textattr(ctx, hi, lo);
  607. } else if (hi == 0x14 || hi == 0x15 || hi == 0x1c) {
  608. switch (lo) {
  609. case 0x20:
  610. /* resume caption loading */
  611. ctx->mode = CCMODE_POPON;
  612. break;
  613. case 0x24:
  614. handle_delete_end_of_row(ctx, hi, lo);
  615. break;
  616. case 0x25:
  617. case 0x26:
  618. case 0x27:
  619. ctx->rollup = lo - 0x23;
  620. ctx->mode = CCMODE_ROLLUP;
  621. break;
  622. case 0x29:
  623. /* resume direct captioning */
  624. ctx->mode = CCMODE_PAINTON;
  625. break;
  626. case 0x2b:
  627. /* resume text display */
  628. ctx->mode = CCMODE_TEXT;
  629. break;
  630. case 0x2c:
  631. /* erase display memory */
  632. handle_edm(ctx, pts);
  633. break;
  634. case 0x2d:
  635. /* carriage return */
  636. ff_dlog(ctx, "carriage return\n");
  637. if (!ctx->real_time)
  638. reap_screen(ctx, pts);
  639. roll_up(ctx);
  640. ctx->cursor_column = 0;
  641. break;
  642. case 0x2e:
  643. /* erase buffered (non displayed) memory */
  644. // Only in realtime mode. In buffered mode, we re-use the inactive screen
  645. // for our own buffering.
  646. if (ctx->real_time) {
  647. struct Screen *screen = ctx->screen + !ctx->active_screen;
  648. screen->row_used = 0;
  649. }
  650. break;
  651. case 0x2f:
  652. /* end of caption */
  653. ff_dlog(ctx, "handle_eoc\n");
  654. handle_eoc(ctx, pts);
  655. break;
  656. default:
  657. ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
  658. break;
  659. }
  660. } else if (hi >= 0x11 && hi <= 0x13) {
  661. /* Special characters */
  662. handle_char(ctx, hi, lo, pts);
  663. } else if (hi >= 0x20) {
  664. /* Standard characters (always in pairs) */
  665. handle_char(ctx, hi, lo, pts);
  666. ctx->prev_cmd[0] = ctx->prev_cmd[1] = 0;
  667. } else if (hi == 0x17 && lo >= 0x21 && lo <= 0x23) {
  668. int i;
  669. /* Tab offsets (spacing) */
  670. for (i = 0; i < lo - 0x20; i++) {
  671. handle_char(ctx, ' ', 0, pts);
  672. }
  673. } else {
  674. /* Ignoring all other non data code */
  675. ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
  676. }
  677. }
  678. static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
  679. {
  680. CCaptionSubContext *ctx = avctx->priv_data;
  681. AVSubtitle *sub = data;
  682. const int64_t start_time = sub->pts;
  683. uint8_t *bptr = NULL;
  684. int len = avpkt->size;
  685. int ret = 0;
  686. int i;
  687. av_fast_padded_malloc(&ctx->pktbuf, &ctx->pktbuf_size, len);
  688. if (!ctx->pktbuf) {
  689. av_log(ctx, AV_LOG_WARNING, "Insufficient Memory of %d truncated to %d\n", len, ctx->pktbuf_size);
  690. return AVERROR(ENOMEM);
  691. }
  692. memcpy(ctx->pktbuf, avpkt->data, len);
  693. bptr = ctx->pktbuf;
  694. for (i = 0; i < len; i += 3) {
  695. uint8_t cc_type = *(bptr + i) & 3;
  696. if (validate_cc_data_pair(bptr + i))
  697. continue;
  698. /* ignoring data field 1 */
  699. if(cc_type == 1)
  700. continue;
  701. else
  702. process_cc608(ctx, start_time, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f);
  703. if (!ctx->buffer_changed)
  704. continue;
  705. ctx->buffer_changed = 0;
  706. if (*ctx->buffer.str || ctx->real_time)
  707. {
  708. ff_dlog(ctx, "cdp writing data (%s)\n",ctx->buffer.str);
  709. ret = ff_ass_add_rect(sub, ctx->buffer.str, ctx->readorder++, 0, NULL, NULL);
  710. if (ret < 0)
  711. return ret;
  712. sub->pts = ctx->start_time;
  713. if (!ctx->real_time)
  714. sub->end_display_time = av_rescale_q(ctx->end_time - ctx->start_time,
  715. AV_TIME_BASE_Q, ms_tb);
  716. else
  717. sub->end_display_time = -1;
  718. ctx->buffer_changed = 0;
  719. ctx->last_real_time = sub->pts;
  720. ctx->screen_touched = 0;
  721. }
  722. }
  723. if (ctx->real_time && ctx->screen_touched &&
  724. sub->pts > ctx->last_real_time + av_rescale_q(200, ms_tb, AV_TIME_BASE_Q)) {
  725. ctx->last_real_time = sub->pts;
  726. ctx->screen_touched = 0;
  727. capture_screen(ctx);
  728. ctx->buffer_changed = 0;
  729. ret = ff_ass_add_rect(sub, ctx->buffer.str, ctx->readorder++, 0, NULL, NULL);
  730. if (ret < 0)
  731. return ret;
  732. sub->end_display_time = -1;
  733. }
  734. *got_sub = sub->num_rects > 0;
  735. return ret;
  736. }
  737. #define OFFSET(x) offsetof(CCaptionSubContext, x)
  738. #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
  739. static const AVOption options[] = {
  740. { "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 },
  741. {NULL}
  742. };
  743. static const AVClass ccaption_dec_class = {
  744. .class_name = "Closed caption Decoder",
  745. .item_name = av_default_item_name,
  746. .option = options,
  747. .version = LIBAVUTIL_VERSION_INT,
  748. };
  749. AVCodec ff_ccaption_decoder = {
  750. .name = "cc_dec",
  751. .long_name = NULL_IF_CONFIG_SMALL("Closed Caption (EIA-608 / CEA-708)"),
  752. .type = AVMEDIA_TYPE_SUBTITLE,
  753. .id = AV_CODEC_ID_EIA_608,
  754. .priv_data_size = sizeof(CCaptionSubContext),
  755. .init = init_decoder,
  756. .close = close_decoder,
  757. .flush = flush_decoder,
  758. .decode = decode,
  759. .priv_class = &ccaption_dec_class,
  760. };