I'm using Visual Studio 2008 with the latest ffpmeg from svn built as shared 
libraries with msys.

My application is taking a .vob file as input, decoding the video and 
watermarking it and then encoding it again as a .vob file. The audio is stream 
copied.

The problem I'm having is that with Pal vobs, no output frames are written to 
disk until the entire file has been processed, resulting in the entire output 
stream being buffered in memory. With 1 GB vobs this is a major problem as the 
computers physical RAM is exceeded.  This problem only occurs with Pal vobs, it 
never happens with Ntsc ones.

The problem can be reproduced with any Pal .vob file.  I would be grateful for 
any advice as to what I might be doing wrong.  Parts of my code are included 
below:


      bool CVideoContext::Create(CVideoContext* sourceContext)
      {

            //return false;

            bool CreateSuccess = false;

            _formatCtx = (AVFormatContext*) av_alloc_format_context();
            _formatCtx->oformat = av_guess_format("dvd", NULL, NULL);

            if ( url_fopen( &_formatCtx->pb, _filenameA, URL_WRONLY ) >= 0 )
            {
                  AVFormatParameters FormatParameters = {0};

                  _formatCtx->mux_rate    = 10080000;
                  _formatCtx->packet_size = 2048;
                  //_formatCtx->max_delay = 
sourceContext->_formatCtx->max_delay;
                  _formatCtx->max_delay   = (int)(0.7*AV_TIME_BASE);
                  //_formatCtx->preload         = 
sourceContext->_formatCtx->preload;
                  _formatCtx->preload           = (int)(0.5*AV_TIME_BASE);

                  _formatCtx->start_time  = 
sourceContext->_formatCtx->start_time;
                  _formatCtx->duration    = sourceContext->_formatCtx->duration;
                  _formatCtx->bit_rate    = sourceContext->_formatCtx->bit_rate;
                  _formatCtx->flags       = sourceContext->_formatCtx->flags;
                  //_formatCtx->flags           = 
sourceContext->_formatCtx->flags | AVFMT_FLAG_NONBLOCK;

                  if ( sourceContext->NumVideoStreams > 0 )
                  {
                        CVideoStream* VideoStream = 
sourceContext->GetVideoStream(0);

                        if ( VideoStream != NULL )
                        {
                              FormatParameters.video_codec_id = 
VideoStream->_codecContext->codec_id;
                              FormatParameters.width = VideoStream->FrameWidth;
                              FormatParameters.height = 
VideoStream->FrameHeight;
                              FormatParameters.pix_fmt = 
VideoStream->_codecContext->pix_fmt;
                        }
                  }

                  if ( sourceContext->NumAudioStreams > 0 )
                  {
                        CAudioStream* AudioStream = 
sourceContext->GetAudioStream(0);
                        if ( AudioStream != NULL )
                        {
                              FormatParameters.audio_codec_id = 
AudioStream->_codecContext->codec_id;
                              FormatParameters.channels = AudioStream->Channels;
                              FormatParameters.sample_rate = 
AudioStream->SampleRate;
                        }
                  }


                  av_set_parameters(_formatCtx, &FormatParameters);

                  CreateSuccess = true;
            }


            return CreateSuccess;
      }


