clib/dvb_ts_lib/ts_parse.c

filter: Statementstmt 0.0% 0 / 806 Branchbran 0.0% 0 / 576 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 1382 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« clib/dvb_ts_lib/ts_cut.c ↑ Summary clib/dvb_ts_lib/ts_skip.c »
1 /*
2 * ts_parse.c
3 *
4 * Created on: 28 Apr 2010
5 * Author: sdprice1
6 */
7  
8 // VERSION = 2.00
9  
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <ctype.h>
16 #include <fcntl.h>
17 #include <inttypes.h>
18  
19 #include "ts_parse.h"
20 #include "ts_bits.h"
21 #include "tables/parse_si.h"
22 #include "dvbsnoop/crc32.h"
23  
24 /*=============================================================================================*/
25 // libmpeg2
26 void dump_state (FILE * f, mpeg2_state_t state, const mpeg2_info_t * info,
27 int offset, int verbose);
28  
29 /*=============================================================================================*/
30  
31 // Check for PTS/DTS wrap
32 #define MAX_TS_DIFF (60 * TS_FREQ)
33 #define TS_WRAP (1LL << 33)
34  
35 // libmpeg2 states
36 static char *STATE_STRINGS[16] = {
37 [0 ... 15] = "UNKNOWN",
38 [STATE_BUFFER] = "STATE_BUFFER",
39 [STATE_SEQUENCE] = "STATE_SEQUENCE",
40 [STATE_SEQUENCE_REPEATED] = "STATE_SEQUENCE_REPEATED",
41 [STATE_GOP] = "STATE_GOP",
42 [STATE_PICTURE] = "STATE_PICTURE",
43 [STATE_SLICE_1ST] = "STATE_SLICE_1ST",
44 [STATE_PICTURE_2ND] = "STATE_PICTURE_2ND",
45 [STATE_SLICE] = "STATE_SLICE",
46 [STATE_END] = "STATE_END",
47 [STATE_INVALID] = "STATE_INVALID",
48 [STATE_INVALID_END] = "STATE_INVALID_END",
49 [STATE_SEQUENCE_MODIFIED] = "STATE_SEQUENCE_MODIFIED"
50 } ;
51  
52  
53  
54 // mpeg2audio settings
55 #define AUDIOBUFFER 80000
56 #define STORAGE_SIZE 100000
57 #define MIN_BUFFER 4000
58  
59  
60 //define DEBUG_PTS
61  
62 #define CHECK_MAGIC
63 #ifndef CHECK_MAGIC
64 #undef CHECK_TS_READER
65 #define CHECK_TS_READER(b)
66 #undef CHECK_TS_BUFF
67 #define CHECK_TS_BUFF(b)
68 #endif
69  
70  
71 /*=============================================================================================*/
72 #define FRAMEINFO_BLOCKSIZE 1024
73  
74 //---------------------------------------------------------------------------------------------------------------------------
75 // Return the frame info array entry for this index. Allocates more memory as appropriate
76 0 struct TS_frame_info *frame_info_entry(struct TS_reader *tsreader, unsigned index)
77 {
78 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
79  
80 // Allocate if not already allocated
81 0 ▶ if (!tsreader->mpeg2.frame_info_list)
Branch: 1 branch
BranchTrueFalseTotal
if (!tsreader->mpeg2.frame_info_list) 0 0 0
82 {
83 0 tsreader->mpeg2.frame_info_list_size = FRAMEINFO_BLOCKSIZE ;
84 0 tsreader->mpeg2.frame_info_list = (struct TS_frame_info *)malloc(tsreader->mpeg2.frame_info_list_size * sizeof(struct TS_frame_info)) ;
85 0 memset(tsreader->mpeg2.frame_info_list, 0, tsreader->mpeg2.frame_info_list_size * sizeof(struct TS_frame_info)) ;
86 }
87  
88 // If index > array size, expand
89 0 ▶ if (index >= tsreader->mpeg2.frame_info_list_size)
Branch: 1 branch
BranchTrueFalseTotal
if (index >= tsreader->mpeg2.frame_info_list_size) 0 0 0
90 {
91 0 tsreader->mpeg2.frame_info_list_size += FRAMEINFO_BLOCKSIZE ;
92 0 tsreader->mpeg2.frame_info_list = (struct TS_frame_info *)realloc(tsreader->mpeg2.frame_info_list, tsreader->mpeg2.frame_info_list_size * sizeof(struct TS_frame_info)) ;
93 0 memset(&tsreader->mpeg2.frame_info_list[tsreader->mpeg2.frame_info_list_size - FRAMEINFO_BLOCKSIZE], 0, FRAMEINFO_BLOCKSIZE * sizeof(struct TS_frame_info)) ;
94 }
95  
96 0 return &tsreader->mpeg2.frame_info_list[index] ;
97 }
98  
99 //---------------------------------------------------------------------------------------------------------------------------
100 // Free the frame info array
101 0 void free_frame_info_list(struct TS_reader *tsreader)
102 {
103 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
104 0 ▶ if (tsreader->mpeg2.frame_info_list_size)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.frame_info_list_size) 0 0 0
105 {
106 0 tsreader->mpeg2.frame_info_list_size = 0 ;
107 0 free(tsreader->mpeg2.frame_info_list) ;
108 0 tsreader->mpeg2.frame_info_list = NULL ;
109 }
110 0 }
111  
112  
113 /*=============================================================================================*/
114  
115 /* ----------------------------------------------------------------------- */
116 0 void dump_buff(const uint8_t *payload, unsigned payload_len, unsigned display_len)
117 {
118 unsigned byte ;
119  
120 0 ▶ if ((display_len == 0) || (display_len > payload_len))
Branch: 2 branches
BranchTrueFalseTotal
if ((display_len == 0) || (display_len > payload_len)) 0 0 0
if ((display_len == 0) || (display_len > payload_len)) 0 0 0
121 0 display_len = payload_len ;
122  
123 0 printf("---[ Len: %d Displaying: %d ]------------------------------------------\n", payload_len, display_len) ;
124 0 ▶ for (byte=0; byte < display_len; ++byte)
Branch: 1 branch
BranchTrueFalseTotal
for (byte=0; byte < display_len; ++byte) 0 0 0
125 {
126 0 ▶ if (byte % 32 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (byte % 32 == 0) 0 0 0
127 {
128 0 printf("%04x: ", byte) ;
129 }
130 0 printf("%02x ", payload[byte]) ;
131 0 ▶ if (byte % 8 == 7)
Branch: 1 branch
BranchTrueFalseTotal
if (byte % 8 == 7) 0 0 0
132 {
133 0 printf(" - ") ;
134 }
135 0 ▶ if (byte % 32 == 31)
Branch: 1 branch
BranchTrueFalseTotal
if (byte % 32 == 31) 0 0 0
136 {
137 0 printf("\n") ;
138 }
139 }
140 0 ▶ if (display_len < payload_len)
Branch: 1 branch
BranchTrueFalseTotal
if (display_len < payload_len) 0 0 0
141 {
142 0 printf("[") ;
143 0 ▶ for (; (byte < display_len+3) && (byte < payload_len); ++byte)
Branch: 2 branches
BranchTrueFalseTotal
for (; (byte < display_len+3) && (byte < payload_len); ++byte) 0 0 0
for (; (byte < display_len+3) && (byte < payload_len); ++byte) 0 0 0
144 {
145 0 printf("%02x ", payload[byte]) ;
146 0 ▶ if (byte % 32 == 31)
Branch: 1 branch
BranchTrueFalseTotal
if (byte % 32 == 31) 0 0 0
147 {
148 0 printf("\n") ;
149 }
150 }
151 0 printf("... ]") ;
152 }
153 0 printf("\n------------------------------------------------------------\n") ;
154 0 }
155  
156  
157 /*=============================================================================================*/
158 // TS_buff
159  
160 /* ----------------------------------------------------------------------- */
161 0 void buffer_free(struct TS_buffer **buff)
162 {
163 0 struct TS_buffer *bp = *buff ;
164  
165 //fprintf(stderr, "buffer_free(**TS_buffer=%p)\n", buff) ;
166  
167 0 ▶ if (bp)
Branch: 1 branch
BranchTrueFalseTotal
if (bp) 0 0 0
168 {
169 // fprintf(stderr, " + **TS_buffer=%p [=> %p]\n", buff, *buff) ;
170 // fprintf(stderr, " + buff_size=%d buff= %p\n", bp->buff_size, bp->buff) ;
171 0 ▶ if (bp->buff_size)
Branch: 1 branch
BranchTrueFalseTotal
if (bp->buff_size) 0 0 0
172 {
173 0 free(bp->buff) ;
174 }
175 0 free(bp) ;
176 }
177 0 *buff = NULL ;
178 0 }
179  
180 /* ----------------------------------------------------------------------- */
181 0 struct TS_buffer *buffer_new()
182 {
183 struct TS_buffer *bp ;
184  
185 // create struct
186 0 bp = (struct TS_buffer *)malloc(sizeof(struct TS_buffer)) ;
187 0 CLEAR_MEM(bp) ;
188  
189 0 bp->MAGIC = MAGIC_BUFF ;
190  
191  
192 // create buffer
193 0 bp->data_len = 0 ;
194 0 bp->buff = (uint8_t *)malloc(TS_BUFFSIZE*sizeof(uint8_t)) ;
195 0 bp->buff_size = TS_BUFFSIZE ;
196  
197 //fprintf(stderr, "buffer_new() TS_buffer=%p [buff = %p])\n", bp, bp->buff) ;
198  
199 0 return bp ;
200 }
201  
202 /* ----------------------------------------------------------------------- */
203 0 void buffer_clear(struct TS_buffer *bp)
204 {
205 //CLEAR_MEM(bp)
206 0 bp->data_len = 0 ;
207 0 }
208  
209  
210 /* ----------------------------------------------------------------------- */
211 0 uint8_t *buffer_data(struct TS_buffer **buff, const uint8_t *data, unsigned data_len)
212 {
213 struct TS_buffer *bp ;
214 unsigned new_len ;
215  
216 //fprintf(stderr, "buffer_data(**TS_buffer=%p, data_len=%d)\n", buff, data_len) ;
217  
218 // check for first time
219 0 ▶ if (!*buff)
Branch: 1 branch
BranchTrueFalseTotal
if (!*buff) 0 0 0
220 {
221 // create struct
222 0 *buff = buffer_new() ;
223 }
224 0 bp = *buff ;
225 0 new_len = bp->data_len + data_len ;
226  
227 // fprintf(stderr, " + **TS_buffer=%p [=> %p] : buff=%p\n", buff, *buff, bp->buff) ;
228  
229 // able to add data?
230 0 ▶ if (new_len >= bp->buff_size)
Branch: 1 branch
BranchTrueFalseTotal
if (new_len >= bp->buff_size) 0 0 0
231 {
232 // fprintf(stderr, " + + realloc()\n") ;
233 0 bp->buff_size += TS_BUFFSIZE ;
234 0 bp->buff = realloc(bp->buff, bp->buff_size) ;
235 // fprintf(stderr, " + + buff=%p\n", bp->buff) ;
236 }
237 0 ▶ else if (bp->buff_size - new_len > 2*TS_BUFFSIZE )
Branch: 1 branch
BranchTrueFalseTotal
else if (bp->buff_size - new_len > 2*TS_BUFFSIZE ) 0 0 0
238 {
239 // fprintf(stderr, " + + downsize() data_len=%d, new_len=%d : curr size=%d\n", bp->data_len, new_len, bp->buff_size) ;
240 // scale down if required
241 0 bp->buff_size = ( ( (new_len + TS_BUFFSIZE-1) / TS_BUFFSIZE) + 1) * TS_BUFFSIZE ;
242 // fprintf(stderr, " + + new size=%d\n", bp->buff_size) ;
243 0 bp->buff = realloc(bp->buff, bp->buff_size) ;
244 // fprintf(stderr, " + + buff=%p\n", bp->buff) ;
245 }
246  
247 // fprintf(stderr, " + buff=%p .. %p : copying into %p .. %p\n", bp->buff, &bp->buff[bp->buff_size-1], &bp->buff[bp->data_len], &bp->buff[bp->data_len+data_len-1]) ;
248  
249 // copy data
250 0 memcpy(&bp->buff[bp->data_len], data, data_len) ;
251 0 bp->data_len += data_len ;
252  
253 0 return bp->buff ;
254 }
255  
256  
257 /*=============================================================================================*/
258 // TS_pid
259  
260 /* ----------------------------------------------------------------------- */
261 // get existing or return created
262 0 static struct TS_pid* piditem_get(struct list_head *pid_list, struct TS_pidinfo *pidinfo)
263 {
264 struct TS_pid *piditem;
265 struct list_head *item;
266  
267 0 ▶ list_for_each(item, pid_list)
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(item, pid_list) 0 0 0
268 {
269 0 piditem = list_entry(item, struct TS_pid, next);
270 0 ▶ if (piditem->pidinfo.pid != pidinfo->pid)
Branch: 1 branch
BranchTrueFalseTotal
if (piditem->pidinfo.pid != pidinfo->pid) 0 0 0
271 0 continue;
272  
273 0 return piditem;
274 }
275  
276 0 piditem = malloc(sizeof(*piditem));
277 0 CLEAR_MEM(piditem);
278 0 piditem->MAGIC = MAGIC_PID ;
279  
280 // struct TS_pid {
281 // struct list_head next;
282 //
283 // struct TS_pidinfo pidinfo ;
284 // struct TS_buffer * pes_buff ;
285 // enum TS_pesstate pes_state ;
286 // };
287  
288 0 memcpy(&piditem->pidinfo, pidinfo, sizeof(*pidinfo)) ;
289 0 piditem->pes_buff = buffer_new() ;
290 0 piditem->pes_state = PES_SKIP ;
291 0 list_add_tail(&piditem->next, pid_list);
292  
293 0 piditem->pesinfo.start_pts = UNSET_TS ;
294 0 piditem->pesinfo.start_dts = UNSET_TS ;
295 0 piditem->pesinfo.end_pts = UNSET_TS ;
296 0 piditem->pesinfo.end_dts = UNSET_TS ;
297 0 piditem->pesinfo.pts = UNSET_TS ;
298 0 piditem->pesinfo.dts = UNSET_TS ;
299  
300 0 piditem->pesinfo.pes_psi = T_PES ;
301  
302 0 return piditem;
303 }
304  
305 /* ----------------------------------------------------------------------- */
306 // get existing or return created
307 0 static void piditem_free(struct TS_pid * piditem)
308 {
309 // struct TS_pid {
310 // struct list_head next;
311 //
312 // struct TS_pidinfo pidinfo ;
313 // struct TS_buffer * pes_buff ;
314 // enum TS_pesstate pes_state ;
315 // };
316  
317 0 buffer_free(&piditem->pes_buff) ;
318  
319 0 free(piditem);
320 0 }
321  
322 /* ----------------------------------------------------------------------- */
323 // Start of new PES, reset flags/counters etc
324 0 static void pes_start(struct TS_pid * pid_item)
325 {
326 0 pid_item->pes_buff->data_len = 0 ;
327 0 pid_item->pes_state = PES_HEADER ;
328  
329 0 pid_item->pesinfo.psi_error = 0 ;
330 0 pid_item->pesinfo.pes_error = 0 ;
331 0 pid_item->pesinfo.ts_error = 0 ;
332 0 }
333  
334  
335 /*=============================================================================================*/
336 // TS_state
337  
338 //struct TS_state {
339 // unsigned MAGIC ;
340 // struct TS_pidinfo pidinfo ;
341 // struct TS_pid *pid_item ;
342 //
343 // // list of TS_pid
344 // struct list_head pid_list;
345 //
346 // // Set to total number of packets
347 // unsigned total_pkts ;
348 //
349 // // set to min/max pts/dts times
350 // int64_t start_ts ;
351 // int64_t end_ts ;
352 //
353 // // Stop flag - set by callbacks to exit the loop
354 // unsigned stop_flag ;
355 //
356 //// // list of TS packets
357 //// unsigned start_pktnum ;
358 ////
359 //// // list of TS_pkt
360 //// struct list_head pkt_list ;
361 //};
362  
363 /* ----------------------------------------------------------------------- */
364 0 static void tsstate_free(struct TS_state *tsstate)
365 {
366 struct list_head *item, *safe;
367 struct TS_pid *piditem;
368 //struct TS_pkt *pktitem;
369  
370 0 ▶ list_for_each_safe(item,safe,&tsstate->pid_list)
Branch: 1 branch
BranchTrueFalseTotal
list_for_each_safe(item,safe,&tsstate->pid_list) 0 0 0
371 {
372 0 piditem = list_entry(item, struct TS_pid, next);
373 0 list_del(&piditem->next);
374 0 piditem_free(piditem);
375 };
376 // list_for_each_safe(item,safe,&tsstate->pkt_list)
377 // {
378 // pktitem = list_entry(item, struct TS_pkt, next);
379 // list_del(&pktitem->next);
380 // free(pktitem);
381 // };
382  
383 0 free(tsstate) ;
384 0 }
385  
386 /* ----------------------------------------------------------------------- */
387 0 static struct TS_state *tsstate_new()
388 {
389 struct TS_state *tsstate ;
390  
391 // create struct
392 0 tsstate = (struct TS_state *)malloc(sizeof(struct TS_state)) ;
393 0 CLEAR_MEM(tsstate) ;
394 0 tsstate->MAGIC = MAGIC_STATE ;
395  
396 0 tsstate->pidinfo.pktnum = 0 ;
397 0 tsstate->pidinfo.pid_error = 0 ;
398  
399 0 INIT_LIST_HEAD(&tsstate->pid_list);
400  
401 0 tsstate->start_ts = UNSET_TS ;
402 0 tsstate->end_ts = UNSET_TS ;
403  
404 0 tsstate->pid_item = (struct TS_pid *)0 ;
405 0 tsstate->total_pkts = 0 ;
406 0 tsstate->stop_flag = 0 ;
407  
408 // // list of TS packets
409 // INIT_LIST_HEAD(&tsstate->pkt_list);
410 // tsstate->start_pktnum = 0LLU ;
411  
412 0 return tsstate ;
413 }
414  
415  
416 /*=============================================================================================*/
417  
418  
419 /* ----------------------------------------------------------------------- */
420 0 static int getbuff(int fh, uint8_t *buffer, int *count)
421 {
422 int rc ;
423 int status ;
424 int data_ready ;
425  
426 0 status = 0 ;
427  
428 0 rc = read(fh, buffer, *count);
429  
430 // return actual read amount
431 0 *count = rc ;
432  
433 0 switch (rc) {
434 0 case -1:
435 0 RETURN_DVB_ERROR(ERR_READ) ;
436 0 case 0:
437 0 RETURN_DVB_ERROR(ERR_EOF) ;
438  
439 0 default:
440 0 break;
441 }
442 0 return(status) ;
443 }
444  
445  
446 //---------------------------------------------------------------------------------------------------------------------------
447 0 unsigned mpeg2_frame_flags(struct TS_reader *tsreader, struct TS_state *tsstate, uint8_t *pesdata, unsigned pesdata_len)
448 {
449 0 uint8_t *p = pesdata ;
450 0 unsigned flags = 0 ;
451  
452 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
453 0 ▶ while (p && ((int)pesdata_len-(int)(p-pesdata) >= 4) && (p = memchr(p, 0, (int)pesdata_len-(int)(p-pesdata))) )
Branch: 3 branches
BranchTrueFalseTotal
while (p && ((int)pesdata_len-(int)(p-pesdata) >= 4) && (p = memchr(p, 0, (int)pesdata_len-(int)(p-pesdata))) ) 0 0 0
while (p && ((int)pesdata_len-(int)(p-pesdata) >= 4) && (p = memchr(p, 0, (int)pesdata_len-(int)(p-pesdata))) ) 0 0 0
while (p && ((int)pesdata_len-(int)(p-pesdata) >= 4) && (p = memchr(p, 0, (int)pesdata_len-(int)(p-pesdata))) ) 0 0 0
454 {
455 0 ▶ if ((int)pesdata_len-(int)(p-pesdata) >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if ((int)pesdata_len-(int)(p-pesdata) >= 4) 0 0 0
456 {
457 0 ▶ if ( (p[0]==0) && (p[1]==0) && (p[2]==1) )
Branch: 3 branches
BranchTrueFalseTotal
if ( (p[0]==0) && (p[1]==0) && (p[2]==1) ) 0 0 0
if ( (p[0]==0) && (p[1]==0) && (p[2]==1) ) 0 0 0
if ( (p[0]==0) && (p[1]==0) && (p[2]==1) ) 0 0 0
458 {
459 0 ▶ if ( p[3]==0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( p[3]==0 ) 0 0 0
460 {
461 0 flags |= FRAME_FLAG_START ;
462  
463 0 ▶ tsparse_dbg_prt(200, (" @@ Video Start @@ pes start pkt %u : [at offset %d]\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(200, (" @@ Video Start @@ pes start pkt %u : [at offset %d]\n", 0 0 0
464 tsstate->pid_item->pesinfo.start_pkt, (int)(p-pesdata))) ;
465 }
466 else
467 {
468 char *codestr ;
469 char tmp[256] ;
470  
471 0 int code = 0x100 + (int)p[3] ;
472 0 switch (code)
473 {
474 0 case user_data_start_code:
475 0 flags |= FRAME_FLAG_USER_DATA ;
476 0 codestr = "USER DATA" ;
477 0 break ;
478  
479 0 case sequence_header_code:
480 0 flags |= FRAME_FLAG_SEQ_HEAD ;
481 0 codestr = "SEQ HEAD" ;
482 0 break ;
483  
484 0 case sequence_error_code:
485 0 flags |= FRAME_FLAG_SEQ_ERROR ;
486 0 codestr = "SEQ ERROR" ;
487 0 break ;
488  
489 0 case extension_start_code:
490 0 flags |= FRAME_FLAG_EXTENSION ;
491 0 codestr = "EXTENSION" ;
492 0 break ;
493  
494 0 case sequence_end_code:
495 0 flags |= FRAME_FLAG_SEQ_END ;
496 0 codestr = "SEQ END" ;
497 0 break ;
498  
499 0 case group_start_code:
500 0 flags |= FRAME_FLAG_GOP ;
501 0 codestr = "GOP" ;
502 0 break ;
503  
504 0 default:
505 0 ▶ if ( (code >= slice_start_code_start) && (code <= slice_start_code_end) )
Branch: 2 branches
BranchTrueFalseTotal
if ( (code >= slice_start_code_start) && (code <= slice_start_code_end) ) 0 0 0
if ( (code >= slice_start_code_start) && (code <= slice_start_code_end) ) 0 0 0
506 {
507 0 flags |= FRAME_FLAG_SLICE ;
508 0 sprintf(tmp, "SLICE %d", code - slice_start_code_start + 1) ;
509 0 codestr = tmp ;
510 }
511 0 ▶ else if ( (code >= system_start_code_start) && (code <= system_start_code_end) )
Branch: 2 branches
BranchTrueFalseTotal
else if ( (code >= system_start_code_start) && (code <= system_start_code_end) ) 0 0 0
else if ( (code >= system_start_code_start) && (code <= system_start_code_end) ) 0 0 0
512 {
513 0 flags |= FRAME_FLAG_SYSTEM ;
514 0 sprintf(tmp, "SYSTEM") ;
515 0 codestr = tmp ;
516 }
517 else
518 {
519 0 flags |= FRAME_FLAG_RESERVED ;
520 0 codestr = "" ;
521 }
522 0 break ;
523 }
524  
525 0 ▶ tsparse_dbg_prt(200, (" @#@ code 0x%02x %s @#@ pes start pkt %u : [at offset %d]\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(200, (" @#@ code 0x%02x %s @#@ pes start pkt %u : [at offset %d]\n", 0 0 0
526 (int)p[3], codestr, tsstate->pid_item->pesinfo.start_pkt, (int)(p-pesdata))) ;
527 }
528 0 p+=3 ;
529 }
530 }
531  
532 0 ▶ if ((int)pesdata_len-(int)(p-pesdata) > 4)
Branch: 1 branch
BranchTrueFalseTotal
if ((int)pesdata_len-(int)(p-pesdata) > 4) 0 0 0
533 {
534 0 ++p ;
535 }
536 else
537 {
538 0 p = NULL ;
539 }
540 }
541  
542 0 return flags ;
543 }
544  
545  
546 //---------------------------------------------------------------------------------------------------------------------------
547 // NOTE: When skipping packets/frames, we need to have seen the previous GOP to get the frame for this GOP. Otherwise, we only
548 // get a frame out for the frame following the GOP
549 //
550 // GOP GOP GOP
551 // DATA: ------|--------------------------|--------------------------|--------------------------|
552 // 012345678901234567890123456012345678901234567890123456012345678901234567890123456
553 //
554 // Skip=0 v
555 // ------|--------------------------|--------------------------|--------------------------|
556 // |
557 // v
558 // First frame = Frame 0
559 //
560 // Skip=2nd GOP
561 // v
562 // ------|--------------------------|--------------------------|--------------------------|
563 // 012345678901234567890123456012345678901234567890123456012345678901234567890123456
564 // |
565 // v
566 // First frame
567 //
568 // i.e. first frame out = Frame 27 NOT Frame 26
569 //
570  
571 #define MAX_STATE_CYCLE 100
572 0 static void process_mpeg2(struct TS_reader *tsreader, struct TS_state *tsstate, uint8_t *pesdata, unsigned pesdata_len)
573 {
574 mpeg2_state_t state;
575 0 unsigned state_cycle = 0 ;
576 unsigned frame_flags ;
577  
578 // for debug
579 static int total_offset = 0;
580 const mpeg2_info_t * info;
581  
582 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
583  
584  
585 0 ▶ tsparse_dbg_prt(202, ("\n\n--[ video ]----------\n%d bytes\n", pesdata_len)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(202, ("\n\n--[ video ]----------\n%d bytes\n", pesdata_len)) ; 0 0 0
586 0 ▶ if (tsreader->debug >= 202)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 202) 0 0 0
587 0 dump_buff(pesdata, pesdata_len, pesdata_len) ;
588  
589 // mainly looking for GOP start
590 0 frame_flags = mpeg2_frame_flags(tsreader, tsstate, pesdata, pesdata_len) ;
591 0 ▶ if (frame_flags & FRAME_FLAG_GOP)
Branch: 1 branch
BranchTrueFalseTotal
if (frame_flags & FRAME_FLAG_GOP) 0 0 0
592 {
593 0 tsreader->mpeg2.gop_pktnum = tsstate->pid_item->pesinfo.start_pkt ;
594 }
595  
596 // check this is a video stream
597 0 ▶ if ( (tsstate->pid_item->pesinfo.code & video_stream_mask) != video_stream)
Branch: 1 branch
BranchTrueFalseTotal
if ( (tsstate->pid_item->pesinfo.code & video_stream_mask) != video_stream) 0 0 0
598 {
599 0 return ;
600 }
601  
602 // for debug
603 0 info = mpeg2_info(tsreader->mpeg2.decoder);
604 0 total_offset += pesdata_len ;
605  
606 do
607 {
608 0 state = mpeg2_parse (tsreader->mpeg2.decoder);
609  
610  
611 /*
612 * With verbose=100, dump_state format:
613 *
614 * %8x = file offset
615 * %c%c = seq code, gop code S=seq, G=gop
616 * %c%c%c = curr fbuff code, curr pic code, curr pic2 code (a-z or A-Z for new) 0..25
617 * %c%c%c = disp fbuff code, disp pic code, disp pic2 code
618 * %c = discard fbuf code
619 * %s = State name
620 *
621 *
622 * GOP = Group of 26 pictures
623 */
624 0 ▶ tsparse_dbg_prt(200, ("---[ mpeg2 dump_state ]------------------------\nstate = %s [%d]\n", STATE_STRINGS[state], state));
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(200, ("---[ mpeg2 dump_state ]------------------------\nstate = %s [%d]\n", STATE_STRINGS[state], state)); 0 0 0
625 0 ▶ if (tsreader->debug >= 202)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 202) 0 0 0
626 0 dump_state (stderr, state, info,
627 0 total_offset - mpeg2_getpos(tsreader->mpeg2.decoder), 100 /* verbosity */);
628 0 ▶ tsparse_dbg_prt(102, ("state = %s [%d]\n", STATE_STRINGS[state], state));
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("state = %s [%d]\n", STATE_STRINGS[state], state)); 0 0 0
629  
630 0 switch (state) {
631 0 case STATE_BUFFER:
632 // HAVE TO COPY DATA TO NEW BUFFER SO THAT libmpeg2 HAS A COPY TO WORK ON IN THE MEANTIME!
633 0 ▶ if (tsreader->mpeg2.video_buffer) free(tsreader->mpeg2.video_buffer) ;
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.video_buffer) free(tsreader->mpeg2.video_buffer) ; 0 0 0
634 0 tsreader->mpeg2.video_buffer = (uint8_t *)malloc(pesdata_len * sizeof(uint8_t)) ;
635 0 memcpy(tsreader->mpeg2.video_buffer, pesdata, pesdata_len) ;
636 0 mpeg2_buffer (tsreader->mpeg2.decoder, tsreader->mpeg2.video_buffer, tsreader->mpeg2.video_buffer + pesdata_len);
637 0 break;
638  
639 0 case STATE_SEQUENCE:
640 0 ▶ if (tsreader->mpeg2.convert_rgb)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.convert_rgb) 0 0 0
641 {
642 0 mpeg2_convert(tsreader->mpeg2.decoder, mpeg2convert_rgb24, NULL);
643 }
644 0 break;
645 0 case STATE_SLICE:
646 case STATE_END:
647 case STATE_INVALID_END:
648 0 ▶ if (tsreader->mpeg2.info->display_fbuf)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.info->display_fbuf) 0 0 0
649 {
650 // call callback
651 // if (tsreader->mpeg2_hook)
652 {
653 struct TS_frame_info *frame_info ;
654  
655 0 unsigned frame_info_index = (unsigned)tsreader->mpeg2.info->current_picture->tag ;
656 0 frame_info = frame_info_entry(tsreader, frame_info_index) ;
657  
658 0 ▶ if (tsreader->mpeg2.convert_rgb)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.convert_rgb) 0 0 0
659 {
660 0 tsreader->mpeg2_rgb_hook(&tsstate->pidinfo,
661 frame_info,
662 tsreader->mpeg2.info,
663 tsreader->user_data) ;
664 }
665 else
666 {
667 0 tsreader->mpeg2_hook(&tsstate->pidinfo,
668 frame_info,
669 tsreader->mpeg2.info,
670 tsreader->user_data) ;
671 }
672 }
673 0 ++tsreader->mpeg2.framenum ;
674 }
675 0 break;
676  
677 0 case STATE_INVALID:
678 0 break ;
679 0 case STATE_SEQUENCE_REPEATED:
680 0 break ;
681 0 case STATE_GOP:
682 0 break ;
683 0 case STATE_PICTURE:
684 0 break ;
685 0 case STATE_SLICE_1ST:
686 0 break ;
687 0 case STATE_PICTURE_2ND:
688 0 break ;
689 0 case STATE_SEQUENCE_MODIFIED:
690 0 break ;
691  
692 0 default:
693 0 break;
694 }
695 0 ▶ } while ( (state != STATE_BUFFER) && (state_cycle++ < MAX_STATE_CYCLE) ) ;
Branch: 2 branches
BranchTrueFalseTotal
} while ( (state != STATE_BUFFER) && (state_cycle++ < MAX_STATE_CYCLE) ) ; 0 0 0
} while ( (state != STATE_BUFFER) && (state_cycle++ < MAX_STATE_CYCLE) ) ; 0 0 0
696  
697 // Use the tags to store:
698 // tag = frame info index
699 // tag2 = packet count of pes containing GOP (once every 26 frames)
700 0 struct TS_frame_info *frame_info = frame_info_entry(tsreader, tsreader->mpeg2.frame_info_index) ;
701 0 frame_info->framenum = tsreader->mpeg2.framenum ;
702 0 frame_info->gop_pkt = tsreader->mpeg2.gop_pktnum ;
703 0 memcpy(&frame_info->pesinfo, &tsstate->pid_item->pesinfo, sizeof(tsstate->pid_item->pesinfo)) ;
704 0 memcpy(&frame_info->pidinfo, &tsstate->pid_item->pidinfo, sizeof(tsstate->pid_item->pidinfo)) ;
705  
706 0 mpeg2_tag_picture(tsreader->mpeg2.decoder, tsreader->mpeg2.frame_info_index, tsreader->mpeg2.gop_pktnum) ;
707 0 tsreader->mpeg2.frame_info_index++ ;
708  
709 0 ▶ tsparse_dbg_prt(102, ("cycle=%d\n-------------------------------------\n\n", state_cycle));
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("cycle=%d\n-------------------------------------\n\n", state_cycle)); 0 0 0
710 }
711  
712 //---------------------------------------------------------------------------------------------------------------------------
713 // mpeg2 decoder (again) but this time converts the image to RGB
714 //
715 0 static void process_mpeg2_rgb(struct TS_reader *tsreader, struct TS_state *tsstate, uint8_t *pesdata, unsigned pesdata_len)
716 {
717 mpeg2_state_t state;
718 0 unsigned state_cycle = 0 ;
719 unsigned frame_flags ;
720  
721 // for debug
722 static int total_offset = 0;
723 const mpeg2_info_t * info;
724  
725 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
726  
727 0 ▶ tsparse_dbg_prt(202, ("\n\n--[ rgb video ]----------\n%d bytes\n", pesdata_len)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(202, ("\n\n--[ rgb video ]----------\n%d bytes\n", pesdata_len)) ; 0 0 0
728 0 ▶ if (tsreader->debug >= 202)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 202) 0 0 0
729 0 dump_buff(pesdata, pesdata_len, pesdata_len) ;
730  
731 // mainly looking for GOP start
732 0 frame_flags = mpeg2_frame_flags(tsreader, tsstate, pesdata, pesdata_len) ;
733 0 ▶ if (frame_flags & FRAME_FLAG_GOP)
Branch: 1 branch
BranchTrueFalseTotal
if (frame_flags & FRAME_FLAG_GOP) 0 0 0
734 {
735 0 tsreader->mpeg2.gop_pktnum = tsstate->pid_item->pesinfo.start_pkt ;
736 }
737  
738 // check this is a video stream
739 0 ▶ if ( (tsstate->pid_item->pesinfo.code & video_stream_mask) != video_stream)
Branch: 1 branch
BranchTrueFalseTotal
if ( (tsstate->pid_item->pesinfo.code & video_stream_mask) != video_stream) 0 0 0
740 {
741 0 return ;
742 }
743  
744 // for debug
745 0 info = mpeg2_info(tsreader->mpeg2.decoder);
746 0 total_offset += pesdata_len ;
747  
748 do
749 {
750 0 state = mpeg2_parse (tsreader->mpeg2.decoder);
751  
752 /*
753 * With verbose=100, dump_state format:
754 *
755 * %8x = file offset
756 * %c%c = seq code, gop code S=seq, G=gop
757 * %c%c%c = curr fbuff code, curr pic code, curr pic2 code (a-z or A-Z for new) 0..25
758 * %c%c%c = disp fbuff code, disp pic code, disp pic2 code
759 * %c = discard fbuf code
760 * %s = State name
761 *
762 *
763 * GOP = Group of 26 pictures
764 */
765 0 ▶ tsparse_dbg_prt(200, ("---[ rgb mpeg2 dump_state ]------------------------\nstate = %s [%d]\n", STATE_STRINGS[state], state));
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(200, ("---[ rgb mpeg2 dump_state ]------------------------\nstate = %s [%d]\n", STATE_STRINGS[state], state)); 0 0 0
766 0 ▶ if (tsreader->debug >= 202)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 202) 0 0 0
767 0 dump_state (stderr, state, info,
768 0 total_offset - mpeg2_getpos(tsreader->mpeg2.decoder), 100 /* verbosity */);
769 0 ▶ tsparse_dbg_prt(102, ("state = %s [%d]\n", STATE_STRINGS[state], state));
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("state = %s [%d]\n", STATE_STRINGS[state], state)); 0 0 0
770  
771 0 switch (state) {
772 0 case STATE_BUFFER:
773 // HAVE TO COPY DATA TO NEW BUFFER SO THAT libmpeg2 HAS A COPY TO WORK ON IN THE MEANTIME!
774 0 ▶ if (tsreader->mpeg2.video_buffer) free(tsreader->mpeg2.video_buffer) ;
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.video_buffer) free(tsreader->mpeg2.video_buffer) ; 0 0 0
775 0 tsreader->mpeg2.video_buffer = (uint8_t *)malloc(pesdata_len * sizeof(uint8_t)) ;
776 0 memcpy(tsreader->mpeg2.video_buffer, pesdata, pesdata_len) ;
777 0 mpeg2_buffer (tsreader->mpeg2.decoder, tsreader->mpeg2.video_buffer, tsreader->mpeg2.video_buffer + pesdata_len);
778 0 break;
779  
780 0 case STATE_SEQUENCE:
781 0 mpeg2_convert (tsreader->mpeg2.decoder, mpeg2convert_rgb24, NULL);
782 0 break;
783  
784 0 case STATE_SLICE:
785 case STATE_END:
786 case STATE_INVALID_END:
787 0 ▶ if (tsreader->mpeg2.info->display_fbuf)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.info->display_fbuf) 0 0 0
788 {
789 // call callback
790 0 ▶ if (tsreader->mpeg2_rgb_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2_rgb_hook) 0 0 0
791 {
792 struct TS_frame_info *frame_info ;
793  
794 0 unsigned frame_info_index = (unsigned)tsreader->mpeg2.info->current_picture->tag ;
795 0 frame_info = frame_info_entry(tsreader, frame_info_index) ;
796  
797 0 tsreader->mpeg2_rgb_hook(&tsstate->pidinfo,
798 frame_info,
799 tsreader->mpeg2.info,
800 tsreader->user_data) ;
801 }
802 0 ++tsreader->mpeg2.framenum ;
803 }
804 0 break;
805  
806 0 case STATE_INVALID:
807 0 break ;
808 0 case STATE_SEQUENCE_REPEATED:
809 0 break ;
810 0 case STATE_GOP:
811 0 break ;
812 0 case STATE_PICTURE:
813 0 break ;
814 0 case STATE_SLICE_1ST:
815 0 break ;
816 0 case STATE_PICTURE_2ND:
817 0 break ;
818 0 case STATE_SEQUENCE_MODIFIED:
819 0 break ;
820  
821 0 default:
822 0 break;
823 }
824 0 ▶ } while ( (state != STATE_BUFFER) && (state_cycle++ < MAX_STATE_CYCLE) ) ;
Branch: 2 branches
BranchTrueFalseTotal
} while ( (state != STATE_BUFFER) && (state_cycle++ < MAX_STATE_CYCLE) ) ; 0 0 0
} while ( (state != STATE_BUFFER) && (state_cycle++ < MAX_STATE_CYCLE) ) ; 0 0 0
825  
826 // Use the tags to store:
827 // tag = frame info index
828 // tag2 = packet count of pes containing GOP (once every 26 frames)
829 0 struct TS_frame_info *frame_info = frame_info_entry(tsreader, tsreader->mpeg2.frame_info_index) ;
830 0 frame_info->framenum = tsreader->mpeg2.framenum ;
831 0 frame_info->gop_pkt = tsreader->mpeg2.gop_pktnum ;
832 0 memcpy(&frame_info->pesinfo, &tsstate->pid_item->pesinfo, sizeof(tsstate->pid_item->pesinfo)) ;
833 0 memcpy(&frame_info->pidinfo, &tsstate->pid_item->pidinfo, sizeof(tsstate->pid_item->pidinfo)) ;
834  
835 0 mpeg2_tag_picture(tsreader->mpeg2.decoder, tsreader->mpeg2.frame_info_index, tsreader->mpeg2.gop_pktnum) ;
836 0 tsreader->mpeg2.frame_info_index++ ;
837  
838 0 ▶ tsparse_dbg_prt(102, ("cycle=%d\n-------------------------------------\n\n", state_cycle));
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("cycle=%d\n-------------------------------------\n\n", state_cycle)); 0 0 0
839 }
840  
841  
842  
843 //---------------------------------------------------------------------------------------------------------------------------
844 0 static void process_audio(struct TS_reader *tsreader, struct TS_state *tsstate, uint8_t *pesdata, unsigned pesdata_len)
845 {
846 int done_bytes, avail_bytes, bytes;
847 int samples, audio_bytes;
848 mpeg2_audio_t audio_info ;
849  
850 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
851  
852 // check this is an audio stream
853 0 ▶ if ( (tsstate->pid_item->pesinfo.code & audio_stream_mask) != audio_stream)
Branch: 1 branch
BranchTrueFalseTotal
if ( (tsstate->pid_item->pesinfo.code & audio_stream_mask) != audio_stream) 0 0 0
854 {
855 0 return ;
856 }
857  
858 0 ▶ if (&tsreader->mpeg2audio.write_ptr[pesdata_len] - tsreader->mpeg2audio.storage_buf > STORAGE_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (&tsreader->mpeg2audio.write_ptr[pesdata_len] - tsreader->mpeg2audio.storage_buf > STORAGE_SIZE) 0 0 0
859 {
860 0 printf("ERROR: Audio buffer full\n");
861 0 abort() ;
862 }
863 0 memcpy(tsreader->mpeg2audio.write_ptr, pesdata, pesdata_len);
864 0 tsreader->mpeg2audio.write_ptr = &tsreader->mpeg2audio.write_ptr[pesdata_len];
865 0 avail_bytes = tsreader->mpeg2audio.write_ptr - tsreader->mpeg2audio.read_ptr;
866  
867 // start with this PTS
868 0 audio_info.pts = tsstate->pid_item->pesinfo.dts ;
869  
870 0 done_bytes = 1;
871 0 ▶ while ( (avail_bytes > MIN_BUFFER) && (done_bytes > 0) )
Branch: 2 branches
BranchTrueFalseTotal
while ( (avail_bytes > MIN_BUFFER) && (done_bytes > 0) ) 0 0 0
while ( (avail_bytes > MIN_BUFFER) && (done_bytes > 0) ) 0 0 0
872 {
873 0 audio_bytes = 0;
874 0 ▶ tsparse_dbg_prt(102, ("avail_bytes=%d, done=%d\n", avail_bytes, done_bytes)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("avail_bytes=%d, done=%d\n", avail_bytes, done_bytes)) ; 0 0 0
875  
876 // Decode the frame in buffer
877 0 done_bytes = decode_frame(tsreader->mpeg2audio.audio_buf, &audio_bytes, tsreader->mpeg2audio.read_ptr, (int)(tsreader->mpeg2audio.write_ptr - tsreader->mpeg2audio.read_ptr));
878  
879 // convert the audio buffer size in bytes, into the total number of samples (shorts). Samples per chan = samples / num_chans
880 0 samples = audio_bytes / sizeof(short) ;
881  
882 0 tsreader->mpeg2audio.read_ptr = &tsreader->mpeg2audio.read_ptr[done_bytes];
883 0 avail_bytes -= done_bytes;
884  
885 // Pass the samples to the user
886 0 audio_info.sample_rate = get_samplerate() ;
887 0 audio_info.channels = get_channels() ;
888 0 audio_info.framesize = get_framesize() ;
889 0 audio_info.audio = tsreader->mpeg2audio.audio_buf ;
890 0 audio_info.samples = samples ;
891 0 audio_info.audio_framenum = tsreader->mpeg2audio.framenum ;
892  
893 0 audio_info.samples_per_frame = audio_info.framesize * audio_info.channels ;
894  
895 // Calculate the PTS delta between audio frames
896 0 audio_info.pts_delta = 90000 * audio_info.framesize / audio_info.sample_rate ;
897  
898 // call callback
899 0 ▶ if ( (audio_info.samples_per_frame > 0) && tsreader->audio_hook )
Branch: 2 branches
BranchTrueFalseTotal
if ( (audio_info.samples_per_frame > 0) && tsreader->audio_hook ) 0 0 0
if ( (audio_info.samples_per_frame > 0) && tsreader->audio_hook ) 0 0 0
900 {
901 0 tsreader->mpeg2audio.audio_samples += samples;
902 0 ▶ while (tsreader->mpeg2audio.audio_samples >= audio_info.samples_per_frame)
Branch: 1 branch
BranchTrueFalseTotal
while (tsreader->mpeg2audio.audio_samples >= audio_info.samples_per_frame) 0 0 0
903 {
904 // callback a frame at a time
905 0 audio_info.samples = audio_info.samples_per_frame ;
906 0 tsreader->audio_hook(&tsstate->pidinfo, &tsstate->pid_item->pesinfo,
907 &audio_info, tsreader->user_data) ;
908  
909 0 tsreader->mpeg2audio.audio_samples -= audio_info.samples_per_frame;
910 0 audio_info.audio -= audio_info.samples_per_frame ;
911  
912 // This is the audio frame count
913 0 audio_info.audio_framenum++ ;
914  
915 // step to expected next DTS
916 0 audio_info.pts += (int64_t)audio_info.pts_delta ;
917 }
918 0 tsreader->mpeg2audio.framenum = audio_info.audio_framenum ;
919  
920 }
921 }
922  
923 // Shuffle any remaining data down to the bottom of the buffer
924 0 bytes = 0 ;
925 0 ▶ while (tsreader->mpeg2audio.read_ptr < tsreader->mpeg2audio.write_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
while (tsreader->mpeg2audio.read_ptr < tsreader->mpeg2audio.write_ptr) { 0 0 0
926 0 tsreader->mpeg2audio.storage_buf[bytes++] = *tsreader->mpeg2audio.read_ptr++;
927 }
928 0 tsreader->mpeg2audio.write_ptr = &tsreader->mpeg2audio.storage_buf[bytes];
929 0 tsreader->mpeg2audio.read_ptr = tsreader->mpeg2audio.storage_buf;
930  
931 }
932  
933  
934 ///* ----------------------------------------------------------------------- */
935 //static int process_psi(struct TS_reader *tsreader, struct TS_state *tsstate,
936 // uint8_t *payload, unsigned payload_len)
937 //{
938 // tsparse_dbg_prt(102, ("process_psi(pid %d) payload len %d\n",
939 // tsstate->pidinfo.pid, payload_len)) ;
940 //
941 // // Call table processor
942 //
943 // return 0 ;
944 //}
945  
946 /* ----------------------------------------------------------------------- */
947 0 static int process_pes(struct TS_reader *tsreader, struct TS_state *tsstate,
948 uint8_t *payload, unsigned payload_len)
949 {
950 unsigned code ;
951 unsigned packet_length ;
952 unsigned byte ;
953 int64_t pts, dts ;
954 int i ;
955 0 unsigned set_ts = 0 ;
956 unsigned pts_dts ;
957  
958 #ifdef DEBUG_PTS
959 unsigned dbg_bytes[32] ;
960 unsigned dbg_num ;
961 #endif
962  
963 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
964  
965 0 ▶ tsparse_dbg_prt(102, ("process_pes(pid %d) payload len %d\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("process_pes(pid %d) payload len %d\n", 0 0 0
966 tsstate->pidinfo.pid, payload_len)) ;
967  
968 0 ▶ if (payload_len < 7)
Branch: 1 branch
BranchTrueFalseTotal
if (payload_len < 7) 0 0 0
969 0 return 0 ;
970  
971 // packet_start_code_prefix 24 bslbf
972 // stream_id 8 uimsbf
973 // packet_length 16 uimsbf
974 //
975 0 code = 0x100 | payload[3] ;
976 0 packet_length = ((payload[4])<<8) | payload[5] ;
977  
978 0 tsstate->pid_item->pesinfo.code = code ;
979  
980 0 ▶ tsparse_dbg_prt(102, ("PES code 0x%03x PES Len %d Data:\n", code, packet_length)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("PES code 0x%03x PES Len %d Data:\n", code, packet_length)) ; 0 0 0
981 0 ▶ if (tsreader->debug >= 103)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 103) 0 0 0
982 0 dump_buff(payload, payload_len, 32) ;
983  
984 0 byte = 6 ;
985  
986 // ISO13818-1:
987 //
988 // if (stream_id != program_stream_map
989 // && stream_id != padding_stream
990 // && stream_id != private_stream_2
991 // && stream_id != ECM
992 // && stream_id != EMM
993 // && stream_id != program_stream_directory
994 // && stream_id != DSMCC_stream
995 // && stream_id != ITU-T Rec. H.222.1 type E stream) {
996 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 0 0
997 code != program_stream_map
998 0 ▶ && code != padding_stream
Branch: 1 branch
BranchTrueFalseTotal
&& code != padding_stream 0 0 0
999 0 ▶ && code != private_stream_2
Branch: 1 branch
BranchTrueFalseTotal
&& code != private_stream_2 0 0 0
1000 0 ▶ && code != ECM_stream
Branch: 1 branch
BranchTrueFalseTotal
&& code != ECM_stream 0 0 0
1001 0 ▶ && code != EMM_stream
Branch: 1 branch
BranchTrueFalseTotal
&& code != EMM_stream 0 0 0
1002 0 ▶ && code != program_stream_directory
Branch: 1 branch
BranchTrueFalseTotal
&& code != program_stream_directory 0 0 0
1003 0 ▶ && code != DSMCC_stream
Branch: 1 branch
BranchTrueFalseTotal
&& code != DSMCC_stream 0 0 0
1004 0 ▶ && code != H2221_E_stream
Branch: 1 branch
BranchTrueFalseTotal
&& code != H2221_E_stream 0 0 0
1005 )
1006 {
1007 unsigned pes_header_len ;
1008  
1009 // '10' 2 bslbf
1010 // PES_scrambling_control 2 bslbf
1011 // PES_priority 1 bslbf
1012 // data_alignment_indicator 1 bslbf
1013 // copyright 1 bslbf
1014 // original_or_copy 1 bslbf
1015 0 ++byte ;
1016  
1017 // PTS_DTS_flags 2 bslbf
1018 // ESCR_flag 1 bslbf
1019 // ES_rate_flag 1 bslbf
1020 // DSM_trick_mode_flag 1 bslbf
1021 // additional_copy_info_flag 1 bslbf
1022 // PES_CRC_flag 1 bslbf
1023 // PES_extension_flag 1 bslbf
1024 0 pts_dts = (payload[byte] >> 6) & 0x3 ;
1025 0 ++byte ;
1026  
1027 0 ▶ tsparse_dbg_prt(102, (" + decoding pes data (pts_dts=%d [0x%02x])...\n", pts_dts, payload[byte-1])) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, (" + decoding pes data (pts_dts=%d [0x%02x])...\n", pts_dts, payload[byte-1])) ; 0 0 0
1028  
1029 // PES_header_data_length 8 uimsbf
1030 0 pes_header_len = payload[byte] ;
1031 0 ++byte ;
1032  
1033 // point at just the PES data part
1034 0 tsstate->pid_item->pesinfo.pesdata_p = &payload[byte+pes_header_len] ;
1035 0 tsstate->pid_item->pesinfo.pesdata_len = payload_len - (byte+pes_header_len) ;
1036  
1037 0 ▶ tsparse_dbg_prt(102, (" + pes header len=%d...\n", pes_header_len)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, (" + pes header len=%d...\n", pes_header_len)) ; 0 0 0
1038  
1039 // if (PTS_DTS_flags = = '10') {
1040 // '0010' 4 bslbf
1041 // PTS [32..30] 3 bslbf
1042 // marker_bit 1 bslbf
1043 // PTS [29..15] 15 bslbf
1044 // marker_bit 1 bslbf
1045 // PTS [14..0] 15 bslbf
1046 // marker_bit 1 bslbf
1047 // }
1048 0 ▶ if (pts_dts == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (pts_dts == 2) 0 0 0
1049 {
1050 #ifdef DEBUG_PTS
1051 dbg_bytes[0] = payload[byte] ;
1052 dbg_bytes[1] = payload[byte+1] ;
1053 dbg_bytes[2] = payload[byte+2] ;
1054 dbg_bytes[3] = payload[byte+3] ;
1055 dbg_bytes[4] = payload[byte+4] ;
1056 dbg_num=5 ;
1057 #endif
1058  
1059 0 ▶ if ( (payload[byte] & 0xf0) != 0x20)
Branch: 1 branch
BranchTrueFalseTotal
if ( (payload[byte] & 0xf0) != 0x20) 0 0 0
1060 {
1061 // handle error
1062 0 tsstate->pid_item->pesinfo.pes_error++ ;
1063 0 tsstate->pidinfo.pid_error++ ;
1064 0 ▶ if (tsreader->error_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->error_hook) 0 0 0
1065 {
1066 0 SET_DVB_ERROR(ERR_PESHEAD) ;
1067 0 tsreader->error_hook(dvb_error_code, &tsstate->pidinfo, tsreader->user_data) ;
1068 }
1069 }
1070 0 pts = (int64_t)((payload[byte] >> 1) & 0x07) << 30;
1071 0 pts |= (((payload[byte+1]<<8) | payload[byte+2]) >> 1) << 15;
1072 0 pts |= (((payload[byte+3]<<8) | payload[byte+4]) >> 1);
1073  
1074 0 ▶ tsparse_dbg_prt(100, ("PTS definition:\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("PTS definition:\n")) ; 0 0 0
1075 0 ▶ if (tsreader->debug >= 100)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 100) 0 0 0
1076 0 dump_buff(&payload[byte], payload_len, 5) ;
1077  
1078 0 byte += 5 ;
1079  
1080 0 dts = pts ;
1081 0 ++set_ts ;
1082 }
1083  
1084 // if (PTS_DTS_flags = = '11') {
1085 // '0011' 4 bslbf
1086 // PTS [32..30] 3 bslbf
1087 // marker_bit 1 bslbf
1088  
1089 // PTS [29..15] 15 bslbf
1090 // marker_bit 1 bslbf
1091  
1092 // PTS [14..0] 15 bslbf
1093 // marker_bit 1 bslbf
1094  
1095 // '0001' 4 bslbf
1096 // DTS [32..30] 3 bslbf
1097 // marker_bit 1 bslbf
1098  
1099 // DTS [29..15] 15 bslbf
1100 // marker_bit 1 bslbf
1101  
1102 // DTS [14..0] 15 bslbf
1103 // marker_bit 1 bslbf
1104 // }
1105 0 ▶ if (pts_dts == 3)
Branch: 1 branch
BranchTrueFalseTotal
if (pts_dts == 3) 0 0 0
1106 {
1107 #ifdef DEBUG_PTS
1108 dbg_bytes[0] = payload[byte] ;
1109 dbg_bytes[1] = payload[byte+1] ;
1110 dbg_bytes[2] = payload[byte+2] ;
1111 dbg_bytes[3] = payload[byte+3] ;
1112 dbg_bytes[4] = payload[byte+4] ;
1113 dbg_bytes[5] = payload[byte+5] ;
1114 dbg_bytes[6] = payload[byte+6] ;
1115 dbg_bytes[7] = payload[byte+7] ;
1116 dbg_bytes[8] = payload[byte+8] ;
1117 dbg_bytes[9] = payload[byte+9] ;
1118 dbg_num=10 ;
1119 #endif
1120 0 ▶ if ( (payload[byte] & 0xf0) != 0x30)
Branch: 1 branch
BranchTrueFalseTotal
if ( (payload[byte] & 0xf0) != 0x30) 0 0 0
1121 {
1122 // handle error
1123 0 tsstate->pid_item->pesinfo.pes_error++ ;
1124 0 tsstate->pidinfo.pid_error++ ;
1125 0 ▶ if (tsreader->error_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->error_hook) 0 0 0
1126 {
1127 0 SET_DVB_ERROR(ERR_PESHEAD) ;
1128 0 tsreader->error_hook(dvb_error_code, &tsstate->pidinfo, tsreader->user_data) ;
1129 }
1130 }
1131 0 pts = (int64_t)((payload[byte] >> 1) & 0x07) << 30;
1132 0 pts |= (((payload[byte+1]<<8) | payload[byte+2]) >> 1) << 15;
1133 0 pts |= (((payload[byte+3]<<8) | payload[byte+4]) >> 1);
1134  
1135 0 ▶ tsparse_dbg_prt(100, ("PTS definition:\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("PTS definition:\n")) ; 0 0 0
1136 0 ▶ if (tsreader->debug >= 100)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 100) 0 0 0
1137 0 dump_buff(&payload[byte], payload_len, 5) ;
1138  
1139 0 byte += 5 ;
1140  
1141 0 dts = (int64_t)((payload[byte] >> 1) & 0x07) << 30;
1142 0 dts |= (((payload[byte+1]<<8) | payload[byte+2]) >> 1) << 15;
1143 0 dts |= (((payload[byte+3]<<8) | payload[byte+4]) >> 1);
1144  
1145 0 ▶ tsparse_dbg_prt(100, ("DTS definition:\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("DTS definition:\n")) ; 0 0 0
1146 0 ▶ if (tsreader->debug >= 100)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 100) 0 0 0
1147 0 dump_buff(&payload[byte], payload_len, 5) ;
1148  
1149 0 byte += 5 ;
1150  
1151 0 ++set_ts ;
1152 }
1153  
1154  
1155 // if (ESCR_flag = = '1') {
1156 // reserved 2 bslbf
1157 // ESCR_base[32..30] 3 bslbf
1158 // marker_bit 1 bslbf
1159 // ESCR_base[29..15] 15 bslbf
1160 // marker_bit 1 bslbf
1161 // ESCR_base[14..0] 15 bslbf
1162 // marker_bit 1 bslbf
1163 // ESCR_extension 9 uimsbf
1164 // marker_bit 1 bslbf
1165 // }
1166 // if (ES_rate_flag = = '1') {
1167 // marker_bit 1 bslbf
1168 // ES_rate 22 uimsbf
1169 // marker_bit 1 bslbf
1170 // }
1171 // if (DSM_trick_mode_flag = = '1') {
1172 // trick_mode_control 3 uimsbf
1173 // if ( trick_mode_control = = fast_forward ) {
1174 // field_id 2 bslbf
1175 // intra_slice_refresh 1 bslbf
1176 // frequency_truncation 2 bslbf
1177 // }
1178 // else if ( trick_mode_control = = slow_motion ) {
1179 // rep_cntrl 5 uimsbf
1180 // }
1181 // else if ( trick_mode_control = = freeze_frame ) {
1182 // field_id 2 uimsbf
1183 // reserved 3 bslbf
1184 // }
1185 // else if ( trick_mode_control = = fast_reverse ) {
1186 // field_id 2 bslbf
1187 // intra_slice_refresh 1 bslbf
1188 // frequency_truncation 2 bslbf
1189 // else if ( trick_mode_control = = slow_reverse ) {
1190 // rep_cntrl 5 uimsbf
1191 // }
1192 // else
1193 // reserved 5 bslbf
1194 // }
1195 // if ( additional_copy_info_flag = = '1') {
1196 // marker_bit 1 bslbf
1197 // additional_copy_info 7 bslbf
1198 // }
1199 // if ( PES_CRC_flag = = '1') {
1200 // previous_PES_packet_CRC 16 bslbf
1201 // }
1202 // if ( PES_extension_flag = = '1') {
1203 // PES_private_data_flag 1 bslbf
1204 // pack_header_field_flag 1 bslbf
1205 // program_packet_sequence_counter_flag 1 bslbf
1206 // P-STD_buffer_flag 1 bslbf
1207 // reserved 3 bslbf
1208 // PES_extension_flag_2 1 bslbf
1209 // if ( PES_private_data_flag = = '1') {
1210 // PES_private_data 128 bslbf
1211 // }
1212 // if (pack_header_field_flag = = '1') {
1213 // pack_field_length 8 uimsbf
1214 // pack_header()
1215 // }
1216 // if (program_packet_sequence_counter_flag = = '1') {
1217 // marker_bit 1 bslbf
1218 // program_packet_sequence_counter 7 uimsbf
1219 // marker_bit 1 bslbf
1220 // MPEG1_MPEG2_identifier 1 bslbf
1221 // original_stuff_length 6 uimsbf
1222 // }
1223 // if ( P-STD_buffer_flag = = '1') {
1224 // '01' 2 bslbf
1225 // P-STD_buffer_scale 1 bslbf
1226 // P-STD_buffer_size 13 uimsbf
1227 // }
1228 // if ( PES_extension_flag_2 = = '1') {
1229 // marker_bit 1 bslbf
1230 // PES_extension_field_length 7 uimsbf
1231 // for (i = 0; i < PES_extension_field_length; i++) {
1232 // reserved 8 bslbf
1233 // }
1234 // }
1235 // }
1236 // for (i = 0; i < N1; i++) {
1237 // stuffing_byte 8 bslbf
1238 // }
1239 // for (i = 0; i < N2; i++) {
1240 // PES_packet_data_byte 8 bslbf
1241 // }
1242 // }
1243 }
1244  
1245  
1246 // setting the pts/dts?
1247 0 ▶ if (set_ts)
Branch: 1 branch
BranchTrueFalseTotal
if (set_ts) 0 0 0
1248 {
1249 // PTS
1250 0 ▶ if (tsstate->pid_item->pesinfo.start_pts == UNSET_TS)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pid_item->pesinfo.start_pts == UNSET_TS) 0 0 0
1251 {
1252 0 tsstate->pid_item->pesinfo.start_pts = pts ;
1253 }
1254 else
1255 {
1256 // check for wrap
1257 0 ▶ if (pts+MAX_TS_DIFF < tsstate->pid_item->pesinfo.start_pts)
Branch: 1 branch
BranchTrueFalseTotal
if (pts+MAX_TS_DIFF < tsstate->pid_item->pesinfo.start_pts) 0 0 0
1258 {
1259 // wrapped, so adjust
1260 0 pts += TS_WRAP ;
1261 }
1262 }
1263  
1264 0 ▶ if (tsstate->pid_item->pesinfo.end_pts == UNSET_TS)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pid_item->pesinfo.end_pts == UNSET_TS) 0 0 0
1265 {
1266 0 tsstate->pid_item->pesinfo.end_pts = pts ;
1267 }
1268 else
1269 {
1270 0 ▶ if (tsstate->pid_item->pesinfo.end_pts < pts)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pid_item->pesinfo.end_pts < pts) 0 0 0
1271 0 tsstate->pid_item->pesinfo.end_pts = pts ;
1272 }
1273 0 tsstate->pid_item->pesinfo.pts = pts ;
1274  
1275 // DTS
1276 0 ▶ if (tsstate->pid_item->pesinfo.start_dts == UNSET_TS)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pid_item->pesinfo.start_dts == UNSET_TS) 0 0 0
1277 {
1278 0 tsstate->pid_item->pesinfo.start_dts = pts ;
1279 }
1280 else
1281 {
1282 // check for wrap
1283 0 ▶ if (dts+MAX_TS_DIFF < tsstate->pid_item->pesinfo.start_dts)
Branch: 1 branch
BranchTrueFalseTotal
if (dts+MAX_TS_DIFF < tsstate->pid_item->pesinfo.start_dts) 0 0 0
1284 {
1285 // wrapped, so adjust
1286 0 dts += TS_WRAP ;
1287 }
1288 }
1289  
1290 0 ▶ if (tsstate->pid_item->pesinfo.end_dts == UNSET_TS)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pid_item->pesinfo.end_dts == UNSET_TS) 0 0 0
1291 {
1292 0 tsstate->pid_item->pesinfo.end_dts = pts ;
1293 }
1294 else
1295 {
1296 0 ▶ if (tsstate->pid_item->pesinfo.end_dts < dts)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pid_item->pesinfo.end_dts < dts) 0 0 0
1297 0 tsstate->pid_item->pesinfo.end_dts = dts ;
1298 }
1299 0 tsstate->pid_item->pesinfo.dts = dts ;
1300  
1301 0 ▶ tsparse_dbg_prt(102, ("TIMESTAMP: pid %d [0x%03x] rel ts=%d\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("TIMESTAMP: pid %d [0x%03x] rel ts=%d\n", 0 0 0
1302 tsstate->pidinfo.pid, code,
1303 (int)(dts - tsstate->pid_item->pesinfo.start_dts)
1304 ));
1305  
1306 #ifdef DEBUG_PTS
1307 printf("DTS: pid %d [0x%03x] pts=%"PRId64" 0x%0"PRIx64" (dts=%"PRId64") : pts_dts=%d [",
1308 tsstate->pidinfo.pid, code, pts, pts, dts, pts_dts);
1309 for (i=0; i<dbg_num; ++i)
1310 {
1311 printf("0x%02x ", dbg_bytes[i]) ;
1312 }
1313 printf("]\n") ;
1314 #endif
1315 }
1316  
1317 0 return 0 ;
1318 }
1319  
1320  
1321  
1322  
1323 /* ----------------------------------------------------------------------- */
1324 0 static int handle_payload(struct TS_reader *tsreader, struct TS_state *tsstate,
1325 uint8_t *payload, unsigned payload_len)
1326 {
1327 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1328 0 ▶ if (tsstate->pidinfo.pid == NULL_PID)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pidinfo.pid == NULL_PID) 0 0 0
1329 0 return 0 ;
1330  
1331 0 ▶ tsparse_dbg_prt(102, ("handle_payload(pid %d) : pkt %u : payload len %d pes_start=%d\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("handle_payload(pid %d) : pkt %u : payload len %d pes_start=%d\n", 0 0 0
1332 tsstate->pidinfo.pid, tsstate->pidinfo.pktnum,
1333 payload_len, tsstate->pidinfo.pes_start?1:0)) ;
1334  
1335 // process buffered data
1336 0 ▶ if (tsstate->pidinfo.pes_start)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pidinfo.pes_start) 0 0 0
1337 {
1338 0 ▶ if (tsstate->pid_item->pes_buff->data_len)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pid_item->pes_buff->data_len) 0 0 0
1339 {
1340 0 uint8_t *buff = tsstate->pid_item->pes_buff->buff ;
1341 0 unsigned buff_len = tsstate->pid_item->pes_buff->data_len ;
1342  
1343 0 ▶ tsparse_dbg_prt(102, ("handle_payload(pid %d) : call process_*() with existing buffer : buffered data len %d:\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("handle_payload(pid %d) : call process_*() with existing buffer : buffered data len %d:\n", 0 0 0
1344 tsstate->pidinfo.pid, tsstate->pid_item->pes_buff->data_len)) ;
1345  
1346 0 ▶ if ((buff[0]==0) && (buff[1]==0) && (buff[2]==1))
Branch: 3 branches
BranchTrueFalseTotal
if ((buff[0]==0) && (buff[1]==0) && (buff[2]==1)) 0 0 0
if ((buff[0]==0) && (buff[1]==0) && (buff[2]==1)) 0 0 0
if ((buff[0]==0) && (buff[1]==0) && (buff[2]==1)) 0 0 0
1347 {
1348 // do something with the PES data
1349 0 tsstate->pid_item->pesinfo.pes_psi = T_PES ;
1350 0 process_pes(tsreader, tsstate, buff, buff_len) ;
1351 }
1352  
1353 //# PSI
1354 else
1355 {
1356 0 ▶ if (tsreader->debug >= 103)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 103) 0 0 0
1357 0 ▶ dump_buff(buff, buff_len, (tsreader->debug >= 104 ? buff_len : 31) ) ;
Branch: 1 branch
BranchTrueFalseTotal
dump_buff(buff, buff_len, (tsreader->debug >= 104 ? buff_len : 31) ) ; 0 0 0
1358  
1359 // do something with the PSI data
1360 0 tsstate->pid_item->pesinfo.pes_psi = T_PSI ;
1361 0 parse_si(tsreader, tsstate, buff, buff_len) ;
1362 }
1363  
1364 // send to hook
1365 0 ▶ if (tsreader->pes_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->pes_hook) 0 0 0
1366 {
1367 // Complete PES packet - Header + Data
1368 0 tsreader->pes_hook(&tsstate->pidinfo, &tsstate->pid_item->pesinfo,
1369 buff, buff_len,
1370 tsreader->user_data) ;
1371 }
1372  
1373 // send to hook
1374 0 ▶ if (tsreader->pes_data_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->pes_data_hook) 0 0 0
1375 {
1376 // Just the PES data
1377 0 tsreader->pes_data_hook(&tsstate->pidinfo, &tsstate->pid_item->pesinfo,
1378 0 tsstate->pid_item->pesinfo.pesdata_p, tsstate->pid_item->pesinfo.pesdata_len,
1379 tsreader->user_data) ;
1380 }
1381  
1382 // send to hook
1383 0 ▶ if (tsreader->mpeg2.decoder)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.decoder) 0 0 0
1384 {
1385 // Just the PES data
1386 0 process_mpeg2(tsreader, tsstate, tsstate->pid_item->pesinfo.pesdata_p, tsstate->pid_item->pesinfo.pesdata_len) ;
1387 }
1388  
1389 // send to hook
1390 0 ▶ if (tsreader->audio_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->audio_hook) 0 0 0
1391 {
1392 // Just the PES data
1393 0 process_audio(tsreader, tsstate, tsstate->pid_item->pesinfo.pesdata_p, tsstate->pid_item->pesinfo.pesdata_len) ;
1394 }
1395 }
1396  
1397 // reset state
1398 0 pes_start(tsstate->pid_item) ;
1399 }
1400  
1401 // process current data
1402 0 ▶ if (tsstate->pidinfo.pes_start)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pidinfo.pes_start) 0 0 0
1403 {
1404 0 tsstate->pid_item->pesinfo.start_pkt = tsreader->tsstate->pidinfo.pktnum ;
1405 0 tsstate->pid_item->pesinfo.end_pkt = tsreader->tsstate->pidinfo.pktnum ;
1406  
1407 0 ▶ tsparse_dbg_prt(102, ("handle_payload(pid %d) : calling process_* with new data : payload len = %d\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("handle_payload(pid %d) : calling process_* with new data : payload len = %d\n", 0 0 0
1408 tsstate->pidinfo.pid, payload_len)) ;
1409  
1410 // # Name Size Description
1411 // #
1412 // # Packet start code prefix 3 bytes 0x000001
1413 // # Stream id 1 byte Examples: Audio streams (0xC0-0xDF), Video streams (0xE0-0xEF) [2] [3] [4] [5]
1414 // # Note: The above 4 bytes is called the 32 bit start code.
1415 // # PES Packet length 2 bytes Can be zero.If the PES packet length is set to zero, the PES packet can be of any length. A value of zero for the PES packet length can be used only when the PES packet payload is a video elementary stream.[6]
1416 // # Optional PES header variable length
1417 // # Stuffing bytes variable length
1418 // # Data See elementary stream. In the case of private streams the first byte of the payload is the sub-stream number.
1419 // #
1420 0 ▶ if ((payload[0]==0) && (payload[1]==0) && (payload[2]==1))
Branch: 3 branches
BranchTrueFalseTotal
if ((payload[0]==0) && (payload[1]==0) && (payload[2]==1)) 0 0 0
if ((payload[0]==0) && (payload[1]==0) && (payload[2]==1)) 0 0 0
if ((payload[0]==0) && (payload[1]==0) && (payload[2]==1)) 0 0 0
1421 {
1422 // do something with the PES data
1423 0 process_pes(tsreader, tsstate, payload, payload_len) ;
1424 }
1425  
1426 //# PSI
1427 else
1428 {
1429 // do something with the PSI data
1430 //SDP-just buffer until next start?// process_psi(tsreader, tsstate, payload, payload_len) ;
1431 }
1432 }
1433  
1434 // buffer if required
1435 0 ▶ if (tsstate->pid_item->pes_state != PES_SKIP)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pid_item->pes_state != PES_SKIP) 0 0 0
1436 {
1437 0 tsstate->pid_item->pesinfo.end_pkt = tsreader->tsstate->pidinfo.pktnum ;
1438  
1439 0 buffer_data(&tsstate->pid_item->pes_buff, payload, payload_len) ;
1440  
1441 0 ▶ tsparse_dbg_prt(102, ("<buffered> handle_payload(pid %d) : buffered - length now = %d\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("<buffered> handle_payload(pid %d) : buffered - length now = %d\n", 0 0 0
1442 tsstate->pidinfo.pid, tsstate->pid_item->pes_buff->data_len)) ;
1443 // if (tsreader->debug >= 103)
1444 // dump_buff(tsstate->pid_item->pes_buff->buff, tsstate->pid_item->pes_buff->data_len, (tsreader->debug >= 104 ? tsstate->pid_item->pes_buff->data_len : 31) ) ;
1445 }
1446  
1447 0 return 0 ;
1448 }
1449  
1450 /* ----------------------------------------------------------------------- */
1451 0 static int parse_ts_packet(struct TS_reader *tsreader, struct TS_state *tsstate,
1452 uint8_t *packet, unsigned packet_len)
1453 {
1454 unsigned pid_ok, pes_complete ;
1455  
1456 uint8_t *payload ;
1457 unsigned start ;
1458 int payload_len ;
1459  
1460 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1461 0 tsstate->pid_item = NULL ;
1462  
1463 /*
1464 # ISO 13818-1
1465 #
1466 # sync_byte 8 bslbf
1467 #
1468 # transport_error_indicator 1 bslbf
1469 # payload_unit_start_indicator 1 bslbf
1470 # transport_priority 1 bslbf
1471 # PID 13 uimsbf
1472 #
1473 # transport_scrambling_control 2 bslbf
1474 # adaptation_field_control 2 bslbf
1475 # continuity_counter 4 uimsbf
1476 #
1477 # if(adaptation_field_control = = '10' || adaptation_field_control = = '11'){
1478 # adaptation_field()
1479 # }
1480 # if(adaptation_field_control = = '01' || adaptation_field_control = = '11') {
1481 # for (i = 0; i < N; i++){
1482 # data_byte 8 bslbf
1483 # }
1484 # }
1485 */
1486 0 tsstate->pidinfo.pid = (((packet[1] & 0x1f) << 8) | (packet[2] & 0xff)) & MAX_PID ;
1487 0 tsstate->pidinfo.err_flag = packet[1] & 0x80;
1488 0 tsstate->pidinfo.pes_start = packet[1] & 0x40;
1489  
1490 /* skip adaptation field */
1491 0 tsstate->pidinfo.afc = (packet[3] >> 4) & 3;
1492 0 start=4 ;
1493 0 payload = &packet[start] ;
1494  
1495 0 ▶ if (tsstate->pidinfo.afc == 0) /* reserved value */
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pidinfo.afc == 0) /* reserved value */ 0 0 0
1496 0 return 0;
1497 0 ▶ if (tsstate->pidinfo.afc == 2) /* adaptation field only */
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pidinfo.afc == 2) /* adaptation field only */ 0 0 0
1498 0 return 0;
1499 0 ▶ if (tsstate->pidinfo.afc == 3)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pidinfo.afc == 3) 0 0 0
1500 {
1501 /* skip adaptation_field */
1502 0 start += payload[0] + 1 ;
1503 }
1504 /* if past the end of packet, ignore */
1505 0 ▶ if (start >= packet_len)
Branch: 1 branch
BranchTrueFalseTotal
if (start >= packet_len) 0 0 0
1506 0 return 0;
1507  
1508 0 payload = &packet[start] ;
1509 0 payload_len = packet_len - start ;
1510  
1511 0 ▶if (tsreader->debug >= 100)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 100) 0 0 0
1512 {
1513 0 printf ("PID %d (0x%03x) - start=%d, payload len=%d\n", tsstate->pidinfo.pid, tsstate->pidinfo.pid, start, payload_len) ;
1514 0 dump_buff(packet, packet_len, 31) ;
1515 }
1516  
1517  
1518 // cumulative error
1519 0 tsstate->pidinfo.pid_error = 0 ;
1520  
1521 // look at pid?
1522 0 pid_ok=1;
1523 0 ▶ if (tsreader->pid_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->pid_hook) 0 0 0
1524 {
1525 //# see if we want to process this pid
1526 0 pid_ok = tsreader->pid_hook(tsstate->pidinfo.pid, tsreader->user_data) ;
1527 }
1528  
1529  
1530 0 ▶ if (pid_ok)
Branch: 1 branch
BranchTrueFalseTotal
if (pid_ok) 0 0 0
1531 {
1532 // Update pid
1533 0 tsstate->pid_item = piditem_get(&tsstate->pid_list, &tsstate->pidinfo) ;
1534  
1535 //## Do checks for this packet
1536  
1537 //# sync check
1538 0 ▶ if (packet[0] != SYNC_BYTE)
Branch: 1 branch
BranchTrueFalseTotal
if (packet[0] != SYNC_BYTE) 0 0 0
1539 {
1540 0 ++tsstate->pidinfo.pid_error ;
1541 0 ▶ if (tsreader->error_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->error_hook) 0 0 0
1542 {
1543 0 SET_DVB_ERROR(ERR_BADSYNC) ;
1544 0 tsreader->error_hook(dvb_error_code, &tsstate->pidinfo, tsreader->user_data) ;
1545 }
1546 }
1547  
1548 // # error check
1549 0 ▶ if (tsstate->pidinfo.err_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pidinfo.err_flag) 0 0 0
1550 {
1551 0 ++tsstate->pidinfo.pid_error ;
1552 0 ▶ if (tsreader->error_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->error_hook) 0 0 0
1553 {
1554 0 SET_DVB_ERROR(ERR_TSERR) ;
1555 0 tsreader->error_hook(dvb_error_code, &tsstate->pidinfo, tsreader->user_data) ;
1556 }
1557 }
1558  
1559 // /* continuity check (currently not used) */
1560 // cc = (packet[3] & 0xf);
1561 // cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
1562 // tss->last_cc = cc;
1563  
1564 0 tsstate->pid_item->pidinfo.pid_error = tsstate->pidinfo.pid_error ;
1565 0 tsstate->pid_item->pesinfo.ts_error += tsstate->pidinfo.pid_error ;
1566 0 ▶ if (tsstate->pidinfo.err_flag) ++tsstate->pid_item->pesinfo.ts_error ;
Branch: 1 branch
BranchTrueFalseTotal
if (tsstate->pidinfo.err_flag) ++tsstate->pid_item->pesinfo.ts_error ; 0 0 0
1567  
1568 0 ▶ if (tsreader->payload_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->payload_hook) 0 0 0
1569 {
1570 0 tsreader->payload_hook(&tsstate->pidinfo, payload, payload_len, tsreader->user_data) ;
1571 }
1572  
1573 //# data may be a complete PES/PSI
1574 0 handle_payload(tsreader, tsstate, payload, payload_len) ;
1575  
1576 //## do something with raw packet
1577 0 ▶ if (tsreader->ts_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->ts_hook) 0 0 0
1578 {
1579 0 tsreader->ts_hook(&tsstate->pidinfo, packet, packet_len, tsreader->user_data) ;
1580 }
1581 }
1582  
1583 0 return 0 ;
1584 }
1585  
1586 /*=============================================================================================*/
1587 // PUBLIC
1588 /*=============================================================================================*/
1589  
1590 /* ----------------------------------------------------------------------- */
1591 // Convert a TS packet into a NULL packet - overwrites the data
1592 0 void ts_null_packet(uint8_t *packet, unsigned packet_len)
1593 {
1594  
1595 /*
1596 * For null packets the payload_unit_start_indicator shall be set to '0'.
1597 * PID value 0x1FFF is reserved for null packets
1598 * In the case of a null packet the value of the adaptation_field_control shall be set to '01'.
1599 * In the case of a null packet the value of the continuity_counter is undefined.
1600 * In the case of null packets with PID value 0x1FFF, data_bytes may be assigned any value.
1601 *
1602 *
1603 # ISO 13818-1
1604 #
1605 # sync_byte 8 bslbf
1606 #
1607 # transport_error_indicator 1 bslbf
1608 # payload_unit_start_indicator 1 bslbf
1609 # transport_priority 1 bslbf
1610 # PID 13 uimsbf
1611 #
1612 # transport_scrambling_control 2 bslbf
1613 # adaptation_field_control 2 bslbf
1614 # continuity_counter 4 uimsbf
1615 #
1616 # if(adaptation_field_control = = '10' || adaptation_field_control = = '11'){
1617 # adaptation_field()
1618 # }
1619 # if(adaptation_field_control = = '01' || adaptation_field_control = = '11') {
1620 # for (i = 0; i < N; i++){
1621 # data_byte 8 bslbf
1622 # }
1623 # }
1624 */
1625 // sync
1626 0 packet[0] = SYNC_BYTE ;
1627  
1628 // pid = 0x1fff
1629 // transport_error_indicator = 0
1630 // payload_unit_start_indicator = 0
1631 // transport_priority = 0
1632 0 packet[1] = (NULL_PID>>8) & 0x1f ;
1633 0 packet[2] = NULL_PID & 0xff ;
1634  
1635 /* adaptation field = 01 */
1636 0 packet[3] = (packet[3] & 0xcf) | 0x10 ;
1637  
1638 0 }
1639  
1640  
1641  
1642 /*=============================================================================================*/
1643 // SI table decoding
1644  
1645 /* ----------------------------------------------------------------------- */
1646 0 int tsreader_register_section(struct TS_reader *tsreader,
1647 unsigned table_id, unsigned mask,
1648 Section_handler handler, struct Section_decode_flags flags)
1649 {
1650 0 int updated = 0 ;
1651 enum TS_section_ids masked_val ;
1652 enum TS_section_ids id ;
1653  
1654 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1655  
1656 // add/remove handler entries from the table
1657 0 id = table_id & SECTION_MAX ;
1658  
1659 0 id &= mask ;
1660 0 masked_val = id ;
1661 0 ▶ for ( ; (id <= SECTION_MAX) && ( (id & mask) == masked_val); ++id )
Branch: 2 branches
BranchTrueFalseTotal
for ( ; (id <= SECTION_MAX) && ( (id & mask) == masked_val); ++id ) 0 0 0
for ( ; (id <= SECTION_MAX) && ( (id & mask) == masked_val); ++id ) 0 0 0
1662 {
1663 0 tsreader->section_decode_table[id].flags = flags ;
1664 0 tsreader->section_decode_table[id].handler = handler ;
1665 0 ++updated ;
1666 }
1667 0 return (updated) ;
1668 }
1669  
1670  
1671 /*=============================================================================================*/
1672 // TS_reader
1673  
1674 /* ----------------------------------------------------------------------- */
1675 // Set the start & end points for whole video based on min/max times of the streams
1676 0 void tsreader_set_timing(struct TS_reader *tsreader)
1677 {
1678 struct list_head *item;
1679 struct TS_pid *piditem;
1680  
1681 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1682 0 ▶ tsparse_dbg_prt(102, ("tsreader_set_timing()\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("tsreader_set_timing()\n")) ; 0 0 0
1683  
1684 0 ▶ list_for_each(item,&tsreader->tsstate->pid_list)
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(item,&tsreader->tsstate->pid_list) 0 0 0
1685 {
1686 0 piditem = list_entry(item, struct TS_pid, next);
1687  
1688 0 ▶ tsparse_dbg_prt(102, ("Start=%"PRId64" End=%"PRId64"\n", tsreader->tsstate->start_ts, tsreader->tsstate->end_ts)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("Start=%"PRId64" End=%"PRId64"\n", tsreader->tsstate->start_ts, tsreader->tsstate->end_ts)) ; 0 0 0
1689 0 ▶ tsparse_dbg_prt(102, ("PID %d\n", piditem->pidinfo.pid)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("PID %d\n", piditem->pidinfo.pid)) ; 0 0 0
1690 0 ▶ tsparse_dbg_prt(102, (" + PTS Start=%"PRId64" End=%"PRId64" : DTS Start=%"PRId64" End=%"PRId64"\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, (" + PTS Start=%"PRId64" End=%"PRId64" : DTS Start=%"PRId64" End=%"PRId64"\n", 0 0 0
1691 piditem->pesinfo.start_pts, piditem->pesinfo.end_pts,
1692 piditem->pesinfo.start_dts, piditem->pesinfo.end_dts
1693 )) ;
1694  
1695 // start
1696 0 ▶ if (piditem->pesinfo.start_pts != UNSET_TS)
Branch: 1 branch
BranchTrueFalseTotal
if (piditem->pesinfo.start_pts != UNSET_TS) 0 0 0
1697 {
1698 0 ▶ if ((tsreader->tsstate->start_ts == UNSET_TS) || (piditem->pesinfo.start_pts < tsreader->tsstate->start_ts))
Branch: 2 branches
BranchTrueFalseTotal
if ((tsreader->tsstate->start_ts == UNSET_TS) || (piditem->pesinfo.start_pts < tsreader->tsstate->start_ts)) 0 0 0
if ((tsreader->tsstate->start_ts == UNSET_TS) || (piditem->pesinfo.start_pts < tsreader->tsstate->start_ts)) 0 0 0
1699 {
1700 0 tsreader->tsstate->start_ts = piditem->pesinfo.start_pts ;
1701 0 ▶ tsparse_dbg_prt(102, (" + + Set start = pts\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, (" + + Set start = pts\n")) ; 0 0 0
1702 }
1703 }
1704 0 ▶ if (piditem->pesinfo.start_dts != UNSET_TS)
Branch: 1 branch
BranchTrueFalseTotal
if (piditem->pesinfo.start_dts != UNSET_TS) 0 0 0
1705 {
1706 0 ▶ if ((tsreader->tsstate->start_ts == UNSET_TS) || (piditem->pesinfo.start_dts < tsreader->tsstate->start_ts))
Branch: 2 branches
BranchTrueFalseTotal
if ((tsreader->tsstate->start_ts == UNSET_TS) || (piditem->pesinfo.start_dts < tsreader->tsstate->start_ts)) 0 0 0
if ((tsreader->tsstate->start_ts == UNSET_TS) || (piditem->pesinfo.start_dts < tsreader->tsstate->start_ts)) 0 0 0
1707 {
1708 0 tsreader->tsstate->start_ts = piditem->pesinfo.start_dts ;
1709 0 ▶ tsparse_dbg_prt(102, (" + + Set start = dts\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, (" + + Set start = dts\n")) ; 0 0 0
1710 }
1711 }
1712  
1713 // end
1714 0 ▶ if (piditem->pesinfo.end_pts != UNSET_TS)
Branch: 1 branch
BranchTrueFalseTotal
if (piditem->pesinfo.end_pts != UNSET_TS) 0 0 0
1715 {
1716 0 ▶ if ((tsreader->tsstate->end_ts == UNSET_TS) || (piditem->pesinfo.end_pts > tsreader->tsstate->end_ts))
Branch: 2 branches
BranchTrueFalseTotal
if ((tsreader->tsstate->end_ts == UNSET_TS) || (piditem->pesinfo.end_pts > tsreader->tsstate->end_ts)) 0 0 0
if ((tsreader->tsstate->end_ts == UNSET_TS) || (piditem->pesinfo.end_pts > tsreader->tsstate->end_ts)) 0 0 0
1717 {
1718 0 tsreader->tsstate->end_ts = piditem->pesinfo.end_pts ;
1719 0 ▶ tsparse_dbg_prt(102, (" + + Set end = pts\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, (" + + Set end = pts\n")) ; 0 0 0
1720 }
1721 }
1722 0 ▶ if (piditem->pesinfo.end_dts != UNSET_TS)
Branch: 1 branch
BranchTrueFalseTotal
if (piditem->pesinfo.end_dts != UNSET_TS) 0 0 0
1723 {
1724 0 ▶ if ((tsreader->tsstate->end_ts == UNSET_TS) || (piditem->pesinfo.end_dts > tsreader->tsstate->end_ts))
Branch: 2 branches
BranchTrueFalseTotal
if ((tsreader->tsstate->end_ts == UNSET_TS) || (piditem->pesinfo.end_dts > tsreader->tsstate->end_ts)) 0 0 0
if ((tsreader->tsstate->end_ts == UNSET_TS) || (piditem->pesinfo.end_dts > tsreader->tsstate->end_ts)) 0 0 0
1725 {
1726 0 tsreader->tsstate->end_ts = piditem->pesinfo.end_dts ;
1727 0 ▶ tsparse_dbg_prt(102, (" + + Set end = dts\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, (" + + Set end = dts\n")) ; 0 0 0
1728 }
1729 }
1730 };
1731  
1732 0 ▶ tsparse_dbg_prt(102, ("Start=%"PRId64" End=%"PRId64"\n", tsreader->tsstate->start_ts, tsreader->tsstate->end_ts)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("Start=%"PRId64" End=%"PRId64"\n", tsreader->tsstate->start_ts, tsreader->tsstate->end_ts)) ; 0 0 0
1733 0 ▶ tsparse_dbg_prt(102, ("tsreader_set_timing() - DONE\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(102, ("tsreader_set_timing() - DONE\n")) ; 0 0 0
1734 0 }
1735  
1736  
1737 /* ----------------------------------------------------------------------- */
1738 // Origin is as lseek ; skip_pkts can be -ve if origin is SEEK_END
1739 0 int tsreader_setpos(struct TS_reader *tsreader, int skip_pkts, int origin, unsigned num_pkts)
1740 {
1741 0 off64_t rc = 0 ;
1742 off64_t pos ;
1743 int abs_skip ;
1744 int skip_sign ;
1745  
1746 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1747  
1748 0 abs_skip = skip_pkts ;
1749 0 skip_sign = 1 ;
1750 0 ▶ if (skip_pkts < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (skip_pkts < 0) 0 0 0
1751 {
1752 0 abs_skip = -skip_pkts ;
1753 0 skip_sign = -1 ;
1754 }
1755  
1756 // check for over-run
1757 0 ▶ if (abs_skip > tsreader->tsstate->total_pkts)
Branch: 1 branch
BranchTrueFalseTotal
if (abs_skip > tsreader->tsstate->total_pkts) 0 0 0
1758 {
1759 0 abs_skip = tsreader->tsstate->total_pkts ;
1760 }
1761 0 skip_pkts = skip_sign * abs_skip ;
1762  
1763 // set position
1764 0 tsreader->num_pkts = num_pkts ;
1765 0 tsreader->skip = abs_skip ;
1766 0 tsreader->origin = origin ;
1767 0 tsreader->tsstate->pidinfo.pktnum = 0 ;
1768  
1769 // skip if no file open
1770 0 ▶ if (!tsreader->file)
Branch: 1 branch
BranchTrueFalseTotal
if (!tsreader->file) 0 0 0
1771 0 return 0 ;
1772  
1773 //TODO: check skip_pkts < num_pkts
1774 //TODO: check against total pkts (tsreader->tsstate->total_pkts)
1775  
1776  
1777 // calc pos
1778 0 pos = (off64_t)(skip_pkts) * (off64_t)(TS_PACKET_LEN) ;
1779  
1780 0 ▶ tsparse_dbg_prt(100, ("tsreader_setpos(skip=%d, origin=%d) pos=%"PRId64"\n", skip_pkts, origin, (long long int)pos)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("tsreader_setpos(skip=%d, origin=%d) pos=%"PRId64"\n", skip_pkts, origin, (long long int)pos)) ; 0 0 0
1781  
1782 0 rc = lseek64(tsreader->file, pos, origin) ;
1783  
1784 0 ▶ tsparse_dbg_prt(100, ("lseek pos now = %"PRId64"\n", (long long int)rc)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("lseek pos now = %"PRId64"\n", (long long int)rc)) ; 0 0 0
1785  
1786 0 ▶ if (rc == (off64_t)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (rc == (off64_t)-1) 0 0 0
1787 {
1788 0 SET_DVB_ERROR(ERR_FILE_SEEK) ;
1789 0 ▶ if (tsreader->debug >= 100)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->debug >= 100) 0 0 0
1790 0 perror("File seek error: ") ;
1791  
1792 // On error - reset to start of file
1793 0 lseek64(tsreader->file, (off64_t)0, SEEK_SET) ;
1794  
1795 //TODO: on error seek to 0?
1796 }
1797 else
1798 {
1799 // set packet number
1800 0 tsreader->tsstate->pidinfo.pktnum = (unsigned)(rc / TS_PACKET_LEN) ;
1801 }
1802  
1803 0 return(dvb_error_code) ;
1804 }
1805  
1806 /* ----------------------------------------------------------------------- */
1807 0 void tsreader_start_framenum(struct TS_reader *tsreader, unsigned framenum)
1808 {
1809 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1810 0 tsreader->mpeg2.start_framenum = framenum ;
1811 0 tsreader->mpeg2.framenum = framenum ;
1812 0 }
1813  
1814  
1815 /* ----------------------------------------------------------------------- */
1816 0 struct TS_reader *tsreader_new(char *filename)
1817 {
1818 0 int file = 0 ;
1819 0 struct TS_reader *tsreader = NULL ;
1820 off64_t size ;
1821  
1822 // open file iff specified
1823 0 ▶ if (filename)
Branch: 1 branch
BranchTrueFalseTotal
if (filename) 0 0 0
1824 {
1825 0 file = open(filename, O_RDONLY | O_LARGEFILE | O_BINARY, 0666);
1826 0 ▶ if (-1 == file)
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == file) 0 0 0
1827 {
1828 0 SET_DVB_ERROR(ERR_FILE) ;
1829 0 return(NULL);
1830 }
1831 }
1832  
1833 // create struct
1834 0 tsreader = (struct TS_reader *)malloc(sizeof(struct TS_reader)) ;
1835 0 CLEAR_MEM(tsreader) ;
1836 0 tsreader->MAGIC = MAGIC_READER ;
1837  
1838 0 tsreader->file = file ;
1839 0 tsreader->tsstate = tsstate_new() ;
1840  
1841 // work out total number of packets
1842 0 ▶ if (tsreader->file)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->file) 0 0 0
1843 {
1844 0 size = lseek64(tsreader->file, -1, SEEK_END) ;
1845  
1846 // size = -1 :
1847 // errorno=EINVAL for zero length file
1848 0 ▶ if (size < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (size < 0) 0 0 0
1849 {
1850 0 size = 0 ;
1851 0 SET_DVB_ERROR(ERR_FILE) ;
1852 0 ▶ if (errno == EINVAL)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINVAL) 0 0 0
1853 {
1854 0 SET_DVB_ERROR(ERR_FILE_ZERO) ;
1855 }
1856 0 tsreader_free(tsreader) ;
1857 0 return(NULL) ;
1858 }
1859 0 tsreader->tsstate->total_pkts = (unsigned)(size / (off64_t)TS_PACKET_LEN) ;
1860 }
1861  
1862 // set position
1863 0 tsreader_setpos(tsreader, 0, SEEK_SET, 0) ;
1864  
1865 0 return tsreader ;
1866 }
1867  
1868  
1869 /* ----------------------------------------------------------------------- */
1870 0 struct TS_reader *tsreader_new_nofile()
1871 {
1872 0 struct TS_reader *tsreader = tsreader_new( (char *)NULL ) ;
1873  
1874 0 return tsreader ;
1875 }
1876  
1877 /* ----------------------------------------------------------------------- */
1878 // abort loop
1879 0 void tsreader_stop(struct TS_reader *tsreader)
1880 {
1881 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1882 0 tsreader->tsstate->stop_flag = 1 ;
1883 0 }
1884  
1885 /* ----------------------------------------------------------------------- */
1886 0 void tsreader_free(struct TS_reader *tsreader)
1887 {
1888 0 ▶ if (tsreader)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader) 0 0 0
1889 {
1890 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1891 0 ▶ if (tsreader->file)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->file) 0 0 0
1892 0 close(tsreader->file) ;
1893  
1894 0 tsstate_free(tsreader->tsstate) ;
1895  
1896 // optionally clear out libmpeg2 settings
1897 0 ▶ if (tsreader->mpeg2.decoder)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.decoder) 0 0 0
1898 0 mpeg2_close (tsreader->mpeg2.decoder);
1899 0 ▶ if (tsreader->mpeg2.video_buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.video_buffer) 0 0 0
1900 0 free(tsreader->mpeg2.video_buffer) ;
1901 0 free_frame_info_list(tsreader) ;
1902  
1903 // optionally clear mpeg2audio settings
1904 0 ▶ if (tsreader->mpeg2audio.audio_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2audio.audio_buf) 0 0 0
1905 0 free(tsreader->mpeg2audio.audio_buf) ;
1906  
1907 0 ▶ if (tsreader->mpeg2audio.storage_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2audio.storage_buf) 0 0 0
1908 0 free(tsreader->mpeg2audio.storage_buf) ;
1909  
1910 0 free(tsreader) ;
1911 }
1912 0 }
1913  
1914 /* ----------------------------------------------------------------------- */
1915 0 static void tsreader_set_mpeg2(struct TS_reader *tsreader)
1916 {
1917 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1918  
1919 // optionally set up libmpeg2 settings
1920 0 ▶ if (tsreader->mpeg2_hook || tsreader->mpeg2_rgb_hook)
Branch: 2 branches
BranchTrueFalseTotal
if (tsreader->mpeg2_hook || tsreader->mpeg2_rgb_hook) 0 0 0
if (tsreader->mpeg2_hook || tsreader->mpeg2_rgb_hook) 0 0 0
1921 {
1922 0 ▶ if (!tsreader->mpeg2.decoder)
Branch: 1 branch
BranchTrueFalseTotal
if (!tsreader->mpeg2.decoder) 0 0 0
1923 {
1924 0 tsreader->mpeg2.decoder = mpeg2_init ();
1925 0 ▶ if (tsreader->mpeg2.decoder == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2.decoder == NULL) { 0 0 0
1926 0 fprintf (stderr, "Could not allocate a decoder object.\n");
1927 0 exit (1);
1928 }
1929 0 tsreader->mpeg2.info = mpeg2_info (tsreader->mpeg2.decoder);
1930 0 tsreader->mpeg2.framenum = 0;
1931 0 tsreader->mpeg2.start_framenum = 0;
1932 0 tsreader->mpeg2.gop_pktnum = 0;
1933  
1934 0 tsreader->mpeg2.convert_rgb = 0;
1935 0 ▶ if (tsreader->mpeg2_rgb_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->mpeg2_rgb_hook) 0 0 0
1936 {
1937 // colour conversion
1938 0 tsreader->mpeg2.convert_rgb = 1;
1939 }
1940  
1941 0 tsreader->mpeg2.frame_info_list_size = 0 ;
1942 0 tsreader->mpeg2.frame_info_list = NULL ;
1943 0 tsreader->mpeg2.frame_info_index = 0 ;
1944 }
1945 }
1946 0 }
1947  
1948  
1949 /* ----------------------------------------------------------------------- */
1950 0 static void tsreader_set_audio(struct TS_reader *tsreader)
1951 {
1952 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1953  
1954 // optionally set up mpeg2audio settings
1955 0 ▶ if (tsreader->audio_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->audio_hook) 0 0 0
1956 {
1957 0 ▶ if (!tsreader->mpeg2audio.audio_init)
Branch: 1 branch
BranchTrueFalseTotal
if (!tsreader->mpeg2audio.audio_init) 0 0 0
1958 {
1959 0 decode_init();
1960 0 tsreader->mpeg2audio.audio_init = 1 ;
1961 0 tsreader->mpeg2audio.framenum = 0;
1962 0 tsreader->mpeg2audio.audio_samples = 0;
1963  
1964 0 tsreader->mpeg2audio.audio_buf = (short *)malloc(sizeof(short) * AUDIOBUFFER) ;
1965 0 CLEAR_MEM(tsreader->mpeg2audio.audio_buf) ;
1966  
1967 0 tsreader->mpeg2audio.storage_buf = (uint8_t *)malloc(sizeof(uint8_t) * STORAGE_SIZE) ;
1968 0 CLEAR_MEM(tsreader->mpeg2audio.storage_buf) ;
1969 0 tsreader->mpeg2audio.write_ptr = tsreader->mpeg2audio.storage_buf ;
1970 0 tsreader->mpeg2audio.read_ptr = tsreader->mpeg2audio.storage_buf ;
1971 }
1972 }
1973 0 }
1974  
1975  
1976 /* ----------------------------------------------------------------------- */
1977 // Only called when a file is specified
1978 0 int ts_parse(struct TS_reader *tsreader)
1979 {
1980 uint8_t buffer[TS_BUFFSIZE];
1981 int bytes_read ;
1982 int status;
1983  
1984 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
1985  
1986 0 status = 0 ;
1987  
1988 // check file open
1989 0 ▶ if (!tsreader->file)
Branch: 1 branch
BranchTrueFalseTotal
if (!tsreader->file) 0 0 0
1990 {
1991 0 RETURN_DVB_ERROR(ERR_FILE) ;
1992 }
1993  
1994 // initialise
1995 0 status = tsreader_data_start(tsreader) ;
1996 0 ▶ if (status) return (status) ;
Branch: 1 branch
BranchTrueFalseTotal
if (status) return (status) ; 0 0 0
1997  
1998 // main loop
1999 0 ▶ while (tsreader->buff_state.running > 0)
Branch: 1 branch
BranchTrueFalseTotal
while (tsreader->buff_state.running > 0) 0 0 0
2000 {
2001 0 bytes_read = TS_BUFFSIZE_READ ;
2002 0 status = getbuff(tsreader->file, buffer, &bytes_read) ;
2003  
2004 // add packet and process
2005 0 status = tsreader_data_add(tsreader, buffer, bytes_read) ;
2006 0 ▶ if (status) return (status) ;
Branch: 1 branch
BranchTrueFalseTotal
if (status) return (status) ; 0 0 0
2007  
2008 } // while running
2009  
2010 // finish off
2011 0 status = tsreader_data_end(tsreader) ;
2012  
2013 0 return status;
2014 }
2015  
2016  
2017 /* ----------------------------------------------------------------------- */
2018 0 int tsreader_data_start(struct TS_reader *tsreader)
2019 {
2020 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
2021 0 ▶ tsparse_dbg_prt(10, ("TS: tsreader_data_start()\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: tsreader_data_start()\n")) ; 0 0 0
2022 0 ▶ tsparse_dbg_prt(100, ("TS: # Total packets = %d\n", tsreader->tsstate->total_pkts)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("TS: # Total packets = %d\n", tsreader->tsstate->total_pkts)) ; 0 0 0
2023  
2024 0 tsreader->buff_state.bptr = tsreader->buff_state.buffer ;
2025 0 tsreader->buff_state.buffer_len = 0 ;
2026 0 tsreader->buff_state.running = 1 ;
2027 0 tsreader->buff_state.get_sync = 1 ;
2028 0 tsreader->buff_state.pktnum = 0 ;
2029 0 tsreader->buff_state.status = 0 ;
2030  
2031  
2032 // Ensure libmpeg2 info is correct
2033 0 tsreader_set_mpeg2(tsreader) ;
2034  
2035 // Ensure audio info is set up
2036 0 tsreader_set_audio(tsreader) ;
2037  
2038 // Progress required?
2039 0 ▶ if (tsreader->progress_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->progress_hook) 0 0 0
2040 {
2041 0 tsreader->progress_info.scale = 1 ;
2042 if (tsreader->tsstate->total_pkts*100 > 0xffffffff)
2043 {
2044 tsreader->progress_info.scale = ( (tsreader->tsstate->total_pkts*100) / 0xffffffff) + 1 ;
2045 }
2046 0 tsreader->progress_info.step = tsreader->tsstate->total_pkts / (tsreader->progress_info.scale * 100) ;
2047 0 tsreader->progress_info.total = tsreader->tsstate->total_pkts / tsreader->progress_info.scale ;
2048 0 tsreader->progress_info.next_progress = tsreader->progress_info.step ;
2049  
2050 0 tsreader->progress_hook(PROGRESS_START, 0, tsreader->progress_info.total, tsreader->user_data) ;
2051 }
2052  
2053 0 return 0 ;
2054 }
2055  
2056  
2057  
2058 /* ----------------------------------------------------------------------- */
2059 0 int tsreader_data_add(struct TS_reader *tsreader, uint8_t *data, unsigned data_len)
2060 {
2061 unsigned byte_num ;
2062  
2063 0 ▶tsparse_dbg_prt(10, ("TS: tsreader_data_add() running=%d data_len=%d : Current size = %d\n", tsreader->buff_state.running, data_len, tsreader->buff_state.buffer_len)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: tsreader_data_add() running=%d data_len=%d : Current size = %d\n", tsreader->buff_state.running, data_len, tsreader->buff_state.buffer_len)) ; 0 0 0
2064  
2065 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
2066  
2067 // skip if stopped
2068 0 ▶ if (!tsreader->buff_state.running)
Branch: 1 branch
BranchTrueFalseTotal
if (!tsreader->buff_state.running) 0 0 0
2069 0 return 0 ;
2070  
2071 // skip if no data
2072 0 ▶ if (data_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (data_len == 0) 0 0 0
2073 0 return 0 ;
2074  
2075 // add new data
2076 0 ▶ if (tsreader->buff_state.buffer_len < TS_PACKET_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->buff_state.buffer_len < TS_PACKET_LEN) 0 0 0
2077 {
2078 0 tsreader->buff_state.bptr = tsreader->buff_state.buffer ;
2079 0 tsreader->buff_state.buffer_len = data_len ;
2080 0 memcpy(tsreader->buff_state.buffer, data, data_len*sizeof(uint8_t)) ;
2081  
2082 0 tsreader->buff_state.get_sync = 1 ;
2083  
2084 //fprintf(stderr, "bptr[0]=0x%02x\n", tsreader->buff_state.bptr[0]) ;
2085 //fprintf(stderr, "bptr=%p\n", tsreader->buff_state.bptr) ;
2086 //fprintf(stderr, "buffer_len=%d\n", tsreader->buff_state.buffer_len) ;
2087  
2088 0 ▶ tsparse_dbg_prt(10, ("TS: Reload buffer : 0x%02x (bptr @ %p) %d bytes left\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: Reload buffer : 0x%02x (bptr @ %p) %d bytes left\n", 0 0 0
2089 tsreader->buff_state.bptr[0], tsreader->buff_state.bptr, tsreader->buff_state.buffer_len)) ;
2090 }
2091 // check overflow (should never happen!)
2092 0 ▶ else if (tsreader->buff_state.buffer_len + data_len >= TS_BUFFSIZE)
Branch: 1 branch
BranchTrueFalseTotal
else if (tsreader->buff_state.buffer_len + data_len >= TS_BUFFSIZE) 0 0 0
2093 {
2094 // restart
2095 0 data_len %= TS_BUFFSIZE ;
2096 0 tsreader->buff_state.bptr = tsreader->buff_state.buffer ;
2097 0 tsreader->buff_state.buffer_len = data_len ;
2098 0 memcpy(tsreader->buff_state.buffer, data, data_len*sizeof(uint8_t)) ;
2099  
2100 0 tsreader->buff_state.get_sync = 1 ;
2101  
2102 0 fprintf(stderr, "\n**OVERFLOW**\n") ;
2103  
2104 0 ▶ tsparse_dbg_prt(10, ("TS: *OVERFLOW* Reload buffer : 0x%02x (bptr @ %p) %d bytes left\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: *OVERFLOW* Reload buffer : 0x%02x (bptr @ %p) %d bytes left\n", 0 0 0
2105 tsreader->buff_state.bptr[0], tsreader->buff_state.bptr, tsreader->buff_state.buffer_len)) ;
2106 }
2107 // append data
2108 else
2109 {
2110 // unsigned data_used = (tsreader->buff_state.bptr - tsreader->buff_state.buffer) ;
2111 // if (data_len + tsreader->buff_state.buffer_len + data_used >= TS_BUFFSIZE )
2112 // {
2113 // data_len = TS_BUFFSIZE - 1 - (tsreader->buff_state.buffer_len + data_used) ;
2114 // }
2115  
2116 // append data
2117 0 memcpy(&tsreader->buff_state.bptr[tsreader->buff_state.buffer_len], data, data_len*sizeof(uint8_t)) ;
2118 0 tsreader->buff_state.buffer_len += data_len ;
2119  
2120 0 ▶ tsparse_dbg_prt(10, ("TS: Append buffer : 0x%02x (bptr @ %p) %d bytes left\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: Append buffer : 0x%02x (bptr @ %p) %d bytes left\n", 0 0 0
2121 tsreader->buff_state.bptr[0], tsreader->buff_state.bptr, tsreader->buff_state.buffer_len)) ;
2122 }
2123  
2124 0 ▶ while (tsreader->buff_state.running && (tsreader->buff_state.buffer_len > 0) )
Branch: 2 branches
BranchTrueFalseTotal
while (tsreader->buff_state.running && (tsreader->buff_state.buffer_len > 0) ) 0 0 0
while (tsreader->buff_state.running && (tsreader->buff_state.buffer_len > 0) ) 0 0 0
2125 {
2126 0 ▶ tsparse_dbg_prt(100, ("TS: Loop start...(len=%d) running=%d, get sync=%d\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("TS: Loop start...(len=%d) running=%d, get sync=%d\n", 0 0 0
2127 tsreader->buff_state.buffer_len,
2128 tsreader->buff_state.running,
2129 tsreader->buff_state.get_sync)) ;
2130  
2131 // check for sync if required
2132 0 ▶ if (tsreader->buff_state.get_sync)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->buff_state.get_sync) 0 0 0
2133 {
2134 0 ▶ tsparse_dbg_prt(10, ("TS: waiting for sync...\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: waiting for sync...\n")) ; 0 0 0
2135  
2136 // wait for sync byte, but abort if we've waited for at least 4 packets and not found it
2137 0 byte_num=0;
2138 0 ▶ while ( (tsreader->buff_state.buffer_len > 0) && (tsreader->buff_state.bptr[0] != SYNC_BYTE) && (byte_num < (4*TS_PACKET_LEN)) )
Branch: 3 branches
BranchTrueFalseTotal
while ( (tsreader->buff_state.buffer_len > 0) && (tsreader->buff_state.bptr[0] != SYNC_BYTE) && (byte_num < (4*TS_PACKET_LEN)) ) 0 0 0
while ( (tsreader->buff_state.buffer_len > 0) && (tsreader->buff_state.bptr[0] != SYNC_BYTE) && (byte_num < (4*TS_PACKET_LEN)) ) 0 0 0
while ( (tsreader->buff_state.buffer_len > 0) && (tsreader->buff_state.bptr[0] != SYNC_BYTE) && (byte_num < (4*TS_PACKET_LEN)) ) 0 0 0
2139 {
2140 0 tsreader->buff_state.buffer_len-- ;
2141 0 tsreader->buff_state.bptr++ ;
2142 0 byte_num++ ;
2143  
2144 0 ▶ tsparse_dbg_prt(10, ("TS: + byte[0]=0x%02x num=%d\n", tsreader->buff_state.bptr[0], byte_num)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: + byte[0]=0x%02x num=%d\n", tsreader->buff_state.bptr[0], byte_num)) ; 0 0 0
2145 }
2146 0 tsreader->buff_state.get_sync = 0 ;
2147  
2148 // did we find it?
2149 0 ▶ if ((tsreader->buff_state.buffer_len == 0) || (tsreader->buff_state.bptr[0] != SYNC_BYTE))
Branch: 2 branches
BranchTrueFalseTotal
if ((tsreader->buff_state.buffer_len == 0) || (tsreader->buff_state.bptr[0] != SYNC_BYTE)) 0 0 0
if ((tsreader->buff_state.buffer_len == 0) || (tsreader->buff_state.bptr[0] != SYNC_BYTE)) 0 0 0
2150 {
2151 // clear buffer
2152 0 tsreader->buff_state.get_sync = 1 ;
2153 0 tsreader->buff_state.buffer_len = 0 ;
2154  
2155 // return error
2156 0 RETURN_DVB_ERROR(ERR_NOSYNC) ;
2157 }
2158 }
2159 0 ▶ tsparse_dbg_prt(10, ("TS: handling TS packets...(buffer @ %p => 0x%02x)\n", tsreader->buff_state.buffer, tsreader->buff_state.bptr[0])) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: handling TS packets...(buffer @ %p => 0x%02x)\n", tsreader->buff_state.buffer, tsreader->buff_state.bptr[0])) ; 0 0 0
2160  
2161 // handle rest of TS packet(s)
2162 0 ▶ while ( tsreader->buff_state.running && !tsreader->buff_state.get_sync && (tsreader->buff_state.buffer_len >= TS_PACKET_LEN) )
Branch: 3 branches
BranchTrueFalseTotal
while ( tsreader->buff_state.running && !tsreader->buff_state.get_sync && (tsreader->buff_state.buffer_len >= TS_PACKET_LEN) ) 0 0 0
while ( tsreader->buff_state.running && !tsreader->buff_state.get_sync && (tsreader->buff_state.buffer_len >= TS_PACKET_LEN) ) 0 0 0
while ( tsreader->buff_state.running && !tsreader->buff_state.get_sync && (tsreader->buff_state.buffer_len >= TS_PACKET_LEN) ) 0 0 0
2163 {
2164 0 ▶ tsparse_dbg_prt(10, ("TS: Start data of loop : 0x%02x (bptr @ %p) %d bytes left : local pkt count = %u\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: Start data of loop : 0x%02x (bptr @ %p) %d bytes left : local pkt count = %u\n", 0 0 0
2165 tsreader->buff_state.bptr[0], tsreader->buff_state.bptr, tsreader->buff_state.buffer_len, tsreader->buff_state.pktnum)) ;
2166 0 ▶ tsparse_dbg_prt(10, ("TS: # pkt count = %u\n", tsreader->buff_state.pktnum)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: # pkt count = %u\n", tsreader->buff_state.pktnum)) ; 0 0 0
2167  
2168 // check sync byte
2169 0 ▶ if (tsreader->buff_state.bptr[0] != SYNC_BYTE)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->buff_state.bptr[0] != SYNC_BYTE) 0 0 0
2170 {
2171 // re-sync
2172 0 ++tsreader->buff_state.get_sync ;
2173  
2174 0 ▶ tsparse_dbg_prt(10, ("TS: ! Resync required : 0x%02x (bptr @ %p)\n", tsreader->buff_state.bptr[0], tsreader->buff_state.bptr)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: ! Resync required : 0x%02x (bptr @ %p)\n", tsreader->buff_state.bptr[0], tsreader->buff_state.bptr)) ; 0 0 0
2175 }
2176 else
2177 {
2178 // Do something with the packet
2179 0 parse_ts_packet(tsreader, tsreader->tsstate, tsreader->buff_state.bptr, TS_PACKET_LEN) ;
2180  
2181 // Progress required?
2182 0 ▶ if (tsreader->progress_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->progress_hook) 0 0 0
2183 {
2184 0 ▶ if (tsreader->buff_state.pktnum == tsreader->progress_info.next_progress)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->buff_state.pktnum == tsreader->progress_info.next_progress) 0 0 0
2185 {
2186 0 tsreader->progress_hook(PROGRESS_RUNNING,
2187 0 (unsigned)(tsreader->buff_state.pktnum / tsreader->progress_info.scale),
2188 tsreader->progress_info.total,
2189 tsreader->user_data) ;
2190  
2191 0 tsreader->progress_info.next_progress += tsreader->progress_info.step ;
2192 }
2193 }
2194  
2195 //== check for end ==
2196 0 ++tsreader->buff_state.pktnum ;
2197 0 ++tsreader->tsstate->pidinfo.pktnum ;
2198  
2199 // only compare with total packets if it's been set!
2200 0 ▶ if (tsreader->tsstate->total_pkts && (tsreader->buff_state.pktnum >= tsreader->tsstate->total_pkts) )
Branch: 2 branches
BranchTrueFalseTotal
if (tsreader->tsstate->total_pkts && (tsreader->buff_state.pktnum >= tsreader->tsstate->total_pkts) ) 0 0 0
if (tsreader->tsstate->total_pkts && (tsreader->buff_state.pktnum >= tsreader->tsstate->total_pkts) ) 0 0 0
2201 {
2202 // reached end of file
2203 0 tsreader->buff_state.running = 0 ;
2204 0 ▶ tsparse_dbg_prt(100, ("TS: stop running total pkts (len=%d)\n", tsreader->buff_state.buffer_len)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("TS: stop running total pkts (len=%d)\n", tsreader->buff_state.buffer_len)) ; 0 0 0
2205 }
2206  
2207 // check if max packets has been set
2208 0 ▶ if (tsreader->num_pkts && (tsreader->buff_state.pktnum >= tsreader->num_pkts) )
Branch: 2 branches
BranchTrueFalseTotal
if (tsreader->num_pkts && (tsreader->buff_state.pktnum >= tsreader->num_pkts) ) 0 0 0
if (tsreader->num_pkts && (tsreader->buff_state.pktnum >= tsreader->num_pkts) ) 0 0 0
2209 {
2210 // reached requested number of packets
2211 0 tsreader->buff_state.running = 0 ;
2212 0 ▶ tsparse_dbg_prt(100, ("TS: stop running num pkts> (len=%d)\n", tsreader->buff_state.buffer_len)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("TS: stop running num pkts> (len=%d)\n", tsreader->buff_state.buffer_len)) ; 0 0 0
2213 }
2214  
2215 // check special STOP flag
2216 0 ▶ if (tsreader->tsstate->stop_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->tsstate->stop_flag) 0 0 0
2217 {
2218 // stop request
2219 0 tsreader->buff_state.running = 0 ;
2220 0 ▶ tsparse_dbg_prt(100, ("TS: stop running flag (len=%d)\n", tsreader->buff_state.buffer_len)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("TS: stop running flag (len=%d)\n", tsreader->buff_state.buffer_len)) ; 0 0 0
2221 }
2222  
2223 // update buffer
2224 0 tsreader->buff_state.buffer_len -= TS_PACKET_LEN ;
2225 0 tsreader->buff_state.bptr += TS_PACKET_LEN ;
2226  
2227 } // if get_sync
2228  
2229 0 ▶ tsparse_dbg_prt(10, ("TS: End of data loop : 0x%02x (bptr @ %p) %d bytes left\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: End of data loop : 0x%02x (bptr @ %p) %d bytes left\n", 0 0 0
2230 tsreader->buff_state.bptr[0], tsreader->buff_state.bptr, tsreader->buff_state.buffer_len)) ;
2231  
2232 0 ▶ tsparse_dbg_prt(10, ("TS: running=%d, get sync=%d, buff len=%d\n",
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: running=%d, get sync=%d, buff len=%d\n", 0 0 0
2233 tsreader->buff_state.running, tsreader->buff_state.get_sync, tsreader->buff_state.buffer_len)) ;
2234  
2235 } // while in sync
2236  
2237  
2238 // process other bytes if still got data
2239 0 ▶ if ((tsreader->buff_state.buffer_len > 0) && (tsreader->buff_state.bptr[0] != SYNC_BYTE))
Branch: 2 branches
BranchTrueFalseTotal
if ((tsreader->buff_state.buffer_len > 0) && (tsreader->buff_state.bptr[0] != SYNC_BYTE)) 0 0 0
if ((tsreader->buff_state.buffer_len > 0) && (tsreader->buff_state.bptr[0] != SYNC_BYTE)) 0 0 0
2240 {
2241 0 tsreader->buff_state.get_sync = 1 ;
2242 }
2243  
2244 0 ▶ tsparse_dbg_prt(100, ("TS: Loop end...(len=%d)\n", tsreader->buff_state.buffer_len)) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(100, ("TS: Loop end...(len=%d)\n", tsreader->buff_state.buffer_len)) ; 0 0 0
2245  
2246 } // while got data
2247  
2248 0 return 0 ;
2249 }
2250  
2251  
2252 /* ----------------------------------------------------------------------- */
2253 0 int tsreader_data_end(struct TS_reader *tsreader)
2254 {
2255 0 ▶ CHECK_TS_READER(tsreader) ;
Branch: 2 branches
BranchTrueFalseTotal
CHECK_TS_READER(tsreader) ; 0 0 0
CHECK_TS_READER(tsreader) ; 0 0 0
2256  
2257 // Progress required?
2258 0 ▶ if (tsreader->progress_hook)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->progress_hook) 0 0 0
2259 {
2260 0 unsigned scaled_pktnum = (unsigned)(tsreader->buff_state.pktnum / tsreader->progress_info.scale) ;
2261 0 ▶ if (scaled_pktnum > tsreader->progress_info.total) scaled_pktnum = tsreader->progress_info.total ;
Branch: 1 branch
BranchTrueFalseTotal
if (scaled_pktnum > tsreader->progress_info.total) scaled_pktnum = tsreader->progress_info.total ; 0 0 0
2262  
2263 0 ▶ if (tsreader->tsstate->stop_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (tsreader->tsstate->stop_flag) 0 0 0
2264 {
2265 // premature stop
2266 0 tsreader->progress_hook(PROGRESS_STOPPED,
2267 scaled_pktnum,
2268 tsreader->progress_info.total,
2269 tsreader->user_data) ;
2270 }
2271 else
2272 {
2273 // run to end
2274 0 tsreader->progress_hook(PROGRESS_END,
2275 tsreader->progress_info.total,
2276 tsreader->progress_info.total,
2277 tsreader->user_data) ;
2278 }
2279 }
2280  
2281 0 ▶ tsparse_dbg_prt(10, ("TS: tsreader_data_end() - END\n")) ;
Branch: 1 branch
BranchTrueFalseTotal
tsparse_dbg_prt(10, ("TS: tsreader_data_end() - END\n")) ; 0 0 0
2282  
2283 0 return 0;
2284 }
2285  
« clib/dvb_ts_lib/ts_cut.c ↑ Summary clib/dvb_ts_lib/ts_skip.c »