demux: ts: only seek on pcr for current program
[vlc.git] / modules / access / decklink.cpp
blob04f0b469f4ac39a2bb1f111555356513a89d21c4
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->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->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->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 /* Only when selected */
490 if (*demux->psz_access == '\0')
491 return VLC_EGENERIC;
493 /* Set up demux */
494 demux->pf_demux = NULL;
495 demux->pf_control = Control;
496 demux->info.i_update = 0;
497 demux->info.i_title = 0;
498 demux->info.i_seekpoint = 0;
499 demux->p_sys = sys = (demux_sys_t*)calloc(1, sizeof(demux_sys_t));
500 if (!sys)
501 return VLC_ENOMEM;
503 vlc_mutex_init(&sys->pts_lock);
505 sys->tenbits = var_InheritBool(p_this, "decklink-tenbits");
507 IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
508 if (!decklink_iterator) {
509 msg_Err(demux, "DeckLink drivers not found.");
510 goto finish;
513 card_index = var_InheritInteger(demux, "decklink-card-index");
514 if (card_index < 0) {
515 msg_Err(demux, "Invalid card index %d", card_index);
516 goto finish;
519 for (int i = 0; i <= card_index; i++) {
520 if (sys->card)
521 sys->card->Release();
522 if (decklink_iterator->Next(&sys->card) != S_OK) {
523 msg_Err(demux, "DeckLink PCI card %d not found", card_index);
524 goto finish;
528 const char *model_name;
529 if (sys->card->GetModelName(&model_name) != S_OK)
530 model_name = "unknown";
532 msg_Dbg(demux, "Opened DeckLink PCI card %d (%s)", card_index, model_name);
534 if (sys->card->QueryInterface(IID_IDeckLinkInput, (void**)&sys->input) != S_OK) {
535 msg_Err(demux, "Card has no inputs");
536 goto finish;
539 /* Set up the video and audio sources. */
540 if (sys->card->QueryInterface(IID_IDeckLinkConfiguration, (void**)&sys->config) != S_OK) {
541 msg_Err(demux, "Failed to get configuration interface");
542 goto finish;
545 if (sys->card->QueryInterface(IID_IDeckLinkAttributes, (void**)&sys->attributes) != S_OK) {
546 msg_Err(demux, "Failed to get attributes interface");
547 goto finish;
550 if (GetVideoConn(demux) || GetAudioConn(demux))
551 goto finish;
553 BMDPixelFormat fmt;
554 fmt = sys->tenbits ? bmdFormat10BitYUV : bmdFormat8BitYUV;
555 if (sys->attributes->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &sys->autodetect) != S_OK) {
556 msg_Err(demux, "Failed to query card attribute");
557 goto finish;
560 /* Get the list of display modes. */
561 IDeckLinkDisplayModeIterator *mode_it;
562 if (sys->input->GetDisplayModeIterator(&mode_it) != S_OK) {
563 msg_Err(demux, "Failed to enumerate display modes");
564 goto finish;
567 union {
568 BMDDisplayMode id;
569 char str[4];
570 } u;
572 u.id = 0;
574 char *mode;
575 mode = var_CreateGetNonEmptyString(demux, "decklink-mode");
576 if (mode)
577 sys->autodetect = false; // disable autodetection if mode was set
579 if (sys->autodetect) {
580 msg_Dbg(demux, "Card supports input format detection");
581 flags |= bmdVideoInputEnableFormatDetection;
582 /* Enable a random format, we will reconfigure on format detection */
583 u.id = htonl(bmdModeHD1080p2997);
584 } else {
585 if (!mode || strlen(mode) < 3 || strlen(mode) > 4) {
586 msg_Err(demux, "Invalid mode: \'%s\'", mode ? mode : "");
587 free(mode);
588 goto finish;
591 msg_Dbg(demux, "Looking for mode \'%s\'", mode);
592 memcpy(u.str, mode, 4);
593 if (u.str[3] == '\0')
594 u.str[3] = ' '; /* 'pal'\0 -> 'pal ' */
595 free(mode);
598 sys->video_fmt.video.i_width = 0;
600 for (IDeckLinkDisplayMode *m;; m->Release()) {
601 if ((mode_it->Next(&m) != S_OK) || !m)
602 break;
604 const char *mode_name;
605 BMDTimeValue frame_duration, time_scale;
606 uint32_t field_flags;
607 const char *field = GetFieldDominance(m->GetFieldDominance(), &field_flags);
608 BMDDisplayMode id = ntohl(m->GetDisplayMode());
610 if (m->GetName(&mode_name) != S_OK)
611 mode_name = "unknown";
612 if (m->GetFrameRate(&frame_duration, &time_scale) != S_OK) {
613 time_scale = 0;
614 frame_duration = 1;
617 msg_Dbg(demux, "Found mode '%4.4s': %s (%dx%d, %.3f fps%s)",
618 (char*)&id, mode_name,
619 (int)m->GetWidth(), (int)m->GetHeight(),
620 double(time_scale) / frame_duration, field);
622 if (u.id == id) {
623 sys->video_fmt = GetModeSettings(demux, m, bmdDetectedVideoInputYCbCr422);
624 msg_Dbg(demux, "Using that mode");
628 mode_it->Release();
630 if (sys->video_fmt.video.i_width == 0) {
631 msg_Err(demux, "Unknown video mode `%4.4s\' specified.", (char*)&u.id);
632 goto finish;
635 if (sys->input->EnableVideoInput(htonl(u.id), fmt, flags) != S_OK) {
636 msg_Err(demux, "Failed to enable video input");
637 goto finish;
640 /* Set up audio. */
641 sys->channels = var_InheritInteger(demux, "decklink-audio-channels");
642 switch (sys->channels) {
643 case 0:
644 break;
645 case 2:
646 physical_channels = AOUT_CHANS_STEREO;
647 break;
648 case 8:
649 physical_channels = AOUT_CHANS_7_1;
650 break;
651 //case 16:
652 default:
653 msg_Err(demux, "Invalid number of channels (%d), disabling audio", sys->channels);
654 sys->channels = 0;
656 rate = var_InheritInteger(demux, "decklink-audio-rate");
657 if (rate > 0 && sys->channels > 0) {
658 if (sys->input->EnableAudioInput(rate, bmdAudioSampleType16bitInteger, sys->channels) != S_OK) {
659 msg_Err(demux, "Failed to enable audio input");
660 goto finish;
664 sys->delegate = new DeckLinkCaptureDelegate(demux);
665 sys->input->SetCallback(sys->delegate);
667 if (sys->input->StartStreams() != S_OK) {
668 msg_Err(demux, "Could not start streaming from SDI card. This could be caused "
669 "by invalid video mode or flags, access denied, or card already in use.");
670 goto finish;
673 msg_Dbg(demux, "added new video es %4.4s %dx%d",
674 (char*)&sys->video_fmt.i_codec, sys->video_fmt.video.i_width, sys->video_fmt.video.i_height);
675 sys->video_es = es_out_Add(demux->out, &sys->video_fmt);
677 es_format_t audio_fmt;
678 es_format_Init(&audio_fmt, AUDIO_ES, VLC_CODEC_S16N);
679 audio_fmt.audio.i_channels = sys->channels;
680 audio_fmt.audio.i_physical_channels = physical_channels;
681 audio_fmt.audio.i_rate = rate;
682 audio_fmt.audio.i_bitspersample = 16;
683 audio_fmt.audio.i_blockalign = audio_fmt.audio.i_channels * audio_fmt.audio.i_bitspersample / 8;
684 audio_fmt.i_bitrate = audio_fmt.audio.i_channels * audio_fmt.audio.i_rate * audio_fmt.audio.i_bitspersample;
686 msg_Dbg(demux, "added new audio es %4.4s %dHz %dbpp %dch",
687 (char*)&audio_fmt.i_codec, audio_fmt.audio.i_rate, audio_fmt.audio.i_bitspersample, audio_fmt.audio.i_channels);
688 sys->audio_es = es_out_Add(demux->out, &audio_fmt);
690 ret = VLC_SUCCESS;
692 finish:
693 if (decklink_iterator)
694 decklink_iterator->Release();
696 if (ret != VLC_SUCCESS)
697 Close(p_this);
699 return ret;
702 static void Close(vlc_object_t *p_this)
704 demux_t *demux = (demux_t *)p_this;
705 demux_sys_t *sys = demux->p_sys;
707 if (sys->attributes)
708 sys->attributes->Release();
710 if (sys->config)
711 sys->config->Release();
713 if (sys->input) {
714 sys->input->StopStreams();
715 sys->input->Release();
718 if (sys->card)
719 sys->card->Release();
721 if (sys->delegate)
722 sys->delegate->Release();
724 vlc_mutex_destroy(&sys->pts_lock);
725 free(sys);
728 static int Control(demux_t *demux, int query, va_list args)
730 demux_sys_t *sys = demux->p_sys;
731 bool *pb;
732 int64_t *pi64;
734 switch(query)
736 /* Special for access_demux */
737 case DEMUX_CAN_PAUSE:
738 case DEMUX_CAN_SEEK:
739 case DEMUX_CAN_CONTROL_PACE:
740 pb = va_arg(args, bool *);
741 *pb = false;
742 return VLC_SUCCESS;
744 case DEMUX_GET_PTS_DELAY:
745 pi64 = va_arg(args, int64_t *);
746 *pi64 = INT64_C(1000) * var_InheritInteger(demux, "live-caching");
747 return VLC_SUCCESS;
749 case DEMUX_GET_TIME:
750 pi64 = va_arg(args, int64_t *);
751 vlc_mutex_lock(&sys->pts_lock);
752 *pi64 = sys->last_pts;
753 vlc_mutex_unlock(&sys->pts_lock);
754 return VLC_SUCCESS;
756 default:
757 return VLC_EGENERIC;