source: soft/giet_vm/applications/mjpeg/mjpeg.c @ 778

Last change on this file since 778 was 772, checked in by meunier, 9 years ago
  • Ajout de l'application rosenfeld
  • Changement du nom du flag O_CREATE en O_CREAT
File size: 19.4 KB
RevLine 
[723]1/////////////////////////////////////////////////////////////////////////////////////////
2// File   : mjpeg.c   
3// Date   : octobre 2015
4// author : Alain Greiner
5/////////////////////////////////////////////////////////////////////////////////////////
6// This multi-threaded application illustrates "pipe-line" parallelism, and message
7// passing programming model, on top of the POSIX threads API.
8// It makes the parallel decompression of a MJPEG bitstream contained in a file.
9// The application is described as a TCG (Task and Communication Graph), and all
10// communications between threads uses MWMR channels,.
11// It uses the chained buffer DMA component to display the images on the graphic display.
[741]12// It contains 5 types of threads, plus the "main" thread, that makes initialisation,
13// dispatch the byte stream to the various pipelines, and makes instrumentation.
[723]14// and 7 types of MWMR communication channels:
[741]15// - the main thread is only mapped in cluster[0,0], but all other threads
[723]16//   (DEMUX, VLD, IQZZ, IDCT, LIBU) are replicated in all clusters.
17// - all MWMR channels are replicated in all clusters.
18// The number of cluster cannot be larger than 16*16.
19// The number of processors per cluster is not constrained.
20// The frame buffer size must fit the decompressed images size.
21// It uses one TTY terminal shared by all tasks.
22/////////////////////////////////////////////////////////////////////////////////////////
23
24#include <stdio.h>
25#include <mwmr_channel.h>
26#include <malloc.h>
27#include <stdlib.h>
28#include "mjpeg.h"
[736]29#include <mapping_info.h>     // for coprocessor types and modes
[723]30
31
32// macro to use a shared TTY
[748]33#define PRINTF(...)    do { lock_acquire( &tty_lock ); \
34                            giet_tty_printf(__VA_ARGS__);  \
35                            lock_release( &tty_lock ); } while(0);
[723]36
37///////////////////////////////////////////////
38//       Global variables
39///////////////////////////////////////////////
40
41uint32_t         fd;    // file descriptor for the file containing the MJPEG stream
42
[736]43// arrays of pointers on MWMR channels
[741]44mwmr_channel_t*  main_2_demux[256];       // one per cluster
[723]45mwmr_channel_t*  demux_2_vld_data[256];   // one per cluster
46mwmr_channel_t*  demux_2_vld_huff[256];   // one per cluster
47mwmr_channel_t*  demux_2_iqzz[256];       // one per cluster
48mwmr_channel_t*  vld_2_iqzz[256];         // one per cluster
49mwmr_channel_t*  iqzz_2_idct[256];        // one per cluster
50mwmr_channel_t*  idct_2_libu[256];        // one per cluster
51
[736]52// thread trdid ( for pthread_create() and pthread_join() )
53pthread_t   trdid_demux[256];             // one per cluster
54pthread_t   trdid_vld[256];               // one per cluster
55pthread_t   trdid_iqzz[256];              // one per cluster
56pthread_t   trdid_idct[256];              // one per cluster
57pthread_t   trdid_libu[256];              // one per cluster
58
[723]59user_lock_t      tty_lock;                // lock protecting shared TTY
60
61uint8_t*         cma_buf[256];            // CMA buffers (one per cluster)
62void*            cma_sts[256];            // CMA buffers status
63
64uint32_t         fbf_width;               // Frame Buffer width
65uint32_t         fbf_height;              // Frame Buffer height
66
67uint32_t         nblocks_h;               // number of blocks in a column
68uint32_t         nblocks_w;               // number of blocks in a row   
69
70uint32_t         date[MAX_IMAGES];        // date of libu completion
71
72////////////////////////////////////////////////
73// declare thread functions
74////////////////////////////////////////////////
75
76extern void demux( uint32_t index );
77extern void vld( uint32_t index );
78extern void iqzz( uint32_t index );
79extern void idct( uint32_t index );
80extern void libu( uint32_t index );
81
82/////////////////////////////////////////
83__attribute__ ((constructor)) void main()
84/////////////////////////////////////////
85{
86    // get platform parameters
87    uint32_t  x_size;
88    uint32_t  y_size;
89    uint32_t  nprocs;
90    giet_procs_number( &x_size , &y_size , &nprocs );
91
92    // shared TTY allocation
93    giet_tty_alloc( 1 );
94    lock_init( &tty_lock );
95
96    // check platform parameters
97    giet_pthread_assert( (nprocs <= 6),
98                         "[MJPEG ERROR] nprocs cannot be larger than 4");
99
100    giet_pthread_assert( (x_size <= 16),
101                         "[MJPEG ERROR] x_size cannot be larger than 16");
102
103    giet_pthread_assert( (y_size <= 16),
104                         "[MJPEG ERROR] y_size cannot be larger than 16");
105
106    giet_pthread_assert( (MAX_IMAGES >= (x_size*y_size)),
107                         "MJPEG ERROR] number of images smaller than x_size * y_size");
108
109    // check frame buffer size
110    giet_fbf_size( &fbf_width , &fbf_height );
111
112    giet_pthread_assert( ((fbf_width & 0x7) == 0) && ((fbf_height & 0x7) == 0) ,
113                         "[MJPEG ERROR] image width and height must be multiple of 8");
114
115    // request frame buffer and CMA channel allocation
116    giet_fbf_alloc();
117    giet_fbf_cma_alloc( x_size * y_size );
118
119    // file name and image size acquisition
120    char          file_pathname[256];
121    uint32_t      image_width;
122    uint32_t      image_height;
[741]123    uint32_t      fd;                   // file descriptor
[723]124
[736]125    if ( INTERACTIVE_MODE )
126    {
[748]127        PRINTF("\n[MJPEG] enter path for JPEG stream file (default is plan_48.mjpg)\n> ");
[736]128        giet_tty_gets( file_pathname , 256 );
[723]129
[736]130        if ( file_pathname[0] == 0 )
131        {
132            strcpy( file_pathname , "/misc/plan_48.mjpg" );
133            image_width  = 48;
134            image_height = 48;
135        }
136        else
137        {
[748]138            PRINTF("\n[MJPEG] enter image width\n> ");
[736]139            giet_tty_getw( &image_width );
[748]140            PRINTF("\n[MJPEG] enter image height\n> "); 
[736]141            giet_tty_getw( &image_height );
[748]142            PRINTF("\n");
[736]143        }
144    }
145    else
[723]146    {
147        strcpy( file_pathname , "/misc/plan_48.mjpg" );
148        image_width  = 48;
149        image_height = 48;
150    }
[736]151   
[723]152    giet_pthread_assert( (image_width == fbf_width) && (image_height == fbf_height) ,
153                         "[MJPEG ERROR] image size doesn't fit frame buffer size");
154 
[740]155    if ( USE_DCT_COPROC )
[748]156    {
157        PRINTF("\n\n[MJPEG] stream %s / %d clusters / %d cores / DCT COPROC\n\n",
158               file_pathname , x_size*y_size , nprocs );
159    }
[740]160    else
[748]161    { 
162        PRINTF("\n\n[MJPEG] stream %s / %d clusters / %d cores / NO DCT COPROC\n\n",
163               file_pathname , x_size*y_size , nprocs );
164    }
[736]165
[723]166    // compute nblocks_h & nblocks_w
167    nblocks_w = fbf_width / 8;
168    nblocks_h = fbf_height / 8;
169
170    // open file containing the MJPEG bit stream
[741]171    fd = giet_fat_open( file_pathname , 0 );
[723]172
173    giet_pthread_assert( (fd >= 0),
174                         "[MJPEG ERROR] cannot open MJPEG stream file");
175
176    // index for loops
177    uint32_t x;
178    uint32_t y;
179    uint32_t n;
180
181    uint32_t*  buffer; 
182
183    // initialise distributed heap,
184    // allocate MWMR channels
185    // allocate buffers for CMA
186    for ( x = 0 ; x < x_size ; x++ ) 
187    {
188        for ( y = 0 ; y < y_size ; y++ ) 
189        {
190            uint32_t index = x*y_size + y;
191
192            // initialise heap[x][y]
193            heap_init( x , y );
194
195            // allocate MWMR channels in cluster[x][y]
[741]196            main_2_demux[index]     = remote_malloc( sizeof( mwmr_channel_t ) , x , y );
197            buffer                  = remote_malloc( 4 * MAIN_2_DEMUX_DEPTH , x , y );
198            mwmr_init( main_2_demux[index] , buffer , 1 , MAIN_2_DEMUX_DEPTH );
[723]199
200            demux_2_vld_data[index] = remote_malloc( sizeof( mwmr_channel_t ) , x , y );
201            buffer                  = remote_malloc( 4 * DEMUX_2_VLD_DATA_DEPTH , x , y );
202            mwmr_init( demux_2_vld_data[index] , buffer , 1 , DEMUX_2_VLD_DATA_DEPTH );
203
204            demux_2_vld_huff[index] = remote_malloc( sizeof( mwmr_channel_t ) , x , y );
205            buffer                  = remote_malloc( 4 * DEMUX_2_VLD_HUFF_DEPTH , x , y );
206            mwmr_init( demux_2_vld_huff[index] , buffer , 1 , DEMUX_2_VLD_HUFF_DEPTH );
207
208            demux_2_iqzz[index]     = remote_malloc( sizeof( mwmr_channel_t ) , x , y );
209            buffer                  = remote_malloc( 4 * DEMUX_2_IQZZ_DEPTH , x , y );
210            mwmr_init( demux_2_iqzz[index] , buffer , 1 , DEMUX_2_IQZZ_DEPTH );
211
212            vld_2_iqzz[index]       = remote_malloc( sizeof( mwmr_channel_t ) , x , y );
213            buffer                  = remote_malloc( 4 * VLD_2_IQZZ_DEPTH , x , y );
214            mwmr_init( vld_2_iqzz[index] , buffer , 1 , VLD_2_IQZZ_DEPTH );
215
216            iqzz_2_idct[index]      = remote_malloc( sizeof( mwmr_channel_t ) , x , y );
217            buffer                  = remote_malloc( 4 * IQZZ_2_IDCT_DEPTH , x , y );
218            mwmr_init( iqzz_2_idct[index] , buffer , 1 , IQZZ_2_IDCT_DEPTH );
219
220            idct_2_libu[index]      = remote_malloc( sizeof( mwmr_channel_t ) , x , y );
221            buffer                  = remote_malloc( 4 * IDCT_2_LIBU_DEPTH , x , y );
222            mwmr_init( idct_2_libu[index] , buffer , 1 , IDCT_2_LIBU_DEPTH );
223
224            // allocate and register CMA buffers in cluster[x][y]
225            cma_buf[index] = remote_malloc( fbf_width * fbf_height , x , y );
226            cma_sts[index] = remote_malloc( 64 , x , y );
227            giet_fbf_cma_init_buf( index , cma_buf[index] , cma_sts[index] );
228        }
229    }
230
231    // start CMA channel
[736]232    giet_fbf_cma_start();
[723]233
[741]234    mwmr_channel_t* pc;
[723]235
[736]236    for ( n = 0 ; n < x_size*y_size ; n++ )
237    {
[741]238        pc = main_2_demux[n];
239        PRINTF(" - main_2_demux[%d]  = %x / &lock = %x / &buf = %x / size = %d\n",
[748]240               n, pc, (uint32_t)&pc->lock, (uint32_t)pc->data, pc->depth<<2 );
[723]241
[741]242        pc = demux_2_vld_data[n];
[736]243        PRINTF(" - demux_2_vld[%d] = %x / &lock = %x / &buf = %x / size = %d\n",
[748]244               n, pc, (uint32_t)&pc->lock, (uint32_t)pc->data, pc->depth<<2 );
[736]245
[741]246        pc = vld_2_iqzz[n];
[736]247        PRINTF(" - vld_2_iqzz[%d]  = %x / &lock = %x / &buf = %x / size = %d\n",
[748]248               n, pc, (uint32_t)&pc->lock, (uint32_t)pc->data, pc->depth<<2 );
[736]249
[741]250        pc = iqzz_2_idct[n];
[736]251        PRINTF(" - iqzz_2_idct[%d] = %x / &lock = %x / &buf = %x / size = %d\n",
[748]252               n, pc, (uint32_t)&pc->lock, (uint32_t)pc->data, pc->depth<<2 );
[736]253
[741]254        pc = idct_2_libu[n];
[736]255        PRINTF(" - idct_2_libu[%d] = %x / &lock = %x / &buf = %x / size = %d\n",
[748]256               n, pc, (uint32_t)&pc->lock, (uint32_t)pc->data, pc->depth<<2 );
[736]257    }
258
[723]259    // launch all threads : precise mapping is defined in the mjpeg.py file
[736]260    uint32_t index;
[723]261
[736]262    for ( x = 0 ; x < x_size ; x++ )
[723]263    {
[736]264        for ( y = 0 ; y < y_size ; y++ )
265        {
266            index = x * y_size + y;
[723]267
[736]268            // DEMUX 
269            if ( giet_pthread_create( &trdid_demux[index], NULL, &demux , (void*)index ) )
270            giet_pthread_exit( "error launching thread demux\n");
[723]271
[736]272            // VLD
273            if ( giet_pthread_create( &trdid_vld[index], NULL, &vld , (void*)index ) )
274            giet_pthread_exit( "error launching thread vld\n");
[723]275
[736]276            // IQZZ
277            if ( giet_pthread_create( &trdid_iqzz[index], NULL, &iqzz , (void*)index ) )
278            giet_pthread_exit( "error launching thread iqzz");
[723]279
[736]280            // IDCT
281            if ( USE_DCT_COPROC )  // allocate, initialise, and start hardware coprocessor
282            {
283                giet_coproc_channel_t in_channel;
284                giet_coproc_channel_t out_channel;
285                uint32_t  cluster_xy  = (x<<4) + y;
286                uint32_t  coproc_type = 2;
287                uint32_t  info;
288
289                // allocate DCT coprocessor
290                giet_coproc_alloc( cluster_xy , coproc_type , &info );
291
292                // initialize channels
293                in_channel.channel_mode = MODE_MWMR;
294                in_channel.buffer_size  = (iqzz_2_idct[index]->depth)<<2;
295                in_channel.buffer_vaddr = (uint32_t)(iqzz_2_idct[index]->data);
296                in_channel.status_vaddr = (uint32_t)(&iqzz_2_idct[index]->sts);
297                in_channel.lock_vaddr   = (uint32_t)(&iqzz_2_idct[index]->lock);
298   
299                giet_coproc_channel_init( cluster_xy , coproc_type , 0 , &in_channel );
300
301                out_channel.channel_mode = MODE_MWMR;
302                out_channel.buffer_size  = (idct_2_libu[index]->depth)<<2;
303                out_channel.buffer_vaddr = (uint32_t)(idct_2_libu[index]->data);
304                out_channel.status_vaddr = (uint32_t)(&idct_2_libu[index]->sts);
305                out_channel.lock_vaddr   = (uint32_t)(&idct_2_libu[index]->lock);
306
307                giet_coproc_channel_init( cluster_xy , coproc_type , 1 , &out_channel );
308
309                // start coprocessor
310                giet_coproc_run( cluster_xy , coproc_type );
311            }
312            else                   // launches a software thread
313            {
314                if ( giet_pthread_create( &trdid_idct[index], NULL, &idct , (void*)index ) )
315                giet_pthread_exit( "error launching thread idct\n");
316            }
317
318            // LIBU
319            if ( giet_pthread_create( &trdid_libu[index], NULL, &libu , (void*)index ) )
320            giet_pthread_exit( "error launching thread libu\n");
321        }
[723]322    }
323
[741]324    /////////////////////////////////////////////////////////////////////////////////////
325    // dispatch the byte stream to the demux threads, one compressed image per cluster.
326    // It transfer the stream from the file identified by the fd argument to a 1024
327    // bytes local buffer. It analyses the stream to detect the End_of_Image markers.
328    // All the bytes corresponding to a single image from the first byte, to the EOI
329    // marker included, are written in the main_2_demux[index] channel, in increasing
330    // order of the cluster index.
331    /////////////////////////////////////////////////////////////////////////////////////
332
333    // allocate input buffer : 1024 bytes
334    uint8_t        bufin[1024];
335
336    // allocate output bufio to access output MWMR channels : 64 bytes == 16 words
337    mwmr_bufio_t  bufio;
338    uint8_t       bufout[64]; 
339    mwmr_bufio_init( &bufio , bufout , 64 , 0 , main_2_demux[0] );
340
341    uint32_t  image;           // image index
342    uint32_t  cluster;         // cluster index / modulo x_size*y_size
343    uint32_t  ptr;             // byte pointer in input buffer
344    uint32_t  eoi_found;       // boolean : End-of-Image found
345    uint32_t  ff_found;        // boolean : 0xFF value found
346    uint32_t  bytes_count;     // mumber of bytes in compressed image
347       
348    // initialise image and cluster index, and bufin pointer
349    image   = 0;
350    cluster = 0;
351    ptr     = 0;
352
353    while( image < MAX_IMAGES )  // one compressed image per iteration
354    {
355        // initialise image specific variables
356        eoi_found   = 0;
357        ff_found    = 0;
358        bytes_count = 0; 
359       
360        // re-initialise the destination buffer for each image
361        bufio.mwmr = main_2_demux[cluster];
362
363        // scan bit stream until EOI found
364        // transfer one byte per iteration from input buffer to output bufio
365        while ( eoi_found == 0 )
366        {
367            // - tranfer 1024 bytes from file to input buffer when input buffer empty.
368            // - return to first byte in input file when EOF found,
369            //   to emulate an infinite stream of images.
370            if ( ptr == 0 )
371            {
372                uint32_t r = giet_fat_read( fd , bufin , 1024 );
373                if ( r < 1024 ) 
374                {
375                    giet_fat_lseek( fd , 0 , SEEK_SET );
376                    giet_fat_read( fd , bufin + r , 1024 - r );
377                }
378            }
379
380            // transfer one byte from input buffer to output bufio
381            mwmr_bufio_write_byte( &bufio , bufin[ptr] );
382
383            // analyse this byte to find EOI marker OxFFD8
384            // flush the output buffer when EOI found
385            if ( ff_found )  // possible End of Image
386            { 
387                ff_found = 0;
388                if ( bufin[ptr] == 0xD9 )   // End of Image found
389                {
390                    // exit current image
391                    eoi_found = 1;
392
393                    // flush output bufio
394                    mwmr_bufio_flush( &bufio );
395                }
396            }
397            else           // test if first byte of a marker
398            {
399                if ( bufin[ptr] == 0xFF )  ff_found = 1;
400            }       
401
402            // increment input buffer pointer modulo 1024
403            ptr++;
404            if ( ptr == 1024 ) ptr = 0;
405               
406            // increment bytes_count for current image
407            bytes_count++;
408
409        } // end while (eoi)
410 
411#if DEBUG_MAIN
412PRINTF("\nMAIN send image %d to cluster %d at cycle %d : %d bytes\n",
[748]413       image , cluster , giet_proctime() , bytes_count );
[741]414#endif
415        // increment image index
416        image++;
417
418        // increment cluster index modulo (x_size*y_size)   
419        cluster++; 
420        if (cluster == x_size * y_size) cluster = 0;   
421
422    } // end while on images
423
424    /////////////////////////////////////////////////////////////////////////////////////
[723]425    // wait all threads completion
[741]426    /////////////////////////////////////////////////////////////////////////////////////
[723]427
[736]428    for ( x = 0 ; x < x_size ; x++ )
[723]429    {
[736]430        for ( y = 0 ; y < y_size ; y++ )
431        {
432            index = x * y_size + y;
[723]433
[736]434            if ( giet_pthread_join( trdid_demux[index] , NULL ) )
[748]435            PRINTF("\n[MJPEG ERROR] calling giet_pthread_join() for demux[%d]\n", index );
[723]436
[736]437            if ( giet_pthread_join( trdid_vld[index] , NULL ) )
[748]438            PRINTF("\n[MJPEG ERROR] calling giet_pthread_join() for vld[%d]\n", index );
[723]439
[736]440            if ( giet_pthread_join( trdid_iqzz[index] , NULL ) )
[748]441            PRINTF("\n[MJPEG ERROR] calling giet_pthread_join() for iqzz[%d]\n", index );
[723]442
[736]443            if ( USE_DCT_COPROC == 0 )
444            {
445                if ( giet_pthread_join( trdid_idct[index] , NULL ) )
[748]446                PRINTF("\n[MJPEG ERROR] calling giet_pthread_join() for idct[%d]\n", index );
[736]447            }
448
449            if ( giet_pthread_join( trdid_libu[index] , NULL ) )
[748]450            PRINTF("\n[MJPEG ERROR] calling giet_pthread_join() for libu[%d]\n", index );
[736]451
452            if ( USE_DCT_COPROC )
453            {
454                uint32_t  cluster_xy  = (x<<4) + y;
455                uint32_t  coproc_type = 2;
456                giet_coproc_release( cluster_xy , coproc_type );
457            }
458        }
[723]459    }
460
[741]461    /////////////////////////////////////////////////////////////////////////////////////
462    // makes instrumentation
463    /////////////////////////////////////////////////////////////////////////////////////
[723]464
[748]465    // display on TTY
466    PRINTF("\n[MJPEG] Instumentation Results\n" );
[723]467    for ( image = 0 ; image < MAX_IMAGES ; image++ )
[748]468    PRINTF(" - Image %d : completed at cycle %d\n", image , date[image]);
[723]469
[748]470    // save on disk
[772]471    unsigned int fdout = giet_fat_open( "/home/mjpeg_instrumentation" , O_CREAT);
[748]472    if ( fdout < 0 ) 
473    PRINTF("\n[MJPEG ERROR] cannot open file /home/mjpeg_instrumentation\n");
474
475    int ret = giet_fat_lseek( fdout, 0 , SEEK_END );
476    if( ret < 0 )
477    PRINTF("\n[MJPEG ERROR] cannot seek file /home/mjpeg_instrumentation\n");
478
479    if( USE_DCT_COPROC )
480    {
481        giet_fat_fprintf( fdout, "\n*** stream %s / %d clusters / %d cores / DCT COPROC\n",
482                          file_pathname , x_size*y_size , nprocs );
483    }
484    else
485    {
486        giet_fat_fprintf( fdout, "stream %s / %d clusters / %d cores / NO DCT COPROC\n\n",
487                          file_pathname , x_size*y_size , nprocs );
488    }
489    for ( image = 0 ; image < MAX_IMAGES ; image++ )
490    {
491        giet_fat_fprintf( fdout, " - Image %d : completed at cycle %d\n", 
492                          image , date[image]);
493    } 
494
495    // completed
[723]496    giet_pthread_exit( "main completed" );
497   
498} // end main()
499
Note: See TracBrowser for help on using the repository browser.