for( int i = 0; i < NumInputStreams; i++)
{

      InputStream = (AVInputStream*) av_mallocz(sizeof(AVInputStream));
      InputStream->st = _inputContext->_formatCtx->streams[i];
      InputStream->index = i;

      _inputStreamTable[i] = InputStream;
      OutputStream = (AVOutputStream*) av_mallocz(sizeof(AVOutputStream));

      OutputStream->st = av_new_stream(_outputContext->_formatCtx, 
InputStream->st->id);
      OutputStream->st->codec = avcodec_alloc_context();

      if (InputStream->st->codec->codec_type == CODEC_TYPE_VIDEO )
      {
            OutputStream->frame_max_number = 
_inputContext->_videoStreams[InputVideoStreamIndex]->Packets.size();
            InputVideoStreamIndex++;
      }

      //avcodec_thread_init( OutputStream->st->codec, 8 );



/*    if ( _inputContext->_formatCtx->flags & AVFMT_GLOBALHEADER )
            OutputStream->st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;*/

      //OutputStream->st->codec->codec_type = CODEC_TYPE_VIDEO;
      OutputStream->st->stream_copy = 1;


      if ( OutputStream->st->stream_copy == 1 )
      {
            //OutputStream->st->id                                      = 
InputStream->st->id;
            OutputStream->st->codec->codec_type                   = 
InputStream->st->codec->codec_type;
            OutputStream->st->codec->codec_id                     = 
InputStream->st->codec->codec_id;
            OutputStream->st->codec->codec_tag                    = 
InputStream->st->codec->codec_tag;
            OutputStream->st->codec->bit_rate                     = 
InputStream->st->codec->bit_rate;
            OutputStream->st->codec->bit_rate_tolerance           = 
InputStream->st->codec->bit_rate_tolerance;

            //OutputStream->st->avg_frame_rate.num                = 30000;
            //OutputStream->st->avg_frame_rate.den                = 1001;
            //OutputStream->st->r_frame_rate.num                        = 30000;
            //OutputStream->st->r_frame_rate.den                        = 1001;



            OutputStream->st->disposition                         = 
InputStream->st->disposition;
            OutputStream->st->codec->bits_per_raw_sample    = 
InputStream->st->codec->bits_per_raw_sample;
            OutputStream->st->codec->chroma_sample_location = 
InputStream->st->codec->chroma_sample_location;
            OutputStream->st->codec->extradata                    = 
InputStream->st->codec->extradata;
            OutputStream->st->codec->extradata_size               = 
InputStream->st->codec->extradata_size;


            if ( OutputStream->st->codec->codec_type == CODEC_TYPE_VIDEO )
            {
                  _videoDecodeBufferSize = 
avpicture_get_size(InputStream->st->codec->pix_fmt,
                        InputStream->st->codec->width, 
InputStream->st->codec->height);
                  _videoDecodeBuffer = (uint8_t*) 
av_mallocz(_videoDecodeBufferSize);

                  OutputStream->st->codec->codec_id                     = 
CODEC_ID_MPEG2VIDEO;

                  //OutputStream->st->codec->time_base.num              = 1001;
                  //OutputStream->st->codec->time_base.den              = 30000;
                  //OutputStream->st->codec->time_base.num              = 1;
                  //OutputStream->st->codec->time_base.den              = 25;
                  OutputStream->st->codec->time_base.num                = 
InputStream->st->codec->time_base.num;
                  OutputStream->st->codec->time_base.den                = 
InputStream->st->codec->time_base.den / 2;

                  OutputStream->st->codec->pix_fmt                      = 
InputStream->st->codec->pix_fmt;
                  OutputStream->st->codec->width                              = 
InputStream->st->codec->width;
                  OutputStream->st->codec->height                             = 
InputStream->st->codec->height;

                  //AVRational SAR = av_d2q( (4.0 / 3.0) / ( (double) 
OutputStream->st->codec->width / (double) OutputStream->st->codec->height    ), 
21);
                  //AVRational SAR = av_d2q( (16.0 / 9.0) * ( (double) 
OutputStream->st->codec->height / (double) OutputStream->st->codec->width    ), 
255);

            //double DAR = av_q2d(SAR) * OutputStream->st->codec->width / 
OutputStream->st->codec->height    ;


                  /*OutputStream->st->sample_aspect_ratio.num           = 
SAR.num;
                  OutputStream->st->sample_aspect_ratio.den       = SAR.den;
                  OutputStream->st->codec->sample_aspect_ratio.num = SAR.num;
                  OutputStream->st->codec->sample_aspect_ratio.den = SAR.den;*/
                  OutputStream->st->sample_aspect_ratio.num       = 
InputStream->st->codec->sample_aspect_ratio.num;
                  OutputStream->st->sample_aspect_ratio.den       = 
InputStream->st->codec->sample_aspect_ratio.den;
                  OutputStream->st->codec->sample_aspect_ratio.num = 
InputStream->st->codec->sample_aspect_ratio.num;
                  OutputStream->st->codec->sample_aspect_ratio.den = 
InputStream->st->codec->sample_aspect_ratio.den;

                  //OutputStream->st->codec->profile                    = 0;
                  //OutputStream->st->codec->level                            = 
5;
                  //OutputStream->st->codec->qmin                             = 
