Debian Codegraph @

encodedv/dvconnect.c

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