Beispiel #1
0
int V4LCamera::exec() {
    char header [50];
    sprintf(header,"P6\n%d %d 255\n",_fmt.fmt.pix.width,_fmt.fmt.pix.height);
    unsigned char *dst_buf;
#ifdef _DEBUG_CAPTURE_THREADS
    qDebug() << "[CAMERA_THREAD::V4L_CAMERA] - exec() - Started";
#endif //_DEBUG_CAPTURE_THREADS
    while(true){
        myTimer.start();
        do {
            FD_ZERO(&_fds);
            FD_SET(_fd, &_fds);
            _r = select(_fd + 1, &_fds, NULL, NULL, &_tv);
        } while ((_r == -1 && (errno = EINTR)));
        if (_r == -1)
            qFatal("[CAMERA_THREAD::V4L_CAMERA] - exec() - select() returns error!");
        CLEAR(_buf);
        _buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        _buf.memory = V4L2_MEMORY_MMAP;
        V4LSettings::qioctl(_fd, VIDIOC_DQBUF, &_buf, "V4LCamera::exec()");

        dst_buf = (unsigned char*)malloc(_fmt.fmt.pix.sizeimage);
        if (v4lconvert_convert(_v4lconvert_data, &_src_fmt, &_fmt,
                                (unsigned char*)_buffers[_buf.index].start, _buf.bytesused,
                                dst_buf, _fmt.fmt.pix.sizeimage) < 0) {
#ifdef _DEBUG_CAPTURE_THREADS
            if (errno != EAGAIN)
                qWarning("[CAMERA_THREAD::V4L_CAMERA] - exec() - v4l_convert() returns error");
#endif //_DEBUG_CAPTURE_THREADS
        }
        unsigned char* asil=(unsigned char*)malloc(_fmt.fmt.pix.sizeimage+qstrlen(header));
        memmove(asil, dst_buf, _fmt.fmt.pix.sizeimage);
        memmove(asil+qstrlen(header), asil, _fmt.fmt.pix.sizeimage);
        memcpy(asil,header,qstrlen(header));

        Mat *qq = new Mat(_fmt.fmt.pix.height, _fmt.fmt.pix.width, CV_8UC2, asil);
        if(!qq->empty()) {
            Mat newFrame = qq->clone();
            switch(_fmt.fmt.pix.height) {
                case 720:
                case 800:
                case 960:
                    cvtColor(newFrame, newFrame, CV_YUV2RGB_YUYV);
                    break;
                case 1024:
                    cvtColor(newFrame, newFrame, CV_YUV2BGR_Y422);
                    break;
                default:
                    cvtColor(newFrame, newFrame, CV_YUV2RGB_Y422);
                    break;
            }
            _cvMatbuffer.enqueue(newFrame);
            emit availableFrame();
        }
        free(asil);
        free(dst_buf);
        delete qq;
        V4LSettings::qioctl(_fd, VIDIOC_QBUF, &_buf, "V4LCamera::exec()");
        _fps = 1000.0/myTimer.elapsed();
    }
    return 0;
}
//Replacement algorithm for LRU using stack
int lruAlgorithm(int* refString, int refLength, int frames)
{
    Stack* LRUStack = stackCreate();
    int pageFaults = 0;

    int i = 0;
    //Initialize pointer to memory map
    int **memoryMap = malloc(ARRAY_INIT*sizeof(int));

    // Create 2D Array
    for(i=0; i<refLength; i++)
    {
        memoryMap[i] = (int *)malloc(ARRAY_INIT*sizeof(int));
    }

    int j = 0;
    // Initialize 2D Array with value 0
    for(i=0; i<frames; i++)
    {
        for(j=0; j<refLength; j++)
        {
            memoryMap[i][j] = 0;
        }
    }

    // Populate memory map
    for(j=0; j<refLength; j++)
    {
        // Copy previous contents to current ref string index
        if (j>0)
        {
            for(i=0; i<frames; i++)
            {
                memoryMap[i][j] = memoryMap[i][j-1];
            }
        }

        int freeFrame = availableFrame(memoryMap, refString, j, frames);

        //Frame is available, save contents to open frame
        if(freeFrame >= 0)
        {
            pageFaults++;
            memoryMap[freeFrame][j] = refString[j];
            stackPush(LRUStack, freeFrame);
        }

        //Page fault, use LRU stack to determine victim and save
        if(freeFrame == -1)
        {
            pageFaults++;
            int replaceFrame = stackPop(LRUStack);
            memoryMap[replaceFrame][j] = refString[j];
            stackPush(LRUStack, replaceFrame);
        }

        //Page already in memory
        if(freeFrame == -2)
        {
          int memoryFrame = stackPop(LRUStack);
          stackPush(LRUStack, memoryFrame);
        }

    }

    //Print memory map after population
    for(i=0; i<frames; i++)
    {
        for(j=0; j<refLength; j++)
        {
            printf("%d ", memoryMap[i][j]);
        }
        puts("");
    }

    //Garbage collection
    free(memoryMap);
    free(LRUStack);
    return pageFaults;
}