Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg::avcodec_encode_video setting PTS h264

I'm trying to encode video as H264 using libavcodec

ffmpeg::avcodec_encode_video(codec,output,size,avframe);

returns an error that I don't have the avframe->pts value set correctly.
I have tried setting it to 0,1, AV_NOPTS_VALUE and 90khz * framenumber but still get the error non-strictly-monotonic PTS

The ffmpeg.c example sets the packet.pts with ffmpeg::av_rescale_q() but this is only called after you have encoded the frame !

When used with the MP4V codec the avcodec_encode_video() sets the pts value correctly itself.

like image 511
Martin Beckett Avatar asked Jul 06 '11 22:07

Martin Beckett


2 Answers

I had the same problem, solved it by calculating pts before calling avcodec_encode_video as follows:

//Calculate PTS: (1 / FPS) * sample rate * frame number
//sample rate 90KHz is for h.264 at 30 fps
picture->pts = (1.0 / 30) * 90 * frame_count;
out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);

Solution stolen from this helpful blog post

(Note: Changed sample rate to khz, expressed in hz was far too long between frames, may need to play with this value - not a video encoding expert here, just wanted something that worked and this did)

like image 58
Ozone Avatar answered Oct 19 '22 04:10

Ozone


I had this problem too. I sloved the problem in this way:

Before you invoke

ffmpeg::avcodec_encode_video(codec,output,size,avframe);

you set the pts value of avframe an integer value which has an initial value 0 and increments by one every time, just like this:

avframe->pts = nextPTS();

The implementation of nextPTS() is:

int nextPTS()
{
    static int static_pts = 0;
    return static_pts ++;
}

After giving the pts of avframe a value, then encoded it. If encoding successfully. Add the following code:

    if (packet.pts != AV_NOPTS_VALUE)
        packet.pts = av_rescale_q(packet.pts, mOutputCodecCtxPtr->time_base, mOutputStreamPtr->time_base);
    if (packet.dts != AV_NOPTS_VALUE)
         packet.dts = av_rescale_q(packet.dts, mOutputCodecCtxPtr->time_base, mOutputStreamPtr->time_base);

It'll add correct dts value for the encoded AVFrame. Among the code, packe of type AVPacket, mOutputCodeCtxPtr of type AVCodecContext* and mOutputStreamPtr of type AVStream.

avcodec_encode_video returns 0 indicates the current frame is buffered, you have to flush all buffered frames after all frames have been encoded. The code flushs all buffered frame somewhat like:

int ret;
while((ret = ffmpeg::avcodec_encode_video(codec,output,size,NULL)) >0)
    ;// place your code here.
like image 37
Alanmars Avatar answered Oct 19 '22 04:10

Alanmars