]> git.sesse.net Git - vlc/blob - modules/access/sdi.cpp
Replace hardcoded memcpy with a more correct, parametrized one.
[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_es;
56
57     vlc_mutex_t frame_lock;
58     block_t *p_frame;  // protected by <frame_lock>
59     vlc_cond_t has_frame;  // related to <frame_lock>
60 };
61
62 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
63 {
64 public:
65     DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux) {}
66
67     // FIXME: These leak.
68     virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
69     virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; }
70     virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; }
71
72     virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags);
73     virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
74
75 private:
76     demux_t *p_demux_;
77 };
78
79 HRESULT DeckLinkCaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags)
80 {
81     msg_Dbg( p_demux_, "Video input format changed" );    
82     return S_OK;
83 }
84
85 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
86 {
87     demux_sys_t *p_sys = p_demux_->p_sys;
88
89     if(videoFrame)
90     {
91         if(videoFrame->GetFlags() & bmdFrameHasNoInputSource)
92         {
93             msg_Warn( p_demux_, "No input signal detected" );
94             return S_OK;
95         }
96
97         const int i_width = videoFrame->GetWidth();
98         const int i_height = videoFrame->GetHeight();
99         const int i_stride = videoFrame->GetRowBytes();
100         const int i_bpp = 2;
101
102         block_t *p_frame;
103         p_frame = block_New( p_demux_, i_width * i_height * i_bpp );
104         if( !p_frame )
105         {
106             msg_Err( p_demux_, "Could not allocate memory for frame" );
107             return S_OK;
108         }
109
110         void *frame_bytes;
111         videoFrame->GetBytes( &frame_bytes );
112         for( int y = 0; y < i_height; ++y )
113         {
114             const uint8_t *src = (const uint8_t *)frame_bytes + i_stride * y;
115             uint8_t *dst = (uint8_t *)p_frame->p_buffer + i_width * i_bpp * y;
116             memcpy( dst, src, i_width * i_bpp );
117         }
118
119         BMDTimeValue stream_time, frame_duration;
120         videoFrame->GetStreamTime( &stream_time, &frame_duration, CLOCK_FREQ );
121         p_frame->i_pts = VLC_TS_0 + stream_time;
122
123         vlc_mutex_lock( &p_sys->frame_lock );
124         p_sys->p_frame = p_frame;  // FIXME: leak
125         vlc_cond_signal( &p_sys->has_frame );
126         vlc_mutex_unlock( &p_sys->frame_lock );
127     }
128
129     return S_OK;
130 }
131
132 static int Open( vlc_object_t *p_this )
133 {
134     demux_t     *p_demux = (demux_t*)p_this;
135     demux_sys_t *p_sys;
136
137     /* Only when selected */
138     if( *p_demux->psz_access == '\0' )
139         return VLC_EGENERIC;
140
141     /* Set up p_demux */
142     p_demux->pf_demux = Demux;
143     p_demux->pf_control = Control;
144     p_demux->info.i_update = 0;
145     p_demux->info.i_title = 0;
146     p_demux->info.i_seekpoint = 0;
147     p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
148     if( !p_sys )
149         return VLC_ENOMEM;
150
151     vlc_mutex_init( &p_sys->frame_lock );
152     vlc_cond_init( &p_sys->has_frame );
153     p_sys->p_frame = NULL;
154
155     IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
156     if( !decklink_iterator )
157     {
158         msg_Err( p_demux, "DeckLink drivers not found." );
159         // FIXME: Leak here and several other error paths.
160         return VLC_EGENERIC;
161     }
162
163     HRESULT result;
164     result = decklink_iterator->Next( &p_sys->p_card );
165
166     if( result != S_OK )
167     {
168         msg_Err( p_demux, "No DeckLink PCI cards found" );
169         return VLC_EGENERIC;
170     }
171
172     if( p_sys->p_card->QueryInterface(IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
173     {
174         msg_Err( p_demux, "Card has no inputs" );
175         return VLC_EGENERIC;
176     }
177
178     p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
179     p_sys->p_input->SetCallback( p_sys->p_delegate );
180
181     result = p_sys->p_input->EnableVideoInput(bmdModePAL, bmdFormat8BitYUV, 0);
182     if( result != S_OK )
183     {
184         msg_Err( p_demux, "Failed to enable video input" );
185         return VLC_EGENERIC;
186     }
187
188     // FIXME: add audio
189     result = p_sys->p_input->StartStreams();
190     if( result != S_OK )
191     {
192         msg_Err( p_demux, "Failed to start streams" );
193         return VLC_EGENERIC;
194     }
195
196     /*eDeclare elementary streams */
197     es_format_t fmt;
198     es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_UYVY );
199     fmt.video.i_width = 720;
200     fmt.video.i_height = 576;
201     fmt.video.i_sar_num = 16 * fmt.video.i_height;
202     fmt.video.i_sar_den = 9 * fmt.video.i_width;
203     fmt.video.i_frame_rate = 25;
204     fmt.video.i_frame_rate_base = 1;
205
206     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
207              (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
208     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
209
210     /* Update default_pts to a suitable value for access */
211     var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
212
213     return VLC_SUCCESS;
214 }
215
216 static void Close( vlc_object_t *p_this )
217 {
218     demux_t     *p_demux = (demux_t *)p_this;
219     demux_sys_t *p_sys   = p_demux->p_sys;
220
221     free( p_sys );
222 }
223
224 static int Control( demux_t *p_demux, int i_query, va_list args )
225 {
226     bool *pb;
227     int64_t    *pi64;
228
229     switch( i_query )
230     {
231         /* Special for access_demux */
232         case DEMUX_CAN_PAUSE:
233         case DEMUX_CAN_SEEK:
234         case DEMUX_CAN_CONTROL_PACE:
235             pb = (bool*)va_arg( args, bool * );
236             *pb = false;
237             return VLC_SUCCESS;
238
239         case DEMUX_GET_PTS_DELAY:
240             pi64 = (int64_t*)va_arg( args, int64_t * );
241             *pi64 = var_GetInteger( p_demux, "sdi-caching" ) * 1000;
242             return VLC_SUCCESS;
243
244         case DEMUX_GET_TIME:
245             pi64 = (int64_t*)va_arg( args, int64_t * );
246             *pi64 = mdate();  // FIXME
247             return VLC_SUCCESS;
248
249         /* TODO implement others */
250         default:
251             return VLC_EGENERIC;
252     }
253
254     return VLC_EGENERIC;
255 }
256
257 static int Demux( demux_t *p_demux )
258 {
259     demux_sys_t *p_sys = p_demux->p_sys;
260     block_t *p_block;
261
262     vlc_mutex_lock( &p_sys->frame_lock );
263
264     while( !p_sys->p_frame )
265         vlc_cond_wait( &p_sys->has_frame, &p_sys->frame_lock );
266
267     p_block = p_sys->p_frame;
268     p_sys->p_frame = NULL;
269
270     vlc_mutex_unlock( &p_sys->frame_lock );
271
272     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
273     es_out_Send( p_demux->out, p_sys->p_es, p_block );
274
275     return 1;
276 }
277