ffmpeg::avcodec_encode_video setting PTS h264
Asked Answered
C

4

13

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.

Creech answered 6/7, 2011 at 22:49 Comment(1)
With a newer version off ffmpeg simple setting " ppicture->pts=pCodecCtx->frame_number;" worksCreech
W
8

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)

Withy answered 24/2, 2012 at 0:25 Comment(0)
T
3

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.
Tillion answered 9/10, 2012 at 16:56 Comment(2)
What about when your nextPTS function overflows (i.e. static_pts > INT_MAX) ?Agamemnon
If static_pts is in danger of overflowing, just changing its type from int to int64_t.Tillion
L
0

I had this problem too. As far as I remember, the error is related to dts

setting

out_video_packet.dts = AV_NOPTS_VALUE;

helped me

Leopold answered 10/7, 2011 at 22:42 Comment(0)
P
0

A strictly increase monotonic function is a function where f(x) < f(y) if x < y. So it means you cannot encode 2 frames with the same PTS as you were doing... check for example with a counter and it should not return error anymore.

Parody answered 12/2, 2012 at 22:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.