direct3d11: only do the SwapChain Present() during Display
[vlc.git] / modules / stream_out / smem.c
blob43ad651fd123e4e807e772c90239926a84209f80
1 /*****************************************************************************
2 * smem.c: stream output to memory buffer module
3 *****************************************************************************
4 * Copyright (C) 2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Christophe Courtaut <christophe.courtaut@gmail.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * How to use it
26 *****************************************************************************
28 * You should use this module in combination with the transcode module, to get
29 * raw datas from it. This module does not make any conversion at all, so you
30 * need to use the transcode module for this purpose.
32 * For example, you can use smem as it :
33 * --sout="#transcode{vcodec=RV24,acodec=s16l}:smem{smem-options}"
35 * Into each lock function (audio and video), you will have all the information
36 * you need to allocate a buffer, so that this module will copy data in it.
38 * the video-data and audio-data pointers will be passed to lock/unlock function
40 ******************************************************************************/
42 /*****************************************************************************
43 * Preamble
44 *****************************************************************************/
46 #ifdef HAVE_CONFIG_H
47 # include "config.h"
48 #endif
50 #include <vlc_common.h>
51 #include <vlc_plugin.h>
52 #include <vlc_sout.h>
53 #include <vlc_block.h>
54 #include <vlc_codec.h>
55 #include <vlc_aout.h>
57 /*****************************************************************************
58 * Module descriptor
59 *****************************************************************************/
61 #define T_VIDEO_PRERENDER_CALLBACK N_( "Video prerender callback" )
62 #define LT_VIDEO_PRERENDER_CALLBACK N_( "Address of the video prerender callback function. " \
63 "This function will set the buffer where render will be done." )
65 #define T_AUDIO_PRERENDER_CALLBACK N_( "Audio prerender callback" )
66 #define LT_AUDIO_PRERENDER_CALLBACK N_( "Address of the audio prerender callback function. " \
67 "This function will set the buffer where render will be done." )
69 #define T_VIDEO_POSTRENDER_CALLBACK N_( "Video postrender callback" )
70 #define LT_VIDEO_POSTRENDER_CALLBACK N_( "Address of the video postrender callback function. " \
71 "This function will be called when the render is into the buffer." )
73 #define T_AUDIO_POSTRENDER_CALLBACK N_( "Audio postrender callback" )
74 #define LT_AUDIO_POSTRENDER_CALLBACK N_( "Address of the audio postrender callback function. " \
75 "This function will be called when the render is into the buffer." )
77 #define T_VIDEO_DATA N_( "Video Callback data" )
78 #define LT_VIDEO_DATA N_( "Data for the video callback function." )
80 #define T_AUDIO_DATA N_( "Audio callback data" )
81 #define LT_AUDIO_DATA N_( "Data for the audio callback function." )
83 #define T_TIME_SYNC N_( "Time Synchronized output" )
84 #define LT_TIME_SYNC N_( "Time Synchronisation option for output. " \
85 "If true, stream will render as usual, else " \
86 "it will be rendered as fast as possible.")
88 static int Open ( vlc_object_t * );
89 static void Close( vlc_object_t * );
91 #define SOUT_CFG_PREFIX "sout-smem-"
92 #define SOUT_PREFIX_VIDEO SOUT_CFG_PREFIX"video-"
93 #define SOUT_PREFIX_AUDIO SOUT_CFG_PREFIX"audio-"
95 vlc_module_begin ()
96 set_shortname( N_("Smem"))
97 set_description( N_("Stream output to memory buffer") )
98 set_capability( "sout stream", 0 )
99 add_shortcut( "smem" )
100 set_category( CAT_SOUT )
101 set_subcategory( SUBCAT_SOUT_STREAM )
102 add_string( SOUT_PREFIX_VIDEO "prerender-callback", "0", T_VIDEO_PRERENDER_CALLBACK, LT_VIDEO_PRERENDER_CALLBACK, true )
103 change_volatile()
104 add_string( SOUT_PREFIX_AUDIO "prerender-callback", "0", T_AUDIO_PRERENDER_CALLBACK, LT_AUDIO_PRERENDER_CALLBACK, true )
105 change_volatile()
106 add_string( SOUT_PREFIX_VIDEO "postrender-callback", "0", T_VIDEO_POSTRENDER_CALLBACK, LT_VIDEO_POSTRENDER_CALLBACK, true )
107 change_volatile()
108 add_string( SOUT_PREFIX_AUDIO "postrender-callback", "0", T_AUDIO_POSTRENDER_CALLBACK, LT_AUDIO_POSTRENDER_CALLBACK, true )
109 change_volatile()
110 add_string( SOUT_PREFIX_VIDEO "data", "0", T_VIDEO_DATA, LT_VIDEO_DATA, true )
111 change_volatile()
112 add_string( SOUT_PREFIX_AUDIO "data", "0", T_AUDIO_DATA, LT_VIDEO_DATA, true )
113 change_volatile()
114 add_bool( SOUT_CFG_PREFIX "time-sync", true, T_TIME_SYNC, LT_TIME_SYNC, true )
115 change_private()
116 set_callbacks( Open, Close )
117 vlc_module_end ()
120 /*****************************************************************************
121 * Exported prototypes
122 *****************************************************************************/
123 static const char *const ppsz_sout_options[] = {
124 "video-prerender-callback", "audio-prerender-callback",
125 "video-postrender-callback", "audio-postrender-callback", "video-data", "audio-data", "time-sync", NULL
128 static sout_stream_id_sys_t *Add( sout_stream_t *, const es_format_t * );
129 static void Del ( sout_stream_t *, sout_stream_id_sys_t * );
130 static int Send( sout_stream_t *, sout_stream_id_sys_t *, block_t* );
132 static sout_stream_id_sys_t *AddVideo( sout_stream_t *p_stream,
133 const es_format_t *p_fmt );
134 static sout_stream_id_sys_t *AddAudio( sout_stream_t *p_stream,
135 const es_format_t *p_fmt );
137 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
138 block_t *p_buffer );
139 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
140 block_t *p_buffer );
142 struct sout_stream_id_sys_t
144 es_format_t format;
145 void *p_data;
148 struct sout_stream_sys_t
150 vlc_mutex_t *p_lock;
151 void ( *pf_video_prerender_callback ) ( void* p_video_data, uint8_t** pp_pixel_buffer, size_t size );
152 void ( *pf_audio_prerender_callback ) ( void* p_audio_data, uint8_t** pp_pcm_buffer, size_t size );
153 void ( *pf_video_postrender_callback ) ( void* p_video_data, uint8_t* p_pixel_buffer, int width, int height, int pixel_pitch, size_t size, mtime_t pts );
154 void ( *pf_audio_postrender_callback ) ( void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels, unsigned int rate, unsigned int nb_samples, unsigned int bits_per_sample, size_t size, mtime_t pts );
155 bool time_sync;
158 void VideoPrerenderDefaultCallback( void* p_video_data, uint8_t** pp_pixel_buffer, size_t size );
159 void AudioPrerenderDefaultCallback( void* p_audio_data, uint8_t** pp_pcm_buffer, size_t size );
160 void VideoPostrenderDefaultCallback( void* p_video_data, uint8_t* p_pixel_buffer, int width, int height,
161 int pixel_pitch, size_t size, mtime_t pts );
162 void AudioPostrenderDefaultCallback( void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels,
163 unsigned int rate, unsigned int nb_samples, unsigned int bits_per_sample,
164 size_t size, mtime_t pts );
166 /*****************************************************************************
167 * Default empty callbacks
168 *****************************************************************************/
170 void VideoPrerenderDefaultCallback( void* p_video_data, uint8_t** pp_pixel_buffer, size_t size )
172 VLC_UNUSED( p_video_data ); VLC_UNUSED( pp_pixel_buffer ); VLC_UNUSED( size );
175 void AudioPrerenderDefaultCallback( void* p_audio_data, uint8_t** pp_pcm_buffer, size_t size )
177 VLC_UNUSED( p_audio_data ); VLC_UNUSED( pp_pcm_buffer ); VLC_UNUSED( size );
180 void VideoPostrenderDefaultCallback( void* p_video_data, uint8_t* p_pixel_buffer, int width, int height,
181 int pixel_pitch, size_t size, mtime_t pts )
183 VLC_UNUSED( p_video_data ); VLC_UNUSED( p_pixel_buffer );
184 VLC_UNUSED( width ); VLC_UNUSED( height );
185 VLC_UNUSED( pixel_pitch ); VLC_UNUSED( size ); VLC_UNUSED( pts );
188 void AudioPostrenderDefaultCallback( void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels,
189 unsigned int rate, unsigned int nb_samples, unsigned int bits_per_sample,
190 size_t size, mtime_t pts )
192 VLC_UNUSED( p_audio_data ); VLC_UNUSED( p_pcm_buffer );
193 VLC_UNUSED( channels ); VLC_UNUSED( rate ); VLC_UNUSED( nb_samples );
194 VLC_UNUSED( bits_per_sample ); VLC_UNUSED( size ); VLC_UNUSED( pts );
197 /*****************************************************************************
198 * Open:
199 *****************************************************************************/
200 static int Open( vlc_object_t *p_this )
202 char* psz_tmp;
203 sout_stream_t *p_stream = (sout_stream_t*)p_this;
204 sout_stream_sys_t *p_sys;
206 p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
207 if( !p_sys )
208 return VLC_ENOMEM;
209 p_stream->p_sys = p_sys;
211 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
212 p_stream->p_cfg );
214 p_sys->time_sync = var_GetBool( p_stream, SOUT_CFG_PREFIX "time-sync" );
216 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "prerender-callback" );
217 p_sys->pf_video_prerender_callback = (void (*) (void *, uint8_t**, size_t))(intptr_t)atoll( psz_tmp );
218 free( psz_tmp );
219 if (p_sys->pf_video_prerender_callback == NULL)
220 p_sys->pf_video_prerender_callback = VideoPrerenderDefaultCallback;
222 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "prerender-callback" );
223 p_sys->pf_audio_prerender_callback = (void (*) (void* , uint8_t**, size_t))(intptr_t)atoll( psz_tmp );
224 free( psz_tmp );
225 if (p_sys->pf_audio_prerender_callback == NULL)
226 p_sys->pf_audio_prerender_callback = AudioPrerenderDefaultCallback;
228 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "postrender-callback" );
229 p_sys->pf_video_postrender_callback = (void (*) (void*, uint8_t*, int, int, int, size_t, mtime_t))(intptr_t)atoll( psz_tmp );
230 free( psz_tmp );
231 if (p_sys->pf_video_postrender_callback == NULL)
232 p_sys->pf_video_postrender_callback = VideoPostrenderDefaultCallback;
234 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "postrender-callback" );
235 p_sys->pf_audio_postrender_callback = (void (*) (void*, uint8_t*, unsigned int, unsigned int, unsigned int, unsigned int, size_t, mtime_t))(intptr_t)atoll( psz_tmp );
236 free( psz_tmp );
237 if (p_sys->pf_audio_postrender_callback == NULL)
238 p_sys->pf_audio_postrender_callback = AudioPostrenderDefaultCallback;
240 /* Setting stream out module callbacks */
241 p_stream->pf_add = Add;
242 p_stream->pf_del = Del;
243 p_stream->pf_send = Send;
244 p_stream->pace_nocontrol = p_sys->time_sync;
246 return VLC_SUCCESS;
249 /*****************************************************************************
250 * Close:
251 *****************************************************************************/
252 static void Close( vlc_object_t * p_this )
254 sout_stream_t *p_stream = (sout_stream_t*)p_this;
255 free( p_stream->p_sys );
258 static sout_stream_id_sys_t *Add( sout_stream_t *p_stream,
259 const es_format_t *p_fmt )
261 sout_stream_id_sys_t *id = NULL;
263 if ( p_fmt->i_cat == VIDEO_ES )
264 id = AddVideo( p_stream, p_fmt );
265 else if ( p_fmt->i_cat == AUDIO_ES )
266 id = AddAudio( p_stream, p_fmt );
267 return id;
270 static sout_stream_id_sys_t *AddVideo( sout_stream_t *p_stream,
271 const es_format_t *p_fmt )
273 char* psz_tmp;
274 sout_stream_id_sys_t *id;
275 int i_bits_per_pixel;
277 switch( p_fmt->i_codec )
279 case VLC_CODEC_RGB32:
280 case VLC_CODEC_RGBA:
281 case VLC_CODEC_ARGB:
282 i_bits_per_pixel = 32;
283 break;
284 case VLC_CODEC_I444:
285 case VLC_CODEC_RGB24:
286 i_bits_per_pixel = 24;
287 break;
288 case VLC_CODEC_RGB16:
289 case VLC_CODEC_RGB15:
290 case VLC_CODEC_RGB8:
291 case VLC_CODEC_I422:
292 i_bits_per_pixel = 16;
293 break;
294 case VLC_CODEC_YV12:
295 case VLC_CODEC_I420:
296 i_bits_per_pixel = 12;
297 break;
298 case VLC_CODEC_RGBP:
299 i_bits_per_pixel = 8;
300 break;
301 default:
302 i_bits_per_pixel = 0;
303 msg_Dbg( p_stream, "non raw video format detected (%4.4s), buffers will contain compressed video", (char *)&p_fmt->i_codec );
304 break;
307 id = calloc( 1, sizeof( sout_stream_id_sys_t ) );
308 if( !id )
309 return NULL;
311 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "data" );
312 id->p_data = (void *)( intptr_t )atoll( psz_tmp );
313 free( psz_tmp );
315 es_format_Copy( &id->format, p_fmt );
316 id->format.video.i_bits_per_pixel = i_bits_per_pixel;
317 return id;
320 static sout_stream_id_sys_t *AddAudio( sout_stream_t *p_stream,
321 const es_format_t *p_fmt )
323 char* psz_tmp;
324 sout_stream_id_sys_t* id;
325 int i_bits_per_sample = aout_BitsPerSample( p_fmt->i_codec );
327 if( !i_bits_per_sample )
329 msg_Err( p_stream, "Smem does only support raw audio format" );
330 return NULL;
333 id = calloc( 1, sizeof( sout_stream_id_sys_t ) );
334 if( !id )
335 return NULL;
337 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "data" );
338 id->p_data = (void *)( intptr_t )atoll( psz_tmp );
339 free( psz_tmp );
341 es_format_Copy( &id->format, p_fmt );
342 id->format.audio.i_bitspersample = i_bits_per_sample;
343 return id;
346 static void Del( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
348 VLC_UNUSED( p_stream );
349 es_format_Clean( &id->format );
350 free( id );
353 static int Send( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
354 block_t *p_buffer )
356 if ( id->format.i_cat == VIDEO_ES )
357 return SendVideo( p_stream, id, p_buffer );
358 else if ( id->format.i_cat == AUDIO_ES )
359 return SendAudio( p_stream, id, p_buffer );
360 return VLC_SUCCESS;
363 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
364 block_t *p_buffer )
366 sout_stream_sys_t *p_sys = p_stream->p_sys;
367 size_t i_size = p_buffer->i_buffer;
368 uint8_t* p_pixels = NULL;
370 /* Calling the prerender callback to get user buffer */
371 p_sys->pf_video_prerender_callback( id->p_data, &p_pixels, i_size );
373 if (!p_pixels)
375 msg_Err( p_stream, "No buffer given!" );
376 block_ChainRelease( p_buffer );
377 return VLC_EGENERIC;
380 /* Copying data into user buffer */
381 memcpy( p_pixels, p_buffer->p_buffer, i_size );
382 /* Calling the postrender callback to tell the user his buffer is ready */
383 p_sys->pf_video_postrender_callback( id->p_data, p_pixels,
384 id->format.video.i_width, id->format.video.i_height,
385 id->format.video.i_bits_per_pixel, i_size, p_buffer->i_pts );
386 block_ChainRelease( p_buffer );
387 return VLC_SUCCESS;
390 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
391 block_t *p_buffer )
393 sout_stream_sys_t *p_sys = p_stream->p_sys;
394 int i_size;
395 uint8_t* p_pcm_buffer = NULL;
396 int i_samples = 0;
398 i_size = p_buffer->i_buffer;
399 if (id->format.audio.i_channels == 0)
401 msg_Warn( p_stream, "No buffer given!" );
402 block_ChainRelease( p_buffer );
403 return VLC_EGENERIC;
406 i_samples = i_size / ( ( id->format.audio.i_bitspersample / 8 ) * id->format.audio.i_channels );
407 /* Calling the prerender callback to get user buffer */
408 p_sys->pf_audio_prerender_callback( id->p_data, &p_pcm_buffer, i_size );
409 if (!p_pcm_buffer)
411 msg_Err( p_stream, "No buffer given!" );
412 block_ChainRelease( p_buffer );
413 return VLC_EGENERIC;
416 /* Copying data into user buffer */
417 memcpy( p_pcm_buffer, p_buffer->p_buffer, i_size );
418 /* Calling the postrender callback to tell the user his buffer is ready */
419 p_sys->pf_audio_postrender_callback( id->p_data, p_pcm_buffer,
420 id->format.audio.i_channels, id->format.audio.i_rate, i_samples,
421 id->format.audio.i_bitspersample, i_size, p_buffer->i_pts );
422 block_ChainRelease( p_buffer );
423 return VLC_SUCCESS;