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.

588 lines
19KB

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