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 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
55 set_description( _("Transcode stream output") );
56 set_capability( "sout stream", 50 );
57 add_shortcut( "transcode" );
58 set_callbacks( Open, Close );
62 /*****************************************************************************
64 *****************************************************************************/
65 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
66 static int Del ( sout_stream_t *, sout_stream_id_t * );
67 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
69 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
70 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
71 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
73 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
74 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
75 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
77 static int transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
79 static int EncoderThread( struct sout_stream_sys_t * p_sys );
81 static int pi_channels_maps[6] =
84 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
85 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
86 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
87 | AOUT_CHAN_REARRIGHT,
88 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
89 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
92 #define PICTURE_RING_SIZE 64
94 struct sout_stream_sys_t
98 sout_stream_t * p_out;
99 sout_stream_id_t * id_video;
101 vlc_mutex_t lock_out;
103 picture_t * pp_pics[PICTURE_RING_SIZE];
104 int i_first_pic, i_last_pic;
106 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
111 vlc_fourcc_t i_vcodec; /* " video " " " " */
122 vlc_bool_t b_deinterlace;
123 vlc_bool_t b_interlace;
124 vlc_bool_t b_strict_rc;
126 vlc_bool_t b_hurry_up;
127 int i_rc_buffer_size;
128 float f_rc_buffer_aggressivity;
129 float f_i_quant_factor;
130 int i_noise_reduction;
131 vlc_bool_t b_mpeg4_matrix;
133 vlc_bool_t b_trellis;
142 vlc_bool_t b_input_has_b_frames;
144 mtime_t i_output_pts;
147 /*****************************************************************************
149 *****************************************************************************/
150 static int Open( vlc_object_t *p_this )
152 sout_stream_t *p_stream = (sout_stream_t*)p_this;
153 sout_stream_sys_t *p_sys;
156 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
157 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
160 p_sys->i_vtolerance = -1;
161 p_sys->i_key_int = -1;
164 p_sys->f_i_quant_factor = 0.0;
165 #if LIBAVCODEC_BUILD >= 4673
166 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
168 p_sys->i_hq = VLC_FALSE;
170 p_sys->i_rc_buffer_size = 224*1024*8 * 3/2;
171 p_sys->f_rc_buffer_aggressivity = 0.1;
172 p_sys->i_threads = 0;
173 p_sys->b_trellis = 0;
174 p_sys->b_input_has_b_frames = VLC_FALSE;
175 p_sys->i_output_pts = 0;
177 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "acodec" ) ) )
182 memcpy( fcc, codec, __MIN( strlen( codec ), 4 ) );
184 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
186 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "samplerate" ) ) )
188 p_sys->i_sample_rate = atoi( val );
190 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "channels" ) ) )
192 p_sys->i_channels = atoi( val );
194 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ab" ) ) )
196 p_sys->i_abitrate = atoi( val );
197 if( p_sys->i_abitrate < 4000 )
199 p_sys->i_abitrate *= 1000;
203 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s", fcc,
204 p_sys->i_sample_rate, p_sys->i_channels,
205 p_sys->i_abitrate / 1000 );
208 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "vcodec" ) ) )
213 memcpy( fcc, codec, __MIN( strlen( codec ), 4 ) );
215 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
217 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "scale" ) ) )
219 p_sys->f_scale = atof( val );
221 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "width" ) ) )
223 p_sys->i_width = atoi( val );
225 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "height" ) ) )
227 p_sys->i_height = atoi( val );
229 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
231 p_sys->i_vbitrate = atoi( val );
232 if( p_sys->i_vbitrate < 16000 )
234 p_sys->i_vbitrate *= 1000;
237 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vt" ) ) )
239 p_sys->i_vtolerance = atoi( val );
241 if( sout_cfg_find( p_stream->p_cfg, "deinterlace" ) )
243 p_sys->b_deinterlace = VLC_TRUE;
245 if( sout_cfg_find( p_stream->p_cfg, "interlace" ) )
247 p_sys->b_interlace = VLC_TRUE;
249 if( sout_cfg_find( p_stream->p_cfg, "strict_rc" ) )
251 p_sys->b_strict_rc = VLC_TRUE;
253 if( sout_cfg_find( p_stream->p_cfg, "pre_me" ) )
255 p_sys->b_pre_me = VLC_TRUE;
257 if( sout_cfg_find( p_stream->p_cfg, "hurry_up" ) )
259 p_sys->b_hurry_up = VLC_TRUE;
260 /* hurry up mode needs noise reduction, even small */
261 p_sys->i_noise_reduction = 1;
263 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "rc_buffer_size" ) ) )
265 p_sys->i_rc_buffer_size = atoi( val );
267 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "rc_buffer_aggressivity" ) ) )
269 p_sys->f_rc_buffer_aggressivity = atof( val );
271 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "i_quant_factor" ) ) )
273 p_sys->f_i_quant_factor = atof( val );
275 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "noise_reduction" ) ) )
277 p_sys->i_noise_reduction = atoi( val );
279 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "mpeg4_matrix" ) ) )
281 p_sys->b_mpeg4_matrix = VLC_TRUE;
284 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "croptop" ) ) )
286 p_sys->i_crop_top = atoi( val );
288 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropbottom" ) ) )
290 p_sys->i_crop_bottom = atoi( val );
292 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropleft" ) ) )
294 p_sys->i_crop_left = atoi( val );
296 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropright" ) ) )
298 p_sys->i_crop_right = atoi( val );
300 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "keyint" ) ) )
302 p_sys->i_key_int = atoi( val );
304 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "bframes" ) ) )
306 p_sys->i_b_frames = atoi( val );
308 #if LIBAVCODEC_BUILD >= 4673
309 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "hq" ) ) )
311 if( !strcmp( val, "rd" ) )
313 p_sys->i_hq = FF_MB_DECISION_RD;
315 else if( !strcmp( val, "bits" ) )
317 p_sys->i_hq = FF_MB_DECISION_BITS;
319 else if( !strcmp( val, "simple" ) )
321 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
325 p_sys->i_hq = FF_MB_DECISION_RD;
329 if( sout_cfg_find( p_stream->p_cfg, "hq" ) )
331 p_sys->i_hq = VLC_TRUE;
334 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmin" ) ) )
336 p_sys->i_qmin = atoi( val );
338 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmax" ) ) )
340 p_sys->i_qmax = atoi( val );
342 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "threads" ) ) )
344 p_sys->i_threads = atoi( val );
346 if( sout_cfg_find( p_stream->p_cfg, "trellis" ) )
348 p_sys->b_trellis = VLC_TRUE;
351 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
352 fcc, p_sys->i_width, p_sys->i_height, p_sys->f_scale,
353 p_sys->i_vbitrate / 1000 );
358 msg_Err( p_stream, "cannot create chain" );
363 p_stream->pf_add = Add;
364 p_stream->pf_del = Del;
365 p_stream->pf_send = Send;
366 p_stream->p_sys = p_sys;
369 avcodec_register_all();
374 /*****************************************************************************
376 *****************************************************************************/
377 static void Close( vlc_object_t * p_this )
379 sout_stream_t *p_stream = (sout_stream_t*)p_this;
380 sout_stream_sys_t *p_sys = p_stream->p_sys;
382 sout_stream_delete( p_sys->p_out );
383 vlc_object_destroy( p_sys );
386 struct sout_stream_id_t
388 vlc_fourcc_t b_transcode;
389 es_format_t f_src; /* only if transcoding */
390 es_format_t f_dst; /* " " " */
391 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
393 /* id of the out stream */
397 encoder_t *p_encoder;
398 vlc_fourcc_t b_enc_inited;
402 AVCodecContext *ff_dec_c;
412 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
413 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
414 AVFrame *p_ff_pic_tmp2; /* to do resample */
416 ImgReSampleContext *p_vresample;
420 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
422 sout_stream_sys_t *p_sys = p_stream->p_sys;
423 sout_stream_id_t *id;
425 id = malloc( sizeof( sout_stream_id_t ) );
428 id->p_encoder = NULL;
430 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
433 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
434 (char*)&p_fmt->i_codec,
435 (char*)&p_sys->i_acodec );
438 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
440 /* create dst format */
441 es_format_Init( &id->f_dst, AUDIO_ES, p_sys->i_acodec );
442 id->f_dst.i_id = id->f_src.i_id;
443 id->f_dst.i_group = id->f_src.i_group;
444 if( id->f_src.psz_language ) id->f_dst.psz_language = strdup( id->f_src.psz_language );
445 id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ? p_sys->i_sample_rate : id->f_src.audio.i_rate;
446 id->f_dst.audio.i_channels = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.audio.i_channels;
447 id->f_dst.i_bitrate = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
448 id->f_dst.audio.i_blockalign = 0;
449 id->f_dst.i_extra = 0;
450 id->f_dst.p_extra = NULL;
452 /* build decoder -> filter -> encoder */
453 if( transcode_audio_ffmpeg_new( p_stream, id ) )
455 msg_Err( p_stream, "cannot create audio chain" );
460 /* open output stream */
461 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
462 id->b_transcode = VLC_TRUE;
470 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
473 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
474 (char*)&p_fmt->i_codec,
475 (char*)&p_sys->i_vcodec );
477 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
479 /* create dst format */
480 es_format_Init( &id->f_dst, VIDEO_ES, p_sys->i_vcodec );
481 id->f_dst.i_id = id->f_src.i_id;
482 id->f_dst.i_group = id->f_src.i_group;
483 if( id->f_src.psz_language ) id->f_dst.psz_language = strdup( id->f_src.psz_language );
484 id->f_dst.video.i_width = p_sys->i_width;
485 id->f_dst.video.i_height= p_sys->i_height;
486 id->f_dst.i_bitrate = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
487 id->f_dst.i_extra = 0;
488 id->f_dst.p_extra = NULL;
490 /* build decoder -> filter -> encoder */
491 if( transcode_video_ffmpeg_new( p_stream, id ) )
493 msg_Err( p_stream, "cannot create video chain" );
498 /* open output stream */
499 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
501 id->b_transcode = VLC_TRUE;
505 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
506 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
507 id->b_transcode = VLC_FALSE;
519 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
521 sout_stream_sys_t *p_sys = p_stream->p_sys;
523 if( id->b_transcode )
525 if( id->f_src.i_cat == AUDIO_ES )
527 transcode_audio_ffmpeg_close( p_stream, id );
529 else if( id->f_src.i_cat == VIDEO_ES )
531 transcode_video_ffmpeg_close( p_stream, id );
535 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
541 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
544 sout_stream_sys_t *p_sys = p_stream->p_sys;
546 if( id->b_transcode )
548 block_t *p_buffer_out;
550 /* Be sure to have padding */
551 p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
552 if( p_buffer == NULL )
556 p_buffer->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
557 memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
559 if( id->f_src.i_cat == AUDIO_ES )
561 transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
564 else if( id->f_src.i_cat == VIDEO_ES )
566 if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
567 &p_buffer_out ) != VLC_SUCCESS )
569 block_Release( p_buffer );
573 block_Release( p_buffer );
577 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
581 else if( id->id != NULL )
583 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
587 block_Release( p_buffer );
592 /****************************************************************************
593 * ffmpeg decoder reencocdr part
594 ****************************************************************************/
600 } fourcc_to_ff_code[] =
603 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
604 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
605 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
606 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
607 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
608 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
609 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
610 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
611 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
614 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
615 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
616 #if LIBAVCODEC_BUILD >= 4676
617 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
619 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
620 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
621 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
622 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
623 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
624 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
625 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
626 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
627 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
628 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
629 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
630 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
631 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
632 #if LIBAVCODEC_BUILD >= 4666
633 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
636 /* raw video code, only used for 'encoding' */
637 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
638 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
639 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
640 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
641 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
642 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
643 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
644 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
645 { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
646 { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
648 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
651 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
655 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
657 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
659 return fourcc_to_ff_code[i].i_ff_codec;
666 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
670 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
671 case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
672 case VLC_FOURCC( 'I', '4', '2', '0' ):
673 return PIX_FMT_YUV420P;
674 case VLC_FOURCC( 'I', '4', '2', '2' ):
675 return PIX_FMT_YUV422P;
676 case VLC_FOURCC( 'I', '4', '4', '4' ):
677 return PIX_FMT_YUV444P;
678 case VLC_FOURCC( 'R', 'V', '1', '5' ):
679 return PIX_FMT_RGB555;
680 case VLC_FOURCC( 'R', 'V', '1', '6' ):
681 return PIX_FMT_RGB565;
682 case VLC_FOURCC( 'R', 'V', '2', '4' ):
683 return PIX_FMT_BGR24;
684 case VLC_FOURCC( 'R', 'V', '3', '2' ):
685 return PIX_FMT_RGBA32;
686 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
687 return PIX_FMT_GRAY8;
688 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
689 return PIX_FMT_YUV422;
695 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
699 case PIX_FMT_YUV420P:
700 return VLC_FOURCC('I','4','2','0');
701 case PIX_FMT_YUV422P:
702 return VLC_FOURCC('I','4','2','2');
703 case PIX_FMT_YUV444P:
704 return VLC_FOURCC('I','4','4','4');
707 return VLC_FOURCC('Y','U','Y','2');
710 return VLC_FOURCC('R','V','1','5');
712 return VLC_FOURCC('R','V','1','6');
714 return VLC_FOURCC('R','V','2','4');
716 return VLC_FOURCC('R','V','3','2');
718 return VLC_FOURCC('G','R','E','Y');
720 case PIX_FMT_YUV410P:
721 case PIX_FMT_YUV411P:
728 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
729 sout_stream_id_t *id )
733 if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
734 id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
735 id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
736 id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
740 id->ff_dec_c = avcodec_alloc_context();
741 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
742 id->ff_dec_c->channels = id->f_src.audio.i_channels;
743 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
744 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
749 i_ff_codec = get_ff_codec( id->f_src.i_codec );
750 if( i_ff_codec == 0 )
752 msg_Err( p_stream, "cannot find decoder id" );
756 id->ff_dec = avcodec_find_decoder( i_ff_codec );
759 msg_Err( p_stream, "cannot find decoder (avcodec)" );
763 id->ff_dec_c = avcodec_alloc_context();
764 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
765 id->ff_dec_c->channels = id->f_src.audio.i_channels;
766 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
767 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
769 id->ff_dec_c->extradata_size = id->f_src.i_extra;
770 id->ff_dec_c->extradata = id->f_src.p_extra;
771 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
773 msg_Err( p_stream, "cannot open decoder" );
778 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
779 id->i_buffer_pos = 0;
780 id->p_buffer = malloc( id->i_buffer );
782 /* Sanity check for audio channels */
783 id->f_dst.audio.i_channels = __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
786 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
788 /* Initialization of encoder format structures */
789 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
790 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
791 id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
792 id->p_encoder->fmt_in.audio.i_physical_channels =
793 id->p_encoder->fmt_in.audio.i_original_channels =
794 pi_channels_maps[id->f_dst.audio.i_channels];
795 id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
797 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
798 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
799 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
801 id->p_encoder->p_module =
802 module_Need( id->p_encoder, "encoder", NULL, 0 );
803 if( !id->p_encoder->p_module )
805 vlc_object_destroy( id->p_encoder );
806 msg_Err( p_stream, "cannot open encoder" );
810 id->b_enc_inited = VLC_FALSE;
812 id->f_dst.audio.i_channels = id->p_encoder->fmt_in.audio.i_channels;
813 id->f_dst.audio.i_rate = id->p_encoder->fmt_in.audio.i_rate;
814 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
815 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
817 /* Hack for mp3 transcoding support */
818 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
820 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
826 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
827 sout_stream_id_t *id )
831 avcodec_close( id->ff_dec_c );
832 free( id->ff_dec_c );
835 module_Unneed( id->p_encoder, id->p_encoder->p_module );
836 vlc_object_destroy( id->p_encoder );
838 free( id->p_buffer );
841 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
842 sout_stream_id_t *id,
846 aout_buffer_t aout_buf;
848 int i_buffer = in->i_buffer;
849 char *p_buffer = in->p_buffer;
850 id->i_dts = in->i_dts;
855 id->i_buffer_pos = 0;
857 /* decode as much data as possible */
862 i_used = avcodec_decode_audio( id->ff_dec_c,
863 (int16_t*)id->p_buffer, &id->i_buffer_pos,
864 p_buffer, i_buffer );
867 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
872 msg_Warn( p_stream, "error audio decoding");
879 if ( id->i_buffer_pos < 0 )
881 msg_Warn( p_stream, "weird error audio decoding");
887 int16_t *sout = (int16_t*)id->p_buffer;
889 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
890 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
892 int8_t *sin = (int8_t*)p_buffer;
893 int i_used = __MIN( id->i_buffer/2, i_buffer );
894 int i_samples = i_used;
896 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
897 while( i_samples > 0 )
899 *sout++ = ( *sin++ ) << 8;
903 while( i_samples > 0 )
905 *sout++ = ( *sin++ - 128 ) << 8;
911 id->i_buffer_pos = i_used * 2;
913 else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
914 id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
916 int16_t *sin = (int16_t*)p_buffer;
917 int i_used = __MIN( id->i_buffer, i_buffer );
918 int i_samples = i_used / 2;
921 memcpy( sout, sin, i_used );
923 #ifdef WORDS_BIGENDIAN
924 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
926 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
929 uint8_t *dat = (uint8_t*)sout;
931 while( i_samples > 0 )
946 id->i_buffer_pos = i_used;
950 if( id->i_buffer_pos == 0 ) continue;
952 /* Encode as much data as possible */
953 if( !id->b_enc_inited && id->p_encoder->pf_header )
955 p_block = id->p_encoder->pf_header( id->p_encoder );
959 block_t *p_prev_block = p_block;
961 p_out = block_New( p_stream, p_block->i_buffer );
962 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
963 p_out->i_dts = p_out->i_pts = in->i_dts;
965 block_ChainAppend( out, p_out );
967 p_block = p_block->p_next;
968 block_Release( p_prev_block );
971 id->b_enc_inited = VLC_TRUE;
974 aout_buf.p_buffer = id->p_buffer;
975 aout_buf.i_nb_bytes = id->i_buffer_pos;
976 aout_buf.i_nb_samples = id->i_buffer_pos / 2 / id->f_src.audio.i_channels;
977 aout_buf.start_date = id->i_dts;
978 aout_buf.end_date = id->i_dts;
980 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
981 id->f_src.audio.i_channels / id->f_src.audio.i_rate );
983 if( id->p_encoder->fmt_in.audio.i_channels == 1 &&
984 id->f_src.audio.i_channels > 1 )
986 int16_t *p_sample = (int16_t *)aout_buf.p_buffer;
987 int i_src_c = id->f_src.audio.i_channels;
990 for( i = 0; i < aout_buf.i_nb_samples; i++ )
994 for( j = 1; j < i_src_c; j++ )
996 c += p_sample[i_src_c * i + j];
998 p_sample[i] = c / (i_src_c-1);
1000 aout_buf.i_nb_bytes = i * 2;
1002 else if( id->p_encoder->fmt_in.audio.i_channels == 2 &&
1003 id->f_src.audio.i_channels > 2 )
1005 int i_src_c = id->f_src.audio.i_channels;
1008 static const float mixf_l[4][6] = /* [i_src_c - 3][channel index] */
1010 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1011 { 0.00, 0.50, 0.50, 0.00, 0.00, 0.00 }, /* 4 channels */
1012 { 0.00, 0.50, 0.00, 0.50, 0.00, 0.00 }, /* 5 channels */
1013 { 0.00, 0.34, 0.33, 0.00, 0.33, 0.00 }, /* 6 channels */
1015 static const float mixf_r[4][6] = /* [i_src_c - 3][channel index] */
1017 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1018 { 0.00, 0.00, 0.50, 0.50, 0.00, 0.00 }, /* 4 channels */
1019 { 0.00, 0.00, 0.50, 0.00, 0.50, 0.00 }, /* 5 channels */
1020 { 0.00, 0.00, 0.33, 0.34, 0.00, 0.33 }, /* 6 channels */
1024 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1026 int16_t *p_src = (int16_t *)aout_buf.p_buffer + i_src_c * i;
1027 int16_t *p_dst = (int16_t *)aout_buf.p_buffer + 2 * i;
1030 float l = 0.0, r = 0.0;
1031 for( j = 0; j < i_src_c; j++ )
1033 l += mixf_l[i_src_c-3][j] * p_src[j];
1034 r += mixf_r[i_src_c-3][j] * p_src[j];
1037 p_dst[0] = (int)( l + 0.5 );
1038 p_dst[1] = (int)( r + 0.5 );
1040 aout_buf.i_nb_bytes = i * 2 * 2;
1042 else if( id->f_src.audio.i_channels !=
1043 id->p_encoder->fmt_in.audio.i_channels )
1048 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
1049 static const int translation[7][6] =
1050 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
1051 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
1052 { 0, 1, 0, 0, 0, 0 }, /* 2 */
1053 { 1, 2, 0, 0, 0, 0 }, /* 3 */
1054 { 1, 3, 2, 0, 0, 0 }, /* 4 */
1055 { 1, 3, 4, 2, 0, 0 }, /* 5 */
1056 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
1058 /* dumb downmixing */
1059 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1061 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
1062 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
1064 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
1065 p_buffer[i*id->f_src.audio.i_channels+
1066 translation[id->f_src.audio.i_channels][j]];
1069 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1072 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1076 block_t *p_prev_block = p_block;
1078 p_out = block_New( p_stream, p_block->i_buffer );
1079 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1080 p_out->i_dts = p_block->i_dts;
1081 p_out->i_pts = p_block->i_pts;
1082 p_out->i_length = p_block->i_length;
1083 block_ChainAppend( out, p_out );
1085 p_block = p_block->p_next;
1086 block_Release( p_prev_block );
1097 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1098 sout_stream_id_t *id )
1100 sout_stream_sys_t *p_sys = p_stream->p_sys;
1105 if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1106 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1107 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1108 id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1109 id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1110 id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1111 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1112 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1113 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1114 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1115 id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1118 id->ff_dec_c = avcodec_alloc_context();
1119 id->ff_dec_c->width = id->f_src.video.i_width;
1120 id->ff_dec_c->height = id->f_src.video.i_height;
1121 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_codec );
1123 #if LIBAVCODEC_BUILD >= 4687
1124 if( id->ff_dec_c->width )
1125 id->ff_dec_c->sample_aspect_ratio =
1126 av_d2q( id->f_src.video.i_aspect / (double)VOUT_ASPECT_FACTOR *
1127 id->ff_dec_c->height / id->ff_dec_c->width, 255 );
1129 id->ff_dec_c->aspect_ratio =
1130 id->f_src.video.i_aspect / (float)VOUT_ASPECT_FACTOR;
1136 i_ff_codec = get_ff_codec( id->f_src.i_codec );
1137 if( i_ff_codec == 0 )
1139 msg_Err( p_stream, "cannot find decoder" );
1140 return VLC_EGENERIC;
1143 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1146 msg_Err( p_stream, "cannot find decoder" );
1147 return VLC_EGENERIC;
1150 id->ff_dec_c = avcodec_alloc_context();
1151 id->ff_dec_c->width = id->f_src.video.i_width;
1152 id->ff_dec_c->height = id->f_src.video.i_height;
1153 id->ff_dec_c->bits_per_sample=id->f_src.video.i_bits_per_pixel;
1154 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1156 if( id->f_src.i_extra > 0 )
1158 if( i_ff_codec == CODEC_ID_SVQ3 )
1160 int i_size = id->f_src.i_extra;
1163 id->ff_dec_c->extradata_size = i_size + 12;
1164 p = id->ff_dec_c->extradata = malloc( i_size + 12 );
1166 memcpy( &p[0], "SVQ3", 4 );
1167 memset( &p[4], 0, 8 );
1168 memcpy( &p[12], id->f_src.p_extra, i_size );
1170 /* Now remove all atoms before the SMI one */
1171 if( id->ff_dec_c->extradata_size > 0x5a && strncmp( &p[0x56], "SMI ", 4 ) )
1173 uint8_t *psz = &p[0x52];
1175 while( psz < &p[id->ff_dec_c->extradata_size - 8] )
1177 int i_size = GetDWBE( psz );
1180 /* FIXME handle 1 as long size */
1183 if( !strncmp( &psz[4], "SMI ", 4 ) )
1185 memmove( &p[0x52], psz, &p[id->ff_dec_c->extradata_size] - psz );
1194 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1195 id->ff_dec_c->extradata = malloc( id->f_src.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
1197 memcpy( id->ff_dec_c->extradata, id->f_src.p_extra, id->f_src.i_extra );
1198 memset( (uint8_t*)id->ff_dec_c->extradata + id->f_src.i_extra, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1201 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1202 id->ff_dec_c->error_resilience= -1;
1203 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1204 id->ff_dec_c->opaque = p_sys;
1206 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1208 msg_Err( p_stream, "cannot open decoder" );
1209 return VLC_EGENERIC;
1214 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1216 /* Initialization of encoder format structures */
1217 es_format_Init( &id->p_encoder->fmt_in,
1218 id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1220 /* The dimensions will be set properly later on.
1221 * Just put sensible values so we can test if there is an encoder. */
1222 id->p_encoder->fmt_in.video.i_width =
1223 id->f_src.video.i_width ? id->f_src.video.i_width : 16;
1224 id->p_encoder->fmt_in.video.i_height =
1225 id->f_src.video.i_height ? id->f_src.video.i_height : 16;
1227 id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1228 id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1231 id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1232 #if LIBAVCODEC_BUILD >= 4662
1233 id->p_encoder->fmt_in.video.i_frame_rate_base =
1234 id->ff_dec_c->frame_rate_base;
1237 #if LIBAVCODEC_BUILD >= 4687
1238 if( id->ff_dec_c->height )
1239 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1240 ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1241 id->ff_dec_c->width / id->ff_dec_c->height );
1243 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1244 id->ff_dec_c->aspect_ratio;
1248 /* Check whether a particular aspect ratio was requested */
1249 if( id->f_src.video.i_aspect )
1251 id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1252 id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1255 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1256 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1257 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1259 id->p_encoder->i_vtolerance = p_sys->i_vtolerance;
1260 id->p_encoder->i_key_int = p_sys->i_key_int;
1261 id->p_encoder->i_b_frames = p_sys->i_b_frames;
1262 id->p_encoder->i_qmin = p_sys->i_qmin;
1263 id->p_encoder->i_qmax = p_sys->i_qmax;
1264 id->p_encoder->i_hq = p_sys->i_hq;
1265 id->p_encoder->b_strict_rc = p_sys->b_strict_rc;
1266 id->p_encoder->b_pre_me = p_sys->b_pre_me;
1267 id->p_encoder->b_hurry_up = p_sys->b_hurry_up;
1268 id->p_encoder->b_interlace = p_sys->b_interlace;
1269 id->p_encoder->i_rc_buffer_size = p_sys->i_rc_buffer_size;
1270 id->p_encoder->f_rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
1271 id->p_encoder->f_i_quant_factor = p_sys->f_i_quant_factor;
1272 id->p_encoder->i_noise_reduction = p_sys->i_noise_reduction;
1273 id->p_encoder->b_mpeg4_matrix = p_sys->b_mpeg4_matrix;
1274 id->p_encoder->i_threads = p_sys->i_threads;
1275 id->p_encoder->b_trellis = p_sys->b_trellis;
1277 id->p_ff_pic = avcodec_alloc_frame();
1278 id->p_ff_pic_tmp0 = NULL;
1279 id->p_ff_pic_tmp1 = NULL;
1280 id->p_ff_pic_tmp2 = NULL;
1281 id->p_vresample = NULL;
1283 id->p_encoder->p_module =
1284 module_Need( id->p_encoder, "encoder", NULL, 0 );
1286 if( !id->p_encoder->p_module )
1288 vlc_object_destroy( id->p_encoder );
1289 msg_Err( p_stream, "cannot find encoder" );
1290 return VLC_EGENERIC;
1293 /* Close the encoder.
1294 * We'll open it only when we have the first frame */
1295 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1296 id->p_encoder->p_module = NULL;
1298 id->b_enc_inited = VLC_FALSE;
1300 if ( p_sys->i_threads >= 1 )
1302 p_sys->id_video = id;
1303 vlc_mutex_init( p_stream, &p_sys->lock_out );
1304 vlc_cond_init( p_stream, &p_sys->cond );
1305 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1306 p_sys->i_first_pic = 0;
1307 p_sys->i_last_pic = 0;
1308 p_sys->p_buffers = NULL;
1309 p_sys->b_die = p_sys->b_error = 0;
1310 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1311 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1313 vlc_object_destroy( id->p_encoder );
1314 msg_Err( p_stream, "cannot spawn encoder thread" );
1315 return VLC_EGENERIC;
1322 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1323 sout_stream_id_t *id )
1325 if ( p_stream->p_sys->i_threads >= 1 )
1327 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1328 p_stream->p_sys->b_die = 1;
1329 vlc_cond_signal( &p_stream->p_sys->cond );
1330 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1331 vlc_thread_join( p_stream->p_sys );
1332 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1333 vlc_cond_destroy( &p_stream->p_sys->cond );
1339 avcodec_close( id->ff_dec_c );
1340 free( id->ff_dec_c );
1344 if( id->p_encoder->p_module )
1345 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1346 vlc_object_destroy( id->p_encoder );
1351 free( id->p_ff_pic );
1354 if( id->p_ff_pic_tmp0 )
1356 free( id->p_ff_pic_tmp0->data[0] );
1357 free( id->p_ff_pic_tmp0 );
1359 if( id->p_ff_pic_tmp1)
1361 free( id->p_ff_pic_tmp1->data[0] );
1362 free( id->p_ff_pic_tmp1 );
1364 if( id->p_ff_pic_tmp2)
1366 free( id->p_ff_pic_tmp2->data[0] );
1367 free( id->p_ff_pic_tmp2 );
1369 if( id->p_vresample )
1371 img_resample_close( id->p_vresample );
1375 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1376 sout_stream_id_t *id, block_t *in, block_t **out )
1378 sout_stream_sys_t *p_sys = p_stream->p_sys;
1388 i_data = in->i_buffer;
1389 p_data = in->p_buffer;
1398 frame = id->p_ff_pic;
1399 p_sys->i_input_pts = in->i_pts;
1400 p_sys->i_input_dts = in->i_dts;
1403 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1410 avpicture_fill( (AVPicture*)frame, p_data,
1411 id->ff_dec_c->pix_fmt,
1412 id->ff_dec_c->width, id->ff_dec_c->height );
1417 frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts :
1420 frame->pict_type = FF_I_TYPE;
1425 msg_Warn( p_stream, "error");
1426 return VLC_EGENERIC;
1436 /* Get the pts of the decoded frame if any, otherwise keep the
1437 * interpolated one */
1438 if( frame->pts != AV_NOPTS_VALUE )
1440 p_sys->i_output_pts = frame->pts;
1443 /* Sanity check (seems to be needed for some streams ) */
1444 if( frame->pict_type == FF_B_TYPE )
1446 p_sys->b_input_has_b_frames = VLC_TRUE;
1449 if( !id->b_enc_inited )
1451 /* Hack because of the copy packetizer which can fail to detect the
1452 * proper size (which forces us to wait until the 1st frame
1454 int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1455 p_sys->i_crop_right;
1456 int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1457 p_sys->i_crop_bottom;
1459 if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1462 /* Apply the scaling */
1463 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1464 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1466 else if( id->f_dst.video.i_width > 0 &&
1467 id->f_dst.video.i_height <= 0 )
1469 id->f_dst.video.i_height =
1470 id->f_dst.video.i_width / (double)i_width * i_height;
1472 else if( id->f_dst.video.i_width <= 0 &&
1473 id->f_dst.video.i_height > 0 )
1475 id->f_dst.video.i_width =
1476 id->f_dst.video.i_height / (double)i_height * i_width;
1479 id->p_encoder->fmt_in.video.i_width =
1480 id->p_encoder->fmt_out.video.i_width =
1481 id->f_dst.video.i_width;
1482 id->p_encoder->fmt_in.video.i_height =
1483 id->p_encoder->fmt_out.video.i_height =
1484 id->f_dst.video.i_height;
1486 id->p_encoder->fmt_out.i_extra = 0;
1487 id->p_encoder->fmt_out.p_extra = NULL;
1489 id->p_encoder->p_module =
1490 module_Need( id->p_encoder, "encoder", NULL, 0 );
1491 if( !id->p_encoder->p_module )
1493 vlc_object_destroy( id->p_encoder );
1494 msg_Err( p_stream, "cannot find encoder" );
1495 id->b_transcode = VLC_FALSE;
1496 return VLC_EGENERIC;
1499 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1500 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1502 /* Hack for mp2v/mp1v transcoding support */
1503 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1504 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1506 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1510 p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1513 msg_Err( p_stream, "cannot add this stream" );
1514 transcode_video_ffmpeg_close( p_stream, id );
1515 id->b_transcode = VLC_FALSE;
1516 return VLC_EGENERIC;
1519 if( id->p_encoder->pf_header )
1521 p_block = id->p_encoder->pf_header( id->p_encoder );
1525 block_t *p_prev_block = p_block;
1527 p_out = block_New( p_stream,
1528 p_block->i_buffer );
1529 memcpy( p_out->p_buffer, p_block->p_buffer,
1531 p_out->i_dts = p_out->i_pts = in->i_dts;
1532 p_out->i_length = 0;
1533 block_ChainAppend( out, p_out );
1535 p_block = p_block->p_next;
1536 block_Release( p_prev_block );
1540 id->i_inter_pixfmt =
1541 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1543 id->b_enc_inited = VLC_TRUE;
1547 if( p_stream->p_sys->b_deinterlace )
1549 if( id->p_ff_pic_tmp0 == NULL )
1553 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1554 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1555 id->ff_dec_c->width, id->ff_dec_c->height );
1557 buf = malloc( i_size );
1559 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1561 id->ff_dec_c->width, id->ff_dec_c->height );
1564 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1565 id->ff_dec_c->pix_fmt,
1566 id->ff_dec_c->width, id->ff_dec_c->height );
1568 #if LIBAVCODEC_BUILD >= 4685
1569 id->p_ff_pic_tmp0->interlaced_frame = 0;
1571 id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1572 frame = id->p_ff_pic_tmp0;
1575 /* convert pix format */
1576 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1578 if( id->p_ff_pic_tmp1 == NULL )
1582 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1583 i_size = avpicture_get_size( id->i_inter_pixfmt,
1584 id->ff_dec_c->width,
1585 id->ff_dec_c->height );
1587 buf = malloc( i_size );
1589 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1591 id->ff_dec_c->width, id->ff_dec_c->height );
1594 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1595 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1596 id->ff_dec_c->width, id->ff_dec_c->height );
1598 id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1599 #if LIBAVCODEC_BUILD >= 4685
1600 id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1601 id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1603 frame = id->p_ff_pic_tmp1;
1606 /* convert size and crop */
1607 if( id->ff_dec_c->width != id->f_dst.video.i_width ||
1608 id->ff_dec_c->height != id->f_dst.video.i_height ||
1609 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1610 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1612 if( id->p_ff_pic_tmp2 == NULL )
1616 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1617 i_size = avpicture_get_size( id->i_inter_pixfmt,
1618 id->f_dst.video.i_width,
1619 id->f_dst.video.i_height );
1621 buf = malloc( i_size );
1623 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1625 id->f_dst.video.i_width,
1626 id->f_dst.video.i_height );
1629 img_resample_full_init( id->f_dst.video.i_width,
1630 id->f_dst.video.i_height,
1631 id->ff_dec_c->width,
1632 id->ff_dec_c->height,
1633 p_stream->p_sys->i_crop_top,
1634 p_stream->p_sys->i_crop_bottom,
1635 p_stream->p_sys->i_crop_left,
1636 p_stream->p_sys->i_crop_right
1637 #if LIBAVCODEC_BUILD >= 4708
1644 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1645 (AVPicture*)frame );
1647 id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1648 #if LIBAVCODEC_BUILD >= 4685
1649 id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1650 id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1652 frame = id->p_ff_pic_tmp2;
1656 p_pic = malloc(sizeof(picture_t));
1657 vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1658 id->p_encoder->fmt_in.i_codec,
1659 id->f_dst.video.i_width, id->f_dst.video.i_height,
1660 id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1661 id->f_dst.video.i_height );
1663 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1665 p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1666 if ( p_sys->i_threads >= 1 )
1668 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines *
1669 p_pic->p[i_plane].i_pitch);
1670 p_stream->p_vlc->pf_memcpy( p_pic->p[i_plane].p_pixels,
1671 frame->data[i_plane], p_pic->p[i_plane].i_lines *
1672 p_pic->p[i_plane].i_pitch );
1676 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1680 /* Set the pts of the frame being encoded */
1681 p_pic->date = p_sys->i_output_pts;
1683 p_pic->i_nb_fields = frame->repeat_pict;
1684 #if LIBAVCODEC_BUILD >= 4685
1685 p_pic->b_progressive = !frame->interlaced_frame;
1686 p_pic->b_top_field_first = frame->top_field_first;
1689 /* Interpolate the next PTS
1690 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1691 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1693 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1694 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1697 if ( p_sys->i_threads >= 1 )
1699 vlc_mutex_lock( &p_sys->lock_out );
1700 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1701 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1702 *out = p_sys->p_buffers;
1703 p_sys->p_buffers = NULL;
1704 vlc_cond_signal( &p_sys->cond );
1705 vlc_mutex_unlock( &p_sys->lock_out );
1710 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1713 block_ChainAppend( out, p_block );
1727 static int EncoderThread( sout_stream_sys_t * p_sys )
1729 sout_stream_id_t * id = p_sys->id_video;
1733 while ( !p_sys->b_die && !p_sys->b_error )
1737 vlc_mutex_lock( &p_sys->lock_out );
1738 while ( p_sys->i_last_pic == p_sys->i_first_pic )
1740 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1741 if ( p_sys->b_die || p_sys->b_error )
1744 if ( p_sys->b_die || p_sys->b_error )
1746 vlc_mutex_unlock( &p_sys->lock_out );
1750 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1751 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1752 vlc_mutex_unlock( &p_sys->lock_out );
1754 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1755 vlc_mutex_lock( &p_sys->lock_out );
1758 block_ChainAppend( &p_sys->p_buffers, p_block );
1760 vlc_mutex_unlock( &p_sys->lock_out );
1762 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1764 free( p_pic->p[i_plane].p_pixels );
1769 while ( p_sys->i_last_pic != p_sys->i_first_pic )
1771 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1772 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1774 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1776 free( p_pic->p[i_plane].p_pixels );
1781 block_ChainRelease( p_sys->p_buffers );
1786 /*****************************************************************************
1787 * transcode_video_ffmpeg_getframebuf:
1789 * Callback used by ffmpeg to get a frame buffer.
1790 * We use it to get the right PTS for each decoded picture.
1791 *****************************************************************************/
1792 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1795 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1798 if( p_sys->i_input_pts )
1800 p_frame->pts = p_sys->i_input_pts;
1802 else if( p_sys->i_input_dts )
1804 /* Some demuxers/packetizers only set the dts so let's try to find a
1805 * useful timestamp from this */
1806 if( !p_context->has_b_frames || !p_sys->b_input_has_b_frames ||
1807 !p_frame->reference || !p_sys->i_output_pts )
1809 p_frame->pts = p_sys->i_input_dts +
1810 /* Hack: ffmpeg encoding doesn't like frames with identical pts */
1811 (p_sys->i_output_pts ? 0 : 50000);
1813 else p_frame->pts = AV_NOPTS_VALUE;
1815 else p_frame->pts = AV_NOPTS_VALUE;
1817 if( p_sys->i_output_pts ) /* make sure 1st frame has a pts > 0 */
1819 p_sys->i_input_pts = 0;
1820 p_sys->i_input_dts = 0;
1823 return avcodec_default_get_buffer( p_context, p_frame );