]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode.c
* modules/codec/ffmpeg/video_filter.c, include/vlc_filter.h:
[vlc] / modules / stream_out / transcode.c
1 /*****************************************************************************
2  * transcode.c: transcoding stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/sout.h>
34 #include <vlc/vout.h>
35 #include <vlc/decoder.h>
36 #include "vlc_filter.h"
37 #include "osd.h"
38
39 /* ffmpeg header */
40 #ifdef HAVE_FFMPEG_AVCODEC_H
41 #   include <ffmpeg/avcodec.h>
42 #else
43 #   include <avcodec.h>
44 #endif
45
46 #if LIBAVCODEC_BUILD < 4704
47 #   define AV_NOPTS_VALUE 0
48 #endif
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 #define VENC_TEXT N_("Video encoder")
54 #define VENC_LONGTEXT N_( \
55     "Allows you to specify the video encoder to use and its associated " \
56     "options." )
57 #define VCODEC_TEXT N_("Destination video codec")
58 #define VCODEC_LONGTEXT N_( \
59     "Allows you to specify the destination video codec used for the " \
60     "streaming output." )
61 #define VB_TEXT N_("Video bitrate")
62 #define VB_LONGTEXT N_( \
63     "Allows you to specify the video bitrate used for the streaming " \
64     "output." )
65 #define SCALE_TEXT N_("Video scaling")
66 #define SCALE_LONGTEXT N_( \
67     "Allows you to scale the video before encoding." )
68 #define FPS_TEXT N_("Video frame-rate")
69 #define FPS_LONGTEXT N_( \
70     "Allows you to specify an output frame rate for the video." )
71 #define DEINTERLACE_TEXT N_("Deinterlace video")
72 #define DEINTERLACE_LONGTEXT N_( \
73     "Allows you to deinterlace the video before encoding." )
74 #define WIDTH_TEXT N_("Video width")
75 #define WIDTH_LONGTEXT N_( \
76     "Allows you to specify the output video width." )
77 #define HEIGHT_TEXT N_("Video height")
78 #define HEIGHT_LONGTEXT N_( \
79     "Allows you to specify the output video height." )
80
81 #define CROPTOP_TEXT N_("Video crop top")
82 #define CROPTOP_LONGTEXT N_( \
83     "Allows you to specify the top coordinate for the video cropping." )
84 #define CROPLEFT_TEXT N_("Video crop left")
85 #define CROPLEFT_LONGTEXT N_( \
86     "Allows you to specify the left coordinate for the video cropping." )
87 #define CROPBOTTOM_TEXT N_("Video crop bottom")
88 #define CROPBOTTOM_LONGTEXT N_( \
89     "Allows you to specify the bottom coordinate for the video cropping." )
90 #define CROPRIGHT_TEXT N_("Video crop right")
91 #define CROPRIGHT_LONGTEXT N_( \
92     "Allows you to specify the right coordinate for the video cropping." )
93
94 #define AENC_TEXT N_("Audio encoder")
95 #define AENC_LONGTEXT N_( \
96     "Allows you to specify the audio encoder to use and its associated " \
97     "options." )
98 #define ACODEC_TEXT N_("Destination audio codec")
99 #define ACODEC_LONGTEXT N_( \
100     "Allows you to specify the destination audio codec used for the " \
101     "streaming output." )
102 #define AB_TEXT N_("Audio bitrate")
103 #define AB_LONGTEXT N_( \
104     "Allows you to specify the audio bitrate used for the streaming " \
105     "output." )
106 #define ARATE_TEXT N_("Audio sample rate")
107 #define ARATE_LONGTEXT N_( \
108     "Allows you to specify the audio sample rate used for the streaming " \
109     "output." )
110 #define ACHANS_TEXT N_("Audio channels")
111 #define ACHANS_LONGTEXT N_( \
112     "Allows you to specify the number of audio channels used for the " \
113     "streaming output." )
114
115 #define SENC_TEXT N_("Subtitles encoder")
116 #define SENC_LONGTEXT N_( \
117     "Allows you to specify the subtitles encoder to use and its associated " \
118     "options." )
119 #define SCODEC_TEXT N_("Destination subtitles codec")
120 #define SCODEC_LONGTEXT N_( \
121     "Allows you to specify the destination subtitles codec used for the " \
122     "streaming output." )
123
124 #define THREADS_TEXT N_("Number of threads")
125 #define THREADS_LONGTEXT N_( \
126     "Allows you to specify the number of threads used for the transcoding." )
127
128 #define ASYNC_TEXT N_("Synchronise on audio track")
129 #define ASYNC_LONGTEXT N_( \
130     "This option will drop/duplicate video frames to synchronise the video " \
131     "track on the audio track." )
132
133 static int  Open ( vlc_object_t * );
134 static void Close( vlc_object_t * );
135
136 #define SOUT_CFG_PREFIX "sout-transcode-"
137
138 vlc_module_begin();
139 #if defined(MODULE_NAME_is_stream_out_transcodealtivec) \
140      || (defined(CAN_COMPILE_ALTIVEC) && !defined(NO_ALTIVEC_IN_FFMPEG))
141     set_description( _("AltiVec transcode stream output") );
142     add_requirement( ALTIVEC );
143     set_capability( "sout stream", 51 );
144 #else
145     set_description( _("Transcode stream output") );
146     set_capability( "sout stream", 50 );
147 #endif
148     add_shortcut( "transcode" );
149     set_callbacks( Open, Close );
150
151     add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
152                 VENC_LONGTEXT, VLC_FALSE );
153     add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
154                 VCODEC_LONGTEXT, VLC_FALSE );
155     add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
156                  VB_LONGTEXT, VLC_FALSE );
157     add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
158                SCALE_LONGTEXT, VLC_FALSE );
159     add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
160                FPS_LONGTEXT, VLC_FALSE );
161     add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
162               DEINTERLACE_LONGTEXT, VLC_FALSE );
163     add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
164                  WIDTH_LONGTEXT, VLC_TRUE );
165     add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
166                  HEIGHT_LONGTEXT, VLC_TRUE );
167
168     add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
169                  CROPTOP_LONGTEXT, VLC_TRUE );
170     add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
171                  CROPLEFT_LONGTEXT, VLC_TRUE );
172     add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
173                  CROPBOTTOM_LONGTEXT, VLC_TRUE );
174     add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
175                  CROPRIGHT_LONGTEXT, VLC_TRUE );
176
177     add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
178                 AENC_LONGTEXT, VLC_FALSE );
179     add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
180                 ACODEC_LONGTEXT, VLC_FALSE );
181     add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
182                  AB_LONGTEXT, VLC_FALSE );
183     add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
184                  ACHANS_LONGTEXT, VLC_FALSE );
185     add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
186                  ARATE_LONGTEXT, VLC_TRUE );
187
188     add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
189                 SENC_LONGTEXT, VLC_FALSE );
190     add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
191                 SCODEC_LONGTEXT, VLC_FALSE );
192     add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
193                SCODEC_LONGTEXT, VLC_FALSE );
194
195     add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
196                  THREADS_LONGTEXT, VLC_TRUE );
197
198     add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
199               ASYNC_LONGTEXT, VLC_FALSE );
200 vlc_module_end();
201
202 static const char *ppsz_sout_options[] = {
203     "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
204     "scale", "fps", "width", "height", "deinterlace", "threads",
205     "aenc", "acodec", "ab", "samplerate", "channels",
206     "senc", "scodec", "soverlay",
207     "audio-sync", NULL
208 };
209
210 /*****************************************************************************
211  * Exported prototypes
212  *****************************************************************************/
213 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
214 static int               Del ( sout_stream_t *, sout_stream_id_t * );
215 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
216
217 static int  transcode_audio_new    ( sout_stream_t *, sout_stream_id_t * );
218 static void transcode_audio_close  ( sout_stream_t *, sout_stream_id_t * );
219 static int  transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
220                                      block_t *, block_t ** );
221
222 static int  transcode_video_new    ( sout_stream_t *, sout_stream_id_t * );
223 static void transcode_video_close  ( sout_stream_t *, sout_stream_id_t * );
224 static int  transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
225 static int  transcode_video_process( sout_stream_t *, sout_stream_id_t *,
226                                      block_t *, block_t ** );
227
228 static picture_t *video_new_buffer( decoder_t * );
229 static void video_del_buffer( decoder_t *, picture_t * );
230 static void video_link_picture( decoder_t *, picture_t * );
231 static void video_unlink_picture( decoder_t *, picture_t * );
232
233 static int  transcode_spu_new    ( sout_stream_t *, sout_stream_id_t * );
234 static void transcode_spu_close  ( sout_stream_t *, sout_stream_id_t * );
235 static int  transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
236                                    block_t *, block_t ** );
237 static subpicture_t *transcode_spu_get( sout_stream_t *, sout_stream_id_t *,
238                                         mtime_t );
239
240 static int  EncoderThread( struct sout_stream_sys_t * p_sys );
241
242 static int pi_channels_maps[6] =
243 {
244     0,
245     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
246     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
247     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
248      | AOUT_CHAN_REARRIGHT,
249     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
250      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
251 };
252
253 #define PICTURE_RING_SIZE 64
254 #define SUBPICTURE_RING_SIZE 20
255
256 struct sout_stream_sys_t
257 {
258     VLC_COMMON_MEMBERS
259
260     sout_stream_t   *p_out;
261     sout_stream_id_t *id_video;
262     block_t         *p_buffers;
263     vlc_mutex_t     lock_out;
264     vlc_cond_t      cond;
265     picture_t *     pp_pics[PICTURE_RING_SIZE];
266     int             i_first_pic, i_last_pic;
267
268     /* Audio */
269     vlc_fourcc_t    i_acodec;   /* codec audio (0 if not transcode) */
270     char            *psz_aenc;
271     sout_cfg_t      *p_audio_cfg;
272     int             i_sample_rate;
273     int             i_channels;
274     int             i_abitrate;
275
276     /* Video */
277     vlc_fourcc_t    i_vcodec;   /* codec video (0 if not transcode) */
278     char            *psz_venc;
279     sout_cfg_t      *p_video_cfg;
280     int             i_vbitrate;
281     double          f_scale;
282     double          f_fps;
283     int             i_width;
284     int             i_height;
285     vlc_bool_t      b_deinterlace;
286     int             i_threads;
287
288     int             i_crop_top;
289     int             i_crop_bottom;
290     int             i_crop_right;
291     int             i_crop_left;
292
293     /* SPU */
294     vlc_fourcc_t    i_scodec;   /* codec spu (0 if not transcode) */
295     char            *psz_senc;
296     vlc_bool_t      b_soverlay;
297     sout_cfg_t      *p_spu_cfg;
298     subpicture_t    *pp_subpics[SUBPICTURE_RING_SIZE];
299
300     /* Filters */
301     filter_t        *p_filter_blend;
302
303     /* Sync */
304     vlc_bool_t      b_audio_sync;
305     mtime_t         i_master_drift;
306 };
307
308 /*****************************************************************************
309  * Open:
310  *****************************************************************************/
311 static int Open( vlc_object_t *p_this )
312 {
313     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
314     sout_stream_sys_t *p_sys;
315     vlc_value_t       val;
316     int               i;
317
318     p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
319
320     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
321     if( !p_sys->p_out )
322     {
323         msg_Err( p_stream, "cannot create chain" );
324         free( p_sys );
325         return VLC_EGENERIC;
326     }
327
328     p_sys->i_master_drift = 0;
329
330     sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
331                    p_stream->p_cfg );
332
333     /* Audio transcoding parameters */
334     var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
335     p_sys->psz_aenc = NULL;
336     p_sys->p_audio_cfg = NULL;
337     if( val.psz_string && *val.psz_string )
338     {
339         char *psz_next;
340         psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
341                                    val.psz_string );
342         if( psz_next ) free( psz_next );
343     }
344     if( val.psz_string ) free( val.psz_string );
345
346     var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
347     p_sys->i_acodec = 0;
348     if( val.psz_string && *val.psz_string )
349     {
350         char fcc[4] = "    ";
351         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
352         p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
353     }
354     if( val.psz_string ) free( val.psz_string );
355
356     var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
357     p_sys->i_abitrate = val.i_int;
358     if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
359
360     var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
361     p_sys->i_sample_rate = val.i_int;
362
363     var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
364     p_sys->i_channels = val.i_int;
365
366     if( p_sys->i_acodec )
367     {
368         msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
369                  (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
370                  p_sys->i_channels, p_sys->i_abitrate / 1000 );
371     }
372
373     /* Video transcoding parameters */
374     var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
375     p_sys->psz_venc = NULL;
376     p_sys->p_video_cfg = NULL;
377     if( val.psz_string && *val.psz_string )
378     {
379         char *psz_next;
380         psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
381                                    val.psz_string );
382         if( psz_next ) free( psz_next );
383     }
384     if( val.psz_string ) free( val.psz_string );
385
386     var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
387     p_sys->i_vcodec = 0;
388     if( val.psz_string && *val.psz_string )
389     {
390         char fcc[4] = "    ";
391         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
392         p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
393     }
394     if( val.psz_string ) free( val.psz_string );
395
396     var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
397     p_sys->i_vbitrate = val.i_int;
398     if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
399
400     var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
401     p_sys->f_scale = val.f_float;
402
403     var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
404     p_sys->f_fps = val.f_float;
405
406     var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
407     p_sys->i_width = val.i_int;
408
409     var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
410     p_sys->i_height = val.i_int;
411
412     var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
413     p_sys->b_deinterlace = val.b_bool;
414
415     var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
416     p_sys->i_crop_top = val.i_int;
417     var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
418     p_sys->i_crop_bottom = val.i_int;
419     var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
420     p_sys->i_crop_left = val.i_int;
421     var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
422     p_sys->i_crop_right = val.i_int;
423
424     var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
425     p_sys->i_threads = val.i_int;
426
427     if( p_sys->i_vcodec )
428     {
429         msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
430                  (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
431                  p_sys->f_scale, p_sys->i_vbitrate / 1000 );
432     }
433
434     /* Subpictures transcoding parameters */
435     var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
436     p_sys->psz_senc = NULL;
437     p_sys->p_spu_cfg = NULL;
438     if( val.psz_string && *val.psz_string )
439     {
440         char *psz_next;
441         psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
442                                    val.psz_string );
443         if( psz_next ) free( psz_next );
444     }
445     if( val.psz_string ) free( val.psz_string );
446
447     var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
448     p_sys->i_scodec = 0;
449     if( val.psz_string && *val.psz_string )
450     {
451         char fcc[4] = "    ";
452         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
453         p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
454     }
455     if( val.psz_string ) free( val.psz_string );
456
457     if( p_sys->i_scodec )
458     {
459         msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_acodec );
460     }
461
462     var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
463     p_sys->b_soverlay = val.b_bool;
464     p_sys->p_filter_blend = 0;
465
466     for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
467     {
468         p_sys->pp_subpics[i] = 0;
469     }
470
471     var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
472     p_sys->b_audio_sync = val.b_bool;
473     if( p_sys->f_fps > 0 ) p_sys->b_audio_sync = VLC_TRUE;
474
475     p_stream->pf_add    = Add;
476     p_stream->pf_del    = Del;
477     p_stream->pf_send   = Send;
478     p_stream->p_sys     = p_sys;
479
480     avcodec_init();
481     avcodec_register_all();
482
483     return VLC_SUCCESS;
484 }
485
486 /*****************************************************************************
487  * Close:
488  *****************************************************************************/
489 static void Close( vlc_object_t * p_this )
490 {
491     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
492     sout_stream_sys_t   *p_sys = p_stream->p_sys;
493
494     sout_StreamDelete( p_sys->p_out );
495
496     while( p_sys->p_audio_cfg != NULL )
497     {
498         sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
499
500         if( p_sys->p_audio_cfg->psz_name )
501             free( p_sys->p_audio_cfg->psz_name );
502         if( p_sys->p_audio_cfg->psz_value )
503             free( p_sys->p_audio_cfg->psz_value );
504         free( p_sys->p_audio_cfg );
505
506         p_sys->p_audio_cfg = p_next;
507     }
508     if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
509
510     while( p_sys->p_video_cfg != NULL )
511     {
512         sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
513
514         if( p_sys->p_video_cfg->psz_name )
515             free( p_sys->p_video_cfg->psz_name );
516         if( p_sys->p_video_cfg->psz_value )
517             free( p_sys->p_video_cfg->psz_value );
518         free( p_sys->p_video_cfg );
519
520         p_sys->p_video_cfg = p_next;
521     }
522     if( p_sys->psz_venc ) free( p_sys->psz_venc );
523
524     while( p_sys->p_spu_cfg != NULL )
525     {
526         sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
527
528         if( p_sys->p_spu_cfg->psz_name )
529             free( p_sys->p_spu_cfg->psz_name );
530         if( p_sys->p_spu_cfg->psz_value )
531             free( p_sys->p_spu_cfg->psz_value );
532         free( p_sys->p_spu_cfg );
533
534         p_sys->p_spu_cfg = p_next;
535     }
536     if( p_sys->psz_senc ) free( p_sys->psz_senc );
537
538     if( p_sys->p_filter_blend )
539     {
540         if( p_sys->p_filter_blend->p_module )
541             module_Unneed( p_sys->p_filter_blend,
542                            p_sys->p_filter_blend->p_module );
543
544         vlc_object_detach( p_sys->p_filter_blend );
545         vlc_object_destroy( p_sys->p_filter_blend );
546     }
547
548     vlc_object_destroy( p_sys );
549 }
550
551 struct sout_stream_id_t
552 {
553     vlc_fourcc_t  b_transcode;
554
555     unsigned int  i_inter_pixfmt; /* intermediary format when transcoding */
556
557     /* id of the out stream */
558     void *id;
559
560     /* Decoder */
561     decoder_t       *p_decoder;
562
563     /* Filters */
564     filter_t        *pp_filter[10];
565     int             i_filter;
566
567     /* Encoder */
568     encoder_t       *p_encoder;
569
570     /* ffmpeg part */
571     AVCodec         *ff_dec;
572     AVCodecContext  *ff_dec_c;
573
574     mtime_t         i_dts;
575     mtime_t         i_length;
576
577     int             i_buffer;
578     int             i_buffer_pos;
579     uint8_t         *p_buffer;
580
581     AVFrame         *p_ff_pic;
582     AVFrame         *p_ff_pic_tmp0; /* to do deinterlace */
583     AVFrame         *p_ff_pic_tmp1; /* to do pix conversion */
584     AVFrame         *p_ff_pic_tmp2; /* to do resample */
585     AVFrame         *p_ff_pic_tmp3; /* to do subpicture overlay */
586
587     ImgReSampleContext *p_vresample;
588
589     /* Sync */
590     date_t          interpolated_pts;
591     mtime_t         i_initial_pts;
592 };
593
594
595 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
596 {
597     sout_stream_sys_t *p_sys = p_stream->p_sys;
598     sout_stream_id_t *id;
599
600     id = malloc( sizeof( sout_stream_id_t ) );
601     memset( id, 0, sizeof(sout_stream_id_t) );
602
603     id->id = NULL;
604     id->p_decoder = NULL;
605     id->p_encoder = NULL;
606
607     /* Create decoder object */
608     id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
609     if( !id->p_decoder )
610     {
611         msg_Err( p_stream, "out of memory" );
612         goto error;
613     }
614     vlc_object_attach( id->p_decoder, p_stream );
615     id->p_decoder->p_module = NULL;
616     id->p_decoder->fmt_in = *p_fmt;
617     id->p_decoder->fmt_out = *p_fmt;
618     id->p_decoder->fmt_out.i_extra = 0;
619     id->p_decoder->fmt_out.p_extra = 0;
620     id->p_decoder->b_pace_control = VLC_TRUE;
621
622     /* Create encoder object */
623     id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
624     if( !id->p_encoder )
625     {
626         msg_Err( p_stream, "out of memory" );
627         goto error;
628     }
629     vlc_object_attach( id->p_encoder, p_stream );
630     id->p_encoder->p_module = NULL;
631
632     /* Create destination format */
633     es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
634     id->p_encoder->fmt_out.i_id    = p_fmt->i_id;
635     id->p_encoder->fmt_out.i_group = p_fmt->i_group;
636     if( p_fmt->psz_language )
637         id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
638
639     if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
640     {
641         msg_Dbg( p_stream,
642                  "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
643                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
644
645         /* Complete destination format */
646         id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
647         id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
648             p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
649         id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
650             p_sys->i_channels : p_fmt->audio.i_channels;
651         id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
652         id->p_encoder->fmt_out.audio.i_bitspersample =
653             p_fmt->audio.i_bitspersample;
654
655         /* Build decoder -> filter -> encoder chain */
656         if( transcode_audio_new( p_stream, id ) )
657         {
658             msg_Err( p_stream, "cannot create audio chain" );
659             goto error;
660         }
661
662         /* Open output stream */
663         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
664         id->b_transcode = VLC_TRUE;
665
666         if( !id->id ) goto error;
667
668         date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
669     }
670     else if( p_fmt->i_cat == VIDEO_ES &&
671              (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
672     {
673         msg_Dbg( p_stream,
674                  "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
675                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
676
677         /* Complete destination format */
678         id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
679         id->p_encoder->fmt_out.video.i_width  = p_sys->i_width;
680         id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
681         id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
682
683         /* Build decoder -> filter -> encoder chain */
684         if( transcode_video_new( p_stream, id ) )
685         {
686             msg_Err( p_stream, "cannot create video chain" );
687             goto error;
688         }
689
690         /* Stream will be added later on because we don't know
691          * all the characteristics of the decoded stream yet */
692         id->b_transcode = VLC_TRUE;
693
694         if( p_sys->f_fps > 0 )
695         {
696             id->p_encoder->fmt_out.video.i_frame_rate = p_sys->f_fps * 1000;
697             id->p_encoder->fmt_out.video.i_frame_rate_base = 1000;
698         }
699     }
700     else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
701     {
702         msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
703                  "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
704                  (char*)&p_sys->i_scodec );
705
706         /* Complete destination format */
707         id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
708
709         /* build decoder -> filter -> encoder */
710         if( transcode_spu_new( p_stream, id ) )
711         {
712             msg_Err( p_stream, "cannot create subtitles chain" );
713             goto error;
714         }
715
716         /* open output stream */
717         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
718         id->b_transcode = VLC_TRUE;
719
720         if( !id->id ) goto error;
721     }
722     else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
723     {
724         msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
725                  (char*)&p_fmt->i_codec );
726
727         id->b_transcode = VLC_TRUE;
728
729         /* Build decoder -> filter -> overlaying chain */
730         if( transcode_spu_new( p_stream, id ) )
731         {
732             msg_Err( p_stream, "cannot create subtitles chain" );
733             goto error;
734         }
735     }
736     else
737     {
738         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
739                  (char*)&p_fmt->i_codec );
740         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
741         id->b_transcode = VLC_FALSE;
742
743         if( !id->id ) goto error;
744     }
745
746     return id;
747
748  error:
749     if( id->p_decoder )
750     {
751         vlc_object_detach( id->p_decoder );
752         vlc_object_destroy( id->p_decoder );
753     }
754
755     if( id->p_encoder )
756     {
757         vlc_object_detach( id->p_encoder );
758         vlc_object_destroy( id->p_encoder );
759     }
760
761     free( id );
762     return NULL;
763 }
764
765 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
766 {
767     sout_stream_sys_t *p_sys = p_stream->p_sys;
768
769     if( id->b_transcode )
770     {
771         switch( id->p_decoder->fmt_in.i_cat )
772         {
773         case AUDIO_ES:
774             transcode_audio_close( p_stream, id );
775             break;
776         case VIDEO_ES:
777             transcode_video_close( p_stream, id );
778             break;
779         case SPU_ES:
780             transcode_spu_close( p_stream, id );
781             break;
782         }
783     }
784
785     if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
786
787     if( id->p_decoder )
788     {
789         vlc_object_detach( id->p_decoder );
790         vlc_object_destroy( id->p_decoder );
791     }
792
793     if( id->p_encoder )
794     {
795         vlc_object_detach( id->p_encoder );
796         vlc_object_destroy( id->p_encoder );
797     }
798
799     free( id );
800
801     return VLC_SUCCESS;
802 }
803
804 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
805                  block_t *p_buffer )
806 {
807     sout_stream_sys_t *p_sys = p_stream->p_sys;
808     block_t *p_out;
809
810     if( !id->b_transcode && id->id )
811     {
812         return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
813     }
814     else if( !id->b_transcode )
815     {
816         block_Release( p_buffer );
817         return VLC_EGENERIC;
818     }
819
820     switch( id->p_decoder->fmt_in.i_cat )
821     {
822     case AUDIO_ES:
823         transcode_audio_process( p_stream, id, p_buffer, &p_out );
824         block_Release( p_buffer );
825         break;
826
827     case VIDEO_ES:
828         if( transcode_video_process( p_stream, id, p_buffer, &p_out )
829             != VLC_SUCCESS )
830         {
831             return VLC_EGENERIC;
832         }
833         break;
834
835     case SPU_ES:
836         if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
837             VLC_SUCCESS )
838         {
839             return VLC_EGENERIC;
840         }
841         break;
842
843     default:
844         block_Release( p_buffer );
845         break;
846     }
847
848     if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
849     return VLC_SUCCESS;
850 }
851
852 /****************************************************************************
853  * ffmpeg decoder reencoder part
854  ****************************************************************************/
855 static struct
856 {
857     vlc_fourcc_t i_fcc;
858     int          i_ff_codec;
859
860 } fourcc_to_ff_code[] =
861 {
862     /* audio */
863     { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
864     { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
865     { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
866     { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
867     { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
868     { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
869     { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
870     { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
871     { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
872
873     { VLC_FOURCC(   0,   0,   0,   0 ), 0 }
874 };
875
876 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
877 {
878     int i;
879
880     for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
881     {
882         if( fourcc_to_ff_code[i].i_fcc == i_fcc )
883         {
884             return fourcc_to_ff_code[i].i_ff_codec;
885         }
886     }
887
888     return 0;
889 }
890
891 static int transcode_audio_new( sout_stream_t *p_stream,
892                                 sout_stream_id_t *id )
893 {
894     int i_ff_codec;
895
896     if( id->p_decoder->fmt_in.i_codec == VLC_FOURCC('s','1','6','l') ||
897         id->p_decoder->fmt_in.i_codec == VLC_FOURCC('s','1','6','b') ||
898         id->p_decoder->fmt_in.i_codec == VLC_FOURCC('s','8',' ',' ') ||
899         id->p_decoder->fmt_in.i_codec == VLC_FOURCC('u','8',' ',' ') )
900     {
901         id->ff_dec = NULL;
902
903         id->ff_dec_c = avcodec_alloc_context();
904         id->ff_dec_c->sample_rate = id->p_decoder->fmt_in.audio.i_rate;
905         id->ff_dec_c->channels    = id->p_decoder->fmt_in.audio.i_channels;
906         id->ff_dec_c->block_align = id->p_decoder->fmt_in.audio.i_blockalign;
907         id->ff_dec_c->bit_rate    = id->p_decoder->fmt_in.i_bitrate;
908     }
909     else
910     {
911         /* find decoder */
912         i_ff_codec = get_ff_codec( id->p_decoder->fmt_in.i_codec );
913         if( i_ff_codec == 0 )
914         {
915             msg_Err( p_stream, "cannot find decoder id" );
916             return VLC_EGENERIC;
917         }
918
919         id->ff_dec = avcodec_find_decoder( i_ff_codec );
920         if( !id->ff_dec )
921         {
922             msg_Err( p_stream, "cannot find decoder (avcodec)" );
923             return VLC_EGENERIC;
924         }
925
926         id->ff_dec_c = avcodec_alloc_context();
927         id->ff_dec_c->sample_rate = id->p_decoder->fmt_in.audio.i_rate;
928         id->ff_dec_c->channels    = id->p_decoder->fmt_in.audio.i_channels;
929         id->ff_dec_c->block_align = id->p_decoder->fmt_in.audio.i_blockalign;
930         id->ff_dec_c->bit_rate    = id->p_decoder->fmt_in.i_bitrate;
931
932         id->ff_dec_c->extradata_size = id->p_decoder->fmt_in.i_extra;
933         id->ff_dec_c->extradata      = id->p_decoder->fmt_in.p_extra;
934         if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
935         {
936             msg_Err( p_stream, "cannot open decoder" );
937             av_free( id->ff_dec_c );
938             return VLC_EGENERIC;
939         }
940     }
941
942     id->i_buffer     = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
943     id->i_buffer_pos = 0;
944     id->p_buffer     = malloc( id->i_buffer );
945
946     /* Sanity check for audio channels */
947     id->p_encoder->fmt_out.audio.i_channels =
948         __MIN( id->p_encoder->fmt_out.audio.i_channels,
949                id->p_decoder->fmt_in.audio.i_channels );
950
951     /* Initialization of encoder format structures */
952     es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
953     id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
954     id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
955     id->p_encoder->fmt_in.audio.i_physical_channels =
956         id->p_encoder->fmt_in.audio.i_original_channels =
957             pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
958     id->p_encoder->fmt_in.audio.i_channels =
959         id->p_encoder->fmt_out.audio.i_channels;
960     id->p_encoder->fmt_in.audio.i_bitspersample = 16;
961
962     id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
963
964     id->p_encoder->p_module =
965         module_Need( id->p_encoder, "encoder",
966                      p_stream->p_sys->psz_aenc, VLC_TRUE );
967     if( !id->p_encoder->p_module )
968     {
969         vlc_object_detach( id->p_encoder );
970         vlc_object_destroy( id->p_encoder );
971         msg_Err( p_stream, "cannot open encoder" );
972         av_free( id->ff_dec_c );
973         return VLC_EGENERIC;
974     }
975
976     /* FIXME: Hack for mp3 transcoding support */
977     if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
978         id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
979
980     return VLC_SUCCESS;
981 }
982
983 static void transcode_audio_close( sout_stream_t *p_stream,
984                                    sout_stream_id_t *id )
985 {
986     if( id->ff_dec ) avcodec_close( id->ff_dec_c );
987     av_free( id->ff_dec_c );
988
989     module_Unneed( id->p_encoder, id->p_encoder->p_module );
990
991     vlc_object_detach( id->p_encoder );
992     vlc_object_destroy( id->p_encoder );
993
994     free( id->p_buffer );
995 }
996
997 static int transcode_audio_process( sout_stream_t *p_stream,
998                                     sout_stream_id_t *id,
999                                     block_t *in, block_t **out )
1000 {
1001     sout_stream_sys_t *p_sys = p_stream->p_sys;
1002     aout_buffer_t aout_buf;
1003     block_t *p_block;
1004     int i_buffer = in->i_buffer;
1005     char *p_buffer = in->p_buffer;
1006     id->i_dts = in->i_dts;
1007     *out = NULL;
1008
1009     while( i_buffer )
1010     {
1011         id->i_buffer_pos = 0;
1012
1013         /* decode as much data as possible */
1014         if( id->ff_dec )
1015         {
1016             int i_used;
1017
1018             i_used = avcodec_decode_audio( id->ff_dec_c,
1019                          (int16_t*)id->p_buffer, &id->i_buffer_pos,
1020                          p_buffer, i_buffer );
1021
1022 #if 0
1023             msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
1024                       i_used, i_buffer );
1025 #endif
1026             if( i_used < 0 )
1027             {
1028                 msg_Warn( p_stream, "error audio decoding");
1029                 break;
1030             }
1031
1032             i_buffer -= i_used;
1033             p_buffer += i_used;
1034
1035             if ( id->i_buffer_pos < 0 )
1036             {
1037                 msg_Warn( p_stream, "weird error audio decoding");
1038                 break;
1039             }
1040         }
1041         else
1042         {
1043             int16_t *sout = (int16_t*)id->p_buffer;
1044             vlc_fourcc_t i_codec = id->p_decoder->fmt_in.i_codec;
1045
1046             if( i_codec == VLC_FOURCC('s','8',' ',' ') ||
1047                 i_codec == VLC_FOURCC('u','8',' ',' ') )
1048             {
1049                 int8_t *sin = (int8_t*)p_buffer;
1050                 int i_used = __MIN( id->i_buffer/2, i_buffer );
1051                 int i_samples = i_used;
1052
1053                 if( i_codec == VLC_FOURCC('s','8',' ',' ') )
1054                     while( i_samples > 0 )
1055                     {
1056                         *sout++ = ( *sin++ ) << 8;
1057                         i_samples--;
1058                     }
1059                 else
1060                     while( i_samples > 0 )
1061                     {
1062                         *sout++ = ( *sin++ - 128 ) << 8;
1063                         i_samples--;
1064                     }
1065
1066                 i_buffer -= i_used;
1067                 p_buffer += i_used;
1068                 id->i_buffer_pos = i_used * 2;
1069             }
1070             else if( i_codec == VLC_FOURCC('s','1','6','l') ||
1071                      i_codec == VLC_FOURCC('s','1','6','b') )
1072             {
1073                 int16_t *sin = (int16_t*)p_buffer;
1074                 int i_used = __MIN( id->i_buffer, i_buffer );
1075                 int i_samples = i_used / 2;
1076
1077                 /* first copy */
1078                 memcpy( sout, sin, i_used );
1079
1080 #ifdef WORDS_BIGENDIAN
1081                 if( i_codec == VLC_FOURCC('s','1','6','l') )
1082 #else
1083                 if( i_codec == VLC_FOURCC('s','1','6','b') )
1084 #endif
1085                 {
1086                     uint8_t *dat = (uint8_t*)sout;
1087
1088                     while( i_samples > 0 )
1089                     {
1090                         uint8_t tmp;
1091                         tmp    = dat[0];
1092                         dat[0] = dat[1];
1093                         dat[1] = tmp;
1094
1095                         dat += 2;
1096
1097                         i_samples--;
1098                     }
1099                 }
1100
1101                 i_buffer -= i_used;
1102                 p_buffer += i_used;
1103                 id->i_buffer_pos = i_used;
1104             }
1105         }
1106
1107         if( id->i_buffer_pos == 0 ) continue;
1108
1109         aout_buf.p_buffer = id->p_buffer;
1110         aout_buf.i_nb_bytes = id->i_buffer_pos;
1111         aout_buf.i_nb_samples = id->i_buffer_pos / 2 /
1112             id->p_decoder->fmt_in.audio.i_channels;
1113         aout_buf.start_date = id->i_dts;
1114         aout_buf.end_date = id->i_dts;
1115
1116         if( p_sys->b_audio_sync )
1117         {
1118             aout_buf.start_date = date_Get( &id->interpolated_pts ) + 1;
1119             p_sys->i_master_drift = id->i_dts - aout_buf.start_date;
1120             date_Increment( &id->interpolated_pts, aout_buf.i_nb_samples );
1121         }
1122
1123         id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
1124             id->p_decoder->fmt_in.audio.i_channels /
1125             id->p_decoder->fmt_in.audio.i_rate );
1126
1127         if( id->p_encoder->fmt_in.audio.i_channels == 1 &&
1128             id->p_decoder->fmt_in.audio.i_channels > 1 )
1129         {
1130             int16_t *p_sample = (int16_t *)aout_buf.p_buffer;
1131             int i_src_c = id->p_decoder->fmt_in.audio.i_channels;
1132             unsigned int i;
1133
1134             for( i = 0; i < aout_buf.i_nb_samples; i++ )
1135             {
1136                 int j, c = 0;
1137
1138                 for( j = 1; j < i_src_c; j++ )
1139                 {
1140                     c += p_sample[i_src_c * i + j];
1141                 }
1142                 p_sample[i] = c / (i_src_c-1);
1143             }
1144             aout_buf.i_nb_bytes = i * 2;
1145         }
1146         else if( id->p_encoder->fmt_in.audio.i_channels == 2 &&
1147                  id->p_decoder->fmt_in.audio.i_channels > 2 )
1148         {
1149             int i_src_c = id->p_decoder->fmt_in.audio.i_channels;
1150             unsigned int i;
1151
1152             static const float mixf_l[4][6] =/* [i_src_c - 3][channel index] */
1153             {
1154                 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1155                 { 0.00, 0.50, 0.50, 0.00, 0.00, 0.00 }, /* 4 channels */
1156                 { 0.00, 0.50, 0.00, 0.50, 0.00, 0.00 }, /* 5 channels */
1157                 { 0.00, 0.34, 0.33, 0.00, 0.33, 0.00 }, /* 6 channels */
1158             };
1159             static const float mixf_r[4][6] =/* [i_src_c - 3][channel index] */
1160             {
1161                 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1162                 { 0.00, 0.00, 0.50, 0.50, 0.00, 0.00 }, /* 4 channels */
1163                 { 0.00, 0.00, 0.50, 0.00, 0.50, 0.00 }, /* 5 channels */
1164                 { 0.00, 0.00, 0.33, 0.34, 0.00, 0.33 }, /* 6 channels */
1165             };
1166
1167
1168             for( i = 0; i < aout_buf.i_nb_samples; i++ )
1169             {
1170                 int16_t *p_src = (int16_t *)aout_buf.p_buffer + i_src_c * i;
1171                 int16_t *p_dst = (int16_t *)aout_buf.p_buffer + 2 * i;
1172
1173                 int j;
1174                 float l = 0.0, r = 0.0;
1175                 for( j = 0; j < i_src_c; j++ )
1176                 {
1177                     l += mixf_l[i_src_c-3][j] * p_src[j];
1178                     r += mixf_r[i_src_c-3][j] * p_src[j];
1179                 }
1180
1181                 p_dst[0] = (int)( l + 0.5 );
1182                 p_dst[1] = (int)( r + 0.5 );
1183             }
1184             aout_buf.i_nb_bytes = i * 2 * 2;
1185         }
1186         else if( id->p_decoder->fmt_in.audio.i_channels !=
1187                  id->p_encoder->fmt_in.audio.i_channels )
1188         {
1189             unsigned int i;
1190             int j;
1191
1192             /* This is for liba52 which is what ffmpeg uses to decode ac3 */
1193             static const int translation[7][6] =
1194             {{ 0, 0, 0, 0, 0, 0 },      /* 0 channels (rarely used) */
1195              { 0, 0, 0, 0, 0, 0 },       /* 1 ch */
1196              { 0, 1, 0, 0, 0, 0 },       /* 2 */
1197              { 1, 2, 0, 0, 0, 0 },       /* 3 */
1198              { 1, 3, 2, 0, 0, 0 },       /* 4 */
1199              { 1, 3, 4, 2, 0, 0 },       /* 5 */
1200              { 1, 3, 4, 5, 2, 0 }};      /* 6 */
1201
1202             /* dumb downmixing */
1203             for( i = 0; i < aout_buf.i_nb_samples; i++ )
1204             {
1205                 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
1206                 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
1207                 {
1208                     p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
1209                       p_buffer[i*id->p_decoder->fmt_in.audio.i_channels +
1210                        translation[id->p_decoder->fmt_in.audio.i_channels][j]];
1211                 }
1212             }
1213             aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1214         }
1215
1216         p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1217         block_ChainAppend( out, p_block );
1218     }
1219
1220     return VLC_SUCCESS;
1221 }
1222
1223
1224 /*
1225  * video
1226  */
1227 static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1228 {
1229     sout_stream_sys_t *p_sys = p_stream->p_sys;
1230
1231     /*
1232      * Open decoder
1233      */
1234
1235     /* Initialization of decoder structures */
1236     id->p_decoder->pf_decode_video = 0;
1237     id->p_decoder->pf_vout_buffer_new = video_new_buffer;
1238     id->p_decoder->pf_vout_buffer_del = video_del_buffer;
1239     id->p_decoder->pf_picture_link    = video_link_picture;
1240     id->p_decoder->pf_picture_unlink  = video_unlink_picture;
1241     //id->p_decoder->p_cfg = p_sys->p_video_cfg;
1242
1243     id->p_decoder->p_module =
1244         module_Need( id->p_decoder, "decoder", "$codec", 0 );
1245
1246     if( !id->p_decoder->p_module )
1247     {
1248         msg_Err( p_stream, "cannot find decoder" );
1249         return VLC_EGENERIC;
1250     }
1251
1252     /*
1253      * Open encoder.
1254      * Because some info about the decoded input will only be available
1255      * once the first frame is decoded, we actually only test the availability
1256      * of the encoder here.
1257      */
1258
1259     /* Initialization of encoder format structures */
1260     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1261                     id->p_decoder->fmt_out.i_codec );
1262     id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
1263
1264     /* The dimensions will be set properly later on.
1265      * Just put sensible values so we can test an encoder is available. */
1266     id->p_encoder->fmt_in.video.i_width =
1267         id->p_encoder->fmt_out.video.i_width ?
1268         id->p_encoder->fmt_out.video.i_width :
1269         id->p_decoder->fmt_in.video.i_width ?
1270         id->p_decoder->fmt_in.video.i_width : 16;
1271     id->p_encoder->fmt_in.video.i_height =
1272         id->p_encoder->fmt_out.video.i_height ?
1273         id->p_encoder->fmt_out.video.i_height :
1274         id->p_decoder->fmt_in.video.i_height ?
1275         id->p_decoder->fmt_in.video.i_height : 16;
1276     id->p_encoder->fmt_in.video.i_frame_rate = 25;
1277     id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
1278
1279     id->p_encoder->i_threads = p_sys->i_threads;
1280     id->p_encoder->p_cfg = p_sys->p_video_cfg;
1281
1282     id->p_encoder->p_module =
1283         module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1284     if( !id->p_encoder->p_module )
1285     {
1286         msg_Err( p_stream, "cannot find encoder" );
1287         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1288         id->p_decoder->p_module = 0;
1289         return VLC_EGENERIC;
1290     }
1291
1292     /* Close the encoder.
1293      * We'll open it only when we have the first frame. */
1294     module_Unneed( id->p_encoder, id->p_encoder->p_module );
1295     id->p_encoder->p_module = NULL;
1296
1297     if( p_sys->i_threads >= 1 )
1298     {
1299         p_sys->id_video = id;
1300         vlc_mutex_init( p_stream, &p_sys->lock_out );
1301         vlc_cond_init( p_stream, &p_sys->cond );
1302         memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1303         p_sys->i_first_pic = 0;
1304         p_sys->i_last_pic = 0;
1305         p_sys->p_buffers = NULL;
1306         p_sys->b_die = p_sys->b_error = 0;
1307         if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1308                                VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1309         {
1310             msg_Err( p_stream, "cannot spawn encoder thread" );
1311             module_Unneed( id->p_decoder, id->p_decoder->p_module );
1312             id->p_decoder->p_module = 0;
1313             return VLC_EGENERIC;
1314         }
1315     }
1316
1317     return VLC_SUCCESS;
1318 }
1319
1320 static int transcode_video_encoder_open( sout_stream_t *p_stream,
1321                                          sout_stream_id_t *id )
1322 {
1323     sout_stream_sys_t *p_sys = p_stream->p_sys;
1324
1325     /* Hack because of the copy packetizer which can fail to detect the
1326      * proper size (which forces us to wait until the 1st frame
1327      * is decoded) */
1328     int i_width = id->p_decoder->fmt_out.video.i_width -
1329         p_sys->i_crop_left - p_sys->i_crop_right;
1330     int i_height = id->p_decoder->fmt_out.video.i_height -
1331         p_sys->i_crop_top - p_sys->i_crop_bottom;
1332
1333     if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1334         id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
1335     {
1336         /* Apply the scaling */
1337         id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
1338         id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
1339     }
1340     else if( id->p_encoder->fmt_out.video.i_width > 0 &&
1341              id->p_encoder->fmt_out.video.i_height <= 0 )
1342     {
1343         id->p_encoder->fmt_out.video.i_height =
1344             id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
1345     }
1346     else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
1347              id->p_encoder->fmt_out.video.i_height > 0 )
1348     {
1349         id->p_encoder->fmt_out.video.i_width =
1350             id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
1351     }
1352
1353     id->p_encoder->fmt_in.video.i_width =
1354         id->p_encoder->fmt_out.video.i_width;
1355     id->p_encoder->fmt_in.video.i_height =
1356         id->p_encoder->fmt_out.video.i_height;
1357
1358     if( !id->p_encoder->fmt_out.video.i_frame_rate ||
1359         !id->p_encoder->fmt_out.video.i_frame_rate_base )
1360     {
1361         if( id->p_decoder->fmt_out.video.i_frame_rate &&
1362             id->p_decoder->fmt_out.video.i_frame_rate_base )
1363         {
1364             id->p_encoder->fmt_out.video.i_frame_rate =
1365                 id->p_decoder->fmt_out.video.i_frame_rate;
1366             id->p_encoder->fmt_out.video.i_frame_rate_base =
1367                 id->p_decoder->fmt_out.video.i_frame_rate_base;
1368         }
1369         else
1370         {
1371             /* Pick a sensible default value */
1372             id->p_encoder->fmt_out.video.i_frame_rate = 25;
1373             id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
1374         }
1375     }
1376
1377     id->p_encoder->fmt_in.video.i_frame_rate =
1378         id->p_encoder->fmt_out.video.i_frame_rate;
1379     id->p_encoder->fmt_in.video.i_frame_rate_base =
1380         id->p_encoder->fmt_out.video.i_frame_rate_base;
1381
1382     date_Init( &id->interpolated_pts,
1383                id->p_encoder->fmt_out.video.i_frame_rate,
1384                id->p_encoder->fmt_out.video.i_frame_rate_base );
1385
1386     /* Check whether a particular aspect ratio was requested */
1387     if( !id->p_encoder->fmt_out.video.i_aspect )
1388     {
1389         id->p_encoder->fmt_out.video.i_aspect =
1390             id->p_decoder->fmt_out.video.i_aspect;
1391     }
1392     id->p_encoder->fmt_in.video.i_aspect =
1393         id->p_encoder->fmt_out.video.i_aspect;
1394
1395     id->p_encoder->p_module =
1396         module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1397     if( !id->p_encoder->p_module )
1398     {
1399         msg_Err( p_stream, "cannot find encoder" );
1400         return VLC_EGENERIC;
1401     }
1402
1403     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
1404
1405     /* Hack for mp2v/mp1v transcoding support */
1406     if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
1407         id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
1408     {
1409         id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
1410     }
1411
1412     id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1413                                              &id->p_encoder->fmt_out );
1414     if( !id->id )
1415     {
1416         msg_Err( p_stream, "cannot add this stream" );
1417         return VLC_EGENERIC;
1418     }
1419
1420     return VLC_SUCCESS;
1421 }
1422
1423 static void transcode_video_close( sout_stream_t *p_stream,
1424                                    sout_stream_id_t *id )
1425 {
1426     int i;
1427
1428     if( p_stream->p_sys->i_threads >= 1 )
1429     {
1430         vlc_mutex_lock( &p_stream->p_sys->lock_out );
1431         p_stream->p_sys->b_die = 1;
1432         vlc_cond_signal( &p_stream->p_sys->cond );
1433         vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1434         vlc_thread_join( p_stream->p_sys );
1435         vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1436         vlc_cond_destroy( &p_stream->p_sys->cond );
1437     }
1438
1439     /* Close decoder */
1440     if( id->p_decoder->p_module )
1441         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1442
1443     /* Close encoder */
1444     if( id->p_encoder->p_module )
1445         module_Unneed( id->p_encoder, id->p_encoder->p_module );
1446
1447     /* Close filters */
1448     for( i = 0; i < id->i_filter; i++ )
1449     {
1450         vlc_object_detach( id->pp_filter[i] );
1451         if( id->pp_filter[i]->p_module )
1452             module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
1453         vlc_object_destroy( id->pp_filter[i] );
1454     }
1455 }
1456
1457 static int transcode_video_process( sout_stream_t *p_stream,
1458                                     sout_stream_id_t *id,
1459                                     block_t *in, block_t **out )
1460 {
1461     sout_stream_sys_t *p_sys = p_stream->p_sys;
1462     int i_duplicate = 1, i;
1463     picture_t *p_pic;
1464     *out = NULL;
1465
1466     while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
1467     {
1468         subpicture_t *p_subpic = 0;
1469
1470         if( p_sys->b_audio_sync )
1471         {
1472             mtime_t i_video_drift;
1473             mtime_t i_master_drift = p_sys->i_master_drift;
1474             mtime_t i_pts;
1475
1476             if( !id->i_initial_pts ) id->i_initial_pts = p_pic->date;
1477
1478             if( !i_master_drift )
1479             {
1480                 /* No audio track ? */
1481                 i_master_drift = id->i_initial_pts;
1482             }
1483
1484             i_pts = date_Get( &id->interpolated_pts ) + 1;
1485             i_video_drift = p_pic->date - i_pts;
1486             i_duplicate = 1;
1487
1488             /* Set the pts of the frame being encoded */
1489             p_pic->date = i_pts;
1490
1491             if( i_video_drift < i_master_drift - 50000 )
1492             {
1493                 msg_Dbg( p_stream, "dropping frame (%i)",
1494                          (int)(i_video_drift - i_master_drift) );
1495                 return VLC_EGENERIC;
1496             }
1497             else if( i_video_drift > i_master_drift + 50000 )
1498             {
1499                 msg_Dbg( p_stream, "adding frame (%i)",
1500                          (int)(i_video_drift - i_master_drift) );
1501                 i_duplicate = 2;
1502             }
1503
1504             date_Increment( &id->interpolated_pts, 1 );
1505         }
1506
1507         if( !id->p_encoder->p_module )
1508         {
1509             if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
1510             {
1511                 transcode_video_close( p_stream, id );
1512                 id->b_transcode = VLC_FALSE;
1513             }
1514
1515             /* Check if we need a filter for chroma conversion or resizing */
1516             if( id->p_decoder->fmt_out.video.i_chroma !=
1517                 id->p_encoder->fmt_in.video.i_chroma ||
1518                 id->p_decoder->fmt_out.video.i_width !=
1519                 id->p_encoder->fmt_out.video.i_width ||
1520                 id->p_decoder->fmt_out.video.i_height !=
1521                 id->p_encoder->fmt_out.video.i_height ||
1522                 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1523                 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1524             {
1525                 id->pp_filter[0] =
1526                     vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1527                 vlc_object_attach( id->pp_filter[0], p_stream );
1528
1529                 id->pp_filter[0]->pf_vout_buffer_new = video_new_buffer;
1530                 id->pp_filter[0]->pf_vout_buffer_del = video_del_buffer;
1531
1532                 id->pp_filter[0]->fmt_in = id->p_decoder->fmt_out;
1533                 id->pp_filter[0]->fmt_out = id->p_encoder->fmt_in;
1534                 id->pp_filter[0]->p_module =
1535                     module_Need( id->pp_filter[0], "video filter2", 0, 0 );
1536                 if( id->pp_filter[0]->p_module ) id->i_filter++;
1537                 else
1538                 {
1539                     msg_Dbg( p_stream, "no video filter found" );
1540                     vlc_object_detach( id->pp_filter[0] );
1541                     vlc_object_destroy( id->pp_filter[0] );
1542                 }
1543             }
1544         }
1545
1546         /* Deinterlace */
1547         if( p_stream->p_sys->b_deinterlace )
1548         {
1549         }
1550
1551         /* Run filter chain */
1552         for( i = 0; i < id->i_filter; i++ )
1553         {
1554             p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
1555         }
1556
1557         /*
1558          * Encoding
1559          */
1560
1561         /* Check if we have a subpicture to overlay */
1562         if( p_sys->p_filter_blend )
1563         {
1564             p_subpic = transcode_spu_get( p_stream, id, p_pic->date );
1565             /* TODO: get another pic */
1566         }
1567
1568         /* Overlay subpicture */
1569         if( p_subpic )
1570         {
1571             int i_width, i_height;
1572
1573             p_sys->p_filter_blend->fmt_out = id->p_encoder->fmt_in;
1574             p_sys->p_filter_blend->fmt_out.video.i_visible_width =
1575                 p_sys->p_filter_blend->fmt_out.video.i_width;
1576             p_sys->p_filter_blend->fmt_out.video.i_visible_height =
1577                 p_sys->p_filter_blend->fmt_out.video.i_height;
1578             p_sys->p_filter_blend->fmt_out.video.i_chroma =
1579                 VLC_FOURCC('I','4','2','0');
1580
1581             i_width = id->p_encoder->fmt_in.video.i_width;
1582             i_height = id->p_encoder->fmt_in.video.i_height;
1583
1584             while( p_subpic != NULL )
1585             {
1586                 subpicture_region_t *p_region = p_subpic->p_region;
1587
1588                 while( p_region && p_sys->p_filter_blend &&
1589                        p_sys->p_filter_blend->pf_video_blend )
1590                 {
1591                     int i_x_offset = p_region->i_x + p_subpic->i_x;
1592                     int i_y_offset = p_region->i_y + p_subpic->i_y;
1593
1594                     if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
1595                     {
1596                         i_y_offset = i_height - p_region->fmt.i_height -
1597                             p_subpic->i_y;
1598                     }
1599                     else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
1600                     {
1601                         i_y_offset = i_height / 2 - p_region->fmt.i_height / 2;
1602                     }
1603
1604                     if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
1605                     {
1606                         i_x_offset = i_width - p_region->fmt.i_width -
1607                             p_subpic->i_x;
1608                     }
1609                     else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
1610                     {
1611                         i_x_offset = i_width / 2 - p_region->fmt.i_width / 2;
1612                     }
1613
1614                     if( p_subpic->b_absolute )
1615                     {
1616                         i_x_offset = p_region->i_x + p_subpic->i_x;
1617                         i_y_offset = p_region->i_y + p_subpic->i_y;
1618                     }
1619
1620                     p_sys->p_filter_blend->fmt_in.video = p_region->fmt;
1621
1622                     p_sys->p_filter_blend->pf_video_blend(
1623                          p_sys->p_filter_blend, p_pic, p_pic,
1624                          &p_region->picture, i_x_offset, i_y_offset );
1625
1626                     p_region = p_region->p_next;
1627                 }
1628
1629                 p_subpic = p_subpic->p_next;
1630             }
1631         }
1632
1633         if( p_sys->i_threads >= 1 )
1634         {
1635             vlc_mutex_lock( &p_sys->lock_out );
1636             p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1637             p_sys->i_last_pic %= PICTURE_RING_SIZE;
1638             *out = p_sys->p_buffers;
1639             p_sys->p_buffers = NULL;
1640             vlc_cond_signal( &p_sys->cond );
1641             vlc_mutex_unlock( &p_sys->lock_out );
1642         }
1643         else
1644         {
1645             block_t *p_block;
1646             p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1647             block_ChainAppend( out, p_block );
1648
1649             if( p_sys->b_audio_sync && i_duplicate > 1 )
1650             {
1651                 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
1652                 date_Increment( &id->interpolated_pts, 1 );
1653                 p_pic->date = i_pts;
1654                 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
1655                 block_ChainAppend( out, p_block );
1656             }
1657
1658             p_pic->pf_release( p_pic );
1659         }
1660     }
1661
1662     return VLC_SUCCESS;
1663 }
1664
1665 static int EncoderThread( sout_stream_sys_t *p_sys )
1666 {
1667     sout_stream_id_t *id = p_sys->id_video;
1668     picture_t *p_pic;
1669     int i_plane;
1670
1671     while( !p_sys->b_die && !p_sys->b_error )
1672     {
1673         block_t *p_block;
1674
1675         vlc_mutex_lock( &p_sys->lock_out );
1676         while( p_sys->i_last_pic == p_sys->i_first_pic )
1677         {
1678             vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1679             if( p_sys->b_die || p_sys->b_error ) break;
1680         }
1681         if( p_sys->b_die || p_sys->b_error )
1682         {
1683             vlc_mutex_unlock( &p_sys->lock_out );
1684             break;
1685         }
1686
1687         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1688         p_sys->i_first_pic %= PICTURE_RING_SIZE;
1689         vlc_mutex_unlock( &p_sys->lock_out );
1690
1691         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1692         vlc_mutex_lock( &p_sys->lock_out );
1693         block_ChainAppend( &p_sys->p_buffers, p_block );
1694         vlc_mutex_unlock( &p_sys->lock_out );
1695
1696         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1697         {
1698             free( p_pic->p[i_plane].p_pixels );
1699         }
1700         free( p_pic );
1701     }
1702
1703     while( p_sys->i_last_pic != p_sys->i_first_pic )
1704     {
1705         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1706         p_sys->i_first_pic %= PICTURE_RING_SIZE;
1707
1708         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1709         {
1710             free( p_pic->p[i_plane].p_pixels );
1711         }
1712         free( p_pic );
1713     }
1714
1715     block_ChainRelease( p_sys->p_buffers );
1716
1717     return 0;
1718 }
1719
1720 struct picture_sys_t
1721 {
1722     decoder_t *p_dec;
1723 };
1724
1725 static void video_release_buffer( picture_t *p_pic )
1726 {
1727     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
1728     {
1729         video_del_buffer( p_pic->p_sys->p_dec, p_pic );
1730     }
1731     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
1732 }
1733
1734 static picture_t *video_new_buffer( decoder_t *p_dec )
1735 {
1736     picture_t *p_pic = malloc( sizeof(picture_t) );
1737
1738     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1739     vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
1740                           p_dec->fmt_out.video.i_chroma,
1741                           p_dec->fmt_out.video.i_width,
1742                           p_dec->fmt_out.video.i_height,
1743                           p_dec->fmt_out.video.i_aspect );
1744
1745     if( !p_pic->i_planes )
1746     {
1747         free( p_pic );
1748         return 0;
1749     }
1750
1751     p_pic->pf_release = video_release_buffer;
1752     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
1753     p_pic->p_sys->p_dec = p_dec;
1754
1755     return p_pic;
1756 }
1757
1758 static void video_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1759 {
1760     if( p_pic && p_pic->p_data ) free( p_pic->p_data );
1761     if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
1762     if( p_pic ) free( p_pic );
1763 }
1764
1765 static void video_link_picture( decoder_t *p_dec, picture_t *p_pic )
1766 {
1767     p_pic->i_refcount++;
1768 }
1769
1770 static void video_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1771 {
1772     video_release_buffer( p_pic );
1773 }
1774
1775 /*
1776  * SPU
1777  */
1778 static subpicture_t *spu_new_buffer( decoder_t * );
1779 static void spu_del_buffer( decoder_t *, subpicture_t * );
1780
1781 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
1782 {
1783     sout_stream_sys_t *p_sys = p_stream->p_sys;
1784
1785     /*
1786      * Open decoder
1787      */
1788
1789     /* Initialization of decoder structures */
1790     id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
1791     id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
1792     //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
1793
1794     id->p_decoder->p_module =
1795         module_Need( id->p_decoder, "decoder", "$codec", 0 );
1796
1797     if( !id->p_decoder->p_module )
1798     {
1799         msg_Err( p_stream, "cannot find decoder" );
1800         return VLC_EGENERIC;
1801     }
1802
1803     if( !p_sys->b_soverlay )
1804     {
1805         /*
1806          * Open encoder
1807          */
1808
1809         /* Initialization of encoder format structures */
1810         es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
1811                         id->p_decoder->fmt_in.i_codec );
1812
1813         id->p_encoder->p_cfg = p_sys->p_spu_cfg;
1814
1815         id->p_encoder->p_module =
1816             module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
1817
1818         if( !id->p_encoder->p_module )
1819         {
1820             module_Unneed( id->p_decoder, id->p_decoder->p_module );
1821             msg_Err( p_stream, "cannot find encoder" );
1822             return VLC_EGENERIC;
1823         }
1824     }
1825     else
1826     {
1827         p_sys->p_filter_blend =
1828             vlc_object_create( p_stream, VLC_OBJECT_FILTER );
1829         vlc_object_attach( p_sys->p_filter_blend, p_stream );
1830         p_sys->p_filter_blend->fmt_out.video.i_chroma =
1831             VLC_FOURCC('I','4','2','0');
1832         p_sys->p_filter_blend->fmt_in.video.i_chroma =
1833             VLC_FOURCC('Y','U','V','A');
1834         p_sys->p_filter_blend->p_module =
1835             module_Need( p_sys->p_filter_blend, "video blending", 0, 0 );
1836     }
1837
1838     return VLC_SUCCESS;
1839 }
1840
1841 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
1842 {
1843     sout_stream_sys_t *p_sys = p_stream->p_sys;
1844     int i;
1845
1846     /* Close decoder */
1847     if( id->p_decoder->p_module )
1848         module_Unneed( id->p_decoder, id->p_decoder->p_module );
1849
1850     /* Close encoder */
1851     if( id->p_encoder->p_module )
1852         module_Unneed( id->p_encoder, id->p_encoder->p_module );
1853
1854     /* Free subpictures */
1855     for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
1856     {
1857         if( !p_sys->pp_subpics[i] ) continue;
1858
1859         spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
1860         p_sys->pp_subpics[i] = NULL;
1861     }
1862 }
1863
1864 static int transcode_spu_process( sout_stream_t *p_stream,
1865                                   sout_stream_id_t *id,
1866                                   block_t *in, block_t **out )
1867 {
1868     sout_stream_sys_t *p_sys = p_stream->p_sys;
1869     subpicture_t *p_subpic;
1870     *out = NULL;
1871
1872     p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
1873     if( p_subpic && p_sys->b_soverlay )
1874     {
1875         int i;
1876
1877         /* Find a free slot in our supictures ring buffer */
1878         for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
1879         {
1880             if( !p_sys->pp_subpics[i] )
1881             {
1882                 p_sys->pp_subpics[i] = p_subpic;
1883                 break;
1884             }
1885         }
1886         if( i == SUBPICTURE_RING_SIZE )
1887         {
1888             spu_del_buffer( id->p_decoder, p_subpic );
1889         }
1890     }
1891
1892     if(  p_subpic && !p_sys->b_soverlay )
1893     {
1894         block_t *p_block;
1895
1896         p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
1897         spu_del_buffer( id->p_decoder, p_subpic );
1898
1899         if( p_block )
1900         {
1901             block_ChainAppend( out, p_block );
1902             return VLC_SUCCESS;
1903         }
1904     }
1905
1906     return VLC_EGENERIC;
1907 }
1908
1909 static subpicture_t *transcode_spu_get( sout_stream_t *p_stream,
1910                                         sout_stream_id_t *id,
1911                                         mtime_t display_date )
1912 {
1913     sout_stream_sys_t *p_sys = p_stream->p_sys;
1914     subpicture_t *p_subpic = 0;
1915     subpicture_t *p_ephemer = 0;
1916     subpicture_t **pp_subpic = &p_subpic;
1917     int i;
1918
1919     /* Find current subpictures and remove old ones */
1920     for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
1921     {
1922         if( !p_sys->pp_subpics[i] ) continue;
1923
1924         if( !p_sys->pp_subpics[i]->b_ephemer &&
1925             p_sys->pp_subpics[i]->i_stop < display_date )
1926         {
1927             spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
1928             p_sys->pp_subpics[i] = NULL;
1929             continue;
1930         }
1931
1932         if( p_sys->pp_subpics[i]->i_start > display_date ) continue;
1933
1934         if( p_sys->pp_subpics[i]->b_ephemer && !p_ephemer )
1935         {
1936             p_ephemer = p_sys->pp_subpics[i];
1937         }
1938         else if( p_sys->pp_subpics[i]->b_ephemer )
1939         {
1940             if( p_ephemer->i_start < p_sys->pp_subpics[i]->i_start )
1941             {
1942                 subpicture_t tmp;
1943                 tmp = *p_ephemer;
1944                 *p_ephemer = *p_sys->pp_subpics[i];
1945                 *p_sys->pp_subpics[i] = tmp;
1946             }
1947
1948             spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
1949             p_sys->pp_subpics[i] = NULL;
1950             continue;
1951         }
1952
1953         /* Add subpicture to the list */
1954         *pp_subpic = p_sys->pp_subpics[i];
1955         pp_subpic = &p_sys->pp_subpics[i]->p_next;
1956     }
1957
1958     return p_subpic;
1959 }
1960
1961 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1962 {
1963     subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
1964     memset( p_subpic, 0, sizeof(subpicture_t) );
1965     p_subpic->b_absolute = VLC_TRUE;
1966
1967     p_subpic->pf_create_region = __spu_CreateRegion;
1968     p_subpic->pf_destroy_region = __spu_DestroyRegion;
1969
1970     return p_subpic;
1971 }
1972
1973 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1974 {
1975     while( p_subpic->p_region )
1976     {
1977         subpicture_region_t *p_region = p_subpic->p_region;
1978         p_subpic->p_region = p_region->p_next;
1979         p_subpic->pf_destroy_region( VLC_OBJECT(p_dec), p_region );
1980     }
1981
1982     free( p_subpic );
1983 }