]> git.sesse.net Git - vlc/blobdiff - modules/codec/speex.c
Added support for explicit HeV2 LOAS/LATM stream in packetizer.
[vlc] / modules / codec / speex.c
index 2dc0cfdde9c0ff6d7fbaddb12b05e38cafc2830f..af1ea6bd1ba4f231dea96c17adf299f99de01db9 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * speex.c: speex decoder/packetizer/encoder module making use of libspeex.
  *****************************************************************************
- * Copyright (C) 2003 the VideoLAN team
+ * Copyright (C) 2003-2008 the VideoLAN team
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_input.h>
 #include <vlc_codec.h>
 #include <vlc_aout.h>
 
 #include <assert.h>
 
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+static int  OpenDecoder   ( vlc_object_t * );
+static int  OpenPacketizer( vlc_object_t * );
+static void CloseDecoder  ( vlc_object_t * );
+static int OpenEncoder   ( vlc_object_t * );
+static void CloseEncoder ( vlc_object_t * );
+
+#define ENC_CFG_PREFIX "sout-speex-"
+
+#define ENC_MODE_TEXT N_("Mode" )
+#define ENC_MODE_LONGTEXT N_( \
+    "Enforce the mode of the encoder." )
+
+#define ENC_QUALITY_TEXT N_("Encoding quality")
+#define ENC_QUALITY_LONGTEXT N_( \
+    "Enforce a quality between 0 (low) and 10 (high)." )
+
+#define ENC_COMPLEXITY_TEXT N_("Encoding complexity" )
+#define ENC_COMPLEXITY_LONGTEXT N_( \
+    "Enforce the complexity of the encoder." )
+
+#define ENC_MAXBITRATE_TEXT N_( "Maximal bitrate" )
+#define ENC_MAXBITRATE_LONGTEXT N_( \
+    "Enforce the maximal VBR bitrate" )
+
+#define ENC_CBR_TEXT N_( "CBR encoding" )
+#define ENC_CBR_LONGTEXT N_( \
+    "Enforce a constant bitrate encoding (CBR) instead of default " \
+    "variable bitrate encoding (VBR)." )
+
+#define ENC_VAD_TEXT N_( "Voice activity detection" )
+#define ENC_VAD_LONGTEXT N_( \
+    "Enable voice activity detection (VAD). It is automatically " \
+    "activated in VBR mode." )
+
+#define ENC_DTX_TEXT N_( "Discontinuous Transmission" )
+#define ENC_DTX_LONGTEXT N_( \
+    "Enable discontinuous transmission (DTX)." )
+
+static const int pi_enc_mode_values[] = { 0, 1, 2 };
+static const char * const ppsz_enc_mode_descriptions[] = {
+    N_("Narrow-band (8kHz)"), N_("Wide-band (16kHz)"), N_("Ultra-wideband (32kHz)"), NULL
+};
+
+vlc_module_begin ()
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_ACODEC )
+
+    set_description( N_("Speex audio decoder") )
+    set_capability( "decoder", 100 )
+    set_shortname( N_("Speex") )
+    set_callbacks( OpenDecoder, CloseDecoder )
+
+    add_submodule ()
+    set_description( N_("Speex audio packetizer") )
+    set_capability( "packetizer", 100 )
+    set_callbacks( OpenPacketizer, CloseDecoder )
+
+    add_submodule ()
+    set_description( N_("Speex audio encoder") )
+    set_capability( "encoder", 100 )
+    set_callbacks( OpenEncoder, CloseEncoder )
+
+    add_integer( ENC_CFG_PREFIX "mode", 0, NULL, ENC_MODE_TEXT,
+                 ENC_MODE_LONGTEXT, false )
+        change_integer_list( pi_enc_mode_values, ppsz_enc_mode_descriptions, NULL )
+
+    add_integer( ENC_CFG_PREFIX "complexity", 3, NULL, ENC_COMPLEXITY_TEXT,
+                 ENC_COMPLEXITY_LONGTEXT, false )
+        change_integer_range( 1, 10 )
+
+    add_bool( ENC_CFG_PREFIX "cbr", false, NULL, ENC_CBR_TEXT,
+                 ENC_CBR_LONGTEXT, false )
+
+    add_float( ENC_CFG_PREFIX "quality", 8.0, NULL, ENC_QUALITY_TEXT,
+               ENC_QUALITY_LONGTEXT, false )
+        change_float_range( 0.0, 10.0 )
+
+    add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBITRATE_TEXT,
+                 ENC_MAXBITRATE_LONGTEXT, false )
+
+    add_bool( ENC_CFG_PREFIX "vad", true, NULL, ENC_VAD_TEXT,
+                 ENC_VAD_LONGTEXT, false )
+
+    add_bool( ENC_CFG_PREFIX "dtx", false, NULL, ENC_DTX_TEXT,
+                 ENC_DTX_LONGTEXT, false )
+
+    /* TODO agc, noise suppression, */
+
+vlc_module_end ()
+
+static const char *const ppsz_enc_options[] = {
+    "mode", "complexity", "cbr", "quality", "max-bitrate", "vad", "dtx", NULL
+};
+
 /*****************************************************************************
  * decoder_sys_t : speex decoder descriptor
  *****************************************************************************/
 struct decoder_sys_t
 {
     /* Module mode */
-    vlc_bool_t b_packetizer;
+    bool b_packetizer;
 
     /*
      * Input properties
@@ -67,7 +169,7 @@ struct decoder_sys_t
 
 };
 
-static int pi_channels_maps[6] =
+static const int pi_channels_maps[6] =
 {
     0,
     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
@@ -81,9 +183,6 @@ static int pi_channels_maps[6] =
 /****************************************************************************
  * Local prototypes
  ****************************************************************************/
-static int  OpenDecoder   ( vlc_object_t * );
-static int  OpenPacketizer( vlc_object_t * );
-static void CloseDecoder  ( vlc_object_t * );
 
 static void *DecodeBlock  ( decoder_t *, block_t ** );
 static aout_buffer_t *DecodeRtpSpeexPacket( decoder_t *, block_t **);
@@ -92,36 +191,12 @@ static int  ProcessInitialHeader ( decoder_t *, ogg_packet * );
 static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
 
 static aout_buffer_t *DecodePacket( decoder_t *, ogg_packet * );
-static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
+static block_t *SendPacket( decoder_t *, block_t * );
 
 static void ParseSpeexComments( decoder_t *, ogg_packet * );
 
-static int OpenEncoder   ( vlc_object_t * );
-static void CloseEncoder ( vlc_object_t * );
 static block_t *Encode   ( encoder_t *, aout_buffer_t * );
 
-/*****************************************************************************
- * Module descriptor
- *****************************************************************************/
-vlc_module_begin();
-    set_category( CAT_INPUT );
-    set_subcategory( SUBCAT_INPUT_ACODEC );
-
-    set_description( _("Speex audio decoder") );
-    set_capability( "decoder", 100 );
-    set_callbacks( OpenDecoder, CloseDecoder );
-
-    add_submodule();
-    set_description( _("Speex audio packetizer") );
-    set_capability( "packetizer", 100 );
-    set_callbacks( OpenPacketizer, CloseDecoder );
-
-    add_submodule();
-    set_description( _("Speex audio encoder") );
-    set_capability( "encoder", 100 );
-    set_callbacks( OpenEncoder, CloseEncoder );
-vlc_module_end();
-
 /*****************************************************************************
  * OpenDecoder: probe the decoder and return score
  *****************************************************************************/
@@ -139,12 +214,9 @@ static int OpenDecoder( vlc_object_t *p_this )
     /* Allocate the memory needed to store the decoder's structure */
     if( ( p_dec->p_sys = p_sys =
           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
-    {
-        msg_Err( p_dec, "out of memory" );
-        return VLC_EGENERIC;
-    }
+        return VLC_ENOMEM;
     p_dec->p_sys->bits.buf_size = 0;
-    p_dec->p_sys->b_packetizer = VLC_FALSE;
+    p_dec->p_sys->b_packetizer = false;
     p_dec->p_sys->rtp_rate = p_dec->fmt_in.audio.i_rate;
 
     aout_DateSet( &p_sys->end_date, 0 );
@@ -190,7 +262,7 @@ static int OpenPacketizer( vlc_object_t *p_this )
 
     if( i_ret == VLC_SUCCESS )
     {
-        p_dec->p_sys->b_packetizer = VLC_TRUE;
+        p_dec->p_sys->b_packetizer = true;
         p_dec->fmt_out.i_codec = VLC_FOURCC('s','p','x',' ');
     }
 
@@ -353,7 +425,7 @@ static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
         msg_Err( p_dec, "cannot read Speex header" );
         return VLC_EGENERIC;
     }
-    if( p_header->mode >= SPEEX_NB_MODES )
+    if( p_header->mode >= SPEEX_NB_MODES || p_header->mode < 0 )
     {
         msg_Err( p_dec, "mode number %d does not (yet/any longer) exist in "
                  "this version of libspeex.", p_header->mode );
@@ -482,8 +554,8 @@ static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
             */
            speex_bits_rewind( &p_sys->bits );
            speex_bits_write( &p_sys->bits, 
-               p_new_block->p_buffer, 
-               i_bytes_in_speex_frame );
+               (char*)p_new_block->p_buffer, 
+                   (int)i_bytes_in_speex_frame );
 
            /*
             * Move the remaining part of the original packet (subsequent
@@ -502,11 +574,11 @@ static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
                 */
                i_bytes_in_speex_frame--;
                speex_bits_write( &p_sys->bits, 
-                   p_block->p_buffer, 
-                   p_block->i_buffer - i_bytes_in_speex_frame );
-               p_block = block_Realloc( p_block, 
+                       (char*)p_block->p_buffer, 
+                       p_block->i_buffer - i_bytes_in_speex_frame );
+            p_block = block_Realloc( p_block, 
                    0, 
-                   p_block->i_buffer-i_bytes_in_speex_frame );
+                       p_block->i_buffer-i_bytes_in_speex_frame );
                *pp_block = p_block;
            }
            else
