1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: transcode.c,v 1.35 2003/10/08 21:01:07 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; /* " " " */
322 /* id of the out stream */
326 encoder_t *p_encoder;
330 AVCodecContext *ff_dec_c;
333 vlc_fourcc_t b_enc_inited;
335 AVCodecContext *ff_enc_c;
342 uint8_t *p_buffer_in;
349 int i_buffer_out_pos;
350 uint8_t *p_buffer_out;
353 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
354 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
355 AVFrame *p_ff_pic_tmp2; /* to do resample */
357 ImgReSampleContext *p_vresample;
359 #ifdef HAVE_VORBIS_VORBISENC_H
363 vorbis_dsp_state *p_vd;
365 vorbis_comment *p_vc;
366 int i_last_block_size;
368 vlc_bool_t b_headers_sent;
373 static sout_stream_id_t * Add( sout_stream_t *p_stream, sout_format_t *p_fmt )
375 sout_stream_sys_t *p_sys = p_stream->p_sys;
376 sout_stream_id_t *id;
378 id = malloc( sizeof( sout_stream_id_t ) );
381 id->p_encoder = NULL;
383 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
386 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
387 (char*)&p_fmt->i_fourcc,
388 (char*)&p_sys->i_acodec );
391 memcpy( &id->f_src, p_fmt, sizeof( sout_format_t ) );
393 /* create dst format */
394 id->f_dst.i_cat = AUDIO_ES;
395 id->f_dst.i_fourcc = p_sys->i_acodec;
396 id->f_dst.i_sample_rate = p_sys->i_sample_rate > 0 ? p_sys->i_sample_rate : id->f_src.i_sample_rate;
397 id->f_dst.i_channels = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.i_channels;
398 id->f_dst.i_bitrate = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
399 id->f_dst.i_block_align = 0;
400 id->f_dst.i_extra_data = 0;
401 id->f_dst.p_extra_data = NULL;
403 /* build decoder -> filter -> encoder */
404 if( transcode_audio_ffmpeg_new( p_stream, id ) )
406 msg_Err( p_stream, "cannot create audio chain" );
411 /* open output stream */
412 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
413 id->b_transcode = VLC_TRUE;
421 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
424 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
425 (char*)&p_fmt->i_fourcc,
426 (char*)&p_sys->i_vcodec );
428 memcpy( &id->f_src, p_fmt, sizeof( sout_format_t ) );
430 /* create dst format */
431 id->f_dst.i_cat = VIDEO_ES;
432 id->f_dst.i_fourcc = p_sys->i_vcodec;
433 id->f_dst.i_width = p_sys->i_width ; /* > 0 ? p_sys->i_width : id->f_src.i_width; */
434 id->f_dst.i_height = p_sys->i_height; /* > 0 ? p_sys->i_height: id->f_src.i_height; */
435 id->f_dst.i_bitrate = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
436 id->f_dst.i_extra_data = 0;
437 id->f_dst.p_extra_data = NULL;
439 /* build decoder -> filter -> encoder */
440 if( transcode_video_ffmpeg_new( p_stream, id ) )
442 msg_Err( p_stream, "cannot create video chain" );
447 /* open output stream */
448 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
450 id->b_transcode = VLC_TRUE;
454 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_fourcc );
455 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
456 id->b_transcode = VLC_FALSE;
468 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
470 sout_stream_sys_t *p_sys = p_stream->p_sys;
472 if( id->b_transcode )
474 if( id->f_src.i_cat == AUDIO_ES )
476 transcode_audio_ffmpeg_close( p_stream, id );
478 else if( id->f_src.i_cat == VIDEO_ES )
480 transcode_video_ffmpeg_close( p_stream, id );
484 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
490 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
491 sout_buffer_t *p_buffer )
493 sout_stream_sys_t *p_sys = p_stream->p_sys;
495 if( id->b_transcode )
497 sout_buffer_t *p_buffer_out;
498 if( id->f_src.i_cat == AUDIO_ES )
500 transcode_audio_ffmpeg_process( p_stream, id, p_buffer, &p_buffer_out );
502 else if( id->f_src.i_cat == VIDEO_ES )
504 transcode_video_ffmpeg_process( p_stream, id, p_buffer, &p_buffer_out );
506 sout_BufferDelete( p_stream->p_sout, p_buffer );
510 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
514 else if( id->id != NULL )
516 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
520 sout_BufferDelete( p_stream->p_sout, p_buffer );
525 /****************************************************************************
526 * ffmpeg decoder reencocdr part
527 ****************************************************************************/
533 } fourcc_to_ff_code[] =
536 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
537 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
538 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
539 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
540 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
541 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
542 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
543 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
544 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
547 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
548 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
549 #if LIBAVCODEC_BUILD >= 4676
550 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
552 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
553 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
554 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
555 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
556 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
557 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
558 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
559 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
560 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
561 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
562 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
563 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
564 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
565 #if LIBAVCODEC_BUILD >= 4666
566 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
569 /* raw video code, only used for 'encoding' */
570 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
571 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
572 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
573 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
574 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
575 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
576 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
577 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
579 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
582 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
586 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
588 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
590 return fourcc_to_ff_code[i].i_ff_codec;
597 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
601 case VLC_FOURCC( 'I', '4', '2', '0' ):
602 return PIX_FMT_YUV420P;
603 case VLC_FOURCC( 'I', '4', '2', '2' ):
604 return PIX_FMT_YUV422P;
605 case VLC_FOURCC( 'I', '4', '4', '4' ):
606 return PIX_FMT_YUV444P;
607 case VLC_FOURCC( 'R', 'V', '1', '5' ):
608 return PIX_FMT_RGB555;
609 case VLC_FOURCC( 'R', 'V', '1', '6' ):
610 return PIX_FMT_RGB565;
611 case VLC_FOURCC( 'R', 'V', '2', '4' ):
612 return PIX_FMT_RGB24;
613 case VLC_FOURCC( 'R', 'V', '3', '2' ):
614 return PIX_FMT_RGBA32;
615 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
616 return PIX_FMT_GRAY8;
617 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
618 return PIX_FMT_YUV422;
624 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
625 sout_stream_id_t *id )
629 if( id->f_src.i_fourcc == VLC_FOURCC('s','1','6','l') ||
630 id->f_src.i_fourcc == VLC_FOURCC('s','1','6','b') ||
631 id->f_src.i_fourcc == VLC_FOURCC('s','8',' ',' ') ||
632 id->f_src.i_fourcc == VLC_FOURCC('u','8',' ',' ') )
636 id->ff_dec_c = avcodec_alloc_context();
637 id->ff_dec_c->sample_rate = id->f_src.i_sample_rate;
638 id->ff_dec_c->channels = id->f_src.i_channels;
639 id->ff_dec_c->block_align = id->f_src.i_block_align;
640 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
645 i_ff_codec = get_ff_codec( id->f_src.i_fourcc );
646 if( i_ff_codec == 0 )
648 msg_Err( p_stream, "cannot find decoder id" );
652 id->ff_dec = avcodec_find_decoder( i_ff_codec );
655 msg_Err( p_stream, "cannot find decoder (avcodec)" );
659 id->ff_dec_c = avcodec_alloc_context();
660 id->ff_dec_c->sample_rate = id->f_src.i_sample_rate;
661 id->ff_dec_c->channels = id->f_src.i_channels;
662 id->ff_dec_c->block_align = id->f_src.i_block_align;
663 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
665 id->ff_dec_c->extradata_size = id->f_src.i_extra_data;
666 id->ff_dec_c->extradata = id->f_src.p_extra_data;
667 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
669 msg_Err( p_stream, "cannot open decoder" );
675 id->i_buffer_in = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
676 id->i_buffer_in_pos = 0;
677 id->p_buffer_in = malloc( id->i_buffer_in );
679 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
680 id->i_buffer_pos = 0;
681 id->p_buffer = malloc( id->i_buffer );
683 id->i_buffer_out = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
684 id->i_buffer_out_pos = 0;
685 id->p_buffer_out = malloc( id->i_buffer_out );
687 /* Sanity check for audio channels */
688 id->f_dst.i_channels = __MIN( id->f_dst.i_channels, id->f_src.i_channels );
690 #ifdef HAVE_VORBIS_VORBISENC_H
691 if( id->f_dst.i_fourcc == VLC_FOURCC('v','o','r','b') )
693 id->p_vi = (vorbis_info *)malloc( sizeof(vorbis_info) );
694 id->p_vd = (vorbis_dsp_state *)malloc( sizeof(vorbis_dsp_state) );
695 id->p_vb = (vorbis_block *)malloc( sizeof(vorbis_block) );
696 id->p_vc = (vorbis_comment *)malloc( sizeof(vorbis_comment) );
698 vorbis_info_init( id->p_vi );
700 if( vorbis_encode_setup_managed( id->p_vi, id->f_dst.i_channels,
701 id->f_dst.i_sample_rate, -1, id->f_dst.i_bitrate, -1 ) ||
702 vorbis_encode_ctl( id->p_vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
703 vorbis_encode_setup_init( id->p_vi ) ){}
706 vorbis_comment_init( id->p_vc);
707 vorbis_comment_add_tag( id->p_vc, "ENCODER", "VLC media player");
709 /* set up the analysis state and auxiliary encoding storage */
710 vorbis_analysis_init( id->p_vd, id->p_vi );
711 vorbis_block_init( id->p_vd, id->p_vb );
713 id->b_headers_sent = VLC_FALSE;
714 id->i_last_block_size = 0;
715 id->i_samples_delay = 0;
721 i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
722 if( i_ff_codec == 0 )
724 msg_Err( p_stream, "cannot find encoder id" );
728 id->ff_enc = avcodec_find_encoder( i_ff_codec );
731 msg_Err( p_stream, "cannot find encoder (avcodec)" );
735 /* Hack for mp3 transcoding support */
736 if( id->f_dst.i_fourcc == VLC_FOURCC( 'm','p','3',' ' ) )
738 id->f_dst.i_fourcc = VLC_FOURCC( 'm','p','g','a' );
741 id->ff_enc_c = avcodec_alloc_context();
742 id->ff_enc_c->bit_rate = id->f_dst.i_bitrate;
743 id->ff_enc_c->sample_rate = id->f_dst.i_sample_rate;
744 id->ff_enc_c->channels = id->f_dst.i_channels;
746 /* Make sure we get extradata filled by the encoder */
747 id->ff_enc_c->extradata_size = 0;
748 id->ff_enc_c->extradata = NULL;
749 id->ff_enc_c->flags |= CODEC_FLAG_GLOBAL_HEADER;
751 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
753 if( id->ff_enc_c->channels > 2 )
755 id->ff_enc_c->channels = 2;
756 id->f_dst.i_channels = 2;
757 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
759 msg_Err( p_stream, "cannot open encoder" );
762 msg_Warn( p_stream, "stereo mode selected (codec limitation)" );
766 msg_Err( p_stream, "cannot open encoder" );
771 id->f_dst.i_extra_data = id->ff_enc_c->extradata_size;
772 id->f_dst.p_extra_data = id->ff_enc_c->extradata;
773 id->ff_enc_c->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
778 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
779 sout_stream_id_t *id )
783 avcodec_close( id->ff_dec_c );
786 #ifdef HAVE_VORBIS_VORBISENC_H
787 if( id->f_dst.i_fourcc == VLC_FOURCC('v','o','r','b') )
789 vorbis_block_clear( id->p_vb );
790 vorbis_dsp_clear( id->p_vd );
791 vorbis_comment_clear( id->p_vc );
792 vorbis_info_clear( id->p_vi ); /* must be called last */
801 avcodec_close( id->ff_enc_c );
803 free( id->ff_dec_c );
804 if( id->f_dst.i_fourcc != VLC_FOURCC('v','o','r','b') )
805 free( id->ff_enc_c );
807 free( id->p_buffer_in );
808 free( id->p_buffer );
809 free( id->p_buffer_out );
812 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
813 sout_stream_id_t *id,
815 sout_buffer_t **out )
817 vlc_bool_t b_again = VLC_FALSE;
821 /* gather data into p_buffer_in */
822 #ifdef HAVE_VORBIS_VORBISENC_H
823 if( id->f_dst.i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
824 id->i_dts = in->i_dts -
825 (mtime_t)1000000 * (mtime_t)id->i_samples_delay /
826 (mtime_t)id->f_dst.i_sample_rate;
829 id->i_dts = in->i_dts -
831 (mtime_t)(id->i_buffer_pos / 2 / id->ff_dec_c->channels )/
832 (mtime_t)id->ff_dec_c->sample_rate;
834 if( id->i_buffer_in_pos + (int)in->i_size > id->i_buffer_in )
836 /* extend buffer_in */
837 id->i_buffer_in = id->i_buffer_in_pos + in->i_size + 1024;
838 id->p_buffer_in = realloc( id->p_buffer_in, id->i_buffer_in );
840 memcpy( &id->p_buffer_in[id->i_buffer_in_pos],
841 in->p_buffer, in->i_size );
842 id->i_buffer_in_pos += in->i_size;
848 /* decode as much data as possible */
856 i_buffer_size = id->i_buffer - id->i_buffer_pos;
858 i_used = avcodec_decode_audio( id->ff_dec_c,
859 (int16_t*)&id->p_buffer[id->i_buffer_pos],
860 &i_buffer_size, id->p_buffer_in,
861 id->i_buffer_in_pos );
863 /* msg_Warn( p_stream, "avcodec_decode_audio: %d used",
865 id->i_buffer_pos += i_buffer_size;
869 msg_Warn( p_stream, "error");
870 id->i_buffer_in_pos = 0;
873 else if( i_used < id->i_buffer_in_pos )
875 memmove( id->p_buffer_in,
876 &id->p_buffer_in[i_used],
877 id->i_buffer_in - i_used );
878 id->i_buffer_in_pos -= i_used;
882 id->i_buffer_in_pos = 0;
886 if( id->i_buffer_pos >= AVCODEC_MAX_AUDIO_FRAME_SIZE )
896 int16_t *sout = (int16_t*)&id->p_buffer[id->i_buffer_pos];
899 if( id->f_src.i_fourcc == VLC_FOURCC( 's', '8', ' ', ' ' ) )
901 int8_t *sin = (int8_t*)id->p_buffer_in;
902 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos )
903 / 2, id->i_buffer_in_pos );
905 while( i_samples > 0 )
907 *sout++ = ( *sin++ ) << 8;
911 else if( id->f_src.i_fourcc == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
913 int8_t *sin = (int8_t*)id->p_buffer_in;
914 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos )
915 / 2, id->i_buffer_in_pos );
917 while( i_samples > 0 )
919 *sout++ = ( *sin++ - 128 ) << 8;
923 else if( id->f_src.i_fourcc == VLC_FOURCC( 's', '1', '6', 'l' ) )
925 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos )
926 / 2, id->i_buffer_in_pos / 2);
927 #ifdef WORDS_BIGENDIAN
928 uint8_t *sin = (uint8_t*)id->p_buffer_in;
929 i_used = i_samples * 2;
930 while( i_samples > 0 )
936 *sout++ = *(int16_t*)tmp;
941 memcpy( sout, id->p_buffer_in, i_samples * 2 );
943 i_used = i_samples * 2;
946 else if( id->f_src.i_fourcc == VLC_FOURCC( 's', '1', '6', 'b' ) )
948 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos )
949 / 2, id->i_buffer_in_pos / 2);
950 #ifdef WORDS_BIGENDIAN
951 memcpy( sout, id->p_buffer_in, i_samples * 2 );
953 i_used = i_samples * 2;
955 uint8_t *sin = (uint8_t*)id->p_buffer_in;
956 i_used = i_samples * 2;
957 while( i_samples > 0 )
963 *sout++ = *(int16_t*)tmp;
969 id->i_buffer_pos = (uint8_t*)sout - id->p_buffer;
970 if( i_used < id->i_buffer_in_pos )
972 memmove( id->p_buffer_in,
973 &id->p_buffer_in[i_used],
974 id->i_buffer_in - i_used );
976 id->i_buffer_in_pos -= i_used;
979 i_buffer_pos = id->i_buffer_pos;
981 /* encode as much data as possible */
983 #ifdef HAVE_VORBIS_VORBISENC_H
984 if( id->i_buffer_pos == 0 );
985 else if( id->f_dst.i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
989 sout_buffer_t *p_out;
992 if( !id->b_headers_sent )
994 ogg_packet header[3];
995 vorbis_analysis_headerout( id->p_vd, id->p_vc,
996 &header[0], &header[1], &header[2]);
997 for( i = 0; i < 3; i++ )
999 p_out = sout_BufferNew( p_stream->p_sout, header[i].bytes);
1000 memcpy( p_out->p_buffer, header[i].packet,
1003 p_out->i_size = header[i].bytes;
1004 p_out->i_length = 0;
1006 p_out->i_dts = p_out->i_pts = 0;
1008 sout_BufferChain( out, p_out );
1010 id->b_headers_sent = VLC_TRUE;
1013 i_samples = id->i_buffer_pos / id->f_src.i_channels / 2;
1014 id->i_samples_delay += i_samples;
1015 id->i_buffer_pos = 0;
1017 buffer = vorbis_analysis_buffer( id->p_vd, i_samples );
1019 /* convert samples to float and uninterleave */
1020 for( i = 0; i < id->f_dst.i_channels; i++ )
1022 for( j = 0 ; j < i_samples ; j++ )
1024 buffer[i][j]= ((float)( ((int16_t *)id->p_buffer)
1025 [j*id->f_src.i_channels + i ] ))/ 32768.f;
1029 vorbis_analysis_wrote( id->p_vd, i_samples );
1031 while( vorbis_analysis_blockout( id->p_vd, id->p_vb ) == 1 )
1033 vorbis_analysis( id->p_vb, NULL );
1034 vorbis_bitrate_addblock( id->p_vb );
1036 while( vorbis_bitrate_flushpacket( id->p_vd, &op ) )
1039 p_out = sout_BufferNew( p_stream->p_sout, op.bytes );
1040 memcpy( p_out->p_buffer, op.packet, op.bytes );
1042 i_block_size = vorbis_packet_blocksize( id->p_vi, &op );
1044 if( i_block_size < 0 ) i_block_size = 0;
1045 i_samples = ( id->i_last_block_size +
1046 i_block_size ) >> 2;
1047 id->i_last_block_size = i_block_size;
1049 p_out->i_size = op.bytes;
1050 p_out->i_length = (mtime_t)1000000 *
1051 (mtime_t)i_samples /
1052 (mtime_t)id->f_dst.i_sample_rate;
1054 //msg_Err( p_stream, "i_dts: %lld", id->i_dts );
1057 p_out->i_dts = id->i_dts;
1058 p_out->i_pts = id->i_dts;
1060 id->i_samples_delay -= i_samples;
1063 id->i_dts += p_out->i_length;
1064 sout_BufferChain( out, p_out );
1074 int i_frame_size = id->ff_enc_c->frame_size * 2 *
1075 id->ff_dec_c->channels;
1076 int i_out_size, i, j;
1077 sout_buffer_t *p_out;
1078 int16_t *p_buffer = (int16_t *)(id->p_buffer + i_buffer_pos -
1081 if( id->i_buffer_pos < i_frame_size )
1086 if( id->ff_dec_c->channels != id->ff_enc_c->channels )
1088 /* dumb downmixing */
1089 for( i = 0; i < id->ff_enc_c->frame_size; i++ )
1091 for( j = 0 ; j < id->f_dst.i_channels; j++ )
1093 p_buffer[i*id->f_dst.i_channels+j] = p_buffer[i*id->f_src.i_channels+j];
1098 /* msg_Warn( p_stream, "avcodec_encode_audio: frame size%d",
1100 i_out_size = avcodec_encode_audio( id->ff_enc_c,
1101 id->p_buffer_out, id->i_buffer_out, p_buffer );
1103 if( i_out_size <= 0 )
1108 id->i_buffer_pos -= i_frame_size;
1110 p_out = sout_BufferNew( p_stream->p_sout, i_out_size );
1111 memcpy( p_out->p_buffer, id->p_buffer_out, i_out_size );
1112 p_out->i_size = i_out_size;
1113 p_out->i_length = (mtime_t)1000000 *
1114 (mtime_t)id->ff_enc_c->frame_size /
1115 (mtime_t)id->ff_enc_c->sample_rate;
1118 p_out->i_dts = id->i_dts;
1119 p_out->i_pts = id->i_dts;
1122 id->i_dts += p_out->i_length;
1124 /* msg_Warn( p_stream, "frame dts=%lld len %lld out=%d",
1125 p_out->i_dts, p_out->i_length, i_out_size ); */
1127 sout_BufferChain( out, p_out );
1130 /* Copy the remaining raw samples */
1131 if( id->i_buffer_pos != 0 )
1133 memmove( id->p_buffer,
1134 &id->p_buffer[i_buffer_pos - id->i_buffer_pos],
1147 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1148 sout_stream_id_t *id )
1150 sout_stream_sys_t *p_sys = p_stream->p_sys;
1154 if( id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1155 id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1156 id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1157 id->f_src.i_fourcc == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1158 id->f_src.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1159 id->f_src.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1160 id->f_src.i_fourcc == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1161 id->f_src.i_fourcc == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1162 id->f_src.i_fourcc == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1165 id->ff_dec_c = avcodec_alloc_context();
1166 id->ff_dec_c->width = id->f_src.i_width;
1167 id->ff_dec_c->height = id->f_src.i_height;
1168 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_fourcc );
1173 i_ff_codec = get_ff_codec( id->f_src.i_fourcc );
1174 if( i_ff_codec == 0 )
1176 msg_Err( p_stream, "cannot find decoder" );
1177 return VLC_EGENERIC;
1180 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1183 msg_Err( p_stream, "cannot find decoder" );
1184 return VLC_EGENERIC;
1187 id->ff_dec_c = avcodec_alloc_context();
1188 id->ff_dec_c->width = id->f_src.i_width;
1189 id->ff_dec_c->height = id->f_src.i_height;
1190 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1191 id->ff_dec_c->extradata_size= id->f_src.i_extra_data;
1192 id->ff_dec_c->extradata = id->f_src.p_extra_data;
1193 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1194 id->ff_dec_c->error_resilience= -1;
1195 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1196 id->ff_dec_c->opaque = p_sys;
1198 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1200 msg_Err( p_stream, "cannot open decoder" );
1201 return VLC_EGENERIC;
1204 if( i_ff_codec == CODEC_ID_MPEG4 && id->ff_dec_c->extradata_size > 0 )
1208 uint8_t *p_vol = malloc( id->ff_dec_c->extradata_size +
1209 FF_INPUT_BUFFER_PADDING_SIZE );
1211 memcpy( p_vol, id->ff_dec_c->extradata,
1212 id->ff_dec_c->extradata_size );
1213 memset( p_vol + id->ff_dec_c->extradata_size, 0,
1214 FF_INPUT_BUFFER_PADDING_SIZE );
1216 avcodec_decode_video( id->ff_dec_c, &frame, &b_gotpicture,
1217 id->ff_dec_c->extradata,
1218 id->ff_dec_c->extradata_size );
1226 i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
1227 if( i_ff_codec != 0 )
1229 id->ff_enc = avcodec_find_encoder( i_ff_codec );
1232 /* Hack for external encoders */
1235 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1236 id->p_encoder->i_fourcc = id->f_dst.i_fourcc;
1237 id->p_encoder->format.video.i_width = p_sys->i_width;
1238 id->p_encoder->format.video.i_height = p_sys->i_height;
1239 id->p_encoder->i_bitrate = p_sys->i_vbitrate;
1241 if( id->p_encoder->format.video.i_width <= 0 )
1243 id->p_encoder->format.video.i_width = id->f_dst.i_width =
1244 id->ff_dec_c->width - p_sys->i_crop_left -
1245 p_sys->i_crop_right;
1247 if( id->p_encoder->format.video.i_height <= 0 )
1249 id->p_encoder->format.video.i_height = id->f_dst.i_height =
1250 id->ff_dec_c->height - p_sys->i_crop_top -
1251 p_sys->i_crop_bottom;
1254 id->p_encoder->p_module =
1255 module_Need( id->p_encoder, "video encoder", NULL );
1257 if( !id->p_encoder->p_module )
1259 free( id->p_encoder );
1260 id->p_encoder = NULL;
1263 /* End hack for external encoders */
1265 if( !id->ff_enc && !id->p_encoder )
1267 msg_Err( p_stream, "cannot find encoder" );
1268 return VLC_EGENERIC;
1271 /* XXX open it only when we have the first frame */
1272 id->b_enc_inited = VLC_FALSE;
1273 id->i_buffer_in = 0;
1274 id->i_buffer_in_pos = 0;
1275 id->p_buffer_in = NULL;
1277 id->i_buffer = 3*1024*1024;
1278 id->i_buffer_pos = 0;
1279 id->p_buffer = malloc( id->i_buffer );
1281 id->i_buffer_out = 0;
1282 id->i_buffer_out_pos = 0;
1283 id->p_buffer_out = NULL;
1285 id->p_ff_pic = avcodec_alloc_frame();
1286 id->p_ff_pic_tmp0 = NULL;
1287 id->p_ff_pic_tmp1 = NULL;
1288 id->p_ff_pic_tmp2 = NULL;
1289 id->p_vresample = NULL;
1291 p_sys->i_last_ref_pts = 0;
1292 p_sys->i_buggy_pts_detect = 0;
1294 /* This is enough for external encoders */
1295 if( id->p_encoder->p_module ) return VLC_SUCCESS;
1297 if( id->f_dst.i_fourcc == VLC_FOURCC( 'm','p','1','v' )||
1298 id->f_dst.i_fourcc == VLC_FOURCC( 'm','p','2','v' ) )
1300 id->f_dst.i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1303 id->ff_enc_c = avcodec_alloc_context();
1304 id->ff_enc_c->width = id->f_dst.i_width;
1305 id->ff_enc_c->height = id->f_dst.i_height;
1306 id->ff_enc_c->bit_rate = id->f_dst.i_bitrate;
1310 id->ff_enc_c->frame_rate = id->ff_dec_c->frame_rate;
1311 #if LIBAVCODEC_BUILD >= 4662
1312 id->ff_enc_c->frame_rate_base= id->ff_dec_c->frame_rate_base;
1317 #if LIBAVCODEC_BUILD >= 4662
1318 id->ff_enc_c->frame_rate = 25 ; /* FIXME as it break mpeg */
1319 id->ff_enc_c->frame_rate_base= 1;
1321 id->ff_enc_c->frame_rate = 25 * FRAME_RATE_BASE;
1325 id->ff_enc_c->gop_size = p_sys->i_key_int >= 0 ? p_sys->i_key_int : 50;
1326 id->ff_enc_c->max_b_frames = __MIN( p_sys->i_b_frames, FF_MAX_B_FRAMES );
1327 id->ff_enc_c->b_frame_strategy = 0;
1328 id->ff_enc_c->b_quant_factor = 2.0;
1330 if( p_sys->i_vtolerance >= 0 )
1332 id->ff_enc_c->bit_rate_tolerance = p_sys->i_vtolerance;
1334 id->ff_enc_c->qmin = p_sys->i_qmin;
1335 id->ff_enc_c->qmax = p_sys->i_qmax;
1337 #if LIBAVCODEC_BUILD >= 4673
1338 id->ff_enc_c->mb_decision = p_sys->i_hq;
1342 id->ff_enc_c->flags |= CODEC_FLAG_HQ;
1346 if( i_ff_codec == CODEC_ID_RAWVIDEO )
1348 id->ff_enc_c->pix_fmt = get_ff_chroma( id->f_dst.i_fourcc );
1354 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream, sout_stream_id_t *id )
1358 avcodec_close( id->ff_dec_c );
1362 /* External encoding */
1363 module_Unneed( p_stream, id->p_encoder->p_module );
1364 vlc_object_destroy( id->p_encoder->p_module );
1366 else if( id->b_enc_inited )
1368 avcodec_close( id->ff_enc_c );
1373 free( id->p_ff_pic );
1376 if( id->p_ff_pic_tmp0 )
1378 free( id->p_ff_pic_tmp0->data[0] );
1379 free( id->p_ff_pic_tmp0 );
1381 if( id->p_ff_pic_tmp1)
1383 free( id->p_ff_pic_tmp1->data[0] );
1384 free( id->p_ff_pic_tmp1 );
1386 if( id->p_ff_pic_tmp2)
1388 free( id->p_ff_pic_tmp2->data[0] );
1389 free( id->p_ff_pic_tmp2 );
1391 if( id->p_vresample )
1393 free( id->p_vresample );
1396 free( id->ff_dec_c );
1397 free( id->ff_enc_c );
1398 free( id->p_buffer );
1401 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1402 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1404 sout_stream_sys_t *p_sys = p_stream->p_sys;
1415 i_data = in->i_size;
1416 p_data = in->p_buffer;
1421 frame = id->p_ff_pic;
1422 p_sys->i_input_pts = in->i_pts;
1425 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1432 avpicture_fill( (AVPicture*)frame,
1434 id->ff_dec_c->pix_fmt,
1435 id->ff_dec_c->width, id->ff_dec_c->height );
1440 frame->pts = p_sys->i_input_pts;
1445 msg_Warn( p_stream, "error");
1446 return VLC_EGENERIC;
1456 /* Get the pts of the decoded frame if any, otherwise keep the
1457 * interpolated one */
1458 if( frame->pts > 0 )
1460 p_sys->i_output_pts = frame->pts;
1463 if( !id->b_enc_inited && id->p_encoder )
1467 /* XXX hack because of copy packetizer and mpeg4video that can fail
1470 if( id->p_encoder->i_width <= 0 )
1472 id->p_encoder->i_width = id->f_dst.i_width =
1473 id->ff_dec_c->width - p_sys->i_crop_left -
1474 p_sys->i_crop_right;
1476 if( id->p_encoder->i_height <= 0 )
1478 id->p_encoder->i_height = id->f_dst.i_height =
1479 id->ff_dec_c->height - p_sys->i_crop_top -
1480 p_sys->i_crop_bottom;
1484 id->p_encoder->i_bitrate = p_sys->i_vbitrate;
1486 if( !( id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out, &id->f_dst ) ) )
1488 msg_Err( p_stream, "cannot add this stream" );
1489 id->b_transcode = VLC_FALSE;
1490 return VLC_EGENERIC;
1493 while( (p_block = id->p_encoder->pf_header( id->p_encoder )) )
1495 sout_buffer_t *p_out;
1496 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1497 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1498 sout_BufferChain( out, p_out );
1501 id->b_enc_inited = VLC_TRUE;
1503 else if( !id->b_enc_inited )
1505 /* XXX hack because of copy packetizer and mpeg4video that can fail
1507 if( id->ff_enc_c->width <= 0 )
1509 id->ff_enc_c->width =
1510 id->f_dst.i_width = id->ff_dec_c->width - p_sys->i_crop_left - p_sys->i_crop_right;
1512 if( id->ff_enc_c->height <= 0 )
1514 id->ff_enc_c->height =
1515 id->f_dst.i_height = id->ff_dec_c->height - p_sys->i_crop_top - p_sys->i_crop_bottom;
1518 /* Make sure we get extradata filled by the encoder */
1519 id->ff_enc_c->extradata_size = 0;
1520 id->ff_enc_c->extradata = NULL;
1521 id->ff_enc_c->flags |= CODEC_FLAG_GLOBAL_HEADER;
1523 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
1525 msg_Err( p_stream, "cannot open encoder" );
1526 return VLC_EGENERIC;
1529 id->f_dst.i_extra_data = id->ff_enc_c->extradata_size;
1530 id->f_dst.p_extra_data = id->ff_enc_c->extradata;
1531 id->ff_enc_c->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
1533 if( !( id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out, &id->f_dst ) ) )
1535 msg_Err( p_stream, "cannot add this stream" );
1536 transcode_video_ffmpeg_close( p_stream, id );
1537 id->b_transcode = VLC_FALSE;
1538 return VLC_EGENERIC;
1540 id->b_enc_inited = VLC_TRUE;
1545 if( p_stream->p_sys->b_deinterlace )
1547 if( id->p_ff_pic_tmp0 == NULL )
1551 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1552 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1553 id->ff_dec_c->width, id->ff_dec_c->height );
1555 buf = malloc( i_size );
1557 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1558 id->ff_enc_c->pix_fmt,
1559 id->ff_dec_c->width, id->ff_dec_c->height );
1562 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1563 id->ff_dec_c->pix_fmt,
1564 id->ff_dec_c->width, id->ff_dec_c->height );
1566 frame = id->p_ff_pic_tmp0;
1569 /* convert pix format */
1570 if( !id->p_encoder )
1571 if( id->ff_dec_c->pix_fmt != id->ff_enc_c->pix_fmt )
1573 if( id->p_ff_pic_tmp1 == NULL )
1577 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1578 i_size = avpicture_get_size( id->ff_enc_c->pix_fmt,
1579 id->ff_dec_c->width, id->ff_dec_c->height );
1581 buf = malloc( i_size );
1583 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1584 id->ff_enc_c->pix_fmt,
1585 id->ff_dec_c->width, id->ff_dec_c->height );
1588 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->ff_enc_c->pix_fmt,
1589 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1590 id->ff_dec_c->width, id->ff_dec_c->height );
1592 frame = id->p_ff_pic_tmp1;
1595 /* convert size and crop */
1596 if( !id->p_encoder )
1597 if( id->ff_dec_c->width != id->ff_enc_c->width ||
1598 id->ff_dec_c->height != id->ff_enc_c->height ||
1599 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1600 p_sys->i_crop_left > 0 || p_sys->i_crop_right )
1602 if( id->p_ff_pic_tmp2 == NULL )
1606 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1607 i_size = avpicture_get_size( id->ff_enc_c->pix_fmt,
1608 id->ff_enc_c->width, id->ff_enc_c->height );
1610 buf = malloc( i_size );
1612 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1613 id->ff_enc_c->pix_fmt,
1614 id->ff_enc_c->width, id->ff_enc_c->height );
1617 img_resample_full_init( id->ff_enc_c->width, id->ff_enc_c->height,
1618 id->ff_dec_c->width, id->ff_dec_c->height,
1619 p_stream->p_sys->i_crop_top,
1620 p_stream->p_sys->i_crop_bottom,
1621 p_stream->p_sys->i_crop_left,
1622 p_stream->p_sys->i_crop_right );
1625 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1626 (AVPicture*)frame );
1628 frame = id->p_ff_pic_tmp2;
1631 /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
1632 if( id->f_dst.i_fourcc == VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
1633 frame->pts = p_sys->i_output_pts;
1637 /* Interpolate the next PTS
1638 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1639 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1641 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1642 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1647 /* External encoding */
1652 vout_InitPicture( VLC_OBJECT(p_stream), &pic,
1653 id->ff_dec_c->width, id->ff_dec_c->height,
1654 VLC_FOURCC('I','4','2','0') );
1656 for( i_plane = 0; i_plane < pic.i_planes; i_plane++ )
1658 pic.p[i_plane].p_pixels = frame->data[i_plane];
1659 pic.p[i_plane].i_pitch = frame->linesize[i_plane];
1662 pic.date = frame->pts;
1664 p_block = id->p_encoder->pf_encode_video( id->p_encoder, &pic );
1667 sout_buffer_t *p_out;
1668 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1669 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1670 sout_BufferChain( out, p_out );
1676 /* Let ffmpeg select the frame type */
1677 frame->pict_type = 0;
1679 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer,
1680 id->i_buffer, frame );
1683 sout_buffer_t *p_out;
1684 p_out = sout_BufferNew( p_stream->p_sout, i_out );
1686 memcpy( p_out->p_buffer, id->p_buffer, i_out );
1688 p_out->i_size = i_out;
1690 if( id->ff_enc_c->coded_frame->pts != 0 &&
1691 p_sys->i_buggy_pts_detect != id->ff_enc_c->coded_frame->pts )
1693 p_sys->i_buggy_pts_detect = id->ff_enc_c->coded_frame->pts;
1695 /* FIXME, 3-2 pulldown is not handled correctly */
1696 p_out->i_length = in->i_length;
1697 p_out->i_pts = id->ff_enc_c->coded_frame->pts;
1699 if( !id->ff_enc_c->delay ||
1700 ( id->ff_enc_c->coded_frame->pict_type != FF_I_TYPE &&
1701 id->ff_enc_c->coded_frame->pict_type != FF_P_TYPE ) )
1703 p_out->i_dts = p_out->i_pts;
1707 if( p_sys->i_last_ref_pts )
1709 p_out->i_dts = p_sys->i_last_ref_pts;
1713 /* Let's put something sensible */
1714 p_out->i_dts = p_out->i_pts;
1717 p_sys->i_last_ref_pts = p_out->i_pts;
1722 /* Buggy libavcodec which doesn't update coded_frame->pts
1724 p_out->i_length = in->i_length;
1725 p_out->i_dts = in->i_dts;
1726 p_out->i_pts = in->i_dts;
1729 sout_BufferChain( out, p_out );
1741 /*****************************************************************************
1742 * transcode_video_ffmpeg_getframebuf:
1744 * Callback used by ffmpeg to get a frame buffer.
1745 * We use it to get the right PTS for each decoded picture.
1746 *****************************************************************************/
1747 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1750 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1753 p_frame->pts = p_sys->i_input_pts;
1755 return avcodec_default_get_buffer( p_context, p_frame );