[723] | 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 | |
---|
[736] | 41 | PRINTF("\n[MJPEG] thread TG starts on P[%d,%d,%d] / trdid = %x\n", |
---|
| 42 | x , y , p , (uint32_t)trdid_tg ) |
---|
[723] | 43 | |
---|
| 44 | // allocate input buffer : 1024 bytes |
---|
| 45 | uint8_t bufin[1024]; |
---|
| 46 | |
---|
| 47 | // allocate output bufio to access MWMR channel : 64 bytes == 16 words |
---|
| 48 | mwmr_bufio_t bufio; |
---|
| 49 | uint8_t bufout[64]; |
---|
| 50 | mwmr_bufio_init( &bufio , bufout , 64 , 0 , tg_2_demux[0] ); |
---|
| 51 | |
---|
| 52 | uint32_t image; // image index |
---|
| 53 | uint32_t cluster; // cluster index / modulo x_size*y_size |
---|
| 54 | uint32_t ptr; // byte pointer in input buffer |
---|
| 55 | uint32_t eoi_found; // boolean : End-of-Image found |
---|
| 56 | uint32_t ff_found; // boolean : 0xFF value found |
---|
| 57 | uint32_t bytes_count; // mumber of bytes in compressed image |
---|
| 58 | |
---|
| 59 | // initialise image and cluster index, and bufin pointer |
---|
| 60 | image = 0; |
---|
| 61 | cluster = 0; |
---|
| 62 | ptr = 0; |
---|
| 63 | |
---|
| 64 | while( image < MAX_IMAGES ) // one compressed image per iteration |
---|
| 65 | { |
---|
| 66 | // initialise image specific variables |
---|
| 67 | eoi_found = 0; |
---|
| 68 | ff_found = 0; |
---|
| 69 | bytes_count = 0; |
---|
| 70 | |
---|
| 71 | // re-initialise the destination buffer for each image |
---|
| 72 | bufio.mwmr = tg_2_demux[cluster]; |
---|
| 73 | |
---|
| 74 | // scan bit stream until EOI found |
---|
| 75 | // transfer one byte per iteration from input buffer to output bufio |
---|
| 76 | while ( eoi_found == 0 ) |
---|
| 77 | { |
---|
| 78 | // - tranfer 1024 bytes from file to input buffer when input buffer empty. |
---|
| 79 | // - return to first byte in input file when EOF found, |
---|
| 80 | // to emulate an infinite stream of images. |
---|
| 81 | if ( ptr == 0 ) |
---|
| 82 | { |
---|
| 83 | uint32_t r = giet_fat_read( fd , bufin , 1024 ); |
---|
| 84 | if ( r < 1024 ) |
---|
| 85 | { |
---|
| 86 | giet_fat_lseek( fd , 0 , SEEK_SET ); |
---|
| 87 | giet_fat_read( fd , bufin + r , 1024 - r ); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | // transfer one byte from input buffer to output bufio |
---|
| 92 | mwmr_bufio_write_byte( &bufio , bufin[ptr] ); |
---|
| 93 | |
---|
| 94 | // analyse this byte to find EOI marker OxFFD8 |
---|
| 95 | // flush the output buffer when EOI found |
---|
| 96 | if ( ff_found ) // possible End of Image |
---|
| 97 | { |
---|
| 98 | ff_found = 0; |
---|
| 99 | if ( bufin[ptr] == 0xD9 ) // End of Image found |
---|
| 100 | { |
---|
| 101 | // exit current image |
---|
| 102 | eoi_found = 1; |
---|
| 103 | |
---|
| 104 | // flush output bufio |
---|
| 105 | mwmr_bufio_flush( &bufio ); |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | else // test if first byte of a marker |
---|
| 109 | { |
---|
| 110 | if ( bufin[ptr] == 0xFF ) ff_found = 1; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | // increment input buffer pointer modulo 1024 |
---|
| 114 | ptr++; |
---|
| 115 | if ( ptr == 1024 ) ptr = 0; |
---|
| 116 | |
---|
| 117 | // increment bytes_count for current image |
---|
| 118 | bytes_count++; |
---|
| 119 | |
---|
| 120 | } // end while (eoi) |
---|
| 121 | |
---|
| 122 | #if DEBUG_TG |
---|
| 123 | PRINTF("\nTG send image %d to cluster %d at cycle %d : %d bytes\n", |
---|
| 124 | image , cluster , giet_proctime() , bytes_count ) |
---|
| 125 | #endif |
---|
| 126 | // increment image index |
---|
| 127 | image++; |
---|
| 128 | |
---|
| 129 | // increment cluster index modulo (x_size*y_size) |
---|
| 130 | cluster++; |
---|
| 131 | if (cluster == x_size * y_size) cluster = 0; |
---|
| 132 | |
---|
| 133 | } // end while on images |
---|
| 134 | |
---|
| 135 | giet_pthread_exit("TG completed"); |
---|
| 136 | |
---|
| 137 | } // end tg() |
---|
| 138 | |
---|
| 139 | |
---|