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