ccaption_dec.c 19.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * 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

29 30 31
#define SET_FLAG(var, val)   ( (var) |=   ( 1 << (val)) )
#define UNSET_FLAG(var, val) ( (var) &=  ~( 1 << (val)) )
#define CHECK_FLAG(var, val) ( (var) &    ( 1 << (val)) )
32 33 34 35 36 37 38 39 40 41 42 43 44 45

/*
 * TODO list
 * 1) handle font and color completely
 */
enum cc_mode {
    CCMODE_POPON,
    CCMODE_PAINTON,
    CCMODE_ROLLUP_2,
    CCMODE_ROLLUP_3,
    CCMODE_ROLLUP_4,
    CCMODE_TEXT,
};

46
enum cc_color_code {
47 48 49 50 51 52 53 54 55
    CCCOL_WHITE,
    CCCOL_GREEN,
    CCCOL_BLUE,
    CCCOL_CYAN,
    CCCOL_RED,
    CCCOL_YELLOW,
    CCCOL_MAGENTA,
    CCCOL_USERDEFINED,
    CCCOL_BLACK,
56
    CCCOL_TRANSPARENT,
57 58
};

59
enum cc_font {
60 61 62
    CCFONT_REGULAR,
    CCFONT_ITALICS,
    CCFONT_UNDERLINED,
63
    CCFONT_UNDERLINED_ITALICS,
64 65
};

66
static const unsigned char pac2_attribs[32][3] = // Color, font, ident
67
{
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    { 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
100
    /* total 32 entries */
101
};
102

103
/* 0-255 needs 256 spaces */
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
static const uint8_t parity_table[256] = { 0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           0, 1, 1, 0, 1, 0, 0, 1,
                                           1, 0, 0, 1, 0, 1, 1, 0 };
136

137 138 139
struct Screen {
    /* +1 is used to compensate null character of string */
    uint8_t characters[SCREEN_ROWS][SCREEN_COLUMNS+1];
140 141
    uint8_t colors[SCREEN_ROWS][SCREEN_COLUMNS+1];
    uint8_t fonts[SCREEN_ROWS][SCREEN_COLUMNS+1];
142 143 144
    /*
     * Bitmask of used rows; if a bit is not set, the
     * corresponding row is not used.
145
     * for setting row 1  use row | (1 << 0)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
     * for setting row 15 use row | (1 << 14)
     */
    int16_t  row_used;
};


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;
161
    int screen_changed;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    int rollup;
    enum  cc_mode mode;
    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;
}CCaptionSubContext;


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 */
181
    ctx->mode = CCMODE_ROLLUP_2;
182 183
    ctx->rollup = 2;
    ret = ff_ass_subtitle_header_default(avctx);
184
    if(ret < 0) {
185
        return ret;
186
    }
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    /* allocate pkt buffer */
    ctx->pktbuf = av_buffer_alloc(128);
    if( !ctx->pktbuf) {
        ret = AVERROR(ENOMEM);
    }
    return ret;
}

static av_cold int close_decoder(AVCodecContext *avctx)
{
    CCaptionSubContext *ctx = avctx->priv_data;
    av_bprint_finalize( &ctx->buffer, NULL);
    av_buffer_unref(&ctx->pktbuf);
    return 0;
}
202

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
/**
 * @param ctx closed caption context just to print log
 */
static int write_char (CCaptionSubContext *ctx, char *row,uint8_t col, char ch)
{
    if(col < SCREEN_COLUMNS) {
        row[col] = ch;
        return 0;
    }
    /* We have extra space at end only for null character */
    else if ( col == SCREEN_COLUMNS && ch == 0) {
        row[col] = ch;
        return 0;
    }
    else {
218
        av_log(ctx, AV_LOG_WARNING,"Data Ignored since exceeding screen width\n");
219 220 221
        return AVERROR_INVALIDDATA;
    }
}
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
/**
 * 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.
 */
