Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.git] / cinelerra-5.1 / cinelerra / vdevice1394.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "assets.h"
23 #include "audio1394.h"
24 #include "audioconfig.h"
25 #include "audiodevice.h"
26 #include "device1394input.h"
27 #include "device1394output.h"
28 #include "iec61883input.h"
29 #include "iec61883output.h"
30 #include "file.inc"
31 #include "preferences.h"
32 #include "recordconfig.h"
33 #include "vdevice1394.h"
34 #include "vframe.h"
35 #include "playbackconfig.h"
36 #include "videodevice.h"
37
38
39
40
41
42
43
44
45
46
47 VDevice1394::VDevice1394(VideoDevice *device)
48  : VDeviceBase(device)
49 {
50         initialize();
51 }
52
53 VDevice1394::~VDevice1394()
54 {
55         close_all();
56 }
57
58 void VDevice1394::initialize()
59 {
60         input_thread = 0;
61         output_thread = 0;
62         input_iec = 0;
63         output_iec = 0;
64         user_frame = 0;
65 }
66
67 int VDevice1394::open_input()
68 {
69 // Share audio driver.  The audio driver does the capturing in this case
70 // and fills video frames for us.
71         int result = 0;
72         if(device->adevice &&
73                 (device->adevice->in_config->driver == AUDIO_1394 ||
74                         device->adevice->in_config->driver == AUDIO_DV1394 ||
75                         device->adevice->in_config->driver == AUDIO_IEC61883))
76         {
77                 Audio1394 *low_level = (Audio1394*)device->adevice->lowlevel_in;
78                 input_thread = low_level->input_thread;
79                 input_iec = low_level->input_iec;
80                 device->sharing = 1;
81         }
82         else
83         if(!input_thread && !input_iec)
84         {
85                 if(device->in_config->driver == CAPTURE_FIREWIRE)
86                 {
87                         input_thread = new Device1394Input;
88                         result = input_thread->open(device->in_config->firewire_path,
89                                 device->in_config->firewire_port,
90                                 device->in_config->firewire_channel,
91                                 device->in_config->capture_length,
92                                 2,
93                                 48000,
94                                 16,
95                                 device->in_config->w,
96                                 device->in_config->h);
97                         if(result)
98                         {
99                                 delete input_thread;
100                                 input_thread = 0;
101                         }
102                 }
103                 else
104                 {
105                         input_iec = new IEC61883Input;
106                         result = input_iec->open(device->in_config->firewire_port,
107                                 device->in_config->firewire_channel,
108                                 device->in_config->capture_length,
109                                 2,
110                                 48000,
111                                 16,
112                                 device->in_config->w,
113                                 device->in_config->h);
114                         if(result)
115                         {
116                                 delete input_iec;
117                                 input_iec = 0;
118                         }
119                 }
120         }
121         return result;
122 }
123
124 int VDevice1394::open_output()
125 {
126 // Share audio driver.  The audio driver takes DV frames from us and
127 // inserts audio.
128         if(device->adevice &&
129                 (device->adevice->out_config->driver == AUDIO_1394 ||
130                         device->adevice->out_config->driver == AUDIO_DV1394 ||
131                         device->adevice->out_config->driver == AUDIO_IEC61883))
132         {
133                 Audio1394 *low_level = (Audio1394*)device->adevice->lowlevel_out;
134                 output_thread = low_level->output_thread;
135                 output_iec = low_level->output_iec;
136                 device->sharing = 1;
137         }
138         else
139         if(!output_thread && !output_iec)
140         {
141                 if(device->out_config->driver == PLAYBACK_DV1394)
142                 {
143                         output_thread = new Device1394Output(device);
144                         output_thread->open(device->out_config->dv1394_path,
145                                 device->out_config->dv1394_port,
146                                 device->out_config->dv1394_channel,
147                                 30,
148                                 2,
149                                 16,
150                                 48000,
151                                 device->out_config->dv1394_syt);
152                 }
153                 else
154                 if(device->out_config->driver == PLAYBACK_FIREWIRE)
155                 {
156                         output_thread = new Device1394Output(device);
157                         output_thread->open(device->out_config->firewire_path,
158                                 device->out_config->firewire_port,
159                                 device->out_config->firewire_channel,
160                                 30,
161                                 2,
162                                 16,
163                                 48000,
164                                 device->out_config->firewire_syt);
165                 }
166                 else
167                 {
168                         output_iec = new IEC61883Output(device);
169                         output_iec->open(device->out_config->firewire_port,
170                                 device->out_config->firewire_channel,
171                                 30,
172                                 2,
173                                 16,
174                                 48000,
175                                 device->out_config->firewire_syt);
176                 }
177         }
178
179         return 0;
180 }
181
182 int VDevice1394::close_all()
183 {
184         if(device->sharing)
185         {
186                 input_thread = 0;
187                 output_thread = 0;
188                 output_iec = 0;
189                 input_iec = 0;
190                 device->sharing = 0;
191         }
192         else
193         {
194                 if(input_thread)
195                 {
196                         delete input_thread;
197                         input_thread = 0;
198                 }
199                 if(output_thread)
200                 {
201                         delete output_thread;
202                         output_thread = 0;
203                 }
204                 if(input_iec)
205                 {
206                         delete input_iec;
207                         input_iec = 0;
208                 }
209                 if(output_iec)
210                 {
211                         delete output_iec;
212                         output_iec = 0;
213                 }
214         }
215         if(user_frame) delete user_frame;
216         initialize();
217         return 0;
218 }
219
220
221 int VDevice1394::read_buffer(VFrame *frame)
222 {
223         int result = 0;
224         if(!input_thread && !input_iec) return 1;
225
226         if(input_thread) input_thread->read_video(frame);
227         else
228         if(input_iec) input_iec->read_video(frame);
229
230         return result;
231 }
232
233
234 void VDevice1394::new_output_buffer(VFrame **output,
235         int colormodel)
236 {
237         if(user_frame)
238         {
239                 if(colormodel != user_frame->get_color_model())
240                 {
241                         delete user_frame;
242                         user_frame = 0;
243                 }
244         }
245
246         if(!user_frame)
247         {
248                 switch(colormodel)
249                 {
250                         case BC_COMPRESSED:
251                                 user_frame = new VFrame;
252                                 break;
253                         default:
254                                 user_frame = new VFrame(0,
255                                         -1,
256                                         device->out_w,
257                                         device->out_h,
258                                         colormodel,
259                                         -1);
260                                 break;
261                 }
262         }
263
264 //      user_frame->set_shm_offset(0);
265         *output = user_frame;
266 }
267
268 int VDevice1394::write_buffer(VFrame *frame, EDL *edl)
269 {
270         if(output_thread) output_thread->write_frame(frame);
271         else
272         if(output_iec) output_iec->write_frame(frame);
273         return 0;
274 }
275
276
277
278
279 int VDevice1394::can_copy_from(Asset *asset, int output_w, int output_h)
280 {
281         return 0;
282 }