]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/transcode/video.c
transcode: user ENC_FRAMERATE_BASE instead of fixed value
[vlc] / modules / stream_out / transcode / video.c
index 9b16ae6246033a35a5e1834f869991d0d79124a5..2475cd04c566812d96bc43ccc8ae3de85b8d97be 100644 (file)
@@ -32,6 +32,7 @@
 
 #include <vlc_meta.h>
 #include <vlc_spu.h>
+#include <vlc_modules.h>
 
 #define ENC_FRAMERATE (25 * 1000 + .5)
 #define ENC_FRAMERATE_BASE 1000
@@ -41,23 +42,6 @@ struct decoder_owner_sys_t
     sout_stream_sys_t *p_sys;
 };
 
-static inline void video_timer_start( encoder_t * p_encoder )
-{
-    stats_TimerStart( p_encoder, "encoding video frame",
-                      STATS_TIMER_VIDEO_FRAME_ENCODING );
-}
-
-static inline void video_timer_stop( encoder_t * p_encoder )
-{
-    stats_TimerStop( p_encoder, STATS_TIMER_VIDEO_FRAME_ENCODING );
-}
-
-static inline void video_timer_close( encoder_t * p_encoder )
-{
-    stats_TimerDump(  p_encoder, STATS_TIMER_VIDEO_FRAME_ENCODING );
-    stats_TimerClean( p_encoder, STATS_TIMER_VIDEO_FRAME_ENCODING );
-}
-
 static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
 {
     VLC_UNUSED(p_decoder);
@@ -124,24 +108,22 @@ static void transcode_video_filter_allocation_clear( filter_t *p_filter )
     VLC_UNUSED(p_filter);
 }
 
