FFmpeg视频像素格式转换和尺寸转换

优缺点

相比openGL shader转换来说,ffmpeg转换相对简单,但是效率相对较低

函数

1> sws_getContext:获取上下文

2> sws_getCachedContext:获取上下文,与sws_getContext区别在于内存空间由调用者自己管理

    @param SwsContext *context:格式转换上下文

    @param int srcW, int srcH, enum AVPixelFormat srcFormat:输入格式

    @param int dstW, int dstH, enum AVPixelFormat dstFormat:输出格式

    @param  int flags:转换算法,如线性差值算法

    @param  SwsFilter *srcFilter:通常不用

    @param  SwsFilter *dstFilter:通常不用

    @param  const double **param:算法参数,不涉及可不指定

3> sws_scale:转换

    @param SwsContext *context:格式转换上下文

    @param const uint8_t *const srcSlice[]:源数据数组,二维数组数据

    @param const int srcStride[]:一行数据的宽度,linesize

    @param int srcSliceY:用不到

    @param int srcSliceH:图像高度,遍历一行行数据

    @param uint8_t *dst[]:输出数据地址

    @param const int dstStride[]:输出数据长度

    @return:输出的数据高度

4> sws_freeContext:释放上下文空间

示例程序

代码:

void transVideoPixel(SwsContext *ctx, AVFrame *frame)
{
    ctx = sws_getCachedContext(ctx,
                         frame->width, frame->height, (AVPixelFormat)frame->format, // 输入宽高、格式
                         frame->width, frame->height, AV_PIX_FMT_BGRA,  // 输出宽高、格式
                         SWS_BILINEAR, // 临近差值
                         0, 0, 0);
    if (nullptr != ctx) {
        // 一个像素四个字节
        unsigned char *rgb = new unsigned char[frame->width * frame->height * 4];
        uint8_t *dstData[2] = {0};
        dstData[0] = rgb;
        int lines[2] = {0};
        lines[0] = frame->width * 4;
        int retHeight = sws_scale(ctx,
                  frame->data, frame->linesize, 0, frame->height,
                  dstData, lines);
        cout << "sws scaled retHeight = " << retHeight << endl;
        delete []rgb;
        rgb = nullptr;
    }
}

输出:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperYang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值