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
23 #ifdef HAVE_VIDEO4LINUX
25 // V4L2 is incompatible with large file support
26 // ALPHA C++ can't compile 64 bit headers
27 #undef _FILE_OFFSET_BITS
28 #undef _LARGEFILE_SOURCE
29 #undef _LARGEFILE64_SOURCE
33 #include "bcsignals.h"
35 #include "chantables.h"
39 #include "preferences.h"
40 #include "recordconfig.h"
41 #include "vdevicev4l.h"
43 #include "videodevice.h"
46 #include <sys/ioctl.h>
51 VDeviceV4L::VDeviceV4L(VideoDevice *device)
57 VDeviceV4L::~VDeviceV4L()
61 int VDeviceV4L::initialize()
64 capture_frame_number = 0;
65 read_frame_number = 0;
67 initialization_complete = 0;
71 int VDeviceV4L::open_input()
73 device->channel->use_frequency = 1;
74 device->channel->use_fine = 1;
75 device->channel->use_norm = 1;
76 device->channel->use_input = 1;
79 device->picture->use_brightness = 1;
80 device->picture->use_contrast = 1;
81 device->picture->use_color = 1;
82 device->picture->use_hue = 1;
83 device->picture->use_whiteness = 1;
85 if((input_fd = open(device->in_config->v4l_in_device, O_RDWR)) < 0)
87 perror("VDeviceV4L::open_input");
98 int VDeviceV4L::close_all()
104 int VDeviceV4L::close_v4l()
107 if(input_fd != -1) close(input_fd);
111 int VDeviceV4L::unmap_v4l_shmem()
116 munmap(capture_buffer, capture_params.size);
118 delete capture_buffer;
124 int VDeviceV4L::v4l_init()
126 input_fd = open(device->in_config->v4l_in_device, O_RDWR);
129 perror("VDeviceV4L::v4l_init");
132 set_cloexec_flag(input_fd, 1);
134 if(ioctl(input_fd, VIDIOCGWIN, &window_params) < 0)
135 perror("VDeviceV4L::v4l_init VIDIOCGWIN");
138 window_params.width = device->in_config->w;
139 window_params.height = device->in_config->h;
140 window_params.chromakey = 0;
141 window_params.flags = 0;
142 window_params.clipcount = 0;
143 if(ioctl(input_fd, VIDIOCSWIN, &window_params) < 0)
144 perror("VDeviceV4L::v4l_init VIDIOCSWIN");
145 if(ioctl(input_fd, VIDIOCGWIN, &window_params) < 0)
146 perror("VDeviceV4L::v4l_init VIDIOCGWIN");
148 device->in_config->w = window_params.width;
149 device->in_config->h = window_params.height;
151 PictureConfig picture(0);
152 set_picture(&picture);
154 if(ioctl(input_fd, VIDIOCGMBUF, &capture_params) < 0)
155 perror("VDeviceV4L::v4l_init VIDIOCGMBUF");
157 capture_buffer = (char*)mmap(0,
159 PROT_READ|PROT_WRITE,
164 capture_frame_number = 0;
166 if((long)capture_buffer < 0)
169 perror("VDeviceV4L::v4l_init mmap");
171 capture_buffer = new char[capture_params.size];
175 // Get all frames capturing
183 void VDeviceV4L::v4l1_start_capture()
185 for(int i = 0; i < MIN(capture_params.frames, device->in_config->capture_length); i++)
196 int VDeviceV4L::v4l1_get_inputs()
198 struct video_channel channel_struct;
201 while(!done && i < 20)
203 channel_struct.channel = i;
204 if(ioctl(input_fd, VIDIOCGCHAN, &channel_struct) < 0)
211 Channel *channel = new Channel;
212 strcpy(channel->device_name, channel_struct.name);
213 device->input_sources.append(channel);
220 int VDeviceV4L::set_mute(int muted)
222 // Open audio, which obviously is controlled by the video driver.
223 // and apparently resets the input source.
224 v4l1_set_mute(muted);
228 int VDeviceV4L::v4l1_set_mute(int muted)
230 struct video_audio audio;
232 if(ioctl(input_fd, VIDIOCGAUDIO, &audio))
233 if(ioctl(input_fd, VIDIOCGAUDIO, &audio) < 0)
234 perror("VDeviceV4L::ioctl VIDIOCGAUDIO");
236 audio.volume = 65535;
238 audio.treble = 65535;
240 audio.flags |= VIDEO_AUDIO_MUTE | VIDEO_AUDIO_VOLUME;
242 audio.flags &= ~VIDEO_AUDIO_MUTE;
244 if(ioctl(input_fd, VIDIOCSAUDIO, &audio) < 0)
245 perror("VDeviceV4L::ioctl VIDIOCSAUDIO");
250 int VDeviceV4L::set_cloexec_flag(int desc, int value)
252 int oldflags = fcntl(desc, F_GETFD, 0);
253 if(oldflags < 0) return oldflags;
255 oldflags |= FD_CLOEXEC;
257 oldflags &= ~FD_CLOEXEC;
258 return fcntl(desc, F_SETFD, oldflags);
265 int VDeviceV4L::get_best_colormodel(Asset *asset)
267 int result = BC_RGB888;
269 // Get best colormodel for hardware acceleration
271 result = File::get_best_colormodel(asset, device->in_config->driver);
274 // Need to get color model before opening device but don't call this
275 // unless you want to open the device either.
276 if(!initialization_complete)
278 device_colormodel = translate_colormodel(result);
279 this->colormodel = result;
281 initialization_complete = 1;
283 // printf("VDeviceV4L::get_best_colormodel %c%c%c%c\n",
284 // ((char*)&device_colormodel)[0],
285 // ((char*)&device_colormodel)[1],
286 // ((char*)&device_colormodel)[2],
287 // ((char*)&device_colormodel)[3]);
291 unsigned long VDeviceV4L::translate_colormodel(int colormodel)
293 unsigned long result = 0;
296 case BC_YUV422: result = VIDEO_PALETTE_YUV422; break;
297 case BC_YUV420P: result = VIDEO_PALETTE_YUV420P; break;
298 case BC_YUV422P: result = VIDEO_PALETTE_YUV422P; break;
299 case BC_YUV411P: result = VIDEO_PALETTE_YUV411P; break;
300 case BC_RGB888: result = VIDEO_PALETTE_RGB24; break;
301 default: result = VIDEO_PALETTE_RGB24; break;
303 //printf("VDeviceV4L::translate_colormodel %d\n", result);
307 int VDeviceV4L::set_channel(Channel *channel)
309 return v4l1_set_channel(channel);
312 int VDeviceV4L::v4l1_set_channel(Channel *channel)
314 struct video_channel channel_struct;
315 struct video_tuner tuner_struct;
316 unsigned long new_freq;
318 // Mute changed the input to TV
321 //printf("VDeviceV4L::v4l1_set_channel 1 %d\n", channel->input);
322 // Read norm/input defaults
323 channel_struct.channel = channel->input;
324 if(ioctl(input_fd, VIDIOCGCHAN, &channel_struct) < 0)
325 perror("VDeviceV4L::v4l1_set_channel VIDIOCGCHAN");
328 channel_struct.channel = channel->input;
329 channel_struct.norm = v4l1_get_norm(channel->norm);
330 if(ioctl(input_fd, VIDIOCSCHAN, &channel_struct) < 0)
331 perror("VDeviceV4L::v4l1_set_channel VIDIOCSCHAN");
333 if(channel_struct.flags & VIDEO_VC_TUNER)
335 // Read tuner defaults
336 tuner_struct.tuner = channel->input;
337 if(ioctl(input_fd, VIDIOCGTUNER, &tuner_struct) < 0)
338 perror("VDeviceV4L::v4l1_set_channel VIDIOCGTUNER");
341 tuner_struct.mode = v4l1_get_norm(channel->norm);
342 if(ioctl(input_fd, VIDIOCSTUNER, &tuner_struct) < 0)
343 perror("VDeviceV4L::v4l1_set_channel VIDIOCSTUNER");
345 new_freq = chanlists[channel->freqtable].list[channel->entry].freq;
346 new_freq = (int)(new_freq * 0.016);
347 new_freq += channel->fine_tune;
349 if(ioctl(input_fd, VIDIOCSFREQ, &new_freq) < 0)
350 perror("VDeviceV4L::v4l1_set_channel VIDIOCSFREQ");
356 int VDeviceV4L::v4l1_get_norm(int norm)
360 case NTSC: return VIDEO_MODE_NTSC; break;
361 case PAL: return VIDEO_MODE_PAL; break;
362 case SECAM: return VIDEO_MODE_SECAM; break;
367 int VDeviceV4L::set_picture(PictureConfig *picture)
369 v4l1_set_picture(picture);
374 int VDeviceV4L::v4l1_set_picture(PictureConfig *picture)
376 int brightness = (int)((float)picture->brightness / 100 * 32767 + 32768);
377 int hue = (int)((float)picture->hue / 100 * 32767 + 32768);
378 int color = (int)((float)picture->color / 100 * 32767 + 32768);
379 int contrast = (int)((float)picture->contrast / 100 * 32767 + 32768);
380 int whiteness = (int)((float)picture->whiteness / 100 * 32767 + 32768);
382 if(ioctl(input_fd, VIDIOCGPICT, &picture_params) < 0)
383 perror("VDeviceV4L::v4l1_set_picture VIDIOCGPICT");
384 picture_params.brightness = brightness;
385 picture_params.hue = hue;
386 picture_params.colour = color;
387 picture_params.contrast = contrast;
388 picture_params.whiteness = whiteness;
389 // Bogus. Values are only set in the capture routine.
390 picture_params.depth = 3;
391 picture_params.palette = device_colormodel;
392 if(ioctl(input_fd, VIDIOCSPICT, &picture_params) < 0)
393 perror("VDeviceV4L::v4l1_set_picture VIDIOCSPICT");
394 if(ioctl(input_fd, VIDIOCGPICT, &picture_params) < 0)
395 perror("VDeviceV4L::v4l1_set_picture VIDIOCGPICT");
400 int VDeviceV4L::capture_frame(int capture_frame_number)
402 struct video_mmap params;
403 params.frame = capture_frame_number;
404 params.width = device->in_config->w;
405 params.height = device->in_config->h;
406 // Required to actually set the palette.
407 params.format = device_colormodel;
408 // Tells the driver the buffer is available for writing
409 if(ioctl(input_fd, VIDIOCMCAPTURE, ¶ms) < 0)
410 perror("VDeviceV4L::capture_frame VIDIOCMCAPTURE");
414 int VDeviceV4L::wait_v4l_frame()
416 //printf("VDeviceV4L::wait_v4l_frame 1 %d\n", capture_frame_number);
417 if(ioctl(input_fd, VIDIOCSYNC, &capture_frame_number))
418 perror("VDeviceV4L::wait_v4l_frame VIDIOCSYNC");
419 //printf("VDeviceV4L::wait_v4l_frame 2 %d\n", capture_frame_number);
423 int VDeviceV4L::read_v4l_frame(VFrame *frame)
425 frame_to_vframe(frame, (unsigned char*)capture_buffer + capture_params.offsets[capture_frame_number]);
430 #define MIN(x, y) ((x) < (y) ? (x) : (y))
433 int VDeviceV4L::frame_to_vframe(VFrame *frame, unsigned char *input)
435 int inwidth, inheight;
438 inwidth = window_params.width;
439 inheight = window_params.height;
441 width = MIN(inwidth, frame->get_w());
442 height = MIN(inheight, frame->get_h());
443 //printf("VDeviceV4L::frame_to_vframe %d %d\n", colormodel, frame->get_color_model());
445 if(frame->get_color_model() == colormodel)
447 switch(frame->get_color_model())
451 unsigned char *row_in;
452 unsigned char *row_out_start, *row_out_end;
453 int bytes_per_inrow = inwidth * 3;
454 int bytes_per_outrow = frame->get_bytes_per_line();
455 unsigned char **rows_out = frame->get_rows();
457 for(int i = 0; i < frame->get_h(); i++)
459 row_in = input + bytes_per_inrow * i;
460 row_out_start = rows_out[i];
461 row_out_end = row_out_start +
462 MIN(bytes_per_outrow, bytes_per_inrow);
464 while(row_out_start < row_out_end)
466 *row_out_start++ = row_in[2];
467 *row_out_start++ = row_in[1];
468 *row_out_start++ = row_in[0];
477 memcpy(frame->get_y(), input, width * height);
478 memcpy(frame->get_u(), input + width * height, width * height / 4);
479 memcpy(frame->get_v(), input + width * height + width * height / 4, width * height / 4);
483 memcpy(frame->get_y(), input, width * height);
484 memcpy(frame->get_u(), input + width * height, width * height / 2);
485 memcpy(frame->get_v(), input + width * height + width * height / 2, width * height / 2);
489 memcpy(frame->get_data(),
491 VFrame::calculate_data_size(width,
494 frame->get_color_model()));
500 VFrame *in_frame = new VFrame(input,
506 BC_CModels::transfer(frame->get_rows(),
507 in_frame->get_rows(),
523 frame->get_color_model(),
533 int VDeviceV4L::next_frame(int previous_frame)
535 int result = previous_frame + 1;
537 if(result >= MIN(capture_params.frames, device->in_config->capture_length)) result = 0;
541 int VDeviceV4L::read_buffer(VFrame *frame)
546 // Read the current frame
547 if(!got_first_frame) v4l1_start_capture();
549 read_v4l_frame(frame);
550 // Free this frame up for capturing
551 capture_frame(capture_frame_number);
552 // Advance the frame to capture.
553 capture_frame_number = next_frame(capture_frame_number);
557 read(input_fd, capture_buffer, capture_params.size);
568 #endif // HAVE_VIDEO4LINUX