]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/transcode.c
* modules/mux/ogg.c, modules/stream_out/transcode.c: better fix for the starting...
[vlc] / modules / stream_out / transcode.c
index 1a4a19382e5f38e8c85905f4f10b7de3e7423677..53bb67bcdcc1a9432c89fe02b3f9c3379646e69e 100644 (file)
@@ -2,7 +2,7 @@
  * transcode.c
  *****************************************************************************
  * Copyright (C) 2001, 2002 VideoLAN
- * $Id: transcode.c,v 1.29 2003/08/09 14:59:24 gbazin Exp $
+ * $Id: transcode.c,v 1.41 2003/10/09 19:31:38 gbazin Exp $
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
@@ -30,6 +30,8 @@
 #include <vlc/vlc.h>
 #include <vlc/input.h>
 #include <vlc/sout.h>
+#include <vlc/vout.h>
+#include <vlc/decoder.h>
 
 /* ffmpeg header */
 #ifdef HAVE_FFMPEG_AVCODEC_H
@@ -316,10 +318,14 @@ struct sout_stream_id_t
     vlc_fourcc_t  b_transcode;
     sout_format_t f_src;        /* only if transcoding */
     sout_format_t f_dst;        /*  "   "      " */
+    unsigned int  i_inter_pixfmt; /* intermediary format when transcoding */
 
     /* id of the out stream */
     void *id;
 
+    /* Encoder */
+    encoder_t *p_encoder;
+
     /* ffmpeg part */
     AVCodec         *ff_dec;
     AVCodecContext  *ff_dec_c;
@@ -373,6 +379,8 @@ static sout_stream_id_t * Add( sout_stream_t *p_stream, sout_format_t *p_fmt )
     id = malloc( sizeof( sout_stream_id_t ) );
     id->i_dts = 0;
     id->id = NULL;
