static void _encode(Json_val_t *root, const char *name, size_t len, Json_encode_ctx *enc) { size_t i = 0; switch (root->val_type) { case JT_NULL: assert(Json_encode_append_null(enc, name, len)); break; case JT_TRUE: assert(Json_encode_append_bool(enc, 1, name, len)); break; case JT_FALSE: assert(Json_encode_append_bool(enc, 0, name, len)); break; case JT_STRING: assert(Json_encode_append_string(enc, root->v.string.str, root->v.string.len, name, len)); break; case JT_INT: assert(Json_encode_append_integer(enc, root->v.integer, name, len)); break; case JT_REAL: assert(Json_encode_append_real(enc, root->v.real, name, len)); break; case JT_ARRAY: assert(Json_encode_begin_array(enc, name, len)); for (i = 0; i < root->v.array.len; i++) { _encode(root->v.array.arr + i, NULL, 0, enc); } assert(Json_encode_end_array(enc)); break; case JT_OBJECT: assert(Json_encode_begin_object(enc, name, len)); for (i = 0; i < root->v.object.len; i++) { Json_pair_t *pair = root->v.object.objects + i; _encode(&pair->value, pair->name.v.string.str,pair->name.v.string.len,enc); } assert(Json_encode_end_object(enc)); break; } }
/* See store.py:_auxencode for a description. */ static Py_ssize_t auxencode(char *dest, size_t destsize, const char *src, Py_ssize_t len) { static const uint32_t twobytes[8]; static const uint32_t onebyte[8] = { ~0U, 0xffff3ffe, ~0U, ~0U, ~0U, ~0U, ~0U, ~0U, }; return _encode(twobytes, onebyte, dest, 0, destsize, src, len, 0); }
unsigned CShuangpinSegmentor::_push (unsigned ch) { int startFrom = 0; bool isInputPy; EShuangpinType shpType; m_pystr.push_back (ch); const int len = m_pystr.size(); if (m_hasInvalid) { startFrom = len - 1; m_segs.push_back (TSegment (ch, startFrom, 1, IPySegmentor::INVALID)); goto RETURN; } shpType = s_shpData.getShuangpinType(); isInputPy = ( islower(ch) || (ch == ';' && (shpType == MS2003 || shpType == ZIGUANG)) ); if (!isInputPy) { startFrom = len - 1; IPySegmentor::ESegmentType seg_type; if (ch == '\'' && m_inputBuf.size() > 1) seg_type = IPySegmentor::SYLLABLE_SEP; else seg_type = IPySegmentor::STRING; m_segs.push_back (TSegment (ch, startFrom, 1, seg_type)); m_nAlpha += 1; m_nLastValidPos += 1; } else { bool bCompleted = !((len - m_nAlpha)%2) && isInputPy; char buf[4]; if (bCompleted) { sprintf(buf, "%c%c", m_pystr[len-2], ch); } else { sprintf(buf, "%c", ch); } startFrom = _encode(buf, ch, bCompleted); if (startFrom < 0) { m_hasInvalid = true; startFrom = m_pystr.size() - 1; m_segs.push_back (TSegment (ch, startFrom, 1, IPySegmentor::INVALID)); } } RETURN:; if (m_pGetFuzzySyllablesOp && m_pGetFuzzySyllablesOp->isEnabled()) if ( m_segs.back().m_type == SYLLABLE) _addFuzzySyllables (m_segs.back ()); return startFrom; }
static Py_ssize_t basicencode(char *dest, size_t destsize, const char *src, Py_ssize_t len) { static const uint32_t twobytes[8] = { 0, 0, 0x87fffffe }; static const uint32_t onebyte[8] = { 1, 0x2bff3bfa, 0x68000001, 0x2fffffff, }; Py_ssize_t destlen = 0; return _encode(twobytes, onebyte, dest, destlen, destsize, src, len, 1); }
int _tmain(int argc, _TCHAR* argv[]) { int nChannelCount; int nSampleRate; int nBitrate=128000; int nBitsPerSample=16; int nChannelMode=2; //0-Multichannel, 1-Mono, 2-Stereo, 3-IS, 4-PS, 5-DC int bMPEG4AAC=9; // Let the encoder choice: with enc_aacplus.dll 1.24 was 4 but with 1.26 was MPEG2 int mp4mode = 0; int bNoPS = 0; int bRawPCM = 0; int bSpeech = 0; int bPNS = 0; int nType=1; //0- he, 1 - lc, 2 - he-high, 3-PS DWORD dwBytesRead; _TCHAR szTempDir[MAX_PATH]; char szTempName[MAX_PATH]; char lpPathBuffer[BUFSIZE]; _TCHAR * strOutputFileName = NULL; _TCHAR * strInputFileName = NULL; FILE* hInput=NULL; FILE* hOutput=NULL; wavHeader wav; _tsetlocale(LC_ALL, _T("")); if(argc<2) { showUsage(argv[0]); return ERROR_INSUFFICIENT_ARGS; } strInputFileName = argv[1]; strOutputFileName = argv[2]; // let's parse command line for(int i=3;i<argc;++i) { if(0==_tcscmp(_T("--rawpcm"), argv[i])) { nSampleRate=_tstoi(argv[++i]); nChannelCount=_tstoi(argv[++i]); nBitsPerSample=_tstoi(argv[++i]); bRawPCM=1; continue; } if(0==_tcscmp(_T("--mono"), argv[i])) { nChannelMode=1; continue; } if(0==_tcscmp(_T("--ps"), argv[i])) { bNoPS=1; nType=0; //nChannelMode=4; assigned only if bitrate <=56000 continue; } if(0==_tcscmp(_T("--is"), argv[i])) { nChannelMode=3; continue; } if(0==_tcscmp(_T("--dc"), argv[i])) { nChannelMode=5; continue; } if(0==_tcscmp(_T("--he"), argv[i])) { nType=0; continue; } if(0==_tcscmp(_T("--lc"), argv[i])) { nType=1; continue; } if(0==_tcscmp(_T("--high"), argv[i])) { nType=2; continue; } if(0==_tcscmp(_T("--br"), argv[i])) { nBitrate=_tstoi(argv[++i]); continue; } if(0==_tcscmp(_T("--mpeg2aac"), argv[i])) { bMPEG4AAC=0; continue; } if(0==_tcscmp(_T("--mpeg4aac"), argv[i])) { bMPEG4AAC=1; continue; } if(0==_tcscmp(_T("--speech"), argv[i])) { bSpeech=1; continue; } if(0==_tcscmp(_T("--pns"), argv[i])) { bPNS=1; continue; } } showLogo(); int bStdIn = 0==_tcscmp(_T("-"), strInputFileName)?1:0; // let's open WAV file if(bStdIn) { #ifdef WIN32 setmode(fileno(stdin), O_BINARY); #endif hInput = stdin; } else { hInput = _tfopen(strInputFileName,_T("rb")); } if(!hInput) { printf("Can't open input file!\n"); return ERROR_CANNOT_OPEN_INFILE; }; if(0==bRawPCM) { // let's read WAV HEADER memset(&wav, 0, sizeof(wav)); if(fread(&wav, 1, sizeof(wav), hInput)!=sizeof(wav)) { // Can't read wav header fclose(hInput); printf("Input file must be WAV PCM!\n"); return ERROR_CANNOT_LOAD_DECODER; } if( wav.chunkid!=0x46464952 || wav.rifftype!=0x45564157 || wav.fmt!=0x20746D66 || wav.fmtsize!=0x00000010 || wav.bps!=16 || (wav.srate!=32000 && wav.srate!=44100 && wav.srate!=48000 ) || wav.ccode!=0x0001) { // unsupported or invalid wav format fclose(hInput); printf("Invalid or unsuppored WAV file format (must be 16 bit PCM)\n"); return ERROR_INCOMPATABLE_DECODER; } nBitsPerSample = wav.bps; nChannelCount = wav.nch; nSampleRate = wav.srate; } int bitrates[3][6] = { { //he - mono, stereo, 6ch 8000, 64000, 16000, 128000, 96000, 213335 }, { //lc - mono, stereo, 6ch 8000, 160000, 16000, 320000, 160000, 320000 }, { //hi - mono, stereo, 6ch 64000, 160000, 96000, 256000, 8000, 256000 } }; int maxBitrate=0; int minBitrate=0; switch(nChannelCount) { case 1: minBitrate = bitrates[nType][0]; maxBitrate = bitrates[nType][1]; break; case 2: minBitrate = bitrates[nType][2]; maxBitrate = bitrates[nType][3]; break; default: minBitrate = bitrates[nType][4]; maxBitrate = bitrates[nType][5]; break; } const _TCHAR* channelModes[] = {_T("Multichannel"), _T("Mono"), _T("Stereo"), _T("Independant Stereo"), _T("Parametric Stereo"), _T("Dual Channels")}; const _TCHAR* codecName[] = {_T("HE-AAC"),_T("LC-AAC"),_T("HE-AAC High"),_T("HE-AAC+PS")}; const _TCHAR* BooleanResult[] = {_T("No"), _T("Yes")}; nBitrate = min(max(minBitrate, nBitrate), maxBitrate); nChannelMode = 1==nChannelCount ? 1:((2==nChannelCount) ? ( (nBitrate<=56000 && 0==nType && 1==bNoPS) ? 4:nChannelMode):0); if (4==nChannelMode)// only for printf nType=3; // just to be sure that we have the correct mode _TCHAR *ext = PathFindExtension(strOutputFileName); if(ext) { if(!_tcsicmp(ext,_T(".m4a")) || !_tcsicmp(ext,_T(".mp4"))) mp4mode = 1; } // let's write used config: _tprintf(_T("\nInput file: %s\nOutput file: %s\nSampleRate: %d\nChannelCount: %d\nBitsPerSample: %d\nBitrate: %d\nChannelMode: %s\nEngine: %s\nTune For Speech: %s\nPNS: %s\nMP4 Output: %s\n"), strInputFileName, strOutputFileName, nSampleRate, nChannelCount,nBitsPerSample , nBitrate, channelModes[nChannelMode], codecName[nType], BooleanResult[bSpeech?1:0], BooleanResult[bPNS?1:0], BooleanResult[mp4mode?1:0]); fflush(stdout); if (4==nChannelMode) //back nType=0; //create temp file name #ifdef UNICODE _TCHAR tempFileW[MAX_PATH]; #endif GetTempPath( MAX_PATH, // length of the buffer szTempDir); // buffer for path #ifdef UNICODE GetTempFileName(szTempDir, // directory for temp files NULL, // temp file name prefix 0, // create unique name tempFileW); // buffer for name wcstombs_s(NULL,szTempName,MAX_PATH,tempFileW,(MAX_PATH)*sizeof(_TCHAR)); #else GetTempFileName(szTempDir, // directory for temp files NULL, // temp file name prefix 0, // create unique name szTempName); // buffer for name #endif AudioCoder * encoder=NULL; AudioCoder *(*finishAudio3)(_TCHAR *fn, AudioCoder *c)=NULL; void (*prepareToFinish)(_TCHAR *filename, AudioCoder *coder)=NULL; AudioCoder* (*createAudio3)(int nch, int srate, int bps, unsigned int srct, unsigned int *outt, char *configfile)=NULL; HMODULE encplug = LoadLibrary(_T("enc_aacplus.dll")); if(NULL == encplug) { fclose(hInput); printf("Can't find enc_aacplus.dll!\n"); return ERROR_CANNOT_LOAD_ENCODER; } *(void **)&createAudio3 = (void *)GetProcAddress(encplug, "CreateAudio3"); if( NULL == createAudio3) { FreeLibrary(encplug); fclose(hInput); printf("Can't find CreateAudio3 in enc_aacplus.dll!\n"); return ERROR_CANNOT_LOAD_DECODER; } #ifdef UNICODE *(void **)&finishAudio3=(void *)GetProcAddress(encplug,"FinishAudio3W"); *(void **)&prepareToFinish=(void *)GetProcAddress(encplug,"PrepareToFinishW"); #else *(void **)&finishAudio3=(void *)GetProcAddress(encplug,"FinishAudio3"); *(void **)&prepareToFinish=(void *)GetProcAddress(encplug,"PrepareToFinish"); #endif { //const char* codecSection[] = {"audio_aacplus","audio_aac","audio_aacplushigh"}; FILE * tmp = fopen(szTempName,"wt"); fprintf(tmp, "[audio%s_aac%s]\nsamplerate=%u\nchannelmode=%u\nbitrate=%u\nv2enable=1\nbitstream=%i\nsignallingmode=0\nspeech=%i\npns=%i\n", mp4mode?"_mp4":"",1==nType?"":2==nType?"plushigh":"plus", nSampleRate, nChannelMode, nBitrate, bMPEG4AAC?5:0, bSpeech?1:0, bPNS?1:0); fclose(tmp); unsigned int outt = 0; if (1==nType) outt = mp4mode ? mmioFOURCC('M','4','A',' ') : mmioFOURCC('A','A','C','r'); else if (2==nType) outt = mp4mode ? mmioFOURCC('M','4','A','H') : mmioFOURCC('A','A','C','H'); else outt = mp4mode ? mmioFOURCC('M','4','A','+') : mmioFOURCC('A','A','C','P'); encoder=createAudio3(nChannelCount,nSampleRate, nBitsPerSample ,mmioFOURCC('P','C','M',' '),&outt,szTempName); DeleteFileA(szTempName); } if(NULL==encoder) { FreeLibrary(encplug); fclose(hInput); printf("Can't create encoder!\n"); return ERROR_CANNOT_OPEN_ENCODER; } hOutput = _tfopen(strOutputFileName, _T("wb")); if (!hOutput) { delete encoder; FreeLibrary(encplug); fclose(hInput); printf("Can't create output file!\n"); return ERROR_CANNOT_OPEN_OUTFILE; } // encode printf("Encoding..."); int toRead = (2*nChannelCount*2*1024); toRead = (sizeof(lpPathBuffer)/toRead)*toRead; while(0!=(dwBytesRead = fread(lpPathBuffer, 1, toRead, hInput))) { _encode(hOutput, encoder, dwBytesRead, lpPathBuffer, bMPEG4AAC); } printf("\rFinalizing..."); // finalize encoding if(prepareToFinish) prepareToFinish(strOutputFileName,encoder); _finalize(hOutput, encoder, lpPathBuffer, bMPEG4AAC); fclose(hOutput); if (finishAudio3) finishAudio3(strOutputFileName,encoder); fclose(hInput); delete encoder; FreeLibrary(encplug); if(mp4mode) { struct __stat64 statbuf; if(!_tstat64(strOutputFileName,&statbuf)) { if(!_tfopen_s(&hOutput,strOutputFileName,_T("r+b"))) { optimizeAtoms(hOutput,statbuf.st_size); } if(hOutput) fclose(hOutput); } } // shut down printf("\rDone \n"); return TRANSCODE_FINISHED_OK; }
// TOTEST unsigned CHunpinSegmentor::_push (unsigned ch) { m_pystr.push_back (ch); TSegmentVec::iterator ite = m_segs.size() > 0 ? m_segs.end() - 1 : m_segs.begin() - 1; const unsigned maxStringCount = 6; unsigned syllableCount = 0; unsigned stringCount = 0; for(; ite != m_segs.begin() - 1 ; ite --) { stringCount += (*ite).m_len; syllableCount ++; if (stringCount > maxStringCount) { syllableCount --; break; } } unsigned strlen = m_pystr.size(); unsigned ret; for(int index = syllableCount ; index >= 0; index --) { TSegmentVec::iterator it = m_segs.end() - index; unsigned tmpl; unsigned v; if(index != 0) { if((strlen - (*it).m_start) == 2) { char buf[4]; sprintf(buf, "%c%c", m_pystr[(*it).m_start], m_pystr[(*it).m_start+1]); int startFrom = _encode(buf); if(startFrom >= 0) break; } v = m_pytrie.match_longest (m_pystr.rbegin(), m_pystr.rbegin() + strlen - (*it).m_start, tmpl); if(tmpl == (strlen - (*it).m_start)) { TSegmentVec new_segs(1, TSegment(v, (*it).m_start, tmpl)); m_segs.erase (m_segs.end()-index, m_segs.end()); std::copy (new_segs.rbegin(), new_segs.rend(), back_inserter (m_segs)); break; } } else { v = m_pytrie.match_longest (m_pystr.rbegin(), m_pystr.rbegin() + 1, tmpl); if(tmpl == 0) { IPySegmentor::ESegmentType seg_type; if (ch == '\'' && m_inputBuf.size() > 1) { seg_type = IPySegmentor::SYLLABLE_SEP; } else if (islower (ch)) { seg_type = IPySegmentor::INVALID; } else { seg_type = IPySegmentor::STRING; } ret = m_pystr.size () - 1; m_segs.push_back (TSegment (ch, ret, 1, seg_type)); } else { ret = m_pystr.size () - 1; m_segs.push_back (TSegment (v, ret, 1)); } } } TSegment &last_seg = m_segs.back(); if (m_pGetFuzzySyllablesOp && m_pGetFuzzySyllablesOp->isEnabled()) if ( m_segs.back().m_type == SYLLABLE) _addFuzzySyllables (last_seg); return last_seg.m_start; }
void encode(Json_t *root, Json_encode_ctx *enc) { _encode(&root->root, NULL, 0, enc); }
static int _clipline (SDL_Rect *clip, int x1, int _y1, int x2, int y2, int *outpts) { int left = clip->x; int right = clip->x + clip->w - 1; int top = clip->y; int bottom = clip->y + clip->h - 1; int code1, code2; int draw = 0; int swaptmp; float m; /*slope*/ if (!outpts) { SDL_SetError ("outpts argument NULL"); return 0; } while (1) { code1 = _encode (x1, _y1, left, top, right, bottom); code2 = _encode (x2, y2, left, top, right, bottom); if (ACCEPT (code1, code2)) { draw = 1; break; } else if (REJECT (code1, code2)) break; else { if (INSIDE (code1)) { swaptmp = x2; x2 = x1; x1 = swaptmp; swaptmp = y2; y2 = _y1; _y1 = swaptmp; swaptmp = code2; code2 = code1; code1 = swaptmp; } if (x2 != x1) m = (y2 - _y1) / (float)(x2 - x1); else m = 1.0f; if (code1 & LEFT_EDGE) { _y1 += (int)((left - x1) * m); x1 = left; } else if (code1 & RIGHT_EDGE) { _y1 += (int)((right - x1) * m); x1 = right; } else if (code1 & BOTTOM_EDGE) { if (x2 != x1) x1 += (int)((bottom - _y1) / m); _y1 = bottom; } else if (code1 & TOP_EDGE) { if(x2 != x1) x1 += (int)((top - _y1) / m); _y1 = top; } } } if (draw) { outpts[0] = x1; outpts[1] = _y1; outpts[2] = x2; outpts[3] = y2; } return draw; }