如果在延迟后调用,则tjDecompress2需要更多时间来执行

时间:2017-03-29 11:47:29

标签: c jpeg mjpeg libjpeg libjpeg-turbo

编写以下代码来测试我系统上libjpeg-turbo的性能

unsigned char *source; 
unsigned int _jpegSize = 0; 
unsigned long long GetTime( void )
{
    struct timeval tv;
    struct timezone tz;

    gettimeofday(&tv, &tz);

    return tv.tv_sec * 1000000ULL + tv.tv_usec;
}
int main(int argc,char**argv)
{
    if(argc < 3)
        return 0;
    char *fileName = argv[1];
    int fileSize = atoi(argv[2]);

    source = (unsigned char*)malloc(fileSize);
    ReadFile(argv[1],fileSize); //Reads the file and copy the data to source buffer

for(int i=0; i<500; i++)
{

    unsigned char* _compressedImage = source;   
    int jpegSubsamp, width, height;
    tjhandle _jpegDecompressor = tjInitDecompress();
    tjDecompressHeader2(_jpegDecompressor, _compressedImage, fileSize, &width, &height, &jpegSubsamp);
    unsigned char dstBuffer[width*height*3]; //!< will contain the decompressed image

    unsigned long long t1 = GetTime();

    tjDecompress2(_jpegDecompressor, _compressedImage, fileSize, dstBuffer, width, 0/*pitch*/, height, TJPF_RGB, TJFLAG_FASTDCT);

    unsigned long long t2 = GetTime();
    printf("TimeTaken: %llu microseconds for %f ms\n", t2-t1,(float)((t2-t1)/(1000))); 

    tjDestroy(_jpegDecompressor);    
    usleep(50000);   
}

它在for循环中重复解码jpeg图像。在我的系统上 tjDecompress2 需要 5-7ms 来解码1024x768图像。问题是如果我在循环结束时添加 usleep(50000)命令, tjDecompress2 的执行时间会增加到 25-28ms 。这种行为的原因是什么以及如何修复它。  基本上我想以20 FPS从USB摄像头读取MJPEG图像并使用libjpeg-turbo对其进行解码,因此添加 usleep(5000)的目的是模拟两个连续帧到达之间的时间< / p>

0 个答案:

没有答案