1 /*****************************************************************************
2 * smem.c: stream output to memory buffer module
3 *****************************************************************************
4 * Copyright (C) 2009 VLC authors and VideoLAN
6 * Authors: Christophe Courtaut <christophe.courtaut@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************
27 * You should use this module in combination with the transcode module, to get
28 * raw datas from it. This module does not make any conversion at all, so you
29 * need to use the transcode module for this purpose.
31 * For example, you can use smem as it :
32 * --sout="#transcode{vcodec=RV24,acodec=s16l}:smem{smem-options}"
34 * Into each lock function (audio and video), you will have all the information
35 * you need to allocate a buffer, so that this module will copy data in it.
37 * the video-data and audio-data pointers will be passed to lock/unlock function
39 ******************************************************************************/
41 /*****************************************************************************
43 *****************************************************************************/
49 #include <vlc_common.h>
50 #include <vlc_plugin.h>
52 #include <vlc_block.h>
53 #include <vlc_codec.h>
56 /*****************************************************************************
58 *****************************************************************************/
60 #define T_VIDEO_PRERENDER_CALLBACK N_( "Video prerender callback" )
61 #define LT_VIDEO_PRERENDER_CALLBACK N_( "Address of the video prerender callback function. " \
62 "This function will set the buffer where render will be done." )
64 #define T_AUDIO_PRERENDER_CALLBACK N_( "Audio prerender callback" )
65 #define LT_AUDIO_PRERENDER_CALLBACK N_( "Address of the audio prerender callback function. " \
66 "This function will set the buffer where render will be done." )
68 #define T_VIDEO_POSTRENDER_CALLBACK N_( "Video postrender callback" )
69 #define LT_VIDEO_POSTRENDER_CALLBACK N_( "Address of the video postrender callback function. " \
70 "This function will be called when the render is into the buffer." )
72 #define T_AUDIO_POSTRENDER_CALLBACK N_( "Audio postrender callback" )
73 #define LT_AUDIO_POSTRENDER_CALLBACK N_( "Address of the audio postrender callback function. " \
74 "This function will be called when the render is into the buffer." )
76 #define T_VIDEO_DATA N_( "Video Callback data" )
77 #define LT_VIDEO_DATA N_( "Data for the video callback function." )
79 #define T_AUDIO_DATA N_( "Audio callback data" )
80 #define LT_AUDIO_DATA N_( "Data for the audio callback function." )
82 #define T_TIME_SYNC N_( "Time Synchronized output" )
83 #define LT_TIME_SYNC N_( "Time Synchronisation option for output. " \
84 "If true, stream will render as usual, else " \
85 "it will be rendered as fast as possible.")
87 static int Open ( vlc_object_t
* );
88 static void Close( vlc_object_t
* );
90 #define SOUT_CFG_PREFIX "sout-smem-"
91 #define SOUT_PREFIX_VIDEO SOUT_CFG_PREFIX"video-"
92 #define SOUT_PREFIX_AUDIO SOUT_CFG_PREFIX"audio-"
95 set_shortname( N_("Smem"))
96 set_description( N_("Stream output to memory buffer") )
97 set_capability( "sout stream", 0 )
98 add_shortcut( "smem" )
99 set_category( CAT_SOUT
)
100 set_subcategory( SUBCAT_SOUT_STREAM
)
101 add_string( SOUT_PREFIX_VIDEO
"prerender-callback", "0", T_VIDEO_PRERENDER_CALLBACK
, LT_VIDEO_PRERENDER_CALLBACK
, true )
103 add_string( SOUT_PREFIX_AUDIO
"prerender-callback", "0", T_AUDIO_PRERENDER_CALLBACK
, LT_AUDIO_PRERENDER_CALLBACK
, true )
105 add_string( SOUT_PREFIX_VIDEO
"postrender-callback", "0", T_VIDEO_POSTRENDER_CALLBACK
, LT_VIDEO_POSTRENDER_CALLBACK
, true )
107 add_string( SOUT_PREFIX_AUDIO
"postrender-callback", "0", T_AUDIO_POSTRENDER_CALLBACK
, LT_AUDIO_POSTRENDER_CALLBACK
, true )
109 add_string( SOUT_PREFIX_VIDEO
"data", "0", T_VIDEO_DATA
, LT_VIDEO_DATA
, true )
111 add_string( SOUT_PREFIX_AUDIO
"data", "0", T_AUDIO_DATA
, LT_VIDEO_DATA
, true )
113 add_bool( SOUT_CFG_PREFIX
"time-sync", true, T_TIME_SYNC
, LT_TIME_SYNC
, true )
115 set_callbacks( Open
, Close
)
119 /*****************************************************************************
120 * Exported prototypes
121 *****************************************************************************/
122 static const char *const ppsz_sout_options
[] = {
123 "video-prerender-callback", "audio-prerender-callback",
124 "video-postrender-callback", "audio-postrender-callback", "video-data", "audio-data", "time-sync", NULL
127 static void *Add( sout_stream_t
*, const es_format_t
* );
128 static void Del( sout_stream_t
*, void * );
129 static int Send( sout_stream_t
*, void *, block_t
* );
131 static void *AddVideo( sout_stream_t
*p_stream
, const es_format_t
*p_fmt
);
132 static void *AddAudio( sout_stream_t
*p_stream
, const es_format_t
*p_fmt
);
134 static int SendVideo( sout_stream_t
*p_stream
, void *id
, block_t
*p_buffer
);
135 static int SendAudio( sout_stream_t
*p_stream
, void *id
, block_t
*p_buffer
);
141 } sout_stream_id_sys_t
;
146 void ( *pf_video_prerender_callback
) ( void* p_video_data
, uint8_t** pp_pixel_buffer
, size_t size
);
147 void ( *pf_audio_prerender_callback
) ( void* p_audio_data
, uint8_t** pp_pcm_buffer
, size_t size
);
148 void ( *pf_video_postrender_callback
) ( void* p_video_data
, uint8_t* p_pixel_buffer
, int width
, int height
, int pixel_pitch
, size_t size
, vlc_tick_t pts
);
149 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
, vlc_tick_t pts
);
153 void VideoPrerenderDefaultCallback( void* p_video_data
, uint8_t** pp_pixel_buffer
, size_t size
);
154 void AudioPrerenderDefaultCallback( void* p_audio_data
, uint8_t** pp_pcm_buffer
, size_t size
);
155 void VideoPostrenderDefaultCallback( void* p_video_data
, uint8_t* p_pixel_buffer
, int width
, int height
,
156 int pixel_pitch
, size_t size
, vlc_tick_t pts
);
157 void AudioPostrenderDefaultCallback( void* p_audio_data
, uint8_t* p_pcm_buffer
, unsigned int channels
,
158 unsigned int rate
, unsigned int nb_samples
, unsigned int bits_per_sample
,
159 size_t size
, vlc_tick_t pts
);
161 /*****************************************************************************
162 * Default empty callbacks
163 *****************************************************************************/
165 void VideoPrerenderDefaultCallback( void* p_video_data
, uint8_t** pp_pixel_buffer
, size_t size
)
167 VLC_UNUSED( p_video_data
); VLC_UNUSED( pp_pixel_buffer
); VLC_UNUSED( size
);
170 void AudioPrerenderDefaultCallback( void* p_audio_data
, uint8_t** pp_pcm_buffer
, size_t size
)
172 VLC_UNUSED( p_audio_data
); VLC_UNUSED( pp_pcm_buffer
); VLC_UNUSED( size
);
175 void VideoPostrenderDefaultCallback( void* p_video_data
, uint8_t* p_pixel_buffer
, int width
, int height
,
176 int pixel_pitch
, size_t size
, vlc_tick_t pts
)
178 VLC_UNUSED( p_video_data
); VLC_UNUSED( p_pixel_buffer
);
179 VLC_UNUSED( width
); VLC_UNUSED( height
);
180 VLC_UNUSED( pixel_pitch
); VLC_UNUSED( size
); VLC_UNUSED( pts
);
183 void AudioPostrenderDefaultCallback( void* p_audio_data
, uint8_t* p_pcm_buffer
, unsigned int channels
,
184 unsigned int rate
, unsigned int nb_samples
, unsigned int bits_per_sample
,
185 size_t size
, vlc_tick_t pts
)
187 VLC_UNUSED( p_audio_data
); VLC_UNUSED( p_pcm_buffer
);
188 VLC_UNUSED( channels
); VLC_UNUSED( rate
); VLC_UNUSED( nb_samples
);
189 VLC_UNUSED( bits_per_sample
); VLC_UNUSED( size
); VLC_UNUSED( pts
);
192 /*****************************************************************************
194 *****************************************************************************/
195 static int Open( vlc_object_t
*p_this
)
198 sout_stream_t
*p_stream
= (sout_stream_t
*)p_this
;
199 sout_stream_sys_t
*p_sys
;
201 p_sys
= calloc( 1, sizeof( sout_stream_sys_t
) );
204 p_stream
->p_sys
= p_sys
;
206 config_ChainParse( p_stream
, SOUT_CFG_PREFIX
, ppsz_sout_options
,
209 p_sys
->time_sync
= var_GetBool( p_stream
, SOUT_CFG_PREFIX
"time-sync" );
211 psz_tmp
= var_GetString( p_stream
, SOUT_PREFIX_VIDEO
"prerender-callback" );
212 p_sys
->pf_video_prerender_callback
= (void (*) (void *, uint8_t**, size_t))(intptr_t)atoll( psz_tmp
);
214 if (p_sys
->pf_video_prerender_callback
== NULL
)
215 p_sys
->pf_video_prerender_callback
= VideoPrerenderDefaultCallback
;
217 psz_tmp
= var_GetString( p_stream
, SOUT_PREFIX_AUDIO
"prerender-callback" );
218 p_sys
->pf_audio_prerender_callback
= (void (*) (void* , uint8_t**, size_t))(intptr_t)atoll( psz_tmp
);
220 if (p_sys
->pf_audio_prerender_callback
== NULL
)
221 p_sys
->pf_audio_prerender_callback
= AudioPrerenderDefaultCallback
;
223 psz_tmp
= var_GetString( p_stream
, SOUT_PREFIX_VIDEO
"postrender-callback" );
224 p_sys
->pf_video_postrender_callback
= (void (*) (void*, uint8_t*, int, int, int, size_t, vlc_tick_t
))(intptr_t)atoll( psz_tmp
);
226 if (p_sys
->pf_video_postrender_callback
== NULL
)
227 p_sys
->pf_video_postrender_callback
= VideoPostrenderDefaultCallback
;
229 psz_tmp
= var_GetString( p_stream
, SOUT_PREFIX_AUDIO
"postrender-callback" );
230 p_sys
->pf_audio_postrender_callback
= (void (*) (void*, uint8_t*, unsigned int, unsigned int, unsigned int, unsigned int, size_t, vlc_tick_t
))(intptr_t)atoll( psz_tmp
);
232 if (p_sys
->pf_audio_postrender_callback
== NULL
)
233 p_sys
->pf_audio_postrender_callback
= AudioPostrenderDefaultCallback
;
235 /* Setting stream out module callbacks */
236 p_stream
->pf_add
= Add
;
237 p_stream
->pf_del
= Del
;
238 p_stream
->pf_send
= Send
;
239 p_stream
->pace_nocontrol
= p_sys
->time_sync
;
244 /*****************************************************************************
246 *****************************************************************************/
247 static void Close( vlc_object_t
* p_this
)
249 sout_stream_t
*p_stream
= (sout_stream_t
*)p_this
;
250 free( p_stream
->p_sys
);
253 static void *Add( sout_stream_t
*p_stream
, const es_format_t
*p_fmt
)
255 sout_stream_id_sys_t
*id
= NULL
;
257 if ( p_fmt
->i_cat
== VIDEO_ES
)
258 id
= AddVideo( p_stream
, p_fmt
);
259 else if ( p_fmt
->i_cat
== AUDIO_ES
)
260 id
= AddAudio( p_stream
, p_fmt
);
264 static void *AddVideo( sout_stream_t
*p_stream
, const es_format_t
*p_fmt
)
267 sout_stream_id_sys_t
*id
;
268 int i_bits_per_pixel
;
270 switch( p_fmt
->i_codec
)
272 case VLC_CODEC_RGB32
:
275 i_bits_per_pixel
= 32;
278 case VLC_CODEC_RGB24
:
279 i_bits_per_pixel
= 24;
281 case VLC_CODEC_RGB16
:
282 case VLC_CODEC_RGB15
:
285 i_bits_per_pixel
= 16;
289 i_bits_per_pixel
= 12;
292 i_bits_per_pixel
= 8;
295 i_bits_per_pixel
= 0;
296 msg_Dbg( p_stream
, "non raw video format detected (%4.4s), buffers will contain compressed video", (char *)&p_fmt
->i_codec
);
300 id
= calloc( 1, sizeof( sout_stream_id_sys_t
) );
304 psz_tmp
= var_GetString( p_stream
, SOUT_PREFIX_VIDEO
"data" );
305 id
->p_data
= (void *)( intptr_t )atoll( psz_tmp
);
308 es_format_Copy( &id
->format
, p_fmt
);
309 id
->format
.video
.i_bits_per_pixel
= i_bits_per_pixel
;
313 static void *AddAudio( sout_stream_t
*p_stream
, const es_format_t
*p_fmt
)
316 sout_stream_id_sys_t
* id
;
317 int i_bits_per_sample
= aout_BitsPerSample( p_fmt
->i_codec
);
319 if( !i_bits_per_sample
)
321 msg_Err( p_stream
, "Smem does only support raw audio format" );
325 id
= calloc( 1, sizeof( sout_stream_id_sys_t
) );
329 psz_tmp
= var_GetString( p_stream
, SOUT_PREFIX_AUDIO
"data" );
330 id
->p_data
= (void *)( intptr_t )atoll( psz_tmp
);
333 es_format_Copy( &id
->format
, p_fmt
);
334 id
->format
.audio
.i_bitspersample
= i_bits_per_sample
;
338 static void Del( sout_stream_t
*p_stream
, void *_id
)
340 VLC_UNUSED( p_stream
);
341 sout_stream_id_sys_t
*id
= (sout_stream_id_sys_t
*)_id
;
342 es_format_Clean( &id
->format
);
346 static int Send( sout_stream_t
*p_stream
, void *_id
, block_t
*p_buffer
)
348 sout_stream_id_sys_t
*id
= (sout_stream_id_sys_t
*)_id
;
349 if ( id
->format
.i_cat
== VIDEO_ES
)
350 return SendVideo( p_stream
, id
, p_buffer
);
351 else if ( id
->format
.i_cat
== AUDIO_ES
)
352 return SendAudio( p_stream
, id
, p_buffer
);
356 static int SendVideo( sout_stream_t
*p_stream
, void *_id
, block_t
*p_buffer
)
358 sout_stream_sys_t
*p_sys
= p_stream
->p_sys
;
359 sout_stream_id_sys_t
*id
= (sout_stream_id_sys_t
*)_id
;
360 size_t i_size
= p_buffer
->i_buffer
;
361 uint8_t* p_pixels
= NULL
;
363 /* Calling the prerender callback to get user buffer */
364 p_sys
->pf_video_prerender_callback( id
->p_data
, &p_pixels
, i_size
);
368 msg_Err( p_stream
, "No buffer given!" );
369 block_ChainRelease( p_buffer
);
373 /* Copying data into user buffer */
374 memcpy( p_pixels
, p_buffer
->p_buffer
, i_size
);
375 /* Calling the postrender callback to tell the user his buffer is ready */
376 p_sys
->pf_video_postrender_callback( id
->p_data
, p_pixels
,
377 id
->format
.video
.i_width
, id
->format
.video
.i_height
,
378 id
->format
.video
.i_bits_per_pixel
, i_size
, p_buffer
->i_pts
);
379 block_ChainRelease( p_buffer
);
383 static int SendAudio( sout_stream_t
*p_stream
, void *_id
, block_t
*p_buffer
)
385 sout_stream_sys_t
*p_sys
= p_stream
->p_sys
;
386 sout_stream_id_sys_t
*id
= (sout_stream_id_sys_t
*)_id
;
388 uint8_t* p_pcm_buffer
= NULL
;
391 i_size
= p_buffer
->i_buffer
;
392 if (id
->format
.audio
.i_channels
== 0)
394 msg_Warn( p_stream
, "No buffer given!" );
395 block_ChainRelease( p_buffer
);
399 i_samples
= i_size
/ ( ( id
->format
.audio
.i_bitspersample
/ 8 ) * id
->format
.audio
.i_channels
);
400 /* Calling the prerender callback to get user buffer */
401 p_sys
->pf_audio_prerender_callback( id
->p_data
, &p_pcm_buffer
, i_size
);
404 msg_Err( p_stream
, "No buffer given!" );
405 block_ChainRelease( p_buffer
);
409 /* Copying data into user buffer */
410 memcpy( p_pcm_buffer
, p_buffer
->p_buffer
, i_size
);
411 /* Calling the postrender callback to tell the user his buffer is ready */
412 p_sys
->pf_audio_postrender_callback( id
->p_data
, p_pcm_buffer
,
413 id
->format
.audio
.i_channels
, id
->format
.audio
.i_rate
, i_samples
,
414 id
->format
.audio
.i_bitspersample
, i_size
, p_buffer
->i_pts
);
415 block_ChainRelease( p_buffer
);