| 1 |
#include <stdio.h> |
|---|
| 2 |
#include <stdlib.h> |
|---|
| 3 |
#include <string.h> |
|---|
| 4 |
|
|---|
| 5 |
#include "sdt.h" |
|---|
| 6 |
#include "ts_ctl.h" |
|---|
| 7 |
|
|---|
| 8 |
int parseSDThead(unsigned char *data, SDThead *h) { |
|---|
| 9 |
int boff = 0; |
|---|
| 10 |
|
|---|
| 11 |
memset(h, 0, sizeof(SDThead)); |
|---|
| 12 |
|
|---|
| 13 |
boff = 0; |
|---|
| 14 |
h->table_id = getBit(data, &boff, 8); |
|---|
| 15 |
h->section_syntax_indicator = getBit(data, &boff, 1); |
|---|
| 16 |
h->reserved_future_use1 = getBit(data, &boff, 1); |
|---|
| 17 |
h->reserved1 = getBit(data, &boff, 2); |
|---|
| 18 |
h->section_length = getBit(data, &boff, 12); |
|---|
| 19 |
h->transport_stream_id = getBit(data, &boff, 16); |
|---|
| 20 |
h->reserved2 = getBit(data, &boff, 2); |
|---|
| 21 |
h->version_number = getBit(data, &boff, 5); |
|---|
| 22 |
h->current_next_indicator = getBit(data, &boff, 1); |
|---|
| 23 |
h->section_number = getBit(data, &boff, 8); |
|---|
| 24 |
h->last_section_number = getBit(data, &boff, 8); |
|---|
| 25 |
h->original_network_id = getBit(data, &boff, 16); |
|---|
| 26 |
h->reserved_future_use2 = getBit(data, &boff, 8); |
|---|
| 27 |
|
|---|
| 28 |
return 11; |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
int parseSDTbody(unsigned char *data, SDTbody *b) { |
|---|
| 32 |
int boff = 0; |
|---|
| 33 |
|
|---|
| 34 |
memset(b, 0, sizeof(SDTbody)); |
|---|
| 35 |
|
|---|
| 36 |
b->service_id = getBit(data, &boff, 16); |
|---|
| 37 |
b->reserved_future_use1 = getBit(data, &boff, 3); |
|---|
| 38 |
b->EIT_user_defined_flags = getBit(data, &boff, 3); |
|---|
| 39 |
b->EIT_schedule_flag = getBit(data, &boff, 1); |
|---|
| 40 |
b->EIT_present_following_flag = getBit(data, &boff, 1); |
|---|
| 41 |
b->running_status = getBit(data, &boff, 3); |
|---|
| 42 |
b->free_CA_mode = getBit(data, &boff, 1); |
|---|
| 43 |
b->descriptors_loop_length = getBit(data, &boff, 12); |
|---|
| 44 |
|
|---|
| 45 |
return 5; |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
int parseSVCdesc(unsigned char *data, SVCdesc *desc) { |
|---|
| 49 |
int boff = 0; |
|---|
| 50 |
|
|---|
| 51 |
memset(desc, 0, sizeof(SVCdesc)); |
|---|
| 52 |
|
|---|
| 53 |
desc->descriptor_tag = getBit(data, &boff, 8); |
|---|
| 54 |
desc->descriptor_length = getBit(data, &boff, 8); |
|---|
| 55 |
desc->service_type = getBit(data, &boff, 8); |
|---|
| 56 |
desc->service_provider_name_length = getBit(data, &boff, 8); |
|---|
| 57 |
getStr(desc->service_provider_name, data, &boff, desc->service_provider_name_length); |
|---|
| 58 |
desc->service_name_length = getBit(data, &boff, 8); |
|---|
| 59 |
getStr(desc->service_name, data, &boff, desc->service_name_length); |
|---|
| 60 |
|
|---|
| 61 |
return desc->descriptor_length + 2; |
|---|
| 62 |
} |
|---|
| 63 |
int serachid(SVT_CONTROL *top, int service_id) |
|---|
| 64 |
{ |
|---|
| 65 |
SVT_CONTROL *cur = top ; |
|---|
| 66 |
while(cur != NULL){ |
|---|
| 67 |
if(cur->event_id == service_id){ |
|---|
| 68 |
return 1 ; |
|---|
| 69 |
} |
|---|
| 70 |
cur = cur->next ; |
|---|
| 71 |
} |
|---|
| 72 |
return 0 ; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
void enqueue_sdt(SVT_CONTROL *top, SVT_CONTROL *sdtptr) |
|---|
| 76 |
{ |
|---|
| 77 |
SVT_CONTROL *cur ; |
|---|
| 78 |
|
|---|
| 79 |
if(top->next == NULL){ |
|---|
| 80 |
top->next = sdtptr ; |
|---|
| 81 |
top->prev = top ; |
|---|
| 82 |
return ; |
|---|
| 83 |
} |
|---|
| 84 |
cur = top->next ; |
|---|
| 85 |
while(cur != NULL){ |
|---|
| 86 |
if(sdtptr->event_id < cur->event_id){ |
|---|
| 87 |
if(cur->prev != NULL){ |
|---|
| 88 |
cur->prev->next = sdtptr ; |
|---|
| 89 |
sdtptr->prev = cur->prev ; |
|---|
| 90 |
} |
|---|
| 91 |
cur->prev = sdtptr ; |
|---|
| 92 |
sdtptr->next = cur ; |
|---|
| 93 |
return ; |
|---|
| 94 |
} |
|---|
| 95 |
if(cur->next == NULL){ |
|---|
| 96 |
cur->next = sdtptr ; |
|---|
| 97 |
sdtptr->prev = cur ; |
|---|
| 98 |
return ; |
|---|
| 99 |
} |
|---|
| 100 |
cur = cur->next ; |
|---|
| 101 |
} |
|---|
| 102 |
return ; |
|---|
| 103 |
|
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
void dumpSDT(unsigned char *ptr, SVT_CONTROL *top) |
|---|
| 107 |
{ |
|---|
| 108 |
|
|---|
| 109 |
SDThead sdth; |
|---|
| 110 |
SDTbody sdtb; |
|---|
| 111 |
SVCdesc desc; |
|---|
| 112 |
SVT_CONTROL *svtptr ; |
|---|
| 113 |
int rc ; |
|---|
| 114 |
|
|---|
| 115 |
int len = 0; |
|---|
| 116 |
int loop_len = 0; |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
len = parseSDThead(ptr, &sdth); |
|---|
| 120 |
ptr += len; |
|---|
| 121 |
loop_len = sdth.section_length - (len - 3 + 4); |
|---|
| 122 |
while(loop_len > 0) { |
|---|
| 123 |
len = parseSDTbody(ptr, &sdtb); |
|---|
| 124 |
ptr += len; |
|---|
| 125 |
loop_len -= len; |
|---|
| 126 |
parseSVCdesc(ptr, &desc); |
|---|
| 127 |
|
|---|
| 128 |
rc = serachid(top, sdtb.service_id); |
|---|
| 129 |
if(rc == 0){ |
|---|
| 130 |
svtptr = calloc(1, sizeof(SVT_CONTROL)); |
|---|
| 131 |
svtptr->event_id = sdtb.service_id; |
|---|
| 132 |
svtptr->original_network_id = sdth.original_network_id; |
|---|
| 133 |
svtptr->transport_stream_id = sdth.transport_stream_id; |
|---|
| 134 |
svtptr->event_id = sdtb.service_id; |
|---|
| 135 |
memcpy(svtptr->servicename, desc.service_name, strlen(desc.service_name)); |
|---|
| 136 |
enqueue_sdt(top, svtptr); |
|---|
| 137 |
#if 0 |
|---|
| 138 |
printf("SDT=%s,%d,%x,%x,%x,%x,%x,%x,%x\n", |
|---|
| 139 |
desc.service_name, sdtb.service_id, sdtb.reserved_future_use1, |
|---|
| 140 |
sdtb.EIT_user_defined_flags, sdtb.EIT_schedule_flag, sdtb.EIT_present_following_flag, |
|---|
| 141 |
sdtb.running_status, sdtb.free_CA_mode, sdtb.descriptors_loop_length); |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
printf("SDT=(%x:%x)%s,%d,%d,%d,%d,%d(%d,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x)\n", |
|---|
| 148 |
sdth.table_id, desc.service_type, |
|---|
| 149 |
desc.service_name, sdtb.service_id, |
|---|
| 150 |
desc.descriptor_tag, desc.descriptor_length, desc.service_type, |
|---|
| 151 |
desc.service_provider_name_length, desc.service_name_length, |
|---|
| 152 |
sdth.table_id, sdth.section_syntax_indicator, sdth.reserved_future_use1, |
|---|
| 153 |
sdth.reserved1, sdth.section_length, sdth.transport_stream_id, |
|---|
| 154 |
sdth.reserved2, sdth.version_number, sdth.current_next_indicator, |
|---|
| 155 |
sdth.section_number, sdth.last_section_number, sdth.original_network_id, |
|---|
| 156 |
sdth.reserved_future_use2); |
|---|
| 157 |
#endif |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
ptr += sdtb.descriptors_loop_length; |
|---|
| 161 |
loop_len -= sdtb.descriptors_loop_length; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
return; |
|---|
| 165 |
} |
|---|