• 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];
115         strd2 = ps_buf2->au4_strd[comp];
116         incr1 = 1;
117         incr2 = 1;
118 
119         if((IV_YUV_420SP_UV == ps_buf1->e_color_fmt)
120                         || (IV_YUV_420SP_UV == 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                     break;
134                 case 2:
135                     if(IV_YUV_420SP_UV == ps_buf1->e_color_fmt)
136                         pu1_buf1 = (UWORD8 *)ps_buf1->apv_bufs[1] + 1;
137                     else
138                         pu1_buf1 = ps_buf1->apv_bufs[1];
139                     incr1 = 2;
140                     break;
141             }
142         }
143         if ((IV_YUV_420SP_UV == ps_buf2->e_color_fmt)
144                         || (IV_YUV_420SP_UV == ps_buf2->e_color_fmt))
145         {
146             switch(comp)
147             {
148                 case 0:
149                     pu1_buf2 = ps_buf2->apv_bufs[0];
150                     break;
151                 case 1:
152                     if(IV_YUV_420SP_UV == ps_buf2->e_color_fmt)
153                         pu1_buf2 = ps_buf2->apv_bufs[1];
154                     else
155                         pu1_buf2 = (UWORD8 *)ps_buf2->apv_bufs[1] + 1;
156                     incr1 = 2;
157                     break;
158                 case 2:
159                     if(IV_YUV_420SP_UV == ps_buf2->e_color_fmt)
160                         pu1_buf2 = (UWORD8 *)ps_buf2->apv_bufs[1] + 1;
161                     else
162                         pu1_buf2 = ps_buf2->apv_bufs[1];
163                     incr1 = 2;
164                     break;
165             }
166         }
167 
168         for(i = 0; i < ht; i++)
169         {
170             for(j = 0; j < wd; j++)
171             {
172                 WORD32 diff;
173                 diff = (*pu1_buf1 - *pu1_buf2);
174                 pu1_buf1 += incr1;
175                 pu1_buf2 += incr2;
176                 df_psnr[comp] += diff * diff;
177             }
178             pu1_buf1 += strd1 - ps_buf1->au4_wd[comp];
179             pu1_buf2 += strd2 - ps_buf2->au4_wd[comp];
180         }
181         df_psnr[comp] /= (wd * ht);
182         if(df_psnr[comp])
183             df_psnr[comp] = 20 * log10(255 / sqrt(df_psnr[comp]));
184         else
185             df_psnr[comp] = 100;
186 
187         ps_app_ctxt->adbl_psnr[comp] += df_psnr[comp];
188         switch(comp)
189         {
190             case 0:
191                 printf("Y :");
192                 break;
193             case 1:
194                 printf("U :");
195                 break;
196             case 2:
197                 printf("V :");
198                 break;
199             default:
200                 break;
201         }
202         printf("%2.2f\t", df_psnr[comp]);
203 
204     }
205 
206     ps_app_ctxt->u4_psnr_cnt++;
207 }
208 
209 
210 /*****************************************************************************/
211 /*                                                                           */
212 /*  Function Name : print_average_psnr                                       */
213 /*                                                                           */
214 /*  Description   : Computes the average PSNR for the Y, U, V component      */
215 /*                                                                           */
216 /*  Inputs        :                                                          */
217 /*                                                                           */
218 /*  Globals       :                                                          */
219 /*                                                                           */
220 /*  Processing    :                                                          */
221 /*                                                                           */
222 /*  Outputs       :                                                          */
223 /*                                                                           */
224 /*  Returns       :                                                          */
225 /*                                                                           */
226 /*  Issues        :                                                          */
227 /*                                                                           */
228 /*  Revision History:                                                        */
229 /*                                                                           */
230 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
231 /*         28 12 2005   Ittiam          Draft                                */
232 /*                                                                           */
233 /*****************************************************************************/
print_average_psnr(app_ctxt_t * ps_app_ctxt)234 void print_average_psnr(app_ctxt_t *ps_app_ctxt)
235 {
236     printf("\n");
237 
238     printf("Avg PSNR Y                      : %-2.2f\n", (ps_app_ctxt->adbl_psnr[0] / ps_app_ctxt->u4_psnr_cnt));
239     printf("Avg PSNR U                      : %-2.2f\n", (ps_app_ctxt->adbl_psnr[1] / ps_app_ctxt->u4_psnr_cnt));
240     printf("Avg PSNR V                      : %-2.2f\n", (ps_app_ctxt->adbl_psnr[2] / ps_app_ctxt->u4_psnr_cnt));
241 }
242 
243