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"
29 #include <sys/ioctl.h>
35 #define INPUT_SAMPLES 131072
36 #define BUFFER_TIMEOUT 500000
39 IEC61883Input::IEC61883Input()
51 current_outbuffer = 0;
61 IEC61883Input::~IEC61883Input()
63 // Driver crashes if it isn't stopped before cancelling the thread.
64 // May still crash during the cancel though.
74 for(int i = 0; i < total_buffers; i++)
77 delete [] buffer_valid;
81 munmap(input_buffer, total_buffers * buffer_size);
85 delete [] audio_buffer;
93 if(video_lock) delete video_lock;
94 if(audio_lock) delete audio_lock;
95 if(buffer_lock) delete buffer_lock;
96 if(frame) iec61883_dv_fb_close(frame);
97 if(handle) raw1394_destroy_handle(handle);
100 static int write_frame_static(unsigned char *data, int len, int complete, void *ptr)
102 IEC61883Input *input = (IEC61883Input*)ptr;
103 return input->write_frame(data, len, complete);
108 int IEC61883Input::open(int port,
118 this->channel = channel;
119 this->length = length;
120 this->channels = channels;
121 this->samplerate = samplerate;
126 buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
127 total_buffers = length;
130 // Initialize grabbing
133 handle = raw1394_new_handle_on_port(port);
136 frame = iec61883_dv_fb_init(handle, write_frame_static, (void *)this);
139 if(!iec61883_dv_fb_start(frame, channel))
141 fd = raw1394_get_fd(handle);
146 buffer = new char*[total_buffers];
147 buffer_valid = new int[total_buffers];
148 bzero(buffer_valid, sizeof(int) * total_buffers);
149 for(int i = 0; i < total_buffers; i++)
151 buffer[i] = new char[DV_PAL_SIZE];
155 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
157 audio_lock = new Condition(0, "IEC61883Input::audio_lock");
158 video_lock = new Condition(0, "IEC61883Input::video_lock");
159 buffer_lock = new Mutex("IEC61883Input::buffer_lock");
166 if(!handle || !frame || !fd) return 1;
172 void IEC61883Input::run()
174 while(!done && handle)
182 if(select(fd + 1, &rfds, 0, 0, &tv) > 0)
184 raw1394_loop_iterate(handle);
197 int IEC61883Input::write_frame(unsigned char *data, int len, int complete)
199 if(!complete) printf("write_frame: incomplete frame received.\n");
201 buffer_lock->lock("IEC61883Input write_frame 1");
203 // Get a buffer to transfer to
206 if(!buffer_valid[current_inbuffer])
207 dst = buffer[current_inbuffer];
211 char *src = (char*)(data);
212 // static FILE *test = 0;
213 // if(!test) test = fopen("/tmp/test", "w");
214 // fwrite(src, buffer_size, 1, test);
219 memcpy(dst, src, len);
220 buffer_valid[current_inbuffer] = 1;
221 video_lock->unlock();
226 if(audio_samples < INPUT_SAMPLES - 2048)
228 int audio_result = dv_read_audio(decoder,
229 (unsigned char*)audio_buffer +
230 audio_samples * 2 * 2,
235 int real_freq = decoder->decoder->audio->frequency;
236 if (real_freq == 32000)
238 // do in-place _FAST_ && _SIMPLE_ upsampling to 48khz
239 // i also think user should get a warning that his material is effectively 32khz
240 // we take 16bit samples for both channels in one 32bit int
241 int *twosample = (int*) (audio_buffer + audio_samples * 2 * 2);
242 int from = audio_result - 1;
243 int new_result = audio_result * 48000 / real_freq;
244 for (int to = new_result - 1; to >=0; to--)
246 if ((to % 3) == 0 || (to % 3) == 1) from --;
247 twosample[to] = twosample[from];
249 audio_result = new_result;
253 audio_samples += audio_result;
256 audio_lock->unlock();
261 increment_counter(¤t_inbuffer);
264 buffer_lock->unlock();
274 void IEC61883Input::increment_counter(int *counter)
277 if(*counter >= total_buffers) *counter = 0;
280 void IEC61883Input::decrement_counter(int *counter)
283 if(*counter < 0) *counter = total_buffers - 1;
288 int IEC61883Input::read_video(VFrame *data)
292 // Take over buffer table
293 buffer_lock->lock("IEC61883Input::read_video 1");
294 // Wait for buffer with timeout
295 while(!buffer_valid[current_outbuffer] && !result)
297 buffer_lock->unlock();
298 result = video_lock->timed_lock(BUFFER_TIMEOUT, "IEC61883Input::read_video 2");
299 buffer_lock->lock("IEC61883Input::read_video 3");
303 if(buffer_valid[current_outbuffer])
305 data->allocate_compressed_data(buffer_size);
306 data->set_compressed_size(buffer_size);
307 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
308 buffer_valid[current_outbuffer] = 0;
309 increment_counter(¤t_outbuffer);
312 buffer_lock->unlock();
319 int IEC61883Input::read_audio(char *data, int samples)
322 int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
323 if(timeout < 500000) timeout = 500000;
325 // Take over buffer table
326 buffer_lock->lock("IEC61883Input::read_audio 1");
327 // Wait for buffer with timeout
328 while(audio_samples < samples && !result)
330 buffer_lock->unlock();
331 result = audio_lock->timed_lock(timeout, "IEC61883Input::read_audio 2");
332 buffer_lock->lock("IEC61883Input::read_audio 3");
335 if(audio_samples >= samples)
337 memcpy(data, audio_buffer, samples * bits * channels / 8);
339 audio_buffer + samples * bits * channels / 8,
340 (audio_samples - samples) * bits * channels / 8);
341 audio_samples -= samples;
344 buffer_lock->unlock();