1 /******************************************************************************
2 *
3 * Copyright (C) 2015 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20
21 /**
22 *******************************************************************************
23 * @file
24 * psnr.c
25 *
26 * @brief
27 * Contains functions necessary for computing psnr
28 *
29 * @author
30 * ittiam
31 *
32 * @remarks
33 * none
34 *
35 *******************************************************************************
36 */
37
38 /*****************************************************************************/
39 /* File Includes */
40 /*****************************************************************************/
41 /* System include files */
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <math.h>
46
47 /* User include files */
48 #include "ih264_typedefs.h"
49 #include "iv2.h"
50 #include "ive2.h"
51 #include "ih264e.h"
52 #include "app.h"
53 #include "psnr.h"
54
55
56 /*****************************************************************************/
57 /* Function Definition */
58 /*****************************************************************************/
59
60 /**
61 **************************************************************************
62 * @brief Initialize PSNR for the Y, U, V component
63 **************************************************************************
64 */
init_psnr(app_ctxt_t * ps_app_ctxt)65 void init_psnr(app_ctxt_t *ps_app_ctxt)
66 {
67 ps_app_ctxt->adbl_psnr[0] = 0;
68 ps_app_ctxt->adbl_psnr[1] = 0;
69 ps_app_ctxt->adbl_psnr[2] = 0;
70 ps_app_ctxt->u4_psnr_cnt = 0;
71 }
72
73 /**
74 **************************************************************************
75 * @brief Computes PSNR for the Y, U, V component
76 **************************************************************************
77 */
compute_psnr(app_ctxt_t * ps_app_ctxt,iv_raw_buf_t * ps_buf1,iv_raw_buf_t * ps_buf2)78 void compute_psnr(app_ctxt_t *ps_app_ctxt,
79 iv_raw_buf_t *ps_buf1,
80 iv_raw_buf_t *ps_buf2)
81 {
82 WORD32 i, j;
83 WORD32 comp;
84 DOUBLE df_psnr[3];
85 WORD32 wd, ht, strd1, strd2;
86 UWORD8 *pu1_buf1, *pu1_buf2;
87 WORD32 incr1, incr2;
88
89 printf("\nPicNum %4d\t ", ps_app_ctxt->u4_psnr_cnt);
90
91 for(comp = 0; comp < 3; comp++)
92 {
93 df_psnr[comp] = 0;
94 pu1_buf1 = (UWORD8 *)ps_buf1->apv_bufs[comp];
95 pu1_buf2 = (UWORD8 *)ps_buf2->apv_bufs[comp];
96 wd = ps_buf1->au4_wd[comp];
97 ht = ps_buf1->au4_ht[comp];
98 strd1 = ps_buf1->au4_strd[comp] - ps_buf1->au4_wd[comp];
99 strd2 = ps_buf2->au4_strd[comp] - ps_buf2->au4_wd[comp];
100 incr1 = 1;
101 incr2 = 1;
102
103 if((IV_YUV_420SP_UV == ps_buf1->e_color_fmt)
104 || (IV_YUV_420SP_VU == ps_buf1->e_color_fmt))
105 {
106 switch(comp)
107 {
108 case 0:
109 pu1_buf1 = ps_buf1->apv_bufs[0];
110 break;
111 case 1:
112 if(IV_YUV_420SP_UV == ps_buf1->e_color_fmt)
113 pu1_buf1 = (UWORD8 *)ps_buf1->apv_bufs[1];
114 else
115 pu1_buf1 = (UWORD8 *)ps_buf1->apv_bufs[1] + 1;
116 incr1 = 2;
117 wd = ps_buf1->au4_wd[0] >> 1;
118 ht = ps_buf1->au4_ht[0] >> 1;
119 break;
120 case 2:
121 if(IV_YUV_420SP_UV == ps_buf1->e_color_fmt)
122 pu1_buf1 = (UWORD8 *)ps_buf1->apv_bufs[1] + 1;
123 else
124 pu1_buf1 = ps_buf1->apv_bufs[1];
125 incr1 = 2;
126 wd = ps_buf1->au4_wd[0] >> 1;
127 ht = ps_buf1->au4_ht[0] >> 1;
128 strd1 = ps_buf1->au4_strd[1] - ps_buf1->au4_wd[1];
129 break;
130 }
131 }
132 if((IV_YUV_420SP_UV == ps_buf2->e_color_fmt)
133 || (IV_YUV_420SP_VU == ps_buf2->e_color_fmt))
134 {
135 switch(comp)
136 {
137 case 0:
138 pu1_buf2 = ps_buf2->apv_bufs[0];
139 break;
140 case 1:
141 if(IV_YUV_420SP_UV == ps_buf2->e_color_fmt)
142 pu1_buf2 = ps_buf2->apv_bufs[1];
143 else
144 pu1_buf2 = (UWORD8 *)ps_buf2->apv_bufs[1] + 1;
145 incr2 = 2;
146 wd = ps_buf1->au4_wd[0] >> 1;
147 ht = ps_buf1->au4_ht[0] >> 1;
148
149 break;
150 case 2:
151 if(IV_YUV_420SP_UV == ps_buf2->e_color_fmt)
152 pu1_buf2 = (UWORD8 *)ps_buf2->apv_bufs[1] + 1;
153 else
154 pu1_buf2 = ps_buf2->apv_bufs[1];
155 incr2 = 2;
156 wd = ps_buf1->au4_wd[0] >> 1;
157 ht = ps_buf1->au4_ht[0] >> 1;
158 strd2 = ps_buf2->au4_strd[1] - ps_buf2->au4_wd[1];
159
160 break;
161 }
162 }
163
164 for(i = 0; i < ht; i++)
165 {
166 for(j = 0; j < wd; j++)
167 {
168 WORD32 diff;
169 diff = (*pu1_buf1 - *pu1_buf2);
170 pu1_buf1 += incr1;
171 pu1_buf2 += incr2;
172 df_psnr[comp] += diff * diff;
173 }
174 pu1_buf1 += strd1;
175 pu1_buf2 += strd2;
176 }
177 df_psnr[comp] /= (wd * ht);
178 if(df_psnr[comp])
179 df_psnr[comp] = 20 * log10(255 / sqrt(df_psnr[comp]));
180 else
181 df_psnr[comp] = 100;
182
183 ps_app_ctxt->adbl_psnr[comp] += df_psnr[comp];
184 switch(comp)
185 {
186 case 0:
187 printf("Y :");
188 break;
189 case 1:
190 printf("U :");
191 break;
192 case 2:
193 printf("V :");
194 break;
195 default:
196 break;
197 }
198 printf("%2.2f\t", df_psnr[comp]);
199 }
200
201 ps_app_ctxt->u4_psnr_cnt++;
202 }
203
204 /**
205 **************************************************************************
206 * @brief Prints PSNR for the Y, U, V component
207 **************************************************************************
208 */
print_average_psnr(app_ctxt_t * ps_app_ctxt)209 void print_average_psnr(app_ctxt_t *ps_app_ctxt)
210 {
211 printf("\n");
212
213 printf("Avg PSNR Y : %-2.2f\n",
214 (ps_app_ctxt->adbl_psnr[0] / ps_app_ctxt->u4_psnr_cnt));
215 printf("Avg PSNR U : %-2.2f\n",
216 (ps_app_ctxt->adbl_psnr[1] / ps_app_ctxt->u4_psnr_cnt));
217 printf("Avg PSNR V : %-2.2f\n",
218 (ps_app_ctxt->adbl_psnr[2] / ps_app_ctxt->u4_psnr_cnt));
219 }
220
221