consumer_avformat.c: further analysis and testing reveals the DV encoder does not...
[melted] / src / modules / avformat / consumer_avformat.c
index d2c6fe1..a56c387 100644 (file)
@@ -31,6 +31,7 @@
 #include <pthread.h>
 #include <sys/time.h>
 #include <math.h>
+#include <unistd.h>
 
 // avformat header files
 #include <avformat.h>
@@ -221,8 +222,34 @@ static int consumer_start( mlt_consumer this )
                }
                
                // Now ensure we honour the multiple of two requested by libavformat
-               mlt_properties_set_int( properties, "width", ( width / 2 ) * 2 );
-               mlt_properties_set_int( properties, "height", ( height / 2 ) * 2 );
+               width = ( width / 2 ) * 2;
+               height = ( height / 2 ) * 2;
+               mlt_properties_set_int( properties, "width", width );
+               mlt_properties_set_int( properties, "height", height );
+
+               // We need to set these on the profile as well because the s property is
+               // an alias to mlt properties that correspond to profile settings.
+               mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
+               if ( profile )
+               {
+                       profile->width = width;
+                       profile->height = height;
+               }
+
+               // Handle the ffmpeg command line "-r" property for frame rate
+               if ( mlt_properties_get( properties, "r" ) )
+               {
+                       double frame_rate = mlt_properties_get_double( properties, "r" );
+                       AVRational rational = av_d2q( frame_rate, 255 );
+                       mlt_properties_set_int( properties, "frame_rate_num", rational.num );
+                       mlt_properties_set_int( properties, "frame_rate_den", rational.den );
+                       if ( profile )
+                       {
+                               profile->frame_rate_num = rational.num;
+                               profile->frame_rate_den = rational.den;
+                               mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
+                       }
+               }
                
                // Apply AVOptions that are synonyms for standard mlt_consumer options
                if ( mlt_properties_get( properties, "ac" ) )
@@ -288,7 +315,13 @@ static void apply_properties( void *obj, mlt_properties properties, int flags )
                const char *opt_name = mlt_properties_get_name( properties, i );
                const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
                if ( opt != NULL )
+#if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
+                       av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
+#elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
+                       av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
+#else
                        av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
+#endif
        }
 }
 
