2 #include "iec61883input.h"
14 #define INPUT_SAMPLES 131072
15 #define BUFFER_TIMEOUT 500000
18 IEC61883Input::IEC61883Input()
30 current_outbuffer = 0;
40 IEC61883Input::~IEC61883Input()
42 // Driver crashes if it isn't stopped before cancelling the thread.
43 // May still crash during the cancel though.
53 for(int i = 0; i < total_buffers; i++)
56 delete [] buffer_valid;
60 munmap(input_buffer, total_buffers * buffer_size);
64 delete [] audio_buffer;
72 if(video_lock) delete video_lock;
73 if(audio_lock) delete audio_lock;
74 if(buffer_lock) delete buffer_lock;
75 if(frame) iec61883_dv_fb_close(frame);
76 if(handle) raw1394_destroy_handle(handle);
79 static int write_frame_static(unsigned char *data, int len, int complete, void *ptr)
81 IEC61883Input *input = (IEC61883Input*)ptr;
82 return input->write_frame(data, len, complete);
87 int IEC61883Input::open(int port,
97 this->channel = channel;
98 this->length = length;
99 this->channels = channels;
100 this->samplerate = samplerate;
105 buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
106 total_buffers = length;
109 // Initialize grabbing
112 handle = raw1394_new_handle_on_port(port);
115 frame = iec61883_dv_fb_init(handle, write_frame_static, (void *)this);
118 if(!iec61883_dv_fb_start(frame, channel))
120 fd = raw1394_get_fd(handle);
125 buffer = new char*[total_buffers];
126 buffer_valid = new int[total_buffers];
127 bzero(buffer_valid, sizeof(int) * total_buffers);
128 for(int i = 0; i < total_buffers; i++)
130 buffer[i] = new char[DV_PAL_SIZE];
134 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
136 audio_lock = new Condition(0, "IEC61883Input::audio_lock");
137 video_lock = new Condition(0, "IEC61883Input::video_lock");
138 buffer_lock = new Mutex("IEC61883Input::buffer_lock");
145 if(!handle || !frame || !fd) return 1;
151 void IEC61883Input::run()
153 while(!done && handle)
161 if(select(fd + 1, &rfds, 0, 0, &tv) > 0)
163 raw1394_loop_iterate(handle);
176 int IEC61883Input::write_frame(unsigned char *data, int len, int complete)
178 if(!complete) printf("write_frame: incomplete frame received.\n");
180 buffer_lock->lock("IEC61883Input write_frame 1");
182 // Get a buffer to transfer to
185 if(!buffer_valid[current_inbuffer])
186 dst = buffer[current_inbuffer];
190 char *src = (char*)(data);
191 // static FILE *test = 0;
192 // if(!test) test = fopen("/tmp/test", "w");
193 // fwrite(src, buffer_size, 1, test);
198 memcpy(dst, src, len);
199 buffer_valid[current_inbuffer] = 1;
200 video_lock->unlock();
205 if(audio_samples < INPUT_SAMPLES - 2048)
207 int audio_result = dv_read_audio(decoder,
208 (unsigned char*)audio_buffer +
209 audio_samples * 2 * 2,
214 int real_freq = decoder->decoder->audio->frequency;
215 if (real_freq == 32000)
217 // do in-place _FAST_ && _SIMPLE_ upsampling to 48khz
218 // i also think user should get a warning that his material is effectively 32khz
219 // we take 16bit samples for both channels in one 32bit int
220 int *twosample = (int*) (audio_buffer + audio_samples * 2 * 2);
221 int from = audio_result - 1;
222 int new_result = audio_result * 48000 / real_freq;
223 for (int to = new_result - 1; to >=0; to--)
225 if ((to % 3) == 0 || (to % 3) == 1) from --;
226 twosample[to] = twosample[from];
228 audio_result = new_result;
232 audio_samples += audio_result;
235 audio_lock->unlock();
240 increment_counter(¤t_inbuffer);
243 buffer_lock->unlock();
253 void IEC61883Input::increment_counter(int *counter)
256 if(*counter >= total_buffers) *counter = 0;
259 void IEC61883Input::decrement_counter(int *counter)
262 if(*counter < 0) *counter = total_buffers - 1;
267 int IEC61883Input::read_video(VFrame *data)
271 // Take over buffer table
272 buffer_lock->lock("IEC61883Input::read_video 1");
273 // Wait for buffer with timeout
274 while(!buffer_valid[current_outbuffer] && !result)
276 buffer_lock->unlock();
277 result = video_lock->timed_lock(BUFFER_TIMEOUT, "IEC61883Input::read_video 2");
278 buffer_lock->lock("IEC61883Input::read_video 3");
282 if(buffer_valid[current_outbuffer])
284 data->allocate_compressed_data(buffer_size);
285 data->set_compressed_size(buffer_size);
286 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
287 buffer_valid[current_outbuffer] = 0;
288 increment_counter(¤t_outbuffer);
291 buffer_lock->unlock();
298 int IEC61883Input::read_audio(char *data, int samples)
301 int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
302 if(timeout < 500000) timeout = 500000;
304 // Take over buffer table
305 buffer_lock->lock("IEC61883Input::read_audio 1");
306 // Wait for buffer with timeout
307 while(audio_samples < samples && !result)
309 buffer_lock->unlock();
310 result = audio_lock->timed_lock(timeout, "IEC61883Input::read_audio 2");
311 buffer_lock->lock("IEC61883Input::read_audio 3");
314 if(audio_samples >= samples)
316 memcpy(data, audio_buffer, samples * bits * channels / 8);
318 audio_buffer + samples * bits * channels / 8,
319 (audio_samples - samples) * bits * channels / 8);
320 audio_samples -= samples;
323 buffer_lock->unlock();