static int validate_cc_data_pair (uint8_t *cc_data_pair)
{
    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) {
        if (!parity_table[cc_data_pair[2]]) {
            return AVERROR_INVALIDDATA;
        }
        if (!parity_table[cc_data_pair[1]]) {
            cc_data_pair[1]=0x7F;
        }
    }

    //Skip non-data
    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)
        return AVERROR_PATCHWELCOME;

    //skip 708 data
    if(cc_type == 3 || cc_type == 2 )
        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:
    case CCMODE_ROLLUP_2:
    case CCMODE_ROLLUP_3:
    case CCMODE_ROLLUP_4:
    case CCMODE_TEXT:
        // use active screen
        return ctx->screen + ctx->active_screen;
    }
    /* It was never an option */
    return NULL;
}

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
static void roll_up(CCaptionSubContext *ctx)
{
    struct Screen *screen;
    int i, keep_lines;

    if(ctx->mode == CCMODE_TEXT)
        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);

    for( i = 0; i < ctx->cursor_row - keep_lines; i++ )
        UNSET_FLAG(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;

        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))
            SET_FLAG(screen->row_used, i_row);

    }
    UNSET_FLAG(screen->row_used, ctx->cursor_row);

}

static int reap_screen(CCaptionSubContext *ctx, int64_t pts)
{
    int i;
    int ret = 0;
    struct Screen *screen = ctx->screen + ctx->active_screen;
    ctx->start_time = ctx->startv_time;

    for( i = 0; screen->row_used && i < SCREEN_ROWS; i++)
    {
        if(CHECK_FLAG(screen->row_used,i)) {
            char *str = screen->characters[i];
            /* skip space */
            while (*str == ' ')
                str++;

            av_bprintf(&ctx->buffer, "%s\\N", str);
            ret = av_bprint_is_complete(&ctx->buffer);
            if( ret == 0) {
                ret = AVERROR(ENOMEM);
                break;
            }
        }

    }
341 342 343 344
    if(screen->row_used && ctx->buffer.len >= 2 ) {
        ctx->buffer.len -= 2;
        ctx->buffer.str[ctx->buffer.len] = 0;
    }
345 346 347 348 349
    ctx->startv_time = pts;
    ctx->end_time = pts;
    return ret;
}

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
static void handle_textattr( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo )
{
    int i = lo - 0x20;
    int ret;
    struct Screen *screen = get_writing_screen(ctx);
    char *row = screen->characters[ctx->cursor_row];

    if( i >= 32)
        return;

    ctx->cursor_color =  pac2_attribs[i][0];
    ctx->cursor_font = pac2_attribs[i][1];

    SET_FLAG(screen->row_used,ctx->cursor_row);
    ret = write_char(ctx, row, ctx->cursor_column, ' ');
    if(ret == 0)
        ctx->cursor_column++;
}
368

369 370 371 372 373 374 375 376 377 378
static void handle_pac( CCaptionSubContext *ctx, uint8_t hi, uint8_t lo )
{
    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);
    char *row;
    int indent,i,ret;

379 380
    if( row_map[index] <= 0 ) {
        av_log(ctx, AV_LOG_DEBUG,"Invalid pac index encountered\n");
381
        return;
382
    }
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

    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];
    row = screen->characters[ctx->cursor_row];
    for(i = 0;i < indent; i++) {
        ret = write_char(ctx, row, ctx->cursor_column, ' ');
        if(  ret == 0 )
            ctx->cursor_column++;
    }

}

/**
 * @param pts it is required to set end time
 */
static int handle_edm(CCaptionSubContext *ctx,int64_t pts)
{
    int ret = 0;
    struct Screen *screen = ctx->screen + ctx->active_screen;

408 409
    reap_screen(ctx, pts);
    screen->row_used = 0;
410
    ctx->screen_changed = 1;
411 412
    return ret;
}
413

414 415 416 417 418 419 420 421
static int handle_eoc(CCaptionSubContext *ctx, int64_t pts)
{
    int ret;
    ret = handle_edm(ctx,pts);
    ctx->active_screen = !ctx->active_screen;
    ctx->cursor_column = 0;
    return ret;
}
422

