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.

671 lines
21KB

  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 ass_tb = {1, 100};
  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. static const unsigned char pac2_attribs[32][3] = // Color, font, ident
  59. {
  60. { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x40 || 0x60
  61. { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x41 || 0x61
  62. { CCCOL_GREEN, CCFONT_REGULAR, 0 }, // 0x42 || 0x62
  63. { CCCOL_GREEN, CCFONT_UNDERLINED, 0 }, // 0x43 || 0x63
  64. { CCCOL_BLUE, CCFONT_REGULAR, 0 }, // 0x44 || 0x64
  65. { CCCOL_BLUE, CCFONT_UNDERLINED, 0 }, // 0x45 || 0x65
  66. { CCCOL_CYAN, CCFONT_REGULAR, 0 }, // 0x46 || 0x66
  67. { CCCOL_CYAN, CCFONT_UNDERLINED, 0 }, // 0x47 || 0x67
  68. { CCCOL_RED, CCFONT_REGULAR, 0 }, // 0x48 || 0x68
  69. { CCCOL_RED, CCFONT_UNDERLINED, 0 }, // 0x49 || 0x69
  70. { CCCOL_YELLOW, CCFONT_REGULAR, 0 }, // 0x4a || 0x6a
  71. { CCCOL_YELLOW, CCFONT_UNDERLINED, 0 }, // 0x4b || 0x6b
  72. { CCCOL_MAGENTA, CCFONT_REGULAR, 0 }, // 0x4c || 0x6c
  73. { CCCOL_MAGENTA, CCFONT_UNDERLINED, 0 }, // 0x4d || 0x6d
  74. { CCCOL_WHITE, CCFONT_ITALICS, 0 }, // 0x4e || 0x6e
  75. { CCCOL_WHITE, CCFONT_UNDERLINED_ITALICS, 0 }, // 0x4f || 0x6f
  76. { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x50 || 0x70
  77. { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x51 || 0x71
  78. { CCCOL_WHITE, CCFONT_REGULAR, 4 }, // 0x52 || 0x72
  79. { CCCOL_WHITE, CCFONT_UNDERLINED, 4 }, // 0x53 || 0x73
  80. { CCCOL_WHITE, CCFONT_REGULAR, 8 }, // 0x54 || 0x74
  81. { CCCOL_WHITE, CCFONT_UNDERLINED, 8 }, // 0x55 || 0x75
  82. { CCCOL_WHITE, CCFONT_REGULAR, 12 }, // 0x56 || 0x76
  83. { CCCOL_WHITE, CCFONT_UNDERLINED, 12 }, // 0x57 || 0x77
  84. { CCCOL_WHITE, CCFONT_REGULAR, 16 }, // 0x58 || 0x78
  85. { CCCOL_WHITE, CCFONT_UNDERLINED, 16 }, // 0x59 || 0x79
  86. { CCCOL_WHITE, CCFONT_REGULAR, 20 }, // 0x5a || 0x7a
  87. { CCCOL_WHITE, CCFONT_UNDERLINED, 20 }, // 0x5b || 0x7b
  88. { CCCOL_WHITE, CCFONT_REGULAR, 24 }, // 0x5c || 0x7c
  89. { CCCOL_WHITE, CCFONT_UNDERLINED, 24 }, // 0x5d || 0x7d
  90. { CCCOL_WHITE, CCFONT_REGULAR, 28 }, // 0x5e || 0x7e
  91. { CCCOL_WHITE, CCFONT_UNDERLINED, 28 } // 0x5f || 0x7f
  92. /* total 32 entries */
  93. };
  94. struct Screen {
  95. /* +1 is used to compensate null character of string */
  96. uint8_t characters[SCREEN_ROWS][SCREEN_COLUMNS+1];
  97. uint8_t colors[SCREEN_ROWS][SCREEN_COLUMNS+1];
  98. uint8_t fonts[SCREEN_ROWS][SCREEN_COLUMNS+1];
  99. /*
  100. * Bitmask of used rows; if a bit is not set, the
  101. * corresponding row is not used.
  102. * for setting row 1 use row | (1 << 0)
  103. * for setting row 15 use row | (1 << 14)
  104. */
  105. int16_t row_used;
  106. };
  107. typedef struct CCaptionSubContext {
  108. AVClass *class;
  109. int real_time;
  110. struct Screen screen[2];
  111. int active_screen;
  112. uint8_t cursor_row;
  113. uint8_t cursor_column;
  114. uint8_t cursor_color;
  115. uint8_t cursor_font;
  116. AVBPrint buffer;
  117. int buffer_changed;
  118. int rollup;
  119. enum cc_mode mode;
  120. int64_t start_time;
  121. /* visible screen time */
  122. int64_t startv_time;
  123. int64_t end_time;
  124. int screen_touched;
  125. int64_t last_real_time;
  126. char prev_cmd[2];
  127. /* buffer to store pkt data */
  128. AVBufferRef *pktbuf;
  129. } CCaptionSubContext;
  130. static av_cold int init_decoder(AVCodecContext *avctx)
  131. {
  132. int ret;
  133. CCaptionSubContext *ctx = avctx->priv_data;
  134. av_bprint_init(&ctx->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
  135. /* taking by default roll up to 2 */
  136. ctx->mode = CCMODE_ROLLUP;
  137. ctx->rollup = 2;
  138. ret = ff_ass_subtitle_header(avctx, "Monospace",
  139. ASS_DEFAULT_FONT_SIZE,
  140. ASS_DEFAULT_COLOR,
  141. ASS_DEFAULT_BACK_COLOR,
  142. ASS_DEFAULT_BOLD,
  143. ASS_DEFAULT_ITALIC,
  144. ASS_DEFAULT_UNDERLINE,
  145. 3,
  146. ASS_DEFAULT_ALIGNMENT);
  147. if (ret < 0) {
  148. return ret;
  149. }
  150. /* allocate pkt buffer */
  151. ctx->pktbuf = av_buffer_alloc(128);
  152. if (!ctx->pktbuf) {
  153. ret = AVERROR(ENOMEM);
  154. }
  155. return ret;
  156. }
  157. static av_cold int close_decoder(AVCodecContext *avctx)
  158. {
  159. CCaptionSubContext *ctx = avctx->priv_data;
  160. av_bprint_finalize(&ctx->buffer, NULL);
  161. av_buffer_unref(&ctx->pktbuf);
  162. return 0;
  163. }
  164. static void flush_decoder(AVCodecContext *avctx)
  165. {
  166. CCaptionSubContext *ctx = avctx->priv_data;
  167. ctx->screen[0].row_used = 0;
  168. ctx->screen[1].row_used = 0;
  169. ctx->prev_cmd[0] = 0;
  170. ctx->prev_cmd[1] = 0;
  171. ctx->mode = CCMODE_ROLLUP;
  172. ctx->rollup = 2;
  173. ctx->cursor_row = 0;
  174. ctx->cursor_column = 0;
  175. ctx->cursor_font = 0;
  176. ctx->cursor_color = 0;
  177. ctx->active_screen = 0;
  178. ctx->last_real_time = 0;
  179. ctx->screen_touched = 0;
  180. ctx->buffer_changed = 0;
  181. av_bprint_clear(&ctx->buffer);
  182. }
  183. /**
  184. * @param ctx closed caption context just to print log
  185. */
  186. static int write_char(CCaptionSubContext *ctx, struct Screen *screen, char ch)
  187. {
  188. uint8_t col = ctx->cursor_column;
  189. char *row = screen->characters[ctx->cursor_row];
  190. char *font = screen->fonts[ctx->cursor_row];
  191. if (col < SCREEN_COLUMNS) {
  192. row[col] = ch;
  193. font[col] = ctx->cursor_font;
  194. if (ch) ctx->cursor_column++;
  195. return 0;
  196. }
  197. /* We have extra space at end only for null character */
  198. else if (col == SCREEN_COLUMNS && ch == 0) {
  199. row[col] = ch;
  200. return 0;
  201. }
  202. else {
  203. av_log(ctx, AV_LOG_WARNING, "Data Ignored since exceeding screen width\n");
  204. return AVERROR_INVALIDDATA;
  205. }
  206. }
  207. /**
  208. * This function after validating parity bit, also remove it from data pair.
  209. * The first byte doesn't pass parity, we replace it with a solid blank
  210. * and process the pair.
  211. * If the second byte doesn't pass parity, it returns INVALIDDATA
  212. * user can ignore the whole pair and pass the other pair.
  213. */
  214. static int validate_cc_data_pair(uint8_t *cc_data_pair)
  215. {
  216. uint8_t cc_valid = (*cc_data_pair & 4) >>2;
  217. uint8_t cc_type = *cc_data_pair & 3;
  218. if (!cc_valid)
  219. return AVERROR_INVALIDDATA;
  220. // if EIA-608 data then verify parity.
  221. if (cc_type==0 || cc_type==1) {
  222. if (!av_parity(cc_data_pair[2])) {
  223. return AVERROR_INVALIDDATA;
  224. }
  225. if (!av_parity(cc_data_pair[1])) {
  226. cc_data_pair[1]=0x7F;
  227. }
  228. }
  229. //Skip non-data
  230. if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD)
  231. && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0)
  232. return AVERROR_PATCHWELCOME;
  233. //skip 708 data
  234. if (cc_type == 3 || cc_type == 2)
  235. return AVERROR_PATCHWELCOME;
  236. /* remove parity bit */
  237. cc_data_pair[1] &= 0x7F;
  238. cc_data_pair[2] &= 0x7F;
  239. return 0;
  240. }
  241. static struct Screen *get_writing_screen(CCaptionSubContext *ctx)
  242. {
  243. switch (ctx->mode) {
  244. case CCMODE_POPON:
  245. // use Inactive screen
  246. return ctx->screen + !ctx->active_screen;
  247. case CCMODE_PAINTON:
  248. case CCMODE_ROLLUP:
  249. case CCMODE_TEXT:
  250. // use active screen
  251. return ctx->screen + ctx->active_screen;
  252. }
  253. /* It was never an option */
  254. return NULL;
  255. }
  256. static void roll_up(CCaptionSubContext *ctx)
  257. {
  258. struct Screen *screen;
  259. int i, keep_lines;
  260. if (ctx->mode == CCMODE_TEXT)
  261. return;
  262. screen = get_writing_screen(ctx);
  263. /* +1 signify cursor_row starts from 0
  264. * Can't keep lines less then row cursor pos
  265. */
  266. keep_lines = FFMIN(ctx->cursor_row + 1, ctx->rollup);
  267. for (i = 0; i < SCREEN_ROWS; i++) {
  268. if (i > ctx->cursor_row - keep_lines && i <= ctx->cursor_row)
  269. continue;
  270. UNSET_FLAG(screen->row_used, i);
  271. }
  272. for (i = 0; i < keep_lines && screen->row_used; i++) {
  273. const int i_row = ctx->cursor_row - keep_lines + i + 1;
  274. memcpy(screen->characters[i_row], screen->characters[i_row+1], SCREEN_COLUMNS);
  275. memcpy(screen->colors[i_row], screen->colors[i_row+1], SCREEN_COLUMNS);
  276. memcpy(screen->fonts[i_row], screen->fonts[i_row+1], SCREEN_COLUMNS);
  277. if (CHECK_FLAG(screen->row_used, i_row + 1))
  278. SET_FLAG(screen->row_used, i_row);
  279. }
  280. UNSET_FLAG(screen->row_used, ctx->cursor_row);
  281. }
  282. static int capture_screen(CCaptionSubContext *ctx)
  283. {
  284. int i;
  285. struct Screen *screen = ctx->screen + ctx->active_screen;
  286. enum cc_font prev_font = CCFONT_REGULAR;
  287. av_bprint_clear(&ctx->buffer);
  288. for (i = 0; screen->row_used && i < SCREEN_ROWS; i++)
  289. {
  290. if (CHECK_FLAG(screen->row_used, i)) {
  291. const char *row = screen->characters[i];
  292. const char *font = screen->fonts[i];
  293. int j = 0;
  294. /* skip leading space */
  295. while (row[j] == ' ')
  296. j++;
  297. for (; j < SCREEN_COLUMNS; j++) {
  298. const char *e_tag = "", *s_tag = "";
  299. if (row[j] == 0)
  300. break;
  301. if (prev_font != font[j]) {
  302. switch (prev_font) {
  303. case CCFONT_ITALICS:
  304. e_tag = "{\\i0}";
  305. break;
  306. case CCFONT_UNDERLINED:
  307. e_tag = "{\\u0}";
  308. break;
  309. case CCFONT_UNDERLINED_ITALICS:
  310. e_tag = "{\\u0}{\\i0}";
  311. break;
  312. }
  313. switch (font[j]) {
  314. case CCFONT_ITALICS:
  315. s_tag = "{\\i1}";
  316. break;
  317. case CCFONT_UNDERLINED:
  318. s_tag = "{\\u1}";
  319. break;
  320. case CCFONT_UNDERLINED_ITALICS:
  321. s_tag = "{\\u1}{\\i1}";
  322. break;
  323. }
  324. }
  325. prev_font = font[j];
  326. av_bprintf(&ctx->buffer, "%s%s%c", e_tag, s_tag, row[j]);
  327. }
  328. av_bprintf(&ctx->buffer, "\\N");
  329. }
  330. }
  331. if (!av_bprint_is_complete(&ctx->buffer))
  332. return AVERROR(ENOMEM);
  333. if (screen->row_used && ctx->buffer.len >= 2) {
  334. ctx->buffer.len -= 2;
  335. ctx->buffer.str[ctx->buffer.len] = 0;
  336. }
  337. ctx->buffer_changed = 1;
  338. return 0;
  339. }
  340. static int reap_screen(CCaptionSubContext *ctx, int64_t pts)
  341. {
  342. ctx->start_time = ctx->startv_time;
  343. ctx->startv_time = pts;
  344. ctx->end_time = pts;
  345. return capture_screen(ctx);
  346. }
  347. static void handle_textattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
  348. {
  349. int i = lo - 0x20;
  350. struct Screen *screen = get_writing_screen(ctx);
  351. if (i >= 32)
  352. return;
  353. ctx->cursor_color = pac2_attribs[i][0];
  354. ctx->cursor_font = pac2_attribs[i][1];
  355. SET_FLAG(screen->row_used, ctx->cursor_row);
  356. write_char(ctx, screen, ' ');
  357. }
  358. static void handle_pac(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
  359. {
  360. static const int8_t row_map[] = {
  361. 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
  362. };
  363. const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
  364. struct Screen *screen = get_writing_screen(ctx);
  365. int indent, i;
  366. if (row_map[index] <= 0) {
  367. av_log(ctx, AV_LOG_DEBUG, "Invalid pac index encountered\n");
  368. return;
  369. }
  370. lo &= 0x1f;
  371. ctx->cursor_row = row_map[index] - 1;
  372. ctx->cursor_color = pac2_attribs[lo][0];
  373. ctx->cursor_font = pac2_attribs[lo][1];
  374. ctx->cursor_column = 0;
  375. indent = pac2_attribs[lo][2];
  376. for (i = 0; i < indent; i++) {
  377. write_char(ctx, screen, ' ');
  378. }
  379. }
  380. /**
  381. * @param pts it is required to set end time
  382. */
  383. static void handle_edm(CCaptionSubContext *ctx, int64_t pts)
  384. {
  385. struct Screen *screen = ctx->screen + ctx->active_screen;
  386. // In buffered mode, keep writing to screen until it is wiped.
  387. // Before wiping the display, capture contents to emit subtitle.
  388. if (!ctx->real_time)
  389. reap_screen(ctx, pts);
  390. screen->row_used = 0;
  391. // In realtime mode, emit an empty caption so the last one doesn't
  392. // stay on the screen.
  393. if (ctx->real_time)
  394. reap_screen(ctx, pts);
  395. }
  396. static void handle_eoc(CCaptionSubContext *ctx, int64_t pts)
  397. {
  398. // In buffered mode, we wait til the *next* EOC and
  399. // reap what was already on the screen since the last EOC.
  400. if (!ctx->real_time)
  401. handle_edm(ctx,pts);
  402. ctx->active_screen = !ctx->active_screen;
  403. ctx->cursor_column = 0;
  404. // In realtime mode, we display the buffered contents (after
  405. // flipping the buffer to active above) as soon as EOC arrives.
  406. if (ctx->real_time)
  407. reap_screen(ctx, pts);
  408. }
  409. static void handle_delete_end_of_row(CCaptionSubContext *ctx, char hi, char lo)
  410. {
  411. struct Screen *screen = get_writing_screen(ctx);
  412. write_char(ctx, screen, 0);
  413. }
  414. static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
  415. {
  416. struct Screen *screen = get_writing_screen(ctx);
  417. SET_FLAG(screen->row_used, ctx->cursor_row);
  418. write_char(ctx, screen, hi);
  419. if (lo) {
  420. write_char(ctx, screen, lo);
  421. }
  422. write_char(ctx, screen, 0);
  423. if (ctx->mode != CCMODE_POPON)
  424. ctx->screen_touched = 1;
  425. /* reset prev command since character can repeat */
  426. ctx->prev_cmd[0] = 0;
  427. ctx->prev_cmd[1] = 0;
  428. if (lo)
  429. ff_dlog(ctx, "(%c,%c)\n", hi, lo);
  430. else
  431. ff_dlog(ctx, "(%c)\n", hi);
  432. }
  433. static void process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8_t lo)
  434. {
  435. if (hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) {
  436. /* ignore redundant command */
  437. } else if ( (hi == 0x10 && (lo >= 0x40 && lo <= 0x5f)) ||
  438. ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
  439. handle_pac(ctx, hi, lo);
  440. } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) ||
  441. ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) {
  442. handle_textattr(ctx, hi, lo);
  443. } else if (hi == 0x14 || hi == 0x15 || hi == 0x1c) {
  444. switch (lo) {
  445. case 0x20:
  446. /* resume caption loading */
  447. ctx->mode = CCMODE_POPON;
  448. break;
  449. case 0x24:
  450. handle_delete_end_of_row(ctx, hi, lo);
  451. break;
  452. case 0x25:
  453. case 0x26:
  454. case 0x27:
  455. ctx->rollup = lo - 0x23;
  456. ctx->mode = CCMODE_ROLLUP;
  457. break;
  458. case 0x29:
  459. /* resume direct captioning */
  460. ctx->mode = CCMODE_PAINTON;
  461. break;
  462. case 0x2b:
  463. /* resume text display */
  464. ctx->mode = CCMODE_TEXT;
  465. break;
  466. case 0x2c:
  467. /* erase display memory */
  468. handle_edm(ctx, pts);
  469. break;
  470. case 0x2d:
  471. /* carriage return */
  472. ff_dlog(ctx, "carriage return\n");
  473. if (!ctx->real_time)
  474. reap_screen(ctx, pts);
  475. roll_up(ctx);
  476. ctx->cursor_column = 0;
  477. break;
  478. case 0x2e:
  479. /* erase buffered (non displayed) memory */
  480. // Only in realtime mode. In buffered mode, we re-use the inactive screen
  481. // for our own buffering.
  482. if (ctx->real_time) {
  483. struct Screen *screen = ctx->screen + !ctx->active_screen;
  484. screen->row_used = 0;
  485. }
  486. break;
  487. case 0x2f:
  488. /* end of caption */
  489. ff_dlog(ctx, "handle_eoc\n");
  490. handle_eoc(ctx, pts);
  491. break;
  492. default:
  493. ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
  494. break;
  495. }
  496. } else if (hi >= 0x20) {
  497. /* Standard characters (always in pairs) */
  498. handle_char(ctx, hi, lo, pts);
  499. } else {
  500. /* Ignoring all other non data code */
  501. ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
  502. }
  503. /* set prev command */
  504. ctx->prev_cmd[0] = hi;
  505. ctx->prev_cmd[1] = lo;
  506. }
  507. static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
  508. {
  509. CCaptionSubContext *ctx = avctx->priv_data;
  510. AVSubtitle *sub = data;
  511. uint8_t *bptr = NULL;
  512. int len = avpkt->size;
  513. int ret = 0;
  514. int i;
  515. if (ctx->pktbuf->size < len) {
  516. ret = av_buffer_realloc(&ctx->pktbuf, len);
  517. if (ret < 0) {
  518. av_log(ctx, AV_LOG_WARNING, "Insufficient Memory of %d truncated to %d\n", len, ctx->pktbuf->size);
  519. len = ctx->pktbuf->size;
  520. ret = 0;
  521. }
  522. }
  523. memcpy(ctx->pktbuf->data, avpkt->data, len);
  524. bptr = ctx->pktbuf->data;
  525. for (i = 0; i < len; i += 3) {
  526. uint8_t cc_type = *(bptr + i) & 3;
  527. if (validate_cc_data_pair(bptr + i))
  528. continue;
  529. /* ignoring data field 1 */
  530. if(cc_type == 1)
  531. continue;
  532. else
  533. process_cc608(ctx, avpkt->pts, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f);
  534. if (!ctx->buffer_changed)
  535. continue;
  536. ctx->buffer_changed = 0;
  537. if (*ctx->buffer.str || ctx->real_time)
  538. {
  539. int64_t sub_pts = ctx->real_time ? avpkt->pts : ctx->start_time;
  540. int start_time = av_rescale_q(sub_pts, avctx->time_base, ass_tb);
  541. int duration = -1;
  542. if (!ctx->real_time) {
  543. int end_time = av_rescale_q(ctx->end_time, avctx->time_base, ass_tb);
  544. duration = end_time - start_time;
  545. }
  546. ff_dlog(ctx, "cdp writing data (%s)\n",ctx->buffer.str);
  547. ret = ff_ass_add_rect_bprint(sub, &ctx->buffer, start_time, duration);
  548. if (ret < 0)
  549. return ret;
  550. sub->pts = av_rescale_q(sub_pts, avctx->time_base, AV_TIME_BASE_Q);
  551. ctx->buffer_changed = 0;
  552. ctx->last_real_time = avpkt->pts;
  553. ctx->screen_touched = 0;
  554. }
  555. }
  556. if (ctx->real_time && ctx->screen_touched &&
  557. avpkt->pts > ctx->last_real_time + av_rescale_q(20, ass_tb, avctx->time_base)) {
  558. ctx->last_real_time = avpkt->pts;
  559. ctx->screen_touched = 0;
  560. capture_screen(ctx);
  561. ctx->buffer_changed = 0;
  562. int start_time = av_rescale_q(avpkt->pts, avctx->time_base, ass_tb);
  563. ret = ff_ass_add_rect_bprint(sub, &ctx->buffer, start_time, -1);
  564. if (ret < 0)
  565. return ret;
  566. sub->pts = av_rescale_q(avpkt->pts, avctx->time_base, AV_TIME_BASE_Q);
  567. }
  568. *got_sub = sub->num_rects > 0;
  569. return ret;
  570. }
  571. #define OFFSET(x) offsetof(CCaptionSubContext, x)
  572. #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
  573. static const AVOption options[] = {
  574. { "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 },
  575. {NULL}
  576. };
  577. static const AVClass ccaption_dec_class = {
  578. .class_name = "Closed caption Decoder",
  579. .item_name = av_default_item_name,
  580. .option = options,
  581. .version = LIBAVUTIL_VERSION_INT,
  582. };
  583. AVCodec ff_ccaption_decoder = {
  584. .name = "cc_dec",
  585. .long_name = NULL_IF_CONFIG_SMALL("Closed Caption (EIA-608 / CEA-708) Decoder"),
  586. .type = AVMEDIA_TYPE_SUBTITLE,
  587. .id = AV_CODEC_ID_EIA_608,
  588. .priv_data_size = sizeof(CCaptionSubContext),
  589. .init = init_decoder,
  590. .close = close_decoder,
  591. .flush = flush_decoder,
  592. .decode = decode,
  593. .priv_class = &ccaption_dec_class,
  594. };