1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: transcode.c,v 1.8 2003/05/02 16:25:12 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>
35 #ifdef HAVE_FFMPEG_AVCODEC_H
36 # include <ffmpeg/avcodec.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
47 static sout_stream_id_t *Add ( sout_stream_t *, sout_format_t * );
48 static int Del ( sout_stream_t *, sout_stream_id_t * );
49 static int Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
51 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
52 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
53 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
55 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
56 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
57 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
59 /*****************************************************************************
61 *****************************************************************************/
63 set_description( _("Transcode stream") );
64 set_capability( "sout stream", 50 );
65 add_shortcut( "transcode" );
66 set_callbacks( Open, Close );
69 struct sout_stream_sys_t
73 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
78 vlc_fourcc_t i_vcodec; /* " video " " " " */
82 vlc_bool_t b_deinterlace;
90 /*****************************************************************************
92 *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
95 sout_stream_t *p_stream = (sout_stream_t*)p_this;
96 sout_stream_sys_t *p_sys;
99 p_sys = malloc( sizeof( sout_stream_sys_t ) );
100 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
103 p_sys->i_sample_rate = 0;
104 p_sys->i_channels = 0;
105 p_sys->i_abitrate = 0;
108 p_sys->i_vbitrate = 0;
111 p_sys->b_deinterlace= VLC_FALSE;
113 p_sys->i_crop_top = 0;
114 p_sys->i_crop_bottom= 0;
115 p_sys->i_crop_right = 0;
116 p_sys->i_crop_left = 0;
118 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "acodec" ) ) )
123 memcpy( fcc, codec, strlen( codec ) );
125 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
127 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "samplerate" ) ) )
129 p_sys->i_sample_rate = atoi( val );
131 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "channels" ) ) )
133 p_sys->i_channels = atoi( val );
135 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ab" ) ) )
137 p_sys->i_abitrate = atoi( val );
140 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
142 p_sys->i_sample_rate, p_sys->i_channels,
143 p_sys->i_abitrate / 1024 );
146 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "vcodec" ) ) )
151 memcpy( fcc, codec, strlen( codec ) );
153 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
155 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "width" ) ) )
157 p_sys->i_width = atoi( val );
159 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "height" ) ) )
161 p_sys->i_height = atoi( val );
163 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
165 p_sys->i_vbitrate = atoi( val );
167 if( sout_cfg_find( p_stream->p_cfg, "deinterlace" ) )
169 p_sys->b_deinterlace = VLC_TRUE;
172 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "croptop" ) ) )
174 p_sys->i_crop_top = atoi( val );
176 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropbottom" ) ) )
178 p_sys->i_crop_bottom = atoi( val );
180 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropleft" ) ) )
182 p_sys->i_crop_left = atoi( val );
184 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropright" ) ) )
186 p_sys->i_crop_right = atoi( val );
189 msg_Dbg( p_stream, "codec video=%4.4s %dx%d %dkb/s",
191 p_sys->i_width, p_sys->i_height,
192 p_sys->i_vbitrate / 1024 );
197 msg_Err( p_stream, "cannot create chain" );
201 p_stream->pf_add = Add;
202 p_stream->pf_del = Del;
203 p_stream->pf_send = Send;
205 p_stream->p_sys = p_sys;
208 avcodec_register_all();
213 /*****************************************************************************
215 *****************************************************************************/
217 static void Close( vlc_object_t * p_this )
219 sout_stream_t *p_stream = (sout_stream_t*)p_this;
220 sout_stream_sys_t *p_sys = p_stream->p_sys;
222 sout_stream_delete( p_sys->p_out );
226 struct sout_stream_id_t
228 vlc_fourcc_t b_transcode;
229 sout_format_t f_src; /* only if transcoding */
230 sout_format_t f_dst; /* " " " */
232 /* id of the out stream */
237 AVCodecContext *ff_dec_c;
240 vlc_fourcc_t b_enc_inited;
242 AVCodecContext *ff_enc_c;
249 uint8_t *p_buffer_in;
256 int i_buffer_out_pos;
257 uint8_t *p_buffer_out;
260 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
261 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
262 AVFrame *p_ff_pic_tmp2; /* to do resample */
264 ImgReSampleContext *p_vresample;
268 static sout_stream_id_t * Add ( sout_stream_t *p_stream, sout_format_t *p_fmt )
270 sout_stream_sys_t *p_sys = p_stream->p_sys;
271 sout_stream_id_t *id;
273 id = malloc( sizeof( sout_stream_id_t ) );
275 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
278 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
279 (char*)&p_fmt->i_fourcc,
280 (char*)&p_sys->i_acodec );
283 memcpy( &id->f_src, p_fmt, sizeof( sout_format_t ) );
285 /* create dst format */
286 id->f_dst.i_cat = AUDIO_ES;
287 id->f_dst.i_fourcc = p_sys->i_acodec;
288 id->f_dst.i_sample_rate = p_sys->i_sample_rate > 0 ? p_sys->i_sample_rate : id->f_src.i_sample_rate;
289 id->f_dst.i_channels = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.i_channels;
290 id->f_dst.i_bitrate = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
291 id->f_dst.i_block_align = 0;
292 id->f_dst.i_extra_data = 0;
293 id->f_dst.p_extra_data = NULL;
295 /* build decoder -> filter -> encoder */
296 if( transcode_audio_ffmpeg_new( p_stream, id ) )
298 msg_Err( p_stream, "cannot create audio chain" );
303 /* open output stream */
304 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
305 id->b_transcode = VLC_TRUE;
307 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
310 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
311 (char*)&p_fmt->i_fourcc,
312 (char*)&p_sys->i_vcodec );
314 memcpy( &id->f_src, p_fmt, sizeof( sout_format_t ) );
316 /* create dst format */
317 id->f_dst.i_cat = VIDEO_ES;
318 id->f_dst.i_fourcc = p_sys->i_vcodec;
319 id->f_dst.i_width = p_sys->i_width ; /* > 0 ? p_sys->i_width : id->f_src.i_width; */
320 id->f_dst.i_height = p_sys->i_height; /* > 0 ? p_sys->i_height: id->f_src.i_height; */
321 id->f_dst.i_bitrate = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
322 id->f_dst.i_extra_data = 0;
323 id->f_dst.p_extra_data = NULL;
325 /* build decoder -> filter -> encoder */
326 if( transcode_video_ffmpeg_new( p_stream, id ) )
328 msg_Err( p_stream, "cannot create video chain" );
333 /* open output stream */
334 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
336 id->b_transcode = VLC_TRUE;
340 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_fourcc );
341 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
342 id->b_transcode = VLC_FALSE;
354 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
356 sout_stream_sys_t *p_sys = p_stream->p_sys;
358 if( id->b_transcode )
360 if( id->f_src.i_cat == AUDIO_ES )
362 transcode_audio_ffmpeg_close( p_stream, id );
364 else if( id->f_src.i_cat == VIDEO_ES )
366 transcode_video_ffmpeg_close( p_stream, id );
370 p_sys->p_out->pf_del( p_sys->p_out, id->id );
376 static int Send ( sout_stream_t *p_stream, sout_stream_id_t *id, sout_buffer_t *p_buffer )
378 sout_stream_sys_t *p_sys = p_stream->p_sys;
380 if( id->b_transcode )
382 sout_buffer_t *p_buffer_out;
383 if( id->f_src.i_cat == AUDIO_ES )
385 transcode_audio_ffmpeg_process( p_stream, id, p_buffer, &p_buffer_out );
387 else if( id->f_src.i_cat == VIDEO_ES )
389 transcode_video_ffmpeg_process( p_stream, id, p_buffer, &p_buffer_out );
391 sout_BufferDelete( p_stream->p_sout, p_buffer );
395 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
399 else if( id->id != NULL )
401 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
405 sout_BufferDelete( p_stream->p_sout, p_buffer );
410 /****************************************************************************
411 * ffmpeg decoder reencocdr part
412 ****************************************************************************/
418 } fourcc_to_ff_code[] =
421 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
422 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
423 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
424 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
425 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
428 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
429 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
430 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
431 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
432 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
433 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
434 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
435 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
436 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
437 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
438 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
439 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
440 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
442 /* raw video code, only used for 'encoding' */
443 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
444 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
445 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
446 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
447 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
449 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
452 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
456 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
458 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
460 return fourcc_to_ff_code[i].i_ff_codec;
467 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
471 case VLC_FOURCC( 'I', '4', '2', '0' ):
472 return PIX_FMT_YUV420P;
473 case VLC_FOURCC( 'I', '4', '2', '2' ):
474 return PIX_FMT_YUV422P;
475 case VLC_FOURCC( 'I', '4', '4', '4' ):
476 return PIX_FMT_YUV444P;
477 case VLC_FOURCC( 'R', 'V', '2', '4' ):
478 return PIX_FMT_RGB24;
479 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
480 return PIX_FMT_YUV422;
486 static int transcode_audio_ffmpeg_new ( sout_stream_t *p_stream, sout_stream_id_t *id )
490 if( id->f_src.i_fourcc == VLC_FOURCC('s','1','6','l') ||
491 id->f_src.i_fourcc == VLC_FOURCC('s','1','6','b') ||
492 id->f_src.i_fourcc == VLC_FOURCC('s','8',' ',' ') ||
493 id->f_src.i_fourcc == VLC_FOURCC('u','8',' ',' ') )
497 id->ff_dec_c = avcodec_alloc_context();
498 id->ff_dec_c->sample_rate = id->f_src.i_sample_rate;
499 id->ff_dec_c->channels = id->f_src.i_channels;
500 id->ff_dec_c->block_align = id->f_src.i_block_align;
501 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
506 i_ff_codec = get_ff_codec( id->f_src.i_fourcc );
507 if( i_ff_codec == 0 )
509 msg_Err( p_stream, "cannot find decoder" );
513 id->ff_dec = avcodec_find_decoder( i_ff_codec );
516 msg_Err( p_stream, "cannot find decoder" );
520 id->ff_dec_c = avcodec_alloc_context();
521 id->ff_dec_c->sample_rate = id->f_src.i_sample_rate;
522 id->ff_dec_c->channels = id->f_src.i_channels;
523 id->ff_dec_c->block_align = id->f_src.i_block_align;
524 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
526 id->ff_dec_c->extradata_size = id->f_src.i_extra_data;
527 id->ff_dec_c->extradata = id->f_src.p_extra_data;
528 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
530 msg_Err( p_stream, "cannot open decoder" );
536 i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
537 if( i_ff_codec == 0 )
539 msg_Err( p_stream, "cannot find encoder" );
543 id->ff_enc = avcodec_find_encoder( i_ff_codec );
546 msg_Err( p_stream, "cannot find encoder" );
550 id->ff_enc_c = avcodec_alloc_context();
551 id->ff_enc_c->bit_rate = id->f_dst.i_bitrate;
552 id->ff_enc_c->sample_rate = id->f_dst.i_sample_rate;
553 id->ff_enc_c->channels = id->f_dst.i_channels;
555 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
557 msg_Err( p_stream, "cannot open encoder" );
562 id->i_buffer_in = AVCODEC_MAX_AUDIO_FRAME_SIZE;
563 id->i_buffer_in_pos = 0;
564 id->p_buffer_in = malloc( id->i_buffer_in );
566 id->i_buffer = AVCODEC_MAX_AUDIO_FRAME_SIZE;
567 id->i_buffer_pos = 0;
568 id->p_buffer = malloc( id->i_buffer );
570 id->i_buffer_out = AVCODEC_MAX_AUDIO_FRAME_SIZE;
571 id->i_buffer_out_pos = 0;
572 id->p_buffer_out = malloc( id->i_buffer_out );
577 static void transcode_audio_ffmpeg_close ( sout_stream_t *p_stream, sout_stream_id_t *id )
581 avcodec_close( id->ff_dec_c );
583 avcodec_close( id->ff_enc_c );
585 free( id->ff_dec_c );
586 free( id->ff_enc_c );
588 free( id->p_buffer );
591 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream, sout_stream_id_t *id,
592 sout_buffer_t *in, sout_buffer_t **out )
596 /* gather data into p_buffer_in */
597 id->i_dts = in->i_dts -
599 (mtime_t)(id->i_buffer_pos / 2 / id->ff_enc_c->channels )/
600 (mtime_t)id->ff_enc_c->sample_rate;
602 if( id->i_buffer_in_pos + in->i_size > id->i_buffer_in )
604 /* extend buffer_in */
605 id->i_buffer_in = id->i_buffer_in_pos + in->i_size + 1024;
606 id->p_buffer_in = realloc( id->p_buffer_in, id->i_buffer_in );
608 memcpy( &id->p_buffer_in[id->i_buffer_in_pos],
611 id->i_buffer_in_pos += in->i_size;
613 /* decode as many data as possible */
621 i_buffer_size = id->i_buffer - id->i_buffer_pos;
623 i_used = avcodec_decode_audio( id->ff_dec_c,
624 (int16_t*)&id->p_buffer[id->i_buffer_pos], &i_buffer_size,
625 id->p_buffer_in, id->i_buffer_in_pos );
627 /* msg_Warn( p_stream, "avcodec_decode_audio: %d used", i_used ); */
628 id->i_buffer_pos += i_buffer_size;
632 msg_Warn( p_stream, "error");
633 id->i_buffer_in_pos = 0;
636 else if( i_used < id->i_buffer_in_pos )
638 memmove( id->p_buffer_in,
639 &id->p_buffer_in[i_used],
640 id->i_buffer_in - i_used );
641 id->i_buffer_in_pos -= i_used;
645 id->i_buffer_in_pos = 0;
652 int16_t *sout = (int16_t*)&id->p_buffer[id->i_buffer_pos];
655 if( id->f_src.i_fourcc == VLC_FOURCC( 's', '8', ' ', ' ' ) )
657 int8_t *sin = (int8_t*)id->p_buffer_in;
658 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos ) / 2, id->i_buffer_in_pos );
660 while( i_samples > 0 )
662 *sout++ = ( *sin++ ) << 8;
666 else if( id->f_src.i_fourcc == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
668 int8_t *sin = (int8_t*)id->p_buffer_in;
669 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos ) / 2, id->i_buffer_in_pos );
671 while( i_samples > 0 )
673 *sout++ = ( *sin++ - 128 ) << 8;
677 else if( id->f_src.i_fourcc == VLC_FOURCC( 's', '1', '6', 'l' ) )
679 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos ) / 2, id->i_buffer_in_pos / 2);
680 #ifdef WORDS_BIGENDIAN
681 uint8_t *sin = (uint8_t*)id->p_buffer_in;
682 while( i_samples > 0 )
688 *sout++ = *(int16_t*)tmp;
693 memcpy( sout, id->p_buffer_in, i_samples * 2 );
696 i_used = i_samples * 2;
698 else if( id->f_src.i_fourcc == VLC_FOURCC( 's', '1', '6', 'b' ) )
700 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos ) / 2, id->i_buffer_in_pos / 2);
701 #ifdef WORDS_BIGENDIAN
702 memcpy( sout, id->p_buffer_in, i_samples * 2 );
705 uint8_t *sin = (uint8_t*)id->p_buffer_in;
706 while( i_samples > 0 )
712 *sout++ = *(int16_t*)tmp;
716 i_used = i_samples * 2;
719 id->i_buffer_pos = (uint8_t*)sout - id->p_buffer;
720 if( i_used < id->i_buffer_in_pos )
722 memmove( id->p_buffer_in,
723 &id->p_buffer_in[i_used],
724 id->i_buffer_in - i_used );
726 id->i_buffer_in_pos -= i_used;
729 /* encode as many data as possible */
732 int i_frame_size = id->ff_enc_c->frame_size * 2 * id->ff_enc_c->channels;
734 sout_buffer_t *p_out;
736 if( id->i_buffer_pos < i_frame_size )
741 /* msg_Warn( p_stream, "avcodec_encode_audio: frame size%d", i_frame_size); */
742 i_out_size = avcodec_encode_audio( id->ff_enc_c,
743 id->p_buffer_out, id->i_buffer_out,
744 (int16_t*)id->p_buffer );
746 if( i_out_size <= 0 )
750 memmove( id->p_buffer,
751 &id->p_buffer[i_frame_size],
752 id->i_buffer - i_frame_size );
753 id->i_buffer_pos -= i_frame_size;
755 p_out = sout_BufferNew( p_stream->p_sout, i_out_size );
756 memcpy( p_out->p_buffer, id->p_buffer_out, i_out_size );
757 p_out->i_size = i_out_size;
758 p_out->i_length = (mtime_t)1000000 * (mtime_t)id->ff_enc_c->frame_size / (mtime_t)id->ff_enc_c->sample_rate;
760 p_out->i_dts = id->i_dts;
761 p_out->i_pts = id->i_dts;
764 id->i_dts += p_out->i_length;
766 /* msg_Warn( p_stream, "frame dts=%lld len %lld out=%d", p_out->i_dts, p_out->i_length, i_out_size ); */
767 sout_BufferChain( out, p_out );
777 static int transcode_video_ffmpeg_new ( sout_stream_t *p_stream, sout_stream_id_t *id )
781 if( id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '2', '0' ) ||
782 id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '2', '2' ) ||
783 id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '4', '4' ) ||
784 id->f_src.i_fourcc == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
785 id->f_src.i_fourcc == VLC_FOURCC( 'R', 'V', '2', '4' ) )
788 id->ff_dec_c = avcodec_alloc_context();
789 id->ff_dec_c->width = id->f_src.i_width;
790 id->ff_dec_c->height = id->f_src.i_height;
791 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_fourcc );
796 i_ff_codec = get_ff_codec( id->f_src.i_fourcc );
797 if( i_ff_codec == 0 )
799 msg_Err( p_stream, "cannot find decoder" );
803 id->ff_dec = avcodec_find_decoder( i_ff_codec );
806 msg_Err( p_stream, "cannot find decoder" );
810 id->ff_dec_c = avcodec_alloc_context();
811 id->ff_dec_c->width = id->f_src.i_width;
812 id->ff_dec_c->height = id->f_src.i_height;
813 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
814 id->ff_dec_c->extradata_size= id->f_src.i_extra_data;
815 id->ff_dec_c->extradata = id->f_src.p_extra_data;
816 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
817 id->ff_dec_c->error_resilience= -1;
818 if( id->ff_dec->capabilities & CODEC_CAP_TRUNCATED )
820 id->ff_dec_c->flags |= CODEC_FLAG_TRUNCATED;
823 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
825 msg_Err( p_stream, "cannot open decoder" );
829 if( i_ff_codec == CODEC_ID_MPEG4 && id->ff_dec_c->extradata_size > 0 )
834 avcodec_decode_video( id->ff_dec_c, &frame,
836 id->ff_dec_c->extradata, id->ff_dec_c->extradata_size );
842 i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
843 if( i_ff_codec == 0 )
845 msg_Err( p_stream, "cannot find encoder" );
849 id->ff_enc = avcodec_find_encoder( i_ff_codec );
852 msg_Err( p_stream, "cannot find encoder" );
856 id->ff_enc_c = avcodec_alloc_context();
857 id->ff_enc_c->width = id->f_dst.i_width;
858 id->ff_enc_c->height = id->f_dst.i_height;
859 id->ff_enc_c->bit_rate = id->f_dst.i_bitrate;
860 #if LIBAVCODEC_BUILD >= 4662
861 id->ff_enc_c->frame_rate = 25 ; /* FIXME as it break mpeg */
862 id->ff_enc_c->frame_rate_base= 1;
864 id->ff_enc_c->frame_rate = 25 * FRAME_RATE_BASE;
866 id->ff_enc_c->gop_size = 25;
867 id->ff_enc_c->qmin = 2;
868 id->ff_enc_c->qmax = 31;
870 if( i_ff_codec == CODEC_ID_RAWVIDEO )
872 id->ff_enc_c->pix_fmt = get_ff_chroma( id->f_dst.i_fourcc );
874 /* XXX open it only when we have the first frame */
875 id->b_enc_inited = VLC_FALSE;
877 id->i_buffer_in_pos = 0;
878 id->p_buffer_in = NULL;
880 id->i_buffer = 3*1024*1024;
881 id->i_buffer_pos = 0;
882 id->p_buffer = malloc( id->i_buffer );
884 id->i_buffer_out = 0;
885 id->i_buffer_out_pos = 0;
886 id->p_buffer_out = NULL;
888 id->p_ff_pic = avcodec_alloc_frame();
889 id->p_ff_pic_tmp0 = NULL;
890 id->p_ff_pic_tmp1 = NULL;
891 id->p_ff_pic_tmp2 = NULL;
892 id->p_vresample = NULL;
896 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream, sout_stream_id_t *id )
900 avcodec_close( id->ff_dec_c );
902 if( id->b_enc_inited )
904 avcodec_close( id->ff_enc_c );
909 free( id->p_ff_pic );
912 if( id->p_ff_pic_tmp0 )
914 free( id->p_ff_pic_tmp0->data[0] );
915 free( id->p_ff_pic_tmp0 );
917 if( id->p_ff_pic_tmp1)
919 free( id->p_ff_pic_tmp1->data[0] );
920 free( id->p_ff_pic_tmp1 );
922 if( id->p_ff_pic_tmp2)
924 free( id->p_ff_pic_tmp2->data[0] );
925 free( id->p_ff_pic_tmp2 );
927 if( id->p_vresample )
929 free( id->p_vresample );
932 free( id->ff_dec_c );
933 free( id->ff_enc_c );
935 free( id->p_buffer );
938 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream, sout_stream_id_t *id,
939 sout_buffer_t *in, sout_buffer_t **out )
952 p_data = in->p_buffer;
957 frame = id->p_ff_pic;
960 i_used = avcodec_decode_video( id->ff_dec_c, frame,
967 avpicture_fill( (AVPicture*)frame,
969 id->ff_dec_c->pix_fmt,
970 id->ff_dec_c->width, id->ff_dec_c->height );
977 msg_Warn( p_stream, "error");
988 if( !id->b_enc_inited )
990 /* XXX hack because of copy packetizer and mpeg4video that can failed
992 if( id->ff_enc_c->width <= 0 || id->ff_enc_c->height <= 0 )
994 id->ff_enc_c->width = id->f_dst.i_width = id->ff_dec_c->width;
995 id->ff_enc_c->height = id->f_dst.i_height = id->ff_dec_c->height;
998 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
1000 msg_Err( p_stream, "cannot open encoder" );
1001 return VLC_EGENERIC;
1004 if( !( id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out, &id->f_dst ) ) )
1006 msg_Err( p_stream, "cannot add this stream" );
1007 transcode_video_ffmpeg_close( p_stream, id );
1008 id->b_transcode = VLC_FALSE;
1009 return VLC_EGENERIC;
1011 id->b_enc_inited = VLC_TRUE;
1016 if( p_stream->p_sys->b_deinterlace )
1018 if( id->p_ff_pic_tmp0 == NULL )
1022 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1023 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1024 id->ff_dec_c->width, id->ff_dec_c->height );
1026 buf = malloc( i_size );
1028 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1029 id->ff_enc_c->pix_fmt,
1030 id->ff_dec_c->width, id->ff_dec_c->height );
1033 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1034 id->ff_dec_c->pix_fmt,
1035 id->ff_dec_c->width, id->ff_dec_c->height );
1037 frame = id->p_ff_pic_tmp0;
1040 /* convert pix format */
1041 if( id->ff_dec_c->pix_fmt != id->ff_enc_c->pix_fmt )
1043 if( id->p_ff_pic_tmp1 == NULL )
1047 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1048 i_size = avpicture_get_size( id->ff_enc_c->pix_fmt,
1049 id->ff_dec_c->width, id->ff_dec_c->height );
1051 buf = malloc( i_size );
1053 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1054 id->ff_enc_c->pix_fmt,
1055 id->ff_dec_c->width, id->ff_dec_c->height );
1058 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->ff_enc_c->pix_fmt,
1059 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1060 id->ff_dec_c->width, id->ff_dec_c->height );
1062 frame = id->p_ff_pic_tmp1;
1065 /* convert size and crop */
1066 if( ( id->ff_dec_c->width != id->ff_enc_c->width ) ||
1067 ( id->ff_dec_c->height != id->ff_enc_c->height ) )
1069 if( id->p_ff_pic_tmp2 == NULL )
1073 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1074 i_size = avpicture_get_size( id->ff_enc_c->pix_fmt,
1075 id->ff_enc_c->width, id->ff_enc_c->height );
1077 buf = malloc( i_size );
1079 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1080 id->ff_enc_c->pix_fmt,
1081 id->ff_enc_c->width, id->ff_enc_c->height );
1084 img_resample_full_init( id->ff_enc_c->width, id->ff_enc_c->height,
1085 id->ff_dec_c->width, id->ff_dec_c->height,
1086 p_stream->p_sys->i_crop_top,
1087 p_stream->p_sys->i_crop_bottom,
1088 p_stream->p_sys->i_crop_left,
1089 p_stream->p_sys->i_crop_right );
1092 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2, (AVPicture*)frame );
1094 frame = id->p_ff_pic_tmp2;
1097 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer, id->i_buffer, frame );
1100 sout_buffer_t *p_out;
1101 p_out = sout_BufferNew( p_stream->p_sout, i_out );
1103 memcpy( p_out->p_buffer, id->p_buffer, i_out );
1105 p_out->i_size = i_out;
1106 p_out->i_length = in->i_length;
1107 p_out->i_dts = in->i_dts;
1108 p_out->i_pts = in->i_dts; /* FIXME */
1110 sout_BufferChain( out, p_out );