423 424 425 426 427 428 429
static void handle_delete_end_of_row( CCaptionSubContext *ctx, char hi, char lo)
{
    struct Screen *screen = get_writing_screen(ctx);
    char *row = screen->characters[ctx->cursor_row];
    write_char(ctx, row, ctx->cursor_column, 0);

}
430

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
{
    struct Screen *screen = get_writing_screen(ctx);
    char *row = screen->characters[ctx->cursor_row];
    int ret;

    SET_FLAG(screen->row_used,ctx->cursor_row);

    ret = write_char(ctx, row, ctx->cursor_column, hi);
    if( ret == 0 )
        ctx->cursor_column++;

    if(lo) {
        ret = write_char(ctx, row, ctx->cursor_column, lo);
        if ( ret == 0 )
            ctx->cursor_column++;
    }
    write_char(ctx, row, ctx->cursor_column, 0);

    /* reset prev command since character can repeat */
    ctx->prev_cmd[0] = 0;
    ctx->prev_cmd[1] = 0;
453 454 455 456
    if (lo)
       av_dlog(ctx, "(%c,%c)\n",hi,lo);
    else
       av_dlog(ctx, "(%c)\n",hi);
457
}
458

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
static int process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8_t lo)
{
    int ret = 0;
#define COR3(var, with1, with2, with3)  ( (var) == (with1) ||  (var) == (with2) || (var) == (with3) )
    if ( hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) {
    /* ignore redundant command */
    } else if ( (hi == 0x10 && (lo >= 0x40 || lo <= 0x5f)) ||
              ( (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);
    } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x20 ) {
    /* resume caption loading */
        ctx->mode = CCMODE_POPON;
    } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x24 ) {
        handle_delete_end_of_row(ctx, hi, lo);
    } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x25 ) {
        ctx->rollup = 2;
478
        ctx->mode = CCMODE_ROLLUP_2;
479 480
    } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x26 ) {
        ctx->rollup = 3;
481
        ctx->mode = CCMODE_ROLLUP_3;
482 483
    } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x27 ) {
        ctx->rollup = 4;
484
        ctx->mode = CCMODE_ROLLUP_4;
485 486 487 488 489 490 491 492 493 494 495
    } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x29 ) {
    /* resume direct captioning */
        ctx->mode = CCMODE_PAINTON;
    } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2B ) {
    /* resume text display */
        ctx->mode = CCMODE_TEXT;
    } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2C ) {
    /* erase display memory */
        ret = handle_edm(ctx, pts);
    } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2D ) {
    /* carriage return */
496
        av_dlog(ctx, "carriage return\n");
497 498 499
        reap_screen(ctx, pts);
        roll_up(ctx);
        ctx->screen_changed = 1;
500 501 502
        ctx->cursor_column = 0;
    } else if ( COR3(hi, 0x14, 0x15, 0x1C) && lo == 0x2F ) {
    /* end of caption */
503
        av_dlog(ctx, "handle_eoc\n");
504 505 506 507 508 509
        ret = handle_eoc(ctx, pts);
    } else if (hi>=0x20) {
    /* Standard characters (always in pairs) */
        handle_char(ctx, hi, lo, pts);
    } else {
    /* Ignoring all other non data code */
510
        av_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
511 512 513 514 515 516 517 518 519 520
    }

    /* set prev command */
     ctx->prev_cmd[0] = hi;
     ctx->prev_cmd[1] = lo;

#undef COR3
    return ret;

}
521

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
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;

    if ( ctx->pktbuf->size < len) {
        ret = av_buffer_realloc(&ctx->pktbuf, len);
         if(ret < 0) {
            av_log(ctx, AV_LOG_WARNING, "Insufficient Memory of %d truncated to %d\n",len, ctx->pktbuf->size);
            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;
        if (validate_cc_data_pair( bptr + i) )
            continue;
        /* ignoring data field 1 */
        if(cc_type == 1)
            continue;
        else
            process_cc608(ctx, avpkt->pts, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f);
552
        if(ctx->screen_changed && *ctx->buffer.str)
553 554 555
        {
            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 });
556
            av_dlog(ctx, "cdp writing data (%s)\n",ctx->buffer.str);
557
            ret = ff_ass_add_rect_bprint(sub, &ctx->buffer, start_time, end_time - start_time);
558 559 560
            if (ret < 0)
                return ret;
            sub->pts = av_rescale_q(ctx->start_time, avctx->time_base, AV_TIME_BASE_Q);
561
            ctx->screen_changed = 0;
562 563 564 565 566 567 568
            av_bprint_clear(&ctx->buffer);
        }
    }

    *got_sub = sub->num_rects > 0;
    return ret;
}
569

570 571 572
static const AVOption options[] = {
    {NULL}
};
573

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
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,
};