Index: configure
===================================================================
--- configure	(revision 13076)
+++ configure	(working copy)
@@ -137,6 +137,7 @@
   #echo "  --disable-decoders       disables all decoders"
   #echo "  --disable-muxers         disables all muxers"
   #echo "  --disable-demuxers       disables all demuxers"
+  echo "  --disable-lame           disable LAME MP3 support"
   echo "  --disable-lirc           disable lirc support (Infrared Remotes)"
   echo "  --disable-joystick-menu  disable joystick menu"
   echo "  --disable-firewire       disable support for FireWire cable boxes"
@@ -670,6 +671,7 @@
 ivtv="yes"
 dvb="no"
 dvb_path="/usr/include"
+lamemp3="yes"
 lirc="yes"
 appleremote="no"
 joystick_menu="no"
@@ -1392,6 +1394,10 @@
   ;;
   --disable-ivtv) ivtv="no"
   ;;
+  --enable-lame) lamemp3="yes"
+  ;;
+  --disable-lame) lamemp3="no"
+  ;;
   --enable-lirc) lirc="yes"
   ;;
   --disable-lirc) lirc="no"
@@ -2728,18 +2734,21 @@
     fi
 fi
 
-lamemp3="no"
-if has_library libmp3lame ; then
-    if has_header lame/lame.h ; then
-        lamemp3="yes"
+if test x"$lamemp3" = x"yes"; then
+    if ! has_library libmp3lame || ! has_header lame/lame.h ; then
+        echo "You must have the Lame MP3 encoding library installed to compile Myth."
+        exit 255
     fi
+else
+    if ! has_library libmad || ! has_header mad.h ; then
+        echo "You must have the libmad MP3 decoding library installed to compile"
+        echo "Myth without Lame MP3 encoding support."
+        exit 255
+    else
+        echo "Myth will be built without MP3 encoding support!"
+    fi
 fi
 
-if test x"$lamemp3" = x"no" ; then
-    echo "You must have the Lame MP3 encoding library installed to compile Myth."
-    exit 255
-fi
-
 #test for lirc client libraries
 if test x"$lirc" = x"yes" ; then
     lirc="no"
@@ -3413,6 +3422,15 @@
     CCONFIG="$CCONFIG using_iptv using_live"
 fi
 
+if test x"$lamemp3" = x"yes" ; then
+  CCONFIG="$CCONFIG using_lame"
+  echo "#define LAME 1" >> $TMPH
+  echo "CONFIG_MP3_LIBS=-lmp3lame" >> $MYTH_CONFIG_MAK
+else
+  CCONFIG="$CCONFIG using_mad"
+  echo "CONFIG_MP3_LIBS=-lmad" >> $MYTH_CONFIG_MAK
+fi
+
 if test x"$lirc" = x"yes" ; then
   CCONFIG="$CCONFIG using_lirc"
   echo "CONFIG_LIRC_LIBS=-llirc_client" >> $MYTH_CONFIG_MAK
Index: libs/libmythtv/nuppeldecoder.cpp
===================================================================
--- libs/libmythtv/nuppeldecoder.cpp	(revision 13076)
+++ libs/libmythtv/nuppeldecoder.cpp	(working copy)
@@ -30,7 +30,10 @@
 
 NuppelDecoder::NuppelDecoder(NuppelVideoPlayer *parent, ProgramInfo *pginfo)
     : DecoderBase(parent, pginfo),
-      gf(0), rtjd(0), video_width(0), video_height(0), video_size(0),
+#ifdef LAME
+      gf(0),
+#endif
+      rtjd(0), video_width(0), video_height(0), video_size(0),
       video_frame_rate(0.0f), audio_samplerate(44100), 
 #ifdef WORDS_BIGENDIAN
       audio_bits_per_sample(0),
@@ -56,10 +59,26 @@
     getrawframes = false;
     getrawvideo = false;
 
+#ifdef LAME
     gf = lame_init();
     lame_set_decode_only(gf, 1);
     lame_decode_init();
     lame_init_params(gf);
