2015-01-06 06:53:34 +00:00
|
|
|
/*
|
|
|
|
* Closed Caption Decoding
|
|
|
|
* Copyright (c) 2015 Anshul Maheshwari
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "ass.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#define SCREEN_ROWS 15
|
|
|
|
#define SCREEN_COLUMNS 32
|
|
|
|
|
2015-01-20 10:21:23 +00:00
|
|
|
#define SET_FLAG(var, val) ( (var) |= ( 1 << (val)) )
|
|
|
|
#define UNSET_FLAG(var, val) ( (var) &= ~( 1 << (val)) )
|
|
|
|
#define CHECK_FLAG(var, val) ( (var) & ( 1 << (val)) )
|
2015-01-06 06:53:34 +00:00
|
|
|
|
2016-01-06 06:55:18 +00:00
|
|
|
static const AVRational ass_tb = {1, 100};
|
|
|
|
|
2015-01-06 06:53:34 +00:00
|
|
|
/*
|
|
|
|
* TODO list
|
|
|
|
* 1) handle font and color completely
|
|
|
|
*/
|
|
|
|
enum cc_mode {
|
|
|
|
CCMODE_POPON,
|
|
|
|
CCMODE_PAINTON,
|
2016-01-06 05:41:03 +00:00
|
|
|
CCMODE_ROLLUP,
|
2015-01-06 06:53:34 +00:00
|
|
|
CCMODE_TEXT,
|
|
|
|
};
|
|
|
|
|
2015-01-20 10:21:23 +00:00
|
|
|
enum cc_color_code {
|
2015-01-06 06:53:34 +00:00
|
|
|
CCCOL_WHITE,
|
|
|
|
CCCOL_GREEN,
|
|
|
|
CCCOL_BLUE,
|
|
|
|
CCCOL_CYAN,
|
|
|
|
CCCOL_RED,
|
|
|
|
CCCOL_YELLOW,
|
|
|
|
CCCOL_MAGENTA,
|
|
|
|
CCCOL_USERDEFINED,
|
|
|
|
CCCOL_BLACK,
|
2015-01-12 12:43:53 +00:00
|
|
|
CCCOL_TRANSPARENT,
|
2015-01-06 06:53:34 +00:00
|
|
|
};
|
|
|
|
|
2015-01-20 10:21:23 +00:00
|
|
|
enum cc_font {
|
2015-01-06 06:53:34 +00:00
|
|
|
CCFONT_REGULAR,
|
|
|
|
CCFONT_ITALICS,
|
|
|
|
CCFONT_UNDERLINED,
|
2015-01-12 12:43:53 +00:00
|
|
|
CCFONT_UNDERLINED_ITALICS,
|
2015-01-06 06:53:34 +00:00
|
|
|
};
|
|
|
|
|
2015-01-20 10:29:48 +00:00
|
|
|
static const unsigned char pac2_attribs[32][3] = // Color, font, ident
|
2015-01-06 06:53:34 +00:00
|
|
|
{
|
2015-01-20 10:21:23 +00:00
|
|
|
{ CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x40 || 0x60
|
|
|
|
{ CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x41 || 0x61
|
|
|
|
{ CCCOL_GREEN, CCFONT_REGULAR, 0 }, // 0x42 || 0x62
|
|
|
|
{ CCCOL_GREEN, CCFONT_UNDERLINED, 0 }, // 0x43 || 0x63
|
|
|
|
{ CCCOL_BLUE, CCFONT_REGULAR, 0 }, // 0x44 || 0x64
|
|
|
|
{ CCCOL_BLUE, CCFONT_UNDERLINED, 0 }, // 0x45 || 0x65
|
|
|
|
{ CCCOL_CYAN, CCFONT_REGULAR, 0 }, // 0x46 || 0x66
|
|
|
|
{ CCCOL_CYAN, CCFONT_UNDERLINED, 0 }, // 0x47 || 0x67
|
|
|
|
{ CCCOL_RED, CCFONT_REGULAR, 0 }, // 0x48 || 0x68
|
|
|
|
{ CCCOL_RED, CCFONT_UNDERLINED, 0 }, // 0x49 || 0x69
|
|
|
|
{ CCCOL_YELLOW, CCFONT_REGULAR, 0 }, // 0x4a || 0x6a
|
|
|
|
{ CCCOL_YELLOW, CCFONT_UNDERLINED, 0 }, // 0x4b || 0x6b
|
|
|
|
{ CCCOL_MAGENTA, CCFONT_REGULAR, 0 }, // 0x4c || 0x6c
|
|
|
|
{ CCCOL_MAGENTA, CCFONT_UNDERLINED, 0 }, // 0x4d || 0x6d
|
|
|
|
{ CCCOL_WHITE, CCFONT_ITALICS, 0 }, // 0x4e || 0x6e
|
|
|
|
{ CCCOL_WHITE, CCFONT_UNDERLINED_ITALICS, 0 }, // 0x4f || 0x6f
|
|
|
|
{ CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x50 || 0x70
|
|
|
|
{ CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x51 || 0x71
|
|
|
|
{ CCCOL_WHITE, CCFONT_REGULAR, 4 }, // 0x52 || 0x72
|
|
|
|
{ CCCOL_WHITE, CCFONT_UNDERLINED, 4 }, // 0x53 || 0x73
|
|
|
|
{ CCCOL_WHITE, CCFONT_REGULAR, 8 }, // 0x54 || 0x74
|
|
|
|
{ CCCOL_WHITE, CCFONT_UNDERLINED, 8 }, // 0x55 || 0x75
|
|
|
|
{ CCCOL_WHITE, CCFONT_REGULAR, 12 }, // 0x56 || 0x76
|
|
|
|
{ CCCOL_WHITE, CCFONT_UNDERLINED, 12 }, // 0x57 || 0x77
|
|
|
|
{ CCCOL_WHITE, CCFONT_REGULAR, 16 }, // 0x58 || 0x78
|
|
|
|
{ CCCOL_WHITE, CCFONT_UNDERLINED, 16 }, // 0x59 || 0x79
|
|
|
|
{ CCCOL_WHITE, CCFONT_REGULAR, 20 }, // 0x5a || 0x7a
|
|
|
|
{ CCCOL_WHITE, CCFONT_UNDERLINED, 20 }, // 0x5b || 0x7b
|
|
|
|
{ CCCOL_WHITE, CCFONT_REGULAR, 24 }, // 0x5c || 0x7c
|
|
|
|
{ CCCOL_WHITE, CCFONT_UNDERLINED, 24 }, // 0x5d || 0x7d
|
|
|
|
{ CCCOL_WHITE, CCFONT_REGULAR, 28 }, // 0x5e || 0x7e
|
|
|
|
{ CCCOL_WHITE, CCFONT_UNDERLINED, 28 } // 0x5f || 0x7f
|
2015-01-12 12:43:53 +00:00
|
|
|
/* total 32 entries */
|
2015-01-06 06:53:34 +00:00
|
|
|
};
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2015-01-06 06:53:34 +00:00
|
|
|
struct Screen {
|
|
|
|
/* +1 is used to compensate null character of string */
|
|
|
|
uint8_t characters[SCREEN_ROWS][SCREEN_COLUMNS+1];
|
2015-01-20 10:32:29 +00:00
|
|
|
uint8_t colors[SCREEN_ROWS][SCREEN_COLUMNS+1];
|
|
|
|
uint8_t fonts[SCREEN_ROWS][SCREEN_COLUMNS+1];
|
2015-01-06 06:53:34 +00:00
|
|
|
/*
|
|
|
|
* Bitmask of used rows; if a bit is not set, the
|
|
|
|
* corresponding row is not used.
|
2015-01-12 12:43:53 +00:00
|
|
|
* for setting row 1 use row | (1 << 0)
|
2015-01-06 06:53:34 +00:00
|
|
|
* for setting row 15 use row | (1 << 14)
|
|
|
|
*/
|
2016-01-05 03:28:02 +00:00
|
|
|
int16_t row_used;
|
2015-01-06 06:53:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct CCaptionSubContext {
|
|
|
|
AVClass *class;
|
|
|
|
struct Screen screen[2];
|
|
|
|
int active_screen;
|
|
|
|
uint8_t cursor_row;
|
|
|
|
uint8_t cursor_column;
|
|
|
|
uint8_t cursor_color;
|
|
|
|
uint8_t cursor_font;
|
|
|
|
AVBPrint buffer;
|
2016-01-09 01:51:32 +00:00
|
|
|
int buffer_changed;
|
2015-01-06 06:53:34 +00:00
|
|
|
int rollup;
|
2016-01-05 23:18:09 +00:00
|
|
|
enum cc_mode mode;
|
2015-01-06 06:53:34 +00:00
|
|
|
int64_t start_time;
|
|
|
|
/* visible screen time */
|
|
|
|
int64_t startv_time;
|
|
|
|
int64_t end_time;
|
|
|
|
char prev_cmd[2];
|
|
|
|
/* buffer to store pkt data */
|
|
|
|
AVBufferRef *pktbuf;
|
2016-01-05 03:28:02 +00:00
|
|
|
} CCaptionSubContext;
|
2015-01-06 06:53:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
static av_cold int init_decoder(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
CCaptionSubContext *ctx = avctx->priv_data;
|
|
|
|
|
|
|
|
av_bprint_init(&ctx->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
|
|
|
|
/* taking by default roll up to 2 */
|
2016-01-06 05:41:03 +00:00
|
|
|
ctx->mode = CCMODE_ROLLUP;
|
2015-01-06 06:53:34 +00:00
|
|
|
ctx->rollup = 2;
|
|
|
|
ret = ff_ass_subtitle_header_default(avctx);
|
2016-01-05 03:28:02 +00:00
|
|
|
if (ret < 0) {
|
2015-04-01 20:58:23 +00:00
|
|
|
return ret;
|
2015-02-06 14:42:13 +00:00
|
|
|
}
|
2015-01-06 06:53:34 +00:00
|
|
|
/* allocate pkt buffer */
|
|
|
|
ctx->pktbuf = av_buffer_alloc(128);
|
2016-01-05 03:28:02 +00:00
|
|
|
if (!ctx->pktbuf) {
|
2015-01-06 06:53:34 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int close_decoder(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
CCaptionSubContext *ctx = avctx->priv_data;
|
2016-01-05 03:28:02 +00:00
|
|
|
av_bprint_finalize(&ctx->buffer, NULL);
|
2015-01-06 06:53:34 +00:00
|
|
|
av_buffer_unref(&ctx->pktbuf);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2015-01-06 06:53:34 +00:00
|
|
|
/**
|
|
|
|
* @param ctx closed caption context just to print log
|
|
|
|
*/
|
2016-01-09 03:21:16 +00:00
|
|
|
static int write_char(CCaptionSubContext *ctx, struct Screen *screen, char ch)
|
2015-01-06 06:53:34 +00:00
|
|
|
{
|
2016-01-09 03:21:16 +00:00
|
|
|
uint8_t col = ctx->cursor_column;
|
|
|
|
char *row = screen->characters[ctx->cursor_row];
|
2016-01-09 03:52:09 +00:00
|
|
|
char *font = screen->fonts[ctx->cursor_row];
|
2016-01-09 03:21:16 +00:00
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
if (col < SCREEN_COLUMNS) {
|
2015-01-06 06:53:34 +00:00
|
|
|
row[col] = ch;
|
2016-01-09 03:52:09 +00:00
|
|
|
font[col] = ctx->cursor_font;
|
2016-01-09 03:24:44 +00:00
|
|
|
if (ch) ctx->cursor_column++;
|
2015-01-06 06:53:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* We have extra space at end only for null character */
|
2016-01-05 03:28:02 +00:00
|
|
|
else if (col == SCREEN_COLUMNS && ch == 0) {
|
2015-01-06 06:53:34 +00:00
|
|
|
row[col] = ch;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
2016-01-05 23:18:09 +00:00
|
|
|
av_log(ctx, AV_LOG_WARNING, "Data Ignored since exceeding screen width\n");
|
2015-01-06 06:53:34 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2015-01-06 06:53:34 +00:00
|
|
|
/**
|
|
|
|
* This function after validating parity bit, also remove it from data pair.
|
|
|
|
* The first byte doesn't pass parity, we replace it with a solid blank
|
|
|
|
* and process the pair.
|
|
|
|
* If the second byte doesn't pass parity, it returns INVALIDDATA
|
|
|
|
* user can ignore the whole pair and pass the other pair.
|
|
|
|
*/
|
2016-01-05 03:28:02 +00:00
|
|
|
static int validate_cc_data_pair(uint8_t *cc_data_pair)
|
2015-01-06 06:53:34 +00:00
|
|
|
{
|
|
|
|
uint8_t cc_valid = (*cc_data_pair & 4) >>2;
|
|
|
|
uint8_t cc_type = *cc_data_pair & 3;
|
|
|
|
|
|
|
|
if (!cc_valid)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
// if EIA-608 data then verify parity.
|
|
|
|
if (cc_type==0 || cc_type==1) {
|
2016-01-07 22:59:03 +00:00
|
|
|
if (!av_parity(cc_data_pair[2])) {
|
2015-01-06 06:53:34 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2016-01-07 22:59:03 +00:00
|
|
|
if (!av_parity(cc_data_pair[1])) {
|
2015-01-06 06:53:34 +00:00
|
|
|
cc_data_pair[1]=0x7F;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Skip non-data
|
2016-01-05 03:28:02 +00:00
|
|
|
if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD)
|
2015-01-06 06:53:34 +00:00
|
|
|
&& (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0)
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
|
|
|
|
//skip 708 data
|
2016-01-05 03:28:02 +00:00
|
|
|
if (cc_type == 3 || cc_type == 2)
|
2015-01-06 06:53:34 +00:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
|
|
|
|
/* remove parity bit */
|
|
|
|
cc_data_pair[1] &= 0x7F;
|
|
|
|
cc_data_pair[2] &= 0x7F;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct Screen *get_writing_screen(CCaptionSubContext *ctx)
|
|
|
|
{
|
|
|
|
switch (ctx->mode) {
|
|
|
|
case CCMODE_POPON:
|
|
|
|
// use Inactive screen
|
|
|
|
return ctx->screen + !ctx->active_screen;
|
|
|
|
case CCMODE_PAINTON:
|
2016-01-06 05:41:03 +00:00
|
|
|
case CCMODE_ROLLUP:
|
2015-01-06 06:53:34 +00:00
|
|
|
case CCMODE_TEXT:
|
|
|
|
// use active screen
|
|
|
|
return ctx->screen + ctx->active_screen;
|
|
|
|
}
|
|
|
|
/* It was never an option */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-06 14:40:11 +00:00
|
|
|
static void roll_up(CCaptionSubContext *ctx)
|
|
|
|
{
|
|
|
|
struct Screen *screen;
|
|
|
|
int i, keep_lines;
|
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
if (ctx->mode == CCMODE_TEXT)
|
2015-02-06 14:40:11 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
screen = get_writing_screen(ctx);
|
|
|
|
|
|
|
|
/* +1 signify cursor_row starts from 0
|
|
|
|
* Can't keep lines less then row cursor pos
|
|
|
|
*/
|
|
|
|
keep_lines = FFMIN(ctx->cursor_row + 1, ctx->rollup);
|
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
for (i = 0; i < ctx->cursor_row - keep_lines; i++)
|
2015-02-06 14:40:11 +00:00
|
|
|
UNSET_FLAG(screen->row_used, i);
|
|
|
|
|
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
for (i = 0; i < keep_lines && screen->row_used; i++) {
|
2015-02-06 14:40:11 +00:00
|
|
|
const int i_row = ctx->cursor_row - keep_lines + i + 1;
|
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
memcpy(screen->characters[i_row], screen->characters[i_row+1], SCREEN_COLUMNS);
|
|
|
|
memcpy(screen->colors[i_row], screen->colors[i_row+1], SCREEN_COLUMNS);
|
|
|
|
memcpy(screen->fonts[i_row], screen->fonts[i_row+1], SCREEN_COLUMNS);
|
|
|
|
if (CHECK_FLAG(screen->row_used, i_row + 1))
|
2015-02-06 14:40:11 +00:00
|
|
|
SET_FLAG(screen->row_used, i_row);
|
|
|
|
|
|
|
|
}
|
|
|
|
UNSET_FLAG(screen->row_used, ctx->cursor_row);
|
|
|
|
}
|
|
|
|
|
2016-01-09 01:56:09 +00:00
|
|
|
static int capture_screen(CCaptionSubContext *ctx)
|
2015-02-06 14:40:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
struct Screen *screen = ctx->screen + ctx->active_screen;
|
2016-01-09 03:52:09 +00:00
|
|
|
enum cc_font prev_font = CCFONT_REGULAR;
|
2016-01-09 01:54:29 +00:00
|
|
|
av_bprint_clear(&ctx->buffer);
|
2015-02-06 14:40:11 +00:00
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
for (i = 0; screen->row_used && i < SCREEN_ROWS; i++)
|
2015-02-06 14:40:11 +00:00
|
|
|
{
|
2016-01-05 23:18:09 +00:00
|
|
|
if (CHECK_FLAG(screen->row_used, i)) {
|
2016-01-09 03:52:09 +00:00
|
|
|
char *row = screen->characters[i];
|
|
|
|
char *font = screen->fonts[i];
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
/* skip leading space */
|
|
|
|
while (row[j] == ' ')
|
|
|
|
j++;
|
|
|
|
|
|
|
|
for (; j < SCREEN_COLUMNS; j++) {
|
2016-01-09 11:37:30 +00:00
|
|
|
const char *e_tag = "", *s_tag = "";
|
|
|
|
|
2016-01-09 03:52:09 +00:00
|
|
|
if (row[j] == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (prev_font != font[j]) {
|
|
|
|
switch (prev_font) {
|
|
|
|
case CCFONT_ITALICS:
|
|
|
|
e_tag = "{/i0}";
|
|
|
|
break;
|
|
|
|
case CCFONT_UNDERLINED:
|
|
|
|
e_tag = "{/u0}";
|
|
|
|
break;
|
|
|
|
case CCFONT_UNDERLINED_ITALICS:
|
|
|
|
e_tag = "{/u0}{/i0}";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (font[j]) {
|
|
|
|
case CCFONT_ITALICS:
|
|
|
|
s_tag = "{/i1}";
|
|
|
|
break;
|
|
|
|
case CCFONT_UNDERLINED:
|
|
|
|
s_tag = "{/u1}";
|
|
|
|
break;
|
|
|
|
case CCFONT_UNDERLINED_ITALICS:
|
|
|
|
s_tag = "{/u1}{/i1}";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev_font = font[j];
|
|
|
|
|
|
|
|
av_bprintf(&ctx->buffer, "%s%s%c", e_tag, s_tag, row[j]);
|
|
|
|
}
|
2015-02-06 14:40:11 +00:00
|
|
|
|
2016-01-09 03:52:09 +00:00
|
|
|
av_bprintf(&ctx->buffer, "\\N");
|
2015-02-06 14:40:11 +00:00
|
|
|
ret = av_bprint_is_complete(&ctx->buffer);
|
2016-01-05 03:28:02 +00:00
|
|
|
if (ret == 0) {
|
2015-02-06 14:40:11 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 22:53:29 +00:00
|
|
|
|
2016-01-09 01:40:22 +00:00
|
|
|
}
|
2016-01-05 03:28:02 +00:00
|
|
|
if (screen->row_used && ctx->buffer.len >= 2) {
|
2015-05-03 13:07:40 +00:00
|
|
|
ctx->buffer.len -= 2;
|
|
|
|
ctx->buffer.str[ctx->buffer.len] = 0;
|
|
|
|
}
|
2016-01-09 01:53:20 +00:00
|
|
|
ctx->buffer_changed = 1;
|
2016-01-09 01:56:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int reap_screen(CCaptionSubContext *ctx, int64_t pts)
|
|
|
|
{
|
|
|
|
ctx->start_time = ctx->startv_time;
|
2015-02-06 14:40:11 +00:00
|
|
|
ctx->startv_time = pts;
|
|
|
|
ctx->end_time = pts;
|
2016-01-09 01:56:09 +00:00
|
|
|
return capture_screen(ctx);
|
2015-02-06 14:40:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
static void handle_textattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
|
2015-01-06 06:53:34 +00:00
|
|
|
{
|
|
|
|
int i = lo - 0x20;
|
|
|
|
struct Screen *screen = get_writing_screen(ctx);
|
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
if (i >= 32)
|
2015-01-06 06:53:34 +00:00
|
|
|
return;
|
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
ctx->cursor_color = pac2_attribs[i][0];
|
2015-01-06 06:53:34 +00:00
|
|
|
ctx->cursor_font = pac2_attribs[i][1];
|
|
|
|
|
2016-01-05 23:18:09 +00:00
|
|
|
SET_FLAG(screen->row_used, ctx->cursor_row);
|
2016-01-09 03:24:44 +00:00
|
|
|
write_char(ctx, screen, ' ');
|
2015-01-06 06:53:34 +00:00
|
|
|
}
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
static void handle_pac(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
|
2015-01-06 06:53:34 +00:00
|
|
|
{
|
|
|
|
static const int8_t row_map[] = {
|
|
|
|
11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
|
|
|
|
};
|
|
|
|
const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
|
|
|
|
struct Screen *screen = get_writing_screen(ctx);
|
2016-01-09 03:24:44 +00:00
|
|
|
int indent, i;
|
2015-01-06 06:53:34 +00:00
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
if (row_map[index] <= 0) {
|
2016-01-05 23:18:09 +00:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "Invalid pac index encountered\n");
|
2015-01-06 06:53:34 +00:00
|
|
|
return;
|
2015-02-06 14:42:40 +00:00
|
|
|
}
|
2015-01-06 06:53:34 +00:00
|
|
|
|
|
|
|
lo &= 0x1f;
|
|
|
|
|
|
|
|
ctx->cursor_row = row_map[index] - 1;
|
|
|
|
ctx->cursor_color = pac2_attribs[lo][0];
|
|
|
|
ctx->cursor_font = pac2_attribs[lo][1];
|
|
|
|
ctx->cursor_column = 0;
|
|
|
|
indent = pac2_attribs[lo][2];
|
2016-01-05 03:28:02 +00:00
|
|
|
for (i = 0; i < indent; i++) {
|
2016-01-09 03:24:44 +00:00
|
|
|
write_char(ctx, screen, ' ');
|
2015-01-06 06:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 01:40:11 +00:00
|
|
|
/**
|
|
|
|
* @param pts it is required to set end time
|
|
|
|
*/
|
|
|
|
static void handle_edm(CCaptionSubContext *ctx, int64_t pts)
|
2015-01-06 06:53:34 +00:00
|
|
|
{
|
2016-01-09 01:40:11 +00:00
|
|
|
struct Screen *screen = ctx->screen + ctx->active_screen;
|
2016-01-09 01:40:04 +00:00
|
|
|
|
|
|
|
reap_screen(ctx, pts);
|
2015-02-06 14:40:11 +00:00
|
|
|
screen->row_used = 0;
|
2015-01-06 06:53:34 +00:00
|
|
|
}
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2016-01-05 22:50:45 +00:00
|
|
|
static void handle_eoc(CCaptionSubContext *ctx, int64_t pts)
|
2015-01-06 06:53:34 +00:00
|
|
|
{
|
2016-01-09 01:40:11 +00:00
|
|
|
handle_edm(ctx,pts);
|
2015-01-06 06:53:34 +00:00
|
|
|
ctx->active_screen = !ctx->active_screen;
|
|
|
|
ctx->cursor_column = 0;
|
|
|
|
}
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
static void handle_delete_end_of_row(CCaptionSubContext *ctx, char hi, char lo)
|
2015-01-06 06:53:34 +00:00
|
|
|
{
|
|
|
|
struct Screen *screen = get_writing_screen(ctx);
|
2016-01-09 03:21:16 +00:00
|
|
|
write_char(ctx, screen, 0);
|
2015-01-06 06:53:34 +00:00
|
|
|
}
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2015-01-06 06:53:34 +00:00
|
|
|
static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
|
|
|
|
{
|
|
|
|
struct Screen *screen = get_writing_screen(ctx);
|
|
|
|
|
2016-01-09 03:21:16 +00:00
|
|
|
SET_FLAG(screen->row_used, ctx->cursor_row);
|
2015-01-06 06:53:34 +00:00
|
|
|
|
2016-01-09 03:24:44 +00:00
|
|
|
write_char(ctx, screen, hi);
|
2015-01-06 06:53:34 +00:00
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
if (lo) {
|
2016-01-09 03:24:44 +00:00
|
|
|
write_char(ctx, screen, lo);
|
2015-01-06 06:53:34 +00:00
|
|
|
}
|
2016-01-09 03:21:16 +00:00
|
|
|
write_char(ctx, screen, 0);
|
2015-01-06 06:53:34 +00:00
|
|
|
|
|
|
|
/* reset prev command since character can repeat */
|
|
|
|
ctx->prev_cmd[0] = 0;
|
|
|
|
ctx->prev_cmd[1] = 0;
|
2015-01-20 10:24:58 +00:00
|
|
|
if (lo)
|
2016-01-05 03:28:02 +00:00
|
|
|
ff_dlog(ctx, "(%c,%c)\n", hi, lo);
|
2015-01-20 10:24:58 +00:00
|
|
|
else
|
2016-01-05 03:28:02 +00:00
|
|
|
ff_dlog(ctx, "(%c)\n", hi);
|
2015-01-06 06:53:34 +00:00
|
|
|
}
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2016-01-05 22:50:45 +00:00
|
|
|
static void process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8_t lo)
|
2015-01-06 06:53:34 +00:00
|
|
|
{
|
2016-01-05 03:28:02 +00:00
|
|
|
if (hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) {
|
2016-01-05 03:28:03 +00:00
|
|
|
/* ignore redundant command */
|
2016-01-05 20:54:28 +00:00
|
|
|
} else if ( (hi == 0x10 && (lo >= 0x40 && lo <= 0x5f)) ||
|
2015-01-06 06:53:34 +00:00
|
|
|
( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
|
|
|
|
handle_pac(ctx, hi, lo);
|
|
|
|
} else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) ||
|
|
|
|
( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) {
|
|
|
|
handle_textattr(ctx, hi, lo);
|
2016-01-05 03:28:03 +00:00
|
|
|
} else if (hi == 0x14 || hi == 0x15 || hi == 0x1c) {
|
|
|
|
switch (lo) {
|
|
|
|
case 0x20:
|
|
|
|
/* resume caption loading */
|
|
|
|
ctx->mode = CCMODE_POPON;
|
|
|
|
break;
|
|
|
|
case 0x24:
|
|
|
|
handle_delete_end_of_row(ctx, hi, lo);
|
|
|
|
break;
|
|
|
|
case 0x25:
|
|
|
|
ctx->rollup = 2;
|
2016-01-06 05:41:03 +00:00
|
|
|
ctx->mode = CCMODE_ROLLUP;
|
2016-01-05 03:28:03 +00:00
|
|
|
break;
|
|
|
|
case 0x26:
|
|
|
|
ctx->rollup = 3;
|
2016-01-06 05:41:03 +00:00
|
|
|
ctx->mode = CCMODE_ROLLUP;
|
2016-01-05 03:28:03 +00:00
|
|
|
break;
|
|
|
|
case 0x27:
|
|
|
|
ctx->rollup = 4;
|
2016-01-06 05:41:03 +00:00
|
|
|
ctx->mode = CCMODE_ROLLUP;
|
2016-01-05 03:28:03 +00:00
|
|
|
break;
|
|
|
|
case 0x29:
|
|
|
|
/* resume direct captioning */
|
|
|
|
ctx->mode = CCMODE_PAINTON;
|
|
|
|
break;
|
|
|
|
case 0x2b:
|
|
|
|
/* resume text display */
|
|
|
|
ctx->mode = CCMODE_TEXT;
|
|
|
|
break;
|
|
|
|
case 0x2c:
|
|
|
|
/* erase display memory */
|
2016-01-09 01:40:11 +00:00
|
|
|
handle_edm(ctx, pts);
|
2016-01-05 03:28:03 +00:00
|
|
|
break;
|
|
|
|
case 0x2d:
|
|
|
|
/* carriage return */
|
|
|
|
ff_dlog(ctx, "carriage return\n");
|
|
|
|
reap_screen(ctx, pts);
|
|
|
|
roll_up(ctx);
|
|
|
|
ctx->cursor_column = 0;
|
|
|
|
break;
|
|
|
|
case 0x2f:
|
|
|
|
/* end of caption */
|
|
|
|
ff_dlog(ctx, "handle_eoc\n");
|
2016-01-05 22:50:45 +00:00
|
|
|
handle_eoc(ctx, pts);
|
2016-01-05 03:28:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
|
|
|
|
break;
|
|
|
|
}
|
2016-01-05 23:18:09 +00:00
|
|
|
} else if (hi >= 0x20) {
|
2016-01-05 03:28:03 +00:00
|
|
|
/* Standard characters (always in pairs) */
|
2015-01-06 06:53:34 +00:00
|
|
|
handle_char(ctx, hi, lo, pts);
|
|
|
|
} else {
|
2016-01-05 03:28:03 +00:00
|
|
|
/* Ignoring all other non data code */
|
2015-08-18 00:45:35 +00:00
|
|
|
ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
|
2015-01-06 06:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set prev command */
|
2016-01-05 03:28:02 +00:00
|
|
|
ctx->prev_cmd[0] = hi;
|
|
|
|
ctx->prev_cmd[1] = lo;
|
2015-01-06 06:53:34 +00:00
|
|
|
}
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2015-01-06 06:53:34 +00:00
|
|
|
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
|
|
|
|
{
|
|
|
|
CCaptionSubContext *ctx = avctx->priv_data;
|
|
|
|
AVSubtitle *sub = data;
|
|
|
|
uint8_t *bptr = NULL;
|
|
|
|
int len = avpkt->size;
|
|
|
|
int ret = 0;
|
|
|
|
int i;
|
|
|
|
|
2016-01-05 03:28:02 +00:00
|
|
|
if (ctx->pktbuf->size < len) {
|
2015-01-06 06:53:34 +00:00
|
|
|
ret = av_buffer_realloc(&ctx->pktbuf, len);
|
2016-01-05 03:28:02 +00:00
|
|
|
if (ret < 0) {
|
2016-01-05 23:18:09 +00:00
|
|
|
av_log(ctx, AV_LOG_WARNING, "Insufficient Memory of %d truncated to %d\n", len, ctx->pktbuf->size);
|
2015-01-06 06:53:34 +00:00
|
|
|
len = ctx->pktbuf->size;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(ctx->pktbuf->data, avpkt->data, len);
|
|
|
|
bptr = ctx->pktbuf->data;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i += 3) {
|
|
|
|
uint8_t cc_type = *(bptr + i) & 3;
|
2016-01-05 03:28:02 +00:00
|
|
|
if (validate_cc_data_pair(bptr + i))
|
2015-01-06 06:53:34 +00:00
|
|
|
continue;
|
|
|
|
/* ignoring data field 1 */
|
|
|
|
if(cc_type == 1)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
process_cc608(ctx, avpkt->pts, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f);
|
2016-01-09 01:51:32 +00:00
|
|
|
if (ctx->buffer_changed && *ctx->buffer.str)
|
2015-01-06 06:53:34 +00:00
|
|
|
{
|
2016-01-06 06:55:18 +00:00
|
|
|
int start_time = av_rescale_q(ctx->start_time, avctx->time_base, ass_tb);
|
|
|
|
int end_time = av_rescale_q(ctx->end_time, avctx->time_base, ass_tb);
|
2015-08-18 00:45:35 +00:00
|
|
|
ff_dlog(ctx, "cdp writing data (%s)\n",ctx->buffer.str);
|
2015-05-03 13:07:40 +00:00
|
|
|
ret = ff_ass_add_rect_bprint(sub, &ctx->buffer, start_time, end_time - start_time);
|
2015-01-06 06:53:34 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
sub->pts = av_rescale_q(ctx->start_time, avctx->time_base, AV_TIME_BASE_Q);
|
2016-01-09 01:51:32 +00:00
|
|
|
ctx->buffer_changed = 0;
|
2015-01-06 06:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*got_sub = sub->num_rects > 0;
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2015-01-06 06:53:34 +00:00
|
|
|
static const AVOption options[] = {
|
|
|
|
{NULL}
|
|
|
|
};
|
2015-01-20 10:21:23 +00:00
|
|
|
|
2015-01-06 06:53:34 +00:00
|
|
|
static const AVClass ccaption_dec_class = {
|
|
|
|
.class_name = "Closed caption Decoder",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVCodec ff_ccaption_decoder = {
|
|
|
|
.name = "cc_dec",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Closed Caption (EIA-608 / CEA-708) Decoder"),
|
|
|
|
.type = AVMEDIA_TYPE_SUBTITLE,
|
|
|
|
.id = AV_CODEC_ID_EIA_608,
|
|
|
|
.priv_data_size = sizeof(CCaptionSubContext),
|
|
|
|
.init = init_decoder,
|
|
|
|
.close = close_decoder,
|
|
|
|
.decode = decode,
|
|
|
|
.priv_class = &ccaption_dec_class,
|
|
|
|
};
|