<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">I’m using libav for my own playback engine and it works like a charm.<div><br></div><div>But I have trouble with seeking ...</div><div>I’d like to pull individual frames from a stream at a given index.</div><div>To do that I’m using the following code:</div><div><br></div><div><div style="margin: 0px; font-family: Menlo;"><span style="color: #703daa">int64_t</span> seekTarget = theFrameIndex;</div><div style="margin: 0px; font-family: Menlo;"><span style="color: #bb2ca2">int</span> res = <span style="color: #31595d">avformat_seek_file</span>(pFormatCtx, <span style="color: #bb2ca2">self</span>.<span style="color: #4f8187">videoStreamIndex</span>, seekTarget, seekTarget, seekTarget, <span style="color: #78492a">AVSEEK_FLAG_FRAME</span>);</div></div><div style="margin: 0px; font-family: Menlo;">. . . </div><div style="margin: 0px; font-family: Menlo;"><div style="margin: 0px;"><span style="color: #bb2ca2">while</span> (<span style="color: #31595d">av_read_frame</span>(pFormatCtx, &packet)>=<span style="color: #272ad8">0</span>)</div><div style="margin: 0px;">{</div><div style="margin: 0px;">    <span style="color: rgb(187, 44, 162);">int</span> idx = packet.<span style="color: #4f8187">stream_index</span>;</div><p style="margin: 0px; min-height: 14px;">        </p><div style="margin: 0px;">    <span style="color: #bb2ca2">if</span>(idx != <span style="color: #bb2ca2">self</span>.<span style="color: #4f8187">videoStreamIndex</span>)</div><div style="margin: 0px;">    {</div><div style="margin: 0px;">        <span style="color: #31595d">av_free_packet</span>(&packet);</div><div style="margin: 0px;">        <span style="color: #bb2ca2">continue</span>;</div><div style="margin: 0px;">    }</div><div style="margin: 0px; min-height: 14px;"><br></div><div style="margin: 0px;">    <span style="color: #bb2ca2">int</span> len = <span style="color: #31595d">avcodec_decode_video2</span>(pCodecCtx, pFrame, &frameFinished, &packet);</div><div style="margin: 0px;">    <span style="color: #bb2ca2">if</span> (len < <span style="color: #272ad8">0</span>)</div><div style="margin: 0px;">    {</div><div style="margin: 0px;">        <span style="color: #31595d">av_free_packet</span>(&packet);</div><div style="margin: 0px;">        <span style="color: #bb2ca2">break</span>;</div><div style="margin: 0px;">    }     </div><div style="margin: 0px; color: rgb(0, 132, 0);"><br></div><div style="margin: 0px;">    <span style="color: #bb2ca2">if</span>(!frameFinished)</div><div style="margin: 0px;">    {</div><div style="margin: 0px;">        <span style="color: #31595d">av_free_packet</span>(&packet);</div><div style="margin: 0px;">        <span style="color: #bb2ca2">continue</span>;</div><div style="margin: 0px;">    }</div><div style="margin: 0px;"><br></div></div><div style="margin: 0px;"><div style="font-family: Menlo; margin: 0px; color: rgb(49, 89, 93);"><span style="color: #000000">    presentationTimeStamp = </span>av_frame_get_best_effort_timestamp<span style="color: #000000">(pFrame);</span></div><p style="font-family: Menlo; margin: 0px; min-height: 14px;"><span style="color: rgb(79, 129, 135);">    </span></p><p style="font-family: Menlo; margin: 0px; min-height: 14px;"><span style="color: rgb(79, 129, 135);">    AVRational</span> timeBase = pFormatCtx-><span style="color: rgb(79, 129, 135);">streams</span>[<span style="color: rgb(187, 44, 162);">self</span>.<span style="color: rgb(79, 129, 135);">videoStreamIndex</span>]-><span style="color: rgb(79, 129, 135);">time_base</span>;</p><div style="font-family: Menlo; margin: 0px;"><span style="color: #bb2ca2">    float</span> ptsInSeconds = presentationTimeStamp * <span style="color: #31595d">av_q2d</span>(timeBase);</div><div style="font-family: Menlo; margin: 0px;"><span style="color: #bb2ca2">    int</span> frameIndex = (<span style="color: rgb(187, 44, 162);">int</span>)(ptsInSeconds * <span style="color: #bb2ca2">self</span>.<span style="color: #4f8187">videoFrameRate)</span>;</div><div style="font-family: Menlo; margin: 0px;">. . .</div><div style="font-family: Menlo; margin: 0px;">}</div><div style="font-family: Menlo; margin: 0px;"><br></div><div style="margin: 0px;">And that’s how I am pulling the duration that is used to seek:</div><div style="margin: 0px;"><br></div><div style="font-family: Menlo; margin: 0px;"><div style="margin: 0px;"><span style="color: #bb2ca2">float</span> mediaDurationInSecs = pFormatCtx-><span style="color: #4f8187">duration</span> / <span style="color: #272ad8">1000000.0f</span>;</div><div style="margin: 0px;"><span style="color: #bb2ca2">int</span> durationInFrames = (<span style="color: rgb(187, 44, 162);">int</span>)(mediaDurationInSecs * <span style="color: #bb2ca2">self</span>.<span style="color: #4f8187">videoFrameRate</span>);</div></div></div><div><br></div><div>The slider used for seeking returns a value from 0-1 which is then multiplied by the duration to get the frame index.</div><div><br></div><div>I’m not getting any errors but the frames returned don’t belong to the frame index provided.</div><div>It looks like the AVSEEK_FLAG_FRAME doesn’t do what I’d expect.</div><div><br></div><div>I would convert the frame index to stream time units but I don’t know why.</div><div>I’d need a frame duration and I don’t have one at that point…</div><div><br></div><div>I’d appreciate any hint!!</div><div>Thanks!</div><div><div apple-content-edited="true">
<div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><br></div></div></div></body></html>