一.实现图像数据格式转换与图像缩放的三个重要函数

ffmpeg实现图像数据格式的转换以及图片的缩放的功能,主要使用swscale.h中的三个函数:
sws_getContext()
sws_scale()
sws_freeContext()
这三个函数的定义如下:
1.sws_getContext() :


/*** Allocate and return an SwsContext. You need it to perform* scaling/conversion operations using sws_scale().** @param srcW the width of the source image* @param srcH the height of the source image* @param srcFormat the source image format* @param dstW the width of the destination image* @param dstH the height of the destination image* @param dstFormat the destination image format* @param flags specify which algorithm and options to use for rescaling* @param param extra parameters to tune the used scaler*              For SWS_BICUBIC param[0] and [1] tune the shape of the basis*              function, param[0] tunes f(1) and param[1] f麓(1)*              For SWS_GAUSS param[0] tunes the exponent and thus cutoff*              frequency*              For SWS_LANCZOS param[0] tunes the width of the window function* @return a pointer to an allocated context, or NULL in case of error* @note this function is to be removed after a saner alternative is*       written*/
struct SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,int dstW, int dstH, enum AVPixelFormat dstFormat,int flags, SwsFilter *srcFilter,SwsFilter *dstFilter, const double *param);

参数介绍

/*
* @param srcW源图像的宽度
* @param srcH源图像的高度
* @param srcFormat源图像格式
* @param dstW目标图像的宽度
* @param dstH目标图像的高度
* @param dstFormat目标图像格式
* @后面三个参数一般都置为空
* @返回指向分配的上下文的指针,或在出错的情况下为NULL
* /

2.sws_scale()

/*** Scale the image slice in srcSlice and put the resulting scaled* slice in the image in dst. A slice is a sequence of consecutive* rows in an image.** Slices have to be provided in sequential order, either in* top-bottom or bottom-top order. If slices are provided in* non-sequential order the behavior of the function is undefined.** @param c         the scaling context previously created with*                  sws_getContext()* @param srcSlice  the array containing the pointers to the planes of*                  the source slice* @param srcStride the array containing the strides for each plane of*                  the source image* @param srcSliceY the position in the source image of the slice to*                  process, that is the number (counted starting from*                  zero) in the image of the first row of the slice* @param srcSliceH the height of the source slice, that is the number*                  of rows in the slice* @param dst       the array containing the pointers to the planes of*                  the destination image* @param dstStride the array containing the strides for each plane of*                  the destination image* @return          the height of the output slice*/
int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[],const int srcStride[], int srcSliceY, int srcSliceH,uint8_t *const dst[], const int dstStride[]);

参数介绍:
/* @param c sws_getContext()返回的用于图像格式转换和图像缩放的上下文环境
* @param srcSlice 包含源图像数据的数组,它是一个包含多通道数据的二维数组,对于yuv而言,我们会用到 * @它的srcSlice [0],srcSlice [1],srcSlice [2]
* @param srcStride 步幅,可以理解为图像的行宽
* @param srcSliceY 开始处理的在原图像中的横坐标的位置,如果是从头开始,那么此处为0
* @param srcSliceH 开始处理的在原图像中的纵坐标的位置,如果是从头开始,那么此处为0
* @param dst 输出的图像数据
* @param dstStride 输出的图像数据的宽度
* @返回输出图像的高度
* /
3.sws_freeContext()


/*** Free the swscaler context swsContext.* If swsContext is NULL, then does nothing.*/
void sws_freeContext(struct SwsContext *swsContext);

参数介绍:
唯一的一个参数,就是 sws_getContext()返回的用于图像格式转换和图像缩放的上下文环境

三个函数的关系

其中,我们可以把sws_getContext() 看成初始化函数,把sws_freeContext()看成结束函数。这两个函数分别再起始和结束的时候各执行一次即可。真正主要的函数是sws_scale(),它是图像数据格式转换与图像缩放的执行函数。

例程

ffmpeg中已经提供了一个例子,路径为doc/examples/scaling_video.c。
这个程序不长,全部贴出来:

