diff mbox series

[FFmpeg-devel,18/18] avcodec/ac3enc: Avoid calculating the CRC multiple times

Message ID GV1P250MB07377F2B70D5B5B3D11B39EF8F002@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit 0458fdbeeda4e82986a00febebc519aadbb75e3d
Headers show
Series [FFmpeg-devel,01/17] avcodec/ac3enc: Don't presume ch_layout to be AV_CHANNEL_ORDER_NATIVE | 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 April 8, 2024, 12:56 a.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/ac3enc.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index f520e72fc0..49b0d4b956 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -1942,7 +1942,7 @@  static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
 static void output_frame_end(AC3EncodeContext *s)
 {
     const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
-    int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
+    int frame_size_58, pad_bytes, crc1, crc2, crc_inv;
     uint8_t *frame;
 
     frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1;
@@ -1958,7 +1958,7 @@  static void output_frame_end(AC3EncodeContext *s)
 
     if (s->eac3) {
         /* compute crc2 */
-        crc2_partial = av_crc(crc_ctx, 0, frame + 2, s->frame_size - 5);
+        crc2 = av_crc(crc_ctx, 0, frame + 2, s->frame_size - 4);
     } else {
         /* compute crc1 */
         /* this is not so easy because it is at the beginning of the data... */
@@ -1968,16 +1968,17 @@  static void output_frame_end(AC3EncodeContext *s)
         AV_WB16(frame + 2, crc1);
 
         /* compute crc2 */
-        crc2_partial = av_crc(crc_ctx, 0, frame + frame_size_58,
-                              s->frame_size - frame_size_58 - 3);
+        crc2 = av_crc(crc_ctx, 0, frame + frame_size_58,
+                      s->frame_size - frame_size_58 - 2);
     }
-    crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
+    crc2 = av_bswap16(crc2);
     /* ensure crc2 does not match sync word by flipping crcrsv bit if needed */
-    if (crc2 == 0x770B) {
+    if (crc2 == 0x0B77) {
+        /* The CRC generator polynomial is x^16 + x^15 + x^2 + 1,
+         * so xor'ing with 0x18005 does not affect the CRC. */
         frame[s->frame_size - 3] ^= 0x1;
-        crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
+        crc2                     ^= 0x8005;
     }
-    crc2 = av_bswap16(crc2);
     AV_WB16(frame + s->frame_size - 2, crc2);
 }