]> git.sesse.net Git - mlt/blobdiff - src/modules/avformat/consumer_avformat.c
fix build due to removal of deprecated encode() from AVCodec
[mlt] / src / modules / avformat / consumer_avformat.c
index 78edffae360f1e57701da1a9ce81784b70606339..640e2b00bd63b21132a5fb01c9f3d534f3eb0ec3 100644 (file)
@@ -237,7 +237,13 @@ static int consumer_start( mlt_consumer consumer )
                mlt_properties_set_data( properties, "acodec", codecs, 0, (mlt_destructor) mlt_properties_close, NULL );
                mlt_properties_set_data( doc, "audio_codecs", codecs, 0, NULL, NULL );
                while ( ( codec = av_codec_next( codec ) ) )
+#if LIBAVCODEC_VERSION_INT >= ((54<<16)+(56<<8)+100)
+                       if ( codec->encode2 && codec->type == CODEC_TYPE_AUDIO )
+#elif LIBAVCODEC_VERSION_INT >= ((54<<16)+(0<<8)+0)
+                       if ( ( codec->encode || codec->encode2 ) && codec->type == CODEC_TYPE_AUDIO )
+#else
                        if ( codec->encode && codec->type == CODEC_TYPE_AUDIO )
+#endif
                        {
                                snprintf( key, sizeof(key), "%d", mlt_properties_count( codecs ) );
                                mlt_properties_set( codecs, key, codec->name );
@@ -257,7 +263,13 @@ static int consumer_start( mlt_consumer consumer )
                mlt_properties_set_data( properties, "vcodec", codecs, 0, (mlt_destructor) mlt_properties_close, NULL );
                mlt_properties_set_data( doc, "video_codecs", codecs, 0, NULL, NULL );
                while ( ( codec = av_codec_next( codec ) ) )
+#if LIBAVCODEC_VERSION_INT >= ((54<<16)+(56<<8)+100)
+                       if ( codec->encode2 && codec->type == CODEC_TYPE_VIDEO )
+#elif LIBAVCODEC_VERSION_INT >= ((54<<16)+(0<<8)+0)
+                       if ( (codec->encode || codec->encode2) && codec->type == CODEC_TYPE_VIDEO )
+#else
                        if ( codec->encode && codec->type == CODEC_TYPE_VIDEO )
+#endif
                        {
                                snprintf( key, sizeof(key), "%d", mlt_properties_count( codecs ) );
                                mlt_properties_set( codecs, key, codec->name );
@@ -334,11 +346,11 @@ static int consumer_start( mlt_consumer consumer )
                // Assign the thread to properties
                mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
 
-               // Set the running state
-               mlt_properties_set_int( properties, "running", 1 );
-
                // Create the thread
                pthread_create( thread, NULL, consumer_thread, consumer );
+
+               // Set the running state
+               mlt_properties_set_int( properties, "running", 1 );
        }
        return error;
 }
@@ -350,18 +362,18 @@ static int consumer_stop( mlt_consumer consumer )
 {
        // Get the properties
        mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
+       pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
 
        // Check that we're running
-       if ( mlt_properties_get_int( properties, "running" ) )
+       if ( thread )
        {
-               // Get the thread
-               pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
-
                // Stop the thread
                mlt_properties_set_int( properties, "running", 0 );
 
                // Wait for termination
                pthread_join( *thread, NULL );
+
+               mlt_properties_set_data( properties, "thread", NULL, 0, NULL, NULL );
        }
 
        return 0;
@@ -660,6 +672,7 @@ static int open_audio( mlt_properties properties, AVFormatContext *oc, AVStream
        else
        {
                mlt_log_warning( NULL, "%s: Unable to encode audio - disabling audio output.\n", __FILE__ );
+               audio_input_frame_size = 0;
        }
 
        return audio_input_frame_size;
@@ -932,11 +945,14 @@ static AVStream *add_video_stream( mlt_consumer consumer, AVFormatContext *oc, A
                                                mlt_log_fatal( MLT_CONSUMER_SERVICE( consumer ), "Could not allocate log buffer\n" );
                                        else
                                        {
-                                               size = fread( logbuffer, 1, size, f );
-                                               fclose( f );
-                                               logbuffer[size] = '\0';
-                                               c->stats_in = logbuffer;
+                                               if ( size >= 0 )
+                                               {
+                                                       size = fread( logbuffer, 1, size, f );
+                                                       logbuffer[size] = '\0';
+                                                       c->stats_in = logbuffer;
+                                               }
                                        }
+                                       fclose( f );
                                }
                        }
                }
