7 #include "device1394output.h"
15 #include <sys/ioctl.h>
21 #include "avc1394_vcr.h"
28 #define CIP_N_NTSC 2436
29 #define CIP_D_NTSC 38400
32 #define OUTPUT_SAMPLES 262144
33 #define BUFFER_TIMEOUT 500000
36 Device1394Output::Device1394Output()
43 current_outbuffer = 0;
63 Device1394Output::~Device1394Output()
75 for(int i = 0; i < total_buffers; i++)
77 if(buffer[i]) delete [] buffer[i];
80 delete [] buffer_size;
81 delete [] buffer_valid;
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;
91 output_queue.buffer = (output_mmap.nb_buffers + output_queue.buffer - 1) % output_mmap.nb_buffers;
95 if(ioctl(output_fd, DV1394_WAIT_FRAMES, output_mmap.nb_buffers - 1) < 0)
98 "Device1394::close_all: DV1394_WAIT_FRAMES %i: %s",
99 output_mmap.nb_buffers,
102 munmap(output_buffer, status.init.n_frames * DV1394_NTSC_FRAME_SIZE);
106 if(ioctl(output_fd, VIDEO1394_TALK_WAIT_BUFFER, &output_queue) < 0)
109 "Device1394::close_all: VIDEO1394_TALK_WAIT_BUFFER %s: %s",
113 munmap(output_buffer, output_mmap.nb_buffers * output_mmap.buf_size);
118 if(ioctl(output_fd, DV1394_SHUTDOWN, NULL) < 0)
120 perror("Device1394::close_all: DV1394_SHUTDOWN");
125 if(ioctl(output_fd, VIDEO1394_UNTALK_CHANNEL, &output_mmap.channel) < 0)
127 perror("Device1394::close_all: VIDEO1394_UNTALK_CHANNEL");
133 // raw1394_destroy_handle(avc_handle);
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,
157 this->channels = channels;
159 this->samplerate = samplerate;
160 this->total_buffers = length;
162 this->use_dv1394 = use_dv1394;
166 if(strstr(path, "PAL") != NULL || minor(buf.st_rdev) == 34)
171 static struct dv1394_init setup = {
172 api_version: DV1394_API_VERSION,
175 format: is_pal ? DV1394_PAL : DV1394_NTSC,
182 //printf("Device1394::open_output 2 %s %d %d %d %d\n", path, port, channel, length, syt);
185 output_fd = ::open(path, O_RDWR);
190 "Device1394Output::open path=%s: %s",
197 // avc_handle = raw1394_new_handle();
198 // if(avc_handle == 0)
199 // printf("Device1394::open_output avc_handle == 0\n");
201 // struct raw1394_portinfo pinf[16];
202 // int numcards = raw1394_get_port_info(avc_handle, pinf, 16);
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");
208 // int nodecount = raw1394_get_nodecount(avc_handle);
209 // int itemcount = 0;
210 // rom1394_directory rom1394_dir;
213 // for(int currentnode = 0; currentnode < nodecount; currentnode++)
215 // rom1394_get_directory(avc_handle, currentnode, &rom1394_dir);
217 // if((rom1394_get_node_type(&rom1394_dir) == ROM1394_NODE_TYPE_AVC) &&
218 // avc1394_check_subunit_type(avc_handle, currentnode, AVC1394_SUBUNIT_TYPE_VCR))
221 // // default the phyID to the first AVC node found
222 // if(itemcount == 1) avc_id = currentnode;
226 // rom1394_free_directory(&rom1394_dir);
230 // printf("Device1394::open_output no avc_id found.\n");
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);
253 if(ioctl(output_fd, DV1394_INIT, &setup) < 0)
255 perror("Device1394Output::open DV1394_INIT:");
257 if(ioctl(output_fd, DV1394_GET_STATUS, &setup) < 0)
259 perror("Device1394Output::open DV1394_GET_STATUS:");
264 if(ioctl(output_fd, VIDEO1394_TALK_CHANNEL, &output_mmap) < 0)
266 perror("Device1394Output::open VIDEO1394_TALK_CHANNEL:");
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,
282 output_buffer = (unsigned char*)mmap(0,
283 output_mmap.nb_buffers * output_mmap.buf_size,
284 PROT_READ | PROT_WRITE,
289 if(output_buffer <= 0)
291 perror("Device1394Output::open mmap");
294 unused_buffers = output_mmap.nb_buffers;
295 output_queue.buffer = 0;
296 output_queue.packet_sizes = packet_sizes;
297 continuity_counter = 0;
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;
315 audio_buffer = new char[OUTPUT_SAMPLES * channels * bits / 8];
322 void Device1394Output::run()
324 unsigned char *output;
328 Thread::enable_cancel();
330 Thread::disable_cancel();
333 // Write buffers continuously
336 // Get current buffer to play
340 //printf("Device1394Output::run 1\n");
342 //printf("Device1394Output::run 10\n");
346 out_buffer = buffer[status.first_clear_frame];
347 out_size = buffer_size[status.first_clear_frame];
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;
368 output = output_buffer +
370 status.first_clear_frame;
374 output = output_buffer +
375 output_queue.buffer *
376 output_mmap.buf_size;
379 int is_pal = (out_size == DV_PAL_SIZE);
386 if(out_buffer && out_size)
388 // Calculate number of samples needed based on given pattern for
390 int samples_per_frame = 2048;
393 if(audio_samples > samples_per_frame)
396 int samples_written = dv_write_audio(encoder,
397 (unsigned char*)out_buffer,
398 (unsigned char*)audio_buffer,
403 is_pal ? DV_PAL : DV_NTSC);
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();
415 // Copy from current buffer to mmap buffer with firewire encryption
418 memcpy(output, out_buffer, out_size);
422 encrypt((unsigned char*)output,
423 (unsigned char*)out_buffer,
426 buffer_valid[current_outbuffer] = 0;
428 // Advance buffer number if possible
429 increment_counter(¤t_outbuffer);
431 // Reuse same buffer next time
432 if(!buffer_valid[current_outbuffer])
434 decrement_counter(¤t_outbuffer);
437 // Wait for user to reach current buffer before unlocking any more.
439 video_lock->unlock();
442 buffer_lock->unlock();
443 //printf("Device1394Output::run 100\n");
447 // Write mmap to device
448 Thread::enable_cancel();
452 if(ioctl(output_fd, DV1394_SUBMIT_FRAMES, 1) < 0)
454 perror("Device1394Output::run DV1394_SUBMIT_FRAMES:");
456 if(ioctl(output_fd, DV1394_GET_STATUS, &status) < 0)
458 perror("Device1394Output::run DV1394_GET_STATUS:");
463 if(ioctl(output_fd, VIDEO1394_TALK_QUEUE_BUFFER, &output_queue) < 0)
465 perror("Device1394Output::run VIDEO1394_TALK_QUEUE_BUFFER");
468 output_queue.buffer++;
469 if(output_queue.buffer >= output_mmap.nb_buffers)
470 output_queue.buffer = 0;
472 if(unused_buffers <= 0)
476 if(ioctl(output_fd, DV1394_WAIT_FRAMES, 1) < 0)
478 perror("Device1394Output::run DV1394_WAIT_FRAMES");
483 if(ioctl(output_fd, VIDEO1394_TALK_WAIT_BUFFER, &output_queue) < 0)
485 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 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;
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)
585 int is_pal = (input->get_h() == 576);
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)
596 if(!temp_frame) temp_frame = new VFrame;
597 if(!encoder) encoder = dv_new();
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))
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(),
619 // Convert resolution and color model before compressing
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,
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 */
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 */
648 0, /* Dimensions to capture from input frame */
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 */
654 MIN(temp_frame2->get_w(), input->get_w()),
655 MIN(temp_frame2->get_h(), input->get_h()),
656 input->get_color_model(),
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(),
686 // Take over buffer table
689 // Wait for buffer to become available with timeout
690 while(buffer_valid[current_inbuffer] && !result && !interrupted)
692 buffer_lock->unlock();
693 result = video_lock->timed_lock(BUFFER_TIMEOUT);
699 // Write buffer if there's room
700 if(!buffer_valid[current_inbuffer])
702 if(!buffer[current_inbuffer])
704 buffer[current_inbuffer] = new char[ptr->get_compressed_size()];
705 buffer_size[current_inbuffer] = ptr->get_compressed_size();
707 memcpy(buffer[current_inbuffer], ptr->get_data(), ptr->get_compressed_size());
708 buffer_valid[current_inbuffer] = 1;
709 increment_counter(¤t_inbuffer);
712 // Ignore it if there isn't room.
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");
726 int timeout = (int64_t)samples *
730 if(interrupted) return;
732 //printf("Device1394Output::write_samples 2\n");
734 // Check for maximum sample count exceeded
735 if(samples > OUTPUT_SAMPLES)
737 printf("Device1394Output::write_samples samples=%d > OUTPUT_SAMPLES=%d\n",
743 //printf("Device1394Output::write_samples 3\n");
744 // Take over buffer table
746 // Wait for buffer to become available with timeout
747 while(audio_samples > OUTPUT_SAMPLES - samples && !result && !interrupted)
749 buffer_lock->unlock();
750 result = audio_lock->timed_lock(BUFFER_TIMEOUT);
754 if(!interrupted && audio_samples <= OUTPUT_SAMPLES - samples)
756 //printf("Device1394Output::write_samples 4 %d\n", audio_samples);
757 memcpy(audio_buffer + audio_samples * channels * bits / 8,
759 samples * channels * bits / 8);
760 audio_samples += samples;
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();
775 void Device1394Output::interrupt()
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()
789 void Device1394Output::increment_counter(int *counter)
792 if(*counter >= total_buffers) *counter = 0;
795 void Device1394Output::decrement_counter(int *counter)
798 if(*counter < 0) *counter = total_buffers - 1;
814 #endif // HAVE_FIREWIRE