[cvs] / xvidcore / vfw / src / status.c Repository:
ViewVC logotype

Diff of /xvidcore/vfw/src/status.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1, Tue Jun 10 10:07:03 2003 UTC revision 1.1.2.4, Fri Jan 23 11:03:48 2004 UTC
# Line 0  Line 1 
1    /******************************************************************************
2     *
3     * XviD Video-for-Windows Frontend
4     * Quantizer histogram and encoding status window
5     *
6     * Copyright (C) 2003 Peter Ross <pross@xvid.org>
7     *
8     * This program is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation; either version 2 of the License, or
11     * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     * GNU General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21     *
22     * $Id$
23     *
24     *****************************************************************************/
25    
26    
27    #include <windows.h>
28    #include "resource.h"
29    #include "codec.h"
30    #include "status.h"
31    
32    #include "debug.h"
33    
34    
35    #define CLR_BG                  0
36    #define CLR_FG                  1
37    #define CLR_QUANT_I             2
38    #define CLR_QUANT_P             3
39    #define CLR_QUANT_B             4
40    
41    static void set_bic(RGBQUAD * rgb, int index, int r, int g, int b)
42    {
43            rgb[index].rgbRed = r;
44            rgb[index].rgbGreen = g;
45            rgb[index].rgbBlue = b;
46    }
47    
48    /*
49            draw graph into buffer
50    */
51    static void draw_graph(status_t *s)
52    {
53            unsigned int i,j;
54    
55            memset(s->buffer, CLR_BG, s->width*s->stride);
56    
57            for (j=0; j<s->height; j++)
58            for (i=0; i<31; i++)
59                    s->buffer[ j*s->stride + i*s->width31 ] = CLR_FG;
60    
61            if (s->count[0]>0) {
62                    for (i=0; i<31; i++) {
63                            /* i-vops */
64                            unsigned int j_height = (s->height-s->tm.tmHeight)*s->quant[0][i]/s->max_quant_frames;
65                            if (j_height==0 && s->quant[0][i]>0) j_height=1;
66    
67                            for(j=0; j < j_height; j++) {
68                                    memset(s->buffer + (s->tm.tmHeight+j)*s->stride + i*s->width31 + 1,
69                                                    CLR_QUANT_I, s->width31-1);
70                            }
71                            /* p/s-vops */
72                            j_height += (s->height-s->tm.tmHeight)*s->quant[1][i]/s->max_quant_frames;
73                            if (j_height==0 && s->quant[1][i]>0) j_height=1;
74    
75                            for(; j < j_height; j++) {
76                                    memset(s->buffer + (s->tm.tmHeight+j)*s->stride + i*s->width31 + 1,
77                                                    CLR_QUANT_P, s->width31-1);
78                            }
79                            /* b-vops */
80                            j_height += (s->height-s->tm.tmHeight)*s->quant[2][i]/s->max_quant_frames;
81                            if (j_height==0 && s->quant[2][i]>0) j_height=1;
82    
83                            for(; j < j_height; j++) {
84                                    memset(s->buffer + (s->tm.tmHeight+j)*s->stride + i*s->width31 + 1,
85                                                    CLR_QUANT_B, s->width31-1);
86                            }
87                    }
88            }
89    }
90    
91    
92    static const char * number[31] = {
93            "1", "2", "3", "4", "5", "6", "7", "8", "9",
94            "0","1","2","3","4","5","6","7","8","9",
95            "0","1","2","3","4","5","6","7","8","9",
96            "0","1"
97    };
98    
99    static double
100    avg_quant(int quants[31], int min, int max, char* buf)
101    {
102            int i, sum = 0, count = 0;
103            for (i = min; i <= max && i > 0; i++) {
104                    sum += i*quants[i-1];
105                    count += quants[i-1];
106            }
107    
108            if (count != 0) {
109                    double avg = (double)sum/(double)count;
110                    sprintf(buf, "%.2f", avg);
111                    return avg;
112            } else {
113                    buf[0] = 0;
114                    return 0.0;
115            }
116    }
117    
118    /* status window proc handlder */
119    
120    static BOOL CALLBACK status_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
121    {
122            status_t * s = (status_t*)GetWindowLong(hDlg, GWL_USERDATA);
123    
124            switch (uMsg)
125            {
126            case WM_INITDIALOG :
127                    SetWindowLong(hDlg, GWL_USERDATA, lParam);
128                    s = (status_t*)lParam;
129    
130                    s->hGraph = GetDlgItem(hDlg, IDC_STATUS_GRAPH);
131                    s->hDc = GetDC(s->hGraph);
132                    {
133                            RECT rect;
134                            GetWindowRect(s->hGraph, &rect);
135                            s->width = rect.right - rect.left;
136                            s->height = rect.bottom - rect.top;
137                    }
138                    s->width31 = s->width/31;
139                    s->stride = (s->width/4+1)*4;
140    
141                    s->buffer = malloc(s->width * s->stride);
142    
143                    s->bi = malloc(sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD));
144                    memset(s->bi, 0, sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD));
145    
146                    s->bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
147                    s->bi->bmiHeader.biWidth  = s->stride;
148                    s->bi->bmiHeader.biHeight = s->height;
149                    s->bi->bmiHeader.biPlanes = 1;
150                    s->bi->bmiHeader.biBitCount = 8;
151                    s->bi->bmiHeader.biCompression = BI_RGB;
152    
153                    set_bic(s->bi->bmiColors, CLR_BG,          0,   0,   0);
154                    set_bic(s->bi->bmiColors, CLR_FG,        128, 128, 128);
155                    set_bic(s->bi->bmiColors, CLR_QUANT_I,  255, 0,   0);
156                    set_bic(s->bi->bmiColors, CLR_QUANT_P,  0, 0,   255);
157                    set_bic(s->bi->bmiColors, CLR_QUANT_B,  0, 192,   0);
158    
159                    SelectObject(s->hDc, GetStockObject(DEFAULT_GUI_FONT));
160                    SetBkColor(s->hDc, *(DWORD*)&s->bi->bmiColors[CLR_BG]);
161                    SetTextColor(s->hDc, *(DWORD*)&s->bi->bmiColors[CLR_FG]);
162                    GetTextMetrics(s->hDc, &s->tm);
163    
164                    draw_graph(s);
165                    SetTimer(hDlg, IDC_STATUS_GRAPH, 1000, NULL);   /* 1 second */
166                    break;
167    
168            case WM_DESTROY :
169                    free(s->buffer);
170                    free(s->bi);
171                    KillTimer(hDlg, IDC_STATUS_GRAPH);
172                    s->hDlg = NULL;
173                    break;
174    
175            case WM_DRAWITEM :
176                    if (wParam==IDC_STATUS_GRAPH) {
177                            int i;
178    
179                            /* copy buffer into dc */
180                            SetDIBitsToDevice(s->hDc,
181                                    0, 0, s->width, s->height,
182                                    0, 0, 0, s->height,
183                                    s->buffer, s->bi, DIB_RGB_COLORS);
184    
185                            SetTextAlign(s->hDc, GetTextAlign(s->hDc)|TA_CENTER);
186    
187                            for (i=0; i<31; i++) {
188                                    TextOut(s->hDc, i*s->width31 + s->width/62,
189                                            s->height-s->tm.tmHeight, number[i], strlen(number[i]));
190                            }
191                    }
192                    break;
193    
194            case WM_TIMER :
195                    if (wParam==IDC_STATUS_GRAPH) {
196                            double avg_q; char buf[16];
197    
198                            SetDlgItemInt(hDlg, IDC_STATUS_I_NUM, (unsigned int)s->count[1], FALSE);
199                            SetDlgItemInt(hDlg, IDC_STATUS_P_NUM, (unsigned int)s->count[2], FALSE);
200                            SetDlgItemInt(hDlg, IDC_STATUS_B_NUM, (unsigned int)s->count[3], FALSE);
201                            SetDlgItemInt(hDlg, IDC_STATUS_NUM, (unsigned int)s->count[0], FALSE);
202    
203                            SetDlgItemInt(hDlg, IDC_STATUS_IQ_MIN, s->min_quant[1], FALSE);
204                            SetDlgItemInt(hDlg, IDC_STATUS_IQ_MAX, s->max_quant[1], FALSE);
205                            SetDlgItemInt(hDlg, IDC_STATUS_PQ_MIN, s->min_quant[2], FALSE);
206                            SetDlgItemInt(hDlg, IDC_STATUS_PQ_MAX, s->max_quant[2], FALSE);
207                            SetDlgItemInt(hDlg, IDC_STATUS_BQ_MIN, s->min_quant[3], FALSE);
208                            SetDlgItemInt(hDlg, IDC_STATUS_BQ_MAX, s->max_quant[3], FALSE);
209                            SetDlgItemInt(hDlg, IDC_STATUS_Q_MIN, s->min_quant[0], FALSE);
210                            SetDlgItemInt(hDlg, IDC_STATUS_Q_MAX, s->max_quant[0], FALSE);
211    
212                            SetDlgItemInt(hDlg, IDC_STATUS_IL_MIN, s->min_length[1], FALSE);
213                            SetDlgItemInt(hDlg, IDC_STATUS_IL_MAX, s->max_length[1], FALSE);
214                            if (s->count[1]>0)
215                                    SetDlgItemInt(hDlg, IDC_STATUS_IL_AVG, (unsigned int)(s->tot_length[1]/s->count[1]), FALSE);
216                            else
217                                    SetDlgItemInt(hDlg, IDC_STATUS_IL_AVG, 0, FALSE);
218                            SetDlgItemInt(hDlg, IDC_STATUS_IL_TOT, (unsigned int)(s->tot_length[1]/1024), FALSE);
219                            SetDlgItemInt(hDlg, IDC_STATUS_PL_MIN, s->min_length[2], FALSE);
220                            SetDlgItemInt(hDlg, IDC_STATUS_PL_MAX, s->max_length[2], FALSE);
221                            if (s->count[2]>0)
222                                    SetDlgItemInt(hDlg, IDC_STATUS_PL_AVG, (unsigned int)(s->tot_length[2]/s->count[2]), FALSE);
223                            else
224                                    SetDlgItemInt(hDlg, IDC_STATUS_PL_AVG, 0, FALSE);
225                            SetDlgItemInt(hDlg, IDC_STATUS_PL_TOT, (unsigned int)(s->tot_length[2]/1024), FALSE);
226                            SetDlgItemInt(hDlg, IDC_STATUS_BL_MIN, s->min_length[3], FALSE);
227                            SetDlgItemInt(hDlg, IDC_STATUS_BL_MAX, s->max_length[3], FALSE);
228                            if (s->count[3]>0)
229                                    SetDlgItemInt(hDlg, IDC_STATUS_BL_AVG, (unsigned int)(s->tot_length[3]/s->count[3]), FALSE);
230                            else
231                                    SetDlgItemInt(hDlg, IDC_STATUS_BL_AVG, 0, FALSE);
232                            SetDlgItemInt(hDlg, IDC_STATUS_BL_TOT, (unsigned int)(s->tot_length[3]/1024), FALSE);
233                            SetDlgItemInt(hDlg, IDC_STATUS_L_MIN, s->min_length[0], FALSE);
234                            SetDlgItemInt(hDlg, IDC_STATUS_L_MAX, s->max_length[0], FALSE);
235                            if (s->count[0]>0)
236                                    SetDlgItemInt(hDlg, IDC_STATUS_L_AVG, (int)(s->tot_length[0]/s->count[0]), FALSE);
237                            else
238                                    SetDlgItemInt(hDlg, IDC_STATUS_L_AVG, 0, FALSE);
239                            SetDlgItemInt(hDlg, IDC_STATUS_L_TOT, (unsigned int)(s->tot_length[0]/1024), FALSE);
240    
241                            if (s->count[0]>0) {
242                                    uint64_t kbits = 8*s->tot_length[0]/1000;
243                                    double secs = (double)s->count[0]/s->fps;
244                               SetDlgItemInt(hDlg, IDC_STATUS_KBPS, (int)(kbits/secs), FALSE);
245                            }else{
246                                    SetDlgItemInt(hDlg, IDC_STATUS_KBPS, 0, FALSE);
247                            }
248    
249                            avg_q = avg_quant(s->quant[0], s->min_quant[1], s->max_quant[1], buf) * s->count[1];
250                            SetDlgItemText(hDlg, IDC_STATUS_IQ_AVG, buf);
251    
252                            avg_q += avg_quant(s->quant[1], s->min_quant[2], s->max_quant[2], buf) * s->count[2];
253                            SetDlgItemText(hDlg, IDC_STATUS_PQ_AVG, buf);
254    
255                            avg_q += avg_quant(s->quant[2], s->min_quant[3], s->max_quant[3], buf) * s->count[3];
256                            SetDlgItemText(hDlg, IDC_STATUS_BQ_AVG, buf);
257    
258                            if (s->count[0] != 0) avg_q /= (double)s->count[0];
259                            sprintf(buf, "%.2f", avg_q);
260                            SetDlgItemText(hDlg, IDC_STATUS_Q_AVG, buf);
261    
262                            draw_graph(s);
263                            InvalidateRect(s->hGraph, NULL, FALSE);
264                    }
265                    break;
266    
267            case WM_COMMAND :
268                    if (LOWORD(wParam)==IDCANCEL) {
269                            DestroyWindow(hDlg);
270                    }
271                    break;
272    
273            default :
274                    return FALSE;
275            }
276    
277            return TRUE;
278    }
279    
280    
281    /* destroy status window
282       (however if the auto-close box is unchecked, dont destroy) */
283    
284    void status_destroy(status_t *s)
285    {
286            if (s->hDlg && IsDlgButtonChecked(s->hDlg,IDC_STATUS_DESTROY)==BST_CHECKED) {
287                    DestroyWindow(s->hDlg);
288            }
289    }
290    
291    
292    /* destroy status window, alwasys */
293    
294    void status_destroy_always(status_t *s)
295    {
296            if (s->hDlg) {
297                    DestroyWindow(s->hDlg);
298            }
299    }
300    
301    
302    /* create status window */
303    void status_create(status_t * s, unsigned int fps_inc, unsigned int fps_base)
304    {
305            int i;
306    
307            s->fps = fps_base/fps_inc;
308    
309            memset(s->quant[0], 0, 31*sizeof(int));
310            memset(s->quant[1], 0, 31*sizeof(int));
311            memset(s->quant[2], 0, 31*sizeof(int));
312            s->max_quant_frames = 0;
313            for (i=0; i<4; i++) {
314                    s->count[i] = 0;
315                    s->min_quant[i] = s->max_quant[i] = 0;
316                    s->min_length[i] = s->max_length[i] = 0;
317                    s->tot_length[i] = 0;
318            }
319    
320            s->hDlg = CreateDialogParam(g_hInst,
321                                                            MAKEINTRESOURCE(IDD_STATUS),
322                                                            GetDesktopWindow(),
323                                                            status_proc, (LPARAM)s);
324    
325            ShowWindow(s->hDlg, SW_SHOW);
326    }
327    
328    static char
329    type2char(int type)
330    {
331            if (type==XVID_TYPE_IVOP)
332                    return 'I';
333            if (type==XVID_TYPE_PVOP)
334                    return 'P';
335            if (type==XVID_TYPE_BVOP)
336                    return 'B';
337            return 'S';
338    }
339    
340    static void
341    status_debugoutput(status_t *s, int type, int length, int quant)
342    {
343            if (s->hDlg && IsDlgButtonChecked(s->hDlg,IDC_SHOWINTERNALS)==BST_CHECKED) {
344                    LRESULT litem;
345                    char buf[128];
346                    sprintf(buf, "[%6d] ->%c  q:%2d (%6d b)",
347                                    (unsigned int)(s->count[0]), type2char(type), quant, length);
348    
349                    SendDlgItemMessage (s->hDlg,IDC_DEBUGOUTPUT, LB_ADDSTRING, 0, (LPARAM)(LPSTR)buf);
350    
351                    litem = SendDlgItemMessage (s->hDlg, IDC_DEBUGOUTPUT, LB_GETCOUNT, 0, 0L);
352    
353                    if (litem > 12)
354                            litem = SendDlgItemMessage (s->hDlg,IDC_DEBUGOUTPUT, LB_DELETESTRING, 0, 0L);
355    
356                    SendDlgItemMessage(s->hDlg,IDC_DEBUGOUTPUT, LB_SETCURSEL, (WORD)(litem-1), 0L);
357            }
358    }
359    
360    /* feed stats info into the window */
361    void status_update(status_t *s, int type, int length, int quant)
362    {
363            s->count[0]++;
364            s->count[type]++;
365    
366            status_debugoutput(s, type, length, quant);
367    
368            if (type == 4) type = 2; /* XVID_TYPE_SVOP to XVID_TYPE_PVOP */
369    
370            if (s->min_quant[0]==0 || quant<s->min_quant[0]) s->min_quant[0] = quant;
371            if (s->max_quant[0]==0 || quant>s->max_quant[0]) s->max_quant[0] = quant;
372            if (s->min_quant[type]==0 || quant<s->min_quant[type]) s->min_quant[type] = quant;
373            if (s->max_quant[type]==0|| quant>s->max_quant[type]) s->max_quant[type] = quant;
374    
375            s->quant[type-1][quant-1]++;
376            if (s->quant[0][quant-1] + s->quant[1][quant-1] + s->quant[2][quant-1] > s->max_quant_frames)
377                    s->max_quant_frames = s->quant[0][quant-1] + s->quant[1][quant-1] + s->quant[2][quant-1];
378    
379            if (s->min_length[0]==0 || length<s->min_length[0]) s->min_length[0] = length;
380            if (s->max_length[0]==0 || length>s->max_length[0]) s->max_length[0] = length;
381            if (s->min_length[type]==0 || length<s->min_length[type]) s->min_length[type] = length;
382            if (s->max_length[type]==0|| length>s->max_length[type]) s->max_length[type] = length;
383            s->tot_length[0] += length;
384            s->tot_length[type] += length;
385    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.4

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4