4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "audiodevice.h"
27 #include "bccmodels.h"
28 #include "condition.h"
29 #include "device1394output.h"
32 #include "playbackconfig.h"
35 #include "videodevice.h"
40 #include <sys/ioctl.h>
43 #include <sys/utsname.h>
50 #define CIP_N_NTSC 2436
51 #define CIP_D_NTSC 38400
54 #define OUTPUT_SAMPLES 262144
55 #define BUFFER_TIMEOUT 500000
58 Device1394Output::Device1394Output(AudioDevice *adevice)
62 this->adevice = adevice;
67 Device1394Output::Device1394Output(VideoDevice *vdevice)
71 this->vdevice = vdevice;
76 Device1394Output::~Device1394Output()
88 for(int i = 0; i < total_buffers; i++)
90 if(buffer[i]) delete [] buffer[i];
93 delete [] buffer_size;
94 delete [] buffer_valid;
97 if(audio_lock) delete audio_lock;
98 if(video_lock) delete video_lock;
99 if(start_lock) delete start_lock;
100 if(audio_buffer) delete [] audio_buffer;
104 output_queue.buffer = (output_mmap.nb_buffers + output_queue.buffer - 1) % output_mmap.nb_buffers;
108 if(ioctl(output_fd, dv1394_wait_frames, status.init.n_frames - 1) < 0)
111 "Device1394Output::close_all: DV1394_WAIT_FRAMES %i: %s",
112 output_mmap.nb_buffers,
115 munmap(output_buffer, status.init.n_frames *
116 (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE));
117 if(ioctl(output_fd, dv1394_shutdown, NULL) < 0)
119 perror("Device1394Output::close_all: DV1394_SHUTDOWN");
124 if(ioctl(output_fd, video1394_talk_wait_buffer, &output_queue) < 0)
127 "Device1394::close_all: VIDEO1394_TALK_WAIT_BUFFER: %s",
130 munmap(output_buffer, output_mmap.nb_buffers * output_mmap.buf_size);
132 if(ioctl(output_fd, video1394_untalk_channel, &output_mmap.channel) < 0)
134 perror("Device1394::close_all: VIDEO1394_UNTALK_CHANNEL");
141 // raw1394_destroy_handle(avc_handle);
144 if(temp_frame) delete temp_frame;
145 if(temp_frame2) delete temp_frame2;
146 if(video_encoder) dv_delete(video_encoder);
147 if(position_presented) delete [] position_presented;
148 if(audio_encoder) dv_delete(audio_encoder);
149 if(buffer_lock) delete buffer_lock;
150 if(position_lock) delete position_lock;
154 void Device1394Output::reset()
159 current_inbuffer = 0;
160 current_outbuffer = 0;
177 position_presented = 0;
184 int Device1394Output::get_dv1394()
186 if(adevice) return adevice->out_config->driver == AUDIO_DV1394;
187 if(vdevice) return vdevice->out_config->driver == PLAYBACK_DV1394;
191 int Device1394Output::open(char *path,
200 this->channels = channels;
202 this->samplerate = samplerate;
203 this->total_buffers = length;
206 // dv1394 syt is given in frames and limited to 2 or 3, default is 3
207 // Needs to be accurate to calculate presentation time
208 if (syt < 2 || syt > 3)
213 // Set PAL mode based on frame height
214 if(vdevice) is_pal = (vdevice->out_h == 576);
216 struct dv1394_init setup =
218 api_version: DV1394_API_VERSION,
219 channel: (unsigned int)channel,
220 n_frames: (unsigned int)length,
221 format: is_pal ? DV1394_PAL : DV1394_NTSC,
224 syt_offset: (unsigned int)syt
229 //printf("Device1394::open_output 2 %s %d %d %d %d\n", path, port, channel, length, syt);
232 output_fd = ::open(path, O_RDWR);
237 "Device1394Output::open path=%s: %s\n",
244 output_mmap.channel = channel;
245 output_queue.channel = channel;
246 output_mmap.sync_tag = 0;
247 output_mmap.nb_buffers = total_buffers;
248 output_mmap.buf_size = 320 * 512;
249 output_mmap.packet_size = 512;
250 // Shouldn't this be handled by the video1394 driver?
251 // dvgrab originally used 19000
252 // JVC DVL300 -> 30000
253 output_mmap.syt_offset = syt;
254 output_mmap.flags = VIDEO1394_VARIABLE_PACKET_SIZE;
260 if(ioctl(output_fd, dv1394_init, &setup) < 0)
262 perror("Device1394Output::open DV1394_INIT");
265 if(ioctl(output_fd, dv1394_get_status, &status) < 0)
267 perror("Device1394Output::open DV1394_GET_STATUS");
270 output_buffer = (unsigned char*)mmap(0,
271 output_mmap.nb_buffers * (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE),
272 PROT_READ | PROT_WRITE,
277 if(position_presented) delete [] position_presented;
278 position_presented = new long[length];
279 for (int i = 0; i < length; i++)
280 position_presented[i] = 0;
284 if(ioctl(output_fd, video1394_talk_channel, &output_mmap) < 0)
286 perror("Device1394Output::open VIDEO1394_TALK_CHANNEL:");
289 output_buffer = (unsigned char*)mmap(0,
290 output_mmap.nb_buffers * output_mmap.buf_size,
291 PROT_READ | PROT_WRITE,
297 if(output_buffer <= 0)
299 perror("Device1394Output::open mmap");
302 unused_buffers = output_mmap.nb_buffers;
303 output_queue.buffer = 0;
304 output_queue.packet_sizes = packet_sizes;
305 continuity_counter = 0;
309 buffer = new char*[total_buffers];
310 for(int i = 0; i < length; i++)
311 buffer[i] = new char[get_dv1394() ?
312 (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE) :
314 buffer_size = new int[total_buffers];
315 buffer_valid = new int[total_buffers];
316 bzero(buffer_size, sizeof(int) * total_buffers);
317 bzero(buffer_valid, sizeof(int) * total_buffers);
318 bzero(buffer, sizeof(char*) * total_buffers);
319 video_lock = new Condition(0, "Device1394Output::video_lock");
320 audio_lock = new Condition(0, "Device1394Output::audio_lock");
321 start_lock = new Condition(0, "Device1394Output::start_lock");
322 buffer_lock = new Mutex("Device1394Output::buffer_lock");
323 position_lock = new Mutex("Device1394Output::position_lock");
325 audio_buffer = new char[OUTPUT_SAMPLES * channels * bits / 8];
332 void Device1394Output::run()
334 unsigned char *output;
338 Thread::enable_cancel();
339 start_lock->lock("Device1394Output::run");
340 Thread::disable_cancel();
343 // Write buffers continuously
346 // Get current buffer to play
349 buffer_lock->lock("Device1394Output::run 1");
351 out_buffer = buffer[current_outbuffer];
352 out_size = buffer_size[current_outbuffer];
358 // No video. Put in a fake frame for audio only
361 #include "data/fake_ntsc_dv.h"
362 out_size = sizeof(fake_ntsc_dv) - 4;
363 out_buffer = (char*)fake_ntsc_dv + 4;
371 output = output_buffer +
373 status.first_clear_frame;
377 output = output_buffer +
378 output_queue.buffer *
379 output_mmap.buf_size;
390 if(out_buffer && out_size)
392 // Calculate number of samples needed based on given pattern for
394 int samples_per_frame = 2048;
397 if(audio_samples > samples_per_frame)
400 int samples_written = dv_write_audio(encoder,
401 (unsigned char*)out_buffer,
402 (unsigned char*)audio_buffer,
407 is_pal ? DV_PAL : DV_NTSC);
409 audio_buffer + samples_written * bits * channels / 8,
410 (audio_samples - samples_written) * bits * channels / 8);
411 audio_samples -= samples_written;
412 position_lock->lock("Device1394Output::run");
416 // When this frame is being uploaded to the 1394 device,
417 // the frame actually playing on the device will be the one
418 // uploaded syt frames before.
419 position_presented[status.first_clear_frame] =
420 audio_position - syt * samples_per_frame;
421 if (position_presented[status.first_clear_frame] < 0)
422 position_presented[status.first_clear_frame] = 0;
425 audio_position += samples_written;
426 position_lock->unlock();
429 audio_lock->unlock();
432 // Copy from current buffer to mmap buffer with firewire encryption
435 memcpy(output, out_buffer, out_size);
439 encrypt((unsigned char*)output,
440 (unsigned char*)out_buffer,
443 buffer_valid[current_outbuffer] = 0;
446 // Advance buffer number if possible
447 increment_counter(¤t_outbuffer);
449 // Reuse same buffer next time
450 if(!buffer_valid[current_outbuffer])
452 decrement_counter(¤t_outbuffer);
455 // Wait for user to reach current buffer before unlocking any more.
457 video_lock->unlock();
461 buffer_lock->unlock();
462 //printf("Device1394Output::run 100\n");
469 // Write mmap to device
470 Thread::enable_cancel();
476 if(ioctl(output_fd, dv1394_submit_frames, 1) < 0)
478 perror("Device1394Output::run DV1394_SUBMIT_FRAMES");
480 if(ioctl(output_fd, dv1394_wait_frames, 1) < 0)
482 perror("Device1394Output::run DV1394_WAIT_FRAMES");
484 if(ioctl(output_fd, dv1394_get_status, &status) < 0)
486 perror("Device1394Output::run DV1394_GET_STATUS");
491 if(ioctl(output_fd, video1394_talk_queue_buffer, &output_queue) < 0)
493 perror("Device1394Output::run VIDEO1394_TALK_QUEUE_BUFFER");
497 output_queue.buffer++;
498 if(output_queue.buffer >= output_mmap.nb_buffers)
499 output_queue.buffer = 0;
501 if(unused_buffers <= 0)
505 if(ioctl(output_fd, video1394_talk_wait_buffer, &output_queue) < 0)
507 perror("Device1394::run VIDEO1394_TALK_WAIT_BUFFER");
514 Thread::disable_cancel();
518 // Thread::enable_cancel();
519 // start_lock->lock();
520 // Thread::disable_cancel();
523 //printf("Device1394Output::run " _LD "\n", timer.get_difference());
529 void Device1394Output::encrypt(unsigned char *output,
533 // Encode in IEEE1394 video encryption
534 int output_size = 320;
535 int packets_per_frame = (is_pal ? 300 : 250);
536 int min_packet_size = output_mmap.packet_size;
537 unsigned long frame_size = packets_per_frame * 480;
538 unsigned long vdata = 0;
539 unsigned int *packet_sizes = this->packet_sizes;
561 for(int i = 0; i < output_size && vdata < frame_size; i++)
563 unsigned char *p = output;
564 int want_sync = (cip_counter > cip_d);
566 /* Source node ID ! */
568 /* Packet size in quadlets (480 / 4) - this stays the same even for empty packets */
571 *p++ = continuity_counter;
575 /* high bit = 50/60 indicator */
578 /* timestamp - generated in driver */
586 continuity_counter++;
587 cip_counter += cip_n;
589 memcpy(p, data + vdata, 480);
594 cip_counter -= cip_d;
596 *packet_sizes++ = p - output;
597 output += min_packet_size;
605 void Device1394Output::write_frame(VFrame *input)
610 //printf("Device1394Output::write_frame 1\n");
612 if(output_fd <= 0) return;
613 if(interrupted) return;
615 // Encode frame to DV
616 if(input->get_color_model() != BC_COMPRESSED)
618 if(!temp_frame) temp_frame = new VFrame;
619 if(!encoder) encoder = dv_new();
622 // Exact resolution match. Don't do colorspace conversion
623 if(input->get_color_model() == BC_YUV422 &&
624 input->get_w() == 720 &&
625 (input->get_h() == 480 ||
626 input->get_h() == 576))
628 int norm = is_pal ? DV_PAL : DV_NTSC;
629 int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
630 temp_frame->allocate_compressed_data(data_size);
631 temp_frame->set_compressed_size(data_size);
633 dv_write_video(encoder,
634 temp_frame->get_data(),
641 // Convert resolution and color model before compressing
645 int h = input->get_h();
646 // Default to NTSC if unknown
647 if(h != 480 && h != 576) h = 480;
649 temp_frame2 = new VFrame(0,
658 int norm = is_pal ? DV_PAL : DV_NTSC;
659 int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
660 temp_frame->allocate_compressed_data(data_size);
661 temp_frame->set_compressed_size(data_size);
664 BC_CModels::transfer(temp_frame2->get_rows(), /* Leave NULL if non existent */
666 temp_frame2->get_y(), /* Leave NULL if non existent */
667 temp_frame2->get_u(),
668 temp_frame2->get_v(),
669 input->get_y(), /* Leave NULL if non existent */
672 0, /* Dimensions to capture from input frame */
674 MIN(temp_frame2->get_w(), input->get_w()),
675 MIN(temp_frame2->get_h(), input->get_h()),
676 0, /* Dimensions to project on output frame */
678 MIN(temp_frame2->get_w(), input->get_w()),
679 MIN(temp_frame2->get_h(), input->get_h()),
680 input->get_color_model(),
682 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
683 input->get_bytes_per_line(), /* For planar use the luma rowspan */
684 temp_frame2->get_bytes_per_line()); /* For planar use the luma rowspan */
686 dv_write_video(encoder,
687 temp_frame->get_data(),
688 temp_frame2->get_rows(),
710 // Take over buffer table
711 buffer_lock->lock("Device1394Output::write_frame 1");
713 // Wait for buffer to become available with timeout
714 while(buffer_valid[current_inbuffer] && !result && !interrupted)
716 buffer_lock->unlock();
717 result = video_lock->timed_lock(BUFFER_TIMEOUT);
718 buffer_lock->lock("Device1394Output::write_frame 2");
723 // Write buffer if there's room
724 if(!buffer_valid[current_inbuffer])
726 if(!buffer[current_inbuffer])
728 buffer[current_inbuffer] = new char[ptr->get_compressed_size()];
729 buffer_size[current_inbuffer] = ptr->get_compressed_size();
731 memcpy(buffer[current_inbuffer], ptr->get_data(), ptr->get_compressed_size());
732 buffer_valid[current_inbuffer] = 1;
733 increment_counter(¤t_inbuffer);
736 // Ignore it if there isn't room.
741 buffer_lock->unlock();
742 start_lock->unlock();
743 //printf("Device1394Output::write_frame 100\n");
746 void Device1394Output::write_samples(char *data, int samples)
748 //printf("Device1394Output::write_samples 1\n");
750 //int timeout = (samples * 1000000LL * 2)/ samplerate;
751 if(interrupted) return;
753 //printf("Device1394Output::write_samples 2\n");
755 // Check for maximum sample count exceeded
756 if(samples > OUTPUT_SAMPLES)
758 printf("Device1394Output::write_samples samples=%d > OUTPUT_SAMPLES=%d\n",
764 //printf("Device1394Output::write_samples 3\n");
765 // Take over buffer table
766 buffer_lock->lock("Device1394Output::write_samples 1");
767 // Wait for buffer to become available with timeout
768 while(audio_samples > OUTPUT_SAMPLES - samples && !result && !interrupted)
770 buffer_lock->unlock();
771 result = audio_lock->timed_lock(BUFFER_TIMEOUT);
772 buffer_lock->lock("Device1394Output::write_samples 2");
775 if(!interrupted && audio_samples <= OUTPUT_SAMPLES - samples)
777 //printf("Device1394Output::write_samples 4 %d\n", audio_samples);
778 memcpy(audio_buffer + audio_samples * channels * bits / 8,
780 samples * channels * bits / 8);
781 audio_samples += samples;
783 buffer_lock->unlock();
784 start_lock->unlock();
785 //printf("Device1394Output::write_samples 100\n");
788 long Device1394Output::get_audio_position()
790 position_lock->lock("Device1394Output::get_audio_position");
791 long result = audio_position;
794 // Take delay between placing in buffer and presentation
795 // on device into account for dv1394
796 result = position_presented[status.active_frame];
798 position_lock->unlock();
802 void Device1394Output::interrupt()
805 // Break write_samples out of a lock
806 video_lock->unlock();
807 audio_lock->unlock();
808 // Playback should stop when the object is deleted.
811 void Device1394Output::flush()
816 void Device1394Output::increment_counter(int *counter)
819 if(*counter >= total_buffers) *counter = 0;
822 void Device1394Output::decrement_counter(int *counter)
825 if(*counter < 0) *counter = total_buffers - 1;
828 void Device1394Output::set_ioctls()
830 // It would make sense to simply change the file that is included in
831 // order to change the IOCTLs, but it isn't reasonable to think that
832 // people will rebuild their software every time the update their
833 // kernel, hence this fix.
837 // Get the kernel version so we can set the right ioctls
840 char *ptr = strchr(buf.release, '.');
847 major = atoi(buf.release);
848 char *ptr2 = strchr(ptr, '.');
857 if( (major >= 2 && minor >= 6 && point >= 0) ||
858 (major >= 2 && minor >= 4 && point >= 23) )
861 dv1394_init = DV1394_IOC_INIT;
862 dv1394_shutdown = DV1394_IOC_SHUTDOWN;
863 dv1394_submit_frames = DV1394_IOC_SUBMIT_FRAMES;
864 dv1394_wait_frames = DV1394_IOC_WAIT_FRAMES;
865 dv1394_receive_frames = DV1394_IOC_RECEIVE_FRAMES;
866 dv1394_start_receive = DV1394_IOC_START_RECEIVE;
867 dv1394_get_status = DV1394_IOC_GET_STATUS;
870 video1394_listen_channel = VIDEO1394_IOC_LISTEN_CHANNEL;
871 video1394_unlisten_channel = VIDEO1394_IOC_UNLISTEN_CHANNEL;
872 video1394_listen_queue_buffer = VIDEO1394_IOC_LISTEN_QUEUE_BUFFER;
873 video1394_listen_wait_buffer = VIDEO1394_IOC_LISTEN_WAIT_BUFFER;
874 video1394_talk_channel = VIDEO1394_IOC_TALK_CHANNEL;
875 video1394_untalk_channel = VIDEO1394_IOC_UNTALK_CHANNEL;
876 video1394_talk_queue_buffer = VIDEO1394_IOC_TALK_QUEUE_BUFFER;
877 video1394_talk_wait_buffer = VIDEO1394_IOC_TALK_WAIT_BUFFER;
878 video1394_listen_poll_buffer = VIDEO1394_IOC_LISTEN_POLL_BUFFER;
881 // Nothing uses this right now, so I didn't include it.
883 else // we are using an older kernel
886 dv1394_init = DV1394_INIT;
887 dv1394_shutdown = DV1394_SHUTDOWN;
888 dv1394_submit_frames = DV1394_SUBMIT_FRAMES;
889 dv1394_wait_frames = DV1394_WAIT_FRAMES;
890 dv1394_receive_frames = DV1394_RECEIVE_FRAMES;
891 dv1394_start_receive = DV1394_START_RECEIVE;
892 dv1394_get_status = DV1394_GET_STATUS;
895 video1394_listen_channel = VIDEO1394_LISTEN_CHANNEL;
896 video1394_unlisten_channel = VIDEO1394_UNLISTEN_CHANNEL;
897 video1394_listen_queue_buffer = VIDEO1394_LISTEN_QUEUE_BUFFER;
898 video1394_listen_wait_buffer = VIDEO1394_LISTEN_WAIT_BUFFER;
899 video1394_talk_channel = VIDEO1394_TALK_CHANNEL;
900 video1394_untalk_channel = VIDEO1394_UNTALK_CHANNEL;
901 video1394_talk_queue_buffer = VIDEO1394_TALK_QUEUE_BUFFER;
902 video1394_talk_wait_buffer = VIDEO1394_TALK_WAIT_BUFFER;
903 video1394_listen_poll_buffer = VIDEO1394_LISTEN_POLL_BUFFER;
918 #endif // HAVE_FIREWIRE