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