Skip to content

Commit c2eedab

Browse files
committed
Adding simple xml support
Signed-off-by: Anshul Maheshwari <[email protected]>
1 parent 677fee4 commit c2eedab

File tree

5 files changed

+136
-3
lines changed

5 files changed

+136
-3
lines changed

src/lib_ccx/ccx_common_constants.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ enum ccx_output_format
163163
CCX_OF_SMPTETT = 6,
164164
CCX_OF_SPUPNG = 7,
165165
CCX_OF_DVDRAW = 8, // See -d at http://www.geocities.com/mcpoodle43/SCC_TOOLS/DOCS/SCC_TOOLS.HTML#CCExtract
166-
CCX_OF_WEBVTT = 9
166+
CCX_OF_WEBVTT = 9,
167+
CCX_OF_SIMPLE_XML = 10,
167168
};
168169

169170
enum ccx_output_date_format

src/lib_ccx/ccx_decoders_common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ struct lib_cc_decode* init_cc_decode (struct ccx_decoders_common_settings_t *set
300300
setting->output_format == CCX_OF_WEBVTT ||
301301
setting->output_format==CCX_OF_TRANSCRIPT ||
302302
setting->output_format==CCX_OF_SPUPNG ||
303+
setting->output_format==CCX_OF_SIMPLE_XML ||
303304
setting->output_format==CCX_OF_NULL)
304305
ctx->writedata = process608;
305306
else

src/lib_ccx/ccx_encoders_common.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ static const char *smptett_header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
5252

5353
static const char *webvtt_header = "WEBVTT\r\n\r\n";
5454

55+
static const char *simple_xml_header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<captions>\r\n";
56+
5557
void find_limit_characters(unsigned char *line, int *first_non_blank, int *last_non_blank, int max_len)
5658
{
5759
*last_non_blank = -1;
@@ -329,6 +331,19 @@ int write_subtitle_file_footer(struct encoder_ctx *ctx,struct ccx_s_write *out)
329331
case CCX_OF_SPUPNG:
330332
write_spumux_footer(out);
331333
break;
334+
case CCX_OF_SIMPLE_XML:
335+
sprintf ((char *) str,"</captions>\n");
336+
if (ctx->encoding != CCX_ENC_UNICODE)
337+
{
338+
dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str);
339+
}
340+
used = encode_line (ctx, ctx->buffer,(unsigned char *) str);
341+
ret = write (out->fh, ctx->buffer, used);
342+
if (ret != used)
343+
{
344+
mprint("WARNING: loss of data\n");
345+
}
346+
break;
332347
default: // Nothing to do, no footer on this format
333348
break;
334349
}
@@ -445,13 +460,67 @@ static int write_subtitle_file_header(struct encoder_ctx *ctx, struct ccx_s_writ
445460
ret = write_bom(ctx, out);
446461
if(ret < 0)
447462
return -1;
463+
case CCX_OF_SIMPLE_XML: // No header. Fall thru
464+
ret = write_bom(ctx, out);
465+
if(ret < 0)
466+
return -1;
467+
REQUEST_BUFFER_CAPACITY(ctx,strlen (simple_xml_header)*3);
468+
used=encode_line (ctx, ctx->buffer,(unsigned char *) simple_xml_header);
469+
ret = write(out->fh, ctx->buffer, used);
470+
if(ret < used)
471+
{
472+
mprint("WARNING: Unable to write complete Buffer \n");
473+
return -1;
474+
}
448475
default:
449476
break;
450477
}
451478

452479
return ret;
453480
}
454481

482+
int write_cc_subtitle_as_simplexml(struct cc_subtitle *sub, struct encoder_ctx *context)
483+
{
484+
int length;
485+
int ret = 0;
486+
char *str;
487+
char *save_str;
488+
struct cc_subtitle *osub = sub;
489+
struct cc_subtitle *lsub = sub;
490+
491+
while(sub)
492+
{
493+
str = sub->data;
494+
495+
str = strtok_r(str, "\r\n", &save_str);
496+
do
497+
{
498+
length = get_str_basic(context->subline, str, context->trim_subs, sub->enc_type, context->encoding, strlen(str));
499+
if (length <= 0)
500+
{
501+
continue;
502+
}
503+
ret = write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
504+
if(ret < context->encoded_crlf_length)
505+
{
506+
mprint("Warning:Loss of data\n");
507+
}
508+
509+
} while (str = strtok_r(NULL, "\r\n", &save_str) );
510+
511+
freep(&sub->data);
512+
lsub = sub;
513+
sub = sub->next;
514+
}
515+
while(lsub != osub)
516+
{
517+
sub = lsub->prev;
518+
freep(&lsub);
519+
lsub = sub;
520+
}
521+
522+
}
523+
455524
int write_cc_subtitle_as_transcript(struct cc_subtitle *sub, struct encoder_ctx *context)
456525
{
457526
int length;
@@ -573,6 +642,30 @@ int write_cc_subtitle_as_transcript(struct cc_subtitle *sub, struct encoder_ctx
573642
return ret;
574643
}
575644

645+
void write_cc_line_as_simplexml(struct eia608_screen *data, struct encoder_ctx *context, int line_number)
646+
{
647+
int ret;
648+
int length = 0;
649+
char *cap = "<caption>";
650+
char *cap1 = "</caption>";
651+
if (context->sentence_cap)
652+
{
653+
capitalize (context, line_number, data);
654+
correct_case(line_number, data);
655+
}
656+
length = get_str_basic (context->subline, data->characters[line_number],
657+
context->trim_subs, CCX_ENC_ASCII, context->encoding, CCX_DECODER_608_SCREEN_WIDTH);
658+
659+
ret = write(context->out->fh, cap, strlen(cap));
660+
ret = write(context->out->fh, context->subline, length);
661+
if(ret < length)
662+
{
663+
mprint("Warning:Loss of data\n");
664+
}
665+
ret = write(context->out->fh, cap1, strlen(cap1));
666+
ret = write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
667+
668+
}
576669
//TODO Convert CC line to TEXT format and remove this function
577670
void write_cc_line_as_transcript2(struct eia608_screen *data, struct encoder_ctx *context, int line_number)
578671
{
@@ -680,6 +773,21 @@ void write_cc_line_as_transcript2(struct eia608_screen *data, struct encoder_ctx
680773
// fprintf (wb->fh,encoded_crlf);
681774
}
682775

776+
int write_cc_buffer_as_simplexml(struct eia608_screen *data, struct encoder_ctx *context)
777+
{
778+
int wrote_something = 0;
779+
780+
for (int i=0;i<15;i++)
781+
{
782+
if (data->row_used[i])
783+
{
784+
write_cc_line_as_simplexml(data, context, i);
785+
}
786+
wrote_something=1;
787+
}
788+
return wrote_something;
789+
}
790+
683791
int write_cc_buffer_as_transcript2(struct eia608_screen *data, struct encoder_ctx *context)
684792
{
685793
int wrote_something = 0;
@@ -696,6 +804,17 @@ int write_cc_buffer_as_transcript2(struct eia608_screen *data, struct encoder_ct
696804
dbg_print(CCX_DMT_DECODER_608, "- - - - - - - - - - - -\r\n");
697805
return wrote_something;
698806
}
807+
808+
//Dummy Function for support DVB in simple xml
809+
int write_cc_bitmap_as_simplexml(struct cc_subtitle *sub, struct encoder_ctx *context)
810+
{
811+
int ret = 0;
812+
813+
sub->nb_data = 0;
814+
freep(&sub->data);
815+
return ret;
816+
}
817+
699818
int write_cc_bitmap_as_transcript(struct cc_subtitle *sub, struct encoder_ctx *context)
700819
{
701820
int ret = 0;
@@ -1248,6 +1367,9 @@ int encode_sub(struct encoder_ctx *context, struct cc_subtitle *sub)
12481367
case CCX_OF_SPUPNG:
12491368
wrote_something = write_cc_buffer_as_spupng(data, context);
12501369
break;
1370+
case CCX_OF_SIMPLE_XML:
1371+
wrote_something = write_cc_buffer_as_simplexml(data, context);
1372+
break;
12511373
default:
12521374
break;
12531375
}
@@ -1289,6 +1411,9 @@ int encode_sub(struct encoder_ctx *context, struct cc_subtitle *sub)
12891411
case CCX_OF_SPUPNG:
12901412
wrote_something = write_cc_bitmap_as_spupng(sub, context);
12911413
break;
1414+
case CCX_OF_SIMPLE_XML:
1415+
wrote_something = write_cc_bitmap_as_simplexml(sub, context);
1416+
break;
12921417
default:
12931418
break;
12941419
}
@@ -1337,6 +1462,9 @@ int encode_sub(struct encoder_ctx *context, struct cc_subtitle *sub)
13371462
case CCX_OF_SPUPNG:
13381463
wrote_something = write_cc_subtitle_as_spupng(sub, context);
13391464
break;
1465+
case CCX_OF_SIMPLE_XML:
1466+
wrote_something = write_cc_subtitle_as_simplexml(sub, context);
1467+
break;
13401468
default:
13411469
break;
13421470
}

src/lib_ccx/params.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ void set_output_format (struct ccx_s_options *opt, const char *format)
212212
opt->write_format=CCX_OF_DVDRAW;
213213
else if (strcmp (format,"spupng")==0)
214214
opt->write_format=CCX_OF_SPUPNG;
215+
else if (strcmp (format,"simplexml")==0)
216+
opt->write_format=CCX_OF_SIMPLE_XML;
215217
else
216218
fatal (EXIT_MALFORMED_PARAMETER, "Unknown output file format: %s\n", format);
217219
}

src/lib_ccx/utility.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ char *get_file_extension(enum ccx_output_format write_format)
408408
return strdup(".srt");
409409
case CCX_OF_WEBVTT:
410410
return strdup (".vtt");
411-
break;
412411
case CCX_OF_SAMI:
413412
return strdup(".smi");
414413
case CCX_OF_SMPTETT:
@@ -421,6 +420,8 @@ char *get_file_extension(enum ccx_output_format write_format)
421420
return strdup(".xml");
422421
case CCX_OF_DVDRAW:
423422
return strdup(".dvdraw");
423+
case CCX_OF_SIMPLE_XML:
424+
return strdup(".xml");
424425
case CCX_OF_NULL:
425426
return NULL;
426427
default:
@@ -509,4 +510,4 @@ char *strtok_r(char *str, const char *delim, char **saveptr)
509510
{
510511
strtok_s(str, delim, saveptr);
511512
}
512-
#endif //_WIN32
513+
#endif //_WIN32

0 commit comments

Comments
 (0)