热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

FFmpeg中libswscale库简介及测试代码

libswscale库功能主要包括高度优化的图像缩放、颜色空间和像素格式转换操作。以下是测试代码(test_ffmpeg_libswscale.cpp):#inc

libswscale库功能主要包括高度优化的图像缩放、颜色空间和像素格式转换操作。

以下是测试代码(test_ffmpeg_libswscale.cpp):

#include "funset.hpp"
#include
#include
#include
#include
#include #include #ifdef __cplusplus
extern "C" {
#endif#include
#include
#include #ifdef __cplusplus
}
#endifint test_ffmpeg_libswscale_bgr_yuv()
{
#ifdef _MSC_VERconst char* image_name = "E:/GitCode/OpenCV_Test/test_images/lena.png";
#elseconst char* image_name = "test_images/lena.png";
#endifcv::Mat mat &#61; cv::imread(image_name, 1);if (!mat.data || mat.channels() !&#61; 3) {fprintf(stdout, "fail to read image: %s\n", image_name);return -1;}int width &#61; mat.cols, height &#61; mat.rows;std::unique_ptr data(new unsigned char[width * height * 3 / 2]);std::unique_ptr data2(new unsigned char[width * height * 3 / 2]);{ // bgr --> yuv420pint align &#61; 1;uint8_t *bgr_data[4], *yuv420p_data[4];int bgr_linesize[4], yuv420p_linesize[4];int bytes1 &#61; av_image_alloc(bgr_data, bgr_linesize, width, height, AV_PIX_FMT_BGR24, align);memcpy(bgr_data[0], mat.data, width*height * 3);int bytes2 &#61; av_image_alloc(yuv420p_data, yuv420p_linesize, width, height, AV_PIX_FMT_YUV420P, align);fprintf(stdout, "bgr size: %d, linesize: %d, %d, %d, yuv420p size: %d, linesize: %d, %d, %d\n",bytes1, bgr_linesize[0], bgr_linesize[1], bgr_linesize[2], bytes2, yuv420p_linesize[0], yuv420p_linesize[1], yuv420p_linesize[2]);if (bytes1 <0 || bytes2 <0) {fprintf(stderr, "bgr or yuv420p alloc buffer failed: %d, %d\n", bytes1, bytes2);return -1;}SwsContext* sws_ctx &#61; sws_getContext(width, height, AV_PIX_FMT_BGR24, width, height, AV_PIX_FMT_YUV420P, 0, nullptr, nullptr, nullptr);if (!sws_ctx) {fprintf(stderr, "fail to sws_getContext\n");return -1;}sws_scale(sws_ctx, bgr_data, bgr_linesize, 0, height, yuv420p_data, yuv420p_linesize);#ifdef _MSC_VERconst char* name &#61; "E:/GitCode/OpenCV_Test/test_images/512w_512h.yuv420p";
#elseconst char* name &#61; "test_images/512w_512h.yuv420p";
#endifstd::ofstream fout(name, std::ios::out | std::ios::binary);if (!fout.is_open()) {fprintf(stderr, "fail to open file: %s\n", name);return -1;}memcpy(data.get(), yuv420p_data[0], width*height);memcpy(data.get() &#43; width*height, yuv420p_data[1], width*height / 4);memcpy(data.get() &#43; width*height * 5 / 4, yuv420p_data[2], width*height / 4);fout.write((char*)data.get(), width * height * 3 / 2);fout.close();av_freep(&bgr_data[0]);av_freep(&yuv420p_data[0]);sws_freeContext(sws_ctx);
}{ // yuv420p --> bgr24int align &#61; 1;uint8_t *bgr_data[4], *yuv420p_data[4];int bgr_linesize[4], yuv420p_linesize[4];int bytes1 &#61; av_image_alloc(bgr_data, bgr_linesize, width, height, AV_PIX_FMT_BGR24, align);int bytes2 &#61; av_image_alloc(yuv420p_data, yuv420p_linesize, width, height, AV_PIX_FMT_YUV420P, align);memcpy(yuv420p_data[0], data.get(), width*height);memcpy(yuv420p_data[1], data.get() &#43; width*height, width*height / 4);memcpy(yuv420p_data[2], data.get() &#43; width*height * 5 / 4, width*height / 4);fprintf(stdout, "bgr size: %d, linesize: %d, %d, %d, yuv420p size: %d, linesize: %d, %d, %d\n",bytes1, bgr_linesize[0], bgr_linesize[1], bgr_linesize[2], bytes2, yuv420p_linesize[0], yuv420p_linesize[1], yuv420p_linesize[2]);if (bytes1 <0 || bytes2 <0) {fprintf(stderr, "bgr or yuv420p alloc buffer failed: %d, %d\n", bytes1, bytes2);return -1;}SwsContext* sws_ctx &#61; sws_getContext(width, height, AV_PIX_FMT_YUV420P, width, height, AV_PIX_FMT_BGR24, 0, nullptr, nullptr, nullptr);if (!sws_ctx) {fprintf(stderr, "fail to sws_getContext\n");return -1;}sws_scale(sws_ctx, yuv420p_data, yuv420p_linesize, 0, height, bgr_data, bgr_linesize);#ifdef _MSC_VERconst char* name &#61; "E:/GitCode/OpenCV_Test/test_images/yuv420ptobgr24.jpg";
#elseconst char* name &#61; "test_images/yuv420ptobgr24.jpg";
#endifcv::Mat dst(height, width, CV_8UC3, bgr_data[0]);cv::imwrite(name, dst);av_freep(&bgr_data[0]);av_freep(&yuv420p_data[0]);sws_freeContext(sws_ctx);
}{ // bgr --> nv12int align &#61; 1;uint8_t *bgr_data[4], *nv12_data[4];int bgr_linesize[4], nv12_linesize[4];int bytes1 &#61; av_image_alloc(bgr_data, bgr_linesize, width, height, AV_PIX_FMT_BGR24, align);memcpy(bgr_data[0], mat.data, width*height * 3);int bytes2 &#61; av_image_alloc(nv12_data, nv12_linesize, width, height, AV_PIX_FMT_NV12, align);fprintf(stdout, "bgr size: %d, linesize: %d, %d, %d, nv12 size: %d, linesize: %d, %d, %d\n",bytes1, bgr_linesize[0], bgr_linesize[1], bgr_linesize[2], bytes2, nv12_linesize[0], nv12_linesize[1], nv12_linesize[2]);if (bytes1 <0 || bytes2 <0) {fprintf(stderr, "bgr or nv12 alloc buffer failed: %d, %d\n", bytes1, bytes2);return -1;}SwsContext* sws_ctx &#61; sws_getContext(width, height, AV_PIX_FMT_BGR24, width, height, AV_PIX_FMT_NV12, 0, nullptr, nullptr, nullptr);if (!sws_ctx) {fprintf(stderr, "fail to sws_getContext\n");return -1;}sws_scale(sws_ctx, bgr_data, bgr_linesize, 0, height, nv12_data, nv12_linesize);#ifdef _MSC_VERconst char* name &#61; "E:/GitCode/OpenCV_Test/test_images/512w_512h.nv12";
#elseconst char* name &#61; "test_images/512w_512h.nv12";
#endifstd::ofstream fout(name, std::ios::out | std::ios::binary);if (!fout.is_open()) {fprintf(stderr, "fail to open file: %s\n", name);return -1;}memcpy(data2.get(), nv12_data[0], width*height);memcpy(data2.get() &#43; width*height, nv12_data[1], width*height / 2);fout.write((char*)data2.get(), width * height * 3 / 2);fout.close();av_freep(&bgr_data[0]);av_freep(&nv12_data[0]);sws_freeContext(sws_ctx);
}{ // nv12 --> bgr24int align &#61; 1;uint8_t *bgr_data[4], *nv12_data[4];int bgr_linesize[4], nv12_linesize[4];int bytes1 &#61; av_image_alloc(bgr_data, bgr_linesize, width, height, AV_PIX_FMT_BGR24, align);int bytes2 &#61; av_image_alloc(nv12_data, nv12_linesize, width, height, AV_PIX_FMT_NV12, align);memcpy(nv12_data[0], data2.get(), width*height);memcpy(nv12_data[1], data2.get() &#43; width*height, width*height / 2);fprintf(stdout, "bgr size: %d, linesize: %d, %d, %d, nv12 size: %d, linesize: %d, %d, %d\n",bytes1, bgr_linesize[0], bgr_linesize[1], bgr_linesize[2], bytes2, nv12_linesize[0], nv12_linesize[1], nv12_linesize[2]);if (bytes1 <0 || bytes2 <0) {fprintf(stderr, "bgr or nv12 alloc buffer failed: %d, %d\n", bytes1, bytes2);return -1;}SwsContext* sws_ctx &#61; sws_getContext(width, height, AV_PIX_FMT_NV12, width, height, AV_PIX_FMT_BGR24, 0, nullptr, nullptr, nullptr);if (!sws_ctx) {fprintf(stderr, "fail to sws_getContext\n");return -1;}sws_scale(sws_ctx, nv12_data, nv12_linesize, 0, height, bgr_data, bgr_linesize);#ifdef _MSC_VERconst char* name &#61; "E:/GitCode/OpenCV_Test/test_images/nv12tobgr24.jpg";
#elseconst char* name &#61; "test_images/nv12tobgr24.jpg";
#endifcv::Mat dst(height, width, CV_8UC3, bgr_data[0]);cv::imwrite(name, dst);av_freep(&bgr_data[0]);av_freep(&nv12_data[0]);sws_freeContext(sws_ctx);
}return 0;
}int test_ffmpeg_libswscale_scale()
{// bgr to rgb and resize
#ifdef _MSC_VERconst char* image_name &#61; "E:/GitCode/OpenCV_Test/test_images/lena.png";
#elseconst char* image_name &#61; "test_images/lena.png";
#endifcv::Mat src &#61; cv::imread(image_name, 1); if (!src.data || src.channels() !&#61; 3) {fprintf(stderr, "fail to read image: %s\n", image_name);return -1;}int width_src &#61; src.cols, height_src &#61; src.rows;int width_dst &#61; width_src / 1.5, height_dst &#61; height_src / 1.2;std::unique_ptr data(new uint8_t[width_dst * height_dst * 3]);SwsContext* ctx &#61; sws_getContext(width_src, height_src, AV_PIX_FMT_BGR24, width_dst, height_dst, AV_PIX_FMT_RGB24, SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);if (!ctx) {fprintf(stderr, "fail to sws_getContext\n");return -1;}const uint8_t* p1[1] &#61; {(const uint8_t*)src.data};uint8_t* p2[1] &#61; {data.get()};int src_stride[1] &#61; {width_src * 3};int dst_stride[1] &#61; {width_dst * 3};sws_scale(ctx, p1, src_stride, 0, height_src, p2, dst_stride);
#ifdef _MSC_VERconst char* result_image_name &#61; "E:/GitCode/OpenCV_Test/test_images/lena_resize_rgb_libswscale.png";
#elseconst char* result_image_name &#61; "test_images/lena_resize_rgb_libswscale.png";
#endifcv::Mat dst(height_dst, width_dst, CV_8UC3, (unsigned char*)data.get());cv::imwrite(result_image_name, dst);sws_freeContext(ctx);return 0;
}int test_ffmpeg_libswscale_colorspace()
{fprintf(stdout, "swscale configuration: %s\n", swscale_configuration());fprintf(stdout, "swscale license: %s\n", swscale_license());AVPixelFormat pix_fmt &#61; AV_PIX_FMT_YUV420P;fprintf(stdout, "is supported input:: %d\n", sws_isSupportedInput(pix_fmt));pix_fmt &#61; AV_PIX_FMT_BGR24;fprintf(stdout, "is supported output: %d\n", sws_isSupportedOutput(pix_fmt));pix_fmt &#61; AV_PIX_FMT_GRAY8;fprintf(stdout, "is supported endianness conversion: %d\n", sws_isSupportedEndiannessConversion(pix_fmt));// bgr to gray
#ifdef _MSC_VERconst char* image_name &#61; "E:/GitCode/OpenCV_Test/test_images/lena.png";
#elseconst char* image_name &#61; "test_images/lena.png";
#endifcv::Mat src &#61; cv::imread(image_name, 1); if (!src.data || src.channels() !&#61; 3) {fprintf(stderr, "fail to read image: %s\n", image_name);return -1;}int width &#61; src.cols, height &#61; src.rows;std::unique_ptr data(new uint8_t[width * height]);SwsContext* ctx &#61; sws_getContext(width, height, AV_PIX_FMT_BGR24, width, height, AV_PIX_FMT_GRAY8, 0, nullptr, nullptr, nullptr);if (!ctx) {fprintf(stderr, "fail to sws_getContext\n");return -1;}const uint8_t* p1[1] &#61; {(const uint8_t*)src.data};uint8_t* p2[1] &#61; {data.get()};int src_stride[1] &#61; {width*3};int dst_stride[1] &#61; {width};sws_scale(ctx, p1, src_stride, 0, height, p2, dst_stride);
#ifdef _MSC_VERconst char* result_image_name &#61; "E:/GitCode/OpenCV_Test/test_images/lena_gray_libswscale.png";
#elseconst char* result_image_name &#61; "test_images/lena_gray_libswscale.png";
#endifcv::Mat dst(height, width, CV_8UC1, (unsigned char*)data.get());cv::imwrite(result_image_name, dst);sws_freeContext(ctx);return 0;
}

