Hi,

I am using libavcodec and libavformat to transcode WMV video files to AVI
using MPEG2.  I can transcode the files fine when it comes to video only but
when I come to encode and multiplex the audio into the file I get the sound
playing around 500ms before the video when playing back the AVI.

A code snippet below shows what I am upto:

while(av_read_frame(pFormatCtx, &packet)>=0)
{
        // Is this a packet from the video stream or audio stream?
  if ( packet.stream_index == audioStream )
  {
   int data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;

   int len = avcodec_decode_audio2( aCodecCtx, (int16_t*)this->samples,
&data_size,
            packet.data, packet.size );
   if ( data_size > 0 )
   {
    writeAudioFrame();
   }
  }
        else if(packet.stream_index==videoStream)
        {
            // Decode video frame
            avcodec_decode_video( pCodecCtx, pFrame, &frameFinished,
         packet.data, packet.size);

   if(frameFinished)
   {
    static struct SwsContext * img_convert_ctx = NULL;

    if(img_convert_ctx == NULL)
    {
     int w = pCodecCtx->width;
     int h = pCodecCtx->height;
     img_convert_ctx = sws_getContext( w, h,
              STREAM_PIX_FMT /* source */,
              w, h, PIX_FMT_RGB32 /* dest */, SWS_BICUBIC,
              NULL, NULL, NULL);
     if(img_convert_ctx == NULL)
     {
      fprintf(stderr, "Cannot initialize the conversion context!\n");
     }
    }

    sws_scale( img_convert_ctx, pFrame->data,
       pFrame->linesize, 0,
       pCodecCtx->height,
       pFrameRGB->data, pFrameRGB->linesize);

     /* compute current audio and video time */
    if (audio_st)
     audio_pts = (double)audio_st->pts.val * audio_st->time_base.num /
audio_st->time_base.den;
    else
     audio_pts = 0.0;

    if (video_st)
     video_pts = (double)video_st->pts.val * video_st->time_base.num /
video_st->time_base.den;
    else
     video_pts = 0.0;

    if ((!audio_st /*|| audio_pts >= STREAM_DURATION*/) &&
              (!video_st /*|| video_pts >= STREAM_DURATION*/))
     break;

    /* write interleaved audio and video frames */
    if (!video_st || (video_st && audio_st && audio_pts < video_pts))
    {
     //writeAudioFrame();
    }
    else
    {
     if ( !writeVideoFrame( pFrameRGB ) )
     {
      printf( "Video frame not written" );
     }
    }
   }
  }




-- 
Andy Bell
CTO
J2K Video Limited

T: +44 (0)20 8133 2473
M: +34 685 130097
E: [email protected]
W: www.j2kvideo.com
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to