Differences and usage of avpacket processing functions Av_free_packet () and Av_packet_free () in FFmpeg

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.