Create a copy of an AVPacket structure

帅比萌擦擦* 提交于 2019-12-10 11:35:53

问题


I'd like to make a copy of an AVPacket so that I can decode it later, when I like.

The AVPacket is from the audio stream.

av_dup_packet doesn't seem to work.

AVPacket copy constructor does not work.

Creating my own copy constructor causes memory corruption.


回答1:


A way to copy an AVPacket structure: (that works)

AVPacket newPacket(oldPacket);
newPacket->data = reinterpret_cast<uint8_t*>(new uint64_t[(oldPacket->size + FF_INPUT_BUFFER_PADDING_SIZE)/sizeof(uint64_t) + 1]);
memcpy(newPacket->data, oldPacket->data, oldPacket->size);

The reason is that certain decoders use optimizations that require certain byte alignment and padding. Simply allocating oldPacket->size does not allocate enough neither does it allocate at a proper byte alignment.

Please note that this might not work for all cases, for instance if priv is set to something interesting.




回答2:


        AVPacket *newpacket = (AVPacket*)malloc(sizeof(struct AVPacket));
        av_init_packet(newpacket);
        av_packet_ref(newpacket, packet);

        av_packet_unref(packet);
        free(packet);
        packet = nil;

Since function av_dup_packet is deprecated from October 2015 094952 it is better to use instead av_packet_ref / av_packet_unref. New packet should be memory allocated and after initiated with av_init_packet



来源:https://stackoverflow.com/questions/12929330/create-a-copy-of-an-avpacket-structure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!