Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C89 compile #58

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions caption/eia608.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,40 @@ typedef enum {
eia608_control_end_of_caption = 0x142F,
} eia608_control_t;



typedef enum {
eia608_status_unhandled,
eia608_status_unknown_control,
eia608_status_control,
eia608_status_preamble,
eia608_status_pad,
eia608_status_parity_failed,
eia608_status_basicna,
eia608_status_specialna,
eia608_status_westeu,
eia608_status_norpak,
eia608_status_xds,
eia608_status_midrowchange

} eia608_status_t;


typedef struct eia608_parsed{
eia608_status_t status;
eia608_control_t control;
eia608_style_t style;
int row;
int col;
int chan;
int underline;
char char1[5];
char char2[5];
uint8_t has_text;

} eia608_parsed_t;


/*! \brief
\param
*/
Expand Down Expand Up @@ -201,6 +235,9 @@ int eia608_to_utf8(uint16_t c, int* chan, utf8_char_t* char1, utf8_char_t* char2
\param
*/
void eia608_dump(uint16_t cc_data);

void eia608_parse_data(uint16_t cc_data, eia608_parsed_t *parsed);

////////////////////////////////////////////////////////////////////////////////
#ifdef __cplusplus
}
Expand Down
35 changes: 35 additions & 0 deletions caption/eia608_from_utf8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**********************************************************************************************/
/* The MIT License */
/* */
/* Copyright 2016-2017 Twitch Interactive, Inc. or its affiliates. All Rights Reserved. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
/* of this software and associated documentation files (the "Software"), to deal */
/* in the Software without restriction, including without limitation the rights */
/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
/* copies of the Software, and to permit persons to whom the Software is */
/* furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in */
/* all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN */
/* THE SOFTWARE. */
/**********************************************************************************************/
#ifndef LIBCAPTION_EIA608_FROM_UTF8_H
#define LIBCAPTION_EIA608_FROM_UTF8_H
#ifdef __cplusplus
extern "C" {
#endif

uint16_t _eia608_from_utf8 (const utf8_char_t* s);

#ifdef __cplusplus
}
#endif
#endif
149 changes: 148 additions & 1 deletion src/eia608.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ void eia608_dump(uint16_t cc_data)
eia608_style_t style;
const char* text = 0;
char char1[5], char2[5];
char1[0] = char2[0] = 0;
int row, col, chan, underline;

char1[0] = char2[0] = 0;
if (!eia608_parity_varify(cc_data)) {
text = "parity failed";
} else if (0 == eia608_parity_strip(cc_data)) {
Expand Down Expand Up @@ -313,3 +313,150 @@ void eia608_dump(uint16_t cc_data)

fprintf(stderr, "cc %04X (%04X) '%s' '%s' (%s)\n", cc_data, eia608_parity_strip(cc_data), char1, char2, text);
}


