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