<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">Am 22.11.2012 04:32, schrieb hatred:<br>
    </div>
    <blockquote
cite="mid:CAP9VioeNu3LvN9wxxMGdthiTrGRK5P5ypT+JJj5qWY5UFCbY9g@mail.gmail.com"
      type="cite">
      <div>Hi, Hannes!</div>
      <div><br>
      </div>
      <div>My comments below. Worked code also.</div>
      <div><br>
      </div>
      <div>2012/11/21 Hannes Wuerfel <span dir="ltr"><<a
            moz-do-not-send="true"
            href="mailto:hannes.wuerfel@student.hpi.uni-potsdam.de"
            target="_blank">hannes.wuerfel@student.hpi.uni-potsdam.de</a>></span><br>
      </div>
      <blockquote class="gmail_quote" style="margin:0 0 0
        .8ex;border-left:1px #ccc solid;padding-left:1ex">Am 21.11.2012
        07:42, schrieb hatred:
        <div>
          <div class="h5"><br>
          </div>
        </div>
        In my case, I'd like to manipulate video frames of the file with
        some image processing kernels etc. and write the file with the
        same codecs they have back to disc, but of cause with processed
        frames.<br>
        So basically I need a "transcoder" from the codec to the same
        codec.<br>
        <br>
        What I didn't have is your code fragment:<br>
        "if (outCtx.videoCodecCtx->coded_frame->pts !=
        AV_NOPTS_VALUE)<br>
        {<br>
                                outPkt.pts =
        av_rescale_q(outCtx.videoCodecCtx->coded_frame->pts,<br>
        outCtx.videoCodecCtx->time_base,<br>
        outCtx.videoStream->time_base);<br>
        }"<br>
      </blockquote>
      <div><br>
      </div>
      <div>In common way Stream time base and CodecContext time base is
        not equal, but regard comments in AVPacket structure pts field
        must be in Stream time base units and I rescale it before muxing</div>
      <div> </div>
      <blockquote class="gmail_quote" style="margin:0 0 0
        .8ex;border-left:1px #ccc solid;padding-left:1ex">
        <br>
        If I put it into my code almost all or no video frames are
        encoded succesfully and the output is:<br>
        [libx264 @ 0000000002620080] specified frame type (3) at 1314 is
        not compatible with keyframe interval<br>
        [mp4 @ 00000000034ff080] pts (0) < dts (1352000) in stream 0<br>
        Error while writing video frame<br>
        <br>
      </blockquote>
      <div><br>
      </div>
      <div>It is my error: we should rescale dts field also if it is not
        equal to AV_NOPTS.</div>
      <div> </div>
      <blockquote class="gmail_quote" style="margin:0 0 0
        .8ex;border-left:1px #ccc solid;padding-left:1ex">
        I looked into ffmpeg.c as well but I had no time to step threw
        it while transcoding, because I'm currently working on a windows
        machine.<br>
         Perhaps you can explane to me why we have to rescale the
        presentation time stamp...<br>
      </blockquote>
      <div><br>
      </div>
      <div>See comments above. For encoding we can set same time base to
        stream and codec context and remove pts/dts rescaling.</div>
      <div> </div>
      <blockquote class="gmail_quote" style="margin:0 0 0
        .8ex;border-left:1px #ccc solid;padding-left:1ex">
        Ah and you've rescaled the audio too. I did not rescale the
        audio but for every input codec I take, audio is always fine,
        even withouth:<br>
        <br>
        "if (frame->pts == AV_NOPTS_VALUE)<br>
        {<br>
                            AVRational samplesRateInv = {1,
        outCtx.audioCodecCtx->sample_rate};<br>
        int64_t pts = inputAudioSamples;<br>
                            frame->pts = av_rescale_q(pts,
        samplesRateInv, inCtx.audioCodecCtx->time_base);<br>
        }<br>
                        inputAudioSamples += frame->nb_samples;"<br>
        <br>
         Why is this?</blockquote>
      <div><br>
      </div>
      <div>This part of code is root couse of trouble with sound
        encoding and muxing. After deconding audio samples pts value
        always equal to AV_NOPTS_VALUE, so I tried to calculate real
        value using sample_rate value and decoded samples count. But I
        suppose that muxer don't want valid pts values for audio packets
        it want NOPTS value for correct muxing.</div>
      <div><br>
      </div>
      <div>Worked code: <a moz-do-not-send="true"
          href="http://pastebin.com/zYTiuRyA">http://pastebin.com/zYTiuRyA</a></div>
      <div><br>
      </div>
      <div>But I also have some amount of questions:</div>
      <div>1. How to correctly resample audio frames if input and output
        streams have different Sample Format, Sample Rate, Channels
        Count, Channels Layout or Frame Size? Example wanted...</div>
      <div>2. How to correctly synchronize audio and video? In some
        cases in transcoded file video and sound not synchronized and I
        don't understand - why?</div>
    </blockquote>
    Good job man. This works for perfectly for me. But I discovered the
    same when transcoding from some codec to another.<br>
    Now there is only one thing different in my code.<br>
    As in the demuxing.c example I'm using something like this after the
    trancoding loop to flush the remaining cached frames:<br>
    /* flush cached frames */<br>
        pkt.data = NULL;<br>
        pkt.size = 0;<br>
        do {<br>
            decode_packet(&got_frame, 1);<br>
        } while (got_frame);<br>
    <br>
    Perhaps this could help for your synchronization problem of cached
    frames?<br>
  </body>
</html>