In the avpacket there are two more similar functions av_packet_free and Av_free_packet, when looking at the API is a bit confused, do not know how to use, then deliberately read the source, in this record
Not much to say, directly on the source
Av_free_packet actually empties the contents of data and buf in the PKT and does not empty the PKT pointer, and we can see that its function internally calls the Av_buffer_unref
void Av_free_packet ( Avpacket *pkt)
{
if (PKT) {
if (pkt->buf)
av_buffer_unref (&pkt->buf);
Pkt->data = NULL;
Pkt->size = 0;
Av_packet_free_side_data (PKT);
}
}
Simply put, av_buffer_unref only buf the PKT null
void Av_buffer_unref (Avbufferref **buf)
{
if (!buf | |!*buf)
return;
Buffer_replace (buf, NULL);
}
And the sidedata is to remove the additional information, if not understood here, it is recommended to learn more about the Avpacket Sidedata, he is a similar to the array of things, here is not much to say the
void Av_packet_free_side_ Data (Avpacket *pkt)
{
int i;
for (i = 0; i < pkt->side_data_elems; i++)
av_freep (&pkt->side_data[i].data);
Av_freep (&pkt->side_data);
Pkt->side_data_elems = 0;
}
The Av_packet_free is to first empty the contents of the PKT, and then empty the pointer, so that PKT completely unusable, if need re-use, need to reallocate memory
void Av_packet_free (Avpacket **pkt)
{
if (!pkt | |!*pkt)
return;
Av_packet_unref (*PKT);
Av_freep (PKT);
}
About the use of both:
Av_free_packet is to empty the data inside, if not empty what will happen, for a simple example, a char array size of 128, there are 100 bytes of content, the second use you did not empty the first time, the second data size is 60, Then the first 40 bytes of data will still be retained, resulting in data redundancy, greatly likely to affect your processing (this is related to their own processing, not necessarily, just a simple example, you can write a simple example test)
Av_packet_free similar to free (p;) p=null;
If you still do not know the place or have other questions, please add QQ group 445236076 discussion