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