decklink: prepare for void *p_sys
[vlc.git] / modules / access / decklink.cpp
blobd9d059da87993560bb63c4164de39c7be716aae2
1 /*****************************************************************************
2 * decklink.cpp: BlackMagic DeckLink SDI input module
3 *****************************************************************************
4 * Copyright (C) 2010 Steinar H. Gunderson
5 * Copyright (C) 2012-2014 Rafaël Carré
7 * Authors: Steinar H. Gunderson <steinar+vlc@gunderson.no>
8 Rafaël Carré <funman@videolanorg>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_demux.h>
32 #include <vlc_atomic.h>
34 #include <arpa/inet.h>
36 #include <DeckLinkAPI.h>
37 #include <DeckLinkAPIDispatch.cpp>
39 #include "sdi.h"
41 static int Open (vlc_object_t *);
42 static void Close(vlc_object_t *);
44 #define CARD_INDEX_TEXT N_("Input card to use")
45 #define CARD_INDEX_LONGTEXT N_( \
46 "DeckLink capture card to use, if multiple exist. " \
47 "The cards are numbered from 0.")
49 #define MODE_TEXT N_("Desired input video mode. Leave empty for autodetection.")
50 #define MODE_LONGTEXT N_( \
51 "Desired input video mode for DeckLink captures. " \
52 "This value should be a FOURCC code in textual " \
53 "form, e.g. \"ntsc\".")
55 #define AUDIO_CONNECTION_TEXT N_("Audio connection")
56 #define AUDIO_CONNECTION_LONGTEXT N_( \
57 "Audio connection to use for DeckLink captures. " \
58 "Valid choices: embedded, aesebu, analog. " \
59 "Leave blank for card default.")
61 #define RATE_TEXT N_("Audio samplerate (Hz)")
62 #define RATE_LONGTEXT N_( \
63 "Audio sampling rate (in hertz) for DeckLink captures. " \
64 "0 disables audio input.")
66 #define CHANNELS_TEXT N_("Number of audio channels")
67 #define CHANNELS_LONGTEXT N_( \
68 "Number of input audio channels for DeckLink captures. " \
69 "Must be 2, 8 or 16. 0 disables audio input.")
71 #define VIDEO_CONNECTION_TEXT N_("Video connection")
72 #define VIDEO_CONNECTION_LONGTEXT N_( \
73 "Video connection to use for DeckLink captures. " \
74 "Valid choices: sdi, hdmi, opticalsdi, component, " \
75 "composite, svideo. " \
76 "Leave blank for card default.")
78 static const char *const ppsz_videoconns[] = {
79 "sdi", "hdmi", "opticalsdi", "component", "composite", "svideo"
81 static const char *const ppsz_videoconns_text[] = {
82 N_("SDI"), N_("HDMI"), N_("Optical SDI"), N_("Component"), N_("Composite"), N_("S-Video")
85 static const char *const ppsz_audioconns[] = {
86 "embedded", "aesebu", "analog"
88 static const char *const ppsz_audioconns_text[] = {
89 N_("Embedded"), N_("AES/EBU"), N_("Analog")
92 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
93 #define ASPECT_RATIO_LONGTEXT N_(\
94 "Aspect ratio (4:3, 16:9). Default assumes square pixels.")
96 vlc_module_begin ()
97 set_shortname(N_("DeckLink"))
98 set_description(N_("Blackmagic DeckLink SDI input"))
99 set_category(CAT_INPUT)
100 set_subcategory(SUBCAT_INPUT_ACCESS)
102 add_integer("decklink-card-index", 0,
103 CARD_INDEX_TEXT, CARD_INDEX_LONGTEXT, true)
104 add_string("decklink-mode", NULL,
105 MODE_TEXT, MODE_LONGTEXT, true)
106 add_string("decklink-audio-connection", 0,
107 AUDIO_CONNECTION_TEXT, AUDIO_CONNECTION_LONGTEXT, true)
108 change_string_list(ppsz_audioconns, ppsz_audioconns_text)
109 add_integer("decklink-audio-rate", 48000,
110 RATE_TEXT, RATE_LONGTEXT, true)
111 add_integer("decklink-audio-channels", 2,
112 CHANNELS_TEXT, CHANNELS_LONGTEXT, true)
113 add_string("decklink-video-connection", 0,
114 VIDEO_CONNECTION_TEXT, VIDEO_CONNECTION_LONGTEXT, true)
115 change_string_list(ppsz_videoconns, ppsz_videoconns_text)
116 add_string("decklink-aspect-ratio", NULL,
117 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true)
118 add_bool("decklink-tenbits", false, N_("10 bits"), N_("10 bits"), true)
120 add_shortcut("decklink")
121 set_capability("access_demux", 10)
122 set_callbacks(Open, Close)
123 vlc_module_end ()
125 static int Control(demux_t *, int, va_list);
127 class DeckLinkCaptureDelegate;
129 struct demux_sys_t
131 IDeckLink *card;
132 IDeckLinkInput *input;
133 DeckLinkCaptureDelegate *delegate;
135 /* We need to hold onto the IDeckLinkConfiguration object, or our settings will not apply.
136 See section 2.4.15 of the Blackmagic DeckLink SDK documentation. */
137 IDeckLinkConfiguration *config;
138 IDeckLinkAttributes *attributes;
140 bool autodetect;
142 es_out_id_t *video_es;
143 es_format_t video_fmt;
144 es_out_id_t *audio_es;
145 es_out_id_t *cc_es;
147 vlc_mutex_t pts_lock;
148 int last_pts; /* protected by <pts_lock> */
150 uint32_t dominance_flags;
151 int channels;
153 bool tenbits;
156 static const char *GetFieldDominance(BMDFieldDominance dom, uint32_t *flags)
158 switch(dom)
160 case bmdProgressiveFrame:
161 return "";
162 case bmdProgressiveSegmentedFrame:
163 return ", segmented";
164 case bmdLowerFieldFirst:
165 *flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
166 return ", interlaced [BFF]";
167 case bmdUpperFieldFirst:
168 *flags = BLOCK_FLAG_TOP_FIELD_FIRST;
169 return ", interlaced [TFF]";
170 case bmdUnknownFieldDominance:
171 default:
172 return ", unknown field dominance";
176 static es_format_t GetModeSettings(demux_t *demux, IDeckLinkDisplayMode *m,
177 BMDDetectedVideoInputFormatFlags fmt_flags)
179 demux_sys_t *sys = (demux_sys_t *)demux->p_sys;
180 uint32_t flags = 0;
181 (void)GetFieldDominance(m->GetFieldDominance(), &flags);
183 BMDTimeValue frame_duration, time_scale;
184 if (m->GetFrameRate(&frame_duration, &time_scale) != S_OK) {
185 time_scale = 0;
186 frame_duration = 1;
189 es_format_t video_fmt;
190 vlc_fourcc_t chroma = 0;
191 switch (fmt_flags) {
192 case bmdDetectedVideoInputYCbCr422:
193 chroma = sys->tenbits ? VLC_CODEC_I422_10L : VLC_CODEC_UYVY;
194 break;
195 case bmdDetectedVideoInputRGB444:
196 chroma = VLC_CODEC_ARGB;
197 break;
198 default:
199 msg_Err(demux, "Unsupported input format");
200 break;
202 es_format_Init(&video_fmt, VIDEO_ES, chroma);
204 video_fmt.video.i_chroma = chroma;
205 video_fmt.video.i_width = m->GetWidth();
206 video_fmt.video.i_height = m->GetHeight();
207 video_fmt.video.i_sar_num = 1;
208 video_fmt.video.i_sar_den = 1;
209 video_fmt.video.i_frame_rate = time_scale;
210 video_fmt.video.i_frame_rate_base = frame_duration;
211 video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2 * 8;
213 unsigned aspect_num, aspect_den;
214 if (!var_InheritURational(demux, &aspect_num, &aspect_den, "decklink-aspect-ratio") &&
215 aspect_num > 0 && aspect_den > 0) {
216 video_fmt.video.i_sar_num = aspect_num * video_fmt.video.i_height;
217 video_fmt.video.i_sar_den = aspect_den * video_fmt.video.i_width;
220 sys->dominance_flags = flags;
222 return video_fmt;
225 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
227 public:
228 DeckLinkCaptureDelegate(demux_t *demux) : demux_(demux)
230 m_ref_.store(1);
233 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, LPVOID *) { return E_NOINTERFACE; }
235 virtual ULONG STDMETHODCALLTYPE AddRef(void)
237 return m_ref_.fetch_add(1);
240 virtual ULONG STDMETHODCALLTYPE Release(void)
242 uintptr_t new_ref = m_ref_.fetch_sub(1);
243 if (new_ref == 0)
244 delete this;
245 return new_ref;
248 virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags flags)
250 demux_sys_t *sys = demux_->p_sys;
252 if( !(events & bmdVideoInputDisplayModeChanged ))
253 return S_OK;
255 const char *mode_name;
256 if (mode->GetName(&mode_name) != S_OK)
257 mode_name = "unknown";
259 msg_Dbg(demux_, "Video input format changed to %s", mode_name);
260 if (!sys->autodetect) {
261 msg_Err(demux_, "Video format detection disabled");
262 return S_OK;
265 BMDPixelFormat fmt = 0;
266 switch (flags) {
267 case bmdDetectedVideoInputYCbCr422:
268 fmt = sys->tenbits ? bmdFormat10BitYUV : bmdFormat8BitYUV;
269 break;
270 case bmdDetectedVideoInputRGB444:
271 fmt = bmdFormat8BitARGB;
272 break;
273 default:
274 msg_Err(demux_, "Unsupported input format");
275 return S_OK;
278 es_out_Del(demux_->out, sys->video_es);
279 sys->video_fmt = GetModeSettings(demux_, mode, flags);
280 sys->video_es = es_out_Add(demux_->out, &sys->video_fmt);
282 sys->input->PauseStreams();
283 sys->input->EnableVideoInput( mode->GetDisplayMode(), fmt, bmdVideoInputEnableFormatDetection );
284 sys->input->FlushStreams();
285 sys->input->StartStreams();
287 return S_OK;
290 virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
292 private:
293 std::atomic_uint m_ref_;
294 demux_t *demux_;
297 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
299 demux_sys_t *sys = demux_->p_sys;
301 if (videoFrame) {
302 if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
303 msg_Warn(demux_, "No input signal detected (%dx%d)",
304 videoFrame->GetWidth(), videoFrame->GetHeight());
305 return S_OK;
308 const int width = videoFrame->GetWidth();
309 const int height = videoFrame->GetHeight();
310 const int stride = videoFrame->GetRowBytes();
312 int bpp = 0;
313 switch (sys->video_fmt.i_codec) {
314 case VLC_CODEC_I422_10L:
315 case VLC_CODEC_ARGB:
316 bpp = 4;
317 break;
318 case VLC_CODEC_UYVY:
319 bpp = 2;
320 break;
322 block_t *video_frame = block_Alloc(width * height * bpp);
323 if (!video_frame)
324 return S_OK;
326 const uint32_t *frame_bytes;
327 videoFrame->GetBytes((void**)&frame_bytes);
329 BMDTimeValue stream_time, frame_duration;
330 videoFrame->GetStreamTime(&stream_time, &frame_duration, CLOCK_FREQ);
331 video_frame->i_flags = BLOCK_FLAG_TYPE_I | sys->dominance_flags;
332 video_frame->i_pts = video_frame->i_dts = VLC_TS_0 + stream_time;
334 if (sys->video_fmt.i_codec == VLC_CODEC_I422_10L) {
335 v210_convert((uint16_t*)video_frame->p_buffer, frame_bytes, width, height);
336 IDeckLinkVideoFrameAncillary *vanc;
337 if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
338 for (int i = 1; i < 21; i++) {
339 uint32_t *buf;
340 if (vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) != S_OK)
341 break;
342 uint16_t dec[width * 2];
343 v210_convert(&dec[0], buf, width, 1);
344 block_t *cc = vanc_to_cc(demux_, dec, width * 2);
345 if (!cc)
346 continue;
347 cc->i_pts = cc->i_dts = VLC_TS_0 + stream_time;
349 if (!sys->cc_es) {
350 es_format_t fmt;
352 es_format_Init( &fmt, SPU_ES, VLC_CODEC_CEA608 );
353 fmt.psz_description = strdup(N_("Closed captions 1"));
354 if (fmt.psz_description) {
355 sys->cc_es = es_out_Add(demux_->out, &fmt);
356 msg_Dbg(demux_, "Adding Closed captions stream");
359 if (sys->cc_es)
360 es_out_Send(demux_->out, sys->cc_es, cc);
361 else
362 block_Release(cc);
363 break; // we found the line with Closed Caption data
365 vanc->Release();
367 } else if (sys->video_fmt.i_codec == VLC_CODEC_UYVY) {
368 for (int y = 0; y < height; ++y) {
369 const uint8_t *src = (const uint8_t *)frame_bytes + stride * y;
370 uint8_t *dst = video_frame->p_buffer + width * 2 * y;
371 memcpy(dst, src, width * 2);
373 } else {
374 memcpy(video_frame->p_buffer, frame_bytes, width * height * bpp);
377 vlc_mutex_lock(&sys->pts_lock);
378 if (video_frame->i_pts > sys->last_pts)
379 sys->last_pts = video_frame->i_pts;
380 vlc_mutex_unlock(&sys->pts_lock);
382 es_out_SetPCR(demux_->out, video_frame->i_pts);
383 es_out_Send(demux_->out, sys->video_es, video_frame);
386 if (audioFrame) {
387 const int bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * sys->channels;
389 block_t *audio_frame = block_Alloc(bytes);
390 if (!audio_frame)
391 return S_OK;
393 void *frame_bytes;
394 audioFrame->GetBytes(&frame_bytes);
395 memcpy(audio_frame->p_buffer, frame_bytes, bytes);
397 BMDTimeValue packet_time;
398 audioFrame->GetPacketTime(&packet_time, CLOCK_FREQ);
399 audio_frame->i_pts = audio_frame->i_dts = VLC_TS_0 + packet_time;
401 vlc_mutex_lock(&sys->pts_lock);
402 if (audio_frame->i_pts > sys->last_pts)
403 sys->last_pts = audio_frame->i_pts;
404 vlc_mutex_unlock(&sys->pts_lock);
406 es_out_SetPCR(demux_->out, audio_frame->i_pts);
407 es_out_Send(demux_->out, sys->audio_es, audio_frame);
410 return S_OK;
414 static int GetAudioConn(demux_t *demux)
416 demux_sys_t *sys = (demux_sys_t *)demux->p_sys;
418 char *opt = var_CreateGetNonEmptyString(demux, "decklink-audio-connection");
419 if (!opt)
420 return VLC_SUCCESS;
422 BMDAudioConnection c;
423 if (!strcmp(opt, "embedded"))
424 c = bmdAudioConnectionEmbedded;
425 else if (!strcmp(opt, "aesebu"))
426 c = bmdAudioConnectionAESEBU;
427 else if (!strcmp(opt, "analog"))
428 c = bmdAudioConnectionAnalog;
429 else {
430 msg_Err(demux, "Invalid audio-connection: `%s\' specified", opt);
431 free(opt);
432 return VLC_EGENERIC;
435 if (sys->config->SetInt(bmdDeckLinkConfigAudioInputConnection, c) != S_OK) {
436 msg_Err(demux, "Failed to set audio input connection");
437 return VLC_EGENERIC;
440 return VLC_SUCCESS;
443 static int GetVideoConn(demux_t *demux)
445 demux_sys_t *sys = (demux_sys_t *)demux->p_sys;
447 char *opt = var_InheritString(demux, "decklink-video-connection");
448 if (!opt)
449 return VLC_SUCCESS;
451 BMDVideoConnection c;
452 if (!strcmp(opt, "sdi"))
453 c = bmdVideoConnectionSDI;
454 else if (!strcmp(opt, "hdmi"))
455 c = bmdVideoConnectionHDMI;
456 else if (!strcmp(opt, "opticalsdi"))
457 c = bmdVideoConnectionOpticalSDI;
458 else if (!strcmp(opt, "component"))
459 c = bmdVideoConnectionComponent;
460 else if (!strcmp(opt, "composite"))
461 c = bmdVideoConnectionComposite;
462 else if (!strcmp(opt, "svideo"))
463 c = bmdVideoConnectionSVideo;
464 else {
465 msg_Err(demux, "Invalid video-connection: `%s\' specified", opt);
466 free(opt);
467 return VLC_EGENERIC;
470 free(opt);
471 if (sys->config->SetInt(bmdDeckLinkConfigVideoInputConnection, c) != S_OK) {
472 msg_Err(demux, "Failed to set video input connection");
473 return VLC_EGENERIC;
476 return VLC_SUCCESS;
479 static int Open(vlc_object_t *p_this)
481 demux_t *demux = (demux_t*)p_this;
482 demux_sys_t *sys;
483 int ret = VLC_EGENERIC;
484 int card_index;
485 int physical_channels = 0;
486 int rate;
487 BMDVideoInputFlags flags = bmdVideoInputFlagDefault;
489 /* Set up demux */
490 demux->pf_demux = NULL;
491 demux->pf_control = Control;
492 demux->info.i_update = 0;
493 demux->p_sys = sys = (demux_sys_t*)calloc(1, sizeof(demux_sys_t));
494 if (!sys)
495 return VLC_ENOMEM;
497 vlc_mutex_init(&sys->pts_lock);
499 sys->tenbits = var_InheritBool(p_this, "decklink-tenbits");
501 IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
502 if (!decklink_iterator) {
503 msg_Err(demux, "DeckLink drivers not found.");
504 goto finish;
507 card_index = var_InheritInteger(demux, "decklink-card-index");
508 if (card_index < 0) {
509 msg_Err(demux, "Invalid card index %d", card_index);
510 goto finish;
513 for (int i = 0; i <= card_index; i++) {
514 if (sys->card)
515 sys->card->Release();
516 if (decklink_iterator->Next(&sys->card) != S_OK) {
517 msg_Err(demux, "DeckLink PCI card %d not found", card_index);
518 goto finish;
522 const char *model_name;
523 if (sys->card->GetModelName(&model_name) != S_OK)
524 model_name = "unknown";
526 msg_Dbg(demux, "Opened DeckLink PCI card %d (%s)", card_index, model_name);
528 if (sys->card->QueryInterface(IID_IDeckLinkInput, (void**)&sys->input) != S_OK) {
529 msg_Err(demux, "Card has no inputs");
530 goto finish;
533 /* Set up the video and audio sources. */
534 if (sys->card->QueryInterface(IID_IDeckLinkConfiguration, (void**)&sys->config) != S_OK) {
535 msg_Err(demux, "Failed to get configuration interface");
536 goto finish;
539 if (sys->card->QueryInterface(IID_IDeckLinkAttributes, (void**)&sys->attributes) != S_OK) {
540 msg_Err(demux, "Failed to get attributes interface");
541 goto finish;
544 if (GetVideoConn(demux) || GetAudioConn(demux))
545 goto finish;
547 BMDPixelFormat fmt;
548 fmt = sys->tenbits ? bmdFormat10BitYUV : bmdFormat8BitYUV;
549 if (sys->attributes->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &sys->autodetect) != S_OK) {
550 msg_Err(demux, "Failed to query card attribute");
551 goto finish;
554 /* Get the list of display modes. */
555 IDeckLinkDisplayModeIterator *mode_it;
556 if (sys->input->GetDisplayModeIterator(&mode_it) != S_OK) {
557 msg_Err(demux, "Failed to enumerate display modes");
558 goto finish;
561 union {
562 BMDDisplayMode id;
563 char str[4];
564 } u;
566 u.id = 0;
568 char *mode;
569 mode = var_CreateGetNonEmptyString(demux, "decklink-mode");
570 if (mode)
571 sys->autodetect = false; // disable autodetection if mode was set
573 if (sys->autodetect) {
574 msg_Dbg(demux, "Card supports input format detection");
575 flags |= bmdVideoInputEnableFormatDetection;
576 /* Enable a random format, we will reconfigure on format detection */
577 u.id = htonl(bmdModeHD1080p2997);
578 } else {
579 if (!mode || strlen(mode) < 3 || strlen(mode) > 4) {
580 msg_Err(demux, "Invalid mode: \'%s\'", mode ? mode : "");
581 free(mode);
582 goto finish;
585 msg_Dbg(demux, "Looking for mode \'%s\'", mode);
586 memcpy(u.str, mode, 4);
587 if (u.str[3] == '\0')
588 u.str[3] = ' '; /* 'pal'\0 -> 'pal ' */
589 free(mode);
592 sys->video_fmt.video.i_width = 0;
594 for (IDeckLinkDisplayMode *m;; m->Release()) {
595 if ((mode_it->Next(&m) != S_OK) || !m)
596 break;
598 const char *mode_name;
599 BMDTimeValue frame_duration, time_scale;
600 uint32_t field_flags;
601 const char *field = GetFieldDominance(m->GetFieldDominance(), &field_flags);
602 BMDDisplayMode id = ntohl(m->GetDisplayMode());
604 if (m->GetName(&mode_name) != S_OK)
605 mode_name = "unknown";
606 if (m->GetFrameRate(&frame_duration, &time_scale) != S_OK) {
607 time_scale = 0;
608 frame_duration = 1;
611 msg_Dbg(demux, "Found mode '%4.4s': %s (%dx%d, %.3f fps%s)",
612 (char*)&id, mode_name,
613 (int)m->GetWidth(), (int)m->GetHeight(),
614 double(time_scale) / frame_duration, field);
616 if (u.id == id) {
617 sys->video_fmt = GetModeSettings(demux, m, bmdDetectedVideoInputYCbCr422);
618 msg_Dbg(demux, "Using that mode");
622 mode_it->Release();
624 if (sys->video_fmt.video.i_width == 0) {
625 msg_Err(demux, "Unknown video mode `%4.4s\' specified.", (char*)&u.id);
626 goto finish;
629 if (sys->input->EnableVideoInput(htonl(u.id), fmt, flags) != S_OK) {
630 msg_Err(demux, "Failed to enable video input");
631 goto finish;
634 /* Set up audio. */
635 sys->channels = var_InheritInteger(demux, "decklink-audio-channels");
636 switch (sys->channels) {
637 case 0:
638 break;
639 case 2:
640 physical_channels = AOUT_CHANS_STEREO;
641 break;
642 case 8:
643 physical_channels = AOUT_CHANS_7_1;
644 break;
645 //case 16:
646 default:
647 msg_Err(demux, "Invalid number of channels (%d), disabling audio", sys->channels);
648 sys->channels = 0;
650 rate = var_InheritInteger(demux, "decklink-audio-rate");
651 if (rate > 0 && sys->channels > 0) {
652 if (sys->input->EnableAudioInput(rate, bmdAudioSampleType16bitInteger, sys->channels) != S_OK) {
653 msg_Err(demux, "Failed to enable audio input");
654 goto finish;
658 sys->delegate = new DeckLinkCaptureDelegate(demux);
659 sys->input->SetCallback(sys->delegate);
661 if (sys->input->StartStreams() != S_OK) {
662 msg_Err(demux, "Could not start streaming from SDI card. This could be caused "
663 "by invalid video mode or flags, access denied, or card already in use.");
664 goto finish;
667 msg_Dbg(demux, "added new video es %4.4s %dx%d",
668 (char*)&sys->video_fmt.i_codec, sys->video_fmt.video.i_width, sys->video_fmt.video.i_height);
669 sys->video_es = es_out_Add(demux->out, &sys->video_fmt);
671 es_format_t audio_fmt;
672 es_format_Init(&audio_fmt, AUDIO_ES, VLC_CODEC_S16N);
673 audio_fmt.audio.i_channels = sys->channels;
674 audio_fmt.audio.i_physical_channels = physical_channels;
675 audio_fmt.audio.i_rate = rate;
676 audio_fmt.audio.i_bitspersample = 16;
677 audio_fmt.audio.i_blockalign = audio_fmt.audio.i_channels * audio_fmt.audio.i_bitspersample / 8;
678 audio_fmt.i_bitrate = audio_fmt.audio.i_channels * audio_fmt.audio.i_rate * audio_fmt.audio.i_bitspersample;
680 msg_Dbg(demux, "added new audio es %4.4s %dHz %dbpp %dch",
681 (char*)&audio_fmt.i_codec, audio_fmt.audio.i_rate, audio_fmt.audio.i_bitspersample, audio_fmt.audio.i_channels);
682 sys->audio_es = es_out_Add(demux->out, &audio_fmt);
684 ret = VLC_SUCCESS;
686 finish:
687 if (decklink_iterator)
688 decklink_iterator->Release();
690 if (ret != VLC_SUCCESS)
691 Close(p_this);
693 return ret;
696 static void Close(vlc_object_t *p_this)
698 demux_t *demux = (demux_t *)p_this;
699 demux_sys_t *sys = (demux_sys_t *)demux->p_sys;
701 if (sys->attributes)
702 sys->attributes->Release();
704 if (sys->config)
705 sys->config->Release();
707 if (sys->input) {
708 sys->input->StopStreams();
709 sys->input->Release();
712 if (sys->card)
713 sys->card->Release();
715 if (sys->delegate)
716 sys->delegate->Release();
718 vlc_mutex_destroy(&sys->pts_lock);
719 free(sys);
722 static int Control(demux_t *demux, int query, va_list args)
724 demux_sys_t *sys = (demux_sys_t *)demux->p_sys;
725 bool *pb;
726 int64_t *pi64;
728 switch(query)
730 /* Special for access_demux */
731 case DEMUX_CAN_PAUSE:
732 case DEMUX_CAN_SEEK:
733 case DEMUX_CAN_CONTROL_PACE:
734 pb = va_arg(args, bool *);
735 *pb = false;
736 return VLC_SUCCESS;
738 case DEMUX_GET_PTS_DELAY:
739 pi64 = va_arg(args, int64_t *);
740 *pi64 = INT64_C(1000) * var_InheritInteger(demux, "live-caching");
741 return VLC_SUCCESS;
743 case DEMUX_GET_TIME:
744 pi64 = va_arg(args, int64_t *);
745 vlc_mutex_lock(&sys->pts_lock);
746 *pi64 = sys->last_pts;
747 vlc_mutex_unlock(&sys->pts_lock);
748 return VLC_SUCCESS;
750 default:
751 return VLC_EGENERIC;