简体   繁体   English

如何新建和删除 AVPacket?

[英]How to new & delete AVPacket?

I'm working with a FFmpeg project right now, I have to store the data of AVPacket which from av_read_frame , and fill data to new AVPacket for following decoding.我现在正在处理一个 FFmpeg 项目,我必须存储来自av_read_frameAVPacket数据,并将数据填充到新的AVPacket以进行后续解码。

Here is my problem: when I try to new & free an AVPacket , memory leaks always happen.这是我的问题:当我尝试新建和释放AVPacket ,总是会发生内存泄漏。

I am just doing a simple testing:我只是在做一个简单的测试:

for(;;) {
    AVPacket pkt;
    av_new_packet(&pkt, 1000);
    av_init_packet(&pkt);
    av_free_packet(&pkt);
}

What am I doing wrong?我究竟做错了什么?

  • av_new_packet creates a packet and allocates data av_new_packet创建数据包并分配数据
  • av_init_packet sets all packet members to default, and sets data pointer to NULL , the leak is here av_init_packet将所有数据包成员设置为默认值,并将数据指针设置为NULL ,泄漏就在这里
  • av_free_packet clears all visible members, but your data is already leaking av_free_packet清除所有可见成员,但你的数据已经泄露

If you want FFmpeg to allocate the data for you, do not call av_init_packet .如果您希望 FFmpeg 为您分配数据,请不要调用av_init_packet If you want to handle the data yourself, allocate the packet object on the stack and set its data yourself (and free it yourself):如果您想自己处理数据,请在堆栈上分配数据包对象并自行设置其数据(并自行释放):

AVPacket pkt;
av_init_packet(&pkt);
pkt.data = dataBuffer;
pkt.size = dataBufferSize;
// use your packet
// free your dataBuffer

I just read the FFmpeg 2.2 AVPacket.c source code.我刚刚阅读了 FFmpeg 2.2 AVPacket.c源代码。

int av_new_packet(AVPacket *pkt, int size) {
    AVBufferRef *buf = NULL;
    int ret = packet_alloc(&buf, size);
    if (ret < 0)
        return ret;

    av_init_packet(pkt);
    pkt->buf      = buf;
    pkt->data     = buf->data;
    pkt->size     = size;
#if FF_API_DESTRUCT_PACKET
FF_DISABLE_DEPRECATION_WARNINGS
    pkt->destruct = dummy_destruct_packet;
FF_ENABLE_DEPRECATION_WARNINGS
#endif

    return 0;
}

void av_init_packet(AVPacket *pkt) {
    pkt->pts                  = AV_NOPTS_VALUE;
    pkt->dts                  = AV_NOPTS_VALUE;
    pkt->pos                  = -1;
    pkt->duration             = 0;
    pkt->convergence_duration = 0;
    pkt->flags                = 0;
    pkt->stream_index         = 0;
#if FF_API_DESTRUCT_PACKET
FF_DISABLE_DEPRECATION_WARNINGS
    pkt->destruct             = NULL;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
    pkt->buf                  = NULL;
    pkt->side_data            = NULL;
    pkt->side_data_elems      = 0;
}

I don't really know about the defines FF_API_DESTRUCT_PACKET , FF_DISABLE_DEPRECATION_WARNINGS , FF_ENABLE_DEPRECATION_WARNINGS我真的不知道定义FF_API_DESTRUCT_PACKETFF_DISABLE_DEPRECATION_WARNINGSFF_ENABLE_DEPRECATION_WARNINGS

Some reason makes destruct of av_free_packet leak某种原因导致av_free_packet泄漏破坏

According the source code, av_init_packet is called in av_new_packet and av_new_packet already allocates the AVBuffer , so if you want to set the data to new AVPacket .根据源代码, av_init_packet被称为av_new_packetav_new_packet已经分配AVBuffer ,所以如果你想将数据设置为新AVPacket

Just memory copy to data of AVPacket , and call av_free_packet when you are done.只需将内存复制到AVPacket数据,并在完成后调用av_free_packet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM