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