#include <libavutil/imgutils.h>
#include <libavutil/parseutils.h>
#include <libswscale/swscale.h>static void fill_yuv_image(uint8_t *data[4], int linesize[4],int width, int height, int frame_index)
{int x, y;/* Y */for (y = 0; y < height; y++)for (x = 0; x < width; x++)data[0][y * linesize[0] + x] = x + y + frame_index * 3;/* Cb and Cr */for (y = 0; y < height / 2; y++) {for (x = 0; x < width / 2; x++) {data[1][y * linesize[1] + x] = 128 + y + frame_index * 2;data[2][y * linesize[2] + x] = 64 + x + frame_index * 5;}}
}int main(int argc, char **argv)
{uint8_t *src_data[4], *dst_data[4];int src_linesize[4], dst_linesize[4];int src_w = 320, src_h = 240, dst_w, dst_h;enum AVPixelFormat src_pix_fmt = AV_PIX_FMT_YUV420P, dst_pix_fmt = AV_PIX_FMT_RGB24;const char *dst_size = NULL;const char *dst_filename = NULL;FILE *dst_file;int dst_bufsize;struct SwsContext *sws_ctx;int i, ret;if (argc != 3) {fprintf(stderr, "Usage: %s output_file output_size\n""API example program to show how to scale an image with libswscale.\n""This program generates a series of pictures, rescales them to the given ""output_size and saves them to an output file named output_file\n.""\n", argv[0]);exit(1);}dst_filename = argv[1];dst_size     = argv[2];if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {fprintf(stderr,"Invalid size '%s', must be in the form WxH or a valid size abbreviation\n",dst_size);exit(1);}dst_file = fopen(dst_filename, "wb");if (!dst_file) {fprintf(stderr, "Could not open destination file %s\n", dst_filename);exit(1);}/* create scaling context */sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,dst_w, dst_h, dst_pix_fmt,SWS_BILINEAR, NULL, NULL, NULL);if (!sws_ctx) {fprintf(stderr,"Impossible to create scale context for the conversion ""fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);ret = AVERROR(EINVAL);goto end;}/* allocate source and destination image buffers */if ((ret = av_image_alloc(src_data, src_linesize,src_w, src_h, src_pix_fmt, 16)) < 0) {fprintf(stderr, "Could not allocate source image\n");goto end;}/* buffer is going to be written to rawvideo file, no alignment */if ((ret = av_image_alloc(dst_data, dst_linesize,dst_w, dst_h, dst_pix_fmt, 1)) < 0) {fprintf(stderr, "Could not allocate destination image\n");goto end;}dst_bufsize = ret;for (i = 0; i < 100; i++) {/* generate synthetic video */fill_yuv_image(src_data, src_linesize, src_w, src_h, i);/* convert to destination format */sws_scale(sws_ctx, (const uint8_t * const*)src_data,src_linesize, 0, src_h, dst_data, dst_linesize);/* write scaled image to file */fwrite(dst_data[0], 1, dst_bufsize, dst_file);}fprintf(stderr, "Scaling succeeded. Play the output file with the command:\n""ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h, dst_filename);end:fclose(dst_file);av_freep(&src_data[0]);av_freep(&dst_data[0]);sws_freeContext(sws_ctx);return ret < 0;
}

这个文件,能把yuv图像格式的数据转换为rgb格式。并按照指定的图像大小输出到文件。
过程分析如下:
1.首先使用av_parse_video_size()函数获得命令行传入的图像的大小
2.打开输出文件
3.调用sws_getContext函数创建缩放与图像格式转换的上下文环境
4.调用av_image_alloc来分配读取源图像数组需要的内存
5.调用av_image_alloc来分配输出图像数组需要的内存
6.循环处理每一帧图像。调用fill_yuv_image获得原始图像后,使用sws_scale进行转换,然后fwrite写入到文件。
7.调用sws_freeContext结束图像的格式转换与缩放操作。

例程结果展示

编译后,执行:
./scaling_video hello.rgb 600x400
答应如下:
Scaling succeeded. Play the output file with the command:
ffplay -f rawvideo -pix_fmt rgb24 -video_size 600x400 hello.rgb
可见,该文件很友好的打印了怎么播放生成的视频文件。
播放的截图如下:

从而,实现了将图像格式由yuv转为rgb,并将其大小缩放到指定大小的过程。

