]> git.sesse.net Git - vlc/blob - modules/stream_out/smem.c
Move Data Context from sys to stream id struct
[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_audio_data;
138     void *p_video_data;
139 };
140
141 struct sout_stream_sys_t
142 {
143     vlc_mutex_t *p_lock;
144     void ( *pf_video_prerender_callback ) ( void* p_video_data, uint8_t** pp_pixel_buffer , int size );
145     void ( *pf_audio_prerender_callback ) ( void* p_audio_data, uint8_t** pp_pcm_buffer , unsigned int size );
146     void ( *pf_video_postrender_callback ) ( void* p_video_data, uint8_t* p_pixel_buffer, int width, int height, int pixel_pitch, int size, int pts );
147     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, int pts );
148     bool time_sync;
149 };
150
151 /*****************************************************************************
152  * Open:
153  *****************************************************************************/
154 static int Open( vlc_object_t *p_this )
155 {
156     char* psz_tmp;
157     sout_stream_t *p_stream = (sout_stream_t*)p_this;
158     sout_stream_sys_t *p_sys;
159
160     p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
161     if( !p_sys )
162         return VLC_ENOMEM;
163     p_stream->p_sys = p_sys;
164
165     p_sys->time_sync = var_CreateGetBool( p_stream, SOUT_CFG_PREFIX "time-sync" );
166
167     psz_tmp = var_CreateGetString( p_stream, SOUT_PREFIX_VIDEO "prerender-callback" );
168     p_sys->pf_video_prerender_callback = (void (*) (void *, uint8_t**, int))(intptr_t)atoll( psz_tmp );
169     free( psz_tmp );
170
171     psz_tmp = var_CreateGetString( p_stream, SOUT_PREFIX_AUDIO "prerender-callback" );
172     p_sys->pf_audio_prerender_callback = (void (*) (void* , uint8_t**, unsigned int))(intptr_t)atoll( psz_tmp );
173     free( psz_tmp );
174
175     psz_tmp = var_CreateGetString( p_stream, SOUT_PREFIX_VIDEO "postrender-callback" );
176     p_sys->pf_video_postrender_callback = (void (*) (void*, uint8_t*, int, int, int, int, int))(intptr_t)atoll( psz_tmp );
177     free( psz_tmp );
178
179     psz_tmp = var_CreateGetString( p_stream, SOUT_PREFIX_AUDIO "postrender-callback" );
180     p_sys->pf_audio_postrender_callback = (void (*) (void*, uint8_t*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, int))(intptr_t)atoll( psz_tmp );
181     free( psz_tmp );
182
183     /* Setting stream out module callbacks */
184     p_stream->pf_add    = Add;
185     p_stream->pf_del    = Del;
186     p_stream->pf_send   = Send;
187
188     /* Does the module need out_pace_control? */
189     if ( p_sys->time_sync )
190         p_stream->p_sout->i_out_pace_nocontrol++;
191
192     return VLC_SUCCESS;
193 }
194
195 /*****************************************************************************
196  * Close:
197  *****************************************************************************/
198 static void Close( vlc_object_t * p_this )
199 {
200     sout_stream_t *p_stream = (sout_stream_t*)p_this;
201     if ( p_stream->p_sys->time_sync )
202         p_stream->p_sout->i_out_pace_nocontrol--;
203     free( p_stream->p_sys );
204 }
205
206 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
207 {
208     sout_stream_id_t *id = NULL;
209
210     if ( p_fmt->i_cat == VIDEO_ES )
211         id = AddVideo( p_stream, p_fmt );
212     else if ( p_fmt->i_cat == AUDIO_ES )
213         id = AddAudio( p_stream, p_fmt );
214     return id;
215 }
216
217 static sout_stream_id_t *AddVideo( sout_stream_t *p_stream, es_format_t *p_fmt )
218 {
219     char* psz_tmp;
220     sout_stream_id_t    *id;
221     int i_bits_per_pixel;
222
223     switch( p_fmt->i_codec )
224     {
225         case VLC_CODEC_RGB32:
226             i_bits_per_pixel = 32;
227             break;
228         case VLC_CODEC_I444:
229         case VLC_CODEC_RGB24:
230             i_bits_per_pixel = 24;
231             break;
232         case VLC_CODEC_RGB16:
233         case VLC_CODEC_RGB15:
234         case VLC_CODEC_RGB8:
235         case VLC_CODEC_I422:
236             i_bits_per_pixel = 16;
237             break;
238         case VLC_CODEC_YV12:
239         case VLC_CODEC_I420:
240             i_bits_per_pixel = 12;
241             break;
242         case VLC_CODEC_RGBP:
243             i_bits_per_pixel = 8;
244             break;
245         default:
246             msg_Err( p_stream, "Smem does only support raw video format" );
247             return NULL;
248     }
249
250     id = calloc( 1, sizeof( sout_stream_id_t ) );
251     if( !id )
252         return NULL;
253
254     psz_tmp = var_CreateGetString( p_stream, SOUT_PREFIX_VIDEO "data" );
255     id->p_video_data = (void *)( intptr_t )atoll( psz_tmp );
256     free( psz_tmp );
257
258     id->format = p_fmt;
259     id->format->video.i_bits_per_pixel = i_bits_per_pixel;
260     return id;
261 }
262
263 static sout_stream_id_t *AddAudio( sout_stream_t *p_stream, es_format_t *p_fmt )
264 {
265     char* psz_tmp;
266     sout_stream_id_t* id;
267     int i_bits_per_sample;
268
269     switch( p_fmt->i_codec )
270     {
271     case VLC_CODEC_U8:
272     case VLC_CODEC_S8:
273         i_bits_per_sample = 8;
274         break;
275     case VLC_CODEC_U16L:
276     case VLC_CODEC_S16L:
277     case VLC_CODEC_U16B:
278     case VLC_CODEC_S16B:
279         i_bits_per_sample =  16;
280         break;
281     case VLC_CODEC_U24L:
282     case VLC_CODEC_S24L:
283     case VLC_CODEC_U24B:
284     case VLC_CODEC_S24B:
285         i_bits_per_sample = 24;
286         break;
287     case VLC_CODEC_S32L:
288     case VLC_CODEC_S32B:
289     case VLC_CODEC_FL32:
290     case VLC_CODEC_FI32:
291         i_bits_per_sample = 32;
292         break;
293     case VLC_CODEC_FL64:
294         i_bits_per_sample = 64;
295         break;
296     default:
297         msg_Err( p_stream, "Smem does only support raw audio format" );
298         return NULL;
299     }
300
301     id = calloc( 1, sizeof( sout_stream_id_t ) );
302     if( !id )
303         return NULL;
304
305     psz_tmp = var_CreateGetString( p_stream, SOUT_PREFIX_AUDIO "data" );
306     id->p_audio_data = (void *)( intptr_t )atoll( psz_tmp );
307     free( psz_tmp );
308
309     id->format = p_fmt;
310     id->format->audio.i_bitspersample = i_bits_per_sample;
311     return id;
312 }
313
314 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
315 {
316     VLC_UNUSED( p_stream );
317     if ( id != NULL )
318         free( id );
319     return VLC_SUCCESS;
320 }
321
322 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
323                  block_t *p_buffer )
324 {
325     if ( id->format->i_cat == VIDEO_ES )
326         return SendVideo( p_stream, id, p_buffer );
327     else if ( id->format->i_cat == AUDIO_ES )
328         return SendAudio( p_stream, id, p_buffer );
329     return VLC_SUCCESS;
330 }
331
332 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_t *id,
333                       block_t *p_buffer )
334 {
335     sout_stream_sys_t *p_sys = p_stream->p_sys;
336     int i_line, i_line_size, i_size, i_pixel_pitch;
337     uint8_t* p_pixels;
338
339     i_line = id->format->video.i_height;
340     i_pixel_pitch = id->format->video.i_bits_per_pixel / 8;
341     i_line_size = i_pixel_pitch * id->format->video.i_width;
342     i_size = i_line * i_line_size;
343     /* Calling the prerender callback to get user buffer */
344     p_sys->pf_video_prerender_callback( id->p_video_data, &p_pixels , i_size );
345     /* Copying data into user buffer */
346     for ( int line = 0; line < i_line; line++, p_pixels += i_line_size )
347         vlc_memcpy( p_pixels, p_buffer->p_buffer + i_line_size * line , i_line_size );
348     /* Calling the postrender callback to tell the user his buffer is ready */
349     p_sys->pf_video_postrender_callback( id->p_video_data, p_pixels,
350                                          id->format->video.i_width, id->format->video.i_height,
351                                          id->format->video.i_bits_per_pixel, i_size, p_buffer->i_pts );
352     block_ChainRelease( p_buffer );
353     return VLC_SUCCESS;
354 }
355
356 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_t *id,
357                       block_t *p_buffer )
358 {
359     sout_stream_sys_t *p_sys = p_stream->p_sys;
360     int i_size;
361     uint8_t* p_pcm_buffer;
362     int i_samples = 0;
363
364     i_size = p_buffer->i_buffer;
365     i_samples = i_size / ( ( id->format->audio.i_bitspersample / 8 ) * id->format->audio.i_channels );
366     /* Calling the prerender callback to get user buffer */
367     p_sys->pf_audio_prerender_callback( id->p_audio_data, &p_pcm_buffer, i_size );
368     /* Copying data into user buffer */
369     vlc_memcpy( p_pcm_buffer, p_buffer->p_buffer, i_size );
370     /* Calling the postrender callback to tell the user his buffer is ready */
371     p_sys->pf_audio_postrender_callback( id->p_audio_data, p_pcm_buffer,
372                                          id->format->audio.i_channels, id->format->audio.i_rate, p_buffer->i_samples,
373                                          id->format->audio.i_bitspersample, i_size, p_buffer->i_pts );
374     block_ChainRelease( p_buffer );
375     return VLC_SUCCESS;
376 }