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 * );
54 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
55 static int Del ( sout_stream_t *, sout_stream_id_t * );
56 static int Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
58 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
59 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
60 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
62 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
63 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
64 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
66 static int transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
68 static int EncoderThread( struct sout_stream_sys_t * p_sys );
70 static int pi_channels_maps[6] =
73 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
74 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
75 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
76 | AOUT_CHAN_REARRIGHT,
77 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
78 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
81 /*****************************************************************************
83 *****************************************************************************/
85 set_description( _("Transcode stream output") );
86 set_capability( "sout stream", 50 );
87 add_shortcut( "transcode" );
88 set_callbacks( Open, Close );
91 #define PICTURE_RING_SIZE 64
93 struct sout_stream_sys_t
97 sout_stream_t * p_out;
98 sout_stream_id_t * id_video;
99 sout_buffer_t * p_buffers;
100 vlc_mutex_t lock_out;
102 picture_t * pp_pics[PICTURE_RING_SIZE];
103 int i_first_pic, i_last_pic;
105 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
110 vlc_fourcc_t i_vcodec; /* " video " " " " */
121 vlc_bool_t b_deinterlace;
122 vlc_bool_t b_interlace;
123 vlc_bool_t b_strict_rc;
125 vlc_bool_t b_hurry_up;
126 int i_rc_buffer_size;
127 float f_rc_buffer_aggressivity;
128 float f_i_quant_factor;
129 int i_noise_reduction;
130 vlc_bool_t b_mpeg4_matrix;
132 vlc_bool_t b_trellis;
141 vlc_bool_t b_input_has_b_frames;
143 mtime_t i_output_pts;
146 /*****************************************************************************
148 *****************************************************************************/
149 static int Open( vlc_object_t *p_this )
151 sout_stream_t *p_stream = (sout_stream_t*)p_this;
152 sout_stream_sys_t *p_sys;
155 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
156 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
159 p_sys->i_vtolerance = -1;
160 p_sys->i_key_int = -1;
163 p_sys->f_i_quant_factor = 0.0;
164 #if LIBAVCODEC_BUILD >= 4673
165 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
167 p_sys->i_hq = VLC_FALSE;
169 p_sys->i_rc_buffer_size = 224*1024*8 * 3/2;
170 p_sys->f_rc_buffer_aggressivity = 0.1;
171 p_sys->i_threads = 0;
172 p_sys->b_trellis = 0;
173 p_sys->b_input_has_b_frames = VLC_FALSE;
175 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "acodec" ) ) )
180 memcpy( fcc, codec, __MIN( strlen( codec ), 4 ) );
182 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
184 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "samplerate" ) ) )
186 p_sys->i_sample_rate = atoi( val );
188 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "channels" ) ) )
190 p_sys->i_channels = atoi( val );
192 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ab" ) ) )
194 p_sys->i_abitrate = atoi( val );
195 if( p_sys->i_abitrate < 4000 )
197 p_sys->i_abitrate *= 1000;
201 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s", fcc,
202 p_sys->i_sample_rate, p_sys->i_channels,
203 p_sys->i_abitrate / 1000 );
206 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "vcodec" ) ) )
211 memcpy( fcc, codec, __MIN( strlen( codec ), 4 ) );
213 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
215 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "scale" ) ) )
217 p_sys->f_scale = atof( val );
219 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "width" ) ) )
221 p_sys->i_width = atoi( val );
223 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "height" ) ) )
225 p_sys->i_height = atoi( val );
227 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
229 p_sys->i_vbitrate = atoi( val );
230 if( p_sys->i_vbitrate < 16000 )
232 p_sys->i_vbitrate *= 1000;
235 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vt" ) ) )
237 p_sys->i_vtolerance = atoi( val );
239 if( sout_cfg_find( p_stream->p_cfg, "deinterlace" ) )
241 p_sys->b_deinterlace = VLC_TRUE;
243 if( sout_cfg_find( p_stream->p_cfg, "interlace" ) )
245 p_sys->b_interlace = VLC_TRUE;
247 if( sout_cfg_find( p_stream->p_cfg, "strict_rc" ) )
249 p_sys->b_strict_rc = VLC_TRUE;
251 if( sout_cfg_find( p_stream->p_cfg, "pre_me" ) )
253 p_sys->b_pre_me = VLC_TRUE;
255 if( sout_cfg_find( p_stream->p_cfg, "hurry_up" ) )
257 p_sys->b_hurry_up = VLC_TRUE;
258 /* hurry up mode needs noise reduction, even small */
259 p_sys->i_noise_reduction = 1;
261 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "rc_buffer_size" ) ) )
263 p_sys->i_rc_buffer_size = atoi( val );
265 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "rc_buffer_aggressivity" ) ) )
267 p_sys->f_rc_buffer_aggressivity = atof( val );
269 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "i_quant_factor" ) ) )
271 p_sys->f_i_quant_factor = atof( val );
273 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "noise_reduction" ) ) )
275 p_sys->i_noise_reduction = atoi( val );
277 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "mpeg4_matrix" ) ) )
279 p_sys->b_mpeg4_matrix = VLC_TRUE;
282 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "croptop" ) ) )
284 p_sys->i_crop_top = atoi( val );
286 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropbottom" ) ) )
288 p_sys->i_crop_bottom = atoi( val );
290 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropleft" ) ) )
292 p_sys->i_crop_left = atoi( val );
294 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropright" ) ) )
296 p_sys->i_crop_right = atoi( val );
298 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "keyint" ) ) )
300 p_sys->i_key_int = atoi( val );
302 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "bframes" ) ) )
304 p_sys->i_b_frames = atoi( val );
306 #if LIBAVCODEC_BUILD >= 4673
307 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "hq" ) ) )
309 if( !strcmp( val, "rd" ) )
311 p_sys->i_hq = FF_MB_DECISION_RD;
313 else if( !strcmp( val, "bits" ) )
315 p_sys->i_hq = FF_MB_DECISION_BITS;
317 else if( !strcmp( val, "simple" ) )
319 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
323 p_sys->i_hq = FF_MB_DECISION_RD;
327 if( sout_cfg_find( p_stream->p_cfg, "hq" ) )
329 p_sys->i_hq = VLC_TRUE;
332 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmin" ) ) )
334 p_sys->i_qmin = atoi( val );
336 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmax" ) ) )
338 p_sys->i_qmax = atoi( val );
340 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "threads" ) ) )
342 p_sys->i_threads = atoi( val );
344 if( sout_cfg_find( p_stream->p_cfg, "trellis" ) )
346 p_sys->b_trellis = VLC_TRUE;
349 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
350 fcc, p_sys->i_width, p_sys->i_height, p_sys->f_scale,
351 p_sys->i_vbitrate / 1000 );
356 msg_Err( p_stream, "cannot create chain" );
361 p_stream->pf_add = Add;
362 p_stream->pf_del = Del;
363 p_stream->pf_send = Send;
364 p_stream->p_sys = p_sys;
367 avcodec_register_all();
369 /* ffmpeg needs some padding at the end of each buffer */
370 p_stream->p_sout->i_padding += FF_INPUT_BUFFER_PADDING_SIZE;
375 /*****************************************************************************
377 *****************************************************************************/
378 static void Close( vlc_object_t * p_this )
380 sout_stream_t *p_stream = (sout_stream_t*)p_this;
381 sout_stream_sys_t *p_sys = p_stream->p_sys;
383 sout_stream_delete( p_sys->p_out );
384 vlc_object_destroy( p_sys );
387 struct sout_stream_id_t
389 vlc_fourcc_t b_transcode;
390 es_format_t f_src; /* only if transcoding */
391 es_format_t f_dst; /* " " " */
392 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
394 /* id of the out stream */
398 encoder_t *p_encoder;
399 vlc_fourcc_t b_enc_inited;
403 AVCodecContext *ff_dec_c;
413 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
414 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
415 AVFrame *p_ff_pic_tmp2; /* to do resample */
417 ImgReSampleContext *p_vresample;
421 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
423 sout_stream_sys_t *p_sys = p_stream->p_sys;
424 sout_stream_id_t *id;
426 id = malloc( sizeof( sout_stream_id_t ) );
429 id->p_encoder = NULL;
431 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
434 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
435 (char*)&p_fmt->i_codec,
436 (char*)&p_sys->i_acodec );
439 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
441 /* create dst format */
442 es_format_Init( &id->f_dst, AUDIO_ES, p_sys->i_acodec );
443 id->f_dst.i_id = id->f_src.i_id;
444 id->f_dst.i_group = id->f_src.i_group;
445 if( id->f_src.psz_language ) id->f_dst.psz_language = strdup( id->f_src.psz_language );
446 id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ? p_sys->i_sample_rate : id->f_src.audio.i_rate;
447 id->f_dst.audio.i_channels = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.audio.i_channels;
448 id->f_dst.i_bitrate = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
449 id->f_dst.audio.i_blockalign = 0;
450 id->f_dst.i_extra = 0;
451 id->f_dst.p_extra = NULL;
453 /* build decoder -> filter -> encoder */
454 if( transcode_audio_ffmpeg_new( p_stream, id ) )
456 msg_Err( p_stream, "cannot create audio chain" );
461 /* open output stream */
462 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
463 id->b_transcode = VLC_TRUE;
471 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
474 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
475 (char*)&p_fmt->i_codec,
476 (char*)&p_sys->i_vcodec );
478 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
480 /* create dst format */
481 es_format_Init( &id->f_dst, VIDEO_ES, p_sys->i_vcodec );
482 id->f_dst.i_id = id->f_src.i_id;
483 id->f_dst.i_group = id->f_src.i_group;
484 if( id->f_src.psz_language ) id->f_dst.psz_language = strdup( id->f_src.psz_language );
485 id->f_dst.video.i_width = p_sys->i_width;
486 id->f_dst.video.i_height= p_sys->i_height;
487 id->f_dst.i_bitrate = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
488 id->f_dst.i_extra = 0;
489 id->f_dst.p_extra = NULL;
491 /* build decoder -> filter -> encoder */
492 if( transcode_video_ffmpeg_new( p_stream, id ) )
494 msg_Err( p_stream, "cannot create video chain" );
499 /* open output stream */
500 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
502 id->b_transcode = VLC_TRUE;
506 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
507 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
508 id->b_transcode = VLC_FALSE;
520 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
522 sout_stream_sys_t *p_sys = p_stream->p_sys;
524 if( id->b_transcode )
526 if( id->f_src.i_cat == AUDIO_ES )
528 transcode_audio_ffmpeg_close( p_stream, id );
530 else if( id->f_src.i_cat == VIDEO_ES )
532 transcode_video_ffmpeg_close( p_stream, id );
536 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
542 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
543 sout_buffer_t *p_buffer )
545 sout_stream_sys_t *p_sys = p_stream->p_sys;
547 if( id->b_transcode )
549 sout_buffer_t *p_buffer_out;
550 if( id->f_src.i_cat == AUDIO_ES )
552 transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
555 else if( id->f_src.i_cat == VIDEO_ES )
557 if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
558 &p_buffer_out ) != VLC_SUCCESS )
560 sout_BufferDelete( p_stream->p_sout, p_buffer );
564 sout_BufferDelete( p_stream->p_sout, p_buffer );
568 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
572 else if( id->id != NULL )
574 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
578 sout_BufferDelete( p_stream->p_sout, p_buffer );
583 /****************************************************************************
584 * ffmpeg decoder reencocdr part
585 ****************************************************************************/
591 } fourcc_to_ff_code[] =
594 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
595 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
596 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
597 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
598 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
599 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
600 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
601 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
602 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
605 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
606 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
607 #if LIBAVCODEC_BUILD >= 4676
608 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
610 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
611 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
612 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
613 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
614 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
615 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
616 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
617 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
618 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
619 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
620 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
621 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
622 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
623 #if LIBAVCODEC_BUILD >= 4666
624 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
627 /* raw video code, only used for 'encoding' */
628 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
629 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
630 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
631 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
632 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
633 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
634 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
635 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
636 { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
637 { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
639 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
642 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
646 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
648 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
650 return fourcc_to_ff_code[i].i_ff_codec;
657 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
661 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
662 case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
663 case VLC_FOURCC( 'I', '4', '2', '0' ):
664 return PIX_FMT_YUV420P;
665 case VLC_FOURCC( 'I', '4', '2', '2' ):
666 return PIX_FMT_YUV422P;
667 case VLC_FOURCC( 'I', '4', '4', '4' ):
668 return PIX_FMT_YUV444P;
669 case VLC_FOURCC( 'R', 'V', '1', '5' ):
670 return PIX_FMT_RGB555;
671 case VLC_FOURCC( 'R', 'V', '1', '6' ):
672 return PIX_FMT_RGB565;
673 case VLC_FOURCC( 'R', 'V', '2', '4' ):
674 return PIX_FMT_BGR24;
675 case VLC_FOURCC( 'R', 'V', '3', '2' ):
676 return PIX_FMT_RGBA32;
677 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
678 return PIX_FMT_GRAY8;
679 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
680 return PIX_FMT_YUV422;
686 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
690 case PIX_FMT_YUV420P:
691 return VLC_FOURCC('I','4','2','0');
692 case PIX_FMT_YUV422P:
693 return VLC_FOURCC('I','4','2','2');
694 case PIX_FMT_YUV444P:
695 return VLC_FOURCC('I','4','4','4');
698 return VLC_FOURCC('Y','U','Y','2');
701 return VLC_FOURCC('R','V','1','5');
703 return VLC_FOURCC('R','V','1','6');
705 return VLC_FOURCC('R','V','2','4');
707 return VLC_FOURCC('R','V','3','2');
709 return VLC_FOURCC('G','R','E','Y');
711 case PIX_FMT_YUV410P:
712 case PIX_FMT_YUV411P:
719 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
720 sout_stream_id_t *id )
724 if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
725 id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
726 id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
727 id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
731 id->ff_dec_c = avcodec_alloc_context();
732 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
733 id->ff_dec_c->channels = id->f_src.audio.i_channels;
734 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
735 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
740 i_ff_codec = get_ff_codec( id->f_src.i_codec );
741 if( i_ff_codec == 0 )
743 msg_Err( p_stream, "cannot find decoder id" );
747 id->ff_dec = avcodec_find_decoder( i_ff_codec );
750 msg_Err( p_stream, "cannot find decoder (avcodec)" );
754 id->ff_dec_c = avcodec_alloc_context();
755 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
756 id->ff_dec_c->channels = id->f_src.audio.i_channels;
757 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
758 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
760 id->ff_dec_c->extradata_size = id->f_src.i_extra;
761 id->ff_dec_c->extradata = id->f_src.p_extra;
762 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
764 msg_Err( p_stream, "cannot open decoder" );
769 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
770 id->i_buffer_pos = 0;
771 id->p_buffer = malloc( id->i_buffer );
773 /* Sanity check for audio channels */
774 id->f_dst.audio.i_channels = __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
777 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
779 /* Initialization of encoder format structures */
780 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
781 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
782 id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
783 id->p_encoder->fmt_in.audio.i_physical_channels =
784 id->p_encoder->fmt_in.audio.i_original_channels =
785 pi_channels_maps[id->f_dst.audio.i_channels];
786 id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
788 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
789 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
790 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
792 id->p_encoder->p_module =
793 module_Need( id->p_encoder, "encoder", NULL, 0 );
794 if( !id->p_encoder->p_module )
796 vlc_object_destroy( id->p_encoder );
797 msg_Err( p_stream, "cannot open encoder" );
801 id->b_enc_inited = VLC_FALSE;
803 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
804 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
806 /* Hack for mp3 transcoding support */
807 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
809 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
815 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
816 sout_stream_id_t *id )
820 avcodec_close( id->ff_dec_c );
821 free( id->ff_dec_c );
824 module_Unneed( id->p_encoder, id->p_encoder->p_module );
825 vlc_object_destroy( id->p_encoder );
827 free( id->p_buffer );
830 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
831 sout_stream_id_t *id,
833 sout_buffer_t **out )
835 aout_buffer_t aout_buf;
837 int i_buffer = in->i_size;
838 char *p_buffer = in->p_buffer;
839 id->i_dts = in->i_dts;
844 id->i_buffer_pos = 0;
846 /* decode as much data as possible */
851 i_used = avcodec_decode_audio( id->ff_dec_c,
852 (int16_t*)id->p_buffer, &id->i_buffer_pos,
853 p_buffer, i_buffer );
856 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
861 msg_Warn( p_stream, "error audio decoding");
868 if ( id->i_buffer_pos < 0 )
870 msg_Warn( p_stream, "weird error audio decoding");
876 int16_t *sout = (int16_t*)id->p_buffer;
878 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
879 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
881 int8_t *sin = (int8_t*)p_buffer;
882 int i_used = __MIN( id->i_buffer/2, i_buffer );
883 int i_samples = i_used;
885 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
886 while( i_samples > 0 )
888 *sout++ = ( *sin++ ) << 8;
892 while( i_samples > 0 )
894 *sout++ = ( *sin++ - 128 ) << 8;
900 id->i_buffer_pos = i_used * 2;
902 else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
903 id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
905 int16_t *sin = (int16_t*)p_buffer;
906 int i_used = __MIN( id->i_buffer, i_buffer );
907 int i_samples = i_used / 2;
910 memcpy( sout, sin, i_used );
912 #ifdef WORDS_BIGENDIAN
913 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
915 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
918 uint8_t *dat = (uint8_t*)sout;
920 while( i_samples > 0 )
935 id->i_buffer_pos = i_used;
939 if( id->i_buffer_pos == 0 ) continue;
941 /* Encode as much data as possible */
942 if( !id->b_enc_inited && id->p_encoder->pf_header )
944 p_block = id->p_encoder->pf_header( id->p_encoder );
947 sout_buffer_t *p_out;
948 block_t *p_prev_block = p_block;
950 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
951 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
952 p_out->i_dts = p_out->i_pts = in->i_dts;
954 sout_BufferChain( out, p_out );
956 p_block = p_block->p_next;
957 block_Release( p_prev_block );
960 id->b_enc_inited = VLC_TRUE;
963 aout_buf.p_buffer = id->p_buffer;
964 aout_buf.i_nb_bytes = id->i_buffer_pos;
965 aout_buf.i_nb_samples = id->i_buffer_pos / 2 / id->f_src.audio.i_channels;
966 aout_buf.start_date = id->i_dts;
967 aout_buf.end_date = id->i_dts;
969 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
970 id->f_src.audio.i_channels / id->f_src.audio.i_rate );
972 if( id->f_src.audio.i_channels !=
973 id->p_encoder->fmt_in.audio.i_channels )
978 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
979 static const int translation[7][6] =
980 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
981 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
982 { 0, 1, 0, 0, 0, 0 }, /* 2 */
983 { 1, 2, 0, 0, 0, 0 }, /* 3 */
984 { 1, 3, 2, 0, 0, 0 }, /* 4 */
985 { 1, 3, 4, 2, 0, 0 }, /* 5 */
986 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
988 /* dumb downmixing */
989 for( i = 0; i < aout_buf.i_nb_samples; i++ )
991 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
992 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
994 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
995 p_buffer[i*id->f_src.audio.i_channels+
996 translation[id->f_src.audio.i_channels][j]];
999 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1002 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1005 sout_buffer_t *p_out;
1006 block_t *p_prev_block = p_block;
1008 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1009 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1010 p_out->i_dts = p_block->i_dts;
1011 p_out->i_pts = p_block->i_pts;
1012 p_out->i_length = p_block->i_length;
1013 sout_BufferChain( out, p_out );
1015 p_block = p_block->p_next;
1016 block_Release( p_prev_block );
1027 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1028 sout_stream_id_t *id )
1030 sout_stream_sys_t *p_sys = p_stream->p_sys;
1035 if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1036 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1037 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1038 id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1039 id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1040 id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1041 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1042 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1043 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1044 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1045 id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1048 id->ff_dec_c = avcodec_alloc_context();
1049 id->ff_dec_c->width = id->f_src.video.i_width;
1050 id->ff_dec_c->height = id->f_src.video.i_height;
1051 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_codec );
1053 #if LIBAVCODEC_BUILD >= 4687
1054 if( id->ff_dec_c->width )
1055 id->ff_dec_c->sample_aspect_ratio =
1056 av_d2q( id->f_src.video.i_aspect / (double)VOUT_ASPECT_FACTOR *
1057 id->ff_dec_c->height / id->ff_dec_c->width, 255 );
1059 id->ff_dec_c->aspect_ratio =
1060 id->f_src.video.i_aspect / (float)VOUT_ASPECT_FACTOR;
1066 i_ff_codec = get_ff_codec( id->f_src.i_codec );
1067 if( i_ff_codec == 0 )
1069 msg_Err( p_stream, "cannot find decoder" );
1070 return VLC_EGENERIC;
1073 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1076 msg_Err( p_stream, "cannot find decoder" );
1077 return VLC_EGENERIC;
1080 id->ff_dec_c = avcodec_alloc_context();
1081 id->ff_dec_c->width = id->f_src.video.i_width;
1082 id->ff_dec_c->height = id->f_src.video.i_height;
1083 id->ff_dec_c->bits_per_sample=id->f_src.video.i_bits_per_pixel;
1084 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1086 if( id->f_src.i_extra > 0 )
1088 if( i_ff_codec == CODEC_ID_SVQ3 )
1090 int i_size = id->f_src.i_extra;
1093 id->ff_dec_c->extradata_size = i_size + 12;
1094 p = id->ff_dec_c->extradata = malloc( i_size + 12 );
1096 memcpy( &p[0], "SVQ3", 4 );
1097 memset( &p[4], 0, 8 );
1098 memcpy( &p[12], id->f_src.p_extra, i_size );
1100 /* Now remove all atoms before the SMI one */
1101 if( id->ff_dec_c->extradata_size > 0x5a && strncmp( &p[0x56], "SMI ", 4 ) )
1103 uint8_t *psz = &p[0x52];
1105 while( psz < &p[id->ff_dec_c->extradata_size - 8] )
1107 int i_size = GetDWBE( psz );
1110 /* FIXME handle 1 as long size */
1113 if( !strncmp( &psz[4], "SMI ", 4 ) )
1115 memmove( &p[0x52], psz, &p[id->ff_dec_c->extradata_size] - psz );
1124 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1125 id->ff_dec_c->extradata = malloc( id->f_src.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
1127 memcpy( id->ff_dec_c->extradata, id->f_src.p_extra, id->f_src.i_extra );
1128 memset( (uint8_t*)id->ff_dec_c->extradata + id->f_src.i_extra, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1131 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1132 id->ff_dec_c->error_resilience= -1;
1133 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1134 id->ff_dec_c->opaque = p_sys;
1136 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1138 msg_Err( p_stream, "cannot open decoder" );
1139 return VLC_EGENERIC;
1142 if( i_ff_codec == CODEC_ID_MPEG4 && id->ff_dec_c->extradata_size > 0 )
1146 uint8_t *p_vol = malloc( id->ff_dec_c->extradata_size +
1147 FF_INPUT_BUFFER_PADDING_SIZE );
1149 memcpy( p_vol, id->ff_dec_c->extradata,
1150 id->ff_dec_c->extradata_size );
1151 memset( p_vol + id->ff_dec_c->extradata_size, 0,
1152 FF_INPUT_BUFFER_PADDING_SIZE );
1154 avcodec_decode_video( id->ff_dec_c, &frame, &b_gotpicture,
1155 id->ff_dec_c->extradata,
1156 id->ff_dec_c->extradata_size );
1163 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1165 /* Initialization of encoder format structures */
1166 es_format_Init( &id->p_encoder->fmt_in,
1167 id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1169 /* The dimensions will be set properly later on.
1170 * Just put sensible values so we can test if there is an encoder. */
1171 id->p_encoder->fmt_in.video.i_width = 16;
1172 id->p_encoder->fmt_in.video.i_height = 16;
1174 id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1175 id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1178 id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1179 #if LIBAVCODEC_BUILD >= 4662
1180 id->p_encoder->fmt_in.video.i_frame_rate_base =
1181 id->ff_dec_c->frame_rate_base;
1184 #if LIBAVCODEC_BUILD >= 4687
1185 if( id->ff_dec_c->height )
1186 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1187 ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1188 id->ff_dec_c->width / id->ff_dec_c->height );
1190 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1191 id->ff_dec_c->aspect_ratio;
1195 /* Check whether a particular aspect ratio was requested */
1196 if( id->f_src.video.i_aspect )
1198 id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1199 id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1202 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1203 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1204 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1206 id->p_encoder->i_vtolerance = p_sys->i_vtolerance;
1207 id->p_encoder->i_key_int = p_sys->i_key_int;
1208 id->p_encoder->i_b_frames = p_sys->i_b_frames;
1209 id->p_encoder->i_qmin = p_sys->i_qmin;
1210 id->p_encoder->i_qmax = p_sys->i_qmax;
1211 id->p_encoder->i_hq = p_sys->i_hq;
1212 id->p_encoder->b_strict_rc = p_sys->b_strict_rc;
1213 id->p_encoder->b_pre_me = p_sys->b_pre_me;
1214 id->p_encoder->b_hurry_up = p_sys->b_hurry_up;
1215 id->p_encoder->b_interlace = p_sys->b_interlace;
1216 id->p_encoder->i_rc_buffer_size = p_sys->i_rc_buffer_size;
1217 id->p_encoder->f_rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
1218 id->p_encoder->f_i_quant_factor = p_sys->f_i_quant_factor;
1219 id->p_encoder->i_noise_reduction = p_sys->i_noise_reduction;
1220 id->p_encoder->b_mpeg4_matrix = p_sys->b_mpeg4_matrix;
1221 id->p_encoder->i_threads = p_sys->i_threads;
1222 id->p_encoder->b_trellis = p_sys->b_trellis;
1224 id->p_ff_pic = avcodec_alloc_frame();
1225 id->p_ff_pic_tmp0 = NULL;
1226 id->p_ff_pic_tmp1 = NULL;
1227 id->p_ff_pic_tmp2 = NULL;
1228 id->p_vresample = NULL;
1230 id->p_encoder->p_module =
1231 module_Need( id->p_encoder, "encoder", NULL, 0 );
1233 if( !id->p_encoder->p_module )
1235 vlc_object_destroy( id->p_encoder );
1236 msg_Err( p_stream, "cannot find encoder" );
1237 return VLC_EGENERIC;
1240 /* Close the encoder.
1241 * We'll open it only when we have the first frame */
1242 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1243 id->p_encoder->p_module = NULL;
1245 id->b_enc_inited = VLC_FALSE;
1247 if ( p_sys->i_threads >= 1 )
1249 p_sys->id_video = id;
1250 vlc_mutex_init( p_stream, &p_sys->lock_out );
1251 vlc_cond_init( p_stream, &p_sys->cond );
1252 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1253 p_sys->i_first_pic = 0;
1254 p_sys->i_last_pic = 0;
1255 p_sys->p_buffers = NULL;
1256 p_sys->b_die = p_sys->b_error = 0;
1257 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1258 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1260 vlc_object_destroy( id->p_encoder );
1261 msg_Err( p_stream, "cannot spawn encoder thread" );
1262 return VLC_EGENERIC;
1269 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1270 sout_stream_id_t *id )
1272 if ( p_stream->p_sys->i_threads >= 1 )
1274 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1275 p_stream->p_sys->b_die = 1;
1276 vlc_cond_signal( &p_stream->p_sys->cond );
1277 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1278 vlc_thread_join( p_stream->p_sys );
1279 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1280 vlc_cond_destroy( &p_stream->p_sys->cond );
1286 avcodec_close( id->ff_dec_c );
1287 free( id->ff_dec_c );
1291 if( id->p_encoder->p_module )
1292 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1293 vlc_object_destroy( id->p_encoder );
1298 free( id->p_ff_pic );
1301 if( id->p_ff_pic_tmp0 )
1303 free( id->p_ff_pic_tmp0->data[0] );
1304 free( id->p_ff_pic_tmp0 );
1306 if( id->p_ff_pic_tmp1)
1308 free( id->p_ff_pic_tmp1->data[0] );
1309 free( id->p_ff_pic_tmp1 );
1311 if( id->p_ff_pic_tmp2)
1313 free( id->p_ff_pic_tmp2->data[0] );
1314 free( id->p_ff_pic_tmp2 );
1316 if( id->p_vresample )
1318 img_resample_close( id->p_vresample );
1322 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1323 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1325 sout_stream_sys_t *p_sys = p_stream->p_sys;
1335 i_data = in->i_size;
1336 p_data = in->p_buffer;
1345 frame = id->p_ff_pic;
1346 p_sys->i_input_pts = in->i_pts;
1347 p_sys->i_input_dts = in->i_dts;
1350 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1357 avpicture_fill( (AVPicture*)frame, p_data,
1358 id->ff_dec_c->pix_fmt,
1359 id->ff_dec_c->width, id->ff_dec_c->height );
1364 frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts :
1367 frame->pict_type = FF_I_TYPE;
1372 msg_Warn( p_stream, "error");
1373 return VLC_EGENERIC;
1383 /* Get the pts of the decoded frame if any, otherwise keep the
1384 * interpolated one */
1385 if( frame->pts != AV_NOPTS_VALUE )
1387 p_sys->i_output_pts = frame->pts;
1390 /* Sanity check (seems to be needed for some streams ) */
1391 if( frame->pict_type == FF_B_TYPE )
1393 p_sys->b_input_has_b_frames = VLC_TRUE;
1396 if( !id->b_enc_inited )
1398 /* Hack because of the copy packetizer which can fail to detect the
1399 * proper size (which forces us to wait until the 1st frame
1401 int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1402 p_sys->i_crop_right;
1403 int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1404 p_sys->i_crop_bottom;
1406 if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1409 /* Apply the scaling */
1410 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1411 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1413 else if( id->f_dst.video.i_width > 0 &&
1414 id->f_dst.video.i_height <= 0 )
1416 id->f_dst.video.i_height =
1417 id->f_dst.video.i_width / (double)i_width * i_height;
1419 else if( id->f_dst.video.i_width <= 0 &&
1420 id->f_dst.video.i_height > 0 )
1422 id->f_dst.video.i_width =
1423 id->f_dst.video.i_height / (double)i_height * i_width;
1426 id->p_encoder->fmt_in.video.i_width =
1427 id->p_encoder->fmt_out.video.i_width =
1428 id->f_dst.video.i_width;
1429 id->p_encoder->fmt_in.video.i_height =
1430 id->p_encoder->fmt_out.video.i_height =
1431 id->f_dst.video.i_height;
1433 id->p_encoder->fmt_out.i_extra = 0;
1434 id->p_encoder->fmt_out.p_extra = NULL;
1436 id->p_encoder->p_module =
1437 module_Need( id->p_encoder, "encoder", NULL, 0 );
1438 if( !id->p_encoder->p_module )
1440 vlc_object_destroy( id->p_encoder );
1441 msg_Err( p_stream, "cannot find encoder" );
1442 id->b_transcode = VLC_FALSE;
1443 return VLC_EGENERIC;
1446 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1447 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1449 /* Hack for mp2v/mp1v transcoding support */
1450 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1451 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1453 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1457 p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1460 msg_Err( p_stream, "cannot add this stream" );
1461 transcode_video_ffmpeg_close( p_stream, id );
1462 id->b_transcode = VLC_FALSE;
1463 return VLC_EGENERIC;
1466 if( id->p_encoder->pf_header )
1468 p_block = id->p_encoder->pf_header( id->p_encoder );
1471 sout_buffer_t *p_out;
1472 block_t *p_prev_block = p_block;
1474 p_out = sout_BufferNew( p_stream->p_sout,
1475 p_block->i_buffer );
1476 memcpy( p_out->p_buffer, p_block->p_buffer,
1478 p_out->i_dts = p_out->i_pts = in->i_dts;
1479 p_out->i_length = 0;
1480 sout_BufferChain( out, p_out );
1482 p_block = p_block->p_next;
1483 block_Release( p_prev_block );
1487 id->i_inter_pixfmt =
1488 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1490 id->b_enc_inited = VLC_TRUE;
1494 if( p_stream->p_sys->b_deinterlace )
1496 if( id->p_ff_pic_tmp0 == NULL )
1500 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1501 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1502 id->ff_dec_c->width, id->ff_dec_c->height );
1504 buf = malloc( i_size );
1506 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1508 id->ff_dec_c->width, id->ff_dec_c->height );
1511 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1512 id->ff_dec_c->pix_fmt,
1513 id->ff_dec_c->width, id->ff_dec_c->height );
1515 #if LIBAVCODEC_BUILD >= 4685
1516 id->p_ff_pic_tmp0->interlaced_frame = 0;
1518 id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1519 frame = id->p_ff_pic_tmp0;
1522 /* convert pix format */
1523 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1525 if( id->p_ff_pic_tmp1 == NULL )
1529 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1530 i_size = avpicture_get_size( id->i_inter_pixfmt,
1531 id->ff_dec_c->width,
1532 id->ff_dec_c->height );
1534 buf = malloc( i_size );
1536 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1538 id->ff_dec_c->width, id->ff_dec_c->height );
1541 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1542 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1543 id->ff_dec_c->width, id->ff_dec_c->height );
1545 id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1546 #if LIBAVCODEC_BUILD >= 4685
1547 id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1548 id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1550 frame = id->p_ff_pic_tmp1;
1553 /* convert size and crop */
1554 if( id->ff_dec_c->width != id->f_dst.video.i_width ||
1555 id->ff_dec_c->height != id->f_dst.video.i_height ||
1556 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1557 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1559 if( id->p_ff_pic_tmp2 == NULL )
1563 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1564 i_size = avpicture_get_size( id->i_inter_pixfmt,
1565 id->f_dst.video.i_width,
1566 id->f_dst.video.i_height );
1568 buf = malloc( i_size );
1570 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1572 id->f_dst.video.i_width, id->f_dst.video.i_height );
1575 img_resample_full_init( id->f_dst.video.i_width,
1576 id->f_dst.video.i_height,
1577 id->ff_dec_c->width, id->ff_dec_c->height,
1578 p_stream->p_sys->i_crop_top,
1579 p_stream->p_sys->i_crop_bottom,
1580 p_stream->p_sys->i_crop_left,
1581 p_stream->p_sys->i_crop_right );
1584 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1585 (AVPicture*)frame );
1587 id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1588 #if LIBAVCODEC_BUILD >= 4685
1589 id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1590 id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1592 frame = id->p_ff_pic_tmp2;
1596 p_pic = malloc(sizeof(picture_t));
1597 vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1598 id->p_encoder->fmt_in.i_codec,
1599 id->f_dst.video.i_width, id->f_dst.video.i_height,
1600 id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1601 id->f_dst.video.i_height );
1603 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1605 p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1606 if ( p_sys->i_threads >= 1 )
1608 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines *
1609 p_pic->p[i_plane].i_pitch);
1610 p_stream->p_vlc->pf_memcpy( p_pic->p[i_plane].p_pixels,
1611 frame->data[i_plane], p_pic->p[i_plane].i_lines *
1612 p_pic->p[i_plane].i_pitch );
1616 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1620 /* Set the pts of the frame being encoded */
1621 p_pic->date = p_sys->i_output_pts;
1623 p_pic->i_nb_fields = frame->repeat_pict;
1624 #if LIBAVCODEC_BUILD >= 4685
1625 p_pic->b_progressive = !frame->interlaced_frame;
1626 p_pic->b_top_field_first = frame->top_field_first;
1629 /* Interpolate the next PTS
1630 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1631 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1633 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1634 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1637 if ( p_sys->i_threads >= 1 )
1639 vlc_mutex_lock( &p_sys->lock_out );
1640 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1641 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1642 *out = p_sys->p_buffers;
1643 p_sys->p_buffers = NULL;
1644 vlc_cond_signal( &p_sys->cond );
1645 vlc_mutex_unlock( &p_sys->lock_out );
1650 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1653 sout_buffer_t *p_out;
1654 block_t *p_prev_block = p_block;
1656 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1657 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1658 p_out->i_dts = p_block->i_dts;
1659 p_out->i_pts = p_block->i_pts;
1660 p_out->i_length = p_block->i_length;
1662 (p_block->i_flags << SOUT_BUFFER_FLAGS_BLOCK_SHIFT)
1663 & SOUT_BUFFER_FLAGS_BLOCK_MASK;
1664 sout_BufferChain( out, p_out );
1666 p_block = p_block->p_next;
1667 block_Release( p_prev_block );
1681 static int EncoderThread( sout_stream_sys_t * p_sys )
1683 sout_stream_t * p_stream = p_sys->p_out;
1684 sout_stream_id_t * id = p_sys->id_video;
1687 sout_buffer_t * p_buffer;
1689 while ( !p_sys->b_die && !p_sys->b_error )
1693 vlc_mutex_lock( &p_sys->lock_out );
1694 while ( p_sys->i_last_pic == p_sys->i_first_pic )
1696 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1697 if ( p_sys->b_die || p_sys->b_error )
1700 if ( p_sys->b_die || p_sys->b_error )
1702 vlc_mutex_unlock( &p_sys->lock_out );
1706 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1707 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1708 vlc_mutex_unlock( &p_sys->lock_out );
1710 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1711 vlc_mutex_lock( &p_sys->lock_out );
1714 sout_buffer_t *p_out;
1715 block_t *p_prev_block = p_block;
1717 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1718 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1719 p_out->i_dts = p_block->i_dts;
1720 p_out->i_pts = p_block->i_pts;
1721 p_out->i_length = p_block->i_length;
1723 (p_block->i_flags << SOUT_BUFFER_FLAGS_BLOCK_SHIFT)
1724 & SOUT_BUFFER_FLAGS_BLOCK_MASK;
1725 sout_BufferChain( &p_sys->p_buffers, p_out );
1727 p_block = p_block->p_next;
1728 block_Release( p_prev_block );
1730 vlc_mutex_unlock( &p_sys->lock_out );
1732 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1734 free( p_pic->p[i_plane].p_pixels );
1739 while ( p_sys->i_last_pic != p_sys->i_first_pic )
1741 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1742 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1744 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1746 free( p_pic->p[i_plane].p_pixels );
1751 p_buffer = p_sys->p_buffers;
1752 while ( p_buffer != NULL )
1754 sout_buffer_t * p_next = p_buffer->p_next;
1755 sout_BufferDelete( p_stream->p_sout, p_buffer );
1762 /*****************************************************************************
1763 * transcode_video_ffmpeg_getframebuf:
1765 * Callback used by ffmpeg to get a frame buffer.
1766 * We use it to get the right PTS for each decoded picture.
1767 *****************************************************************************/
1768 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1771 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1774 if( p_sys->i_input_pts )
1776 p_frame->pts = p_sys->i_input_pts;
1778 else if( p_sys->i_input_dts )
1780 /* Some demuxers/packetizers only set the dts so let's try to find a
1781 * useful timestamp from this */
1782 if( !p_context->has_b_frames || !p_sys->b_input_has_b_frames ||
1783 !p_frame->reference )
1785 p_frame->pts = p_sys->i_input_dts;
1787 else p_frame->pts = AV_NOPTS_VALUE;
1789 else p_frame->pts = AV_NOPTS_VALUE;
1791 p_sys->i_input_pts = 0;
1792 p_sys->i_input_dts = 0;
1794 return avcodec_default_get_buffer( p_context, p_frame );