add missing GPL information in guicast program files
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / iec61883output.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 #ifdef HAVE_FIREWIRE
23
24
25
26 #include "audiodevice.h"
27 #include "condition.h"
28 #include "iec61883output.h"
29 #include "mutex.h"
30 #include "playbackconfig.h"
31 #include "bctimer.h"
32 #include "vframe.h"
33 #include "videodevice.h"
34
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <sys/ioctl.h>
39 #include <sys/mman.h>
40 #include <unistd.h>
41 #include <sys/utsname.h>
42
43
44
45
46
47 // Crazy DV internals
48 #define CIP_N_NTSC 2436
49 #define CIP_D_NTSC 38400
50 #define CIP_N_PAL 1
51 #define CIP_D_PAL 16
52 #define OUTPUT_SAMPLES 262144
53 #define BUFFER_TIMEOUT 500000
54
55
56 IEC61883Output::IEC61883Output(AudioDevice *adevice)
57  : Thread(1, 0, 0)
58 {
59         reset();
60         this->adevice = adevice;
61 }
62
63 IEC61883Output::IEC61883Output(VideoDevice *vdevice)
64  : Thread(1, 0, 0)
65 {
66         reset();
67         this->vdevice = vdevice;
68 }
69
70 IEC61883Output::~IEC61883Output()
71 {
72         if(Thread::running())
73         {
74                 done = 1;
75                 start_lock->unlock();
76                 Thread::cancel();
77         }
78         Thread::join();
79
80         if(buffer)
81         {
82                 for(int i = 0; i < total_buffers; i++)
83                 {
84                         if(buffer[i]) delete [] buffer[i];
85                 }
86                 delete [] buffer;
87                 delete [] buffer_size;
88                 delete [] buffer_valid;
89         }
90
91         if(audio_lock) delete audio_lock;
92         if(video_lock) delete video_lock;
93         if(start_lock) delete start_lock;
94         if(audio_buffer) delete [] audio_buffer;
95
96         if(temp_frame) delete temp_frame;
97         if(temp_frame2) delete temp_frame2;
98         if(video_encoder) dv_delete(video_encoder);
99         if(audio_encoder) dv_delete(audio_encoder);
100         if(encoder) dv_delete(encoder);
101         if(buffer_lock) delete buffer_lock;
102         if(position_lock) delete position_lock;
103         if(frame) iec61883_dv_close(frame);
104         if(handle) raw1394_destroy_handle(handle);
105 }
106
107
108 void IEC61883Output::reset()
109 {
110         handle = 0;
111         fd = 0;
112         frame = 0;
113         out_position = 0;
114         out_buffer = 0;
115         out_size = 0;
116
117         buffer = 0;
118         buffer_size = 0;
119         total_buffers = 0;
120         current_inbuffer = 0;
121         current_outbuffer = 0;
122         done = 0;
123         audio_lock = 0;
124         video_lock = 0;
125         start_lock = 0;
126         buffer_lock = 0;
127         position_lock = 0;
128         video_encoder = 0;
129         audio_encoder = 0;
130         encoder = 0;
131         audio_buffer = 0;
132         audio_samples = 0;
133         temp_frame = 0;
134         temp_frame2 = 0;
135         audio_position = 0;
136         interrupted = 0;
137         have_video = 0;
138         adevice = 0;
139         vdevice = 0;
140         is_pal = 0;
141 }
142
143
144
145 static int read_frame_static(unsigned char *data, int n, unsigned int dropped, void *ptr)
146 {
147         IEC61883Output *output = (IEC61883Output*)ptr;
148         return output->read_frame(data, n, dropped);
149 }
150
151
152
153
154
155 int IEC61883Output::open(int port,
156         int channel,
157         int length,
158         int channels,
159         int bits,
160         int samplerate,
161         int syt)
162 {
163         this->channels = channels;
164         this->bits = bits;
165         this->samplerate = samplerate;
166         this->total_buffers = length;
167         this->syt = syt;
168
169 // Set PAL mode based on frame height
170         if(vdevice) is_pal = (vdevice->out_h == 576);
171
172
173
174
175         if(!handle)
176         {
177                 handle = raw1394_new_handle_on_port(port);
178                 if(handle)
179                 {
180                         frame = iec61883_dv_xmit_init(handle,
181                                 is_pal,
182                                 read_frame_static,
183                                 (void *)this);
184                         if(frame)
185                         {
186                                 if(!iec61883_dv_xmit_start(frame, channel))
187                                 {
188                                         fd = raw1394_get_fd(handle);
189                                 }
190                         }
191                 }
192
193 // Create buffers
194                 buffer = new char*[total_buffers];
195                 bzero(buffer, sizeof(char*) * total_buffers);
196                 for(int i = 0; i < length; i++)
197                         buffer[i] = new char[DV_PAL_SIZE];
198                 buffer_size = new int[total_buffers];
199                 buffer_valid = new int[total_buffers];
200                 bzero(buffer_size, sizeof(int) * total_buffers);
201                 bzero(buffer_valid, sizeof(int) * total_buffers);
202                 video_lock = new Condition(0, "IEC61883Output::video_lock");
203                 audio_lock = new Condition(0, "IEC61883Output::audio_lock");
204                 start_lock = new Condition(0, "IEC61883Output::start_lock");
205                 buffer_lock = new Mutex("IEC61883Output::buffer_lock");
206                 position_lock = new Mutex("IEC61883Output::position_lock");
207                 encoder = dv_new();
208                 audio_buffer = new char[OUTPUT_SAMPLES * channels * bits / 8];
209                 Thread::start();
210         }
211         return 0;
212 }
213
214 void IEC61883Output::run()
215 {
216         Thread::enable_cancel();
217         start_lock->lock("IEC61883Output::run");
218         Thread::disable_cancel();
219
220         while(!done)
221         {
222                 struct timeval tv;
223                 fd_set rfds;
224                 FD_ZERO (&rfds);
225                 FD_SET (fd, &rfds);
226                 tv.tv_sec = 0;
227                 tv.tv_usec = 20000;
228                 if(select(fd + 1, &rfds, 0, 0, &tv) > 0)
229                         raw1394_loop_iterate (handle);
230         }
231 }
232
233
234
235 int IEC61883Output::read_frame(unsigned char *data, int n, unsigned int dropped)
236 {
237 // Next frame
238         if(!out_buffer || out_position + 480 > out_size)
239         {
240                 buffer_lock->lock("IEC61883Output read_frame 1");
241
242                 out_buffer = buffer[current_outbuffer];
243                 out_size = buffer_size[current_outbuffer];
244                 out_position = 0;
245
246
247 // No video.  Put in a fake frame for audio only
248                 if(!have_video)
249                 {
250 #include "data/fake_ntsc_dv.h"
251                         out_size = sizeof(fake_ntsc_dv) - 4;
252                         out_buffer = (char*)fake_ntsc_dv + 4;
253                 }
254
255
256
257
258
259
260
261
262 // Calculate number of samples needed based on given pattern for
263 // norm.
264                 int samples_per_frame = 2048;
265
266 // Encode audio
267                 if(audio_samples > samples_per_frame)
268                 {
269                         int samples_written = dv_write_audio(encoder,
270                                 (unsigned char*)out_buffer,
271                                 (unsigned char*)audio_buffer,
272                                 samples_per_frame,
273                                 channels,
274                                 bits,
275                                 samplerate,
276                                 is_pal ? DV_PAL : DV_NTSC);
277                         memcpy(audio_buffer,
278                                 audio_buffer + samples_written * bits * channels / 8,
279                                 (audio_samples - samples_written) * bits * channels / 8);
280                         audio_samples -= samples_written;
281                         position_lock->lock("IEC61883Output::run");
282                         audio_position += samples_written;
283                         position_lock->unlock();
284
285
286                         audio_lock->unlock();
287                 }
288
289
290                 buffer_lock->unlock();
291         }
292
293
294
295
296 // Write next chunk of current frame
297         if(out_buffer && out_position + 480 <= out_size)
298         {
299                 memcpy(data, out_buffer + out_position, 480);
300                 out_position += 480;
301
302
303
304                 if(out_position >= out_size)
305                 {
306                         buffer_lock->lock("IEC61883Output read_frame 2");
307                         buffer_valid[current_outbuffer] = 0;
308
309 // Advance buffer number if possible
310                         increment_counter(&current_outbuffer);
311
312 // Reuse same buffer next time
313                         if(!buffer_valid[current_outbuffer])
314                         {
315                                 decrement_counter(&current_outbuffer);
316                         }
317                         else
318 // Wait for user to reach current buffer before unlocking any more.
319                         {
320                                 video_lock->unlock();
321                         }
322
323                         buffer_lock->unlock();
324                 }
325         }
326         return 0;
327 }
328
329
330
331
332 void IEC61883Output::write_frame(VFrame *input)
333 {
334         VFrame *ptr = 0;
335         int result = 0;
336
337 //printf("IEC61883Output::write_frame 1\n");
338
339         if(fd <= 0) return;
340         if(interrupted) return;
341
342 // Encode frame to DV
343         if(input->get_color_model() != BC_COMPRESSED)
344         {
345                 if(!temp_frame) temp_frame = new VFrame;
346                 if(!encoder) encoder = dv_new();
347                 ptr = temp_frame;
348
349 // Exact resolution match.  Don't do colorspace conversion
350                 if(input->get_color_model() == BC_YUV422 &&
351                         input->get_w() == 720 &&
352                         (input->get_h() == 480 ||
353                         input->get_h() == 576))
354                 {
355                         int norm = is_pal ? DV_PAL : DV_NTSC;
356                         int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
357                         temp_frame->allocate_compressed_data(data_size);
358                         temp_frame->set_compressed_size(data_size);
359
360                         dv_write_video(encoder,
361                                 temp_frame->get_data(),
362                                 input->get_rows(),
363                                 BC_YUV422,
364                                 norm);
365                         ptr = temp_frame;
366                 }
367                 else
368 // Convert resolution and color model before compressing
369                 {
370                         if(!temp_frame2)
371                         {
372                                 int h = input->get_h();
373 // Default to NTSC if unknown
374                                 if(h != 480 && h != 576) h = 480;
375                                 temp_frame2 = new VFrame(720, h, BC_YUV422, 0);
376                         }
377
378                         int norm = is_pal ? DV_PAL : DV_NTSC;
379                         int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
380                         temp_frame->allocate_compressed_data(data_size);
381                         temp_frame->set_compressed_size(data_size);
382
383
384                         BC_CModels::transfer(temp_frame2->get_rows(), /* Leave NULL if non existent */
385                                 input->get_rows(),
386                                 temp_frame2->get_y(), /* Leave NULL if non existent */
387                                 temp_frame2->get_u(),
388                                 temp_frame2->get_v(),
389                                 input->get_y(), /* Leave NULL if non existent */
390                                 input->get_u(),
391                                 input->get_v(),
392                                 0,        /* Dimensions to capture from input frame */
393                                 0,
394                                 MIN(temp_frame2->get_w(), input->get_w()),
395                                 MIN(temp_frame2->get_h(), input->get_h()),
396                                 0,       /* Dimensions to project on output frame */
397                                 0,
398                                 MIN(temp_frame2->get_w(), input->get_w()),
399                                 MIN(temp_frame2->get_h(), input->get_h()),
400                                 input->get_color_model(),
401                                 BC_YUV422,
402                                 0,         /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
403                                 input->get_bytes_per_line(),       /* For planar use the luma rowspan */
404                                 temp_frame2->get_bytes_per_line());     /* For planar use the luma rowspan */
405
406                         dv_write_video(encoder,
407                                 temp_frame->get_data(),
408                                 temp_frame2->get_rows(),
409                                 BC_YUV422,
410                                 norm);
411
412
413
414                         ptr = temp_frame;
415                 }
416         }
417         else
418                 ptr = input;
419
420
421
422
423
424
425
426
427
428
429
430 // Take over buffer table
431         buffer_lock->lock("IEC61883Output::write_frame 1");
432         have_video = 1;
433 // Wait for buffer to become available with timeout
434         while(buffer_valid[current_inbuffer] && !result && !interrupted)
435         {
436                 buffer_lock->unlock();
437                 result = video_lock->timed_lock(BUFFER_TIMEOUT);
438                 buffer_lock->lock("IEC61883Output::write_frame 2");
439         }
440
441
442
443 // Write buffer if there's room
444         if(!buffer_valid[current_inbuffer])
445         {
446                 if(!buffer[current_inbuffer])
447                 {
448                         buffer[current_inbuffer] = new char[ptr->get_compressed_size()];
449                         buffer_size[current_inbuffer] = ptr->get_compressed_size();
450                 }
451                 memcpy(buffer[current_inbuffer], ptr->get_data(), ptr->get_compressed_size());
452                 buffer_valid[current_inbuffer] = 1;
453                 increment_counter(&current_inbuffer);
454         }
455         else
456 // Ignore it if there isn't room.
457         {
458                 ;
459         }
460
461         buffer_lock->unlock();
462         start_lock->unlock();
463 //printf("IEC61883Output::write_frame 100\n");
464 }
465
466 void IEC61883Output::write_samples(char *data, int samples)
467 {
468 //printf("IEC61883Output::write_samples 1\n");
469         int result = 0;
470         //int timeout = (samples * 1000000LL * 2) / samplerate;
471         if(interrupted) return;
472
473 //printf("IEC61883Output::write_samples 2\n");
474
475 // Check for maximum sample count exceeded
476         if(samples > OUTPUT_SAMPLES)
477         {
478                 printf("IEC61883Output::write_samples samples=%d > OUTPUT_SAMPLES=%d\n",
479                         samples,
480                         OUTPUT_SAMPLES);
481                 return;
482         }
483
484 //printf("IEC61883Output::write_samples 3\n");
485 // Take over buffer table
486         buffer_lock->lock("IEC61883Output::write_samples 1");
487 // Wait for buffer to become available with timeout
488         while(audio_samples > OUTPUT_SAMPLES - samples && !result && !interrupted)
489         {
490                 buffer_lock->unlock();
491                 result = audio_lock->timed_lock(BUFFER_TIMEOUT);
492                 buffer_lock->lock("IEC61883Output::write_samples 2");
493         }
494
495         if(!interrupted && audio_samples <= OUTPUT_SAMPLES - samples)
496         {
497 //printf("IEC61883Output::write_samples 4 %d\n", audio_samples);
498                 memcpy(audio_buffer + audio_samples * channels * bits / 8,
499                         data,
500                         samples * channels * bits / 8);
501                 audio_samples += samples;
502         }
503         buffer_lock->unlock();
504         start_lock->unlock();
505 //printf("IEC61883Output::write_samples 100\n");
506 }
507
508 long IEC61883Output::get_audio_position()
509 {
510         position_lock->lock("IEC61883Output::get_audio_position");
511         long result = audio_position;
512         position_lock->unlock();
513         return result;
514 }
515
516 void IEC61883Output::interrupt()
517 {
518         interrupted = 1;
519 // Break write_samples out of a lock
520         video_lock->unlock();
521         audio_lock->unlock();
522 // Playback should stop when the object is deleted.
523 }
524
525 void IEC61883Output::flush()
526 {
527
528 }
529
530 void IEC61883Output::increment_counter(int *counter)
531 {
532         (*counter)++;
533         if(*counter >= total_buffers) *counter = 0;
534 }
535
536 void IEC61883Output::decrement_counter(int *counter)
537 {
538         (*counter)--;
539         if(*counter < 0) *counter = total_buffers - 1;
540 }
541
542
543
544
545
546
547
548
549
550
551
552 #endif // HAVE_FIREWIRE
553
554
555
556
557
558