问题
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