<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Hi,<BR> <BR>I am trying to decode my network dvr h264 socket stream directly using av_parser_parse2 but I get a crash when I try to parse the last portion of the buffer I'm passing to the parser. <BR> <BR>For example, if I read 80000 bytes from socket I can parse and then decode most of the data but the last piece passed to av_parser_parse2 causes a crash. <BR> <BR>I have tried different ways to make sure I not trying to pass more than there is in the buffer to the parser.<BR> <BR>I'm not very knowledgeable regarding h264 but I suspect it might be because the parser can't parse the last portion of data  due to it wanting more from the socket.<BR> <BR>Is there something I can do to avoid this crash ?<BR> <BR>Here is my loop just in case it is something I am doing wrong..<BR> <BR><p align="LEFT"><font color="#79017b" size="2"><font color="#79017b" size="2">int</font></font><font size="2"> retval;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">    </font><font color="#007585" size="2"><font color="#007585" size="2">uint8_t</font></font><font size="2"> *inbuf = </font><font color="#79017b" size="2"><font color="#79017b" size="2">new</font></font><font size="2"> </font><font color="#007585" size="2"><font color="#007585" size="2">uint8_t</font></font><font size="2"> [20000000];</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">    </font><font color="#79017b" size="2"><font color="#79017b" size="2">int</font></font><font size="2"> inbuf_start = 0;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">    </font><font color="#79017b" size="2"><font color="#79017b" size="2">int</font></font><font size="2"> inbuf_len = 0;</font></p> <BR><font size="2">
<BR><p align="LEFT">    inbuf_start = 0;</p>
<BR><p align="LEFT">    inbuf_len = 0;</p>
<BR></font><p align="LEFT"><font size="2">    </font><font color="#79017b" size="2"><font color="#79017b" size="2">while</font></font><font size="2"> (socket-></font><font color="#497219" size="2"><font color="#497219" size="2">waitForReadyRead</font></font><font size="2">())</font></p> <BR><font size="2">
<BR><p align="LEFT">    {</p>
<BR></font><p align="LEFT"><font size="2">        </font><font color="#79017b" size="2"><font color="#79017b" size="2">if</font></font><font size="2">(socket-></font><font color="#497219" size="2"><font color="#497219" size="2">bytesAvailable</font></font><font size="2">() > </font><font color="#00424b" size="2"><font color="#00424b" size="2">BUFFER_SIZE</font></font><font size="2">*2 )</font></p> <BR><font size="2">
<BR><p align="LEFT">        {</p>
<BR></font><p align="LEFT"><font size="2">        </font><font color="#007585" size="2"><font color="#007585" size="2">QByteArray</font></font><font size="2">   ba = socket-></font><font color="#497219" size="2"><font color="#497219" size="2">readAll</font></font><font size="2">();</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">        </font><font color="#497219" size="2"><font color="#497219" size="2">memcpy</font></font><font size="2">(inbuf + inbuf_len, ba.</font><font color="#497219" size="2"><font color="#497219" size="2">data</font></font><font size="2">(), ba.</font><font color="#497219" size="2"><font color="#497219" size="2">size</font></font><font size="2">() );</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">        inbuf_len += ba.</font><font color="#497219" size="2"><font color="#497219" size="2">size</font></font><font size="2">();</font></p> <BR><font size="2">
<BR><p align="LEFT"></p>
<BR></font><p align="LEFT"><font size="2">        </font><font color="#497219" size="2"><font color="#497219" size="2">qDebug</font></font><font size="2">() << </font><i><font color="#7a2d0a" size="2"><font color="#7a2d0a" size="2">"read bytes in buffer "</font></font></i><font size="2"><<ba.</font><font color="#497219" size="2"><font color="#497219" size="2">size</font></font><font size="2">();</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">        </font><font color="#79017b" size="2"><font color="#79017b" size="2">if</font></font><font size="2"> (ba.</font><font color="#497219" size="2"><font color="#497219" size="2">size</font></font><font size="2">() == 0)</font></p> <BR><font size="2">
<BR><p align="LEFT">        {</p>
<BR></font><p align="LEFT"><font size="2">            </font><font color="#00424b" size="2"><font color="#00424b" size="2">cerr</font></font><font size="2"> << </font><i><font color="#7a2d0a" size="2"><font color="#7a2d0a" size="2">"read 0 bytes data."</font></font></i><font size="2"> << </font><b><font color="#007585" size="2"><font color="#007585" size="2">endl</font></font></b><font size="2">;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">            </font><font color="#79017b" size="2"><font color="#79017b" size="2">continue</font></font><font size="2">;</font></p> <BR><font size="2">
<BR><p align="LEFT">        }</p>
<BR><p align="LEFT"></p>
<BR></font><p align="LEFT"><font size="2">        </font><font color="#79017b" size="2"><font color="#79017b" size="2">while</font></font><font size="2"> (inbuf_len)</font></p> <BR><font size="2"> <BR> <BR> <BR>
<BR><p align="LEFT">        {</p>
<BR></font><p align="LEFT"><font size="2">            </font><font color="#497219" size="2"><font color="#497219" size="2">qDebug</font></font><font size="2">() << </font><i><font color="#7a2d0a" size="2"><font color="#7a2d0a" size="2">"before parse total to parse = "</font></font></i><font size="2"><<ba.</font><font color="#497219" size="2"><font color="#497219" size="2">size</font></font><font size="2">();</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">            </font><font color="#497219" size="2"><font color="#497219" size="2">qDebug</font></font><font size="2">()  << </font><i><font color="#7a2d0a" size="2"><font color="#7a2d0a" size="2">"parsed already "</font></font></i><font size="2"><< inbuf_start;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">            </font><font color="#497219" size="2"><font color="#497219" size="2">qDebug</font></font><font size="2">() << </font><i><font color="#7a2d0a" size="2"><font color="#7a2d0a" size="2">"left to parse "</font></font></i><font size="2"><<inbuf_len;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">            </font><b><font color="#007585" size="2"><font color="#007585" size="2">av_init_packet</font></font></b><font size="2">(&packet2);</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">            packet2.</font><font color="#00424b" size="2"><font color="#00424b" size="2">data</font></font><font size="2"> = 0;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">            packet2.</font><font color="#00424b" size="2"><font color="#00424b" size="2">size</font></font><font size="2"> = 0;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">            </font><font color="#007585" size="2"><font color="#007585" size="2">uint8_t</font></font><font size="2"> *pout;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">            </font><font color="#79017b" size="2"><font color="#79017b" size="2">int</font></font><font size="2"> pout_len;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">                len = </font><b><font color="#007585" size="2"><font color="#007585" size="2">av_parser_parse2</font></font></b><font size="2">(parser, c, &pout, &pout_len,</font></p> <BR><font size="2">
<BR><p align="LEFT">                        inbuf + inbuf_start, inbuf_len,</p>
<BR></font><p align="LEFT"><font size="2">                        </font><font color="#497219" size="2"><font color="#497219" size="2">AV_NOPTS_VALUE</font></font><font size="2">, </font><font color="#497219" size="2"><font color="#497219" size="2">AV_NOPTS_VALUE</font></font><font size="2">, 0);</font></p> <BR><font size="2">
<BR><p align="LEFT"></p>
<BR><p align="LEFT">            inbuf_start += len;</p>
<BR><p align="LEFT">            inbuf_len -= len;</p>
<BR></font><p align="LEFT"><font size="2">            packet2.</font><font color="#00424b" size="2"><font color="#00424b" size="2">data</font></font><font size="2"> = pout;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">            packet2.</font><font color="#00424b" size="2"><font color="#00424b" size="2">size</font></font><font size="2"> = pout_len;</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">            </font><font color="#79017b" size="2"><font color="#79017b" size="2">if</font></font><font size="2"> (len)</font></p> <BR><font size="2">
<BR><p align="LEFT">            {</p>
<BR></font><p align="LEFT"><font size="2">                retval = </font><b><font color="#007585" size="2"><font color="#007585" size="2">avcodec_decode_video2</font></font></b><font size="2">(c, picture, &got_picture, &packet2);</font></p><font size="2">
</font><BR><p align="LEFT"><font size="2">                </font><font color="#79017b" size="2"><font color="#79017b" size="2">if</font></font><font size="2"> (got_picture && retval > 0)</font></p> <BR><font size="2">
<BR><p align="LEFT">                {</p>
<BR></font><p align="LEFT"><font size="2">                   </font><b><font color="#007585" size="2"><font color="#007585" size="2">display_frame</font></font></b><font size="2">(c-></font><font color="#00424b" size="2"><font color="#00424b" size="2">pix_fmt</font></font><font size="2">, picture, screen_pixz,  screen_contextz,  wind,  stride);</font></p> <BR><font size="2">
<BR><p align="LEFT">                   </p>
<BR><p align="LEFT">                }</p>
<BR><p align="LEFT">            }</p>
<BR><p align="LEFT"></p>
<BR></font><p align="LEFT"><font size="2">            </font><b><font color="#007585" size="2"><font color="#007585" size="2">av_free_packet</font></font></b><font size="2">(&packet2);</font></p> <BR><font size="2">
<BR><p align="LEFT">        }</p>
<BR><p align="LEFT">        </p>
<BR><p align="LEFT">    }</p>
<BR>    }<BR></font> <BR> <BR>Thank you.<BR> <BR>Brendan<BR> <BR>                                       </div></body>
</html>