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.

825 lines
25KB

  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][SCREEN_COLUMNS+1];
  205. uint8_t charsets[SCREEN_ROWS][SCREEN_COLUMNS+1];
  206. uint8_t colors[SCREEN_ROWS][SCREEN_COLUMNS+1];
  207. uint8_t fonts[SCREEN_ROWS][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. AVBufferRef *pktbuf;
  239. int readorder;
  240. } CCaptionSubContext;
  241. static av_cold int init_decoder(AVCodecContext *avctx)
  242. {
  243. int ret;
  244. CCaptionSubContext *ctx = avctx->priv_data;
  245. av_bprint_init(&ctx->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
  246. /* taking by default roll up to 2 */
  247. ctx->mode = CCMODE_ROLLUP;
  248. ctx->rollup = 2;
  249. ret = ff_ass_subtitle_header(avctx, "Monospace",
  250. ASS_DEFAULT_FONT_SIZE,
  251. ASS_DEFAULT_COLOR,
  252. ASS_DEFAULT_BACK_COLOR,
  253. ASS_DEFAULT_BOLD,
  254. ASS_DEFAULT_ITALIC,
  255. ASS_DEFAULT_UNDERLINE,
  256. 3,
  257. ASS_DEFAULT_ALIGNMENT);
  258. if (ret < 0) {
  259. return ret;
  260. }
  261. /* allocate pkt buffer */
  262. ctx->pktbuf = av_buffer_alloc(128);
  263. if (!ctx->pktbuf) {
  264. ret = AVERROR(ENOMEM);
  265. }
  266. return ret;
  267. }
  268. static av_cold int close_decoder(AVCodecContext *avctx)
  269. {
  270. CCaptionSubContext *ctx = avctx->priv_data;
  271. av_bprint_finalize(&ctx->buffer, NULL);
  272. av_buffer_unref(&ctx->pktbuf);
  273. return 0;
  274. }
  275. static void flush_decoder(AVCodecContext *avctx)
  276. {
  277. CCaptionSubContext *ctx = avctx->priv_data;
  278. ctx->screen[0].row_used = 0;
  279. ctx->screen[1].row_used = 0;
  280. ctx->prev_cmd[0] = 0;
  281. ctx->prev_cmd[1] = 0;
  282. ctx->mode = CCMODE_ROLLUP;
  283. ctx->rollup = 2;
  284. ctx->cursor_row = 0;
  285. ctx->cursor_column = 0;
  286. ctx->cursor_font = 0;
  287. ctx->cursor_color = 0;
  288. ctx->cursor_charset = 0;
  289. ctx->active_screen = 0;
  290. ctx->last_real_time = 0;
  291. ctx->screen_touched = 0;
  292. ctx->buffer_changed = 0;
  293. if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
  294. ctx->readorder = 0;
  295. av_bprint_clear(&ctx->buffer);
  296. }
  297. /**
  298. * @param ctx closed caption context just to print log
  299. */
  300. static void write_char(CCaptionSubContext *ctx, struct Screen *screen, char ch)
  301. {
  302. uint8_t col = ctx->cursor_column;
  303. char *row = screen->characters[ctx->cursor_row];
  304. char *font = screen->fonts[ctx->cursor_row];
  305. char *charset = screen->charsets[ctx->cursor_row];
  306. if (col < SCREEN_COLUMNS) {
  307. row[col] = ch;
  308. font[col] = ctx->cursor_font;
  309. charset[col] = ctx->cursor_charset;
  310. ctx->cursor_charset = CCSET_BASIC_AMERICAN;
  311. if (ch) ctx->cursor_column++;
  312. return;
  313. }
  314. /* We have extra space at end only for null character */
  315. else if (col == SCREEN_COLUMNS && ch == 0) {
  316. row[col] = ch;
  317. return;
  318. }
  319. else {
  320. av_log(ctx, AV_LOG_WARNING, "Data Ignored since exceeding screen width\n");
  321. return;
  322. }
  323. }
  324. /**
  325. * This function after validating parity bit, also remove it from data pair.
  326. * The first byte doesn't pass parity, we replace it with a solid blank
  327. * and process the pair.
  328. * If the second byte doesn't pass parity, it returns INVALIDDATA
  329. * user can ignore the whole pair and pass the other pair.
  330. */
  331. static int validate_cc_data_pair(uint8_t *cc_data_pair)
  332. {
  333. uint8_t cc_valid = (*cc_data_pair & 4) >>2;
  334. uint8_t cc_type = *cc_data_pair & 3;
  335. if (!cc_valid)
  336. return AVERROR_INVALIDDATA;
  337. // if EIA-608 data then verify parity.
  338. if (cc_type==0 || cc_type==1) {
  339. if (!av_parity(cc_data_pair[2])) {
  340. return AVERROR_INVALIDDATA;
  341. }
  342. if (!av_parity(cc_data_pair[1])) {
  343. cc_data_pair[1]=0x7F;
  344. }
  345. }
  346. //Skip non-data
  347. if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD)
  348. && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0)
  349. return AVERROR_PATCHWELCOME;
  350. //skip 708 data
  351. if (cc_type == 3 || cc_type == 2)
  352. return AVERROR_PATCHWELCOME;
  353. /* remove parity bit */
  354. cc_data_pair[1] &= 0x7F;
  355. cc_data_pair[2] &= 0x7F;
  356. return 0;
  357. }
  358. static struct Screen *get_writing_screen(CCaptionSubContext *ctx)
  359. {
  360. switch (ctx->mode) {
  361. case CCMODE_POPON:
  362. // use Inactive screen
  363. return ctx->screen + !ctx->active_screen;
  364. case CCMODE_PAINTON:
  365. case CCMODE_ROLLUP:
  366. case CCMODE_TEXT:
  367. // use active screen
  368. return ctx->screen + ctx->active_screen;
  369. }
  370. /* It was never an option */
  371. return NULL;
  372. }
  373. static void roll_up(CCaptionSubContext *ctx)
  374. {
  375. struct Screen *screen;
  376. int i, keep_lines;
  377. if (ctx->mode == CCMODE_TEXT)
  378. return;
  379. screen = get_writing_screen(ctx);
  380. /* +1 signify cursor_row starts from 0
  381. * Can't keep lines less then row cursor pos
  382. */
  383. keep_lines = FFMIN(ctx->cursor_row + 1, ctx->rollup);
  384. for (i = 0; i < SCREEN_ROWS; i++) {
  385. if (i > ctx->cursor_row - keep_lines && i <= ctx->cursor_row)
  386. continue;
  387. UNSET_FLAG(screen->row_used, i);
  388. }
  389. for (i = 0; i < keep_lines && screen->row_used; i++) {
  390. const int i_row = ctx->cursor_row - keep_lines + i + 1;
  391. memcpy(screen->characters[i_row], screen->characters[i_row+1], SCREEN_COLUMNS);
  392. memcpy(screen->colors[i_row], screen->colors[i_row+1], SCREEN_COLUMNS);
  393. memcpy(screen->fonts[i_row], screen->fonts[i_row+1], SCREEN_COLUMNS);
  394. memcpy(screen->charsets[i_row], screen->charsets[i_row+1], SCREEN_COLUMNS);
  395. if (CHECK_FLAG(screen->row_used, i_row + 1))
  396. SET_FLAG(screen->row_used, i_row);
  397. }
  398. UNSET_FLAG(screen->row_used, ctx->cursor_row);
  399. }
  400. static int capture_screen(CCaptionSubContext *ctx)
  401. {
  402. int i;
  403. struct Screen *screen = ctx->screen + ctx->active_screen;
  404. enum cc_font prev_font = CCFONT_REGULAR;
  405. av_bprint_clear(&ctx->buffer);
  406. for (i = 0; screen->row_used && i < SCREEN_ROWS; i++)
  407. {
  408. if (CHECK_FLAG(screen->row_used, i)) {
  409. const char *row = screen->characters[i];
  410. const char *font = screen->fonts[i];
  411. const char *charset = screen->charsets[i];
  412. const char *override;
  413. int j = 0;
  414. /* skip leading space */
  415. while (row[j] == ' ' && charset[j] == CCSET_BASIC_AMERICAN)
  416. j++;
  417. for (; j < SCREEN_COLUMNS; j++) {
  418. const char *e_tag = "", *s_tag = "";
  419. if (row[j] == 0)
  420. break;
  421. if (prev_font != font[j]) {
  422. switch (prev_font) {
  423. case CCFONT_ITALICS:
  424. e_tag = "{\\i0}";
  425. break;
  426. case CCFONT_UNDERLINED:
  427. e_tag = "{\\u0}";
  428. break;
  429. case CCFONT_UNDERLINED_ITALICS:
  430. e_tag = "{\\u0}{\\i0}";
  431. break;
  432. }
  433. switch (font[j]) {
  434. case CCFONT_ITALICS:
  435. s_tag = "{\\i1}";
  436. break;
  437. case CCFONT_UNDERLINED:
  438. s_tag = "{\\u1}";
  439. break;
  440. case CCFONT_UNDERLINED_ITALICS:
  441. s_tag = "{\\u1}{\\i1}";
  442. break;
  443. }
  444. }
  445. prev_font = font[j];
  446. override = charset_overrides[(int)charset[j]][(int)row[j]];
  447. if (override) {
  448. av_bprintf(&ctx->buffer, "%s%s%s", e_tag, s_tag, override);
  449. } else {
  450. av_bprintf(&ctx->buffer, "%s%s%c", e_tag, s_tag, row[j]);
  451. }
  452. }
  453. av_bprintf(&ctx->buffer, "\\N");
  454. }
  455. }
  456. if (!av_bprint_is_complete(&ctx->buffer))
  457. return AVERROR(ENOMEM);
  458. if (screen->row_used && ctx->buffer.len >= 2) {
  459. ctx->buffer.len -= 2;
  460. ctx->buffer.str[ctx->buffer.len] = 0;
  461. }
  462. ctx->buffer_changed = 1;
  463. return 0;
  464. }
  465. static int reap_screen(CCaptionSubContext *ctx, int64_t pts)
  466. {
  467. ctx->start_time = ctx->startv_time;
  468. ctx->startv_time = pts;
  469. ctx->end_time = pts;
  470. return capture_screen(ctx);
  471. }
  472. static void handle_textattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
  473. {
  474. int i = lo - 0x20;
  475. struct Screen *screen = get_writing_screen(ctx);
  476. if (i >= 32)
  477. return;
  478. ctx->cursor_color = pac2_attribs[i][0];
  479. ctx->cursor_font = pac2_attribs[i][1];
  480. SET_FLAG(screen->row_used, ctx->cursor_row);
  481. write_char(ctx, screen, ' ');
  482. }
  483. static void handle_pac(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
  484. {
  485. static const int8_t row_map[] = {
  486. 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
  487. };
  488. const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
  489. struct Screen *screen = get_writing_screen(ctx);
  490. int indent, i;
  491. if (row_map[index] <= 0) {
  492. av_log(ctx, AV_LOG_DEBUG, "Invalid pac index encountered\n");
  493. return;
  494. }
  495. lo &= 0x1f;
  496. ctx->cursor_row = row_map[index] - 1;
  497. ctx->cursor_color = pac2_attribs[lo][0];
  498. ctx->cursor_font = pac2_attribs[lo][1];
  499. ctx->cursor_charset = CCSET_BASIC_AMERICAN;
  500. ctx->cursor_column = 0;
  501. indent = pac2_attribs[lo][2];
  502. for (i = 0; i < indent; i++) {
  503. write_char(ctx, screen, ' ');
  504. }
  505. }
  506. /**
  507. * @param pts it is required to set end time
  508. */
  509. static void handle_edm(CCaptionSubContext *ctx, int64_t pts)
  510. {
  511. struct Screen *screen = ctx->screen + ctx->active_screen;
  512. // In buffered mode, keep writing to screen until it is wiped.
  513. // Before wiping the display, capture contents to emit subtitle.
  514. if (!ctx->real_time)
  515. reap_screen(ctx, pts);
  516. screen->row_used = 0;
  517. // In realtime mode, emit an empty caption so the last one doesn't
  518. // stay on the screen.
  519. if (ctx->real_time)
  520. reap_screen(ctx, pts);
  521. }
  522. static void handle_eoc(CCaptionSubContext *ctx, int64_t pts)
  523. {
  524. // In buffered mode, we wait til the *next* EOC and
  525. // reap what was already on the screen since the last EOC.
  526. if (!ctx->real_time)
  527. handle_edm(ctx,pts);
  528. ctx->active_screen = !ctx->active_screen;
  529. ctx->cursor_column = 0;
  530. // In realtime mode, we display the buffered contents (after
  531. // flipping the buffer to active above) as soon as EOC arrives.
  532. if (ctx->real_time)
  533. reap_screen(ctx, pts);
  534. }
  535. static void handle_delete_end_of_row(CCaptionSubContext *ctx, char hi, char lo)
  536. {
  537. struct Screen *screen = get_writing_screen(ctx);
  538. write_char(ctx, screen, 0);
  539. }
  540. static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
  541. {
  542. struct Screen *screen = get_writing_screen(ctx);
  543. SET_FLAG(screen->row_used, ctx->cursor_row);
  544. switch (hi) {
  545. case 0x11:
  546. ctx->cursor_charset = CCSET_SPECIAL_AMERICAN;
  547. break;
  548. case 0x12:
  549. if (ctx->cursor_column > 0)
  550. ctx->cursor_column -= 1;
  551. ctx->cursor_charset = CCSET_EXTENDED_SPANISH_FRENCH_MISC;
  552. break;
  553. case 0x13:
  554. if (ctx->cursor_column > 0)
  555. ctx->cursor_column -= 1;
  556. ctx->cursor_charset = CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH;
  557. break;
  558. default:
  559. ctx->cursor_charset = CCSET_BASIC_AMERICAN;
  560. write_char(ctx, screen, hi);
  561. break;
  562. }
  563. if (lo) {
  564. write_char(ctx, screen, lo);
  565. }
  566. write_char(ctx, screen, 0);
  567. if (ctx->mode != CCMODE_POPON)
  568. ctx->screen_touched = 1;
  569. if (lo)
  570. ff_dlog(ctx, "(%c,%c)\n", hi, lo);
  571. else
  572. ff_dlog(ctx, "(%c)\n", hi);
  573. }
  574. static void process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8_t lo)
  575. {
  576. if (hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) {
  577. /* ignore redundant command */
  578. return;
  579. }
  580. /* set prev command */
  581. ctx->prev_cmd[0] = hi;
  582. ctx->prev_cmd[1] = lo;
  583. if ( (hi == 0x10 && (lo >= 0x40 && lo <= 0x5f)) ||
  584. ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
  585. handle_pac(ctx, hi, lo);
  586. } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) ||
  587. ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) {
  588. handle_textattr(ctx, hi, lo);
  589. } else if (hi == 0x14 || hi == 0x15 || hi == 0x1c) {
  590. switch (lo) {
  591. case 0x20:
  592. /* resume caption loading */
  593. ctx->mode = CCMODE_POPON;
  594. break;
  595. case 0x24:
  596. handle_delete_end_of_row(ctx, hi, lo);
  597. break;
  598. case 0x25:
  599. case 0x26:
  600. case 0x27:
  601. ctx->rollup = lo - 0x23;
  602. ctx->mode = CCMODE_ROLLUP;
  603. break;
  604. case 0x29:
  605. /* resume direct captioning */
  606. ctx->mode = CCMODE_PAINTON;
  607. break;
  608. case 0x2b:
  609. /* resume text display */
  610. ctx->mode = CCMODE_TEXT;
  611. break;
  612. case 0x2c:
  613. /* erase display memory */
  614. handle_edm(ctx, pts);
  615. break;
  616. case 0x2d:
  617. /* carriage return */
  618. ff_dlog(ctx, "carriage return\n");
  619. if (!ctx->real_time)
  620. reap_screen(ctx, pts);
  621. roll_up(ctx);
  622. ctx->cursor_column = 0;
  623. break;
  624. case 0x2e:
  625. /* erase buffered (non displayed) memory */
  626. // Only in realtime mode. In buffered mode, we re-use the inactive screen
  627. // for our own buffering.
  628. if (ctx->real_time) {
  629. struct Screen *screen = ctx->screen + !ctx->active_screen;
  630. screen->row_used = 0;
  631. }
  632. break;
  633. case 0x2f:
  634. /* end of caption */
  635. ff_dlog(ctx, "handle_eoc\n");
  636. handle_eoc(ctx, pts);
  637. break;
  638. default:
  639. ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
  640. break;
  641. }
  642. } else if (hi >= 0x11 && hi <= 0x13) {
  643. /* Special characters */
  644. handle_char(ctx, hi, lo, pts);
  645. } else if (hi >= 0x20) {
  646. /* Standard characters (always in pairs) */
  647. handle_char(ctx, hi, lo, pts);
  648. ctx->prev_cmd[0] = ctx->prev_cmd[1] = 0;
  649. } else if (hi == 0x17 && lo >= 0x21 && lo <= 0x23) {
  650. int i;
  651. /* Tab offsets (spacing) */
  652. for (i = 0; i < lo - 0x20; i++) {
  653. handle_char(ctx, ' ', 0, pts);
  654. }
  655. } else {
  656. /* Ignoring all other non data code */
  657. ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
  658. }
  659. }
  660. static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
  661. {
  662. CCaptionSubContext *ctx = avctx->priv_data;
  663. AVSubtitle *sub = data;
  664. const int64_t start_time = sub->pts;
  665. uint8_t *bptr = NULL;
  666. int len = avpkt->size;
  667. int ret = 0;
  668. int i;
  669. if (ctx->pktbuf->size < len) {
  670. ret = av_buffer_realloc(&ctx->pktbuf, len);
  671. if (ret < 0) {
  672. av_log(ctx, AV_LOG_WARNING, "Insufficient Memory of %d truncated to %d\n", len, ctx->pktbuf->size);
  673. len = ctx->pktbuf->size;
  674. ret = 0;
  675. }
  676. }
  677. memcpy(ctx->pktbuf->data, avpkt->data, len);
  678. bptr = ctx->pktbuf->data;
  679. for (i = 0; i < len; i += 3) {
  680. uint8_t cc_type = *(bptr + i) & 3;
  681. if (validate_cc_data_pair(bptr + i))
  682. continue;
  683. /* ignoring data field 1 */
  684. if(cc_type == 1)
  685. continue;
  686. else
  687. process_cc608(ctx, start_time, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f);
  688. if (!ctx->buffer_changed)
  689. continue;
  690. ctx->buffer_changed = 0;
  691. if (*ctx->buffer.str || ctx->real_time)
  692. {
  693. ff_dlog(ctx, "cdp writing data (%s)\n",ctx->buffer.str);
  694. ret = ff_ass_add_rect(sub, ctx->buffer.str, ctx->readorder++, 0, NULL, NULL);
  695. if (ret < 0)
  696. return ret;
  697. sub->pts = ctx->start_time;
  698. if (!ctx->real_time)
  699. sub->end_display_time = av_rescale_q(ctx->end_time - ctx->start_time,
  700. AV_TIME_BASE_Q, ms_tb);
  701. else
  702. sub->end_display_time = -1;
  703. ctx->buffer_changed = 0;
  704. ctx->last_real_time = sub->pts;
  705. ctx->screen_touched = 0;
  706. }
  707. }
  708. if (ctx->real_time && ctx->screen_touched &&
  709. sub->pts > ctx->last_real_time + av_rescale_q(200, ms_tb, AV_TIME_BASE_Q)) {
  710. ctx->last_real_time = sub->pts;
  711. ctx->screen_touched = 0;
  712. capture_screen(ctx);
  713. ctx->buffer_changed = 0;
  714. ret = ff_ass_add_rect(sub, ctx->buffer.str, ctx->readorder++, 0, NULL, NULL);
  715. if (ret < 0)
  716. return ret;
  717. sub->end_display_time = -1;
  718. }
  719. *got_sub = sub->num_rects > 0;
  720. return ret;
  721. }
  722. #define OFFSET(x) offsetof(CCaptionSubContext, x)
  723. #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
  724. static const AVOption options[] = {
  725. { "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 },
  726. {NULL}
  727. };
  728. static const AVClass ccaption_dec_class = {
  729. .class_name = "Closed caption Decoder",
  730. .item_name = av_default_item_name,
  731. .option = options,
  732. .version = LIBAVUTIL_VERSION_INT,
  733. };
  734. AVCodec ff_ccaption_decoder = {
  735. .name = "cc_dec",
  736. .long_name = NULL_IF_CONFIG_SMALL("Closed Caption (EIA-608 / CEA-708) Decoder"),
  737. .type = AVMEDIA_TYPE_SUBTITLE,
  738. .id = AV_CODEC_ID_EIA_608,
  739. .priv_data_size = sizeof(CCaptionSubContext),
  740. .init = init_decoder,
  741. .close = close_decoder,
  742. .flush = flush_decoder,
  743. .decode = decode,
  744. .priv_class = &ccaption_dec_class,
  745. };