1. 程式人生 > >ffmpeg學習十三:影象資料格式的轉換與影象的縮放

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 <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,並將其大小縮放到指定大小的過程。