Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

iec61883input.C

Go to the documentation of this file.
00001 #include "condition.h"
00002 #include "iec61883input.h"
00003 #include "mutex.h"
00004 #include "vframe.h"
00005 
00006 #include <errno.h>
00007 #include <fcntl.h>
00008 #include <sys/ioctl.h>
00009 #include <sys/mman.h>
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <unistd.h>
00013 
00014 #define INPUT_SAMPLES 131072
00015 #define BUFFER_TIMEOUT 500000
00016 
00017 
00018 IEC61883Input::IEC61883Input()
00019  : Thread(1, 1, 0)
00020 {
00021         frame = 0;
00022         handle = 0;
00023         fd = 0;
00024         buffer = 0;
00025         buffer_valid = 0;
00026         input_buffer = 0;
00027         done = 0;
00028         total_buffers = 0;
00029         current_inbuffer = 0;
00030         current_outbuffer = 0;
00031         buffer_size = 0;
00032         audio_buffer = 0;
00033         audio_samples = 0;
00034         video_lock = 0;
00035         audio_lock = 0;
00036         buffer_lock = 0;
00037         decoder = 0;
00038 }
00039 
00040 IEC61883Input::~IEC61883Input()
00041 {
00042 // Driver crashes if it isn't stopped before cancelling the thread.
00043 // May still crash during the cancel though.
00044 
00045         if(Thread::running())
00046         {
00047                 done = 1;
00048                 Thread::join();
00049         }
00050 
00051         if(buffer)
00052         {
00053                 for(int i = 0; i < total_buffers; i++)
00054                         delete [] buffer[i];
00055                 delete [] buffer;
00056                 delete [] buffer_valid;
00057         }
00058 
00059         if(input_buffer)
00060                 munmap(input_buffer, total_buffers * buffer_size);
00061 
00062         if(audio_buffer)
00063         {
00064                 delete [] audio_buffer;
00065         }
00066 
00067         if(decoder)
00068         {
00069                 dv_delete(decoder);
00070         }
00071 
00072         if(video_lock) delete video_lock;
00073         if(audio_lock) delete audio_lock;
00074         if(buffer_lock) delete buffer_lock;
00075         if(frame) iec61883_dv_fb_close(frame);
00076         if(handle) raw1394_destroy_handle(handle);
00077 }
00078 
00079 static int write_frame_static(unsigned char *data, int len, int complete, void *ptr)
00080 {
00081         IEC61883Input *input = (IEC61883Input*)ptr;
00082         return input->write_frame(data, len, complete);
00083 }
00084 
00085 
00086 
00087 int IEC61883Input::open(int port,
00088         int channel,
00089         int length,
00090         int channels,
00091         int samplerate,
00092         int bits,
00093         int w,
00094         int h)
00095 {
00096         this->port = port;
00097         this->channel = channel;
00098         this->length = length;
00099         this->channels = channels;
00100         this->samplerate = samplerate;
00101         this->bits = bits;
00102         this->w = w;
00103         this->h = h;
00104         is_pal = (h == 576);
00105         buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
00106         total_buffers = length;
00107 
00108 
00109 // Initialize grabbing
00110         if(!handle)
00111         {
00112                 handle = raw1394_new_handle_on_port(port);
00113                 if(handle)
00114                 {
00115                         frame = iec61883_dv_fb_init(handle, write_frame_static, (void *)this);
00116                         if(frame)
00117                         {
00118                                 if(!iec61883_dv_fb_start(frame, channel))
00119                                 {
00120                                         fd = raw1394_get_fd(handle);
00121                                 }
00122                         }
00123                 }
00124 
00125                 buffer = new char*[total_buffers];
00126                 buffer_valid = new int[total_buffers];
00127                 bzero(buffer_valid, sizeof(int) * total_buffers);
00128                 for(int i = 0; i < total_buffers; i++)
00129                 {
00130                         buffer[i] = new char[DV_PAL_SIZE];
00131                 }
00132 
00133 
00134                 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
00135 
00136                 audio_lock = new Condition(0, "IEC61883Input::audio_lock");
00137                 video_lock = new Condition(0, "IEC61883Input::video_lock");
00138                 buffer_lock = new Mutex("IEC61883Input::buffer_lock");
00139 
00140                 decoder = dv_new();
00141 
00142                 Thread::start();
00143         }
00144 
00145         if(!handle || !frame || !fd) return 1;
00146 
00147         return 0;
00148 }
00149 
00150 
00151 void IEC61883Input::run()
00152 {
00153         while(!done && handle)
00154         {
00155                 struct timeval tv;
00156                 fd_set rfds;
00157                 FD_ZERO(&rfds);
00158                 FD_SET(fd, &rfds);
00159                 tv.tv_sec = 0;
00160                 tv.tv_usec = 20000;
00161                 if(select(fd + 1, &rfds, 0, 0, &tv) > 0)
00162                 {
00163                         raw1394_loop_iterate(handle);
00164                 }
00165         }
00166 
00167 }
00168 
00169 
00170 
00171 
00172 
00173 
00174 
00175 
00176 int IEC61883Input::write_frame(unsigned char *data, int len, int complete)
00177 {
00178         if(!complete) printf("write_frame: incomplete frame received.\n");
00179 
00180         buffer_lock->lock("IEC61883Input write_frame 1");
00181 
00182 // Get a buffer to transfer to
00183         char *dst = 0;
00184         int is_overflow = 0;
00185         if(!buffer_valid[current_inbuffer])
00186                 dst = buffer[current_inbuffer];
00187         else
00188                 is_overflow = 1;
00189 
00190         char *src = (char*)(data);
00191 // static FILE *test = 0;
00192 // if(!test) test = fopen("/tmp/test", "w");
00193 // fwrite(src, buffer_size, 1, test);
00194 
00195 // Export the video
00196         if(dst)
00197         {
00198                 memcpy(dst, src, len);
00199                 buffer_valid[current_inbuffer] = 1;
00200                 video_lock->unlock();
00201         }
00202 
00203 
00204 // Extract the audio
00205         if(audio_samples < INPUT_SAMPLES - 2048)
00206         {
00207                 int audio_result = dv_read_audio(decoder, 
00208                         (unsigned char*)audio_buffer + 
00209                                 audio_samples * 2 * 2,
00210                         (unsigned char*)src,
00211                         len,
00212                         channels,
00213                         bits);
00214                 int real_freq = decoder->decoder->audio->frequency;
00215                 if (real_freq == 32000) 
00216                 {
00217 // do in-place _FAST_ && _SIMPLE_ upsampling to 48khz
00218 // i also think user should get a warning that his material is effectively 32khz
00219 // we take 16bit samples for both channels in one 32bit int
00220                         int *twosample = (int*) (audio_buffer + audio_samples * 2 * 2);
00221                         int from = audio_result - 1;
00222                         int new_result = audio_result * 48000 / real_freq;
00223                         for (int to = new_result - 1; to >=0; to--)
00224                         {       
00225                                 if ((to % 3) == 0 || (to % 3) == 1) from --;
00226                                 twosample[to] = twosample[from];
00227                         }
00228                         audio_result = new_result;
00229                 }
00230 
00231 
00232                 audio_samples += audio_result;
00233 
00234 // Export the audio
00235                 audio_lock->unlock();
00236         }
00237 
00238 // Advance buffer
00239         if(!is_overflow)
00240                 increment_counter(&current_inbuffer);
00241 
00242 
00243         buffer_lock->unlock();
00244         return 0;
00245 }
00246 
00247 
00248 
00249 
00250 
00251 
00252 
00253 void IEC61883Input::increment_counter(int *counter)
00254 {
00255         (*counter)++;
00256         if(*counter >= total_buffers) *counter = 0;
00257 }
00258 
00259 void IEC61883Input::decrement_counter(int *counter)
00260 {
00261         (*counter)--;
00262         if(*counter < 0) *counter = total_buffers - 1;
00263 }
00264 
00265 
00266 
00267 int IEC61883Input::read_video(VFrame *data)
00268 {
00269         int result = 0;
00270 
00271 // Take over buffer table
00272         buffer_lock->lock("IEC61883Input::read_video 1");
00273 // Wait for buffer with timeout
00274         while(!buffer_valid[current_outbuffer] && !result)
00275         {
00276                 buffer_lock->unlock();
00277                 result = video_lock->timed_lock(BUFFER_TIMEOUT, "IEC61883Input::read_video 2");
00278                 buffer_lock->lock("IEC61883Input::read_video 3");
00279         }
00280 
00281 // Copy frame
00282         if(buffer_valid[current_outbuffer])
00283         {
00284                 data->allocate_compressed_data(buffer_size);
00285                 data->set_compressed_size(buffer_size);
00286                 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
00287                 buffer_valid[current_outbuffer] = 0;
00288                 increment_counter(&current_outbuffer);
00289         }
00290 
00291         buffer_lock->unlock();
00292         return result;
00293 }
00294 
00295 
00296 
00297 
00298 int IEC61883Input::read_audio(char *data, int samples)
00299 {
00300         int result = 0;
00301         int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
00302         if(timeout < 500000) timeout = 500000;
00303 
00304 // Take over buffer table
00305         buffer_lock->lock("IEC61883Input::read_audio 1");
00306 // Wait for buffer with timeout
00307         while(audio_samples < samples && !result)
00308         {
00309                 buffer_lock->unlock();
00310                 result = audio_lock->timed_lock(timeout, "IEC61883Input::read_audio 2");
00311                 buffer_lock->lock("IEC61883Input::read_audio 3");
00312         }
00313 
00314         if(audio_samples >= samples)
00315         {
00316                 memcpy(data, audio_buffer, samples * bits * channels / 8);
00317                 memcpy(audio_buffer, 
00318                         audio_buffer + samples * bits * channels / 8,
00319                         (audio_samples - samples) * bits * channels / 8);
00320                 audio_samples -= samples;
00321         }
00322 
00323         buffer_lock->unlock();
00324         return result;
00325 }
00326 
00327 
00328 
00329 
00330 

Generated on Sun Jan 8 13:38:56 2006 for Cinelerra-svn by  doxygen 1.4.4