2 #include "device1394input.h"
3 #include "ieee1394-ioctl.h"
10 #include <sys/ioctl.h>
16 #define INPUT_SAMPLES 131072
17 #define BUFFER_TIMEOUT 500000
20 Device1394Input::Device1394Input()
29 current_outbuffer = 0;
40 Device1394Input::~Device1394Input()
42 // Driver crashes if it isn't stopped before cancelling the thread.
43 // May still crash during the cancel though.
54 for(int i = 0; i < total_buffers; i++)
57 delete [] buffer_valid;
61 munmap(input_buffer, total_buffers * buffer_size);
65 delete [] audio_buffer;
73 if(video_lock) delete video_lock;
74 if(audio_lock) delete audio_lock;
75 if(buffer_lock) delete buffer_lock;
82 int Device1394Input::open(const char *path,
93 this->channel = channel;
94 this->length = length;
95 this->channels = channels;
96 this->samplerate = samplerate;
101 buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
102 total_buffers = length;
105 // Initialize grabbing
108 if((fd = ::open(path, O_RDWR)) < 0)
110 printf("Device1394Input::open %s: %s\n", path, strerror(errno));
114 #define CIP_N_NTSC 68000000
115 #define CIP_D_NTSC 1068000000
120 struct dv1394_init init =
122 api_version: DV1394_API_VERSION,
125 format: is_pal ? DV1394_PAL: DV1394_NTSC,
130 if(ioctl(fd, DV1394_IOC_INIT, &init) < 0)
132 printf("Device1394Input::open DV1394_IOC_INIT: %s\n", strerror(errno));
135 input_buffer = (unsigned char*)mmap(0,
136 length * buffer_size,
137 PROT_READ | PROT_WRITE,
142 if(ioctl(fd, DV1394_IOC_START_RECEIVE, 0) < 0)
144 perror("Device1394Input::open DV1394_START_RECEIVE");
150 buffer = new char*[total_buffers];
151 buffer_valid = new int[total_buffers];
152 bzero(buffer_valid, sizeof(int) * total_buffers);
153 for(int i = 0; i < total_buffers; i++)
155 buffer[i] = new char[DV_PAL_SIZE];
159 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
161 audio_lock = new Condition(0, "Device1394Input::audio_lock");
162 video_lock = new Condition(0, "Device1394Input::video_lock");
163 buffer_lock = new Mutex("Device1394Input::buffer_lock");
172 void Device1394Input::run()
176 // Wait for frame to arrive
177 struct dv1394_status status;
179 Thread::enable_cancel();
180 if(ioctl(fd, DV1394_IOC_WAIT_FRAMES, 1))
182 perror("Device1394Input::run DV1394_IOC_WAIT_FRAMES");
186 if(ioctl(fd, DV1394_IOC_GET_STATUS, &status))
188 perror("Device1394Input::run DV1394_IOC_GET_STATUS");
190 Thread::disable_cancel();
194 buffer_lock->lock("Device1394Input::run 1");
196 for(int i = 0; i < status.n_clear_frames; i++)
198 // Get a buffer to transfer to
201 if(!buffer_valid[current_inbuffer])
202 dst = buffer[current_inbuffer];
206 char *src = (char*)(input_buffer + buffer_size * status.first_clear_frame);
207 // static FILE *test = 0;
208 // if(!test) test = fopen("/tmp/test", "w");
209 // fwrite(src, buffer_size, 1, test);
214 memcpy(dst, src, buffer_size);
215 buffer_valid[current_inbuffer] = 1;
216 video_lock->unlock();
221 if(audio_samples < INPUT_SAMPLES - 2048)
223 int audio_result = dv_read_audio(decoder,
224 (unsigned char*)audio_buffer +
225 audio_samples * 2 * 2,
230 int real_freq = decoder->decoder->audio->frequency;
231 if (real_freq == 32000)
233 // do in-place _FAST_ && _SIMPLE_ upsampling to 48khz
234 // i also think user should get a warning that his material is effectively 32khz
235 // we take 16bit samples for both channels in one 32bit int
236 int *twosample = (int*) (audio_buffer + audio_samples * 2 * 2);
237 int from = audio_result - 1;
238 int new_result = audio_result * 48000 / real_freq;
239 for (int to = new_result - 1; to >=0; to--)
241 if ((to % 3) == 0 || (to % 3) == 1) from --;
242 twosample[to] = twosample[from];
244 audio_result = new_result;
248 audio_samples += audio_result;
251 audio_lock->unlock();
256 increment_counter(¤t_inbuffer);
259 Thread::enable_cancel();
260 if(ioctl(fd, DV1394_IOC_RECEIVE_FRAMES, 1))
262 perror("Device1394Input::run DV1394_IOC_RECEIVE_FRAMES");
265 if(ioctl(fd, DV1394_IOC_GET_STATUS, &status))
267 perror("Device1394Input::run DV1394_IOC_GET_STATUS");
269 Thread::disable_cancel();
272 buffer_lock->unlock();
276 void Device1394Input::increment_counter(int *counter)
279 if(*counter >= total_buffers) *counter = 0;
282 void Device1394Input::decrement_counter(int *counter)
285 if(*counter < 0) *counter = total_buffers - 1;
290 int Device1394Input::read_video(VFrame *data)
294 // Take over buffer table
295 buffer_lock->lock("Device1394Input::read_video 1");
296 // Wait for buffer with timeout
297 while(!buffer_valid[current_outbuffer] && !result)
299 buffer_lock->unlock();
300 result = video_lock->timed_lock(BUFFER_TIMEOUT, "Device1394Input::read_video 2");
301 buffer_lock->lock("Device1394Input::read_video 3");
305 if(buffer_valid[current_outbuffer])
307 data->allocate_compressed_data(buffer_size);
308 data->set_compressed_size(buffer_size);
309 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
310 buffer_valid[current_outbuffer] = 0;
311 increment_counter(¤t_outbuffer);
314 buffer_lock->unlock();
321 int Device1394Input::read_audio(char *data, int samples)
324 int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
325 if(timeout < 500000) timeout = 500000;
327 // Take over buffer table
328 buffer_lock->lock("Device1394Input::read_audio 1");
329 // Wait for buffer with timeout
330 while(audio_samples < samples && !result)
332 buffer_lock->unlock();
333 result = audio_lock->timed_lock(timeout, "Device1394Input::read_audio 2");
334 buffer_lock->lock("Device1394Input::read_audio 3");
336 //printf("Device1394Input::read_audio 1 %d %d\n", result, timeout);
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;
346 //printf("Device1394Input::read_audio 100\n");
347 buffer_lock->unlock();