static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)

{

    struct video_data *s = s1->priv_data;

    AVStream *st;

    int width, height;

    int res, frame_rate, frame_rate_base;

    uint32_t desired_format, capabilities;



    if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {

        av_log(s1, AV_LOG_ERROR, "Missing/Wrong width, height or framerate\n");



        return -1;

    }



    width = ap->width;

    height = ap->height;

    frame_rate = ap->time_base.den;

    frame_rate_base = ap->time_base.num;



    if((unsigned)width > 32767 || (unsigned)height > 32767) {

        av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);



        return -1;

    }



    st = av_new_stream(s1, 0);

    if (!st) {

        return AVERROR(ENOMEM);

    }

    av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */



    s->width = width;

    s->height = height;

    s->frame_rate      = frame_rate;

    s->frame_rate_base = frame_rate_base;



    capabilities = 0;

    s->fd = device_open(s1, &capabilities);

    if (s->fd < 0) {

        av_free(st);



        return AVERROR(EIO);

    }

    av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);



    desired_format = fmt_ff2v4l(ap->pix_fmt);

    if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {

        int i, done;



        done = 0; i = 0;

        while (!done) {

            desired_format = fmt_conversion_table[i].v4l2_fmt;

            if (device_init(s1, &width, &height, desired_format) < 0) {

                desired_format = 0;

                i++;

            } else {

               done = 1;

            }

            if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {

               done = 1;

            }

        }

    }

    if (desired_format == 0) {

        av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");

        close(s->fd);

        av_free(st);



        return AVERROR(EIO);

    }

    s->frame_format = desired_format;



    if( v4l2_set_parameters( s1, ap ) < 0 )

        return AVERROR(EIO);



    st->codec->pix_fmt = fmt_v4l2ff(desired_format);

    s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);

    if (capabilities & V4L2_CAP_STREAMING) {

        s->io_method = io_mmap;

        res = mmap_init(s1);

        if (res == 0) {

            res = mmap_start(s1);

        }

    } else {

        s->io_method = io_read;

        res = read_init(s1);

    }

    if (res < 0) {

        close(s->fd);

        av_free(st);



        return AVERROR(EIO);

    }

    s->top_field_first = first_field(s->fd);



    st->codec->codec_type = CODEC_TYPE_VIDEO;

    st->codec->codec_id = CODEC_ID_RAWVIDEO;

    st->codec->width = width;

    st->codec->height = height;

    st->codec->time_base.den = frame_rate;

    st->codec->time_base.num = frame_rate_base;

    st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;



    return 0;

}

static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)

{

    struct video_data *s = s1->priv_data;

    AVStream *st;

    int width, height;

    int res, frame_rate, frame_rate_base;

    uint32_t desired_format, capabilities;



    if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {

        av_log(s1, AV_LOG_ERROR, "Missing/Wrong width, height or framerate\n");



        return -1;

    }



    width = ap->width;

    height = ap->height;

    frame_rate = ap->time_base.den;

    frame_rate_base = ap->time_base.num;



    if((unsigned)width > 32767 || (unsigned)height > 32767) {

        av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\n", width, height);



        return -1;

    }



    st = av_new_stream(s1, 0);

    if (!st) {

        return AVERROR(ENOMEM);

    }

    av_set_pts_info(st, 64, 1, 1000000); 



    s->width = width;

    s->height = height;

    s->frame_rate      = frame_rate;

    s->frame_rate_base = frame_rate_base;



    capabilities = 0;

    s->fd = device_open(s1, &capabilities);

    if (s->fd < 0) {

        av_free(st);



        return AVERROR(EIO);

    }

    av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);



    desired_format = fmt_ff2v4l(ap->pix_fmt);

    if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {

        int i, done;



        done = 0; i = 0;

        while (!done) {

            desired_format = fmt_conversion_table[i].v4l2_fmt;

            if (device_init(s1, &width, &height, desired_format) < 0) {

                desired_format = 0;

                i++;

            } else {

               done = 1;

            }

            if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {

               done = 1;

            }

        }

    }

    if (desired_format == 0) {

        av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\n");

        close(s->fd);

        av_free(st);



        return AVERROR(EIO);

    }

    s->frame_format = desired_format;



    if( v4l2_set_parameters( s1, ap ) < 0 )

        return AVERROR(EIO);



    st->codec->pix_fmt = fmt_v4l2ff(desired_format);

    s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);

    if (capabilities & V4L2_CAP_STREAMING) {

        s->io_method = io_mmap;

        res = mmap_init(s1);

        if (res == 0) {

            res = mmap_start(s1);

        }

    } else {

        s->io_method = io_read;

        res = read_init(s1);

    }

    if (res < 0) {

        close(s->fd);

        av_free(st);



        return AVERROR(EIO);

    }

    s->top_field_first = first_field(s->fd);



    st->codec->codec_type = CODEC_TYPE_VIDEO;

    st->codec->codec_id = CODEC_ID_RAWVIDEO;

    st->codec->width = width;

    st->codec->height = height;

    st->codec->time_base.den = frame_rate;

    st->codec->time_base.num = frame_rate_base;

    st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;



    return 0;

}

        av_free(st);         av_free(st);         av_free(st);
