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 },
634 { VLC_FOURCC( 'h', '2', '6', '4' ), CODEC_ID_H264 },
637 /* raw video code, only used for 'encoding' */
638 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
639 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
640 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
641 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
642 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
643 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
644 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
645 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
646 { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
647 { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
649 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
652 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
656 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
658 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
660 return fourcc_to_ff_code[i].i_ff_codec;
667 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
671 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
672 case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
673 case VLC_FOURCC( 'I', '4', '2', '0' ):
674 return PIX_FMT_YUV420P;
675 case VLC_FOURCC( 'I', '4', '2', '2' ):
676 return PIX_FMT_YUV422P;
677 case VLC_FOURCC( 'I', '4', '4', '4' ):
678 return PIX_FMT_YUV444P;
679 case VLC_FOURCC( 'R', 'V', '1', '5' ):
680 return PIX_FMT_RGB555;
681 case VLC_FOURCC( 'R', 'V', '1', '6' ):
682 return PIX_FMT_RGB565;
683 case VLC_FOURCC( 'R', 'V', '2', '4' ):
684 return PIX_FMT_BGR24;
685 case VLC_FOURCC( 'R', 'V', '3', '2' ):
686 return PIX_FMT_RGBA32;
687 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
688 return PIX_FMT_GRAY8;
689 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
690 return PIX_FMT_YUV422;
696 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
700 case PIX_FMT_YUV420P:
701 return VLC_FOURCC('I','4','2','0');
702 case PIX_FMT_YUV422P:
703 return VLC_FOURCC('I','4','2','2');
704 case PIX_FMT_YUV444P:
705 return VLC_FOURCC('I','4','4','4');
708 return VLC_FOURCC('Y','U','Y','2');
711 return VLC_FOURCC('R','V','1','5');
713 return VLC_FOURCC('R','V','1','6');
715 return VLC_FOURCC('R','V','2','4');
717 return VLC_FOURCC('R','V','3','2');
719 return VLC_FOURCC('G','R','E','Y');
721 case PIX_FMT_YUV410P:
722 case PIX_FMT_YUV411P:
729 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
730 sout_stream_id_t *id )
734 if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
735 id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
736 id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
737 id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
741 id->ff_dec_c = avcodec_alloc_context();
742 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
743 id->ff_dec_c->channels = id->f_src.audio.i_channels;
744 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
745 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
750 i_ff_codec = get_ff_codec( id->f_src.i_codec );
751 if( i_ff_codec == 0 )
753 msg_Err( p_stream, "cannot find decoder id" );
757 id->ff_dec = avcodec_find_decoder( i_ff_codec );
760 msg_Err( p_stream, "cannot find decoder (avcodec)" );
764 id->ff_dec_c = avcodec_alloc_context();
765 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
766 id->ff_dec_c->channels = id->f_src.audio.i_channels;
767 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
768 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
770 id->ff_dec_c->extradata_size = id->f_src.i_extra;
771 id->ff_dec_c->extradata = id->f_src.p_extra;
772 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
774 msg_Err( p_stream, "cannot open decoder" );
779 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
780 id->i_buffer_pos = 0;
781 id->p_buffer = malloc( id->i_buffer );
783 /* Sanity check for audio channels */
784 id->f_dst.audio.i_channels = __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
787 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
789 /* Initialization of encoder format structures */
790 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
791 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
792 id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
793 id->p_encoder->fmt_in.audio.i_physical_channels =
794 id->p_encoder->fmt_in.audio.i_original_channels =
795 pi_channels_maps[id->f_dst.audio.i_channels];
796 id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
798 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
799 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
800 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
802 id->p_encoder->p_module =
803 module_Need( id->p_encoder, "encoder", NULL, 0 );
804 if( !id->p_encoder->p_module )
806 vlc_object_destroy( id->p_encoder );
807 msg_Err( p_stream, "cannot open encoder" );
811 id->b_enc_inited = VLC_FALSE;
813 id->f_dst.audio.i_channels = id->p_encoder->fmt_in.audio.i_channels;
814 id->f_dst.audio.i_rate = id->p_encoder->fmt_in.audio.i_rate;
815 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
816 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
818 /* Hack for mp3 transcoding support */
819 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
821 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
827 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
828 sout_stream_id_t *id )
830 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
831 av_free( id->ff_dec_c );
833 module_Unneed( id->p_encoder, id->p_encoder->p_module );
834 vlc_object_destroy( id->p_encoder );
836 free( id->p_buffer );
839 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
840 sout_stream_id_t *id,
844 aout_buffer_t aout_buf;
846 int i_buffer = in->i_buffer;
847 char *p_buffer = in->p_buffer;
848 id->i_dts = in->i_dts;
853 id->i_buffer_pos = 0;
855 /* decode as much data as possible */
860 i_used = avcodec_decode_audio( id->ff_dec_c,
861 (int16_t*)id->p_buffer, &id->i_buffer_pos,
862 p_buffer, i_buffer );
865 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
870 msg_Warn( p_stream, "error audio decoding");
877 if ( id->i_buffer_pos < 0 )
879 msg_Warn( p_stream, "weird error audio decoding");
885 int16_t *sout = (int16_t*)id->p_buffer;
887 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
888 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
890 int8_t *sin = (int8_t*)p_buffer;
891 int i_used = __MIN( id->i_buffer/2, i_buffer );
892 int i_samples = i_used;
894 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
895 while( i_samples > 0 )
897 *sout++ = ( *sin++ ) << 8;
901 while( i_samples > 0 )
903 *sout++ = ( *sin++ - 128 ) << 8;
909 id->i_buffer_pos = i_used * 2;
911 else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
912 id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
914 int16_t *sin = (int16_t*)p_buffer;
915 int i_used = __MIN( id->i_buffer, i_buffer );
916 int i_samples = i_used / 2;
919 memcpy( sout, sin, i_used );
921 #ifdef WORDS_BIGENDIAN
922 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
924 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
927 uint8_t *dat = (uint8_t*)sout;
929 while( i_samples > 0 )
944 id->i_buffer_pos = i_used;
948 if( id->i_buffer_pos == 0 ) continue;
950 /* Encode as much data as possible */
951 if( !id->b_enc_inited && id->p_encoder->pf_header )
953 p_block = id->p_encoder->pf_header( id->p_encoder );
957 block_t *p_prev_block = p_block;
959 p_out = block_New( p_stream, p_block->i_buffer );
960 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
961 p_out->i_dts = p_out->i_pts = in->i_dts;
963 block_ChainAppend( out, p_out );
965 p_block = p_block->p_next;
966 block_Release( p_prev_block );
969 id->b_enc_inited = VLC_TRUE;
972 aout_buf.p_buffer = id->p_buffer;
973 aout_buf.i_nb_bytes = id->i_buffer_pos;
974 aout_buf.i_nb_samples = id->i_buffer_pos / 2 / id->f_src.audio.i_channels;
975 aout_buf.start_date = id->i_dts;
976 aout_buf.end_date = id->i_dts;
978 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
979 id->f_src.audio.i_channels / id->f_src.audio.i_rate );
981 if( id->p_encoder->fmt_in.audio.i_channels == 1 &&
982 id->f_src.audio.i_channels > 1 )
984 int16_t *p_sample = (int16_t *)aout_buf.p_buffer;
985 int i_src_c = id->f_src.audio.i_channels;
988 for( i = 0; i < aout_buf.i_nb_samples; i++ )
992 for( j = 1; j < i_src_c; j++ )
994 c += p_sample[i_src_c * i + j];
996 p_sample[i] = c / (i_src_c-1);
998 aout_buf.i_nb_bytes = i * 2;
1000 else if( id->p_encoder->fmt_in.audio.i_channels == 2 &&
1001 id->f_src.audio.i_channels > 2 )
1003 int i_src_c = id->f_src.audio.i_channels;
1006 static const float mixf_l[4][6] = /* [i_src_c - 3][channel index] */
1008 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1009 { 0.00, 0.50, 0.50, 0.00, 0.00, 0.00 }, /* 4 channels */
1010 { 0.00, 0.50, 0.00, 0.50, 0.00, 0.00 }, /* 5 channels */
1011 { 0.00, 0.34, 0.33, 0.00, 0.33, 0.00 }, /* 6 channels */
1013 static const float mixf_r[4][6] = /* [i_src_c - 3][channel index] */
1015 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1016 { 0.00, 0.00, 0.50, 0.50, 0.00, 0.00 }, /* 4 channels */
1017 { 0.00, 0.00, 0.50, 0.00, 0.50, 0.00 }, /* 5 channels */
1018 { 0.00, 0.00, 0.33, 0.34, 0.00, 0.33 }, /* 6 channels */
1022 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1024 int16_t *p_src = (int16_t *)aout_buf.p_buffer + i_src_c * i;
1025 int16_t *p_dst = (int16_t *)aout_buf.p_buffer + 2 * i;
1028 float l = 0.0, r = 0.0;
1029 for( j = 0; j < i_src_c; j++ )
1031 l += mixf_l[i_src_c-3][j] * p_src[j];
1032 r += mixf_r[i_src_c-3][j] * p_src[j];
1035 p_dst[0] = (int)( l + 0.5 );
1036 p_dst[1] = (int)( r + 0.5 );
1038 aout_buf.i_nb_bytes = i * 2 * 2;
1040 else if( id->f_src.audio.i_channels !=
1041 id->p_encoder->fmt_in.audio.i_channels )
1046 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
1047 static const int translation[7][6] =
1048 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
1049 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
1050 { 0, 1, 0, 0, 0, 0 }, /* 2 */
1051 { 1, 2, 0, 0, 0, 0 }, /* 3 */
1052 { 1, 3, 2, 0, 0, 0 }, /* 4 */
1053 { 1, 3, 4, 2, 0, 0 }, /* 5 */
1054 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
1056 /* dumb downmixing */
1057 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1059 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
1060 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
1062 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
1063 p_buffer[i*id->f_src.audio.i_channels+
1064 translation[id->f_src.audio.i_channels][j]];
1067 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1070 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1074 block_t *p_prev_block = p_block;
1076 p_out = block_New( p_stream, p_block->i_buffer );
1077 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1078 p_out->i_dts = p_block->i_dts;
1079 p_out->i_pts = p_block->i_pts;
1080 p_out->i_length = p_block->i_length;
1081 block_ChainAppend( out, p_out );
1083 p_block = p_block->p_next;
1084 block_Release( p_prev_block );
1095 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1096 sout_stream_id_t *id )
1098 sout_stream_sys_t *p_sys = p_stream->p_sys;
1103 if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1104 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1105 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1106 id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1107 id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1108 id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1109 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1110 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1111 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1112 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1113 id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1116 id->ff_dec_c = avcodec_alloc_context();
1117 id->ff_dec_c->width = id->f_src.video.i_width;
1118 id->ff_dec_c->height = id->f_src.video.i_height;
1119 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_codec );
1121 #if LIBAVCODEC_BUILD >= 4687
1122 if( id->ff_dec_c->width )
1123 id->ff_dec_c->sample_aspect_ratio =
1124 av_d2q( id->f_src.video.i_aspect / (double)VOUT_ASPECT_FACTOR *
1125 id->ff_dec_c->height / id->ff_dec_c->width, 255 );
1127 id->ff_dec_c->aspect_ratio =
1128 id->f_src.video.i_aspect / (float)VOUT_ASPECT_FACTOR;
1134 i_ff_codec = get_ff_codec( id->f_src.i_codec );
1135 if( i_ff_codec == 0 )
1137 msg_Err( p_stream, "cannot find decoder" );
1138 return VLC_EGENERIC;
1141 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1144 msg_Err( p_stream, "cannot find decoder" );
1145 return VLC_EGENERIC;
1148 id->ff_dec_c = avcodec_alloc_context();
1149 id->ff_dec_c->width = id->f_src.video.i_width;
1150 id->ff_dec_c->height = id->f_src.video.i_height;
1151 id->ff_dec_c->bits_per_sample=id->f_src.video.i_bits_per_pixel;
1152 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1154 if( id->f_src.i_extra > 0 )
1156 if( i_ff_codec == CODEC_ID_SVQ3 )
1158 int i_size = id->f_src.i_extra;
1161 id->ff_dec_c->extradata_size = i_size + 12;
1162 p = id->ff_dec_c->extradata = malloc( i_size + 12 );
1164 memcpy( &p[0], "SVQ3", 4 );
1165 memset( &p[4], 0, 8 );
1166 memcpy( &p[12], id->f_src.p_extra, i_size );
1168 /* Now remove all atoms before the SMI one */
1169 if( id->ff_dec_c->extradata_size > 0x5a && strncmp( &p[0x56], "SMI ", 4 ) )
1171 uint8_t *psz = &p[0x52];
1173 while( psz < &p[id->ff_dec_c->extradata_size - 8] )
1175 int i_size = GetDWBE( psz );
1178 /* FIXME handle 1 as long size */
1181 if( !strncmp( &psz[4], "SMI ", 4 ) )
1183 memmove( &p[0x52], psz, &p[id->ff_dec_c->extradata_size] - psz );
1192 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1193 id->ff_dec_c->extradata = malloc( id->f_src.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
1195 memcpy( id->ff_dec_c->extradata, id->f_src.p_extra, id->f_src.i_extra );
1196 memset( (uint8_t*)id->ff_dec_c->extradata + id->f_src.i_extra, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1199 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1200 id->ff_dec_c->error_resilience= -1;
1201 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1202 id->ff_dec_c->opaque = p_sys;
1204 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1206 msg_Err( p_stream, "cannot open decoder" );
1207 return VLC_EGENERIC;
1212 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1214 /* Initialization of encoder format structures */
1215 es_format_Init( &id->p_encoder->fmt_in,
1216 id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1218 /* The dimensions will be set properly later on.
1219 * Just put sensible values so we can test if there is an encoder. */
1220 id->p_encoder->fmt_in.video.i_width =
1221 id->f_src.video.i_width ? id->f_src.video.i_width : 16;
1222 id->p_encoder->fmt_in.video.i_height =
1223 id->f_src.video.i_height ? id->f_src.video.i_height : 16;
1225 id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1226 id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1229 id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1230 #if LIBAVCODEC_BUILD >= 4662
1231 id->p_encoder->fmt_in.video.i_frame_rate_base =
1232 id->ff_dec_c->frame_rate_base;
1235 #if LIBAVCODEC_BUILD >= 4687
1236 if( id->ff_dec_c->height )
1237 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1238 ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1239 id->ff_dec_c->width / id->ff_dec_c->height );
1241 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1242 id->ff_dec_c->aspect_ratio;
1246 /* Check whether a particular aspect ratio was requested */
1247 if( id->f_src.video.i_aspect )
1249 id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1250 id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1253 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1254 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1255 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1257 id->p_encoder->i_vtolerance = p_sys->i_vtolerance;
1258 id->p_encoder->i_key_int = p_sys->i_key_int;
1259 id->p_encoder->i_b_frames = p_sys->i_b_frames;
1260 id->p_encoder->i_qmin = p_sys->i_qmin;
1261 id->p_encoder->i_qmax = p_sys->i_qmax;
1262 id->p_encoder->i_hq = p_sys->i_hq;
1263 id->p_encoder->b_strict_rc = p_sys->b_strict_rc;
1264 id->p_encoder->b_pre_me = p_sys->b_pre_me;
1265 id->p_encoder->b_hurry_up = p_sys->b_hurry_up;
1266 id->p_encoder->b_interlace = p_sys->b_interlace;
1267 id->p_encoder->i_rc_buffer_size = p_sys->i_rc_buffer_size;
1268 id->p_encoder->f_rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
1269 id->p_encoder->f_i_quant_factor = p_sys->f_i_quant_factor;
1270 id->p_encoder->i_noise_reduction = p_sys->i_noise_reduction;
1271 id->p_encoder->b_mpeg4_matrix = p_sys->b_mpeg4_matrix;
1272 id->p_encoder->i_threads = p_sys->i_threads;
1273 id->p_encoder->b_trellis = p_sys->b_trellis;
1275 id->p_ff_pic = avcodec_alloc_frame();
1276 id->p_ff_pic_tmp0 = NULL;
1277 id->p_ff_pic_tmp1 = NULL;
1278 id->p_ff_pic_tmp2 = NULL;
1279 id->p_vresample = NULL;
1281 id->p_encoder->p_module =
1282 module_Need( id->p_encoder, "encoder", NULL, 0 );
1284 if( !id->p_encoder->p_module )
1286 vlc_object_destroy( id->p_encoder );
1287 msg_Err( p_stream, "cannot find encoder" );
1288 return VLC_EGENERIC;
1291 /* Close the encoder.
1292 * We'll open it only when we have the first frame */
1293 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1294 id->p_encoder->p_module = NULL;
1296 id->b_enc_inited = VLC_FALSE;
1298 if ( p_sys->i_threads >= 1 )
1300 p_sys->id_video = id;
1301 vlc_mutex_init( p_stream, &p_sys->lock_out );
1302 vlc_cond_init( p_stream, &p_sys->cond );
1303 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1304 p_sys->i_first_pic = 0;
1305 p_sys->i_last_pic = 0;
1306 p_sys->p_buffers = NULL;
1307 p_sys->b_die = p_sys->b_error = 0;
1308 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1309 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1311 vlc_object_destroy( id->p_encoder );
1312 msg_Err( p_stream, "cannot spawn encoder thread" );
1313 return VLC_EGENERIC;
1320 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1321 sout_stream_id_t *id )
1323 if ( p_stream->p_sys->i_threads >= 1 )
1325 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1326 p_stream->p_sys->b_die = 1;
1327 vlc_cond_signal( &p_stream->p_sys->cond );
1328 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1329 vlc_thread_join( p_stream->p_sys );
1330 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1331 vlc_cond_destroy( &p_stream->p_sys->cond );
1335 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1336 av_free( id->ff_dec_c );
1339 if( id->p_encoder->p_module )
1340 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1341 vlc_object_destroy( id->p_encoder );
1346 free( id->p_ff_pic );
1349 if( id->p_ff_pic_tmp0 )
1351 free( id->p_ff_pic_tmp0->data[0] );
1352 free( id->p_ff_pic_tmp0 );
1354 if( id->p_ff_pic_tmp1)
1356 free( id->p_ff_pic_tmp1->data[0] );
1357 free( id->p_ff_pic_tmp1 );
1359 if( id->p_ff_pic_tmp2)
1361 free( id->p_ff_pic_tmp2->data[0] );
1362 free( id->p_ff_pic_tmp2 );
1364 if( id->p_vresample )
1366 img_resample_close( id->p_vresample );
1370 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1371 sout_stream_id_t *id, block_t *in, block_t **out )
1373 sout_stream_sys_t *p_sys = p_stream->p_sys;
1383 i_data = in->i_buffer;
1384 p_data = in->p_buffer;
1393 frame = id->p_ff_pic;
1394 p_sys->i_input_pts = in->i_pts;
1395 p_sys->i_input_dts = in->i_dts;
1398 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1405 avpicture_fill( (AVPicture*)frame, p_data,
1406 id->ff_dec_c->pix_fmt,
1407 id->ff_dec_c->width, id->ff_dec_c->height );
1412 frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts :
1415 frame->pict_type = FF_I_TYPE;
1420 msg_Warn( p_stream, "error");
1421 return VLC_EGENERIC;
1431 /* Get the pts of the decoded frame if any, otherwise keep the
1432 * interpolated one */
1433 if( frame->pts != AV_NOPTS_VALUE )
1435 p_sys->i_output_pts = frame->pts;
1438 /* Sanity check (seems to be needed for some streams ) */
1439 if( frame->pict_type == FF_B_TYPE )
1441 p_sys->b_input_has_b_frames = VLC_TRUE;
1444 if( !id->b_enc_inited )
1446 /* Hack because of the copy packetizer which can fail to detect the
1447 * proper size (which forces us to wait until the 1st frame
1449 int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1450 p_sys->i_crop_right;
1451 int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1452 p_sys->i_crop_bottom;
1454 if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1457 /* Apply the scaling */
1458 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1459 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1461 else if( id->f_dst.video.i_width > 0 &&
1462 id->f_dst.video.i_height <= 0 )
1464 id->f_dst.video.i_height =
1465 id->f_dst.video.i_width / (double)i_width * i_height;
1467 else if( id->f_dst.video.i_width <= 0 &&
1468 id->f_dst.video.i_height > 0 )
1470 id->f_dst.video.i_width =
1471 id->f_dst.video.i_height / (double)i_height * i_width;
1474 id->p_encoder->fmt_in.video.i_width =
1475 id->p_encoder->fmt_out.video.i_width =
1476 id->f_dst.video.i_width;
1477 id->p_encoder->fmt_in.video.i_height =
1478 id->p_encoder->fmt_out.video.i_height =
1479 id->f_dst.video.i_height;
1481 id->p_encoder->fmt_out.i_extra = 0;
1482 id->p_encoder->fmt_out.p_extra = NULL;
1484 id->p_encoder->p_module =
1485 module_Need( id->p_encoder, "encoder", NULL, 0 );
1486 if( !id->p_encoder->p_module )
1488 vlc_object_destroy( id->p_encoder );
1489 msg_Err( p_stream, "cannot find encoder" );
1490 id->b_transcode = VLC_FALSE;
1491 return VLC_EGENERIC;
1494 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1495 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1497 /* Hack for mp2v/mp1v transcoding support */
1498 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1499 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1501 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1505 p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1508 msg_Err( p_stream, "cannot add this stream" );
1509 transcode_video_ffmpeg_close( p_stream, id );
1510 id->b_transcode = VLC_FALSE;
1511 return VLC_EGENERIC;
1514 if( id->p_encoder->pf_header )
1516 p_block = id->p_encoder->pf_header( id->p_encoder );
1520 block_t *p_prev_block = p_block;
1522 p_out = block_New( p_stream,
1523 p_block->i_buffer );
1524 memcpy( p_out->p_buffer, p_block->p_buffer,
1526 p_out->i_dts = p_out->i_pts = in->i_dts;
1527 p_out->i_length = 0;
1528 block_ChainAppend( out, p_out );
1530 p_block = p_block->p_next;
1531 block_Release( p_prev_block );
1535 id->i_inter_pixfmt =
1536 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1538 id->b_enc_inited = VLC_TRUE;
1542 if( p_stream->p_sys->b_deinterlace )
1544 if( id->p_ff_pic_tmp0 == NULL )
1548 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1549 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1550 id->ff_dec_c->width, id->ff_dec_c->height );
1552 buf = malloc( i_size );
1554 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1556 id->ff_dec_c->width, id->ff_dec_c->height );
1559 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1560 id->ff_dec_c->pix_fmt,
1561 id->ff_dec_c->width, id->ff_dec_c->height );
1563 #if LIBAVCODEC_BUILD >= 4685
1564 id->p_ff_pic_tmp0->interlaced_frame = 0;
1566 id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1567 frame = id->p_ff_pic_tmp0;
1570 /* convert pix format */
1571 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1573 if( id->p_ff_pic_tmp1 == NULL )
1577 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1578 i_size = avpicture_get_size( id->i_inter_pixfmt,
1579 id->ff_dec_c->width,
1580 id->ff_dec_c->height );
1582 buf = malloc( i_size );
1584 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1586 id->ff_dec_c->width, id->ff_dec_c->height );
1589 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1590 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1591 id->ff_dec_c->width, id->ff_dec_c->height );
1593 id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1594 #if LIBAVCODEC_BUILD >= 4685
1595 id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1596 id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1598 frame = id->p_ff_pic_tmp1;
1601 /* convert size and crop */
1602 if( id->ff_dec_c->width != id->f_dst.video.i_width ||
1603 id->ff_dec_c->height != id->f_dst.video.i_height ||
1604 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1605 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1607 if( id->p_ff_pic_tmp2 == NULL )
1611 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1612 i_size = avpicture_get_size( id->i_inter_pixfmt,
1613 id->f_dst.video.i_width,
1614 id->f_dst.video.i_height );
1616 buf = malloc( i_size );
1618 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1620 id->f_dst.video.i_width,
1621 id->f_dst.video.i_height );
1624 img_resample_full_init( id->f_dst.video.i_width,
1625 id->f_dst.video.i_height,
1626 id->ff_dec_c->width,
1627 id->ff_dec_c->height,
1628 p_stream->p_sys->i_crop_top,
1629 p_stream->p_sys->i_crop_bottom,
1630 p_stream->p_sys->i_crop_left,
1631 p_stream->p_sys->i_crop_right
1632 #if LIBAVCODEC_BUILD >= 4708
1639 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1640 (AVPicture*)frame );
1642 id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1643 #if LIBAVCODEC_BUILD >= 4685
1644 id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1645 id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1647 frame = id->p_ff_pic_tmp2;
1651 p_pic = malloc(sizeof(picture_t));
1652 vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1653 id->p_encoder->fmt_in.i_codec,
1654 id->f_dst.video.i_width, id->f_dst.video.i_height,
1655 id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1656 id->f_dst.video.i_height );
1658 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1660 p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1661 if ( p_sys->i_threads >= 1 )
1663 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines *
1664 p_pic->p[i_plane].i_pitch);
1665 p_stream->p_vlc->pf_memcpy( p_pic->p[i_plane].p_pixels,
1666 frame->data[i_plane], p_pic->p[i_plane].i_lines *
1667 p_pic->p[i_plane].i_pitch );
1671 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1675 /* Set the pts of the frame being encoded */
1676 p_pic->date = p_sys->i_output_pts;
1678 p_pic->i_nb_fields = frame->repeat_pict;
1679 #if LIBAVCODEC_BUILD >= 4685
1680 p_pic->b_progressive = !frame->interlaced_frame;
1681 p_pic->b_top_field_first = frame->top_field_first;
1684 /* Interpolate the next PTS
1685 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1686 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1688 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1689 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1692 if ( p_sys->i_threads >= 1 )
1694 vlc_mutex_lock( &p_sys->lock_out );
1695 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1696 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1697 *out = p_sys->p_buffers;
1698 p_sys->p_buffers = NULL;
1699 vlc_cond_signal( &p_sys->cond );
1700 vlc_mutex_unlock( &p_sys->lock_out );
1705 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1708 block_ChainAppend( out, p_block );
1722 static int EncoderThread( sout_stream_sys_t * p_sys )
1724 sout_stream_id_t * id = p_sys->id_video;
1728 while ( !p_sys->b_die && !p_sys->b_error )
1732 vlc_mutex_lock( &p_sys->lock_out );
1733 while ( p_sys->i_last_pic == p_sys->i_first_pic )
1735 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1736 if ( p_sys->b_die || p_sys->b_error )
1739 if ( p_sys->b_die || p_sys->b_error )
1741 vlc_mutex_unlock( &p_sys->lock_out );
1745 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1746 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1747 vlc_mutex_unlock( &p_sys->lock_out );
1749 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1750 vlc_mutex_lock( &p_sys->lock_out );
1753 block_ChainAppend( &p_sys->p_buffers, p_block );
1755 vlc_mutex_unlock( &p_sys->lock_out );
1757 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1759 free( p_pic->p[i_plane].p_pixels );
1764 while ( p_sys->i_last_pic != p_sys->i_first_pic )
1766 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1767 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1769 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1771 free( p_pic->p[i_plane].p_pixels );
1776 block_ChainRelease( p_sys->p_buffers );
1781 /*****************************************************************************
1782 * transcode_video_ffmpeg_getframebuf:
1784 * Callback used by ffmpeg to get a frame buffer.
1785 * We use it to get the right PTS for each decoded picture.
1786 *****************************************************************************/
1787 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1790 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1793 if( p_sys->i_input_pts )
1795 p_frame->pts = p_sys->i_input_pts;
1797 else if( p_sys->i_input_dts )
1799 /* Some demuxers/packetizers only set the dts so let's try to find a
1800 * useful timestamp from this */
1801 if( !p_context->has_b_frames || !p_sys->b_input_has_b_frames ||
1802 !p_frame->reference || !p_sys->i_output_pts )
1804 p_frame->pts = p_sys->i_input_dts +
1805 /* Hack: ffmpeg encoding doesn't like frames with identical pts */
1806 (p_sys->i_output_pts ? 0 : 50000);
1808 else p_frame->pts = AV_NOPTS_VALUE;
1810 else p_frame->pts = AV_NOPTS_VALUE;
1812 if( p_sys->i_output_pts ) /* make sure 1st frame has a pts > 0 */
1814 p_sys->i_input_pts = 0;
1815 p_sys->i_input_dts = 0;
1818 return avcodec_default_get_buffer( p_context, p_frame );