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