Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / cinelerra / iec61883input.C
blob53260a0859bde82b4113687273fc030c5787600d
1 #include "condition.h"
2 #include "iec61883input.h"
3 #include "mutex.h"
4 #include "vframe.h"
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <sys/ioctl.h>
9 #include <sys/mman.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
14 #define INPUT_SAMPLES 131072
15 #define BUFFER_TIMEOUT 500000
18 IEC61883Input::IEC61883Input()
19  : Thread(1, 1, 0)
21         frame = 0;
22         handle = 0;
23         fd = 0;
24         buffer = 0;
25         buffer_valid = 0;
26         input_buffer = 0;
27         done = 0;
28         total_buffers = 0;
29         current_inbuffer = 0;
30         current_outbuffer = 0;
31         buffer_size = 0;
32         audio_buffer = 0;
33         audio_samples = 0;
34         video_lock = 0;
35         audio_lock = 0;
36         buffer_lock = 0;
37         decoder = 0;
40 IEC61883Input::~IEC61883Input()
42 // Driver crashes if it isn't stopped before cancelling the thread.
43 // May still crash during the cancel though.
45         if(Thread::running())
46         {
47                 done = 1;
48                 Thread::join();
49         }
51         if(buffer)
52         {
53                 for(int i = 0; i < total_buffers; i++)
54                         delete [] buffer[i];
55                 delete [] buffer;
56                 delete [] buffer_valid;
57         }
59         if(input_buffer)
60                 munmap(input_buffer, total_buffers * buffer_size);
62         if(audio_buffer)
63         {
64                 delete [] audio_buffer;
65         }
67         if(decoder)
68         {
69                 dv_delete(decoder);
70         }
72         if(video_lock) delete video_lock;
73         if(audio_lock) delete audio_lock;
74         if(buffer_lock) delete buffer_lock;
75         if(frame) iec61883_dv_fb_close(frame);
76         if(handle) raw1394_destroy_handle(handle);
79 static int write_frame_static(unsigned char *data, int len, int complete, void *ptr)
81         IEC61883Input *input = (IEC61883Input*)ptr;
82         return input->write_frame(data, len, complete);
87 int IEC61883Input::open(int port,
88         int channel,
89         int length,
90         int channels,
91         int samplerate,
92         int bits,
93         int w,
94         int h)
96         this->port = port;
97         this->channel = channel;
98         this->length = length;
99         this->channels = channels;
100         this->samplerate = samplerate;
101         this->bits = bits;
102         this->w = w;
103         this->h = h;
104         is_pal = (h == 576);
105         buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
106         total_buffers = length;
109 // Initialize grabbing
110         if(!handle)
111         {
112                 handle = raw1394_new_handle_on_port(port);
113                 if(handle)
114                 {
115                         frame = iec61883_dv_fb_init(handle, write_frame_static, (void *)this);
116                         if(frame)
117                         {
118                                 if(!iec61883_dv_fb_start(frame, channel))
119                                 {
120                                         fd = raw1394_get_fd(handle);
121                                 }
122                         }
123                 }
125                 buffer = new char*[total_buffers];
126                 buffer_valid = new int[total_buffers];
127                 bzero(buffer_valid, sizeof(int) * total_buffers);
128                 for(int i = 0; i < total_buffers; i++)
129                 {
130                         buffer[i] = new char[DV_PAL_SIZE];
131                 }
134                 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
136                 audio_lock = new Condition(0, "IEC61883Input::audio_lock");
137                 video_lock = new Condition(0, "IEC61883Input::video_lock");
138                 buffer_lock = new Mutex("IEC61883Input::buffer_lock");
140                 decoder = dv_new();
142                 Thread::start();
143         }
145         if(!handle || !frame || !fd) return 1;
147         return 0;
151 void IEC61883Input::run()
153         while(!done && handle)
154         {
155                 struct timeval tv;
156                 fd_set rfds;
157                 FD_ZERO(&rfds);
158                 FD_SET(fd, &rfds);
159                 tv.tv_sec = 0;
160                 tv.tv_usec = 20000;
161                 if(select(fd + 1, &rfds, 0, 0, &tv) > 0)
162                 {
163                         raw1394_loop_iterate(handle);
164                 }
165         }
176 int IEC61883Input::write_frame(unsigned char *data, int len, int complete)
178         if(!complete) printf("write_frame: incomplete frame received.\n");
180         buffer_lock->lock("IEC61883Input write_frame 1");
182 // Get a buffer to transfer to
183         char *dst = 0;
184         int is_overflow = 0;
185         if(!buffer_valid[current_inbuffer])
186                 dst = buffer[current_inbuffer];
187         else
188                 is_overflow = 1;
190         char *src = (char*)(data);
191 // static FILE *test = 0;
192 // if(!test) test = fopen("/tmp/test", "w");
193 // fwrite(src, buffer_size, 1, test);
195 // Export the video
196         if(dst)
197         {
198                 memcpy(dst, src, len);
199                 buffer_valid[current_inbuffer] = 1;
200                 video_lock->unlock();
201         }
204 // Extract the audio
205         if(audio_samples < INPUT_SAMPLES - 2048)
206         {
207                 int audio_result = dv_read_audio(decoder, 
208                         (unsigned char*)audio_buffer + 
209                                 audio_samples * 2 * 2,
210                         (unsigned char*)src,
211                         len,
212                         channels,
213                         bits);
214                 int real_freq = decoder->decoder->audio->frequency;
215                 if (real_freq == 32000) 
216                 {
217 // do in-place _FAST_ && _SIMPLE_ upsampling to 48khz
218 // i also think user should get a warning that his material is effectively 32khz
219 // we take 16bit samples for both channels in one 32bit int
220                         int *twosample = (int*) (audio_buffer + audio_samples * 2 * 2);
221                         int from = audio_result - 1;
222                         int new_result = audio_result * 48000 / real_freq;
223                         for (int to = new_result - 1; to >=0; to--)
224                         {       
225                                 if ((to % 3) == 0 || (to % 3) == 1) from --;
226                                 twosample[to] = twosample[from];
227                         }
228                         audio_result = new_result;
229                 }
232                 audio_samples += audio_result;
234 // Export the audio
235                 audio_lock->unlock();
236         }
238 // Advance buffer
239         if(!is_overflow)
240                 increment_counter(&current_inbuffer);
243         buffer_lock->unlock();
244         return 0;
253 void IEC61883Input::increment_counter(int *counter)
255         (*counter)++;
256         if(*counter >= total_buffers) *counter = 0;
259 void IEC61883Input::decrement_counter(int *counter)
261         (*counter)--;
262         if(*counter < 0) *counter = total_buffers - 1;
267 int IEC61883Input::read_video(VFrame *data)
269         int result = 0;
271 // Take over buffer table
272         buffer_lock->lock("IEC61883Input::read_video 1");
273 // Wait for buffer with timeout
274         while(!buffer_valid[current_outbuffer] && !result)
275         {
276                 buffer_lock->unlock();
277                 result = video_lock->timed_lock(BUFFER_TIMEOUT, "IEC61883Input::read_video 2");
278                 buffer_lock->lock("IEC61883Input::read_video 3");
279         }
281 // Copy frame
282         if(buffer_valid[current_outbuffer])
283         {
284                 data->allocate_compressed_data(buffer_size);
285                 data->set_compressed_size(buffer_size);
286                 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
287                 buffer_valid[current_outbuffer] = 0;
288                 increment_counter(&current_outbuffer);
289         }
291         buffer_lock->unlock();
292         return result;
298 int IEC61883Input::read_audio(char *data, int samples)
300         int result = 0;
301         int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
302         if(timeout < 500000) timeout = 500000;
304 // Take over buffer table
305         buffer_lock->lock("IEC61883Input::read_audio 1");
306 // Wait for buffer with timeout
307         while(audio_samples < samples && !result)
308         {
309                 buffer_lock->unlock();
310                 result = audio_lock->timed_lock(timeout, "IEC61883Input::read_audio 2");
311                 buffer_lock->lock("IEC61883Input::read_audio 3");
312         }
314         if(audio_samples >= samples)
315         {
316                 memcpy(data, audio_buffer, samples * bits * channels / 8);
317                 memcpy(audio_buffer, 
318                         audio_buffer + samples * bits * channels / 8,
319                         (audio_samples - samples) * bits * channels / 8);
320                 audio_samples -= samples;
321         }
323         buffer_lock->unlock();
324         return result;