1 /*****************************************************************************
2 * transcode.c: transcoding stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
5 * $Id: transcode.c,v 1.76 2004/02/20 18:34:28 massiot Exp $
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 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
50 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
51 static int Del ( sout_stream_t *, sout_stream_id_t * );
52 static int Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
54 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
55 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
56 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
58 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
59 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
60 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
62 static int transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
64 static int EncoderThread( struct sout_stream_sys_t * p_sys );
66 static int pi_channels_maps[6] =
69 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
70 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
71 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
72 | AOUT_CHAN_REARRIGHT,
73 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
74 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
77 /*****************************************************************************
79 *****************************************************************************/
81 set_description( _("Transcode stream output") );
82 set_capability( "sout stream", 50 );
83 add_shortcut( "transcode" );
84 set_callbacks( Open, Close );
87 #define PICTURE_RING_SIZE 64
89 struct sout_stream_sys_t
93 sout_stream_t * p_out;
94 sout_stream_id_t * id_video;
95 sout_buffer_t * p_buffers;
98 picture_t * pp_pics[PICTURE_RING_SIZE];
99 int i_first_pic, i_last_pic;
101 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
106 vlc_fourcc_t i_vcodec; /* " video " " " " */
117 vlc_bool_t b_deinterlace;
118 vlc_bool_t b_interlace;
119 vlc_bool_t b_strict_rc;
121 vlc_bool_t b_hurry_up;
122 int i_rc_buffer_size;
123 float f_rc_buffer_aggressivity;
124 float f_i_quant_factor;
125 int i_noise_reduction;
126 vlc_bool_t b_mpeg4_matrix;
128 vlc_bool_t b_trellis;
136 mtime_t i_output_pts;
139 /*****************************************************************************
141 *****************************************************************************/
142 static int Open( vlc_object_t *p_this )
144 sout_stream_t *p_stream = (sout_stream_t*)p_this;
145 sout_stream_sys_t *p_sys;
148 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
149 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
152 p_sys->i_vtolerance = -1;
153 p_sys->i_key_int = -1;
156 p_sys->f_i_quant_factor = 0.0;
157 #if LIBAVCODEC_BUILD >= 4673
158 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
160 p_sys->i_hq = VLC_FALSE;
162 p_sys->i_rc_buffer_size = 224*1024*8 * 3/2;
163 p_sys->f_rc_buffer_aggressivity = 0.1;
164 p_sys->i_threads = 0;
165 p_sys->b_trellis = 0;
167 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "acodec" ) ) )
172 memcpy( fcc, codec, __MIN( strlen( codec ), 4 ) );
174 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
176 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "samplerate" ) ) )
178 p_sys->i_sample_rate = atoi( val );
180 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "channels" ) ) )
182 p_sys->i_channels = atoi( val );
184 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ab" ) ) )
186 p_sys->i_abitrate = atoi( val );
187 if( p_sys->i_abitrate < 4000 )
189 p_sys->i_abitrate *= 1000;
193 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s", fcc,
194 p_sys->i_sample_rate, p_sys->i_channels,
195 p_sys->i_abitrate / 1000 );
198 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "vcodec" ) ) )
203 memcpy( fcc, codec, __MIN( strlen( codec ), 4 ) );
205 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
207 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "scale" ) ) )
209 p_sys->f_scale = atof( val );
211 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "width" ) ) )
213 p_sys->i_width = atoi( val );
215 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "height" ) ) )
217 p_sys->i_height = atoi( val );
219 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
221 p_sys->i_vbitrate = atoi( val );
222 if( p_sys->i_vbitrate < 16000 )
224 p_sys->i_vbitrate *= 1000;
227 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vt" ) ) )
229 p_sys->i_vtolerance = atoi( val );
231 if( sout_cfg_find( p_stream->p_cfg, "deinterlace" ) )
233 p_sys->b_deinterlace = VLC_TRUE;
235 if( sout_cfg_find( p_stream->p_cfg, "interlace" ) )
237 p_sys->b_interlace = VLC_TRUE;
239 if( sout_cfg_find( p_stream->p_cfg, "strict_rc" ) )
241 p_sys->b_strict_rc = VLC_TRUE;
243 if( sout_cfg_find( p_stream->p_cfg, "pre_me" ) )
245 p_sys->b_pre_me = VLC_TRUE;
247 if( sout_cfg_find( p_stream->p_cfg, "hurry_up" ) )
249 p_sys->b_hurry_up = VLC_TRUE;
250 /* hurry up mode needs noise reduction, even small */
251 p_sys->i_noise_reduction = 1;
253 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "rc_buffer_size" ) ) )
255 p_sys->i_rc_buffer_size = atoi( val );
257 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "rc_buffer_aggressivity" ) ) )
259 p_sys->f_rc_buffer_aggressivity = atof( val );
261 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "i_quant_factor" ) ) )
263 p_sys->f_i_quant_factor = atof( val );
265 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "noise_reduction" ) ) )
267 p_sys->i_noise_reduction = atoi( val );
269 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "mpeg4_matrix" ) ) )
271 p_sys->b_mpeg4_matrix = VLC_TRUE;
274 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "croptop" ) ) )
276 p_sys->i_crop_top = atoi( val );
278 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropbottom" ) ) )
280 p_sys->i_crop_bottom = atoi( val );
282 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropleft" ) ) )
284 p_sys->i_crop_left = atoi( val );
286 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropright" ) ) )
288 p_sys->i_crop_right = atoi( val );
290 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "keyint" ) ) )
292 p_sys->i_key_int = atoi( val );
294 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "bframes" ) ) )
296 p_sys->i_b_frames = atoi( val );
298 #if LIBAVCODEC_BUILD >= 4673
299 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "hq" ) ) )
301 if( !strcmp( val, "rd" ) )
303 p_sys->i_hq = FF_MB_DECISION_RD;
305 else if( !strcmp( val, "bits" ) )
307 p_sys->i_hq = FF_MB_DECISION_BITS;
309 else if( !strcmp( val, "simple" ) )
311 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
315 p_sys->i_hq = FF_MB_DECISION_RD;
319 if( sout_cfg_find( p_stream->p_cfg, "hq" ) )
321 p_sys->i_hq = VLC_TRUE;
324 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmin" ) ) )
326 p_sys->i_qmin = atoi( val );
328 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmax" ) ) )
330 p_sys->i_qmax = atoi( val );
332 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "threads" ) ) )
334 p_sys->i_threads = atoi( val );
336 if( sout_cfg_find( p_stream->p_cfg, "trellis" ) )
338 p_sys->b_trellis = VLC_TRUE;
341 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
342 fcc, p_sys->i_width, p_sys->i_height, p_sys->f_scale,
343 p_sys->i_vbitrate / 1000 );
348 msg_Err( p_stream, "cannot create chain" );
353 p_stream->pf_add = Add;
354 p_stream->pf_del = Del;
355 p_stream->pf_send = Send;
356 p_stream->p_sys = p_sys;
359 avcodec_register_all();
361 /* ffmpeg needs some padding at the end of each buffer */
362 p_stream->p_sout->i_padding += FF_INPUT_BUFFER_PADDING_SIZE;
367 /*****************************************************************************
369 *****************************************************************************/
370 static void Close( vlc_object_t * p_this )
372 sout_stream_t *p_stream = (sout_stream_t*)p_this;
373 sout_stream_sys_t *p_sys = p_stream->p_sys;
375 sout_stream_delete( p_sys->p_out );
376 vlc_object_destroy( p_sys );
379 struct sout_stream_id_t
381 vlc_fourcc_t b_transcode;
382 es_format_t f_src; /* only if transcoding */
383 es_format_t f_dst; /* " " " */
384 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
386 /* id of the out stream */
390 encoder_t *p_encoder;
391 vlc_fourcc_t b_enc_inited;
395 AVCodecContext *ff_dec_c;
405 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
406 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
407 AVFrame *p_ff_pic_tmp2; /* to do resample */
409 ImgReSampleContext *p_vresample;
413 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
415 sout_stream_sys_t *p_sys = p_stream->p_sys;
416 sout_stream_id_t *id;
418 id = malloc( sizeof( sout_stream_id_t ) );
421 id->p_encoder = NULL;
423 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
426 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
427 (char*)&p_fmt->i_codec,
428 (char*)&p_sys->i_acodec );
431 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
433 /* create dst format */
434 es_format_Init( &id->f_dst, AUDIO_ES, p_sys->i_acodec );
435 id->f_dst.i_id = id->f_src.i_id;
436 id->f_dst.i_group = id->f_src.i_group;
437 if( id->f_src.psz_language ) id->f_dst.psz_language = strdup( id->f_src.psz_language );
438 id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ? p_sys->i_sample_rate : id->f_src.audio.i_rate;
439 id->f_dst.audio.i_channels = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.audio.i_channels;
440 id->f_dst.i_bitrate = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
441 id->f_dst.audio.i_blockalign = 0;
442 id->f_dst.i_extra = 0;
443 id->f_dst.p_extra = NULL;
445 /* build decoder -> filter -> encoder */
446 if( transcode_audio_ffmpeg_new( p_stream, id ) )
448 msg_Err( p_stream, "cannot create audio chain" );
453 /* open output stream */
454 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
455 id->b_transcode = VLC_TRUE;
463 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
466 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
467 (char*)&p_fmt->i_codec,
468 (char*)&p_sys->i_vcodec );
470 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
472 /* create dst format */
473 es_format_Init( &id->f_dst, VIDEO_ES, p_sys->i_vcodec );
474 id->f_dst.i_id = id->f_src.i_id;
475 id->f_dst.i_group = id->f_src.i_group;
476 if( id->f_src.psz_language ) id->f_dst.psz_language = strdup( id->f_src.psz_language );
477 id->f_dst.video.i_width = p_sys->i_width;
478 id->f_dst.video.i_height= p_sys->i_height;
479 id->f_dst.i_bitrate = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
480 id->f_dst.i_extra = 0;
481 id->f_dst.p_extra = NULL;
483 /* build decoder -> filter -> encoder */
484 if( transcode_video_ffmpeg_new( p_stream, id ) )
486 msg_Err( p_stream, "cannot create video chain" );
491 /* open output stream */
492 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
494 id->b_transcode = VLC_TRUE;
498 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
499 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
500 id->b_transcode = VLC_FALSE;
512 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
514 sout_stream_sys_t *p_sys = p_stream->p_sys;
516 if( id->b_transcode )
518 if( id->f_src.i_cat == AUDIO_ES )
520 transcode_audio_ffmpeg_close( p_stream, id );
522 else if( id->f_src.i_cat == VIDEO_ES )
524 transcode_video_ffmpeg_close( p_stream, id );
528 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
534 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
535 sout_buffer_t *p_buffer )
537 sout_stream_sys_t *p_sys = p_stream->p_sys;
539 if( id->b_transcode )
541 sout_buffer_t *p_buffer_out;
542 if( id->f_src.i_cat == AUDIO_ES )
544 transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
547 else if( id->f_src.i_cat == VIDEO_ES )
549 if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
550 &p_buffer_out ) != VLC_SUCCESS )
552 sout_BufferDelete( p_stream->p_sout, p_buffer );
556 sout_BufferDelete( p_stream->p_sout, p_buffer );
560 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
564 else if( id->id != NULL )
566 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
570 sout_BufferDelete( p_stream->p_sout, p_buffer );
575 /****************************************************************************
576 * ffmpeg decoder reencocdr part
577 ****************************************************************************/
583 } fourcc_to_ff_code[] =
586 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
587 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
588 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
589 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
590 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
591 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
592 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
593 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
594 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
597 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
598 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
599 #if LIBAVCODEC_BUILD >= 4676
600 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
602 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
603 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
604 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
605 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
606 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
607 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
608 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
609 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
610 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
611 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
612 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
613 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
614 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
615 #if LIBAVCODEC_BUILD >= 4666
616 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
619 /* raw video code, only used for 'encoding' */
620 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
621 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
622 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
623 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
624 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
625 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
626 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
627 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
628 { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
629 { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
631 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
634 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
638 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
640 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
642 return fourcc_to_ff_code[i].i_ff_codec;
649 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
653 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
654 case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
655 case VLC_FOURCC( 'I', '4', '2', '0' ):
656 return PIX_FMT_YUV420P;
657 case VLC_FOURCC( 'I', '4', '2', '2' ):
658 return PIX_FMT_YUV422P;
659 case VLC_FOURCC( 'I', '4', '4', '4' ):
660 return PIX_FMT_YUV444P;
661 case VLC_FOURCC( 'R', 'V', '1', '5' ):
662 return PIX_FMT_RGB555;
663 case VLC_FOURCC( 'R', 'V', '1', '6' ):
664 return PIX_FMT_RGB565;
665 case VLC_FOURCC( 'R', 'V', '2', '4' ):
666 return PIX_FMT_BGR24;
667 case VLC_FOURCC( 'R', 'V', '3', '2' ):
668 return PIX_FMT_RGBA32;
669 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
670 return PIX_FMT_GRAY8;
671 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
672 return PIX_FMT_YUV422;
678 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
682 case PIX_FMT_YUV420P:
683 return VLC_FOURCC('I','4','2','0');
684 case PIX_FMT_YUV422P:
685 return VLC_FOURCC('I','4','2','2');
686 case PIX_FMT_YUV444P:
687 return VLC_FOURCC('I','4','4','4');
690 return VLC_FOURCC('Y','U','Y','2');
693 return VLC_FOURCC('R','V','1','5');
695 return VLC_FOURCC('R','V','1','6');
697 return VLC_FOURCC('R','V','2','4');
699 return VLC_FOURCC('R','V','3','2');
701 return VLC_FOURCC('G','R','E','Y');
703 case PIX_FMT_YUV410P:
704 case PIX_FMT_YUV411P:
711 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
712 sout_stream_id_t *id )
716 if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
717 id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
718 id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
719 id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
723 id->ff_dec_c = avcodec_alloc_context();
724 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
725 id->ff_dec_c->channels = id->f_src.audio.i_channels;
726 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
727 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
732 i_ff_codec = get_ff_codec( id->f_src.i_codec );
733 if( i_ff_codec == 0 )
735 msg_Err( p_stream, "cannot find decoder id" );
739 id->ff_dec = avcodec_find_decoder( i_ff_codec );
742 msg_Err( p_stream, "cannot find decoder (avcodec)" );
746 id->ff_dec_c = avcodec_alloc_context();
747 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
748 id->ff_dec_c->channels = id->f_src.audio.i_channels;
749 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
750 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
752 id->ff_dec_c->extradata_size = id->f_src.i_extra;
753 id->ff_dec_c->extradata = id->f_src.p_extra;
754 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
756 msg_Err( p_stream, "cannot open decoder" );
761 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
762 id->i_buffer_pos = 0;
763 id->p_buffer = malloc( id->i_buffer );
765 /* Sanity check for audio channels */
766 id->f_dst.audio.i_channels = __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
769 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
771 /* Initialization of encoder format structures */
772 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
773 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
774 id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
775 id->p_encoder->fmt_in.audio.i_physical_channels =
776 id->p_encoder->fmt_in.audio.i_original_channels =
777 pi_channels_maps[id->f_dst.audio.i_channels];
778 id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
780 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
781 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
782 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
784 id->p_encoder->p_module =
785 module_Need( id->p_encoder, "encoder", NULL );
786 if( !id->p_encoder->p_module )
788 vlc_object_destroy( id->p_encoder );
789 msg_Err( p_stream, "cannot open encoder" );
793 id->b_enc_inited = VLC_FALSE;
795 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
796 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
798 /* Hack for mp3 transcoding support */
799 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
801 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
807 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
808 sout_stream_id_t *id )
812 avcodec_close( id->ff_dec_c );
813 free( id->ff_dec_c );
816 module_Unneed( id->p_encoder, id->p_encoder->p_module );
817 vlc_object_destroy( id->p_encoder );
819 free( id->p_buffer );
822 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
823 sout_stream_id_t *id,
825 sout_buffer_t **out )
827 aout_buffer_t aout_buf;
829 int i_buffer = in->i_size;
830 char *p_buffer = in->p_buffer;
831 id->i_dts = in->i_dts;
836 id->i_buffer_pos = 0;
838 /* decode as much data as possible */
843 i_used = avcodec_decode_audio( id->ff_dec_c,
844 (int16_t*)id->p_buffer, &id->i_buffer_pos,
845 p_buffer, i_buffer );
848 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
853 msg_Warn( p_stream, "error audio decoding");
860 if ( id->i_buffer_pos < 0 )
862 msg_Warn( p_stream, "weird error audio decoding");
868 int16_t *sout = (int16_t*)id->p_buffer;
870 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
871 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
873 int8_t *sin = (int8_t*)p_buffer;
874 int i_used = __MIN( id->i_buffer/2, i_buffer );
875 int i_samples = i_used;
877 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
878 while( i_samples > 0 )
880 *sout++ = ( *sin++ ) << 8;
884 while( i_samples > 0 )
886 *sout++ = ( *sin++ - 128 ) << 8;
892 id->i_buffer_pos = i_used * 2;
894 else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
895 id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
897 int16_t *sin = (int16_t*)p_buffer;
898 int i_used = __MIN( id->i_buffer, i_buffer );
899 int i_samples = i_used / 2;
902 memcpy( sout, sin, i_used );
904 #ifdef WORDS_BIGENDIAN
905 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
907 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
910 uint8_t *dat = (uint8_t*)sout;
912 while( i_samples > 0 )
927 id->i_buffer_pos = i_used;
931 if( id->i_buffer_pos == 0 ) continue;
933 /* Encode as much data as possible */
934 if( !id->b_enc_inited && id->p_encoder->pf_header )
936 p_block = id->p_encoder->pf_header( id->p_encoder );
939 sout_buffer_t *p_out;
940 block_t *p_prev_block = p_block;
942 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
943 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
944 p_out->i_dts = p_out->i_pts = in->i_dts;
946 sout_BufferChain( out, p_out );
948 p_block = p_block->p_next;
949 block_Release( p_prev_block );
952 id->b_enc_inited = VLC_TRUE;
955 aout_buf.p_buffer = id->p_buffer;
956 aout_buf.i_nb_bytes = id->i_buffer_pos;
957 aout_buf.i_nb_samples = id->i_buffer_pos / 2 / id->f_src.audio.i_channels;
958 aout_buf.start_date = id->i_dts;
959 aout_buf.end_date = id->i_dts;
961 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
962 id->f_src.audio.i_channels / id->f_src.audio.i_rate );
964 if( id->f_src.audio.i_channels !=
965 id->p_encoder->fmt_in.audio.i_channels )
970 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
971 static const int translation[7][6] =
972 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
973 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
974 { 0, 1, 0, 0, 0, 0 }, /* 2 */
975 { 1, 2, 0, 0, 0, 0 }, /* 3 */
976 { 1, 3, 2, 0, 0, 0 }, /* 4 */
977 { 1, 3, 4, 2, 0, 0 }, /* 5 */
978 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
980 /* dumb downmixing */
981 for( i = 0; i < aout_buf.i_nb_samples; i++ )
983 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
984 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
986 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
987 p_buffer[i*id->f_src.audio.i_channels+
988 translation[id->f_src.audio.i_channels][j]];
991 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
994 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
997 sout_buffer_t *p_out;
998 block_t *p_prev_block = p_block;
1000 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1001 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1002 p_out->i_dts = p_block->i_dts;
1003 p_out->i_pts = p_block->i_pts;
1004 p_out->i_length = p_block->i_length;
1005 sout_BufferChain( out, p_out );
1007 p_block = p_block->p_next;
1008 block_Release( p_prev_block );
1019 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1020 sout_stream_id_t *id )
1022 sout_stream_sys_t *p_sys = p_stream->p_sys;
1027 if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1028 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1029 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1030 id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1031 id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1032 id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1033 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1034 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1035 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1036 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1037 id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1040 id->ff_dec_c = avcodec_alloc_context();
1041 id->ff_dec_c->width = id->f_src.video.i_width;
1042 id->ff_dec_c->height = id->f_src.video.i_height;
1043 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_codec );
1048 i_ff_codec = get_ff_codec( id->f_src.i_codec );
1049 if( i_ff_codec == 0 )
1051 msg_Err( p_stream, "cannot find decoder" );
1052 return VLC_EGENERIC;
1055 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1058 msg_Err( p_stream, "cannot find decoder" );
1059 return VLC_EGENERIC;
1062 id->ff_dec_c = avcodec_alloc_context();
1063 id->ff_dec_c->width = id->f_src.video.i_width;
1064 id->ff_dec_c->height = id->f_src.video.i_height;
1065 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1066 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1067 id->ff_dec_c->extradata = id->f_src.p_extra;
1068 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1069 id->ff_dec_c->error_resilience= -1;
1070 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1071 id->ff_dec_c->opaque = p_sys;
1073 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1075 msg_Err( p_stream, "cannot open decoder" );
1076 return VLC_EGENERIC;
1079 if( i_ff_codec == CODEC_ID_MPEG4 && id->ff_dec_c->extradata_size > 0 )
1083 uint8_t *p_vol = malloc( id->ff_dec_c->extradata_size +
1084 FF_INPUT_BUFFER_PADDING_SIZE );
1086 memcpy( p_vol, id->ff_dec_c->extradata,
1087 id->ff_dec_c->extradata_size );
1088 memset( p_vol + id->ff_dec_c->extradata_size, 0,
1089 FF_INPUT_BUFFER_PADDING_SIZE );
1091 avcodec_decode_video( id->ff_dec_c, &frame, &b_gotpicture,
1092 id->ff_dec_c->extradata,
1093 id->ff_dec_c->extradata_size );
1099 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1101 /* Initialization of encoder format structures */
1102 es_format_Init( &id->p_encoder->fmt_in,
1103 id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1105 /* The dimensions will be set properly later on.
1106 * Just put sensible values so we can test if there is an encoder. */
1107 id->p_encoder->fmt_in.video.i_width = 16;
1108 id->p_encoder->fmt_in.video.i_height = 16;
1110 id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1111 id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1114 id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1115 #if LIBAVCODEC_BUILD >= 4662
1116 id->p_encoder->fmt_in.video.i_frame_rate_base =
1117 id->ff_dec_c->frame_rate_base;
1120 #if LIBAVCODEC_BUILD >= 4687
1121 if( id->ff_dec_c->height )
1122 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1123 ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1124 id->ff_dec_c->width / id->ff_dec_c->height );
1126 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1127 id->ff_dec_c->aspect_ratio;
1131 /* Check whether a particular aspect ratio was requested */
1132 if( id->f_src.video.i_aspect )
1134 id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1135 id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1138 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1139 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1140 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1142 id->p_encoder->i_vtolerance = p_sys->i_vtolerance;
1143 id->p_encoder->i_key_int = p_sys->i_key_int;
1144 id->p_encoder->i_b_frames = p_sys->i_b_frames;
1145 id->p_encoder->i_qmin = p_sys->i_qmin;
1146 id->p_encoder->i_qmax = p_sys->i_qmax;
1147 id->p_encoder->i_hq = p_sys->i_hq;
1148 id->p_encoder->b_strict_rc = p_sys->b_strict_rc;
1149 id->p_encoder->b_pre_me = p_sys->b_pre_me;
1150 id->p_encoder->b_hurry_up = p_sys->b_hurry_up;
1151 id->p_encoder->b_interlace = p_sys->b_interlace;
1152 id->p_encoder->i_rc_buffer_size = p_sys->i_rc_buffer_size;
1153 id->p_encoder->f_rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
1154 id->p_encoder->f_i_quant_factor = p_sys->f_i_quant_factor;
1155 id->p_encoder->i_noise_reduction = p_sys->i_noise_reduction;
1156 id->p_encoder->b_mpeg4_matrix = p_sys->b_mpeg4_matrix;
1157 id->p_encoder->i_threads = p_sys->i_threads;
1158 id->p_encoder->b_trellis = p_sys->b_trellis;
1160 id->p_ff_pic = avcodec_alloc_frame();
1161 id->p_ff_pic_tmp0 = NULL;
1162 id->p_ff_pic_tmp1 = NULL;
1163 id->p_ff_pic_tmp2 = NULL;
1164 id->p_vresample = NULL;
1166 id->p_encoder->p_module =
1167 module_Need( id->p_encoder, "encoder", NULL );
1169 if( !id->p_encoder->p_module )
1171 vlc_object_destroy( id->p_encoder );
1172 msg_Err( p_stream, "cannot find encoder" );
1173 return VLC_EGENERIC;
1176 /* Close the encoder.
1177 * We'll open it only when we have the first frame */
1178 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1179 id->p_encoder->p_module = NULL;
1181 id->b_enc_inited = VLC_FALSE;
1183 if ( p_sys->i_threads >= 1 )
1185 p_sys->id_video = id;
1186 vlc_mutex_init( p_stream, &p_sys->lock_out );
1187 vlc_cond_init( p_stream, &p_sys->cond );
1188 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1189 p_sys->i_first_pic = 0;
1190 p_sys->i_last_pic = 0;
1191 p_sys->p_buffers = NULL;
1192 p_sys->b_die = p_sys->b_error = 0;
1193 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1194 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1196 vlc_object_destroy( id->p_encoder );
1197 msg_Err( p_stream, "cannot spawn encoder thread" );
1198 return VLC_EGENERIC;
1205 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1206 sout_stream_id_t *id )
1211 avcodec_close( id->ff_dec_c );
1212 free( id->ff_dec_c );
1216 if( id->p_encoder->p_module )
1217 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1218 vlc_object_destroy( id->p_encoder );
1223 free( id->p_ff_pic );
1226 if( id->p_ff_pic_tmp0 )
1228 free( id->p_ff_pic_tmp0->data[0] );
1229 free( id->p_ff_pic_tmp0 );
1231 if( id->p_ff_pic_tmp1)
1233 free( id->p_ff_pic_tmp1->data[0] );
1234 free( id->p_ff_pic_tmp1 );
1236 if( id->p_ff_pic_tmp2)
1238 free( id->p_ff_pic_tmp2->data[0] );
1239 free( id->p_ff_pic_tmp2 );
1241 if( id->p_vresample )
1243 img_resample_close( id->p_vresample );
1245 if ( p_stream->p_sys->i_threads >= 1 )
1247 p_stream->p_sys->b_die = 1;
1248 vlc_thread_join( p_stream->p_sys );
1249 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1250 vlc_cond_destroy( &p_stream->p_sys->cond );
1254 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1255 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1257 sout_stream_sys_t *p_sys = p_stream->p_sys;
1267 i_data = in->i_size;
1268 p_data = in->p_buffer;
1276 p_pic = malloc(sizeof(picture_t));
1279 frame = id->p_ff_pic;
1280 p_sys->i_input_pts = in->i_pts;
1283 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1290 avpicture_fill( (AVPicture*)frame, p_data,
1291 id->ff_dec_c->pix_fmt,
1292 id->ff_dec_c->width, id->ff_dec_c->height );
1297 frame->pts = p_sys->i_input_pts;
1302 msg_Warn( p_stream, "error");
1303 return VLC_EGENERIC;
1313 /* Get the pts of the decoded frame if any, otherwise keep the
1314 * interpolated one */
1315 if( frame->pts > 0 )
1317 p_sys->i_output_pts = frame->pts;
1320 if( !id->b_enc_inited )
1322 /* Hack because of the copy packetizer which can fail to detect the
1323 * proper size (which forces us to wait until the 1st frame
1325 int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1326 p_sys->i_crop_right;
1327 int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1328 p_sys->i_crop_bottom;
1330 if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1333 /* Apply the scaling */
1334 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1335 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1337 else if( id->f_dst.video.i_width > 0 &&
1338 id->f_dst.video.i_height <= 0 )
1340 id->f_dst.video.i_height =
1341 id->f_dst.video.i_width / (double)i_width * i_height;
1343 else if( id->f_dst.video.i_width <= 0 &&
1344 id->f_dst.video.i_height > 0 )
1346 id->f_dst.video.i_width =
1347 id->f_dst.video.i_height / (double)i_height * i_width;
1350 id->p_encoder->fmt_in.video.i_width =
1351 id->p_encoder->fmt_out.video.i_width =
1352 id->f_dst.video.i_width;
1353 id->p_encoder->fmt_in.video.i_height =
1354 id->p_encoder->fmt_out.video.i_height =
1355 id->f_dst.video.i_height;
1357 id->p_encoder->fmt_out.i_extra = 0;
1358 id->p_encoder->fmt_out.p_extra = NULL;
1360 id->p_encoder->p_module =
1361 module_Need( id->p_encoder, "encoder", NULL );
1362 if( !id->p_encoder->p_module )
1364 vlc_object_destroy( id->p_encoder );
1365 msg_Err( p_stream, "cannot find encoder" );
1366 id->b_transcode = VLC_FALSE;
1367 return VLC_EGENERIC;
1370 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1371 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1373 /* Hack for mp2v/mp1v transcoding support */
1374 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1375 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1377 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1381 p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1384 msg_Err( p_stream, "cannot add this stream" );
1385 transcode_video_ffmpeg_close( p_stream, id );
1386 id->b_transcode = VLC_FALSE;
1387 return VLC_EGENERIC;
1390 if( id->p_encoder->pf_header )
1392 p_block = id->p_encoder->pf_header( id->p_encoder );
1395 sout_buffer_t *p_out;
1396 block_t *p_prev_block = p_block;
1398 p_out = sout_BufferNew( p_stream->p_sout,
1399 p_block->i_buffer );
1400 memcpy( p_out->p_buffer, p_block->p_buffer,
1402 p_out->i_dts = p_out->i_pts = in->i_dts;
1403 p_out->i_length = 0;
1404 sout_BufferChain( out, p_out );
1406 p_block = p_block->p_next;
1407 block_Release( p_prev_block );
1411 id->i_inter_pixfmt =
1412 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1414 id->b_enc_inited = VLC_TRUE;
1418 if( p_stream->p_sys->b_deinterlace )
1420 if( id->p_ff_pic_tmp0 == NULL )
1424 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1425 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1426 id->ff_dec_c->width, id->ff_dec_c->height );
1428 buf = malloc( i_size );
1430 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1432 id->ff_dec_c->width, id->ff_dec_c->height );
1435 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1436 id->ff_dec_c->pix_fmt,
1437 id->ff_dec_c->width, id->ff_dec_c->height );
1439 id->p_ff_pic_tmp0->interlaced_frame = 0;
1440 id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1441 frame = id->p_ff_pic_tmp0;
1444 /* convert pix format */
1445 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1447 if( id->p_ff_pic_tmp1 == NULL )
1451 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1452 i_size = avpicture_get_size( id->i_inter_pixfmt,
1453 id->ff_dec_c->width,
1454 id->ff_dec_c->height );
1456 buf = malloc( i_size );
1458 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1460 id->ff_dec_c->width, id->ff_dec_c->height );
1463 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1464 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1465 id->ff_dec_c->width, id->ff_dec_c->height );
1467 id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1468 id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1469 #if LIBAVCODEC_BUILD >= 4684
1470 id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1472 frame = id->p_ff_pic_tmp1;
1475 /* convert size and crop */
1476 if( id->ff_dec_c->width != id->f_dst.video.i_width ||
1477 id->ff_dec_c->height != id->f_dst.video.i_height ||
1478 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1479 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1481 if( id->p_ff_pic_tmp2 == NULL )
1485 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1486 i_size = avpicture_get_size( id->i_inter_pixfmt,
1487 id->f_dst.video.i_width,
1488 id->f_dst.video.i_height );
1490 buf = malloc( i_size );
1492 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1494 id->f_dst.video.i_width, id->f_dst.video.i_height );
1497 img_resample_full_init( id->f_dst.video.i_width,
1498 id->f_dst.video.i_height,
1499 id->ff_dec_c->width, id->ff_dec_c->height,
1500 p_stream->p_sys->i_crop_top,
1501 p_stream->p_sys->i_crop_bottom,
1502 p_stream->p_sys->i_crop_left,
1503 p_stream->p_sys->i_crop_right );
1506 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1507 (AVPicture*)frame );
1509 id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1510 id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1511 #if LIBAVCODEC_BUILD >= 4684
1512 id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1514 frame = id->p_ff_pic_tmp2;
1518 vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1519 id->p_encoder->fmt_in.i_codec,
1520 id->f_dst.video.i_width, id->f_dst.video.i_height,
1521 id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1522 id->f_dst.video.i_height );
1524 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1526 p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1527 if ( p_sys->i_threads >= 1 )
1529 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines * p_pic->p[i_plane].i_pitch);
1530 p_stream->p_vlc->pf_memcpy(p_pic->p[i_plane].p_pixels, frame->data[i_plane], p_pic->p[i_plane].i_lines * p_pic->p[i_plane].i_pitch);
1534 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1538 /* Set the pts of the frame being encoded */
1539 p_pic->date = p_sys->i_output_pts;
1541 p_pic->b_progressive = !frame->interlaced_frame;
1542 p_pic->i_nb_fields = frame->repeat_pict;
1543 #if LIBAVCODEC_BUILD >= 4684
1544 p_pic->b_top_field_first = frame->top_field_first;
1547 /* Interpolate the next PTS
1548 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1549 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1551 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1552 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1555 if ( p_sys->i_threads >= 1 )
1557 vlc_mutex_lock( &p_sys->lock_out );
1558 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1559 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1560 *out = p_sys->p_buffers;
1561 p_sys->p_buffers = NULL;
1562 vlc_cond_signal( &p_sys->cond );
1563 vlc_mutex_unlock( &p_sys->lock_out );
1568 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1571 sout_buffer_t *p_out;
1572 block_t *p_prev_block = p_block;
1574 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1575 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1576 p_out->i_dts = p_block->i_dts;
1577 p_out->i_pts = p_block->i_pts;
1578 p_out->i_length = p_block->i_length;
1579 sout_BufferChain( out, p_out );
1581 p_block = p_block->p_next;
1582 block_Release( p_prev_block );
1596 static int EncoderThread( sout_stream_sys_t * p_sys )
1598 sout_stream_t * p_stream = p_sys->p_out;
1599 sout_stream_id_t * id = p_sys->id_video;
1602 sout_buffer_t * p_buffer;
1604 while ( !p_sys->b_die && !p_sys->b_error )
1608 vlc_mutex_lock( &p_sys->lock_out );
1609 while ( p_sys->i_last_pic == p_sys->i_first_pic )
1611 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1612 if ( p_sys->b_die || p_sys->b_error )
1615 if ( p_sys->b_die || p_sys->b_error )
1617 vlc_mutex_unlock( &p_sys->lock_out );
1621 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1622 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1623 vlc_mutex_unlock( &p_sys->lock_out );
1625 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1626 vlc_mutex_lock( &p_sys->lock_out );
1629 sout_buffer_t *p_out;
1630 block_t *p_prev_block = p_block;
1632 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1633 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1634 p_out->i_dts = p_block->i_dts;
1635 p_out->i_pts = p_block->i_pts;
1636 p_out->i_length = p_block->i_length;
1637 sout_BufferChain( &p_sys->p_buffers, p_out );
1639 p_block = p_block->p_next;
1640 block_Release( p_prev_block );
1642 vlc_mutex_unlock( &p_sys->lock_out );
1644 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1646 free( p_pic->p[i_plane].p_pixels );
1651 while ( p_sys->i_last_pic != p_sys->i_first_pic )
1653 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1654 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1656 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1658 free( p_pic->p[i_plane].p_pixels );
1663 p_buffer = p_sys->p_buffers;
1664 while ( p_buffer != NULL )
1666 sout_buffer_t * p_next = p_buffer->p_next;
1667 sout_BufferDelete( p_stream->p_sout, p_buffer );
1674 /*****************************************************************************
1675 * transcode_video_ffmpeg_getframebuf:
1677 * Callback used by ffmpeg to get a frame buffer.
1678 * We use it to get the right PTS for each decoded picture.
1679 *****************************************************************************/
1680 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1683 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1686 p_frame->pts = p_sys->i_input_pts;
1688 return avcodec_default_get_buffer( p_context, p_frame );