+#else
+    mad_stream_init(&madstream);
+    mad_frame_init(&madframe);
+    mad_synth_init(&madsynth);
+/* large enough to hold two audio frames */
+#define MAD_BUFFER_SIZE (5*8192)
+    madbuffer = new unsigned char[MAD_BUFFER_SIZE];
+    if (!madbuffer)
+    {
+        VERBOSE(VB_IMPORTANT, "NuppelDecoder: allocating mad buffer failed, "
+                                                                "aborting");
+        errored = true;
+        return;
+    }
+#endif
 
     rtjd = new RTjpeg();
     int format = RTJ_YUV420;
@@ -80,8 +99,16 @@
 
 NuppelDecoder::~NuppelDecoder()
 {
+#ifdef LAME
     if (gf)
         lame_close(gf);
+#else
+    mad_stream_finish(&madstream);
+    mad_frame_finish(&madframe);
+    mad_synth_finish(&madsynth);
+    if (madbuffer)
+        delete [] madbuffer;
+#endif
     if (rtjd)
         delete rtjd;
     if (ffmpeg_extradata)
@@ -916,6 +943,27 @@
     }
 }
 
+#ifndef LAME
+/* Libmad outputs 32bit samples instead of 16bit ones as expected by
+   AddAudioData(), so we have to scale. This is the simple scale() function
+   from the minimad.c example which is Copyright (C) 2000-2004 Underbit
+   Technologies, Inc. and licensed under GPL. */
+short int NuppelDecoder::MadScale(mad_fixed_t sample)
+{
+    /* round */
+    sample += (1L << (MAD_F_FRACBITS - 16));
+
+    /* clip */
+    if (sample >= MAD_F_ONE)
+        sample = MAD_F_ONE - 1;
+    else if (sample < -MAD_F_ONE)
+        sample = -MAD_F_ONE;
+
+     /* quantize */
+     return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+#endif
+
 // avignore = 0  : get audio and video
 //          = 1  : video only
 //          = -1 : neither, just parse
@@ -1117,11 +1165,11 @@
                 if (getrawframes)
                     StoreRawData(strm);
 
-                int lameret = 0;
                 short int pcmlbuffer[audio_samplerate * 4];
                 short int pcmrbuffer[audio_samplerate * 4];
                 int packetlen = frameheader.packetlength;
-
+#ifdef LAME
+                int lameret = 0;
                 do
                 {
                     lameret = lame_decode(strm, packetlen, pcmlbuffer,
@@ -1140,6 +1188,53 @@
                     }
                     packetlen = 0;
                 } while (lameret > 0);
+#else
+                size_t madremain = 0;
+                if (madstream.next_frame)
+                {
+                    madremain = madstream.bufend - madstream.next_frame;
+                    memmove(madbuffer, madstream.next_frame, madremain);
+                }
+
+                if (packetlen + madremain > MAD_BUFFER_SIZE)
+                {
+                    VERBOSE(VB_IMPORTANT, QString("mad buffer too small (%1 "
+                                   "vs. %2), exiting").arg(MAD_BUFFER_SIZE).
+                                                arg(packetlen + madremain));
+                    errored = true;
+                    return false;
+                }
+
+                memcpy(madbuffer + madremain, strm, packetlen);
+                mad_stream_buffer(&madstream, madbuffer,
+                                                     packetlen + madremain);
+
+                while (true)
+                {
+                    if (mad_frame_decode(&madframe, &madstream))
+                    {
+                        if (!MAD_RECOVERABLE(madstream.error))
+                            break;
+                        continue;
+                    }
+                    mad_synth_frame(&madsynth, &madframe);
+
+                    for (int samplenr = 0; samplenr < madsynth.pcm.length;
+                                                                  samplenr++)
+                    {
+                        pcmlbuffer[samplenr] =
+                                  MadScale(madsynth.pcm.samples[0][samplenr]);
+                        if (madsynth.pcm.channels == 2)
+                            pcmrbuffer[samplenr] =
+                                  MadScale(madsynth.pcm.samples[1][samplenr]);
+                        else
+                            pcmrbuffer[samplenr] = pcmlbuffer[samplenr];
+                    }
+
+                    GetNVP()->AddAudioData(pcmlbuffer, pcmrbuffer,
+                                  madsynth.pcm.length, frameheader.timecode);
+                }
+#endif
             }
             else
             {
Index: libs/libmythtv/NuppelVideoRecorder.cpp
===================================================================
--- libs/libmythtv/NuppelVideoRecorder.cpp	(revision 13076)
+++ libs/libmythtv/NuppelVideoRecorder.cpp	(working copy)
@@ -64,7 +64,11 @@
     pid = pid2 = 0;
     inputchannel = 1;
     compression = 1;
+#ifdef LAME
     compressaudio = 1;
+#else
+    compressaudio = 0;
+#endif
     usebttv = 1;
     w = 352;
     h = 240;
@@ -76,11 +80,14 @@
     framerate_multiplier = 1.0;
     height_multiplier = 1.0;
 
+#ifdef LAME
+    mp3buf = NULL;
     mp3quality = 3;
     gf = NULL;
+#endif
+
     rtjc = NULL;
     strm = NULL;   
-    mp3buf = NULL;
 
     transcoding = false;
 
@@ -176,10 +183,12 @@
     }
     if (rtjc)
         delete rtjc;
