clib/dvb_ts_lib/ts_split.c

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 80 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 191 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« clib/dvb_ts_lib/ts_skip.c ↑ Summary clib/libmpeg2/alloc.c »
1 // VERSION = "1.000"
2 //
3 // Standard C code loaded outside XS space. Contains useful routines used by ts TS parsing functions
4  
5 // VERSION 1.01
6  
7 #include "ts_split.h"
8  
9 //========================================================================================================
10 // SPLIT
11 //========================================================================================================
12  
13 //---------------------------------------------------------------------------------------------------------
14 0 static void _print_hook_data(struct TS_cut_data *data)
15 {
16 0 fprintf(stderr, "Hook data @ %p\n", data) ;
17 0 fprintf(stderr, " + magic : 0x%08x\n", data->magic) ;
18 0 fprintf(stderr, " + settings : %p\n", data->settings) ;
19 0 fprintf(stderr, " + ofile : %d\n", data->ofile) ;
20 0 fprintf(stderr, " + debug : %d\n", data->debug) ;
21 0 fprintf(stderr, " + split_count : %d\n", data->split_count) ;
22 0 fprintf(stderr, " + fname : %s\n", data->fname) ;
23 0 fprintf(stderr, " + ofname : %s\n", data->ofname) ;
24 0 fprintf(stderr, " + cut_file : %d\n", data->cut_file) ;
25 0 fprintf(stderr, " + cut_list : %p\n", data->cut_list) ;
26 0 fprintf(stderr, " + current_cut : %p\n", data->current_cut) ;
27 0 fprintf(stderr, " + tsreader : %p\n", data->tsreader) ;
28 0 fprintf(stderr, " + end : 0x%08x\n", data->magic_end) ;
29 0 }
30  
31 //---------------------------------------------------------------------------------------------------------
32 //
33 0 static void next_split_file(struct TS_cut_data *hook_data, unsigned pktnum)
34 {
35 0 ▶ if (hook_data->debug) fprintf(stderr, " + + next_split_file(%u) - start\n", pktnum) ;
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug) fprintf(stderr, " + + next_split_file(%u) - start\n", pktnum) ; 0 0 0
36  
37 // close currently open
38 0 ▶ if (hook_data->cut_file)
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->cut_file) 0 0 0
39 {
40 0 ▶ if (pktnum > hook_data->split_pkt)
Branch: 1 branch
BranchTrueFalseTotal
if (pktnum > hook_data->split_pkt) 0 0 0
41 {
42 0 close(hook_data->cut_file) ;
43 0 hook_data->cut_file = 0 ;
44 0 ▶ if (hook_data->debug) fprintf(stderr, " + + + closed existing file\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug) fprintf(stderr, " + + + closed existing file\n") ; 0 0 0
45 }
46 }
47  
48 // open next
49 0 ▶ if (!hook_data->cut_file)
Branch: 1 branch
BranchTrueFalseTotal
if (!hook_data->cut_file) 0 0 0
50 {
51 char cutname[256] ;
52  
53 0 hook_data->split_pkt = pktnum ;
54  
55 0 sprintf(cutname, "%s-%04u.ts",
56 0 hook_data->ofname, ++hook_data->split_count) ;
57  
58 0 ▶ if (hook_data->debug) fprintf(stderr, " + + New split file %s at pkt %d\n", cutname, pktnum) ;
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug) fprintf(stderr, " + + New split file %s at pkt %d\n", cutname, pktnum) ; 0 0 0
59  
60  
61 0 hook_data->cut_file = open(cutname, O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE, 0666);
62  
63 0 ▶ if (hook_data->debug >= 10) fprintf(stderr, " + + -> save cut sequence: %s [fd %d]\n", cutname, hook_data->cut_file) ;
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug >= 10) fprintf(stderr, " + + -> save cut sequence: %s [fd %d]\n", cutname, hook_data->cut_file) ; 0 0 0
64 }
65  
66 0 ▶ if (hook_data->debug) fprintf(stderr, " + + next_split_file() - end\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug) fprintf(stderr, " + + next_split_file() - end\n") ; 0 0 0
67 0 }
68  
69 //---------------------------------------------------------------------------------------------------------
70 0 static void ts_split_hook(struct TS_pidinfo *pidinfo, uint8_t *packet, unsigned packet_len, void *user_data)
71 {
72 0 struct TS_cut_data *hook_data = (struct TS_cut_data *)user_data ;
73  
74 0 ▶ if (hook_data->debug) fprintf(stderr, "ts_split_hook() - start [hook_data @ %p]\n", hook_data) ;
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug) fprintf(stderr, "ts_split_hook() - start [hook_data @ %p]\n", hook_data) ; 0 0 0
75  
76 0 ▶ if (!hook_data)
Branch: 1 branch
BranchTrueFalseTotal
if (!hook_data) 0 0 0
77 {
78 0 fprintf(stderr, "!!ERROR: Null data pointer!!\n") ;
79 }
80 0 ▶ if (hook_data->magic != TS_CUTDATA_MAGIC)
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->magic != TS_CUTDATA_MAGIC) 0 0 0
81 {
82 0 fprintf(stderr, "!!ERROR: Corrupted data pointer!!\n") ;
83 }
84  
85 0 ▶ if (hook_data->debug >= 10)
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug >= 10) 0 0 0
86 {
87 0 _print_hook_data(hook_data) ;
88  
89 0 fprintf(stderr, "-> TS PID 0x%x (%u) [pkt %u] :: start=%d err=%d\n",
90 pidinfo->pid, pidinfo->pid,
91 pidinfo->pktnum,
92 0 pidinfo->pes_start ? 1 : 0,
93 0 pidinfo->pid_error ? 1 : 0) ;
94  
95 0 fprintf(stderr, " + initial : current cut = "); _print_cut_item(hook_data->current_cut) ;
96  
97 0 ▶ if (pidinfo->pktnum >= 100)
Branch: 1 branch
BranchTrueFalseTotal
if (pidinfo->pktnum >= 100) 0 0 0
98 {
99 0 fprintf(stderr, " + turning off debug....\n");
100 0 hook_data->debug = 0 ;
101 0 hook_data->tsreader->debug = 0 ;
102 }
103 }
104  
105 // check cut
106 0 ▶ if (hook_data->current_cut == UNSET_CUT_LIST)
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->current_cut == UNSET_CUT_LIST) 0 0 0
107 {
108 struct list_head *item;
109  
110 0 ▶ if (hook_data->debug >= 2) fprintf(stderr, " + find first cut entry...\n");
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug >= 2) fprintf(stderr, " + find first cut entry...\n"); 0 0 0
111 0 ▶ list_for_each(item, hook_data->cut_list)
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(item, hook_data->cut_list) 0 0 0
112 {
113 0 hook_data->current_cut = list_entry(item, struct TS_cut, next);
114 0 break;
115 }
116 }
117 0 ▶ if (hook_data->debug >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug >= 2) 0 0 0
118 {
119 0 fprintf(stderr, " + start: current_cut [%p] = ", hook_data->current_cut) ;
120 0 _print_cut_item(hook_data->current_cut) ;
121 }
122  
123 0 ▶ if (hook_data->current_cut != END_CUT_LIST)
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->current_cut != END_CUT_LIST) 0 0 0
124 {
125 // check current
126 0 ▶ if (pidinfo->pktnum < hook_data->current_cut->start)
Branch: 1 branch
BranchTrueFalseTotal
if (pidinfo->pktnum < hook_data->current_cut->start) 0 0 0
127 {
128 // still before start of next band
129 }
130 else
131 {
132 // Now >= start
133  
134 // New file at start of region
135 0 ▶ if ( pidinfo->pktnum == hook_data->current_cut->start )
Branch: 1 branch
BranchTrueFalseTotal
if ( pidinfo->pktnum == hook_data->current_cut->start ) 0 0 0
136 {
137 // save next band into new file
138 0 next_split_file(hook_data, pidinfo->pktnum) ;
139 }
140  
141 // writing : start -> end
142 0 ▶ else if (pidinfo->pktnum < hook_data->current_cut->end)
Branch: 1 branch
BranchTrueFalseTotal
else if (pidinfo->pktnum < hook_data->current_cut->end) 0 0 0
143 {
144 }
145  
146 // writing : end
147 0 ▶ else if (pidinfo->pktnum == hook_data->current_cut->end)
Branch: 1 branch
BranchTrueFalseTotal
else if (pidinfo->pktnum == hook_data->current_cut->end) 0 0 0
148 {
149 }
150  
151 // beyond end
152 else
153 {
154 struct list_head *item;
155  
156 0 ▶ if (hook_data->debug) fprintf(stderr, " + past end of this cut region, move to next...\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug) fprintf(stderr, " + past end of this cut region, move to next...\n") ; 0 0 0
157  
158 // ok, beyond this cut band - find next cut region
159 do
160 {
161 0 ▶ list_next_each(hook_data->current_cut, END_CUT_LIST, item, hook_data->cut_list)
Branch: 1 branch
BranchTrueFalseTotal
list_next_each(hook_data->current_cut, END_CUT_LIST, item, hook_data->cut_list) 0 0 0
162 {
163 0 hook_data->current_cut = list_entry(item, struct TS_cut, next);
164 0 break;
165 }
166  
167 0 ▶ } while ( (hook_data->current_cut != END_CUT_LIST) && (pidinfo->pktnum > hook_data->current_cut->start) ) ;
Branch: 2 branches
BranchTrueFalseTotal
} while ( (hook_data->current_cut != END_CUT_LIST) && (pidinfo->pktnum > hook_data->current_cut->start) ) ; 0 0 0
} while ( (hook_data->current_cut != END_CUT_LIST) && (pidinfo->pktnum > hook_data->current_cut->start) ) ; 0 0 0
168  
169 0 ▶ if (hook_data->debug >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug >= 2) 0 0 0
170 {
171 0 fprintf(stderr, " + next: current_cut %p = ", hook_data->current_cut) ;
172 0 _print_cut_item(hook_data->current_cut) ;
173 }
174  
175 // save next band into new file
176 0 next_split_file(hook_data, pidinfo->pktnum) ;
177 0 ▶ if (hook_data->debug) fprintf(stderr, " + New split file %04d at pkt %d\n", hook_data->split_count, pidinfo->pktnum) ;
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug) fprintf(stderr, " + New split file %04d at pkt %d\n", hook_data->split_count, pidinfo->pktnum) ; 0 0 0
178 }
179 }
180 }
181  
182 // write if allowed to
183 0 ▶ if (hook_data->cut_file)
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->cut_file) 0 0 0
184 {
185 0 ▶ if (hook_data->debug >= 10)
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug >= 10) 0 0 0
186 {
187 0 fprintf(stderr, " + Writing TS PID 0x%x [pkt %u]...\n",
188 pidinfo->pid,
189 pidinfo->pktnum) ;
190 }
191  
192 0 write(hook_data->cut_file, packet, packet_len);
193 }
194  
195 0 ▶ if (hook_data->debug) fprintf(stderr, "ts_split_hook() - end\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data->debug) fprintf(stderr, "ts_split_hook() - end\n") ; 0 0 0
196 0 }
197  
198  
199  
200 //---------------------------------------------------------------------------------------------------------
201 0 int ts_split(char *filename, char *ofilename, struct list_head *cuts_array, unsigned debug)
202 {
203 int file;
204 struct TS_cut_data hook_data ;
205 struct TS_reader *tsreader ;
206  
207 0 ▶ if (debug >= 2) fprintf(stderr, "ts_split() - start [hook_data @ %p]\n", &hook_data) ;
Branch: 1 branch
BranchTrueFalseTotal
if (debug >= 2) fprintf(stderr, "ts_split() - start [hook_data @ %p]\n", &hook_data) ; 0 0 0
208  
209 0 hook_data.magic = TS_CUTDATA_MAGIC ;
210 0 hook_data.magic_end = 0xdeaddead ;
211 0 hook_data.cut_list = cuts_array ;
212  
213 0 hook_data.current_cut = UNSET_CUT_LIST ;
214 0 hook_data.debug = debug ;
215 0 hook_data.ofile = 0 ;
216 0 hook_data.split_count = 0 ;
217 0 hook_data.cut_file = 0 ;
218 0 hook_data.ofile = 0 ;
219  
220 0 tsreader = tsreader_new(filename) ;
221 0 ▶ if (!tsreader)
Branch: 1 branch
BranchTrueFalseTotal
if (!tsreader) 0 0 0
222 {
223 0 return(dvb_error_code);
224 }
225 0 hook_data.tsreader = tsreader ;
226 0 ▶ if (debug >= 5) _print_hook_data(&hook_data) ;
Branch: 1 branch
BranchTrueFalseTotal
if (debug >= 5) _print_hook_data(&hook_data) ; 0 0 0
227 0 remove_ext(filename, hook_data.fname) ;
228 0 remove_ext(ofilename, hook_data.ofname) ;
229 0 ▶ if (debug >= 5) _print_hook_data(&hook_data) ;
Branch: 1 branch
BranchTrueFalseTotal
if (debug >= 5) _print_hook_data(&hook_data) ; 0 0 0
230  
231 0 tsreader->ts_hook = ts_split_hook ;
232 0 tsreader->user_data = &hook_data ;
233 0 tsreader->debug = debug ;
234  
235  
236 // start first file
237 0 next_split_file(&hook_data, 0) ;
238  
239 0 ▶ if (debug >= 5) fprintf(stderr, " + parse file...\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (debug >= 5) fprintf(stderr, " + parse file...\n") ; 0 0 0
240  
241 // parse data
242 0 ts_parse(tsreader) ;
243  
244 0 ▶ if (hook_data.cut_file)
Branch: 1 branch
BranchTrueFalseTotal
if (hook_data.cut_file) 0 0 0
245 0 close(hook_data.cut_file) ;
246  
247 0 ▶ if (debug >= 5) fprintf(stderr, " + free tsreader...\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (debug >= 5) fprintf(stderr, " + free tsreader...\n") ; 0 0 0
248 0 tsreader_free(tsreader) ;
249  
250 0 ▶ if (debug >= 5) fprintf(stderr, " + free cut list...\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (debug >= 5) fprintf(stderr, " + free cut list...\n") ; 0 0 0
251 0 free_cut_list(hook_data.cut_list) ;
252  
253 0 ▶ if (debug >= 2) fprintf(stderr, "ts_split() - start\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (debug >= 2) fprintf(stderr, "ts_split() - start\n") ; 0 0 0
254  
255 0 return(dvb_error_code) ;
256 }
257  
258  
« clib/dvb_ts_lib/ts_skip.c ↑ Summary clib/libmpeg2/alloc.c »