fprintf(stderr, "Fatal: grab device does not handle capture\n");
av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap){    VideoData *s = s1->priv_data;    AVStream *st;    int width, height;    int video_fd, frame_size;    int ret, frame_rate, frame_rate_base;    int desired_palette;    struct video_tuner tuner;    struct video_audio audio;    const char *video_device;    if (!ap || ap->width <= 0 || ap->height <= 0 || ap->frame_rate <= 0)        return -1;        width = ap->width;    height = ap->height;    frame_rate      = ap->frame_rate;    frame_rate_base = ap->frame_rate_base;    st = av_new_stream(s1, 0);    if (!st)        return -ENOMEM;    s->width = width;    s->height = height;    s->frame_rate      = frame_rate;    s->frame_rate_base = frame_rate_base;    video_device = ap->device;    if (!video_device)        video_device = "/dev/video";    video_fd = open(video_device, O_RDWR);    if (video_fd < 0) {        perror(video_device);        goto fail;    }        if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) {        perror("VIDIOCGCAP");        goto fail;    }    if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {        fprintf(stderr, "Fatal: grab device does not handle capture\n");        goto fail;    }    desired_palette = -1;    if (st->codec.pix_fmt == PIX_FMT_YUV420P) {        desired_palette = VIDEO_PALETTE_YUV420P;    } else if (st->codec.pix_fmt == PIX_FMT_YUV422) {        desired_palette = VIDEO_PALETTE_YUV422;    } else if (st->codec.pix_fmt == PIX_FMT_BGR24) {        desired_palette = VIDEO_PALETTE_RGB24;    }        /* set tv standard */    if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {	if (!strcasecmp(ap->standard, "pal"))	    tuner.mode = VIDEO_MODE_PAL;	else if (!strcasecmp(ap->standard, "secam"))	    tuner.mode = VIDEO_MODE_SECAM;	else	    tuner.mode = VIDEO_MODE_NTSC;	ioctl(video_fd, VIDIOCSTUNER, &tuner);    }        /* unmute audio */    audio.audio = 0;    ioctl(video_fd, VIDIOCGAUDIO, &audio);    memcpy(&s->audio_saved, &audio, sizeof(audio));    audio.flags &= ~VIDEO_AUDIO_MUTE;    ioctl(video_fd, VIDIOCSAUDIO, &audio);    ret = ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers);    if (ret < 0) {        /* try to use read based access */        struct video_window win;        struct video_picture pict;        int val;        win.x = 0;        win.y = 0;        win.width = width;        win.height = height;        win.chromakey = -1;        win.flags = 0;        ioctl(video_fd, VIDIOCSWIN, &win);        ioctl(video_fd, VIDIOCGPICT, &pict);#if 0        printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",               pict.colour,               pict.hue,               pict.brightness,               pict.contrast,               pict.whiteness);#endif                /* try to choose a suitable video format */        pict.palette = desired_palette;        if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {            pict.palette=VIDEO_PALETTE_YUV420P;            ret = ioctl(video_fd, VIDIOCSPICT, &pict);            if (ret < 0) {                pict.palette=VIDEO_PALETTE_YUV422;                ret = ioctl(video_fd, VIDIOCSPICT, &pict);                if (ret < 0) {                    pict.palette=VIDEO_PALETTE_RGB24;                    ret = ioctl(video_fd, VIDIOCSPICT, &pict);                    if (ret < 0)                         goto fail1;                }            }        }        s->frame_format = pict.palette;        val = 1;        ioctl(video_fd, VIDIOCCAPTURE, &val);        s->time_frame = av_gettime();        s->use_mmap = 0;                /* ATI All In Wonder automatic activation */        if (!strcmp(s->video_cap.name, "Km")) {            if (aiw_init(s) < 0)                goto fail;            s->aiw_enabled = 1;            /* force 420P format because convertion from YUV422 to YUV420P               is done in this driver (ugly) */            s->frame_format = VIDEO_PALETTE_YUV420P;        }    } else {        s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);        if ((unsigned char*)-1 == s->video_buf) {            perror("mmap");            goto fail;        }        s->gb_frame = 0;        s->time_frame = av_gettime();                /* start to grab the first frame */        s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;        s->gb_buf.height = height;        s->gb_buf.width = width;        s->gb_buf.format = desired_palette;        if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf)) < 0) {            s->gb_buf.format = VIDEO_PALETTE_YUV420P;                        ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);            if (ret < 0 && errno != EAGAIN) {                /* try YUV422 */                s->gb_buf.format = VIDEO_PALETTE_YUV422;                                ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);                if (ret < 0 && errno != EAGAIN) {                    /* try RGB24 */                    s->gb_buf.format = VIDEO_PALETTE_RGB24;                    ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);                }            }        }        if (ret < 0) {            if (errno != EAGAIN) {            fail1:                fprintf(stderr, "Fatal: grab device does not support suitable format\n");            } else {                fprintf(stderr,"Fatal: grab device does not receive any video signal\n");            }            goto fail;        }        s->frame_format = s->gb_buf.format;        s->use_mmap = 1;    }    switch(s->frame_format) {    case VIDEO_PALETTE_YUV420P:        frame_size = (width * height * 3) / 2;        st->codec.pix_fmt = PIX_FMT_YUV420P;        break;    case VIDEO_PALETTE_YUV422:        frame_size = width * height * 2;        st->codec.pix_fmt = PIX_FMT_YUV422;        break;    case VIDEO_PALETTE_RGB24:        frame_size = width * height * 3;        st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */        break;    default:        goto fail;    }    s->fd = video_fd;    s->frame_size = frame_size;        st->codec.codec_type = CODEC_TYPE_VIDEO;    st->codec.codec_id = CODEC_ID_RAWVIDEO;    st->codec.width = width;    st->codec.height = height;    st->codec.frame_rate      = frame_rate;    st->codec.frame_rate_base = frame_rate_base;        av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */    return 0; fail:    if (video_fd >= 0)        close(video_fd);    av_free(st);    return -EIO;}