FFMPEG解碼流程:

FFMPEG解碼流程:
  1. 註冊所有容器格式和CODEC:  av_register_all()
  2. 打開文件:                    av_open_input_file()
  3. 從文件中提取流信息:          av_find_stream_info()
  4. 窮舉所有的流,查找其中種類爲CODEC_TYPE_VIDEO
  5. 查找對應的解碼器:            avcodec_find_decoder()
  6. 打開編解碼器:                avcodec_open2()
      這裏需要注意,從stream裏面獲得的codecCtx不能直接使用
 從stream裏面獲得的codecCtx指的是
 pCodecCtxOrig = pFormatCtx->streams[videoStream]->codec;
 這個pCodecCtxOrig不能直接使用,
 而要拷貝到另外一個位置,使用新的
 原文是這麼寫的
 Note that we must not use the AVCodecContext from the video stream directly!
 So we have to use avcodec_copy_context() to copy the context to a new location
     (after allocating memory for it, of course).


  7. 爲解碼幀分配內存:            avcodec_alloc_frame()
  8. 不停地從碼流中提取出幀數據:  av_read_frame()
  9. 判斷幀的類型,對於視頻幀調用: avcodec_decode_video2()
  10.每讀取完一個packet,解碼之後,釋放packet,然後重新讀: av_free_packet(&packet)
  11.釋放分配的內存(所有的packet解碼之後):av_frame_free
  12. 解碼完後,釋放解碼器:       avcodec_close()
  13. 關閉輸入文件:               avformat_close_input_file()
  
  ========
  有幾個函數不一定在基本流程裏面使用,但是有必要介紹一下
  av_malloc av_free 
  `av_malloc` is ffmpeg's malloc that is just a simple wrapper around malloc
that makes sure the memory addresses are aligned and such. It will _not_
protect you from memory leaks, double freeing, or other malloc problems.
翻譯過來之後是:
av_malloc 簡單的封裝了malloc,保證了內存地址對齊,但是以前malloc具有的內存泄漏,多次釋放和其他問題還是需要自己注意。


avpicture_fill
Now we use avpicture_fill to associate the frame with our newly allocated
buffer. About the AVPicture cast: the AVPicture struct is a subset of the
AVFrame struct - the beginning of the AVFrame struct is identical to the
AVPicture struct.


avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
其中buffer是剛剛用av_malloc分配的內存
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章