1 /*****************************************************************************
2 * transcode.c: transcoding stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
35 #include <vlc/decoder.h>
38 #ifdef HAVE_FFMPEG_AVCODEC_H
39 # include <ffmpeg/avcodec.h>
44 #if LIBAVCODEC_BUILD < 4704
45 # define AV_NOPTS_VALUE 0
48 /*****************************************************************************
50 *****************************************************************************/
51 #define VCODEC_TEXT N_("Destination video codec")
52 #define VCODEC_LONGTEXT N_( \
53 "Allows you to pecify the destination video codec used for the streaming "\
55 #define VB_TEXT N_("Video bitrate")
56 #define VB_LONGTEXT N_( \
57 "Allows you to specify the video bitrate used for the streaming " \
59 #define SCALE_TEXT N_("Video scaling")
60 #define SCALE_LONGTEXT N_( \
61 "Allows you to scale the video before encoding." )
62 #define DEINTERLACE_TEXT N_("Deinterlace video")
63 #define DEINTERLACE_LONGTEXT N_( \
64 "Allows you to deinterlace the video before encoding." )
65 #define WIDTH_TEXT N_("Video width")
66 #define WIDTH_LONGTEXT N_( \
67 "Allows you to specify the output video width." )
68 #define HEIGHT_TEXT N_("Video height")
69 #define HEIGHT_LONGTEXT N_( \
70 "Allows you to specify the output video height." )
72 #define CROPTOP_TEXT N_("Video crop top")
73 #define CROPTOP_LONGTEXT N_( \
74 "Allows you to specify the top coordinate for the video cropping." )
75 #define CROPLEFT_TEXT N_("Video crop left")
76 #define CROPLEFT_LONGTEXT N_( \
77 "Allows you to specify the left coordinate for the video cropping." )
78 #define CROPBOTTOM_TEXT N_("Video crop bottom")
79 #define CROPBOTTOM_LONGTEXT N_( \
80 "Allows you to specify the bottom coordinate for the video cropping." )
81 #define CROPRIGHT_TEXT N_("Video crop right")
82 #define CROPRIGHT_LONGTEXT N_( \
83 "Allows you to specify the right coordinate for the video cropping." )
85 #define ACODEC_TEXT N_("Destination audio codec")
86 #define ACODEC_LONGTEXT N_( \
87 "Allows you to specify the destination audio codec used for the " \
89 #define AB_TEXT N_("Audio bitrate")
90 #define AB_LONGTEXT N_( \
91 "Allows you to specify the audio bitrate used for the streaming " \
93 #define ARATE_TEXT N_("Audio sample rate")
94 #define ARATE_LONGTEXT N_( \
95 "Allows you to specify the audio sample rate used for the streaming " \
97 #define ACHANS_TEXT N_("Audio channels")
98 #define ACHANS_LONGTEXT N_( \
99 "Allows you to specify the number of audio channels used for the " \
100 "streaming output." )
102 #define THREADS_TEXT N_("Number of threads")
103 #define THREADS_LONGTEXT N_( \
104 "Allows you to specify the number of threads used for the transcoding." )
106 static int Open ( vlc_object_t * );
107 static void Close( vlc_object_t * );
109 #define SOUT_CFG_PREFIX "sout-transcode-"
112 set_description( _("Transcode stream output") );
113 set_capability( "sout stream", 50 );
114 add_shortcut( "transcode" );
115 set_callbacks( Open, Close );
117 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
118 VCODEC_LONGTEXT, VLC_FALSE );
119 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
120 VB_LONGTEXT, VLC_FALSE );
121 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
122 SCALE_LONGTEXT, VLC_FALSE );
123 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
124 DEINTERLACE_LONGTEXT, VLC_FALSE );
125 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
126 WIDTH_LONGTEXT, VLC_TRUE );
127 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
128 HEIGHT_LONGTEXT, VLC_TRUE );
130 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
131 CROPTOP_LONGTEXT, VLC_TRUE );
132 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
133 CROPLEFT_LONGTEXT, VLC_TRUE );
134 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
135 CROPBOTTOM_LONGTEXT, VLC_TRUE );
136 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
137 CROPRIGHT_LONGTEXT, VLC_TRUE );
139 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
140 ACODEC_LONGTEXT, VLC_FALSE );
141 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
142 AB_LONGTEXT, VLC_FALSE );
143 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
144 ACHANS_LONGTEXT, VLC_FALSE );
145 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
146 ARATE_LONGTEXT, VLC_TRUE );
148 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
149 THREADS_LONGTEXT, VLC_TRUE );
152 /*****************************************************************************
153 * Exported prototypes
154 *****************************************************************************/
155 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
156 static int Del ( sout_stream_t *, sout_stream_id_t * );
157 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
159 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
160 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
161 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
163 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
164 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
165 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
167 static int transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
169 static int EncoderThread( struct sout_stream_sys_t * p_sys );
171 static int pi_channels_maps[6] =
174 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
175 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
176 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
177 | AOUT_CHAN_REARRIGHT,
178 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
179 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
182 static const char *ppsz_sout_options[] = {
183 "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
184 "scale", "width", "height", "deinterlace", "threads",
185 "acodec", "ab", "samplerate", "channels", NULL
188 #define PICTURE_RING_SIZE 64
190 struct sout_stream_sys_t
194 sout_stream_t * p_out;
195 sout_stream_id_t * id_video;
197 vlc_mutex_t lock_out;
199 picture_t * pp_pics[PICTURE_RING_SIZE];
200 int i_first_pic, i_last_pic;
202 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
203 sout_cfg_t *p_audio_cfg;
208 vlc_fourcc_t i_vcodec; /* " video " " " " */
209 sout_cfg_t *p_video_cfg;
214 vlc_bool_t b_deinterlace;
224 vlc_bool_t b_input_has_b_frames;
226 mtime_t i_output_pts;
229 /*****************************************************************************
231 *****************************************************************************/
232 static int Open( vlc_object_t *p_this )
234 sout_stream_t *p_stream = (sout_stream_t*)p_this;
235 sout_stream_sys_t *p_sys;
239 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
241 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
244 msg_Err( p_stream, "cannot create chain" );
249 p_sys->b_input_has_b_frames = VLC_FALSE;
250 p_sys->i_output_pts = 0;
252 psz_opts = sout_cfg_find_value( p_stream->p_cfg, "aopts" );
253 p_sys->p_audio_cfg = NULL;
254 if( psz_opts && *psz_opts )
256 char *psz_name, *psz_next, *psz_tmp;
257 asprintf( &psz_tmp, "aopts%s", psz_opts );
258 psz_next = sout_cfg_parser( &psz_name, &p_sys->p_audio_cfg, psz_tmp );
259 if( psz_next ) free( psz_next );
263 psz_opts = sout_cfg_find_value( p_stream->p_cfg, "vopts" );
264 p_sys->p_video_cfg = NULL;
265 if( psz_opts && *psz_opts )
267 char *psz_name, *psz_next, *psz_tmp;
268 asprintf( &psz_tmp, "vopts%s", psz_opts );
269 psz_next = sout_cfg_parser( &psz_name, &p_sys->p_video_cfg, psz_tmp );
270 if( psz_next ) free( psz_next );
274 sout_ParseCfg( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
277 /* Audio transcoding parameters */
278 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
280 if( val.psz_string && *val.psz_string )
283 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
284 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
286 if( val.psz_string ) free( val.psz_string );
288 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
289 p_sys->i_abitrate = val.i_int;
290 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
292 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
293 p_sys->i_sample_rate = val.i_int;
295 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
296 p_sys->i_channels = val.i_int;
298 if( p_sys->i_acodec )
300 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
301 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
302 p_sys->i_channels, p_sys->i_abitrate / 1000 );
305 /* Video transcoding parameters */
306 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
308 if( val.psz_string && *val.psz_string )
311 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
312 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
314 if( val.psz_string ) free( val.psz_string );
316 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
317 p_sys->i_vbitrate = val.i_int;
318 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
320 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
321 p_sys->f_scale = val.f_float;
323 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
324 p_sys->i_width = val.i_int;
326 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
327 p_sys->i_height = val.i_int;
329 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
330 p_sys->b_deinterlace = val.b_bool;
332 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
333 p_sys->i_crop_top = val.i_int;
334 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
335 p_sys->i_crop_bottom = val.i_int;
336 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
337 p_sys->i_crop_left = val.i_int;
338 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
339 p_sys->i_crop_right = val.i_int;
341 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
342 p_sys->i_threads = val.i_int;
344 if( p_sys->i_vcodec )
346 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
347 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
348 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
351 p_stream->pf_add = Add;
352 p_stream->pf_del = Del;
353 p_stream->pf_send = Send;
354 p_stream->p_sys = p_sys;
357 avcodec_register_all();
362 /*****************************************************************************
364 *****************************************************************************/
365 static void Close( vlc_object_t * p_this )
367 sout_stream_t *p_stream = (sout_stream_t*)p_this;
368 sout_stream_sys_t *p_sys = p_stream->p_sys;
370 sout_stream_delete( p_sys->p_out );
372 while( p_sys->p_audio_cfg != NULL )
374 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
376 if( p_sys->p_audio_cfg->psz_name )
377 free( p_sys->p_audio_cfg->psz_name );
378 if( p_sys->p_audio_cfg->psz_value )
379 free( p_sys->p_audio_cfg->psz_value );
380 free( p_sys->p_audio_cfg );
382 p_sys->p_audio_cfg = p_next;
385 while( p_sys->p_video_cfg != NULL )
387 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
389 if( p_sys->p_video_cfg->psz_name )
390 free( p_sys->p_video_cfg->psz_name );
391 if( p_sys->p_video_cfg->psz_value )
392 free( p_sys->p_video_cfg->psz_value );
393 free( p_sys->p_video_cfg );
395 p_sys->p_video_cfg = p_next;
398 vlc_object_destroy( p_sys );
401 struct sout_stream_id_t
403 vlc_fourcc_t b_transcode;
404 es_format_t f_src; /* only if transcoding */
405 es_format_t f_dst; /* " " " */
406 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
408 /* id of the out stream */
412 encoder_t *p_encoder;
413 vlc_fourcc_t b_enc_inited;
417 AVCodecContext *ff_dec_c;
427 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
428 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
429 AVFrame *p_ff_pic_tmp2; /* to do resample */
431 ImgReSampleContext *p_vresample;
435 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
437 sout_stream_sys_t *p_sys = p_stream->p_sys;
438 sout_stream_id_t *id;
440 id = malloc( sizeof( sout_stream_id_t ) );
443 id->p_encoder = NULL;
445 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
448 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
449 (char*)&p_fmt->i_codec,
450 (char*)&p_sys->i_acodec );
453 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
455 /* create dst format */
456 es_format_Init( &id->f_dst, AUDIO_ES, p_sys->i_acodec );
457 id->f_dst.i_id = id->f_src.i_id;
458 id->f_dst.i_group = id->f_src.i_group;
459 if( id->f_src.psz_language )
460 id->f_dst.psz_language = strdup( id->f_src.psz_language );
461 id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ?
462 p_sys->i_sample_rate : id->f_src.audio.i_rate;
463 id->f_dst.audio.i_channels = p_sys->i_channels > 0 ?
464 p_sys->i_channels : id->f_src.audio.i_channels;
465 id->f_dst.i_bitrate = p_sys->i_abitrate;
466 id->f_dst.audio.i_blockalign = 0;
467 id->f_dst.i_extra = 0;
468 id->f_dst.p_extra = NULL;
470 /* build decoder -> filter -> encoder */
471 if( transcode_audio_ffmpeg_new( p_stream, id ) )
473 msg_Err( p_stream, "cannot create audio chain" );
478 /* open output stream */
479 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
480 id->b_transcode = VLC_TRUE;
488 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
491 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
492 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
494 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
496 /* create dst format */
497 es_format_Init( &id->f_dst, VIDEO_ES, p_sys->i_vcodec );
498 id->f_dst.i_id = id->f_src.i_id;
499 id->f_dst.i_group = id->f_src.i_group;
500 if( id->f_src.psz_language )
501 id->f_dst.psz_language = strdup( id->f_src.psz_language );
502 id->f_dst.video.i_width = p_sys->i_width;
503 id->f_dst.video.i_height = p_sys->i_height;
504 id->f_dst.i_bitrate = p_sys->i_vbitrate;
505 id->f_dst.i_extra = 0;
506 id->f_dst.p_extra = NULL;
508 /* build decoder -> filter -> encoder */
509 if( transcode_video_ffmpeg_new( p_stream, id ) )
511 msg_Err( p_stream, "cannot create video chain" );
516 /* open output stream */
517 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
519 id->b_transcode = VLC_TRUE;
523 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
524 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
525 id->b_transcode = VLC_FALSE;
537 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
539 sout_stream_sys_t *p_sys = p_stream->p_sys;
541 if( id->b_transcode )
543 if( id->f_src.i_cat == AUDIO_ES )
545 transcode_audio_ffmpeg_close( p_stream, id );
547 else if( id->f_src.i_cat == VIDEO_ES )
549 transcode_video_ffmpeg_close( p_stream, id );
553 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
559 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
562 sout_stream_sys_t *p_sys = p_stream->p_sys;
564 if( id->b_transcode )
566 block_t *p_buffer_out;
568 /* Be sure to have padding */
569 p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
570 if( p_buffer == NULL )
574 p_buffer->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
575 memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
577 if( id->f_src.i_cat == AUDIO_ES )
579 transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
582 else if( id->f_src.i_cat == VIDEO_ES )
584 if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
585 &p_buffer_out ) != VLC_SUCCESS )
587 block_Release( p_buffer );
591 block_Release( p_buffer );
595 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
599 else if( id->id != NULL )
601 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
605 block_Release( p_buffer );
610 /****************************************************************************
611 * ffmpeg decoder reencocdr part
612 ****************************************************************************/
618 } fourcc_to_ff_code[] =
621 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
622 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
623 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
624 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
625 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
626 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
627 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
628 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
629 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
632 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
633 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
634 #if LIBAVCODEC_BUILD >= 4676
635 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
637 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
638 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
639 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
640 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
641 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
642 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
643 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
644 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
645 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
646 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
647 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
648 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
649 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
650 #if LIBAVCODEC_BUILD >= 4666
651 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
652 { VLC_FOURCC( 'h', '2', '6', '4' ), CODEC_ID_H264 },
655 /* raw video code, only used for 'encoding' */
656 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
657 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
658 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
659 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
660 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
661 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
662 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
663 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
664 { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
665 { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
667 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
670 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
674 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
676 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
678 return fourcc_to_ff_code[i].i_ff_codec;
685 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
689 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
690 case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
691 case VLC_FOURCC( 'I', '4', '2', '0' ):
692 return PIX_FMT_YUV420P;
693 case VLC_FOURCC( 'I', '4', '2', '2' ):
694 return PIX_FMT_YUV422P;
695 case VLC_FOURCC( 'I', '4', '4', '4' ):
696 return PIX_FMT_YUV444P;
697 case VLC_FOURCC( 'R', 'V', '1', '5' ):
698 return PIX_FMT_RGB555;
699 case VLC_FOURCC( 'R', 'V', '1', '6' ):
700 return PIX_FMT_RGB565;
701 case VLC_FOURCC( 'R', 'V', '2', '4' ):
702 return PIX_FMT_BGR24;
703 case VLC_FOURCC( 'R', 'V', '3', '2' ):
704 return PIX_FMT_RGBA32;
705 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
706 return PIX_FMT_GRAY8;
707 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
708 return PIX_FMT_YUV422;
714 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
718 case PIX_FMT_YUV420P:
719 return VLC_FOURCC('I','4','2','0');
720 case PIX_FMT_YUV422P:
721 return VLC_FOURCC('I','4','2','2');
722 case PIX_FMT_YUV444P:
723 return VLC_FOURCC('I','4','4','4');
726 return VLC_FOURCC('Y','U','Y','2');
729 return VLC_FOURCC('R','V','1','5');
731 return VLC_FOURCC('R','V','1','6');
733 return VLC_FOURCC('R','V','2','4');
735 return VLC_FOURCC('R','V','3','2');
737 return VLC_FOURCC('G','R','E','Y');
739 case PIX_FMT_YUV410P:
740 case PIX_FMT_YUV411P:
747 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
748 sout_stream_id_t *id )
752 if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
753 id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
754 id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
755 id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
759 id->ff_dec_c = avcodec_alloc_context();
760 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
761 id->ff_dec_c->channels = id->f_src.audio.i_channels;
762 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
763 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
768 i_ff_codec = get_ff_codec( id->f_src.i_codec );
769 if( i_ff_codec == 0 )
771 msg_Err( p_stream, "cannot find decoder id" );
775 id->ff_dec = avcodec_find_decoder( i_ff_codec );
778 msg_Err( p_stream, "cannot find decoder (avcodec)" );
782 id->ff_dec_c = avcodec_alloc_context();
783 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
784 id->ff_dec_c->channels = id->f_src.audio.i_channels;
785 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
786 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
788 id->ff_dec_c->extradata_size = id->f_src.i_extra;
789 id->ff_dec_c->extradata = id->f_src.p_extra;
790 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
792 msg_Err( p_stream, "cannot open decoder" );
797 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
798 id->i_buffer_pos = 0;
799 id->p_buffer = malloc( id->i_buffer );
801 /* Sanity check for audio channels */
802 id->f_dst.audio.i_channels = __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
805 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
807 /* Initialization of encoder format structures */
808 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
809 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
810 id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
811 id->p_encoder->fmt_in.audio.i_physical_channels =
812 id->p_encoder->fmt_in.audio.i_original_channels =
813 pi_channels_maps[id->f_dst.audio.i_channels];
814 id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
816 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
817 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
818 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
820 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
822 /* Attach object to parent so object variables inheritance works */
823 vlc_object_attach( id->p_encoder, p_stream );
825 id->p_encoder->p_module =
826 module_Need( id->p_encoder, "encoder", NULL, 0 );
827 if( !id->p_encoder->p_module )
829 vlc_object_destroy( id->p_encoder );
830 msg_Err( p_stream, "cannot open encoder" );
834 id->b_enc_inited = VLC_FALSE;
836 id->f_dst.audio.i_channels = id->p_encoder->fmt_in.audio.i_channels;
837 id->f_dst.audio.i_rate = id->p_encoder->fmt_in.audio.i_rate;
838 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
839 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
841 /* Hack for mp3 transcoding support */
842 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
844 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
850 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
851 sout_stream_id_t *id )
853 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
854 av_free( id->ff_dec_c );
856 module_Unneed( id->p_encoder, id->p_encoder->p_module );
858 vlc_object_detach( id->p_encoder );
859 vlc_object_destroy( id->p_encoder );
861 free( id->p_buffer );
864 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
865 sout_stream_id_t *id,
869 aout_buffer_t aout_buf;
871 int i_buffer = in->i_buffer;
872 char *p_buffer = in->p_buffer;
873 id->i_dts = in->i_dts;
878 id->i_buffer_pos = 0;
880 /* decode as much data as possible */
885 i_used = avcodec_decode_audio( id->ff_dec_c,
886 (int16_t*)id->p_buffer, &id->i_buffer_pos,
887 p_buffer, i_buffer );
890 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
895 msg_Warn( p_stream, "error audio decoding");
902 if ( id->i_buffer_pos < 0 )
904 msg_Warn( p_stream, "weird error audio decoding");
910 int16_t *sout = (int16_t*)id->p_buffer;
912 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
913 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
915 int8_t *sin = (int8_t*)p_buffer;
916 int i_used = __MIN( id->i_buffer/2, i_buffer );
917 int i_samples = i_used;
919 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
920 while( i_samples > 0 )
922 *sout++ = ( *sin++ ) << 8;
926 while( i_samples > 0 )
928 *sout++ = ( *sin++ - 128 ) << 8;
934 id->i_buffer_pos = i_used * 2;
936 else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
937 id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
939 int16_t *sin = (int16_t*)p_buffer;
940 int i_used = __MIN( id->i_buffer, i_buffer );
941 int i_samples = i_used / 2;
944 memcpy( sout, sin, i_used );
946 #ifdef WORDS_BIGENDIAN
947 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
949 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
952 uint8_t *dat = (uint8_t*)sout;
954 while( i_samples > 0 )
969 id->i_buffer_pos = i_used;
973 if( id->i_buffer_pos == 0 ) continue;
975 /* Encode as much data as possible */
976 if( !id->b_enc_inited && id->p_encoder->pf_header )
978 p_block = id->p_encoder->pf_header( id->p_encoder );
982 block_t *p_prev_block = p_block;
984 p_out = block_New( p_stream, p_block->i_buffer );
985 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
986 p_out->i_dts = p_out->i_pts = in->i_dts;
988 block_ChainAppend( out, p_out );
990 p_block = p_block->p_next;
991 block_Release( p_prev_block );
994 id->b_enc_inited = VLC_TRUE;
997 aout_buf.p_buffer = id->p_buffer;
998 aout_buf.i_nb_bytes = id->i_buffer_pos;
999 aout_buf.i_nb_samples = id->i_buffer_pos / 2 / id->f_src.audio.i_channels;
1000 aout_buf.start_date = id->i_dts;
1001 aout_buf.end_date = id->i_dts;
1003 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
1004 id->f_src.audio.i_channels / id->f_src.audio.i_rate );
1006 if( id->p_encoder->fmt_in.audio.i_channels == 1 &&
1007 id->f_src.audio.i_channels > 1 )
1009 int16_t *p_sample = (int16_t *)aout_buf.p_buffer;
1010 int i_src_c = id->f_src.audio.i_channels;
1013 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1017 for( j = 1; j < i_src_c; j++ )
1019 c += p_sample[i_src_c * i + j];
1021 p_sample[i] = c / (i_src_c-1);
1023 aout_buf.i_nb_bytes = i * 2;
1025 else if( id->p_encoder->fmt_in.audio.i_channels == 2 &&
1026 id->f_src.audio.i_channels > 2 )
1028 int i_src_c = id->f_src.audio.i_channels;
1031 static const float mixf_l[4][6] = /* [i_src_c - 3][channel index] */
1033 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1034 { 0.00, 0.50, 0.50, 0.00, 0.00, 0.00 }, /* 4 channels */
1035 { 0.00, 0.50, 0.00, 0.50, 0.00, 0.00 }, /* 5 channels */
1036 { 0.00, 0.34, 0.33, 0.00, 0.33, 0.00 }, /* 6 channels */
1038 static const float mixf_r[4][6] = /* [i_src_c - 3][channel index] */
1040 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1041 { 0.00, 0.00, 0.50, 0.50, 0.00, 0.00 }, /* 4 channels */
1042 { 0.00, 0.00, 0.50, 0.00, 0.50, 0.00 }, /* 5 channels */
1043 { 0.00, 0.00, 0.33, 0.34, 0.00, 0.33 }, /* 6 channels */
1047 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1049 int16_t *p_src = (int16_t *)aout_buf.p_buffer + i_src_c * i;
1050 int16_t *p_dst = (int16_t *)aout_buf.p_buffer + 2 * i;
1053 float l = 0.0, r = 0.0;
1054 for( j = 0; j < i_src_c; j++ )
1056 l += mixf_l[i_src_c-3][j] * p_src[j];
1057 r += mixf_r[i_src_c-3][j] * p_src[j];
1060 p_dst[0] = (int)( l + 0.5 );
1061 p_dst[1] = (int)( r + 0.5 );
1063 aout_buf.i_nb_bytes = i * 2 * 2;
1065 else if( id->f_src.audio.i_channels !=
1066 id->p_encoder->fmt_in.audio.i_channels )
1071 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
1072 static const int translation[7][6] =
1073 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
1074 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
1075 { 0, 1, 0, 0, 0, 0 }, /* 2 */
1076 { 1, 2, 0, 0, 0, 0 }, /* 3 */
1077 { 1, 3, 2, 0, 0, 0 }, /* 4 */
1078 { 1, 3, 4, 2, 0, 0 }, /* 5 */
1079 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
1081 /* dumb downmixing */
1082 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1084 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
1085 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
1087 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
1088 p_buffer[i*id->f_src.audio.i_channels+
1089 translation[id->f_src.audio.i_channels][j]];
1092 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1095 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1099 block_t *p_prev_block = p_block;
1101 p_out = block_New( p_stream, p_block->i_buffer );
1102 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1103 p_out->i_dts = p_block->i_dts;
1104 p_out->i_pts = p_block->i_pts;
1105 p_out->i_length = p_block->i_length;
1106 block_ChainAppend( out, p_out );
1108 p_block = p_block->p_next;
1109 block_Release( p_prev_block );
1120 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1121 sout_stream_id_t *id )
1123 sout_stream_sys_t *p_sys = p_stream->p_sys;
1127 if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1128 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1129 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1130 id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1131 id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1132 id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1133 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1134 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1135 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1136 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1137 id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1140 id->ff_dec_c = avcodec_alloc_context();
1141 id->ff_dec_c->width = id->f_src.video.i_width;
1142 id->ff_dec_c->height = id->f_src.video.i_height;
1143 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_codec );
1145 #if LIBAVCODEC_BUILD >= 4687
1146 if( id->ff_dec_c->width )
1147 id->ff_dec_c->sample_aspect_ratio =
1148 av_d2q( id->f_src.video.i_aspect / (double)VOUT_ASPECT_FACTOR *
1149 id->ff_dec_c->height / id->ff_dec_c->width, 255 );
1151 id->ff_dec_c->aspect_ratio =
1152 id->f_src.video.i_aspect / (float)VOUT_ASPECT_FACTOR;
1158 i_ff_codec = get_ff_codec( id->f_src.i_codec );
1159 if( i_ff_codec == 0 )
1161 msg_Err( p_stream, "cannot find decoder" );
1162 return VLC_EGENERIC;
1165 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1168 msg_Err( p_stream, "cannot find decoder" );
1169 return VLC_EGENERIC;
1172 id->ff_dec_c = avcodec_alloc_context();
1173 id->ff_dec_c->width = id->f_src.video.i_width;
1174 id->ff_dec_c->height = id->f_src.video.i_height;
1175 id->ff_dec_c->bits_per_sample=id->f_src.video.i_bits_per_pixel;
1176 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1178 if( id->f_src.i_extra > 0 )
1180 if( i_ff_codec == CODEC_ID_SVQ3 )
1182 int i_size = id->f_src.i_extra;
1185 id->ff_dec_c->extradata_size = i_size + 12;
1186 p = id->ff_dec_c->extradata = malloc( i_size + 12 );
1188 memcpy( &p[0], "SVQ3", 4 );
1189 memset( &p[4], 0, 8 );
1190 memcpy( &p[12], id->f_src.p_extra, i_size );
1192 /* Now remove all atoms before the SMI one */
1193 if( id->ff_dec_c->extradata_size > 0x5a && strncmp( &p[0x56], "SMI ", 4 ) )
1195 uint8_t *psz = &p[0x52];
1197 while( psz < &p[id->ff_dec_c->extradata_size - 8] )
1199 int i_size = GetDWBE( psz );
1202 /* FIXME handle 1 as long size */
1205 if( !strncmp( &psz[4], "SMI ", 4 ) )
1207 memmove( &p[0x52], psz, &p[id->ff_dec_c->extradata_size] - psz );
1216 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1217 id->ff_dec_c->extradata = malloc( id->f_src.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
1219 memcpy( id->ff_dec_c->extradata, id->f_src.p_extra, id->f_src.i_extra );
1220 memset( (uint8_t*)id->ff_dec_c->extradata + id->f_src.i_extra, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1223 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1224 id->ff_dec_c->error_resilience= -1;
1225 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1226 id->ff_dec_c->opaque = p_sys;
1228 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1230 msg_Err( p_stream, "cannot open decoder" );
1231 return VLC_EGENERIC;
1236 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1238 /* Initialization of encoder format structures */
1239 es_format_Init( &id->p_encoder->fmt_in,
1240 id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1242 /* The dimensions will be set properly later on.
1243 * Just put sensible values so we can test if there is an encoder. */
1244 id->p_encoder->fmt_in.video.i_width =
1245 id->f_src.video.i_width ? id->f_src.video.i_width : 16;
1246 id->p_encoder->fmt_in.video.i_height =
1247 id->f_src.video.i_height ? id->f_src.video.i_height : 16;
1249 id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1250 id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1253 id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1254 #if LIBAVCODEC_BUILD >= 4662
1255 id->p_encoder->fmt_in.video.i_frame_rate_base =
1256 id->ff_dec_c->frame_rate_base;
1259 #if LIBAVCODEC_BUILD >= 4687
1260 if( id->ff_dec_c->height )
1261 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1262 ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1263 id->ff_dec_c->width / id->ff_dec_c->height );
1265 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1266 id->ff_dec_c->aspect_ratio;
1270 /* Check whether a particular aspect ratio was requested */
1271 if( id->f_src.video.i_aspect )
1273 id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1274 id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1277 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1278 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1279 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1281 id->p_encoder->i_threads = p_sys->i_threads;
1283 id->p_ff_pic = avcodec_alloc_frame();
1284 id->p_ff_pic_tmp0 = NULL;
1285 id->p_ff_pic_tmp1 = NULL;
1286 id->p_ff_pic_tmp2 = NULL;
1287 id->p_vresample = NULL;
1289 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1291 /* Attach object to parent so object variables inheritance works */
1292 vlc_object_attach( id->p_encoder, p_stream );
1294 id->p_encoder->p_module =
1295 module_Need( id->p_encoder, "encoder", NULL, 0 );
1297 if( !id->p_encoder->p_module )
1299 vlc_object_destroy( id->p_encoder );
1300 msg_Err( p_stream, "cannot find encoder" );
1301 return VLC_EGENERIC;
1304 /* Close the encoder.
1305 * We'll open it only when we have the first frame */
1306 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1307 id->p_encoder->p_module = NULL;
1309 id->b_enc_inited = VLC_FALSE;
1311 if ( p_sys->i_threads >= 1 )
1313 p_sys->id_video = id;
1314 vlc_mutex_init( p_stream, &p_sys->lock_out );
1315 vlc_cond_init( p_stream, &p_sys->cond );
1316 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1317 p_sys->i_first_pic = 0;
1318 p_sys->i_last_pic = 0;
1319 p_sys->p_buffers = NULL;
1320 p_sys->b_die = p_sys->b_error = 0;
1321 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1322 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1324 vlc_object_destroy( id->p_encoder );
1325 msg_Err( p_stream, "cannot spawn encoder thread" );
1326 return VLC_EGENERIC;
1333 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1334 sout_stream_id_t *id )
1336 if ( p_stream->p_sys->i_threads >= 1 )
1338 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1339 p_stream->p_sys->b_die = 1;
1340 vlc_cond_signal( &p_stream->p_sys->cond );
1341 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1342 vlc_thread_join( p_stream->p_sys );
1343 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1344 vlc_cond_destroy( &p_stream->p_sys->cond );
1348 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1349 av_free( id->ff_dec_c );
1352 if( id->p_encoder->p_module )
1353 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1354 vlc_object_detach( id->p_encoder );
1355 vlc_object_destroy( id->p_encoder );
1360 free( id->p_ff_pic );
1363 if( id->p_ff_pic_tmp0 )
1365 free( id->p_ff_pic_tmp0->data[0] );
1366 free( id->p_ff_pic_tmp0 );
1368 if( id->p_ff_pic_tmp1)
1370 free( id->p_ff_pic_tmp1->data[0] );
1371 free( id->p_ff_pic_tmp1 );
1373 if( id->p_ff_pic_tmp2)
1375 free( id->p_ff_pic_tmp2->data[0] );
1376 free( id->p_ff_pic_tmp2 );
1378 if( id->p_vresample )
1380 img_resample_close( id->p_vresample );
1384 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1385 sout_stream_id_t *id, block_t *in, block_t **out )
1387 sout_stream_sys_t *p_sys = p_stream->p_sys;
1397 i_data = in->i_buffer;
1398 p_data = in->p_buffer;
1407 frame = id->p_ff_pic;
1408 p_sys->i_input_pts = in->i_pts;
1409 p_sys->i_input_dts = in->i_dts;
1412 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1419 avpicture_fill( (AVPicture*)frame, p_data,
1420 id->ff_dec_c->pix_fmt,
1421 id->ff_dec_c->width, id->ff_dec_c->height );
1426 frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts :
1429 frame->pict_type = FF_I_TYPE;
1434 msg_Warn( p_stream, "error");
1435 return VLC_EGENERIC;
1445 /* Get the pts of the decoded frame if any, otherwise keep the
1446 * interpolated one */
1447 if( frame->pts != AV_NOPTS_VALUE )
1449 p_sys->i_output_pts = frame->pts;
1452 /* Sanity check (seems to be needed for some streams ) */
1453 if( frame->pict_type == FF_B_TYPE )
1455 p_sys->b_input_has_b_frames = VLC_TRUE;
1458 if( !id->b_enc_inited )
1460 /* Hack because of the copy packetizer which can fail to detect the
1461 * proper size (which forces us to wait until the 1st frame
1463 int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1464 p_sys->i_crop_right;
1465 int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1466 p_sys->i_crop_bottom;
1468 if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1471 /* Apply the scaling */
1472 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1473 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1475 else if( id->f_dst.video.i_width > 0 &&
1476 id->f_dst.video.i_height <= 0 )
1478 id->f_dst.video.i_height =
1479 id->f_dst.video.i_width / (double)i_width * i_height;
1481 else if( id->f_dst.video.i_width <= 0 &&
1482 id->f_dst.video.i_height > 0 )
1484 id->f_dst.video.i_width =
1485 id->f_dst.video.i_height / (double)i_height * i_width;
1488 id->p_encoder->fmt_in.video.i_width =
1489 id->p_encoder->fmt_out.video.i_width =
1490 id->f_dst.video.i_width;
1491 id->p_encoder->fmt_in.video.i_height =
1492 id->p_encoder->fmt_out.video.i_height =
1493 id->f_dst.video.i_height;
1495 id->p_encoder->fmt_out.i_extra = 0;
1496 id->p_encoder->fmt_out.p_extra = NULL;
1498 id->p_encoder->p_module =
1499 module_Need( id->p_encoder, "encoder", NULL, 0 );
1500 if( !id->p_encoder->p_module )
1502 vlc_object_destroy( id->p_encoder );
1503 msg_Err( p_stream, "cannot find encoder" );
1504 id->b_transcode = VLC_FALSE;
1505 return VLC_EGENERIC;
1508 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1509 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1511 /* Hack for mp2v/mp1v transcoding support */
1512 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1513 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1515 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1519 p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1522 msg_Err( p_stream, "cannot add this stream" );
1523 transcode_video_ffmpeg_close( p_stream, id );
1524 id->b_transcode = VLC_FALSE;
1525 return VLC_EGENERIC;
1528 if( id->p_encoder->pf_header )
1530 p_block = id->p_encoder->pf_header( id->p_encoder );
1534 block_t *p_prev_block = p_block;
1536 p_out = block_New( p_stream,
1537 p_block->i_buffer );
1538 memcpy( p_out->p_buffer, p_block->p_buffer,
1540 p_out->i_dts = p_out->i_pts = in->i_dts;
1541 p_out->i_length = 0;
1542 block_ChainAppend( out, p_out );
1544 p_block = p_block->p_next;
1545 block_Release( p_prev_block );
1549 id->i_inter_pixfmt =
1550 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1552 id->b_enc_inited = VLC_TRUE;
1556 if( p_stream->p_sys->b_deinterlace )
1558 if( id->p_ff_pic_tmp0 == NULL )
1562 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1563 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1564 id->ff_dec_c->width, id->ff_dec_c->height );
1566 buf = malloc( i_size );
1568 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1570 id->ff_dec_c->width, id->ff_dec_c->height );
1573 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1574 id->ff_dec_c->pix_fmt,
1575 id->ff_dec_c->width, id->ff_dec_c->height );
1577 #if LIBAVCODEC_BUILD >= 4685
1578 id->p_ff_pic_tmp0->interlaced_frame = 0;
1580 id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1581 frame = id->p_ff_pic_tmp0;
1584 /* convert pix format */
1585 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1587 if( id->p_ff_pic_tmp1 == NULL )
1591 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1592 i_size = avpicture_get_size( id->i_inter_pixfmt,
1593 id->ff_dec_c->width,
1594 id->ff_dec_c->height );
1596 buf = malloc( i_size );
1598 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1600 id->ff_dec_c->width, id->ff_dec_c->height );
1603 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1604 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1605 id->ff_dec_c->width, id->ff_dec_c->height );
1607 id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1608 #if LIBAVCODEC_BUILD >= 4685
1609 id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1610 id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1612 frame = id->p_ff_pic_tmp1;
1615 /* convert size and crop */
1616 if( id->ff_dec_c->width != id->f_dst.video.i_width ||
1617 id->ff_dec_c->height != id->f_dst.video.i_height ||
1618 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1619 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1621 if( id->p_ff_pic_tmp2 == NULL )
1625 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1626 i_size = avpicture_get_size( id->i_inter_pixfmt,
1627 id->f_dst.video.i_width,
1628 id->f_dst.video.i_height );
1630 buf = malloc( i_size );
1632 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1634 id->f_dst.video.i_width,
1635 id->f_dst.video.i_height );
1638 img_resample_full_init( id->f_dst.video.i_width,
1639 id->f_dst.video.i_height,
1640 id->ff_dec_c->width,
1641 id->ff_dec_c->height,
1642 p_stream->p_sys->i_crop_top,
1643 p_stream->p_sys->i_crop_bottom,
1644 p_stream->p_sys->i_crop_left,
1645 p_stream->p_sys->i_crop_right
1646 #if LIBAVCODEC_BUILD >= 4708
1653 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1654 (AVPicture*)frame );
1656 id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1657 #if LIBAVCODEC_BUILD >= 4685
1658 id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1659 id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1661 frame = id->p_ff_pic_tmp2;
1665 p_pic = malloc(sizeof(picture_t));
1666 vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1667 id->p_encoder->fmt_in.i_codec,
1668 id->f_dst.video.i_width, id->f_dst.video.i_height,
1669 id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1670 id->f_dst.video.i_height );
1672 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1674 p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1675 if ( p_sys->i_threads >= 1 )
1677 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines *
1678 p_pic->p[i_plane].i_pitch);
1679 p_stream->p_vlc->pf_memcpy( p_pic->p[i_plane].p_pixels,
1680 frame->data[i_plane], p_pic->p[i_plane].i_lines *
1681 p_pic->p[i_plane].i_pitch );
1685 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1689 /* Set the pts of the frame being encoded */
1690 p_pic->date = p_sys->i_output_pts;
1692 p_pic->i_nb_fields = frame->repeat_pict;
1693 #if LIBAVCODEC_BUILD >= 4685
1694 p_pic->b_progressive = !frame->interlaced_frame;
1695 p_pic->b_top_field_first = frame->top_field_first;
1698 /* Interpolate the next PTS
1699 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1700 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1702 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1703 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1706 if ( p_sys->i_threads >= 1 )
1708 vlc_mutex_lock( &p_sys->lock_out );
1709 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1710 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1711 *out = p_sys->p_buffers;
1712 p_sys->p_buffers = NULL;
1713 vlc_cond_signal( &p_sys->cond );
1714 vlc_mutex_unlock( &p_sys->lock_out );
1719 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1722 block_ChainAppend( out, p_block );
1736 static int EncoderThread( sout_stream_sys_t * p_sys )
1738 sout_stream_id_t * id = p_sys->id_video;
1742 while ( !p_sys->b_die && !p_sys->b_error )
1746 vlc_mutex_lock( &p_sys->lock_out );
1747 while ( p_sys->i_last_pic == p_sys->i_first_pic )
1749 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1750 if ( p_sys->b_die || p_sys->b_error )
1753 if ( p_sys->b_die || p_sys->b_error )
1755 vlc_mutex_unlock( &p_sys->lock_out );
1759 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1760 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1761 vlc_mutex_unlock( &p_sys->lock_out );
1763 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1764 vlc_mutex_lock( &p_sys->lock_out );
1767 block_ChainAppend( &p_sys->p_buffers, p_block );
1769 vlc_mutex_unlock( &p_sys->lock_out );
1771 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1773 free( p_pic->p[i_plane].p_pixels );
1778 while ( p_sys->i_last_pic != p_sys->i_first_pic )
1780 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1781 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1783 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1785 free( p_pic->p[i_plane].p_pixels );
1790 block_ChainRelease( p_sys->p_buffers );
1795 /*****************************************************************************
1796 * transcode_video_ffmpeg_getframebuf:
1798 * Callback used by ffmpeg to get a frame buffer.
1799 * We use it to get the right PTS for each decoded picture.
1800 *****************************************************************************/
1801 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1804 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1807 if( p_sys->i_input_pts )
1809 p_frame->pts = p_sys->i_input_pts;
1811 else if( p_sys->i_input_dts )
1813 /* Some demuxers/packetizers only set the dts so let's try to find a
1814 * useful timestamp from this */
1815 if( !p_context->has_b_frames || !p_sys->b_input_has_b_frames ||
1816 !p_frame->reference || !p_sys->i_output_pts )
1818 p_frame->pts = p_sys->i_input_dts +
1819 /* Hack: ffmpeg encoding doesn't like frames with identical pts */
1820 (p_sys->i_output_pts ? 0 : 50000);
1822 else p_frame->pts = AV_NOPTS_VALUE;
1824 else p_frame->pts = AV_NOPTS_VALUE;
1826 if( p_sys->i_output_pts ) /* make sure 1st frame has a pts > 0 */
1828 p_sys->i_input_pts = 0;
1829 p_sys->i_input_dts = 0;
1832 return avcodec_default_get_buffer( p_context, p_frame );