4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
26 #include "audiodevice.h"
27 #include "condition.h"
28 #include "iec61883output.h"
30 #include "playbackconfig.h"
33 #include "videodevice.h"
38 #include <sys/ioctl.h>
41 #include <sys/utsname.h>
48 #define CIP_N_NTSC 2436
49 #define CIP_D_NTSC 38400
52 #define OUTPUT_SAMPLES 262144
53 #define BUFFER_TIMEOUT 500000
56 IEC61883Output::IEC61883Output(AudioDevice *adevice)
60 this->adevice = adevice;
63 IEC61883Output::IEC61883Output(VideoDevice *vdevice)
67 this->vdevice = vdevice;
70 IEC61883Output::~IEC61883Output()
82 for(int i = 0; i < total_buffers; i++)
84 if(buffer[i]) delete [] buffer[i];
87 delete [] buffer_size;
88 delete [] buffer_valid;
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;
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(buffer_lock) delete buffer_lock;
101 if(position_lock) delete position_lock;
102 if(frame) iec61883_dv_close(frame);
103 if(handle) raw1394_destroy_handle(handle);
107 void IEC61883Output::reset()
119 current_inbuffer = 0;
120 current_outbuffer = 0;
143 static int read_frame_static(unsigned char *data, int n, unsigned int dropped, void *ptr)
145 IEC61883Output *output = (IEC61883Output*)ptr;
146 return output->read_frame(data, n, dropped);
153 int IEC61883Output::open(int port,
161 this->channels = channels;
163 this->samplerate = samplerate;
164 this->total_buffers = length;
167 // Set PAL mode based on frame height
168 if(vdevice) is_pal = (vdevice->out_h == 576);
175 handle = raw1394_new_handle_on_port(port);
178 frame = iec61883_dv_xmit_init(handle,
184 if(!iec61883_dv_xmit_start(frame, channel))
186 fd = raw1394_get_fd(handle);
192 buffer = new char*[total_buffers];
193 for(int i = 0; i < length; i++)
194 buffer[i] = new char[DV_PAL_SIZE];
195 buffer_size = new int[total_buffers];
196 buffer_valid = new int[total_buffers];
197 bzero(buffer_size, sizeof(int) * total_buffers);
198 bzero(buffer_valid, sizeof(int) * total_buffers);
199 bzero(buffer, sizeof(char*) * total_buffers);
200 video_lock = new Condition(0, "IEC61883Output::video_lock");
201 audio_lock = new Condition(0, "IEC61883Output::audio_lock");
202 start_lock = new Condition(0, "IEC61883Output::start_lock");
203 buffer_lock = new Mutex("IEC61883Output::buffer_lock");
204 position_lock = new Mutex("IEC61883Output::position_lock");
206 audio_buffer = new char[OUTPUT_SAMPLES * channels * bits / 8];
212 void IEC61883Output::run()
214 Thread::enable_cancel();
215 start_lock->lock("IEC61883Output::run");
216 Thread::disable_cancel();
226 if(select(fd + 1, &rfds, 0, 0, &tv) > 0)
227 raw1394_loop_iterate (handle);
233 int IEC61883Output::read_frame(unsigned char *data, int n, unsigned int dropped)
236 if(!out_buffer || out_position + 480 > out_size)
238 buffer_lock->lock("IEC61883Output read_frame 1");
240 out_buffer = buffer[current_outbuffer];
241 out_size = buffer_size[current_outbuffer];
245 // No video. Put in a fake frame for audio only
248 #include "data/fake_ntsc_dv.h"
249 out_size = sizeof(fake_ntsc_dv) - 4;
250 out_buffer = (char*)fake_ntsc_dv + 4;
260 // Calculate number of samples needed based on given pattern for
262 int samples_per_frame = 2048;
265 if(audio_samples > samples_per_frame)
267 int samples_written = dv_write_audio(encoder,
268 (unsigned char*)out_buffer,
269 (unsigned char*)audio_buffer,
274 is_pal ? DV_PAL : DV_NTSC);
276 audio_buffer + samples_written * bits * channels / 8,
277 (audio_samples - samples_written) * bits * channels / 8);
278 audio_samples -= samples_written;
279 position_lock->lock("IEC61883Output::run");
280 audio_position += samples_written;
281 position_lock->unlock();
284 audio_lock->unlock();
288 buffer_lock->unlock();
294 // Write next chunk of current frame
295 if(out_buffer && out_position + 480 <= out_size)
297 memcpy(data, out_buffer + out_position, 480);
302 if(out_position >= out_size)
304 buffer_lock->lock("IEC61883Output read_frame 2");
305 buffer_valid[current_outbuffer] = 0;
307 // Advance buffer number if possible
308 increment_counter(¤t_outbuffer);
310 // Reuse same buffer next time
311 if(!buffer_valid[current_outbuffer])
313 decrement_counter(¤t_outbuffer);
316 // Wait for user to reach current buffer before unlocking any more.
318 video_lock->unlock();
321 buffer_lock->unlock();
330 void IEC61883Output::write_frame(VFrame *input)
335 //printf("IEC61883Output::write_frame 1\n");
338 if(interrupted) return;
340 // Encode frame to DV
341 if(input->get_color_model() != BC_COMPRESSED)
343 if(!temp_frame) temp_frame = new VFrame;
344 if(!encoder) encoder = dv_new();
347 // Exact resolution match. Don't do colorspace conversion
348 if(input->get_color_model() == BC_YUV422 &&
349 input->get_w() == 720 &&
350 (input->get_h() == 480 ||
351 input->get_h() == 576))
353 int norm = is_pal ? DV_PAL : DV_NTSC;
354 int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
355 temp_frame->allocate_compressed_data(data_size);
356 temp_frame->set_compressed_size(data_size);
358 dv_write_video(encoder,
359 temp_frame->get_data(),
366 // Convert resolution and color model before compressing
370 int h = input->get_h();
371 // Default to NTSC if unknown
372 if(h != 480 && h != 576) h = 480;
373 temp_frame2 = new VFrame(720, h, BC_YUV422, 0);
376 int norm = is_pal ? DV_PAL : DV_NTSC;
377 int data_size = is_pal ? DV_PAL_SIZE : DV_NTSC_SIZE;
378 temp_frame->allocate_compressed_data(data_size);
379 temp_frame->set_compressed_size(data_size);
382 BC_CModels::transfer(temp_frame2->get_rows(), /* Leave NULL if non existent */
384 temp_frame2->get_y(), /* Leave NULL if non existent */
385 temp_frame2->get_u(),
386 temp_frame2->get_v(),
387 input->get_y(), /* Leave NULL if non existent */
390 0, /* Dimensions to capture from input frame */
392 MIN(temp_frame2->get_w(), input->get_w()),
393 MIN(temp_frame2->get_h(), input->get_h()),
394 0, /* Dimensions to project on output frame */
396 MIN(temp_frame2->get_w(), input->get_w()),
397 MIN(temp_frame2->get_h(), input->get_h()),
398 input->get_color_model(),
400 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
401 input->get_bytes_per_line(), /* For planar use the luma rowspan */
402 temp_frame2->get_bytes_per_line()); /* For planar use the luma rowspan */
404 dv_write_video(encoder,
405 temp_frame->get_data(),
406 temp_frame2->get_rows(),
428 // Take over buffer table
429 buffer_lock->lock("IEC61883Output::write_frame 1");
431 // Wait for buffer to become available with timeout
432 while(buffer_valid[current_inbuffer] && !result && !interrupted)
434 buffer_lock->unlock();
435 result = video_lock->timed_lock(BUFFER_TIMEOUT);
436 buffer_lock->lock("IEC61883Output::write_frame 2");
441 // Write buffer if there's room
442 if(!buffer_valid[current_inbuffer])
444 if(!buffer[current_inbuffer])
446 buffer[current_inbuffer] = new char[ptr->get_compressed_size()];
447 buffer_size[current_inbuffer] = ptr->get_compressed_size();
449 memcpy(buffer[current_inbuffer], ptr->get_data(), ptr->get_compressed_size());
450 buffer_valid[current_inbuffer] = 1;
451 increment_counter(¤t_inbuffer);
454 // Ignore it if there isn't room.
459 buffer_lock->unlock();
460 start_lock->unlock();
461 //printf("IEC61883Output::write_frame 100\n");
464 void IEC61883Output::write_samples(char *data, int samples)
466 //printf("IEC61883Output::write_samples 1\n");
468 //int timeout = (samples * 1000000LL * 2) / samplerate;
469 if(interrupted) return;
471 //printf("IEC61883Output::write_samples 2\n");
473 // Check for maximum sample count exceeded
474 if(samples > OUTPUT_SAMPLES)
476 printf("IEC61883Output::write_samples samples=%d > OUTPUT_SAMPLES=%d\n",
482 //printf("IEC61883Output::write_samples 3\n");
483 // Take over buffer table
484 buffer_lock->lock("IEC61883Output::write_samples 1");
485 // Wait for buffer to become available with timeout
486 while(audio_samples > OUTPUT_SAMPLES - samples && !result && !interrupted)
488 buffer_lock->unlock();
489 result = audio_lock->timed_lock(BUFFER_TIMEOUT);
490 buffer_lock->lock("IEC61883Output::write_samples 2");
493 if(!interrupted && audio_samples <= OUTPUT_SAMPLES - samples)
495 //printf("IEC61883Output::write_samples 4 %d\n", audio_samples);
496 memcpy(audio_buffer + audio_samples * channels * bits / 8,
498 samples * channels * bits / 8);
499 audio_samples += samples;
501 buffer_lock->unlock();
502 start_lock->unlock();
503 //printf("IEC61883Output::write_samples 100\n");
506 long IEC61883Output::get_audio_position()
508 position_lock->lock("IEC61883Output::get_audio_position");
509 long result = audio_position;
510 position_lock->unlock();
514 void IEC61883Output::interrupt()
517 // Break write_samples out of a lock
518 video_lock->unlock();
519 audio_lock->unlock();
520 // Playback should stop when the object is deleted.
523 void IEC61883Output::flush()
528 void IEC61883Output::increment_counter(int *counter)
531 if(*counter >= total_buffers) *counter = 0;
534 void IEC61883Output::decrement_counter(int *counter)
537 if(*counter < 0) *counter = total_buffers - 1;
550 #endif // HAVE_FIREWIRE