[FFmpeg-devel] avcodec/cbs: use av_fast_malloc in case of unit write buffer overflow

Submitted by James Almer on May 2, 2018, 1:40 a.m.

Details

Message ID 20180502014035.10628-1-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer May 2, 2018, 1:40 a.m.
Use it instead of av_reallocp, as the contents of the existing buffer
are ultimately discarded.

Signed-off-by: James Almer <jamrial@gmail.com>
---
Barely any speed gain in most cases, but realloc of this data is
nonetheless unnecessary.

 libavcodec/cbs_h2645.c | 13 +++++++------
 libavcodec/cbs_h2645.h |  2 +-
 libavcodec/cbs_mpeg2.c | 13 +++++++------
 libavcodec/cbs_mpeg2.h |  2 +-
 libavcodec/cbs_vp9.c   | 15 ++++++++-------
 libavcodec/cbs_vp9.h   |  2 +-
 6 files changed, 25 insertions(+), 22 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index 64a1a2d1ee..ccb4e5190c 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -1235,19 +1235,20 @@  static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
     CodedBitstreamH2645Context *priv = ctx->priv_data;
     enum AVCodecID codec_id = ctx->codec->codec_id;
     PutBitContext pbc;
+    size_t write_buffer_size;
     int err;
 
     if (!priv->write_buffer) {
         // Initial write buffer size is 1MB.
-        priv->write_buffer_size = 1024 * 1024;
+        write_buffer_size = 1024 * 1024;
 
     reallocate_and_try_again:
-        err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
-        if (err < 0) {
+        av_fast_malloc(&priv->write_buffer, &priv->write_buffer_size, write_buffer_size);
+        if (!priv->write_buffer) {
             av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
                    "sufficiently large write buffer (last attempt "
-                   "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
-            return err;
+                   "%"SIZE_SPECIFIER" bytes).\n", write_buffer_size);
+            return AVERROR(ENOMEM);
         }
     }
 
@@ -1260,7 +1261,7 @@  static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
 
     if (err == AVERROR(ENOSPC)) {
         // Overflow.
-        priv->write_buffer_size *= 2;
+        write_buffer_size = priv->write_buffer_size * 2;
         goto reallocate_and_try_again;
     }
     // Overflow but we didn't notice.
diff --git a/libavcodec/cbs_h2645.h b/libavcodec/cbs_h2645.h
index f4cf65bdde..29630ea7b5 100644
--- a/libavcodec/cbs_h2645.h
+++ b/libavcodec/cbs_h2645.h
@@ -36,7 +36,7 @@  typedef struct CodedBitstreamH2645Context {
 
     // Write buffer
     uint8_t *write_buffer;
-    size_t write_buffer_size;
+    unsigned write_buffer_size;
 } CodedBitstreamH2645Context;
 
 
diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 0df4234b12..c081a63737 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -298,19 +298,20 @@  static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
 {
     CodedBitstreamMPEG2Context *priv = ctx->priv_data;
     PutBitContext pbc;
+    size_t write_buffer_size;
     int err;
 
     if (!priv->write_buffer) {
         // Initial write buffer size is 1MB.
-        priv->write_buffer_size = 1024 * 1024;
+        write_buffer_size = 1024 * 1024;
 
     reallocate_and_try_again:
-        err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
-        if (err < 0) {
+        av_fast_malloc(&priv->write_buffer, &priv->write_buffer_size, write_buffer_size);
+        if (!priv->write_buffer) {
             av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
                    "sufficiently large write buffer (last attempt "
-                   "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
-            return err;
+                   "%"SIZE_SPECIFIER" bytes).\n", write_buffer_size);
+            return AVERROR(ENOMEM);
         }
     }
 
@@ -323,7 +324,7 @@  static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
 
     if (err == AVERROR(ENOSPC)) {
         // Overflow.
-        priv->write_buffer_size *= 2;
+        write_buffer_size = priv->write_buffer_size * 2;
         goto reallocate_and_try_again;
     }
     if (err < 0) {
diff --git a/libavcodec/cbs_mpeg2.h b/libavcodec/cbs_mpeg2.h
index 92caa99dc1..824e15b524 100644
--- a/libavcodec/cbs_mpeg2.h
+++ b/libavcodec/cbs_mpeg2.h
@@ -222,7 +222,7 @@  typedef struct CodedBitstreamMPEG2Context {
 
     // Write buffer.
     uint8_t *write_buffer;
-    size_t write_buffer_size;
+    unsigned write_buffer_size;
 } CodedBitstreamMPEG2Context;
 
 
diff --git a/libavcodec/cbs_vp9.c b/libavcodec/cbs_vp9.c
index 7498be4b73..05bf2da66b 100644
--- a/libavcodec/cbs_vp9.c
+++ b/libavcodec/cbs_vp9.c
@@ -514,19 +514,20 @@  static int cbs_vp9_write_unit(CodedBitstreamContext *ctx,
     CodedBitstreamVP9Context *priv = ctx->priv_data;
     VP9RawFrame *frame = unit->content;
     PutBitContext pbc;
+    size_t write_buffer_size;
     int err;
 
     if (!priv->write_buffer) {
         // Initial write buffer size is 1MB.
-        priv->write_buffer_size = 1024 * 1024;
+        write_buffer_size = 1024 * 1024;
 
     reallocate_and_try_again:
-        err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
-        if (err < 0) {
+        av_fast_malloc(&priv->write_buffer, &priv->write_buffer_size, write_buffer_size);
+        if (!priv->write_buffer) {
             av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
                    "sufficiently large write buffer (last attempt "
-                   "%zu bytes).\n", priv->write_buffer_size);
-            return err;
+                   "%"SIZE_SPECIFIER" bytes).\n", write_buffer_size);
+            return AVERROR(ENOMEM);
         }
     }
 
@@ -534,7 +535,7 @@  static int cbs_vp9_write_unit(CodedBitstreamContext *ctx,
 
     err = cbs_vp9_write_frame(ctx, &pbc, frame);
     if (err == AVERROR(ENOSPC)) {
-        priv->write_buffer_size *= 2;
+        write_buffer_size = priv->write_buffer_size * 2;
         goto reallocate_and_try_again;
     }
     if (err < 0)
@@ -550,7 +551,7 @@  static int cbs_vp9_write_unit(CodedBitstreamContext *ctx,
     if (frame->data) {
         if (unit->data_size + frame->data_size >
             priv->write_buffer_size) {
-            priv->write_buffer_size *= 2;
+            write_buffer_size = priv->write_buffer_size * 2;
             goto reallocate_and_try_again;
         }
 
diff --git a/libavcodec/cbs_vp9.h b/libavcodec/cbs_vp9.h
index 5b99c90c2e..b517e4345a 100644
--- a/libavcodec/cbs_vp9.h
+++ b/libavcodec/cbs_vp9.h
@@ -194,7 +194,7 @@  typedef struct CodedBitstreamVP9Context {
 
     // Write buffer.
     uint8_t *write_buffer;
-    size_t write_buffer_size;
+    unsigned write_buffer_size;
 } CodedBitstreamVP9Context;