| 1 | | |
| 2 | | /* |
| 3 | | * CINELERRA |
| 4 | | * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net> |
| 5 | | * |
| 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. |
| 10 | | * |
| 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. |
| 15 | | * |
| 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 |
| 19 | | * |
| 20 | | */ |
| 21 | | |
| 22 | | // ALPHA C++ can't compile 64 bit headers |
| 23 | | #undef _LARGEFILE_SOURCE |
| 24 | | #undef _LARGEFILE64_SOURCE |
| 25 | | #undef _FILE_OFFSET_BITS |
| 26 | | |
| 27 | | #include "assets.h" |
| 28 | | #include "bcsignals.h" |
| 29 | | #include "channel.h" |
| 30 | | #include "chantables.h" |
| 31 | | #include "condition.h" |
| 32 | | #include "file.inc" |
| 33 | | #include "mutex.h" |
| 34 | | #include "picture.h" |
| 35 | | #include "playbackconfig.h" |
| 36 | | #include "preferences.h" |
| 37 | | #include "recordconfig.h" |
| 38 | | #include "strategies.inc" |
| 39 | | #include "vdevicebuz.h" |
| 40 | | #include "vframe.h" |
| 41 | | #include "videoconfig.h" |
| 42 | | #include "videodevice.h" |
| 43 | | |
| 44 | | #include <errno.h> |
| 45 | | #include <stdint.h> |
| 46 | | #include <linux/kernel.h> |
| 47 | | //#include <linux/videodev2.h> |
| 48 | | #include <linux/videodev.h> |
| 49 | | #include <fcntl.h> |
| 50 | | #include <sys/ioctl.h> |
| 51 | | #include <sys/mman.h> |
| 52 | | #include <unistd.h> |
| 53 | | |
| 54 | | |
| 55 | | |
| 56 | | #define READ_TIMEOUT 5000000 |
| 57 | | |
| 58 | | |
| 59 | | VDeviceBUZInput::VDeviceBUZInput(VDeviceBUZ *device) |
| 60 | | : Thread(1, 1, 0) |
| 61 | | { |
| 62 | | this->device = device; |
| 63 | | buffer = 0; |
| 64 | | buffer_size = 0; |
| 65 | | total_buffers = 0; |
| 66 | | current_inbuffer = 0; |
| 67 | | current_outbuffer = 0; |
| 68 | | done = 0; |
| 69 | | output_lock = new Condition(0, "VDeviceBUZInput::output_lock"); |
| 70 | | buffer_lock = new Mutex("VDeviceBUZInput::buffer_lock"); |
| 71 | | } |
| 72 | | |
| 73 | | VDeviceBUZInput::~VDeviceBUZInput() |
| 74 | | { |
| 75 | | if(Thread::running()) |
| 76 | | { |
| 77 | | done = 1; |
| 78 | | Thread::cancel(); |
| 79 | | Thread::join(); |
| 80 | | } |
| 81 | | |
| 82 | | if(buffer) |
| 83 | | { |
| 84 | | for(int i = 0; i < total_buffers; i++) |
| 85 | | { |
| 86 | | delete [] buffer[i]; |
| 87 | | } |
| 88 | | delete [] buffer; |
| 89 | | delete [] buffer_size; |
| 90 | | } |
| 91 | | delete output_lock; |
| 92 | | delete buffer_lock; |
| 93 | | } |
| 94 | | |
| 95 | | void VDeviceBUZInput::start() |
| 96 | | { |
| 97 | | // Create buffers |
| 98 | | total_buffers = device->device->in_config->capture_length; |
| 99 | | buffer = new char*[total_buffers]; |
| 100 | | buffer_size = new int[total_buffers]; |
| 101 | | bzero(buffer_size, sizeof(int) * total_buffers); |
| 102 | | for(int i = 0; i < total_buffers; i++) |
| 103 | | { |
| 104 | | buffer[i] = new char[INPUT_BUFFER_SIZE]; |
| 105 | | } |
| 106 | | |
| 107 | | Thread::start(); |
| 108 | | } |
| 109 | | |
| 110 | | void VDeviceBUZInput::run() |
| 111 | | { |
| 112 | | struct buz_sync bsync; |
| 113 | | |
| 114 | | // Wait for frame |
| 115 | | while(1) |
| 116 | | { |
| 117 | | Thread::enable_cancel(); |
| 118 | | if(ioctl(device->jvideo_fd, BUZIOC_SYNC, &bsync) < 0) |
| 119 | | { |
| 120 | | perror("VDeviceBUZInput::run BUZIOC_SYNC"); |
| 121 | | if(done) return; |
| 122 | | Thread::disable_cancel(); |
| 123 | | } |
| 124 | | else |
| 125 | | { |
| 126 | | Thread::disable_cancel(); |
| 127 | | |
| 128 | | |
| 129 | | |
| 130 | | int new_buffer = 0; |
| 131 | | buffer_lock->lock("VDeviceBUZInput::run"); |
| 132 | | // Save only if the current buffer is free. |
| 133 | | if(!buffer_size[current_inbuffer]) |
| 134 | | { |
| 135 | | new_buffer = 1; |
| 136 | | // Copy to input buffer |
| 137 | | memcpy(buffer[current_inbuffer], |
| 138 | | device->input_buffer + bsync.frame * device->breq.size, |
| 139 | | bsync.length); |
| 140 | | |
| 141 | | // Advance input buffer number and decrease semaphore. |
| 142 | | buffer_size[current_inbuffer] = bsync.length; |
| 143 | | increment_counter(¤t_inbuffer); |
| 144 | | } |
| 145 | | |
| 146 | | buffer_lock->unlock(); |
| 147 | | |
| 148 | | if(ioctl(device->jvideo_fd, BUZIOC_QBUF_CAPT, &bsync.frame)) |
| 149 | | perror("VDeviceBUZInput::run BUZIOC_QBUF_CAPT"); |
| 150 | | |
| 151 | | if(new_buffer) output_lock->unlock(); |
| 152 | | } |
| 153 | | } |
| 154 | | } |
| 155 | | |
| 156 | | void VDeviceBUZInput::get_buffer(char **ptr, int *size) |
| 157 | | { |
| 158 | | // Increase semaphore to wait for buffer. |
| 159 | | int result = output_lock->timed_lock(READ_TIMEOUT, "VDeviceBUZInput::get_buffer"); |
| 160 | | |
| 161 | | |
| 162 | | // The driver has its own timeout routine but it doesn't work because |
| 163 | | // because the tuner lock is unlocked and relocked with no delay. |
| 164 | | // int result = 0; |
| 165 | | // output_lock->lock("VDeviceBUZInput::get_buffer"); |
| 166 | | |
| 167 | | if(!result) |
| 168 | | { |
| 169 | | // Take over buffer table |
| 170 | | buffer_lock->lock("VDeviceBUZInput::get_buffer"); |
| 171 | | *ptr = buffer[current_outbuffer]; |
| 172 | | *size = buffer_size[current_outbuffer]; |
| 173 | | buffer_lock->unlock(); |
| 174 | | } |
| 175 | | else |
| 176 | | { |
| 177 | | //printf("VDeviceBUZInput::get_buffer 1\n"); |
| 178 | | output_lock->unlock(); |
| 179 | | } |
| 180 | | } |
| 181 | | |
| 182 | | void VDeviceBUZInput::put_buffer() |
| 183 | | { |
| 184 | | buffer_lock->lock("VDeviceBUZInput::put_buffer"); |
| 185 | | buffer_size[current_outbuffer] = 0; |
| 186 | | buffer_lock->unlock(); |
| 187 | | increment_counter(¤t_outbuffer); |
| 188 | | } |
| 189 | | |
| 190 | | void VDeviceBUZInput::increment_counter(int *counter) |
| 191 | | { |
| 192 | | (*counter)++; |
| 193 | | if(*counter >= total_buffers) *counter = 0; |
| 194 | | } |
| 195 | | |
| 196 | | void VDeviceBUZInput::decrement_counter(int *counter) |
| 197 | | { |
| 198 | | (*counter)--; |
| 199 | | if(*counter < 0) *counter = total_buffers - 1; |
| 200 | | } |
| 201 | | |
| 202 | | |
| 203 | | |
| 204 | | |
| 205 | | |
| 206 | | |
| 207 | | |
| 208 | | |
| 209 | | |
| 210 | | |
| 211 | | |
| 212 | | |
| 213 | | |
| 214 | | |
| 215 | | |
| 216 | | VDeviceBUZ::VDeviceBUZ(VideoDevice *device) |
| 217 | | : VDeviceBase(device) |
| 218 | | { |
| 219 | | reset_parameters(); |
| 220 | | render_strategies.append(VRENDER_MJPG); |
| 221 | | tuner_lock = new Mutex("VDeviceBUZ::tuner_lock"); |
| 222 | | } |
| 223 | | |
| 224 | | VDeviceBUZ::~VDeviceBUZ() |
| 225 | | { |
| 226 | | close_all(); |
| 227 | | delete tuner_lock; |
| 228 | | } |
| 229 | | |
| 230 | | int VDeviceBUZ::reset_parameters() |
| 231 | | { |
| 232 | | jvideo_fd = 0; |
| 233 | | input_buffer = 0; |
| 234 | | output_buffer = 0; |
| 235 | | frame_buffer = 0; |
| 236 | | frame_size = 0; |
| 237 | | frame_allocated = 0; |
| 238 | | input_error = 0; |
| 239 | | last_frame_no = 0; |
| 240 | | temp_frame = 0; |
| 241 | | user_frame = 0; |
| 242 | | mjpeg = 0; |
| 243 | | total_loops = 0; |
| 244 | | output_number = 0; |
| 245 | | input_thread = 0; |
| 246 | | brightness = 32768; |
| 247 | | hue = 32768; |
| 248 | | color = 32768; |
| 249 | | contrast = 32768; |
| 250 | | whiteness = 32768; |
| 251 | | } |
| 252 | | |
| 253 | | int VDeviceBUZ::close_input_core() |
| 254 | | { |
| 255 | | if(input_thread) |
| 256 | | { |
| 257 | | delete input_thread; |
| 258 | | input_thread = 0; |
| 259 | | } |
| 260 | | |
| 261 | | |
| 262 | | if(device->r) |
| 263 | | { |
| 264 | | if(jvideo_fd) close(jvideo_fd); |
| 265 | | jvideo_fd = 0; |
| 266 | | } |
| 267 | | |
| 268 | | if(input_buffer) |
| 269 | | { |
| 270 | | if(input_buffer > 0) |
| 271 | | munmap(input_buffer, breq.count * breq.size); |
| 272 | | input_buffer = 0; |
| 273 | | } |
| 274 | | } |
| 275 | | |
| 276 | | int VDeviceBUZ::close_output_core() |
| 277 | | { |
| 278 | | //printf("VDeviceBUZ::close_output_core 1\n"); |
| 279 | | if(device->w) |
| 280 | | { |
| 281 | | int n = -1; |
| 282 | | // if(ioctl(jvideo_fd, BUZIOC_QBUF_PLAY, &n) < 0) |
| 283 | | // perror("VDeviceBUZ::close_output_core BUZIOC_QBUF_PLAY"); |
| 284 | | if(jvideo_fd) close(jvideo_fd); |
| 285 | | jvideo_fd = 0; |
| 286 | | } |
| 287 | | if(output_buffer) |
| 288 | | { |
| 289 | | if(output_buffer > 0) |
| 290 | | munmap(output_buffer, breq.count * breq.size); |
| 291 | | output_buffer = 0; |
| 292 | | } |
| 293 | | if(temp_frame) |
| 294 | | { |
| 295 | | delete temp_frame; |
| 296 | | temp_frame = 0; |
| 297 | | } |
| 298 | | if(mjpeg) |
| 299 | | { |
| 300 | | mjpeg_delete(mjpeg); |
| 301 | | mjpeg = 0; |
| 302 | | } |
| 303 | | if(user_frame) |
| 304 | | { |
| 305 | | delete user_frame; |
| 306 | | user_frame = 0; |
| 307 | | } |
| 308 | | //printf("VDeviceBUZ::close_output_core 2\n"); |
| 309 | | return 0; |
| 310 | | } |
| 311 | | |
| 312 | | |
| 313 | | int VDeviceBUZ::close_all() |
| 314 | | { |
| 315 | | //printf("VDeviceBUZ::close_all 1\n"); |
| 316 | | close_input_core(); |
| 317 | | //printf("VDeviceBUZ::close_all 1\n"); |
| 318 | | close_output_core(); |
| 319 | | //printf("VDeviceBUZ::close_all 1\n"); |
| 320 | | if(frame_buffer) delete frame_buffer; |
| 321 | | //printf("VDeviceBUZ::close_all 1\n"); |
| 322 | | reset_parameters(); |
| 323 | | //printf("VDeviceBUZ::close_all 2\n"); |
| 324 | | return 0; |
| 325 | | } |
| 326 | | |
| 327 | | #define COMPOSITE_TEXT "Composite" |
| 328 | | #define SVIDEO_TEXT "S-Video" |
| 329 | | #define BUZ_COMPOSITE 0 |
| 330 | | #define BUZ_SVIDEO 1 |
| 331 | | |
| 332 | | void VDeviceBUZ::get_inputs(ArrayList<Channel*> *input_sources) |
| 333 | | { |
| 334 | | Channel *new_source = new Channel; |
| 335 | | |
| 336 | | new_source = new Channel; |
| 337 | | strcpy(new_source->device_name, COMPOSITE_TEXT); |
| 338 | | input_sources->append(new_source); |
| 339 | | |
| 340 | | new_source = new Channel; |
| 341 | | strcpy(new_source->device_name, SVIDEO_TEXT); |
| 342 | | input_sources->append(new_source); |
| 343 | | } |
| 344 | | |
| 345 | | int VDeviceBUZ::open_input() |
| 346 | | { |
| 347 | | device->channel->use_norm = 1; |
| 348 | | device->channel->use_input = 1; |
| 349 | | |
| 350 | | device->picture->use_brightness = 1; |
| 351 | | device->picture->use_contrast = 1; |
| 352 | | device->picture->use_color = 1; |
| 353 | | device->picture->use_hue = 1; |
| 354 | | device->picture->use_whiteness = 1; |
| 355 | | |
| 356 | | // Can't open input until after the channel is set |
| 357 | | return 0; |
| 358 | | } |
| 359 | | |
| 360 | | int VDeviceBUZ::open_output() |
| 361 | | { |
| 362 | | // Can't open output until after the channel is set |
| 363 | | return 0; |
| 364 | | } |
| 365 | | |
| 366 | | int VDeviceBUZ::set_channel(Channel *channel) |
| 367 | | { |
| 368 | | if(!channel) return 0; |
| 369 | | |
| 370 | | tuner_lock->lock("VDeviceBUZ::set_channel"); |
| 371 | | |
| 372 | | if(device->r) |
| 373 | | { |
| 374 | | close_input_core(); |
| 375 | | open_input_core(channel); |
| 376 | | } |
| 377 | | else |
| 378 | | { |
| 379 | | close_output_core(); |
| 380 | | open_output_core(channel); |
| 381 | | } |
| 382 | | |
| 383 | | tuner_lock->unlock(); |
| 384 | | |
| 385 | | |
| 386 | | return 0; |
| 387 | | } |
| 388 | | |
| 389 | | void VDeviceBUZ::create_channeldb(ArrayList<Channel*> *channeldb) |
| 390 | | { |
| 391 | | ; |
| 392 | | } |
| 393 | | |
| 394 | | int VDeviceBUZ::set_picture(PictureConfig *picture) |
| 395 | | { |
| 396 | | this->brightness = (int)((float)picture->brightness / 100 * 32767 + 32768); |
| 397 | | this->hue = (int)((float)picture->hue / 100 * 32767 + 32768); |
| 398 | | this->color = (int)((float)picture->color / 100 * 32767 + 32768); |
| 399 | | this->contrast = (int)((float)picture->contrast / 100 * 32767 + 32768); |
| 400 | | this->whiteness = (int)((float)picture->whiteness / 100 * 32767 + 32768); |
| 401 | | |
| 402 | | |
| 403 | | tuner_lock->lock("VDeviceBUZ::set_picture"); |
| 404 | | if(device->r) |
| 405 | | { |
| 406 | | close_input_core(); |
| 407 | | open_input_core(0); |
| 408 | | } |
| 409 | | else |
| 410 | | { |
| 411 | | close_output_core(); |
| 412 | | open_output_core(0); |
| 413 | | } |
| 414 | | tuner_lock->unlock(); |
| 415 | | // |
| 416 | | // |
| 417 | | // TRACE("VDeviceBUZ::set_picture 1"); |
| 418 | | // tuner_lock->lock("VDeviceBUZ::set_picture"); |
| 419 | | // TRACE("VDeviceBUZ::set_picture 2"); |
| 420 | | // |
| 421 | | // |
| 422 | | // |
| 423 | | // struct video_picture picture_params; |
| 424 | | // // This call takes a long time in 2.4.22 |
| 425 | | // if(ioctl(jvideo_fd, VIDIOCGPICT, &picture_params) < 0) |
| 426 | | // perror("VDeviceBUZ::set_picture VIDIOCGPICT"); |
| 427 | | // picture_params.brightness = brightness; |
| 428 | | // picture_params.hue = hue; |
| 429 | | // picture_params.colour = color; |
| 430 | | // picture_params.contrast = contrast; |
| 431 | | // picture_params.whiteness = whiteness; |
| 432 | | // // This call takes a long time in 2.4.22 |
| 433 | | // if(ioctl(jvideo_fd, VIDIOCSPICT, &picture_params) < 0) |
| 434 | | // perror("VDeviceBUZ::set_picture VIDIOCSPICT"); |
| 435 | | // if(ioctl(jvideo_fd, VIDIOCGPICT, &picture_params) < 0) |
| 436 | | // perror("VDeviceBUZ::set_picture VIDIOCGPICT"); |
| 437 | | // |
| 438 | | // |
| 439 | | // TRACE("VDeviceBUZ::set_picture 10"); |
| 440 | | // |
| 441 | | // |
| 442 | | // tuner_lock->unlock(); |
| 443 | | |
| 444 | | return 0; |
| 445 | | } |
| 446 | | |
| 447 | | int VDeviceBUZ::get_norm(int norm) |
| 448 | | { |
| 449 | | switch(norm) |
| 450 | | { |
| 451 | | case NTSC: return VIDEO_MODE_NTSC; break; |
| 452 | | case PAL: return VIDEO_MODE_PAL; break; |
| 453 | | case SECAM: return VIDEO_MODE_SECAM; break; |
| 454 | | } |
| 455 | | } |
| 456 | | |
| 457 | | int VDeviceBUZ::read_buffer(VFrame *frame) |
| 458 | | { |
| 459 | | tuner_lock->lock("VDeviceBUZ::read_buffer"); |
| 460 | | if(!jvideo_fd) open_input_core(0); |
| 461 | | |
| 462 | | // Get buffer from thread |
| 463 | | char *buffer = 0; |
| 464 | | int buffer_size = 0; |
| 465 | | if(input_thread) |
| 466 | | input_thread->get_buffer(&buffer, &buffer_size); |
| 467 | | |
| 468 | | if(buffer) |
| 469 | | { |
| 470 | | frame->allocate_compressed_data(buffer_size); |
| 471 | | frame->set_compressed_size(buffer_size); |
| 472 | | |
| 473 | | // Transfer fields to frame |
| 474 | | if(device->odd_field_first) |
| 475 | | { |
| 476 | | long field2_offset = mjpeg_get_field2((unsigned char*)buffer, buffer_size); |
| 477 | | long field1_len = field2_offset; |
| 478 | | long field2_len = buffer_size - field2_offset; |
| 479 | | |
| 480 | | memcpy(frame->get_data(), buffer + field2_offset, field2_len); |
| 481 | | memcpy(frame->get_data() + field2_len, buffer, field1_len); |
| 482 | | } |
| 483 | | else |
| 484 | | { |
| 485 | | bcopy(buffer, frame->get_data(), buffer_size); |
| 486 | | } |
| 487 | | |
| 488 | | input_thread->put_buffer(); |
| 489 | | tuner_lock->unlock(); |
| 490 | | } |
| 491 | | else |
| 492 | | { |
| 493 | | tuner_lock->unlock(); |
| 494 | | Timer timer; |
| 495 | | // Allow other threads to lock the tuner_lock under NPTL. |
| 496 | | timer.delay(100); |
| 497 | | } |
| 498 | | |
| 499 | | |
| 500 | | return 0; |
| 501 | | } |
| 502 | | |
| 503 | | int VDeviceBUZ::open_input_core(Channel *channel) |
| 504 | | { |
| 505 | | jvideo_fd = open(device->in_config->buz_in_device, O_RDONLY); |
| 506 | | |
| 507 | | if(jvideo_fd <= 0) |
| 508 | | { |
| 509 | | fprintf(stderr, "VDeviceBUZ::open_input %s: %s\n", |
| 510 | | device->in_config->buz_in_device, |
| 511 | | strerror(errno)); |
| 512 | | jvideo_fd = 0; |
| 513 | | return 1; |
| 514 | | } |
| 515 | | |
| 516 | | // Create input sources |
| 517 | | get_inputs(&device->input_sources); |
| 518 | | |
| 519 | | // Set current input source |
| 520 | | if(channel) |
| 521 | | { |
| 522 | | for(int i = 0; i < 2; i++) |
| 523 | | { |
| 524 | | struct video_channel vch; |
| 525 | | vch.channel = channel->input; |
| 526 | | vch.norm = get_norm(channel->norm); |
| 527 | | |
| 528 | | //printf("VDeviceBUZ::open_input_core 2 %d %d\n", vch.channel, vch.norm); |
| 529 | | if(ioctl(jvideo_fd, VIDIOCSCHAN, &vch) < 0) |
| 530 | | perror("VDeviceBUZ::open_input_core VIDIOCSCHAN "); |
| 531 | | } |
| 532 | | } |
| 533 | | |
| 534 | | |
| 535 | | // Throw away |
| 536 | | // struct video_capability vc; |
| 537 | | // if(ioctl(jvideo_fd, VIDIOCGCAP, &vc) < 0) |
| 538 | | // perror("VDeviceBUZ::open_input VIDIOCGCAP"); |
| 539 | | |
| 540 | | // API dependant initialization |
| 541 | | if(ioctl(jvideo_fd, BUZIOC_G_PARAMS, &bparm) < 0) |
| 542 | | perror("VDeviceBUZ::open_input BUZIOC_G_PARAMS"); |
| 543 | | |
| 544 | | bparm.HorDcm = 1; |
| 545 | | bparm.VerDcm = 1; |
| 546 | | bparm.TmpDcm = 1; |
| 547 | | bparm.field_per_buff = 2; |
| 548 | | bparm.img_width = device->in_config->w; |
| 549 | | bparm.img_height = device->in_config->h / bparm.field_per_buff; |
| 550 | | bparm.img_x = 0; |
| 551 | | bparm.img_y = 0; |
| 552 | | // bparm.APPn = 0; |
| 553 | | // bparm.APP_len = 14; |
| 554 | | bparm.APP_len = 0; |
| 555 | | bparm.odd_even = 0; |
| 556 | | bparm.decimation = 0; |
| 557 | | bparm.quality = device->quality; |
| 558 | | bzero(bparm.APP_data, sizeof(bparm.APP_data)); |
| 559 | | |
| 560 | | if(ioctl(jvideo_fd, BUZIOC_S_PARAMS, &bparm) < 0) |
| 561 | | perror("VDeviceBUZ::open_input BUZIOC_S_PARAMS"); |
| 562 | | |
| 563 | | // printf("open_input %d %d %d %d %d %d %d %d %d %d %d %d\n", |
| 564 | | // bparm.HorDcm, |
| 565 | | // bparm.VerDcm, |
| 566 | | // bparm.TmpDcm, |
| 567 | | // bparm.field_per_buff, |
| 568 | | // bparm.img_width, |
| 569 | | // bparm.img_height, |
| 570 | | // bparm.img_x, |
| 571 | | // bparm.img_y, |
| 572 | | // bparm.APP_len, |
| 573 | | // bparm.odd_even, |
| 574 | | // bparm.decimation, |
| 575 | | // bparm.quality); |
| 576 | | |
| 577 | | breq.count = device->in_config->capture_length; |
| 578 | | breq.size = INPUT_BUFFER_SIZE; |
| 579 | | if(ioctl(jvideo_fd, BUZIOC_REQBUFS, &breq) < 0) |
| 580 | | perror("VDeviceBUZ::open_input BUZIOC_REQBUFS"); |
| 581 | | |
| 582 | | //printf("open_input %s %d %d %d %d\n", device->in_config->buz_in_device, breq.count, breq.size, bparm.img_width, bparm.img_height); |
| 583 | | if((input_buffer = (char*)mmap(0, |
| 584 | | breq.count * breq.size, |
| 585 | | PROT_READ, |
| 586 | | MAP_SHARED, |
| 587 | | jvideo_fd, |
| 588 | | 0)) == MAP_FAILED) |
| 589 | | perror("VDeviceBUZ::open_input mmap"); |
| 590 | | |
| 591 | | |
| 592 | | // Set picture quality |
| 593 | | struct video_picture picture_params; |
| 594 | | // This call takes a long time in 2.4.22 |
| 595 | | if(ioctl(jvideo_fd, VIDIOCGPICT, &picture_params) < 0) |
| 596 | | perror("VDeviceBUZ::set_picture VIDIOCGPICT"); |
| 597 | | picture_params.brightness = brightness; |
| 598 | | picture_params.hue = hue; |
| 599 | | picture_params.colour = color; |
| 600 | | picture_params.contrast = contrast; |
| 601 | | picture_params.whiteness = whiteness; |
| 602 | | // This call takes a long time in 2.4.22 |
| 603 | | if(ioctl(jvideo_fd, VIDIOCSPICT, &picture_params) < 0) |
| 604 | | perror("VDeviceBUZ::set_picture VIDIOCSPICT"); |
| 605 | | if(ioctl(jvideo_fd, VIDIOCGPICT, &picture_params) < 0) |
| 606 | | perror("VDeviceBUZ::set_picture VIDIOCGPICT"); |
| 607 | | |
| 608 | | |
| 609 | | |
| 610 | | |
| 611 | | // Start capturing |
| 612 | | for(int i = 0; i < breq.count; i++) |
| 613 | | { |
| 614 | | if(ioctl(jvideo_fd, BUZIOC_QBUF_CAPT, &i) < 0) |
| 615 | | perror("VDeviceBUZ::open_input BUZIOC_QBUF_CAPT"); |
| 616 | | } |
| 617 | | |
| 618 | | |
| 619 | | input_thread = new VDeviceBUZInput(this); |
| 620 | | input_thread->start(); |
| 621 | | //printf("VDeviceBUZ::open_input_core 2\n"); |
| 622 | | return 0; |
| 623 | | } |
| 624 | | |
| 625 | | int VDeviceBUZ::open_output_core(Channel *channel) |
| 626 | | { |
| 627 | | //printf("VDeviceBUZ::open_output 1\n"); |
| 628 | | total_loops = 0; |
| 629 | | output_number = 0; |
| 630 | | jvideo_fd = open(device->out_config->buz_out_device, O_RDWR); |
| 631 | | if(jvideo_fd <= 0) |
| 632 | | { |
| 633 | | perror("VDeviceBUZ::open_output"); |
| 634 | | return 1; |
| 635 | | } |
| 636 | | |
| 637 | | |
| 638 | | // Set current input source |
| 639 | | if(channel) |
| 640 | | { |
| 641 | | struct video_channel vch; |
| 642 | | vch.channel = channel->input; |
| 643 | | vch.norm = get_norm(channel->norm); |
| 644 | | |
| 645 | | if(ioctl(jvideo_fd, VIDIOCSCHAN, &vch) < 0) |
| 646 | | perror("VDeviceBUZ::open_output_core VIDIOCSCHAN "); |
| 647 | | } |
| 648 | | |
| 649 | | breq.count = 10; |
| 650 | | breq.size = INPUT_BUFFER_SIZE; |
| 651 | | if(ioctl(jvideo_fd, BUZIOC_REQBUFS, &breq) < 0) |
| 652 | | perror("VDeviceBUZ::open_output BUZIOC_REQBUFS"); |
| 653 | | if((output_buffer = (char*)mmap(0, |
| 654 | | breq.count * breq.size, |
| 655 | | PROT_READ | PROT_WRITE, |
| 656 | | MAP_SHARED, |
| 657 | | jvideo_fd, |
| 658 | | 0)) == MAP_FAILED) |
| 659 | | perror("VDeviceBUZ::open_output mmap"); |
| 660 | | |
| 661 | | if(ioctl(jvideo_fd, BUZIOC_G_PARAMS, &bparm) < 0) |
| 662 | | perror("VDeviceBUZ::open_output BUZIOC_G_PARAMS"); |
| 663 | | |
| 664 | | bparm.decimation = 1; |
| 665 | | bparm.HorDcm = 1; |
| 666 | | bparm.field_per_buff = 2; |
| 667 | | bparm.TmpDcm = 1; |
| 668 | | bparm.VerDcm = 1; |
| 669 | | bparm.img_width = device->out_w; |
| 670 | | bparm.img_height = device->out_h / bparm.field_per_buff; |
| 671 | | bparm.img_x = 0; |
| 672 | | bparm.img_y = 0; |
| 673 | | bparm.odd_even = 0; |
| 674 | | |
| 675 | | if(ioctl(jvideo_fd, BUZIOC_S_PARAMS, &bparm) < 0) |
| 676 | | perror("VDeviceBUZ::open_output BUZIOC_S_PARAMS"); |
| 677 | | //printf("VDeviceBUZ::open_output 2\n"); |
| 678 | | return 0; |
| 679 | | } |
| 680 | | |
| 681 | | |
| 682 | | |
| 683 | | int VDeviceBUZ::write_buffer(VFrame *frame, EDL *edl) |
| 684 | | { |
| 685 | | //printf("VDeviceBUZ::write_buffer 1\n"); |
| 686 | | tuner_lock->lock("VDeviceBUZ::write_buffer"); |
| 687 | | |
| 688 | | if(!jvideo_fd) open_output_core(0); |
| 689 | | |
| 690 | | VFrame *ptr = 0; |
| 691 | | if(frame->get_color_model() != BC_COMPRESSED) |
| 692 | | { |
| 693 | | if(!temp_frame) temp_frame = new VFrame; |
| 694 | | if(!mjpeg) |
| 695 | | { |
| 696 | | mjpeg = mjpeg_new(device->out_w, device->out_h, 2); |
| 697 | | mjpeg_set_quality(mjpeg, device->quality); |
| 698 | | mjpeg_set_float(mjpeg, 0); |
| 699 | | } |
| 700 | | ptr = temp_frame; |
| 701 | | mjpeg_compress(mjpeg, |
| 702 | | frame->get_rows(), |
| 703 | | frame->get_y(), |
| 704 | | frame->get_u(), |
| 705 | | frame->get_v(), |
| 706 | | frame->get_color_model(), |
| 707 | | device->cpus); |
| 708 | | temp_frame->allocate_compressed_data(mjpeg_output_size(mjpeg)); |
| 709 | | temp_frame->set_compressed_size(mjpeg_output_size(mjpeg)); |
| 710 | | bcopy(mjpeg_output_buffer(mjpeg), temp_frame->get_data(), mjpeg_output_size(mjpeg)); |
| 711 | | } |
| 712 | | else |
| 713 | | ptr = frame; |
| 714 | | |
| 715 | | // Wait for frame to become available |
| 716 | | // Caused close_output_core to lock up. |
| 717 | | // if(total_loops >= 1) |
| 718 | | // { |
| 719 | | // if(ioctl(jvideo_fd, BUZIOC_SYNC, &output_number) < 0) |
| 720 | | // perror("VDeviceBUZ::write_buffer BUZIOC_SYNC"); |
| 721 | | // } |
| 722 | | |
| 723 | | if(device->out_config->buz_swap_fields) |
| 724 | | { |
| 725 | | long field2_offset = mjpeg_get_field2((unsigned char*)ptr->get_data(), |
| 726 | | ptr->get_compressed_size()); |
| 727 | | long field2_len = ptr->get_compressed_size() - field2_offset; |
| 728 | | memcpy(output_buffer + output_number * breq.size, |
| 729 | | ptr->get_data() + field2_offset, |
| 730 | | field2_len); |
| 731 | | memcpy(output_buffer + output_number * breq.size +field2_len, |
| 732 | | ptr->get_data(), |
| 733 | | field2_offset); |
| 734 | | } |
| 735 | | else |
| 736 | | { |
| 737 | | bcopy(ptr->get_data(), |
| 738 | | output_buffer + output_number * breq.size, |
| 739 | | ptr->get_compressed_size()); |
| 740 | | } |
| 741 | | |
| 742 | | if(ioctl(jvideo_fd, BUZIOC_QBUF_PLAY, &output_number) < 0) |
| 743 | | perror("VDeviceBUZ::write_buffer BUZIOC_QBUF_PLAY"); |
| 744 | | |
| 745 | | output_number++; |
| 746 | | if(output_number >= breq.count) |
| 747 | | { |
| 748 | | output_number = 0; |
| 749 | | total_loops++; |
| 750 | | } |
| 751 | | tuner_lock->unlock(); |
| 752 | | //printf("VDeviceBUZ::write_buffer 2\n"); |
| 753 | | |
| 754 | | return 0; |
| 755 | | } |
| 756 | | |
| 757 | | void VDeviceBUZ::new_output_buffer(VFrame *output, |
| 758 | | int colormodel) |
| 759 | | { |
| 760 | | //printf("VDeviceBUZ::new_output_buffer 1 %d\n", colormodel); |
| 761 | | if(user_frame) |
| 762 | | { |
| 763 | | if(colormodel != user_frame->get_color_model()) |
| 764 | | { |
| 765 | | delete user_frame; |
| 766 | | user_frame = 0; |
| 767 | | } |
| 768 | | } |
| 769 | | |
| 770 | | if(!user_frame) |
| 771 | | { |
| 772 | | switch(colormodel) |
| 773 | | { |
| 774 | | case BC_COMPRESSED: |
| 775 | | user_frame = new VFrame; |
| 776 | | break; |
| 777 | | default: |
| 778 | | user_frame = new VFrame(0, |
| 779 | | device->out_w, |
| 780 | | device->out_h, |
| 781 | | colormodel, |
| 782 | | -1); |
| 783 | | break; |
| 784 | | } |
| 785 | | } |
| 786 | | user_frame->set_shm_offset(0); |
| 787 | | output = user_frame; |
| 788 | | //printf("VDeviceBUZ::new_output_buffer 2\n"); |
| 789 | | } |
| 790 | | |
| 791 | | |
| 792 | | ArrayList<int>* VDeviceBUZ::get_render_strategies() |
| 793 | | { |
| 794 | | return &render_strategies; |
| 795 | | } |
| 796 | | |