gpt4 book ai didi

ffmpeg - AV_PIX_FMT_YUVJ422P 到 jpeg 的转换

转载 作者:行者123 更新时间:2023-12-04 23:03:00 28 4
gpt4 key购买 nike

我能够将图像从 AV_PIX_FMT_YUVJ422P 转换为 jpeg 格式(在代码下方),但生成的图像在完整的下半部分有绿色阴影,这表明我做错了。
按照我采取的步骤

  • 最初我有来自相机的 AV_PIX_FMT_UYVY422 图像,我已将其转换为 AV_PIX_FMT_YUVJ422P 格式并能够在 http://rawpixels.net/ 上看到此图像网站显示的参数是size 2448X2050, Bpp1= 8,Bpp2 = 8 and Bpp3 = 8,alignment 1, SubSampling H =2, SubSampling V = 1,格式:YUV422P
    所以输入图像是正确的 AV_PIX_FMT_YUVJ422P 格式。 & 还可以在使用 YUV422 格式的“YUV 图像查看器软件”上看到。
  • 现在我正在尝试使用下面的代码将其转换为 jpeg 格式,并附上在完整的下半部分具有绿色阴影的结果图像。
        AVFormatContext*    pFormatCtx;
    AVOutputFormat* fmt;
    AVStream* video_st;
    AVCodecContext* pCodecCtx;
    AVCodec* pCodec;

    uint8_t* picture_buf;
    AVFrame* picture;
    AVPacket pkt;
    int y_size;
    int size;
    int got_picture=0;
    int ret=0;

    int main( int argc, char* argv[] )
    {

    FILE *in_file = NULL;
    unsigned int in_width = 2448;
    unsigned int in_height = 2050;
    const char* out_file = "encoded_pic.jpg";


    in_file = fopen("c:\\test_Planar.yuv","rb");
    if(in_file == NULL) { printf("\n\tFile Opening error...!!"); exit(1); }
    else printf("\n\tYUV File Open Sucessfully...!!\n\n");

    av_register_all(); // Loads the whole database of available codecs and formats.

    pFormatCtx = avformat_alloc_context();
    fmt = NULL;
    fmt = av_guess_format("mjpeg",NULL,NULL);
    pFormatCtx->oformat = fmt;

    //------Output URL-------------------------
    if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0)
    {
    printf("Couldn't open output file.");
    return -1;
    }

    video_st = avformat_new_stream(pFormatCtx, 0);
    if (video_st==NULL) return -1;


    pCodecCtx = video_st->codec;
    pCodecCtx->codec_id = fmt->video_codec;
    pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
    pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ422P;

    //--------------------------MY SOURCE PIXEL FORMAT--------------

    pCodecCtx->width = in_width;
    pCodecCtx->height = in_height;

    pCodecCtx->time_base.num = 1;
    pCodecCtx->time_base.den = 1;//25;

    //Output some information
    av_dump_format(pFormatCtx, 0, out_file, 1);

    // Determine if desired video encoder is installed
    pCodec = avcodec_find_encoder(pCodecCtx->codec_id);

    if (!pCodec)
    {
    printf("Codec not found.");
    return -1;
    }

    printf("\nCodec Identified done\n");

    if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0){
    printf("Could not open codec.\n");
    return -1;
    }
    picture = av_frame_alloc();
    size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
    picture_buf = (uint8_t *)av_malloc(size);
    if (!picture_buf) return -1;

    avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);

    printf("\t\nWrite Header..");
    avformat_write_header(pFormatCtx,NULL);
    y_size = pCodecCtx->width * pCodecCtx->height;
    av_new_packet(&pkt,y_size*3);


    //Read YUV
    if (fread(picture_buf, 1, y_size*3/2, in_file) <=0)
    {
    printf("Could not read input file.");
    return -1;
    }
    //--------------------------------------------input image format UYVY
    picture->data[0] = picture_buf; // Y
    picture->data[1] = picture_buf+ y_size; // U
    picture->data[2] = picture_buf+ y_size*5/4; // V
    //-----------------------------------------------

    printf("\t\n Encode the image..\n");
    ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);
    if(ret < 0)
    {
    printf("Encode Error.\n");
    return -1;
    }

    if (got_picture==1)
    {
    pkt.stream_index = video_st->index;
    ret = av_write_frame(pFormatCtx, &pkt);
    }

    av_free_packet(&pkt);
    //Write Trailer
    av_write_trailer(pFormatCtx);
    printf("Encode Successful.\n");

    if (video_st)
    {
    avcodec_close(video_st->codec);
    av_free(picture);
    av_free(picture_buf);
    }

    avio_close(pFormatCtx->pb);
    avformat_free_context(pFormatCtx);

    fclose(in_file);
    printf("\n\tYUV File Close Sucessfully...!!");
    }

  • Resultant output jpeg encoded image from yuvj422p image having green shade

    最佳答案

    将输入像素格式从 AV_PIX_FMT_YUVJ422P 更改为 AV_PIX_FMT_YUVJ 420 P 解决问题。

    关于ffmpeg - AV_PIX_FMT_YUVJ422P 到 jpeg 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45343691/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com