.type & VID_TYPE_MONOCHROME)
        viddev->v4l_fmt = VIDEO_PALETTE_GREY;

    if (input!= IN_DEFAULT) {
        memset(&vid_chnl, 0, sizeof(struct video_channel));
        vid_chnl.channel = input;

        if (ioctl (dev, VIDIOCGCHAN, &vid_chnl) == -1) {
            motion_log(LOG_ERR, 1, "ioctl (VIDIOCGCHAN)");
        } else {
            vid_chnl.channel = input;
            vid_chnl.norm    = norm;
            if (ioctl (dev, VIDIOCSCHAN, &vid_chnl) == -1) {
                motion_log(LOG_ERR, 1, "ioctl (VIDIOCSCHAN)");
                return NULL;
            }
        }
    }

    if (freq) {
        memset(&vid_tuner, 0, sizeof(struct video_tuner));
        vid_tuner.tuner = tuner_number;
        if (ioctl (dev, VIDIOCGTUNER, &vid_tuner) == -1) {
            motion_log(LOG_ERR, 1, "ioctl (VIDIOCGTUNER)");
        } else {
            if (vid_tuner.flags & VIDEO_TUNER_LOW) {
                freq = freq * 16; /* steps of 1/16 KHz */
            } else {
                freq = (freq * 10) / 625;
            }

            if (ioctl(dev, VIDIOCSFREQ, &freq) == -1) {
                motion_log(LOG_ERR, 1, "ioctl (VIDIOCSFREQ)");
                return NULL;
            }

            if (cnt->conf.setup_mode)
                motion_log(-1, 0, "Frequency set");
        }
    }

    if (ioctl (dev, VIDIOCGMBUF, &vid_buf) == -1) {
        motion_log(LOG_ERR, 0, "ioctl(VIDIOCGMBUF) - Error device does not support memory map");
        motion_log(LOG_ERR, 0, "V4L capturing using read is deprecated!");
        motion_log(LOG_ERR, 0, "Motion only supports mmap.");
        return NULL;
    } else {
        map = mmap(0, vid_buf.size, PROT_READ|PROT_WRITE, MAP_SHARED, dev, 0);
        viddev->size_map = vid_buf.size;

        if (vid_buf.frames > 1) {
            viddev->v4l_maxbuffer = 2;
            viddev->v4l_buffers[0] = map;
            viddev->v4l_buffers[1] = (unsigned char *)map + vid_buf.offsets[1];
        } else {
            viddev->v4l_buffers[0] = map;
            viddev->v4l_maxbuffer = 1;
        }

        if (MAP_FAILED == map) {
            motion_log(LOG_ERR,1,"MAP_FAILED");
            return NULL;
        }

        viddev->v4l_curbuffer = 0;
        vid_mmap.format = viddev->v4l_fmt;
        vid_mmap.frame = viddev->v4l_curbuffer;
        vid_mmap.width = width;
        vid_mmap.height = height;
        if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) {
            motion_log(LOG_DEBUG, 1, "Failed with YUV420P, trying YUV422 palette");
            viddev->v4l_fmt = VIDEO_PALETTE_YUV422;
            vid_mmap.format = viddev->v4l_fmt;

            /* Try again... */
            if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) {
                motion_log(LOG_DEBUG, 1, "Failed with YUV422, trying YUYV palette");
                viddev->v4l_fmt = VIDEO_PALETTE_YUYV;
                vid_mmap.format = viddev->v4l_fmt;
                
                if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) {
                    motion_log(LOG_DEBUG, 1, "Failed with YUYV, trying RGB24 palette");
                    viddev->v4l_fmt = VIDEO_PALETTE_RGB24;
                    vid_mmap.format = viddev->v4l_fmt;

                    /* Try again... */
                    if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) {
                        motion_log(LOG_DEBUG, 1, "Failed with RGB24, trying GREYSCALE palette");
                        viddev->v4l_fmt = VIDEO_PALETTE_GREY;
                        vid_mmap.format = viddev->v4l_fmt;

                        /* Try one last time... */
                        if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) {
                            motion_log(LOG_ERR, 1, "Failed with all supported palettes "
                                                    "- giving up");
                            return NULL;
                        }
                    }
                }
            }
        }
    }

    switch (viddev->v4l_fmt) {
    case VIDEO_PALETTE_YUV420P:
        viddev->v4l_bufsize = (width * height * 3) / 2;
        motion_log(LOG_DEBUG, 0, "Using VIDEO_PALETTE_YUV420P palette");
        break;
    case VIDEO_PALETTE_YUV422:
        viddev->v4l_bufsize = (width * height * 2);
        motion_log(LOG_DEBUG, 0, "Using VIDEO_PALETTE_YUV422 palette");
        break;
    case VIDEO_PALETTE_YUYV:
        viddev->v4l_bufsize = (width * height * 2);
        motion_log(LOG_DEBUG, 0, "Using VIDEO_PALETTE_YUYV palette");
        break;
    case VIDEO_PALETTE_RGB24:
        viddev->v4l_bufsize = (width * height * 3);
        motion_log(LOG_DEBUG, 0, "Using VIDEO_PALETTE_RGB24 palette");
        break;
    case VIDEO_PALETTE_GREY:
        viddev->v4l_bufsize = width * height;
        motion_log(LOG_DEBUG, 0, "Using VIDEO_PALETTE_GREY palette");
        break;
    }

    return map;
}


/**
 * v4l_next
 *                v4l_next fetches a video frame from a v4l device
 *
 * Parameters:
 *     viddev     Pointer to struct containing video device handle amd device parameters
 *     map        Pointer to the buffer in which the function puts the new image
 *     width      Width of image in pixels
 *     height     Height of image in pixels
 *
 * Returns
 *     0               Success
 *    V4L_FATAL_ERROR  Fatal error
 *    Positive with bit 0 set and bit 1 unset
 *                     Non fatal error (not implemented)
 */
int v4l_next(struct video_dev *viddev, unsigned char *map, int width, int height)
{
    int dev = viddev->fd;
    int frame = viddev->v4l_curbuffer;
    struct video_mmap vid_mmap;
    unsigned char *cap_map;

    sigset_t set, old;

    /* MMAP method is used */
    vid_mmap.format = viddev->v4l_fmt;
    vid_mmap.width = width;
    vid_mmap.height = height;

    /* Block signals during IOCTL */
    sigemptyset(&set);
    sigaddset(&set, SIGCHLD);
    sigaddset(&set, SIGALRM);
    sigaddset(&set, SIGUSR1);
    sigaddset(&set, SIGTERM);
    sigaddset(&set, SIGHUP);
    pthread