+    id->p_encoder = NULL;
+
     if( p_fmt->i_cat == AUDIO_ES && p_sys->i_acodec != 0 )
     {
         msg_Dbg( p_stream,
@@ -528,6 +536,7 @@ static struct
     /* audio */
     { VLC_FOURCC( 'm', 'p', 'g', 'a' ), CODEC_ID_MP2 },
     { VLC_FOURCC( 'm', 'p', '3', ' ' ), CODEC_ID_MP3LAME },
+    { VLC_FOURCC( 'm', 'p', '4', 'a' ), CODEC_ID_AAC },
     { VLC_FOURCC( 'a', '5', '2', ' ' ), CODEC_ID_AC3 },
     { VLC_FOURCC( 'a', 'c', '3', ' ' ), CODEC_ID_AC3 },
     { VLC_FOURCC( 'w', 'm', 'a', '1' ), CODEC_ID_WMAV1 },
@@ -536,8 +545,12 @@ static struct
     { VLC_FOURCC( 'a', 'l', 'a', 'w' ), CODEC_ID_PCM_ALAW },
 
     /* video */
-    { VLC_FOURCC( 'm', 'p', '4', 'v'),  CODEC_ID_MPEG4 },
     { VLC_FOURCC( 'm', 'p', 'g', 'v' ), CODEC_ID_MPEG1VIDEO },
+    { VLC_FOURCC( 'm', 'p', '1', 'v' ), CODEC_ID_MPEG1VIDEO },
+#if LIBAVCODEC_BUILD >= 4676
+    { VLC_FOURCC( 'm', 'p', '2', 'v' ), CODEC_ID_MPEG2VIDEO },
+#endif
+    { VLC_FOURCC( 'm', 'p', '4', 'v'),  CODEC_ID_MPEG4 },
     { VLC_FOURCC( 'D', 'I', 'V', '1' ), CODEC_ID_MSMPEG4V1 },
     { VLC_FOURCC( 'D', 'I', 'V', '2' ), CODEC_ID_MSMPEG4V2 },
     { VLC_FOURCC( 'D', 'I', 'V', '3' ), CODEC_ID_MSMPEG4V3 },
@@ -731,6 +744,11 @@ static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
     id->ff_enc_c->sample_rate = id->f_dst.i_sample_rate;
     id->ff_enc_c->channels    = id->f_dst.i_channels;
 
+    /* Make sure we get extradata filled by the encoder */
+    id->ff_enc_c->extradata_size = 0;
+    id->ff_enc_c->extradata = NULL;
+    id->ff_enc_c->flags |= CODEC_FLAG_GLOBAL_HEADER;
+
     if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
     {
         if( id->ff_enc_c->channels > 2 )
@@ -751,6 +769,10 @@ static int transcode_audio_ffmpeg_new( sout_stream_t *p_stream,
         }
     }
 
+    id->f_dst.i_extra_data = id->ff_enc_c->extradata_size;
+    id->f_dst.p_extra_data = id->ff_enc_c->extradata;
+    id->ff_enc_c->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
+
     return VLC_SUCCESS;
 }
 
@@ -982,7 +1004,7 @@ static int transcode_audio_ffmpeg_process( sout_stream_t *p_stream,
                     p_out->i_size = header[i].bytes;
                     p_out->i_length = 0;
 
-                    p_out->i_dts = p_out->i_pts = 0;
+                    p_out->i_dts = p_out->i_pts = id->i_dts;
 
                     sout_BufferChain( out, p_out );
                 }
@@ -1201,20 +1223,84 @@ static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
 
 
     /* find encoder */
+    id->ff_enc = id->ff_enc_c = NULL;
     i_ff_codec = get_ff_codec( id->f_dst.i_fourcc );
-    if( i_ff_codec == 0 )
+    if( i_ff_codec != 0 )
     {
-        msg_Err( p_stream, "cannot find encoder" );
-        return VLC_EGENERIC;
+        id->ff_enc = avcodec_find_encoder( i_ff_codec );
     }
 
-    id->ff_enc = avcodec_find_encoder( i_ff_codec );
+    /* Hack for external encoders */
     if( !id->ff_enc )
+    {
+        id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
+        id->p_encoder->i_fourcc = id->f_dst.i_fourcc;
+        id->p_encoder->format.video.i_width = p_sys->i_width;
+        id->p_encoder->format.video.i_height = p_sys->i_height;
+        id->p_encoder->i_bitrate = p_sys->i_vbitrate;
+
+            if( id->p_encoder->format.video.i_width <= 0 )
+            {
+                id->p_encoder->format.video.i_width = id->f_dst.i_width =
+                    id->ff_dec_c->width - p_sys->i_crop_left -
+                    p_sys->i_crop_right;
+            }
+            if( id->p_encoder->format.video.i_height <= 0 )
+            {
+                id->p_encoder->format.video.i_height = id->f_dst.i_height =
+                    id->ff_dec_c->height - p_sys->i_crop_top -
+                    p_sys->i_crop_bottom;
+            }
+
+        id->p_encoder->p_module =
+            module_Need( id->p_encoder, "video encoder", NULL );
+
+        if( !id->p_encoder->p_module )
+        {
+            free( id->p_encoder );
+            id->p_encoder = NULL;
+        }
+    }
+    /* End hack for external encoders */
+
+    if( !id->ff_enc && !id->p_encoder )
     {
         msg_Err( p_stream, "cannot find encoder" );
         return VLC_EGENERIC;
     }
 
+    /* XXX open it only when we have the first frame */
+    id->b_enc_inited     = VLC_FALSE;
+    id->i_buffer_in      = 0;
+    id->i_buffer_in_pos  = 0;
+    id->p_buffer_in      = NULL;
+
+    id->i_buffer     = 3*1024*1024;
+    id->i_buffer_pos = 0;
+    id->p_buffer     = malloc( id->i_buffer );
+
+    id->i_buffer_out     = 0;
+    id->i_buffer_out_pos = 0;
+    id->p_buffer_out     = NULL;
+
+    id->p_ff_pic         = avcodec_alloc_frame();
+    id->p_ff_pic_tmp0    = NULL;
+    id->p_ff_pic_tmp1    = NULL;
+    id->p_ff_pic_tmp2    = NULL;
+    id->p_vresample      = NULL;
+
+    p_sys->i_last_ref_pts = 0;
+    p_sys->i_buggy_pts_detect = 0;
+
+    /* This is enough for external encoders */
+    if( id->p_encoder && id->p_encoder->p_module ) return VLC_SUCCESS;
+
+    if( id->f_dst.i_fourcc == VLC_FOURCC( 'm','p','1','v' )||
+        id->f_dst.i_fourcc == VLC_FOURCC( 'm','p','2','v' ) )
+    {
+        id->f_dst.i_fourcc = VLC_FOURCC( 'm','p','g','v' );
+    }
+
     id->ff_enc_c = avcodec_alloc_context();
     id->ff_enc_c->width          = id->f_dst.i_width;
     id->ff_enc_c->height         = id->f_dst.i_height;
@@ -1262,28 +1348,6 @@ static int transcode_video_ffmpeg_new( sout_stream_t *p_stream,
     {
         id->ff_enc_c->pix_fmt = get_ff_chroma( id->f_dst.i_fourcc );
     }
-    /* XXX open it only when we have the first frame */
-    id->b_enc_inited     = VLC_FALSE;
-    id->i_buffer_in      = 0;
-    id->i_buffer_in_pos  = 0;
-    id->p_buffer_in      = NULL;
-
-    id->i_buffer     = 3*1024*1024;
-    id->i_buffer_pos = 0;
-    id->p_buffer     = malloc( id->i_buffer );
-
-    id->i_buffer_out     = 0;
-    id->i_buffer_out_pos = 0;
-    id->p_buffer_out     = NULL;
-
-    id->p_ff_pic         = avcodec_alloc_frame();
-    id->p_ff_pic_tmp0    = NULL;
-    id->p_ff_pic_tmp1    = NULL;
-    id->p_ff_pic_tmp2    = NULL;
-    id->p_vresample      = NULL;
-
-    p_sys->i_last_ref_pts = 0;
-    p_sys->i_buggy_pts_detect = 0;
 
     return VLC_SUCCESS;
 }
@@ -1294,7 +1358,13 @@ static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream, sout_stream_
     {
         avcodec_close( id->ff_dec_c );
     }
-    if( id->b_enc_inited )
+    if( id->p_encoder )
+    {
+        /* External encoding */
+        module_Unneed( id->p_encoder, id->p_encoder->p_module );
+        vlc_object_destroy( id->p_encoder->p_module );
+    }
+    else if( id->b_enc_inited )
     {
         avcodec_close( id->ff_enc_c );
     }
@@ -1325,8 +1395,7 @@ static void transcode_video_ffmpeg_close ( sout_stream_t *p_stream, sout_stream_
     }
 
     free( id->ff_dec_c );
-    free( id->ff_enc_c );
-
+    if( id->ff_enc_c ) free( id->ff_enc_c );
     free( id->p_buffer );
 }
 
