diff mbox series

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

Message ID GV1P250MB0737FCD3267CA4FC2CD6945E8FED9@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State New
Headers show
Series [FFmpeg-devel,1/3] avcodec/bswapdsp: Don't presume src to be naturally aligned | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt Dec. 27, 2022, 10:47 p.m. UTC
It is avoidable for 32-bit FLAC on little endian hardware.

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

Patch

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);