]> git.sesse.net Git - mlt/blobdiff - src/modules/avformat/producer_avformat.c
Fix abruptness when using a luma with softness in composite.
[mlt] / src / modules / avformat / producer_avformat.c
index 098fa5709b5a4b1b692308e7c2dac64306a0446e..7b28ac85cb64db4e3f8620800766ac51db342f5e 100644 (file)
@@ -41,6 +41,7 @@
 #include <pthread.h>
 
 #if LIBAVUTIL_VERSION_INT < (50<<16)
+#define PIX_FMT_RGB32 PIX_FMT_RGBA32
 #define PIX_FMT_YUYV422 PIX_FMT_YUV422
 #endif
 
@@ -611,6 +612,16 @@ static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
                        output.data, output.linesize);
                sws_freeContext( context );
        }
+       else if ( *format == mlt_image_rgb24a || *format == mlt_image_opengl )
+       {
+               struct SwsContext *context = sws_getContext( width, height, pix_fmt,
+                       width, height, PIX_FMT_RGBA, SWS_FAST_BILINEAR, NULL, NULL, NULL);
+               AVPicture output;
+               avpicture_fill( &output, buffer, PIX_FMT_RGBA, width, height );
+               sws_scale( context, frame->data, frame->linesize, 0, height,
+                       output.data, output.linesize);
+               sws_freeContext( context );
+       }
        else
        {
                struct SwsContext *context = sws_getContext( width, height, pix_fmt,
@@ -639,6 +650,12 @@ static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt,
                avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
                img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
        }
+       else if ( format == mlt_image_rgb24a || format == mlt_image_opengl )
+       {
+               AVPicture output;
+               avpicture_fill( &output, buffer, PIX_FMT_RGB32, width, height );
+               img_convert( &output, PIX_FMT_RGB32, (AVPicture *)frame, pix_fmt, width, height );
+       }
        else
        {
                AVPicture output;
@@ -673,6 +690,10 @@ static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *cod
                case mlt_image_rgb24:
                        size = *width * ( *height + 1 ) * 3;
                        break;
+               case mlt_image_rgb24a:
+               case mlt_image_opengl:
+                       size = *width * ( *height + 1 ) * 4;
+                       break;
                default:
                        *format = mlt_image_yuv422;
                        size = *width * ( *height + 1 ) * 2;
@@ -840,6 +861,14 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                                {
                                        int_position = req_position;
                                }
+                               mlt_log_debug( MLT_PRODUCER_SERVICE(this), "pkt.dts %llu req_pos %d cur_pos %d pkt_pos %d",
+                                       pkt.dts, req_position, current_position, int_position );
+                               // Make a dumb assumption on streams that contain wild timestamps
+                               if ( abs( req_position - int_position ) > 999 )
+                               {
+                                       int_position = req_position;
+                                       mlt_log_debug( MLT_PRODUCER_SERVICE(this), " WILD TIMESTAMP!" );
+                               }
                                mlt_properties_set_int( properties, "_last_position", int_position );
 
                                // Decode the image
@@ -863,8 +892,7 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                                                got_picture = 0;
                                        }
                                }
-                               mlt_log_debug( MLT_PRODUCER_SERVICE(this), "pkt.dts %llu req_pos %d cur_pos %d pkt_pos %d got_pic %d key %d\n",
-                                       pkt.dts, req_position, current_position, int_position, got_picture, pkt.flags & PKT_FLAG_KEY );
+                               mlt_log_debug( MLT_PRODUCER_SERVICE(this), " got_pic %d key %d\n", got_picture, pkt.flags & PKT_FLAG_KEY );
                                av_free_packet( &pkt );
                        }
                        else if ( ret >= 0 )
@@ -878,7 +906,8 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
                                if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
                                {
                                        convert_image( av_frame, *buffer, codec_context->pix_fmt, format, *width, *height );
-                                       mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
+                                       if ( !mlt_properties_get( properties, "force_progressive" ) )
+                                               mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
                                        mlt_properties_set_int( properties, "top_field_first", av_frame->top_field_first );
                                        mlt_properties_set_int( properties, "_current_position", int_position );
                                        mlt_properties_set_int( properties, "_got_picture", 1 );
@@ -1046,6 +1075,12 @@ static void producer_set_up_video( mlt_producer this, mlt_frame frame )
                        // Determine the fps
                        source_fps = ( double )codec_context->time_base.den / ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num );
 
+                       // If the muxer reports a frame rate different than the codec
+                       double muxer_fps = av_q2d( context->streams[ index ]->r_frame_rate );
+                       if ( source_fps != muxer_fps )
+                               // Choose the lesser - the wrong tends to be off by some multiple of 10
+                               source_fps = muxer_fps < source_fps ? muxer_fps : source_fps;
+
                        // We'll use fps if it's available
                        if ( source_fps > 0 )
                                mlt_properties_set_double( properties, "source_fps", source_fps );
@@ -1059,6 +1094,8 @@ static void producer_set_up_video( mlt_producer this, mlt_frame frame )
                        mlt_properties_set_int( frame_properties, "real_width", codec_context->width );
                        mlt_properties_set_int( frame_properties, "real_height", codec_context->height );
                        mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
+                       if ( mlt_properties_get( properties, "force_progressive" ) )
+                               mlt_properties_set_int( frame_properties, "progressive", mlt_properties_get_int( properties, "force_progressive" ) );
 
                        mlt_frame_push_get_image( frame, producer_get_image );
                        mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