1;
                  //OutputStream->st->codec->qmax                             = 
1;
                  /*OutputStream->st->codec->profile                    = 
InputStream->st->codec->profile;
                  OutputStream->st->codec->level                              = 
InputStream->st->codec->level;
                  OutputStream->st->codec->qmin                         = 
InputStream->st->codec->qmin;
                  OutputStream->st->codec->qmax                         = 
InputStream->st->codec->qmax;*/



                  //OutputStream->st->codec->flags                            
|= CODEC_FLAG_INTERLACED_DCT;
                  //OutputStream->st->codec->flags                            
|= CODEC_FLAG_INTERLACED_ME;

                  //OutputStream->st->codec->has_b_frames               = 1;
                  OutputStream->st->codec->has_b_frames                 = 
InputStream->st->codec->has_b_frames;
                  //OutputStream->st->codec->gop_size                   = 15;
                  OutputStream->st->codec->gop_size                     = 
InputStream->st->codec->gop_size;
                  //OutputStream->st->codec->max_b_frames               = 3;
                  //InputStream->st->codec->max_b_frames                = 3;
                  OutputStream->st->codec->max_b_frames                 = 
InputStream->st->codec->max_b_frames;
                  //OutputStream->st->codec->flags2                           
|= CODEC_FLAG2_STRICT_GOP;
                  //OutputStream->st->codec->scenechange_threshold  = 
1000000000;
                  //OutputStream->st->codec->flags                            
|= CODEC_FLAG_CLOSED_GOP;

                  //OutputStream->st->codec->rc_max_rate                = 
8000000;
                  //OutputStream->st->codec->rc_min_rate                = 
8000000;
                  //OutputStream->st->codec->rc_max_rate                = 
9000000;
                  //OutputStream->st->codec->rc_min_rate                = 0;
                  OutputStream->st->codec->rc_max_rate                  = 
InputStream->st->codec->bit_rate;
                  OutputStream->st->codec->rc_min_rate                  = 
InputStream->st->codec->bit_rate;
                  OutputStream->st->codec->rc_buffer_size               = 
1851392; // was 1835008 (but new value is dvd compliant)
                  //OutputStream->st->codec->rc_buffer_size             = 
1835008; // was 1835008 (but new value is dvd compliant)

                  OutputStream->st->codec->rc_initial_buffer_occupancy = 
OutputStream->st->codec->rc_buffer_size * 3 / 4;
                  //OutputStream->st->codec->rc_initial_buffer_occupancy = 
