avio_alloc_context 讀內(nèi)存

// 不要用第四個參數(shù)傳自定的數(shù)據(jù)枷餐,當(dāng)av_read_frame的時候會出問題哨坪,無限循環(huán)
avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL);

/* 
avio_alloc_context開頭會讀取部分?jǐn)?shù)據(jù)探測流的信息,不會全部讀取悦穿,除非設(shè)置的緩存過大
av_read_frame會在讀幀的時候調(diào)用avio_alloc_context中的read_packet方法取流數(shù)據(jù)向瓷,每隔avio_ctx_buffer_size調(diào)用一次,直至讀完
*/
/*正確方式*/
struct buffer_data
{
    uint8_t *ptr; /* 文件中對應(yīng)位置指針 */
    size_t size;  ///< size left in the buffer /* 文件當(dāng)前指針到末尾 */
};

// 重點根竿,自定的buffer數(shù)據(jù)要在外面這里定義
struct buffer_data bd = {0};

//用來將內(nèi)存buffer的數(shù)據(jù)拷貝到buf
int read_packet(void *opaque, uint8_t *buf, int buf_size)
{

    buf_size = FFMIN(buf_size, bd.size);

    if (!buf_size)
        return AVERROR_EOF;
    printf("ptr:%p size:%zu bz%zu\n", bd.ptr, bd.size, buf_size);

    /* copy internal buffer data to buf */
    memcpy(buf, bd.ptr, buf_size);
    bd.ptr += buf_size;
    bd.size -= buf_size;

    return buf_size;
}

/* 打開前端傳來的視頻buffer */
int open_input_buffer(uint8_t *buf, int len)
{
    unsigned char *avio_ctx_buffer = NULL;
    size_t avio_ctx_buffer_size = 32768;
    
    AVInputFormat* in_fmt = av_find_input_format("h265");

    bd.ptr = buf;  /* will be grown as needed by the realloc above */
    bd.size = len; /* no data at this point */

    fmt_ctx = avformat_alloc_context();

    avio_ctx_buffer = (unsigned char *)av_malloc(avio_ctx_buffer_size);
    
    /* 讀內(nèi)存數(shù)據(jù) */
    avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL);

    fmt_ctx->pb = avio_ctx;
    fmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;

    /* 打開內(nèi)存緩存文件, and allocate format context */
    if (avformat_open_input(&fmt_ctx, "", in_fmt, NULL) < 0)
    {
        fprintf(stderr, "Could not open input\n");
        return -1;
    }
    return 0;
}

/*錯誤方式*/

struct buffer_data
{
    uint8_t *ptr; /* 文件中對應(yīng)位置指針 */
    size_t size;  ///< size left in the buffer /* 文件當(dāng)前指針到末尾 */
};


//用來將內(nèi)存buffer的數(shù)據(jù)拷貝到buf
int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
    struct buffer_data *bd = (struct buffer_data *)opaque;
    buf_size = FFMIN(buf_size, bd->size);

    if (!buf_size)
        return AVERROR_EOF;
    printf("ptr:%p size:%zu bz%zu\n", bd->ptr, bd->size, buf_size);

    /* copy internal buffer data to buf */
    memcpy(buf, bd->ptr, buf_size);
    bd->ptr += buf_size;
    bd->size -= buf_size;

    return buf_size;
}



/* 打開前端傳來的視頻buffer */
int open_input_buffer(uint8_t *buf, int len)
{
    unsigned char *avio_ctx_buffer = NULL;
    size_t avio_ctx_buffer_size = 32768;
    struct buffer_data bd = { 0 };

    AVInputFormat* in_fmt = av_find_input_format("h265");

    bd.ptr = buf;  /* will be grown as needed by the realloc above */
    bd.size = len; /* no data at this point */

    fmt_ctx = avformat_alloc_context();

    avio_ctx_buffer = (unsigned char *)av_malloc(avio_ctx_buffer_size);

    /* 讀內(nèi)存數(shù)據(jù) */
    //使用了第四個參數(shù)傳buffer陵像,雖然可以獲得流的信息,但是讀幀的時候會無限循環(huán)
    avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &bd, read_packet, NULL, NULL); 

    fmt_ctx->pb = avio_ctx;
    fmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;

    /* 打開內(nèi)存緩存文件, and allocate format context */
    if (avformat_open_input(&fmt_ctx, "", in_fmt, NULL) < 0)
    {
        fprintf(stderr, "Could not open input\n");
        return -1;
    }
    return 0;
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末寇壳,一起剝皮案震驚了整個濱河市醒颖,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌九巡,老刑警劉巖图贸,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蹂季,死亡現(xiàn)場離奇詭異冕广,居然都是意外死亡疏日,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門撒汉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沟优,“玉大人,你說我怎么就攤上這事睬辐∧痈螅” “怎么了?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵溯饵,是天一觀的道長侵俗。 經(jīng)常有香客問我,道長丰刊,這世上最難降的妖魔是什么隘谣? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮啄巧,結(jié)果婚禮上寻歧,老公的妹妹穿的比我還像新娘。我一直安慰自己秩仆,他們只是感情好码泛,可當(dāng)我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著澄耍,像睡著了一般噪珊。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上齐莲,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天卿城,我揣著相機與錄音,去河邊找鬼铅搓。 笑死瑟押,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的星掰。 我是一名探鬼主播多望,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼氢烘!你這毒婦竟也來了怀偷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤播玖,失蹤者是張志新(化名)和其女友劉穎椎工,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡维蒙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年掰吕,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颅痊。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡殖熟,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出斑响,到底是詐尸還是另有隱情菱属,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布舰罚,位于F島的核電站纽门,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏营罢。R本人自食惡果不足惜膜毁,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望愤钾。 院中可真熱鬧瘟滨,春花似錦、人聲如沸能颁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽伙菊。三九已至败玉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間镜硕,已是汗流浹背运翼。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留兴枯,地道東北人血淌。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像财剖,于是被迫代替她去往敵國和親悠夯。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容