leandromoreira / ffmpeg-libav-tutorial

FFmpeg libav tutorial - learn how media works from basic to transmuxing, transcoding and more. Translations: πŸ‡ΊπŸ‡Έ πŸ‡¨πŸ‡³ πŸ‡°πŸ‡· πŸ‡ͺπŸ‡Έ πŸ‡»πŸ‡³ πŸ‡§πŸ‡·
https://github.com/leandromoreira/ffmpeg-libav-tutorial
BSD 3-Clause "New" or "Revised" License
9.78k stars 937 forks source link

Why is there a while loop when decoding packets hello_world #96

Closed LentilStew closed 3 years ago

LentilStew commented 3 years ago

image decode_packet() function in the 0_hello_world.c file what does that while loop do? Can a packet contain more than one frame?

TOTON95 commented 3 years ago

@LentilStew if I'm not wrong, this while loop is to send raw compressed AvPackets to the decoder until the multimedia source is no longer able to provide them, at this point response would be a negative number and you'll have to figure out if the packet is bad or we already reached an EOF situation.

Edit: and yes, the packet can contain a single video frame, or several complete audio frames.

leandromoreira commented 3 years ago

I think you can also see it like this:

# DTS - the order the frames are sent [decoder] - the REAL binary stream 
# (i.e. a Bth frame depends on previsous I frame and next P frame to be fully decodable... and so )
5,2,1,3,

# PTS - the order the frames must be presented [played] - the DECODED binary way
# (i.e. the way you want to see the frames)
1,2,3,4,5,6

This example you'll read/decode frames 5 and 2 and only then you will decode the frame 1.

Therefore in order for you to decode the "first frame" (PTS order) you might need to open and decode more other frames/packets only then to decode the nth frame you want.

A good introduction it's here and there.