在vs2008下使用ffmpeg(4):结构体成员初始化

本文介绍在VS2008环境下如何解决FFmpeg中特定结构体成员初始化问题,通过调整数组成员顺序并修改成员变量初始化方式,使代码能够成功编译。

 

快乐虾

http://blog.youkuaiyun.com/lights_joy/

lights@hb165.com

   

本文适用于

ffmpeg-checkout-20081210

vs2008

Windows XP

 

欢迎转载,但请保留作者信息

 

ffmpeg中,大量使用了c99中的结构体成员初始化方式,如libavcodec/imgconvert.c中的一个定义:

 

/* this table gives more information about formats */

static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {

    /* YUV formats */

    [PIX_FMT_YUV420P] = {

        .name = "yuv420p",

        .nb_channels = 3,

        .color_type = FF_COLOR_YUV,

        .pixel_type = FF_PIXEL_PLANAR,

        .depth = 8,

        .x_chroma_shift = 1, .y_chroma_shift = 1,

    },

    [PIX_FMT_YUV422P] = {

        .name = "yuv422p",

        .nb_channels = 3,

        .color_type = FF_COLOR_YUV,

        .pixel_type = FF_PIXEL_PLANAR,

        .depth = 8,

        .x_chroma_shift = 1, .y_chroma_shift = 0,

    },

………..

};

这一段代码在vs2008下编译将产生很多语法错误,首先在这个结构体数组中,它使用了[PIX_FMT_*]这样的方式指定序号,这样数组的成员可以不必要按照顺序排列,但是在vs2008下,必须调整数组成员的顺序,使之按递增的顺序排列。此外,vs2008也不支持.name = “”这样的定义,因此可以做出如下修改:

 

/* this table gives more information about formats */

static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {

    /* YUV formats */

    /*[PIX_FMT_YUV420P] =*/ { // PIX_FMT_YUV420P = 0

        /*.name = */"yuv420p",

        /*.nb_channels = */3,

        /*.color_type = */FF_COLOR_YUV,

        /*.pixel_type = */FF_PIXEL_PLANAR,

         /*.is_alpha = */0,

        /*.x_chroma_shift = */1,

         /*.y_chroma_shift = */1,

        /*.depth = */8,

    },

    /*[PIX_FMT_YUYV422] =*/ {    // PIX_FMT_YUYV422 = 1

        /*.name = */"yuyv422",

        /*.nb_channels = */1,

        /*.color_type = */FF_COLOR_YUV,

        /*.pixel_type = */FF_PIXEL_PACKED,

         /*.is_alpha = */0,

        /*.x_chroma_shift = */1,

         /*.y_chroma_shift = */0,

        /*.depth = */8,

    },

    /*[PIX_FMT_RGB24] =*/ {      // PIX_FMT_RGB24 = 2

        /*.name = */"rgb24",

        /*.nb_channels = */3,

        /*.color_type = */FF_COLOR_RGB,

        /*.pixel_type = */FF_PIXEL_PACKED,

         /*.is_alpha = */0,

        /*.x_chroma_shift = */0,

         /*.y_chroma_shift = */0,

        /*.depth = */8,

    },

………….

};

 

参考资料

vs2008下使用ffmpeg1):inttypes.h的问题( 2008-12-11 )

vs2008下使用ffmpeg2):readtime的问题( 2008-12-11 )

vs2008下使用ffmpeg3):结构体构建( 2008-12-11 )

 

 

 

不同编程语言中初始化静态结构体的方法有所不同,以下以C语言和C#为例进行介绍: ### C语言 C语言中结构体是一种构造数据类型,可把不同类型的数据组合成一个整体。对于静态结构体初始化,有以下几种常见方式: - **顺序初始化**:按照结构体定义的成员顺序依次赋值。示例代码如下: ```c #include <stdio.h> // 定义结构体 struct Example { int first; double second; char *third; float four; }; // 静态结构体顺序初始化 static struct Example test1 = {-10, 3.141590, "method three", 0.25}; int main() { printf("first: %d, second: %f, third: %s, four: %f\n", test1.first, test1.second, test1.third, test1.four); return 0; } ``` - **指定成员初始化**:可以不按照成员顺序,通过指定成员名来赋值。这种方法在Linux内核(kernel)、音视频编解码库FFmpeg中大量使用。示例代码如下: ```c #include <stdio.h> // 定义结构体 struct Example { int first; double second; char *third; float four; }; // 静态结构体指定成员初始化 static struct Example test2 = { .second = 3.141590, .third = "method three", .first = -10, .four = 0.25 }; int main() { printf("first: %d, second: %f, third: %s, four: %f\n", test2.first, test2.second, test2.third, test2.four); return 0; } ``` ### C# 在C#中,静态结构体初始化可以在静态类中完成。示例代码如下: ```csharp using System; // 定义静态类 public static class TestUse { // 定义结构体 public struct st { public int num; public string str; } // 静态结构体 public static st pst; public static int Count = 0; // 静态构造函数初始化静态成员 static TestUse() { Count += 10; pst = new st(); pst.num += Count++; pst.str += pst.num.ToString(); } } class Program { static void Main() { Console.WriteLine("TestUse.Count = " + TestUse.Count.ToString()); Console.WriteLine("TestUse.pst.num = " + TestUse.pst.num.ToString()); Console.WriteLine("TestUse.pst.str = " + TestUse.pst.str); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌云阁主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值