]> git.sesse.net Git - vlc/blob - modules/stream_out/smem.c
s/informations/information/
[vlc] / modules / stream_out / smem.c
1 /*****************************************************************************
2  * smem.c: stream output to memory buffer module
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Courtaut <christophe.courtaut@gmail.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * How to use it
26  *****************************************************************************
27  *
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.
31  *
32  * For example, you can use smem as it :
33  * --sout="#transcode{vcodec=RV24,acodec=s16l}:smem{smem-options}"
34  *
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.
37  *
38  * the video-data and audio-data pointers will be passed to lock/unlock function
39  *
40  ******************************************************************************/
41
42 /*****************************************************************************
43  * Preamble
44  *****************************************************************************/
45
46 #ifdef HAVE_CONFIG_H
47 # include "config.h"
48 #endif
49
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
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59
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" )
63
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" )
67
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" )
71
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" )
75
76 #define T_VIDEO_DATA N_( "Video Callback data" )
77 #define LT_VIDEO_DATA N_( "Data for the video callback function." )
78
79 #define T_AUDIO_DATA N_( "Audio callback data" )
80 #define LT_AUDIO_DATA N_( "Data for the audio callback function." )
81
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.")
86
87 static int  Open ( vlc_object_t * );
88 static void Close( vlc_object_t * );
89
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-"
93
94 vlc_module_begin ()
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", NULL, T_VIDEO_PRERENDER_CALLBACK, LT_VIDEO_PRERENDER_CALLBACK, true )
102         change_volatile()
103     add_string( SOUT_PREFIX_AUDIO "prerender-callback", "0", NULL, T_AUDIO_PRERENDER_CALLBACK, LT_AUDIO_PRERENDER_CALLBACK, true )
104         change_volatile()
105     add_string( SOUT_PREFIX_VIDEO "postrender-callback", "0", NULL, T_VIDEO_POSTRENDER_CALLBACK, LT_VIDEO_POSTRENDER_CALLBACK, true )
106         change_volatile()
107     add_string( SOUT_PREFIX_AUDIO "postrender-callback", "0", NULL, T_AUDIO_POSTRENDER_CALLBACK, LT_AUDIO_POSTRENDER_CALLBACK, true )
108         change_volatile()
109     add_string( SOUT_PREFIX_VIDEO "data", "0", NULL, T_VIDEO_DATA, LT_VIDEO_DATA, true )
110         change_volatile()
111     add_string( SOUT_PREFIX_AUDIO "data", "0", NULL, T_AUDIO_DATA, LT_VIDEO_DATA, true )
112         change_volatile()
113     add_bool( SOUT_CFG_PREFIX "time-sync", true, NULL, T_TIME_SYNC, LT_TIME_SYNC, true )
114         change_private()
115     set_callbacks( Open, Close )
116 vlc_module_end ()
117
118
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
125 };
126
127 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
128 static int               Del ( sout_stream_t *, sout_stream_id_t * );
129 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
130
131 static sout_stream_id_t *AddVideo( sout_stream_t *p_stream, es_format_t *p_fmt );
132 static sout_stream_id_t *AddAudio( sout_stream_t *p_stream, es_format_t *p_fmt );
133
134 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_t *id,
135                       block_t *p_buffer );
136 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_t *id,
137                       block_t *p_buffer );
138
139 struct sout_stream_id_t
140 {
141     es_format_t* format;
142     void *p_data;
143 };
144
145 struct sout_stream_sys_t
146 {
147     vlc_mutex_t *p_lock;
148     void ( *pf_video_prerender_callback ) ( void* p_video_data, uint8_t** pp_pixel_buffer , int size );
149     void ( *pf_audio_prerender_callback ) ( void* p_audio_data, uint8_t** pp_pcm_buffer , unsigned int size );
150     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 );
151     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 );
152     bool time_sync;
153 };
154
155 /*****************************************************************************
156  * Open:
157  *****************************************************************************/
158 static int Open( vlc_object_t *p_this )
159 {
160     char* psz_tmp;
161     sout_stream_t *p_stream = (sout_stream_t*)p_this;
162     sout_stream_sys_t *p_sys;
163
164     p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
165     if( !p_sys )
166         return VLC_ENOMEM;
167     p_stream->p_sys = p_sys;
168
169     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
170                        p_stream->p_cfg );
171
172     p_sys->time_sync = var_GetBool( p_stream, SOUT_CFG_PREFIX "time-sync" );
173
174     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "prerender-callback" );
175     p_sys->pf_video_prerender_callback = (void (*) (void *, uint8_t**, int))(intptr_t)atoll( psz_tmp );
176     free( psz_tmp );
177
178     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "prerender-callback" );
179     p_sys->pf_audio_prerender_callback = (void (*) (void* , uint8_t**, unsigned int))(intptr_t)atoll( psz_tmp );
180     free( psz_tmp );
181
182     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "postrender-callback" );
183     p_sys->pf_video_postrender_callback = (void (*) (void*, uint8_t*, int, int, int, int, mtime_t))(intptr_t)atoll( psz_tmp );
184     free( psz_tmp );
185
186     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "postrender-callback" );
187     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 );
188     free( psz_tmp );
189
190     /* Setting stream out module callbacks */
191     p_stream->pf_add    = Add;
192     p_stream->pf_del    = Del;
193     p_stream->pf_send   = Send;
194
195     /* Does the module need out_pace_control? */
196     if ( p_sys->time_sync )
197         p_stream->p_sout->i_out_pace_nocontrol++;
198
199     return VLC_SUCCESS;
200 }
201
202 /*****************************************************************************
203  * Close:
204  *****************************************************************************/
205 static void Close( vlc_object_t * p_this )
206 {
207     sout_stream_t *p_stream = (sout_stream_t*)p_this;
208     if ( p_stream->p_sys->time_sync )
209         p_stream->p_sout->i_out_pace_nocontrol--;
210     free( p_stream->p_sys );
211 }
212
213 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
214 {
215     sout_stream_id_t *id = NULL;
216
217     if ( p_fmt->i_cat == VIDEO_ES )
218         id = AddVideo( p_stream, p_fmt );
219     else if ( p_fmt->i_cat == AUDIO_ES )
220         id = AddAudio( p_stream, p_fmt );
221     return id;
222 }
223
224 static sout_stream_id_t *AddVideo( sout_stream_t *p_stream, es_format_t *p_fmt )
225 {
226     char* psz_tmp;
227     sout_stream_id_t    *id;
228     int i_bits_per_pixel;
229
230     switch( p_fmt->i_codec )
231     {
232         case VLC_CODEC_RGB32:
233         case VLC_CODEC_RGBA:
234             i_bits_per_pixel = 32;
235             break;
236         case VLC_CODEC_I444:
237         case VLC_CODEC_RGB24:
238             i_bits_per_pixel = 24;
239             break;
240         case VLC_CODEC_RGB16:
241         case VLC_CODEC_RGB15:
242         case VLC_CODEC_RGB8:
243         case VLC_CODEC_I422:
244             i_bits_per_pixel = 16;
245             break;
246         case VLC_CODEC_YV12:
247         case VLC_CODEC_I420:
248             i_bits_per_pixel = 12;
249             break;
250         case VLC_CODEC_RGBP:
251             i_bits_per_pixel = 8;
252             break;
253         default:
254             i_bits_per_pixel = 0;
255             msg_Dbg( p_stream, "non raw video format detected (%4.4s), buffers will contain compressed video", (char *)&p_fmt->i_codec );
256             break;
257     }
258
259     id = calloc( 1, sizeof( sout_stream_id_t ) );
260     if( !id )
261         return NULL;
262
263     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "data" );
264     id->p_data = (void *)( intptr_t )atoll( psz_tmp );
265     free( psz_tmp );
266
267     id->format = p_fmt;
268     id->format->video.i_bits_per_pixel = i_bits_per_pixel;
269     return id;
270 }
271
272 static sout_stream_id_t *AddAudio( sout_stream_t *p_stream, es_format_t *p_fmt )
273 {
274     char* psz_tmp;
275     sout_stream_id_t* id;
276     int i_bits_per_sample;
277
278     switch( p_fmt->i_codec )
279     {
280     case VLC_CODEC_U8:
281     case VLC_CODEC_S8:
282         i_bits_per_sample = 8;
283         break;
284     case VLC_CODEC_U16L:
285     case VLC_CODEC_S16L:
286     case VLC_CODEC_U16B:
287     case VLC_CODEC_S16B:
288         i_bits_per_sample =  16;
289         break;
290     case VLC_CODEC_U24L:
291     case VLC_CODEC_S24L:
292     case VLC_CODEC_U24B:
293     case VLC_CODEC_S24B:
294         i_bits_per_sample = 24;
295         break;
296     case VLC_CODEC_S32L:
297     case VLC_CODEC_S32B:
298     case VLC_CODEC_FL32:
299     case VLC_CODEC_FI32:
300         i_bits_per_sample = 32;
301         break;
302     case VLC_CODEC_FL64:
303         i_bits_per_sample = 64;
304         break;
305     default:
306         msg_Err( p_stream, "Smem does only support raw audio format" );
307         return NULL;
308     }
309
310     id = calloc( 1, sizeof( sout_stream_id_t ) );
311     if( !id )
312         return NULL;
313
314     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "data" );
315     id->p_data = (void *)( intptr_t )atoll( psz_tmp );
316     free( psz_tmp );
317
318     id->format = p_fmt;
319     id->format->audio.i_bitspersample = i_bits_per_sample;
320     return id;
321 }
322
323 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
324 {
325     VLC_UNUSED( p_stream );
326     free( id );
327     return VLC_SUCCESS;
328 }
329
330 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
331                  block_t *p_buffer )
332 {
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 );
337     return VLC_SUCCESS;
338 }
339
340 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_t *id,
341                       block_t *p_buffer )
342 {
343     sout_stream_sys_t *p_sys = p_stream->p_sys;
344     int i_line, i_line_size, i_size, i_pixel_pitch;
345     uint8_t* p_pixels;
346
347     if( id->format->video.i_bits_per_pixel > 0 )
348     {
349         i_line = id->format->video.i_height;
350         i_pixel_pitch = id->format->video.i_bits_per_pixel / 8;
351         i_line_size = i_pixel_pitch * id->format->video.i_width;
352         i_size = i_line * i_line_size;
353     }
354     else
355     {
356         i_size = p_buffer->i_buffer;
357     }
358     /* Calling the prerender callback to get user buffer */
359     p_sys->pf_video_prerender_callback( id->p_data, &p_pixels , i_size );
360     /* Copying data into user buffer */
361     if( id->format->video.i_bits_per_pixel > 0 )
362     {
363         for ( int line = 0; line < i_line; line++, p_pixels += i_line_size )
364             vlc_memcpy( p_pixels, p_buffer->p_buffer + i_line_size * line , i_line_size );
365     }
366     else
367     {
368         vlc_memcpy( p_pixels, p_buffer->p_buffer, i_size );
369     }
370     /* Calling the postrender callback to tell the user his buffer is ready */
371     p_sys->pf_video_postrender_callback( id->p_data, p_pixels,
372                                          id->format->video.i_width, id->format->video.i_height,
373                                          id->format->video.i_bits_per_pixel, i_size, p_buffer->i_pts );
374     block_ChainRelease( p_buffer );
375     return VLC_SUCCESS;
376 }
377
378 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_t *id,
379                       block_t *p_buffer )
380 {
381     sout_stream_sys_t *p_sys = p_stream->p_sys;
382     int i_size;
383     uint8_t* p_pcm_buffer;
384     int i_samples = 0;
385
386     i_size = p_buffer->i_buffer;
387     i_samples = i_size / ( ( id->format->audio.i_bitspersample / 8 ) * id->format->audio.i_channels );
388     /* Calling the prerender callback to get user buffer */
389     p_sys->pf_audio_prerender_callback( id->p_data, &p_pcm_buffer, i_size );
390     /* Copying data into user buffer */
391     vlc_memcpy( p_pcm_buffer, p_buffer->p_buffer, i_size );
392     /* Calling the postrender callback to tell the user his buffer is ready */
393     p_sys->pf_audio_postrender_callback( id->p_data, p_pcm_buffer,
394                                          id->format->audio.i_channels, id->format->audio.i_rate, i_samples,
395                                          id->format->audio.i_bitspersample, i_size, p_buffer->i_pts );
396     block_ChainRelease( p_buffer );
397     return VLC_SUCCESS;
398 }
399