How to calculate PTS

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to calculate PTS

9,224 Views
remusmp
Contributor I

Dear NXP Community,

In my application frames per second varies somewhere between 15-25 fps, and in order to get the right playback of the video stream I think I need to calculate the presentation timestamp in my application code (the encoder is not doing it itself). In libimxvpuapi encoded_frame->pts just copies raw_frame->pts, so I guess the raw_frame->pts needs to be calculated by the application.

Looking around I found a formula for h264 streams (c++ - ffmpeg::avcodec_encode_video setting PTS h264 - Stack Overflow):

//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;

Has any of you used a similar formula or is there any official documentation for it?

Regards,

Remus.

Labels (3)
Tags (2)
0 Kudos
3 Replies

6,894 Views
remusmp
Contributor I

Here's my calculation of PTS, DTS and frame duration:

unsigned int currentFrameTimestamp = this->getCurrentTimestamp() - this->startTime; // [ms]
pAVPacket->pts = currentFrameTimestamp * 90; // Presentation timestamp. 90000 represents 1 second.
pAVPacket->dts = currentFrameTimestamp * 90; // Decoding timestamp. Same as presentation timestamp.
pAVPacket->duration = currentFrameTimestamp - this->previousFrameTimestamp; // Duration of the frame.
this->previousFrameTimestamp = currentFrameTimestamp; // Used to calculate the duration of the next frame.

It plays correctly in any player showing the right duration even if some frames are dropped from time to time. getCurrentTimestamp is system time in milliseconds.

I also figured out how to create the MP4 container along with the H.264 encoded video stream on-the-fly. This means no post-processing step is necessary for creating the container. If anyone's interested let me know and I'll post some code for it.

Cheers!

0 Kudos

6,894 Views
bensonhuang
Contributor III

Hi Remus,

I am interested with how you create the MP4 container along with the H.264 encoded video stream on-the-fly.

Could you please show me the code for it?

Appreciate it !

Best Regards

Benson

0 Kudos

6,894 Views
joanxie
NXP TechSupport
NXP TechSupport

maybe you can refer to the link as below:

https://community.nxp.com/message/442184 

0 Kudos