4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "condition.h"
23 #include "iec61883input.h"
30 #include <sys/ioctl.h>
36 #define INPUT_SAMPLES 131072
37 #define BUFFER_TIMEOUT 500000
40 IEC61883Input::IEC61883Input()
52 current_outbuffer = 0;
62 IEC61883Input::~IEC61883Input()
64 // Driver crashes if it isn't stopped before cancelling the thread.
65 // May still crash during the cancel though.
75 for(int i = 0; i < total_buffers; i++)
78 delete [] buffer_valid;
82 munmap(input_buffer, total_buffers * buffer_size);
86 delete [] audio_buffer;
94 if(video_lock) delete video_lock;
95 if(audio_lock) delete audio_lock;
96 if(buffer_lock) delete buffer_lock;
97 if(frame) iec61883_dv_fb_close(frame);
98 if(handle) raw1394_destroy_handle(handle);
101 static int write_frame_static(unsigned char *data, int len, int complete, void *ptr)
103 IEC61883Input *input = (IEC61883Input*)ptr;
104 return input->write_frame(data, len, complete);
109 int IEC61883Input::open(int port,
119 this->channel = channel;
120 this->length = length;
121 this->channels = channels;
122 this->samplerate = samplerate;
127 buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
128 total_buffers = length;
131 // Initialize grabbing
134 handle = raw1394_new_handle_on_port(port);
137 frame = iec61883_dv_fb_init(handle, write_frame_static, (void *)this);
140 if(!iec61883_dv_fb_start(frame, channel))
142 fd = raw1394_get_fd(handle);
147 buffer = new char*[total_buffers];
148 buffer_valid = new int[total_buffers];
149 bzero(buffer_valid, sizeof(int) * total_buffers);
150 for(int i = 0; i < total_buffers; i++)
152 buffer[i] = new char[DV_PAL_SIZE];
156 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
158 audio_lock = new Condition(0, "IEC61883Input::audio_lock");
159 video_lock = new Condition(0, "IEC61883Input::video_lock");
160 buffer_lock = new Mutex("IEC61883Input::buffer_lock");
167 if(!handle || !frame || !fd) return 1;
173 void IEC61883Input::run()
175 while(!done && handle)
183 if(select(fd + 1, &rfds, 0, 0, &tv) > 0)
185 raw1394_loop_iterate(handle);
198 int IEC61883Input::write_frame(unsigned char *data, int len, int complete)
200 if(!complete) printf(_("write_frame: incomplete frame received.\n"));
202 buffer_lock->lock("IEC61883Input write_frame 1");
204 // Get a buffer to transfer to
207 if(!buffer_valid[current_inbuffer])
208 dst = buffer[current_inbuffer];
212 char *src = (char*)(data);
213 // static FILE *test = 0;
214 // if(!test) test = fopen("/tmp/test", "w");
215 // fwrite(src, buffer_size, 1, test);
220 memcpy(dst, src, len);
221 buffer_valid[current_inbuffer] = 1;
222 video_lock->unlock();
227 if(audio_samples < INPUT_SAMPLES - 2048)
229 int audio_result = dv_read_audio(decoder,
230 (unsigned char*)audio_buffer +
231 audio_samples * 2 * 2,
236 int real_freq = decoder->decoder->audio->frequency;
237 if (real_freq == 32000)
239 // do in-place _FAST_ && _SIMPLE_ upsampling to 48khz
240 // i also think user should get a warning that his material is effectively 32khz
241 // we take 16bit samples for both channels in one 32bit int
242 int *twosample = (int*) (audio_buffer + audio_samples * 2 * 2);
243 int from = audio_result - 1;
244 int new_result = audio_result * 48000 / real_freq;
245 for (int to = new_result - 1; to >=0; to--)
247 if ((to % 3) == 0 || (to % 3) == 1) from --;
248 twosample[to] = twosample[from];
250 audio_result = new_result;
254 audio_samples += audio_result;
257 audio_lock->unlock();
262 increment_counter(¤t_inbuffer);
265 buffer_lock->unlock();
275 void IEC61883Input::increment_counter(int *counter)
278 if(*counter >= total_buffers) *counter = 0;
281 void IEC61883Input::decrement_counter(int *counter)
284 if(*counter < 0) *counter = total_buffers - 1;
289 int IEC61883Input::read_video(VFrame *data)
293 // Take over buffer table
294 buffer_lock->lock("IEC61883Input::read_video 1");
295 // Wait for buffer with timeout
296 while(!buffer_valid[current_outbuffer] && !result)
298 buffer_lock->unlock();
299 result = video_lock->timed_lock(BUFFER_TIMEOUT, "IEC61883Input::read_video 2");
300 buffer_lock->lock("IEC61883Input::read_video 3");
304 if(buffer_valid[current_outbuffer])
306 data->allocate_compressed_data(buffer_size);
307 data->set_compressed_size(buffer_size);
308 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
309 buffer_valid[current_outbuffer] = 0;
310 increment_counter(¤t_outbuffer);
313 buffer_lock->unlock();
320 int IEC61883Input::read_audio(char *data, int samples)
323 int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
324 if(timeout < 500000) timeout = 500000;
326 // Take over buffer table
327 buffer_lock->lock("IEC61883Input::read_audio 1");
328 // Wait for buffer with timeout
329 while(audio_samples < samples && !result)
331 buffer_lock->unlock();
332 result = audio_lock->timed_lock(timeout, "IEC61883Input::read_audio 2");
333 buffer_lock->lock("IEC61883Input::read_audio 3");
336 if(audio_samples >= samples)
338 memcpy(data, audio_buffer, samples * bits * channels / 8);
340 audio_buffer + samples * bits * channels / 8,
341 (audio_samples - samples) * bits * channels / 8);
342 audio_samples -= samples;
345 buffer_lock->unlock();