@@ -515,11 +587,11 @@ static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
            }
 
            free( p_frame_holder );
-           return SendPacket( p_dec, p_oggpacket /*Not used*/, p_new_block);
+           return SendPacket( p_dec, p_new_block);
        }
        else
        {
-            return SendPacket( p_dec, p_oggpacket, p_block );
+            return SendPacket( p_dec, p_block );
        }
     }
     else
@@ -613,7 +685,7 @@ static aout_buffer_t *DecodeRtpSpeexPacket( decoder_t *p_dec, block_t **pp_block
       Ask for a new audio output buffer and make sure
       we get one. 
     */
-    p_aout_buffer = p_dec->pf_aout_buffer_new( p_dec, 
+    p_aout_buffer = decoder_NewAudioBuffer( p_dec, 
         p_sys->p_header->frame_size );
     if ( !p_aout_buffer || p_aout_buffer->i_nb_bytes == 0 )
     {
@@ -632,8 +704,8 @@ static aout_buffer_t *DecodeRtpSpeexPacket( decoder_t *p_dec, block_t **pp_block
       Decode the input and ensure that no errors 
       were encountered.
     */
-    i_decode_ret = 
-        speex_decode_int( p_sys->p_state,&p_sys->bits,p_aout_buffer->p_buffer );
+    i_decode_ret = speex_decode_int( p_sys->p_state, &p_sys->bits, 
+            (int16_t*)p_aout_buffer->p_buffer );
     if ( i_decode_ret < 0 )
     {
         msg_Err( p_dec, "Decoding failed. Perhaps we have a bad stream?" );
@@ -677,7 +749,7 @@ static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
             return NULL;
 
         p_aout_buffer =
-            p_dec->pf_aout_buffer_new( p_dec, p_sys->p_header->frame_size );
+            decoder_NewAudioBuffer( p_dec, p_sys->p_header->frame_size );
         if( !p_aout_buffer )
         {
             return NULL;
@@ -720,8 +792,7 @@ static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
 /*****************************************************************************
  * SendPacket: send an ogg packet to the stream output.
  *****************************************************************************/
-static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
-                            block_t *p_block )
+static block_t *SendPacket( decoder_t *p_dec, block_t *p_block )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
 
@@ -742,7 +813,7 @@ static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
 }
 
 /*****************************************************************************
- * ParseSpeexComments: FIXME should be done in demuxer
+ * ParseSpeexComments:
  *****************************************************************************/
 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
                            ((buf[base+2]<<16)&0xff0000)| \
@@ -751,40 +822,31 @@ static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
 
 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
 {
-    input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
     decoder_sys_t *p_sys = p_dec->p_sys;
-
-    char *p_buf = (char *)p_oggpacket->packet;
     const SpeexMode *p_mode;
-    int i_len;
-
-    if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
 
     assert( p_sys->p_header->mode < SPEEX_NB_MODES );
 
     p_mode = speex_mode_list[p_sys->p_header->mode];
     assert( p_mode != NULL );
 
-    input_Control( p_input, INPUT_ADD_INFO, _("Speex comment"), _("Mode"),
-                   "%s%s", p_mode->modeName,
-                   p_sys->p_header->vbr ? " VBR" : "" );
-
-    if( p_oggpacket->bytes < 8 )
+    if( !p_dec->p_description )
     {
-        msg_Err( p_dec, "invalid/corrupted comments" );
-        return;
+        p_dec->p_description = vlc_meta_New();
+        if( !p_dec->p_description )
+            return;
     }
 
-    i_len = readint( p_buf, 0 ); p_buf += 4;
-    if( i_len > p_oggpacket->bytes - 4 )
+    /* */
+    char *psz_mode;
+    if( asprintf( &psz_mode, "%s%s", p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" ) >= 0 )
     {
-        msg_Err( p_dec, "invalid/corrupted comments" );
-        return;
+        vlc_meta_AddExtra( p_dec->p_description, _("Mode"), psz_mode );
+        free( psz_mode );
     }
 
-    input_Control( p_input, INPUT_ADD_INFO, _("Speex comment"), p_buf, "" );
-
     /* TODO: finish comments parsing */
+    VLC_UNUSED( p_oggpacket );
 }
 
 /*****************************************************************************
@@ -801,7 +863,7 @@ static void CloseDecoder( vlc_object_t *p_this )
         speex_bits_destroy( &p_sys->bits );
     }
 
-    if( p_sys->p_header ) free( p_sys->p_header );
+    free( p_sys->p_header );
     free( p_sys );
 }
 
@@ -848,7 +910,7 @@ static int OpenEncoder( vlc_object_t *p_this )
     encoder_t *p_enc = (encoder_t *)p_this;
     encoder_sys_t *p_sys;
     const SpeexMode *p_speex_mode = &speex_nb_mode;
-    int i_quality, i;
+    int i_tmp, i;
     const char *pp_header[2];
     int pi_header[2];
     uint8_t *p_extra;
@@ -859,12 +921,26 @@ static int OpenEncoder( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
 
-    /* Allocate the memory needed to store the decoder's structure */
-    if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
+    config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
+    switch( var_GetInteger( p_enc, ENC_CFG_PREFIX "mode" ) )
     {
-        msg_Err( p_enc, "out of memory" );
-        return VLC_EGENERIC;
+    case 1:
+        msg_Dbg( p_enc, "Using wideband" );
+        p_speex_mode = &speex_wb_mode;
+        break;
+    case 2:
+        msg_Dbg( p_enc, "Using ultra-wideband" );
+        p_speex_mode = &speex_uwb_mode;
+        break;
+    default:
+        msg_Dbg( p_enc, "Using narrowband" );
+        p_speex_mode = &speex_nb_mode;
+        break;
     }
+
+    /* Allocate the memory needed to store the decoder's structure */
+    if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
+        return VLC_ENOMEM;
     p_enc->p_sys = p_sys;
     p_enc->pf_encode_audio = Encode;
     p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
@@ -874,15 +950,46 @@ static int OpenEncoder( vlc_object_t *p_this )
                        1, p_speex_mode );
 
     p_sys->header.frames_per_packet = 1;
-    p_sys->header.vbr = 1;
+    p_sys->header.vbr = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
     p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
 
     /* Create a new encoder state in narrowband mode */
     p_sys->p_state = speex_encoder_init( p_speex_mode );
 
-    /* Set the quality to 8 (15 kbps) */
-    i_quality = 8;
-    speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_quality );
+    /* Parameters */
+    i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "complexity" );
+    speex_encoder_ctl( p_sys->p_state, SPEEX_SET_COMPLEXITY, &i_tmp );
+
+    i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
+    speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR, &i_tmp );
+
+    if( i_tmp == 0 ) /* CBR */
+    {
+        i_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
+        speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_tmp );
+
+        i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "vad" ) ? 1 : 0;
+        speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VAD, &i_tmp );
+    }
+    else
+    {
+        float f_tmp;
+
+        f_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
+        speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_QUALITY, &f_tmp );
+
+        i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
+        if( i_tmp > 0 )
+#ifdef SPEEX_SET_VBR_MAX_BITRATE
+            speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_MAX_BITRATE, &i_tmp );
+#else
+            msg_Dbg( p_enc, "max-bitrate cannot be set in this version of libspeex");
+#endif
+    }
+
+    i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "dtx" ) ? 1 : 0;
+    speex_encoder_ctl( p_sys->p_state, SPEEX_SET_DTX, &i_tmp );
+
 
     /*Initialization of the structure that holds the bits*/
     speex_bits_init( &p_sys->bits );
@@ -1028,6 +1135,6 @@ static void CloseEncoder( vlc_object_t *p_this )
     speex_encoder_destroy( p_sys->p_state );
     speex_bits_destroy( &p_sys->bits );
 
-    if( p_sys->p_buffer ) free( p_sys->p_buffer );
+    free( p_sys->p_buffer );
     free( p_sys );
 }