2017-07-27 128 views
1

我能够像从AV_PIX_FMT_YUVJ422P转换为JPEG格式(下面的代码),但具有完整的上下半区绿荫PLZ建议我在哪里做错了所得到的图像。 以下步骤i已经采取AV_PIX_FMT_YUVJ422P以JPEG转换

  1. 最初我有AV_PIX_FMT_UYVY422图像从相机,我有其转换在AV_PIX_FMT_YUVJ422P格式和能够看到http://rawpixels.net/通过网站所示的参数此图像尺寸2448X2050,BPP1 = 8,BPP2 = 8和Bpp3 = 8,对准1,子采样H = 2,以及子采样V = 1,格式:YUV422P 所以输入图像是正确AV_PIX_FMT_YUVJ422P格式。 &还能够看到“YUV图像浏览软件”使用YUV422格式。

  2. 现在我尝试使用下面的代码,将其转换为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主(INT的argc,字符* 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

回答

0

改变从AV_PIX_FMT_YUVJ422P输入像素格式到AV_PIX_FMT_YUVJ P解决问题。