diff mbox series

[FFmpeg-devel,39/40] avcodec/ffv1: Simplify cleanup after allocation failure

Message ID 20200914052747.124118-23-andreas.rheinhardt@gmail.com
State Accepted
Commit fe63b3eedcb2b92b3e086676ebed470d9f0ed64d
Headers show
Series [FFmpeg-devel,01/16] avcodec/snowdec: Use ff_snow_common_init() directly | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Sept. 14, 2020, 5:27 a.m. UTC
Now that ff_ffv1_close() for both the FFV1 encoder and decoder,
the code contained therein can be used to free the partially allocated
slice contexts if allocating the slice contexts failed. One just has to
set the correct number of slice contexts on error. This allows to remove
the code for freeing partially allocated slice contexts in
ff_ffv1_init_slice_contexts().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/ffv1.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

Comments

Michael Niedermayer Sept. 16, 2020, 6:26 p.m. UTC | #1
On Mon, Sep 14, 2020 at 07:27:46AM +0200, Andreas Rheinhardt wrote:
> Now that ff_ffv1_close() for both the FFV1 encoder and decoder,
> the code contained therein can be used to free the partially allocated
> slice contexts if allocating the slice contexts failed. One just has to
> set the correct number of slice contexts on error. This allows to remove
> the code for freeing partially allocated slice contexts in
> ff_ffv1_init_slice_contexts().
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/ffv1.c | 16 ++++------------
>  1 file changed, 4 insertions(+), 12 deletions(-)

LGTM

thx

[...]
diff mbox series

Patch

diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c
index 5b52849400..1c580c3b49 100644
--- a/libavcodec/ffv1.c
+++ b/libavcodec/ffv1.c
@@ -119,7 +119,7 @@  av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
 
     av_assert0(max_slice_count > 0);
 
-    for (i = 0; i < max_slice_count; i++) {
+    for (i = 0; i < max_slice_count;) {
         int sx          = i % f->num_h_slices;
         int sy          = i / f->num_h_slices;
         int sxs         = f->avctx->width  *  sx      / f->num_h_slices;
@@ -131,7 +131,7 @@  av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
         if (!fs)
             goto memfail;
 
-        f->slice_context[i] = fs;
+        f->slice_context[i++] = fs;
         memcpy(fs, f, sizeof(*fs));
         memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
 
@@ -144,22 +144,14 @@  av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
                                       sizeof(*fs->sample_buffer));
         fs->sample_buffer32 = av_malloc_array((fs->width + 6), 3 * MAX_PLANES *
                                         sizeof(*fs->sample_buffer32));
-        if (!fs->sample_buffer || !fs->sample_buffer32) {
-            av_freep(&fs->sample_buffer);
-            av_freep(&fs->sample_buffer32);
-            av_freep(&f->slice_context[i]);
+        if (!fs->sample_buffer || !fs->sample_buffer32)
             goto memfail;
-        }
     }
     f->max_slice_count = max_slice_count;
     return 0;
 
 memfail:
-    while(--i >= 0) {
-        av_freep(&f->slice_context[i]->sample_buffer);
-        av_freep(&f->slice_context[i]->sample_buffer32);
-        av_freep(&f->slice_context[i]);
-    }
+    f->max_slice_count = i;
     return AVERROR(ENOMEM);
 }