2 #include "device1394input.h"
11 #define INPUT_SAMPLES 131072
12 #define BUFFER_TIMEOUT 500000
15 Device1394Input::Device1394Input()
23 current_outbuffer = 0;
36 Device1394Input::~Device1394Input()
39 // Driver crashes if it isn't stopped before cancelling the thread.
40 // May still crash during the cancel though.
43 raw1394_stop_iso_rcv(handle, channel);
55 for(int i = 0; i < total_buffers; i++)
58 delete [] buffer_valid;
68 delete [] audio_buffer;
73 raw1394_destroy_handle(handle);
81 if(video_lock) delete video_lock;
82 if(audio_lock) delete audio_lock;
83 if(buffer_lock) delete buffer_lock;
86 int Device1394Input::open(int port,
94 this->channel = channel;
95 this->length = length;
96 this->channels = channels;
97 this->samplerate = samplerate;
100 // Initialize grabbing
104 struct raw1394_portinfo pinf[16];
106 if(!(handle = raw1394_new_handle()))
108 perror("Device1394::open_input: raw1394_get_handle");
113 if((numcards = raw1394_get_port_info(handle, pinf, 16)) < 0)
115 perror("Device1394::open_input: raw1394_get_port_info");
116 raw1394_destroy_handle(handle);
121 if(!pinf[port].nodes)
123 printf("Device1394::open_input: pinf[port].nodes == 0\n");
124 raw1394_destroy_handle(handle);
129 if(raw1394_set_port(handle, port) < 0)
131 perror("Device1394::open_input: raw1394_set_port");
132 raw1394_destroy_handle(handle);
137 raw1394_set_iso_handler(handle, channel, dv_iso_handler);
138 // raw1394_set_bus_reset_handler(handle, dv_reset_handler);
139 raw1394_set_userdata(handle, this);
140 if(raw1394_start_iso_rcv(handle, channel) < 0)
142 perror("Device1394::open_input: raw1394_start_iso_rcv");
143 raw1394_destroy_handle(handle);
152 total_buffers = length;
153 buffer = new char*[total_buffers];
154 buffer_valid = new int[total_buffers];
155 bzero(buffer_valid, sizeof(int) * total_buffers);
156 for(int i = 0; i < total_buffers; i++)
158 buffer[i] = new char[DV_PAL_SIZE];
161 temp = new char[DV_PAL_SIZE];
163 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
165 audio_lock = new Condition(0);
166 video_lock = new Condition(0);
167 buffer_lock = new Mutex;
176 void Device1394Input::run()
180 Thread::enable_cancel();
181 raw1394_loop_iterate(handle);
182 Thread::disable_cancel();
186 void Device1394Input::increment_counter(int *counter)
189 if(*counter >= total_buffers) *counter = 0;
192 void Device1394Input::decrement_counter(int *counter)
195 if(*counter < 0) *counter = total_buffers - 1;
200 int Device1394Input::read_video(VFrame *data)
204 //printf("Device1394Input::read_video 1\n");
205 // Take over buffer table
207 //printf("Device1394Input::read_video 1\n");
208 // Wait for buffer with timeout
209 while(!buffer_valid[current_outbuffer] && !result)
211 buffer_lock->unlock();
212 result = video_lock->timed_lock(BUFFER_TIMEOUT);
215 //printf("Device1394Input::read_video 1\n");
218 if(buffer_valid[current_outbuffer])
220 data->allocate_compressed_data(buffer_size);
221 data->set_compressed_size(buffer_size);
222 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
223 buffer_valid[current_outbuffer] = 0;
224 increment_counter(¤t_outbuffer);
226 //printf("Device1394Input::read_video 100\n");
228 buffer_lock->unlock();
235 int Device1394Input::read_audio(char *data, int samples)
238 //printf("Device1394Input::read_audio 1\n");
239 int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
240 if(timeout < 500000) timeout = 500000;
241 //printf("Device1394Input::read_audio 1\n");
243 // Take over buffer table
245 // Wait for buffer with timeout
246 while(audio_samples < samples && !result)
248 buffer_lock->unlock();
249 result = audio_lock->timed_lock(timeout);
252 //printf("Device1394Input::read_audio 1 %d %d\n", result, timeout);
254 if(audio_samples >= samples)
256 memcpy(data, audio_buffer, samples * bits * channels / 8);
258 audio_buffer + samples * bits * channels / 8,
259 (audio_samples - samples) * bits * channels / 8);
260 audio_samples -= samples;
262 //printf("Device1394Input::read_audio 100\n");
263 buffer_lock->unlock();
272 int Device1394Input::dv_iso_handler(raw1394handle_t handle,
277 Device1394Input *thread = (Device1394Input*)raw1394_get_userdata(handle);
279 //printf("Device1394Input::dv_iso_handler 1\n");
280 thread->Thread::disable_cancel();
282 #define BLOCK_SIZE 480
285 unsigned char *ptr = (unsigned char*)&data[3];
286 int section_type = ptr[0] >> 5;
287 int dif_sequence = ptr[1] >> 4;
288 int dif_block = ptr[2];
291 if(section_type == 0 &&
295 // Need to conform the frame size so our format detection is right
296 //printf("Device1394Input::dv_iso_handler 10\n");
297 if(thread->bytes_read == DV_PAL_SIZE ||
298 thread->bytes_read == DV_NTSC_SIZE)
300 // Copy frame to buffer
301 //printf("Device1394Input::dv_iso_handler 20\n");
302 thread->buffer_size = thread->bytes_read;
305 thread->buffer_lock->lock();
306 //printf("Device1394Input::dv_iso_handler 21 %p\n", thread->buffer[thread->current_inbuffer]);
307 memcpy(thread->buffer[thread->current_inbuffer],
310 //printf("Device1394Input::dv_iso_handler 22 %p\n", thread->buffer[thread->current_inbuffer]);
311 thread->buffer_valid[thread->current_inbuffer] = 1;
312 thread->video_lock->unlock();
314 // Decode audio to audio store
315 if(thread->audio_samples < INPUT_SAMPLES - 2048)
317 int decode_result = dv_read_audio(thread->decoder,
318 (unsigned char*)thread->audio_buffer +
319 thread->audio_samples * 2 * 2,
320 (unsigned char*)thread->temp,
324 thread->audio_samples += decode_result;
326 //printf("Device1394Input::dv_iso_handler 25 %d\n", decode_result);
327 thread->audio_lock->unlock();
330 // Advance buffer if possible
331 thread->increment_counter(&thread->current_inbuffer);
332 if(thread->buffer_valid[thread->current_inbuffer])
333 thread->decrement_counter(&thread->current_inbuffer);
335 thread->buffer_lock->unlock();
336 //printf("Device1394Input::dv_iso_handler 30\n");
338 thread->bytes_read = 0;
341 //printf("Device1394Input::dv_iso_handler 40\n");
345 memcpy(thread->temp +
346 dif_sequence * 150 * 80, ptr, BLOCK_SIZE);
350 memcpy(thread->temp +
351 dif_sequence * 150 * 80 + (1 + dif_block) * 80,
357 memcpy(thread->temp +
358 dif_sequence * 150 * 80 + (3 + dif_block) * 80,
363 case 3: // Audio block
364 memcpy(thread->temp +
365 dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80,
370 case 4: // Video block
371 memcpy(thread->temp +
372 dif_sequence * 150 * 80 + (7 + (dif_block / 15) + dif_block) * 80,
380 //printf("Device1394Input::dv_iso_handler 50\n");
382 thread->bytes_read += BLOCK_SIZE;
384 //printf("Device1394Input::dv_iso_handler 100\n");
389 bus_reset_handler_t Device1394Input::dv_reset_handler(raw1394handle_t handle,
390 unsigned int generation)
392 printf("Device1394::dv_reset_handler: generation=%p\n", generation);