]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode.c
* modules/stream_out/transcode.c: implemented fps option for video only streams.
[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     set_description( _("Transcode stream output") );
140     set_capability( "sout stream", 50 );
141     add_shortcut( "transcode" );
142     set_callbacks( Open, Close );
143
144     add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
145                 VENC_LONGTEXT, VLC_FALSE );
146     add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
147                 VCODEC_LONGTEXT, VLC_FALSE );
148     add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
149                  VB_LONGTEXT, VLC_FALSE );
150     add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
151                SCALE_LONGTEXT, VLC_FALSE );
152     add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
153                FPS_LONGTEXT, VLC_FALSE );
154     add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
155               DEINTERLACE_LONGTEXT, VLC_FALSE );
156     add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
157                  WIDTH_LONGTEXT, VLC_TRUE );
158     add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
159                  HEIGHT_LONGTEXT, VLC_TRUE );
160
161     add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
162                  CROPTOP_LONGTEXT, VLC_TRUE );
163     add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
164                  CROPLEFT_LONGTEXT, VLC_TRUE );
165     add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
166                  CROPBOTTOM_LONGTEXT, VLC_TRUE );
167     add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
168                  CROPRIGHT_LONGTEXT, VLC_TRUE );
169
170     add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
171                 AENC_LONGTEXT, VLC_FALSE );
172     add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
173                 ACODEC_LONGTEXT, VLC_FALSE );
174     add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
175                  AB_LONGTEXT, VLC_FALSE );
176     add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
177                  ACHANS_LONGTEXT, VLC_FALSE );
178     add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
179                  ARATE_LONGTEXT, VLC_TRUE );
180
181     add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
182                 SENC_LONGTEXT, VLC_FALSE );
183     add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
184                 SCODEC_LONGTEXT, VLC_FALSE );
185     add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
186                SCODEC_LONGTEXT, VLC_FALSE );
187
188     add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
189                  THREADS_LONGTEXT, VLC_TRUE );
190
191     add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
192               ASYNC_LONGTEXT, VLC_FALSE );
193 vlc_module_end();
194
195 static const char *ppsz_sout_options[] = {
196     "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
197     "scale", "fps", "width", "height", "deinterlace", "threads",
198     "aenc", "acodec", "ab", "samplerate", "channels",
199     "senc", "scodec", "soverlay",
200     "audio-sync", NULL
201 };
202
203 /*****************************************************************************
204  * Exported prototypes
205  *****************************************************************************/
206 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
207 static int               Del ( sout_stream_t *, sout_stream_id_t * );
208 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
209
210 static int  transcode_audio_ffmpeg_new    ( sout_stream_t *, sout_stream_id_t * );
211 static void transcode_audio_ffmpeg_close  ( sout_stream_t *, sout_stream_id_t * );
212 static int  transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
213
214 static int  transcode_video_ffmpeg_new    ( sout_stream_t *, sout_stream_id_t * );
215 static void transcode_video_ffmpeg_close  ( sout_stream_t *, sout_stream_id_t * );
216 static int  transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
217
218 static int  transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
219
220 static int  transcode_spu_new    ( sout_stream_t *, sout_stream_id_t * );
221 static void transcode_spu_close  ( sout_stream_t *, sout_stream_id_t * );
222 static int  transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
223                                    block_t *, block_t ** );
224 static subpicture_t *transcode_spu_get( sout_stream_t *, sout_stream_id_t *,
225                                         mtime_t );
226
227 static int  EncoderThread( struct sout_stream_sys_t * p_sys );
228
229 static int pi_channels_maps[6] =
230 {
231     0,
232     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
233     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
234     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
235      | AOUT_CHAN_REARRIGHT,
236     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
237      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
238 };
239
240 #define PICTURE_RING_SIZE 64
241 #define SUBPICTURE_RING_SIZE 20
242
243 struct sout_stream_sys_t
244 {
245     VLC_COMMON_MEMBERS
246
247     sout_stream_t * p_out;
248     sout_stream_id_t * id_video;
249     block_t * p_buffers;
250     vlc_mutex_t     lock_out;
251     vlc_cond_t      cond;
252     picture_t *     pp_pics[PICTURE_RING_SIZE];
253     int             i_first_pic, i_last_pic;
254
255     /* Audio */
256     vlc_fourcc_t    i_acodec;   /* codec audio (0 if not transcode) */
257     char            *psz_aenc;
258     sout_cfg_t      *p_audio_cfg;
259     int             i_sample_rate;
260     int             i_channels;
261     int             i_abitrate;
262
263     /* Video */
264     vlc_fourcc_t    i_vcodec;   /* codec video (0 if not transcode) */
265     char            *psz_venc;
266     sout_cfg_t      *p_video_cfg;
267     int             i_vbitrate;
268     double          f_scale;
269     double          f_fps;
270     int             i_width;
271     int             i_height;
272     vlc_bool_t      b_deinterlace;
273     int             i_threads;
274
275     int             i_crop_top;
276     int             i_crop_bottom;
277     int             i_crop_right;
278     int             i_crop_left;
279
280     mtime_t         i_input_dts;
281     mtime_t         i_input_pts;
282     vlc_bool_t      b_input_has_b_frames;
283
284     mtime_t         i_output_pts;
285
286     /* SPU */
287     vlc_fourcc_t    i_scodec;   /* codec spu (0 if not transcode) */
288     char            *psz_senc;
289     vlc_bool_t      b_soverlay;
290     sout_cfg_t      *p_spu_cfg;
291     subpicture_t    *pp_subpics[SUBPICTURE_RING_SIZE];
292
293     /* Filters */
294     filter_t        *p_filter_blend;
295
296     /* Sync */
297     vlc_bool_t      b_audio_sync;
298     mtime_t         i_master_drift;
299 };
300
301 /*****************************************************************************
302  * Open:
303  *****************************************************************************/
304 static int Open( vlc_object_t *p_this )
305 {
306     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
307     sout_stream_sys_t *p_sys;
308     vlc_value_t       val;
309     int               i;
310
311     p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
312
313     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
314     if( !p_sys->p_out )
315     {
316         msg_Err( p_stream, "cannot create chain" );
317         free( p_sys );
318         return VLC_EGENERIC;
319     }
320
321     p_sys->b_input_has_b_frames = VLC_FALSE;
322     p_sys->i_output_pts = 0;
323     p_sys->i_master_drift = 0;
324
325     sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
326                    p_stream->p_cfg );
327
328     /* Audio transcoding parameters */
329     var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
330     p_sys->psz_aenc = NULL;
331     p_sys->p_audio_cfg = NULL;
332     if( val.psz_string && *val.psz_string )
333     {
334         char *psz_next;
335         psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
336                                    val.psz_string );
337         if( psz_next ) free( psz_next );
338     }
339     if( val.psz_string ) free( val.psz_string );
340
341     var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
342     p_sys->i_acodec = 0;
343     if( val.psz_string && *val.psz_string )
344     {
345         char fcc[4] = "    ";
346         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
347         p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
348     }
349     if( val.psz_string ) free( val.psz_string );
350
351     var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
352     p_sys->i_abitrate = val.i_int;
353     if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
354
355     var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
356     p_sys->i_sample_rate = val.i_int;
357
358     var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
359     p_sys->i_channels = val.i_int;
360
361     if( p_sys->i_acodec )
362     {
363         msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
364                  (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
365                  p_sys->i_channels, p_sys->i_abitrate / 1000 );
366     }
367
368     /* Video transcoding parameters */
369     var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
370     p_sys->psz_venc = NULL;
371     p_sys->p_video_cfg = NULL;
372     if( val.psz_string && *val.psz_string )
373     {
374         char *psz_next;
375         psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
376                                    val.psz_string );
377         if( psz_next ) free( psz_next );
378     }
379     if( val.psz_string ) free( val.psz_string );
380
381     var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
382     p_sys->i_vcodec = 0;
383     if( val.psz_string && *val.psz_string )
384     {
385         char fcc[4] = "    ";
386         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
387         p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
388     }
389     if( val.psz_string ) free( val.psz_string );
390
391     var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
392     p_sys->i_vbitrate = val.i_int;
393     if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
394
395     var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
396     p_sys->f_scale = val.f_float;
397
398     var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
399     p_sys->f_fps = val.f_float;
400
401     var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
402     p_sys->i_width = val.i_int;
403
404     var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
405     p_sys->i_height = val.i_int;
406
407     var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
408     p_sys->b_deinterlace = val.b_bool;
409
410     var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
411     p_sys->i_crop_top = val.i_int;
412     var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
413     p_sys->i_crop_bottom = val.i_int;
414     var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
415     p_sys->i_crop_left = val.i_int;
416     var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
417     p_sys->i_crop_right = val.i_int;
418
419     var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
420     p_sys->i_threads = val.i_int;
421
422     if( p_sys->i_vcodec )
423     {
424         msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
425                  (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
426                  p_sys->f_scale, p_sys->i_vbitrate / 1000 );
427     }
428
429     /* Subpictures transcoding parameters */
430     var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
431     p_sys->psz_senc = NULL;
432     p_sys->p_spu_cfg = NULL;
433     if( val.psz_string && *val.psz_string )
434     {
435         char *psz_next;
436         psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
437                                    val.psz_string );
438         if( psz_next ) free( psz_next );
439     }
440     if( val.psz_string ) free( val.psz_string );
441
442     var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
443     p_sys->i_scodec = 0;
444     if( val.psz_string && *val.psz_string )
445     {
446         char fcc[4] = "    ";
447         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
448         p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
449     }
450     if( val.psz_string ) free( val.psz_string );
451
452     if( p_sys->i_scodec )
453     {
454         msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_acodec );
455     }
456
457     var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
458     p_sys->b_soverlay = val.b_bool;
459     p_sys->p_filter_blend = 0;
460
461     for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
462     {
463         p_sys->pp_subpics[i] = 0;
464     }
465
466     var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
467     p_sys->b_audio_sync = val.b_bool;
468     if( p_sys->f_fps > 0 ) p_sys->b_audio_sync = VLC_TRUE;
469
470     p_stream->pf_add    = Add;
471     p_stream->pf_del    = Del;
472     p_stream->pf_send   = Send;
473     p_stream->p_sys     = p_sys;
474
475     avcodec_init();
476     avcodec_register_all();
477
478     return VLC_SUCCESS;
479 }
480
481 /*****************************************************************************
482  * Close:
483  *****************************************************************************/
484 static void Close( vlc_object_t * p_this )
485 {
486     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
487     sout_stream_sys_t   *p_sys = p_stream->p_sys;
488
489     sout_StreamDelete( p_sys->p_out );
490
491     while( p_sys->p_audio_cfg != NULL )
492     {
493         sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
494
495         if( p_sys->p_audio_cfg->psz_name )
496             free( p_sys->p_audio_cfg->psz_name );
497         if( p_sys->p_audio_cfg->psz_value )
498             free( p_sys->p_audio_cfg->psz_value );
499         free( p_sys->p_audio_cfg );
500
501         p_sys->p_audio_cfg = p_next;
502     }
503     if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
504
505     while( p_sys->p_video_cfg != NULL )
506     {
507         sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
508
509         if( p_sys->p_video_cfg->psz_name )
510             free( p_sys->p_video_cfg->psz_name );
511         if( p_sys->p_video_cfg->psz_value )
512             free( p_sys->p_video_cfg->psz_value );
513         free( p_sys->p_video_cfg );
514
515         p_sys->p_video_cfg = p_next;
516     }
517     if( p_sys->psz_venc ) free( p_sys->psz_venc );
518
519     while( p_sys->p_spu_cfg != NULL )
520     {
521         sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
522
523         if( p_sys->p_spu_cfg->psz_name )
524             free( p_sys->p_spu_cfg->psz_name );
525         if( p_sys->p_spu_cfg->psz_value )
526             free( p_sys->p_spu_cfg->psz_value );
527         free( p_sys->p_spu_cfg );
528
529         p_sys->p_spu_cfg = p_next;
530     }
531     if( p_sys->psz_senc ) free( p_sys->psz_senc );
532
533     if( p_sys->p_filter_blend )
534     {
535         if( p_sys->p_filter_blend->p_module )
536             module_Unneed( p_sys->p_filter_blend,
537                            p_sys->p_filter_blend->p_module );
538
539         vlc_object_detach( p_sys->p_filter_blend );
540         vlc_object_destroy( p_sys->p_filter_blend );
541     }
542
543     vlc_object_destroy( p_sys );
544 }
545
546 struct sout_stream_id_t
547 {
548     vlc_fourcc_t  b_transcode;
549     es_format_t f_src;        /* only if transcoding */
550     es_format_t f_dst;        /*  "   "      " */
551     unsigned int  i_inter_pixfmt; /* intermediary format when transcoding */
552
553     /* id of the out stream */
554     void *id;
555
556     /* Decoder */
557     decoder_t       *p_decoder;
558
559     /* Encoder */
560     encoder_t       *p_encoder;
561     vlc_fourcc_t    b_enc_inited;
562
563     /* ffmpeg part */
564     AVCodec         *ff_dec;
565     AVCodecContext  *ff_dec_c;
566
567     mtime_t         i_dts;
568     mtime_t         i_length;
569
570     int             i_buffer;
571     int             i_buffer_pos;
572     uint8_t         *p_buffer;
573
574     AVFrame         *p_ff_pic;
575     AVFrame         *p_ff_pic_tmp0; /* to do deinterlace */
576     AVFrame         *p_ff_pic_tmp1; /* to do pix conversion */
577     AVFrame         *p_ff_pic_tmp2; /* to do resample */
578     AVFrame         *p_ff_pic_tmp3; /* to do subpicture overlay */
579
580     ImgReSampleContext *p_vresample;
581
582     /* Sync */
583     date_t          interpolated_pts;
584     mtime_t         i_initial_pts;
585 };
586
587
588 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
589 {
590     sout_stream_sys_t *p_sys = p_stream->p_sys;
591     sout_stream_id_t *id;
592
593     id = malloc( sizeof( sout_stream_id_t ) );
594     memset( id, 0, sizeof(sout_stream_id_t) );
595
596     id->id = NULL;
597     id->p_decoder = NULL;
598     id->p_encoder = NULL;
599
600     /* Get source format */
601     id->f_src = *p_fmt;
602
603     /* Create destination format */
604     es_format_Init( &id->f_dst, p_fmt->i_cat, 0 );
605     id->f_dst.i_id    = id->f_src.i_id;
606     id->f_dst.i_group = id->f_src.i_group;
607     if( id->f_src.psz_language )
608         id->f_dst.psz_language = strdup( id->f_src.psz_language );
609
610     if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
611     {
612         msg_Dbg( p_stream,
613                  "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
614                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
615
616         /* Complete destination format */
617         id->f_dst.i_codec = p_sys->i_acodec;
618         id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ?
619             p_sys->i_sample_rate : id->f_src.audio.i_rate;
620         id->f_dst.audio.i_channels = p_sys->i_channels > 0 ?
621             p_sys->i_channels : id->f_src.audio.i_channels;
622         id->f_dst.i_bitrate = p_sys->i_abitrate;
623         id->f_dst.audio.i_bitspersample = id->f_src.audio.i_bitspersample;
624
625         /* build decoder -> filter -> encoder */
626         if( transcode_audio_ffmpeg_new( p_stream, id ) )
627         {
628             msg_Err( p_stream, "cannot create audio chain" );
629             free( id );
630             return NULL;
631         }
632
633         /* open output stream */
634         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
635         id->b_transcode = VLC_TRUE;
636
637         if( id->id == NULL )
638         {
639             free( id );
640             return NULL;
641         }
642
643         date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
644     }
645     else if( p_fmt->i_cat == VIDEO_ES &&
646              (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
647     {
648         msg_Dbg( p_stream,
649                  "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
650                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
651
652         /* Complete destination format */
653         id->f_dst.i_codec = p_sys->i_vcodec;
654         id->f_dst.video.i_width  = p_sys->i_width;
655         id->f_dst.video.i_height = p_sys->i_height;
656         id->f_dst.i_bitrate = p_sys->i_vbitrate;
657
658         /* build decoder -> filter -> encoder */
659         if( transcode_video_ffmpeg_new( p_stream, id ) )
660         {
661             msg_Err( p_stream, "cannot create video chain" );
662             free( id );
663             return NULL;
664         }
665 #if 0
666         /* open output stream */
667         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
668 #endif
669         id->b_transcode = VLC_TRUE;
670
671         if( id->f_dst.video.i_frame_rate && id->f_dst.video.i_frame_rate_base )
672         {
673             date_Init( &id->interpolated_pts, id->f_dst.video.i_frame_rate,
674                        id->f_dst.video.i_frame_rate_base );
675         }
676         else if( p_sys->b_audio_sync )
677         {
678             msg_Warn( p_stream, "no video frame rate available, disabling "
679                       "audio sync" );
680             p_sys->b_audio_sync = VLC_FALSE;
681         }
682     }
683     else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
684     {
685         msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
686                  "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
687                  (char*)&p_sys->i_scodec );
688
689         /* Complete destination format */
690         id->f_dst.i_codec = p_sys->i_scodec;
691
692         /* build decoder -> filter -> encoder */
693         if( transcode_spu_new( p_stream, id ) )
694         {
695             msg_Err( p_stream, "cannot create subtitles chain" );
696             free( id );
697             return NULL;
698         }
699
700         /* open output stream */
701         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
702         id->b_transcode = VLC_TRUE;
703
704         if( id->id == NULL )
705         {
706             free( id );
707             return NULL;
708         }
709     }
710     else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
711     {
712         msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
713                  (char*)&p_fmt->i_codec );
714
715         id->b_transcode = VLC_TRUE;
716
717         /* build decoder -> filter -> encoder */
718         if( transcode_spu_new( p_stream, id ) )
719         {
720             msg_Err( p_stream, "cannot create subtitles chain" );
721             free( id );
722             return NULL;
723         }
724     }
725     else
726     {
727         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
728                  (char*)&p_fmt->i_codec );
729         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
730         id->b_transcode = VLC_FALSE;
731
732         if( id->id == NULL )
733         {
734             free( id );
735             return NULL;
736         }
737     }
738
739     return id;
740 }
741
742 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
743 {
744     sout_stream_sys_t   *p_sys = p_stream->p_sys;
745
746     if( id->b_transcode )
747     {
748         if( id->f_src.i_cat == AUDIO_ES )
749         {
750             transcode_audio_ffmpeg_close( p_stream, id );
751         }
752         else if( id->f_src.i_cat == VIDEO_ES )
753         {
754             transcode_video_ffmpeg_close( p_stream, id );
755         }
756         else if( id->f_src.i_cat == SPU_ES )
757         {
758             transcode_spu_close( p_stream, id );
759         }
760     }
761
762     if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
763     free( id );
764
765     return VLC_SUCCESS;
766 }
767
768 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
769                  block_t *p_buffer )
770 {
771     sout_stream_sys_t *p_sys = p_stream->p_sys;
772
773     if( id->b_transcode )
774     {
775         block_t *p_buffer_out;
776
777         /* Be sure to have padding */
778         p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
779         if( p_buffer == NULL )
780         {
781             return VLC_EGENERIC;
782         }
783         p_buffer->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
784         memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
785
786         if( id->f_src.i_cat == AUDIO_ES )
787         {
788             transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
789                                             &p_buffer_out );
790             block_Release( p_buffer );
791         }
792         else if( id->f_src.i_cat == VIDEO_ES )
793         {
794             if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
795                 &p_buffer_out ) != VLC_SUCCESS )
796             {
797                 block_Release( p_buffer );
798                 return VLC_EGENERIC;
799             }
800             block_Release( p_buffer );
801         }
802         else if( id->f_src.i_cat == SPU_ES )
803         {
804             if( transcode_spu_process( p_stream, id, p_buffer,
805                 &p_buffer_out ) != VLC_SUCCESS )
806             {
807                 return VLC_EGENERIC;
808             }
809         }
810         else
811         {
812             block_Release( p_buffer );
813         }
814
815         if( p_buffer_out )
816         {
817             return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
818         }
819         return VLC_SUCCESS;
820     }
821     else if( id->id != NULL )
822     {
823         return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
824     }
825     else
826     {
827         block_Release( p_buffer );
828         return VLC_EGENERIC;
829     }
830 }
831
832 /****************************************************************************
833  * ffmpeg decoder reencoder part
834  ****************************************************************************/
835 static struct
836 {
837     vlc_fourcc_t i_fcc;
838     int          i_ff_codec;
839 } fourcc_to_ff_code[] =
840 {
841     /* audio */
842     { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
843     { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
844     { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
845     { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
846     { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
847     { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
848     { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
849     { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
850     { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
851
852     /* video */
853     { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
854     { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
855 #if LIBAVCODEC_BUILD >= 4676
856     { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
857 #endif
858     { VLC_FOURCC( 'm', 'p', '4', 'v'),  CODEC_ID_MPEG4 },
859     { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
860     { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
861     { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
862     { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
863     { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
864     { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
865     { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
866     { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
867     { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
868     { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
869     { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
870     { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
871 #if LIBAVCODEC_BUILD >= 4666
872     { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
873     { VLC_FOURCC( 'h', '2', '6', '4' ), CODEC_ID_H264 },
874 #endif
875 #if LIBAVCODEC_BUILD >= 4719
876     { VLC_FOURCC( 'S', 'N', 'O', 'W' ), CODEC_ID_SNOW },
877 #endif
878
879     /* raw video code, only used for 'encoding' */
880     { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
881     { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
882     { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
883     { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
884     { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
885     { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
886     { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
887     { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
888     { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
889     { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
890
891     { VLC_FOURCC(   0,   0,   0,   0 ), 0 }
892 };
893
894 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
895 {
896     int i;
897
898     for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
899     {
900         if( fourcc_to_ff_code[i].i_fcc == i_fcc )
901         {
902             return fourcc_to_ff_code[i].i_ff_codec;
903         }
904     }
905
906     return 0;
907 }
908
909 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
910 {
911     switch( i_chroma )
912     {
913         case VLC_FOURCC( 'Y', 'V', '1', '2' ):
914         case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
915         case VLC_FOURCC( 'I', '4', '2', '0' ):
916             return PIX_FMT_YUV420P;
917         case VLC_FOURCC( 'I', '4', '2', '2' ):
918             return PIX_FMT_YUV422P;
919         case VLC_FOURCC( 'I', '4', '4', '4' ):
920             return PIX_FMT_YUV444P;
921         case VLC_FOURCC( 'R', 'V', '1', '5' ):
922             return PIX_FMT_RGB555;
923         case VLC_FOURCC( 'R', 'V', '1', '6' ):
924             return PIX_FMT_RGB565;
925         case VLC_FOURCC( 'R', 'V', '2', '4' ):
926             return PIX_FMT_BGR24;
927         case VLC_FOURCC( 'R', 'V', '3', '2' ):
928             return PIX_FMT_RGBA32;
929         case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
930             return PIX_FMT_GRAY8;
931         case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
932             return PIX_FMT_YUV422;
933         default:
934             return 0;
935     }
936 }
937
938 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
939 {
940     switch( i_pix_fmt )
941     {
942     case PIX_FMT_YUV420P:
943         return VLC_FOURCC('I','4','2','0');
944     case PIX_FMT_YUV422P:
945         return VLC_FOURCC('I','4','2','2');
946     case PIX_FMT_YUV444P:
947         return VLC_FOURCC('I','4','4','4');
948
949     case PIX_FMT_YUV422:
950         return VLC_FOURCC('Y','U','Y','2');
951
952     case PIX_FMT_RGB555:
953         return VLC_FOURCC('R','V','1','5');
954     case PIX_FMT_RGB565:
955         return VLC_FOURCC('R','V','1','6');
956     case PIX_FMT_RGB24:
957         return VLC_FOURCC('R','V','2','4');
958     case PIX_FMT_RGBA32:
959         return VLC_FOURCC('R','V','3','2');
960     case PIX_FMT_GRAY8:
961         return VLC_FOURCC('G','R','E','Y');
962
963     case PIX_FMT_YUV410P:
964     case PIX_FMT_YUV411P:
965     case PIX_FMT_BGR24:
966     default:
967         return 0;
968     }
969 }
970
971 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
972                                        sout_stream_id_t *id )
973 {
974     int i_ff_codec;
975
976     if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
977         id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
978         id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
979         id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
980     {
981         id->ff_dec = NULL;
982
983         id->ff_dec_c = avcodec_alloc_context();
984         id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
985         id->ff_dec_c->channels    = id->f_src.audio.i_channels;
986         id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
987         id->ff_dec_c->bit_rate    = id->f_src.i_bitrate;
988     }
989     else
990     {
991         /* find decoder */
992         i_ff_codec = get_ff_codec( id->f_src.i_codec );
993         if( i_ff_codec == 0 )
994         {
995             msg_Err( p_stream, "cannot find decoder id" );
996             return VLC_EGENERIC;
997         }
998
999         id->ff_dec = avcodec_find_decoder( i_ff_codec );
1000         if( !id->ff_dec )
1001         {
1002             msg_Err( p_stream, "cannot find decoder (avcodec)" );
1003             return VLC_EGENERIC;
1004         }
1005
1006         id->ff_dec_c = avcodec_alloc_context();
1007         id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
1008         id->ff_dec_c->channels    = id->f_src.audio.i_channels;
1009         id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
1010         id->ff_dec_c->bit_rate    = id->f_src.i_bitrate;
1011
1012         id->ff_dec_c->extradata_size = id->f_src.i_extra;
1013         id->ff_dec_c->extradata      = id->f_src.p_extra;
1014         if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
1015         {
1016             msg_Err( p_stream, "cannot open decoder" );
1017             av_free( id->ff_dec_c );
1018             return VLC_EGENERIC;
1019         }
1020     }
1021
1022     id->i_buffer     = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1023     id->i_buffer_pos = 0;
1024     id->p_buffer     = malloc( id->i_buffer );
1025
1026     /* Sanity check for audio channels */
1027     id->f_dst.audio.i_channels =
1028         __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
1029
1030     /* find encoder */
1031     id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1032
1033     /* Initialization of encoder format structures */
1034     es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1035     id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1036     id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
1037     id->p_encoder->fmt_in.audio.i_physical_channels =
1038         id->p_encoder->fmt_in.audio.i_original_channels =
1039             pi_channels_maps[id->f_dst.audio.i_channels];
1040     id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
1041     id->p_encoder->fmt_in.audio.i_bitspersample = 16;
1042
1043     id->p_encoder->fmt_out = id->f_dst;
1044
1045     id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1046
1047     /* Attach object to parent so object variables inheritance works */
1048     vlc_object_attach( id->p_encoder, p_stream );
1049
1050     id->p_encoder->p_module =
1051         module_Need( id->p_encoder, "encoder",
1052                      p_stream->p_sys->psz_aenc, VLC_TRUE );
1053     if( !id->p_encoder->p_module )
1054     {
1055         vlc_object_detach( id->p_encoder );
1056         vlc_object_destroy( id->p_encoder );
1057         msg_Err( p_stream, "cannot open encoder" );
1058         av_free( id->ff_dec_c );
1059         return VLC_EGENERIC;
1060     }
1061
1062     id->b_enc_inited = VLC_FALSE;
1063
1064     id->f_dst = id->p_encoder->fmt_out;
1065
1066     /* Hack for mp3 transcoding support */
1067     if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1068     {
1069         id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
1070     }
1071
1072     return VLC_SUCCESS;
1073 }
1074
1075 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
1076                                           sout_stream_id_t *id )
1077 {
1078     if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1079     av_free( id->ff_dec_c );
1080
1081     module_Unneed( id->p_encoder, id->p_encoder->p_module );
1082
1083     vlc_object_detach( id->p_encoder );
1084     vlc_object_destroy( id->p_encoder );
1085
1086     free( id->p_buffer );
1087 }
1088
1089 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
1090                                            sout_stream_id_t *id,
1091                                            block_t *in,
1092                                            block_t **out )
1093 {
1094     sout_stream_sys_t *p_sys = p_stream->p_sys;
1095     aout_buffer_t aout_buf;
1096     block_t *p_block;
1097     int i_buffer = in->i_buffer;
1098     char *p_buffer = in->p_buffer;
1099     id->i_dts = in->i_dts;
1100     *out = NULL;
1101
1102     while( i_buffer )
1103     {
1104         id->i_buffer_pos = 0;
1105
1106         /* decode as much data as possible */
1107         if( id->ff_dec )
1108         {
1109             int i_used;
1110
1111             i_used = avcodec_decode_audio( id->ff_dec_c,
1112                          (int16_t*)id->p_buffer, &id->i_buffer_pos,
1113                          p_buffer, i_buffer );
1114
1115 #if 0
1116             msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
1117                       i_used, i_buffer );
1118 #endif
1119             if( i_used < 0 )
1120             {
1121                 msg_Warn( p_stream, "error audio decoding");
1122                 break;
1123             }
1124
1125             i_buffer -= i_used;
1126             p_buffer += i_used;
1127
1128             if ( id->i_buffer_pos < 0 )
1129             {
1130                 msg_Warn( p_stream, "weird error audio decoding");
1131                 break;
1132             }
1133         }
1134         else
1135         {
1136             int16_t *sout = (int16_t*)id->p_buffer;
1137
1138             if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
1139                 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
1140             {
1141                 int8_t *sin = (int8_t*)p_buffer;
1142                 int i_used = __MIN( id->i_buffer/2, i_buffer );
1143                 int i_samples = i_used;
1144
1145                 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
1146                     while( i_samples > 0 )
1147                     {
1148                         *sout++ = ( *sin++ ) << 8;
1149                         i_samples--;
1150                     }
1151                 else
1152                     while( i_samples > 0 )
1153                     {
1154                         *sout++ = ( *sin++ - 128 ) << 8;
1155                         i_samples--;
1156                     }
1157
1158                 i_buffer -= i_used;
1159                 p_buffer += i_used;
1160                 id->i_buffer_pos = i_used * 2;
1161             }
1162             else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
1163                      id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
1164             {
1165                 int16_t *sin = (int16_t*)p_buffer;
1166                 int i_used = __MIN( id->i_buffer, i_buffer );
1167                 int i_samples = i_used / 2;
1168
1169                 /* first copy */
1170                 memcpy( sout, sin, i_used );
1171
1172 #ifdef WORDS_BIGENDIAN
1173                 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
1174 #else
1175                 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
1176 #endif
1177                 {
1178                     uint8_t *dat = (uint8_t*)sout;
1179
1180                     while( i_samples > 0 )
1181                     {
1182                         uint8_t tmp;
1183                         tmp    = dat[0];
1184                         dat[0] = dat[1];
1185                         dat[1] = tmp;
1186
1187                         dat += 2;
1188
1189                         i_samples--;
1190                     }
1191                 }
1192
1193                 i_buffer -= i_used;
1194                 p_buffer += i_used;
1195                 id->i_buffer_pos = i_used;
1196             }
1197         }
1198
1199         if( id->i_buffer_pos == 0 ) continue;
1200
1201         /* Encode as much data as possible */
1202         if( !id->b_enc_inited && id->p_encoder->pf_header )
1203         {
1204             block_t *p_block_tmp;
1205
1206             p_block = id->p_encoder->pf_header( id->p_encoder );
1207             p_block_tmp = p_block;
1208             while( p_block_tmp )
1209             {
1210                 p_block_tmp->i_dts = p_block_tmp->i_pts = in->i_dts;
1211                 p_block_tmp = p_block_tmp->p_next;
1212             }
1213             block_ChainAppend( out, p_block );
1214
1215             id->b_enc_inited = VLC_TRUE;
1216         }
1217
1218         aout_buf.p_buffer = id->p_buffer;
1219         aout_buf.i_nb_bytes = id->i_buffer_pos;
1220         aout_buf.i_nb_samples = id->i_buffer_pos/2/id->f_src.audio.i_channels;
1221         aout_buf.start_date = id->i_dts;
1222         aout_buf.end_date = id->i_dts;
1223
1224         if( p_sys->b_audio_sync )
1225         {
1226             aout_buf.start_date = date_Get( &id->interpolated_pts ) + 1;
1227             p_sys->i_master_drift = id->i_dts - aout_buf.start_date;
1228             date_Increment( &id->interpolated_pts, aout_buf.i_nb_samples );
1229         }
1230
1231         id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
1232             id->f_src.audio.i_channels / id->f_src.audio.i_rate );
1233
1234         if( id->p_encoder->fmt_in.audio.i_channels == 1 &&
1235             id->f_src.audio.i_channels > 1 )
1236         {
1237             int16_t *p_sample = (int16_t *)aout_buf.p_buffer;
1238             int i_src_c = id->f_src.audio.i_channels;
1239             unsigned int i;
1240
1241             for( i = 0; i < aout_buf.i_nb_samples; i++ )
1242             {
1243                 int j, c = 0;
1244
1245                 for( j = 1; j < i_src_c; j++ )
1246                 {
1247                     c += p_sample[i_src_c * i + j];
1248                 }
1249                 p_sample[i] = c / (i_src_c-1);
1250             }
1251             aout_buf.i_nb_bytes = i * 2;
1252         }
1253         else if( id->p_encoder->fmt_in.audio.i_channels == 2 &&
1254                  id->f_src.audio.i_channels > 2 )
1255         {
1256             int i_src_c = id->f_src.audio.i_channels;
1257             unsigned int i;
1258
1259             static const float mixf_l[4][6] = /* [i_src_c - 3][channel index] */
1260             {
1261                 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1262                 { 0.00, 0.50, 0.50, 0.00, 0.00, 0.00 }, /* 4 channels */
1263                 { 0.00, 0.50, 0.00, 0.50, 0.00, 0.00 }, /* 5 channels */
1264                 { 0.00, 0.34, 0.33, 0.00, 0.33, 0.00 }, /* 6 channels */
1265             };
1266             static const float mixf_r[4][6] = /* [i_src_c - 3][channel index] */
1267             {
1268                 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1269                 { 0.00, 0.00, 0.50, 0.50, 0.00, 0.00 }, /* 4 channels */
1270                 { 0.00, 0.00, 0.50, 0.00, 0.50, 0.00 }, /* 5 channels */
1271                 { 0.00, 0.00, 0.33, 0.34, 0.00, 0.33 }, /* 6 channels */
1272             };
1273
1274
1275             for( i = 0; i < aout_buf.i_nb_samples; i++ )
1276             {
1277                 int16_t *p_src = (int16_t *)aout_buf.p_buffer + i_src_c * i;
1278                 int16_t *p_dst = (int16_t *)aout_buf.p_buffer + 2 * i;
1279
1280                 int j;
1281                 float l = 0.0, r = 0.0;
1282                 for( j = 0; j < i_src_c; j++ )
1283                 {
1284                     l += mixf_l[i_src_c-3][j] * p_src[j];
1285                     r += mixf_r[i_src_c-3][j] * p_src[j];
1286                 }
1287
1288                 p_dst[0] = (int)( l + 0.5 );
1289                 p_dst[1] = (int)( r + 0.5 );
1290             }
1291             aout_buf.i_nb_bytes = i * 2 * 2;
1292         }
1293         else if( id->f_src.audio.i_channels !=
1294                  id->p_encoder->fmt_in.audio.i_channels )
1295         {
1296             unsigned int i;
1297             int j;
1298
1299             /* This is for liba52 which is what ffmpeg uses to decode ac3 */
1300             static const int translation[7][6] =
1301             {{ 0, 0, 0, 0, 0, 0 },      /* 0 channels (rarely used) */
1302              { 0, 0, 0, 0, 0, 0 },       /* 1 ch */
1303              { 0, 1, 0, 0, 0, 0 },       /* 2 */
1304              { 1, 2, 0, 0, 0, 0 },       /* 3 */
1305              { 1, 3, 2, 0, 0, 0 },       /* 4 */
1306              { 1, 3, 4, 2, 0, 0 },       /* 5 */
1307              { 1, 3, 4, 5, 2, 0 }};      /* 6 */
1308
1309             /* dumb downmixing */
1310             for( i = 0; i < aout_buf.i_nb_samples; i++ )
1311             {
1312                 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
1313                 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
1314                 {
1315                     p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
1316                         p_buffer[i*id->f_src.audio.i_channels+
1317                                  translation[id->f_src.audio.i_channels][j]];
1318                 }
1319             }
1320             aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1321         }
1322
1323         p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1324         block_ChainAppend( out, p_block );
1325     }
1326
1327     return VLC_SUCCESS;
1328 }
1329
1330
1331 /*
1332  * video
1333  */
1334 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1335                                        sout_stream_id_t *id )
1336 {
1337     sout_stream_sys_t *p_sys = p_stream->p_sys;
1338     int i_ff_codec;
1339
1340     /* Open decoder */
1341     if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1342         id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1343         id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1344         id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1345         id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1346         id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1347         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1348         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1349         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1350         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1351         id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1352     {
1353         id->ff_dec              = NULL;
1354         id->ff_dec_c            = avcodec_alloc_context();
1355         id->ff_dec_c->width     = id->f_src.video.i_width;
1356         id->ff_dec_c->height    = id->f_src.video.i_height;
1357         id->ff_dec_c->pix_fmt   = get_ff_chroma( id->f_src.i_codec );
1358
1359 #if LIBAVCODEC_BUILD >= 4687
1360         if( id->ff_dec_c->width )
1361         id->ff_dec_c->sample_aspect_ratio =
1362             av_d2q( id->f_src.video.i_aspect / (double)VOUT_ASPECT_FACTOR *
1363                     id->ff_dec_c->height / id->ff_dec_c->width, 255 );
1364 #else
1365         id->ff_dec_c->aspect_ratio =
1366             id->f_src.video.i_aspect / (float)VOUT_ASPECT_FACTOR;
1367 #endif
1368     }
1369     else
1370     {
1371         /* find decoder */
1372         i_ff_codec = get_ff_codec( id->f_src.i_codec );
1373         if( i_ff_codec == 0 )
1374         {
1375             msg_Err( p_stream, "cannot find decoder" );
1376             return VLC_EGENERIC;
1377         }
1378
1379         id->ff_dec = avcodec_find_decoder( i_ff_codec );
1380         if( !id->ff_dec )
1381         {
1382             msg_Err( p_stream, "cannot find decoder" );
1383             return VLC_EGENERIC;
1384         }
1385
1386         id->ff_dec_c = avcodec_alloc_context();
1387         id->ff_dec_c->width         = id->f_src.video.i_width;
1388         id->ff_dec_c->height        = id->f_src.video.i_height;
1389         id->ff_dec_c->bits_per_sample=id->f_src.video.i_bits_per_pixel;
1390         /* id->ff_dec_c->bit_rate      = id->f_src.i_bitrate; */
1391
1392         if( id->f_src.i_extra > 0 )
1393         {
1394             if( i_ff_codec == CODEC_ID_SVQ3 )
1395             {
1396                 int i_size = id->f_src.i_extra;
1397                 uint8_t *p;
1398
1399                 id->ff_dec_c->extradata_size = i_size + 12;
1400                 p = id->ff_dec_c->extradata  = malloc( i_size + 12 );
1401
1402                 memcpy( &p[0],  "SVQ3", 4 );
1403                 memset( &p[4], 0, 8 );
1404                 memcpy( &p[12], id->f_src.p_extra, i_size );
1405
1406                 /* Now remove all atoms before the SMI one */
1407                 if( id->ff_dec_c->extradata_size > 0x5a && strncmp( &p[0x56], "SMI ", 4 ) )
1408                 {
1409                     uint8_t *psz = &p[0x52];
1410
1411                     while( psz < &p[id->ff_dec_c->extradata_size - 8] )
1412                     {
1413                         int i_size = GetDWBE( psz );
1414                         if( i_size <= 1 )
1415                         {
1416                             /* FIXME handle 1 as long size */
1417                             break;
1418                         }
1419                         if( !strncmp( &psz[4], "SMI ", 4 ) )
1420                         {
1421                             memmove( &p[0x52], psz, &p[id->ff_dec_c->extradata_size] - psz );
1422                             break;
1423                         }
1424                         psz += i_size;
1425                     }
1426                 }
1427             }
1428             else
1429             {
1430                 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1431                 id->ff_dec_c->extradata = malloc( id->f_src.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
1432
1433                 memcpy( id->ff_dec_c->extradata, id->f_src.p_extra, id->f_src.i_extra );
1434                 memset( (uint8_t*)id->ff_dec_c->extradata + id->f_src.i_extra, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1435             }
1436         }
1437         id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1438         id->ff_dec_c->error_resilience= -1;
1439         id->ff_dec_c->get_buffer    = transcode_video_ffmpeg_getframebuf;
1440         id->ff_dec_c->opaque        = p_sys;
1441
1442         if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1443         {
1444             msg_Err( p_stream, "cannot open decoder" );
1445             av_free( id->ff_dec_c );
1446             return VLC_EGENERIC;
1447         }
1448     }
1449
1450     /* Open encoder */
1451     id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1452
1453     /* Initialization of encoder format structures */
1454     es_format_Init( &id->p_encoder->fmt_in,
1455                     id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1456
1457     /* The dimensions will be set properly later on.
1458      * Just put sensible values so we can test if there is an encoder. */
1459     id->p_encoder->fmt_in.video.i_width =
1460         id->f_src.video.i_width ?  id->f_src.video.i_width : 16;
1461     id->p_encoder->fmt_in.video.i_height =
1462         id->f_src.video.i_height ? id->f_src.video.i_height : 16;
1463
1464     id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1465     id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1466     if( id->ff_dec )
1467     {
1468         id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1469 #if LIBAVCODEC_BUILD >= 4662
1470         id->p_encoder->fmt_in.video.i_frame_rate_base =
1471             id->ff_dec_c->frame_rate_base;
1472 #endif
1473
1474 #if LIBAVCODEC_BUILD >= 4687
1475         if( id->ff_dec_c->height )
1476         id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1477             ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1478               id->ff_dec_c->width / id->ff_dec_c->height );
1479 #else
1480         id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1481             id->ff_dec_c->aspect_ratio;
1482 #endif
1483     }
1484
1485     /* Override with user settings */
1486     if( p_sys->f_fps > 0 )
1487     {
1488         id->p_encoder->fmt_in.video.i_frame_rate = p_sys->f_fps * 1000;
1489         id->p_encoder->fmt_in.video.i_frame_rate_base = 1000;
1490     }
1491
1492     id->f_dst.video.i_frame_rate = id->p_encoder->fmt_in.video.i_frame_rate;
1493     id->f_dst.video.i_frame_rate_base =
1494         id->p_encoder->fmt_in.video.i_frame_rate_base;
1495
1496     /* Check whether a particular aspect ratio was requested */
1497     if( id->f_src.video.i_aspect )
1498     {
1499         id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1500         id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1501     }
1502
1503     id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1504     id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1505     id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1506
1507     id->p_encoder->i_threads = p_sys->i_threads;
1508
1509     id->p_ff_pic         = avcodec_alloc_frame();
1510     id->p_ff_pic_tmp0    = NULL;
1511     id->p_ff_pic_tmp1    = NULL;
1512     id->p_ff_pic_tmp2    = NULL;
1513     id->p_ff_pic_tmp3    = NULL;
1514     id->p_vresample      = NULL;
1515
1516     id->p_encoder->p_cfg = p_sys->p_video_cfg;
1517
1518     /* Attach object to parent so object variables inheritance works */
1519     vlc_object_attach( id->p_encoder, p_stream );
1520
1521     id->p_encoder->p_module =
1522         module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1523
1524     if( !id->p_encoder->p_module )
1525     {
1526         vlc_object_detach( id->p_encoder );
1527         vlc_object_destroy( id->p_encoder );
1528         av_free( id->ff_dec_c );
1529         msg_Err( p_stream, "cannot find encoder" );
1530         return VLC_EGENERIC;
1531     }
1532
1533     /* Close the encoder.
1534      * We'll open it only when we have the first frame */
1535     module_Unneed( id->p_encoder, id->p_encoder->p_module );
1536     id->p_encoder->p_module = NULL;
1537
1538     id->b_enc_inited = VLC_FALSE;
1539
1540     if ( p_sys->i_threads >= 1 )
1541     {
1542         p_sys->id_video = id;
1543         vlc_mutex_init( p_stream, &p_sys->lock_out );
1544         vlc_cond_init( p_stream, &p_sys->cond );
1545         memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1546         p_sys->i_first_pic = 0;
1547         p_sys->i_last_pic = 0;
1548         p_sys->p_buffers = NULL;
1549         p_sys->b_die = p_sys->b_error = 0;
1550         if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1551                                VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1552         {
1553             vlc_object_detach( id->p_encoder );
1554             vlc_object_destroy( id->p_encoder );
1555             av_free( id->ff_dec_c );
1556             msg_Err( p_stream, "cannot spawn encoder thread" );
1557             return VLC_EGENERIC;
1558         }
1559     }
1560
1561     return VLC_SUCCESS;
1562 }
1563
1564 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1565                                            sout_stream_id_t *id )
1566 {
1567     if ( p_stream->p_sys->i_threads >= 1 )
1568     {
1569        vlc_mutex_lock( &p_stream->p_sys->lock_out );
1570        p_stream->p_sys->b_die = 1;
1571        vlc_cond_signal( &p_stream->p_sys->cond );
1572        vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1573        vlc_thread_join( p_stream->p_sys );
1574        vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1575        vlc_cond_destroy( &p_stream->p_sys->cond );
1576     }
1577
1578     /* Close decoder */
1579     if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1580     av_free( id->ff_dec_c );
1581
1582     /* Close encoder */
1583     if( id->p_encoder->p_module )
1584         module_Unneed( id->p_encoder, id->p_encoder->p_module );
1585     vlc_object_detach( id->p_encoder );
1586     vlc_object_destroy( id->p_encoder );
1587
1588     /* Misc cleanup */
1589     if( id->p_ff_pic)
1590     {
1591         free( id->p_ff_pic );
1592     }
1593
1594     if( id->p_ff_pic_tmp0 )
1595     {
1596         free( id->p_ff_pic_tmp0->data[0] );
1597         free( id->p_ff_pic_tmp0 );
1598     }
1599     if( id->p_ff_pic_tmp1 )
1600     {
1601         free( id->p_ff_pic_tmp1->data[0] );
1602         free( id->p_ff_pic_tmp1 );
1603     }
1604     if( id->p_ff_pic_tmp2 )
1605     {
1606         free( id->p_ff_pic_tmp2->data[0] );
1607         free( id->p_ff_pic_tmp2 );
1608     }
1609     if( id->p_ff_pic_tmp3 )
1610     {
1611         free( id->p_ff_pic_tmp3->data[0] );
1612         free( id->p_ff_pic_tmp3 );
1613     }
1614     if( id->p_vresample )
1615     {
1616         img_resample_close( id->p_vresample );
1617     }
1618 }
1619
1620 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1621                sout_stream_id_t *id, block_t *in, block_t **out )
1622 {
1623     sout_stream_sys_t *p_sys = p_stream->p_sys;
1624     int i_used, b_gotpicture, i_duplicate = 1;
1625     AVFrame *frame;
1626     mtime_t i_pts;
1627
1628     int i_data;
1629     uint8_t *p_data;
1630
1631     *out = NULL;
1632
1633     i_data = in->i_buffer;
1634     p_data = in->p_buffer;
1635
1636     for( ;; )
1637     {
1638         block_t *p_block;
1639         picture_t * p_pic;
1640         int i_plane;
1641         subpicture_t *p_subpic = 0;
1642
1643         /* decode frame */
1644         frame = id->p_ff_pic;
1645         p_sys->i_input_pts = in->i_pts;
1646         p_sys->i_input_dts = in->i_dts;
1647         if( id->ff_dec )
1648         {
1649             i_used = avcodec_decode_video( id->ff_dec_c, frame,
1650                                            &b_gotpicture,
1651                                            p_data, i_data );
1652         }
1653         else
1654         {
1655             /* raw video */
1656             avpicture_fill( (AVPicture*)frame, p_data,
1657                             id->ff_dec_c->pix_fmt,
1658                             id->ff_dec_c->width, id->ff_dec_c->height );
1659             i_used = i_data;
1660             b_gotpicture = 1;
1661
1662             /* Set PTS */
1663             frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts :
1664                          AV_NOPTS_VALUE;
1665
1666             frame->pict_type = FF_I_TYPE;
1667         }
1668
1669         if( i_used < 0 )
1670         {
1671             msg_Warn( p_stream, "error");
1672             return VLC_EGENERIC;
1673         }
1674         i_data -= i_used;
1675         p_data += i_used;
1676
1677         if( !b_gotpicture )
1678         {
1679             return VLC_SUCCESS;
1680         }
1681
1682         /* Get the pts of the decoded frame if any, otherwise keep the
1683          * interpolated one */
1684         if( frame->pts != AV_NOPTS_VALUE )
1685         {
1686             p_sys->i_output_pts = frame->pts;
1687         }
1688         i_pts = p_sys->i_output_pts;
1689
1690         /* Sanity check (seems to be needed for some streams ) */
1691         if( frame->pict_type == FF_B_TYPE )
1692         {
1693             p_sys->b_input_has_b_frames = VLC_TRUE;
1694         }
1695
1696         if( p_sys->b_audio_sync )
1697         {
1698             mtime_t i_video_drift;
1699             mtime_t i_master_drift = p_sys->i_master_drift;
1700
1701             if( !id->i_initial_pts ) id->i_initial_pts = p_sys->i_output_pts;
1702
1703             if( !i_master_drift )
1704             {
1705                 /* No audio track ? */
1706                 i_master_drift = id->i_initial_pts;
1707             }
1708
1709             i_pts = date_Get( &id->interpolated_pts ) + 1;
1710             i_video_drift = p_sys->i_output_pts - i_pts;
1711             i_duplicate = 1;
1712
1713             if( i_video_drift < i_master_drift - 50000 )
1714             {
1715                 msg_Dbg( p_stream, "dropping frame (%i)",
1716                          (int)(i_video_drift - i_master_drift) );
1717                 return VLC_EGENERIC;
1718             }
1719             else if( i_video_drift > i_master_drift + 50000 )
1720             {
1721                 msg_Dbg( p_stream, "adding frame (%i)",
1722                          (int)(i_video_drift - i_master_drift) );
1723                 i_duplicate = 2;
1724             }
1725
1726             date_Increment( &id->interpolated_pts, 1 );
1727         }
1728
1729         if( !id->b_enc_inited )
1730         {
1731             /* Hack because of the copy packetizer which can fail to detect the
1732              * proper size (which forces us to wait until the 1st frame
1733              * is decoded) */
1734             int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1735                           p_sys->i_crop_right;
1736             int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1737                            p_sys->i_crop_bottom;
1738
1739             if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1740                 && p_sys->f_scale )
1741             {
1742                 /* Apply the scaling */
1743                 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1744                 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1745             }
1746             else if( id->f_dst.video.i_width > 0 &&
1747                      id->f_dst.video.i_height <= 0 )
1748             {
1749                 id->f_dst.video.i_height =
1750                     id->f_dst.video.i_width / (double)i_width * i_height;
1751             }
1752             else if( id->f_dst.video.i_width <= 0 &&
1753                      id->f_dst.video.i_height > 0 )
1754             {
1755                 id->f_dst.video.i_width =
1756                     id->f_dst.video.i_height / (double)i_height * i_width;
1757             }
1758
1759             id->p_encoder->fmt_in.video.i_width = id->f_dst.video.i_width;
1760             id->p_encoder->fmt_in.video.i_height = id->f_dst.video.i_height;
1761             id->p_encoder->fmt_out = id->f_dst;
1762
1763             id->p_encoder->p_module =
1764                 module_Need( id->p_encoder, "encoder",
1765                              p_sys->psz_venc, VLC_TRUE );
1766             if( !id->p_encoder->p_module )
1767             {
1768                 vlc_object_destroy( id->p_encoder );
1769                 msg_Err( p_stream, "cannot find encoder" );
1770                 id->b_transcode = VLC_FALSE;
1771                 return VLC_EGENERIC;
1772             }
1773
1774             id->f_dst = id->p_encoder->fmt_out;
1775
1776             /* Hack for mp2v/mp1v transcoding support */
1777             if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1778                 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1779             {
1780                 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1781             }
1782
1783             if( !( id->id =
1784                      p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1785                                                      &id->f_dst ) ) )
1786             {
1787                 msg_Err( p_stream, "cannot add this stream" );
1788                 transcode_video_ffmpeg_close( p_stream, id );
1789                 id->b_transcode = VLC_FALSE;
1790                 return VLC_EGENERIC;
1791             }
1792
1793             if( id->p_encoder->pf_header )
1794             {
1795                 block_t *p_block_tmp;
1796
1797                 p_block = id->p_encoder->pf_header( id->p_encoder );
1798                 p_block_tmp = p_block;
1799                 while( p_block_tmp )
1800                 {
1801                     p_block_tmp->i_dts = p_block_tmp->i_pts = in->i_dts;
1802                     p_block_tmp = p_block_tmp->p_next;
1803                 }
1804                 block_ChainAppend( out, p_block );
1805             }
1806
1807             id->i_inter_pixfmt =
1808                 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1809
1810             id->b_enc_inited = VLC_TRUE;
1811         }
1812
1813         /* deinterlace */
1814         if( p_stream->p_sys->b_deinterlace )
1815         {
1816             if( id->p_ff_pic_tmp0 == NULL )
1817             {
1818                 int     i_size;
1819                 uint8_t *buf;
1820                 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1821                 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1822                                              id->ff_dec_c->width,
1823                                              id->ff_dec_c->height );
1824
1825                 buf = malloc( i_size );
1826
1827                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1828                                 id->i_inter_pixfmt,
1829                                 id->ff_dec_c->width, id->ff_dec_c->height );
1830             }
1831
1832             avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0,
1833                                    (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1834                                    id->ff_dec_c->width, id->ff_dec_c->height );
1835
1836 #if LIBAVCODEC_BUILD >= 4685
1837             id->p_ff_pic_tmp0->interlaced_frame = 0;
1838 #endif
1839             id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1840             frame = id->p_ff_pic_tmp0;
1841         }
1842
1843         /* convert pix format */
1844         if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1845         {
1846             if( id->p_ff_pic_tmp1 == NULL )
1847             {
1848                 int     i_size;
1849                 uint8_t *buf;
1850                 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1851                 i_size = avpicture_get_size( id->i_inter_pixfmt,
1852                                              id->ff_dec_c->width,
1853                                              id->ff_dec_c->height );
1854
1855                 buf = malloc( i_size );
1856
1857                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1858                                 id->i_inter_pixfmt,
1859                                 id->ff_dec_c->width, id->ff_dec_c->height );
1860             }
1861
1862             img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1863                          (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1864                          id->ff_dec_c->width, id->ff_dec_c->height );
1865
1866             id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1867 #if LIBAVCODEC_BUILD >= 4685
1868             id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1869             id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1870 #endif
1871             frame = id->p_ff_pic_tmp1;
1872         }
1873
1874         /* convert size and crop */
1875         if( id->ff_dec_c->width  != id->f_dst.video.i_width ||
1876             id->ff_dec_c->height != id->f_dst.video.i_height ||
1877             p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1878             p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1879         {
1880             if( id->p_ff_pic_tmp2 == NULL )
1881             {
1882                 int     i_size;
1883                 uint8_t *buf;
1884                 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1885                 i_size = avpicture_get_size( id->i_inter_pixfmt,
1886                                              id->f_dst.video.i_width,
1887                                              id->f_dst.video.i_height );
1888
1889                 buf = malloc( i_size );
1890
1891                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1892                                 id->i_inter_pixfmt,
1893                                 id->f_dst.video.i_width,
1894                                 id->f_dst.video.i_height );
1895
1896                 id->p_vresample =
1897                     img_resample_full_init( id->f_dst.video.i_width,
1898                                             id->f_dst.video.i_height,
1899                                             id->ff_dec_c->width,
1900                                             id->ff_dec_c->height,
1901                                             p_stream->p_sys->i_crop_top,
1902                                             p_stream->p_sys->i_crop_bottom,
1903                                             p_stream->p_sys->i_crop_left,
1904                                             p_stream->p_sys->i_crop_right
1905 #if LIBAVCODEC_BUILD >= 4708
1906                                             ,0, 0, 0, 0 );
1907 #else
1908                                           );
1909 #endif
1910             }
1911
1912             img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1913                           (AVPicture*)frame );
1914
1915             id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1916 #if LIBAVCODEC_BUILD >= 4685
1917             id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1918             id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1919 #endif
1920             frame = id->p_ff_pic_tmp2;
1921         }
1922
1923         /* Encoding */
1924         p_pic = malloc(sizeof(picture_t));
1925         vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1926                           id->p_encoder->fmt_in.i_codec,
1927                           id->f_dst.video.i_width, id->f_dst.video.i_height,
1928                           id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1929                           id->f_dst.video.i_height );
1930
1931         /* Check if we have a subpicture to overlay */
1932         if( p_sys->p_filter_blend )
1933         {
1934             p_subpic = transcode_spu_get( p_stream, id, p_sys->i_output_pts );
1935
1936             if( p_subpic && frame != id->p_ff_pic_tmp0 &&
1937                 frame != id->p_ff_pic_tmp1 && frame != id->p_ff_pic_tmp2 )
1938             {
1939                 if( id->p_ff_pic_tmp3 == NULL )
1940                 {
1941                     uint8_t *buf = malloc( frame->linesize[0] *
1942                                            p_pic->p[0].i_lines * 3 );
1943                     id->p_ff_pic_tmp3 = avcodec_alloc_frame();
1944                     *id->p_ff_pic_tmp3 = *frame;
1945                     id->p_ff_pic_tmp3->data[0] = buf;
1946                     id->p_ff_pic_tmp3->data[1] = id->p_ff_pic_tmp3->data[0] +
1947                         frame->linesize[0] * p_pic->p[0].i_lines;
1948                     id->p_ff_pic_tmp3->data[2] = id->p_ff_pic_tmp3->data[1] +
1949                         frame->linesize[1] * p_pic->p[1].i_lines;
1950                 }
1951
1952                 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1953                 {
1954                     p_stream->p_vlc->pf_memcpy(
1955                         id->p_ff_pic_tmp3->data[i_plane],
1956                         frame->data[i_plane],
1957                         p_pic->p[i_plane].i_lines * frame->linesize[i_plane] );
1958                 }
1959
1960                 id->p_ff_pic_tmp3->repeat_pict = frame->repeat_pict;
1961 #if LIBAVCODEC_BUILD >= 4685
1962                 id->p_ff_pic_tmp3->interlaced_frame = frame->interlaced_frame;
1963                 id->p_ff_pic_tmp3->top_field_first = frame->top_field_first;
1964 #endif
1965                 frame = id->p_ff_pic_tmp3;
1966             }
1967         }
1968
1969         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1970         {
1971             p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1972             if ( p_sys->i_threads >= 1 )
1973             {
1974                 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines *
1975                                                     p_pic->p[i_plane].i_pitch);
1976                 p_stream->p_vlc->pf_memcpy( p_pic->p[i_plane].p_pixels,
1977                     frame->data[i_plane], p_pic->p[i_plane].i_lines *
1978                      p_pic->p[i_plane].i_pitch );
1979             }
1980             else
1981             {
1982                 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1983             }
1984         }
1985
1986         /* Set the pts of the frame being encoded */
1987         p_pic->date = i_pts;
1988
1989         p_pic->i_nb_fields = frame->repeat_pict;
1990 #if LIBAVCODEC_BUILD >= 4685
1991         p_pic->b_progressive = !frame->interlaced_frame;
1992         p_pic->b_top_field_first = frame->top_field_first;
1993 #endif
1994
1995         /* Interpolate the next PTS
1996          * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1997         if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1998         {
1999             p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
2000               id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
2001         }
2002
2003         /* Overlay subpicture */
2004         if( p_subpic )
2005         {
2006             int i_width, i_height;
2007
2008             p_sys->p_filter_blend->fmt_out = id->p_encoder->fmt_in;
2009             p_sys->p_filter_blend->fmt_out.video.i_visible_width =
2010                 p_sys->p_filter_blend->fmt_out.video.i_width;
2011             p_sys->p_filter_blend->fmt_out.video.i_visible_height =
2012                 p_sys->p_filter_blend->fmt_out.video.i_height;
2013             p_sys->p_filter_blend->fmt_out.video.i_chroma =
2014                 VLC_FOURCC('I','4','2','0');
2015
2016             i_width = id->p_encoder->fmt_in.video.i_width;
2017             i_height = id->p_encoder->fmt_in.video.i_height;
2018
2019             while( p_subpic != NULL )
2020             {
2021                 subpicture_region_t *p_region = p_subpic->p_region;
2022
2023                 while( p_region && p_sys->p_filter_blend &&
2024                        p_sys->p_filter_blend->pf_video_blend )
2025                 {
2026                     int i_x_offset = p_region->i_x + p_subpic->i_x;
2027                     int i_y_offset = p_region->i_y + p_subpic->i_y;
2028
2029                     if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
2030                     {
2031                         i_y_offset = i_height - p_region->fmt.i_height -
2032                             p_subpic->i_y;
2033                     }
2034                     else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
2035                     {
2036                         i_y_offset = i_height / 2 - p_region->fmt.i_height / 2;
2037                     }
2038
2039                     if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
2040                     {
2041                         i_x_offset = i_width - p_region->fmt.i_width -
2042                             p_subpic->i_x;
2043                     }
2044                     else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
2045                     {
2046                         i_x_offset = i_width / 2 - p_region->fmt.i_width / 2;
2047                     }
2048
2049                     if( p_subpic->b_absolute )
2050                     {
2051                         i_x_offset = p_region->i_x + p_subpic->i_x;
2052                         i_y_offset = p_region->i_y + p_subpic->i_y;
2053                     }
2054
2055                     p_sys->p_filter_blend->fmt_in.video = p_region->fmt;
2056
2057                     p_sys->p_filter_blend->pf_video_blend(
2058                          p_sys->p_filter_blend, p_pic, p_pic,
2059                          &p_region->picture, i_x_offset, i_y_offset );
2060
2061                     p_region = p_region->p_next;
2062                 }
2063
2064                 p_subpic = p_subpic->p_next;
2065             }
2066         }
2067
2068         if ( p_sys->i_threads >= 1 )
2069         {
2070             vlc_mutex_lock( &p_sys->lock_out );
2071             p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
2072             p_sys->i_last_pic %= PICTURE_RING_SIZE;
2073             *out = p_sys->p_buffers;
2074             p_sys->p_buffers = NULL;
2075             vlc_cond_signal( &p_sys->cond );
2076             vlc_mutex_unlock( &p_sys->lock_out );
2077         }
2078         else
2079         {
2080             block_t *p_block;
2081             p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2082             block_ChainAppend( out, p_block );
2083
2084             if( p_sys->b_audio_sync && i_duplicate > 1 )
2085             {
2086                 i_pts = date_Get( &id->interpolated_pts ) + 1;
2087                 date_Increment( &id->interpolated_pts, 1 );
2088                 p_pic->date = i_pts;
2089                 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2090                 block_ChainAppend( out, p_block );
2091             }
2092
2093             free( p_pic );
2094         }
2095
2096         if( i_data <= 0 )
2097         {
2098             return VLC_SUCCESS;
2099         }
2100     }
2101
2102     return VLC_SUCCESS;
2103 }
2104
2105 static int EncoderThread( sout_stream_sys_t * p_sys )
2106 {
2107     sout_stream_id_t * id = p_sys->id_video;
2108     picture_t * p_pic;
2109     int i_plane;
2110
2111     while ( !p_sys->b_die && !p_sys->b_error )
2112     {
2113         block_t *p_block;
2114
2115         vlc_mutex_lock( &p_sys->lock_out );
2116         while ( p_sys->i_last_pic == p_sys->i_first_pic )
2117         {
2118             vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
2119             if ( p_sys->b_die || p_sys->b_error )
2120                 break;
2121         }
2122         if ( p_sys->b_die || p_sys->b_error )
2123         {
2124             vlc_mutex_unlock( &p_sys->lock_out );
2125             break;
2126         }
2127
2128         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2129         p_sys->i_first_pic %= PICTURE_RING_SIZE;
2130         vlc_mutex_unlock( &p_sys->lock_out );
2131
2132         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2133         vlc_mutex_lock( &p_sys->lock_out );
2134         block_ChainAppend( &p_sys->p_buffers, p_block );
2135         vlc_mutex_unlock( &p_sys->lock_out );
2136
2137         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
2138         {
2139             free( p_pic->p[i_plane].p_pixels );
2140         }
2141         free( p_pic );
2142     }
2143
2144     while ( p_sys->i_last_pic != p_sys->i_first_pic )
2145     {
2146         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2147         p_sys->i_first_pic %= PICTURE_RING_SIZE;
2148
2149         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
2150         {
2151             free( p_pic->p[i_plane].p_pixels );
2152         }
2153         free( p_pic );
2154     }
2155
2156     block_ChainRelease( p_sys->p_buffers );
2157
2158     return 0;
2159 }
2160
2161 /*****************************************************************************
2162  * transcode_video_ffmpeg_getframebuf:
2163  *
2164  * Callback used by ffmpeg to get a frame buffer.
2165  * We use it to get the right PTS for each decoded picture.
2166  *****************************************************************************/
2167 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
2168                                               AVFrame *p_frame)
2169 {
2170     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
2171
2172     /* Set PTS */
2173     if( p_sys->i_input_pts )
2174     {
2175         p_frame->pts = p_sys->i_input_pts;
2176     }
2177     else if( p_sys->i_input_dts )
2178     {
2179         /* Some demuxers/packetizers only set the dts so let's try to find a
2180          * useful timestamp from this */
2181         if( !p_context->has_b_frames || !p_sys->b_input_has_b_frames ||
2182             !p_frame->reference || !p_sys->i_output_pts )
2183         {
2184             p_frame->pts = p_sys->i_input_dts +
2185             /* Hack: ffmpeg encoding doesn't like frames with identical pts */
2186                 (p_sys->i_output_pts ? 0 : 50000);
2187         }
2188         else p_frame->pts = AV_NOPTS_VALUE;
2189     }
2190     else p_frame->pts = AV_NOPTS_VALUE;
2191
2192     if( p_sys->i_output_pts ) /* make sure 1st frame has a pts > 0 */
2193     {
2194         p_sys->i_input_pts = 0;
2195         p_sys->i_input_dts = 0;
2196     }
2197
2198     return avcodec_default_get_buffer( p_context, p_frame );
2199 }
2200
2201 /*
2202  * SPU
2203  */
2204 static subpicture_t *spu_new_buffer( decoder_t * );
2205 static void spu_del_buffer( decoder_t *, subpicture_t * );
2206
2207 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2208 {
2209     sout_stream_sys_t *p_sys = p_stream->p_sys;
2210
2211     /* Open decoder */
2212     id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
2213
2214     /* Initialization of decoder format structures */
2215     id->p_decoder->fmt_in = id->f_src;
2216     id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2217     id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2218     //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
2219
2220     /* Attach object to parent so object variables inheritance works */
2221     vlc_object_attach( id->p_decoder, p_stream );
2222
2223     id->p_decoder->p_module =
2224         module_Need( id->p_decoder, "decoder", "$codec", VLC_TRUE );
2225
2226     if( !id->p_decoder->p_module )
2227     {
2228         vlc_object_detach( id->p_decoder );
2229         vlc_object_destroy( id->p_decoder );
2230         msg_Err( p_stream, "cannot find decoder" );
2231         return VLC_EGENERIC;
2232     }
2233
2234     if( !p_sys->b_soverlay )
2235     {
2236         /* Open encoder */
2237         id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
2238
2239         /* Initialization of encoder format structures */
2240         es_format_Init( &id->p_encoder->fmt_in,
2241                         id->f_src.i_cat, id->f_src.i_codec );
2242
2243         id->p_encoder->fmt_out = id->f_dst;
2244
2245         id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2246
2247         /* Attach object to parent so object variables inheritance works */
2248         vlc_object_attach( id->p_encoder, p_stream );
2249
2250         id->p_encoder->p_module =
2251             module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2252
2253         if( !id->p_encoder->p_module )
2254         {
2255             module_Unneed( id->p_decoder, id->p_decoder->p_module );
2256             vlc_object_detach( id->p_decoder );
2257             vlc_object_destroy( id->p_decoder );
2258
2259             vlc_object_detach( id->p_encoder );
2260             vlc_object_destroy( id->p_encoder );
2261             msg_Err( p_stream, "cannot find encoder" );
2262             return VLC_EGENERIC;
2263         }
2264
2265         id->f_dst = id->p_encoder->fmt_out;
2266     }
2267     else
2268     {
2269         p_sys->p_filter_blend =
2270             vlc_object_create( p_stream, sizeof(filter_t) );
2271         vlc_object_attach( p_sys->p_filter_blend, p_stream );
2272         p_sys->p_filter_blend->fmt_out.video.i_chroma =
2273             VLC_FOURCC('I','4','2','0');
2274         p_sys->p_filter_blend->fmt_in.video.i_chroma =
2275             VLC_FOURCC('Y','U','V','A');
2276         p_sys->p_filter_blend->p_module =
2277             module_Need( p_sys->p_filter_blend, "video blending", 0, 0 );
2278     }
2279
2280     return VLC_SUCCESS;
2281 }
2282
2283 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2284 {
2285     sout_stream_sys_t *p_sys = p_stream->p_sys;
2286     int i;
2287
2288     /* Close decoder */
2289     if( id->p_decoder->p_module )
2290         module_Unneed( id->p_decoder, id->p_decoder->p_module );
2291     vlc_object_detach( id->p_decoder );
2292     vlc_object_destroy( id->p_decoder );
2293
2294     /* Close encoder */
2295     if( id->p_encoder )
2296     {
2297         if( id->p_encoder->p_module )
2298             module_Unneed( id->p_encoder, id->p_encoder->p_module );
2299         vlc_object_detach( id->p_encoder );
2300         vlc_object_destroy( id->p_encoder );
2301     }
2302
2303     /* Free subpictures */
2304     for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2305     {
2306         if( !p_sys->pp_subpics[i] ) continue;
2307
2308         spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2309         p_sys->pp_subpics[i] = NULL;
2310     }
2311 }
2312
2313 static int transcode_spu_process( sout_stream_t *p_stream,
2314                                   sout_stream_id_t *id,
2315                                   block_t *in, block_t **out )
2316 {
2317     sout_stream_sys_t *p_sys = p_stream->p_sys;
2318     subpicture_t *p_subpic;
2319     *out = NULL;
2320
2321     p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2322     if( p_subpic && p_sys->b_soverlay )
2323     {
2324         int i;
2325
2326         /* Find a free slot in our supictures ring buffer */
2327         for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2328         {
2329             if( !p_sys->pp_subpics[i] )
2330             {
2331                 p_sys->pp_subpics[i] = p_subpic;
2332                 break;
2333             }
2334         }
2335         if( i == SUBPICTURE_RING_SIZE )
2336         {
2337             spu_del_buffer( id->p_decoder, p_subpic );
2338         }
2339     }
2340
2341     if(  p_subpic && !p_sys->b_soverlay )
2342     {
2343         block_t *p_block;
2344
2345         p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2346         spu_del_buffer( id->p_decoder, p_subpic );
2347
2348         if( p_block )
2349         {
2350             block_ChainAppend( out, p_block );
2351             return VLC_SUCCESS;
2352         }
2353     }
2354
2355     return VLC_EGENERIC;
2356 }
2357
2358 static subpicture_t *transcode_spu_get( sout_stream_t *p_stream,
2359                                         sout_stream_id_t *id,
2360                                         mtime_t display_date )
2361 {
2362     sout_stream_sys_t *p_sys = p_stream->p_sys;
2363     subpicture_t *p_subpic = 0;
2364     subpicture_t *p_ephemer = 0;
2365     subpicture_t **pp_subpic = &p_subpic;
2366     int i;
2367
2368     /* Find current subpictures and remove old ones */
2369     for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2370     {
2371         if( !p_sys->pp_subpics[i] ) continue;
2372
2373         if( !p_sys->pp_subpics[i]->b_ephemer &&
2374             p_sys->pp_subpics[i]->i_stop < display_date )
2375         {
2376             spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2377             p_sys->pp_subpics[i] = NULL;
2378             continue;
2379         }
2380
2381         if( p_sys->pp_subpics[i]->i_start > display_date ) continue;
2382
2383         if( p_sys->pp_subpics[i]->b_ephemer && !p_ephemer )
2384         {
2385             p_ephemer = p_sys->pp_subpics[i];
2386         }
2387         else if( p_sys->pp_subpics[i]->b_ephemer )
2388         {
2389             if( p_ephemer->i_start < p_sys->pp_subpics[i]->i_start )
2390             {
2391                 subpicture_t tmp;
2392                 tmp = *p_ephemer;
2393                 *p_ephemer = *p_sys->pp_subpics[i];
2394                 *p_sys->pp_subpics[i] = tmp;
2395             }
2396
2397             spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2398             p_sys->pp_subpics[i] = NULL;
2399             continue;
2400         }
2401
2402         /* Add subpicture to the list */
2403         *pp_subpic = p_sys->pp_subpics[i];
2404         pp_subpic = &p_sys->pp_subpics[i]->p_next;
2405     }
2406
2407     return p_subpic;
2408 }
2409
2410 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2411 {
2412     subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
2413     memset( p_subpic, 0, sizeof(subpicture_t) );
2414     p_subpic->b_absolute = VLC_TRUE;
2415
2416     p_subpic->pf_create_region = __spu_CreateRegion;
2417     p_subpic->pf_destroy_region = __spu_DestroyRegion;
2418
2419     return p_subpic;
2420 }
2421
2422 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2423 {
2424     while( p_subpic->p_region )
2425     {
2426         subpicture_region_t *p_region = p_subpic->p_region;
2427         p_subpic->p_region = p_region->p_next;
2428         p_subpic->pf_destroy_region( VLC_OBJECT(p_dec), p_region );
2429     }
2430
2431     free( p_subpic );
2432 }