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
24 #include "condition.h"
25 #include "iec61883input.h"
32 #include <sys/ioctl.h>
38 #define INPUT_SAMPLES 131072
39 #define BUFFER_TIMEOUT 500000
42 IEC61883Input::IEC61883Input()
54 current_outbuffer = 0;
64 IEC61883Input::~IEC61883Input()
66 // Driver crashes if it isn't stopped before cancelling the thread.
67 // May still crash during the cancel though.
77 for(int i = 0; i < total_buffers; i++)
80 delete [] buffer_valid;
84 munmap(input_buffer, total_buffers * buffer_size);
88 delete [] audio_buffer;
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);
103 static int write_frame_static(unsigned char *data, int len, int complete, void *ptr)
105 IEC61883Input *input = (IEC61883Input*)ptr;
106 return input->write_frame(data, len, complete);
111 int IEC61883Input::open(int port,
121 this->channel = channel;
122 this->length = length;
123 this->channels = channels;
124 this->samplerate = samplerate;
129 buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
130 total_buffers = length;
133 // Initialize grabbing
136 handle = raw1394_new_handle_on_port(port);
139 frame = iec61883_dv_fb_init(handle, write_frame_static, (void *)this);
142 if(!iec61883_dv_fb_start(frame, channel))
144 fd = raw1394_get_fd(handle);
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++)
154 buffer[i] = new char[DV_PAL_SIZE];
158 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
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");
169 if(!handle || !frame || !fd) return 1;
175 void IEC61883Input::run()
177 while(!done && handle)
185 if(select(fd + 1, &rfds, 0, 0, &tv) > 0)
187 raw1394_loop_iterate(handle);
200 int IEC61883Input::write_frame(unsigned char *data, int len, int complete)
202 if(!complete) printf(_("write_frame: incomplete frame received.\n"));
204 buffer_lock->lock("IEC61883Input write_frame 1");
206 // Get a buffer to transfer to
209 if(!buffer_valid[current_inbuffer])
210 dst = buffer[current_inbuffer];
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);
222 memcpy(dst, src, len);
223 buffer_valid[current_inbuffer] = 1;
224 video_lock->unlock();
229 if(audio_samples < INPUT_SAMPLES - 2048)
231 int audio_result = dv_read_audio(decoder,
232 (unsigned char*)audio_buffer +
233 audio_samples * 2 * 2,
238 int real_freq = decoder->decoder->audio->frequency;
239 if (real_freq == 32000)
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--)
249 if ((to % 3) == 0 || (to % 3) == 1) from --;
250 twosample[to] = twosample[from];
252 audio_result = new_result;
256 audio_samples += audio_result;
259 audio_lock->unlock();
264 increment_counter(¤t_inbuffer);
267 buffer_lock->unlock();
277 void IEC61883Input::increment_counter(int *counter)
280 if(*counter >= total_buffers) *counter = 0;
283 void IEC61883Input::decrement_counter(int *counter)
286 if(*counter < 0) *counter = total_buffers - 1;
291 int IEC61883Input::read_video(VFrame *data)
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)
300 buffer_lock->unlock();
301 result = video_lock->timed_lock(BUFFER_TIMEOUT, "IEC61883Input::read_video 2");
302 buffer_lock->lock("IEC61883Input::read_video 3");
306 if(buffer_valid[current_outbuffer])
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(¤t_outbuffer);
315 buffer_lock->unlock();
322 int IEC61883Input::read_audio(char *data, int samples)
325 int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
326 if(timeout < 500000) timeout = 500000;
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)
333 buffer_lock->unlock();
334 result = audio_lock->timed_lock(timeout, "IEC61883Input::read_audio 2");
335 buffer_lock->lock("IEC61883Input::read_audio 3");
338 if(audio_samples >= samples)
340 memcpy(data, audio_buffer, samples * bits * channels / 8);
342 audio_buffer + samples * bits * channels / 8,
343 (audio_samples - samples) * bits * channels / 8);
344 audio_samples -= samples;
347 buffer_lock->unlock();