• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* File Includes                                                             */
23 /*****************************************************************************/
24 /* System include files */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29 
30 /* User include files */
31 #include "ih264_typedefs.h"
32 #include "ih264_defs.h"
33 #include "iv2.h"
34 #include "ive2.h"
35 #include "ih264e.h"
36 #include "app.h"
37 #include "psnr.h"
38 
39 /*****************************************************************************/
40 /*                                                                           */
41 /*  Function Name : init_psnr                                                */
42 /*                                                                           */
43 /*  Description   : Initialize  PSNR for the Y, U, V component               */
44 /*                                                                           */
45 /*  Inputs        :                                                          */
46 /*                                                                           */
47 /*  Globals       :                                                          */
48 /*                                                                           */
49 /*  Processing    :                                                          */
50 /*                                                                           */
51 /*  Outputs       :                                                          */
52 /*                                                                           */
53 /*  Returns       :                                                          */
54 /*                                                                           */
55 /*  Issues        :                                                          */
56 /*                                                                           */
57 /*  Revision History:                                                        */
58 /*                                                                           */
59 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
60 /*         28 12 2005   Ittiam          Draft                                */
61 /*                                                                           */
62 /*****************************************************************************/
init_psnr(app_ctxt_t * ps_app_ctxt)63 void init_psnr(app_ctxt_t *ps_app_ctxt)
64 {
65     ps_app_ctxt->adbl_psnr[0]   = 0;
66     ps_app_ctxt->adbl_psnr[1]   = 0;
67     ps_app_ctxt->adbl_psnr[2]   = 0;
68     ps_app_ctxt->u4_psnr_cnt    = 0;
69 }
70 
71 
72 /*****************************************************************************/
73 /*                                                                           */
74 /*  Function Name : compute_psnr                                             */
75 /*                                                                           */
76 /*  Description   : Computes the PSNR for the Y, U, V component              */
77 /*                                                                           */
78 /*  Inputs        :                                                          */
79 /*                                                                           */
80 /*  Globals       :                                                          */
81 /*                                                                           */
82 /*  Processing    :                                                          */
83 /*                                                                           */
84 /*  Outputs       :                                                          */
85 /*                                                                           */
86 /*  Returns       :                                                          */
87 /*                                                                           */
88 /*  Issues        :                                                          */
89 /*                                                                           */
90 /*  Revision History:                                                        */
91 /*                                                                           */
92 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
93 /*         28 12 2005   Ittiam          Draft                                */
94 /*                                                                           */
95 /*****************************************************************************/
compute_psnr(app_ctxt_t * ps_app_ctxt,iv_raw_buf_t * ps_buf1,iv_raw_buf_t * ps_buf2)96 void compute_psnr(app_ctxt_t *ps_app_ctxt, iv_raw_buf_t *ps_buf1, iv_raw_buf_t *ps_buf2)
97 {
98     WORD32 i, j;
99     WORD32 comp;
100     DOUBLE df_psnr[3];
101     WORD32 wd, ht, strd1, strd2;
102     UWORD8 *pu1_buf1, *pu1_buf2;
103     WORD32 incr1, incr2;
104 
105     printf("\nPicNum %4d\t ", ps_app_ctxt->u4_psnr_cnt);
106 
107     for(comp = 0; comp < 3; comp++)
108     {
109         df_psnr[comp] = 0;
110         pu1_buf1 = (UWORD8 *)ps_buf1->apv_bufs[comp];
111         pu1_buf2 = (UWORD8 *)ps_buf2->apv_bufs[comp];
112         wd = ps_buf1->au4_wd[comp];
113         ht = ps_buf1->au4_ht[comp];
114         strd1 = ps_buf1->au4_strd[comp] - ps_buf1->au4_wd[comp];
115         strd2 = ps_buf2->au4_strd[comp] - ps_buf2->au4_wd[comp];
116         incr1 = 1;
117         incr2 = 1;
118 
119         if((IV_YUV_420SP_UV == ps_buf1->e_color_fmt)
120                         || (IV_YUV_420SP_VU == ps_buf1->e_color_fmt))
121         {
122             switch(comp)
123             {
124                 case 0:
125                     pu1_buf1 = ps_buf1->apv_bufs[0];
126                     break;
127                 case 1:
128                     if(IV_YUV_420SP_UV == ps_buf1->e_color_fmt)
129                         pu1_buf1 = (UWORD8 *)ps_buf1->apv_bufs[1];
130                     else
131                         pu1_buf1 = (UWORD8 *)ps_buf1->apv_bufs[1] + 1;
132                     incr1 = 2;
133                     wd = ps_buf1->au4_wd[0] >> 1;
134                     ht = ps_buf1->au4_ht[0] >> 1;
135                     break;
136                 case 2:
137                     if(IV_YUV_420SP_UV == ps_buf1->e_color_fmt)
138                         pu1_buf1 = (UWORD8 *)ps_buf1->apv_bufs[1] + 1;
139                     else
140                         pu1_buf1 = ps_buf1->apv_bufs[1];
141                     incr1 = 2;
142                     wd = ps_buf1->au4_wd[0] >> 1;
143                     ht = ps_buf1->au4_ht[0] >> 1;
144                     strd1 = ps_buf1->au4_strd[1] - ps_buf1->au4_wd[1];
145                     break;
146             }
147         }
148         if ((IV_YUV_420SP_UV == ps_buf2->e_color_fmt)
149                         || (IV_YUV_420SP_VU == ps_buf2->e_color_fmt))
150         {
151             switch(comp)
152             {
153                 case 0:
154                     pu1_buf2 = ps_buf2->apv_bufs[0];
155                     break;
156                 case 1:
157                     if(IV_YUV_420SP_UV == ps_buf2->e_color_fmt)
158                         pu1_buf2 = ps_buf2->apv_bufs[1];
159                     else
160                         pu1_buf2 = (UWORD8 *)ps_buf2->apv_bufs[1] + 1;
161                     incr2 = 2;
162                     wd = ps_buf2->au4_wd[0] >> 1;
163                     ht = ps_buf2->au4_ht[0] >> 1;
164 
165                     break;
166                 case 2:
167                     if(IV_YUV_420SP_UV == ps_buf2->e_color_fmt)
168                         pu1_buf2 = (UWORD8 *)ps_buf2->apv_bufs[1] + 1;
169                     else
170                         pu1_buf2 = ps_buf2->apv_bufs[1];
171                     incr2 = 2;
172                     wd = ps_buf2->au4_wd[0] >> 1;
173                     ht = ps_buf2->au4_ht[0] >> 1;
174                     strd2 = ps_buf2->au4_strd[1] - ps_buf2->au4_wd[1];
175 
176                     break;
177             }
178         }
179 
180         for(i = 0; i < ht; i++)
181         {
182             for(j = 0; j < wd; j++)
183             {
184                 WORD32 diff;
185                 diff = (*pu1_buf1 - *pu1_buf2);
186                 pu1_buf1 += incr1;
187                 pu1_buf2 += incr2;
188                 df_psnr[comp] += diff * diff;
189             }
190             pu1_buf1 += strd1;
191             pu1_buf2 += strd2;
192         }
193         df_psnr[comp] /= (wd * ht);
194         if(df_psnr[comp])
195             df_psnr[comp] = 20 * log10(255 / sqrt(df_psnr[comp]));
196         else
197             df_psnr[comp] = 100;
198 
199         ps_app_ctxt->adbl_psnr[comp] += df_psnr[comp];
200         switch(comp)
201         {
202             case 0:
203                 printf("Y :");
204                 break;
205             case 1:
206                 printf("U :");
207                 break;
208             case 2:
209                 printf("V :");
210                 break;
211             default:
212                 break;
213         }
214         printf("%2.2f\t", df_psnr[comp]);
215 
216     }
217 
218     ps_app_ctxt->u4_psnr_cnt++;
219 }
220 
221 
222 /*****************************************************************************/
223 /*                                                                           */
224 /*  Function Name : print_average_psnr                                       */
225 /*                                                                           */
226 /*  Description   : Computes the average PSNR for the Y, U, V component      */
227 /*                                                                           */
228 /*  Inputs        :                                                          */
229 /*                                                                           */
230 /*  Globals       :                                                          */
231 /*                                                                           */
232 /*  Processing    :                                                          */
233 /*                                                                           */
234 /*  Outputs       :                                                          */
235 /*                                                                           */
236 /*  Returns       :                                                          */
237 /*                                                                           */
238 /*  Issues        :                                                          */
239 /*                                                                           */
240 /*  Revision History:                                                        */
241 /*                                                                           */
242 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
243 /*         28 12 2005   Ittiam          Draft                                */
244 /*                                                                           */
245 /*****************************************************************************/
print_average_psnr(app_ctxt_t * ps_app_ctxt)246 void print_average_psnr(app_ctxt_t *ps_app_ctxt)
247 {
248     printf("\n");
249 
250     printf("Avg PSNR Y                      : %-2.2f\n", (ps_app_ctxt->adbl_psnr[0] / ps_app_ctxt->u4_psnr_cnt));
251     printf("Avg PSNR U                      : %-2.2f\n", (ps_app_ctxt->adbl_psnr[1] / ps_app_ctxt->u4_psnr_cnt));
252     printf("Avg PSNR V                      : %-2.2f\n", (ps_app_ctxt->adbl_psnr[2] / ps_app_ctxt->u4_psnr_cnt));
253 }
254 
255