OSDN Git Service

commit local dev ver
[rec10/rec10-git.git] / epgdump / cdt.c
1 // -*- tab-width:4 -*-
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "cdt.h"
8 #include "ts_ctl.h"
9 #include "clt2png.h"
10
11 int parseCDThead(unsigned char *data, CDThead *h) {
12         int boff = 0;
13
14         memset(h, 0, sizeof(CDThead));
15
16         h->table_id = getBit(data, &boff, 8);
17         h->section_syntax_indicator = getBit(data, &boff, 1);
18         h->reserved_future_use1 = getBit(data, &boff, 1);
19         h->reserved1 = getBit(data, &boff, 2);
20         h->section_length = getBit(data, &boff, 12);
21         h->download_data_id = getBit(data, &boff, 16);
22         h->reserved2 = getBit(data, &boff, 2);
23         h->version_number = getBit(data, &boff, 5);
24         h->current_next_indicator = getBit(data, &boff, 1);
25         h->section_number = getBit(data, &boff, 8);
26         h->last_section_number = getBit(data, &boff, 8);
27         h->original_network_id = getBit(data, &boff, 16);
28         h->data_type = getBit(data, &boff, 8);
29         h->reserved_future_use2 = getBit(data, &boff, 4);
30         h->descriptors_loop_length = getBit(data, &boff, 12);
31
32         return 13;
33 }
34
35 int parseCDTdesc(unsigned char *data, CDTdesc *desc) {
36 // ほとんど呼ばれることはない
37         int boff = 0;
38
39         memset(desc, 0, sizeof(CDTdesc));
40
41         desc->descriptor_tag = getBit(data, &boff, 8);
42         desc->descriptor_length = getBit(data, &boff, 8);
43
44         return desc->descriptor_length + 2;
45 }
46
47 int parseCDTdata(unsigned char *data, CDTdata *cdtd) {
48         int boff = 0;
49
50         memset(cdtd, 0, sizeof(CDTdata));
51
52         cdtd->logo_type = getBit(data, &boff, 8);
53         cdtd->reserved_future_use1 = getBit(data, &boff, 7);
54         cdtd->logo_id = getBit(data, &boff, 9);
55         cdtd->reserved_future_use2 = getBit(data, &boff, 4);
56         cdtd->logo_version = getBit(data, &boff, 12);
57         cdtd->data_size = getBit(data, &boff, 16);
58         cdtd->data = calloc(1, cdtd->data_size);
59         memcpy(cdtd->data, data + boff / 8, cdtd->data_size);
60         //boff += cdtd->data_size * 8;
61
62         return cdtd->data_size + 7;
63 }
64
65 void dumpCDT(unsigned char *ptr, STATION *station, int station_count)
66 {
67         CDThead  cdth;
68         CDTdesc  desc;
69         CDTdata  cdtd;
70         LOGO * pLogo;
71
72         int len = 0;
73         int loop_len = 0;
74         int desc_len = 0;
75         int i = 0;
76
77         /* CDT */
78         len = parseCDThead(ptr, &cdth);
79         ptr += len;
80         loop_len = cdth.section_length - (len - 3 + 4); // 3は共通ヘッダ長 4はCRC
81
82         desc_len = cdth.descriptors_loop_length;
83         while(desc_len > 0) {
84                 len = parseCDTdesc(ptr, &desc);
85                 ptr += len;
86                 desc_len -= len;
87         }
88
89         while(loop_len > 0) {
90                 /*
91                 logo_type
92                 0x00 24x48 864  SD4:3 スモール 
93                 0x01 24x36 648  SD16:9 スモール
94                 0x02 27x48 972  HD スモール 
95                 0x03 36x72 1296 SD4:3 ラージ 
96                 0x04 36x54 972  SD16:9 ラージ 
97                 0x05 36x64 1152 HD ラージ 
98                 */
99                 len = parseCDTdata(ptr, &cdtd);
100                 ptr += len;
101                 loop_len -= len;
102 /*
103                 char fname[16];
104                 sprintf(fname,"%d_%d.png",cdth.download_data_id,cdtd.logo_type);
105                 FILE* png = fopen(fname,"wb");
106                 void* mem_png = NULL;
107                 int size_png;
108                 clt2png(cdtd.data,&mem_png, &size_png);
109                 fwrite(mem_png, 1, size_png, png);
110                 fclose(png);
111 */
112 #if 0
113                 printf("CDT=(%d:%d:%d:%d:%d:%dbyte:desc%dbyte)%d,%d,%d,%d\n",
114                         cdth.table_id, cdth.download_data_id, cdth.version_number, 
115                         cdth.original_network_id, cdth.data_type, 
116                         cdth.section_length, cdth.descriptors_loop_length, 
117
118                         cdtd.logo_type, cdtd.logo_id, cdtd.logo_version,
119                         cdtd.data_size);
120 #endif
121                 for ( i = 0; i < station_count; i++ ) {
122                         pLogo = &station[i].logo_array[cdtd.logo_type];
123                         if ( station[i].logo_download_data_id == cdth.download_data_id ) {
124                                 pLogo->logo = NULL;
125                                 clt2png(cdtd.data, &pLogo->logo, &pLogo->logo_size);
126                         }
127                 }
128         }
129         return;
130 }
131