音视频

ffmpeg_sample解读_scaling_video

2020-11-18  本文已影响0人  刘佳阔

title: ffmpeg_sample解读_
date: 2020-10-28 10:15:02
tags: [读书笔记]
typora-copy-images-to: ./imgs
typora-root-url: ./imgs


总结

缩放视频,产生400帧未压缩数据.送入转换器转换.然后输出. 这里涉及了切片的概念,需要查一查.

这个流程看似简单.利用sws来做转换.但是里边的有些参数还是比较难理解.现在简单先做记录

流程图

graph TB
apvs[av_parse_video_size]
-->sg[sws_getContext]
-->aia[av_image_alloc]
-->fyi[fill_yuv_image]
-->ss[sws_scale]
-->fw[fwrite]
-->release[release]
image-20201113161701438

代码



/*
/**
 * @file
 * libswscale API use example.
 * @example 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;
        }
    }
}

/**
 * 缩放视频,产生400帧未压缩数据.送入转换器转换.然后输出. 这里涉及了切片的概念,需要查一查.
 * @param argc
 * @param argv
 * @return
 */
int scaling_video_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];
//从 string中解析出宽高数据
    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;
}

上一篇下一篇

猜你喜欢

热点阅读