From 609a53f373b0ecf97f919babf6b73d7205310f34 Mon Sep 17 00:00:00 2001 From: GAURAV KARMAKAR Date: Fri, 19 Dec 2025 13:27:08 +0530 Subject: [PATCH 1/4] [BUG] -out=spupng with EIA608/teletext: offset values in XML may be not correct #893 --- src/_deps/corrosion-src | 1 + src/lib_ccx/ccx_encoders_common.c | 1 + src/lib_ccx/ccx_encoders_common.h | 1 + src/lib_ccx/ccx_encoders_spupng.c | 15 +++++++++++++++ src/lib_ccx/ccx_encoders_structs.h | 2 ++ 5 files changed, 20 insertions(+) create mode 160000 src/_deps/corrosion-src diff --git a/src/_deps/corrosion-src b/src/_deps/corrosion-src new file mode 160000 index 000000000..b1fab7216 --- /dev/null +++ b/src/_deps/corrosion-src @@ -0,0 +1 @@ +Subproject commit b1fab721655c5c4b1b08a083d3cd29f163af75d0 diff --git a/src/lib_ccx/ccx_encoders_common.c b/src/lib_ccx/ccx_encoders_common.c index ac2af699a..58c0b4ecf 100644 --- a/src/lib_ccx/ccx_encoders_common.c +++ b/src/lib_ccx/ccx_encoders_common.c @@ -767,6 +767,7 @@ struct encoder_ctx *init_encoder(struct encoder_cfg *opt) return NULL; } ctx->in_fileformat = opt->in_format; + ctx->is_pal = (opt->in_format == 2); /** used in case of SUB_EOD_MARKER */ ctx->prev_start = -1; diff --git a/src/lib_ccx/ccx_encoders_common.h b/src/lib_ccx/ccx_encoders_common.h index 4540de681..67026a7a8 100644 --- a/src/lib_ccx/ccx_encoders_common.h +++ b/src/lib_ccx/ccx_encoders_common.h @@ -169,6 +169,7 @@ struct encoder_ctx // OCR in SPUPNG int nospupngocr; + int is_pal; }; #define INITIAL_ENC_BUFFER_CAPACITY 2048 diff --git a/src/lib_ccx/ccx_encoders_spupng.c b/src/lib_ccx/ccx_encoders_spupng.c index 1bfe3f248..e49c4ae34 100644 --- a/src/lib_ccx/ccx_encoders_spupng.c +++ b/src/lib_ccx/ccx_encoders_spupng.c @@ -993,6 +993,8 @@ int spupng_export_string2png(struct spupng_t *sp, char *str, FILE *output) */ // Save image + sp->img_w = canvas_width; + sp->img_h = canvas_height; write_image(buffer, output, canvas_width, canvas_height); free(tmp); free(buffer); @@ -1083,6 +1085,18 @@ int eia608_to_str(struct encoder_ctx *context, struct eia608_screen *data, char // string needs to be in UTF-8 encoding. // This function will take care of encoding. +static void calculate_spupng_offsets(struct spupng_t *sp, struct encoder_ctx *ctx) +{ + int screen_w = 720; + int screen_h = ctx->is_pal ? 576 : 480; + + sp->xOffset = (screen_w - sp->img_w) / 2; + sp->yOffset = (screen_h - sp->img_h) / 2; + + // SPU / DVD requires even yOffset (interlacing) + if (sp->yOffset & 1) + sp->yOffset++; +} int spupng_write_string(struct spupng_t *sp, char *string, LLONG start_time, LLONG end_time, struct encoder_ctx *context) { @@ -1101,6 +1115,7 @@ int spupng_write_string(struct spupng_t *sp, char *string, LLONG start_time, LLO } // free(string_utf32); fclose(sp->fppng); + calculate_spupng_offsets(sp, context); write_sputag_open(sp, start_time, end_time); write_spucomment(sp, string); write_sputag_close(sp); diff --git a/src/lib_ccx/ccx_encoders_structs.h b/src/lib_ccx/ccx_encoders_structs.h index efecbb722..9b0cf036b 100644 --- a/src/lib_ccx/ccx_encoders_structs.h +++ b/src/lib_ccx/ccx_encoders_structs.h @@ -39,6 +39,8 @@ struct spupng_t int fileIndex; int xOffset; int yOffset; + int img_w; + int img_h; }; #endif From 2b708c4a31100b245158ff21e0c6af5cb99fa97c Mon Sep 17 00:00:00 2001 From: GAURAV KARMAKAR Date: Sun, 21 Dec 2025 19:20:28 +0530 Subject: [PATCH 2/4] Enhance SPUPNG offset calculations and XML tag handling in EIA608 encoder - Introduced a forward declaration for . - Updated to calculate and set image dimensions before writing XML tags. - Adjusted offset calculations based on screen size for better alignment of subtitles. - Improved handling of the opening XML tag based on subtitle data presence. --- src/lib_ccx/ccx_encoders_spupng.c | 39 ++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/lib_ccx/ccx_encoders_spupng.c b/src/lib_ccx/ccx_encoders_spupng.c index e49c4ae34..9770a228a 100644 --- a/src/lib_ccx/ccx_encoders_spupng.c +++ b/src/lib_ccx/ccx_encoders_spupng.c @@ -251,6 +251,9 @@ void set_spupng_offset(void *ctx, int x, int y) sp->xOffset = x; sp->yOffset = y; } + +// Forward declaration for calculate_spupng_offsets +static void calculate_spupng_offsets(struct spupng_t *sp, struct encoder_ctx *ctx); int save_spupng(const char *filename, uint8_t *bitmap, int w, int h, png_color *palette, png_byte *alpha, int nb_color) { @@ -384,7 +387,7 @@ int write_cc_bitmap_as_spupng(struct cc_subtitle *sub, struct encoder_ctx *conte struct cc_bitmap *rect; png_color *palette = NULL; png_byte *alpha = NULL; - int wrote_opentag = 1; + int wrote_opentag = 0; // Track if we actually wrote the tag x_pos = -1; y_pos = -1; @@ -395,13 +398,11 @@ int write_cc_bitmap_as_spupng(struct cc_subtitle *sub, struct encoder_ctx *conte return 0; inc_spupng_fileindex(sp); - write_sputag_open(sp, sub->start_time, sub->end_time - 1); if (sub->nb_data == 0 && (sub->flags & SUB_EOD_MARKER)) { context->prev_start = -1; - if (wrote_opentag) - write_sputag_close(sp); + // No subtitle data, skip writing return 0; } rect = sub->data; @@ -440,7 +441,13 @@ int write_cc_bitmap_as_spupng(struct cc_subtitle *sub, struct encoder_ctx *conte } } filename = get_spupng_filename(sp); - set_spupng_offset(sp, x_pos, y_pos); + + // Set image dimensions for offset calculation + sp->img_w = width; + sp->img_h = height; + + // Calculate centered offsets based on screen size (PAL/NTSC) + calculate_spupng_offsets(sp, context); if (sub->flags & SUB_EOD_MARKER) context->prev_start = sub->start_time; pbuf = (uint8_t *)malloc(width * height); @@ -475,6 +482,15 @@ int write_cc_bitmap_as_spupng(struct cc_subtitle *sub, struct encoder_ctx *conte /* TODO do rectangle wise, one color table should not be used for all rectangles */ mapclut_paletee(palette, alpha, (uint32_t *)rect[0].data1, rect[0].nb_colors); + + // Save PNG file first + save_spupng(filename, pbuf, width, height, palette, alpha, rect[0].nb_colors); + freep(&pbuf); + + // Write XML tag with calculated centered offsets + write_sputag_open(sp, sub->start_time, sub->end_time - 1); + wrote_opentag = 1; // Mark that we wrote the tag + #ifdef ENABLE_OCR if (!context->nospupngocr) { @@ -487,8 +503,6 @@ int write_cc_bitmap_as_spupng(struct cc_subtitle *sub, struct encoder_ctx *conte } } #endif - save_spupng(filename, pbuf, width, height, palette, alpha, rect[0].nb_colors); - freep(&pbuf); end: if (wrote_opentag) @@ -1088,7 +1102,16 @@ int eia608_to_str(struct encoder_ctx *context, struct eia608_screen *data, char static void calculate_spupng_offsets(struct spupng_t *sp, struct encoder_ctx *ctx) { int screen_w = 720; - int screen_h = ctx->is_pal ? 576 : 480; + int screen_h; + + /* Teletext is always PAL */ + if (ctx->in_fileformat == 2) { + screen_h = 576; + } else if (ctx->is_pal) { + screen_h = 576; + } else { + screen_h = 480; + } sp->xOffset = (screen_w - sp->img_w) / 2; sp->yOffset = (screen_h - sp->img_h) / 2; From 6ed09ea3970a44758f4fca0e25a6dfb75552ccea Mon Sep 17 00:00:00 2001 From: GAURAV KARMAKAR Date: Mon, 22 Dec 2025 13:22:25 +0530 Subject: [PATCH 3/4] SPUPNG: fix formatting to match clang-format --- src/lib_ccx/ccx_encoders_spupng.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/lib_ccx/ccx_encoders_spupng.c b/src/lib_ccx/ccx_encoders_spupng.c index 9770a228a..d1c37242d 100644 --- a/src/lib_ccx/ccx_encoders_spupng.c +++ b/src/lib_ccx/ccx_encoders_spupng.c @@ -441,11 +441,11 @@ int write_cc_bitmap_as_spupng(struct cc_subtitle *sub, struct encoder_ctx *conte } } filename = get_spupng_filename(sp); - + // Set image dimensions for offset calculation sp->img_w = width; sp->img_h = height; - + // Calculate centered offsets based on screen size (PAL/NTSC) calculate_spupng_offsets(sp, context); if (sub->flags & SUB_EOD_MARKER) @@ -482,7 +482,7 @@ int write_cc_bitmap_as_spupng(struct cc_subtitle *sub, struct encoder_ctx *conte /* TODO do rectangle wise, one color table should not be used for all rectangles */ mapclut_paletee(palette, alpha, (uint32_t *)rect[0].data1, rect[0].nb_colors); - + // Save PNG file first save_spupng(filename, pbuf, width, height, palette, alpha, rect[0].nb_colors); freep(&pbuf); @@ -490,7 +490,7 @@ int write_cc_bitmap_as_spupng(struct cc_subtitle *sub, struct encoder_ctx *conte // Write XML tag with calculated centered offsets write_sputag_open(sp, sub->start_time, sub->end_time - 1); wrote_opentag = 1; // Mark that we wrote the tag - + #ifdef ENABLE_OCR if (!context->nospupngocr) { @@ -1105,11 +1105,16 @@ static void calculate_spupng_offsets(struct spupng_t *sp, struct encoder_ctx *ct int screen_h; /* Teletext is always PAL */ - if (ctx->in_fileformat == 2) { + if (ctx->in_fileformat == 2) + { screen_h = 576; - } else if (ctx->is_pal) { + } + else if (ctx->is_pal) + { screen_h = 576; - } else { + } + else + { screen_h = 480; } From e42bc2b9f9251608a466eb3ac6c798d4fb6852f0 Mon Sep 17 00:00:00 2001 From: GAURAV KARMAKAR Date: Wed, 24 Dec 2025 02:22:07 +0530 Subject: [PATCH 4/4] fixed the merged conflict in the ccx_encoders_common.h --- src/lib_ccx/ccx_encoders_common.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib_ccx/ccx_encoders_common.h b/src/lib_ccx/ccx_encoders_common.h index 266ce6f4b..e61f5ef2d 100644 --- a/src/lib_ccx/ccx_encoders_common.h +++ b/src/lib_ccx/ccx_encoders_common.h @@ -175,6 +175,11 @@ struct encoder_ctx // OCR in SPUPNG int nospupngocr; int is_pal; + + struct ccx_s_write *tlt_out[MAX_TLT_PAGES_EXTRACT]; // Output files per teletext page + uint16_t tlt_out_pages[MAX_TLT_PAGES_EXTRACT]; // Page numbers for each output slot + unsigned int tlt_srt_counter[MAX_TLT_PAGES_EXTRACT]; // SRT counter per page + int tlt_out_count; // Number of teletext output files }; #define INITIAL_ENC_BUFFER_CAPACITY 2048