]> git.sesse.net Git - vlc/blob - modules/stream_out/smem.c
avcodec: option name is avcodec-hw
[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 #include <vlc_aout.h>
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60
61 #define T_VIDEO_PRERENDER_CALLBACK N_( "Video prerender callback" )
62 #define LT_VIDEO_PRERENDER_CALLBACK N_( "Address of the video prerender callback function. " \
63                                 "This function will set the buffer where render will be done." )
64
65 #define T_AUDIO_PRERENDER_CALLBACK N_( "Audio prerender callback" )
66 #define LT_AUDIO_PRERENDER_CALLBACK N_( "Address of the audio prerender callback function. " \
67                                         "This function will set the buffer where render will be done." )
68
69 #define T_VIDEO_POSTRENDER_CALLBACK N_( "Video postrender callback" )
70 #define LT_VIDEO_POSTRENDER_CALLBACK N_( "Address of the video postrender callback function. " \
71                                         "This function will be called when the render is into the buffer." )
72
73 #define T_AUDIO_POSTRENDER_CALLBACK N_( "Audio postrender callback" )
74 #define LT_AUDIO_POSTRENDER_CALLBACK N_( "Address of the audio postrender callback function. " \
75                                         "This function will be called when the render is into the buffer." )
76
77 #define T_VIDEO_DATA N_( "Video Callback data" )
78 #define LT_VIDEO_DATA N_( "Data for the video callback function." )
79
80 #define T_AUDIO_DATA N_( "Audio callback data" )
81 #define LT_AUDIO_DATA N_( "Data for the audio callback function." )
82
83 #define T_TIME_SYNC N_( "Time Synchronized output" )
84 #define LT_TIME_SYNC N_( "Time Synchronisation option for output. " \
85                         "If true, stream will render as usual, else " \
86                         "it will be rendered as fast as possible.")
87
88 static int  Open ( vlc_object_t * );
89 static void Close( vlc_object_t * );
90
91 #define SOUT_CFG_PREFIX "sout-smem-"
92 #define SOUT_PREFIX_VIDEO SOUT_CFG_PREFIX"video-"
93 #define SOUT_PREFIX_AUDIO SOUT_CFG_PREFIX"audio-"
94
95 vlc_module_begin ()
96     set_shortname( N_("Smem"))
97     set_description( N_("Stream output to memory buffer") )
98     set_capability( "sout stream", 0 )
99     add_shortcut( "smem" )
100     set_category( CAT_SOUT )
101     set_subcategory( SUBCAT_SOUT_STREAM )
102     add_string( SOUT_PREFIX_VIDEO "prerender-callback", "0", T_VIDEO_PRERENDER_CALLBACK, LT_VIDEO_PRERENDER_CALLBACK, true )
103         change_volatile()
104     add_string( SOUT_PREFIX_AUDIO "prerender-callback", "0", T_AUDIO_PRERENDER_CALLBACK, LT_AUDIO_PRERENDER_CALLBACK, true )
105         change_volatile()
106     add_string( SOUT_PREFIX_VIDEO "postrender-callback", "0", T_VIDEO_POSTRENDER_CALLBACK, LT_VIDEO_POSTRENDER_CALLBACK, true )
107         change_volatile()
108     add_string( SOUT_PREFIX_AUDIO "postrender-callback", "0", T_AUDIO_POSTRENDER_CALLBACK, LT_AUDIO_POSTRENDER_CALLBACK, true )
109         change_volatile()
110     add_string( SOUT_PREFIX_VIDEO "data", "0", T_VIDEO_DATA, LT_VIDEO_DATA, true )
111         change_volatile()
112     add_string( SOUT_PREFIX_AUDIO "data", "0", T_AUDIO_DATA, LT_VIDEO_DATA, true )
113         change_volatile()
114     add_bool( SOUT_CFG_PREFIX "time-sync", true, T_TIME_SYNC, LT_TIME_SYNC, true )
115         change_private()
116     set_callbacks( Open, Close )
117 vlc_module_end ()
118
119
120 /*****************************************************************************
121  * Exported prototypes
122  *****************************************************************************/
123 static const char *const ppsz_sout_options[] = {
124     "video-prerender-callback", "audio-prerender-callback",
125     "video-postrender-callback", "audio-postrender-callback", "video-data", "audio-data", "time-sync", NULL
126 };
127
128 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
129 static int               Del ( sout_stream_t *, sout_stream_id_t * );
130 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
131
132 static sout_stream_id_t *AddVideo( sout_stream_t *p_stream, es_format_t *p_fmt );
133 static sout_stream_id_t *AddAudio( sout_stream_t *p_stream, es_format_t *p_fmt );
134
135 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_t *id,
136                       block_t *p_buffer );
137 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_t *id,
138                       block_t *p_buffer );
139
140 struct sout_stream_id_t
141 {
142     es_format_t* format;
143     void *p_data;
144 };
145
146 struct sout_stream_sys_t
147 {
148     vlc_mutex_t *p_lock;
149     void ( *pf_video_prerender_callback ) ( void* p_video_data, uint8_t** pp_pixel_buffer , int size );
150     void ( *pf_audio_prerender_callback ) ( void* p_audio_data, uint8_t** pp_pcm_buffer , unsigned int size );
151     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 );
152     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 );
153     bool time_sync;
154 };
155
156 /*****************************************************************************
157  * Open:
158  *****************************************************************************/
159 static int Open( vlc_object_t *p_this )
160 {
161     char* psz_tmp;
162     sout_stream_t *p_stream = (sout_stream_t*)p_this;
163     sout_stream_sys_t *p_sys;
164
165     p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
166     if( !p_sys )
167         return VLC_ENOMEM;
168     p_stream->p_sys = p_sys;
169
170     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
171                        p_stream->p_cfg );
172
173     p_sys->time_sync = var_GetBool( p_stream, SOUT_CFG_PREFIX "time-sync" );
174
175     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "prerender-callback" );
176     p_sys->pf_video_prerender_callback = (void (*) (void *, uint8_t**, int))(intptr_t)atoll( psz_tmp );
177     free( psz_tmp );
178
179     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "prerender-callback" );
180     p_sys->pf_audio_prerender_callback = (void (*) (void* , uint8_t**, unsigned int))(intptr_t)atoll( psz_tmp );
181     free( psz_tmp );
182
183     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "postrender-callback" );
184     p_sys->pf_video_postrender_callback = (void (*) (void*, uint8_t*, int, int, int, int, mtime_t))(intptr_t)atoll( psz_tmp );
185     free( psz_tmp );
186
187     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "postrender-callback" );
188     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 );
189     free( psz_tmp );
190
191     /* Setting stream out module callbacks */
192     p_stream->pf_add    = Add;
193     p_stream->pf_del    = Del;
194     p_stream->pf_send   = Send;
195     p_stream->pace_nocontrol = p_sys->time_sync;
196
197     return VLC_SUCCESS;
198 }
199
200 /*****************************************************************************
201  * Close:
202  *****************************************************************************/
203 static void Close( vlc_object_t * p_this )
204 {
205     sout_stream_t *p_stream = (sout_stream_t*)p_this;
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         case VLC_CODEC_RGBA:
230             i_bits_per_pixel = 32;
231             break;
232         case VLC_CODEC_I444:
233         case VLC_CODEC_RGB24:
234             i_bits_per_pixel = 24;
235             break;
236         case VLC_CODEC_RGB16:
237         case VLC_CODEC_RGB15:
238         case VLC_CODEC_RGB8:
239         case VLC_CODEC_I422:
240             i_bits_per_pixel = 16;
241             break;
242         case VLC_CODEC_YV12:
243         case VLC_CODEC_I420:
244             i_bits_per_pixel = 12;
245             break;
246         case VLC_CODEC_RGBP:
247             i_bits_per_pixel = 8;
248             break;
249         default:
250             i_bits_per_pixel = 0;
251             msg_Dbg( p_stream, "non raw video format detected (%4.4s), buffers will contain compressed video", (char *)&p_fmt->i_codec );
252             break;
253     }
254
255     id = calloc( 1, sizeof( sout_stream_id_t ) );
256     if( !id )
257         return NULL;
258
259     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_VIDEO "data" );
260     id->p_data = (void *)( intptr_t )atoll( psz_tmp );
261     free( psz_tmp );
262
263     id->format = p_fmt;
264     id->format->video.i_bits_per_pixel = i_bits_per_pixel;
265     return id;
266 }
267
268 static sout_stream_id_t *AddAudio( sout_stream_t *p_stream, es_format_t *p_fmt )
269 {
270     char* psz_tmp;
271     sout_stream_id_t* id;
272     int i_bits_per_sample = aout_BitsPerSample( p_fmt->i_codec );
273
274     if( !i_bits_per_sample )
275     {
276         msg_Err( p_stream, "Smem does only support raw audio format" );
277         return NULL;
278     }
279
280     id = calloc( 1, sizeof( sout_stream_id_t ) );
281     if( !id )
282         return NULL;
283
284     psz_tmp = var_GetString( p_stream, SOUT_PREFIX_AUDIO "data" );
285     id->p_data = (void *)( intptr_t )atoll( psz_tmp );
286     free( psz_tmp );
287
288     id->format = p_fmt;
289     id->format->audio.i_bitspersample = i_bits_per_sample;
290     return id;
291 }
292
293 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
294 {
295     VLC_UNUSED( p_stream );
296     free( id );
297     return VLC_SUCCESS;
298 }
299
300 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
301                  block_t *p_buffer )
302 {
303     if ( id->format->i_cat == VIDEO_ES )
304         return SendVideo( p_stream, id, p_buffer );
305     else if ( id->format->i_cat == AUDIO_ES )
306         return SendAudio( p_stream, id, p_buffer );
307     return VLC_SUCCESS;
308 }
309
310 static int SendVideo( sout_stream_t *p_stream, sout_stream_id_t *id,
311                       block_t *p_buffer )
312 {
313     sout_stream_sys_t *p_sys = p_stream->p_sys;
314     int i_line, i_line_size, i_size, i_pixel_pitch;
315     uint8_t* p_pixels = NULL;
316
317     if( id->format->video.i_bits_per_pixel > 0 )
318     {
319         i_line = id->format->video.i_height;
320         i_pixel_pitch = id->format->video.i_bits_per_pixel / 8;
321         i_line_size = i_pixel_pitch * id->format->video.i_width;
322         i_size = i_line * i_line_size;
323     }
324     else
325     {
326         i_size = p_buffer->i_buffer;
327     }
328     /* Calling the prerender callback to get user buffer */
329     p_sys->pf_video_prerender_callback( id->p_data, &p_pixels , i_size );
330
331     if (!p_pixels)
332     {
333         msg_Err( p_stream, "No buffer given!" );
334         block_ChainRelease( p_buffer );
335         return VLC_EGENERIC;
336     }
337
338     /* Copying data into user buffer */
339     if( id->format->video.i_bits_per_pixel > 0 )
340     {
341         uint8_t *p_in = p_buffer->p_buffer;
342         uint8_t *p_out = p_pixels;
343
344         for ( int line = 0; line < i_line; line++ )
345         {
346             memcpy( p_out, p_in, i_line_size );
347             p_out += i_line_size;
348             p_in += i_line_size;
349         }
350     }
351     else
352     {
353         memcpy( p_pixels, p_buffer->p_buffer, i_size );
354     }
355     /* Calling the postrender callback to tell the user his buffer is ready */
356     p_sys->pf_video_postrender_callback( id->p_data, p_pixels,
357                                          id->format->video.i_width, id->format->video.i_height,
358                                          id->format->video.i_bits_per_pixel, i_size, p_buffer->i_pts );
359     block_ChainRelease( p_buffer );
360     return VLC_SUCCESS;
361 }
362
363 static int SendAudio( sout_stream_t *p_stream, sout_stream_id_t *id,
364                       block_t *p_buffer )
365 {
366     sout_stream_sys_t *p_sys = p_stream->p_sys;
367     int i_size;
368     uint8_t* p_pcm_buffer = NULL;
369     int i_samples = 0;
370
371     i_size = p_buffer->i_buffer;
372     if (id->format->audio.i_channels <= 0)
373     {
374         msg_Warn( p_stream, "No buffer given!" );
375         block_ChainRelease( p_buffer );
376         return VLC_EGENERIC;
377     }
378
379     i_samples = i_size / ( ( id->format->audio.i_bitspersample / 8 ) * id->format->audio.i_channels );
380     /* Calling the prerender callback to get user buffer */
381     p_sys->pf_audio_prerender_callback( id->p_data, &p_pcm_buffer, i_size );
382     if (!p_pcm_buffer)
383     {
384         msg_Err( p_stream, "No buffer given!" );
385         block_ChainRelease( p_buffer );
386         return VLC_EGENERIC;
387     }
388
389     /* Copying data into user buffer */
390     memcpy( p_pcm_buffer, p_buffer->p_buffer, i_size );
391     /* Calling the postrender callback to tell the user his buffer is ready */
392     p_sys->pf_audio_postrender_callback( id->p_data, p_pcm_buffer,
393                                          id->format->audio.i_channels, id->format->audio.i_rate, i_samples,
394                                          id->format->audio.i_bitspersample, i_size, p_buffer->i_pts );
395     block_ChainRelease( p_buffer );
396     return VLC_SUCCESS;
397 }
398