Hi Igor, my comments below<br><br><div class="gmail_quote">2012/11/28 Morduhaev, Igor (<a href="mailto:igorm@stats.com">igorm@stats.com</a>) <span dir="ltr"><<a href="mailto:igorm@stats.com" target="_blank">igorm@stats.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal" style="text-align:left;line-height:13.5pt;background:white;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:"Arial","sans-serif"">I had fps issues when transcoding from avi to mp4(x264). Eventually the problem was in PTS and DTS values, so lines 12-15 where added before </span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">av_interleaved_write_frame</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> function:</span></p>

<p class="MsoNormal" style="text-align:left;line-height:13.5pt;background:white;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:"Arial","sans-serif""> </span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;text-autospace:none;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>1.<span style="font:7.0pt "Times New Roman""> 
</span></span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">AVFormatContext* outContainer = NULL;</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;text-autospace:none;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>2.<span style="font:7.0pt "Times New Roman""> 
</span></span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">avformat_alloc_output_context2(&outContainer, NULL, "mp4", "c:\\test.mp4";</span></p>

<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>3.<span style="font:7.0pt "Times New Roman""> 
</span></span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_H264);</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>4.<span style="font:7.0pt "Times New Roman""> 
</span></span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">AVStream *outStream = avformat_new_stream(outContainer, encoder);</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>5.<span style="font:7.0pt "Times New Roman""> 
</span></span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">// outStream->codec initiation</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>6.<span style="font:7.0pt "Times New Roman""> 
</span></span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">// ...</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;text-autospace:none;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>7.<span style="font:7.0pt "Times New Roman""> 
</span></span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">avformat_write_header(outContainer, NULL);</span></p>
<p class="MsoNormal" style="text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"> </span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>8.<span style="font:7.0pt "Times New Roman""> 
</span></span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">// reading and decoding packet</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>9.<span style="font:7.0pt "Times New Roman""> 
</span></span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">// ...</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>10.</span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">avcodec_encode_video2(outStream->codec,
 &encodedPacket, decodedFrame, &got_frame)</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>11.</span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"> </span></p>

<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>12.</span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">if
 (encodedPacket.pts != AV_NOPTS_VALUE)</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>13.</span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">encodedPacket.pts
 =  av_rescale_q(encodedPacket.pts, outStream->codec->time_base, outStream->time_base);</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>14.</span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">if
 (encodedPacket.dts != AV_NOPTS_VALUE)</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>15.</span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">encodedPacket.dts
 = av_rescale_q(encodedPacket.dts, outStream->codec->time_base, outStream->time_base);</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;line-height:13.5pt;background:#eeeeee;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>16.</span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"> </span></p>

<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;text-autospace:none;direction:ltr">
<span style="font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"><span>17.</span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">av_interleaved_write_frame(outContainer,
 &encodedPacket)</span></p>
<p style="margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt;text-align:left;text-autospace:none;direction:ltr">
<span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt"> </span></p>
<p class="MsoNormal" style="margin-bottom:12.0pt;text-align:left;line-height:13.5pt;background:white;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:"Arial","sans-serif"">After reading many posts I still do not understand:</span></p>
<p class="MsoNormal" style="margin-left:22.5pt;text-align:left;line-height:13.5pt;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:"Arial","sans-serif""><span>1.<span style="font:7.0pt "Times New Roman"">   
</span></span></span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">outStream->codec->time_base</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> =
 1/25 and </span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">outStream->time_base</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> = 1/12800. The
 1st one was set by me but I cannot figure out why and who set 12800? I noticed that before line (7)
</span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">outStream->time_base</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> = 1/90000 and right after
 it it changes to 1/12800, why?</span></p></div></div></blockquote><div><br></div><div>Read docs:</div><div>1. AVStream::time_base: </div><div>   This is the fundamental unit of time (in seconds) in terms<br>      of which frame timestamps are represented.<br>
     <br>      decoding: set by libavformat<br>      encoding: set by libavformat in av_write_header. The muxer may use the<br>      user-provided value of @ref AVCodecContext.time_base "codec->time_base"<br>
      as a hint.</div><div><br></div><div>So for AVStream you shold not set time-base - it will be setted automatic after av_write_header() call</div><div><br></div><div>2. AVCodecContext::time_base:</div><div> This is the fundamental unit of time (in seconds) in terms<br>
      of which frame timestamps are represented. For fixed-fps content,<br>      timebase should be 1/framerate and timestamp increments should be<br>      identically 1.<br>      - encoding: MUST be set by user.<br>      - decoding: Set by libavcodec. </div>
<div><br></div><div>So for AVCodecContext you MUST set time-base for encoding</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal" style="margin-left:22.5pt;text-align:left;line-height:13.5pt;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:"Arial","sans-serif"">When I transcode from avi to avi, meaning changing the line (2) to
</span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">avformat_alloc_output_context2(&outContainer, NULL, "avi", "c:\\test.avi";</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">
 , so before and after line (7) </span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">outStream->time_base</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">
 remains always 1/25 and not like in mp4 case, why?</span></p></div></div></blockquote><div><br></div><div>see above </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple"><div>
<p class="MsoNormal" style="margin-left:22.5pt;text-align:left;line-height:13.5pt;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:"Arial","sans-serif""><span>2.<span style="font:7.0pt "Times New Roman"">   
</span></span></span><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">What is the difference between </span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">time_base</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> of </span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">outStream->codec</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> and </span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">outStream</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">?</span></p>
</div></div></blockquote><div><br></div><div>see above</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><div>

<p class="MsoNormal" style="margin-left:22.5pt;text-align:left;line-height:13.5pt;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:"Arial","sans-serif""><span>3.<span style="font:7.0pt "Times New Roman"">   
</span></span></span><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">To calc the pts </span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">av_rescale_q</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> does:
 takes 2 </span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">time_base</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">, multiplies their fractions
 in cross and then compute the pts. Why it does this in this way? As I debugged, the </span><span style="background:#eeeeee;font-size:10.5pt;font-family:Consolas;padding:0cm;border:none windowtext 1.0pt">encodedPacket.pts</span><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> has
 value incremental by 1, so why changing it if it does has value?</span></p></div></div></blockquote><div><br></div><div>Generaly coder time base is inverse Frame Rate, so we can increment PTS simple by 1 for next frame, but Stream time base can depend on some format/codec specifications. Packets PTS/DTS must be in Stream time-base units before writing so rescaling between coder and stream time bases is required.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><div>
<p class="MsoNormal" style="margin-left:22.5pt;text-align:left;line-height:13.5pt;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:"Arial","sans-serif""><span>4.<span style="font:7.0pt "Times New Roman"">   
</span></span></span><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">At the beginning the dts value is -2 and after each rescaling it still has negative number, but despite this the video played correctly!
 Shouldn't it be positive?</span></p></div></div></blockquote><div><br></div><div>Its strange for me :-)</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple"><div>
<p class="MsoNormal" style="margin-left:22.5pt;text-align:left;line-height:13.5pt;vertical-align:baseline;direction:ltr">
<span style="font-size:10.5pt;font-family:"Arial","sans-serif""><br></span></p></div></div></blockquote></div>