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.

530 lines
17KB

  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 CHAR_DEBUG
  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. {
  44. CCCOL_WHITE,
  45. CCCOL_GREEN,
  46. CCCOL_BLUE,
  47. CCCOL_CYAN,
  48. CCCOL_RED,
  49. CCCOL_YELLOW,
  50. CCCOL_MAGENTA,
  51. CCCOL_USERDEFINED,
  52. CCCOL_BLACK,
  53. CCCOL_TRANSPARENT
  54. };
  55. enum cc_font
  56. {
  57. CCFONT_REGULAR,
  58. CCFONT_ITALICS,
  59. CCFONT_UNDERLINED,
  60. CCFONT_UNDERLINED_ITALICS
  61. };
  62. static const unsigned char pac2_attribs[][3] = // Color, font, ident
  63. {
  64. { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x40 || 0x60
  65. { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x41 || 0x61
  66. { CCCOL_GREEN, CCFONT_REGULAR, 0 }, // 0x42 || 0x62
  67. { CCCOL_GREEN, CCFONT_UNDERLINED, 0 }, // 0x43 || 0x63
  68. { CCCOL_BLUE, CCFONT_REGULAR, 0 }, // 0x44 || 0x64
  69. { CCCOL_BLUE, CCFONT_UNDERLINED, 0 }, // 0x45 || 0x65
  70. { CCCOL_CYAN, CCFONT_REGULAR, 0 }, // 0x46 || 0x66
  71. { CCCOL_CYAN, CCFONT_UNDERLINED, 0 }, // 0x47 || 0x67
  72. { CCCOL_RED, CCFONT_REGULAR, 0 }, // 0x48 || 0x68
  73. { CCCOL_RED, CCFONT_UNDERLINED, 0 }, // 0x49 || 0x69
  74. { CCCOL_YELLOW, CCFONT_REGULAR, 0 }, // 0x4a || 0x6a
  75. { CCCOL_YELLOW, CCFONT_UNDERLINED, 0 }, // 0x4b || 0x6b
  76. { CCCOL_MAGENTA, CCFONT_REGULAR, 0 }, // 0x4c || 0x6c
  77. { CCCOL_MAGENTA, CCFONT_UNDERLINED, 0 }, // 0x4d || 0x6d
  78. { CCCOL_WHITE, CCFONT_ITALICS, 0 }, // 0x4e || 0x6e
  79. { CCCOL_WHITE, CCFONT_UNDERLINED_ITALICS, 0 }, // 0x4f || 0x6f
  80. { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x50 || 0x70
  81. { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x51 || 0x71
  82. { CCCOL_WHITE, CCFONT_REGULAR, 4 }, // 0x52 || 0x72
  83. { CCCOL_WHITE, CCFONT_UNDERLINED, 4 }, // 0x53 || 0x73
  84. { CCCOL_WHITE, CCFONT_REGULAR, 8 }, // 0x54 || 0x74
  85. { CCCOL_WHITE, CCFONT_UNDERLINED, 8 }, // 0x55 || 0x75
  86. { CCCOL_WHITE, CCFONT_REGULAR, 12 }, // 0x56 || 0x76
  87. { CCCOL_WHITE, CCFONT_UNDERLINED, 12 }, // 0x57 || 0x77
  88. { CCCOL_WHITE, CCFONT_REGULAR, 16 }, // 0x58 || 0x78
  89. { CCCOL_WHITE, CCFONT_UNDERLINED, 16 }, // 0x59 || 0x79
  90. { CCCOL_WHITE, CCFONT_REGULAR, 20 }, // 0x5a || 0x7a
  91. { CCCOL_WHITE, CCFONT_UNDERLINED, 20 }, // 0x5b || 0x7b
  92. { CCCOL_WHITE, CCFONT_REGULAR, 24 }, // 0x5c || 0x7c
  93. { CCCOL_WHITE, CCFONT_UNDERLINED, 24 }, // 0x5d || 0x7d
  94. { CCCOL_WHITE, CCFONT_REGULAR, 28 }, // 0x5e || 0x7e
  95. { CCCOL_WHITE, CCFONT_UNDERLINED, 28 } // 0x5f || 0x7f
  96. /* total 32 entry */
  97. };
  98. /* 0-255 needs 256 space */
  99. static const uint8_t parity_table[256] = { 0, 1, 1, 0, 1, 0, 0, 1,
  100. 1, 0, 0, 1, 0, 1, 1, 0,
  101. 1, 0, 0, 1, 0, 1, 1, 0,
  102. 0, 1, 1, 0, 1, 0, 0, 1,
  103. 1, 0, 0, 1, 0, 1, 1, 0,
  104. 0, 1, 1, 0, 1, 0, 0, 1,
  105. 0, 1, 1, 0, 1, 0, 0, 1,
  106. 1, 0, 0, 1, 0, 1, 1, 0,
  107. 1, 0, 0, 1, 0, 1, 1, 0,
  108. 0, 1, 1, 0, 1, 0, 0, 1,
  109. 0, 1, 1, 0, 1, 0, 0, 1,
  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. 1, 0, 0, 1, 0, 1, 1, 0,
  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. 0, 1, 1, 0, 1, 0, 0, 1,
  118. 1, 0, 0, 1, 0, 1, 1, 0,
  119. 0, 1, 1, 0, 1, 0, 0, 1,
  120. 1, 0, 0, 1, 0, 1, 1, 0,
  121. 1, 0, 0, 1, 0, 1, 1, 0,
  122. 0, 1, 1, 0, 1, 0, 0, 1,
  123. 0, 1, 1, 0, 1, 0, 0, 1,
  124. 1, 0, 0, 1, 0, 1, 1, 0,
  125. 1, 0, 0, 1, 0, 1, 1, 0,
  126. 0, 1, 1, 0, 1, 0, 0, 1,
  127. 1, 0, 0, 1, 0, 1, 1, 0,
  128. 0, 1, 1, 0, 1, 0, 0, 1,
  129. 0, 1, 1, 0, 1, 0, 0, 1,
  130. 1, 0, 0, 1, 0, 1, 1, 0 };
  131. struct Screen {
  132. /* +1 is used to compensate null character of string */
  133. uint8_t characters[SCREEN_ROWS][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 | (0 << 1)
  138. * for setting row 15 use row | (1 << 14)
  139. */
  140. int16_t row_used;
  141. };
  142. typedef struct CCaptionSubContext {
  143. AVClass *class;
  144. int row_cnt;
  145. struct Screen screen[2];
  146. int active_screen;
  147. uint8_t cursor_row;
  148. uint8_t cursor_column;
  149. uint8_t cursor_color;
  150. uint8_t cursor_font;
  151. AVBPrint buffer;
  152. int erase_display_memory;
  153. int rollup;
  154. enum cc_mode mode;
  155. int64_t start_time;
  156. /* visible screen time */
  157. int64_t startv_time;
  158. int64_t end_time;
  159. char prev_cmd[2];
  160. /* buffer to store pkt data */
  161. AVBufferRef *pktbuf;
  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->rollup = 2;
  170. ret = ff_ass_subtitle_header_default(avctx);
  171. /* allocate pkt buffer */
  172. ctx->pktbuf = av_buffer_alloc(128);
  173. if( !ctx->pktbuf) {
  174. ret = AVERROR(ENOMEM);
  175. }
  176. return ret;
  177. }
  178. static av_cold int close_decoder(AVCodecContext *avctx)
  179. {
  180. CCaptionSubContext *ctx = avctx->priv_data;
  181. av_bprint_finalize( &ctx->buffer, NULL);
  182. av_buffer_unref(&ctx->pktbuf);
  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 exciding 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 handle_textattr( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo )
  256. {
  257. int i = lo - 0x20;
  258. int ret;
  259. struct Screen *screen = get_writing_screen(ctx);
  260. char *row = screen->characters[ctx->cursor_row];
  261. if( i >= 32)
  262. return;
  263. ctx->cursor_color = pac2_attribs[i][0];
  264. ctx->cursor_font = pac2_attribs[i][1];
  265. SET_FLAG(screen->row_used,ctx->cursor_row);
  266. ret = write_char(ctx, row, ctx->cursor_column, ' ');
  267. if(ret == 0)
  268. ctx->cursor_column++;
  269. }
  270. static void handle_pac( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo )
  271. {
  272. static const int8_t row_map[] = {
  273. 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
  274. };
  275. const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
  276. struct Screen *screen = get_writing_screen(ctx);
  277. char *row;
  278. int indent,i,ret;
  279. if( row_map[index] <= 0 )
  280. return;
  281. lo &= 0x1f;
  282. ctx->cursor_row = row_map[index] - 1;
  283. ctx->cursor_color = pac2_attribs[lo][0];
  284. ctx->cursor_font = pac2_attribs[lo][1];
  285. ctx->cursor_column = 0;
  286. indent = pac2_attribs[lo][2];
  287. row = screen->characters[ctx->cursor_row];
  288. for(i = 0;i < indent; i++) {
  289. ret = write_char(ctx, row, ctx->cursor_column, ' ');
  290. if( ret == 0 )
  291. ctx->cursor_column++;
  292. }
  293. }
  294. /**
  295. * @param pts it is required to set end time
  296. */
  297. static int handle_edm(CCaptionSubContext *ctx,int64_t pts)
  298. {
  299. int i;
  300. int ret = 0;
  301. struct Screen *screen = ctx->screen + ctx->active_screen;
  302. ctx->start_time = ctx->startv_time;
  303. for( i = 0; screen->row_used && i < SCREEN_ROWS; i++)
  304. {
  305. if(CHECK_FLAG(screen->row_used,i)) {
  306. char *str = screen->characters[i];
  307. /* skip space */
  308. while (*str == ' ')
  309. str++;
  310. av_bprint_append_data(&ctx->buffer, str, strlen(str));
  311. av_bprint_append_data(&ctx->buffer, "\\N",2);
  312. UNSET_FLAG(screen->row_used, i);
  313. ret = av_bprint_is_complete(&ctx->buffer);
  314. if( ret == 0) {
  315. ret = AVERROR(ENOMEM);
  316. break;
  317. }
  318. }
  319. }
  320. ctx->startv_time = pts;
  321. ctx->erase_display_memory = 1;
  322. ctx->end_time = pts;
  323. return ret;
  324. }
  325. static int handle_eoc(CCaptionSubContext *ctx, int64_t pts)
  326. {
  327. int ret;
  328. ret = handle_edm(ctx,pts);
  329. ctx->active_screen = !ctx->active_screen;
  330. ctx->cursor_column = 0;
  331. return ret;
  332. }
  333. static void handle_delete_end_of_row( CCaptionSubContext *ctx, char hi, char lo)
  334. {
  335. struct Screen *screen = get_writing_screen(ctx);
  336. char *row = screen->characters[ctx->cursor_row];
  337. write_char(ctx, row, ctx->cursor_column, 0);
  338. }
  339. static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
  340. {
  341. struct Screen *screen = get_writing_screen(ctx);
  342. char *row = screen->characters[ctx->cursor_row];
  343. int ret;
  344. SET_FLAG(screen->row_used,ctx->cursor_row);
  345. ret = write_char(ctx, row, ctx->cursor_column, hi);
  346. if( ret == 0 )
  347. ctx->cursor_column++;
  348. if(lo) {
  349. ret = write_char(ctx, row, ctx->cursor_column, lo);
  350. if ( ret == 0 )
  351. ctx->cursor_column++;
  352. }
  353. write_char(ctx, row, ctx->cursor_column, 0);
  354. /* reset prev command since character can repeat */
  355. ctx->prev_cmd[0] = 0;
  356. ctx->prev_cmd[1] = 0;
  357. #ifdef CHAR_DEBUG
  358. av_log(ctx, AV_LOG_DEBUG,"(%c,%c)\n",hi,lo);
  359. #endif
  360. }
  361. static int process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8_t lo)
  362. {
  363. int ret = 0;
  364. #define COR3(var, with1, with2, with3) ( (var) == (with1) || (var) == (with2) || (var) == (with3) )
  365. if ( hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) {
  366. /* ignore redundant command */
  367. } else if ( (hi == 0x10 && (lo >= 0x40 || lo <= 0x5f)) ||
  368. ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
  369. handle_pac(ctx, hi, lo);
  370. } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) ||
  371. ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) {
  372. handle_textattr(ctx, hi, lo);
  373. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x20 ) {
  374. /* resume caption loading */
  375. ctx->mode = CCMODE_POPON;
  376. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x24 ) {
  377. handle_delete_end_of_row(ctx, hi, lo);
  378. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x25 ) {
  379. ctx->rollup = 2;
  380. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x26 ) {
  381. ctx->rollup = 3;
  382. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x27 ) {
  383. ctx->rollup = 4;
  384. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x29 ) {
  385. /* resume direct captioning */
  386. ctx->mode = CCMODE_PAINTON;
  387. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2B ) {
  388. /* resume text display */
  389. ctx->mode = CCMODE_TEXT;
  390. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2C ) {
  391. /* erase display memory */
  392. ret = handle_edm(ctx, pts);
  393. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2D ) {
  394. /* carriage return */
  395. ctx->row_cnt++;
  396. if(ctx->row_cnt >= ctx->rollup) {
  397. ctx->row_cnt = 0;
  398. ret = handle_edm(ctx, pts);
  399. ctx->active_screen = !ctx->active_screen;
  400. }
  401. ctx->cursor_column = 0;
  402. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2F ) {
  403. /* end of caption */
  404. ret = handle_eoc(ctx, pts);
  405. } else if (hi>=0x20) {
  406. /* Standard characters (always in pairs) */
  407. handle_char(ctx, hi, lo, pts);
  408. } else {
  409. /* Ignoring all other non data code */
  410. }
  411. /* set prev command */
  412. ctx->prev_cmd[0] = hi;
  413. ctx->prev_cmd[1] = lo;
  414. #undef COR3
  415. return ret;
  416. }
  417. static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
  418. {
  419. CCaptionSubContext *ctx = avctx->priv_data;
  420. AVSubtitle *sub = data;
  421. uint8_t *bptr = NULL;
  422. int len = avpkt->size;
  423. int ret = 0;
  424. int i;
  425. if ( ctx->pktbuf->size < len) {
  426. ret = av_buffer_realloc(&ctx->pktbuf, len);
  427. if(ret < 0) {
  428. av_log(ctx, AV_LOG_WARNING, "Insufficient Memory of %d truncated to %d\n",len, ctx->pktbuf->size);
  429. len = ctx->pktbuf->size;
  430. ret = 0;
  431. }
  432. }
  433. memcpy(ctx->pktbuf->data, avpkt->data, len);
  434. bptr = ctx->pktbuf->data;
  435. for (i = 0; i < len; i += 3) {
  436. uint8_t cc_type = *(bptr + i) & 3;
  437. if (validate_cc_data_pair( bptr + i) )
  438. continue;
  439. /* ignoring data field 1 */
  440. if(cc_type == 1)
  441. continue;
  442. else
  443. process_cc608(ctx, avpkt->pts, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f);
  444. if(ctx->erase_display_memory && *ctx->buffer.str)
  445. {
  446. int start_time = av_rescale_q(ctx->start_time, avctx->time_base, (AVRational){ 1, 100 });
  447. int end_time = av_rescale_q(ctx->end_time, avctx->time_base, (AVRational){ 1, 100 });
  448. #ifdef CHAR_DEBUG
  449. av_log(ctx, AV_LOG_DEBUG,"cdp writing data (%s)\n",ctx->buffer.str);
  450. #endif
  451. ret = ff_ass_add_rect(sub, ctx->buffer.str, start_time, end_time - start_time , 0);
  452. if (ret < 0)
  453. return ret;
  454. sub->pts = av_rescale_q(ctx->start_time, avctx->time_base, AV_TIME_BASE_Q);
  455. ctx->erase_display_memory = 0;
  456. av_bprint_clear(&ctx->buffer);
  457. }
  458. }
  459. *got_sub = sub->num_rects > 0;
  460. return ret;
  461. }
  462. static const AVOption options[] = {
  463. {NULL}
  464. };
  465. static const AVClass ccaption_dec_class = {
  466. .class_name = "Closed caption Decoder",
  467. .item_name = av_default_item_name,
  468. .option = options,
  469. .version = LIBAVUTIL_VERSION_INT,
  470. };
  471. AVCodec ff_ccaption_decoder = {
  472. .name = "cc_dec",
  473. .long_name = NULL_IF_CONFIG_SMALL("Closed Caption (EIA-608 / CEA-708) Decoder"),
  474. .type = AVMEDIA_TYPE_SUBTITLE,
  475. .id = AV_CODEC_ID_EIA_608,
  476. .priv_data_size = sizeof(CCaptionSubContext),
  477. .init = init_decoder,
  478. .close = close_decoder,
  479. .decode = decode,
  480. .priv_class = &ccaption_dec_class,
  481. };