ffmpeg基础 -- avio_alloc_context 读内存
生活随笔
收集整理的這篇文章主要介紹了
ffmpeg基础 -- avio_alloc_context 读内存
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
轉(zhuǎn)載:https://www.jianshu.com/p/3c95b0471d3a
1 // 不要用第四個(gè)參數(shù)傳自定的數(shù)據(jù),當(dāng)av_read_frame的時(shí)候會(huì)出問題,無限循環(huán) 2 avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL); 3 4 /* 5 avio_alloc_context開頭會(huì)讀取部分?jǐn)?shù)據(jù)探測(cè)流的信息,不會(huì)全部讀取,除非設(shè)置的緩存過大 6 av_read_frame會(huì)在讀幀的時(shí)候調(diào)用avio_alloc_context中的read_packet方法取流數(shù)據(jù),每隔avio_ctx_buffer_size調(diào)用一次,直至讀完 7 */
1 /*正確方式*/
2 struct buffer_data
3 {
4 uint8_t *ptr; /* 文件中對(duì)應(yīng)位置指針 */
5 size_t size; ///< size left in the buffer /* 文件當(dāng)前指針到末尾 */
6 };
7
8 // 重點(diǎn),自定的buffer數(shù)據(jù)要在外面這里定義
9 struct buffer_data bd = {0};
10
11 //用來將內(nèi)存buffer的數(shù)據(jù)拷貝到buf
12 int read_packet(void *opaque, uint8_t *buf, int buf_size)
13 {
14
15 buf_size = FFMIN(buf_size, bd.size);
16
17 if (!buf_size)
18 return AVERROR_EOF;
19 printf("ptr:%p size:%zu bz%zu
", bd.ptr, bd.size, buf_size);
20
21 /* copy internal buffer data to buf */
22 memcpy(buf, bd.ptr, buf_size);
23 bd.ptr += buf_size;
24 bd.size -= buf_size;
25
26 return buf_size;
27 }
28
29 /* 打開前端傳來的視頻buffer */
30 int open_input_buffer(uint8_t *buf, int len)
31 {
32 unsigned char *avio_ctx_buffer = NULL;
33 size_t avio_ctx_buffer_size = 32768;
34
35 AVInputFormat* in_fmt = av_find_input_format("h265");
36
37 bd.ptr = buf; /* will be grown as needed by the realloc above */
38 bd.size = len; /* no data at this point */
39
40 fmt_ctx = avformat_alloc_context();
41
42 avio_ctx_buffer = (unsigned char *)av_malloc(avio_ctx_buffer_size);
43
44 /* 讀內(nèi)存數(shù)據(jù) */
45 avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL);
46
47 fmt_ctx->pb = avio_ctx;
48 fmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;
49
50 /* 打開內(nèi)存緩存文件, and allocate format context */
51 if (avformat_open_input(&fmt_ctx, "", in_fmt, NULL) < 0)
52 {
53 fprintf(stderr, "Could not open input
");
54 return -1;
55 }
56 return 0;
57 }
總結(jié)
以上是生活随笔為你收集整理的ffmpeg基础 -- avio_alloc_context 读内存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。