Index: soft/giet_vm/applications/mjpeg/mjpeg.c
===================================================================
--- soft/giet_vm/applications/mjpeg/mjpeg.c	(revision 740)
+++ soft/giet_vm/applications/mjpeg/mjpeg.c	(revision 741)
@@ -10,7 +10,8 @@
 // communications between threads uses MWMR channels,.
 // It uses the chained buffer DMA component to display the images on the graphic display.
-// It contains 6 types of threads (plus the "main" thread, that makes initialisation)
+// It contains 5 types of threads, plus the "main" thread, that makes initialisation,
+// dispatch the byte stream to the various pipelines, and makes instrumentation.
 // and 7 types of MWMR communication channels:
-// - the TG thread is only mapped in cluster[0,0], but all other threads
+// - the main thread is only mapped in cluster[0,0], but all other threads
 //   (DEMUX, VLD, IQZZ, IDCT, LIBU) are replicated in all clusters.
 // - all MWMR channels are replicated in all clusters.
@@ -41,5 +42,5 @@
 
 // arrays of pointers on MWMR channels
-mwmr_channel_t*  tg_2_demux[256];         // one per cluster
+mwmr_channel_t*  main_2_demux[256];       // one per cluster
 mwmr_channel_t*  demux_2_vld_data[256];   // one per cluster
 mwmr_channel_t*  demux_2_vld_huff[256];   // one per cluster
@@ -50,5 +51,4 @@
 
 // thread trdid ( for pthread_create() and pthread_join() )
-pthread_t   trdid_tg; 
 pthread_t   trdid_demux[256];             // one per cluster
 pthread_t   trdid_vld[256];               // one per cluster
@@ -74,5 +74,4 @@
 ////////////////////////////////////////////////
 
-extern void tg( );
 extern void demux( uint32_t index );
 extern void vld( uint32_t index );
@@ -122,4 +121,5 @@
     uint32_t      image_width;
     uint32_t      image_height;
+    uint32_t      fd;                   // file descriptor
 
     if ( INTERACTIVE_MODE )
@@ -165,5 +165,5 @@
 
     // open file containing the MJPEG bit stream
-    int fd = giet_fat_open( file_pathname , 0 );
+    fd = giet_fat_open( file_pathname , 0 );
 
     giet_pthread_assert( (fd >= 0),
@@ -190,7 +190,7 @@
 
             // allocate MWMR channels in cluster[x][y] 
-            tg_2_demux[index]       = remote_malloc( sizeof( mwmr_channel_t ) , x , y );
-            buffer                  = remote_malloc( 4 * TG_2_DEMUX_DEPTH , x , y );
-            mwmr_init( tg_2_demux[index] , buffer , 1 , TG_2_DEMUX_DEPTH );
+            main_2_demux[index]     = remote_malloc( sizeof( mwmr_channel_t ) , x , y );
+            buffer                  = remote_malloc( 4 * MAIN_2_DEMUX_DEPTH , x , y );
+            mwmr_init( main_2_demux[index] , buffer , 1 , MAIN_2_DEMUX_DEPTH );
 
             demux_2_vld_data[index] = remote_malloc( sizeof( mwmr_channel_t ) , x , y );
@@ -228,34 +228,31 @@
     giet_fbf_cma_start();
 
-    mwmr_channel_t* ptr;
+    mwmr_channel_t* pc;
 
     for ( n = 0 ; n < x_size*y_size ; n++ )
     {
-        ptr = tg_2_demux[n];
-        PRINTF(" - tg_2_demux[%d]  = %x / &lock = %x / &buf = %x / size = %d\n",
-               n, ptr, (uint32_t)&ptr->lock, (uint32_t)ptr->data, ptr->depth<<2 ) 
-
-        ptr = demux_2_vld_data[n];
+        pc = main_2_demux[n];
+        PRINTF(" - main_2_demux[%d]  = %x / &lock = %x / &buf = %x / size = %d\n",
+               n, pc, (uint32_t)&pc->lock, (uint32_t)pc->data, pc->depth<<2 ) 
+
+        pc = demux_2_vld_data[n];
         PRINTF(" - demux_2_vld[%d] = %x / &lock = %x / &buf = %x / size = %d\n",
-               n, ptr, (uint32_t)&ptr->lock, (uint32_t)ptr->data, ptr->depth<<2 ) 
-
-        ptr = vld_2_iqzz[n];
+               n, pc, (uint32_t)&pc->lock, (uint32_t)pc->data, pc->depth<<2 ) 
+
+        pc = vld_2_iqzz[n];
         PRINTF(" - vld_2_iqzz[%d]  = %x / &lock = %x / &buf = %x / size = %d\n",
-               n, ptr, (uint32_t)&ptr->lock, (uint32_t)ptr->data, ptr->depth<<2 ) 
-
-        ptr = iqzz_2_idct[n];
+               n, pc, (uint32_t)&pc->lock, (uint32_t)pc->data, pc->depth<<2 ) 
+
+        pc = iqzz_2_idct[n];
         PRINTF(" - iqzz_2_idct[%d] = %x / &lock = %x / &buf = %x / size = %d\n",
-               n, ptr, (uint32_t)&ptr->lock, (uint32_t)ptr->data, ptr->depth<<2 ) 
-
-        ptr = idct_2_libu[n];
+               n, pc, (uint32_t)&pc->lock, (uint32_t)pc->data, pc->depth<<2 ) 
+
+        pc = idct_2_libu[n];
         PRINTF(" - idct_2_libu[%d] = %x / &lock = %x / &buf = %x / size = %d\n",
-               n, ptr, (uint32_t)&ptr->lock, (uint32_t)ptr->data, ptr->depth<<2 ) 
+               n, pc, (uint32_t)&pc->lock, (uint32_t)pc->data, pc->depth<<2 ) 
     }
 
     // launch all threads : precise mapping is defined in the mjpeg.py file
     uint32_t index;
