diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b8044f2 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +all: + cd aribb25; make all + +clean: + cd aribb25; make clean + +install: + cd aribb25; make install diff --git a/arib_std_b25.vcxproj b/arib_std_b25.vcxproj index 66fd17a..6fb63d9 100644 --- a/arib_std_b25.vcxproj +++ b/arib_std_b25.vcxproj @@ -143,7 +143,7 @@ winscard.lib;%(AdditionalDependencies) $(OutDir)b25.exe - No + false Console true true @@ -171,7 +171,7 @@ winscard.lib;%(AdditionalDependencies) $(OutDir)b25.exe - No + false Console true true diff --git a/aribb25/B25Decoder.cpp b/aribb25/B25Decoder.cpp index b0f2875..d9719e1 100644 --- a/aribb25/B25Decoder.cpp +++ b/aribb25/B25Decoder.cpp @@ -9,6 +9,9 @@ int B25Decoder::multi2_round = 4; B25Decoder::B25Decoder() : _bcas(nullptr), _b25(nullptr), _data(nullptr) { +#if !defined(_WIN32) + pthread_mutex_init(&_mtx, nullptr); +#endif } B25Decoder::~B25Decoder() @@ -16,7 +19,11 @@ B25Decoder::~B25Decoder() if (_data) ::free(_data); - std::lock_guard lock(_mtx); +#if defined(_WIN32) + _mtx.lock(); +#else + pthread_mutex_lock(&_mtx); +#endif if (_b25) _b25->release(_b25); @@ -24,31 +31,40 @@ B25Decoder::~B25Decoder() if (_bcas) _bcas->release(_bcas); - char log[64]; - ::wsprintfA(log, "B25Decoder::dtor() : end"); - ::OutputDebugStringA(log); +#if defined(_WIN32) + _mtx.unlock(); +#else + pthread_mutex_unlock(&_mtx); + pthread_mutex_destroy(&_mtx); +#endif } int B25Decoder::init() { int rc; - std::lock_guard lock(_mtx); +#if defined(_WIN32) + _mtx.lock();; +#else + pthread_mutex_lock(&_mtx); +#endif if (_b25) - return -2; - - char log[64]; + { + rc = -2; + goto unlock; + } _bcas = create_b_cas_card(); if (!_bcas) - return -3; + { + rc = -3; + goto unlock; + } rc = _bcas->init(_bcas); if (rc < 0) { - ::wsprintfA(log, "B25Decoder::init() : bcas init error / rc(%d)", rc); - ::OutputDebugStringA(log); rc = -4; goto err; } @@ -66,15 +82,16 @@ int B25Decoder::init() goto err; } + // success _b25->set_strip(_b25, strip); _b25->set_emm_proc(_b25, emm_proc); _b25->set_multi2_round(_b25, multi2_round); - ::wsprintfA(log, "B25Decoder::init() : success"); - ::OutputDebugStringA(log); - return 0; // success + rc = 0; + goto unlock; err: + // error if (_b25) { _b25->release(_b25); @@ -87,10 +104,19 @@ err: _bcas = nullptr; } - ::wsprintfA(log, "B25Decoder::init() : error / rc(%d)", rc); - ::OutputDebugStringA(log); +#if defined(_WIN32) _errtime = ::GetTickCount(); - return rc; // error +#else + clock_gettime(CLOCK_MONOTONIC_COARSE, &_errtime); +#endif + +unlock: +#if defined(_WIN32) + _mtx.unlock(); +#else + pthread_mutex_unlock(&_mtx); +#endif + return rc; } void B25Decoder::setemm(bool flag) @@ -103,8 +129,15 @@ void B25Decoder::decode(BYTE *pSrc, DWORD dwSrcSize, BYTE **ppDst, DWORD *pdwDst { if (!_b25) { +#if defined(_WIN32) DWORD now = ::GetTickCount(); - if ((now - _errtime) > RETRY_INTERVAL) + DWORD interval = (now - _errtime) / 1000; +#else + struct timespec now; + clock_gettime(CLOCK_MONOTONIC_COARSE, &now); + time_t interval = now.tv_sec - _errtime.tv_sec; +#endif + if (interval > RETRY_INTERVAL) { if (init() < 0) _errtime = now; @@ -127,8 +160,6 @@ void B25Decoder::decode(BYTE *pSrc, DWORD dwSrcSize, BYTE **ppDst, DWORD *pdwDst _data = nullptr; } - char log[64]; - ARIB_STD_B25_BUFFER buf; buf.data = pSrc; buf.size = dwSrcSize; @@ -153,11 +184,7 @@ void B25Decoder::decode(BYTE *pSrc, DWORD dwSrcSize, BYTE **ppDst, DWORD *pdwDst BYTE *p = nullptr; _b25->withdraw(_b25, &buf); // withdraw src buffer if (buf.size > 0) - { - ::wsprintfA(log, "B25Decoder::decode() : error / withdraw size(%u)", buf.size); - ::OutputDebugStringA(log); p = (BYTE *)::malloc(buf.size + dwSrcSize); - } if (p) { @@ -185,9 +212,11 @@ void B25Decoder::decode(BYTE *pSrc, DWORD dwSrcSize, BYTE **ppDst, DWORD *pdwDst _bcas = nullptr; } } - ::wsprintfA(log, "B25Decoder::decode() : error / rc(%d)", rc); - ::OutputDebugStringA(log); +#if defined(_WIN32) _errtime = ::GetTickCount(); +#else + clock_gettime(CLOCK_MONOTONIC_COARSE, &_errtime); +#endif return; // error } _b25->get(_b25, &buf); diff --git a/aribb25/B25Decoder.h b/aribb25/B25Decoder.h index d1f838c..d14e5bd 100644 --- a/aribb25/B25Decoder.h +++ b/aribb25/B25Decoder.h @@ -3,12 +3,22 @@ //////////////////////////////////////////////////////////////////////////////// #ifndef __B25DECODER_H__ #define __B25DECODER_H__ -#include -#include + +#if defined(_WIN32) + #include + #include +#else + #include + #include + #include + #include +#endif + +#include "portable.h" #include "arib_std_b25.h" #include "arib_std_b25_error_code.h" -#define RETRY_INTERVAL 10000 // 10sec interval +#define RETRY_INTERVAL 10 // 10sec interval class B25Decoder { @@ -31,11 +41,19 @@ public: static int multi2_round; private: +#if defined(_WIN32) std::mutex _mtx; +#else + pthread_mutex_t _mtx; +#endif B_CAS_CARD *_bcas; ARIB_STD_B25 *_b25; BYTE *_data; +#if defined(_WIN32) DWORD _errtime; +#else + struct timespec _errtime; +#endif }; #endif // __B25DECODER_H__ diff --git a/aribb25/Makefile b/aribb25/Makefile new file mode 100644 index 0000000..9af0bad --- /dev/null +++ b/aribb25/Makefile @@ -0,0 +1,53 @@ +PREFIX = /usr/local +MAJOR = 0 +MINOR = 2 +REVISION = 5 +VER = $(MAJOR).$(MINOR).$(REVISION) + +DEST_HEADER = $(PREFIX)/include/aribb25 + +# PC/SC Lite libraries and headers. +PCSC_CFLAGS ?= `pkg-config libpcsclite --cflags` +PCSC_LDLIBS ?= `pkg-config libpcsclite --libs` + +CC = gcc +CXX = g++ +CFLAGS = -O2 -g -fPIC -Wall $(PCSC_CFLAGS) -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 + +LIBS = $(PCSC_LDLIBS) -lm +LDFLAGS = + +OBJS = arib_std_b25.o b_cas_card.o multi2.o ts_section_parser.o +HEADERS = arib_std_b25.h arib_std_b25_error_code.h b_cas_card.h portable.h +TARGET_APP = b25 +TARGET_LIB = libaribb25.so +TARGETS = $(TARGET_APP) $(TARGET_LIB) +DEPEND = Makefile.dep +SONAME = $(TARGET_LIB).$(MAJOR) + +all: $(TARGETS) + +clean: + rm -f $(OBJS) td.o $(TARGETS) $(DEPEND) + +$(TARGET_APP): $(OBJS) td.o + $(CXX) $(LDFLAGS) -o $(TARGET_APP) $(OBJS) td.o $(LIBS) + +$(TARGET_LIB): $(OBJS) + $(CXX) $(LDFLAGS) -shared -o $(TARGET_LIB) $(OBJS) $(LIBS) -Wl,-soname,$(SONAME) + +$(DEPEND): + $(CC) -fPIC -MM $(OBJS:.o=.c) td.c > $@ + +install: $(TARGET) install-headers + install -m755 b25 $(PREFIX)/bin + install -m755 $(TARGET_LIB) $(PREFIX)/lib/$(TARGET_LIB).$(VER) + ln -sf $(PREFIX)/lib/$(TARGET_LIB).$(VER) $(PREFIX)/lib/$(TARGET_LIB).$(MAJOR) + ln -sf $(PREFIX)/lib/$(TARGET_LIB).$(MAJOR) $(PREFIX)/lib/$(TARGET_LIB) + ldconfig + +install-headers: + mkdir -p $(DEST_HEADER) + install -m644 $(HEADERS) $(DEST_HEADER) + +-include $(DEPEND) diff --git a/aribb25/arib_std_b25.c b/aribb25/arib_std_b25.c index 9399c7a..4493407 100644 --- a/aribb25/arib_std_b25.c +++ b/aribb25/arib_std_b25.c @@ -2005,24 +2005,6 @@ static int proc_ecm(DECRYPTOR_ELEM *dec, B_CAS_CARD *bcas, int32_t multi2_round) dec->m2->set_scramble_key(dec->m2, res.scramble_key); -#if 0 - if (0){ - int i; - fprintf(stdout, "----\n"); - fprintf(stdout, "odd: "); - for(i=0;i<8;i++){ - fprintf(stdout, " %02x", res.scramble_key[i]); - } - fprintf(stdout, "\n"); - fprintf(stdout, "even:"); - for(i=8;i<16;i++){ - fprintf(stdout, " %02x", res.scramble_key[i]); - } - fprintf(stdout, "\n"); - fflush(stdout); - } -#endif - LAST: if(sect.raw != NULL){ n = dec->ecm->ret(dec->ecm, §); @@ -2034,44 +2016,6 @@ LAST: return r; } -#if 0 -static void dump_pts(uint8_t *src, int32_t crypt) -{ - int32_t pts_dts_flag; - int64_t pts,dts; - - src += 4; // TS header - src += 4; // start_code_prefix + stream_id - src += 2; // packet_length - - pts_dts_flag = (src[1] >> 6) & 3; - - src += 3; - if(pts_dts_flag & 2){ - // PTS - pts = (src[0] >> 1) & 0x07; - pts <<= 15; - pts += ((src[1] << 8) + src[2]) >> 1; - pts <<= 15; - pts += ((src[3] << 8) + src[4]) >> 1; - src += 5; - } - if(pts_dts_flag & 1){ - // DTS - dts = (src[0] >> 1) & 0x07; - dts <<= 15; - dts += ((src[1] << 8) + src[2]) >> 1; - dts <<= 15; - dts += ((src[3] << 8) + src[4]) >> 1; - } - - if(pts_dts_flag == 2){ - fprintf(stdout, " key=%d, pts=%"PRId64"\n", crypt, pts/90); - fflush(stdout); - } -} -#endif - static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) { int r; @@ -2176,11 +2120,7 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) }else{ prv->map[pid].normal_packet += 1; } -#if 0 - if( (hdr.payload_unit_start_indicator != 0) && (pid == 0x111) ){ - dump_pts(curr, crypt); - } -#endif + if(!append_work_buffer(&(prv->dbuf), curr, unit)){ return ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; } diff --git a/aribb25/b_cas_card.c b/aribb25/b_cas_card.c index c286048..b344da9 100644 --- a/aribb25/b_cas_card.c +++ b/aribb25/b_cas_card.c @@ -1,17 +1,16 @@ #include "b_cas_card.h" #include "b_cas_card_error_code.h" +#include #include #include -#include - -#include - -#if defined(WIN32) - #include -#endif #include +#if defined(_WIN32) +#include +#include +#endif + /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ inner structures ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ @@ -263,17 +262,7 @@ static int get_id_b_cas_card(void *bcas, B_CAS_ID *dst) return B_CAS_CARD_ERROR_TRANSMIT_FAILED; } - { - int maker_id; - int version; - int check_code; - - maker_id = p[0]; - version = p[1]; - prv->id.data[i] = load_be_uint48(p+2); - check_code = load_be_uint16(p+8); - } - + prv->id.data[i] = load_be_uint48(p+2); p += 10; } diff --git a/aribb25/portable.h b/aribb25/portable.h index a102505..4a068c2 100644 --- a/aribb25/portable.h +++ b/aribb25/portable.h @@ -3,4 +3,33 @@ #include +#if !defined(_WIN32) + #define _open open + #define _close close + #define _read read + #define _write write + #define _lseeki64 lseek + #define _telli64(fd) (lseek(fd,0,SEEK_CUR)) + #define _O_BINARY (0) + #define _O_RDONLY (O_RDONLY) + #define _O_WRONLY (O_WRONLY) + #define _O_SEQUENTIAL (0) + #define _O_CREAT (O_CREAT) + #define _O_TRUNC (O_TRUNC) + #define _S_IREAD (S_IRUSR|S_IRGRP|S_IROTH) + #define _S_IWRITE (S_IWUSR|S_IWGRP|S_IWOTH) + #define _S_IWRITE (S_IWUSR|S_IWGRP|S_IWOTH) + #define _tcslen strlen + #define __inline inline + #define __forceinline inline + typedef unsigned long DWORD; + typedef char TCHAR; + typedef char *LPTSTR; + typedef const char *LPCTSTR; +#endif + +#if !defined(nullptr) + #define nullptr NULL +#endif + #endif /* PORTABLE_H */ diff --git a/aribb25/td.c b/aribb25/td.c index 5b7e569..12727b8 100644 --- a/aribb25/td.c +++ b/aribb25/td.c @@ -1,16 +1,15 @@ -#if defined(WIN32) - #define _CRT_SECURE_NO_WARNINGS +#if defined(_WIN32) +#define _CRT_SECURE_NO_WARNINGS #endif #include #include #include - #include #include #include -#if defined(WIN32) +#if defined(_WIN32) #include #include #include @@ -42,7 +41,7 @@ int main(int argc, char **argv) int n; OPTION opt; - #if defined(WIN32) + #if defined(_WIN32) _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE ); _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT ); _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE ); @@ -62,7 +61,7 @@ int main(int argc, char **argv) test_arib_std_b25(argv[n+0], argv[n+1], &opt); } - #if defined(WIN32) + #if defined(_WIN32) _CrtDumpMemoryLeaks(); #endif @@ -159,9 +158,9 @@ static void test_arib_std_b25(const char *src, const char *dst, OPTION *opt) int code,i,n,m; int sfd,dfd; + uint32_t offset; int64_t total; - int64_t offset; -#if defined(WIN32) +#if defined(_WIN32) unsigned long tick,tock; #else struct timeval tick,tock; @@ -243,7 +242,7 @@ static void test_arib_std_b25(const char *src, const char *dst, OPTION *opt) } offset = 0; -#if defined(WIN32) +#if defined(_WIN32) tock = GetTickCount(); #else gettimeofday(&tock, NULL); @@ -276,7 +275,7 @@ static void test_arib_std_b25(const char *src, const char *dst, OPTION *opt) if(opt->verbose != 0){ m = (int)(10000*offset/total); mbps = 0.0; -#if defined(WIN32) +#if defined(_WIN32) tick = GetTickCount(); if (tick-tock > 100) { mbps = offset; @@ -319,7 +318,7 @@ static void test_arib_std_b25(const char *src, const char *dst, OPTION *opt) if(opt->verbose != 0){ mbps = 0.0; -#if defined(WIN32) +#if defined(_WIN32) tick = GetTickCount(); if (tick-tock > 100) { mbps = offset; @@ -357,7 +356,7 @@ static void test_arib_std_b25(const char *src, const char *dst, OPTION *opt) fprintf(stderr, " channel: %d\n", pgrm.program_number); fprintf(stderr, " unpurchased ECM count: %d\n", pgrm.ecm_unpurchased_count); fprintf(stderr, " last ECM error code: %04x\n", pgrm.last_ecm_error_code); - #if defined(WIN32) + #if defined(_WIN32) fprintf(stderr, " undecrypted TS packet: %I64d\n", pgrm.undecrypted_packet_count); fprintf(stderr, " total TS packet: %I64d\n", pgrm.total_packet_count); #else diff --git a/aribb25/ts_section_parser_error_code.h b/aribb25/ts_section_parser_error_code.h index 7042b76..b84a950 100644 --- a/aribb25/ts_section_parser_error_code.h +++ b/aribb25/ts_section_parser_error_code.h @@ -1,5 +1,5 @@ #ifndef TS_SECTION_PARSER_ERROR_CODE_H -#define TS_SECTION_PARESR_ERROR_CODE_H +#define TS_SECTION_PARSER_ERROR_CODE_H #define TS_SECTION_PARSER_ERROR_INVALID_PARAM -1 #define TS_SECTION_PARSER_ERROR_NO_ENOUGH_MEMORY -2