From e9c088a86ba4d250a76a2ebaa40e1099a8bdf9cc Mon Sep 17 00:00:00 2001 From: AlexBratosin2001 Date: Wed, 28 Dec 2016 20:55:10 +0200 Subject: [PATCH 1/3] Fixed DVB subtitle timing and added -debugdvbsub parameter for DVB sub debug traces --- src/lib_ccx/activity.c | 2 +- src/lib_ccx/ccx_common_option.h | 1 + src/lib_ccx/ccx_decoders_structs.h | 3 +++ src/lib_ccx/ccx_encoders_common.h | 4 ++++ src/lib_ccx/dvb_subtitle_decoder.c | 29 ++++++++++++++++++++++++++--- src/lib_ccx/general_loop.c | 4 ++-- src/lib_ccx/lib_ccx.c | 16 ++++++++++++++++ src/lib_ccx/lib_ccx.h | 1 + src/lib_ccx/params.c | 5 +++++ 9 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/lib_ccx/activity.c b/src/lib_ccx/activity.c index 33b778fcb..5c585052c 100644 --- a/src/lib_ccx/activity.c +++ b/src/lib_ccx/activity.c @@ -16,7 +16,7 @@ void activity_progress (int percentage, int cur_min, int cur_sec) mprint ("\rStreaming | %02d:%02d", cur_min, cur_sec); else mprint ("\r%3d%% | %02d:%02d",percentage, cur_min, cur_sec); - if (ccx_options.pes_header_to_stdout) //For PES Header dumping + if (ccx_options.pes_header_to_stdout || ccx_options.dvb_debug_traces_to_stdout) //For PES Header dumping and DVB debug traces { printf("\n"); } diff --git a/src/lib_ccx/ccx_common_option.h b/src/lib_ccx/ccx_common_option.h index b8f8c1be4..a25f18c84 100644 --- a/src/lib_ccx/ccx_common_option.h +++ b/src/lib_ccx/ccx_common_option.h @@ -165,6 +165,7 @@ struct ccx_s_options // Options from user parameters LLONG subs_delay; // ms to delay (or advance) subs int cc_to_stdout; // If this is set to 1, the stdout will be flushed when data was written to the screen during a process_608 call. int pes_header_to_stdout; //If this is set to 1, the PES Header will be printed to console (debugging purposes) + int dvb_debug_traces_to_stdout; // If 1, DVB subtitle debug traces will be outputted to console int multiprogram; int out_interval; #ifdef WITH_LIBCURL diff --git a/src/lib_ccx/ccx_decoders_structs.h b/src/lib_ccx/ccx_decoders_structs.h index b1cfdfad7..675977795 100644 --- a/src/lib_ccx/ccx_decoders_structs.h +++ b/src/lib_ccx/ccx_decoders_structs.h @@ -196,6 +196,9 @@ struct lib_cc_decode struct ccx_decoder_vbi_ctx *vbi_decoder; int (*writedata)(const unsigned char *data, int length, void *private_data, struct cc_subtitle *sub); + + //dvb subtitle related + struct lib_cc_decode *prev; }; #endif diff --git a/src/lib_ccx/ccx_encoders_common.h b/src/lib_ccx/ccx_encoders_common.h index 2af17d875..733ae6da3 100644 --- a/src/lib_ccx/ccx_encoders_common.h +++ b/src/lib_ccx/ccx_encoders_common.h @@ -126,6 +126,10 @@ struct encoder_ctx LLONG sbs_time_from; // Used by the split-by-sentence code to know when the current block starts... LLONG sbs_time_trim; // ... and ends size_t sbs_capacity; + + //for dvb subs + struct encoder_ctx* prev; + int write_previous; }; #define INITIAL_ENC_BUFFER_CAPACITY 2048 diff --git a/src/lib_ccx/dvb_subtitle_decoder.c b/src/lib_ccx/dvb_subtitle_decoder.c index 2e5dbb6ae..b379f9450 100644 --- a/src/lib_ccx/dvb_subtitle_decoder.c +++ b/src/lib_ccx/dvb_subtitle_decoder.c @@ -1561,7 +1561,7 @@ static int write_dvb_sub(struct lib_cc_decode *dec_ctx, struct cc_subtitle *sub) * * @return -1 on error */ -int dvbsub_decode(struct lib_cc_decode *dec_ctx, const unsigned char *buf, int buf_size, struct cc_subtitle *sub) +int dvbsub_decode(struct encoder_ctx *enc_ctx, struct lib_cc_decode *dec_ctx, const unsigned char *buf, int buf_size, struct cc_subtitle *sub) { DVBSubContext *ctx = (DVBSubContext *) dec_ctx->private_data; const uint8_t *p, *p_end; @@ -1601,6 +1601,13 @@ int dvbsub_decode(struct lib_cc_decode *dec_ctx, const unsigned char *buf, int b if (page_id == ctx->composition_id || page_id == ctx->ancillary_id || ctx->composition_id == -1 || ctx->ancillary_id == -1) { + if (ccx_options.dvb_debug_traces_to_stdout) + { + //debug traces + printf("DVBSUB - PTS: %d, ", dec_ctx->timing->current_pts); + printf("FTS: %d, ", dec_ctx->timing->fts_now); + printf("SEGMENT TYPE : %d \n", segment_type); + } switch (segment_type) { case DVBSUB_PAGE_SEGMENT: @@ -1625,8 +1632,24 @@ int dvbsub_decode(struct lib_cc_decode *dec_ctx, const unsigned char *buf, int b dvbsub_parse_display_definition_segment(ctx, p, segment_length); break; - case DVBSUB_DISPLAY_SEGMENT: - write_dvb_sub(dec_ctx, sub); + case DVBSUB_DISPLAY_SEGMENT: //when we get a display segment, we save the current page + if (enc_ctx->write_previous) //this condition is used for the first subtitle - write_previous will be 0 first so we don't encode a non-existing previous sub + { + sub->prev->end_time = (dec_ctx->timing->current_pts - dec_ctx->timing->min_pts) / (MPEG_CLOCK_FREQ / 1000); //we set the end time of the previous sub the current pts + encode_sub(enc_ctx->prev, sub->prev); //we encode it + enc_ctx->srt_counter = enc_ctx->prev->srt_counter; //for dvb subs we need to update the current srt counter because we always encode the previous subtitle (and the counter is increased for the previous context) + sub->prev->got_output = 0; + free(sub->prev); //we free it to avoid memory leak + enc_ctx->write_previous = 0; + + } + memcpy(enc_ctx->prev, enc_ctx, sizeof(struct encoder_ctx)); //we save the current encoder context + sub->prev = malloc(sizeof(struct cc_subtitle)); //we allocate space for the previous subtitle of the current subtitle + memcpy(sub->prev, sub, sizeof(struct cc_subtitle)); //we save the current subtitle + memcpy(dec_ctx->prev, dec_ctx, sizeof(struct lib_cc_decode)); //we save the current decoder context + sub->prev->start_time = (dec_ctx->timing->current_pts - dec_ctx->timing->min_pts) / (MPEG_CLOCK_FREQ / 1000); //we set the start time of the previous sub the current pts + write_dvb_sub(dec_ctx->prev, sub->prev); //we write the current dvb sub to update decoder context + enc_ctx->write_previous = 1; //we update our boolean value so next time the program reaches this block of code, it encodes the previous sub got_segment |= 16; break; default: diff --git a/src/lib_ccx/general_loop.c b/src/lib_ccx/general_loop.c index 642b3bf73..6f38f4818 100644 --- a/src/lib_ccx/general_loop.c +++ b/src/lib_ccx/general_loop.c @@ -645,7 +645,7 @@ int process_data(struct encoder_ctx *enc_ctx, struct lib_cc_decode *dec_ctx, str } else if(data_node->bufferdatatype == CCX_DVB_SUBTITLE) { - dvbsub_decode(dec_ctx, data_node->buffer + 2, data_node->len - 2, dec_sub); + dvbsub_decode(enc_ctx, dec_ctx, data_node->buffer + 2, data_node->len - 2, dec_sub); set_fts(dec_ctx->timing); got = data_node->len; } @@ -751,7 +751,7 @@ int process_data(struct encoder_ctx *enc_ctx, struct lib_cc_decode *dec_ctx, str } } - if (dec_sub->got_output) + if (data_node->bufferdatatype != CCX_DVB_SUBTITLE && dec_sub->got_output) { encode_sub(enc_ctx, dec_sub); dec_sub->got_output = 0; diff --git a/src/lib_ccx/lib_ccx.c b/src/lib_ccx/lib_ccx.c index 549138453..e70d55581 100644 --- a/src/lib_ccx/lib_ccx.c +++ b/src/lib_ccx/lib_ccx.c @@ -170,6 +170,7 @@ struct lib_ccx_ctx* init_libraries(struct ccx_s_options *opt) ctx->cc_to_stdout = opt->cc_to_stdout; ctx->pes_header_to_stdout = opt->pes_header_to_stdout; + ctx->dvb_debug_traces_to_stdout = opt->dvb_debug_traces_to_stdout; ctx->hauppauge_mode = opt->hauppauge_mode; ctx->live_stream = opt->live_stream; @@ -318,6 +319,13 @@ struct lib_cc_decode *update_decoder_list_cinfo(struct lib_ccx_ctx *ctx, struct fatal(EXIT_NOT_ENOUGH_MEMORY, "update_decoder_list_cinfo: Not enough memory allocating dec_ctx ((multiprogram == true)\n"); list_add_tail( &(dec_ctx->list), &(ctx->dec_ctx_head) ); } + if (cinfo) + { + if (cinfo->codec == CCX_CODEC_DVB) + { + dec_ctx->prev = malloc(sizeof(struct lib_cc_decode)); + } + } return dec_ctx; } @@ -406,6 +414,14 @@ struct encoder_ctx *update_encoder_list_cinfo(struct lib_ccx_ctx *ctx, struct ca freep(&ccx_options.enc_cfg.output_filename); } freep(&extension); + if (cinfo) + { + if (cinfo->codec == CCX_CODEC_DVB) + { + enc_ctx->write_previous = 0; + enc_ctx->prev = malloc(sizeof(struct encoder_ctx)); + } + } return enc_ctx; } diff --git a/src/lib_ccx/lib_ccx.h b/src/lib_ccx/lib_ccx.h index 12cc2329d..e2dc69134 100644 --- a/src/lib_ccx/lib_ccx.h +++ b/src/lib_ccx/lib_ccx.h @@ -103,6 +103,7 @@ struct lib_ccx_ctx int cc_to_stdout; // If 1, captions go to stdout instead of file int pes_header_to_stdout; //If 1, PES Header data will be outputted to console + int dvb_debug_traces_to_stdout; // If 1, DVB subtitle debug traces will be outputted to console LLONG subs_delay; // ms to delay (or advance) subs diff --git a/src/lib_ccx/params.c b/src/lib_ccx/params.c index a598e13f9..0682f2e78 100644 --- a/src/lib_ccx/params.c +++ b/src/lib_ccx/params.c @@ -1650,6 +1650,11 @@ int parse_parameters (struct ccx_s_options *opt, int argc, char *argv[]) opt->pes_header_to_stdout = 1; continue; } + if (strcmp(argv[i], "-debugdvbsub") == 0) + { + opt->dvb_debug_traces_to_stdout = 1; + continue; + } if (strcmp (argv[i],"-quiet")==0) { opt->messages_target=0; From 121ac2bdfe12c40c2702fad2ab476f8a845d3ed9 Mon Sep 17 00:00:00 2001 From: AlexBratosin2001 Date: Wed, 28 Dec 2016 21:05:54 +0200 Subject: [PATCH 2/3] Fixed DVB subtitle timing and added -debugdvbsub parameter for DVB sub debug traces --- src/lib_ccx/ccx_decoders_common.c | 1 + src/lib_ccx/ccx_encoders_common.c | 3 ++- src/lib_ccx/ccx_encoders_srt.c | 16 +--------------- src/lib_ccx/dvb_subtitle_decoder.c | 2 +- src/lib_ccx/general_loop.c | 1 + 5 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/lib_ccx/ccx_decoders_common.c b/src/lib_ccx/ccx_decoders_common.c index 9f09bd637..7c57ec743 100644 --- a/src/lib_ccx/ccx_decoders_common.c +++ b/src/lib_ccx/ccx_decoders_common.c @@ -220,6 +220,7 @@ void dinit_cc_decode(struct lib_cc_decode **ctx) ccx_decoder_608_dinit_library(&lctx->context_cc608_field_1); ccx_decoder_608_dinit_library(&lctx->context_cc608_field_2); dinit_timing_ctx(&lctx->timing); + freep(&lctx->prev); freep(ctx); } diff --git a/src/lib_ccx/ccx_encoders_common.c b/src/lib_ccx/ccx_encoders_common.c index adc25ba11..60b6f60c1 100644 --- a/src/lib_ccx/ccx_encoders_common.c +++ b/src/lib_ccx/ccx_encoders_common.c @@ -892,7 +892,8 @@ void dinit_encoder(struct encoder_ctx **arg, LLONG current_fts) freep(&ctx->subline); freep(&ctx->buffer); ctx->capacity = 0; - freep(arg); + freep(&ctx->prev); + freep(arg); } struct encoder_ctx *init_encoder(struct encoder_cfg *opt) diff --git a/src/lib_ccx/ccx_encoders_srt.c b/src/lib_ccx/ccx_encoders_srt.c index 598f81fdc..2fccafe66 100644 --- a/src/lib_ccx/ccx_encoders_srt.c +++ b/src/lib_ccx/ccx_encoders_srt.c @@ -91,23 +91,9 @@ int write_cc_bitmap_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context) int i = 0; char *str; - //if (context->prev_start != -1 && (sub->flags & SUB_EOD_MARKER)) - //{ - // ms_start = context->prev_start; - // ms_end = sub->start_time; - //} - //else if ( !(sub->flags & SUB_EOD_MARKER)) - //{ - // 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; - //} ms_start = sub->start_time; ms_end = sub->end_time; + if(sub->nb_data == 0 ) return 0; diff --git a/src/lib_ccx/dvb_subtitle_decoder.c b/src/lib_ccx/dvb_subtitle_decoder.c index b379f9450..04f83cb95 100644 --- a/src/lib_ccx/dvb_subtitle_decoder.c +++ b/src/lib_ccx/dvb_subtitle_decoder.c @@ -1639,7 +1639,7 @@ int dvbsub_decode(struct encoder_ctx *enc_ctx, struct lib_cc_decode *dec_ctx, co encode_sub(enc_ctx->prev, sub->prev); //we encode it enc_ctx->srt_counter = enc_ctx->prev->srt_counter; //for dvb subs we need to update the current srt counter because we always encode the previous subtitle (and the counter is increased for the previous context) sub->prev->got_output = 0; - free(sub->prev); //we free it to avoid memory leak + freep(sub->prev); //we free it to avoid memory leak enc_ctx->write_previous = 0; } diff --git a/src/lib_ccx/general_loop.c b/src/lib_ccx/general_loop.c index 6f38f4818..ef6edc4a5 100644 --- a/src/lib_ccx/general_loop.c +++ b/src/lib_ccx/general_loop.c @@ -872,6 +872,7 @@ void general_loop(struct lib_ccx_ctx *ctx) enc_ctx = update_encoder_list_cinfo(ctx, cinfo); dec_ctx = update_decoder_list_cinfo(ctx, cinfo); dec_ctx->dtvcc->encoder = (void *)enc_ctx; //WARN: otherwise cea-708 will not work + if (enc_ctx) enc_ctx->timing = dec_ctx->timing; From a09a7e4930ad153ce6aacea4eda14df41f212146 Mon Sep 17 00:00:00 2001 From: AlexBratosin2001 Date: Wed, 28 Dec 2016 21:35:26 +0200 Subject: [PATCH 3/3] Fixed DVB subtitle timing and added -debugdvbsub parameter for DVB sub debug traces. --- src/lib_ccx/dvb_subtitle_decoder.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib_ccx/dvb_subtitle_decoder.h b/src/lib_ccx/dvb_subtitle_decoder.h index a56777980..4cdf9a342 100644 --- a/src/lib_ccx/dvb_subtitle_decoder.h +++ b/src/lib_ccx/dvb_subtitle_decoder.h @@ -55,7 +55,7 @@ int dvbsub_close_decoder(void **dvb_ctx); * * @return -1 on error */ -int dvbsub_decode(struct lib_cc_decode *dec_ctx, const unsigned char *buf, int buf_size, struct cc_subtitle *sub); +int dvbsub_decode(struct encoder_ctx *enc_ctx, struct lib_cc_decode *dec_ctx, const unsigned char *buf, int buf_size, struct cc_subtitle *sub); /** * @func parse_dvb_description