1 /* stats.c, coding statistics */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
35 /* private prototypes */
36 static void calcSNR1 _ANSI_ARGS_((unsigned char *org, unsigned char *rec,
37 int lx, int w, int h, double *pv, double *pe));
41 unsigned char *org[3];
42 unsigned char *rec[3];
48 h = (pict_struct==FRAME_PICTURE) ? vertical_size : (vertical_size>>1);
49 offs = (pict_struct==BOTTOM_FIELD) ? width : 0;
51 calcSNR1(org[0]+offs,rec[0]+offs,width2,w,h,&v,&e);
52 fprintf(statfile,"Y: variance=%4.4g, MSE=%3.3g (%3.3g dB), SNR=%3.3g dB\n",
53 v, e, 10.0*log10(255.0*255.0/e), 10.0*log10(v/e));
55 if (chroma_format!=CHROMA444)
61 if (chroma_format==CHROMA420)
64 calcSNR1(org[1]+offs,rec[1]+offs,chrom_width2,w,h,&v,&e);
65 fprintf(statfile,"U: variance=%4.4g, MSE=%3.3g (%3.3g dB), SNR=%3.3g dB\n",
66 v, e, 10.0*log10(255.0*255.0/e), 10.0*log10(v/e));
68 calcSNR1(org[2]+offs,rec[2]+offs,chrom_width2,w,h,&v,&e);
69 fprintf(statfile,"V: variance=%4.4g, MSE=%3.3g (%3.3g dB), SNR=%3.3g dB\n",
70 v, e, 10.0*log10(255.0*255.0/e), 10.0*log10(v/e));
73 static void calcSNR1(org,rec,lx,w,h,pv,pe)
80 double v1, s1, s2, e2;
102 /* prevent division by zero in calcSNR() */
106 *pv = s2 - s1*s1; /* variance */
112 int i, j, k, nmb, mb_type;
113 int n_skipped, n_intra, n_ncoded, n_blocks, n_interp, n_forward, n_backward;
116 nmb = mb_width*mb_height2;
118 n_skipped=n_intra=n_ncoded=n_blocks=n_interp=n_forward=n_backward=0;
120 for (k=0; k<nmb; k++)
125 else if (mbi->mb_type & MB_INTRA)
127 else if (!(mbi->mb_type & MB_PATTERN))
130 for (i=0; i<block_count; i++)
131 if (mbi->cbp & (1<<i))
134 if (mbi->mb_type & MB_FORWARD)
136 if (mbi->mb_type & MB_BACKWARD)
141 else if (mbi->mb_type & MB_BACKWARD)
145 fprintf(statfile,"\npicture statistics:\n");
146 fprintf(statfile," # of intra coded macroblocks: %4d (%.1f%%)\n",
147 n_intra,100.0*(double)n_intra/nmb);
148 fprintf(statfile," # of coded blocks: %4d (%.1f%%)\n",
149 n_blocks,100.0*(double)n_blocks/(block_count*nmb));
150 fprintf(statfile," # of not coded macroblocks: %4d (%.1f%%)\n",
151 n_ncoded,100.0*(double)n_ncoded/nmb);
152 fprintf(statfile," # of skipped macroblocks: %4d (%.1f%%)\n",
153 n_skipped,100.0*(double)n_skipped/nmb);
154 fprintf(statfile," # of forw. pred. macroblocks: %4d (%.1f%%)\n",
155 n_forward,100.0*(double)n_forward/nmb);
156 fprintf(statfile," # of backw. pred. macroblocks: %4d (%.1f%%)\n",
157 n_backward,100.0*(double)n_backward/nmb);
158 fprintf(statfile," # of interpolated macroblocks: %4d (%.1f%%)\n",
159 n_interp,100.0*(double)n_interp/nmb);
161 fprintf(statfile,"\nmacroblock_type map:\n");
165 for (j=0; j<mb_height2; j++)
167 for (i=0; i<mb_width; i++)
170 mb_type = mbi->mb_type;
173 else if (mb_type & MB_INTRA)
175 else switch (mb_type & (MB_FORWARD|MB_BACKWARD))
178 putc(mbi->motion_type==MC_FIELD ? 'f' :
179 mbi->motion_type==MC_DMV ? 'p' :
180 'F',statfile); break;
182 putc(mbi->motion_type==MC_FIELD ? 'b' :
183 'B',statfile); break;
184 case MB_FORWARD|MB_BACKWARD:
185 putc(mbi->motion_type==MC_FIELD ? 'd' :
186 'D',statfile); break;
188 putc('0',statfile); break;
191 if (mb_type & MB_QUANT)
193 else if (mb_type & (MB_PATTERN|MB_INTRA))
205 fprintf(statfile,"\nmquant map:\n");
208 for (j=0; j<mb_height2; j++)
210 for (i=0; i<mb_width; i++)
212 if (i==0 || mbinfo[k].mquant!=mbinfo[k-1].mquant)
213 fprintf(statfile,"%3d",mbinfo[k].mquant);
215 fprintf(statfile," ");
223 fprintf(statfile,"\ncbp map:\n");
226 for (j=0; j<mb_height2; j++)
228 for (i=0; i<mb_width; i++)
230 fprintf(statfile,"%02x ",mbinfo[k].cbp);
237 if (pict_struct==FRAME_PICTURE && !frame_pred_dct)
239 fprintf(statfile,"\ndct_type map:\n");
242 for (j=0; j<mb_height2; j++)
244 for (i=0; i<mb_width; i++)
246 if (mbinfo[k].mb_type & (MB_PATTERN|MB_INTRA))
247 fprintf(statfile,"%d ",mbinfo[k].dct_type);
249 fprintf(statfile," ");
257 if (pict_type!=I_TYPE)
259 fprintf(statfile,"\nforward motion vectors (first vector, horizontal):\n");
262 for (j=0; j<mb_height2; j++)
264 for (i=0; i<mb_width; i++)
266 if (mbinfo[k].mb_type & MB_FORWARD)
267 fprintf(statfile,"%4d",mbinfo[k].MV[0][0][0]);
269 fprintf(statfile," .");
276 fprintf(statfile,"\nforward motion vectors (first vector, vertical):\n");
279 for (j=0; j<mb_height2; j++)
281 for (i=0; i<mb_width; i++)
283 if (mbinfo[k].mb_type & MB_FORWARD)
284 fprintf(statfile,"%4d",mbinfo[k].MV[0][0][1]);
286 fprintf(statfile," .");
293 fprintf(statfile,"\nforward motion vectors (second vector, horizontal):\n");
296 for (j=0; j<mb_height2; j++)
298 for (i=0; i<mb_width; i++)
300 if (mbinfo[k].mb_type & MB_FORWARD
301 && ((pict_struct==FRAME_PICTURE && mbinfo[k].motion_type==MC_FIELD) ||
302 (pict_struct!=FRAME_PICTURE && mbinfo[k].motion_type==MC_16X8)))
303 fprintf(statfile,"%4d",mbinfo[k].MV[1][0][0]);
305 fprintf(statfile," .");
312 fprintf(statfile,"\nforward motion vectors (second vector, vertical):\n");
315 for (j=0; j<mb_height2; j++)
317 for (i=0; i<mb_width; i++)
319 if (mbinfo[k].mb_type & MB_FORWARD
320 && ((pict_struct==FRAME_PICTURE && mbinfo[k].motion_type==MC_FIELD) ||
321 (pict_struct!=FRAME_PICTURE && mbinfo[k].motion_type==MC_16X8)))
322 fprintf(statfile,"%4d",mbinfo[k].MV[1][0][1]);
324 fprintf(statfile," .");
334 if (pict_type==B_TYPE)
336 fprintf(statfile,"\nbackward motion vectors (first vector, horizontal):\n");
339 for (j=0; j<mb_height2; j++)
341 for (i=0; i<mb_width; i++)
343 if (mbinfo[k].mb_type & MB_BACKWARD)
344 fprintf(statfile,"%4d",mbinfo[k].MV[0][1][0]);
346 fprintf(statfile," .");
353 fprintf(statfile,"\nbackward motion vectors (first vector, vertical):\n");
356 for (j=0; j<mb_height2; j++)
358 for (i=0; i<mb_width; i++)
360 if (mbinfo[k].mb_type & MB_BACKWARD)
361 fprintf(statfile,"%4d",mbinfo[k].MV[0][1][1]);
363 fprintf(statfile," .");
370 fprintf(statfile,"\nbackward motion vectors (second vector, horizontal):\n");
373 for (j=0; j<mb_height2; j++)
375 for (i=0; i<mb_width; i++)
377 if (mbinfo[k].mb_type & MB_BACKWARD
378 && ((pict_struct==FRAME_PICTURE && mbinfo[k].motion_type==MC_FIELD) ||
379 (pict_struct!=FRAME_PICTURE && mbinfo[k].motion_type==MC_16X8)))
380 fprintf(statfile,"%4d",mbinfo[k].MV[1][1][0]);
382 fprintf(statfile," .");
389 fprintf(statfile,"\nbackward motion vectors (second vector, vertical):\n");
392 for (j=0; j<mb_height2; j++)
394 for (i=0; i<mb_width; i++)
396 if (mbinfo[k].mb_type & MB_BACKWARD
397 && ((pict_struct==FRAME_PICTURE && mbinfo[k].motion_type==MC_FIELD) ||
398 (pict_struct!=FRAME_PICTURE && mbinfo[k].motion_type==MC_16X8)))
399 fprintf(statfile,"%4d",mbinfo[k].MV[1][1][1]);
401 fprintf(statfile," .");
413 /* useful for debugging */
414 fprintf(statfile,"\nmacroblock info dump:\n");
417 for (j=0; j<mb_height2; j++)
419 for (i=0; i<mb_width; i++)
421 fprintf(statfile,"%d: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
424 mbinfo[k].motion_type,
429 mbinfo[k].MV[0][0][0],
430 mbinfo[k].MV[0][0][1],
431 mbinfo[k].MV[0][1][0],
432 mbinfo[k].MV[0][1][1],
433 mbinfo[k].MV[1][0][0],
434 mbinfo[k].MV[1][0][1],
435 mbinfo[k].MV[1][1][0],
436 mbinfo[k].MV[1][1][1],
437 mbinfo[k].mv_field_sel[0][0],
438 mbinfo[k].mv_field_sel[0][1],
439 mbinfo[k].mv_field_sel[1][0],
440 mbinfo[k].mv_field_sel[1][1]);