-
-    if ( giet_pthread_create( &trdid_tg, NULL, &tg , NULL ) )
-    giet_pthread_exit( "error launching thread tg\n");
 
     for ( x = 0 ; x < x_size ; x++ )
@@ -321,8 +318,107 @@
     }
 
+    /////////////////////////////////////////////////////////////////////////////////////
+    // dispatch the byte stream to the demux threads, one compressed image per cluster.
+    // It transfer the stream from the file identified by the fd argument to a 1024 
+    // bytes local buffer. It analyses the stream to detect the End_of_Image markers.
+    // All the bytes corresponding to a single image from the first byte, to the EOI 
+    // marker included, are written in the main_2_demux[index] channel, in increasing 
+    // order of the cluster index.
+    /////////////////////////////////////////////////////////////////////////////////////
+
+    // allocate input buffer : 1024 bytes
+    uint8_t        bufin[1024];
+
+    // allocate output bufio to access output MWMR channels : 64 bytes == 16 words
+    mwmr_bufio_t  bufio;
+    uint8_t       bufout[64]; 
+    mwmr_bufio_init( &bufio , bufout , 64 , 0 , main_2_demux[0] );
+
+    uint32_t  image;           // image index
+    uint32_t  cluster;         // cluster index / modulo x_size*y_size
+    uint32_t  ptr;             // byte pointer in input buffer
+    uint32_t  eoi_found;       // boolean : End-of-Image found
+    uint32_t  ff_found;        // boolean : 0xFF value found
+    uint32_t  bytes_count;     // mumber of bytes in compressed image
+       
+    // initialise image and cluster index, and bufin pointer
+    image   = 0;
+    cluster = 0;
+    ptr     = 0;
+
+    while( image < MAX_IMAGES )  // one compressed image per iteration
+    {
+        // initialise image specific variables
+        eoi_found   = 0;
+        ff_found    = 0;
+        bytes_count = 0; 
+       
+        // re-initialise the destination buffer for each image
+        bufio.mwmr = main_2_demux[cluster];
+
+        // scan bit stream until EOI found
+        // transfer one byte per iteration from input buffer to output bufio
+        while ( eoi_found == 0 )
+        {
+            // - tranfer 1024 bytes from file to input buffer when input buffer empty.
+            // - return to first byte in input file when EOF found, 
+            //   to emulate an infinite stream of images.
+            if ( ptr == 0 )
+            {
+                uint32_t r = giet_fat_read( fd , bufin , 1024 );
+                if ( r < 1024 ) 
+                {
+                    giet_fat_lseek( fd , 0 , SEEK_SET );
+                    giet_fat_read( fd , bufin + r , 1024 - r );
+                }
+            }
+
+            // transfer one byte from input buffer to output bufio
+            mwmr_bufio_write_byte( &bufio , bufin[ptr] );
+
+            // analyse this byte to find EOI marker OxFFD8
+            // flush the output buffer when EOI found
+            if ( ff_found )  // possible End of Image
+            { 
+                ff_found = 0;
+                if ( bufin[ptr] == 0xD9 )   // End of Image found
+                {
+                    // exit current image
+                    eoi_found = 1;
+
+                    // flush output bufio
+                    mwmr_bufio_flush( &bufio );
+                }
+            }
+            else           // test if first byte of a marker
+            {
+                if ( bufin[ptr] == 0xFF )  ff_found = 1;
+            }        
+
+            // increment input buffer pointer modulo 1024
+            ptr++;
+            if ( ptr == 1024 ) ptr = 0;
+                
+            // increment bytes_count for current image
+            bytes_count++;
+
+        } // end while (eoi)
+ 
+#if DEBUG_MAIN
+PRINTF("\nMAIN send image %d to cluster %d at cycle %d : %d bytes\n",
+       image , cluster , giet_proctime() , bytes_count )
+#endif
+        // increment image index
+        image++;
+
+        // increment cluster index modulo (x_size*y_size)   
+        cluster++; 
+        if (cluster == x_size * y_size) cluster = 0;    
+
+    } // end while on images
+
+    /////////////////////////////////////////////////////////////////////////////////////
     // wait all threads completion
-
-    if ( giet_pthread_join( trdid_tg , NULL ) )
-    { PRINTF("\n[MJPEG ERROR] calling giet_pthread_join() for tg\n" ) }
+    /////////////////////////////////////////////////////////////////////////////////////
 
     for ( x = 0 ; x < x_size ; x++ )
@@ -359,7 +455,8 @@
     }
 
-    // instrumentation
-
-    uint32_t image;
+    /////////////////////////////////////////////////////////////////////////////////////
+    // makes instrumentation
+    /////////////////////////////////////////////////////////////////////////////////////
+
     PRINTF("\n[MJPEG] Instumentation Results\n" )
     for ( image = 0 ; image < MAX_IMAGES ; image++ )
