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 if(position_presented) delete [] position_presented;
255 position_presented = new long[length];
256 for (int i = 0; i < length; i++)
257 position_presented[i] = 0;
261 if(ioctl(output_fd, video1394_talk_channel, &output_mmap) < 0)
263 perror("Device1394Output::open VIDEO1394_TALK_CHANNEL:");
266 output_buffer = (unsigned char*)mmap(0,
267 output_mmap.nb_buffers * output_mmap.buf_size,
268 PROT_READ | PROT_WRITE,
274 if(output_buffer <= 0)
276 perror("Device1394Output::open mmap");
279 unused_buffers = output_mmap.nb_buffers;
280 output_queue.buffer = 0;
281 output_queue.packet_sizes = packet_sizes;
282 continuity_counter = 0;
286 buffer = new char*[total_buffers];
287 for(int i = 0; i < length; i++)
288 buffer[i] = new char[get_dv1394() ?
289 (is_pal ? DV1394_PAL_FRAME_SIZE : DV1394_NTSC_FRAME_SIZE) :
291 buffer_size = new int[total_buffers];
292 buffer_valid = new int[total_buffers];
293 bzero(buffer_size, sizeof(int) * total_buffers);
294 bzero(buffer_valid, sizeof(int) * total_buffers);
295 bzero(buffer, sizeof(char*) * total_buffers);
296 video_lock = new Condition(0, "Device1394Output::video_lock");
297 audio_lock = new Condition(0, "Device1394Output::audio_lock");
298 start_lock = new Condition(0, "Device1394Output::start_lock");
299 buffer_lock = new Mutex("Device1394Output::buffer_lock");
300 position_lock = new Mutex("Device1394Output::position_lock");
302 audio_buffer = new char[OUTPUT_SAMPLES * channels * bits / 8];
309 void Device1394Output::run()
311 unsigned char *output;
315 Thread::enable_cancel();
316 start_lock->lock("Device1394Output::run");
317 Thread::disable_cancel();
320 // Write buffers continuously
323 // Get current buffer to play
326 buffer_lock->lock("Device1394Output::run 1");
328 out_buffer = buffer[current_outbuffer];
329 out_size = buffer_size[current_outbuffer];
335 // No video. Put in a fake frame for audio only
338 #include "data/fake_ntsc_dv.h"
339 out_size = sizeof(fake_ntsc_dv) - 4;
340 out_buffer = (char*)fake_ntsc_dv + 4;
348 output = output_buffer +
350 status.first_clear_frame;
354 output = output_buffer +
355 output_queue.buffer *
356 output_mmap.buf_size;
367 if(out_buffer && out_size)
369 // Calculate number of samples needed based on given pattern for
371 int samples_per_frame = 2048;
374 if(audio_samples > samples_per_frame)
377 int samples_written = dv_write_audio(encoder,
378 (unsigned char*)out_buffer,
379 (unsigned char*)audio_buffer,
384 is_pal ? DV_PAL : DV_NTSC);
386 audio_buffer + samples_written * bits * channels / 8,
387 (audio_samples - samples_written) * bits * channels / 8);
388 audio_samples -= samples_written;
389 position_lock->lock("Device1394Output::run");
393 // When this frame is being uploaded to the 1394 device,
394 // the frame actually playing on the device will be the one
395 // uploaded syt frames before.
396 position_presented[status.first_clear_frame] =
397 audio_position - syt * samples_per_frame;
398 if (position_presented[status.first_clear_frame] < 0)
399 position_presented[status.first_clear_frame] = 0;
402 audio_position += samples_written;
403 position_lock->unlock();
406 audio_lock->unlock();
409 // Copy from current buffer to mmap buffer with firewire encryption
412 memcpy(output, out_buffer, out_size);
416 encrypt((unsigned char*)output,
417 (unsigned char*)out_buffer,
420 buffer_valid[current_outbuffer] = 0;
423 // Advance buffer number if possible
424 increment_counter(¤t_outbuffer);
426 // Reuse same buffer next time
427 if(!buffer_valid[current_outbuffer])
429 decrement_counter(¤t_outbuffer);
432 // Wait for user to reach current buffer before unlocking any more.
434 video_lock->unlock();
438 buffer_lock->unlock();
439 //printf("Device1394Output::run 100\n");
446 // Write mmap to device
447 Thread::enable_cancel();
453 if(ioctl(output_fd, DV1394_IOC_SUBMIT_FRAMES, 1) < 0)
455 perror("Device1394Output::run DV1394_SUBMIT_FRAMES");
457 if(ioctl(output_fd, DV1394_IOC_WAIT_FRAMES, 1) < 0)
459 perror("Device1394Output::run DV1394_WAIT_FRAMES");
461 if(ioctl(output_fd, DV1394_IOC_GET_STATUS, &status) < 0)
463 perror("Device1394Output::run DV1394_GET_STATUS");
468 if(ioctl(output_fd, video1394_talk_queue_buffer, &output_queue) < 0)
470 perror("Device1394Output::run VIDEO1394_TALK_QUEUE_BUFFER");
474 output_queue.buffer++;
475 if(output_queue.buffer >= output_mmap.nb_buffers)
476 output_queue.buffer = 0;
478 if(unused_buffers <= 0)
482 if(ioctl(output_fd, video1394_talk_wait_buffer, &output_queue) < 0)
484 perror("Device1394::run VIDEO1394_TALK_WAIT_BUFFER");
491 Thread::disable_cancel();
495 // Thread::enable_cancel();
496 // start_lock->lock();
497 // Thread::disable_cancel();
500 //printf("Device1394Output::run %lld\n", timer.get_difference());
506 void Device1394Output::encrypt(unsigned char *output,
510 // Encode in IEEE1394 video encryption
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;
538 for(int i = 0; i < output_size && vdata < frame_size; i++)
540 unsigned char *p = output;
541 int want_sync = (cip_counter > cip_d);
543 /* Source node ID ! */
545 /* Packet size in quadlets (480 / 4) - this stays the same even for empty packets */
548 *p++ = continuity_counter;
552 /* high bit = 50/60 indicator */
555 /* timestamp - generated in driver */
563 continuity_counter++;
564 cip_counter += cip_n;
566 memcpy(p, data + vdata, 480);
571 cip_counter -= cip_d;
573 *packet_sizes++ = p - output;
574 output += min_packet_size;
582 void Device1394Output::write_frame(VFrame *input)
587 //printf("Device1394Output::write_frame 1\n");
589 if(output_fd <= 0) return;
590 if(interrupted) return;
592 // Encode frame to DV
593 if(input->get_color_model() != BC_COMPRESSED)
595 if(!temp_frame) temp_frame = new VFrame;
596 if(!encoder) encoder = dv_new();
599 // Exact resolution match. Don't do colorspace conversion
600 if(input->get_color_model() == BC_YUV422 &&
601 input->get_w() == 720 &&
602 (input->get_h() == 480 ||
603 input->get_h() == 576))
605 int norm = is_pal ? DV_PAL : DV_NTSC;
606 int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
607 temp_frame->allocate_compressed_data(data_size);
608 temp_frame->set_compressed_size(data_size);
610 dv_write_video(encoder,
611 temp_frame->get_data(),
618 // Convert resolution and color model before compressing
622 int h = input->get_h();
623 // Default to NTSC if unknown
624 if(h != 480 && h != 576) h = 480;
626 temp_frame2 = new VFrame(0,
633 int norm = is_pal ? DV_PAL : DV_NTSC;
634 int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
635 temp_frame->allocate_compressed_data(data_size);
636 temp_frame->set_compressed_size(data_size);
639 cmodel_transfer(temp_frame2->get_rows(), /* Leave NULL if non existent */
641 temp_frame2->get_y(), /* Leave NULL if non existent */
642 temp_frame2->get_u(),
643 temp_frame2->get_v(),
644 input->get_y(), /* Leave NULL if non existent */
647 0, /* Dimensions to capture from input frame */
649 MIN(temp_frame2->get_w(), input->get_w()),
650 MIN(temp_frame2->get_h(), input->get_h()),
651 0, /* Dimensions to project on output frame */
653 MIN(temp_frame2->get_w(), input->get_w()),
654 MIN(temp_frame2->get_h(), input->get_h()),
655 input->get_color_model(),
657 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
658 input->get_bytes_per_line(), /* For planar use the luma rowspan */
659 temp_frame2->get_bytes_per_line()); /* For planar use the luma rowspan */
661 dv_write_video(encoder,
662 temp_frame->get_data(),
663 temp_frame2->get_rows(),
685 // Take over buffer table
686 buffer_lock->lock("Device1394Output::write_frame 1");
688 // Wait for buffer to become available with timeout
689 while(buffer_valid[current_inbuffer] && !result && !interrupted)
691 buffer_lock->unlock();
692 result = video_lock->timed_lock(BUFFER_TIMEOUT);
693 buffer_lock->lock("Device1394Output::write_frame 2");
698 // Write buffer if there's room
699 if(!buffer_valid[current_inbuffer])
701 if(!buffer[current_inbuffer])
703 buffer[current_inbuffer] = new char[ptr->get_compressed_size()];
704 buffer_size[current_inbuffer] = ptr->get_compressed_size();
706 memcpy(buffer[current_inbuffer], ptr->get_data(), ptr->get_compressed_size());
707 buffer_valid[current_inbuffer] = 1;
708 increment_counter(¤t_inbuffer);
711 // Ignore it if there isn't room.
716 buffer_lock->unlock();
717 start_lock->unlock();
718 //printf("Device1394Output::write_frame 100\n");
721 void Device1394Output::write_samples(char *data, int samples)
723 //printf("Device1394Output::write_samples 1\n");
725 int timeout = (int64_t)samples *
729 if(interrupted) return;
731 //printf("Device1394Output::write_samples 2\n");
733 // Check for maximum sample count exceeded
734 if(samples > OUTPUT_SAMPLES)
736 printf("Device1394Output::write_samples samples=%d > OUTPUT_SAMPLES=%d\n",
742 //printf("Device1394Output::write_samples 3\n");
743 // Take over buffer table
744 buffer_lock->lock("Device1394Output::write_samples 1");
745 // Wait for buffer to become available with timeout
746 while(audio_samples > OUTPUT_SAMPLES - samples && !result && !interrupted)
748 buffer_lock->unlock();
749 result = audio_lock->timed_lock(BUFFER_TIMEOUT);
750 buffer_lock->lock("Device1394Output::write_samples 2");
753 if(!interrupted && audio_samples <= OUTPUT_SAMPLES - samples)
755 //printf("Device1394Output::write_samples 4 %d\n", audio_samples);
756 memcpy(audio_buffer + audio_samples * channels * bits / 8,
758 samples * channels * bits / 8);
759 audio_samples += samples;
761 buffer_lock->unlock();
762 start_lock->unlock();
763 //printf("Device1394Output::write_samples 100\n");
766 long Device1394Output::get_audio_position()
768 position_lock->lock("Device1394Output::get_audio_position");
769 long result = audio_position;
772 // Take delay between placing in buffer and presentation
773 // on device into account for dv1394
774 result = position_presented[status.active_frame];
776 position_lock->unlock();
780 void Device1394Output::interrupt()
783 // Break write_samples out of a lock
784 video_lock->unlock();
785 audio_lock->unlock();
786 // Playback should stop when the object is deleted.
789 void Device1394Output::flush()
794 void Device1394Output::increment_counter(int *counter)
797 if(*counter >= total_buffers) *counter = 0;
800 void Device1394Output::decrement_counter(int *counter)
803 if(*counter < 0) *counter = total_buffers - 1;
806 void Device1394Output::set_ioctls()
808 // It would make sense to simply change the file that is included in
809 // order to change the IOCTLs, but it isn't reasonable to think that
810 // people will rebuild their software every time the update their
811 // kernel, hence this fix.
815 // Get the kernel version so we can set the right ioctls
818 char *ptr = strchr(buf.release, '.');
825 major = atoi(buf.release);
826 char *ptr2 = strchr(ptr, '.');
835 if(major >= 2 && minor >= 6 && point >= 0 ||
836 major >= 2 && minor >= 4 && point >= 23)
839 video1394_listen_channel = VIDEO1394_IOC_LISTEN_CHANNEL;
840 video1394_unlisten_channel = VIDEO1394_IOC_UNLISTEN_CHANNEL;
841 video1394_listen_queue_buffer = VIDEO1394_IOC_LISTEN_QUEUE_BUFFER;
842 video1394_listen_wait_buffer = VIDEO1394_IOC_LISTEN_WAIT_BUFFER;
843 video1394_talk_channel = VIDEO1394_IOC_TALK_CHANNEL;
844 video1394_untalk_channel = VIDEO1394_IOC_UNTALK_CHANNEL;
845 video1394_talk_queue_buffer = VIDEO1394_IOC_TALK_QUEUE_BUFFER;
846 video1394_talk_wait_buffer = VIDEO1394_IOC_TALK_WAIT_BUFFER;
847 video1394_listen_poll_buffer = VIDEO1394_IOC_LISTEN_POLL_BUFFER;
850 // Nothing uses this right now, so I didn't include it.
852 else // we are using an older kernel
855 video1394_listen_channel = VIDEO1394_LISTEN_CHANNEL;
856 video1394_unlisten_channel = VIDEO1394_UNLISTEN_CHANNEL;
857 video1394_listen_queue_buffer = VIDEO1394_LISTEN_QUEUE_BUFFER;
858 video1394_listen_wait_buffer = VIDEO1394_LISTEN_WAIT_BUFFER;
859 video1394_talk_channel = VIDEO1394_TALK_CHANNEL;
860 video1394_untalk_channel = VIDEO1394_UNTALK_CHANNEL;
861 video1394_talk_queue_buffer = VIDEO1394_TALK_QUEUE_BUFFER;
862 video1394_talk_wait_buffer = VIDEO1394_TALK_WAIT_BUFFER;
863 video1394_listen_poll_buffer = VIDEO1394_LISTEN_POLL_BUFFER;
878 #endif // HAVE_FIREWIRE