]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2enc/stats.c
ENH: For the long long long long term adding the part of lib that encode in mpeg
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2enc / stats.c
1 /* stats.c, coding statistics                                               */
2
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
5 /*
6  * Disclaimer of Warranty
7  *
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.
15  *
16  * This disclaimer of warranty extends to the user of these programs and user's
17  * customers, employees, agents, transferees, successors, and assigns.
18  *
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
21  * patents.
22  *
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
26  * design.
27  *
28  */
29
30 #include <stdio.h>
31 #include <math.h>
32 #include "config.h"
33 #include "global.h"
34
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));
38
39
40 void calcSNR(org,rec)
41 unsigned char *org[3];
42 unsigned char *rec[3];
43 {
44   int w,h,offs;
45   double v,e;
46
47   w = horizontal_size;
48   h = (pict_struct==FRAME_PICTURE) ? vertical_size : (vertical_size>>1);
49   offs = (pict_struct==BOTTOM_FIELD) ? width : 0;
50
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));
54
55   if (chroma_format!=CHROMA444)
56   {
57     w >>= 1;
58     offs >>= 1;
59   }
60
61   if (chroma_format==CHROMA420)
62     h >>= 1;
63
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));
67
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));
71 }
72
73 static void calcSNR1(org,rec,lx,w,h,pv,pe)
74 unsigned char *org;
75 unsigned char *rec;
76 int lx,w,h;
77 double *pv,*pe;
78 {
79   int i, j;
80   double v1, s1, s2, e2;
81
82   s1 = s2 = e2 = 0.0;
83
84   for (j=0; j<h; j++)
85   {
86     for (i=0; i<w; i++)
87     {
88       v1 = org[i];
89       s1+= v1;
90       s2+= v1*v1;
91       v1-= rec[i];
92       e2+= v1*v1;
93     }
94     org += lx;
95     rec += lx;
96   }
97
98   s1 /= w*h;
99   s2 /= w*h;
100   e2 /= w*h;
101
102   /* prevent division by zero in calcSNR() */
103   if(e2==0.0)
104     e2 = 0.00001;
105
106   *pv = s2 - s1*s1; /* variance */
107   *pe = e2;         /* MSE */
108 }
109
110 void stats()
111 {
112   int i, j, k, nmb, mb_type;
113   int n_skipped, n_intra, n_ncoded, n_blocks, n_interp, n_forward, n_backward;
114   struct mbinfo *mbi;
115
116   nmb = mb_width*mb_height2;
117
118   n_skipped=n_intra=n_ncoded=n_blocks=n_interp=n_forward=n_backward=0;
119
120   for (k=0; k<nmb; k++)
121   {
122     mbi = mbinfo+k;
123     if (mbi->skipped)
124       n_skipped++;
125     else if (mbi->mb_type & MB_INTRA)
126       n_intra++;
127     else if (!(mbi->mb_type & MB_PATTERN))
128       n_ncoded++;
129
130     for (i=0; i<block_count; i++)
131       if (mbi->cbp & (1<<i))
132         n_blocks++;
133
134     if (mbi->mb_type & MB_FORWARD)
135     {
136       if (mbi->mb_type & MB_BACKWARD)
137         n_interp++;
138       else
139         n_forward++;
140     }
141     else if (mbi->mb_type & MB_BACKWARD)
142       n_backward++;
143   }
144
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);
160
161   fprintf(statfile,"\nmacroblock_type map:\n");
162
163   k = 0;
164
165   for (j=0; j<mb_height2; j++)
166   {
167     for (i=0; i<mb_width; i++)
168     {
169       mbi = mbinfo + k;
170       mb_type = mbi->mb_type;
171       if (mbi->skipped)
172         putc('S',statfile);
173       else if (mb_type & MB_INTRA)
174         putc('I',statfile);
175       else switch (mb_type & (MB_FORWARD|MB_BACKWARD))
176       {
177       case MB_FORWARD:
178         putc(mbi->motion_type==MC_FIELD ? 'f' :
179              mbi->motion_type==MC_DMV   ? 'p' :
180                                           'F',statfile); break;
181       case MB_BACKWARD:
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;
187       default:
188         putc('0',statfile); break;
189       }
190
191       if (mb_type & MB_QUANT)
192         putc('Q',statfile);
193       else if (mb_type & (MB_PATTERN|MB_INTRA))
194         putc(' ',statfile);
195       else
196         putc('N',statfile);
197
198       putc(' ',statfile);
199
200       k++;
201     }
202     putc('\n',statfile);
203   }
204
205   fprintf(statfile,"\nmquant map:\n");
206
207   k=0;
208   for (j=0; j<mb_height2; j++)
209   {
210     for (i=0; i<mb_width; i++)
211     {
212       if (i==0 || mbinfo[k].mquant!=mbinfo[k-1].mquant)
213         fprintf(statfile,"%3d",mbinfo[k].mquant);
214       else
215         fprintf(statfile,"   ");
216
217       k++;
218     }
219     putc('\n',statfile);
220   }
221
222 #if 0
223   fprintf(statfile,"\ncbp map:\n");
224
225   k=0;
226   for (j=0; j<mb_height2; j++)
227   {
228     for (i=0; i<mb_width; i++)
229     {
230       fprintf(statfile,"%02x ",mbinfo[k].cbp);
231
232       k++;
233     }
234     putc('\n',statfile);
235   }
236
237   if (pict_struct==FRAME_PICTURE && !frame_pred_dct)
238   {
239     fprintf(statfile,"\ndct_type map:\n");
240
241     k=0;
242     for (j=0; j<mb_height2; j++)
243     {
244       for (i=0; i<mb_width; i++)
245       {
246         if (mbinfo[k].mb_type & (MB_PATTERN|MB_INTRA))
247           fprintf(statfile,"%d  ",mbinfo[k].dct_type);
248         else
249           fprintf(statfile,"   ");
250   
251         k++;
252       }
253       putc('\n',statfile);
254     }
255   }
256
257   if (pict_type!=I_TYPE)
258   {
259     fprintf(statfile,"\nforward motion vectors (first vector, horizontal):\n");
260
261     k=0;
262     for (j=0; j<mb_height2; j++)
263     {
264       for (i=0; i<mb_width; i++)
265       {
266         if (mbinfo[k].mb_type & MB_FORWARD)
267           fprintf(statfile,"%4d",mbinfo[k].MV[0][0][0]);
268         else
269           fprintf(statfile,"   .");
270   
271         k++;
272       }
273       putc('\n',statfile);
274     }
275
276     fprintf(statfile,"\nforward motion vectors (first vector, vertical):\n");
277
278     k=0;
279     for (j=0; j<mb_height2; j++)
280     {
281       for (i=0; i<mb_width; i++)
282       {
283         if (mbinfo[k].mb_type & MB_FORWARD)
284           fprintf(statfile,"%4d",mbinfo[k].MV[0][0][1]);
285         else
286           fprintf(statfile,"   .");
287   
288         k++;
289       }
290       putc('\n',statfile);
291     }
292
293     fprintf(statfile,"\nforward motion vectors (second vector, horizontal):\n");
294
295     k=0;
296     for (j=0; j<mb_height2; j++)
297     {
298       for (i=0; i<mb_width; i++)
299       {
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]);
304         else
305           fprintf(statfile,"   .");
306   
307         k++;
308       }
309       putc('\n',statfile);
310     }
311
312     fprintf(statfile,"\nforward motion vectors (second vector, vertical):\n");
313
314     k=0;
315     for (j=0; j<mb_height2; j++)
316     {
317       for (i=0; i<mb_width; i++)
318       {
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]);
323         else
324           fprintf(statfile,"   .");
325   
326         k++;
327       }
328       putc('\n',statfile);
329     }
330
331
332   }
333     
334   if (pict_type==B_TYPE)
335   {
336     fprintf(statfile,"\nbackward motion vectors (first vector, horizontal):\n");
337
338     k=0;
339     for (j=0; j<mb_height2; j++)
340     {
341       for (i=0; i<mb_width; i++)
342       {
343         if (mbinfo[k].mb_type & MB_BACKWARD)
344           fprintf(statfile,"%4d",mbinfo[k].MV[0][1][0]);
345         else
346           fprintf(statfile,"   .");
347   
348         k++;
349       }
350       putc('\n',statfile);
351     }
352
353     fprintf(statfile,"\nbackward motion vectors (first vector, vertical):\n");
354
355     k=0;
356     for (j=0; j<mb_height2; j++)
357     {
358       for (i=0; i<mb_width; i++)
359       {
360         if (mbinfo[k].mb_type & MB_BACKWARD)
361           fprintf(statfile,"%4d",mbinfo[k].MV[0][1][1]);
362         else
363           fprintf(statfile,"   .");
364   
365         k++;
366       }
367       putc('\n',statfile);
368     }
369
370     fprintf(statfile,"\nbackward motion vectors (second vector, horizontal):\n");
371
372     k=0;
373     for (j=0; j<mb_height2; j++)
374     {
375       for (i=0; i<mb_width; i++)
376       {
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]);
381         else
382           fprintf(statfile,"   .");
383   
384         k++;
385       }
386       putc('\n',statfile);
387     }
388
389     fprintf(statfile,"\nbackward motion vectors (second vector, vertical):\n");
390
391     k=0;
392     for (j=0; j<mb_height2; j++)
393     {
394       for (i=0; i<mb_width; i++)
395       {
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]);
400         else
401           fprintf(statfile,"   .");
402   
403         k++;
404       }
405       putc('\n',statfile);
406     }
407
408
409   }
410 #endif
411     
412 #if 0
413   /* useful for debugging */
414   fprintf(statfile,"\nmacroblock info dump:\n");
415
416   k=0;
417   for (j=0; j<mb_height2; j++)
418   {
419     for (i=0; i<mb_width; i++)
420     {
421       fprintf(statfile,"%d: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
422       k,
423       mbinfo[k].mb_type,
424       mbinfo[k].motion_type,
425       mbinfo[k].dct_type,
426       mbinfo[k].mquant,
427       mbinfo[k].cbp,
428       mbinfo[k].skipped,
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]);
441
442       k++;
443     }
444   }
445 #endif
446 }