<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>
<div>okay I could use some tools like valgrind, but first I wanted to understand what is goigng on here.</div>

<div>Also I cannot test every case with valgrind as this code should work with different versions of ffmpeg and various kind of videos.</div>

<div>But anyway thanks for this idea, I might use it after I (might) figured out how to fix this bug correctly</div>

<div> </div>

<div>My main question is: why / how does</div>

<div>
<div>av_free_packet (&packet);</div>

<div>impact</div>

<div><span>the AVFrame picture (set here: avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);)</span>? And why is this only the case for a raw video input?</div>

<div> </div>

<div>If I find out why this is the case I hope to be able to fix this without introducing a memory leak (or you can tell me that I am completly going in the wrong direction).</div>

<div> </div>

<div name="quote" style="margin:10px 5px 5px 10px; padding: 10px 0 10px 10px; border-left:2px solid #C3D9E5; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="margin:0 0 10px 0;"><b>Gesendet:</b> Donnerstag, 05. März 2015 um 14:57 Uhr<br/>
<b>Von:</b> poste9 <poste9@gmail.com><br/>
<b>An:</b> "This list is about using libavcodec, libavformat, libavutil, libavdevice and libavfilter." <libav-user@ffmpeg.org><br/>
<b>Betreff:</b> Re: [Libav-user] OpenCV VideoCapture FFmpeg Module Bug on Linux using Video without a Codec</div>

<div name="quoted-content">
<div>use valgrind</div>

<div class="gmail_extra"> 
<div class="gmail_quote">2015-03-05 10:50 GMT-03:00 Johannes Rehm <span><<a href="hahne.rehm@gmx.de" target="_parent">hahne.rehm@gmx.de</a>></span>:

<blockquote class="gmail_quote" style="margin: 0 0 0 0.8ex;border-left: 1.0px rgb(204,204,204) solid;padding-left: 1.0ex;">
<div>
<div style="font-family: Verdana;font-size: 12.0px;">
<div>
<div>So this is the function where the error happens (look for av_free_packet (&packet);):</div>

<div> </div>

<div>
<div>bool CvCapture_FFMPEG::grabFrame()<br/>
{<br/>
    bool valid = false;<br/>
    int got_picture;</div>

<div>    int count_errs = 0;<br/>
    const int max_number_of_attempts = 1 << 9;</div>

<div>    if( !ic || !video_st )  return false;</div>

<div>    if( ic->streams[video_stream]->nb_frames > 0 &&<br/>
        frame_number > ic->streams[video_stream]->nb_frames )<br/>
        return false;</div>

<div>    av_free_packet (&packet);</div>

<div>    picture_pts = AV_NOPTS_VALUE_;</div>

<div>    // get the next frame<br/>
    while (!valid)<br/>
    {<br/>
        int ret = av_read_frame(ic, &packet);<br/>
        if (ret == AVERROR(EAGAIN)) continue;</div>

<div>        /* else if (ret < 0) break; */</div>

<div>        if( packet.stream_index != video_stream )<br/>
        {<br/>
            av_free_packet (&packet);<br/>
            count_errs++;<br/>
            if (count_errs > max_number_of_attempts)<br/>
                break;<br/>
            continue;<br/>
        }</div>

<div>        // Decode video frame<br/>
        #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)<br/>
<span>            avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);</span><br/>
        #elif LIBAVFORMAT_BUILD > 4628<br/>
                avcodec_decode_video(video_st->codec,<br/>
                                     picture, &got_picture,<br/>
                                     packet.data, packet.size);<br/>
        #else<br/>
                avcodec_decode_video(&video_st->codec,<br/>
                                     picture, &got_picture,<br/>
                                     packet.data, packet.size);<br/>
        #endif</div>

<div>        // Did we get a video frame?<br/>
        if(got_picture)<br/>
        {<br/>
            //picture_pts = picture->best_effort_timestamp;<br/>
            if( picture_pts == AV_NOPTS_VALUE_ )<br/>
                picture_pts = packet.pts != AV_NOPTS_VALUE_ && packet.pts != 0 ? packet.pts : packet.dts;<br/>
            frame_number++;<br/>
            valid = true;<br/>
        }<br/>
        else<br/>
        {<br/>
            count_errs++;<br/>
            if (count_errs > max_number_of_attempts)<br/>
                break;<br/>
        }</div>

<div>        //I need to comment out the following line to prevent an segmentation fault when processing a raw video file without a codec:</div>

<div>        av_free_packet (&packet);<br/>
    }</div>

<div>    if( valid && first_frame_number < 0 )<br/>
        first_frame_number = dts_to_frame_number(picture_pts);</div>

