int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
{
    int w2;
    const PixFmtInfo *pinfo;
    memset(picture->linesize, 0, sizeof(picture->linesize));
    pinfo = &pix_fmt_info[pix_fmt];
    switch(pix_fmt) {
    case PIX_FMT_YUV420P:
    case PIX_FMT_YUV422P:
    case PIX_FMT_YUV444P:
    case PIX_FMT_YUV410P:
    case PIX_FMT_YUV411P:
    case PIX_FMT_YUV440P:
    case PIX_FMT_YUVJ420P:
    case PIX_FMT_YUVJ422P:
    case PIX_FMT_YUVJ444P:
    case PIX_FMT_YUVJ440P:
        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
        picture->linesize[0] = width;
        picture->linesize[1] = w2;
        picture->linesize[2] = w2;
        break;
    case PIX_FMT_YUVA420P:
        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
        picture->linesize[0] = width;
        picture->linesize[1] = w2;
        picture->linesize[2] = w2;
        picture->linesize[3] = width;
        break;
    case PIX_FMT_NV12:
    case PIX_FMT_NV21:
        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
        picture->linesize[0] = width;
        picture->linesize[1] = w2;
        break;
    case PIX_FMT_RGB24:
    case PIX_FMT_BGR24:
        picture->linesize[0] = width * 3;
        break;
    case PIX_FMT_RGB32:
    case PIX_FMT_BGR32:
    case PIX_FMT_RGB32_1:
    case PIX_FMT_BGR32_1:
        picture->linesize[0] = width * 4;
        break;
    case PIX_FMT_GRAY16BE:
    case PIX_FMT_GRAY16LE:
    case PIX_FMT_BGR555:
    case PIX_FMT_BGR565:
    case PIX_FMT_RGB555:
    case PIX_FMT_RGB565:
    case PIX_FMT_YUYV422:
        picture->linesize[0] = width * 2;
        break;
    case PIX_FMT_UYVY422:
        picture->linesize[0] = width * 2;
        break;
    case PIX_FMT_UYYVYY411:
        picture->linesize[0] = width + width/2;
        break;
    case PIX_FMT_RGB8:
    case PIX_FMT_BGR8:
    case PIX_FMT_RGB4_BYTE:
    case PIX_FMT_BGR4_BYTE:
    case PIX_FMT_GRAY8:
        picture->linesize[0] = width;
        break;
    case PIX_FMT_RGB4:
    case PIX_FMT_BGR4:
        picture->linesize[0] = width / 2;
        break;
    case PIX_FMT_MONOWHITE:
    case PIX_FMT_MONOBLACK:
        picture->linesize[0] = (width + 7) >> 3;
        break;
    case PIX_FMT_PAL8:
        picture->linesize[0] = width;
        picture->linesize[1] = 4;
        break;
    default:
        return -1;
    }
    return 0;
}
['static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)\n{\n    struct video_data *s = s1->priv_data;\n    AVStream *st;\n    int width, height;\n    int res, frame_rate, frame_rate_base;\n    uint32_t desired_format, capabilities;\n    if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {\n        av_log(s1, AV_LOG_ERROR, "Missing/Wrong width, height or framerate\\n");\n        return -1;\n    }\n    width = ap->width;\n    height = ap->height;\n    frame_rate = ap->time_base.den;\n    frame_rate_base = ap->time_base.num;\n    if((unsigned)width > 32767 || (unsigned)height > 32767) {\n        av_log(s1, AV_LOG_ERROR, "Wrong size %dx%d\\n", width, height);\n        return -1;\n    }\n    st = av_new_stream(s1, 0);\n    if (!st) {\n        return AVERROR(ENOMEM);\n    }\n    av_set_pts_info(st, 64, 1, 1000000);\n    s->width = width;\n    s->height = height;\n    s->frame_rate      = frame_rate;\n    s->frame_rate_base = frame_rate_base;\n    capabilities = 0;\n    s->fd = device_open(s1, &capabilities);\n    if (s->fd < 0) {\n        av_free(st);\n        return AVERROR(EIO);\n    }\n    av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\\n", s->fd, capabilities);\n    desired_format = fmt_ff2v4l(ap->pix_fmt);\n    if (desired_format == 0 || (device_init(s1, &width, &height, desired_format) < 0)) {\n        int i, done;\n        done = 0; i = 0;\n        while (!done) {\n            desired_format = fmt_conversion_table[i].v4l2_fmt;\n            if (device_init(s1, &width, &height, desired_format) < 0) {\n                desired_format = 0;\n                i++;\n            } else {\n               done = 1;\n            }\n            if (i == sizeof(fmt_conversion_table) / sizeof(struct fmt_map)) {\n               done = 1;\n            }\n        }\n    }\n    if (desired_format == 0) {\n        av_log(s1, AV_LOG_ERROR, "Cannot find a proper format.\\n");\n        close(s->fd);\n        av_free(st);\n        return AVERROR(EIO);\n    }\n    s->frame_format = desired_format;\n    if( v4l2_set_parameters( s1, ap ) < 0 )\n        return AVERROR(EIO);\n    st->codec->pix_fmt = fmt_v4l2ff(desired_format);\n    s->frame_size = avpicture_get_size(st->codec->pix_fmt, width, height);\n    if (capabilities & V4L2_CAP_STREAMING) {\n        s->io_method = io_mmap;\n        res = mmap_init(s1);\n        if (res == 0) {\n            res = mmap_start(s1);\n        }\n    } else {\n        s->io_method = io_read;\n        res = read_init(s1);\n    }\n    if (res < 0) {\n        close(s->fd);\n        av_free(st);\n        return AVERROR(EIO);\n    }\n    s->top_field_first = first_field(s->fd);\n    st->codec->codec_type = CODEC_TYPE_VIDEO;\n    st->codec->codec_id = CODEC_ID_RAWVIDEO;\n    st->codec->width = width;\n    st->codec->height = height;\n    st->codec->time_base.den = frame_rate;\n    st->codec->time_base.num = frame_rate_base;\n    st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;\n    return 0;\n}', 'void av_set_pts_info(AVStream *s, int pts_wrap_bits,\n                     int pts_num, int pts_den)\n{\n    s->pts_wrap_bits = pts_wrap_bits;\n    s->time_base.num = pts_num;\n    s->time_base.den = pts_den;\n}', 'static enum PixelFormat fmt_v4l2ff(uint32_t pix_fmt)\n{\n    int i;\n    for (i = 0; i < sizeof(fmt_conversion_table) / sizeof(struct fmt_map); i++) {\n        if (fmt_conversion_table[i].v4l2_fmt == pix_fmt) {\n            return fmt_conversion_table[i].ff_fmt;\n        }\n    }\n    return -1;\n}', 'int avpicture_get_size(int pix_fmt, int width, int height)\n{\n    AVPicture dummy_pict;\n    return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);\n}', 'int avpicture_fill(AVPicture *picture, uint8_t *ptr,\n                   int pix_fmt, int width, int height)\n{\n    if(avcodec_check_dimensions(NULL, width, height))\n        return -1;\n    if (ff_fill_linesize(picture, pix_fmt, width))\n        return -1;\n    return ff_fill_pointer(picture, ptr, pix_fmt, height);\n}', 'int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)\n{\n    int w2;\n    const PixFmtInfo *pinfo;\n    memset(picture->linesize, 0, sizeof(picture->linesize));\n    pinfo = &pix_fmt_info[pix_fmt];\n    switch(pix_fmt) {\n    case PIX_FMT_YUV420P:\n    case PIX_FMT_YUV422P:\n    case PIX_FMT_YUV444P:\n    case PIX_FMT_YUV410P:\n    case PIX_FMT_YUV411P:\n    case PIX_FMT_YUV440P:\n    case PIX_FMT_YUVJ420P:\n    case PIX_FMT_YUVJ422P:\n    case PIX_FMT_YUVJ444P:\n    case PIX_FMT_YUVJ440P:\n        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;\n        picture->linesize[0] = width;\n        picture->linesize[1] = w2;\n        picture->linesize[2] = w2;\n        break;\n    case PIX_FMT_YUVA420P:\n        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;\n        picture->linesize[0] = width;\n        picture->linesize[1] = w2;\n        picture->linesize[2] = w2;\n        picture->linesize[3] = width;\n        break;\n    case PIX_FMT_NV12:\n    case PIX_FMT_NV21:\n        w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;\n        picture->linesize[0] = width;\n        picture->linesize[1] = w2;\n        break;\n    case PIX_FMT_RGB24:\n    case PIX_FMT_BGR24:\n        picture->linesize[0] = width * 3;\n        break;\n    case PIX_FMT_RGB32:\n    case PIX_FMT_BGR32:\n    case PIX_FMT_RGB32_1:\n    case PIX_FMT_BGR32_1:\n        picture->linesize[0] = width * 4;\n        break;\n    case PIX_FMT_GRAY16BE:\n    case PIX_FMT_GRAY16LE:\n    case PIX_FMT_BGR555:\n    case PIX_FMT_BGR565:\n    case PIX_FMT_RGB555:\n    case PIX_FMT_RGB565:\n    case PIX_FMT_YUYV422:\n        picture->linesize[0] = width * 2;\n        break;\n    case PIX_FMT_UYVY422:\n        picture->linesize[0] = width * 2;\n        break;\n    case PIX_FMT_UYYVYY411:\n        picture->linesize[0] = width + width/2;\n        break;\n    case PIX_FMT_RGB8:\n    case PIX_FMT_BGR8:\n    case PIX_FMT_RGB4_BYTE:\n    case PIX_FMT_BGR4_BYTE:\n    case PIX_FMT_GRAY8:\n        picture->linesize[0] = width;\n        break;\n    case PIX_FMT_RGB4:\n    case PIX_FMT_BGR4:\n        picture->linesize[0] = width / 2;\n        break;\n    case PIX_FMT_MONOWHITE:\n    case PIX_FMT_MONOBLACK:\n        picture->linesize[0] = (width + 7) >> 3;\n        break;\n    case PIX_FMT_PAL8:\n        picture->linesize[0] = width;\n        picture->linesize[1] = 4;\n        break;\n    default:\n        return -1;\n    }\n    return 0;\n}']