]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/transcode/video.c
DTV: add so-called "Clear" QAM
[vlc] / modules / stream_out / transcode / video.c
index 5e3ed9415ee761139b8051cc141f1be9fffba89e..3740f63cbf79e65e946ee0d6d436bee11e6443eb 100644 (file)
@@ -31,7 +31,8 @@
 #include "transcode.h"
 
 #include <vlc_meta.h>
-#include <vlc_osd.h>
+#include <vlc_spu.h>
+#include <vlc_modules.h>
 
 #define ENC_FRAMERATE (25 * 1000 + .5)
 #define ENC_FRAMERATE_BASE 1000
@@ -114,8 +115,8 @@ static int transcode_video_filter_allocation_init( filter_t *p_filter,
                                                    void *p_data )
 {
     VLC_UNUSED(p_data);
-    p_filter->pf_vout_buffer_new = transcode_video_filter_buffer_new;
-    p_filter->pf_vout_buffer_del = transcode_video_filter_buffer_del;
+    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;
 }
 
@@ -242,7 +243,7 @@ int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
         module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
     if( !id->p_encoder->p_module )
     {
-        msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
+        msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s). Take a look few lines earlier to see possible reason.",
                  p_sys->psz_venc ? p_sys->psz_venc : "any",
                  (char *)&p_sys->i_vcodec );
         module_unneed( id->p_decoder, id->p_decoder->p_module );
@@ -274,7 +275,7 @@ int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
         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 ) )
+        if( vlc_thread_create( p_sys, EncoderThread, i_priority ) )
         {
             msg_Err( p_stream, "cannot spawn encoder thread" );
             module_unneed( id->p_decoder, id->p_decoder->p_module );
@@ -285,6 +286,65 @@ int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
     }
     return VLC_SUCCESS;
 }
+
+static void transcode_video_filter_init( sout_stream_t *p_stream,
+                                         sout_stream_id_t *id )
+{
+
+    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 );
+    /* Deinterlace */
+    if( p_stream->p_sys->b_deinterlace )
+    {
+       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 );
+    }
+
+    /* Take care of the scaling and chroma conversions */
+    if( ( id->p_decoder->fmt_out.video.i_chroma !=
+          id->p_encoder->fmt_in.video.i_chroma ) ||
+        ( id->p_decoder->fmt_out.video.i_width !=
+          id->p_encoder->fmt_in.video.i_width ) ||
+        ( id->p_decoder->fmt_out.video.i_height !=
+          id->p_encoder->fmt_in.video.i_height ) )
+    {
+       filter_chain_AppendFilter( id->p_f_chain,
+                                  NULL, NULL,
+                                  &id->p_decoder->fmt_out,
+                                  &id->p_encoder->fmt_in );
+    }
+
+    if( p_stream->p_sys->psz_vf2 )
+    {
+        const es_format_t *p_fmt_out;
+        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 );
+        filter_chain_Reset( id->p_uf_chain, &id->p_encoder->fmt_in,
+                            &id->p_encoder->fmt_in );
+        filter_chain_AppendFromString( id->p_uf_chain, p_stream->p_sys->psz_vf2 );
+        p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
+        es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
+        id->p_encoder->fmt_out.video.i_width =
+            id->p_encoder->fmt_in.video.i_width;
+        id->p_encoder->fmt_out.video.i_height =
+            id->p_encoder->fmt_in.video.i_height;
+        id->p_encoder->fmt_out.video.i_sar_num =
+            id->p_encoder->fmt_in.video.i_sar_num;
+        id->p_encoder->fmt_out.video.i_sar_den =
+            id->p_encoder->fmt_in.video.i_sar_den;
+    }
+
+}
+
 static void transcode_video_encoder_init( sout_stream_t *p_stream,
                                           sout_stream_id_t *id )
 {
@@ -435,11 +495,21 @@ 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 =
-            f_aspect * id->p_encoder->fmt_out.video.i_height + 0.5;
-        id->p_encoder->fmt_out.video.i_sar_num =
-            VOUT_ASPECT_FACTOR * id->p_encoder->fmt_out.video.i_width;
+        vlc_ureduce( &id->p_decoder->fmt_out.video.i_sar_num,
+                     &id->p_decoder->fmt_out.video.i_sar_den,
+                     id->p_decoder->fmt_out.video.i_sar_num,
+                     id->p_decoder->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,
+                 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;
     id->p_encoder->fmt_in.video.i_sar_den =
@@ -478,8 +548,7 @@ static int transcode_video_encoder_open( sout_stream_t *p_stream,
     id->p_encoder->fmt_out.i_codec =
         vlc_fourcc_GetCodec( VIDEO_ES, id->p_encoder->fmt_out.i_codec );
 
-    id->id = sout_StreamIdAdd( p_stream->p_sys->p_out,
-                               &id->p_encoder->fmt_out );
+    id->id = sout_StreamIdAdd( p_stream->p_next, &id->p_encoder->fmt_out );
     if( !id->id )
     {
         msg_Err( p_stream, "cannot add this stream" );
@@ -528,13 +597,35 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
                                     block_t *in, block_t **out )
 {
     sout_stream_sys_t *p_sys = p_stream->p_sys;
-    int i_duplicate = 1;
+    bool b_need_duplicate = false;
     picture_t *p_pic, *p_pic2 = NULL;
     *out = NULL;
 
+    if( in == NULL )
+    {
+        if( p_sys->i_threads == 0 )
+        {
+            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 );
+        }
+        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 );
 
@@ -565,7 +656,7 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
                 i_pts = p_pic->date + 1;
             }
             i_video_drift = p_pic->date - i_pts;
-            i_duplicate = 1;
+            b_need_duplicate = false;
 
             /* Set the pts of the frame being encoded */
             p_pic->date = i_pts;
@@ -585,65 +676,15 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
                 msg_Dbg( p_stream, "adding frame (%i)",
                          (int)(i_video_drift - i_master_drift) );
 #endif
-                i_duplicate = 2;
+                b_need_duplicate = true;
             }
         }
 