@@ -1077,7 +1114,7 @@ static void producer_set_up_video( mlt_producer this, mlt_frame frame )
 /** Get the audio from a frame.
 */
 
-static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
+static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
 {
        // Get the properties from the frame
        mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
@@ -1106,15 +1143,9 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
        // Obtain the resample context if it exists (not always needed)
        ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
 
-#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
-       // Get the format converter context if it exists
-       AVAudioConvert *convert = mlt_properties_get_data( properties, "audio_convert", NULL );
-#endif
-
        // Obtain the audio buffers
        int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
        int16_t *decode_buffer = mlt_properties_get_data( properties, "decode_buffer", NULL );
-       int16_t *convert_buffer = mlt_properties_get_data( properties, "convert_buffer", NULL );
 
        // Get amount of audio used
        int audio_used =  mlt_properties_get_int( properties, "_audio_used" );
@@ -1141,27 +1172,24 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
        if ( resample == NULL && codec_context->channels <= 2 )
        {
                // Create the resampler
+#if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
+               resample = av_audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate,
+                       SAMPLE_FMT_S16, codec_context->sample_fmt, 16, 10, 0, 0.8 );
+#else
                resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
+#endif
 
                // And store it on properties
                mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
        }
        else if ( resample == NULL )
        {
-               *channels = codec_context->channels;
-               *frequency = codec_context->sample_rate;
-       }
+               // TODO: uncomment and remove following line when full multi-channel support is ready
+               // *channels = codec_context->channels;
+               codec_context->request_channels = *channels;
 
-#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
-       // Check for audio format converter and create if necessary
-       // TODO: support higher resolutions than 16-bit.
-       if ( convert == NULL && codec_context->sample_fmt != SAMPLE_FMT_S16 )
-       {
-               // Create single channel converter for interleaved with no mixing matrix
-               convert = av_audio_convert_alloc( SAMPLE_FMT_S16, 1, codec_context->sample_fmt, 1, NULL, 0 );
-               mlt_properties_set_data( properties, "audio_convert", convert, 0, ( mlt_destructor )av_audio_convert_free, NULL );
+               *frequency = codec_context->sample_rate;
        }
-#endif
 
        // Check for audio buffer and create if necessary
        if ( audio_buffer == NULL )
@@ -1183,18 +1211,6 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
                mlt_properties_set_data( properties, "decode_buffer", decode_buffer, 0, ( mlt_destructor )av_free, NULL );
        }
 
-#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
-       // Check for format converter buffer and create if necessary
-       if ( resample && convert && convert_buffer == NULL )
-       {
-               // Allocate the audio buffer
-               convert_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
-
-               // And store it on properties for reuse
-               mlt_properties_set_data( properties, "convert_buffer", convert_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
-       }
-#endif
-
        // Seek if necessary
        if ( position != expected )
        {
@@ -1268,43 +1284,20 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
                                len -= ret;
                                ptr += ret;
 
-                               if ( data_size > 0 )
+                               if ( data_size > 0 && ( audio_used * *channels + data_size < AVCODEC_MAX_AUDIO_FRAME_SIZE ) )
                                {
-                                       int src_stride[6]= { av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 };
-                                       int dst_stride[6]= { av_get_bits_per_sample_format( SAMPLE_FMT_S16 ) / 8 };
-
                                        if ( resample )
                                        {
                                                int16_t *source = decode_buffer;
                                                int16_t *dest = &audio_buffer[ audio_used * *channels ];
-                                               int convert_samples = data_size / src_stride[0];
+                                               int convert_samples = data_size / av_get_bits_per_sample_format( codec_context->sample_fmt ) * 8 / codec_context->channels;
 
-#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
-                                               if ( convert )
-                                               {
-                                                       const void *src_buf[6] = { decode_buffer };
-                                                       void *dst_buf[6] = { convert_buffer };
-                                                       av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, convert_samples );
-                                                       source = convert_buffer;
-                                               }
-#endif
-                                               audio_used += audio_resample( resample, dest, source, convert_samples / codec_context->channels );
+                                               audio_used += audio_resample( resample, dest, source, convert_samples );
                                        }
                                        else
                                        {
-#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
-                                               if ( convert )
-                                               {
-                                                       const void *src_buf[6] = { decode_buffer };
-                                                       void *dst_buf[6] = { &audio_buffer[ audio_used * *channels ] };
-                                                       av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, data_size / src_stride[0] );
-                                               }
-                                               else
-#endif
-                                               {
-                                                       memcpy( &audio_buffer[ audio_used * *channels ], decode_buffer, data_size );
-                                               }
-                                               audio_used += data_size / *channels / src_stride[0];
+                                               memcpy( &audio_buffer[ audio_used * *channels ], decode_buffer, data_size );
+                                               audio_used += data_size / *channels / av_get_bits_per_sample_format( codec_context->sample_fmt ) * 8;
                                        }
 
                                        // Handle ignore
@@ -1335,8 +1328,10 @@ static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
                        av_free_packet( &pkt );
                }
 
-               *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
-               mlt_properties_set_data( frame_properties, "audio", *buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
+               int size = *samples * *channels * sizeof( int16_t );
+               *format = mlt_audio_s16;
+               *buffer = mlt_pool_alloc( size );
+               mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
 
                // Now handle the audio if we have enough
                if ( audio_used >= *samples )