r999: maintainers added to README_en.
[cinelerra_cv/mob.git] / cinelerra / device1394input.C
blob7abbdb48e1c5e1bc692be48c1c98fcdfb5a2f789
1 #include "condition.h"
2 #include "device1394input.h"
3 #include "ieee1394-ioctl.h"
4 #include "mutex.h"
5 #include "vframe.h"
6 #include "video1394.h"
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <sys/ioctl.h>
11 #include <sys/mman.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
16 #define INPUT_SAMPLES 131072
17 #define BUFFER_TIMEOUT 500000
20 Device1394Input::Device1394Input()
21  : Thread(1, 1, 0)
23         buffer = 0;
24         buffer_valid = 0;
25         input_buffer = 0;
26         done = 0;
27         total_buffers = 0;
28         current_inbuffer = 0;
29         current_outbuffer = 0;
30         buffer_size = 0;
31         audio_buffer = 0;
32         audio_samples = 0;
33         video_lock = 0;
34         audio_lock = 0;
35         buffer_lock = 0;
36         decoder = 0;
37         fd = -1;
40 Device1394Input::~Device1394Input()
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::cancel();
49                 Thread::join();
50         }
52         if(buffer)
53         {
54                 for(int i = 0; i < total_buffers; i++)
55                         delete [] buffer[i];
56                 delete [] buffer;
57                 delete [] buffer_valid;
58         }
60         if(input_buffer)
61                 munmap(input_buffer, total_buffers * buffer_size);
63         if(audio_buffer)
64         {
65                 delete [] audio_buffer;
66         }
68         if(decoder)
69         {
70                 dv_delete(decoder);
71         }
73         if(video_lock) delete video_lock;
74         if(audio_lock) delete audio_lock;
75         if(buffer_lock) delete buffer_lock;
76         if(fd > 0)
77         {
78                 close(fd);
79         }
82 int Device1394Input::open(const char *path,
83         int port,
84         int channel,
85         int length,
86         int channels,
87         int samplerate,
88         int bits,
89         int w,
90         int h)
92         int result = 0;
93         this->channel = channel;
94         this->length = length;
95         this->channels = channels;
96         this->samplerate = samplerate;
97         this->bits = bits;
98         this->w = w;
99         this->h = h;
100         is_pal = (h == 576);
101         buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
102         total_buffers = length;
105 // Initialize grabbing
106         if(fd < 0)
107         {
108                 if((fd = ::open(path, O_RDWR)) < 0)
109                 {
110                         printf("Device1394Input::open %s: %s\n", path, strerror(errno));
111                 }
112                 else
113                 {
114 #define CIP_N_NTSC   68000000
115 #define CIP_D_NTSC 1068000000
117 #define CIP_N_PAL  1
118 #define CIP_D_PAL 16
120                         struct dv1394_init init = 
121                         {
122                                 api_version: DV1394_API_VERSION,
123                                 channel: channel,
124                                 n_frames: length,
125                                 format: is_pal ? DV1394_PAL: DV1394_NTSC,
126                                 cip_n: 0,
127                                 cip_d: 0,
128                                 syt_offset: 0
129                         };
130                         if(ioctl(fd, DV1394_IOC_INIT, &init) < 0)
131                         {
132                                 printf("Device1394Input::open DV1394_IOC_INIT: %s\n", strerror(errno));
133                         }
135                         input_buffer = (unsigned char*)mmap(0,
136                 length * buffer_size,
137                 PROT_READ | PROT_WRITE,
138                 MAP_SHARED,
139                 fd,
140                 0);
142                         if(ioctl(fd, DV1394_IOC_START_RECEIVE, 0) < 0)
143                         {
144                                 perror("Device1394Input::open DV1394_START_RECEIVE");
145                         }
146                 }
150                 buffer = new char*[total_buffers];
151                 buffer_valid = new int[total_buffers];
152                 bzero(buffer_valid, sizeof(int) * total_buffers);
153                 for(int i = 0; i < total_buffers; i++)
154                 {
155                         buffer[i] = new char[DV_PAL_SIZE];
156                 }
159                 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
161                 audio_lock = new Condition(0, "Device1394Input::audio_lock");
162                 video_lock = new Condition(0, "Device1394Input::video_lock");
163                 buffer_lock = new Mutex("Device1394Input::buffer_lock");
165                 decoder = dv_new();
167                 Thread::start();
168         }
169         return result;
172 void Device1394Input::run()
174         while(!done)
175         {
176 // Wait for frame to arrive
177                 struct dv1394_status status;
179                 Thread::enable_cancel();
180                 if(ioctl(fd, DV1394_IOC_WAIT_FRAMES, 1))
181                 {
182                         perror("Device1394Input::run DV1394_IOC_WAIT_FRAMES");
183                         sleep(1);
184                 }
185                 else
186                 if(ioctl(fd, DV1394_IOC_GET_STATUS, &status))
187                 {
188                         perror("Device1394Input::run DV1394_IOC_GET_STATUS");
189                 }
190                 Thread::disable_cancel();
194                 buffer_lock->lock("Device1394Input::run 1");
196                 for(int i = 0; i < status.n_clear_frames; i++)
197                 {
198 // Get a buffer to transfer to
199                         char *dst = 0;
200                         int is_overflow = 0;
201                         if(!buffer_valid[current_inbuffer])
202                                 dst = buffer[current_inbuffer];
203                         else
204                                 is_overflow = 1;
206                         char *src = (char*)(input_buffer + buffer_size * status.first_clear_frame);
207 // static FILE *test = 0;
208 // if(!test) test = fopen("/tmp/test", "w");
209 // fwrite(src, buffer_size, 1, test);
211 // Export the video
212                         if(dst)
213                         {
214                                 memcpy(dst, src, buffer_size);
215                                 buffer_valid[current_inbuffer] = 1;
216                                 video_lock->unlock();
217                         }
220 // Extract the audio
221                         if(audio_samples < INPUT_SAMPLES - 2048)
222                         {
223                                 int audio_result = dv_read_audio(decoder, 
224                                         (unsigned char*)audio_buffer + 
225                                                 audio_samples * 2 * 2,
226                                         (unsigned char*)src,
227                                         buffer_size,
228                                         channels,
229                                         bits);
230                                 int real_freq = decoder->decoder->audio->frequency;
231                                 if (real_freq == 32000) 
232                                 {
233 // do in-place _FAST_ && _SIMPLE_ upsampling to 48khz
234 // i also think user should get a warning that his material is effectively 32khz
235 // we take 16bit samples for both channels in one 32bit int
236                                         int *twosample = (int*) (audio_buffer + audio_samples * 2 * 2);
237                                         int from = audio_result - 1;
238                                         int new_result = audio_result * 48000 / real_freq;
239                                         for (int to = new_result - 1; to >=0; to--)
240                                         {       
241                                                 if ((to % 3) == 0 || (to % 3) == 1) from --;
242                                                 twosample[to] = twosample[from];
243                                         }
244                                         audio_result = new_result;
245                                 }
248                                 audio_samples += audio_result;
250 // Export the audio
251                                 audio_lock->unlock();
252                         }
254 // Advance buffer
255                         if(!is_overflow)
256                                 increment_counter(&current_inbuffer);
259                         Thread::enable_cancel();
260                         if(ioctl(fd, DV1394_IOC_RECEIVE_FRAMES, 1))
261                         {
262                                 perror("Device1394Input::run DV1394_IOC_RECEIVE_FRAMES");
263                         }
265                         if(ioctl(fd, DV1394_IOC_GET_STATUS, &status))
266                         {
267                                 perror("Device1394Input::run DV1394_IOC_GET_STATUS");
268                         }
269                         Thread::disable_cancel();
270                 }
272                 buffer_lock->unlock();
273         }
276 void Device1394Input::increment_counter(int *counter)
278         (*counter)++;
279         if(*counter >= total_buffers) *counter = 0;
282 void Device1394Input::decrement_counter(int *counter)
284         (*counter)--;
285         if(*counter < 0) *counter = total_buffers - 1;
290 int Device1394Input::read_video(VFrame *data)
292         int result = 0;
294 // Take over buffer table
295         buffer_lock->lock("Device1394Input::read_video 1");
296 // Wait for buffer with timeout
297         while(!buffer_valid[current_outbuffer] && !result)
298         {
299                 buffer_lock->unlock();
300                 result = video_lock->timed_lock(BUFFER_TIMEOUT, "Device1394Input::read_video 2");
301                 buffer_lock->lock("Device1394Input::read_video 3");
302         }
304 // Copy frame
305         if(buffer_valid[current_outbuffer])
306         {
307                 data->allocate_compressed_data(buffer_size);
308                 data->set_compressed_size(buffer_size);
309                 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
310                 buffer_valid[current_outbuffer] = 0;
311                 increment_counter(&current_outbuffer);
312         }
314         buffer_lock->unlock();
315         return result;
321 int Device1394Input::read_audio(char *data, int samples)
323         int result = 0;
324         int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
325         if(timeout < 500000) timeout = 500000;
327 // Take over buffer table
328         buffer_lock->lock("Device1394Input::read_audio 1");
329 // Wait for buffer with timeout
330         while(audio_samples < samples && !result)
331         {
332                 buffer_lock->unlock();
333                 result = audio_lock->timed_lock(timeout, "Device1394Input::read_audio 2");
334                 buffer_lock->lock("Device1394Input::read_audio 3");
335         }
336 //printf("Device1394Input::read_audio 1 %d %d\n", result, timeout);
338         if(audio_samples >= samples)
339         {
340                 memcpy(data, audio_buffer, samples * bits * channels / 8);
341                 memcpy(audio_buffer, 
342                         audio_buffer + samples * bits * channels / 8,
343                         (audio_samples - samples) * bits * channels / 8);
344                 audio_samples -= samples;
345         }
346 //printf("Device1394Input::read_audio 100\n");
347         buffer_lock->unlock();
348         return result;