]> git.sesse.net Git - vlc/blob - modules/stream_out/smem.c
Correcting type and value of the audio sample
[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 informations
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  * For now, audio is NOT IMPLEMENTED.
41  *
42  ******************************************************************************/
43
44 /*****************************************************************************
45  * Preamble
46  *****************************************************************************/
47
48 #ifdef HAVE_CONFIG_H
49 # include "config.h"
50 #endif
51
52 #include <vlc_common.h>
53 #include <vlc_plugin.h>
54 #include <vlc_sout.h>
55 #include <vlc_block.h>
56 #include <vlc_codec.h>
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61
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" )
65
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" )
69
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" )
73
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" )
77
78 #define T_VIDEO_DATA N_( "Video Callback data" )
79 #define LT_VIDEO_DATA N_( "Data for the video callback function." )
80
81 #define T_AUDIO_DATA N_( "Audio callback data" )
82 #define LT_AUDIO_DATA N_( "Data for the audio callback function." )
83
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.")
88
89 static int  Open ( vlc_object_t * );
90 static void Close( vlc_object_t * );
91
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-"
95
96 vlc_module_begin ()
97     set_shortname( N_("smem"))
98     set_description( N_("Stream output to memory buffer") )
99     set_capability( "sout stream", 50 )
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 )
104     add_string( SOUT_PREFIX_AUDIO "prerender-callback", "0", NULL, T_AUDIO_PRERENDER_CALLBACK, LT_AUDIO_PRERENDER_CALLBACK, true )
105     add_string( SOUT_PREFIX_VIDEO "postrender-callback", "0", NULL, T_VIDEO_POSTRENDER_CALLBACK, LT_VIDEO_POSTRENDER_CALLBACK, true )
106     add_string( SOUT_PREFIX_AUDIO "postrender-callback", "0", NULL, T_AUDIO_POSTRENDER_CALLBACK, LT_AUDIO_POSTRENDER_CALLBACK, true )
107     add_string( SOUT_PREFIX_VIDEO "data", "0", NULL, T_VIDEO_DATA, LT_VIDEO_DATA, true )
108     add_string( SOUT_PREFIX_AUDIO "data", "0", NULL, T_AUDIO_DATA, LT_VIDEO_DATA, true )
109     add_bool( SOUT_CFG_PREFIX "time-sync", true, NULL, T_TIME_SYNC, LT_TIME_SYNC, true );
110     set_callbacks( Open, Close )
111 vlc_module_end ()
112
113
114 /*****************************************************************************
115  * Exported prototypes
116  *****************************************************************************/
117 static const char *const ppsz_sout_options[] = {
118     "video-prerender-callback", "audio-prerender-callback",
119     "video-postrender-callback", "audio-postrender-callback", "video-data", "audio-data", "time-sync", NULL
120 };
121
122 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
123 static int               Del ( sout_stream_t *, sout_stream_id_t * );
124 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
125
126 static sout_stream_id_t *AddVideo( sout_stream_t *p_stream, es_format_t *p_fmt );
127 static sout_stream_id_t *AddAudio( sout_stream_t *p_stream, es_format_t *p_fmt );
128
129 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_t *id,
130                       block_t *p_buffer );
131 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_t *id,
132                       block_t *p_buffer );
133
134 struct sout_stream_id_t
135 {
136     es_format_t* format;
137     void *p_data;
138 };
139
140 struct sout_stream_sys_t
141 {
142     vlc_mutex_t *p_lock;
143     void ( *pf_video_prerender_callback ) ( void* p_video_data, uint8_t** pp_pixel_buffer , int size );
144     void ( *pf_audio_prerender_callback ) ( void* p_audio_data, uint8_t** pp_pcm_buffer , unsigned int size );
145     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 );
146     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 );
147     bool time_sync;
148 };
149
150 /*****************************************************************************
151  * Open:
152  *****************************************************************************/
153 static int Open( vlc_object_t *p_this )
154 {
155     char* psz_tmp;
156     sout_stream_t *p_stream = (sout_stream_t*)p_this;
157     sout_stream_sys_t *p_sys;
158
159     p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
160     if( !p_sys )
161         return VLC_ENOMEM;
162     p_stream->p_sys = p_sys;
163
164     p_sys->time_sync = var_CreateGetBool( p_stream, SOUT_CFG_PREFIX "time-sync" );
165
166     psz_tmp = var_CreateGetString( p_stream, SOUT_PREFIX_VIDEO "prerender-callback" );
167     p_sys->pf_video_prerender_callback = (void (*) (void *, uint8_t**, int))(intptr_t)atoll( psz_tmp );
168     free( psz_tmp );
169
170     psz_tmp = var_CreateGetString( p_stream, SOUT_PREFIX_AUDIO "prerender-callback" );
171     p_sys->pf_audio_prerender_callback = (void (*) (void* , uint8_t**, unsigned int))(intptr_t)atoll( psz_tmp );
172     free( psz_tmp );
173
174     psz_tmp = var_CreateGetString( p_stream, SOUT_PREFIX_VIDEO "postrender-callback" );
175     p_sys->pf_video_postrender_callback = (void (*) (void*, uint8_t*, int, int, int, int, mtime_t))(intptr_t)atoll( psz_tmp );
176     free( psz_tmp );
177
178     psz_tmp = var_CreateGetString( p_stream, SOUT_PREFIX_AUDIO "postrender-callback" );
179     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 );
180     free( psz_tmp );
181
182     /* Create the remaining variables for a later use */
183     var_Create( p_stream, SOUT_PREFIX_VIDEO "data", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
184     var_Create( p_stream, SOUT_PREFIX_AUDIO "data", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
185
186     /* Setting stream out module callbacks */
187     p_stream->pf_add    = Add;
188     p_stream->pf_del    = Del;
189     p_stream->pf_send   = Send;
190
191     /* Does the module need out_pace_control? */
192     if ( p_sys->time_sync )
193         p_stream->p_sout->i_out_pace_nocontrol++;
194
195     return VLC_SUCCESS;
196 }
197
198 /*****************************************************************************
199  * Close:
200  *****************************************************************************/
201 static void Close( vlc_object_t * p_this )
202 {
203     sout_stream_t *p_stream = (sout_stream_t*)p_this;
204     if ( p_stream->p_sys->time_sync )
205         p_stream->p_sout->i_out_pace_nocontrol--;
206     free( p_stream->p_sys );
207 }
208
209 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
210 {
211     sout_stream_id_t *id = NULL;
212
213     if ( p_fmt->i_cat == VIDEO_ES )
214         id = AddVideo( p_stream, p_fmt );
215     else if ( p_fmt->i_cat == AUDIO_ES )
216         id = AddAudio( p_stream, p_fmt );
217     return id;
218 }
219
220 static sout_stream_id_t *AddVideo( sout_stream_t *p_stream, es_format_t *p_fmt )
221 {
222     char* psz_tmp;
223     sout_stream_id_t    *id;
224     int i_bits_per_pixel;
225
226     switch( p_fmt->i_codec )
227     {
228         case VLC_CODEC_RGB32:
229             i_bits_per_pixel = 32;
230             break;
231         case VLC_CODEC_I444:
232         case VLC_CODEC_RGB24:
233             i_bits_per_pixel = 24;
234             break;
235         case VLC_CODEC_RGB16:
236         case VLC_CODEC_RGB15:
237         case VLC_CODEC_RGB8:
238         case VLC_CODEC_I422:
239             i_bits_per_pixel = 16;
240             break;
241         case VLC_CODEC_YV12:
242         case VLC_CODEC_I420:
243             i_bits_per_pixel = 12;
244             break;
245         case VLC_CODEC_RGBP:
246             i_bits_per_pixel = 8;
247             break;
248         default:
249             msg_Err( p_stream, "Smem does only support raw video format" );
250             return NULL;
251     }
252
253     id = calloc( 1, sizeof( sout_stream_id_t ) );
254     if( !id )
255         return NULL;
256
257     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "data" );
258     id->p_data = (void *)( intptr_t )atoll( psz_tmp );
259     free( psz_tmp );
260
261     id->format = p_fmt;
262     id->format->video.i_bits_per_pixel = i_bits_per_pixel;
263     return id;
264 }
265
266 static sout_stream_id_t *AddAudio( sout_stream_t *p_stream, es_format_t *p_fmt )
267 {
268     char* psz_tmp;
269     sout_stream_id_t* id;
270     int i_bits_per_sample;
271
272     switch( p_fmt->i_codec )
273     {
274     case VLC_CODEC_U8:
275     case VLC_CODEC_S8:
276         i_bits_per_sample = 8;
277         break;
278     case VLC_CODEC_U16L:
279     case VLC_CODEC_S16L:
280     case VLC_CODEC_U16B:
281     case VLC_CODEC_S16B:
282         i_bits_per_sample =  16;
283         break;
284     case VLC_CODEC_U24L:
285     case VLC_CODEC_S24L:
286     case VLC_CODEC_U24B:
287     case VLC_CODEC_S24B:
288         i_bits_per_sample = 24;
289         break;
290     case VLC_CODEC_S32L:
291     case VLC_CODEC_S32B:
292     case VLC_CODEC_FL32:
293     case VLC_CODEC_FI32:
294         i_bits_per_sample = 32;
295         break;
296     case VLC_CODEC_FL64:
297         i_bits_per_sample = 64;
298         break;
299     default:
300         msg_Err( p_stream, "Smem does only support raw audio format" );
301         return NULL;
302     }
303
304     id = calloc( 1, sizeof( sout_stream_id_t ) );
305     if( !id )
306         return NULL;
307
308     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "data" );
309     id->p_data = (void *)( intptr_t )atoll( psz_tmp );
310     free( psz_tmp );
311
312     id->format = p_fmt;
313     id->format->audio.i_bitspersample = i_bits_per_sample;
314     return id;
315 }
316
317 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
318 {
319     VLC_UNUSED( p_stream );
320     free( id );
321     return VLC_SUCCESS;
322 }
323
324 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
325                  block_t *p_buffer )
326 {
327     if ( id->format->i_cat == VIDEO_ES )
328         return SendVideo( p_stream, id, p_buffer );
329     else if ( id->format->i_cat == AUDIO_ES )
330         return SendAudio( p_stream, id, p_buffer );
331     return VLC_SUCCESS;
332 }
333
334 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_t *id,
335                       block_t *p_buffer )
336 {
337     sout_stream_sys_t *p_sys = p_stream->p_sys;
338     int i_line, i_line_size, i_size, i_pixel_pitch;
339     uint8_t* p_pixels;
340
341     i_line = id->format->video.i_height;
342     i_pixel_pitch = id->format->video.i_bits_per_pixel / 8;
343     i_line_size = i_pixel_pitch * id->format->video.i_width;
344     i_size = i_line * i_line_size;
345     /* Calling the prerender callback to get user buffer */
346     p_sys->pf_video_prerender_callback( id->p_data, &p_pixels , i_size );
347     /* Copying data into user buffer */
348     for ( int line = 0; line < i_line; line++, p_pixels += i_line_size )
349         vlc_memcpy( p_pixels, p_buffer->p_buffer + i_line_size * line , i_line_size );
350     /* Calling the postrender callback to tell the user his buffer is ready */
351     p_sys->pf_video_postrender_callback( id->p_data, p_pixels,
352                                          id->format->video.i_width, id->format->video.i_height,
353                                          id->format->video.i_bits_per_pixel, i_size, p_buffer->i_pts );
354     block_ChainRelease( p_buffer );
355     return VLC_SUCCESS;
356 }
357
358 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_t *id,
359                       block_t *p_buffer )
360 {
361     sout_stream_sys_t *p_sys = p_stream->p_sys;
362     int i_size;
363     uint8_t* p_pcm_buffer;
364     int i_samples = 0;
365
366     i_size = p_buffer->i_buffer;
367     i_samples = i_size / ( ( id->format->audio.i_bitspersample / 8 ) * id->format->audio.i_channels );
368     /* Calling the prerender callback to get user buffer */
369     p_sys->pf_audio_prerender_callback( id->p_data, &p_pcm_buffer, i_size );
370     /* Copying data into user buffer */
371     vlc_memcpy( p_pcm_buffer, p_buffer->p_buffer, i_size );
372     /* Calling the postrender callback to tell the user his buffer is ready */
373     p_sys->pf_audio_postrender_callback( id->p_data, p_pcm_buffer,
374                                          id->format->audio.i_channels, id->format->audio.i_rate, i_samples,
375                                          id->format->audio.i_bitspersample, i_size, p_buffer->i_pts );
376     block_ChainRelease( p_buffer );
377     return VLC_SUCCESS;
378 }