sw_scale 色彩空间转化

void ConvertYUYV2ToYUV422P()
{
    int w = 640;
    int h = 480;
    uint8_t *in_buf = new uint8_t[614400 + 4];
    uint8_t *out_buf = new uint8_t[614400 + 4];
    
    //auto m_av_input_frame  = avcodec_alloc_frame();
    //auto m_av_output_frame = avcodec_alloc_frame();
    //auto aia_in = av_image_alloc(m_av_input_frame->data, m_av_input_frame->linesize,
    //            w, h, PIX_FMT_YUYV422, 1);
    //auto aia_out = av_image_alloc(m_av_output_frame->data, m_av_output_frame->linesize,
    //            w, h, PIX_FMT_YUV422P, 1);
 
    AVPicture m_av_input_pic;
    AVPicture m_av_output_pic;
    auto one_yuy2_size    = avpicture_get_size(PIX_FMT_YUYV422, w, h);
    auto one_yuv422p_size = avpicture_get_size(PIX_FMT_YUV422P, w, h);
    //avpicture_alloc(&m_av_input_pic,  PIX_FMT_YUYV422, w, h);
    avpicture_alloc(&m_av_output_pic, PIX_FMT_YUV422P, w, h);
 
    std::fstream input_file, output_file;
    input_file.open("e:\\640_480_yuy2.yuv",     std::ios_base::in  | std::ios_base::binary);
    output_file.open("e:\\640_480_yuv422p.yuv", std::ios_base::out | std::ios_base::binary);
 
    auto s_input  = sws_isSupportedInput(PIX_FMT_YUYV422);
    auto s_output = sws_isSupportedOutput(PIX_FMT_YUYV422);
 
    auto sws_ct = sws_getCachedContext(NULL, 
        w, h, PIX_FMT_YUYV422, 
        w, h, PIX_FMT_YUV422P, 
        SWS_BILINEAR, NULL, NULL, NULL);
 
    while (!input_file.eof()) {
        input_file.read((char *)in_buf, one_yuy2_size);
        auto rel_size = input_file.gcount();
        if (rel_size != one_yuy2_size) break;
 
        auto fill_size = avpicture_fill(&m_av_input_pic, in_buf, PIX_FMT_YUYV422, w, h);
        auto sws_rel = sws_scale(sws_ct, m_av_input_pic.data, m_av_input_pic.linesize, 0, h, 
            m_av_output_pic.data, m_av_output_pic.linesize);
 
        auto layout_rel = avpicture_layout(&m_av_output_pic, PIX_FMT_YUV422P, w, h, out_buf, 614404);
        AtlTrace("Fill PIX_FMT_YUY2, Size:[%d], sws_scale return:[%d], layout return:[%d]\n",
            fill_size, sws_rel, layout_rel);
        output_file.write((char *)out_buf, layout_rel);
    }
    input_file.close();
    output_file.close();
    //av_free(m_av_input_frame->data[0]);
    //av_free(m_av_input_frame);
    //av_free(m_av_output_frame->data[0]);
    //av_free(m_av_output_frame);
    //avpicture_free(&m_av_input_pic);
    avpicture_free(&m_av_output_pic);
    sws_freeContext(sws_ct);
 
    delete [] in_buf;
    delete [] out_buf;
}

sw_scale 色彩空间转化

posted @ 2018-12-06 09:00  vkang  阅读(281)  评论(0)    收藏  举报