user1914692
user1914692

Reputation: 3073

How to know the size of one AVPacket in ffmpeg?

I am using ffmpeg library. I want to know how much memory one packet can take. I debug to check the members in on AVPacket, and none of them seem reasonable, such as AVPacket.size, ec.

Upvotes: 1

Views: 3951

Answers (2)

jyavenard
jyavenard

Reputation: 2095

If you provide your own data buffer, it needs to have a size of mininum FF_MIN_BUFFER_SIZE. You would then set the AVPacket.size to the allocated size, and AVPacket.data to the memory you've allocated.

Note that all FFmpeg decoding routine will simply fail if you provide your own buffer and it's too small.

The other possibility, is let FFmpeg calculates the optimal size for you. Then do something like:

AVPacket pkt;
pkt.size = 0;
pkt.data = NULL; // <-- the critical part is there
int got_output = 0;

ret = avcodec_encode_audio2(ctx, &pkt, NULL, &got_output);

and provide this AVPacket to the encoding codec. Memory will be allocated automatically. You will have to call av_free_packet upon return from the encoder and if got_output is set to 1. FFmpeg will automatically free the AVPacket content in case of error.

Upvotes: 3

Roman Ryltsov
Roman Ryltsov

Reputation: 69662

AVPacket::size holds the size of the referenced data. Because it is a generic container for data, there can be no definite answer to the question

how much memory one packet can take

It can actually take from zero to a lot. Everything depends on data type, codec and other related parameters.

From FFmpeg examples:

static void audio_encode_example(const char *filename)
{
    // ...
    AVPacket pkt;
    // ...
    ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
    // ...
    if (got_output) {
        fwrite(pkt.data, 1, pkt.size, f); // <<--- AVPacket.size
        av_free_packet(&pkt);
    }

Upvotes: 1

Related Questions