@@ -1392,10 +1461,55 @@ static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
             p_sys->i_output_pts = frame->pts;
         }
 
-        if( !id->b_enc_inited )
+        if( !id->b_enc_inited && id->p_encoder )
+        {
+            block_t *p_block;
+
+            /* XXX hack because of copy packetizer and mpeg4video that can fail
+             * detecting size */
+#if 0
+            if( id->p_encoder->i_width <= 0 )
+            {
+                id->p_encoder->i_width = id->f_dst.i_width =
+                    id->ff_dec_c->width - p_sys->i_crop_left -
+                    p_sys->i_crop_right;
+            }
+            if( id->p_encoder->i_height <= 0 )
+            {
+                id->p_encoder->i_height = id->f_dst.i_height =
+                    id->ff_dec_c->height - p_sys->i_crop_top -
+                    p_sys->i_crop_bottom;
+            }
+#endif
+
+            id->p_encoder->i_bitrate = p_sys->i_vbitrate;
+
+            if( !( id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out, &id->f_dst ) ) )
+            {
+                msg_Err( p_stream, "cannot add this stream" );
+                id->b_transcode = VLC_FALSE;
+                return VLC_EGENERIC;
+            }
+
+            while( (p_block = id->p_encoder->pf_header( id->p_encoder )) )
+            {
+                sout_buffer_t *p_out;
+                p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
+                memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
+                p_out->i_dts = id->i_dts;
+                p_out->i_pts = id->i_dts;
+                sout_BufferChain( out, p_out );
+            }
+
+            id->i_inter_pixfmt =
+                get_ff_chroma( id->p_encoder->format.video.i_chroma );
+
+            id->b_enc_inited = VLC_TRUE;
+        }
+        else if( !id->b_enc_inited )
         {
-            /* XXX hack because of copy packetizer and mpeg4video that can failed
-               detecting size */
+            /* XXX hack because of copy packetizer and mpeg4video that can fail
+             * detecting size */
             if( id->ff_enc_c->width <= 0 )
             {
                 id->ff_enc_c->width    =
@@ -1407,12 +1521,21 @@ static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
                     id->f_dst.i_height = id->ff_dec_c->height - p_sys->i_crop_top - p_sys->i_crop_bottom;
             }
 
+            /* Make sure we get extradata filled by the encoder */
+            id->ff_enc_c->extradata_size = 0;
+            id->ff_enc_c->extradata = NULL;
+            id->ff_enc_c->flags |= CODEC_FLAG_GLOBAL_HEADER;
+
             if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
             {
                 msg_Err( p_stream, "cannot open encoder" );
                 return VLC_EGENERIC;
             }
 
+            id->f_dst.i_extra_data = id->ff_enc_c->extradata_size;
+            id->f_dst.p_extra_data = id->ff_enc_c->extradata;
+            id->ff_enc_c->flags &= ~CODEC_FLAG_GLOBAL_HEADER;
+
             if( !( id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out, &id->f_dst ) ) )
             {
                 msg_Err( p_stream, "cannot add this stream" );
@@ -1420,6 +1543,9 @@ static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
                 id->b_transcode = VLC_FALSE;
                 return VLC_EGENERIC;
             }
