tsselect を Mac でコンパイル

生 TS を Mac でいじるのに tsselect を手ビルドしたのでパッチの記録。

Linux 版を持ってきても使えることを知ったときには遅かった。

--- tsselect.c.orig	2010-01-17 04:54:56.000000000 +0900
+++ tsselect.c	2010-01-17 05:10:33.000000000 +0900
@@ -1,8 +1,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdint.h>
 
-#include <io.h>
+#include <unistd.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -12,11 +13,11 @@
 	int           pid;
 	int           last_continuity_counter;
 
-	__int64       first;
-	__int64       total;
-	__int64       error;
-	__int64       drop;
-	__int64       scrambling;
+	int64_t       first;
+	int64_t       total;
+	int64_t       error;
+	int64_t       drop;
+	int64_t       scrambling;
 
 	unsigned char last_packet[188];
 	int           duplicate_count;
@@ -25,13 +26,13 @@
 
 typedef struct {
 
-	__int64       miss;
-	__int64       sync;
+	int64_t       miss;
+	int64_t       sync;
 
-	__int64       drop_count;
+	int64_t       drop_count;
 	
 	short         drop_pid[4];
-	__int64       drop_pos[4];
+	int64_t       drop_pos[4];
 	
 } RESYNC_REPORT;
 
@@ -59,8 +60,8 @@
 	int           transport_private_data_flag;
 	int           adaptation_field_extension_flag;
 
-	__int64       program_clock_reference;
-	__int64       original_program_clock_reference;
+	int64_t       program_clock_reference;
+	int64_t       original_program_clock_reference;
 
 	int           splice_countdown;
 
@@ -74,7 +75,7 @@
 	int           ltw_offset;
 	int           piecewise_rate;
 	int           splice_type;
-	__int64       dts_next_au;
+	int64_t       dts_next_au;
 	
 } ADAPTATION_FIELD;
 
@@ -89,10 +90,10 @@
 static void extract_adaptation_field(ADAPTATION_FIELD *dst, unsigned char *data);
 static int check_unit_invert(unsigned char *head, unsigned char *tail);
 
-static void add_drop_info(RESYNC_REPORT *report, int count, int max, int pid, __int64 pos);
+static void add_drop_info(RESYNC_REPORT *report, int count, int max, int pid, int64_t pos);
 static void print_resync_report(RESYNC_REPORT *report, int count, int max);
 
