diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
index db0bc82..f9f196a 100644
--- a/libavcodec/libmp3lame.c
+++ b/libavcodec/libmp3lame.c
@@ -28,12 +28,26 @@
 #include "mpegaudio.h"
 #include <lame/lame.h>
 
+#if (2147483647 == INT_MAX)
+#define av_lame_encode_buffer_int32 lame_encode_buffer_int
+typedef int av_lame_int32_t;
+#elif (2147483647L == LONG_MAX)
+#define av_lame_encode_buffer_int32 lame_encode_buffer_long2
+typedef long av_lame_int32_t;
+#endif
+
 #define BUFFER_SIZE (7200 + 2*MPA_FRAME_SIZE + MPA_FRAME_SIZE/4)
 typedef struct Mp3AudioContext {
     lame_global_flags *gfp;
     int stereo;
     uint8_t buffer[BUFFER_SIZE];
     int buffer_index;
+#ifdef av_lame_encode_buffer_int32
+    struct {
+        av_lame_int32_t *left;
+        av_lame_int32_t *right;
+    } s32_data;
+#endif
 } Mp3AudioContext;
 
 static av_cold int MP3lame_encode_init(AVCodecContext *avctx)
@@ -69,8 +83,26 @@ static av_cold int MP3lame_encode_init(AVCodecContext *avctx)
 
     avctx->frame_size = lame_get_framesize(s->gfp);
 
-    avctx->coded_frame= avcodec_alloc_frame();
+    if(!(avctx->coded_frame= avcodec_alloc_frame())) {
+        lame_close(s->gfp);
+
+        return AVERROR(ENOMEM);
+    }
     avctx->coded_frame->key_frame= 1;
+#ifdef av_lame_encode_buffer_int32
+    if(AV_SAMPLE_FMT_S32 == avctx->sample_fmt && s->stereo) {
+        int nelem = 2 * avctx->frame_size;
+
+        if(! (s->s32_data.left = av_malloc(nelem * sizeof(av_lame_int32_t)))) {
+            av_freep(&avctx->coded_frame);
+            lame_close(s->gfp);
+
+            return AVERROR(ENOMEM);
+        }
+
+        s->s32_data.right = s->s32_data.left + avctx->frame_size;
+    }
+#endif
 
     return 0;
 
@@ -146,31 +178,63 @@ static int MP3lame_encode_frame(AVCodecContext *avctx,
 
     /* lame 3.91 dies on '1-channel interleaved' data */
 
-    if(data){
+    if(!data){
+        lame_result= lame_encode_flush(
+                s->gfp,
+                s->buffer + s->buffer_index,
+                BUFFER_SIZE - s->buffer_index
+                );
+#ifdef av_lame_encode_buffer_int32
+    }else if(AV_SAMPLE_FMT_S32 == avctx->sample_fmt){
         if (s->stereo) {
-            lame_result = lame_encode_buffer_interleaved(
+            int32_t *rp = (int32_t *)data;
+            int32_t *mp = rp + 2*avctx->frame_size;
+            av_lame_int32_t *wpl = s->s32_data.left;
+            av_lame_int32_t *wpr = s->s32_data.right;
+
+            while (rp < mp) {
+                *wpl++ = *rp++;
+                *wpr++ = *rp++;
+            }
+
+            lame_result = av_lame_encode_buffer_int32(
                 s->gfp,
-                data,
+                s->s32_data.left,
+                s->s32_data.right,
                 avctx->frame_size,
                 s->buffer + s->buffer_index,
                 BUFFER_SIZE - s->buffer_index
                 );
         } else {
-            lame_result = lame_encode_buffer(
+            lame_result = av_lame_encode_buffer_int32(
                 s->gfp,
-                data,
-                data,
+                (av_lame_int32_t *)data,
+                (av_lame_int32_t *)data,
                 avctx->frame_size,
                 s->buffer + s->buffer_index,
                 BUFFER_SIZE - s->buffer_index
                 );
         }
+#endif
     }else{
-        lame_result= lame_encode_flush(
+        if (s->stereo) {
+            lame_result = lame_encode_buffer_interleaved(
                 s->gfp,
+                data,
+                avctx->frame_size,
                 s->buffer + s->buffer_index,
                 BUFFER_SIZE - s->buffer_index
                 );
+        } else {
+            lame_result = lame_encode_buffer(
+                s->gfp,
+                data,
+                data,
+                avctx->frame_size,
+                s->buffer + s->buffer_index,
+                BUFFER_SIZE - s->buffer_index
+                );
+        }
     }
 
     if(lame_result < 0){
@@ -206,6 +270,9 @@ static av_cold int MP3lame_encode_close(AVCodecContext *avctx)
 {
     Mp3AudioContext *s = avctx->priv_data;
 
+#ifdef av_lame_encode_buffer_int32
+    av_freep(&s->s32_data.left);
+#endif
     av_freep(&avctx->coded_frame);
 
     lame_close(s->gfp);
@@ -222,7 +289,11 @@ AVCodec ff_libmp3lame_encoder = {
     MP3lame_encode_frame,
     MP3lame_encode_close,
     .capabilities= CODEC_CAP_DELAY,
+#ifdef av_lame_encode_buffer_int32
+    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_S32,AV_SAMPLE_FMT_NONE},
+#else
     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
+#endif
     .supported_samplerates= sSampleRates,
     .long_name= NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
 };

