5 #include "audiodevice.h"
7 #include "device1394output.h"
9 #include "playbackconfig.h"
12 #include "videodevice.h"
17 #include <sys/ioctl.h>
20 #include <sys/utsname.h>
27 #define CIP_N_NTSC 2436
28 #define CIP_D_NTSC 38400
31 #define OUTPUT_SAMPLES 262144
32 #define BUFFER_TIMEOUT 500000
35 Device1394Output::Device1394Output(AudioDevice *adevice)
39 this->adevice = adevice;
44 Device1394Output::Device1394Output(VideoDevice *vdevice)
48 this->vdevice = vdevice;
53 Device1394Output::~Device1394Output()
65 for(int i = 0; i < total_buffers; i++)
67 if(buffer[i]) delete [] buffer[i];
70 delete [] buffer_size;
71 delete [] buffer_valid;
74 if(audio_lock) delete audio_lock;
75 if(video_lock) delete video_lock;
76 if(start_lock) delete start_lock;
77 if(audio_buffer) delete [] audio_buffer;
81 output_queue.buffer = (output_mmap.nb_buffers + output_queue.buffer - 1) % output_mmap.nb_buffers;
85 if(ioctl(output_fd, DV1394_IOC_WAIT_FRAMES, status.init.n_frames - 1) < 0)
88 "Device1394Output::close_all: DV1394_WAIT_FRAMES %i: %s",
89 output_mmap.nb_buffers,
92 munmap(output_buffer, status.init.n_frames *
93 (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE));
94 if(ioctl(output_fd, DV1394_IOC_SHUTDOWN, NULL) < 0)
96 perror("Device1394Output::close_all: DV1394_SHUTDOWN");
101 if(ioctl(output_fd, video1394_talk_wait_buffer, &output_queue) < 0)
104 "Device1394::close_all: VIDEO1394_TALK_WAIT_BUFFER %s: %s",
108 munmap(output_buffer, output_mmap.nb_buffers * output_mmap.buf_size);
110 if(ioctl(output_fd, video1394_untalk_channel, &output_mmap.channel) < 0)
112 perror("Device1394::close_all: VIDEO1394_UNTALK_CHANNEL");
119 // raw1394_destroy_handle(avc_handle);
122 if(temp_frame) delete temp_frame;
123 if(temp_frame2) delete temp_frame2;
124 if(video_encoder) dv_delete(video_encoder);
125 if(position_presented) delete position_presented;
126 if(audio_encoder) dv_delete(audio_encoder);
127 if(buffer_lock) delete buffer_lock;
128 if(position_lock) delete position_lock;
132 void Device1394Output::reset()
137 current_inbuffer = 0;
138 current_outbuffer = 0;
155 position_presented = 0;
162 int Device1394Output::get_dv1394()
164 if(adevice) return adevice->out_config->driver == AUDIO_DV1394;
165 if(vdevice) return vdevice->out_config->driver == PLAYBACK_DV1394;
168 int Device1394Output::open(char *path,
177 this->channels = channels;
179 this->samplerate = samplerate;
180 this->total_buffers = length;
183 // dv1394 syt is given in frames and limited to 2 or 3, default is 3
184 // Needs to be accurate to calculate presentation time
185 if (syt < 2 || syt > 3)
190 // Set PAL mode based on frame height
191 if(vdevice) is_pal = (vdevice->out_h == 576);
193 struct dv1394_init setup =
195 api_version: DV1394_API_VERSION,
198 format: is_pal ? DV1394_PAL : DV1394_NTSC,
206 //printf("Device1394::open_output 2 %s %d %d %d %d\n", path, port, channel, length, syt);
209 output_fd = ::open(path, O_RDWR);
214 "Device1394Output::open path=%s: %s\n",
221 output_mmap.channel = channel;
222 output_queue.channel = channel;
223 output_mmap.sync_tag = 0;
224 output_mmap.nb_buffers = total_buffers;
225 output_mmap.buf_size = 320 * 512;
226 output_mmap.packet_size = 512;
227 // Shouldn't this be handled by the video1394 driver?
228 // dvgrab originally used 19000
229 // JVC DVL300 -> 30000
230 output_mmap.syt_offset = syt;
231 output_mmap.flags = VIDEO1394_VARIABLE_PACKET_SIZE;
237 if(ioctl(output_fd, DV1394_IOC_INIT, &setup) < 0)
239 perror("Device1394Output::open DV1394_INIT");
242 if(ioctl(output_fd, DV1394_IOC_GET_STATUS, &status) < 0)
244 perror("Device1394Output::open DV1394_GET_STATUS");
247 output_buffer = (unsigned char*)mmap(0,
248 output_mmap.nb_buffers * (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE),
249 PROT_READ | PROT_WRITE,
254 position_presented = new long[length];
255 for (int i = 0; i < length; i++)
256 position_presented[i] = 0;
260 if(ioctl(output_fd, video1394_talk_channel, &output_mmap) < 0)
262 perror("Device1394Output::open VIDEO1394_TALK_CHANNEL:");
265 output_buffer = (unsigned char*)mmap(0,
266 output_mmap.nb_buffers * output_mmap.buf_size,
267 PROT_READ | PROT_WRITE,
273 if(output_buffer <= 0)
275 perror("Device1394Output::open mmap");
278 unused_buffers = output_mmap.nb_buffers;
279 output_queue.buffer = 0;
280 output_queue.packet_sizes = packet_sizes;
281 continuity_counter = 0;
285 buffer = new char*[total_buffers];
286 for(int i = 0; i < length; i++)
287 buffer[i] = new char[get_dv1394() ?
288 (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE) :
290 buffer_size = new int[total_buffers];
291 buffer_valid = new int[total_buffers];
292 bzero(buffer_size, sizeof(int) * total_buffers);
293 bzero(buffer_valid, sizeof(int) * total_buffers);
294 bzero(buffer, sizeof(char*) * total_buffers);
295 video_lock = new Condition(0, "Device1394Output::video_lock");
296 audio_lock = new Condition(0, "Device1394Output::audio_lock");
297 start_lock = new Condition(0, "Device1394Output::start_lock");
298 buffer_lock = new Mutex("Device1394Output::buffer_lock");
299 position_lock = new Mutex("Device1394Output::position_lock");
301 audio_buffer = new char[OUTPUT_SAMPLES * channels * bits / 8];
308 void Device1394Output::run()
310 unsigned char *output;
314 Thread::enable_cancel();
315 start_lock->lock("Device1394Output::run");
316 Thread::disable_cancel();
319 // Write buffers continuously
322 // Get current buffer to play
325 buffer_lock->lock("Device1394Output::run 1");
327 out_buffer = buffer[current_outbuffer];
328 out_size = buffer_size[current_outbuffer];
334 // No video. Put in a fake frame for audio only
337 #include "data/fake_ntsc_dv.h"
338 out_size = sizeof(fake_ntsc_dv) - 4;
339 out_buffer = (char*)fake_ntsc_dv + 4;
347 output = output_buffer +
349 status.first_clear_frame;
353 output = output_buffer +
354 output_queue.buffer *
355 output_mmap.buf_size;
366 if(out_buffer && out_size)
368 // Calculate number of samples needed based on given pattern for
370 int samples_per_frame = 2048;
373 if(audio_samples > samples_per_frame)
376 int samples_written = dv_write_audio(encoder,
377 (unsigned char*)out_buffer,
378 (unsigned char*)audio_buffer,
383 is_pal ? DV_PAL : DV_NTSC);
385 audio_buffer + samples_written * bits * channels / 8,
386 (audio_samples - samples_written) * bits * channels / 8);
387 audio_samples -= samples_written;
388 position_lock->lock("Device1394Output::run");
392 // When this frame is being uploaded to the 1394 device,
393 // the frame actually playing on the device will be the one
394 // uploaded syt frames before.
395 position_presented[status.first_clear_frame] =
396 audio_position - syt * samples_per_frame;
397 if (position_presented[status.first_clear_frame] < 0)
398 position_presented[status.first_clear_frame] = 0;
401 audio_position += samples_written;
402 position_lock->unlock();
405 audio_lock->unlock();
408 // Copy from current buffer to mmap buffer with firewire encryption
411 memcpy(output, out_buffer, out_size);
415 encrypt((unsigned char*)output,
416 (unsigned char*)out_buffer,
419 buffer_valid[current_outbuffer] = 0;
422 // Advance buffer number if possible
423 increment_counter(¤t_outbuffer);
425 // Reuse same buffer next time
426 if(!buffer_valid[current_outbuffer])
428 decrement_counter(¤t_outbuffer);
431 // Wait for user to reach current buffer before unlocking any more.
433 video_lock->unlock();
437 buffer_lock->unlock();
438 //printf("Device1394Output::run 100\n");
445 // Write mmap to device
446 Thread::enable_cancel();
452 if(ioctl(output_fd, DV1394_IOC_SUBMIT_FRAMES, 1) < 0)
454 perror("Device1394Output::run DV1394_SUBMIT_FRAMES");
456 if(ioctl(output_fd, DV1394_IOC_WAIT_FRAMES, 1) < 0)
458 perror("Device1394Output::run DV1394_WAIT_FRAMES");
460 if(ioctl(output_fd, DV1394_IOC_GET_STATUS, &status) < 0)
462 perror("Device1394Output::run DV1394_GET_STATUS");
467 if(ioctl(output_fd, video1394_talk_queue_buffer, &output_queue) < 0)
469 perror("Device1394Output::run VIDEO1394_TALK_QUEUE_BUFFER");
473 output_queue.buffer++;
474 if(output_queue.buffer >= output_mmap.nb_buffers)
475 output_queue.buffer = 0;
477 if(unused_buffers <= 0)
481 if(ioctl(output_fd, video1394_talk_wait_buffer, &output_queue) < 0)
483 perror("Device1394::run VIDEO1394_TALK_WAIT_BUFFER");
490 Thread::disable_cancel();
494 // Thread::enable_cancel();
495 // start_lock->lock();
496 // Thread::disable_cancel();
499 //printf("Device1394Output::run %lld\n", timer.get_difference());
505 void Device1394Output::encrypt(unsigned char *output,
509 // Encode in IEEE1394 video encryption
510 int output_size = 320;
511 int packets_per_frame = (is_pal ? 300 : 250);
512 int min_packet_size = output_mmap.packet_size;
513 unsigned long frame_size = packets_per_frame * 480;
514 unsigned long vdata = 0;
515 unsigned int *packet_sizes = this->packet_sizes;
537 for(int i = 0; i < output_size && vdata < frame_size; i++)
539 unsigned char *p = output;
540 int want_sync = (cip_counter > cip_d);
542 /* Source node ID ! */
544 /* Packet size in quadlets (480 / 4) - this stays the same even for empty packets */
547 *p++ = continuity_counter;
551 /* high bit = 50/60 indicator */
554 /* timestamp - generated in driver */
562 continuity_counter++;
563 cip_counter += cip_n;
565 memcpy(p, data + vdata, 480);
570 cip_counter -= cip_d;
572 *packet_sizes++ = p - output;
573 output += min_packet_size;
581 void Device1394Output::write_frame(VFrame *input)
586 //printf("Device1394Output::write_frame 1\n");
588 if(output_fd <= 0) return;
589 if(interrupted) return;
591 // Encode frame to DV
592 if(input->get_color_model() != BC_COMPRESSED)
594 if(!temp_frame) temp_frame = new VFrame;
595 if(!encoder) encoder = dv_new();
598 // Exact resolution match. Don't do colorspace conversion
599 if(input->get_color_model() == BC_YUV422 &&
600 input->get_w() == 720 &&
601 (input->get_h() == 480 ||
602 input->get_h() == 576))
604 int norm = is_pal ? DV_PAL : DV_NTSC;
605 int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
606 temp_frame->allocate_compressed_data(data_size);
607 temp_frame->set_compressed_size(data_size);
609 dv_write_video(encoder,
610 temp_frame->get_data(),
617 // Convert resolution and color model before compressing
621 int h = input->get_h();
622 // Default to NTSC if unknown
623 if(h != 480 && h != 576) h = 480;
625 temp_frame2 = new VFrame(0,
632 int norm = is_pal ? DV_PAL : DV_NTSC;
633 int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
634 temp_frame->allocate_compressed_data(data_size);
635 temp_frame->set_compressed_size(data_size);
638 cmodel_transfer(temp_frame2->get_rows(), /* Leave NULL if non existent */
640 temp_frame2->get_y(), /* Leave NULL if non existent */
641 temp_frame2->get_u(),
642 temp_frame2->get_v(),
643 input->get_y(), /* Leave NULL if non existent */
646 0, /* Dimensions to capture from input frame */
648 MIN(temp_frame2->get_w(), input->get_w()),
649 MIN(temp_frame2->get_h(), input->get_h()),
650 0, /* Dimensions to project on output frame */
652 MIN(temp_frame2->get_w(), input->get_w()),
653 MIN(temp_frame2->get_h(), input->get_h()),
654 input->get_color_model(),
656 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
657 input->get_bytes_per_line(), /* For planar use the luma rowspan */
658 temp_frame2->get_bytes_per_line()); /* For planar use the luma rowspan */
660 dv_write_video(encoder,
661 temp_frame->get_data(),
662 temp_frame2->get_rows(),
684 // Take over buffer table
685 buffer_lock->lock("Device1394Output::write_frame 1");
687 // Wait for buffer to become available with timeout
688 while(buffer_valid[current_inbuffer] && !result && !interrupted)
690 buffer_lock->unlock();
691 result = video_lock->timed_lock(BUFFER_TIMEOUT);
692 buffer_lock->lock("Device1394Output::write_frame 2");
697 // Write buffer if there's room
698 if(!buffer_valid[current_inbuffer])
700 if(!buffer[current_inbuffer])
702 buffer[current_inbuffer] = new char[ptr->get_compressed_size()];
703 buffer_size[current_inbuffer] = ptr->get_compressed_size();
705 memcpy(buffer[current_inbuffer], ptr->get_data(), ptr->get_compressed_size());
706 buffer_valid[current_inbuffer] = 1;
707 increment_counter(¤t_inbuffer);
710 // Ignore it if there isn't room.
715 buffer_lock->unlock();
716 start_lock->unlock();
717 //printf("Device1394Output::write_frame 100\n");
720 void Device1394Output::write_samples(char *data, int samples)
722 //printf("Device1394Output::write_samples 1\n");
724 int timeout = (int64_t)samples *
728 if(interrupted) return;
730 //printf("Device1394Output::write_samples 2\n");
732 // Check for maximum sample count exceeded
733 if(samples > OUTPUT_SAMPLES)
735 printf("Device1394Output::write_samples samples=%d > OUTPUT_SAMPLES=%d\n",
741 //printf("Device1394Output::write_samples 3\n");
742 // Take over buffer table
743 buffer_lock->lock("Device1394Output::write_samples 1");
744 // Wait for buffer to become available with timeout
745 while(audio_samples > OUTPUT_SAMPLES - samples && !result && !interrupted)
747 buffer_lock->unlock();
748 result = audio_lock->timed_lock(BUFFER_TIMEOUT);
749 buffer_lock->lock("Device1394Output::write_samples 2");
752 if(!interrupted && audio_samples <= OUTPUT_SAMPLES - samples)
754 //printf("Device1394Output::write_samples 4 %d\n", audio_samples);
755 memcpy(audio_buffer + audio_samples * channels * bits / 8,
757 samples * channels * bits / 8);
758 audio_samples += samples;
760 buffer_lock->unlock();
761 start_lock->unlock();
762 //printf("Device1394Output::write_samples 100\n");
765 long Device1394Output::get_audio_position()
767 position_lock->lock("Device1394Output::get_audio_position");
768 long result = audio_position;
771 // Take delay between placing in buffer and presentation
772 // on device into account for dv1394
773 result = position_presented[status.active_frame];
775 position_lock->unlock();
779 void Device1394Output::interrupt()
782 // Break write_samples out of a lock
783 video_lock->unlock();
784 audio_lock->unlock();
785 // Playback should stop when the object is deleted.
788 void Device1394Output::flush()
793 void Device1394Output::increment_counter(int *counter)
796 if(*counter >= total_buffers) *counter = 0;
799 void Device1394Output::decrement_counter(int *counter)
802 if(*counter < 0) *counter = total_buffers - 1;
805 void Device1394Output::set_ioctls()
807 // It would make sense to simply change the file that is included in
808 // order to change the IOCTLs, but it isn't reasonable to think that
809 // people will rebuild their software every time the update their
810 // kernel, hence this fix.
814 // Get the kernel version so we can set the right ioctls
817 char *ptr = strchr(buf.release, '.');
824 major = atoi(buf.release);
825 char *ptr2 = strchr(ptr, '.');
834 if(major >= 2 && minor >= 6 && point >= 0 ||
835 major >= 2 && minor >= 4 && point >= 23)
838 video1394_listen_channel = VIDEO1394_IOC_LISTEN_CHANNEL;
839 video1394_unlisten_channel = VIDEO1394_IOC_UNLISTEN_CHANNEL;
840 video1394_listen_queue_buffer = VIDEO1394_IOC_LISTEN_QUEUE_BUFFER;
841 video1394_listen_wait_buffer = VIDEO1394_IOC_LISTEN_WAIT_BUFFER;
842 video1394_talk_channel = VIDEO1394_IOC_TALK_CHANNEL;
843 video1394_untalk_channel = VIDEO1394_IOC_UNTALK_CHANNEL;
844 video1394_talk_queue_buffer = VIDEO1394_IOC_TALK_QUEUE_BUFFER;
845 video1394_talk_wait_buffer = VIDEO1394_IOC_TALK_WAIT_BUFFER;
846 video1394_listen_poll_buffer = VIDEO1394_IOC_LISTEN_POLL_BUFFER;
849 // Nothing uses this right now, so I didn't include it.
851 else // we are using an older kernel
854 video1394_listen_channel = VIDEO1394_LISTEN_CHANNEL;
855 video1394_unlisten_channel = VIDEO1394_UNLISTEN_CHANNEL;
856 video1394_listen_queue_buffer = VIDEO1394_LISTEN_QUEUE_BUFFER;
857 video1394_listen_wait_buffer = VIDEO1394_LISTEN_WAIT_BUFFER;
858 video1394_talk_channel = VIDEO1394_TALK_CHANNEL;
859 video1394_untalk_channel = VIDEO1394_UNTALK_CHANNEL;
860 video1394_talk_queue_buffer = VIDEO1394_TALK_QUEUE_BUFFER;
861 video1394_talk_wait_buffer = VIDEO1394_TALK_WAIT_BUFFER;
862 video1394_listen_poll_buffer = VIDEO1394_LISTEN_POLL_BUFFER;
877 #endif // HAVE_FIREWIRE