1 #ifndef CCX_DEMUXER_H
2 #define CCX_DEMUXER_H
3 #include "ccx_common_constants.h"
4 #include "ccx_common_option.h"
5 #include "ts_functions.h"
6 #include "list.h"
7 #include "activity.h"
8 #include "utility.h"
9 
10 /* Report information */
11 #define SUB_STREAMS_CNT 10
12 #define MAX_PID 65536
13 #define MAX_NUM_OF_STREAMIDS 51
14 #define MAX_PSI_PID 8191
15 #define TS_PMT_MAP_SIZE 128
16 #define MAX_PROGRAM 128
17 #define MAX_PROGRAM_NAME_LEN 128
18 
19 enum STREAM_TYPE
20 {
21 	PRIVATE_STREAM_1 = 0,
22 	AUDIO,
23 	VIDEO,
24 	COUNT
25 };
26 struct ccx_demux_report
27 {
28         unsigned program_cnt;
29         unsigned dvb_sub_pid[SUB_STREAMS_CNT];
30         unsigned tlt_sub_pid[SUB_STREAMS_CNT];
31         unsigned mp4_cc_track_cnt;
32 };
33 
34 struct program_info
35 {
36 	int pid;
37 	int program_number;
38 	uint8_t analysed_PMT_once:1;
39 	uint8_t version;
40 	uint8_t saved_section[1021];
41 	int32_t crc;
42 	uint8_t valid_crc:1;
43 	char name[MAX_PROGRAM_NAME_LEN];
44 	/**
45 	 * -1 pid represent that pcr_pid is not available
46 	 */
47 	int16_t pcr_pid;
48 	uint64_t got_important_streams_min_pts[COUNT];
49 	int has_all_min_pts;
50 };
51 
52 struct cap_info
53 {
54 	int pid;
55 	int program_number;
56 	enum ccx_stream_type stream;
57 	enum ccx_code_type codec;
58 	long capbufsize;
59 	unsigned char *capbuf;
60 	long capbuflen; // Bytes read in capbuf
61 	int saw_pesstart;
62 	int prev_counter;
63 	void *codec_private_data;
64 	int ignore;
65 
66 	/**
67 	  List joining all stream in TS
68 	*/
69 	struct list_head all_stream;
70 	/**
71 	  List joining all sibling Stream in Program
72 	*/
73 	struct list_head sib_head;
74 	struct list_head sib_stream;
75 	/**
76 	  List joining all sibling Stream in Program
77 	*/
78 	struct list_head pg_stream;
79 
80 };
81 struct ccx_demuxer
82 {
83 	int m2ts;
84 	enum ccx_stream_mode_enum stream_mode;
85 	enum ccx_stream_mode_enum auto_stream;
86 
87 	// Small buffer to help us with the initial sync
88 	unsigned char startbytes[STARTBYTESLENGTH];
89 	unsigned int startbytes_pos;
90 	int startbytes_avail;
91 
92 	// User Specified Param
93 	int ts_autoprogram;
94 	int ts_allprogram;
95 	int flag_ts_forced_pn;
96 	int flag_ts_forced_cappid;
97 	int ts_datastreamtype;
98 
99 
100 	struct program_info pinfo[MAX_PROGRAM];
101 	int nb_program;
102 	/* subtitle codec type */
103 	enum ccx_code_type codec;
104 	enum ccx_code_type nocodec;
105 	struct cap_info cinfo_tree;
106 
107 	/* File handles */
108 	FILE *fh_out_elementarystream;
109 	int infd;   // descriptor number to input.
110 	LLONG past; /* Position in file, if in sync same as ftell()  */
111 
112 	// TODO relates to fts_global
113 	int64_t global_timestamp;
114 	int64_t min_global_timestamp;
115 	int64_t offset_global_timestamp;
116 	int64_t last_global_timestamp;
117 	int global_timestamp_inited;
118 
119 	struct PSI_buffer *PID_buffers[MAX_PSI_PID];
120 	int PIDs_seen[MAX_PID];
121 
122 	/*51 possible stream ids in total, 0xbd is private stream, 0xbe is padding stream,
123 	0xbf private stream 2, 0xc0 - 0xdf audio, 0xe0 - 0xef video
124 	(stream ids range from 0xbd to 0xef so 0xef - 0xbd + 1 = 51)*/
125 	//uint8_t found_stream_ids[MAX_NUM_OF_STREAMIDS];
126 
127 	uint8_t stream_id_of_each_pid[MAX_PSI_PID + 1];
128 	uint64_t min_pts[MAX_PSI_PID + 1];
129 	int have_PIDs[MAX_PSI_PID + 1];
130 	int num_of_PIDs;
131 
132 	struct PMT_entry *PIDs_programs[MAX_PID];
133 	struct ccx_demux_report freport;
134 
135 	/* Hauppauge support */
136 	unsigned hauppauge_warning_shown; // Did we detect a possible Hauppauge capture and told the user already?
137 
138 	int multi_stream_per_prog;
139 
140 	unsigned char *last_pat_payload;
141 	unsigned last_pat_length;
142 
143 	unsigned char *filebuffer;
144 	LLONG filebuffer_start;      // Position of buffer start relative to file
145 	unsigned int filebuffer_pos; // Position of pointer relative to buffer start
146 	unsigned int bytesinbuffer;  // Number of bytes we actually have on buffer
147 
148 	int warning_program_not_found_shown;
149 
150 	// Remember if the last header was valid. Used to suppress too much output
151 	// and the expected unrecognized first header for TiVo files.
152 	int strangeheader;
153 #ifdef ENABLE_FFMPEG
154 	void *ffmpeg_ctx;
155 #endif
156 
157 	void *parent;
158 
159 	//Will contain actual Demuxer Context
160 	void *private_data;
161 	void (*print_cfg)(struct ccx_demuxer *ctx);
162 	void (*reset)(struct ccx_demuxer *ctx);
163 	void (*close)(struct ccx_demuxer *ctx);
164 	int (*open)(struct ccx_demuxer *ctx, const char *file_name);
165 	int (*is_open)(struct ccx_demuxer *ctx);
166 	int (*get_stream_mode)(struct ccx_demuxer *ctx);
167 	LLONG (*get_filesize) (struct ccx_demuxer *ctx);
168 	int (*write_es)(struct ccx_demuxer *ctx, unsigned char* buf, size_t len);
169 };
170 
171 struct demuxer_data
172 {
173 	int program_number;
174 	int stream_pid;
175 	enum ccx_code_type codec;
176 	enum ccx_bufferdata_type bufferdatatype;
177 	unsigned char *buffer;
178 	size_t len;
179 	unsigned int rollover_bits; // The PTS rolls over every 26 hours and that can happen in the middle of a stream.
180 	LLONG pts;
181 	struct ccx_rational tb;
182 	struct demuxer_data *next_stream;
183 	struct demuxer_data *next_program;
184 };
185 
186 struct cap_info *get_sib_stream_by_type(struct cap_info* program, enum ccx_code_type type);
187 struct ccx_demuxer *init_demuxer(void *parent, struct demuxer_cfg *cfg);
188 void ccx_demuxer_delete(struct ccx_demuxer **ctx);
189 struct demuxer_data* alloc_demuxer_data(void);
190 void delete_demuxer_data(struct demuxer_data *data);
191 int update_capinfo(struct ccx_demuxer *ctx, int pid, enum ccx_stream_type stream, enum ccx_code_type codec, int pn, void *private_data);
192 struct cap_info * get_cinfo(struct ccx_demuxer *ctx, int pid);
193 int need_cap_info(struct ccx_demuxer *ctx, int program_number);
194 int need_cap_info_for_pid(struct ccx_demuxer *ctx, int pid);
195 struct demuxer_data *get_best_data(struct demuxer_data *data);
196 struct demuxer_data *get_data_stream(struct demuxer_data *data, int pid);
197 int get_best_stream(struct ccx_demuxer *ctx);
198 void ignore_other_stream(struct ccx_demuxer *ctx, int pid);
199 void dinit_cap (struct ccx_demuxer *ctx);
200 int get_programme_number(struct ccx_demuxer *ctx, int pid);
201 struct cap_info* get_best_sib_stream(struct cap_info* program);
202 void ignore_other_sib_stream(struct cap_info* head, int pid);
203 #endif
204