1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: transcode.c,v 1.1 2003/04/13 20:00:21 fenrir 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 "/home/fenrir/videolan/ffmpeg/libavcodec/avcodec.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
42 static sout_stream_id_t *Add ( sout_stream_t *, sout_format_t * );
43 static int Del ( sout_stream_t *, sout_stream_id_t * );
44 static int Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
46 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
47 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
48 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
50 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
51 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
52 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
54 /*****************************************************************************
56 *****************************************************************************/
58 set_description( _("Transcode stream") );
59 set_capability( "sout stream", 50 );
60 add_shortcut( "transcode" );
61 set_callbacks( Open, Close );
64 struct sout_stream_sys_t
68 vlc_fourcc_t i_acodec; // codec audio (0 if not transcode)
73 vlc_fourcc_t i_vcodec; // " video " " " "
77 vlc_bool_t b_deinterlace;
85 /*****************************************************************************
87 *****************************************************************************/
88 static int Open( vlc_object_t *p_this )
90 sout_stream_t *p_stream = (sout_stream_t*)p_this;
91 sout_stream_sys_t *p_sys;
94 p_sys = malloc( sizeof( sout_stream_sys_t ) );
95 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
98 p_sys->i_sample_rate = 0;
99 p_sys->i_channels = 0;
100 p_sys->i_abitrate = 0;
103 p_sys->i_vbitrate = 0;
106 p_sys->b_deinterlace= VLC_FALSE;
108 p_sys->i_crop_top = 0;
109 p_sys->i_crop_bottom= 0;
110 p_sys->i_crop_right = 0;
111 p_sys->i_crop_left = 0;
113 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "acodec" ) ) )
118 memcpy( fcc, codec, strlen( codec ) );
120 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
122 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "samplerate" ) ) )
124 p_sys->i_sample_rate = atoi( val );
126 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "channels" ) ) )
128 p_sys->i_channels = atoi( val );
130 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ab" ) ) )
132 p_sys->i_abitrate = atoi( val );
135 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
137 p_sys->i_sample_rate, p_sys->i_channels,
138 p_sys->i_abitrate / 1024 );
141 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "vcodec" ) ) )
146 memcpy( fcc, codec, strlen( codec ) );
148 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
150 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "width" ) ) )
152 p_sys->i_width = atoi( val );
154 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "height" ) ) )
156 p_sys->i_height = atoi( val );
158 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
160 p_sys->i_vbitrate = atoi( val );
162 if( sout_cfg_find( p_stream->p_cfg, "deinterlace" ) )
164 p_sys->b_deinterlace = VLC_TRUE;
167 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "croptop" ) ) )
169 p_sys->i_crop_top = atoi( val );
171 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropbottom" ) ) )
173 p_sys->i_crop_bottom = atoi( val );
175 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropleft" ) ) )
177 p_sys->i_crop_left = atoi( val );
179 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropright" ) ) )
181 p_sys->i_crop_right = atoi( val );
184 msg_Dbg( p_stream, "codec video=%4.4s %dx%d %dkb/s",
186 p_sys->i_width, p_sys->i_height,
187 p_sys->i_vbitrate / 1024 );
192 msg_Err( p_stream, "cannot create chain" );
196 p_stream->pf_add = Add;
197 p_stream->pf_del = Del;
198 p_stream->pf_send = Send;
200 p_stream->p_sys = p_sys;
203 avcodec_register_all();
208 /*****************************************************************************
210 *****************************************************************************/
212 static void Close( vlc_object_t * p_this )
214 sout_stream_t *p_stream = (sout_stream_t*)p_this;
215 sout_stream_sys_t *p_sys = p_stream->p_sys;
217 sout_stream_delete( p_sys->p_out );
221 struct sout_stream_id_t
223 vlc_fourcc_t b_transcode;
224 sout_format_t f_src; // only if transcoding
225 sout_format_t f_dst; // " " "
227 /* id of the out stream */
232 AVCodecContext *ff_dec_c;
235 vlc_fourcc_t b_enc_inited;
237 AVCodecContext *ff_enc_c;
244 uint8_t *p_buffer_in;
251 int i_buffer_out_pos;
252 uint8_t *p_buffer_out;
255 AVFrame *p_ff_pic_tmp0; // to do deinterlace
256 AVFrame *p_ff_pic_tmp1; // to do pix conversion
257 AVFrame *p_ff_pic_tmp2; // to do resample
259 ImgReSampleContext *p_vresample;
263 static sout_stream_id_t * Add ( sout_stream_t *p_stream, sout_format_t *p_fmt )
265 sout_stream_sys_t *p_sys = p_stream->p_sys;
266 sout_stream_id_t *id;
268 id = malloc( sizeof( sout_stream_id_t ) );
270 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
273 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
274 (char*)&p_fmt->i_fourcc,
275 (char*)&p_sys->i_acodec );
278 memcpy( &id->f_src, p_fmt, sizeof( sout_format_t ) );
280 /* create dst format */
281 id->f_dst.i_cat = AUDIO_ES;
282 id->f_dst.i_fourcc = p_sys->i_acodec;
283 id->f_dst.i_sample_rate = p_sys->i_sample_rate > 0 ? p_sys->i_sample_rate : id->f_src.i_sample_rate;
284 id->f_dst.i_channels = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.i_channels;
285 id->f_dst.i_bitrate = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
286 id->f_dst.i_block_align = 0;
287 id->f_dst.i_extra_data = 0;
288 id->f_dst.p_extra_data = NULL;
290 /* build decoder -> filter -> encoder */
291 if( transcode_audio_ffmpeg_new( p_stream, id ) )
293 msg_Err( p_stream, "cannot create audio chain" );
298 /* open output stream */
299 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
300 id->b_transcode = VLC_TRUE;
302 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
305 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
306 (char*)&p_fmt->i_fourcc,
307 (char*)&p_sys->i_vcodec );
309 memcpy( &id->f_src, p_fmt, sizeof( sout_format_t ) );
311 /* create dst format */
312 id->f_dst.i_cat = VIDEO_ES;
313 id->f_dst.i_fourcc = p_sys->i_vcodec;
314 id->f_dst.i_width = p_sys->i_width ; //> 0 ? p_sys->i_width : id->f_src.i_width;
315 id->f_dst.i_height = p_sys->i_height; // > 0 ? p_sys->i_height: id->f_src.i_height;
316 id->f_dst.i_bitrate = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
317 id->f_dst.i_extra_data = 0;
318 id->f_dst.p_extra_data = NULL;
320 /* build decoder -> filter -> encoder */
321 if( transcode_video_ffmpeg_new( p_stream, id ) )
323 msg_Err( p_stream, "cannot create video chain" );
328 /* open output stream */
329 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
330 id->b_transcode = VLC_TRUE;
334 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_fourcc );
335 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
336 id->b_transcode = VLC_FALSE;
348 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
350 sout_stream_sys_t *p_sys = p_stream->p_sys;
352 if( id->b_transcode )
354 if( id->f_src.i_cat == AUDIO_ES )
356 transcode_audio_ffmpeg_close( p_stream, id );
358 else if( id->f_src.i_cat == VIDEO_ES )
360 transcode_video_ffmpeg_close( p_stream, id );
364 p_sys->p_out->pf_del( p_sys->p_out, id->id );
370 static int Send ( sout_stream_t *p_stream, sout_stream_id_t *id, sout_buffer_t *p_buffer )
372 sout_stream_sys_t *p_sys = p_stream->p_sys;
374 if( id->b_transcode )
376 sout_buffer_t *p_buffer_out;
377 if( id->f_src.i_cat == AUDIO_ES )
379 transcode_audio_ffmpeg_process( p_stream, id, p_buffer, &p_buffer_out );
381 else if( id->f_src.i_cat == VIDEO_ES )
383 transcode_video_ffmpeg_process( p_stream, id, p_buffer, &p_buffer_out );
385 sout_BufferDelete( p_stream->p_sout, p_buffer );
389 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
395 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
399 /****************************************************************************
400 * ffmpeg decoder reencocdr part
401 ****************************************************************************/
407 } fourcc_to_ff_code[] =
410 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
411 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
412 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
413 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
414 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
417 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
418 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
419 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
420 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
421 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
422 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
423 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
424 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
425 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
426 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
427 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
428 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
429 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
431 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
434 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
438 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
440 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
442 return fourcc_to_ff_code[i].i_ff_codec;
449 static int transcode_audio_ffmpeg_new ( sout_stream_t *p_stream, sout_stream_id_t *id )
455 i_ff_codec = get_ff_codec( id->f_src.i_fourcc );
456 if( i_ff_codec == 0 )
458 msg_Err( p_stream, "cannot find decoder" );
462 id->ff_dec = avcodec_find_decoder( i_ff_codec );
465 msg_Err( p_stream, "cannot find decoder" );
469 id->ff_dec_c = avcodec_alloc_context();
470 id->ff_dec_c->sample_rate = id->f_src.i_sample_rate;
471 id->ff_dec_c->channels = id->f_src.i_channels;
472 id->ff_dec_c->block_align = id->f_src.i_block_align;
473 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
475 id->ff_dec_c->extradata_size = id->f_src.i_extra_data;
476 id->ff_dec_c->extradata = id->f_src.p_extra_data;
477 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
479 msg_Err( p_stream, "cannot open decoder" );
484 i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
485 if( i_ff_codec == 0 )
487 msg_Err( p_stream, "cannot find encoder" );
491 id->ff_enc = avcodec_find_encoder( i_ff_codec );
494 msg_Err( p_stream, "cannot find encoder" );
498 id->ff_enc_c = avcodec_alloc_context();
499 id->ff_enc_c->bit_rate = id->f_dst.i_bitrate;
500 id->ff_enc_c->sample_rate = id->f_dst.i_sample_rate;
501 id->ff_enc_c->channels = id->f_dst.i_channels;
503 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
505 msg_Err( p_stream, "cannot open encoder" );
510 id->i_buffer_in = AVCODEC_MAX_AUDIO_FRAME_SIZE;
511 id->i_buffer_in_pos = 0;
512 id->p_buffer_in = malloc( id->i_buffer_in );
514 id->i_buffer = AVCODEC_MAX_AUDIO_FRAME_SIZE;
515 id->i_buffer_pos = 0;
516 id->p_buffer = malloc( id->i_buffer );
518 id->i_buffer_out = AVCODEC_MAX_AUDIO_FRAME_SIZE;
519 id->i_buffer_out_pos = 0;
520 id->p_buffer_out = malloc( id->i_buffer_out );
525 static void transcode_audio_ffmpeg_close ( sout_stream_t *p_stream, sout_stream_id_t *id )
527 avcodec_close( id->ff_dec_c );
528 avcodec_close( id->ff_enc_c );
530 free( id->ff_dec_c );
531 free( id->ff_enc_c );
533 free( id->p_buffer );
536 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream, sout_stream_id_t *id,
537 sout_buffer_t *in, sout_buffer_t **out )
541 /* gather data into p_buffer_in */
542 id->i_dts = in->i_dts -
544 (mtime_t)(id->i_buffer_pos / 2 / id->ff_enc_c->channels )/
545 (mtime_t)id->ff_enc_c->sample_rate;
547 memcpy( &id->p_buffer_in[id->i_buffer_in_pos],
550 id->i_buffer_in_pos += in->i_size;
552 /* decode as many data as possible */
558 i_buffer_size = id->i_buffer - id->i_buffer_pos;
560 i_used = avcodec_decode_audio( id->ff_dec_c,
561 (int16_t*)&id->p_buffer[id->i_buffer_pos], &i_buffer_size,
562 id->p_buffer_in, id->i_buffer_in_pos );
564 //msg_Warn( p_stream, "avcodec_decode_audio: %d used", i_used );
565 id->i_buffer_pos += i_buffer_size;
569 msg_Warn( p_stream, "error");
570 id->i_buffer_in_pos = 0;
573 else if( i_used < id->i_buffer_in_pos )
575 memmove( id->p_buffer_in,
576 &id->p_buffer_in[i_used],
577 id->i_buffer_in - i_used );
578 id->i_buffer_in_pos -= i_used;
582 id->i_buffer_in_pos = 0;
587 /* encode as many data as possible */
590 int i_frame_size = id->ff_enc_c->frame_size * 2 * id->ff_enc_c->channels;
592 sout_buffer_t *p_out;
594 if( id->i_buffer_pos < i_frame_size )
599 //msg_Warn( p_stream, "avcodec_encode_audio: frame size%d", i_frame_size);
600 i_out_size = avcodec_encode_audio( id->ff_enc_c,
601 id->p_buffer_out, id->i_buffer_out,
602 (int16_t*)id->p_buffer );
604 if( i_out_size <= 0 )
608 memmove( id->p_buffer,
609 &id->p_buffer[i_frame_size],
610 id->i_buffer - i_frame_size );
611 id->i_buffer_pos -= i_frame_size;
613 p_out = sout_BufferNew( p_stream->p_sout, i_out_size );
614 memcpy( p_out->p_buffer, id->p_buffer_out, i_out_size );
615 p_out->i_size = i_out_size;
616 p_out->i_length = (mtime_t)1000000 * (mtime_t)id->ff_enc_c->frame_size / (mtime_t)id->ff_enc_c->sample_rate;
618 p_out->i_dts = id->i_dts;
619 p_out->i_pts = id->i_dts;
622 id->i_dts += p_out->i_length;
624 //msg_Warn( p_stream, "frame dts=%lld len %lld out=%d", p_out->i_dts, p_out->i_length, i_out_size );
625 sout_BufferChain( out, p_out );
635 static int transcode_video_ffmpeg_new ( sout_stream_t *p_stream, sout_stream_id_t *id )
640 i_ff_codec = get_ff_codec( id->f_src.i_fourcc );
641 if( i_ff_codec == 0 )
643 msg_Err( p_stream, "cannot find decoder" );
647 id->ff_dec = avcodec_find_decoder( i_ff_codec );
650 msg_Err( p_stream, "cannot find decoder" );
654 id->ff_dec_c = avcodec_alloc_context();
655 id->ff_dec_c->width = id->f_src.i_width;
656 id->ff_dec_c->height = id->f_src.i_height;
657 //id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
658 id->ff_dec_c->extradata_size= id->f_src.i_extra_data;
659 id->ff_dec_c->extradata = id->f_src.p_extra_data;
660 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
661 id->ff_dec_c->error_resilience= -1;
662 if( id->ff_dec->capabilities & CODEC_CAP_TRUNCATED )
664 id->ff_dec_c->flags |= CODEC_FLAG_TRUNCATED;
667 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
669 msg_Err( p_stream, "cannot open decoder" );
673 if( i_ff_codec == CODEC_ID_MPEG4 && id->ff_dec_c->extradata_size > 0 )
678 avcodec_decode_video( id->ff_dec_c, &frame,
680 id->ff_dec_c->extradata, id->ff_dec_c->extradata_size );
685 i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
686 if( i_ff_codec == 0 )
688 msg_Err( p_stream, "cannot find encoder" );
692 id->ff_enc = avcodec_find_encoder( i_ff_codec );
695 msg_Err( p_stream, "cannot find encoder" );
699 id->ff_enc_c = avcodec_alloc_context();
700 id->ff_enc_c->width = id->f_dst.i_width;
701 id->ff_enc_c->height = id->f_dst.i_height;
702 id->ff_enc_c->bit_rate = id->f_dst.i_bitrate;
703 id->ff_enc_c->frame_rate = 25 ; // FIXME as it break mpeg
704 id->ff_enc_c->frame_rate_base= 1;
705 id->ff_enc_c->gop_size = 25;
706 id->ff_enc_c->qmin = 2;
707 id->ff_enc_c->qmax = 31;
709 /* XXX open it when we have the first frame */
710 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
712 msg_Err( p_stream, "cannot open encoder" );
716 id->b_enc_inited = VLC_FALSE;
718 id->i_buffer_in_pos = 0;
719 id->p_buffer_in = NULL;
721 id->i_buffer = 3*1024*1024;
722 id->i_buffer_pos = 0;
723 id->p_buffer = malloc( id->i_buffer );
725 id->i_buffer_out = 0;
726 id->i_buffer_out_pos = 0;
727 id->p_buffer_out = NULL;
729 id->p_ff_pic = avcodec_alloc_frame();
730 id->p_ff_pic_tmp0 = NULL;
731 id->p_ff_pic_tmp1 = NULL;
732 id->p_ff_pic_tmp2 = NULL;
733 id->p_vresample = NULL;
737 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream, sout_stream_id_t *id )
739 avcodec_close( id->ff_dec_c );
740 if( id->b_enc_inited )
742 avcodec_close( id->ff_enc_c );
747 free( id->p_ff_pic );
750 if( id->p_ff_pic_tmp0 )
752 free( id->p_ff_pic_tmp0->data[0] );
753 free( id->p_ff_pic_tmp0 );
755 if( id->p_ff_pic_tmp1)
757 free( id->p_ff_pic_tmp1->data[0] );
758 free( id->p_ff_pic_tmp1 );
760 if( id->p_ff_pic_tmp2)
762 free( id->p_ff_pic_tmp2->data[0] );
763 free( id->p_ff_pic_tmp2 );
765 if( id->p_vresample )
767 free( id->p_vresample );
770 free( id->ff_dec_c );
771 free( id->ff_enc_c );
773 free( id->p_buffer );
776 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream, sout_stream_id_t *id,
777 sout_buffer_t *in, sout_buffer_t **out )
790 p_data = in->p_buffer;
795 frame = id->p_ff_pic;
796 i_used = avcodec_decode_video( id->ff_dec_c, frame,
802 msg_Warn( p_stream, "error");
813 if( !id->b_enc_inited )
815 /* XXX hack because of copy packetizer and mpeg4video that can failed
817 if( id->ff_enc_c->width == 0 || id->ff_enc_c->height == 0 )
819 id->ff_enc_c->width = id->ff_dec_c->width;
820 id->ff_enc_c->height = id->ff_dec_c->height;
823 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
825 msg_Err( p_stream, "cannot open encoder" );
828 id->b_enc_inited = VLC_TRUE;
833 if( p_stream->p_sys->b_deinterlace )
835 if( id->p_ff_pic_tmp0 == NULL )
839 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
840 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
841 id->ff_dec_c->width, id->ff_dec_c->height );
843 buf = malloc( i_size );
845 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
846 id->ff_enc_c->pix_fmt,
847 id->ff_dec_c->width, id->ff_dec_c->height );
850 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
851 id->ff_dec_c->pix_fmt,
852 id->ff_dec_c->width, id->ff_dec_c->height );
854 frame = id->p_ff_pic_tmp0;
857 /* convert pix format */
858 if( id->ff_dec_c->pix_fmt != id->ff_enc_c->pix_fmt )
860 if( id->p_ff_pic_tmp1 == NULL )
864 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
865 i_size = avpicture_get_size( id->ff_enc_c->pix_fmt,
866 id->ff_dec_c->width, id->ff_dec_c->height );
868 buf = malloc( i_size );
870 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
871 id->ff_enc_c->pix_fmt,
872 id->ff_dec_c->width, id->ff_dec_c->height );
875 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->ff_enc_c->pix_fmt,
876 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
877 id->ff_dec_c->width, id->ff_dec_c->height );
879 frame = id->p_ff_pic_tmp1;
882 /* convert size and crop */
883 if( ( id->ff_dec_c->width != id->ff_enc_c->width ) ||
884 ( id->ff_dec_c->height != id->ff_enc_c->height ) )
886 if( id->p_ff_pic_tmp2 == NULL )
890 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
891 i_size = avpicture_get_size( id->ff_enc_c->pix_fmt,
892 id->ff_enc_c->width, id->ff_enc_c->height );
894 buf = malloc( i_size );
896 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
897 id->ff_enc_c->pix_fmt,
898 id->ff_enc_c->width, id->ff_enc_c->height );
901 img_resample_full_init( id->ff_enc_c->width, id->ff_enc_c->height,
902 id->ff_dec_c->width, id->ff_dec_c->height,
903 p_stream->p_sys->i_crop_top,
904 p_stream->p_sys->i_crop_bottom,
905 p_stream->p_sys->i_crop_left,
906 p_stream->p_sys->i_crop_right );
909 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2, (AVPicture*)frame );
911 frame = id->p_ff_pic_tmp2;
915 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer, id->i_buffer, frame );
919 sout_buffer_t *p_out;
920 p_out = sout_BufferNew( p_stream->p_sout, i_out );
922 memcpy( p_out->p_buffer, id->p_buffer, i_out );
924 p_out->i_size = i_out;
925 p_out->i_length = in->i_length;
926 p_out->i_dts = in->i_dts;
927 p_out->i_pts = in->i_dts; /* FIXME */
929 sout_BufferChain( out, p_out );