FFmpeg 4.0.2 实现两个YUV序列拼接成一个YUV序列

一、C++代码:

/*
 * 两个YUV拼接成一个YUV
 * FFmpeg4.0.2
 */
int YUVCombine(AVFrame *srcFrame1, AVFrame *srcFrame2, AVFrame *dstFrame, int dstWidth, int dstHeight)
{
    // 合成后得到的帧
    int nDstSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, dstWidth * 2, dstHeight, 1);
    uint8_t *dstbuf = (uint8_t *)av_malloc(nDstSize);
    av_image_fill_arrays(dstFrame->data, dstFrame->linesize, dstbuf, AV_PIX_FMT_YUV420P, dstWidth * 2, dstHeight, 1);

    if (srcFrame1 && srcFrame2)
    {
        int nYIndex = 0, nUVIndex = 0;
        for (int i = 0; i < dstHeight; i++)
        {
            //Y  
            memcpy(dstFrame->data[0] + i * dstWidth * 2, srcFrame1->data[0] + nYIndex * dstWidth, dstWidth);
            memcpy(dstFrame->data[0] + dstWidth + i * dstWidth * 2, srcFrame2->data[0] + nYIndex * dstWidth, dstWidth);
            nYIndex++;
        }
        for (int i = 0; i < dstHeight / 4; i++)
        {
            //U
            memcpy(dstFrame->data[1] + i * dstWidth * 2, srcFrame1->data[1] + nUVIndex * dstWidth, dstWidth);
            memcpy(dstFrame->data[1] + dstWidth + i * dstWidth * 2, srcFrame2->data[1] + nUVIndex * dstWidth, dstWidth);
            //V  
            memcpy(dstFrame->data[2] + i * dstWidth * 2, srcFrame1->data[2] + nUVIndex * dstWidth, dstWidth);
            memcpy(dstFrame->data[2] + dstWidth + i * dstWidth * 2, srcFrame2->data[2] + nUVIndex * dstWidth, dstWidth);
            nUVIndex++;
        }
    }
    return 0;
}

二、实现效果如下图所示:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/hsq1596753614/article/details/82258698
今日推荐