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