asset drag/drop to viewers, bluebanana bug, listbox fontlist highlight
[goodguy/history.git] / cinelerra-5.1 / cinelerra / device1394input.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 #ifdef HAVE_FIREWIRE
23
24 #include "condition.h"
25 #include "device1394input.h"
26 #include "ieee1394-ioctl.h"
27 #include "mutex.h"
28 #include "vframe.h"
29 #include "video1394.h"
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <sys/ioctl.h>
34 #include <sys/mman.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #define INPUT_SAMPLES 131072
40 #define BUFFER_TIMEOUT 500000
41
42
43 Device1394Input::Device1394Input()
44  : Thread(1, 1, 0)
45 {
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         fd = -1;
61 }
62
63 Device1394Input::~Device1394Input()
64 {
65 // Driver crashes if it isn't stopped before cancelling the thread.
66 // May still crash during the cancel though.
67
68         if(Thread::running())
69         {
70                 done = 1;
71                 Thread::cancel();
72         }
73         Thread::join();
74
75         if(buffer)
76         {
77                 for(int i = 0; i < total_buffers; i++)
78                         delete [] buffer[i];
79                 delete [] buffer;
80                 delete [] buffer_valid;
81         }
82
83         if(input_buffer)
84                 munmap(input_buffer, total_buffers * buffer_size);
85
86         if(audio_buffer)
87         {
88                 delete [] audio_buffer;
89         }
90
91         if(decoder)
92         {
93                 dv_delete(decoder);
94         }
95
96         if(video_lock) delete video_lock;
97         if(audio_lock) delete audio_lock;
98         if(buffer_lock) delete buffer_lock;
99         if(fd > 0)
100         {
101                 close(fd);
102         }
103 }
104
105 int Device1394Input::open(const char *path,
106         int port,
107         int channel,
108         int length,
109         int channels,
110         int samplerate,
111         int bits,
112         int w,
113         int h)
114 {
115         int result = 0;
116         this->channel = channel;
117         this->length = length;
118         this->channels = channels;
119         this->samplerate = samplerate;
120         this->bits = bits;
121         this->w = w;
122         this->h = h;
123         is_pal = (h == 576);
124         buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
125         total_buffers = length;
126
127
128 // Initialize grabbing
129         if(fd < 0)
130         {
131                 if((fd = ::open(path, O_RDWR)) < 0)
132                 {
133                         printf("Device1394Input::open %s: %s\n", path, strerror(errno));
134                 }
135                 else
136                 {
137 #define CIP_N_NTSC   68000000
138 #define CIP_D_NTSC 1068000000
139
140 #define CIP_N_PAL  1
141 #define CIP_D_PAL 16
142
143                         struct dv1394_init init =
144                         {
145                                 api_version: DV1394_API_VERSION,
146                                 channel: (unsigned int)channel,
147                                 n_frames: (unsigned int)length,
148                                 format: is_pal ? DV1394_PAL: DV1394_NTSC,
149                                 cip_n: 0,
150                                 cip_d: 0,
151                                 syt_offset: 0
152                         };
153                         if(ioctl(fd, DV1394_IOC_INIT, &init) < 0)
154                         {
155                                 printf("Device1394Input::open DV1394_IOC_INIT: %s\n", strerror(errno));
156                         }
157
158                         input_buffer = (unsigned char*)mmap(0,
159                 length * buffer_size,
160                 PROT_READ | PROT_WRITE,
161                 MAP_SHARED,
162                 fd,
163                 0);
164
165                         if(ioctl(fd, DV1394_IOC_START_RECEIVE, 0) < 0)
166                         {
167                                 perror("Device1394Input::open DV1394_START_RECEIVE");
168                         }
169                 }
170
171
172
173                 buffer = new char*[total_buffers];
174                 buffer_valid = new int[total_buffers];
175                 bzero(buffer_valid, sizeof(int) * total_buffers);
176                 for(int i = 0; i < total_buffers; i++)
177                 {
178                         buffer[i] = new char[DV_PAL_SIZE];
179                 }
180
181
182                 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
183                 audio_lock = new Condition(0, "Device1394Input::audio_lock");
184                 video_lock = new Condition(0, "Device1394Input::video_lock");
185                 buffer_lock = new Mutex("Device1394Input::buffer_lock");
186
187                 decoder = dv_new();
188
189                 Thread::start();
190         }
191         return result;
192 }
193
194 void Device1394Input::run()
195 {
196         while(!done)
197         {
198 // Wait for frame to arrive
199                 struct dv1394_status status;
200 printf("Device1394Input::run %d done=%d\n", __LINE__, done);
201
202                 Thread::enable_cancel();
203                 if(ioctl(fd, DV1394_IOC_WAIT_FRAMES, 1))
204                 {
205                         perror("Device1394Input::run DV1394_IOC_WAIT_FRAMES");
206                         sleep(1);
207                 }
208                 else
209                 if(ioctl(fd, DV1394_IOC_GET_STATUS, &status))
210                 {
211                         perror("Device1394Input::run DV1394_IOC_GET_STATUS");
212                 }
213                 Thread::disable_cancel();
214
215
216
217                 buffer_lock->lock("Device1394Input::run 1");
218
219                 int nframes = status.n_clear_frames;
220                 for(int i = 0; i < nframes; i++)
221                 {
222 // Get a buffer to transfer to
223                         char *dst = 0;
224                         int is_overflow = 0;
225                         if(!buffer_valid[current_inbuffer])
226                                 dst = buffer[current_inbuffer];
227                         else
228                                 is_overflow = 1;
229
230                         char *src = (char*)(input_buffer + buffer_size * status.first_clear_frame);
231 // static FILE *test = 0;
232 // if(!test) test = fopen("/tmp/test", "w");
233 // fwrite(src, buffer_size, 1, test);
234
235 // Export the video
236                         if(dst)
237                         {
238                                 memcpy(dst, src, buffer_size);
239                                 buffer_valid[current_inbuffer] = 1;
240                                 video_lock->unlock();
241                         }
242
243
244 // Extract the audio
245                         if(audio_samples < INPUT_SAMPLES - 2048)
246                         {
247                                 int audio_result = dv_read_audio(decoder,
248                                         (unsigned char*)audio_buffer +
249                                                 audio_samples * 2 * 2,
250                                         (unsigned char*)src,
251                                         buffer_size,
252                                         channels,
253                                         bits);
254                                 int real_freq = decoder->decoder->audio->frequency;
255                                 if (real_freq == 32000)
256                                 {
257 // do in-place _FAST_ && _SIMPLE_ upsampling to 48khz
258 // i also think user should get a warning that his material is effectively 32khz
259 // we take 16bit samples for both channels in one 32bit int
260                                         int *twosample = (int*) (audio_buffer + audio_samples * 2 * 2);
261                                         int from = audio_result - 1;
262                                         int new_result = audio_result * 48000 / real_freq;
263                                         for (int to = new_result - 1; to >=0; to--)
264                                         {
265                                                 if ((to % 3) == 0 || (to % 3) == 1) from --;
266                                                 twosample[to] = twosample[from];
267                                         }
268                                         audio_result = new_result;
269                                 }
270
271
272                                 audio_samples += audio_result;
273
274 // Export the audio
275                                 audio_lock->unlock();
276                         }
277
278 // Advance buffer
279                         if(!is_overflow)
280                                 increment_counter(&current_inbuffer);
281
282
283                         Thread::enable_cancel();
284                         if(ioctl(fd, DV1394_IOC_RECEIVE_FRAMES, 1))
285                         {
286                                 perror("Device1394Input::run DV1394_IOC_RECEIVE_FRAMES");
287                         }
288
289                         if(ioctl(fd, DV1394_IOC_GET_STATUS, &status))
290                         {
291                                 perror("Device1394Input::run DV1394_IOC_GET_STATUS");
292                         }
293                         Thread::disable_cancel();
294                 }
295
296                 buffer_lock->unlock();
297         }
298 }
299
300 void Device1394Input::increment_counter(int *counter)
301 {
302         (*counter)++;
303         if(*counter >= total_buffers) *counter = 0;
304 }
305
306 void Device1394Input::decrement_counter(int *counter)
307 {
308         (*counter)--;
309         if(*counter < 0) *counter = total_buffers - 1;
310 }
311
312
313
314 int Device1394Input::read_video(VFrame *data)
315 {
316         int result = 0;
317
318 // Take over buffer table
319         buffer_lock->lock("Device1394Input::read_video 1");
320 // Wait for buffer with timeout
321         while(!buffer_valid[current_outbuffer] && !result)
322         {
323                 buffer_lock->unlock();
324                 result = video_lock->timed_lock(BUFFER_TIMEOUT, "Device1394Input::read_video 2");
325                 buffer_lock->lock("Device1394Input::read_video 3");
326         }
327
328 // Copy frame
329         if(buffer_valid[current_outbuffer])
330         {
331                 data->allocate_compressed_data(buffer_size);
332                 data->set_compressed_size(buffer_size);
333                 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
334                 buffer_valid[current_outbuffer] = 0;
335                 increment_counter(&current_outbuffer);
336         }
337
338         buffer_lock->unlock();
339         return result;
340 }
341
342
343
344
345 int Device1394Input::read_audio(char *data, int samples)
346 {
347         int result = 0;
348         int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
349         if(timeout < 500000) timeout = 500000;
350
351 // Take over buffer table
352         buffer_lock->lock("Device1394Input::read_audio 1");
353 // Wait for buffer with timeout
354         while(audio_samples < samples && !result)
355         {
356                 buffer_lock->unlock();
357                 result = audio_lock->timed_lock(timeout, "Device1394Input::read_audio 2");
358                 buffer_lock->lock("Device1394Input::read_audio 3");
359         }
360 //printf("Device1394Input::read_audio 1 %d %d\n", result, timeout);
361
362         if(audio_samples >= samples)
363         {
364                 memcpy(data, audio_buffer, samples * bits * channels / 8);
365                 memcpy(audio_buffer,
366                         audio_buffer + samples * bits * channels / 8,
367                         (audio_samples - samples) * bits * channels / 8);
368                 audio_samples -= samples;
369         }
370 //printf("Device1394Input::read_audio 100\n");
371         buffer_lock->unlock();
372         return result;
373 }
374
375 #endif