r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / device1394output.C
blobb4567812c0f52fb32b59c22863d626e569bbf803
1 #ifdef HAVE_FIREWIRE
6 #include "condition.h"
7 #include "device1394output.h"
8 #include "mutex.h"
9 #include "timer.h"
10 #include "vframe.h"
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <string.h>
15 #include <sys/ioctl.h>
16 #include <sys/mman.h>
17 #include <unistd.h>
20 #include "avc1394.h"
21 #include "avc1394_vcr.h"
22 #include "rom1394.h"
27 // Crazy DV internals
28 #define CIP_N_NTSC 2436
29 #define CIP_D_NTSC 38400
30 #define CIP_N_PAL 1
31 #define CIP_D_PAL 16
32 #define OUTPUT_SAMPLES 262144
33 #define BUFFER_TIMEOUT 500000
36 Device1394Output::Device1394Output()
37  : Thread(1, 0, 0)
39         buffer = 0;
40         buffer_size = 0;
41         total_buffers = 0;
42         current_inbuffer = 0;
43         current_outbuffer = 0;
44         done = 0;
45         audio_lock = 0;
46         video_lock = 0;
47         start_lock = 0;
48         buffer_lock = 0;
49         position_lock = 0;
50         video_encoder = 0;
51         audio_encoder = 0;
52         audio_buffer = 0;
53         audio_samples = 0;
54         output_fd = -1;
55 //      avc_handle = 0;
56         temp_frame = 0;
57         temp_frame2 = 0;
58         audio_position = 0;
59         interrupted = 0;
60         have_video = 0;
63 Device1394Output::~Device1394Output()
65         if(Thread::running())
66         {
67                 done = 1;
68                 start_lock->unlock();
69                 Thread::cancel();
70                 Thread::join();
71         }
73         if(buffer)
74         {
75                 for(int i = 0; i < total_buffers; i++)
76                 {
77                         if(buffer[i]) delete [] buffer[i];
78                 }
79                 delete [] buffer;
80                 delete [] buffer_size;
81                 delete [] buffer_valid;
82         }
84         if(audio_lock) delete audio_lock;
85         if(video_lock) delete video_lock;
86         if(start_lock) delete start_lock;
87         if(audio_buffer) delete [] audio_buffer;
89         if(output_fd >= 0)
90         {
91         output_queue.buffer = (output_mmap.nb_buffers + output_queue.buffer - 1) % output_mmap.nb_buffers;
93                 if(use_dv1394 == 1)
94                 {
95                         if(ioctl(output_fd, DV1394_WAIT_FRAMES, output_mmap.nb_buffers - 1) < 0)
96                         {
97                                 fprintf(stderr,
98                                 "Device1394::close_all: DV1394_WAIT_FRAMES %i: %s",
99                                 output_mmap.nb_buffers,
100                                 strerror(errno));
101                         }
102                         munmap(output_buffer, status.init.n_frames * DV1394_NTSC_FRAME_SIZE);
103                 }
104                 else
105                 {
106         if(ioctl(output_fd, VIDEO1394_TALK_WAIT_BUFFER, &output_queue) < 0) 
107                         {
108             fprintf(stderr, 
109                                 "Device1394::close_all: VIDEO1394_TALK_WAIT_BUFFER %s: %s",
110                                 output_queue,
111                                 strerror(errno));
112         }
113         munmap(output_buffer, output_mmap.nb_buffers * output_mmap.buf_size);
114                 }
116                 if(use_dv1394 == 1)
117                 {
118                         if(ioctl(output_fd, DV1394_SHUTDOWN, NULL) < 0)
119                         {
120                                 perror("Device1394::close_all: DV1394_SHUTDOWN");
121                         }
122                 }
123                 else
124                 {
125         if(ioctl(output_fd, VIDEO1394_UNTALK_CHANNEL, &output_mmap.channel) < 0)
126                         {
127             perror("Device1394::close_all: VIDEO1394_UNTALK_CHANNEL");
128         }
129                 }
130         close(output_fd);
132 //              if(avc_handle)
133 //                      raw1394_destroy_handle(avc_handle);
134         }
136         if(temp_frame) delete temp_frame;
137         if(temp_frame2) delete temp_frame2;
138         if(video_encoder) dv_delete(video_encoder);
139         if(audio_encoder) dv_delete(audio_encoder);
140         if(buffer_lock) delete buffer_lock;
141         if(position_lock) delete position_lock;
145 int Device1394Output::open(char *path,
146         int port,
147         int channel,
148         int length,
149         int channels, 
150         int bits, 
151         int samplerate,
152         int syt,
153         int use_dv1394)
155         int is_pal;
156         struct stat buf;
157         this->channels = channels;
158         this->bits = bits;
159         this->samplerate = samplerate;
160         this->total_buffers = length;
161         this->syt = syt;
162         this->use_dv1394 = use_dv1394;
164         stat(path, &buf);
166         if(strstr(path, "PAL") != NULL || minor(buf.st_rdev) == 34)
167                 is_pal = 1;
168         else
169                 is_pal = 0;
171    static struct dv1394_init setup = {
172       api_version: DV1394_API_VERSION,
173       channel:     channel,
174                 n_frames:    length,
175       format:      is_pal ? DV1394_PAL : DV1394_NTSC,
176       cip_n:       0,
177       cip_d:       0,
178       syt_offset:  syt
179    };
182 //printf("Device1394::open_output 2 %s %d %d %d %d\n", path, port, channel, length, syt);
183         if(output_fd < 0)
184         {
185         output_fd = ::open(path, O_RDWR);
187                 if(output_fd <= 0)
188                 {
189                         fprintf(stderr, 
190                                 "Device1394Output::open path=%s: %s", 
191                                 path,
192                                 strerror(errno));
193                         return 1;
194                 }
195                 else
196                 {
197 //              avc_handle = raw1394_new_handle();
198 //                      if(avc_handle == 0)
199 //                              printf("Device1394::open_output avc_handle == 0\n");
200 // 
201 //                      struct raw1394_portinfo pinf[16];
202 //                      int numcards = raw1394_get_port_info(avc_handle, pinf, 16);
203 //                      if(numcards < 0)
204 //                              printf("Device1394::open_output raw1394_get_port_info failed\n");
205 //                      if(raw1394_set_port(avc_handle, out_config->firewire_port) < 0)
206 //                              printf("Device1394::open_output raw1394_set_port\n");
207 // 
208 //              int nodecount = raw1394_get_nodecount(avc_handle);
209 //                      int itemcount = 0;
210 //                      rom1394_directory rom1394_dir;
211 // 
212 //                      avc_id = -1;
213 //              for(int currentnode = 0; currentnode < nodecount; currentnode++) 
214 //                      {
215 //                      rom1394_get_directory(avc_handle, currentnode, &rom1394_dir);
216 // 
217 //                      if((rom1394_get_node_type(&rom1394_dir) == ROM1394_NODE_TYPE_AVC) &&
218 //                      avc1394_check_subunit_type(avc_handle, currentnode, AVC1394_SUBUNIT_TYPE_VCR)) 
219 //                              {
220 //                      itemcount++;
221 // // default the phyID to the first AVC node found
222 //                      if(itemcount == 1) avc_id = currentnode;
223 //                                      break;
224 //                      }
225 // 
226 //                      rom1394_free_directory(&rom1394_dir);
227 //              }
228 // 
229 //                      if(avc_id < 0)
230 //                              printf("Device1394::open_output no avc_id found.\n");
231 // 
232 //printf("Device1394::open_output 1 %p %d\n", avc_handle, avc_id);
233 //              avc1394_vcr_record(avc_handle, avc_id);
236                 output_mmap.channel = channel;
237                 output_queue.channel = channel;
238                 output_mmap.sync_tag = 0;
239                 output_mmap.nb_buffers = total_buffers;
240                 output_mmap.buf_size = 320 * 512;
241                 output_mmap.packet_size = 512;
242 // Shouldn't this be handled by the video1394 driver?
243 // dvgrab originally used 19000
244 // JVC DVL300 -> 30000
245                 output_mmap.syt_offset = syt;
246                 output_mmap.flags = VIDEO1394_VARIABLE_PACKET_SIZE;
249 // printf("Device1394Output::open %d %d %d %d %d %d %d\n", 
250 // output_mmap.channel, output_queue.channel, output_mmap.sync_tag, output_mmap.nb_buffers, output_mmap.buf_size, output_mmap.packet_size, output_mmap.syt_offset, output_mmap.flags);
251                         if(use_dv1394 == 1)
252                         {
253                                 if(ioctl(output_fd, DV1394_INIT, &setup) < 0)
254                                 {
255                                         perror("Device1394Output::open DV1394_INIT:");
256                                 }
257                                 if(ioctl(output_fd, DV1394_GET_STATUS, &setup) < 0)
258                                 {
259                                         perror("Device1394Output::open DV1394_GET_STATUS:");
260                                 }
261                         }
262                         else
263                         {
264                         if(ioctl(output_fd, VIDEO1394_TALK_CHANNEL, &output_mmap) < 0)
265                                 {
266                 perror("Device1394Output::open VIDEO1394_TALK_CHANNEL:");
267                         }
268                         }
270                         if(use_dv1394 == 1)
271                         {
272                                 int out_size;
273          output_buffer = (unsigned char*)mmap(0,
274             output_mmap.nb_buffers * (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE),
275                PROT_READ | PROT_WRITE,
276             MAP_SHARED,
277             output_fd,
278             0);
279                         }
280                         else
281                         {
282                 output_buffer = (unsigned char*)mmap(0, 
283                                 output_mmap.nb_buffers * output_mmap.buf_size,
284                 PROT_READ | PROT_WRITE, 
285                                 MAP_SHARED, 
286                                 output_fd, 
287                                 0);
288                         }
289                 if(output_buffer <= 0)
290                         {
291                 perror("Device1394Output::open mmap");
292                 }
294                         unused_buffers = output_mmap.nb_buffers;
295                 output_queue.buffer = 0;
296                 output_queue.packet_sizes = packet_sizes;
297                 continuity_counter = 0;
298                 cip_counter = 0;
300 // Create buffers
301                         buffer = new char*[total_buffers];
302                         for(int i = 0; i < length; i++)
303                                 buffer[i] = new char[DV_PAL_SIZE];
304                         buffer_size = new int[total_buffers];
305                         buffer_valid = new int[total_buffers];
306                         bzero(buffer_size, sizeof(int) * total_buffers);
307                         bzero(buffer_valid, sizeof(int) * total_buffers);
308                         bzero(buffer, sizeof(char*) * total_buffers);
309                         video_lock = new Condition(0);
310                         audio_lock = new Condition(0);
311                         start_lock = new Condition(0);
312                         buffer_lock = new Mutex;
313                         position_lock = new Mutex;
314                         encoder = dv_new();
315                         audio_buffer = new char[OUTPUT_SAMPLES * channels * bits / 8];
316                         Thread::start();
317         }
318         }
319         return 0;
322 void Device1394Output::run()
324         unsigned char *output;
325         char *out_buffer;
326         int out_size;
328         Thread::enable_cancel();
329         start_lock->lock();
330         Thread::disable_cancel();
332 //Timer timer;
333 // Write buffers continuously
334         while(!done)
335         {
336 // Get current buffer to play
337                 if(done) return;
338 //timer.update();
340 //printf("Device1394Output::run 1\n");
341                 buffer_lock->lock();
342 //printf("Device1394Output::run 10\n");
344                 if(use_dv1394 == 1)
345                 {
346                         out_buffer = buffer[status.first_clear_frame];
347                         out_size = buffer_size[status.first_clear_frame];
348                 }
349                 else
350                 {
351                         out_buffer = buffer[current_outbuffer];
352                         out_size = buffer_size[current_outbuffer];
353                 }
354                 
358 // No video.  Put in a fake frame for audio only
359                 if(!have_video)
360                 {
361 #include "data/fake_ntsc_dv.h"
362                         out_size = sizeof(fake_ntsc_dv) - 4;
363                         out_buffer = (char*)fake_ntsc_dv + 4;
364                 }
366                 if(use_dv1394 == 1)
367                 {
368                         output = output_buffer + 
369                         out_size *
370                         status.first_clear_frame;
371                 }
372                 else
373                 {
374                         output = output_buffer +
375                         output_queue.buffer * 
376                         output_mmap.buf_size;
377                 }
379                 int is_pal = (out_size == DV_PAL_SIZE);
385 // Got a buffer
386                 if(out_buffer && out_size)
387                 {
388 // Calculate number of samples needed based on given pattern for 
389 // norm.
390                         int samples_per_frame = 2048;
392 // Encode audio
393                         if(audio_samples > samples_per_frame)
394                         {
396                                 int samples_written = dv_write_audio(encoder,
397                                         (unsigned char*)out_buffer,
398                                         (unsigned char*)audio_buffer,
399                                         samples_per_frame,
400                                         channels,
401                                         bits,
402                                         samplerate,
403                                         is_pal ? DV_PAL : DV_NTSC);
404                                 memcpy(audio_buffer, 
405                                         audio_buffer + samples_written * bits * channels / 8,
406                                         (audio_samples - samples_written) * bits * channels / 8);
407                                 audio_samples -= samples_written;
408                                 position_lock->lock();
409                                 audio_position += samples_written;
410                                 position_lock->unlock();
413                                 audio_lock->unlock();
414                         }
415 // Copy from current buffer to mmap buffer with firewire encryption
416                         if(use_dv1394 == 1)
417                         {
418                                 memcpy(output, out_buffer, out_size);
419                         }
420                         else
421                         {
422                                 encrypt((unsigned char*)output, 
423                                         (unsigned char*)out_buffer, 
424                                         out_size);
425                         }
426                         buffer_valid[current_outbuffer] = 0;
427                 }
428 // Advance buffer number if possible
429                 increment_counter(&current_outbuffer);
431 // Reuse same buffer next time
432                 if(!buffer_valid[current_outbuffer])
433                 {
434                         decrement_counter(&current_outbuffer);
435                 }
436                 else
437 // Wait for user to reach current buffer before unlocking any more.
438                 {
439                         video_lock->unlock();
440                 }
442                 buffer_lock->unlock();
443 //printf("Device1394Output::run 100\n");
445                 if(out_size > 0)
446                 {
447 // Write mmap to device
448                         Thread::enable_cancel();
449                         unused_buffers--;
450                         if(use_dv1394 == 1)
451                         {
452                                 if(ioctl(output_fd, DV1394_SUBMIT_FRAMES, 1) < 0)
453                                 {
454                                         perror("Device1394Output::run DV1394_SUBMIT_FRAMES:");
455                                 }
456                                 if(ioctl(output_fd, DV1394_GET_STATUS, &status) < 0)
457                                 {
458                                         perror("Device1394Output::run DV1394_GET_STATUS:");
459                                 }
460                         }
461                         else
462                         {
463                                 if(ioctl(output_fd, VIDEO1394_TALK_QUEUE_BUFFER, &output_queue) < 0)
464                                 {
465                         perror("Device1394Output::run VIDEO1394_TALK_QUEUE_BUFFER");
466                         }
467                         }
468                 output_queue.buffer++;
469                         if(output_queue.buffer >= output_mmap.nb_buffers) 
470                                 output_queue.buffer = 0;
472                         if(unused_buffers <= 0)
473                         {
474                                 if(use_dv1394 == 1)
475                                 {
476                                         if(ioctl(output_fd, DV1394_WAIT_FRAMES, 1) < 0)
477                                         {
478                                                 perror("Device1394Output::run DV1394_WAIT_FRAMES");
479                                         }
480                                 }
481                                 else
482                                 {
483                                 if(ioctl(output_fd, VIDEO1394_TALK_WAIT_BUFFER, &output_queue) < 0) 
484                                         {
485                                 perror("Device1394::run VIDEO1394_TALK_WAIT_BUFFER");
486                                 }
487                                 }
488                                 unused_buffers++;
489                         }
490                         Thread::disable_cancel();
491                 }
492                 else
493                 {
494 //                      Thread::enable_cancel();
495 //                      start_lock->lock();
496 //                      Thread::disable_cancel();
497                 }
499 //printf("Device1394Output::run %lld\n", timer.get_difference());
500         }
505 void Device1394Output::encrypt(unsigned char *output, 
506         unsigned char *data, 
507         int data_size)
509 // Encode in IEEE1394 video encryption
510         int is_pal = (data_size == DV_PAL_SIZE);
511         int output_size = 320;
512         int packets_per_frame = (is_pal ? 300 : 250);
513         int min_packet_size = output_mmap.packet_size;
514         unsigned long frame_size = packets_per_frame * 480;
515         unsigned long vdata = 0;
516         unsigned int *packet_sizes = this->packet_sizes;
518     if(cip_counter == 0) 
519         {
520         if(!is_pal) 
521                 {
522             cip_n = CIP_N_NTSC;
523             cip_d = CIP_D_NTSC;
524             f50_60 = 0x00;
525         }
526                 else 
527                 {
528             cip_n = CIP_N_PAL;
529             cip_d = CIP_D_PAL;
530             f50_60 = 0x80;
531         }
532         cip_counter = cip_n;
533     }
538         for(int i = 0; i < output_size && vdata < frame_size; i++)
539         {
540         unsigned char *p = output;
541         int want_sync = (cip_counter > cip_d);
543 /* Source node ID ! */
544         *p++ = 0x01; 
545 /* Packet size in quadlets (480 / 4) - this stays the same even for empty packets */
546         *p++ = 0x78; 
547         *p++ = 0x00;
548         *p++ = continuity_counter;
550 /* const */
551         *p++ = 0x80; 
552 /* high bit = 50/60 indicator */
553         *p++ = f50_60; 
555 /* timestamp - generated in driver */
556         *p++ = 0xff; 
557 /* timestamp */
558         *p++ = 0xff; 
560 /* video data */
561         if(!want_sync)
562                 {
563             continuity_counter++;
564             cip_counter += cip_n;
566             memcpy(p, data + vdata, 480);
567             p += 480;
568             vdata += 480;
569         }
570         else
571             cip_counter -= cip_d;
573         *packet_sizes++ = p - output;
574         output += min_packet_size;
575         }
576         *packet_sizes++ = 0;
582 void Device1394Output::write_frame(VFrame *input)
584         VFrame *ptr = 0;
585         int is_pal = (input->get_h() == 576);
586         int result = 0;
588 //printf("Device1394Output::write_frame 1\n");
590         if(output_fd <= 0) return;
591         if(interrupted) return;
593 // Encode frame to DV
594         if(input->get_color_model() != BC_COMPRESSED)
595         {
596                 if(!temp_frame) temp_frame = new VFrame;
597                 if(!encoder) encoder = dv_new();
598                 ptr = temp_frame;
600 // Exact resolution match.  Don't do colorspace conversion
601                 if(input->get_color_model() == BC_YUV422 &&
602                         input->get_w() == 720 &&
603                         (input->get_h() == 480 ||
604                         input->get_h() == 576))
605                 {
606                         int norm = is_pal ? DV_PAL : DV_NTSC;
607                         int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
608                         temp_frame->allocate_compressed_data(data_size);
609                         temp_frame->set_compressed_size(data_size);
611                         dv_write_video(encoder,
612                                 temp_frame->get_data(),
613                                 input->get_rows(),
614                                 BC_YUV422,
615                                 norm);
616                         ptr = temp_frame;
617                 }
618                 else
619 // Convert resolution and color model before compressing
620                 {
621                         if(!temp_frame2)
622                         {
623                                 int h = input->get_h();
624 // Default to NTSC if unknown
625                                 if(h != 480 && h != 576) h = 480;
627                                 temp_frame2 = new VFrame(0,
628                                         720,
629                                         h,
630                                         BC_YUV422);
631                                 
632                         }
634                         int norm = is_pal ? DV_PAL : DV_NTSC;
635                         int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
636                         temp_frame->allocate_compressed_data(data_size);
637                         temp_frame->set_compressed_size(data_size);
640                         cmodel_transfer(temp_frame2->get_rows(), /* Leave NULL if non existent */
641                                 input->get_rows(),
642                                 temp_frame2->get_y(), /* Leave NULL if non existent */
643                                 temp_frame2->get_u(),
644                                 temp_frame2->get_v(),
645                                 input->get_y(), /* Leave NULL if non existent */
646                                 input->get_u(),
647                                 input->get_v(),
648                                 0,        /* Dimensions to capture from input frame */
649                                 0, 
650                                 MIN(temp_frame2->get_w(), input->get_w()),
651                                 MIN(temp_frame2->get_h(), input->get_h()),
652                                 0,       /* Dimensions to project on output frame */
653                                 0, 
654                                 MIN(temp_frame2->get_w(), input->get_w()),
655                                 MIN(temp_frame2->get_h(), input->get_h()),
656                                 input->get_color_model(), 
657                                 BC_YUV422,
658                                 0,         /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
659                                 input->get_bytes_per_line(),       /* For planar use the luma rowspan */
660                                 temp_frame2->get_bytes_per_line());     /* For planar use the luma rowspan */
662                         dv_write_video(encoder,
663                                 temp_frame->get_data(),
664                                 temp_frame2->get_rows(),
665                                 BC_YUV422,
666                                 norm);
670                         ptr = temp_frame;
671                 }
672         }
673         else
674                 ptr = input;
686 // Take over buffer table
687         buffer_lock->lock();
688         have_video = 1;
689 // Wait for buffer to become available with timeout
690         while(buffer_valid[current_inbuffer] && !result && !interrupted)
691         {
692                 buffer_lock->unlock();
693                 result = video_lock->timed_lock(BUFFER_TIMEOUT);
694                 buffer_lock->lock();
695         }
699 // Write buffer if there's room
700         if(!buffer_valid[current_inbuffer])
701         {
702                 if(!buffer[current_inbuffer])
703                 {
704                         buffer[current_inbuffer] = new char[ptr->get_compressed_size()];
705                         buffer_size[current_inbuffer] = ptr->get_compressed_size();
706                 }
707                 memcpy(buffer[current_inbuffer], ptr->get_data(), ptr->get_compressed_size());
708                 buffer_valid[current_inbuffer] = 1;
709                 increment_counter(&current_inbuffer);
710         }
711         else
712 // Ignore it if there isn't room.
713         {
714                 ;
715         }
717         buffer_lock->unlock();
718         start_lock->unlock();
719 //printf("Device1394Output::write_frame 100\n");
722 void Device1394Output::write_samples(char *data, int samples)
724 //printf("Device1394Output::write_samples 1\n");
725         int result = 0;
726         int timeout = (int64_t)samples * 
727                 (int64_t)1000000 * 
728                 (int64_t)2 / 
729                 (int64_t)samplerate;
730         if(interrupted) return;
732 //printf("Device1394Output::write_samples 2\n");
734 // Check for maximum sample count exceeded
735         if(samples > OUTPUT_SAMPLES)
736         {
737                 printf("Device1394Output::write_samples samples=%d > OUTPUT_SAMPLES=%d\n",
738                         samples,
739                         OUTPUT_SAMPLES);
740                 return;
741         }
743 //printf("Device1394Output::write_samples 3\n");
744 // Take over buffer table
745         buffer_lock->lock();
746 // Wait for buffer to become available with timeout
747         while(audio_samples > OUTPUT_SAMPLES - samples && !result && !interrupted)
748         {
749                 buffer_lock->unlock();
750                 result = audio_lock->timed_lock(BUFFER_TIMEOUT);
751                 buffer_lock->lock();
752         }
754         if(!interrupted && audio_samples <= OUTPUT_SAMPLES - samples)
755         {
756 //printf("Device1394Output::write_samples 4 %d\n", audio_samples);
757                 memcpy(audio_buffer + audio_samples * channels * bits / 8,
758                         data,
759                         samples * channels * bits / 8);
760                 audio_samples += samples;
761         }
762         buffer_lock->unlock();
763         start_lock->unlock();
764 //printf("Device1394Output::write_samples 100\n");
767 long Device1394Output::get_audio_position()
769         position_lock->lock();
770         long result = audio_position;
771         position_lock->unlock();
772         return result;
775 void Device1394Output::interrupt()
777         interrupted = 1;
778 // Break write_samples out of a lock
779         video_lock->unlock();
780         audio_lock->unlock();
781 // Playback should stop when the object is deleted.
784 void Device1394Output::flush()
786         
789 void Device1394Output::increment_counter(int *counter)
791         (*counter)++;
792         if(*counter >= total_buffers) *counter = 0;
795 void Device1394Output::decrement_counter(int *counter)
797         (*counter)--;
798         if(*counter < 0) *counter = total_buffers - 1;
814 #endif // HAVE_FIREWIRE