*** ieee1394io.cc.original	Wed Aug  8 18:22:06 2001
--- ieee1394io.cc	Mon Jul 22 20:22:15 2002
***************
*** 29,34 ****
--- 29,37 ----
  #include <stdlib.h>
  #include <signal.h>
  #include <pthread.h>
+ #include <iostream>
+ #include <string>
+ #include <vector>
  
  #include <libraw1394/raw1394.h>
  #include <libraw1394/csr.h>
***************
*** 150,166 ****
  }
  
  
- int raw_iso_handler(raw1394handle_t handle, int channel, size_t length,
-                     quadlet_t *data)
- {
-         if (length < RAW_BUF_SIZE) {
-                 *(int*)g_frame = length;
-                 memcpy(g_frame + 4, data, length);
-         }
-         return 0;
- }
- 
- 
  int my_reset_handler(raw1394handle_t handle, unsigned int generation)
  {
          static int i = 0;
--- 153,158 ----
***************
*** 315,320 ****
--- 307,321 ----
          extern void real_fail_null(void *p, char *file, int line);
  }
  
+ int test_iso_handler(raw1394handle_t handle, int channel, size_t length,
+ 		                    quadlet_t *data)
+ {
+         if (length < RAW_BUF_SIZE) {
+                 *(int*)g_frame = length;
+                 memcpy(g_frame + 4, data, length);
+ 	        }
+         return 0;
+ }
  
  int capture_test()
  {
***************
*** 331,337 ****
  
          fail_null(g_frame = (unsigned char*)malloc(RAW_BUF_SIZE));
  
!         handle = open_1394_driver(g_channel, raw_iso_handler);
  
          frames_read = 0;
          while ((!g_alldone) && (frames_read < g_frame_count)) {
--- 332,338 ----
  
          fail_null(g_frame = (unsigned char*)malloc(RAW_BUF_SIZE));
  
!         handle = open_1394_driver(g_channel, test_iso_handler);
  
          frames_read = 0;
          while ((!g_alldone) && (frames_read < g_frame_count)) {
***************
*** 350,408 ****
  
  /* capture in a format suitable for playdv (part of libdv) */
  /* courtesy Alastair Mayer [alastair@ajwm.net] */
  
! int capture_raw()
  {
-         int frames_read;
-         int length;
-         int dst_fd;
-         raw1394handle_t handle;
-         bool found_first_frame;
-         int skipped = 0;
- //        unsigned char *g_frame;
- 
-         char filename[256];
-         sprintf(filename, "%s.dv", g_dst_file_name);
-         if ((dst_fd = open(filename, O_WRONLY | O_CREAT, 00644)) < 0) {
-                 perror("problem with output file");
-                 usage();
-                 exit(1);
-         }
  
!         fail_null(g_frame = (unsigned char*)malloc(RAW_BUF_SIZE));
  
!         handle = open_1394_driver(g_channel, raw_iso_handler);
  
-         frames_read = 0;
-         found_first_frame = false;
  
!         while (!g_alldone) {
!                 raw1394_loop_iterate(handle);
!                 length = *(int*)g_frame;
!                 if (length >= 492) {
!                         if (!found_first_frame) {
!                                 if (g_frame[16] == 0x1f &&
!                                                 g_frame[17] == 0x07)
!                                         found_first_frame = true;
!                                 else skipped++;
!                         }
!                         if (skipped > 500) {
!                                 printf("skipped too many without finding frame\n");
!                                 break;
!                         }
!                         if (found_first_frame) {
!                                 if (g_frame[16] == 0x1f && g_frame[17] == 0x07)
!                                         frames_read++;
!                                 if (frames_read > g_frame_count)
  					break;
! 				write(dst_fd, (g_frame + 16), 480);
!                         }
!                 }
!         }
          close_1394_driver(g_channel, handle);
!         free(g_frame);
!         close(dst_fd);
  
          return 0;
  }
  
--- 351,441 ----
  
  /* capture in a format suitable for playdv (part of libdv) */
  /* courtesy Alastair Mayer [alastair@ajwm.net] */
+ /* Updated to use the same start-of-frame detection algorithm as avi_iso_handler by Tim Shead [tshead@k-3d.com] */
  
! namespace
  {
  
! // Note: storage in quadlets instead of bytes
! std::vector<quadlet_t> g_raw_packet;
! 	
! int raw_iso_handler(raw1394handle_t handle, int channel, size_t length, quadlet_t *data)
! {
! 	// Make sure the buffer is large enough (note: length is in bytes, buffer is in quadlets) 
! 	g_raw_packet.resize(length / 4);
! 	memcpy(&g_raw_packet[0], data, length);
!         
! 	return 0;
! }
  
! } // namespace
  
  
! int capture_raw()
! {
! 	const std::string output_path = std::string(g_dst_file_name) + ".dv";
! 
!         const int destination_file = open(output_path.c_str(), O_WRONLY | O_CREAT, 00644);
! 	if(destination_file < 0)
! 		{
! 			perror("problem with output file");
! 			usage();
! 			exit(1);
!         	}
! 
! 	// Make the buffer as big as it should ever need to be ...
! 	g_raw_packet.reserve(RAW_BUF_SIZE / 4);
! 
! 	const raw1394handle_t handle = open_1394_driver(g_channel, raw_iso_handler);
! 
! 	const unsigned long max_packets_skipped = 500;	
! 	unsigned long packets_skipped = 0;
!         unsigned long frames_read = 0;
!         bool found_first_frame = false;
! 
!         while(!g_alldone)
! 		{
! 			raw1394_loop_iterate(handle);
! 
! 			if(g_raw_packet.size() != 123)
! 				continue;
! 
! 			const unsigned char* const p = reinterpret_cast<unsigned char*>(&g_raw_packet[3]);
! 			const int section_type = p[0] >> 5;
! 			const int dif_sequence = p[1] >> 4;
! 
! 			if(!found_first_frame)
! 				{
! 
! 					if(section_type == 0 && dif_sequence == 0)
! 						found_first_frame = true;
! 					else
! 						++packets_skipped;
! 				}
! 			
! 			if(packets_skipped > max_packets_skipped)
! 				{
! 					std::cerr << "skipped " << packets_skipped << " packets without finding start-of-frame" << std::endl;
  					break;
! 				}
! 			
! 			if(found_first_frame)
! 				{
! 					if(section_type == 0 && dif_sequence == 0)
! 						++frames_read;
! 					
! 					if(frames_read > static_cast<unsigned long>(g_frame_count))
! 						break;
! 
! 					write(destination_file, &g_raw_packet[3], 480);
! 				}
!         	}
! 
! 	g_raw_packet.resize(0);
          close_1394_driver(g_channel, handle);
!         close(destination_file);
  
          return 0;
  }
  
+ 
