contrib: cargo: use the 0.6.13 cargo-c version
[vlc.git] / modules / stream_out / smem.c
blob23323e39e4ac985c64d6c815731b501e0932849a
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 /*****************************************************************************
24 * How to use it
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 /*****************************************************************************
42 * Preamble
43 *****************************************************************************/
45 #ifdef HAVE_CONFIG_H
46 # include "config.h"
47 #endif
49 #include <vlc_common.h>
50 #include <vlc_plugin.h>
51 #include <vlc_sout.h>
52 #include <vlc_block.h>
53 #include <vlc_codec.h>
54 #include <vlc_aout.h>
56 /*****************************************************************************
57 * Module descriptor
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-"
94 vlc_module_begin ()
95 set_shortname( N_("Smem"))
96 set_description( N_("Stream output to memory buffer") )
97 set_capability( "sout output", 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 )
102 change_volatile()
103 add_string( SOUT_PREFIX_AUDIO "prerender-callback", "0", T_AUDIO_PRERENDER_CALLBACK, LT_AUDIO_PRERENDER_CALLBACK, true )
104 change_volatile()
105 add_string( SOUT_PREFIX_VIDEO "postrender-callback", "0", T_VIDEO_POSTRENDER_CALLBACK, LT_VIDEO_POSTRENDER_CALLBACK, true )
106 change_volatile()
107 add_string( SOUT_PREFIX_AUDIO "postrender-callback", "0", T_AUDIO_POSTRENDER_CALLBACK, LT_AUDIO_POSTRENDER_CALLBACK, true )
108 change_volatile()
109 add_string( SOUT_PREFIX_VIDEO "data", "0", T_VIDEO_DATA, LT_VIDEO_DATA, true )
110 change_volatile()
111 add_string( SOUT_PREFIX_AUDIO "data", "0", T_AUDIO_DATA, LT_VIDEO_DATA, true )
112 change_volatile()
113 add_bool( SOUT_CFG_PREFIX "time-sync", true, T_TIME_SYNC, LT_TIME_SYNC, true )
114 change_private()
115 set_callbacks( Open, Close )
116 vlc_module_end ()
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 );
137 typedef struct
139 es_format_t format;
140 void *p_data;
141 } sout_stream_id_sys_t;
143 typedef struct
145 vlc_mutex_t *p_lock;
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 );
150 bool time_sync;
151 } sout_stream_sys_t;
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 static int Control(sout_stream_t *stream, int query, va_list args)
194 sout_stream_sys_t *sys = stream->p_sys;
196 switch (query)
198 case SOUT_STREAM_IS_SYNCHRONOUS:
199 *va_arg(args, bool *) = sys->time_sync;
200 break;
202 default:
203 return VLC_EGENERIC;
206 return VLC_SUCCESS;
209 static const struct sout_stream_operations ops = {
210 Add, Del, Send, Control, NULL,
213 /*****************************************************************************
214 * Open:
215 *****************************************************************************/
216 static int Open( vlc_object_t *p_this )
218 char* psz_tmp;
219 sout_stream_t *p_stream = (sout_stream_t*)p_this;
220 sout_stream_sys_t *p_sys;
222 p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
223 if( !p_sys )
224 return VLC_ENOMEM;
225 p_stream->p_sys = p_sys;
227 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
228 p_stream->p_cfg );
230 p_sys->time_sync = var_GetBool( p_stream, SOUT_CFG_PREFIX "time-sync" );
232 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "prerender-callback" );
233 p_sys->pf_video_prerender_callback = (void (*) (void *, uint8_t**, size_t))(intptr_t)atoll( psz_tmp );
234 free( psz_tmp );
235 if (p_sys->pf_video_prerender_callback == NULL)
236 p_sys->pf_video_prerender_callback = VideoPrerenderDefaultCallback;
238 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "prerender-callback" );
239 p_sys->pf_audio_prerender_callback = (void (*) (void* , uint8_t**, size_t))(intptr_t)atoll( psz_tmp );
240 free( psz_tmp );
241 if (p_sys->pf_audio_prerender_callback == NULL)
242 p_sys->pf_audio_prerender_callback = AudioPrerenderDefaultCallback;
244 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "postrender-callback" );
245 p_sys->pf_video_postrender_callback = (void (*) (void*, uint8_t*, int, int, int, size_t, vlc_tick_t))(intptr_t)atoll( psz_tmp );
246 free( psz_tmp );
247 if (p_sys->pf_video_postrender_callback == NULL)
248 p_sys->pf_video_postrender_callback = VideoPostrenderDefaultCallback;
250 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "postrender-callback" );
251 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 );
252 free( psz_tmp );
253 if (p_sys->pf_audio_postrender_callback == NULL)
254 p_sys->pf_audio_postrender_callback = AudioPostrenderDefaultCallback;
256 /* Setting stream out module callbacks */
257 p_stream->ops = &ops;
258 return VLC_SUCCESS;
261 /*****************************************************************************
262 * Close:
263 *****************************************************************************/
264 static void Close( vlc_object_t * p_this )
266 sout_stream_t *p_stream = (sout_stream_t*)p_this;
267 free( p_stream->p_sys );
270 static void *Add( sout_stream_t *p_stream, const es_format_t *p_fmt )
272 sout_stream_id_sys_t *id = NULL;
274 if ( p_fmt->i_cat == VIDEO_ES )
275 id = AddVideo( p_stream, p_fmt );
276 else if ( p_fmt->i_cat == AUDIO_ES )
277 id = AddAudio( p_stream, p_fmt );
278 return id;
281 static void *AddVideo( sout_stream_t *p_stream, const es_format_t *p_fmt )
283 char* psz_tmp;
284 sout_stream_id_sys_t *id;
285 int i_bits_per_pixel;
287 switch( p_fmt->i_codec )
289 case VLC_CODEC_RGB32:
290 case VLC_CODEC_RGBA:
291 case VLC_CODEC_ARGB:
292 i_bits_per_pixel = 32;
293 break;
294 case VLC_CODEC_I444:
295 case VLC_CODEC_RGB24:
296 i_bits_per_pixel = 24;
297 break;
298 case VLC_CODEC_RGB16:
299 case VLC_CODEC_RGB15:
300 case VLC_CODEC_RGB8:
301 case VLC_CODEC_I422:
302 i_bits_per_pixel = 16;
303 break;
304 case VLC_CODEC_YV12:
305 case VLC_CODEC_I420:
306 i_bits_per_pixel = 12;
307 break;
308 case VLC_CODEC_RGBP:
309 i_bits_per_pixel = 8;
310 break;
311 default:
312 i_bits_per_pixel = 0;
313 msg_Dbg( p_stream, "non raw video format detected (%4.4s), buffers will contain compressed video", (char *)&p_fmt->i_codec );
314 break;
317 id = calloc( 1, sizeof( sout_stream_id_sys_t ) );
318 if( !id )
319 return NULL;
321 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "data" );
322 id->p_data = (void *)( intptr_t )atoll( psz_tmp );
323 free( psz_tmp );
325 es_format_Copy( &id->format, p_fmt );
326 id->format.video.i_bits_per_pixel = i_bits_per_pixel;
327 return id;
330 static void *AddAudio( sout_stream_t *p_stream, const es_format_t *p_fmt )
332 char* psz_tmp;
333 sout_stream_id_sys_t* id;
334 int i_bits_per_sample = aout_BitsPerSample( p_fmt->i_codec );
336 if( !i_bits_per_sample )
338 msg_Err( p_stream, "Smem does only support raw audio format" );
339 return NULL;
342 id = calloc( 1, sizeof( sout_stream_id_sys_t ) );
343 if( !id )
344 return NULL;
346 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "data" );
347 id->p_data = (void *)( intptr_t )atoll( psz_tmp );
348 free( psz_tmp );
350 es_format_Copy( &id->format, p_fmt );
351 id->format.audio.i_bitspersample = i_bits_per_sample;
352 return id;
355 static void Del( sout_stream_t *p_stream, void *_id )
357 VLC_UNUSED( p_stream );
358 sout_stream_id_sys_t *id = (sout_stream_id_sys_t *)_id;
359 es_format_Clean( &id->format );
360 free( id );
363 static int Send( sout_stream_t *p_stream, void *_id, block_t *p_buffer )
365 sout_stream_id_sys_t *id = (sout_stream_id_sys_t *)_id;
366 if ( id->format.i_cat == VIDEO_ES )
367 return SendVideo( p_stream, id, p_buffer );
368 else if ( id->format.i_cat == AUDIO_ES )
369 return SendAudio( p_stream, id, p_buffer );
370 return VLC_SUCCESS;
373 static int SendVideo( sout_stream_t *p_stream, void *_id, block_t *p_buffer )
375 sout_stream_sys_t *p_sys = p_stream->p_sys;
376 sout_stream_id_sys_t *id = (sout_stream_id_sys_t *)_id;
377 size_t i_size = p_buffer->i_buffer;
378 uint8_t* p_pixels = NULL;
380 /* Calling the prerender callback to get user buffer */
381 p_sys->pf_video_prerender_callback( id->p_data, &p_pixels, i_size );
383 if (!p_pixels)
385 msg_Err( p_stream, "No buffer given!" );
386 block_ChainRelease( p_buffer );
387 return VLC_EGENERIC;
390 /* Copying data into user buffer */
391 memcpy( p_pixels, p_buffer->p_buffer, i_size );
392 /* Calling the postrender callback to tell the user his buffer is ready */
393 p_sys->pf_video_postrender_callback( id->p_data, p_pixels,
394 id->format.video.i_width, id->format.video.i_height,
395 id->format.video.i_bits_per_pixel, i_size, p_buffer->i_pts );
396 block_ChainRelease( p_buffer );
397 return VLC_SUCCESS;
400 static int SendAudio( sout_stream_t *p_stream, void *_id, block_t *p_buffer )
402 sout_stream_sys_t *p_sys = p_stream->p_sys;
403 sout_stream_id_sys_t *id = (sout_stream_id_sys_t *)_id;
404 int i_size;
405 uint8_t* p_pcm_buffer = NULL;
406 int i_samples = 0;
408 i_size = p_buffer->i_buffer;
409 if (id->format.audio.i_channels == 0)
411 msg_Warn( p_stream, "No buffer given!" );
412 block_ChainRelease( p_buffer );
413 return VLC_EGENERIC;
416 i_samples = i_size / ( ( id->format.audio.i_bitspersample / 8 ) * id->format.audio.i_channels );
417 /* Calling the prerender callback to get user buffer */
418 p_sys->pf_audio_prerender_callback( id->p_data, &p_pcm_buffer, i_size );
419 if (!p_pcm_buffer)
421 msg_Err( p_stream, "No buffer given!" );
422 block_ChainRelease( p_buffer );
423 return VLC_EGENERIC;
426 /* Copying data into user buffer */
427 memcpy( p_pcm_buffer, p_buffer->p_buffer, i_size );
428 /* Calling the postrender callback to tell the user his buffer is ready */
429 p_sys->pf_audio_postrender_callback( id->p_data, p_pcm_buffer,
430 id->format.audio.i_channels, id->format.audio.i_rate, i_samples,
431 id->format.audio.i_bitspersample, i_size, p_buffer->i_pts );
432 block_ChainRelease( p_buffer );
433 return VLC_SUCCESS;