OSDN Git Service

TS分離用テストコードの削除など
[tsparser/tsparser.git] / src / tsparser.cpp
1
2 #include <inttypes.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <time.h>
10 #include <map>
11 #include <set>
12 #include "TS/ts_packet.h"
13 #include "TS/psi.h"
14 #include "ARIB/si.h"
15 #include "aribstr.h"
16
17 using namespace TS;
18 using namespace TS::PSI;
19 using namespace TS::PSI::ARIB;
20 using namespace TS::Description;
21 using namespace TS::Description::ARIB;
22
23 typedef std::map< uint16_t, Section*>   SECTIONS;
24
25
26 void printPAT( Section *sec)
27 {
28         PAT *pat = (PAT *)sec;
29
30         fprintf( stdout, "\n[ PAT ]\n");
31         fprintf( stdout, "table id                           = 0x%02X\n", pat->table_id);
32         fprintf( stdout, "section syntax indicator           = %5u\n", pat->section_syntax_indicator);
33         fprintf( stdout, "section length                     = %5u\n", pat->section_length);
34         fprintf( stdout, "transport stream id                = %5u\n", pat->psi_id.transport_stream_id);
35         fprintf( stdout, "version number                     = %5u\n", pat->version_number);
36         fprintf( stdout, "current next indicator             = %5u\n", pat->current_next_indicator);
37         fprintf( stdout, "section number                     = %5u\n", pat->section_number);
38         fprintf( stdout, "last section number                = %5u\n", pat->last_section_number);
39         fprintf( stdout, "network PID                        = %5u\n", pat->network_PID);
40
41         PAT::PROGRAM_MAP::iterator it;
42         for( it = pat->program_map_PID.begin(); it != pat->program_map_PID.end(); it++) {
43                 fprintf( stdout, "  program number = 0x%04X, program map PID = 0x%04X\n", it->first, it->second);
44         }
45 }
46
47 void printCAT( Section *sec)
48 {
49         CAT *cat = (CAT *)sec;
50
51         fprintf( stdout, "\n[ CAT ]\n");
52         fprintf( stdout, "table id                           = 0x%02X\n", cat->table_id);
53         fprintf( stdout, "section syntax indicator           = %5u\n", cat->section_syntax_indicator);
54         fprintf( stdout, "section length                     = %5u\n", cat->section_length);
55         fprintf( stdout, "version number                     = %5u\n", cat->version_number);
56         fprintf( stdout, "current next indicator             = %5u\n", cat->current_next_indicator);
57         fprintf( stdout, "section number                     = %5u\n", cat->section_number);
58         fprintf( stdout, "last section number                = %5u\n", cat->last_section_number);
59
60         CAT::DESCRIPTORS::iterator d;
61         for( d = cat->descriptors.begin(); d != cat->descriptors.end(); d++) {
62                 fprintf( stdout, "  descriptor_tag                   = 0x%02X\n", (*d)->descriptor_tag);
63                 fprintf( stdout, "  descriptor_length                = %5u\n", (*d)->descriptor_length);
64         }
65 }
66
67 void printTSDT( Section *sec)
68 {
69         TSDT *tsdt = (TSDT *)sec;
70
71         fprintf( stdout, "\n[ TSDT ]\n");
72         fprintf( stdout, "table id                           = 0x%02X\n", tsdt->table_id);
73         fprintf( stdout, "section syntax indicator           = %5u\n", tsdt->section_syntax_indicator);
74         fprintf( stdout, "section length                     = %5u\n", tsdt->section_length);
75         fprintf( stdout, "version number                     = %5u\n", tsdt->version_number);
76         fprintf( stdout, "current next indicator             = %5u\n", tsdt->current_next_indicator);
77         fprintf( stdout, "section number                     = %5u\n", tsdt->section_number);
78         fprintf( stdout, "last section number                = %5u\n", tsdt->last_section_number);
79
80         TSDT::DESCRIPTORS::iterator d;
81         for( d = tsdt->descriptors.begin(); d != tsdt->descriptors.end(); d++) {
82                 fprintf( stdout, "  descriptor_tag                   = 0x%02X\n", (*d)->descriptor_tag);
83                 fprintf( stdout, "  descriptor_length                = %5u\n", (*d)->descriptor_length);
84         }
85 }
86
87 void printPMT( Section *sec)
88 {
89         PMT *pmt = (PMT *)sec;
90         PMT::DESCRIPTORS::iterator d;
91
92         fprintf( stdout, "\n[ PMT ]\n");
93         fprintf( stdout, "table id                           = 0x%02X\n", pmt->table_id);
94         fprintf( stdout, "section syntax indicator           = %5u\n", pmt->section_syntax_indicator);
95         fprintf( stdout, "section length                     = %5u\n", pmt->section_length);
96         fprintf( stdout, "program number                     = %5u\n", pmt->psi_id.program_number);
97         fprintf( stdout, "version number                     = %5u\n", pmt->version_number);
98         fprintf( stdout, "current next indicator             = %5u\n", pmt->current_next_indicator);
99         fprintf( stdout, "section number                     = %5u\n", pmt->section_number);
100         fprintf( stdout, "last section number                = %5u\n", pmt->last_section_number);
101         fprintf( stdout, "PCR_PID                            = 0x%04X\n", pmt->PCR_PID);
102         fprintf( stdout, "program_info_length                = %5u\n", pmt->program_info_length);
103
104         for( d = pmt->program_info_descriptors.begin(); d != pmt->program_info_descriptors.end(); d++) {
105                 fprintf( stdout, "descriptor_tag                     = 0x%02X\n", (*d)->descriptor_tag);
106                 fprintf( stdout, "descriptor_length                  = %5u\n", (*d)->descriptor_length);
107
108                 if( (*d)->descriptor_tag == ConditionalAccess::TAG) {
109                         ConditionalAccess *ca = (ConditionalAccess *)*d;
110                         fprintf( stdout, "CA_system_ID                       = %5u\n", ca->CA_system_ID);
111                         fprintf( stdout, "CA_PID                             = 0x%04X\n", ca->CA_PID);
112                 }
113         }
114
115         PMT::ELEMENTS::iterator e;
116         for( e = pmt->elements.begin(); e != pmt->elements.end(); e++) {
117                 fprintf( stdout, "  stream_type                      = 0x%02X\n", e->stream_type);
118                 fprintf( stdout, "  elementary_PID                   = 0x%04X\n", e->elementary_PID);
119                 fprintf( stdout, "  ES_info_length                   = %5u\n", e->ES_info_length);
120
121                 PMT::DESCRIPTORS::iterator d;
122                 for( d = e->descriptors.begin(); d != e->descriptors.end(); d++) {
123                         fprintf( stdout, "    descriptor_tag                 = 0x%02X\n", (*d)->descriptor_tag);
124                         fprintf( stdout, "    descriptor_length              = %5u\n", (*d)->descriptor_length);        
125
126                         if( (*d)->descriptor_tag == ConditionalAccess::TAG) {
127                                 ConditionalAccess *ca = (ConditionalAccess *)*d;
128                                 fprintf( stdout, "    CA_system_ID                   = %5u\n", ca->CA_system_ID);
129                                 fprintf( stdout, "    CA_PID                         = 0x%04X\n", ca->CA_PID);
130                         }
131                 }
132         }
133 }
134         
135
136 void printEIT( Section *sec)
137 {
138         EIT *eit = (EIT *)sec;
139
140         fprintf( stdout, "\n[ EIT ]\n");
141         fprintf( stdout, "table id                           = 0x%02X\n", eit->table_id);
142         fprintf( stdout, "section syntax indicator           = %5u\n", eit->section_syntax_indicator);
143         fprintf( stdout, "private indicator                  = %5u\n", eit->private_indicator);
144         fprintf( stdout, "section length                     = %5u\n", eit->section_length);
145         fprintf( stdout, "service_id                         = %5u\n", eit->si_id.service_id);
146         fprintf( stdout, "version number                     = %5u\n", eit->version_number);
147         fprintf( stdout, "current next indicator             = %5u\n", eit->current_next_indicator);
148         fprintf( stdout, "section number                     = %5u\n", eit->section_number);
149         fprintf( stdout, "last section number                = %5u\n", eit->last_section_number);
150         fprintf( stdout, "transport_stream_id                = %5u\n", eit->transport_stream_id);
151         fprintf( stdout, "original_network_id                = %5u\n", eit->original_network_id);
152         fprintf( stdout, "segment_last_section_number        = %5u\n", eit->segment_last_section_number);
153         fprintf( stdout, "last_table_id                      = %5u\n", eit->last_table_id);
154
155         EIT::EVENTS::iterator e;
156         for( e = eit->events.begin(); e != eit->events.end(); e++) {
157                 fprintf( stdout, "  event_id                         = %5u\n", e->event_id);
158                 fprintf( stdout, "  start_time                       = %s", asctime( localtime( &e->start_time)));
159                 fprintf( stdout, "  duration                         = %5d\n", e->duration);
160                 fprintf( stdout, "  running_status                   = %5u\n", e->running_status);
161                 fprintf( stdout, "  free_CA_mode                     = %5u\n", e->free_CA_mode);
162                 fprintf( stdout, "  descriptors_loop_length          = %5u\n", e->descriptors_loop_length);                                     
163
164                 EIT::DESCRIPTORS::iterator d;
165                 for( d = e->descriptors.begin(); d != e->descriptors.end(); d++) {
166                         fprintf( stdout, "    descriptor_tag                 = 0x%02X\n", (*d)->descriptor_tag);
167                         fprintf( stdout, "    descriptor_length              = %5u\n", (*d)->descriptor_length);        
168 #if 1
169                         if( (*d)->descriptor_tag == ShortEvent::TAG) {
170                                 ShortEvent *des = (ShortEvent*)*d;
171                                 std::string str;
172                                 AribToString( &str, &des->event_name_char);
173                                 fprintf( stdout, "    event_name                     = %s\n", str.c_str());
174                                 AribToString( &str, &des->text_char);
175                                 fprintf( stdout, "    text                           = %s\n", str.c_str());
176
177                         }
178                         if( (*d)->descriptor_tag == ExtendedEvent::TAG) {
179                                 ExtendedEvent *des = (ExtendedEvent *)*d;
180                                 fprintf( stdout, "    descriptor_number              = %d\n", des->descriptor_number);
181                                 fprintf( stdout, "    last_descriptor_number         = %d\n", des->last_descriptor_number);
182                                 fprintf( stdout, "    length_of_items                = %d\n", des->length_of_items);
183
184                                 ExtendedEvent::ITEMS::iterator it;
185                                 std::string str;
186                                 for( it = des->items.begin(); it != des->items.end(); it++) {
187                                         AribToString( &str, &it->item_description_char);
188                                         fprintf( stdout, "    item_description               = %s\n", str.c_str());
189                                         AribToString( &str, &it->item_char);
190                                         fprintf( stdout, "    item                           = %s\n", str.c_str());
191                                 }
192                                 AribToString( &str, &des->text_char);
193                                 fprintf( stdout, "    text_char                      = %s\n", str.c_str());
194                         }
195                         if( (*d)->descriptor_tag == Component::TAG) {
196                                 Component *des = (Component *)*d;
197                                 std::string str;
198                                 fprintf( stdout, "    stream_content                 = 0x%02X\n", des->stream_content);
199                                 fprintf( stdout, "    component_type                 = 0x%02X\n", des->component_type);
200                                 fprintf( stdout, "    component_tag                  = 0x%02X\n", des->component_tag);
201                                 AribToString( &str, &des->text_char);
202                                 fprintf( stdout, "    text                           = %s\n", str.c_str());
203                         }
204                         if( (*d)->descriptor_tag == Content::TAG) {
205                                 Content *des = (Content *)*d;
206                                 Content::CONTENTS::iterator it;
207                                 for( it = des->contents.begin(); it != des->contents.end(); it++) {
208                                         fprintf( stdout, "    content_nibble_level_1         = %d\n", it->content_nibble_level_1);
209                                         fprintf( stdout, "    content_nibble_level_2         = %d\n", it->content_nibble_level_2);
210                                         fprintf( stdout, "    user_nibble_1                  = %d\n", it->user_nibble_1);
211                                         fprintf( stdout, "    user_nibble_2                  = %d\n", it->user_nibble_2);
212                                 }
213                         }
214                         if( (*d)->descriptor_tag == AudioComponent::TAG) {
215                                 AudioComponent *des = (AudioComponent *)*d;
216                                 std::string str;
217                                 fprintf( stdout, "    stream_content                 = 0x%02X\n", des->stream_content);
218                                 fprintf( stdout, "    component_type                 = 0x%02X\n", des->component_type);
219                                 fprintf( stdout, "    component_tag                  = 0x%02X\n", des->component_tag);
220                                 fprintf( stdout, "    stream_type                    = 0x%02X\n", des->stream_type);
221                                 fprintf( stdout, "    simulcast_group_tag            = 0x%02X\n", des->simulcast_group_tag);
222                                 fprintf( stdout, "    ES_multi_lingual_flag          = %2d\n", des->ES_multi_lingual_flag);
223                                 fprintf( stdout, "    main_component_flag            = %2d\n", des->main_component_flag);
224                                 fprintf( stdout, "    quality_indicator              = 0x%02X\n", des->quality_indicator);
225                                 fprintf( stdout, "    sampling_rate                  = 0x%02X\n", des->sampling_rate);
226                                 fprintf( stdout, "    component_tag                  = 0x%02X\n", des->component_tag);
227                                 fprintf( stdout, "    ISO_639_language_code          = %s\n", des->ISO_639_language_code.c_str());
228                                 if( des->ES_multi_lingual_flag) {
229                                         fprintf( stdout, "    ISO_639_language_code_2        = %s\n", des->ISO_639_language_code_2.c_str());
230                                 }
231                                 AribToString( &str, &des->text_char);
232                                 fprintf( stdout, "    text                           = %s\n", str.c_str());
233                         }
234                         if( (*d)->descriptor_tag == EventGroup::TAG) {
235                                 EventGroup *des = (EventGroup *)*d;
236                                 EventGroup::EVENTS::iterator                    e;
237                                 EventGroup::NETWORK_EVENTS::iterator    n;
238                                 fprintf( stdout, "    group_type                     = %X\n", des->group_type);
239                                 fprintf( stdout, "    event_count                    = %d\n", des->event_count);
240                                 for( e = des->events.begin(); e != des->events.end(); e++) {
241                                         fprintf( stdout, "    service_id                     = %d\n", e->service_id);
242                                         fprintf( stdout, "    event_id                       = %d\n", e->event_id);
243                                 }
244                                 if( des->group_type == 4 || des->group_type == 5) {
245                                         for( n = des->network_events.begin(); n != des->network_events.end(); n++) {
246                                                 fprintf( stdout, "    original_network_id            = %d\n", n->original_network_id);
247                                                 fprintf( stdout, "    transport_stream_id            = %d\n", n->transport_stream_id);
248                                                 fprintf( stdout, "    service_id                     = %d\n", n->event.service_id);
249                                                 fprintf( stdout, "    event_id                       = %d\n", n->event.event_id);
250                                         }
251                                 }
252                                 else {
253                                         fprintf( stdout, "    private_data_length            = %d\n", des->getPrivateData()->size());
254                                 }
255                         }
256 #endif
257                 }
258         }
259 }
260
261 void printNIT( Section *sec)
262 {
263         NIT *nit = (NIT *)sec;
264
265         fprintf( stdout, "\n[ NIT ]\n");
266         fprintf( stdout, "table id                           = 0x%02X\n", nit->table_id);
267         fprintf( stdout, "section syntax indicator           = %5u\n", nit->section_syntax_indicator);
268         fprintf( stdout, "private indicator                  = %5u\n", nit->private_indicator);
269         fprintf( stdout, "section length                     = %5u\n", nit->section_length);
270         fprintf( stdout, "network_id                         = %5u\n", nit->si_id.network_id);
271         fprintf( stdout, "version number                     = %5u\n", nit->version_number);
272         fprintf( stdout, "current next indicator             = %5u\n", nit->current_next_indicator);
273         fprintf( stdout, "section number                     = %5u\n", nit->section_number);
274         fprintf( stdout, "last section number                = %5u\n", nit->last_section_number);
275         fprintf( stdout, "network_descriptors_length         = %5u\n", nit->network_descriptors_length);
276
277         NIT::DESCRIPTORS::iterator d;
278         for( d = nit->networks.begin(); d != nit->networks.end(); d++) {
279                 fprintf( stdout, "  descriptor_tag                   = 0x%02X\n", (*d)->descriptor_tag);
280                 fprintf( stdout, "  descriptor_length                = %5u\n", (*d)->descriptor_length);
281                 if( (*d)->descriptor_tag == NetworkName::TAG) {
282                         NetworkName *des = (NetworkName *)*d;
283                         std::string str;
284                         AribToString( &str, &des->name);
285                         fprintf( stdout, "  name                             = %s\n", str.c_str());
286                 }
287         }
288
289         fprintf( stdout, "transport_stream_loop_length       = %5u\n", nit->transport_stream_loop_length);
290         NIT::STREAMS::iterator s;
291         for( s = nit->streams.begin(); s != nit->streams.end(); s++) {
292                 fprintf( stdout, "  transport_stream_id              = %5u\n", s->transport_stream_id);
293                 fprintf( stdout, "  original_network_id              = %5u\n", s->original_network_id);
294                 fprintf( stdout, "  transport_descriptors_length     = %5u\n", s->transport_descriptors_length);
295                 for( d = s->descriptors.begin(); d != s->descriptors.end(); d++) {
296                         fprintf( stdout, "    descriptor_tag                 = 0x%02X\n", (*d)->descriptor_tag);
297                         fprintf( stdout, "    descriptor_length              = %5u\n", (*d)->descriptor_length);
298                         if( (*d)->descriptor_tag == ServiceList::TAG) {
299                                 ServiceList *des = (ServiceList *)*d;
300                                 ServiceList::SERVICES::iterator it;
301                                 for( it = des->services.begin(); it != des->services.end(); it++) {
302                                         fprintf( stdout, "    service_id                     = %5u\n", it->service_id);
303                                         fprintf( stdout, "    service_type                   = %5u\n", it->service_type);
304                                 }
305                         }
306                 }
307         }
308 }
309
310 void printSDT( Section *sec)
311 {
312         SDT *sdt = (SDT *)sec;
313         std::string str;
314
315         fprintf( stdout, "\n[ SDT ]\n");
316         fprintf( stdout, "table id                           = 0x%02X\n", sdt->table_id);
317         fprintf( stdout, "section syntax indicator           = %5u\n", sdt->section_syntax_indicator);
318         fprintf( stdout, "private indicator                  = %5u\n", sdt->private_indicator);
319         fprintf( stdout, "section length                     = %5u\n", sdt->section_length);
320         fprintf( stdout, "transport_stream_id                = %5u\n", sdt->si_id.transport_stream_id);
321         fprintf( stdout, "version number                     = %5u\n", sdt->version_number);
322         fprintf( stdout, "current next indicator             = %5u\n", sdt->current_next_indicator);
323         fprintf( stdout, "section number                     = %5u\n", sdt->section_number);
324         fprintf( stdout, "last section number                = %5u\n", sdt->last_section_number);
325         fprintf( stdout, "original_network_id                = %5u\n", sdt->original_network_id);
326
327         SDT::SERVICES::iterator s;
328         for( s = sdt->services.begin(); s != sdt->services.end(); s++) {
329                 fprintf( stdout, "  service_id                       = %5u\n", s->service_id);
330                 fprintf( stdout, "  EIT_user_defined_flags           = %d\n", s->EIT_user_defined_flags);
331                 fprintf( stdout, "  EIT_schedule_flag                = %d\n", s->EIT_schedule_flag);
332                 fprintf( stdout, "  EIT_present_following_flag       = %d\n", s->EIT_present_following_flag);
333                 fprintf( stdout, "  running_status                   = %5u\n", s->running_status);
334                 fprintf( stdout, "  free_CA_mode                     = %5u\n", s->free_CA_mode);
335                 fprintf( stdout, "  descriptors_loop_length          = %5u\n", s->descriptors_loop_length);
336         
337                 SDT::DESCRIPTORS::iterator d;
338                 for( d = s->descriptors.begin(); d != s->descriptors.end(); d++) {
339                         fprintf( stdout, "    descriptor_tag                 = 0x%02X\n", (*d)->descriptor_tag);
340                         fprintf( stdout, "    descriptor_length              = %5u\n", (*d)->descriptor_length);
341                         if( (*d)->descriptor_tag == Service::TAG) {
342                                 Service *p = (Service *)*d;
343                                 fprintf( stdout, "    service_type                   = %d\n", p->service_type);
344 #if 1
345                                 AribToString( &str, &p->service_provider_name);
346                                 fprintf( stdout, "    service_provider_name          = %s\n", str.c_str());
347                                 AribToString( &str, &p->service_name);
348                                 fprintf( stdout, "    service_name                   = %s\n", str.c_str());
349 #endif
350                         }
351                 }
352         }
353 }
354
355 void printTOT( Section *sec)
356 {
357         TOT *tot = (TOT *)sec;
358
359         if( tot->table_id == TDT::TDT_ID) {
360                 fprintf( stdout, "\n[ TDT ]\n");
361         }
362         else {
363                 fprintf( stdout, "\n[ TOT ]\n");
364         }
365         fprintf( stdout, "table id                           = 0x%02X\n", tot->table_id);
366         fprintf( stdout, "section syntax indicator           = %5u\n", tot->section_syntax_indicator);
367         fprintf( stdout, "section length                     = %5u\n", tot->section_length);
368         fprintf( stdout, "JST_time                           = %s", asctime( localtime( &tot->JST_time)));
369
370         if( tot->table_id == TOT::TOT_ID) {
371                 fprintf( stdout, "descriptors_loop_length            = %5u\n", tot->descriptors_loop_length);
372                 
373                 TOT::DESCRIPTORS::iterator d;
374                 for( d = tot->descriptors.begin(); d != tot->descriptors.end(); d++) {
375                         fprintf( stdout, "  descriptor_tag                   = 0x%02X\n", (*d)->descriptor_tag);
376                         fprintf( stdout, "  descriptor_length                = %5u\n", (*d)->descriptor_length);
377                 }
378         }
379 }
380
381
382
383 int main( int argc, char **argv)
384 {
385         int fd;
386         char *filename;
387         
388         const uint32_t buf_size = TS::TSPacket::TS_PACKET_SIZE * 10;
389
390         uint16_t        pid;
391         uint8_t         buf[ buf_size];
392         uint32_t        index = 0;
393         uint32_t        read_len;
394         uint32_t        offset = 0;
395         ssize_t         file_read_size;
396
397         TSPacket                        *ts = NULL;
398         Section::STATUS         status;
399         SECTIONS                        secs;
400         SECTIONS::iterator      sec_it;
401
402         AribDescriptorParser    des_parser;
403         
404         uint32_t payload_index = 0;
405
406         if( argc <2) {
407                 fprintf( stderr, "required TS file\n");
408                 return 1;
409         }
410
411         filename = argv[ 1];
412
413         if( strcmp( filename, "-") == 0) {
414                 fd = 0;
415         }
416         else {
417                 fd = open( filename, O_RDONLY);
418         }
419         if( fd == -1) {
420                 fprintf( stderr, "%s is not open.\n", filename);
421                 return 1;
422         }
423
424         secs[ PID_PAT ] = new PAT( &des_parser);
425         secs[ PID_CAT ] = new CAT( &des_parser);
426         secs[ PID_TSDT] = new TSDT( &des_parser);
427         secs[ PID_NIT ] = new NIT( &des_parser);
428         secs[ PID_SDT ] = new SDT( &des_parser);
429         secs[ PID_EIT ] = new EIT( &des_parser);
430         secs[ PID_TOT ] = new TOT( &des_parser);
431         secs[ PID_EIT1] = new EIT( &des_parser);
432         secs[ PID_EIT2] = new EIT( &des_parser);
433
434         std::set< uint16_t>     pcr_pid;
435
436         while( (file_read_size = read( fd, buf + offset, buf_size - offset) + offset) > 0 ) {
437                 index = 0;
438                 while( index + TS::TSPacket::TS_PACKET_SIZE <= file_read_size) {
439                         if( ts) {
440                                 delete ts;
441                         }
442
443                         ts = TS::TSPacket::parse( &buf[ index], file_read_size, &read_len);
444                         if( !ts) {
445                                 fprintf( stderr, "TS Packet parse error\n");
446                                 index += TS::TSPacket::TS_PACKET_SIZE;
447                                 continue;
448                         }
449                         pid = ts->getPID();
450 #if 0
451                         if( pcr_pid.find( pid) != pcr_pid.end()) {
452                                 uint64_t pcr = ts->getAdaptationField()->getProgramClockReference();
453                                 fprintf( stdout, "***** PCR = %lu, PID =  0x%04X *****\n", pcr, pid);
454                         }
455 #endif
456                         sec_it = secs.find( pid);
457                         if( sec_it != secs.end()) {
458                                 payload_index = 0;
459                                 do {
460                                         status = sec_it->second->append( ts, &payload_index);
461                                         if( status == Section::SUCCESS || status == Section::INCLUDE_NEW_SECTION) {
462                                                 if( sec_it->first == PID_PAT) {
463                                                         //printPAT( sec_it->second);
464
465                                                         PAT *pat = (PAT *)sec_it->second;
466                                                         PAT::PROGRAM_MAP::iterator it;
467                                                         SECTIONS::iterator pmt;
468                                                         
469                                                         for( it = pat->program_map_PID.begin(); it != pat->program_map_PID.end(); it++) {
470                                                                 pmt = secs.find( it->second);
471                                                                 if( pmt == secs.end()) {
472                                                                         secs[ it->second] = new PMT( &des_parser);
473                                                                         fprintf( stderr, "new pid 0x%04X, program number %u\n", it->second, it->first);
474                                                                 }
475                                                         }
476                                                 }
477                                                 else if( sec_it->first == PID_CAT) {
478                                                         //printCAT( sec_it->second);
479                                                 }
480                                                 else if( sec_it->first == PID_TSDT) {
481                                                         //printTSDT( sec_it->second);
482                                                 }
483                                                 else if( sec_it->first == PID_NIT) {
484                                                         //printNIT( sec_it->second);
485                                                 }
486                                                 else if( sec_it->first == PID_SDT) {
487                                                         //printSDT( sec_it->second);
488                                                 }
489                                                 else if( (sec_it->first == PID_EIT || sec_it->first == PID_EIT1 || sec_it->first == PID_EIT2)) {
490                                                         printEIT( sec_it->second);
491                                                 }
492                                                 else if( sec_it->first == PID_TOT) {
493                                                         //printTOT( sec_it->second);
494                                                 }
495                                                 else {
496                                                         if( sec_it->second->table_id == PMT::ID) {
497                                                                 //printPMT( sec_it->second);
498
499                                                                 PMT *pmt = (PMT *)sec_it->second;
500                                                                 PMT::DESCRIPTORS::iterator      d;
501                                                                 PMT::ELEMENTS::iterator e;
502
503                                                                 if( pcr_pid.find( pmt->PCR_PID) == pcr_pid.end()) {
504                                                                         pcr_pid.insert( pmt->PCR_PID);
505                                                                 }
506                                                         }
507                                                 }
508                                         }
509                                         else if( status != Section::CONTINUE) {
510                                                 fprintf( stderr, "Section parse error = %d, PID = 0x%04X\n", status, ts->getPID());
511                                         }
512                                 } while( status == Section::INCLUDE_NEW_SECTION);
513                         }
514                         
515                         index += read_len;
516                 }
517                 offset = file_read_size - index;
518                 memmove( buf, buf + index, offset);
519         }
520         
521 EXIT:
522         if( fd > 0) {
523                 close( fd);
524         }
525
526         for( sec_it = secs.begin(); sec_it != secs.end(); sec_it++) {
527                 delete sec_it->second;
528         }
529         secs.clear();
530
531         return 0;
532 }