-static void show_tdt_or_tot(TS_HEADER *hdr, unsigned char *packet, __int64 pos);
+static void show_tdt_or_tot(TS_HEADER *hdr, unsigned char *packet, int64_t pos);
 
 int main(int argc, char **argv)
 {
@@ -169,8 +170,8 @@
 	int i,m,n;
 	int unit_size;
 
-	__int64 offset;
-	__int64 total;
+	int64_t offset;
+	int64_t total;
 
 	TS_STATUS *stat;
 	TS_HEADER  header;
@@ -193,15 +194,14 @@
 	resync_log_max = sizeof(resync_report)/sizeof(RESYNC_REPORT);
 	resync_count = 0;
 	
-	fd = _open(path, _O_BINARY|_O_RDONLY|_O_SEQUENTIAL);
+	fd = open(path, O_RDONLY);
 	if(fd < 0){
 		fprintf(stderr, "error - failed on open(%s) [src]\n", path);
 		goto LAST;
 	}
 
-	_lseeki64(fd, 0, SEEK_END);
-	total = _telli64(fd);
-	_lseeki64(fd, 0, SEEK_SET);
+	total = lseek(fd, 0, SEEK_END);
+	lseek(fd, 0, SEEK_SET);
 
 	stat = (TS_STATUS *)calloc(8192, sizeof(TS_STATUS));
 	if(stat == NULL){
@@ -217,7 +217,7 @@
 
 	offset = 0;
 	idx = 0;
-	n = _read(fd, buf, sizeof(buf));
+	n = read(fd, buf, sizeof(buf));
 
 	unit_size = select_unit_size(buf, buf+n);
 	if(unit_size < 188){
@@ -315,7 +315,7 @@
 		if(n > 0){
 			memcpy(buf, curr, n);
 		}
-		m = _read(fd, buf+n, sizeof(buf)-n);
+		m = read(fd, buf+n, sizeof(buf)-n);
 		if(m < 1){
 			break;
 		}
@@ -408,7 +408,7 @@
 	if(stat){
 		for(i=0;i<8192;i++){
 			if(stat[i].total > 0){
-				printf("pid=0x%04x, total=%8I64d, d=%3I64d, e=%3I64d, scrambling=%I64d, offset=%I64d\n", i, stat[i].total, stat[i].drop, stat[i].error, stat[i].scrambling, stat[i].first);
+				printf("pid=0x%04x, total=%8d, d=%3d, e=%3d, scrambling=%d, offset=%d\n", i, stat[i].total, stat[i].drop, stat[i].error, stat[i].scrambling, stat[i].first);
 			}
 		}
 		free(stat);
@@ -416,7 +416,7 @@
 	}
 
 	if(fd >= 0){
-		_close(fd);
+		close(fd);
 		fd = -1;
 	}
 }
@@ -431,8 +431,8 @@
 
 	TS_HEADER header;
 
-	__int64 offset;
-	__int64 total;
+	int64_t offset;
+	int64_t total;
 
 	unsigned char *p;
 	unsigned char *curr;
@@ -443,25 +443,24 @@
 	sfd = -1;
 	dfd = -1;
 
-	sfd = _open(src, _O_BINARY|_O_RDONLY|_O_SEQUENTIAL);
+	sfd = open(src, O_RDONLY);
 	if(sfd < 0){
 		fprintf(stderr, "error - failed on open(%s) [src]\n", src);
 		goto LAST;
 	}
 
-	dfd = _open(dst, _O_WRONLY|_O_BINARY|_O_CREAT|_O_TRUNC, _S_IREAD|_S_IWRITE);
+	dfd = open(dst, O_WRONLY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE);
 	if(dfd < 0){
 		fprintf(stderr, "error - failed on open(%s) [dst]\n", dst);
 		goto LAST;
 	}
 
-	_lseeki64(sfd, 0, SEEK_END);
-	total = _telli64(sfd);
-	_lseeki64(sfd, 0, SEEK_SET);
+	total = lseek(sfd, 0, SEEK_END);
+	lseek(sfd, 0, SEEK_SET);
 
 	offset = 0;
 	idx = 0;
-	n = _read(sfd, buf, sizeof(buf));
+	n = read(sfd, buf, sizeof(buf));
 
 	unit_size = select_unit_size(buf, buf+n);
 	if(unit_size < 188){
@@ -485,7 +484,7 @@
 			}
 			extract_ts_header(&header, curr);
 			if(pid[header.pid] != 0){
-				m = _write(dfd, curr, 188);
+				m = write(dfd, curr, 188);
 				if(m != 188){
 					fprintf(stderr, "error - failed on write() [dst]\n");
 					goto LAST;
@@ -506,7 +505,7 @@
 		if(n > 0){
 			memcpy(buf, curr, n);
 		}
-		m = _read(sfd, buf+n, sizeof(buf)-n);
+		m = read(sfd, buf+n, sizeof(buf)-n);
 		if(m < 1){
 			break;
 		}
@@ -528,7 +527,7 @@
 		}
 		extract_ts_header(&header, curr);
 		if(pid[header.pid] != 0){
-			m = _write(dfd, curr, 188);
+			m = write(dfd, curr, 188);
 			if(m != 188){
 				fprintf(stderr, "error - failed on write() [dst]\n");
 				goto LAST;
@@ -541,11 +540,11 @@
 
 LAST:
 	if(dfd >= 0){
-		_close(dfd);
+		close(dfd);
 		dfd = -1;
 	}
 	if(sfd >= 0){
-		_close(sfd);
+		close(sfd);
 		sfd = -1;
 	}
 }
@@ -807,7 +806,7 @@
 	return 0;
 }
 
-static void add_drop_info(RESYNC_REPORT *report, int count, int max, int pid, __int64 pos)
+static void add_drop_info(RESYNC_REPORT *report, int count, int max, int pid, int64_t pos)
 {
 	int idx;
 	int n;
@@ -840,18 +839,18 @@
 	}
 
 	for(i=0;i<m;i++){
-		printf("  resync[%d] : miss=0x%012I64x, sync=0x%012I64x, drop=%I64d\n", i, report[i].miss, report[i].sync, report[i].drop_count);
+		printf("  resync[%d] : miss=0x%012x, sync=0x%012x, drop=%d\n", i, report[i].miss, report[i].sync, report[i].drop_count);
 		n = (int)report[i].drop_count;
 		if(n > 4){
 			n = 4;
 		}
 		for(j=0;j<n;j++){
-			printf("    drop[%d] : pid=0x%04x, pos=0x%012I64x\n", j, report[i].drop_pid[j], report[i].drop_pos[j]);
+			printf("    drop[%d] : pid=0x%04x, pos=0x%012x\n", j, report[i].drop_pid[j], report[i].drop_pos[j]);
 		}			
 	}
 }
 
-static void show_tdt_or_tot(TS_HEADER *hdr, unsigned char *packet, __int64 pos)
+static void show_tdt_or_tot(TS_HEADER *hdr, unsigned char *packet, int64_t pos)
 {
 	unsigned char *p;
 
@@ -874,10 +873,10 @@
 	
 	if(table_id == 0x70){
 		/* TDT */
-		fprintf(stdout, "TDT: [%02x:%02x:%02x] offset=%I64d\n", p[2], p[3], p[4], pos);
+		fprintf(stdout, "TDT: [%02x:%02x:%02x] offset=%d\n", p[2], p[3], p[4], pos);
 	}else if(table_id == 0x73){
 		/* TOT */
-		fprintf(stdout, "TOT: [%02x:%02x:%02x] offset=%I64d\n", p[2], p[3], p[4], pos);
+		fprintf(stdout, "TOT: [%02x:%02x:%02x] offset=%d\n", p[2], p[3], p[4], pos);
 	}
 }