-        if( !id->p_encoder->p_module )
+        if( unlikely( !id->p_encoder->p_module ) )
         {
             transcode_video_encoder_init( p_stream, id );
 
-            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 );
-
-            /* Deinterlace */
-            if( p_stream->p_sys->b_deinterlace )
-            {
-                filter_chain_AppendFilter( id->p_f_chain,
-                                           p_sys->psz_deinterlace,
-                                           p_sys->p_deinterlace_cfg,
-                                           &id->p_decoder->fmt_out,
-                                           &id->p_decoder->fmt_out );
-            }
-            /* Take care of the scaling and chroma conversions */
-            if( ( id->p_decoder->fmt_out.video.i_chroma !=
-                  id->p_encoder->fmt_in.video.i_chroma ) ||
-                ( id->p_decoder->fmt_out.video.i_width !=
-                  id->p_encoder->fmt_in.video.i_width ) ||
-                ( id->p_decoder->fmt_out.video.i_height !=
-                  id->p_encoder->fmt_in.video.i_height ) )
-            {
-                filter_chain_AppendFilter( id->p_f_chain,
-                                           NULL, NULL,
-                                           &id->p_decoder->fmt_out,
-                                           &id->p_encoder->fmt_in );
-            }
-
-            if( p_sys->psz_vf2 )
-            {
-                const es_format_t *p_fmt_out;
-                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 );
-                filter_chain_Reset( id->p_uf_chain, &id->p_encoder->fmt_in,
-                                    &id->p_encoder->fmt_in );
-                filter_chain_AppendFromString( id->p_uf_chain, p_sys->psz_vf2 );
-                p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
-                es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
-                id->p_encoder->fmt_out.video.i_width =
-                    id->p_encoder->fmt_in.video.i_width;
-                id->p_encoder->fmt_out.video.i_height =
-                    id->p_encoder->fmt_in.video.i_height;
-                id->p_encoder->fmt_out.video.i_sar_num =
-                    id->p_encoder->fmt_in.video.i_sar_num;
-                id->p_encoder->fmt_out.video.i_sar_den =
-                    id->p_encoder->fmt_in.video.i_sar_den;
-            }
+            transcode_video_filter_init( p_stream, id );
 
             if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
             {
@@ -664,36 +705,36 @@ 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 */
-        }
-
-        /* Overlay subpicture */
-        if( p_subpic )
         {
             video_format_t fmt;
-
-            if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
-            {
-                /* 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 )
-                {
-                    picture_Copy( p_tmp, p_pic );
-                    picture_Release( p_pic );
-                    p_pic = p_tmp;
-                }
-            }
-
             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() );
+            subpicture_t *p_subpic = spu_Render( p_sys->p_spu, NULL, &fmt, &fmt,
+                                                 p_pic->date, p_pic->date, false );
+
+            /* Overlay subpicture */
+            if( p_subpic )
+            {
+                if( picture_IsReferenced( p_pic ) && !filter_chain_GetLength( id->p_f_chain ) )
+                {
+                    /* 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 )
+                    {
+                        picture_Copy( p_tmp, p_pic );
+                        picture_Release( p_pic );
+                        p_pic = p_tmp;
+                    }
+                }
+                if( !p_sys->p_spu_blend )
+                    p_sys->p_spu_blend = filter_NewBlend( VLC_OBJECT( p_sys->p_spu ), &fmt );
+                if( p_sys->p_spu_blend )
+                    picture_BlendSubpicture( p_pic, p_sys->p_spu_blend, p_subpic );
+                subpicture_Delete( p_subpic );
+            }
         }
 
         /* Run user specified filter chain */
@@ -722,39 +763,30 @@ int transcode_video_process( sout_stream_t *p_stream, sout_stream_id_t *id,
                 i_pts = p_pic->date + 1;
             }
             date_Increment( &id->interpolated_pts, 1 );
-        }
 
-        if( p_sys->b_master_sync && i_duplicate > 1 )
-        {
-            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( b_need_duplicate ) )
             {
-                msg_Dbg( p_stream, "drift is too high, resetting master sync" );
-                date_Set( &id->interpolated_pts, p_pic->date );
-                i_pts = p_pic->date + 1;
-            }
-            date_Increment( &id->interpolated_pts, 1 );
 
-            if( p_sys->i_threads >= 1 )
-            {
-                /* We can't modify the picture, we need to duplicate it */
-                p_pic2 = video_new_buffer_decoder( id->p_decoder );
-                if( p_pic2 != NULL )
-                {
-                    picture_Copy( p_pic2, p_pic );
-                    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 );
-            }
+               if( p_sys->i_threads >= 1 )
+               {
+                   /* We can't modify the picture, we need to duplicate it */
+                   p_pic2 = video_new_buffer_decoder( id->p_decoder );
+                   if( p_pic2 != NULL )
+                   {
+                       picture_Copy( p_pic2, p_pic );
+                       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 );
+               }
+           }
         }
 
         if( p_sys->i_threads == 0 )