1 /*****************************************************************************
2 * smem.c: stream output to memory buffer module
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
7 * Authors: Christophe Courtaut <christophe.courtaut@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
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 informations
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 * For now, audio is NOT IMPLEMENTED.
42 ******************************************************************************/
44 /*****************************************************************************
46 *****************************************************************************/
52 #include <vlc_common.h>
53 #include <vlc_plugin.h>
55 #include <vlc_block.h>
56 #include <vlc_codec.h>
58 /*****************************************************************************
60 *****************************************************************************/
62 #define T_VIDEO_PRERENDER_CALLBACK N_( "Video prerender callback" )
63 #define LT_VIDEO_PRERENDER_CALLBACK N_( "Address of the video prerender callback function" \
64 "this function will set the buffer where render will be done" )
66 #define T_AUDIO_PRERENDER_CALLBACK N_( "Audio prerender callback" )
67 #define LT_AUDIO_PRERENDER_CALLBACK N_( "Address of the audio prerender callback function." \
68 "this function will set the buffer where render will be done" )
70 #define T_VIDEO_POSTRENDER_CALLBACK N_( "Video postrender callback" )
71 #define LT_VIDEO_POSTRENDER_CALLBACK N_( "Address of the video postrender callback function." \
72 "this function will be called when the render is into the buffer" )
74 #define T_AUDIO_POSTRENDER_CALLBACK N_( "Audio postrender callback" )
75 #define LT_AUDIO_POSTRENDER_CALLBACK N_( "Address of the audio postrender callback function." \
76 "this function will be called when the render is into the buffer" )
78 #define T_VIDEO_DATA N_( "Video Callback data" )
79 #define LT_VIDEO_DATA N_( "Data for the video callback function." )
81 #define T_AUDIO_DATA N_( "Audio callback data" )
82 #define LT_AUDIO_DATA N_( "Data for the audio callback function." )
84 #define T_TIME_SYNC N_( "Time Synchronized output" )
85 #define LT_TIME_SYNC N_( "Time Synchronisation option for output. " \
86 "If true, stream will render as usual, else " \
87 "it will be rendered as fast as possible.")
89 static int Open ( vlc_object_t * );
90 static void Close( vlc_object_t * );
92 #define SOUT_CFG_PREFIX "sout-smem-"
93 #define SOUT_PREFIX_VIDEO SOUT_CFG_PREFIX"video-"
94 #define SOUT_PREFIX_AUDIO SOUT_CFG_PREFIX"audio-"
97 set_shortname( N_("Smem"))
98 set_description( N_("Stream output to memory buffer") )
99 set_capability( "sout stream", 0 )
100 add_shortcut( "smem" )
101 set_category( CAT_SOUT )
102 set_subcategory( SUBCAT_SOUT_STREAM )
103 add_string( SOUT_PREFIX_VIDEO "prerender-callback", "0", NULL, T_VIDEO_PRERENDER_CALLBACK, LT_VIDEO_PRERENDER_CALLBACK, true )
105 add_string( SOUT_PREFIX_AUDIO "prerender-callback", "0", NULL, T_AUDIO_PRERENDER_CALLBACK, LT_AUDIO_PRERENDER_CALLBACK, true )
107 add_string( SOUT_PREFIX_VIDEO "postrender-callback", "0", NULL, T_VIDEO_POSTRENDER_CALLBACK, LT_VIDEO_POSTRENDER_CALLBACK, true )
109 add_string( SOUT_PREFIX_AUDIO "postrender-callback", "0", NULL, T_AUDIO_POSTRENDER_CALLBACK, LT_AUDIO_POSTRENDER_CALLBACK, true )
111 add_string( SOUT_PREFIX_VIDEO "data", "0", NULL, T_VIDEO_DATA, LT_VIDEO_DATA, true )
113 add_string( SOUT_PREFIX_AUDIO "data", "0", NULL, T_AUDIO_DATA, LT_VIDEO_DATA, true )
115 add_bool( SOUT_CFG_PREFIX "time-sync", true, NULL, T_TIME_SYNC, LT_TIME_SYNC, true )
117 set_callbacks( Open, Close )
121 /*****************************************************************************
122 * Exported prototypes
123 *****************************************************************************/
124 static const char *const ppsz_sout_options[] = {
125 "video-prerender-callback", "audio-prerender-callback",
126 "video-postrender-callback", "audio-postrender-callback", "video-data", "audio-data", "time-sync", NULL
129 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
130 static int Del ( sout_stream_t *, sout_stream_id_t * );
131 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
133 static sout_stream_id_t *AddVideo( sout_stream_t *p_stream, es_format_t *p_fmt );
134 static sout_stream_id_t *AddAudio( sout_stream_t *p_stream, es_format_t *p_fmt );
136 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_t *id,
138 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_t *id,
141 struct sout_stream_id_t
147 struct sout_stream_sys_t
150 void ( *pf_video_prerender_callback ) ( void* p_video_data, uint8_t** pp_pixel_buffer , int size );
151 void ( *pf_audio_prerender_callback ) ( void* p_audio_data, uint8_t** pp_pcm_buffer , unsigned int size );
152 void ( *pf_video_postrender_callback ) ( void* p_video_data, uint8_t* p_pixel_buffer, int width, int height, int pixel_pitch, int size, mtime_t pts );
153 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, unsigned int size, mtime_t pts );
157 /*****************************************************************************
159 *****************************************************************************/
160 static int Open( vlc_object_t *p_this )
163 sout_stream_t *p_stream = (sout_stream_t*)p_this;
164 sout_stream_sys_t *p_sys;
166 p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
169 p_stream->p_sys = p_sys;
171 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
174 p_sys->time_sync = var_GetBool( p_stream, SOUT_CFG_PREFIX "time-sync" );
176 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "prerender-callback" );
177 p_sys->pf_video_prerender_callback = (void (*) (void *, uint8_t**, int))(intptr_t)atoll( psz_tmp );
180 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "prerender-callback" );
181 p_sys->pf_audio_prerender_callback = (void (*) (void* , uint8_t**, unsigned int))(intptr_t)atoll( psz_tmp );
184 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "postrender-callback" );
185 p_sys->pf_video_postrender_callback = (void (*) (void*, uint8_t*, int, int, int, int, mtime_t))(intptr_t)atoll( psz_tmp );
188 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "postrender-callback" );
189 p_sys->pf_audio_postrender_callback = (void (*) (void*, uint8_t*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, mtime_t))(intptr_t)atoll( psz_tmp );
192 /* Setting stream out module callbacks */
193 p_stream->pf_add = Add;
194 p_stream->pf_del = Del;
195 p_stream->pf_send = Send;
197 /* Does the module need out_pace_control? */
198 if ( p_sys->time_sync )
199 p_stream->p_sout->i_out_pace_nocontrol++;
204 /*****************************************************************************
206 *****************************************************************************/
207 static void Close( vlc_object_t * p_this )
209 sout_stream_t *p_stream = (sout_stream_t*)p_this;
210 if ( p_stream->p_sys->time_sync )
211 p_stream->p_sout->i_out_pace_nocontrol--;
212 free( p_stream->p_sys );
215 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
217 sout_stream_id_t *id = NULL;
219 if ( p_fmt->i_cat == VIDEO_ES )
220 id = AddVideo( p_stream, p_fmt );
221 else if ( p_fmt->i_cat == AUDIO_ES )
222 id = AddAudio( p_stream, p_fmt );
226 static sout_stream_id_t *AddVideo( sout_stream_t *p_stream, es_format_t *p_fmt )
229 sout_stream_id_t *id;
230 int i_bits_per_pixel;
232 switch( p_fmt->i_codec )
234 case VLC_CODEC_RGB32:
235 i_bits_per_pixel = 32;
238 case VLC_CODEC_RGB24:
239 i_bits_per_pixel = 24;
241 case VLC_CODEC_RGB16:
242 case VLC_CODEC_RGB15:
245 i_bits_per_pixel = 16;
249 i_bits_per_pixel = 12;
252 i_bits_per_pixel = 8;
255 msg_Err( p_stream, "Smem does only support raw video format" );
259 id = calloc( 1, sizeof( sout_stream_id_t ) );
263 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "data" );
264 id->p_data = (void *)( intptr_t )atoll( psz_tmp );
268 id->format->video.i_bits_per_pixel = i_bits_per_pixel;
272 static sout_stream_id_t *AddAudio( sout_stream_t *p_stream, es_format_t *p_fmt )
275 sout_stream_id_t* id;
276 int i_bits_per_sample;
278 switch( p_fmt->i_codec )
282 i_bits_per_sample = 8;
288 i_bits_per_sample = 16;
294 i_bits_per_sample = 24;
300 i_bits_per_sample = 32;
303 i_bits_per_sample = 64;
306 msg_Err( p_stream, "Smem does only support raw audio format" );
310 id = calloc( 1, sizeof( sout_stream_id_t ) );
314 psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "data" );
315 id->p_data = (void *)( intptr_t )atoll( psz_tmp );
319 id->format->audio.i_bitspersample = i_bits_per_sample;
323 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
325 VLC_UNUSED( p_stream );
330 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
333 if ( id->format->i_cat == VIDEO_ES )
334 return SendVideo( p_stream, id, p_buffer );
335 else if ( id->format->i_cat == AUDIO_ES )
336 return SendAudio( p_stream, id, p_buffer );
340 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_t *id,
343 sout_stream_sys_t *p_sys = p_stream->p_sys;
344 int i_line, i_line_size, i_size, i_pixel_pitch;
347 i_line = id->format->video.i_height;
348 i_pixel_pitch = id->format->video.i_bits_per_pixel / 8;
349 i_line_size = i_pixel_pitch * id->format->video.i_width;
350 i_size = i_line * i_line_size;
351 /* Calling the prerender callback to get user buffer */
352 p_sys->pf_video_prerender_callback( id->p_data, &p_pixels , i_size );
353 /* Copying data into user buffer */
354 for ( int line = 0; line < i_line; line++, p_pixels += i_line_size )
355 vlc_memcpy( p_pixels, p_buffer->p_buffer + i_line_size * line , i_line_size );
356 /* Calling the postrender callback to tell the user his buffer is ready */
357 p_sys->pf_video_postrender_callback( id->p_data, p_pixels,
358 id->format->video.i_width, id->format->video.i_height,
359 id->format->video.i_bits_per_pixel, i_size, p_buffer->i_pts );
360 block_ChainRelease( p_buffer );
364 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_t *id,
367 sout_stream_sys_t *p_sys = p_stream->p_sys;
369 uint8_t* p_pcm_buffer;
372 i_size = p_buffer->i_buffer;
373 i_samples = i_size / ( ( id->format->audio.i_bitspersample / 8 ) * id->format->audio.i_channels );
374 /* Calling the prerender callback to get user buffer */
375 p_sys->pf_audio_prerender_callback( id->p_data, &p_pcm_buffer, i_size );
376 /* Copying data into user buffer */
377 vlc_memcpy( p_pcm_buffer, p_buffer->p_buffer, i_size );
378 /* Calling the postrender callback to tell the user his buffer is ready */
379 p_sys->pf_audio_postrender_callback( id->p_data, p_pcm_buffer,
380 id->format->audio.i_channels, id->format->audio.i_rate, i_samples,
381 id->format->audio.i_bitspersample, i_size, p_buffer->i_pts );
382 block_ChainRelease( p_buffer );