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 "device1394input.h"
26 #include "ieee1394-ioctl.h"
29 #include "video1394.h"
33 #include <sys/ioctl.h>
39 #define INPUT_SAMPLES 131072
40 #define BUFFER_TIMEOUT 500000
43 Device1394Input::Device1394Input()
52 current_outbuffer = 0;
63 Device1394Input::~Device1394Input()
65 // Driver crashes if it isn't stopped before cancelling the thread.
66 // 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;
105 int Device1394Input::open(const char *path,
116 this->channel = channel;
117 this->length = length;
118 this->channels = channels;
119 this->samplerate = samplerate;
124 buffer_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
125 total_buffers = length;
128 // Initialize grabbing
129 if(fd < 0 && (fd = ::open(path, O_RDWR)) < 0) {
130 printf("Device1394Input::open %s: %s\n", path, strerror(errno));
134 #define CIP_N_NTSC 68000000
135 #define CIP_D_NTSC 1068000000
138 struct dv1394_init init = {
139 api_version: DV1394_API_VERSION,
140 channel: (unsigned int)channel,
141 n_frames: (unsigned int)length,
142 format: is_pal ? DV1394_PAL: DV1394_NTSC,
147 if(ioctl(fd, DV1394_IOC_INIT, &init) < 0) {
148 printf("Device1394Input::open DV1394_IOC_INIT: %s\n", strerror(errno));
153 input_buffer = (unsigned char*)mmap(0, length * buffer_size,
154 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
155 if( input_buffer == MAP_FAILED ) {
156 perror("Device1394Input::open mmap");
162 if(ioctl(fd, DV1394_IOC_START_RECEIVE, 0) < 0) {
163 perror("Device1394Input::open DV1394_START_RECEIVE");
169 buffer = new char*[total_buffers];
170 buffer_valid = new int[total_buffers];
171 bzero(buffer_valid, sizeof(int) * total_buffers);
172 for(int i = 0; i < total_buffers; i++) {
173 buffer[i] = new char[DV_PAL_SIZE];
176 audio_buffer = new char[INPUT_SAMPLES * 2 * channels];
177 audio_lock = new Condition(0, "Device1394Input::audio_lock");
178 video_lock = new Condition(0, "Device1394Input::video_lock");
179 buffer_lock = new Mutex("Device1394Input::buffer_lock");
188 void Device1394Input::run()
193 // Wait for frame to arrive
194 struct dv1394_status status;
195 printf("Device1394Input::run %d done=%d\n", __LINE__, done);
197 Thread::enable_cancel();
198 if(ioctl(fd, DV1394_IOC_WAIT_FRAMES, 1)) {
199 perror("Device1394Input::run DV1394_IOC_WAIT_FRAMES");
202 else if(ioctl(fd, DV1394_IOC_GET_STATUS, &status)) {
203 perror("Device1394Input::run DV1394_IOC_GET_STATUS");
206 else if( !input_buffer ) {
207 fprintf(stderr, "Device1394Input::run !input_buffer");
212 Thread::disable_cancel();
215 buffer_lock->lock("Device1394Input::run 1");
217 int nframes = status.n_clear_frames;
218 for(int i = 0; i < nframes; i++)
220 // Get a buffer to transfer to
223 if(!buffer_valid[current_inbuffer])
224 dst = buffer[current_inbuffer];
228 char *src = (char*)(input_buffer + buffer_size * status.first_clear_frame);
229 // static FILE *test = 0;
230 // if(!test) test = fopen("/tmp/test", "w");
231 // fwrite(src, buffer_size, 1, test);
236 memcpy(dst, src, buffer_size);
237 buffer_valid[current_inbuffer] = 1;
238 video_lock->unlock();
243 if(audio_samples < INPUT_SAMPLES - 2048)
245 int audio_result = dv_read_audio(decoder,
246 (unsigned char*)audio_buffer +
247 audio_samples * 2 * 2,
252 int real_freq = decoder->decoder->audio->frequency;
253 if (real_freq == 32000)
255 // do in-place _FAST_ && _SIMPLE_ upsampling to 48khz
256 // i also think user should get a warning that his material is effectively 32khz
257 // we take 16bit samples for both channels in one 32bit int
258 int *twosample = (int*) (audio_buffer + audio_samples * 2 * 2);
259 int from = audio_result - 1;
260 int new_result = audio_result * 48000 / real_freq;
261 for (int to = new_result - 1; to >=0; to--)
263 if ((to % 3) == 0 || (to % 3) == 1) from --;
264 twosample[to] = twosample[from];
266 audio_result = new_result;
270 audio_samples += audio_result;
273 audio_lock->unlock();
278 increment_counter(¤t_inbuffer);
281 Thread::enable_cancel();
282 if(ioctl(fd, DV1394_IOC_RECEIVE_FRAMES, 1))
284 perror("Device1394Input::run DV1394_IOC_RECEIVE_FRAMES");
287 if(ioctl(fd, DV1394_IOC_GET_STATUS, &status))
289 perror("Device1394Input::run DV1394_IOC_GET_STATUS");
291 Thread::disable_cancel();
294 buffer_lock->unlock();
298 void Device1394Input::increment_counter(int *counter)
301 if(*counter >= total_buffers) *counter = 0;
304 void Device1394Input::decrement_counter(int *counter)
307 if(*counter < 0) *counter = total_buffers - 1;
312 int Device1394Input::read_video(VFrame *data)
316 // Take over buffer table
317 buffer_lock->lock("Device1394Input::read_video 1");
318 // Wait for buffer with timeout
319 while(!buffer_valid[current_outbuffer] && !result)
321 buffer_lock->unlock();
322 result = video_lock->timed_lock(BUFFER_TIMEOUT, "Device1394Input::read_video 2");
323 buffer_lock->lock("Device1394Input::read_video 3");
327 if(buffer_valid[current_outbuffer])
329 data->allocate_compressed_data(buffer_size);
330 data->set_compressed_size(buffer_size);
331 memcpy(data->get_data(), buffer[current_outbuffer], buffer_size);
332 buffer_valid[current_outbuffer] = 0;
333 increment_counter(¤t_outbuffer);
336 buffer_lock->unlock();
343 int Device1394Input::read_audio(char *data, int samples)
346 int timeout = (int64_t)samples * (int64_t)1000000 * (int64_t)2 / (int64_t)samplerate;
347 if(timeout < 500000) timeout = 500000;
349 // Take over buffer table
350 buffer_lock->lock("Device1394Input::read_audio 1");
351 // Wait for buffer with timeout
352 while(audio_samples < samples && !result)
354 buffer_lock->unlock();
355 result = audio_lock->timed_lock(timeout, "Device1394Input::read_audio 2");
356 buffer_lock->lock("Device1394Input::read_audio 3");
358 //printf("Device1394Input::read_audio 1 %d %d\n", result, timeout);
360 if(audio_samples >= samples)
362 memcpy(data, audio_buffer, samples * bits * channels / 8);
364 audio_buffer + samples * bits * channels / 8,
365 (audio_samples - samples) * bits * channels / 8);
366 audio_samples -= samples;
368 //printf("Device1394Input::read_audio 100\n");
369 buffer_lock->unlock();