]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode.c
* src/stream_output/stream_output.c: small sout_cfg_parser() changes.
[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@netcourrier.com>
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
37 /* ffmpeg header */
38 #ifdef HAVE_FFMPEG_AVCODEC_H
39 #   include <ffmpeg/avcodec.h>
40 #else
41 #   include <avcodec.h>
42 #endif
43
44 #if LIBAVCODEC_BUILD < 4704
45 #   define AV_NOPTS_VALUE 0
46 #endif
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 #define VCODEC_TEXT N_("Destination video codec")
52 #define VCODEC_LONGTEXT N_( \
53     "Allows you to pecify the destination video codec used for the streaming "\
54     "output." )
55 #define VB_TEXT N_("Video bitrate")
56 #define VB_LONGTEXT N_( \
57     "Allows you to specify the video bitrate used for the streaming " \
58     "output." )
59 #define SCALE_TEXT N_("Video scaling")
60 #define SCALE_LONGTEXT N_( \
61     "Allows you to scale the video before encoding." )
62 #define DEINTERLACE_TEXT N_("Deinterlace video")
63 #define DEINTERLACE_LONGTEXT N_( \
64     "Allows you to deinterlace the video before encoding." )
65 #define WIDTH_TEXT N_("Video width")
66 #define WIDTH_LONGTEXT N_( \
67     "Allows you to specify the output video width." )
68 #define HEIGHT_TEXT N_("Video height")
69 #define HEIGHT_LONGTEXT N_( \
70     "Allows you to specify the output video height." )
71
72 #define CROPTOP_TEXT N_("Video crop top")
73 #define CROPTOP_LONGTEXT N_( \
74     "Allows you to specify the top coordinate for the video cropping." )
75 #define CROPLEFT_TEXT N_("Video crop left")
76 #define CROPLEFT_LONGTEXT N_( \
77     "Allows you to specify the left coordinate for the video cropping." )
78 #define CROPBOTTOM_TEXT N_("Video crop bottom")
79 #define CROPBOTTOM_LONGTEXT N_( \
80     "Allows you to specify the bottom coordinate for the video cropping." )
81 #define CROPRIGHT_TEXT N_("Video crop right")
82 #define CROPRIGHT_LONGTEXT N_( \
83     "Allows you to specify the right coordinate for the video cropping." )
84
85 #define ACODEC_TEXT N_("Destination audio codec")
86 #define ACODEC_LONGTEXT N_( \
87     "Allows you to specify the destination audio codec used for the " \
88     "streaming output." )
89 #define AB_TEXT N_("Audio bitrate")
90 #define AB_LONGTEXT N_( \
91     "Allows you to specify the audio bitrate used for the streaming " \
92     "output." )
93 #define ARATE_TEXT N_("Audio sample rate")
94 #define ARATE_LONGTEXT N_( \
95     "Allows you to specify the audio sample rate used for the streaming " \
96     "output." )
97 #define ACHANS_TEXT N_("Audio channels")
98 #define ACHANS_LONGTEXT N_( \
99     "Allows you to specify the number of audio channels used for the " \
100     "streaming output." )
101
102 #define THREADS_TEXT N_("Number of threads")
103 #define THREADS_LONGTEXT N_( \
104     "Allows you to specify the number of threads used for the transcoding." )
105
106 static int  Open ( vlc_object_t * );
107 static void Close( vlc_object_t * );
108
109 #define SOUT_CFG_PREFIX "sout-transcode-"
110
111 vlc_module_begin();
112     set_description( _("Transcode stream output") );
113     set_capability( "sout stream", 50 );
114     add_shortcut( "transcode" );
115     set_callbacks( Open, Close );
116
117     add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
118                 VCODEC_LONGTEXT, VLC_FALSE );
119     add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
120                  VB_LONGTEXT, VLC_FALSE );
121     add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
122                SCALE_LONGTEXT, VLC_FALSE );
123     add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
124               DEINTERLACE_LONGTEXT, VLC_FALSE );
125     add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
126                  WIDTH_LONGTEXT, VLC_TRUE );
127     add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
128                  HEIGHT_LONGTEXT, VLC_TRUE );
129
130     add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
131                  CROPTOP_LONGTEXT, VLC_TRUE );
132     add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
133                  CROPLEFT_LONGTEXT, VLC_TRUE );
134     add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
135                  CROPBOTTOM_LONGTEXT, VLC_TRUE );
136     add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
137                  CROPRIGHT_LONGTEXT, VLC_TRUE );
138
139     add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
140                 ACODEC_LONGTEXT, VLC_FALSE );
141     add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
142                  AB_LONGTEXT, VLC_FALSE );
143     add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
144                  ACHANS_LONGTEXT, VLC_FALSE );
145     add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
146                  ARATE_LONGTEXT, VLC_TRUE );
147
148     add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
149                  THREADS_LONGTEXT, VLC_TRUE );
150 vlc_module_end();
151
152 /*****************************************************************************
153  * Exported prototypes
154  *****************************************************************************/
155 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
156 static int               Del ( sout_stream_t *, sout_stream_id_t * );
157 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
158
159 static int  transcode_audio_ffmpeg_new    ( sout_stream_t *, sout_stream_id_t * );
160 static void transcode_audio_ffmpeg_close  ( sout_stream_t *, sout_stream_id_t * );
161 static int  transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
162
163 static int  transcode_video_ffmpeg_new    ( sout_stream_t *, sout_stream_id_t * );
164 static void transcode_video_ffmpeg_close  ( sout_stream_t *, sout_stream_id_t * );
165 static int  transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
166
167 static int  transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
168
169 static int  EncoderThread( struct sout_stream_sys_t * p_sys );
170
171 static int pi_channels_maps[6] =
172 {
173     0,
174     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
175     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
176     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
177      | AOUT_CHAN_REARRIGHT,
178     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
179      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
180 };
181
182 static const char *ppsz_sout_options[] = {
183     "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
184     "scale", "width", "height", "deinterlace", "threads",
185     "acodec", "ab", "samplerate", "channels", NULL
186 };
187
188 #define PICTURE_RING_SIZE 64
189
190 struct sout_stream_sys_t
191 {
192     VLC_COMMON_MEMBERS
193
194     sout_stream_t * p_out;
195     sout_stream_id_t * id_video;
196     block_t * p_buffers;
197     vlc_mutex_t     lock_out;
198     vlc_cond_t      cond;
199     picture_t *     pp_pics[PICTURE_RING_SIZE];
200     int             i_first_pic, i_last_pic;
201
202     vlc_fourcc_t    i_acodec;   /* codec audio (0 if not transcode) */
203     sout_cfg_t      *p_audio_cfg;
204     int             i_sample_rate;
205     int             i_channels;
206     int             i_abitrate;
207
208     vlc_fourcc_t    i_vcodec;   /*    "   video  " "   "      " */
209     sout_cfg_t      *p_video_cfg;
210     int             i_vbitrate;
211     double          f_scale;
212     int             i_width;
213     int             i_height;
214     vlc_bool_t      b_deinterlace;
215     int             i_threads;
216
217     int             i_crop_top;
218     int             i_crop_bottom;
219     int             i_crop_right;
220     int             i_crop_left;
221
222     mtime_t         i_input_dts;
223     mtime_t         i_input_pts;
224     vlc_bool_t      b_input_has_b_frames;
225
226     mtime_t         i_output_pts;
227 };
228
229 /*****************************************************************************
230  * Open:
231  *****************************************************************************/
232 static int Open( vlc_object_t *p_this )
233 {
234     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
235     sout_stream_sys_t *p_sys;
236     vlc_value_t       val;
237     char              *psz_opts;
238
239     p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
240
241     p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
242     if( !p_sys->p_out )
243     {
244         msg_Err( p_stream, "cannot create chain" );
245         free( p_sys );
246         return VLC_EGENERIC;
247     }
248
249     p_sys->b_input_has_b_frames = VLC_FALSE;
250     p_sys->i_output_pts = 0;
251
252     psz_opts = sout_cfg_find_value( p_stream->p_cfg, "aopts" );
253     p_sys->p_audio_cfg = NULL;
254     if( psz_opts && *psz_opts )
255     {
256         char *psz_name, *psz_next, *psz_tmp;
257         asprintf( &psz_tmp, "aopts%s", psz_opts );
258         psz_next = sout_cfg_parser( &psz_name, &p_sys->p_audio_cfg, psz_tmp );
259         if( psz_next ) free( psz_next );
260         free( psz_tmp );
261     }
262
263     psz_opts = sout_cfg_find_value( p_stream->p_cfg, "vopts" );
264     p_sys->p_video_cfg = NULL;
265     if( psz_opts && *psz_opts )
266     {
267         char *psz_name, *psz_next, *psz_tmp;
268         asprintf( &psz_tmp, "vopts%s", psz_opts );
269         psz_next = sout_cfg_parser( &psz_name, &p_sys->p_video_cfg, psz_tmp );
270         if( psz_next ) free( psz_next );
271         free( psz_tmp );
272     }
273
274     sout_ParseCfg( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
275                    p_stream->p_cfg );
276
277     /* Audio transcoding parameters */
278     var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
279     p_sys->i_acodec = 0;
280     if( val.psz_string && *val.psz_string )
281     {
282         char fcc[4] = "    ";
283         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
284         p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
285     }
286     if( val.psz_string ) free( val.psz_string );
287
288     var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
289     p_sys->i_abitrate = val.i_int;
290     if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
291
292     var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
293     p_sys->i_sample_rate = val.i_int;
294
295     var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
296     p_sys->i_channels = val.i_int;
297
298     if( p_sys->i_acodec )
299     {
300         msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
301                  (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
302                  p_sys->i_channels, p_sys->i_abitrate / 1000 );
303     }
304
305     /* Video transcoding parameters */
306     var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
307     p_sys->i_vcodec = 0;
308     if( val.psz_string && *val.psz_string )
309     {
310         char fcc[4] = "    ";
311         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
312         p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
313     }
314     if( val.psz_string ) free( val.psz_string );
315
316     var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
317     p_sys->i_vbitrate = val.i_int;
318     if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
319
320     var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
321     p_sys->f_scale = val.f_float;
322
323     var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
324     p_sys->i_width = val.i_int;
325
326     var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
327     p_sys->i_height = val.i_int;
328
329     var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
330     p_sys->b_deinterlace = val.b_bool;
331
332     var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
333     p_sys->i_crop_top = val.i_int;
334     var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
335     p_sys->i_crop_bottom = val.i_int;
336     var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
337     p_sys->i_crop_left = val.i_int;
338     var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
339     p_sys->i_crop_right = val.i_int;
340
341     var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
342     p_sys->i_threads = val.i_int;
343
344     if( p_sys->i_vcodec )
345     {
346         msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
347                  (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
348                  p_sys->f_scale, p_sys->i_vbitrate / 1000 );
349     }
350
351     p_stream->pf_add    = Add;
352     p_stream->pf_del    = Del;
353     p_stream->pf_send   = Send;
354     p_stream->p_sys     = p_sys;
355
356     avcodec_init();
357     avcodec_register_all();
358
359     return VLC_SUCCESS;
360 }
361
362 /*****************************************************************************
363  * Close:
364  *****************************************************************************/
365 static void Close( vlc_object_t * p_this )
366 {
367     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
368     sout_stream_sys_t   *p_sys = p_stream->p_sys;
369
370     sout_stream_delete( p_sys->p_out );
371
372     while( p_sys->p_audio_cfg != NULL )
373     {
374         sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
375
376         if( p_sys->p_audio_cfg->psz_name )
377             free( p_sys->p_audio_cfg->psz_name );
378         if( p_sys->p_audio_cfg->psz_value )
379             free( p_sys->p_audio_cfg->psz_value );
380         free( p_sys->p_audio_cfg );
381
382         p_sys->p_audio_cfg = p_next;
383     }
384
385     while( p_sys->p_video_cfg != NULL )
386     {
387         sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
388
389         if( p_sys->p_video_cfg->psz_name )
390             free( p_sys->p_video_cfg->psz_name );
391         if( p_sys->p_video_cfg->psz_value )
392             free( p_sys->p_video_cfg->psz_value );
393         free( p_sys->p_video_cfg );
394
395         p_sys->p_video_cfg = p_next;
396     }
397
398     vlc_object_destroy( p_sys );
399 }
400
401 struct sout_stream_id_t
402 {
403     vlc_fourcc_t  b_transcode;
404     es_format_t f_src;        /* only if transcoding */
405     es_format_t f_dst;        /*  "   "      " */
406     unsigned int  i_inter_pixfmt; /* intermediary format when transcoding */
407
408     /* id of the out stream */
409     void *id;
410
411     /* Encoder */
412     encoder_t       *p_encoder;
413     vlc_fourcc_t    b_enc_inited;
414
415     /* ffmpeg part */
416     AVCodec         *ff_dec;
417     AVCodecContext  *ff_dec_c;
418
419     mtime_t         i_dts;
420     mtime_t         i_length;
421
422     int             i_buffer;
423     int             i_buffer_pos;
424     uint8_t         *p_buffer;
425
426     AVFrame         *p_ff_pic;
427     AVFrame         *p_ff_pic_tmp0; /* to do deinterlace */
428     AVFrame         *p_ff_pic_tmp1; /* to do pix conversion */
429     AVFrame         *p_ff_pic_tmp2; /* to do resample */
430
431     ImgReSampleContext *p_vresample;
432 };
433
434
435 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
436 {
437     sout_stream_sys_t   *p_sys = p_stream->p_sys;
438     sout_stream_id_t    *id;
439
440     id = malloc( sizeof( sout_stream_id_t ) );
441     id->i_dts = 0;
442     id->id = NULL;
443     id->p_encoder = NULL;
444
445     if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
446     {
447         msg_Dbg( p_stream,
448                  "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
449                  (char*)&p_fmt->i_codec,
450                  (char*)&p_sys->i_acodec );
451
452         /* src format */
453         memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
454
455         /* create dst format */
456         es_format_Init( &id->f_dst, AUDIO_ES, p_sys->i_acodec );
457         id->f_dst.i_id    = id->f_src.i_id;
458         id->f_dst.i_group = id->f_src.i_group;
459         if( id->f_src.psz_language )
460             id->f_dst.psz_language = strdup( id->f_src.psz_language );
461         id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ?
462             p_sys->i_sample_rate : id->f_src.audio.i_rate;
463         id->f_dst.audio.i_channels = p_sys->i_channels > 0 ?
464             p_sys->i_channels : id->f_src.audio.i_channels;
465         id->f_dst.i_bitrate = p_sys->i_abitrate;
466         id->f_dst.audio.i_blockalign = 0;
467         id->f_dst.i_extra  = 0;
468         id->f_dst.p_extra  = NULL;
469
470         /* build decoder -> filter -> encoder */
471         if( transcode_audio_ffmpeg_new( p_stream, id ) )
472         {
473             msg_Err( p_stream, "cannot create audio chain" );
474             free( id );
475             return NULL;
476         }
477
478         /* open output stream */
479         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
480         id->b_transcode = VLC_TRUE;
481
482         if( id->id == NULL )
483         {
484             free( id );
485             return NULL;
486         }
487     }
488     else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
489     {
490         msg_Dbg( p_stream,
491                  "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
492                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
493
494         memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
495
496         /* create dst format */
497         es_format_Init( &id->f_dst, VIDEO_ES, p_sys->i_vcodec );
498         id->f_dst.i_id    = id->f_src.i_id;
499         id->f_dst.i_group = id->f_src.i_group;
500         if( id->f_src.psz_language )
501             id->f_dst.psz_language = strdup( id->f_src.psz_language );
502         id->f_dst.video.i_width  = p_sys->i_width;
503         id->f_dst.video.i_height = p_sys->i_height;
504         id->f_dst.i_bitrate = p_sys->i_vbitrate;
505         id->f_dst.i_extra = 0;
506         id->f_dst.p_extra = NULL;
507
508         /* build decoder -> filter -> encoder */
509         if( transcode_video_ffmpeg_new( p_stream, id ) )
510         {
511             msg_Err( p_stream, "cannot create video chain" );
512             free( id );
513             return NULL;
514         }
515 #if 0
516         /* open output stream */
517         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
518 #endif
519         id->b_transcode = VLC_TRUE;
520     }
521     else
522     {
523         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
524         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
525         id->b_transcode = VLC_FALSE;
526
527         if( id->id == NULL )
528         {
529             free( id );
530             return NULL;
531         }
532     }
533
534     return id;
535 }
536
537 static int     Del      ( sout_stream_t *p_stream, sout_stream_id_t *id )
538 {
539     sout_stream_sys_t   *p_sys = p_stream->p_sys;
540
541     if( id->b_transcode )
542     {
543         if( id->f_src.i_cat == AUDIO_ES )
544         {
545             transcode_audio_ffmpeg_close( p_stream, id );
546         }
547         else if( id->f_src.i_cat == VIDEO_ES )
548         {
549             transcode_video_ffmpeg_close( p_stream, id );
550         }
551     }
552
553     if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
554     free( id );
555
556     return VLC_SUCCESS;
557 }
558
559 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
560                  block_t *p_buffer )
561 {
562     sout_stream_sys_t   *p_sys = p_stream->p_sys;
563
564     if( id->b_transcode )
565     {
566         block_t *p_buffer_out;
567
568         /* Be sure to have padding */
569         p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
570         if( p_buffer == NULL )
571         {
572             return VLC_EGENERIC;
573         }
574         p_buffer->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
575         memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
576
577         if( id->f_src.i_cat == AUDIO_ES )
578         {
579             transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
580                                             &p_buffer_out );
581         }
582         else if( id->f_src.i_cat == VIDEO_ES )
583         {
584             if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
585                 &p_buffer_out ) != VLC_SUCCESS )
586             {
587                 block_Release( p_buffer );
588                 return VLC_EGENERIC;
589             }
590         }
591         block_Release( p_buffer );
592
593         if( p_buffer_out )
594         {
595             return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
596         }
597         return VLC_SUCCESS;
598     }
599     else if( id->id != NULL )
600     {
601         return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
602     }
603     else
604     {
605         block_Release( p_buffer );
606         return VLC_EGENERIC;
607     }
608 }
609
610 /****************************************************************************
611  * ffmpeg decoder reencocdr part
612  ****************************************************************************/
613
614 static struct
615 {
616     vlc_fourcc_t i_fcc;
617     int          i_ff_codec;
618 } fourcc_to_ff_code[] =
619 {
620     /* audio */
621     { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
622     { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
623     { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
624     { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
625     { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
626     { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
627     { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
628     { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
629     { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
630
631     /* video */
632     { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
633     { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
634 #if LIBAVCODEC_BUILD >= 4676
635     { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
636 #endif
637     { VLC_FOURCC( 'm', 'p', '4', 'v'),  CODEC_ID_MPEG4 },
638     { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
639     { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
640     { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
641     { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
642     { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
643     { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
644     { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
645     { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
646     { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
647     { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
648     { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
649     { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
650 #if LIBAVCODEC_BUILD >= 4666
651     { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
652     { VLC_FOURCC( 'h', '2', '6', '4' ), CODEC_ID_H264 },
653 #endif
654
655     /* raw video code, only used for 'encoding' */
656     { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
657     { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
658     { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
659     { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
660     { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
661     { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
662     { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
663     { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
664     { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
665     { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
666
667     { VLC_FOURCC(   0,   0,   0,   0 ), 0 }
668 };
669
670 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
671 {
672     int i;
673
674     for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
675     {
676         if( fourcc_to_ff_code[i].i_fcc == i_fcc )
677         {
678             return fourcc_to_ff_code[i].i_ff_codec;
679         }
680     }
681
682     return 0;
683 }
684
685 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
686 {
687     switch( i_chroma )
688     {
689         case VLC_FOURCC( 'Y', 'V', '1', '2' ):
690         case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
691         case VLC_FOURCC( 'I', '4', '2', '0' ):
692             return PIX_FMT_YUV420P;
693         case VLC_FOURCC( 'I', '4', '2', '2' ):
694             return PIX_FMT_YUV422P;
695         case VLC_FOURCC( 'I', '4', '4', '4' ):
696             return PIX_FMT_YUV444P;
697         case VLC_FOURCC( 'R', 'V', '1', '5' ):
698             return PIX_FMT_RGB555;
699         case VLC_FOURCC( 'R', 'V', '1', '6' ):
700             return PIX_FMT_RGB565;
701         case VLC_FOURCC( 'R', 'V', '2', '4' ):
702             return PIX_FMT_BGR24;
703         case VLC_FOURCC( 'R', 'V', '3', '2' ):
704             return PIX_FMT_RGBA32;
705         case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
706             return PIX_FMT_GRAY8;
707         case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
708             return PIX_FMT_YUV422;
709         default:
710             return 0;
711     }
712 }
713
714 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
715 {
716     switch( i_pix_fmt )
717     {
718     case PIX_FMT_YUV420P:
719         return VLC_FOURCC('I','4','2','0');
720     case PIX_FMT_YUV422P:
721         return VLC_FOURCC('I','4','2','2');
722     case PIX_FMT_YUV444P:
723         return VLC_FOURCC('I','4','4','4');
724
725     case PIX_FMT_YUV422:
726         return VLC_FOURCC('Y','U','Y','2');
727
728     case PIX_FMT_RGB555:
729         return VLC_FOURCC('R','V','1','5');
730     case PIX_FMT_RGB565:
731         return VLC_FOURCC('R','V','1','6');
732     case PIX_FMT_RGB24:
733         return VLC_FOURCC('R','V','2','4');
734     case PIX_FMT_RGBA32:
735         return VLC_FOURCC('R','V','3','2');
736     case PIX_FMT_GRAY8:
737         return VLC_FOURCC('G','R','E','Y');
738
739     case PIX_FMT_YUV410P:
740     case PIX_FMT_YUV411P:
741     case PIX_FMT_BGR24:
742     default:
743         return 0;
744     }
745 }
746
747 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
748                                        sout_stream_id_t *id )
749 {
750     int i_ff_codec;
751
752     if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
753         id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
754         id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
755         id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
756     {
757         id->ff_dec = NULL;
758
759         id->ff_dec_c = avcodec_alloc_context();
760         id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
761         id->ff_dec_c->channels    = id->f_src.audio.i_channels;
762         id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
763         id->ff_dec_c->bit_rate    = id->f_src.i_bitrate;
764     }
765     else
766     {
767         /* find decoder */
768         i_ff_codec = get_ff_codec( id->f_src.i_codec );
769         if( i_ff_codec == 0 )
770         {
771             msg_Err( p_stream, "cannot find decoder id" );
772             return VLC_EGENERIC;
773         }
774
775         id->ff_dec = avcodec_find_decoder( i_ff_codec );
776         if( !id->ff_dec )
777         {
778             msg_Err( p_stream, "cannot find decoder (avcodec)" );
779             return VLC_EGENERIC;
780         }
781
782         id->ff_dec_c = avcodec_alloc_context();
783         id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
784         id->ff_dec_c->channels    = id->f_src.audio.i_channels;
785         id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
786         id->ff_dec_c->bit_rate    = id->f_src.i_bitrate;
787
788         id->ff_dec_c->extradata_size = id->f_src.i_extra;
789         id->ff_dec_c->extradata      = id->f_src.p_extra;
790         if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
791         {
792             msg_Err( p_stream, "cannot open decoder" );
793             return VLC_EGENERIC;
794         }
795     }
796
797     id->i_buffer     = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
798     id->i_buffer_pos = 0;
799     id->p_buffer     = malloc( id->i_buffer );
800
801     /* Sanity check for audio channels */
802     id->f_dst.audio.i_channels = __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
803
804     /* find encoder */
805     id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
806
807     /* Initialization of encoder format structures */
808     es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
809     id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
810     id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
811     id->p_encoder->fmt_in.audio.i_physical_channels =
812         id->p_encoder->fmt_in.audio.i_original_channels =
813             pi_channels_maps[id->f_dst.audio.i_channels];
814     id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
815
816     id->p_encoder->fmt_out = id->p_encoder->fmt_in;
817     id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
818     id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
819
820     id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
821
822     /* Attach object to parent so object variables inheritance works */
823     vlc_object_attach( id->p_encoder, p_stream );
824
825     id->p_encoder->p_module =
826         module_Need( id->p_encoder, "encoder", NULL, 0 );
827     if( !id->p_encoder->p_module )
828     {
829         vlc_object_destroy( id->p_encoder );
830         msg_Err( p_stream, "cannot open encoder" );
831         return VLC_EGENERIC;
832     }
833
834     id->b_enc_inited = VLC_FALSE;
835
836     id->f_dst.audio.i_channels = id->p_encoder->fmt_in.audio.i_channels;
837     id->f_dst.audio.i_rate     = id->p_encoder->fmt_in.audio.i_rate;
838     id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
839     id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
840
841     /* Hack for mp3 transcoding support */
842     if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
843     {
844         id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
845     }
846
847     return VLC_SUCCESS;
848 }
849
850 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
851                                           sout_stream_id_t *id )
852 {
853     if( id->ff_dec ) avcodec_close( id->ff_dec_c );
854     av_free( id->ff_dec_c );
855
856     module_Unneed( id->p_encoder, id->p_encoder->p_module );
857
858     vlc_object_detach( id->p_encoder );
859     vlc_object_destroy( id->p_encoder );
860
861     free( id->p_buffer );
862 }
863
864 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
865                                            sout_stream_id_t *id,
866                                            block_t *in,
867                                            block_t **out )
868 {
869     aout_buffer_t aout_buf;
870     block_t *p_block;
871     int i_buffer = in->i_buffer;
872     char *p_buffer = in->p_buffer;
873     id->i_dts = in->i_dts;
874     *out = NULL;
875
876     while( i_buffer )
877     {
878         id->i_buffer_pos = 0;
879
880         /* decode as much data as possible */
881         if( id->ff_dec )
882         {
883             int i_used;
884
885             i_used = avcodec_decode_audio( id->ff_dec_c,
886                          (int16_t*)id->p_buffer, &id->i_buffer_pos,
887                          p_buffer, i_buffer );
888
889 #if 0
890             msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
891                       i_used, i_buffer );
892 #endif
893             if( i_used < 0 )
894             {
895                 msg_Warn( p_stream, "error audio decoding");
896                 break;
897             }
898
899             i_buffer -= i_used;
900             p_buffer += i_used;
901
902             if ( id->i_buffer_pos < 0 )
903             {
904                 msg_Warn( p_stream, "weird error audio decoding");
905                 break;
906             }
907         }
908         else
909         {
910             int16_t *sout = (int16_t*)id->p_buffer;
911
912             if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
913                 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
914             {
915                 int8_t *sin = (int8_t*)p_buffer;
916                 int i_used = __MIN( id->i_buffer/2, i_buffer );
917                 int i_samples = i_used;
918
919                 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
920                     while( i_samples > 0 )
921                     {
922                         *sout++ = ( *sin++ ) << 8;
923                         i_samples--;
924                     }
925                 else
926                     while( i_samples > 0 )
927                     {
928                         *sout++ = ( *sin++ - 128 ) << 8;
929                         i_samples--;
930                     }
931
932                 i_buffer -= i_used;
933                 p_buffer += i_used;
934                 id->i_buffer_pos = i_used * 2;
935             }
936             else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
937                      id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
938             {
939                 int16_t *sin = (int16_t*)p_buffer;
940                 int i_used = __MIN( id->i_buffer, i_buffer );
941                 int i_samples = i_used / 2;
942
943                 /* first copy */
944                 memcpy( sout, sin, i_used );
945
946 #ifdef WORDS_BIGENDIAN
947                 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
948 #else
949                 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
950 #endif
951                 {
952                     uint8_t *dat = (uint8_t*)sout;
953
954                     while( i_samples > 0 )
955                     {
956                         uint8_t tmp;
957                         tmp    = dat[0];
958                         dat[0] = dat[1];
959                         dat[1] = tmp;
960
961                         dat += 2;
962
963                         i_samples--;
964                     }
965                 }
966
967                 i_buffer -= i_used;
968                 p_buffer += i_used;
969                 id->i_buffer_pos = i_used;
970             }
971         }
972
973         if( id->i_buffer_pos == 0 ) continue;
974
975         /* Encode as much data as possible */
976         if( !id->b_enc_inited && id->p_encoder->pf_header )
977         {
978             p_block = id->p_encoder->pf_header( id->p_encoder );
979             while( p_block )
980             {
981                 block_t *p_out;
982                 block_t *p_prev_block = p_block;
983
984                 p_out = block_New( p_stream, p_block->i_buffer );
985                 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
986                 p_out->i_dts = p_out->i_pts = in->i_dts;
987                 p_out->i_length = 0;
988                 block_ChainAppend( out, p_out );
989
990                 p_block = p_block->p_next;
991                 block_Release( p_prev_block );
992             }
993
994             id->b_enc_inited = VLC_TRUE;
995         }
996
997         aout_buf.p_buffer = id->p_buffer;
998         aout_buf.i_nb_bytes = id->i_buffer_pos;
999         aout_buf.i_nb_samples = id->i_buffer_pos / 2 / id->f_src.audio.i_channels;
1000         aout_buf.start_date = id->i_dts;
1001         aout_buf.end_date = id->i_dts;
1002
1003         id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
1004             id->f_src.audio.i_channels / id->f_src.audio.i_rate );
1005
1006         if( id->p_encoder->fmt_in.audio.i_channels == 1 &&
1007             id->f_src.audio.i_channels > 1 )
1008         {
1009             int16_t *p_sample = (int16_t *)aout_buf.p_buffer;
1010             int i_src_c = id->f_src.audio.i_channels;
1011             unsigned int i;
1012
1013             for( i = 0; i < aout_buf.i_nb_samples; i++ )
1014             {
1015                 int j, c = 0;
1016
1017                 for( j = 1; j < i_src_c; j++ )
1018                 {
1019                     c += p_sample[i_src_c * i + j];
1020                 }
1021                 p_sample[i] = c / (i_src_c-1);
1022             }
1023             aout_buf.i_nb_bytes = i * 2;
1024         }
1025         else if( id->p_encoder->fmt_in.audio.i_channels == 2 &&
1026                  id->f_src.audio.i_channels > 2 )
1027         {
1028             int i_src_c = id->f_src.audio.i_channels;
1029             unsigned int i;
1030
1031             static const float mixf_l[4][6] = /* [i_src_c - 3][channel index] */
1032             {
1033                 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1034                 { 0.00, 0.50, 0.50, 0.00, 0.00, 0.00 }, /* 4 channels */
1035                 { 0.00, 0.50, 0.00, 0.50, 0.00, 0.00 }, /* 5 channels */
1036                 { 0.00, 0.34, 0.33, 0.00, 0.33, 0.00 }, /* 6 channels */
1037             };
1038             static const float mixf_r[4][6] = /* [i_src_c - 3][channel index] */
1039             {
1040                 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1041                 { 0.00, 0.00, 0.50, 0.50, 0.00, 0.00 }, /* 4 channels */
1042                 { 0.00, 0.00, 0.50, 0.00, 0.50, 0.00 }, /* 5 channels */
1043                 { 0.00, 0.00, 0.33, 0.34, 0.00, 0.33 }, /* 6 channels */
1044             };
1045
1046
1047             for( i = 0; i < aout_buf.i_nb_samples; i++ )
1048             {
1049                 int16_t *p_src = (int16_t *)aout_buf.p_buffer + i_src_c * i;
1050                 int16_t *p_dst = (int16_t *)aout_buf.p_buffer + 2 * i;
1051
1052                 int j;
1053                 float l = 0.0, r = 0.0;
1054                 for( j = 0; j < i_src_c; j++ )
1055                 {
1056                     l += mixf_l[i_src_c-3][j] * p_src[j];
1057                     r += mixf_r[i_src_c-3][j] * p_src[j];
1058                 }
1059
1060                 p_dst[0] = (int)( l + 0.5 );
1061                 p_dst[1] = (int)( r + 0.5 );
1062             }
1063             aout_buf.i_nb_bytes = i * 2 * 2;
1064         }
1065         else if( id->f_src.audio.i_channels !=
1066                  id->p_encoder->fmt_in.audio.i_channels )
1067         {
1068             unsigned int i;
1069             int j;
1070
1071             /* This is for liba52 which is what ffmpeg uses to decode ac3 */
1072             static const int translation[7][6] =
1073             {{ 0, 0, 0, 0, 0, 0 },      /* 0 channels (rarely used) */
1074              { 0, 0, 0, 0, 0, 0 },       /* 1 ch */
1075              { 0, 1, 0, 0, 0, 0 },       /* 2 */
1076              { 1, 2, 0, 0, 0, 0 },       /* 3 */
1077              { 1, 3, 2, 0, 0, 0 },       /* 4 */
1078              { 1, 3, 4, 2, 0, 0 },       /* 5 */
1079              { 1, 3, 4, 5, 2, 0 }};      /* 6 */
1080
1081             /* dumb downmixing */
1082             for( i = 0; i < aout_buf.i_nb_samples; i++ )
1083             {
1084                 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
1085                 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
1086                 {
1087                     p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
1088                         p_buffer[i*id->f_src.audio.i_channels+
1089                                  translation[id->f_src.audio.i_channels][j]];
1090                 }
1091             }
1092             aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1093         }
1094
1095         p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1096         while( p_block )
1097         {
1098             block_t *p_out;
1099             block_t *p_prev_block = p_block;
1100
1101             p_out = block_New( p_stream, p_block->i_buffer );
1102             memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1103             p_out->i_dts = p_block->i_dts;
1104             p_out->i_pts = p_block->i_pts;
1105             p_out->i_length = p_block->i_length;
1106             block_ChainAppend( out, p_out );
1107
1108             p_block = p_block->p_next;
1109             block_Release( p_prev_block );
1110         }
1111     }
1112
1113     return VLC_SUCCESS;
1114 }
1115
1116
1117 /*
1118  * video
1119  */
1120 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1121                                        sout_stream_id_t *id )
1122 {
1123     sout_stream_sys_t *p_sys = p_stream->p_sys;
1124     int i_ff_codec;
1125
1126     /* Open decoder */
1127     if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1128         id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1129         id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1130         id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1131         id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1132         id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1133         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1134         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1135         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1136         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1137         id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1138     {
1139         id->ff_dec              = NULL;
1140         id->ff_dec_c            = avcodec_alloc_context();
1141         id->ff_dec_c->width     = id->f_src.video.i_width;
1142         id->ff_dec_c->height    = id->f_src.video.i_height;
1143         id->ff_dec_c->pix_fmt   = get_ff_chroma( id->f_src.i_codec );
1144
1145 #if LIBAVCODEC_BUILD >= 4687
1146         if( id->ff_dec_c->width )
1147         id->ff_dec_c->sample_aspect_ratio =
1148             av_d2q( id->f_src.video.i_aspect / (double)VOUT_ASPECT_FACTOR *
1149                     id->ff_dec_c->height / id->ff_dec_c->width, 255 );
1150 #else
1151         id->ff_dec_c->aspect_ratio =
1152             id->f_src.video.i_aspect / (float)VOUT_ASPECT_FACTOR;
1153 #endif
1154     }
1155     else
1156     {
1157         /* find decoder */
1158         i_ff_codec = get_ff_codec( id->f_src.i_codec );
1159         if( i_ff_codec == 0 )
1160         {
1161             msg_Err( p_stream, "cannot find decoder" );
1162             return VLC_EGENERIC;
1163         }
1164
1165         id->ff_dec = avcodec_find_decoder( i_ff_codec );
1166         if( !id->ff_dec )
1167         {
1168             msg_Err( p_stream, "cannot find decoder" );
1169             return VLC_EGENERIC;
1170         }
1171
1172         id->ff_dec_c = avcodec_alloc_context();
1173         id->ff_dec_c->width         = id->f_src.video.i_width;
1174         id->ff_dec_c->height        = id->f_src.video.i_height;
1175         id->ff_dec_c->bits_per_sample=id->f_src.video.i_bits_per_pixel;
1176         /* id->ff_dec_c->bit_rate      = id->f_src.i_bitrate; */
1177
1178         if( id->f_src.i_extra > 0 )
1179         {
1180             if( i_ff_codec == CODEC_ID_SVQ3 )
1181             {
1182                 int i_size = id->f_src.i_extra;
1183                 uint8_t *p;
1184
1185                 id->ff_dec_c->extradata_size = i_size + 12;
1186                 p = id->ff_dec_c->extradata  = malloc( i_size + 12 );
1187
1188                 memcpy( &p[0],  "SVQ3", 4 );
1189                 memset( &p[4], 0, 8 );
1190                 memcpy( &p[12], id->f_src.p_extra, i_size );
1191
1192                 /* Now remove all atoms before the SMI one */
1193                 if( id->ff_dec_c->extradata_size > 0x5a && strncmp( &p[0x56], "SMI ", 4 ) )
1194                 {
1195                     uint8_t *psz = &p[0x52];
1196
1197                     while( psz < &p[id->ff_dec_c->extradata_size - 8] )
1198                     {
1199                         int i_size = GetDWBE( psz );
1200                         if( i_size <= 1 )
1201                         {
1202                             /* FIXME handle 1 as long size */
1203                             break;
1204                         }
1205                         if( !strncmp( &psz[4], "SMI ", 4 ) )
1206                         {
1207                             memmove( &p[0x52], psz, &p[id->ff_dec_c->extradata_size] - psz );
1208                             break;
1209                         }
1210                         psz += i_size;
1211                     }
1212                 }
1213             }
1214             else
1215             {
1216                 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1217                 id->ff_dec_c->extradata = malloc( id->f_src.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
1218
1219                 memcpy( id->ff_dec_c->extradata, id->f_src.p_extra, id->f_src.i_extra );
1220                 memset( (uint8_t*)id->ff_dec_c->extradata + id->f_src.i_extra, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1221             }
1222         }
1223         id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1224         id->ff_dec_c->error_resilience= -1;
1225         id->ff_dec_c->get_buffer    = transcode_video_ffmpeg_getframebuf;
1226         id->ff_dec_c->opaque        = p_sys;
1227
1228         if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1229         {
1230             msg_Err( p_stream, "cannot open decoder" );
1231             return VLC_EGENERIC;
1232         }
1233     }
1234
1235     /* Open encoder */
1236     id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1237
1238     /* Initialization of encoder format structures */
1239     es_format_Init( &id->p_encoder->fmt_in,
1240                     id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1241
1242     /* The dimensions will be set properly later on.
1243      * Just put sensible values so we can test if there is an encoder. */
1244     id->p_encoder->fmt_in.video.i_width =
1245         id->f_src.video.i_width ?  id->f_src.video.i_width : 16;
1246     id->p_encoder->fmt_in.video.i_height =
1247         id->f_src.video.i_height ? id->f_src.video.i_height : 16;
1248
1249     id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1250     id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1251     if( id->ff_dec )
1252     {
1253         id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1254 #if LIBAVCODEC_BUILD >= 4662
1255         id->p_encoder->fmt_in.video.i_frame_rate_base =
1256             id->ff_dec_c->frame_rate_base;
1257 #endif
1258
1259 #if LIBAVCODEC_BUILD >= 4687
1260         if( id->ff_dec_c->height )
1261         id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1262             ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1263               id->ff_dec_c->width / id->ff_dec_c->height );
1264 #else
1265         id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1266             id->ff_dec_c->aspect_ratio;
1267 #endif
1268     }
1269
1270     /* Check whether a particular aspect ratio was requested */
1271     if( id->f_src.video.i_aspect )
1272     {
1273         id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1274         id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1275     }
1276
1277     id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1278     id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1279     id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1280
1281     id->p_encoder->i_threads = p_sys->i_threads;
1282
1283     id->p_ff_pic         = avcodec_alloc_frame();
1284     id->p_ff_pic_tmp0    = NULL;
1285     id->p_ff_pic_tmp1    = NULL;
1286     id->p_ff_pic_tmp2    = NULL;
1287     id->p_vresample      = NULL;
1288
1289     id->p_encoder->p_cfg = p_sys->p_video_cfg;
1290
1291     /* Attach object to parent so object variables inheritance works */
1292     vlc_object_attach( id->p_encoder, p_stream );
1293
1294     id->p_encoder->p_module =
1295         module_Need( id->p_encoder, "encoder", NULL, 0 );
1296
1297     if( !id->p_encoder->p_module )
1298     {
1299         vlc_object_destroy( id->p_encoder );
1300         msg_Err( p_stream, "cannot find encoder" );
1301         return VLC_EGENERIC;
1302     }
1303
1304     /* Close the encoder.
1305      * We'll open it only when we have the first frame */
1306     module_Unneed( id->p_encoder, id->p_encoder->p_module );
1307     id->p_encoder->p_module = NULL;
1308
1309     id->b_enc_inited = VLC_FALSE;
1310
1311     if ( p_sys->i_threads >= 1 )
1312     {
1313         p_sys->id_video = id;
1314         vlc_mutex_init( p_stream, &p_sys->lock_out );
1315         vlc_cond_init( p_stream, &p_sys->cond );
1316         memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1317         p_sys->i_first_pic = 0;
1318         p_sys->i_last_pic = 0;
1319         p_sys->p_buffers = NULL;
1320         p_sys->b_die = p_sys->b_error = 0;
1321         if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1322                                VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1323         {
1324             vlc_object_destroy( id->p_encoder );
1325             msg_Err( p_stream, "cannot spawn encoder thread" );
1326             return VLC_EGENERIC;
1327         }
1328     }
1329
1330     return VLC_SUCCESS;
1331 }
1332
1333 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1334                                            sout_stream_id_t *id )
1335 {
1336     if ( p_stream->p_sys->i_threads >= 1 )
1337     {
1338        vlc_mutex_lock( &p_stream->p_sys->lock_out );
1339        p_stream->p_sys->b_die = 1;
1340        vlc_cond_signal( &p_stream->p_sys->cond );
1341        vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1342        vlc_thread_join( p_stream->p_sys );
1343        vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1344        vlc_cond_destroy( &p_stream->p_sys->cond );
1345     }
1346
1347     /* Close decoder */
1348     if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1349     av_free( id->ff_dec_c );
1350
1351     /* Close encoder */
1352     if( id->p_encoder->p_module )
1353         module_Unneed( id->p_encoder, id->p_encoder->p_module );
1354     vlc_object_detach( id->p_encoder );
1355     vlc_object_destroy( id->p_encoder );
1356
1357     /* Misc cleanup */
1358     if( id->p_ff_pic)
1359     {
1360         free( id->p_ff_pic );
1361     }
1362
1363     if( id->p_ff_pic_tmp0 )
1364     {
1365         free( id->p_ff_pic_tmp0->data[0] );
1366         free( id->p_ff_pic_tmp0 );
1367     }
1368     if( id->p_ff_pic_tmp1)
1369     {
1370         free( id->p_ff_pic_tmp1->data[0] );
1371         free( id->p_ff_pic_tmp1 );
1372     }
1373     if( id->p_ff_pic_tmp2)
1374     {
1375         free( id->p_ff_pic_tmp2->data[0] );
1376         free( id->p_ff_pic_tmp2 );
1377     }
1378     if( id->p_vresample )
1379     {
1380         img_resample_close( id->p_vresample );
1381     }
1382 }
1383
1384 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1385                sout_stream_id_t *id, block_t *in, block_t **out )
1386 {
1387     sout_stream_sys_t   *p_sys = p_stream->p_sys;
1388     int     i_used;
1389     int     b_gotpicture;
1390     AVFrame *frame;
1391
1392     int     i_data;
1393     uint8_t *p_data;
1394
1395     *out = NULL;
1396
1397     i_data = in->i_buffer;
1398     p_data = in->p_buffer;
1399
1400     for( ;; )
1401     {
1402         block_t *p_block;
1403         picture_t * p_pic;
1404         int i_plane;
1405
1406         /* decode frame */
1407         frame = id->p_ff_pic;
1408         p_sys->i_input_pts = in->i_pts;
1409         p_sys->i_input_dts = in->i_dts;
1410         if( id->ff_dec )
1411         {
1412             i_used = avcodec_decode_video( id->ff_dec_c, frame,
1413                                            &b_gotpicture,
1414                                            p_data, i_data );
1415         }
1416         else
1417         {
1418             /* raw video */
1419             avpicture_fill( (AVPicture*)frame, p_data,
1420                             id->ff_dec_c->pix_fmt,
1421                             id->ff_dec_c->width, id->ff_dec_c->height );
1422             i_used = i_data;
1423             b_gotpicture = 1;
1424
1425             /* Set PTS */
1426             frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts :
1427                          AV_NOPTS_VALUE;
1428
1429             frame->pict_type = FF_I_TYPE;
1430         }
1431
1432         if( i_used < 0 )
1433         {
1434             msg_Warn( p_stream, "error");
1435             return VLC_EGENERIC;
1436         }
1437         i_data -= i_used;
1438         p_data += i_used;
1439
1440         if( !b_gotpicture )
1441         {
1442             return VLC_SUCCESS;
1443         }
1444
1445         /* Get the pts of the decoded frame if any, otherwise keep the
1446          * interpolated one */
1447         if( frame->pts != AV_NOPTS_VALUE )
1448         {
1449             p_sys->i_output_pts = frame->pts;
1450         }
1451
1452         /* Sanity check (seems to be needed for some streams ) */
1453         if( frame->pict_type == FF_B_TYPE )
1454         {
1455             p_sys->b_input_has_b_frames = VLC_TRUE;
1456         }
1457
1458         if( !id->b_enc_inited )
1459         {
1460             /* Hack because of the copy packetizer which can fail to detect the
1461              * proper size (which forces us to wait until the 1st frame
1462              * is decoded) */
1463             int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1464                           p_sys->i_crop_right;
1465             int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1466                            p_sys->i_crop_bottom;
1467
1468             if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1469                 && p_sys->f_scale )
1470             {
1471                 /* Apply the scaling */
1472                 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1473                 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1474             }
1475             else if( id->f_dst.video.i_width > 0 &&
1476                      id->f_dst.video.i_height <= 0 )
1477             {
1478                 id->f_dst.video.i_height =
1479                     id->f_dst.video.i_width / (double)i_width * i_height;
1480             }
1481             else if( id->f_dst.video.i_width <= 0 &&
1482                      id->f_dst.video.i_height > 0 )
1483             {
1484                 id->f_dst.video.i_width =
1485                     id->f_dst.video.i_height / (double)i_height * i_width;
1486             }
1487
1488             id->p_encoder->fmt_in.video.i_width =
1489               id->p_encoder->fmt_out.video.i_width =
1490                 id->f_dst.video.i_width;
1491             id->p_encoder->fmt_in.video.i_height =
1492               id->p_encoder->fmt_out.video.i_height =
1493                 id->f_dst.video.i_height;
1494
1495             id->p_encoder->fmt_out.i_extra = 0;
1496             id->p_encoder->fmt_out.p_extra = NULL;
1497
1498             id->p_encoder->p_module =
1499                 module_Need( id->p_encoder, "encoder", NULL, 0 );
1500             if( !id->p_encoder->p_module )
1501             {
1502                 vlc_object_destroy( id->p_encoder );
1503                 msg_Err( p_stream, "cannot find encoder" );
1504                 id->b_transcode = VLC_FALSE;
1505                 return VLC_EGENERIC;
1506             }
1507
1508             id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1509             id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1510
1511             /* Hack for mp2v/mp1v transcoding support */
1512             if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1513                 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1514             {
1515                 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1516             }
1517
1518             if( !( id->id =
1519                      p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1520                                                      &id->f_dst ) ) )
1521             {
1522                 msg_Err( p_stream, "cannot add this stream" );
1523                 transcode_video_ffmpeg_close( p_stream, id );
1524                 id->b_transcode = VLC_FALSE;
1525                 return VLC_EGENERIC;
1526             }
1527
1528             if( id->p_encoder->pf_header )
1529             {
1530                 p_block = id->p_encoder->pf_header( id->p_encoder );
1531                 while( p_block )
1532                 {
1533                     block_t *p_out;
1534                     block_t *p_prev_block = p_block;
1535
1536                     p_out = block_New( p_stream,
1537                                             p_block->i_buffer );
1538                     memcpy( p_out->p_buffer, p_block->p_buffer,
1539                             p_block->i_buffer);
1540                     p_out->i_dts = p_out->i_pts = in->i_dts;
1541                     p_out->i_length = 0;
1542                     block_ChainAppend( out, p_out );
1543
1544                     p_block = p_block->p_next;
1545                     block_Release( p_prev_block );
1546                 }
1547             }
1548
1549             id->i_inter_pixfmt =
1550                 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1551
1552             id->b_enc_inited = VLC_TRUE;
1553         }
1554
1555         /* deinterlace */
1556         if( p_stream->p_sys->b_deinterlace )
1557         {
1558             if( id->p_ff_pic_tmp0 == NULL )
1559             {
1560                 int     i_size;
1561                 uint8_t *buf;
1562                 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1563                 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1564                                              id->ff_dec_c->width, id->ff_dec_c->height );
1565
1566                 buf = malloc( i_size );
1567
1568                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1569                                 id->i_inter_pixfmt,
1570                                 id->ff_dec_c->width, id->ff_dec_c->height );
1571             }
1572
1573             avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1574                                    id->ff_dec_c->pix_fmt,
1575                                    id->ff_dec_c->width, id->ff_dec_c->height );
1576
1577 #if LIBAVCODEC_BUILD >= 4685
1578             id->p_ff_pic_tmp0->interlaced_frame = 0;
1579 #endif
1580             id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1581             frame = id->p_ff_pic_tmp0;
1582         }
1583
1584         /* convert pix format */
1585         if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1586         {
1587             if( id->p_ff_pic_tmp1 == NULL )
1588             {
1589                 int     i_size;
1590                 uint8_t *buf;
1591                 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1592                 i_size = avpicture_get_size( id->i_inter_pixfmt,
1593                                              id->ff_dec_c->width,
1594                                              id->ff_dec_c->height );
1595
1596                 buf = malloc( i_size );
1597
1598                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1599                                 id->i_inter_pixfmt,
1600                                 id->ff_dec_c->width, id->ff_dec_c->height );
1601             }
1602
1603             img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1604                          (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1605                          id->ff_dec_c->width, id->ff_dec_c->height );
1606
1607             id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1608 #if LIBAVCODEC_BUILD >= 4685
1609             id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1610             id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1611 #endif
1612             frame = id->p_ff_pic_tmp1;
1613         }
1614
1615         /* convert size and crop */
1616         if( id->ff_dec_c->width  != id->f_dst.video.i_width ||
1617             id->ff_dec_c->height != id->f_dst.video.i_height ||
1618             p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1619             p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1620         {
1621             if( id->p_ff_pic_tmp2 == NULL )
1622             {
1623                 int     i_size;
1624                 uint8_t *buf;
1625                 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1626                 i_size = avpicture_get_size( id->i_inter_pixfmt,
1627                                              id->f_dst.video.i_width,
1628                                              id->f_dst.video.i_height );
1629
1630                 buf = malloc( i_size );
1631
1632                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1633                                 id->i_inter_pixfmt,
1634                                 id->f_dst.video.i_width,
1635                                 id->f_dst.video.i_height );
1636
1637                 id->p_vresample =
1638                     img_resample_full_init( id->f_dst.video.i_width,
1639                                             id->f_dst.video.i_height,
1640                                             id->ff_dec_c->width,
1641                                             id->ff_dec_c->height,
1642                                             p_stream->p_sys->i_crop_top,
1643                                             p_stream->p_sys->i_crop_bottom,
1644                                             p_stream->p_sys->i_crop_left,
1645                                             p_stream->p_sys->i_crop_right
1646 #if LIBAVCODEC_BUILD >= 4708
1647                                             ,0, 0, 0, 0 );
1648 #else
1649                                           );
1650 #endif
1651             }
1652
1653             img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1654                           (AVPicture*)frame );
1655
1656             id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1657 #if LIBAVCODEC_BUILD >= 4685
1658             id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1659             id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1660 #endif
1661             frame = id->p_ff_pic_tmp2;
1662         }
1663
1664         /* Encoding */
1665         p_pic = malloc(sizeof(picture_t));
1666         vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1667                           id->p_encoder->fmt_in.i_codec,
1668                           id->f_dst.video.i_width, id->f_dst.video.i_height,
1669                           id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1670                           id->f_dst.video.i_height );
1671
1672         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1673         {
1674             p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1675             if ( p_sys->i_threads >= 1 )
1676             {
1677                 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines *
1678                                                     p_pic->p[i_plane].i_pitch);
1679                 p_stream->p_vlc->pf_memcpy( p_pic->p[i_plane].p_pixels,
1680                     frame->data[i_plane], p_pic->p[i_plane].i_lines *
1681                      p_pic->p[i_plane].i_pitch );
1682             }
1683             else
1684             {
1685                 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1686             }
1687         }
1688
1689         /* Set the pts of the frame being encoded */
1690         p_pic->date = p_sys->i_output_pts;
1691
1692         p_pic->i_nb_fields = frame->repeat_pict;
1693 #if LIBAVCODEC_BUILD >= 4685
1694         p_pic->b_progressive = !frame->interlaced_frame;
1695         p_pic->b_top_field_first = frame->top_field_first;
1696 #endif
1697
1698         /* Interpolate the next PTS
1699          * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1700         if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1701         {
1702             p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1703               id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1704         }
1705
1706         if ( p_sys->i_threads >= 1 )
1707         {
1708             vlc_mutex_lock( &p_sys->lock_out );
1709             p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1710             p_sys->i_last_pic %= PICTURE_RING_SIZE;
1711             *out = p_sys->p_buffers;
1712             p_sys->p_buffers = NULL;
1713             vlc_cond_signal( &p_sys->cond );
1714             vlc_mutex_unlock( &p_sys->lock_out );
1715         }
1716         else
1717         {
1718             block_t *p_block;
1719             p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1720             if( p_block )
1721             {
1722                 block_ChainAppend( out, p_block );
1723             }
1724             free( p_pic );
1725         }
1726
1727         if( i_data <= 0 )
1728         {
1729             return VLC_SUCCESS;
1730         }
1731     }
1732
1733     return VLC_SUCCESS;
1734 }
1735
1736 static int EncoderThread( sout_stream_sys_t * p_sys )
1737 {
1738     sout_stream_id_t * id = p_sys->id_video;
1739     picture_t * p_pic;
1740     int i_plane;
1741
1742     while ( !p_sys->b_die && !p_sys->b_error )
1743     {
1744         block_t *p_block;
1745
1746         vlc_mutex_lock( &p_sys->lock_out );
1747         while ( p_sys->i_last_pic == p_sys->i_first_pic )
1748         {
1749             vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1750             if ( p_sys->b_die || p_sys->b_error )
1751                 break;
1752         }
1753         if ( p_sys->b_die || p_sys->b_error )
1754         {
1755             vlc_mutex_unlock( &p_sys->lock_out );
1756             break;
1757         }
1758
1759         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1760         p_sys->i_first_pic %= PICTURE_RING_SIZE;
1761         vlc_mutex_unlock( &p_sys->lock_out );
1762
1763         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1764         vlc_mutex_lock( &p_sys->lock_out );
1765         if( p_block )
1766         {
1767             block_ChainAppend( &p_sys->p_buffers, p_block );
1768         }
1769         vlc_mutex_unlock( &p_sys->lock_out );
1770
1771         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1772         {
1773             free( p_pic->p[i_plane].p_pixels );
1774         }
1775         free( p_pic );
1776     }
1777
1778     while ( p_sys->i_last_pic != p_sys->i_first_pic )
1779     {
1780         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1781         p_sys->i_first_pic %= PICTURE_RING_SIZE;
1782
1783         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1784         {
1785             free( p_pic->p[i_plane].p_pixels );
1786         }
1787         free( p_pic );
1788     }
1789
1790     block_ChainRelease( p_sys->p_buffers );
1791
1792     return 0;
1793 }
1794
1795 /*****************************************************************************
1796  * transcode_video_ffmpeg_getframebuf:
1797  *
1798  * Callback used by ffmpeg to get a frame buffer.
1799  * We use it to get the right PTS for each decoded picture.
1800  *****************************************************************************/
1801 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1802                                               AVFrame *p_frame)
1803 {
1804     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1805
1806     /* Set PTS */
1807     if( p_sys->i_input_pts )
1808     {
1809         p_frame->pts = p_sys->i_input_pts;
1810     }
1811     else if( p_sys->i_input_dts )
1812     {
1813         /* Some demuxers/packetizers only set the dts so let's try to find a
1814          * useful timestamp from this */
1815         if( !p_context->has_b_frames || !p_sys->b_input_has_b_frames ||
1816             !p_frame->reference || !p_sys->i_output_pts )
1817         {
1818             p_frame->pts = p_sys->i_input_dts +
1819             /* Hack: ffmpeg encoding doesn't like frames with identical pts */
1820                 (p_sys->i_output_pts ? 0 : 50000);
1821         }
1822         else p_frame->pts = AV_NOPTS_VALUE;
1823     }
1824     else p_frame->pts = AV_NOPTS_VALUE;
1825
1826     if( p_sys->i_output_pts ) /* make sure 1st frame has a pts > 0 */
1827     {
1828         p_sys->i_input_pts = 0;
1829         p_sys->i_input_dts = 0;
1830     }
1831
1832     return avcodec_default_get_buffer( p_context, p_frame );
1833 }