OutputStream->st->codec->rc_buffer_size * 9 / 16;
            }
            else if ( OutputStream->st->codec->codec_type == CODEC_TYPE_AUDIO )
            {
                  //OutputStream->st->codec->codec_id                   = 
CODEC_ID_AC3;
                  OutputStream->st->codec->codec_id                     = 
InputStream->st->codec->codec_id;

                  OutputStream->st->codec->channel_layout               = 
InputStream->st->codec->channel_layout;
                  OutputStream->st->codec->channels                     = 
InputStream->st->codec->channels;
                  OutputStream->st->codec->sample_fmt                   = 
InputStream->st->codec->sample_fmt;
                  OutputStream->st->codec->sample_rate                  = 
InputStream->st->codec->sample_rate;

                  OutputStream->st->codec->frame_size                   = 
InputStream->st->codec->frame_size;
                  OutputStream->st->codec->block_align                  = 
InputStream->st->codec->block_align;
            }

            if (_outputContext->_formatCtx->oformat->flags & AVFMT_GLOBALHEADER)
            {
                  OutputStream->st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
            }

            AVCodec* StreamCodec = avcodec_find_encoder( 
OutputStream->st->codec->codec_id );

            if ( StreamCodec != NULL )
            {
                  int CodecOpen = avcodec_open( OutputStream->st->codec, 
StreamCodec );
                  if ( CodecOpen < 0 ) {
                        PrepareSuccess = false;
                        switch (CodecOpen) {
                              case AVERROR(EINVAL) : fprintf(_fpLog, 
"av_read_frame returned AVERROR(EINVAL)\n"); break;
                              case AVERROR(EIO) : fprintf(_fpLog, 
"av_read_frame returned AVERROR(EIO)\n"); break;
                              case AVERROR(ENOENT) : fprintf(_fpLog, 
"av_read_frame returned AVERROR(ENOENT)\n"); break;
                              case AVERROR(EILSEQ) : fprintf(_fpLog, 
"av_read_frame returned AVERROR(EILSEQ)\n"); break;
                              case AVERROR(ENOMEM) : fprintf(_fpLog, 
"av_read_frame returned AVERROR(ENOMEM)\n"); break;
                              case AVERROR(ENOSYS) : fprintf(_fpLog, 
"av_read_frame returned AVERROR(ENOSYS)\n"); break;
                              case AVERROR(EDOM) : fprintf(_fpLog, 
"av_read_frame returned AVERROR(EDOM)\n"); break;
                              case AVERROR(EPIPE) : fprintf(_fpLog, 
"av_read_frame returned AVERROR(EPIPE) (end of file)\n"); break;
                              case AVERROR(EAGAIN) : fprintf(_fpLog, 
"av_read_frame returned AVERROR(EAGAIN)\n"); break;
                              case AVERROR_PATCHWELCOME : fprintf(_fpLog, 
"av_read_frame returned AVERROR_PATCHWELCOME\n"); break;
                        }
                  }
                  else
                  {
                        OutputStream->st->codec->min_prediction_order = 0;
                        OutputStream->st->codec->max_prediction_order = 0;
                        OutputStream->st->codec->prediction_order_method = 0;
                        OutputStream->st->codec->min_partition_order = 0;
                        OutputStream->st->codec->max_partition_order = 0;
                        OutputStream->st->codec->drc_scale = 0;
                        OutputStream->st->codec->reordered_opaque = 0;

                  }
            }
      }

      if ( OutputStream != NULL )
            _outputStreamTable[i] = OutputStream;
}





      bool CVideoTranscodeContext::_DoTranscode()
      {
            bool Transcoding = true;
            bool error = false;
            _cancel = false;

            AVFormatContext* InputFormatContext = _inputContext->_formatCtx;
            AVFormatContext* OutputFormatContext = _outputContext->_formatCtx;
            AVInputStream* InputStream = NULL;
            AVOutputStream* OutputStream = NULL;

            int64_t InputFileTsOffset = 0;
            AVRational AVTimeBaseQ;
            AVTimeBaseQ.num = 1;
            AVTimeBaseQ.den = VIDEO_TIME_BASE;

            int AvReadFrameRet = 0;


            InputFileTsOffset = - _inputContext->_formatCtx->start_time;


            // Initialize Presentation time stamps
            for ( int i = 0; i < _numInputStreams; i++ )
            {
                  AVInputStream* InputStream = _inputStreamTable[i];
                  if ( InputStream != NULL )
                  {
                        AVStream* Stream = InputStream->st;
                        InputStream->pts = 0;
                        InputStream->next_pts = VIDEO_NOPTS_VALUE;
                        InputStream->is_start = 1;
                  }
            }

            // Write the header for the output file
            av_write_header(OutputFormatContext);

            //int ct[2] = {0, 0};
            int ct = 0;

            // Do the actual transcoding
            while ( Transcoding && ! error && ! _IsBuildCancelledCallback() )
            {
                  AVPacket InputPacket = {0};

                  AvReadFrameRet = av_read_frame(InputFormatContext, 
&InputPacket);

                  switch (AvReadFrameRet) {
                        case AVERROR(EINVAL) : fprintf(_fpLog, "av_read_frame 
returned AVERROR(EINVAL)\n"); break;
                        case AVERROR(EIO) : fprintf(_fpLog, "av_read_frame 
returned AVERROR(EIO)\n"); break;
                        case AVERROR(ENOENT) : fprintf(_fpLog, "av_read_frame 
returned AVERROR(ENOENT)\n"); break;
                        case AVERROR(EILSEQ) : fprintf(_fpLog, "av_read_frame 
returned AVERROR(EILSEQ)\n"); break;
                        case AVERROR(ENOMEM) : fprintf(_fpLog, "av_read_frame 
returned AVERROR(ENOMEM)\n"); break;
                        case AVERROR(ENOSYS) : fprintf(_fpLog, "av_read_frame 
returned AVERROR(ENOSYS)\n"); break;
                        case AVERROR(EDOM) : fprintf(_fpLog, "av_read_frame 
returned AVERROR(EDOM)\n"); break;
                        case AVERROR(EPIPE) : fprintf(_fpLog, "av_read_frame 
returned AVERROR(EPIPE) (end of file)\n"); break;
                        case AVERROR(EAGAIN) : fprintf(_fpLog, "av_read_frame 
returned AVERROR(EAGAIN)\n"); break;
                        case AVERROR_PATCHWELCOME : fprintf(_fpLog, 
"av_read_frame returned AVERROR_PATCHWELCOME\n"); break;
                  }


                  if ( AvReadFrameRet >= 0)
                  {
                        if (InputPacket.stream_index < _numInputStreams )     
// Ignore any dynamic streams that appear
                        {
                              InputStream  = 
_inputStreamTable[InputPacket.stream_index];
                              OutputStream = 
_outputStreamTable[InputPacket.stream_index];

                              if ( InputPacket.dts != VIDEO_NOPTS_VALUE )
                                    InputPacket.dts += 
av_rescale_q(InputFileTsOffset, AVTimeBaseQ, InputStream->st->time_base);

                              if ( InputPacket.pts != VIDEO_NOPTS_VALUE )
                                    InputPacket.pts += 
av_rescale_q(InputFileTsOffset, AVTimeBaseQ, InputStream->st->time_base);

                              if ( _DoTranscodeOutputPacket(InputStream, 
OutputStream, &InputPacket) == false )
                              {
                                    error = true;
                                    fprintf(_fpLog, "Error in_DoTranscode, or 
end of file, AvReadFrameRet == %d\n", AvReadFrameRet);
                              }

                              if (ct % 200 == 0)
                              {
                                    
//_DoTranscodeOutputVideo(_inputStreamTable[InputPacket.stream_index],
                                    //    
_outputStreamTable[InputPacket.stream_index], NULL);
                                    fprintf(_fpLog, "Packet number is %d\n", 
ct);
                                    
//avcodec_flush_buffers(InputStream->st->codec);
                              }
                        }
                  }
                  else {
                        error = true;
                        fprintf(_fpLog, "Error in_DoTranscode, or end of file, 
AvReadFrameRet == %d\n", AvReadFrameRet);
                  }

                  av_free_packet(&InputPacket);

                  ct++;
            };

            //if (! error)
            //{
                  // Flush the decode buffers (at the end of stream)
                  for ( int i = 0; i < _numInputStreams; i++ )
                  {
                        InputStream  = _inputStreamTable[i];
                        OutputStream = _outputStreamTable[i];

                        _DoTranscodeOutputPacket(InputStream, OutputStream, 
NULL);
                  }

                  // Write the trailer for the output file
                  av_write_trailer(OutputFormatContext);
            //}


            return false;
      }




      bool CVideoTranscodeContext::_DoTranscodeOutputPacket(AVInputStream* 
inputStream, AVOutputStream* outputStream, AVPacket* inputPacket)
      {
            bool AvFailDecode = false;
            int AvVideoDecodeRet = 0;
            int AvVideoDecodeGotPicture = 0;
            bool IsEmptyPacket = false;
            AVFrame VideoDecodedPicture;
            int ret = 0;
            bool freePacket = false;
            static int highPts = 0;
            static bool wait = false;
            static int lastFrameFlush = 0;

            AVRational AVTimeBaseQ;
            AVTimeBaseQ.num = 1;
            AVTimeBaseQ.den = VIDEO_TIME_BASE;

            if ( inputStream->next_pts == VIDEO_NOPTS_VALUE )
                  inputStream->next_pts = inputStream->pts;

            if ( inputPacket == NULL )
            {
                  IsEmptyPacket = true;
                  inputPacket = (AVPacket*) av_mallocz(sizeof(AVPacket));
                  inputPacket->pts = VIDEO_NOPTS_VALUE;
                  inputPacket->dts = VIDEO_NOPTS_VALUE;
                  inputPacket->data = NULL;
                  inputPacket->size = 0;
                  freePacket = true;
            }
            else
            {
                  if ( inputPacket->dts != VIDEO_NOPTS_VALUE )
                        inputStream->next_pts = inputStream->pts = 
av_rescale_q(inputPacket->dts, inputStream->st->time_base, AVTimeBaseQ );
            }

            if ( inputPacket != NULL )
            {
                  while ( ( (inputPacket->size > 0) || ( IsEmptyPacket == true 
) && (! AvFailDecode) ) )
                  {
                        inputStream->pts = inputStream->next_pts;

                        if ( inputStream->st->codec->codec_type == 
CODEC_TYPE_VIDEO )
                        {
                              int progress = floor(100.0 / 
outputStream->frame_max_number * outputStream->frame_number);
                              _ProgressUpdateCallback(progress);


                              /*if (outputStream->frame_number < 
outputStream->frame_max_number)
                              {
                                    int64_t pts = 
_inputContext->_videoStreams[0]->Packets[outputStream->frame_number]->Pts;
                                    //int64_t pts = inputPacket->pts;
                                    if (pts <= highPts)
                                          wait = true;
                                    if (pts > highPts)
                                    {
                                          highPts = pts;
                                          if (wait)
                                          {
                                                fprintf(_fpLog, "Writing NULL 
frame.\n");
                                                
_DoTranscodeOutputVideo(inputStream, outputStream, NULL);
                                                fprintf(_fpLog, "NULL frame 
written.\n");
                                                wait = false;
                                          }
                                    }
                              }*/


                              avcodec_get_frame_defaults((AVFrame*) 
&VideoDecodedPicture);

                              AvVideoDecodeRet = 
avcodec_decode_video2(inputStream->st->codec,
                                    &VideoDecodedPicture, 
&AvVideoDecodeGotPicture, (AVPacket*) inputPacket );

                              inputStream->st->quality = 
VideoDecodedPicture.quality;

                              /*if ( _fpLog != NULL )
                              {
                                    fprintf(_fpLog, "dec, %d, %d, %d, %I64d, 
%I64d,\n",
                                          AvVideoDecodeRet, 
AvVideoDecodeGotPicture, inputPacket->size, inputPacket->pts, inputPacket->dts);
                              }*/

                              if ( AvVideoDecodeRet >= 0 )
                              {

                                    if ( AvVideoDecodeGotPicture > 0 )
                                    {
                                          //if (inputPacket->flags & 
PKT_FLAG_KEY)
                                          //    
_DoTranscodeOutputVideo(inputStream, outputStream, NULL);

                                          int ticks = inputStream->st->parser ? 
inputStream->st->parser->repeat_pict+1 : 
inputStream->st->codec->ticks_per_frame;
                                          inputStream->next_pts += 
((int64_t)VIDEO_TIME_BASE * inputStream->st->codec->time_base.num * ticks) / 
inputStream->st->codec->time_base.den;

                                          if 
(_DoTranscodeOutputVideo(inputStream, outputStream, &VideoDecodedPicture) == 
false )
                                          {
                                                AvFailDecode = true;
                                                fprintf(_fpLog, "Error in 
_DoTranscodeOutputPacket, _DoTranscodeOutputVideo failed. 
AvVideoDecodeGotPicture > 0\n");
                                                break;
                                          }
                                    }
                                    else
                                    {
                                          if ( IsEmptyPacket == true )
                                          {
                                                while 
(outputStream->frame_number < (outputStream->frame_max_number+3) )
                                                {
                                                      if ( 
_DoTranscodeOutputVideo(inputStream, outputStream, NULL) == false )
                                                      {
                                                            fprintf(_fpLog, 
"Error in _DoTranscodeOutputPacket, _DoTranscodeOutputVideo failed. 
IsEmptyPacket == true.\n");
                                                            AvFailDecode = true;
                                                            break;
                                                      }
                                                }

                                                fprintf(_fpLog, "Error in 
_DoTranscodeOutputPacket, _DoTranscodeOutputVideo failed. IsEmptyPacket == 
true.\n");
                                                AvFailDecode = true;
                                          }
                                    }

                                    inputPacket->size = 0;
                              }
                              else {
                                    fprintf(_fpLog, "Error in 
_DoTranscodeOutputPacket, AvVideoDecodeRet < 0\n");
                                    AvFailDecode = true;
                              }
                        }
                        else if ( inputStream->st->codec->codec_type == 
CODEC_TYPE_AUDIO && _inputContext->NumAudioStreams > 0 )
                        {
                              inputPacket->pts = 0;
                              inputPacket->dts = 0;
                              //inputPacket->flags |= PKT_FLAG_KEY;

                              if ( outputStream->frame_number < 
_inputContext->_audioStreams[0]->Packets.size() )
                              {
                                    inputPacket->pts =
                                          
_inputContext->_audioStreams[0]->Packets[outputStream->frame_number]->Pts;

                                    inputPacket->dts =
                                          
_inputContext->_audioStreams[0]->Packets[outputStream->frame_number]->Dts;
                                    //fprintf(_fpLog, "inputPacket->pts = %d, 
inputPacket->dts = %d\n", inputPacket->pts, inputPacket->dts);
                              }

                              ret = av_interleaved_write_frame( 
_outputContext->_formatCtx, inputPacket);
                              if (ret == 1)
                              {
                                    fprintf(_fpLog, "audio 
av_interleaved_write_frame returned %d\n", ret);
                              }
                              if (ret < 0)
                              {
                                    switch (ret) {
                                          case AVERROR(EINVAL) : 
fprintf(_fpLog, "av_interleaved_write_frame returned AVERROR(EINVAL)\n"); break;
                                          case AVERROR(EIO) : fprintf(_fpLog, 
"av_interleaved_write_frame returned AVERROR(EIO)\n"); break;
                                          case AVERROR(ENOENT) : 
fprintf(_fpLog, "av_interleaved_write_frame returned AVERROR(ENOENT)\n"); break;
                                          case AVERROR(EILSEQ) : 
fprintf(_fpLog, "av_interleaved_write_frame returned AVERROR(EILSEQ)\n"); break;
                                          case AVERROR(ENOMEM) : 
fprintf(_fpLog, "av_interleaved_write_frame returned AVERROR(ENOMEM)\n"); break;
                                          case AVERROR(ENOSYS) : 
fprintf(_fpLog, "av_interleaved_write_frame returned AVERROR(ENOSYS)\n"); break;
                                          case AVERROR(EDOM) : fprintf(_fpLog, 
"av_interleaved_write_frame returned AVERROR(EDOM)\n"); break;
                                          case AVERROR(EPIPE) : fprintf(_fpLog, 
"av_interleaved_write_frame returned AVERROR(EPIPE) (end of file)\n"); break;
                                          case AVERROR(EAGAIN) : 
fprintf(_fpLog, "av_interleaved_write_frame returned AVERROR(EAGAIN)\n"); break;
                                          case AVERROR_PATCHWELCOME : 
fprintf(_fpLog, "av_interleaved_write_frame returned AVERROR_PATCHWELCOME\n"); 
break;
                                          default: fprintf(_fpLog, "audio 
av_interleaved_write_frame returned %d\n", ret);
                                                break;
                                    }
                                    AvFailDecode = true;
                              }

                              inputPacket->size = 0;
                              outputStream->frame_number++;

                              if (outputStream->frame_number > lastFrameFlush + 
100)
                              {
                                    lastFrameFlush = outputStream->frame_number;
                                    //_DoTranscodeOutputPacket(inputStream, 
outputStream, NULL);
                                    //_DoTranscodeOutputVideo(inputStream, 
outputStream, NULL);
                              }

                              if ( IsEmptyPacket ) {
                                    AvFailDecode = true;
                                    fprintf(_fpLog, "Error in 
_DoTranscodeOutputPacket, codec_type is audio, IsEmptyPacket == true.\n");
                              }
                        }
                        else
                        {
                              char *msg;
                              switch (inputStream->st->codec->codec_type) {
                                    case CODEC_TYPE_UNKNOWN : msg = 
"_DoTranscodeOutputPacket, codec_type is CODEC_TYPE_UNKNOWN\n"; break;
                                    case CODEC_TYPE_DATA : msg = 
"_DoTranscodeOutputPacket, codec_type is CODEC_TYPE_DATA\n"; break;
                                    case CODEC_TYPE_SUBTITLE : msg = 
"_DoTranscodeOutputPacket, codec_type is CODEC_TYPE_SUBTITLE\n"; break;
                                    case CODEC_TYPE_ATTACHMENT : msg = 
"_DoTranscodeOutputPacket, codec_type is CODEC_TYPE_ATTACHMENT\n"; break;
                                    case CODEC_TYPE_NB : msg = 
"_DoTranscodeOutputPacket, codec_type is CODEC_TYPE_NB\n"; break;
                                    default : msg = "_DoTranscodeOutputPacket, 
codec_type is not recognized\n"; break;
                              }
                              fprintf(_fpLog, msg);
                              //AvFailDecode = true;
                              inputPacket->size = 0;

                              if ( IsEmptyPacket ) {
                                    AvFailDecode = true;
                                    fprintf(_fpLog, "Error in 
_DoTranscodeOutputPacket, codec_type is other, IsEmptyPacket == true.\n");
                              }
                        }

                        if ( AvFailDecode == true )
                              break;
                  }
            }
            else
            {
                  AvFailDecode = true;
                  fprintf(_fpLog, "Error in _DoTranscodeOutputPacket, 
inputPacket is null.\n");
            }

            if (freePacket)
                  av_free_packet(inputPacket);

            //if ( inputPacket != NULL && inputPacket->destruct)
            //    inputPacket->destruct(inputPacket);

            if ( AvFailDecode == true )
                  return false;

            return true;
      }





      bool CVideoTranscodeContext::_DoTranscodeOutputVideo(AVInputStream* 
inputStream, AVOutputStream* outputStream, AVFrame* decodedPicture)
      {
            static int framect = 0;
            int NumFrames = 1;
            int AvVideoEncodeRet = 0;
            bool ignoreErrors = false;
            int lastPts = 0;
            int highPts = 0;
            CVideoStream* videostream = _inputContext->GetVideoStream(0);

            AVCodecContext* Decoder = inputStream->st->codec;
            AVCodecContext* Encoder = outputStream->st->codec;

            bool FrameSuccess = false;

            for ( int i = 0; i < NumFrames; i++ )
            {
                  AVPacket VideoPacket;
                  av_init_packet(&VideoPacket);
                  VideoPacket.stream_index = outputStream->index;
                  bool overlayRequired = false;

                  if ( decodedPicture != NULL )
                  {
                        decodedPicture->quality = inputStream->st->quality;
                        decodedPicture->pts = outputStream->sync_opts;
                        decodedPicture->pict_type = 0;
                  }

                  AvVideoEncodeRet = 
avcodec_encode_video(outputStream->st->codec, _videoDecodeBuffer, 
_videoDecodeBufferSize, (const AVFrame*) decodedPicture);

                  if ( AvVideoEncodeRet < 0 && ! ignoreErrors) {
                        fprintf(_fpLog, "AvVideoEncodeRet returned %d\n", 
AvVideoEncodeRet);
                        return false;
                  }
                  else
                  {
                        VideoPacket.data = _videoDecodeBuffer;
                        VideoPacket.size = AvVideoEncodeRet;

                        if ( Encoder->coded_frame->pts != VIDEO_NOPTS_VALUE )
                              VideoPacket.pts = av_rescale_q(     
Encoder->coded_frame->pts, Encoder->time_base, outputStream->st->time_base);

                        if ( Encoder->coded_frame->key_frame )
                              VideoPacket.flags |= PKT_FLAG_KEY;

                        int ret = 
av_interleaved_write_frame(_outputContext->_formatCtx, &VideoPacket);
                        if (ret == 1 && ! ignoreErrors)
                        {
                              fprintf(_fpLog, "video av_interleaved_write_frame 
returned %d\n", ret);
                              return false;
                        }
                        if (ret < 0 && ! ignoreErrors)
                        {
                              fprintf(_fpLog, "video av_interleaved_write_frame 
returned %d\n", ret);
                              return false;
                        }
                        outputStream->frame_number++;

                        FrameSuccess = true;
                  }

                  outputStream->sync_opts++;

            }


            return FrameSuccess;
      }


Thanks,

John Mapley.


The contents of this e-mail (including any attachments) are confidential and 
may be legally privileged. If you are not the intended recipient of this 
e-mail, any disclosure, copying, distribution or use of its contents is 
strictly prohibited, and you should please notify the sender immediately and 
then delete it (including any attachments) from your system.


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to