天天看點

ffmpeg常用接口FFmpeg新舊接口對照使用一覽

ffmpeg編解碼常用接口

一、媒體檔案分流解析處理

libavformat庫中的函數

  1. avformat_open_input() / avformat_close_input();
  1. avformat_find_stream_info()
  1. av_find_best_stream()
  1. av_dump_format()
  1. av_seek_frame()/avformat_seek_file()
  1. av_read_frame()

二、音視訊解碼器

libavcodec庫中的函數

  1. avcodec_alloc_context3() / avcodec_free_context()
  1. avcodec_parameters_to_context()
  1. avcodec_find_decoder()
  1. avcodec_open2() / avcodec_close()
  1. avcodec_send_packet() / avcodec_receive_frame(): 解碼擷取frame
  1. av_packet_alloc()/av_packet_free()

A. //方法1:計算pixfmt格式圖檔需要的位元組數

  1. avpicture_get_size()-->由av_image_get_buffer_size()代替,通過指定像素格式、圖像寬、圖像高來計算所需的記憶體大小
  1. avpicture_fill() -->由av_image_fill_arrays()代替,使用前先申請記憶體
// libavcodec/avpicture.c
int avpicture_fill(AVPicture *picture, const uint8_t *ptr,
                   enum AVPixelFormat pix_fmt, int width, int height)
{
    return av_image_fill_arrays(picture->data, picture->linesize,
                                ptr, pix_fmt, width, height, 1);
}
           
  1. av_picture_copy -->由av_image_copy()代替

B. //方法2:計算pixfmt格式圖檔需要的位元組數

  1. av_frame_get_buffer()

在AVFrame結構體中,buf和data數組所指向的是同一塊資料區域,在釋放記憶體時調用av_frame_free時,隻會釋放buf。調用av_frame_get_buffer填充時,ffmpeg會将這兩部分一起初始化,是以釋放時隻釋放buf,data部分也會一起釋放。

調用av_image_fill_arrays填充時,隻會更改AVFrame中的data部分,不會改變buf,是以釋放時data不會随着buf釋放,需要自己手動釋放這部分空間。

結論:

av_frame_get_buffer()可以了解為自動為AVFrame配置設定空間,而av_image_fill_arrays可以了解為手動填充。

在初始化一個AVFrame時,如果需要填充自己準備好的資料(如捕獲到的螢幕圖像資料),采用av_image_fill_arrays(),但是在使用完後,一定要注意釋放data

如果用一個AVFrame初始化是為了繼承sws轉換的資料,可以選擇av_frame_get_buffer()進行初始化,這樣在釋放時直接調用av_frame_free即可,不用擔心記憶體洩漏問題。

————————————————

原文連結:https://blog.csdn.net/oooooome/article/details/111993911

libavutil函數

  1. av_frame_alloc()/av_frame_free()
  1. av_frame_ref()/av_frame_unref()
  1. av_frame_clone()

三、視訊格式轉換

libswscale庫

  1. sws_getContext():根據輸入輸出圖像的 寬高和 像素格式 建立轉換器
  1. sws_scale():根據輸入圖像資料進行轉換(大小縮放、圖像格式、顔色空間、存儲布局)操作,結果輸出到輸出緩沖區
  1. sws_freeContext():釋放轉換器

四、音頻格式轉換

libswresample庫

  1. swr_alloc() / swr_free():建立/釋放音頻轉換器
  1. swr_init()/swr_alloc_set_opts():設定轉換參數
  1. swr_convert():進行實際的音頻資料轉換

重點是不同采樣格式、不同通道情況下,音頻資料的記憶體布局

五、音視訊編碼處理

libavformat 庫中的函數

  1. avcodec_find_encoder() / avcodec_find_encoder_by_name();
  1. avcodec_alloc_context3() / avcodec_free_context()
  1. avcodec_open2() / avcodec_close()
  1. avcodec_send_frame() / avcodec_receive_packet()

六、音視訊混流處理

libavformat 庫中的函數

  1. avformat_alloc_output_context2() / avformat_free_context()
  1. avformat_new_stream() 和 avcodec_parameters_from_context()
  1. avio_open() / avio_closep()
  1. avformat_write_header() / av_write_trailer()
  1. av_interleaved_write_frame()

FFmpeg新舊接口對照使用一覽

繼續閱讀