File: dvc_test.c

package info (click to toggle)
directfb 1.7.7-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,212 kB
  • sloc: ansic: 306,760; cpp: 46,357; sh: 11,720; makefile: 5,620; perl: 662; asm: 507; xml: 116
file content (333 lines) | stat: -rw-r--r-- 10,538 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>

#include <directfb.h>

#include "dvc.h"

/*****************************************************************************/

static char                  *file = NULL;
static DFBSurfacePixelFormat  sf   = DSPF_I420;
static int                    sw   = 480;
static int                    sh   = 360;

/*****************************************************************************/

static struct {
     DFBSurfacePixelFormat  id;
     const char            *name;
} formats[] = {
     { DSPF_RGB332,   "RGB332" },
     { DSPF_ARGB1555, "ARGB1555" },
     { DSPF_RGB16,    "RGB16" },
     { DSPF_RGB24,    "RGB24" },
     { DSPF_RGB32,    "RGB32" },
     { DSPF_ARGB,     "ARGB" },
     { DSPF_YUY2,     "YUY2" },
     { DSPF_UYVY,     "UYVY" },
     { DSPF_I420,     "I420" },
     { DSPF_YV12,     "YV12" },
     { DSPF_NV12,     "NV12" },
     { DSPF_NV21,     "NV21" }
};

#define NUM_FORMATS (sizeof(formats)/sizeof(formats[0]))

static DFBSurfacePixelFormat
format_name_to_id( const char *name )
{
     int i;
     
     for (i = 0; i < NUM_FORMATS; i++) {
          if (!strcmp( name, formats[i].name ))
               return formats[i].id;
     }
     
     return DSPF_UNKNOWN;
}

static const char*
format_id_to_name( DFBSurfacePixelFormat id )
{
     int i;
     
     for (i = 0; i < NUM_FORMATS; i++) {
          if (id == formats[i].id)
               return formats[i].name;
     }
     
     return "Unknown";
}

/*****************************************************************************/

static inline unsigned long 
millisec( void )
{
     return clock()/1000;
}

static unsigned int rand_pool = 0x12345678;
static unsigned int rand_add  = 0x87654321;

static inline unsigned int
myrand( void ) 
{
     rand_pool ^= ((rand_pool << 7) | (rand_pool >> 25));
     rand_pool += rand_add;
     rand_add  += rand_pool;
     
     return rand_pool;
}

/*****************************************************************************/



static void
usage( void )
{
     fprintf( stderr, "Usage: dvc_test [options] image-file\n\n" );
     fprintf( stderr, "Options:\n" );
     fprintf( stderr, "  -h, --help                   Show this help\n" );
     fprintf( stderr, "  -f, --format <format>        Set frame pixel format\n" );
     fprintf( stderr, "  -s, --size <width>x<height>  Set frame size\n" );
     fprintf( stderr, "\n" );
     exit( EXIT_FAILURE );
}

static void
parse_options( int argc, char **argv )
{
     int i;
     
     for (i = 1; i < argc; i++) {
          char *opt = argv[i];
           
          if (*opt != '-') {
               file = opt;
               continue;
          }
          
          if (!strcmp( opt, "-h" ) || !strcmp( opt, "--help" )) {
               usage();
          }
          else if (!strcmp( opt, "-f" ) || !strcmp( opt, "--format" )) {
               opt = argv[++i];
               if (opt) {
                    sf = format_name_to_id( opt );
                    if (!sf) {
                         fprintf( stderr, "Unsupported format '%s'!\n", opt );
                         exit( EXIT_FAILURE );
                    }
               } else {
                    fprintf( stderr, "No format specified!\n" );
                    exit( EXIT_FAILURE );
               }
          }
          else if (!strcmp( opt, "-s" ) || !strcmp( opt, "--size" )) {
               opt = argv[++i];
               if (opt) {
                    if (sscanf( opt, "%ux%u", &sw, &sh ) != 2) {
                         fprintf( stderr, "Invalid size '%s'!\n", opt );
                         exit( EXIT_FAILURE );
                    }
               } else {
                    fprintf( stderr, "No size specified!\n" );
                    exit( EXIT_FAILURE );
               }
          }
     }
     
     if (!file || !*file)
          usage();
}               

static void
init_picture( DVCPicture *picture, IDirectFBSurface *surface )
{
     DFBSurfacePixelFormat format;
     
     surface->GetPixelFormat( surface, &format );
     picture->format = dfb2dvc_pixelformat( format );
     surface->GetSize( surface, &picture->width, &picture->height );
     surface->Lock( surface, DSLF_READ, &picture->base[0], &picture->pitch[0] );
     if (DFB_PLANAR_PIXELFORMAT(format)) {
          switch (format) {
               case DSPF_I420:
                    picture->pitch[1] = 
                    picture->pitch[2] = picture->pitch[0]/2;
                    picture->base[1] = picture->base[0] + 
                                       picture->pitch[0] * picture->height;
                    picture->base[2] = picture->base[1] + 
                                       picture->pitch[1] * picture->height/2;
                    break;
               case DSPF_YV12:
                    picture->pitch[1] = 
                    picture->pitch[2] = picture->pitch[0]/2;
                    picture->base[2] = picture->base[0] + 
                                       picture->pitch[0] * picture->height;
                    picture->base[1] = picture->base[2] + 
                                       picture->pitch[2] * picture->height/2;
                    break;
               case DSPF_NV12:
               case DSPF_NV21:
                    picture->pitch[1] = picture->pitch[0] & ~1;
                    picture->base[1] = picture->base[0] + 
                                       picture->pitch[0] * picture->height;
                    break;
               default:
                    break;
          }
     }
     surface->Unlock( surface );
     
     picture->palette = NULL;
     picture->palette_size = 0;
     
     picture->premultiplied = 
     picture->separated = false;
}

