From 74bb91aa5b0207a7a0c19256b1d4dfe8af66f312 Mon Sep 17 00:00:00 2001 From: Anshul Maheshwari Date: Tue, 6 Jan 2015 21:19:39 +0530 Subject: [PATCH] fixing minor bugs --- linux/Makefile | 1 + src/lib_ccx/608_sami.c | 5 +++ src/lib_ccx/608_smptett.c | 5 +++ src/lib_ccx/608_srt.c | 6 ++- src/lib_ccx/ccx_common_constants.h | 2 + src/lib_ccx/ccx_decoders_608.c | 2 +- src/lib_ccx/ccx_encoders_helpers.c | 2 +- src/lib_ccx/dvb_subtitle_decoder.c | 14 +++--- src/lib_ccx/ffmpeg_intgr.c | 2 +- src/lib_ccx/spupng_encoder.c | 71 ++++++++++++++++++++++++------ 10 files changed, 87 insertions(+), 23 deletions(-) diff --git a/linux/Makefile b/linux/Makefile index ea61c2fb..9471b4c5 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -45,6 +45,7 @@ ifeq ($(ENABLE_OCR),yes) CFLAGS+=-I/usr/local/include/tesseract -I/usr/local/include/leptonica CFLAGS+=-DENABLE_OCR LDFLAGS+= $(shell pkg-config --libs tesseract) +LDFLAGS+= $(shell pkg-config --libs lept) endif ifeq ($(ENABLE_FFMPEG),yes) CFLAGS+=-DENABLE_FFMPEG diff --git a/src/lib_ccx/608_sami.c b/src/lib_ccx/608_sami.c index ab945621..61785001 100644 --- a/src/lib_ccx/608_sami.c +++ b/src/lib_ccx/608_sami.c @@ -115,6 +115,11 @@ int write_cc_bitmap_as_sami(struct cc_subtitle *sub, struct encoder_ctx *context ms_start = sub->start_time + context->subs_delay; ms_end = sub->end_time - 1; } + else if ( context->prev_start == -1 && (sub->flags & SUB_EOD_MARKER) ) + { + ms_start = 1 + context->subs_delay; + ms_end = sub->start_time - 1; + } if(sub->nb_data == 0 ) return 0; diff --git a/src/lib_ccx/608_smptett.c b/src/lib_ccx/608_smptett.c index 884bd35b..ee11c5e8 100644 --- a/src/lib_ccx/608_smptett.c +++ b/src/lib_ccx/608_smptett.c @@ -141,6 +141,11 @@ int write_cc_bitmap_as_smptett(struct cc_subtitle *sub, struct encoder_ctx *cont ms_start = sub->start_time + context->subs_delay; ms_end = sub->end_time - 1; } + else if (context->prev_start == -1 && (sub->flags & SUB_EOD_MARKER)) + { + ms_start = 1 + context->subs_delay; + ms_end = sub->start_time - 1; + } if(sub->nb_data == 0 ) return 0; diff --git a/src/lib_ccx/608_srt.c b/src/lib_ccx/608_srt.c index 18a51ef5..ed703dae 100644 --- a/src/lib_ccx/608_srt.c +++ b/src/lib_ccx/608_srt.c @@ -75,7 +75,6 @@ void write_stringz_as_srt(char *string, struct encoder_ctx *context, LLONG ms_st int write_cc_bitmap_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context) { - struct spupng_t *sp = (struct spupng_t *)context->out->spupng_data; int x_pos, y_pos, width, height, i; int x, y, y_off, x_off, ret; uint8_t *pbuf; @@ -110,6 +109,11 @@ int write_cc_bitmap_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context) ms_start = sub->start_time; ms_end = sub->end_time; } + else if (context->prev_start == -1 && (sub->flags & SUB_EOD_MARKER)) + { + ms_start = 1; + ms_end = sub->start_time; + } if(sub->nb_data == 0 ) return 0; diff --git a/src/lib_ccx/ccx_common_constants.h b/src/lib_ccx/ccx_common_constants.h index ab03bfe4..8867d2c1 100644 --- a/src/lib_ccx/ccx_common_constants.h +++ b/src/lib_ccx/ccx_common_constants.h @@ -131,6 +131,7 @@ enum ccx_mpeg_descriptor CCX_MPEG_DSC_VBI_TELETEXT_DESCRIPTOR = 0x46, CCX_MPEG_DSC_TELETEXT_DESCRIPTOR = 0x56, CCX_MPEG_DSC_DVB_SUBTITLE = 0x59, + CCX_MPEG_DESC_DATA_COMP = 0xfd, }; @@ -227,6 +228,7 @@ enum ccx_code_type CCX_CODEC_ANY, CCX_CODEC_TELETEXT, CCX_CODEC_DVB, + CCX_CODEC_ISDB_CC, CCX_CODEC_NONE, }; /* diff --git a/src/lib_ccx/ccx_decoders_608.c b/src/lib_ccx/ccx_decoders_608.c index 71c1c0c3..a340cf56 100644 --- a/src/lib_ccx/ccx_decoders_608.c +++ b/src/lib_ccx/ccx_decoders_608.c @@ -952,7 +952,7 @@ void handle_pac(unsigned char c1, unsigned char c2, ccx_decoder_608_context *con memset(use_buffer->colors[j], context->settings.default_color, CCX_DECODER_608_SCREEN_WIDTH); memset(use_buffer->fonts[j], FONT_REGULAR, CCX_DECODER_608_SCREEN_WIDTH); use_buffer->characters[j][CCX_DECODER_608_SCREEN_WIDTH] = 0; - use_buffer->row_used[j]=0; + use_buffer->row_used[j] = 0; } } } diff --git a/src/lib_ccx/ccx_encoders_helpers.c b/src/lib_ccx/ccx_encoders_helpers.c index 118fbc7d..722a4e30 100644 --- a/src/lib_ccx/ccx_encoders_helpers.c +++ b/src/lib_ccx/ccx_encoders_helpers.c @@ -356,7 +356,7 @@ int add_built_in_words(void) * @param size size of each element * @param compar Comparison function, which is called with three argument * that point to the objects being compared and arg. -* @param arg argument passed as it is to compare function +* @param arg argument passed as it is, to compare function */ void shell_sort(void *base, int nb, size_t size, int(*compar)(const void*p1, const void *p2, void*arg), void *arg) { diff --git a/src/lib_ccx/dvb_subtitle_decoder.c b/src/lib_ccx/dvb_subtitle_decoder.c index 7838058c..5dc9b7d9 100644 --- a/src/lib_ccx/dvb_subtitle_decoder.c +++ b/src/lib_ccx/dvb_subtitle_decoder.c @@ -1440,19 +1440,21 @@ static int write_dvb_sub(void *dvb_ctx, struct cc_subtitle *sub) if (region && region->dirty) sub->nb_data++; } - sub->start_time = get_visible_start(); - sub->end_time = sub->start_time + ( ctx->time_out * 1000 ); - sub->flags |= SUB_EOD_MARKER; - sub->got_output = 1; if( sub->nb_data <= 0 ) { return 0; } + rect = malloc( sizeof(struct cc_bitmap) * sub->nb_data); if(!rect) { return -1; } + + sub->start_time = get_visible_start(); + sub->end_time = sub->start_time + ( ctx->time_out * 1000 ); + sub->flags |= SUB_EOD_MARKER; + sub->got_output = 1; sub->data = rect; for (display = ctx->display_list; display; display = display->next) { @@ -1528,7 +1530,7 @@ int dvbsub_decode(void *dvb_ctx, const unsigned char *buf, int buf_size, struct if (buf_size <= 6 || *buf != 0x0f) { - mprint("incomplete or broken packet"); + mprint("incomplete or broken packet\n"); return -1; } @@ -1547,7 +1549,7 @@ int dvbsub_decode(void *dvb_ctx, const unsigned char *buf, int buf_size, struct if (p_end - p < segment_length) { - mprint("incomplete or broken packet"); + mprint("incomplete or broken packet\n"); return -1; } diff --git a/src/lib_ccx/ffmpeg_intgr.c b/src/lib_ccx/ffmpeg_intgr.c index 0a74ec9c..54d1fcf4 100644 --- a/src/lib_ccx/ffmpeg_intgr.c +++ b/src/lib_ccx/ffmpeg_intgr.c @@ -74,7 +74,7 @@ void *init_ffmpeg(char *path) av_log_set_callback(log_cb); - ctx = av_malloc(sizeof(*ctx)); + ctx = av_mallocz(sizeof(*ctx)); if(!ctx) { av_log(NULL,AV_LOG_ERROR,"Not enough memory\n"); diff --git a/src/lib_ccx/spupng_encoder.c b/src/lib_ccx/spupng_encoder.c index 1a36a7a5..d42cf479 100644 --- a/src/lib_ccx/spupng_encoder.c +++ b/src/lib_ccx/spupng_encoder.c @@ -5,6 +5,7 @@ #include #ifdef ENABLE_OCR #include "ocr.h" +#undef OCR_DEBUG #endif #define CCPL (ccfont2_width / CCW * ccfont2_height / CCH) @@ -430,7 +431,12 @@ static int save_spupng(const char *filename, uint8_t *bitmap, int w, int h, return ret; } - +/** + * alpha value 255 means completely opaque + * alpha value 0 means completely transparent + * r g b all 0 means black + * r g b all 255 means white + */ int mapclut_paletee(png_color *palette, png_byte *alpha, uint32_t *clut, uint8_t depth) { @@ -441,6 +447,14 @@ int mapclut_paletee(png_color *palette, png_byte *alpha, uint32_t *clut, palette[i].blue = (clut[i] & 0xff); alpha[i] = ((clut[i] >> 24) & 0xff); } +#if OCR_DEBUG + ccx_common_logging.log_ftn("Colors present in original Image\n"); + for (int i = 0; i < depth; i++) + { + ccx_common_logging.log_ftn("%02d)r %03d g %03d b %03d a %03d\n", + i, palette[i].red, palette[i].green, palette[i].blue, alpha[i]); + } +#endif return 0; } @@ -519,6 +533,8 @@ int quantize_map(png_byte *alpha, png_color *palette, } memset(histogram, 0, nb_color * sizeof(uint32_t)); + + /* initializing intensity ordered table with serial order of unsorted color table */ for (int i = 0; i < nb_color; i++) { iot[i] = i; @@ -530,9 +546,21 @@ int quantize_map(png_byte *alpha, png_color *palette, { histogram[bitmap[i]]++; } + /* sorted in increasing order of intensity */ shell_sort((void*)iot, nb_color, sizeof(*iot), check_trans_tn_intensity, (void*)&ti); - /* using selection sort since need to find only max_color */ +#if OCR_DEBUG + ccx_common_logging.log_ftn("Intensity ordered table\n"); + for (int i = 0; i < nb_color; i++) + { + ccx_common_logging.log_ftn("%02d) map %02d hist %02d\n", + i, iot[i], histogram[iot[i]]); + } +#endif + /** + * using selection sort since need to find only max_color + * Hostogram becomes invalid in this loop + */ for (int i = 0; i < max_color; i++) { uint32_t max_val = 0; @@ -554,6 +582,14 @@ int quantize_map(png_byte *alpha, png_color *palette, histogram[iot[max_ind]] = 0; } +#if OCR_DEBUG + ccx_common_logging.log_ftn("max redundant intensities table\n"); + for (int i = 0; i < max_color; i++) + { + ccx_common_logging.log_ftn("%02d) mcit %02d\n", + i, mcit[i]); + } +#endif for (int i = 0, mxi = 0; i < nb_color; i++) { int step, inc; @@ -563,25 +599,33 @@ int quantize_map(png_byte *alpha, png_color *palette, continue; } inc = (mxi) ? -1 : 0; - step = mcit[mxi + inc] + ((mcit[mxi] - mcit[mxi + inc]) / 3); + step = mcit[mxi + inc] + ((mcit[mxi] - mcit[mxi + inc]) / 2); if (i <= step) { int index = iot[mcit[mxi + inc]]; - alpha[i] = alpha[index]; - palette[i].red = palette[index].red; - palette[i].blue = palette[index].blue; - palette[i].green = palette[index].green; + alpha[iot[i]] = alpha[index]; + palette[iot[i]].red = palette[index].red; + palette[iot[i]].blue = palette[index].blue; + palette[iot[i]].green = palette[index].green; } else { int index = iot[mcit[mxi]]; - alpha[i] = alpha[index]; - palette[i].red = palette[index].red; - palette[i].blue = palette[index].blue; - palette[i].green = palette[index].green; + alpha[iot[i]] = alpha[index]; + palette[iot[i]].red = palette[index].red; + palette[iot[i]].blue = palette[index].blue; + palette[iot[i]].green = palette[index].green; } } +#if OCR_DEBUG + ccx_common_logging.log_ftn("Colors present in quantized Image\n"); + for (int i = 0; i < nb_color; i++) + { + ccx_common_logging.log_ftn("%02d)r %03d g %03d b %03d a %03d\n", + i, palette[i].red, palette[i].green, palette[i].blue, alpha[i]); + } +#endif end: freep(&histogram); freep(&mcit); freep(&iot); @@ -685,10 +729,11 @@ int write_cc_bitmap_as_spupng(struct cc_subtitle *sub, struct encoder_ctx *conte ret = -1; goto end; } - /* TODO do rectangle, wise one color table should not be used for all rectangle */ + +#ifdef ENABLE_OCR + /* TODO do rectangle wise, one color table should not be used for all rectangles */ mapclut_paletee(palette, alpha, (uint32_t *)rect[0].data[1],rect[0].nb_colors); quantize_map(alpha, palette, pbuf, width*height, 3, rect[0].nb_colors); -#ifdef ENABLE_OCR str = ocr_bitmap(palette,alpha,pbuf,width,height); if(str && str[0]) {