FFMpeg SDK 开发手册

FFMpeg 中比较重要的函数以及数据结构如下:

  1. 数据结构: (1) AVFormatContext (2) AVOutputFormat (3) AVInputFormat (4) AVCodecContext (5) AVCodec (6) AVFrame (7) AVPacket (8) AVPicture (9) AVStream 2. 初始化函数: (1) av_register_all() (2) avcodec_open() (3) avcodec_close() (4) av_open_input_file() (5) av_find_input_format() (6) av_find_stream_info() (7) av_close_input_file() 3. 音视频编解码函数: (1) avcodec_find_decoder() (2) avcodec_alloc_frame() (3) avpicture_get_size() (4) avpicture_fill() (5) img_convert() (6) avcodec_alloc_context() (7) avcodec_decode_video() (8) av_free_packet() (9) av_free() 4. 文件操作: (1) avnew_steam() (2) av_read_frame() (3) av_write_frame() (4) dump_format() 5. 其他函数: (1) avpicture_deinterlace() (2) ImgReSampleContext()
    以下就根据,以上数据结构及函数在 ffmpeg 测试代码 output_example.c 中出现的前后顺进行分析。在 此之前还是先谈一下 ffmpeg 的编译问题。在 linux 下的编译比较简单,这里不多说了。在 windows 下的编 译可以参考以下网页:
    http://bbs.chinavideo.org/viewthread.php?tid=1897&extra=page%3D1
    值得一提的是,在使用编译后的 sdk 进行测试时(用到 ffmpeg 目录下的 output_example.c)编译过程 中可能会有以下两个问题:
    1. Output_example.c用到了snprintf.h这个头文件。然而这个头文件在win下和linux下有所不同。 具体在 win 下可以用以下方法解决:
    http://www.ijs.si/software/snprintf/
    2. 如果使用 vc6,或是 vc6 的命令行进行编译,inline 可能不认。错误会出现在 common.h 文件 中,可以在 common.h 中加入
    #ifdef _MSC_VAR
    #define inline __inline
    #endif

交待完毕进入正题。
一.FFMpeg 中的数据结构:
I. AVFormatContext
一般在使用 ffmpeg sdk 的代码中 AVFormatContext 是一个贯穿始终的数据结构,很多函数都要用到它 作为参数。FFmpeg 代码中对这个数据结构的注释是:format I/O context 此结构包含了一个视频流的格式内容。其中存有了 AVInputFormat(or AVOutputFormat 同一时间 AVFormatContext 内只能存在其中一个),和 AVStream、AVPacket 这几个重要的数据结构以及一些其他的 相关信息,比如 title,author,copyright 等。还有一些可能在编解码中会用到的信息,诸如:duration, file_size, bit_rate 等。参考 avformat.h 头文件。 Useage: 声明:
AVFormatContext oc; (1)
初始化: 由于 AVFormatConext 结构包含许多信息因此初始化过程是分步完成,而且有些变量如果没 有值可用,也可不初始化。但是由于一般声明都是用指针因此一个分配内存过程不可少:
oc = av_alloc_format_context(); (2)
结构中的 AVInputFormat
(或 AVOutputFormat*)是一定要初始化的,基本上这是编译码要使用什么 codec 的依据所在:
oc->oformat = fmt; or oc->iformat = fmt; (3)
其中 AVOutputFormat* fmt 或 AVInputFormat* fmt。(AVInputFormat and AVOutputFormat 的初始化在后 面介绍。随后在参考代码 output_example.c 中有一行:
snprintf(oc-filename, sizeof(oc->filename), “%s”, filename); (4)
还不是十分清楚有什么作用,估计是先要在输出文件中写一些头信息。
在完成以上步骤後,(初始化完毕AVInputFormat*(或AVOutputFormat*)以及AVFormatContext)接下 来就是要利用oc初始化本节开始讲到的AVFormatContext中的第二个重要结构。AVStream(假设已经有了声 明AVStream video_st。参考代码中用了一个函数来完成初始化,当然也可以在主函数中做,传递进函数的 参数是oc 和fmt->video_codec(这个在下一节介绍(29)):
vdeo_st = add_video_stream(oc, fmt->video_codec); (5)
此函数会在后面讲到 AVStream 结构时分析。 AVFormatContext 最后的一个设置工作是:
if( av_set_paramters(oc,NULL) < 0){ (6) //handle error; } dump_format(oc, 0, filename, 1); (7)
作用就是看看先前的初始化过程中设置的参数是否符合规范,否则将报错。 上面讲的都是初始化的过程,包括 AVFormatContext 本身的和利用 AVFormatContext 初始化其他数据 结构的。接下来要讲讲整个的编解码过程。我想先将 ouput_example.c 中 main 函数内的编解码函数框架描 述一下。这样比较清晰,而且编码者为了结构清晰,在写 ouput_example.c 的过程中也基本上在 main 函数 中只保持 AVFormatContext 和 AVStream 两个数据结构(AVOutputFormat 其实也在但是包含在 AVFormatContext 中了)。
// open video codec and allocate the necessary encode buffers if(video_st) open_video(oc, video_st); (8) // write the stream header, if any av_write_header(oc); (9)
// encode and decode process for(; 😉{ write_video_frame(oc, video_st); (10) // break condition…here }
//close codec if(video_st) close_video(oc, video_st); (11)
//write the trailer , if any av_write_trailer(oc); (12)
// free the streams for(i=0; ib_streams; i++){ av_freep(&oc->streams[i]->codec); (13) av_freep(&oc->streams[i]); (14) }
//close the ouput file if(!(fmt->flags & AVFMT_NOFILE)){ url_fclose(&oc->pb); (15) }
av_free(oc); (16)
通过以上的一串代码,就可以清晰地看出 AVFormatContex
oc 和 AVStream* video_st 是在使用 ffmpeg SDK 开发时贯穿始终的两个数据结构。以下,简要介绍一下三个标为红色的函数,他们是参考代码 output_example.c 开发者自行定义的函数。这样可以使整个代码结构清晰,当然你在使用 ffmpeg SDK 时也 可以在主函数中完成对应的功能。在后面我们会专门针对这三个函数做分析。

  1. open_video(oc, video_st);
    此函数主要是对视频编码器(或解码器)的初始化过程。初始化的数据结构为 AVCodec* codec 和 AVCodecContext* c 包括用到了的 SDK 函数有:
    c = st->codec;
    codec = avcodec_find_encoder(c->codec_id); //编码时,找编码器 (17) codec = avcodec_find_decoder(c->codec_id); //解码时,找解码器 (18)
    AVCodecContex是结构AVStream中的一个数据结构,因此在AVStream初始化後(5)直接复值给c。
    // internal open video codec avcodec_open(c,codec); (19)
    // allocate video stream buffer // AVFrame *picture // uint8_t *video_outbuf video_outbuf_size=200000; video_outbuf = av_maloc(video_outbuf_size); (20)
    // allocate video frame buffer picture = alloc_picture(c->pix_fmt, c->width, c->height); (21)
    上述三步比较容易理解,打开视频编解码codec、分配输出流缓存大小、分配每一帧图像缓存大小。其 中AVFrame也是ffmpeg中主要数据结构之一。这一步(8)是对编解码器的初始化过程。
  2. write_video_frame(AVFormatContext *oc, AVStream *st)
    这个函数中做了真正的编解码工作,其中的函数比较复杂先列出来慢慢分析。 用到的数据结构有 AVCodecContext *c, SwsContext *img_convert_ctx。其中 SwsContext 是用来变 换图像格式的。比如 yuv422 变到 yuv420 等,当然也用到函数,见下面
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值