int
main( int argc, char **argv )
{
     DFBResult               ret;
     IDirectFB              *dfb;
     IDirectFBSurface       *primary;
     IDirectFBSurface       *source;
     IDirectFBImageProvider *provider;
     IDirectFBEventBuffer   *buffer;
     DFBSurfaceDescription   dsc;
     DVCPicture              picture;
     unsigned long           start, now;
     unsigned long           num;
     int                     dw, dh;
     DFBSurfacePixelFormat   df;
     static char             buf[128];
          
     DirectFBInit( &argc, &argv );
     
     parse_options( argc, argv );
          
     ret = DirectFBCreate( &dfb );
     if (ret)
          DirectFBErrorFatal( "DirectFBCreate()", ret );
          
     dfb->SetCooperativeLevel( dfb, DFSCL_FULLSCREEN );
     
     ret = dfb->CreateImageProvider( dfb, file, &provider );
     if (ret)
          DirectFBErrorFatal( "IDirectFB::CreateImageProvider()", ret );
     
     dsc.flags       = DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT;
     dsc.caps        = DSCAPS_SYSTEMONLY;
     dsc.width       = sw;
     dsc.height      = sh;
     dsc.pixelformat = sf;
     
     ret = dfb->CreateSurface( dfb, &dsc, &source );
     if (ret)
          DirectFBErrorFatal( "IDirectFB::CreateSurface( source )", ret );
     provider->RenderTo( provider, source, NULL );
     provider->Release( provider );
     
     init_picture( &picture, source );
     
     dsc.flags = DSDESC_CAPS;
     dsc.caps  = DSCAPS_PRIMARY;
          
     ret = dfb->CreateSurface( dfb, &dsc, &primary );
     if (ret)
          DirectFBErrorFatal( "IDirectFB::CreateSurface( primary )", ret );
     primary->GetSize( primary, &dw, &dh );
     primary->GetPixelFormat( primary, &df );
     primary->Clear( primary, 0, 0, 0, 0 );
     
     {
          IDirectFBFont      *font;
          DFBFontDescription  fdsc;
     
          fdsc.flags  = DFDESC_HEIGHT;
          fdsc.height = 36;
          
          ret = dfb->CreateFont( dfb, "/usr/share/fonts/truetype/decker.ttf", &fdsc, &font );
          if (ret)
               ret = dfb->CreateFont( dfb, NULL, &fdsc, &font );
          if (ret)
               DirectFBErrorFatal( "IDirectFB::CreateFont()", ret );
          primary->SetFont( primary, font );
          font->Release( font );
     }     
     
     ret = dfb->CreateInputEventBuffer( dfb, DICAPS_KEYS, 0, &buffer );
     if (ret)
          DirectFBErrorFatal( "IDirectFB::CreateInputEventBuffer()", ret );
     
     sleep(2);
     
     printf( "** Benchmarking at %dx%d %s->%s **\n", 
             sw, sh, format_id_to_name(sf), format_id_to_name(df) );
     fflush( stdout );
     
     /* copy/convert test */
     start = millisec();
     num   = 0;
     do {
          DFBRectangle rect = { x:myrand()%(dw-sw), y:myrand()%(dh-sh), w:sw, h:sh };
          dvc_scale_to_surface( &picture, primary, &rect, NULL );
          num++;
     } while ((now=millisec()) < (start+3000));
     
     printf( "Copy/Convert    %.3f MPixel/s, %.3f Frames/s, (%.3f sec)\n", 
             (double)sw*sh*num/(double)(now-start)/1000.0, 
             (double)num*1000.0/(double)(now-start), (double)(now-start)/1000.0 );
     fflush( stdout );
     
     snprintf( buf, sizeof(buf), "%.3f MPixel/s, %.3f Frames/s", 
               (double)sw*sh*num/(double)(now-start)/1000.0,
               (double)num*1000.0/(double)(now-start) );
     primary->SetColor( primary, 0xff, 0xff, 0x00, 0xff );
     primary->DrawString( primary, buf, -1, dw/2, dh/2-36, DSTF_CENTER | DSTF_TOP );

     buffer->Reset( buffer );   
     buffer->WaitForEventWithTimeout( buffer, 5, 0 );
     buffer->Reset( buffer );
     
     /* scale/convert test */
     start = millisec();
     num   = 0;
     do {
          dvc_scale_to_surface( &picture, primary, NULL, NULL );
          num++;
     } while ((now=millisec()) < (start+3000));
     
     printf( "Scale/Convert   %.3f MPixel/s, %.3f Frames/s, (%.3f sec)\n", 
             (double)dw*dh*num/(double)(now-start)/1000.0, 
             (double)num*1000.0/(double)(now-start), (double)(now-start)/1000.0 );
     fflush( stdout );
     
     snprintf( buf, sizeof(buf), "%.3f MPixel/s, %.3f Frames/s", 
               (double)dw*dh*num/(double)(now-start)/1000.0,
               (double)num*1000.0/(double)(now-start) );
     primary->SetColor( primary, 0xff, 0xff, 0x00, 0xff );
     primary->DrawString( primary, buf, -1, dw/2, dh/2-36, DSTF_CENTER | DSTF_TOP );
     
     buffer->Reset( buffer );
     buffer->WaitForEventWithTimeout( buffer, 5, 0 );
     buffer->Reset( buffer );
             
     buffer->Release( buffer );
     source->Release( source );
     primary->Release( primary );
     dfb->Release( dfb );
     
     return 0;
}