1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: transcode.c,v 1.43 2003/10/24 21:27:06 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc/input.h>
34 #include <vlc/decoder.h>
37 #ifdef HAVE_FFMPEG_AVCODEC_H
38 # include <ffmpeg/avcodec.h>
44 #ifdef HAVE_VORBIS_VORBISENC_H
45 # include <vorbis/vorbisenc.h>
46 # ifndef OV_ECTL_RATEMANAGE_AVG
47 # define OV_ECTL_RATEMANAGE_AVG 0x0
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close ( vlc_object_t * );
57 static sout_stream_id_t *Add ( sout_stream_t *, sout_format_t * );
58 static int Del ( sout_stream_t *, sout_stream_id_t * );
59 static int Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
61 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
62 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
63 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
65 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
66 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
67 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
69 static int transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
71 /*****************************************************************************
73 *****************************************************************************/
75 set_description( _("Transcode stream") );
76 set_capability( "sout stream", 50 );
77 add_shortcut( "transcode" );
78 set_callbacks( Open, Close );
81 struct sout_stream_sys_t
85 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
90 vlc_fourcc_t i_vcodec; /* " video " " " " */
100 vlc_bool_t b_deinterlace;
108 mtime_t i_output_pts;
109 mtime_t i_last_ref_pts;
111 mtime_t i_buggy_pts_detect;
114 /*****************************************************************************
116 *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
119 sout_stream_t *p_stream = (sout_stream_t*)p_this;
120 sout_stream_sys_t *p_sys;
123 p_sys = malloc( sizeof( sout_stream_sys_t ) );
124 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
127 p_sys->i_sample_rate = 0;
128 p_sys->i_channels = 0;
129 p_sys->i_abitrate = 0;
132 p_sys->i_vbitrate = 0;
133 p_sys->i_vtolerance = -1;
136 p_sys->i_key_int = -1;
137 p_sys->i_b_frames = 0;
140 #if LIBAVCODEC_BUILD >= 4673
141 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
143 p_sys->i_hq = VLC_FALSE;
145 p_sys->b_deinterlace= VLC_FALSE;
147 p_sys->i_crop_top = 0;
148 p_sys->i_crop_bottom= 0;
149 p_sys->i_crop_right = 0;
150 p_sys->i_crop_left = 0;
152 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "acodec" ) ) )
157 memcpy( fcc, codec, strlen( codec ) );
159 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
161 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "samplerate" ) ) )
163 p_sys->i_sample_rate = atoi( val );
165 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "channels" ) ) )
167 p_sys->i_channels = atoi( val );
169 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ab" ) ) )
171 p_sys->i_abitrate = atoi( val );
172 if( p_sys->i_abitrate < 4000 )
174 p_sys->i_abitrate *= 1000;
178 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s", fcc,
179 p_sys->i_sample_rate, p_sys->i_channels,
180 p_sys->i_abitrate / 1024 );
183 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "vcodec" ) ) )
188 memcpy( fcc, codec, strlen( codec ) );
190 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
192 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "width" ) ) )
194 p_sys->i_width = atoi( val );
196 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "height" ) ) )
198 p_sys->i_height = atoi( val );
200 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
202 p_sys->i_vbitrate = atoi( val );
203 if( p_sys->i_vbitrate < 16000 )
205 p_sys->i_vbitrate *= 1000;
208 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vt" ) ) )
210 p_sys->i_vtolerance = atoi( val );
212 if( sout_cfg_find( p_stream->p_cfg, "deinterlace" ) )
214 p_sys->b_deinterlace = VLC_TRUE;
217 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "croptop" ) ) )
219 p_sys->i_crop_top = atoi( val );
221 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropbottom" ) ) )
223 p_sys->i_crop_bottom = atoi( val );
225 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropleft" ) ) )
227 p_sys->i_crop_left = atoi( val );
229 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropright" ) ) )
231 p_sys->i_crop_right = atoi( val );
233 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "keyint" ) ) )
235 p_sys->i_key_int = atoi( val );
237 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "bframes" ) ) )
239 p_sys->i_b_frames = atoi( val );
241 #if LIBAVCODEC_BUILD >= 4673
242 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "hq" ) ) )
244 if( !strcmp( val, "rd" ) )
246 p_sys->i_hq = FF_MB_DECISION_RD;
248 else if( !strcmp( val, "bits" ) )
250 p_sys->i_hq = FF_MB_DECISION_BITS;
252 else if( !strcmp( val, "simple" ) )
254 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
258 p_sys->i_hq = FF_MB_DECISION_RD;
262 if( sout_cfg_find( p_stream->p_cfg, "hq" ) )
264 p_sys->i_hq = VLC_TRUE;
267 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmin" ) ) )
269 p_sys->i_qmin = atoi( val );
271 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmax" ) ) )
273 p_sys->i_qmax = atoi( val );
276 msg_Dbg( p_stream, "codec video=%4.4s %dx%d %dkb/s",
278 p_sys->i_width, p_sys->i_height,
279 p_sys->i_vbitrate / 1024 );
284 msg_Err( p_stream, "cannot create chain" );
288 p_stream->pf_add = Add;
289 p_stream->pf_del = Del;
290 p_stream->pf_send = Send;
292 p_stream->p_sys = p_sys;
295 avcodec_register_all();
297 /* ffmpeg needs some padding at the end of each buffer */
298 p_stream->p_sout->i_padding += FF_INPUT_BUFFER_PADDING_SIZE;
303 /*****************************************************************************
305 *****************************************************************************/
307 static void Close( vlc_object_t * p_this )
309 sout_stream_t *p_stream = (sout_stream_t*)p_this;
310 sout_stream_sys_t *p_sys = p_stream->p_sys;
312 sout_stream_delete( p_sys->p_out );
316 struct sout_stream_id_t
318 vlc_fourcc_t b_transcode;
319 sout_format_t f_src; /* only if transcoding */
320 sout_format_t f_dst; /* " " " */
321 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
323 /* id of the out stream */
327 encoder_t *p_encoder;
331 AVCodecContext *ff_dec_c;
334 vlc_fourcc_t b_enc_inited;
336 AVCodecContext *ff_enc_c;
343 uint8_t *p_buffer_in;
350 int i_buffer_out_pos;
351 uint8_t *p_buffer_out;
354 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
355 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
356 AVFrame *p_ff_pic_tmp2; /* to do resample */
358 ImgReSampleContext *p_vresample;
360 #ifdef HAVE_VORBIS_VORBISENC_H
364 vorbis_dsp_state *p_vd;
366 vorbis_comment *p_vc;
367 int i_last_block_size;
369 vlc_bool_t b_headers_sent;
374 static sout_stream_id_t * Add( sout_stream_t *p_stream, sout_format_t *p_fmt )
376 sout_stream_sys_t *p_sys = p_stream->p_sys;
377 sout_stream_id_t *id;
379 id = malloc( sizeof( sout_stream_id_t ) );
382 id->p_encoder = NULL;
384 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
387 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
388 (char*)&p_fmt->i_fourcc,
389 (char*)&p_sys->i_acodec );
392 memcpy( &id->f_src, p_fmt, sizeof( sout_format_t ) );
394 /* create dst format */
395 id->f_dst.i_cat = AUDIO_ES;
396 id->f_dst.i_fourcc = p_sys->i_acodec;
397 id->f_dst.i_sample_rate = p_sys->i_sample_rate > 0 ? p_sys->i_sample_rate : id->f_src.i_sample_rate;
398 id->f_dst.i_channels = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.i_channels;
399 id->f_dst.i_bitrate = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
400 id->f_dst.i_block_align = 0;
401 id->f_dst.i_extra_data = 0;
402 id->f_dst.p_extra_data = NULL;
404 /* build decoder -> filter -> encoder */
405 if( transcode_audio_ffmpeg_new( p_stream, id ) )
407 msg_Err( p_stream, "cannot create audio chain" );
412 /* open output stream */
413 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
414 id->b_transcode = VLC_TRUE;
422 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
425 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
426 (char*)&p_fmt->i_fourcc,
427 (char*)&p_sys->i_vcodec );
429 memcpy( &id->f_src, p_fmt, sizeof( sout_format_t ) );
431 /* create dst format */
432 id->f_dst.i_cat = VIDEO_ES;
433 id->f_dst.i_fourcc = p_sys->i_vcodec;
434 id->f_dst.i_width = p_sys->i_width ; /* > 0 ? p_sys->i_width : id->f_src.i_width; */
435 id->f_dst.i_height = p_sys->i_height; /* > 0 ? p_sys->i_height: id->f_src.i_height; */
436 id->f_dst.i_bitrate = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
437 id->f_dst.i_extra_data = 0;
438 id->f_dst.p_extra_data = NULL;
440 /* build decoder -> filter -> encoder */
441 if( transcode_video_ffmpeg_new( p_stream, id ) )
443 msg_Err( p_stream, "cannot create video chain" );
448 /* open output stream */
449 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
451 id->b_transcode = VLC_TRUE;
455 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_fourcc );
456 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
457 id->b_transcode = VLC_FALSE;
469 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
471 sout_stream_sys_t *p_sys = p_stream->p_sys;
473 if( id->b_transcode )
475 if( id->f_src.i_cat == AUDIO_ES )
477 transcode_audio_ffmpeg_close( p_stream, id );
479 else if( id->f_src.i_cat == VIDEO_ES )
481 transcode_video_ffmpeg_close( p_stream, id );
485 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
491 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
492 sout_buffer_t *p_buffer )
494 sout_stream_sys_t *p_sys = p_stream->p_sys;
496 if( id->b_transcode )
498 sout_buffer_t *p_buffer_out;
499 if( id->f_src.i_cat == AUDIO_ES )
501 transcode_audio_ffmpeg_process( p_stream, id, p_buffer, &p_buffer_out );
503 else if( id->f_src.i_cat == VIDEO_ES )
505 transcode_video_ffmpeg_process( p_stream, id, p_buffer, &p_buffer_out );
507 sout_BufferDelete( p_stream->p_sout, p_buffer );
511 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
515 else if( id->id != NULL )
517 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
521 sout_BufferDelete( p_stream->p_sout, p_buffer );
526 /****************************************************************************
527 * ffmpeg decoder reencocdr part
528 ****************************************************************************/
534 } fourcc_to_ff_code[] =
537 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
538 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
539 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
540 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
541 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
542 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
543 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
544 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
545 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
548 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
549 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
550 #if LIBAVCODEC_BUILD >= 4676
551 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
553 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
554 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
555 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
556 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
557 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
558 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
559 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
560 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
561 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
562 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
563 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
564 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
565 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
566 #if LIBAVCODEC_BUILD >= 4666
567 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
570 /* raw video code, only used for 'encoding' */
571 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
572 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
573 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
574 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
575 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
576 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
577 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
578 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
580 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
583 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
587 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
589 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
591 return fourcc_to_ff_code[i].i_ff_codec;
598 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
602 case VLC_FOURCC( 'I', '4', '2', '0' ):
603 return PIX_FMT_YUV420P;
604 case VLC_FOURCC( 'I', '4', '2', '2' ):
605 return PIX_FMT_YUV422P;
606 case VLC_FOURCC( 'I', '4', '4', '4' ):
607 return PIX_FMT_YUV444P;
608 case VLC_FOURCC( 'R', 'V', '1', '5' ):
609 return PIX_FMT_RGB555;
610 case VLC_FOURCC( 'R', 'V', '1', '6' ):
611 return PIX_FMT_RGB565;
612 case VLC_FOURCC( 'R', 'V', '2', '4' ):
613 return PIX_FMT_RGB24;
614 case VLC_FOURCC( 'R', 'V', '3', '2' ):
615 return PIX_FMT_RGBA32;
616 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
617 return PIX_FMT_GRAY8;
618 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
619 return PIX_FMT_YUV422;
625 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
626 sout_stream_id_t *id )
630 if( id->f_src.i_fourcc == VLC_FOURCC('s','1','6','l') ||
631 id->f_src.i_fourcc == VLC_FOURCC('s','1','6','b') ||
632 id->f_src.i_fourcc == VLC_FOURCC('s','8',' ',' ') ||
633 id->f_src.i_fourcc == VLC_FOURCC('u','8',' ',' ') )
637 id->ff_dec_c = avcodec_alloc_context();
638 id->ff_dec_c->sample_rate = id->f_src.i_sample_rate;
639 id->ff_dec_c->channels = id->f_src.i_channels;
640 id->ff_dec_c->block_align = id->f_src.i_block_align;
641 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
646 i_ff_codec = get_ff_codec( id->f_src.i_fourcc );
647 if( i_ff_codec == 0 )
649 msg_Err( p_stream, "cannot find decoder id" );
653 id->ff_dec = avcodec_find_decoder( i_ff_codec );
656 msg_Err( p_stream, "cannot find decoder (avcodec)" );
660 id->ff_dec_c = avcodec_alloc_context();
661 id->ff_dec_c->sample_rate = id->f_src.i_sample_rate;
662 id->ff_dec_c->channels = id->f_src.i_channels;
663 id->ff_dec_c->block_align = id->f_src.i_block_align;
664 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
666 id->ff_dec_c->extradata_size = id->f_src.i_extra_data;
667 id->ff_dec_c->extradata = id->f_src.p_extra_data;
668 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
670 msg_Err( p_stream, "cannot open decoder" );
676 id->i_buffer_in = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
677 id->i_buffer_in_pos = 0;
678 id->p_buffer_in = malloc( id->i_buffer_in );
680 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
681 id->i_buffer_pos = 0;
682 id->p_buffer = malloc( id->i_buffer );
684 id->i_buffer_out = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
685 id->i_buffer_out_pos = 0;
686 id->p_buffer_out = malloc( id->i_buffer_out );
688 /* Sanity check for audio channels */
689 id->f_dst.i_channels = __MIN( id->f_dst.i_channels, id->f_src.i_channels );
691 #ifdef HAVE_VORBIS_VORBISENC_H
692 if( id->f_dst.i_fourcc == VLC_FOURCC('v','o','r','b') )
694 id->p_vi = (vorbis_info *)malloc( sizeof(vorbis_info) );
695 id->p_vd = (vorbis_dsp_state *)malloc( sizeof(vorbis_dsp_state) );
696 id->p_vb = (vorbis_block *)malloc( sizeof(vorbis_block) );
697 id->p_vc = (vorbis_comment *)malloc( sizeof(vorbis_comment) );
699 vorbis_info_init( id->p_vi );
701 if( vorbis_encode_setup_managed( id->p_vi, id->f_dst.i_channels,
702 id->f_dst.i_sample_rate, -1, id->f_dst.i_bitrate, -1 ) ||
703 vorbis_encode_ctl( id->p_vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
704 vorbis_encode_setup_init( id->p_vi ) ){}
707 vorbis_comment_init( id->p_vc);
708 vorbis_comment_add_tag( id->p_vc, "ENCODER", "VLC media player");
710 /* set up the analysis state and auxiliary encoding storage */
711 vorbis_analysis_init( id->p_vd, id->p_vi );
712 vorbis_block_init( id->p_vd, id->p_vb );
714 id->b_headers_sent = VLC_FALSE;
715 id->i_last_block_size = 0;
716 id->i_samples_delay = 0;
722 i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
723 if( i_ff_codec == 0 )
725 msg_Err( p_stream, "cannot find encoder id" );
729 id->ff_enc = avcodec_find_encoder( i_ff_codec );
732 msg_Err( p_stream, "cannot find encoder (avcodec)" );
736 /* Hack for mp3 transcoding support */
737 if( id->f_dst.i_fourcc == VLC_FOURCC( 'm','p','3',' ' ) )
739 id->f_dst.i_fourcc = VLC_FOURCC( 'm','p','g','a' );
742 id->ff_enc_c = avcodec_alloc_context();
743 id->ff_enc_c->bit_rate = id->f_dst.i_bitrate;
744 id->ff_enc_c->sample_rate = id->f_dst.i_sample_rate;
745 id->ff_enc_c->channels = id->f_dst.i_channels;
747 /* Make sure we get extradata filled by the encoder */
748 id->ff_enc_c->extradata_size = 0;
749 id->ff_enc_c->extradata = NULL;
750 id->ff_enc_c->flags |= CODEC_FLAG_GLOBAL_HEADER;
752 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
754 if( id->ff_enc_c->channels > 2 )
756 id->ff_enc_c->channels = 2;
757 id->f_dst.i_channels = 2;
758 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
760 msg_Err( p_stream, "cannot open encoder" );
763 msg_Warn( p_stream, "stereo mode selected (codec limitation)" );
767 msg_Err( p_stream, "cannot open encoder" );
772 id->f_dst.i_extra_data = id->ff_enc_c->extradata_size;
773 id->f_dst.p_extra_data = id->ff_enc_c->extradata;
774 id->ff_enc_c->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
779 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
780 sout_stream_id_t *id )
784 avcodec_close( id->ff_dec_c );
787 #ifdef HAVE_VORBIS_VORBISENC_H
788 if( id->f_dst.i_fourcc == VLC_FOURCC('v','o','r','b') )
790 vorbis_block_clear( id->p_vb );
791 vorbis_dsp_clear( id->p_vd );
792 vorbis_comment_clear( id->p_vc );
793 vorbis_info_clear( id->p_vi ); /* must be called last */
802 avcodec_close( id->ff_enc_c );
804 free( id->ff_dec_c );
805 if( id->f_dst.i_fourcc != VLC_FOURCC('v','o','r','b') )
806 free( id->ff_enc_c );
808 free( id->p_buffer_in );
809 free( id->p_buffer );
810 free( id->p_buffer_out );
813 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
814 sout_stream_id_t *id,
816 sout_buffer_t **out )
818 vlc_bool_t b_again = VLC_FALSE;
822 /* gather data into p_buffer_in */
823 #ifdef HAVE_VORBIS_VORBISENC_H
824 if( id->f_dst.i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
825 id->i_dts = in->i_dts -
826 (mtime_t)1000000 * (mtime_t)id->i_samples_delay /
827 (mtime_t)id->f_dst.i_sample_rate;
830 id->i_dts = in->i_dts -
832 (mtime_t)(id->i_buffer_pos / 2 / id->ff_dec_c->channels )/
833 (mtime_t)id->ff_dec_c->sample_rate;
835 if( id->i_buffer_in_pos + (int)in->i_size > id->i_buffer_in )
837 /* extend buffer_in */
838 id->i_buffer_in = id->i_buffer_in_pos + in->i_size + 1024;
839 id->p_buffer_in = realloc( id->p_buffer_in, id->i_buffer_in );
841 memcpy( &id->p_buffer_in[id->i_buffer_in_pos],
842 in->p_buffer, in->i_size );
843 id->i_buffer_in_pos += in->i_size;
849 /* decode as much data as possible */
857 i_buffer_size = id->i_buffer - id->i_buffer_pos;
859 i_used = avcodec_decode_audio( id->ff_dec_c,
860 (int16_t*)&id->p_buffer[id->i_buffer_pos],
861 &i_buffer_size, id->p_buffer_in,
862 id->i_buffer_in_pos );
864 /* msg_Warn( p_stream, "avcodec_decode_audio: %d used",
866 id->i_buffer_pos += i_buffer_size;
870 msg_Warn( p_stream, "error");
871 id->i_buffer_in_pos = 0;
874 else if( i_used < id->i_buffer_in_pos )
876 memmove( id->p_buffer_in,
877 &id->p_buffer_in[i_used],
878 id->i_buffer_in - i_used );
879 id->i_buffer_in_pos -= i_used;
883 id->i_buffer_in_pos = 0;
887 if( id->i_buffer_pos >= AVCODEC_MAX_AUDIO_FRAME_SIZE )
897 int16_t *sout = (int16_t*)&id->p_buffer[id->i_buffer_pos];
900 if( id->f_src.i_fourcc == VLC_FOURCC( 's', '8', ' ', ' ' ) )
902 int8_t *sin = (int8_t*)id->p_buffer_in;
903 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos )
904 / 2, id->i_buffer_in_pos );
906 while( i_samples > 0 )
908 *sout++ = ( *sin++ ) << 8;
912 else if( id->f_src.i_fourcc == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
914 int8_t *sin = (int8_t*)id->p_buffer_in;
915 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos )
916 / 2, id->i_buffer_in_pos );
918 while( i_samples > 0 )
920 *sout++ = ( *sin++ - 128 ) << 8;
924 else if( id->f_src.i_fourcc == VLC_FOURCC( 's', '1', '6', 'l' ) )
926 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos )
927 / 2, id->i_buffer_in_pos / 2);
928 #ifdef WORDS_BIGENDIAN
929 uint8_t *sin = (uint8_t*)id->p_buffer_in;
930 i_used = i_samples * 2;
931 while( i_samples > 0 )
937 *sout++ = *(int16_t*)tmp;
942 memcpy( sout, id->p_buffer_in, i_samples * 2 );
944 i_used = i_samples * 2;
947 else if( id->f_src.i_fourcc == VLC_FOURCC( 's', '1', '6', 'b' ) )
949 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos )
950 / 2, id->i_buffer_in_pos / 2);
951 #ifdef WORDS_BIGENDIAN
952 memcpy( sout, id->p_buffer_in, i_samples * 2 );
954 i_used = i_samples * 2;
956 uint8_t *sin = (uint8_t*)id->p_buffer_in;
957 i_used = i_samples * 2;
958 while( i_samples > 0 )
964 *sout++ = *(int16_t*)tmp;
970 id->i_buffer_pos = (uint8_t*)sout - id->p_buffer;
971 if( i_used < id->i_buffer_in_pos )
973 memmove( id->p_buffer_in,
974 &id->p_buffer_in[i_used],
975 id->i_buffer_in - i_used );
977 id->i_buffer_in_pos -= i_used;
980 i_buffer_pos = id->i_buffer_pos;
982 /* encode as much data as possible */
984 #ifdef HAVE_VORBIS_VORBISENC_H
985 if( id->i_buffer_pos == 0 );
986 else if( id->f_dst.i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
990 sout_buffer_t *p_out;
993 if( !id->b_headers_sent )
995 ogg_packet header[3];
996 vorbis_analysis_headerout( id->p_vd, id->p_vc,
997 &header[0], &header[1], &header[2]);
998 for( i = 0; i < 3; i++ )
1000 p_out = sout_BufferNew( p_stream->p_sout, header[i].bytes);
1001 memcpy( p_out->p_buffer, header[i].packet,
1004 p_out->i_size = header[i].bytes;
1005 p_out->i_length = 0;
1007 p_out->i_dts = p_out->i_pts = id->i_dts;
1009 sout_BufferChain( out, p_out );
1011 id->b_headers_sent = VLC_TRUE;
1014 i_samples = id->i_buffer_pos / id->f_src.i_channels / 2;
1015 id->i_samples_delay += i_samples;
1016 id->i_buffer_pos = 0;
1018 buffer = vorbis_analysis_buffer( id->p_vd, i_samples );
1020 /* convert samples to float and uninterleave */
1021 for( i = 0; i < id->f_dst.i_channels; i++ )
1023 for( j = 0 ; j < i_samples ; j++ )
1025 buffer[i][j]= ((float)( ((int16_t *)id->p_buffer)
1026 [j*id->f_src.i_channels + i ] ))/ 32768.f;
1030 vorbis_analysis_wrote( id->p_vd, i_samples );
1032 while( vorbis_analysis_blockout( id->p_vd, id->p_vb ) == 1 )
1034 vorbis_analysis( id->p_vb, NULL );
1035 vorbis_bitrate_addblock( id->p_vb );
1037 while( vorbis_bitrate_flushpacket( id->p_vd, &op ) )
1040 p_out = sout_BufferNew( p_stream->p_sout, op.bytes );
1041 memcpy( p_out->p_buffer, op.packet, op.bytes );
1043 i_block_size = vorbis_packet_blocksize( id->p_vi, &op );
1045 if( i_block_size < 0 ) i_block_size = 0;
1046 i_samples = ( id->i_last_block_size +
1047 i_block_size ) >> 2;
1048 id->i_last_block_size = i_block_size;
1050 p_out->i_size = op.bytes;
1051 p_out->i_length = (mtime_t)1000000 *
1052 (mtime_t)i_samples /
1053 (mtime_t)id->f_dst.i_sample_rate;
1055 //msg_Err( p_stream, "i_dts: %lld", id->i_dts );
1058 p_out->i_dts = id->i_dts;
1059 p_out->i_pts = id->i_dts;
1061 id->i_samples_delay -= i_samples;
1064 id->i_dts += p_out->i_length;
1065 sout_BufferChain( out, p_out );
1075 int i_frame_size = id->ff_enc_c->frame_size * 2 *
1076 id->ff_dec_c->channels;
1077 int i_out_size, i, j;
1078 sout_buffer_t *p_out;
1079 int16_t *p_buffer = (int16_t *)(id->p_buffer + i_buffer_pos -
1082 if( id->i_buffer_pos < i_frame_size )
1087 if( id->ff_dec_c->channels != id->ff_enc_c->channels )
1089 /* dumb downmixing */
1090 for( i = 0; i < id->ff_enc_c->frame_size; i++ )
1092 for( j = 0 ; j < id->f_dst.i_channels; j++ )
1094 p_buffer[i*id->f_dst.i_channels+j] = p_buffer[i*id->f_src.i_channels+j];
1099 /* msg_Warn( p_stream, "avcodec_encode_audio: frame size%d",
1101 i_out_size = avcodec_encode_audio( id->ff_enc_c,
1102 id->p_buffer_out, id->i_buffer_out, p_buffer );
1104 if( i_out_size <= 0 )
1109 id->i_buffer_pos -= i_frame_size;
1111 p_out = sout_BufferNew( p_stream->p_sout, i_out_size );
1112 memcpy( p_out->p_buffer, id->p_buffer_out, i_out_size );
1113 p_out->i_size = i_out_size;
1114 p_out->i_length = (mtime_t)1000000 *
1115 (mtime_t)id->ff_enc_c->frame_size /
1116 (mtime_t)id->ff_enc_c->sample_rate;
1119 p_out->i_dts = id->i_dts;
1120 p_out->i_pts = id->i_dts;
1123 id->i_dts += p_out->i_length;
1125 /* msg_Warn( p_stream, "frame dts=%lld len %lld out=%d",
1126 p_out->i_dts, p_out->i_length, i_out_size ); */
1128 sout_BufferChain( out, p_out );
1131 /* Copy the remaining raw samples */
1132 if( id->i_buffer_pos != 0 )
1134 memmove( id->p_buffer,
1135 &id->p_buffer[i_buffer_pos - id->i_buffer_pos],
1148 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1149 sout_stream_id_t *id )
1151 sout_stream_sys_t *p_sys = p_stream->p_sys;
1155 if( id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1156 id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1157 id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1158 id->f_src.i_fourcc == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1159 id->f_src.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1160 id->f_src.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1161 id->f_src.i_fourcc == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1162 id->f_src.i_fourcc == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1163 id->f_src.i_fourcc == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1166 id->ff_dec_c = avcodec_alloc_context();
1167 id->ff_dec_c->width = id->f_src.i_width;
1168 id->ff_dec_c->height = id->f_src.i_height;
1169 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_fourcc );
1174 i_ff_codec = get_ff_codec( id->f_src.i_fourcc );
1175 if( i_ff_codec == 0 )
1177 msg_Err( p_stream, "cannot find decoder" );
1178 return VLC_EGENERIC;
1181 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1184 msg_Err( p_stream, "cannot find decoder" );
1185 return VLC_EGENERIC;
1188 id->ff_dec_c = avcodec_alloc_context();
1189 id->ff_dec_c->width = id->f_src.i_width;
1190 id->ff_dec_c->height = id->f_src.i_height;
1191 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1192 id->ff_dec_c->extradata_size= id->f_src.i_extra_data;
1193 id->ff_dec_c->extradata = id->f_src.p_extra_data;
1194 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1195 id->ff_dec_c->error_resilience= -1;
1196 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1197 id->ff_dec_c->opaque = p_sys;
1199 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1201 msg_Err( p_stream, "cannot open decoder" );
1202 return VLC_EGENERIC;
1205 if( i_ff_codec == CODEC_ID_MPEG4 && id->ff_dec_c->extradata_size > 0 )
1209 uint8_t *p_vol = malloc( id->ff_dec_c->extradata_size +
1210 FF_INPUT_BUFFER_PADDING_SIZE );
1212 memcpy( p_vol, id->ff_dec_c->extradata,
1213 id->ff_dec_c->extradata_size );
1214 memset( p_vol + id->ff_dec_c->extradata_size, 0,
1215 FF_INPUT_BUFFER_PADDING_SIZE );
1217 avcodec_decode_video( id->ff_dec_c, &frame, &b_gotpicture,
1218 id->ff_dec_c->extradata,
1219 id->ff_dec_c->extradata_size );
1226 id->ff_enc = id->ff_enc_c = NULL;
1227 i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
1228 if( i_ff_codec != 0 )
1230 id->ff_enc = avcodec_find_encoder( i_ff_codec );
1233 /* Hack for external encoders */
1236 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1237 id->p_encoder->i_fourcc = id->f_dst.i_fourcc;
1238 id->p_encoder->format.video.i_width = p_sys->i_width;
1239 id->p_encoder->format.video.i_height = p_sys->i_height;
1240 id->p_encoder->i_bitrate = p_sys->i_vbitrate;
1242 if( id->p_encoder->format.video.i_width <= 0 )
1244 id->p_encoder->format.video.i_width = id->f_dst.i_width =
1245 id->ff_dec_c->width - p_sys->i_crop_left -
1246 p_sys->i_crop_right;
1248 if( id->p_encoder->format.video.i_height <= 0 )
1250 id->p_encoder->format.video.i_height = id->f_dst.i_height =
1251 id->ff_dec_c->height - p_sys->i_crop_top -
1252 p_sys->i_crop_bottom;
1255 id->p_encoder->p_module =
1256 module_Need( id->p_encoder, "video encoder", NULL );
1258 if( !id->p_encoder->p_module )
1260 free( id->p_encoder );
1261 id->p_encoder = NULL;
1264 /* End hack for external encoders */
1266 if( !id->ff_enc && !id->p_encoder )
1268 msg_Err( p_stream, "cannot find encoder" );
1269 return VLC_EGENERIC;
1272 /* XXX open it only when we have the first frame */
1273 id->b_enc_inited = VLC_FALSE;
1274 id->i_buffer_in = 0;
1275 id->i_buffer_in_pos = 0;
1276 id->p_buffer_in = NULL;
1278 id->i_buffer = 3*1024*1024;
1279 id->i_buffer_pos = 0;
1280 id->p_buffer = malloc( id->i_buffer );
1282 id->i_buffer_out = 0;
1283 id->i_buffer_out_pos = 0;
1284 id->p_buffer_out = NULL;
1286 id->p_ff_pic = avcodec_alloc_frame();
1287 id->p_ff_pic_tmp0 = NULL;
1288 id->p_ff_pic_tmp1 = NULL;
1289 id->p_ff_pic_tmp2 = NULL;
1290 id->p_vresample = NULL;
1292 p_sys->i_last_ref_pts = 0;
1293 p_sys->i_buggy_pts_detect = 0;
1295 /* This is enough for external encoders */
1296 if( id->p_encoder && id->p_encoder->p_module ) return VLC_SUCCESS;
1298 if( id->f_dst.i_fourcc == VLC_FOURCC( 'm','p','1','v' )||
1299 id->f_dst.i_fourcc == VLC_FOURCC( 'm','p','2','v' ) )
1301 id->f_dst.i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1304 id->ff_enc_c = avcodec_alloc_context();
1305 id->ff_enc_c->width = id->f_dst.i_width;
1306 id->ff_enc_c->height = id->f_dst.i_height;
1307 id->ff_enc_c->bit_rate = id->f_dst.i_bitrate;
1311 id->ff_enc_c->frame_rate = id->ff_dec_c->frame_rate;
1312 #if LIBAVCODEC_BUILD >= 4662
1313 id->ff_enc_c->frame_rate_base= id->ff_dec_c->frame_rate_base;
1318 #if LIBAVCODEC_BUILD >= 4662
1319 id->ff_enc_c->frame_rate = 25 ; /* FIXME as it break mpeg */
1320 id->ff_enc_c->frame_rate_base= 1;
1322 id->ff_enc_c->frame_rate = 25 * FRAME_RATE_BASE;
1326 id->ff_enc_c->gop_size = p_sys->i_key_int >= 0 ? p_sys->i_key_int : 50;
1327 id->ff_enc_c->max_b_frames = __MIN( p_sys->i_b_frames, FF_MAX_B_FRAMES );
1328 id->ff_enc_c->b_frame_strategy = 0;
1329 id->ff_enc_c->b_quant_factor = 2.0;
1331 if( p_sys->i_vtolerance >= 0 )
1333 id->ff_enc_c->bit_rate_tolerance = p_sys->i_vtolerance;
1335 id->ff_enc_c->qmin = p_sys->i_qmin;
1336 id->ff_enc_c->qmax = p_sys->i_qmax;
1338 #if LIBAVCODEC_BUILD >= 4673
1339 id->ff_enc_c->mb_decision = p_sys->i_hq;
1343 id->ff_enc_c->flags |= CODEC_FLAG_HQ;
1347 if( i_ff_codec == CODEC_ID_RAWVIDEO )
1349 id->ff_enc_c->pix_fmt = get_ff_chroma( id->f_dst.i_fourcc );
1355 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream, sout_stream_id_t *id )
1359 avcodec_close( id->ff_dec_c );
1363 /* External encoding */
1364 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1365 vlc_object_destroy( id->p_encoder->p_module );
1367 else if( id->b_enc_inited )
1369 avcodec_close( id->ff_enc_c );
1374 free( id->p_ff_pic );
1377 if( id->p_ff_pic_tmp0 )
1379 free( id->p_ff_pic_tmp0->data[0] );
1380 free( id->p_ff_pic_tmp0 );
1382 if( id->p_ff_pic_tmp1)
1384 free( id->p_ff_pic_tmp1->data[0] );
1385 free( id->p_ff_pic_tmp1 );
1387 if( id->p_ff_pic_tmp2)
1389 free( id->p_ff_pic_tmp2->data[0] );
1390 free( id->p_ff_pic_tmp2 );
1392 if( id->p_vresample )
1394 free( id->p_vresample );
1397 free( id->ff_dec_c );
1398 if( id->ff_enc_c ) free( id->ff_enc_c );
1399 free( id->p_buffer );
1402 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1403 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1405 sout_stream_sys_t *p_sys = p_stream->p_sys;
1416 i_data = in->i_size;
1417 p_data = in->p_buffer;
1422 frame = id->p_ff_pic;
1423 p_sys->i_input_pts = in->i_pts;
1426 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1433 avpicture_fill( (AVPicture*)frame,
1435 id->ff_dec_c->pix_fmt,
1436 id->ff_dec_c->width, id->ff_dec_c->height );
1441 frame->pts = p_sys->i_input_pts;
1446 msg_Warn( p_stream, "error");
1447 return VLC_EGENERIC;
1457 /* Get the pts of the decoded frame if any, otherwise keep the
1458 * interpolated one */
1459 if( frame->pts > 0 )
1461 p_sys->i_output_pts = frame->pts;
1464 if( !id->b_enc_inited && id->p_encoder )
1468 /* XXX hack because of copy packetizer and mpeg4video that can fail
1471 if( id->p_encoder->i_width <= 0 )
1473 id->p_encoder->i_width = id->f_dst.i_width =
1474 id->ff_dec_c->width - p_sys->i_crop_left -
1475 p_sys->i_crop_right;
1477 if( id->p_encoder->i_height <= 0 )
1479 id->p_encoder->i_height = id->f_dst.i_height =
1480 id->ff_dec_c->height - p_sys->i_crop_top -
1481 p_sys->i_crop_bottom;
1485 id->p_encoder->i_bitrate = p_sys->i_vbitrate;
1487 if( !( id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out, &id->f_dst ) ) )
1489 msg_Err( p_stream, "cannot add this stream" );
1490 id->b_transcode = VLC_FALSE;
1491 return VLC_EGENERIC;
1494 while( (p_block = id->p_encoder->pf_header( id->p_encoder )) )
1496 sout_buffer_t *p_out;
1497 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1498 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1499 p_out->i_dts = in->i_dts;
1500 p_out->i_pts = in->i_dts;
1501 sout_BufferChain( out, p_out );
1504 id->i_inter_pixfmt =
1505 get_ff_chroma( id->p_encoder->format.video.i_chroma );
1507 id->b_enc_inited = VLC_TRUE;
1509 else if( !id->b_enc_inited )
1511 /* XXX hack because of copy packetizer and mpeg4video that can fail
1513 if( id->ff_enc_c->width <= 0 )
1515 id->ff_enc_c->width =
1516 id->f_dst.i_width = id->ff_dec_c->width - p_sys->i_crop_left - p_sys->i_crop_right;
1518 if( id->ff_enc_c->height <= 0 )
1520 id->ff_enc_c->height =
1521 id->f_dst.i_height = id->ff_dec_c->height - p_sys->i_crop_top - p_sys->i_crop_bottom;
1524 /* Make sure we get extradata filled by the encoder */
1525 id->ff_enc_c->extradata_size = 0;
1526 id->ff_enc_c->extradata = NULL;
1527 id->ff_enc_c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1529 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
1531 msg_Err( p_stream, "cannot open encoder" );
1532 return VLC_EGENERIC;
1535 id->f_dst.i_extra_data = id->ff_enc_c->extradata_size;
1536 id->f_dst.p_extra_data = id->ff_enc_c->extradata;
1537 id->ff_enc_c->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
1539 if( !( id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out, &id->f_dst ) ) )
1541 msg_Err( p_stream, "cannot add this stream" );
1542 transcode_video_ffmpeg_close( p_stream, id );
1543 id->b_transcode = VLC_FALSE;
1544 return VLC_EGENERIC;
1547 id->i_inter_pixfmt = id->ff_enc_c->pix_fmt;
1549 id->b_enc_inited = VLC_TRUE;
1554 if( p_stream->p_sys->b_deinterlace )
1556 if( id->p_ff_pic_tmp0 == NULL )
1560 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1561 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1562 id->ff_dec_c->width, id->ff_dec_c->height );
1564 buf = malloc( i_size );
1566 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1568 id->ff_dec_c->width, id->ff_dec_c->height );
1571 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1572 id->ff_dec_c->pix_fmt,
1573 id->ff_dec_c->width, id->ff_dec_c->height );
1575 frame = id->p_ff_pic_tmp0;
1578 /* convert pix format */
1579 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1581 if( id->p_ff_pic_tmp1 == NULL )
1585 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1586 i_size = avpicture_get_size( id->i_inter_pixfmt,
1587 id->ff_dec_c->width,
1588 id->ff_dec_c->height );
1590 buf = malloc( i_size );
1592 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1594 id->ff_dec_c->width, id->ff_dec_c->height );
1597 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1598 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1599 id->ff_dec_c->width, id->ff_dec_c->height );
1601 frame = id->p_ff_pic_tmp1;
1604 /* convert size and crop */
1605 if( id->ff_dec_c->width != id->f_dst.i_width ||
1606 id->ff_dec_c->height != id->f_dst.i_height ||
1607 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1608 p_sys->i_crop_left > 0 || p_sys->i_crop_right )
1610 if( id->p_ff_pic_tmp2 == NULL )
1614 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1615 i_size = avpicture_get_size( id->i_inter_pixfmt,
1617 id->f_dst.i_height );
1619 buf = malloc( i_size );
1621 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1623 id->f_dst.i_width, id->f_dst.i_height );
1626 img_resample_full_init( id->f_dst.i_width,
1628 id->ff_dec_c->width, id->ff_dec_c->height,
1629 p_stream->p_sys->i_crop_top,
1630 p_stream->p_sys->i_crop_bottom,
1631 p_stream->p_sys->i_crop_left,
1632 p_stream->p_sys->i_crop_right );
1635 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1636 (AVPicture*)frame );
1638 frame = id->p_ff_pic_tmp2;
1641 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
1642 if( id->p_encoder ||
1643 id->f_dst.i_fourcc == VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
1644 frame->pts = p_sys->i_output_pts;
1648 /* Interpolate the next PTS
1649 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1650 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1652 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1653 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1658 /* External encoding */
1663 vout_InitPicture( VLC_OBJECT(p_stream), &pic,
1664 id->p_encoder->format.video.i_chroma,
1665 id->f_dst.i_width, id->f_dst.i_height,
1666 id->f_dst.i_width * VOUT_ASPECT_FACTOR /
1667 id->f_dst.i_height );
1669 for( i_plane = 0; i_plane < pic.i_planes; i_plane++ )
1671 pic.p[i_plane].p_pixels = frame->data[i_plane];
1672 pic.p[i_plane].i_pitch = frame->linesize[i_plane];
1675 pic.date = frame->pts;
1677 p_block = id->p_encoder->pf_encode_video( id->p_encoder, &pic );
1680 sout_buffer_t *p_out;
1681 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1682 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1683 p_out->i_dts = p_block->i_dts;
1684 p_out->i_pts = p_block->i_pts;
1685 sout_BufferChain( out, p_out );
1686 block_Release( p_block );
1692 /* Let ffmpeg select the frame type */
1693 frame->pict_type = 0;
1695 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer,
1696 id->i_buffer, frame );
1699 sout_buffer_t *p_out;
1700 p_out = sout_BufferNew( p_stream->p_sout, i_out );
1702 memcpy( p_out->p_buffer, id->p_buffer, i_out );
1704 p_out->i_size = i_out;
1706 if( id->ff_enc_c->coded_frame->pts != 0 &&
1707 p_sys->i_buggy_pts_detect != id->ff_enc_c->coded_frame->pts )
1709 p_sys->i_buggy_pts_detect = id->ff_enc_c->coded_frame->pts;
1711 /* FIXME, 3-2 pulldown is not handled correctly */
1712 p_out->i_length = in->i_length;
1713 p_out->i_pts = id->ff_enc_c->coded_frame->pts;
1715 if( !id->ff_enc_c->delay ||
1716 ( id->ff_enc_c->coded_frame->pict_type != FF_I_TYPE &&
1717 id->ff_enc_c->coded_frame->pict_type != FF_P_TYPE ) )
1719 p_out->i_dts = p_out->i_pts;
1723 if( p_sys->i_last_ref_pts )
1725 p_out->i_dts = p_sys->i_last_ref_pts;
1729 /* Let's put something sensible */
1730 p_out->i_dts = p_out->i_pts;
1733 p_sys->i_last_ref_pts = p_out->i_pts;
1738 /* Buggy libavcodec which doesn't update coded_frame->pts
1740 p_out->i_length = in->i_length;
1741 p_out->i_dts = in->i_dts;
1742 p_out->i_pts = in->i_dts;
1745 sout_BufferChain( out, p_out );
1757 /*****************************************************************************
1758 * transcode_video_ffmpeg_getframebuf:
1760 * Callback used by ffmpeg to get a frame buffer.
1761 * We use it to get the right PTS for each decoded picture.
1762 *****************************************************************************/
1763 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1766 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1769 p_frame->pts = p_sys->i_input_pts;
1771 return avcodec_default_get_buffer( p_context, p_frame );