1 /*****************************************************************************
2 * video.c: transcoding stream output module (video)
3 *****************************************************************************
4 * Copyright (C) 2003-2009 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
10 * Antoine Cellerier <dionoea at videolan dot org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
31 #include "transcode.h"
36 #define ENC_FRAMERATE (25 * 1000 + .5)
37 #define ENC_FRAMERATE_BASE 1000
39 struct decoder_owner_sys_t
41 sout_stream_sys_t *p_sys;
44 static inline void video_timer_start( encoder_t * p_encoder )
46 stats_TimerStart( p_encoder, "encoding video frame",
47 STATS_TIMER_VIDEO_FRAME_ENCODING );
50 static inline void video_timer_stop( encoder_t * p_encoder )
52 stats_TimerStop( p_encoder, STATS_TIMER_VIDEO_FRAME_ENCODING );
55 static inline void video_timer_close( encoder_t * p_encoder )
57 stats_TimerDump( p_encoder, STATS_TIMER_VIDEO_FRAME_ENCODING );
58 stats_TimerClean( p_encoder, STATS_TIMER_VIDEO_FRAME_ENCODING );
61 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
63 VLC_UNUSED(p_decoder);
64 picture_Release( p_pic );
67 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
70 picture_Hold( p_pic );
73 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
76 picture_Release( p_pic );
79 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
81 sout_stream_sys_t *p_ssys = p_dec->p_owner->p_sys;
82 if( p_ssys->i_threads >= 1 )
84 int i_first_pic = p_ssys->i_first_pic;
86 if( p_ssys->i_first_pic != p_ssys->i_last_pic )
88 /* Encoder still has stuff to encode, wait to clear-up the list */
89 while( p_ssys->i_first_pic == i_first_pic )
91 #warning THERE IS DEFINITELY A BUG! LOCKING IS INSUFFICIENT!
98 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
99 return picture_NewFromFormat( &p_dec->fmt_out.video );
102 static picture_t *transcode_video_filter_buffer_new( filter_t *p_filter )
104 p_filter->fmt_out.video.i_chroma = p_filter->fmt_out.i_codec;
105 return picture_NewFromFormat( &p_filter->fmt_out.video );
107 static void transcode_video_filter_buffer_del( filter_t *p_filter, picture_t *p_pic )
109 VLC_UNUSED(p_filter);
110 picture_Release( p_pic );
113 static int transcode_video_filter_allocation_init( filter_t *p_filter,
117 p_filter->pf_vout_buffer_new = transcode_video_filter_buffer_new;
118 p_filter->pf_vout_buffer_del = transcode_video_filter_buffer_del;
122 static void transcode_video_filter_allocation_clear( filter_t *p_filter )
124 VLC_UNUSED(p_filter);
127 static void* EncoderThread( vlc_object_t* p_this )
129 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_this;
130 sout_stream_id_t *id = p_sys->id_video;
132 int canc = vlc_savecancel ();
134 while( vlc_object_alive (p_sys) && !p_sys->b_error )
138 vlc_mutex_lock( &p_sys->lock_out );
139 while( p_sys->i_last_pic == p_sys->i_first_pic )
141 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
142 if( !vlc_object_alive (p_sys) || p_sys->b_error ) break;
144 if( !vlc_object_alive (p_sys) || p_sys->b_error )
146 vlc_mutex_unlock( &p_sys->lock_out );
150 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
151 p_sys->i_first_pic %= PICTURE_RING_SIZE;
152 vlc_mutex_unlock( &p_sys->lock_out );
154 video_timer_start( id->p_encoder );
155 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
156 video_timer_stop( id->p_encoder );
158 vlc_mutex_lock( &p_sys->lock_out );
159 block_ChainAppend( &p_sys->p_buffers, p_block );
161 vlc_mutex_unlock( &p_sys->lock_out );
162 picture_Release( p_pic );
165 while( p_sys->i_last_pic != p_sys->i_first_pic )
167 p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
168 p_sys->i_first_pic %= PICTURE_RING_SIZE;
169 picture_Release( p_pic );
171 block_ChainRelease( p_sys->p_buffers );
173 vlc_restorecancel (canc);
177 int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
179 sout_stream_sys_t *p_sys = p_stream->p_sys;
182 * Initialization of decoder structures
184 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
185 id->p_decoder->fmt_out.i_extra = 0;
186 id->p_decoder->fmt_out.p_extra = 0;
187 id->p_decoder->pf_decode_video = NULL;
188 id->p_decoder->pf_get_cc = NULL;
189 id->p_decoder->pf_get_cc = 0;
190 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
191 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
192 id->p_decoder->pf_picture_link = video_link_picture_decoder;
193 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
194 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
195 if( !id->p_decoder->p_owner )
198 id->p_decoder->p_owner->p_sys = p_sys;
199 /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
201 id->p_decoder->p_module =
202 module_need( id->p_decoder, "decoder", "$codec", false );
204 if( !id->p_decoder->p_module )
206 msg_Err( p_stream, "cannot find video decoder" );
207 free( id->p_decoder->p_owner );
213 * Because some info about the decoded input will only be available
214 * once the first frame is decoded, we actually only test the availability
215 * of the encoder here.
218 /* Initialization of encoder format structures */
219 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
220 id->p_decoder->fmt_out.i_codec );
221 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
223 /* The dimensions will be set properly later on.
224 * Just put sensible values so we can test an encoder is available. */
225 id->p_encoder->fmt_in.video.i_width =
226 id->p_encoder->fmt_out.video.i_width
227 ? id->p_encoder->fmt_out.video.i_width
228 : id->p_decoder->fmt_in.video.i_width
229 ? id->p_decoder->fmt_in.video.i_width : 16;
230 id->p_encoder->fmt_in.video.i_height =
231 id->p_encoder->fmt_out.video.i_height
232 ? id->p_encoder->fmt_out.video.i_height
233 : id->p_decoder->fmt_in.video.i_height
234 ? id->p_decoder->fmt_in.video.i_height : 16;
235 id->p_encoder->fmt_in.video.i_frame_rate = ENC_FRAMERATE;
236 id->p_encoder->fmt_in.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
238 id->p_encoder->i_threads = p_sys->i_threads;
239 id->p_encoder->p_cfg = p_sys->p_video_cfg;
241 id->p_encoder->p_module =
242 module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
243 if( !id->p_encoder->p_module )
245 msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
246 p_sys->psz_venc ? p_sys->psz_venc : "any",
247 (char *)&p_sys->i_vcodec );
248 module_unneed( id->p_decoder, id->p_decoder->p_module );
249 id->p_decoder->p_module = 0;
250 free( id->p_decoder->p_owner );
254 /* Close the encoder.
255 * We'll open it only when we have the first frame. */
256 module_unneed( id->p_encoder, id->p_encoder->p_module );
257 if( id->p_encoder->fmt_out.p_extra )
259 free( id->p_encoder->fmt_out.p_extra );
260 id->p_encoder->fmt_out.p_extra = NULL;
261 id->p_encoder->fmt_out.i_extra = 0;
263 id->p_encoder->p_module = NULL;
265 if( p_sys->i_threads >= 1 )
267 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
268 VLC_THREAD_PRIORITY_VIDEO;
269 p_sys->id_video = id;
270 vlc_mutex_init( &p_sys->lock_out );
271 vlc_cond_init( &p_sys->cond );
272 memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
273 p_sys->i_first_pic = 0;
274 p_sys->i_last_pic = 0;
275 p_sys->p_buffers = NULL;
276 p_sys->b_die = p_sys->b_error = 0;
277 if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority ) )
279 msg_Err( p_stream, "cannot spawn encoder thread" );
280 module_unneed( id->p_decoder, id->p_decoder->p_module );
281 id->p_decoder->p_module = 0;
282 free( id->p_decoder->p_owner );
288 static void transcode_video_encoder_init( sout_stream_t *p_stream,
289 sout_stream_id_t *id )
291 sout_stream_sys_t *p_sys = p_stream->p_sys;
294 * width/height of source */
295 int i_src_width = id->p_decoder->fmt_out.video.i_width;
296 int i_src_height = id->p_decoder->fmt_out.video.i_height;
298 /* with/height scaling */
299 float f_scale_width = 1;
300 float f_scale_height = 1;
302 /* width/height of output stream */
307 float f_aspect = (double)id->p_decoder->fmt_out.video.i_sar_num *
308 id->p_decoder->fmt_out.video.i_width /
309 id->p_decoder->fmt_out.video.i_sar_den /
310 id->p_decoder->fmt_out.video.i_height;
312 msg_Dbg( p_stream, "decoder aspect is %f:1", f_aspect );
314 /* Change f_aspect from source frame to source pixel */
315 f_aspect = f_aspect * i_src_height / i_src_width;
316 msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );
318 /* Calculate scaling factor for specified parameters */
319 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
320 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
322 /* Global scaling. Make sure width will remain a factor of 16 */
325 int i_new_width = i_src_width * p_sys->f_scale;
327 if( i_new_width % 16 <= 7 && i_new_width >= 16 )
328 i_new_width -= i_new_width % 16;
330 i_new_width += 16 - i_new_width % 16;
332 f_real_scale = (float)( i_new_width ) / (float) i_src_width;
334 i_new_height = __MAX( 16, i_src_height * (float)f_real_scale );
336 f_scale_width = f_real_scale;
337 f_scale_height = (float) i_new_height / (float) i_src_height;
339 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
340 id->p_encoder->fmt_out.video.i_height <= 0 )
342 /* Only width specified */
343 f_scale_width = (float)id->p_encoder->fmt_out.video.i_width/i_src_width;
344 f_scale_height = f_scale_width;
346 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
347 id->p_encoder->fmt_out.video.i_height > 0 )
349 /* Only height specified */
350 f_scale_height = (float)id->p_encoder->fmt_out.video.i_height/i_src_height;
351 f_scale_width = f_scale_height;
353 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
354 id->p_encoder->fmt_out.video.i_height > 0 )
356 /* Width and height specified */
357 f_scale_width = (float)id->p_encoder->fmt_out.video.i_width/i_src_width;
358 f_scale_height = (float)id->p_encoder->fmt_out.video.i_height/i_src_height;
361 /* check maxwidth and maxheight */
362 if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
365 f_scale_width = (float)p_sys->i_maxwidth / i_src_width;
368 if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
371 f_scale_height = (float)p_sys->i_maxheight / i_src_height;
375 /* Change aspect ratio from source pixel to scaled pixel */
376 f_aspect = f_aspect * f_scale_height / f_scale_width;
377 msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );
379 /* f_scale_width and f_scale_height are now final */
380 /* Calculate width, height from scaling
381 * Make sure its multiple of 2
383 i_dst_width = 2 * (int)(f_scale_width*i_src_width/2+0.5);
384 i_dst_height = 2 * (int)(f_scale_height*i_src_height/2+0.5);
386 /* Change aspect ratio from scaled pixel to output frame */
387 f_aspect = f_aspect * i_dst_width / i_dst_height;
389 /* Store calculated values */
390 id->p_encoder->fmt_out.video.i_width =
391 id->p_encoder->fmt_out.video.i_visible_width = i_dst_width;
392 id->p_encoder->fmt_out.video.i_height =
393 id->p_encoder->fmt_out.video.i_visible_height = i_dst_height;
395 id->p_encoder->fmt_in.video.i_width =
396 id->p_encoder->fmt_in.video.i_visible_width = i_dst_width;
397 id->p_encoder->fmt_in.video.i_height =
398 id->p_encoder->fmt_in.video.i_visible_height = i_dst_height;
400 msg_Dbg( p_stream, "source %ix%i, destination %ix%i",
401 i_src_width, i_src_height,
402 i_dst_width, i_dst_height
405 /* Handle frame rate conversion */
406 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
407 !id->p_encoder->fmt_out.video.i_frame_rate_base )
409 if( id->p_decoder->fmt_out.video.i_frame_rate &&
410 id->p_decoder->fmt_out.video.i_frame_rate_base )
412 id->p_encoder->fmt_out.video.i_frame_rate =
413 id->p_decoder->fmt_out.video.i_frame_rate;
414 id->p_encoder->fmt_out.video.i_frame_rate_base =
415 id->p_decoder->fmt_out.video.i_frame_rate_base;
419 /* Pick a sensible default value */
420 id->p_encoder->fmt_out.video.i_frame_rate = ENC_FRAMERATE;
421 id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
425 id->p_encoder->fmt_in.video.i_frame_rate =
426 id->p_encoder->fmt_out.video.i_frame_rate;
427 id->p_encoder->fmt_in.video.i_frame_rate_base =
428 id->p_encoder->fmt_out.video.i_frame_rate_base;
430 date_Init( &id->interpolated_pts,
431 id->p_encoder->fmt_out.video.i_frame_rate,
432 id->p_encoder->fmt_out.video.i_frame_rate_base );
434 /* Check whether a particular aspect ratio was requested */
435 if( id->p_encoder->fmt_out.video.i_sar_num <= 0 ||
436 id->p_encoder->fmt_out.video.i_sar_den <= 0 )
438 id->p_encoder->fmt_out.video.i_sar_num = id->p_decoder->fmt_out.video.i_sar_num * i_src_width / i_dst_width;
439 id->p_encoder->fmt_out.video.i_sar_den = id->p_decoder->fmt_out.video.i_sar_den * i_src_height / i_dst_height;
441 vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
442 &id->p_encoder->fmt_out.video.i_sar_den,
443 id->p_encoder->fmt_out.video.i_sar_num,
444 id->p_encoder->fmt_out.video.i_sar_den,
447 id->p_encoder->fmt_in.video.i_sar_num =
448 id->p_encoder->fmt_out.video.i_sar_num;
449 id->p_encoder->fmt_in.video.i_sar_den =
450 id->p_encoder->fmt_out.video.i_sar_den;
452 msg_Dbg( p_stream, "encoder aspect is %i:%i",
453 id->p_encoder->fmt_out.video.i_sar_num * id->p_encoder->fmt_out.video.i_width,
454 id->p_encoder->fmt_out.video.i_sar_den * id->p_encoder->fmt_out.video.i_height );
456 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
459 static int transcode_video_encoder_open( sout_stream_t *p_stream,
460 sout_stream_id_t *id )
462 sout_stream_sys_t *p_sys = p_stream->p_sys;
465 msg_Dbg( p_stream, "destination (after video filters) %ix%i",
466 id->p_encoder->fmt_in.video.i_width,
467 id->p_encoder->fmt_in.video.i_height );
469 id->p_encoder->p_module =
470 module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
471 if( !id->p_encoder->p_module )
473 msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
474 p_sys->psz_venc ? p_sys->psz_venc : "any",
475 (char *)&p_sys->i_vcodec );
479 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
482 id->p_encoder->fmt_out.i_codec =
483 vlc_fourcc_GetCodec( VIDEO_ES, id->p_encoder->fmt_out.i_codec );
485 id->id = sout_StreamIdAdd( p_stream->p_sys->p_out,
486 &id->p_encoder->fmt_out );
489 msg_Err( p_stream, "cannot add this stream" );
496 void transcode_video_close( sout_stream_t *p_stream,
497 sout_stream_id_t *id )
499 if( p_stream->p_sys->i_threads >= 1 )
501 vlc_mutex_lock( &p_stream->p_sys->lock_out );
502 vlc_object_kill( p_stream->p_sys );
503 vlc_cond_signal( &p_stream->p_sys->cond );
504 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
505 vlc_thread_join( p_stream->p_sys );
506 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
507 vlc_cond_destroy( &p_stream->p_sys->cond );
510 video_timer_close( id->p_encoder );
513 if( id->p_decoder->p_module )
514 module_unneed( id->p_decoder, id->p_decoder->p_module );
515 if( id->p_decoder->p_description )
516 vlc_meta_Delete( id->p_decoder->p_description );
518 free( id->p_decoder->p_owner );
521 if( id->p_encoder->p_module )
522 module_unneed( id->p_encoder, id->p_encoder->p_module );
526 filter_chain_Delete( id->p_f_chain );
528 filter_chain_Delete( id->p_uf_chain );
531 int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
532 block_t *in, block_t **out )
534 sout_stream_sys_t *p_sys = p_stream->p_sys;
536 picture_t *p_pic, *p_pic2 = NULL;
539 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
541 subpicture_t *p_subpic = NULL;
543 sout_UpdateStatistic( p_stream->p_sout, SOUT_STATISTIC_DECODED_VIDEO, 1 );
545 if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
547 mtime_t current_date = mdate();
548 if( current_date + 50000 > p_pic->date )
550 msg_Dbg( p_stream, "late picture skipped (%"PRId64")",
551 current_date + 50000 - p_pic->date );
552 picture_Release( p_pic );
557 if( p_sys->b_master_sync )
559 mtime_t i_video_drift;
560 mtime_t i_master_drift = p_sys->i_master_drift;
563 i_pts = date_Get( &id->interpolated_pts ) + 1;
564 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
565 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
567 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
568 date_Set( &id->interpolated_pts, p_pic->date );
569 i_pts = p_pic->date + 1;
571 i_video_drift = p_pic->date - i_pts;
574 /* Set the pts of the frame being encoded */
577 if( i_video_drift < (i_master_drift - 50000) )
580 msg_Dbg( p_stream, "dropping frame (%i)",
581 (int)(i_video_drift - i_master_drift) );
583 picture_Release( p_pic );
586 else if( i_video_drift > (i_master_drift + 50000) )
589 msg_Dbg( p_stream, "adding frame (%i)",
590 (int)(i_video_drift - i_master_drift) );
596 if( !id->p_encoder->p_module )
598 transcode_video_encoder_init( p_stream, id );
600 id->p_f_chain = filter_chain_New( p_stream, "video filter2",
602 transcode_video_filter_allocation_init,
603 transcode_video_filter_allocation_clear,
607 if( p_stream->p_sys->b_deinterlace )
609 filter_chain_AppendFilter( id->p_f_chain,
610 p_sys->psz_deinterlace,
611 p_sys->p_deinterlace_cfg,
612 &id->p_decoder->fmt_out,
613 &id->p_decoder->fmt_out );
615 /* Take care of the scaling and chroma conversions */
616 if( ( id->p_decoder->fmt_out.video.i_chroma !=
617 id->p_encoder->fmt_in.video.i_chroma ) ||
618 ( id->p_decoder->fmt_out.video.i_width !=
619 id->p_encoder->fmt_in.video.i_width ) ||
620 ( id->p_decoder->fmt_out.video.i_height !=
621 id->p_encoder->fmt_in.video.i_height ) )
623 filter_chain_AppendFilter( id->p_f_chain,
625 &id->p_decoder->fmt_out,
626 &id->p_encoder->fmt_in );
631 const es_format_t *p_fmt_out;
632 id->p_uf_chain = filter_chain_New( p_stream, "video filter2",
634 transcode_video_filter_allocation_init,
635 transcode_video_filter_allocation_clear,
637 filter_chain_Reset( id->p_uf_chain, &id->p_encoder->fmt_in,
638 &id->p_encoder->fmt_in );
639 filter_chain_AppendFromString( id->p_uf_chain, p_sys->psz_vf2 );
640 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
641 es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
642 id->p_encoder->fmt_out.video.i_width =
643 id->p_encoder->fmt_in.video.i_width;
644 id->p_encoder->fmt_out.video.i_height =
645 id->p_encoder->fmt_in.video.i_height;
646 id->p_encoder->fmt_out.video.i_sar_num =
647 id->p_encoder->fmt_in.video.i_sar_num;
648 id->p_encoder->fmt_out.video.i_sar_den =
649 id->p_encoder->fmt_in.video.i_sar_den;
652 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
654 picture_Release( p_pic );
655 transcode_video_close( p_stream, id );
656 id->b_transcode = false;
661 /* Run filter chain */
663 p_pic = filter_chain_VideoFilter( id->p_f_chain, p_pic );
669 /* Check if we have a subpicture to overlay */
672 p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date, false );
673 /* TODO: get another pic */
676 /* Overlay subpicture */
681 if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
683 /* We can't modify the picture, we need to duplicate it */
684 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
687 picture_Copy( p_tmp, p_pic );
688 picture_Release( p_pic );
693 if( filter_chain_GetLength( id->p_f_chain ) > 0 )
694 fmt = filter_chain_GetFmtOut( id->p_f_chain )->video;
696 fmt = id->p_decoder->fmt_out.video;
698 /* FIXME the mdate() seems highly suspicious */
699 spu_RenderSubpictures( p_sys->p_spu, p_pic, &fmt,
700 p_subpic, &id->p_decoder->fmt_out.video, mdate() );
703 /* Run user specified filter chain */
705 p_pic = filter_chain_VideoFilter( id->p_uf_chain, p_pic );
707 if( p_sys->i_threads == 0 )
711 video_timer_start( id->p_encoder );
712 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
713 video_timer_stop( id->p_encoder );
715 block_ChainAppend( out, p_block );
718 if( p_sys->b_master_sync )
720 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
721 if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
722 || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
724 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
725 date_Set( &id->interpolated_pts, p_pic->date );
726 i_pts = p_pic->date + 1;
728 date_Increment( &id->interpolated_pts, 1 );
731 if( p_sys->b_master_sync && i_duplicate > 1 )
733 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
734 if( (p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT)
735 || ((p_pic->date - i_pts) < -MASTER_SYNC_MAX_DRIFT) )
737 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
738 date_Set( &id->interpolated_pts, p_pic->date );
739 i_pts = p_pic->date + 1;
741 date_Increment( &id->interpolated_pts, 1 );
743 if( p_sys->i_threads >= 1 )
745 /* We can't modify the picture, we need to duplicate it */
746 p_pic2 = video_new_buffer_decoder( id->p_decoder );
749 picture_Copy( p_pic2, p_pic );
750 p_pic2->date = i_pts;
757 video_timer_start( id->p_encoder );
758 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
759 video_timer_stop( id->p_encoder );
760 block_ChainAppend( out, p_block );
764 if( p_sys->i_threads == 0 )
766 picture_Release( p_pic );
770 vlc_mutex_lock( &p_sys->lock_out );
771 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
772 p_sys->i_last_pic %= PICTURE_RING_SIZE;
773 *out = p_sys->p_buffers;
774 p_sys->p_buffers = NULL;
777 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic2;
778 p_sys->i_last_pic %= PICTURE_RING_SIZE;
780 vlc_cond_signal( &p_sys->cond );
781 vlc_mutex_unlock( &p_sys->lock_out );
788 bool transcode_video_add( sout_stream_t *p_stream, es_format_t *p_fmt,
789 sout_stream_id_t *id )
791 sout_stream_sys_t *p_sys = p_stream->p_sys;
794 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
795 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
797 /* Complete destination format */
798 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
799 id->p_encoder->fmt_out.video.i_width = p_sys->i_width & ~1;
800 id->p_encoder->fmt_out.video.i_height = p_sys->i_height & ~1;
801 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
803 /* Build decoder -> filter -> encoder chain */
804 if( transcode_video_new( p_stream, id ) )
806 msg_Err( p_stream, "cannot create video chain" );
810 /* Stream will be added later on because we don't know
811 * all the characteristics of the decoded stream yet */
812 id->b_transcode = true;
814 if( p_sys->f_fps > 0 )
816 id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->f_fps * 1000) + 0.5;
817 id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;