Chinaunix首页 | 论坛 | 博客
  • 博客访问: 379931
  • 博文数量: 35
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 797
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-02 08:51
文章分类

全部博文(35)

文章存档

2011年(1)

2010年(3)

2009年(3)

2008年(28)

我的朋友

分类: WINDOWS

2008-08-18 23:44:45

14. void av_free(void *ptr)

usage: free the stream. Free memory which has been allocated with av_malloc(z)() or av_realloc().

av_free(oc);

/******************************************************************

******************************************************************

add_video_stream()

AVCodecContext *c

AVStream *st

******************************************************************/

******************************************************************

1.AVStream *av_new_stream(AVFormatContext *s, int id)

usage: add a new stream to a media file. s: media file handle, id: file format dependent stream id

st = av_new_stream(oc, 0);

/******************************************************************

******************************************************************

open_video()

AVCodecContext *c

AVCodec *codec

AVFrame *picture, *tmp_picture

uint8_t *video_output

int frame_count, video_outbuf_size;

******************************************************************

******************************************************************/

1 AVCodec *avcodec_find_encoder(enum CodecID id)

usage: find the codec of encoder by CodecID. 在前面main中的guess_format()就已经开始为此准备了。

codec = avcodec_find_encoder(c->codec_id);

2 int avcodec_open(AVCodecContext *avctx, AVCodec *codec);

usage: opens / inits the AVCodecContext. 打开失败的话,返回值小于零。

avcodec_open(c, codec);

3 void *av_malloc(unsigned in size);

usage: you can redefine av_malloc and av_free in your project to use your memory allocator. You do not need

to suppress this file because the linker will do it automatically

Memory allocation of size byte with alignment suitable for all memory accesses (including vectors if

available on the CPU). av_malloc(0) must return a non NULL pointer.

video_outbuf_size = 200000;

video_outbuf = avmalloc(video_outbuf_size);

4 static AVFrame *alloc_picture(int pix_fmt, int width, int height)

picture = alloc_picture(c->pix_fmt, c->width, c->height); 

 

/******************************************************************

******************************************************************

******************************************************************

alloc_picture()

AVFrame *picture

uint8_t *picture_buf

int size

******************************************************************

******************************************************************/

******************************************************************

1. avcodec_alloc_frame()

 

usage: initialize AVFrame* picture

picture = avcodec_alloc_frame()

2. int avpicture_get_size(int pix_fmt, int width, int height)

 

usage: 根据像素格式和视频分辨率获得picture存储大小。

size = avpicture_get_size(pix_fmt, width, height);

picture_buf = av_malloc(size)

3. int avpicture_fill(AVPicture *picture, uint8_t *ptr, int pix_fmt, int width, int height)

 

usage: Picture field are filled with ptr addresses, also return size。用ptr中的内容根据文件格式(YUV…)

和分辨率填充picture。这里由于是在初始化阶段,所以填充的可能全是零。

avpicture_fill((AVPicture*)picture, picture_buf, pix_fmt, width, height);

/******************************************************************

******************************************************************

write_video_frame()

int out_size, ret;

AVCodecContext *c;

static struct SwsContext *img_convert_ctx

******************************************************************

******************************************************************/

1 struct SwsContext *sws_getContext(int srcW, ……)

usage: 转变raw picture格式的获取context函数,比如下面的代码就是将其他格式的(如yuv422)转为yuv420,就要将

context 保存在img_convert_ctx中,然后再利用后面的介绍函数做转化。

img_convert_ctx = sws_getContext(c->width, c->height,

PIX_FMT_YUV420P,

c->width, c->height,

c->pix_fmt,

sws_flags, NULL, NULL, NULL);

2 int sws_scale(struct SwsContext *ctx, uing8_t* src[], int srcStride[], int srcSliceY, int srcSliceH,

uint8_t* dst[], int dstStride[]);

usage: 根据SwsContext保存的目标文件contextsrcsource)转为dst(destination)

sws_scale(img_convert_ctx, tmp_picture->data, tmp_picture->linesize, 0, c->height, picture->data, picture-

>linesize);

4. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVFrame *pict);

 

usage: 根据AVCodecContextpict编码到buf中,buf_sizebuf的大小。 

 

out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);

5 static inline void av_init_packet(AVPacket *pkt)

usage: initialize optional fields of a packet.初始化AVPacket

AVPacket pkt;

av_init_packet(&pkt)

6 int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)

 

usage: 校准时间基(maybe

pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);

7 int av_write_frame(AVFormatContext *s, AVPacket *pkt)

 

usage: write a packet to an output media file . pkt: the packet, which contains the stream_index,

buf/buf_size, dts/pts, …if error return<0, if OK =0, if end of stream wanted =1

ret = av_write_frame(oc, &pke);

/******************************************************************

******************************************************************

static void close_video(AVFormatContext *oc, AVStream *st)

{

avcodec_close(st->codec);

av_free(picture->data[0]);

av_free(picture);

if (tmp_picture) {

av_free(tmp_picture->data[0]);

av_free(tmp_picture);

}

av_free(video_outbuf);

}

/******************************************************************

******************************************************************

讲初始化过的,特别是分配过内存的,都要释放。

注:标定红色的函数为需要编程人员自己按照实际应用情况编写的,蓝色的是FFMpeg SDK中定义的函数。我们会分析函数的

作用和调用方法。

由于时间关系,写得很乱。希望没有给你带来太大困扰,这份不成熟的文档能对你有一点点地帮助。chinavideo版上有很多

大牛,我想做抛砖引玉者,希望以后能有更好的文档出现。我也是ffmpeg的初学者,如果大家有什么问题也希望能在版上一

阅读(2423) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~