+#ifdef LAME
     if (mp3buf)
         delete [] mp3buf;
     if (gf)
         lame_close(gf);  
+#endif
     if (strm)
         delete [] strm;
     if (fd >= 0)
@@ -290,8 +299,10 @@
         hmjpg_vdecimation = value;
     else if (opt == "audiocompression")
         compressaudio = value;
+#ifdef LAME
     else if (opt == "mp3quality")
         mp3quality = value;
+#endif
     else if (opt == "samplerate")
         audio_samplerate = value;
     else if (opt == "audioframesize")
@@ -375,13 +386,16 @@
     if ((tmp = profile->byName("audiocodec")))
         setting = tmp->getValue();
 
+#ifdef LAME
     if (setting == "MP3")
     {
         SetOption("audiocompression", 1);
         SetIntOption(profile, "mp3quality");
         SetIntOption(profile, "samplerate");
     }
-    else if (setting == "Uncompressed")
+    else
+#endif
+         if (setting == "Uncompressed")
     {
         SetOption("audiocompression", 0);
         SetIntOption(profile, "samplerate");
@@ -697,6 +711,7 @@
 
     if (compressaudio)
     {
+#ifdef LAME
         gf = lame_init();
         lame_set_bWriteVbrTag(gf, 0);
         lame_set_quality(gf, mp3quality);
@@ -717,9 +732,17 @@
                     "AudioInit(): lame support requires 16bit audio");
             compressaudio = false;
         }
+#else
+        VERBOSE(VB_IMPORTANT, "NVR: AudioInit(): support for LAME MP3 "
+                                       "compression not enabled, disabling");
+        compressaudio = false;
+#endif
     }
+
+#ifdef LAME
     mp3buf_size = (int)(1.25 * 16384 + 7200);
     mp3buf = new char[mp3buf_size];
+#endif
 
     return 0; 
 }
@@ -1935,6 +1958,7 @@
         moredata.rtjpeg_chroma_filter = M2;
     }
 
+#ifdef LAME
     if (compressaudio)
     {
         moredata.audio_fourcc = FOURCC_LAME;
@@ -1942,6 +1966,7 @@
         moredata.audio_quality = mp3quality;
     }
     else
+#endif
     {
         moredata.audio_fourcc = FOURCC_RAWA;
     }
@@ -3188,6 +3213,7 @@
         }
     }
 
+#ifdef LAME
     if (compressaudio) 
     {
         char mp3gapless[7200];
@@ -3244,6 +3270,7 @@
         audiobytes += audio_buffer_size;
     } 
     else 
