1 /*****************************************************************************
2 * transcode.c: transcoding stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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>
36 #include "vlc_filter.h"
40 #ifdef HAVE_FFMPEG_AVCODEC_H
41 # include <ffmpeg/avcodec.h>
46 #if LIBAVCODEC_BUILD < 4704
47 # define AV_NOPTS_VALUE 0
50 /*****************************************************************************
52 *****************************************************************************/
53 #define VENC_TEXT N_("Video encoder")
54 #define VENC_LONGTEXT N_( \
55 "Allows you to specify the video encoder to use and its associated " \
57 #define VCODEC_TEXT N_("Destination video codec")
58 #define VCODEC_LONGTEXT N_( \
59 "Allows you to specify the destination video codec used for the " \
61 #define VB_TEXT N_("Video bitrate")
62 #define VB_LONGTEXT N_( \
63 "Allows you to specify the video bitrate used for the streaming " \
65 #define SCALE_TEXT N_("Video scaling")
66 #define SCALE_LONGTEXT N_( \
67 "Allows you to scale the video before encoding." )
68 #define FPS_TEXT N_("Video frame-rate")
69 #define FPS_LONGTEXT N_( \
70 "Allows you to specify an output frame rate for the video." )
71 #define DEINTERLACE_TEXT N_("Deinterlace video")
72 #define DEINTERLACE_LONGTEXT N_( \
73 "Allows you to deinterlace the video before encoding." )
74 #define WIDTH_TEXT N_("Video width")
75 #define WIDTH_LONGTEXT N_( \
76 "Allows you to specify the output video width." )
77 #define HEIGHT_TEXT N_("Video height")
78 #define HEIGHT_LONGTEXT N_( \
79 "Allows you to specify the output video height." )
81 #define CROPTOP_TEXT N_("Video crop top")
82 #define CROPTOP_LONGTEXT N_( \
83 "Allows you to specify the top coordinate for the video cropping." )
84 #define CROPLEFT_TEXT N_("Video crop left")
85 #define CROPLEFT_LONGTEXT N_( \
86 "Allows you to specify the left coordinate for the video cropping." )
87 #define CROPBOTTOM_TEXT N_("Video crop bottom")
88 #define CROPBOTTOM_LONGTEXT N_( \
89 "Allows you to specify the bottom coordinate for the video cropping." )
90 #define CROPRIGHT_TEXT N_("Video crop right")
91 #define CROPRIGHT_LONGTEXT N_( \
92 "Allows you to specify the right coordinate for the video cropping." )
94 #define AENC_TEXT N_("Audio encoder")
95 #define AENC_LONGTEXT N_( \
96 "Allows you to specify the audio encoder to use and its associated " \
98 #define ACODEC_TEXT N_("Destination audio codec")
99 #define ACODEC_LONGTEXT N_( \
100 "Allows you to specify the destination audio codec used for the " \
101 "streaming output." )
102 #define AB_TEXT N_("Audio bitrate")
103 #define AB_LONGTEXT N_( \
104 "Allows you to specify the audio bitrate used for the streaming " \
106 #define ARATE_TEXT N_("Audio sample rate")
107 #define ARATE_LONGTEXT N_( \
108 "Allows you to specify the audio sample rate used for the streaming " \
110 #define ACHANS_TEXT N_("Audio channels")
111 #define ACHANS_LONGTEXT N_( \
112 "Allows you to specify the number of audio channels used for the " \
113 "streaming output." )
115 #define SENC_TEXT N_("Subtitles encoder")
116 #define SENC_LONGTEXT N_( \
117 "Allows you to specify the subtitles encoder to use and its associated " \
119 #define SCODEC_TEXT N_("Destination subtitles codec")
120 #define SCODEC_LONGTEXT N_( \
121 "Allows you to specify the destination subtitles codec used for the " \
122 "streaming output." )
124 #define THREADS_TEXT N_("Number of threads")
125 #define THREADS_LONGTEXT N_( \
126 "Allows you to specify the number of threads used for the transcoding." )
128 #define ASYNC_TEXT N_("Synchronise on audio track")
129 #define ASYNC_LONGTEXT N_( \
130 "This option will drop/duplicate video frames to synchronise the video " \
131 "track on the audio track." )
133 static int Open ( vlc_object_t * );
134 static void Close( vlc_object_t * );
136 #define SOUT_CFG_PREFIX "sout-transcode-"
139 #if defined(MODULE_NAME_is_stream_out_transcodealtivec) \
140 || (defined(CAN_COMPILE_ALTIVEC) && !defined(NO_ALTIVEC_IN_FFMPEG))
141 set_description( _("AltiVec transcode stream output") );
142 add_requirement( ALTIVEC );
143 set_capability( "sout stream", 51 );
145 set_description( _("Transcode stream output") );
146 set_capability( "sout stream", 50 );
148 add_shortcut( "transcode" );
149 set_callbacks( Open, Close );
151 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
152 VENC_LONGTEXT, VLC_FALSE );
153 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
154 VCODEC_LONGTEXT, VLC_FALSE );
155 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
156 VB_LONGTEXT, VLC_FALSE );
157 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
158 SCALE_LONGTEXT, VLC_FALSE );
159 add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
160 FPS_LONGTEXT, VLC_FALSE );
161 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
162 DEINTERLACE_LONGTEXT, VLC_FALSE );
163 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
164 WIDTH_LONGTEXT, VLC_TRUE );
165 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
166 HEIGHT_LONGTEXT, VLC_TRUE );
168 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
169 CROPTOP_LONGTEXT, VLC_TRUE );
170 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
171 CROPLEFT_LONGTEXT, VLC_TRUE );
172 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
173 CROPBOTTOM_LONGTEXT, VLC_TRUE );
174 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
175 CROPRIGHT_LONGTEXT, VLC_TRUE );
177 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
178 AENC_LONGTEXT, VLC_FALSE );
179 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
180 ACODEC_LONGTEXT, VLC_FALSE );
181 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
182 AB_LONGTEXT, VLC_FALSE );
183 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
184 ACHANS_LONGTEXT, VLC_FALSE );
185 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
186 ARATE_LONGTEXT, VLC_TRUE );
188 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
189 SENC_LONGTEXT, VLC_FALSE );
190 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
191 SCODEC_LONGTEXT, VLC_FALSE );
192 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
193 SCODEC_LONGTEXT, VLC_FALSE );
195 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
196 THREADS_LONGTEXT, VLC_TRUE );
198 add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
199 ASYNC_LONGTEXT, VLC_FALSE );
202 static const char *ppsz_sout_options[] = {
203 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
204 "scale", "fps", "width", "height", "deinterlace", "threads",
205 "aenc", "acodec", "ab", "samplerate", "channels",
206 "senc", "scodec", "soverlay",
210 /*****************************************************************************
211 * Exported prototypes
212 *****************************************************************************/
213 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
214 static int Del ( sout_stream_t *, sout_stream_id_t * );
215 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
217 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
218 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
219 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
221 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
222 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
223 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
225 static int transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
227 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
228 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
229 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
230 block_t *, block_t ** );
231 static subpicture_t *transcode_spu_get( sout_stream_t *, sout_stream_id_t *,
234 static int EncoderThread( struct sout_stream_sys_t * p_sys );
236 static int pi_channels_maps[6] =
239 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
240 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
241 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
242 | AOUT_CHAN_REARRIGHT,
243 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
244 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
247 #define PICTURE_RING_SIZE 64
248 #define SUBPICTURE_RING_SIZE 20
250 struct sout_stream_sys_t
254 sout_stream_t * p_out;
255 sout_stream_id_t * id_video;
257 vlc_mutex_t lock_out;
259 picture_t * pp_pics[PICTURE_RING_SIZE];
260 int i_first_pic, i_last_pic;
263 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
265 sout_cfg_t *p_audio_cfg;
271 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
273 sout_cfg_t *p_video_cfg;
279 vlc_bool_t b_deinterlace;
289 vlc_bool_t b_input_has_b_frames;
291 mtime_t i_output_pts;
294 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
296 vlc_bool_t b_soverlay;
297 sout_cfg_t *p_spu_cfg;
298 subpicture_t *pp_subpics[SUBPICTURE_RING_SIZE];
301 filter_t *p_filter_blend;
304 vlc_bool_t b_audio_sync;
305 mtime_t i_master_drift;
308 /*****************************************************************************
310 *****************************************************************************/
311 static int Open( vlc_object_t *p_this )
313 sout_stream_t *p_stream = (sout_stream_t*)p_this;
314 sout_stream_sys_t *p_sys;
318 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
320 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
323 msg_Err( p_stream, "cannot create chain" );
328 p_sys->b_input_has_b_frames = VLC_FALSE;
329 p_sys->i_output_pts = 0;
330 p_sys->i_master_drift = 0;
332 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
335 /* Audio transcoding parameters */
336 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
337 p_sys->psz_aenc = NULL;
338 p_sys->p_audio_cfg = NULL;
339 if( val.psz_string && *val.psz_string )
342 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
344 if( psz_next ) free( psz_next );
346 if( val.psz_string ) free( val.psz_string );
348 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
350 if( val.psz_string && *val.psz_string )
353 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
354 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
356 if( val.psz_string ) free( val.psz_string );
358 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
359 p_sys->i_abitrate = val.i_int;
360 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
362 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
363 p_sys->i_sample_rate = val.i_int;
365 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
366 p_sys->i_channels = val.i_int;
368 if( p_sys->i_acodec )
370 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
371 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
372 p_sys->i_channels, p_sys->i_abitrate / 1000 );
375 /* Video transcoding parameters */
376 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
377 p_sys->psz_venc = NULL;
378 p_sys->p_video_cfg = NULL;
379 if( val.psz_string && *val.psz_string )
382 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
384 if( psz_next ) free( psz_next );
386 if( val.psz_string ) free( val.psz_string );
388 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
390 if( val.psz_string && *val.psz_string )
393 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
394 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
396 if( val.psz_string ) free( val.psz_string );
398 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
399 p_sys->i_vbitrate = val.i_int;
400 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
402 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
403 p_sys->f_scale = val.f_float;
405 var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
406 p_sys->f_fps = val.f_float;
408 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
409 p_sys->i_width = val.i_int;
411 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
412 p_sys->i_height = val.i_int;
414 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
415 p_sys->b_deinterlace = val.b_bool;
417 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
418 p_sys->i_crop_top = val.i_int;
419 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
420 p_sys->i_crop_bottom = val.i_int;
421 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
422 p_sys->i_crop_left = val.i_int;
423 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
424 p_sys->i_crop_right = val.i_int;
426 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
427 p_sys->i_threads = val.i_int;
429 if( p_sys->i_vcodec )
431 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
432 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
433 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
436 /* Subpictures transcoding parameters */
437 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
438 p_sys->psz_senc = NULL;
439 p_sys->p_spu_cfg = NULL;
440 if( val.psz_string && *val.psz_string )
443 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
445 if( psz_next ) free( psz_next );
447 if( val.psz_string ) free( val.psz_string );
449 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
451 if( val.psz_string && *val.psz_string )
454 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
455 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
457 if( val.psz_string ) free( val.psz_string );
459 if( p_sys->i_scodec )
461 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_acodec );
464 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
465 p_sys->b_soverlay = val.b_bool;
466 p_sys->p_filter_blend = 0;
468 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
470 p_sys->pp_subpics[i] = 0;
473 var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
474 p_sys->b_audio_sync = val.b_bool;
475 if( p_sys->f_fps > 0 ) p_sys->b_audio_sync = VLC_TRUE;
477 p_stream->pf_add = Add;
478 p_stream->pf_del = Del;
479 p_stream->pf_send = Send;
480 p_stream->p_sys = p_sys;
483 avcodec_register_all();
488 /*****************************************************************************
490 *****************************************************************************/
491 static void Close( vlc_object_t * p_this )
493 sout_stream_t *p_stream = (sout_stream_t*)p_this;
494 sout_stream_sys_t *p_sys = p_stream->p_sys;
496 sout_StreamDelete( p_sys->p_out );
498 while( p_sys->p_audio_cfg != NULL )
500 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
502 if( p_sys->p_audio_cfg->psz_name )
503 free( p_sys->p_audio_cfg->psz_name );
504 if( p_sys->p_audio_cfg->psz_value )
505 free( p_sys->p_audio_cfg->psz_value );
506 free( p_sys->p_audio_cfg );
508 p_sys->p_audio_cfg = p_next;
510 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
512 while( p_sys->p_video_cfg != NULL )
514 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
516 if( p_sys->p_video_cfg->psz_name )
517 free( p_sys->p_video_cfg->psz_name );
518 if( p_sys->p_video_cfg->psz_value )
519 free( p_sys->p_video_cfg->psz_value );
520 free( p_sys->p_video_cfg );
522 p_sys->p_video_cfg = p_next;
524 if( p_sys->psz_venc ) free( p_sys->psz_venc );
526 while( p_sys->p_spu_cfg != NULL )
528 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
530 if( p_sys->p_spu_cfg->psz_name )
531 free( p_sys->p_spu_cfg->psz_name );
532 if( p_sys->p_spu_cfg->psz_value )
533 free( p_sys->p_spu_cfg->psz_value );
534 free( p_sys->p_spu_cfg );
536 p_sys->p_spu_cfg = p_next;
538 if( p_sys->psz_senc ) free( p_sys->psz_senc );
540 if( p_sys->p_filter_blend )
542 if( p_sys->p_filter_blend->p_module )
543 module_Unneed( p_sys->p_filter_blend,
544 p_sys->p_filter_blend->p_module );
546 vlc_object_detach( p_sys->p_filter_blend );
547 vlc_object_destroy( p_sys->p_filter_blend );
550 vlc_object_destroy( p_sys );
553 struct sout_stream_id_t
555 vlc_fourcc_t b_transcode;
556 es_format_t f_src; /* only if transcoding */
557 es_format_t f_dst; /* " " " */
558 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
560 /* id of the out stream */
564 decoder_t *p_decoder;
567 encoder_t *p_encoder;
568 vlc_fourcc_t b_enc_inited;
572 AVCodecContext *ff_dec_c;
582 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
583 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
584 AVFrame *p_ff_pic_tmp2; /* to do resample */
585 AVFrame *p_ff_pic_tmp3; /* to do subpicture overlay */
587 ImgReSampleContext *p_vresample;
590 date_t interpolated_pts;
591 mtime_t i_initial_pts;
595 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
597 sout_stream_sys_t *p_sys = p_stream->p_sys;
598 sout_stream_id_t *id;
600 id = malloc( sizeof( sout_stream_id_t ) );
601 memset( id, 0, sizeof(sout_stream_id_t) );
604 id->p_decoder = NULL;
605 id->p_encoder = NULL;
607 /* Get source format */
610 /* Create destination format */
611 es_format_Init( &id->f_dst, p_fmt->i_cat, 0 );
612 id->f_dst.i_id = id->f_src.i_id;
613 id->f_dst.i_group = id->f_src.i_group;
614 if( id->f_src.psz_language )
615 id->f_dst.psz_language = strdup( id->f_src.psz_language );
617 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
620 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
621 (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
623 /* Complete destination format */
624 id->f_dst.i_codec = p_sys->i_acodec;
625 id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ?
626 p_sys->i_sample_rate : id->f_src.audio.i_rate;
627 id->f_dst.audio.i_channels = p_sys->i_channels > 0 ?
628 p_sys->i_channels : id->f_src.audio.i_channels;
629 id->f_dst.i_bitrate = p_sys->i_abitrate;
630 id->f_dst.audio.i_bitspersample = id->f_src.audio.i_bitspersample;
632 /* build decoder -> filter -> encoder */
633 if( transcode_audio_ffmpeg_new( p_stream, id ) )
635 msg_Err( p_stream, "cannot create audio chain" );
640 /* open output stream */
641 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
642 id->b_transcode = VLC_TRUE;
650 date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
652 else if( p_fmt->i_cat == VIDEO_ES &&
653 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
656 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
657 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
659 /* Complete destination format */
660 id->f_dst.i_codec = p_sys->i_vcodec;
661 id->f_dst.video.i_width = p_sys->i_width;
662 id->f_dst.video.i_height = p_sys->i_height;
663 id->f_dst.i_bitrate = p_sys->i_vbitrate;
665 /* build decoder -> filter -> encoder */
666 if( transcode_video_ffmpeg_new( p_stream, id ) )
668 msg_Err( p_stream, "cannot create video chain" );
673 /* open output stream */
674 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
676 id->b_transcode = VLC_TRUE;
678 if( id->f_dst.video.i_frame_rate && id->f_dst.video.i_frame_rate_base )
680 date_Init( &id->interpolated_pts, id->f_dst.video.i_frame_rate,
681 id->f_dst.video.i_frame_rate_base );
683 else if( p_sys->b_audio_sync )
685 msg_Warn( p_stream, "no video frame rate available, disabling "
687 p_sys->b_audio_sync = VLC_FALSE;
690 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
692 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
693 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
694 (char*)&p_sys->i_scodec );
696 /* Complete destination format */
697 id->f_dst.i_codec = p_sys->i_scodec;
699 /* build decoder -> filter -> encoder */
700 if( transcode_spu_new( p_stream, id ) )
702 msg_Err( p_stream, "cannot create subtitles chain" );
707 /* open output stream */
708 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
709 id->b_transcode = VLC_TRUE;
717 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
719 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
720 (char*)&p_fmt->i_codec );
722 id->b_transcode = VLC_TRUE;
724 /* build decoder -> filter -> encoder */
725 if( transcode_spu_new( p_stream, id ) )
727 msg_Err( p_stream, "cannot create subtitles chain" );
734 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
735 (char*)&p_fmt->i_codec );
736 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
737 id->b_transcode = VLC_FALSE;
749 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
751 sout_stream_sys_t *p_sys = p_stream->p_sys;
753 if( id->b_transcode )
755 if( id->f_src.i_cat == AUDIO_ES )
757 transcode_audio_ffmpeg_close( p_stream, id );
759 else if( id->f_src.i_cat == VIDEO_ES )
761 transcode_video_ffmpeg_close( p_stream, id );
763 else if( id->f_src.i_cat == SPU_ES )
765 transcode_spu_close( p_stream, id );
769 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
775 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
778 sout_stream_sys_t *p_sys = p_stream->p_sys;
780 if( id->b_transcode )
782 block_t *p_buffer_out;
784 /* Be sure to have padding */
785 p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
786 if( p_buffer == NULL )
790 p_buffer->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
791 memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
793 if( id->f_src.i_cat == AUDIO_ES )
795 transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
797 block_Release( p_buffer );
799 else if( id->f_src.i_cat == VIDEO_ES )
801 if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
802 &p_buffer_out ) != VLC_SUCCESS )
804 block_Release( p_buffer );
807 block_Release( p_buffer );
809 else if( id->f_src.i_cat == SPU_ES )
811 if( transcode_spu_process( p_stream, id, p_buffer,
812 &p_buffer_out ) != VLC_SUCCESS )
819 block_Release( p_buffer );
824 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
828 else if( id->id != NULL )
830 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
834 block_Release( p_buffer );
839 /****************************************************************************
840 * ffmpeg decoder reencoder part
841 ****************************************************************************/
846 } fourcc_to_ff_code[] =
849 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
850 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
851 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
852 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
853 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
854 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
855 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
856 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
857 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
860 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
861 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
862 #if LIBAVCODEC_BUILD >= 4676
863 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
865 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
866 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
867 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
868 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
869 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
870 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
871 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
872 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
873 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
874 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
875 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
876 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
877 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
878 #if LIBAVCODEC_BUILD >= 4666
879 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
880 { VLC_FOURCC( 'h', '2', '6', '4' ), CODEC_ID_H264 },
882 #if LIBAVCODEC_BUILD >= 4719
883 { VLC_FOURCC( 'S', 'N', 'O', 'W' ), CODEC_ID_SNOW },
886 /* raw video code, only used for 'encoding' */
887 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
888 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
889 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
890 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
891 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
892 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
893 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
894 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
895 { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
896 { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
898 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
901 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
905 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
907 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
909 return fourcc_to_ff_code[i].i_ff_codec;
916 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
920 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
921 case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
922 case VLC_FOURCC( 'I', '4', '2', '0' ):
923 return PIX_FMT_YUV420P;
924 case VLC_FOURCC( 'I', '4', '2', '2' ):
925 return PIX_FMT_YUV422P;
926 case VLC_FOURCC( 'I', '4', '4', '4' ):
927 return PIX_FMT_YUV444P;
928 case VLC_FOURCC( 'R', 'V', '1', '5' ):
929 return PIX_FMT_RGB555;
930 case VLC_FOURCC( 'R', 'V', '1', '6' ):
931 return PIX_FMT_RGB565;
932 case VLC_FOURCC( 'R', 'V', '2', '4' ):
933 return PIX_FMT_BGR24;
934 case VLC_FOURCC( 'R', 'V', '3', '2' ):
935 return PIX_FMT_RGBA32;
936 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
937 return PIX_FMT_GRAY8;
938 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
939 return PIX_FMT_YUV422;
945 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
949 case PIX_FMT_YUV420P:
950 return VLC_FOURCC('I','4','2','0');
951 case PIX_FMT_YUV422P:
952 return VLC_FOURCC('I','4','2','2');
953 case PIX_FMT_YUV444P:
954 return VLC_FOURCC('I','4','4','4');
957 return VLC_FOURCC('Y','U','Y','2');
960 return VLC_FOURCC('R','V','1','5');
962 return VLC_FOURCC('R','V','1','6');
964 return VLC_FOURCC('R','V','2','4');
966 return VLC_FOURCC('R','V','3','2');
968 return VLC_FOURCC('G','R','E','Y');
970 case PIX_FMT_YUV410P:
971 case PIX_FMT_YUV411P:
978 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
979 sout_stream_id_t *id )
983 if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
984 id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
985 id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
986 id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
990 id->ff_dec_c = avcodec_alloc_context();
991 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
992 id->ff_dec_c->channels = id->f_src.audio.i_channels;
993 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
994 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
999 i_ff_codec = get_ff_codec( id->f_src.i_codec );
1000 if( i_ff_codec == 0 )
1002 msg_Err( p_stream, "cannot find decoder id" );
1003 return VLC_EGENERIC;
1006 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1009 msg_Err( p_stream, "cannot find decoder (avcodec)" );
1010 return VLC_EGENERIC;
1013 id->ff_dec_c = avcodec_alloc_context();
1014 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
1015 id->ff_dec_c->channels = id->f_src.audio.i_channels;
1016 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
1017 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
1019 id->ff_dec_c->extradata_size = id->f_src.i_extra;
1020 id->ff_dec_c->extradata = id->f_src.p_extra;
1021 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
1023 msg_Err( p_stream, "cannot open decoder" );
1024 av_free( id->ff_dec_c );
1025 return VLC_EGENERIC;
1029 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1030 id->i_buffer_pos = 0;
1031 id->p_buffer = malloc( id->i_buffer );
1033 /* Sanity check for audio channels */
1034 id->f_dst.audio.i_channels =
1035 __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
1038 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1040 /* Initialization of encoder format structures */
1041 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1042 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1043 id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
1044 id->p_encoder->fmt_in.audio.i_physical_channels =
1045 id->p_encoder->fmt_in.audio.i_original_channels =
1046 pi_channels_maps[id->f_dst.audio.i_channels];
1047 id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
1048 id->p_encoder->fmt_in.audio.i_bitspersample = 16;
1050 id->p_encoder->fmt_out = id->f_dst;
1052 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1054 /* Attach object to parent so object variables inheritance works */
1055 vlc_object_attach( id->p_encoder, p_stream );
1057 id->p_encoder->p_module =
1058 module_Need( id->p_encoder, "encoder",
1059 p_stream->p_sys->psz_aenc, VLC_TRUE );
1060 if( !id->p_encoder->p_module )
1062 vlc_object_detach( id->p_encoder );
1063 vlc_object_destroy( id->p_encoder );
1064 msg_Err( p_stream, "cannot open encoder" );
1065 av_free( id->ff_dec_c );
1066 return VLC_EGENERIC;
1069 id->b_enc_inited = VLC_FALSE;
1071 id->f_dst = id->p_encoder->fmt_out;
1073 /* Hack for mp3 transcoding support */
1074 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1076 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
1082 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
1083 sout_stream_id_t *id )
1085 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1086 av_free( id->ff_dec_c );
1088 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1090 vlc_object_detach( id->p_encoder );
1091 vlc_object_destroy( id->p_encoder );
1093 free( id->p_buffer );
1096 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
1097 sout_stream_id_t *id,
1101 sout_stream_sys_t *p_sys = p_stream->p_sys;
1102 aout_buffer_t aout_buf;
1104 int i_buffer = in->i_buffer;
1105 char *p_buffer = in->p_buffer;
1106 id->i_dts = in->i_dts;
1111 id->i_buffer_pos = 0;
1113 /* decode as much data as possible */
1118 i_used = avcodec_decode_audio( id->ff_dec_c,
1119 (int16_t*)id->p_buffer, &id->i_buffer_pos,
1120 p_buffer, i_buffer );
1123 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
1128 msg_Warn( p_stream, "error audio decoding");
1135 if ( id->i_buffer_pos < 0 )
1137 msg_Warn( p_stream, "weird error audio decoding");
1143 int16_t *sout = (int16_t*)id->p_buffer;
1145 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
1146 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
1148 int8_t *sin = (int8_t*)p_buffer;
1149 int i_used = __MIN( id->i_buffer/2, i_buffer );
1150 int i_samples = i_used;
1152 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
1153 while( i_samples > 0 )
1155 *sout++ = ( *sin++ ) << 8;
1159 while( i_samples > 0 )
1161 *sout++ = ( *sin++ - 128 ) << 8;
1167 id->i_buffer_pos = i_used * 2;
1169 else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
1170 id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
1172 int16_t *sin = (int16_t*)p_buffer;
1173 int i_used = __MIN( id->i_buffer, i_buffer );
1174 int i_samples = i_used / 2;
1177 memcpy( sout, sin, i_used );
1179 #ifdef WORDS_BIGENDIAN
1180 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
1182 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
1185 uint8_t *dat = (uint8_t*)sout;
1187 while( i_samples > 0 )
1202 id->i_buffer_pos = i_used;
1206 if( id->i_buffer_pos == 0 ) continue;
1208 /* Encode as much data as possible */
1209 if( !id->b_enc_inited && id->p_encoder->pf_header )
1211 block_t *p_block_tmp;
1213 p_block = id->p_encoder->pf_header( id->p_encoder );
1214 p_block_tmp = p_block;
1215 while( p_block_tmp )
1217 p_block_tmp->i_dts = p_block_tmp->i_pts = in->i_dts;
1218 p_block_tmp = p_block_tmp->p_next;
1220 block_ChainAppend( out, p_block );
1222 id->b_enc_inited = VLC_TRUE;
1225 aout_buf.p_buffer = id->p_buffer;
1226 aout_buf.i_nb_bytes = id->i_buffer_pos;
1227 aout_buf.i_nb_samples = id->i_buffer_pos/2/id->f_src.audio.i_channels;
1228 aout_buf.start_date = id->i_dts;
1229 aout_buf.end_date = id->i_dts;
1231 if( p_sys->b_audio_sync )
1233 aout_buf.start_date = date_Get( &id->interpolated_pts ) + 1;
1234 p_sys->i_master_drift = id->i_dts - aout_buf.start_date;
1235 date_Increment( &id->interpolated_pts, aout_buf.i_nb_samples );
1238 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
1239 id->f_src.audio.i_channels / id->f_src.audio.i_rate );
1241 if( id->p_encoder->fmt_in.audio.i_channels == 1 &&
1242 id->f_src.audio.i_channels > 1 )
1244 int16_t *p_sample = (int16_t *)aout_buf.p_buffer;
1245 int i_src_c = id->f_src.audio.i_channels;
1248 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1252 for( j = 1; j < i_src_c; j++ )
1254 c += p_sample[i_src_c * i + j];
1256 p_sample[i] = c / (i_src_c-1);
1258 aout_buf.i_nb_bytes = i * 2;
1260 else if( id->p_encoder->fmt_in.audio.i_channels == 2 &&
1261 id->f_src.audio.i_channels > 2 )
1263 int i_src_c = id->f_src.audio.i_channels;
1266 static const float mixf_l[4][6] = /* [i_src_c - 3][channel index] */
1268 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1269 { 0.00, 0.50, 0.50, 0.00, 0.00, 0.00 }, /* 4 channels */
1270 { 0.00, 0.50, 0.00, 0.50, 0.00, 0.00 }, /* 5 channels */
1271 { 0.00, 0.34, 0.33, 0.00, 0.33, 0.00 }, /* 6 channels */
1273 static const float mixf_r[4][6] = /* [i_src_c - 3][channel index] */
1275 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1276 { 0.00, 0.00, 0.50, 0.50, 0.00, 0.00 }, /* 4 channels */
1277 { 0.00, 0.00, 0.50, 0.00, 0.50, 0.00 }, /* 5 channels */
1278 { 0.00, 0.00, 0.33, 0.34, 0.00, 0.33 }, /* 6 channels */
1282 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1284 int16_t *p_src = (int16_t *)aout_buf.p_buffer + i_src_c * i;
1285 int16_t *p_dst = (int16_t *)aout_buf.p_buffer + 2 * i;
1288 float l = 0.0, r = 0.0;
1289 for( j = 0; j < i_src_c; j++ )
1291 l += mixf_l[i_src_c-3][j] * p_src[j];
1292 r += mixf_r[i_src_c-3][j] * p_src[j];
1295 p_dst[0] = (int)( l + 0.5 );
1296 p_dst[1] = (int)( r + 0.5 );
1298 aout_buf.i_nb_bytes = i * 2 * 2;
1300 else if( id->f_src.audio.i_channels !=
1301 id->p_encoder->fmt_in.audio.i_channels )
1306 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
1307 static const int translation[7][6] =
1308 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
1309 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
1310 { 0, 1, 0, 0, 0, 0 }, /* 2 */
1311 { 1, 2, 0, 0, 0, 0 }, /* 3 */
1312 { 1, 3, 2, 0, 0, 0 }, /* 4 */
1313 { 1, 3, 4, 2, 0, 0 }, /* 5 */
1314 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
1316 /* dumb downmixing */
1317 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1319 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
1320 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
1322 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
1323 p_buffer[i*id->f_src.audio.i_channels+
1324 translation[id->f_src.audio.i_channels][j]];
1327 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1330 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1331 block_ChainAppend( out, p_block );
1341 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1342 sout_stream_id_t *id )
1344 sout_stream_sys_t *p_sys = p_stream->p_sys;
1348 if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1349 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1350 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1351 id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1352 id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1353 id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1354 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1355 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1356 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1357 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1358 id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1361 id->ff_dec_c = avcodec_alloc_context();
1362 id->ff_dec_c->width = id->f_src.video.i_width;
1363 id->ff_dec_c->height = id->f_src.video.i_height;
1364 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_codec );
1366 #if LIBAVCODEC_BUILD >= 4687
1367 if( id->ff_dec_c->width )
1368 id->ff_dec_c->sample_aspect_ratio =
1369 av_d2q( id->f_src.video.i_aspect / (double)VOUT_ASPECT_FACTOR *
1370 id->ff_dec_c->height / id->ff_dec_c->width, 255 );
1372 id->ff_dec_c->aspect_ratio =
1373 id->f_src.video.i_aspect / (float)VOUT_ASPECT_FACTOR;
1379 i_ff_codec = get_ff_codec( id->f_src.i_codec );
1380 if( i_ff_codec == 0 )
1382 msg_Err( p_stream, "cannot find decoder" );
1383 return VLC_EGENERIC;
1386 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1389 msg_Err( p_stream, "cannot find decoder" );
1390 return VLC_EGENERIC;
1393 id->ff_dec_c = avcodec_alloc_context();
1394 id->ff_dec_c->width = id->f_src.video.i_width;
1395 id->ff_dec_c->height = id->f_src.video.i_height;
1396 id->ff_dec_c->bits_per_sample=id->f_src.video.i_bits_per_pixel;
1397 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1399 if( id->f_src.i_extra > 0 )
1401 if( i_ff_codec == CODEC_ID_SVQ3 )
1403 int i_size = id->f_src.i_extra;
1406 id->ff_dec_c->extradata_size = i_size + 12;
1407 p = id->ff_dec_c->extradata = malloc( i_size + 12 );
1409 memcpy( &p[0], "SVQ3", 4 );
1410 memset( &p[4], 0, 8 );
1411 memcpy( &p[12], id->f_src.p_extra, i_size );
1413 /* Now remove all atoms before the SMI one */
1414 if( id->ff_dec_c->extradata_size > 0x5a && strncmp( &p[0x56], "SMI ", 4 ) )
1416 uint8_t *psz = &p[0x52];
1418 while( psz < &p[id->ff_dec_c->extradata_size - 8] )
1420 int i_size = GetDWBE( psz );
1423 /* FIXME handle 1 as long size */
1426 if( !strncmp( &psz[4], "SMI ", 4 ) )
1428 memmove( &p[0x52], psz, &p[id->ff_dec_c->extradata_size] - psz );
1437 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1438 id->ff_dec_c->extradata = malloc( id->f_src.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
1440 memcpy( id->ff_dec_c->extradata, id->f_src.p_extra, id->f_src.i_extra );
1441 memset( (uint8_t*)id->ff_dec_c->extradata + id->f_src.i_extra, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1444 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1445 id->ff_dec_c->error_resilience= -1;
1446 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1447 id->ff_dec_c->opaque = p_sys;
1449 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1451 msg_Err( p_stream, "cannot open decoder" );
1452 av_free( id->ff_dec_c );
1453 return VLC_EGENERIC;
1458 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1460 /* Initialization of encoder format structures */
1461 es_format_Init( &id->p_encoder->fmt_in,
1462 id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1464 /* The dimensions will be set properly later on.
1465 * Just put sensible values so we can test if there is an encoder. */
1466 id->p_encoder->fmt_in.video.i_width =
1467 id->f_src.video.i_width ? id->f_src.video.i_width : 16;
1468 id->p_encoder->fmt_in.video.i_height =
1469 id->f_src.video.i_height ? id->f_src.video.i_height : 16;
1471 id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1472 id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1475 id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1476 #if LIBAVCODEC_BUILD >= 4662
1477 id->p_encoder->fmt_in.video.i_frame_rate_base =
1478 id->ff_dec_c->frame_rate_base;
1481 #if LIBAVCODEC_BUILD >= 4687
1482 if( id->ff_dec_c->height )
1483 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1484 ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1485 id->ff_dec_c->width / id->ff_dec_c->height );
1487 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1488 id->ff_dec_c->aspect_ratio;
1492 /* Override with user settings */
1493 if( p_sys->f_fps > 0 )
1495 id->p_encoder->fmt_in.video.i_frame_rate = p_sys->f_fps * 1000;
1496 id->p_encoder->fmt_in.video.i_frame_rate_base = 1000;
1499 id->f_dst.video.i_frame_rate = id->p_encoder->fmt_in.video.i_frame_rate;
1500 id->f_dst.video.i_frame_rate_base =
1501 id->p_encoder->fmt_in.video.i_frame_rate_base;
1503 /* Check whether a particular aspect ratio was requested */
1504 if( id->f_src.video.i_aspect )
1506 id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1507 id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1510 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1511 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1512 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1514 id->p_encoder->i_threads = p_sys->i_threads;
1516 id->p_ff_pic = avcodec_alloc_frame();
1517 id->p_ff_pic_tmp0 = NULL;
1518 id->p_ff_pic_tmp1 = NULL;
1519 id->p_ff_pic_tmp2 = NULL;
1520 id->p_ff_pic_tmp3 = NULL;
1521 id->p_vresample = NULL;
1523 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1525 /* Attach object to parent so object variables inheritance works */
1526 vlc_object_attach( id->p_encoder, p_stream );
1528 id->p_encoder->p_module =
1529 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1531 if( !id->p_encoder->p_module )
1533 vlc_object_detach( id->p_encoder );
1534 vlc_object_destroy( id->p_encoder );
1535 av_free( id->ff_dec_c );
1536 msg_Err( p_stream, "cannot find encoder" );
1537 return VLC_EGENERIC;
1540 /* Close the encoder.
1541 * We'll open it only when we have the first frame */
1542 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1543 id->p_encoder->p_module = NULL;
1545 id->b_enc_inited = VLC_FALSE;
1547 if ( p_sys->i_threads >= 1 )
1549 p_sys->id_video = id;
1550 vlc_mutex_init( p_stream, &p_sys->lock_out );
1551 vlc_cond_init( p_stream, &p_sys->cond );
1552 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1553 p_sys->i_first_pic = 0;
1554 p_sys->i_last_pic = 0;
1555 p_sys->p_buffers = NULL;
1556 p_sys->b_die = p_sys->b_error = 0;
1557 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1558 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1560 vlc_object_detach( id->p_encoder );
1561 vlc_object_destroy( id->p_encoder );
1562 av_free( id->ff_dec_c );
1563 msg_Err( p_stream, "cannot spawn encoder thread" );
1564 return VLC_EGENERIC;
1571 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1572 sout_stream_id_t *id )
1574 if ( p_stream->p_sys->i_threads >= 1 )
1576 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1577 p_stream->p_sys->b_die = 1;
1578 vlc_cond_signal( &p_stream->p_sys->cond );
1579 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1580 vlc_thread_join( p_stream->p_sys );
1581 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1582 vlc_cond_destroy( &p_stream->p_sys->cond );
1586 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1587 av_free( id->ff_dec_c );
1590 if( id->p_encoder->p_module )
1591 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1592 vlc_object_detach( id->p_encoder );
1593 vlc_object_destroy( id->p_encoder );
1598 free( id->p_ff_pic );
1601 if( id->p_ff_pic_tmp0 )
1603 free( id->p_ff_pic_tmp0->data[0] );
1604 free( id->p_ff_pic_tmp0 );
1606 if( id->p_ff_pic_tmp1 )
1608 free( id->p_ff_pic_tmp1->data[0] );
1609 free( id->p_ff_pic_tmp1 );
1611 if( id->p_ff_pic_tmp2 )
1613 free( id->p_ff_pic_tmp2->data[0] );
1614 free( id->p_ff_pic_tmp2 );
1616 if( id->p_ff_pic_tmp3 )
1618 free( id->p_ff_pic_tmp3->data[0] );
1619 free( id->p_ff_pic_tmp3 );
1621 if( id->p_vresample )
1623 img_resample_close( id->p_vresample );
1627 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1628 sout_stream_id_t *id, block_t *in, block_t **out )
1630 sout_stream_sys_t *p_sys = p_stream->p_sys;
1631 int i_used, b_gotpicture, i_duplicate = 1;
1640 i_data = in->i_buffer;
1641 p_data = in->p_buffer;
1648 subpicture_t *p_subpic = 0;
1651 frame = id->p_ff_pic;
1652 p_sys->i_input_pts = in->i_pts;
1653 p_sys->i_input_dts = in->i_dts;
1656 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1663 avpicture_fill( (AVPicture*)frame, p_data,
1664 id->ff_dec_c->pix_fmt,
1665 id->ff_dec_c->width, id->ff_dec_c->height );
1670 frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts :
1673 frame->pict_type = FF_I_TYPE;
1678 msg_Warn( p_stream, "error");
1679 return VLC_EGENERIC;
1689 /* Get the pts of the decoded frame if any, otherwise keep the
1690 * interpolated one */
1691 if( frame->pts != AV_NOPTS_VALUE )
1693 p_sys->i_output_pts = frame->pts;
1695 i_pts = p_sys->i_output_pts;
1697 /* Sanity check (seems to be needed for some streams ) */
1698 if( frame->pict_type == FF_B_TYPE )
1700 p_sys->b_input_has_b_frames = VLC_TRUE;
1703 if( p_sys->b_audio_sync )
1705 mtime_t i_video_drift;
1706 mtime_t i_master_drift = p_sys->i_master_drift;
1708 if( !id->i_initial_pts ) id->i_initial_pts = p_sys->i_output_pts;
1710 if( !i_master_drift )
1712 /* No audio track ? */
1713 i_master_drift = id->i_initial_pts;
1716 i_pts = date_Get( &id->interpolated_pts ) + 1;
1717 i_video_drift = p_sys->i_output_pts - i_pts;
1720 if( i_video_drift < i_master_drift - 50000 )
1722 msg_Dbg( p_stream, "dropping frame (%i)",
1723 (int)(i_video_drift - i_master_drift) );
1724 return VLC_EGENERIC;
1726 else if( i_video_drift > i_master_drift + 50000 )
1728 msg_Dbg( p_stream, "adding frame (%i)",
1729 (int)(i_video_drift - i_master_drift) );
1733 date_Increment( &id->interpolated_pts, 1 );
1736 if( !id->b_enc_inited )
1738 /* Hack because of the copy packetizer which can fail to detect the
1739 * proper size (which forces us to wait until the 1st frame
1741 int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1742 p_sys->i_crop_right;
1743 int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1744 p_sys->i_crop_bottom;
1746 if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1749 /* Apply the scaling */
1750 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1751 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1753 else if( id->f_dst.video.i_width > 0 &&
1754 id->f_dst.video.i_height <= 0 )
1756 id->f_dst.video.i_height =
1757 id->f_dst.video.i_width / (double)i_width * i_height;
1759 else if( id->f_dst.video.i_width <= 0 &&
1760 id->f_dst.video.i_height > 0 )
1762 id->f_dst.video.i_width =
1763 id->f_dst.video.i_height / (double)i_height * i_width;
1766 id->p_encoder->fmt_in.video.i_width = id->f_dst.video.i_width;
1767 id->p_encoder->fmt_in.video.i_height = id->f_dst.video.i_height;
1768 id->p_encoder->fmt_out = id->f_dst;
1770 id->p_encoder->p_module =
1771 module_Need( id->p_encoder, "encoder",
1772 p_sys->psz_venc, VLC_TRUE );
1773 if( !id->p_encoder->p_module )
1775 vlc_object_destroy( id->p_encoder );
1776 msg_Err( p_stream, "cannot find encoder" );
1777 id->b_transcode = VLC_FALSE;
1778 return VLC_EGENERIC;
1781 id->f_dst = id->p_encoder->fmt_out;
1783 /* Hack for mp2v/mp1v transcoding support */
1784 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1785 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1787 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1791 p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1794 msg_Err( p_stream, "cannot add this stream" );
1795 transcode_video_ffmpeg_close( p_stream, id );
1796 id->b_transcode = VLC_FALSE;
1797 return VLC_EGENERIC;
1800 if( id->p_encoder->pf_header )
1802 block_t *p_block_tmp;
1804 p_block = id->p_encoder->pf_header( id->p_encoder );
1805 p_block_tmp = p_block;
1806 while( p_block_tmp )
1808 p_block_tmp->i_dts = p_block_tmp->i_pts = in->i_dts;
1809 p_block_tmp = p_block_tmp->p_next;
1811 block_ChainAppend( out, p_block );
1814 id->i_inter_pixfmt =
1815 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1817 id->b_enc_inited = VLC_TRUE;
1821 if( p_stream->p_sys->b_deinterlace )
1823 if( id->p_ff_pic_tmp0 == NULL )
1827 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1828 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1829 id->ff_dec_c->width,
1830 id->ff_dec_c->height );
1832 buf = malloc( i_size );
1834 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1836 id->ff_dec_c->width, id->ff_dec_c->height );
1839 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0,
1840 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1841 id->ff_dec_c->width, id->ff_dec_c->height );
1843 #if LIBAVCODEC_BUILD >= 4685
1844 id->p_ff_pic_tmp0->interlaced_frame = 0;
1846 id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1847 frame = id->p_ff_pic_tmp0;
1850 /* convert pix format */
1851 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1853 if( id->p_ff_pic_tmp1 == NULL )
1857 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1858 i_size = avpicture_get_size( id->i_inter_pixfmt,
1859 id->ff_dec_c->width,
1860 id->ff_dec_c->height );
1862 buf = malloc( i_size );
1864 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1866 id->ff_dec_c->width, id->ff_dec_c->height );
1869 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1870 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1871 id->ff_dec_c->width, id->ff_dec_c->height );
1873 id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1874 #if LIBAVCODEC_BUILD >= 4685
1875 id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1876 id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1878 frame = id->p_ff_pic_tmp1;
1881 /* convert size and crop */
1882 if( id->ff_dec_c->width != id->f_dst.video.i_width ||
1883 id->ff_dec_c->height != id->f_dst.video.i_height ||
1884 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1885 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1887 if( id->p_ff_pic_tmp2 == NULL )
1891 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1892 i_size = avpicture_get_size( id->i_inter_pixfmt,
1893 id->f_dst.video.i_width,
1894 id->f_dst.video.i_height );
1896 buf = malloc( i_size );
1898 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1900 id->f_dst.video.i_width,
1901 id->f_dst.video.i_height );
1904 img_resample_full_init( id->f_dst.video.i_width,
1905 id->f_dst.video.i_height,
1906 id->ff_dec_c->width,
1907 id->ff_dec_c->height,
1908 p_stream->p_sys->i_crop_top,
1909 p_stream->p_sys->i_crop_bottom,
1910 p_stream->p_sys->i_crop_left,
1911 p_stream->p_sys->i_crop_right
1912 #if LIBAVCODEC_BUILD >= 4708
1919 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1920 (AVPicture*)frame );
1922 id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1923 #if LIBAVCODEC_BUILD >= 4685
1924 id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1925 id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1927 frame = id->p_ff_pic_tmp2;
1931 p_pic = malloc(sizeof(picture_t));
1932 vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1933 id->p_encoder->fmt_in.i_codec,
1934 id->f_dst.video.i_width, id->f_dst.video.i_height,
1935 id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1936 id->f_dst.video.i_height );
1938 /* Check if we have a subpicture to overlay */
1939 if( p_sys->p_filter_blend )
1941 p_subpic = transcode_spu_get( p_stream, id, p_sys->i_output_pts );
1943 if( p_subpic && frame != id->p_ff_pic_tmp0 &&
1944 frame != id->p_ff_pic_tmp1 && frame != id->p_ff_pic_tmp2 )
1946 if( id->p_ff_pic_tmp3 == NULL )
1948 uint8_t *buf = malloc( frame->linesize[0] *
1949 p_pic->p[0].i_lines * 3 );
1950 id->p_ff_pic_tmp3 = avcodec_alloc_frame();
1951 *id->p_ff_pic_tmp3 = *frame;
1952 id->p_ff_pic_tmp3->data[0] = buf;
1953 id->p_ff_pic_tmp3->data[1] = id->p_ff_pic_tmp3->data[0] +
1954 frame->linesize[0] * p_pic->p[0].i_lines;
1955 id->p_ff_pic_tmp3->data[2] = id->p_ff_pic_tmp3->data[1] +
1956 frame->linesize[1] * p_pic->p[1].i_lines;
1959 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1961 p_stream->p_vlc->pf_memcpy(
1962 id->p_ff_pic_tmp3->data[i_plane],
1963 frame->data[i_plane],
1964 p_pic->p[i_plane].i_lines * frame->linesize[i_plane] );
1967 id->p_ff_pic_tmp3->repeat_pict = frame->repeat_pict;
1968 #if LIBAVCODEC_BUILD >= 4685
1969 id->p_ff_pic_tmp3->interlaced_frame = frame->interlaced_frame;
1970 id->p_ff_pic_tmp3->top_field_first = frame->top_field_first;
1972 frame = id->p_ff_pic_tmp3;
1976 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1978 p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1979 if ( p_sys->i_threads >= 1 )
1981 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines *
1982 p_pic->p[i_plane].i_pitch);
1983 p_stream->p_vlc->pf_memcpy( p_pic->p[i_plane].p_pixels,
1984 frame->data[i_plane], p_pic->p[i_plane].i_lines *
1985 p_pic->p[i_plane].i_pitch );
1989 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1993 /* Set the pts of the frame being encoded */
1994 p_pic->date = i_pts;
1996 p_pic->i_nb_fields = frame->repeat_pict;
1997 #if LIBAVCODEC_BUILD >= 4685
1998 p_pic->b_progressive = !frame->interlaced_frame;
1999 p_pic->b_top_field_first = frame->top_field_first;
2002 /* Interpolate the next PTS
2003 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
2004 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
2006 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
2007 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
2010 /* Overlay subpicture */
2013 int i_width, i_height;
2015 p_sys->p_filter_blend->fmt_out = id->p_encoder->fmt_in;
2016 p_sys->p_filter_blend->fmt_out.video.i_visible_width =
2017 p_sys->p_filter_blend->fmt_out.video.i_width;
2018 p_sys->p_filter_blend->fmt_out.video.i_visible_height =
2019 p_sys->p_filter_blend->fmt_out.video.i_height;
2020 p_sys->p_filter_blend->fmt_out.video.i_chroma =
2021 VLC_FOURCC('I','4','2','0');
2023 i_width = id->p_encoder->fmt_in.video.i_width;
2024 i_height = id->p_encoder->fmt_in.video.i_height;
2026 while( p_subpic != NULL )
2028 subpicture_region_t *p_region = p_subpic->p_region;
2030 while( p_region && p_sys->p_filter_blend &&
2031 p_sys->p_filter_blend->pf_video_blend )
2033 int i_x_offset = p_region->i_x + p_subpic->i_x;
2034 int i_y_offset = p_region->i_y + p_subpic->i_y;
2036 if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
2038 i_y_offset = i_height - p_region->fmt.i_height -
2041 else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
2043 i_y_offset = i_height / 2 - p_region->fmt.i_height / 2;
2046 if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
2048 i_x_offset = i_width - p_region->fmt.i_width -
2051 else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
2053 i_x_offset = i_width / 2 - p_region->fmt.i_width / 2;
2056 if( p_subpic->b_absolute )
2058 i_x_offset = p_region->i_x + p_subpic->i_x;
2059 i_y_offset = p_region->i_y + p_subpic->i_y;
2062 p_sys->p_filter_blend->fmt_in.video = p_region->fmt;
2064 p_sys->p_filter_blend->pf_video_blend(
2065 p_sys->p_filter_blend, p_pic, p_pic,
2066 &p_region->picture, i_x_offset, i_y_offset );
2068 p_region = p_region->p_next;
2071 p_subpic = p_subpic->p_next;
2075 if ( p_sys->i_threads >= 1 )
2077 vlc_mutex_lock( &p_sys->lock_out );
2078 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
2079 p_sys->i_last_pic %= PICTURE_RING_SIZE;
2080 *out = p_sys->p_buffers;
2081 p_sys->p_buffers = NULL;
2082 vlc_cond_signal( &p_sys->cond );
2083 vlc_mutex_unlock( &p_sys->lock_out );
2088 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2089 block_ChainAppend( out, p_block );
2091 if( p_sys->b_audio_sync && i_duplicate > 1 )
2093 i_pts = date_Get( &id->interpolated_pts ) + 1;
2094 date_Increment( &id->interpolated_pts, 1 );
2095 p_pic->date = i_pts;
2096 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2097 block_ChainAppend( out, p_block );
2112 static int EncoderThread( sout_stream_sys_t * p_sys )
2114 sout_stream_id_t * id = p_sys->id_video;
2118 while ( !p_sys->b_die && !p_sys->b_error )
2122 vlc_mutex_lock( &p_sys->lock_out );
2123 while ( p_sys->i_last_pic == p_sys->i_first_pic )
2125 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
2126 if ( p_sys->b_die || p_sys->b_error )
2129 if ( p_sys->b_die || p_sys->b_error )
2131 vlc_mutex_unlock( &p_sys->lock_out );
2135 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2136 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2137 vlc_mutex_unlock( &p_sys->lock_out );
2139 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2140 vlc_mutex_lock( &p_sys->lock_out );
2141 block_ChainAppend( &p_sys->p_buffers, p_block );
2142 vlc_mutex_unlock( &p_sys->lock_out );
2144 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
2146 free( p_pic->p[i_plane].p_pixels );
2151 while ( p_sys->i_last_pic != p_sys->i_first_pic )
2153 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2154 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2156 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
2158 free( p_pic->p[i_plane].p_pixels );
2163 block_ChainRelease( p_sys->p_buffers );
2168 /*****************************************************************************
2169 * transcode_video_ffmpeg_getframebuf:
2171 * Callback used by ffmpeg to get a frame buffer.
2172 * We use it to get the right PTS for each decoded picture.
2173 *****************************************************************************/
2174 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
2177 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
2180 if( p_sys->i_input_pts )
2182 p_frame->pts = p_sys->i_input_pts;
2184 else if( p_sys->i_input_dts )
2186 /* Some demuxers/packetizers only set the dts so let's try to find a
2187 * useful timestamp from this */
2188 if( !p_context->has_b_frames || !p_sys->b_input_has_b_frames ||
2189 !p_frame->reference || !p_sys->i_output_pts )
2191 p_frame->pts = p_sys->i_input_dts +
2192 /* Hack: ffmpeg encoding doesn't like frames with identical pts */
2193 (p_sys->i_output_pts ? 0 : 50000);
2195 else p_frame->pts = AV_NOPTS_VALUE;
2197 else p_frame->pts = AV_NOPTS_VALUE;
2199 if( p_sys->i_output_pts ) /* make sure 1st frame has a pts > 0 */
2201 p_sys->i_input_pts = 0;
2202 p_sys->i_input_dts = 0;
2205 return avcodec_default_get_buffer( p_context, p_frame );
2211 static subpicture_t *spu_new_buffer( decoder_t * );
2212 static void spu_del_buffer( decoder_t *, subpicture_t * );
2214 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2216 sout_stream_sys_t *p_sys = p_stream->p_sys;
2219 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
2221 /* Initialization of decoder format structures */
2222 id->p_decoder->fmt_in = id->f_src;
2223 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2224 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2225 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
2227 /* Attach object to parent so object variables inheritance works */
2228 vlc_object_attach( id->p_decoder, p_stream );
2230 id->p_decoder->p_module =
2231 module_Need( id->p_decoder, "decoder", "$codec", VLC_TRUE );
2233 if( !id->p_decoder->p_module )
2235 vlc_object_detach( id->p_decoder );
2236 vlc_object_destroy( id->p_decoder );
2237 msg_Err( p_stream, "cannot find decoder" );
2238 return VLC_EGENERIC;
2241 if( !p_sys->b_soverlay )
2244 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
2246 /* Initialization of encoder format structures */
2247 es_format_Init( &id->p_encoder->fmt_in,
2248 id->f_src.i_cat, id->f_src.i_codec );
2250 id->p_encoder->fmt_out = id->f_dst;
2252 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2254 /* Attach object to parent so object variables inheritance works */
2255 vlc_object_attach( id->p_encoder, p_stream );
2257 id->p_encoder->p_module =
2258 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2260 if( !id->p_encoder->p_module )
2262 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2263 vlc_object_detach( id->p_decoder );
2264 vlc_object_destroy( id->p_decoder );
2266 vlc_object_detach( id->p_encoder );
2267 vlc_object_destroy( id->p_encoder );
2268 msg_Err( p_stream, "cannot find encoder" );
2269 return VLC_EGENERIC;
2272 id->f_dst = id->p_encoder->fmt_out;
2276 p_sys->p_filter_blend =
2277 vlc_object_create( p_stream, sizeof(filter_t) );
2278 vlc_object_attach( p_sys->p_filter_blend, p_stream );
2279 p_sys->p_filter_blend->fmt_out.video.i_chroma =
2280 VLC_FOURCC('I','4','2','0');
2281 p_sys->p_filter_blend->fmt_in.video.i_chroma =
2282 VLC_FOURCC('Y','U','V','A');
2283 p_sys->p_filter_blend->p_module =
2284 module_Need( p_sys->p_filter_blend, "video blending", 0, 0 );
2290 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2292 sout_stream_sys_t *p_sys = p_stream->p_sys;
2296 if( id->p_decoder->p_module )
2297 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2298 vlc_object_detach( id->p_decoder );
2299 vlc_object_destroy( id->p_decoder );
2304 if( id->p_encoder->p_module )
2305 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2306 vlc_object_detach( id->p_encoder );
2307 vlc_object_destroy( id->p_encoder );
2310 /* Free subpictures */
2311 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2313 if( !p_sys->pp_subpics[i] ) continue;
2315 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2316 p_sys->pp_subpics[i] = NULL;
2320 static int transcode_spu_process( sout_stream_t *p_stream,
2321 sout_stream_id_t *id,
2322 block_t *in, block_t **out )
2324 sout_stream_sys_t *p_sys = p_stream->p_sys;
2325 subpicture_t *p_subpic;
2328 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2329 if( p_subpic && p_sys->b_soverlay )
2333 /* Find a free slot in our supictures ring buffer */
2334 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2336 if( !p_sys->pp_subpics[i] )
2338 p_sys->pp_subpics[i] = p_subpic;
2342 if( i == SUBPICTURE_RING_SIZE )
2344 spu_del_buffer( id->p_decoder, p_subpic );
2348 if( p_subpic && !p_sys->b_soverlay )
2352 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2353 spu_del_buffer( id->p_decoder, p_subpic );
2357 block_ChainAppend( out, p_block );
2362 return VLC_EGENERIC;
2365 static subpicture_t *transcode_spu_get( sout_stream_t *p_stream,
2366 sout_stream_id_t *id,
2367 mtime_t display_date )
2369 sout_stream_sys_t *p_sys = p_stream->p_sys;
2370 subpicture_t *p_subpic = 0;
2371 subpicture_t *p_ephemer = 0;
2372 subpicture_t **pp_subpic = &p_subpic;
2375 /* Find current subpictures and remove old ones */
2376 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2378 if( !p_sys->pp_subpics[i] ) continue;
2380 if( !p_sys->pp_subpics[i]->b_ephemer &&
2381 p_sys->pp_subpics[i]->i_stop < display_date )
2383 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2384 p_sys->pp_subpics[i] = NULL;
2388 if( p_sys->pp_subpics[i]->i_start > display_date ) continue;
2390 if( p_sys->pp_subpics[i]->b_ephemer && !p_ephemer )
2392 p_ephemer = p_sys->pp_subpics[i];
2394 else if( p_sys->pp_subpics[i]->b_ephemer )
2396 if( p_ephemer->i_start < p_sys->pp_subpics[i]->i_start )
2400 *p_ephemer = *p_sys->pp_subpics[i];
2401 *p_sys->pp_subpics[i] = tmp;
2404 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2405 p_sys->pp_subpics[i] = NULL;
2409 /* Add subpicture to the list */
2410 *pp_subpic = p_sys->pp_subpics[i];
2411 pp_subpic = &p_sys->pp_subpics[i]->p_next;
2417 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2419 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
2420 memset( p_subpic, 0, sizeof(subpicture_t) );
2421 p_subpic->b_absolute = VLC_TRUE;
2423 p_subpic->pf_create_region = __spu_CreateRegion;
2424 p_subpic->pf_destroy_region = __spu_DestroyRegion;
2429 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2431 while( p_subpic->p_region )
2433 subpicture_region_t *p_region = p_subpic->p_region;
2434 p_subpic->p_region = p_region->p_next;
2435 p_subpic->pf_destroy_region( VLC_OBJECT(p_dec), p_region );