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 DEINTERLACE_TEXT N_("Deinterlace video")
69 #define DEINTERLACE_LONGTEXT N_( \
70 "Allows you to deinterlace the video before encoding." )
71 #define WIDTH_TEXT N_("Video width")
72 #define WIDTH_LONGTEXT N_( \
73 "Allows you to specify the output video width." )
74 #define HEIGHT_TEXT N_("Video height")
75 #define HEIGHT_LONGTEXT N_( \
76 "Allows you to specify the output video height." )
78 #define CROPTOP_TEXT N_("Video crop top")
79 #define CROPTOP_LONGTEXT N_( \
80 "Allows you to specify the top coordinate for the video cropping." )
81 #define CROPLEFT_TEXT N_("Video crop left")
82 #define CROPLEFT_LONGTEXT N_( \
83 "Allows you to specify the left coordinate for the video cropping." )
84 #define CROPBOTTOM_TEXT N_("Video crop bottom")
85 #define CROPBOTTOM_LONGTEXT N_( \
86 "Allows you to specify the bottom coordinate for the video cropping." )
87 #define CROPRIGHT_TEXT N_("Video crop right")
88 #define CROPRIGHT_LONGTEXT N_( \
89 "Allows you to specify the right coordinate for the video cropping." )
91 #define AENC_TEXT N_("Audio encoder")
92 #define AENC_LONGTEXT N_( \
93 "Allows you to specify the audio encoder to use and its associated " \
95 #define ACODEC_TEXT N_("Destination audio codec")
96 #define ACODEC_LONGTEXT N_( \
97 "Allows you to specify the destination audio codec used for the " \
99 #define AB_TEXT N_("Audio bitrate")
100 #define AB_LONGTEXT N_( \
101 "Allows you to specify the audio bitrate used for the streaming " \
103 #define ARATE_TEXT N_("Audio sample rate")
104 #define ARATE_LONGTEXT N_( \
105 "Allows you to specify the audio sample rate used for the streaming " \
107 #define ACHANS_TEXT N_("Audio channels")
108 #define ACHANS_LONGTEXT N_( \
109 "Allows you to specify the number of audio channels used for the " \
110 "streaming output." )
112 #define SENC_TEXT N_("Subtitles encoder")
113 #define SENC_LONGTEXT N_( \
114 "Allows you to specify the subtitles encoder to use and its associated " \
116 #define SCODEC_TEXT N_("Destination subtitles codec")
117 #define SCODEC_LONGTEXT N_( \
118 "Allows you to specify the destination subtitles codec used for the " \
119 "streaming output." )
121 #define THREADS_TEXT N_("Number of threads")
122 #define THREADS_LONGTEXT N_( \
123 "Allows you to specify the number of threads used for the transcoding." )
125 static int Open ( vlc_object_t * );
126 static void Close( vlc_object_t * );
128 #define SOUT_CFG_PREFIX "sout-transcode-"
131 set_description( _("Transcode stream output") );
132 set_capability( "sout stream", 50 );
133 add_shortcut( "transcode" );
134 set_callbacks( Open, Close );
136 add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
137 VENC_LONGTEXT, VLC_FALSE );
138 add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
139 VCODEC_LONGTEXT, VLC_FALSE );
140 add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
141 VB_LONGTEXT, VLC_FALSE );
142 add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
143 SCALE_LONGTEXT, VLC_FALSE );
144 add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
145 DEINTERLACE_LONGTEXT, VLC_FALSE );
146 add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
147 WIDTH_LONGTEXT, VLC_TRUE );
148 add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
149 HEIGHT_LONGTEXT, VLC_TRUE );
151 add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
152 CROPTOP_LONGTEXT, VLC_TRUE );
153 add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
154 CROPLEFT_LONGTEXT, VLC_TRUE );
155 add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
156 CROPBOTTOM_LONGTEXT, VLC_TRUE );
157 add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
158 CROPRIGHT_LONGTEXT, VLC_TRUE );
160 add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
161 AENC_LONGTEXT, VLC_FALSE );
162 add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
163 ACODEC_LONGTEXT, VLC_FALSE );
164 add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
165 AB_LONGTEXT, VLC_FALSE );
166 add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
167 ACHANS_LONGTEXT, VLC_FALSE );
168 add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
169 ARATE_LONGTEXT, VLC_TRUE );
171 add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
172 SENC_LONGTEXT, VLC_FALSE );
173 add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
174 SCODEC_LONGTEXT, VLC_FALSE );
175 add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
176 SCODEC_LONGTEXT, VLC_FALSE );
178 add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
179 THREADS_LONGTEXT, VLC_TRUE );
182 static const char *ppsz_sout_options[] = {
183 "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
184 "scale", "width", "height", "deinterlace", "threads",
185 "aenc", "acodec", "ab", "samplerate", "channels",
186 "senc", "scodec", "soverlay", NULL
189 /*****************************************************************************
190 * Exported prototypes
191 *****************************************************************************/
192 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
193 static int Del ( sout_stream_t *, sout_stream_id_t * );
194 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
196 static int transcode_audio_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
197 static void transcode_audio_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
198 static int transcode_audio_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
200 static int transcode_video_ffmpeg_new ( sout_stream_t *, sout_stream_id_t * );
201 static void transcode_video_ffmpeg_close ( sout_stream_t *, sout_stream_id_t * );
202 static int transcode_video_ffmpeg_process( sout_stream_t *, sout_stream_id_t *, block_t *, block_t ** );
204 static int transcode_video_ffmpeg_getframebuf( struct AVCodecContext *, AVFrame *);
206 static int transcode_spu_new ( sout_stream_t *, sout_stream_id_t * );
207 static void transcode_spu_close ( sout_stream_t *, sout_stream_id_t * );
208 static int transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
209 block_t *, block_t ** );
210 static subpicture_t *transcode_spu_get( sout_stream_t *, sout_stream_id_t *,
213 static int EncoderThread( struct sout_stream_sys_t * p_sys );
215 static int pi_channels_maps[6] =
218 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
219 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
220 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
221 | AOUT_CHAN_REARRIGHT,
222 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
223 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
226 #define PICTURE_RING_SIZE 64
227 #define SUBPICTURE_RING_SIZE 20
229 struct sout_stream_sys_t
233 sout_stream_t * p_out;
234 sout_stream_id_t * id_video;
236 vlc_mutex_t lock_out;
238 picture_t * pp_pics[PICTURE_RING_SIZE];
239 int i_first_pic, i_last_pic;
242 vlc_fourcc_t i_acodec; /* codec audio (0 if not transcode) */
244 sout_cfg_t *p_audio_cfg;
250 vlc_fourcc_t i_vcodec; /* codec video (0 if not transcode) */
252 sout_cfg_t *p_video_cfg;
257 vlc_bool_t b_deinterlace;
267 vlc_bool_t b_input_has_b_frames;
269 mtime_t i_output_pts;
272 vlc_fourcc_t i_scodec; /* codec spu (0 if not transcode) */
274 vlc_bool_t b_soverlay;
275 sout_cfg_t *p_spu_cfg;
276 subpicture_t *pp_subpics[SUBPICTURE_RING_SIZE];
279 filter_t *p_filter_blend;
282 /*****************************************************************************
284 *****************************************************************************/
285 static int Open( vlc_object_t *p_this )
287 sout_stream_t *p_stream = (sout_stream_t*)p_this;
288 sout_stream_sys_t *p_sys;
292 p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
294 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
297 msg_Err( p_stream, "cannot create chain" );
302 p_sys->b_input_has_b_frames = VLC_FALSE;
303 p_sys->i_output_pts = 0;
305 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
308 /* Audio transcoding parameters */
309 var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
310 p_sys->psz_aenc = NULL;
311 p_sys->p_audio_cfg = NULL;
312 if( val.psz_string && *val.psz_string )
315 psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
317 if( psz_next ) free( psz_next );
319 if( val.psz_string ) free( val.psz_string );
321 var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
323 if( val.psz_string && *val.psz_string )
326 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
327 p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
329 if( val.psz_string ) free( val.psz_string );
331 var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
332 p_sys->i_abitrate = val.i_int;
333 if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
335 var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
336 p_sys->i_sample_rate = val.i_int;
338 var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
339 p_sys->i_channels = val.i_int;
341 if( p_sys->i_acodec )
343 msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
344 (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
345 p_sys->i_channels, p_sys->i_abitrate / 1000 );
348 /* Video transcoding parameters */
349 var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
350 p_sys->psz_venc = NULL;
351 p_sys->p_video_cfg = NULL;
352 if( val.psz_string && *val.psz_string )
355 psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
357 if( psz_next ) free( psz_next );
359 if( val.psz_string ) free( val.psz_string );
361 var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
363 if( val.psz_string && *val.psz_string )
366 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
367 p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
369 if( val.psz_string ) free( val.psz_string );
371 var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
372 p_sys->i_vbitrate = val.i_int;
373 if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
375 var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
376 p_sys->f_scale = val.f_float;
378 var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
379 p_sys->i_width = val.i_int;
381 var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
382 p_sys->i_height = val.i_int;
384 var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
385 p_sys->b_deinterlace = val.b_bool;
387 var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
388 p_sys->i_crop_top = val.i_int;
389 var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
390 p_sys->i_crop_bottom = val.i_int;
391 var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
392 p_sys->i_crop_left = val.i_int;
393 var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
394 p_sys->i_crop_right = val.i_int;
396 var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
397 p_sys->i_threads = val.i_int;
399 if( p_sys->i_vcodec )
401 msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
402 (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
403 p_sys->f_scale, p_sys->i_vbitrate / 1000 );
406 /* Subpictures transcoding parameters */
407 var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
408 p_sys->psz_senc = NULL;
409 p_sys->p_spu_cfg = NULL;
410 if( val.psz_string && *val.psz_string )
413 psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
415 if( psz_next ) free( psz_next );
417 if( val.psz_string ) free( val.psz_string );
419 var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
421 if( val.psz_string && *val.psz_string )
424 memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
425 p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
427 if( val.psz_string ) free( val.psz_string );
429 if( p_sys->i_scodec )
431 msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_acodec );
434 var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
435 p_sys->b_soverlay = val.b_bool;
436 p_sys->p_filter_blend = 0;
438 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
440 p_sys->pp_subpics[i] = 0;
443 p_stream->pf_add = Add;
444 p_stream->pf_del = Del;
445 p_stream->pf_send = Send;
446 p_stream->p_sys = p_sys;
449 avcodec_register_all();
454 /*****************************************************************************
456 *****************************************************************************/
457 static void Close( vlc_object_t * p_this )
459 sout_stream_t *p_stream = (sout_stream_t*)p_this;
460 sout_stream_sys_t *p_sys = p_stream->p_sys;
462 sout_StreamDelete( p_sys->p_out );
464 while( p_sys->p_audio_cfg != NULL )
466 sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
468 if( p_sys->p_audio_cfg->psz_name )
469 free( p_sys->p_audio_cfg->psz_name );
470 if( p_sys->p_audio_cfg->psz_value )
471 free( p_sys->p_audio_cfg->psz_value );
472 free( p_sys->p_audio_cfg );
474 p_sys->p_audio_cfg = p_next;
476 if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
478 while( p_sys->p_video_cfg != NULL )
480 sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
482 if( p_sys->p_video_cfg->psz_name )
483 free( p_sys->p_video_cfg->psz_name );
484 if( p_sys->p_video_cfg->psz_value )
485 free( p_sys->p_video_cfg->psz_value );
486 free( p_sys->p_video_cfg );
488 p_sys->p_video_cfg = p_next;
490 if( p_sys->psz_venc ) free( p_sys->psz_venc );
492 while( p_sys->p_spu_cfg != NULL )
494 sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
496 if( p_sys->p_spu_cfg->psz_name )
497 free( p_sys->p_spu_cfg->psz_name );
498 if( p_sys->p_spu_cfg->psz_value )
499 free( p_sys->p_spu_cfg->psz_value );
500 free( p_sys->p_spu_cfg );
502 p_sys->p_spu_cfg = p_next;
504 if( p_sys->psz_senc ) free( p_sys->psz_senc );
506 if( p_sys->p_filter_blend )
508 if( p_sys->p_filter_blend->p_module )
509 module_Unneed( p_sys->p_filter_blend,
510 p_sys->p_filter_blend->p_module );
512 vlc_object_detach( p_sys->p_filter_blend );
513 vlc_object_destroy( p_sys->p_filter_blend );
516 vlc_object_destroy( p_sys );
519 struct sout_stream_id_t
521 vlc_fourcc_t b_transcode;
522 es_format_t f_src; /* only if transcoding */
523 es_format_t f_dst; /* " " " */
524 unsigned int i_inter_pixfmt; /* intermediary format when transcoding */
526 /* id of the out stream */
530 decoder_t *p_decoder;
533 encoder_t *p_encoder;
534 vlc_fourcc_t b_enc_inited;
538 AVCodecContext *ff_dec_c;
548 AVFrame *p_ff_pic_tmp0; /* to do deinterlace */
549 AVFrame *p_ff_pic_tmp1; /* to do pix conversion */
550 AVFrame *p_ff_pic_tmp2; /* to do resample */
551 AVFrame *p_ff_pic_tmp3; /* to do subpicture overlay */
553 ImgReSampleContext *p_vresample;
557 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
559 sout_stream_sys_t *p_sys = p_stream->p_sys;
560 sout_stream_id_t *id;
562 id = malloc( sizeof( sout_stream_id_t ) );
565 id->p_decoder = NULL;
566 id->p_encoder = NULL;
568 if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
571 "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
572 (char*)&p_fmt->i_codec,
573 (char*)&p_sys->i_acodec );
576 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
578 /* create dst format */
579 es_format_Init( &id->f_dst, AUDIO_ES, p_sys->i_acodec );
580 id->f_dst.i_id = id->f_src.i_id;
581 id->f_dst.i_group = id->f_src.i_group;
582 if( id->f_src.psz_language )
583 id->f_dst.psz_language = strdup( id->f_src.psz_language );
584 id->f_dst.audio.i_rate = p_sys->i_sample_rate > 0 ?
585 p_sys->i_sample_rate : id->f_src.audio.i_rate;
586 id->f_dst.audio.i_channels = p_sys->i_channels > 0 ?
587 p_sys->i_channels : id->f_src.audio.i_channels;
588 id->f_dst.i_bitrate = p_sys->i_abitrate;
589 id->f_dst.audio.i_blockalign = 0;
590 id->f_dst.i_extra = 0;
591 id->f_dst.p_extra = NULL;
593 /* build decoder -> filter -> encoder */
594 if( transcode_audio_ffmpeg_new( p_stream, id ) )
596 msg_Err( p_stream, "cannot create audio chain" );
601 /* open output stream */
602 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
603 id->b_transcode = VLC_TRUE;
611 else if( p_fmt->i_cat == VIDEO_ES &&
612 (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
615 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
616 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
618 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
620 /* create dst format */
621 es_format_Init( &id->f_dst, VIDEO_ES, p_sys->i_vcodec );
622 id->f_dst.i_id = id->f_src.i_id;
623 id->f_dst.i_group = id->f_src.i_group;
624 if( id->f_src.psz_language )
625 id->f_dst.psz_language = strdup( id->f_src.psz_language );
626 id->f_dst.video.i_width = p_sys->i_width;
627 id->f_dst.video.i_height = p_sys->i_height;
628 id->f_dst.i_bitrate = p_sys->i_vbitrate;
629 id->f_dst.i_extra = 0;
630 id->f_dst.p_extra = NULL;
632 /* build decoder -> filter -> encoder */
633 if( transcode_video_ffmpeg_new( p_stream, id ) )
635 msg_Err( p_stream, "cannot create video chain" );
640 /* open output stream */
641 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
643 id->b_transcode = VLC_TRUE;
645 else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
647 msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
648 "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
649 (char*)&p_sys->i_scodec );
652 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
654 /* create dst format */
655 es_format_Init( &id->f_dst, SPU_ES, p_sys->i_scodec );
656 id->f_dst.i_id = id->f_src.i_id;
657 id->f_dst.i_group = id->f_src.i_group;
658 if( id->f_src.psz_language )
659 id->f_dst.psz_language = strdup( id->f_src.psz_language );
660 id->f_dst.i_extra = 0;
661 id->f_dst.p_extra = NULL;
663 /* build decoder -> filter -> encoder */
664 if( transcode_spu_new( p_stream, id ) )
666 msg_Err( p_stream, "cannot create subtitles chain" );
671 /* open output stream */
672 id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->f_dst );
673 id->b_transcode = VLC_TRUE;
681 else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
683 msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
684 (char*)&p_fmt->i_codec );
686 id->b_transcode = VLC_TRUE;
689 memcpy( &id->f_src, p_fmt, sizeof( es_format_t ) );
691 /* build decoder -> filter -> encoder */
692 if( transcode_spu_new( p_stream, id ) )
694 msg_Err( p_stream, "cannot create subtitles chain" );
701 msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
702 (char*)&p_fmt->i_codec );
703 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
704 id->b_transcode = VLC_FALSE;
716 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
718 sout_stream_sys_t *p_sys = p_stream->p_sys;
720 if( id->b_transcode )
722 if( id->f_src.i_cat == AUDIO_ES )
724 transcode_audio_ffmpeg_close( p_stream, id );
726 else if( id->f_src.i_cat == VIDEO_ES )
728 transcode_video_ffmpeg_close( p_stream, id );
730 else if( id->f_src.i_cat == SPU_ES )
732 transcode_spu_close( p_stream, id );
736 if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
742 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
745 sout_stream_sys_t *p_sys = p_stream->p_sys;
747 if( id->b_transcode )
749 block_t *p_buffer_out;
751 /* Be sure to have padding */
752 p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
753 if( p_buffer == NULL )
757 p_buffer->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
758 memset( &p_buffer->p_buffer[p_buffer->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
760 if( id->f_src.i_cat == AUDIO_ES )
762 transcode_audio_ffmpeg_process( p_stream, id, p_buffer,
764 block_Release( p_buffer );
766 else if( id->f_src.i_cat == VIDEO_ES )
768 if( transcode_video_ffmpeg_process( p_stream, id, p_buffer,
769 &p_buffer_out ) != VLC_SUCCESS )
771 block_Release( p_buffer );
774 block_Release( p_buffer );
776 else if( id->f_src.i_cat == SPU_ES )
778 if( transcode_spu_process( p_stream, id, p_buffer,
779 &p_buffer_out ) != VLC_SUCCESS )
786 block_Release( p_buffer );
791 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
795 else if( id->id != NULL )
797 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
801 block_Release( p_buffer );
806 /****************************************************************************
807 * ffmpeg decoder reencocdr part
808 ****************************************************************************/
814 } fourcc_to_ff_code[] =
817 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
818 { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
819 { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
820 { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
821 { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
822 { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
823 { VLC_FOURCC( 'w', 'm', 'a', '2' ), CODEC_ID_WMAV2 },
824 { VLC_FOURCC( 'v', 'o', 'r', 'b' ), CODEC_ID_VORBIS },
825 { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
828 { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
829 { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
830 #if LIBAVCODEC_BUILD >= 4676
831 { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
833 { VLC_FOURCC( 'm', 'p', '4', 'v'), CODEC_ID_MPEG4 },
834 { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
835 { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
836 { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
837 { VLC_FOURCC( 'H', '2', '6', '3' ), CODEC_ID_H263 },
838 { VLC_FOURCC( 'I', '2', '6', '3' ), CODEC_ID_H263I },
839 { VLC_FOURCC( 'h', 'u', 'f', 'f' ), CODEC_ID_HUFFYUV },
840 { VLC_FOURCC( 'W', 'M', 'V', '1' ), CODEC_ID_WMV1 },
841 { VLC_FOURCC( 'W', 'M', 'V', '2' ), CODEC_ID_WMV2 },
842 { VLC_FOURCC( 'M', 'J', 'P', 'G' ), CODEC_ID_MJPEG },
843 { VLC_FOURCC( 'm', 'j', 'p', 'b' ), CODEC_ID_MJPEGB },
844 { VLC_FOURCC( 'd', 'v', 's', 'l' ), CODEC_ID_DVVIDEO },
845 { VLC_FOURCC( 'S', 'V', 'Q', '1' ), CODEC_ID_SVQ1 },
846 #if LIBAVCODEC_BUILD >= 4666
847 { VLC_FOURCC( 'S', 'V', 'Q', '3' ), CODEC_ID_SVQ3 },
848 { VLC_FOURCC( 'h', '2', '6', '4' ), CODEC_ID_H264 },
850 #if LIBAVCODEC_BUILD >= 4719
851 { VLC_FOURCC( 'S', 'N', 'O', 'W' ), CODEC_ID_SNOW },
854 /* raw video code, only used for 'encoding' */
855 { VLC_FOURCC( 'I', '4', '2', '0' ), CODEC_ID_RAWVIDEO },
856 { VLC_FOURCC( 'I', '4', '2', '2' ), CODEC_ID_RAWVIDEO },
857 { VLC_FOURCC( 'I', '4', '4', '4' ), CODEC_ID_RAWVIDEO },
858 { VLC_FOURCC( 'R', 'V', '1', '5' ), CODEC_ID_RAWVIDEO },
859 { VLC_FOURCC( 'R', 'V', '1', '6' ), CODEC_ID_RAWVIDEO },
860 { VLC_FOURCC( 'R', 'V', '2', '4' ), CODEC_ID_RAWVIDEO },
861 { VLC_FOURCC( 'R', 'V', '3', '2' ), CODEC_ID_RAWVIDEO },
862 { VLC_FOURCC( 'Y', 'U', 'Y', '2' ), CODEC_ID_RAWVIDEO },
863 { VLC_FOURCC( 'Y', 'V', '1', '2' ), CODEC_ID_RAWVIDEO },
864 { VLC_FOURCC( 'I', 'Y', 'U', 'V' ), CODEC_ID_RAWVIDEO },
866 { VLC_FOURCC( 0, 0, 0, 0 ), 0 }
869 static inline int get_ff_codec( vlc_fourcc_t i_fcc )
873 for( i = 0; fourcc_to_ff_code[i].i_fcc != 0; i++ )
875 if( fourcc_to_ff_code[i].i_fcc == i_fcc )
877 return fourcc_to_ff_code[i].i_ff_codec;
884 static inline int get_ff_chroma( vlc_fourcc_t i_chroma )
888 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
889 case VLC_FOURCC( 'I', 'Y', 'U', 'V' ):
890 case VLC_FOURCC( 'I', '4', '2', '0' ):
891 return PIX_FMT_YUV420P;
892 case VLC_FOURCC( 'I', '4', '2', '2' ):
893 return PIX_FMT_YUV422P;
894 case VLC_FOURCC( 'I', '4', '4', '4' ):
895 return PIX_FMT_YUV444P;
896 case VLC_FOURCC( 'R', 'V', '1', '5' ):
897 return PIX_FMT_RGB555;
898 case VLC_FOURCC( 'R', 'V', '1', '6' ):
899 return PIX_FMT_RGB565;
900 case VLC_FOURCC( 'R', 'V', '2', '4' ):
901 return PIX_FMT_BGR24;
902 case VLC_FOURCC( 'R', 'V', '3', '2' ):
903 return PIX_FMT_RGBA32;
904 case VLC_FOURCC( 'G', 'R', 'E', 'Y' ):
905 return PIX_FMT_GRAY8;
906 case VLC_FOURCC( 'Y', 'U', 'Y', '2' ):
907 return PIX_FMT_YUV422;
913 static inline vlc_fourcc_t get_vlc_chroma( int i_pix_fmt )
917 case PIX_FMT_YUV420P:
918 return VLC_FOURCC('I','4','2','0');
919 case PIX_FMT_YUV422P:
920 return VLC_FOURCC('I','4','2','2');
921 case PIX_FMT_YUV444P:
922 return VLC_FOURCC('I','4','4','4');
925 return VLC_FOURCC('Y','U','Y','2');
928 return VLC_FOURCC('R','V','1','5');
930 return VLC_FOURCC('R','V','1','6');
932 return VLC_FOURCC('R','V','2','4');
934 return VLC_FOURCC('R','V','3','2');
936 return VLC_FOURCC('G','R','E','Y');
938 case PIX_FMT_YUV410P:
939 case PIX_FMT_YUV411P:
946 static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
947 sout_stream_id_t *id )
951 if( id->f_src.i_codec == VLC_FOURCC('s','1','6','l') ||
952 id->f_src.i_codec == VLC_FOURCC('s','1','6','b') ||
953 id->f_src.i_codec == VLC_FOURCC('s','8',' ',' ') ||
954 id->f_src.i_codec == VLC_FOURCC('u','8',' ',' ') )
958 id->ff_dec_c = avcodec_alloc_context();
959 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
960 id->ff_dec_c->channels = id->f_src.audio.i_channels;
961 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
962 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
967 i_ff_codec = get_ff_codec( id->f_src.i_codec );
968 if( i_ff_codec == 0 )
970 msg_Err( p_stream, "cannot find decoder id" );
974 id->ff_dec = avcodec_find_decoder( i_ff_codec );
977 msg_Err( p_stream, "cannot find decoder (avcodec)" );
981 id->ff_dec_c = avcodec_alloc_context();
982 id->ff_dec_c->sample_rate = id->f_src.audio.i_rate;
983 id->ff_dec_c->channels = id->f_src.audio.i_channels;
984 id->ff_dec_c->block_align = id->f_src.audio.i_blockalign;
985 id->ff_dec_c->bit_rate = id->f_src.i_bitrate;
987 id->ff_dec_c->extradata_size = id->f_src.i_extra;
988 id->ff_dec_c->extradata = id->f_src.p_extra;
989 if( avcodec_open( id->ff_dec_c, id->ff_dec ) )
991 msg_Err( p_stream, "cannot open decoder" );
992 av_free( id->ff_dec_c );
997 id->i_buffer = 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE;
998 id->i_buffer_pos = 0;
999 id->p_buffer = malloc( id->i_buffer );
1001 /* Sanity check for audio channels */
1002 id->f_dst.audio.i_channels =
1003 __MIN( id->f_dst.audio.i_channels, id->f_src.audio.i_channels );
1006 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1008 /* Initialization of encoder format structures */
1009 es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
1010 id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
1011 id->p_encoder->fmt_in.audio.i_rate = id->f_dst.audio.i_rate;
1012 id->p_encoder->fmt_in.audio.i_physical_channels =
1013 id->p_encoder->fmt_in.audio.i_original_channels =
1014 pi_channels_maps[id->f_dst.audio.i_channels];
1015 id->p_encoder->fmt_in.audio.i_channels = id->f_dst.audio.i_channels;
1017 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1018 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1019 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1021 id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
1023 /* Attach object to parent so object variables inheritance works */
1024 vlc_object_attach( id->p_encoder, p_stream );
1026 id->p_encoder->p_module =
1027 module_Need( id->p_encoder, "encoder",
1028 p_stream->p_sys->psz_aenc, VLC_TRUE );
1029 if( !id->p_encoder->p_module )
1031 vlc_object_detach( id->p_encoder );
1032 vlc_object_destroy( id->p_encoder );
1033 msg_Err( p_stream, "cannot open encoder" );
1034 av_free( id->ff_dec_c );
1035 return VLC_EGENERIC;
1038 id->b_enc_inited = VLC_FALSE;
1040 id->f_dst.audio.i_channels = id->p_encoder->fmt_in.audio.i_channels;
1041 id->f_dst.audio.i_rate = id->p_encoder->fmt_in.audio.i_rate;
1042 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1043 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1044 id->f_dst.i_codec = id->p_encoder->fmt_out.i_codec;
1046 /* Hack for mp3 transcoding support */
1047 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
1049 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','a' );
1055 static void transcode_audio_ffmpeg_close( sout_stream_t *p_stream,
1056 sout_stream_id_t *id )
1058 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1059 av_free( id->ff_dec_c );
1061 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1063 vlc_object_detach( id->p_encoder );
1064 vlc_object_destroy( id->p_encoder );
1066 free( id->p_buffer );
1069 static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
1070 sout_stream_id_t *id,
1074 aout_buffer_t aout_buf;
1076 int i_buffer = in->i_buffer;
1077 char *p_buffer = in->p_buffer;
1078 id->i_dts = in->i_dts;
1083 id->i_buffer_pos = 0;
1085 /* decode as much data as possible */
1090 i_used = avcodec_decode_audio( id->ff_dec_c,
1091 (int16_t*)id->p_buffer, &id->i_buffer_pos,
1092 p_buffer, i_buffer );
1095 msg_Warn( p_stream, "avcodec_decode_audio: %d used on %d",
1100 msg_Warn( p_stream, "error audio decoding");
1107 if ( id->i_buffer_pos < 0 )
1109 msg_Warn( p_stream, "weird error audio decoding");
1115 int16_t *sout = (int16_t*)id->p_buffer;
1117 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) ||
1118 id->f_src.i_codec == VLC_FOURCC( 'u', '8', ' ', ' ' ) )
1120 int8_t *sin = (int8_t*)p_buffer;
1121 int i_used = __MIN( id->i_buffer/2, i_buffer );
1122 int i_samples = i_used;
1124 if( id->f_src.i_codec == VLC_FOURCC( 's', '8', ' ', ' ' ) )
1125 while( i_samples > 0 )
1127 *sout++ = ( *sin++ ) << 8;
1131 while( i_samples > 0 )
1133 *sout++ = ( *sin++ - 128 ) << 8;
1139 id->i_buffer_pos = i_used * 2;
1141 else if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) ||
1142 id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
1144 int16_t *sin = (int16_t*)p_buffer;
1145 int i_used = __MIN( id->i_buffer, i_buffer );
1146 int i_samples = i_used / 2;
1149 memcpy( sout, sin, i_used );
1151 #ifdef WORDS_BIGENDIAN
1152 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'l' ) )
1154 if( id->f_src.i_codec == VLC_FOURCC( 's', '1', '6', 'b' ) )
1157 uint8_t *dat = (uint8_t*)sout;
1159 while( i_samples > 0 )
1174 id->i_buffer_pos = i_used;
1178 if( id->i_buffer_pos == 0 ) continue;
1180 /* Encode as much data as possible */
1181 if( !id->b_enc_inited && id->p_encoder->pf_header )
1183 block_t *p_block_tmp;
1185 p_block = id->p_encoder->pf_header( id->p_encoder );
1186 p_block_tmp = p_block;
1187 while( p_block_tmp )
1189 p_block_tmp->i_dts = p_block_tmp->i_pts = in->i_dts;
1190 p_block_tmp = p_block_tmp->p_next;
1192 block_ChainAppend( out, p_block );
1194 id->b_enc_inited = VLC_TRUE;
1197 aout_buf.p_buffer = id->p_buffer;
1198 aout_buf.i_nb_bytes = id->i_buffer_pos;
1199 aout_buf.i_nb_samples = id->i_buffer_pos / 2 / id->f_src.audio.i_channels;
1200 aout_buf.start_date = id->i_dts;
1201 aout_buf.end_date = id->i_dts;
1203 id->i_dts += ( I64C(1000000) * id->i_buffer_pos / 2 /
1204 id->f_src.audio.i_channels / id->f_src.audio.i_rate );
1206 if( id->p_encoder->fmt_in.audio.i_channels == 1 &&
1207 id->f_src.audio.i_channels > 1 )
1209 int16_t *p_sample = (int16_t *)aout_buf.p_buffer;
1210 int i_src_c = id->f_src.audio.i_channels;
1213 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1217 for( j = 1; j < i_src_c; j++ )
1219 c += p_sample[i_src_c * i + j];
1221 p_sample[i] = c / (i_src_c-1);
1223 aout_buf.i_nb_bytes = i * 2;
1225 else if( id->p_encoder->fmt_in.audio.i_channels == 2 &&
1226 id->f_src.audio.i_channels > 2 )
1228 int i_src_c = id->f_src.audio.i_channels;
1231 static const float mixf_l[4][6] = /* [i_src_c - 3][channel index] */
1233 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1234 { 0.00, 0.50, 0.50, 0.00, 0.00, 0.00 }, /* 4 channels */
1235 { 0.00, 0.50, 0.00, 0.50, 0.00, 0.00 }, /* 5 channels */
1236 { 0.00, 0.34, 0.33, 0.00, 0.33, 0.00 }, /* 6 channels */
1238 static const float mixf_r[4][6] = /* [i_src_c - 3][channel index] */
1240 { 0.00, 1.00, 0.00, 0.00, 0.00, 0.00 }, /* 3 channels */
1241 { 0.00, 0.00, 0.50, 0.50, 0.00, 0.00 }, /* 4 channels */
1242 { 0.00, 0.00, 0.50, 0.00, 0.50, 0.00 }, /* 5 channels */
1243 { 0.00, 0.00, 0.33, 0.34, 0.00, 0.33 }, /* 6 channels */
1247 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1249 int16_t *p_src = (int16_t *)aout_buf.p_buffer + i_src_c * i;
1250 int16_t *p_dst = (int16_t *)aout_buf.p_buffer + 2 * i;
1253 float l = 0.0, r = 0.0;
1254 for( j = 0; j < i_src_c; j++ )
1256 l += mixf_l[i_src_c-3][j] * p_src[j];
1257 r += mixf_r[i_src_c-3][j] * p_src[j];
1260 p_dst[0] = (int)( l + 0.5 );
1261 p_dst[1] = (int)( r + 0.5 );
1263 aout_buf.i_nb_bytes = i * 2 * 2;
1265 else if( id->f_src.audio.i_channels !=
1266 id->p_encoder->fmt_in.audio.i_channels )
1271 /* This is for liba52 which is what ffmpeg uses to decode ac3 */
1272 static const int translation[7][6] =
1273 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
1274 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
1275 { 0, 1, 0, 0, 0, 0 }, /* 2 */
1276 { 1, 2, 0, 0, 0, 0 }, /* 3 */
1277 { 1, 3, 2, 0, 0, 0 }, /* 4 */
1278 { 1, 3, 4, 2, 0, 0 }, /* 5 */
1279 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
1281 /* dumb downmixing */
1282 for( i = 0; i < aout_buf.i_nb_samples; i++ )
1284 uint16_t *p_buffer = (uint16_t *)aout_buf.p_buffer;
1285 for( j = 0 ; j < id->p_encoder->fmt_in.audio.i_channels; j++ )
1287 p_buffer[i*id->p_encoder->fmt_in.audio.i_channels+j] =
1288 p_buffer[i*id->f_src.audio.i_channels+
1289 translation[id->f_src.audio.i_channels][j]];
1292 aout_buf.i_nb_bytes = i*id->p_encoder->fmt_in.audio.i_channels * 2;
1295 p_block = id->p_encoder->pf_encode_audio( id->p_encoder, &aout_buf );
1296 block_ChainAppend( out, p_block );
1306 static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
1307 sout_stream_id_t *id )
1309 sout_stream_sys_t *p_sys = p_stream->p_sys;
1313 if( id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '0' ) ||
1314 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '2', '2' ) ||
1315 id->f_src.i_codec == VLC_FOURCC( 'I', '4', '4', '4' ) ||
1316 id->f_src.i_codec == VLC_FOURCC( 'Y', 'V', '1', '2' ) ||
1317 id->f_src.i_codec == VLC_FOURCC( 'Y', 'U', 'Y', '2' ) ||
1318 id->f_src.i_codec == VLC_FOURCC( 'I', 'Y', 'U', 'V' ) ||
1319 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
1320 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
1321 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
1322 id->f_src.i_codec == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
1323 id->f_src.i_codec == VLC_FOURCC( 'G', 'R', 'E', 'Y' ) )
1326 id->ff_dec_c = avcodec_alloc_context();
1327 id->ff_dec_c->width = id->f_src.video.i_width;
1328 id->ff_dec_c->height = id->f_src.video.i_height;
1329 id->ff_dec_c->pix_fmt = get_ff_chroma( id->f_src.i_codec );
1331 #if LIBAVCODEC_BUILD >= 4687
1332 if( id->ff_dec_c->width )
1333 id->ff_dec_c->sample_aspect_ratio =
1334 av_d2q( id->f_src.video.i_aspect / (double)VOUT_ASPECT_FACTOR *
1335 id->ff_dec_c->height / id->ff_dec_c->width, 255 );
1337 id->ff_dec_c->aspect_ratio =
1338 id->f_src.video.i_aspect / (float)VOUT_ASPECT_FACTOR;
1344 i_ff_codec = get_ff_codec( id->f_src.i_codec );
1345 if( i_ff_codec == 0 )
1347 msg_Err( p_stream, "cannot find decoder" );
1348 return VLC_EGENERIC;
1351 id->ff_dec = avcodec_find_decoder( i_ff_codec );
1354 msg_Err( p_stream, "cannot find decoder" );
1355 return VLC_EGENERIC;
1358 id->ff_dec_c = avcodec_alloc_context();
1359 id->ff_dec_c->width = id->f_src.video.i_width;
1360 id->ff_dec_c->height = id->f_src.video.i_height;
1361 id->ff_dec_c->bits_per_sample=id->f_src.video.i_bits_per_pixel;
1362 /* id->ff_dec_c->bit_rate = id->f_src.i_bitrate; */
1364 if( id->f_src.i_extra > 0 )
1366 if( i_ff_codec == CODEC_ID_SVQ3 )
1368 int i_size = id->f_src.i_extra;
1371 id->ff_dec_c->extradata_size = i_size + 12;
1372 p = id->ff_dec_c->extradata = malloc( i_size + 12 );
1374 memcpy( &p[0], "SVQ3", 4 );
1375 memset( &p[4], 0, 8 );
1376 memcpy( &p[12], id->f_src.p_extra, i_size );
1378 /* Now remove all atoms before the SMI one */
1379 if( id->ff_dec_c->extradata_size > 0x5a && strncmp( &p[0x56], "SMI ", 4 ) )
1381 uint8_t *psz = &p[0x52];
1383 while( psz < &p[id->ff_dec_c->extradata_size - 8] )
1385 int i_size = GetDWBE( psz );
1388 /* FIXME handle 1 as long size */
1391 if( !strncmp( &psz[4], "SMI ", 4 ) )
1393 memmove( &p[0x52], psz, &p[id->ff_dec_c->extradata_size] - psz );
1402 id->ff_dec_c->extradata_size= id->f_src.i_extra;
1403 id->ff_dec_c->extradata = malloc( id->f_src.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
1405 memcpy( id->ff_dec_c->extradata, id->f_src.p_extra, id->f_src.i_extra );
1406 memset( (uint8_t*)id->ff_dec_c->extradata + id->f_src.i_extra, 0, FF_INPUT_BUFFER_PADDING_SIZE );
1409 id->ff_dec_c->workaround_bugs = FF_BUG_AUTODETECT;
1410 id->ff_dec_c->error_resilience= -1;
1411 id->ff_dec_c->get_buffer = transcode_video_ffmpeg_getframebuf;
1412 id->ff_dec_c->opaque = p_sys;
1414 if( avcodec_open( id->ff_dec_c, id->ff_dec ) < 0 )
1416 msg_Err( p_stream, "cannot open decoder" );
1417 av_free( id->ff_dec_c );
1418 return VLC_EGENERIC;
1423 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
1425 /* Initialization of encoder format structures */
1426 es_format_Init( &id->p_encoder->fmt_in,
1427 id->f_src.i_cat, get_vlc_chroma(id->ff_dec_c->pix_fmt) );
1429 /* The dimensions will be set properly later on.
1430 * Just put sensible values so we can test if there is an encoder. */
1431 id->p_encoder->fmt_in.video.i_width =
1432 id->f_src.video.i_width ? id->f_src.video.i_width : 16;
1433 id->p_encoder->fmt_in.video.i_height =
1434 id->f_src.video.i_height ? id->f_src.video.i_height : 16;
1436 id->p_encoder->fmt_in.video.i_frame_rate = 25; /* FIXME as it break mpeg */
1437 id->p_encoder->fmt_in.video.i_frame_rate_base= 1;
1440 id->p_encoder->fmt_in.video.i_frame_rate = id->ff_dec_c->frame_rate;
1441 #if LIBAVCODEC_BUILD >= 4662
1442 id->p_encoder->fmt_in.video.i_frame_rate_base =
1443 id->ff_dec_c->frame_rate_base;
1446 #if LIBAVCODEC_BUILD >= 4687
1447 if( id->ff_dec_c->height )
1448 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1449 ( av_q2d(id->ff_dec_c->sample_aspect_ratio) *
1450 id->ff_dec_c->width / id->ff_dec_c->height );
1452 id->p_encoder->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR *
1453 id->ff_dec_c->aspect_ratio;
1457 /* Check whether a particular aspect ratio was requested */
1458 if( id->f_src.video.i_aspect )
1460 id->p_encoder->fmt_in.video.i_aspect = id->f_src.video.i_aspect;
1461 id->f_dst.video.i_aspect = id->f_src.video.i_aspect;
1464 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
1465 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
1466 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
1468 id->p_encoder->i_threads = p_sys->i_threads;
1470 id->p_ff_pic = avcodec_alloc_frame();
1471 id->p_ff_pic_tmp0 = NULL;
1472 id->p_ff_pic_tmp1 = NULL;
1473 id->p_ff_pic_tmp2 = NULL;
1474 id->p_ff_pic_tmp3 = NULL;
1475 id->p_vresample = NULL;
1477 id->p_encoder->p_cfg = p_sys->p_video_cfg;
1479 /* Attach object to parent so object variables inheritance works */
1480 vlc_object_attach( id->p_encoder, p_stream );
1482 id->p_encoder->p_module =
1483 module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
1485 if( !id->p_encoder->p_module )
1487 vlc_object_detach( id->p_encoder );
1488 vlc_object_destroy( id->p_encoder );
1489 av_free( id->ff_dec_c );
1490 msg_Err( p_stream, "cannot find encoder" );
1491 return VLC_EGENERIC;
1494 /* Close the encoder.
1495 * We'll open it only when we have the first frame */
1496 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1497 id->p_encoder->p_module = NULL;
1499 id->b_enc_inited = VLC_FALSE;
1501 if ( p_sys->i_threads >= 1 )
1503 p_sys->id_video = id;
1504 vlc_mutex_init( p_stream, &p_sys->lock_out );
1505 vlc_cond_init( p_stream, &p_sys->cond );
1506 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
1507 p_sys->i_first_pic = 0;
1508 p_sys->i_last_pic = 0;
1509 p_sys->p_buffers = NULL;
1510 p_sys->b_die = p_sys->b_error = 0;
1511 if( vlc_thread_create( p_sys, "encoder", EncoderThread,
1512 VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
1514 vlc_object_detach( id->p_encoder );
1515 vlc_object_destroy( id->p_encoder );
1516 av_free( id->ff_dec_c );
1517 msg_Err( p_stream, "cannot spawn encoder thread" );
1518 return VLC_EGENERIC;
1525 static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream,
1526 sout_stream_id_t *id )
1528 if ( p_stream->p_sys->i_threads >= 1 )
1530 vlc_mutex_lock( &p_stream->p_sys->lock_out );
1531 p_stream->p_sys->b_die = 1;
1532 vlc_cond_signal( &p_stream->p_sys->cond );
1533 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
1534 vlc_thread_join( p_stream->p_sys );
1535 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
1536 vlc_cond_destroy( &p_stream->p_sys->cond );
1540 if( id->ff_dec ) avcodec_close( id->ff_dec_c );
1541 av_free( id->ff_dec_c );
1544 if( id->p_encoder->p_module )
1545 module_Unneed( id->p_encoder, id->p_encoder->p_module );
1546 vlc_object_detach( id->p_encoder );
1547 vlc_object_destroy( id->p_encoder );
1552 free( id->p_ff_pic );
1555 if( id->p_ff_pic_tmp0 )
1557 free( id->p_ff_pic_tmp0->data[0] );
1558 free( id->p_ff_pic_tmp0 );
1560 if( id->p_ff_pic_tmp1 )
1562 free( id->p_ff_pic_tmp1->data[0] );
1563 free( id->p_ff_pic_tmp1 );
1565 if( id->p_ff_pic_tmp2 )
1567 free( id->p_ff_pic_tmp2->data[0] );
1568 free( id->p_ff_pic_tmp2 );
1570 if( id->p_ff_pic_tmp3 )
1572 free( id->p_ff_pic_tmp3->data[0] );
1573 free( id->p_ff_pic_tmp3 );
1575 if( id->p_vresample )
1577 img_resample_close( id->p_vresample );
1581 static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
1582 sout_stream_id_t *id, block_t *in, block_t **out )
1584 sout_stream_sys_t *p_sys = p_stream->p_sys;
1594 i_data = in->i_buffer;
1595 p_data = in->p_buffer;
1602 subpicture_t *p_subpic = 0;
1605 frame = id->p_ff_pic;
1606 p_sys->i_input_pts = in->i_pts;
1607 p_sys->i_input_dts = in->i_dts;
1610 i_used = avcodec_decode_video( id->ff_dec_c, frame,
1617 avpicture_fill( (AVPicture*)frame, p_data,
1618 id->ff_dec_c->pix_fmt,
1619 id->ff_dec_c->width, id->ff_dec_c->height );
1624 frame->pts = p_sys->i_input_pts ? p_sys->i_input_pts :
1627 frame->pict_type = FF_I_TYPE;
1632 msg_Warn( p_stream, "error");
1633 return VLC_EGENERIC;
1643 /* Get the pts of the decoded frame if any, otherwise keep the
1644 * interpolated one */
1645 if( frame->pts != AV_NOPTS_VALUE )
1647 p_sys->i_output_pts = frame->pts;
1650 /* Sanity check (seems to be needed for some streams ) */
1651 if( frame->pict_type == FF_B_TYPE )
1653 p_sys->b_input_has_b_frames = VLC_TRUE;
1656 if( !id->b_enc_inited )
1658 /* Hack because of the copy packetizer which can fail to detect the
1659 * proper size (which forces us to wait until the 1st frame
1661 int i_width = id->ff_dec_c->width - p_sys->i_crop_left -
1662 p_sys->i_crop_right;
1663 int i_height = id->ff_dec_c->height - p_sys->i_crop_top -
1664 p_sys->i_crop_bottom;
1666 if( id->f_dst.video.i_width <= 0 && id->f_dst.video.i_height <= 0
1669 /* Apply the scaling */
1670 id->f_dst.video.i_width = i_width * p_sys->f_scale;
1671 id->f_dst.video.i_height = i_height * p_sys->f_scale;
1673 else if( id->f_dst.video.i_width > 0 &&
1674 id->f_dst.video.i_height <= 0 )
1676 id->f_dst.video.i_height =
1677 id->f_dst.video.i_width / (double)i_width * i_height;
1679 else if( id->f_dst.video.i_width <= 0 &&
1680 id->f_dst.video.i_height > 0 )
1682 id->f_dst.video.i_width =
1683 id->f_dst.video.i_height / (double)i_height * i_width;
1686 id->p_encoder->fmt_in.video.i_width =
1687 id->p_encoder->fmt_out.video.i_width =
1688 id->f_dst.video.i_width;
1689 id->p_encoder->fmt_in.video.i_height =
1690 id->p_encoder->fmt_out.video.i_height =
1691 id->f_dst.video.i_height;
1693 id->p_encoder->fmt_out.i_extra = 0;
1694 id->p_encoder->fmt_out.p_extra = NULL;
1696 id->p_encoder->p_module =
1697 module_Need( id->p_encoder, "encoder",
1698 p_sys->psz_venc, VLC_TRUE );
1699 if( !id->p_encoder->p_module )
1701 vlc_object_destroy( id->p_encoder );
1702 msg_Err( p_stream, "cannot find encoder" );
1703 id->b_transcode = VLC_FALSE;
1704 return VLC_EGENERIC;
1707 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
1708 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
1709 id->f_dst.i_codec = id->p_encoder->fmt_out.i_codec;
1711 /* Hack for mp2v/mp1v transcoding support */
1712 if( id->f_dst.i_codec == VLC_FOURCC( 'm','p','1','v' ) ||
1713 id->f_dst.i_codec == VLC_FOURCC( 'm','p','2','v' ) )
1715 id->f_dst.i_codec = VLC_FOURCC( 'm','p','g','v' );
1719 p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
1722 msg_Err( p_stream, "cannot add this stream" );
1723 transcode_video_ffmpeg_close( p_stream, id );
1724 id->b_transcode = VLC_FALSE;
1725 return VLC_EGENERIC;
1728 if( id->p_encoder->pf_header )
1730 block_t *p_block_tmp;
1732 p_block = id->p_encoder->pf_header( id->p_encoder );
1733 p_block_tmp = p_block;
1734 while( p_block_tmp )
1736 p_block_tmp->i_dts = p_block_tmp->i_pts = in->i_dts;
1737 p_block_tmp = p_block_tmp->p_next;
1739 block_ChainAppend( out, p_block );
1742 id->i_inter_pixfmt =
1743 get_ff_chroma( id->p_encoder->fmt_in.i_codec );
1745 id->b_enc_inited = VLC_TRUE;
1749 if( p_stream->p_sys->b_deinterlace )
1751 if( id->p_ff_pic_tmp0 == NULL )
1755 id->p_ff_pic_tmp0 = avcodec_alloc_frame();
1756 i_size = avpicture_get_size( id->ff_dec_c->pix_fmt,
1757 id->ff_dec_c->width,
1758 id->ff_dec_c->height );
1760 buf = malloc( i_size );
1762 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
1764 id->ff_dec_c->width, id->ff_dec_c->height );
1767 avpicture_deinterlace( (AVPicture*)id->p_ff_pic_tmp0,
1768 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1769 id->ff_dec_c->width, id->ff_dec_c->height );
1771 #if LIBAVCODEC_BUILD >= 4685
1772 id->p_ff_pic_tmp0->interlaced_frame = 0;
1774 id->p_ff_pic_tmp0->repeat_pict = frame->repeat_pict;
1775 frame = id->p_ff_pic_tmp0;
1778 /* convert pix format */
1779 if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
1781 if( id->p_ff_pic_tmp1 == NULL )
1785 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
1786 i_size = avpicture_get_size( id->i_inter_pixfmt,
1787 id->ff_dec_c->width,
1788 id->ff_dec_c->height );
1790 buf = malloc( i_size );
1792 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
1794 id->ff_dec_c->width, id->ff_dec_c->height );
1797 img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
1798 (AVPicture*)frame, id->ff_dec_c->pix_fmt,
1799 id->ff_dec_c->width, id->ff_dec_c->height );
1801 id->p_ff_pic_tmp1->repeat_pict = frame->repeat_pict;
1802 #if LIBAVCODEC_BUILD >= 4685
1803 id->p_ff_pic_tmp1->interlaced_frame = frame->interlaced_frame;
1804 id->p_ff_pic_tmp1->top_field_first = frame->top_field_first;
1806 frame = id->p_ff_pic_tmp1;
1809 /* convert size and crop */
1810 if( id->ff_dec_c->width != id->f_dst.video.i_width ||
1811 id->ff_dec_c->height != id->f_dst.video.i_height ||
1812 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
1813 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
1815 if( id->p_ff_pic_tmp2 == NULL )
1819 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
1820 i_size = avpicture_get_size( id->i_inter_pixfmt,
1821 id->f_dst.video.i_width,
1822 id->f_dst.video.i_height );
1824 buf = malloc( i_size );
1826 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
1828 id->f_dst.video.i_width,
1829 id->f_dst.video.i_height );
1832 img_resample_full_init( id->f_dst.video.i_width,
1833 id->f_dst.video.i_height,
1834 id->ff_dec_c->width,
1835 id->ff_dec_c->height,
1836 p_stream->p_sys->i_crop_top,
1837 p_stream->p_sys->i_crop_bottom,
1838 p_stream->p_sys->i_crop_left,
1839 p_stream->p_sys->i_crop_right
1840 #if LIBAVCODEC_BUILD >= 4708
1847 img_resample( id->p_vresample, (AVPicture*)id->p_ff_pic_tmp2,
1848 (AVPicture*)frame );
1850 id->p_ff_pic_tmp2->repeat_pict = frame->repeat_pict;
1851 #if LIBAVCODEC_BUILD >= 4685
1852 id->p_ff_pic_tmp2->interlaced_frame = frame->interlaced_frame;
1853 id->p_ff_pic_tmp2->top_field_first = frame->top_field_first;
1855 frame = id->p_ff_pic_tmp2;
1859 p_pic = malloc(sizeof(picture_t));
1860 vout_InitPicture( VLC_OBJECT(p_stream), p_pic,
1861 id->p_encoder->fmt_in.i_codec,
1862 id->f_dst.video.i_width, id->f_dst.video.i_height,
1863 id->f_dst.video.i_width * VOUT_ASPECT_FACTOR /
1864 id->f_dst.video.i_height );
1866 /* Check if we have a subpicture to overlay */
1867 if( p_sys->p_filter_blend )
1869 p_subpic = transcode_spu_get( p_stream, id, p_sys->i_output_pts );
1871 if( p_subpic && frame != id->p_ff_pic_tmp0 &&
1872 frame != id->p_ff_pic_tmp1 && frame != id->p_ff_pic_tmp2 )
1874 if( id->p_ff_pic_tmp3 == NULL )
1876 uint8_t *buf = malloc( frame->linesize[0] *
1877 p_pic->p[0].i_lines * 3 );
1878 id->p_ff_pic_tmp3 = avcodec_alloc_frame();
1879 *id->p_ff_pic_tmp3 = *frame;
1880 id->p_ff_pic_tmp3->data[0] = buf;
1881 id->p_ff_pic_tmp3->data[1] = id->p_ff_pic_tmp3->data[0] +
1882 frame->linesize[0] * p_pic->p[0].i_lines;
1883 id->p_ff_pic_tmp3->data[2] = id->p_ff_pic_tmp3->data[1] +
1884 frame->linesize[1] * p_pic->p[1].i_lines;
1887 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1889 p_stream->p_vlc->pf_memcpy(
1890 id->p_ff_pic_tmp3->data[i_plane],
1891 frame->data[i_plane],
1892 p_pic->p[i_plane].i_lines * frame->linesize[i_plane] );
1895 id->p_ff_pic_tmp3->repeat_pict = frame->repeat_pict;
1896 #if LIBAVCODEC_BUILD >= 4685
1897 id->p_ff_pic_tmp3->interlaced_frame = frame->interlaced_frame;
1898 id->p_ff_pic_tmp3->top_field_first = frame->top_field_first;
1900 frame = id->p_ff_pic_tmp3;
1904 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
1906 p_pic->p[i_plane].i_pitch = frame->linesize[i_plane];
1907 if ( p_sys->i_threads >= 1 )
1909 p_pic->p[i_plane].p_pixels = malloc(p_pic->p[i_plane].i_lines *
1910 p_pic->p[i_plane].i_pitch);
1911 p_stream->p_vlc->pf_memcpy( p_pic->p[i_plane].p_pixels,
1912 frame->data[i_plane], p_pic->p[i_plane].i_lines *
1913 p_pic->p[i_plane].i_pitch );
1917 p_pic->p[i_plane].p_pixels = frame->data[i_plane];
1921 /* Set the pts of the frame being encoded */
1922 p_pic->date = p_sys->i_output_pts;
1924 p_pic->i_nb_fields = frame->repeat_pict;
1925 #if LIBAVCODEC_BUILD >= 4685
1926 p_pic->b_progressive = !frame->interlaced_frame;
1927 p_pic->b_top_field_first = frame->top_field_first;
1930 /* Interpolate the next PTS
1931 * (needed by the mpeg video packetizer which can send pts <= 0 ) */
1932 if( id->ff_dec_c && id->ff_dec_c->frame_rate > 0 )
1934 p_sys->i_output_pts += I64C(1000000) * (2 + frame->repeat_pict) *
1935 id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
1938 /* Overlay subpicture */
1941 int i_width, i_height;
1943 p_sys->p_filter_blend->fmt_out = id->p_encoder->fmt_in;
1944 p_sys->p_filter_blend->fmt_out.video.i_visible_width =
1945 p_sys->p_filter_blend->fmt_out.video.i_width;
1946 p_sys->p_filter_blend->fmt_out.video.i_visible_height =
1947 p_sys->p_filter_blend->fmt_out.video.i_height;
1948 p_sys->p_filter_blend->fmt_out.video.i_chroma =
1949 VLC_FOURCC('I','4','2','0');
1951 i_width = id->p_encoder->fmt_in.video.i_width;
1952 i_height = id->p_encoder->fmt_in.video.i_height;
1954 while( p_subpic != NULL )
1956 subpicture_region_t *p_region = p_subpic->p_region;
1958 while( p_region && p_sys->p_filter_blend &&
1959 p_sys->p_filter_blend->pf_video_blend )
1961 int i_x_offset = p_region->i_x + p_subpic->i_x;
1962 int i_y_offset = p_region->i_y + p_subpic->i_y;
1964 if( p_subpic->i_flags & OSD_ALIGN_BOTTOM )
1966 i_y_offset = i_height - p_region->fmt.i_height -
1969 else if ( !(p_subpic->i_flags & OSD_ALIGN_TOP) )
1971 i_y_offset = i_height / 2 - p_region->fmt.i_height / 2;
1974 if( p_subpic->i_flags & OSD_ALIGN_RIGHT )
1976 i_x_offset = i_width - p_region->fmt.i_width -
1979 else if ( !(p_subpic->i_flags & OSD_ALIGN_LEFT) )
1981 i_x_offset = i_width / 2 - p_region->fmt.i_width / 2;
1984 if( p_subpic->b_absolute )
1986 i_x_offset = p_region->i_x + p_subpic->i_x;
1987 i_y_offset = p_region->i_y + p_subpic->i_y;
1990 p_sys->p_filter_blend->fmt_in.video = p_region->fmt;
1992 p_sys->p_filter_blend->pf_video_blend(
1993 p_sys->p_filter_blend, p_pic, p_pic,
1994 &p_region->picture, i_x_offset, i_y_offset );
1996 p_region = p_region->p_next;
1999 p_subpic = p_subpic->p_next;
2003 if ( p_sys->i_threads >= 1 )
2005 vlc_mutex_lock( &p_sys->lock_out );
2006 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
2007 p_sys->i_last_pic %= PICTURE_RING_SIZE;
2008 *out = p_sys->p_buffers;
2009 p_sys->p_buffers = NULL;
2010 vlc_cond_signal( &p_sys->cond );
2011 vlc_mutex_unlock( &p_sys->lock_out );
2016 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2017 block_ChainAppend( out, p_block );
2030 static int EncoderThread( sout_stream_sys_t * p_sys )
2032 sout_stream_id_t * id = p_sys->id_video;
2036 while ( !p_sys->b_die && !p_sys->b_error )
2040 vlc_mutex_lock( &p_sys->lock_out );
2041 while ( p_sys->i_last_pic == p_sys->i_first_pic )
2043 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
2044 if ( p_sys->b_die || p_sys->b_error )
2047 if ( p_sys->b_die || p_sys->b_error )
2049 vlc_mutex_unlock( &p_sys->lock_out );
2053 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2054 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2055 vlc_mutex_unlock( &p_sys->lock_out );
2057 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
2058 vlc_mutex_lock( &p_sys->lock_out );
2059 block_ChainAppend( &p_sys->p_buffers, p_block );
2060 vlc_mutex_unlock( &p_sys->lock_out );
2062 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
2064 free( p_pic->p[i_plane].p_pixels );
2069 while ( p_sys->i_last_pic != p_sys->i_first_pic )
2071 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
2072 p_sys->i_first_pic %= PICTURE_RING_SIZE;
2074 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
2076 free( p_pic->p[i_plane].p_pixels );
2081 block_ChainRelease( p_sys->p_buffers );
2086 /*****************************************************************************
2087 * transcode_video_ffmpeg_getframebuf:
2089 * Callback used by ffmpeg to get a frame buffer.
2090 * We use it to get the right PTS for each decoded picture.
2091 *****************************************************************************/
2092 static int transcode_video_ffmpeg_getframebuf(struct AVCodecContext *p_context,
2095 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_context->opaque;
2098 if( p_sys->i_input_pts )
2100 p_frame->pts = p_sys->i_input_pts;
2102 else if( p_sys->i_input_dts )
2104 /* Some demuxers/packetizers only set the dts so let's try to find a
2105 * useful timestamp from this */
2106 if( !p_context->has_b_frames || !p_sys->b_input_has_b_frames ||
2107 !p_frame->reference || !p_sys->i_output_pts )
2109 p_frame->pts = p_sys->i_input_dts +
2110 /* Hack: ffmpeg encoding doesn't like frames with identical pts */
2111 (p_sys->i_output_pts ? 0 : 50000);
2113 else p_frame->pts = AV_NOPTS_VALUE;
2115 else p_frame->pts = AV_NOPTS_VALUE;
2117 if( p_sys->i_output_pts ) /* make sure 1st frame has a pts > 0 */
2119 p_sys->i_input_pts = 0;
2120 p_sys->i_input_dts = 0;
2123 return avcodec_default_get_buffer( p_context, p_frame );
2129 static subpicture_t *spu_new_buffer( decoder_t * );
2130 static void spu_del_buffer( decoder_t *, subpicture_t * );
2132 static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
2134 sout_stream_sys_t *p_sys = p_stream->p_sys;
2137 id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
2139 /* Initialization of decoder format structures */
2140 id->p_decoder->fmt_in = id->f_src;
2141 id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
2142 id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
2143 //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
2145 /* Attach object to parent so object variables inheritance works */
2146 vlc_object_attach( id->p_decoder, p_stream );
2148 id->p_decoder->p_module =
2149 module_Need( id->p_decoder, "decoder", "$codec", VLC_TRUE );
2151 if( !id->p_decoder->p_module )
2153 vlc_object_detach( id->p_decoder );
2154 vlc_object_destroy( id->p_decoder );
2155 msg_Err( p_stream, "cannot find decoder" );
2156 return VLC_EGENERIC;
2159 if( !p_sys->b_soverlay )
2162 id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
2164 /* Initialization of encoder format structures */
2165 es_format_Init( &id->p_encoder->fmt_in,
2166 id->f_src.i_cat, id->f_src.i_codec );
2168 id->p_encoder->fmt_out = id->p_encoder->fmt_in;
2169 id->p_encoder->fmt_out.i_codec = id->f_dst.i_codec;
2170 id->p_encoder->fmt_out.i_bitrate = id->f_dst.i_bitrate;
2172 id->p_encoder->p_cfg = p_sys->p_spu_cfg;
2174 /* Attach object to parent so object variables inheritance works */
2175 vlc_object_attach( id->p_encoder, p_stream );
2177 id->p_encoder->p_module =
2178 module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
2180 if( !id->p_encoder->p_module )
2182 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2183 vlc_object_detach( id->p_decoder );
2184 vlc_object_destroy( id->p_decoder );
2186 vlc_object_detach( id->p_encoder );
2187 vlc_object_destroy( id->p_encoder );
2188 msg_Err( p_stream, "cannot find encoder" );
2189 return VLC_EGENERIC;
2192 id->f_dst.i_extra = id->p_encoder->fmt_out.i_extra;
2193 id->f_dst.p_extra = id->p_encoder->fmt_out.p_extra;
2194 id->f_dst.i_codec = id->p_encoder->fmt_out.i_codec;
2195 id->f_dst.subs = id->p_encoder->fmt_out.subs;
2199 p_sys->p_filter_blend =
2200 vlc_object_create( p_stream, sizeof(filter_t) );
2201 vlc_object_attach( p_sys->p_filter_blend, p_stream );
2202 p_sys->p_filter_blend->fmt_out.video.i_chroma =
2203 VLC_FOURCC('I','4','2','0');
2204 p_sys->p_filter_blend->fmt_in.video.i_chroma =
2205 VLC_FOURCC('Y','U','V','A');
2206 p_sys->p_filter_blend->p_module =
2207 module_Need( p_sys->p_filter_blend, "video blending", 0, 0 );
2213 static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
2215 sout_stream_sys_t *p_sys = p_stream->p_sys;
2219 if( id->p_decoder->p_module )
2220 module_Unneed( id->p_decoder, id->p_decoder->p_module );
2221 vlc_object_detach( id->p_decoder );
2222 vlc_object_destroy( id->p_decoder );
2227 if( id->p_encoder->p_module )
2228 module_Unneed( id->p_encoder, id->p_encoder->p_module );
2229 vlc_object_detach( id->p_encoder );
2230 vlc_object_destroy( id->p_encoder );
2233 /* Free subpictures */
2234 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2236 if( !p_sys->pp_subpics[i] ) continue;
2238 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2239 p_sys->pp_subpics[i] = NULL;
2243 static int transcode_spu_process( sout_stream_t *p_stream,
2244 sout_stream_id_t *id,
2245 block_t *in, block_t **out )
2247 sout_stream_sys_t *p_sys = p_stream->p_sys;
2248 subpicture_t *p_subpic;
2251 p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
2252 if( p_subpic && p_sys->b_soverlay )
2256 /* Find a free slot in our supictures ring buffer */
2257 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2259 if( !p_sys->pp_subpics[i] )
2261 p_sys->pp_subpics[i] = p_subpic;
2265 if( i == SUBPICTURE_RING_SIZE )
2267 spu_del_buffer( id->p_decoder, p_subpic );
2271 if( p_subpic && !p_sys->b_soverlay )
2275 p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
2276 spu_del_buffer( id->p_decoder, p_subpic );
2280 block_ChainAppend( out, p_block );
2285 return VLC_EGENERIC;
2288 static subpicture_t *transcode_spu_get( sout_stream_t *p_stream,
2289 sout_stream_id_t *id,
2290 mtime_t display_date )
2292 sout_stream_sys_t *p_sys = p_stream->p_sys;
2293 subpicture_t *p_subpic = 0;
2294 subpicture_t *p_ephemer = 0;
2295 subpicture_t **pp_subpic = &p_subpic;
2298 /* Find current subpictures and remove old ones */
2299 for( i = 0; i < SUBPICTURE_RING_SIZE; i++ )
2301 if( !p_sys->pp_subpics[i] ) continue;
2303 if( !p_sys->pp_subpics[i]->b_ephemer &&
2304 p_sys->pp_subpics[i]->i_stop < display_date )
2306 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2307 p_sys->pp_subpics[i] = NULL;
2311 if( p_sys->pp_subpics[i]->i_start > display_date ) continue;
2313 if( p_sys->pp_subpics[i]->b_ephemer && !p_ephemer )
2315 p_ephemer = p_sys->pp_subpics[i];
2317 else if( p_sys->pp_subpics[i]->b_ephemer )
2319 if( p_ephemer->i_start < p_sys->pp_subpics[i]->i_start )
2323 *p_ephemer = *p_sys->pp_subpics[i];
2324 *p_sys->pp_subpics[i] = tmp;
2327 spu_del_buffer( id->p_decoder, p_sys->pp_subpics[i] );
2328 p_sys->pp_subpics[i] = NULL;
2332 /* Add subpicture to the list */
2333 *pp_subpic = p_sys->pp_subpics[i];
2334 pp_subpic = &p_sys->pp_subpics[i]->p_next;
2340 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2342 subpicture_t *p_subpic = (subpicture_t *)malloc(sizeof(subpicture_t));
2343 memset( p_subpic, 0, sizeof(subpicture_t) );
2344 p_subpic->b_absolute = VLC_TRUE;
2346 p_subpic->pf_create_region = __spu_CreateRegion;
2347 p_subpic->pf_destroy_region = __spu_DestroyRegion;
2352 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2354 while( p_subpic->p_region )
2356 subpicture_region_t *p_region = p_subpic->p_region;
2357 p_subpic->p_region = p_region->p_next;
2358 p_subpic->pf_destroy_region( VLC_OBJECT(p_dec), p_region );