当前位置:网站首页>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 .
边栏推荐
- VS Code 常用快捷键
- 项目套价是什么意思?
- 小程序畢設作品之微信在線教育視頻點播學習小程序畢業設計(3)後臺功能
- 机器学习11:代价敏感学习
- Intel experts share: how to program efficiently on XPU architecture? Zhiqiang Research Institute
- A Tutorial on Learned Multi-dimensional Indexes
- 机器学习10:集成学习
- H5 embedded app, how to communicate with the web? H5 and web communication
- leetcode7-dfs+动态规划+双指针
- GNN series GCN brief derivation and understanding and DGL source code analysis
猜你喜欢

Chapter 4 performance platform godeye source code analysis - monitoring module

FTXUI基础笔记(botton按钮组件基础)

In tech 2022 | Intel technology product innovation quick view

Wechat Online Education video on Demand Learning of applet Graduation Design (3) Background Function

Intel helps open medical service and promote the intellectualization of ultrasonic prenatal examination

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(4)开题报告

EAS(能量感知调度)绿色节能调度器

Openresty 做静态资源服务器

C'est génial de jouer vscode comme un effet idea.

Container adapter - stack, queue, priority queue
随机推荐
VS Code 常用快捷键
C# 字符串(string)常用方法
2022 Yangtze River Delta mathematical modeling: Gearbox Fault Diagnosis
小程序毕设作品之微信电子书阅读小程序毕业设计(1)开发概要
Find the central subscript of the array
Tutorial: Adaptive Replication and Partitioning in Data Systems
Laradock restart MySQL found
[database] must know at the end of the term ----- Chapter VII database integrity
C语言详解系列——循环语句的练习与巩固,二分查找的讲解
Unity - 如何修改一个 Package 或是如何将 Package Local化
[super cloud terminal to create a leading opportunity] local computing cloud management, Intel helps digitalize Education
minimum spanning tree
Realize the dual opening of wechat on the computer (log in to two wechat)
In the era of super video, what is the solution to the data flood?
Laravel's file upload
mqant 深入分析
Chapter 3 performance platform godeye source code analysis - memory module
IN Tech 2022|英特尔技术产品创新速览
Wechat official account page authorization 40029 error "suggested collection"
[MySQL] install and configure MySQL on the ECS and connect with idea