<div dir="ltr"><div><br></div><div>I am using a <b>AVFormatContext</b> in conjunction with an <b>avio_ctx_buffer</b> to read streaming data into a demuxer. <br></div><div><br></div><div>The source data is received via a socket and injected via the read_packet() calback. When no data is available, I let the callback sleep. I have verified that the data is read by the callback function, so it is available.  </div><div><br></div><div>My video track is always at the same index so I have no need for searching for streams.<br></div><div><br></div><div>When receiving the AVPackets I deal with them in my own custom functions.</div><div><br></div><div>I am basically replacing a flaky homebrewed demuxer with a more standard one. </div><div><br></div><div>The problem is that when I start it up, the context reads almost 3MB before it returns the first AVPacket.</div><div><br></div><div>I see various topics online on how to reduce the analysis time, but I am unable to reduce mine.</div><div><br></div><div>Would any of you guys know where to look?</div><div><br></div><div>Kind regards</div><div><br></div><div>Jesper</div><div><br></div><div><br></div><div><br></div><div><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;line-height:19px;white-space:pre"><div><div style="line-height:19px"><div><span style="color:rgb(86,156,214)">static</span> <span style="color:rgb(86,156,214)">int</span> <span style="color:rgb(220,220,170)">read_packet</span>(<span style="color:rgb(86,156,214)">void</span> <span style="color:rgb(86,156,214)">*</span><span style="color:rgb(156,220,254)">opaque</span>, <span style="color:rgb(86,156,214)">uint8_t</span> <span style="color:rgb(86,156,214)">*</span><span style="color:rgb(156,220,254)">buf</span>, <span style="color:rgb(86,156,214)">int</span> <span style="color:rgb(156,220,254)">buf_size</span>)</div><div>{</div><div>    //sleep while no data available</div><div>    //Read bytes into buf</div><div>    <span style="color:rgb(197,134,192)">return</span> buf_size;</div><div>}</div></div></div><div><span style="color:rgb(86,156,214)"><br></span></div><div><span style="color:rgb(86,156,214)">void*</span> <span style="color:rgb(220,220,170)">DemuxThread</span>(<span style="color:rgb(86,156,214)">void*</span><span style="color:rgb(156,220,254)">pParam</span>)</div><div>{</div><div>    <span style="color:rgb(220,220,170)">printf</span>(<span style="color:rgb(206,145,120)">"Starting demux thread</span><span style="color:rgb(215,186,125)">\r\n</span><span style="color:rgb(206,145,120)">"</span>);</div><br><div>    <span style="color:rgb(86,156,214)">static</span> AVFormatContext *fmt_ctx = <span style="color:rgb(86,156,214)">NULL</span>;</div><div>    <span style="color:rgb(197,134,192)">if</span> (!(fmt_ctx = <span style="color:rgb(220,220,170)">avformat_alloc_context</span>())) {</div><div>        <span style="color:rgb(220,220,170)">fprintf</span>(stderr, <span style="color:rgb(206,145,120)">"Could not allocate fmt_ctx</span><span style="color:rgb(215,186,125)">\n</span><span style="color:rgb(206,145,120)">"</span>);</div><div>        <span style="color:rgb(220,220,170)">exit</span>(<span style="color:rgb(181,206,168)">1</span>);</div><div>    }</div><div><br></div><div>    <span style="color:rgb(86,156,214)">size_t</span> avio_ctx_buffer_size = <span style="color:rgb(181,206,168)">4096</span>;</div><div>    <span style="color:rgb(86,156,214)">uint8_t</span>* avio_ctx_buffer = (<span style="color:rgb(86,156,214)">uint8_t</span>*)<span style="color:rgb(220,220,170)">av_malloc</span>(avio_ctx_buffer_size);</div><div>    <span style="color:rgb(197,134,192)">if</span> (!avio_ctx_buffer) {</div><div>        <span style="color:rgb(220,220,170)">printf</span>(<span style="color:rgb(206,145,120)">"error allocating avio_ctx_buffer</span><span style="color:rgb(215,186,125)">\r\n</span><span style="color:rgb(206,145,120)">"</span>);</div><div>        <span style="color:rgb(220,220,170)">exit</span>(<span style="color:rgb(181,206,168)">1</span>);</div><div>    }</div><div>    AVIOContext* avio_ctx = <span style="color:rgb(220,220,170)">avio_alloc_context</span>(avio_ctx_buffer, avio_ctx_buffer_size, <span style="color:rgb(181,206,168)">0</span>,  <span style="color:rgb(86,156,214)">NULL</span>, &read_packet, <span style="color:rgb(86,156,214)">NULL</span>, <span style="color:rgb(86,156,214)">NULL</span>);</div><br><div>    <span style="color:rgb(197,134,192)">if</span> (!avio_ctx) {</div><div>        <span style="color:rgb(220,220,170)">printf</span>(<span style="color:rgb(206,145,120)">"error allocating avio_ctx</span><span style="color:rgb(215,186,125)">\r\n</span><span style="color:rgb(206,145,120)">"</span>);</div><div>        <span style="color:rgb(220,220,170)">exit</span>(<span style="color:rgb(181,206,168)">1</span>);</div><div>    }</div><div>    </div><div>    <span style="color:rgb(156,220,254)">fmt_ctx</span>-><span style="color:rgb(156,220,254)">pb</span> = avio_ctx;</div><div><br></div><div>    <span style="color:rgb(197,134,192)">if</span> (<span style="color:rgb(220,220,170)">avformat_open_input</span>(&fmt_ctx, <span style="color:rgb(86,156,214)">NULL</span>, <span style="color:rgb(86,156,214)">NULL</span>, <span style="color:rgb(86,156,214)">NULL</span>) < <span style="color:rgb(181,206,168)">0</span>)</div><div>    {</div><div>        <span style="color:rgb(220,220,170)">fprintf</span>(stderr, <span style="color:rgb(206,145,120)">"Could not open source</span><span style="color:rgb(215,186,125)">\n</span><span style="color:rgb(206,145,120)">"</span>);</div><div>        <span style="color:rgb(220,220,170)">exit</span>(<span style="color:rgb(181,206,168)">1</span>);</div><div>    }</div><div>    </div><div>    AVPacket pkt;<br></div><div>    <span style="color:rgb(220,220,170)">av_init_packet</span>(&pkt);</div><div>    <span style="color:rgb(156,220,254)">pkt</span>.<span style="color:rgb(156,220,254)">data</span> = <span style="color:rgb(86,156,214)">NULL</span>;</div><div>    <span style="color:rgb(156,220,254)">pkt</span>.<span style="color:rgb(156,220,254)">size</span> = <span style="color:rgb(181,206,168)">0</span>;</div><div>    </div><div>    <span style="color:rgb(197,134,192)">while</span>(<span style="color:rgb(220,220,170)">av_read_frame</span>(fmt_ctx, &pkt) >= <span style="color:rgb(181,206,168)">0</span> && running)</div><div>    {</div><div>        <span style="color:rgb(197,134,192)">switch</span>(<span style="color:rgb(156,220,254)">pkt</span>.<span style="color:rgb(156,220,254)">stream_index</span>)<br></div><div>        {   </div><div>            <span style="color:rgb(197,134,192)">case</span> <span style="color:rgb(181,206,168)">0</span>:<span style="color:rgb(106,153,85)"> //video</span></div><div>                {</div><div>                    <span style="color:rgb(220,220,170)">printf</span>(<span style="color:rgb(206,145,120)">"Demuxer got video, %d</span><span style="color:rgb(215,186,125)">\r\n</span><span style="color:rgb(206,145,120)">"</span>, readbytes);</div><div>                }<br></div><div>                <span style="color:rgb(197,134,192)">break</span>;</div><div>            <span style="color:rgb(197,134,192)">case</span> <span style="color:rgb(181,206,168)">1</span>:<span style="color:rgb(106,153,85)"> //Audio</span></div><div>                {</div><div>                    <span style="color:rgb(220,220,170)">printf</span>(<span style="color:rgb(206,145,120)">"Demuxer got audio, %d</span><span style="color:rgb(215,186,125)">\r\n</span><span style="color:rgb(206,145,120)">"</span>, readbytes);</div><div>                }<br></div><div>                <span style="color:rgb(197,134,192)">break</span>;</div><div>            <span style="color:rgb(197,134,192)">default</span>:<br></div><div>                <span style="color:rgb(220,220,170)">printf</span>(<span style="color:rgb(206,145,120)">"Unknown payload</span><span style="color:rgb(215,186,125)">\r\n</span><span style="color:rgb(206,145,120)">"</span>);</div><div>                <span style="color:rgb(197,134,192)">break</span>;</div><div>        };</div><div>        <span style="color:rgb(220,220,170)">av_free_packet</span>(&pkt);    </div><div>    }</div><div>    <span style="color:rgb(220,220,170)">printf</span>(<span style="color:rgb(206,145,120)">"Ending demux thread</span><span style="color:rgb(215,186,125)">\r\n</span><span style="color:rgb(206,145,120)">"</span>);</div><div>    <span style="color:rgb(197,134,192)">return</span> <span style="color:rgb(181,206,168)">0</span>;</div><div>}</div></div><div><br></div></div><div><br></div><br clear="all"><div><br></div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><br></div></div>