<div>    // return if we have a new picture or not<br/>
    return valid;<br/>
}</div>
</div>

<div>
<div> </div>

<div>I am not even sure if there will be a memory leak if I delete av_free_packet (&packet);</div>

<div>But they might put this command for a reason and I do not want to create a new bug.</div>

<div>Sorry for asking such a basic question: do you know how I can verify that there will be no memory leak (with or without adding av_init_packet before)?</div>

<div> </div>

<div style="margin: 10.0px 5.0px 5.0px 10.0px;padding: 10.0px 0 10.0px 10.0px;border-left: 2.0px solid rgb(195,217,229);">
<div style="margin: 0 0 10.0px 0;"><b>Gesendet:</b> Donnerstag, 05. März 2015 um 14:22 Uhr<br/>
<b>Von:</b> "Rafael Lúcio" <<a href="rafael@inova.in" target="_parent">rafael@inova.in</a>><br/>
<b>An:</b> "This list is about using libavcodec, libavformat, libavutil, libavdevice and libavfilter." <<a href="libav-user@ffmpeg.org" target="_parent">libav-user@ffmpeg.org</a>><br/>
<b>Betreff:</b> Re: [Libav-user] OpenCV VideoCapture FFmpeg Module Bug on Linux using Video without a Codec</div>

<div>
<div class="h5">
<div>
<div>I'm not an expert but maybe I can give u a simpler solution.... just ignore if I'm talking nonsense stuffs..
<div> </div>

<div>maybe if u just av_init_packet before av_free_packet your problem is solved and you don't have memory leak.</div>

<div>I haven't tested this theory, its just an idea.</div>
</div>

<div class="gmail_extra"> 
<div class="gmail_quote">2015-03-05 8:30 GMT-03:00 Johannes Rehm <span><<a href="http://hahne.rehm@gmx.de" target="_blank">hahne.rehm@gmx.de</a>></span>:

<blockquote class="gmail_quote" style="margin: 0 0 0 0.8ex;border-left: 1.0px rgb(204,204,204) solid;padding-left: 1.0ex;">
<div>
<div style="font-family: Verdana;font-size: 12.0px;">
<div>
<div>Hi All,</div>

<div> </div>

<div>there is a known bug in the VideoCapture ffmpeg module of OpenCV. It occurs while loading videos without  a codec:</div>

<div><a href="http://code.opencv.org/issues/1915" target="_blank">http://code.opencv.org/issues/1915</a></div>

<div>I had some time now to debug this bug and found the issue in:</div>

<div>CvCapture_FFMPEG::grabFrame()</div>

<div>In the function</div>

<div>avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);</div>

<div>the AVFrame picture get set. But after</div>

<div>av_free_packet (&packet);</div>

<div>I get: "error: Cannot access memory at address..." for picture.data[0] which leads to a segmentation fault later in the function sws_scale().</div>

<div>I can encode this video with a video codec using ffmpeg or whatever and I get no segmentation fault (memory in picture.data[0] can still be accessed).</div>

<div>Of course there is an easy fix for this bug: Simply comment out / delete</div>

<div>av_free_packet (&packet);</div>

<div>This is working fine for me. Now I can process videos with and without codecs. But I am not sure if this will lead to memory issues if av_free_packet gets not called. Is there anybody who is familiar with the FFmpeg interface of OpenCV and can help me here?</div>

<div>Thanks a lot!</div>

<div> </div>

<div>Best regards,</div>

<div>Johannes Rehm</div>
</div>
</div>
</div>
<br/>
_______________________________________________<br/>
Libav-user mailing list<br/>
<a href="http://Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br/>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br/>
 </blockquote>
</div>
 

<div> </div>
--

<div>
<div>-----------------------------------------------------------------
<div>Rafael Lúcio</div>
</div>
</div>
</div>
_______________________________________________ Libav-user mailing list <a href="Libav-user@ffmpeg.org" target="_parent">Libav-user@ffmpeg.org</a> <a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br/>
_______________________________________________<br/>
Libav-user mailing list<br/>
<a href="Libav-user@ffmpeg.org" target="_parent">Libav-user@ffmpeg.org</a><br/>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br/>
 </blockquote>
</div>
 

<div> </div>
--

<div class="gmail_signature">
<div><img src="https://lh3.googleusercontent.com/-jTO_Hn0p0VY/UUdsZPT_KGI/AAAAAAAAAN4/bpTrpfmgLRc/s389/Rafael.png"/></div>
</div>
</div>
_______________________________________________ Libav-user mailing list Libav-user@ffmpeg.org <a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a></div>
</div>
</div>
</div></div></body></html>