编解码相关函数详解

一、通用函数
 1、av_register_all()注册所有组件
二、编码函数
三、解码函数

1、avcodec_find_decoder()

2、avcodec_open2()

3、avformat_open_input()

4、avformat_alloc_output_context2()

5、avformat_new_stream()

6、av_read_frame()

7、av_interleaved_write_frame()

8、avcodec_copy_context()

9、avio_open2()

10、avformat_write_header()

11、avio_open()

12、av_frame_alloc()

13、avpicture_get_size()

14、avpicture_fill()

15、avformat_find_stream_info()

16、avcodec_decode_video2()


17、avcodec_close()


18、avcodec_send_frame()


19、avcodec_receive_frame()


#################################################

#################################################

1、av_register_all()

注册了所有的文件格式和编解码器的库


#################################################

#################################################



1、avcodec_find_decoder()

AVCodec *avcodec_find_decoder(enum AVCodecID id);  

函数的参数是一个解码器的ID,返回查找到的解码器(没有找到就返回NULL)。

#################################################

#################################################

2、avcodec_open2()

int avcodec_open(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);  



用中文简单转述一下avcodec_open2()各个参数的含义:

avctx:需要初始化的AVCodecContext。
codec:输入的AVCodec

options:一些选项。例如使用libx264编码的时候,“preset”,“tune”等都可以通过该参数设置。



#################################################

#################################################

3、avformat_open_input(int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options);  

把结构体和文件相关联

ps:函数调用成功之后处理过的AVFormatContext结构体。
file:打开的视音频流的URL。
fmt:强制指定AVFormatContext中AVInputFormat的。这个参数一般情况下可以设置为NULL,这样FFmpeg可以自动检测AVInputFormat。

dictionay:附加的一些选项,一般情况下可以设置为NULL。

函数执行成功的话,其返回值大于等于0。


#################################################

#################################################


4、 int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat,   const char *format_name, const char *filename);
/**
 * Open an input stream and read the header. The codecs are not opened.
 * The stream must be closed with avformat_close_input().
 *
 * @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context).
 *           May be a pointer to NULL, in which case an AVFormatContext is allocated by this
 *           function and written into ps.
 *           Note that a user-supplied AVFormatContext will be freed on failure.
 * @param filename Name of the stream to open.
 * @param fmt If non-NULL, this parameter forces a specific input format.
 *            Otherwise the format is autodetected.
 * @param options  A dictionary filled with AVFormatContext and demuxer-private options.
 *                 On return this parameter will be destroyed and replaced with a dict containing
 *                 options that were not found. May be NULL.
 *
 * @return 0 on success, a negative AVERROR on failure.
 *
 * @note If you want to use custom IO, preallocate the format context and set its pb field.
 */
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options);

ctx:函数调用成功之后创建的AVFormatContext结构体。

oformat:指定AVFormatContext中的AVOutputFormat,用于确定输出格式。如果指定为NULL,可以设定后两个参数(format_name或者filename)由FFmpeg猜测输出格式。

PS:使用该参数需要自己手动获取AVOutputFormat,相对于使用后两个参数来说要麻烦一些。

format_name:指定输出格式的名称。根据格式名称,FFmpeg会推测输出格式。输出格式可以是“flv”,“mkv”等等。

filename:指定输出文件的名称。根据文件名称,FFmpeg会推测输出格式。文件名称可以是“xx.flv”,“yy.mkv”等等。

函数执行成功的话,其返回值大于等于0。

#################################################

#################################################


5、 avformat_new_stream()

AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)  

在 AVFormatContext 里增加了 AVStream 通道(相关的index已经被设置了)之后,我们就可以自行设置 AVStream 的一些参数信息。例如 : codec_id , format ,bit_rate ,width , height ... ...

#################################################

#################################################


6、av_read_frame()

int av_read_frame(AVFormatContext *s, AVPacket *pkt);

av_read_frame()的作用是读取码流中的音频若干帧或者视频一帧。例如,解码视频的时候,每解码一个视频帧,需要先调用 av_read_frame()获得一帧视频的压缩数据,然后才能对该数据进行解码(例如H.264中一帧压缩数据通常对应一个NAL)。

#################################################

#################################################


7、av_interleaved_write_frame()
av_interleaved_write_frame():写入一个AVPacket到输出文件。

#################################################

#################################################


8、avcodec_copy_context():
拷贝输入视频码流的AVCodecContex的数值t到输出视频的AVCodecContext

#################################################

#################################################


9、avio_open2()
打开相应的编解码器

该函数用于打开FFmpeg的输入输出文件
  1. int avio_open2(AVIOContext **s, const char *url, int flags,
    1.            const AVIOInterruptCB *int_cb, AVDictionary **options);  
            1. avio_open2()函数参数的含义如下:

        1. s:函数调用成功之后创建的AVIOContext结构体。
          url:输入输出协议的地址(文件也是一种“广义”的协议,对于文件来说就是文件的路径)。
          flags:打开地址的方式。可以选择只读,只写,或者读写。取值如下。
          AVIO_FLAG_READ:只读。
          AVIO_FLAG_WRITE:只写。
          AVIO_FLAG_READ_WRITE:读写。
          int_cb:目前还没有用过。

          options:目前还没有用过。

                        1. #################################################

                          #################################################


          10、avformat_write_header()

          1. 写视频文件头
                            1. int avformat_write_header(AVFormatContext *s, AVDictionary **options);  
                                                1. 简单解释一下它的参数的含义:
                                                  s:用于输出的AVFormatContext。
                                                  options:额外的选项,目前没有深入研究过,一般为NULL。
                                                  函数正常执行后返回值等于0。
                                                                                            1. #################################################

                                                                                              #################################################

