1 /*****************************************************************************
2 * transcode.c: transcoding stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
5 * $Id: transcode.c,v 1.81 2004/03/03 11:29:26 massiot Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
35 #include <vlc/decoder.h>
38 #ifdef HAVE_FFMPEG_AVCODEC_H
39 # include <ffmpeg/avcodec.h>
44 #if LIBAVCODEC_BUILD < 4704
45 # define AV_NOPTS_VALUE 0
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close ( vlc_object_t * );
54 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
55 static int Del ( sout_stream_t *, sout_stream_id_t * );
56 static int Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
58 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
59 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
60 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
62 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
63 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
64 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
66 static int transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
68 static int EncoderThread( struct sout_stream_sys_t * p_sys );
70 static int pi_channels_maps[6] =
73 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
74 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
75 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
76 | AOUT_CHAN_REARRIGHT,
77 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
78 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
81 /*****************************************************************************
83 *****************************************************************************/
85 set_description( _("Transcode stream output") );
86 set_capability( "sout stream", 50 );
87 add_shortcut( "transcode" );
88 set_callbacks( Open, Close );
91 #define PICTURE_RING_SIZE 64
93 struct sout_stream_sys_t
97 sout_stream_t * p_out;
98 sout_stream_id_t * id_video;
99 sout_buffer_t * p_buffers;
100 vlc_mutex_t lock_out;
102 picture_t * pp_pics[PICTURE_RING_SIZE];
103 int i_first_pic, i_last_pic;
105 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
110 vlc_fourcc_t i_vcodec; /* " video " " " " */
121 vlc_bool_t b_deinterlace;
122 vlc_bool_t b_interlace;
123 vlc_bool_t b_strict_rc;
125 vlc_bool_t b_hurry_up;
126 int i_rc_buffer_size;
127 float f_rc_buffer_aggressivity;
128 float f_i_quant_factor;
129 int i_noise_reduction;
130 vlc_bool_t b_mpeg4_matrix;
132 vlc_bool_t b_trellis;
140 mtime_t i_output_pts;
143 /*****************************************************************************
145 *****************************************************************************/
146 static int Open( vlc_object_t *p_this )
148 sout_stream_t *p_stream = (sout_stream_t*)p_this;
149 sout_stream_sys_t *p_sys;
152 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
153 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
156 p_sys->i_vtolerance = -1;
157 p_sys->i_key_int = -1;
160 p_sys->f_i_quant_factor = 0.0;
161 #if LIBAVCODEC_BUILD >= 4673
162 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
164 p_sys->i_hq = VLC_FALSE;
166 p_sys->i_rc_buffer_size = 224*1024*8 * 3/2;
167 p_sys->f_rc_buffer_aggressivity = 0.1;
168 p_sys->i_threads = 0;
169 p_sys->b_trellis = 0;
171 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "acodec" ) ) )
176 memcpy( fcc, codec, __MIN( strlen( codec ), 4 ) );
178 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
180 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "samplerate" ) ) )
182 p_sys->i_sample_rate = atoi( val );
184 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "channels" ) ) )
186 p_sys->i_channels = atoi( val );
188 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ab" ) ) )
190 p_sys->i_abitrate = atoi( val );
191 if( p_sys->i_abitrate < 4000 )
193 p_sys->i_abitrate *= 1000;
197 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s", fcc,
198 p_sys->i_sample_rate, p_sys->i_channels,
199 p_sys->i_abitrate / 1000 );
202 if( ( codec = sout_cfg_find_value( p_stream->p_cfg, "vcodec" ) ) )
207 memcpy( fcc, codec, __MIN( strlen( codec ), 4 ) );
209 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
211 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "scale" ) ) )
213 p_sys->f_scale = atof( val );
215 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "width" ) ) )
217 p_sys->i_width = atoi( val );
219 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "height" ) ) )
221 p_sys->i_height = atoi( val );
223 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
225 p_sys->i_vbitrate = atoi( val );
226 if( p_sys->i_vbitrate < 16000 )
228 p_sys->i_vbitrate *= 1000;
231 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vt" ) ) )
233 p_sys->i_vtolerance = atoi( val );
235 if( sout_cfg_find( p_stream->p_cfg, "deinterlace" ) )
237 p_sys->b_deinterlace = VLC_TRUE;
239 if( sout_cfg_find( p_stream->p_cfg, "interlace" ) )
241 p_sys->b_interlace = VLC_TRUE;
243 if( sout_cfg_find( p_stream->p_cfg, "strict_rc" ) )
245 p_sys->b_strict_rc = VLC_TRUE;
247 if( sout_cfg_find( p_stream->p_cfg, "pre_me" ) )
249 p_sys->b_pre_me = VLC_TRUE;
251 if( sout_cfg_find( p_stream->p_cfg, "hurry_up" ) )
253 p_sys->b_hurry_up = VLC_TRUE;
254 /* hurry up mode needs noise reduction, even small */
255 p_sys->i_noise_reduction = 1;
257 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "rc_buffer_size" ) ) )
259 p_sys->i_rc_buffer_size = atoi( val );
261 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "rc_buffer_aggressivity" ) ) )
263 p_sys->f_rc_buffer_aggressivity = atof( val );
265 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "i_quant_factor" ) ) )
267 p_sys->f_i_quant_factor = atof( val );
269 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "noise_reduction" ) ) )
271 p_sys->i_noise_reduction = atoi( val );
273 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "mpeg4_matrix" ) ) )
275 p_sys->b_mpeg4_matrix = VLC_TRUE;
278 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "croptop" ) ) )
280 p_sys->i_crop_top = atoi( val );
282 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropbottom" ) ) )
284 p_sys->i_crop_bottom = atoi( val );
286 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropleft" ) ) )
288 p_sys->i_crop_left = atoi( val );
290 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "cropright" ) ) )
292 p_sys->i_crop_right = atoi( val );
294 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "keyint" ) ) )
296 p_sys->i_key_int = atoi( val );
298 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "bframes" ) ) )
300 p_sys->i_b_frames = atoi( val );
302 #if LIBAVCODEC_BUILD >= 4673
303 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "hq" ) ) )
305 if( !strcmp( val, "rd" ) )
307 p_sys->i_hq = FF_MB_DECISION_RD;
309 else if( !strcmp( val, "bits" ) )
311 p_sys->i_hq = FF_MB_DECISION_BITS;
313 else if( !strcmp( val, "simple" ) )
315 p_sys->i_hq = FF_MB_DECISION_SIMPLE;
319 p_sys->i_hq = FF_MB_DECISION_RD;
323 if( sout_cfg_find( p_stream->p_cfg, "hq" ) )
325 p_sys->i_hq = VLC_TRUE;
328 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmin" ) ) )
330 p_sys->i_qmin = atoi( val );
332 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "qmax" ) ) )
334 p_sys->i_qmax = atoi( val );
336 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "threads" ) ) )
338 p_sys->i_threads = atoi( val );
340 if( sout_cfg_find( p_stream->p_cfg, "trellis" ) )
342 p_sys->b_trellis = VLC_TRUE;
345 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
346 fcc, p_sys->i_width, p_sys->i_height, p_sys->f_scale,
347 p_sys->i_vbitrate / 1000 );
352 msg_Err( p_stream, "cannot create chain" );
357 p_stream->pf_add = Add;
358 p_stream->pf_del = Del;
359 p_stream->pf_send = Send;
360 p_stream->p_sys = p_sys;
363 avcodec_register_all();
365 /* ffmpeg needs some padding at the end of each buffer */
366 p_stream->p_sout->i_padding += FF_INPUT_BUFFER_PADDING_SIZE;
371 /*****************************************************************************
373 *****************************************************************************/
374 static void Close( vlc_object_t * p_this )
376 sout_stream_t *p_stream = (sout_stream_t*)p_this;
377 sout_stream_sys_t *p_sys = p_stream->p_sys;
379 sout_stream_delete( p_sys->p_out );
380 vlc_object_destroy( p_sys );
383 struct sout_stream_id_t
385 vlc_fourcc_t b_transcode;
386 es_format_t f_src; /* only if transcoding */
387 es_format_t f_dst; /* " " " */
388 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
390 /* id of the out stream */
394 encoder_t *p_encoder;
395 vlc_fourcc_t b_enc_inited;
399 AVCodecContext *ff_dec_c;
409 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
410 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
411 AVFrame *p_ff_pic_tmp2; /* to do resample */
413 ImgReSampleContext *p_vresample;
417 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
419 sout_stream_sys_t *p_sys = p_stream->p_sys;
420 sout_stream_id_t *id;
422 id = malloc( sizeof( sout_stream_id_t ) );
425 id->p_encoder = NULL;
427 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
430 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
431 (char*)&p_fmt->i_codec,
432 (char*)&p_sys->i_acodec );
435 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
437 /* create dst format */
438 es_format_Init( &id->f_dst, AUDIO_ES, p_sys->i_acodec );
439 id->f_dst.i_id = id->f_src.i_id;
440 id->f_dst.i_group = id->f_src.i_group;
441 if( id->f_src.psz_language ) id->f_dst.psz_language = strdup( id->f_src.psz_language );
442 id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ? p_sys->i_sample_rate : id->f_src.audio.i_rate;
443 id->f_dst.audio.i_channels = p_sys->i_channels > 0 ? p_sys->i_channels : id->f_src.audio.i_channels;
444 id->f_dst.i_bitrate = p_sys->i_abitrate > 0 ? p_sys->i_abitrate : 64000;
445 id->f_dst.audio.i_blockalign = 0;
446 id->f_dst.i_extra = 0;
447 id->f_dst.p_extra = NULL;
449 /* build decoder -> filter -> encoder */
450 if( transcode_audio_ffmpeg_new( p_stream, id ) )
452 msg_Err( p_stream, "cannot create audio chain" );
457 /* open output stream */
458 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
459 id->b_transcode = VLC_TRUE;
467 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_vcodec != 0 )
470 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
471 (char*)&p_fmt->i_codec,
472 (char*)&p_sys->i_vcodec );
474 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
476 /* create dst format */
477 es_format_Init( &id->f_dst, VIDEO_ES, p_sys->i_vcodec );
478 id->f_dst.i_id = id->f_src.i_id;
479 id->f_dst.i_group = id->f_src.i_group;
480 if( id->f_src.psz_language ) id->f_dst.psz_language = strdup( id->f_src.psz_language );
481 id->f_dst.video.i_width = p_sys->i_width;
482 id->f_dst.video.i_height= p_sys->i_height;
483 id->f_dst.i_bitrate = p_sys->i_vbitrate > 0 ? p_sys->i_vbitrate : 800*1000;
484 id->f_dst.i_extra = 0;
485 id->f_dst.p_extra = NULL;
487 /* build decoder -> filter -> encoder */
488 if( transcode_video_ffmpeg_new( p_stream, id ) )
490 msg_Err( p_stream, "cannot create video chain" );
495 /* open output stream */
496 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
498 id->b_transcode = VLC_TRUE;
502 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
503 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
504 id->b_transcode = VLC_FALSE;
516 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
518 sout_stream_sys_t *p_sys = p_stream->p_sys;
520 if( id->b_transcode )
522 if( id->f_src.i_cat == AUDIO_ES )
524 transcode_audio_ffmpeg_close( p_stream, id );
526 else if( id->f_src.i_cat == VIDEO_ES )
528 transcode_video_ffmpeg_close( p_stream, id );
532 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
538 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
539 sout_buffer_t *p_buffer )
541 sout_stream_sys_t *p_sys = p_stream->p_sys;
543 if( id->b_transcode )
545 sout_buffer_t *p_buffer_out;
546 if( id->f_src.i_cat == AUDIO_ES )
548 transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
551 else if( id->f_src.i_cat == VIDEO_ES )
553 if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
554 &p_buffer_out ) != VLC_SUCCESS )
556 sout_BufferDelete( p_stream->p_sout, p_buffer );
560 sout_BufferDelete( p_stream->p_sout, p_buffer );
564 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
568 else if( id->id != NULL )
570 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
574 sout_BufferDelete( p_stream->p_sout, p_buffer );
579 /****************************************************************************
580 * ffmpeg decoder reencocdr part
581 ****************************************************************************/
587 } fourcc_to_ff_code[] =
590 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
591 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
592 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
593 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
594 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
595 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
596 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
597 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
598 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
601 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
602 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
603 #if LIBAVCODEC_BUILD >= 4676
604 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
606 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
607 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
608 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
609 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
610 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
611 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
612 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
613 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
614 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
615 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
616 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
617 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
618 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
619 #if LIBAVCODEC_BUILD >= 4666
620 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
623 /* raw video code, only used for 'encoding' */
624 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
625 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
626 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
627 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
628 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
629 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
630 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
631 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
632 { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
633 { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
635 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
638 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
642 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
644 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
646 return fourcc_to_ff_code[i].i_ff_codec;
653 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
657 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
658 case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
659 case VLC_FOURCC( 'I', '4', '2', '0' ):
660 return PIX_FMT_YUV420P;
661 case VLC_FOURCC( 'I', '4', '2', '2' ):
662 return PIX_FMT_YUV422P;
663 case VLC_FOURCC( 'I', '4', '4', '4' ):
664 return PIX_FMT_YUV444P;
665 case VLC_FOURCC( 'R', 'V', '1', '5' ):
666 return PIX_FMT_RGB555;
667 case VLC_FOURCC( 'R', 'V', '1', '6' ):
668 return PIX_FMT_RGB565;
669 case VLC_FOURCC( 'R', 'V', '2', '4' ):
670 return PIX_FMT_BGR24;
671 case VLC_FOURCC( 'R', 'V', '3', '2' ):
672 return PIX_FMT_RGBA32;
673 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
674 return PIX_FMT_GRAY8;
675 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
676 return PIX_FMT_YUV422;
682 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
686 case PIX_FMT_YUV420P:
687 return VLC_FOURCC('I','4','2','0');
688 case PIX_FMT_YUV422P:
689 return VLC_FOURCC('I','4','2','2');
690 case PIX_FMT_YUV444P:
691 return VLC_FOURCC('I','4','4','4');
694 return VLC_FOURCC('Y','U','Y','2');
697 return VLC_FOURCC('R','V','1','5');
699 return VLC_FOURCC('R','V','1','6');
701 return VLC_FOURCC('R','V','2','4');
703 return VLC_FOURCC('R','V','3','2');
705 return VLC_FOURCC('G','R','E','Y');
707 case PIX_FMT_YUV410P:
708 case PIX_FMT_YUV411P:
715 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
716 sout_stream_id_t *id )
720 if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
721 id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
722 id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
723 id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
727 id->ff_dec_c = avcodec_alloc_context();
728 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
729 id->ff_dec_c->channels = id->f_src.audio.i_channels;
730 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
731 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
736 i_ff_codec = get_ff_codec( id->f_src.i_codec );
737 if( i_ff_codec == 0 )
739 msg_Err( p_stream, "cannot find decoder id" );
743 id->ff_dec = avcodec_find_decoder( i_ff_codec );
746 msg_Err( p_stream, "cannot find decoder (avcodec)" );
750 id->ff_dec_c = avcodec_alloc_context();
751 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
752 id->ff_dec_c->channels = id->f_src.audio.i_channels;
753 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
754 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
756 id->ff_dec_c->extradata_size = id->f_src.i_extra;
757 id->ff_dec_c->extradata = id->f_src.p_extra;
758 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
760 msg_Err( p_stream, "cannot open decoder" );
765 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
766 id->i_buffer_pos = 0;
767 id->p_buffer = malloc( id->i_buffer );
769 /* Sanity check for audio channels */
770 id->f_dst.audio.i_channels = __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
773 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
775 /* Initialization of encoder format structures */
776 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
777 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
778 id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
779 id->p_encoder->fmt_in.audio.i_physical_channels =
780 id->p_encoder->fmt_in.audio.i_original_channels =
781 pi_channels_maps[id->f_dst.audio.i_channels];
782 id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
784 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
785 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
786 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
788 id->p_encoder->p_module =
789 module_Need( id->p_encoder, "encoder", NULL );
790 if( !id->p_encoder->p_module )
792 vlc_object_destroy( id->p_encoder );
793 msg_Err( p_stream, "cannot open encoder" );
797 id->b_enc_inited = VLC_FALSE;
799 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
800 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
802 /* Hack for mp3 transcoding support */
803 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
805 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
811 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
812 sout_stream_id_t *id )
816 avcodec_close( id->ff_dec_c );
817 free( id->ff_dec_c );
820 module_Unneed( id->p_encoder, id->p_encoder->p_module );
821 vlc_object_destroy( id->p_encoder );
823 free( id->p_buffer );
826 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
827 sout_stream_id_t *id,
829 sout_buffer_t **out )
831 aout_buffer_t aout_buf;
833 int i_buffer = in->i_size;
834 char *p_buffer = in->p_buffer;
835 id->i_dts = in->i_dts;
840 id->i_buffer_pos = 0;
842 /* decode as much data as possible */
847 i_used = avcodec_decode_audio( id->ff_dec_c,
848 (int16_t*)id->p_buffer, &id->i_buffer_pos,
849 p_buffer, i_buffer );
852 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
857 msg_Warn( p_stream, "error audio decoding");
864 if ( id->i_buffer_pos < 0 )
866 msg_Warn( p_stream, "weird error audio decoding");
872 int16_t *sout = (int16_t*)id->p_buffer;
874 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
875 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
877 int8_t *sin = (int8_t*)p_buffer;
878 int i_used = __MIN( id->i_buffer/2, i_buffer );
879 int i_samples = i_used;
881 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
882 while( i_samples > 0 )
884 *sout++ = ( *sin++ ) << 8;
888 while( i_samples > 0 )
890 *sout++ = ( *sin++ - 128 ) << 8;
896 id->i_buffer_pos = i_used * 2;
898 else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
899 id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
901 int16_t *sin = (int16_t*)p_buffer;
902 int i_used = __MIN( id->i_buffer, i_buffer );
903 int i_samples = i_used / 2;
906 memcpy( sout, sin, i_used );
908 #ifdef WORDS_BIGENDIAN
909 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
911 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
914 uint8_t *dat = (uint8_t*)sout;
916 while( i_samples > 0 )
931 id->i_buffer_pos = i_used;
935 if( id->i_buffer_pos == 0 ) continue;
937 /* Encode as much data as possible */
938 if( !id->b_enc_inited && id->p_encoder->pf_header )
940 p_block = id->p_encoder->pf_header( id->p_encoder );
943 sout_buffer_t *p_out;
944 block_t *p_prev_block = p_block;
946 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
947 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
948 p_out->i_dts = p_out->i_pts = in->i_dts;
950 sout_BufferChain( out, p_out );
952 p_block = p_block->p_next;
953 block_Release( p_prev_block );
956 id->b_enc_inited = VLC_TRUE;
959 aout_buf.p_buffer = id->p_buffer;
960 aout_buf.i_nb_bytes = id->i_buffer_pos;
961 aout_buf.i_nb_samples = id->i_buffer_pos / 2 / id->f_src.audio.i_channels;
962 aout_buf.start_date = id->i_dts;
963 aout_buf.end_date = id->i_dts;
965 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
966 id->f_src.audio.i_channels / id->f_src.audio.i_rate );
968 if( id->f_src.audio.i_channels !=
969 id->p_encoder->fmt_in.audio.i_channels )
974 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
975 static const int translation[7][6] =
976 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
977 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
978 { 0, 1, 0, 0, 0, 0 }, /* 2 */
979 { 1, 2, 0, 0, 0, 0 }, /* 3 */
980 { 1, 3, 2, 0, 0, 0 }, /* 4 */
981 { 1, 3, 4, 2, 0, 0 }, /* 5 */
982 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
984 /* dumb downmixing */
985 for( i = 0; i < aout_buf.i_nb_samples; i++ )
987 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
988 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
990 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
991 p_buffer[i*id->f_src.audio.i_channels+
992 translation[id->f_src.audio.i_channels][j]];
995 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
998 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1001 sout_buffer_t *p_out;
1002 block_t *p_prev_block = p_block;
1004 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1005 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1006 p_out->i_dts = p_block->i_dts;
1007 p_out->i_pts = p_block->i_pts;
1008 p_out->i_length = p_block->i_length;
1009 sout_BufferChain( out, p_out );
1011 p_block = p_block->p_next;
1012 block_Release( p_prev_block );
1023 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1024 sout_stream_id_t *id )
1026 sout_stream_sys_t *p_sys = p_stream->p_sys;
1031 if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1032 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1033 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1034 id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1035 id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1036 id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1037 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1038 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1039 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1040 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1041 id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1044 id->ff_dec_c = avcodec_alloc_context();
1045 id->ff_dec_c->width = id->f_src.video.i_width;
1046 id->ff_dec_c->height = id->f_src.video.i_height;
1047 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_codec );
1049 #if LIBAVCODEC_BUILD >= 4687
1050 if( id->ff_dec_c->width )
1051 id->ff_dec_c->sample_aspect_ratio =
1052 av_d2q( id->f_src.video.i_aspect / (double)VOUT_ASPECT_FACTOR *
1053 id->ff_dec_c->height / id->ff_dec_c->width, 255 );
1055 id->ff_dec_c->aspect_ratio =
1056 id->f_src.video.i_aspect / (float)VOUT_ASPECT_FACTOR;
1062 i_ff_codec = get_ff_codec( id->f_src.i_codec );
1063 if( i_ff_codec == 0 )
1065 msg_Err( p_stream, "cannot find decoder" );
1066 return VLC_EGENERIC;
1069 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1072 msg_Err( p_stream, "cannot find decoder" );
1073 return VLC_EGENERIC;
1076 id->ff_dec_c = avcodec_alloc_context();
1077 id->ff_dec_c->width = id->f_src.video.i_width;
1078 id->ff_dec_c->height = id->f_src.video.i_height;
1079 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1080 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1081 id->ff_dec_c->extradata = id->f_src.p_extra;
1082 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1083 id->ff_dec_c->error_resilience= -1;
1084 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1085 id->ff_dec_c->opaque = p_sys;
1087 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1089 msg_Err( p_stream, "cannot open decoder" );
1090 return VLC_EGENERIC;
1093 if( i_ff_codec == CODEC_ID_MPEG4 && id->ff_dec_c->extradata_size > 0 )
1097 uint8_t *p_vol = malloc( id->ff_dec_c->extradata_size +
1098 FF_INPUT_BUFFER_PADDING_SIZE );
1100 memcpy( p_vol, id->ff_dec_c->extradata,
1101 id->ff_dec_c->extradata_size );
1102 memset( p_vol + id->ff_dec_c->extradata_size, 0,
1103 FF_INPUT_BUFFER_PADDING_SIZE );
1105 avcodec_decode_video( id->ff_dec_c, &frame, &b_gotpicture,
1106 id->ff_dec_c->extradata,
1107 id->ff_dec_c->extradata_size );
1113 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1115 /* Initialization of encoder format structures */
1116 es_format_Init( &id->p_encoder->fmt_in,
1117 id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1119 /* The dimensions will be set properly later on.
1120 * Just put sensible values so we can test if there is an encoder. */
1121 id->p_encoder->fmt_in.video.i_width = 16;
1122 id->p_encoder->fmt_in.video.i_height = 16;
1124 id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1125 id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1128 id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1129 #if LIBAVCODEC_BUILD >= 4662
1130 id->p_encoder->fmt_in.video.i_frame_rate_base =
1131 id->ff_dec_c->frame_rate_base;
1134 #if LIBAVCODEC_BUILD >= 4687
1135 if( id->ff_dec_c->height )
1136 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1137 ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1138 id->ff_dec_c->width / id->ff_dec_c->height );
1140 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1141 id->ff_dec_c->aspect_ratio;
1145 /* Check whether a particular aspect ratio was requested */
1146 if( id->f_src.video.i_aspect )
1148 id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1149 id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1152 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1153 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1154 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1156 id->p_encoder->i_vtolerance = p_sys->i_vtolerance;
1157 id->p_encoder->i_key_int = p_sys->i_key_int;
1158 id->p_encoder->i_b_frames = p_sys->i_b_frames;
1159 id->p_encoder->i_qmin = p_sys->i_qmin;
1160 id->p_encoder->i_qmax = p_sys->i_qmax;
1161 id->p_encoder->i_hq = p_sys->i_hq;
1162 id->p_encoder->b_strict_rc = p_sys->b_strict_rc;
1163 id->p_encoder->b_pre_me = p_sys->b_pre_me;
1164 id->p_encoder->b_hurry_up = p_sys->b_hurry_up;
1165 id->p_encoder->b_interlace = p_sys->b_interlace;
1166 id->p_encoder->i_rc_buffer_size = p_sys->i_rc_buffer_size;
1167 id->p_encoder->f_rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
1168 id->p_encoder->f_i_quant_factor = p_sys->f_i_quant_factor;
1169 id->p_encoder->i_noise_reduction = p_sys->i_noise_reduction;
1170 id->p_encoder->b_mpeg4_matrix = p_sys->b_mpeg4_matrix;
1171 id->p_encoder->i_threads = p_sys->i_threads;
1172 id->p_encoder->b_trellis = p_sys->b_trellis;
1174 id->p_ff_pic = avcodec_alloc_frame();
1175 id->p_ff_pic_tmp0 = NULL;
1176 id->p_ff_pic_tmp1 = NULL;
1177 id->p_ff_pic_tmp2 = NULL;
1178 id->p_vresample = NULL;
1180 id->p_encoder->p_module =
1181 module_Need( id->p_encoder, "encoder", NULL );
1183 if( !id->p_encoder->p_module )
1185 vlc_object_destroy( id->p_encoder );
1186 msg_Err( p_stream, "cannot find encoder" );
1187 return VLC_EGENERIC;
1190 /* Close the encoder.
1191 * We'll open it only when we have the first frame */
1192 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1193 id->p_encoder->p_module = NULL;
1195 id->b_enc_inited = VLC_FALSE;
1197 if ( p_sys->i_threads >= 1 )
1199 p_sys->id_video = id;
1200 vlc_mutex_init( p_stream, &p_sys->lock_out );
1201 vlc_cond_init( p_stream, &p_sys->cond );
1202 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1203 p_sys->i_first_pic = 0;
1204 p_sys->i_last_pic = 0;
1205 p_sys->p_buffers = NULL;
1206 p_sys->b_die = p_sys->b_error = 0;
1207 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1208 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1210 vlc_object_destroy( id->p_encoder );
1211 msg_Err( p_stream, "cannot spawn encoder thread" );
1212 return VLC_EGENERIC;
1219 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1220 sout_stream_id_t *id )
1222 if ( p_stream->p_sys->i_threads >= 1 )
1224 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1225 p_stream->p_sys->b_die = 1;
1226 vlc_cond_signal( &p_stream->p_sys->cond );
1227 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1228 vlc_thread_join( p_stream->p_sys );
1229 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1230 vlc_cond_destroy( &p_stream->p_sys->cond );
1236 avcodec_close( id->ff_dec_c );
1237 free( id->ff_dec_c );
1241 if( id->p_encoder->p_module )
1242 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1243 vlc_object_destroy( id->p_encoder );
1248 free( id->p_ff_pic );
1251 if( id->p_ff_pic_tmp0 )
1253 free( id->p_ff_pic_tmp0->data[0] );
1254 free( id->p_ff_pic_tmp0 );
1256 if( id->p_ff_pic_tmp1)
1258 free( id->p_ff_pic_tmp1->data[0] );
1259 free( id->p_ff_pic_tmp1 );
1261 if( id->p_ff_pic_tmp2)
1263 free( id->p_ff_pic_tmp2->data[0] );
1264 free( id->p_ff_pic_tmp2 );
1266 if( id->p_vresample )
1268 img_resample_close( id->p_vresample );
1272 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1273 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1275 sout_stream_sys_t *p_sys = p_stream->p_sys;
1285 i_data = in->i_size;
1286 p_data = in->p_buffer;
1295 frame = id->p_ff_pic;
1296 p_sys->i_input_pts = in->i_pts;
1299 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1306 avpicture_fill( (AVPicture*)frame, p_data,
1307 id->ff_dec_c->pix_fmt,
1308 id->ff_dec_c->width, id->ff_dec_c->height );
1313 frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts :
1319 msg_Warn( p_stream, "error");
1320 return VLC_EGENERIC;
1330 /* Get the pts of the decoded frame if any, otherwise keep the
1331 * interpolated one */
1332 if( frame->pts != AV_NOPTS_VALUE )
1334 p_sys->i_output_pts = frame->pts;
1337 if( !id->b_enc_inited )
1339 /* Hack because of the copy packetizer which can fail to detect the
1340 * proper size (which forces us to wait until the 1st frame
1342 int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1343 p_sys->i_crop_right;
1344 int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1345 p_sys->i_crop_bottom;
1347 if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1350 /* Apply the scaling */
1351 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1352 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1354 else if( id->f_dst.video.i_width > 0 &&
1355 id->f_dst.video.i_height <= 0 )
1357 id->f_dst.video.i_height =
1358 id->f_dst.video.i_width / (double)i_width * i_height;
1360 else if( id->f_dst.video.i_width <= 0 &&
1361 id->f_dst.video.i_height > 0 )
1363 id->f_dst.video.i_width =
1364 id->f_dst.video.i_height / (double)i_height * i_width;
1367 id->p_encoder->fmt_in.video.i_width =
1368 id->p_encoder->fmt_out.video.i_width =
1369 id->f_dst.video.i_width;
1370 id->p_encoder->fmt_in.video.i_height =
1371 id->p_encoder->fmt_out.video.i_height =
1372 id->f_dst.video.i_height;
1374 id->p_encoder->fmt_out.i_extra = 0;
1375 id->p_encoder->fmt_out.p_extra = NULL;
1377 id->p_encoder->p_module =
1378 module_Need( id->p_encoder, "encoder", NULL );
1379 if( !id->p_encoder->p_module )
1381 vlc_object_destroy( id->p_encoder );
1382 msg_Err( p_stream, "cannot find encoder" );
1383 id->b_transcode = VLC_FALSE;
1384 return VLC_EGENERIC;
1387 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1388 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1390 /* Hack for mp2v/mp1v transcoding support */
1391 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1392 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1394 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1398 p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1401 msg_Err( p_stream, "cannot add this stream" );
1402 transcode_video_ffmpeg_close( p_stream, id );
1403 id->b_transcode = VLC_FALSE;
1404 return VLC_EGENERIC;
1407 if( id->p_encoder->pf_header )
1409 p_block = id->p_encoder->pf_header( id->p_encoder );
1412 sout_buffer_t *p_out;
1413 block_t *p_prev_block = p_block;
1415 p_out = sout_BufferNew( p_stream->p_sout,
1416 p_block->i_buffer );
1417 memcpy( p_out->p_buffer, p_block->p_buffer,
1419 p_out->i_dts = p_out->i_pts = in->i_dts;
1420 p_out->i_length = 0;
1421 sout_BufferChain( out, p_out );
1423 p_block = p_block->p_next;
1424 block_Release( p_prev_block );
1428 id->i_inter_pixfmt =
1429 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1431 id->b_enc_inited = VLC_TRUE;
1435 if( p_stream->p_sys->b_deinterlace )
1437 if( id->p_ff_pic_tmp0 == NULL )
1441 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1442 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1443 id->ff_dec_c->width, id->ff_dec_c->height );
1445 buf = malloc( i_size );
1447 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1449 id->ff_dec_c->width, id->ff_dec_c->height );
1452 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0, (AVPicture*)frame,
1453 id->ff_dec_c->pix_fmt,
1454 id->ff_dec_c->width, id->ff_dec_c->height );
1456 #if LIBAVCODEC_BUILD >= 4685
1457 id->p_ff_pic_tmp0->interlaced_frame = 0;
1459 id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1460 frame = id->p_ff_pic_tmp0;
1463 /* convert pix format */
1464 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1466 if( id->p_ff_pic_tmp1 == NULL )
1470 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1471 i_size = avpicture_get_size( id->i_inter_pixfmt,
1472 id->ff_dec_c->width,
1473 id->ff_dec_c->height );
1475 buf = malloc( i_size );
1477 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1479 id->ff_dec_c->width, id->ff_dec_c->height );
1482 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1483 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1484 id->ff_dec_c->width, id->ff_dec_c->height );
1486 id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1487 #if LIBAVCODEC_BUILD >= 4685
1488 id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1489 id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1491 frame = id->p_ff_pic_tmp1;
1494 /* convert size and crop */
1495 if( id->ff_dec_c->width != id->f_dst.video.i_width ||
1496 id->ff_dec_c->height != id->f_dst.video.i_height ||
1497 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1498 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1500 if( id->p_ff_pic_tmp2 == NULL )
1504 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1505 i_size = avpicture_get_size( id->i_inter_pixfmt,
1506 id->f_dst.video.i_width,
1507 id->f_dst.video.i_height );
1509 buf = malloc( i_size );
1511 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1513 id->f_dst.video.i_width, id->f_dst.video.i_height );
1516 img_resample_full_init( id->f_dst.video.i_width,
1517 id->f_dst.video.i_height,
1518 id->ff_dec_c->width, id->ff_dec_c->height,
1519 p_stream->p_sys->i_crop_top,
1520 p_stream->p_sys->i_crop_bottom,
1521 p_stream->p_sys->i_crop_left,
1522 p_stream->p_sys->i_crop_right );
1525 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1526 (AVPicture*)frame );
1528 id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1529 #if LIBAVCODEC_BUILD >= 4685
1530 id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1531 id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1533 frame = id->p_ff_pic_tmp2;
1537 p_pic = malloc(sizeof(picture_t));
1538 vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1539 id->p_encoder->fmt_in.i_codec,
1540 id->f_dst.video.i_width, id->f_dst.video.i_height,
1541 id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1542 id->f_dst.video.i_height );
1544 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1546 p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1547 if ( p_sys->i_threads >= 1 )
1549 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines *
1550 p_pic->p[i_plane].i_pitch);
1551 p_stream->p_vlc->pf_memcpy( p_pic->p[i_plane].p_pixels,
1552 frame->data[i_plane], p_pic->p[i_plane].i_lines *
1553 p_pic->p[i_plane].i_pitch );
1557 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1561 /* Set the pts of the frame being encoded */
1562 p_pic->date = p_sys->i_output_pts;
1564 p_pic->i_nb_fields = frame->repeat_pict;
1565 #if LIBAVCODEC_BUILD >= 4685
1566 p_pic->b_progressive = !frame->interlaced_frame;
1567 p_pic->b_top_field_first = frame->top_field_first;
1570 /* Interpolate the next PTS
1571 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1572 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1574 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1575 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1578 if ( p_sys->i_threads >= 1 )
1580 vlc_mutex_lock( &p_sys->lock_out );
1581 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
1582 p_sys->i_last_pic %= PICTURE_RING_SIZE;
1583 *out = p_sys->p_buffers;
1584 p_sys->p_buffers = NULL;
1585 vlc_cond_signal( &p_sys->cond );
1586 vlc_mutex_unlock( &p_sys->lock_out );
1591 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1594 sout_buffer_t *p_out;
1595 block_t *p_prev_block = p_block;
1597 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1598 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1599 p_out->i_dts = p_block->i_dts;
1600 p_out->i_pts = p_block->i_pts;
1601 p_out->i_length = p_block->i_length;
1603 (p_block->i_flags << SOUT_BUFFER_FLAGS_BLOCK_SHIFT)
1604 & SOUT_BUFFER_FLAGS_BLOCK_MASK;
1605 sout_BufferChain( out, p_out );
1607 p_block = p_block->p_next;
1608 block_Release( p_prev_block );
1622 static int EncoderThread( sout_stream_sys_t * p_sys )
1624 sout_stream_t * p_stream = p_sys->p_out;
1625 sout_stream_id_t * id = p_sys->id_video;
1628 sout_buffer_t * p_buffer;
1630 while ( !p_sys->b_die && !p_sys->b_error )
1634 vlc_mutex_lock( &p_sys->lock_out );
1635 while ( p_sys->i_last_pic == p_sys->i_first_pic )
1637 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
1638 if ( p_sys->b_die || p_sys->b_error )
1641 if ( p_sys->b_die || p_sys->b_error )
1643 vlc_mutex_unlock( &p_sys->lock_out );
1647 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1648 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1649 vlc_mutex_unlock( &p_sys->lock_out );
1651 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
1652 vlc_mutex_lock( &p_sys->lock_out );
1655 sout_buffer_t *p_out;
1656 block_t *p_prev_block = p_block;
1658 p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
1659 memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
1660 p_out->i_dts = p_block->i_dts;
1661 p_out->i_pts = p_block->i_pts;
1662 p_out->i_length = p_block->i_length;
1664 (p_block->i_flags << SOUT_BUFFER_FLAGS_BLOCK_SHIFT)
1665 & SOUT_BUFFER_FLAGS_BLOCK_MASK;
1666 sout_BufferChain( &p_sys->p_buffers, p_out );
1668 p_block = p_block->p_next;
1669 block_Release( p_prev_block );
1671 vlc_mutex_unlock( &p_sys->lock_out );
1673 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1675 free( p_pic->p[i_plane].p_pixels );
1680 while ( p_sys->i_last_pic != p_sys->i_first_pic )
1682 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
1683 p_sys->i_first_pic %= PICTURE_RING_SIZE;
1685 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1687 free( p_pic->p[i_plane].p_pixels );
1692 p_buffer = p_sys->p_buffers;
1693 while ( p_buffer != NULL )
1695 sout_buffer_t * p_next = p_buffer->p_next;
1696 sout_BufferDelete( p_stream->p_sout, p_buffer );
1703 /*****************************************************************************
1704 * transcode_video_ffmpeg_getframebuf:
1706 * Callback used by ffmpeg to get a frame buffer.
1707 * We use it to get the right PTS for each decoded picture.
1708 *****************************************************************************/
1709 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
1712 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
1715 p_frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts : AV_NOPTS_VALUE;
1717 return avcodec_default_get_buffer( p_context, p_frame );