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.

540 lines
18KB

  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. 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[][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][SCREEN_COLUMNS+1];
  132. /*
  133. * Bitmask of used rows; if a bit is not set, the
  134. * corresponding row is not used.
  135. * for setting row 1 use row | (1 << 0)
  136. * for setting row 15 use row | (1 << 14)
  137. */
  138. int16_t row_used;
  139. };
  140. typedef struct CCaptionSubContext {
  141. AVClass *class;
  142. int row_cnt;
  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 erase_display_memory;
  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->rollup = 2;
  168. ret = ff_ass_subtitle_header_default(avctx);
  169. /* allocate pkt buffer */
  170. ctx->pktbuf = av_buffer_alloc(128);
  171. if( !ctx->pktbuf) {
  172. ret = AVERROR(ENOMEM);
  173. }
  174. return ret;
  175. }
  176. static av_cold int close_decoder(AVCodecContext *avctx)
  177. {
  178. CCaptionSubContext *ctx = avctx->priv_data;
  179. av_bprint_finalize( &ctx->buffer, NULL);
  180. av_buffer_unref(&ctx->pktbuf);
  181. return 0;
  182. }
  183. /**
  184. * @param ctx closed caption context just to print log
  185. */
  186. static int write_char (CCaptionSubContext *ctx, char *row,uint8_t col, char ch)
  187. {
  188. if(col < SCREEN_COLUMNS) {
  189. row[col] = ch;
  190. return 0;
  191. }
  192. /* We have extra space at end only for null character */
  193. else if ( col == SCREEN_COLUMNS && ch == 0) {
  194. row[col] = ch;
  195. return 0;
  196. }
  197. else {
  198. av_log(ctx, AV_LOG_WARNING,"Data Ignored since exceeding screen width\n");
  199. return AVERROR_INVALIDDATA;
  200. }
  201. }
  202. /**
  203. * This function after validating parity bit, also remove it from data pair.
  204. * The first byte doesn't pass parity, we replace it with a solid blank
  205. * and process the pair.
  206. * If the second byte doesn't pass parity, it returns INVALIDDATA
  207. * user can ignore the whole pair and pass the other pair.
  208. */
  209. static int validate_cc_data_pair (uint8_t *cc_data_pair)
  210. {
  211. uint8_t cc_valid = (*cc_data_pair & 4) >>2;
  212. uint8_t cc_type = *cc_data_pair & 3;
  213. if (!cc_valid)
  214. return AVERROR_INVALIDDATA;
  215. // if EIA-608 data then verify parity.
  216. if (cc_type==0 || cc_type==1) {
  217. if (!parity_table[cc_data_pair[2]]) {
  218. return AVERROR_INVALIDDATA;
  219. }
  220. if (!parity_table[cc_data_pair[1]]) {
  221. cc_data_pair[1]=0x7F;
  222. }
  223. }
  224. //Skip non-data
  225. if( (cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD )
  226. && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0)
  227. return AVERROR_PATCHWELCOME;
  228. //skip 708 data
  229. if(cc_type == 3 || cc_type == 2 )
  230. return AVERROR_PATCHWELCOME;
  231. /* remove parity bit */
  232. cc_data_pair[1] &= 0x7F;
  233. cc_data_pair[2] &= 0x7F;
  234. return 0;
  235. }
  236. static struct Screen *get_writing_screen(CCaptionSubContext *ctx)
  237. {
  238. switch (ctx->mode) {
  239. case CCMODE_POPON:
  240. // use Inactive screen
  241. return ctx->screen + !ctx->active_screen;
  242. case CCMODE_PAINTON:
  243. case CCMODE_ROLLUP_2:
  244. case CCMODE_ROLLUP_3:
  245. case CCMODE_ROLLUP_4:
  246. case CCMODE_TEXT:
  247. // use active screen
  248. return ctx->screen + ctx->active_screen;
  249. }
  250. /* It was never an option */
  251. return NULL;
  252. }
  253. static void handle_textattr( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo )
  254. {
  255. int i = lo - 0x20;
  256. int ret;
  257. struct Screen *screen = get_writing_screen(ctx);
  258. char *row = screen->characters[ctx->cursor_row];
  259. if( i >= 32)
  260. return;
  261. ctx->cursor_color = pac2_attribs[i][0];
  262. ctx->cursor_font = pac2_attribs[i][1];
  263. SET_FLAG(screen->row_used,ctx->cursor_row);
  264. ret = write_char(ctx, row, ctx->cursor_column, ' ');
  265. if(ret == 0)
  266. ctx->cursor_column++;
  267. }
  268. static void handle_pac( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo )
  269. {
  270. static const int8_t row_map[] = {
  271. 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
  272. };
  273. const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
  274. struct Screen *screen = get_writing_screen(ctx);
  275. char *row;
  276. int indent,i,ret;
  277. if( row_map[index] <= 0 )
  278. return;
  279. lo &= 0x1f;
  280. ctx->cursor_row = row_map[index] - 1;
  281. ctx->cursor_color = pac2_attribs[lo][0];
  282. ctx->cursor_font = pac2_attribs[lo][1];
  283. ctx->cursor_column = 0;
  284. indent = pac2_attribs[lo][2];
  285. row = screen->characters[ctx->cursor_row];
  286. for(i = 0;i < indent; i++) {
  287. ret = write_char(ctx, row, ctx->cursor_column, ' ');
  288. if( ret == 0 )
  289. ctx->cursor_column++;
  290. }
  291. }
  292. /**
  293. * @param pts it is required to set end time
  294. */
  295. static int handle_edm(CCaptionSubContext *ctx,int64_t pts)
  296. {
  297. int i;
  298. int ret = 0;
  299. struct Screen *screen = ctx->screen + ctx->active_screen;
  300. ctx->start_time = ctx->startv_time;
  301. for( i = 0; screen->row_used && i < SCREEN_ROWS; i++)
  302. {
  303. if(CHECK_FLAG(screen->row_used,i)) {
  304. char *str = screen->characters[i];
  305. /* skip space */
  306. while (*str == ' ')
  307. str++;
  308. av_bprint_append_data(&ctx->buffer, str, strlen(str));
  309. av_bprint_append_data(&ctx->buffer, "\\N",2);
  310. UNSET_FLAG(screen->row_used, i);
  311. ret = av_bprint_is_complete(&ctx->buffer);
  312. if( ret == 0) {
  313. ret = AVERROR(ENOMEM);
  314. break;
  315. }
  316. }
  317. }
  318. ctx->startv_time = pts;
  319. ctx->erase_display_memory = 1;
  320. ctx->end_time = pts;
  321. return ret;
  322. }
  323. static int handle_eoc(CCaptionSubContext *ctx, int64_t pts)
  324. {
  325. int ret;
  326. ret = handle_edm(ctx,pts);
  327. ctx->active_screen = !ctx->active_screen;
  328. ctx->cursor_column = 0;
  329. return ret;
  330. }
  331. static void handle_delete_end_of_row( CCaptionSubContext *ctx, char hi, char lo)
  332. {
  333. struct Screen *screen = get_writing_screen(ctx);
  334. char *row = screen->characters[ctx->cursor_row];
  335. write_char(ctx, row, ctx->cursor_column, 0);
  336. }
  337. static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
  338. {
  339. struct Screen *screen = get_writing_screen(ctx);
  340. char *row = screen->characters[ctx->cursor_row];
  341. int ret;
  342. SET_FLAG(screen->row_used,ctx->cursor_row);
  343. ret = write_char(ctx, row, ctx->cursor_column, hi);
  344. if( ret == 0 )
  345. ctx->cursor_column++;
  346. if(lo) {
  347. ret = write_char(ctx, row, ctx->cursor_column, lo);
  348. if ( ret == 0 )
  349. ctx->cursor_column++;
  350. }
  351. write_char(ctx, row, ctx->cursor_column, 0);
  352. /* reset prev command since character can repeat */
  353. ctx->prev_cmd[0] = 0;
  354. ctx->prev_cmd[1] = 0;
  355. #ifdef CHAR_DEBUG
  356. av_log(ctx, AV_LOG_DEBUG,"(%c,%c)\n",hi,lo);
  357. #endif
  358. }
  359. static int process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8_t lo)
  360. {
  361. int ret = 0;
  362. #define COR3(var, with1, with2, with3) ( (var) == (with1) || (var) == (with2) || (var) == (with3) )
  363. if ( hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) {
  364. /* ignore redundant command */
  365. } else if ( (hi == 0x10 && (lo >= 0x40 || lo <= 0x5f)) ||
  366. ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
  367. handle_pac(ctx, hi, lo);
  368. } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) ||
  369. ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) {
  370. handle_textattr(ctx, hi, lo);
  371. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x20 ) {
  372. /* resume caption loading */
  373. ctx->mode = CCMODE_POPON;
  374. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x24 ) {
  375. handle_delete_end_of_row(ctx, hi, lo);
  376. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x25 ) {
  377. ctx->rollup = 2;
  378. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x26 ) {
  379. ctx->rollup = 3;
  380. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x27 ) {
  381. ctx->rollup = 4;
  382. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x29 ) {
  383. /* resume direct captioning */
  384. ctx->mode = CCMODE_PAINTON;
  385. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2B ) {
  386. /* resume text display */
  387. ctx->mode = CCMODE_TEXT;
  388. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2C ) {
  389. /* erase display memory */
  390. ret = handle_edm(ctx, pts);
  391. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2D ) {
  392. /* carriage return */
  393. ctx->row_cnt++;
  394. if(ctx->row_cnt >= ctx->rollup) {
  395. ctx->row_cnt = 0;
  396. ret = handle_edm(ctx, pts);
  397. ctx->active_screen = !ctx->active_screen;
  398. }
  399. ctx->cursor_column = 0;
  400. } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2F ) {
  401. /* end of caption */
  402. ret = handle_eoc(ctx, pts);
  403. } else if (hi>=0x20) {
  404. /* Standard characters (always in pairs) */
  405. handle_char(ctx, hi, lo, pts);
  406. } else {
  407. /* Ignoring all other non data code */
  408. }
  409. /* set prev command */
  410. ctx->prev_cmd[0] = hi;
  411. ctx->prev_cmd[1] = lo;
  412. #undef COR3
  413. return ret;
  414. }
  415. static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
  416. {
  417. CCaptionSubContext *ctx = avctx->priv_data;
  418. AVSubtitle *sub = data;
  419. uint8_t *bptr = NULL;
  420. int len = avpkt->size;
  421. int ret = 0;
  422. int i;
  423. if ( ctx->pktbuf->size < len) {
  424. ret = av_buffer_realloc(&ctx->pktbuf, len);
  425. if(ret < 0) {
  426. av_log(ctx, AV_LOG_WARNING, "Insufficient Memory of %d truncated to %d\n",len, ctx->pktbuf->size);
  427. len = ctx->pktbuf->size;
  428. ret = 0;
  429. }
  430. }
  431. memcpy(ctx->pktbuf->data, avpkt->data, len);
  432. bptr = ctx->pktbuf->data;
  433. for (i = 0; i < len; i += 3) {
  434. uint8_t cc_type = *(bptr + i) & 3;
  435. if (validate_cc_data_pair( bptr + i) )
  436. continue;
  437. /* ignoring data field 1 */
  438. if(cc_type == 1)
  439. continue;
  440. else
  441. process_cc608(ctx, avpkt->pts, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f);
  442. if(ctx->erase_display_memory && *ctx->buffer.str)
  443. {
  444. int start_time = av_rescale_q(ctx->start_time, avctx->time_base, (AVRational){ 1, 100 });
  445. int end_time = av_rescale_q(ctx->end_time, avctx->time_base, (AVRational){ 1, 100 });
  446. #ifdef CHAR_DEBUG
  447. av_log(ctx, AV_LOG_DEBUG,"cdp writing data (%s)\n",ctx->buffer.str);
  448. #endif
  449. ret = ff_ass_add_rect(sub, ctx->buffer.str, start_time, end_time - start_time , 0);
  450. if (ret < 0)
  451. return ret;
  452. sub->pts = av_rescale_q(ctx->start_time, avctx->time_base, AV_TIME_BASE_Q);
  453. ctx->erase_display_memory = 0;
  454. av_bprint_clear(&ctx->buffer);
  455. }
  456. }
  457. *got_sub = sub->num_rects > 0;
  458. return ret;
  459. }
  460. static const AVOption options[] = {
  461. {NULL}
  462. };
  463. static const AVClass ccaption_dec_class = {
  464. .class_name = "Closed caption Decoder",
  465. .item_name = av_default_item_name,
  466. .option = options,
  467. .version = LIBAVUTIL_VERSION_INT,
  468. };
  469. AVCodec ff_ccaption_decoder = {
  470. .name = "cc_dec",
  471. .long_name = NULL_IF_CONFIG_SMALL("Closed Caption (EIA-608 / CEA-708) Decoder"),
  472. .type = AVMEDIA_TYPE_SUBTITLE,
  473. .id = AV_CODEC_ID_EIA_608,
  474. .priv_data_size = sizeof(CCaptionSubContext),
  475. .init = init_decoder,
  476. .close = close_decoder,
  477. .decode = decode,
  478. .priv_class = &ccaption_dec_class,
  479. };