@@ -1216,6 +1232,10 @@ static void *consumer_thread( void *arg )
                                acodec = mlt_properties_get( properties, "_acodec" );
                                audio_codec = avcodec_find_encoder_by_name( acodec );
                        }
+                       else if ( !strcmp( acodec, "aac" ) )
+                       {
+                               mlt_properties_set( properties, "astrict", "experimental" );
+                       }
                }
                else
                {
@@ -1393,7 +1413,17 @@ static void *consumer_thread( void *arg )
                        audio_input_frame_size = open_audio( properties, oc, audio_st[i], audio_outbuf_size,
                                acodec? acodec : NULL );
                        if ( !audio_input_frame_size )
+                       {
+                               // Remove the audio stream from the output context
+                               int j;
+                               for ( j = 0; j < oc->nb_streams; j++ )
+                               {
+                                       if ( oc->streams[j] == audio_st[i] )
+                                               av_freep( &oc->streams[j] );
+                               }
+                               --oc->nb_streams;
                                audio_st[i] = NULL;
+                       }
                }
 
                // Setup custom I/O if redirecting
@@ -1432,7 +1462,8 @@ static void *consumer_thread( void *arg )
 #endif
                        {
                                mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Could not open '%s'\n", filename );
-                               mlt_properties_set_int( properties, "running", 0 );
+                               mlt_events_fire( properties, "consumer-fatal-error", NULL );
+                               goto on_fatal_error;
                        }
                }
        
@@ -1448,7 +1479,8 @@ static void *consumer_thread( void *arg )
        else
        {
                mlt_log_error( MLT_CONSUMER_SERVICE( consumer ), "Invalid output format parameters\n" );
-               mlt_properties_set_int( properties, "running", 0 );
+               mlt_events_fire( properties, "consumer-fatal-error", NULL );
+               goto on_fatal_error;
        }
 #endif
 
@@ -1458,7 +1490,10 @@ static void *consumer_thread( void *arg )
 
        // Last check - need at least one stream
        if ( !audio_st[0] && !video_st )
-               mlt_properties_set_int( properties, "running", 0 );
+       {
+               mlt_events_fire( properties, "consumer-fatal-error", NULL );
+               goto on_fatal_error;
+       }
 
        // Get the starting time (can ignore the times above)
        gettimeofday( &ante, NULL );
@@ -1851,7 +1886,7 @@ static void *consumer_thread( void *arg )
                        av_init_packet( &pkt );
                        pkt.size = 0;
 
-                       if ( /*( c->capabilities & CODEC_CAP_SMALL_LAST_FRAME ) &&*/
+                       if ( fifo &&
                                ( channels * audio_input_frame_size < sample_fifo_used( fifo ) / sample_bytes ) )
                        {
                                sample_fifo_fetch( fifo, audio_buf_1, channels * audio_input_frame_size * sample_bytes );
@@ -1913,7 +1948,8 @@ static void *consumer_thread( void *arg )
 on_fatal_error:
        
        // Write the trailer, if any
-       av_write_trailer( oc );
+       if ( frames )
+               av_write_trailer( oc );
 
        // close each codec
        if ( video_st )
@@ -1926,13 +1962,13 @@ on_fatal_error:
                av_freep( &oc->streams[i] );
 
        // Close the output file
-       if ( !( fmt->flags & AVFMT_NOFILE ) )
+       if ( !( fmt->flags & AVFMT_NOFILE ) &&
+               !mlt_properties_get_int( properties, "redirect" ) )
        {
 #if LIBAVFORMAT_VERSION_MAJOR >= 53
-               if ( !mlt_properties_get_int( properties, "redirect" ) )
-                       avio_close( oc->pb );
+               if ( oc->pb  ) avio_close( oc->pb );
 #elif LIBAVFORMAT_VERSION_MAJOR >= 52
-               url_fclose( oc->pb );
+               if ( oc->pb  ) url_fclose( oc->pb );
 #else
                url_fclose( &oc->pb );
 #endif
@@ -1952,8 +1988,6 @@ on_fatal_error:
        av_free( oc );
 
        // Just in case we terminated on pause
-       mlt_properties_set_int( properties, "running", 0 );
-
        mlt_consumer_stopped( consumer );
        mlt_properties_close( frame_meta_properties );