void eia608_parse_data(uint16_t cc_data, eia608_parsed_t *parsed) {
const char* text = 0;

parsed->status = eia608_status_unhandled;
parsed->control = 0;
parsed->chan = -1;
parsed->col = 0;
parsed->row = 0;
parsed->style = eia608_style_white;
parsed->underline = 0;
parsed->char1[0] = parsed->char2[0] = 0;
parsed->has_text = 0;

if (!eia608_parity_varify(cc_data)) {
parsed->status = eia608_status_parity_failed;
text = "parity failed";
} else if (0 == eia608_parity_strip(cc_data)) {
parsed->status = eia608_status_pad;
text = "pad";
} else if (eia608_is_basicna(cc_data)) {
parsed->status = eia608_status_basicna;
text = "basicna";
parsed->has_text = 1;
eia608_to_utf8(cc_data, &parsed->chan, &parsed->char1[0], &parsed->char2[0]);
} else if (eia608_is_specialna(cc_data)) {
parsed->status = eia608_status_specialna;
text = "specialna";
parsed->has_text = 1;
eia608_to_utf8(cc_data, &parsed->chan, &parsed->char1[0], &parsed->char2[0]);
} else if (eia608_is_westeu(cc_data)) {
parsed->status = eia608_status_westeu;
text = "westeu";
parsed->has_text = 1;
eia608_to_utf8(cc_data, &parsed->chan, &parsed->char1[0], &parsed->char2[0]);
} else if (eia608_is_xds(cc_data)) {
parsed->status = eia608_status_xds;
text = "xds";
} else if (eia608_is_midrowchange(cc_data)) {
parsed->status = eia608_status_midrowchange;
text = "midrowchange";
eia608_parse_midrowchange(cc_data, &parsed->chan, &parsed->style, &parsed->underline);
} else if (eia608_is_norpak(cc_data)) {
parsed->status = eia608_status_norpak;
text = "norpak";
} else if (eia608_is_preamble(cc_data)) {
parsed->status = eia608_status_preamble;
text = "preamble";
eia608_parse_preamble(cc_data, &parsed->row, &parsed->col,
&parsed->style, &parsed->chan, &parsed->underline);
fprintf(stderr, "preamble %d %d %d %d %d\n", parsed->row, parsed->col, parsed->style, parsed->chan, parsed->underline);

} else if (eia608_is_control(cc_data)) {
parsed->status = eia608_status_control;
parsed->control = eia608_parse_control(cc_data, &parsed->chan);
switch (parsed->control) {

default:
parsed->status = eia608_status_unknown_control;
text = "unknown_control";
break;

case eia608_tab_offset_0:
text = "eia608_tab_offset_0";
break;

case eia608_tab_offset_1:
text = "eia608_tab_offset_1";
break;

case eia608_tab_offset_2:
text = "eia608_tab_offset_2";
break;

case eia608_tab_offset_3:
text = "eia608_tab_offset_3";
break;

case eia608_control_resume_caption_loading:
text = "eia608_control_resume_caption_loading";
break;

case eia608_control_backspace:
text = "eia608_control_backspace";
break;

case eia608_control_alarm_off:
text = "eia608_control_alarm_off";
break;

case eia608_control_alarm_on:
text = "eia608_control_alarm_on";
break;

case eia608_control_delete_to_end_of_row:
text = "eia608_control_delete_to_end_of_row";
break;

case eia608_control_roll_up_2:
text = "eia608_control_roll_up_2";
break;

case eia608_control_roll_up_3:
text = "eia608_control_roll_up_3";
break;

case eia608_control_roll_up_4:
text = "eia608_control_roll_up_4";
break;

case eia608_control_resume_direct_captioning:
text = "eia608_control_resume_direct_captioning";
break;

case eia608_control_text_restart:
text = "eia608_control_text_restart";
break;

case eia608_control_text_resume_text_display:
text = "eia608_control_text_resume_text_display";
break;

case eia608_control_erase_display_memory:
text = "eia608_control_erase_display_memory";
break;

case eia608_control_carriage_return:
text = "eia608_control_carriage_return";
break;

case eia608_control_erase_non_displayed_memory:
text = "eia608_control_erase_non_displayed_memory";
break;

case eia608_control_end_of_caption:
text = "eia608_control_end_of_caption";
break;
}
} else {
text = "unhandled";
}

fprintf(stderr, "cc %04X (%04X) '%s' '%s' (%s)\n", cc_data,
eia608_parity_strip(cc_data), parsed->char1, parsed->char2, text);
}

23 changes: 13 additions & 10 deletions src/utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ const utf8_char_t* utf8_char_next(const utf8_char_t* c)
// returnes the length of the char in bytes
size_t utf8_char_length(const utf8_char_t* c)
{
// count null term as zero size
if (!c || 0x00 == c[0]) {
return 0;
}

static const size_t _utf8_char_length[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0
};

// count null term as zero size
if (!c || 0x00 == c[0]) {
return 0;
}

return _utf8_char_length[(c[0] >> 3) & 0x1F];
}

Expand Down Expand Up @@ -120,8 +120,9 @@ utf8_size_t utf8_char_count(const char* data, size_t size)
// returns the length of the line in bytes triming not printable charcters at the end
size_t utf8_trimmed_length(const utf8_char_t* data, utf8_size_t charcters)
{
size_t l, t = 0, split_at = 0;
for (size_t c = 0; (*data) && c < charcters; ++c) {
size_t l, t = 0, split_at = 0, c = 0;

for (c = 0; (*data) && c < charcters; ++c) {
l = utf8_char_length(data);
if (!utf8_char_whitespace(data)) {
split_at = t + l;
Expand All @@ -132,7 +133,7 @@ size_t utf8_trimmed_length(const utf8_char_t* data, utf8_size_t charcters)
return split_at;
}

size_t _utf8_newline(const utf8_char_t* data)
static size_t _utf8_newline(const utf8_char_t* data)
{
if ('\r' == data[0]) {
return '\n' == data[1] ? 2 : 1; // windows/unix
Expand Down Expand Up @@ -199,8 +200,10 @@ utf8_char_t* utf8_load_text_file(const char* path, size_t* size)
FILE* file = fopen(path, "r");

if (file) {
size_t file_size = 0;

fseek(file, 0, SEEK_END);
size_t file_size = ftell(file);
file_size = ftell(file);
fseek(file, 0, SEEK_SET);

if (0 == (*size) || file_size <= (*size)) {
Expand Down Expand Up @@ -244,4 +247,4 @@ char* strnstr(const char* string1, const char* string2, size_t len)
}
return NULL;
}
#endif
#endif