evrc_codec.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <time.h>
  6. #ifdef WIN32
  7. # include <windows.h>
  8. #else
  9. # include <sys/time.h>
  10. #endif
  11. #ifdef WIN32
  12. static int gettimeofday(struct timeval *tp, void *tzp)
  13. {
  14. time_t clock;
  15. struct tm tm;
  16. SYSTEMTIME wtm;
  17. GetLocalTime(&wtm);
  18. tm.tm_year = wtm.wYear - 1900;
  19. tm.tm_mon = wtm.wMonth - 1;
  20. tm.tm_mday = wtm.wDay;
  21. tm.tm_hour = wtm.wHour;
  22. tm.tm_min = wtm.wMinute;
  23. tm.tm_sec = wtm.wSecond;
  24. tm. tm_isdst = -1;
  25. clock = mktime(&tm);
  26. tp->tv_sec = clock;
  27. tp->tv_usec = wtm.wMilliseconds * 1000;
  28. return (0);
  29. }
  30. #endif
  31. #include "evrcc.h"
  32. using namespace std;
  33. int encodefile(const std::string& infile,const std::string& outfile) {
  34. ifstream ifs;
  35. ifs.open(infile.c_str(),ios::in | ios::binary);
  36. if( ifs.fail() ) {
  37. cout << "can not open file " << infile << endl;
  38. return -1;
  39. }
  40. ofstream ofs;
  41. ofs.open(outfile.c_str(),ios::out | ios::binary);
  42. if( ofs.fail() ) {
  43. cout << "can not create file " << outfile << endl;
  44. return -1;
  45. }
  46. unsigned char* evrc = new unsigned char[1024*100];
  47. std::vector<unsigned char> pcm;
  48. typedef std::istreambuf_iterator<char> FsIt;
  49. std::copy(FsIt(ifs),FsIt(),std::back_inserter(pcm));
  50. size_t nFrame = pcm.size()/320;
  51. void* ct = evrc_encoder_init(3,3,1);
  52. struct timeval start;
  53. gettimeofday(&start,NULL);
  54. int r = evrc_encoder_encode_to_stream(ct,(short*)&(pcm[0]),pcm.size()/sizeof(short),evrc,1024*100);
  55. struct timeval end;
  56. gettimeofday(&end,NULL);
  57. long msUse = ( (end.tv_sec - start.tv_sec) * 1000 + (end.tv_usec - start.tv_usec)/1000 );
  58. cout << "encode " << nFrame << " frames for " << nFrame * 20 << "ms, use " << msUse << "ms" <<
  59. " average " << (float)msUse/nFrame << "ms/frame" << endl;
  60. if( r > 0 ) {
  61. ofs.write((char*)evrc,r);
  62. } else {
  63. cout << "no data output at frame " << nFrame << endl;
  64. }
  65. evrc_encoder_uninit(ct);
  66. ifs.close();
  67. ofs.close();
  68. return 0;
  69. }
  70. int decodefile(const std::string& infile,const std::string& outfile) {
  71. ifstream ifs;
  72. ifs.open(infile.c_str(),ios::in | ios::binary);
  73. if( ifs.fail() ) {
  74. cout << "can not open file " << infile << endl;
  75. return -1;
  76. }
  77. ofstream ofs;
  78. ofs.open(outfile.c_str(),ios::out | ios::binary);
  79. if( ofs.fail() ) {
  80. cout << "can not create file " << outfile << endl;
  81. return -1;
  82. }
  83. std::vector<unsigned char> bits;
  84. typedef std::istreambuf_iterator<char> FsIt;
  85. std::copy(FsIt(ifs),FsIt(),std::back_inserter(bits));
  86. if( !bits.empty() ) {
  87. void* ct = evrc_decoder_init();
  88. const int words = evrc_decoder_stream_max_sample(&(bits[0]),bits.size());
  89. short* pcm_buf = new short[words];
  90. int bytes = evrc_decoder_decode_from_stream(ct,&(bits[0]),bits.size(),pcm_buf,words);
  91. if( bytes > 0 ) {
  92. cout << "decode " << bytes / 320 << " frames" << endl;
  93. ofs.write((char*)pcm_buf,bytes);
  94. }
  95. delete [] pcm_buf;
  96. evrc_decoder_uninit(ct);
  97. }
  98. ifs.close();
  99. ofs.close();
  100. return 0;
  101. }
  102. int main(int argc,char* argv[]) {
  103. /*
  104. *cout << "Usage: " << argv[0] << " e speechfile bitstreamfile" << endl;
  105. *cout << "or : " << argv[0] << " d bitstreamfile speechfile" << endl;
  106. *encodefile("/sdcard/pcm.raw","/sdcard/evrc.raw");
  107. *return 0;
  108. */
  109. if( argc < 4 ) {
  110. cout << "Usage: " << argv[0] << " e speechfile bitstreamfile" << endl;
  111. cout << "or : " << argv[0] << " d bitstreamfile speechfile" << endl;
  112. return 0;
  113. }
  114. if( 'd' == argv[1][0] ) {
  115. decodefile(argv[2],argv[3]);
  116. } else if( 'e' == argv[1][0] ) {
  117. encodefile(argv[2],argv[3]);
  118. } else {
  119. std::cout << "unknown opt" << std::endl;
  120. }
  121. std::cout << "half done" << std::endl;
  122. return 0;
  123. }