-static void* EncoderThread( vlc_object_t* p_this )
+static void* EncoderThread( void *obj )
 {
-    sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_this;
+    sout_stream_sys_t *p_sys = (sout_stream_sys_t*)obj;
     sout_stream_id_t *id = p_sys->id_video;
     picture_t *p_pic;
     int canc = vlc_savecancel ();
 
-    while( vlc_object_alive (p_sys) && !p_sys->b_error )
+    for( ;; )
     {
         block_t *p_block;
 
         vlc_mutex_lock( &p_sys->lock_out );
-        while( p_sys->i_last_pic == p_sys->i_first_pic )
-        {
+        while( !p_sys->b_abort && p_sys->i_last_pic == p_sys->i_first_pic )
             vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
-            if( !vlc_object_alive (p_sys) || p_sys->b_error ) break;
-        }
-        if( !vlc_object_alive (p_sys) || p_sys->b_error )
+
+        if( p_sys->b_abort )
         {
             vlc_mutex_unlock( &p_sys->lock_out );
             break;
@@ -151,9 +133,7 @@ static void* EncoderThread( vlc_object_t* p_this )
         p_sys->i_first_pic %= PICTURE_RING_SIZE;
         vlc_mutex_unlock( &p_sys->lock_out );
 
-        video_timer_start( id->p_encoder );
         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
-        video_timer_stop( id->p_encoder );
 
         vlc_mutex_lock( &p_sys->lock_out );
         block_ChainAppend( &p_sys->p_buffers, p_block );
@@ -273,8 +253,8 @@ int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
         p_sys->i_first_pic = 0;
         p_sys->i_last_pic = 0;
         p_sys->p_buffers = NULL;
-        p_sys->b_die = p_sys->b_error = 0;
-        if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority ) )
+        p_sys->b_abort = 0;
+        if( vlc_clone( &p_sys->thread, EncoderThread, p_sys, i_priority ) )
         {
             msg_Err( p_stream, "cannot spawn encoder thread" );
             module_unneed( id->p_decoder, id->p_decoder->p_module );
@@ -494,14 +474,20 @@ static void transcode_video_encoder_init( sout_stream_t *p_stream,
     if( id->p_encoder->fmt_out.video.i_sar_num <= 0 ||
         id->p_encoder->fmt_out.video.i_sar_den <= 0 )
     {
-        id->p_encoder->fmt_out.video.i_sar_num = id->p_decoder->fmt_out.video.i_sar_num * i_src_width / i_dst_width;
-        id->p_encoder->fmt_out.video.i_sar_den = id->p_decoder->fmt_out.video.i_sar_den * i_src_height / i_dst_height;
+        vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
+                     &id->p_encoder->fmt_out.video.i_sar_den,
+                     (uint64_t)id->p_decoder->fmt_out.video.i_sar_num * i_src_width  * i_dst_height,
+                     (uint64_t)id->p_decoder->fmt_out.video.i_sar_den * i_src_height * i_dst_width,
+                     0 );
+    }
+    else
+    {
+        vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
+                     &id->p_encoder->fmt_out.video.i_sar_den,
+                     id->p_encoder->fmt_out.video.i_sar_num,
+                     id->p_encoder->fmt_out.video.i_sar_den,
+                     0 );
     }
-    vlc_ureduce( &id->p_encoder->fmt_out.video.i_sar_num,
-                 &id->p_encoder->fmt_out.video.i_sar_den,
-                 id->p_encoder->fmt_out.video.i_sar_num,
-                 id->p_encoder->fmt_out.video.i_sar_den,
-                 0 );
 
     id->p_encoder->fmt_in.video.i_sar_num =
         id->p_encoder->fmt_out.video.i_sar_num;
@@ -557,16 +543,15 @@ void transcode_video_close( sout_stream_t *p_stream,
     if( p_stream->p_sys->i_threads >= 1 )
     {
         vlc_mutex_lock( &p_stream->p_sys->lock_out );
-        vlc_object_kill( p_stream->p_sys );
+        p_stream->p_sys->b_abort = true;
         vlc_cond_signal( &p_stream->p_sys->cond );
         vlc_mutex_unlock( &p_stream->p_sys->lock_out );
-        vlc_thread_join( p_stream->p_sys );
+
+        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 );
     }
 
-    video_timer_close( id->p_encoder );
-
     /* Close decoder */
     if( id->p_decoder->p_module )
         module_unneed( id->p_decoder, id->p_decoder->p_module );
@@ -594,29 +579,34 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
     picture_t *p_pic, *p_pic2 = NULL;
     *out = NULL;
 
-    if( in == NULL )
+    if( unlikely( in == NULL ) )
     {
-       block_t *p_block;
-       do {
-           video_timer_start( id->p_encoder );
-           p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
-           video_timer_stop( id->p_encoder );
-           block_ChainAppend( out, p_block );
-       } while( p_block );
-       return VLC_SUCCESS;
+        if( p_sys->i_threads == 0 )
+        {
+            block_t *p_block;
+            do {
+                p_block = id->p_encoder->pf_encode_video(id->p_encoder, NULL );
+                block_ChainAppend( out, p_block );
+            } while( p_block );
+        }
+        else
+        {
+            /*
+             * FIXME: we need EncoderThread() to flush buffers and signal us
+             * when it's done so we can send the last frames to the chain
+             */
+        }
+        return VLC_SUCCESS;
     }
 
 
     while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
     {
-        subpicture_t *p_subpic = NULL;
-
-        sout_UpdateStatistic( p_stream->p_sout, SOUT_STATISTIC_DECODED_VIDEO, 1 );
 
         if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
         {
             mtime_t current_date = mdate();
-            if( current_date + 50000 > p_pic->date )
+            if( unlikely( current_date + 50000 > p_pic->date ) )
             {
                 msg_Dbg( p_stream, "late picture skipped (%"PRId64")",
                          current_date + 50000 - p_pic->date );
@@ -632,8 +622,8 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
             mtime_t i_pts;
 
             i_pts = date_Get( &id->interpolated_pts ) + 1;
-            if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
-                  || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
+            if ( unlikely( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
+                  || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT ) )
             {
                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
                 date_Set( &id->interpolated_pts, p_pic->date );
@@ -645,7 +635,7 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
             /* Set the pts of the frame being encoded */
             p_pic->date = i_pts;
 
-            if( i_video_drift < (i_master_drift - 50000) )
+            if( unlikely( i_video_drift < (i_master_drift - 50000) ) )
             {
 #if 0
                 msg_Dbg( p_stream, "dropping frame (%i)",
@@ -654,7 +644,7 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
                 picture_Release( p_pic );
                 continue;
             }
-            else if( i_video_drift > (i_master_drift + 50000) )
+            else if( unlikely( i_video_drift > (i_master_drift + 50000) ) )
             {
 #if 0
                 msg_Dbg( p_stream, "adding frame (%i)",
@@ -683,6 +673,10 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
         if( id->p_f_chain )
             p_pic = filter_chain_VideoFilter( id->p_f_chain, p_pic );
 
+        /* Run user specified filter chain */
+        if( id->p_uf_chain )
+            p_pic = filter_chain_VideoFilter( id->p_uf_chain, p_pic );
+
         /*
          * Encoding
          */
@@ -690,57 +684,53 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
         /* Check if we have a subpicture to overlay */
         if( p_sys->p_spu )
         {
-            p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date, false );
-            /* TODO: get another pic */
-        }
+            video_format_t fmt = id->p_encoder->fmt_in.video;
+            if( fmt.i_visible_width <= 0 || fmt.i_visible_height <= 0 )
+            {
+                fmt.i_visible_width  = fmt.i_width;
+                fmt.i_visible_height = fmt.i_height;
+                fmt.i_x_offset       = 0;
+                fmt.i_y_offset       = 0;
+            }
 
-        /* Overlay subpicture */
-        if( p_subpic )
-        {
-            video_format_t fmt;
+            subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt, &fmt,
+                                                 p_pic->date, p_pic->date, false );
 
-            if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
+            /* Overlay subpicture */
+            if( p_subpic )
             {
-                /* We can't modify the picture, we need to duplicate it */
-                picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
-                if( p_tmp )
+                if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
                 {
-                    picture_Copy( p_tmp, p_pic );
-                    picture_Release( p_pic );
-                    p_pic = p_tmp;
+                    /* We can't modify the picture, we need to duplicate it */
+                    picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
+                    if( likely( p_tmp ) )
+                    {
+                        picture_Copy( p_tmp, p_pic );
+                        picture_Release( p_pic );
+                        p_pic = p_tmp;
+                    }
                 }
+                if( unlikely( !p_sys->p_spu_blend ) )
+                    p_sys->p_spu_blend = filter_NewBlend( VLC_OBJECT( p_sys->p_spu ), &fmt );
+                if( likely( p_sys->p_spu_blend ) )
+                    picture_BlendSubpicture( p_pic, p_sys->p_spu_blend, p_subpic );
+                subpicture_Delete( p_subpic );
             }
-
-            if( filter_chain_GetLength( id->p_f_chain ) > 0 )
-                fmt = filter_chain_GetFmtOut( id->p_f_chain )->video;
-            else
-                fmt = id->p_decoder->fmt_out.video;
-
-            /* FIXME the mdate() seems highly suspicious */
-            spu_RenderSubpictures( p_sys->p_spu, p_pic, &fmt,
-                                   p_subpic, &id->p_decoder->fmt_out.video, mdate() );
         }
 
-        /* Run user specified filter chain */
-        if( id->p_uf_chain )
-            p_pic = filter_chain_VideoFilter( id->p_uf_chain, p_pic );
-
         if( p_sys->i_threads == 0 )
         {
             block_t *p_block;
 
-            video_timer_start( id->p_encoder );
             p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
-            video_timer_stop( id->p_encoder );
-
             block_ChainAppend( out, p_block );
         }
 
         if( p_sys->b_master_sync )
         {
             mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
-            if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
-                  || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
+            if (unlikely ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
+                  || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT ) )
             {
                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
                 date_Set( &id->interpolated_pts, p_pic->date );
@@ -755,19 +745,17 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
                {
                    /* We can't modify the picture, we need to duplicate it */
                    p_pic2 = video_new_buffer_decoder( id->p_decoder );
-                   if( p_pic2 != NULL )
+                   if( unlikely( p_pic2 != NULL ) )
                    {
                        picture_Copy( p_pic2, p_pic );
-                       p_pic2->date = i_pts + 1;
+                       p_pic2->date = i_pts;
                    }
                }
                else
                {
                    block_t *p_block;
                    p_pic->date = i_pts;
-                   video_timer_start( id->p_encoder );
                    p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
-                   video_timer_stop( id->p_encoder );
                    block_ChainAppend( out, p_block );
                }
            }
@@ -825,7 +813,7 @@ bool transcode_video_add( sout_stream_t *p_stream, es_format_t *p_fmt,
 
     if( p_sys->f_fps > 0 )
     {
-        id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->f_fps * 1000) + 0.5;
+        id->p_encoder->fmt_out.video.i_frame_rate = (p_sys->f_fps * ENC_FRAMERATE_BASE) + 0.5;
         id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
     }