-
Notifications
You must be signed in to change notification settings - Fork 420
/
ccx_dtvcc.c
156 lines (131 loc) · 4.43 KB
/
ccx_dtvcc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "ccx_dtvcc.h"
#include "ccx_common_common.h"
#include "ccx_encoders_common.h"
#include "ccx_decoders_708_output.h"
void dtvcc_process_data(struct dtvcc_ctx *dtvcc,
const unsigned char *data)
{
/*
* Note: the data has following format:
* 1 byte for cc_valid
* 1 byte for cc_type
* 2 bytes for the actual data
*/
if (!dtvcc->is_active && !dtvcc->report_enabled)
return;
unsigned char cc_valid = data[0];
unsigned char cc_type = data[1];
switch (cc_type)
{
case 2:
ccx_common_logging.debug_ftn(CCX_DMT_708, "[CEA-708] dtvcc_process_data: DTVCC Channel Packet Data\n");
if (cc_valid && dtvcc->is_current_packet_header_parsed)
{
if (dtvcc->current_packet_length > 253)
{
ccx_common_logging.debug_ftn(CCX_DMT_708, "[CEA-708] dtvcc_process_data: "
"Warning: Legal packet size exceeded (1), data not added.\n");
}
else
{
dtvcc->current_packet[dtvcc->current_packet_length++] = data[2];
dtvcc->current_packet[dtvcc->current_packet_length++] = data[3];
int len = dtvcc->current_packet[0] & 0x3F; // 6 least significants bits
if (len == 0) // This is well defined in EIA-708; no magic.
len = 128;
else
len = len * 2;
// Note that len here is the length including the header
if (dtvcc->current_packet_length >= len)
dtvcc_process_current_packet(dtvcc, len);
}
}
break;
case 3:
ccx_common_logging.debug_ftn(CCX_DMT_708, "[CEA-708] dtvcc_process_data: DTVCC Channel Packet Start\n");
if (cc_valid)
{
if (dtvcc->current_packet_length > CCX_DTVCC_MAX_PACKET_LENGTH - 1)
{
ccx_common_logging.debug_ftn(CCX_DMT_708, "[CEA-708] dtvcc_process_data: "
"Warning: Legal packet size exceeded (2), data not added.\n");
}
else
{
if (dtvcc->is_current_packet_header_parsed)
{
ccx_common_logging.debug_ftn(CCX_DMT_708, "[CEA-708] dtvcc_process_data: "
"Warning: Incorrect packet length specified. Packet will be skipped.\n");
dtvcc_clear_packet(dtvcc);
}
dtvcc->current_packet[dtvcc->current_packet_length++] = data[2];
dtvcc->current_packet[dtvcc->current_packet_length++] = data[3];
dtvcc->is_current_packet_header_parsed = 1;
}
}
break;
default:
ccx_common_logging.fatal_ftn(CCX_COMMON_EXIT_BUG_BUG, "[CEA-708] dtvcc_process_data: "
"shouldn't be here - cc_type: %d\n",
cc_type);
}
}
//--------------------------------------------------------------------------------------
dtvcc_ctx *dtvcc_init(struct ccx_decoder_dtvcc_settings *opts)
{
ccx_common_logging.debug_ftn(CCX_DMT_708, "[CEA-708] initializing dtvcc decoder\n");
dtvcc_ctx *ctx = (dtvcc_ctx *)malloc(sizeof(dtvcc_ctx));
if (!ctx)
{
ccx_common_logging.fatal_ftn(EXIT_NOT_ENOUGH_MEMORY, "[CEA-708] dtvcc_init");
return NULL;
}
ctx->report = opts->report;
ctx->report->reset_count = 0;
ctx->is_active = 0;
ctx->report_enabled = 0;
ctx->no_rollup = opts->no_rollup;
ctx->active_services_count = opts->active_services_count;
memcpy(ctx->services_active, opts->services_enabled, CCX_DTVCC_MAX_SERVICES * sizeof(int));
dtvcc_clear_packet(ctx);
ctx->last_sequence = CCX_DTVCC_NO_LAST_SEQUENCE;
ctx->report_enabled = opts->print_file_reports;
ctx->timing = opts->timing;
ccx_common_logging.debug_ftn(CCX_DMT_708, "[CEA-708] initializing services\n");
for (int i = 0; i < CCX_DTVCC_MAX_SERVICES; i++)
{
if (!ctx->services_active[i])
continue;
dtvcc_service_decoder *decoder = &ctx->decoders[i];
decoder->cc_count = 0;
decoder->tv = (dtvcc_tv_screen *)malloc(sizeof(dtvcc_tv_screen));
decoder->tv->service_number = i + 1;
decoder->tv->cc_count = 0;
if (!decoder->tv)
ccx_common_logging.fatal_ftn(EXIT_NOT_ENOUGH_MEMORY, "dtvcc_init");
for (int j = 0; j < CCX_DTVCC_MAX_WINDOWS; j++)
decoder->windows[j].memory_reserved = 0;
dtvcc_windows_reset(decoder);
}
return ctx;
}
void dtvcc_free(dtvcc_ctx **ctx_ptr)
{
ccx_common_logging.debug_ftn(CCX_DMT_708, "[CEA-708] dtvcc_free: cleaning up\n");
dtvcc_ctx *ctx = *ctx_ptr;
for (int i = 0; i < CCX_DTVCC_MAX_SERVICES; i++)
{
if (!ctx->services_active[i])
continue;
dtvcc_service_decoder *decoder = &ctx->decoders[i];
for (int j = 0; j < CCX_DTVCC_MAX_WINDOWS; j++)
if (decoder->windows[j].memory_reserved)
{
for (int k = 0; k < CCX_DTVCC_MAX_ROWS; k++)
free(decoder->windows[j].rows[k]);
decoder->windows[j].memory_reserved = 0;
}
free(decoder->tv);
}
freep(ctx_ptr);
}