1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: transcode.c,v 1.15 2003/05/04 18:51:34 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 " " " " */
87 vlc_bool_t b_deinterlace;
95 /*****************************************************************************
97 *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
100 sout_stream_t *p_stream = (sout_stream_t*)p_this;
101 sout_stream_sys_t *p_sys;
104 p_sys = malloc( sizeof( sout_stream_sys_t ) );
105 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
108 p_sys->i_sample_rate = 0;
109 p_sys->i_channels = 0;
110 p_sys->i_abitrate = 0;
113 p_sys->i_vbitrate = 0;
114 p_sys->i_vtolerance = -1;
117 p_sys->i_key_int = -1;
120 p_sys->b_hq = VLC_FALSE;
121 p_sys->b_deinterlace= VLC_FALSE;
123 p_sys->i_crop_top = 0;
124 p_sys->i_crop_bottom= 0;
125 p_sys->i_crop_right = 0;
126 p_sys->i_crop_left = 0;
128 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "acodec" ) ) )
133 memcpy( fcc, codec, strlen( codec ) );
135 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
137 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "samplerate" ) ) )
139 p_sys->i_sample_rate = atoi( val );
141 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "channels" ) ) )
143 p_sys->i_channels = atoi( val );
145 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ab" ) ) )
147 p_sys->i_abitrate = atoi( val );
148 if( p_sys->i_abitrate < 4000 )
150 p_sys->i_abitrate *= 1000;
154 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
156 p_sys->i_sample_rate, p_sys->i_channels,
157 p_sys->i_abitrate / 1024 );
160 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "vcodec" ) ) )
165 memcpy( fcc, codec, strlen( codec ) );
167 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
169 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "width" ) ) )
171 p_sys->i_width = atoi( val );
173 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "height" ) ) )
175 p_sys->i_height = atoi( val );
177 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
179 p_sys->i_vbitrate = atoi( val );
180 if( p_sys->i_vbitrate < 16000 )
182 p_sys->i_vbitrate *= 1000;
185 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vt" ) ) )
187 p_sys->i_vtolerance = atoi( val );
189 if( sout_cfg_find( p_stream->p_cfg, "deinterlace" ) )
191 p_sys->b_deinterlace = VLC_TRUE;
194 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "croptop" ) ) )
196 p_sys->i_crop_top = atoi( val );
198 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropbottom" ) ) )
200 p_sys->i_crop_bottom = atoi( val );
202 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropleft" ) ) )
204 p_sys->i_crop_left = atoi( val );
206 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropright" ) ) )
208 p_sys->i_crop_right = atoi( val );
210 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "keyint" ) ) )
212 p_sys->i_key_int = atoi( val );
214 if( sout_cfg_find( p_stream->p_cfg, "hq" ) )
216 p_sys->b_hq = VLC_TRUE;
218 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmin" ) ) )
220 p_sys->i_qmin = atoi( val );
222 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmax" ) ) )
224 p_sys->i_qmax = atoi( val );
227 msg_Dbg( p_stream, "codec video=%4.4s %dx%d %dkb/s",
229 p_sys->i_width, p_sys->i_height,
230 p_sys->i_vbitrate / 1024 );
235 msg_Err( p_stream, "cannot create chain" );
239 p_stream->pf_add = Add;
240 p_stream->pf_del = Del;
241 p_stream->pf_send = Send;
243 p_stream->p_sys = p_sys;
246 avcodec_register_all();
251 /*****************************************************************************
253 *****************************************************************************/
255 static void Close( vlc_object_t * p_this )
257 sout_stream_t *p_stream = (sout_stream_t*)p_this;
258 sout_stream_sys_t *p_sys = p_stream->p_sys;
260 sout_stream_delete( p_sys->p_out );
264 struct sout_stream_id_t
266 vlc_fourcc_t b_transcode;
267 sout_format_t f_src; /* only if transcoding */
268 sout_format_t f_dst; /* " " " */
270 /* id of the out stream */
275 AVCodecContext *ff_dec_c;
278 vlc_fourcc_t b_enc_inited;
280 AVCodecContext *ff_enc_c;
287 uint8_t *p_buffer_in;
294 int i_buffer_out_pos;
295 uint8_t *p_buffer_out;
298 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
299 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
300 AVFrame *p_ff_pic_tmp2; /* to do resample */
302 ImgReSampleContext *p_vresample;
306 static sout_stream_id_t * Add ( sout_stream_t *p_stream, sout_format_t *p_fmt )
308 sout_stream_sys_t *p_sys = p_stream->p_sys;
309 sout_stream_id_t *id;
311 id = malloc( sizeof( sout_stream_id_t ) );
313 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
316 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
317 (char*)&p_fmt->i_fourcc,
318 (char*)&p_sys->i_acodec );
321 memcpy( &id->f_src, p_fmt, sizeof( sout_format_t ) );
323 /* create dst format */
324 id->f_dst.i_cat = AUDIO_ES;
325 id->f_dst.i_fourcc = p_sys->i_acodec;
326 id->f_dst.i_sample_rate = p_sys->i_sample_rate > 0 ? p_sys->i_sample_rate : id->f_src.i_sample_rate;
327 id->f_dst.i_channels = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.i_channels;
328 id->f_dst.i_bitrate = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
329 id->f_dst.i_block_align = 0;
330 id->f_dst.i_extra_data = 0;
331 id->f_dst.p_extra_data = NULL;
333 /* build decoder -> filter -> encoder */
334 if( transcode_audio_ffmpeg_new( p_stream, id ) )
336 msg_Err( p_stream, "cannot create audio chain" );
341 /* open output stream */
342 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
343 id->b_transcode = VLC_TRUE;
345 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
348 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
349 (char*)&p_fmt->i_fourcc,
350 (char*)&p_sys->i_vcodec );
352 memcpy( &id->f_src, p_fmt, sizeof( sout_format_t ) );
354 /* create dst format */
355 id->f_dst.i_cat = VIDEO_ES;
356 id->f_dst.i_fourcc = p_sys->i_vcodec;
357 id->f_dst.i_width = p_sys->i_width ; /* > 0 ? p_sys->i_width : id->f_src.i_width; */
358 id->f_dst.i_height = p_sys->i_height; /* > 0 ? p_sys->i_height: id->f_src.i_height; */
359 id->f_dst.i_bitrate = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
360 id->f_dst.i_extra_data = 0;
361 id->f_dst.p_extra_data = NULL;
363 /* build decoder -> filter -> encoder */
364 if( transcode_video_ffmpeg_new( p_stream, id ) )
366 msg_Err( p_stream, "cannot create video chain" );
371 /* open output stream */
372 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
374 id->b_transcode = VLC_TRUE;
378 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_fourcc );
379 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
380 id->b_transcode = VLC_FALSE;
392 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
394 sout_stream_sys_t *p_sys = p_stream->p_sys;
396 if( id->b_transcode )
398 if( id->f_src.i_cat == AUDIO_ES )
400 transcode_audio_ffmpeg_close( p_stream, id );
402 else if( id->f_src.i_cat == VIDEO_ES )
404 transcode_video_ffmpeg_close( p_stream, id );
408 p_sys->p_out->pf_del( p_sys->p_out, id->id );
414 static int Send ( sout_stream_t *p_stream, sout_stream_id_t *id, sout_buffer_t *p_buffer )
416 sout_stream_sys_t *p_sys = p_stream->p_sys;
418 if( id->b_transcode )
420 sout_buffer_t *p_buffer_out;
421 if( id->f_src.i_cat == AUDIO_ES )
423 transcode_audio_ffmpeg_process( p_stream, id, p_buffer, &p_buffer_out );
425 else if( id->f_src.i_cat == VIDEO_ES )
427 transcode_video_ffmpeg_process( p_stream, id, p_buffer, &p_buffer_out );
429 sout_BufferDelete( p_stream->p_sout, p_buffer );
433 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
437 else if( id->id != NULL )
439 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
443 sout_BufferDelete( p_stream->p_sout, p_buffer );
448 /****************************************************************************
449 * ffmpeg decoder reencocdr part
450 ****************************************************************************/
456 } fourcc_to_ff_code[] =
459 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
460 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
461 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
462 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
463 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
466 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
467 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
468 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
469 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
470 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
471 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
472 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
473 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
474 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
475 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
476 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
477 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
478 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
480 /* raw video code, only used for 'encoding' */
481 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
482 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
483 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
484 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
485 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
487 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
490 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
494 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
496 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
498 return fourcc_to_ff_code[i].i_ff_codec;
505 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
509 case VLC_FOURCC( 'I', '4', '2', '0' ):
510 return PIX_FMT_YUV420P;
511 case VLC_FOURCC( 'I', '4', '2', '2' ):
512 return PIX_FMT_YUV422P;
513 case VLC_FOURCC( 'I', '4', '4', '4' ):
514 return PIX_FMT_YUV444P;
515 case VLC_FOURCC( 'R', 'V', '2', '4' ):
516 return PIX_FMT_RGB24;
517 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
518 return PIX_FMT_YUV422;
524 static int transcode_audio_ffmpeg_new ( sout_stream_t *p_stream, sout_stream_id_t *id )
528 if( id->f_src.i_fourcc == VLC_FOURCC('s','1','6','l') ||
529 id->f_src.i_fourcc == VLC_FOURCC('s','1','6','b') ||
530 id->f_src.i_fourcc == VLC_FOURCC('s','8',' ',' ') ||
531 id->f_src.i_fourcc == VLC_FOURCC('u','8',' ',' ') )
535 id->ff_dec_c = avcodec_alloc_context();
536 id->ff_dec_c->sample_rate = id->f_src.i_sample_rate;
537 id->ff_dec_c->channels = id->f_src.i_channels;
538 id->ff_dec_c->block_align = id->f_src.i_block_align;
539 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
544 i_ff_codec = get_ff_codec( id->f_src.i_fourcc );
545 if( i_ff_codec == 0 )
547 msg_Err( p_stream, "cannot find decoder" );
551 id->ff_dec = avcodec_find_decoder( i_ff_codec );
554 msg_Err( p_stream, "cannot find decoder" );
558 id->ff_dec_c = avcodec_alloc_context();
559 id->ff_dec_c->sample_rate = id->f_src.i_sample_rate;
560 id->ff_dec_c->channels = id->f_src.i_channels;
561 id->ff_dec_c->block_align = id->f_src.i_block_align;
562 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
564 id->ff_dec_c->extradata_size = id->f_src.i_extra_data;
565 id->ff_dec_c->extradata = id->f_src.p_extra_data;
566 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
568 msg_Err( p_stream, "cannot open decoder" );
574 i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
575 if( i_ff_codec == 0 )
577 msg_Err( p_stream, "cannot find encoder" );
581 id->ff_enc = avcodec_find_encoder( i_ff_codec );
584 msg_Err( p_stream, "cannot find encoder" );
588 id->ff_enc_c = avcodec_alloc_context();
589 id->ff_enc_c->bit_rate = id->f_dst.i_bitrate;
590 id->ff_enc_c->sample_rate = id->f_dst.i_sample_rate;
591 id->ff_enc_c->channels = id->f_dst.i_channels;
593 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
595 msg_Err( p_stream, "cannot open encoder" );
600 id->i_buffer_in = AVCODEC_MAX_AUDIO_FRAME_SIZE;
601 id->i_buffer_in_pos = 0;
602 id->p_buffer_in = malloc( id->i_buffer_in );
604 id->i_buffer = AVCODEC_MAX_AUDIO_FRAME_SIZE;
605 id->i_buffer_pos = 0;
606 id->p_buffer = malloc( id->i_buffer );
608 id->i_buffer_out = AVCODEC_MAX_AUDIO_FRAME_SIZE;
609 id->i_buffer_out_pos = 0;
610 id->p_buffer_out = malloc( id->i_buffer_out );
615 static void transcode_audio_ffmpeg_close ( sout_stream_t *p_stream, sout_stream_id_t *id )
619 avcodec_close( id->ff_dec_c );
621 avcodec_close( id->ff_enc_c );
623 free( id->ff_dec_c );
624 free( id->ff_enc_c );
626 free( id->p_buffer_in );
627 free( id->p_buffer );
628 free( id->p_buffer_out );
631 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream, sout_stream_id_t *id,
632 sout_buffer_t *in, sout_buffer_t **out )
636 /* gather data into p_buffer_in */
637 id->i_dts = in->i_dts -
639 (mtime_t)(id->i_buffer_pos / 2 / id->ff_enc_c->channels )/
640 (mtime_t)id->ff_enc_c->sample_rate;
642 if( id->i_buffer_in_pos + in->i_size > id->i_buffer_in )
644 /* extend buffer_in */
645 id->i_buffer_in = id->i_buffer_in_pos + in->i_size + 1024;
646 id->p_buffer_in = realloc( id->p_buffer_in, id->i_buffer_in );
648 memcpy( &id->p_buffer_in[id->i_buffer_in_pos],
651 id->i_buffer_in_pos += in->i_size;
653 /* decode as many data as possible */
661 i_buffer_size = id->i_buffer - id->i_buffer_pos;
663 i_used = avcodec_decode_audio( id->ff_dec_c,
664 (int16_t*)&id->p_buffer[id->i_buffer_pos], &i_buffer_size,
665 id->p_buffer_in, id->i_buffer_in_pos );
667 /* msg_Warn( p_stream, "avcodec_decode_audio: %d used", i_used ); */
668 id->i_buffer_pos += i_buffer_size;
672 msg_Warn( p_stream, "error");
673 id->i_buffer_in_pos = 0;
676 else if( i_used < id->i_buffer_in_pos )
678 memmove( id->p_buffer_in,
679 &id->p_buffer_in[i_used],
680 id->i_buffer_in - i_used );
681 id->i_buffer_in_pos -= i_used;
685 id->i_buffer_in_pos = 0;
692 int16_t *sout = (int16_t*)&id->p_buffer[id->i_buffer_pos];
695 if( id->f_src.i_fourcc == VLC_FOURCC( 's', '8', ' ', ' ' ) )
697 int8_t *sin = (int8_t*)id->p_buffer_in;
698 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos ) / 2, id->i_buffer_in_pos );
700 while( i_samples > 0 )
702 *sout++ = ( *sin++ ) << 8;
706 else if( id->f_src.i_fourcc == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
708 int8_t *sin = (int8_t*)id->p_buffer_in;
709 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos ) / 2, id->i_buffer_in_pos );
711 while( i_samples > 0 )
713 *sout++ = ( *sin++ - 128 ) << 8;
717 else if( id->f_src.i_fourcc == VLC_FOURCC( 's', '1', '6', 'l' ) )
719 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos ) / 2, id->i_buffer_in_pos / 2);
720 #ifdef WORDS_BIGENDIAN
721 uint8_t *sin = (uint8_t*)id->p_buffer_in;
722 i_used = i_samples * 2;
723 while( i_samples > 0 )
729 *sout++ = *(int16_t*)tmp;
734 memcpy( sout, id->p_buffer_in, i_samples * 2 );
736 i_used = i_samples * 2;
739 else if( id->f_src.i_fourcc == VLC_FOURCC( 's', '1', '6', 'b' ) )
741 int i_samples = __MIN( ( id->i_buffer - id->i_buffer_pos ) / 2, id->i_buffer_in_pos / 2);
742 #ifdef WORDS_BIGENDIAN
743 memcpy( sout, id->p_buffer_in, i_samples * 2 );
745 i_used = i_samples * 2;
747 uint8_t *sin = (uint8_t*)id->p_buffer_in;
748 i_used = i_samples * 2;
749 while( i_samples > 0 )
755 *sout++ = *(int16_t*)tmp;
761 id->i_buffer_pos = (uint8_t*)sout - id->p_buffer;
762 if( i_used < id->i_buffer_in_pos )
764 memmove( id->p_buffer_in,
765 &id->p_buffer_in[i_used],
766 id->i_buffer_in - i_used );
768 id->i_buffer_in_pos -= i_used;
771 /* encode as many data as possible */
774 int i_frame_size = id->ff_enc_c->frame_size * 2 * id->ff_enc_c->channels;
776 sout_buffer_t *p_out;
778 if( id->i_buffer_pos < i_frame_size )
783 /* msg_Warn( p_stream, "avcodec_encode_audio: frame size%d", i_frame_size); */
784 i_out_size = avcodec_encode_audio( id->ff_enc_c,
785 id->p_buffer_out, id->i_buffer_out,
786 (int16_t*)id->p_buffer );
788 if( i_out_size <= 0 )
792 memmove( id->p_buffer,
793 &id->p_buffer[i_frame_size],
794 id->i_buffer - i_frame_size );
795 id->i_buffer_pos -= i_frame_size;
797 p_out = sout_BufferNew( p_stream->p_sout, i_out_size );
798 memcpy( p_out->p_buffer, id->p_buffer_out, i_out_size );
799 p_out->i_size = i_out_size;
800 p_out->i_length = (mtime_t)1000000 * (mtime_t)id->ff_enc_c->frame_size / (mtime_t)id->ff_enc_c->sample_rate;
802 p_out->i_dts = id->i_dts;
803 p_out->i_pts = id->i_dts;
806 id->i_dts += p_out->i_length;
808 /* msg_Warn( p_stream, "frame dts=%lld len %lld out=%d", p_out->i_dts, p_out->i_length, i_out_size ); */
809 sout_BufferChain( out, p_out );
819 static int transcode_video_ffmpeg_new ( sout_stream_t *p_stream, sout_stream_id_t *id )
821 sout_stream_sys_t *p_sys = p_stream->p_sys;
825 if( id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '2', '0' ) ||
826 id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '2', '2' ) ||
827 id->f_src.i_fourcc == VLC_FOURCC( 'I', '4', '4', '4' ) ||
828 id->f_src.i_fourcc == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
829 id->f_src.i_fourcc == VLC_FOURCC( 'R', 'V', '2', '4' ) )
832 id->ff_dec_c = avcodec_alloc_context();
833 id->ff_dec_c->width = id->f_src.i_width;
834 id->ff_dec_c->height = id->f_src.i_height;
835 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_fourcc );
840 i_ff_codec = get_ff_codec( id->f_src.i_fourcc );
841 if( i_ff_codec == 0 )
843 msg_Err( p_stream, "cannot find decoder" );
847 id->ff_dec = avcodec_find_decoder( i_ff_codec );
850 msg_Err( p_stream, "cannot find decoder" );
854 id->ff_dec_c = avcodec_alloc_context();
855 id->ff_dec_c->width = id->f_src.i_width;
856 id->ff_dec_c->height = id->f_src.i_height;
857 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
858 id->ff_dec_c->extradata_size= id->f_src.i_extra_data;
859 id->ff_dec_c->extradata = id->f_src.p_extra_data;
860 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
861 id->ff_dec_c->error_resilience= -1;
862 if( id->ff_dec->capabilities & CODEC_CAP_TRUNCATED )
864 id->ff_dec_c->flags |= CODEC_FLAG_TRUNCATED;
867 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
869 msg_Err( p_stream, "cannot open decoder" );
873 if( i_ff_codec == CODEC_ID_MPEG4 && id->ff_dec_c->extradata_size > 0 )
878 avcodec_decode_video( id->ff_dec_c, &frame,
880 id->ff_dec_c->extradata, id->ff_dec_c->extradata_size );
886 i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
887 if( i_ff_codec == 0 )
889 msg_Err( p_stream, "cannot find encoder" );
893 id->ff_enc = avcodec_find_encoder( i_ff_codec );
896 msg_Err( p_stream, "cannot find encoder" );
900 id->ff_enc_c = avcodec_alloc_context();
901 id->ff_enc_c->width = id->f_dst.i_width;
902 id->ff_enc_c->height = id->f_dst.i_height;
903 id->ff_enc_c->bit_rate = id->f_dst.i_bitrate;
904 #if LIBAVCODEC_BUILD >= 4662
905 id->ff_enc_c->frame_rate = 25 ; /* FIXME as it break mpeg */
906 id->ff_enc_c->frame_rate_base= 1;
908 id->ff_enc_c->frame_rate = 25 * FRAME_RATE_BASE;
910 id->ff_enc_c->gop_size = p_sys->i_key_int >= 0 ? p_sys->i_key_int : 50;
912 if( p_sys->i_vtolerance >= 0 )
914 id->ff_enc_c->bit_rate_tolerance = p_sys->i_vtolerance;
916 id->ff_enc_c->qmin = p_sys->i_qmin;
917 id->ff_enc_c->qmax = p_sys->i_qmax;
920 id->ff_enc_c->flags |= CODEC_FLAG_HQ;
923 if( i_ff_codec == CODEC_ID_RAWVIDEO )
925 id->ff_enc_c->pix_fmt = get_ff_chroma( id->f_dst.i_fourcc );
927 /* XXX open it only when we have the first frame */
928 id->b_enc_inited = VLC_FALSE;
930 id->i_buffer_in_pos = 0;
931 id->p_buffer_in = NULL;
933 id->i_buffer = 3*1024*1024;
934 id->i_buffer_pos = 0;
935 id->p_buffer = malloc( id->i_buffer );
937 id->i_buffer_out = 0;
938 id->i_buffer_out_pos = 0;
939 id->p_buffer_out = NULL;
941 id->p_ff_pic = avcodec_alloc_frame();
942 id->p_ff_pic_tmp0 = NULL;
943 id->p_ff_pic_tmp1 = NULL;
944 id->p_ff_pic_tmp2 = NULL;
945 id->p_vresample = NULL;
949 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream, sout_stream_id_t *id )
953 avcodec_close( id->ff_dec_c );
955 if( id->b_enc_inited )
957 avcodec_close( id->ff_enc_c );
962 free( id->p_ff_pic );
965 if( id->p_ff_pic_tmp0 )
967 free( id->p_ff_pic_tmp0->data[0] );
968 free( id->p_ff_pic_tmp0 );
970 if( id->p_ff_pic_tmp1)
972 free( id->p_ff_pic_tmp1->data[0] );
973 free( id->p_ff_pic_tmp1 );
975 if( id->p_ff_pic_tmp2)
977 free( id->p_ff_pic_tmp2->data[0] );
978 free( id->p_ff_pic_tmp2 );
980 if( id->p_vresample )
982 free( id->p_vresample );
985 free( id->ff_dec_c );
986 free( id->ff_enc_c );
988 free( id->p_buffer );
991 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream, sout_stream_id_t *id,
992 sout_buffer_t *in, sout_buffer_t **out )
994 sout_stream_sys_t *p_sys = p_stream->p_sys;
1005 i_data = in->i_size;
1006 p_data = in->p_buffer;
1011 frame = id->p_ff_pic;
1014 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1021 avpicture_fill( (AVPicture*)frame,
1023 id->ff_dec_c->pix_fmt,
1024 id->ff_dec_c->width, id->ff_dec_c->height );
1031 msg_Warn( p_stream, "error");
1032 return VLC_EGENERIC;
1042 if( !id->b_enc_inited )
1044 /* XXX hack because of copy packetizer and mpeg4video that can failed
1046 if( id->ff_enc_c->width <= 0 )
1048 id->ff_enc_c->width =
1049 id->f_dst.i_width = id->ff_dec_c->width - p_sys->i_crop_left - p_sys->i_crop_right;
1051 if( id->ff_enc_c->height <= 0 )
1053 id->ff_enc_c->height =
1054 id->f_dst.i_height = id->ff_dec_c->height - p_sys->i_crop_top - p_sys->i_crop_bottom;
1057 if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
1059 msg_Err( p_stream, "cannot open encoder" );
1060 return VLC_EGENERIC;
1063 if( !( id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out, &id->f_dst ) ) )
1065 msg_Err( p_stream, "cannot add this stream" );
1066 transcode_video_ffmpeg_close( p_stream, id );
1067 id->b_transcode = VLC_FALSE;
1068 return VLC_EGENERIC;
1070 id->b_enc_inited = VLC_TRUE;
1075 if( p_stream->p_sys->b_deinterlace )
1077 if( id->p_ff_pic_tmp0 == NULL )
1081 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1082 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1083 id->ff_dec_c->width, id->ff_dec_c->height );
1085 buf = malloc( i_size );
1087 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1088 id->ff_enc_c->pix_fmt,
1089 id->ff_dec_c->width, id->ff_dec_c->height );
1092 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1093 id->ff_dec_c->pix_fmt,
1094 id->ff_dec_c->width, id->ff_dec_c->height );
1096 frame = id->p_ff_pic_tmp0;
1099 /* convert pix format */
1100 if( id->ff_dec_c->pix_fmt != id->ff_enc_c->pix_fmt )
1102 if( id->p_ff_pic_tmp1 == NULL )
1106 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1107 i_size = avpicture_get_size( id->ff_enc_c->pix_fmt,
1108 id->ff_dec_c->width, id->ff_dec_c->height );
1110 buf = malloc( i_size );
1112 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1113 id->ff_enc_c->pix_fmt,
1114 id->ff_dec_c->width, id->ff_dec_c->height );
1117 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->ff_enc_c->pix_fmt,
1118 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1119 id->ff_dec_c->width, id->ff_dec_c->height );
1121 frame = id->p_ff_pic_tmp1;
1124 /* convert size and crop */
1125 if( id->ff_dec_c->width != id->ff_enc_c->width ||
1126 id->ff_dec_c->height != id->ff_enc_c->height ||
1127 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1128 p_sys->i_crop_left > 0 || p_sys->i_crop_right )
1130 if( id->p_ff_pic_tmp2 == NULL )
1134 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1135 i_size = avpicture_get_size( id->ff_enc_c->pix_fmt,
1136 id->ff_enc_c->width, id->ff_enc_c->height );
1138 buf = malloc( i_size );
1140 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1141 id->ff_enc_c->pix_fmt,
1142 id->ff_enc_c->width, id->ff_enc_c->height );
1145 img_resample_full_init( id->ff_enc_c->width, id->ff_enc_c->height,
1146 id->ff_dec_c->width, id->ff_dec_c->height,
1147 p_stream->p_sys->i_crop_top,
1148 p_stream->p_sys->i_crop_bottom,
1149 p_stream->p_sys->i_crop_left,
1150 p_stream->p_sys->i_crop_right );
1153 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2, (AVPicture*)frame );
1155 frame = id->p_ff_pic_tmp2;
1158 i_out = avcodec_encode_video( id->ff_enc_c, id->p_buffer, id->i_buffer, frame );
1161 sout_buffer_t *p_out;
1162 p_out = sout_BufferNew( p_stream->p_sout, i_out );
1164 memcpy( p_out->p_buffer, id->p_buffer, i_out );
1166 p_out->i_size = i_out;
1167 p_out->i_length = in->i_length;
1168 p_out->i_dts = in->i_dts;
1169 p_out->i_pts = in->i_dts; /* FIXME */
1171 sout_BufferChain( out, p_out );