initial commit
[arib_std_b25.git] / src / td.c
blobe703b87c2c5f8e3ab0c0c3c5c493f5f4495cb5b7
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
5 #include <fcntl.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
9 #if defined(WIN32)
10 #include <io.h>
11 #include <windows.h>
12 #include <crtdbg.h>
13 #else
14 #define __STDC_FORMAT_MACROS
15 #include <inttypes.h>
16 #include <unistd.h>
17 #include <sys/time.h>
18 #endif
20 #include "arib_std_b25.h"
21 #include "b_cas_card.h"
23 typedef struct {
24 int32_t round;
25 int32_t strip;
26 int32_t emm;
27 int32_t verbose;
28 int32_t power_ctrl;
29 } OPTION;
31 static void show_usage();
32 static int parse_arg(OPTION *dst, int argc, char **argv);
33 static void test_arib_std_b25(const char *src, const char *dst, OPTION *opt);
34 static void show_bcas_power_on_control_info(B_CAS_CARD *bcas);
36 int main(int argc, char **argv)
38 int n;
39 OPTION opt;
41 #if defined(WIN32)
42 _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
43 _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
44 _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
45 _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
46 _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
47 _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
48 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_DELAY_FREE_MEM_DF|_CRTDBG_CHECK_ALWAYS_DF|_CRTDBG_LEAK_CHECK_DF);
49 #endif
51 n = parse_arg(&opt, argc, argv);
52 if(n+2 > argc){
53 show_usage();
54 exit(EXIT_FAILURE);
57 for(;n<=(argc-2);n+=2){
58 test_arib_std_b25(argv[n+0], argv[n+1], &opt);
61 #if defined(WIN32)
62 _CrtDumpMemoryLeaks();
63 #endif
65 return EXIT_SUCCESS;
68 static void show_usage()
70 fprintf(stderr, "b25 - ARIB STD-B25 test program ver. 0.2.5 (2012, 2/13)\n");
71 fprintf(stderr, "usage: b25 [options] src.m2t dst.m2t [more pair ..]\n");
72 fprintf(stderr, "options:\n");
73 fprintf(stderr, " -r round (integer, default=4)\n");
74 fprintf(stderr, " -s strip\n");
75 fprintf(stderr, " 0: keep null(padding) stream (default)\n");
76 fprintf(stderr, " 1: strip null stream\n");
77 fprintf(stderr, " -m EMM\n");
78 fprintf(stderr, " 0: ignore EMM (default)\n");
79 fprintf(stderr, " 1: send EMM to B-CAS card\n");
80 fprintf(stderr, " -p power_on_control_info\n");
81 fprintf(stderr, " 0: do nothing additionaly\n");
82 fprintf(stderr, " 1: show B-CAS EMM receiving request (default)\n");
83 fprintf(stderr, " -v verbose\n");
84 fprintf(stderr, " 0: silent\n");
85 fprintf(stderr, " 1: show processing status (default)\n");
86 fprintf(stderr, "\n");
89 static int parse_arg(OPTION *dst, int argc, char **argv)
91 int i;
93 dst->round = 4;
94 dst->strip = 0;
95 dst->emm = 0;
96 dst->power_ctrl = 1;
97 dst->verbose = 1;
99 for(i=1;i<argc;i++){
100 if(argv[i][0] != '-'){
101 break;
103 switch(argv[i][1]){
104 case 'm':
105 if(argv[i][2]){
106 dst->emm = atoi(argv[i]+2);
107 }else{
108 dst->emm = atoi(argv[i+1]);
109 i += 1;
111 break;
112 case 'p':
113 if(argv[i][2]){
114 dst->power_ctrl = atoi(argv[i]+2);
115 }else{
116 dst->power_ctrl = atoi(argv[i+1]);
117 i += 1;
119 break;
120 case 'r':
121 if(argv[i][2]){
122 dst->round = atoi(argv[i]+2);
123 }else{
124 dst->round = atoi(argv[i+1]);
125 i += 1;
127 break;
128 case 's':
129 if(argv[i][2]){
130 dst->strip = atoi(argv[i]+2);
131 }else{
132 dst->strip = atoi(argv[i+1]);
133 i += 1;
135 break;
136 case 'v':
137 if(argv[i][2]){
138 dst->verbose = atoi(argv[i]+2);
139 }else{
140 dst->verbose = atoi(argv[i+1]);
141 i += 1;
143 break;
144 default:
145 fprintf(stderr, "error - unknown option '-%c'\n", argv[i][1]);
146 return argc;
150 return i;
153 static void test_arib_std_b25(const char *src, const char *dst, OPTION *opt)
155 int code,i,n,m;
156 int sfd,dfd;
158 int64_t total;
159 int64_t offset;
160 #if defined(WIN32)
161 unsigned long tick,tock;
162 #else
163 struct timeval tick,tock;
164 double millisec;
165 #endif
166 double mbps;
168 ARIB_STD_B25 *b25;
169 B_CAS_CARD *bcas;
171 ARIB_STD_B25_PROGRAM_INFO pgrm;
173 uint8_t data[64*1024];
175 ARIB_STD_B25_BUFFER sbuf;
176 ARIB_STD_B25_BUFFER dbuf;
178 sfd = -1;
179 dfd = -1;
180 b25 = NULL;
181 bcas = NULL;
183 sfd = _open(src, _O_BINARY|_O_RDONLY|_O_SEQUENTIAL);
184 if(sfd < 0){
185 fprintf(stderr, "error - failed on _open(%s) [src]\n", src);
186 goto LAST;
189 _lseeki64(sfd, 0, SEEK_END);
190 total = _telli64(sfd);
191 _lseeki64(sfd, 0, SEEK_SET);
193 b25 = create_arib_std_b25();
194 if(b25 == NULL){
195 fprintf(stderr, "error - failed on create_arib_std_b25()\n");
196 goto LAST;
199 code = b25->set_multi2_round(b25, opt->round);
200 if(code < 0){
201 fprintf(stderr, "error - failed on ARIB_STD_B25::set_multi2_round() : code=%d\n", code);
202 goto LAST;
205 code = b25->set_strip(b25, opt->strip);
206 if(code < 0){
207 fprintf(stderr, "error - failed on ARIB_STD_B25::set_strip() : code=%d\n", code);
208 goto LAST;
211 code = b25->set_emm_proc(b25, opt->emm);
212 if(code < 0){
213 fprintf(stderr, "error - failed on ARIB_STD_B25::set_emm_proc() : code=%d\n", code);
214 goto LAST;
217 bcas = create_b_cas_card();
218 if(bcas == NULL){
219 fprintf(stderr, "error - failed on create_b_cas_card()\n");
220 goto LAST;
223 code = bcas->init(bcas);
224 if(code < 0){
225 fprintf(stderr, "error - failed on B_CAS_CARD::init() : code=%d\n", code);
226 goto LAST;
229 code = b25->set_b_cas_card(b25, bcas);
230 if(code < 0){
231 fprintf(stderr, "error - failed on ARIB_STD_B25::set_b_cas_card() : code=%d\n", code);
232 goto LAST;
235 dfd = _open(dst, _O_BINARY|_O_WRONLY|_O_SEQUENTIAL|_O_CREAT|_O_TRUNC, _S_IREAD|_S_IWRITE);
236 if(dfd < 0){
237 fprintf(stderr, "error - failed on _open(%s) [dst]\n", dst);
238 goto LAST;
241 offset = 0;
242 #if defined(WIN32)
243 tock = GetTickCount();
244 #else
245 gettimeofday(&tock, NULL);
246 #endif
247 while( (n = _read(sfd, data, sizeof(data))) > 0 ){
248 sbuf.data = data;
249 sbuf.size = n;
251 code = b25->put(b25, &sbuf);
252 if(code < 0){
253 fprintf(stderr, "error - failed on ARIB_STD_B25::put() : code=%d\n", code);
254 goto LAST;
257 code = b25->get(b25, &dbuf);
258 if(code < 0){
259 fprintf(stderr, "error - failed on ARIB_STD_B25::get() : code=%d\n", code);
260 goto LAST;
263 if(dbuf.size > 0){
264 n = _write(dfd, dbuf.data, dbuf.size);
265 if(n != dbuf.size){
266 fprintf(stderr, "error failed on _write(%d)\n", dbuf.size);
267 goto LAST;
271 offset += sbuf.size;
272 if(opt->verbose != 0){
273 m = (int)(10000*offset/total);
274 mbps = 0.0;
275 #if defined(WIN32)
276 tick = GetTickCount();
277 if (tick-tock > 100) {
278 mbps = offset;
279 mbps /= 1024;
280 mbps /= (tick-tock);
282 #else
283 gettimeofday(&tick, NULL);
284 millisec = (tick.tv_sec - tock.tv_sec) * 1000;
285 millisec += (tick.tv_usec - tock.tv_usec) / 1000;
286 if(millisec > 100.0) {
287 mbps = offset;
288 mbps /= 1024;
289 mbps /= millisec;
291 #endif
292 fprintf(stderr, "\rprocessing: %2d.%02d%% [%6.2f MB/sec]", m/100, m%100, mbps);
296 code = b25->flush(b25);
297 if(code < 0){
298 fprintf(stderr, "error - failed on ARIB_STD_B25::flush() : code=%d\n", code);
299 goto LAST;
302 code = b25->get(b25, &dbuf);
303 if(code < 0){
304 fprintf(stderr, "error - failed on ARIB_STD_B25::get() : code=%d\n", code);
305 goto LAST;
308 if(dbuf.size > 0){
309 n = _write(dfd, dbuf.data, dbuf.size);
310 if(n != dbuf.size){
311 fprintf(stderr, "error - failed on _write(%d)\n", dbuf.size);
312 goto LAST;
316 if(opt->verbose != 0){
317 mbps = 0.0;
318 #if defined(WIN32)
319 tick = GetTickCount();
320 if (tick-tock > 100) {
321 mbps = offset;
322 mbps /= 1024;
323 mbps /= (tick-tock);
325 #else
326 gettimeofday(&tick, NULL);
327 millisec = (tick.tv_sec - tock.tv_sec) * 1000;
328 millisec += (tick.tv_usec - tock.tv_usec) / 1000;
329 if(millisec > 100.0) {
330 mbps = offset;
331 mbps /= 1024;
332 mbps /= millisec;
334 #endif
335 fprintf(stderr, "\rprocessing: finish [%6.2f MB/sec]\n", mbps);
336 fflush(stderr);
337 fflush(stdout);
340 n = b25->get_program_count(b25);
341 if(n < 0){
342 fprintf(stderr, "error - failed on ARIB_STD_B25::get_program_count() : code=%d\n", code);
343 goto LAST;
345 for(i=0;i<n;i++){
346 code = b25->get_program_info(b25, &pgrm, i);
347 if(code < 0){
348 fprintf(stderr, "error - failed on ARIB_STD_B25::get_program_info(%d) : code=%d\n", i, code);
349 goto LAST;
351 if(pgrm.ecm_unpurchased_count > 0){
352 fprintf(stderr, "warning - unpurchased ECM is detected\n");
353 fprintf(stderr, " channel: %d\n", pgrm.program_number);
354 fprintf(stderr, " unpurchased ECM count: %d\n", pgrm.ecm_unpurchased_count);
355 fprintf(stderr, " last ECM error code: %04x\n", pgrm.last_ecm_error_code);
356 #if defined(WIN32)
357 fprintf(stderr, " undecrypted TS packet: %d\n", pgrm.undecrypted_packet_count);
358 fprintf(stderr, " total TS packet: %d\n", pgrm.total_packet_count);
359 #else
360 fprintf(stderr, " undecrypted TS packet: %"PRId64"\n", pgrm.undecrypted_packet_count);
361 fprintf(stderr, " total TS packet: %"PRId64"\n", pgrm.total_packet_count);
362 #endif
366 if(opt->power_ctrl != 0){
367 show_bcas_power_on_control_info(bcas);
370 LAST:
372 if(sfd >= 0){
373 _close(sfd);
374 sfd = -1;
377 if(dfd >= 0){
378 _close(dfd);
379 dfd = -1;
382 if(b25 != NULL){
383 b25->release(b25);
384 b25 = NULL;
387 if(bcas != NULL){
388 bcas->release(bcas);
389 bcas = NULL;
393 static void show_bcas_power_on_control_info(B_CAS_CARD *bcas)
395 int code;
396 int i,w;
397 B_CAS_PWR_ON_CTRL_INFO pwc;
399 code = bcas->get_pwr_on_ctrl(bcas, &pwc);
400 if(code < 0){
401 fprintf(stderr, "error - failed on B_CAS_CARD::get_pwr_on_ctrl() : code=%d\n", code);
402 return;
405 if(pwc.count == 0){
406 fprintf(stdout, "no EMM receiving request\n");
407 return;
410 fprintf(stdout, "total %d EMM receiving request\n", pwc.count);
411 for(i=0;i<pwc.count;i++){
412 fprintf(stdout, "+ [%d] : tune ", i);
413 switch(pwc.data[i].network_id){
414 case 4:
415 w = pwc.data[i].transport_id;
416 fprintf(stdout, "BS-%d/TS-%d ", ((w >> 4) & 0x1f), (w & 7));
417 break;
418 case 6:
419 case 7:
420 w = pwc.data[i].transport_id;
421 fprintf(stdout, "ND-%d/TS-%d ", ((w >> 4) & 0x1f), (w & 7));
422 break;
423 default:
424 fprintf(stdout, "unknown(b:0x%02x,n:0x%04x,t:0x%04x) ", pwc.data[i].broadcaster_group_id, pwc.data[i].network_id, pwc.data[i].transport_id);
425 break;
427 fprintf(stdout, "between %04d %02d/%02d ", pwc.data[i].s_yy, pwc.data[i].s_mm, pwc.data[i].s_dd);
428 fprintf(stdout, "to %04d %02d/%02d ", pwc.data[i].l_yy, pwc.data[i].l_mm, pwc.data[i].l_dd);
429 fprintf(stdout, "least %d hours\n", pwc.data[i].hold_time);