#include "transcode.h"
+#include <math.h>
#include <vlc_meta.h>
#include <vlc_spu.h>
#include <vlc_modules.h>
sout_stream_sys_t *p_sys;
};
-static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
+static int video_update_format_decoder( decoder_t *p_dec )
{
- VLC_UNUSED(p_decoder);
- picture_Release( p_pic );
-}
-
-static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
-{
- VLC_UNUSED(p_dec);
- picture_Hold( p_pic );
-}
-
-static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
-{
- VLC_UNUSED(p_dec);
- picture_Release( p_pic );
+ p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
+ return 0;
}
static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
{
- p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
return picture_NewFromFormat( &p_dec->fmt_out.video );
}
p_filter->fmt_out.video.i_chroma = p_filter->fmt_out.i_codec;
return picture_NewFromFormat( &p_filter->fmt_out.video );
}
-static void transcode_video_filter_buffer_del( filter_t *p_filter, picture_t *p_pic )
-{
- VLC_UNUSED(p_filter);
- picture_Release( p_pic );
-}
-
-static int transcode_video_filter_allocation_init( filter_t *p_filter,
- void *p_data )
-{
- VLC_UNUSED(p_data);
- p_filter->pf_video_buffer_new = transcode_video_filter_buffer_new;
- p_filter->pf_video_buffer_del = transcode_video_filter_buffer_del;
- return VLC_SUCCESS;
-}
-
-static void transcode_video_filter_allocation_clear( filter_t *p_filter )
-{
- VLC_UNUSED(p_filter);
-}
static void* EncoderThread( void *obj )
{
int canc = vlc_savecancel ();
block_t *p_block = NULL;
+ vlc_mutex_lock( &p_sys->lock_out );
+
for( ;; )
{
-
- vlc_mutex_lock( &p_sys->lock_out );
while( !p_sys->b_abort &&
(p_pic = picture_fifo_Pop( p_sys->pp_pics )) == NULL )
vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
- if( p_sys->b_abort && !p_pic )
- {
- vlc_mutex_unlock( &p_sys->lock_out );
- break;
- }
- vlc_mutex_unlock( &p_sys->lock_out );
-
if( p_pic )
{
+ /* release lock while encoding */
+ vlc_mutex_unlock( &p_sys->lock_out );
p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
-
+ picture_Release( p_pic );
vlc_mutex_lock( &p_sys->lock_out );
- block_ChainAppend( &p_sys->p_buffers, p_block );
- vlc_mutex_unlock( &p_sys->lock_out );
- picture_Release( p_pic );
+ block_ChainAppend( &p_sys->p_buffers, p_block );
}
- vlc_mutex_lock( &p_sys->lock_out );
if( p_sys->b_abort )
- {
- vlc_mutex_unlock( &p_sys->lock_out );
break;
- }
- vlc_mutex_unlock( &p_sys->lock_out );
}
/*Encode what we have in the buffer on closing*/
- vlc_mutex_lock( &p_sys->lock_out );
while( (p_pic = picture_fifo_Pop( p_sys->pp_pics )) != NULL )
{
p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
-
- block_ChainAppend( &p_sys->p_buffers, p_block );
-
picture_Release( p_pic );
+ block_ChainAppend( &p_sys->p_buffers, p_block );
}
/*Now flush encoder*/
do {
- p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
- block_ChainAppend( &p_sys->p_buffers, p_block );
+ p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
+ block_ChainAppend( &p_sys->p_buffers, p_block );
} while( p_block );
vlc_mutex_unlock( &p_sys->lock_out );
-
vlc_restorecancel (canc);
+
return NULL;
}
id->p_decoder->pf_decode_video = NULL;
id->p_decoder->pf_get_cc = NULL;
id->p_decoder->pf_get_cc = 0;
+ id->p_decoder->pf_vout_format_update = video_update_format_decoder;
id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
- id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
- id->p_decoder->pf_picture_link = video_link_picture_decoder;
- id->p_decoder->pf_picture_unlink = video_unlink_picture_decoder;
id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
if( !id->p_decoder->p_owner )
return VLC_EGENERIC;
}
id->p_encoder->p_module = NULL;
- if( p_sys->i_threads >= 1 )
+ if( p_sys->i_threads <= 0 )
+ return VLC_SUCCESS;
+
+ int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
+ VLC_THREAD_PRIORITY_VIDEO;
+ p_sys->id_video = id;
+ p_sys->pp_pics = picture_fifo_New();
+ if( p_sys->pp_pics == NULL )
{
- int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
- VLC_THREAD_PRIORITY_VIDEO;
- p_sys->id_video = id;
- vlc_mutex_init( &p_sys->lock_out );
- vlc_cond_init( &p_sys->cond );
- p_sys->pp_pics = picture_fifo_New();
- if( p_sys->pp_pics == NULL )
- {
- msg_Err( p_stream, "cannot create picture fifo" );
- vlc_mutex_destroy( &p_sys->lock_out );
- vlc_cond_destroy( &p_sys->cond );
- module_unneed( id->p_decoder, id->p_decoder->p_module );
- id->p_decoder->p_module = NULL;
- free( id->p_decoder->p_owner );
- return VLC_ENOMEM;
- }
- p_sys->p_buffers = NULL;
- p_sys->b_abort = false;
- if( vlc_clone( &p_sys->thread, EncoderThread, p_sys, i_priority ) )
- {
- msg_Err( p_stream, "cannot spawn encoder thread" );
- vlc_mutex_destroy( &p_sys->lock_out );
- vlc_cond_destroy( &p_sys->cond );
- picture_fifo_Delete( p_sys->pp_pics );
- module_unneed( id->p_decoder, id->p_decoder->p_module );
- id->p_decoder->p_module = NULL;
- free( id->p_decoder->p_owner );
- return VLC_EGENERIC;
- }
+ msg_Err( p_stream, "cannot create picture fifo" );
+ module_unneed( id->p_decoder, id->p_decoder->p_module );
+ id->p_decoder->p_module = NULL;
+ free( id->p_decoder->p_owner );
+ return VLC_ENOMEM;
+ }
+ vlc_mutex_init( &p_sys->lock_out );
+ vlc_cond_init( &p_sys->cond );
+ p_sys->p_buffers = NULL;
+ p_sys->b_abort = false;
+ if( vlc_clone( &p_sys->thread, EncoderThread, p_sys, i_priority ) )
+ {
+ msg_Err( p_stream, "cannot spawn encoder thread" );
+ vlc_mutex_destroy( &p_sys->lock_out );
+ vlc_cond_destroy( &p_sys->cond );
+ picture_fifo_Delete( p_sys->pp_pics );
+ module_unneed( id->p_decoder, id->p_decoder->p_module );
+ id->p_decoder->p_module = NULL;
+ free( id->p_decoder->p_owner );
+ return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
+static filter_t *AppendDeinterlaceFilter( sout_stream_t *p_stream,
+ filter_chain_t *p_chain,
+ const char *psz_name,
+ config_chain_t *p_cfg,
+ const es_format_t *p_fmt_in,
+ const es_format_t *p_fmt_out )
+{
+ filter_t *p_filter =
+ filter_chain_AppendFilter( p_chain, psz_name, p_cfg, p_fmt_in, p_fmt_out );
+ if( p_filter )
+ return p_filter;
+
+ msg_Dbg( p_stream, "deinterlace init failed, trying colorspace conversion" );
+
+ /* Most likely the filter failed due to wrong (e.g. non-planar) input format.
+ * Try converting to a different color space and try again.
+ *
+ * We use the recommended fallback formats in order, to get a conversion that's
+ * as cheap (both CPU-wise and quality-wise) as possible.
+ */
+ for( const vlc_fourcc_t *fourcc = vlc_fourcc_GetYUVFallback( p_fmt_out->video.i_chroma );
+ *fourcc != 0;
+ ++fourcc )
+ {
+ msg_Dbg( p_stream, "trying deinterlace through colorspace conversion to %4.4s", (char *)fourcc );
+
+ es_format_t p_new_fmt;
+ es_format_Copy( &p_new_fmt, p_fmt_in );
+ p_new_fmt.video.i_chroma = *fourcc; // XXX: is this enough?
+
+ filter_chain_Reset( p_chain, NULL, NULL );
+
+ p_filter = filter_chain_AppendFilter( p_chain,
+ NULL, NULL,
+ p_fmt_in,
+ &p_new_fmt );
+ if( !p_filter )
+ continue;
+
+ p_filter = filter_chain_AppendFilter( p_chain,
+ psz_name,
+ p_cfg,
+ &p_new_fmt,
+ &p_new_fmt );
+ if( p_filter )
+ {
+ msg_Dbg( p_stream, "deinterlace through colorspace conversion to %4.4s succeeded", (char *)fourcc );
+ return p_filter;
+ }
+ }
+
+ msg_Err( p_stream, "deinterlace init failed, even with colorspace conversion" );
+ return NULL;
+}
+
static void transcode_video_filter_init( sout_stream_t *p_stream,
sout_stream_id_sys_t *id )
{
+ filter_owner_t owner = {
+ .sys = p_stream->p_sys,
+ .video = {
+ .buffer_new = transcode_video_filter_buffer_new,
+ },
+ };
es_format_t *p_fmt_out = &id->p_decoder->fmt_out;
- id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
- id->p_f_chain = filter_chain_New( p_stream, "video filter2",
- false,
- transcode_video_filter_allocation_init,
- transcode_video_filter_allocation_clear,
- p_stream->p_sys );
+ id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
+ id->p_f_chain = filter_chain_NewVideo( p_stream, false, &owner );
filter_chain_Reset( id->p_f_chain, p_fmt_out, p_fmt_out );
/* Deinterlace */
if( p_stream->p_sys->b_deinterlace )
+ {
+ filter_t *p_filter =
+ AppendDeinterlaceFilter( p_stream,
+ id->p_f_chain,
+ p_stream->p_sys->psz_deinterlace,
+ p_stream->p_sys->p_deinterlace_cfg,
+ p_fmt_out,
+ p_fmt_out );
+ if( !p_filter )
+ {
+ msg_Err( p_stream, "deinterlace init failed, even with colorspace conversion" );
+ return;
+ }
+
+ p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
+ }
+ if( p_stream->p_sys->b_master_sync )
{
filter_chain_AppendFilter( id->p_f_chain,
- p_stream->p_sys->psz_deinterlace,
- p_stream->p_sys->p_deinterlace_cfg,
- &id->p_decoder->fmt_out,
- &id->p_decoder->fmt_out );
+ "fps",
+ NULL,
+ p_fmt_out,
+ &id->p_encoder->fmt_in );
p_fmt_out = filter_chain_GetFmtOut( id->p_f_chain );
}
if( p_stream->p_sys->psz_vf2 )
{
- id->p_uf_chain = filter_chain_New( p_stream, "video filter2",
- true,
- transcode_video_filter_allocation_init,
- transcode_video_filter_allocation_clear,
- p_stream->p_sys );
+ id->p_uf_chain = filter_chain_NewVideo( p_stream, true, &owner );
filter_chain_Reset( id->p_uf_chain, p_fmt_out,
&id->p_encoder->fmt_in );
if( p_fmt_out->video.i_chroma != id->p_encoder->fmt_in.video.i_chroma )
p_fmt_out->video.i_sar_den /
p_fmt_out->video.i_height;
- msg_Dbg( p_stream, "decoder aspect is %f:1", f_aspect );
+ msg_Dbg( p_stream, "decoder aspect is %f:1", (double) f_aspect );
/* Change f_aspect from source frame to source pixel */
f_aspect = f_aspect * i_src_visible_height / i_src_visible_width;
- msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );
+ msg_Dbg( p_stream, "source pixel aspect is %f:1", (double) f_aspect );
/* Calculate scaling factor for specified parameters */
if( id->p_encoder->fmt_out.video.i_visible_width <= 0 &&
/* Change aspect ratio from source pixel to scaled pixel */
f_aspect = f_aspect * f_scale_height / f_scale_width;
- msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );
+ msg_Dbg( p_stream, "scaled pixel aspect is %f:1", (double) f_aspect );
/* f_scale_width and f_scale_height are now final */
/* Calculate width, height from scaling
* Make sure its multiple of 2
*/
/* width/height of output stream */
- int i_dst_visible_width = 2 * (int)(f_scale_width*i_src_visible_width/2+0.5);
- int i_dst_visible_height = 2 * (int)(f_scale_height*i_src_visible_height/2+0.5);
- int i_dst_width = 2 * (int)(f_scale_width*p_fmt_out->video.i_width/2+0.5);
- int i_dst_height = 2 * (int)(f_scale_height*p_fmt_out->video.i_height/2+0.5);
+ int i_dst_visible_width = 2 * lroundf(f_scale_width*i_src_visible_width/2);
+ int i_dst_visible_height = 2 * lroundf(f_scale_height*i_src_visible_height/2);
+ int i_dst_width = 2 * lroundf(f_scale_width*p_fmt_out->video.i_width/2);
+ int i_dst_height = 2 * lroundf(f_scale_height*p_fmt_out->video.i_height/2);
/* Change aspect ratio from scaled pixel to output frame */
f_aspect = f_aspect * i_dst_visible_width / i_dst_visible_height;
}
}
+ id->p_encoder->fmt_in.video.orientation =
+ id->p_encoder->fmt_out.video.orientation =
+ id->p_decoder->fmt_in.video.orientation;
+
id->p_encoder->fmt_in.video.i_frame_rate =
id->p_encoder->fmt_out.video.i_frame_rate;
id->p_encoder->fmt_in.video.i_frame_rate_base =
id->p_encoder->fmt_in.video.i_frame_rate,
id->p_encoder->fmt_in.video.i_frame_rate_base );
- id->i_output_frame_interval = id->p_encoder->fmt_out.video.i_frame_rate_base * CLOCK_FREQ / id->p_encoder->fmt_out.video.i_frame_rate;
- msg_Info( p_stream, "output interval %d (base %d)",
- id->i_output_frame_interval, id->p_encoder->fmt_in.video.i_frame_rate_base );
-
- date_Init( &id->next_output_pts,
- id->p_encoder->fmt_in.video.i_frame_rate,
- 1 );
/* Check whether a particular aspect ratio was requested */
if( id->p_encoder->fmt_out.video.i_sar_num <= 0 ||
void transcode_video_close( sout_stream_t *p_stream,
sout_stream_id_sys_t *id )
{
- if( p_stream->p_sys->i_threads >= 1 )
+ if( p_stream->p_sys->i_threads >= 1 && !p_stream->p_sys->b_abort )
{
vlc_mutex_lock( &p_stream->p_sys->lock_out );
p_stream->p_sys->b_abort = true;
vlc_mutex_unlock( &p_stream->p_sys->lock_out );
vlc_join( p_stream->p_sys->thread, NULL );
- vlc_mutex_destroy( &p_stream->p_sys->lock_out );
- vlc_cond_destroy( &p_stream->p_sys->cond );
picture_fifo_Delete( p_stream->p_sys->pp_pics );
block_ChainRelease( p_stream->p_sys->p_buffers );
- p_stream->p_sys->pp_pics = NULL;
}
+ vlc_mutex_destroy( &p_stream->p_sys->lock_out );
+ vlc_cond_destroy( &p_stream->p_sys->cond );
+
/* Close decoder */
if( id->p_decoder->p_module )
module_unneed( id->p_decoder, id->p_decoder->p_module );
{
sout_stream_sys_t *p_sys = p_stream->p_sys;
picture_t *p_pic2 = NULL;
- const mtime_t original_date = p_pic->date;
- bool b_need_duplicate=false;
- /* If input pts is lower than next_output_pts - output_frame_interval
- * Then the future input frame should fit better and we can drop this one
- *
- * We check it here also because we can have case that video filters outputs multiple
- * pictures but we don't need to use them all, for example yadif2x and outputting to some
- * different fps value
- */
- if( ( original_date ) <
- ( date_Get( &id->next_output_pts ) - (mtime_t)id->i_output_frame_interval ) )
- {
-#if 0
- msg_Dbg( p_stream, "dropping frame (%"PRId64" + %"PRId64" vs %"PRId64")",
- p_pic->date, id->i_input_frame_interval, date_Get(&id->next_output_pts) );
-#endif
- picture_Release( p_pic );
- return;
- }
/*
* Encoding
fmt.i_y_offset = 0;
}
- subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt, &fmt,
+ subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt,
+ &id->p_decoder->fmt_out.video,
p_pic->date, p_pic->date, false );
/* Overlay subpicture */
}
}
- /* set output pts*/
- p_pic->date = date_Get( &id->next_output_pts );
- /*This pts is handled, increase clock to next one*/
- date_Increment( &id->next_output_pts, id->p_encoder->fmt_in.video.i_frame_rate_base );
-
if( p_sys->i_threads == 0 )
{
block_t *p_block;
block_ChainAppend( out, p_block );
}
- /* we need to duplicate while next_output_pts + output_frame_interval < input_pts (next input pts)*/
- b_need_duplicate = ( date_Get( &id->next_output_pts ) + id->i_output_frame_interval ) <
- ( original_date );
-
if( p_sys->i_threads )
{
- if( p_sys->b_master_sync )
- {
- p_pic2 = video_new_buffer_encoder( id->p_encoder );
- if( likely( p_pic2 != NULL ) )
- picture_Copy( p_pic2, p_pic );
- }
vlc_mutex_lock( &p_sys->lock_out );
picture_fifo_Push( p_sys->pp_pics, p_pic );
vlc_cond_signal( &p_sys->cond );
vlc_mutex_unlock( &p_sys->lock_out );
}
- while( (p_sys->b_master_sync && b_need_duplicate ))
- {
- if( p_sys->i_threads >= 1 )
- {
- picture_t *p_tmp = NULL;
- /* We can't modify the picture, we need to duplicate it */
- p_tmp = video_new_buffer_encoder( id->p_encoder );
- if( likely( p_tmp != NULL ) )
- {
- picture_Copy( p_tmp, p_pic2 );
- p_tmp->date = date_Get( &id->next_output_pts );
- vlc_mutex_lock( &p_sys->lock_out );
- picture_fifo_Push( p_sys->pp_pics, p_tmp );
- vlc_cond_signal( &p_sys->cond );
- vlc_mutex_unlock( &p_sys->lock_out );
- }
- }
- else
- {
- block_t *p_block;
- p_pic->date = date_Get( &id->next_output_pts );
- p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
- block_ChainAppend( out, p_block );
- }
-#if 0
- msg_Dbg( p_stream, "duplicated frame");
-#endif
- date_Increment( &id->next_output_pts, id->p_encoder->fmt_in.video.i_frame_rate_base );
- b_need_duplicate = ( date_Get( &id->next_output_pts ) + id->i_output_frame_interval ) <
- ( original_date );
- }
-
if( p_sys->i_threads && p_pic2 )
picture_Release( p_pic2 );
else if ( p_sys->i_threads == 0 )
if( unlikely (
id->p_encoder->p_module &&
- !video_format_IsSimilar( &p_sys->fmt_input_video, &id->p_decoder->fmt_out.video )
+ !video_format_IsSimilar( &id->fmt_input_video, &id->p_decoder->fmt_out.video )
)
)
{
msg_Info( p_stream, "aspect-ratio changed, reiniting. %i -> %i : %i -> %i.",
- p_sys->fmt_input_video.i_sar_num, id->p_decoder->fmt_out.video.i_sar_num,
- p_sys->fmt_input_video.i_sar_den, id->p_decoder->fmt_out.video.i_sar_den
+ id->fmt_input_video.i_sar_num, id->p_decoder->fmt_out.video.i_sar_num,
+ id->fmt_input_video.i_sar_den, id->p_decoder->fmt_out.video.i_sar_den
);
/* Close filters */
if( id->p_f_chain )
transcode_video_filter_init( p_stream, id );
transcode_video_encoder_init( p_stream, id );
conversion_video_filter_append( id );
- memcpy( &p_sys->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
+ memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
}
transcode_video_filter_init( p_stream, id );
transcode_video_encoder_init( p_stream, id );
conversion_video_filter_append( id );
- memcpy( &p_sys->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
+ memcpy( &id->fmt_input_video, &id->p_decoder->fmt_out.video, sizeof(video_format_t));
if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
{
id->b_transcode = false;
return VLC_EGENERIC;
}
- date_Set( &id->next_output_pts, p_pic->date );
- }
-
- /*Input lipsync and drop check */
- if( p_sys->b_master_sync )
- {
- /* If input pts lower than next_output_pts - output_frame_interval
- * Then the future input frame should fit better and we can drop this one
- *
- * We check this here as we don't need to run video filter at all for pictures
- * we are going to drop anyway
- *
- * Duplication need is checked in OutputFrame */
- if( ( p_pic->date ) <
- ( date_Get( &id->next_output_pts ) - (mtime_t)id->i_output_frame_interval ) )
- {
-#if 0
- msg_Dbg( p_stream, "dropping frame (%"PRId64" + %"PRId64" vs %"PRId64")",
- p_pic->date, id->i_input_frame_interval, date_Get(&id->next_output_pts) );
-#endif
- picture_Release( p_pic );
- continue;
- }
-#if 0
- msg_Dbg( p_stream, "not dropping frame");
-#endif
-
}
/* Run the filter and output chains; first with the picture,
return VLC_SUCCESS;
}
-bool transcode_video_add( sout_stream_t *p_stream, es_format_t *p_fmt,
+bool transcode_video_add( sout_stream_t *p_stream, const es_format_t *p_fmt,
sout_stream_id_sys_t *id )
{
sout_stream_sys_t *p_sys = p_stream->p_sys;
if( p_sys->fps_num )
{
- id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->fps_num );
- id->p_encoder->fmt_out.video.i_frame_rate_base = (p_sys->fps_den ? p_sys->fps_den : 1);
+ id->p_encoder->fmt_in.video.i_frame_rate = id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->fps_num );
+ id->p_encoder->fmt_in.video.i_frame_rate_base = id->p_encoder->fmt_out.video.i_frame_rate_base = (p_sys->fps_den ? p_sys->fps_den : 1);
}
return true;