Commit 55ca79f5 authored by Aman Gupta's avatar Aman Gupta Committed by Michael Niedermayer

libavcodec/ccaption_dec: clean up and standardize white space

Signed-off-by: 's avatarMichael Niedermayer <michael@niedermayer.cc>
parent 23679d82
...@@ -148,7 +148,6 @@ struct Screen { ...@@ -148,7 +148,6 @@ struct Screen {
int16_t row_used; int16_t row_used;
}; };
typedef struct CCaptionSubContext { typedef struct CCaptionSubContext {
AVClass *class; AVClass *class;
struct Screen screen[2]; struct Screen screen[2];
...@@ -168,7 +167,7 @@ typedef struct CCaptionSubContext { ...@@ -168,7 +167,7 @@ typedef struct CCaptionSubContext {
char prev_cmd[2]; char prev_cmd[2];
/* buffer to store pkt data */ /* buffer to store pkt data */
AVBufferRef *pktbuf; AVBufferRef *pktbuf;
}CCaptionSubContext; } CCaptionSubContext;
static av_cold int init_decoder(AVCodecContext *avctx) static av_cold int init_decoder(AVCodecContext *avctx)
...@@ -181,12 +180,12 @@ static av_cold int init_decoder(AVCodecContext *avctx) ...@@ -181,12 +180,12 @@ static av_cold int init_decoder(AVCodecContext *avctx)
ctx->mode = CCMODE_ROLLUP_2; ctx->mode = CCMODE_ROLLUP_2;
ctx->rollup = 2; ctx->rollup = 2;
ret = ff_ass_subtitle_header_default(avctx); ret = ff_ass_subtitle_header_default(avctx);
if(ret < 0) { if (ret < 0) {
return ret; return ret;
} }
/* allocate pkt buffer */ /* allocate pkt buffer */
ctx->pktbuf = av_buffer_alloc(128); ctx->pktbuf = av_buffer_alloc(128);
if( !ctx->pktbuf) { if (!ctx->pktbuf) {
ret = AVERROR(ENOMEM); ret = AVERROR(ENOMEM);
} }
return ret; return ret;
...@@ -195,7 +194,7 @@ static av_cold int init_decoder(AVCodecContext *avctx) ...@@ -195,7 +194,7 @@ static av_cold int init_decoder(AVCodecContext *avctx)
static av_cold int close_decoder(AVCodecContext *avctx) static av_cold int close_decoder(AVCodecContext *avctx)
{ {
CCaptionSubContext *ctx = avctx->priv_data; CCaptionSubContext *ctx = avctx->priv_data;
av_bprint_finalize( &ctx->buffer, NULL); av_bprint_finalize(&ctx->buffer, NULL);
av_buffer_unref(&ctx->pktbuf); av_buffer_unref(&ctx->pktbuf);
return 0; return 0;
} }
...@@ -203,14 +202,14 @@ static av_cold int close_decoder(AVCodecContext *avctx) ...@@ -203,14 +202,14 @@ static av_cold int close_decoder(AVCodecContext *avctx)
/** /**
* @param ctx closed caption context just to print log * @param ctx closed caption context just to print log
*/ */
static int write_char (CCaptionSubContext *ctx, char *row,uint8_t col, char ch) static int write_char(CCaptionSubContext *ctx, char *row, uint8_t col, char ch)
{ {
if(col < SCREEN_COLUMNS) { if (col < SCREEN_COLUMNS) {
row[col] = ch; row[col] = ch;
return 0; return 0;
} }
/* We have extra space at end only for null character */ /* We have extra space at end only for null character */
else if ( col == SCREEN_COLUMNS && ch == 0) { else if (col == SCREEN_COLUMNS && ch == 0) {
row[col] = ch; row[col] = ch;
return 0; return 0;
} }
...@@ -227,7 +226,7 @@ static int write_char (CCaptionSubContext *ctx, char *row,uint8_t col, char ch) ...@@ -227,7 +226,7 @@ static int write_char (CCaptionSubContext *ctx, char *row,uint8_t col, char ch)
* If the second byte doesn't pass parity, it returns INVALIDDATA * If the second byte doesn't pass parity, it returns INVALIDDATA
* user can ignore the whole pair and pass the other pair. * user can ignore the whole pair and pass the other pair.
*/ */
static int validate_cc_data_pair (uint8_t *cc_data_pair) static int validate_cc_data_pair(uint8_t *cc_data_pair)
{ {
uint8_t cc_valid = (*cc_data_pair & 4) >>2; uint8_t cc_valid = (*cc_data_pair & 4) >>2;
uint8_t cc_type = *cc_data_pair & 3; uint8_t cc_type = *cc_data_pair & 3;
...@@ -246,21 +245,19 @@ static int validate_cc_data_pair (uint8_t *cc_data_pair) ...@@ -246,21 +245,19 @@ static int validate_cc_data_pair (uint8_t *cc_data_pair)
} }
//Skip non-data //Skip non-data
if( (cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD ) if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD)
&& (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0) && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0)
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
//skip 708 data //skip 708 data
if(cc_type == 3 || cc_type == 2 ) if (cc_type == 3 || cc_type == 2)
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
/* remove parity bit */ /* remove parity bit */
cc_data_pair[1] &= 0x7F; cc_data_pair[1] &= 0x7F;
cc_data_pair[2] &= 0x7F; cc_data_pair[2] &= 0x7F;
return 0; return 0;
} }
static struct Screen *get_writing_screen(CCaptionSubContext *ctx) static struct Screen *get_writing_screen(CCaptionSubContext *ctx)
...@@ -286,7 +283,7 @@ static void roll_up(CCaptionSubContext *ctx) ...@@ -286,7 +283,7 @@ static void roll_up(CCaptionSubContext *ctx)
struct Screen *screen; struct Screen *screen;
int i, keep_lines; int i, keep_lines;
if(ctx->mode == CCMODE_TEXT) if (ctx->mode == CCMODE_TEXT)
return; return;
screen = get_writing_screen(ctx); screen = get_writing_screen(ctx);
...@@ -296,22 +293,21 @@ static void roll_up(CCaptionSubContext *ctx) ...@@ -296,22 +293,21 @@ static void roll_up(CCaptionSubContext *ctx)
*/ */
keep_lines = FFMIN(ctx->cursor_row + 1, ctx->rollup); keep_lines = FFMIN(ctx->cursor_row + 1, ctx->rollup);
for( i = 0; i < ctx->cursor_row - keep_lines; i++ ) for (i = 0; i < ctx->cursor_row - keep_lines; i++)
UNSET_FLAG(screen->row_used, i); UNSET_FLAG(screen->row_used, i);
for( i = 0; i < keep_lines && screen->row_used; i++ ) { for (i = 0; i < keep_lines && screen->row_used; i++) {
const int i_row = ctx->cursor_row - keep_lines + i + 1; const int i_row = ctx->cursor_row - keep_lines + i + 1;
memcpy( screen->characters[i_row], screen->characters[i_row+1], SCREEN_COLUMNS ); 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->colors[i_row], screen->colors[i_row+1], SCREEN_COLUMNS);
memcpy( screen->fonts[i_row], screen->fonts[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)) if (CHECK_FLAG(screen->row_used, i_row + 1))
SET_FLAG(screen->row_used, i_row); SET_FLAG(screen->row_used, i_row);
} }
UNSET_FLAG(screen->row_used, ctx->cursor_row); UNSET_FLAG(screen->row_used, ctx->cursor_row);
} }
static int reap_screen(CCaptionSubContext *ctx, int64_t pts) static int reap_screen(CCaptionSubContext *ctx, int64_t pts)
...@@ -321,9 +317,9 @@ static int reap_screen(CCaptionSubContext *ctx, int64_t pts) ...@@ -321,9 +317,9 @@ static int reap_screen(CCaptionSubContext *ctx, int64_t pts)
struct Screen *screen = ctx->screen + ctx->active_screen; struct Screen *screen = ctx->screen + ctx->active_screen;
ctx->start_time = ctx->startv_time; ctx->start_time = ctx->startv_time;
for( i = 0; screen->row_used && i < SCREEN_ROWS; i++) for (i = 0; screen->row_used && i < SCREEN_ROWS; i++)
{ {
if(CHECK_FLAG(screen->row_used,i)) { if (CHECK_FLAG(screen->row_used,i)) {
char *str = screen->characters[i]; char *str = screen->characters[i];
/* skip space */ /* skip space */
while (*str == ' ') while (*str == ' ')
...@@ -331,14 +327,14 @@ static int reap_screen(CCaptionSubContext *ctx, int64_t pts) ...@@ -331,14 +327,14 @@ static int reap_screen(CCaptionSubContext *ctx, int64_t pts)
av_bprintf(&ctx->buffer, "%s\\N", str); av_bprintf(&ctx->buffer, "%s\\N", str);
ret = av_bprint_is_complete(&ctx->buffer); ret = av_bprint_is_complete(&ctx->buffer);
if( ret == 0) { if (ret == 0) {
ret = AVERROR(ENOMEM); ret = AVERROR(ENOMEM);
break; break;
} }
} }
} }
if(screen->row_used && ctx->buffer.len >= 2 ) { if (screen->row_used && ctx->buffer.len >= 2) {
ctx->buffer.len -= 2; ctx->buffer.len -= 2;
ctx->buffer.str[ctx->buffer.len] = 0; ctx->buffer.str[ctx->buffer.len] = 0;
} }
...@@ -347,14 +343,14 @@ static int reap_screen(CCaptionSubContext *ctx, int64_t pts) ...@@ -347,14 +343,14 @@ static int reap_screen(CCaptionSubContext *ctx, int64_t pts)
return ret; return ret;
} }
static void handle_textattr( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo ) static void handle_textattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
{ {
int i = lo - 0x20; int i = lo - 0x20;
int ret; int ret;
struct Screen *screen = get_writing_screen(ctx); struct Screen *screen = get_writing_screen(ctx);
char *row = screen->characters[ctx->cursor_row]; char *row = screen->characters[ctx->cursor_row];
if( i >= 32) if (i >= 32)
return; return;
ctx->cursor_color = pac2_attribs[i][0]; ctx->cursor_color = pac2_attribs[i][0];
...@@ -362,11 +358,11 @@ static void handle_textattr( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo ) ...@@ -362,11 +358,11 @@ static void handle_textattr( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo )
SET_FLAG(screen->row_used,ctx->cursor_row); SET_FLAG(screen->row_used,ctx->cursor_row);
ret = write_char(ctx, row, ctx->cursor_column, ' '); ret = write_char(ctx, row, ctx->cursor_column, ' ');
if(ret == 0) if (ret == 0)
ctx->cursor_column++; ctx->cursor_column++;
} }
static void handle_pac( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo ) static void handle_pac(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
{ {
static const int8_t row_map[] = { static const int8_t row_map[] = {
11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
...@@ -374,9 +370,9 @@ static void handle_pac( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo ) ...@@ -374,9 +370,9 @@ static void handle_pac( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo )
const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 ); const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
struct Screen *screen = get_writing_screen(ctx); struct Screen *screen = get_writing_screen(ctx);
char *row; char *row;
int indent,i,ret; int indent, i, ret;
if( row_map[index] <= 0 ) { if (row_map[index] <= 0) {
av_log(ctx, AV_LOG_DEBUG,"Invalid pac index encountered\n"); av_log(ctx, AV_LOG_DEBUG,"Invalid pac index encountered\n");
return; return;
} }
...@@ -389,18 +385,17 @@ static void handle_pac( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo ) ...@@ -389,18 +385,17 @@ static void handle_pac( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo )
ctx->cursor_column = 0; ctx->cursor_column = 0;
indent = pac2_attribs[lo][2]; indent = pac2_attribs[lo][2];
row = screen->characters[ctx->cursor_row]; row = screen->characters[ctx->cursor_row];
for(i = 0;i < indent; i++) { for (i = 0; i < indent; i++) {
ret = write_char(ctx, row, ctx->cursor_column, ' '); ret = write_char(ctx, row, ctx->cursor_column, ' ');
if( ret == 0 ) if (ret == 0)
ctx->cursor_column++; ctx->cursor_column++;
} }
} }
/** /**
* @param pts it is required to set end time * @param pts it is required to set end time
*/ */
static int handle_edm(CCaptionSubContext *ctx,int64_t pts) static int handle_edm(CCaptionSubContext *ctx, int64_t pts)
{ {
int ret = 0; int ret = 0;
struct Screen *screen = ctx->screen + ctx->active_screen; struct Screen *screen = ctx->screen + ctx->active_screen;
...@@ -420,12 +415,11 @@ static int handle_eoc(CCaptionSubContext *ctx, int64_t pts) ...@@ -420,12 +415,11 @@ static int handle_eoc(CCaptionSubContext *ctx, int64_t pts)
return ret; return ret;
} }
static void handle_delete_end_of_row( CCaptionSubContext *ctx, char hi, char lo) static void handle_delete_end_of_row(CCaptionSubContext *ctx, char hi, char lo)
{ {
struct Screen *screen = get_writing_screen(ctx); struct Screen *screen = get_writing_screen(ctx);
char *row = screen->characters[ctx->cursor_row]; char *row = screen->characters[ctx->cursor_row];
write_char(ctx, row, ctx->cursor_column, 0); write_char(ctx, row, ctx->cursor_column, 0);
} }
static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts) static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
...@@ -437,12 +431,12 @@ static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts) ...@@ -437,12 +431,12 @@ static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
SET_FLAG(screen->row_used,ctx->cursor_row); SET_FLAG(screen->row_used,ctx->cursor_row);
ret = write_char(ctx, row, ctx->cursor_column, hi); ret = write_char(ctx, row, ctx->cursor_column, hi);
if( ret == 0 ) if (ret == 0)
ctx->cursor_column++; ctx->cursor_column++;
if(lo) { if (lo) {
ret = write_char(ctx, row, ctx->cursor_column, lo); ret = write_char(ctx, row, ctx->cursor_column, lo);
if ( ret == 0 ) if (ret == 0)
ctx->cursor_column++; ctx->cursor_column++;
} }
write_char(ctx, row, ctx->cursor_column, 0); write_char(ctx, row, ctx->cursor_column, 0);
...@@ -451,16 +445,16 @@ static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts) ...@@ -451,16 +445,16 @@ static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
ctx->prev_cmd[0] = 0; ctx->prev_cmd[0] = 0;
ctx->prev_cmd[1] = 0; ctx->prev_cmd[1] = 0;
if (lo) if (lo)
ff_dlog(ctx, "(%c,%c)\n",hi,lo); ff_dlog(ctx, "(%c,%c)\n", hi, lo);
else else
ff_dlog(ctx, "(%c)\n",hi); ff_dlog(ctx, "(%c)\n", hi);
} }
static int process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8_t lo) static int process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8_t lo)
{ {
int ret = 0; int ret = 0;
#define COR3(var, with1, with2, with3) ( (var) == (with1) || (var) == (with2) || (var) == (with3) ) #define COR3(var, with1, with2, with3) ( (var) == (with1) || (var) == (with2) || (var) == (with3) )
if ( hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) { if (hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) {
/* ignore redundant command */ /* ignore redundant command */
} else if ( (hi == 0x10 && (lo >= 0x40 || lo <= 0x5f)) || } else if ( (hi == 0x10 && (lo >= 0x40 || lo <= 0x5f)) ||
( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) { ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
...@@ -516,7 +510,6 @@ static int process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8 ...@@ -516,7 +510,6 @@ static int process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8
#undef COR3 #undef COR3
return ret; return ret;
} }
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt) static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
...@@ -528,9 +521,9 @@ static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avp ...@@ -528,9 +521,9 @@ static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avp
int ret = 0; int ret = 0;
int i; int i;
if ( ctx->pktbuf->size < len) { if (ctx->pktbuf->size < len) {
ret = av_buffer_realloc(&ctx->pktbuf, len); ret = av_buffer_realloc(&ctx->pktbuf, len);
if(ret < 0) { if (ret < 0) {
av_log(ctx, AV_LOG_WARNING, "Insufficient Memory of %d truncated to %d\n",len, ctx->pktbuf->size); av_log(ctx, AV_LOG_WARNING, "Insufficient Memory of %d truncated to %d\n",len, ctx->pktbuf->size);
len = ctx->pktbuf->size; len = ctx->pktbuf->size;
ret = 0; ret = 0;
...@@ -539,17 +532,16 @@ static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avp ...@@ -539,17 +532,16 @@ static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avp
memcpy(ctx->pktbuf->data, avpkt->data, len); memcpy(ctx->pktbuf->data, avpkt->data, len);
bptr = ctx->pktbuf->data; bptr = ctx->pktbuf->data;
for (i = 0; i < len; i += 3) { for (i = 0; i < len; i += 3) {
uint8_t cc_type = *(bptr + i) & 3; uint8_t cc_type = *(bptr + i) & 3;
if (validate_cc_data_pair( bptr + i) ) if (validate_cc_data_pair(bptr + i))
continue; continue;
/* ignoring data field 1 */ /* ignoring data field 1 */
if(cc_type == 1) if(cc_type == 1)
continue; continue;
else else
process_cc608(ctx, avpkt->pts, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f); process_cc608(ctx, avpkt->pts, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f);
if(ctx->screen_changed && *ctx->buffer.str) if (ctx->screen_changed && *ctx->buffer.str)
{ {
int start_time = av_rescale_q(ctx->start_time, avctx->time_base, (AVRational){ 1, 100 }); int start_time = av_rescale_q(ctx->start_time, avctx->time_base, (AVRational){ 1, 100 });
int end_time = av_rescale_q(ctx->end_time, avctx->time_base, (AVRational){ 1, 100 }); int end_time = av_rescale_q(ctx->end_time, avctx->time_base, (AVRational){ 1, 100 });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment