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.

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