<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hi All,<br>
    <br>
    Here is my code for encoding Audio.  Can anyone help and point me in
    the direction I need to fix this issue?<br>
    <br>
        AVFrame* frame;<br>
        AVPacket pkt = { 0 };<br>
        AVCodecContext *c = m_audio_st.st->codec;<br>
        int got_packet;<br>
        int dst_nb_samples;<br>
        int bytesReady = audioBuffer.size();<br>
        QByteArray samples;<br>
        int sampleSize;<br>
    <br>
        if(m_errorCode != NoError){<br>
            return;<br>
        }<br>
    <br>
        frame = m_audio_st.tmp_frame;<br>
        if(m_audio_st.swr_ctx == NULL || c == NULL<br>
           || frame == NULL){<br>
            return;<br>
        }<br>
    <br>
        sampleSize = av_samples_get_buffer_size(NULL, c->channels,<br>
                                                c->frame_size,<br>
                                                c->sample_fmt, 0);<br>
    <br>
        do{<br>
            samples.clear();<br>
    <br>
            if(m_audioBuffer.size() + bytesReady == m_sampleSize){<br>
                samples.append(m_audioBuffer + audioBuffer);<br>
                m_audioBuffer.clear();<br>
            }<br>
            else if(m_audioBuffer.size() + bytesReady >
    m_sampleSize){<br>
                m_audioBuffer.append(audioBuffer);<br>
                samples.append(m_audioBuffer.left(m_sampleSize));<br>
                m_audioBuffer.remove(0, m_sampleSize);<br>
            }<br>
            else if(m_audioBuffer.size() + bytesReady <
    m_sampleSize){<br>
                m_audioBuffer.append(audioBuffer);<br>
                return;<br>
            }<br>
    <br>
            bytesReady = 0;<br>
            audioBuffer.clear();<br>
            int ret;<br>
            if((ret = avcodec_fill_audio_frame(frame, c->channels,<br>
                                     c->sample_fmt,<br>
                                     (uint8_t*) samples.data(),<br>
                                     sampleSize, 0)) < 0){<br>
                m_errorCode = AudioEncoderError;<br>
                Q_EMIT(m_errorCode);<br>
                continue;<br>
            }<br>
            frame->pts = m_audio_st.next_pts;<br>
            m_audio_st.next_pts += frame->nb_samples;<br>
            dst_nb_samples =
    av_rescale_rnd(swr_get_delay(m_audio_st.swr_ctx, c->sample_rate)
    + frame->nb_samples,<br>
                                            c->sample_rate,
    c->sample_rate, AV_ROUND_UP);<br>
            av_assert0(dst_nb_samples == frame->nb_samples);<br>
    <br>
            ret = av_frame_make_writable(m_audio_st.frame);<br>
            if(ret < 0){<br>
                m_errorCode = AudioEncoderError;<br>
                Q_EMIT(m_errorCode);<br>
                continue;<br>
            }<br>
    <br>
            ret = swr_convert(m_audio_st.swr_ctx,<br>
                              m_audio_st.frame->data, dst_nb_samples,<br>
                              (const uint8_t **)frame->data,
    frame->nb_samples);<br>
            if(ret < 0){<br>
                m_errorCode = AudioEncoderError;<br>
                Q_EMIT(m_errorCode);<br>
                return;<br>
            }<br>
    <br>
    <br>
            frame = m_audio_st.frame;<br>
            frame->pts = av_rescale_q(m_audio_st.samples_count, {1,
    c->sample_rate}, c->time_base);<br>
    <br>
            m_audio_st.samples_count += dst_nb_samples;<br>
            av_init_packet(&pkt);<br>
            ret = avcodec_encode_audio2(c, &pkt, frame,
    &got_packet);<br>
            if(ret < 0){<br>
                m_errorCode = AudioEncoderError;<br>
                Q_EMIT(m_errorCode);<br>
                continue;<br>
            }<br>
    <br>
            if(got_packet){<br>
                ret = write_frame(m_oc, &c->time_base,
    m_audio_st.st, &pkt);<br>
                if(ret < 0){<br>
                    m_errorCode = AudioEncoderError;<br>
                    Q_EMIT(m_errorCode);<br>
                    continue;<br>
                }<br>
            }<br>
        }while(m_audioBuffer.size() > m_sampleSize);<br>
    <br>
    <div class="moz-cite-prefix">
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <title></title>
      Sincerely,<span style="font-size:12.0pt;color:#0070C0"><br>
        Kevin J. Brooks<o:p></o:p></span><span
        style="font-size:12.0pt;color:#0070C0"></span><br>
      <br>
      On 9/22/2015 8:34 AM, Kevin J. Brooks wrote:<br>
    </div>
    <blockquote cite="mid:560158FE.8090502@r2c-ss.com" type="cite">
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      Hi All,<br>
      <br>
      Obviously I don't understand enough about encoding video with
      audio to figure out how to fix this.  If I take the captured sound
      from the microphone and pipe it directly back to my headphones, it
      is crystal clear.  Well maybe a little tinny sounding, but clear. 
      However, when I try to encode it into the video, it is not synced
      up with the images and there is a bunch of white noise.  It is
      only after the video has ended that you finally can barely hear my
      voice and whistles.<br>
      <br>
      Can anyone give me and idea where to start to fix this problem?<br>
      <div class="moz-signature">-- <br>
        <meta http-equiv="content-type" content="text/html;
          charset=utf-8">
        <title></title>
        Sincerely,<span style="font-size:12.0pt;color:#0070C0"><br>
          Kevin J. Brooks<o:p></o:p></span><span
          style="font-size:12.0pt;color:#0070C0"><br>
          Senior Software Engineer<o:p></o:p></span><span
          style="font-size:14.0pt;color:black;mso-themecolor: text1"><br>
        </span><br>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Libav-user mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a>
<a class="moz-txt-link-freetext" href="http://ffmpeg.org/mailman/listinfo/libav-user">http://ffmpeg.org/mailman/listinfo/libav-user</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>