其中test_ffmpeg_libswscale_scale的执行结果如下&#xff1a;

GitHub&#xff1a;https://github.com/fengbingchun/OpenCV_Test


推荐阅读
  • 题目描述Takuru是一名情报强者,所以他想利用他强大的情报搜集能力来当中间商赚差价。Takuru的计划是让Hinae帮他去市场上买一个商品,然后再以另一个价格卖掉它。Takur ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 本文介绍了使用C++Builder实现获取USB优盘序列号的方法,包括相关的代码和说明。通过该方法,可以获取指定盘符的USB优盘序列号,并将其存放在缓冲中。该方法可以在Windows系统中有效地获取USB优盘序列号,并且适用于C++Builder开发环境。 ... [详细]
  • java io换行符_Java IO:为什么从stdin读取时,换行符的数字表示出现在控制台上?...
    只是为了更好地理解我在讲座中听到的内容(关于Java输入和输出流),我自己做了这个小程序:publicstaticvoidmain(String[]args)thro ... [详细]
  • 796.[APIO2012]派遣在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿。在这个帮派里,有一名忍者被称之为Master。 ... [详细]
  • DescriptionclickmeSolution套路的状压期望DP题。。。考虑倒退期望:设fi,jrolepresentationstyleposi ... [详细]
  • P113:集成日志组件 logback 2彩色日志
    第二步,将控制台的日志改成彩色日志,便于查看修改logback.xml文件。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 最大子序列和(maxsum)【问题描述】输入一个长度为n的整数序列(A1,A2,……,An),从中找出一段连续的长度不超过M的子序列,使得这个序列的和最大。例如:序列1,-3,5 ... [详细]
  • 刚开始crousera上学习<algorithmspart1>但对JAVA实在是不熟。******************************************** ... [详细]
  • APUE学习笔记可变参数(apue中错误输出函数的实现)
    2019独角兽企业重金招聘Python工程师标准voiderr_dump(constchar*fmt,){va_listap;va_start(ap,fmt);初始化 ... [详细]
author-avatar
黑天鹅猪猪_251
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有