美文网首页APP端
AVFrame转换yuv420p

AVFrame转换yuv420p

作者: 我们都很努力着 | 来源:发表于2020-09-08 14:04 被阅读0次
uint8_t *AVFrame2Img(AVFrame *pFrame) {
    int frameHeight = pFrame->height;
    int frameWidth = pFrame->width;
    int channels = 3;

    //反转图像
    pFrame->data[0] += pFrame->linesize[0] * (frameHeight - 1);
    pFrame->linesize[0] *= -1;
    pFrame->data[1] += pFrame->linesize[1] * (frameHeight / 2 - 1);
    pFrame->linesize[1] *= -1;
    pFrame->data[2] += pFrame->linesize[2] * (frameHeight / 2 - 1);
    pFrame->linesize[2] *= -1;

    //创建保存yuv数据的buffer
    uint8_t *pDecodedBuffer = (uint8_t *) malloc(
            frameHeight * frameWidth * sizeof(uint8_t) * channels);

    //从AVFrame中获取yuv420p数据,并保存到buffer
    int i, j, k;
    //拷贝y分量
    for (i = 0; i < frameHeight; i++) {
        memcpy(pDecodedBuffer + frameWidth * i,
               pFrame->data[0] + pFrame->linesize[0] * i,
               frameWidth);
    }
    //拷贝u分量
    for (j = 0; j < frameHeight / 2; j++) {
        memcpy(pDecodedBuffer + frameWidth * i + frameWidth / 2 * j,
               pFrame->data[1] + pFrame->linesize[1] * j,
               frameWidth / 2);
    }
    //拷贝v分量
    for (k = 0; k < frameHeight / 2; k++) {
        memcpy(pDecodedBuffer + frameWidth * i + frameWidth / 2 * j + frameWidth / 2 * k,
               pFrame->data[2] + pFrame->linesize[2] * k,
               frameWidth / 2);
    } 
    return pDecodedBuffer;
}

相关文章

网友评论

    本文标题:AVFrame转换yuv420p

    本文链接:https://www.haomeiwen.com/subject/qmgdektx.html