]> git.sesse.net Git - vlc/blob - modules/access/sdi.cpp
Set i_flags correctly in the video frames.
[vlc] / modules / access / sdi.cpp
1 /* BlackMagic SDI driver */
2
3 #ifdef HAVE_CONFIG_H
4 # include "config.h"
5 #endif
6
7 #ifndef INT64_C
8 #define INT64_C(c) c ## LL
9 #endif
10
11 #include <vlc_common.h>
12 #include <vlc_plugin.h>
13 #include <vlc_input.h>
14 #include <vlc_demux.h>
15 #include <vlc_access.h>
16 #include <vlc_picture.h>
17 #include <vlc_charset.h>
18 #include <vlc_fs.h>
19
20 #include "DeckLinkAPI.h"
21 #include "DeckLinkAPIDispatch.cpp"
22
23 static int  Open ( vlc_object_t * );
24 static void Close( vlc_object_t * );
25
26 #define CACHING_TEXT N_("Caching value in ms")
27 #define CACHING_LONGTEXT N_( \
28     "Caching value for SDI captures. This " \
29     "value should be set in milliseconds." )
30
31 vlc_module_begin ()
32     set_shortname( N_("SDI") )
33     set_description( N_("BlackMagic SDI input") )
34     set_category( CAT_INPUT )
35     set_subcategory( SUBCAT_INPUT_ACCESS )
36
37     add_integer( "sdi-caching", DEFAULT_PTS_DELAY / 1000, NULL,
38                  CACHING_TEXT, CACHING_LONGTEXT, true )
39
40     add_shortcut( "sdi" )
41     set_capability( "access_demux", 10 )
42     set_callbacks( Open, Close )
43 vlc_module_end ()
44
45 static int Demux  ( demux_t * );
46 static int Control( demux_t *, int, va_list );
47
48 class DeckLinkCaptureDelegate;
49
50 struct demux_sys_t
51 {
52     IDeckLink *p_card;
53     IDeckLinkInput *p_input;
54     DeckLinkCaptureDelegate *p_delegate;
55     es_out_id_t *p_video_es;
56     es_out_id_t *p_audio_es;
57     bool b_first_frame;
58
59     vlc_mutex_t frame_lock;
60     block_t *p_video_frame;  // protected by <frame_lock>
61     block_t *p_audio_frame;  // protected by <frame_lock>
62     vlc_cond_t has_frame;  // related to <frame_lock>
63 };
64
65 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
66 {
67 public:
68     DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux) {}
69
70     // FIXME: These leak.
71     virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
72     virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
73     virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
74
75     virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
76     virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
77
78 private:
79     demux_t *p_demux_;
80 };
81
82 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
83 {
84     msg_Dbg( p_demux_, "Video input format changed" );    
85     return S_OK;
86 }
87
88 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
89 {
90     demux_sys_t *p_sys = p_demux_->p_sys;
91     block_t *p_video_frame = NULL;
92     block_t *p_audio_frame = NULL;
93
94     if( videoFrame )
95     {
96         if( videoFrame->GetFlags() & bmdFrameHasNoInputSource )
97         {
98             msg_Warn( p_demux_, "No input signal detected" );
99             return S_OK;
100         }
101
102         const int i_width = videoFrame->GetWidth();
103         const int i_height = videoFrame->GetHeight();
104         const int i_stride = videoFrame->GetRowBytes();
105         const int i_bpp = 2;
106
107         p_video_frame = block_New( p_demux_, i_width * i_height * i_bpp );
108         if( !p_video_frame )
109         {
110             msg_Err( p_demux_, "Could not allocate memory for video frame" );
111             return S_OK;
112         }
113
114         void *frame_bytes;
115         videoFrame->GetBytes( &frame_bytes );
116         for( int y = 0; y < i_height; ++y )
117         {
118             const uint8_t *src = (const uint8_t *)frame_bytes + i_stride * y;
119             uint8_t *dst = (uint8_t *)p_video_frame->p_buffer + i_width * i_bpp * y;
120             memcpy( dst, src, i_width * i_bpp );
121         }
122
123         BMDTimeValue stream_time, frame_duration;
124         videoFrame->GetStreamTime( &stream_time, &frame_duration, CLOCK_FREQ );
125         p_video_frame->i_flags = BLOCK_FLAG_TYPE_I;
126         if( p_sys->b_first_frame )
127         {
128             p_video_frame->i_flags |= BLOCK_FLAG_DISCONTINUITY;
129             p_sys->b_first_frame = false;
130         }
131         p_video_frame->i_pts = VLC_TS_0 + stream_time;
132     }
133     
134     if( audioFrame )
135     {
136         const int i_bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * 2;
137
138         p_audio_frame = block_New( p_demux_, i_bytes );
139         if( !p_audio_frame )
140         {
141             msg_Err( p_demux_, "Could not allocate memory for audio frame" );
142             return S_OK;
143         }
144
145         void *frame_bytes;
146         audioFrame->GetBytes( &frame_bytes );
147         memcpy( p_audio_frame->p_buffer, frame_bytes, i_bytes );
148
149         BMDTimeValue packet_time;
150         audioFrame->GetPacketTime( &packet_time, CLOCK_FREQ );
151         p_audio_frame->i_pts = VLC_TS_0 + packet_time;
152     }
153
154     if( p_video_frame || p_audio_frame )
155     {
156         vlc_mutex_lock( &p_sys->frame_lock );
157         if( p_video_frame )
158             p_sys->p_video_frame = p_video_frame;  // FIXME: leak
159         if( p_audio_frame )
160             p_sys->p_audio_frame = p_audio_frame;  // FIXME: leak
161         vlc_cond_signal( &p_sys->has_frame );
162         vlc_mutex_unlock( &p_sys->frame_lock );
163     }
164
165     return S_OK;
166 }
167
168 static int Open( vlc_object_t *p_this )
169 {
170     demux_t     *p_demux = (demux_t*)p_this;
171     demux_sys_t *p_sys;
172
173     /* Only when selected */
174     if( *p_demux->psz_access == '\0' )
175         return VLC_EGENERIC;
176
177     /* Set up p_demux */
178     p_demux->pf_demux = Demux;
179     p_demux->pf_control = Control;
180     p_demux->info.i_update = 0;
181     p_demux->info.i_title = 0;
182     p_demux->info.i_seekpoint = 0;
183     p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
184     if( !p_sys )
185         return VLC_ENOMEM;
186
187     vlc_mutex_init( &p_sys->frame_lock );
188     vlc_cond_init( &p_sys->has_frame );
189     p_sys->p_video_frame = NULL;
190
191     IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
192     if( !decklink_iterator )
193     {
194         msg_Err( p_demux, "DeckLink drivers not found." );
195         // FIXME: Leak here and several other error paths.
196         return VLC_EGENERIC;
197     }
198
199     HRESULT result;
200     result = decklink_iterator->Next( &p_sys->p_card );
201
202     if( result != S_OK )
203     {
204         msg_Err( p_demux, "No DeckLink PCI cards found" );
205         return VLC_EGENERIC;
206     }
207
208     if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
209     {
210         msg_Err( p_demux, "Card has no inputs" );
211         return VLC_EGENERIC;
212     }
213
214     result = p_sys->p_input->EnableVideoInput( bmdModePAL, bmdFormat8BitYUV, 0 );
215     if( result != S_OK )
216     {
217         msg_Err( p_demux, "Failed to enable video input" );
218         return VLC_EGENERIC;
219     }
220     
221     result = p_sys->p_input->EnableAudioInput( 48000, bmdAudioSampleType16bitInteger, 2 );
222     if( result != S_OK )
223     {
224         msg_Err( p_demux, "Failed to enable audio input" );
225         return VLC_EGENERIC;
226     }
227     
228     p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
229     p_sys->p_input->SetCallback( p_sys->p_delegate );
230
231     result = p_sys->p_input->StartStreams();
232     if( result != S_OK )
233     {
234         msg_Err( p_demux, "Failed to start streams" );
235         return VLC_EGENERIC;
236     }
237
238     /* Declare elementary streams */
239     es_format_t video_fmt;
240     es_format_Init( &video_fmt, VIDEO_ES, VLC_CODEC_UYVY );
241     video_fmt.video.i_width = 720;
242     video_fmt.video.i_height = 576;
243     video_fmt.video.i_sar_num = 16 * video_fmt.video.i_height;
244     video_fmt.video.i_sar_den = 9 * video_fmt.video.i_width;
245     video_fmt.video.i_frame_rate = 25;
246     video_fmt.video.i_frame_rate_base = 1;
247     video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2;
248
249     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
250              (char*)&video_fmt.i_codec, video_fmt.video.i_width, video_fmt.video.i_height );
251     p_sys->p_video_es = es_out_Add( p_demux->out, &video_fmt );
252     
253     es_format_t audio_fmt;
254     es_format_Init( &audio_fmt, AUDIO_ES, VLC_CODEC_S16N );
255     audio_fmt.audio.i_channels = 2;
256     audio_fmt.audio.i_rate = 48000;
257     audio_fmt.audio.i_bitspersample = 16;
258     audio_fmt.audio.i_blockalign = audio_fmt.audio.i_channels * audio_fmt.audio.i_bitspersample / 8;
259     audio_fmt.i_bitrate = audio_fmt.audio.i_channels * audio_fmt.audio.i_rate * audio_fmt.audio.i_bitspersample;
260
261     msg_Dbg( p_demux, "added new audio es %4.4s %dHz %dbpp %dch",
262              (char*)&audio_fmt.i_codec, audio_fmt.audio.i_rate, audio_fmt.audio.i_bitspersample, audio_fmt.audio.i_channels);
263     p_sys->p_audio_es = es_out_Add( p_demux->out, &audio_fmt );
264
265     p_sys->b_first_frame = true;
266
267     /* Update default_pts to a suitable value for access */
268     var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
269
270     return VLC_SUCCESS;
271 }
272
273 static void Close( vlc_object_t *p_this )
274 {
275     demux_t     *p_demux = (demux_t *)p_this;
276     demux_sys_t *p_sys   = p_demux->p_sys;
277
278     free( p_sys );
279 }
280
281 static int Control( demux_t *p_demux, int i_query, va_list args )
282 {
283     bool *pb;
284     int64_t    *pi64;
285
286     switch( i_query )
287     {
288         /* Special for access_demux */
289         case DEMUX_CAN_PAUSE:
290         case DEMUX_CAN_SEEK:
291         case DEMUX_CAN_CONTROL_PACE:
292             pb = (bool*)va_arg( args, bool * );
293             *pb = false;
294             return VLC_SUCCESS;
295
296         case DEMUX_GET_PTS_DELAY:
297             pi64 = (int64_t*)va_arg( args, int64_t * );
298             *pi64 = var_GetInteger( p_demux, "sdi-caching" ) * 1000;
299             return VLC_SUCCESS;
300
301         case DEMUX_GET_TIME:
302             pi64 = (int64_t*)va_arg( args, int64_t * );
303             *pi64 = mdate();  // FIXME
304             return VLC_SUCCESS;
305
306         /* TODO implement others */
307         default:
308             return VLC_EGENERIC;
309     }
310
311     return VLC_EGENERIC;
312 }
313
314 static int Demux( demux_t *p_demux )
315 {
316     demux_sys_t *p_sys = p_demux->p_sys;
317     block_t *p_video_block = NULL;
318     block_t *p_audio_block = NULL;
319
320     vlc_mutex_lock( &p_sys->frame_lock );
321
322     while( !p_sys->p_video_frame && !p_sys->p_audio_frame )
323         vlc_cond_wait( &p_sys->has_frame, &p_sys->frame_lock );
324
325     p_video_block = p_sys->p_video_frame;
326     p_sys->p_video_frame = NULL;
327
328     p_audio_block = p_sys->p_audio_frame;
329     p_sys->p_audio_frame = NULL;
330
331     vlc_mutex_unlock( &p_sys->frame_lock );
332
333     if( p_video_block )
334     {
335         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_video_block->i_pts );
336         es_out_Send( p_demux->out, p_sys->p_video_es, p_video_block );
337     }
338     
339     if( p_audio_block )
340     {
341         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_audio_block->i_pts );
342         es_out_Send( p_demux->out, p_sys->p_audio_es, p_audio_block );
343     }
344
345     return 1;
346 }
347