centos build workarounds
[goodguy/history.git] / cinelerra-5.0 / cinelerra / iec61883input.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  */
21
22 #include "condition.h"
23 #include "iec61883input.h"
24 #include "language.h"
25 #include "mutex.h"
26 #include "vframe.h"
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <sys/mman.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #define INPUT_SAMPLES 131072
37 #define BUFFER_TIMEOUT 500000
38
39
40 IEC61883Input::IEC61883Input()
41  : Thread(1, 1, 0)
42 {
43         frame = 0;
44         handle = 0;
45         fd = 0;
46         buffer = 0;
47         buffer_valid = 0;
48         input_buffer = 0;
49         done = 0;
50         total_buffers = 0;
51         current_inbuffer = 0;
52         current_outbuffer = 0;
53         buffer_size = 0;
54         audio_buffer = 0;
55         audio_samples = 0;
56         video_lock = 0;
57         audio_lock = 0;
58         buffer_lock = 0;
59         decoder = 0;
60 }
61
62 IEC61883Input::~IEC61883Input()
63 {
64 // Driver crashes if it isn't stopped before cancelling the thread.
65 // May still crash during the cancel though.
66
67         if(Thread::running())
68         {
69                 done = 1;
70                 Thread::join();
71         }
72
73         if(buffer)
74         {
75                 for(int i = 0; i < total_buffers; i++)
76                         delete [] buffer[i];
77                 delete [] buffer;
78                 delete [] buffer_valid;
79         }
80
81         if(input_buffer)
82                 munmap(input_buffer, total_buffers * buffer_size);
83
84         if(audio_buffer)
85         {
86                 delete [] audio_buffer;
87         }
88
89         if(decoder)
90         {
91                 dv_delete(decoder);
92         }
93
94         if(video_lock) delete video_lock;
95         if(audio_lock) delete audio_lock;
96         if(buffer_lock) delete buffer_lock;
97         if(frame) iec61883_dv_fb_close(frame);
98         if(handle) raw1394_destroy_handle(handle);
99 }
100
101 static int write_frame_static(unsigned char *data, int len, int complete, void *ptr)
102 {
103         IEC61883Input *input = (IEC61883Input*)ptr;
104         return input->write_frame(data, len, complete);
105 }
106
107
108
109 int IEC61883Input::open(int port,
110         int channel,
111         int length,
112         int channels,
113         int samplerate,
114         int bits,
115         int w,
116         int h)
117 {
118         this->port = port;
119         this->channel = channel;
120         this->length = length;
121         this->channels = channels;
122         this->samplerate = samplerate;
123         this->bits = bits;
124         this->w = w;
125         this->h = h;
126         is_pal = (h == 576);
127         buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
128         total_buffers = length;
129
130
131 // Initialize grabbing
132         if(!handle)
133         {
134                 handle = raw1394_new_handle_on_port(port);
135                 if(handle)
136                 {
137                         frame = iec61883_dv_fb_init(handle, write_frame_static, (void *)this);
138                         if(frame)
139                         {
140                                 if(!iec61883_dv_fb_start(frame, channel))
141                                 {
142                                         fd = raw1394_get_fd(handle);
143                                 }
144                         }
145                 }
146
147                 buffer = new char*[total_buffers];
148                 buffer_valid = new int[total_buffers];
149                 bzero(buffer_valid, sizeof(int) * total_buffers);
150                 for(int i = 0; i < total_buffers; i++)
151                 {
152                         buffer[i] = new char[DV_PAL_SIZE];
153                 }
154
155
156                 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
157
158                 audio_lock = new Condition(0, "IEC61883Input::audio_lock");
159                 video_lock = new Condition(0, "IEC61883Input::video_lock");
160                 buffer_lock = new Mutex("IEC61883Input::buffer_lock");
161
162                 decoder = dv_new();
163
164                 Thread::start();
165         }
166
167         if(!handle || !frame || !fd) return 1;
168
169         return 0;
170 }
171
172
173 void IEC61883Input::run()
174 {
175         while(!done && handle)
176         {
177                 struct timeval tv;
178                 fd_set rfds;
179                 FD_ZERO(&rfds);
180                 FD_SET(fd, &rfds);
181                 tv.tv_sec = 0;
182                 tv.tv_usec = 20000;
183                 if(select(fd + 1, &rfds, 0, 0, &tv) > 0)
184                 {
185                         raw1394_loop_iterate(handle);
186                 }
187         }
188
189 }
190
191
192
193
194
195
196
197
198 int IEC61883Input::write_frame(unsigned char *data, int len, int complete)
199 {
200         if(!complete) printf(_("write_frame: incomplete frame received.\n"));
201
202         buffer_lock->lock("IEC61883Input write_frame 1");
203
204 // Get a buffer to transfer to
205         char *dst = 0;
206         int is_overflow = 0;
207         if(!buffer_valid[current_inbuffer])
208                 dst = buffer[current_inbuffer];
209         else
210                 is_overflow = 1;
211
212         char *src = (char*)(data);
213 // static FILE *test = 0;
214 // if(!test) test = fopen("/tmp/test", "w");
215 // fwrite(src, buffer_size, 1, test);
216
217 // Export the video
218         if(dst)
219         {
220                 memcpy(dst, src, len);
221                 buffer_valid[current_inbuffer] = 1;
222                 video_lock->unlock();
223         }
224
225
226 // Extract the audio
227         if(audio_samples < INPUT_SAMPLES - 2048)
228         {
229                 int audio_result = dv_read_audio(decoder, 
230                         (unsigned char*)audio_buffer + 
231                                 audio_samples * 2 * 2,
232                         (unsigned char*)src,
233                         len,
234                         channels,
235                         bits);
236                 int real_freq = decoder->decoder->audio->frequency;
237                 if (real_freq == 32000) 
238                 {
239 // do in-place _FAST_ && _SIMPLE_ upsampling to 48khz
240 // i also think user should get a warning that his material is effectively 32khz
241 // we take 16bit samples for both channels in one 32bit int
242                         int *twosample = (int*) (audio_buffer + audio_samples * 2 * 2);
243                         int from = audio_result - 1;
244                         int new_result = audio_result * 48000 / real_freq;
245                         for (int to = new_result - 1; to >=0; to--)
246                         {       
247                                 if ((to % 3) == 0 || (to % 3) == 1) from --;
248                                 twosample[to] = twosample[from];
249                         }
250                         audio_result = new_result;
251                 }
252
253
254                 audio_samples += audio_result;
255
256 // Export the audio
257                 audio_lock->unlock();
258         }
259
260 // Advance buffer
261         if(!is_overflow)
262                 increment_counter(&current_inbuffer);
263
264
265         buffer_lock->unlock();
266         return 0;
267 }
268
269
270
271
272
273
274
275 void IEC61883Input::increment_counter(int *counter)
276 {
277         (*counter)++;
278         if(*counter >= total_buffers) *counter = 0;
279 }
280
281 void IEC61883Input::decrement_counter(int *counter)
282 {
283         (*counter)--;
284         if(*counter < 0) *counter = total_buffers - 1;
285 }
286
287
288
289 int IEC61883Input::read_video(VFrame *data)
290 {
291         int result = 0;
292
293 // Take over buffer table
294         buffer_lock->lock("IEC61883Input::read_video 1");
295 // Wait for buffer with timeout
296         while(!buffer_valid[current_outbuffer] && !result)
297         {
298                 buffer_lock->unlock();
299                 result = video_lock->timed_lock(BUFFER_TIMEOUT, "IEC61883Input::read_video 2");
300                 buffer_lock->lock("IEC61883Input::read_video 3");
301         }
302
303 // Copy frame
304         if(buffer_valid[current_outbuffer])
305         {
306                 data->allocate_compressed_data(buffer_size);
307                 data->set_compressed_size(buffer_size);
308                 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
309                 buffer_valid[current_outbuffer] = 0;
310                 increment_counter(&current_outbuffer);
311         }
312
313         buffer_lock->unlock();
314         return result;
315 }
316
317
318
319
320 int IEC61883Input::read_audio(char *data, int samples)
321 {
322         int result = 0;
323         int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
324         if(timeout < 500000) timeout = 500000;
325
326 // Take over buffer table
327         buffer_lock->lock("IEC61883Input::read_audio 1");
328 // Wait for buffer with timeout
329         while(audio_samples < samples && !result)
330         {
331                 buffer_lock->unlock();
332                 result = audio_lock->timed_lock(timeout, "IEC61883Input::read_audio 2");
333                 buffer_lock->lock("IEC61883Input::read_audio 3");
334         }
335
336         if(audio_samples >= samples)
337         {
338                 memcpy(data, audio_buffer, samples * bits * channels / 8);
339                 memcpy(audio_buffer, 
340                         audio_buffer + samples * bits * channels / 8,
341                         (audio_samples - samples) * bits * channels / 8);
342                 audio_samples -= samples;
343         }
344
345         buffer_lock->unlock();
346         return result;
347 }
348
349
350
351
352