Question

I'm streaming H.264(libx264) videos using RTP. I would like to compare the different error resiliency methods in h.264. In some research papers, psnr was used to compare them. So I would like to know how to calculate the psnr of the h.264 video during streaming.

Was it helpful?

Solution

To calculate PSNR, you must compare two frames, so first step is to make sure you have a copy of the source video. Next, you must be able to match the frames 1:1. so if a frame is droped, you need to compare the source frame to the previous streamed frame. This may be difficult if the timestamps do not match (They may be modified by the RTP server). Next decode each frame into its YUV channels. the PSNR of the channels needs to be calculated independently. You can average the three PSNR values at the end, but this puts too much weight on the U,V channels. I recommend just using the Y channel, as it is most important. And since you are measuring packet loss, the values will be strongly correlated anyway.

Next calculate your mean squared error like so:

int64_t mse = 0;
for(int x = 0 ; x < frame.width  ; ++x ) {
for(int y = 0 ; y < frame.height ; ++y ) {
    mse += pow( source_frame[x][y] - streamed_frame[x][y], 2 );
}}
mse *= 1.0/(frame.width*frame.height);

And finally:

/* use 219 instead of 255 if you know your colorspace is BT.709 */
double psnr = 20 * log10( 255 ) - 10 * log10( mse );

You can average the per frame PSNRs together to get a full stream PSNR.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top