Hello,

For experiment, i am reading a flv file, reading its packet and outputing it in
a new flv file. Basically copying a file but in a different manner. For video
stream, I read a full frame, decode it using avcodec_video_decoder. Then I again
encode it using avcodec_video_encode and then I try to write the packet. For the
first frame, it works fine, but for the rest of the frame, it throws me the
following error:

[flv @ 0x9932e50]Error, Invalid timestamp=0, last=0

On debugging further, I see that except for the first frame, when I decode the
frame, I get a response of -1.

  while(av_read_frame(pFormatCtx, &packet)>=0) {
    if(packet.stream_index==audioStream) {      
      frameFinished =  AVCODEC_MAX_AUDIO_FRAME_SIZE;
      avcodec_decode_audio2(pCodecCtx, samples, &frameFinished, 
                           packet.data, packet.size);

      // Did we get a video frame?
      if(frameFinished) {              
            AVPacket pkt;
            av_init_packet(&pkt);
            pkt.size= avcodec_encode_audio(audio_st->codec, audio_outbuf,
audio_outbuf_size, samples);
            if (audio_st->codec->coded_frame->pts != AV_NOPTS_VALUE)
                pkt.pts= av_rescale_q(audio_st->codec->coded_frame->pts,
audio_st->codec->time_base, audio_st->time_base);
            pkt.flags |= PKT_FLAG_KEY;
            pkt.stream_index= audio_st->index;
            pkt.data= audio_outbuf;
          
            if (av_write_frame(oc, &pkt) != 0) {
                fprintf(stderr, "Error while writing audio frame\n");
                exit(1);
            }

      }       
    }
    // Is this a packet from the video stream?
    if(packet.stream_index==videoStream) {
      // Decode video frame
      avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, 
                           packet.data, packet.size);

      // Did we get a video frame?
      if(frameFinished) {              
        int w = pCodecCtx->width;
        int h = pCodecCtx->height;              
        int numBytes=avpicture_get_size(video_st->codec->pix_fmt, w,h);
        uint8_t *buf=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
        int out_size = avcodec_encode_video(video_st->codec, buf, 
numBytes,pFrame);
        if (out_size > 0) {
            AVPacket pkt;
            av_init_packet(&pkt);
            if (video_st->codec->coded_frame->pts != AV_NOPTS_VALUE)
               pkt.pts= av_rescale_q(video_st->codec->coded_frame->pts,
video_st->codec->time_base, video_st->time_base);
            if(video_st->codec->coded_frame->key_frame)
               pkt.flags |= PKT_FLAG_KEY;
            pkt.stream_index= video_st->index;
            pkt.data= buf;
            pkt.size= numBytes;
           // Write it to the output file
           if (av_write_frame(oc, &pkt) != 0) {
              fprintf(stderr, "Error while writing vedio frame\n");
              exit(1);      
           }  
         }

      }
    }    

    // Free the packet that was allocated by av_read_frame
    av_free_packet(&packet);    
  }

I dont know whats going wrong. 


_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to