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"
35 #include <vlc_modules.h>
37 #define ENC_FRAMERATE (25 * 1001 + .5)
38 #define ENC_FRAMERATE_BASE 1001
40 struct decoder_owner_sys_t
42 sout_stream_sys_t *p_sys;
45 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
47 VLC_UNUSED(p_decoder);
48 picture_Release( p_pic );
51 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
54 picture_Hold( p_pic );
57 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
60 picture_Release( p_pic );
63 static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
65 p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
66 return picture_NewFromFormat( &p_dec->fmt_out.video );
69 static picture_t *video_new_buffer_encoder( encoder_t *p_enc )
71 p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
72 return picture_NewFromFormat( &p_enc->fmt_in.video );
75 static picture_t *transcode_video_filter_buffer_new( filter_t *p_filter )
77 p_filter->fmt_out.video.i_chroma = p_filter->fmt_out.i_codec;
78 return picture_NewFromFormat( &p_filter->fmt_out.video );
80 static void transcode_video_filter_buffer_del( filter_t *p_filter, picture_t *p_pic )
83 picture_Release( p_pic );
86 static int transcode_video_filter_allocation_init( filter_t *p_filter,
90 p_filter->pf_video_buffer_new = transcode_video_filter_buffer_new;
91 p_filter->pf_video_buffer_del = transcode_video_filter_buffer_del;
95 static void transcode_video_filter_allocation_clear( filter_t *p_filter )
100 static void* EncoderThread( void *obj )
102 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)obj;
103 sout_stream_id_t *id = p_sys->id_video;
105 int canc = vlc_savecancel ();
111 vlc_mutex_lock( &p_sys->lock_out );
112 while( !p_sys->b_abort &&
113 (p_pic = picture_fifo_Pop( p_sys->pp_pics )) == NULL )
114 vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
118 vlc_mutex_unlock( &p_sys->lock_out );
121 vlc_mutex_unlock( &p_sys->lock_out );
123 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
125 vlc_mutex_lock( &p_sys->lock_out );
126 block_ChainAppend( &p_sys->p_buffers, p_block );
128 vlc_mutex_unlock( &p_sys->lock_out );
129 picture_Release( p_pic );
132 block_ChainRelease( p_sys->p_buffers );
134 vlc_restorecancel (canc);
138 int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
140 sout_stream_sys_t *p_sys = p_stream->p_sys;
143 * Initialization of decoder structures
145 id->p_decoder->fmt_out = id->p_decoder->fmt_in;
146 id->p_decoder->fmt_out.i_extra = 0;
147 id->p_decoder->fmt_out.p_extra = 0;
148 id->p_decoder->pf_decode_video = NULL;
149 id->p_decoder->pf_get_cc = NULL;
150 id->p_decoder->pf_get_cc = 0;
151 id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
152 id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
153 id->p_decoder->pf_picture_link = video_link_picture_decoder;
154 id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
155 id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
156 if( !id->p_decoder->p_owner )
159 id->p_decoder->p_owner->p_sys = p_sys;
160 /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
162 id->p_decoder->p_module =
163 module_need( id->p_decoder, "decoder", "$codec", false );
165 if( !id->p_decoder->p_module )
167 msg_Err( p_stream, "cannot find video decoder" );
168 free( id->p_decoder->p_owner );
174 * Because some info about the decoded input will only be available
175 * once the first frame is decoded, we actually only test the availability
176 * of the encoder here.
179 /* Initialization of encoder format structures */
180 es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
181 id->p_decoder->fmt_out.i_codec );
182 id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
184 /* The dimensions will be set properly later on.
185 * Just put sensible values so we can test an encoder is available. */
186 id->p_encoder->fmt_in.video.i_width =
187 id->p_encoder->fmt_out.video.i_width
188 ? id->p_encoder->fmt_out.video.i_width
189 : id->p_decoder->fmt_in.video.i_width
190 ? id->p_decoder->fmt_in.video.i_width : 16;
191 id->p_encoder->fmt_in.video.i_height =
192 id->p_encoder->fmt_out.video.i_height
193 ? id->p_encoder->fmt_out.video.i_height
194 : id->p_decoder->fmt_in.video.i_height
195 ? id->p_decoder->fmt_in.video.i_height : 16;
196 id->p_encoder->fmt_in.video.i_frame_rate = ENC_FRAMERATE;
197 id->p_encoder->fmt_in.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
199 id->p_encoder->i_threads = p_sys->i_threads;
200 id->p_encoder->p_cfg = p_sys->p_video_cfg;
202 id->p_encoder->p_module =
203 module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
204 if( !id->p_encoder->p_module )
206 msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s). Take a look few lines earlier to see possible reason.",
207 p_sys->psz_venc ? p_sys->psz_venc : "any",
208 (char *)&p_sys->i_vcodec );
209 module_unneed( id->p_decoder, id->p_decoder->p_module );
210 id->p_decoder->p_module = 0;
211 free( id->p_decoder->p_owner );
215 /* Close the encoder.
216 * We'll open it only when we have the first frame. */
217 module_unneed( id->p_encoder, id->p_encoder->p_module );
218 if( id->p_encoder->fmt_out.p_extra )
220 free( id->p_encoder->fmt_out.p_extra );
221 id->p_encoder->fmt_out.p_extra = NULL;
222 id->p_encoder->fmt_out.i_extra = 0;
224 id->p_encoder->p_module = NULL;
226 if( p_sys->i_threads >= 1 )
228 int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
229 VLC_THREAD_PRIORITY_VIDEO;
230 p_sys->id_video = id;
231 vlc_mutex_init( &p_sys->lock_out );
232 vlc_cond_init( &p_sys->cond );
233 p_sys->pp_pics = picture_fifo_New();
234 if( p_sys->pp_pics == NULL )
236 msg_Err( p_stream, "cannot create picture fifo" );
237 vlc_mutex_destroy( &p_sys->lock_out );
238 vlc_cond_destroy( &p_sys->cond );
239 module_unneed( id->p_decoder, id->p_decoder->p_module );
240 id->p_decoder->p_module = NULL;
241 free( id->p_decoder->p_owner );
244 p_sys->p_buffers = NULL;
245 p_sys->b_abort = false;
246 if( vlc_clone( &p_sys->thread, EncoderThread, p_sys, i_priority ) )
248 msg_Err( p_stream, "cannot spawn encoder thread" );
249 vlc_mutex_destroy( &p_sys->lock_out );
250 vlc_cond_destroy( &p_sys->cond );
251 picture_fifo_Delete( p_sys->pp_pics );
252 module_unneed( id->p_decoder, id->p_decoder->p_module );
253 id->p_decoder->p_module = NULL;
254 free( id->p_decoder->p_owner );
261 static void transcode_video_filter_init( sout_stream_t *p_stream,
262 sout_stream_id_t *id )
264 const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
266 id->p_f_chain = filter_chain_New( p_stream, "video filter2",
268 transcode_video_filter_allocation_init,
269 transcode_video_filter_allocation_clear,
271 filter_chain_Reset( id->p_f_chain, p_fmt_out, p_fmt_out );
274 if( p_stream->p_sys->b_deinterlace )
276 filter_chain_AppendFilter( id->p_f_chain,
277 p_stream->p_sys->psz_deinterlace,
278 p_stream->p_sys->p_deinterlace_cfg,
279 &id->p_decoder->fmt_out,
280 &id->p_decoder->fmt_out );
282 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
285 if( p_stream->p_sys->psz_vf2 )
287 const es_format_t *p_fmt_out;
288 id->p_uf_chain = filter_chain_New( p_stream, "video filter2",
290 transcode_video_filter_allocation_init,
291 transcode_video_filter_allocation_clear,
293 filter_chain_Reset( id->p_uf_chain, &id->p_encoder->fmt_in,
294 &id->p_encoder->fmt_in );
295 filter_chain_AppendFromString( id->p_uf_chain, p_stream->p_sys->psz_vf2 );
296 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
297 es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
298 id->p_encoder->fmt_out.video.i_width =
299 id->p_encoder->fmt_in.video.i_width;
300 id->p_encoder->fmt_out.video.i_height =
301 id->p_encoder->fmt_in.video.i_height;
302 id->p_encoder->fmt_out.video.i_sar_num =
303 id->p_encoder->fmt_in.video.i_sar_num;
304 id->p_encoder->fmt_out.video.i_sar_den =
305 id->p_encoder->fmt_in.video.i_sar_den;
310 /* Take care of the scaling and chroma conversions.
312 * XXX: Shouldn't this really be after p_uf_chain, not p_f_chain,
313 * in case p_uf_chain changes the format?
315 static void conversion_video_filter_append( sout_stream_id_t *id )
317 const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
319 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
321 if( ( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma ) ||
322 ( p_fmt_out->video.i_width != id->p_encoder->fmt_in.video.i_width ) ||
323 ( p_fmt_out->video.i_height != id->p_encoder->fmt_in.video.i_height ) )
325 filter_chain_AppendFilter( id->p_f_chain,
328 &id->p_encoder->fmt_in );
332 static void transcode_video_encoder_init( sout_stream_t *p_stream,
333 sout_stream_id_t *id )
335 sout_stream_sys_t *p_sys = p_stream->p_sys;
337 const es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
338 if( id->p_f_chain ) {
339 p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
341 if( id->p_uf_chain ) {
342 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
346 * width/height of source */
347 int i_src_width = p_fmt_out->video.i_width;
348 int i_src_height = p_fmt_out->video.i_height;
350 /* with/height scaling */
351 float f_scale_width = 1;
352 float f_scale_height = 1;
354 /* width/height of output stream */
359 float f_aspect = (double)p_fmt_out->video.i_sar_num *
360 p_fmt_out->video.i_width /
361 p_fmt_out->video.i_sar_den /
362 p_fmt_out->video.i_height;
364 msg_Dbg( p_stream, "decoder aspect is %f:1", f_aspect );
366 /* Change f_aspect from source frame to source pixel */
367 f_aspect = f_aspect * i_src_height / i_src_width;
368 msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );
370 /* Calculate scaling factor for specified parameters */
371 if( id->p_encoder->fmt_out.video.i_width <= 0 &&
372 id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
374 /* Global scaling. Make sure width will remain a factor of 16 */
377 int i_new_width = i_src_width * p_sys->f_scale;
379 if( i_new_width % 16 <= 7 && i_new_width >= 16 )
380 i_new_width -= i_new_width % 16;
382 i_new_width += 16 - i_new_width % 16;
384 f_real_scale = (float)( i_new_width ) / (float) i_src_width;
386 i_new_height = __MAX( 16, i_src_height * (float)f_real_scale );
388 f_scale_width = f_real_scale;
389 f_scale_height = (float) i_new_height / (float) i_src_height;
391 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
392 id->p_encoder->fmt_out.video.i_height <= 0 )
394 /* Only width specified */
395 f_scale_width = (float)id->p_encoder->fmt_out.video.i_width/i_src_width;
396 f_scale_height = f_scale_width;
398 else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
399 id->p_encoder->fmt_out.video.i_height > 0 )
401 /* Only height specified */
402 f_scale_height = (float)id->p_encoder->fmt_out.video.i_height/i_src_height;
403 f_scale_width = f_scale_height;
405 else if( id->p_encoder->fmt_out.video.i_width > 0 &&
406 id->p_encoder->fmt_out.video.i_height > 0 )
408 /* Width and height specified */
409 f_scale_width = (float)id->p_encoder->fmt_out.video.i_width/i_src_width;
410 f_scale_height = (float)id->p_encoder->fmt_out.video.i_height/i_src_height;
413 /* check maxwidth and maxheight */
414 if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
417 f_scale_width = (float)p_sys->i_maxwidth / i_src_width;
420 if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
423 f_scale_height = (float)p_sys->i_maxheight / i_src_height;
427 /* Change aspect ratio from source pixel to scaled pixel */
428 f_aspect = f_aspect * f_scale_height / f_scale_width;
429 msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );
431 /* f_scale_width and f_scale_height are now final */
432 /* Calculate width, height from scaling
433 * Make sure its multiple of 2
435 i_dst_width = 2 * (int)(f_scale_width*i_src_width/2+0.5);
436 i_dst_height = 2 * (int)(f_scale_height*i_src_height/2+0.5);
438 /* Change aspect ratio from scaled pixel to output frame */
439 f_aspect = f_aspect * i_dst_width / i_dst_height;
441 /* Store calculated values */
442 id->p_encoder->fmt_out.video.i_width =
443 id->p_encoder->fmt_out.video.i_visible_width = i_dst_width;
444 id->p_encoder->fmt_out.video.i_height =
445 id->p_encoder->fmt_out.video.i_visible_height = i_dst_height;
447 id->p_encoder->fmt_in.video.i_width =
448 id->p_encoder->fmt_in.video.i_visible_width = i_dst_width;
449 id->p_encoder->fmt_in.video.i_height =
450 id->p_encoder->fmt_in.video.i_visible_height = i_dst_height;
452 msg_Dbg( p_stream, "source %ix%i, destination %ix%i",
453 i_src_width, i_src_height,
454 i_dst_width, i_dst_height
457 /* Handle frame rate conversion */
458 if( !id->p_encoder->fmt_out.video.i_frame_rate ||
459 !id->p_encoder->fmt_out.video.i_frame_rate_base )
461 if( p_fmt_out->video.i_frame_rate &&
462 p_fmt_out->video.i_frame_rate_base )
464 id->p_encoder->fmt_out.video.i_frame_rate =
465 p_fmt_out->video.i_frame_rate;
466 id->p_encoder->fmt_out.video.i_frame_rate_base =
467 p_fmt_out->video.i_frame_rate_base;
471 /* Pick a sensible default value */
472 id->p_encoder->fmt_out.video.i_frame_rate = ENC_FRAMERATE;
473 id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
477 id->p_encoder->fmt_in.video.i_frame_rate =
478 id->p_encoder->fmt_out.video.i_frame_rate;
479 id->p_encoder->fmt_in.video.i_frame_rate_base =
480 id->p_encoder->fmt_out.video.i_frame_rate_base;
482 date_Init( &id->interpolated_pts,
483 id->p_encoder->fmt_out.video.i_frame_rate,
484 id->p_encoder->fmt_out.video.i_frame_rate_base );
486 /* Check whether a particular aspect ratio was requested */
487 if( id->p_encoder->fmt_out.video.i_sar_num <= 0 ||
488 id->p_encoder->fmt_out.video.i_sar_den <= 0 )
490 vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
491 &id->p_encoder->fmt_out.video.i_sar_den,
492 (uint64_t)p_fmt_out->video.i_sar_num * i_src_width * i_dst_height,
493 (uint64_t)p_fmt_out->video.i_sar_den * i_src_height * i_dst_width,
498 vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
499 &id->p_encoder->fmt_out.video.i_sar_den,
500 id->p_encoder->fmt_out.video.i_sar_num,
501 id->p_encoder->fmt_out.video.i_sar_den,
505 id->p_encoder->fmt_in.video.i_sar_num =
506 id->p_encoder->fmt_out.video.i_sar_num;
507 id->p_encoder->fmt_in.video.i_sar_den =
508 id->p_encoder->fmt_out.video.i_sar_den;
510 msg_Dbg( p_stream, "encoder aspect is %i:%i",
511 id->p_encoder->fmt_out.video.i_sar_num * id->p_encoder->fmt_out.video.i_width,
512 id->p_encoder->fmt_out.video.i_sar_den * id->p_encoder->fmt_out.video.i_height );
514 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
517 static int transcode_video_encoder_open( sout_stream_t *p_stream,
518 sout_stream_id_t *id )
520 sout_stream_sys_t *p_sys = p_stream->p_sys;
523 msg_Dbg( p_stream, "destination (after video filters) %ix%i",
524 id->p_encoder->fmt_in.video.i_width,
525 id->p_encoder->fmt_in.video.i_height );
527 id->p_encoder->p_module =
528 module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
529 if( !id->p_encoder->p_module )
531 msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
532 p_sys->psz_venc ? p_sys->psz_venc : "any",
533 (char *)&p_sys->i_vcodec );
537 id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
540 id->p_encoder->fmt_out.i_codec =
541 vlc_fourcc_GetCodec( VIDEO_ES, id->p_encoder->fmt_out.i_codec );
543 id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
546 msg_Err( p_stream, "cannot add this stream" );
553 void transcode_video_close( sout_stream_t *p_stream,
554 sout_stream_id_t *id )
556 if( p_stream->p_sys->i_threads >= 1 )
558 vlc_mutex_lock( &p_stream->p_sys->lock_out );
559 p_stream->p_sys->b_abort = true;
560 vlc_cond_signal( &p_stream->p_sys->cond );
561 vlc_mutex_unlock( &p_stream->p_sys->lock_out );
563 vlc_join( p_stream->p_sys->thread, NULL );
564 vlc_mutex_destroy( &p_stream->p_sys->lock_out );
565 vlc_cond_destroy( &p_stream->p_sys->cond );
567 picture_fifo_Delete( p_stream->p_sys->pp_pics );
568 p_stream->p_sys->pp_pics = NULL;
572 if( id->p_decoder->p_module )
573 module_unneed( id->p_decoder, id->p_decoder->p_module );
574 if( id->p_decoder->p_description )
575 vlc_meta_Delete( id->p_decoder->p_description );
577 free( id->p_decoder->p_owner );
580 if( id->p_encoder->p_module )
581 module_unneed( id->p_encoder, id->p_encoder->p_module );
585 filter_chain_Delete( id->p_f_chain );
587 filter_chain_Delete( id->p_uf_chain );
590 static void OutputFrame( sout_stream_sys_t *p_sys, picture_t *p_pic, bool b_need_duplicate, sout_stream_t *p_stream, sout_stream_id_t *id, block_t **out )
592 picture_t *p_pic2 = NULL;
598 /* Check if we have a subpicture to overlay */
601 video_format_t fmt = id->p_encoder->fmt_in.video;
602 if( fmt.i_visible_width <= 0 || fmt.i_visible_height <= 0 )
604 fmt.i_visible_width = fmt.i_width;
605 fmt.i_visible_height = fmt.i_height;
610 subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt, &fmt,
611 p_pic->date, p_pic->date, false );
613 /* Overlay subpicture */
616 if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
618 /* We can't modify the picture, we need to duplicate it,
619 * in this point the picture is already p_encoder->fmt.in format*/
620 picture_t *p_tmp = video_new_buffer_encoder( id->p_encoder );
621 if( likely( p_tmp ) )
623 picture_Copy( p_tmp, p_pic );
624 picture_Release( p_pic );
628 if( unlikely( !p_sys->p_spu_blend ) )
629 p_sys->p_spu_blend = filter_NewBlend( VLC_OBJECT( p_sys->p_spu ), &fmt );
630 if( likely( p_sys->p_spu_blend ) )
631 picture_BlendSubpicture( p_pic, p_sys->p_spu_blend, p_subpic );
632 subpicture_Delete( p_subpic );
636 if( p_sys->i_threads == 0 )
640 p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
641 block_ChainAppend( out, p_block );
644 if( p_sys->b_master_sync )
646 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
647 mtime_t i_video_drift = p_pic->date - i_pts;
648 if (unlikely ( i_video_drift > MASTER_SYNC_MAX_DRIFT
649 || i_video_drift < -MASTER_SYNC_MAX_DRIFT ) )
652 "drift is too high (%"PRId64"), resetting master sync",
654 date_Set( &id->interpolated_pts, p_pic->date );
655 i_pts = p_pic->date + 1;
657 date_Increment( &id->interpolated_pts, 1 );
659 if( unlikely( b_need_duplicate ) )
662 if( p_sys->i_threads >= 1 )
664 /* We can't modify the picture, we need to duplicate it */
665 p_pic2 = video_new_buffer_encoder( id->p_encoder );
666 if( likely( p_pic2 != NULL ) )
668 picture_Copy( p_pic2, p_pic );
669 p_pic2->date = i_pts;
676 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
677 block_ChainAppend( out, p_block );
682 if( p_sys->i_threads == 0 )
684 picture_Release( p_pic );
688 vlc_mutex_lock( &p_sys->lock_out );
689 picture_fifo_Push( p_sys->pp_pics, p_pic );
692 picture_fifo_Push( p_sys->pp_pics, p_pic2 );
694 vlc_cond_signal( &p_sys->cond );
695 vlc_mutex_unlock( &p_sys->lock_out );
699 int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
700 block_t *in, block_t **out )
702 sout_stream_sys_t *p_sys = p_stream->p_sys;
703 bool b_need_duplicate = false;
707 if( unlikely( in == NULL ) )
709 if( p_sys->i_threads == 0 )
713 p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
714 block_ChainAppend( out, p_block );
720 * FIXME: we need EncoderThread() to flush buffers and signal us
721 * when it's done so we can send the last frames to the chain
728 while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
731 if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
733 mtime_t current_date = mdate();
734 if( unlikely( current_date + 50000 > p_pic->date ) )
736 msg_Dbg( p_stream, "late picture skipped (%"PRId64")",
737 current_date + 50000 - p_pic->date );
738 picture_Release( p_pic );
743 if( p_sys->b_master_sync )
745 mtime_t i_master_drift = p_sys->i_master_drift;
746 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
747 mtime_t i_video_drift = p_pic->date - i_pts;
749 if ( unlikely( i_video_drift > MASTER_SYNC_MAX_DRIFT
750 || i_video_drift < -MASTER_SYNC_MAX_DRIFT ) )
753 "drift is too high (%"PRId64", resetting master sync",
755 date_Set( &id->interpolated_pts, p_pic->date );
756 i_pts = p_pic->date + 1;
758 i_video_drift = p_pic->date - i_pts;
759 b_need_duplicate = false;
761 /* Set the pts of the frame being encoded */
764 if( unlikely( i_video_drift < (i_master_drift - 50000) ) )
767 msg_Dbg( p_stream, "dropping frame (%i)",
768 (int)(i_video_drift - i_master_drift) );
770 picture_Release( p_pic );
773 else if( unlikely( i_video_drift > (i_master_drift + 50000) ) )
776 msg_Dbg( p_stream, "adding frame (%i)",
777 (int)(i_video_drift - i_master_drift) );
779 b_need_duplicate = true;
783 id->p_encoder->p_module &&
784 !video_format_IsSimilar( &p_sys->fmt_input_video, &id->p_decoder->fmt_out.video )
788 msg_Info( p_stream, "aspect-ratio changed, reiniting. %i -> %i : %i -> %i.",
789 p_sys->fmt_input_video.i_sar_num, id->p_decoder->fmt_out.video.i_sar_num,
790 p_sys->fmt_input_video.i_sar_den, id->p_decoder->fmt_out.video.i_sar_den
794 filter_chain_Delete( id->p_f_chain );
795 id->p_f_chain = NULL;
797 filter_chain_Delete( id->p_uf_chain );
798 id->p_uf_chain = NULL;
800 /* Reinitialize filters */
801 id->p_encoder->fmt_out.video.i_width = p_sys->i_width & ~1;
802 id->p_encoder->fmt_out.video.i_height = p_sys->i_height & ~1;
803 id->p_encoder->fmt_out.video.i_sar_num = id->p_encoder->fmt_out.video.i_sar_den = 0;
805 transcode_video_filter_init( p_stream, id );
806 transcode_video_encoder_init( p_stream, id );
807 conversion_video_filter_append( id );
808 memcpy( &p_sys->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
812 if( unlikely( !id->p_encoder->p_module ) )
815 filter_chain_Delete( id->p_f_chain );
817 filter_chain_Delete( id->p_uf_chain );
818 id->p_f_chain = id->p_uf_chain = NULL;
820 transcode_video_filter_init( p_stream, id );
821 transcode_video_encoder_init( p_stream, id );
822 conversion_video_filter_append( id );
823 memcpy( &p_sys->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
825 if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
827 picture_Release( p_pic );
828 transcode_video_close( p_stream, id );
829 id->b_transcode = false;
834 /* Run the filter and output chains; first with the picture,
835 * and then with NULL as many times as we need until they
836 * stop outputting frames.
839 picture_t *p_filtered_pic = p_pic;
841 /* Run filter chain */
843 p_filtered_pic = filter_chain_VideoFilter( id->p_f_chain, p_filtered_pic );
844 if( !p_filtered_pic )
848 picture_t *p_user_filtered_pic = p_filtered_pic;
850 /* Run user specified filter chain */
852 p_user_filtered_pic = filter_chain_VideoFilter( id->p_uf_chain, p_user_filtered_pic );
853 if( !p_user_filtered_pic )
856 OutputFrame( p_sys, p_user_filtered_pic, b_need_duplicate, p_stream, id, out );
857 b_need_duplicate = false;
859 p_filtered_pic = NULL;
866 if( p_sys->i_threads >= 1 )
868 /* Pick up any return data the encoder thread wants to output. */
869 vlc_mutex_lock( &p_sys->lock_out );
870 *out = p_sys->p_buffers;
871 p_sys->p_buffers = NULL;
872 vlc_mutex_unlock( &p_sys->lock_out );
878 bool transcode_video_add( sout_stream_t *p_stream, es_format_t *p_fmt,
879 sout_stream_id_t *id )
881 sout_stream_sys_t *p_sys = p_stream->p_sys;
884 "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
885 (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
887 /* Complete destination format */
888 id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
889 id->p_encoder->fmt_out.video.i_width = p_sys->i_width & ~1;
890 id->p_encoder->fmt_out.video.i_height = p_sys->i_height & ~1;
891 id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
893 /* Build decoder -> filter -> encoder chain */
894 if( transcode_video_new( p_stream, id ) )
896 msg_Err( p_stream, "cannot create video chain" );
900 /* Stream will be added later on because we don't know
901 * all the characteristics of the decoded stream yet */
902 id->b_transcode = true;
904 if( p_sys->f_fps > 0 )
906 id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->f_fps * ENC_FRAMERATE_BASE) + 0.5;
907 id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;