+
+            id->i_inter_pixfmt = id->ff_enc_c->pix_fmt;
+
             id->b_enc_inited = VLC_TRUE;
         }
 
@@ -1438,7 +1564,7 @@ static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
                 buf = malloc( i_size );
 
                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp0, buf,
-                                id->ff_enc_c->pix_fmt,
+                                id->i_inter_pixfmt,
                                 id->ff_dec_c->width, id->ff_dec_c->height );
             }
 
@@ -1450,33 +1576,34 @@ static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
         }
 
         /* convert pix format */
-        if( id->ff_dec_c->pix_fmt != id->ff_enc_c->pix_fmt )
+        if( id->ff_dec_c->pix_fmt != id->i_inter_pixfmt )
         {
             if( id->p_ff_pic_tmp1 == NULL )
             {
                 int     i_size;
                 uint8_t *buf;
                 id->p_ff_pic_tmp1 = avcodec_alloc_frame();
-                i_size = avpicture_get_size( id->ff_enc_c->pix_fmt,
-                                             id->ff_dec_c->width, id->ff_dec_c->height );
+                i_size = avpicture_get_size( id->i_inter_pixfmt,
+                                             id->ff_dec_c->width,
+                                             id->ff_dec_c->height );
 
                 buf = malloc( i_size );
 
                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp1, buf,
-                                id->ff_enc_c->pix_fmt,
+                                id->i_inter_pixfmt,
                                 id->ff_dec_c->width, id->ff_dec_c->height );
             }
 
-            img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->ff_enc_c->pix_fmt,
-                         (AVPicture*)frame,             id->ff_dec_c->pix_fmt,
+            img_convert( (AVPicture*)id->p_ff_pic_tmp1, id->i_inter_pixfmt,
+                         (AVPicture*)frame, id->ff_dec_c->pix_fmt,
                          id->ff_dec_c->width, id->ff_dec_c->height );
 
             frame = id->p_ff_pic_tmp1;
         }
 
         /* convert size and crop */
