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

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

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 
#include 
#include 

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
可见,该文件很友好的打印了怎么播放生成的视频文件。
播放的截图如下:
ffmpeg学习十三:图像数据格式的转换与图像的缩放_第1张图片
从而,实现了将图像格式由yuv转为rgb,并将其大小缩放到指定大小的过程。

你可能感兴趣的:(ffmpeg)