[Libav-user] Cloning an AVFormatContext

c0olcast c0olcast at gmail.com
Wed Oct 26 01:26:15 CEST 2011

Hello Everyone,

    First let me explain what I'm trying to do, so that you all have an idea
of what I want to accomplish and perhaps suggest a better approach than the
one I'm think of.

    What I want to do is read the streams independently from and input file.
That is, I want to be able to be able to fetch the packets from one stream
without affecting the "position" inside the file for another stream, some
what what Microsoft provides in AVI files. I did this by creating various
AVFomartContext by doing multiple calls to av_open_input_file, one for each
stream in the file. Each of the resulting AVFormatContext was then set to
drop packets for all streams that were not for the stream that that
particular AVFormatContext was to handle. That works but it creates an
initial analysis pverhead which I want to avoid, not to mention the wasted
resources, which are not much, but then still wasted.

    What've I've done is very basic, create an AVFormatContext and try to
clone that for every stream in the file (BTW I will only be reading off
files). In the clone process what I do is allocated a new AVFormatContext
struct, then assign the value of original AVFormatContext, as to copy all
basic stuff and structs to that. Then I create a new ByteIOContext based on
the file's name, copy stuff over from the orignal ByteIOContext, such as
positions, buffers, etc, kind of replicating where the file was left off
when it was originally opened and analyzed. Then I make copies of the
buffered packets and the "raw_packet" of the original AVFormatContext.

    Since I used the streams and codecs structs found in the original
AVFormatContext I thought it was going to be ok, and have independent access
to the file's streams thanks to the use of the independent ByteIOContext's.
The approach seams to somewhat work, but I get problems while reading from
some streams. It work on MPEG-PS but not in AVI, or MKV.

   I would really appreciate any light you guys could shed on this. Thanks a
bunch for your time and efforts.


View this message in context: http://libav-users.943685.n4.nabble.com/Cloning-an-AVFormatContext-tp3938713p3938713.html
Sent from the libav-users mailing list archive at Nabble.com.

More information about the Libav-user mailing list