FFmpeg(8)-開啟和配置音視頻解碼器(avcodec_find_decoder()、avcodec_alloc_context3())

來源:互聯網
上載者:User

標籤:ons   最簡   div   int   base   ext   bsp   code   name   

一.avcodec_find_decoder

擷取解碼器。在使用之前必須保證所用到的解碼器已經註冊,最簡單的就是調用avcodec_register_all() 函數,就像之前註冊解鎖裝器的時候,也要註冊一下。。

AVCodec *avcodec_find_decoder(enum AVCodecID id);

// 尋找解碼器,第一種方法就是直接通過ID號尋找,這個ID號從哪裡擷取呢?就像剛才我們解鎖裝之後,你可以發現我們的AVStream裡面其實是有一個codecID, 那個ID號就是我們要用到的解碼器的ID號。當然如果本身知道格式的ID號,也可以直接傳進去(一般我們用h264,那這個codecID就是28)。找到這個解碼器,然後返回到AVCodec當中去。AVCodec當中存放的是解碼器格式的配置資訊,並不代表最終要處理的解碼器。

AVCodec *avcodec_find_decoder_by_name(const char name);

// 除了通過解碼器的ID號來尋找解碼器,還可能通過名字開啟解碼器。例:avcodec_find_decoder_by_name(“h264_mediacodec”);  // 用Android裡面內建的解碼模組)

 

二.AVCodecContext 解碼器上下文

AVCodecContext *avcode_alloc_context3(const AVCodec *codec);       // 申請AVCodecContext空間。需要傳遞一個編碼器,也可以不傳,但不會包含編碼器。

void avcodec_free_context(AVCodecContext **avctx);                          // 清理並AVCodecContext空間。

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

// 開啟視頻解碼器。如果在 avcode_alloc_context3 的時候沒有傳解碼器,則在此需要進行傳遞,後面的options是選擇性參數。參見:libavcodec/options_table.h。

AVCodecContext 的常用參數:

int thread_count; // 用於配置解碼線程數

time_base             // 時間基數。

三.avcodec_parameters_to_context

avcodec_parameters_to_context(codec, p)。該函數用於將流裡面的參數,也就是AVStream裡面的參數直接複製到AVCodecContext的上下文當中。

 

四. 開啟音視頻解碼器樣本

// 註冊解碼器avcodec_register_all();AVCodec *vc = avcodec_find_decoder(ic->streams[videoStream]->codecpar->codec_id); // 軟解    // vc = avcodec_find_decoder_by_name("h264_mediacodec"); // 硬解    if (!vc) {        LOGE("avcodec_find_decoder[videoStream] failure");        return env->NewStringUTF(hello.c_str());    }    // 配置解碼器    AVCodecContext *vct = avcodec_alloc_context3(vc);    avcodec_parameters_to_context(vct, ic->streams[videoStream]->codecpar);    vct->thread_count = 1;    // 開啟解碼器    int re = avcodec_open2(vct, vc, 0);    if (re != 0) {        LOGE("avcodec_open2 failure");        return env->NewStringUTF(hello.c_str());    }

 

FFmpeg(8)-開啟和配置音視頻解碼器(avcodec_find_decoder()、avcodec_alloc_context3())

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.