| 1 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 2 | // File   : tg.c | 
|---|
| 3 | // Date   : octobre 2015 | 
|---|
| 4 | // author : Alain Greiner | 
|---|
| 5 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 6 | // This file define the code of the TG (trafic generator) thread for MJPEG application. | 
|---|
| 7 | // It transfer the byte stream from the file identified by the fd argument to a 1024 | 
|---|
| 8 | // bytes local buffer. It analyses the byte stream to detect the End_of_Image markers. | 
|---|
| 9 | // All the bytes corresponding to a single image (from the first byte, to the EOI marker | 
|---|
| 10 | // included) is written in the TG_2_DEMUX[index] channel of a single cluster, | 
|---|
| 11 | // in increasing order in of the cluster index. | 
|---|
| 12 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 13 |  | 
|---|
| 14 | #include <stdio.h> | 
|---|
| 15 | #include <mwmr_channel.h> | 
|---|
| 16 | #include <stdint.h> | 
|---|
| 17 | #include "mjpeg.h" | 
|---|
| 18 |  | 
|---|
| 19 | // macro to use a shared TTY | 
|---|
| 20 | #define PRINTF(...)    lock_acquire( &tty_lock ); \ | 
|---|
| 21 | giet_tty_printf(__VA_ARGS__);  \ | 
|---|
| 22 | lock_release( &tty_lock ); | 
|---|
| 23 |  | 
|---|
| 24 | ////////////////////////////////////// | 
|---|
| 25 | __attribute__ ((constructor)) void tg() | 
|---|
| 26 | /////////////////////////////////////// | 
|---|
| 27 | { | 
|---|
| 28 | // get platform parameters | 
|---|
| 29 | uint32_t  x_size; | 
|---|
| 30 | uint32_t  y_size; | 
|---|
| 31 | uint32_t  nprocs; | 
|---|
| 32 | giet_procs_number( &x_size , &y_size , &nprocs ); | 
|---|
| 33 |  | 
|---|
| 34 | // get processor coordinates | 
|---|
| 35 | uint32_t x , y , p; | 
|---|
| 36 | giet_proc_xyp( &x , &y , &p ); | 
|---|
| 37 |  | 
|---|
| 38 | // private TTY allocation | 
|---|
| 39 | //  giet_tty_alloc( 0 ); | 
|---|
| 40 |  | 
|---|
| 41 | PRINTF("\n[MJPEG] thread TG starts on P[%d,%d,%d]\n", x , y , p ) | 
|---|
| 42 |  | 
|---|
| 43 | // allocate input buffer : 1024 bytes | 
|---|
| 44 | uint8_t        bufin[1024]; | 
|---|
| 45 |  | 
|---|
| 46 | // allocate output bufio to access MWMR channel : 64 bytes == 16 words | 
|---|
| 47 | mwmr_bufio_t  bufio; | 
|---|
| 48 | uint8_t       bufout[64]; | 
|---|
| 49 | mwmr_bufio_init( &bufio , bufout , 64 , 0 , tg_2_demux[0] ); | 
|---|
| 50 |  | 
|---|
| 51 | uint32_t  image;           // image index | 
|---|
| 52 | uint32_t  cluster;         // cluster index / modulo x_size*y_size | 
|---|
| 53 | uint32_t  ptr;             // byte pointer in input buffer | 
|---|
| 54 | uint32_t  eoi_found;       // boolean : End-of-Image found | 
|---|
| 55 | uint32_t  ff_found;        // boolean : 0xFF value found | 
|---|
| 56 | uint32_t  bytes_count;     // mumber of bytes in compressed image | 
|---|
| 57 |  | 
|---|
| 58 | // initialise image and cluster index, and bufin pointer | 
|---|
| 59 | image   = 0; | 
|---|
| 60 | cluster = 0; | 
|---|
| 61 | ptr     = 0; | 
|---|
| 62 |  | 
|---|
| 63 | while( image < MAX_IMAGES )  // one compressed image per iteration | 
|---|
| 64 | { | 
|---|
| 65 | // initialise image specific variables | 
|---|
| 66 | eoi_found   = 0; | 
|---|
| 67 | ff_found    = 0; | 
|---|
| 68 | bytes_count = 0; | 
|---|
| 69 |  | 
|---|
| 70 | // re-initialise the destination buffer for each image | 
|---|
| 71 | bufio.mwmr = tg_2_demux[cluster]; | 
|---|
| 72 |  | 
|---|
| 73 | // scan bit stream until EOI found | 
|---|
| 74 | // transfer one byte per iteration from input buffer to output bufio | 
|---|
| 75 | while ( eoi_found == 0 ) | 
|---|
| 76 | { | 
|---|
| 77 | // - tranfer 1024 bytes from file to input buffer when input buffer empty. | 
|---|
| 78 | // - return to first byte in input file when EOF found, | 
|---|
| 79 | //   to emulate an infinite stream of images. | 
|---|
| 80 | if ( ptr == 0 ) | 
|---|
| 81 | { | 
|---|
| 82 | uint32_t r = giet_fat_read( fd , bufin , 1024 ); | 
|---|
| 83 | if ( r < 1024 ) | 
|---|
| 84 | { | 
|---|
| 85 | giet_fat_lseek( fd , 0 , SEEK_SET ); | 
|---|
| 86 | giet_fat_read( fd , bufin + r , 1024 - r ); | 
|---|
| 87 | } | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | // transfer one byte from input buffer to output bufio | 
|---|
| 91 | mwmr_bufio_write_byte( &bufio , bufin[ptr] ); | 
|---|
| 92 |  | 
|---|
| 93 | // analyse this byte to find EOI marker OxFFD8 | 
|---|
| 94 | // flush the output buffer when EOI found | 
|---|
| 95 | if ( ff_found )  // possible End of Image | 
|---|
| 96 | { | 
|---|
| 97 | ff_found = 0; | 
|---|
| 98 | if ( bufin[ptr] == 0xD9 )   // End of Image found | 
|---|
| 99 | { | 
|---|
| 100 | // exit current image | 
|---|
| 101 | eoi_found = 1; | 
|---|
| 102 |  | 
|---|
| 103 | // flush output bufio | 
|---|
| 104 | mwmr_bufio_flush( &bufio ); | 
|---|
| 105 | } | 
|---|
| 106 | } | 
|---|
| 107 | else           // test if first byte of a marker | 
|---|
| 108 | { | 
|---|
| 109 | if ( bufin[ptr] == 0xFF )  ff_found = 1; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | // increment input buffer pointer modulo 1024 | 
|---|
| 113 | ptr++; | 
|---|
| 114 | if ( ptr == 1024 ) ptr = 0; | 
|---|
| 115 |  | 
|---|
| 116 | // increment bytes_count for current image | 
|---|
| 117 | bytes_count++; | 
|---|
| 118 |  | 
|---|
| 119 | } // end while (eoi) | 
|---|
| 120 |  | 
|---|
| 121 | #if DEBUG_TG | 
|---|
| 122 | PRINTF("\nTG send image %d to cluster %d at cycle %d : %d bytes\n", | 
|---|
| 123 | image , cluster , giet_proctime() , bytes_count ) | 
|---|
| 124 | #endif | 
|---|
| 125 | // increment image index | 
|---|
| 126 | image++; | 
|---|
| 127 |  | 
|---|
| 128 | // increment cluster index modulo (x_size*y_size) | 
|---|
| 129 | cluster++; | 
|---|
| 130 | if (cluster == x_size * y_size) cluster = 0; | 
|---|
| 131 |  | 
|---|
| 132 | } // end while on images | 
|---|
| 133 |  | 
|---|
| 134 | giet_pthread_exit("TG completed"); | 
|---|
| 135 |  | 
|---|
| 136 | }  // end tg() | 
|---|
| 137 |  | 
|---|
| 138 |  | 
|---|