static int FUNC_0(AVFormatContext *VAR_0, AVFormatParameters *VAR_1)

{

    struct video_data *VAR_2 = VAR_0->priv_data;

    AVStream *st;

    int VAR_3, VAR_4;

    int VAR_5, VAR_6, VAR_7;

    uint32_t desired_format, capabilities;



    if (VAR_1->VAR_3 <= 0 || VAR_1->VAR_4 <= 0 || VAR_1->time_base.den <= 0) {

        av_log(VAR_0, AV_LOG_ERROR, "Missing/Wrong VAR_3, VAR_4 or framerate\n");



        return -1;

    }



    VAR_3 = VAR_1->VAR_3;

    VAR_4 = VAR_1->VAR_4;

    VAR_6 = VAR_1->time_base.den;

    VAR_7 = VAR_1->time_base.num;



    if((unsigned)VAR_3 > 32767 || (unsigned)VAR_4 > 32767) {

        av_log(VAR_0, AV_LOG_ERROR, "Wrong size %dx%d\n", VAR_3, VAR_4);



        return -1;

    }



    st = av_new_stream(VAR_0, 0);

    if (!st) {

        return AVERROR(ENOMEM);

    }

    av_set_pts_info(st, 64, 1, 1000000); 



    VAR_2->VAR_3 = VAR_3;

    VAR_2->VAR_4 = VAR_4;

    VAR_2->VAR_6      = VAR_6;

    VAR_2->VAR_7 = VAR_7;



    capabilities = 0;

    VAR_2->fd = device_open(VAR_0, &capabilities);

    if (VAR_2->fd < 0) {

        av_free(st);



        return AVERROR(EIO);

    }

    av_log(VAR_0, AV_LOG_INFO, "[%d]Capabilities: %x\n", VAR_2->fd, capabilities);



    desired_format = fmt_ff2v4l(VAR_1->pix_fmt);

    if (desired_format == 0 || (device_init(VAR_0, &VAR_3, &VAR_4, desired_format) < 0)) {

        int VAR_8, VAR_9;



        VAR_9 = 0; VAR_8 = 0;

        while (!VAR_9) {

            desired_format = fmt_conversion_table[VAR_8].v4l2_fmt;

            if (device_init(VAR_0, &VAR_3, &VAR_4, desired_format) < 0) {

                desired_format = 0;

                VAR_8++;

            } else {

               VAR_9 = 1;

            }

            if (VAR_8 == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {

               VAR_9 = 1;

            }

        }

    }

    if (desired_format == 0) {

        av_log(VAR_0, AV_LOG_ERROR, "Cannot find a proper format.\n");

        close(VAR_2->fd);

        av_free(st);



        return AVERROR(EIO);

    }

    VAR_2->frame_format = desired_format;



    if( v4l2_set_parameters( VAR_0, VAR_1 ) < 0 )

        return AVERROR(EIO);



    st->codec->pix_fmt = fmt_v4l2ff(desired_format);

    VAR_2->frame_size = avpicture_get_size(st->codec->pix_fmt, VAR_3, VAR_4);

    if (capabilities & V4L2_CAP_STREAMING) {

        VAR_2->io_method = io_mmap;

        VAR_5 = mmap_init(VAR_0);

        if (VAR_5 == 0) {

            VAR_5 = mmap_start(VAR_0);

        }

    } else {

        VAR_2->io_method = io_read;

        VAR_5 = read_init(VAR_0);

    }

    if (VAR_5 < 0) {

        close(VAR_2->fd);

        av_free(st);



        return AVERROR(EIO);

    }

    VAR_2->top_field_first = first_field(VAR_2->fd);



    st->codec->codec_type = CODEC_TYPE_VIDEO;

    st->codec->codec_id = CODEC_ID_RAWVIDEO;

    st->codec->VAR_3 = VAR_3;

    st->codec->VAR_4 = VAR_4;

    st->codec->time_base.den = VAR_6;

    st->codec->time_base.num = VAR_7;

    st->codec->bit_rate = VAR_2->frame_size * 1/av_q2d(st->codec->time_base) * 8;



    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, AVFormatParameters *VAR_1)
{ struct video_data *VAR_2 = VAR_0->priv_data; AVStream *st; int VAR_3, VAR_4; int VAR_5, VAR_6, VAR_7; uint32_t desired_format, capabilities; if (VAR_1->VAR_3 <= 0 || VAR_1->VAR_4 <= 0 || VAR_1->time_base.den <= 0) { av_log(VAR_0, AV_LOG_ERROR, "Missing/Wrong VAR_3, VAR_4 or framerate\n"); return -1; } VAR_3 = VAR_1->VAR_3; VAR_4 = VAR_1->VAR_4; VAR_6 = VAR_1->time_base.den; VAR_7 = VAR_1->time_base.num; if((unsigned)VAR_3 > 32767 || (unsigned)VAR_4 > 32767) { av_log(VAR_0, AV_LOG_ERROR, "Wrong size %dx%d\n", VAR_3, VAR_4); return -1; } st = av_new_stream(VAR_0, 0); if (!st) { return AVERROR(ENOMEM); } av_set_pts_info(st, 64, 1, 1000000); VAR_2->VAR_3 = VAR_3; VAR_2->VAR_4 = VAR_4; VAR_2->VAR_6      = VAR_6; VAR_2->VAR_7 = VAR_7; capabilities = 0; VAR_2->fd = device_open(VAR_0, &capabilities); if (VAR_2->fd < 0) { av_free(st); return AVERROR(EIO); } av_log(VAR_0, AV_LOG_INFO, "[%d]Capabilities: %x\n", VAR_2->fd, capabilities); desired_format = fmt_ff2v4l(VAR_1->pix_fmt); if (desired_format == 0 || (device_init(VAR_0, &VAR_3, &VAR_4, desired_format) < 0)) { int VAR_8, VAR_9; VAR_9 = 0; VAR_8 = 0; while (!VAR_9) { desired_format = fmt_conversion_table[VAR_8].v4l2_fmt; if (device_init(VAR_0, &VAR_3, &VAR_4, desired_format) < 0) { desired_format = 0; VAR_8++; } else { VAR_9 = 1; } if (VAR_8 == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) { VAR_9 = 1; } } } if (desired_format == 0) { av_log(VAR_0, AV_LOG_ERROR, "Cannot find a proper format.\n"); close(VAR_2->fd); av_free(st); return AVERROR(EIO); } VAR_2->frame_format = desired_format; if( v4l2_set_parameters( VAR_0, VAR_1 ) < 0 )
return AVERROR(EIO); st->codec->pix_fmt = fmt_v4l2ff(desired_format); VAR_2->frame_size = avpicture_get_size(st->codec->pix_fmt, VAR_3, VAR_4); if (capabilities & V4L2_CAP_STREAMING) { VAR_2->io_method = io_mmap; VAR_5 = mmap_init(VAR_0); if (VAR_5 == 0) { VAR_5 = mmap_start(VAR_0); } } else { VAR_2->io_method = io_read; VAR_5 = read_init(VAR_0); } if (VAR_5 < 0) { close(VAR_2->fd); av_free(st); return AVERROR(EIO); } VAR_2->top_field_first = first_field(VAR_2->fd); st->codec->codec_type = CODEC_TYPE_VIDEO; st->codec->codec_id = CODEC_ID_RAWVIDEO; st->codec->VAR_3 = VAR_3; st->codec->VAR_4 = VAR_4; st->codec->time_base.den = VAR_6; st->codec->time_base.num = VAR_7; st->codec->bit_rate = VAR_2->frame_size * 1/av_q2d(st->codec->time_base) * 8; return 0; }