11、avio_open()
avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE)
参数1是输入数据的缓存,第2个参数是输入输出协议的地址(文件也是一种“广义”的协议,对于文件来说就是文件的路径),第3个参数flags:打开地址的方式。可以选择只读,只写,或者读写。取值如下。
AVIO_FLAG_READ:只读。
AVIO_FLAG_WRITE:只写。
AVIO_FLAG_READ_WRITE:读写。

#################################################

#################################################


12、av_frame_alloc()
AVFrame结构体保存的是解码后和原始的音视频信息。AVFrame通过函数av_frame_alloc()初始化,该函数仅仅分配AVFrame实例本身,而没有分配其内部的缓存。AVFrame实例由av_frame_free()释放;AVFrame实例通常分配一次,重复使用,如分配一个AVFrame实例来保留解码器中输出的视频帧(此时应在恰当的时候使用av_frame_unref()清理参考帧并将AVFrame归零)。该类所描述的数据通常由AVBuffer的API来保存一个引用计数,并保存于AVFrame.buf
 /AVFrame.extended_buf,在至少存在一个参考的时候(如AVFrame.buf[0] != NULL),则该对象被标记为“被引用”。在此情况下,AVFrame所包含的每一组数据必须包含于AVFrame的缓存中。

#################################################

#################################################


13、avpicture_get_size()
intavpicture_get_size(int pix_fmt, int width, int height) usage: 根据像素格式和视频分辨率获得 picture 存储大小

#################################################

#################################################


14、avpicture_fill()
将分配的数据缓存空间和AVFrame关联起来
int avpicture_fill(AVPicture *picture, uint8_t *ptr,
                    int pix_fmt, int width, int height);
这个函数的使用本质上是 为已经分配的空间的结构体AVPicture挂上一段用于保存数据的空间,这个结构体中有一个指针数组data[4],挂在这个数组里。一般我们这么使用:
1) pFrameRGB=avcodec_alloc_frame();
2) numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,pCodecCtx->height);
    buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
3) avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx-         >height);
以上就是为pFrameRGB挂上buffer。这个buffer是用于存缓冲数据的。

Fill in the AVPicture fields.

The fields of the given AVPicture are filled in by using the 'ptr' address which points to the image data buffer. Depending on the specified picture format, one or multiple image data pointers and line sizes will be set.

If a planar format is specified, several pointers will be set pointing to the different picture planes and the line sizes of the different planes will be stored in the lines_sizes array.


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

Parameters

picture
AVPicture *picture

AVPicture whose fields are to be filled in.

avpicture填写的字段。

ptr
uint8_t *ptr

Buffer which will contain or contains the actual image data.

缓冲区,该缓冲区将包含或包含实际的图像数据。 

pix_fmt
int pix_fmt

The format in which the picture data is stored. The format must be one of the values in the PixelFormat enum.

存储图像数据的格式。格式必须PixelFormat枚举的值之一。

width
int width

The width of the image in pixels.

像素的图像宽度。 

height
int height

The height of the image in pixels.

像素中的图像的高度。

Return value

Size of the image data in bytes

以字节为单位的图像数据的大小


#################################################

#################################################



15、 15、 avformat_find_stream_info()
该函数可以读取一部分视音频数据并且获得一些相关的信息
  1. int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);
    简单解释一下它的参数的含义:
    ic:输入的AVFormatContext。
    options:额外的选项,目前没有深入研究过。
    函数正常执行后返回值大于等于0。


  2.   #################################################################################################

16、avcodec_decode_video2()
作用是解码一帧视频数据。输入一个压缩编码的结构体AVPacket,输出一个解码后的结构体AVFrame  int  avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,                         int  *got_picture_ptr,                         const  AVPacket *avpkt); 
 
待解码的数据保存在avpkt->data中,大小为avpkt->size;解码完成后,picture用于保存输出图像数据。

该方法的各个参数:

AVCodecContext *avctx:编解码上下文环境,定义了编解码操作的一些细节;

AVFrame *picture:输出参数;传递到该方法的对象本身必须在外部由av_frame_alloc()分配空间,而实际解码过后的数据储存区将由AVCodecContext.get_buffer2()分配;AVCodecContext.refcounted_frames表示该frame的引用计数,当这个值为1时,表示有另外一帧将该帧用作参考帧,而且参考帧返回给调用者;当参考完成时,调用者需要调用av_frame_unref()方法解除对该帧的参考;av_frame_is_writable()可以通过返回值是否为1来验证该帧是否可写。

int *got_picture_ptr:该值为0表明没有图像可以解码,否则表明有图像可以解码;

const AVPacket *avpkt:输入参数,包含待解码数据。


17、avcodec_close()

18、avcodec_send_frame()

int avcodec_send_frame(AVCodecContextavctx,
  const AVFrameframe 
 )

Parameters
 avctxcodec context
[in]frameAVFrame containing the raw audio or video frame to be encoded
Returns
0 on success, otherwise negative error code: AVERROR(EAGAIN): input is not accepted right now - the frame must be resent after trying to read output packets AVERROR_EOF: the encoder has been flushed, and no new frames can be sent to it AVERROR(EINVAL): codec not opened, refcounted_frames not set, it is a decoder, or requires flush AVERROR(ENOMEM): failed to add packet to internal queue, or similar other errors: legitimate decoding errors 

19、 avcodec_receive_frame()





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值