当前位置:网站首页>Data interaction between avframe\avpacket and itself in ffmpeg
Data interaction between avframe\avpacket and itself in ffmpeg
2022-07-19 04:12:00 【Taylor lance】
In the use of ffmpeg Sometimes our own data structures, such as yuv Data or compressed H264 Data wants to be converted to ffmpeg Medium AVFrame perhaps AVPacket Structure , How to turn it ?
This is in ffmpg Of libavcodec Of v4l2_buffers.h/.c There are very good cases in .
Let's talk about my personal understanding .
1、 Here's my yuv Data structures
struct Private;
struct MyYUV{
uint8_t *data[8];
int planes[8];
int length;
int width;
int height;
int pix_fmt;
int off_set;
Private *private;
}
1、yuv The data goes to AVFrame Packet data :
void int MyYUV_2_Avframe(AVFrame *frame, MyYUV *in){
int i , ret;
frame->format = in ->pix_fmt;
for (i = 0; i < in->length; i++) {
ret = buf_to_bufref(in, i, &frame->buf[i]);
if (ret) return ret;
frame->linesize[i] = in->planes[i];
frame->data[i] = frame->buf[i]->data;
}
}
// This function mainly creates AVBufferRef , And point the relevant data to its internal pointer
int buf_to_bufref(MyYUV *in, int plane, AVBufferRef **buf){
int ret;
if (plane >= in->num_planes)
return AVERROR(EINVAL);
/* even though most encoders return 0 in data_offset encoding vp8 does require this value */
// among my_free_buffer It's a callback function , To release yuv data .
*buf = av_buffer_create((char *)in->data[plane] + in->off_set,
in->data[plane] * in->height, my_free_buffer, in, 0);
if (!*buf)
return AVERROR(ENOMEM);
return ret;
}
// The next two functions are ffmpeg Internal function buffer.c In file
// It can be found through observation , There is no storage space for application data inside , Just created a AVBufferRef And a AVBuffer , Then let the data pointer copy directly .
static AVBufferRef *buffer_create(AVBuffer *buf, uint8_t *data, size_t size,
void (*free)(void *opaque, uint8_t *data),
void *opaque, int flags)
{
AVBufferRef *ref = NULL;
buf->data = data;
buf->size = size;
buf->free = free ? free : av_buffer_default_free;
buf->opaque = opaque;
atomic_init(&buf->refcount, 1);
buf->flags = flags;
ref = av_mallocz(sizeof(*ref));
if (!ref)
return NULL;
ref->buffer = buf;
ref->data = data;
ref->size = size;
return ref;
}
AVBufferRef *av_buffer_create(uint8_t *data, size_t size,
void (*free)(void *opaque, uint8_t *data),
void *opaque, int flags)
{
AVBufferRef *ret;
AVBuffer *buf = av_mallocz(sizeof(*buf));
if (!buf)
return NULL;
ret = buffer_create(buf, data, size, free, opaque, flags);
if (!ret) {
av_free(buf);
return NULL;
}
return ret;
}
2、h264 The data goes to AVPacket Packet data :
int mybuf_to_avpkt(AVPacket *pkt, MyYUV *avbuf)
{
int ret;
av_packet_unref(pkt);
// Special note ffmpeg Use of references in , Here is the same as the previous case .
ret = buf_to_bufref(avbuf, 0, &pkt->buf);
if (ret)
return ret;
pkt->size = avbuf->planes[0] * height;
pkt->data = pkt->buf->data;
if (avbuf->buf.flags & BUF_FLAG_KEYFRAME)
pkt->flags |= AV_PKT_FLAG_KEY;
if (avbuf->buf.flags & BUF_FLAG_ERROR) {
av_log(logger(avbuf), AV_LOG_ERROR, "%s driver encode error\n", avbuf->context->name);
pkt->flags |= AV_PKT_FLAG_CORRUPT;
}
pkt->dts = pkt->pts = buf_get_pts(avbuf);
return 0;
}
On the contrary, it is very simple, simple data copy .
边栏推荐
- [database] must know at the end of the term ----- Chapter 2 relational data model
- AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
- Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture
- IDEA及控制台 设置管理员权限
- 库函数的模拟实现
- 厲害,竟然把VSCode玩成了IDEA的效果,有點哇塞
- 机器学习11:代价敏感学习
- Skillfully use enterprise network disk to collect reports or summaries
- [database] must know at the end of the term ----- Chapter 6 experiment
- 可省近90%服务器,反欺诈效率却大增,PayPal打破「AI内存墙」的方案为何如此划算?
猜你喜欢

【Notebook系列第七期】OpenVINO预训练模型的的下载和使用方法

Academic sharing | design and development of multi staining pathological image information evaluation system based on openvino

小程序毕设作品之微信电子书阅读小程序毕业设计(5)任务书

IDEA及控制台 设置管理员权限

厲害,竟然把VSCode玩成了IDEA的效果,有點哇塞

How to use Google Earth client and KML Download

C# 字符串(string)常用方法

Nearly 90% of servers can be saved, but the anti fraud efficiency has increased significantly. Why is PayPal's plan to break the "Ai memory wall" so cost-effective?

leetcode7-dfs+动态规划+双指针

Wechat online education video on demand learning applet graduation design (3) background function
随机推荐
Nearly 90% of servers can be saved, but the anti fraud efficiency has increased significantly. Why is PayPal's plan to break the "Ai memory wall" so cost-effective?
Leetcode 931: minimum sum of descent path
【Notebook系列第七期】OpenVINO预训练模型的的下载和使用方法
[database] must know and be able at the end of the term ----- Chapter VIII database security
最小生成树
Unity - 如何修改一个 Package 或是如何将 Package Local化
minimum spanning tree
7.16 simulation summary
donet framework4.X==windows窗体应用新建项目,通过System.Data.SqlClient连接sqlserver进行查询
Timeline components
Skillfully use enterprise network disk to collect reports or summaries
AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
VS Code 常用快捷键
Chapter 2 - create and maintain MySQL database
IN Tech 2022|英特尔技术产品创新速览
Build a portrait matting server based on openvino model server
JS array processing [slice implements the deletion, insertion and replacement of arrays]
企业邮局如何设置SPF记录?
micro、M3O微服务系列(三)
Wechat online education video on demand learning applet graduation design (3) background function