+#endif
     {
         frameheader.comptype = '0'; // uncompressed audio
         frameheader.packetlength = audio_buffer_size;
Index: libs/libmythtv/nuppeldecoder.h
===================================================================
--- libs/libmythtv/nuppeldecoder.h	(revision 13076)
+++ libs/libmythtv/nuppeldecoder.h	(working copy)
@@ -8,6 +8,7 @@
 #include "format.h"
 #include "decoderbase.h"
 
+#ifdef LAME
 #ifdef MMX
 #undef MMX
 #define MMXBLAH
@@ -16,6 +17,9 @@
 #ifdef MMXBLAH
 #define MMX
 #endif
+#else
+#include <mad.h>
+#endif
 
 #include "RTjpegN.h"
 
@@ -73,13 +77,24 @@
     void SeekReset(long long newKey = 0, uint skipFrames = 0,
                    bool needFlush = false, bool discardFrames = false);
 
+#ifndef LAME
+    short int MadScale(mad_fixed_t sample);
+#endif
+
     friend int get_nuppel_buffer(struct AVCodecContext *c, AVFrame *pic);
     friend void release_nuppel_buffer(struct AVCodecContext *c, AVFrame *pic);
 
     struct rtfileheader fileheader;
     struct rtframeheader frameheader;
 
+#ifdef LAME
     lame_global_flags *gf;
+#else
+    struct mad_stream madstream;
+    struct mad_frame madframe;
+    struct mad_synth madsynth;
+    unsigned char *madbuffer;
+#endif
     RTjpeg *rtjd;
 
     int video_width, video_height, video_size;
Index: libs/libmythtv/NuppelVideoRecorder.h
===================================================================
--- libs/libmythtv/NuppelVideoRecorder.h	(revision 13076)
+++ libs/libmythtv/NuppelVideoRecorder.h	(working copy)
@@ -5,6 +5,8 @@
 #include <sys/time.h>
 #include <time.h>
 #include <pthread.h>
+
+#ifdef LAME
 #ifdef MMX
 #undef MMX
 #define MMXBLAH
@@ -13,6 +15,7 @@
 #ifdef MMXBLAH
 #define MMX
 #endif
+#endif
 
 #include "filter.h"
 #include "minilzo.h"
@@ -166,10 +169,12 @@
 
     bool transcoding;
 
+#ifdef LAME
     int mp3quality;
     char *mp3buf;
     int mp3buf_size;
     lame_global_flags *gf;
+#endif
 
     RTjpeg *rtjc;
 
Index: libs/libmythtv/recordingprofile.cpp
===================================================================
--- libs/libmythtv/recordingprofile.cpp	(revision 13076)
+++ libs/libmythtv/recordingprofile.cpp	(working copy)
@@ -78,6 +78,7 @@
     }
 };
 
+#ifdef LAME
 class MP3Quality : public SliderSetting, public CodecParamStorage
 {
   public:
@@ -92,6 +93,7 @@
                     "numbers) requires more CPU."));
     };
 };
+#endif
 
 class BTTVVolume : public SliderSetting, public CodecParamStorage
 {
@@ -363,12 +365,16 @@
         addChild(codecName);
         setTrigger(codecName);
 
-        ConfigurationGroup* params = new VerticalConfigurationGroup(false);
+        ConfigurationGroup* params;
+
+#ifdef LAME
+        params = new VerticalConfigurationGroup(false);
         params->setLabel("MP3");
         params->addChild(new SampleRate(parent));
         params->addChild(new MP3Quality(parent));
         params->addChild(new BTTVVolume(parent));
         addTarget("MP3", params);
+#endif
 
         params = new VerticalConfigurationGroup(false, false, true, true);
         params->setLabel("MPEG-2 Hardware Encoder");
@@ -395,13 +401,17 @@
             else
             {
                 // V4L, TRANSCODE (and any undefined types)
+#ifdef LAME
                 codecName->addSelection("MP3");
+#endif
                 codecName->addSelection("Uncompressed");
             }
         }
         else
         {
+#ifdef LAME
             codecName->addSelection("MP3");
+#endif
             codecName->addSelection("Uncompressed");
             codecName->addSelection("MPEG-2 Hardware Encoder");
         }
Index: settings.pro
===================================================================
--- settings.pro	(revision 13076)
+++ settings.pro	(working copy)
@@ -88,7 +88,8 @@
 }
 QMAKE_LIBDIR_OPENGL =
 
-EXTRA_LIBS = $$FREETYPE_LIBS -lmp3lame
+EXTRA_LIBS = $$FREETYPE_LIBS
+EXTRA_LIBS += $$CONFIG_MP3_LIBS
 EXTRA_LIBS += $$CONFIG_AUDIO_OSS_LIBS
 EXTRA_LIBS += $$CONFIG_AUDIO_ALSA_LIBS
 EXTRA_LIBS += $$CONFIG_AUDIO_ARTS_LIBS
