1 /*****************************************************************************
2 * encoder.c : encoder wrapper plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: encoder.c,v 1.2 2003/01/28 16:57:28 sam Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc/input.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Create ( vlc_object_t * );
39 static void Destroy ( vlc_object_t * );
41 static int Init ( vout_thread_t * );
42 static void End ( vout_thread_t * );
43 static int Manage ( vout_thread_t * );
44 static void Render ( vout_thread_t *, picture_t * );
45 static void Display ( vout_thread_t *, picture_t * );
47 static void SetPalette( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
49 /*****************************************************************************
51 *****************************************************************************/
53 set_description( _("Encoder wrapper module") );
54 set_capability( "video output", 0 );
55 set_callbacks( Create, Destroy );
56 add_shortcut( "encoder" );
59 /*****************************************************************************
60 * vout_sys_t: video output descriptor
61 *****************************************************************************
62 * This structure is part of the video output thread descriptor.
63 * It describes the SVGAlib specific properties of an output thread.
64 *****************************************************************************/
68 video_encoder_t *p_encoder;
73 input_thread_t *p_input;
74 es_descriptor_t *p_es;
77 /*****************************************************************************
78 * Create: allocates video thread
79 *****************************************************************************
80 * This function allocates and initializes a vout method.
81 *****************************************************************************/
82 static int Create( vlc_object_t *p_this )
84 vout_thread_t *p_vout = (vout_thread_t *)p_this;
86 char *psz_sout_vcodec;
89 psz_sout = config_GetPsz( p_vout, "sout" );
90 if( !psz_sout || !*psz_sout )
92 /* avoid bad infinite loop */
93 msg_Err( p_vout, "encoder video output should be used only in sout mode" );
94 if( psz_sout ) free( psz_sout );
99 psz_sout_vcodec = config_GetPsz( p_vout, "sout-vcodec" );
100 if( !psz_sout_vcodec || !*psz_sout_vcodec )
102 msg_Err( p_vout, "you have to specify a video codec using sout-vcodec" );
103 if( psz_sout_vcodec ) free( psz_sout_vcodec );
106 if( !strcmp( psz_sout_vcodec, "mpeg4" ) )
108 i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
110 else if( !strcmp( psz_sout_vcodec, "mpeg2" ) )
112 i_codec = VLC_FOURCC( 'm', 'p', '2', 'v' );
114 else if( !strcmp( psz_sout_vcodec, "mpeg1" ) )
116 i_codec = VLC_FOURCC( 'm', 'p', '1', 'v' );
122 msg_Warn( p_vout, "unknown codec %s used as a fourcc", psz_sout_vcodec );
123 for( i = 0; i < 4; i++ )
125 if( psz_sout_vcodec[i] )
126 c[i] = psz_sout_vcodec[i];
130 i_codec = VLC_FOURCC( c[0], c[1], c[2], c[3] );
132 free( psz_sout_vcodec );
134 /* Allocate instance and initialize some members */
135 if( !( p_vout->p_sys = malloc( sizeof( vout_sys_t ) ) ) )
139 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
141 /* *** save parameters *** */
142 p_vout->p_sys->i_codec = i_codec;
144 /* *** set exported functions *** */
145 p_vout->pf_init = Init;
146 p_vout->pf_end = End;
147 p_vout->pf_render = Render;
148 p_vout->pf_manage = Manage;
149 p_vout->pf_display = Display;
154 /*****************************************************************************
155 * Init: initialize video thread
156 *****************************************************************************/
157 static int Init( vout_thread_t *p_vout )
159 vout_sys_t *p_sys = p_vout->p_sys;
160 video_encoder_t *p_encoder = p_vout->p_sys->p_encoder;
161 char *psz_sout_vcodec;
166 /* *** create a video encoder object *** */
167 p_vout->p_sys->p_encoder =
168 p_encoder = vlc_object_create( p_vout, sizeof( video_encoder_t ) );
170 /* *** set wanted input format *** */
171 p_encoder->i_codec = p_vout->p_sys->i_codec;
173 /* *** set preferred properties *** */
174 /* encoder can modify all these values except i_codec */
175 p_encoder->i_chroma = p_vout->render.i_chroma;
176 p_encoder->i_width = p_vout->render.i_width;
177 p_encoder->i_height = p_vout->render.i_height;
178 p_encoder->i_aspect = p_vout->render.i_aspect;
179 p_encoder->i_buffer_size = 0;
181 /* *** requuest this module *** */
182 p_encoder->p_module = module_Need( p_encoder,
185 if( !p_encoder->p_module )
188 "no suitable encoder to %4.4s",
189 (char*)&p_encoder->i_codec );
190 vlc_object_destroy( p_encoder );
194 /* *** init the codec *** */
195 if( p_encoder->pf_init( p_encoder ) )
197 msg_Err( p_vout, "failed to initialize video encoder plugin" );
198 vlc_object_destroy( p_encoder );
202 /* *** alloacted buffer *** */
203 if( p_encoder->i_buffer_size <= 0 )
205 p_encoder->i_buffer_size = 10 * p_encoder->i_width * p_encoder->i_height;
207 p_sys->i_buffer = p_encoder->i_buffer_size;
208 if( !( p_sys->p_buffer = malloc( p_encoder->i_buffer_size ) ) )
210 msg_Err( p_vout, "out of memory" );
214 /* *** create a new standalone ES *** */
216 p_sys->p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_ANYWHERE );
217 if( !p_sys->p_input )
219 msg_Err( p_vout, "cannot find p_input" );
223 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
225 /* avoid bad loop (else output will also be reencoded until it segfault :p*/
226 /* XXX do it after the lock (if we have multiple video stream...) */
227 psz_sout_vcodec = config_GetPsz( p_vout, "sout-vcodec" );
228 config_PutPsz( p_vout, "sout-vcodec", NULL );
230 /* add a new stream */
231 p_sys->p_es = input_AddES( p_sys->p_input,
232 NULL, /* we aren't attached to a program */
234 0 ); /* no extra data */
237 msg_Err( p_vout, "cannot create es" );
238 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
241 p_sys->p_es->i_stream_id = 1;
242 p_sys->p_es->i_fourcc = p_encoder->i_codec;
243 p_sys->p_es->i_cat = VIDEO_ES;
244 if( input_SelectES( p_sys->p_input, p_sys->p_es ) )
246 input_DelES( p_sys->p_input, p_sys->p_es );
247 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
248 msg_Err( p_vout, "cannot select es" );
251 /* restore value as we could have multiple video stream (have you a 42*12 GHz ?) */
252 config_PutPsz( p_vout, "sout-vcodec", psz_sout_vcodec );
253 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
256 I_OUTPUTPICTURES = 0;
258 p_vout->output.pf_setpalette = SetPalette;
259 /* remember that this value could have been modified by encoder */
260 p_vout->output.i_chroma = p_vout->p_sys->p_encoder->i_chroma;
261 p_vout->output.i_width = p_vout->p_sys->p_encoder->i_width;
262 p_vout->output.i_height = p_vout->p_sys->p_encoder->i_height;
263 p_vout->output.i_aspect = p_vout->p_sys->p_encoder->i_aspect;
265 /* Try to initialize 1 direct buffer */
268 /* Find an empty picture slot */
269 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
271 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
273 p_pic = p_vout->p_picture + i_index;
278 /* Allocate the picture */
284 vout_AllocatePicture( p_vout, p_pic, p_vout->output.i_width,
285 p_vout->output.i_height,
286 p_vout->output.i_chroma );
288 if( p_pic->i_planes == 0 )
293 p_pic->i_status = DESTROYED_PICTURE;
294 p_pic->i_type = DIRECT_PICTURE;
296 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
303 /*****************************************************************************
304 * End: terminate video thread
305 *****************************************************************************/
306 static void End( vout_thread_t *p_vout )
308 vout_sys_t *p_sys = p_vout->p_sys;
309 video_encoder_t *p_encoder = p_vout->p_sys->p_encoder;
311 /* *** stop encoder *** */
312 p_encoder->pf_end( p_encoder );
314 vlc_object_release( p_sys->p_input );
316 /* *** unload encoder plugin *** */
317 module_Unneed( p_encoder,
318 p_encoder->p_module );
319 vlc_object_destroy( p_encoder );
321 free( p_sys->p_buffer );
324 /*****************************************************************************
325 * Destroy: destroy video thread
326 *****************************************************************************
327 * Terminate an output method created by Create
328 *****************************************************************************/
329 static void Destroy( vlc_object_t *p_this )
331 vout_thread_t *p_vout = (vout_thread_t *)p_this;
333 /* Destroy structure */
334 free( p_vout->p_sys );
335 p_vout->p_sys = NULL;
338 /*****************************************************************************
339 * Manage: handle events
340 *****************************************************************************
341 * This function should be called regularly by video output thread. It manages
342 * console events. It returns a non null value on error.
343 *****************************************************************************/
344 static int Manage( vout_thread_t *p_vout )
349 /*****************************************************************************
351 *****************************************************************************
353 *****************************************************************************/
354 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
358 /*****************************************************************************
359 * Display: displays previously rendered output
360 *****************************************************************************
361 * This function sends the currently rendered image to the VGA card.
362 *****************************************************************************/
363 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
365 vout_sys_t *p_sys = p_vout->p_sys;
366 video_encoder_t *p_encoder = p_vout->p_sys->p_encoder;
371 i_data = p_sys->i_buffer;
372 i_err = p_encoder->pf_encode( p_encoder,
378 msg_Err( p_vout, "failed to encode a frame (err:0x%x)", i_err );
382 if( i_data > 0 && p_sys->p_es->p_decoder_fifo )
385 data_packet_t *p_data;
387 if( !( p_pes = input_NewPES( p_sys->p_input->p_method_data ) ) )
389 msg_Err( p_vout, "cannot allocate new PES" );
392 if( !( p_data = input_NewPacket( p_sys->p_input->p_method_data, i_data ) ) )
394 msg_Err( p_vout, "cannot allocate new data_packet" );
397 p_pes->i_dts = p_pic->date;
398 p_pes->i_pts = p_pic->date;
399 p_pes->p_first = p_pes->p_last = p_data;
400 p_pes->i_nb_data = 1;
401 p_pes->i_pes_size = i_data;
403 p_vout->p_vlc->pf_memcpy( p_data->p_payload_start,
407 input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
412 /*****************************************************************************
413 * SetPalette: set a 8bpp palette
414 *****************************************************************************
415 * TODO: support 8 bits clut (for Mach32 cards and others).
416 *****************************************************************************/
417 static void SetPalette( vout_thread_t *p_vout, uint16_t *red, uint16_t *green, uint16_t *blue )