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