[FFmpeg-devel] [PATCH 2/3] avcodec/flacenc: Don't copy buffer if avoidable

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Wed Dec 28 00:47:38 EET 2022


It is avoidable for 32-bit FLAC on little endian hardware.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/flacenc.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c
index bf5d12facb..adaaa77b79 100644
--- a/libavcodec/flacenc.c
+++ b/libavcodec/flacenc.c
@@ -1573,22 +1573,22 @@ static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
 
 static int update_md5_sum(FlacEncodeContext *s, const void *samples)
 {
-    const uint8_t *buf;
+    const uint8_t *buf = samples;
     int buf_size = s->frame.blocksize * s->channels *
                    ((s->avctx->bits_per_raw_sample + 7) / 8);
 
-    if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
+    if (HAVE_BIGENDIAN ||
+        s->avctx->bits_per_raw_sample > 16 && s->avctx->bits_per_raw_sample <= 24) {
         av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
         if (!s->md5_buffer)
             return AVERROR(ENOMEM);
+        buf = s->md5_buffer;
     }
 
     if (s->avctx->bits_per_raw_sample <= 16) {
-        buf = (const uint8_t *)samples;
 #if HAVE_BIGENDIAN
         s->bdsp.bswap16_buf((uint16_t *) s->md5_buffer,
                             samples, buf_size / 2);
-        buf = s->md5_buffer;
 #endif
     } else if (s->avctx->bits_per_raw_sample <= 24) {
         int i;
@@ -1599,16 +1599,12 @@ static int update_md5_sum(FlacEncodeContext *s, const void *samples)
             int32_t v = samples0[i] >> 8;
             AV_WL24(tmp + 3*i, v);
         }
-        buf = s->md5_buffer;
     } else {
         /* s->avctx->bits_per_raw_sample <= 32 */
-        int i;
-        const int32_t *samples0 = samples;
-        uint8_t *tmp            = s->md5_buffer;
-
-        for (i = 0; i < s->frame.blocksize * s->channels; i++)
-            AV_WL32(tmp + 4*i, samples0[i]);
-        buf = s->md5_buffer;
+#if HAVE_BIGENDIAN
+        s->bdsp.bswap_buf((uint32_t *) s->md5_buffer,
+                          samples, s->frame.blocksize * s->channels);
+#endif
     }
     av_md5_update(s->md5ctx, buf, buf_size);
 
-- 
2.34.1



More information about the ffmpeg-devel mailing list