1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "media/cast/test/video_utility.h"
12 double I420PSNR(const I420VideoFrame
& frame1
, const I420VideoFrame
& frame2
) {
13 // Frames should have equal resolution.
14 if (frame1
.width
!= frame2
.width
|| frame1
.height
!= frame2
.height
) return -1;
18 uint8
* data1
= frame1
.y_plane
.data
;
19 uint8
* data2
= frame2
.y_plane
.data
;
20 for (int i
= 0; i
< frame1
.height
; ++i
) {
21 for (int j
= 0; j
< frame1
.width
; ++j
) {
22 y_mse
+= (data1
[j
] - data2
[j
]) * (data1
[j
] - data2
[j
]);
24 // Account for stride.
25 data1
+= frame1
.y_plane
.stride
;
26 data2
+= frame2
.y_plane
.stride
;
28 y_mse
/= (frame1
.width
* frame1
.height
);
30 int half_width
= (frame1
.width
+ 1) / 2;
31 int half_height
= (frame1
.height
+ 1) / 2;
34 data1
= frame1
.u_plane
.data
;
35 data2
= frame2
.u_plane
.data
;
36 for (int i
= 0; i
< half_height
; ++i
) {
37 for (int j
= 0; j
< half_width
; ++j
) {
38 u_mse
+= (data1
[j
] - data2
[j
]) * (data1
[j
] - data2
[j
]);
40 // Account for stride.
41 data1
+= frame1
.u_plane
.stride
;
42 data2
+= frame2
.u_plane
.stride
;
44 u_mse
/= half_width
* half_height
;
48 data1
= frame1
.v_plane
.data
;
49 data2
= frame2
.v_plane
.data
;
50 for (int i
= 0; i
< half_height
; ++i
) {
51 for (int j
= 0; j
< half_width
; ++j
) {
52 v_mse
+= (data1
[j
] - data2
[j
]) * (data1
[j
] - data2
[j
]);
54 // Account for stride.
55 data1
+= frame1
.v_plane
.stride
;
56 data2
+= frame2
.v_plane
.stride
;
58 v_mse
/= half_width
* half_height
;
60 // Combine to one psnr value.
61 static const double kVideoBitRange
= 255.0;
62 return 20.0 * log10(kVideoBitRange
) -
63 10.0 * log10((y_mse
+ u_mse
+ v_mse
) / 3.0);
66 void PopulateVideoFrame(I420VideoFrame
* frame
, int start_value
) {
67 int half_width
= (frame
->width
+ 1) / 2;
68 int half_height
= (frame
->height
+ 1) / 2;
69 frame
->y_plane
.stride
= frame
->width
;
70 frame
->y_plane
.length
= frame
->width
* frame
->height
;
71 frame
->y_plane
.data
= new uint8
[frame
->y_plane
.length
];
73 frame
->u_plane
.stride
= half_width
;
74 frame
->u_plane
.length
= half_width
* half_height
;
75 frame
->u_plane
.data
= new uint8
[frame
->u_plane
.length
];
77 frame
->v_plane
.stride
= half_width
;
78 frame
->v_plane
.length
= half_width
* half_height
;
79 frame
->v_plane
.data
= new uint8
[frame
->v_plane
.length
];
82 for (int i
= 0; i
< frame
->y_plane
.length
; ++i
) {
83 frame
->y_plane
.data
[i
] = static_cast<uint8
>(start_value
+ i
);
87 for (int i
= 0; i
< frame
->u_plane
.length
; ++i
) {
88 frame
->u_plane
.data
[i
] = static_cast<uint8
>(start_value
+ i
);
92 for (int i
= 0; i
< frame
->v_plane
.length
; ++i
) {
93 frame
->v_plane
.data
[i
] = static_cast<uint8
>(start_value
+ i
);