]> git.sesse.net Git - vlc/blob - modules/video_output/encoder.c
f11a9441f0fd6b859b585e9572bf7cc06c74ab74
[vlc] / modules / video_output / encoder.c
1 /*****************************************************************************
2  * encoder.c :  encoder wrapper plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: encoder.c,v 1.1 2003/01/22 10:41:57 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31 #include <vlc/input.h>
32
33 #include "encoder.h"
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Create    ( vlc_object_t * );
39 static void Destroy   ( vlc_object_t * );
40
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 * );
46
47 static void SetPalette( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 vlc_module_begin();
53     set_description( _("Encoder wrapper module") );
54     set_capability( "video output", 0 );
55     set_callbacks( Create, Destroy );
56     add_shortcut( "encoder" );
57 vlc_module_end();
58
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  *****************************************************************************/
65 struct vout_sys_t
66 {
67     vlc_fourcc_t    i_codec;
68     video_encoder_t *p_encoder;
69
70     int             i_buffer;
71     void            *p_buffer;
72
73     input_thread_t  *p_input;
74     es_descriptor_t *p_es;
75 };
76
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 )
83 {
84     vout_thread_t   *p_vout = (vout_thread_t *)p_this;
85     char *psz_sout;
86     char *psz_sout_vcodec;
87     vlc_fourcc_t i_codec;
88
89     psz_sout = config_GetPsz( p_vout, "sout" );
90     if( !psz_sout || !*psz_sout )
91     {
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 );
95         return VLC_EGENERIC;
96     }
97     free( psz_sout );
98
99     psz_sout_vcodec = config_GetPsz( p_vout, "sout-vcodec" );
100     if( !psz_sout_vcodec || !*psz_sout_vcodec )
101     {
102         msg_Err( p_vout, "you have to specify a video codec using sout-vcodec" );
103         if( psz_sout_vcodec ) free( psz_sout_vcodec );
104         return VLC_EGENERIC;
105     }
106     if( !strcmp( psz_sout_vcodec, "mpeg4" ) )
107     {
108         i_codec  = VLC_FOURCC( 'm', 'p', '4', 'v' );
109     }
110     else if( !strcmp( psz_sout_vcodec, "mpeg2" ) )
111     {
112         i_codec  = VLC_FOURCC( 'm', 'p', '2', 'v' );
113     }
114     else if( !strcmp( psz_sout_vcodec, "mpeg1" ) )
115     {
116         i_codec  = VLC_FOURCC( 'm', 'p', '1', 'v' );
117     }
118     else
119     {
120         int i;
121         int c[4];
122         msg_Warn( p_vout, "unknown codec %s used as a fourcc", psz_sout_vcodec );
123         for( i = 0; i < 4; i++ )
124         {
125             if( psz_sout_vcodec[i] )
126                 c[i] = psz_sout_vcodec[i];
127             else
128                 c[i] = ' ';
129         }
130         i_codec = VLC_FOURCC( c[0], c[1], c[2], c[3] );
131     }
132     free( psz_sout_vcodec );
133
134     /* Allocate instance and initialize some members */
135     if( !( p_vout->p_sys = malloc( sizeof( vout_sys_t ) ) ) )
136     {
137         return VLC_ENOMEM;
138     }
139     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
140
141     /* *** save parameters *** */
142     p_vout->p_sys->i_codec = i_codec;
143
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;
150
151     return VLC_SUCCESS;
152 }
153
154 /*****************************************************************************
155  * Init: initialize video thread
156  *****************************************************************************/
157 static int Init( vout_thread_t *p_vout )
158 {
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;
162
163     int i_index;
164     picture_t *p_pic;
165
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 ) );
169
170     /* *** set wanted input format *** */
171     p_encoder->i_codec  = p_vout->p_sys->i_codec;
172
173     /* *** set prefered 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;
180
181     /* *** requuest this module *** */
182     p_encoder->p_module = module_Need( p_encoder,
183                                        "video encoder",
184                                        "$video-encoder" );
185     if( !p_encoder->p_module )
186     {
187         msg_Warn( p_vout,
188                   "no suitable encoder to %4.4s",
189                   (char*)&p_encoder->i_codec );
190         vlc_object_destroy( p_encoder );
191         return VLC_EGENERIC;
192     }
193
194     /* *** init the codec *** */
195     if( p_encoder->pf_init( p_encoder ) )
196     {
197         msg_Err( p_vout, "failed to initialize video encoder plugin" );
198         vlc_object_destroy( p_encoder );
199         return VLC_EGENERIC;
200     }
201
202     /* *** alloacted buffer *** */
203     if( p_encoder->i_buffer_size <= 0 )
204     {
205         p_encoder->i_buffer_size = 10 * p_encoder->i_width * p_encoder->i_height;
206     }
207     p_sys->i_buffer = p_encoder->i_buffer_size;
208     if( !( p_sys->p_buffer = malloc( p_encoder->i_buffer_size ) ) )
209     {
210         msg_Err( p_vout, "out of memory" );
211         return VLC_ENOMEM;
212     }
213
214     /* *** create a new standalone ES *** */
215     /* find a p_input  */
216     p_sys->p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_ANYWHERE );
217     if( !p_sys->p_input )
218     {
219         msg_Err( p_vout, "cannot find p_input" );
220         return VLC_EGENERIC;
221     }
222
223     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
224
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 );
229
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 */
233                                12,   /* es_id */
234                                0 );  /* no extra data */
235     if( !p_sys->p_es )
236     {
237         msg_Err( p_vout, "cannot create es" );
238         vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
239         return VLC_EGENERIC;
240     }
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 ) )
245     {
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" );
249         return VLC_EGENERIC;
250     }
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 );
254
255
256     I_OUTPUTPICTURES = 0;
257
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;
264
265     /* Try to initialize 1 direct buffer */
266     p_pic = NULL;
267
268     /* Find an empty picture slot */
269     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
270     {
271         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
272         {
273             p_pic = p_vout->p_picture + i_index;
274             break;
275         }
276     }
277
278     /* Allocate the picture */
279     if( p_pic == NULL )
280     {
281         return VLC_SUCCESS;
282     }
283
284     vout_AllocatePicture( p_vout, p_pic, p_vout->output.i_width,
285                           p_vout->output.i_height,
286                           p_vout->output.i_chroma );
287
288     if( p_pic->i_planes == 0 )
289     {
290         return VLC_SUCCESS;
291     }
292
293     p_pic->i_status = DESTROYED_PICTURE;
294     p_pic->i_type   = DIRECT_PICTURE;
295
296     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
297
298     I_OUTPUTPICTURES++;
299
300     return VLC_SUCCESS;
301 }
302
303 /*****************************************************************************
304  * End: terminate video thread
305  *****************************************************************************/
306 static void End( vout_thread_t *p_vout )
307 {
308     vout_sys_t      *p_sys = p_vout->p_sys;
309     video_encoder_t *p_encoder = p_vout->p_sys->p_encoder;
310
311     /* *** stop encoder *** */
312     p_encoder->pf_end( p_encoder );
313
314     vlc_object_release( p_sys->p_input );
315
316     /* *** unload encoder plugin *** */
317     module_Unneed( p_encoder,
318                    p_encoder->p_module );
319     vlc_object_destroy( p_encoder );
320
321     free( p_sys->p_buffer );
322 }
323
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 )
330 {
331     vout_thread_t *p_vout = (vout_thread_t *)p_this;
332
333     /* Destroy structure */
334     free( p_vout->p_sys );
335     p_vout->p_sys = NULL;
336 }
337
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 )
345 {
346     return VLC_SUCCESS;
347 }
348
349 /*****************************************************************************
350  * Render:
351  *****************************************************************************
352  *
353  *****************************************************************************/
354 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
355 {
356 }
357
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 )
364 {
365     vout_sys_t *p_sys = p_vout->p_sys;
366     video_encoder_t *p_encoder = p_vout->p_sys->p_encoder;
367
368     int     i_err;
369     size_t  i_data;
370
371     i_data = p_sys->i_buffer;
372     i_err  = p_encoder->pf_encode( p_encoder,
373                                    p_pic,
374                                    p_sys->p_buffer,
375                                    &i_data );
376     if( i_err )
377     {
378         msg_Err( p_vout, "failed to encode a frame (err:0x%x)", i_err );
379         return;
380     }
381
382     if( i_data > 0 && p_sys->p_es->p_decoder_fifo )
383     {
384         pes_packet_t *p_pes;
385         data_packet_t   *p_data;
386
387         if( !( p_pes = input_NewPES( p_sys->p_input->p_method_data ) ) )
388         {
389             msg_Err( p_vout, "cannot allocate new PES" );
390             return;
391         }
392         if( !( p_data = input_NewPacket( p_sys->p_input->p_method_data, i_data ) ) )
393         {
394             msg_Err( p_vout, "cannot allocate new data_packet" );
395             return;
396         }
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;
402
403         p_vout->p_vlc->pf_memcpy( p_data->p_payload_start,
404                                   p_sys->p_buffer,
405                                   i_data );
406
407         input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
408     }
409
410 }
411
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 )
418 {
419     ;
420 }
421