Hi This is basic implementation where all features of closed caption are not implemented.
-Anshul
>From 09a199afa72eeb7b8be8527da7239f4abcc52dbe Mon Sep 17 00:00:00 2001 From: Nicolas George <geo...@nsup.org> Date: Tue, 28 Oct 2014 14:41:15 +0530 Subject: [PATCH 1/2] allow to extract subcc --- doc/indevs.texi | 9 ++++++ libavdevice/lavfi.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/doc/indevs.texi b/doc/indevs.texi index 5d2a308..f31e2e3 100644 --- a/doc/indevs.texi +++ b/doc/indevs.texi @@ -494,6 +494,9 @@ number starting from 0 corresponding to the mapped input stream generated by the device. The first unlabelled output is automatically assigned to the "out0" label, but all the others need to be specified explicitly. +The suffix "+subcc" can be appended to the output label to create an extra +stream with the closed captions packets attached to that output +(experimental). If not specified defaults to the filename specified for the input device. @@ -541,6 +544,12 @@ Read an audio stream and a video stream and play it back with ffplay -f lavfi "movie=test.avi[out0];amovie=test.wav[out1]" @end example +@item +Dump decoded frames to images and closed captions to a file (experimental): +@example +ffmpeg -f lavfi -i "movie=test.ts[out0+subcc]" -map v frame%08d.png -map s -c copy -f rawvideo subcc.bin +@end example + @end itemize @section libcdio diff --git a/libavdevice/lavfi.c b/libavdevice/lavfi.c index 1398ece..bc45117 100644 --- a/libavdevice/lavfi.c +++ b/libavdevice/lavfi.c @@ -51,7 +51,10 @@ typedef struct { int *sink_stream_map; int *sink_eof; int *stream_sink_map; + int *sink_stream_subcc_map; AVFrame *decoded_frame; + int nb_sinks; + AVPacket subcc_packet; } LavfiContext; static int *create_all_formats(int n) @@ -82,6 +85,7 @@ av_cold static int lavfi_read_close(AVFormatContext *avctx) av_freep(&lavfi->sink_stream_map); av_freep(&lavfi->sink_eof); av_freep(&lavfi->stream_sink_map); + av_freep(&lavfi->sink_stream_subcc_map); av_freep(&lavfi->sinks); avfilter_graph_free(&lavfi->graph); av_frame_free(&lavfi->decoded_frame); @@ -89,6 +93,27 @@ av_cold static int lavfi_read_close(AVFormatContext *avctx) return 0; } +static int create_subcc_streams(AVFormatContext *avctx) +{ + LavfiContext *lavfi = avctx->priv_data; + AVStream *st; + int stream_idx, sink_idx; + + for (stream_idx = 0; stream_idx < lavfi->nb_sinks; stream_idx++) { + sink_idx = lavfi->stream_sink_map[stream_idx]; + if (lavfi->sink_stream_subcc_map[sink_idx]) { + lavfi->sink_stream_subcc_map[sink_idx] = avctx->nb_streams; + if (!(st = avformat_new_stream(avctx, NULL))) + return AVERROR(ENOMEM); + st->codec->codec_id = AV_CODEC_ID_EIA_608; + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; + } else { + lavfi->sink_stream_subcc_map[sink_idx] = -1; + } + } + return 0; +} + av_cold static int lavfi_read_header(AVFormatContext *avctx) { LavfiContext *lavfi = avctx->priv_data; @@ -153,6 +178,7 @@ av_cold static int lavfi_read_header(AVFormatContext *avctx) /* count the outputs */ for (n = 0, inout = output_links; inout; n++, inout = inout->next); + lavfi->nb_sinks = n; if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n))) FAIL(AVERROR(ENOMEM)); @@ -160,6 +186,8 @@ av_cold static int lavfi_read_header(AVFormatContext *avctx) FAIL(AVERROR(ENOMEM)); if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n))) FAIL(AVERROR(ENOMEM)); + if (!(lavfi->sink_stream_subcc_map = av_malloc(sizeof(int) * n))) + FAIL(AVERROR(ENOMEM)); for (i = 0; i < n; i++) lavfi->stream_sink_map[i] = -1; @@ -167,10 +195,10 @@ av_cold static int lavfi_read_header(AVFormatContext *avctx) /* parse the output link names - they need to be of the form out0, out1, ... * create a mapping between them and the streams */ for (i = 0, inout = output_links; inout; i++, inout = inout->next) { - int stream_idx; + int stream_idx, use_subcc = 0; if (!strcmp(inout->name, "out")) stream_idx = 0; - else if (sscanf(inout->name, "out%d\n", &stream_idx) != 1) { + else if (sscanf(inout->name, "out%d+subcc%n\n", &stream_idx, &use_subcc) != 1) { av_log(avctx, AV_LOG_ERROR, "Invalid outpad name '%s'\n", inout->name); FAIL(AVERROR(EINVAL)); @@ -192,6 +220,7 @@ av_cold static int lavfi_read_header(AVFormatContext *avctx) } lavfi->sink_stream_map[i] = stream_idx; lavfi->stream_sink_map[stream_idx] = i; + lavfi->sink_stream_subcc_map[stream_idx] = !!use_subcc; } /* for each open output create a corresponding stream */ @@ -203,7 +232,7 @@ av_cold static int lavfi_read_header(AVFormatContext *avctx) } /* create a sink for each output and connect them to the graph */ - lavfi->sinks = av_malloc_array(avctx->nb_streams, sizeof(AVFilterContext *)); + lavfi->sinks = av_malloc_array(lavfi->nb_sinks, sizeof(AVFilterContext *)); if (!lavfi->sinks) FAIL(AVERROR(ENOMEM)); @@ -267,7 +296,7 @@ av_cold static int lavfi_read_header(AVFormatContext *avctx) } /* fill each stream with the information in the corresponding sink */ - for (i = 0; i < avctx->nb_streams; i++) { + for (i = 0; i < lavfi->nb_sinks; i++) { AVFilterLink *link = lavfi->sinks[lavfi->stream_sink_map[i]]->inputs[0]; AVStream *st = avctx->streams[i]; st->codec->codec_type = link->type; @@ -298,6 +327,9 @@ av_cold static int lavfi_read_header(AVFormatContext *avctx) } } + if ((ret = create_subcc_streams(avctx)) < 0) + FAIL(ret); + if (!(lavfi->decoded_frame = av_frame_alloc())) FAIL(AVERROR(ENOMEM)); @@ -310,6 +342,30 @@ end: return ret; } +static int create_subcc_packet(AVFormatContext *avctx, AVFrame *frame, + int sink_idx) +{ + LavfiContext *lavfi = avctx->priv_data; + AVFrameSideData *sd; + int stream_idx, i, ret; + + if ((stream_idx = lavfi->sink_stream_subcc_map[sink_idx]) < 0) + return 0; + for (i = 0; i < frame->nb_side_data; i++) + if (frame->side_data[i]->type == AV_FRAME_DATA_A53_CC) + break; + if (i >= frame->nb_side_data) + return 0; + sd = frame->side_data[i]; + if ((ret = av_new_packet(&lavfi->subcc_packet, sd->size)) < 0) + return ret; + memcpy(lavfi->subcc_packet.data, sd->data, sd->size); + lavfi->subcc_packet.stream_index = stream_idx; + lavfi->subcc_packet.pts = frame->pts; + lavfi->subcc_packet.pos = av_frame_get_pkt_pos(frame); + return 0; +} + static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt) { LavfiContext *lavfi = avctx->priv_data; @@ -321,9 +377,17 @@ static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt) int ret, i; int size = 0; + if (lavfi->subcc_packet.size) { + *pkt = lavfi->subcc_packet; + av_init_packet(&lavfi->subcc_packet); + lavfi->subcc_packet.size = 0; + lavfi->subcc_packet.data = NULL; + return pkt->size; + } + /* iterate through all the graph sinks. Select the sink with the * minimum PTS */ - for (i = 0; i < avctx->nb_streams; i++) { + for (i = 0; i < lavfi->nb_sinks; i++) { AVRational tb = lavfi->sinks[i]->inputs[0]->time_base; double d; int ret; @@ -397,6 +461,12 @@ static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt) av_bprint_finalize(&meta_buf, NULL); } + if ((ret = create_subcc_packet(avctx, frame, min_pts_sink_idx)) < 0) { + av_frame_unref(frame); + av_packet_unref(pkt); + return ret; + } + pkt->stream_index = stream_idx; pkt->pts = frame->pts; pkt->pos = av_frame_get_pkt_pos(frame); -- 1.8.1.4
>From 1e52ce4b9b47f0cc630cfaee783ea0f44e212476 Mon Sep 17 00:00:00 2001 From: Anshul Maheshwari <anshul.ffm...@gmail.com> Date: Mon, 1 Dec 2014 22:25:04 +0530 Subject: [PATCH 2/2] Adding closed caption decoder --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/ccaption_dec.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 245 insertions(+) create mode 100644 libavcodec/ccaption_dec.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index fa0f53d..bbc516d 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -173,6 +173,7 @@ OBJS-$(CONFIG_BRENDER_PIX_DECODER) += brenderpix.o OBJS-$(CONFIG_C93_DECODER) += c93.o OBJS-$(CONFIG_CAVS_DECODER) += cavs.o cavsdec.o cavsdsp.o \ cavsdata.o mpeg12data.o +OBJS-$(CONFIG_CCAPTION_DECODER) += ccaption_dec.o OBJS-$(CONFIG_CDGRAPHICS_DECODER) += cdgraphics.o OBJS-$(CONFIG_CDXL_DECODER) += cdxl.o OBJS-$(CONFIG_CINEPAK_DECODER) += cinepak.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 0d39d33..8c07388 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -480,6 +480,7 @@ void avcodec_register_all(void) /* subtitles */ REGISTER_ENCDEC (SSA, ssa); REGISTER_ENCDEC (ASS, ass); + REGISTER_DECODER(CCAPTION, ccaption); REGISTER_ENCDEC (DVBSUB, dvbsub); REGISTER_ENCDEC (DVDSUB, dvdsub); REGISTER_DECODER(JACOSUB, jacosub); diff --git a/libavcodec/ccaption_dec.c b/libavcodec/ccaption_dec.c new file mode 100644 index 0000000..a665f64 --- /dev/null +++ b/libavcodec/ccaption_dec.c @@ -0,0 +1,243 @@ +#include "avcodec.h" +#include "libavcodec/ass.h" + +typedef struct CCaptionSubContext { + int parity_table[256]; + int row_cnt; + char *buffer; + int index; + int data_len; + int buf_len; + /* erase display memory */ + int edm; + int rollup; + int64_t start_time; + int64_t end_time; + char prev_cmd[2]; +}CCaptionSubContext; + +static unsigned int av_always_inline is_oddparity1p7(unsigned int val) +{ +//#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) +#if 0 + asm goto ( + "and $0x7F, %0\n\t" + "jnp %l1 \n\t" + :/* no output */ + :"r"(val) + :"cc" + :odd + ); + val = !(val&0x8); +odd: + val = !!(val&0x80); +#else + int ones = 0; + int i = 0; + + for (i = 0; i < 7; i++) { + if (val & (1 << i)) + ones++; + } + val = ones & 1; +#endif + return val; +} + +static void build_parity_table(int *parity_table) +{ + unsigned int byte; + int parity_v; + for (byte = 0; byte <= 127; byte++) { + parity_v = is_oddparity1p7(byte); + parity_table[byte] = parity_v; + parity_table[byte | 0x80] = !parity_v; + } +} + +static av_cold int init_decoder(AVCodecContext *avctx) +{ + + CCaptionSubContext *ctx = avctx->priv_data; + + build_parity_table(ctx->parity_table); + ctx->row_cnt = 0; + ctx->buf_len = 256; + ctx->buffer = av_realloc(NULL, ctx->buf_len); + ctx->index = 0; + ctx->edm = 0; + /* taking by default roll up to 2 */ + ctx->rollup = 2; + ctx->data_len = 0; + memset(ctx->prev_cmd,0,2); + ff_ass_subtitle_header_default(avctx); + return 0; +} + +static av_cold int close_decoder(AVCodecContext *avctx) +{ + CCaptionSubContext *ctx = avctx->priv_data; + av_free(ctx->buffer); + return 0; +} + +/** + * This function after validating parity bit, also remove it from data pair. + */ +static int validate_cc_data_pair (unsigned char *cc_data_pair, int *parity_table) +{ + unsigned char cc_valid = (*cc_data_pair & 4) >>2; + unsigned char cc_type = *cc_data_pair & 3; + + if (!cc_valid) + return -1; + + // if EIA-608 data then verify parity. + if (cc_type==0 || cc_type==1) { + if (!parity_table[cc_data_pair[2]]) { + // If the second byte doesn't pass parity, ignore pair + return -1; + } + if (!parity_table[cc_data_pair[1]]) { + // The first byte doesn't pass parity, we replace it with a solid blank + // and process the pair. + 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 -1; + + //skip 708 data + if(cc_type == 3 || cc_type == 2 ) + return -1; + + /* remove parity bit */ + cc_data_pair[1] &= 0x7F; + cc_data_pair[2] &= 0x7F; + + + return 0; + +} + +/** + * @param pts it is required to set end time + */ +static void av_always_inline handle_edm(CCaptionSubContext *ctx,int64_t pts) +{ + ctx->edm = 1; + ctx->data_len = ctx->index; + ctx->index = 0; + ctx->end_time = pts; +} +static int process_cc608(CCaptionSubContext *ctx, int64_t pts, unsigned char hi, unsigned char lo) +{ + + if ( hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) { + /* ignore redundant command */ + } else if ((hi == 0x14 || hi == 0x15 || hi == 0x1C) && lo == 0x2C) { + /* erase display memory */ + ctx->buffer[ctx->index] = 0; + handle_edm(ctx, pts); + } else if ( (hi == 0x14 || hi == 0x15 || hi == 0x1C) && lo == 0x25 ) { + ctx->rollup = 2; + } else if ( (hi == 0x14 || hi == 0x15 || hi == 0x1C) && lo == 0x26 ) { + ctx->rollup = 3; + } else if ( (hi == 0x14 || hi == 0x15 || hi == 0x1C) && lo == 0x27 ) { + ctx->rollup = 4; + } else if ( (hi == 0x14 || hi == 0x15 || hi == 0x1C) && lo == 0x2D ) { + /* carriage return */ + if(ctx->buf_len < ctx->index + 3 ) { + ctx->buf_len *= 2; + ctx->buffer = av_realloc(ctx->buffer, ctx->buf_len); + } + ctx->row_cnt++; + if(ctx->row_cnt == ctx->rollup) { + ctx->row_cnt = 0; + ctx->buffer[ctx->index ] = 0; + handle_edm(ctx, pts); + } + /* If there is no data or end of data then no point using cr */ + if(ctx->index) { + ctx->buffer[ctx->index] = '\\'; + ctx->index++; + ctx->buffer[ctx->index] = 'N'; + ctx->index++; + } + } else if (hi>=0x20) { + /* Standard characters (always in pairs) */ + if(ctx->buf_len < ctx->index + 3 ) { + ctx->buf_len *= 2; + ctx->buffer = av_realloc(ctx->buffer, ctx->buf_len); + } + if(!ctx->index) + ctx->start_time = pts; + + ctx->buffer[ctx->index] = hi; + ctx->index++; + if(lo) { + ctx->buffer[ctx->index] = lo; + ctx->index++; + } + /* reset prev command since character can repeat */ + ctx->prev_cmd[0] = 0; + ctx->prev_cmd[1] = 0; + } else { + /* Ignoring all other non data code */ + } + + /* set prev command */ + ctx->prev_cmd[0] = hi; + ctx->prev_cmd[1] = lo; + + return 0; + +} +static int decode(AVCodecContext *avctx, + void *data, int *got_sub, + AVPacket *avpkt) { + CCaptionSubContext *ctx = avctx->priv_data; + AVSubtitle *sub = data; + unsigned char *bptr = avpkt->data; + int len = avpkt->size; + int ret = 0; + int i; + + for (i = 0; i < len; i += 3) { + unsigned char cc_type = *(bptr + i) & 3; + if (validate_cc_data_pair( bptr + i, ctx->parity_table ) ) + continue; + /* ignoring data field 1 */ + if(cc_type == 1) + continue; + else + process_cc608(ctx, avpkt->pts, *(bptr + i + 1), *(bptr + i + 2)); + } + if(ctx->edm) + { + 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 }); + ret = ff_ass_add_rect(sub, ctx->buffer, start_time, end_time - start_time , 0); + if (ret < 0) + return ret; + sub->pts = av_rescale_q(ctx->start_time, avctx->time_base, AV_TIME_BASE_Q); + ctx->edm = 0; + } + + *got_sub = sub->num_rects > 0; + return 0; +} + +AVCodec ff_ccaption_decoder = { + .name = "cc_dec", + .long_name = NULL_IF_CONFIG_SMALL("Closed Caption Decoder"), + .type = AVMEDIA_TYPE_SUBTITLE, + .id = AV_CODEC_ID_EIA_608, + .priv_data_size = sizeof(CCaptionSubContext), + .init = init_decoder, + .close = close_decoder, + .decode = decode, +}; -- 1.8.1.4
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel