[FFmpeg-cvslog] examples/muxing: fix memleaks in resampler

Ilya Basin git at videolan.org
Sat Dec 21 12:33:53 CET 2013


ffmpeg | branch: master | Ilya Basin <basinilya at gmail.com> | Mon Dec 16 13:08:34 2013 +0400| [d1b8e01ef1fe53281e91e0e89be626fc759a2524] | committer: Michael Niedermayer

examples/muxing: fix memleaks in resampler

  - do not allocate resample dst buffer when resample is off
  - free sample buffers in addition to freeing data pointer arrays

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=d1b8e01ef1fe53281e91e0e89be626fc759a2524
---

 doc/examples/muxing.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/doc/examples/muxing.c b/doc/examples/muxing.c
index d6e0256..4cd3f65 100644
--- a/doc/examples/muxing.c
+++ b/doc/examples/muxing.c
@@ -163,6 +163,11 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
         exit(1);
     }
 
+    /* compute the number of converted samples: buffering is avoided
+     * ensuring that the output buffer will contain at least all the
+     * converted input samples */
+    max_dst_nb_samples = src_nb_samples;
+
     /* create resampler context */
     if (c->sample_fmt != AV_SAMPLE_FMT_S16) {
         swr_ctx = swr_alloc();
@@ -184,17 +189,15 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
             fprintf(stderr, "Failed to initialize the resampling context\n");
             exit(1);
         }
-    }
 
-    /* compute the number of converted samples: buffering is avoided
-     * ensuring that the output buffer will contain at least all the
-     * converted input samples */
-    max_dst_nb_samples = src_nb_samples;
-    ret = av_samples_alloc_array_and_samples(&dst_samples_data, &dst_samples_linesize, c->channels,
-                                             max_dst_nb_samples, c->sample_fmt, 0);
-    if (ret < 0) {
-        fprintf(stderr, "Could not allocate destination samples\n");
-        exit(1);
+        ret = av_samples_alloc_array_and_samples(&dst_samples_data, &dst_samples_linesize, c->channels,
+                                                 max_dst_nb_samples, c->sample_fmt, 0);
+        if (ret < 0) {
+            fprintf(stderr, "Could not allocate destination samples\n");
+            exit(1);
+        }
+    } else {
+        dst_samples_data = src_samples_data;
     }
     dst_samples_size = av_samples_get_buffer_size(NULL, c->channels, max_dst_nb_samples,
                                                   c->sample_fmt, 0);
@@ -254,7 +257,6 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
             exit(1);
         }
     } else {
-        dst_samples_data[0] = src_samples_data[0];
         dst_nb_samples = src_nb_samples;
     }
 
@@ -287,8 +289,12 @@ freeframe:
 static void close_audio(AVFormatContext *oc, AVStream *st)
 {
     avcodec_close(st->codec);
+    if (dst_samples_data != src_samples_data) {
+        av_free(dst_samples_data[0]);
+        av_free(dst_samples_data);
+    }
     av_free(src_samples_data[0]);
-    av_free(dst_samples_data[0]);
+    av_free(src_samples_data);
 }
 
 /**************************************************************/



More information about the ffmpeg-cvslog mailing list