@@ -429,7 +462,6 @@ static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int c
        if ( st != NULL ) 
        {
                char *pix_fmt = mlt_properties_get( properties, "pix_fmt" );
-               double ar = mlt_properties_get_double( properties, "display_ratio" );
                AVCodecContext *c = st->codec;
 
                // Establish defaults from AVOptions
@@ -451,11 +483,52 @@ static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int c
                // Set options controlled by MLT
                c->width = mlt_properties_get_int( properties, "width" );
                c->height = mlt_properties_get_int( properties, "height" );
-               c->sample_aspect_ratio = av_d2q( ar * c->height / c->width , 255);
                c->time_base.num = mlt_properties_get_int( properties, "frame_rate_den" );
                c->time_base.den = mlt_properties_get_int( properties, "frame_rate_num" );
+               st->time_base = c->time_base;
                c->pix_fmt = pix_fmt ? avcodec_get_pix_fmt( pix_fmt ) : PIX_FMT_YUV420P;
 
+               if ( mlt_properties_get( properties, "aspect" ) )
+               {
+                       // "-aspect" on ffmpeg command line is display aspect ratio
+                       double ar = mlt_properties_get_double( properties, "aspect" );
+                       AVRational rational = av_d2q( ar, 255 );
+
+                       // Update the profile and properties as well since this is an alias 
+                       // for mlt properties that correspond to profile settings
+                       mlt_properties_set_int( properties, "display_aspect_num", rational.num );
+                       mlt_properties_set_int( properties, "display_aspect_den", rational.den );
+                       mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
+                       if ( profile )
+                       {
+                               profile->display_aspect_num = rational.num;
+                               profile->display_aspect_den = rational.den;
+                               mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
+                       }
+
+                       // Now compute the sample aspect ratio
+                       rational = av_d2q( ar * c->height / c->width, 255 );
+                       c->sample_aspect_ratio = rational;
+                       // Update the profile and properties as well since this is an alias 
+                       // for mlt properties that correspond to profile settings
+                       mlt_properties_set_int( properties, "sample_aspect_num", rational.num );
+                       mlt_properties_set_int( properties, "sample_aspect_den", rational.den );
+                       if ( profile )
+                       {
+                               profile->sample_aspect_num = rational.num;
+                               profile->sample_aspect_den = rational.den;
+                               mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
+                       }
+               }
+               else
+               {
+                       c->sample_aspect_ratio.num = mlt_properties_get_int( properties, "sample_aspect_num" );
+                       c->sample_aspect_ratio.den = mlt_properties_get_int( properties, "sample_aspect_den" );
+               }
+#if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
+               st->sample_aspect_ratio = c->sample_aspect_ratio;
+#endif
+
                if ( mlt_properties_get_double( properties, "qscale" ) > 0 )
                {
                        c->flags |= CODEC_FLAG_QSCALE;
@@ -524,7 +597,7 @@ static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int c
                        c->flags |= CODEC_FLAG_PASS1;
                else if ( i == 2 )
                        c->flags |= CODEC_FLAG_PASS2;
-               if ( c->flags & ( CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2 ) )
+               if ( codec_id != CODEC_ID_H264 && ( c->flags & ( CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2 ) ) )
                {
                        char logfilename[1024];
                        FILE *f;
@@ -551,6 +624,7 @@ static AVStream *add_video_stream( mlt_consumer this, AVFormatContext *oc, int c
                                }
                                else
                                {
+                                       mlt_properties_set( properties, "_logfilename", logfilename );
                                        fseek( f, 0, SEEK_END );
                                        size = ftell( f );
                                        fseek( f, 0, SEEK_SET );
@@ -760,13 +834,7 @@ static void *consumer_thread( void *arg )
        // Check for audio codec overides
        if ( acodec != NULL )
        {
-               AVCodec *p = first_avcodec;
-               while( p != NULL ) 
-               {
-                       if ( !strcmp( p->name, acodec ) && p->type == CODEC_TYPE_AUDIO )
-                               break;
-                       p = p->next;
-               }
+               AVCodec *p = avcodec_find_encoder_by_name( acodec );
                if ( p != NULL )
                        audio_codec_id = p->id;
                else
@@ -776,13 +844,7 @@ static void *consumer_thread( void *arg )
        // Check for video codec overides
        if ( vcodec != NULL )
        {
-               AVCodec *p = first_avcodec;
-               while( p != NULL ) 
-               {
-                       if ( !strcmp( p->name, vcodec ) && p->type == CODEC_TYPE_VIDEO )
-                               break;
-                       p = p->next;
-               }
+               AVCodec *p = avcodec_find_encoder_by_name( vcodec );
                if ( p != NULL )
                        video_codec_id = p->id;
                else
@@ -1221,6 +1283,28 @@ static void *consumer_thread( void *arg )
 
        mlt_consumer_stopped( this );
 
+       if ( mlt_properties_get_int( properties, "pass" ) == 2 )
+       {
+               // Remove the dual pass log file
+               if ( mlt_properties_get( properties, "_logfilename" ) )
+                       remove( mlt_properties_get( properties, "_logfilename" ) );
+
+               // Remove the x264 dual pass logs
+               char *cwd = getcwd( NULL, 0 );
+               char *file = "x264_2pass.log";
+               char *full = malloc( strlen( cwd ) + strlen( file ) + 2 );
+               sprintf( full, "%s/%s", cwd, file );
+               remove( full );
+               free( full );
+               file = "x264_2pass.log.temp";
+               full = malloc( strlen( cwd ) + strlen( file ) + 2 );
+               sprintf( full, "%s/%s", cwd, file );
+               remove( full );
+               free( full );
+               free( cwd );
+               remove( "x264_2pass.log.temp" );
+       }
+
        return NULL;
 }