ffmpeg学习十三:图像数据格式的转换与图像的缩放相关推荐

  1. FFmpeg学习4:音频格式转换

    前段时间,在学习试用FFmpeg播放音频的时候总是有杂音,网上的很多教程是基于之前版本的FFmpeg的,而新的FFmepg3中audio增加了平面(planar)格式,而SDL播放音频是不支持平面格式 ...

  2. 【nv12 格式转换】不同图像数据格式之间转换代码实操

    文章目录 1 问题先行 2 nv12介绍 2.1 YUV格式 2.2 NV12排布 3 不同数据格式之间转换实操 4 参考链接 1 问题先行 nv12是什么格式?和常见的rgb/bgr有什么关系吗?他 ...

  3. 使用ffmpeg进行图像格式转换以及图像缩放/sws_scale/linux/c++/c/rgb-yuv420

    利用ffmpeg进行图像数据格式的转换以及图片的缩放应用中,主要用到了swscale.h文件中的三个函数,分别是: struct SwsContext *sws_getContext(int srcW ...

  4. android音视频工程师,音视频学习 (十三) Android 中通过 FFmpeg 命令对音视频编辑处理(已开源)...

    ## 音视频学习 (十三) Android 中通过 FFmpeg 命令对音视频编辑处理(已开源) ## 视音频编辑器 ## 前言 有时候我们想对音视频进行加工处理,比如视频编辑.添加字幕.裁剪等功能处 ...

  5. ffmpeg学习日记612-指令-转换视频格式

    ffmpeg学习日记612-指令-转换视频格式 mkv转mp4 ffmpeg -i LostInTranslation.mkv -codec copy LostInTranslation.mp4 Li ...

  6. ffmpeg学习日记602-指令-转换视频的分辨率

    ffmpeg学习日记602-指令-转换视频的分辨率 指令如下 ffmpeg -i video_1920.mp4 -vf scale=640:360 video_640.mp4 -hide_banner

  7. OpenCV图像处理学习十三,图像金字塔——高斯金字塔和拉普拉斯金字塔

    一.图像金字塔概念 我们在图像处理中常常会调整图像大小,最常见的就是放大(zoom in)和缩小(zoom out),尽管几何变换也可以实现图像放大和缩小,但是这里我们介绍图像金字塔 . 一个图像金字 ...

  8. FFmpeg学习(音视频理论知识)

    文章目录 1. 音视频理论知识 1.1 基本概念 1.1.1 音视频必备的基本概念 常用的视频封装格式 常用的视频编码器 常用的音频编程器: 视频流 裸数据YUV 1.1.2 音视频常见处理 采集 处 ...

  9. FFmpeg学习(一)开篇

    文章目录 FFmpeg学习(一)开篇 为什么要学习FFmpeg 1. FFmpeg简介 2. FFmpeg能做什么 3. FFmpeg架构模块组成 3. 1 libavutil 3. 2 libavf ...

最新文章

  1. 史上最全开源中文 NLP 数据集:包括10大类、142条数据源,总有你钟意的那一款!...
  2. java-1.11.0的环境配置,JAVA 环境配置
  3. 浅析java内存管理机制
  4. 用py2exe打包成一个exe文件
  5. AI修复技术为何这么强?原来背后的技术是……
  6. Qt工作笔记-对*QObject::sender()的认识
  7. html css图片展开动画,8个实用炫酷的HTML5图片动画应用
  8. python中的series的结构_pandas 数据结构之Series的使用方法
  9. jsp form提交到后台中文乱码_2019.6.12 servlet 3.0 和 JSP
  10. python selenium 小知识点整理笔记(更新中...)
  11. c语言music算法,PROJECT:以music算法为基础的几种DOA算法的研究及性能分析
  12. Eclipse下载安装Spring插件
  13. 字体靠右代码_html中怎么把文字往右移
  14. php 字符串首字母ucfirst函数转换成大写
  15. ROHS认证是什么?
  16. 微信注册验证成功之后不跳转_微信公众号申请教程,怎么创建公众号?
  17. 无涯教程: Laravel 8 - 队列介绍
  18. Python用requests库+BeautifulSoup库+re库获取微博热搜(有详解)
  19. 机器学习所需要的高数知识
  20. 从微软官网下载VS2015(2016年8月25日)

热门文章

  1. GBA 开发简单入门
  2. 浅谈泡妞   文 / 中国鄂霸
  3. 巴菲特:我可以发2100万个巴菲特币|附视频
  4. 【上海交大oj】畅畅的牙签袋(改)(枚举+模拟)
  5. Tableau基础 Tableau 数据集的使用
  6. 西北工业大学计算机学院教授张凯龙,张凯龙
  7. 视频分辨率转码(ffmpeg)
  8. 一日一技|Stata筛选出字符串中非数字的行
  9. 页码怎么设置从某一页位置开始,word文档如何设置开始页面
  10. uni-app富文本图片太大溢出以及富文本显示问题