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"
31 #include "playbackconfig.h"
34 #include "videodevice.h"
39 #include <sys/ioctl.h>
42 #include <sys/utsname.h>
49 #define CIP_N_NTSC 2436
50 #define CIP_D_NTSC 38400
53 #define OUTPUT_SAMPLES 262144
54 #define BUFFER_TIMEOUT 500000
57 Device1394Output::Device1394Output(AudioDevice *adevice)
61 this->adevice = adevice;
66 Device1394Output::Device1394Output(VideoDevice *vdevice)
70 this->vdevice = vdevice;
75 Device1394Output::~Device1394Output()
87 for(int i = 0; i < total_buffers; i++)
89 if(buffer[i]) delete [] buffer[i];
92 delete [] buffer_size;
93 delete [] buffer_valid;
96 if(audio_lock) delete audio_lock;
97 if(video_lock) delete video_lock;
98 if(start_lock) delete start_lock;
99 if(audio_buffer) delete [] audio_buffer;
103 output_queue.buffer = (output_mmap.nb_buffers + output_queue.buffer - 1) % output_mmap.nb_buffers;
107 if(ioctl(output_fd, DV1394_IOC_WAIT_FRAMES, status.init.n_frames - 1) < 0)
110 "Device1394Output::close_all: DV1394_WAIT_FRAMES %i: %s",
111 output_mmap.nb_buffers,
114 munmap(output_buffer, status.init.n_frames *
115 (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE));
116 if(ioctl(output_fd, DV1394_IOC_SHUTDOWN, NULL) < 0)
118 perror("Device1394Output::close_all: DV1394_SHUTDOWN");
123 if(ioctl(output_fd, video1394_talk_wait_buffer, &output_queue) < 0)
126 "Device1394::close_all: VIDEO1394_TALK_WAIT_BUFFER %p: %s",
127 &output_queue, strerror(errno));
129 munmap(output_buffer, output_mmap.nb_buffers * output_mmap.buf_size);
131 if(ioctl(output_fd, video1394_untalk_channel, &output_mmap.channel) < 0)
133 perror("Device1394::close_all: VIDEO1394_UNTALK_CHANNEL");
140 // raw1394_destroy_handle(avc_handle);
143 if(temp_frame) delete temp_frame;
144 if(temp_frame2) delete temp_frame2;
145 if(video_encoder) dv_delete(video_encoder);
146 if(position_presented) delete [] position_presented;
147 if(audio_encoder) dv_delete(audio_encoder);
148 if(buffer_lock) delete buffer_lock;
149 if(position_lock) delete position_lock;
153 void Device1394Output::reset()
158 current_inbuffer = 0;
159 current_outbuffer = 0;
176 position_presented = 0;
183 int Device1394Output::get_dv1394()
185 if(adevice) return adevice->out_config->driver == AUDIO_DV1394;
186 if(vdevice) return vdevice->out_config->driver == PLAYBACK_DV1394;
190 int Device1394Output::open(char *path,
199 this->channels = channels;
201 this->samplerate = samplerate;
202 this->total_buffers = length;
205 // dv1394 syt is given in frames and limited to 2 or 3, default is 3
206 // Needs to be accurate to calculate presentation time
207 if (syt < 2 || syt > 3)
212 // Set PAL mode based on frame height
213 if(vdevice) is_pal = (vdevice->out_h == 576);
215 struct dv1394_init setup =
217 api_version: DV1394_API_VERSION,
218 channel: (unsigned int)channel,
219 n_frames: (unsigned int)length,
220 format: is_pal ? DV1394_PAL : DV1394_NTSC,
223 syt_offset: (unsigned int)syt
228 //printf("Device1394::open_output 2 %s %d %d %d %d\n", path, port, channel, length, syt);
231 output_fd = ::open(path, O_RDWR);
236 "Device1394Output::open path=%s: %s\n",
243 output_mmap.channel = channel;
244 output_queue.channel = channel;
245 output_mmap.sync_tag = 0;
246 output_mmap.nb_buffers = total_buffers;
247 output_mmap.buf_size = 320 * 512;
248 output_mmap.packet_size = 512;
249 // Shouldn't this be handled by the video1394 driver?
250 // dvgrab originally used 19000
251 // JVC DVL300 -> 30000
252 output_mmap.syt_offset = syt;
253 output_mmap.flags = VIDEO1394_VARIABLE_PACKET_SIZE;
259 if(ioctl(output_fd, DV1394_IOC_INIT, &setup) < 0)
261 perror("Device1394Output::open DV1394_INIT");
264 if(ioctl(output_fd, DV1394_IOC_GET_STATUS, &status) < 0)
266 perror("Device1394Output::open DV1394_GET_STATUS");
269 output_buffer = (unsigned char*)mmap(0,
270 output_mmap.nb_buffers * (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE),
271 PROT_READ | PROT_WRITE,
276 if(position_presented) delete [] position_presented;
277 position_presented = new long[length];
278 for (int i = 0; i < length; i++)
279 position_presented[i] = 0;
283 if(ioctl(output_fd, video1394_talk_channel, &output_mmap) < 0)
285 perror("Device1394Output::open VIDEO1394_TALK_CHANNEL:");
288 output_buffer = (unsigned char*)mmap(0,
289 output_mmap.nb_buffers * output_mmap.buf_size,
290 PROT_READ | PROT_WRITE,
296 if(output_buffer == MAP_FAILED)
298 perror("Device1394Output::open mmap");
301 unused_buffers = output_mmap.nb_buffers;
302 output_queue.buffer = 0;
303 output_queue.packet_sizes = packet_sizes;
304 continuity_counter = 0;
308 buffer = new char*[total_buffers];
309 for(int i = 0; i < length; i++)
310 buffer[i] = new char[get_dv1394() ?
311 (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE) :
313 buffer_size = new int[total_buffers];
314 buffer_valid = new int[total_buffers];
315 bzero(buffer_size, sizeof(int) * total_buffers);
316 bzero(buffer_valid, sizeof(int) * total_buffers);
317 bzero(buffer, sizeof(char*) * total_buffers);
318 video_lock = new Condition(0, "Device1394Output::video_lock");
319 audio_lock = new Condition(0, "Device1394Output::audio_lock");
320 start_lock = new Condition(0, "Device1394Output::start_lock");
321 buffer_lock = new Mutex("Device1394Output::buffer_lock");
322 position_lock = new Mutex("Device1394Output::position_lock");
324 audio_buffer = new char[OUTPUT_SAMPLES * channels * bits / 8];
331 void Device1394Output::run()
333 unsigned char *output;
337 Thread::enable_cancel();
338 start_lock->lock("Device1394Output::run");
339 Thread::disable_cancel();
342 // Write buffers continuously
345 // Get current buffer to play
348 buffer_lock->lock("Device1394Output::run 1");
350 out_buffer = buffer[current_outbuffer];
351 out_size = buffer_size[current_outbuffer];
357 // No video. Put in a fake frame for audio only
360 #include "data/fake_ntsc_dv.h"
361 out_size = sizeof(fake_ntsc_dv) - 4;
362 out_buffer = (char*)fake_ntsc_dv + 4;
370 output = output_buffer +
372 status.first_clear_frame;
376 output = output_buffer +
377 output_queue.buffer *
378 output_mmap.buf_size;
389 if(out_buffer && out_size)
391 // Calculate number of samples needed based on given pattern for
393 int samples_per_frame = 2048;
396 if(audio_samples > samples_per_frame)
399 int samples_written = dv_write_audio(encoder,
400 (unsigned char*)out_buffer,
401 (unsigned char*)audio_buffer,
406 is_pal ? DV_PAL : DV_NTSC);
408 audio_buffer + samples_written * bits * channels / 8,
409 (audio_samples - samples_written) * bits * channels / 8);
410 audio_samples -= samples_written;
411 position_lock->lock("Device1394Output::run");
415 // When this frame is being uploaded to the 1394 device,
416 // the frame actually playing on the device will be the one
417 // uploaded syt frames before.
418 position_presented[status.first_clear_frame] =
419 audio_position - syt * samples_per_frame;
420 if (position_presented[status.first_clear_frame] < 0)
421 position_presented[status.first_clear_frame] = 0;
424 audio_position += samples_written;
425 position_lock->unlock();
428 audio_lock->unlock();
431 // Copy from current buffer to mmap buffer with firewire encryption
434 memcpy(output, out_buffer, out_size);
438 encrypt((unsigned char*)output,
439 (unsigned char*)out_buffer,
442 buffer_valid[current_outbuffer] = 0;
445 // Advance buffer number if possible
446 increment_counter(¤t_outbuffer);
448 // Reuse same buffer next time
449 if(!buffer_valid[current_outbuffer])
451 decrement_counter(¤t_outbuffer);
454 // Wait for user to reach current buffer before unlocking any more.
456 video_lock->unlock();
460 buffer_lock->unlock();
461 //printf("Device1394Output::run 100\n");
468 // Write mmap to device
469 Thread::enable_cancel();
475 if(ioctl(output_fd, DV1394_IOC_SUBMIT_FRAMES, 1) < 0)
477 perror("Device1394Output::run DV1394_SUBMIT_FRAMES");
479 if(ioctl(output_fd, DV1394_IOC_WAIT_FRAMES, 1) < 0)
481 perror("Device1394Output::run DV1394_WAIT_FRAMES");
483 if(ioctl(output_fd, DV1394_IOC_GET_STATUS, &status) < 0)
485 perror("Device1394Output::run DV1394_GET_STATUS");
490 if(ioctl(output_fd, video1394_talk_queue_buffer, &output_queue) < 0)
492 perror("Device1394Output::run VIDEO1394_TALK_QUEUE_BUFFER");
496 output_queue.buffer++;
497 if(output_queue.buffer >= output_mmap.nb_buffers)
498 output_queue.buffer = 0;
500 if(unused_buffers <= 0)
504 if(ioctl(output_fd, video1394_talk_wait_buffer, &output_queue) < 0)
506 perror("Device1394::run VIDEO1394_TALK_WAIT_BUFFER");
513 Thread::disable_cancel();
517 // Thread::enable_cancel();
518 // start_lock->lock();
519 // Thread::disable_cancel();
522 //printf("Device1394Output::run %jd\n", timer.get_difference());
528 void Device1394Output::encrypt(unsigned char *output,
532 // Encode in IEEE1394 video encryption
533 int output_size = 320;
534 int packets_per_frame = (is_pal ? 300 : 250);
535 int min_packet_size = output_mmap.packet_size;
536 unsigned long frame_size = packets_per_frame * 480;
537 unsigned long vdata = 0;
538 unsigned int *packet_sizes = this->packet_sizes;
560 for(int i = 0; i < output_size && vdata < frame_size; i++)
562 unsigned char *p = output;
563 int want_sync = (cip_counter > cip_d);
565 /* Source node ID ! */
567 /* Packet size in quadlets (480 / 4) - this stays the same even for empty packets */
570 *p++ = continuity_counter;
574 /* high bit = 50/60 indicator */
577 /* timestamp - generated in driver */
585 continuity_counter++;
586 cip_counter += cip_n;
588 memcpy(p, data + vdata, 480);
593 cip_counter -= cip_d;
595 *packet_sizes++ = p - output;
596 output += min_packet_size;
604 void Device1394Output::write_frame(VFrame *input)
609 //printf("Device1394Output::write_frame 1\n");
611 if(output_fd <= 0) return;
612 if(interrupted) return;
614 // Encode frame to DV
615 if(input->get_color_model() != BC_COMPRESSED)
617 if(!temp_frame) temp_frame = new VFrame;
618 if(!encoder) encoder = dv_new();
621 // Exact resolution match. Don't do colorspace conversion
622 if(input->get_color_model() == BC_YUV422 &&
623 input->get_w() == 720 &&
624 (input->get_h() == 480 ||
625 input->get_h() == 576))
627 int norm = is_pal ? DV_PAL : DV_NTSC;
628 int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
629 temp_frame->allocate_compressed_data(data_size);
630 temp_frame->set_compressed_size(data_size);
632 dv_write_video(encoder,
633 temp_frame->get_data(),
640 // Convert resolution and color model before compressing
644 int h = input->get_h();
645 // Default to NTSC if unknown
646 if(h != 480 && h != 576) h = 480;
648 temp_frame2 = new VFrame(0,
657 int norm = is_pal ? DV_PAL : DV_NTSC;
658 int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
659 temp_frame->allocate_compressed_data(data_size);
660 temp_frame->set_compressed_size(data_size);
663 BC_CModels::transfer(temp_frame2->get_rows(), /* Leave NULL if non existent */
665 temp_frame2->get_y(), /* Leave NULL if non existent */
666 temp_frame2->get_u(),
667 temp_frame2->get_v(),
668 input->get_y(), /* Leave NULL if non existent */
671 0, /* Dimensions to capture from input frame */
673 MIN(temp_frame2->get_w(), input->get_w()),
674 MIN(temp_frame2->get_h(), input->get_h()),
675 0, /* Dimensions to project on output frame */
677 MIN(temp_frame2->get_w(), input->get_w()),
678 MIN(temp_frame2->get_h(), input->get_h()),
679 input->get_color_model(),
681 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
682 input->get_bytes_per_line(), /* For planar use the luma rowspan */
683 temp_frame2->get_bytes_per_line()); /* For planar use the luma rowspan */
685 dv_write_video(encoder,
686 temp_frame->get_data(),
687 temp_frame2->get_rows(),
709 // Take over buffer table
710 buffer_lock->lock("Device1394Output::write_frame 1");
712 // Wait for buffer to become available with timeout
713 while(buffer_valid[current_inbuffer] && !result && !interrupted)
715 buffer_lock->unlock();
716 result = video_lock->timed_lock(BUFFER_TIMEOUT);
717 buffer_lock->lock("Device1394Output::write_frame 2");
722 // Write buffer if there's room
723 if(!buffer_valid[current_inbuffer])
725 if(!buffer[current_inbuffer])
727 buffer[current_inbuffer] = new char[ptr->get_compressed_size()];
728 buffer_size[current_inbuffer] = ptr->get_compressed_size();
730 memcpy(buffer[current_inbuffer], ptr->get_data(), ptr->get_compressed_size());
731 buffer_valid[current_inbuffer] = 1;
732 increment_counter(¤t_inbuffer);
735 // Ignore it if there isn't room.
740 buffer_lock->unlock();
741 start_lock->unlock();
742 //printf("Device1394Output::write_frame 100\n");
745 void Device1394Output::write_samples(char *data, int samples)
747 //printf("Device1394Output::write_samples 1\n");
749 //int timeout = (samples * 1000000LL * 2)/ samplerate;
750 if(interrupted) return;
752 //printf("Device1394Output::write_samples 2\n");
754 // Check for maximum sample count exceeded
755 if(samples > OUTPUT_SAMPLES)
757 printf("Device1394Output::write_samples samples=%d > OUTPUT_SAMPLES=%d\n",
763 //printf("Device1394Output::write_samples 3\n");
764 // Take over buffer table
765 buffer_lock->lock("Device1394Output::write_samples 1");
766 // Wait for buffer to become available with timeout
767 while(audio_samples > OUTPUT_SAMPLES - samples && !result && !interrupted)
769 buffer_lock->unlock();
770 result = audio_lock->timed_lock(BUFFER_TIMEOUT);
771 buffer_lock->lock("Device1394Output::write_samples 2");
774 if(!interrupted && audio_samples <= OUTPUT_SAMPLES - samples)
776 //printf("Device1394Output::write_samples 4 %d\n", audio_samples);
777 memcpy(audio_buffer + audio_samples * channels * bits / 8,
779 samples * channels * bits / 8);
780 audio_samples += samples;
782 buffer_lock->unlock();
783 start_lock->unlock();
784 //printf("Device1394Output::write_samples 100\n");
787 long Device1394Output::get_audio_position()
789 position_lock->lock("Device1394Output::get_audio_position");
790 long result = audio_position;
793 // Take delay between placing in buffer and presentation
794 // on device into account for dv1394
795 result = position_presented[status.active_frame];
797 position_lock->unlock();
801 void Device1394Output::interrupt()
804 // Break write_samples out of a lock
805 video_lock->unlock();
806 audio_lock->unlock();
807 // Playback should stop when the object is deleted.
810 void Device1394Output::flush()
815 void Device1394Output::increment_counter(int *counter)
818 if(*counter >= total_buffers) *counter = 0;
821 void Device1394Output::decrement_counter(int *counter)
824 if(*counter < 0) *counter = total_buffers - 1;
827 void Device1394Output::set_ioctls()
829 // It would make sense to simply change the file that is included in
830 // order to change the IOCTLs, but it isn't reasonable to think that
831 // people will rebuild their software every time the update their
832 // kernel, hence this fix.
836 // Get the kernel version so we can set the right ioctls
839 char *ptr = strchr(buf.release, '.');
846 major = atoi(buf.release);
847 char *ptr2 = strchr(ptr, '.');
856 if( (major >= 2 && minor >= 6 && point >= 0) ||
857 (major >= 2 && minor >= 4 && point >= 23) )
860 video1394_listen_channel = VIDEO1394_IOC_LISTEN_CHANNEL;
861 video1394_unlisten_channel = VIDEO1394_IOC_UNLISTEN_CHANNEL;
862 video1394_listen_queue_buffer = VIDEO1394_IOC_LISTEN_QUEUE_BUFFER;
863 video1394_listen_wait_buffer = VIDEO1394_IOC_LISTEN_WAIT_BUFFER;
864 video1394_talk_channel = VIDEO1394_IOC_TALK_CHANNEL;
865 video1394_untalk_channel = VIDEO1394_IOC_UNTALK_CHANNEL;
866 video1394_talk_queue_buffer = VIDEO1394_IOC_TALK_QUEUE_BUFFER;
867 video1394_talk_wait_buffer = VIDEO1394_IOC_TALK_WAIT_BUFFER;
868 video1394_listen_poll_buffer = VIDEO1394_IOC_LISTEN_POLL_BUFFER;
871 // Nothing uses this right now, so I didn't include it.
873 else // we are using an older kernel
876 video1394_listen_channel = VIDEO1394_LISTEN_CHANNEL;
877 video1394_unlisten_channel = VIDEO1394_UNLISTEN_CHANNEL;
878 video1394_listen_queue_buffer = VIDEO1394_LISTEN_QUEUE_BUFFER;
879 video1394_listen_wait_buffer = VIDEO1394_LISTEN_WAIT_BUFFER;
880 video1394_talk_channel = VIDEO1394_TALK_CHANNEL;
881 video1394_untalk_channel = VIDEO1394_UNTALK_CHANNEL;
882 video1394_talk_queue_buffer = VIDEO1394_TALK_QUEUE_BUFFER;
883 video1394_talk_wait_buffer = VIDEO1394_TALK_WAIT_BUFFER;
884 video1394_listen_poll_buffer = VIDEO1394_LISTEN_POLL_BUFFER;
899 #endif // HAVE_FIREWIRE