FFMpeg SDK 開發手冊(3)
:轉載時請以超連結形式標明文章原始出處和作者資訊及本宣告
http://leezen.blogbus.com/logs/16084644.html
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儲存的目標檔案context將src(source)轉為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: 根據AVCodecContext將pict編碼到buf中,buf_size是buf的大小。
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的初學者,如果大家有什麼問題也希望能在版上一
起討論。我的email:yunfhu@gmail.com
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24790158/viewspace-1040905/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- FFMpeg SDK 開發手冊 1
- FFMpeg SDK 開發手冊(2)
- 阿里java開發手冊阿里Java
- matrixone/ematrix開發手冊
- FFmpeg開發筆記(五):ffmpeg解碼的基本流程詳解(ffmpeg3新解碼api)筆記API
- [開發文件]bootstrap中文手冊boot
- base業務框架開發手冊框架
- DATA CARTRIDGE開發手冊
- 物件相關開發手冊物件
- 開心網外掛開發手冊
- Web 開發手冊——PHP 開發環境搭建WebPHP開發環境
- 阿里Java開發手冊思考(三)阿里Java
- 阿里Java開發手冊思考(一)阿里Java
- 阿里Java開發手冊思考(二)阿里Java
- Java開發手冊精華總結Java
- 阿里Java開發手冊思考(五)阿里Java
- 阿里Java開發手冊思考(四)阿里Java
- Web前端開發規範手冊Web前端
- Xiuno 開發手冊正式釋出。
- ZooKeeper開發手冊中文翻譯
- XML資料庫開發手冊XML資料庫
- 安卓開發開發規範手冊V1.0安卓
- 安卓開發開發規範手冊 V1.0安卓
- SDK開發
- MaxPHP(原Yao框架)完全開發手冊PHP框架
- wxpython - 快速開發封裝手冊Python封裝
- 阿里巴巴Java開發手冊阿里Java
- Java Web程式開發參考手冊JavaWeb
- 物件相關開發手冊總結物件
- DATA CARTRIDGE開發手冊總結
- Ffmpeg快速應用開發
- [譯]SearchFragment --Android TV 開發手冊十二FragmentAndroid
- XML資料庫開發手冊總結XML資料庫
- 安全檔案和大物件開發手冊物件
- Qt 嵌入式圖形開發大全和QT開發手冊QT
- ffmpeg播放器開發 詳細記錄+程式碼實現3播放器
- 【iCore3雙核心板】釋出 iCore3 應用開發平臺使用者手冊
- javacv教程文件手冊開發指南匯總篇Java