clib/libng/parse-mpeg.c

filter: Statementstmt 0.0% 0 / 425 Branchbran 0.0% 0 / 190 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 615 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« clib/libng/parse-dvb.c ↑ Summary clib/mpeg2audio_stubs/mpegaudio_stubs.c »
1 /*
2 * parse mpeg program + transport streams.
3 *
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <errno.h>
11 #include <iconv.h>
12 #include <inttypes.h>
13  
14 #include "parse-mpeg.h"
15  
16 // dvb_ts_lib
17 #include "descriptors/desc_structs.h"
18  
19 // dvb_lib
20 #include "dvb_debug.h"
21 #include "dvb_lib.h"
22  
23 // TODO: Move into dvb_ts_lib
24 enum MPEG1_descriptor_ids {
25 DESC_ISO639_LANG = 0x0A, // ISO13818-1 table 2-39
26 } ;
27  
28  
29 static const char *stream_type_s[] = {
30 [ 0x00 ] = "reserved",
31 [ 0x01 ] = "ISO 11172 Video",
32 [ 0x02 ] = "ISO 13818-2 Video",
33 [ 0x03 ] = "ISO 11172 Audio",
34 [ 0x04 ] = "ISO 13818-3 Audio",
35 [ 0x05 ] = "ISO 13818-1 private sections",
36 [ 0x06 ] = "ISO 13818-1 private data",
37 [ 0x07 ] = "ISO 13522 MHEG",
38 [ 0x08 ] = "ISO 13818-1 Annex A DSS CC",
39 [ 0x09 ] = "ITU-T H.222.1",
40 [ 0x0a ] = "ISO 13818-6 type A",
41 [ 0x0b ] = "ISO 13818-6 type B",
42 [ 0x0c ] = "ISO 13818-6 type C",
43 [ 0x0d ] = "ISO 13818-6 type D",
44 [ 0x0e ] = "ISO 13818-6 auxiliary",
45 [ 0x0f ... 0x7f ] = "reserved",
46 [ 0x80 ... 0xff ] = "user private",
47 };
48  
49 /* ----------------------------------------------------------------------- */
50  
51 char *psi_charset[0x20] = {
52 [ 0x00 ... 0x1f ] = "reserved",
53 [ 0x00 ] = "ISO-8859-1",
54 [ 0x01 ] = "ISO-8859-5",
55 [ 0x02 ] = "ISO-8859-6",
56 [ 0x03 ] = "ISO-8859-7",
57 [ 0x04 ] = "ISO-8859-8",
58 [ 0x05 ] = "ISO-8859-9",
59 [ 0x06 ] = "ISO-8859-10",
60 [ 0x07 ] = "ISO-8859-11",
61 [ 0x08 ] = "ISO-8859-12",
62 [ 0x09 ] = "ISO-8859-13",
63 [ 0x0a ] = "ISO-8859-14",
64 [ 0x0b ] = "ISO-8859-15",
65 [ 0x10 ] = "fixme",
66 [ 0x11 ] = "UCS-2BE", // correct?
67 [ 0x12 ] = "EUC-KR",
68 [ 0x13 ] = "GB2312",
69 [ 0x14 ] = "BIG5"
70 };
71  
72 char *psi_service_type[0x100] = {
73 [ 0x00 ... 0xff ] = "reserved",
74 [ 0x80 ... 0xfe ] = "user defined",
75 [ 0x01 ] = "digital television service",
76 [ 0x02 ] = "digital radio sound service",
77 [ 0x03 ] = "teletext service",
78 [ 0x04 ] = "NVOD reference service",
79 [ 0x05 ] = "NVOD time-shifted service",
80 [ 0x06 ] = "mosaic service",
81 [ 0x07 ] = "PAL coded signal",
82 [ 0x08 ] = "SECAM coded signal",
83 [ 0x09 ] = "D/D2-MAC",
84 [ 0x0a ] = "FM Radio",
85 [ 0x0b ] = "NTSC coded signal",
86 [ 0x0c ] = "data broadcast service",
87 [ 0x0d ] = "reserved for CI",
88 [ 0x0e ] = "RCS Map",
89 [ 0x0f ] = "RCS FLS",
90 [ 0x10 ] = "DVB MHP service",
91 };
92  
93  
94 /* ----------------------------------------------------------------------- */
95 // DEBUG
96 /* ----------------------------------------------------------------------- */
97  
98 /* ----------------------------------------------------------------------- */
99 0 void print_stream(struct psi_stream *stream)
100 {
101 int i ;
102 struct list_head *item, *safe, *pitem ;
103 struct prog_info *pinfo ;
104  
105 // int tsid;
106 //
107 // /* network */
108 // int netid;
109 // char net[PSI_STR_MAX];
110 //
111 // int frequency;
112 // int symbol_rate;
113 // char *bandwidth;
114 // char *constellation;
115 // char *hierarchy;
116 // char *code_rate_hp;
117 // char *code_rate_lp;
118 // char *fec_inner;
119 // char *guard;
120 // char *transmission;
121 // char *polarization;
122 // int other_freq;
123 // int *freq_list;
124 //
125 // /* status info */
126 // int updated;
127 // int tuned;
128 //
129 0 fprintf(stderr, "TSID %d NETID %d : network %s : freq %d : sr %d : BW %s : Const %s : Hier %s : Code rate hp %s lp %s : FEC %s : Guard %s : Tx %s : Pol %s : Other Freq %d (up %d, tuned %d) : Freq list len=%d\n",
130 0 stream->tsid, stream->netid, stream->net, stream->frequency, stream->symbol_rate,
131 stream->bandwidth, stream->constellation, stream->hierarchy, stream->code_rate_hp, stream->code_rate_lp,
132 stream->fec_inner, stream->guard, stream->transmission, stream->polarization,
133 stream->other_freq,
134 stream->updated, stream->tuned,
135 stream->freq_list_len
136 ) ;
137  
138 0 ▶ list_for_each(pitem,&stream->prog_info_list)
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(pitem,&stream->prog_info_list) 0 0 0
139 {
140 0 pinfo = list_entry(pitem, struct prog_info, next);
141  
142 /*
143 int service_id ;
144 int service_type ;
145 int visible ;
146 int lcn ;
147 */
148 0 fprintf(stderr, " LCN: %3d sid 0x%04x type %d visible %d\n",
149 pinfo->lcn,
150 pinfo->service_id,
151 pinfo->service_type,
152 pinfo->visible
153 ) ;
154 }
155  
156 0 ▶ for (i = 0; i < stream->freq_list_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < stream->freq_list_len; i++) 0 0 0
157 {
158 0 fprintf(stderr, " FREQ[%3d] = %d Hz\n", i, stream->freq_list[i]) ;
159 }
160  
161 0 }
162  
163 /* ----------------------------------------------------------------------- */
164 0 void print_program(struct psi_program *program)
165 {
166 struct list_head *item, *safe, *pitem ;
167 struct freq_info *finfo ;
168  
169 // int tsid;
170 // int pnr;
171 // int version;
172 // int running;
173 // int ca;
174 //
175 // /* program data */
176 // int type;
177 // int p_pid; // program
178 // int v_pid; // video
179 // int a_pid; // audio
180 // int t_pid; // teletext
181 // char audio[PSI_STR_MAX];
182 // char net[PSI_STR_MAX];
183 // char name[PSI_STR_MAX];
184 //
185 // /* status info */
186 // int updated;
187 // int seen;
188  
189 // if (dvb_debug >= 15)
190 // {
191 // fprintf(stderr, "PROG [program=>%p list.next=%p list.prev=%p] ", program, program->next.next, program->next.prev) ;
192 // }
193 0 fprintf(stderr, "TSID %d PNR %d : name %s : network %s : running %d : type %d : prog %d, video %d, audio %d, ttext %d, pcr %d : audio %s subtitle %s (up %d / seen %d) : Tuned ", /*by rainbowcrypt*/
194 0 program->tsid, program->pnr, program->name, program->net, program->running,
195 program->type, program->p_pid, program->v_pid, program->a_pid, program->t_pid, program->pcr_pid,
196 0 program->audio,
197 0 program->subtitle, /*by rainbowcrypt */
198 program->updated, program->seen
199 ) ;
200
201 0 ▶ list_for_each(item,&program->tuned_freq_list) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(item,&program->tuned_freq_list) { 0 0 0
202 0 finfo = list_entry(item, struct freq_info, next);
203 0 fprintf(stderr, "%d Hz, ", finfo->frequency) ;
204 }
205
206 0 fprintf(stderr, "\n") ;
207 0 }
208  
209  
210 // FREE =====================================================================
211  
212 /* ----------------------------------------------------------------------- */
213 0 void freq_info_free(struct psi_program *program)
214 {
215 struct freq_info *finfo;
216 struct list_head *item,*safe;
217  
218 0 ▶ list_for_each_safe(item,safe,&program->tuned_freq_list) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each_safe(item,safe,&program->tuned_freq_list) { 0 0 0
219 0 finfo = list_entry(item, struct freq_info, next);
220 0 list_del(&finfo->next) ;
221 0 free(finfo) ;
222 }
223  
224 0 }
225  
226  
227 /* ----------------------------------------------------------------------- */
228 0 void program_free(struct psi_program *program)
229 {
230 0 freq_info_free(program) ;
231 0 free(program);
232 0 }
233  
234  
235  
236  
237 /* ----------------------------------------------------------------------- */
238 0 void prog_info_free(struct psi_stream *stream)
239 {
240 struct prog_info *pinfo;
241 struct list_head *item,*safe;
242  
243  
244  
245 0 ▶if (dvb_debug>=20) fprintf(stderr, "!! prog_info_free() !!\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>=20) fprintf(stderr, "!! prog_info_free() !!\n") ; 0 0 0
246  
247 0 ▶ list_for_each_safe(item,safe,&stream->prog_info_list) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each_safe(item,safe,&stream->prog_info_list) { 0 0 0
248 0 pinfo = list_entry(item, struct prog_info, next);
249  
250 0 ▶if (dvb_debug>=20) fprintf(stderr, "!! alloc free - sid=%d [%p] !!\n", pinfo->service_id, pinfo) ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>=20) fprintf(stderr, "!! alloc free - sid=%d [%p] !!\n", pinfo->service_id, pinfo) ; 0 0 0
251
252 0 list_del(&pinfo->next);
253 0 free(pinfo);
254 }
255  
256 0 ▶if (dvb_debug>=20) fprintf(stderr, "!! prog_info_free() - COMPLETE !!\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>=20) fprintf(stderr, "!! prog_info_free() - COMPLETE !!\n") ; 0 0 0
257 0 }
258  
259  
260 /* ----------------------------------------------------------------------- */
261 0 void stream_free(struct psi_stream *stream)
262 {
263 0 ▶ if (stream->freq_list_len && stream->freq_list)
Branch: 2 branches
BranchTrueFalseTotal
if (stream->freq_list_len && stream->freq_list) 0 0 0
if (stream->freq_list_len && stream->freq_list) 0 0 0
264 {
265 0 free(stream->freq_list) ;
266 }
267 0 prog_info_free(stream) ;
268 0 free(stream);
269  
270 0 }
271  
272  
273 /* ----------------------------------------------------------------------- */
274 /* handle psi_ structs */
275  
276  
277 /* ----------------------------------------------------------------------- */
278 0 struct psi_info* psi_info_alloc(void)
279 {
280 struct psi_info *info;
281  
282 0 info = malloc(sizeof(*info));
283 0 memset(info,0,sizeof(*info));
284 0 INIT_LIST_HEAD(&info->streams);
285 0 INIT_LIST_HEAD(&info->programs);
286 0 info->pat_version = PSI_NEW;
287 0 info->sdt_version = PSI_NEW;
288 0 info->nit_version = PSI_NEW;
289 0 return info;
290 }
291  
292 /* ----------------------------------------------------------------------- */
293 0 void psi_info_free(struct psi_info *info)
294 {
295 struct psi_program *program;
296 struct psi_stream *stream;
297 struct list_head *item,*safe;
298  
299 0 ▶ list_for_each_safe(item,safe,&info->streams) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each_safe(item,safe,&info->streams) { 0 0 0
300 0 stream = list_entry(item, struct psi_stream, next);
301 0 list_del(&stream->next);
302 0 stream_free(stream);
303 }
304 0 ▶ list_for_each_safe(item,safe,&info->programs) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each_safe(item,safe,&info->programs) { 0 0 0
305 0 program = list_entry(item, struct psi_program, next);
306 0 list_del(&program->next);
307 0 program_free(program);
308 }
309 0 free(info);
310 0 }
311  
312  
313 /* ----------------------------------------------------------------------- */
314 0 struct prog_info* prog_info_get(struct psi_stream *stream, int sid, int alloc)
315 {
316 struct prog_info *pinfo;
317 struct list_head *item;
318  
319 0 ▶ list_for_each(item,&stream->prog_info_list) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(item,&stream->prog_info_list) { 0 0 0
320 0 pinfo = list_entry(item, struct prog_info, next);
321 0 ▶ if (pinfo->service_id == sid)
Branch: 1 branch
BranchTrueFalseTotal
if (pinfo->service_id == sid) 0 0 0
322 0 return pinfo;
323 }
324 0 ▶ if (!alloc)
Branch: 1 branch
BranchTrueFalseTotal
if (!alloc) 0 0 0
325 0 return NULL;
326
327 0 pinfo = malloc(sizeof(*pinfo));
328 0 memset(pinfo,0,sizeof(*pinfo));
329  
330 0 ▶if (dvb_debug>=20) fprintf(stderr, "!! malloc - sid=%d [%p] !!\n", sid, pinfo) ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>=20) fprintf(stderr, "!! malloc - sid=%d [%p] !!\n", sid, pinfo) ; 0 0 0
331
332 0 pinfo->service_id = sid;
333  
334 // flag unset
335 0 pinfo->service_type = -1 ;
336 0 pinfo->visible = -1 ;
337 0 pinfo->lcn = -1 ;
338  
339 0 list_add_tail(&pinfo->next,&stream->prog_info_list);
340 0 return pinfo;
341 }
342  
343  
344 /* ----------------------------------------------------------------------- */
345 0 struct psi_stream* psi_stream_get(struct psi_info *info, int tsid, int netid, int alloc)
346 {
347 struct psi_stream *stream;
348 struct list_head *item;
349  
350 0 ▶if (dvb_debug >= 15) fprintf_timestamp(stderr, "psi_stream_get(tsid=%d, netid=%d [alloc=%d])\n", tsid, netid, alloc) ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) fprintf_timestamp(stderr, "psi_stream_get(tsid=%d, netid=%d [alloc=%d])\n", tsid, netid, alloc) ; 0 0 0
351  
352 0 ▶ list_for_each(item,&info->streams) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(item,&info->streams) { 0 0 0
353 0 stream = list_entry(item, struct psi_stream, next);
354 0 ▶ if (stream->tsid != tsid)
Branch: 1 branch
BranchTrueFalseTotal
if (stream->tsid != tsid) 0 0 0
355 0 continue ;
356 0 ▶ if (stream->netid != netid)
Branch: 1 branch
BranchTrueFalseTotal
if (stream->netid != netid) 0 0 0
357 0 continue ;
358
359 0 return stream;
360 }
361 0 ▶ if (!alloc)
Branch: 1 branch
BranchTrueFalseTotal
if (!alloc) 0 0 0
362 0 return NULL;
363  
364 0 ▶if (dvb_debug >= 15) fprintf_timestamp(stderr, "## New stream\n", tsid, netid, alloc) ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) fprintf_timestamp(stderr, "## New stream\n", tsid, netid, alloc) ; 0 0 0
365  
366 0 stream = malloc(sizeof(*stream));
367 0 memset(stream,0,sizeof(*stream));
368  
369 // int tsid;
370 //
371 // /* network */
372 // int netid;
373 // char net[PSI_STR_MAX];
374 //
375 // int frequency;
376 // int symbol_rate;
377 // char *bandwidth;
378 // char *constellation;
379 // char *hierarchy;
380 // char *code_rate_hp;
381 // char *code_rate_lp;
382 // char *fec_inner;
383 // char *guard;
384 // char *transmission;
385 // char *polarization;
386 // int other_freq;
387 // int freq_list_len;
388 // int *freq_list;
389 //
390 // /* status info */
391 // int updated;
392 // int tuned;
393 //
394 // /* signal quality measure */
395 // unsigned ber ;
396 // unsigned snr ;
397 // unsigned strength ;
398 // unsigned uncorrected_blocks ;
399 //
400 // /* program info */
401 // struct list_head prog_info_list;
402
403  
404 0 INIT_LIST_HEAD(&stream->prog_info_list);
405 0 stream->tsid = tsid;
406 0 stream->netid = netid ;
407 0 stream->updated = 1;
408 0 stream->tuned = 0;
409  
410 // flag unset
411 0 stream->frequency = -1 ;
412 0 stream->symbol_rate = -1 ;
413  
414 0 list_add_tail(&stream->next,&info->streams);
415 0 return stream;
416 }
417  
418 /* ----------------------------------------------------------------------- */
419 // Copy an existing stream but set a new frequency. Then add to the list
420 0 struct psi_stream* psi_stream_newfreq(struct psi_info *info, struct psi_stream* src_stream, int frequency)
421 {
422 struct psi_stream *stream;
423 struct prog_info *pinfo, *src_pinfo;
424 struct list_head *item;
425  
426  
427 0 stream = malloc(sizeof(*stream));
428 0 memset(stream,0,sizeof(*stream));
429
430 0 INIT_LIST_HEAD(&stream->prog_info_list);
431  
432 0 stream->frequency = frequency;
433  
434 0 stream->tsid = src_stream->tsid;
435 0 stream->netid = src_stream->netid;
436 0 strcpy(stream->net, src_stream->net);
437  
438 0 ▶if (dvb_debug >= 15) fprintf_timestamp(stderr, "psi_stream_newfreq(tsid=%d, netid=%d : freq=%d)\n", stream->tsid, stream->netid, frequency) ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) fprintf_timestamp(stderr, "psi_stream_newfreq(tsid=%d, netid=%d : freq=%d)\n", stream->tsid, stream->netid, frequency) ; 0 0 0
439  
440 0 stream->symbol_rate = src_stream->symbol_rate;
441  
442 0 stream->bandwidth = src_stream->bandwidth ;
443 0 stream->constellation = src_stream->constellation ;
444 0 stream->hierarchy = src_stream->hierarchy ;
445 0 stream->code_rate_hp = src_stream->code_rate_hp ;
446 0 stream->code_rate_lp = src_stream->code_rate_lp ;
447 0 stream->guard = src_stream->guard ;
448 0 stream->transmission = src_stream->transmission ;
449 0 stream->other_freq = src_stream->other_freq ;
450 0 stream->freq_list_len = 0 ;
451 0 stream->freq_list = NULL ;
452  
453 0 stream->updated = 0;
454 0 stream->tuned = 0;
455
456 // copy program information from source stream
457 0 ▶ list_for_each(item,&src_stream->prog_info_list) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(item,&src_stream->prog_info_list) { 0 0 0
458 0 src_pinfo = list_entry(item, struct prog_info, next);
459  
460 // copy
461 0 pinfo = prog_info_get(stream, src_pinfo->service_id, /* int alloc */ 1) ;
462 0 pinfo->service_type = src_pinfo->service_type ;
463 0 pinfo->visible = src_pinfo->visible ;
464 0 pinfo->lcn = src_pinfo->lcn ;
465 }
466
467  
468 0 list_add_tail(&stream->next,&info->streams);
469 0 return stream;
470 }
471  
472  
473 /* ----------------------------------------------------------------------- */
474 0 struct freq_info* freq_info_get(struct psi_program *program, int freq)
475 {
476 struct freq_info *finfo;
477 struct list_head *item;
478  
479 0 ▶ list_for_each(item,&program->tuned_freq_list) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(item,&program->tuned_freq_list) { 0 0 0
480 0 finfo = list_entry(item, struct freq_info, next);
481 0 ▶ if (finfo->frequency == freq)
Branch: 1 branch
BranchTrueFalseTotal
if (finfo->frequency == freq) 0 0 0
482 0 return finfo;
483 }
484
485 0 finfo = malloc(sizeof(*finfo));
486 0 memset(finfo,0,sizeof(*finfo));
487  
488 0 finfo->frequency = freq;
489  
490 0 list_add_tail(&finfo->next,&program->tuned_freq_list);
491 0 return finfo;
492 }
493  
494 /* ----------------------------------------------------------------------- */
495 0 struct psi_program* psi_program_get(struct psi_info *info, int tsid,
496 int pnr, int tuned_freq, int alloc)
497 {
498 struct psi_program *program;
499 struct list_head *item;
500  
501 0 ▶if (dvb_debug >= 15) fprintf(stderr, "<get prog(tsid=%d, pnr=%d, freq=%d, alloc=%d)>\n", tsid, pnr, tuned_freq, alloc) ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) fprintf(stderr, "<get prog(tsid=%d, pnr=%d, freq=%d, alloc=%d)>\n", tsid, pnr, tuned_freq, alloc) ; 0 0 0
502 0 ▶ list_for_each(item,&info->programs) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(item,&info->programs) { 0 0 0
503 0 program = list_entry(item, struct psi_program, next);
504 0 ▶ if (program->tsid == tsid &&
Branch: 1 branch
BranchTrueFalseTotal
if (program->tsid == tsid && 0 0 0
505 0 ▶ program->pnr == pnr)
Branch: 1 branch
BranchTrueFalseTotal
program->pnr == pnr) 0 0 0
506 {
507 0 ▶if (dvb_debug >= 15) fprintf(stderr, "<< found prog - set freq>>\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) fprintf(stderr, "<< found prog - set freq>>\n") ; 0 0 0
508 0 ▶ if (alloc) freq_info_get(program, tuned_freq) ;
Branch: 1 branch
BranchTrueFalseTotal
if (alloc) freq_info_get(program, tuned_freq) ; 0 0 0
509 0 ▶if (dvb_debug >= 15) fprintf(stderr, "<< return prog >>\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) fprintf(stderr, "<< return prog >>\n") ; 0 0 0
510 0 ▶if (dvb_debug >= 15) print_program(program) ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) print_program(program) ; 0 0 0
511 0 return program;
512 }
513 }
514  
515 0 ▶ if (!alloc)
Branch: 1 branch
BranchTrueFalseTotal
if (!alloc) 0 0 0
516 0 return NULL;
517  
518 0 ▶if (dvb_debug >= 15) fprintf(stderr, "<< create prog (size=%d) >>\n", sizeof(*program)) ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) fprintf(stderr, "<< create prog (size=%d) >>\n", sizeof(*program)) ; 0 0 0
519  
520 0 program = malloc(sizeof(*program));
521 0 memset(program,0,sizeof(*program));
522  
523 0 INIT_LIST_HEAD(&program->tuned_freq_list);
524  
525 0 program->tsid = tsid;
526 0 program->pnr = pnr;
527 0 program->version = PSI_NEW;
528 0 program->updated = 1;
529
530 0 ▶if (dvb_debug >= 15) fprintf(stderr, "<< set freq>>\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) fprintf(stderr, "<< set freq>>\n") ; 0 0 0
531
532 // set frequency
533 0 freq_info_get(program, tuned_freq) ;
534  
535 0 ▶if (dvb_debug >= 15) fprintf(stderr, "<< set freq done >>\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) fprintf(stderr, "<< set freq done >>\n") ; 0 0 0
536  
537 // int tsid; X
538 // int pnr; X
539 // int version; X
540 // int running;
541 // int ca;
542 //
543 // /* program data */
544 // int type;
545 // int p_pid; // program
546 // int v_pid; // video
547 // int a_pid; // audio
548 // int t_pid; // teletext
549 // char audio[PSI_STR_MAX]; 0
550 // char net[PSI_STR_MAX]; 0
551 // char name[PSI_STR_MAX]; 0
552 //
553 // /* status info */
554 // int updated; X
555 // int seen;
556 //
557 // /* hmm ... */
558 // int fd;
559  
560 // flag unset
561 0 program->running = -1;
562 0 program->ca = -1;
563 0 program->type = -1;
564 0 program->p_pid = -1;
565 0 program->v_pid = -1;
566 0 program->a_pid = -1;
567 0 program->t_pid = -1;
568 0 program->s_pid = -1;
569  
570 0 list_add_tail(&program->next,&info->programs);
571  
572 0 ▶ if (dvb_debug>=2) fprintf(stderr, "## Add program: tsid=%d pnr=%d freq=%d\n", tsid, pnr, tuned_freq) ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>=2) fprintf(stderr, "## Add program: tsid=%d pnr=%d freq=%d\n", tsid, pnr, tuned_freq) ; 0 0 0
573  
574 0 ▶if (dvb_debug >= 15) fprintf(stderr, "<< return prog >>\n") ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) fprintf(stderr, "<< return prog >>\n") ; 0 0 0
575 0 ▶if (dvb_debug >= 15) print_program(program) ;
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 15) print_program(program) ; 0 0 0
576  
577 0 return program;
578 }
579  
580 /* ----------------------------------------------------------------------- */
581 /* bit fiddeling */
582  
583 0 unsigned int mpeg_getbits(unsigned char *buf, int start, int count)
584 {
585 0 unsigned int result = 0;
586 unsigned char bit;
587  
588 0 ▶ while (count) {
Branch: 1 branch
BranchTrueFalseTotal
while (count) { 0 0 0
589 0 result <<= 1;
590 0 bit = 1 << (7 - (start % 8));
591 0 ▶ result |= (buf[start/8] & bit) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
result |= (buf[start/8] & bit) ? 1 : 0; 0 0 0
592 0 start++;
593 0 count--;
594 }
595 0 return result;
596 }
597  
598 /* ----------------------------------------------------------------------- */
599 0 void hexdump(char *prefix, unsigned char *data, size_t size)
600 {
601 char ascii[17];
602 int i;
603  
604 0 ▶ for (i = 0; i < size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < size; i++) { 0 0 0
605 0 ▶ if (0 == (i%16)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == (i%16)) { 0 0 0
606 0 ▶ fprintf(stderr,"%s%s%04x:",
Branch: 2 branches
BranchTrueFalseTotal
fprintf(stderr,"%s%s%04x:", 0 0 0
fprintf(stderr,"%s%s%04x:", 0 0 0
607 prefix ? prefix : "",
608 prefix ? ": " : "",
609 i);
610 0 memset(ascii,0,sizeof(ascii));
611 }
612 0 ▶ if (0 == (i%4))
Branch: 1 branch
BranchTrueFalseTotal
if (0 == (i%4)) 0 0 0
613 0 fprintf(stderr," ");
614 0 fprintf(stderr," %02x",data[i]);
615 0 ▶ ascii[i%16] = isprint(data[i]) ? data[i] : '.';
Branch: 1 branch
BranchTrueFalseTotal
ascii[i%16] = isprint(data[i]) ? data[i] : '.'; 0 0 0
616 0 ▶ if (15 == (i%16))
Branch: 1 branch
BranchTrueFalseTotal
if (15 == (i%16)) 0 0 0
617 0 fprintf(stderr," %s\n",ascii);
618 }
619 0 ▶ if (0 != (i%16)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 != (i%16)) { 0 0 0
620 0 ▶ while (0 != (i%16)) {
Branch: 1 branch
BranchTrueFalseTotal
while (0 != (i%16)) { 0 0 0
621 0 ▶ if (0 == (i%4))
Branch: 1 branch
BranchTrueFalseTotal
if (0 == (i%4)) 0 0 0
622 0 fprintf(stderr," ");
623 0 fprintf(stderr," ");
624 0 i++;
625 };
626 0 fprintf(stderr," %s\n",ascii);
627 }
628 0 }
629  
630  
631 /* ----------------------------------------------------------------------- */
632 0 static void add_audio_pid(struct psi_program *program, int audio_pid, const char *lang)
633 {
634 0 ▶ if (!program->a_pid)
Branch: 1 branch
BranchTrueFalseTotal
if (!program->a_pid) 0 0 0
635 0 program->a_pid = audio_pid;
636  
637 0 int slen = strlen(program->audio);
638 0 ▶ snprintf(program->audio + slen, sizeof(program->audio) - slen,
Branch: 2 branches
BranchTrueFalseTotal
snprintf(program->audio + slen, sizeof(program->audio) - slen, 0 0 0
snprintf(program->audio + slen, sizeof(program->audio) - slen, 0 0 0
639 "%s%.3s:%d", slen ? " " : "", lang ? lang : "xxx", audio_pid);
640 0 }
641  
642 /* ----------------------------------------------------------------------- */
643 0 static char* get_lang_tag(unsigned char *desc, int dlen)
644 {
645 int i,t,l;
646  
647 0 ▶ for (i = 0; i < dlen; i += desc[i+1] +2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < dlen; i += desc[i+1] +2) { 0 0 0
648 0 t = desc[i];
649 0 l = desc[i+1];
650  
651 0 ▶ if (DESC_ISO639_LANG == t)
Branch: 1 branch
BranchTrueFalseTotal
if (DESC_ISO639_LANG == t) 0 0 0
652 0 return (char*) desc+i+2;
653 }
654 0 return NULL;
655 }
656  
657  
658  
659 /* ----------------------------------------------------------------------- */
660 /* transport streams */
661  
662 0 static void parse_pmt_desc(unsigned char *desc, int dlen,
663 struct psi_program *program, int pid)
664 {
665 int i,t,l,slen;
666 0 char lang[4] = "" ;
667 0 int audio_pid = -1 ;
668  
669 0 ▶ for (i = 0; i < dlen; i += desc[i+1] +2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < dlen; i += desc[i+1] +2) { 0 0 0
670 0 t = desc[i];
671 0 l = desc[i+1];
672  
673 0 ▶ if (dvb_debug>5)
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>5) 0 0 0
674 0 fprintf(stderr," parse_pmt_desc() pid=%d t=0x%02x\n",pid,t);
675  
676 0 switch (t) {
677 0 case DESC_ISO639_LANG:
678 {
679 0 char *langp = get_lang_tag(desc, dlen) ;
680 0 strncpy(lang, langp, sizeof(lang)) ;
681 0 ▶ if (dvb_debug>5) {
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>5) { 0 0 0
682 0 fprintf_timestamp(stderr, " + lang=%s\n", lang) ;
683 }
684 }
685 0 break;
686  
687 0 case DESC_AC3:
688 case DESC_ENHANCED_AC3:
689 0 audio_pid = pid ;
690 0 ▶ if (dvb_debug>5) {
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>5) { 0 0 0
691 0 fprintf_timestamp(stderr, " + AC3 pid=%d\n", pid) ;
692 }
693 0 break;
694  
695 0 case DESC_TELETEXT:
696 0 ▶ if (!program->t_pid)
Branch: 1 branch
BranchTrueFalseTotal
if (!program->t_pid) 0 0 0
697 0 program->t_pid = pid;
698  
699 0 ▶ if (dvb_debug>5) {
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>5) { 0 0 0
700 0 fprintf_timestamp(stderr, " + t_pid=%d\n", program->t_pid) ;
701 }
702 0 break;
703  
704 0 case DESC_SUBTITLING: /* subtitles (pmt) */
705 0 ▶ if (!program->s_pid)
Branch: 1 branch
BranchTrueFalseTotal
if (!program->s_pid) 0 0 0
706 0 program->s_pid = pid;
707 0 slen = strlen(program->subtitle); /*by rainbowcrypt*/
708 0 snprintf(program->subtitle + slen, sizeof(program->subtitle) - slen,
709 0 ▶ "%s%.3s:%d",slen ? " " : "", desc+i+2,pid);/*by rainbowcrypt*/
Branch: 1 branch
BranchTrueFalseTotal
"%s%.3s:%d",slen ? " " : "", desc+i+2,pid);/*by rainbowcrypt*/ 0 0 0
710 0 ▶ if (dvb_debug>5)
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>5) 0 0 0
711 0 fprintf(stderr," subtitles=%3.3s\n",desc+i+2);
712 0 break;
713 }
714 }
715  
716 // Check to see if we got an audio pid as part of this section
717 0 ▶ if (audio_pid >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (audio_pid >= 0) 0 0 0
718 {
719 // add the information to the audio list (and set the audio pid if not already set)
720 0 add_audio_pid(program, audio_pid, lang) ;
721 }
722 0 }
723  
724 /* ----------------------------------------------------------------------- */
725 0 static void dump_data(unsigned char *data, int len)
726 {
727 int i;
728  
729 0 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 0 0 0
730 {
731 // if (isprint(data[i]))
732 // fprintf(stderr,"%c", data[i]);
733 // else
734 // fprintf(stderr,"\\x%02x", (int)data[i]);
735 0 fprintf(stderr,"0x%02x ", (int)data[i]);
736 }
737 0 }
738  
739 /* ----------------------------------------------------------------------- */
740 0 void mpeg_dump_desc(unsigned char *desc, int dlen)
741 {
742 int i,j,t,l,l2,l3;
743  
744 0 ▶ for (i = 0; i < dlen; i += desc[i+1] +2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < dlen; i += desc[i+1] +2) { 0 0 0
745 0 t = desc[i];
746 0 l = desc[i+1];
747  
748 0 switch (t) {
749 0 case 0x0a: /* ??? (pmt) */
750 0 fprintf(stderr," lang=%3.3s",desc+i+2);
751 0 break;
752 0 case 0x45: /* vbi data (pmt) */
753 0 fprintf(stderr," vbidata=");
754 0 dump_data(desc+i+2,l);
755 0 break;
756 0 case 0x52: /* stream identifier */
757 0 fprintf(stderr," sid=%d",(int)desc[i+2]);
758 0 break;
759 0 case 0x56: /* teletext (pmt) */
760 0 fprintf(stderr," teletext=%3.3s",desc+i+2);
761 0 break;
762 0 case 0x59: /* subtitles (pmt) */
763 0 fprintf(stderr," subtitles=%3.3s",desc+i+2);
764 0 break;
765 0 case 0x6a: /* ac3 (pmt) */
766 0 fprintf(stderr," ac3");
767 0 break;
768  
769 0 case 0x40: /* network name (nit) */
770 0 fprintf(stderr," name=");
771 0 dump_data(desc+i+2,l);
772 0 break;
773 0 case 0x43: /* satellite delivery system (nit) */
774 0 fprintf(stderr," dvb-s");
775 0 break;
776 0 case 0x44: /* cable delivery system (nit) */
777 0 fprintf(stderr," dvb-c");
778 0 break;
779 0 case 0x5a: /* terrestrial delivery system (nit) */
780 0 fprintf(stderr," dvb-t");
781 0 break;
782  
783 0 case 0x48: /* service (sdt) */
784 0 fprintf(stderr," service=%d,",desc[i+2]);
785 0 l2 = desc[i+3];
786 0 dump_data(desc+i+4,desc[i+3]);
787 0 fprintf(stderr,",");
788 0 dump_data(desc+i+l2+5,desc[i+l2+4]);
789 0 break;
790  
791 0 case 0x4d: /* event (eid) */
792 0 fprintf(stderr," short=[%3.3s|",desc+i+2);
793 0 l2 = desc[i+5];
794 0 l3 = desc[i+6+l2];
795 0 dump_data(desc+i+6,l2);
796 0 fprintf(stderr,"|");
797 0 dump_data(desc+i+7+l2,l3);
798 0 fprintf(stderr,"]");
799 0 break;
800 0 case 0x4e: /* event (eid) */
801 0 fprintf(stderr," *ext event");
802 0 break;
803 0 case 0x4f: /* event (eid) */
804 0 fprintf(stderr," *time shift event");
805 0 break;
806 0 case 0x50: /* event (eid) */
807 0 fprintf(stderr," *component");
808 0 break;
809 0 case 0x54: /* event (eid) */
810 0 fprintf(stderr," content=");
811 0 ▶ for (j = 0; j < l; j+=2)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < l; j+=2) 0 0 0
812 0 ▶ fprintf(stderr,"%s0x%02x", j ? "," : "", desc[i+j+2]);
Branch: 1 branch
BranchTrueFalseTotal
fprintf(stderr,"%s0x%02x", j ? "," : "", desc[i+j+2]); 0 0 0
813 0 break;
814 0 case 0x55: /* event (eid) */
815 0 fprintf(stderr," *parental rating");
816 0 break;
817  
818 0 default:
819 0 fprintf(stderr," 0x%02x[",desc[i]);
820 0 dump_data(desc+i+2,l);
821 0 fprintf(stderr,"]");
822 }
823 }
824 0 }
825  
826 /* ----------------------------------------------------------------------- */
827 0 int mpeg_parse_psi_pat(struct psi_info *info, unsigned char *data, int verbose, int tuned_freq)
828 {
829 struct list_head *item;
830 struct psi_program *pr;
831 int tsid,pnr,version,current;
832 int j,len,pid;
833  
834 0 len = mpeg_getbits(data,12,12) + 3 - 4;
835 0 tsid = mpeg_getbits(data,24,16);
836 0 version = mpeg_getbits(data,42,5);
837 0 current = mpeg_getbits(data,47,1);
838 0 ▶ if (!current)
Branch: 1 branch
BranchTrueFalseTotal
if (!current) 0 0 0
839 0 return len+4;
840 0 ▶ if (info->tsid == tsid && info->pat_version == version)
Branch: 2 branches
BranchTrueFalseTotal
if (info->tsid == tsid && info->pat_version == version) 0 0 0
if (info->tsid == tsid && info->pat_version == version) 0 0 0
841 0 return len+4;
842 0 info->tsid = tsid;
843 0 info->pat_version = version;
844 0 info->pat_updated = 1;
845  
846 0 ▶ if (verbose>1)
Branch: 1 branch
BranchTrueFalseTotal
if (verbose>1) 0 0 0
847 0 fprintf_timestamp(stderr, "ts [pat]: tsid %d ver %2d [%d/%d]\n",
848 tsid, version,
849 mpeg_getbits(data,48, 8),
850 mpeg_getbits(data,56, 8));
851
852 0 ▶ for (j = 64; j < len*8; j += 32) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 64; j < len*8; j += 32) { 0 0 0
853 0 pnr = mpeg_getbits(data,j+0,16);
854 0 pid = mpeg_getbits(data,j+19,13);
855 0 ▶ if (0 == pnr) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == pnr) { 0 0 0
856 /* network */
857 0 ▶ if (verbose > 2)
Branch: 1 branch
BranchTrueFalseTotal
if (verbose > 2) 0 0 0
858 0 fprintf(stderr," pid 0x%04x [network]\n",
859 pid);
860 } else {
861 /* program */
862 0 pr = psi_program_get(info, tsid, pnr, tuned_freq, 1);
863 0 pr->p_pid = pid;
864 0 pr->updated = 1;
865 0 pr->seen = 1;
866 0 ▶ if (NULL == info->pr)
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == info->pr) 0 0 0
867 0 info->pr = pr;
868 }
869 }
870  
871 0 ▶ if (verbose > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (verbose > 2) { 0 0 0
872 0 ▶ list_for_each(item,&info->programs) {
Branch: 1 branch
BranchTrueFalseTotal
list_for_each(item,&info->programs) { 0 0 0
873 0 pr = list_entry(item, struct psi_program, next);
874 0 ▶ if (pr->tsid != tsid)
Branch: 1 branch
BranchTrueFalseTotal
if (pr->tsid != tsid) 0 0 0
875 0 continue;
876 0 fprintf(stderr," pid 0x%04x => pnr %2d [program map%s]\n",
877 pr->p_pid, pr->pnr,
878 0 ▶ pr->seen ? ",seen" : "");
Branch: 1 branch
BranchTrueFalseTotal
pr->seen ? ",seen" : ""); 0 0 0
879 }
880 0 fprintf(stderr,"\n");
881 }
882 0 return len+4;
883 }
884  
885 /* ----------------------------------------------------------------------- */
886 0 int mpeg_parse_psi_pmt(struct psi_program *program, unsigned char *data, int verbose, int tuned_freq)
887 {
888 int pnr,version,current;
889 int j,len,dlen,type,pid,slen,pcr_pid;
890 char *lang;
891  
892 0 len = mpeg_getbits(data,12,12) + 3 - 4;
893 0 pnr = mpeg_getbits(data,24,16);
894 0 version = mpeg_getbits(data,42,5);
895 0 current = mpeg_getbits(data,47,1);
896 0 pcr_pid = mpeg_getbits(data,67,13);
897 0 ▶ if (!current)
Branch: 1 branch
BranchTrueFalseTotal
if (!current) 0 0 0
898 0 return len+4;
899 0 ▶ if (program->pnr == pnr && program->version == version)
Branch: 2 branches
BranchTrueFalseTotal
if (program->pnr == pnr && program->version == version) 0 0 0
if (program->pnr == pnr && program->version == version) 0 0 0
900 0 return len+4;
901 0 program->version = version;
902 0 program->updated = 1;
903 0 program->pcr_pid = pcr_pid ;
904  
905 0 dlen = mpeg_getbits(data,84,12);
906 /* TODO: decode descriptor? */
907 0 ▶ if (verbose>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (verbose>1) { 0 0 0
908 0 fprintf_timestamp(stderr,
909 "ts [pmt]: pnr %d ver %2d [%d/%d] pcr 0x%04x "
910 "pid 0x%04x type %2d #",
911 pnr, version,
912 mpeg_getbits(data,48, 8),
913 mpeg_getbits(data,56, 8),
914 pcr_pid,
915 program->p_pid, program->type);
916 0 mpeg_dump_desc(data + 96/8, dlen);
917 0 fprintf(stderr,"\n");
918 }
919 0 j = 96 + dlen*8;
920 0 program->v_pid = 0;
921 0 program->a_pid = 0;
922 0 program->t_pid = 0;
923 0 program->s_pid = 0;
924 0 memset(program->audio,0,sizeof(program->audio));
925 0 ▶ while (j < len*8) {
Branch: 1 branch
BranchTrueFalseTotal
while (j < len*8) { 0 0 0
926 0 type = mpeg_getbits(data,j,8);
927 0 pid = mpeg_getbits(data,j+11,13);
928 0 dlen = mpeg_getbits(data,j+28,12);
929  
930 0 ▶ if (dvb_debug>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug>1) { 0 0 0
931 0 fprintf_timestamp(stderr, " + type=%2d (0x%02x) pid=%d (0x%04x)\n",
932 type, type, pid, pid) ;
933 }
934  
935 0 switch (type) {
936  
937 /* video */
938 0 case MPEG1Video:
939 case MPEG2Video:
940 case MPEG4Video:
941 case H264Video:
942  
943 0 ▶ if (!program->v_pid)
Branch: 1 branch
BranchTrueFalseTotal
if (!program->v_pid) 0 0 0
944 0 program->v_pid = pid;
945 0 break;
946  
947  
948 /* audio */
949 0 case MPEG1Audio:
950 case MPEG2Audio:
951 case MPEG2AudioAmd1:
952 case AACAudio:
953 0 lang = get_lang_tag(data + (j+40)/8, dlen);
954 0 add_audio_pid(program, pid, lang) ;
955 0 break;
956  
957 /* private data */
958 0 case PrivSec:
959 case PrivData:
960  
961 0 parse_pmt_desc(data + (j+40)/8, dlen, program, pid);
962 0 break;
963 }
964
965 0 ▶ if (dvb_debug >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (dvb_debug >= 2) 0 0 0
966 {
967 0 fprintf(stderr, " PROG: tsid=%d pnr=%d video=%d audio=%d text=%d sub=%d (freq=%d)\n",
968 program->tsid, program->pnr,
969 program->v_pid, program->a_pid, program->t_pid, program->s_pid,
970 tuned_freq);
971 }
972 0 ▶ if ( (verbose > 2) || (dvb_debug >= 3) )
Branch: 2 branches
BranchTrueFalseTotal
if ( (verbose > 2) || (dvb_debug >= 3) ) 0 0 0
if ( (verbose > 2) || (dvb_debug >= 3) ) 0 0 0
973 {
974 0 fprintf(stderr, " pid 0x%04x (video=%d audio=%d) => %-32s #",
975 pid,
976 program->v_pid, program->a_pid,
977 stream_type_s[type]);
978 0 mpeg_dump_desc(data + (j+40)/8, dlen);
979 0 fprintf(stderr,"\n");
980 }
981  
982 0 j += 40 + dlen*8;
983 }
984  
985 0 ▶ if (verbose > 2)
Branch: 1 branch
BranchTrueFalseTotal
if (verbose > 2) 0 0 0
986 0 fprintf(stderr,"\n");
987  
988 0 return len+4;
989 }
990  
« clib/libng/parse-dvb.c ↑ Summary clib/mpeg2audio_stubs/mpegaudio_stubs.c »