Debian Codegraph @

encodedv/dvconnect.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * dvconnect.c
3 *
4 * Copyright (C) Peter Schlaile - May 2001
5 *
6 * This file is part of libdv, a free DV (IEC 61834/SMPTE 314M)
7 * codec.
8 *
9 * libdv is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser Public License as published by
11 * the Free Software Foundation; either version 2.1, or (at your
12 * option) any later version.
13 *
14 * libdv is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser Public License
20 * along with libdv; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * The libdv homepage is http://libdv.sourceforge.net/.
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif // HAVE_CONFIG_H
29
30 #define _FILE_OFFSET_BITS 64
31
32 #include <stdio.h>
33 #include <sys/ioctl.h>
34 #include <sys/mman.h>
35 #include <sys/time.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sched.h>
39 #include <sys/resource.h>
40 #include <string.h>
41 #include <pthread.h>
42 #include <signal.h>
43
44 #if HAVE_LIBPOPT
45 #include <popt.h>
46 #endif
47
48 #include <stdlib.h>
49
50 static long cip_n_ntsc = 2436;
51 static long cip_d_ntsc = 38400;
52 static long cip_n_pal = 1;
53 static long cip_d_pal = 16;
54 static long syt_offset = 11000;
55
56 #define TARGETBUFSIZE 320
57 #define MAX_PACKET_SIZE 512
58 #define VBUF_SIZE (320*512)
59
60 #define VIDEO1394_MAX_SIZE 0x4000000
61
62 enum {
63 VIDEO1394_BUFFER_FREE = 0,
64 VIDEO1394_BUFFER_QUEUED,
65 VIDEO1394_BUFFER_READY
66 };
67
68 #define VIDEO1394_SYNC_FRAMES 0x00000001
69 #define VIDEO1394_INCLUDE_ISO_HEADERS 0x00000002
70 #define VIDEO1394_VARIABLE_PACKET_SIZE 0x00000004
71
72 struct video1394_mmap
73 {
74 int channel;
75 unsigned int sync_tag;
76 unsigned int nb_buffers;
77 unsigned int buf_size;
78 /* For VARIABLE_PACKET_SIZE: Maximum packet size */
79 unsigned int packet_size;
80 unsigned int fps;
81 unsigned int syt_offset;
82 unsigned int flags;
83 };
84
85
86 /* For TALK_QUEUE_BUFFER with VIDEO1394_VARIABLE_PACKET_SIZE use */
87 struct video1394_queue_variable
88 {
89 unsigned int channel;
90 unsigned int buffer;
91 unsigned int* packet_sizes; /* Buffer of size: buf_size / packet_size*/
92 };
93
94 struct video1394_wait
95 {
96 unsigned int channel;
97 unsigned int buffer;
98 struct timeval filltime; /* time of buffer full */
99 };
100
101 #define VIDEO1394_LISTEN_CHANNEL \
102 _IOWR('#', 0x10, struct video1394_mmap)
103 #define VIDEO1394_UNLISTEN_CHANNEL \
104 _IOW ('#', 0x11, int)
105 #define VIDEO1394_LISTEN_QUEUE_BUFFER \
106 _IOW ('#', 0x12, struct video1394_wait)
107 #define VIDEO1394_LISTEN_WAIT_BUFFER \
108 _IOWR('#', 0x13, struct video1394_wait)
109 #define VIDEO1394_TALK_CHANNEL \
110 _IOWR('#', 0x14, struct video1394_mmap)
111 #define VIDEO1394_UNTALK_CHANNEL \
112 _IOW ('#', 0x15, int)
113 #define VIDEO1394_TALK_QUEUE_BUFFER \
114 _IOW ('#', 0x16, size_t)
115 #define VIDEO1394_TALK_WAIT_BUFFER \
116 _IOW ('#', 0x17, struct video1394_wait)
117 #define VIDEO1394_LISTEN_POLL_BUFFER \
118 _IOWR('#', 0x18, struct video1394_wait)
119
120 static int cap_start_frame = 0;
121 static int cap_num_frames = 0xfffffff;
122 static int cap_verbose_mode;
123 static int frames_captured = 0;
124 static int broken_frames = 0;
125
126 /* ------------------------------------------------------------------------
127 - buffer management
128 ------------------------------------------------------------------------ */
129
130 static int max_buffer_blocks = 25*10;
131 static int ceil_buffer_blocks = 0;
132
133 struct buf_node {
134 unsigned char data[144000]; /* FIXME: We are wasting space on NTSC! */
135 int usage;
136 struct buf_node* next;
137 };
138
139 struct buf_list {
140 struct buf_node * first;
141 struct buf_node * last;
142 int usage;
143 };
144
145 static struct buf_list free_list = { NULL, NULL, 0 };
146 static struct buf_list buf_queue = { NULL, NULL, 0 };
147
148 static pthread_mutex_t queue_mutex;
149 static pthread_mutex_t wakeup_mutex;
150 static pthread_cond_t wakeup_cond;
151 static pthread_t file_io_thread;
152
153 void push_back(struct buf_list * l, struct buf_node * elem)
154 {
155 pthread_mutex_lock(&queue_mutex);
156 if (l->last) {
157 l->last->next = elem;
158 } else {
159 l->first = elem;
160 }
161 elem->next = NULL;
162 l->last = elem;
163 l->usage++;
164 pthread_mutex_unlock(&queue_mutex);
165 }
166
167 struct buf_node * pop_front(struct buf_list * l)
168 {
169 struct buf_node * rval;
170
171 pthread_mutex_lock(&queue_mutex);
172 rval = l->first;
173
174 if (l->first == l->last) {
175 l->last = NULL;
176 }
177 if (rval) {
178 l->first = rval->next;
179 l->usage--;
180 } else {
181 l->first = NULL;
182 }
183
184 pthread_mutex_unlock(&queue_mutex);
185 return rval;
186 }
187
188 struct buf_node * get_free_block()
189 {
190 struct buf_node * f = pop_front(&free_list);
191 if (!f) {
192 if (buf_queue.usage >= max_buffer_blocks) {
193 return NULL;
194 }
195 f = (struct buf_node*) malloc(sizeof(struct buf_node));
196 }
197 return f;
198 }
199
200 /* ------------------------------------------------------------------------
201 - receiver
202 ------------------------------------------------------------------------ */
203
204 static FILE* dst_fp;
205
206 void handle_packet(unsigned char* data, int len)
207 {
208 int start_of_frame = 0;
209 static int frame_size = 0;
210 static int isPAL = 0;
211 static int found_first_block = 0;
212 static unsigned char* p_out = NULL;
213 static struct buf_node * f = NULL;
214
215 switch (len) {
216 case 488:
217 if (data[12] == 0x1f && data[13] == 0x07) {
218 start_of_frame = 1;
219 }
220
221 if (start_of_frame) {
222 int frame_broken = 0;
223 if (!found_first_block) {
224 if (!cap_start_frame) {
225 found_first_block = 1;
226 } else {
227 cap_start_frame--;
228 if (cap_verbose_mode) {
229 fprintf(stderr,
230 "Skipped frame %8d\r",
231 cap_start_frame);
232 }
233 return;
234 }
235 frame_size = 0;
236 }
237 if (!cap_num_frames) {
238 return;
239 }
240 cap_num_frames--;
241
242 if (frame_size) {
243 if ((isPAL && frame_size != 144000)
244 || (!isPAL && frame_size != 120000)) {
245 fprintf(stderr,
246 "Broken frame %8d "
247 "(size = %d)!\n",
248 frames_captured, frame_size);
249 broken_frames++;
250 frame_broken = 1;
251 }
252 if (cap_verbose_mode) {
253 fprintf(stderr,
254 "Captured frame %8d\r",
255 frames_captured);
256 }
257 frames_captured++;
258 }
259 isPAL = (data[15] & 0x80);
260
261 if (f && !frame_broken) {
262 f->usage = frame_size;
263 push_back(&buf_queue, f);
264 pthread_mutex_lock(&wakeup_mutex);
265 pthread_cond_signal(&wakeup_cond);
266 pthread_mutex_unlock(&wakeup_mutex);
267 f = NULL;
268 }
269
270 if (!f) {
271 f = get_free_block();
272 if (!f) {
273 fprintf(stderr,
274 "Buffer overrun: "
275 "Frame discarded!\n");
276 broken_frames++;
277 }
278 }
279 if (f) {
280 p_out = f->data;
281 }
282 frame_size = 0;
283 }
284
285 if (found_first_block && cap_num_frames != 0 && f) {
286 if (frame_size < 144000) {
287 memcpy(p_out, data + 12, 480);
288 p_out += 480;
289 frame_size += 480;
290 } else {
291 fprintf(stderr, "Frame buffer overrun: "
292 "Frame discarded!\n");
293 broken_frames++;
294 p_out = f->data;
295 frame_size = 0;
296 }
297 }
298
299 break;
300 case 8:
301 break;
302 default:
303 fprintf(stderr, "Penguin on the bus? (packet size = %d!)\n",
304 len);
305 break;
306 }
307 }
308
309 void* write_out_thread(void * arg)
310 {
311 while (cap_num_frames != 0) {
312 struct buf_node * f;
313 pthread_mutex_lock(&wakeup_mutex);
314 pthread_cond_wait(&wakeup_cond, &wakeup_mutex);
315 pthread_mutex_unlock(&wakeup_mutex);
316
317 while ((f = pop_front(&buf_queue)) != NULL) {
318 fwrite(f->data, 1, f->usage, dst_fp);
319 push_back(&free_list, f);
320 }
321 }
322 return NULL;
323 }
324
325 void sig_int_recv_handler(int signum)
326 {
327 char t [] = "Terminating on user's request...\n";
328 write(2, t, sizeof(t));
329
330 cap_num_frames = 0;
331 }
332
333 int capture_raw(const char* filename, int channel, int nbuffers,
334 int start_frame, int end_frame, int verbose_mode,
335 char *device)
336 {
337 int viddev;
338 unsigned char *recv_buf;
339 struct video1394_mmap v;
340 struct video1394_wait w;
341 int unused_buffers;
342 unsigned char outbuf[2*65536];
343 int outbuf_used = 0;
344
345 cap_start_frame = start_frame;
346 cap_num_frames = end_frame - start_frame;
347 cap_verbose_mode = verbose_mode;
348
349 if (!filename || strcmp(filename, "-") == 0) {
350 dst_fp = stdout;
351 } else {
352 dst_fp = fopen(filename, "wb");
353 if (!dst_fp) {
354 perror("fopen input file");
355 return(-1);
356 }
357 }
358
359 if ((viddev = open(device, O_RDWR)) < 0) {
360 perror("open video1394 device");
361 return -1;
362 }
363
364 v.channel = channel;
365 v.sync_tag = 0;
366 v.nb_buffers = nbuffers;
367 v.buf_size = VBUF_SIZE;
368 v.packet_size = MAX_PACKET_SIZE;
369 v.syt_offset = syt_offset;
370 v.flags = VIDEO1394_INCLUDE_ISO_HEADERS;
371 w.channel = v.channel;
372
373 if (ioctl(viddev, VIDEO1394_LISTEN_CHANNEL, &v) < 0) {
374 perror("VIDEO1394_LISTEN_CHANNEL");
375 return -1;
376 }
377
378 if ((recv_buf = (unsigned char *) mmap(
379 0, v.nb_buffers*v.buf_size, PROT_READ|PROT_WRITE,
380 MAP_SHARED, viddev, 0)) == (unsigned char *)-1) {
381 perror("mmap videobuffer");
382 return -1;
383 }
384
385 pthread_mutex_init(&queue_mutex, NULL);
386 pthread_mutex_init(&wakeup_mutex, NULL);
387 pthread_cond_init(&wakeup_cond, NULL);
388
389 pthread_create(&file_io_thread, NULL, write_out_thread, NULL);
390
391 /* signal(SIGTERM, sig_int_recv_handler);
392 signal(SIGINT, sig_int_recv_handler); */
393
394 unused_buffers = v.nb_buffers;
395 w.buffer = 0;
396
397 while (cap_num_frames != 0) {
398 struct video1394_wait wcopy;
399 unsigned char * curr;
400 int ofs;
401
402 while (unused_buffers--) {
403 unsigned char * curr = recv_buf+ v.buf_size * w.buffer;
404
405 memset(curr, 0, v.buf_size);
406
407 wcopy = w;
408
409 if (ioctl(viddev,VIDEO1394_LISTEN_QUEUE_BUFFER, &wcopy) < 0) {
410 perror("VIDEO1394_LISTEN_QUEUE_BUFFER");
411 }
412 w.buffer++;
413 w.buffer %= v.nb_buffers;
414 }
415 wcopy = w;
416 if (ioctl(viddev, VIDEO1394_LISTEN_WAIT_BUFFER, &wcopy) < 0) {
417 perror("VIDEO1394_LISTEN_WAIT_BUFFER");
418 }
419 curr = recv_buf + v.buf_size * w.buffer;
420 ofs = 0;
421
422 while (ofs < VBUF_SIZE) {
423 while (outbuf_used < 4 && ofs < VBUF_SIZE) {
424 outbuf[outbuf_used++] = curr[ofs++];
425 }
426 if (ofs != VBUF_SIZE) {
427 int len = outbuf[2] + (outbuf[3] << 8) + 8;
428 if (ofs + len - outbuf_used > VBUF_SIZE) {
429 memcpy(outbuf + outbuf_used, curr+ofs,
430 VBUF_SIZE - ofs);
431 outbuf_used += VBUF_SIZE - ofs;
432 ofs = VBUF_SIZE;
433 } else {
434 memcpy(outbuf + outbuf_used,
435 curr + ofs, len - outbuf_used);
436 ofs += len - outbuf_used;
437 outbuf_used = 0;
438 handle_packet(outbuf, len - 8);
439 }
440 }
441 }
442 unused_buffers = 1;
443 }
444
445 if (broken_frames) {
446 fprintf(stderr, "\nCaptured %d broken frames!\n", broken_frames);
447 }
448
449 munmap(recv_buf, v.nb_buffers * v.buf_size);
450
451 if (ioctl(viddev, VIDEO1394_UNLISTEN_CHANNEL, &v.channel)<0) {
452 perror("VIDEO1394_UNLISTEN_CHANNEL");
453 }
454
455 close(viddev);
456
457 pthread_join(file_io_thread, NULL);
458
459 if (dst_fp != stdout) {
460 fclose(dst_fp);
461 }
462
463 return 0;
464 }
465
466 /* ------------------------------------------------------------------------
467 - sender
468 ------------------------------------------------------------------------ */
469
470 static const char*const* src_filenames;
471 static FILE* src_fp;
472 static int is_eof = 0;
473 static unsigned char * underrun_data_frame = NULL;
474 static int underrun_frame_ispal = 0;
475 static unsigned char *device = NULL;
476
477 static pthread_mutex_t wakeup_rev_mutex;
478 static pthread_cond_t wakeup_rev_cond;
479
480 int read_frame(FILE* fp, unsigned char* frame, int* isPAL)
481 {
482 if (is_eof) {
483 return -1;
484 }
485 if (fread(frame, 1, 120000, fp) != 120000) {
486 return -1;
487 }
488 *isPAL = (frame[3] & 0x80);
489
490 if (*isPAL) {
491 if (fread(frame + 120000, 1, 144000 - 120000, fp)
492 != 144000 - 120000) {
493 return -1;
494 }
495 }
496 return 0;
497 }
498
499 void sig_int_send_handler(int signum)
500 {
501 char t [] = "Terminating on user's request...\n";
502 write(2, t, sizeof(t));
503
504 is_eof = 1;
505 }
506
507 static int
508 open_next_input(void)
509 {
510 if (src_fp && src_fp != stdin) {
511 fclose (src_fp);
512 src_fp = NULL;
513 }
514 if (!src_filenames || !*src_filenames)
515 return 1;
516 if (!strcmp (*src_filenames++, "-"))
517 src_fp = stdin;
518 else {
519 src_fp = fopen(src_filenames[-1], "rb");
520 if (!src_fp) {
521 perror("fopen input file");
522 return 1;
523 }
524 }
525 return 0;
526 }
527
528
529 void fill_buf_queue(int fire)
530 {
531 struct buf_node * f;
532
533 while ((f = get_free_block()) != NULL) {
534 int isPAL;
535 again:
536 if (read_frame(src_fp, f->data, &isPAL) < 0) {
537 is_eof = open_next_input ();
538 if (!is_eof)
539 goto again;
540 push_back(&free_list, f);
541 return;
542 }
543 if (isPAL) {
544 f->usage = 144000;
545 } else {
546 f->usage = 120000;
547 }
548 push_back(&buf_queue, f);
549 if (fire) {
550 pthread_mutex_lock(&wakeup_rev_mutex);
551 pthread_cond_signal(&wakeup_rev_cond);
552 pthread_mutex_unlock(&wakeup_rev_mutex);
553 }
554 }
555 }
556
557 void* read_in_thread(void * arg)
558 {
559 fill_buf_queue(0);
560
561 pthread_mutex_lock(&wakeup_rev_mutex);
562 pthread_cond_signal(&wakeup_rev_cond);
563 pthread_mutex_unlock(&wakeup_rev_mutex);
564
565 while (!is_eof) {
566 pthread_mutex_lock(&wakeup_mutex);
567 pthread_cond_wait(&wakeup_cond, &wakeup_mutex);
568 pthread_mutex_unlock(&wakeup_mutex);
569
570 fill_buf_queue(1);
571 }
572 return NULL;
573 }
574
575 static int fill_buffer(unsigned char* targetbuf, unsigned int * packet_sizes)
576 {
577 unsigned char* frame;
578 struct buf_node * f_node;
579 int isPAL;
580 int frame_size;
581 unsigned long vdata = 0;
582 int i;
583 static unsigned char continuity_counter = 0;
584 static unsigned int cip_counter = 0;
585 static unsigned int cip_n = 0;
586 static unsigned int cip_d = 0;
587
588 f_node = pop_front(&buf_queue);
589 if (!f_node) {
590 if (!is_eof) {
591 if (!underrun_data_frame) {
592 fprintf(stderr, "Buffer underrun ");
593 if (ceil_buffer_blocks > 0 &&
594 max_buffer_blocks < ceil_buffer_blocks) {
595 max_buffer_blocks += 25;
596 fprintf(stderr,
597 "(raising buffer limit +25 => %d)!\n",
598 max_buffer_blocks);
599 } else {
600 fprintf(stderr,
601 "(not raising buffer space, "
602 "hard limit reached)\n");
603 }
604
605
606 pthread_mutex_lock(&wakeup_rev_mutex);
607 pthread_cond_wait(&wakeup_rev_cond,
608 &wakeup_rev_mutex);
609 pthread_mutex_unlock(&wakeup_rev_mutex);
610 f_node = pop_front(&buf_queue);
611 } else {
612 f_node = get_free_block();
613
614 if (underrun_frame_ispal) {
615 f_node->usage = 144000;
616 } else {
617 f_node->usage = 120000;
618 }
619 memcpy(f_node->data, underrun_data_frame,
620 f_node->usage);
621 }
622
623 } else {
624 return -1;
625 }
626 }
627
628 frame = f_node->data;
629 frame_size = f_node->usage;
630
631 isPAL = (frame_size == 144000);
632
633 if (cip_counter == 0) {
634 if (!isPAL) {
635 cip_n = cip_n_ntsc;
636 cip_d = cip_d_ntsc;
637 } else {
638 cip_n = cip_n_pal;
639 cip_d = cip_d_pal;
640 }
641 cip_counter = cip_n;
642 }
643
644 for (i = 0; i < TARGETBUFSIZE && vdata < frame_size; i++) {
645 unsigned char* p = targetbuf;
646 int want_sync = 0;
647 cip_counter += cip_n;
648
649 if (cip_counter > cip_d) {
650 want_sync = 1;
651 cip_counter -= cip_d;
652 }
653
654 *p++ = 0x01; /* Source node ID ! */
655 *p++ = 0x78; /* Packet size in quadlets (480 / 4) */
656 *p++ = 0x00;
657 *p++ = continuity_counter;
658
659 *p++ = 0x80; /* const */
660 *p++ = isPAL ? 0x80 : 0x00;
661 *p++ = 0xff; /* timestamp */
662 *p++ = 0xff; /* timestamp */
663
664 /* Timestamping is now done in the kernel driver! */
665 if (!want_sync) { /* video data */
666 continuity_counter++;
667
668 memcpy(p, frame + vdata, 480);
669 p += 480;
670 vdata += 480;
671 }
672
673 *packet_sizes++ = p - targetbuf;
674 targetbuf += MAX_PACKET_SIZE;
675 }
676 *packet_sizes++ = 0;
677
678 push_back(&free_list, f_node);
679
680 pthread_mutex_lock(&wakeup_mutex);
681 pthread_cond_signal(&wakeup_cond);
682 pthread_mutex_unlock(&wakeup_mutex);
683
684 return 0;
685 }
686
687 int send_raw(const char*const* filenames, int channel, int nbuffers,
688 int start_frame, int end_frame, int verbose_mode,
689 const char * underrun_data_filename, char *device)
690 {
691 int viddev;
692 unsigned char *send_buf;
693 struct video1394_mmap v;
694 struct video1394_queue_variable w;
695 int unused_buffers;
696 int got_frame;
697 unsigned int packet_sizes[321];
698
699 if ( filenames == NULL )
700 return -1;
701
702 src_filenames = filenames;
703 if (!*src_filenames) {
704 src_filenames = 0;
705 src_fp = stdin;
706 }
707 else if (open_next_input ())
708 return -1;
709
710 if (underrun_data_filename) {
711 FILE * fp = fopen(underrun_data_filename, "rb");
712 if (!fp) {
713 perror("fopen underrun data file");
714 return -1;
715 }
716 underrun_data_frame = (unsigned char*) malloc(144000);
717 if (read_frame(fp, underrun_data_frame,
718 &underrun_frame_ispal) < 0) {
719 fprintf(stderr, "Short read on reading underrun data "
720 "frame...\n");
721 fclose(fp);
722 return -1;
723 }
724 fclose(fp);
725 }
726
727 while (start_frame > 0) {
728 int isPAL,i;
729 unsigned char frame[144000]; /* PAL is large enough... */
730
731 for (i = 0; i < start_frame; i++) {
732 if (verbose_mode) {
733 fprintf(stderr, "Skipped frame %8d\r",
734 start_frame - i);
735 }
736 if (read_frame(src_fp, frame, &isPAL) < 0) {
737 break;
738 }
739 }
740 start_frame -= i;
741 if (!start_frame)
742 break;
743 if (open_next_input ())
744 return -1;
745 }
746
747 if ((viddev = open(device,O_RDWR)) < 0) {
748 perror("open video1394 device");
749 if (src_fp && src_fp != stdin)
750 fclose (src_fp);
751 return -1;
752 }
753
754 v.channel = channel;
755 v.sync_tag = 0;
756 v.nb_buffers = nbuffers;
757 v.buf_size = TARGETBUFSIZE * MAX_PACKET_SIZE;
758 v.packet_size = MAX_PACKET_SIZE;
759 v.flags = VIDEO1394_VARIABLE_PACKET_SIZE;
760 v.syt_offset = syt_offset;
761 w.channel = v.channel;
762
763 if (ioctl(viddev, VIDEO1394_TALK_CHANNEL, &v) < 0) {
764 perror("VIDEO1394_TALK_CHANNEL");
765 close (viddev);
766 if (src_fp && src_fp != stdin)
767 fclose (src_fp);
768 return -1;
769 }
770
771 if ((send_buf = (unsigned char *) mmap(
772 0, v.nb_buffers * v.buf_size, PROT_READ|PROT_WRITE,
773 MAP_SHARED, viddev, 0)) == (unsigned char *)-1) {
774 perror("mmap videobuffer");
775 close (viddev);
776 if (src_fp && src_fp != stdin)
777 fclose (src_fp);
778 return -1;
779 }
780
781 if (verbose_mode) {
782 fprintf(stderr, "Filling buffers...\r");
783 }
784
785 /* signal(SIGTERM, sig_int_send_handler);
786 signal(SIGINT, sig_int_send_handler); */
787
788 pthread_mutex_init(&queue_mutex, NULL);
789 pthread_mutex_init(&wakeup_mutex, NULL);
790 pthread_cond_init(&wakeup_cond, NULL);
791 pthread_mutex_init(&wakeup_rev_mutex, NULL);
792 pthread_cond_init(&wakeup_rev_cond, NULL);
793
794 pthread_create(&file_io_thread, NULL, read_in_thread, NULL);
795
796 pthread_mutex_lock(&wakeup_rev_mutex);
797 pthread_cond_wait(&wakeup_rev_cond, &wakeup_rev_mutex);
798 pthread_mutex_unlock(&wakeup_rev_mutex);
799
800 if (verbose_mode) {
801 fprintf(stderr, "Transmitting...\r");
802 }
803
804 unused_buffers = v.nb_buffers;
805 w.buffer = 0;
806 got_frame = 1;
807 w.packet_sizes = packet_sizes;
808 memset(packet_sizes, 0, sizeof(packet_sizes));
809
810 for (;start_frame < end_frame;) {
811 while (unused_buffers--) {
812 got_frame = (fill_buffer(
813 send_buf + w.buffer * v.buf_size,
814 packet_sizes) < 0) ? 0 : 1;
815
816 if (!got_frame) {
817 break;
818 }
819 if (ioctl(viddev, VIDEO1394_TALK_QUEUE_BUFFER, &w)<0) {
820 perror("VIDEO1394_TALK_QUEUE_BUFFER");
821 }
822 if (verbose_mode) {
823 fprintf(stderr, "Sent frame %8d\r",
824 start_frame);
825 }
826 w.buffer ++;
827 w.buffer %= v.nb_buffers;
828 start_frame++;
829 }
830 if (!got_frame) {
831 break;
832 }
833 if (ioctl(viddev, VIDEO1394_TALK_WAIT_BUFFER, &w) < 0) {
834 perror("VIDEO1394_TALK_WAIT_BUFFER");
835 }
836 unused_buffers = 1;
837 }
838
839 w.buffer = (v.nb_buffers + w.buffer - 1) % v.nb_buffers;
840
841 if (ioctl(viddev, VIDEO1394_TALK_WAIT_BUFFER, &w) < 0) {
842 perror("VIDEO1394_TALK_WAIT_BUFFER");
843 }
844
845 munmap(send_buf, v.nb_buffers * v.buf_size);
846
847 if (ioctl(viddev, VIDEO1394_UNTALK_CHANNEL, &v.channel)<0) {
848 perror("VIDEO1394_UNTALK_CHANNEL");
849 }
850
851 close(viddev);
852
853 pthread_join(file_io_thread, NULL);
854
855 return 0;
856 }
857
858 int rt_raisepri (int pri)
859 {
860 #ifdef _SC_PRIORITY_SCHEDULING
861 struct sched_param scp;
862
863 /*
864 * Verify that scheduling is available
865 */
866 if (sysconf (_SC_PRIORITY_SCHEDULING) == -1) {
867 fprintf (stderr, "WARNING: RR-scheduler not available, "
868 "disabling.\n");
869 return (-1);
870 } else {
871 memset (&scp, '\0', sizeof (scp));
872 scp.sched_priority = sched_get_priority_max (SCHED_RR) - pri;
873 if (sched_setscheduler (0, SCHED_RR, &scp) < 0) {
874 fprintf (stderr, "WARNING: Cannot set RR-scheduler\n");
875 return (-1);
876 }
877 }
878 #endif
879 return (0);
880 }
881
882 /* ------------------------------------------------------------------------
883 - main()
884 ------------------------------------------------------------------------ */
885
886 #define DV_CONNECT_OPT_VERSION 0
887 #define DV_CONNECT_OPT_SEND 1
888 #define DV_CONNECT_OPT_VERBOSE 2
889 #define DV_CONNECT_OPT_CHANNEL 3
890 #define DV_CONNECT_OPT_KBUFFERS 4
891 #define DV_CONNECT_OPT_START_FRAME 5
892 #define DV_CONNECT_OPT_END_FRAME 6
893 #define DV_CONNECT_OPT_CIP_N_NTSC 7
894 #define DV_CONNECT_OPT_CIP_D_NTSC 8
895 #define DV_CONNECT_OPT_SYT_OFFSET 9
896 #define DV_CONNECT_OPT_MAX_BUFFERS 10
897 #define DV_CONNECT_OPT_UNDERRUN_DATA 11
898 #define DV_CONNECT_OPT_AUTOHELP 12
899 #define DV_CONNECT_OPT_DEVICE 13
900 #define DV_CONNECT_NUM_OPTS 14
901
902
903 int main(int argc, const char** argv)
904 {
905 #if HAVE_LIBPOPT
906 int verbose_mode = 0;
907 int send_mode = 0;
908 int channel = 63;
909 int buffers = 8;
910 int start = 0;
911 int end = 0xfffffff;
912 const char* underrun_data = NULL;
913
914 struct poptOption option_table[DV_CONNECT_NUM_OPTS+1];
915 int rc; /* return code from popt */
916 poptContext optCon; /* context for parsing command-line options */
917
918 option_table[DV_CONNECT_OPT_VERSION] = (struct poptOption) {
919 longName: "version",
920 val: 'v',
921 descrip: "show dvconnect version number"
922 }; /* version */
923
924 option_table[DV_CONNECT_OPT_SEND] = (struct poptOption) {
925 longName: "send",
926 shortName: 's',
927 arg: &send_mode,
928 descrip: "send data instead of capturing"
929 }; /* send mode */
930
931 option_table[DV_CONNECT_OPT_VERBOSE] = (struct poptOption) {
932 longName: "verbose",
933 shortName: 'v',
934 arg: &verbose_mode,
935 descrip: "show status information while receiving / sending"
936 }; /* verbose mode */
937
938 option_table[DV_CONNECT_OPT_CHANNEL] = (struct poptOption) {
939 longName: "channel",
940 shortName: 'c',
941 argInfo: POPT_ARG_INT,
942 arg: &channel,
943 argDescrip: "number",
944 descrip: "channel to send / receive on "
945 "(range: 0 - 63, default = 63)"
946 }; /* channel */
947
948 option_table[DV_CONNECT_OPT_KBUFFERS] = (struct poptOption) {
949 longName: "kbuffers",
950 shortName: 'k',
951 argInfo: POPT_ARG_INT,
952 arg: &buffers,
953 argDescrip: "number",
954 descrip: "number of kernel video frame buffers to use. "
955 "default = 8"
956 }; /* buffers */
957
958 option_table[DV_CONNECT_OPT_START_FRAME] = (struct poptOption) {
959 longName: "start-frame",
960 argInfo: POPT_ARG_INT,
961 arg: &start,
962 argDescrip: "count",
963 descrip: "start at <count> frame (defaults to 0)"
964 }; /* start-frame */
965
966 option_table[DV_CONNECT_OPT_END_FRAME] = (struct poptOption) {
967 longName: "end-frame",
968 shortName: 'e',
969 argInfo: POPT_ARG_INT,
970 arg: &end,
971 argDescrip: "count",
972 descrip: "end at <count> frame (defaults to unlimited)"
973 }; /* end-frames */
974
975 option_table[DV_CONNECT_OPT_CIP_N_NTSC] = (struct poptOption) {
976 longName: "cip-n-ntsc",
977 argInfo: POPT_ARG_INT,
978 arg: &cip_n_ntsc,
979 descrip: "cip n ntsc timing counter (default: 2436)"
980 }; /* cip_n_ntsc */
981
982 option_table[DV_CONNECT_OPT_CIP_D_NTSC] = (struct poptOption) {
983 longName: "cip-d-ntsc",
984 argInfo: POPT_ARG_INT,
985 arg: &cip_d_ntsc,
986 descrip: "cip d ntsc timing counter (default: 38400)"
987 }; /* cip_d_ntsc */
988
989 option_table[DV_CONNECT_OPT_SYT_OFFSET] = (struct poptOption) {
990 longName: "syt-offset",
991 argInfo: POPT_ARG_INT,
992 arg: &syt_offset,
993 descrip: "syt offset (default: 10000 range: 10000-26000)"
994 }; /* syt offset */
995
996 option_table[DV_CONNECT_OPT_MAX_BUFFERS] = (struct poptOption) {
997 longName: "buffers",
998 shortName: 'b',
999 argInfo: POPT_ARG_INT,
1000 arg: &max_buffer_blocks,
1001 argDescrip: "number",
1002 descrip: "max number of file io thread buffers to use. "
1003 "default = 250"
1004 }; /* buffers */
1005
1006 option_table[DV_CONNECT_OPT_UNDERRUN_DATA] = (struct poptOption) {
1007 longName: "underrun-data",
1008 shortName: 'u',
1009 argInfo: POPT_ARG_STRING,
1010 arg: &underrun_data,
1011 argDescrip: "filename",
1012 descrip: "file with the frame to send in case of underruns"
1013 }; /* underrun-data */
1014
1015 option_table[DV_CONNECT_OPT_AUTOHELP] = (struct poptOption) {
1016 argInfo: POPT_ARG_INCLUDE_TABLE,
1017 arg: poptHelpOptions,
1018 descrip: "Help options",
1019 }; /* autohelp */
1020
1021 option_table[DV_CONNECT_OPT_DEVICE] = (struct poptOption) {
1022 longName: "device",
1023 shortName: 'd',
1024 argInfo: POPT_ARG_STRING,
1025 arg: &device,
1026 descrip: "Specify the video1394 device (default /dev/video1394/0)",
1027 }; /* device */
1028
1029 option_table[DV_CONNECT_NUM_OPTS] = (struct poptOption) {
1030 NULL, 0, 0, NULL, 0 };
1031
1032 optCon = poptGetContext(NULL, argc,
1033 (const char **)argv, option_table, 0);
1034 poptSetOtherOptionHelp(optCon, "<raw dv files or '-- -' for stdin/stdout>");
1035
1036 while ((rc = poptGetNextOpt(optCon)) > 0) {
1037 switch (rc) {
1038 case 'v':
1039 fprintf(stderr,"dvconnect: version %s, "
1040 "http://libdv.sourceforge.net/\n",
1041 "CVS 05/06/2001");
1042 return 0;
1043 break;
1044 default:
1045 break;
1046 }
1047 }
1048
1049 if (rc < -1) {
1050 /* an error occurred during option processing */
1051 fprintf(stderr, "%s: %s\n",
1052 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
1053 poptStrerror(rc));
1054 return -1;
1055 }
1056
1057 if (channel < 0 || channel > 63) {
1058 fprintf(stderr, "Invalid channel chosen: %d\n"
1059 "Should be in the range 0 - 63\n", channel);
1060 return -1;
1061 }
1062 if (buffers < 0) {
1063 fprintf(stderr, "Number of buffers should be > 0!\n");
1064 return -1;
1065 }
1066
1067 setpriority (PRIO_PROCESS, 0, -20);
1068 if (rt_raisepri (1) != 0) {
1069 setpriority (PRIO_PROCESS, 0, -20);
1070 }
1071
1072 #if _POSIX_MEMLOCK > 0
1073 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
1074 {
1075 if (verbose_mode)
1076 fprintf(stderr, "Cannot disable swapping\n");
1077 }
1078 else
1079 #endif
1080 {
1081 /* Prevent excessive underruns from locking down all mem. */
1082 ceil_buffer_blocks = 10*max_buffer_blocks;
1083 }
1084
1085 if ( device == NULL )
1086 device = "/dev/video1394/0";
1087
1088 if (send_mode) {
1089 send_raw(poptGetArgs(optCon), channel, buffers, start, end,
1090 verbose_mode, underrun_data, device);
1091 } else {
1092 capture_raw(poptGetArg(optCon), channel, buffers, start, end,
1093 verbose_mode, device);
1094 }
1095 return 0;
1096 #else
1097 #warning dvconnect can not work without libpopt!
1098
1099 #endif
1100 }
1101