Allocate the buffer as needed instead of relying on an arbitrary constant that 
might not be enough.
---
 src/decoder/FfmpegDecoderPlugin.cxx | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)

diff --git a/src/decoder/FfmpegDecoderPlugin.cxx 
b/src/decoder/FfmpegDecoderPlugin.cxx
index b57ba3f..636d432 100644
--- a/src/decoder/FfmpegDecoderPlugin.cxx
+++ b/src/decoder/FfmpegDecoderPlugin.cxx
@@ -218,7 +218,7 @@ copy_interleave_frame2(uint8_t *dest, uint8_t **src,
 static int
 copy_interleave_frame(const AVCodecContext *codec_context,
                      const AVFrame *frame,
-                     uint8_t *buffer, size_t buffer_size)
+                     uint8_t **buffer)
 {
        int plane_size;
        const int data_size =
@@ -226,18 +226,19 @@ copy_interleave_frame(const AVCodecContext *codec_context,
                                           codec_context->channels,
                                           frame->nb_samples,
                                           codec_context->sample_fmt, 1);
-       if (buffer_size < (size_t)data_size)
-               /* buffer is too small - shouldn't happen */
-               return AVERROR(EINVAL);
+        *buffer = (uint8_t*)av_malloc(data_size);
+       if (!*buffer)
+               /* Not enough memory - shouldn't happen */
+               return AVERROR(ENOMEM);
 
        if (av_sample_fmt_is_planar(codec_context->sample_fmt) &&
            codec_context->channels > 1) {
-               copy_interleave_frame2(buffer, frame->extended_data,
+               copy_interleave_frame2(*buffer, frame->extended_data,
                                       frame->nb_samples,
                                       codec_context->channels,
                                       
av_get_bytes_per_sample(codec_context->sample_fmt));
        } else {
-               memcpy(buffer, frame->extended_data[0], data_size);
+               memcpy(*buffer, frame->extended_data[0], data_size);
        }
 
        return data_size;
@@ -256,26 +257,16 @@ ffmpeg_send_packet(struct decoder *decoder, struct 
input_stream *is,
 
        AVPacket packet2 = *packet;
 
-       uint8_t aligned_buffer[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2 + 16];
-       const size_t buffer_size = sizeof(aligned_buffer);
+       uint8_t* aligned_buffer = NULL;
 
        enum decoder_command cmd = DECODE_COMMAND_NONE;
        while (packet2.size > 0 &&
               cmd == DECODE_COMMAND_NONE) {
-               int audio_size = buffer_size;
+               int audio_size = 0;
                int got_frame = 0;
                int len = avcodec_decode_audio4(codec_context,
                                                frame, &got_frame,
                                                &packet2);
-               if (len >= 0 && got_frame) {
-                       audio_size = copy_interleave_frame(codec_context,
-                                                          frame,
-                                                          aligned_buffer,
-                                                          buffer_size);
-                       if (audio_size < 0)
-                               len = audio_size;
-               } else if (len >= 0)
-                       len = -1;
 
                if (len < 0) {
                        /* if error, we skip the frame */
@@ -283,6 +274,12 @@ ffmpeg_send_packet(struct decoder *decoder, struct 
input_stream *is,
                        break;
                }
 
+               if (got_frame) {
+                       audio_size = copy_interleave_frame(codec_context,
+                                                          frame,
+                                                          &aligned_buffer);
+               }
+
                packet2.data += len;
                packet2.size -= len;
 
@@ -292,6 +289,7 @@ ffmpeg_send_packet(struct decoder *decoder, struct 
input_stream *is,
                cmd = decoder_data(decoder, is,
                                   aligned_buffer, audio_size,
                                   codec_context->bit_rate / 1000);
+                av_freep(&aligned_buffer);
        }
        return cmd;
 }
-- 
1.8.3.2


------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead. 
Download for free and get started troubleshooting in minutes. 
http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
_______________________________________________
Musicpd-dev-team mailing list
Musicpd-dev-team@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/musicpd-dev-team

Reply via email to