]> git.sesse.net Git - mlt/commitdiff
Add mlt_audio_u8 (sourceforce-182).
authorDan Dennedy <dan@dennedy.org>
Sat, 22 Dec 2012 22:16:04 +0000 (14:16 -0800)
committerDan Dennedy <dan@dennedy.org>
Sat, 22 Dec 2012 22:16:04 +0000 (14:16 -0800)
It should support planar libavutil AV_SAMPLE_FMT_U8P, but it is untested
due to lacking a sample.

src/framework/mlt_consumer.c
src/framework/mlt_frame.c
src/framework/mlt_types.h
src/modules/avformat/consumer_avformat.c
src/modules/avformat/producer_avformat.c
src/modules/core/filter_audiochannels.c
src/modules/core/filter_audioconvert.c
src/modules/core/filter_channelcopy.c
src/modules/core/filter_mono.c

index 2a350a76abf8ecf4e529bfdcd2d9cee93a9b3bb3..652354320ef6bd7178e11d3a79cf9697b0d77e60 100644 (file)
@@ -669,6 +669,8 @@ static void *consumer_read_ahead_thread( void *arg )
                        afmt = mlt_audio_float;
                else if ( !strcmp( format, "f32le" ) )
                        afmt = mlt_audio_f32le;
+               else if ( !strcmp( format, "u8" ) )
+                       afmt = mlt_audio_u8;
        }
        int counter = 0;
        double fps = mlt_properties_get_double( properties, "fps" );
index 677a51610527f97ba6d94025d43c187a64ccc013..5787aed8d29a5ebb9fe1d459263cbdaa87a91333 100644 (file)
@@ -629,6 +629,7 @@ const char * mlt_audio_format_name( mlt_audio_format format )
                case mlt_audio_s32le:  return "s32le";
                case mlt_audio_float:  return "float";
                case mlt_audio_f32le:  return "f32le";
+               case mlt_audio_u8:     return "u8";
        }
        return "invalid";
 }
@@ -652,6 +653,7 @@ int mlt_audio_format_size( mlt_audio_format format, int samples, int channels )
                case mlt_audio_s32:    return samples * channels * sizeof( int32_t );
                case mlt_audio_f32le:
                case mlt_audio_float:  return samples * channels * sizeof( float );
+               case mlt_audio_u8:     return samples * channels;
        }
        return 0;
 }
index f242f12cc2b8bf2d58a4d69701f5938a24c86491..62442368ec55f7224e7b452702bda1c8587d9913 100644 (file)
@@ -44,7 +44,7 @@ typedef enum
        mlt_image_rgb24a,  /**< 8-bit RGB with alpha channel */
        mlt_image_yuv422,  /**< 8-bit YUV 4:2:2 packed */
        mlt_image_yuv420p, /**< 8-bit YUV 4:2:0 planar */
-       mlt_image_opengl   /**< suitable for OpenGL texture */
+       mlt_image_opengl  /**< (deprecated) suitable for OpenGL texture */
 }
 mlt_image_format;
 
@@ -58,7 +58,8 @@ typedef enum
        mlt_audio_s32,     /**< signed 32-bit non-interleaved PCM */
        mlt_audio_float,   /**< 32-bit non-interleaved floating point */
        mlt_audio_s32le,   /**< signed 32-bit interleaved PCM */
-       mlt_audio_f32le    /**< 32-bit interleaved floating point */
+       mlt_audio_f32le,   /**< 32-bit interleaved floating point */
+       mlt_audio_u8       /**< unsigned 8-bit interleaved PCM */
 }
 mlt_audio_format;
 
index 20b85eb86ea43e7f9305c4d9b4c69cc7242043b5..a93219fe9c6b259b1b0984112d96a8b4083cf1de 100644 (file)
@@ -474,11 +474,15 @@ static int get_mlt_audio_format( int av_sample_fmt )
 {
        switch ( av_sample_fmt )
        {
+       case AV_SAMPLE_FMT_U8:
+               return mlt_audio_u8;
        case AV_SAMPLE_FMT_S32:
                return mlt_audio_s32le;
        case AV_SAMPLE_FMT_FLT:
                return mlt_audio_f32le;
 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
+       case AV_SAMPLE_FMT_U8P:
+               return mlt_audio_u8;
        case AV_SAMPLE_FMT_S32P:
                return mlt_audio_s32le;
        case AV_SAMPLE_FMT_FLTP:
@@ -502,6 +506,8 @@ static int pick_sample_fmt( mlt_properties properties, AVCodec *codec )
                        sample_fmt = AV_SAMPLE_FMT_S32;
                else if ( !strcmp( format, "f32le" ) )
                        sample_fmt = AV_SAMPLE_FMT_FLT;
+               else if ( !strcmp( format, "u8" ) )
+                       sample_fmt = AV_SAMPLE_FMT_U8;
 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
                else if ( !strcmp( format, "s32" ) )
                        sample_fmt = AV_SAMPLE_FMT_S32P;
@@ -520,10 +526,12 @@ static int pick_sample_fmt( mlt_properties properties, AVCodec *codec )
        {
                switch (*p)
                {
+               case AV_SAMPLE_FMT_U8:
                case AV_SAMPLE_FMT_S16:
                case AV_SAMPLE_FMT_S32:
                case AV_SAMPLE_FMT_FLT:
 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
+               case AV_SAMPLE_FMT_U8P:
                case AV_SAMPLE_FMT_S16P:
                case AV_SAMPLE_FMT_S32P:
                case AV_SAMPLE_FMT_FLTP:
@@ -1639,13 +1647,13 @@ static void *consumer_thread( void *arg )
                                                                p = interleaved_to_planar( samples, channels, p, sizeof( int16_t ) );
                                                        else if ( codec->sample_fmt == AV_SAMPLE_FMT_S32P )
                                                                p = interleaved_to_planar( samples, channels, p, sizeof( int32_t ) );
+                                                       else if ( codec->sample_fmt == AV_SAMPLE_FMT_U8P )
+                                                               p = interleaved_to_planar( samples, channels, p, sizeof( uint8_t ) );
 #endif
                                                        pkt.size = avcodec_encode_audio( codec, audio_outbuf, audio_outbuf_size, p );
 
 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
-                                                       if ( codec->sample_fmt == AV_SAMPLE_FMT_FLTP
-                                                                || codec->sample_fmt == AV_SAMPLE_FMT_S16P
-                                                                || codec->sample_fmt == AV_SAMPLE_FMT_S32P )
+                                                       if ( p != audio_buf_1 )
                                                                mlt_pool_release( p );
 #endif
                                                }
@@ -1948,12 +1956,12 @@ static void *consumer_thread( void *arg )
                                        p = interleaved_to_planar( audio_input_frame_size, channels, p, sizeof( int16_t ) );
                                else if ( c->sample_fmt == AV_SAMPLE_FMT_S32P )
                                        p = interleaved_to_planar( audio_input_frame_size, channels, p, sizeof( int32_t ) );
+                               else if ( c->sample_fmt == AV_SAMPLE_FMT_U8P )
+                                       p = interleaved_to_planar( audio_input_frame_size, channels, p, sizeof( uint8_t ) );
 #endif
                                pkt.size = avcodec_encode_audio( c, audio_outbuf, audio_outbuf_size, p );
 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
-                               if ( c->sample_fmt == AV_SAMPLE_FMT_FLTP
-                                    || c->sample_fmt == AV_SAMPLE_FMT_S16P
-                                    || c->sample_fmt == AV_SAMPLE_FMT_S32P )
+                               if ( p != audio_buf_1 )
                                        mlt_pool_release( p );
 #endif
                        }
index 5d20c74adf361d13d663188e7e4fd0d5f4c4df94..14e1083ba9841c02d3e96b14c50fe2bda110367a 100644 (file)
@@ -1257,6 +1257,8 @@ static mlt_audio_format pick_audio_format( int sample_fmt )
        switch ( sample_fmt )
        {
        // interleaved
+       case AV_SAMPLE_FMT_U8:
+               return mlt_audio_u8;
        case AV_SAMPLE_FMT_S16:
                return mlt_audio_s16;
        case AV_SAMPLE_FMT_S32:
@@ -1265,6 +1267,8 @@ static mlt_audio_format pick_audio_format( int sample_fmt )
                return mlt_audio_f32le;
        // planar - this producer converts planar to interleaved
 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
+       case AV_SAMPLE_FMT_U8P:
+               return mlt_audio_u8;
        case AV_SAMPLE_FMT_S16P:
                return mlt_audio_s16;
        case AV_SAMPLE_FMT_S32P:
@@ -2247,6 +2251,7 @@ static int decode_audio( producer_avformat self, int *ignore, AVPacket pkt, int
                                switch ( codec_context->sample_fmt )
                                {
 #if LIBAVUTIL_VERSION_INT >= ((51<<16)+(17<<8)+0)
+                               case AV_SAMPLE_FMT_U8P:
                                case AV_SAMPLE_FMT_S16P:
                                case AV_SAMPLE_FMT_S32P:
                                case AV_SAMPLE_FMT_FLTP:
index 544d932b99b54f9dcd51f86222a573fb1273d1e7..ba8a85b27fe939935fa0cf9f160d8ddd50b0d078 100644 (file)
@@ -64,6 +64,19 @@ static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *f
                                }
                        }
                }
+               else if ( *format == mlt_audio_u8 )
+               {
+                       uint8_t *p = (uint8_t*) new_buffer;
+                       int i, j, k = 0;
+                       for ( i = 0; i < *samples; i++ )
+                       {
+                               for ( j = 0; j < *channels; j++ )
+                               {
+                                       p[ ( i * *channels ) + j ] = ((uint8_t*)(*buffer))[ ( i * channels_avail ) + k ];
+                                       k = ( k + 1 ) % channels_avail;
+                               }
+                       }
+               }
                else
                {
                        // non-interleaved - s32 or float
index 67b78ea61ef6d0f2b082906eea16b3aa703c3e5f..a38219286f7c10968b2589296f240b3e0d91d398 100644 (file)
@@ -109,6 +109,18 @@ static int convert_audio( mlt_frame frame, void **audio, mlt_audio_format *forma
                                error = 0;
                                break;
                        }
+                       case mlt_audio_u8:
+                       {
+                               uint8_t *buffer = mlt_pool_alloc( size );
+                               uint8_t *p = buffer;
+                               int16_t *q = (int16_t*) *audio;
+                               int i = samples * channels + 1;
+                               while ( --i )
+                                       *p++ = ( *q++ >> 8 ) + 128;
+                               *audio = buffer;
+                               error = 0;
+                               break;
+                       }
                        default:
                                break;
                        }
@@ -171,6 +183,19 @@ static int convert_audio( mlt_frame frame, void **audio, mlt_audio_format *forma
                                error = 0;
                                break;
                        }
+                       case mlt_audio_u8:
+                       {
+                               uint8_t *buffer = mlt_pool_alloc( size );
+                               uint8_t *p = buffer;
+                               int32_t *q = (int32_t*) *audio;
+                               int s, c;
+                               for ( s = 0; s < samples; s++ )
+                                       for ( c = 0; c < channels; c++ )
+                                               *p++ = ( q[c * samples + s] >> 24 ) + 128;
+                               *audio = buffer;
+                               error = 0;
+                               break;
+                       }
                        default:
                                break;
                        }
@@ -241,6 +266,23 @@ static int convert_audio( mlt_frame frame, void **audio, mlt_audio_format *forma
                                error = 0;
                                break;
                        }
+                       case mlt_audio_u8:
+                       {
+                               uint8_t *buffer = mlt_pool_alloc( size );
+                               uint8_t *p = buffer;
+                               float *q = (float*) *audio;
+                               int s, c;
+                               for ( s = 0; s < samples; s++ )
+                                       for ( c = 0; c < channels; c++ )
+                                       {
+                                               float f = *( q + c * samples + s );
+                                               f = f > 1.0 ? 1.0 : f < -1.0 ? -1.0 : f;
+                                               *p++ = ( 127 * f ) + 128;
+                                       }
+                               *audio = buffer;
+                               error = 0;
+                               break;
+                       }
                        default:
                                break;
                        }
@@ -310,6 +352,18 @@ static int convert_audio( mlt_frame frame, void **audio, mlt_audio_format *forma
                                error = 0;
                                break;
                        }
+                       case mlt_audio_u8:
+                       {
+                               uint8_t *buffer = mlt_pool_alloc( size );
+                               uint8_t *p = buffer;
+                               int32_t *q = (int32_t*) *audio;
+                               int i = samples * channels + 1;
+                               while ( --i )
+                                       *p++ = ( *q++ >> 24 ) + 128;
+                               *audio = buffer;
+                               error = 0;
+                               break;
+                       }
                        default:
                                break;
                        }
@@ -389,6 +443,107 @@ static int convert_audio( mlt_frame frame, void **audio, mlt_audio_format *forma
                                error = 0;
                                break;
                        }
+                       case mlt_audio_u8:
+                       {
+                               uint8_t *buffer = mlt_pool_alloc( size );
+                               uint8_t *p = buffer;
+                               float *q = (float*) *audio;
+                               int i = samples * channels + 1;
+                               while ( --i )
+                               {
+                                       float f = *q++;
+                                       f = f > 1.0 ? 1.0 : f < -1.0 ? -1.0 : f;
+                                       *p++ = ( 127 * f ) + 128;
+                               }
+                               *audio = buffer;
+                               error = 0;
+                               break;
+                       }
+                       default:
+                               break;
+                       }
+                       break;
+               case mlt_audio_u8:
+                       switch ( requested_format )
+                       {
+                       case mlt_audio_s32:
+                       {
+                               int32_t *buffer = mlt_pool_alloc( size );
+                               int32_t *p = buffer;
+                               int c;
+                               for ( c = 0; c < channels; c++ )
+                               {
+                                       uint8_t *q = (uint8_t*) *audio + c;
+                                       int i = samples + 1;
+                                       while ( --i )
+                                       {
+                                               *p++ = ( (int32_t) *q - 128 ) << 24;
+                                               q += channels;
+                                       }
+                               }
+                               *audio = buffer;
+                               error = 0;
+                               break;
+                       }
+                       case mlt_audio_float:
+                       {
+                               float *buffer = mlt_pool_alloc( size );
+                               float *p = buffer;
+                               int c;
+                               for ( c = 0; c < channels; c++ )
+                               {
+                                       uint8_t *q = (uint8_t*) *audio + c;
+                                       int i = samples + 1;
+                                       while ( --i )
+                                       {
+                                               *p++ = ( (float) *q - 128 ) / 256.0f;
+                                               q += channels;
+                                       }
+                               }
+                               *audio = buffer;
+                               error = 0;
+                               break;
+                       }
+                       case mlt_audio_s16:
+                       {
+                               int16_t *buffer = mlt_pool_alloc( size );
+                               int16_t *p = buffer;
+                               uint8_t *q = (uint8_t*) *audio;
+                               int i = samples * channels + 1;
+                               while ( --i )
+                                       *p++ = ( (int16_t) *q++ - 128 ) << 8;
+                               *audio = buffer;
+                               error = 0;
+                               break;
+                       }
+                       case mlt_audio_s32le:
+                       {
+                               int32_t *buffer = mlt_pool_alloc( size );
+                               int32_t *p = buffer;
+                               uint8_t *q = (uint8_t*) *audio;
+                               int i = samples * channels + 1;
+                               while ( --i )
+                                       *p++ = ( (int32_t) *q++ - 128 ) << 24;
+                               *audio = buffer;
+                               error = 0;
+                               break;
+                       }
+                       case mlt_audio_f32le:
+                       {
+                               float *buffer = mlt_pool_alloc( size );
+                               float *p = buffer;
+                               uint8_t *q = (uint8_t*) *audio;
+                               int i = samples * channels + 1;
+                               while ( --i )
+                               {
+                                       float f = ( (float) *q++ - 128 ) / 256.0f;
+                                       f = f > 1.0 ? 1.0 : f < -1.0 ? -1.0 : f;
+                                       *p++ = f;
+                               }
+                               *audio = buffer;
+                               error = 0;
+                               break;
+                       }
                        default:
                                break;
                        }
index be206f89a61c4a840b7982bd42ac9bc390c53bff..dc971f9128ab00e9a723126c98ab914c2347e8a7 100644 (file)
@@ -48,6 +48,25 @@ static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *f
        if ( from != to)
        switch ( *format )
        {
+               case mlt_audio_u8:
+               {
+                       uint8_t *f = (uint8_t*) *buffer + from;
+                       uint8_t *t = (uint8_t*) *buffer + to;
+                       uint8_t x;
+                       int i;
+
+                       if ( swap )
+                               for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
+                               {
+                                       x = *t;
+                                       *t = *f;
+                                       *f = x;
+                               }
+                       else
+                               for ( i = 0; i < *samples; i++, f += *channels, t += *channels )
+                                       *t = *f;
+                       break;
+               }
                case mlt_audio_s16:
                {
                        int16_t *f = (int16_t*) *buffer + from;
index 3d2eba71334d66b5576be9ee6be13962c4d4ee90..10952e13989c84e15b493539e6025835cf145d8b 100644 (file)
@@ -44,6 +44,20 @@ static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *f
 
        switch ( *format )
        {
+               case mlt_audio_u8:
+               {
+                       uint8_t *new_buffer = mlt_pool_alloc( size );
+                       for ( i = 0; i < *samples; i++ )
+                       {
+                               uint8_t mixdown = 0;
+                               for ( j = 0; j < *channels; j++ )
+                                       mixdown += ((uint8_t*) *buffer)[ ( i * *channels ) + j ] / *channels;
+                               for ( j = 0; j < channels_out; j++ )
+                                       new_buffer[ ( i * channels_out ) + j ] = mixdown;
+                       }
+                       *buffer = new_buffer;
+                       break;
+               }
                case mlt_audio_s16:
                {
                        int16_t *new_buffer = mlt_pool_alloc( size );