1 #ifndef CCX_CCEXTRACTOR_H
2 #define CCX_CCEXTRACTOR_H
3 
4 #define VERSION "0.85"
5 
6 // Load common includes and constants for library usage
7 #include "ccx_common_platform.h"
8 #include "ccx_common_constants.h"
9 #include "ccx_common_common.h"
10 #include "ccx_common_char_encoding.h"
11 #include "ccx_common_structs.h"
12 #include "ccx_common_timing.h"
13 #include "ccx_common_option.h"
14 
15 #include "ccx_demuxer.h"
16 #include "ccx_encoders_common.h"
17 #include "ccx_decoders_common.h"
18 #include "bitstream.h"
19 
20 #include "networking.h"
21 #include "avc_functions.h"
22 
23 #ifdef WITH_LIBCURL
24 #include <curl/curl.h>
25 #endif
26 
27 //#include "ccx_decoders_708.h"
28 
29 /* Report information */
30 #define SUB_STREAMS_CNT 10
31 
32 #define TELETEXT_CHUNK_LEN 1 + 8 + 44
33 struct file_report
34 {
35 	unsigned width;
36 	unsigned height;
37 	unsigned aspect_ratio;
38 	unsigned frame_rate;
39 	struct ccx_decoder_608_report *data_from_608;
40 	struct ccx_decoder_dtvcc_report *data_from_708;
41 	unsigned mp4_cc_track_cnt;
42 };
43 
44 // Stuff for telxcc.c
45 struct ccx_s_teletext_config
46 {
47 	uint8_t verbose : 1;                                       // should telxcc be verbose?
48 	uint16_t page;                                             // teletext page containing cc we want to filter
49 	uint16_t tid;                                              // 13-bit packet ID for teletext stream
50 	double offset;                                             // time offset in seconds
51 	uint8_t bom : 1;                                           // print UTF-8 BOM characters at the beginning of output
52 	uint8_t nonempty : 1;                                      // produce at least one (dummy) frame
53 	// uint8_t se_mode : 1;                                    // search engine compatible mode => Uses CCExtractor's write_format
54 	// uint64_t utc_refvalue;                                  // UTC referential value => Moved to ccx_decoders_common, so can be used for other decoders (608/xds) too
55 	uint16_t user_page;                                        // Page selected by user, which MIGHT be different to 'page' depending on autodetection stuff
56 	int levdistmincnt, levdistmaxpct;                          // Means 2 fails or less is "the same", 10% or less is also "the same"
57 	struct ccx_boundary_time extraction_start, extraction_end; // Segment we actually process
58 	enum ccx_output_format write_format;                       // 0=Raw, 1=srt, 2=SMI
59 	int gui_mode_reports;                                      // If 1, output in stderr progress updates so the GUI can grab them
60 	enum ccx_output_date_format date_format;
61 	int noautotimeref;                                         // Do NOT set time automatically?
62 	unsigned send_to_srv;
63 	enum ccx_encoding_type encoding;
64 	int nofontcolor;
65 	int nohtmlescape;
66 	char millis_separator;
67 };
68 
69 struct ccx_s_mp4Cfg
70 {
71 	unsigned int mp4vidtrack :1;
72 };
73 
74 struct lib_ccx_ctx
75 {
76 
77 	// Stuff common to both loops
78 	unsigned char *pesheaderbuf;
79 	LLONG inputsize;
80 	LLONG total_inputsize;
81 	LLONG total_past; // Only in binary concat mode
82 
83 	int last_reported_progress;
84 
85 
86 	/* Stats */
87 	int stat_numuserheaders;
88 	int stat_dvdccheaders;
89 	int stat_scte20ccheaders;
90 	int stat_replay5000headers;
91 	int stat_replay4000headers;
92 	int stat_dishheaders;
93 	int stat_hdtv;
94 	int stat_divicom;
95 	int false_pict_header;
96 
97 	// int hex_mode=HEX_NONE; // Are we processing an hex file?
98 	struct ccx_decoders_common_settings_t *dec_global_setting;
99 	struct list_head dec_ctx_head;
100 
101 	int rawmode; // Broadcast or DVD
102 	// See -d from
103 
104 	int cc_to_stdout; // If 1, captions go to stdout instead of file
105 	int pes_header_to_stdout; //If 1, PES Header data will be outputted to console
106 	int dvb_debug_traces_to_stdout; // If 1, DVB subtitle debug traces will be outputted to console
107 	int ignore_pts_jumps; //If 1, the program will ignore PTS jumps. Sometimes this parameter is required for DVB subs with > 30s pause time
108 
109 	LLONG subs_delay; // ms to delay (or advance) subs
110 
111 	int startcredits_displayed;
112 	int end_credits_displayed;
113 	LLONG last_displayed_subs_ms;    // When did the last subs end?
114 	LLONG screens_to_process;        // How many screenfuls we want?
115 	char *basefilename;              // Input filename without the extension
116 
117 	const char *extension;           // Output extension
118 	int current_file;                // If current_file!=1, we are processing *inputfile[current_file]
119 
120 	char **inputfile;                // List of files to process
121 	int num_input_files;             // How many?
122 
123 	unsigned teletext_warning_shown; // Did we detect a possible PAL (with teletext subs) and told the user already?
124 
125 	int epg_inited;
126 	struct PSI_buffer *epg_buffers;
127 	struct EIT_program *eit_programs;
128 	int32_t *eit_current_events;
129 	int16_t *ATSC_source_pg_map;
130 	int epg_last_output;
131 	int epg_last_live_output;
132 	struct file_report freport;
133 
134 	unsigned int hauppauge_mode;         // If 1, use PID=1003, process specially and so on
135 	int live_stream;                     /* -1 -> Not a complete file but a live stream, without timeout
136                                           0 -> A regular file
137                                          >0 -> Live stream with a timeout of this value in seconds */
138 	int binary_concat;                   // Disabled by -ve or --videoedited
139 	int multiprogram;
140 	enum ccx_output_format write_format; // 0=Raw, 1=srt, 2=SMI
141 
142 	struct ccx_demuxer *demux_ctx;
143 	struct list_head enc_ctx_head;
144 	struct ccx_s_mp4Cfg mp4_cfg;
145 	int out_interval;
146 	int segment_counter;
147 	LLONG system_start_time;
148 };
149 
150 
151 struct lib_ccx_ctx* init_libraries(struct ccx_s_options *opt);
152 void dinit_libraries( struct lib_ccx_ctx **ctx);
153 
154 //params.c
155 int parse_parameters (struct ccx_s_options *opt, int argc, char *argv[]);
156 void print_usage (void);
157 int detect_input_file_overwrite(struct lib_ccx_ctx *ctx, const char *output_filename);
158 int atoi_hex (char *s);
159 int stringztoms (const char *s, struct ccx_boundary_time *bt);
160 
161 // general_loop.c
162 void position_sanity_check(struct ccx_demuxer *ctx);
163 int init_file_buffer(struct ccx_demuxer *ctx);
164 int ps_get_more_data(struct lib_ccx_ctx *ctx, struct demuxer_data **ppdata);
165 int general_get_more_data(struct lib_ccx_ctx *ctx, struct demuxer_data **data);
166 int raw_loop (struct lib_ccx_ctx *ctx);
167 size_t process_raw(struct lib_cc_decode *ctx, struct cc_subtitle *sub, unsigned char *buffer, size_t len);
168 int general_loop(struct lib_ccx_ctx *ctx);
169 void process_hex (char *filename);
170 int rcwt_loop(struct lib_ccx_ctx *ctx);
171 
172 extern int end_of_file;
173 
174 // asf_functions.c
175 int asf_get_more_data(struct lib_ccx_ctx *ctx, struct demuxer_data **ppdata);
176 
177 // wtv_functions.c
178 int wtv_get_more_data(struct lib_ccx_ctx *ctx, struct demuxer_data **ppdata);
179 
180 // es_functions.c
181 size_t process_m2v(struct lib_cc_decode *ctx, unsigned char *data, size_t length, struct cc_subtitle *sub);
182 
183 extern unsigned top_field_first;
184 
185 // es_userdata.c
186 int user_data(struct lib_cc_decode *ctx, struct bitstream *ustream, int udtype, struct cc_subtitle *sub);
187 
188 // bitstream.c - see bitstream.h
189 
190 // file_functions.c
191 LLONG get_file_size (int in);
192 LLONG get_total_file_size (struct lib_ccx_ctx *ctx);
193 void prepare_for_new_file (struct lib_ccx_ctx *ctx);
194 void close_input_file (struct lib_ccx_ctx *ctx);
195 int switch_to_next_file (struct lib_ccx_ctx *ctx, LLONG bytesinbuffer);
196 void return_to_buffer (struct ccx_demuxer *ctx, unsigned char *buffer, unsigned int bytes);
197 
198 // sequencing.c
199 void init_hdcc (struct lib_cc_decode *ctx);
200 void store_hdcc(struct lib_cc_decode *ctx, unsigned char *cc_data, int cc_count, int sequence_number, LLONG current_fts_now, struct cc_subtitle *sub);
201 void anchor_hdcc(struct lib_cc_decode *ctx, int seq);
202 void process_hdcc (struct lib_cc_decode *ctx, struct cc_subtitle *sub);
203 
204 // params_dump.c
205 void params_dump(struct lib_ccx_ctx *ctx);
206 void print_file_report(struct lib_ccx_ctx *ctx);
207 
208 // output.c
209 void dinit_write(struct ccx_s_write *wb);
210 int temporarily_open_output(struct ccx_s_write *wb);
211 int temporarily_close_output(struct ccx_s_write *wb);
212 int init_write(struct ccx_s_write *wb, char *filename, int with_semaphore);
213 int writeraw (const unsigned char *data, int length, void *private_data, struct cc_subtitle *sub);
214 void flushbuffer (struct lib_ccx_ctx *ctx, struct ccx_s_write *wb, int closefile);
215 void writercwtdata (struct lib_cc_decode *ctx, const unsigned char *data, struct cc_subtitle *sub);
216 
217 // stream_functions.c
218 int isValidMP4Box(unsigned char *buffer, size_t position, size_t *nextBoxLocation, int *boxScore);
219 void detect_stream_type (struct ccx_demuxer *ctx);
220 int detect_myth( struct ccx_demuxer *ctx );
221 int read_video_pes_header (struct ccx_demuxer *ctx, struct demuxer_data *data, unsigned char *nextheader, int *headerlength, int sbuflen);
222 
223 // ts_functions.c
224 void init_ts(struct ccx_demuxer *ctx);
225 int ts_readpacket(struct ccx_demuxer* ctx, struct ts_payload *payload);
226 long ts_readstream(struct ccx_demuxer *ctx, struct demuxer_data **data);
227 int ts_get_more_data(struct ccx_demuxer *ctx, struct demuxer_data **data);
228 int write_section(struct ccx_demuxer *ctx, struct ts_payload *payload, unsigned char*buf, int size,  struct program_info *pinfo);
229 void ts_buffer_psi_packet(struct ccx_demuxer *ctx);
230 int parse_PMT (struct ccx_demuxer *ctx, unsigned char *buf, int len,  struct program_info *pinfo);
231 int parse_PAT (struct ccx_demuxer *ctx);
232 void parse_EPG_packet (struct lib_ccx_ctx *ctx);
233 void EPG_free(struct lib_ccx_ctx *ctx);
234 char* EPG_DVB_decode_string(uint8_t *in, size_t size);
235 void parse_SDT(struct ccx_demuxer *ctx);
236 
237 // myth.c
238 int myth_loop(struct lib_ccx_ctx *ctx);
239 
240 // utility.c
241 void fatal(int exit_code, const char *fmt, ...);
242 void mprint (const char *fmt, ...);
243 void sleep_secs (int secs);
244 void dump (LLONG mask, unsigned char *start, int l, unsigned long abs_start, unsigned clear_high_bit);
245 bool_t in_array(uint16_t *array, uint16_t length, uint16_t element) ;
246 int hex_to_int (char high, char low);
247 int hex_string_to_int(char* string, int len);
248 void timestamp_to_srttime(uint64_t timestamp, char *buffer);
249 void timestamp_to_smptetttime(uint64_t timestamp, char *buffer);
250 int levenshtein_dist (const uint64_t *s1, const uint64_t *s2, unsigned s1len, unsigned s2len);
251 void millis_to_date (uint64_t timestamp, char *buffer, enum ccx_output_date_format date_format, char millis_separator);
252 void create_signal(int sigtype);
253 void signal_handler(int sig_type);
254 struct encoder_ctx* change_filename(struct encoder_ctx*);
255 #ifndef _WIN32
256 void m_signal(int sig, void (*func)(int));
257 #endif
258 
259 
260 void buffered_seek (struct ccx_demuxer *ctx, int offset);
261 extern void build_parity_table(void);
262 
263 int tlt_process_pes_packet(struct lib_cc_decode *dec_ctx, uint8_t *buffer, uint16_t size, struct cc_subtitle *sub, int sentence_cap);
264 void* telxcc_init(void);
265 void telxcc_close(void **ctx, struct cc_subtitle *sub);
266 void tlt_read_rcwt(void *codec, unsigned char *buf, struct cc_subtitle *sub);
267 void telxcc_configure (void *codec, struct ccx_s_teletext_config *cfg);
268 void telxcc_update_gt(void *codec, uint32_t global_timestamp);
269 
270 extern int strangeheader;
271 
272 extern const char *desc[256];
273 
274 
275 extern long FILEBUFFERSIZE; // Uppercase because it used to be a define
276 
277 extern int firstcall;
278 
279 // From ts_functions
280 //extern struct ts_payload payload;
281 extern unsigned char tspacket[188];
282 extern unsigned char *last_pat_payload;
283 extern unsigned last_pat_length;
284 extern volatile int terminate_asap;
285 
286 #define HAUPPAGE_CCPID	1003 // PID for CC's in some Hauppauge recordings
287 
288 extern unsigned teletext_mode;
289 
290 #define MAX_TLT_PAGES 1000
291 
292 extern struct ccx_s_teletext_config tlt_config;
293 
294 int is_decoder_processed_enough(struct lib_ccx_ctx *ctx);
295 struct lib_cc_decode *update_decoder_list_cinfo(struct lib_ccx_ctx *ctx, struct cap_info* cinfo);
296 struct lib_cc_decode *update_decoder_list(struct lib_ccx_ctx *ctx);
297 
298 struct encoder_ctx *update_encoder_list_cinfo(struct lib_ccx_ctx *ctx, struct cap_info* cinfo);
299 struct encoder_ctx * update_encoder_list(struct lib_ccx_ctx *ctx);
300 struct encoder_ctx *get_encoder_by_pn(struct lib_ccx_ctx *ctx, int pn);
301 #endif
302