1313#include "TSDRPluginLoader.h"
1414#include "osdetect.h"
1515#include "threading.h"
16+ #include <math.h>
17+ #include "circbuff.h"
1618
1719#define MAX_ARR_SIZE (4000*4000)
20+ #define CIRC_BUFF_FRAME_COUNT (100)
1821
1922struct tsdr_context {
2023 tsdr_readasync_function cb ;
2124 float * buffer ;
2225 tsdr_lib_t * this ;
2326 int bufsize ;
2427 void * ctx ;
28+ CircBuff_t circbuf ;
2529 } typedef tsdr_context_t ;
2630
2731int tsdr_setsamplerate (tsdr_lib_t * tsdr , uint32_t rate ) {
@@ -30,6 +34,12 @@ int tsdr_setsamplerate(tsdr_lib_t * tsdr, uint32_t rate) {
3034 return TSDR_OK ;
3135}
3236
37+ int tsdr_getsamplerate (tsdr_lib_t * tsdr ) {
38+ pluginsource_t * plugin = (pluginsource_t * )(tsdr -> plugin );
39+ tsdr -> samplerate = plugin -> tsdrplugin_getsamplerate ();
40+ return TSDR_OK ;
41+ }
42+
3343int tsdr_setbasefreq (tsdr_lib_t * tsdr , uint32_t freq ) {
3444 pluginsource_t * plugin = (pluginsource_t * )(tsdr -> plugin );
3545 return plugin -> tsdrplugin_setbasefreq (freq );
@@ -44,6 +54,7 @@ int tsdr_stop(tsdr_lib_t * tsdr) {
4454
4555 mutex_wait ((mutex_t * ) tsdr -> mutex_sync_unload );
4656
57+ mutex_free ((mutex_t * ) tsdr -> mutex_sync_unload );
4758 free (tsdr -> mutex_sync_unload );
4859 return status ;
4960}
@@ -55,15 +66,38 @@ int tsdr_setgain(tsdr_lib_t * tsdr, float gain) {
5566 return plugin -> tsdrplugin_setgain (gain );
5667}
5768
69+ void videodecodingthread (void * ctx ) {
70+ tsdr_context_t * context = (tsdr_context_t * ) ctx ;
71+
72+ const uint32_t samplerate = context -> this -> samplerate ;
73+ const int height = context -> this -> height ;
74+ const int width = context -> this -> width ;
75+ const float fh = context -> this -> fh ;
76+ const float fv = context -> this -> fv ;
77+
78+ while (context -> this -> running ) {
79+ if (cb_rem_blocking (& context -> circbuf , context -> buffer , context -> bufsize ) == CB_OK ) {
80+ context -> cb (context -> buffer , context -> this -> width , context -> this -> height , context -> ctx );
81+ }
82+ }
83+
84+ mutex_signal ((mutex_t * ) context -> this -> mutex_video_stopped );
85+ }
86+
5887void process (float * buf , uint32_t len , void * ctx ) {
5988 tsdr_context_t * context = (tsdr_context_t * ) ctx ;
6089
61- const int length = (len < context -> bufsize ) ? (len ) : (context -> bufsize );
90+ int i = 0 ;
91+ int id ;
92+ const int size = len /2 ;
93+ for (id = 0 ; id < size ; id ++ ) {
94+ const float I = buf [i ++ ];
95+ const float Q = buf [i ++ ];
6296
63- int i ;
64- for ( i = 0 ; i < length ; i ++ ) context -> buffer [ i ] = buf [ i ];
97+ buf [ id ] = sqrtf ( I * I + Q * Q ) ;
98+ }
6599
66- context -> cb ( context -> buffer , context -> this -> width , context -> this -> height , context -> ctx );
100+ cb_add ( & context -> circbuf , buf , size );
67101}
68102
69103int tsdr_readasync (tsdr_lib_t * tsdr , const char * pluginfilepath , tsdr_readasync_function cb , void * ctx , const char * params ) {
@@ -82,6 +116,10 @@ int tsdr_readasync(tsdr_lib_t * tsdr, const char * pluginfilepath, tsdr_readasyn
82116
83117 tsdr -> mutex_sync_unload = malloc (sizeof (mutex_t ));
84118 mutex_init ((mutex_t * ) tsdr -> mutex_sync_unload );
119+
120+ tsdr -> mutex_video_stopped = malloc (sizeof (mutex_t ));
121+ mutex_init ((mutex_t * ) tsdr -> mutex_video_stopped );
122+
85123 pluginsource_t * plugin = (pluginsource_t * )(tsdr -> plugin );
86124
87125 const int width = tsdr -> width ;
@@ -97,40 +135,43 @@ int tsdr_readasync(tsdr_lib_t * tsdr, const char * pluginfilepath, tsdr_readasyn
97135 context -> bufsize = width * height ;
98136 context -> buffer = (float * ) malloc (sizeof (float ) * context -> bufsize );
99137 context -> ctx = ctx ;
138+ cb_init (& context -> circbuf , CIRC_BUFF_FRAME_COUNT * context -> bufsize );
100139
140+ tsdr_getsamplerate (tsdr );
141+
142+ thread_start (videodecodingthread , (void * ) context );
101143 status = plugin -> tsdrplugin_readasync (process , (void * ) context , params );
144+ tsdr -> running = 0 ;
145+
146+ mutex_wait ((mutex_t * ) tsdr -> mutex_video_stopped );
147+
148+ mutex_free ((mutex_t * ) tsdr -> mutex_video_stopped );
149+ free (tsdr -> mutex_video_stopped );
102150
103151 if (status != TSDR_OK ) return status ;
104152
105153 free (context -> buffer );
106154 free (context );
107155
156+ cb_free (& context -> circbuf );
108157 tsdrplug_close ((pluginsource_t * )(tsdr -> plugin ));
109158 free (tsdr -> plugin );
110159
111160 mutex_signal ((mutex_t * ) tsdr -> mutex_sync_unload );
112161
113- tsdr -> running = 0 ;
114162 return status ;
115163}
116164
117- int tsdr_setresolution (tsdr_lib_t * tsdr , int width , int height ) {
165+ int tsdr_setresolution (tsdr_lib_t * tsdr , int width , int height , float refreshrate ) {
166+ if (width < 0 || height < 0 || width * height > MAX_ARR_SIZE || refreshrate <= 0 )
167+ return TSDR_WRONG_WIDTHHEIGHT ;
168+
118169 tsdr -> width = width ;
119170 tsdr -> height = height ;
120-
121- if ( width < 0 || height < 0 || width * height > MAX_ARR_SIZE )
122- return TSDR_WRONG_WIDTHHEIGHT ;
171+ tsdr -> refreshrate = refreshrate ;
172+ tsdr -> fh = refreshrate / ( float ) width ;
173+ tsdr -> fv = refreshrate / ( float ) height ;
123174
124175 return TSDR_OK ;
125176}
126177
127- int tsdr_setvfreq (tsdr_lib_t * tsdr , float freq ) {
128- tsdr -> vf = freq ;
129- return TSDR_OK ;
130- }
131-
132- int tsdr_sethfreq (tsdr_lib_t * tsdr , float freq ) {
133- tsdr -> hf = freq ;
134- return TSDR_OK ;
135-
136- }
0 commit comments