-        if( id->ff_dec_c->width  != id->ff_enc_c->width ||
-            id->ff_dec_c->height != id->ff_enc_c->height ||
+        if( id->ff_dec_c->width  != id->f_dst.i_width ||
+            id->ff_dec_c->height != id->f_dst.i_height ||
             p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
             p_sys->i_crop_left > 0 || p_sys->i_crop_right )
         {
@@ -1485,17 +1612,19 @@ static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
                 int     i_size;
                 uint8_t *buf;
                 id->p_ff_pic_tmp2 = avcodec_alloc_frame();
-                i_size = avpicture_get_size( id->ff_enc_c->pix_fmt,
-                                             id->ff_enc_c->width, id->ff_enc_c->height );
+                i_size = avpicture_get_size( id->i_inter_pixfmt,
+                                             id->f_dst.i_width,
+                                             id->f_dst.i_height );
 
                 buf = malloc( i_size );
 
                 avpicture_fill( (AVPicture*)id->p_ff_pic_tmp2, buf,
-                                id->ff_enc_c->pix_fmt,
-                                id->ff_enc_c->width, id->ff_enc_c->height );
+                                id->i_inter_pixfmt,
+                                id->f_dst.i_width, id->f_dst.i_height );
 
                 id->p_vresample =
-                    img_resample_full_init( id->ff_enc_c->width, id->ff_enc_c->height,
+                    img_resample_full_init( id->f_dst.i_width,
+                                            id->f_dst.i_height,
                                             id->ff_dec_c->width, id->ff_dec_c->height,
                                             p_stream->p_sys->i_crop_top,
                                             p_stream->p_sys->i_crop_bottom,
@@ -1509,8 +1638,12 @@ static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
             frame = id->p_ff_pic_tmp2;
         }
 
-        /* Set the pts of the frame being encoded */
-        frame->pts = p_sys->i_output_pts;
+        /* Set the pts of the frame being encoded (segfaults with mpeg4!)*/
+        if( id->p_encoder ||
+            id->f_dst.i_fourcc == VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
+            frame->pts = p_sys->i_output_pts;
+        else
+            frame->pts = 0;
 
         /* Interpolate the next PTS
          * (needed by the mpeg video packetizer which can send pts <= 0 ) */
@@ -1520,6 +1653,40 @@ static int transcode_video_ffmpeg_process( sout_stream_t *p_stream,
               id->ff_dec_c->frame_rate_base / (2 * id->ff_dec_c->frame_rate);
         }
 
+        if( id->p_encoder )
+        {
+            /* External encoding */
+            block_t *p_block;
+            picture_t pic;
+            int i_plane;
+
+            vout_InitPicture( VLC_OBJECT(p_stream), &pic,
+                              id->f_dst.i_width, id->f_dst.i_height,
+                              id->p_encoder->format.video.i_chroma );
+
+            for( i_plane = 0; i_plane < pic.i_planes; i_plane++ )
+            {
+                pic.p[i_plane].p_pixels = frame->data[i_plane];
+                pic.p[i_plane].i_pitch = frame->linesize[i_plane];
+            }
+
+            pic.date = frame->pts;
+
+            p_block = id->p_encoder->pf_encode_video( id->p_encoder, &pic );
+            if( p_block )
+            {
+                sout_buffer_t *p_out;
+                p_out = sout_BufferNew( p_stream->p_sout, p_block->i_buffer );
+                memcpy( p_out->p_buffer, p_block->p_buffer, p_block->i_buffer);
+                p_out->i_dts = p_block->i_dts;
+                p_out->i_pts = p_block->i_pts;
+                sout_BufferChain( out, p_out );
+                block_Release( p_block );
+            }
+
+            return VLC_SUCCESS;
+        }
+
         /* Let ffmpeg select the frame type */
         frame->pict_type = 0;