ffmpeg中sws_scale()用法实例

2023-11-07


 

ffmpeg中sws_scale()用法实例

视频编码  2009-06-30 10:02:27  阅读1185  评论0   字号:   订阅

/*
 * Copyright (C) 2003 Michael Niedermayer <michaelni@gmx.at >
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <stdarg.h>

#undef HAVE_AV_CONFIG_H
#include "libavutil/avutil.h"
#include "swscale.h"
#include "swscale_internal.h"

static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h){
    int x,y;
    uint64_t ssd=0;

//printf("%d %d/n", w, h);

    for (y=0; y<h; y++){
        for (x=0; x<w; x++){
            int d= src1[x + y*stride1] - src2[x + y*stride2];
            ssd+= d*d;
//printf("%d", abs(src1[x + y*stride1] - src2[x + y*stride2])/26 );
        }
//printf("/n");
    }
    return ssd;
}

// test by ref -> src -> dst -> out & compare out against ref
// ref & out are YV12
static int doTest(uint8_t *ref[3], int refStride[3], int w, int h, int srcFormat, int dstFormat,
                  int srcW, int srcH, int dstW, int dstH, int flags){
    uint8_t *src[3];
    uint8_t *dst[3];
    uint8_t *out[3];
    int srcStride[3], dstStride[3];
    int i;
    uint64_t ssdY, ssdU, ssdV;
    struct SwsContext *srcContext, *dstContext, *outContext;
    int res;

    res = 0;
    for (i=0; i<3; i++){
        // avoid stride % bpp != 0
        if (srcFormat==PIX_FMT_RGB24 || srcFormat==PIX_FMT_BGR24)
            srcStride[i]= srcW*3;
        else
            srcStride[i]= srcW*4;

        if (dstFormat==PIX_FMT_RGB24 || dstFormat==PIX_FMT_BGR24)
            dstStride[i]= dstW*3;
        else
            dstStride[i]= dstW*4;

        src[i]= (uint8_t*) malloc(srcStride[i]*srcH);
        dst[i]= (uint8_t*) malloc(dstStride[i]*dstH);
        out[i]= (uint8_t*) malloc(refStride[i]*h);
        if (!src[i] || !dst[i] || !out[i]) {
            perror("Malloc");
            res = -1;

            goto end;
        }
    }

    dstContext = outContext = NULL;
    srcContext= sws_getContext(w, h, PIX_FMT_YUV420P, srcW, srcH, srcFormat, flags, NULL, NULL, NULL);
    if (!srcContext) {
        fprintf(stderr, "Failed to get %s ---> %s/n",
                sws_format_name(PIX_FMT_YUV420P),
                sws_format_name(srcFormat));
        res = -1;

        goto end;
    }
    dstContext= sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL, NULL);
    if (!dstContext) {
        fprintf(stderr, "Failed to get %s ---> %s/n",
                sws_format_name(srcFormat),
                sws_format_name(dstFormat));
        res = -1;

        goto end;
    }
    outContext= sws_getContext(dstW, dstH, dstFormat, w, h, PIX_FMT_YUV420P, flags, NULL, NULL, NULL);
    if (!outContext) {
        fprintf(stderr, "Failed to get %s ---> %s/n",
                sws_format_name(dstFormat),
                sws_format_name(PIX_FMT_YUV420P));
        res = -1;

        goto end;
    }
//    printf("test %X %X %X -> %X %X %X/n", (int)ref[0], (int)ref[1], (int)ref[2],
//        (int)src[0], (int)src[1], (int)src[2]);

    sws_scale(srcContext, ref, refStride, 0, h   , src, srcStride);
    sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
    sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);

    ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
    ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
    ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);

    if (srcFormat == PIX_FMT_GRAY8 || dstFormat==PIX_FMT_GRAY8) ssdU=ssdV=0; //FIXME check that output is really gray

    ssdY/= w*h;
    ssdU/= w*h/4;
    ssdV/= w*h/4;

    printf(" %s %dx%d -> %s %4dx%4d flags=%2d SSD=%5lld,%5lld,%5lld/n",
           sws_format_name(srcFormat), srcW, srcH,
           sws_format_name(dstFormat), dstW, dstH,
           flags, ssdY, ssdU, ssdV);
    fflush(stdout);

    end:

    sws_freeContext(srcContext);
    sws_freeContext(dstContext);
    sws_freeContext(outContext);

    for (i=0; i<3; i++){
        free(src[i]);
        free(dst[i]);
        free(out[i]);
    }

    return res;
}

static void selfTest(uint8_t *src[3], int stride[3], int w, int h){
    enum PixelFormat srcFormat, dstFormat;
    int srcW, srcH, dstW, dstH;
    int flags;

    for (srcFormat = 0; srcFormat < PIX_FMT_NB; srcFormat++) {
        for (dstFormat = 0; dstFormat < PIX_FMT_NB; dstFormat++) {
            printf("%s -> %s/n",
                   sws_format_name(srcFormat),
                   sws_format_name(dstFormat));
            fflush(stdout);

            srcW= w;
            srcH= h;
            for (dstW=w - w/3; dstW<= 4*w/3; dstW+= w/3){
                for (dstH=h - h/3; dstH<= 4*h/3; dstH+= h/3){
                    for (flags=1; flags<33; flags*=2) {
                        int res;

                        res = doTest(src, stride, w, h, srcFormat, dstFormat,
                                     srcW, srcH, dstW, dstH, flags);
                        if (res < 0) {
                            dstW = 4 * w / 3;
                            dstH = 4 * h / 3;
                            flags = 33;
                        }
                    }
                }
            }
        }
    }
}

#define W 96
#define H 96

int main(int argc, char **argv){
    uint8_t *rgb_data = malloc (W*H*4);
    uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
    int rgb_stride[3]={4*W, 0, 0};
    uint8_t *data = malloc (3*W*H);
    uint8_t *src[3]= {data, data+W*H, data+W*H*2};
    int stride[3]={W, W, W};
    int x, y;
    struct SwsContext *sws;

    sws= sws_getContext(W/12, H/12, PIX_FMT_RGB32, W, H, PIX_FMT_YUV420P, 2, NULL, NULL, NULL);

    for (y=0; y<H; y++){
        for (x=0; x<W*4; x++){
            rgb_data[ x + y*4*W]= random();
        }
    }
    sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);

    selfTest(src, stride, W, H);

    return 123;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ffmpeg中sws_scale()用法实例 的相关文章

  • PHP 构造函数返回 NULL

    我有这个代码 是否有可能User对象构造函数以某种方式失败 以便 this gt LoggedUser被分配了一个NULL构造函数返回后值和对象被释放吗 this gt LoggedUser NULL if SESSION verbiste
  • perl生成字符串来匹配正则表达式

    我尝试找到一种方法来生成与正则表达式匹配的字符串 例如以下正则表达式 A Z 6 6 A Z2 9 A NP Z0 9 A Z0 9 3 3 0 1 我尝试过 Cpan 上的一些 perl 模块不起作用 gt 字符串 随机 gt 正则表达式
  • 如何在Java中从一组选定的颜色中输出随机颜色? (安卓)

    因此 我希望每当用户输入答案时都为字符串赋予随机颜色 我的问题是 我不确定如何使字符串的随机颜色成为特定范围的颜色 例如 如果我希望字符串随机变成蓝色 红色 绿色 粉色 白色或棕色 只有这些颜色 没有其他颜色 到目前为止 我已经使用以下代码
  • Python错误代码:IndexError:索引错误列表索引超出范围

    我正在尝试用 Python 编写一个模拟赛马的函数 虽然没有获胜者 但它会清除屏幕 显示马匹列表 所有马匹的索引都从零开始 然后 在我标记的行上 代码变得混乱 我发现索引错误列表超出范围 我正在尝试随机选择一匹马 随机选择一个索引号 并将该
  • 为什么 int 数组的最大大小小于 Int32.MaxValue? [复制]

    这个问题在这里已经有答案了 虽然这篇文章说它应该有效 https stackoverflow com questions 2338778 what is the maximum length of an array in net on 64
  • VBA rand 如何使用上限和下限生成随机数?

    所以也许这是多余的 也许这就像问为什么大多数人生来就有 5 个手指 最后的简短答案总是 因为事情就是这样 而且它就是这样工作的 但我讨厌这个答案 该死的我想知道怎么做VBA 中的 Rnd 函数有效 Ms Office Excel 的 MSD
  • 如果仅使用第一个元素,是否必须为整个结构分配内存?

    我有一个结构 其中第一个元素被测试 并且根据其值 结构的其余部分将被读取或不会被读取 在第一个元素的值指示结构的其余部分不会被读取的情况下 我是否必须为整个结构或仅第一个元素分配足够的内存 struct element int x int
  • 为什么结构中“[0]byte”的位置很重要?

    0 byte在golang中不应该占用任何内存空间 但这两个结构体的大小不同 type bar2 struct A int 0 byte type bar3 struct 0 byte A int 那么为什么这个位置 0 byte这里重要吗
  • 当结构体包含字符串时为其分配内存

    假设如果我有一个这样的结构 struct node note that i have changed the struct code according to my convenience char lastname char employ
  • 如何删除非空约束?

    假设创建了一个表 如下所示 create table testTable colA int not null 您将如何删除非空约束 我正在寻找类似的东西 ALTER TABLE testTable ALTER COLUMN colA DRO
  • 如果一列没有值,MySQL 返回最大值或 null

    我尝试获取 mysql select 的最大值 但如果有一行不包含时间戳 则希望将其设置为 null empty 0 表统计数据 简化 ID CLIENT ORDER DATE CANCEL DATE 1 5 1213567200 2 5
  • 样本()和r样本()有什么区别?

    当我从 PyTorch 中的发行版中采样时 两者sample and rsample似乎给出了类似的结果 import torch seaborn as sns x torch distributions Normal torch tens
  • 在 C# 中生成随机浮点数的最佳方法[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 在 C 中生成随机浮点数的最佳方法是什么 更新 我想要从 float Minvalue 到 float Maxvalue 的随机浮点数 我在一些
  • 使用 forge(或其他 JavaScript 方法)生成随机大素数

    我需要在 JavaScript 中生成一个随机大 大约 4096 位 素数 并且我已经在使用 forge Forge 必须有某种生成器来完成此类任务 因为它实现了 RSA 而 RSA 也依赖于随机素数 然而 当你只想获得一个随机素数 类似于
  • 在 Go 中初始化嵌入结构

    我有以下内容struct其中包含一个net http Request type MyRequest struct http Request PathParams map string string 现在我想初始化匿名内部结构http Req
  • 如何在php中关闭夏令时

    我有这行代码将夏令时设置为 打开 将其设置为 关闭 的正确方法是什么 is daylight saving On rcmail config dst active bool date I date I 这是一个大写字母 i 当当前默认时区
  • 结构体实现接口安全吗?

    我似乎记得读过一些关于在 CLR 中通过 C 实现结构如何不好的内容 但我似乎找不到任何相关内容 是不是很糟糕 这样做会产生意想不到的后果吗 public interface Foo Bar GetBar public struct Fub
  • 我在哪里可以获得可靠的熵来源(真正的随机性字节[])?

    目前 我正在寻找一种方法来增加随机性的质量 in my Android应用程序 纸牌游戏 之前 估计对于我的情况 52 排列 至少需要 226 位熵 226 个随机位 我打算用这个byte 作为种子SecureRandom SecureRa
  • 在MySQL中生成随机字符串

    我正在尝试使用函数在 phpmyadmin 中获取随机字符串 我有以下代码 CREATE FUNCTION randomPassword RETURNS varchar 128 BEGIN SET chars ABCDEFGHIJKLMNO
  • 在 C# 中创建加密随机数的最快、线程安全的方法?

    请注意 在多个线程上并行生成随机数时 加密随机数生成器不是线程安全的 使用的发电机是RNGCryptoServiceProvider它似乎重复了很长一段随机位 128 位 重现此情况的代码如下所示 缺乏使用锁来保护访问RNGCryptoSe

随机推荐