]> git.sesse.net Git - vlc/blob - modules/stream_out/transcode.c
e532aa5b83ba438d5a065afd0168c3e1de4517f0
[vlc] / modules / stream_out / transcode.c
1 /*****************************************************************************
2  * transcode.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: transcode.c,v 1.63 2003/12/08 13:02:40 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 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         id->f_dst.i_cat    = AUDIO_ES;
377         id->f_dst.i_codec  = p_sys->i_acodec;
378         id->f_dst.audio.i_rate = p_sys->i_sample_rate  > 0 ? p_sys->i_sample_rate : id->f_src.audio.i_rate;
379         id->f_dst.audio.i_channels    = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.audio.i_channels;
380         id->f_dst.i_bitrate     = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
381         id->f_dst.audio.i_blockalign = 0;
382         id->f_dst.i_extra  = 0;
383         id->f_dst.p_extra  = NULL;
384
385         /* build decoder -> filter -> encoder */
386         if( transcode_audio_ffmpeg_new( p_stream, id ) )
387         {
388             msg_Err( p_stream, "cannot create audio chain" );
389             free( id );
390             return NULL;
391         }
392
393         /* open output stream */
394         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
395         id->b_transcode = VLC_TRUE;
396
397         if( id->id == NULL )
398         {
399             free( id );
400             return NULL;
401         }
402     }
403     else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
404     {
405         msg_Dbg( p_stream,
406                  "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
407                  (char*)&p_fmt->i_codec,
408                  (char*)&p_sys->i_vcodec );
409
410         memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
411
412         /* create dst format */
413         id->f_dst.i_cat         = VIDEO_ES;
414         id->f_dst.i_codec       = p_sys->i_vcodec;
415         id->f_dst.video.i_width = p_sys->i_width;
416         id->f_dst.video.i_height= p_sys->i_height;
417         id->f_dst.i_bitrate     = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
418         id->f_dst.i_extra       = 0;
419         id->f_dst.p_extra       = NULL;
420
421         /* build decoder -> filter -> encoder */
422         if( transcode_video_ffmpeg_new( p_stream, id ) )
423         {
424             msg_Err( p_stream, "cannot create video chain" );
425             free( id );
426             return NULL;
427         }
428 #if 0
429         /* open output stream */
430         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
431 #endif
432         id->b_transcode = VLC_TRUE;
433     }
434     else
435     {
436         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
437         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
438         id->b_transcode = VLC_FALSE;
439
440         if( id->id == NULL )
441         {
442             free( id );
443             return NULL;
444         }
445     }
446
447     return id;
448 }
449
450 static int     Del      ( sout_stream_t *p_stream, sout_stream_id_t *id )
451 {
452     sout_stream_sys_t   *p_sys = p_stream->p_sys;
453
454     if( id->b_transcode )
455     {
456         if( id->f_src.i_cat == AUDIO_ES )
457         {
458             transcode_audio_ffmpeg_close( p_stream, id );
459         }
460         else if( id->f_src.i_cat == VIDEO_ES )
461         {
462             transcode_video_ffmpeg_close( p_stream, id );
463         }
464     }
465
466     if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
467     free( id );
468
469     return VLC_SUCCESS;
470 }
471
472 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
473                  sout_buffer_t *p_buffer )
474 {
475     sout_stream_sys_t   *p_sys = p_stream->p_sys;
476
477     if( id->b_transcode )
478     {
479         sout_buffer_t *p_buffer_out;
480         if( id->f_src.i_cat == AUDIO_ES )
481         {
482             transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
483                                             &p_buffer_out );
484         }
485         else if( id->f_src.i_cat == VIDEO_ES )
486         {
487             if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
488                 &p_buffer_out ) != VLC_SUCCESS )
489             {
490                 sout_BufferDelete( p_stream->p_sout, p_buffer );
491                 return VLC_EGENERIC;
492             }
493         }
494         sout_BufferDelete( p_stream->p_sout, p_buffer );
495
496         if( p_buffer_out )
497         {
498             return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
499         }
500         return VLC_SUCCESS;
501     }
502     else if( id->id != NULL )
503     {
504         return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
505     }
506     else
507     {
508         sout_BufferDelete( p_stream->p_sout, p_buffer );
509         return VLC_EGENERIC;
510     }
511 }
512
513 /****************************************************************************
514  * ffmpeg decoder reencocdr part
515  ****************************************************************************/
516
517 static struct
518 {
519     vlc_fourcc_t i_fcc;
520     int          i_ff_codec;
521 } fourcc_to_ff_code[] =
522 {
523     /* audio */
524     { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
525     { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
526     { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
527     { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
528     { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
529     { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
530     { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
531     { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
532     { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
533
534     /* video */
535     { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
536     { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
537 #if LIBAVCODEC_BUILD >= 4676
538     { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
539 #endif
540     { VLC_FOURCC( 'm', 'p', '4', 'v'),  CODEC_ID_MPEG4 },
541     { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
542     { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
543     { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
544     { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
545     { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
546     { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
547     { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
548     { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
549     { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
550     { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
551     { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
552     { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
553 #if LIBAVCODEC_BUILD >= 4666
554     { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
555 #endif
556
557     /* raw video code, only used for 'encoding' */
558     { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
559     { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
560     { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
561     { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
562     { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
563     { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
564     { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
565     { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
566
567     { VLC_FOURCC(   0,   0,   0,   0 ), 0 }
568 };
569
570 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
571 {
572     int i;
573
574     for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
575     {
576         if( fourcc_to_ff_code[i].i_fcc == i_fcc )
577         {
578             return fourcc_to_ff_code[i].i_ff_codec;
579         }
580     }
581
582     return 0;
583 }
584
585 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
586 {
587     switch( i_chroma )
588     {
589         case VLC_FOURCC( 'I', '4', '2', '0' ):
590             return PIX_FMT_YUV420P;
591         case VLC_FOURCC( 'I', '4', '2', '2' ):
592             return PIX_FMT_YUV422P;
593         case VLC_FOURCC( 'I', '4', '4', '4' ):
594             return PIX_FMT_YUV444P;
595         case VLC_FOURCC( 'R', 'V', '1', '5' ):
596             return PIX_FMT_RGB555;
597         case VLC_FOURCC( 'R', 'V', '1', '6' ):
598             return PIX_FMT_RGB565;
599         case VLC_FOURCC( 'R', 'V', '2', '4' ):
600             return PIX_FMT_RGB24;
601         case VLC_FOURCC( 'R', 'V', '3', '2' ):
602             return PIX_FMT_RGBA32;
603         case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
604             return PIX_FMT_GRAY8;
605         case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
606             return PIX_FMT_YUV422;
607         default:
608             return 0;
609     }
610 }
611
612 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
613 {
614     switch( i_pix_fmt )
615     {
616     case PIX_FMT_YUV420P:
617         return VLC_FOURCC('I','4','2','0');
618     case PIX_FMT_YUV422P:
619         return VLC_FOURCC('I','4','2','2');
620     case PIX_FMT_YUV444P:
621         return VLC_FOURCC('I','4','4','4');
622
623     case PIX_FMT_YUV422:
624         return VLC_FOURCC('Y','U','Y','2');
625
626     case PIX_FMT_RGB555:
627         return VLC_FOURCC('R','V','1','5');
628     case PIX_FMT_RGB565:
629         return VLC_FOURCC('R','V','1','6');
630     case PIX_FMT_RGB24:
631         return VLC_FOURCC('R','V','2','4');
632     case PIX_FMT_RGBA32:
633         return VLC_FOURCC('R','V','3','2');
634     case PIX_FMT_GRAY8:
635         return VLC_FOURCC('G','R','E','Y');
636
637     case PIX_FMT_YUV410P:
638     case PIX_FMT_YUV411P:
639     case PIX_FMT_BGR24:
640     default:
641         return 0;
642     }
643 }
644
645 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
646                                        sout_stream_id_t *id )
647 {
648     int i_ff_codec;
649
650     if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
651         id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
652         id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
653         id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
654     {
655         id->ff_dec = NULL;
656
657         id->ff_dec_c = avcodec_alloc_context();
658         id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
659         id->ff_dec_c->channels    = id->f_src.audio.i_channels;
660         id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
661         id->ff_dec_c->bit_rate    = id->f_src.i_bitrate;
662     }
663     else
664     {
665         /* find decoder */
666         i_ff_codec = get_ff_codec( id->f_src.i_codec );
667         if( i_ff_codec == 0 )
668         {
669             msg_Err( p_stream, "cannot find decoder id" );
670             return VLC_EGENERIC;
671         }
672
673         id->ff_dec = avcodec_find_decoder( i_ff_codec );
674         if( !id->ff_dec )
675         {
676             msg_Err( p_stream, "cannot find decoder (avcodec)" );
677             return VLC_EGENERIC;
678         }
679
680         id->ff_dec_c = avcodec_alloc_context();
681         id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
682         id->ff_dec_c->channels    = id->f_src.audio.i_channels;
683         id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
684         id->ff_dec_c->bit_rate    = id->f_src.i_bitrate;
685
686         id->ff_dec_c->extradata_size = id->f_src.i_extra;
687         id->ff_dec_c->extradata      = id->f_src.p_extra;
688         if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
689         {
690             msg_Err( p_stream, "cannot open decoder" );
691             return VLC_EGENERIC;
692         }
693     }
694
695     id->i_buffer     = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
696     id->i_buffer_pos = 0;
697     id->p_buffer     = malloc( id->i_buffer );
698
699     /* Sanity check for audio channels */
700     id->f_dst.audio.i_channels = __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
701
702     /* find encoder */
703     id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
704
705     /* Initialization of encoder format structures */
706     es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
707     id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
708     id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
709     id->p_encoder->fmt_in.audio.i_physical_channels =
710         id->p_encoder->fmt_in.audio.i_original_channels =
711             pi_channels_maps[id->f_dst.audio.i_channels];
712     id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
713
714     id->p_encoder->fmt_out = id->p_encoder->fmt_in;
715     id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
716     id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
717
718     id->p_encoder->p_module =
719         module_Need( id->p_encoder, "encoder", NULL );
720     if( !id->p_encoder->p_module )
721     {
722         vlc_object_destroy( id->p_encoder );
723         msg_Err( p_stream, "cannot open encoder" );
724         return VLC_EGENERIC;
725     }
726
727     id->b_enc_inited = VLC_FALSE;
728
729     id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
730     id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
731
732     /* Hack for mp3 transcoding support */
733     if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
734     {
735         id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
736     }
737
738     return VLC_SUCCESS;
739 }
740
741 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
742                                           sout_stream_id_t *id )
743 {
744     if( id->ff_dec )
745     {
746         avcodec_close( id->ff_dec_c );
747         free( id->ff_dec_c );
748     }
749
750     module_Unneed( id->p_encoder, id->p_encoder->p_module );
751     vlc_object_destroy( id->p_encoder );
752
753     free( id->p_buffer );
754 }
755
756 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
757                                            sout_stream_id_t *id,
758                                            sout_buffer_t *in,
759                                            sout_buffer_t **out )
760 {
761     aout_buffer_t aout_buf;
762     block_t *p_block;
763     int i_buffer = in->i_size;
764     char *p_buffer = in->p_buffer;
765     id->i_dts = in->i_dts;
766     *out = NULL;
767
768     while( i_buffer )
769     {
770         id->i_buffer_pos = 0;
771
772         /* decode as much data as possible */
773         if( id->ff_dec )
774         {
775             int i_used;
776
777             i_used = avcodec_decode_audio( id->ff_dec_c,
778                          (int16_t*)id->p_buffer, &id->i_buffer_pos,
779                          p_buffer, i_buffer );
780
781 #if 0
782             msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
783                       i_used, i_buffer );
784 #endif
785             if( i_used < 0 )
786             {
787                 msg_Warn( p_stream, "error audio decoding");
788                 break;
789             }
790
791             i_buffer -= i_used;
792             p_buffer += i_used;
793         }
794         else
795         {
796             int16_t *sout = (int16_t*)id->p_buffer;
797
798             if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
799                 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
800             {
801                 int8_t *sin = (int8_t*)p_buffer;
802                 int i_used = __MIN( id->i_buffer/2, i_buffer );
803                 int i_samples = i_used;
804
805                 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
806                     while( i_samples > 0 )
807                     {
808                         *sout++ = ( *sin++ ) << 8;
809                         i_samples--;
810                     }
811                 else
812                     while( i_samples > 0 )
813                     {
814                         *sout++ = ( *sin++ - 128 ) << 8;
815                         i_samples--;
816                     }
817
818                 i_buffer -= i_used;
819                 p_buffer += i_used;
820                 id->i_buffer_pos = i_used * 2;
821             }
822             else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
823                      id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
824             {
825                 int16_t *sin = (int16_t*)p_buffer;
826                 int i_used = __MIN( id->i_buffer, i_buffer );
827                 int i_samples = i_used / 2;
828                 int b_invert_indianness;
829
830                 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
831 #ifdef WORDS_BIGENDIAN
832                     b_invert_indianness = 1;
833 #else
834                     b_invert_indianness = 0;
835 #endif
836                 else
837 #ifdef WORDS_BIGENDIAN
838                     b_invert_indianness = 0;
839 #else
840                     b_invert_indianness = 1;
841 #endif
842
843                 if( b_invert_indianness )
844                 {
845                     while( i_samples > 0 )
846                     {
847                         uint8_t tmp[2];
848
849                         tmp[1] = *sin++;
850                         tmp[0] = *sin++;
851                         *sout++ = *(int16_t*)tmp;
852                         i_samples--;
853                     }
854                 }
855                 else
856                 {
857                     memcpy( sout, sin, i_used );
858                     sout += i_samples;
859                 }
860
861                 i_buffer -= i_used;
862                 p_buffer += i_used;
863                 id->i_buffer_pos = i_used;
864             }
865         }
866
867         if( id->i_buffer_pos == 0 ) continue;
868
869         /* Encode as much data as possible */
870         if( !id->b_enc_inited && id->p_encoder->pf_header )
871         {
872             p_block = id->p_encoder->pf_header( id->p_encoder );
873             while( p_block )
874             {
875                 sout_buffer_t *p_out;
876                 block_t *p_prev_block = p_block;
877
878                 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
879                 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
880                 p_out->i_dts = p_out->i_pts = in->i_dts;
881                 p_out->i_length = 0;
882                 sout_BufferChain( out, p_out );
883
884                 p_block = p_block->p_next;
885                 block_Release( p_prev_block );
886             }
887
888             id->b_enc_inited = VLC_TRUE;
889         }
890
891         aout_buf.p_buffer = id->p_buffer;
892         aout_buf.i_nb_bytes = id->i_buffer_pos;
893         aout_buf.i_nb_samples = id->i_buffer_pos / 2 / id->f_src.audio.i_channels;
894         aout_buf.start_date = id->i_dts;
895         aout_buf.end_date = id->i_dts;
896
897         id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
898             id->f_src.audio.i_channels / id->f_src.audio.i_rate );
899
900         if( id->f_src.audio.i_channels !=
901             id->p_encoder->fmt_in.audio.i_channels )
902         {
903             unsigned int i;
904             int j;
905
906             /* This is for liba52 which is what ffmpeg uses to decode ac3 */
907             static const int translation[7][6] =
908             {{ 0, 0, 0, 0, 0, 0 },      /* 0 channels (rarely used) */
909              { 0, 0, 0, 0, 0, 0 },       /* 1 ch */
910              { 0, 1, 0, 0, 0, 0 },       /* 2 */
911              { 1, 2, 0, 0, 0, 0 },       /* 3 */
912              { 1, 3, 2, 0, 0, 0 },       /* 4 */
913              { 1, 3, 4, 2, 0, 0 },       /* 5 */
914              { 1, 3, 4, 5, 2, 0 }};      /* 6 */
915
916             /* dumb downmixing */
917             for( i = 0; i < aout_buf.i_nb_samples; i++ )
918             {
919                 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
920                 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
921                 {
922                     p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
923                         p_buffer[i*id->f_src.audio.i_channels+
924                                  translation[id->f_src.audio.i_channels][j]];
925                 }
926             }
927             aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
928         }
929
930         p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
931         while( p_block )
932         {
933             sout_buffer_t *p_out;
934             block_t *p_prev_block = p_block;
935
936             p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
937             memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
938             p_out->i_dts = p_block->i_dts;
939             p_out->i_pts = p_block->i_pts;
940             p_out->i_length = p_block->i_length;
941             sout_BufferChain( out, p_out );
942
943             p_block = p_block->p_next;
944             block_Release( p_prev_block );
945         }
946     }
947
948     return VLC_SUCCESS;
949 }
950
951
952 /*
953  * video
954  */
955 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
956                                        sout_stream_id_t *id )
957 {
958     sout_stream_sys_t   *p_sys = p_stream->p_sys;
959
960     int i_ff_codec;
961
962     /* Open decoder */
963     if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
964         id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
965         id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
966         id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
967         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
968         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
969         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
970         id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
971         id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
972     {
973         id->ff_dec              = NULL;
974         id->ff_dec_c            = avcodec_alloc_context();
975         id->ff_dec_c->width     = id->f_src.video.i_width;
976         id->ff_dec_c->height    = id->f_src.video.i_height;
977         id->ff_dec_c->pix_fmt   = get_ff_chroma( id->f_src.i_codec );
978     }
979     else
980     {
981         /* find decoder */
982         i_ff_codec = get_ff_codec( id->f_src.i_codec );
983         if( i_ff_codec == 0 )
984         {
985             msg_Err( p_stream, "cannot find decoder" );
986             return VLC_EGENERIC;
987         }
988
989         id->ff_dec = avcodec_find_decoder( i_ff_codec );
990         if( !id->ff_dec )
991         {
992             msg_Err( p_stream, "cannot find decoder" );
993             return VLC_EGENERIC;
994         }
995
996         id->ff_dec_c = avcodec_alloc_context();
997         id->ff_dec_c->width         = id->f_src.video.i_width;
998         id->ff_dec_c->height        = id->f_src.video.i_height;
999         /* id->ff_dec_c->bit_rate      = id->f_src.i_bitrate; */
1000         id->ff_dec_c->extradata_size= id->f_src.i_extra;
1001         id->ff_dec_c->extradata     = id->f_src.p_extra;
1002         id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1003         id->ff_dec_c->error_resilience= -1;
1004         id->ff_dec_c->get_buffer    = transcode_video_ffmpeg_getframebuf;
1005         id->ff_dec_c->opaque        = p_sys;
1006
1007         if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1008         {
1009             msg_Err( p_stream, "cannot open decoder" );
1010             return VLC_EGENERIC;
1011         }
1012
1013         if( i_ff_codec == CODEC_ID_MPEG4 && id->ff_dec_c->extradata_size > 0 )
1014         {
1015             int b_gotpicture;
1016             AVFrame frame;
1017             uint8_t *p_vol = malloc( id->ff_dec_c->extradata_size +
1018                                      FF_INPUT_BUFFER_PADDING_SIZE );
1019
1020             memcpy( p_vol, id->ff_dec_c->extradata,
1021                     id->ff_dec_c->extradata_size );
1022             memset( p_vol + id->ff_dec_c->extradata_size, 0,
1023                     FF_INPUT_BUFFER_PADDING_SIZE );
1024
1025             avcodec_decode_video( id->ff_dec_c, &frame, &b_gotpicture,
1026                                   id->ff_dec_c->extradata,
1027                                   id->ff_dec_c->extradata_size );
1028             free( p_vol );
1029         }
1030     }
1031
1032     /* Open encoder */
1033     id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1034
1035     /* Initialization of encoder format structures */
1036     es_format_Init( &id->p_encoder->fmt_in,
1037                     id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1038
1039     /* The dimensions will be set properly later on.
1040      * Just put sensible values so we can test if there is an encoder. */
1041     id->p_encoder->fmt_in.video.i_width = 16;
1042     id->p_encoder->fmt_in.video.i_height = 16;
1043
1044     id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1045     id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1046     if( id->ff_dec )
1047     {
1048         id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1049 #if LIBAVCODEC_BUILD >= 4662
1050         id->p_encoder->fmt_in.video.i_frame_rate_base =
1051             id->ff_dec_c->frame_rate_base;
1052 #endif
1053
1054 #if LIBAVCODEC_BUILD >= 4687
1055         id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1056             ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1057               id->ff_dec_c->width / id->ff_dec_c->height );
1058 #else
1059         id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1060             id->ff_dec_c->aspect_ratio;
1061 #endif
1062     }
1063
1064     /* Check whether a particular aspect ratio was requested */
1065     if( id->f_src.video.i_aspect )
1066     {
1067         id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1068         id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1069     }
1070
1071     id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1072     id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1073     id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1074
1075     id->p_encoder->i_vtolerance = p_sys->i_vtolerance;
1076     id->p_encoder->i_key_int = p_sys->i_key_int;
1077     id->p_encoder->i_b_frames = p_sys->i_b_frames;
1078     id->p_encoder->i_qmin = p_sys->i_qmin;
1079     id->p_encoder->i_qmax = p_sys->i_qmax;
1080     id->p_encoder->i_hq = p_sys->i_hq;
1081     id->p_encoder->b_strict_rc = p_sys->b_strict_rc;
1082     id->p_encoder->b_pre_me = p_sys->b_pre_me;
1083     id->p_encoder->b_hurry_up = p_sys->b_hurry_up;
1084
1085     id->p_ff_pic         = avcodec_alloc_frame();
1086     id->p_ff_pic_tmp0    = NULL;
1087     id->p_ff_pic_tmp1    = NULL;
1088     id->p_ff_pic_tmp2    = NULL;
1089     id->p_vresample      = NULL;
1090
1091     id->p_encoder->p_module =
1092         module_Need( id->p_encoder, "encoder", NULL );
1093
1094     if( !id->p_encoder->p_module )
1095     {
1096         vlc_object_destroy( id->p_encoder );
1097         msg_Err( p_stream, "cannot find encoder" );
1098         return VLC_EGENERIC;
1099     }
1100
1101     /* Close the encoder.
1102      * We'll open it only when we have the first frame */
1103     module_Unneed( id->p_encoder, id->p_encoder->p_module );
1104
1105     id->b_enc_inited = VLC_FALSE;
1106
1107     return VLC_SUCCESS;
1108 }
1109
1110 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1111                                            sout_stream_id_t *id )
1112 {
1113     /* Close decoder */
1114     if( id->ff_dec )
1115     {
1116         avcodec_close( id->ff_dec_c );
1117         free( id->ff_dec_c );
1118     }
1119
1120     /* Close encoder */
1121     module_Unneed( id->p_encoder, id->p_encoder->p_module );
1122     vlc_object_destroy( id->p_encoder );
1123
1124     /* Misc cleanup */
1125     if( id->p_ff_pic)
1126     {
1127         free( id->p_ff_pic );
1128     }
1129
1130     if( id->p_ff_pic_tmp0 )
1131     {
1132         free( id->p_ff_pic_tmp0->data[0] );
1133         free( id->p_ff_pic_tmp0 );
1134     }
1135     if( id->p_ff_pic_tmp1)
1136     {
1137         free( id->p_ff_pic_tmp1->data[0] );
1138         free( id->p_ff_pic_tmp1 );
1139     }
1140     if( id->p_ff_pic_tmp2)
1141     {
1142         free( id->p_ff_pic_tmp2->data[0] );
1143         free( id->p_ff_pic_tmp2 );
1144     }
1145     if( id->p_vresample )
1146     {
1147         free( id->p_vresample );
1148     }
1149 }
1150
1151 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1152                sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1153 {
1154     sout_stream_sys_t   *p_sys = p_stream->p_sys;
1155     int     i_used;
1156     int     b_gotpicture;
1157     AVFrame *frame;
1158
1159     int     i_data;
1160     uint8_t *p_data;
1161
1162     *out = NULL;
1163
1164     i_data = in->i_size;
1165     p_data = in->p_buffer;
1166  
1167     for( ;; )
1168     {
1169         block_t *p_block;
1170         picture_t pic;
1171         int i_plane;
1172
1173         /* decode frame */
1174         frame = id->p_ff_pic;
1175         p_sys->i_input_pts = in->i_pts;
1176         if( id->ff_dec )
1177         {
1178             i_used = avcodec_decode_video( id->ff_dec_c, frame,
1179                                            &b_gotpicture,
1180                                            p_data, i_data );
1181         }
1182         else
1183         {
1184             /* raw video */
1185             avpicture_fill( (AVPicture*)frame, p_data,
1186                             id->ff_dec_c->pix_fmt,
1187                             id->ff_dec_c->width, id->ff_dec_c->height );
1188             i_used = i_data;
1189             b_gotpicture = 1;
1190
1191             /* Set PTS */
1192             frame->pts = p_sys->i_input_pts;
1193         }
1194
1195         if( i_used < 0 )
1196         {
1197             msg_Warn( p_stream, "error");
1198             return VLC_EGENERIC;
1199         }
1200         i_data -= i_used;
1201         p_data += i_used;
1202
1203         if( !b_gotpicture )
1204         {
1205             return VLC_SUCCESS;
1206         }
1207
1208         /* Get the pts of the decoded frame if any, otherwise keep the
1209          * interpolated one */
1210         if( frame->pts > 0 )
1211         {
1212             p_sys->i_output_pts = frame->pts;
1213         }
1214
1215         if( !id->b_enc_inited )
1216         {
1217             /* Hack because of the copy packetizer which can fail to detect the
1218              * proper size (which forces us to wait until the 1st frame
1219              * is decoded) */
1220             int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1221                           p_sys->i_crop_right;
1222             int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1223                            p_sys->i_crop_bottom;
1224
1225             if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0 )
1226             {
1227                 /* Apply the scaling */
1228                 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1229                 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1230             }
1231             else if( id->f_dst.video.i_width > 0 &&
1232                      id->f_dst.video.i_height <= 0 )
1233             {
1234                 id->f_dst.video.i_height =
1235                     id->f_dst.video.i_width / (double)i_width * i_height;
1236             }
1237             else if( id->f_dst.video.i_width <= 0 &&
1238                      id->f_dst.video.i_height > 0 )
1239             {
1240                 id->f_dst.video.i_width =
1241                     id->f_dst.video.i_height / (double)i_height * i_width;
1242             }
1243
1244             id->p_encoder->fmt_in.video.i_width =
1245               id->p_encoder->fmt_out.video.i_width =
1246                 id->f_dst.video.i_width;
1247             id->p_encoder->fmt_in.video.i_height =
1248               id->p_encoder->fmt_out.video.i_height =
1249                 id->f_dst.video.i_height;
1250
1251             id->p_encoder->fmt_out.i_extra = 0;
1252             id->p_encoder->fmt_out.p_extra = NULL;
1253
1254             id->p_encoder->p_module =
1255                 module_Need( id->p_encoder, "encoder", NULL );
1256             if( !id->p_encoder->p_module )
1257             {
1258                 vlc_object_destroy( id->p_encoder );
1259                 msg_Err( p_stream, "cannot find encoder" );
1260                 id->b_transcode = VLC_FALSE;
1261                 return VLC_EGENERIC;
1262             }
1263
1264             id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1265             id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1266
1267             /* Hack for mp2v/mp1v transcoding support */
1268             if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1269                 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1270             {
1271                 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1272             }
1273
1274             if( !( id->id =
1275                      p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1276                                                      &id->f_dst ) ) )
1277             {
1278                 msg_Err( p_stream, "cannot add this stream" );
1279                 transcode_video_ffmpeg_close( p_stream, id );
1280                 id->b_transcode = VLC_FALSE;
1281                 return VLC_EGENERIC;
1282             }
1283
1284             if( id->p_encoder->pf_header )
1285             {
1286                 p_block = id->p_encoder->pf_header( id->p_encoder );
1287                 while( p_block )
1288                 {
1289                     sout_buffer_t *p_out;
1290                     block_t *p_prev_block = p_block;
1291
1292                     p_out = sout_BufferNew( p_stream->p_sout,
1293                                             p_block->i_buffer );
1294                     memcpy( p_out->p_buffer, p_block->p_buffer,
1295                             p_block->i_buffer);
1296                     p_out->i_dts = p_out->i_pts = in->i_dts;
1297                     p_out->i_length = 0;
1298                     sout_BufferChain( out, p_out );
1299
1300                     p_block = p_block->p_next;
1301                     block_Release( p_prev_block );
1302                 }
1303             }
1304
1305             id->i_inter_pixfmt =
1306                 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1307
1308             id->b_enc_inited = VLC_TRUE;
1309         }
1310
1311         /* deinterlace */
1312         if( p_stream->p_sys->b_deinterlace )
1313         {
1314             if( id->p_ff_pic_tmp0 == NULL )
1315             {
1316                 int     i_size;
1317                 uint8_t *buf;
1318                 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1319                 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1320                                              id->ff_dec_c->width, id->ff_dec_c->height );
1321
1322                 buf = malloc( i_size );
1323
1324                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1325                                 id->i_inter_pixfmt,
1326                                 id->ff_dec_c->width, id->ff_dec_c->height );
1327             }
1328
1329             avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1330                                    id->ff_dec_c->pix_fmt,
1331                                    id->ff_dec_c->width, id->ff_dec_c->height );
1332
1333             frame = id->p_ff_pic_tmp0;
1334         }
1335
1336         /* convert pix format */
1337         if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1338         {
1339             if( id->p_ff_pic_tmp1 == NULL )
1340             {
1341                 int     i_size;
1342                 uint8_t *buf;
1343                 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1344                 i_size = avpicture_get_size( id->i_inter_pixfmt,
1345                                              id->ff_dec_c->width,
1346                                              id->ff_dec_c->height );
1347
1348                 buf = malloc( i_size );
1349
1350                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1351                                 id->i_inter_pixfmt,
1352                                 id->ff_dec_c->width, id->ff_dec_c->height );
1353             }
1354
1355             img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1356                          (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1357                          id->ff_dec_c->width, id->ff_dec_c->height );
1358
1359             frame = id->p_ff_pic_tmp1;
1360         }
1361
1362         /* convert size and crop */
1363         if( id->ff_dec_c->width  != id->f_dst.video.i_width ||
1364             id->ff_dec_c->height != id->f_dst.video.i_height ||
1365             p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1366             p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1367         {
1368             if( id->p_ff_pic_tmp2 == NULL )
1369             {
1370                 int     i_size;
1371                 uint8_t *buf;
1372                 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1373                 i_size = avpicture_get_size( id->i_inter_pixfmt,
1374                                              id->f_dst.video.i_width,
1375                                              id->f_dst.video.i_height );
1376
1377                 buf = malloc( i_size );
1378
1379                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1380                                 id->i_inter_pixfmt,
1381                                 id->f_dst.video.i_width, id->f_dst.video.i_height );
1382
1383                 id->p_vresample =
1384                     img_resample_full_init( id->f_dst.video.i_width,
1385                                             id->f_dst.video.i_height,
1386                                             id->ff_dec_c->width, id->ff_dec_c->height,
1387                                             p_stream->p_sys->i_crop_top,
1388                                             p_stream->p_sys->i_crop_bottom,
1389                                             p_stream->p_sys->i_crop_left,
1390                                             p_stream->p_sys->i_crop_right );
1391             }
1392
1393             img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1394                           (AVPicture*)frame );
1395
1396             frame = id->p_ff_pic_tmp2;
1397         }
1398
1399         /* Encoding */
1400         vout_InitPicture( VLC_OBJECT(p_stream), &pic,
1401                           id->p_encoder->fmt_in.i_codec,
1402                           id->f_dst.video.i_width, id->f_dst.video.i_height,
1403                           id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1404                           id->f_dst.video.i_height );
1405
1406         for( i_plane = 0; i_plane < pic.i_planes; i_plane++ )
1407         {
1408             pic.p[i_plane].p_pixels = frame->data[i_plane];
1409             pic.p[i_plane].i_pitch = frame->linesize[i_plane];
1410         }
1411
1412         /* Set the pts of the frame being encoded */
1413         pic.date = p_sys->i_output_pts;
1414
1415         pic.b_progressive = 1; /* ffmpeg doesn't support interlaced encoding */
1416         pic.i_nb_fields = frame->repeat_pict;
1417 #if LIBAVCODEC_BUILD >= 4684
1418         pic.b_top_field_first = frame->top_field_first;
1419 #endif
1420
1421         /* Interpolate the next PTS
1422          * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1423         if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1424         {
1425             p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1426               id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1427         }
1428
1429         p_block = id->p_encoder->pf_encode_video( id->p_encoder, &pic );
1430         while( p_block )
1431         {
1432             sout_buffer_t *p_out;
1433             block_t *p_prev_block = p_block;
1434
1435             p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1436             memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1437             p_out->i_dts = p_block->i_dts;
1438             p_out->i_pts = p_block->i_pts;
1439             p_out->i_length = p_block->i_length;
1440             sout_BufferChain( out, p_out );
1441
1442             p_block = p_block->p_next;
1443             block_Release( p_prev_block );
1444         }
1445
1446         if( i_data <= 0 )
1447         {
1448             return VLC_SUCCESS;
1449         }
1450     }
1451
1452     return VLC_SUCCESS;
1453 }
1454
1455 /*****************************************************************************
1456  * transcode_video_ffmpeg_getframebuf:
1457  *
1458  * Callback used by ffmpeg to get a frame buffer.
1459  * We use it to get the right PTS for each decoded picture.
1460  *****************************************************************************/
1461 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1462                                               AVFrame *p_frame)
1463 {
1464     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1465
1466     /* Set PTS */
1467     p_frame->pts = p_sys->i_input_pts;
1468
1469     return avcodec_default_get_buffer( p_context, p_frame );
1470 }